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, 2014, 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>
56 #include <linux/proc_fs.h>
58 #include <linux/poll.h>
59 #include <linux/init.h>
60 #include <linux/list.h>
61 #include <linux/highmem.h>
63 #include <asm/ioctls.h>
65 #include <asm/uaccess.h>
66 #include <linux/miscdevice.h>
67 #include <linux/seq_file.h>
69 #include <libcfs/libcfs.h>
70 #include <obd_support.h>
71 #include <obd_class.h>
72 #include <lnet/lnetctl.h>
73 #include <lprocfs_status.h>
74 #include <lustre_ioctl.h>
75 #include <lustre_ver.h>
79 /* buffer MUST be at least the size of obd_ioctl_hdr */
80 int obd_ioctl_getdata(char **buf, int *len, void __user *arg)
82 struct obd_ioctl_hdr hdr;
83 struct obd_ioctl_data *data;
87 if (copy_from_user(&hdr, arg, sizeof(hdr)))
90 if (hdr.ioc_version != OBD_IOCTL_VERSION) {
91 CERROR("Version mismatch kernel (%x) vs application (%x)\n",
92 OBD_IOCTL_VERSION, hdr.ioc_version);
96 if (hdr.ioc_len > OBD_MAX_IOCTL_BUFFER) {
97 CERROR("User buffer len %d exceeds %d max buffer\n",
98 hdr.ioc_len, OBD_MAX_IOCTL_BUFFER);
102 if (hdr.ioc_len < sizeof(struct obd_ioctl_data)) {
103 CERROR("User buffer too small for ioctl (%d)\n", hdr.ioc_len);
107 /* When there are lots of processes calling vmalloc on multi-core
108 * system, the high lock contention will hurt performance badly,
109 * obdfilter-survey is an example, which relies on ioctl. So we'd
110 * better avoid vmalloc on ioctl path. LU-66 */
111 OBD_ALLOC_LARGE(*buf, hdr.ioc_len);
113 CERROR("Cannot allocate control buffer of len %d\n",
118 data = (struct obd_ioctl_data *)*buf;
120 if (copy_from_user(*buf, arg, hdr.ioc_len)) {
121 OBD_FREE_LARGE(*buf, hdr.ioc_len);
125 if (obd_ioctl_is_invalid(data)) {
126 CERROR("ioctl not correctly formatted\n");
127 OBD_FREE_LARGE(*buf, hdr.ioc_len);
131 if (data->ioc_inllen1) {
132 data->ioc_inlbuf1 = &data->ioc_bulk[0];
133 offset += cfs_size_round(data->ioc_inllen1);
136 if (data->ioc_inllen2) {
137 data->ioc_inlbuf2 = &data->ioc_bulk[0] + offset;
138 offset += cfs_size_round(data->ioc_inllen2);
141 if (data->ioc_inllen3) {
142 data->ioc_inlbuf3 = &data->ioc_bulk[0] + offset;
143 offset += cfs_size_round(data->ioc_inllen3);
146 if (data->ioc_inllen4)
147 data->ioc_inlbuf4 = &data->ioc_bulk[0] + offset;
151 EXPORT_SYMBOL(obd_ioctl_getdata);
153 int obd_ioctl_popdata(void __user *arg, void *data, int len)
158 err = copy_to_user(arg, data, len) ? -EFAULT : 0;
162 /* opening /dev/obd */
163 static int obd_class_open(struct inode * inode, struct file * file)
167 try_module_get(THIS_MODULE);
171 /* closing /dev/obd */
172 static int obd_class_release(struct inode * inode, struct file * file)
176 module_put(THIS_MODULE);
180 /* to control /dev/obd */
181 static long obd_class_ioctl(struct file *filp, unsigned int cmd,
187 /* Allow non-root access for OBD_IOC_PING_TARGET - used by lfs check */
188 if (!cfs_capable(CFS_CAP_SYS_ADMIN) && (cmd != OBD_IOC_PING_TARGET))
189 RETURN(err = -EACCES);
190 if ((cmd & 0xffffff00) == ((int)'T') << 8) /* ignore all tty ioctls */
191 RETURN(err = -ENOTTY);
193 err = class_handle_ioctl(cmd, (unsigned long)arg);
198 /* declare character device */
199 static struct file_operations obd_psdev_fops = {
200 .owner = THIS_MODULE,
201 .unlocked_ioctl = obd_class_ioctl, /* unlocked_ioctl */
202 .open = obd_class_open, /* open */
203 .release = obd_class_release, /* release */
207 struct miscdevice obd_psdev = {
208 .minor = OBD_DEV_MINOR,
209 .name = OBD_DEV_NAME,
210 .fops = &obd_psdev_fops,
214 #ifdef CONFIG_PROC_FS
215 static int obd_proc_version_seq_show(struct seq_file *m, void *v)
217 seq_printf(m, "lustre: %s\nkernel: %s\nbuild: %s\n",
218 LUSTRE_VERSION_STRING, "patchless_client",
219 LUSTRE_VERSION_STRING);
222 LPROC_SEQ_FOPS_RO(obd_proc_version);
224 static int obd_proc_pinger_seq_show(struct seq_file *m, void *v)
226 seq_printf(m, "%s\n",
235 LPROC_SEQ_FOPS_RO(obd_proc_pinger);
238 * Check all obd devices health
241 * \param data [in] unused
243 * \retval number of characters printed if healthy
245 static int obd_proc_health_seq_show(struct seq_file *m, void *data)
250 if (libcfs_catastrophe)
251 seq_printf(m, "LBUG\n");
253 read_lock(&obd_dev_lock);
254 for (i = 0; i < class_devno_max(); i++) {
255 struct obd_device *obd;
257 obd = class_num2obd(i);
258 if (obd == NULL || !obd->obd_attached || !obd->obd_set_up)
261 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
262 if (obd->obd_stopping)
265 class_incref(obd, __FUNCTION__, current);
266 read_unlock(&obd_dev_lock);
268 if (obd_health_check(NULL, obd)) {
269 seq_printf(m, "device %s reported unhealthy\n",
273 class_decref(obd, __FUNCTION__, current);
274 read_lock(&obd_dev_lock);
276 read_unlock(&obd_dev_lock);
279 seq_puts(m, "healthy\n");
281 seq_puts(m, "NOT HEALTHY\n");
284 LPROC_SEQ_FOPS_RO(obd_proc_health);
286 static int obd_proc_jobid_var_seq_show(struct seq_file *m, void *v)
288 if (strlen(obd_jobid_var) != 0)
289 seq_printf(m, "%s\n", obd_jobid_var);
294 obd_proc_jobid_var_seq_write(struct file *file, const char __user *buffer,
295 size_t count, loff_t *off)
297 if (!count || count > JOBSTATS_JOBID_VAR_MAX_LEN)
300 memset(obd_jobid_var, 0, JOBSTATS_JOBID_VAR_MAX_LEN + 1);
302 /* This might leave the var invalid on error, which is probably fine.*/
303 if (copy_from_user(obd_jobid_var, buffer, count))
306 /* Trim the trailing '\n' if any */
307 if (obd_jobid_var[count - 1] == '\n')
308 obd_jobid_var[count - 1] = 0;
312 LPROC_SEQ_FOPS(obd_proc_jobid_var);
314 static int obd_proc_jobid_name_seq_show(struct seq_file *m, void *v)
316 if (strlen(obd_jobid_node) != 0)
317 seq_printf(m, "%s\n", obd_jobid_node);
321 static ssize_t obd_proc_jobid_name_seq_write(struct file *file,
322 const char __user *buffer,
323 size_t count, loff_t *off)
325 if (count == 0 || count > LUSTRE_JOBID_SIZE)
328 /* clear previous value */
329 memset(obd_jobid_node, 0, LUSTRE_JOBID_SIZE);
331 if (copy_from_user(obd_jobid_node, buffer, count))
334 /* Trim the trailing '\n' if any */
335 if (obd_jobid_node[count - 1] == '\n') {
336 /* Don't echo just a newline */
339 obd_jobid_node[count - 1] = 0;
344 LPROC_SEQ_FOPS(obd_proc_jobid_name);
346 /* Root for /proc/fs/lustre */
347 struct proc_dir_entry *proc_lustre_root = NULL;
348 EXPORT_SYMBOL(proc_lustre_root);
350 static struct lprocfs_vars lprocfs_base[] = {
352 .fops = &obd_proc_version_fops },
354 .fops = &obd_proc_pinger_fops },
355 { .name = "health_check",
356 .fops = &obd_proc_health_fops },
357 { .name = "jobid_var",
358 .fops = &obd_proc_jobid_var_fops},
359 { .name = "jobid_name",
360 .fops = &obd_proc_jobid_name_fops},
364 #define lprocfs_base NULL
365 #endif /* CONFIG_PROC_FS */
367 static void *obd_device_list_seq_start(struct seq_file *p, loff_t *pos)
369 if (*pos >= class_devno_max())
375 static void obd_device_list_seq_stop(struct seq_file *p, void *v)
379 static void *obd_device_list_seq_next(struct seq_file *p, void *v, loff_t *pos)
382 if (*pos >= class_devno_max())
388 static int obd_device_list_seq_show(struct seq_file *p, void *v)
390 loff_t index = *(loff_t *)v;
391 struct obd_device *obd = class_num2obd((int)index);
397 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
398 if (obd->obd_stopping)
400 else if (obd->obd_inactive)
402 else if (obd->obd_set_up)
404 else if (obd->obd_attached)
409 seq_printf(p, "%3d %s %s %s %s %d\n",
410 (int)index, status, obd->obd_type->typ_name,
411 obd->obd_name, obd->obd_uuid.uuid,
412 atomic_read(&obd->obd_refcount));
416 static const struct seq_operations obd_device_list_sops = {
417 .start = obd_device_list_seq_start,
418 .stop = obd_device_list_seq_stop,
419 .next = obd_device_list_seq_next,
420 .show = obd_device_list_seq_show,
423 static int obd_device_list_open(struct inode *inode, struct file *file)
425 struct seq_file *seq;
426 int rc = seq_open(file, &obd_device_list_sops);
431 seq = file->private_data;
432 seq->private = PDE_DATA(inode);
436 static const struct file_operations obd_device_list_fops = {
437 .owner = THIS_MODULE,
438 .open = obd_device_list_open,
441 .release = seq_release,
444 int class_procfs_init(void)
446 struct proc_dir_entry *entry;
452 entry = lprocfs_register("fs/lustre", NULL, lprocfs_base, NULL);
455 CERROR("cannot create '/proc/fs/lustre': rc = %d\n", rc);
459 proc_lustre_root = entry;
461 rc = lprocfs_seq_create(proc_lustre_root, "devices", 0444,
462 &obd_device_list_fops, NULL);
464 CERROR("cannot create '/proc/fs/lustre/devices': rc = %d\n",
472 lprocfs_remove(&proc_lustre_root);
477 int class_procfs_clean(void)
480 if (proc_lustre_root) {
481 lprocfs_remove(&proc_lustre_root);