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 <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>
73 struct static_lustre_uintvalue_attr {
75 struct attribute attr;
76 ssize_t (*show)(struct kobject *kobj, struct attribute *attr,
78 ssize_t (*store)(struct kobject *kobj, struct attribute *attr,
79 const char *buf, size_t len);
84 static ssize_t static_uintvalue_show(struct kobject *kobj,
85 struct attribute *attr,
88 struct static_lustre_uintvalue_attr *lattr = (void *)attr;
90 return sprintf(buf, "%d\n", *lattr->value);
93 static ssize_t static_uintvalue_store(struct kobject *kobj,
94 struct attribute *attr,
95 const char *buffer, size_t count)
97 struct static_lustre_uintvalue_attr *lattr = (void *)attr;
101 rc = kstrtouint(buffer, 10, &val);
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 }
115 LUSTRE_STATIC_UINT_ATTR(timeout, &obd_timeout);
116 LUSTRE_STATIC_UINT_ATTR(debug_peer_on_timeout, &obd_debug_peer_on_timeout);
117 LUSTRE_STATIC_UINT_ATTR(dump_on_timeout, &obd_dump_on_timeout);
118 LUSTRE_STATIC_UINT_ATTR(dump_on_eviction, &obd_dump_on_eviction);
119 LUSTRE_STATIC_UINT_ATTR(at_min, &at_min);
120 LUSTRE_STATIC_UINT_ATTR(at_max, &at_max);
121 LUSTRE_STATIC_UINT_ATTR(at_extra, &at_extra);
122 LUSTRE_STATIC_UINT_ATTR(at_early_margin, &at_early_margin);
123 LUSTRE_STATIC_UINT_ATTR(at_history, &at_history);
124 LUSTRE_STATIC_UINT_ATTR(lbug_on_eviction, &obd_lbug_on_eviction);
126 #ifdef HAVE_SERVER_SUPPORT
127 LUSTRE_STATIC_UINT_ATTR(ldlm_timeout, &ldlm_timeout);
128 LUSTRE_STATIC_UINT_ATTR(bulk_timeout, &bulk_timeout);
131 static ssize_t memused_show(struct kobject *kobj, struct attribute *attr,
134 return sprintf(buf, "%llu\n", obd_memory_sum());
136 LUSTRE_RO_ATTR(memused);
138 static ssize_t memused_max_show(struct kobject *kobj, struct attribute *attr,
141 return sprintf(buf, "%llu\n", obd_memory_max());
143 LUSTRE_RO_ATTR(memused_max);
145 static ssize_t max_dirty_mb_show(struct kobject *kobj, struct attribute *attr,
148 return sprintf(buf, "%lu\n",
149 obd_max_dirty_pages / (1 << (20 - PAGE_SHIFT)));
152 static ssize_t max_dirty_mb_store(struct kobject *kobj, struct attribute *attr,
153 const char *buffer, size_t count)
158 rc = kstrtoul(buffer, 10, &val);
162 val *= 1 << (20 - PAGE_SHIFT); /* convert to pages */
164 if (val > ((cfs_totalram_pages() / 10) * 9)) {
165 /* Somebody wants to assign too much memory to dirty pages */
169 if (val < 4 << (20 - PAGE_SHIFT)) {
170 /* Less than 4 Mb for dirty cache is also bad */
174 obd_max_dirty_pages = val;
178 LUSTRE_RW_ATTR(max_dirty_mb);
180 #ifdef HAVE_SERVER_SUPPORT
181 static ssize_t no_transno_store(struct kobject *kobj,
182 struct attribute *attr,
183 const char *buffer, size_t count)
185 struct obd_device *obd;
189 rc = kstrtouint(buffer, 10, &idx);
193 obd = class_num2obd(idx);
194 if (!obd || !obd->obd_attached) {
196 CERROR("%s: not attached\n", obd->obd_name);
200 spin_lock(&obd->obd_dev_lock);
201 obd->obd_no_transno = 1;
202 spin_unlock(&obd->obd_dev_lock);
205 LUSTRE_WO_ATTR(no_transno);
206 #endif /* HAVE_SERVER_SUPPORT */
208 static ssize_t version_show(struct kobject *kobj, struct attribute *attr,
211 return sprintf(buf, "%s\n", LUSTRE_VERSION_STRING);
214 static ssize_t pinger_show(struct kobject *kobj, struct attribute *attr,
217 #ifdef CONFIG_LUSTRE_FS_PINGER
218 const char *state = "on";
220 const char *state = "off";
222 return sprintf(buf, "%s\n", state);
226 * Check all obd devices health
231 * \retval number of characters printed if healthy
234 health_check_show(struct kobject *kobj, struct attribute *attr, char *buf)
240 if (libcfs_catastrophe)
241 return sprintf(buf, "LBUG\n");
243 read_lock(&obd_dev_lock);
244 for (i = 0; i < class_devno_max(); i++) {
245 struct obd_device *obd;
247 obd = class_num2obd(i);
248 if (obd == NULL || !obd->obd_attached || !obd->obd_set_up)
251 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
252 if (obd->obd_stopping)
255 class_incref(obd, __func__, current);
256 read_unlock(&obd_dev_lock);
258 if (obd_health_check(NULL, obd))
261 class_decref(obd, __func__, current);
262 read_lock(&obd_dev_lock);
267 read_unlock(&obd_dev_lock);
270 len = sprintf(buf, "healthy\n");
272 len = sprintf(buf, "NOT HEALTHY\n");
277 static ssize_t jobid_var_show(struct kobject *kobj, struct attribute *attr,
282 if (strlen(obd_jobid_var))
283 rc = scnprintf(buf, PAGE_SIZE, "%s\n", obd_jobid_var);
287 static ssize_t jobid_var_store(struct kobject *kobj, struct attribute *attr,
288 const char *buffer, size_t count)
290 if (!count || count > JOBSTATS_JOBID_VAR_MAX_LEN)
293 memset(obd_jobid_var, 0, JOBSTATS_JOBID_VAR_MAX_LEN + 1);
295 memcpy(obd_jobid_var, buffer, count);
297 /* Trim the trailing '\n' if any */
298 if (obd_jobid_var[count - 1] == '\n')
299 obd_jobid_var[count - 1] = 0;
304 static ssize_t jobid_name_show(struct kobject *kobj, struct attribute *attr,
309 if (strlen(obd_jobid_name))
310 rc = scnprintf(buf, PAGE_SIZE, "%s\n", obd_jobid_name);
314 static ssize_t jobid_name_store(struct kobject *kobj, struct attribute *attr,
315 const char *buffer, size_t count)
317 if (!count || count > LUSTRE_JOBID_SIZE)
320 if (strcmp(obd_jobid_var, JOBSTATS_NODELOCAL) != 0 &&
321 !strchr(buffer, '%')) {
322 lustre_jobid_clear(buffer);
326 /* clear previous value */
327 memset(obd_jobid_name, 0, LUSTRE_JOBID_SIZE);
329 memcpy(obd_jobid_name, buffer, count);
331 /* Trim the trailing '\n' if any */
332 if (obd_jobid_name[count - 1] == '\n') {
333 /* Don't echo just a newline */
336 obd_jobid_name[count - 1] = 0;
342 static ssize_t jobid_this_session_show(struct kobject *kobj,
343 struct attribute *attr,
350 jid = jobid_current();
352 ret = scnprintf(buf, PAGE_SIZE, "%s\n", jid);
357 static ssize_t jobid_this_session_store(struct kobject *kobj,
358 struct attribute *attr,
366 if (!count || count > LUSTRE_JOBID_SIZE)
369 jobid = kstrndup(buffer, count, GFP_KERNEL);
372 len = strcspn(jobid, "\n ");
374 ret = jobid_set_current(jobid);
380 /* Root for /sys/kernel/debug/lustre */
381 struct dentry *debugfs_lustre_root;
382 EXPORT_SYMBOL_GPL(debugfs_lustre_root);
384 #ifdef CONFIG_PROC_FS
385 /* Root for /proc/fs/lustre */
386 struct proc_dir_entry *proc_lustre_root;
387 EXPORT_SYMBOL(proc_lustre_root);
389 #define lprocfs_base NULL
390 #endif /* CONFIG_PROC_FS */
392 LUSTRE_RO_ATTR(version);
393 LUSTRE_RO_ATTR(pinger);
394 LUSTRE_RO_ATTR(health_check);
395 LUSTRE_RW_ATTR(jobid_var);
396 LUSTRE_RW_ATTR(jobid_name);
397 LUSTRE_RW_ATTR(jobid_this_session);
399 static struct attribute *lustre_attrs[] = {
400 &lustre_attr_version.attr,
401 &lustre_attr_pinger.attr,
402 &lustre_attr_health_check.attr,
403 &lustre_attr_jobid_name.attr,
404 &lustre_attr_jobid_var.attr,
405 &lustre_attr_jobid_this_session.attr,
406 &lustre_sattr_timeout.u.attr,
407 &lustre_attr_max_dirty_mb.attr,
408 &lustre_sattr_debug_peer_on_timeout.u.attr,
409 &lustre_sattr_dump_on_timeout.u.attr,
410 &lustre_sattr_dump_on_eviction.u.attr,
411 &lustre_sattr_at_min.u.attr,
412 &lustre_sattr_at_max.u.attr,
413 &lustre_sattr_at_extra.u.attr,
414 &lustre_sattr_at_early_margin.u.attr,
415 &lustre_sattr_at_history.u.attr,
416 &lustre_attr_memused_max.attr,
417 &lustre_attr_memused.attr,
418 #ifdef HAVE_SERVER_SUPPORT
419 &lustre_sattr_ldlm_timeout.u.attr,
420 &lustre_sattr_bulk_timeout.u.attr,
421 &lustre_attr_no_transno.attr,
423 &lustre_sattr_lbug_on_eviction.u.attr,
427 static void *obd_device_list_seq_start(struct seq_file *p, loff_t *pos)
429 if (*pos >= class_devno_max())
435 static void obd_device_list_seq_stop(struct seq_file *p, void *v)
439 static void *obd_device_list_seq_next(struct seq_file *p, void *v, loff_t *pos)
442 if (*pos >= class_devno_max())
448 static int obd_device_list_seq_show(struct seq_file *p, void *v)
450 loff_t index = *(loff_t *)v;
451 struct obd_device *obd = class_num2obd((int)index);
457 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
458 if (obd->obd_stopping)
460 else if (obd->obd_inactive)
462 else if (obd->obd_set_up)
464 else if (obd->obd_attached)
469 seq_printf(p, "%3d %s %s %s %s %d\n",
470 (int)index, status, obd->obd_type->typ_name,
471 obd->obd_name, obd->obd_uuid.uuid,
472 atomic_read(&obd->obd_refcount));
476 static const struct seq_operations obd_device_list_sops = {
477 .start = obd_device_list_seq_start,
478 .stop = obd_device_list_seq_stop,
479 .next = obd_device_list_seq_next,
480 .show = obd_device_list_seq_show,
483 static int obd_device_list_open(struct inode *inode, struct file *file)
485 struct seq_file *seq;
486 int rc = seq_open(file, &obd_device_list_sops);
491 seq = file->private_data;
492 seq->private = inode->i_private;
496 static const struct file_operations obd_device_list_fops = {
497 .owner = THIS_MODULE,
498 .open = obd_device_list_open,
501 .release = seq_release,
505 static void *checksum_speed_start(struct seq_file *p, loff_t *pos)
510 static void checksum_speed_stop(struct seq_file *p, void *v)
514 static void *checksum_speed_next(struct seq_file *p, void *v, loff_t *pos)
517 if (*pos >= CFS_HASH_ALG_SPEED_MAX - 1)
523 static int checksum_speed_show(struct seq_file *p, void *v)
525 loff_t index = *(loff_t *)v;
527 if (!index || index > CFS_HASH_ALG_SPEED_MAX - 1)
530 seq_printf(p, "%s: %d\n", cfs_crypto_hash_name(index),
531 cfs_crypto_hash_speeds[index]);
536 static const struct seq_operations checksum_speed_sops = {
537 .start = checksum_speed_start,
538 .stop = checksum_speed_stop,
539 .next = checksum_speed_next,
540 .show = checksum_speed_show,
543 static int checksum_speed_open(struct inode *inode, struct file *file)
545 int rc = seq_open(file, &checksum_speed_sops);
553 static const struct file_operations checksum_speed_fops = {
554 .owner = THIS_MODULE,
555 .open = checksum_speed_open,
558 .release = seq_release,
562 health_check_seq_show(struct seq_file *m, void *unused)
566 read_lock(&obd_dev_lock);
567 for (i = 0; i < class_devno_max(); i++) {
568 struct obd_device *obd;
570 obd = class_num2obd(i);
571 if (obd == NULL || !obd->obd_attached || !obd->obd_set_up)
574 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
575 if (obd->obd_stopping)
578 class_incref(obd, __func__, current);
579 read_unlock(&obd_dev_lock);
581 if (obd_health_check(NULL, obd)) {
582 seq_printf(m, "device %s reported unhealthy\n",
585 class_decref(obd, __func__, current);
586 read_lock(&obd_dev_lock);
588 read_unlock(&obd_dev_lock);
593 LDEBUGFS_SEQ_FOPS_RO(health_check);
595 struct kset *lustre_kset;
596 EXPORT_SYMBOL_GPL(lustre_kset);
598 static struct attribute_group lustre_attr_group = {
599 .attrs = lustre_attrs,
602 ssize_t class_set_global(const char *param)
604 const char *value = strchr(param, '=') + 1;
605 size_t off = value - param - 1;
606 ssize_t count = -ENOENT;
609 for (i = 0; lustre_attrs[i]; i++) {
610 if (!strncmp(lustre_attrs[i]->name, param, off)) {
611 count = lustre_attr_store(&lustre_kset->kobj,
612 lustre_attrs[i], value,
620 int class_procfs_init(void)
622 struct proc_dir_entry *entry;
628 lustre_kset = kset_create_and_add("lustre", NULL, fs_kobj);
632 /* Create the files associated with this kobject */
633 rc = sysfs_create_group(&lustre_kset->kobj, &lustre_attr_group);
635 kset_unregister(lustre_kset);
639 rc = jobid_cache_init();
641 kset_unregister(lustre_kset);
645 debugfs_lustre_root = debugfs_create_dir("lustre", NULL);
647 file = debugfs_create_file("devices", 0444, debugfs_lustre_root, NULL,
648 &obd_device_list_fops);
650 file = debugfs_create_file("health_check", 0444, debugfs_lustre_root,
651 NULL, &health_check_fops);
653 file = debugfs_create_file("checksum_speed", 0444, debugfs_lustre_root,
654 NULL, &checksum_speed_fops);
656 entry = lprocfs_register("fs/lustre", NULL, NULL, NULL);
659 CERROR("cannot create '/proc/fs/lustre': rc = %d\n", rc);
660 debugfs_remove_recursive(debugfs_lustre_root);
661 kset_unregister(lustre_kset);
665 proc_lustre_root = entry;
670 int class_procfs_clean(void)
674 debugfs_remove_recursive(debugfs_lustre_root);
676 debugfs_lustre_root = NULL;
679 if (proc_lustre_root)
680 lprocfs_remove(&proc_lustre_root);
682 sysfs_remove_group(&lustre_kset->kobj, &lustre_attr_group);
684 kset_unregister(lustre_kset);