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