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 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
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 # define EXPORT_SYMTAB
49 #ifndef AUTOCONF_INCLUDED
50 #include <linux/config.h> /* for CONFIG_PROC_FS */
52 #include <linux/module.h>
53 #include <linux/errno.h>
54 #include <linux/kernel.h>
55 #include <linux/major.h>
56 #include <linux/sched.h>
58 #include <linux/slab.h>
59 #include <linux/ioport.h>
60 #include <linux/fcntl.h>
61 #include <linux/delay.h>
62 #include <linux/skbuff.h>
63 #include <linux/proc_fs.h>
65 #include <linux/poll.h>
66 #include <linux/init.h>
67 #include <linux/list.h>
68 #include <linux/highmem.h>
70 #include <asm/ioctls.h>
71 #include <asm/system.h>
73 #include <asm/uaccess.h>
74 #include <linux/miscdevice.h>
75 #include <linux/smp_lock.h>
76 #include <linux/seq_file.h>
78 # include <liblustre.h>
81 #include <libcfs/libcfs.h>
82 #include <obd_support.h>
83 #include <obd_class.h>
84 #include <lprocfs_status.h>
85 #include <lustre_ver.h>
86 #include <lustre/lustre_build_version.h>
88 #include <linux/lustre_version.h>
92 /* buffer MUST be at least the size of obd_ioctl_hdr */
93 int obd_ioctl_getdata(char **buf, int *len, void *arg)
95 struct obd_ioctl_hdr hdr;
96 struct obd_ioctl_data *data;
101 err = cfs_copy_from_user(&hdr, (void *)arg, sizeof(hdr));
105 if (hdr.ioc_version != OBD_IOCTL_VERSION) {
106 CERROR("Version mismatch kernel (%x) vs application (%x)\n",
107 OBD_IOCTL_VERSION, hdr.ioc_version);
111 if (hdr.ioc_len > OBD_MAX_IOCTL_BUFFER) {
112 CERROR("User buffer len %d exceeds %d max buffer\n",
113 hdr.ioc_len, OBD_MAX_IOCTL_BUFFER);
117 if (hdr.ioc_len < sizeof(struct obd_ioctl_data)) {
118 CERROR("User buffer too small for ioctl (%d)\n", hdr.ioc_len);
122 /* XXX allocate this more intelligently, using kmalloc when
124 OBD_VMALLOC(*buf, hdr.ioc_len);
126 CERROR("Cannot allocate control buffer of len %d\n",
131 data = (struct obd_ioctl_data *)*buf;
133 err = cfs_copy_from_user(*buf, (void *)arg, hdr.ioc_len);
135 OBD_VFREE(*buf, hdr.ioc_len);
139 if (obd_ioctl_is_invalid(data)) {
140 CERROR("ioctl not correctly formatted\n");
141 OBD_VFREE(*buf, hdr.ioc_len);
145 if (data->ioc_inllen1) {
146 data->ioc_inlbuf1 = &data->ioc_bulk[0];
147 offset += cfs_size_round(data->ioc_inllen1);
150 if (data->ioc_inllen2) {
151 data->ioc_inlbuf2 = &data->ioc_bulk[0] + offset;
152 offset += cfs_size_round(data->ioc_inllen2);
155 if (data->ioc_inllen3) {
156 data->ioc_inlbuf3 = &data->ioc_bulk[0] + offset;
157 offset += cfs_size_round(data->ioc_inllen3);
160 if (data->ioc_inllen4) {
161 data->ioc_inlbuf4 = &data->ioc_bulk[0] + offset;
168 int obd_ioctl_popdata(void *arg, void *data, int len)
172 err = cfs_copy_to_user(arg, data, len);
178 EXPORT_SYMBOL(obd_ioctl_getdata);
179 EXPORT_SYMBOL(obd_ioctl_popdata);
181 #define OBD_MINOR 241
182 extern struct cfs_psdev_ops obd_psdev_ops;
184 /* opening /dev/obd */
185 static int obd_class_open(struct inode * inode, struct file * file)
187 if (obd_psdev_ops.p_open != NULL)
188 return obd_psdev_ops.p_open(0, NULL);
192 /* closing /dev/obd */
193 static int obd_class_release(struct inode * inode, struct file * file)
195 if (obd_psdev_ops.p_close != NULL)
196 return obd_psdev_ops.p_close(0, NULL);
200 /* to control /dev/obd */
201 static int obd_class_ioctl(struct inode *inode, struct file *filp,
202 unsigned int cmd, unsigned long arg)
207 /* Allow non-root access for OBD_IOC_PING_TARGET - used by lfs check */
208 if (!cfs_capable(CFS_CAP_SYS_ADMIN) && (cmd != OBD_IOC_PING_TARGET))
209 RETURN(err = -EACCES);
210 if ((cmd & 0xffffff00) == ((int)'T') << 8) /* ignore all tty ioctls */
211 RETURN(err = -ENOTTY);
213 if (obd_psdev_ops.p_ioctl != NULL)
214 err = obd_psdev_ops.p_ioctl(NULL, cmd, (void *)arg);
221 /* declare character device */
222 static struct file_operations obd_psdev_fops = {
223 .owner = THIS_MODULE,
224 .ioctl = obd_class_ioctl, /* ioctl */
225 .open = obd_class_open, /* open */
226 .release = obd_class_release, /* release */
230 cfs_psdev_t obd_psdev = {
233 .fops = &obd_psdev_fops,
239 int obd_proc_read_version(char *page, char **start, off_t off, int count,
240 int *eof, void *data)
243 #ifdef HAVE_VFS_INTENT_PATCHES
244 return snprintf(page, count, "lustre: %s\nkernel: %u\nbuild: %s\n",
245 LUSTRE_VERSION_STRING, LUSTRE_KERNEL_VERSION,
248 return snprintf(page, count, "lustre: %s\nkernel: %s\nbuild: %s\n",
249 LUSTRE_VERSION_STRING, "patchless_client",
254 int obd_proc_read_pinger(char *page, char **start, off_t off, int count,
255 int *eof, void *data)
258 return snprintf(page, count, "%s\n",
268 * Check all obd devices health
276 * proc read function parameters, please refer to kernel
277 * code fs/proc/generic.c proc_file_read()
278 * \param data [in] unused
280 * \retval number of characters printed
282 static int obd_proc_read_health(char *page, char **start, off_t off,
283 int count, int *eof, void *data)
288 if (libcfs_catastrophe)
289 rc += snprintf(page + rc, count - rc, "LBUG\n");
291 cfs_spin_lock(&obd_dev_lock);
292 for (i = 0; i < class_devno_max(); i++) {
293 struct obd_device *obd;
295 obd = class_num2obd(i);
296 if (obd == NULL || !obd->obd_attached || !obd->obd_set_up)
299 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
300 if (obd->obd_stopping)
303 class_incref(obd, __FUNCTION__, cfs_current());
304 cfs_spin_unlock(&obd_dev_lock);
306 if (obd_health_check(obd)) {
307 rc += snprintf(page + rc, count - rc,
308 "device %s reported unhealthy\n",
311 class_decref(obd, __FUNCTION__, cfs_current());
312 cfs_spin_lock(&obd_dev_lock);
314 cfs_spin_unlock(&obd_dev_lock);
317 return snprintf(page, count, "healthy\n");
319 rc += snprintf(page + rc, count - rc, "NOT HEALTHY\n");
323 /* Root for /proc/fs/lustre */
324 struct proc_dir_entry *proc_lustre_root = NULL;
326 struct lprocfs_vars lprocfs_base[] = {
327 { "version", obd_proc_read_version, NULL, NULL },
328 { "pinger", obd_proc_read_pinger, NULL, NULL },
329 { "health_check", obd_proc_read_health, NULL, NULL },
333 #define lprocfs_base NULL
337 static void *obd_device_list_seq_start(struct seq_file *p, loff_t *pos)
339 if (*pos >= class_devno_max())
345 static void obd_device_list_seq_stop(struct seq_file *p, void *v)
349 static void *obd_device_list_seq_next(struct seq_file *p, void *v, loff_t *pos)
352 if (*pos >= class_devno_max())
358 static int obd_device_list_seq_show(struct seq_file *p, void *v)
360 loff_t index = *(loff_t *)v;
361 struct obd_device *obd = class_num2obd((int)index);
367 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
368 if (obd->obd_stopping)
370 else if (obd->obd_inactive)
372 else if (obd->obd_set_up)
374 else if (obd->obd_attached)
379 return seq_printf(p, "%3d %s %s %s %s %d\n",
380 (int)index, status, obd->obd_type->typ_name,
381 obd->obd_name, obd->obd_uuid.uuid,
382 cfs_atomic_read(&obd->obd_refcount));
385 struct seq_operations obd_device_list_sops = {
386 .start = obd_device_list_seq_start,
387 .stop = obd_device_list_seq_stop,
388 .next = obd_device_list_seq_next,
389 .show = obd_device_list_seq_show,
392 static int obd_device_list_open(struct inode *inode, struct file *file)
394 struct proc_dir_entry *dp = PDE(inode);
395 struct seq_file *seq;
396 int rc = seq_open(file, &obd_device_list_sops);
401 seq = file->private_data;
402 seq->private = dp->data;
407 struct file_operations obd_device_list_fops = {
408 .owner = THIS_MODULE,
409 .open = obd_device_list_open,
412 .release = seq_release,
416 int class_procfs_init(void)
423 proc_lustre_root = lprocfs_register("fs/lustre", NULL,
425 rc = lprocfs_seq_create(proc_lustre_root, "devices", 0444,
426 &obd_device_list_fops, NULL);
428 CERROR("error adding /proc/fs/lustre/devices file\n");
436 int class_procfs_clean(void)
439 if (proc_lustre_root) {
440 lprocfs_remove(&proc_lustre_root);
446 /* Check that we're building against the appropriate version of the Lustre
448 #include <linux/lustre_version.h>
449 #ifdef LUSTRE_KERNEL_VERSION
450 #define LUSTRE_MIN_VERSION 45
451 #define LUSTRE_MAX_VERSION 47
452 #if (LUSTRE_KERNEL_VERSION < LUSTRE_MIN_VERSION)
453 # error Cannot continue: Your Lustre kernel patch is older than the sources
454 #elif (LUSTRE_KERNEL_VERSION > LUSTRE_MAX_VERSION)
455 # error Cannot continue: Your Lustre sources are older than the kernel patch