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