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