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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/obdclass/linux/linux-module.c
38 * Object Devices Class Driver
39 * These are the only exported functions, they provide some generic
40 * infrastructure for managing object devices
43 #define DEBUG_SUBSYSTEM S_CLASS
45 #include <linux/module.h>
46 #include <linux/errno.h>
47 #include <linux/kernel.h>
48 #include <linux/major.h>
49 #include <linux/sched.h>
51 #include <linux/slab.h>
52 #include <linux/ioport.h>
53 #include <linux/fcntl.h>
54 #include <linux/delay.h>
55 #include <linux/skbuff.h>
57 #include <linux/poll.h>
58 #include <linux/list.h>
59 #include <linux/highmem.h>
61 #include <asm/ioctls.h>
62 #include <linux/poll.h>
63 #include <linux/uaccess.h>
64 #include <linux/miscdevice.h>
65 #include <linux/seq_file.h>
66 #include <linux/kobject.h>
68 #include "../../../include/linux/libcfs/libcfs.h"
69 #include "../../../include/linux/lnet/lnetctl.h"
70 #include "../../include/obd_support.h"
71 #include "../../include/obd_class.h"
72 #include "../../include/lprocfs_status.h"
73 #include "../../include/lustre_ver.h"
74 #include "../../include/lustre/lustre_build_version.h"
76 /* buffer MUST be at least the size of obd_ioctl_hdr */
77 int obd_ioctl_getdata(char **buf, int *len, void *arg)
79 struct obd_ioctl_hdr hdr;
80 struct obd_ioctl_data *data;
84 if (copy_from_user(&hdr, (void *)arg, sizeof(hdr)))
87 if (hdr.ioc_version != OBD_IOCTL_VERSION) {
88 CERROR("Version mismatch kernel (%x) vs application (%x)\n",
89 OBD_IOCTL_VERSION, hdr.ioc_version);
93 if (hdr.ioc_len > OBD_MAX_IOCTL_BUFFER) {
94 CERROR("User buffer len %d exceeds %d max buffer\n",
95 hdr.ioc_len, OBD_MAX_IOCTL_BUFFER);
99 if (hdr.ioc_len < sizeof(struct obd_ioctl_data)) {
100 CERROR("User buffer too small for ioctl (%d)\n", hdr.ioc_len);
104 /* When there are lots of processes calling vmalloc on multi-core
105 * system, the high lock contention will hurt performance badly,
106 * obdfilter-survey is an example, which relies on ioctl. So we'd
107 * better avoid vmalloc on ioctl path. LU-66 */
108 *buf = libcfs_kvzalloc(hdr.ioc_len, GFP_NOFS);
110 CERROR("Cannot allocate control buffer of len %d\n",
115 data = (struct obd_ioctl_data *)*buf;
117 if (copy_from_user(*buf, (void *)arg, hdr.ioc_len)) {
121 if (hdr.ioc_len != data->ioc_len) {
126 if (obd_ioctl_is_invalid(data)) {
127 CERROR("ioctl not correctly formatted\n");
132 if (data->ioc_inllen1) {
133 data->ioc_inlbuf1 = &data->ioc_bulk[0];
134 offset += cfs_size_round(data->ioc_inllen1);
137 if (data->ioc_inllen2) {
138 data->ioc_inlbuf2 = &data->ioc_bulk[0] + offset;
139 offset += cfs_size_round(data->ioc_inllen2);
142 if (data->ioc_inllen3) {
143 data->ioc_inlbuf3 = &data->ioc_bulk[0] + offset;
144 offset += cfs_size_round(data->ioc_inllen3);
147 if (data->ioc_inllen4) {
148 data->ioc_inlbuf4 = &data->ioc_bulk[0] + offset;
157 EXPORT_SYMBOL(obd_ioctl_getdata);
159 int obd_ioctl_popdata(void *arg, void *data, int len)
163 err = copy_to_user(arg, data, len);
168 EXPORT_SYMBOL(obd_ioctl_popdata);
170 /* opening /dev/obd */
171 static int obd_class_open(struct inode *inode, struct file *file)
173 try_module_get(THIS_MODULE);
177 /* closing /dev/obd */
178 static int obd_class_release(struct inode *inode, struct file *file)
180 module_put(THIS_MODULE);
184 /* to control /dev/obd */
185 static long obd_class_ioctl(struct file *filp, unsigned int cmd,
190 /* Allow non-root access for OBD_IOC_PING_TARGET - used by lfs check */
191 if (!capable(CFS_CAP_SYS_ADMIN) && (cmd != OBD_IOC_PING_TARGET))
192 return err = -EACCES;
193 if ((cmd & 0xffffff00) == ((int)'T') << 8) /* ignore all tty ioctls */
194 return err = -ENOTTY;
196 err = class_handle_ioctl(cmd, (unsigned long)arg);
201 /* declare character device */
202 static struct file_operations obd_psdev_fops = {
203 .owner = THIS_MODULE,
204 .unlocked_ioctl = obd_class_ioctl, /* unlocked_ioctl */
205 .open = obd_class_open, /* open */
206 .release = obd_class_release, /* release */
210 struct miscdevice obd_psdev = {
211 .minor = OBD_DEV_MINOR,
212 .name = OBD_DEV_NAME,
213 .fops = &obd_psdev_fops,
216 static ssize_t version_show(struct kobject *kobj, struct attribute *attr,
219 return sprintf(buf, "%s\n", LUSTRE_VERSION_STRING);
222 static ssize_t pinger_show(struct kobject *kobj, struct attribute *attr,
225 return sprintf(buf, "%s\n", "on");
228 static ssize_t health_show(struct kobject *kobj, struct attribute *attr,
235 if (libcfs_catastrophe)
236 return sprintf(buf, "LBUG\n");
238 read_lock(&obd_dev_lock);
239 for (i = 0; i < class_devno_max(); i++) {
240 struct obd_device *obd;
242 obd = class_num2obd(i);
243 if (obd == NULL || !obd->obd_attached || !obd->obd_set_up)
246 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
247 if (obd->obd_stopping)
250 class_incref(obd, __func__, current);
251 read_unlock(&obd_dev_lock);
253 if (obd_health_check(NULL, obd)) {
256 class_decref(obd, __func__, current);
257 read_lock(&obd_dev_lock);
259 read_unlock(&obd_dev_lock);
262 len = sprintf(buf, "healthy\n");
264 len = sprintf(buf, "NOT HEALTHY\n");
269 static ssize_t jobid_var_show(struct kobject *kobj, struct attribute *attr,
272 return snprintf(buf, PAGE_SIZE, "%s\n", obd_jobid_var);
275 static ssize_t jobid_var_store(struct kobject *kobj, struct attribute *attr,
279 if (!count || count > JOBSTATS_JOBID_VAR_MAX_LEN)
282 memset(obd_jobid_var, 0, JOBSTATS_JOBID_VAR_MAX_LEN + 1);
284 memcpy(obd_jobid_var, buffer, count);
286 /* Trim the trailing '\n' if any */
287 if (obd_jobid_var[count - 1] == '\n')
288 obd_jobid_var[count - 1] = 0;
293 static ssize_t jobid_name_show(struct kobject *kobj, struct attribute *attr,
296 return snprintf(buf, PAGE_SIZE, "%s\n", obd_jobid_node);
299 static ssize_t jobid_name_store(struct kobject *kobj, struct attribute *attr,
303 if (!count || count > JOBSTATS_JOBID_SIZE)
306 memcpy(obd_jobid_node, buffer, count);
308 obd_jobid_node[count] = 0;
310 /* Trim the trailing '\n' if any */
311 if (obd_jobid_node[count - 1] == '\n')
312 obd_jobid_node[count - 1] = 0;
317 /* Root for /sys/kernel/debug/lustre */
318 struct dentry *debugfs_lustre_root;
319 EXPORT_SYMBOL_GPL(debugfs_lustre_root);
321 LUSTRE_RO_ATTR(version);
322 LUSTRE_RO_ATTR(pinger);
323 LUSTRE_RO_ATTR(health);
324 LUSTRE_RW_ATTR(jobid_var);
325 LUSTRE_RW_ATTR(jobid_name);
327 static struct attribute *lustre_attrs[] = {
328 &lustre_attr_version.attr,
329 &lustre_attr_pinger.attr,
330 &lustre_attr_health.attr,
331 &lustre_attr_jobid_name.attr,
332 &lustre_attr_jobid_var.attr,
336 static void *obd_device_list_seq_start(struct seq_file *p, loff_t *pos)
338 if (*pos >= class_devno_max())
344 static void obd_device_list_seq_stop(struct seq_file *p, void *v)
348 static void *obd_device_list_seq_next(struct seq_file *p, void *v, loff_t *pos)
351 if (*pos >= class_devno_max())
357 static int obd_device_list_seq_show(struct seq_file *p, void *v)
359 loff_t index = *(loff_t *)v;
360 struct obd_device *obd = class_num2obd((int)index);
366 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
367 if (obd->obd_stopping)
369 else if (obd->obd_inactive)
371 else if (obd->obd_set_up)
373 else if (obd->obd_attached)
378 seq_printf(p, "%3d %s %s %s %s %d\n",
379 (int)index, status, obd->obd_type->typ_name,
380 obd->obd_name, obd->obd_uuid.uuid,
381 atomic_read(&obd->obd_refcount));
385 static const struct seq_operations obd_device_list_sops = {
386 .start = obd_device_list_seq_start,
387 .stop = obd_device_list_seq_stop,
388 .next = obd_device_list_seq_next,
389 .show = obd_device_list_seq_show,
392 static int obd_device_list_open(struct inode *inode, struct file *file)
394 struct seq_file *seq;
395 int rc = seq_open(file, &obd_device_list_sops);
400 seq = file->private_data;
401 seq->private = inode->i_private;
406 static const struct file_operations obd_device_list_fops = {
407 .owner = THIS_MODULE,
408 .open = obd_device_list_open,
411 .release = seq_release,
414 struct kobject *lustre_kobj;
415 EXPORT_SYMBOL_GPL(lustre_kobj);
417 static struct attribute_group lustre_attr_group = {
418 .attrs = lustre_attrs,
421 int class_procfs_init(void)
426 lustre_kobj = kobject_create_and_add("lustre", fs_kobj);
427 if (lustre_kobj == NULL)
430 /* Create the files associated with this kobject */
431 rc = sysfs_create_group(lustre_kobj, &lustre_attr_group);
433 kobject_put(lustre_kobj);
437 debugfs_lustre_root = debugfs_create_dir("lustre", NULL);
438 if (IS_ERR_OR_NULL(debugfs_lustre_root)) {
439 rc = debugfs_lustre_root ? PTR_ERR(debugfs_lustre_root)
441 debugfs_lustre_root = NULL;
442 kobject_put(lustre_kobj);
446 file = debugfs_create_file("devices", 0444, debugfs_lustre_root, NULL,
447 &obd_device_list_fops);
448 if (IS_ERR_OR_NULL(file)) {
449 rc = file ? PTR_ERR(file) : -ENOMEM;
450 kobject_put(lustre_kobj);
457 int class_procfs_clean(void)
459 if (debugfs_lustre_root != NULL)
460 debugfs_remove_recursive(debugfs_lustre_root);
462 debugfs_lustre_root = NULL;
464 kobject_put(lustre_kobj);