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