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