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