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