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