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