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