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