Whamcloud - gitweb
LU-17005 obdclass: allow stats header to be disabled
[fs/lustre-release.git] / lustre / obdclass / obd_sysfs.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) 2007, 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  *
31  * lustre/obdclass/obd_sysfs.c
32  *
33  * Object Devices Class Driver
34  * These are the only exported functions, they provide some generic
35  * infrastructure for managing object devices
36  */
37
38 #define DEBUG_SUBSYSTEM S_CLASS
39
40 #include <linux/module.h>
41 #include <linux/errno.h>
42 #include <linux/kernel.h>
43 #include <linux/sched.h>
44 #include <linux/lp.h>
45 #include <linux/slab.h>
46 #include <linux/ioport.h>
47 #include <linux/fcntl.h>
48 #include <linux/delay.h>
49 #include <linux/skbuff.h>
50 #include <linux/proc_fs.h>
51 #include <linux/fs.h>
52 #include <linux/poll.h>
53 #include <linux/init.h>
54 #include <linux/list.h>
55 #include <linux/highmem.h>
56 #include <asm/io.h>
57 #include <asm/ioctls.h>
58 #include <asm/poll.h>
59 #include <asm/uaccess.h>
60 #include <linux/miscdevice.h>
61 #include <linux/seq_file.h>
62 #include <linux/kobject.h>
63
64 #include <libcfs/libcfs.h>
65 #include <libcfs/libcfs_crypto.h>
66 #include <obd_support.h>
67 #include <obd_class.h>
68 #include <lprocfs_status.h>
69 #include <uapi/linux/lnet/lnetctl.h>
70 #include <uapi/linux/lustre/lustre_ioctl.h>
71 #include <uapi/linux/lustre/lustre_ver.h>
72
73 struct static_lustre_uintvalue_attr {
74         struct {
75                 struct attribute attr;
76                 ssize_t (*show)(struct kobject *kobj, struct attribute *attr,
77                                 char *buf);
78                 ssize_t (*store)(struct kobject *kobj, struct attribute *attr,
79                                  const char *buf, size_t len);
80         } u;
81         int *value;
82 };
83
84 static ssize_t static_uintvalue_show(struct kobject *kobj,
85                                      struct attribute *attr,
86                                      char *buf)
87 {
88         struct static_lustre_uintvalue_attr *lattr = (void *)attr;
89
90         return sprintf(buf, "%d\n", *lattr->value);
91 }
92
93 static ssize_t static_uintvalue_store(struct kobject *kobj,
94                                       struct attribute *attr,
95                                       const char *buffer, size_t count)
96 {
97         struct static_lustre_uintvalue_attr *lattr = (void *)attr;
98         unsigned int val;
99         int rc;
100
101         rc = kstrtouint(buffer, 10, &val);
102         if (rc)
103                 return rc;
104
105         *lattr->value = val;
106
107         return count;
108 }
109
110 #define LUSTRE_STATIC_UINT_ATTR(name, value)                            \
111 static struct static_lustre_uintvalue_attr lustre_sattr_##name =        \
112         { __ATTR(name, 0644, static_uintvalue_show,                     \
113                  static_uintvalue_store), value }
114
115 LUSTRE_STATIC_UINT_ATTR(debug_peer_on_timeout, &obd_debug_peer_on_timeout);
116 LUSTRE_STATIC_UINT_ATTR(dump_on_timeout, &obd_dump_on_timeout);
117 LUSTRE_STATIC_UINT_ATTR(dump_on_eviction, &obd_dump_on_eviction);
118 LUSTRE_STATIC_UINT_ATTR(at_min, &at_min);
119 LUSTRE_STATIC_UINT_ATTR(at_max, &at_max);
120 LUSTRE_STATIC_UINT_ATTR(at_extra, &at_extra);
121 LUSTRE_STATIC_UINT_ATTR(at_early_margin, &at_early_margin);
122 LUSTRE_STATIC_UINT_ATTR(at_history, &at_history);
123 LUSTRE_STATIC_UINT_ATTR(enable_stats_header, &obd_enable_stats_header);
124 LUSTRE_STATIC_UINT_ATTR(lbug_on_eviction, &obd_lbug_on_eviction);
125 LUSTRE_STATIC_UINT_ATTR(ping_interval, &ping_interval);
126 LUSTRE_STATIC_UINT_ATTR(evict_multiplier, &ping_evict_timeout_multiplier);
127
128 #ifdef HAVE_SERVER_SUPPORT
129 LUSTRE_STATIC_UINT_ATTR(ldlm_timeout, &ldlm_timeout);
130 LUSTRE_STATIC_UINT_ATTR(bulk_timeout, &bulk_timeout);
131 #endif
132
133 static ssize_t memused_show(struct kobject *kobj, struct attribute *attr,
134                             char *buf)
135 {
136         return sprintf(buf, "%llu\n", obd_memory_sum());
137 }
138 LUSTRE_RO_ATTR(memused);
139
140 static ssize_t memused_max_show(struct kobject *kobj, struct attribute *attr,
141                                 char *buf)
142 {
143         return sprintf(buf, "%llu\n", obd_memory_max());
144 }
145 LUSTRE_RO_ATTR(memused_max);
146
147 static ssize_t max_dirty_mb_show(struct kobject *kobj, struct attribute *attr,
148                                  char *buf)
149 {
150         return sprintf(buf, "%lu\n",
151                        obd_max_dirty_pages / (1 << (20 - PAGE_SHIFT)));
152 }
153
154 static ssize_t max_dirty_mb_store(struct kobject *kobj, struct attribute *attr,
155                                   const char *buffer, size_t count)
156 {
157         unsigned long val;
158         int rc;
159
160         rc = kstrtoul(buffer, 10, &val);
161         if (rc)
162                 return rc;
163
164         val *= 1 << (20 - PAGE_SHIFT); /* convert to pages */
165
166         if (val > ((cfs_totalram_pages() / 10) * 9)) {
167                 /* Somebody wants to assign too much memory to dirty pages */
168                 return -EINVAL;
169         }
170
171         if (val < 4 << (20 - PAGE_SHIFT)) {
172                 /* Less than 4 Mb for dirty cache is also bad */
173                 return -EINVAL;
174         }
175
176         obd_max_dirty_pages = val;
177
178         return count;
179 }
180 LUSTRE_RW_ATTR(max_dirty_mb);
181
182 #ifdef HAVE_SERVER_SUPPORT
183 static ssize_t no_transno_store(struct kobject *kobj,
184                                 struct attribute *attr,
185                                 const char *buffer, size_t count)
186 {
187         struct obd_device *obd;
188         unsigned int idx;
189         int rc;
190
191         rc = kstrtouint(buffer, 10, &idx);
192         if (rc)
193                 return rc;
194
195         obd = class_num2obd(idx);
196         if (!obd || !obd->obd_attached) {
197                 if (obd)
198                         CERROR("%s: not attached\n", obd->obd_name);
199                 return -ENODEV;
200         }
201
202         spin_lock(&obd->obd_dev_lock);
203         obd->obd_no_transno = 1;
204         spin_unlock(&obd->obd_dev_lock);
205         return count;
206 }
207 LUSTRE_WO_ATTR(no_transno);
208 #endif /* HAVE_SERVER_SUPPORT */
209
210 static ssize_t version_show(struct kobject *kobj, struct attribute *attr,
211                             char *buf)
212 {
213         return sprintf(buf, "%s\n", LUSTRE_VERSION_STRING);
214 }
215
216 static ssize_t pinger_show(struct kobject *kobj, struct attribute *attr,
217                            char *buf)
218 {
219 #ifdef CONFIG_LUSTRE_FS_PINGER
220         const char *state = "on";
221 #else
222         const char *state = "off";
223 #endif
224         return sprintf(buf, "%s\n", state);
225 }
226
227 /**
228  * Check all obd devices health
229  *
230  * \param kobj
231  * \param buf [in]
232  *
233  * \retval number of characters printed if healthy
234  */
235 static ssize_t
236 health_check_show(struct kobject *kobj, struct attribute *attr, char *buf)
237 {
238         bool healthy = true;
239         size_t len = 0;
240         int i;
241
242         if (libcfs_catastrophe)
243                 return sprintf(buf, "LBUG\n");
244
245         read_lock(&obd_dev_lock);
246         for (i = 0; i < class_devno_max(); i++) {
247                 struct obd_device *obd;
248
249                 obd = class_num2obd(i);
250                 if (obd == NULL || !obd->obd_attached || !obd->obd_set_up)
251                         continue;
252
253                 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
254                 if (obd->obd_stopping)
255                         continue;
256
257                 if (obd->obd_read_only)
258                         continue;
259
260                 class_incref(obd, __func__, current);
261                 read_unlock(&obd_dev_lock);
262
263                 if (obd_health_check(NULL, obd))
264                         healthy = false;
265
266                 class_decref(obd, __func__, current);
267                 read_lock(&obd_dev_lock);
268
269                 if (!healthy)
270                         break;
271         }
272         read_unlock(&obd_dev_lock);
273
274         if (healthy)
275                 len = sprintf(buf, "healthy\n");
276         else
277                 len = sprintf(buf, "NOT HEALTHY\n");
278
279         return len;
280 }
281
282 static ssize_t jobid_var_show(struct kobject *kobj, struct attribute *attr,
283                               char *buf)
284 {
285         int rc = 0;
286
287         if (strlen(obd_jobid_var))
288                 rc = scnprintf(buf, PAGE_SIZE, "%s\n", obd_jobid_var);
289         return rc;
290 }
291
292 static ssize_t jobid_var_store(struct kobject *kobj, struct attribute *attr,
293                                const char *buffer, size_t count)
294 {
295         if (!count || count > JOBSTATS_JOBID_VAR_MAX_LEN)
296                 return -EINVAL;
297
298         memset(obd_jobid_var, 0, JOBSTATS_JOBID_VAR_MAX_LEN + 1);
299
300         memcpy(obd_jobid_var, buffer, count);
301
302         /* Trim the trailing '\n' if any */
303         if (obd_jobid_var[count - 1] == '\n')
304                 obd_jobid_var[count - 1] = 0;
305
306         return count;
307 }
308
309 static ssize_t jobid_name_show(struct kobject *kobj, struct attribute *attr,
310                                char *buf)
311 {
312         int rc = 0;
313
314         if (strlen(obd_jobid_name))
315                 rc = scnprintf(buf, PAGE_SIZE, "%s\n", obd_jobid_name);
316         return rc;
317 }
318
319 static ssize_t jobid_name_store(struct kobject *kobj, struct attribute *attr,
320                                 const char *buffer, size_t count)
321 {
322         if (!count || count > LUSTRE_JOBID_SIZE)
323                 return -EINVAL;
324
325         if (strcmp(obd_jobid_var, JOBSTATS_NODELOCAL) != 0 &&
326             !strchr(buffer, '%')) {
327                 lustre_jobid_clear(buffer);
328                 return count;
329         }
330
331         /* clear previous value */
332         memset(obd_jobid_name, 0, LUSTRE_JOBID_SIZE);
333
334         memcpy(obd_jobid_name, buffer, count);
335
336         /* Trim the trailing '\n' if any */
337         if (obd_jobid_name[count - 1] == '\n') {
338                 /* Don't echo just a newline */
339                 if (count == 1)
340                         return -EINVAL;
341                 obd_jobid_name[count - 1] = 0;
342         }
343
344         return count;
345 }
346
347 static ssize_t jobid_this_session_show(struct kobject *kobj,
348                                        struct attribute *attr,
349                                        char *buf)
350 {
351         char *jid;
352         int ret = -ENOENT;
353
354         rcu_read_lock();
355         jid = jobid_current();
356         if (jid)
357                 ret = scnprintf(buf, PAGE_SIZE, "%s\n", jid);
358         rcu_read_unlock();
359         return ret;
360 }
361
362 static ssize_t jobid_this_session_store(struct kobject *kobj,
363                                         struct attribute *attr,
364                                         const char *buffer,
365                                         size_t count)
366 {
367         char *jobid;
368         int len;
369         int ret;
370
371         if (!count || count > LUSTRE_JOBID_SIZE)
372                 return -EINVAL;
373
374         jobid = kstrndup(buffer, count, GFP_KERNEL);
375         if (!jobid)
376                 return -ENOMEM;
377         len = strcspn(jobid, "\n ");
378         jobid[len] = '\0';
379         ret = jobid_set_current(jobid);
380         kfree(jobid);
381
382         return ret ?: count;
383 }
384
385 static ssize_t timeout_show(struct kobject *kobj,
386                             struct attribute *attr,
387                             char *buf)
388 {
389         return sprintf(buf, "%u\n", obd_timeout);
390 }
391
392 static ssize_t timeout_store(struct kobject *kobj,
393                              struct attribute *attr,
394                              const char *buffer,
395                              size_t count)
396 {
397         unsigned int val;
398         int rc;
399
400         rc = kstrtouint(buffer, 10, &val);
401         if (rc)
402                 return rc;
403         obd_timeout = val ?: 1U;
404         ping_interval = max(obd_timeout / 4, 1U);
405
406         return count;
407 }
408
409 /* Root for /sys/kernel/debug/lustre */
410 struct dentry *debugfs_lustre_root;
411 EXPORT_SYMBOL_GPL(debugfs_lustre_root);
412
413 #ifdef CONFIG_PROC_FS
414 /* Root for /proc/fs/lustre */
415 struct proc_dir_entry *proc_lustre_root;
416 EXPORT_SYMBOL(proc_lustre_root);
417 #else
418 #define lprocfs_base NULL
419 #endif /* CONFIG_PROC_FS */
420
421 LUSTRE_RO_ATTR(version);
422 LUSTRE_RO_ATTR(pinger);
423 LUSTRE_RO_ATTR(health_check);
424 LUSTRE_RW_ATTR(jobid_var);
425 LUSTRE_RW_ATTR(jobid_name);
426 LUSTRE_RW_ATTR(jobid_this_session);
427 LUSTRE_RW_ATTR(timeout);
428
429 static struct attribute *lustre_attrs[] = {
430         &lustre_attr_version.attr,
431         &lustre_attr_pinger.attr,
432         &lustre_sattr_enable_stats_header.u.attr,
433         &lustre_attr_health_check.attr,
434         &lustre_attr_jobid_name.attr,
435         &lustre_attr_jobid_var.attr,
436         &lustre_attr_jobid_this_session.attr,
437         &lustre_attr_timeout.attr,
438         &lustre_attr_max_dirty_mb.attr,
439         &lustre_sattr_debug_peer_on_timeout.u.attr,
440         &lustre_sattr_dump_on_timeout.u.attr,
441         &lustre_sattr_dump_on_eviction.u.attr,
442         &lustre_sattr_at_min.u.attr,
443         &lustre_sattr_at_max.u.attr,
444         &lustre_sattr_at_extra.u.attr,
445         &lustre_sattr_at_early_margin.u.attr,
446         &lustre_sattr_at_history.u.attr,
447         &lustre_attr_memused_max.attr,
448         &lustre_attr_memused.attr,
449 #ifdef HAVE_SERVER_SUPPORT
450         &lustre_sattr_ldlm_timeout.u.attr,
451         &lustre_sattr_bulk_timeout.u.attr,
452         &lustre_attr_no_transno.attr,
453 #endif
454         &lustre_sattr_lbug_on_eviction.u.attr,
455         &lustre_sattr_ping_interval.u.attr,
456         &lustre_sattr_evict_multiplier.u.attr,
457         NULL,
458 };
459
460 static void *obd_device_list_seq_start(struct seq_file *p, loff_t *pos)
461 {
462         if (*pos >= class_devno_max())
463                 return NULL;
464
465         return pos;
466 }
467
468 static void obd_device_list_seq_stop(struct seq_file *p, void *v)
469 {
470 }
471
472 static void *obd_device_list_seq_next(struct seq_file *p, void *v, loff_t *pos)
473 {
474         ++*pos;
475         if (*pos >= class_devno_max())
476                 return NULL;
477
478         return pos;
479 }
480
481 static int obd_device_list_seq_show(struct seq_file *p, void *v)
482 {
483         loff_t index = *(loff_t *)v;
484         struct obd_device *obd = class_num2obd((int)index);
485         char *status;
486
487         if (obd == NULL)
488                 return 0;
489
490         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
491         if (obd->obd_stopping)
492                 status = "ST";
493         else if (obd->obd_inactive)
494                 status = "IN";
495         else if (obd->obd_set_up)
496                 status = "UP";
497         else if (obd->obd_attached)
498                 status = "AT";
499         else
500                 status = "--";
501
502         seq_printf(p, "%3d %s %s %s %s %d\n",
503                    (int)index, status, obd->obd_type->typ_name,
504                    obd->obd_name, obd->obd_uuid.uuid,
505                    atomic_read(&obd->obd_refcount));
506         return 0;
507 }
508
509 static const struct seq_operations obd_device_list_sops = {
510         .start = obd_device_list_seq_start,
511         .stop = obd_device_list_seq_stop,
512         .next = obd_device_list_seq_next,
513         .show = obd_device_list_seq_show,
514 };
515
516 static int obd_device_list_open(struct inode *inode, struct file *file)
517 {
518         struct seq_file *seq;
519         int rc = seq_open(file, &obd_device_list_sops);
520
521         if (rc)
522                 return rc;
523
524         seq = file->private_data;
525         seq->private = inode->i_private;
526         return 0;
527 }
528
529 static const struct file_operations obd_device_list_fops = {
530         .owner   = THIS_MODULE,
531         .open    = obd_device_list_open,
532         .read    = seq_read,
533         .llseek  = seq_lseek,
534         .release = seq_release,
535 };
536
537 /* checksum_speed */
538 static void *checksum_speed_start(struct seq_file *p, loff_t *pos)
539 {
540         return pos;
541 }
542
543 static void checksum_speed_stop(struct seq_file *p, void *v)
544 {
545 }
546
547 static void *checksum_speed_next(struct seq_file *p, void *v, loff_t *pos)
548 {
549         ++(*pos);
550         if (*pos >= CFS_HASH_ALG_SPEED_MAX - 1)
551                 return NULL;
552
553         return pos;
554 }
555
556 static int checksum_speed_show(struct seq_file *p, void *v)
557 {
558         loff_t index = *(loff_t *)v;
559
560         if (!index || index > CFS_HASH_ALG_SPEED_MAX - 1)
561                 return 0;
562
563         seq_printf(p, "%s: %d\n", cfs_crypto_hash_name(index),
564                    cfs_crypto_hash_speeds[index]);
565
566         return 0;
567 }
568
569 static const struct seq_operations checksum_speed_sops = {
570         .start = checksum_speed_start,
571         .stop = checksum_speed_stop,
572         .next = checksum_speed_next,
573         .show = checksum_speed_show,
574 };
575
576 static int checksum_speed_open(struct inode *inode, struct file *file)
577 {
578         int rc = seq_open(file, &checksum_speed_sops);
579
580         if (rc)
581                 return rc;
582
583         return 0;
584 }
585
586 static const struct file_operations checksum_speed_fops = {
587         .owner   = THIS_MODULE,
588         .open    = checksum_speed_open,
589         .read    = seq_read,
590         .llseek  = seq_lseek,
591         .release = seq_release,
592 };
593
594 static int
595 health_check_seq_show(struct seq_file *m, void *unused)
596 {
597         int i;
598
599         read_lock(&obd_dev_lock);
600         for (i = 0; i < class_devno_max(); i++) {
601                 struct obd_device *obd;
602
603                 obd = class_num2obd(i);
604                 if (obd == NULL || !obd->obd_attached || !obd->obd_set_up)
605                         continue;
606
607                 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
608                 if (obd->obd_stopping)
609                         continue;
610
611                 class_incref(obd, __func__, current);
612                 read_unlock(&obd_dev_lock);
613
614                 if (obd_health_check(NULL, obd)) {
615                         seq_printf(m, "device %s reported unhealthy\n",
616                                    obd->obd_name);
617                 }
618                 class_decref(obd, __func__, current);
619                 read_lock(&obd_dev_lock);
620         }
621         read_unlock(&obd_dev_lock);
622
623         return 0;
624 }
625
626 LDEBUGFS_SEQ_FOPS_RO(health_check);
627
628 struct kset *lustre_kset;
629 EXPORT_SYMBOL_GPL(lustre_kset);
630
631 static struct attribute_group lustre_attr_group = {
632         .attrs = lustre_attrs,
633 };
634
635 ssize_t class_set_global(const char *param)
636 {
637         const char *value = strchr(param, '=') + 1;
638         size_t off = value - param - 1;
639         ssize_t count = -ENOENT;
640         int i;
641
642         for (i = 0; lustre_attrs[i]; i++) {
643                 if (!strncmp(lustre_attrs[i]->name, param, off)) {
644                         count = lustre_attr_store(&lustre_kset->kobj,
645                                                   lustre_attrs[i], value,
646                                                   strlen(value));
647                         break;
648                 }
649         }
650         return count;
651 }
652
653 int class_procfs_init(void)
654 {
655         struct proc_dir_entry *entry;
656         struct dentry *file;
657         int rc = -ENOMEM;
658
659         ENTRY;
660
661         lustre_kset = kset_create_and_add("lustre", NULL, fs_kobj);
662         if (!lustre_kset)
663                 goto out;
664
665         /* Create the files associated with this kobject */
666         rc = sysfs_create_group(&lustre_kset->kobj, &lustre_attr_group);
667         if (rc) {
668                 kset_unregister(lustre_kset);
669                 goto out;
670         }
671
672         rc = jobid_cache_init();
673         if (rc) {
674                 kset_unregister(lustre_kset);
675                 goto out;
676         }
677
678         debugfs_lustre_root = debugfs_create_dir("lustre", NULL);
679
680         file = debugfs_create_file("devices", 0444, debugfs_lustre_root, NULL,
681                                    &obd_device_list_fops);
682
683         file = debugfs_create_file("health_check", 0444, debugfs_lustre_root,
684                                    NULL, &health_check_fops);
685
686         file = debugfs_create_file("checksum_speed", 0444, debugfs_lustre_root,
687                                    NULL, &checksum_speed_fops);
688
689         entry = lprocfs_register("fs/lustre", NULL, NULL, NULL);
690         if (IS_ERR(entry)) {
691                 rc = PTR_ERR(entry);
692                 CERROR("cannot create '/proc/fs/lustre': rc = %d\n", rc);
693                 debugfs_remove_recursive(debugfs_lustre_root);
694                 kset_unregister(lustre_kset);
695                 goto out;
696         }
697
698         proc_lustre_root = entry;
699 out:
700         RETURN(rc);
701 }
702
703 int class_procfs_clean(void)
704 {
705         ENTRY;
706
707         debugfs_remove_recursive(debugfs_lustre_root);
708
709         debugfs_lustre_root = NULL;
710         jobid_cache_fini();
711
712         if (proc_lustre_root)
713                 lprocfs_remove(&proc_lustre_root);
714
715         sysfs_remove_group(&lustre_kset->kobj, &lustre_attr_group);
716
717         kset_unregister(lustre_kset);
718
719         RETURN(0);
720 }