4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * lustre/obdclass/obd_sysfs.c
33 * Object Devices Class Driver
34 * These are the only exported functions, they provide some generic
35 * infrastructure for managing object devices
38 #define DEBUG_SUBSYSTEM S_CLASS
40 #include <linux/module.h>
41 #include <linux/errno.h>
42 #include <linux/kernel.h>
43 #include <linux/sched.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>
52 #include <linux/poll.h>
53 #include <linux/init.h>
54 #include <linux/list.h>
55 #include <linux/highmem.h>
57 #include <asm/ioctls.h>
59 #include <asm/uaccess.h>
60 #include <linux/miscdevice.h>
61 #include <linux/seq_file.h>
62 #include <linux/kobject.h>
64 #include <libcfs/libcfs.h>
65 #include <obd_support.h>
66 #include <obd_class.h>
67 #include <lprocfs_status.h>
68 #include <uapi/linux/lnet/lnetctl.h>
69 #include <uapi/linux/lustre/lustre_ioctl.h>
70 #include <uapi/linux/lustre/lustre_ver.h>
72 struct static_lustre_uintvalue_attr {
74 struct attribute attr;
75 ssize_t (*show)(struct kobject *kobj, struct attribute *attr,
77 ssize_t (*store)(struct kobject *kobj, struct attribute *attr,
78 const char *buf, size_t len);
83 static ssize_t static_uintvalue_show(struct kobject *kobj,
84 struct attribute *attr,
87 struct static_lustre_uintvalue_attr *lattr = (void *)attr;
89 return sprintf(buf, "%d\n", *lattr->value);
92 static ssize_t static_uintvalue_store(struct kobject *kobj,
93 struct attribute *attr,
94 const char *buffer, size_t count)
96 struct static_lustre_uintvalue_attr *lattr = (void *)attr;
100 rc = kstrtouint(buffer, 10, &val);
109 #define LUSTRE_STATIC_UINT_ATTR(name, value) \
110 static struct static_lustre_uintvalue_attr lustre_sattr_##name = \
111 { __ATTR(name, 0644, static_uintvalue_show, \
112 static_uintvalue_store), value }
114 LUSTRE_STATIC_UINT_ATTR(timeout, &obd_timeout);
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(lbug_on_eviction, &obd_lbug_on_eviction);
125 #ifdef HAVE_SERVER_SUPPORT
126 LUSTRE_STATIC_UINT_ATTR(ldlm_timeout, &ldlm_timeout);
127 LUSTRE_STATIC_UINT_ATTR(bulk_timeout, &bulk_timeout);
130 static ssize_t memused_show(struct kobject *kobj, struct attribute *attr,
133 return sprintf(buf, "%llu\n", obd_memory_sum());
135 LUSTRE_RO_ATTR(memused);
137 static ssize_t memused_max_show(struct kobject *kobj, struct attribute *attr,
140 return sprintf(buf, "%llu\n", obd_memory_max());
142 LUSTRE_RO_ATTR(memused_max);
144 static ssize_t max_dirty_mb_show(struct kobject *kobj, struct attribute *attr,
147 return sprintf(buf, "%lu\n",
148 obd_max_dirty_pages / (1 << (20 - PAGE_SHIFT)));
151 static ssize_t max_dirty_mb_store(struct kobject *kobj, struct attribute *attr,
152 const char *buffer, size_t count)
157 rc = kstrtoul(buffer, 10, &val);
161 val *= 1 << (20 - PAGE_SHIFT); /* convert to pages */
163 if (val > ((cfs_totalram_pages() / 10) * 9)) {
164 /* Somebody wants to assign too much memory to dirty pages */
168 if (val < 4 << (20 - PAGE_SHIFT)) {
169 /* Less than 4 Mb for dirty cache is also bad */
173 obd_max_dirty_pages = val;
177 LUSTRE_RW_ATTR(max_dirty_mb);
179 #ifdef HAVE_SERVER_SUPPORT
180 static ssize_t no_transno_store(struct kobject *kobj,
181 struct attribute *attr,
182 const char *buffer, size_t count)
184 struct obd_device *obd;
188 rc = kstrtouint(buffer, 10, &idx);
192 obd = class_num2obd(idx);
193 if (!obd || !obd->obd_attached) {
195 CERROR("%s: not attached\n", obd->obd_name);
199 spin_lock(&obd->obd_dev_lock);
200 obd->obd_no_transno = 1;
201 spin_unlock(&obd->obd_dev_lock);
204 LUSTRE_WO_ATTR(no_transno);
205 #endif /* HAVE_SERVER_SUPPORT */
207 static ssize_t version_show(struct kobject *kobj, struct attribute *attr,
210 return sprintf(buf, "%s\n", LUSTRE_VERSION_STRING);
213 static ssize_t pinger_show(struct kobject *kobj, struct attribute *attr,
216 #ifdef CONFIG_LUSTRE_FS_PINGER
217 const char *state = "on";
219 const char *state = "off";
221 return sprintf(buf, "%s\n", state);
225 * Check all obd devices health
230 * \retval number of characters printed if healthy
233 health_check_show(struct kobject *kobj, struct attribute *attr, char *buf)
239 if (libcfs_catastrophe)
240 return sprintf(buf, "LBUG\n");
242 read_lock(&obd_dev_lock);
243 for (i = 0; i < class_devno_max(); i++) {
244 struct obd_device *obd;
246 obd = class_num2obd(i);
247 if (obd == NULL || !obd->obd_attached || !obd->obd_set_up)
250 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
251 if (obd->obd_stopping)
254 class_incref(obd, __func__, current);
255 read_unlock(&obd_dev_lock);
257 if (obd_health_check(NULL, obd))
260 class_decref(obd, __func__, current);
261 read_lock(&obd_dev_lock);
266 read_unlock(&obd_dev_lock);
269 len = sprintf(buf, "healthy\n");
271 len = sprintf(buf, "NOT HEALTHY\n");
276 static ssize_t jobid_var_show(struct kobject *kobj, struct attribute *attr,
281 if (strlen(obd_jobid_var))
282 rc = scnprintf(buf, PAGE_SIZE, "%s\n", obd_jobid_var);
286 static ssize_t jobid_var_store(struct kobject *kobj, struct attribute *attr,
287 const char *buffer, size_t count)
289 if (!count || count > JOBSTATS_JOBID_VAR_MAX_LEN)
292 memset(obd_jobid_var, 0, JOBSTATS_JOBID_VAR_MAX_LEN + 1);
294 memcpy(obd_jobid_var, buffer, count);
296 /* Trim the trailing '\n' if any */
297 if (obd_jobid_var[count - 1] == '\n')
298 obd_jobid_var[count - 1] = 0;
303 static ssize_t jobid_name_show(struct kobject *kobj, struct attribute *attr,
308 if (strlen(obd_jobid_name))
309 rc = scnprintf(buf, PAGE_SIZE, "%s\n", obd_jobid_name);
313 static ssize_t jobid_name_store(struct kobject *kobj, struct attribute *attr,
314 const char *buffer, size_t count)
316 if (!count || count > LUSTRE_JOBID_SIZE)
319 if (strcmp(obd_jobid_var, JOBSTATS_NODELOCAL) != 0 &&
320 !strchr(buffer, '%')) {
321 lustre_jobid_clear(buffer);
325 /* clear previous value */
326 memset(obd_jobid_name, 0, LUSTRE_JOBID_SIZE);
328 memcpy(obd_jobid_name, buffer, count);
330 /* Trim the trailing '\n' if any */
331 if (obd_jobid_name[count - 1] == '\n') {
332 /* Don't echo just a newline */
335 obd_jobid_name[count - 1] = 0;
341 static ssize_t jobid_this_session_show(struct kobject *kobj,
342 struct attribute *attr,
349 jid = jobid_current();
351 ret = scnprintf(buf, PAGE_SIZE, "%s\n", jid);
356 static ssize_t jobid_this_session_store(struct kobject *kobj,
357 struct attribute *attr,
365 if (!count || count > LUSTRE_JOBID_SIZE)
368 jobid = kstrndup(buffer, count, GFP_KERNEL);
371 len = strcspn(jobid, "\n ");
373 ret = jobid_set_current(jobid);
379 /* Root for /sys/kernel/debug/lustre */
380 struct dentry *debugfs_lustre_root;
381 EXPORT_SYMBOL_GPL(debugfs_lustre_root);
383 #ifdef CONFIG_PROC_FS
384 /* Root for /proc/fs/lustre */
385 struct proc_dir_entry *proc_lustre_root;
386 EXPORT_SYMBOL(proc_lustre_root);
388 #define lprocfs_base NULL
389 #endif /* CONFIG_PROC_FS */
391 LUSTRE_RO_ATTR(version);
392 LUSTRE_RO_ATTR(pinger);
393 LUSTRE_RO_ATTR(health_check);
394 LUSTRE_RW_ATTR(jobid_var);
395 LUSTRE_RW_ATTR(jobid_name);
396 LUSTRE_RW_ATTR(jobid_this_session);
398 static struct attribute *lustre_attrs[] = {
399 &lustre_attr_version.attr,
400 &lustre_attr_pinger.attr,
401 &lustre_attr_health_check.attr,
402 &lustre_attr_jobid_name.attr,
403 &lustre_attr_jobid_var.attr,
404 &lustre_attr_jobid_this_session.attr,
405 &lustre_sattr_timeout.u.attr,
406 &lustre_attr_max_dirty_mb.attr,
407 &lustre_sattr_debug_peer_on_timeout.u.attr,
408 &lustre_sattr_dump_on_timeout.u.attr,
409 &lustre_sattr_dump_on_eviction.u.attr,
410 &lustre_sattr_at_min.u.attr,
411 &lustre_sattr_at_max.u.attr,
412 &lustre_sattr_at_extra.u.attr,
413 &lustre_sattr_at_early_margin.u.attr,
414 &lustre_sattr_at_history.u.attr,
415 &lustre_attr_memused_max.attr,
416 &lustre_attr_memused.attr,
417 #ifdef HAVE_SERVER_SUPPORT
418 &lustre_sattr_ldlm_timeout.u.attr,
419 &lustre_sattr_bulk_timeout.u.attr,
420 &lustre_attr_no_transno.attr,
422 &lustre_sattr_lbug_on_eviction.u.attr,
426 static void *obd_device_list_seq_start(struct seq_file *p, loff_t *pos)
428 if (*pos >= class_devno_max())
434 static void obd_device_list_seq_stop(struct seq_file *p, void *v)
438 static void *obd_device_list_seq_next(struct seq_file *p, void *v, loff_t *pos)
441 if (*pos >= class_devno_max())
447 static int obd_device_list_seq_show(struct seq_file *p, void *v)
449 loff_t index = *(loff_t *)v;
450 struct obd_device *obd = class_num2obd((int)index);
456 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
457 if (obd->obd_stopping)
459 else if (obd->obd_inactive)
461 else if (obd->obd_set_up)
463 else if (obd->obd_attached)
468 seq_printf(p, "%3d %s %s %s %s %d\n",
469 (int)index, status, obd->obd_type->typ_name,
470 obd->obd_name, obd->obd_uuid.uuid,
471 atomic_read(&obd->obd_refcount));
475 static const struct seq_operations obd_device_list_sops = {
476 .start = obd_device_list_seq_start,
477 .stop = obd_device_list_seq_stop,
478 .next = obd_device_list_seq_next,
479 .show = obd_device_list_seq_show,
482 static int obd_device_list_open(struct inode *inode, struct file *file)
484 struct seq_file *seq;
485 int rc = seq_open(file, &obd_device_list_sops);
490 seq = file->private_data;
491 seq->private = inode->i_private;
495 static const struct file_operations obd_device_list_fops = {
496 .owner = THIS_MODULE,
497 .open = obd_device_list_open,
500 .release = seq_release,
504 health_check_seq_show(struct seq_file *m, void *unused)
508 read_lock(&obd_dev_lock);
509 for (i = 0; i < class_devno_max(); i++) {
510 struct obd_device *obd;
512 obd = class_num2obd(i);
513 if (obd == NULL || !obd->obd_attached || !obd->obd_set_up)
516 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
517 if (obd->obd_stopping)
520 class_incref(obd, __func__, current);
521 read_unlock(&obd_dev_lock);
523 if (obd_health_check(NULL, obd)) {
524 seq_printf(m, "device %s reported unhealthy\n",
527 class_decref(obd, __func__, current);
528 read_lock(&obd_dev_lock);
530 read_unlock(&obd_dev_lock);
535 LDEBUGFS_SEQ_FOPS_RO(health_check);
537 struct kset *lustre_kset;
538 EXPORT_SYMBOL_GPL(lustre_kset);
540 static struct attribute_group lustre_attr_group = {
541 .attrs = lustre_attrs,
544 ssize_t class_set_global(const char *param)
546 const char *value = strchr(param, '=') + 1;
547 size_t off = value - param - 1;
548 ssize_t count = -ENOENT;
551 for (i = 0; lustre_attrs[i]; i++) {
552 if (!strncmp(lustre_attrs[i]->name, param, off)) {
553 count = lustre_attr_store(&lustre_kset->kobj,
554 lustre_attrs[i], value,
562 int class_procfs_init(void)
564 struct proc_dir_entry *entry;
570 lustre_kset = kset_create_and_add("lustre", NULL, fs_kobj);
574 /* Create the files associated with this kobject */
575 rc = sysfs_create_group(&lustre_kset->kobj, &lustre_attr_group);
577 kset_unregister(lustre_kset);
581 rc = jobid_cache_init();
583 kset_unregister(lustre_kset);
587 debugfs_lustre_root = debugfs_create_dir("lustre", NULL);
589 file = debugfs_create_file("devices", 0444, debugfs_lustre_root, NULL,
590 &obd_device_list_fops);
592 file = debugfs_create_file("health_check", 0444, debugfs_lustre_root,
593 NULL, &health_check_fops);
595 entry = lprocfs_register("fs/lustre", NULL, NULL, NULL);
598 CERROR("cannot create '/proc/fs/lustre': rc = %d\n", rc);
599 debugfs_remove_recursive(debugfs_lustre_root);
600 kset_unregister(lustre_kset);
604 proc_lustre_root = entry;
609 int class_procfs_clean(void)
613 debugfs_remove_recursive(debugfs_lustre_root);
615 debugfs_lustre_root = NULL;
618 if (proc_lustre_root)
619 lprocfs_remove(&proc_lustre_root);
621 sysfs_remove_group(&lustre_kset->kobj, &lustre_attr_group);
623 kset_unregister(lustre_kset);