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