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