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