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