Whamcloud - gitweb
LU-10664 dom: non-blocking enqueue for DOM locks
[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         "lseek",                /* 0x40000 */
639         "dom_lvb",              /* 0x80000 */
640         NULL
641 };
642
643 void obd_connect_seq_flags2str(struct seq_file *m, __u64 flags, __u64 flags2,
644                                const char *sep)
645 {
646         bool first = true;
647         __u64 mask;
648         int i;
649
650         for (i = 0, mask = 1; i < 64; i++, mask <<= 1) {
651                 if (flags & mask) {
652                         seq_printf(m, "%s%s",
653                                    first ? "" : sep, obd_connect_names[i]);
654                         first = false;
655                 }
656         }
657
658         if (flags & ~(mask - 1)) {
659                 seq_printf(m, "%sunknown_%#llx",
660                            first ? "" : sep, flags & ~(mask - 1));
661                 first = false;
662         }
663
664         if (!(flags & OBD_CONNECT_FLAGS2) || flags2 == 0)
665                 return;
666
667         for (i = 64, mask = 1; obd_connect_names[i] != NULL; i++, mask <<= 1) {
668                 if (flags2 & mask) {
669                         seq_printf(m, "%s%s",
670                                    first ? "" : sep, obd_connect_names[i]);
671                         first = false;
672                 }
673         }
674
675         if (flags2 & ~(mask - 1)) {
676                 seq_printf(m, "%sunknown2_%#llx",
677                            first ? "" : sep, flags2 & ~(mask - 1));
678                 first = false;
679         }
680 }
681 EXPORT_SYMBOL(obd_connect_seq_flags2str);
682
683 int obd_connect_flags2str(char *page, int count, __u64 flags, __u64 flags2,
684                           const char *sep)
685 {
686         __u64 mask;
687         int i, ret = 0;
688
689         for (i = 0, mask = 1; i < 64; i++, mask <<= 1) {
690                 if (flags & mask)
691                         ret += snprintf(page + ret, count - ret, "%s%s",
692                                         ret ? sep : "", obd_connect_names[i]);
693         }
694
695         if (flags & ~(mask - 1))
696                 ret += snprintf(page + ret, count - ret,
697                                 "%sunknown_%#llx",
698                                 ret ? sep : "", flags & ~(mask - 1));
699
700         if (!(flags & OBD_CONNECT_FLAGS2) || flags2 == 0)
701                 return ret;
702
703         for (i = 64, mask = 1; obd_connect_names[i] != NULL; i++, mask <<= 1) {
704                 if (flags2 & mask)
705                         ret += snprintf(page + ret, count - ret, "%s%s",
706                                         ret ? sep : "", obd_connect_names[i]);
707         }
708
709         if (flags2 & ~(mask - 1))
710                 ret += snprintf(page + ret, count - ret,
711                                 "%sunknown2_%#llx",
712                                 ret ? sep : "", flags2 & ~(mask - 1));
713
714         return ret;
715 }
716 EXPORT_SYMBOL(obd_connect_flags2str);
717
718 void
719 obd_connect_data_seqprint(struct seq_file *m, struct obd_connect_data *ocd)
720 {
721         __u64 flags;
722
723         LASSERT(ocd != NULL);
724         flags = ocd->ocd_connect_flags;
725
726         seq_printf(m, "    connect_data:\n"
727                    "       flags: %#llx\n"
728                    "       instance: %u\n",
729                    ocd->ocd_connect_flags,
730                    ocd->ocd_instance);
731         if (flags & OBD_CONNECT_VERSION)
732                 seq_printf(m, "       target_version: %u.%u.%u.%u\n",
733                            OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
734                            OBD_OCD_VERSION_MINOR(ocd->ocd_version),
735                            OBD_OCD_VERSION_PATCH(ocd->ocd_version),
736                            OBD_OCD_VERSION_FIX(ocd->ocd_version));
737         if (flags & OBD_CONNECT_MDS)
738                 seq_printf(m, "       mdt_index: %d\n", ocd->ocd_group);
739         if (flags & OBD_CONNECT_GRANT)
740                 seq_printf(m, "       initial_grant: %d\n", ocd->ocd_grant);
741         if (flags & OBD_CONNECT_INDEX)
742                 seq_printf(m, "       target_index: %u\n", ocd->ocd_index);
743         if (flags & OBD_CONNECT_BRW_SIZE)
744                 seq_printf(m, "       max_brw_size: %d\n", ocd->ocd_brw_size);
745         if (flags & OBD_CONNECT_IBITS)
746                 seq_printf(m, "       ibits_known: %#llx\n",
747                            ocd->ocd_ibits_known);
748         if (flags & OBD_CONNECT_GRANT_PARAM)
749                 seq_printf(m, "       grant_block_size: %d\n"
750                            "       grant_inode_size: %d\n"
751                            "       grant_max_extent_size: %d\n"
752                            "       grant_extent_tax: %d\n",
753                            1 << ocd->ocd_grant_blkbits,
754                            1 << ocd->ocd_grant_inobits,
755                            ocd->ocd_grant_max_blks << ocd->ocd_grant_blkbits,
756                            ocd->ocd_grant_tax_kb << 10);
757         if (flags & OBD_CONNECT_TRANSNO)
758                 seq_printf(m, "       first_transno: %#llx\n",
759                            ocd->ocd_transno);
760         if (flags & OBD_CONNECT_CKSUM)
761                 seq_printf(m, "       cksum_types: %#x\n",
762                            ocd->ocd_cksum_types);
763         if (flags & OBD_CONNECT_MAX_EASIZE)
764                 seq_printf(m, "       max_easize: %d\n", ocd->ocd_max_easize);
765         if (flags & OBD_CONNECT_MAXBYTES)
766                 seq_printf(m, "       max_object_bytes: %llu\n",
767                            ocd->ocd_maxbytes);
768         if (flags & OBD_CONNECT_MULTIMODRPCS)
769                 seq_printf(m, "       max_mod_rpcs: %hu\n",
770                            ocd->ocd_maxmodrpcs);
771 }
772
773 static void lprocfs_import_seq_show_locked(struct seq_file *m,
774                                            struct obd_device *obd,
775                                            struct obd_import *imp)
776 {
777         char nidstr[LNET_NIDSTR_SIZE];
778         struct lprocfs_counter ret;
779         struct lprocfs_counter_header *header;
780         struct obd_import_conn *conn;
781         struct obd_connect_data *ocd;
782         int j;
783         int k;
784         int rw = 0;
785
786         ocd = &imp->imp_connect_data;
787
788         seq_printf(m, "import:\n"
789                    "    name: %s\n"
790                    "    target: %s\n"
791                    "    state: %s\n"
792                    "    connect_flags: [ ",
793                    obd->obd_name,
794                    obd2cli_tgt(obd),
795                    ptlrpc_import_state_name(imp->imp_state));
796         obd_connect_seq_flags2str(m, imp->imp_connect_data.ocd_connect_flags,
797                                   imp->imp_connect_data.ocd_connect_flags2,
798                                   ", ");
799         seq_printf(m, " ]\n");
800         obd_connect_data_seqprint(m, ocd);
801         seq_printf(m, "    import_flags: [ ");
802         obd_import_flags2str(imp, m);
803
804         seq_printf(m, " ]\n"
805                    "    connection:\n"
806                    "       failover_nids: [ ");
807         spin_lock(&imp->imp_lock);
808         j = 0;
809         list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
810                 libcfs_nid2str_r(conn->oic_conn->c_peer.nid,
811                                  nidstr, sizeof(nidstr));
812                 seq_printf(m, "%s%s", j ? ", " : "", nidstr);
813                 j++;
814         }
815         if (imp->imp_connection)
816                 libcfs_nid2str_r(imp->imp_connection->c_peer.nid,
817                                  nidstr, sizeof(nidstr));
818         else
819                 strncpy(nidstr, "<none>", sizeof(nidstr));
820         seq_printf(m, " ]\n"
821                    "       current_connection: %s\n"
822                    "       connection_attempts: %u\n"
823                    "       generation: %u\n"
824                    "       in-progress_invalidations: %u\n"
825                    "       idle: %lld sec\n",
826                    nidstr,
827                    imp->imp_conn_cnt,
828                    imp->imp_generation,
829                    atomic_read(&imp->imp_inval_count),
830                    ktime_get_real_seconds() - imp->imp_last_reply_time);
831         spin_unlock(&imp->imp_lock);
832
833         if (!obd->obd_svc_stats)
834                 return;
835
836         header = &obd->obd_svc_stats->ls_cnt_header[PTLRPC_REQWAIT_CNTR];
837         lprocfs_stats_collect(obd->obd_svc_stats, PTLRPC_REQWAIT_CNTR, &ret);
838         if (ret.lc_count != 0)
839                 ret.lc_sum = div64_s64(ret.lc_sum, ret.lc_count);
840         else
841                 ret.lc_sum = 0;
842         seq_printf(m, "    rpcs:\n"
843                    "       inflight: %u\n"
844                    "       unregistering: %u\n"
845                    "       timeouts: %u\n"
846                    "       avg_waittime: %llu %s\n",
847                    atomic_read(&imp->imp_inflight),
848                    atomic_read(&imp->imp_unregistering),
849                    atomic_read(&imp->imp_timeouts),
850                    ret.lc_sum, header->lc_units);
851
852         k = 0;
853         for(j = 0; j < IMP_AT_MAX_PORTALS; j++) {
854                 if (imp->imp_at.iat_portal[j] == 0)
855                         break;
856                 k = max_t(unsigned int, k,
857                           at_get(&imp->imp_at.iat_service_estimate[j]));
858         }
859         seq_printf(m, "    service_estimates:\n"
860                    "       services: %u sec\n"
861                    "       network: %d sec\n",
862                    k,
863                    at_get(&imp->imp_at.iat_net_latency));
864
865         seq_printf(m, "    transactions:\n"
866                    "       last_replay: %llu\n"
867                    "       peer_committed: %llu\n"
868                    "       last_checked: %llu\n",
869                    imp->imp_last_replay_transno,
870                    imp->imp_peer_committed_transno,
871                    imp->imp_last_transno_checked);
872
873         /* avg data rates */
874         for (rw = 0; rw <= 1; rw++) {
875                 lprocfs_stats_collect(obd->obd_svc_stats,
876                                       PTLRPC_LAST_CNTR + BRW_READ_BYTES + rw,
877                                       &ret);
878                 if (ret.lc_sum > 0 && ret.lc_count > 0) {
879                         ret.lc_sum = div64_s64(ret.lc_sum, ret.lc_count);
880                         seq_printf(m, "    %s_data_averages:\n"
881                                    "       bytes_per_rpc: %llu\n",
882                                    rw ? "write" : "read",
883                                    ret.lc_sum);
884                 }
885                 k = (int)ret.lc_sum;
886                 j = opcode_offset(OST_READ + rw) + EXTRA_MAX_OPCODES;
887                 header = &obd->obd_svc_stats->ls_cnt_header[j];
888                 lprocfs_stats_collect(obd->obd_svc_stats, j, &ret);
889                 if (ret.lc_sum > 0 && ret.lc_count != 0) {
890                         ret.lc_sum = div64_s64(ret.lc_sum, ret.lc_count);
891                         seq_printf(m, "       %s_per_rpc: %llu\n",
892                                    header->lc_units, ret.lc_sum);
893                         j = (int)ret.lc_sum;
894                         if (j > 0)
895                                 seq_printf(m, "       MB_per_sec: %u.%.02u\n",
896                                            k / j, (100 * k / j) % 100);
897                 }
898         }
899 }
900
901 int lprocfs_import_seq_show(struct seq_file *m, void *data)
902 {
903         struct obd_device *obd = (struct obd_device *)data;
904         struct obd_import *imp;
905         int rv;
906
907         LASSERT(obd != NULL);
908         with_imp_locked(obd, imp, rv)
909                 lprocfs_import_seq_show_locked(m, obd, imp);
910         return rv;
911 }
912 EXPORT_SYMBOL(lprocfs_import_seq_show);
913
914 int lprocfs_state_seq_show(struct seq_file *m, void *data)
915 {
916         struct obd_device *obd = (struct obd_device *)data;
917         struct obd_import *imp;
918         int j, k;
919         int rc;
920
921         LASSERT(obd != NULL);
922         with_imp_locked(obd, imp, rc) {
923                 seq_printf(m, "current_state: %s\n",
924                            ptlrpc_import_state_name(imp->imp_state));
925                 seq_printf(m, "state_history:\n");
926                 k = imp->imp_state_hist_idx;
927                 for (j = 0; j < IMP_STATE_HIST_LEN; j++) {
928                         struct import_state_hist *ish =
929                                 &imp->imp_state_hist[(k + j) % IMP_STATE_HIST_LEN];
930                         if (ish->ish_state == 0)
931                                 continue;
932                         seq_printf(m, " - [ %lld, %s ]\n", (s64)ish->ish_time,
933                                    ptlrpc_import_state_name(ish->ish_state));
934                 }
935         }
936
937         return rc;
938 }
939 EXPORT_SYMBOL(lprocfs_state_seq_show);
940
941 int lprocfs_at_hist_helper(struct seq_file *m, struct adaptive_timeout *at)
942 {
943         int i;
944         for (i = 0; i < AT_BINS; i++)
945                 seq_printf(m, "%3u ", at->at_hist[i]);
946         seq_printf(m, "\n");
947         return 0;
948 }
949 EXPORT_SYMBOL(lprocfs_at_hist_helper);
950
951 /* See also ptlrpc_lprocfs_timeouts_show_seq */
952 static void lprocfs_timeouts_seq_show_locked(struct seq_file *m,
953                                              struct obd_device *obd,
954                                              struct obd_import *imp)
955 {
956         timeout_t cur_timeout, worst_timeout;
957         time64_t now, worst_timestamp;
958         int i;
959
960         LASSERT(obd != NULL);
961
962         now = ktime_get_real_seconds();
963
964         /* Some network health info for kicks */
965         seq_printf(m, "%-10s : %lld, %llds ago\n",
966                    "last reply", (s64)imp->imp_last_reply_time,
967                    (s64)(now - imp->imp_last_reply_time));
968
969         cur_timeout = at_get(&imp->imp_at.iat_net_latency);
970         worst_timeout = imp->imp_at.iat_net_latency.at_worst_timeout_ever;
971         worst_timestamp = imp->imp_at.iat_net_latency.at_worst_timestamp;
972         seq_printf(m, "%-10s : cur %3u  worst %3u (at %lld, %llds ago) ",
973                    "network", cur_timeout, worst_timeout, worst_timestamp,
974                    now - worst_timestamp);
975         lprocfs_at_hist_helper(m, &imp->imp_at.iat_net_latency);
976
977         for(i = 0; i < IMP_AT_MAX_PORTALS; i++) {
978                 struct adaptive_timeout *service_est;
979
980                 if (imp->imp_at.iat_portal[i] == 0)
981                         break;
982
983                 service_est = &imp->imp_at.iat_service_estimate[i];
984                 cur_timeout = at_get(service_est);
985                 worst_timeout = service_est->at_worst_timeout_ever;
986                 worst_timestamp = service_est->at_worst_timestamp;
987                 seq_printf(m, "portal %-2d  : cur %3u  worst %3u (at %lld, %llds ago) ",
988                            imp->imp_at.iat_portal[i], cur_timeout,
989                            worst_timeout, worst_timestamp,
990                            now - worst_timestamp);
991                 lprocfs_at_hist_helper(m, service_est);
992         }
993 }
994
995 int lprocfs_timeouts_seq_show(struct seq_file *m, void *data)
996 {
997         struct obd_device *obd = (struct obd_device *)data;
998         struct obd_import *imp;
999         int rc;
1000
1001         with_imp_locked(obd, imp, rc)
1002                 lprocfs_timeouts_seq_show_locked(m, obd, imp);
1003         return rc;
1004 }
1005 EXPORT_SYMBOL(lprocfs_timeouts_seq_show);
1006
1007 int lprocfs_connect_flags_seq_show(struct seq_file *m, void *data)
1008 {
1009         struct obd_device *obd = data;
1010         __u64 flags;
1011         __u64 flags2;
1012         struct obd_import *imp;
1013         int rc;
1014
1015         with_imp_locked(obd, imp, rc) {
1016                 flags = imp->imp_connect_data.ocd_connect_flags;
1017                 flags2 = imp->imp_connect_data.ocd_connect_flags2;
1018                 seq_printf(m, "flags=%#llx\n", flags);
1019                 seq_printf(m, "flags2=%#llx\n", flags2);
1020                 obd_connect_seq_flags2str(m, flags, flags2, "\n");
1021                 seq_printf(m, "\n");
1022         }
1023
1024         return rc;
1025 }
1026 EXPORT_SYMBOL(lprocfs_connect_flags_seq_show);
1027
1028 static const struct attribute *obd_def_uuid_attrs[] = {
1029         &lustre_attr_uuid.attr,
1030         NULL,
1031 };
1032
1033 static const struct attribute *obd_def_attrs[] = {
1034         &lustre_attr_blocksize.attr,
1035         &lustre_attr_kbytestotal.attr,
1036         &lustre_attr_kbytesfree.attr,
1037         &lustre_attr_kbytesavail.attr,
1038         &lustre_attr_filestotal.attr,
1039         &lustre_attr_filesfree.attr,
1040         &lustre_attr_uuid.attr,
1041         NULL,
1042 };
1043
1044 static void obd_sysfs_release(struct kobject *kobj)
1045 {
1046         struct obd_device *obd = container_of(kobj, struct obd_device,
1047                                               obd_kset.kobj);
1048
1049         complete(&obd->obd_kobj_unregister);
1050 }
1051
1052 int lprocfs_obd_setup(struct obd_device *obd, bool uuid_only)
1053 {
1054         struct ldebugfs_vars *debugfs_vars = NULL;
1055         int rc;
1056
1057         if (!obd || obd->obd_magic != OBD_DEVICE_MAGIC)
1058                 return -ENODEV;
1059
1060         rc = kobject_set_name(&obd->obd_kset.kobj, "%s", obd->obd_name);
1061         if (rc)
1062                 return rc;
1063
1064         obd->obd_ktype.sysfs_ops = &lustre_sysfs_ops;
1065         obd->obd_ktype.release = obd_sysfs_release;
1066
1067         obd->obd_kset.kobj.parent = &obd->obd_type->typ_kobj;
1068         obd->obd_kset.kobj.ktype = &obd->obd_ktype;
1069         init_completion(&obd->obd_kobj_unregister);
1070         rc = kset_register(&obd->obd_kset);
1071         if (rc)
1072                 return rc;
1073
1074         if (uuid_only)
1075                 obd->obd_attrs = obd_def_uuid_attrs;
1076         else
1077                 obd->obd_attrs = obd_def_attrs;
1078
1079         rc = sysfs_create_files(&obd->obd_kset.kobj, obd->obd_attrs);
1080         if (rc) {
1081                 kset_unregister(&obd->obd_kset);
1082                 return rc;
1083         }
1084
1085         if (!obd->obd_type->typ_procroot)
1086                 debugfs_vars = obd->obd_debugfs_vars;
1087         obd->obd_debugfs_entry = debugfs_create_dir(
1088                 obd->obd_name, obd->obd_type->typ_debugfs_entry);
1089         ldebugfs_add_vars(obd->obd_debugfs_entry, debugfs_vars, obd);
1090
1091         if (obd->obd_proc_entry || !obd->obd_type->typ_procroot)
1092                 GOTO(already_registered, rc);
1093
1094         obd->obd_proc_entry = lprocfs_register(obd->obd_name,
1095                                                obd->obd_type->typ_procroot,
1096                                                obd->obd_vars, obd);
1097         if (IS_ERR(obd->obd_proc_entry)) {
1098                 rc = PTR_ERR(obd->obd_proc_entry);
1099                 CERROR("error %d setting up lprocfs for %s\n",rc,obd->obd_name);
1100                 obd->obd_proc_entry = NULL;
1101
1102                 debugfs_remove_recursive(obd->obd_debugfs_entry);
1103                 obd->obd_debugfs_entry = NULL;
1104
1105                 sysfs_remove_files(&obd->obd_kset.kobj, obd->obd_attrs);
1106                 obd->obd_attrs = NULL;
1107                 kset_unregister(&obd->obd_kset);
1108                 return rc;
1109         }
1110 already_registered:
1111         return rc;
1112 }
1113 EXPORT_SYMBOL(lprocfs_obd_setup);
1114
1115 int lprocfs_obd_cleanup(struct obd_device *obd)
1116 {
1117         if (!obd)
1118                 return -EINVAL;
1119
1120         if (obd->obd_proc_exports_entry) {
1121                 /* Should be no exports left */
1122                 lprocfs_remove(&obd->obd_proc_exports_entry);
1123                 obd->obd_proc_exports_entry = NULL;
1124         }
1125
1126         if (obd->obd_proc_entry) {
1127                 lprocfs_remove(&obd->obd_proc_entry);
1128                 obd->obd_proc_entry = NULL;
1129         }
1130
1131         debugfs_remove_recursive(obd->obd_debugfs_entry);
1132         obd->obd_debugfs_entry = NULL;
1133
1134         /* obd device never allocated a kset */
1135         if (!obd->obd_kset.kobj.state_initialized)
1136                 return 0;
1137
1138         if (obd->obd_attrs) {
1139                 sysfs_remove_files(&obd->obd_kset.kobj, obd->obd_attrs);
1140                 obd->obd_attrs = NULL;
1141         }
1142
1143         kset_unregister(&obd->obd_kset);
1144         wait_for_completion(&obd->obd_kobj_unregister);
1145         return 0;
1146 }
1147 EXPORT_SYMBOL(lprocfs_obd_cleanup);
1148
1149 int lprocfs_stats_alloc_one(struct lprocfs_stats *stats, unsigned int cpuid)
1150 {
1151         struct lprocfs_counter *cntr;
1152         unsigned int percpusize;
1153         int rc = -ENOMEM;
1154         unsigned long flags = 0;
1155         int i;
1156
1157         LASSERT(stats->ls_percpu[cpuid] == NULL);
1158         LASSERT((stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) == 0);
1159
1160         percpusize = lprocfs_stats_counter_size(stats);
1161         LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[cpuid], percpusize);
1162         if (stats->ls_percpu[cpuid]) {
1163                 rc = 0;
1164                 if (unlikely(stats->ls_biggest_alloc_num <= cpuid)) {
1165                         if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1166                                 spin_lock_irqsave(&stats->ls_lock, flags);
1167                         else
1168                                 spin_lock(&stats->ls_lock);
1169                         if (stats->ls_biggest_alloc_num <= cpuid)
1170                                 stats->ls_biggest_alloc_num = cpuid + 1;
1171                         if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) {
1172                                 spin_unlock_irqrestore(&stats->ls_lock, flags);
1173                         } else {
1174                                 spin_unlock(&stats->ls_lock);
1175                         }
1176                 }
1177                 /* initialize the ls_percpu[cpuid] non-zero counter */
1178                 for (i = 0; i < stats->ls_num; ++i) {
1179                         cntr = lprocfs_stats_counter_get(stats, cpuid, i);
1180                         cntr->lc_min = LC_MIN_INIT;
1181                 }
1182         }
1183         return rc;
1184 }
1185
1186 struct lprocfs_stats *lprocfs_alloc_stats(unsigned int num,
1187                                           enum lprocfs_stats_flags flags)
1188 {
1189         struct lprocfs_stats *stats;
1190         unsigned int num_entry;
1191         unsigned int percpusize = 0;
1192         int i;
1193
1194         if (num == 0)
1195                 return NULL;
1196
1197         if (lprocfs_no_percpu_stats != 0)
1198                 flags |= LPROCFS_STATS_FLAG_NOPERCPU;
1199
1200         if (flags & LPROCFS_STATS_FLAG_NOPERCPU)
1201                 num_entry = 1;
1202         else
1203                 num_entry = num_possible_cpus();
1204
1205         /* alloc percpu pointers for all possible cpu slots */
1206         LIBCFS_ALLOC(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1207         if (!stats)
1208                 return NULL;
1209
1210         stats->ls_num = num;
1211         stats->ls_flags = flags;
1212         spin_lock_init(&stats->ls_lock);
1213
1214         /* alloc num of counter headers */
1215         CFS_ALLOC_PTR_ARRAY(stats->ls_cnt_header, stats->ls_num);
1216         if (!stats->ls_cnt_header)
1217                 goto fail;
1218
1219         if ((flags & LPROCFS_STATS_FLAG_NOPERCPU) != 0) {
1220                 /* contains only one set counters */
1221                 percpusize = lprocfs_stats_counter_size(stats);
1222                 LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[0], percpusize);
1223                 if (!stats->ls_percpu[0])
1224                         goto fail;
1225                 stats->ls_biggest_alloc_num = 1;
1226         } else if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0) {
1227                 /* alloc all percpu data, currently only obd_memory use this */
1228                 for (i = 0; i < num_entry; ++i)
1229                         if (lprocfs_stats_alloc_one(stats, i) < 0)
1230                                 goto fail;
1231         }
1232
1233         return stats;
1234
1235 fail:
1236         lprocfs_free_stats(&stats);
1237         return NULL;
1238 }
1239 EXPORT_SYMBOL(lprocfs_alloc_stats);
1240
1241 void lprocfs_free_stats(struct lprocfs_stats **statsh)
1242 {
1243         struct lprocfs_stats *stats = *statsh;
1244         unsigned int num_entry;
1245         unsigned int percpusize;
1246         unsigned int i;
1247
1248         if (!stats || stats->ls_num == 0)
1249                 return;
1250         *statsh = NULL;
1251
1252         if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU)
1253                 num_entry = 1;
1254         else
1255                 num_entry = num_possible_cpus();
1256
1257         percpusize = lprocfs_stats_counter_size(stats);
1258         for (i = 0; i < num_entry; i++)
1259                 if (stats->ls_percpu[i])
1260                         LIBCFS_FREE(stats->ls_percpu[i], percpusize);
1261         if (stats->ls_cnt_header)
1262                 CFS_FREE_PTR_ARRAY(stats->ls_cnt_header, stats->ls_num);
1263         LIBCFS_FREE(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1264 }
1265 EXPORT_SYMBOL(lprocfs_free_stats);
1266
1267 u64 lprocfs_stats_collector(struct lprocfs_stats *stats, int idx,
1268                             enum lprocfs_fields_flags field)
1269 {
1270         unsigned long flags = 0;
1271         unsigned int num_cpu;
1272         unsigned int i;
1273         u64 ret = 0;
1274
1275         LASSERT(stats);
1276
1277         num_cpu = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1278         for (i = 0; i < num_cpu; i++) {
1279                 struct lprocfs_counter *cntr;
1280
1281                 if (!stats->ls_percpu[i])
1282                         continue;
1283
1284                 cntr = lprocfs_stats_counter_get(stats, i, idx);
1285                 ret += lprocfs_read_helper(cntr, &stats->ls_cnt_header[idx],
1286                                            stats->ls_flags, field);
1287         }
1288         lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1289         return ret;
1290 }
1291 EXPORT_SYMBOL(lprocfs_stats_collector);
1292
1293 void lprocfs_clear_stats(struct lprocfs_stats *stats)
1294 {
1295         struct lprocfs_counter *percpu_cntr;
1296         int i;
1297         int j;
1298         unsigned int num_entry;
1299         unsigned long flags = 0;
1300
1301         num_entry = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1302
1303         for (i = 0; i < num_entry; i++) {
1304                 if (!stats->ls_percpu[i])
1305                         continue;
1306                 for (j = 0; j < stats->ls_num; j++) {
1307                         percpu_cntr = lprocfs_stats_counter_get(stats, i, j);
1308                         percpu_cntr->lc_count           = 0;
1309                         percpu_cntr->lc_min             = LC_MIN_INIT;
1310                         percpu_cntr->lc_max             = 0;
1311                         percpu_cntr->lc_sumsquare       = 0;
1312                         percpu_cntr->lc_sum             = 0;
1313                         if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1314                                 percpu_cntr->lc_sum_irq = 0;
1315                 }
1316         }
1317
1318         lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1319 }
1320 EXPORT_SYMBOL(lprocfs_clear_stats);
1321
1322 static ssize_t lprocfs_stats_seq_write(struct file *file,
1323                                        const char __user *buf,
1324                                        size_t len, loff_t *off)
1325 {
1326         struct seq_file *seq = file->private_data;
1327         struct lprocfs_stats *stats = seq->private;
1328
1329         lprocfs_clear_stats(stats);
1330
1331         return len;
1332 }
1333
1334 static void *lprocfs_stats_seq_start(struct seq_file *p, loff_t *pos)
1335 {
1336         struct lprocfs_stats *stats = p->private;
1337
1338         return (*pos < stats->ls_num) ? pos : NULL;
1339 }
1340
1341 static void lprocfs_stats_seq_stop(struct seq_file *p, void *v)
1342 {
1343 }
1344
1345 static void *lprocfs_stats_seq_next(struct seq_file *p, void *v, loff_t *pos)
1346 {
1347         (*pos)++;
1348
1349         return lprocfs_stats_seq_start(p, pos);
1350 }
1351
1352 /* seq file export of one lprocfs counter */
1353 static int lprocfs_stats_seq_show(struct seq_file *p, void *v)
1354 {
1355         struct lprocfs_stats *stats = p->private;
1356         struct lprocfs_counter_header *hdr;
1357         struct lprocfs_counter ctr;
1358         int idx = *(loff_t *)v;
1359
1360         if (idx == 0) {
1361                 struct timespec64 now;
1362
1363                 ktime_get_real_ts64(&now);
1364                 seq_printf(p, "%-25s %llu.%09lu secs.nsecs\n",
1365                            "snapshot_time", (s64)now.tv_sec, now.tv_nsec);
1366         }
1367
1368         hdr = &stats->ls_cnt_header[idx];
1369         lprocfs_stats_collect(stats, idx, &ctr);
1370
1371         if (ctr.lc_count == 0)
1372                 return 0;
1373
1374         seq_printf(p, "%-25s %lld samples [%s]", hdr->lc_name,
1375                    ctr.lc_count, hdr->lc_units);
1376
1377         if ((hdr->lc_config & LPROCFS_CNTR_AVGMINMAX) && ctr.lc_count > 0) {
1378                 seq_printf(p, " %lld %lld %lld",
1379                            ctr.lc_min, ctr.lc_max, ctr.lc_sum);
1380                 if (hdr->lc_config & LPROCFS_CNTR_STDDEV)
1381                         seq_printf(p, " %llu", ctr.lc_sumsquare);
1382         }
1383         seq_putc(p, '\n');
1384         return 0;
1385 }
1386
1387 static const struct seq_operations lprocfs_stats_seq_sops = {
1388         .start  = lprocfs_stats_seq_start,
1389         .stop   = lprocfs_stats_seq_stop,
1390         .next   = lprocfs_stats_seq_next,
1391         .show   = lprocfs_stats_seq_show,
1392 };
1393
1394 static int lprocfs_stats_seq_open(struct inode *inode, struct file *file)
1395 {
1396         struct seq_file *seq;
1397         int rc;
1398
1399         rc = seq_open(file, &lprocfs_stats_seq_sops);
1400         if (rc)
1401                 return rc;
1402         seq = file->private_data;
1403         seq->private = inode->i_private ? inode->i_private : PDE_DATA(inode);
1404         return 0;
1405 }
1406
1407 const struct file_operations ldebugfs_stats_seq_fops = {
1408         .owner   = THIS_MODULE,
1409         .open    = lprocfs_stats_seq_open,
1410         .read    = seq_read,
1411         .write   = lprocfs_stats_seq_write,
1412         .llseek  = seq_lseek,
1413         .release = lprocfs_seq_release,
1414 };
1415 EXPORT_SYMBOL(ldebugfs_stats_seq_fops);
1416
1417 static const struct file_operations lprocfs_stats_seq_fops = {
1418         .owner   = THIS_MODULE,
1419         .open    = lprocfs_stats_seq_open,
1420         .read    = seq_read,
1421         .write   = lprocfs_stats_seq_write,
1422         .llseek  = seq_lseek,
1423         .release = lprocfs_seq_release,
1424 };
1425
1426 int lprocfs_register_stats(struct proc_dir_entry *root, const char *name,
1427                            struct lprocfs_stats *stats)
1428 {
1429         struct proc_dir_entry *entry;
1430         LASSERT(root != NULL);
1431
1432         entry = proc_create_data(name, 0644, root,
1433                                  &lprocfs_stats_seq_fops, stats);
1434         if (!entry)
1435                 return -ENOMEM;
1436         return 0;
1437 }
1438 EXPORT_SYMBOL(lprocfs_register_stats);
1439
1440 void lprocfs_counter_init(struct lprocfs_stats *stats, int index,
1441                           unsigned conf, const char *name, const char *units)
1442 {
1443         struct lprocfs_counter_header *header;
1444         struct lprocfs_counter *percpu_cntr;
1445         unsigned long flags = 0;
1446         unsigned int i;
1447         unsigned int num_cpu;
1448
1449         LASSERT(stats != NULL);
1450
1451         header = &stats->ls_cnt_header[index];
1452         LASSERTF(header != NULL, "Failed to allocate stats header:[%d]%s/%s\n",
1453                  index, name, units);
1454
1455         header->lc_config = conf;
1456         header->lc_name   = name;
1457         header->lc_units  = units;
1458
1459         num_cpu = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1460         for (i = 0; i < num_cpu; ++i) {
1461                 if (!stats->ls_percpu[i])
1462                         continue;
1463                 percpu_cntr = lprocfs_stats_counter_get(stats, i, index);
1464                 percpu_cntr->lc_count           = 0;
1465                 percpu_cntr->lc_min             = LC_MIN_INIT;
1466                 percpu_cntr->lc_max             = 0;
1467                 percpu_cntr->lc_sumsquare       = 0;
1468                 percpu_cntr->lc_sum             = 0;
1469                 if ((stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1470                         percpu_cntr->lc_sum_irq = 0;
1471         }
1472         lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1473 }
1474 EXPORT_SYMBOL(lprocfs_counter_init);
1475
1476 static const char * const mps_stats[] = {
1477         [LPROC_MD_CLOSE]                = "close",
1478         [LPROC_MD_CREATE]               = "create",
1479         [LPROC_MD_ENQUEUE]              = "enqueue",
1480         [LPROC_MD_GETATTR]              = "getattr",
1481         [LPROC_MD_INTENT_LOCK]          = "intent_lock",
1482         [LPROC_MD_LINK]                 = "link",
1483         [LPROC_MD_RENAME]               = "rename",
1484         [LPROC_MD_SETATTR]              = "setattr",
1485         [LPROC_MD_FSYNC]                = "fsync",
1486         [LPROC_MD_READ_PAGE]            = "read_page",
1487         [LPROC_MD_UNLINK]               = "unlink",
1488         [LPROC_MD_SETXATTR]             = "setxattr",
1489         [LPROC_MD_GETXATTR]             = "getxattr",
1490         [LPROC_MD_INTENT_GETATTR_ASYNC] = "intent_getattr_async",
1491         [LPROC_MD_REVALIDATE_LOCK]      = "revalidate_lock",
1492 };
1493
1494 int lprocfs_alloc_md_stats(struct obd_device *obd,
1495                            unsigned int num_private_stats)
1496 {
1497         struct lprocfs_stats *stats;
1498         unsigned int num_stats;
1499         int rc, i;
1500
1501         /*
1502          * TODO Ensure that this function is only used where
1503          * appropriate by adding an assertion to the effect that
1504          * obd->obd_type->typ_md_ops is not NULL. We can't do this now
1505          * because mdt_procfs_init() uses this function to allocate
1506          * the stats backing /proc/fs/lustre/mdt/.../md_stats but the
1507          * mdt layer does not use the md_ops interface. This is
1508          * confusing and a waste of memory. See LU-2484.
1509          */
1510         LASSERT(obd->obd_proc_entry != NULL);
1511         LASSERT(obd->obd_md_stats == NULL);
1512
1513         num_stats = ARRAY_SIZE(mps_stats) + num_private_stats;
1514         stats = lprocfs_alloc_stats(num_stats, 0);
1515         if (!stats)
1516                 return -ENOMEM;
1517
1518         for (i = 0; i < ARRAY_SIZE(mps_stats); i++) {
1519                 lprocfs_counter_init(stats, i, 0, mps_stats[i], "reqs");
1520                 if (!stats->ls_cnt_header[i].lc_name) {
1521                         CERROR("Missing md_stat initializer md_op operation at offset %d. Aborting.\n",
1522                                i);
1523                         LBUG();
1524                 }
1525         }
1526
1527         rc = lprocfs_register_stats(obd->obd_proc_entry, "md_stats", stats);
1528         if (rc < 0) {
1529                 lprocfs_free_stats(&stats);
1530         } else {
1531                 obd->obd_md_stats = stats;
1532         }
1533
1534         return rc;
1535 }
1536 EXPORT_SYMBOL(lprocfs_alloc_md_stats);
1537
1538 void lprocfs_free_md_stats(struct obd_device *obd)
1539 {
1540         struct lprocfs_stats *stats = obd->obd_md_stats;
1541
1542         if (stats) {
1543                 obd->obd_md_stats = NULL;
1544                 lprocfs_free_stats(&stats);
1545         }
1546 }
1547 EXPORT_SYMBOL(lprocfs_free_md_stats);
1548
1549 void lprocfs_init_ldlm_stats(struct lprocfs_stats *ldlm_stats)
1550 {
1551         lprocfs_counter_init(ldlm_stats,
1552                              LDLM_ENQUEUE - LDLM_FIRST_OPC,
1553                              0, "ldlm_enqueue", "reqs");
1554         lprocfs_counter_init(ldlm_stats,
1555                              LDLM_CONVERT - LDLM_FIRST_OPC,
1556                              0, "ldlm_convert", "reqs");
1557         lprocfs_counter_init(ldlm_stats,
1558                              LDLM_CANCEL - LDLM_FIRST_OPC,
1559                              0, "ldlm_cancel", "reqs");
1560         lprocfs_counter_init(ldlm_stats,
1561                              LDLM_BL_CALLBACK - LDLM_FIRST_OPC,
1562                              0, "ldlm_bl_callback", "reqs");
1563         lprocfs_counter_init(ldlm_stats,
1564                              LDLM_CP_CALLBACK - LDLM_FIRST_OPC,
1565                              0, "ldlm_cp_callback", "reqs");
1566         lprocfs_counter_init(ldlm_stats,
1567                              LDLM_GL_CALLBACK - LDLM_FIRST_OPC,
1568                              0, "ldlm_gl_callback", "reqs");
1569 }
1570 EXPORT_SYMBOL(lprocfs_init_ldlm_stats);
1571
1572 __s64 lprocfs_read_helper(struct lprocfs_counter *lc,
1573                           struct lprocfs_counter_header *header,
1574                           enum lprocfs_stats_flags flags,
1575                           enum lprocfs_fields_flags field)
1576 {
1577         __s64 ret = 0;
1578
1579         if (!lc || !header)
1580                 RETURN(0);
1581
1582         switch (field) {
1583                 case LPROCFS_FIELDS_FLAGS_CONFIG:
1584                         ret = header->lc_config;
1585                         break;
1586                 case LPROCFS_FIELDS_FLAGS_SUM:
1587                         ret = lc->lc_sum;
1588                         if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1589                                 ret += lc->lc_sum_irq;
1590                         break;
1591                 case LPROCFS_FIELDS_FLAGS_MIN:
1592                         ret = lc->lc_min;
1593                         break;
1594                 case LPROCFS_FIELDS_FLAGS_MAX:
1595                         ret = lc->lc_max;
1596                         break;
1597                 case LPROCFS_FIELDS_FLAGS_AVG:
1598                         ret = (lc->lc_max - lc->lc_min) / 2;
1599                         break;
1600                 case LPROCFS_FIELDS_FLAGS_SUMSQUARE:
1601                         ret = lc->lc_sumsquare;
1602                         break;
1603                 case LPROCFS_FIELDS_FLAGS_COUNT:
1604                         ret = lc->lc_count;
1605                         break;
1606                 default:
1607                         break;
1608         };
1609         RETURN(ret);
1610 }
1611 EXPORT_SYMBOL(lprocfs_read_helper);
1612
1613 /**
1614  * string_to_size - convert ASCII string representing a numerical
1615  *                  value with optional units to 64-bit binary value
1616  *
1617  * @size:       The numerical value extract out of @buffer
1618  * @buffer:     passed in string to parse
1619  * @count:      length of the @buffer
1620  *
1621  * This function returns a 64-bit binary value if @buffer contains a valid
1622  * numerical string. The string is parsed to 3 significant figures after
1623  * the decimal point. Support the string containing an optional units at
1624  * the end which can be base 2 or base 10 in value. If no units are given
1625  * the string is assumed to just a numerical value.
1626  *
1627  * Returns:     @count if the string is successfully parsed,
1628  *              -errno on invalid input strings. Error values:
1629  *
1630  *  - ``-EINVAL``: @buffer is not a proper numerical string
1631  *  - ``-EOVERFLOW``: results does not fit into 64 bits.
1632  *  - ``-E2BIG ``: @buffer is too large (not a valid number)
1633  */
1634 int string_to_size(u64 *size, const char *buffer, size_t count)
1635 {
1636         /* For string_get_size() it can support values above exabytes,
1637          * (ZiB, YiB) due to breaking the return value into a size and
1638          * bulk size to avoid 64 bit overflow. We don't break the size
1639          * up into block size units so we don't support ZiB or YiB.
1640          */
1641         static const char *const units_10[] = {
1642                 "kB", "MB", "GB", "TB", "PB", "EB",
1643         };
1644         static const char *const units_2[] = {
1645                 "K",  "M",  "G",  "T",  "P",  "E",
1646         };
1647         static const char *const *const units_str[] = {
1648                 [STRING_UNITS_2] = units_2,
1649                 [STRING_UNITS_10] = units_10,
1650         };
1651         static const unsigned int coeff[] = {
1652                 [STRING_UNITS_10] = 1000,
1653                 [STRING_UNITS_2] = 1024,
1654         };
1655         enum string_size_units unit = STRING_UNITS_2;
1656         u64 whole, blk_size = 1;
1657         char kernbuf[22], *end;
1658         size_t len = count;
1659         int rc;
1660         int i;
1661
1662         if (count >= sizeof(kernbuf)) {
1663                 CERROR("count %zd > buffer %zd\n", count, sizeof(kernbuf));
1664                 return -E2BIG;
1665         }
1666
1667         *size = 0;
1668         /* The "iB" suffix is optionally allowed for indicating base-2 numbers.
1669          * If suffix is only "B" and not "iB" then we treat it as base-10.
1670          */
1671         end = strstr(buffer, "B");
1672         if (end && *(end - 1) != 'i')
1673                 unit = STRING_UNITS_10;
1674
1675         i = unit == STRING_UNITS_2 ? ARRAY_SIZE(units_2) - 1 :
1676                                      ARRAY_SIZE(units_10) - 1;
1677         do {
1678                 end = strnstr(buffer, units_str[unit][i], count);
1679                 if (end) {
1680                         for (; i >= 0; i--)
1681                                 blk_size *= coeff[unit];
1682                         len = end - buffer;
1683                         break;
1684                 }
1685         } while (i--);
1686
1687         /* as 'B' is a substring of all units, we need to handle it
1688          * separately.
1689          */
1690         if (!end) {
1691                 /* 'B' is only acceptable letter at this point */
1692                 end = strnchr(buffer, count, 'B');
1693                 if (end) {
1694                         len = end - buffer;
1695
1696                         if (count - len > 2 ||
1697                             (count - len == 2 && strcmp(end, "B\n") != 0)) {
1698                                 CDEBUG(D_INFO, "unknown suffix '%s'\n", buffer);
1699                                 return -EINVAL;
1700                         }
1701                 }
1702                 /* kstrtoull will error out if it has non digits */
1703                 goto numbers_only;
1704         }
1705
1706         end = strnchr(buffer, count, '.');
1707         if (end) {
1708                 /* need to limit 3 decimal places */
1709                 char rem[4] = "000";
1710                 u64 frac = 0;
1711                 size_t off;
1712
1713                 len = end - buffer;
1714                 end++;
1715
1716                 /* limit to 3 decimal points */
1717                 off = min_t(size_t, 3, strspn(end, "0123456789"));
1718                 /* need to limit frac_d to a u32 */
1719                 memcpy(rem, end, off);
1720                 rc = kstrtoull(rem, 10, &frac);
1721                 if (rc)
1722                         return rc;
1723
1724                 if (fls64(frac) + fls64(blk_size) - 1 > 64)
1725                         return -EOVERFLOW;
1726
1727                 frac *= blk_size;
1728                 do_div(frac, 1000);
1729                 *size += frac;
1730         }
1731 numbers_only:
1732         snprintf(kernbuf, sizeof(kernbuf), "%.*s", (int)len, buffer);
1733         rc = kstrtoull(kernbuf, 10, &whole);
1734         if (rc)
1735                 return rc;
1736
1737         if (whole != 0 && fls64(whole) + fls64(blk_size) - 1 > 64)
1738                 return -EOVERFLOW;
1739
1740         *size += whole * blk_size;
1741
1742         return count;
1743 }
1744 EXPORT_SYMBOL(string_to_size);
1745
1746 /**
1747  * sysfs_memparse - parse a ASCII string to 64-bit binary value,
1748  *                  with optional units
1749  *
1750  * @buffer:     kernel pointer to input string
1751  * @count:      number of bytes in the input @buffer
1752  * @val:        (output) binary value returned to caller
1753  * @defunit:    default unit suffix to use if none is provided
1754  *
1755  * Parses a string into a number. The number stored at @buffer is
1756  * potentially suffixed with K, M, G, T, P, E. Besides these other
1757  * valid suffix units are shown in the string_to_size() function.
1758  * If the string lacks a suffix then the defunit is used. The defunit
1759  * should be given as a binary unit (e.g. MiB) as that is the standard
1760  * for tunables in Lustre. If no unit suffix is given (e.g. 'G'), then
1761  * it is assumed to be in binary units.
1762  *
1763  * Returns:     0 on success or -errno on failure.
1764  */
1765 int sysfs_memparse(const char *buffer, size_t count, u64 *val,
1766                    const char *defunit)
1767 {
1768         const char *param = buffer;
1769         char tmp_buf[23];
1770         int rc;
1771
1772         count = strlen(buffer);
1773         while (count > 0 && isspace(buffer[count - 1]))
1774                 count--;
1775
1776         if (!count)
1777                 RETURN(-EINVAL);
1778
1779         /* If there isn't already a unit on this value, append @defunit.
1780          * Units of 'B' don't affect the value, so don't bother adding.
1781          */
1782         if (!isalpha(buffer[count - 1]) && defunit[0] != 'B') {
1783                 if (count + 3 >= sizeof(tmp_buf)) {
1784                         CERROR("count %zd > size %zd\n", count, sizeof(param));
1785                         RETURN(-E2BIG);
1786                 }
1787
1788                 scnprintf(tmp_buf, sizeof(tmp_buf), "%.*s%s", (int)count,
1789                           buffer, defunit);
1790                 param = tmp_buf;
1791                 count = strlen(param);
1792         }
1793
1794         rc = string_to_size(val, param, count);
1795
1796         return rc < 0 ? rc : 0;
1797 }
1798 EXPORT_SYMBOL(sysfs_memparse);
1799
1800 char *lprocfs_strnstr(const char *s1, const char *s2, size_t len)
1801 {
1802         size_t l2;
1803
1804         l2 = strlen(s2);
1805         if (!l2)
1806                 return (char *)s1;
1807         while (len >= l2) {
1808                 len--;
1809                 if (!memcmp(s1, s2, l2))
1810                         return (char *)s1;
1811                 s1++;
1812         }
1813         return NULL;
1814 }
1815 EXPORT_SYMBOL(lprocfs_strnstr);
1816
1817 /**
1818  * Find the string \a name in the input \a buffer, and return a pointer to the
1819  * value immediately following \a name, reducing \a count appropriately.
1820  * If \a name is not found the original \a buffer is returned.
1821  */
1822 char *lprocfs_find_named_value(const char *buffer, const char *name,
1823                                 size_t *count)
1824 {
1825         char *val;
1826         size_t buflen = *count;
1827
1828         /* there is no strnstr() in rhel5 and ubuntu kernels */
1829         val = lprocfs_strnstr(buffer, name, buflen);
1830         if (!val)
1831                 return (char *)buffer;
1832
1833         val += strlen(name);                             /* skip prefix */
1834         while (val < buffer + buflen && isspace(*val)) /* skip separator */
1835                 val++;
1836
1837         *count = 0;
1838         while (val < buffer + buflen && isalnum(*val)) {
1839                 ++*count;
1840                 ++val;
1841         }
1842
1843         return val - *count;
1844 }
1845 EXPORT_SYMBOL(lprocfs_find_named_value);
1846
1847 int lprocfs_seq_create(struct proc_dir_entry *parent,
1848                        const char *name,
1849                        mode_t mode,
1850                        const struct file_operations *seq_fops,
1851                        void *data)
1852 {
1853         struct proc_dir_entry *entry;
1854         ENTRY;
1855
1856         /* Disallow secretly (un)writable entries. */
1857         LASSERT((seq_fops->write == NULL) == ((mode & 0222) == 0));
1858
1859         entry = proc_create_data(name, mode, parent, seq_fops, data);
1860
1861         if (!entry)
1862                 RETURN(-ENOMEM);
1863
1864         RETURN(0);
1865 }
1866 EXPORT_SYMBOL(lprocfs_seq_create);
1867
1868 int lprocfs_obd_seq_create(struct obd_device *obd,
1869                            const char *name,
1870                            mode_t mode,
1871                            const struct file_operations *seq_fops,
1872                            void *data)
1873 {
1874         return lprocfs_seq_create(obd->obd_proc_entry, name,
1875                                   mode, seq_fops, data);
1876 }
1877 EXPORT_SYMBOL(lprocfs_obd_seq_create);
1878
1879 void lprocfs_oh_tally(struct obd_histogram *oh, unsigned int value)
1880 {
1881         if (value >= OBD_HIST_MAX)
1882                 value = OBD_HIST_MAX - 1;
1883
1884         spin_lock(&oh->oh_lock);
1885         oh->oh_buckets[value]++;
1886         spin_unlock(&oh->oh_lock);
1887 }
1888 EXPORT_SYMBOL(lprocfs_oh_tally);
1889
1890 void lprocfs_oh_tally_log2(struct obd_histogram *oh, unsigned int value)
1891 {
1892         unsigned int val = 0;
1893
1894         if (likely(value != 0))
1895                 val = min(fls(value - 1), OBD_HIST_MAX);
1896
1897         lprocfs_oh_tally(oh, val);
1898 }
1899 EXPORT_SYMBOL(lprocfs_oh_tally_log2);
1900
1901 unsigned long lprocfs_oh_sum(struct obd_histogram *oh)
1902 {
1903         unsigned long ret = 0;
1904         int i;
1905
1906         for (i = 0; i < OBD_HIST_MAX; i++)
1907                 ret +=  oh->oh_buckets[i];
1908         return ret;
1909 }
1910 EXPORT_SYMBOL(lprocfs_oh_sum);
1911
1912 void lprocfs_oh_clear(struct obd_histogram *oh)
1913 {
1914         spin_lock(&oh->oh_lock);
1915         memset(oh->oh_buckets, 0, sizeof(oh->oh_buckets));
1916         spin_unlock(&oh->oh_lock);
1917 }
1918 EXPORT_SYMBOL(lprocfs_oh_clear);
1919
1920 ssize_t lustre_attr_show(struct kobject *kobj,
1921                          struct attribute *attr, char *buf)
1922 {
1923         struct lustre_attr *a = container_of(attr, struct lustre_attr, attr);
1924
1925         return a->show ? a->show(kobj, attr, buf) : 0;
1926 }
1927 EXPORT_SYMBOL_GPL(lustre_attr_show);
1928
1929 ssize_t lustre_attr_store(struct kobject *kobj, struct attribute *attr,
1930                           const char *buf, size_t len)
1931 {
1932         struct lustre_attr *a = container_of(attr, struct lustre_attr, attr);
1933
1934         return a->store ? a->store(kobj, attr, buf, len) : len;
1935 }
1936 EXPORT_SYMBOL_GPL(lustre_attr_store);
1937
1938 const struct sysfs_ops lustre_sysfs_ops = {
1939         .show  = lustre_attr_show,
1940         .store = lustre_attr_store,
1941 };
1942 EXPORT_SYMBOL_GPL(lustre_sysfs_ops);
1943
1944 int lprocfs_obd_max_pages_per_rpc_seq_show(struct seq_file *m, void *data)
1945 {
1946         struct obd_device *obd = data;
1947         struct client_obd *cli = &obd->u.cli;
1948
1949         spin_lock(&cli->cl_loi_list_lock);
1950         seq_printf(m, "%d\n", cli->cl_max_pages_per_rpc);
1951         spin_unlock(&cli->cl_loi_list_lock);
1952         return 0;
1953 }
1954 EXPORT_SYMBOL(lprocfs_obd_max_pages_per_rpc_seq_show);
1955
1956 ssize_t lprocfs_obd_max_pages_per_rpc_seq_write(struct file *file,
1957                                                 const char __user *buffer,
1958                                                 size_t count, loff_t *off)
1959 {
1960         struct seq_file *m = file->private_data;
1961         struct obd_device *obd = m->private;
1962         struct client_obd *cli = &obd->u.cli;
1963         struct obd_import *imp;
1964         struct obd_connect_data *ocd;
1965         int chunk_mask, rc;
1966         char kernbuf[22];
1967         u64 val;
1968
1969         if (count > sizeof(kernbuf) - 1)
1970                 return -EINVAL;
1971
1972         if (copy_from_user(kernbuf, buffer, count))
1973                 return -EFAULT;
1974
1975         kernbuf[count] = '\0';
1976
1977         rc = sysfs_memparse(kernbuf, count, &val, "B");
1978         if (rc)
1979                 return rc;
1980
1981         /* if the max_pages is specified in bytes, convert to pages */
1982         if (val >= ONE_MB_BRW_SIZE)
1983                 val >>= PAGE_SHIFT;
1984
1985         with_imp_locked(obd, imp, rc) {
1986                 ocd = &imp->imp_connect_data;
1987                 chunk_mask = ~((1 << (cli->cl_chunkbits - PAGE_SHIFT)) - 1);
1988                 /* max_pages_per_rpc must be chunk aligned */
1989                 val = (val + ~chunk_mask) & chunk_mask;
1990                 if (val == 0 || (ocd->ocd_brw_size != 0 &&
1991                                  val > ocd->ocd_brw_size >> PAGE_SHIFT)) {
1992                         rc = -ERANGE;
1993                 } else {
1994                         spin_lock(&cli->cl_loi_list_lock);
1995                         cli->cl_max_pages_per_rpc = val;
1996                         client_adjust_max_dirty(cli);
1997                         spin_unlock(&cli->cl_loi_list_lock);
1998                 }
1999         }
2000
2001         return rc ?: count;
2002 }
2003 EXPORT_SYMBOL(lprocfs_obd_max_pages_per_rpc_seq_write);
2004
2005 ssize_t short_io_bytes_show(struct kobject *kobj, struct attribute *attr,
2006                             char *buf)
2007 {
2008         struct obd_device *obd = container_of(kobj, struct obd_device,
2009                                               obd_kset.kobj);
2010         struct client_obd *cli = &obd->u.cli;
2011         int rc;
2012
2013         spin_lock(&cli->cl_loi_list_lock);
2014         rc = sprintf(buf, "%d\n", cli->cl_max_short_io_bytes);
2015         spin_unlock(&cli->cl_loi_list_lock);
2016         return rc;
2017 }
2018 EXPORT_SYMBOL(short_io_bytes_show);
2019
2020 /* Used to catch people who think they're specifying pages. */
2021 #define MIN_SHORT_IO_BYTES 64U
2022
2023 ssize_t short_io_bytes_store(struct kobject *kobj, struct attribute *attr,
2024                              const char *buffer, size_t count)
2025 {
2026         struct obd_device *obd = container_of(kobj, struct obd_device,
2027                                               obd_kset.kobj);
2028         struct client_obd *cli = &obd->u.cli;
2029         u64 val;
2030         int rc;
2031
2032         if (strcmp(buffer, "-1") == 0) {
2033                 val = OBD_DEF_SHORT_IO_BYTES;
2034         } else {
2035                 rc = sysfs_memparse(buffer, count, &val, "B");
2036                 if (rc)
2037                         GOTO(out, rc);
2038         }
2039
2040         if (val && (val < MIN_SHORT_IO_BYTES || val > LNET_MTU))
2041                 GOTO(out, rc = -ERANGE);
2042
2043         rc = count;
2044
2045         spin_lock(&cli->cl_loi_list_lock);
2046         cli->cl_max_short_io_bytes = min_t(u64, val, OST_MAX_SHORT_IO_BYTES);
2047         spin_unlock(&cli->cl_loi_list_lock);
2048
2049 out:
2050         return rc;
2051 }
2052 EXPORT_SYMBOL(short_io_bytes_store);
2053
2054 int lprocfs_wr_root_squash(const char __user *buffer, unsigned long count,
2055                            struct root_squash_info *squash, char *name)
2056 {
2057         int rc;
2058         char kernbuf[64], *tmp, *errmsg;
2059         unsigned long uid, gid;
2060         ENTRY;
2061
2062         if (count >= sizeof(kernbuf)) {
2063                 errmsg = "string too long";
2064                 GOTO(failed_noprint, rc = -EINVAL);
2065         }
2066         if (copy_from_user(kernbuf, buffer, count)) {
2067                 errmsg = "bad address";
2068                 GOTO(failed_noprint, rc = -EFAULT);
2069         }
2070         kernbuf[count] = '\0';
2071
2072         /* look for uid gid separator */
2073         tmp = strchr(kernbuf, ':');
2074         if (!tmp) {
2075                 errmsg = "needs uid:gid format";
2076                 GOTO(failed, rc = -EINVAL);
2077         }
2078         *tmp = '\0';
2079         tmp++;
2080
2081         /* parse uid */
2082         if (kstrtoul(kernbuf, 0, &uid) != 0) {
2083                 errmsg = "bad uid";
2084                 GOTO(failed, rc = -EINVAL);
2085         }
2086
2087         /* parse gid */
2088         if (kstrtoul(tmp, 0, &gid) != 0) {
2089                 errmsg = "bad gid";
2090                 GOTO(failed, rc = -EINVAL);
2091         }
2092
2093         squash->rsi_uid = uid;
2094         squash->rsi_gid = gid;
2095
2096         LCONSOLE_INFO("%s: root_squash is set to %u:%u\n",
2097                       name, squash->rsi_uid, squash->rsi_gid);
2098         RETURN(count);
2099
2100 failed:
2101         if (tmp) {
2102                 tmp--;
2103                 *tmp = ':';
2104         }
2105         CWARN("%s: failed to set root_squash to \"%s\", %s, rc = %d\n",
2106               name, kernbuf, errmsg, rc);
2107         RETURN(rc);
2108 failed_noprint:
2109         CWARN("%s: failed to set root_squash due to %s, rc = %d\n",
2110               name, errmsg, rc);
2111         RETURN(rc);
2112 }
2113 EXPORT_SYMBOL(lprocfs_wr_root_squash);
2114
2115
2116 int lprocfs_wr_nosquash_nids(const char __user *buffer, unsigned long count,
2117                              struct root_squash_info *squash, char *name)
2118 {
2119         int rc;
2120         char *kernbuf = NULL;
2121         char *errmsg;
2122         LIST_HEAD(tmp);
2123         int len = count;
2124         ENTRY;
2125
2126         if (count > 4096) {
2127                 errmsg = "string too long";
2128                 GOTO(failed, rc = -EINVAL);
2129         }
2130
2131         OBD_ALLOC(kernbuf, count + 1);
2132         if (!kernbuf) {
2133                 errmsg = "no memory";
2134                 GOTO(failed, rc = -ENOMEM);
2135         }
2136         if (copy_from_user(kernbuf, buffer, count)) {
2137                 errmsg = "bad address";
2138                 GOTO(failed, rc = -EFAULT);
2139         }
2140         kernbuf[count] = '\0';
2141
2142         if (count > 0 && kernbuf[count - 1] == '\n')
2143                 len = count - 1;
2144
2145         if ((len == 4 && strncmp(kernbuf, "NONE", len) == 0) ||
2146             (len == 5 && strncmp(kernbuf, "clear", len) == 0)) {
2147                 /* empty string is special case */
2148                 spin_lock(&squash->rsi_lock);
2149                 if (!list_empty(&squash->rsi_nosquash_nids))
2150                         cfs_free_nidlist(&squash->rsi_nosquash_nids);
2151                 spin_unlock(&squash->rsi_lock);
2152                 LCONSOLE_INFO("%s: nosquash_nids is cleared\n", name);
2153                 OBD_FREE(kernbuf, count + 1);
2154                 RETURN(count);
2155         }
2156
2157         if (cfs_parse_nidlist(kernbuf, count, &tmp) <= 0) {
2158                 errmsg = "can't parse";
2159                 GOTO(failed, rc = -EINVAL);
2160         }
2161         LCONSOLE_INFO("%s: nosquash_nids set to %s\n",
2162                       name, kernbuf);
2163         OBD_FREE(kernbuf, count + 1);
2164         kernbuf = NULL;
2165
2166         spin_lock(&squash->rsi_lock);
2167         if (!list_empty(&squash->rsi_nosquash_nids))
2168                 cfs_free_nidlist(&squash->rsi_nosquash_nids);
2169         list_splice(&tmp, &squash->rsi_nosquash_nids);
2170         spin_unlock(&squash->rsi_lock);
2171
2172         RETURN(count);
2173
2174 failed:
2175         if (kernbuf) {
2176                 CWARN("%s: failed to set nosquash_nids to \"%s\", %s rc = %d\n",
2177                       name, kernbuf, errmsg, rc);
2178                 OBD_FREE(kernbuf, count + 1);
2179         } else {
2180                 CWARN("%s: failed to set nosquash_nids due to %s rc = %d\n",
2181                       name, errmsg, rc);
2182         }
2183         RETURN(rc);
2184 }
2185 EXPORT_SYMBOL(lprocfs_wr_nosquash_nids);
2186
2187 #endif /* CONFIG_PROC_FS*/