Whamcloud - gitweb
LU-8066 osp: migrate from proc to sysfs
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/obdclass/lprocfs_status.c
33  *
34  * Author: Hariharan Thantry <thantry@users.sourceforge.net>
35  */
36
37 #define DEBUG_SUBSYSTEM S_CLASS
38
39 #include <obd_class.h>
40 #include <lprocfs_status.h>
41
42 #ifdef CONFIG_PROC_FS
43
44 static int lprocfs_no_percpu_stats = 0;
45 module_param(lprocfs_no_percpu_stats, int, 0644);
46 MODULE_PARM_DESC(lprocfs_no_percpu_stats, "Do not alloc percpu data for lprocfs stats");
47
48 #define MAX_STRING_SIZE 128
49
50 int lprocfs_single_release(struct inode *inode, struct file *file)
51 {
52         return single_release(inode, file);
53 }
54 EXPORT_SYMBOL(lprocfs_single_release);
55
56 int lprocfs_seq_release(struct inode *inode, struct file *file)
57 {
58         return seq_release(inode, file);
59 }
60 EXPORT_SYMBOL(lprocfs_seq_release);
61
62 struct dentry *ldebugfs_add_simple(struct dentry *root,
63                                    char *name, void *data,
64                                    const struct file_operations *fops)
65 {
66         struct dentry *entry;
67         umode_t mode = 0;
68
69         if (!root || !name || !fops)
70                 return ERR_PTR(-EINVAL);
71
72         if (fops->read)
73                 mode = 0444;
74         if (fops->write)
75                 mode |= 0200;
76         entry = debugfs_create_file(name, mode, root, data, fops);
77         if (IS_ERR_OR_NULL(entry)) {
78                 CERROR("LprocFS: No memory to create <debugfs> entry %s", name);
79                 return entry ?: ERR_PTR(-ENOMEM);
80         }
81         return entry;
82 }
83 EXPORT_SYMBOL(ldebugfs_add_simple);
84
85 struct proc_dir_entry *
86 lprocfs_add_simple(struct proc_dir_entry *root, char *name,
87                    void *data, const struct file_operations *fops)
88 {
89         struct proc_dir_entry *proc;
90         mode_t mode = 0;
91
92         if (root == NULL || name == NULL || fops == NULL)
93                 return ERR_PTR(-EINVAL);
94
95         if (fops->read)
96                 mode = 0444;
97         if (fops->write)
98                 mode |= 0200;
99         proc = proc_create_data(name, mode, root, fops, data);
100         if (!proc) {
101                 CERROR("LprocFS: No memory to create /proc entry %s\n",
102                        name);
103                 return ERR_PTR(-ENOMEM);
104         }
105         return proc;
106 }
107 EXPORT_SYMBOL(lprocfs_add_simple);
108
109 struct proc_dir_entry *lprocfs_add_symlink(const char *name,
110                         struct proc_dir_entry *parent, const char *format, ...)
111 {
112         struct proc_dir_entry *entry;
113         char *dest;
114         va_list ap;
115
116         if (parent == NULL || format == NULL)
117                 return NULL;
118
119         OBD_ALLOC_WAIT(dest, MAX_STRING_SIZE + 1);
120         if (dest == NULL)
121                 return NULL;
122
123         va_start(ap, format);
124         vsnprintf(dest, MAX_STRING_SIZE, format, ap);
125         va_end(ap);
126
127         entry = proc_symlink(name, parent, dest);
128         if (entry == NULL)
129                 CERROR("LprocFS: Could not create symbolic link from "
130                        "%s to %s\n", name, dest);
131
132         OBD_FREE(dest, MAX_STRING_SIZE + 1);
133         return entry;
134 }
135 EXPORT_SYMBOL(lprocfs_add_symlink);
136
137 static const struct file_operations lprocfs_generic_fops = { };
138
139 int ldebugfs_add_vars(struct dentry *parent, struct lprocfs_vars *list,
140                       void *data)
141 {
142         if (IS_ERR_OR_NULL(parent) || IS_ERR_OR_NULL(list))
143                 return -EINVAL;
144
145         while (list->name) {
146                 struct dentry *entry;
147                 umode_t mode = 0;
148
149                 if (list->proc_mode != 0000) {
150                         mode = list->proc_mode;
151                 } else if (list->fops) {
152                         if (list->fops->read)
153                                 mode = 0444;
154                         if (list->fops->write)
155                                 mode |= 0200;
156                 }
157                 entry = debugfs_create_file(list->name, mode, parent,
158                                             list->data ? : data,
159                                             list->fops ? : &lprocfs_generic_fops);
160                 if (IS_ERR_OR_NULL(entry))
161                         return entry ? PTR_ERR(entry) : -ENOMEM;
162                 list++;
163         }
164         return 0;
165 }
166 EXPORT_SYMBOL_GPL(ldebugfs_add_vars);
167
168 /**
169  * Add /proc entries.
170  *
171  * \param root [in]  The parent proc entry on which new entry will be added.
172  * \param list [in]  Array of proc entries to be added.
173  * \param data [in]  The argument to be passed when entries read/write routines
174  *                   are called through /proc file.
175  *
176  * \retval 0   on success
177  *         < 0 on error
178  */
179 int
180 lprocfs_add_vars(struct proc_dir_entry *root, struct lprocfs_vars *list,
181                  void *data)
182 {
183         if (root == NULL || list == NULL)
184                 return -EINVAL;
185
186         while (list->name != NULL) {
187                 struct proc_dir_entry *proc;
188                 mode_t mode = 0;
189
190                 if (list->proc_mode != 0000) {
191                         mode = list->proc_mode;
192                 } else if (list->fops) {
193                         if (list->fops->read)
194                                 mode = 0444;
195                         if (list->fops->write)
196                                 mode |= 0200;
197                 }
198                 proc = proc_create_data(list->name, mode, root,
199                                         list->fops ?: &lprocfs_generic_fops,
200                                         list->data ?: data);
201                 if (proc == NULL)
202                         return -ENOMEM;
203                 list++;
204         }
205         return 0;
206 }
207 EXPORT_SYMBOL(lprocfs_add_vars);
208
209 void ldebugfs_remove(struct dentry **entryp)
210 {
211         debugfs_remove_recursive(*entryp);
212         *entryp = NULL;
213 }
214 EXPORT_SYMBOL_GPL(ldebugfs_remove);
215
216 #ifndef HAVE_REMOVE_PROC_SUBTREE
217 /* for b=10866, global variable */
218 DECLARE_RWSEM(_lprocfs_lock);
219 EXPORT_SYMBOL(_lprocfs_lock);
220
221 static void lprocfs_remove_nolock(struct proc_dir_entry **proot)
222 {
223         struct proc_dir_entry *root = *proot;
224         struct proc_dir_entry *temp = root;
225         struct proc_dir_entry *rm_entry;
226         struct proc_dir_entry *parent;
227
228         *proot = NULL;
229         if (root == NULL || IS_ERR(root))
230                 return;
231
232         parent = root->parent;
233         LASSERT(parent != NULL);
234
235         while (1) {
236                 while (temp->subdir != NULL)
237                         temp = temp->subdir;
238
239                 rm_entry = temp;
240                 temp = temp->parent;
241
242                 /* Memory corruption once caused this to fail, and
243                    without this LASSERT we would loop here forever. */
244                 LASSERTF(strlen(rm_entry->name) == rm_entry->namelen,
245                          "0x%p  %s/%s len %d\n", rm_entry, temp->name,
246                          rm_entry->name, (int)strlen(rm_entry->name));
247
248                 remove_proc_entry(rm_entry->name, temp);
249                 if (temp == parent)
250                         break;
251         }
252 }
253
254 int remove_proc_subtree(const char *name, struct proc_dir_entry *parent)
255 {
256         struct proc_dir_entry    *t = NULL;
257         struct proc_dir_entry   **p;
258         int                       len, busy = 0;
259
260         LASSERT(parent != NULL);
261         len = strlen(name);
262
263         down_write(&_lprocfs_lock);
264         /* lookup target name */
265         for (p = &parent->subdir; *p; p = &(*p)->next) {
266                 if ((*p)->namelen != len)
267                         continue;
268                 if (memcmp(name, (*p)->name, len))
269                         continue;
270                 t = *p;
271                 break;
272         }
273
274         if (t) {
275                 /* verify it's empty: do not count "num_refs" */
276                 for (p = &t->subdir; *p; p = &(*p)->next) {
277                         if ((*p)->namelen != strlen("num_refs")) {
278                                 busy = 1;
279                                 break;
280                         }
281                         if (memcmp("num_refs", (*p)->name,
282                                    strlen("num_refs"))) {
283                                 busy = 1;
284                                 break;
285                         }
286                 }
287         }
288
289         if (busy == 0)
290                 lprocfs_remove_nolock(&t);
291
292         up_write(&_lprocfs_lock);
293         return 0;
294 }
295 #endif /* !HAVE_REMOVE_PROC_SUBTREE */
296
297 #ifndef HAVE_PROC_REMOVE
298 void proc_remove(struct proc_dir_entry *de)
299 {
300 #ifndef HAVE_REMOVE_PROC_SUBTREE
301         down_write(&_lprocfs_lock); /* search vs remove race */
302         lprocfs_remove_nolock(&de);
303         up_write(&_lprocfs_lock);
304 #else
305         if (de)
306                 remove_proc_subtree(de->name, de->parent);
307 #endif
308 }
309 #endif
310
311 void lprocfs_remove(struct proc_dir_entry **rooth)
312 {
313         proc_remove(*rooth);
314         *rooth = NULL;
315 }
316 EXPORT_SYMBOL(lprocfs_remove);
317
318 void lprocfs_remove_proc_entry(const char *name, struct proc_dir_entry *parent)
319 {
320         LASSERT(parent != NULL);
321         remove_proc_entry(name, parent);
322 }
323 EXPORT_SYMBOL(lprocfs_remove_proc_entry);
324
325 struct dentry *ldebugfs_register(const char *name, struct dentry *parent,
326                                  struct lprocfs_vars *list, void *data)
327 {
328         struct dentry *entry;
329
330         entry = debugfs_create_dir(name, parent);
331         if (IS_ERR_OR_NULL(entry)) {
332                 entry = entry ?: ERR_PTR(-ENOMEM);
333                 goto out;
334         }
335
336         if (!IS_ERR_OR_NULL(list)) {
337                 int rc;
338
339                 rc = ldebugfs_add_vars(entry, list, data);
340                 if (rc) {
341                         debugfs_remove(entry);
342                         entry = ERR_PTR(rc);
343                 }
344         }
345 out:
346         return entry;
347 }
348 EXPORT_SYMBOL_GPL(ldebugfs_register);
349
350 struct proc_dir_entry *
351 lprocfs_register(const char *name, struct proc_dir_entry *parent,
352                  struct lprocfs_vars *list, void *data)
353 {
354         struct proc_dir_entry *newchild;
355
356         newchild = proc_mkdir(name, parent);
357         if (newchild == NULL)
358                 return ERR_PTR(-ENOMEM);
359
360         if (list != NULL) {
361                 int rc = lprocfs_add_vars(newchild, list, data);
362                 if (rc) {
363                         lprocfs_remove(&newchild);
364                         return ERR_PTR(rc);
365                 }
366         }
367         return newchild;
368 }
369 EXPORT_SYMBOL(lprocfs_register);
370
371 /* Generic callbacks */
372 int lprocfs_uuid_seq_show(struct seq_file *m, void *data)
373 {
374         struct obd_device *obd = data;
375
376         LASSERT(obd != NULL);
377         seq_printf(m, "%s\n", obd->obd_uuid.uuid);
378         return 0;
379 }
380 EXPORT_SYMBOL(lprocfs_uuid_seq_show);
381
382 static ssize_t uuid_show(struct kobject *kobj, struct attribute *attr,
383                          char *buf)
384 {
385         struct obd_device *obd = container_of(kobj, struct obd_device,
386                                               obd_kset.kobj);
387
388         return sprintf(buf, "%s\n", obd->obd_uuid.uuid);
389 }
390 LUSTRE_RO_ATTR(uuid);
391
392 int lprocfs_name_seq_show(struct seq_file *m, void *data)
393 {
394         struct obd_device *dev = data;
395
396         LASSERT(dev != NULL);
397         seq_printf(m, "%s\n", dev->obd_name);
398         return 0;
399 }
400 EXPORT_SYMBOL(lprocfs_name_seq_show);
401
402 static ssize_t blocksize_show(struct kobject *kobj, struct attribute *attr,
403                               char *buf)
404 {
405         struct obd_device *obd = container_of(kobj, struct obd_device,
406                                               obd_kset.kobj);
407         struct obd_statfs osfs;
408         int rc;
409
410         rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
411                         ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
412                         OBD_STATFS_NODELAY);
413         if (!rc)
414                 return sprintf(buf, "%u\n", osfs.os_bsize);
415
416         return rc;
417 }
418 LUSTRE_RO_ATTR(blocksize);
419
420 static ssize_t kbytestotal_show(struct kobject *kobj, struct attribute *attr,
421                                 char *buf)
422 {
423         struct obd_device *obd = container_of(kobj, struct obd_device,
424                                               obd_kset.kobj);
425         struct obd_statfs osfs;
426         int rc;
427
428         rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
429                         ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
430                         OBD_STATFS_NODELAY);
431         if (!rc) {
432                 u32 blk_size = osfs.os_bsize >> 10;
433                 u64 result = osfs.os_blocks;
434
435                 while (blk_size >>= 1)
436                         result <<= 1;
437
438                 return sprintf(buf, "%llu\n", result);
439         }
440
441         return rc;
442 }
443 LUSTRE_RO_ATTR(kbytestotal);
444
445 static ssize_t kbytesfree_show(struct kobject *kobj, struct attribute *attr,
446                                char *buf)
447 {
448         struct obd_device *obd = container_of(kobj, struct obd_device,
449                                               obd_kset.kobj);
450         struct obd_statfs osfs;
451         int rc;
452
453         rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
454                         ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
455                         OBD_STATFS_NODELAY);
456         if (!rc) {
457                 u32 blk_size = osfs.os_bsize >> 10;
458                 u64 result = osfs.os_bfree;
459
460                 while (blk_size >>= 1)
461                         result <<= 1;
462
463                 return sprintf(buf, "%llu\n", result);
464         }
465
466         return rc;
467 }
468 LUSTRE_RO_ATTR(kbytesfree);
469
470 static ssize_t kbytesavail_show(struct kobject *kobj, struct attribute *attr,
471                                 char *buf)
472 {
473         struct obd_device *obd = container_of(kobj, struct obd_device,
474                                               obd_kset.kobj);
475         struct obd_statfs osfs;
476         int rc;
477
478         rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
479                         ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
480                         OBD_STATFS_NODELAY);
481         if (!rc) {
482                 u32 blk_size = osfs.os_bsize >> 10;
483                 u64 result = osfs.os_bavail;
484
485                 while (blk_size >>= 1)
486                         result <<= 1;
487
488                 return sprintf(buf, "%llu\n", result);
489         }
490
491         return rc;
492 }
493 LUSTRE_RO_ATTR(kbytesavail);
494
495 static ssize_t filestotal_show(struct kobject *kobj, struct attribute *attr,
496                                char *buf)
497 {
498         struct obd_device *obd = container_of(kobj, struct obd_device,
499                                               obd_kset.kobj);
500         struct obd_statfs osfs;
501         int rc;
502
503         rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
504                         ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
505                         OBD_STATFS_NODELAY);
506         if (!rc)
507                 return sprintf(buf, "%llu\n", osfs.os_files);
508
509         return rc;
510 }
511 LUSTRE_RO_ATTR(filestotal);
512
513 static ssize_t filesfree_show(struct kobject *kobj, struct attribute *attr,
514                               char *buf)
515 {
516         struct obd_device *obd = container_of(kobj, struct obd_device,
517                                               obd_kset.kobj);
518         struct obd_statfs osfs;
519         int rc;
520
521         rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
522                         ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
523                         OBD_STATFS_NODELAY);
524         if (!rc)
525                 return sprintf(buf, "%llu\n", osfs.os_ffree);
526
527         return rc;
528 }
529 LUSTRE_RO_ATTR(filesfree);
530
531 ssize_t conn_uuid_show(struct kobject *kobj, struct attribute *attr, char *buf)
532 {
533         struct obd_device *obd = container_of(kobj, struct obd_device,
534                                               obd_kset.kobj);
535         struct ptlrpc_connection *conn;
536         ssize_t count;
537
538         LPROCFS_CLIMP_CHECK(obd);
539         conn = obd->u.cli.cl_import->imp_connection;
540         if (conn && obd->u.cli.cl_import)
541                 count = sprintf(buf, "%s\n", conn->c_remote_uuid.uuid);
542         else
543                 count = sprintf(buf, "%s\n", "<none>");
544
545         LPROCFS_CLIMP_EXIT(obd);
546         return count;
547 }
548 EXPORT_SYMBOL(conn_uuid_show);
549
550 int lprocfs_server_uuid_seq_show(struct seq_file *m, void *data)
551 {
552         struct obd_device *obd = data;
553         struct obd_import *imp;
554         char *imp_state_name = NULL;
555         int rc = 0;
556
557         LASSERT(obd != NULL);
558         LPROCFS_CLIMP_CHECK(obd);
559         imp = obd->u.cli.cl_import;
560         imp_state_name = ptlrpc_import_state_name(imp->imp_state);
561         seq_printf(m, "%s\t%s%s\n", obd2cli_tgt(obd), imp_state_name,
562                    imp->imp_deactive ? "\tDEACTIVATED" : "");
563
564         LPROCFS_CLIMP_EXIT(obd);
565         return rc;
566 }
567 EXPORT_SYMBOL(lprocfs_server_uuid_seq_show);
568
569 int lprocfs_conn_uuid_seq_show(struct seq_file *m, void *data)
570 {
571         struct obd_device *obd = data;
572         struct ptlrpc_connection *conn;
573         int rc = 0;
574
575         LASSERT(obd != NULL);
576
577         LPROCFS_CLIMP_CHECK(obd);
578         conn = obd->u.cli.cl_import->imp_connection;
579         if (conn && obd->u.cli.cl_import)
580                 seq_printf(m, "%s\n", conn->c_remote_uuid.uuid);
581         else
582                 seq_printf(m, "%s\n", "<none>");
583
584         LPROCFS_CLIMP_EXIT(obd);
585         return rc;
586 }
587 EXPORT_SYMBOL(lprocfs_conn_uuid_seq_show);
588
589 /** add up per-cpu counters */
590
591 /**
592  * Lock statistics structure for access, possibly only on this CPU.
593  *
594  * The statistics struct may be allocated with per-CPU structures for
595  * efficient concurrent update (usually only on server-wide stats), or
596  * as a single global struct (e.g. for per-client or per-job statistics),
597  * so the required locking depends on the type of structure allocated.
598  *
599  * For per-CPU statistics, pin the thread to the current cpuid so that
600  * will only access the statistics for that CPU.  If the stats structure
601  * for the current CPU has not been allocated (or previously freed),
602  * allocate it now.  The per-CPU statistics do not need locking since
603  * the thread is pinned to the CPU during update.
604  *
605  * For global statistics, lock the stats structure to prevent concurrent update.
606  *
607  * \param[in] stats     statistics structure to lock
608  * \param[in] opc       type of operation:
609  *                      LPROCFS_GET_SMP_ID: "lock" and return current CPU index
610  *                              for incrementing statistics for that CPU
611  *                      LPROCFS_GET_NUM_CPU: "lock" and return number of used
612  *                              CPU indices to iterate over all indices
613  * \param[out] flags    CPU interrupt saved state for IRQ-safe locking
614  *
615  * \retval cpuid of current thread or number of allocated structs
616  * \retval negative on error (only for opc LPROCFS_GET_SMP_ID + per-CPU stats)
617  */
618 int lprocfs_stats_lock(struct lprocfs_stats *stats,
619                        enum lprocfs_stats_lock_ops opc,
620                        unsigned long *flags)
621 {
622         if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) {
623                 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
624                         spin_lock_irqsave(&stats->ls_lock, *flags);
625                 else
626                         spin_lock(&stats->ls_lock);
627                 return opc == LPROCFS_GET_NUM_CPU ? 1 : 0;
628         }
629
630         switch (opc) {
631         case LPROCFS_GET_SMP_ID: {
632                 unsigned int cpuid = get_cpu();
633
634                 if (unlikely(!stats->ls_percpu[cpuid])) {
635                         int rc = lprocfs_stats_alloc_one(stats, cpuid);
636
637                         if (rc < 0) {
638                                 put_cpu();
639                                 return rc;
640                         }
641                 }
642                 return cpuid;
643         }
644         case LPROCFS_GET_NUM_CPU:
645                 return stats->ls_biggest_alloc_num;
646         default:
647                 LBUG();
648         }
649 }
650
651 /**
652  * Unlock statistics structure after access.
653  *
654  * Unlock the lock acquired via lprocfs_stats_lock() for global statistics,
655  * or unpin this thread from the current cpuid for per-CPU statistics.
656  *
657  * This function must be called using the same arguments as used when calling
658  * lprocfs_stats_lock() so that the correct operation can be performed.
659  *
660  * \param[in] stats     statistics structure to unlock
661  * \param[in] opc       type of operation (current cpuid or number of structs)
662  * \param[in] flags     CPU interrupt saved state for IRQ-safe locking
663  */
664 void lprocfs_stats_unlock(struct lprocfs_stats *stats,
665                           enum lprocfs_stats_lock_ops opc,
666                           unsigned long *flags)
667 {
668         if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) {
669                 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
670                         spin_unlock_irqrestore(&stats->ls_lock, *flags);
671                 else
672                         spin_unlock(&stats->ls_lock);
673         } else if (opc == LPROCFS_GET_SMP_ID) {
674                 put_cpu();
675         }
676 }
677
678 /** add up per-cpu counters */
679 void lprocfs_stats_collect(struct lprocfs_stats *stats, int idx,
680                            struct lprocfs_counter *cnt)
681 {
682         unsigned int                    num_entry;
683         struct lprocfs_counter          *percpu_cntr;
684         int                             i;
685         unsigned long                   flags = 0;
686
687         memset(cnt, 0, sizeof(*cnt));
688
689         if (stats == NULL) {
690                 /* set count to 1 to avoid divide-by-zero errs in callers */
691                 cnt->lc_count = 1;
692                 return;
693         }
694
695         cnt->lc_min = LC_MIN_INIT;
696
697         num_entry = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
698
699         for (i = 0; i < num_entry; i++) {
700                 if (stats->ls_percpu[i] == NULL)
701                         continue;
702                 percpu_cntr = lprocfs_stats_counter_get(stats, i, idx);
703
704                 cnt->lc_count += percpu_cntr->lc_count;
705                 cnt->lc_sum += percpu_cntr->lc_sum;
706                 if (percpu_cntr->lc_min < cnt->lc_min)
707                         cnt->lc_min = percpu_cntr->lc_min;
708                 if (percpu_cntr->lc_max > cnt->lc_max)
709                         cnt->lc_max = percpu_cntr->lc_max;
710                 cnt->lc_sumsquare += percpu_cntr->lc_sumsquare;
711         }
712
713         lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
714 }
715
716 static void obd_import_flags2str(struct obd_import *imp, struct seq_file *m)
717 {
718         bool first = true;
719
720         if (imp->imp_obd->obd_no_recov) {
721                 seq_printf(m, "no_recov");
722                 first = false;
723         }
724
725         flag2str(imp, invalid);
726         flag2str(imp, deactive);
727         flag2str(imp, replayable);
728         flag2str(imp, delayed_recovery);
729         flag2str(imp, vbr_failed);
730         flag2str(imp, pingable);
731         flag2str(imp, resend_replay);
732         flag2str(imp, no_pinger_recover);
733         flag2str(imp, connect_tried);
734 }
735
736 static const char *obd_connect_names[] = {
737         /* flags names  */
738         "read_only",
739         "lov_index",
740         "connect_from_mds",
741         "write_grant",
742         "server_lock",
743         "version",
744         "request_portal",
745         "acl",
746         "xattr",
747         "create_on_write",
748         "truncate_lock",
749         "initial_transno",
750         "inode_bit_locks",
751         "barrier",
752         "getattr_by_fid",
753         "no_oh_for_devices",
754         "remote_client",
755         "remote_client_by_force",
756         "max_byte_per_rpc",
757         "64bit_qdata",
758         "mds_capability",
759         "oss_capability",
760         "early_lock_cancel",
761         "som",
762         "adaptive_timeouts",
763         "lru_resize",
764         "mds_mds_connection",
765         "real_conn",
766         "change_qunit_size",
767         "alt_checksum_algorithm",
768         "fid_is_enabled",
769         "version_recovery",
770         "pools",
771         "grant_shrink",
772         "skip_orphan",
773         "large_ea",
774         "full20",
775         "layout_lock",
776         "64bithash",
777         "object_max_bytes",
778         "imp_recov",
779         "jobstats",
780         "umask",
781         "einprogress",
782         "grant_param",
783         "flock_owner",
784         "lvb_type",
785         "nanoseconds_times",
786         "lightweight_conn",
787         "short_io",
788         "pingless",
789         "flock_deadlock",
790         "disp_stripe",
791         "open_by_fid",
792         "lfsck",
793         "unknown",
794         "unlink_close",
795         "multi_mod_rpcs",
796         "dir_stripe",
797         "subtree",
798         "lockahead",
799         "bulk_mbits",
800         "compact_obdo",
801         "second_flags",
802         /* flags2 names */
803         "file_secctx",  /* 0x01 */
804         "lockaheadv2",  /* 0x02 */
805         "dir_migrate",  /* 0x04 */
806         "unknown",      /* 0x08 */
807         "unknown",      /* 0x10 */
808         "flr",          /* 0x20 */
809         "wbc",          /* 0x40 */
810         "lock_convert",  /* 0x80 */
811         NULL
812 };
813
814 void obd_connect_seq_flags2str(struct seq_file *m, __u64 flags, __u64 flags2,
815                                const char *sep)
816 {
817         bool first = true;
818         __u64 mask;
819         int i;
820
821         for (i = 0, mask = 1; i < 64; i++, mask <<= 1) {
822                 if (flags & mask) {
823                         seq_printf(m, "%s%s",
824                                    first ? "" : sep, obd_connect_names[i]);
825                         first = false;
826                 }
827         }
828
829         if (flags & ~(mask - 1)) {
830                 seq_printf(m, "%sunknown_%#llx",
831                            first ? "" : sep, flags & ~(mask - 1));
832                 first = false;
833         }
834
835         if (!(flags & OBD_CONNECT_FLAGS2) || flags2 == 0)
836                 return;
837
838         for (i = 64, mask = 1; obd_connect_names[i] != NULL; i++, mask <<= 1) {
839                 if (flags2 & mask) {
840                         seq_printf(m, "%s%s",
841                                    first ? "" : sep, obd_connect_names[i]);
842                         first = false;
843                 }
844         }
845
846         if (flags2 & ~(mask - 1)) {
847                 seq_printf(m, "%sunknown2_%#llx",
848                            first ? "" : sep, flags2 & ~(mask - 1));
849                 first = false;
850         }
851 }
852 EXPORT_SYMBOL(obd_connect_seq_flags2str);
853
854 int obd_connect_flags2str(char *page, int count, __u64 flags, __u64 flags2,
855                           const char *sep)
856 {
857         __u64 mask;
858         int i, ret = 0;
859
860         for (i = 0, mask = 1; i < 64; i++, mask <<= 1) {
861                 if (flags & mask)
862                         ret += snprintf(page + ret, count - ret, "%s%s",
863                                         ret ? sep : "", obd_connect_names[i]);
864         }
865
866         if (flags & ~(mask - 1))
867                 ret += snprintf(page + ret, count - ret,
868                                 "%sunknown_%#llx",
869                                 ret ? sep : "", flags & ~(mask - 1));
870
871         if (!(flags & OBD_CONNECT_FLAGS2) || flags2 == 0)
872                 return ret;
873
874         for (i = 64, mask = 1; obd_connect_names[i] != NULL; i++, mask <<= 1) {
875                 if (flags2 & mask)
876                         ret += snprintf(page + ret, count - ret, "%s%s",
877                                         ret ? sep : "", obd_connect_names[i]);
878         }
879
880         if (flags2 & ~(mask - 1))
881                 ret += snprintf(page + ret, count - ret,
882                                 "%sunknown2_%#llx",
883                                 ret ? sep : "", flags2 & ~(mask - 1));
884
885         return ret;
886 }
887 EXPORT_SYMBOL(obd_connect_flags2str);
888
889 void
890 obd_connect_data_seqprint(struct seq_file *m, struct obd_connect_data *ocd)
891 {
892         __u64 flags;
893
894         LASSERT(ocd != NULL);
895         flags = ocd->ocd_connect_flags;
896
897         seq_printf(m, "    connect_data:\n"
898                    "       flags: %#llx\n"
899                    "       instance: %u\n",
900                    ocd->ocd_connect_flags,
901                    ocd->ocd_instance);
902         if (flags & OBD_CONNECT_VERSION)
903                 seq_printf(m, "       target_version: %u.%u.%u.%u\n",
904                            OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
905                            OBD_OCD_VERSION_MINOR(ocd->ocd_version),
906                            OBD_OCD_VERSION_PATCH(ocd->ocd_version),
907                            OBD_OCD_VERSION_FIX(ocd->ocd_version));
908         if (flags & OBD_CONNECT_MDS)
909                 seq_printf(m, "       mdt_index: %d\n", ocd->ocd_group);
910         if (flags & OBD_CONNECT_GRANT)
911                 seq_printf(m, "       initial_grant: %d\n", ocd->ocd_grant);
912         if (flags & OBD_CONNECT_INDEX)
913                 seq_printf(m, "       target_index: %u\n", ocd->ocd_index);
914         if (flags & OBD_CONNECT_BRW_SIZE)
915                 seq_printf(m, "       max_brw_size: %d\n", ocd->ocd_brw_size);
916         if (flags & OBD_CONNECT_IBITS)
917                 seq_printf(m, "       ibits_known: %#llx\n",
918                            ocd->ocd_ibits_known);
919         if (flags & OBD_CONNECT_GRANT_PARAM)
920                 seq_printf(m, "       grant_block_size: %d\n"
921                            "       grant_inode_size: %d\n"
922                            "       grant_max_extent_size: %d\n"
923                            "       grant_extent_tax: %d\n",
924                            1 << ocd->ocd_grant_blkbits,
925                            1 << ocd->ocd_grant_inobits,
926                            ocd->ocd_grant_max_blks << ocd->ocd_grant_blkbits,
927                            ocd->ocd_grant_tax_kb << 10);
928         if (flags & OBD_CONNECT_TRANSNO)
929                 seq_printf(m, "       first_transno: %#llx\n",
930                            ocd->ocd_transno);
931         if (flags & OBD_CONNECT_CKSUM)
932                 seq_printf(m, "       cksum_types: %#x\n",
933                            ocd->ocd_cksum_types);
934         if (flags & OBD_CONNECT_MAX_EASIZE)
935                 seq_printf(m, "       max_easize: %d\n", ocd->ocd_max_easize);
936         if (flags & OBD_CONNECT_MAXBYTES)
937                 seq_printf(m, "       max_object_bytes: %llu\n",
938                            ocd->ocd_maxbytes);
939         if (flags & OBD_CONNECT_MULTIMODRPCS)
940                 seq_printf(m, "       max_mod_rpcs: %hu\n",
941                            ocd->ocd_maxmodrpcs);
942 }
943
944 int lprocfs_import_seq_show(struct seq_file *m, void *data)
945 {
946         char                            nidstr[LNET_NIDSTR_SIZE];
947         struct lprocfs_counter          ret;
948         struct lprocfs_counter_header   *header;
949         struct obd_device               *obd    = (struct obd_device *)data;
950         struct obd_import               *imp;
951         struct obd_import_conn          *conn;
952         struct obd_connect_data         *ocd;
953         int                             j;
954         int                             k;
955         int                             rw      = 0;
956
957         LASSERT(obd != NULL);
958         LPROCFS_CLIMP_CHECK(obd);
959         imp = obd->u.cli.cl_import;
960         ocd = &imp->imp_connect_data;
961
962         seq_printf(m, "import:\n"
963                    "    name: %s\n"
964                    "    target: %s\n"
965                    "    state: %s\n"
966                    "    connect_flags: [ ",
967                    obd->obd_name,
968                    obd2cli_tgt(obd),
969                    ptlrpc_import_state_name(imp->imp_state));
970         obd_connect_seq_flags2str(m, imp->imp_connect_data.ocd_connect_flags,
971                                   imp->imp_connect_data.ocd_connect_flags2,
972                                   ", ");
973         seq_printf(m, " ]\n");
974         obd_connect_data_seqprint(m, ocd);
975         seq_printf(m, "    import_flags: [ ");
976         obd_import_flags2str(imp, m);
977
978         seq_printf(m, " ]\n"
979                    "    connection:\n"
980                    "       failover_nids: [ ");
981         spin_lock(&imp->imp_lock);
982         j = 0;
983         list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
984                 libcfs_nid2str_r(conn->oic_conn->c_peer.nid,
985                                  nidstr, sizeof(nidstr));
986                 seq_printf(m, "%s%s", j ? ", " : "", nidstr);
987                 j++;
988         }
989         if (imp->imp_connection != NULL)
990                 libcfs_nid2str_r(imp->imp_connection->c_peer.nid,
991                                  nidstr, sizeof(nidstr));
992         else
993                 strncpy(nidstr, "<none>", sizeof(nidstr));
994         seq_printf(m, " ]\n"
995                    "       current_connection: %s\n"
996                    "       connection_attempts: %u\n"
997                    "       generation: %u\n"
998                    "       in-progress_invalidations: %u\n",
999                    nidstr,
1000                    imp->imp_conn_cnt,
1001                    imp->imp_generation,
1002                    atomic_read(&imp->imp_inval_count));
1003         spin_unlock(&imp->imp_lock);
1004
1005         if (obd->obd_svc_stats == NULL)
1006                 goto out_climp;
1007
1008         header = &obd->obd_svc_stats->ls_cnt_header[PTLRPC_REQWAIT_CNTR];
1009         lprocfs_stats_collect(obd->obd_svc_stats, PTLRPC_REQWAIT_CNTR, &ret);
1010         if (ret.lc_count != 0) {
1011                 /* first argument to do_div MUST be __u64 */
1012                 __u64 sum = ret.lc_sum;
1013                 do_div(sum, ret.lc_count);
1014                 ret.lc_sum = sum;
1015         } else
1016                 ret.lc_sum = 0;
1017         seq_printf(m, "    rpcs:\n"
1018                    "       inflight: %u\n"
1019                    "       unregistering: %u\n"
1020                    "       timeouts: %u\n"
1021                    "       avg_waittime: %llu %s\n",
1022                    atomic_read(&imp->imp_inflight),
1023                    atomic_read(&imp->imp_unregistering),
1024                    atomic_read(&imp->imp_timeouts),
1025                    ret.lc_sum, header->lc_units);
1026
1027         k = 0;
1028         for(j = 0; j < IMP_AT_MAX_PORTALS; j++) {
1029                 if (imp->imp_at.iat_portal[j] == 0)
1030                         break;
1031                 k = max_t(unsigned int, k,
1032                           at_get(&imp->imp_at.iat_service_estimate[j]));
1033         }
1034         seq_printf(m, "    service_estimates:\n"
1035                    "       services: %u sec\n"
1036                    "       network: %u sec\n",
1037                    k,
1038                    at_get(&imp->imp_at.iat_net_latency));
1039
1040         seq_printf(m, "    transactions:\n"
1041                    "       last_replay: %llu\n"
1042                    "       peer_committed: %llu\n"
1043                    "       last_checked: %llu\n",
1044                    imp->imp_last_replay_transno,
1045                    imp->imp_peer_committed_transno,
1046                    imp->imp_last_transno_checked);
1047
1048         /* avg data rates */
1049         for (rw = 0; rw <= 1; rw++) {
1050                 lprocfs_stats_collect(obd->obd_svc_stats,
1051                                       PTLRPC_LAST_CNTR + BRW_READ_BYTES + rw,
1052                                       &ret);
1053                 if (ret.lc_sum > 0 && ret.lc_count > 0) {
1054                         /* first argument to do_div MUST be __u64 */
1055                         __u64 sum = ret.lc_sum;
1056                         do_div(sum, ret.lc_count);
1057                         ret.lc_sum = sum;
1058                         seq_printf(m, "    %s_data_averages:\n"
1059                                    "       bytes_per_rpc: %llu\n",
1060                                    rw ? "write" : "read",
1061                                    ret.lc_sum);
1062                 }
1063                 k = (int)ret.lc_sum;
1064                 j = opcode_offset(OST_READ + rw) + EXTRA_MAX_OPCODES;
1065                 header = &obd->obd_svc_stats->ls_cnt_header[j];
1066                 lprocfs_stats_collect(obd->obd_svc_stats, j, &ret);
1067                 if (ret.lc_sum > 0 && ret.lc_count != 0) {
1068                         /* first argument to do_div MUST be __u64 */
1069                         __u64 sum = ret.lc_sum;
1070                         do_div(sum, ret.lc_count);
1071                         ret.lc_sum = sum;
1072                         seq_printf(m, "       %s_per_rpc: %llu\n",
1073                                    header->lc_units, ret.lc_sum);
1074                         j = (int)ret.lc_sum;
1075                         if (j > 0)
1076                                 seq_printf(m, "       MB_per_sec: %u.%.02u\n",
1077                                            k / j, (100 * k / j) % 100);
1078                 }
1079         }
1080
1081 out_climp:
1082         LPROCFS_CLIMP_EXIT(obd);
1083         return 0;
1084 }
1085 EXPORT_SYMBOL(lprocfs_import_seq_show);
1086
1087 int lprocfs_state_seq_show(struct seq_file *m, void *data)
1088 {
1089         struct obd_device *obd = (struct obd_device *)data;
1090         struct obd_import *imp;
1091         int j, k;
1092
1093         LASSERT(obd != NULL);
1094         LPROCFS_CLIMP_CHECK(obd);
1095         imp = obd->u.cli.cl_import;
1096
1097         seq_printf(m, "current_state: %s\n",
1098                    ptlrpc_import_state_name(imp->imp_state));
1099         seq_printf(m, "state_history:\n");
1100         k = imp->imp_state_hist_idx;
1101         for (j = 0; j < IMP_STATE_HIST_LEN; j++) {
1102                 struct import_state_hist *ish =
1103                         &imp->imp_state_hist[(k + j) % IMP_STATE_HIST_LEN];
1104                 if (ish->ish_state == 0)
1105                         continue;
1106                 seq_printf(m, " - [ %lld, %s ]\n", (s64)ish->ish_time,
1107                            ptlrpc_import_state_name(ish->ish_state));
1108         }
1109
1110         LPROCFS_CLIMP_EXIT(obd);
1111         return 0;
1112 }
1113 EXPORT_SYMBOL(lprocfs_state_seq_show);
1114
1115 int lprocfs_at_hist_helper(struct seq_file *m, struct adaptive_timeout *at)
1116 {
1117         int i;
1118         for (i = 0; i < AT_BINS; i++)
1119                 seq_printf(m, "%3u ", at->at_hist[i]);
1120         seq_printf(m, "\n");
1121         return 0;
1122 }
1123 EXPORT_SYMBOL(lprocfs_at_hist_helper);
1124
1125 /* See also ptlrpc_lprocfs_timeouts_show_seq */
1126 int lprocfs_timeouts_seq_show(struct seq_file *m, void *data)
1127 {
1128         struct obd_device *obd = (struct obd_device *)data;
1129         struct obd_import *imp;
1130         unsigned int cur, worst;
1131         time64_t now, worstt;
1132         int i;
1133
1134         LASSERT(obd != NULL);
1135         LPROCFS_CLIMP_CHECK(obd);
1136         imp = obd->u.cli.cl_import;
1137
1138         now = ktime_get_real_seconds();
1139
1140         /* Some network health info for kicks */
1141         seq_printf(m, "%-10s : %lld, %llds ago\n",
1142                    "last reply", (s64)imp->imp_last_reply_time,
1143                    (s64)(now - imp->imp_last_reply_time));
1144
1145         cur = at_get(&imp->imp_at.iat_net_latency);
1146         worst = imp->imp_at.iat_net_latency.at_worst_ever;
1147         worstt = imp->imp_at.iat_net_latency.at_worst_time;
1148         seq_printf(m, "%-10s : cur %3u  worst %3u (at %lld, %llds ago) ",
1149                    "network", cur, worst, (s64)worstt, (s64)(now - worstt));
1150         lprocfs_at_hist_helper(m, &imp->imp_at.iat_net_latency);
1151
1152         for(i = 0; i < IMP_AT_MAX_PORTALS; i++) {
1153                 if (imp->imp_at.iat_portal[i] == 0)
1154                         break;
1155                 cur = at_get(&imp->imp_at.iat_service_estimate[i]);
1156                 worst = imp->imp_at.iat_service_estimate[i].at_worst_ever;
1157                 worstt = imp->imp_at.iat_service_estimate[i].at_worst_time;
1158                 seq_printf(m, "portal %-2d  : cur %3u  worst %3u (at %lld, %llds ago) ",
1159                            imp->imp_at.iat_portal[i], cur, worst, (s64)worstt,
1160                            (s64)(now - worstt));
1161                 lprocfs_at_hist_helper(m, &imp->imp_at.iat_service_estimate[i]);
1162         }
1163
1164         LPROCFS_CLIMP_EXIT(obd);
1165         return 0;
1166 }
1167 EXPORT_SYMBOL(lprocfs_timeouts_seq_show);
1168
1169 int lprocfs_connect_flags_seq_show(struct seq_file *m, void *data)
1170 {
1171         struct obd_device *obd = data;
1172         __u64 flags;
1173         __u64 flags2;
1174
1175         LPROCFS_CLIMP_CHECK(obd);
1176         flags = obd->u.cli.cl_import->imp_connect_data.ocd_connect_flags;
1177         flags2 = obd->u.cli.cl_import->imp_connect_data.ocd_connect_flags2;
1178         seq_printf(m, "flags=%#llx\n", flags);
1179         seq_printf(m, "flags2=%#llx\n", flags2);
1180         obd_connect_seq_flags2str(m, flags, flags2, "\n");
1181         seq_printf(m, "\n");
1182         LPROCFS_CLIMP_EXIT(obd);
1183         return 0;
1184 }
1185 EXPORT_SYMBOL(lprocfs_connect_flags_seq_show);
1186
1187 static const struct attribute *obd_def_uuid_attrs[] = {
1188         &lustre_attr_uuid.attr,
1189         NULL,
1190 };
1191
1192 static const struct attribute *obd_def_attrs[] = {
1193         &lustre_attr_blocksize.attr,
1194         &lustre_attr_kbytestotal.attr,
1195         &lustre_attr_kbytesfree.attr,
1196         &lustre_attr_kbytesavail.attr,
1197         &lustre_attr_filestotal.attr,
1198         &lustre_attr_filesfree.attr,
1199         &lustre_attr_uuid.attr,
1200         NULL,
1201 };
1202
1203 static void obd_sysfs_release(struct kobject *kobj)
1204 {
1205         struct obd_device *obd = container_of(kobj, struct obd_device,
1206                                               obd_kset.kobj);
1207
1208         complete(&obd->obd_kobj_unregister);
1209 }
1210
1211 int lprocfs_obd_setup(struct obd_device *obd, bool uuid_only)
1212 {
1213         struct lprocfs_vars *debugfs_vars = NULL;
1214         int rc;
1215
1216         if (!obd || obd->obd_magic != OBD_DEVICE_MAGIC)
1217                 return -ENODEV;
1218
1219         rc = kobject_set_name(&obd->obd_kset.kobj, "%s", obd->obd_name);
1220         if (rc)
1221                 return rc;
1222
1223         obd->obd_ktype.sysfs_ops = &lustre_sysfs_ops;
1224         obd->obd_ktype.release = obd_sysfs_release;
1225
1226         obd->obd_kset.kobj.parent = obd->obd_type->typ_kobj;
1227         obd->obd_kset.kobj.ktype = &obd->obd_ktype;
1228         init_completion(&obd->obd_kobj_unregister);
1229         rc = kset_register(&obd->obd_kset);
1230         if (rc)
1231                 return rc;
1232
1233         if (uuid_only)
1234                 obd->obd_attrs = obd_def_uuid_attrs;
1235         else
1236                 obd->obd_attrs = obd_def_attrs;
1237
1238         rc = sysfs_create_files(&obd->obd_kset.kobj, obd->obd_attrs);
1239         if (rc) {
1240                 kset_unregister(&obd->obd_kset);
1241                 return rc;
1242         }
1243
1244         if (!obd->obd_type->typ_procroot)
1245                 debugfs_vars = obd->obd_vars;
1246         obd->obd_debugfs_entry = ldebugfs_register(obd->obd_name,
1247                                                    obd->obd_type->typ_debugfs_entry,
1248                                                    debugfs_vars, obd);
1249         if (IS_ERR_OR_NULL(obd->obd_debugfs_entry)) {
1250                 rc = obd->obd_debugfs_entry ? PTR_ERR(obd->obd_debugfs_entry)
1251                                             : -ENOMEM;
1252                 CERROR("error %d setting up debugfs for %s\n",
1253                        rc, obd->obd_name);
1254                 obd->obd_debugfs_entry = NULL;
1255
1256                 sysfs_remove_files(&obd->obd_kset.kobj, obd->obd_attrs);
1257                 obd->obd_attrs = NULL;
1258                 kset_unregister(&obd->obd_kset);
1259                 return rc;
1260         }
1261
1262         if (obd->obd_proc_entry || !obd->obd_type->typ_procroot)
1263                 GOTO(already_registered, rc);
1264
1265         obd->obd_proc_entry = lprocfs_register(obd->obd_name,
1266                                                obd->obd_type->typ_procroot,
1267                                                obd->obd_vars, obd);
1268         if (IS_ERR(obd->obd_proc_entry)) {
1269                 rc = PTR_ERR(obd->obd_proc_entry);
1270                 CERROR("error %d setting up lprocfs for %s\n",rc,obd->obd_name);
1271                 obd->obd_proc_entry = NULL;
1272
1273                 ldebugfs_remove(&obd->obd_debugfs_entry);
1274                 sysfs_remove_files(&obd->obd_kset.kobj, obd->obd_attrs);
1275                 obd->obd_attrs = NULL;
1276                 kset_unregister(&obd->obd_kset);
1277                 return rc;
1278         }
1279 already_registered:
1280         return rc;
1281 }
1282 EXPORT_SYMBOL(lprocfs_obd_setup);
1283
1284 int lprocfs_obd_cleanup(struct obd_device *obd)
1285 {
1286         if (!obd)
1287                 return -EINVAL;
1288
1289         if (obd->obd_proc_exports_entry) {
1290                 /* Should be no exports left */
1291                 lprocfs_remove(&obd->obd_proc_exports_entry);
1292                 obd->obd_proc_exports_entry = NULL;
1293         }
1294
1295         if (obd->obd_proc_entry) {
1296                 lprocfs_remove(&obd->obd_proc_entry);
1297                 obd->obd_proc_entry = NULL;
1298         }
1299
1300         if (!IS_ERR_OR_NULL(obd->obd_debugfs_entry))
1301                 ldebugfs_remove(&obd->obd_debugfs_entry);
1302
1303         /* obd device never allocated a kset */
1304         if (!obd->obd_kset.kobj.state_initialized)
1305                 return 0;
1306
1307         if (obd->obd_attrs) {
1308                 sysfs_remove_files(&obd->obd_kset.kobj, obd->obd_attrs);
1309                 obd->obd_attrs = NULL;
1310         }
1311
1312         kset_unregister(&obd->obd_kset);
1313         wait_for_completion(&obd->obd_kobj_unregister);
1314         return 0;
1315 }
1316 EXPORT_SYMBOL(lprocfs_obd_cleanup);
1317
1318 int lprocfs_stats_alloc_one(struct lprocfs_stats *stats, unsigned int cpuid)
1319 {
1320         struct lprocfs_counter  *cntr;
1321         unsigned int            percpusize;
1322         int                     rc = -ENOMEM;
1323         unsigned long           flags = 0;
1324         int                     i;
1325
1326         LASSERT(stats->ls_percpu[cpuid] == NULL);
1327         LASSERT((stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) == 0);
1328
1329         percpusize = lprocfs_stats_counter_size(stats);
1330         LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[cpuid], percpusize);
1331         if (stats->ls_percpu[cpuid] != NULL) {
1332                 rc = 0;
1333                 if (unlikely(stats->ls_biggest_alloc_num <= cpuid)) {
1334                         if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1335                                 spin_lock_irqsave(&stats->ls_lock, flags);
1336                         else
1337                                 spin_lock(&stats->ls_lock);
1338                         if (stats->ls_biggest_alloc_num <= cpuid)
1339                                 stats->ls_biggest_alloc_num = cpuid + 1;
1340                         if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) {
1341                                 spin_unlock_irqrestore(&stats->ls_lock, flags);
1342                         } else {
1343                                 spin_unlock(&stats->ls_lock);
1344                         }
1345                 }
1346                 /* initialize the ls_percpu[cpuid] non-zero counter */
1347                 for (i = 0; i < stats->ls_num; ++i) {
1348                         cntr = lprocfs_stats_counter_get(stats, cpuid, i);
1349                         cntr->lc_min = LC_MIN_INIT;
1350                 }
1351         }
1352         return rc;
1353 }
1354
1355 struct lprocfs_stats *lprocfs_alloc_stats(unsigned int num,
1356                                           enum lprocfs_stats_flags flags)
1357 {
1358         struct lprocfs_stats    *stats;
1359         unsigned int            num_entry;
1360         unsigned int            percpusize = 0;
1361         int                     i;
1362
1363         if (num == 0)
1364                 return NULL;
1365
1366         if (lprocfs_no_percpu_stats != 0)
1367                 flags |= LPROCFS_STATS_FLAG_NOPERCPU;
1368
1369         if (flags & LPROCFS_STATS_FLAG_NOPERCPU)
1370                 num_entry = 1;
1371         else
1372                 num_entry = num_possible_cpus();
1373
1374         /* alloc percpu pointers for all possible cpu slots */
1375         LIBCFS_ALLOC(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1376         if (stats == NULL)
1377                 return NULL;
1378
1379         stats->ls_num = num;
1380         stats->ls_flags = flags;
1381         spin_lock_init(&stats->ls_lock);
1382
1383         /* alloc num of counter headers */
1384         LIBCFS_ALLOC(stats->ls_cnt_header,
1385                      stats->ls_num * sizeof(struct lprocfs_counter_header));
1386         if (stats->ls_cnt_header == NULL)
1387                 goto fail;
1388
1389         if ((flags & LPROCFS_STATS_FLAG_NOPERCPU) != 0) {
1390                 /* contains only one set counters */
1391                 percpusize = lprocfs_stats_counter_size(stats);
1392                 LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[0], percpusize);
1393                 if (stats->ls_percpu[0] == NULL)
1394                         goto fail;
1395                 stats->ls_biggest_alloc_num = 1;
1396         } else if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0) {
1397                 /* alloc all percpu data, currently only obd_memory use this */
1398                 for (i = 0; i < num_entry; ++i)
1399                         if (lprocfs_stats_alloc_one(stats, i) < 0)
1400                                 goto fail;
1401         }
1402
1403         return stats;
1404
1405 fail:
1406         lprocfs_free_stats(&stats);
1407         return NULL;
1408 }
1409 EXPORT_SYMBOL(lprocfs_alloc_stats);
1410
1411 void lprocfs_free_stats(struct lprocfs_stats **statsh)
1412 {
1413         struct lprocfs_stats *stats = *statsh;
1414         unsigned int num_entry;
1415         unsigned int percpusize;
1416         unsigned int i;
1417
1418         if (stats == NULL || stats->ls_num == 0)
1419                 return;
1420         *statsh = NULL;
1421
1422         if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU)
1423                 num_entry = 1;
1424         else
1425                 num_entry = num_possible_cpus();
1426
1427         percpusize = lprocfs_stats_counter_size(stats);
1428         for (i = 0; i < num_entry; i++)
1429                 if (stats->ls_percpu[i] != NULL)
1430                         LIBCFS_FREE(stats->ls_percpu[i], percpusize);
1431         if (stats->ls_cnt_header != NULL)
1432                 LIBCFS_FREE(stats->ls_cnt_header, stats->ls_num *
1433                                         sizeof(struct lprocfs_counter_header));
1434         LIBCFS_FREE(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1435 }
1436 EXPORT_SYMBOL(lprocfs_free_stats);
1437
1438 u64 lprocfs_stats_collector(struct lprocfs_stats *stats, int idx,
1439                             enum lprocfs_fields_flags field)
1440 {
1441         unsigned long flags = 0;
1442         unsigned int num_cpu;
1443         unsigned int i;
1444         u64 ret = 0;
1445
1446         LASSERT(stats);
1447
1448         num_cpu = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1449         for (i = 0; i < num_cpu; i++) {
1450                 struct lprocfs_counter *cntr;
1451
1452                 if (!stats->ls_percpu[i])
1453                         continue;
1454
1455                 cntr = lprocfs_stats_counter_get(stats, i, idx);
1456                 ret += lprocfs_read_helper(cntr, &stats->ls_cnt_header[idx],
1457                                            stats->ls_flags, field);
1458         }
1459         lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1460         return ret;
1461 }
1462 EXPORT_SYMBOL(lprocfs_stats_collector);
1463
1464 void lprocfs_clear_stats(struct lprocfs_stats *stats)
1465 {
1466         struct lprocfs_counter          *percpu_cntr;
1467         int                             i;
1468         int                             j;
1469         unsigned int                    num_entry;
1470         unsigned long                   flags = 0;
1471
1472         num_entry = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1473
1474         for (i = 0; i < num_entry; i++) {
1475                 if (stats->ls_percpu[i] == NULL)
1476                         continue;
1477                 for (j = 0; j < stats->ls_num; j++) {
1478                         percpu_cntr = lprocfs_stats_counter_get(stats, i, j);
1479                         percpu_cntr->lc_count           = 0;
1480                         percpu_cntr->lc_min             = LC_MIN_INIT;
1481                         percpu_cntr->lc_max             = 0;
1482                         percpu_cntr->lc_sumsquare       = 0;
1483                         percpu_cntr->lc_sum             = 0;
1484                         if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1485                                 percpu_cntr->lc_sum_irq = 0;
1486                 }
1487         }
1488
1489         lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1490 }
1491 EXPORT_SYMBOL(lprocfs_clear_stats);
1492
1493 static ssize_t lprocfs_stats_seq_write(struct file *file,
1494                                        const char __user *buf,
1495                                        size_t len, loff_t *off)
1496 {
1497         struct seq_file *seq = file->private_data;
1498         struct lprocfs_stats *stats = seq->private;
1499
1500         lprocfs_clear_stats(stats);
1501
1502         return len;
1503 }
1504
1505 static void *lprocfs_stats_seq_start(struct seq_file *p, loff_t *pos)
1506 {
1507         struct lprocfs_stats *stats = p->private;
1508
1509         return (*pos < stats->ls_num) ? pos : NULL;
1510 }
1511
1512 static void lprocfs_stats_seq_stop(struct seq_file *p, void *v)
1513 {
1514 }
1515
1516 static void *lprocfs_stats_seq_next(struct seq_file *p, void *v, loff_t *pos)
1517 {
1518         (*pos)++;
1519
1520         return lprocfs_stats_seq_start(p, pos);
1521 }
1522
1523 /* seq file export of one lprocfs counter */
1524 static int lprocfs_stats_seq_show(struct seq_file *p, void *v)
1525 {
1526         struct lprocfs_stats            *stats  = p->private;
1527         struct lprocfs_counter_header   *hdr;
1528         struct lprocfs_counter           ctr;
1529         int                              idx    = *(loff_t *)v;
1530
1531         if (idx == 0) {
1532                 struct timespec64 now;
1533
1534                 ktime_get_real_ts64(&now);
1535                 seq_printf(p, "%-25s %llu.%09lu secs.nsecs\n",
1536                            "snapshot_time", (s64)now.tv_sec, now.tv_nsec);
1537         }
1538
1539         hdr = &stats->ls_cnt_header[idx];
1540         lprocfs_stats_collect(stats, idx, &ctr);
1541
1542         if (ctr.lc_count == 0)
1543                 return 0;
1544
1545         seq_printf(p, "%-25s %lld samples [%s]", hdr->lc_name,
1546                    ctr.lc_count, hdr->lc_units);
1547
1548         if ((hdr->lc_config & LPROCFS_CNTR_AVGMINMAX) && ctr.lc_count > 0) {
1549                 seq_printf(p, " %lld %lld %lld",
1550                            ctr.lc_min, ctr.lc_max, ctr.lc_sum);
1551                 if (hdr->lc_config & LPROCFS_CNTR_STDDEV)
1552                         seq_printf(p, " %llu", ctr.lc_sumsquare);
1553         }
1554         seq_putc(p, '\n');
1555         return 0;
1556 }
1557
1558 static const struct seq_operations lprocfs_stats_seq_sops = {
1559         .start  = lprocfs_stats_seq_start,
1560         .stop   = lprocfs_stats_seq_stop,
1561         .next   = lprocfs_stats_seq_next,
1562         .show   = lprocfs_stats_seq_show,
1563 };
1564
1565 static int lprocfs_stats_seq_open(struct inode *inode, struct file *file)
1566 {
1567         struct seq_file *seq;
1568         int rc;
1569
1570         rc = LPROCFS_ENTRY_CHECK(inode);
1571         if (rc < 0)
1572                 return rc;
1573
1574         rc = seq_open(file, &lprocfs_stats_seq_sops);
1575         if (rc)
1576                 return rc;
1577         seq = file->private_data;
1578         seq->private = inode->i_private ? inode->i_private : PDE_DATA(inode);
1579         return 0;
1580 }
1581
1582 static const struct file_operations lprocfs_stats_seq_fops = {
1583         .owner   = THIS_MODULE,
1584         .open    = lprocfs_stats_seq_open,
1585         .read    = seq_read,
1586         .write   = lprocfs_stats_seq_write,
1587         .llseek  = seq_lseek,
1588         .release = lprocfs_seq_release,
1589 };
1590
1591 int ldebugfs_register_stats(struct dentry *parent, const char *name,
1592                             struct lprocfs_stats *stats)
1593 {
1594         struct dentry *entry;
1595
1596         LASSERT(!IS_ERR_OR_NULL(parent));
1597
1598         entry = debugfs_create_file(name, 0644, parent, stats,
1599                                     &lprocfs_stats_seq_fops);
1600         if (IS_ERR_OR_NULL(entry))
1601                 return entry ? PTR_ERR(entry) : -ENOMEM;
1602
1603         return 0;
1604 }
1605 EXPORT_SYMBOL_GPL(ldebugfs_register_stats);
1606
1607 int lprocfs_register_stats(struct proc_dir_entry *root, const char *name,
1608                            struct lprocfs_stats *stats)
1609 {
1610         struct proc_dir_entry *entry;
1611         LASSERT(root != NULL);
1612
1613         entry = proc_create_data(name, 0644, root,
1614                                  &lprocfs_stats_seq_fops, stats);
1615         if (entry == NULL)
1616                 return -ENOMEM;
1617         return 0;
1618 }
1619 EXPORT_SYMBOL(lprocfs_register_stats);
1620
1621 void lprocfs_counter_init(struct lprocfs_stats *stats, int index,
1622                           unsigned conf, const char *name, const char *units)
1623 {
1624         struct lprocfs_counter_header   *header;
1625         struct lprocfs_counter          *percpu_cntr;
1626         unsigned long                   flags = 0;
1627         unsigned int                    i;
1628         unsigned int                    num_cpu;
1629
1630         LASSERT(stats != NULL);
1631
1632         header = &stats->ls_cnt_header[index];
1633         LASSERTF(header != NULL, "Failed to allocate stats header:[%d]%s/%s\n",
1634                  index, name, units);
1635
1636         header->lc_config = conf;
1637         header->lc_name   = name;
1638         header->lc_units  = units;
1639
1640         num_cpu = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1641         for (i = 0; i < num_cpu; ++i) {
1642                 if (stats->ls_percpu[i] == NULL)
1643                         continue;
1644                 percpu_cntr = lprocfs_stats_counter_get(stats, i, index);
1645                 percpu_cntr->lc_count           = 0;
1646                 percpu_cntr->lc_min             = LC_MIN_INIT;
1647                 percpu_cntr->lc_max             = 0;
1648                 percpu_cntr->lc_sumsquare       = 0;
1649                 percpu_cntr->lc_sum             = 0;
1650                 if ((stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1651                         percpu_cntr->lc_sum_irq = 0;
1652         }
1653         lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1654 }
1655 EXPORT_SYMBOL(lprocfs_counter_init);
1656
1657 /* Note that we only init md counters for ops whose offset is less
1658  * than NUM_MD_STATS. This is explained in a comment in the definition
1659  * of struct md_ops. */
1660 #define LPROCFS_MD_OP_INIT(base, stats, op)                                    \
1661         do {                                                                   \
1662                 unsigned int _idx = base + MD_COUNTER_OFFSET(op);              \
1663                                                                                \
1664                 if (MD_COUNTER_OFFSET(op) < NUM_MD_STATS) {                    \
1665                         LASSERT(_idx < stats->ls_num);                         \
1666                         lprocfs_counter_init(stats, _idx, 0, #op, "reqs");     \
1667                 }                                                              \
1668         } while (0)
1669
1670 void lprocfs_init_mps_stats(int num_private_stats, struct lprocfs_stats *stats)
1671 {
1672         LPROCFS_MD_OP_INIT(num_private_stats, stats, get_root);
1673         LPROCFS_MD_OP_INIT(num_private_stats, stats, null_inode);
1674         LPROCFS_MD_OP_INIT(num_private_stats, stats, close);
1675         LPROCFS_MD_OP_INIT(num_private_stats, stats, create);
1676         LPROCFS_MD_OP_INIT(num_private_stats, stats, enqueue);
1677         LPROCFS_MD_OP_INIT(num_private_stats, stats, getattr);
1678         LPROCFS_MD_OP_INIT(num_private_stats, stats, getattr_name);
1679         LPROCFS_MD_OP_INIT(num_private_stats, stats, intent_lock);
1680         LPROCFS_MD_OP_INIT(num_private_stats, stats, link);
1681         LPROCFS_MD_OP_INIT(num_private_stats, stats, rename);
1682         LPROCFS_MD_OP_INIT(num_private_stats, stats, setattr);
1683         LPROCFS_MD_OP_INIT(num_private_stats, stats, fsync);
1684         LPROCFS_MD_OP_INIT(num_private_stats, stats, read_page);
1685         LPROCFS_MD_OP_INIT(num_private_stats, stats, unlink);
1686         LPROCFS_MD_OP_INIT(num_private_stats, stats, setxattr);
1687         LPROCFS_MD_OP_INIT(num_private_stats, stats, getxattr);
1688         LPROCFS_MD_OP_INIT(num_private_stats, stats, init_ea_size);
1689         LPROCFS_MD_OP_INIT(num_private_stats, stats, get_lustre_md);
1690         LPROCFS_MD_OP_INIT(num_private_stats, stats, free_lustre_md);
1691         LPROCFS_MD_OP_INIT(num_private_stats, stats, merge_attr);
1692         LPROCFS_MD_OP_INIT(num_private_stats, stats, set_open_replay_data);
1693         LPROCFS_MD_OP_INIT(num_private_stats, stats, clear_open_replay_data);
1694         LPROCFS_MD_OP_INIT(num_private_stats, stats, set_lock_data);
1695         LPROCFS_MD_OP_INIT(num_private_stats, stats, lock_match);
1696         LPROCFS_MD_OP_INIT(num_private_stats, stats, cancel_unused);
1697         LPROCFS_MD_OP_INIT(num_private_stats, stats, intent_getattr_async);
1698         LPROCFS_MD_OP_INIT(num_private_stats, stats, revalidate_lock);
1699 }
1700
1701 int lprocfs_alloc_md_stats(struct obd_device *obd,
1702                            unsigned int num_private_stats)
1703 {
1704         struct lprocfs_stats *stats;
1705         unsigned int num_stats;
1706         int rc, i;
1707
1708         CLASSERT(offsetof(struct md_ops, MD_STATS_FIRST_OP) == 0);
1709         CLASSERT(_MD_COUNTER_OFFSET(MD_STATS_FIRST_OP) == 0);
1710         CLASSERT(_MD_COUNTER_OFFSET(MD_STATS_LAST_OP) > 0);
1711
1712         /* TODO Ensure that this function is only used where
1713          * appropriate by adding an assertion to the effect that
1714          * obd->obd_type->typ_md_ops is not NULL. We can't do this now
1715          * because mdt_procfs_init() uses this function to allocate
1716          * the stats backing /proc/fs/lustre/mdt/.../md_stats but the
1717          * mdt layer does not use the md_ops interface. This is
1718          * confusing and a waste of memory. See LU-2484.
1719          */
1720         LASSERT(obd->obd_proc_entry != NULL);
1721         LASSERT(obd->obd_md_stats == NULL);
1722         LASSERT(obd->obd_md_cntr_base == 0);
1723
1724         num_stats = NUM_MD_STATS + num_private_stats;
1725         stats = lprocfs_alloc_stats(num_stats, 0);
1726         if (stats == NULL)
1727                 return -ENOMEM;
1728
1729         lprocfs_init_mps_stats(num_private_stats, stats);
1730
1731         for (i = num_private_stats; i < num_stats; i++) {
1732                 if (stats->ls_cnt_header[i].lc_name == NULL) {
1733                         CERROR("Missing md_stat initializer md_op "
1734                                "operation at offset %d. Aborting.\n",
1735                                i - num_private_stats);
1736                         LBUG();
1737                 }
1738         }
1739
1740         rc = lprocfs_register_stats(obd->obd_proc_entry, "md_stats", stats);
1741         if (rc < 0) {
1742                 lprocfs_free_stats(&stats);
1743         } else {
1744                 obd->obd_md_stats = stats;
1745                 obd->obd_md_cntr_base = num_private_stats;
1746         }
1747
1748         return rc;
1749 }
1750 EXPORT_SYMBOL(lprocfs_alloc_md_stats);
1751
1752 void lprocfs_free_md_stats(struct obd_device *obd)
1753 {
1754         struct lprocfs_stats *stats = obd->obd_md_stats;
1755
1756         if (stats != NULL) {
1757                 obd->obd_md_stats = NULL;
1758                 obd->obd_md_cntr_base = 0;
1759                 lprocfs_free_stats(&stats);
1760         }
1761 }
1762 EXPORT_SYMBOL(lprocfs_free_md_stats);
1763
1764 void lprocfs_init_ldlm_stats(struct lprocfs_stats *ldlm_stats)
1765 {
1766         lprocfs_counter_init(ldlm_stats,
1767                              LDLM_ENQUEUE - LDLM_FIRST_OPC,
1768                              0, "ldlm_enqueue", "reqs");
1769         lprocfs_counter_init(ldlm_stats,
1770                              LDLM_CONVERT - LDLM_FIRST_OPC,
1771                              0, "ldlm_convert", "reqs");
1772         lprocfs_counter_init(ldlm_stats,
1773                              LDLM_CANCEL - LDLM_FIRST_OPC,
1774                              0, "ldlm_cancel", "reqs");
1775         lprocfs_counter_init(ldlm_stats,
1776                              LDLM_BL_CALLBACK - LDLM_FIRST_OPC,
1777                              0, "ldlm_bl_callback", "reqs");
1778         lprocfs_counter_init(ldlm_stats,
1779                              LDLM_CP_CALLBACK - LDLM_FIRST_OPC,
1780                              0, "ldlm_cp_callback", "reqs");
1781         lprocfs_counter_init(ldlm_stats,
1782                              LDLM_GL_CALLBACK - LDLM_FIRST_OPC,
1783                              0, "ldlm_gl_callback", "reqs");
1784 }
1785 EXPORT_SYMBOL(lprocfs_init_ldlm_stats);
1786
1787 __s64 lprocfs_read_helper(struct lprocfs_counter *lc,
1788                           struct lprocfs_counter_header *header,
1789                           enum lprocfs_stats_flags flags,
1790                           enum lprocfs_fields_flags field)
1791 {
1792         __s64 ret = 0;
1793
1794         if (lc == NULL || header == NULL)
1795                 RETURN(0);
1796
1797         switch (field) {
1798                 case LPROCFS_FIELDS_FLAGS_CONFIG:
1799                         ret = header->lc_config;
1800                         break;
1801                 case LPROCFS_FIELDS_FLAGS_SUM:
1802                         ret = lc->lc_sum;
1803                         if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1804                                 ret += lc->lc_sum_irq;
1805                         break;
1806                 case LPROCFS_FIELDS_FLAGS_MIN:
1807                         ret = lc->lc_min;
1808                         break;
1809                 case LPROCFS_FIELDS_FLAGS_MAX:
1810                         ret = lc->lc_max;
1811                         break;
1812                 case LPROCFS_FIELDS_FLAGS_AVG:
1813                         ret = (lc->lc_max - lc->lc_min) / 2;
1814                         break;
1815                 case LPROCFS_FIELDS_FLAGS_SUMSQUARE:
1816                         ret = lc->lc_sumsquare;
1817                         break;
1818                 case LPROCFS_FIELDS_FLAGS_COUNT:
1819                         ret = lc->lc_count;
1820                         break;
1821                 default:
1822                         break;
1823         };
1824         RETURN(ret);
1825 }
1826 EXPORT_SYMBOL(lprocfs_read_helper);
1827
1828 int lprocfs_read_frac_helper(char *buffer, unsigned long count, long val,
1829                              int mult)
1830 {
1831         long decimal_val, frac_val;
1832         int prtn;
1833
1834         if (count < 10)
1835                 return -EINVAL;
1836
1837         decimal_val = val / mult;
1838         prtn = snprintf(buffer, count, "%ld", decimal_val);
1839         frac_val = val % mult;
1840
1841         if (prtn < (count - 4) && frac_val > 0) {
1842                 long temp_frac;
1843                 int i, temp_mult = 1, frac_bits = 0;
1844
1845                 temp_frac = frac_val * 10;
1846                 buffer[prtn++] = '.';
1847                 while (frac_bits < 2 && (temp_frac / mult) < 1 ) {
1848                         /* only reserved 2 bits fraction */
1849                         buffer[prtn++] ='0';
1850                         temp_frac *= 10;
1851                         frac_bits++;
1852                 }
1853                 /*
1854                  * Need to think these cases :
1855                  *      1. #echo x.00 > /proc/xxx       output result : x
1856                  *      2. #echo x.0x > /proc/xxx       output result : x.0x
1857                  *      3. #echo x.x0 > /proc/xxx       output result : x.x
1858                  *      4. #echo x.xx > /proc/xxx       output result : x.xx
1859                  *      Only reserved 2 bits fraction.
1860                  */
1861                 for (i = 0; i < (5 - prtn); i++)
1862                         temp_mult *= 10;
1863
1864                 frac_bits = min((int)count - prtn, 3 - frac_bits);
1865                 prtn += snprintf(buffer + prtn, frac_bits, "%ld",
1866                                  frac_val * temp_mult / mult);
1867
1868                 prtn--;
1869                 while(buffer[prtn] < '1' || buffer[prtn] > '9') {
1870                         prtn--;
1871                         if (buffer[prtn] == '.') {
1872                                 prtn--;
1873                                 break;
1874                         }
1875                 }
1876                 prtn++;
1877         }
1878         buffer[prtn++] ='\n';
1879         return prtn;
1880 }
1881 EXPORT_SYMBOL(lprocfs_read_frac_helper);
1882
1883 int lprocfs_seq_read_frac_helper(struct seq_file *m, long val, int mult)
1884 {
1885         long decimal_val, frac_val;
1886
1887         decimal_val = val / mult;
1888         seq_printf(m, "%ld", decimal_val);
1889         frac_val = val % mult;
1890
1891         if (frac_val > 0) {
1892                 frac_val *= 100;
1893                 frac_val /= mult;
1894         }
1895         if (frac_val > 0) {
1896                 /* Three cases: x0, xx, 0x */
1897                 if ((frac_val % 10) != 0)
1898                         seq_printf(m, ".%ld", frac_val);
1899                 else
1900                         seq_printf(m, ".%ld", frac_val / 10);
1901         }
1902
1903         seq_printf(m, "\n");
1904         return 0;
1905 }
1906 EXPORT_SYMBOL(lprocfs_seq_read_frac_helper);
1907
1908 /* Obtains the conversion factor for the unit specified */
1909 static int get_mult(char unit, __u64 *mult)
1910 {
1911         __u64 units = 1;
1912
1913         switch (unit) {
1914         /* peta, tera, giga, mega, and kilo */
1915         case 'p':
1916         case 'P':
1917                 units <<= 10;
1918         case 't':
1919         case 'T':
1920                 units <<= 10;
1921         case 'g':
1922         case 'G':
1923                 units <<= 10;
1924         case 'm':
1925         case 'M':
1926                 units <<= 10;
1927         case 'k':
1928         case 'K':
1929                 units <<= 10;
1930                 break;
1931         /* some tests expect % to be accepted */
1932         case '%':
1933                 units = 1;
1934                 break;
1935         default:
1936                 return -EINVAL;
1937         }
1938
1939         *mult = units;
1940
1941         return 0;
1942 }
1943
1944 /*
1945  * Ensures the numeric string is valid. The function provides the final
1946  * multiplier in the case a unit exists at the end of the string. It also
1947  * locates the start of the whole and fractional parts (if any). This
1948  * function modifies the string so kstrtoull can be used to parse both
1949  * the whole and fraction portions. This function also figures out
1950  * the base of the number.
1951  */
1952 static int preprocess_numeric_str(char *buffer, __u64 *mult, __u64 def_mult,
1953                                   bool allow_units, char **whole, char **frac,
1954                                   unsigned int *base)
1955 {
1956         bool hit_decimal = false;
1957         bool hit_unit = false;
1958         int rc = 0;
1959         char *start;
1960         *mult = def_mult;
1961         *whole = NULL;
1962         *frac = NULL;
1963         *base = 10;
1964
1965         /* a hex string if it starts with "0x" */
1966         if (buffer[0] == '0' && tolower(buffer[1]) == 'x') {
1967                 *base = 16;
1968                 buffer += 2;
1969         }
1970
1971         start = buffer;
1972
1973         while (*buffer) {
1974                 /* allow for a single new line before the null terminator */
1975                 if (*buffer == '\n') {
1976                         *buffer = '\0';
1977                         buffer++;
1978
1979                         if (*buffer)
1980                                 return -EINVAL;
1981
1982                         break;
1983                 }
1984
1985                 /* any chars after our unit indicates a malformed string */
1986                 if (hit_unit)
1987                         return -EINVAL;
1988
1989                 /* ensure we only hit one decimal */
1990                 if (*buffer == '.') {
1991                         if (hit_decimal)
1992                                 return -EINVAL;
1993
1994                         /* if past start, there's a whole part */
1995                         if (start != buffer)
1996                                 *whole = start;
1997
1998                         *buffer = '\0';
1999                         start = buffer + 1;
2000                         hit_decimal = true;
2001                 } else if (!isdigit(*buffer) &&
2002                            !(*base == 16 && isxdigit(*buffer))) {
2003                         if (allow_units) {
2004                                 /* if we allow units, attempt to get mult */
2005                                 hit_unit = true;
2006                                 rc = get_mult(*buffer, mult);
2007                                 if (rc)
2008                                         return rc;
2009
2010                                 /* string stops here, but keep processing */
2011                                 *buffer = '\0';
2012                         } else {
2013                                 /* bad string */
2014                                 return -EINVAL;
2015                         }
2016                 }
2017
2018                 buffer++;
2019         }
2020
2021         if (hit_decimal) {
2022                 /* hit a decimal, make sure there's a fractional part */
2023                 if (!*start)
2024                         return -EINVAL;
2025
2026                 *frac = start;
2027         } else {
2028                 /* didn't hit a decimal, but may have a whole part */
2029                 if (start != buffer && *start)
2030                         *whole = start;
2031         }
2032
2033         /* malformed string if we didn't get anything */
2034         if (!*frac && !*whole)
2035                 return -EINVAL;
2036
2037         return 0;
2038 }
2039
2040 /*
2041  * Parses a numeric string which can contain a whole and fraction portion
2042  * into a __u64. Accepts a multiplier to apply to the value parsed. Also
2043  * allows the string to have a unit at the end. The function handles
2044  * wrapping of the final unsigned value.
2045  */
2046 static int str_to_u64_parse(char *buffer, unsigned long count,
2047                             __u64 *val, __u64 def_mult, bool allow_units)
2048 {
2049         __u64 whole = 0;
2050         __u64 frac = 0;
2051         unsigned int frac_d = 1;
2052         __u64 wrap_indicator = ULLONG_MAX;
2053         int rc = 0;
2054         __u64 mult;
2055         char *strwhole;
2056         char *strfrac;
2057         unsigned int base = 10;
2058
2059         rc = preprocess_numeric_str(buffer, &mult, def_mult, allow_units,
2060                                     &strwhole, &strfrac, &base);
2061
2062         if (rc)
2063                 return rc;
2064
2065         if (mult == 0) {
2066                 *val = 0;
2067                 return 0;
2068         }
2069
2070         /* the multiplier limits how large the value can be */
2071         wrap_indicator = div64_u64(wrap_indicator, mult);
2072
2073         if (strwhole) {
2074                 rc = kstrtoull(strwhole, base, &whole);
2075                 if (rc)
2076                         return rc;
2077
2078                 if (whole > wrap_indicator)
2079                         return -ERANGE;
2080
2081                 whole *= mult;
2082         }
2083
2084         if (strfrac) {
2085                 if (strlen(strfrac) > 10)
2086                         strfrac[10] = '\0';
2087
2088                 rc = kstrtoull(strfrac, base, &frac);
2089                 if (rc)
2090                         return rc;
2091
2092                 /* determine power of fractional portion */
2093                 while (*strfrac) {
2094                         frac_d *= base;
2095                         strfrac++;
2096                 }
2097
2098                 /* fractional portion is too large to perform calculation */
2099                 if (frac > wrap_indicator)
2100                         return -ERANGE;
2101
2102                 frac *= mult;
2103                 do_div(frac, frac_d);
2104         }
2105
2106         /* check that the sum of whole and fraction fits in u64 */
2107         if (whole > (ULLONG_MAX - frac))
2108                 return -ERANGE;
2109
2110         *val = whole + frac;
2111
2112         return 0;
2113 }
2114
2115 /*
2116  * This function parses numeric/hex strings into __s64. It accepts a multiplier
2117  * which will apply to the value parsed. It also can allow the string to
2118  * have a unit as the last character. The function handles overflow/underflow
2119  * of the signed integer.
2120  */
2121 static int str_to_s64_internal(const char __user *buffer, unsigned long count,
2122                                __s64 *val, __u64 def_mult, bool allow_units)
2123 {
2124         char kernbuf[22];
2125         __u64 tmp;
2126         unsigned int offset = 0;
2127         int signed sign = 1;
2128         __u64 max = LLONG_MAX;
2129         int rc = 0;
2130
2131         if (count > (sizeof(kernbuf) - 1))
2132                 return -EINVAL;
2133
2134         if (copy_from_user(kernbuf, buffer, count))
2135                 return -EFAULT;
2136
2137         kernbuf[count] = '\0';
2138
2139         /* keep track of our sign */
2140         if (*kernbuf == '-') {
2141                 sign = -1;
2142                 offset++;
2143                 /* equivalent to max = -LLONG_MIN, avoids overflow */
2144                 max++;
2145         }
2146
2147         rc = str_to_u64_parse(kernbuf + offset, count - offset,
2148                               &tmp, def_mult, allow_units);
2149         if (rc)
2150                 return rc;
2151
2152         /* check for overflow/underflow */
2153         if (max < tmp)
2154                 return -ERANGE;
2155
2156         *val = (__s64)tmp * sign;
2157
2158         return 0;
2159 }
2160
2161 /**
2162  * Convert a user string into a signed 64 bit number. This function produces
2163  * an error when the value parsed from the string times multiplier underflows or
2164  * overflows. This function only accepts strings that contains digits, an
2165  * optional decimal, and a char representing a unit at the end. If a unit is
2166  * specified in the string, the multiplier provided by the caller is ignored.
2167  * This function can also accept hexadecimal strings which are prefixed with
2168  * "0x".
2169  *
2170  * \param[in] buffer    string consisting of numbers, a decimal, and a unit
2171  * \param[in] count     buffer length
2172  * \param[in] val       if successful, the value represented by the string
2173  * \param[in] defunit   default unit if string doesn't contain one
2174  *
2175  * \retval              0 on success
2176  * \retval              negative number on error
2177  */
2178 int lprocfs_str_with_units_to_s64(const char __user *buffer,
2179                                   unsigned long count, __s64 *val, char defunit)
2180 {
2181         __u64 mult = 1;
2182         int rc;
2183
2184         if (defunit != '1') {
2185                 rc = get_mult(defunit, &mult);
2186                 if (rc)
2187                         return rc;
2188         }
2189
2190         return str_to_s64_internal(buffer, count, val, mult, true);
2191 }
2192 EXPORT_SYMBOL(lprocfs_str_with_units_to_s64);
2193
2194 char *lprocfs_strnstr(const char *s1, const char *s2, size_t len)
2195 {
2196         size_t l2;
2197
2198         l2 = strlen(s2);
2199         if (!l2)
2200                 return (char *)s1;
2201         while (len >= l2) {
2202                 len--;
2203                 if (!memcmp(s1, s2, l2))
2204                         return (char *)s1;
2205                 s1++;
2206         }
2207         return NULL;
2208 }
2209 EXPORT_SYMBOL(lprocfs_strnstr);
2210
2211 /**
2212  * Find the string \a name in the input \a buffer, and return a pointer to the
2213  * value immediately following \a name, reducing \a count appropriately.
2214  * If \a name is not found the original \a buffer is returned.
2215  */
2216 char *lprocfs_find_named_value(const char *buffer, const char *name,
2217                                 size_t *count)
2218 {
2219         char *val;
2220         size_t buflen = *count;
2221
2222         /* there is no strnstr() in rhel5 and ubuntu kernels */
2223         val = lprocfs_strnstr(buffer, name, buflen);
2224         if (val == NULL)
2225                 return (char *)buffer;
2226
2227         val += strlen(name);                             /* skip prefix */
2228         while (val < buffer + buflen && isspace(*val)) /* skip separator */
2229                 val++;
2230
2231         *count = 0;
2232         while (val < buffer + buflen && isalnum(*val)) {
2233                 ++*count;
2234                 ++val;
2235         }
2236
2237         return val - *count;
2238 }
2239 EXPORT_SYMBOL(lprocfs_find_named_value);
2240
2241 int ldebugfs_seq_create(struct dentry *parent, const char *name, umode_t mode,
2242                         const struct file_operations *seq_fops, void *data)
2243 {
2244         struct dentry *entry;
2245
2246         /* Disallow secretly (un)writable entries. */
2247         LASSERT((!seq_fops->write) == (!(mode & 0222)));
2248
2249         entry = debugfs_create_file(name, mode, parent, data, seq_fops);
2250         if (IS_ERR_OR_NULL(entry))
2251                 return entry ? PTR_ERR(entry) : -ENOMEM;
2252
2253         return 0;
2254 }
2255 EXPORT_SYMBOL_GPL(ldebugfs_seq_create);
2256
2257 int lprocfs_seq_create(struct proc_dir_entry *parent,
2258                        const char *name,
2259                        mode_t mode,
2260                        const struct file_operations *seq_fops,
2261                        void *data)
2262 {
2263         struct proc_dir_entry *entry;
2264         ENTRY;
2265
2266         /* Disallow secretly (un)writable entries. */
2267         LASSERT((seq_fops->write == NULL) == ((mode & 0222) == 0));
2268
2269         entry = proc_create_data(name, mode, parent, seq_fops, data);
2270
2271         if (entry == NULL)
2272                 RETURN(-ENOMEM);
2273
2274         RETURN(0);
2275 }
2276 EXPORT_SYMBOL(lprocfs_seq_create);
2277
2278 int lprocfs_obd_seq_create(struct obd_device *dev,
2279                            const char *name,
2280                            mode_t mode,
2281                            const struct file_operations *seq_fops,
2282                            void *data)
2283 {
2284         return (lprocfs_seq_create(dev->obd_proc_entry, name,
2285                                    mode, seq_fops, data));
2286 }
2287 EXPORT_SYMBOL(lprocfs_obd_seq_create);
2288
2289 void lprocfs_oh_tally(struct obd_histogram *oh, unsigned int value)
2290 {
2291         if (value >= OBD_HIST_MAX)
2292                 value = OBD_HIST_MAX - 1;
2293
2294         spin_lock(&oh->oh_lock);
2295         oh->oh_buckets[value]++;
2296         spin_unlock(&oh->oh_lock);
2297 }
2298 EXPORT_SYMBOL(lprocfs_oh_tally);
2299
2300 void lprocfs_oh_tally_log2(struct obd_histogram *oh, unsigned int value)
2301 {
2302         unsigned int val = 0;
2303
2304         if (likely(value != 0))
2305                 val = min(fls(value - 1), OBD_HIST_MAX);
2306
2307         lprocfs_oh_tally(oh, val);
2308 }
2309 EXPORT_SYMBOL(lprocfs_oh_tally_log2);
2310
2311 unsigned long lprocfs_oh_sum(struct obd_histogram *oh)
2312 {
2313         unsigned long ret = 0;
2314         int i;
2315
2316         for (i = 0; i < OBD_HIST_MAX; i++)
2317                 ret +=  oh->oh_buckets[i];
2318         return ret;
2319 }
2320 EXPORT_SYMBOL(lprocfs_oh_sum);
2321
2322 void lprocfs_oh_clear(struct obd_histogram *oh)
2323 {
2324         spin_lock(&oh->oh_lock);
2325         memset(oh->oh_buckets, 0, sizeof(oh->oh_buckets));
2326         spin_unlock(&oh->oh_lock);
2327 }
2328 EXPORT_SYMBOL(lprocfs_oh_clear);
2329
2330 ssize_t lustre_attr_show(struct kobject *kobj,
2331                          struct attribute *attr, char *buf)
2332 {
2333         struct lustre_attr *a = container_of(attr, struct lustre_attr, attr);
2334
2335         return a->show ? a->show(kobj, attr, buf) : 0;
2336 }
2337 EXPORT_SYMBOL_GPL(lustre_attr_show);
2338
2339 ssize_t lustre_attr_store(struct kobject *kobj, struct attribute *attr,
2340                           const char *buf, size_t len)
2341 {
2342         struct lustre_attr *a = container_of(attr, struct lustre_attr, attr);
2343
2344         return a->store ? a->store(kobj, attr, buf, len) : len;
2345 }
2346 EXPORT_SYMBOL_GPL(lustre_attr_store);
2347
2348 const struct sysfs_ops lustre_sysfs_ops = {
2349         .show  = lustre_attr_show,
2350         .store = lustre_attr_store,
2351 };
2352 EXPORT_SYMBOL_GPL(lustre_sysfs_ops);
2353
2354 int lprocfs_obd_max_pages_per_rpc_seq_show(struct seq_file *m, void *data)
2355 {
2356         struct obd_device *dev = data;
2357         struct client_obd *cli = &dev->u.cli;
2358
2359         spin_lock(&cli->cl_loi_list_lock);
2360         seq_printf(m, "%d\n", cli->cl_max_pages_per_rpc);
2361         spin_unlock(&cli->cl_loi_list_lock);
2362         return 0;
2363 }
2364 EXPORT_SYMBOL(lprocfs_obd_max_pages_per_rpc_seq_show);
2365
2366 ssize_t lprocfs_obd_max_pages_per_rpc_seq_write(struct file *file,
2367                                                 const char __user *buffer,
2368                                                 size_t count, loff_t *off)
2369 {
2370         struct obd_device *dev =
2371                 ((struct seq_file *)file->private_data)->private;
2372         struct client_obd *cli = &dev->u.cli;
2373         struct obd_connect_data *ocd = &cli->cl_import->imp_connect_data;
2374         int chunk_mask, rc;
2375         s64 val;
2376
2377         rc = lprocfs_str_with_units_to_s64(buffer, count, &val, '1');
2378         if (rc)
2379                 return rc;
2380         if (val < 0)
2381                 return -ERANGE;
2382
2383         /* if the max_pages is specified in bytes, convert to pages */
2384         if (val >= ONE_MB_BRW_SIZE)
2385                 val >>= PAGE_SHIFT;
2386
2387         LPROCFS_CLIMP_CHECK(dev);
2388
2389         chunk_mask = ~((1 << (cli->cl_chunkbits - PAGE_SHIFT)) - 1);
2390         /* max_pages_per_rpc must be chunk aligned */
2391         val = (val + ~chunk_mask) & chunk_mask;
2392         if (val == 0 || (ocd->ocd_brw_size != 0 &&
2393                          val > ocd->ocd_brw_size >> PAGE_SHIFT)) {
2394                 LPROCFS_CLIMP_EXIT(dev);
2395                 return -ERANGE;
2396         }
2397         spin_lock(&cli->cl_loi_list_lock);
2398         cli->cl_max_pages_per_rpc = val;
2399         client_adjust_max_dirty(cli);
2400         spin_unlock(&cli->cl_loi_list_lock);
2401
2402         LPROCFS_CLIMP_EXIT(dev);
2403         return count;
2404 }
2405 EXPORT_SYMBOL(lprocfs_obd_max_pages_per_rpc_seq_write);
2406
2407 ssize_t short_io_bytes_show(struct kobject *kobj, struct attribute *attr,
2408                             char *buf)
2409 {
2410         struct obd_device *dev = container_of(kobj, struct obd_device,
2411                                               obd_kset.kobj);
2412         struct client_obd *cli = &dev->u.cli;
2413         int rc;
2414
2415         spin_lock(&cli->cl_loi_list_lock);
2416         rc = sprintf(buf, "%d\n", cli->cl_short_io_bytes);
2417         spin_unlock(&cli->cl_loi_list_lock);
2418         return rc;
2419 }
2420 EXPORT_SYMBOL(short_io_bytes_show);
2421
2422 /* Used to catch people who think they're specifying pages. */
2423 #define MIN_SHORT_IO_BYTES 64
2424
2425 ssize_t short_io_bytes_store(struct kobject *kobj, struct attribute *attr,
2426                              const char *buffer, size_t count)
2427 {
2428         struct obd_device *dev = container_of(kobj, struct obd_device,
2429                                               obd_kset.kobj);
2430         struct client_obd *cli = &dev->u.cli;
2431         u32 val;
2432         int rc;
2433
2434         LPROCFS_CLIMP_CHECK(dev);
2435
2436         rc = kstrtouint(buffer, 0, &val);
2437         if (rc)
2438                 GOTO(out, rc);
2439
2440         if (val < MIN_SHORT_IO_BYTES || val > OBD_MAX_SHORT_IO_BYTES)
2441                 GOTO(out, rc = -ERANGE);
2442
2443         rc = count;
2444
2445         spin_lock(&cli->cl_loi_list_lock);
2446         if (val > (cli->cl_max_pages_per_rpc << PAGE_SHIFT))
2447                 rc = -ERANGE;
2448         else
2449                 cli->cl_short_io_bytes = val;
2450         spin_unlock(&cli->cl_loi_list_lock);
2451
2452 out:
2453         LPROCFS_CLIMP_EXIT(dev);
2454         return rc;
2455 }
2456 EXPORT_SYMBOL(short_io_bytes_store);
2457
2458 int lprocfs_wr_root_squash(const char __user *buffer, unsigned long count,
2459                            struct root_squash_info *squash, char *name)
2460 {
2461         int rc;
2462         char kernbuf[64], *tmp, *errmsg;
2463         unsigned long uid, gid;
2464         ENTRY;
2465
2466         if (count >= sizeof(kernbuf)) {
2467                 errmsg = "string too long";
2468                 GOTO(failed_noprint, rc = -EINVAL);
2469         }
2470         if (copy_from_user(kernbuf, buffer, count)) {
2471                 errmsg = "bad address";
2472                 GOTO(failed_noprint, rc = -EFAULT);
2473         }
2474         kernbuf[count] = '\0';
2475
2476         /* look for uid gid separator */
2477         tmp = strchr(kernbuf, ':');
2478         if (tmp == NULL) {
2479                 errmsg = "needs uid:gid format";
2480                 GOTO(failed, rc = -EINVAL);
2481         }
2482         *tmp = '\0';
2483         tmp++;
2484
2485         /* parse uid */
2486         if (kstrtoul(kernbuf, 0, &uid) != 0) {
2487                 errmsg = "bad uid";
2488                 GOTO(failed, rc = -EINVAL);
2489         }
2490
2491         /* parse gid */
2492         if (kstrtoul(tmp, 0, &gid) != 0) {
2493                 errmsg = "bad gid";
2494                 GOTO(failed, rc = -EINVAL);
2495         }
2496
2497         squash->rsi_uid = uid;
2498         squash->rsi_gid = gid;
2499
2500         LCONSOLE_INFO("%s: root_squash is set to %u:%u\n",
2501                       name, squash->rsi_uid, squash->rsi_gid);
2502         RETURN(count);
2503
2504 failed:
2505         if (tmp != NULL) {
2506                 tmp--;
2507                 *tmp = ':';
2508         }
2509         CWARN("%s: failed to set root_squash to \"%s\", %s, rc = %d\n",
2510               name, kernbuf, errmsg, rc);
2511         RETURN(rc);
2512 failed_noprint:
2513         CWARN("%s: failed to set root_squash due to %s, rc = %d\n",
2514               name, errmsg, rc);
2515         RETURN(rc);
2516 }
2517 EXPORT_SYMBOL(lprocfs_wr_root_squash);
2518
2519
2520 int lprocfs_wr_nosquash_nids(const char __user *buffer, unsigned long count,
2521                              struct root_squash_info *squash, char *name)
2522 {
2523         int rc;
2524         char *kernbuf = NULL;
2525         char *errmsg;
2526         struct list_head tmp;
2527         int len = count;
2528         ENTRY;
2529
2530         if (count > 4096) {
2531                 errmsg = "string too long";
2532                 GOTO(failed, rc = -EINVAL);
2533         }
2534
2535         OBD_ALLOC(kernbuf, count + 1);
2536         if (kernbuf == NULL) {
2537                 errmsg = "no memory";
2538                 GOTO(failed, rc = -ENOMEM);
2539         }
2540         if (copy_from_user(kernbuf, buffer, count)) {
2541                 errmsg = "bad address";
2542                 GOTO(failed, rc = -EFAULT);
2543         }
2544         kernbuf[count] = '\0';
2545
2546         if (count > 0 && kernbuf[count - 1] == '\n')
2547                 len = count - 1;
2548
2549         if ((len == 4 && strncmp(kernbuf, "NONE", len) == 0) ||
2550             (len == 5 && strncmp(kernbuf, "clear", len) == 0)) {
2551                 /* empty string is special case */
2552                 down_write(&squash->rsi_sem);
2553                 if (!list_empty(&squash->rsi_nosquash_nids))
2554                         cfs_free_nidlist(&squash->rsi_nosquash_nids);
2555                 up_write(&squash->rsi_sem);
2556                 LCONSOLE_INFO("%s: nosquash_nids is cleared\n", name);
2557                 OBD_FREE(kernbuf, count + 1);
2558                 RETURN(count);
2559         }
2560
2561         INIT_LIST_HEAD(&tmp);
2562         if (cfs_parse_nidlist(kernbuf, count, &tmp) <= 0) {
2563                 errmsg = "can't parse";
2564                 GOTO(failed, rc = -EINVAL);
2565         }
2566         LCONSOLE_INFO("%s: nosquash_nids set to %s\n",
2567                       name, kernbuf);
2568         OBD_FREE(kernbuf, count + 1);
2569         kernbuf = NULL;
2570
2571         down_write(&squash->rsi_sem);
2572         if (!list_empty(&squash->rsi_nosquash_nids))
2573                 cfs_free_nidlist(&squash->rsi_nosquash_nids);
2574         list_splice(&tmp, &squash->rsi_nosquash_nids);
2575         up_write(&squash->rsi_sem);
2576
2577         RETURN(count);
2578
2579 failed:
2580         if (kernbuf) {
2581                 CWARN("%s: failed to set nosquash_nids to \"%s\", %s rc = %d\n",
2582                       name, kernbuf, errmsg, rc);
2583                 OBD_FREE(kernbuf, count + 1);
2584         } else {
2585                 CWARN("%s: failed to set nosquash_nids due to %s rc = %d\n",
2586                       name, errmsg, rc);
2587         }
2588         RETURN(rc);
2589 }
2590 EXPORT_SYMBOL(lprocfs_wr_nosquash_nids);
2591
2592 #endif /* CONFIG_PROC_FS*/