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