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/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/obdclass/obd_sysfs.c
34 * Object Devices Class Driver
35 * These are the only exported functions, they provide some generic
36 * infrastructure for managing object devices
39 #define DEBUG_SUBSYSTEM S_CLASS
41 #include <linux/module.h>
42 #include <linux/errno.h>
43 #include <linux/kernel.h>
44 #include <linux/sched.h>
46 #include <linux/slab.h>
47 #include <linux/ioport.h>
48 #include <linux/fcntl.h>
49 #include <linux/delay.h>
50 #include <linux/skbuff.h>
51 #include <linux/proc_fs.h>
53 #include <linux/poll.h>
54 #include <linux/init.h>
55 #include <linux/list.h>
56 #include <linux/highmem.h>
58 #include <asm/ioctls.h>
60 #include <asm/uaccess.h>
61 #include <linux/miscdevice.h>
62 #include <linux/seq_file.h>
63 #include <linux/kobject.h>
65 #include <libcfs/libcfs.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 > ((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 static ssize_t version_show(struct kobject *kobj, struct attribute *attr,
183 return sprintf(buf, "%s\n", LUSTRE_VERSION_STRING);
186 static ssize_t pinger_show(struct kobject *kobj, struct attribute *attr,
190 const char *state = "on";
192 const char *state = "off";
194 return sprintf(buf, "%s\n", state);
198 * Check all obd devices health
203 * \retval number of characters printed if healthy
206 health_check_show(struct kobject *kobj, struct attribute *attr, char *buf)
212 if (libcfs_catastrophe) {
213 len = sprintf(buf, "LBUG\n");
217 read_lock(&obd_dev_lock);
218 for (i = 0; i < class_devno_max(); i++) {
219 struct obd_device *obd;
221 obd = class_num2obd(i);
222 if (obd == NULL || !obd->obd_attached || !obd->obd_set_up)
225 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
226 if (obd->obd_stopping)
229 class_incref(obd, __FUNCTION__, current);
230 read_unlock(&obd_dev_lock);
232 if (obd_health_check(NULL, obd)) {
233 len = sprintf(buf, "device %s reported unhealthy\n",
237 class_decref(obd, __FUNCTION__, current);
238 read_lock(&obd_dev_lock);
240 read_unlock(&obd_dev_lock);
243 len = sprintf(buf, "healthy\n");
245 len = sprintf(buf, "NOT HEALTHY\n");
250 static ssize_t jobid_var_show(struct kobject *kobj, struct attribute *attr,
255 if (strlen(obd_jobid_var))
256 rc = snprintf(buf, PAGE_SIZE, "%s\n", obd_jobid_var);
260 static ssize_t jobid_var_store(struct kobject *kobj, struct attribute *attr,
261 const char *buffer, size_t count)
263 if (!count || count > JOBSTATS_JOBID_VAR_MAX_LEN)
266 memset(obd_jobid_var, 0, JOBSTATS_JOBID_VAR_MAX_LEN + 1);
268 memcpy(obd_jobid_var, buffer, count);
270 /* Trim the trailing '\n' if any */
271 if (obd_jobid_var[count - 1] == '\n')
272 obd_jobid_var[count - 1] = 0;
277 static ssize_t jobid_name_show(struct kobject *kobj, struct attribute *attr,
282 if (strlen(obd_jobid_name))
283 rc = snprintf(buf, PAGE_SIZE, "%s\n", obd_jobid_name);
287 static ssize_t jobid_name_store(struct kobject *kobj, struct attribute *attr,
288 const char *buffer, size_t count)
290 if (!count || count > LUSTRE_JOBID_SIZE)
293 if (strcmp(obd_jobid_var, JOBSTATS_NODELOCAL) != 0 &&
294 !strchr(buffer, '%')) {
295 lustre_jobid_clear(buffer);
299 /* clear previous value */
300 memset(obd_jobid_name, 0, LUSTRE_JOBID_SIZE);
302 memcpy(obd_jobid_name, buffer, count);
304 /* Trim the trailing '\n' if any */
305 if (obd_jobid_name[count - 1] == '\n') {
306 /* Don't echo just a newline */
309 obd_jobid_name[count - 1] = 0;
315 /* Root for /sys/kernel/debug/lustre */
316 struct dentry *debugfs_lustre_root;
317 EXPORT_SYMBOL_GPL(debugfs_lustre_root);
319 #ifdef CONFIG_PROC_FS
320 /* Root for /proc/fs/lustre */
321 struct proc_dir_entry *proc_lustre_root;
322 EXPORT_SYMBOL(proc_lustre_root);
324 #define lprocfs_base NULL
325 #endif /* CONFIG_PROC_FS */
327 LUSTRE_RO_ATTR(version);
328 LUSTRE_RO_ATTR(pinger);
329 LUSTRE_RO_ATTR(health_check);
330 LUSTRE_RW_ATTR(jobid_var);
331 LUSTRE_RW_ATTR(jobid_name);
333 static struct attribute *lustre_attrs[] = {
334 &lustre_attr_version.attr,
335 &lustre_attr_pinger.attr,
336 &lustre_attr_health_check.attr,
337 &lustre_attr_jobid_name.attr,
338 &lustre_attr_jobid_var.attr,
339 &lustre_sattr_timeout.u.attr,
340 &lustre_attr_max_dirty_mb.attr,
341 &lustre_sattr_debug_peer_on_timeout.u.attr,
342 &lustre_sattr_dump_on_timeout.u.attr,
343 &lustre_sattr_dump_on_eviction.u.attr,
344 &lustre_sattr_at_min.u.attr,
345 &lustre_sattr_at_max.u.attr,
346 &lustre_sattr_at_extra.u.attr,
347 &lustre_sattr_at_early_margin.u.attr,
348 &lustre_sattr_at_history.u.attr,
349 &lustre_attr_memused_max.attr,
350 &lustre_attr_memused.attr,
351 #ifdef HAVE_SERVER_SUPPORT
352 &lustre_sattr_ldlm_timeout.u.attr,
353 &lustre_sattr_bulk_timeout.u.attr,
355 &lustre_sattr_lbug_on_eviction.u.attr,
359 static void *obd_device_list_seq_start(struct seq_file *p, loff_t *pos)
361 if (*pos >= class_devno_max())
367 static void obd_device_list_seq_stop(struct seq_file *p, void *v)
371 static void *obd_device_list_seq_next(struct seq_file *p, void *v, loff_t *pos)
374 if (*pos >= class_devno_max())
380 static int obd_device_list_seq_show(struct seq_file *p, void *v)
382 loff_t index = *(loff_t *)v;
383 struct obd_device *obd = class_num2obd((int)index);
389 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
390 if (obd->obd_stopping)
392 else if (obd->obd_inactive)
394 else if (obd->obd_set_up)
396 else if (obd->obd_attached)
401 seq_printf(p, "%3d %s %s %s %s %d\n",
402 (int)index, status, obd->obd_type->typ_name,
403 obd->obd_name, obd->obd_uuid.uuid,
404 atomic_read(&obd->obd_refcount));
408 static const struct seq_operations obd_device_list_sops = {
409 .start = obd_device_list_seq_start,
410 .stop = obd_device_list_seq_stop,
411 .next = obd_device_list_seq_next,
412 .show = obd_device_list_seq_show,
415 static int obd_device_list_open(struct inode *inode, struct file *file)
417 struct seq_file *seq;
418 int rc = seq_open(file, &obd_device_list_sops);
423 seq = file->private_data;
424 seq->private = inode->i_private;
428 static const struct file_operations obd_device_list_fops = {
429 .owner = THIS_MODULE,
430 .open = obd_device_list_open,
433 .release = seq_release,
436 struct kset *lustre_kset;
437 EXPORT_SYMBOL_GPL(lustre_kset);
439 static struct attribute_group lustre_attr_group = {
440 .attrs = lustre_attrs,
443 ssize_t class_set_global(const char *param)
445 const char *value = strchr(param, '=') + 1;
446 size_t off = value - param - 1;
447 ssize_t count = -ENOENT;
450 for (i = 0; lustre_attrs[i]; i++) {
451 if (!strncmp(lustre_attrs[i]->name, param, off)) {
452 count = lustre_attr_store(&lustre_kset->kobj,
453 lustre_attrs[i], value,
461 int class_procfs_init(void)
463 struct proc_dir_entry *entry;
469 lustre_kset = kset_create_and_add("lustre", NULL, fs_kobj);
473 /* Create the files associated with this kobject */
474 rc = sysfs_create_group(&lustre_kset->kobj, &lustre_attr_group);
476 kset_unregister(lustre_kset);
480 rc = jobid_cache_init();
482 kset_unregister(lustre_kset);
486 debugfs_lustre_root = debugfs_create_dir("lustre", NULL);
487 if (IS_ERR_OR_NULL(debugfs_lustre_root)) {
488 rc = debugfs_lustre_root ? PTR_ERR(debugfs_lustre_root)
490 debugfs_lustre_root = NULL;
491 kset_unregister(lustre_kset);
495 file = debugfs_create_file("devices", 0444, debugfs_lustre_root, NULL,
496 &obd_device_list_fops);
497 if (IS_ERR_OR_NULL(file)) {
498 rc = file ? PTR_ERR(file) : -ENOMEM;
499 debugfs_remove(debugfs_lustre_root);
500 kset_unregister(lustre_kset);
504 entry = lprocfs_register("fs/lustre", NULL, NULL, NULL);
507 CERROR("cannot create '/proc/fs/lustre': rc = %d\n", rc);
508 debugfs_remove_recursive(debugfs_lustre_root);
509 kset_unregister(lustre_kset);
513 proc_lustre_root = entry;
518 int class_procfs_clean(void)
522 debugfs_remove_recursive(debugfs_lustre_root);
524 debugfs_lustre_root = NULL;
527 if (proc_lustre_root)
528 lprocfs_remove(&proc_lustre_root);
530 sysfs_remove_group(&lustre_kset->kobj, &lustre_attr_group);
532 kset_unregister(lustre_kset);