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