1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2011, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
38 * lustre/obdclass/linux/linux-module.c
40 * Object Devices Class Driver
41 * These are the only exported functions, they provide some generic
42 * infrastructure for managing object devices
45 #define DEBUG_SUBSYSTEM S_CLASS
47 # define EXPORT_SYMTAB
51 #ifndef AUTOCONF_INCLUDED
52 #include <linux/config.h> /* for CONFIG_PROC_FS */
54 #include <linux/module.h>
55 #include <linux/errno.h>
56 #include <linux/kernel.h>
57 #include <linux/major.h>
58 #include <linux/sched.h>
60 #include <linux/slab.h>
61 #include <linux/ioport.h>
62 #include <linux/fcntl.h>
63 #include <linux/delay.h>
64 #include <linux/skbuff.h>
65 #include <linux/proc_fs.h>
67 #include <linux/poll.h>
68 #include <linux/init.h>
69 #include <linux/list.h>
70 #include <linux/highmem.h>
72 #include <asm/ioctls.h>
73 #include <asm/system.h>
75 #include <asm/uaccess.h>
76 #include <linux/miscdevice.h>
77 #include <linux/smp_lock.h>
78 #include <linux/seq_file.h>
80 # include <liblustre.h>
83 #include <libcfs/libcfs.h>
84 #include <obd_support.h>
85 #include <obd_class.h>
86 #include <lnet/lnetctl.h>
87 #include <lprocfs_status.h>
88 #include <lustre_ver.h>
89 #include <lustre/lustre_build_version.h>
91 #include <linux/lustre_version.h>
95 /* buffer MUST be at least the size of obd_ioctl_hdr */
96 int obd_ioctl_getdata(char **buf, int *len, void *arg)
98 struct obd_ioctl_hdr hdr;
99 struct obd_ioctl_data *data;
104 err = cfs_copy_from_user(&hdr, (void *)arg, sizeof(hdr));
108 if (hdr.ioc_version != OBD_IOCTL_VERSION) {
109 CERROR("Version mismatch kernel (%x) vs application (%x)\n",
110 OBD_IOCTL_VERSION, hdr.ioc_version);
114 if (hdr.ioc_len > OBD_MAX_IOCTL_BUFFER) {
115 CERROR("User buffer len %d exceeds %d max buffer\n",
116 hdr.ioc_len, OBD_MAX_IOCTL_BUFFER);
120 if (hdr.ioc_len < sizeof(struct obd_ioctl_data)) {
121 CERROR("User buffer too small for ioctl (%d)\n", hdr.ioc_len);
125 /* When there are lots of processes calling vmalloc on multi-core
126 * system, the high lock contention will hurt performance badly,
127 * obdfilter-survey is an example, which relies on ioctl. So we'd
128 * better avoid vmalloc on ioctl path. LU-66 */
129 OBD_ALLOC_LARGE(*buf, hdr.ioc_len);
131 CERROR("Cannot allocate control buffer of len %d\n",
136 data = (struct obd_ioctl_data *)*buf;
138 err = cfs_copy_from_user(*buf, (void *)arg, hdr.ioc_len);
140 OBD_FREE_LARGE(*buf, hdr.ioc_len);
144 if (obd_ioctl_is_invalid(data)) {
145 CERROR("ioctl not correctly formatted\n");
146 OBD_FREE_LARGE(*buf, hdr.ioc_len);
150 if (data->ioc_inllen1) {
151 data->ioc_inlbuf1 = &data->ioc_bulk[0];
152 offset += cfs_size_round(data->ioc_inllen1);
155 if (data->ioc_inllen2) {
156 data->ioc_inlbuf2 = &data->ioc_bulk[0] + offset;
157 offset += cfs_size_round(data->ioc_inllen2);
160 if (data->ioc_inllen3) {
161 data->ioc_inlbuf3 = &data->ioc_bulk[0] + offset;
162 offset += cfs_size_round(data->ioc_inllen3);
165 if (data->ioc_inllen4) {
166 data->ioc_inlbuf4 = &data->ioc_bulk[0] + offset;
173 int obd_ioctl_popdata(void *arg, void *data, int len)
177 err = cfs_copy_to_user(arg, data, len);
183 EXPORT_SYMBOL(obd_ioctl_getdata);
184 EXPORT_SYMBOL(obd_ioctl_popdata);
186 /* opening /dev/obd */
187 static int obd_class_open(struct inode * inode, struct file * file)
195 /* closing /dev/obd */
196 static int obd_class_release(struct inode * inode, struct file * file)
204 /* to control /dev/obd */
205 #ifdef HAVE_UNLOCKED_IOCTL
206 static long obd_class_ioctl(struct file *filp, unsigned int cmd,
209 static int obd_class_ioctl(struct inode *inode, struct file *filp,
210 unsigned int cmd, unsigned long arg)
216 /* Allow non-root access for OBD_IOC_PING_TARGET - used by lfs check */
217 if (!cfs_capable(CFS_CAP_SYS_ADMIN) && (cmd != OBD_IOC_PING_TARGET))
218 RETURN(err = -EACCES);
219 if ((cmd & 0xffffff00) == ((int)'T') << 8) /* ignore all tty ioctls */
220 RETURN(err = -ENOTTY);
222 err = class_handle_ioctl(cmd, (unsigned long)arg);
227 /* declare character device */
228 static struct file_operations obd_psdev_fops = {
229 .owner = THIS_MODULE,
230 #if HAVE_UNLOCKED_IOCTL
231 .unlocked_ioctl = obd_class_ioctl, /* unlocked_ioctl */
233 .ioctl = obd_class_ioctl, /* ioctl */
235 .open = obd_class_open, /* open */
236 .release = obd_class_release, /* release */
240 cfs_psdev_t obd_psdev = {
241 .minor = OBD_DEV_MINOR,
242 .name = OBD_DEV_NAME,
243 .fops = &obd_psdev_fops,
249 int obd_proc_read_version(char *page, char **start, off_t off, int count,
250 int *eof, void *data)
253 return snprintf(page, count, "lustre: %s\nkernel: %s\nbuild: %s\n",
254 LUSTRE_VERSION_STRING, "patchless_client",
258 int obd_proc_read_pinger(char *page, char **start, off_t off, int count,
259 int *eof, void *data)
262 return snprintf(page, count, "%s\n",
272 * Check all obd devices health
280 * proc read function parameters, please refer to kernel
281 * code fs/proc/generic.c proc_file_read()
282 * \param data [in] unused
284 * \retval number of characters printed
286 static int obd_proc_read_health(char *page, char **start, off_t off,
287 int count, int *eof, void *data)
292 if (libcfs_catastrophe)
293 rc += snprintf(page + rc, count - rc, "LBUG\n");
295 cfs_read_lock(&obd_dev_lock);
296 for (i = 0; i < class_devno_max(); i++) {
297 struct obd_device *obd;
299 obd = class_num2obd(i);
300 if (obd == NULL || !obd->obd_attached || !obd->obd_set_up)
303 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
304 if (obd->obd_stopping)
307 class_incref(obd, __FUNCTION__, cfs_current());
308 cfs_read_unlock(&obd_dev_lock);
310 if (obd_health_check(obd)) {
311 rc += snprintf(page + rc, count - rc,
312 "device %s reported unhealthy\n",
315 class_decref(obd, __FUNCTION__, cfs_current());
316 cfs_read_lock(&obd_dev_lock);
318 cfs_read_unlock(&obd_dev_lock);
321 return snprintf(page, count, "healthy\n");
323 rc += snprintf(page + rc, count - rc, "NOT HEALTHY\n");
327 /* Root for /proc/fs/lustre */
328 struct proc_dir_entry *proc_lustre_root = NULL;
330 struct lprocfs_vars lprocfs_base[] = {
331 { "version", obd_proc_read_version, NULL, NULL },
332 { "pinger", obd_proc_read_pinger, NULL, NULL },
333 { "health_check", obd_proc_read_health, NULL, NULL },
337 #define lprocfs_base NULL
341 static void *obd_device_list_seq_start(struct seq_file *p, loff_t *pos)
343 if (*pos >= class_devno_max())
349 static void obd_device_list_seq_stop(struct seq_file *p, void *v)
353 static void *obd_device_list_seq_next(struct seq_file *p, void *v, loff_t *pos)
356 if (*pos >= class_devno_max())
362 static int obd_device_list_seq_show(struct seq_file *p, void *v)
364 loff_t index = *(loff_t *)v;
365 struct obd_device *obd = class_num2obd((int)index);
371 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
372 if (obd->obd_stopping)
374 else if (obd->obd_inactive)
376 else if (obd->obd_set_up)
378 else if (obd->obd_attached)
383 return seq_printf(p, "%3d %s %s %s %s %d\n",
384 (int)index, status, obd->obd_type->typ_name,
385 obd->obd_name, obd->obd_uuid.uuid,
386 cfs_atomic_read(&obd->obd_refcount));
389 struct seq_operations obd_device_list_sops = {
390 .start = obd_device_list_seq_start,
391 .stop = obd_device_list_seq_stop,
392 .next = obd_device_list_seq_next,
393 .show = obd_device_list_seq_show,
396 static int obd_device_list_open(struct inode *inode, struct file *file)
398 struct proc_dir_entry *dp = PDE(inode);
399 struct seq_file *seq;
400 int rc = seq_open(file, &obd_device_list_sops);
405 seq = file->private_data;
406 seq->private = dp->data;
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_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);
450 /* Check that we're building against the appropriate version of the Lustre
452 #include <linux/lustre_version.h>
453 #ifdef LUSTRE_KERNEL_VERSION
454 #define LUSTRE_MIN_VERSION 45
455 #define LUSTRE_MAX_VERSION 47
456 #if (LUSTRE_KERNEL_VERSION < LUSTRE_MIN_VERSION)
457 # error Cannot continue: Your Lustre kernel patch is older than the sources
458 #elif (LUSTRE_KERNEL_VERSION > LUSTRE_MAX_VERSION)
459 # error Cannot continue: Your Lustre sources are older than the kernel patch