Whamcloud - gitweb
LU-4843 mdt: Add OBD_CONNECT_DIR_STRIPE flag
[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, 2013, 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 #ifndef __KERNEL__
44 #include <liblustre.h>
45 #endif
46
47 #include <obd_class.h>
48 #include <lprocfs_status.h>
49 #include <lustre/lustre_idl.h>
50
51 #if defined(LPROCFS)
52
53 static int lprocfs_no_percpu_stats = 0;
54 CFS_MODULE_PARM(lprocfs_no_percpu_stats, "i", int, 0644,
55                 "Do not alloc percpu data for lprocfs stats");
56
57 #define MAX_STRING_SIZE 128
58
59 int lprocfs_single_release(struct inode *inode, struct file *file)
60 {
61         return single_release(inode, file);
62 }
63 EXPORT_SYMBOL(lprocfs_single_release);
64
65 int lprocfs_seq_release(struct inode *inode, struct file *file)
66 {
67         return seq_release(inode, file);
68 }
69 EXPORT_SYMBOL(lprocfs_seq_release);
70
71 struct proc_dir_entry *
72 lprocfs_add_simple(struct proc_dir_entry *root, char *name,
73 #ifndef HAVE_ONLY_PROCFS_SEQ
74                    read_proc_t *read_proc, write_proc_t *write_proc,
75 #endif
76                    void *data, const struct file_operations *fops)
77 {
78         struct proc_dir_entry *proc;
79         mode_t mode = 0;
80
81         if (root == NULL || name == NULL)
82                 return ERR_PTR(-EINVAL);
83
84         if (!fops) {
85 #ifndef HAVE_ONLY_PROCFS_SEQ
86                 if (read_proc)
87                         mode = 0444;
88                 if (write_proc)
89                         mode |= 0200;
90
91                 LPROCFS_WRITE_ENTRY();
92                 proc = create_proc_entry(name, mode, root);
93                 if (!proc) {
94                         CERROR("LprocFS: No memory to create /proc entry %s", name);
95                         LPROCFS_WRITE_EXIT();
96                         return ERR_PTR(-ENOMEM);
97                 }
98                 proc->read_proc = read_proc;
99                 proc->write_proc = write_proc;
100                 proc->data = data;
101                 LPROCFS_WRITE_EXIT();
102 #else
103                 return ERR_PTR(-EINVAL);
104 #endif
105         } else {
106                 if (fops->read)
107                         mode = 0444;
108                 if (fops->write)
109                         mode |= 0200;
110                 proc = proc_create_data(name, mode, root, fops, data);
111                 if (!proc) {
112                         CERROR("LprocFS: No memory to create /proc entry %s",
113                                name);
114                         return ERR_PTR(-ENOMEM);
115                 }
116         }
117         return proc;
118 }
119 EXPORT_SYMBOL(lprocfs_add_simple);
120
121 struct proc_dir_entry *lprocfs_add_symlink(const char *name,
122                         struct proc_dir_entry *parent, const char *format, ...)
123 {
124         struct proc_dir_entry *entry;
125         char *dest;
126         va_list ap;
127
128         if (parent == NULL || format == NULL)
129                 return NULL;
130
131         OBD_ALLOC_WAIT(dest, MAX_STRING_SIZE + 1);
132         if (dest == NULL)
133                 return NULL;
134
135         va_start(ap, format);
136         vsnprintf(dest, MAX_STRING_SIZE, format, ap);
137         va_end(ap);
138
139         entry = proc_symlink(name, parent, dest);
140         if (entry == NULL)
141                 CERROR("LprocFS: Could not create symbolic link from %s to %s",
142                         name, dest);
143
144         OBD_FREE(dest, MAX_STRING_SIZE + 1);
145         return entry;
146 }
147 EXPORT_SYMBOL(lprocfs_add_symlink);
148
149 #ifdef HAVE_ONLY_PROCFS_SEQ
150 static const struct file_operations lprocfs_generic_fops = { };
151 #else
152
153 ssize_t
154 lprocfs_fops_read(struct file *f, char __user *buf, size_t size, loff_t *ppos)
155 {
156         struct proc_dir_entry *dp = PDE(f->f_dentry->d_inode);
157         char *page, *start = NULL;
158         int rc = 0, eof = 1, count;
159
160         if (*ppos >= PAGE_CACHE_SIZE)
161                 return 0;
162
163         page = (char *)__get_free_page(GFP_KERNEL);
164         if (page == NULL)
165                 return -ENOMEM;
166
167         if (LPROCFS_ENTRY_CHECK(dp)) {
168                 rc = -ENOENT;
169                 goto out;
170         }
171
172         OBD_FAIL_TIMEOUT(OBD_FAIL_LPROC_REMOVE, 10);
173         if (dp->read_proc)
174                 rc = dp->read_proc(page, &start, *ppos, PAGE_CACHE_SIZE,
175                                    &eof, dp->data);
176         if (rc <= 0)
177                 goto out;
178
179         /* for lustre proc read, the read count must be less than PAGE_SIZE */
180         LASSERT(eof == 1);
181
182         if (start == NULL) {
183                 rc -= *ppos;
184                 if (rc < 0)
185                         rc = 0;
186                 if (rc == 0)
187                         goto out;
188                 start = page + *ppos;
189         } else if (start < page) {
190                 start = page;
191         }
192
193         count = (rc < size) ? rc : size;
194         if (copy_to_user(buf, start, count)) {
195                 rc = -EFAULT;
196                 goto out;
197         }
198         *ppos += count;
199
200 out:
201         free_page((unsigned long)page);
202         return rc;
203 }
204
205 ssize_t
206 lprocfs_fops_write(struct file *f, const char __user *buf, size_t size,
207                    loff_t *ppos)
208 {
209         struct proc_dir_entry *dp = PDE(f->f_dentry->d_inode);
210         int rc = -EIO;
211
212         if (LPROCFS_ENTRY_CHECK(dp))
213                 return -ENOENT;
214         if (dp->write_proc)
215                 rc = dp->write_proc(f, buf, size, dp->data);
216         return rc;
217 }
218
219 static struct file_operations lprocfs_generic_fops = {
220         .owner = THIS_MODULE,
221         .read = lprocfs_fops_read,
222         .write = lprocfs_fops_write,
223 };
224
225 /* for b=10866, global variable */
226 DECLARE_RWSEM(_lprocfs_lock);
227 EXPORT_SYMBOL(_lprocfs_lock);
228
229 static struct proc_dir_entry *__lprocfs_srch(struct proc_dir_entry *head,
230                                              const char *name)
231 {
232         struct proc_dir_entry *temp;
233
234         if (head == NULL)
235                 return NULL;
236
237         temp = head->subdir;
238         while (temp != NULL) {
239                 if (strcmp(temp->name, name) == 0)
240                         return temp;
241                 temp = temp->next;
242         }
243         return NULL;
244 }
245
246 struct proc_dir_entry *lprocfs_srch(struct proc_dir_entry *head,
247                                     const char *name)
248 {
249         struct proc_dir_entry *temp;
250
251         LPROCFS_SRCH_ENTRY();
252         temp = __lprocfs_srch(head, name);
253         LPROCFS_SRCH_EXIT();
254         return temp;
255 }
256 EXPORT_SYMBOL(lprocfs_srch);
257
258 static int __lprocfs_add_vars(struct proc_dir_entry *root,
259                               struct lprocfs_vars *list,
260                               void *data)
261 {
262         int rc = 0;
263
264         if (root == NULL || list == NULL)
265                 return -EINVAL;
266
267         while (list->name != NULL) {
268                 struct proc_dir_entry *cur_root, *proc;
269                 char *pathcopy, *cur, *next, pathbuf[64];
270                 int pathsize = strlen(list->name) + 1;
271
272                 proc = NULL;
273                 cur_root = root;
274
275                 /* need copy of path for strsep */
276                 if (strlen(list->name) > sizeof(pathbuf) - 1) {
277                         OBD_ALLOC(pathcopy, pathsize);
278                         if (pathcopy == NULL)
279                                 GOTO(out, rc = -ENOMEM);
280                 } else {
281                         pathcopy = pathbuf;
282                 }
283
284                 next = pathcopy;
285                 strcpy(pathcopy, list->name);
286
287                 while (cur_root != NULL && (cur = strsep(&next, "/"))) {
288                         if (*cur =='\0') /* skip double/trailing "/" */
289                                 continue;
290
291                         proc = __lprocfs_srch(cur_root, cur);
292                         CDEBUG(D_OTHER, "cur_root=%s, cur=%s, next=%s, (%s)\n",
293                                cur_root->name, cur, next,
294                                (proc ? "exists" : "new"));
295                         if (next != NULL) {
296                                 cur_root = (proc ? proc :
297                                             proc_mkdir(cur, cur_root));
298                         } else if (proc == NULL) {
299                                 mode_t mode = 0;
300                                 if (list->proc_mode != 0000) {
301                                         mode = list->proc_mode;
302                                 } else {
303                                         if (list->read_fptr)
304                                                 mode = 0444;
305                                         if (list->write_fptr)
306                                                 mode |= 0200;
307                                 }
308                                 proc = create_proc_entry(cur, mode, cur_root);
309                         }
310                 }
311
312                 if (pathcopy != pathbuf)
313                         OBD_FREE(pathcopy, pathsize);
314
315                 if (cur_root == NULL || proc == NULL) {
316                         CERROR("LprocFS: No memory to create /proc entry %s",
317                                list->name);
318                         GOTO(out, rc = -ENOMEM);
319                 }
320
321                 if (list->fops)
322                         proc->proc_fops = list->fops;
323                 else
324                         proc->proc_fops = &lprocfs_generic_fops;
325                 proc->read_proc = list->read_fptr;
326                 proc->write_proc = list->write_fptr;
327                 proc->data = (list->data ? list->data : data);
328                 list++;
329         }
330 out:
331         return rc;
332 }
333
334 int lprocfs_add_vars(struct proc_dir_entry *root, struct lprocfs_vars *list,
335                      void *data)
336 {
337         int rc = 0;
338
339         LPROCFS_WRITE_ENTRY();
340         rc = __lprocfs_add_vars(root, list, data);
341         LPROCFS_WRITE_EXIT();
342
343         return rc;
344 }
345 EXPORT_SYMBOL(lprocfs_add_vars);
346 #endif
347
348 /**
349  * Add /proc entries.
350  *
351  * \param root [in]  The parent proc entry on which new entry will be added.
352  * \param list [in]  Array of proc entries to be added.
353  * \param data [in]  The argument to be passed when entries read/write routines
354  *                   are called through /proc file.
355  *
356  * \retval 0   on success
357  *         < 0 on error
358  */
359 int
360 lprocfs_seq_add_vars(struct proc_dir_entry *root, struct lprocfs_seq_vars *list,
361                      void *data)
362 {
363         if (root == NULL || list == NULL)
364                 return -EINVAL;
365
366         while (list->name != NULL) {
367                 struct proc_dir_entry *proc;
368                 mode_t mode = 0;
369
370                 if (list->proc_mode != 0000) {
371                         mode = list->proc_mode;
372                 } else if (list->fops) {
373                         if (list->fops->read)
374                                 mode = 0444;
375                         if (list->fops->write)
376                                 mode |= 0200;
377                 }
378                 proc = proc_create_data(list->name, mode, root,
379                                         list->fops ?: &lprocfs_generic_fops,
380                                         list->data ?: data);
381                 if (proc == NULL)
382                         return -ENOMEM;
383                 list++;
384         }
385         return 0;
386 }
387 EXPORT_SYMBOL(lprocfs_seq_add_vars);
388
389 #ifndef HAVE_ONLY_PROCFS_SEQ
390 void lprocfs_remove_nolock(struct proc_dir_entry **proot)
391 {
392         struct proc_dir_entry *root = *proot;
393         struct proc_dir_entry *temp = root;
394         struct proc_dir_entry *rm_entry;
395         struct proc_dir_entry *parent;
396
397         *proot = NULL;
398         if (root == NULL || IS_ERR(root))
399                 return;
400
401         parent = root->parent;
402         LASSERT(parent != NULL);
403
404         while (1) {
405                 while (temp->subdir != NULL)
406                         temp = temp->subdir;
407
408                 rm_entry = temp;
409                 temp = temp->parent;
410
411                 /* Memory corruption once caused this to fail, and
412                    without this LASSERT we would loop here forever. */
413                 LASSERTF(strlen(rm_entry->name) == rm_entry->namelen,
414                          "0x%p  %s/%s len %d\n", rm_entry, temp->name,
415                          rm_entry->name, (int)strlen(rm_entry->name));
416
417                 remove_proc_entry(rm_entry->name, temp);
418                 if (temp == parent)
419                         break;
420         }
421 }
422 #endif
423
424 void lprocfs_remove(struct proc_dir_entry **rooth)
425 {
426 #ifndef HAVE_ONLY_PROCFS_SEQ
427         LPROCFS_WRITE_ENTRY(); /* search vs remove race */
428         lprocfs_remove_nolock(rooth);
429         LPROCFS_WRITE_EXIT();
430 #else
431         proc_remove(*rooth);
432         *rooth = NULL;
433 #endif
434 }
435 EXPORT_SYMBOL(lprocfs_remove);
436
437 void lprocfs_remove_proc_entry(const char *name, struct proc_dir_entry *parent)
438 {
439         LASSERT(parent != NULL);
440         remove_proc_entry(name, parent);
441 }
442 EXPORT_SYMBOL(lprocfs_remove_proc_entry);
443
444 #ifndef HAVE_ONLY_PROCFS_SEQ
445 void lprocfs_try_remove_proc_entry(const char *name,
446                                    struct proc_dir_entry *parent)
447 {
448         struct proc_dir_entry    *t = NULL;
449         struct proc_dir_entry   **p;
450         int                       len, busy = 0;
451
452         LASSERT(parent != NULL);
453         len = strlen(name);
454
455         LPROCFS_WRITE_ENTRY();
456
457         /* lookup target name */
458         for (p = &parent->subdir; *p; p = &(*p)->next) {
459                 if ((*p)->namelen != len)
460                         continue;
461                 if (memcmp(name, (*p)->name, len))
462                         continue;
463                 t = *p;
464                 break;
465         }
466
467         if (t) {
468                 /* verify it's empty: do not count "num_refs" */
469                 for (p = &t->subdir; *p; p = &(*p)->next) {
470                         if ((*p)->namelen != strlen("num_refs")) {
471                                 busy = 1;
472                                 break;
473                         }
474                         if (memcmp("num_refs", (*p)->name,
475                                    strlen("num_refs"))) {
476                                 busy = 1;
477                                 break;
478                         }
479                 }
480         }
481
482         if (busy == 0)
483                 lprocfs_remove_nolock(&t);
484
485         LPROCFS_WRITE_EXIT();
486
487         return;
488 }
489 EXPORT_SYMBOL(lprocfs_try_remove_proc_entry);
490
491 struct proc_dir_entry *lprocfs_register(const char *name,
492                                         struct proc_dir_entry *parent,
493                                         struct lprocfs_vars *list, void *data)
494 {
495         struct proc_dir_entry *entry;
496         int rc;
497
498         LPROCFS_WRITE_ENTRY();
499         entry = __lprocfs_srch(parent, name);
500         if (entry != NULL) {
501                 CERROR("entry '%s' already registered\n", name);
502                 GOTO(out, entry = ERR_PTR(-EALREADY));
503         }
504
505         entry = proc_mkdir(name, parent);
506         if (entry == NULL)
507                 GOTO(out, entry = ERR_PTR(-ENOMEM));
508
509         if (list != NULL) {
510                 rc = __lprocfs_add_vars(entry, list, data);
511                 if (rc != 0) {
512                         lprocfs_remove_nolock(&entry);
513                         GOTO(out, entry = ERR_PTR(rc));
514                 }
515         }
516 out:
517         LPROCFS_WRITE_EXIT();
518         return entry;
519 }
520 EXPORT_SYMBOL(lprocfs_register);
521 #endif
522
523 struct proc_dir_entry *
524 lprocfs_seq_register(const char *name, struct proc_dir_entry *parent,
525                      struct lprocfs_seq_vars *list, void *data)
526 {
527         struct proc_dir_entry *newchild;
528
529         newchild = proc_mkdir(name, parent);
530         if (newchild != NULL && list != NULL) {
531                 int rc = lprocfs_seq_add_vars(newchild, list, data);
532                 if (rc) {
533                         lprocfs_remove(&newchild);
534                         return ERR_PTR(rc);
535                 }
536         }
537         return newchild;
538 }
539 EXPORT_SYMBOL(lprocfs_seq_register);
540
541 /* Generic callbacks */
542 int lprocfs_uint_seq_show(struct seq_file *m, void *data)
543 {
544         return seq_printf(m, "%u\n", *(unsigned int *)data);
545 }
546 EXPORT_SYMBOL(lprocfs_uint_seq_show);
547
548 int lprocfs_wr_uint(struct file *file, const char *buffer,
549                     unsigned long count, void *data)
550 {
551         unsigned *p = data;
552         char dummy[MAX_STRING_SIZE + 1], *end;
553         unsigned long tmp;
554
555         dummy[MAX_STRING_SIZE] = '\0';
556         if (copy_from_user(dummy, buffer, MAX_STRING_SIZE))
557                 return -EFAULT;
558
559         tmp = simple_strtoul(dummy, &end, 0);
560         if (dummy == end)
561                 return -EINVAL;
562
563         *p = (unsigned int)tmp;
564         return count;
565 }
566 EXPORT_SYMBOL(lprocfs_wr_uint);
567
568 ssize_t lprocfs_uint_seq_write(struct file *file, const char *buffer,
569                                size_t count, loff_t *off)
570 {
571         int *data = ((struct seq_file *)file->private_data)->private;
572         int val = 0, rc;
573
574         rc = lprocfs_write_helper(buffer, count, &val);
575         if (rc < 0)
576                 return rc;
577
578         return lprocfs_wr_uint(file, buffer, count, data);
579 }
580 EXPORT_SYMBOL(lprocfs_uint_seq_write);
581
582 int lprocfs_u64_seq_show(struct seq_file *m, void *data)
583 {
584         LASSERT(data != NULL);
585         return seq_printf(m, LPU64"\n", *(__u64 *)data);
586 }
587 EXPORT_SYMBOL(lprocfs_u64_seq_show);
588
589 int lprocfs_atomic_seq_show(struct seq_file *m, void *data)
590 {
591         atomic_t *atom = data;
592         LASSERT(atom != NULL);
593         return seq_printf(m, "%d\n", atomic_read(atom));
594 }
595 EXPORT_SYMBOL(lprocfs_atomic_seq_show);
596
597 ssize_t
598 lprocfs_atomic_seq_write(struct file *file, const char *buffer,
599                         size_t count, loff_t *off)
600 {
601         atomic_t *atm = ((struct seq_file *)file->private_data)->private;
602         int val = 0;
603         int rc;
604
605         rc = lprocfs_write_helper(buffer, count, &val);
606         if (rc < 0)
607                 return rc;
608
609         if (val <= 0)
610                 return -ERANGE;
611
612         atomic_set(atm, val);
613         return count;
614 }
615 EXPORT_SYMBOL(lprocfs_atomic_seq_write);
616
617 int lprocfs_uuid_seq_show(struct seq_file *m, void *data)
618 {
619         struct obd_device *obd = data;
620
621         LASSERT(obd != NULL);
622         return seq_printf(m, "%s\n", obd->obd_uuid.uuid);
623 }
624 EXPORT_SYMBOL(lprocfs_uuid_seq_show);
625
626 int lprocfs_name_seq_show(struct seq_file *m, void *data)
627 {
628         struct obd_device *dev = data;
629
630         LASSERT(dev != NULL);
631         return seq_printf(m, "%s\n", dev->obd_name);
632 }
633 EXPORT_SYMBOL(lprocfs_name_seq_show);
634
635 int lprocfs_blksize_seq_show(struct seq_file *m, void *data)
636 {
637         struct obd_device *obd = data;
638         struct obd_statfs  osfs;
639         int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
640                             cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
641                             OBD_STATFS_NODELAY);
642         if (!rc)
643                 rc = seq_printf(m, "%u\n", osfs.os_bsize);
644         return rc;
645 }
646 EXPORT_SYMBOL(lprocfs_blksize_seq_show);
647
648 int lprocfs_kbytestotal_seq_show(struct seq_file *m, void *data)
649 {
650         struct obd_device *obd = data;
651         struct obd_statfs  osfs;
652         int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
653                             cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
654                             OBD_STATFS_NODELAY);
655         if (!rc) {
656                 __u32 blk_size = osfs.os_bsize >> 10;
657                 __u64 result = osfs.os_blocks;
658
659                 while (blk_size >>= 1)
660                         result <<= 1;
661
662                 rc = seq_printf(m, LPU64"\n", result);
663         }
664         return rc;
665 }
666 EXPORT_SYMBOL(lprocfs_kbytestotal_seq_show);
667
668 int lprocfs_kbytesfree_seq_show(struct seq_file *m, void *data)
669 {
670         struct obd_device *obd = data;
671         struct obd_statfs  osfs;
672         int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
673                             cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
674                             OBD_STATFS_NODELAY);
675         if (!rc) {
676                 __u32 blk_size = osfs.os_bsize >> 10;
677                 __u64 result = osfs.os_bfree;
678
679                 while (blk_size >>= 1)
680                         result <<= 1;
681
682                 rc = seq_printf(m, LPU64"\n", result);
683         }
684         return rc;
685 }
686 EXPORT_SYMBOL(lprocfs_kbytesfree_seq_show);
687
688 int lprocfs_kbytesavail_seq_show(struct seq_file *m, void *data)
689 {
690         struct obd_device *obd = data;
691         struct obd_statfs  osfs;
692         int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
693                             cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
694                             OBD_STATFS_NODELAY);
695         if (!rc) {
696                 __u32 blk_size = osfs.os_bsize >> 10;
697                 __u64 result = osfs.os_bavail;
698
699                 while (blk_size >>= 1)
700                         result <<= 1;
701
702                 rc = seq_printf(m, LPU64"\n", result);
703         }
704         return rc;
705 }
706 EXPORT_SYMBOL(lprocfs_kbytesavail_seq_show);
707
708 int lprocfs_filestotal_seq_show(struct seq_file *m, void *data)
709 {
710         struct obd_device *obd = data;
711         struct obd_statfs  osfs;
712         int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
713                             cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
714                             OBD_STATFS_NODELAY);
715         if (!rc)
716                 rc = seq_printf(m, LPU64"\n", osfs.os_files);
717         return rc;
718 }
719 EXPORT_SYMBOL(lprocfs_filestotal_seq_show);
720
721 int lprocfs_filesfree_seq_show(struct seq_file *m, void *data)
722 {
723         struct obd_device *obd = data;
724         struct obd_statfs  osfs;
725         int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
726                             cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
727                             OBD_STATFS_NODELAY);
728         if (!rc)
729                 rc = seq_printf(m, LPU64"\n", osfs.os_ffree);
730         return rc;
731 }
732 EXPORT_SYMBOL(lprocfs_filesfree_seq_show);
733
734 int lprocfs_server_uuid_seq_show(struct seq_file *m, void *data)
735 {
736         struct obd_device *obd = data;
737         struct obd_import *imp;
738         char *imp_state_name = NULL;
739         int rc = 0;
740
741         LASSERT(obd != NULL);
742         LPROCFS_CLIMP_CHECK(obd);
743         imp = obd->u.cli.cl_import;
744         imp_state_name = ptlrpc_import_state_name(imp->imp_state);
745         rc = seq_printf(m, "%s\t%s%s\n", obd2cli_tgt(obd), imp_state_name,
746                         imp->imp_deactive ? "\tDEACTIVATED" : "");
747
748         LPROCFS_CLIMP_EXIT(obd);
749         return rc;
750 }
751 EXPORT_SYMBOL(lprocfs_server_uuid_seq_show);
752
753 int lprocfs_conn_uuid_seq_show(struct seq_file *m, void *data)
754 {
755         struct obd_device *obd = data;
756         struct ptlrpc_connection *conn;
757         int rc = 0;
758
759         LASSERT(obd != NULL);
760
761         LPROCFS_CLIMP_CHECK(obd);
762         conn = obd->u.cli.cl_import->imp_connection;
763         if (conn && obd->u.cli.cl_import)
764                 rc = seq_printf(m, "%s\n", conn->c_remote_uuid.uuid);
765         else
766                 rc = seq_printf(m, "%s\n", "<none>");
767
768         LPROCFS_CLIMP_EXIT(obd);
769         return rc;
770 }
771 EXPORT_SYMBOL(lprocfs_conn_uuid_seq_show);
772
773 /** add up per-cpu counters */
774 void lprocfs_stats_collect(struct lprocfs_stats *stats, int idx,
775                            struct lprocfs_counter *cnt)
776 {
777         unsigned int                    num_entry;
778         struct lprocfs_counter          *percpu_cntr;
779         int                             i;
780         unsigned long                   flags = 0;
781
782         memset(cnt, 0, sizeof(*cnt));
783
784         if (stats == NULL) {
785                 /* set count to 1 to avoid divide-by-zero errs in callers */
786                 cnt->lc_count = 1;
787                 return;
788         }
789
790         cnt->lc_min = LC_MIN_INIT;
791
792         num_entry = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
793
794         for (i = 0; i < num_entry; i++) {
795                 if (stats->ls_percpu[i] == NULL)
796                         continue;
797                 percpu_cntr = lprocfs_stats_counter_get(stats, i, idx);
798
799                 cnt->lc_count += percpu_cntr->lc_count;
800                 cnt->lc_sum += percpu_cntr->lc_sum;
801                 if (percpu_cntr->lc_min < cnt->lc_min)
802                         cnt->lc_min = percpu_cntr->lc_min;
803                 if (percpu_cntr->lc_max > cnt->lc_max)
804                         cnt->lc_max = percpu_cntr->lc_max;
805                 cnt->lc_sumsquare += percpu_cntr->lc_sumsquare;
806         }
807
808         lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
809 }
810 EXPORT_SYMBOL(lprocfs_stats_collect);
811
812 /**
813  * Append a space separated list of current set flags to str.
814  */
815 #define flag2seqstr(flag)                                               \
816         do {                                                            \
817                 if (imp->imp_##flag) {                                  \
818                         seq_printf(m, "%s" #flag, first ? "" : ", ");   \
819                         first = false;                                  \
820                 }                                                       \
821         } while (0)
822 static void obd_import_flags2seqstr(struct obd_import *imp, struct seq_file *m)
823 {
824         bool first = true;
825
826         if (imp->imp_obd->obd_no_recov) {
827                 seq_printf(m, "no_recov");
828                 first = false;
829         }
830
831         flag2seqstr(invalid);
832         flag2seqstr(deactive);
833         flag2seqstr(replayable);
834         flag2seqstr(delayed_recovery);
835         flag2seqstr(no_lock_replay);
836         flag2seqstr(vbr_failed);
837         flag2seqstr(pingable);
838         flag2seqstr(resend_replay);
839         flag2seqstr(no_pinger_recover);
840         flag2seqstr(need_mne_swab);
841         flag2seqstr(connect_tried);
842 }
843 #undef flag2seqstr
844
845 static const char *obd_connect_names[] = {
846         "read_only",
847         "lov_index",
848         "connect_from_mds",
849         "write_grant",
850         "server_lock",
851         "version",
852         "request_portal",
853         "acl",
854         "xattr",
855         "create_on_write",
856         "truncate_lock",
857         "initial_transno",
858         "inode_bit_locks",
859         "join_file(obsolete)",
860         "getattr_by_fid",
861         "no_oh_for_devices",
862         "remote_client",
863         "remote_client_by_force",
864         "max_byte_per_rpc",
865         "64bit_qdata",
866         "mds_capability",
867         "oss_capability",
868         "early_lock_cancel",
869         "som",
870         "adaptive_timeouts",
871         "lru_resize",
872         "mds_mds_connection",
873         "real_conn",
874         "change_qunit_size",
875         "alt_checksum_algorithm",
876         "fid_is_enabled",
877         "version_recovery",
878         "pools",
879         "grant_shrink",
880         "skip_orphan",
881         "large_ea",
882         "full20",
883         "layout_lock",
884         "64bithash",
885         "object_max_bytes",
886         "imp_recov",
887         "jobstats",
888         "umask",
889         "einprogress",
890         "grant_param",
891         "flock_owner",
892         "lvb_type",
893         "nanoseconds_times",
894         "lightweight_conn",
895         "short_io",
896         "pingless",
897         "flock_deadlock",
898         "disp_stripe",
899         "open_by_fid",
900         "lfsck",
901         "unknown",
902         "unlink_close",
903         "unknown",
904         "dir_stripe",
905         "unknown",
906         NULL
907 };
908
909 static void obd_connect_seq_flags2str(struct seq_file *m, __u64 flags, char *sep)
910 {
911         bool first = true;
912         __u64 mask = 1;
913         int i;
914
915         for (i = 0; obd_connect_names[i] != NULL; i++, mask <<= 1) {
916                 if (flags & mask) {
917                         seq_printf(m, "%s%s",
918                                    first ? "" : sep, obd_connect_names[i]);
919                         first = false;
920                 }
921         }
922         if (flags & ~(mask - 1))
923                 seq_printf(m, "%sunknown_"LPX64,
924                            first ? "" : sep, flags & ~(mask - 1));
925 }
926
927 int obd_connect_flags2str(char *page, int count, __u64 flags, char *sep)
928 {
929         __u64 mask = 1;
930         int i, ret = 0;
931
932         for (i = 0; obd_connect_names[i] != NULL; i++, mask <<= 1) {
933                 if (flags & mask)
934                         ret += snprintf(page + ret, count - ret, "%s%s",
935                                         ret ? sep : "", obd_connect_names[i]);
936         }
937         if (flags & ~(mask - 1))
938                 ret += snprintf(page + ret, count - ret,
939                                 "%sunknown_"LPX64,
940                                 ret ? sep : "", flags & ~(mask - 1));
941         return ret;
942 }
943 EXPORT_SYMBOL(obd_connect_flags2str);
944
945 static void obd_connect_data_seqprint(struct seq_file *m,
946                                       struct obd_connect_data *ocd)
947 {
948         int flags;
949
950         LASSERT(ocd != NULL);
951         flags = ocd->ocd_connect_flags;
952
953         seq_printf(m, "    connect_data:\n"
954                       "       flags: "LPX64"\n"
955                       "       instance: %u\n",
956                       ocd->ocd_connect_flags,
957                       ocd->ocd_instance);
958         if (flags & OBD_CONNECT_VERSION)
959                 seq_printf(m, "       target_version: %u.%u.%u.%u\n",
960                               OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
961                               OBD_OCD_VERSION_MINOR(ocd->ocd_version),
962                               OBD_OCD_VERSION_PATCH(ocd->ocd_version),
963                               OBD_OCD_VERSION_FIX(ocd->ocd_version));
964         if (flags & OBD_CONNECT_MDS)
965                 seq_printf(m, "       mdt_index: %d\n", ocd->ocd_group);
966         if (flags & OBD_CONNECT_GRANT)
967                 seq_printf(m, "       initial_grant: %d\n", ocd->ocd_grant);
968         if (flags & OBD_CONNECT_INDEX)
969                 seq_printf(m, "       target_index: %u\n", ocd->ocd_index);
970         if (flags & OBD_CONNECT_BRW_SIZE)
971                 seq_printf(m, "       max_brw_size: %d\n", ocd->ocd_brw_size);
972         if (flags & OBD_CONNECT_IBITS)
973                 seq_printf(m, "       ibits_known: "LPX64"\n",
974                                 ocd->ocd_ibits_known);
975         if (flags & OBD_CONNECT_GRANT_PARAM)
976                 seq_printf(m, "       grant_block_size: %d\n"
977                               "       grant_inode_size: %d\n"
978                               "       grant_extent_overhead: %d\n",
979                               ocd->ocd_blocksize,
980                               ocd->ocd_inodespace,
981                               ocd->ocd_grant_extent);
982         if (flags & OBD_CONNECT_TRANSNO)
983                 seq_printf(m, "       first_transno: "LPX64"\n",
984                                 ocd->ocd_transno);
985         if (flags & OBD_CONNECT_CKSUM)
986                 seq_printf(m, "       cksum_types: %#x\n",
987                               ocd->ocd_cksum_types);
988         if (flags & OBD_CONNECT_MAX_EASIZE)
989                 seq_printf(m, "       max_easize: %d\n", ocd->ocd_max_easize);
990         if (flags & OBD_CONNECT_MAXBYTES)
991                 seq_printf(m, "       max_object_bytes: "LPU64"\n",
992                               ocd->ocd_maxbytes);
993 }
994
995 int lprocfs_import_seq_show(struct seq_file *m, void *data)
996 {
997         struct lprocfs_counter          ret;
998         struct lprocfs_counter_header   *header;
999         struct obd_device               *obd    = (struct obd_device *)data;
1000         struct obd_import               *imp;
1001         struct obd_import_conn          *conn;
1002         struct obd_connect_data         *ocd;
1003         int                             j;
1004         int                             k;
1005         int                             rw      = 0;
1006
1007         LASSERT(obd != NULL);
1008         LPROCFS_CLIMP_CHECK(obd);
1009         imp = obd->u.cli.cl_import;
1010         ocd = &imp->imp_connect_data;
1011
1012         seq_printf(m, "import:\n"
1013                       "    name: %s\n"
1014                       "    target: %s\n"
1015                       "    state: %s\n"
1016                       "    connect_flags: [ ",
1017                       obd->obd_name,
1018                       obd2cli_tgt(obd),
1019                       ptlrpc_import_state_name(imp->imp_state));
1020         obd_connect_seq_flags2str(m, imp->imp_connect_data.ocd_connect_flags,
1021                                         ", ");
1022         seq_printf(m, " ]\n");
1023         obd_connect_data_seqprint(m, ocd);
1024         seq_printf(m, "    import_flags: [ ");
1025         obd_import_flags2seqstr(imp, m);
1026
1027         seq_printf(m, " ]\n"
1028                       "    connection:\n"
1029                       "       failover_nids: [ ");
1030         spin_lock(&imp->imp_lock);
1031         j = 0;
1032         list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
1033                 seq_printf(m, "%s%s", j ? ", " : "",
1034                            libcfs_nid2str(conn->oic_conn->c_peer.nid));
1035                 j++;
1036         }
1037         seq_printf(m, " ]\n"
1038                       "       current_connection: %s\n"
1039                       "       connection_attempts: %u\n"
1040                       "       generation: %u\n"
1041                       "       in-progress_invalidations: %u\n",
1042                       imp->imp_connection == NULL ? "<none>" :
1043                               libcfs_nid2str(imp->imp_connection->c_peer.nid),
1044                       imp->imp_conn_cnt,
1045                       imp->imp_generation,
1046                       atomic_read(&imp->imp_inval_count));
1047         spin_unlock(&imp->imp_lock);
1048
1049         if (obd->obd_svc_stats == NULL)
1050                 goto out_climp;
1051
1052         header = &obd->obd_svc_stats->ls_cnt_header[PTLRPC_REQWAIT_CNTR];
1053         lprocfs_stats_collect(obd->obd_svc_stats, PTLRPC_REQWAIT_CNTR, &ret);
1054         if (ret.lc_count != 0) {
1055                 /* first argument to do_div MUST be __u64 */
1056                 __u64 sum = ret.lc_sum;
1057                 do_div(sum, ret.lc_count);
1058                 ret.lc_sum = sum;
1059         } else
1060                 ret.lc_sum = 0;
1061         seq_printf(m, "    rpcs:\n"
1062                       "       inflight: %u\n"
1063                       "       unregistering: %u\n"
1064                       "       timeouts: %u\n"
1065                       "       avg_waittime: "LPU64" %s\n",
1066                       atomic_read(&imp->imp_inflight),
1067                       atomic_read(&imp->imp_unregistering),
1068                       atomic_read(&imp->imp_timeouts),
1069                       ret.lc_sum, header->lc_units);
1070
1071         k = 0;
1072         for(j = 0; j < IMP_AT_MAX_PORTALS; j++) {
1073                 if (imp->imp_at.iat_portal[j] == 0)
1074                         break;
1075                 k = max_t(unsigned int, k,
1076                           at_get(&imp->imp_at.iat_service_estimate[j]));
1077         }
1078         seq_printf(m, "    service_estimates:\n"
1079                       "       services: %u sec\n"
1080                       "       network: %u sec\n",
1081                       k,
1082                       at_get(&imp->imp_at.iat_net_latency));
1083
1084         seq_printf(m, "    transactions:\n"
1085                       "       last_replay: "LPU64"\n"
1086                       "       peer_committed: "LPU64"\n"
1087                       "       last_checked: "LPU64"\n",
1088                       imp->imp_last_replay_transno,
1089                       imp->imp_peer_committed_transno,
1090                       imp->imp_last_transno_checked);
1091
1092         /* avg data rates */
1093         for (rw = 0; rw <= 1; rw++) {
1094                 lprocfs_stats_collect(obd->obd_svc_stats,
1095                                       PTLRPC_LAST_CNTR + BRW_READ_BYTES + rw,
1096                                       &ret);
1097                 if (ret.lc_sum > 0 && ret.lc_count > 0) {
1098                         /* first argument to do_div MUST be __u64 */
1099                         __u64 sum = ret.lc_sum;
1100                         do_div(sum, ret.lc_count);
1101                         ret.lc_sum = sum;
1102                         seq_printf(m, "    %s_data_averages:\n"
1103                                       "       bytes_per_rpc: "LPU64"\n",
1104                                       rw ? "write" : "read",
1105                                       ret.lc_sum);
1106                 }
1107                 k = (int)ret.lc_sum;
1108                 j = opcode_offset(OST_READ + rw) + EXTRA_MAX_OPCODES;
1109                 header = &obd->obd_svc_stats->ls_cnt_header[j];
1110                 lprocfs_stats_collect(obd->obd_svc_stats, j, &ret);
1111                 if (ret.lc_sum > 0 && ret.lc_count != 0) {
1112                         /* first argument to do_div MUST be __u64 */
1113                         __u64 sum = ret.lc_sum;
1114                         do_div(sum, ret.lc_count);
1115                         ret.lc_sum = sum;
1116                         seq_printf(m, "       %s_per_rpc: "LPU64"\n",
1117                                         header->lc_units, ret.lc_sum);
1118                         j = (int)ret.lc_sum;
1119                         if (j > 0)
1120                                 seq_printf(m, "       MB_per_sec: %u.%.02u\n",
1121                                                 k / j, (100 * k / j) % 100);
1122                 }
1123         }
1124
1125 out_climp:
1126         LPROCFS_CLIMP_EXIT(obd);
1127         return 0;
1128 }
1129 EXPORT_SYMBOL(lprocfs_import_seq_show);
1130
1131 int lprocfs_state_seq_show(struct seq_file *m, void *data)
1132 {
1133         struct obd_device *obd = (struct obd_device *)data;
1134         struct obd_import *imp;
1135         int j, k;
1136
1137         LASSERT(obd != NULL);
1138         LPROCFS_CLIMP_CHECK(obd);
1139         imp = obd->u.cli.cl_import;
1140
1141         seq_printf(m, "current_state: %s\n",
1142                    ptlrpc_import_state_name(imp->imp_state));
1143         seq_printf(m, "state_history:\n");
1144         k = imp->imp_state_hist_idx;
1145         for (j = 0; j < IMP_STATE_HIST_LEN; j++) {
1146                 struct import_state_hist *ish =
1147                         &imp->imp_state_hist[(k + j) % IMP_STATE_HIST_LEN];
1148                 if (ish->ish_state == 0)
1149                         continue;
1150                 seq_printf(m, " - [ "CFS_TIME_T", %s ]\n",
1151                            ish->ish_time,
1152                 ptlrpc_import_state_name(ish->ish_state));
1153         }
1154
1155         LPROCFS_CLIMP_EXIT(obd);
1156         return 0;
1157 }
1158 EXPORT_SYMBOL(lprocfs_state_seq_show);
1159
1160 int lprocfs_seq_at_hist_helper(struct seq_file *m, struct adaptive_timeout *at)
1161 {
1162         int i;
1163         for (i = 0; i < AT_BINS; i++)
1164                 seq_printf(m, "%3u ", at->at_hist[i]);
1165         seq_printf(m, "\n");
1166         return 0;
1167 }
1168 EXPORT_SYMBOL(lprocfs_seq_at_hist_helper);
1169
1170 /* See also ptlrpc_lprocfs_timeouts_show_seq */
1171 int lprocfs_timeouts_seq_show(struct seq_file *m, void *data)
1172 {
1173         struct obd_device *obd = (struct obd_device *)data;
1174         struct obd_import *imp;
1175         unsigned int cur, worst;
1176         time_t now, worstt;
1177         struct dhms ts;
1178         int i;
1179
1180         LASSERT(obd != NULL);
1181         LPROCFS_CLIMP_CHECK(obd);
1182         imp = obd->u.cli.cl_import;
1183
1184         now = cfs_time_current_sec();
1185
1186         /* Some network health info for kicks */
1187         s2dhms(&ts, now - imp->imp_last_reply_time);
1188         seq_printf(m, "%-10s : %ld, "DHMS_FMT" ago\n",
1189                    "last reply", imp->imp_last_reply_time, DHMS_VARS(&ts));
1190
1191         cur = at_get(&imp->imp_at.iat_net_latency);
1192         worst = imp->imp_at.iat_net_latency.at_worst_ever;
1193         worstt = imp->imp_at.iat_net_latency.at_worst_time;
1194         s2dhms(&ts, now - worstt);
1195         seq_printf(m, "%-10s : cur %3u  worst %3u (at %ld, "DHMS_FMT" ago) ",
1196                    "network", cur, worst, worstt, DHMS_VARS(&ts));
1197         lprocfs_seq_at_hist_helper(m, &imp->imp_at.iat_net_latency);
1198
1199         for(i = 0; i < IMP_AT_MAX_PORTALS; i++) {
1200                 if (imp->imp_at.iat_portal[i] == 0)
1201                         break;
1202                 cur = at_get(&imp->imp_at.iat_service_estimate[i]);
1203                 worst = imp->imp_at.iat_service_estimate[i].at_worst_ever;
1204                 worstt = imp->imp_at.iat_service_estimate[i].at_worst_time;
1205                 s2dhms(&ts, now - worstt);
1206                 seq_printf(m, "portal %-2d  : cur %3u  worst %3u (at %ld, "
1207                            DHMS_FMT" ago) ", imp->imp_at.iat_portal[i],
1208                            cur, worst, worstt, DHMS_VARS(&ts));
1209                 lprocfs_seq_at_hist_helper(m, &imp->imp_at.iat_service_estimate[i]);
1210         }
1211
1212         LPROCFS_CLIMP_EXIT(obd);
1213         return 0;
1214 }
1215 EXPORT_SYMBOL(lprocfs_timeouts_seq_show);
1216
1217 int lprocfs_connect_flags_seq_show(struct seq_file *m, void *data)
1218 {
1219         struct obd_device *obd = data;
1220         __u64 flags;
1221
1222         LPROCFS_CLIMP_CHECK(obd);
1223         flags = obd->u.cli.cl_import->imp_connect_data.ocd_connect_flags;
1224         seq_printf(m, "flags="LPX64"\n", flags);
1225         obd_connect_seq_flags2str(m, flags, "\n");
1226         seq_printf(m, "\n");
1227         LPROCFS_CLIMP_EXIT(obd);
1228         return 0;
1229 }
1230 EXPORT_SYMBOL(lprocfs_connect_flags_seq_show);
1231
1232 #ifndef HAVE_ONLY_PROCFS_SEQ
1233
1234 int lprocfs_rd_uint(char *page, char **start, off_t off,
1235                     int count, int *eof, void *data)
1236 {
1237         unsigned int *temp = data;
1238         return snprintf(page, count, "%u\n", *temp);
1239 }
1240 EXPORT_SYMBOL(lprocfs_rd_uint);
1241
1242 int lprocfs_rd_u64(char *page, char **start, off_t off,
1243                    int count, int *eof, void *data)
1244 {
1245         LASSERT(data != NULL);
1246         *eof = 1;
1247         return snprintf(page, count, LPU64"\n", *(__u64 *)data);
1248 }
1249 EXPORT_SYMBOL(lprocfs_rd_u64);
1250
1251 int lprocfs_rd_atomic(char *page, char **start, off_t off,
1252                    int count, int *eof, void *data)
1253 {
1254         atomic_t *atom = data;
1255         LASSERT(atom != NULL);
1256         *eof = 1;
1257         return snprintf(page, count, "%d\n", atomic_read(atom));
1258 }
1259 EXPORT_SYMBOL(lprocfs_rd_atomic);
1260
1261 int lprocfs_wr_atomic(struct file *file, const char *buffer,
1262                       unsigned long count, void *data)
1263 {
1264         atomic_t *atm = data;
1265         int val = 0;
1266         int rc;
1267
1268         rc = lprocfs_write_helper(buffer, count, &val);
1269         if (rc < 0)
1270                 return rc;
1271
1272         if (val <= 0)
1273                 return -ERANGE;
1274
1275         atomic_set(atm, val);
1276         return count;
1277 }
1278 EXPORT_SYMBOL(lprocfs_wr_atomic);
1279
1280 int lprocfs_rd_uuid(char *page, char **start, off_t off, int count,
1281                     int *eof, void *data)
1282 {
1283         struct obd_device *obd = data;
1284
1285         LASSERT(obd != NULL);
1286         *eof = 1;
1287         return snprintf(page, count, "%s\n", obd->obd_uuid.uuid);
1288 }
1289 EXPORT_SYMBOL(lprocfs_rd_uuid);
1290
1291 int lprocfs_rd_name(char *page, char **start, off_t off, int count,
1292                     int *eof, void *data)
1293 {
1294         struct obd_device *dev = data;
1295
1296         LASSERT(dev != NULL);
1297         *eof = 1;
1298         return snprintf(page, count, "%s\n", dev->obd_name);
1299 }
1300 EXPORT_SYMBOL(lprocfs_rd_name);
1301
1302 int lprocfs_rd_blksize(char *page, char **start, off_t off, int count,
1303                        int *eof, void *data)
1304 {
1305         struct obd_device *obd = data;
1306         struct obd_statfs  osfs;
1307         int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
1308                             cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
1309                             OBD_STATFS_NODELAY);
1310         if (!rc) {
1311                 *eof = 1;
1312                 rc = snprintf(page, count, "%u\n", osfs.os_bsize);
1313         }
1314         return rc;
1315 }
1316 EXPORT_SYMBOL(lprocfs_rd_blksize);
1317
1318 int lprocfs_rd_kbytestotal(char *page, char **start, off_t off, int count,
1319                            int *eof, void *data)
1320 {
1321         struct obd_device *obd = data;
1322         struct obd_statfs  osfs;
1323         int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
1324                             cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
1325                             OBD_STATFS_NODELAY);
1326         if (!rc) {
1327                 __u32 blk_size = osfs.os_bsize >> 10;
1328                 __u64 result = osfs.os_blocks;
1329
1330                 while (blk_size >>= 1)
1331                         result <<= 1;
1332
1333                 *eof = 1;
1334                 rc = snprintf(page, count, LPU64"\n", result);
1335         }
1336         return rc;
1337 }
1338 EXPORT_SYMBOL(lprocfs_rd_kbytestotal);
1339
1340 int lprocfs_rd_kbytesfree(char *page, char **start, off_t off, int count,
1341                           int *eof, void *data)
1342 {
1343         struct obd_device *obd = data;
1344         struct obd_statfs  osfs;
1345         int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
1346                             cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
1347                             OBD_STATFS_NODELAY);
1348         if (!rc) {
1349                 __u32 blk_size = osfs.os_bsize >> 10;
1350                 __u64 result = osfs.os_bfree;
1351
1352                 while (blk_size >>= 1)
1353                         result <<= 1;
1354
1355                 *eof = 1;
1356                 rc = snprintf(page, count, LPU64"\n", result);
1357         }
1358         return rc;
1359 }
1360 EXPORT_SYMBOL(lprocfs_rd_kbytesfree);
1361
1362 int lprocfs_rd_kbytesavail(char *page, char **start, off_t off, int count,
1363                            int *eof, void *data)
1364 {
1365         struct obd_device *obd = data;
1366         struct obd_statfs  osfs;
1367         int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
1368                             cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
1369                             OBD_STATFS_NODELAY);
1370         if (!rc) {
1371                 __u32 blk_size = osfs.os_bsize >> 10;
1372                 __u64 result = osfs.os_bavail;
1373
1374                 while (blk_size >>= 1)
1375                         result <<= 1;
1376
1377                 *eof = 1;
1378                 rc = snprintf(page, count, LPU64"\n", result);
1379         }
1380         return rc;
1381 }
1382 EXPORT_SYMBOL(lprocfs_rd_kbytesavail);
1383
1384 int lprocfs_rd_filestotal(char *page, char **start, off_t off, int count,
1385                           int *eof, void *data)
1386 {
1387         struct obd_device *obd = data;
1388         struct obd_statfs  osfs;
1389         int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
1390                             cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
1391                             OBD_STATFS_NODELAY);
1392         if (!rc) {
1393                 *eof = 1;
1394                 rc = snprintf(page, count, LPU64"\n", osfs.os_files);
1395         }
1396
1397         return rc;
1398 }
1399 EXPORT_SYMBOL(lprocfs_rd_filestotal);
1400
1401 int lprocfs_rd_filesfree(char *page, char **start, off_t off, int count,
1402                          int *eof, void *data)
1403 {
1404         struct obd_device *obd = data;
1405         struct obd_statfs  osfs;
1406         int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
1407                             cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
1408                             OBD_STATFS_NODELAY);
1409         if (!rc) {
1410                 *eof = 1;
1411                 rc = snprintf(page, count, LPU64"\n", osfs.os_ffree);
1412         }
1413         return rc;
1414 }
1415 EXPORT_SYMBOL(lprocfs_rd_filesfree);
1416
1417 int lprocfs_rd_server_uuid(char *page, char **start, off_t off, int count,
1418                            int *eof, void *data)
1419 {
1420         struct obd_device *obd = data;
1421         struct obd_import *imp;
1422         char *imp_state_name = NULL;
1423         int rc = 0;
1424
1425         LASSERT(obd != NULL);
1426         LPROCFS_CLIMP_CHECK(obd);
1427         imp = obd->u.cli.cl_import;
1428         imp_state_name = ptlrpc_import_state_name(imp->imp_state);
1429         *eof = 1;
1430         rc = snprintf(page, count, "%s\t%s%s\n",
1431                       obd2cli_tgt(obd), imp_state_name,
1432                       imp->imp_deactive ? "\tDEACTIVATED" : "");
1433
1434         LPROCFS_CLIMP_EXIT(obd);
1435         return rc;
1436 }
1437 EXPORT_SYMBOL(lprocfs_rd_server_uuid);
1438
1439 int lprocfs_rd_conn_uuid(char *page, char **start, off_t off, int count,
1440                          int *eof,  void *data)
1441 {
1442         struct obd_device *obd = data;
1443         struct ptlrpc_connection *conn;
1444         int rc = 0;
1445
1446         LASSERT(obd != NULL);
1447
1448         LPROCFS_CLIMP_CHECK(obd);
1449         conn = obd->u.cli.cl_import->imp_connection;
1450         *eof = 1;
1451         if (conn && obd->u.cli.cl_import) {
1452                 rc = snprintf(page, count, "%s\n",
1453                               conn->c_remote_uuid.uuid);
1454         } else {
1455                 rc = snprintf(page, count, "%s\n", "<none>");
1456         }
1457
1458         LPROCFS_CLIMP_EXIT(obd);
1459         return rc;
1460 }
1461 EXPORT_SYMBOL(lprocfs_rd_conn_uuid);
1462
1463 /**
1464  * Append a space separated list of current set flags to str.
1465  */
1466 #define flag2str(flag) \
1467         if (imp->imp_##flag && max - len > 0) \
1468              len += snprintf(str + len, max - len, "%s" #flag, len ? ", " : "");
1469 static int obd_import_flags2str(struct obd_import *imp, char *str, int max)
1470 {
1471         int len = 0;
1472
1473         if (imp->imp_obd->obd_no_recov)
1474                 len += snprintf(str, max - len, "no_recov");
1475
1476         flag2str(invalid);
1477         flag2str(deactive);
1478         flag2str(replayable);
1479         flag2str(pingable);
1480         return len;
1481 }
1482 #undef flags2str
1483
1484 int lprocfs_rd_import(char *page, char **start, off_t off, int count,
1485                       int *eof, void *data)
1486 {
1487         struct lprocfs_counter          ret;
1488         struct lprocfs_counter_header   *header;
1489         struct obd_device               *obd    = (struct obd_device *)data;
1490         struct obd_import               *imp;
1491         struct obd_import_conn          *conn;
1492         int                             i;
1493         int                             j;
1494         int                             k;
1495         int                             rw      = 0;
1496
1497         LASSERT(obd != NULL);
1498         LPROCFS_CLIMP_CHECK(obd);
1499         imp = obd->u.cli.cl_import;
1500         *eof = 1;
1501
1502         i = snprintf(page, count,
1503                      "import:\n"
1504                      "    name: %s\n"
1505                      "    target: %s\n"
1506                      "    state: %s\n"
1507                      "    instance: %u\n"
1508                      "    connect_flags: [",
1509                      obd->obd_name,
1510                      obd2cli_tgt(obd),
1511                      ptlrpc_import_state_name(imp->imp_state),
1512                      imp->imp_connect_data.ocd_instance);
1513         i += obd_connect_flags2str(page + i, count - i,
1514                                    imp->imp_connect_data.ocd_connect_flags,
1515                                    ", ");
1516         i += snprintf(page + i, count - i,
1517                       "]\n"
1518                       "    import_flags: [");
1519         i += obd_import_flags2str(imp, page + i, count - i);
1520
1521         i += snprintf(page + i, count - i,
1522                       "]\n"
1523                       "    connection:\n"
1524                       "       failover_nids: [");
1525         spin_lock(&imp->imp_lock);
1526         j = 0;
1527         list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
1528                 i += snprintf(page + i, count - i, "%s%s", j ? ", " : "",
1529                               libcfs_nid2str(conn->oic_conn->c_peer.nid));
1530                 j++;
1531         }
1532         i += snprintf(page + i, count - i,
1533                       "]\n"
1534                       "       current_connection: %s\n"
1535                       "       connection_attempts: %u\n"
1536                       "       generation: %u\n"
1537                       "       in-progress_invalidations: %u\n",
1538                       imp->imp_connection == NULL ? "<none>" :
1539                               libcfs_nid2str(imp->imp_connection->c_peer.nid),
1540                       imp->imp_conn_cnt,
1541                       imp->imp_generation,
1542                       atomic_read(&imp->imp_inval_count));
1543         spin_unlock(&imp->imp_lock);
1544
1545         if (obd->obd_svc_stats == NULL)
1546                 goto out_climp;
1547
1548         header = &obd->obd_svc_stats->ls_cnt_header[PTLRPC_REQWAIT_CNTR];
1549         lprocfs_stats_collect(obd->obd_svc_stats, PTLRPC_REQWAIT_CNTR, &ret);
1550         if (ret.lc_count != 0) {
1551                 /* first argument to do_div MUST be __u64 */
1552                 __u64 sum = ret.lc_sum;
1553                 do_div(sum, ret.lc_count);
1554                 ret.lc_sum = sum;
1555         } else
1556                 ret.lc_sum = 0;
1557         i += snprintf(page + i, count - i,
1558                       "    rpcs:\n"
1559                       "       inflight: %u\n"
1560                       "       unregistering: %u\n"
1561                       "       timeouts: %u\n"
1562                       "       avg_waittime: "LPU64" %s\n",
1563                       atomic_read(&imp->imp_inflight),
1564                       atomic_read(&imp->imp_unregistering),
1565                       atomic_read(&imp->imp_timeouts),
1566                       ret.lc_sum, header->lc_units);
1567
1568         k = 0;
1569         for(j = 0; j < IMP_AT_MAX_PORTALS; j++) {
1570                 if (imp->imp_at.iat_portal[j] == 0)
1571                         break;
1572                 k = max_t(unsigned int, k,
1573                           at_get(&imp->imp_at.iat_service_estimate[j]));
1574         }
1575         i += snprintf(page + i, count - i,
1576                       "    service_estimates:\n"
1577                       "       services: %u sec\n"
1578                       "       network: %u sec\n",
1579                       k,
1580                       at_get(&imp->imp_at.iat_net_latency));
1581
1582         i += snprintf(page + i, count - i,
1583                       "    transactions:\n"
1584                       "       last_replay: "LPU64"\n"
1585                       "       peer_committed: "LPU64"\n"
1586                       "       last_checked: "LPU64"\n",
1587                       imp->imp_last_replay_transno,
1588                       imp->imp_peer_committed_transno,
1589                       imp->imp_last_transno_checked);
1590
1591         /* avg data rates */
1592         for (rw = 0; rw <= 1; rw++) {
1593                 lprocfs_stats_collect(obd->obd_svc_stats,
1594                                       PTLRPC_LAST_CNTR + BRW_READ_BYTES + rw,
1595                                       &ret);
1596                 if (ret.lc_sum > 0 && ret.lc_count > 0) {
1597                         /* first argument to do_div MUST be __u64 */
1598                         __u64 sum = ret.lc_sum;
1599                         do_div(sum, ret.lc_count);
1600                         ret.lc_sum = sum;
1601                         i += snprintf(page + i, count - i,
1602                                       "    %s_data_averages:\n"
1603                                       "       bytes_per_rpc: "LPU64"\n",
1604                                       rw ? "write" : "read",
1605                                       ret.lc_sum);
1606                 }
1607                 k = (int)ret.lc_sum;
1608                 j = opcode_offset(OST_READ + rw) + EXTRA_MAX_OPCODES;
1609                 header = &obd->obd_svc_stats->ls_cnt_header[j];
1610                 lprocfs_stats_collect(obd->obd_svc_stats, j, &ret);
1611                 if (ret.lc_sum > 0 && ret.lc_count != 0) {
1612                         /* first argument to do_div MUST be __u64 */
1613                         __u64 sum = ret.lc_sum;
1614                         do_div(sum, ret.lc_count);
1615                         ret.lc_sum = sum;
1616                         i += snprintf(page + i, count - i,
1617                                       "       %s_per_rpc: "LPU64"\n",
1618                                       header->lc_units, ret.lc_sum);
1619                         j = (int)ret.lc_sum;
1620                         if (j > 0)
1621                                 i += snprintf(page + i, count - i,
1622                                               "       MB_per_sec: %u.%.02u\n",
1623                                               k / j, (100 * k / j) % 100);
1624                 }
1625         }
1626
1627 out_climp:
1628         LPROCFS_CLIMP_EXIT(obd);
1629         return i;
1630 }
1631 EXPORT_SYMBOL(lprocfs_rd_import);
1632
1633 int lprocfs_rd_state(char *page, char **start, off_t off, int count,
1634                       int *eof, void *data)
1635 {
1636         struct obd_device *obd = (struct obd_device *)data;
1637         struct obd_import *imp;
1638         int i, j, k;
1639
1640         LASSERT(obd != NULL);
1641         LPROCFS_CLIMP_CHECK(obd);
1642         imp = obd->u.cli.cl_import;
1643         *eof = 1;
1644
1645         i = snprintf(page, count, "current_state: %s\n",
1646                      ptlrpc_import_state_name(imp->imp_state));
1647         i += snprintf(page + i, count - i,
1648                       "state_history:\n");
1649         k = imp->imp_state_hist_idx;
1650         for (j = 0; j < IMP_STATE_HIST_LEN; j++) {
1651                 struct import_state_hist *ish =
1652                         &imp->imp_state_hist[(k + j) % IMP_STATE_HIST_LEN];
1653                 if (ish->ish_state == 0)
1654                         continue;
1655                 i += snprintf(page + i, count - i, " - ["CFS_TIME_T", %s]\n",
1656                               ish->ish_time,
1657                               ptlrpc_import_state_name(ish->ish_state));
1658         }
1659
1660         LPROCFS_CLIMP_EXIT(obd);
1661         return i;
1662 }
1663 EXPORT_SYMBOL(lprocfs_rd_state);
1664
1665 int lprocfs_at_hist_helper(char *page, int count, int rc,
1666                            struct adaptive_timeout *at)
1667 {
1668         int i;
1669         for (i = 0; i < AT_BINS; i++)
1670                 rc += snprintf(page + rc, count - rc, "%3u ", at->at_hist[i]);
1671         rc += snprintf(page + rc, count - rc, "\n");
1672         return rc;
1673 }
1674 EXPORT_SYMBOL(lprocfs_at_hist_helper);
1675
1676 /* See also ptlrpc_lprocfs_rd_timeouts */
1677 int lprocfs_rd_timeouts(char *page, char **start, off_t off, int count,
1678                         int *eof, void *data)
1679 {
1680         struct obd_device *obd = (struct obd_device *)data;
1681         struct obd_import *imp;
1682         unsigned int cur, worst;
1683         time_t now, worstt;
1684         struct dhms ts;
1685         int i, rc = 0;
1686
1687         LASSERT(obd != NULL);
1688         LPROCFS_CLIMP_CHECK(obd);
1689         imp = obd->u.cli.cl_import;
1690         *eof = 1;
1691
1692         now = cfs_time_current_sec();
1693
1694         /* Some network health info for kicks */
1695         s2dhms(&ts, now - imp->imp_last_reply_time);
1696         rc += snprintf(page + rc, count - rc,
1697                        "%-10s : %ld, "DHMS_FMT" ago\n",
1698                        "last reply", imp->imp_last_reply_time, DHMS_VARS(&ts));
1699
1700         cur = at_get(&imp->imp_at.iat_net_latency);
1701         worst = imp->imp_at.iat_net_latency.at_worst_ever;
1702         worstt = imp->imp_at.iat_net_latency.at_worst_time;
1703         s2dhms(&ts, now - worstt);
1704         rc += snprintf(page + rc, count - rc,
1705                        "%-10s : cur %3u  worst %3u (at %ld, "DHMS_FMT" ago) ",
1706                        "network", cur, worst, worstt, DHMS_VARS(&ts));
1707         rc = lprocfs_at_hist_helper(page, count, rc,
1708                                     &imp->imp_at.iat_net_latency);
1709
1710         for(i = 0; i < IMP_AT_MAX_PORTALS; i++) {
1711                 if (imp->imp_at.iat_portal[i] == 0)
1712                         break;
1713                 cur = at_get(&imp->imp_at.iat_service_estimate[i]);
1714                 worst = imp->imp_at.iat_service_estimate[i].at_worst_ever;
1715                 worstt = imp->imp_at.iat_service_estimate[i].at_worst_time;
1716                 s2dhms(&ts, now - worstt);
1717                 rc += snprintf(page + rc, count - rc,
1718                                "portal %-2d  : cur %3u  worst %3u (at %ld, "
1719                                DHMS_FMT" ago) ", imp->imp_at.iat_portal[i],
1720                                cur, worst, worstt, DHMS_VARS(&ts));
1721                 rc = lprocfs_at_hist_helper(page, count, rc,
1722                                           &imp->imp_at.iat_service_estimate[i]);
1723         }
1724
1725         LPROCFS_CLIMP_EXIT(obd);
1726         return rc;
1727 }
1728 EXPORT_SYMBOL(lprocfs_rd_timeouts);
1729
1730 int lprocfs_rd_connect_flags(char *page, char **start, off_t off,
1731                              int count, int *eof, void *data)
1732 {
1733         struct obd_device *obd = data;
1734         __u64 flags;
1735         int ret = 0;
1736
1737         LPROCFS_CLIMP_CHECK(obd);
1738         flags = obd->u.cli.cl_import->imp_connect_data.ocd_connect_flags;
1739         ret = snprintf(page, count, "flags="LPX64"\n", flags);
1740         ret += obd_connect_flags2str(page + ret, count - ret, flags, "\n");
1741         ret += snprintf(page + ret, count - ret, "\n");
1742         LPROCFS_CLIMP_EXIT(obd);
1743         return ret;
1744 }
1745 EXPORT_SYMBOL(lprocfs_rd_connect_flags);
1746
1747 int lprocfs_rd_numrefs(char *page, char **start, off_t off, int count,
1748                        int *eof, void *data)
1749 {
1750         struct obd_type *class = (struct obd_type*) data;
1751
1752         LASSERT(class != NULL);
1753         *eof = 1;
1754         return snprintf(page, count, "%d\n", class->typ_refcnt);
1755 }
1756 EXPORT_SYMBOL(lprocfs_rd_numrefs);
1757
1758 int lprocfs_obd_setup(struct obd_device *obd, struct lprocfs_vars *list)
1759 {
1760         int rc = 0;
1761
1762         LASSERT(obd != NULL);
1763         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
1764         LASSERT(obd->obd_type->typ_procroot != NULL);
1765
1766         obd->obd_proc_entry = lprocfs_register(obd->obd_name,
1767                                                obd->obd_type->typ_procroot,
1768                                                list, obd);
1769         if (IS_ERR(obd->obd_proc_entry)) {
1770                 rc = PTR_ERR(obd->obd_proc_entry);
1771                 CERROR("error %d setting up lprocfs for %s\n",rc,obd->obd_name);
1772                 obd->obd_proc_entry = NULL;
1773         }
1774         return rc;
1775 }
1776 EXPORT_SYMBOL(lprocfs_obd_setup);
1777 #endif
1778
1779 int
1780 lprocfs_seq_obd_setup(struct obd_device *obd)
1781 {
1782         int rc = 0;
1783
1784         LASSERT(obd != NULL);
1785         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
1786         LASSERT(obd->obd_type->typ_procroot != NULL);
1787
1788         obd->obd_proc_entry = lprocfs_seq_register(obd->obd_name,
1789                                                    obd->obd_type->typ_procroot,
1790                                                    obd->obd_vars, obd);
1791         if (IS_ERR(obd->obd_proc_entry)) {
1792                 rc = PTR_ERR(obd->obd_proc_entry);
1793                 CERROR("error %d setting up lprocfs for %s\n",rc,obd->obd_name);
1794                 obd->obd_proc_entry = NULL;
1795         }
1796         return rc;
1797 }
1798 EXPORT_SYMBOL(lprocfs_seq_obd_setup);
1799
1800 int lprocfs_obd_cleanup(struct obd_device *obd)
1801 {
1802         if (!obd)
1803                 return -EINVAL;
1804         if (obd->obd_proc_exports_entry) {
1805                 /* Should be no exports left */
1806                 lprocfs_remove(&obd->obd_proc_exports_entry);
1807                 obd->obd_proc_exports_entry = NULL;
1808         }
1809         if (obd->obd_proc_entry) {
1810                 lprocfs_remove(&obd->obd_proc_entry);
1811                 obd->obd_proc_entry = NULL;
1812         }
1813         return 0;
1814 }
1815 EXPORT_SYMBOL(lprocfs_obd_cleanup);
1816
1817 int lprocfs_stats_alloc_one(struct lprocfs_stats *stats, unsigned int cpuid)
1818 {
1819         struct lprocfs_counter  *cntr;
1820         unsigned int            percpusize;
1821         int                     rc = -ENOMEM;
1822         unsigned long           flags = 0;
1823         int                     i;
1824
1825         LASSERT(stats->ls_percpu[cpuid] == NULL);
1826         LASSERT((stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) == 0);
1827
1828         percpusize = lprocfs_stats_counter_size(stats);
1829         LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[cpuid], percpusize);
1830         if (stats->ls_percpu[cpuid] != NULL) {
1831                 rc = 0;
1832                 if (unlikely(stats->ls_biggest_alloc_num <= cpuid)) {
1833                         if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1834                                 spin_lock_irqsave(&stats->ls_lock, flags);
1835                         else
1836                                 spin_lock(&stats->ls_lock);
1837                         if (stats->ls_biggest_alloc_num <= cpuid)
1838                                 stats->ls_biggest_alloc_num = cpuid + 1;
1839                         if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) {
1840                                 spin_unlock_irqrestore(&stats->ls_lock, flags);
1841                         } else {
1842                                 spin_unlock(&stats->ls_lock);
1843                         }
1844                 }
1845                 /* initialize the ls_percpu[cpuid] non-zero counter */
1846                 for (i = 0; i < stats->ls_num; ++i) {
1847                         cntr = lprocfs_stats_counter_get(stats, cpuid, i);
1848                         cntr->lc_min = LC_MIN_INIT;
1849                 }
1850         }
1851         return rc;
1852 }
1853 EXPORT_SYMBOL(lprocfs_stats_alloc_one);
1854
1855 struct lprocfs_stats *lprocfs_alloc_stats(unsigned int num,
1856                                           enum lprocfs_stats_flags flags)
1857 {
1858         struct lprocfs_stats    *stats;
1859         unsigned int            num_entry;
1860         unsigned int            percpusize = 0;
1861         int                     i;
1862
1863         if (num == 0)
1864                 return NULL;
1865
1866         if (lprocfs_no_percpu_stats != 0)
1867                 flags |= LPROCFS_STATS_FLAG_NOPERCPU;
1868
1869         if (flags & LPROCFS_STATS_FLAG_NOPERCPU)
1870                 num_entry = 1;
1871         else
1872                 num_entry = num_possible_cpus();
1873
1874         /* alloc percpu pointers for all possible cpu slots */
1875         LIBCFS_ALLOC(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1876         if (stats == NULL)
1877                 return NULL;
1878
1879         stats->ls_num = num;
1880         stats->ls_flags = flags;
1881         spin_lock_init(&stats->ls_lock);
1882
1883         /* alloc num of counter headers */
1884         LIBCFS_ALLOC(stats->ls_cnt_header,
1885                      stats->ls_num * sizeof(struct lprocfs_counter_header));
1886         if (stats->ls_cnt_header == NULL)
1887                 goto fail;
1888
1889         if ((flags & LPROCFS_STATS_FLAG_NOPERCPU) != 0) {
1890                 /* contains only one set counters */
1891                 percpusize = lprocfs_stats_counter_size(stats);
1892                 LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[0], percpusize);
1893                 if (stats->ls_percpu[0] == NULL)
1894                         goto fail;
1895                 stats->ls_biggest_alloc_num = 1;
1896         } else if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0) {
1897                 /* alloc all percpu data, currently only obd_memory use this */
1898                 for (i = 0; i < num_entry; ++i)
1899                         if (lprocfs_stats_alloc_one(stats, i) < 0)
1900                                 goto fail;
1901         }
1902
1903         return stats;
1904
1905 fail:
1906         lprocfs_free_stats(&stats);
1907         return NULL;
1908 }
1909 EXPORT_SYMBOL(lprocfs_alloc_stats);
1910
1911 void lprocfs_free_stats(struct lprocfs_stats **statsh)
1912 {
1913         struct lprocfs_stats *stats = *statsh;
1914         unsigned int num_entry;
1915         unsigned int percpusize;
1916         unsigned int i;
1917
1918         if (stats == NULL || stats->ls_num == 0)
1919                 return;
1920         *statsh = NULL;
1921
1922         if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU)
1923                 num_entry = 1;
1924         else
1925                 num_entry = num_possible_cpus();
1926
1927         percpusize = lprocfs_stats_counter_size(stats);
1928         for (i = 0; i < num_entry; i++)
1929                 if (stats->ls_percpu[i] != NULL)
1930                         LIBCFS_FREE(stats->ls_percpu[i], percpusize);
1931         if (stats->ls_cnt_header != NULL)
1932                 LIBCFS_FREE(stats->ls_cnt_header, stats->ls_num *
1933                                         sizeof(struct lprocfs_counter_header));
1934         LIBCFS_FREE(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1935 }
1936 EXPORT_SYMBOL(lprocfs_free_stats);
1937
1938 void lprocfs_clear_stats(struct lprocfs_stats *stats)
1939 {
1940         struct lprocfs_counter          *percpu_cntr;
1941         int                             i;
1942         int                             j;
1943         unsigned int                    num_entry;
1944         unsigned long                   flags = 0;
1945
1946         num_entry = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1947
1948         for (i = 0; i < num_entry; i++) {
1949                 if (stats->ls_percpu[i] == NULL)
1950                         continue;
1951                 for (j = 0; j < stats->ls_num; j++) {
1952                         percpu_cntr = lprocfs_stats_counter_get(stats, i, j);
1953                         percpu_cntr->lc_count           = 0;
1954                         percpu_cntr->lc_min             = LC_MIN_INIT;
1955                         percpu_cntr->lc_max             = 0;
1956                         percpu_cntr->lc_sumsquare       = 0;
1957                         percpu_cntr->lc_sum             = 0;
1958                         if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1959                                 percpu_cntr->lc_sum_irq = 0;
1960                 }
1961         }
1962
1963         lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1964 }
1965 EXPORT_SYMBOL(lprocfs_clear_stats);
1966
1967 static ssize_t lprocfs_stats_seq_write(struct file *file, const char *buf,
1968                                        size_t len, loff_t *off)
1969 {
1970         struct seq_file *seq = file->private_data;
1971         struct lprocfs_stats *stats = seq->private;
1972
1973         lprocfs_clear_stats(stats);
1974
1975         return len;
1976 }
1977
1978 static void *lprocfs_stats_seq_start(struct seq_file *p, loff_t *pos)
1979 {
1980         struct lprocfs_stats *stats = p->private;
1981
1982         return (*pos < stats->ls_num) ? pos : NULL;
1983 }
1984
1985 static void lprocfs_stats_seq_stop(struct seq_file *p, void *v)
1986 {
1987 }
1988
1989 static void *lprocfs_stats_seq_next(struct seq_file *p, void *v, loff_t *pos)
1990 {
1991         (*pos)++;
1992
1993         return lprocfs_stats_seq_start(p, pos);
1994 }
1995
1996 /* seq file export of one lprocfs counter */
1997 static int lprocfs_stats_seq_show(struct seq_file *p, void *v)
1998 {
1999         struct lprocfs_stats            *stats  = p->private;
2000         struct lprocfs_counter_header   *hdr;
2001         struct lprocfs_counter           ctr;
2002         int                              idx    = *(loff_t *)v;
2003         int                              rc     = 0;
2004
2005         if (idx == 0) {
2006                 struct timeval now;
2007
2008                 do_gettimeofday(&now);
2009                 rc = seq_printf(p, "%-25s %lu.%lu secs.usecs\n",
2010                                 "snapshot_time", now.tv_sec, now.tv_usec);
2011                 if (rc < 0)
2012                         return rc;
2013         }
2014
2015         hdr = &stats->ls_cnt_header[idx];
2016         lprocfs_stats_collect(stats, idx, &ctr);
2017
2018         if (ctr.lc_count == 0)
2019                 goto out;
2020
2021         rc = seq_printf(p, "%-25s "LPD64" samples [%s]", hdr->lc_name,
2022                         ctr.lc_count, hdr->lc_units);
2023         if (rc < 0)
2024                 goto out;
2025
2026         if ((hdr->lc_config & LPROCFS_CNTR_AVGMINMAX) && ctr.lc_count > 0) {
2027                 rc = seq_printf(p, " "LPD64" "LPD64" "LPD64,
2028                                 ctr.lc_min, ctr.lc_max, ctr.lc_sum);
2029                 if (rc < 0)
2030                         goto out;
2031                 if (hdr->lc_config & LPROCFS_CNTR_STDDEV)
2032                         rc = seq_printf(p, " "LPD64, ctr.lc_sumsquare);
2033                 if (rc < 0)
2034                         goto out;
2035         }
2036         rc = seq_printf(p, "\n");
2037 out:
2038         return (rc < 0) ? rc : 0;
2039 }
2040
2041 struct seq_operations lprocfs_stats_seq_sops = {
2042         .start  = lprocfs_stats_seq_start,
2043         .stop   = lprocfs_stats_seq_stop,
2044         .next   = lprocfs_stats_seq_next,
2045         .show   = lprocfs_stats_seq_show,
2046 };
2047
2048 static int lprocfs_stats_seq_open(struct inode *inode, struct file *file)
2049 {
2050         struct seq_file *seq;
2051         int rc;
2052
2053 #ifndef HAVE_ONLY_PROCFS_SEQ
2054         if (LPROCFS_ENTRY_CHECK(PDE(inode)))
2055                 return -ENOENT;
2056 #endif
2057         rc = seq_open(file, &lprocfs_stats_seq_sops);
2058         if (rc)
2059                 return rc;
2060         seq = file->private_data;
2061         seq->private = PDE_DATA(inode);
2062         return 0;
2063 }
2064
2065 struct file_operations lprocfs_stats_seq_fops = {
2066         .owner   = THIS_MODULE,
2067         .open    = lprocfs_stats_seq_open,
2068         .read    = seq_read,
2069         .write   = lprocfs_stats_seq_write,
2070         .llseek  = seq_lseek,
2071         .release = lprocfs_seq_release,
2072 };
2073
2074 int lprocfs_register_stats(struct proc_dir_entry *root, const char *name,
2075                            struct lprocfs_stats *stats)
2076 {
2077         struct proc_dir_entry *entry;
2078         LASSERT(root != NULL);
2079
2080         entry = proc_create_data(name, 0644, root,
2081                                  &lprocfs_stats_seq_fops, stats);
2082         if (entry == NULL)
2083                 return -ENOMEM;
2084         return 0;
2085 }
2086 EXPORT_SYMBOL(lprocfs_register_stats);
2087
2088 void lprocfs_counter_init(struct lprocfs_stats *stats, int index,
2089                           unsigned conf, const char *name, const char *units)
2090 {
2091         struct lprocfs_counter_header   *header;
2092         struct lprocfs_counter          *percpu_cntr;
2093         unsigned long                   flags = 0;
2094         unsigned int                    i;
2095         unsigned int                    num_cpu;
2096
2097         LASSERT(stats != NULL);
2098
2099         header = &stats->ls_cnt_header[index];
2100         LASSERTF(header != NULL, "Failed to allocate stats header:[%d]%s/%s\n",
2101                  index, name, units);
2102
2103         header->lc_config = conf;
2104         header->lc_name   = name;
2105         header->lc_units  = units;
2106
2107         num_cpu = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
2108         for (i = 0; i < num_cpu; ++i) {
2109                 if (stats->ls_percpu[i] == NULL)
2110                         continue;
2111                 percpu_cntr = lprocfs_stats_counter_get(stats, i, index);
2112                 percpu_cntr->lc_count           = 0;
2113                 percpu_cntr->lc_min             = LC_MIN_INIT;
2114                 percpu_cntr->lc_max             = 0;
2115                 percpu_cntr->lc_sumsquare       = 0;
2116                 percpu_cntr->lc_sum             = 0;
2117                 if ((stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
2118                         percpu_cntr->lc_sum_irq = 0;
2119         }
2120         lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
2121 }
2122 EXPORT_SYMBOL(lprocfs_counter_init);
2123
2124 /* Note that we only init md counters for ops whose offset is less
2125  * than NUM_MD_STATS. This is explained in a comment in the definition
2126  * of struct md_ops. */
2127 #define LPROCFS_MD_OP_INIT(base, stats, op)                                    \
2128         do {                                                                   \
2129                 unsigned int _idx = base + MD_COUNTER_OFFSET(op);              \
2130                                                                                \
2131                 if (MD_COUNTER_OFFSET(op) < NUM_MD_STATS) {                    \
2132                         LASSERT(_idx < stats->ls_num);                         \
2133                         lprocfs_counter_init(stats, _idx, 0, #op, "reqs");     \
2134                 }                                                              \
2135         } while (0)
2136
2137 void lprocfs_init_mps_stats(int num_private_stats, struct lprocfs_stats *stats)
2138 {
2139         LPROCFS_MD_OP_INIT(num_private_stats, stats, getstatus);
2140         LPROCFS_MD_OP_INIT(num_private_stats, stats, null_inode);
2141         LPROCFS_MD_OP_INIT(num_private_stats, stats, find_cbdata);
2142         LPROCFS_MD_OP_INIT(num_private_stats, stats, close);
2143         LPROCFS_MD_OP_INIT(num_private_stats, stats, create);
2144         LPROCFS_MD_OP_INIT(num_private_stats, stats, done_writing);
2145         LPROCFS_MD_OP_INIT(num_private_stats, stats, enqueue);
2146         LPROCFS_MD_OP_INIT(num_private_stats, stats, getattr);
2147         LPROCFS_MD_OP_INIT(num_private_stats, stats, getattr_name);
2148         LPROCFS_MD_OP_INIT(num_private_stats, stats, intent_lock);
2149         LPROCFS_MD_OP_INIT(num_private_stats, stats, link);
2150         LPROCFS_MD_OP_INIT(num_private_stats, stats, rename);
2151         LPROCFS_MD_OP_INIT(num_private_stats, stats, setattr);
2152         LPROCFS_MD_OP_INIT(num_private_stats, stats, fsync);
2153         LPROCFS_MD_OP_INIT(num_private_stats, stats, read_page);
2154         LPROCFS_MD_OP_INIT(num_private_stats, stats, unlink);
2155         LPROCFS_MD_OP_INIT(num_private_stats, stats, setxattr);
2156         LPROCFS_MD_OP_INIT(num_private_stats, stats, getxattr);
2157         LPROCFS_MD_OP_INIT(num_private_stats, stats, init_ea_size);
2158         LPROCFS_MD_OP_INIT(num_private_stats, stats, get_lustre_md);
2159         LPROCFS_MD_OP_INIT(num_private_stats, stats, free_lustre_md);
2160         LPROCFS_MD_OP_INIT(num_private_stats, stats, update_lsm_md);
2161         LPROCFS_MD_OP_INIT(num_private_stats, stats, merge_attr);
2162         LPROCFS_MD_OP_INIT(num_private_stats, stats, set_open_replay_data);
2163         LPROCFS_MD_OP_INIT(num_private_stats, stats, clear_open_replay_data);
2164         LPROCFS_MD_OP_INIT(num_private_stats, stats, set_lock_data);
2165         LPROCFS_MD_OP_INIT(num_private_stats, stats, lock_match);
2166         LPROCFS_MD_OP_INIT(num_private_stats, stats, cancel_unused);
2167         LPROCFS_MD_OP_INIT(num_private_stats, stats, renew_capa);
2168         LPROCFS_MD_OP_INIT(num_private_stats, stats, unpack_capa);
2169         LPROCFS_MD_OP_INIT(num_private_stats, stats, get_remote_perm);
2170         LPROCFS_MD_OP_INIT(num_private_stats, stats, intent_getattr_async);
2171         LPROCFS_MD_OP_INIT(num_private_stats, stats, revalidate_lock);
2172 }
2173 EXPORT_SYMBOL(lprocfs_init_mps_stats);
2174
2175 int lprocfs_alloc_md_stats(struct obd_device *obd,
2176                            unsigned int num_private_stats)
2177 {
2178         struct lprocfs_stats *stats;
2179         unsigned int num_stats;
2180         int rc, i;
2181
2182         CLASSERT(offsetof(struct md_ops, MD_STATS_FIRST_OP) == 0);
2183         CLASSERT(_MD_COUNTER_OFFSET(MD_STATS_FIRST_OP) == 0);
2184         CLASSERT(_MD_COUNTER_OFFSET(MD_STATS_LAST_OP) > 0);
2185
2186         /* TODO Ensure that this function is only used where
2187          * appropriate by adding an assertion to the effect that
2188          * obd->obd_type->typ_md_ops is not NULL. We can't do this now
2189          * because mdt_procfs_init() uses this function to allocate
2190          * the stats backing /proc/fs/lustre/mdt/.../md_stats but the
2191          * mdt layer does not use the md_ops interface. This is
2192          * confusing and a waste of memory. See LU-2484.
2193          */
2194         LASSERT(obd->obd_proc_entry != NULL);
2195         LASSERT(obd->obd_md_stats == NULL);
2196         LASSERT(obd->obd_md_cntr_base == 0);
2197
2198         num_stats = NUM_MD_STATS + num_private_stats;
2199         stats = lprocfs_alloc_stats(num_stats, 0);
2200         if (stats == NULL)
2201                 return -ENOMEM;
2202
2203         lprocfs_init_mps_stats(num_private_stats, stats);
2204
2205         for (i = num_private_stats; i < num_stats; i++) {
2206                 if (stats->ls_cnt_header[i].lc_name == NULL) {
2207                         CERROR("Missing md_stat initializer md_op "
2208                                "operation at offset %d. Aborting.\n",
2209                                i - num_private_stats);
2210                         LBUG();
2211                 }
2212         }
2213
2214         rc = lprocfs_register_stats(obd->obd_proc_entry, "md_stats", stats);
2215         if (rc < 0) {
2216                 lprocfs_free_stats(&stats);
2217         } else {
2218                 obd->obd_md_stats = stats;
2219                 obd->obd_md_cntr_base = num_private_stats;
2220         }
2221
2222         return rc;
2223 }
2224 EXPORT_SYMBOL(lprocfs_alloc_md_stats);
2225
2226 void lprocfs_free_md_stats(struct obd_device *obd)
2227 {
2228         struct lprocfs_stats *stats = obd->obd_md_stats;
2229
2230         if (stats != NULL) {
2231                 obd->obd_md_stats = NULL;
2232                 obd->obd_md_cntr_base = 0;
2233                 lprocfs_free_stats(&stats);
2234         }
2235 }
2236 EXPORT_SYMBOL(lprocfs_free_md_stats);
2237
2238 void lprocfs_init_ldlm_stats(struct lprocfs_stats *ldlm_stats)
2239 {
2240         lprocfs_counter_init(ldlm_stats,
2241                              LDLM_ENQUEUE - LDLM_FIRST_OPC,
2242                              0, "ldlm_enqueue", "reqs");
2243         lprocfs_counter_init(ldlm_stats,
2244                              LDLM_CONVERT - LDLM_FIRST_OPC,
2245                              0, "ldlm_convert", "reqs");
2246         lprocfs_counter_init(ldlm_stats,
2247                              LDLM_CANCEL - LDLM_FIRST_OPC,
2248                              0, "ldlm_cancel", "reqs");
2249         lprocfs_counter_init(ldlm_stats,
2250                              LDLM_BL_CALLBACK - LDLM_FIRST_OPC,
2251                              0, "ldlm_bl_callback", "reqs");
2252         lprocfs_counter_init(ldlm_stats,
2253                              LDLM_CP_CALLBACK - LDLM_FIRST_OPC,
2254                              0, "ldlm_cp_callback", "reqs");
2255         lprocfs_counter_init(ldlm_stats,
2256                              LDLM_GL_CALLBACK - LDLM_FIRST_OPC,
2257                              0, "ldlm_gl_callback", "reqs");
2258 }
2259 EXPORT_SYMBOL(lprocfs_init_ldlm_stats);
2260
2261 __s64 lprocfs_read_helper(struct lprocfs_counter *lc,
2262                           struct lprocfs_counter_header *header,
2263                           enum lprocfs_stats_flags flags,
2264                           enum lprocfs_fields_flags field)
2265 {
2266         __s64 ret = 0;
2267
2268         if (lc == NULL || header == NULL)
2269                 RETURN(0);
2270
2271         switch (field) {
2272                 case LPROCFS_FIELDS_FLAGS_CONFIG:
2273                         ret = header->lc_config;
2274                         break;
2275                 case LPROCFS_FIELDS_FLAGS_SUM:
2276                         ret = lc->lc_sum;
2277                         if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
2278                                 ret += lc->lc_sum_irq;
2279                         break;
2280                 case LPROCFS_FIELDS_FLAGS_MIN:
2281                         ret = lc->lc_min;
2282                         break;
2283                 case LPROCFS_FIELDS_FLAGS_MAX:
2284                         ret = lc->lc_max;
2285                         break;
2286                 case LPROCFS_FIELDS_FLAGS_AVG:
2287                         ret = (lc->lc_max - lc->lc_min) / 2;
2288                         break;
2289                 case LPROCFS_FIELDS_FLAGS_SUMSQUARE:
2290                         ret = lc->lc_sumsquare;
2291                         break;
2292                 case LPROCFS_FIELDS_FLAGS_COUNT:
2293                         ret = lc->lc_count;
2294                         break;
2295                 default:
2296                         break;
2297         };
2298         RETURN(ret);
2299 }
2300 EXPORT_SYMBOL(lprocfs_read_helper);
2301
2302 int lprocfs_write_helper(const char *buffer, unsigned long count,
2303                          int *val)
2304 {
2305         return lprocfs_write_frac_helper(buffer, count, val, 1);
2306 }
2307 EXPORT_SYMBOL(lprocfs_write_helper);
2308
2309 int lprocfs_write_frac_helper(const char *buffer, unsigned long count,
2310                               int *val, int mult)
2311 {
2312         char kernbuf[20], *end, *pbuf;
2313
2314         if (count > (sizeof(kernbuf) - 1))
2315                 return -EINVAL;
2316
2317         if (copy_from_user(kernbuf, buffer, count))
2318                 return -EFAULT;
2319
2320         kernbuf[count] = '\0';
2321         pbuf = kernbuf;
2322         if (*pbuf == '-') {
2323                 mult = -mult;
2324                 pbuf++;
2325         }
2326
2327         *val = (int)simple_strtoul(pbuf, &end, 10) * mult;
2328         if (pbuf == end)
2329                 return -EINVAL;
2330
2331         if (end != NULL && *end == '.') {
2332                 int temp_val, pow = 1;
2333                 int i;
2334
2335                 pbuf = end + 1;
2336                 if (strlen(pbuf) > 5)
2337                         pbuf[5] = '\0'; /*only allow 5bits fractional*/
2338
2339                 temp_val = (int)simple_strtoul(pbuf, &end, 10) * mult;
2340
2341                 if (pbuf < end) {
2342                         for (i = 0; i < (end - pbuf); i++)
2343                                 pow *= 10;
2344
2345                         *val += temp_val / pow;
2346                 }
2347         }
2348         return 0;
2349 }
2350 EXPORT_SYMBOL(lprocfs_write_frac_helper);
2351
2352 int lprocfs_read_frac_helper(char *buffer, unsigned long count, long val,
2353                              int mult)
2354 {
2355         long decimal_val, frac_val;
2356         int prtn;
2357
2358         if (count < 10)
2359                 return -EINVAL;
2360
2361         decimal_val = val / mult;
2362         prtn = snprintf(buffer, count, "%ld", decimal_val);
2363         frac_val = val % mult;
2364
2365         if (prtn < (count - 4) && frac_val > 0) {
2366                 long temp_frac;
2367                 int i, temp_mult = 1, frac_bits = 0;
2368
2369                 temp_frac = frac_val * 10;
2370                 buffer[prtn++] = '.';
2371                 while (frac_bits < 2 && (temp_frac / mult) < 1 ) {
2372                         /* only reserved 2 bits fraction */
2373                         buffer[prtn++] ='0';
2374                         temp_frac *= 10;
2375                         frac_bits++;
2376                 }
2377                 /*
2378                  * Need to think these cases :
2379                  *      1. #echo x.00 > /proc/xxx       output result : x
2380                  *      2. #echo x.0x > /proc/xxx       output result : x.0x
2381                  *      3. #echo x.x0 > /proc/xxx       output result : x.x
2382                  *      4. #echo x.xx > /proc/xxx       output result : x.xx
2383                  *      Only reserved 2 bits fraction.
2384                  */
2385                 for (i = 0; i < (5 - prtn); i++)
2386                         temp_mult *= 10;
2387
2388                 frac_bits = min((int)count - prtn, 3 - frac_bits);
2389                 prtn += snprintf(buffer + prtn, frac_bits, "%ld",
2390                                  frac_val * temp_mult / mult);
2391
2392                 prtn--;
2393                 while(buffer[prtn] < '1' || buffer[prtn] > '9') {
2394                         prtn--;
2395                         if (buffer[prtn] == '.') {
2396                                 prtn--;
2397                                 break;
2398                         }
2399                 }
2400                 prtn++;
2401         }
2402         buffer[prtn++] ='\n';
2403         return prtn;
2404 }
2405 EXPORT_SYMBOL(lprocfs_read_frac_helper);
2406
2407 int lprocfs_seq_read_frac_helper(struct seq_file *m, long val, int mult)
2408 {
2409         long decimal_val, frac_val;
2410
2411         decimal_val = val / mult;
2412         seq_printf(m, "%ld", decimal_val);
2413         frac_val = val % mult;
2414
2415         if (frac_val > 0) {
2416                 frac_val *= 100;
2417                 frac_val /= mult;
2418         }
2419         if (frac_val > 0) {
2420                 /* Three cases: x0, xx, 0x */
2421                 if ((frac_val % 10) != 0)
2422                         seq_printf(m, ".%ld", frac_val);
2423                 else
2424                         seq_printf(m, ".%ld", frac_val / 10);
2425         }
2426
2427         seq_printf(m, "\n");
2428         return 0;
2429 }
2430 EXPORT_SYMBOL(lprocfs_seq_read_frac_helper);
2431
2432 int lprocfs_write_u64_helper(const char *buffer, unsigned long count,__u64 *val)
2433 {
2434         return lprocfs_write_frac_u64_helper(buffer, count, val, 1);
2435 }
2436 EXPORT_SYMBOL(lprocfs_write_u64_helper);
2437
2438 int lprocfs_write_frac_u64_helper(const char *buffer, unsigned long count,
2439                               __u64 *val, int mult)
2440 {
2441         char kernbuf[22], *end, *pbuf;
2442         __u64 whole, frac = 0, units;
2443         unsigned frac_d = 1;
2444
2445         if (count > (sizeof(kernbuf) - 1))
2446                 return -EINVAL;
2447
2448         if (copy_from_user(kernbuf, buffer, count))
2449                 return -EFAULT;
2450
2451         kernbuf[count] = '\0';
2452         pbuf = kernbuf;
2453         if (*pbuf == '-') {
2454                 mult = -mult;
2455                 pbuf++;
2456         }
2457
2458         whole = simple_strtoull(pbuf, &end, 10);
2459         if (pbuf == end)
2460                 return -EINVAL;
2461
2462         if (end != NULL && *end == '.') {
2463                 int i;
2464                 pbuf = end + 1;
2465
2466                 /* need to limit frac_d to a __u32 */
2467                 if (strlen(pbuf) > 10)
2468                         pbuf[10] = '\0';
2469
2470                 frac = simple_strtoull(pbuf, &end, 10);
2471                 /* count decimal places */
2472                 for (i = 0; i < (end - pbuf); i++)
2473                         frac_d *= 10;
2474         }
2475
2476         units = 1;
2477         if (end != NULL) {
2478                 switch (*end) {
2479                 case 'p': case 'P':
2480                         units <<= 10;
2481                 case 't': case 'T':
2482                         units <<= 10;
2483                 case 'g': case 'G':
2484                         units <<= 10;
2485                 case 'm': case 'M':
2486                         units <<= 10;
2487                 case 'k': case 'K':
2488                         units <<= 10;
2489                 }
2490         }
2491         /* Specified units override the multiplier */
2492         if (units > 1)
2493                 mult = mult < 0 ? -units : units;
2494
2495         frac *= mult;
2496         do_div(frac, frac_d);
2497         *val = whole * mult + frac;
2498         return 0;
2499 }
2500 EXPORT_SYMBOL(lprocfs_write_frac_u64_helper);
2501
2502 static char *lprocfs_strnstr(const char *s1, const char *s2, size_t len)
2503 {
2504         size_t l2;
2505
2506         l2 = strlen(s2);
2507         if (!l2)
2508                 return (char *)s1;
2509         while (len >= l2) {
2510                 len--;
2511                 if (!memcmp(s1, s2, l2))
2512                         return (char *)s1;
2513                 s1++;
2514         }
2515         return NULL;
2516 }
2517
2518 /**
2519  * Find the string \a name in the input \a buffer, and return a pointer to the
2520  * value immediately following \a name, reducing \a count appropriately.
2521  * If \a name is not found the original \a buffer is returned.
2522  */
2523 char *lprocfs_find_named_value(const char *buffer, const char *name,
2524                                 size_t *count)
2525 {
2526         char *val;
2527         size_t buflen = *count;
2528
2529         /* there is no strnstr() in rhel5 and ubuntu kernels */
2530         val = lprocfs_strnstr(buffer, name, buflen);
2531         if (val == NULL)
2532                 return (char *)buffer;
2533
2534         val += strlen(name);                             /* skip prefix */
2535         while (val < buffer + buflen && isspace(*val)) /* skip separator */
2536                 val++;
2537
2538         *count = 0;
2539         while (val < buffer + buflen && isalnum(*val)) {
2540                 ++*count;
2541                 ++val;
2542         }
2543
2544         return val - *count;
2545 }
2546 EXPORT_SYMBOL(lprocfs_find_named_value);
2547
2548 int lprocfs_seq_create(struct proc_dir_entry *parent,
2549                        const char *name,
2550                        mode_t mode,
2551                        const struct file_operations *seq_fops,
2552                        void *data)
2553 {
2554         struct proc_dir_entry *entry;
2555         ENTRY;
2556
2557         /* Disallow secretly (un)writable entries. */
2558         LASSERT((seq_fops->write == NULL) == ((mode & 0222) == 0));
2559
2560         entry = proc_create_data(name, mode, parent, seq_fops, data);
2561
2562         if (entry == NULL)
2563                 RETURN(-ENOMEM);
2564
2565         RETURN(0);
2566 }
2567 EXPORT_SYMBOL(lprocfs_seq_create);
2568
2569 int lprocfs_obd_seq_create(struct obd_device *dev,
2570                            const char *name,
2571                            mode_t mode,
2572                            const struct file_operations *seq_fops,
2573                            void *data)
2574 {
2575         return (lprocfs_seq_create(dev->obd_proc_entry, name,
2576                                    mode, seq_fops, data));
2577 }
2578 EXPORT_SYMBOL(lprocfs_obd_seq_create);
2579
2580 void lprocfs_oh_tally(struct obd_histogram *oh, unsigned int value)
2581 {
2582         if (value >= OBD_HIST_MAX)
2583                 value = OBD_HIST_MAX - 1;
2584
2585         spin_lock(&oh->oh_lock);
2586         oh->oh_buckets[value]++;
2587         spin_unlock(&oh->oh_lock);
2588 }
2589 EXPORT_SYMBOL(lprocfs_oh_tally);
2590
2591 void lprocfs_oh_tally_log2(struct obd_histogram *oh, unsigned int value)
2592 {
2593         unsigned int val = 0;
2594
2595         if (likely(value != 0))
2596                 val = min(fls(value - 1), OBD_HIST_MAX);
2597
2598         lprocfs_oh_tally(oh, val);
2599 }
2600 EXPORT_SYMBOL(lprocfs_oh_tally_log2);
2601
2602 unsigned long lprocfs_oh_sum(struct obd_histogram *oh)
2603 {
2604         unsigned long ret = 0;
2605         int i;
2606
2607         for (i = 0; i < OBD_HIST_MAX; i++)
2608                 ret +=  oh->oh_buckets[i];
2609         return ret;
2610 }
2611 EXPORT_SYMBOL(lprocfs_oh_sum);
2612
2613 void lprocfs_oh_clear(struct obd_histogram *oh)
2614 {
2615         spin_lock(&oh->oh_lock);
2616         memset(oh->oh_buckets, 0, sizeof(oh->oh_buckets));
2617         spin_unlock(&oh->oh_lock);
2618 }
2619 EXPORT_SYMBOL(lprocfs_oh_clear);
2620
2621 int lprocfs_obd_rd_max_pages_per_rpc(char *page, char **start, off_t off,
2622                                      int count, int *eof, void *data)
2623 {
2624         struct obd_device *dev = data;
2625         struct client_obd *cli = &dev->u.cli;
2626         int rc;
2627
2628         client_obd_list_lock(&cli->cl_loi_list_lock);
2629         rc = snprintf(page, count, "%d\n", cli->cl_max_pages_per_rpc);
2630         client_obd_list_unlock(&cli->cl_loi_list_lock);
2631         return rc;
2632 }
2633 EXPORT_SYMBOL(lprocfs_obd_rd_max_pages_per_rpc);
2634
2635 int lprocfs_obd_max_pages_per_rpc_seq_show(struct seq_file *m, void *data)
2636 {
2637         struct obd_device *dev = data;
2638         struct client_obd *cli = &dev->u.cli;
2639         int rc;
2640
2641         client_obd_list_lock(&cli->cl_loi_list_lock);
2642         rc = seq_printf(m, "%d\n", cli->cl_max_pages_per_rpc);
2643         client_obd_list_unlock(&cli->cl_loi_list_lock);
2644         return rc;
2645 }
2646 EXPORT_SYMBOL(lprocfs_obd_max_pages_per_rpc_seq_show);
2647
2648 int lprocfs_wr_root_squash(const char __user *buffer, unsigned long count,
2649                            struct root_squash_info *squash, char *name)
2650 {
2651         int rc;
2652         char kernbuf[64], *tmp, *errmsg;
2653         unsigned long uid, gid;
2654         ENTRY;
2655
2656         if (count >= sizeof(kernbuf)) {
2657                 errmsg = "string too long";
2658                 GOTO(failed_noprint, rc = -EINVAL);
2659         }
2660         if (copy_from_user(kernbuf, buffer, count)) {
2661                 errmsg = "bad address";
2662                 GOTO(failed_noprint, rc = -EFAULT);
2663         }
2664         kernbuf[count] = '\0';
2665
2666         /* look for uid gid separator */
2667         tmp = strchr(kernbuf, ':');
2668         if (tmp == NULL) {
2669                 errmsg = "needs uid:gid format";
2670                 GOTO(failed, rc = -EINVAL);
2671         }
2672         *tmp = '\0';
2673         tmp++;
2674
2675         /* parse uid */
2676         if (kstrtoul(kernbuf, 0, &uid) != 0) {
2677                 errmsg = "bad uid";
2678                 GOTO(failed, rc = -EINVAL);
2679         }
2680
2681         /* parse gid */
2682         if (kstrtoul(tmp, 0, &gid) != 0) {
2683                 errmsg = "bad gid";
2684                 GOTO(failed, rc = -EINVAL);
2685         }
2686
2687         squash->rsi_uid = uid;
2688         squash->rsi_gid = gid;
2689
2690         LCONSOLE_INFO("%s: root_squash is set to %u:%u\n",
2691                       name, squash->rsi_uid, squash->rsi_gid);
2692         RETURN(count);
2693
2694 failed:
2695         if (tmp != NULL) {
2696                 tmp--;
2697                 *tmp = ':';
2698         }
2699         CWARN("%s: failed to set root_squash to \"%s\", %s, rc = %d\n",
2700               name, kernbuf, errmsg, rc);
2701         RETURN(rc);
2702 failed_noprint:
2703         CWARN("%s: failed to set root_squash due to %s, rc = %d\n",
2704               name, errmsg, rc);
2705         RETURN(rc);
2706 }
2707 EXPORT_SYMBOL(lprocfs_wr_root_squash);
2708
2709
2710 int lprocfs_wr_nosquash_nids(const char __user *buffer, unsigned long count,
2711                              struct root_squash_info *squash, char *name)
2712 {
2713         int rc;
2714         char *kernbuf = NULL;
2715         char *errmsg;
2716         struct list_head tmp;
2717         ENTRY;
2718
2719         if (count > 4096) {
2720                 errmsg = "string too long";
2721                 GOTO(failed, rc = -EINVAL);
2722         }
2723
2724         OBD_ALLOC(kernbuf, count + 1);
2725         if (kernbuf == NULL) {
2726                 errmsg = "no memory";
2727                 GOTO(failed, rc = -ENOMEM);
2728         }
2729         if (copy_from_user(kernbuf, buffer, count)) {
2730                 errmsg = "bad address";
2731                 GOTO(failed, rc = -EFAULT);
2732         }
2733         kernbuf[count] = '\0';
2734
2735         if (count > 0 && kernbuf[count - 1] == '\n')
2736                 kernbuf[count - 1] = '\0';
2737
2738         if (strcmp(kernbuf, "NONE") == 0 || strcmp(kernbuf, "clear") == 0) {
2739                 /* empty string is special case */
2740                 down_write(&squash->rsi_sem);
2741                 if (!list_empty(&squash->rsi_nosquash_nids))
2742                         cfs_free_nidlist(&squash->rsi_nosquash_nids);
2743                 up_write(&squash->rsi_sem);
2744                 LCONSOLE_INFO("%s: nosquash_nids is cleared\n", name);
2745                 OBD_FREE(kernbuf, count + 1);
2746                 RETURN(count);
2747         }
2748
2749         INIT_LIST_HEAD(&tmp);
2750         if (cfs_parse_nidlist(kernbuf, count, &tmp) <= 0) {
2751                 errmsg = "can't parse";
2752                 GOTO(failed, rc = -EINVAL);
2753         }
2754         LCONSOLE_INFO("%s: nosquash_nids set to %s\n",
2755                       name, kernbuf);
2756         OBD_FREE(kernbuf, count + 1);
2757         kernbuf = NULL;
2758
2759         down_write(&squash->rsi_sem);
2760         if (!list_empty(&squash->rsi_nosquash_nids))
2761                 cfs_free_nidlist(&squash->rsi_nosquash_nids);
2762         list_splice(&tmp, &squash->rsi_nosquash_nids);
2763         up_write(&squash->rsi_sem);
2764
2765         RETURN(count);
2766
2767 failed:
2768         if (kernbuf) {
2769                 CWARN("%s: failed to set nosquash_nids to \"%s\", %s rc = %d\n",
2770                       name, kernbuf, errmsg, rc);
2771                 OBD_FREE(kernbuf, count + 1);
2772         } else {
2773                 CWARN("%s: failed to set nosquash_nids due to %s rc = %d\n",
2774                       name, errmsg, rc);
2775         }
2776         RETURN(rc);
2777 }
2778 EXPORT_SYMBOL(lprocfs_wr_nosquash_nids);
2779
2780 #endif /* LPROCFS*/