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
46 #include <linux/module.h>
47 #include <linux/errno.h>
48 #include <linux/kernel.h>
49 #include <linux/major.h>
50 #include <linux/sched.h>
52 #include <linux/slab.h>
53 #include <linux/ioport.h>
54 #include <linux/fcntl.h>
55 #include <linux/delay.h>
56 #include <linux/skbuff.h>
57 #include <linux/proc_fs.h>
59 #include <linux/poll.h>
60 #include <linux/init.h>
61 #include <linux/list.h>
62 #include <linux/highmem.h>
64 #include <asm/ioctls.h>
66 #include <asm/uaccess.h>
67 #include <linux/miscdevice.h>
68 #include <linux/seq_file.h>
70 # include <liblustre.h>
73 #include <libcfs/libcfs.h>
74 #include <obd_support.h>
75 #include <obd_class.h>
76 #include <lnet/lnetctl.h>
77 #include <lprocfs_status.h>
78 #include <lustre_ver.h>
79 #include <lustre/lustre_build_version.h>
84 /* buffer MUST be at least the size of obd_ioctl_hdr */
85 int obd_ioctl_getdata(char **buf, int *len, void *arg)
87 struct obd_ioctl_hdr hdr;
88 struct obd_ioctl_data *data;
93 err = copy_from_user(&hdr, (void *)arg, sizeof(hdr));
97 if (hdr.ioc_version != OBD_IOCTL_VERSION) {
98 CERROR("Version mismatch kernel (%x) vs application (%x)\n",
99 OBD_IOCTL_VERSION, hdr.ioc_version);
103 if (hdr.ioc_len > OBD_MAX_IOCTL_BUFFER) {
104 CERROR("User buffer len %d exceeds %d max buffer\n",
105 hdr.ioc_len, OBD_MAX_IOCTL_BUFFER);
109 if (hdr.ioc_len < sizeof(struct obd_ioctl_data)) {
110 CERROR("User buffer too small for ioctl (%d)\n", hdr.ioc_len);
114 /* When there are lots of processes calling vmalloc on multi-core
115 * system, the high lock contention will hurt performance badly,
116 * obdfilter-survey is an example, which relies on ioctl. So we'd
117 * better avoid vmalloc on ioctl path. LU-66 */
118 OBD_ALLOC_LARGE(*buf, hdr.ioc_len);
120 CERROR("Cannot allocate control buffer of len %d\n",
125 data = (struct obd_ioctl_data *)*buf;
127 err = copy_from_user(*buf, (void *)arg, hdr.ioc_len);
129 OBD_FREE_LARGE(*buf, hdr.ioc_len);
133 if (obd_ioctl_is_invalid(data)) {
134 CERROR("ioctl not correctly formatted\n");
135 OBD_FREE_LARGE(*buf, hdr.ioc_len);
139 if (data->ioc_inllen1) {
140 data->ioc_inlbuf1 = &data->ioc_bulk[0];
141 offset += cfs_size_round(data->ioc_inllen1);
144 if (data->ioc_inllen2) {
145 data->ioc_inlbuf2 = &data->ioc_bulk[0] + offset;
146 offset += cfs_size_round(data->ioc_inllen2);
149 if (data->ioc_inllen3) {
150 data->ioc_inlbuf3 = &data->ioc_bulk[0] + offset;
151 offset += cfs_size_round(data->ioc_inllen3);
154 if (data->ioc_inllen4) {
155 data->ioc_inlbuf4 = &data->ioc_bulk[0] + offset;
161 EXPORT_SYMBOL(obd_ioctl_getdata);
163 int obd_ioctl_popdata(void *arg, void *data, int len)
167 err = copy_to_user(arg, data, len);
172 EXPORT_SYMBOL(obd_ioctl_popdata);
174 /* opening /dev/obd */
175 static int obd_class_open(struct inode * inode, struct file * file)
179 try_module_get(THIS_MODULE);
183 /* closing /dev/obd */
184 static int obd_class_release(struct inode * inode, struct file * file)
188 module_put(THIS_MODULE);
192 /* to control /dev/obd */
193 static long obd_class_ioctl(struct file *filp, unsigned int cmd,
199 /* Allow non-root access for OBD_IOC_PING_TARGET - used by lfs check */
200 if (!cfs_capable(CFS_CAP_SYS_ADMIN) && (cmd != OBD_IOC_PING_TARGET))
201 RETURN(err = -EACCES);
202 if ((cmd & 0xffffff00) == ((int)'T') << 8) /* ignore all tty ioctls */
203 RETURN(err = -ENOTTY);
205 err = class_handle_ioctl(cmd, (unsigned long)arg);
210 /* declare character device */
211 static struct file_operations obd_psdev_fops = {
212 .owner = THIS_MODULE,
213 .unlocked_ioctl = obd_class_ioctl, /* unlocked_ioctl */
214 .open = obd_class_open, /* open */
215 .release = obd_class_release, /* release */
219 struct miscdevice obd_psdev = {
220 .minor = OBD_DEV_MINOR,
221 .name = OBD_DEV_NAME,
222 .fops = &obd_psdev_fops,
228 static int obd_proc_version_seq_show(struct seq_file *m, void *v)
230 return seq_printf(m, "lustre: %s\nkernel: %s\nbuild: %s\n",
231 LUSTRE_VERSION_STRING, "patchless_client",
234 LPROC_SEQ_FOPS_RO(obd_proc_version);
236 static int obd_proc_pinger_seq_show(struct seq_file *m, void *v)
238 return seq_printf(m, "%s\n",
246 LPROC_SEQ_FOPS_RO(obd_proc_pinger);
249 * Check all obd devices health
252 * \param data [in] unused
254 * \retval number of characters printed if healthy
256 static int obd_proc_health_seq_show(struct seq_file *m, void *data)
261 if (libcfs_catastrophe)
262 seq_printf(m, "LBUG\n");
264 read_lock(&obd_dev_lock);
265 for (i = 0; i < class_devno_max(); i++) {
266 struct obd_device *obd;
268 obd = class_num2obd(i);
269 if (obd == NULL || !obd->obd_attached || !obd->obd_set_up)
272 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
273 if (obd->obd_stopping)
276 class_incref(obd, __FUNCTION__, current);
277 read_unlock(&obd_dev_lock);
279 if (obd_health_check(NULL, obd)) {
280 seq_printf(m, "device %s reported unhealthy\n",
284 class_decref(obd, __FUNCTION__, current);
285 read_lock(&obd_dev_lock);
287 read_unlock(&obd_dev_lock);
290 return seq_printf(m, "healthy\n");
292 seq_printf(m, "NOT HEALTHY\n");
295 LPROC_SEQ_FOPS_RO(obd_proc_health);
297 static int obd_proc_jobid_var_seq_show(struct seq_file *m, void *v)
299 return seq_printf(m, "%s\n", obd_jobid_var);
303 obd_proc_jobid_var_seq_write(struct file *file, const char __user *buffer,
304 size_t count, loff_t *off)
306 if (!count || count > JOBSTATS_JOBID_VAR_MAX_LEN)
309 memset(obd_jobid_var, 0, JOBSTATS_JOBID_VAR_MAX_LEN + 1);
311 /* This might leave the var invalid on error, which is probably fine.*/
312 if (copy_from_user(obd_jobid_var, buffer, count))
315 /* Trim the trailing '\n' if any */
316 if (obd_jobid_var[count - 1] == '\n')
317 obd_jobid_var[count - 1] = 0;
321 LPROC_SEQ_FOPS(obd_proc_jobid_var);
323 /* Root for /proc/fs/lustre */
324 struct proc_dir_entry *proc_lustre_root = NULL;
325 EXPORT_SYMBOL(proc_lustre_root);
327 struct lprocfs_seq_vars lprocfs_base[] = {
329 .fops = &obd_proc_version_fops },
331 .fops = &obd_proc_pinger_fops },
332 { .name = "health_check",
333 .fops = &obd_proc_health_fops },
334 { .name = "jobid_var",
335 .fops = &obd_proc_jobid_var_fops},
339 #define lprocfs_base NULL
343 static void *obd_device_list_seq_start(struct seq_file *p, loff_t *pos)
345 if (*pos >= class_devno_max())
351 static void obd_device_list_seq_stop(struct seq_file *p, void *v)
355 static void *obd_device_list_seq_next(struct seq_file *p, void *v, loff_t *pos)
358 if (*pos >= class_devno_max())
364 static int obd_device_list_seq_show(struct seq_file *p, void *v)
366 loff_t index = *(loff_t *)v;
367 struct obd_device *obd = class_num2obd((int)index);
373 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
374 if (obd->obd_stopping)
376 else if (obd->obd_inactive)
378 else if (obd->obd_set_up)
380 else if (obd->obd_attached)
385 return seq_printf(p, "%3d %s %s %s %s %d\n",
386 (int)index, status, obd->obd_type->typ_name,
387 obd->obd_name, obd->obd_uuid.uuid,
388 atomic_read(&obd->obd_refcount));
391 struct seq_operations obd_device_list_sops = {
392 .start = obd_device_list_seq_start,
393 .stop = obd_device_list_seq_stop,
394 .next = obd_device_list_seq_next,
395 .show = obd_device_list_seq_show,
398 static int obd_device_list_open(struct inode *inode, struct file *file)
400 struct seq_file *seq;
401 int rc = seq_open(file, &obd_device_list_sops);
406 seq = file->private_data;
407 seq->private = PDE_DATA(inode);
411 struct file_operations obd_device_list_fops = {
412 .owner = THIS_MODULE,
413 .open = obd_device_list_open,
416 .release = seq_release,
420 int class_procfs_init(void)
427 proc_lustre_root = lprocfs_seq_register("fs/lustre", NULL,
429 rc = lprocfs_seq_create(proc_lustre_root, "devices", 0444,
430 &obd_device_list_fops, NULL);
432 CERROR("error adding /proc/fs/lustre/devices file\n");
440 int class_procfs_clean(void)
443 if (proc_lustre_root) {
444 lprocfs_remove(&proc_lustre_root);