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