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