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