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