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>
56 #include <linux/proc_fs.h>
58 #include <linux/poll.h>
59 #include <linux/list.h>
60 #include <linux/highmem.h>
62 #include <asm/ioctls.h>
63 #include <linux/poll.h>
64 #include <linux/uaccess.h>
65 #include <linux/miscdevice.h>
66 #include <linux/seq_file.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"
78 /* buffer MUST be at least the size of obd_ioctl_hdr */
79 int obd_ioctl_getdata(char **buf, int *len, void *arg)
81 struct obd_ioctl_hdr hdr;
82 struct obd_ioctl_data *data;
86 if (copy_from_user(&hdr, (void *)arg, sizeof(hdr)))
89 if (hdr.ioc_version != OBD_IOCTL_VERSION) {
90 CERROR("Version mismatch kernel (%x) vs application (%x)\n",
91 OBD_IOCTL_VERSION, hdr.ioc_version);
95 if (hdr.ioc_len > OBD_MAX_IOCTL_BUFFER) {
96 CERROR("User buffer len %d exceeds %d max buffer\n",
97 hdr.ioc_len, OBD_MAX_IOCTL_BUFFER);
101 if (hdr.ioc_len < sizeof(struct obd_ioctl_data)) {
102 CERROR("User buffer too small for ioctl (%d)\n", hdr.ioc_len);
106 /* When there are lots of processes calling vmalloc on multi-core
107 * system, the high lock contention will hurt performance badly,
108 * obdfilter-survey is an example, which relies on ioctl. So we'd
109 * better avoid vmalloc on ioctl path. LU-66 */
110 OBD_ALLOC_LARGE(*buf, hdr.ioc_len);
112 CERROR("Cannot allocate control buffer of len %d\n",
117 data = (struct obd_ioctl_data *)*buf;
119 if (copy_from_user(*buf, (void *)arg, hdr.ioc_len)) {
123 if (hdr.ioc_len != data->ioc_len) {
128 if (obd_ioctl_is_invalid(data)) {
129 CERROR("ioctl not correctly formatted\n");
134 if (data->ioc_inllen1) {
135 data->ioc_inlbuf1 = &data->ioc_bulk[0];
136 offset += cfs_size_round(data->ioc_inllen1);
139 if (data->ioc_inllen2) {
140 data->ioc_inlbuf2 = &data->ioc_bulk[0] + offset;
141 offset += cfs_size_round(data->ioc_inllen2);
144 if (data->ioc_inllen3) {
145 data->ioc_inlbuf3 = &data->ioc_bulk[0] + offset;
146 offset += cfs_size_round(data->ioc_inllen3);
149 if (data->ioc_inllen4) {
150 data->ioc_inlbuf4 = &data->ioc_bulk[0] + offset;
156 OBD_FREE_LARGE(*buf, hdr.ioc_len);
159 EXPORT_SYMBOL(obd_ioctl_getdata);
161 int obd_ioctl_popdata(void *arg, void *data, int len)
165 err = copy_to_user(arg, data, len);
170 EXPORT_SYMBOL(obd_ioctl_popdata);
172 /* opening /dev/obd */
173 static int obd_class_open(struct inode *inode, struct file *file)
175 try_module_get(THIS_MODULE);
179 /* closing /dev/obd */
180 static int obd_class_release(struct inode *inode, struct file *file)
182 module_put(THIS_MODULE);
186 /* to control /dev/obd */
187 static long obd_class_ioctl(struct file *filp, unsigned int cmd,
192 /* Allow non-root access for OBD_IOC_PING_TARGET - used by lfs check */
193 if (!capable(CFS_CAP_SYS_ADMIN) && (cmd != OBD_IOC_PING_TARGET))
194 return err = -EACCES;
195 if ((cmd & 0xffffff00) == ((int)'T') << 8) /* ignore all tty ioctls */
196 return err = -ENOTTY;
198 err = class_handle_ioctl(cmd, (unsigned long)arg);
203 /* declare character device */
204 static struct file_operations obd_psdev_fops = {
205 .owner = THIS_MODULE,
206 .unlocked_ioctl = obd_class_ioctl, /* unlocked_ioctl */
207 .open = obd_class_open, /* open */
208 .release = obd_class_release, /* release */
212 struct miscdevice obd_psdev = {
213 .minor = OBD_DEV_MINOR,
214 .name = OBD_DEV_NAME,
215 .fops = &obd_psdev_fops,
219 #if defined (CONFIG_PROC_FS)
220 static int obd_proc_version_seq_show(struct seq_file *m, void *v)
222 seq_printf(m, "lustre: %s\nkernel: %s\nbuild: %s\n",
223 LUSTRE_VERSION_STRING, "patchless_client", BUILD_VERSION);
226 LPROC_SEQ_FOPS_RO(obd_proc_version);
228 int obd_proc_pinger_seq_show(struct seq_file *m, void *v)
230 seq_printf(m, "%s\n", "on");
233 LPROC_SEQ_FOPS_RO(obd_proc_pinger);
235 static int obd_proc_health_seq_show(struct seq_file *m, void *v)
240 if (libcfs_catastrophe)
241 seq_printf(m, "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)) {
259 seq_printf(m, "device %s reported unhealthy\n",
263 class_decref(obd, __func__, current);
264 read_lock(&obd_dev_lock);
266 read_unlock(&obd_dev_lock);
269 seq_puts(m, "healthy\n");
271 seq_puts(m, "NOT HEALTHY\n");
275 LPROC_SEQ_FOPS_RO(obd_proc_health);
277 static int obd_proc_jobid_var_seq_show(struct seq_file *m, void *v)
279 seq_printf(m, "%s\n", obd_jobid_var);
283 static ssize_t obd_proc_jobid_var_seq_write(struct file *file,
284 const char __user *buffer,
285 size_t count, loff_t *off)
287 if (!count || count > JOBSTATS_JOBID_VAR_MAX_LEN)
290 memset(obd_jobid_var, 0, JOBSTATS_JOBID_VAR_MAX_LEN + 1);
292 /* This might leave the var invalid on error, which is probably fine.*/
293 if (copy_from_user(obd_jobid_var, buffer, count))
296 /* Trim the trailing '\n' if any */
297 if (obd_jobid_var[count - 1] == '\n')
298 obd_jobid_var[count - 1] = 0;
302 LPROC_SEQ_FOPS(obd_proc_jobid_var);
304 static int obd_proc_jobid_name_seq_show(struct seq_file *m, void *v)
306 seq_printf(m, "%s\n", obd_jobid_var);
310 static ssize_t obd_proc_jobid_name_seq_write(struct file *file,
311 const char __user *buffer,
312 size_t count, loff_t *off)
314 if (!count || count > JOBSTATS_JOBID_SIZE)
317 if (copy_from_user(obd_jobid_node, buffer, count))
320 obd_jobid_node[count] = 0;
322 /* Trim the trailing '\n' if any */
323 if (obd_jobid_node[count - 1] == '\n')
324 obd_jobid_node[count - 1] = 0;
328 LPROC_SEQ_FOPS(obd_proc_jobid_name);
330 /* Root for /proc/fs/lustre */
331 struct proc_dir_entry *proc_lustre_root = NULL;
332 EXPORT_SYMBOL(proc_lustre_root);
334 struct lprocfs_vars lprocfs_base[] = {
335 { "version", &obd_proc_version_fops },
336 { "pinger", &obd_proc_pinger_fops },
337 { "health_check", &obd_proc_health_fops },
338 { "jobid_var", &obd_proc_jobid_var_fops },
339 { .name = "jobid_name",
340 .fops = &obd_proc_jobid_name_fops},
344 static void *obd_device_list_seq_start(struct seq_file *p, loff_t *pos)
346 if (*pos >= class_devno_max())
352 static void obd_device_list_seq_stop(struct seq_file *p, void *v)
356 static void *obd_device_list_seq_next(struct seq_file *p, void *v, loff_t *pos)
359 if (*pos >= class_devno_max())
365 static int obd_device_list_seq_show(struct seq_file *p, void *v)
367 loff_t index = *(loff_t *)v;
368 struct obd_device *obd = class_num2obd((int)index);
374 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
375 if (obd->obd_stopping)
377 else if (obd->obd_inactive)
379 else if (obd->obd_set_up)
381 else if (obd->obd_attached)
386 seq_printf(p, "%3d %s %s %s %s %d\n",
387 (int)index, status, obd->obd_type->typ_name,
388 obd->obd_name, obd->obd_uuid.uuid,
389 atomic_read(&obd->obd_refcount));
393 struct seq_operations obd_device_list_sops = {
394 .start = obd_device_list_seq_start,
395 .stop = obd_device_list_seq_stop,
396 .next = obd_device_list_seq_next,
397 .show = obd_device_list_seq_show,
400 static int obd_device_list_open(struct inode *inode, struct file *file)
402 struct seq_file *seq;
403 int rc = seq_open(file, &obd_device_list_sops);
408 seq = file->private_data;
409 seq->private = PDE_DATA(inode);
414 struct file_operations obd_device_list_fops = {
415 .owner = THIS_MODULE,
416 .open = obd_device_list_open,
419 .release = seq_release,
422 int class_procfs_init(void)
426 proc_lustre_root = lprocfs_register("fs/lustre", NULL,
428 if (IS_ERR(proc_lustre_root)) {
429 rc = PTR_ERR(proc_lustre_root);
430 proc_lustre_root = NULL;
434 rc = lprocfs_seq_create(proc_lustre_root, "devices", 0444,
435 &obd_device_list_fops, NULL);
438 CERROR("error adding /proc/fs/lustre/devices file\n");
442 int class_procfs_clean(void)
444 if (proc_lustre_root) {
445 lprocfs_remove(&proc_lustre_root);
449 #endif /* CONFIG_PROC_FS */