Whamcloud - gitweb
10f80521603b9a7b2c1dc8256dbab72c57f37cec
[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 flag2str(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_flags2str(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         flag2str(invalid);
830         flag2str(deactive);
831         flag2str(replayable);
832         flag2str(delayed_recovery);
833         flag2str(no_lock_replay);
834         flag2str(vbr_failed);
835         flag2str(pingable);
836         flag2str(resend_replay);
837         flag2str(no_pinger_recover);
838         flag2str(need_mne_swab);
839         flag2str(connect_tried);
840 }
841 #undef flag2str
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_flags2str(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 int
1231 lprocfs_obd_setup(struct obd_device *obd)
1232 {
1233         int rc = 0;
1234
1235         LASSERT(obd != NULL);
1236         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
1237         LASSERT(obd->obd_type->typ_procroot != NULL);
1238
1239         obd->obd_proc_entry = lprocfs_seq_register(obd->obd_name,
1240                                                    obd->obd_type->typ_procroot,
1241                                                    obd->obd_vars, obd);
1242         if (IS_ERR(obd->obd_proc_entry)) {
1243                 rc = PTR_ERR(obd->obd_proc_entry);
1244                 CERROR("error %d setting up lprocfs for %s\n",rc,obd->obd_name);
1245                 obd->obd_proc_entry = NULL;
1246         }
1247         return rc;
1248 }
1249 EXPORT_SYMBOL(lprocfs_obd_setup);
1250
1251 int lprocfs_obd_cleanup(struct obd_device *obd)
1252 {
1253         if (!obd)
1254                 return -EINVAL;
1255         if (obd->obd_proc_exports_entry) {
1256                 /* Should be no exports left */
1257                 lprocfs_remove(&obd->obd_proc_exports_entry);
1258                 obd->obd_proc_exports_entry = NULL;
1259         }
1260         if (obd->obd_proc_entry) {
1261                 lprocfs_remove(&obd->obd_proc_entry);
1262                 obd->obd_proc_entry = NULL;
1263         }
1264         return 0;
1265 }
1266 EXPORT_SYMBOL(lprocfs_obd_cleanup);
1267
1268 int lprocfs_stats_alloc_one(struct lprocfs_stats *stats, unsigned int cpuid)
1269 {
1270         struct lprocfs_counter  *cntr;
1271         unsigned int            percpusize;
1272         int                     rc = -ENOMEM;
1273         unsigned long           flags = 0;
1274         int                     i;
1275
1276         LASSERT(stats->ls_percpu[cpuid] == NULL);
1277         LASSERT((stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) == 0);
1278
1279         percpusize = lprocfs_stats_counter_size(stats);
1280         LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[cpuid], percpusize);
1281         if (stats->ls_percpu[cpuid] != NULL) {
1282                 rc = 0;
1283                 if (unlikely(stats->ls_biggest_alloc_num <= cpuid)) {
1284                         if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1285                                 spin_lock_irqsave(&stats->ls_lock, flags);
1286                         else
1287                                 spin_lock(&stats->ls_lock);
1288                         if (stats->ls_biggest_alloc_num <= cpuid)
1289                                 stats->ls_biggest_alloc_num = cpuid + 1;
1290                         if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) {
1291                                 spin_unlock_irqrestore(&stats->ls_lock, flags);
1292                         } else {
1293                                 spin_unlock(&stats->ls_lock);
1294                         }
1295                 }
1296                 /* initialize the ls_percpu[cpuid] non-zero counter */
1297                 for (i = 0; i < stats->ls_num; ++i) {
1298                         cntr = lprocfs_stats_counter_get(stats, cpuid, i);
1299                         cntr->lc_min = LC_MIN_INIT;
1300                 }
1301         }
1302         return rc;
1303 }
1304 EXPORT_SYMBOL(lprocfs_stats_alloc_one);
1305
1306 struct lprocfs_stats *lprocfs_alloc_stats(unsigned int num,
1307                                           enum lprocfs_stats_flags flags)
1308 {
1309         struct lprocfs_stats    *stats;
1310         unsigned int            num_entry;
1311         unsigned int            percpusize = 0;
1312         int                     i;
1313
1314         if (num == 0)
1315                 return NULL;
1316
1317         if (lprocfs_no_percpu_stats != 0)
1318                 flags |= LPROCFS_STATS_FLAG_NOPERCPU;
1319
1320         if (flags & LPROCFS_STATS_FLAG_NOPERCPU)
1321                 num_entry = 1;
1322         else
1323                 num_entry = num_possible_cpus();
1324
1325         /* alloc percpu pointers for all possible cpu slots */
1326         LIBCFS_ALLOC(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1327         if (stats == NULL)
1328                 return NULL;
1329
1330         stats->ls_num = num;
1331         stats->ls_flags = flags;
1332         spin_lock_init(&stats->ls_lock);
1333
1334         /* alloc num of counter headers */
1335         LIBCFS_ALLOC(stats->ls_cnt_header,
1336                      stats->ls_num * sizeof(struct lprocfs_counter_header));
1337         if (stats->ls_cnt_header == NULL)
1338                 goto fail;
1339
1340         if ((flags & LPROCFS_STATS_FLAG_NOPERCPU) != 0) {
1341                 /* contains only one set counters */
1342                 percpusize = lprocfs_stats_counter_size(stats);
1343                 LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[0], percpusize);
1344                 if (stats->ls_percpu[0] == NULL)
1345                         goto fail;
1346                 stats->ls_biggest_alloc_num = 1;
1347         } else if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0) {
1348                 /* alloc all percpu data, currently only obd_memory use this */
1349                 for (i = 0; i < num_entry; ++i)
1350                         if (lprocfs_stats_alloc_one(stats, i) < 0)
1351                                 goto fail;
1352         }
1353
1354         return stats;
1355
1356 fail:
1357         lprocfs_free_stats(&stats);
1358         return NULL;
1359 }
1360 EXPORT_SYMBOL(lprocfs_alloc_stats);
1361
1362 void lprocfs_free_stats(struct lprocfs_stats **statsh)
1363 {
1364         struct lprocfs_stats *stats = *statsh;
1365         unsigned int num_entry;
1366         unsigned int percpusize;
1367         unsigned int i;
1368
1369         if (stats == NULL || stats->ls_num == 0)
1370                 return;
1371         *statsh = NULL;
1372
1373         if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU)
1374                 num_entry = 1;
1375         else
1376                 num_entry = num_possible_cpus();
1377
1378         percpusize = lprocfs_stats_counter_size(stats);
1379         for (i = 0; i < num_entry; i++)
1380                 if (stats->ls_percpu[i] != NULL)
1381                         LIBCFS_FREE(stats->ls_percpu[i], percpusize);
1382         if (stats->ls_cnt_header != NULL)
1383                 LIBCFS_FREE(stats->ls_cnt_header, stats->ls_num *
1384                                         sizeof(struct lprocfs_counter_header));
1385         LIBCFS_FREE(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1386 }
1387 EXPORT_SYMBOL(lprocfs_free_stats);
1388
1389 void lprocfs_clear_stats(struct lprocfs_stats *stats)
1390 {
1391         struct lprocfs_counter          *percpu_cntr;
1392         int                             i;
1393         int                             j;
1394         unsigned int                    num_entry;
1395         unsigned long                   flags = 0;
1396
1397         num_entry = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1398
1399         for (i = 0; i < num_entry; i++) {
1400                 if (stats->ls_percpu[i] == NULL)
1401                         continue;
1402                 for (j = 0; j < stats->ls_num; j++) {
1403                         percpu_cntr = lprocfs_stats_counter_get(stats, i, j);
1404                         percpu_cntr->lc_count           = 0;
1405                         percpu_cntr->lc_min             = LC_MIN_INIT;
1406                         percpu_cntr->lc_max             = 0;
1407                         percpu_cntr->lc_sumsquare       = 0;
1408                         percpu_cntr->lc_sum             = 0;
1409                         if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1410                                 percpu_cntr->lc_sum_irq = 0;
1411                 }
1412         }
1413
1414         lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1415 }
1416 EXPORT_SYMBOL(lprocfs_clear_stats);
1417
1418 static ssize_t lprocfs_stats_seq_write(struct file *file, const char *buf,
1419                                        size_t len, loff_t *off)
1420 {
1421         struct seq_file *seq = file->private_data;
1422         struct lprocfs_stats *stats = seq->private;
1423
1424         lprocfs_clear_stats(stats);
1425
1426         return len;
1427 }
1428
1429 static void *lprocfs_stats_seq_start(struct seq_file *p, loff_t *pos)
1430 {
1431         struct lprocfs_stats *stats = p->private;
1432
1433         return (*pos < stats->ls_num) ? pos : NULL;
1434 }
1435
1436 static void lprocfs_stats_seq_stop(struct seq_file *p, void *v)
1437 {
1438 }
1439
1440 static void *lprocfs_stats_seq_next(struct seq_file *p, void *v, loff_t *pos)
1441 {
1442         (*pos)++;
1443
1444         return lprocfs_stats_seq_start(p, pos);
1445 }
1446
1447 /* seq file export of one lprocfs counter */
1448 static int lprocfs_stats_seq_show(struct seq_file *p, void *v)
1449 {
1450         struct lprocfs_stats            *stats  = p->private;
1451         struct lprocfs_counter_header   *hdr;
1452         struct lprocfs_counter           ctr;
1453         int                              idx    = *(loff_t *)v;
1454         int                              rc     = 0;
1455
1456         if (idx == 0) {
1457                 struct timeval now;
1458
1459                 do_gettimeofday(&now);
1460                 rc = seq_printf(p, "%-25s %lu.%lu secs.usecs\n",
1461                                 "snapshot_time", now.tv_sec, now.tv_usec);
1462                 if (rc < 0)
1463                         return rc;
1464         }
1465
1466         hdr = &stats->ls_cnt_header[idx];
1467         lprocfs_stats_collect(stats, idx, &ctr);
1468
1469         if (ctr.lc_count == 0)
1470                 goto out;
1471
1472         rc = seq_printf(p, "%-25s "LPD64" samples [%s]", hdr->lc_name,
1473                         ctr.lc_count, hdr->lc_units);
1474         if (rc < 0)
1475                 goto out;
1476
1477         if ((hdr->lc_config & LPROCFS_CNTR_AVGMINMAX) && ctr.lc_count > 0) {
1478                 rc = seq_printf(p, " "LPD64" "LPD64" "LPD64,
1479                                 ctr.lc_min, ctr.lc_max, ctr.lc_sum);
1480                 if (rc < 0)
1481                         goto out;
1482                 if (hdr->lc_config & LPROCFS_CNTR_STDDEV)
1483                         rc = seq_printf(p, " "LPD64, ctr.lc_sumsquare);
1484                 if (rc < 0)
1485                         goto out;
1486         }
1487         rc = seq_printf(p, "\n");
1488 out:
1489         return (rc < 0) ? rc : 0;
1490 }
1491
1492 struct seq_operations lprocfs_stats_seq_sops = {
1493         .start  = lprocfs_stats_seq_start,
1494         .stop   = lprocfs_stats_seq_stop,
1495         .next   = lprocfs_stats_seq_next,
1496         .show   = lprocfs_stats_seq_show,
1497 };
1498
1499 static int lprocfs_stats_seq_open(struct inode *inode, struct file *file)
1500 {
1501         struct seq_file *seq;
1502         int rc;
1503
1504 #ifndef HAVE_ONLY_PROCFS_SEQ
1505         if (LPROCFS_ENTRY_CHECK(PDE(inode)))
1506                 return -ENOENT;
1507 #endif
1508         rc = seq_open(file, &lprocfs_stats_seq_sops);
1509         if (rc)
1510                 return rc;
1511         seq = file->private_data;
1512         seq->private = PDE_DATA(inode);
1513         return 0;
1514 }
1515
1516 struct file_operations lprocfs_stats_seq_fops = {
1517         .owner   = THIS_MODULE,
1518         .open    = lprocfs_stats_seq_open,
1519         .read    = seq_read,
1520         .write   = lprocfs_stats_seq_write,
1521         .llseek  = seq_lseek,
1522         .release = lprocfs_seq_release,
1523 };
1524
1525 int lprocfs_register_stats(struct proc_dir_entry *root, const char *name,
1526                            struct lprocfs_stats *stats)
1527 {
1528         struct proc_dir_entry *entry;
1529         LASSERT(root != NULL);
1530
1531         entry = proc_create_data(name, 0644, root,
1532                                  &lprocfs_stats_seq_fops, stats);
1533         if (entry == NULL)
1534                 return -ENOMEM;
1535         return 0;
1536 }
1537 EXPORT_SYMBOL(lprocfs_register_stats);
1538
1539 void lprocfs_counter_init(struct lprocfs_stats *stats, int index,
1540                           unsigned conf, const char *name, const char *units)
1541 {
1542         struct lprocfs_counter_header   *header;
1543         struct lprocfs_counter          *percpu_cntr;
1544         unsigned long                   flags = 0;
1545         unsigned int                    i;
1546         unsigned int                    num_cpu;
1547
1548         LASSERT(stats != NULL);
1549
1550         header = &stats->ls_cnt_header[index];
1551         LASSERTF(header != NULL, "Failed to allocate stats header:[%d]%s/%s\n",
1552                  index, name, units);
1553
1554         header->lc_config = conf;
1555         header->lc_name   = name;
1556         header->lc_units  = units;
1557
1558         num_cpu = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1559         for (i = 0; i < num_cpu; ++i) {
1560                 if (stats->ls_percpu[i] == NULL)
1561                         continue;
1562                 percpu_cntr = lprocfs_stats_counter_get(stats, i, index);
1563                 percpu_cntr->lc_count           = 0;
1564                 percpu_cntr->lc_min             = LC_MIN_INIT;
1565                 percpu_cntr->lc_max             = 0;
1566                 percpu_cntr->lc_sumsquare       = 0;
1567                 percpu_cntr->lc_sum             = 0;
1568                 if ((stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1569                         percpu_cntr->lc_sum_irq = 0;
1570         }
1571         lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1572 }
1573 EXPORT_SYMBOL(lprocfs_counter_init);
1574
1575 /* Note that we only init md counters for ops whose offset is less
1576  * than NUM_MD_STATS. This is explained in a comment in the definition
1577  * of struct md_ops. */
1578 #define LPROCFS_MD_OP_INIT(base, stats, op)                                    \
1579         do {                                                                   \
1580                 unsigned int _idx = base + MD_COUNTER_OFFSET(op);              \
1581                                                                                \
1582                 if (MD_COUNTER_OFFSET(op) < NUM_MD_STATS) {                    \
1583                         LASSERT(_idx < stats->ls_num);                         \
1584                         lprocfs_counter_init(stats, _idx, 0, #op, "reqs");     \
1585                 }                                                              \
1586         } while (0)
1587
1588 void lprocfs_init_mps_stats(int num_private_stats, struct lprocfs_stats *stats)
1589 {
1590         LPROCFS_MD_OP_INIT(num_private_stats, stats, getstatus);
1591         LPROCFS_MD_OP_INIT(num_private_stats, stats, null_inode);
1592         LPROCFS_MD_OP_INIT(num_private_stats, stats, find_cbdata);
1593         LPROCFS_MD_OP_INIT(num_private_stats, stats, close);
1594         LPROCFS_MD_OP_INIT(num_private_stats, stats, create);
1595         LPROCFS_MD_OP_INIT(num_private_stats, stats, done_writing);
1596         LPROCFS_MD_OP_INIT(num_private_stats, stats, enqueue);
1597         LPROCFS_MD_OP_INIT(num_private_stats, stats, getattr);
1598         LPROCFS_MD_OP_INIT(num_private_stats, stats, getattr_name);
1599         LPROCFS_MD_OP_INIT(num_private_stats, stats, intent_lock);
1600         LPROCFS_MD_OP_INIT(num_private_stats, stats, link);
1601         LPROCFS_MD_OP_INIT(num_private_stats, stats, rename);
1602         LPROCFS_MD_OP_INIT(num_private_stats, stats, setattr);
1603         LPROCFS_MD_OP_INIT(num_private_stats, stats, fsync);
1604         LPROCFS_MD_OP_INIT(num_private_stats, stats, read_page);
1605         LPROCFS_MD_OP_INIT(num_private_stats, stats, unlink);
1606         LPROCFS_MD_OP_INIT(num_private_stats, stats, setxattr);
1607         LPROCFS_MD_OP_INIT(num_private_stats, stats, getxattr);
1608         LPROCFS_MD_OP_INIT(num_private_stats, stats, init_ea_size);
1609         LPROCFS_MD_OP_INIT(num_private_stats, stats, get_lustre_md);
1610         LPROCFS_MD_OP_INIT(num_private_stats, stats, free_lustre_md);
1611         LPROCFS_MD_OP_INIT(num_private_stats, stats, update_lsm_md);
1612         LPROCFS_MD_OP_INIT(num_private_stats, stats, merge_attr);
1613         LPROCFS_MD_OP_INIT(num_private_stats, stats, set_open_replay_data);
1614         LPROCFS_MD_OP_INIT(num_private_stats, stats, clear_open_replay_data);
1615         LPROCFS_MD_OP_INIT(num_private_stats, stats, set_lock_data);
1616         LPROCFS_MD_OP_INIT(num_private_stats, stats, lock_match);
1617         LPROCFS_MD_OP_INIT(num_private_stats, stats, cancel_unused);
1618         LPROCFS_MD_OP_INIT(num_private_stats, stats, renew_capa);
1619         LPROCFS_MD_OP_INIT(num_private_stats, stats, unpack_capa);
1620         LPROCFS_MD_OP_INIT(num_private_stats, stats, get_remote_perm);
1621         LPROCFS_MD_OP_INIT(num_private_stats, stats, intent_getattr_async);
1622         LPROCFS_MD_OP_INIT(num_private_stats, stats, revalidate_lock);
1623 }
1624 EXPORT_SYMBOL(lprocfs_init_mps_stats);
1625
1626 int lprocfs_alloc_md_stats(struct obd_device *obd,
1627                            unsigned int num_private_stats)
1628 {
1629         struct lprocfs_stats *stats;
1630         unsigned int num_stats;
1631         int rc, i;
1632
1633         CLASSERT(offsetof(struct md_ops, MD_STATS_FIRST_OP) == 0);
1634         CLASSERT(_MD_COUNTER_OFFSET(MD_STATS_FIRST_OP) == 0);
1635         CLASSERT(_MD_COUNTER_OFFSET(MD_STATS_LAST_OP) > 0);
1636
1637         /* TODO Ensure that this function is only used where
1638          * appropriate by adding an assertion to the effect that
1639          * obd->obd_type->typ_md_ops is not NULL. We can't do this now
1640          * because mdt_procfs_init() uses this function to allocate
1641          * the stats backing /proc/fs/lustre/mdt/.../md_stats but the
1642          * mdt layer does not use the md_ops interface. This is
1643          * confusing and a waste of memory. See LU-2484.
1644          */
1645         LASSERT(obd->obd_proc_entry != NULL);
1646         LASSERT(obd->obd_md_stats == NULL);
1647         LASSERT(obd->obd_md_cntr_base == 0);
1648
1649         num_stats = NUM_MD_STATS + num_private_stats;
1650         stats = lprocfs_alloc_stats(num_stats, 0);
1651         if (stats == NULL)
1652                 return -ENOMEM;
1653
1654         lprocfs_init_mps_stats(num_private_stats, stats);
1655
1656         for (i = num_private_stats; i < num_stats; i++) {
1657                 if (stats->ls_cnt_header[i].lc_name == NULL) {
1658                         CERROR("Missing md_stat initializer md_op "
1659                                "operation at offset %d. Aborting.\n",
1660                                i - num_private_stats);
1661                         LBUG();
1662                 }
1663         }
1664
1665         rc = lprocfs_register_stats(obd->obd_proc_entry, "md_stats", stats);
1666         if (rc < 0) {
1667                 lprocfs_free_stats(&stats);
1668         } else {
1669                 obd->obd_md_stats = stats;
1670                 obd->obd_md_cntr_base = num_private_stats;
1671         }
1672
1673         return rc;
1674 }
1675 EXPORT_SYMBOL(lprocfs_alloc_md_stats);
1676
1677 void lprocfs_free_md_stats(struct obd_device *obd)
1678 {
1679         struct lprocfs_stats *stats = obd->obd_md_stats;
1680
1681         if (stats != NULL) {
1682                 obd->obd_md_stats = NULL;
1683                 obd->obd_md_cntr_base = 0;
1684                 lprocfs_free_stats(&stats);
1685         }
1686 }
1687 EXPORT_SYMBOL(lprocfs_free_md_stats);
1688
1689 void lprocfs_init_ldlm_stats(struct lprocfs_stats *ldlm_stats)
1690 {
1691         lprocfs_counter_init(ldlm_stats,
1692                              LDLM_ENQUEUE - LDLM_FIRST_OPC,
1693                              0, "ldlm_enqueue", "reqs");
1694         lprocfs_counter_init(ldlm_stats,
1695                              LDLM_CONVERT - LDLM_FIRST_OPC,
1696                              0, "ldlm_convert", "reqs");
1697         lprocfs_counter_init(ldlm_stats,
1698                              LDLM_CANCEL - LDLM_FIRST_OPC,
1699                              0, "ldlm_cancel", "reqs");
1700         lprocfs_counter_init(ldlm_stats,
1701                              LDLM_BL_CALLBACK - LDLM_FIRST_OPC,
1702                              0, "ldlm_bl_callback", "reqs");
1703         lprocfs_counter_init(ldlm_stats,
1704                              LDLM_CP_CALLBACK - LDLM_FIRST_OPC,
1705                              0, "ldlm_cp_callback", "reqs");
1706         lprocfs_counter_init(ldlm_stats,
1707                              LDLM_GL_CALLBACK - LDLM_FIRST_OPC,
1708                              0, "ldlm_gl_callback", "reqs");
1709 }
1710 EXPORT_SYMBOL(lprocfs_init_ldlm_stats);
1711
1712 __s64 lprocfs_read_helper(struct lprocfs_counter *lc,
1713                           struct lprocfs_counter_header *header,
1714                           enum lprocfs_stats_flags flags,
1715                           enum lprocfs_fields_flags field)
1716 {
1717         __s64 ret = 0;
1718
1719         if (lc == NULL || header == NULL)
1720                 RETURN(0);
1721
1722         switch (field) {
1723                 case LPROCFS_FIELDS_FLAGS_CONFIG:
1724                         ret = header->lc_config;
1725                         break;
1726                 case LPROCFS_FIELDS_FLAGS_SUM:
1727                         ret = lc->lc_sum;
1728                         if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1729                                 ret += lc->lc_sum_irq;
1730                         break;
1731                 case LPROCFS_FIELDS_FLAGS_MIN:
1732                         ret = lc->lc_min;
1733                         break;
1734                 case LPROCFS_FIELDS_FLAGS_MAX:
1735                         ret = lc->lc_max;
1736                         break;
1737                 case LPROCFS_FIELDS_FLAGS_AVG:
1738                         ret = (lc->lc_max - lc->lc_min) / 2;
1739                         break;
1740                 case LPROCFS_FIELDS_FLAGS_SUMSQUARE:
1741                         ret = lc->lc_sumsquare;
1742                         break;
1743                 case LPROCFS_FIELDS_FLAGS_COUNT:
1744                         ret = lc->lc_count;
1745                         break;
1746                 default:
1747                         break;
1748         };
1749         RETURN(ret);
1750 }
1751 EXPORT_SYMBOL(lprocfs_read_helper);
1752
1753 int lprocfs_write_helper(const char *buffer, unsigned long count,
1754                          int *val)
1755 {
1756         return lprocfs_write_frac_helper(buffer, count, val, 1);
1757 }
1758 EXPORT_SYMBOL(lprocfs_write_helper);
1759
1760 int lprocfs_write_frac_helper(const char *buffer, unsigned long count,
1761                               int *val, int mult)
1762 {
1763         char kernbuf[20], *end, *pbuf;
1764
1765         if (count > (sizeof(kernbuf) - 1))
1766                 return -EINVAL;
1767
1768         if (copy_from_user(kernbuf, buffer, count))
1769                 return -EFAULT;
1770
1771         kernbuf[count] = '\0';
1772         pbuf = kernbuf;
1773         if (*pbuf == '-') {
1774                 mult = -mult;
1775                 pbuf++;
1776         }
1777
1778         *val = (int)simple_strtoul(pbuf, &end, 10) * mult;
1779         if (pbuf == end)
1780                 return -EINVAL;
1781
1782         if (end != NULL && *end == '.') {
1783                 int temp_val, pow = 1;
1784                 int i;
1785
1786                 pbuf = end + 1;
1787                 if (strlen(pbuf) > 5)
1788                         pbuf[5] = '\0'; /*only allow 5bits fractional*/
1789
1790                 temp_val = (int)simple_strtoul(pbuf, &end, 10) * mult;
1791
1792                 if (pbuf < end) {
1793                         for (i = 0; i < (end - pbuf); i++)
1794                                 pow *= 10;
1795
1796                         *val += temp_val / pow;
1797                 }
1798         }
1799         return 0;
1800 }
1801 EXPORT_SYMBOL(lprocfs_write_frac_helper);
1802
1803 int lprocfs_read_frac_helper(char *buffer, unsigned long count, long val,
1804                              int mult)
1805 {
1806         long decimal_val, frac_val;
1807         int prtn;
1808
1809         if (count < 10)
1810                 return -EINVAL;
1811
1812         decimal_val = val / mult;
1813         prtn = snprintf(buffer, count, "%ld", decimal_val);
1814         frac_val = val % mult;
1815
1816         if (prtn < (count - 4) && frac_val > 0) {
1817                 long temp_frac;
1818                 int i, temp_mult = 1, frac_bits = 0;
1819
1820                 temp_frac = frac_val * 10;
1821                 buffer[prtn++] = '.';
1822                 while (frac_bits < 2 && (temp_frac / mult) < 1 ) {
1823                         /* only reserved 2 bits fraction */
1824                         buffer[prtn++] ='0';
1825                         temp_frac *= 10;
1826                         frac_bits++;
1827                 }
1828                 /*
1829                  * Need to think these cases :
1830                  *      1. #echo x.00 > /proc/xxx       output result : x
1831                  *      2. #echo x.0x > /proc/xxx       output result : x.0x
1832                  *      3. #echo x.x0 > /proc/xxx       output result : x.x
1833                  *      4. #echo x.xx > /proc/xxx       output result : x.xx
1834                  *      Only reserved 2 bits fraction.
1835                  */
1836                 for (i = 0; i < (5 - prtn); i++)
1837                         temp_mult *= 10;
1838
1839                 frac_bits = min((int)count - prtn, 3 - frac_bits);
1840                 prtn += snprintf(buffer + prtn, frac_bits, "%ld",
1841                                  frac_val * temp_mult / mult);
1842
1843                 prtn--;
1844                 while(buffer[prtn] < '1' || buffer[prtn] > '9') {
1845                         prtn--;
1846                         if (buffer[prtn] == '.') {
1847                                 prtn--;
1848                                 break;
1849                         }
1850                 }
1851                 prtn++;
1852         }
1853         buffer[prtn++] ='\n';
1854         return prtn;
1855 }
1856 EXPORT_SYMBOL(lprocfs_read_frac_helper);
1857
1858 int lprocfs_seq_read_frac_helper(struct seq_file *m, long val, int mult)
1859 {
1860         long decimal_val, frac_val;
1861
1862         decimal_val = val / mult;
1863         seq_printf(m, "%ld", decimal_val);
1864         frac_val = val % mult;
1865
1866         if (frac_val > 0) {
1867                 frac_val *= 100;
1868                 frac_val /= mult;
1869         }
1870         if (frac_val > 0) {
1871                 /* Three cases: x0, xx, 0x */
1872                 if ((frac_val % 10) != 0)
1873                         seq_printf(m, ".%ld", frac_val);
1874                 else
1875                         seq_printf(m, ".%ld", frac_val / 10);
1876         }
1877
1878         seq_printf(m, "\n");
1879         return 0;
1880 }
1881 EXPORT_SYMBOL(lprocfs_seq_read_frac_helper);
1882
1883 int lprocfs_write_u64_helper(const char *buffer, unsigned long count,__u64 *val)
1884 {
1885         return lprocfs_write_frac_u64_helper(buffer, count, val, 1);
1886 }
1887 EXPORT_SYMBOL(lprocfs_write_u64_helper);
1888
1889 int lprocfs_write_frac_u64_helper(const char *buffer, unsigned long count,
1890                               __u64 *val, int mult)
1891 {
1892         char kernbuf[22], *end, *pbuf;
1893         __u64 whole, frac = 0, units;
1894         unsigned frac_d = 1;
1895
1896         if (count > (sizeof(kernbuf) - 1))
1897                 return -EINVAL;
1898
1899         if (copy_from_user(kernbuf, buffer, count))
1900                 return -EFAULT;
1901
1902         kernbuf[count] = '\0';
1903         pbuf = kernbuf;
1904         if (*pbuf == '-') {
1905                 mult = -mult;
1906                 pbuf++;
1907         }
1908
1909         whole = simple_strtoull(pbuf, &end, 10);
1910         if (pbuf == end)
1911                 return -EINVAL;
1912
1913         if (end != NULL && *end == '.') {
1914                 int i;
1915                 pbuf = end + 1;
1916
1917                 /* need to limit frac_d to a __u32 */
1918                 if (strlen(pbuf) > 10)
1919                         pbuf[10] = '\0';
1920
1921                 frac = simple_strtoull(pbuf, &end, 10);
1922                 /* count decimal places */
1923                 for (i = 0; i < (end - pbuf); i++)
1924                         frac_d *= 10;
1925         }
1926
1927         units = 1;
1928         if (end != NULL) {
1929                 switch (*end) {
1930                 case 'p': case 'P':
1931                         units <<= 10;
1932                 case 't': case 'T':
1933                         units <<= 10;
1934                 case 'g': case 'G':
1935                         units <<= 10;
1936                 case 'm': case 'M':
1937                         units <<= 10;
1938                 case 'k': case 'K':
1939                         units <<= 10;
1940                 }
1941         }
1942         /* Specified units override the multiplier */
1943         if (units > 1)
1944                 mult = mult < 0 ? -units : units;
1945
1946         frac *= mult;
1947         do_div(frac, frac_d);
1948         *val = whole * mult + frac;
1949         return 0;
1950 }
1951 EXPORT_SYMBOL(lprocfs_write_frac_u64_helper);
1952
1953 static char *lprocfs_strnstr(const char *s1, const char *s2, size_t len)
1954 {
1955         size_t l2;
1956
1957         l2 = strlen(s2);
1958         if (!l2)
1959                 return (char *)s1;
1960         while (len >= l2) {
1961                 len--;
1962                 if (!memcmp(s1, s2, l2))
1963                         return (char *)s1;
1964                 s1++;
1965         }
1966         return NULL;
1967 }
1968
1969 /**
1970  * Find the string \a name in the input \a buffer, and return a pointer to the
1971  * value immediately following \a name, reducing \a count appropriately.
1972  * If \a name is not found the original \a buffer is returned.
1973  */
1974 char *lprocfs_find_named_value(const char *buffer, const char *name,
1975                                 size_t *count)
1976 {
1977         char *val;
1978         size_t buflen = *count;
1979
1980         /* there is no strnstr() in rhel5 and ubuntu kernels */
1981         val = lprocfs_strnstr(buffer, name, buflen);
1982         if (val == NULL)
1983                 return (char *)buffer;
1984
1985         val += strlen(name);                             /* skip prefix */
1986         while (val < buffer + buflen && isspace(*val)) /* skip separator */
1987                 val++;
1988
1989         *count = 0;
1990         while (val < buffer + buflen && isalnum(*val)) {
1991                 ++*count;
1992                 ++val;
1993         }
1994
1995         return val - *count;
1996 }
1997 EXPORT_SYMBOL(lprocfs_find_named_value);
1998
1999 int lprocfs_seq_create(struct proc_dir_entry *parent,
2000                        const char *name,
2001                        mode_t mode,
2002                        const struct file_operations *seq_fops,
2003                        void *data)
2004 {
2005         struct proc_dir_entry *entry;
2006         ENTRY;
2007
2008         /* Disallow secretly (un)writable entries. */
2009         LASSERT((seq_fops->write == NULL) == ((mode & 0222) == 0));
2010
2011         entry = proc_create_data(name, mode, parent, seq_fops, data);
2012
2013         if (entry == NULL)
2014                 RETURN(-ENOMEM);
2015
2016         RETURN(0);
2017 }
2018 EXPORT_SYMBOL(lprocfs_seq_create);
2019
2020 int lprocfs_obd_seq_create(struct obd_device *dev,
2021                            const char *name,
2022                            mode_t mode,
2023                            const struct file_operations *seq_fops,
2024                            void *data)
2025 {
2026         return (lprocfs_seq_create(dev->obd_proc_entry, name,
2027                                    mode, seq_fops, data));
2028 }
2029 EXPORT_SYMBOL(lprocfs_obd_seq_create);
2030
2031 void lprocfs_oh_tally(struct obd_histogram *oh, unsigned int value)
2032 {
2033         if (value >= OBD_HIST_MAX)
2034                 value = OBD_HIST_MAX - 1;
2035
2036         spin_lock(&oh->oh_lock);
2037         oh->oh_buckets[value]++;
2038         spin_unlock(&oh->oh_lock);
2039 }
2040 EXPORT_SYMBOL(lprocfs_oh_tally);
2041
2042 void lprocfs_oh_tally_log2(struct obd_histogram *oh, unsigned int value)
2043 {
2044         unsigned int val = 0;
2045
2046         if (likely(value != 0))
2047                 val = min(fls(value - 1), OBD_HIST_MAX);
2048
2049         lprocfs_oh_tally(oh, val);
2050 }
2051 EXPORT_SYMBOL(lprocfs_oh_tally_log2);
2052
2053 unsigned long lprocfs_oh_sum(struct obd_histogram *oh)
2054 {
2055         unsigned long ret = 0;
2056         int i;
2057
2058         for (i = 0; i < OBD_HIST_MAX; i++)
2059                 ret +=  oh->oh_buckets[i];
2060         return ret;
2061 }
2062 EXPORT_SYMBOL(lprocfs_oh_sum);
2063
2064 void lprocfs_oh_clear(struct obd_histogram *oh)
2065 {
2066         spin_lock(&oh->oh_lock);
2067         memset(oh->oh_buckets, 0, sizeof(oh->oh_buckets));
2068         spin_unlock(&oh->oh_lock);
2069 }
2070 EXPORT_SYMBOL(lprocfs_oh_clear);
2071
2072 int lprocfs_obd_rd_max_pages_per_rpc(char *page, char **start, off_t off,
2073                                      int count, int *eof, void *data)
2074 {
2075         struct obd_device *dev = data;
2076         struct client_obd *cli = &dev->u.cli;
2077         int rc;
2078
2079         client_obd_list_lock(&cli->cl_loi_list_lock);
2080         rc = snprintf(page, count, "%d\n", cli->cl_max_pages_per_rpc);
2081         client_obd_list_unlock(&cli->cl_loi_list_lock);
2082         return rc;
2083 }
2084 EXPORT_SYMBOL(lprocfs_obd_rd_max_pages_per_rpc);
2085
2086 int lprocfs_obd_max_pages_per_rpc_seq_show(struct seq_file *m, void *data)
2087 {
2088         struct obd_device *dev = data;
2089         struct client_obd *cli = &dev->u.cli;
2090         int rc;
2091
2092         client_obd_list_lock(&cli->cl_loi_list_lock);
2093         rc = seq_printf(m, "%d\n", cli->cl_max_pages_per_rpc);
2094         client_obd_list_unlock(&cli->cl_loi_list_lock);
2095         return rc;
2096 }
2097 EXPORT_SYMBOL(lprocfs_obd_max_pages_per_rpc_seq_show);
2098
2099 int lprocfs_wr_root_squash(const char __user *buffer, unsigned long count,
2100                            struct root_squash_info *squash, char *name)
2101 {
2102         int rc;
2103         char kernbuf[64], *tmp, *errmsg;
2104         unsigned long uid, gid;
2105         ENTRY;
2106
2107         if (count >= sizeof(kernbuf)) {
2108                 errmsg = "string too long";
2109                 GOTO(failed_noprint, rc = -EINVAL);
2110         }
2111         if (copy_from_user(kernbuf, buffer, count)) {
2112                 errmsg = "bad address";
2113                 GOTO(failed_noprint, rc = -EFAULT);
2114         }
2115         kernbuf[count] = '\0';
2116
2117         /* look for uid gid separator */
2118         tmp = strchr(kernbuf, ':');
2119         if (tmp == NULL) {
2120                 errmsg = "needs uid:gid format";
2121                 GOTO(failed, rc = -EINVAL);
2122         }
2123         *tmp = '\0';
2124         tmp++;
2125
2126         /* parse uid */
2127         if (kstrtoul(kernbuf, 0, &uid) != 0) {
2128                 errmsg = "bad uid";
2129                 GOTO(failed, rc = -EINVAL);
2130         }
2131
2132         /* parse gid */
2133         if (kstrtoul(tmp, 0, &gid) != 0) {
2134                 errmsg = "bad gid";
2135                 GOTO(failed, rc = -EINVAL);
2136         }
2137
2138         squash->rsi_uid = uid;
2139         squash->rsi_gid = gid;
2140
2141         LCONSOLE_INFO("%s: root_squash is set to %u:%u\n",
2142                       name, squash->rsi_uid, squash->rsi_gid);
2143         RETURN(count);
2144
2145 failed:
2146         if (tmp != NULL) {
2147                 tmp--;
2148                 *tmp = ':';
2149         }
2150         CWARN("%s: failed to set root_squash to \"%s\", %s, rc = %d\n",
2151               name, kernbuf, errmsg, rc);
2152         RETURN(rc);
2153 failed_noprint:
2154         CWARN("%s: failed to set root_squash due to %s, rc = %d\n",
2155               name, errmsg, rc);
2156         RETURN(rc);
2157 }
2158 EXPORT_SYMBOL(lprocfs_wr_root_squash);
2159
2160
2161 int lprocfs_wr_nosquash_nids(const char __user *buffer, unsigned long count,
2162                              struct root_squash_info *squash, char *name)
2163 {
2164         int rc;
2165         char *kernbuf = NULL;
2166         char *errmsg;
2167         struct list_head tmp;
2168         ENTRY;
2169
2170         if (count > 4096) {
2171                 errmsg = "string too long";
2172                 GOTO(failed, rc = -EINVAL);
2173         }
2174
2175         OBD_ALLOC(kernbuf, count + 1);
2176         if (kernbuf == NULL) {
2177                 errmsg = "no memory";
2178                 GOTO(failed, rc = -ENOMEM);
2179         }
2180         if (copy_from_user(kernbuf, buffer, count)) {
2181                 errmsg = "bad address";
2182                 GOTO(failed, rc = -EFAULT);
2183         }
2184         kernbuf[count] = '\0';
2185
2186         if (count > 0 && kernbuf[count - 1] == '\n')
2187                 kernbuf[count - 1] = '\0';
2188
2189         if (strcmp(kernbuf, "NONE") == 0 || strcmp(kernbuf, "clear") == 0) {
2190                 /* empty string is special case */
2191                 down_write(&squash->rsi_sem);
2192                 if (!list_empty(&squash->rsi_nosquash_nids))
2193                         cfs_free_nidlist(&squash->rsi_nosquash_nids);
2194                 up_write(&squash->rsi_sem);
2195                 LCONSOLE_INFO("%s: nosquash_nids is cleared\n", name);
2196                 OBD_FREE(kernbuf, count + 1);
2197                 RETURN(count);
2198         }
2199
2200         INIT_LIST_HEAD(&tmp);
2201         if (cfs_parse_nidlist(kernbuf, count, &tmp) <= 0) {
2202                 errmsg = "can't parse";
2203                 GOTO(failed, rc = -EINVAL);
2204         }
2205         LCONSOLE_INFO("%s: nosquash_nids set to %s\n",
2206                       name, kernbuf);
2207         OBD_FREE(kernbuf, count + 1);
2208         kernbuf = NULL;
2209
2210         down_write(&squash->rsi_sem);
2211         if (!list_empty(&squash->rsi_nosquash_nids))
2212                 cfs_free_nidlist(&squash->rsi_nosquash_nids);
2213         list_splice(&tmp, &squash->rsi_nosquash_nids);
2214         up_write(&squash->rsi_sem);
2215
2216         RETURN(count);
2217
2218 failed:
2219         if (kernbuf) {
2220                 CWARN("%s: failed to set nosquash_nids to \"%s\", %s rc = %d\n",
2221                       name, kernbuf, errmsg, rc);
2222                 OBD_FREE(kernbuf, count + 1);
2223         } else {
2224                 CWARN("%s: failed to set nosquash_nids due to %s rc = %d\n",
2225                       name, errmsg, rc);
2226         }
2227         RETURN(rc);
2228 }
2229 EXPORT_SYMBOL(lprocfs_wr_nosquash_nids);
2230
2231 #endif /* LPROCFS*/