Whamcloud - gitweb
LU-17705 ptlrpc: replace synchronize_rcu() with rcu_barrier()
[fs/lustre-release.git] / 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, 2017, 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 <obd_class.h>
40 #include <lprocfs_status.h>
41
42 #ifdef CONFIG_PROC_FS
43
44 static int lprocfs_no_percpu_stats = 0;
45 module_param(lprocfs_no_percpu_stats, int, 0644);
46 MODULE_PARM_DESC(lprocfs_no_percpu_stats, "Do not alloc percpu data for lprocfs stats");
47
48 #define MAX_STRING_SIZE 128
49
50 int lprocfs_single_release(struct inode *inode, struct file *file)
51 {
52         return single_release(inode, file);
53 }
54 EXPORT_SYMBOL(lprocfs_single_release);
55
56 int lprocfs_seq_release(struct inode *inode, struct file *file)
57 {
58         return seq_release(inode, file);
59 }
60 EXPORT_SYMBOL(lprocfs_seq_release);
61
62 struct proc_dir_entry *
63 lprocfs_add_simple(struct proc_dir_entry *root, char *name,
64                    void *data, const struct file_operations *fops)
65 {
66         struct proc_dir_entry *proc;
67         mode_t mode = 0;
68
69         if (!root || !name || !fops)
70                 return ERR_PTR(-EINVAL);
71
72         if (fops->read)
73                 mode = 0444;
74         if (fops->write)
75                 mode |= 0200;
76         proc = proc_create_data(name, mode, root, fops, data);
77         if (!proc) {
78                 CERROR("LprocFS: No memory to create /proc entry %s\n",
79                        name);
80                 return ERR_PTR(-ENOMEM);
81         }
82         return proc;
83 }
84 EXPORT_SYMBOL(lprocfs_add_simple);
85
86 struct proc_dir_entry *lprocfs_add_symlink(const char *name,
87                                            struct proc_dir_entry *parent,
88                                            const char *format, ...)
89 {
90         struct proc_dir_entry *entry;
91         char *dest;
92         va_list ap;
93
94         if (!parent || !format)
95                 return NULL;
96
97         OBD_ALLOC_WAIT(dest, MAX_STRING_SIZE + 1);
98         if (!dest)
99                 return NULL;
100
101         va_start(ap, format);
102         vsnprintf(dest, MAX_STRING_SIZE, format, ap);
103         va_end(ap);
104
105         entry = proc_symlink(name, parent, dest);
106         if (!entry)
107                 CERROR("LprocFS: Could not create symbolic link from "
108                        "%s to %s\n", name, dest);
109
110         OBD_FREE(dest, MAX_STRING_SIZE + 1);
111         return entry;
112 }
113 EXPORT_SYMBOL(lprocfs_add_symlink);
114
115 static const struct file_operations ldebugfs_empty_ops = { };
116
117 void ldebugfs_add_vars(struct dentry *parent, struct ldebugfs_vars *list,
118                        void *data)
119 {
120         if (IS_ERR_OR_NULL(parent) || IS_ERR_OR_NULL(list))
121                 return;
122
123         while (list->name) {
124                 umode_t mode = 0;
125
126                 if (list->proc_mode != 0000) {
127                         mode = list->proc_mode;
128                 } else if (list->fops) {
129                         if (list->fops->read)
130                                 mode = 0444;
131                         if (list->fops->write)
132                                 mode |= 0200;
133                 }
134                 debugfs_create_file(list->name, mode, parent,
135                                     list->data ? : data,
136                                     list->fops ? : &ldebugfs_empty_ops);
137                 list++;
138         }
139 }
140 EXPORT_SYMBOL_GPL(ldebugfs_add_vars);
141
142 static const struct file_operations lprocfs_empty_ops = { };
143
144 /**
145  * Add /proc entries.
146  *
147  * \param root [in]  The parent proc entry on which new entry will be added.
148  * \param list [in]  Array of proc entries to be added.
149  * \param data [in]  The argument to be passed when entries read/write routines
150  *                   are called through /proc file.
151  *
152  * \retval 0   on success
153  *         < 0 on error
154  */
155 int
156 lprocfs_add_vars(struct proc_dir_entry *root, struct lprocfs_vars *list,
157                  void *data)
158 {
159         if (!root || !list)
160                 return -EINVAL;
161
162         while (list->name) {
163                 struct proc_dir_entry *proc;
164                 mode_t mode = 0;
165
166                 if (list->proc_mode != 0000) {
167                         mode = list->proc_mode;
168                 } else if (list->fops) {
169                         if (list->fops->read)
170                                 mode = 0444;
171                         if (list->fops->write)
172                                 mode |= 0200;
173                 }
174                 proc = proc_create_data(list->name, mode, root,
175                                         list->fops ?: &lprocfs_empty_ops,
176                                         list->data ?: data);
177                 if (!proc)
178                         return -ENOMEM;
179                 list++;
180         }
181         return 0;
182 }
183 EXPORT_SYMBOL(lprocfs_add_vars);
184
185 void lprocfs_remove(struct proc_dir_entry **rooth)
186 {
187         proc_remove(*rooth);
188         *rooth = NULL;
189 }
190 EXPORT_SYMBOL(lprocfs_remove);
191
192 void lprocfs_remove_proc_entry(const char *name, struct proc_dir_entry *parent)
193 {
194         LASSERT(parent != NULL);
195         remove_proc_entry(name, parent);
196 }
197 EXPORT_SYMBOL(lprocfs_remove_proc_entry);
198
199 struct proc_dir_entry *
200 lprocfs_register(const char *name, struct proc_dir_entry *parent,
201                  struct lprocfs_vars *list, void *data)
202 {
203         struct proc_dir_entry *newchild;
204
205         newchild = proc_mkdir(name, parent);
206         if (!newchild)
207                 return ERR_PTR(-ENOMEM);
208
209         if (list) {
210                 int rc = lprocfs_add_vars(newchild, list, data);
211                 if (rc) {
212                         lprocfs_remove(&newchild);
213                         return ERR_PTR(rc);
214                 }
215         }
216         return newchild;
217 }
218 EXPORT_SYMBOL(lprocfs_register);
219
220 /* Generic callbacks */
221 int lprocfs_uuid_seq_show(struct seq_file *m, void *data)
222 {
223         struct obd_device *obd = data;
224
225         LASSERT(obd != NULL);
226         seq_printf(m, "%s\n", obd->obd_uuid.uuid);
227         return 0;
228 }
229 EXPORT_SYMBOL(lprocfs_uuid_seq_show);
230
231 static ssize_t uuid_show(struct kobject *kobj, struct attribute *attr,
232                          char *buf)
233 {
234         struct obd_device *obd = container_of(kobj, struct obd_device,
235                                               obd_kset.kobj);
236
237         return sprintf(buf, "%s\n", obd->obd_uuid.uuid);
238 }
239 LUSTRE_RO_ATTR(uuid);
240
241 static ssize_t blocksize_show(struct kobject *kobj, struct attribute *attr,
242                               char *buf)
243 {
244         struct obd_device *obd = container_of(kobj, struct obd_device,
245                                               obd_kset.kobj);
246         struct obd_statfs osfs;
247         int rc;
248
249         rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
250                         ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
251                         OBD_STATFS_NODELAY);
252         if (!rc)
253                 return sprintf(buf, "%u\n", osfs.os_bsize);
254
255         return rc;
256 }
257 LUSTRE_RO_ATTR(blocksize);
258
259 static ssize_t kbytestotal_show(struct kobject *kobj, struct attribute *attr,
260                                 char *buf)
261 {
262         struct obd_device *obd = container_of(kobj, struct obd_device,
263                                               obd_kset.kobj);
264         struct obd_statfs osfs;
265         int rc;
266
267         rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
268                         ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
269                         OBD_STATFS_NODELAY);
270         if (!rc) {
271                 u32 blk_size = osfs.os_bsize >> 10;
272                 u64 result = osfs.os_blocks;
273
274                 result *= rounddown_pow_of_two(blk_size ?: 1);
275                 return sprintf(buf, "%llu\n", result);
276         }
277
278         return rc;
279 }
280 LUSTRE_RO_ATTR(kbytestotal);
281
282 static ssize_t kbytesfree_show(struct kobject *kobj, struct attribute *attr,
283                                char *buf)
284 {
285         struct obd_device *obd = container_of(kobj, struct obd_device,
286                                               obd_kset.kobj);
287         struct obd_statfs osfs;
288         int rc;
289
290         rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
291                         ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
292                         OBD_STATFS_NODELAY);
293         if (!rc) {
294                 u32 blk_size = osfs.os_bsize >> 10;
295                 u64 result = osfs.os_bfree;
296
297                 while (blk_size >>= 1)
298                         result <<= 1;
299
300                 return sprintf(buf, "%llu\n", result);
301         }
302
303         return rc;
304 }
305 LUSTRE_RO_ATTR(kbytesfree);
306
307 static ssize_t kbytesavail_show(struct kobject *kobj, struct attribute *attr,
308                                 char *buf)
309 {
310         struct obd_device *obd = container_of(kobj, struct obd_device,
311                                               obd_kset.kobj);
312         struct obd_statfs osfs;
313         int rc;
314
315         rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
316                         ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
317                         OBD_STATFS_NODELAY);
318         if (!rc) {
319                 u32 blk_size = osfs.os_bsize >> 10;
320                 u64 result = osfs.os_bavail;
321
322                 while (blk_size >>= 1)
323                         result <<= 1;
324
325                 return sprintf(buf, "%llu\n", result);
326         }
327
328         return rc;
329 }
330 LUSTRE_RO_ATTR(kbytesavail);
331
332 static ssize_t filestotal_show(struct kobject *kobj, struct attribute *attr,
333                                char *buf)
334 {
335         struct obd_device *obd = container_of(kobj, struct obd_device,
336                                               obd_kset.kobj);
337         struct obd_statfs osfs;
338         int rc;
339
340         rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
341                         ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
342                         OBD_STATFS_NODELAY);
343         if (!rc)
344                 return sprintf(buf, "%llu\n", osfs.os_files);
345
346         return rc;
347 }
348 LUSTRE_RO_ATTR(filestotal);
349
350 static ssize_t filesfree_show(struct kobject *kobj, struct attribute *attr,
351                               char *buf)
352 {
353         struct obd_device *obd = container_of(kobj, struct obd_device,
354                                               obd_kset.kobj);
355         struct obd_statfs osfs;
356         int rc;
357
358         rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
359                         ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
360                         OBD_STATFS_NODELAY);
361         if (!rc)
362                 return sprintf(buf, "%llu\n", osfs.os_ffree);
363
364         return rc;
365 }
366 LUSTRE_RO_ATTR(filesfree);
367
368 ssize_t conn_uuid_show(struct kobject *kobj, struct attribute *attr, char *buf)
369 {
370         struct obd_device *obd = container_of(kobj, struct obd_device,
371                                               obd_kset.kobj);
372         struct obd_import *imp;
373         struct ptlrpc_connection *conn;
374         ssize_t count;
375
376         with_imp_locked(obd, imp, count) {
377                 conn = imp->imp_connection;
378                 if (conn)
379                         count = sprintf(buf, "%s\n", conn->c_remote_uuid.uuid);
380                 else
381                         count = sprintf(buf, "%s\n", "<none>");
382         }
383
384         return count;
385 }
386 EXPORT_SYMBOL(conn_uuid_show);
387
388 int lprocfs_server_uuid_seq_show(struct seq_file *m, void *data)
389 {
390         struct obd_device *obd = data;
391         struct obd_import *imp;
392         const char *imp_state_name = NULL;
393         int rc = 0;
394
395         LASSERT(obd != NULL);
396         with_imp_locked(obd, imp, rc) {
397                 imp_state_name = ptlrpc_import_state_name(imp->imp_state);
398                 seq_printf(m, "%s\t%s%s\n", obd2cli_tgt(obd), imp_state_name,
399                            imp->imp_deactive ? "\tDEACTIVATED" : "");
400         }
401
402         return rc;
403 }
404 EXPORT_SYMBOL(lprocfs_server_uuid_seq_show);
405
406 /** add up per-cpu counters */
407
408 /**
409  * Lock statistics structure for access, possibly only on this CPU.
410  *
411  * The statistics struct may be allocated with per-CPU structures for
412  * efficient concurrent update (usually only on server-wide stats), or
413  * as a single global struct (e.g. for per-client or per-job statistics),
414  * so the required locking depends on the type of structure allocated.
415  *
416  * For per-CPU statistics, pin the thread to the current cpuid so that
417  * will only access the statistics for that CPU.  If the stats structure
418  * for the current CPU has not been allocated (or previously freed),
419  * allocate it now.  The per-CPU statistics do not need locking since
420  * the thread is pinned to the CPU during update.
421  *
422  * For global statistics, lock the stats structure to prevent concurrent update.
423  *
424  * \param[in] stats     statistics structure to lock
425  * \param[in] opc       type of operation:
426  *                      LPROCFS_GET_SMP_ID: "lock" and return current CPU index
427  *                              for incrementing statistics for that CPU
428  *                      LPROCFS_GET_NUM_CPU: "lock" and return number of used
429  *                              CPU indices to iterate over all indices
430  * \param[out] flags    CPU interrupt saved state for IRQ-safe locking
431  *
432  * \retval cpuid of current thread or number of allocated structs
433  * \retval negative on error (only for opc LPROCFS_GET_SMP_ID + per-CPU stats)
434  */
435 int lprocfs_stats_lock(struct lprocfs_stats *stats,
436                        enum lprocfs_stats_lock_ops opc,
437                        unsigned long *flags)
438 {
439         if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) {
440                 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
441                         spin_lock_irqsave(&stats->ls_lock, *flags);
442                 else
443                         spin_lock(&stats->ls_lock);
444                 return opc == LPROCFS_GET_NUM_CPU ? 1 : 0;
445         }
446
447         switch (opc) {
448         case LPROCFS_GET_SMP_ID: {
449                 unsigned int cpuid = get_cpu();
450
451                 if (unlikely(!stats->ls_percpu[cpuid])) {
452                         int rc = lprocfs_stats_alloc_one(stats, cpuid);
453
454                         if (rc < 0) {
455                                 put_cpu();
456                                 return rc;
457                         }
458                 }
459                 return cpuid;
460         }
461         case LPROCFS_GET_NUM_CPU:
462                 return stats->ls_biggest_alloc_num;
463         default:
464                 LBUG();
465         }
466 }
467
468 /**
469  * Unlock statistics structure after access.
470  *
471  * Unlock the lock acquired via lprocfs_stats_lock() for global statistics,
472  * or unpin this thread from the current cpuid for per-CPU statistics.
473  *
474  * This function must be called using the same arguments as used when calling
475  * lprocfs_stats_lock() so that the correct operation can be performed.
476  *
477  * \param[in] stats     statistics structure to unlock
478  * \param[in] opc       type of operation (current cpuid or number of structs)
479  * \param[in] flags     CPU interrupt saved state for IRQ-safe locking
480  */
481 void lprocfs_stats_unlock(struct lprocfs_stats *stats,
482                           enum lprocfs_stats_lock_ops opc,
483                           unsigned long *flags)
484 {
485         if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) {
486                 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
487                         spin_unlock_irqrestore(&stats->ls_lock, *flags);
488                 else
489                         spin_unlock(&stats->ls_lock);
490         } else if (opc == LPROCFS_GET_SMP_ID) {
491                 put_cpu();
492         }
493 }
494
495 /** add up per-cpu counters */
496 void lprocfs_stats_collect(struct lprocfs_stats *stats, int idx,
497                            struct lprocfs_counter *cnt)
498 {
499         unsigned int num_entry;
500         struct lprocfs_counter *percpu_cntr;
501         int i;
502         unsigned long flags = 0;
503
504         memset(cnt, 0, sizeof(*cnt));
505
506         if (!stats) {
507                 /* set count to 1 to avoid divide-by-zero errs in callers */
508                 cnt->lc_count = 1;
509                 return;
510         }
511
512         cnt->lc_min = LC_MIN_INIT;
513
514         num_entry = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
515
516         for (i = 0; i < num_entry; i++) {
517                 if (!stats->ls_percpu[i])
518                         continue;
519                 percpu_cntr = lprocfs_stats_counter_get(stats, i, idx);
520
521                 cnt->lc_count += percpu_cntr->lc_count;
522                 cnt->lc_sum += percpu_cntr->lc_sum;
523                 if (percpu_cntr->lc_min < cnt->lc_min)
524                         cnt->lc_min = percpu_cntr->lc_min;
525                 if (percpu_cntr->lc_max > cnt->lc_max)
526                         cnt->lc_max = percpu_cntr->lc_max;
527                 cnt->lc_sumsquare += percpu_cntr->lc_sumsquare;
528         }
529
530         lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
531 }
532
533 static void obd_import_flags2str(struct obd_import *imp, struct seq_file *m)
534 {
535         bool first = true;
536
537         if (imp->imp_obd->obd_no_recov) {
538                 seq_printf(m, "no_recov");
539                 first = false;
540         }
541
542         flag2str(imp, invalid);
543         flag2str(imp, deactive);
544         flag2str(imp, replayable);
545         flag2str(imp, delayed_recovery);
546         flag2str(imp, vbr_failed);
547         flag2str(imp, pingable);
548         flag2str(imp, resend_replay);
549         flag2str(imp, no_pinger_recover);
550         flag2str(imp, connect_tried);
551 }
552
553 static const char *obd_connect_names[] = {
554         /* flags names  */
555         "read_only",
556         "lov_index",
557         "connect_from_mds",
558         "write_grant",
559         "server_lock",
560         "version",
561         "request_portal",
562         "acl",
563         "xattr",
564         "create_on_write",
565         "truncate_lock",
566         "initial_transno",
567         "inode_bit_locks",
568         "barrier",
569         "getattr_by_fid",
570         "no_oh_for_devices",
571         "remote_client",
572         "remote_client_by_force",
573         "max_byte_per_rpc",
574         "64bit_qdata",
575         "mds_capability",
576         "oss_capability",
577         "early_lock_cancel",
578         "som",
579         "adaptive_timeouts",
580         "lru_resize",
581         "mds_mds_connection",
582         "real_conn",
583         "change_qunit_size",
584         "alt_checksum_algorithm",
585         "fid_is_enabled",
586         "version_recovery",
587         "pools",
588         "grant_shrink",
589         "skip_orphan",
590         "large_ea",
591         "full20",
592         "layout_lock",
593         "64bithash",
594         "object_max_bytes",
595         "imp_recov",
596         "jobstats",
597         "umask",
598         "einprogress",
599         "grant_param",
600         "flock_owner",
601         "lvb_type",
602         "nanoseconds_times",
603         "lightweight_conn",
604         "short_io",
605         "pingless",
606         "flock_deadlock",
607         "disp_stripe",
608         "open_by_fid",
609         "lfsck",
610         "unknown",
611         "unlink_close",
612         "multi_mod_rpcs",
613         "dir_stripe",
614         "subtree",
615         "lockahead",
616         "bulk_mbits",
617         "compact_obdo",
618         "second_flags",
619         /* flags2 names */
620         "file_secctx",  /* 0x01 */
621         "lockaheadv2",  /* 0x02 */
622         "dir_migrate",  /* 0x04 */
623         "sum_statfs",   /* 0x08 */
624         "overstriping", /* 0x10 */
625         "flr",          /* 0x20 */
626         "wbc",          /* 0x40 */
627         "lock_convert",  /* 0x80 */
628         "archive_id_array",     /* 0x100 */
629         "increasing_xid",       /* 0x200 */
630         "selinux_policy",       /* 0x400 */
631         "lsom",                 /* 0x800 */
632         "pcc",                  /* 0x1000 */
633         "crush",                /* 0x2000 */
634         "async_discard",        /* 0x4000 */
635         "client_encryption",    /* 0x8000 */
636         "fidmap",               /* 0x10000 */
637         "getattr_pfid",         /* 0x20000 */
638         NULL
639 };
640
641 void obd_connect_seq_flags2str(struct seq_file *m, __u64 flags, __u64 flags2,
642                                const char *sep)
643 {
644         bool first = true;
645         __u64 mask;
646         int i;
647
648         for (i = 0, mask = 1; i < 64; i++, mask <<= 1) {
649                 if (flags & mask) {
650                         seq_printf(m, "%s%s",
651                                    first ? "" : sep, obd_connect_names[i]);
652                         first = false;
653                 }
654         }
655
656         if (flags & ~(mask - 1)) {
657                 seq_printf(m, "%sunknown_%#llx",
658                            first ? "" : sep, flags & ~(mask - 1));
659                 first = false;
660         }
661
662         if (!(flags & OBD_CONNECT_FLAGS2) || flags2 == 0)
663                 return;
664
665         for (i = 64, mask = 1; obd_connect_names[i] != NULL; i++, mask <<= 1) {
666                 if (flags2 & mask) {
667                         seq_printf(m, "%s%s",
668                                    first ? "" : sep, obd_connect_names[i]);
669                         first = false;
670                 }
671         }
672
673         if (flags2 & ~(mask - 1)) {
674                 seq_printf(m, "%sunknown2_%#llx",
675                            first ? "" : sep, flags2 & ~(mask - 1));
676                 first = false;
677         }
678 }
679 EXPORT_SYMBOL(obd_connect_seq_flags2str);
680
681 int obd_connect_flags2str(char *page, int count, __u64 flags, __u64 flags2,
682                           const char *sep)
683 {
684         __u64 mask;
685         int i, ret = 0;
686
687         for (i = 0, mask = 1; i < 64; i++, mask <<= 1) {
688                 if (flags & mask)
689                         ret += snprintf(page + ret, count - ret, "%s%s",
690                                         ret ? sep : "", obd_connect_names[i]);
691         }
692
693         if (flags & ~(mask - 1))
694                 ret += snprintf(page + ret, count - ret,
695                                 "%sunknown_%#llx",
696                                 ret ? sep : "", flags & ~(mask - 1));
697
698         if (!(flags & OBD_CONNECT_FLAGS2) || flags2 == 0)
699                 return ret;
700
701         for (i = 64, mask = 1; obd_connect_names[i] != NULL; i++, mask <<= 1) {
702                 if (flags2 & mask)
703                         ret += snprintf(page + ret, count - ret, "%s%s",
704                                         ret ? sep : "", obd_connect_names[i]);
705         }
706
707         if (flags2 & ~(mask - 1))
708                 ret += snprintf(page + ret, count - ret,
709                                 "%sunknown2_%#llx",
710                                 ret ? sep : "", flags2 & ~(mask - 1));
711
712         return ret;
713 }
714 EXPORT_SYMBOL(obd_connect_flags2str);
715
716 void
717 obd_connect_data_seqprint(struct seq_file *m, struct obd_connect_data *ocd)
718 {
719         __u64 flags;
720
721         LASSERT(ocd != NULL);
722         flags = ocd->ocd_connect_flags;
723
724         seq_printf(m, "    connect_data:\n"
725                    "       flags: %#llx\n"
726                    "       instance: %u\n",
727                    ocd->ocd_connect_flags,
728                    ocd->ocd_instance);
729         if (flags & OBD_CONNECT_VERSION)
730                 seq_printf(m, "       target_version: %u.%u.%u.%u\n",
731                            OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
732                            OBD_OCD_VERSION_MINOR(ocd->ocd_version),
733                            OBD_OCD_VERSION_PATCH(ocd->ocd_version),
734                            OBD_OCD_VERSION_FIX(ocd->ocd_version));
735         if (flags & OBD_CONNECT_MDS)
736                 seq_printf(m, "       mdt_index: %d\n", ocd->ocd_group);
737         if (flags & OBD_CONNECT_GRANT)
738                 seq_printf(m, "       initial_grant: %d\n", ocd->ocd_grant);
739         if (flags & OBD_CONNECT_INDEX)
740                 seq_printf(m, "       target_index: %u\n", ocd->ocd_index);
741         if (flags & OBD_CONNECT_BRW_SIZE)
742                 seq_printf(m, "       max_brw_size: %d\n", ocd->ocd_brw_size);
743         if (flags & OBD_CONNECT_IBITS)
744                 seq_printf(m, "       ibits_known: %#llx\n",
745                            ocd->ocd_ibits_known);
746         if (flags & OBD_CONNECT_GRANT_PARAM)
747                 seq_printf(m, "       grant_block_size: %d\n"
748                            "       grant_inode_size: %d\n"
749                            "       grant_max_extent_size: %d\n"
750                            "       grant_extent_tax: %d\n",
751                            1 << ocd->ocd_grant_blkbits,
752                            1 << ocd->ocd_grant_inobits,
753                            ocd->ocd_grant_max_blks << ocd->ocd_grant_blkbits,
754                            ocd->ocd_grant_tax_kb << 10);
755         if (flags & OBD_CONNECT_TRANSNO)
756                 seq_printf(m, "       first_transno: %#llx\n",
757                            ocd->ocd_transno);
758         if (flags & OBD_CONNECT_CKSUM)
759                 seq_printf(m, "       cksum_types: %#x\n",
760                            ocd->ocd_cksum_types);
761         if (flags & OBD_CONNECT_MAX_EASIZE)
762                 seq_printf(m, "       max_easize: %d\n", ocd->ocd_max_easize);
763         if (flags & OBD_CONNECT_MAXBYTES)
764                 seq_printf(m, "       max_object_bytes: %llu\n",
765                            ocd->ocd_maxbytes);
766         if (flags & OBD_CONNECT_MULTIMODRPCS)
767                 seq_printf(m, "       max_mod_rpcs: %hu\n",
768                            ocd->ocd_maxmodrpcs);
769 }
770
771 static void lprocfs_import_seq_show_locked(struct seq_file *m,
772                                            struct obd_device *obd,
773                                            struct obd_import *imp)
774 {
775         char nidstr[LNET_NIDSTR_SIZE];
776         struct lprocfs_counter ret;
777         struct lprocfs_counter_header *header;
778         struct obd_import_conn *conn;
779         struct obd_connect_data *ocd;
780         int j;
781         int k;
782         int rw = 0;
783
784         ocd = &imp->imp_connect_data;
785
786         seq_printf(m, "import:\n"
787                    "    name: %s\n"
788                    "    target: %s\n"
789                    "    state: %s\n"
790                    "    connect_flags: [ ",
791                    obd->obd_name,
792                    obd2cli_tgt(obd),
793                    ptlrpc_import_state_name(imp->imp_state));
794         obd_connect_seq_flags2str(m, imp->imp_connect_data.ocd_connect_flags,
795                                   imp->imp_connect_data.ocd_connect_flags2,
796                                   ", ");
797         seq_printf(m, " ]\n");
798         obd_connect_data_seqprint(m, ocd);
799         seq_printf(m, "    import_flags: [ ");
800         obd_import_flags2str(imp, m);
801
802         seq_printf(m, " ]\n"
803                    "    connection:\n"
804                    "       failover_nids: [ ");
805         spin_lock(&imp->imp_lock);
806         j = 0;
807         list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
808                 libcfs_nid2str_r(conn->oic_conn->c_peer.nid,
809                                  nidstr, sizeof(nidstr));
810                 seq_printf(m, "%s%s", j ? ", " : "", nidstr);
811                 j++;
812         }
813         if (imp->imp_connection)
814                 libcfs_nid2str_r(imp->imp_connection->c_peer.nid,
815                                  nidstr, sizeof(nidstr));
816         else
817                 strncpy(nidstr, "<none>", sizeof(nidstr));
818         seq_printf(m, " ]\n"
819                    "       current_connection: %s\n"
820                    "       connection_attempts: %u\n"
821                    "       generation: %u\n"
822                    "       in-progress_invalidations: %u\n"
823                    "       idle: %lld sec\n",
824                    nidstr,
825                    imp->imp_conn_cnt,
826                    imp->imp_generation,
827                    atomic_read(&imp->imp_inval_count),
828                    ktime_get_real_seconds() - imp->imp_last_reply_time);
829         spin_unlock(&imp->imp_lock);
830
831         if (!obd->obd_svc_stats)
832                 return;
833
834         header = &obd->obd_svc_stats->ls_cnt_header[PTLRPC_REQWAIT_CNTR];
835         lprocfs_stats_collect(obd->obd_svc_stats, PTLRPC_REQWAIT_CNTR, &ret);
836         if (ret.lc_count != 0)
837                 ret.lc_sum = div64_s64(ret.lc_sum, ret.lc_count);
838         else
839                 ret.lc_sum = 0;
840         seq_printf(m, "    rpcs:\n"
841                    "       inflight: %u\n"
842                    "       unregistering: %u\n"
843                    "       timeouts: %u\n"
844                    "       avg_waittime: %llu %s\n",
845                    atomic_read(&imp->imp_inflight),
846                    atomic_read(&imp->imp_unregistering),
847                    atomic_read(&imp->imp_timeouts),
848                    ret.lc_sum, header->lc_units);
849
850         k = 0;
851         for(j = 0; j < IMP_AT_MAX_PORTALS; j++) {
852                 if (imp->imp_at.iat_portal[j] == 0)
853                         break;
854                 k = max_t(unsigned int, k,
855                           at_get(&imp->imp_at.iat_service_estimate[j]));
856         }
857         seq_printf(m, "    service_estimates:\n"
858                    "       services: %u sec\n"
859                    "       network: %d sec\n",
860                    k,
861                    at_get(&imp->imp_at.iat_net_latency));
862
863         seq_printf(m, "    transactions:\n"
864                    "       last_replay: %llu\n"
865                    "       peer_committed: %llu\n"
866                    "       last_checked: %llu\n",
867                    imp->imp_last_replay_transno,
868                    imp->imp_peer_committed_transno,
869                    imp->imp_last_transno_checked);
870
871         /* avg data rates */
872         for (rw = 0; rw <= 1; rw++) {
873                 lprocfs_stats_collect(obd->obd_svc_stats,
874                                       PTLRPC_LAST_CNTR + BRW_READ_BYTES + rw,
875                                       &ret);
876                 if (ret.lc_sum > 0 && ret.lc_count > 0) {
877                         ret.lc_sum = div64_s64(ret.lc_sum, ret.lc_count);
878                         seq_printf(m, "    %s_data_averages:\n"
879                                    "       bytes_per_rpc: %llu\n",
880                                    rw ? "write" : "read",
881                                    ret.lc_sum);
882                 }
883                 k = (int)ret.lc_sum;
884                 j = opcode_offset(OST_READ + rw) + EXTRA_MAX_OPCODES;
885                 header = &obd->obd_svc_stats->ls_cnt_header[j];
886                 lprocfs_stats_collect(obd->obd_svc_stats, j, &ret);
887                 if (ret.lc_sum > 0 && ret.lc_count != 0) {
888                         ret.lc_sum = div64_s64(ret.lc_sum, ret.lc_count);
889                         seq_printf(m, "       %s_per_rpc: %llu\n",
890                                    header->lc_units, ret.lc_sum);
891                         j = (int)ret.lc_sum;
892                         if (j > 0)
893                                 seq_printf(m, "       MB_per_sec: %u.%.02u\n",
894                                            k / j, (100 * k / j) % 100);
895                 }
896         }
897 }
898
899 int lprocfs_import_seq_show(struct seq_file *m, void *data)
900 {
901         struct obd_device *obd = (struct obd_device *)data;
902         struct obd_import *imp;
903         int rv;
904
905         LASSERT(obd != NULL);
906         with_imp_locked(obd, imp, rv)
907                 lprocfs_import_seq_show_locked(m, obd, imp);
908         return rv;
909 }
910 EXPORT_SYMBOL(lprocfs_import_seq_show);
911
912 int lprocfs_state_seq_show(struct seq_file *m, void *data)
913 {
914         struct obd_device *obd = (struct obd_device *)data;
915         struct obd_import *imp;
916         int j, k;
917         int rc;
918
919         LASSERT(obd != NULL);
920         with_imp_locked(obd, imp, rc) {
921                 seq_printf(m, "current_state: %s\n",
922                            ptlrpc_import_state_name(imp->imp_state));
923                 seq_printf(m, "state_history:\n");
924                 k = imp->imp_state_hist_idx;
925                 for (j = 0; j < IMP_STATE_HIST_LEN; j++) {
926                         struct import_state_hist *ish =
927                                 &imp->imp_state_hist[(k + j) % IMP_STATE_HIST_LEN];
928                         if (ish->ish_state == 0)
929                                 continue;
930                         seq_printf(m, " - [ %lld, %s ]\n", (s64)ish->ish_time,
931                                    ptlrpc_import_state_name(ish->ish_state));
932                 }
933         }
934
935         return rc;
936 }
937 EXPORT_SYMBOL(lprocfs_state_seq_show);
938
939 int lprocfs_at_hist_helper(struct seq_file *m, struct adaptive_timeout *at)
940 {
941         int i;
942         for (i = 0; i < AT_BINS; i++)
943                 seq_printf(m, "%3u ", at->at_hist[i]);
944         seq_printf(m, "\n");
945         return 0;
946 }
947 EXPORT_SYMBOL(lprocfs_at_hist_helper);
948
949 /* See also ptlrpc_lprocfs_timeouts_show_seq */
950 static void lprocfs_timeouts_seq_show_locked(struct seq_file *m,
951                                              struct obd_device *obd,
952                                              struct obd_import *imp)
953 {
954         timeout_t cur_timeout, worst_timeout;
955         time64_t now, worst_timestamp;
956         int i;
957
958         LASSERT(obd != NULL);
959
960         now = ktime_get_real_seconds();
961
962         /* Some network health info for kicks */
963         seq_printf(m, "%-10s : %lld, %llds ago\n",
964                    "last reply", (s64)imp->imp_last_reply_time,
965                    (s64)(now - imp->imp_last_reply_time));
966
967         cur_timeout = at_get(&imp->imp_at.iat_net_latency);
968         worst_timeout = imp->imp_at.iat_net_latency.at_worst_timeout_ever;
969         worst_timestamp = imp->imp_at.iat_net_latency.at_worst_timestamp;
970         seq_printf(m, "%-10s : cur %3u  worst %3u (at %lld, %llds ago) ",
971                    "network", cur_timeout, worst_timeout, worst_timestamp,
972                    now - worst_timestamp);
973         lprocfs_at_hist_helper(m, &imp->imp_at.iat_net_latency);
974
975         for(i = 0; i < IMP_AT_MAX_PORTALS; i++) {
976                 struct adaptive_timeout *service_est;
977
978                 if (imp->imp_at.iat_portal[i] == 0)
979                         break;
980
981                 service_est = &imp->imp_at.iat_service_estimate[i];
982                 cur_timeout = at_get(service_est);
983                 worst_timeout = service_est->at_worst_timeout_ever;
984                 worst_timestamp = service_est->at_worst_timestamp;
985                 seq_printf(m, "portal %-2d  : cur %3u  worst %3u (at %lld, %llds ago) ",
986                            imp->imp_at.iat_portal[i], cur_timeout,
987                            worst_timeout, worst_timestamp,
988                            now - worst_timestamp);
989                 lprocfs_at_hist_helper(m, service_est);
990         }
991 }
992
993 int lprocfs_timeouts_seq_show(struct seq_file *m, void *data)
994 {
995         struct obd_device *obd = (struct obd_device *)data;
996         struct obd_import *imp;
997         int rc;
998
999         with_imp_locked(obd, imp, rc)
1000                 lprocfs_timeouts_seq_show_locked(m, obd, imp);
1001         return rc;
1002 }
1003 EXPORT_SYMBOL(lprocfs_timeouts_seq_show);
1004
1005 int lprocfs_connect_flags_seq_show(struct seq_file *m, void *data)
1006 {
1007         struct obd_device *obd = data;
1008         __u64 flags;
1009         __u64 flags2;
1010         struct obd_import *imp;
1011         int rc;
1012
1013         with_imp_locked(obd, imp, rc) {
1014                 flags = imp->imp_connect_data.ocd_connect_flags;
1015                 flags2 = imp->imp_connect_data.ocd_connect_flags2;
1016                 seq_printf(m, "flags=%#llx\n", flags);
1017                 seq_printf(m, "flags2=%#llx\n", flags2);
1018                 obd_connect_seq_flags2str(m, flags, flags2, "\n");
1019                 seq_printf(m, "\n");
1020         }
1021
1022         return rc;
1023 }
1024 EXPORT_SYMBOL(lprocfs_connect_flags_seq_show);
1025
1026 static const struct attribute *obd_def_uuid_attrs[] = {
1027         &lustre_attr_uuid.attr,
1028         NULL,
1029 };
1030
1031 static const struct attribute *obd_def_attrs[] = {
1032         &lustre_attr_blocksize.attr,
1033         &lustre_attr_kbytestotal.attr,
1034         &lustre_attr_kbytesfree.attr,
1035         &lustre_attr_kbytesavail.attr,
1036         &lustre_attr_filestotal.attr,
1037         &lustre_attr_filesfree.attr,
1038         &lustre_attr_uuid.attr,
1039         NULL,
1040 };
1041
1042 static void obd_sysfs_release(struct kobject *kobj)
1043 {
1044         struct obd_device *obd = container_of(kobj, struct obd_device,
1045                                               obd_kset.kobj);
1046
1047         complete(&obd->obd_kobj_unregister);
1048 }
1049
1050 int lprocfs_obd_setup(struct obd_device *obd, bool uuid_only)
1051 {
1052         struct ldebugfs_vars *debugfs_vars = NULL;
1053         int rc;
1054
1055         if (!obd || obd->obd_magic != OBD_DEVICE_MAGIC)
1056                 return -ENODEV;
1057
1058         rc = kobject_set_name(&obd->obd_kset.kobj, "%s", obd->obd_name);
1059         if (rc)
1060                 return rc;
1061
1062         obd->obd_ktype.sysfs_ops = &lustre_sysfs_ops;
1063         obd->obd_ktype.release = obd_sysfs_release;
1064
1065         obd->obd_kset.kobj.parent = &obd->obd_type->typ_kobj;
1066         obd->obd_kset.kobj.ktype = &obd->obd_ktype;
1067         init_completion(&obd->obd_kobj_unregister);
1068         rc = kset_register(&obd->obd_kset);
1069         if (rc)
1070                 return rc;
1071
1072         if (uuid_only)
1073                 obd->obd_attrs = obd_def_uuid_attrs;
1074         else
1075                 obd->obd_attrs = obd_def_attrs;
1076
1077         rc = sysfs_create_files(&obd->obd_kset.kobj, obd->obd_attrs);
1078         if (rc) {
1079                 kset_unregister(&obd->obd_kset);
1080                 return rc;
1081         }
1082
1083         if (!obd->obd_type->typ_procroot)
1084                 debugfs_vars = obd->obd_debugfs_vars;
1085         obd->obd_debugfs_entry = debugfs_create_dir(
1086                 obd->obd_name, obd->obd_type->typ_debugfs_entry);
1087         ldebugfs_add_vars(obd->obd_debugfs_entry, debugfs_vars, obd);
1088
1089         if (obd->obd_proc_entry || !obd->obd_type->typ_procroot)
1090                 GOTO(already_registered, rc);
1091
1092         obd->obd_proc_entry = lprocfs_register(obd->obd_name,
1093                                                obd->obd_type->typ_procroot,
1094                                                obd->obd_vars, obd);
1095         if (IS_ERR(obd->obd_proc_entry)) {
1096                 rc = PTR_ERR(obd->obd_proc_entry);
1097                 CERROR("error %d setting up lprocfs for %s\n",rc,obd->obd_name);
1098                 obd->obd_proc_entry = NULL;
1099
1100                 debugfs_remove_recursive(obd->obd_debugfs_entry);
1101                 obd->obd_debugfs_entry = NULL;
1102
1103                 sysfs_remove_files(&obd->obd_kset.kobj, obd->obd_attrs);
1104                 obd->obd_attrs = NULL;
1105                 kset_unregister(&obd->obd_kset);
1106                 return rc;
1107         }
1108 already_registered:
1109         return rc;
1110 }
1111 EXPORT_SYMBOL(lprocfs_obd_setup);
1112
1113 int lprocfs_obd_cleanup(struct obd_device *obd)
1114 {
1115         if (!obd)
1116                 return -EINVAL;
1117
1118         if (obd->obd_proc_exports_entry) {
1119                 /* Should be no exports left */
1120                 lprocfs_remove(&obd->obd_proc_exports_entry);
1121                 obd->obd_proc_exports_entry = NULL;
1122         }
1123
1124         if (obd->obd_proc_entry) {
1125                 lprocfs_remove(&obd->obd_proc_entry);
1126                 obd->obd_proc_entry = NULL;
1127         }
1128
1129         debugfs_remove_recursive(obd->obd_debugfs_entry);
1130         obd->obd_debugfs_entry = NULL;
1131
1132         /* obd device never allocated a kset */
1133         if (!obd->obd_kset.kobj.state_initialized)
1134                 return 0;
1135
1136         if (obd->obd_attrs) {
1137                 sysfs_remove_files(&obd->obd_kset.kobj, obd->obd_attrs);
1138                 obd->obd_attrs = NULL;
1139         }
1140
1141         kset_unregister(&obd->obd_kset);
1142         wait_for_completion(&obd->obd_kobj_unregister);
1143         return 0;
1144 }
1145 EXPORT_SYMBOL(lprocfs_obd_cleanup);
1146
1147 int lprocfs_stats_alloc_one(struct lprocfs_stats *stats, unsigned int cpuid)
1148 {
1149         struct lprocfs_counter *cntr;
1150         unsigned int percpusize;
1151         int rc = -ENOMEM;
1152         unsigned long flags = 0;
1153         int i;
1154
1155         LASSERT(stats->ls_percpu[cpuid] == NULL);
1156         LASSERT((stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) == 0);
1157
1158         percpusize = lprocfs_stats_counter_size(stats);
1159         LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[cpuid], percpusize);
1160         if (stats->ls_percpu[cpuid]) {
1161                 rc = 0;
1162                 if (unlikely(stats->ls_biggest_alloc_num <= cpuid)) {
1163                         if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1164                                 spin_lock_irqsave(&stats->ls_lock, flags);
1165                         else
1166                                 spin_lock(&stats->ls_lock);
1167                         if (stats->ls_biggest_alloc_num <= cpuid)
1168                                 stats->ls_biggest_alloc_num = cpuid + 1;
1169                         if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) {
1170                                 spin_unlock_irqrestore(&stats->ls_lock, flags);
1171                         } else {
1172                                 spin_unlock(&stats->ls_lock);
1173                         }
1174                 }
1175                 /* initialize the ls_percpu[cpuid] non-zero counter */
1176                 for (i = 0; i < stats->ls_num; ++i) {
1177                         cntr = lprocfs_stats_counter_get(stats, cpuid, i);
1178                         cntr->lc_min = LC_MIN_INIT;
1179                 }
1180         }
1181         return rc;
1182 }
1183
1184 struct lprocfs_stats *lprocfs_alloc_stats(unsigned int num,
1185                                           enum lprocfs_stats_flags flags)
1186 {
1187         struct lprocfs_stats *stats;
1188         unsigned int num_entry;
1189         unsigned int percpusize = 0;
1190         int i;
1191
1192         if (num == 0)
1193                 return NULL;
1194
1195         if (lprocfs_no_percpu_stats != 0)
1196                 flags |= LPROCFS_STATS_FLAG_NOPERCPU;
1197
1198         if (flags & LPROCFS_STATS_FLAG_NOPERCPU)
1199                 num_entry = 1;
1200         else
1201                 num_entry = num_possible_cpus();
1202
1203         /* alloc percpu pointers for all possible cpu slots */
1204         LIBCFS_ALLOC(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1205         if (!stats)
1206                 return NULL;
1207
1208         stats->ls_num = num;
1209         stats->ls_flags = flags;
1210         spin_lock_init(&stats->ls_lock);
1211
1212         /* alloc num of counter headers */
1213         CFS_ALLOC_PTR_ARRAY(stats->ls_cnt_header, stats->ls_num);
1214         if (!stats->ls_cnt_header)
1215                 goto fail;
1216
1217         if ((flags & LPROCFS_STATS_FLAG_NOPERCPU) != 0) {
1218                 /* contains only one set counters */
1219                 percpusize = lprocfs_stats_counter_size(stats);
1220                 LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[0], percpusize);
1221                 if (!stats->ls_percpu[0])
1222                         goto fail;
1223                 stats->ls_biggest_alloc_num = 1;
1224         } else if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0) {
1225                 /* alloc all percpu data, currently only obd_memory use this */
1226                 for (i = 0; i < num_entry; ++i)
1227                         if (lprocfs_stats_alloc_one(stats, i) < 0)
1228                                 goto fail;
1229         }
1230
1231         return stats;
1232
1233 fail:
1234         lprocfs_free_stats(&stats);
1235         return NULL;
1236 }
1237 EXPORT_SYMBOL(lprocfs_alloc_stats);
1238
1239 void lprocfs_free_stats(struct lprocfs_stats **statsh)
1240 {
1241         struct lprocfs_stats *stats = *statsh;
1242         unsigned int num_entry;
1243         unsigned int percpusize;
1244         unsigned int i;
1245
1246         if (!stats || stats->ls_num == 0)
1247                 return;
1248         *statsh = NULL;
1249
1250         if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU)
1251                 num_entry = 1;
1252         else
1253                 num_entry = num_possible_cpus();
1254
1255         percpusize = lprocfs_stats_counter_size(stats);
1256         for (i = 0; i < num_entry; i++)
1257                 if (stats->ls_percpu[i])
1258                         LIBCFS_FREE(stats->ls_percpu[i], percpusize);
1259         if (stats->ls_cnt_header)
1260                 CFS_FREE_PTR_ARRAY(stats->ls_cnt_header, stats->ls_num);
1261         LIBCFS_FREE(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1262 }
1263 EXPORT_SYMBOL(lprocfs_free_stats);
1264
1265 u64 lprocfs_stats_collector(struct lprocfs_stats *stats, int idx,
1266                             enum lprocfs_fields_flags field)
1267 {
1268         unsigned long flags = 0;
1269         unsigned int num_cpu;
1270         unsigned int i;
1271         u64 ret = 0;
1272
1273         LASSERT(stats);
1274
1275         num_cpu = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1276         for (i = 0; i < num_cpu; i++) {
1277                 struct lprocfs_counter *cntr;
1278
1279                 if (!stats->ls_percpu[i])
1280                         continue;
1281
1282                 cntr = lprocfs_stats_counter_get(stats, i, idx);
1283                 ret += lprocfs_read_helper(cntr, &stats->ls_cnt_header[idx],
1284                                            stats->ls_flags, field);
1285         }
1286         lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1287         return ret;
1288 }
1289 EXPORT_SYMBOL(lprocfs_stats_collector);
1290
1291 void lprocfs_clear_stats(struct lprocfs_stats *stats)
1292 {
1293         struct lprocfs_counter *percpu_cntr;
1294         int i;
1295         int j;
1296         unsigned int num_entry;
1297         unsigned long flags = 0;
1298
1299         num_entry = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1300
1301         for (i = 0; i < num_entry; i++) {
1302                 if (!stats->ls_percpu[i])
1303                         continue;
1304                 for (j = 0; j < stats->ls_num; j++) {
1305                         percpu_cntr = lprocfs_stats_counter_get(stats, i, j);
1306                         percpu_cntr->lc_count           = 0;
1307                         percpu_cntr->lc_min             = LC_MIN_INIT;
1308                         percpu_cntr->lc_max             = 0;
1309                         percpu_cntr->lc_sumsquare       = 0;
1310                         percpu_cntr->lc_sum             = 0;
1311                         if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1312                                 percpu_cntr->lc_sum_irq = 0;
1313                 }
1314         }
1315
1316         lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1317 }
1318 EXPORT_SYMBOL(lprocfs_clear_stats);
1319
1320 static ssize_t lprocfs_stats_seq_write(struct file *file,
1321                                        const char __user *buf,
1322                                        size_t len, loff_t *off)
1323 {
1324         struct seq_file *seq = file->private_data;
1325         struct lprocfs_stats *stats = seq->private;
1326
1327         lprocfs_clear_stats(stats);
1328
1329         return len;
1330 }
1331
1332 static void *lprocfs_stats_seq_start(struct seq_file *p, loff_t *pos)
1333 {
1334         struct lprocfs_stats *stats = p->private;
1335
1336         return (*pos < stats->ls_num) ? pos : NULL;
1337 }
1338
1339 static void lprocfs_stats_seq_stop(struct seq_file *p, void *v)
1340 {
1341 }
1342
1343 static void *lprocfs_stats_seq_next(struct seq_file *p, void *v, loff_t *pos)
1344 {
1345         (*pos)++;
1346
1347         return lprocfs_stats_seq_start(p, pos);
1348 }
1349
1350 /* seq file export of one lprocfs counter */
1351 static int lprocfs_stats_seq_show(struct seq_file *p, void *v)
1352 {
1353         struct lprocfs_stats *stats = p->private;
1354         struct lprocfs_counter_header *hdr;
1355         struct lprocfs_counter ctr;
1356         int idx = *(loff_t *)v;
1357
1358         if (idx == 0) {
1359                 struct timespec64 now;
1360
1361                 ktime_get_real_ts64(&now);
1362                 seq_printf(p, "%-25s %llu.%09lu secs.nsecs\n",
1363                            "snapshot_time", (s64)now.tv_sec, now.tv_nsec);
1364         }
1365
1366         hdr = &stats->ls_cnt_header[idx];
1367         lprocfs_stats_collect(stats, idx, &ctr);
1368
1369         if (ctr.lc_count == 0)
1370                 return 0;
1371
1372         seq_printf(p, "%-25s %lld samples [%s]", hdr->lc_name,
1373                    ctr.lc_count, hdr->lc_units);
1374
1375         if ((hdr->lc_config & LPROCFS_CNTR_AVGMINMAX) && ctr.lc_count > 0) {
1376                 seq_printf(p, " %lld %lld %lld",
1377                            ctr.lc_min, ctr.lc_max, ctr.lc_sum);
1378                 if (hdr->lc_config & LPROCFS_CNTR_STDDEV)
1379                         seq_printf(p, " %llu", ctr.lc_sumsquare);
1380         }
1381         seq_putc(p, '\n');
1382         return 0;
1383 }
1384
1385 static const struct seq_operations lprocfs_stats_seq_sops = {
1386         .start  = lprocfs_stats_seq_start,
1387         .stop   = lprocfs_stats_seq_stop,
1388         .next   = lprocfs_stats_seq_next,
1389         .show   = lprocfs_stats_seq_show,
1390 };
1391
1392 static int lprocfs_stats_seq_open(struct inode *inode, struct file *file)
1393 {
1394         struct seq_file *seq;
1395         int rc;
1396
1397         rc = seq_open(file, &lprocfs_stats_seq_sops);
1398         if (rc)
1399                 return rc;
1400         seq = file->private_data;
1401         seq->private = inode->i_private ? inode->i_private : PDE_DATA(inode);
1402         return 0;
1403 }
1404
1405 const struct file_operations ldebugfs_stats_seq_fops = {
1406         .owner   = THIS_MODULE,
1407         .open    = lprocfs_stats_seq_open,
1408         .read    = seq_read,
1409         .write   = lprocfs_stats_seq_write,
1410         .llseek  = seq_lseek,
1411         .release = lprocfs_seq_release,
1412 };
1413 EXPORT_SYMBOL(ldebugfs_stats_seq_fops);
1414
1415 static const struct file_operations lprocfs_stats_seq_fops = {
1416         .owner   = THIS_MODULE,
1417         .open    = lprocfs_stats_seq_open,
1418         .read    = seq_read,
1419         .write   = lprocfs_stats_seq_write,
1420         .llseek  = seq_lseek,
1421         .release = lprocfs_seq_release,
1422 };
1423
1424 int lprocfs_register_stats(struct proc_dir_entry *root, const char *name,
1425                            struct lprocfs_stats *stats)
1426 {
1427         struct proc_dir_entry *entry;
1428         LASSERT(root != NULL);
1429
1430         entry = proc_create_data(name, 0644, root,
1431                                  &lprocfs_stats_seq_fops, stats);
1432         if (!entry)
1433                 return -ENOMEM;
1434         return 0;
1435 }
1436 EXPORT_SYMBOL(lprocfs_register_stats);
1437
1438 void lprocfs_counter_init(struct lprocfs_stats *stats, int index,
1439                           unsigned conf, const char *name, const char *units)
1440 {
1441         struct lprocfs_counter_header *header;
1442         struct lprocfs_counter *percpu_cntr;
1443         unsigned long flags = 0;
1444         unsigned int i;
1445         unsigned int num_cpu;
1446
1447         LASSERT(stats != NULL);
1448
1449         header = &stats->ls_cnt_header[index];
1450         LASSERTF(header != NULL, "Failed to allocate stats header:[%d]%s/%s\n",
1451                  index, name, units);
1452
1453         header->lc_config = conf;
1454         header->lc_name   = name;
1455         header->lc_units  = units;
1456
1457         num_cpu = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1458         for (i = 0; i < num_cpu; ++i) {
1459                 if (!stats->ls_percpu[i])
1460                         continue;
1461                 percpu_cntr = lprocfs_stats_counter_get(stats, i, index);
1462                 percpu_cntr->lc_count           = 0;
1463                 percpu_cntr->lc_min             = LC_MIN_INIT;
1464                 percpu_cntr->lc_max             = 0;
1465                 percpu_cntr->lc_sumsquare       = 0;
1466                 percpu_cntr->lc_sum             = 0;
1467                 if ((stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1468                         percpu_cntr->lc_sum_irq = 0;
1469         }
1470         lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1471 }
1472 EXPORT_SYMBOL(lprocfs_counter_init);
1473
1474 static const char * const mps_stats[] = {
1475         [LPROC_MD_CLOSE]                = "close",
1476         [LPROC_MD_CREATE]               = "create",
1477         [LPROC_MD_ENQUEUE]              = "enqueue",
1478         [LPROC_MD_GETATTR]              = "getattr",
1479         [LPROC_MD_INTENT_LOCK]          = "intent_lock",
1480         [LPROC_MD_LINK]                 = "link",
1481         [LPROC_MD_RENAME]               = "rename",
1482         [LPROC_MD_SETATTR]              = "setattr",
1483         [LPROC_MD_FSYNC]                = "fsync",
1484         [LPROC_MD_READ_PAGE]            = "read_page",
1485         [LPROC_MD_UNLINK]               = "unlink",
1486         [LPROC_MD_SETXATTR]             = "setxattr",
1487         [LPROC_MD_GETXATTR]             = "getxattr",
1488         [LPROC_MD_INTENT_GETATTR_ASYNC] = "intent_getattr_async",
1489         [LPROC_MD_REVALIDATE_LOCK]      = "revalidate_lock",
1490 };
1491
1492 int lprocfs_alloc_md_stats(struct obd_device *obd,
1493                            unsigned int num_private_stats)
1494 {
1495         struct lprocfs_stats *stats;
1496         unsigned int num_stats;
1497         int rc, i;
1498
1499         /*
1500          * TODO Ensure that this function is only used where
1501          * appropriate by adding an assertion to the effect that
1502          * obd->obd_type->typ_md_ops is not NULL. We can't do this now
1503          * because mdt_procfs_init() uses this function to allocate
1504          * the stats backing /proc/fs/lustre/mdt/.../md_stats but the
1505          * mdt layer does not use the md_ops interface. This is
1506          * confusing and a waste of memory. See LU-2484.
1507          */
1508         LASSERT(obd->obd_proc_entry != NULL);
1509         LASSERT(obd->obd_md_stats == NULL);
1510
1511         num_stats = ARRAY_SIZE(mps_stats) + num_private_stats;
1512         stats = lprocfs_alloc_stats(num_stats, 0);
1513         if (!stats)
1514                 return -ENOMEM;
1515
1516         for (i = 0; i < ARRAY_SIZE(mps_stats); i++) {
1517                 lprocfs_counter_init(stats, i, 0, mps_stats[i], "reqs");
1518                 if (!stats->ls_cnt_header[i].lc_name) {
1519                         CERROR("Missing md_stat initializer md_op operation at offset %d. Aborting.\n",
1520                                i);
1521                         LBUG();
1522                 }
1523         }
1524
1525         rc = lprocfs_register_stats(obd->obd_proc_entry, "md_stats", stats);
1526         if (rc < 0) {
1527                 lprocfs_free_stats(&stats);
1528         } else {
1529                 obd->obd_md_stats = stats;
1530         }
1531
1532         return rc;
1533 }
1534 EXPORT_SYMBOL(lprocfs_alloc_md_stats);
1535
1536 void lprocfs_free_md_stats(struct obd_device *obd)
1537 {
1538         struct lprocfs_stats *stats = obd->obd_md_stats;
1539
1540         if (stats) {
1541                 obd->obd_md_stats = NULL;
1542                 lprocfs_free_stats(&stats);
1543         }
1544 }
1545 EXPORT_SYMBOL(lprocfs_free_md_stats);
1546
1547 void lprocfs_init_ldlm_stats(struct lprocfs_stats *ldlm_stats)
1548 {
1549         lprocfs_counter_init(ldlm_stats,
1550                              LDLM_ENQUEUE - LDLM_FIRST_OPC,
1551                              0, "ldlm_enqueue", "reqs");
1552         lprocfs_counter_init(ldlm_stats,
1553                              LDLM_CONVERT - LDLM_FIRST_OPC,
1554                              0, "ldlm_convert", "reqs");
1555         lprocfs_counter_init(ldlm_stats,
1556                              LDLM_CANCEL - LDLM_FIRST_OPC,
1557                              0, "ldlm_cancel", "reqs");
1558         lprocfs_counter_init(ldlm_stats,
1559                              LDLM_BL_CALLBACK - LDLM_FIRST_OPC,
1560                              0, "ldlm_bl_callback", "reqs");
1561         lprocfs_counter_init(ldlm_stats,
1562                              LDLM_CP_CALLBACK - LDLM_FIRST_OPC,
1563                              0, "ldlm_cp_callback", "reqs");
1564         lprocfs_counter_init(ldlm_stats,
1565                              LDLM_GL_CALLBACK - LDLM_FIRST_OPC,
1566                              0, "ldlm_gl_callback", "reqs");
1567 }
1568 EXPORT_SYMBOL(lprocfs_init_ldlm_stats);
1569
1570 __s64 lprocfs_read_helper(struct lprocfs_counter *lc,
1571                           struct lprocfs_counter_header *header,
1572                           enum lprocfs_stats_flags flags,
1573                           enum lprocfs_fields_flags field)
1574 {
1575         __s64 ret = 0;
1576
1577         if (!lc || !header)
1578                 RETURN(0);
1579
1580         switch (field) {
1581                 case LPROCFS_FIELDS_FLAGS_CONFIG:
1582                         ret = header->lc_config;
1583                         break;
1584                 case LPROCFS_FIELDS_FLAGS_SUM:
1585                         ret = lc->lc_sum;
1586                         if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1587                                 ret += lc->lc_sum_irq;
1588                         break;
1589                 case LPROCFS_FIELDS_FLAGS_MIN:
1590                         ret = lc->lc_min;
1591                         break;
1592                 case LPROCFS_FIELDS_FLAGS_MAX:
1593                         ret = lc->lc_max;
1594                         break;
1595                 case LPROCFS_FIELDS_FLAGS_AVG:
1596                         ret = (lc->lc_max - lc->lc_min) / 2;
1597                         break;
1598                 case LPROCFS_FIELDS_FLAGS_SUMSQUARE:
1599                         ret = lc->lc_sumsquare;
1600                         break;
1601                 case LPROCFS_FIELDS_FLAGS_COUNT:
1602                         ret = lc->lc_count;
1603                         break;
1604                 default:
1605                         break;
1606         };
1607         RETURN(ret);
1608 }
1609 EXPORT_SYMBOL(lprocfs_read_helper);
1610
1611 /**
1612  * string_to_size - convert ASCII string representing a numerical
1613  *                  value with optional units to 64-bit binary value
1614  *
1615  * @size:       The numerical value extract out of @buffer
1616  * @buffer:     passed in string to parse
1617  * @count:      length of the @buffer
1618  *
1619  * This function returns a 64-bit binary value if @buffer contains a valid
1620  * numerical string. The string is parsed to 3 significant figures after
1621  * the decimal point. Support the string containing an optional units at
1622  * the end which can be base 2 or base 10 in value. If no units are given
1623  * the string is assumed to just a numerical value.
1624  *
1625  * Returns:     @count if the string is successfully parsed,
1626  *              -errno on invalid input strings. Error values:
1627  *
1628  *  - ``-EINVAL``: @buffer is not a proper numerical string
1629  *  - ``-EOVERFLOW``: results does not fit into 64 bits.
1630  *  - ``-E2BIG ``: @buffer is too large (not a valid number)
1631  */
1632 int string_to_size(u64 *size, const char *buffer, size_t count)
1633 {
1634         /* For string_get_size() it can support values above exabytes,
1635          * (ZiB, YiB) due to breaking the return value into a size and
1636          * bulk size to avoid 64 bit overflow. We don't break the size
1637          * up into block size units so we don't support ZiB or YiB.
1638          */
1639         static const char *const units_10[] = {
1640                 "kB", "MB", "GB", "TB", "PB", "EB",
1641         };
1642         static const char *const units_2[] = {
1643                 "K",  "M",  "G",  "T",  "P",  "E",
1644         };
1645         static const char *const *const units_str[] = {
1646                 [STRING_UNITS_2] = units_2,
1647                 [STRING_UNITS_10] = units_10,
1648         };
1649         static const unsigned int coeff[] = {
1650                 [STRING_UNITS_10] = 1000,
1651                 [STRING_UNITS_2] = 1024,
1652         };
1653         enum string_size_units unit = STRING_UNITS_2;
1654         u64 whole, blk_size = 1;
1655         char kernbuf[22], *end;
1656         size_t len = count;
1657         int rc;
1658         int i;
1659
1660         if (count >= sizeof(kernbuf)) {
1661                 CERROR("count %zd > buffer %zd\n", count, sizeof(kernbuf));
1662                 return -E2BIG;
1663         }
1664
1665         *size = 0;
1666         /* The "iB" suffix is optionally allowed for indicating base-2 numbers.
1667          * If suffix is only "B" and not "iB" then we treat it as base-10.
1668          */
1669         end = strstr(buffer, "B");
1670         if (end && *(end - 1) != 'i')
1671                 unit = STRING_UNITS_10;
1672
1673         i = unit == STRING_UNITS_2 ? ARRAY_SIZE(units_2) - 1 :
1674                                      ARRAY_SIZE(units_10) - 1;
1675         do {
1676                 end = strnstr(buffer, units_str[unit][i], count);
1677                 if (end) {
1678                         for (; i >= 0; i--)
1679                                 blk_size *= coeff[unit];
1680                         len = end - buffer;
1681                         break;
1682                 }
1683         } while (i--);
1684
1685         /* as 'B' is a substring of all units, we need to handle it
1686          * separately.
1687          */
1688         if (!end) {
1689                 /* 'B' is only acceptable letter at this point */
1690                 end = strnchr(buffer, count, 'B');
1691                 if (end) {
1692                         len = end - buffer;
1693
1694                         if (count - len > 2 ||
1695                             (count - len == 2 && strcmp(end, "B\n") != 0)) {
1696                                 CDEBUG(D_INFO, "unknown suffix '%s'\n", buffer);
1697                                 return -EINVAL;
1698                         }
1699                 }
1700                 /* kstrtoull will error out if it has non digits */
1701                 goto numbers_only;
1702         }
1703
1704         end = strnchr(buffer, count, '.');
1705         if (end) {
1706                 /* need to limit 3 decimal places */
1707                 char rem[4] = "000";
1708                 u64 frac = 0;
1709                 size_t off;
1710
1711                 len = end - buffer;
1712                 end++;
1713
1714                 /* limit to 3 decimal points */
1715                 off = min_t(size_t, 3, strspn(end, "0123456789"));
1716                 /* need to limit frac_d to a u32 */
1717                 memcpy(rem, end, off);
1718                 rc = kstrtoull(rem, 10, &frac);
1719                 if (rc)
1720                         return rc;
1721
1722                 if (fls64(frac) + fls64(blk_size) - 1 > 64)
1723                         return -EOVERFLOW;
1724
1725                 frac *= blk_size;
1726                 do_div(frac, 1000);
1727                 *size += frac;
1728         }
1729 numbers_only:
1730         snprintf(kernbuf, sizeof(kernbuf), "%.*s", (int)len, buffer);
1731         rc = kstrtoull(kernbuf, 10, &whole);
1732         if (rc)
1733                 return rc;
1734
1735         if (whole != 0 && fls64(whole) + fls64(blk_size) - 1 > 64)
1736                 return -EOVERFLOW;
1737
1738         *size += whole * blk_size;
1739
1740         return count;
1741 }
1742 EXPORT_SYMBOL(string_to_size);
1743
1744 /**
1745  * sysfs_memparse - parse a ASCII string to 64-bit binary value,
1746  *                  with optional units
1747  *
1748  * @buffer:     kernel pointer to input string
1749  * @count:      number of bytes in the input @buffer
1750  * @val:        (output) binary value returned to caller
1751  * @defunit:    default unit suffix to use if none is provided
1752  *
1753  * Parses a string into a number. The number stored at @buffer is
1754  * potentially suffixed with K, M, G, T, P, E. Besides these other
1755  * valid suffix units are shown in the string_to_size() function.
1756  * If the string lacks a suffix then the defunit is used. The defunit
1757  * should be given as a binary unit (e.g. MiB) as that is the standard
1758  * for tunables in Lustre. If no unit suffix is given (e.g. 'G'), then
1759  * it is assumed to be in binary units.
1760  *
1761  * Returns:     0 on success or -errno on failure.
1762  */
1763 int sysfs_memparse(const char *buffer, size_t count, u64 *val,
1764                    const char *defunit)
1765 {
1766         const char *param = buffer;
1767         char tmp_buf[23];
1768         int rc;
1769
1770         count = strlen(buffer);
1771         while (count > 0 && isspace(buffer[count - 1]))
1772                 count--;
1773
1774         if (!count)
1775                 RETURN(-EINVAL);
1776
1777         /* If there isn't already a unit on this value, append @defunit.
1778          * Units of 'B' don't affect the value, so don't bother adding.
1779          */
1780         if (!isalpha(buffer[count - 1]) && defunit[0] != 'B') {
1781                 if (count + 3 >= sizeof(tmp_buf)) {
1782                         CERROR("count %zd > size %zd\n", count, sizeof(param));
1783                         RETURN(-E2BIG);
1784                 }
1785
1786                 scnprintf(tmp_buf, sizeof(tmp_buf), "%.*s%s", (int)count,
1787                           buffer, defunit);
1788                 param = tmp_buf;
1789                 count = strlen(param);
1790         }
1791
1792         rc = string_to_size(val, param, count);
1793
1794         return rc < 0 ? rc : 0;
1795 }
1796 EXPORT_SYMBOL(sysfs_memparse);
1797
1798 char *lprocfs_strnstr(const char *s1, const char *s2, size_t len)
1799 {
1800         size_t l2;
1801
1802         l2 = strlen(s2);
1803         if (!l2)
1804                 return (char *)s1;
1805         while (len >= l2) {
1806                 len--;
1807                 if (!memcmp(s1, s2, l2))
1808                         return (char *)s1;
1809                 s1++;
1810         }
1811         return NULL;
1812 }
1813 EXPORT_SYMBOL(lprocfs_strnstr);
1814
1815 /**
1816  * Find the string \a name in the input \a buffer, and return a pointer to the
1817  * value immediately following \a name, reducing \a count appropriately.
1818  * If \a name is not found the original \a buffer is returned.
1819  */
1820 char *lprocfs_find_named_value(const char *buffer, const char *name,
1821                                 size_t *count)
1822 {
1823         char *val;
1824         size_t buflen = *count;
1825
1826         /* there is no strnstr() in rhel5 and ubuntu kernels */
1827         val = lprocfs_strnstr(buffer, name, buflen);
1828         if (!val)
1829                 return (char *)buffer;
1830
1831         val += strlen(name);                             /* skip prefix */
1832         while (val < buffer + buflen && isspace(*val)) /* skip separator */
1833                 val++;
1834
1835         *count = 0;
1836         while (val < buffer + buflen && isalnum(*val)) {
1837                 ++*count;
1838                 ++val;
1839         }
1840
1841         return val - *count;
1842 }
1843 EXPORT_SYMBOL(lprocfs_find_named_value);
1844
1845 int lprocfs_seq_create(struct proc_dir_entry *parent,
1846                        const char *name,
1847                        mode_t mode,
1848                        const struct file_operations *seq_fops,
1849                        void *data)
1850 {
1851         struct proc_dir_entry *entry;
1852         ENTRY;
1853
1854         /* Disallow secretly (un)writable entries. */
1855         LASSERT((seq_fops->write == NULL) == ((mode & 0222) == 0));
1856
1857         entry = proc_create_data(name, mode, parent, seq_fops, data);
1858
1859         if (!entry)
1860                 RETURN(-ENOMEM);
1861
1862         RETURN(0);
1863 }
1864 EXPORT_SYMBOL(lprocfs_seq_create);
1865
1866 int lprocfs_obd_seq_create(struct obd_device *obd,
1867                            const char *name,
1868                            mode_t mode,
1869                            const struct file_operations *seq_fops,
1870                            void *data)
1871 {
1872         return lprocfs_seq_create(obd->obd_proc_entry, name,
1873                                   mode, seq_fops, data);
1874 }
1875 EXPORT_SYMBOL(lprocfs_obd_seq_create);
1876
1877 void lprocfs_oh_tally(struct obd_histogram *oh, unsigned int value)
1878 {
1879         if (value >= OBD_HIST_MAX)
1880                 value = OBD_HIST_MAX - 1;
1881
1882         spin_lock(&oh->oh_lock);
1883         oh->oh_buckets[value]++;
1884         spin_unlock(&oh->oh_lock);
1885 }
1886 EXPORT_SYMBOL(lprocfs_oh_tally);
1887
1888 void lprocfs_oh_tally_log2(struct obd_histogram *oh, unsigned int value)
1889 {
1890         unsigned int val = 0;
1891
1892         if (likely(value != 0))
1893                 val = min(fls(value - 1), OBD_HIST_MAX);
1894
1895         lprocfs_oh_tally(oh, val);
1896 }
1897 EXPORT_SYMBOL(lprocfs_oh_tally_log2);
1898
1899 unsigned long lprocfs_oh_sum(struct obd_histogram *oh)
1900 {
1901         unsigned long ret = 0;
1902         int i;
1903
1904         for (i = 0; i < OBD_HIST_MAX; i++)
1905                 ret +=  oh->oh_buckets[i];
1906         return ret;
1907 }
1908 EXPORT_SYMBOL(lprocfs_oh_sum);
1909
1910 void lprocfs_oh_clear(struct obd_histogram *oh)
1911 {
1912         spin_lock(&oh->oh_lock);
1913         memset(oh->oh_buckets, 0, sizeof(oh->oh_buckets));
1914         spin_unlock(&oh->oh_lock);
1915 }
1916 EXPORT_SYMBOL(lprocfs_oh_clear);
1917
1918 ssize_t lustre_attr_show(struct kobject *kobj,
1919                          struct attribute *attr, char *buf)
1920 {
1921         struct lustre_attr *a = container_of(attr, struct lustre_attr, attr);
1922
1923         return a->show ? a->show(kobj, attr, buf) : 0;
1924 }
1925 EXPORT_SYMBOL_GPL(lustre_attr_show);
1926
1927 ssize_t lustre_attr_store(struct kobject *kobj, struct attribute *attr,
1928                           const char *buf, size_t len)
1929 {
1930         struct lustre_attr *a = container_of(attr, struct lustre_attr, attr);
1931
1932         return a->store ? a->store(kobj, attr, buf, len) : len;
1933 }
1934 EXPORT_SYMBOL_GPL(lustre_attr_store);
1935
1936 const struct sysfs_ops lustre_sysfs_ops = {
1937         .show  = lustre_attr_show,
1938         .store = lustre_attr_store,
1939 };
1940 EXPORT_SYMBOL_GPL(lustre_sysfs_ops);
1941
1942 int lprocfs_obd_max_pages_per_rpc_seq_show(struct seq_file *m, void *data)
1943 {
1944         struct obd_device *obd = data;
1945         struct client_obd *cli = &obd->u.cli;
1946
1947         spin_lock(&cli->cl_loi_list_lock);
1948         seq_printf(m, "%d\n", cli->cl_max_pages_per_rpc);
1949         spin_unlock(&cli->cl_loi_list_lock);
1950         return 0;
1951 }
1952 EXPORT_SYMBOL(lprocfs_obd_max_pages_per_rpc_seq_show);
1953
1954 ssize_t lprocfs_obd_max_pages_per_rpc_seq_write(struct file *file,
1955                                                 const char __user *buffer,
1956                                                 size_t count, loff_t *off)
1957 {
1958         struct seq_file *m = file->private_data;
1959         struct obd_device *obd = m->private;
1960         struct client_obd *cli = &obd->u.cli;
1961         struct obd_import *imp;
1962         struct obd_connect_data *ocd;
1963         int chunk_mask, rc;
1964         char kernbuf[22];
1965         u64 val;
1966
1967         if (count > sizeof(kernbuf) - 1)
1968                 return -EINVAL;
1969
1970         if (copy_from_user(kernbuf, buffer, count))
1971                 return -EFAULT;
1972
1973         kernbuf[count] = '\0';
1974
1975         rc = sysfs_memparse(kernbuf, count, &val, "B");
1976         if (rc)
1977                 return rc;
1978
1979         /* if the max_pages is specified in bytes, convert to pages */
1980         if (val >= ONE_MB_BRW_SIZE)
1981                 val >>= PAGE_SHIFT;
1982
1983         with_imp_locked(obd, imp, rc) {
1984                 ocd = &imp->imp_connect_data;
1985                 chunk_mask = ~((1 << (cli->cl_chunkbits - PAGE_SHIFT)) - 1);
1986                 /* max_pages_per_rpc must be chunk aligned */
1987                 val = (val + ~chunk_mask) & chunk_mask;
1988                 if (val == 0 || (ocd->ocd_brw_size != 0 &&
1989                                  val > ocd->ocd_brw_size >> PAGE_SHIFT)) {
1990                         rc = -ERANGE;
1991                 } else {
1992                         spin_lock(&cli->cl_loi_list_lock);
1993                         cli->cl_max_pages_per_rpc = val;
1994                         client_adjust_max_dirty(cli);
1995                         spin_unlock(&cli->cl_loi_list_lock);
1996                 }
1997         }
1998
1999         return rc ?: count;
2000 }
2001 EXPORT_SYMBOL(lprocfs_obd_max_pages_per_rpc_seq_write);
2002
2003 ssize_t short_io_bytes_show(struct kobject *kobj, struct attribute *attr,
2004                             char *buf)
2005 {
2006         struct obd_device *obd = container_of(kobj, struct obd_device,
2007                                               obd_kset.kobj);
2008         struct client_obd *cli = &obd->u.cli;
2009         int rc;
2010
2011         spin_lock(&cli->cl_loi_list_lock);
2012         rc = sprintf(buf, "%d\n", cli->cl_max_short_io_bytes);
2013         spin_unlock(&cli->cl_loi_list_lock);
2014         return rc;
2015 }
2016 EXPORT_SYMBOL(short_io_bytes_show);
2017
2018 /* Used to catch people who think they're specifying pages. */
2019 #define MIN_SHORT_IO_BYTES 64U
2020
2021 ssize_t short_io_bytes_store(struct kobject *kobj, struct attribute *attr,
2022                              const char *buffer, size_t count)
2023 {
2024         struct obd_device *obd = container_of(kobj, struct obd_device,
2025                                               obd_kset.kobj);
2026         struct client_obd *cli = &obd->u.cli;
2027         u64 val;
2028         int rc;
2029
2030         if (strcmp(buffer, "-1") == 0) {
2031                 val = OBD_DEF_SHORT_IO_BYTES;
2032         } else {
2033                 rc = sysfs_memparse(buffer, count, &val, "B");
2034                 if (rc)
2035                         GOTO(out, rc);
2036         }
2037
2038         if (val && (val < MIN_SHORT_IO_BYTES || val > LNET_MTU))
2039                 GOTO(out, rc = -ERANGE);
2040
2041         rc = count;
2042
2043         spin_lock(&cli->cl_loi_list_lock);
2044         cli->cl_max_short_io_bytes = min_t(u64, val, OST_MAX_SHORT_IO_BYTES);
2045         spin_unlock(&cli->cl_loi_list_lock);
2046
2047 out:
2048         return rc;
2049 }
2050 EXPORT_SYMBOL(short_io_bytes_store);
2051
2052 int lprocfs_wr_root_squash(const char __user *buffer, unsigned long count,
2053                            struct root_squash_info *squash, char *name)
2054 {
2055         int rc;
2056         char kernbuf[64], *tmp, *errmsg;
2057         unsigned long uid, gid;
2058         ENTRY;
2059
2060         if (count >= sizeof(kernbuf)) {
2061                 errmsg = "string too long";
2062                 GOTO(failed_noprint, rc = -EINVAL);
2063         }
2064         if (copy_from_user(kernbuf, buffer, count)) {
2065                 errmsg = "bad address";
2066                 GOTO(failed_noprint, rc = -EFAULT);
2067         }
2068         kernbuf[count] = '\0';
2069
2070         /* look for uid gid separator */
2071         tmp = strchr(kernbuf, ':');
2072         if (!tmp) {
2073                 errmsg = "needs uid:gid format";
2074                 GOTO(failed, rc = -EINVAL);
2075         }
2076         *tmp = '\0';
2077         tmp++;
2078
2079         /* parse uid */
2080         if (kstrtoul(kernbuf, 0, &uid) != 0) {
2081                 errmsg = "bad uid";
2082                 GOTO(failed, rc = -EINVAL);
2083         }
2084
2085         /* parse gid */
2086         if (kstrtoul(tmp, 0, &gid) != 0) {
2087                 errmsg = "bad gid";
2088                 GOTO(failed, rc = -EINVAL);
2089         }
2090
2091         squash->rsi_uid = uid;
2092         squash->rsi_gid = gid;
2093
2094         LCONSOLE_INFO("%s: root_squash is set to %u:%u\n",
2095                       name, squash->rsi_uid, squash->rsi_gid);
2096         RETURN(count);
2097
2098 failed:
2099         if (tmp) {
2100                 tmp--;
2101                 *tmp = ':';
2102         }
2103         CWARN("%s: failed to set root_squash to \"%s\", %s, rc = %d\n",
2104               name, kernbuf, errmsg, rc);
2105         RETURN(rc);
2106 failed_noprint:
2107         CWARN("%s: failed to set root_squash due to %s, rc = %d\n",
2108               name, errmsg, rc);
2109         RETURN(rc);
2110 }
2111 EXPORT_SYMBOL(lprocfs_wr_root_squash);
2112
2113
2114 int lprocfs_wr_nosquash_nids(const char __user *buffer, unsigned long count,
2115                              struct root_squash_info *squash, char *name)
2116 {
2117         int rc;
2118         char *kernbuf = NULL;
2119         char *errmsg;
2120         LIST_HEAD(tmp);
2121         int len = count;
2122         ENTRY;
2123
2124         if (count > 4096) {
2125                 errmsg = "string too long";
2126                 GOTO(failed, rc = -EINVAL);
2127         }
2128
2129         OBD_ALLOC(kernbuf, count + 1);
2130         if (!kernbuf) {
2131                 errmsg = "no memory";
2132                 GOTO(failed, rc = -ENOMEM);
2133         }
2134         if (copy_from_user(kernbuf, buffer, count)) {
2135                 errmsg = "bad address";
2136                 GOTO(failed, rc = -EFAULT);
2137         }
2138         kernbuf[count] = '\0';
2139
2140         if (count > 0 && kernbuf[count - 1] == '\n')
2141                 len = count - 1;
2142
2143         if ((len == 4 && strncmp(kernbuf, "NONE", len) == 0) ||
2144             (len == 5 && strncmp(kernbuf, "clear", len) == 0)) {
2145                 /* empty string is special case */
2146                 spin_lock(&squash->rsi_lock);
2147                 if (!list_empty(&squash->rsi_nosquash_nids))
2148                         cfs_free_nidlist(&squash->rsi_nosquash_nids);
2149                 spin_unlock(&squash->rsi_lock);
2150                 LCONSOLE_INFO("%s: nosquash_nids is cleared\n", name);
2151                 OBD_FREE(kernbuf, count + 1);
2152                 RETURN(count);
2153         }
2154
2155         if (cfs_parse_nidlist(kernbuf, count, &tmp) <= 0) {
2156                 errmsg = "can't parse";
2157                 GOTO(failed, rc = -EINVAL);
2158         }
2159         LCONSOLE_INFO("%s: nosquash_nids set to %s\n",
2160                       name, kernbuf);
2161         OBD_FREE(kernbuf, count + 1);
2162         kernbuf = NULL;
2163
2164         spin_lock(&squash->rsi_lock);
2165         if (!list_empty(&squash->rsi_nosquash_nids))
2166                 cfs_free_nidlist(&squash->rsi_nosquash_nids);
2167         list_splice(&tmp, &squash->rsi_nosquash_nids);
2168         spin_unlock(&squash->rsi_lock);
2169
2170         RETURN(count);
2171
2172 failed:
2173         if (kernbuf) {
2174                 CWARN("%s: failed to set nosquash_nids to \"%s\", %s rc = %d\n",
2175                       name, kernbuf, errmsg, rc);
2176                 OBD_FREE(kernbuf, count + 1);
2177         } else {
2178                 CWARN("%s: failed to set nosquash_nids due to %s rc = %d\n",
2179                       name, errmsg, rc);
2180         }
2181         RETURN(rc);
2182 }
2183 EXPORT_SYMBOL(lprocfs_wr_nosquash_nids);
2184
2185 #endif /* CONFIG_PROC_FS*/