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