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