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