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