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