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