Whamcloud - gitweb
LU-10114 hsm: add OBD_CONNECT2_ARCHIVE_ID_ARRAY to pass archive_id lists in array
[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         "archive_id_array",     /* 0x100 */
812         NULL
813 };
814
815 void obd_connect_seq_flags2str(struct seq_file *m, __u64 flags, __u64 flags2,
816                                const char *sep)
817 {
818         bool first = true;
819         __u64 mask;
820         int i;
821
822         for (i = 0, mask = 1; i < 64; i++, mask <<= 1) {
823                 if (flags & mask) {
824                         seq_printf(m, "%s%s",
825                                    first ? "" : sep, obd_connect_names[i]);
826                         first = false;
827                 }
828         }
829
830         if (flags & ~(mask - 1)) {
831                 seq_printf(m, "%sunknown_%#llx",
832                            first ? "" : sep, flags & ~(mask - 1));
833                 first = false;
834         }
835
836         if (!(flags & OBD_CONNECT_FLAGS2) || flags2 == 0)
837                 return;
838
839         for (i = 64, mask = 1; obd_connect_names[i] != NULL; i++, mask <<= 1) {
840                 if (flags2 & mask) {
841                         seq_printf(m, "%s%s",
842                                    first ? "" : sep, obd_connect_names[i]);
843                         first = false;
844                 }
845         }
846
847         if (flags2 & ~(mask - 1)) {
848                 seq_printf(m, "%sunknown2_%#llx",
849                            first ? "" : sep, flags2 & ~(mask - 1));
850                 first = false;
851         }
852 }
853 EXPORT_SYMBOL(obd_connect_seq_flags2str);
854
855 int obd_connect_flags2str(char *page, int count, __u64 flags, __u64 flags2,
856                           const char *sep)
857 {
858         __u64 mask;
859         int i, ret = 0;
860
861         for (i = 0, mask = 1; i < 64; i++, mask <<= 1) {
862                 if (flags & mask)
863                         ret += snprintf(page + ret, count - ret, "%s%s",
864                                         ret ? sep : "", obd_connect_names[i]);
865         }
866
867         if (flags & ~(mask - 1))
868                 ret += snprintf(page + ret, count - ret,
869                                 "%sunknown_%#llx",
870                                 ret ? sep : "", flags & ~(mask - 1));
871
872         if (!(flags & OBD_CONNECT_FLAGS2) || flags2 == 0)
873                 return ret;
874
875         for (i = 64, mask = 1; obd_connect_names[i] != NULL; i++, mask <<= 1) {
876                 if (flags2 & mask)
877                         ret += snprintf(page + ret, count - ret, "%s%s",
878                                         ret ? sep : "", obd_connect_names[i]);
879         }
880
881         if (flags2 & ~(mask - 1))
882                 ret += snprintf(page + ret, count - ret,
883                                 "%sunknown2_%#llx",
884                                 ret ? sep : "", flags2 & ~(mask - 1));
885
886         return ret;
887 }
888 EXPORT_SYMBOL(obd_connect_flags2str);
889
890 void
891 obd_connect_data_seqprint(struct seq_file *m, struct obd_connect_data *ocd)
892 {
893         __u64 flags;
894
895         LASSERT(ocd != NULL);
896         flags = ocd->ocd_connect_flags;
897
898         seq_printf(m, "    connect_data:\n"
899                    "       flags: %#llx\n"
900                    "       instance: %u\n",
901                    ocd->ocd_connect_flags,
902                    ocd->ocd_instance);
903         if (flags & OBD_CONNECT_VERSION)
904                 seq_printf(m, "       target_version: %u.%u.%u.%u\n",
905                            OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
906                            OBD_OCD_VERSION_MINOR(ocd->ocd_version),
907                            OBD_OCD_VERSION_PATCH(ocd->ocd_version),
908                            OBD_OCD_VERSION_FIX(ocd->ocd_version));
909         if (flags & OBD_CONNECT_MDS)
910                 seq_printf(m, "       mdt_index: %d\n", ocd->ocd_group);
911         if (flags & OBD_CONNECT_GRANT)
912                 seq_printf(m, "       initial_grant: %d\n", ocd->ocd_grant);
913         if (flags & OBD_CONNECT_INDEX)
914                 seq_printf(m, "       target_index: %u\n", ocd->ocd_index);
915         if (flags & OBD_CONNECT_BRW_SIZE)
916                 seq_printf(m, "       max_brw_size: %d\n", ocd->ocd_brw_size);
917         if (flags & OBD_CONNECT_IBITS)
918                 seq_printf(m, "       ibits_known: %#llx\n",
919                            ocd->ocd_ibits_known);
920         if (flags & OBD_CONNECT_GRANT_PARAM)
921                 seq_printf(m, "       grant_block_size: %d\n"
922                            "       grant_inode_size: %d\n"
923                            "       grant_max_extent_size: %d\n"
924                            "       grant_extent_tax: %d\n",
925                            1 << ocd->ocd_grant_blkbits,
926                            1 << ocd->ocd_grant_inobits,
927                            ocd->ocd_grant_max_blks << ocd->ocd_grant_blkbits,
928                            ocd->ocd_grant_tax_kb << 10);
929         if (flags & OBD_CONNECT_TRANSNO)
930                 seq_printf(m, "       first_transno: %#llx\n",
931                            ocd->ocd_transno);
932         if (flags & OBD_CONNECT_CKSUM)
933                 seq_printf(m, "       cksum_types: %#x\n",
934                            ocd->ocd_cksum_types);
935         if (flags & OBD_CONNECT_MAX_EASIZE)
936                 seq_printf(m, "       max_easize: %d\n", ocd->ocd_max_easize);
937         if (flags & OBD_CONNECT_MAXBYTES)
938                 seq_printf(m, "       max_object_bytes: %llu\n",
939                            ocd->ocd_maxbytes);
940         if (flags & OBD_CONNECT_MULTIMODRPCS)
941                 seq_printf(m, "       max_mod_rpcs: %hu\n",
942                            ocd->ocd_maxmodrpcs);
943 }
944
945 int lprocfs_import_seq_show(struct seq_file *m, void *data)
946 {
947         char                            nidstr[LNET_NIDSTR_SIZE];
948         struct lprocfs_counter          ret;
949         struct lprocfs_counter_header   *header;
950         struct obd_device               *obd    = (struct obd_device *)data;
951         struct obd_import               *imp;
952         struct obd_import_conn          *conn;
953         struct obd_connect_data         *ocd;
954         int                             j;
955         int                             k;
956         int                             rw      = 0;
957
958         LASSERT(obd != NULL);
959         LPROCFS_CLIMP_CHECK(obd);
960         imp = obd->u.cli.cl_import;
961         ocd = &imp->imp_connect_data;
962
963         seq_printf(m, "import:\n"
964                    "    name: %s\n"
965                    "    target: %s\n"
966                    "    state: %s\n"
967                    "    connect_flags: [ ",
968                    obd->obd_name,
969                    obd2cli_tgt(obd),
970                    ptlrpc_import_state_name(imp->imp_state));
971         obd_connect_seq_flags2str(m, imp->imp_connect_data.ocd_connect_flags,
972                                   imp->imp_connect_data.ocd_connect_flags2,
973                                   ", ");
974         seq_printf(m, " ]\n");
975         obd_connect_data_seqprint(m, ocd);
976         seq_printf(m, "    import_flags: [ ");
977         obd_import_flags2str(imp, m);
978
979         seq_printf(m, " ]\n"
980                    "    connection:\n"
981                    "       failover_nids: [ ");
982         spin_lock(&imp->imp_lock);
983         j = 0;
984         list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
985                 libcfs_nid2str_r(conn->oic_conn->c_peer.nid,
986                                  nidstr, sizeof(nidstr));
987                 seq_printf(m, "%s%s", j ? ", " : "", nidstr);
988                 j++;
989         }
990         if (imp->imp_connection != NULL)
991                 libcfs_nid2str_r(imp->imp_connection->c_peer.nid,
992                                  nidstr, sizeof(nidstr));
993         else
994                 strncpy(nidstr, "<none>", sizeof(nidstr));
995         seq_printf(m, " ]\n"
996                    "       current_connection: %s\n"
997                    "       connection_attempts: %u\n"
998                    "       generation: %u\n"
999                    "       in-progress_invalidations: %u\n",
1000                    nidstr,
1001                    imp->imp_conn_cnt,
1002                    imp->imp_generation,
1003                    atomic_read(&imp->imp_inval_count));
1004         spin_unlock(&imp->imp_lock);
1005
1006         if (obd->obd_svc_stats == NULL)
1007                 goto out_climp;
1008
1009         header = &obd->obd_svc_stats->ls_cnt_header[PTLRPC_REQWAIT_CNTR];
1010         lprocfs_stats_collect(obd->obd_svc_stats, PTLRPC_REQWAIT_CNTR, &ret);
1011         if (ret.lc_count != 0) {
1012                 /* first argument to do_div MUST be __u64 */
1013                 __u64 sum = ret.lc_sum;
1014                 do_div(sum, ret.lc_count);
1015                 ret.lc_sum = sum;
1016         } else
1017                 ret.lc_sum = 0;
1018         seq_printf(m, "    rpcs:\n"
1019                    "       inflight: %u\n"
1020                    "       unregistering: %u\n"
1021                    "       timeouts: %u\n"
1022                    "       avg_waittime: %llu %s\n",
1023                    atomic_read(&imp->imp_inflight),
1024                    atomic_read(&imp->imp_unregistering),
1025                    atomic_read(&imp->imp_timeouts),
1026                    ret.lc_sum, header->lc_units);
1027
1028         k = 0;
1029         for(j = 0; j < IMP_AT_MAX_PORTALS; j++) {
1030                 if (imp->imp_at.iat_portal[j] == 0)
1031                         break;
1032                 k = max_t(unsigned int, k,
1033                           at_get(&imp->imp_at.iat_service_estimate[j]));
1034         }
1035         seq_printf(m, "    service_estimates:\n"
1036                    "       services: %u sec\n"
1037                    "       network: %u sec\n",
1038                    k,
1039                    at_get(&imp->imp_at.iat_net_latency));
1040
1041         seq_printf(m, "    transactions:\n"
1042                    "       last_replay: %llu\n"
1043                    "       peer_committed: %llu\n"
1044                    "       last_checked: %llu\n",
1045                    imp->imp_last_replay_transno,
1046                    imp->imp_peer_committed_transno,
1047                    imp->imp_last_transno_checked);
1048
1049         /* avg data rates */
1050         for (rw = 0; rw <= 1; rw++) {
1051                 lprocfs_stats_collect(obd->obd_svc_stats,
1052                                       PTLRPC_LAST_CNTR + BRW_READ_BYTES + rw,
1053                                       &ret);
1054                 if (ret.lc_sum > 0 && ret.lc_count > 0) {
1055                         /* first argument to do_div MUST be __u64 */
1056                         __u64 sum = ret.lc_sum;
1057                         do_div(sum, ret.lc_count);
1058                         ret.lc_sum = sum;
1059                         seq_printf(m, "    %s_data_averages:\n"
1060                                    "       bytes_per_rpc: %llu\n",
1061                                    rw ? "write" : "read",
1062                                    ret.lc_sum);
1063                 }
1064                 k = (int)ret.lc_sum;
1065                 j = opcode_offset(OST_READ + rw) + EXTRA_MAX_OPCODES;
1066                 header = &obd->obd_svc_stats->ls_cnt_header[j];
1067                 lprocfs_stats_collect(obd->obd_svc_stats, j, &ret);
1068                 if (ret.lc_sum > 0 && ret.lc_count != 0) {
1069                         /* first argument to do_div MUST be __u64 */
1070                         __u64 sum = ret.lc_sum;
1071                         do_div(sum, ret.lc_count);
1072                         ret.lc_sum = sum;
1073                         seq_printf(m, "       %s_per_rpc: %llu\n",
1074                                    header->lc_units, ret.lc_sum);
1075                         j = (int)ret.lc_sum;
1076                         if (j > 0)
1077                                 seq_printf(m, "       MB_per_sec: %u.%.02u\n",
1078                                            k / j, (100 * k / j) % 100);
1079                 }
1080         }
1081
1082 out_climp:
1083         LPROCFS_CLIMP_EXIT(obd);
1084         return 0;
1085 }
1086 EXPORT_SYMBOL(lprocfs_import_seq_show);
1087
1088 int lprocfs_state_seq_show(struct seq_file *m, void *data)
1089 {
1090         struct obd_device *obd = (struct obd_device *)data;
1091         struct obd_import *imp;
1092         int j, k;
1093
1094         LASSERT(obd != NULL);
1095         LPROCFS_CLIMP_CHECK(obd);
1096         imp = obd->u.cli.cl_import;
1097
1098         seq_printf(m, "current_state: %s\n",
1099                    ptlrpc_import_state_name(imp->imp_state));
1100         seq_printf(m, "state_history:\n");
1101         k = imp->imp_state_hist_idx;
1102         for (j = 0; j < IMP_STATE_HIST_LEN; j++) {
1103                 struct import_state_hist *ish =
1104                         &imp->imp_state_hist[(k + j) % IMP_STATE_HIST_LEN];
1105                 if (ish->ish_state == 0)
1106                         continue;
1107                 seq_printf(m, " - [ %lld, %s ]\n", (s64)ish->ish_time,
1108                            ptlrpc_import_state_name(ish->ish_state));
1109         }
1110
1111         LPROCFS_CLIMP_EXIT(obd);
1112         return 0;
1113 }
1114 EXPORT_SYMBOL(lprocfs_state_seq_show);
1115
1116 int lprocfs_at_hist_helper(struct seq_file *m, struct adaptive_timeout *at)
1117 {
1118         int i;
1119         for (i = 0; i < AT_BINS; i++)
1120                 seq_printf(m, "%3u ", at->at_hist[i]);
1121         seq_printf(m, "\n");
1122         return 0;
1123 }
1124 EXPORT_SYMBOL(lprocfs_at_hist_helper);
1125
1126 /* See also ptlrpc_lprocfs_timeouts_show_seq */
1127 int lprocfs_timeouts_seq_show(struct seq_file *m, void *data)
1128 {
1129         struct obd_device *obd = (struct obd_device *)data;
1130         struct obd_import *imp;
1131         unsigned int cur, worst;
1132         time64_t now, worstt;
1133         int i;
1134
1135         LASSERT(obd != NULL);
1136         LPROCFS_CLIMP_CHECK(obd);
1137         imp = obd->u.cli.cl_import;
1138
1139         now = ktime_get_real_seconds();
1140
1141         /* Some network health info for kicks */
1142         seq_printf(m, "%-10s : %lld, %llds ago\n",
1143                    "last reply", (s64)imp->imp_last_reply_time,
1144                    (s64)(now - imp->imp_last_reply_time));
1145
1146         cur = at_get(&imp->imp_at.iat_net_latency);
1147         worst = imp->imp_at.iat_net_latency.at_worst_ever;
1148         worstt = imp->imp_at.iat_net_latency.at_worst_time;
1149         seq_printf(m, "%-10s : cur %3u  worst %3u (at %lld, %llds ago) ",
1150                    "network", cur, worst, (s64)worstt, (s64)(now - worstt));
1151         lprocfs_at_hist_helper(m, &imp->imp_at.iat_net_latency);
1152
1153         for(i = 0; i < IMP_AT_MAX_PORTALS; i++) {
1154                 if (imp->imp_at.iat_portal[i] == 0)
1155                         break;
1156                 cur = at_get(&imp->imp_at.iat_service_estimate[i]);
1157                 worst = imp->imp_at.iat_service_estimate[i].at_worst_ever;
1158                 worstt = imp->imp_at.iat_service_estimate[i].at_worst_time;
1159                 seq_printf(m, "portal %-2d  : cur %3u  worst %3u (at %lld, %llds ago) ",
1160                            imp->imp_at.iat_portal[i], cur, worst, (s64)worstt,
1161                            (s64)(now - worstt));
1162                 lprocfs_at_hist_helper(m, &imp->imp_at.iat_service_estimate[i]);
1163         }
1164
1165         LPROCFS_CLIMP_EXIT(obd);
1166         return 0;
1167 }
1168 EXPORT_SYMBOL(lprocfs_timeouts_seq_show);
1169
1170 int lprocfs_connect_flags_seq_show(struct seq_file *m, void *data)
1171 {
1172         struct obd_device *obd = data;
1173         __u64 flags;
1174         __u64 flags2;
1175
1176         LPROCFS_CLIMP_CHECK(obd);
1177         flags = obd->u.cli.cl_import->imp_connect_data.ocd_connect_flags;
1178         flags2 = obd->u.cli.cl_import->imp_connect_data.ocd_connect_flags2;
1179         seq_printf(m, "flags=%#llx\n", flags);
1180         seq_printf(m, "flags2=%#llx\n", flags2);
1181         obd_connect_seq_flags2str(m, flags, flags2, "\n");
1182         seq_printf(m, "\n");
1183         LPROCFS_CLIMP_EXIT(obd);
1184         return 0;
1185 }
1186 EXPORT_SYMBOL(lprocfs_connect_flags_seq_show);
1187
1188 static const struct attribute *obd_def_uuid_attrs[] = {
1189         &lustre_attr_uuid.attr,
1190         NULL,
1191 };
1192
1193 static const struct attribute *obd_def_attrs[] = {
1194         &lustre_attr_blocksize.attr,
1195         &lustre_attr_kbytestotal.attr,
1196         &lustre_attr_kbytesfree.attr,
1197         &lustre_attr_kbytesavail.attr,
1198         &lustre_attr_filestotal.attr,
1199         &lustre_attr_filesfree.attr,
1200         &lustre_attr_uuid.attr,
1201         NULL,
1202 };
1203
1204 static void obd_sysfs_release(struct kobject *kobj)
1205 {
1206         struct obd_device *obd = container_of(kobj, struct obd_device,
1207                                               obd_kset.kobj);
1208
1209         complete(&obd->obd_kobj_unregister);
1210 }
1211
1212 int lprocfs_obd_setup(struct obd_device *obd, bool uuid_only)
1213 {
1214         struct lprocfs_vars *debugfs_vars = NULL;
1215         int rc;
1216
1217         if (!obd || obd->obd_magic != OBD_DEVICE_MAGIC)
1218                 return -ENODEV;
1219
1220         rc = kobject_set_name(&obd->obd_kset.kobj, "%s", obd->obd_name);
1221         if (rc)
1222                 return rc;
1223
1224         obd->obd_ktype.sysfs_ops = &lustre_sysfs_ops;
1225         obd->obd_ktype.release = obd_sysfs_release;
1226
1227         obd->obd_kset.kobj.parent = obd->obd_type->typ_kobj;
1228         obd->obd_kset.kobj.ktype = &obd->obd_ktype;
1229         init_completion(&obd->obd_kobj_unregister);
1230         rc = kset_register(&obd->obd_kset);
1231         if (rc)
1232                 return rc;
1233
1234         if (uuid_only)
1235                 obd->obd_attrs = obd_def_uuid_attrs;
1236         else
1237                 obd->obd_attrs = obd_def_attrs;
1238
1239         rc = sysfs_create_files(&obd->obd_kset.kobj, obd->obd_attrs);
1240         if (rc) {
1241                 kset_unregister(&obd->obd_kset);
1242                 return rc;
1243         }
1244
1245         if (!obd->obd_type->typ_procroot)
1246                 debugfs_vars = obd->obd_vars;
1247         obd->obd_debugfs_entry = ldebugfs_register(obd->obd_name,
1248                                                    obd->obd_type->typ_debugfs_entry,
1249                                                    debugfs_vars, obd);
1250         if (IS_ERR_OR_NULL(obd->obd_debugfs_entry)) {
1251                 rc = obd->obd_debugfs_entry ? PTR_ERR(obd->obd_debugfs_entry)
1252                                             : -ENOMEM;
1253                 CERROR("error %d setting up debugfs for %s\n",
1254                        rc, obd->obd_name);
1255                 obd->obd_debugfs_entry = NULL;
1256
1257                 sysfs_remove_files(&obd->obd_kset.kobj, obd->obd_attrs);
1258                 obd->obd_attrs = NULL;
1259                 kset_unregister(&obd->obd_kset);
1260                 return rc;
1261         }
1262
1263         if (obd->obd_proc_entry || !obd->obd_type->typ_procroot)
1264                 GOTO(already_registered, rc);
1265
1266         obd->obd_proc_entry = lprocfs_register(obd->obd_name,
1267                                                obd->obd_type->typ_procroot,
1268                                                obd->obd_vars, obd);
1269         if (IS_ERR(obd->obd_proc_entry)) {
1270                 rc = PTR_ERR(obd->obd_proc_entry);
1271                 CERROR("error %d setting up lprocfs for %s\n",rc,obd->obd_name);
1272                 obd->obd_proc_entry = NULL;
1273
1274                 ldebugfs_remove(&obd->obd_debugfs_entry);
1275                 sysfs_remove_files(&obd->obd_kset.kobj, obd->obd_attrs);
1276                 obd->obd_attrs = NULL;
1277                 kset_unregister(&obd->obd_kset);
1278                 return rc;
1279         }
1280 already_registered:
1281         return rc;
1282 }
1283 EXPORT_SYMBOL(lprocfs_obd_setup);
1284
1285 int lprocfs_obd_cleanup(struct obd_device *obd)
1286 {
1287         if (!obd)
1288                 return -EINVAL;
1289
1290         if (obd->obd_proc_exports_entry) {
1291                 /* Should be no exports left */
1292                 lprocfs_remove(&obd->obd_proc_exports_entry);
1293                 obd->obd_proc_exports_entry = NULL;
1294         }
1295
1296         if (obd->obd_proc_entry) {
1297                 lprocfs_remove(&obd->obd_proc_entry);
1298                 obd->obd_proc_entry = NULL;
1299         }
1300
1301         if (!IS_ERR_OR_NULL(obd->obd_debugfs_entry))
1302                 ldebugfs_remove(&obd->obd_debugfs_entry);
1303
1304         /* obd device never allocated a kset */
1305         if (!obd->obd_kset.kobj.state_initialized)
1306                 return 0;
1307
1308         if (obd->obd_attrs) {
1309                 sysfs_remove_files(&obd->obd_kset.kobj, obd->obd_attrs);
1310                 obd->obd_attrs = NULL;
1311         }
1312
1313         kset_unregister(&obd->obd_kset);
1314         wait_for_completion(&obd->obd_kobj_unregister);
1315         return 0;
1316 }
1317 EXPORT_SYMBOL(lprocfs_obd_cleanup);
1318
1319 int lprocfs_stats_alloc_one(struct lprocfs_stats *stats, unsigned int cpuid)
1320 {
1321         struct lprocfs_counter  *cntr;
1322         unsigned int            percpusize;
1323         int                     rc = -ENOMEM;
1324         unsigned long           flags = 0;
1325         int                     i;
1326
1327         LASSERT(stats->ls_percpu[cpuid] == NULL);
1328         LASSERT((stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) == 0);
1329
1330         percpusize = lprocfs_stats_counter_size(stats);
1331         LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[cpuid], percpusize);
1332         if (stats->ls_percpu[cpuid] != NULL) {
1333                 rc = 0;
1334                 if (unlikely(stats->ls_biggest_alloc_num <= cpuid)) {
1335                         if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1336                                 spin_lock_irqsave(&stats->ls_lock, flags);
1337                         else
1338                                 spin_lock(&stats->ls_lock);
1339                         if (stats->ls_biggest_alloc_num <= cpuid)
1340                                 stats->ls_biggest_alloc_num = cpuid + 1;
1341                         if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) {
1342                                 spin_unlock_irqrestore(&stats->ls_lock, flags);
1343                         } else {
1344                                 spin_unlock(&stats->ls_lock);
1345                         }
1346                 }
1347                 /* initialize the ls_percpu[cpuid] non-zero counter */
1348                 for (i = 0; i < stats->ls_num; ++i) {
1349                         cntr = lprocfs_stats_counter_get(stats, cpuid, i);
1350                         cntr->lc_min = LC_MIN_INIT;
1351                 }
1352         }
1353         return rc;
1354 }
1355
1356 struct lprocfs_stats *lprocfs_alloc_stats(unsigned int num,
1357                                           enum lprocfs_stats_flags flags)
1358 {
1359         struct lprocfs_stats    *stats;
1360         unsigned int            num_entry;
1361         unsigned int            percpusize = 0;
1362         int                     i;
1363
1364         if (num == 0)
1365                 return NULL;
1366
1367         if (lprocfs_no_percpu_stats != 0)
1368                 flags |= LPROCFS_STATS_FLAG_NOPERCPU;
1369
1370         if (flags & LPROCFS_STATS_FLAG_NOPERCPU)
1371                 num_entry = 1;
1372         else
1373                 num_entry = num_possible_cpus();
1374
1375         /* alloc percpu pointers for all possible cpu slots */
1376         LIBCFS_ALLOC(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1377         if (stats == NULL)
1378                 return NULL;
1379
1380         stats->ls_num = num;
1381         stats->ls_flags = flags;
1382         spin_lock_init(&stats->ls_lock);
1383
1384         /* alloc num of counter headers */
1385         LIBCFS_ALLOC(stats->ls_cnt_header,
1386                      stats->ls_num * sizeof(struct lprocfs_counter_header));
1387         if (stats->ls_cnt_header == NULL)
1388                 goto fail;
1389
1390         if ((flags & LPROCFS_STATS_FLAG_NOPERCPU) != 0) {
1391                 /* contains only one set counters */
1392                 percpusize = lprocfs_stats_counter_size(stats);
1393                 LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[0], percpusize);
1394                 if (stats->ls_percpu[0] == NULL)
1395                         goto fail;
1396                 stats->ls_biggest_alloc_num = 1;
1397         } else if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0) {
1398                 /* alloc all percpu data, currently only obd_memory use this */
1399                 for (i = 0; i < num_entry; ++i)
1400                         if (lprocfs_stats_alloc_one(stats, i) < 0)
1401                                 goto fail;
1402         }
1403
1404         return stats;
1405
1406 fail:
1407         lprocfs_free_stats(&stats);
1408         return NULL;
1409 }
1410 EXPORT_SYMBOL(lprocfs_alloc_stats);
1411
1412 void lprocfs_free_stats(struct lprocfs_stats **statsh)
1413 {
1414         struct lprocfs_stats *stats = *statsh;
1415         unsigned int num_entry;
1416         unsigned int percpusize;
1417         unsigned int i;
1418
1419         if (stats == NULL || stats->ls_num == 0)
1420                 return;
1421         *statsh = NULL;
1422
1423         if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU)
1424                 num_entry = 1;
1425         else
1426                 num_entry = num_possible_cpus();
1427
1428         percpusize = lprocfs_stats_counter_size(stats);
1429         for (i = 0; i < num_entry; i++)
1430                 if (stats->ls_percpu[i] != NULL)
1431                         LIBCFS_FREE(stats->ls_percpu[i], percpusize);
1432         if (stats->ls_cnt_header != NULL)
1433                 LIBCFS_FREE(stats->ls_cnt_header, stats->ls_num *
1434                                         sizeof(struct lprocfs_counter_header));
1435         LIBCFS_FREE(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1436 }
1437 EXPORT_SYMBOL(lprocfs_free_stats);
1438
1439 u64 lprocfs_stats_collector(struct lprocfs_stats *stats, int idx,
1440                             enum lprocfs_fields_flags field)
1441 {
1442         unsigned long flags = 0;
1443         unsigned int num_cpu;
1444         unsigned int i;
1445         u64 ret = 0;
1446
1447         LASSERT(stats);
1448
1449         num_cpu = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1450         for (i = 0; i < num_cpu; i++) {
1451                 struct lprocfs_counter *cntr;
1452
1453                 if (!stats->ls_percpu[i])
1454                         continue;
1455
1456                 cntr = lprocfs_stats_counter_get(stats, i, idx);
1457                 ret += lprocfs_read_helper(cntr, &stats->ls_cnt_header[idx],
1458                                            stats->ls_flags, field);
1459         }
1460         lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1461         return ret;
1462 }
1463 EXPORT_SYMBOL(lprocfs_stats_collector);
1464
1465 void lprocfs_clear_stats(struct lprocfs_stats *stats)
1466 {
1467         struct lprocfs_counter          *percpu_cntr;
1468         int                             i;
1469         int                             j;
1470         unsigned int                    num_entry;
1471         unsigned long                   flags = 0;
1472
1473         num_entry = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1474
1475         for (i = 0; i < num_entry; i++) {
1476                 if (stats->ls_percpu[i] == NULL)
1477                         continue;
1478                 for (j = 0; j < stats->ls_num; j++) {
1479                         percpu_cntr = lprocfs_stats_counter_get(stats, i, j);
1480                         percpu_cntr->lc_count           = 0;
1481                         percpu_cntr->lc_min             = LC_MIN_INIT;
1482                         percpu_cntr->lc_max             = 0;
1483                         percpu_cntr->lc_sumsquare       = 0;
1484                         percpu_cntr->lc_sum             = 0;
1485                         if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1486                                 percpu_cntr->lc_sum_irq = 0;
1487                 }
1488         }
1489
1490         lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1491 }
1492 EXPORT_SYMBOL(lprocfs_clear_stats);
1493
1494 static ssize_t lprocfs_stats_seq_write(struct file *file,
1495                                        const char __user *buf,
1496                                        size_t len, loff_t *off)
1497 {
1498         struct seq_file *seq = file->private_data;
1499         struct lprocfs_stats *stats = seq->private;
1500
1501         lprocfs_clear_stats(stats);
1502
1503         return len;
1504 }
1505
1506 static void *lprocfs_stats_seq_start(struct seq_file *p, loff_t *pos)
1507 {
1508         struct lprocfs_stats *stats = p->private;
1509
1510         return (*pos < stats->ls_num) ? pos : NULL;
1511 }
1512
1513 static void lprocfs_stats_seq_stop(struct seq_file *p, void *v)
1514 {
1515 }
1516
1517 static void *lprocfs_stats_seq_next(struct seq_file *p, void *v, loff_t *pos)
1518 {
1519         (*pos)++;
1520
1521         return lprocfs_stats_seq_start(p, pos);
1522 }
1523
1524 /* seq file export of one lprocfs counter */
1525 static int lprocfs_stats_seq_show(struct seq_file *p, void *v)
1526 {
1527         struct lprocfs_stats            *stats  = p->private;
1528         struct lprocfs_counter_header   *hdr;
1529         struct lprocfs_counter           ctr;
1530         int                              idx    = *(loff_t *)v;
1531
1532         if (idx == 0) {
1533                 struct timespec64 now;
1534
1535                 ktime_get_real_ts64(&now);
1536                 seq_printf(p, "%-25s %llu.%09lu secs.nsecs\n",
1537                            "snapshot_time", (s64)now.tv_sec, now.tv_nsec);
1538         }
1539
1540         hdr = &stats->ls_cnt_header[idx];
1541         lprocfs_stats_collect(stats, idx, &ctr);
1542
1543         if (ctr.lc_count == 0)
1544                 return 0;
1545
1546         seq_printf(p, "%-25s %lld samples [%s]", hdr->lc_name,
1547                    ctr.lc_count, hdr->lc_units);
1548
1549         if ((hdr->lc_config & LPROCFS_CNTR_AVGMINMAX) && ctr.lc_count > 0) {
1550                 seq_printf(p, " %lld %lld %lld",
1551                            ctr.lc_min, ctr.lc_max, ctr.lc_sum);
1552                 if (hdr->lc_config & LPROCFS_CNTR_STDDEV)
1553                         seq_printf(p, " %llu", ctr.lc_sumsquare);
1554         }
1555         seq_putc(p, '\n');
1556         return 0;
1557 }
1558
1559 static const struct seq_operations lprocfs_stats_seq_sops = {
1560         .start  = lprocfs_stats_seq_start,
1561         .stop   = lprocfs_stats_seq_stop,
1562         .next   = lprocfs_stats_seq_next,
1563         .show   = lprocfs_stats_seq_show,
1564 };
1565
1566 static int lprocfs_stats_seq_open(struct inode *inode, struct file *file)
1567 {
1568         struct seq_file *seq;
1569         int rc;
1570
1571         rc = LPROCFS_ENTRY_CHECK(inode);
1572         if (rc < 0)
1573                 return rc;
1574
1575         rc = seq_open(file, &lprocfs_stats_seq_sops);
1576         if (rc)
1577                 return rc;
1578         seq = file->private_data;
1579         seq->private = inode->i_private ? inode->i_private : PDE_DATA(inode);
1580         return 0;
1581 }
1582
1583 static const struct file_operations lprocfs_stats_seq_fops = {
1584         .owner   = THIS_MODULE,
1585         .open    = lprocfs_stats_seq_open,
1586         .read    = seq_read,
1587         .write   = lprocfs_stats_seq_write,
1588         .llseek  = seq_lseek,
1589         .release = lprocfs_seq_release,
1590 };
1591
1592 int ldebugfs_register_stats(struct dentry *parent, const char *name,
1593                             struct lprocfs_stats *stats)
1594 {
1595         struct dentry *entry;
1596
1597         LASSERT(!IS_ERR_OR_NULL(parent));
1598
1599         entry = debugfs_create_file(name, 0644, parent, stats,
1600                                     &lprocfs_stats_seq_fops);
1601         if (IS_ERR_OR_NULL(entry))
1602                 return entry ? PTR_ERR(entry) : -ENOMEM;
1603
1604         return 0;
1605 }
1606 EXPORT_SYMBOL_GPL(ldebugfs_register_stats);
1607
1608 int lprocfs_register_stats(struct proc_dir_entry *root, const char *name,
1609                            struct lprocfs_stats *stats)
1610 {
1611         struct proc_dir_entry *entry;
1612         LASSERT(root != NULL);
1613
1614         entry = proc_create_data(name, 0644, root,
1615                                  &lprocfs_stats_seq_fops, stats);
1616         if (entry == NULL)
1617                 return -ENOMEM;
1618         return 0;
1619 }
1620 EXPORT_SYMBOL(lprocfs_register_stats);
1621
1622 void lprocfs_counter_init(struct lprocfs_stats *stats, int index,
1623                           unsigned conf, const char *name, const char *units)
1624 {
1625         struct lprocfs_counter_header   *header;
1626         struct lprocfs_counter          *percpu_cntr;
1627         unsigned long                   flags = 0;
1628         unsigned int                    i;
1629         unsigned int                    num_cpu;
1630
1631         LASSERT(stats != NULL);
1632
1633         header = &stats->ls_cnt_header[index];
1634         LASSERTF(header != NULL, "Failed to allocate stats header:[%d]%s/%s\n",
1635                  index, name, units);
1636
1637         header->lc_config = conf;
1638         header->lc_name   = name;
1639         header->lc_units  = units;
1640
1641         num_cpu = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1642         for (i = 0; i < num_cpu; ++i) {
1643                 if (stats->ls_percpu[i] == NULL)
1644                         continue;
1645                 percpu_cntr = lprocfs_stats_counter_get(stats, i, index);
1646                 percpu_cntr->lc_count           = 0;
1647                 percpu_cntr->lc_min             = LC_MIN_INIT;
1648                 percpu_cntr->lc_max             = 0;
1649                 percpu_cntr->lc_sumsquare       = 0;
1650                 percpu_cntr->lc_sum             = 0;
1651                 if ((stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1652                         percpu_cntr->lc_sum_irq = 0;
1653         }
1654         lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1655 }
1656 EXPORT_SYMBOL(lprocfs_counter_init);
1657
1658 /* Note that we only init md counters for ops whose offset is less
1659  * than NUM_MD_STATS. This is explained in a comment in the definition
1660  * of struct md_ops. */
1661 #define LPROCFS_MD_OP_INIT(base, stats, op)                                    \
1662         do {                                                                   \
1663                 unsigned int _idx = base + MD_COUNTER_OFFSET(op);              \
1664                                                                                \
1665                 if (MD_COUNTER_OFFSET(op) < NUM_MD_STATS) {                    \
1666                         LASSERT(_idx < stats->ls_num);                         \
1667                         lprocfs_counter_init(stats, _idx, 0, #op, "reqs");     \
1668                 }                                                              \
1669         } while (0)
1670
1671 void lprocfs_init_mps_stats(int num_private_stats, struct lprocfs_stats *stats)
1672 {
1673         LPROCFS_MD_OP_INIT(num_private_stats, stats, get_root);
1674         LPROCFS_MD_OP_INIT(num_private_stats, stats, null_inode);
1675         LPROCFS_MD_OP_INIT(num_private_stats, stats, close);
1676         LPROCFS_MD_OP_INIT(num_private_stats, stats, create);
1677         LPROCFS_MD_OP_INIT(num_private_stats, stats, enqueue);
1678         LPROCFS_MD_OP_INIT(num_private_stats, stats, getattr);
1679         LPROCFS_MD_OP_INIT(num_private_stats, stats, getattr_name);
1680         LPROCFS_MD_OP_INIT(num_private_stats, stats, intent_lock);
1681         LPROCFS_MD_OP_INIT(num_private_stats, stats, link);
1682         LPROCFS_MD_OP_INIT(num_private_stats, stats, rename);
1683         LPROCFS_MD_OP_INIT(num_private_stats, stats, setattr);
1684         LPROCFS_MD_OP_INIT(num_private_stats, stats, fsync);
1685         LPROCFS_MD_OP_INIT(num_private_stats, stats, read_page);
1686         LPROCFS_MD_OP_INIT(num_private_stats, stats, unlink);
1687         LPROCFS_MD_OP_INIT(num_private_stats, stats, setxattr);
1688         LPROCFS_MD_OP_INIT(num_private_stats, stats, getxattr);
1689         LPROCFS_MD_OP_INIT(num_private_stats, stats, init_ea_size);
1690         LPROCFS_MD_OP_INIT(num_private_stats, stats, get_lustre_md);
1691         LPROCFS_MD_OP_INIT(num_private_stats, stats, free_lustre_md);
1692         LPROCFS_MD_OP_INIT(num_private_stats, stats, merge_attr);
1693         LPROCFS_MD_OP_INIT(num_private_stats, stats, set_open_replay_data);
1694         LPROCFS_MD_OP_INIT(num_private_stats, stats, clear_open_replay_data);
1695         LPROCFS_MD_OP_INIT(num_private_stats, stats, set_lock_data);
1696         LPROCFS_MD_OP_INIT(num_private_stats, stats, lock_match);
1697         LPROCFS_MD_OP_INIT(num_private_stats, stats, cancel_unused);
1698         LPROCFS_MD_OP_INIT(num_private_stats, stats, intent_getattr_async);
1699         LPROCFS_MD_OP_INIT(num_private_stats, stats, revalidate_lock);
1700 }
1701
1702 int lprocfs_alloc_md_stats(struct obd_device *obd,
1703                            unsigned int num_private_stats)
1704 {
1705         struct lprocfs_stats *stats;
1706         unsigned int num_stats;
1707         int rc, i;
1708
1709         CLASSERT(offsetof(struct md_ops, MD_STATS_FIRST_OP) == 0);
1710         CLASSERT(_MD_COUNTER_OFFSET(MD_STATS_FIRST_OP) == 0);
1711         CLASSERT(_MD_COUNTER_OFFSET(MD_STATS_LAST_OP) > 0);
1712
1713         /* TODO Ensure that this function is only used where
1714          * appropriate by adding an assertion to the effect that
1715          * obd->obd_type->typ_md_ops is not NULL. We can't do this now
1716          * because mdt_procfs_init() uses this function to allocate
1717          * the stats backing /proc/fs/lustre/mdt/.../md_stats but the
1718          * mdt layer does not use the md_ops interface. This is
1719          * confusing and a waste of memory. See LU-2484.
1720          */
1721         LASSERT(obd->obd_proc_entry != NULL);
1722         LASSERT(obd->obd_md_stats == NULL);
1723         LASSERT(obd->obd_md_cntr_base == 0);
1724
1725         num_stats = NUM_MD_STATS + num_private_stats;
1726         stats = lprocfs_alloc_stats(num_stats, 0);
1727         if (stats == NULL)
1728                 return -ENOMEM;
1729
1730         lprocfs_init_mps_stats(num_private_stats, stats);
1731
1732         for (i = num_private_stats; i < num_stats; i++) {
1733                 if (stats->ls_cnt_header[i].lc_name == NULL) {
1734                         CERROR("Missing md_stat initializer md_op "
1735                                "operation at offset %d. Aborting.\n",
1736                                i - num_private_stats);
1737                         LBUG();
1738                 }
1739         }
1740
1741         rc = lprocfs_register_stats(obd->obd_proc_entry, "md_stats", stats);
1742         if (rc < 0) {
1743                 lprocfs_free_stats(&stats);
1744         } else {
1745                 obd->obd_md_stats = stats;
1746                 obd->obd_md_cntr_base = num_private_stats;
1747         }
1748
1749         return rc;
1750 }
1751 EXPORT_SYMBOL(lprocfs_alloc_md_stats);
1752
1753 void lprocfs_free_md_stats(struct obd_device *obd)
1754 {
1755         struct lprocfs_stats *stats = obd->obd_md_stats;
1756
1757         if (stats != NULL) {
1758                 obd->obd_md_stats = NULL;
1759                 obd->obd_md_cntr_base = 0;
1760                 lprocfs_free_stats(&stats);
1761         }
1762 }
1763 EXPORT_SYMBOL(lprocfs_free_md_stats);
1764
1765 void lprocfs_init_ldlm_stats(struct lprocfs_stats *ldlm_stats)
1766 {
1767         lprocfs_counter_init(ldlm_stats,
1768                              LDLM_ENQUEUE - LDLM_FIRST_OPC,
1769                              0, "ldlm_enqueue", "reqs");
1770         lprocfs_counter_init(ldlm_stats,
1771                              LDLM_CONVERT - LDLM_FIRST_OPC,
1772                              0, "ldlm_convert", "reqs");
1773         lprocfs_counter_init(ldlm_stats,
1774                              LDLM_CANCEL - LDLM_FIRST_OPC,
1775                              0, "ldlm_cancel", "reqs");
1776         lprocfs_counter_init(ldlm_stats,
1777                              LDLM_BL_CALLBACK - LDLM_FIRST_OPC,
1778                              0, "ldlm_bl_callback", "reqs");
1779         lprocfs_counter_init(ldlm_stats,
1780                              LDLM_CP_CALLBACK - LDLM_FIRST_OPC,
1781                              0, "ldlm_cp_callback", "reqs");
1782         lprocfs_counter_init(ldlm_stats,
1783                              LDLM_GL_CALLBACK - LDLM_FIRST_OPC,
1784                              0, "ldlm_gl_callback", "reqs");
1785 }
1786 EXPORT_SYMBOL(lprocfs_init_ldlm_stats);
1787
1788 __s64 lprocfs_read_helper(struct lprocfs_counter *lc,
1789                           struct lprocfs_counter_header *header,
1790                           enum lprocfs_stats_flags flags,
1791                           enum lprocfs_fields_flags field)
1792 {
1793         __s64 ret = 0;
1794
1795         if (lc == NULL || header == NULL)
1796                 RETURN(0);
1797
1798         switch (field) {
1799                 case LPROCFS_FIELDS_FLAGS_CONFIG:
1800                         ret = header->lc_config;
1801                         break;
1802                 case LPROCFS_FIELDS_FLAGS_SUM:
1803                         ret = lc->lc_sum;
1804                         if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1805                                 ret += lc->lc_sum_irq;
1806                         break;
1807                 case LPROCFS_FIELDS_FLAGS_MIN:
1808                         ret = lc->lc_min;
1809                         break;
1810                 case LPROCFS_FIELDS_FLAGS_MAX:
1811                         ret = lc->lc_max;
1812                         break;
1813                 case LPROCFS_FIELDS_FLAGS_AVG:
1814                         ret = (lc->lc_max - lc->lc_min) / 2;
1815                         break;
1816                 case LPROCFS_FIELDS_FLAGS_SUMSQUARE:
1817                         ret = lc->lc_sumsquare;
1818                         break;
1819                 case LPROCFS_FIELDS_FLAGS_COUNT:
1820                         ret = lc->lc_count;
1821                         break;
1822                 default:
1823                         break;
1824         };
1825         RETURN(ret);
1826 }
1827 EXPORT_SYMBOL(lprocfs_read_helper);
1828
1829 int lprocfs_read_frac_helper(char *buffer, unsigned long count, long val,
1830                              int mult)
1831 {
1832         long decimal_val, frac_val;
1833         int prtn;
1834
1835         if (count < 10)
1836                 return -EINVAL;
1837
1838         decimal_val = val / mult;
1839         prtn = snprintf(buffer, count, "%ld", decimal_val);
1840         frac_val = val % mult;
1841
1842         if (prtn < (count - 4) && frac_val > 0) {
1843                 long temp_frac;
1844                 int i, temp_mult = 1, frac_bits = 0;
1845
1846                 temp_frac = frac_val * 10;
1847                 buffer[prtn++] = '.';
1848                 while (frac_bits < 2 && (temp_frac / mult) < 1 ) {
1849                         /* only reserved 2 bits fraction */
1850                         buffer[prtn++] ='0';
1851                         temp_frac *= 10;
1852                         frac_bits++;
1853                 }
1854                 /*
1855                  * Need to think these cases :
1856                  *      1. #echo x.00 > /proc/xxx       output result : x
1857                  *      2. #echo x.0x > /proc/xxx       output result : x.0x
1858                  *      3. #echo x.x0 > /proc/xxx       output result : x.x
1859                  *      4. #echo x.xx > /proc/xxx       output result : x.xx
1860                  *      Only reserved 2 bits fraction.
1861                  */
1862                 for (i = 0; i < (5 - prtn); i++)
1863                         temp_mult *= 10;
1864
1865                 frac_bits = min((int)count - prtn, 3 - frac_bits);
1866                 prtn += snprintf(buffer + prtn, frac_bits, "%ld",
1867                                  frac_val * temp_mult / mult);
1868
1869                 prtn--;
1870                 while(buffer[prtn] < '1' || buffer[prtn] > '9') {
1871                         prtn--;
1872                         if (buffer[prtn] == '.') {
1873                                 prtn--;
1874                                 break;
1875                         }
1876                 }
1877                 prtn++;
1878         }
1879         buffer[prtn++] ='\n';
1880         return prtn;
1881 }
1882 EXPORT_SYMBOL(lprocfs_read_frac_helper);
1883
1884 int lprocfs_seq_read_frac_helper(struct seq_file *m, long val, int mult)
1885 {
1886         long decimal_val, frac_val;
1887
1888         decimal_val = val / mult;
1889         seq_printf(m, "%ld", decimal_val);
1890         frac_val = val % mult;
1891
1892         if (frac_val > 0) {
1893                 frac_val *= 100;
1894                 frac_val /= mult;
1895         }
1896         if (frac_val > 0) {
1897                 /* Three cases: x0, xx, 0x */
1898                 if ((frac_val % 10) != 0)
1899                         seq_printf(m, ".%ld", frac_val);
1900                 else
1901                         seq_printf(m, ".%ld", frac_val / 10);
1902         }
1903
1904         seq_printf(m, "\n");
1905         return 0;
1906 }
1907 EXPORT_SYMBOL(lprocfs_seq_read_frac_helper);
1908
1909 /* Obtains the conversion factor for the unit specified */
1910 static int get_mult(char unit, __u64 *mult)
1911 {
1912         __u64 units = 1;
1913
1914         switch (unit) {
1915         /* peta, tera, giga, mega, and kilo */
1916         case 'p':
1917         case 'P':
1918                 units <<= 10;
1919         case 't':
1920         case 'T':
1921                 units <<= 10;
1922         case 'g':
1923         case 'G':
1924                 units <<= 10;
1925         case 'm':
1926         case 'M':
1927                 units <<= 10;
1928         case 'k':
1929         case 'K':
1930                 units <<= 10;
1931                 break;
1932         /* some tests expect % to be accepted */
1933         case '%':
1934                 units = 1;
1935                 break;
1936         default:
1937                 return -EINVAL;
1938         }
1939
1940         *mult = units;
1941
1942         return 0;
1943 }
1944
1945 /*
1946  * Ensures the numeric string is valid. The function provides the final
1947  * multiplier in the case a unit exists at the end of the string. It also
1948  * locates the start of the whole and fractional parts (if any). This
1949  * function modifies the string so kstrtoull can be used to parse both
1950  * the whole and fraction portions. This function also figures out
1951  * the base of the number.
1952  */
1953 static int preprocess_numeric_str(char *buffer, __u64 *mult, __u64 def_mult,
1954                                   bool allow_units, char **whole, char **frac,
1955                                   unsigned int *base)
1956 {
1957         bool hit_decimal = false;
1958         bool hit_unit = false;
1959         int rc = 0;
1960         char *start;
1961         *mult = def_mult;
1962         *whole = NULL;
1963         *frac = NULL;
1964         *base = 10;
1965
1966         /* a hex string if it starts with "0x" */
1967         if (buffer[0] == '0' && tolower(buffer[1]) == 'x') {
1968                 *base = 16;
1969                 buffer += 2;
1970         }
1971
1972         start = buffer;
1973
1974         while (*buffer) {
1975                 /* allow for a single new line before the null terminator */
1976                 if (*buffer == '\n') {
1977                         *buffer = '\0';
1978                         buffer++;
1979
1980                         if (*buffer)
1981                                 return -EINVAL;
1982
1983                         break;
1984                 }
1985
1986                 /* any chars after our unit indicates a malformed string */
1987                 if (hit_unit)
1988                         return -EINVAL;
1989
1990                 /* ensure we only hit one decimal */
1991                 if (*buffer == '.') {
1992                         if (hit_decimal)
1993                                 return -EINVAL;
1994
1995                         /* if past start, there's a whole part */
1996                         if (start != buffer)
1997                                 *whole = start;
1998
1999                         *buffer = '\0';
2000                         start = buffer + 1;
2001                         hit_decimal = true;
2002                 } else if (!isdigit(*buffer) &&
2003                            !(*base == 16 && isxdigit(*buffer))) {
2004                         if (allow_units) {
2005                                 /* if we allow units, attempt to get mult */
2006                                 hit_unit = true;
2007                                 rc = get_mult(*buffer, mult);
2008                                 if (rc)
2009                                         return rc;
2010
2011                                 /* string stops here, but keep processing */
2012                                 *buffer = '\0';
2013                         } else {
2014                                 /* bad string */
2015                                 return -EINVAL;
2016                         }
2017                 }
2018
2019                 buffer++;
2020         }
2021
2022         if (hit_decimal) {
2023                 /* hit a decimal, make sure there's a fractional part */
2024                 if (!*start)
2025                         return -EINVAL;
2026
2027                 *frac = start;
2028         } else {
2029                 /* didn't hit a decimal, but may have a whole part */
2030                 if (start != buffer && *start)
2031                         *whole = start;
2032         }
2033
2034         /* malformed string if we didn't get anything */
2035         if (!*frac && !*whole)
2036                 return -EINVAL;
2037
2038         return 0;
2039 }
2040
2041 /*
2042  * Parses a numeric string which can contain a whole and fraction portion
2043  * into a __u64. Accepts a multiplier to apply to the value parsed. Also
2044  * allows the string to have a unit at the end. The function handles
2045  * wrapping of the final unsigned value.
2046  */
2047 static int str_to_u64_parse(char *buffer, unsigned long count,
2048                             __u64 *val, __u64 def_mult, bool allow_units)
2049 {
2050         __u64 whole = 0;
2051         __u64 frac = 0;
2052         unsigned int frac_d = 1;
2053         __u64 wrap_indicator = ULLONG_MAX;
2054         int rc = 0;
2055         __u64 mult;
2056         char *strwhole;
2057         char *strfrac;
2058         unsigned int base = 10;
2059
2060         rc = preprocess_numeric_str(buffer, &mult, def_mult, allow_units,
2061                                     &strwhole, &strfrac, &base);
2062
2063         if (rc)
2064                 return rc;
2065
2066         if (mult == 0) {
2067                 *val = 0;
2068                 return 0;
2069         }
2070
2071         /* the multiplier limits how large the value can be */
2072         wrap_indicator = div64_u64(wrap_indicator, mult);
2073
2074         if (strwhole) {
2075                 rc = kstrtoull(strwhole, base, &whole);
2076                 if (rc)
2077                         return rc;
2078
2079                 if (whole > wrap_indicator)
2080                         return -ERANGE;
2081
2082                 whole *= mult;
2083         }
2084
2085         if (strfrac) {
2086                 if (strlen(strfrac) > 10)
2087                         strfrac[10] = '\0';
2088
2089                 rc = kstrtoull(strfrac, base, &frac);
2090                 if (rc)
2091                         return rc;
2092
2093                 /* determine power of fractional portion */
2094                 while (*strfrac) {
2095                         frac_d *= base;
2096                         strfrac++;
2097                 }
2098
2099                 /* fractional portion is too large to perform calculation */
2100                 if (frac > wrap_indicator)
2101                         return -ERANGE;
2102
2103                 frac *= mult;
2104                 do_div(frac, frac_d);
2105         }
2106
2107         /* check that the sum of whole and fraction fits in u64 */
2108         if (whole > (ULLONG_MAX - frac))
2109                 return -ERANGE;
2110
2111         *val = whole + frac;
2112
2113         return 0;
2114 }
2115
2116 /*
2117  * This function parses numeric/hex strings into __s64. It accepts a multiplier
2118  * which will apply to the value parsed. It also can allow the string to
2119  * have a unit as the last character. The function handles overflow/underflow
2120  * of the signed integer.
2121  */
2122 static int str_to_s64_internal(const char __user *buffer, unsigned long count,
2123                                __s64 *val, __u64 def_mult, bool allow_units)
2124 {
2125         char kernbuf[22];
2126         __u64 tmp;
2127         unsigned int offset = 0;
2128         int signed sign = 1;
2129         __u64 max = LLONG_MAX;
2130         int rc = 0;
2131
2132         if (count > (sizeof(kernbuf) - 1))
2133                 return -EINVAL;
2134
2135         if (copy_from_user(kernbuf, buffer, count))
2136                 return -EFAULT;
2137
2138         kernbuf[count] = '\0';
2139
2140         /* keep track of our sign */
2141         if (*kernbuf == '-') {
2142                 sign = -1;
2143                 offset++;
2144                 /* equivalent to max = -LLONG_MIN, avoids overflow */
2145                 max++;
2146         }
2147
2148         rc = str_to_u64_parse(kernbuf + offset, count - offset,
2149                               &tmp, def_mult, allow_units);
2150         if (rc)
2151                 return rc;
2152
2153         /* check for overflow/underflow */
2154         if (max < tmp)
2155                 return -ERANGE;
2156
2157         *val = (__s64)tmp * sign;
2158
2159         return 0;
2160 }
2161
2162 /**
2163  * Convert a user string into a signed 64 bit number. This function produces
2164  * an error when the value parsed from the string times multiplier underflows or
2165  * overflows. This function only accepts strings that contains digits, an
2166  * optional decimal, and a char representing a unit at the end. If a unit is
2167  * specified in the string, the multiplier provided by the caller is ignored.
2168  * This function can also accept hexadecimal strings which are prefixed with
2169  * "0x".
2170  *
2171  * \param[in] buffer    string consisting of numbers, a decimal, and a unit
2172  * \param[in] count     buffer length
2173  * \param[in] val       if successful, the value represented by the string
2174  * \param[in] defunit   default unit if string doesn't contain one
2175  *
2176  * \retval              0 on success
2177  * \retval              negative number on error
2178  */
2179 int lprocfs_str_with_units_to_s64(const char __user *buffer,
2180                                   unsigned long count, __s64 *val, char defunit)
2181 {
2182         __u64 mult = 1;
2183         int rc;
2184
2185         if (defunit != '1') {
2186                 rc = get_mult(defunit, &mult);
2187                 if (rc)
2188                         return rc;
2189         }
2190
2191         return str_to_s64_internal(buffer, count, val, mult, true);
2192 }
2193 EXPORT_SYMBOL(lprocfs_str_with_units_to_s64);
2194
2195 char *lprocfs_strnstr(const char *s1, const char *s2, size_t len)
2196 {
2197         size_t l2;
2198
2199         l2 = strlen(s2);
2200         if (!l2)
2201                 return (char *)s1;
2202         while (len >= l2) {
2203                 len--;
2204                 if (!memcmp(s1, s2, l2))
2205                         return (char *)s1;
2206                 s1++;
2207         }
2208         return NULL;
2209 }
2210 EXPORT_SYMBOL(lprocfs_strnstr);
2211
2212 /**
2213  * Find the string \a name in the input \a buffer, and return a pointer to the
2214  * value immediately following \a name, reducing \a count appropriately.
2215  * If \a name is not found the original \a buffer is returned.
2216  */
2217 char *lprocfs_find_named_value(const char *buffer, const char *name,
2218                                 size_t *count)
2219 {
2220         char *val;
2221         size_t buflen = *count;
2222
2223         /* there is no strnstr() in rhel5 and ubuntu kernels */
2224         val = lprocfs_strnstr(buffer, name, buflen);
2225         if (val == NULL)
2226                 return (char *)buffer;
2227
2228         val += strlen(name);                             /* skip prefix */
2229         while (val < buffer + buflen && isspace(*val)) /* skip separator */
2230                 val++;
2231
2232         *count = 0;
2233         while (val < buffer + buflen && isalnum(*val)) {
2234                 ++*count;
2235                 ++val;
2236         }
2237
2238         return val - *count;
2239 }
2240 EXPORT_SYMBOL(lprocfs_find_named_value);
2241
2242 int ldebugfs_seq_create(struct dentry *parent, const char *name, umode_t mode,
2243                         const struct file_operations *seq_fops, void *data)
2244 {
2245         struct dentry *entry;
2246
2247         /* Disallow secretly (un)writable entries. */
2248         LASSERT((!seq_fops->write) == (!(mode & 0222)));
2249
2250         entry = debugfs_create_file(name, mode, parent, data, seq_fops);
2251         if (IS_ERR_OR_NULL(entry))
2252                 return entry ? PTR_ERR(entry) : -ENOMEM;
2253
2254         return 0;
2255 }
2256 EXPORT_SYMBOL_GPL(ldebugfs_seq_create);
2257
2258 int lprocfs_seq_create(struct proc_dir_entry *parent,
2259                        const char *name,
2260                        mode_t mode,
2261                        const struct file_operations *seq_fops,
2262                        void *data)
2263 {
2264         struct proc_dir_entry *entry;
2265         ENTRY;
2266
2267         /* Disallow secretly (un)writable entries. */
2268         LASSERT((seq_fops->write == NULL) == ((mode & 0222) == 0));
2269
2270         entry = proc_create_data(name, mode, parent, seq_fops, data);
2271
2272         if (entry == NULL)
2273                 RETURN(-ENOMEM);
2274
2275         RETURN(0);
2276 }
2277 EXPORT_SYMBOL(lprocfs_seq_create);
2278
2279 int lprocfs_obd_seq_create(struct obd_device *dev,
2280                            const char *name,
2281                            mode_t mode,
2282                            const struct file_operations *seq_fops,
2283                            void *data)
2284 {
2285         return (lprocfs_seq_create(dev->obd_proc_entry, name,
2286                                    mode, seq_fops, data));
2287 }
2288 EXPORT_SYMBOL(lprocfs_obd_seq_create);
2289
2290 void lprocfs_oh_tally(struct obd_histogram *oh, unsigned int value)
2291 {
2292         if (value >= OBD_HIST_MAX)
2293                 value = OBD_HIST_MAX - 1;
2294
2295         spin_lock(&oh->oh_lock);
2296         oh->oh_buckets[value]++;
2297         spin_unlock(&oh->oh_lock);
2298 }
2299 EXPORT_SYMBOL(lprocfs_oh_tally);
2300
2301 void lprocfs_oh_tally_log2(struct obd_histogram *oh, unsigned int value)
2302 {
2303         unsigned int val = 0;
2304
2305         if (likely(value != 0))
2306                 val = min(fls(value - 1), OBD_HIST_MAX);
2307
2308         lprocfs_oh_tally(oh, val);
2309 }
2310 EXPORT_SYMBOL(lprocfs_oh_tally_log2);
2311
2312 unsigned long lprocfs_oh_sum(struct obd_histogram *oh)
2313 {
2314         unsigned long ret = 0;
2315         int i;
2316
2317         for (i = 0; i < OBD_HIST_MAX; i++)
2318                 ret +=  oh->oh_buckets[i];
2319         return ret;
2320 }
2321 EXPORT_SYMBOL(lprocfs_oh_sum);
2322
2323 void lprocfs_oh_clear(struct obd_histogram *oh)
2324 {
2325         spin_lock(&oh->oh_lock);
2326         memset(oh->oh_buckets, 0, sizeof(oh->oh_buckets));
2327         spin_unlock(&oh->oh_lock);
2328 }
2329 EXPORT_SYMBOL(lprocfs_oh_clear);
2330
2331 ssize_t lustre_attr_show(struct kobject *kobj,
2332                          struct attribute *attr, char *buf)
2333 {
2334         struct lustre_attr *a = container_of(attr, struct lustre_attr, attr);
2335
2336         return a->show ? a->show(kobj, attr, buf) : 0;
2337 }
2338 EXPORT_SYMBOL_GPL(lustre_attr_show);
2339
2340 ssize_t lustre_attr_store(struct kobject *kobj, struct attribute *attr,
2341                           const char *buf, size_t len)
2342 {
2343         struct lustre_attr *a = container_of(attr, struct lustre_attr, attr);
2344
2345         return a->store ? a->store(kobj, attr, buf, len) : len;
2346 }
2347 EXPORT_SYMBOL_GPL(lustre_attr_store);
2348
2349 const struct sysfs_ops lustre_sysfs_ops = {
2350         .show  = lustre_attr_show,
2351         .store = lustre_attr_store,
2352 };
2353 EXPORT_SYMBOL_GPL(lustre_sysfs_ops);
2354
2355 int lprocfs_obd_max_pages_per_rpc_seq_show(struct seq_file *m, void *data)
2356 {
2357         struct obd_device *dev = data;
2358         struct client_obd *cli = &dev->u.cli;
2359
2360         spin_lock(&cli->cl_loi_list_lock);
2361         seq_printf(m, "%d\n", cli->cl_max_pages_per_rpc);
2362         spin_unlock(&cli->cl_loi_list_lock);
2363         return 0;
2364 }
2365 EXPORT_SYMBOL(lprocfs_obd_max_pages_per_rpc_seq_show);
2366
2367 ssize_t lprocfs_obd_max_pages_per_rpc_seq_write(struct file *file,
2368                                                 const char __user *buffer,
2369                                                 size_t count, loff_t *off)
2370 {
2371         struct obd_device *dev =
2372                 ((struct seq_file *)file->private_data)->private;
2373         struct client_obd *cli = &dev->u.cli;
2374         struct obd_connect_data *ocd = &cli->cl_import->imp_connect_data;
2375         int chunk_mask, rc;
2376         s64 val;
2377
2378         rc = lprocfs_str_with_units_to_s64(buffer, count, &val, '1');
2379         if (rc)
2380                 return rc;
2381         if (val < 0)
2382                 return -ERANGE;
2383
2384         /* if the max_pages is specified in bytes, convert to pages */
2385         if (val >= ONE_MB_BRW_SIZE)
2386                 val >>= PAGE_SHIFT;
2387
2388         LPROCFS_CLIMP_CHECK(dev);
2389
2390         chunk_mask = ~((1 << (cli->cl_chunkbits - PAGE_SHIFT)) - 1);
2391         /* max_pages_per_rpc must be chunk aligned */
2392         val = (val + ~chunk_mask) & chunk_mask;
2393         if (val == 0 || (ocd->ocd_brw_size != 0 &&
2394                          val > ocd->ocd_brw_size >> PAGE_SHIFT)) {
2395                 LPROCFS_CLIMP_EXIT(dev);
2396                 return -ERANGE;
2397         }
2398         spin_lock(&cli->cl_loi_list_lock);
2399         cli->cl_max_pages_per_rpc = val;
2400         client_adjust_max_dirty(cli);
2401         spin_unlock(&cli->cl_loi_list_lock);
2402
2403         LPROCFS_CLIMP_EXIT(dev);
2404         return count;
2405 }
2406 EXPORT_SYMBOL(lprocfs_obd_max_pages_per_rpc_seq_write);
2407
2408 ssize_t short_io_bytes_show(struct kobject *kobj, struct attribute *attr,
2409                             char *buf)
2410 {
2411         struct obd_device *dev = container_of(kobj, struct obd_device,
2412                                               obd_kset.kobj);
2413         struct client_obd *cli = &dev->u.cli;
2414         int rc;
2415
2416         spin_lock(&cli->cl_loi_list_lock);
2417         rc = sprintf(buf, "%d\n", cli->cl_short_io_bytes);
2418         spin_unlock(&cli->cl_loi_list_lock);
2419         return rc;
2420 }
2421 EXPORT_SYMBOL(short_io_bytes_show);
2422
2423 /* Used to catch people who think they're specifying pages. */
2424 #define MIN_SHORT_IO_BYTES 64
2425
2426 ssize_t short_io_bytes_store(struct kobject *kobj, struct attribute *attr,
2427                              const char *buffer, size_t count)
2428 {
2429         struct obd_device *dev = container_of(kobj, struct obd_device,
2430                                               obd_kset.kobj);
2431         struct client_obd *cli = &dev->u.cli;
2432         u32 val;
2433         int rc;
2434
2435         LPROCFS_CLIMP_CHECK(dev);
2436
2437         rc = kstrtouint(buffer, 0, &val);
2438         if (rc)
2439                 GOTO(out, rc);
2440
2441         if (val < MIN_SHORT_IO_BYTES || val > OBD_MAX_SHORT_IO_BYTES)
2442                 GOTO(out, rc = -ERANGE);
2443
2444         rc = count;
2445
2446         spin_lock(&cli->cl_loi_list_lock);
2447         if (val > (cli->cl_max_pages_per_rpc << PAGE_SHIFT))
2448                 rc = -ERANGE;
2449         else
2450                 cli->cl_short_io_bytes = val;
2451         spin_unlock(&cli->cl_loi_list_lock);
2452
2453 out:
2454         LPROCFS_CLIMP_EXIT(dev);
2455         return rc;
2456 }
2457 EXPORT_SYMBOL(short_io_bytes_store);
2458
2459 int lprocfs_wr_root_squash(const char __user *buffer, unsigned long count,
2460                            struct root_squash_info *squash, char *name)
2461 {
2462         int rc;
2463         char kernbuf[64], *tmp, *errmsg;
2464         unsigned long uid, gid;
2465         ENTRY;
2466
2467         if (count >= sizeof(kernbuf)) {
2468                 errmsg = "string too long";
2469                 GOTO(failed_noprint, rc = -EINVAL);
2470         }
2471         if (copy_from_user(kernbuf, buffer, count)) {
2472                 errmsg = "bad address";
2473                 GOTO(failed_noprint, rc = -EFAULT);
2474         }
2475         kernbuf[count] = '\0';
2476
2477         /* look for uid gid separator */
2478         tmp = strchr(kernbuf, ':');
2479         if (tmp == NULL) {
2480                 errmsg = "needs uid:gid format";
2481                 GOTO(failed, rc = -EINVAL);
2482         }
2483         *tmp = '\0';
2484         tmp++;
2485
2486         /* parse uid */
2487         if (kstrtoul(kernbuf, 0, &uid) != 0) {
2488                 errmsg = "bad uid";
2489                 GOTO(failed, rc = -EINVAL);
2490         }
2491
2492         /* parse gid */
2493         if (kstrtoul(tmp, 0, &gid) != 0) {
2494                 errmsg = "bad gid";
2495                 GOTO(failed, rc = -EINVAL);
2496         }
2497
2498         squash->rsi_uid = uid;
2499         squash->rsi_gid = gid;
2500
2501         LCONSOLE_INFO("%s: root_squash is set to %u:%u\n",
2502                       name, squash->rsi_uid, squash->rsi_gid);
2503         RETURN(count);
2504
2505 failed:
2506         if (tmp != NULL) {
2507                 tmp--;
2508                 *tmp = ':';
2509         }
2510         CWARN("%s: failed to set root_squash to \"%s\", %s, rc = %d\n",
2511               name, kernbuf, errmsg, rc);
2512         RETURN(rc);
2513 failed_noprint:
2514         CWARN("%s: failed to set root_squash due to %s, rc = %d\n",
2515               name, errmsg, rc);
2516         RETURN(rc);
2517 }
2518 EXPORT_SYMBOL(lprocfs_wr_root_squash);
2519
2520
2521 int lprocfs_wr_nosquash_nids(const char __user *buffer, unsigned long count,
2522                              struct root_squash_info *squash, char *name)
2523 {
2524         int rc;
2525         char *kernbuf = NULL;
2526         char *errmsg;
2527         struct list_head tmp;
2528         int len = count;
2529         ENTRY;
2530
2531         if (count > 4096) {
2532                 errmsg = "string too long";
2533                 GOTO(failed, rc = -EINVAL);
2534         }
2535
2536         OBD_ALLOC(kernbuf, count + 1);
2537         if (kernbuf == NULL) {
2538                 errmsg = "no memory";
2539                 GOTO(failed, rc = -ENOMEM);
2540         }
2541         if (copy_from_user(kernbuf, buffer, count)) {
2542                 errmsg = "bad address";
2543                 GOTO(failed, rc = -EFAULT);
2544         }
2545         kernbuf[count] = '\0';
2546
2547         if (count > 0 && kernbuf[count - 1] == '\n')
2548                 len = count - 1;
2549
2550         if ((len == 4 && strncmp(kernbuf, "NONE", len) == 0) ||
2551             (len == 5 && strncmp(kernbuf, "clear", len) == 0)) {
2552                 /* empty string is special case */
2553                 down_write(&squash->rsi_sem);
2554                 if (!list_empty(&squash->rsi_nosquash_nids))
2555                         cfs_free_nidlist(&squash->rsi_nosquash_nids);
2556                 up_write(&squash->rsi_sem);
2557                 LCONSOLE_INFO("%s: nosquash_nids is cleared\n", name);
2558                 OBD_FREE(kernbuf, count + 1);
2559                 RETURN(count);
2560         }
2561
2562         INIT_LIST_HEAD(&tmp);
2563         if (cfs_parse_nidlist(kernbuf, count, &tmp) <= 0) {
2564                 errmsg = "can't parse";
2565                 GOTO(failed, rc = -EINVAL);
2566         }
2567         LCONSOLE_INFO("%s: nosquash_nids set to %s\n",
2568                       name, kernbuf);
2569         OBD_FREE(kernbuf, count + 1);
2570         kernbuf = NULL;
2571
2572         down_write(&squash->rsi_sem);
2573         if (!list_empty(&squash->rsi_nosquash_nids))
2574                 cfs_free_nidlist(&squash->rsi_nosquash_nids);
2575         list_splice(&tmp, &squash->rsi_nosquash_nids);
2576         up_write(&squash->rsi_sem);
2577
2578         RETURN(count);
2579
2580 failed:
2581         if (kernbuf) {
2582                 CWARN("%s: failed to set nosquash_nids to \"%s\", %s rc = %d\n",
2583                       name, kernbuf, errmsg, rc);
2584                 OBD_FREE(kernbuf, count + 1);
2585         } else {
2586                 CWARN("%s: failed to set nosquash_nids due to %s rc = %d\n",
2587                       name, errmsg, rc);
2588         }
2589         RETURN(rc);
2590 }
2591 EXPORT_SYMBOL(lprocfs_wr_nosquash_nids);
2592
2593 #endif /* CONFIG_PROC_FS*/