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, 2014, 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/linux/linux-module.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/major.h>
45 #include <linux/sched.h>
47 #include <linux/slab.h>
48 #include <linux/ioport.h>
49 #include <linux/fcntl.h>
50 #include <linux/delay.h>
51 #include <linux/skbuff.h>
52 #include <linux/proc_fs.h>
54 #include <linux/poll.h>
55 #include <linux/init.h>
56 #include <linux/list.h>
57 #include <linux/highmem.h>
59 #include <asm/ioctls.h>
61 #include <asm/uaccess.h>
62 #include <linux/miscdevice.h>
63 #include <linux/seq_file.h>
65 #include <libcfs/libcfs.h>
66 #include <obd_support.h>
67 #include <obd_class.h>
68 #include <lnet/lnetctl.h>
69 #include <lprocfs_status.h>
70 #include <lustre_ioctl.h>
71 #include <lustre_ver.h>
75 /* buffer MUST be at least the size of obd_ioctl_hdr */
76 int obd_ioctl_getdata(char **buf, int *len, void __user *arg)
78 struct obd_ioctl_hdr hdr;
79 struct obd_ioctl_data *data;
83 if (copy_from_user(&hdr, arg, sizeof(hdr)))
86 if (hdr.ioc_version != OBD_IOCTL_VERSION) {
87 CERROR("Version mismatch kernel (%x) vs application (%x)\n",
88 OBD_IOCTL_VERSION, hdr.ioc_version);
92 if (hdr.ioc_len > OBD_MAX_IOCTL_BUFFER) {
93 CERROR("User buffer len %d exceeds %d max buffer\n",
94 hdr.ioc_len, OBD_MAX_IOCTL_BUFFER);
98 if (hdr.ioc_len < sizeof(struct obd_ioctl_data)) {
99 CERROR("User buffer too small for ioctl (%d)\n", hdr.ioc_len);
103 /* When there are lots of processes calling vmalloc on multi-core
104 * system, the high lock contention will hurt performance badly,
105 * obdfilter-survey is an example, which relies on ioctl. So we'd
106 * better avoid vmalloc on ioctl path. LU-66 */
107 OBD_ALLOC_LARGE(*buf, hdr.ioc_len);
109 CERROR("Cannot allocate control buffer of len %d\n",
114 data = (struct obd_ioctl_data *)*buf;
116 if (copy_from_user(*buf, arg, hdr.ioc_len)) {
117 OBD_FREE_LARGE(*buf, hdr.ioc_len);
121 if (obd_ioctl_is_invalid(data)) {
122 CERROR("ioctl not correctly formatted\n");
123 OBD_FREE_LARGE(*buf, hdr.ioc_len);
127 if (data->ioc_inllen1) {
128 data->ioc_inlbuf1 = &data->ioc_bulk[0];
129 offset += cfs_size_round(data->ioc_inllen1);
132 if (data->ioc_inllen2) {
133 data->ioc_inlbuf2 = &data->ioc_bulk[0] + offset;
134 offset += cfs_size_round(data->ioc_inllen2);
137 if (data->ioc_inllen3) {
138 data->ioc_inlbuf3 = &data->ioc_bulk[0] + offset;
139 offset += cfs_size_round(data->ioc_inllen3);
142 if (data->ioc_inllen4)
143 data->ioc_inlbuf4 = &data->ioc_bulk[0] + offset;
147 EXPORT_SYMBOL(obd_ioctl_getdata);
149 int obd_ioctl_popdata(void __user *arg, void *data, int len)
154 err = copy_to_user(arg, data, len) ? -EFAULT : 0;
158 /* opening /dev/obd */
159 static int obd_class_open(struct inode * inode, struct file * file)
163 try_module_get(THIS_MODULE);
167 /* closing /dev/obd */
168 static int obd_class_release(struct inode * inode, struct file * file)
172 module_put(THIS_MODULE);
176 /* to control /dev/obd */
177 static long obd_class_ioctl(struct file *filp, unsigned int cmd,
183 /* Allow non-root access for OBD_IOC_PING_TARGET - used by lfs check */
184 if (!cfs_capable(CFS_CAP_SYS_ADMIN) && (cmd != OBD_IOC_PING_TARGET))
185 RETURN(err = -EACCES);
186 if ((cmd & 0xffffff00) == ((int)'T') << 8) /* ignore all tty ioctls */
187 RETURN(err = -ENOTTY);
189 err = class_handle_ioctl(cmd, (unsigned long)arg);
194 /* declare character device */
195 static struct file_operations obd_psdev_fops = {
196 .owner = THIS_MODULE,
197 .unlocked_ioctl = obd_class_ioctl, /* unlocked_ioctl */
198 .open = obd_class_open, /* open */
199 .release = obd_class_release, /* release */
203 struct miscdevice obd_psdev = {
204 .minor = OBD_DEV_MINOR,
205 .name = OBD_DEV_NAME,
206 .fops = &obd_psdev_fops,
210 #ifdef CONFIG_PROC_FS
211 static int obd_proc_version_seq_show(struct seq_file *m, void *v)
213 seq_printf(m, "lustre: %s\n", LUSTRE_VERSION_STRING);
216 LPROC_SEQ_FOPS_RO(obd_proc_version);
218 static int obd_proc_pinger_seq_show(struct seq_file *m, void *v)
220 seq_printf(m, "%s\n",
229 LPROC_SEQ_FOPS_RO(obd_proc_pinger);
232 * Check all obd devices health
235 * \param data [in] unused
237 * \retval number of characters printed if healthy
239 static int obd_proc_health_seq_show(struct seq_file *m, void *data)
244 if (libcfs_catastrophe) {
245 seq_printf(m, "LBUG\n");
249 read_lock(&obd_dev_lock);
250 for (i = 0; i < class_devno_max(); i++) {
251 struct obd_device *obd;
253 obd = class_num2obd(i);
254 if (obd == NULL || !obd->obd_attached || !obd->obd_set_up)
257 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
258 if (obd->obd_stopping)
261 class_incref(obd, __FUNCTION__, current);
262 read_unlock(&obd_dev_lock);
264 if (obd_health_check(NULL, obd)) {
265 seq_printf(m, "device %s reported unhealthy\n",
269 class_decref(obd, __FUNCTION__, current);
270 read_lock(&obd_dev_lock);
272 read_unlock(&obd_dev_lock);
275 seq_puts(m, "healthy\n");
277 seq_puts(m, "NOT HEALTHY\n");
280 LPROC_SEQ_FOPS_RO(obd_proc_health);
282 static int obd_proc_jobid_var_seq_show(struct seq_file *m, void *v)
284 if (strlen(obd_jobid_var) != 0)
285 seq_printf(m, "%s\n", obd_jobid_var);
290 obd_proc_jobid_var_seq_write(struct file *file, const char __user *buffer,
291 size_t count, loff_t *off)
293 if (!count || count > JOBSTATS_JOBID_VAR_MAX_LEN)
296 memset(obd_jobid_var, 0, JOBSTATS_JOBID_VAR_MAX_LEN + 1);
298 /* This might leave the var invalid on error, which is probably fine.*/
299 if (copy_from_user(obd_jobid_var, buffer, count))
302 /* Trim the trailing '\n' if any */
303 if (obd_jobid_var[count - 1] == '\n')
304 obd_jobid_var[count - 1] = 0;
308 LPROC_SEQ_FOPS(obd_proc_jobid_var);
310 static int obd_proc_jobid_name_seq_show(struct seq_file *m, void *v)
312 if (strlen(obd_jobid_node) != 0)
313 seq_printf(m, "%s\n", obd_jobid_node);
317 static ssize_t obd_proc_jobid_name_seq_write(struct file *file,
318 const char __user *buffer,
319 size_t count, loff_t *off)
321 if (count == 0 || count > LUSTRE_JOBID_SIZE)
324 /* clear previous value */
325 memset(obd_jobid_node, 0, LUSTRE_JOBID_SIZE);
327 if (copy_from_user(obd_jobid_node, buffer, count))
330 /* Trim the trailing '\n' if any */
331 if (obd_jobid_node[count - 1] == '\n') {
332 /* Don't echo just a newline */
335 obd_jobid_node[count - 1] = 0;
340 LPROC_SEQ_FOPS(obd_proc_jobid_name);
342 /* Root for /proc/fs/lustre */
343 struct proc_dir_entry *proc_lustre_root = NULL;
344 EXPORT_SYMBOL(proc_lustre_root);
346 static struct lprocfs_vars lprocfs_base[] = {
348 .fops = &obd_proc_version_fops },
350 .fops = &obd_proc_pinger_fops },
351 { .name = "health_check",
352 .fops = &obd_proc_health_fops },
353 { .name = "jobid_var",
354 .fops = &obd_proc_jobid_var_fops},
355 { .name = "jobid_name",
356 .fops = &obd_proc_jobid_name_fops},
360 #define lprocfs_base NULL
361 #endif /* CONFIG_PROC_FS */
363 static void *obd_device_list_seq_start(struct seq_file *p, loff_t *pos)
365 if (*pos >= class_devno_max())
371 static void obd_device_list_seq_stop(struct seq_file *p, void *v)
375 static void *obd_device_list_seq_next(struct seq_file *p, void *v, loff_t *pos)
378 if (*pos >= class_devno_max())
384 static int obd_device_list_seq_show(struct seq_file *p, void *v)
386 loff_t index = *(loff_t *)v;
387 struct obd_device *obd = class_num2obd((int)index);
393 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
394 if (obd->obd_stopping)
396 else if (obd->obd_inactive)
398 else if (obd->obd_set_up)
400 else if (obd->obd_attached)
405 seq_printf(p, "%3d %s %s %s %s %d\n",
406 (int)index, status, obd->obd_type->typ_name,
407 obd->obd_name, obd->obd_uuid.uuid,
408 atomic_read(&obd->obd_refcount));
412 static const struct seq_operations obd_device_list_sops = {
413 .start = obd_device_list_seq_start,
414 .stop = obd_device_list_seq_stop,
415 .next = obd_device_list_seq_next,
416 .show = obd_device_list_seq_show,
419 static int obd_device_list_open(struct inode *inode, struct file *file)
421 struct seq_file *seq;
422 int rc = seq_open(file, &obd_device_list_sops);
427 seq = file->private_data;
428 seq->private = PDE_DATA(inode);
432 static const struct file_operations obd_device_list_fops = {
433 .owner = THIS_MODULE,
434 .open = obd_device_list_open,
437 .release = seq_release,
440 int class_procfs_init(void)
442 struct proc_dir_entry *entry;
448 entry = lprocfs_register("fs/lustre", NULL, lprocfs_base, NULL);
451 CERROR("cannot create '/proc/fs/lustre': rc = %d\n", rc);
455 proc_lustre_root = entry;
457 rc = lprocfs_seq_create(proc_lustre_root, "devices", 0444,
458 &obd_device_list_fops, NULL);
460 CERROR("cannot create '/proc/fs/lustre/devices': rc = %d\n",
468 lprocfs_remove(&proc_lustre_root);
473 int class_procfs_clean(void)
476 if (proc_lustre_root) {
477 lprocfs_remove(&proc_lustre_root);