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