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