Merge tag 'ext4_for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tytso...
[cascardo/linux.git] / drivers / staging / lustre / lustre / obdclass / lprocfs_status.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2015, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/obdclass/lprocfs_status.c
33  *
34  * Author: Hariharan Thantry <thantry@users.sourceforge.net>
35  */
36
37 #define DEBUG_SUBSYSTEM S_CLASS
38
39 #include "../include/obd_class.h"
40 #include "../include/lprocfs_status.h"
41 #include "../include/lustre/lustre_idl.h"
42 #include <linux/seq_file.h>
43 #include <linux/ctype.h>
44
45 static const char * const obd_connect_names[] = {
46         "read_only",
47         "lov_index",
48         "connect_from_mds",
49         "write_grant",
50         "server_lock",
51         "version",
52         "request_portal",
53         "acl",
54         "xattr",
55         "create_on_write",
56         "truncate_lock",
57         "initial_transno",
58         "inode_bit_locks",
59         "join_file(obsolete)",
60         "getattr_by_fid",
61         "no_oh_for_devices",
62         "remote_client",
63         "remote_client_by_force",
64         "max_byte_per_rpc",
65         "64bit_qdata",
66         "mds_capability",
67         "oss_capability",
68         "early_lock_cancel",
69         "som",
70         "adaptive_timeouts",
71         "lru_resize",
72         "mds_mds_connection",
73         "real_conn",
74         "change_qunit_size",
75         "alt_checksum_algorithm",
76         "fid_is_enabled",
77         "version_recovery",
78         "pools",
79         "grant_shrink",
80         "skip_orphan",
81         "large_ea",
82         "full20",
83         "layout_lock",
84         "64bithash",
85         "object_max_bytes",
86         "imp_recov",
87         "jobstats",
88         "umask",
89         "einprogress",
90         "grant_param",
91         "flock_owner",
92         "lvb_type",
93         "nanoseconds_times",
94         "lightweight_conn",
95         "short_io",
96         "pingless",
97         "flock_deadlock",
98         "disp_stripe",
99         "unknown",
100         NULL
101 };
102
103 int obd_connect_flags2str(char *page, int count, __u64 flags, char *sep)
104 {
105         __u64 mask = 1;
106         int i, ret = 0;
107
108         for (i = 0; obd_connect_names[i]; i++, mask <<= 1) {
109                 if (flags & mask)
110                         ret += snprintf(page + ret, count - ret, "%s%s",
111                                         ret ? sep : "", obd_connect_names[i]);
112         }
113         if (flags & ~(mask - 1))
114                 ret += snprintf(page + ret, count - ret,
115                                 "%sunknown flags %#llx",
116                                 ret ? sep : "", flags & ~(mask - 1));
117         return ret;
118 }
119 EXPORT_SYMBOL(obd_connect_flags2str);
120
121 static void obd_connect_data_seqprint(struct seq_file *m,
122                                       struct obd_connect_data *ocd)
123 {
124         int flags;
125
126         LASSERT(ocd);
127         flags = ocd->ocd_connect_flags;
128
129         seq_printf(m, "    connect_data:\n"
130                    "       flags: %llx\n"
131                    "       instance: %u\n",
132                    ocd->ocd_connect_flags,
133                    ocd->ocd_instance);
134         if (flags & OBD_CONNECT_VERSION)
135                 seq_printf(m, "       target_version: %u.%u.%u.%u\n",
136                            OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
137                            OBD_OCD_VERSION_MINOR(ocd->ocd_version),
138                            OBD_OCD_VERSION_PATCH(ocd->ocd_version),
139                            OBD_OCD_VERSION_FIX(ocd->ocd_version));
140         if (flags & OBD_CONNECT_MDS)
141                 seq_printf(m, "       mdt_index: %d\n", ocd->ocd_group);
142         if (flags & OBD_CONNECT_GRANT)
143                 seq_printf(m, "       initial_grant: %d\n", ocd->ocd_grant);
144         if (flags & OBD_CONNECT_INDEX)
145                 seq_printf(m, "       target_index: %u\n", ocd->ocd_index);
146         if (flags & OBD_CONNECT_BRW_SIZE)
147                 seq_printf(m, "       max_brw_size: %d\n", ocd->ocd_brw_size);
148         if (flags & OBD_CONNECT_IBITS)
149                 seq_printf(m, "       ibits_known: %llx\n",
150                            ocd->ocd_ibits_known);
151         if (flags & OBD_CONNECT_GRANT_PARAM)
152                 seq_printf(m, "       grant_block_size: %d\n"
153                            "       grant_inode_size: %d\n"
154                            "       grant_extent_overhead: %d\n",
155                            ocd->ocd_blocksize,
156                            ocd->ocd_inodespace,
157                            ocd->ocd_grant_extent);
158         if (flags & OBD_CONNECT_TRANSNO)
159                 seq_printf(m, "       first_transno: %llx\n",
160                            ocd->ocd_transno);
161         if (flags & OBD_CONNECT_CKSUM)
162                 seq_printf(m, "       cksum_types: %#x\n",
163                            ocd->ocd_cksum_types);
164         if (flags & OBD_CONNECT_MAX_EASIZE)
165                 seq_printf(m, "       max_easize: %d\n", ocd->ocd_max_easize);
166         if (flags & OBD_CONNECT_MAXBYTES)
167                 seq_printf(m, "       max_object_bytes: %llx\n",
168                            ocd->ocd_maxbytes);
169 }
170
171 int lprocfs_read_frac_helper(char *buffer, unsigned long count, long val,
172                              int mult)
173 {
174         long decimal_val, frac_val;
175         int prtn;
176
177         if (count < 10)
178                 return -EINVAL;
179
180         decimal_val = val / mult;
181         prtn = snprintf(buffer, count, "%ld", decimal_val);
182         frac_val = val % mult;
183
184         if (prtn < (count - 4) && frac_val > 0) {
185                 long temp_frac;
186                 int i, temp_mult = 1, frac_bits = 0;
187
188                 temp_frac = frac_val * 10;
189                 buffer[prtn++] = '.';
190                 while (frac_bits < 2 && (temp_frac / mult) < 1) {
191                         /* only reserved 2 bits fraction */
192                         buffer[prtn++] = '0';
193                         temp_frac *= 10;
194                         frac_bits++;
195                 }
196                 /*
197                  * Need to think these cases :
198                  *      1. #echo x.00 > /sys/xxx       output result : x
199                  *      2. #echo x.0x > /sys/xxx       output result : x.0x
200                  *      3. #echo x.x0 > /sys/xxx       output result : x.x
201                  *      4. #echo x.xx > /sys/xxx       output result : x.xx
202                  *      Only reserved 2 bits fraction.
203                  */
204                 for (i = 0; i < (5 - prtn); i++)
205                         temp_mult *= 10;
206
207                 frac_bits = min((int)count - prtn, 3 - frac_bits);
208                 prtn += snprintf(buffer + prtn, frac_bits, "%ld",
209                                  frac_val * temp_mult / mult);
210
211                 prtn--;
212                 while (buffer[prtn] < '1' || buffer[prtn] > '9') {
213                         prtn--;
214                         if (buffer[prtn] == '.') {
215                                 prtn--;
216                                 break;
217                         }
218                 }
219                 prtn++;
220         }
221         buffer[prtn++] = '\n';
222         return prtn;
223 }
224 EXPORT_SYMBOL(lprocfs_read_frac_helper);
225
226 int lprocfs_write_frac_helper(const char __user *buffer, unsigned long count,
227                               int *val, int mult)
228 {
229         char kernbuf[20], *end, *pbuf;
230
231         if (count > (sizeof(kernbuf) - 1))
232                 return -EINVAL;
233
234         if (copy_from_user(kernbuf, buffer, count))
235                 return -EFAULT;
236
237         kernbuf[count] = '\0';
238         pbuf = kernbuf;
239         if (*pbuf == '-') {
240                 mult = -mult;
241                 pbuf++;
242         }
243
244         *val = (int)simple_strtoul(pbuf, &end, 10) * mult;
245         if (pbuf == end)
246                 return -EINVAL;
247
248         if (end && *end == '.') {
249                 int temp_val, pow = 1;
250                 int i;
251
252                 pbuf = end + 1;
253                 if (strlen(pbuf) > 5)
254                         pbuf[5] = '\0'; /*only allow 5bits fractional*/
255
256                 temp_val = (int)simple_strtoul(pbuf, &end, 10) * mult;
257
258                 if (pbuf < end) {
259                         for (i = 0; i < (end - pbuf); i++)
260                                 pow *= 10;
261
262                         *val += temp_val / pow;
263                 }
264         }
265         return 0;
266 }
267 EXPORT_SYMBOL(lprocfs_write_frac_helper);
268
269 static int lprocfs_no_percpu_stats;
270 module_param(lprocfs_no_percpu_stats, int, 0644);
271 MODULE_PARM_DESC(lprocfs_no_percpu_stats, "Do not alloc percpu data for lprocfs stats");
272
273 #define MAX_STRING_SIZE 128
274
275 int lprocfs_single_release(struct inode *inode, struct file *file)
276 {
277         return single_release(inode, file);
278 }
279 EXPORT_SYMBOL(lprocfs_single_release);
280
281 int lprocfs_seq_release(struct inode *inode, struct file *file)
282 {
283         return seq_release(inode, file);
284 }
285 EXPORT_SYMBOL(lprocfs_seq_release);
286
287 /* lprocfs API calls */
288
289 struct dentry *ldebugfs_add_simple(struct dentry *root,
290                                    char *name, void *data,
291                                    struct file_operations *fops)
292 {
293         struct dentry *entry;
294         umode_t mode = 0;
295
296         if (!root || !name || !fops)
297                 return ERR_PTR(-EINVAL);
298
299         if (fops->read)
300                 mode = 0444;
301         if (fops->write)
302                 mode |= 0200;
303         entry = debugfs_create_file(name, mode, root, data, fops);
304         if (IS_ERR_OR_NULL(entry)) {
305                 CERROR("LprocFS: No memory to create <debugfs> entry %s\n", name);
306                 return entry ?: ERR_PTR(-ENOMEM);
307         }
308         return entry;
309 }
310 EXPORT_SYMBOL_GPL(ldebugfs_add_simple);
311
312 static struct file_operations lprocfs_generic_fops = { };
313
314 int ldebugfs_add_vars(struct dentry *parent,
315                       struct lprocfs_vars *list,
316                       void *data)
317 {
318         if (IS_ERR_OR_NULL(parent) || IS_ERR_OR_NULL(list))
319                 return -EINVAL;
320
321         while (list->name) {
322                 struct dentry *entry;
323                 umode_t mode = 0;
324
325                 if (list->proc_mode != 0000) {
326                         mode = list->proc_mode;
327                 } else if (list->fops) {
328                         if (list->fops->read)
329                                 mode = 0444;
330                         if (list->fops->write)
331                                 mode |= 0200;
332                 }
333                 entry = debugfs_create_file(list->name, mode, parent,
334                                             list->data ?: data,
335                                             list->fops ?: &lprocfs_generic_fops
336                                            );
337                 if (IS_ERR_OR_NULL(entry))
338                         return entry ? PTR_ERR(entry) : -ENOMEM;
339                 list++;
340         }
341         return 0;
342 }
343 EXPORT_SYMBOL_GPL(ldebugfs_add_vars);
344
345 void ldebugfs_remove(struct dentry **entryp)
346 {
347         debugfs_remove_recursive(*entryp);
348         *entryp = NULL;
349 }
350 EXPORT_SYMBOL_GPL(ldebugfs_remove);
351
352 struct dentry *ldebugfs_register(const char *name,
353                                  struct dentry *parent,
354                                  struct lprocfs_vars *list, void *data)
355 {
356         struct dentry *entry;
357
358         entry = debugfs_create_dir(name, parent);
359         if (IS_ERR_OR_NULL(entry)) {
360                 entry = entry ?: ERR_PTR(-ENOMEM);
361                 goto out;
362         }
363
364         if (!IS_ERR_OR_NULL(list)) {
365                 int rc;
366
367                 rc = ldebugfs_add_vars(entry, list, data);
368                 if (rc != 0) {
369                         debugfs_remove(entry);
370                         entry = ERR_PTR(rc);
371                 }
372         }
373 out:
374         return entry;
375 }
376 EXPORT_SYMBOL_GPL(ldebugfs_register);
377
378 /* Generic callbacks */
379 int lprocfs_rd_uint(struct seq_file *m, void *data)
380 {
381         seq_printf(m, "%u\n", *(unsigned int *)data);
382         return 0;
383 }
384 EXPORT_SYMBOL(lprocfs_rd_uint);
385
386 int lprocfs_wr_uint(struct file *file, const char __user *buffer,
387                     unsigned long count, void *data)
388 {
389         unsigned *p = data;
390         char dummy[MAX_STRING_SIZE + 1], *end;
391         unsigned long tmp;
392
393         dummy[MAX_STRING_SIZE] = '\0';
394         if (copy_from_user(dummy, buffer, MAX_STRING_SIZE))
395                 return -EFAULT;
396
397         tmp = simple_strtoul(dummy, &end, 0);
398         if (dummy == end)
399                 return -EINVAL;
400
401         *p = (unsigned int)tmp;
402         return count;
403 }
404 EXPORT_SYMBOL(lprocfs_wr_uint);
405
406 static ssize_t uuid_show(struct kobject *kobj, struct attribute *attr,
407                          char *buf)
408 {
409         struct obd_device *obd = container_of(kobj, struct obd_device,
410                                               obd_kobj);
411
412         return sprintf(buf, "%s\n", obd->obd_uuid.uuid);
413 }
414 LUSTRE_RO_ATTR(uuid);
415
416 static ssize_t blocksize_show(struct kobject *kobj, struct attribute *attr,
417                               char *buf)
418 {
419         struct obd_device *obd = container_of(kobj, struct obd_device,
420                                               obd_kobj);
421         struct obd_statfs  osfs;
422         int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
423                             cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
424                             OBD_STATFS_NODELAY);
425         if (!rc)
426                 return sprintf(buf, "%u\n", osfs.os_bsize);
427
428         return rc;
429 }
430 LUSTRE_RO_ATTR(blocksize);
431
432 static ssize_t kbytestotal_show(struct kobject *kobj, struct attribute *attr,
433                                 char *buf)
434 {
435         struct obd_device *obd = container_of(kobj, struct obd_device,
436                                               obd_kobj);
437         struct obd_statfs  osfs;
438         int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
439                             cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
440                             OBD_STATFS_NODELAY);
441         if (!rc) {
442                 __u32 blk_size = osfs.os_bsize >> 10;
443                 __u64 result = osfs.os_blocks;
444
445                 while (blk_size >>= 1)
446                         result <<= 1;
447
448                 return sprintf(buf, "%llu\n", result);
449         }
450
451         return rc;
452 }
453 LUSTRE_RO_ATTR(kbytestotal);
454
455 static ssize_t kbytesfree_show(struct kobject *kobj, struct attribute *attr,
456                                char *buf)
457 {
458         struct obd_device *obd = container_of(kobj, struct obd_device,
459                                               obd_kobj);
460         struct obd_statfs  osfs;
461         int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
462                             cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
463                             OBD_STATFS_NODELAY);
464         if (!rc) {
465                 __u32 blk_size = osfs.os_bsize >> 10;
466                 __u64 result = osfs.os_bfree;
467
468                 while (blk_size >>= 1)
469                         result <<= 1;
470
471                 return sprintf(buf, "%llu\n", result);
472         }
473
474         return rc;
475 }
476 LUSTRE_RO_ATTR(kbytesfree);
477
478 static ssize_t kbytesavail_show(struct kobject *kobj, struct attribute *attr,
479                                 char *buf)
480 {
481         struct obd_device *obd = container_of(kobj, struct obd_device,
482                                               obd_kobj);
483         struct obd_statfs  osfs;
484         int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
485                             cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
486                             OBD_STATFS_NODELAY);
487         if (!rc) {
488                 __u32 blk_size = osfs.os_bsize >> 10;
489                 __u64 result = osfs.os_bavail;
490
491                 while (blk_size >>= 1)
492                         result <<= 1;
493
494                 return sprintf(buf, "%llu\n", result);
495         }
496
497         return rc;
498 }
499 LUSTRE_RO_ATTR(kbytesavail);
500
501 static ssize_t filestotal_show(struct kobject *kobj, struct attribute *attr,
502                                char *buf)
503 {
504         struct obd_device *obd = container_of(kobj, struct obd_device,
505                                               obd_kobj);
506         struct obd_statfs  osfs;
507         int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
508                             cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
509                             OBD_STATFS_NODELAY);
510         if (!rc)
511                 return sprintf(buf, "%llu\n", osfs.os_files);
512
513         return rc;
514 }
515 LUSTRE_RO_ATTR(filestotal);
516
517 static ssize_t filesfree_show(struct kobject *kobj, struct attribute *attr,
518                               char *buf)
519 {
520         struct obd_device *obd = container_of(kobj, struct obd_device,
521                                               obd_kobj);
522         struct obd_statfs  osfs;
523         int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
524                             cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
525                             OBD_STATFS_NODELAY);
526         if (!rc)
527                 return sprintf(buf, "%llu\n", osfs.os_ffree);
528
529         return rc;
530 }
531 LUSTRE_RO_ATTR(filesfree);
532
533 int lprocfs_rd_server_uuid(struct seq_file *m, void *data)
534 {
535         struct obd_device *obd = data;
536         struct obd_import *imp;
537         char *imp_state_name = NULL;
538         int rc;
539
540         LASSERT(obd);
541         rc = lprocfs_climp_check(obd);
542         if (rc)
543                 return rc;
544
545         imp = obd->u.cli.cl_import;
546         imp_state_name = ptlrpc_import_state_name(imp->imp_state);
547         seq_printf(m, "%s\t%s%s\n",
548                    obd2cli_tgt(obd), imp_state_name,
549                    imp->imp_deactive ? "\tDEACTIVATED" : "");
550
551         up_read(&obd->u.cli.cl_sem);
552
553         return 0;
554 }
555 EXPORT_SYMBOL(lprocfs_rd_server_uuid);
556
557 int lprocfs_rd_conn_uuid(struct seq_file *m, void *data)
558 {
559         struct obd_device *obd = data;
560         struct ptlrpc_connection *conn;
561         int rc;
562
563         LASSERT(obd);
564
565         rc = lprocfs_climp_check(obd);
566         if (rc)
567                 return rc;
568
569         conn = obd->u.cli.cl_import->imp_connection;
570         if (conn && obd->u.cli.cl_import)
571                 seq_printf(m, "%s\n", conn->c_remote_uuid.uuid);
572         else
573                 seq_puts(m, "<none>\n");
574
575         up_read(&obd->u.cli.cl_sem);
576
577         return 0;
578 }
579 EXPORT_SYMBOL(lprocfs_rd_conn_uuid);
580
581 /** add up per-cpu counters */
582 void lprocfs_stats_collect(struct lprocfs_stats *stats, int idx,
583                            struct lprocfs_counter *cnt)
584 {
585         unsigned int                    num_entry;
586         struct lprocfs_counter          *percpu_cntr;
587         int                             i;
588         unsigned long                   flags = 0;
589
590         memset(cnt, 0, sizeof(*cnt));
591
592         if (!stats) {
593                 /* set count to 1 to avoid divide-by-zero errs in callers */
594                 cnt->lc_count = 1;
595                 return;
596         }
597
598         cnt->lc_min = LC_MIN_INIT;
599
600         num_entry = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
601
602         for (i = 0; i < num_entry; i++) {
603                 if (!stats->ls_percpu[i])
604                         continue;
605                 percpu_cntr = lprocfs_stats_counter_get(stats, i, idx);
606
607                 cnt->lc_count += percpu_cntr->lc_count;
608                 cnt->lc_sum += percpu_cntr->lc_sum;
609                 if (percpu_cntr->lc_min < cnt->lc_min)
610                         cnt->lc_min = percpu_cntr->lc_min;
611                 if (percpu_cntr->lc_max > cnt->lc_max)
612                         cnt->lc_max = percpu_cntr->lc_max;
613                 cnt->lc_sumsquare += percpu_cntr->lc_sumsquare;
614         }
615
616         lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
617 }
618 EXPORT_SYMBOL(lprocfs_stats_collect);
619
620 /**
621  * Append a space separated list of current set flags to str.
622  */
623 #define flag2str(flag, first)                                           \
624         do {                                                            \
625                 if (imp->imp_##flag)                                    \
626                         seq_printf(m, "%s" #flag, first ? "" : ", ");   \
627         } while (0)
628 static int obd_import_flags2str(struct obd_import *imp, struct seq_file *m)
629 {
630         bool first = true;
631
632         if (imp->imp_obd->obd_no_recov) {
633                 seq_printf(m, "no_recov");
634                 first = false;
635         }
636
637         flag2str(invalid, first);
638         first = false;
639         flag2str(deactive, first);
640         flag2str(replayable, first);
641         flag2str(pingable, first);
642         return 0;
643 }
644
645 #undef flags2str
646
647 static void obd_connect_seq_flags2str(struct seq_file *m, __u64 flags, char *sep)
648 {
649         __u64 mask = 1;
650         int i;
651         bool first = true;
652
653         for (i = 0; obd_connect_names[i]; i++, mask <<= 1) {
654                 if (flags & mask) {
655                         seq_printf(m, "%s%s",
656                                    first ? sep : "", obd_connect_names[i]);
657                         first = false;
658                 }
659         }
660         if (flags & ~(mask - 1))
661                 seq_printf(m, "%sunknown flags %#llx",
662                            first ? sep : "", flags & ~(mask - 1));
663 }
664
665 int lprocfs_rd_import(struct seq_file *m, void *data)
666 {
667         char                            nidstr[LNET_NIDSTR_SIZE];
668         struct lprocfs_counter          ret;
669         struct lprocfs_counter_header   *header;
670         struct obd_device               *obd    = data;
671         struct obd_import               *imp;
672         struct obd_import_conn          *conn;
673         struct obd_connect_data *ocd;
674         int                             j;
675         int                             k;
676         int                             rw      = 0;
677         int                             rc;
678
679         LASSERT(obd);
680         rc = lprocfs_climp_check(obd);
681         if (rc)
682                 return rc;
683
684         imp = obd->u.cli.cl_import;
685         ocd = &imp->imp_connect_data;
686
687         seq_printf(m, "import:\n"
688                    "    name: %s\n"
689                    "    target: %s\n"
690                    "    state: %s\n"
691                    "    instance: %u\n"
692                    "    connect_flags: [ ",
693                    obd->obd_name,
694                    obd2cli_tgt(obd),
695                    ptlrpc_import_state_name(imp->imp_state),
696                    imp->imp_connect_data.ocd_instance);
697         obd_connect_seq_flags2str(m, imp->imp_connect_data.ocd_connect_flags,
698                                   ", ");
699         seq_printf(m, " ]\n");
700         obd_connect_data_seqprint(m, ocd);
701         seq_printf(m, "    import_flags: [ ");
702         obd_import_flags2str(imp, m);
703
704         seq_printf(m,
705                    " ]\n"
706                    "    connection:\n"
707                    "       failover_nids: [ ");
708         spin_lock(&imp->imp_lock);
709         j = 0;
710         list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
711                 libcfs_nid2str_r(conn->oic_conn->c_peer.nid,
712                                  nidstr, sizeof(nidstr));
713                 seq_printf(m, "%s%s", j ? ", " : "", nidstr);
714                 j++;
715         }
716         if (imp->imp_connection)
717                 libcfs_nid2str_r(imp->imp_connection->c_peer.nid,
718                                  nidstr, sizeof(nidstr));
719         else
720                 strncpy(nidstr, "<none>", sizeof(nidstr));
721         seq_printf(m,
722                    " ]\n"
723                    "       current_connection: %s\n"
724                    "       connection_attempts: %u\n"
725                    "       generation: %u\n"
726                    "       in-progress_invalidations: %u\n",
727                    nidstr,
728                    imp->imp_conn_cnt,
729                    imp->imp_generation,
730                    atomic_read(&imp->imp_inval_count));
731         spin_unlock(&imp->imp_lock);
732
733         if (!obd->obd_svc_stats)
734                 goto out_climp;
735
736         header = &obd->obd_svc_stats->ls_cnt_header[PTLRPC_REQWAIT_CNTR];
737         lprocfs_stats_collect(obd->obd_svc_stats, PTLRPC_REQWAIT_CNTR, &ret);
738         if (ret.lc_count != 0) {
739                 /* first argument to do_div MUST be __u64 */
740                 __u64 sum = ret.lc_sum;
741
742                 do_div(sum, ret.lc_count);
743                 ret.lc_sum = sum;
744         } else {
745                 ret.lc_sum = 0;
746         }
747         seq_printf(m,
748                    "    rpcs:\n"
749                    "       inflight: %u\n"
750                    "       unregistering: %u\n"
751                    "       timeouts: %u\n"
752                    "       avg_waittime: %llu %s\n",
753                    atomic_read(&imp->imp_inflight),
754                    atomic_read(&imp->imp_unregistering),
755                    atomic_read(&imp->imp_timeouts),
756                    ret.lc_sum, header->lc_units);
757
758         k = 0;
759         for (j = 0; j < IMP_AT_MAX_PORTALS; j++) {
760                 if (imp->imp_at.iat_portal[j] == 0)
761                         break;
762                 k = max_t(unsigned int, k,
763                           at_get(&imp->imp_at.iat_service_estimate[j]));
764         }
765         seq_printf(m,
766                    "    service_estimates:\n"
767                    "       services: %u sec\n"
768                    "       network: %u sec\n",
769                    k,
770                    at_get(&imp->imp_at.iat_net_latency));
771
772         seq_printf(m,
773                    "    transactions:\n"
774                    "       last_replay: %llu\n"
775                    "       peer_committed: %llu\n"
776                    "       last_checked: %llu\n",
777                    imp->imp_last_replay_transno,
778                    imp->imp_peer_committed_transno,
779                    imp->imp_last_transno_checked);
780
781         /* avg data rates */
782         for (rw = 0; rw <= 1; rw++) {
783                 lprocfs_stats_collect(obd->obd_svc_stats,
784                                       PTLRPC_LAST_CNTR + BRW_READ_BYTES + rw,
785                                       &ret);
786                 if (ret.lc_sum > 0 && ret.lc_count > 0) {
787                         /* first argument to do_div MUST be __u64 */
788                         __u64 sum = ret.lc_sum;
789
790                         do_div(sum, ret.lc_count);
791                         ret.lc_sum = sum;
792                         seq_printf(m,
793                                    "    %s_data_averages:\n"
794                                    "       bytes_per_rpc: %llu\n",
795                                    rw ? "write" : "read",
796                                    ret.lc_sum);
797                 }
798                 k = (int)ret.lc_sum;
799                 j = opcode_offset(OST_READ + rw) + EXTRA_MAX_OPCODES;
800                 header = &obd->obd_svc_stats->ls_cnt_header[j];
801                 lprocfs_stats_collect(obd->obd_svc_stats, j, &ret);
802                 if (ret.lc_sum > 0 && ret.lc_count != 0) {
803                         /* first argument to do_div MUST be __u64 */
804                         __u64 sum = ret.lc_sum;
805
806                         do_div(sum, ret.lc_count);
807                         ret.lc_sum = sum;
808                         seq_printf(m,
809                                    "       %s_per_rpc: %llu\n",
810                                    header->lc_units, ret.lc_sum);
811                         j = (int)ret.lc_sum;
812                         if (j > 0)
813                                 seq_printf(m,
814                                            "       MB_per_sec: %u.%.02u\n",
815                                            k / j, (100 * k / j) % 100);
816                 }
817         }
818
819 out_climp:
820         up_read(&obd->u.cli.cl_sem);
821         return 0;
822 }
823 EXPORT_SYMBOL(lprocfs_rd_import);
824
825 int lprocfs_rd_state(struct seq_file *m, void *data)
826 {
827         struct obd_device *obd = data;
828         struct obd_import *imp;
829         int j, k, rc;
830
831         LASSERT(obd);
832         rc = lprocfs_climp_check(obd);
833         if (rc)
834                 return rc;
835
836         imp = obd->u.cli.cl_import;
837
838         seq_printf(m, "current_state: %s\n",
839                    ptlrpc_import_state_name(imp->imp_state));
840         seq_printf(m, "state_history:\n");
841         k = imp->imp_state_hist_idx;
842         for (j = 0; j < IMP_STATE_HIST_LEN; j++) {
843                 struct import_state_hist *ish =
844                         &imp->imp_state_hist[(k + j) % IMP_STATE_HIST_LEN];
845                 if (ish->ish_state == 0)
846                         continue;
847                 seq_printf(m, " - [ %lld, %s ]\n", (s64)ish->ish_time,
848                            ptlrpc_import_state_name(ish->ish_state));
849         }
850
851         up_read(&obd->u.cli.cl_sem);
852         return 0;
853 }
854 EXPORT_SYMBOL(lprocfs_rd_state);
855
856 int lprocfs_at_hist_helper(struct seq_file *m, struct adaptive_timeout *at)
857 {
858         int i;
859
860         for (i = 0; i < AT_BINS; i++)
861                 seq_printf(m, "%3u ", at->at_hist[i]);
862         seq_printf(m, "\n");
863         return 0;
864 }
865 EXPORT_SYMBOL(lprocfs_at_hist_helper);
866
867 /* See also ptlrpc_lprocfs_rd_timeouts */
868 int lprocfs_rd_timeouts(struct seq_file *m, void *data)
869 {
870         struct obd_device *obd = data;
871         struct obd_import *imp;
872         unsigned int cur, worst;
873         time64_t now, worstt;
874         struct dhms ts;
875         int i, rc;
876
877         LASSERT(obd);
878         rc = lprocfs_climp_check(obd);
879         if (rc)
880                 return rc;
881
882         imp = obd->u.cli.cl_import;
883
884         now = ktime_get_real_seconds();
885
886         /* Some network health info for kicks */
887         s2dhms(&ts, now - imp->imp_last_reply_time);
888         seq_printf(m, "%-10s : %lld, " DHMS_FMT " ago\n",
889                    "last reply", (s64)imp->imp_last_reply_time, DHMS_VARS(&ts));
890
891         cur = at_get(&imp->imp_at.iat_net_latency);
892         worst = imp->imp_at.iat_net_latency.at_worst_ever;
893         worstt = imp->imp_at.iat_net_latency.at_worst_time;
894         s2dhms(&ts, now - worstt);
895         seq_printf(m, "%-10s : cur %3u  worst %3u (at %lld, " DHMS_FMT " ago) ",
896                    "network", cur, worst, (s64)worstt, DHMS_VARS(&ts));
897         lprocfs_at_hist_helper(m, &imp->imp_at.iat_net_latency);
898
899         for (i = 0; i < IMP_AT_MAX_PORTALS; i++) {
900                 if (imp->imp_at.iat_portal[i] == 0)
901                         break;
902                 cur = at_get(&imp->imp_at.iat_service_estimate[i]);
903                 worst = imp->imp_at.iat_service_estimate[i].at_worst_ever;
904                 worstt = imp->imp_at.iat_service_estimate[i].at_worst_time;
905                 s2dhms(&ts, now - worstt);
906                 seq_printf(m, "portal %-2d  : cur %3u  worst %3u (at %lld, "
907                            DHMS_FMT " ago) ", imp->imp_at.iat_portal[i],
908                            cur, worst, (s64)worstt, DHMS_VARS(&ts));
909                 lprocfs_at_hist_helper(m, &imp->imp_at.iat_service_estimate[i]);
910         }
911
912         up_read(&obd->u.cli.cl_sem);
913         return 0;
914 }
915 EXPORT_SYMBOL(lprocfs_rd_timeouts);
916
917 int lprocfs_rd_connect_flags(struct seq_file *m, void *data)
918 {
919         struct obd_device *obd = data;
920         __u64 flags;
921         int rc;
922
923         rc = lprocfs_climp_check(obd);
924         if (rc)
925                 return rc;
926
927         flags = obd->u.cli.cl_import->imp_connect_data.ocd_connect_flags;
928         seq_printf(m, "flags=%#llx\n", flags);
929         obd_connect_seq_flags2str(m, flags, "\n");
930         seq_printf(m, "\n");
931         up_read(&obd->u.cli.cl_sem);
932         return 0;
933 }
934 EXPORT_SYMBOL(lprocfs_rd_connect_flags);
935
936 static struct attribute *obd_def_attrs[] = {
937         &lustre_attr_blocksize.attr,
938         &lustre_attr_kbytestotal.attr,
939         &lustre_attr_kbytesfree.attr,
940         &lustre_attr_kbytesavail.attr,
941         &lustre_attr_filestotal.attr,
942         &lustre_attr_filesfree.attr,
943         &lustre_attr_uuid.attr,
944         NULL,
945 };
946
947 static void obd_sysfs_release(struct kobject *kobj)
948 {
949         struct obd_device *obd = container_of(kobj, struct obd_device,
950                                               obd_kobj);
951
952         complete(&obd->obd_kobj_unregister);
953 }
954
955 static struct kobj_type obd_ktype = {
956         .default_attrs  = obd_def_attrs,
957         .sysfs_ops      = &lustre_sysfs_ops,
958         .release        = obd_sysfs_release,
959 };
960
961 int lprocfs_obd_setup(struct obd_device *obd, struct lprocfs_vars *list,
962                       struct attribute_group *attrs)
963 {
964         int rc = 0;
965
966         init_completion(&obd->obd_kobj_unregister);
967         rc = kobject_init_and_add(&obd->obd_kobj, &obd_ktype,
968                                   obd->obd_type->typ_kobj,
969                                   "%s", obd->obd_name);
970         if (rc)
971                 return rc;
972
973         if (attrs) {
974                 rc = sysfs_create_group(&obd->obd_kobj, attrs);
975                 if (rc) {
976                         kobject_put(&obd->obd_kobj);
977                         return rc;
978                 }
979         }
980
981         obd->obd_debugfs_entry = ldebugfs_register(obd->obd_name,
982                                                    obd->obd_type->typ_debugfs_entry,
983                                                    list, obd);
984         if (IS_ERR_OR_NULL(obd->obd_debugfs_entry)) {
985                 rc = obd->obd_debugfs_entry ? PTR_ERR(obd->obd_debugfs_entry)
986                                             : -ENOMEM;
987                 CERROR("error %d setting up lprocfs for %s\n",
988                        rc, obd->obd_name);
989                 obd->obd_debugfs_entry = NULL;
990         }
991
992         return rc;
993 }
994 EXPORT_SYMBOL_GPL(lprocfs_obd_setup);
995
996 int lprocfs_obd_cleanup(struct obd_device *obd)
997 {
998         if (!obd)
999                 return -EINVAL;
1000
1001         if (!IS_ERR_OR_NULL(obd->obd_debugfs_entry))
1002                 ldebugfs_remove(&obd->obd_debugfs_entry);
1003
1004         kobject_put(&obd->obd_kobj);
1005         wait_for_completion(&obd->obd_kobj_unregister);
1006
1007         return 0;
1008 }
1009 EXPORT_SYMBOL_GPL(lprocfs_obd_cleanup);
1010
1011 int lprocfs_stats_alloc_one(struct lprocfs_stats *stats, unsigned int cpuid)
1012 {
1013         struct lprocfs_counter  *cntr;
1014         unsigned int            percpusize;
1015         int                     rc = -ENOMEM;
1016         unsigned long           flags = 0;
1017         int                     i;
1018
1019         LASSERT(!stats->ls_percpu[cpuid]);
1020         LASSERT((stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) == 0);
1021
1022         percpusize = lprocfs_stats_counter_size(stats);
1023         LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[cpuid], percpusize);
1024         if (stats->ls_percpu[cpuid]) {
1025                 rc = 0;
1026                 if (unlikely(stats->ls_biggest_alloc_num <= cpuid)) {
1027                         if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1028                                 spin_lock_irqsave(&stats->ls_lock, flags);
1029                         else
1030                                 spin_lock(&stats->ls_lock);
1031                         if (stats->ls_biggest_alloc_num <= cpuid)
1032                                 stats->ls_biggest_alloc_num = cpuid + 1;
1033                         if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1034                                 spin_unlock_irqrestore(&stats->ls_lock, flags);
1035                         else
1036                                 spin_unlock(&stats->ls_lock);
1037                 }
1038                 /* initialize the ls_percpu[cpuid] non-zero counter */
1039                 for (i = 0; i < stats->ls_num; ++i) {
1040                         cntr = lprocfs_stats_counter_get(stats, cpuid, i);
1041                         cntr->lc_min = LC_MIN_INIT;
1042                 }
1043         }
1044         return rc;
1045 }
1046 EXPORT_SYMBOL(lprocfs_stats_alloc_one);
1047
1048 struct lprocfs_stats *lprocfs_alloc_stats(unsigned int num,
1049                                           enum lprocfs_stats_flags flags)
1050 {
1051         struct lprocfs_stats    *stats;
1052         unsigned int            num_entry;
1053         unsigned int            percpusize = 0;
1054         int                     i;
1055
1056         if (num == 0)
1057                 return NULL;
1058
1059         if (lprocfs_no_percpu_stats != 0)
1060                 flags |= LPROCFS_STATS_FLAG_NOPERCPU;
1061
1062         if (flags & LPROCFS_STATS_FLAG_NOPERCPU)
1063                 num_entry = 1;
1064         else
1065                 num_entry = num_possible_cpus();
1066
1067         /* alloc percpu pointers for all possible cpu slots */
1068         LIBCFS_ALLOC(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1069         if (!stats)
1070                 return NULL;
1071
1072         stats->ls_num = num;
1073         stats->ls_flags = flags;
1074         spin_lock_init(&stats->ls_lock);
1075
1076         /* alloc num of counter headers */
1077         LIBCFS_ALLOC(stats->ls_cnt_header,
1078                      stats->ls_num * sizeof(struct lprocfs_counter_header));
1079         if (!stats->ls_cnt_header)
1080                 goto fail;
1081
1082         if ((flags & LPROCFS_STATS_FLAG_NOPERCPU) != 0) {
1083                 /* contains only one set counters */
1084                 percpusize = lprocfs_stats_counter_size(stats);
1085                 LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[0], percpusize);
1086                 if (!stats->ls_percpu[0])
1087                         goto fail;
1088                 stats->ls_biggest_alloc_num = 1;
1089         } else if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0) {
1090                 /* alloc all percpu data */
1091                 for (i = 0; i < num_entry; ++i)
1092                         if (lprocfs_stats_alloc_one(stats, i) < 0)
1093                                 goto fail;
1094         }
1095
1096         return stats;
1097
1098 fail:
1099         lprocfs_free_stats(&stats);
1100         return NULL;
1101 }
1102 EXPORT_SYMBOL(lprocfs_alloc_stats);
1103
1104 void lprocfs_free_stats(struct lprocfs_stats **statsh)
1105 {
1106         struct lprocfs_stats *stats = *statsh;
1107         unsigned int num_entry;
1108         unsigned int percpusize;
1109         unsigned int i;
1110
1111         if (!stats || stats->ls_num == 0)
1112                 return;
1113         *statsh = NULL;
1114
1115         if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU)
1116                 num_entry = 1;
1117         else
1118                 num_entry = num_possible_cpus();
1119
1120         percpusize = lprocfs_stats_counter_size(stats);
1121         for (i = 0; i < num_entry; i++)
1122                 if (stats->ls_percpu[i])
1123                         LIBCFS_FREE(stats->ls_percpu[i], percpusize);
1124         if (stats->ls_cnt_header)
1125                 LIBCFS_FREE(stats->ls_cnt_header, stats->ls_num *
1126                                         sizeof(struct lprocfs_counter_header));
1127         LIBCFS_FREE(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1128 }
1129 EXPORT_SYMBOL(lprocfs_free_stats);
1130
1131 void lprocfs_clear_stats(struct lprocfs_stats *stats)
1132 {
1133         struct lprocfs_counter          *percpu_cntr;
1134         int                             i;
1135         int                             j;
1136         unsigned int                    num_entry;
1137         unsigned long                   flags = 0;
1138
1139         num_entry = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1140
1141         for (i = 0; i < num_entry; i++) {
1142                 if (!stats->ls_percpu[i])
1143                         continue;
1144                 for (j = 0; j < stats->ls_num; j++) {
1145                         percpu_cntr = lprocfs_stats_counter_get(stats, i, j);
1146                         percpu_cntr->lc_count           = 0;
1147                         percpu_cntr->lc_min             = LC_MIN_INIT;
1148                         percpu_cntr->lc_max             = 0;
1149                         percpu_cntr->lc_sumsquare       = 0;
1150                         percpu_cntr->lc_sum             = 0;
1151                         if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1152                                 percpu_cntr->lc_sum_irq = 0;
1153                 }
1154         }
1155
1156         lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1157 }
1158 EXPORT_SYMBOL(lprocfs_clear_stats);
1159
1160 static ssize_t lprocfs_stats_seq_write(struct file *file,
1161                                        const char __user *buf,
1162                                        size_t len, loff_t *off)
1163 {
1164         struct seq_file *seq = file->private_data;
1165         struct lprocfs_stats *stats = seq->private;
1166
1167         lprocfs_clear_stats(stats);
1168
1169         return len;
1170 }
1171
1172 static void *lprocfs_stats_seq_start(struct seq_file *p, loff_t *pos)
1173 {
1174         struct lprocfs_stats *stats = p->private;
1175
1176         return (*pos < stats->ls_num) ? pos : NULL;
1177 }
1178
1179 static void lprocfs_stats_seq_stop(struct seq_file *p, void *v)
1180 {
1181 }
1182
1183 static void *lprocfs_stats_seq_next(struct seq_file *p, void *v, loff_t *pos)
1184 {
1185         (*pos)++;
1186         return lprocfs_stats_seq_start(p, pos);
1187 }
1188
1189 /* seq file export of one lprocfs counter */
1190 static int lprocfs_stats_seq_show(struct seq_file *p, void *v)
1191 {
1192         struct lprocfs_stats            *stats  = p->private;
1193         struct lprocfs_counter_header   *hdr;
1194         struct lprocfs_counter           ctr;
1195         int                              idx    = *(loff_t *)v;
1196
1197         if (idx == 0) {
1198                 struct timespec64 now;
1199
1200                 ktime_get_real_ts64(&now);
1201                 seq_printf(p, "%-25s %llu.%9lu secs.usecs\n",
1202                            "snapshot_time",
1203                            (s64)now.tv_sec, (unsigned long)now.tv_nsec);
1204         }
1205
1206         hdr = &stats->ls_cnt_header[idx];
1207         lprocfs_stats_collect(stats, idx, &ctr);
1208
1209         if (ctr.lc_count != 0) {
1210                 seq_printf(p, "%-25s %lld samples [%s]",
1211                            hdr->lc_name, ctr.lc_count, hdr->lc_units);
1212
1213                 if ((hdr->lc_config & LPROCFS_CNTR_AVGMINMAX) &&
1214                     (ctr.lc_count > 0)) {
1215                         seq_printf(p, " %lld %lld %lld",
1216                                    ctr.lc_min, ctr.lc_max, ctr.lc_sum);
1217                         if (hdr->lc_config & LPROCFS_CNTR_STDDEV)
1218                                 seq_printf(p, " %lld", ctr.lc_sumsquare);
1219                 }
1220                 seq_putc(p, '\n');
1221         }
1222
1223         return 0;
1224 }
1225
1226 static const struct seq_operations lprocfs_stats_seq_sops = {
1227         .start  = lprocfs_stats_seq_start,
1228         .stop   = lprocfs_stats_seq_stop,
1229         .next   = lprocfs_stats_seq_next,
1230         .show   = lprocfs_stats_seq_show,
1231 };
1232
1233 static int lprocfs_stats_seq_open(struct inode *inode, struct file *file)
1234 {
1235         struct seq_file *seq;
1236         int rc;
1237
1238         rc = seq_open(file, &lprocfs_stats_seq_sops);
1239         if (rc)
1240                 return rc;
1241
1242         seq = file->private_data;
1243         seq->private = inode->i_private;
1244
1245         return 0;
1246 }
1247
1248 static const struct file_operations lprocfs_stats_seq_fops = {
1249         .owner   = THIS_MODULE,
1250         .open    = lprocfs_stats_seq_open,
1251         .read    = seq_read,
1252         .write   = lprocfs_stats_seq_write,
1253         .llseek  = seq_lseek,
1254         .release = lprocfs_seq_release,
1255 };
1256
1257 int ldebugfs_register_stats(struct dentry *parent, const char *name,
1258                             struct lprocfs_stats *stats)
1259 {
1260         struct dentry *entry;
1261
1262         LASSERT(!IS_ERR_OR_NULL(parent));
1263
1264         entry = debugfs_create_file(name, 0644, parent, stats,
1265                                     &lprocfs_stats_seq_fops);
1266         if (IS_ERR_OR_NULL(entry))
1267                 return entry ? PTR_ERR(entry) : -ENOMEM;
1268
1269         return 0;
1270 }
1271 EXPORT_SYMBOL_GPL(ldebugfs_register_stats);
1272
1273 void lprocfs_counter_init(struct lprocfs_stats *stats, int index,
1274                           unsigned conf, const char *name, const char *units)
1275 {
1276         struct lprocfs_counter_header   *header;
1277         struct lprocfs_counter          *percpu_cntr;
1278         unsigned long                   flags = 0;
1279         unsigned int                    i;
1280         unsigned int                    num_cpu;
1281
1282         header = &stats->ls_cnt_header[index];
1283         LASSERTF(header, "Failed to allocate stats header:[%d]%s/%s\n",
1284                  index, name, units);
1285
1286         header->lc_config = conf;
1287         header->lc_name   = name;
1288         header->lc_units  = units;
1289
1290         num_cpu = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1291         for (i = 0; i < num_cpu; ++i) {
1292                 if (!stats->ls_percpu[i])
1293                         continue;
1294                 percpu_cntr = lprocfs_stats_counter_get(stats, i, index);
1295                 percpu_cntr->lc_count           = 0;
1296                 percpu_cntr->lc_min             = LC_MIN_INIT;
1297                 percpu_cntr->lc_max             = 0;
1298                 percpu_cntr->lc_sumsquare       = 0;
1299                 percpu_cntr->lc_sum             = 0;
1300                 if ((stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1301                         percpu_cntr->lc_sum_irq = 0;
1302         }
1303         lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1304 }
1305 EXPORT_SYMBOL(lprocfs_counter_init);
1306
1307 int lprocfs_exp_cleanup(struct obd_export *exp)
1308 {
1309         return 0;
1310 }
1311 EXPORT_SYMBOL(lprocfs_exp_cleanup);
1312
1313 __s64 lprocfs_read_helper(struct lprocfs_counter *lc,
1314                           struct lprocfs_counter_header *header,
1315                           enum lprocfs_stats_flags flags,
1316                           enum lprocfs_fields_flags field)
1317 {
1318         __s64 ret = 0;
1319
1320         if (!lc || !header)
1321                 return 0;
1322
1323         switch (field) {
1324         case LPROCFS_FIELDS_FLAGS_CONFIG:
1325                 ret = header->lc_config;
1326                 break;
1327         case LPROCFS_FIELDS_FLAGS_SUM:
1328                 ret = lc->lc_sum;
1329                 if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1330                         ret += lc->lc_sum_irq;
1331                 break;
1332         case LPROCFS_FIELDS_FLAGS_MIN:
1333                 ret = lc->lc_min;
1334                 break;
1335         case LPROCFS_FIELDS_FLAGS_MAX:
1336                 ret = lc->lc_max;
1337                 break;
1338         case LPROCFS_FIELDS_FLAGS_AVG:
1339                 ret = (lc->lc_max - lc->lc_min) / 2;
1340                 break;
1341         case LPROCFS_FIELDS_FLAGS_SUMSQUARE:
1342                 ret = lc->lc_sumsquare;
1343                 break;
1344         case LPROCFS_FIELDS_FLAGS_COUNT:
1345                 ret = lc->lc_count;
1346                 break;
1347         default:
1348                 break;
1349         }
1350
1351         return 0;
1352 }
1353 EXPORT_SYMBOL(lprocfs_read_helper);
1354
1355 int lprocfs_write_helper(const char __user *buffer, unsigned long count,
1356                          int *val)
1357 {
1358         return lprocfs_write_frac_helper(buffer, count, val, 1);
1359 }
1360 EXPORT_SYMBOL(lprocfs_write_helper);
1361
1362 int lprocfs_write_u64_helper(const char __user *buffer, unsigned long count,
1363                              __u64 *val)
1364 {
1365         return lprocfs_write_frac_u64_helper(buffer, count, val, 1);
1366 }
1367 EXPORT_SYMBOL(lprocfs_write_u64_helper);
1368
1369 int lprocfs_write_frac_u64_helper(const char __user *buffer,
1370                                   unsigned long count, __u64 *val, int mult)
1371 {
1372         char kernbuf[22], *end, *pbuf;
1373         __u64 whole, frac = 0, units;
1374         unsigned frac_d = 1;
1375         int sign = 1;
1376
1377         if (count > (sizeof(kernbuf) - 1))
1378                 return -EINVAL;
1379
1380         if (copy_from_user(kernbuf, buffer, count))
1381                 return -EFAULT;
1382
1383         kernbuf[count] = '\0';
1384         pbuf = kernbuf;
1385         if (*pbuf == '-') {
1386                 sign = -1;
1387                 pbuf++;
1388         }
1389
1390         whole = simple_strtoull(pbuf, &end, 10);
1391         if (pbuf == end)
1392                 return -EINVAL;
1393
1394         if (*end == '.') {
1395                 int i;
1396
1397                 pbuf = end + 1;
1398
1399                 /* need to limit frac_d to a __u32 */
1400                 if (strlen(pbuf) > 10)
1401                         pbuf[10] = '\0';
1402
1403                 frac = simple_strtoull(pbuf, &end, 10);
1404                 /* count decimal places */
1405                 for (i = 0; i < (end - pbuf); i++)
1406                         frac_d *= 10;
1407         }
1408
1409         units = 1;
1410         if (end) {
1411                 switch (tolower(*end)) {
1412                 case 'p':
1413                         units <<= 10;
1414                 case 't':
1415                         units <<= 10;
1416                 case 'g':
1417                         units <<= 10;
1418                 case 'm':
1419                         units <<= 10;
1420                 case 'k':
1421                         units <<= 10;
1422                 }
1423         }
1424         /* Specified units override the multiplier */
1425         if (units > 1)
1426                 mult = units;
1427
1428         frac *= mult;
1429         do_div(frac, frac_d);
1430         *val = sign * (whole * mult + frac);
1431         return 0;
1432 }
1433 EXPORT_SYMBOL(lprocfs_write_frac_u64_helper);
1434
1435 static char *lprocfs_strnstr(const char *s1, const char *s2, size_t len)
1436 {
1437         size_t l2;
1438
1439         l2 = strlen(s2);
1440         if (!l2)
1441                 return (char *)s1;
1442         while (len >= l2) {
1443                 len--;
1444                 if (!memcmp(s1, s2, l2))
1445                         return (char *)s1;
1446                 s1++;
1447         }
1448         return NULL;
1449 }
1450
1451 /**
1452  * Find the string \a name in the input \a buffer, and return a pointer to the
1453  * value immediately following \a name, reducing \a count appropriately.
1454  * If \a name is not found the original \a buffer is returned.
1455  */
1456 char *lprocfs_find_named_value(const char *buffer, const char *name,
1457                                size_t *count)
1458 {
1459         char *val;
1460         size_t buflen = *count;
1461
1462         /* there is no strnstr() in rhel5 and ubuntu kernels */
1463         val = lprocfs_strnstr(buffer, name, buflen);
1464         if (!val)
1465                 return (char *)buffer;
1466
1467         val += strlen(name);                         /* skip prefix */
1468         while (val < buffer + buflen && isspace(*val)) /* skip separator */
1469                 val++;
1470
1471         *count = 0;
1472         while (val < buffer + buflen && isalnum(*val)) {
1473                 ++*count;
1474                 ++val;
1475         }
1476
1477         return val - *count;
1478 }
1479 EXPORT_SYMBOL(lprocfs_find_named_value);
1480
1481 int ldebugfs_seq_create(struct dentry *parent, const char *name,
1482                         umode_t mode, const struct file_operations *seq_fops,
1483                         void *data)
1484 {
1485         struct dentry *entry;
1486
1487         /* Disallow secretly (un)writable entries. */
1488         LASSERT((seq_fops->write == NULL) == ((mode & 0222) == 0));
1489
1490         entry = debugfs_create_file(name, mode, parent, data, seq_fops);
1491         if (IS_ERR_OR_NULL(entry))
1492                 return entry ? PTR_ERR(entry) : -ENOMEM;
1493
1494         return 0;
1495 }
1496 EXPORT_SYMBOL_GPL(ldebugfs_seq_create);
1497
1498 int ldebugfs_obd_seq_create(struct obd_device *dev,
1499                             const char *name,
1500                             umode_t mode,
1501                             const struct file_operations *seq_fops,
1502                             void *data)
1503 {
1504         return ldebugfs_seq_create(dev->obd_debugfs_entry, name,
1505                                    mode, seq_fops, data);
1506 }
1507 EXPORT_SYMBOL_GPL(ldebugfs_obd_seq_create);
1508
1509 void lprocfs_oh_tally(struct obd_histogram *oh, unsigned int value)
1510 {
1511         if (value >= OBD_HIST_MAX)
1512                 value = OBD_HIST_MAX - 1;
1513
1514         spin_lock(&oh->oh_lock);
1515         oh->oh_buckets[value]++;
1516         spin_unlock(&oh->oh_lock);
1517 }
1518 EXPORT_SYMBOL(lprocfs_oh_tally);
1519
1520 void lprocfs_oh_tally_log2(struct obd_histogram *oh, unsigned int value)
1521 {
1522         unsigned int val = 0;
1523
1524         if (likely(value != 0))
1525                 val = min(fls(value - 1), OBD_HIST_MAX);
1526
1527         lprocfs_oh_tally(oh, val);
1528 }
1529 EXPORT_SYMBOL(lprocfs_oh_tally_log2);
1530
1531 unsigned long lprocfs_oh_sum(struct obd_histogram *oh)
1532 {
1533         unsigned long ret = 0;
1534         int i;
1535
1536         for (i = 0; i < OBD_HIST_MAX; i++)
1537                 ret +=  oh->oh_buckets[i];
1538         return ret;
1539 }
1540 EXPORT_SYMBOL(lprocfs_oh_sum);
1541
1542 void lprocfs_oh_clear(struct obd_histogram *oh)
1543 {
1544         spin_lock(&oh->oh_lock);
1545         memset(oh->oh_buckets, 0, sizeof(oh->oh_buckets));
1546         spin_unlock(&oh->oh_lock);
1547 }
1548 EXPORT_SYMBOL(lprocfs_oh_clear);
1549
1550 static ssize_t lustre_attr_show(struct kobject *kobj,
1551                                 struct attribute *attr, char *buf)
1552 {
1553         struct lustre_attr *a = container_of(attr, struct lustre_attr, attr);
1554
1555         return a->show ? a->show(kobj, attr, buf) : 0;
1556 }
1557
1558 static ssize_t lustre_attr_store(struct kobject *kobj, struct attribute *attr,
1559                                  const char *buf, size_t len)
1560 {
1561         struct lustre_attr *a = container_of(attr, struct lustre_attr, attr);
1562
1563         return a->store ? a->store(kobj, attr, buf, len) : len;
1564 }
1565
1566 const struct sysfs_ops lustre_sysfs_ops = {
1567         .show  = lustre_attr_show,
1568         .store = lustre_attr_store,
1569 };
1570 EXPORT_SYMBOL_GPL(lustre_sysfs_ops);