Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / lustre / obdclass / linux / linux-module.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
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.
11  *
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).
17  *
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
21  *
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
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/obdclass/linux/linux-module.c
37  *
38  * Object Devices Class Driver
39  * These are the only exported functions, they provide some generic
40  * infrastructure for managing object devices
41  */
42
43 #define DEBUG_SUBSYSTEM S_CLASS
44 #ifndef EXPORT_SYMTAB
45 # define EXPORT_SYMTAB
46 #endif
47
48 #ifdef __KERNEL__
49 #ifndef AUTOCONF_INCLUDED
50 #include <linux/config.h> /* for CONFIG_PROC_FS */
51 #endif
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>
57 #include <linux/lp.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>
64 #include <linux/fs.h>
65 #include <linux/poll.h>
66 #include <linux/init.h>
67 #include <linux/list.h>
68 #include <linux/highmem.h>
69 #include <asm/io.h>
70 #include <asm/ioctls.h>
71 #include <asm/system.h>
72 #include <asm/poll.h>
73 #include <asm/uaccess.h>
74 #include <linux/miscdevice.h>
75 #include <linux/smp_lock.h>
76 #include <linux/seq_file.h>
77 #else
78 # include <liblustre.h>
79 #endif
80
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>
87 #ifdef __KERNEL__
88 #include <linux/lustre_version.h>
89
90 int proc_version;
91
92 /* buffer MUST be at least the size of obd_ioctl_hdr */
93 int obd_ioctl_getdata(char **buf, int *len, void *arg)
94 {
95         struct obd_ioctl_hdr hdr;
96         struct obd_ioctl_data *data;
97         int err;
98         int offset = 0;
99         ENTRY;
100
101         err = copy_from_user(&hdr, (void *)arg, sizeof(hdr));
102         if ( err )
103                 RETURN(err);
104
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);
108                 RETURN(-EINVAL);
109         }
110
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);
114                 RETURN(-EINVAL);
115         }
116
117         if (hdr.ioc_len < sizeof(struct obd_ioctl_data)) {
118                 CERROR("User buffer too small for ioctl (%d)\n", hdr.ioc_len);
119                 RETURN(-EINVAL);
120         }
121
122         /* XXX allocate this more intelligently, using kmalloc when
123          * appropriate */
124         OBD_VMALLOC(*buf, hdr.ioc_len);
125         if (*buf == NULL) {
126                 CERROR("Cannot allocate control buffer of len %d\n",
127                        hdr.ioc_len);
128                 RETURN(-EINVAL);
129         }
130         *len = hdr.ioc_len;
131         data = (struct obd_ioctl_data *)*buf;
132
133         err = copy_from_user(*buf, (void *)arg, hdr.ioc_len);
134         if ( err ) {
135                 OBD_VFREE(*buf, hdr.ioc_len);
136                 RETURN(err);
137         }
138
139         if (obd_ioctl_is_invalid(data)) {
140                 CERROR("ioctl not correctly formatted\n");
141                 OBD_VFREE(*buf, hdr.ioc_len);
142                 RETURN(-EINVAL);
143         }
144
145         if (data->ioc_inllen1) {
146                 data->ioc_inlbuf1 = &data->ioc_bulk[0];
147                 offset += size_round(data->ioc_inllen1);
148         }
149
150         if (data->ioc_inllen2) {
151                 data->ioc_inlbuf2 = &data->ioc_bulk[0] + offset;
152                 offset += size_round(data->ioc_inllen2);
153         }
154
155         if (data->ioc_inllen3) {
156                 data->ioc_inlbuf3 = &data->ioc_bulk[0] + offset;
157                 offset += size_round(data->ioc_inllen3);
158         }
159
160         if (data->ioc_inllen4) {
161                 data->ioc_inlbuf4 = &data->ioc_bulk[0] + offset;
162         }
163
164         EXIT;
165         return 0;
166 }
167
168 int obd_ioctl_popdata(void *arg, void *data, int len)
169 {
170         int err;
171
172         err = copy_to_user(arg, data, len);
173         if (err)
174                 err = -EFAULT;
175         return err;
176 }
177
178 EXPORT_SYMBOL(obd_ioctl_getdata);
179 EXPORT_SYMBOL(obd_ioctl_popdata);
180
181 #define OBD_MINOR 241
182 extern struct cfs_psdev_ops          obd_psdev_ops;
183
184 /*  opening /dev/obd */
185 static int obd_class_open(struct inode * inode, struct file * file)
186 {
187         if (obd_psdev_ops.p_open != NULL)
188                 return obd_psdev_ops.p_open(0, NULL);
189         return -EPERM;
190 }
191
192 /*  closing /dev/obd */
193 static int obd_class_release(struct inode * inode, struct file * file)
194 {
195         if (obd_psdev_ops.p_close != NULL)
196                 return obd_psdev_ops.p_close(0, NULL);
197         return -EPERM;
198 }
199
200 /* to control /dev/obd */
201 static int obd_class_ioctl(struct inode *inode, struct file *filp,
202                            unsigned int cmd, unsigned long arg)
203 {
204         int err = 0;
205         ENTRY;
206
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);
212
213         if (obd_psdev_ops.p_ioctl != NULL)
214                 err = obd_psdev_ops.p_ioctl(NULL, cmd, (void *)arg);
215         else
216                 err = -EPERM;
217
218         RETURN(err);
219 }
220
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 */
227 };
228
229 /* modules setup */
230 cfs_psdev_t obd_psdev = {
231         .minor = OBD_MINOR,
232         .name  = "obd_psdev",
233         .fops  = &obd_psdev_fops,
234 };
235
236 #endif
237
238 #ifdef LPROCFS
239 int obd_proc_read_version(char *page, char **start, off_t off, int count,
240                           int *eof, void *data)
241 {
242         *eof = 1;
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,
246                         BUILD_VERSION);
247 #else
248         return snprintf(page, count, "lustre: %s\nkernel: %s\nbuild:  %s\n",
249                         LUSTRE_VERSION_STRING, "patchless_client",
250                         BUILD_VERSION);
251 #endif
252 }
253
254 int obd_proc_read_pinger(char *page, char **start, off_t off, int count,
255                          int *eof, void *data)
256 {
257         *eof = 1;
258         return snprintf(page, count, "%s\n",
259 #ifdef ENABLE_PINGER
260                         "on"
261 #else
262                         "off"
263 #endif
264                        );
265 }
266
267 /**
268  * Check all obd devices health
269  *
270  * \param page
271  * \param start
272  * \param off
273  * \param count
274  * \param eof
275  * \param data
276  *                  proc read function parameters, please refer to kernel
277  *                  code fs/proc/generic.c proc_file_read()
278  * \param data [in] unused
279  *
280  * \retval number of characters printed
281  */
282 static int obd_proc_read_health(char *page, char **start, off_t off,
283                                 int count, int *eof, void *data)
284 {
285         int rc = 0, i;
286         *eof = 1;
287
288         if (libcfs_catastrophe)
289                 rc += snprintf(page + rc, count - rc, "LBUG\n");
290
291         spin_lock(&obd_dev_lock);
292         for (i = 0; i < class_devno_max(); i++) {
293                 struct obd_device *obd;
294
295                 obd = class_num2obd(i);
296                 if (obd == NULL || !obd->obd_attached || !obd->obd_set_up)
297                         continue;
298
299                 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
300                 if (obd->obd_stopping)
301                         continue;
302
303                 class_incref(obd, __FUNCTION__, cfs_current());
304                 spin_unlock(&obd_dev_lock);
305
306                 if (obd_health_check(obd)) {
307                         rc += snprintf(page + rc, count - rc,
308                                        "device %s reported unhealthy\n",
309                                        obd->obd_name);
310                 }
311                 class_decref(obd, __FUNCTION__, cfs_current());
312                 spin_lock(&obd_dev_lock);
313         }
314         spin_unlock(&obd_dev_lock);
315
316         if (rc == 0)
317                 return snprintf(page, count, "healthy\n");
318
319         rc += snprintf(page + rc, count - rc, "NOT HEALTHY\n");
320         return rc;
321 }
322
323 /* Root for /proc/fs/lustre */
324 struct proc_dir_entry *proc_lustre_root = NULL;
325
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 },
330         { 0 }
331 };
332 #else
333 #define lprocfs_base NULL
334 #endif /* LPROCFS */
335
336 #ifdef __KERNEL__
337 static void *obd_device_list_seq_start(struct seq_file *p, loff_t *pos)
338 {
339         if (*pos >= class_devno_max())
340                 return NULL;
341
342         return pos;
343 }
344
345 static void obd_device_list_seq_stop(struct seq_file *p, void *v)
346 {
347 }
348
349 static void *obd_device_list_seq_next(struct seq_file *p, void *v, loff_t *pos)
350 {
351         ++*pos;
352         if (*pos >= class_devno_max())
353                 return NULL;
354
355         return pos;
356 }
357
358 static int obd_device_list_seq_show(struct seq_file *p, void *v)
359 {
360         loff_t index = *(loff_t *)v;
361         struct obd_device *obd = class_num2obd((int)index);
362         char *status;
363
364         if (obd == NULL)
365                 return 0;
366
367         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
368         if (obd->obd_stopping)
369                 status = "ST";
370         else if (obd->obd_inactive)
371                 status = "IN";
372         else if (obd->obd_set_up)
373                 status = "UP";
374         else if (obd->obd_attached)
375                 status = "AT";
376         else
377                 status = "--";
378
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                           atomic_read(&obd->obd_refcount));
383 }
384
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,
390 };
391
392 static int obd_device_list_open(struct inode *inode, struct file *file)
393 {
394         struct proc_dir_entry *dp = PDE(inode);
395         struct seq_file *seq;
396         int rc = seq_open(file, &obd_device_list_sops);
397
398         if (rc)
399                 return rc;
400
401         seq = file->private_data;
402         seq->private = dp->data;
403
404         return 0;
405 }
406
407 struct file_operations obd_device_list_fops = {
408         .owner   = THIS_MODULE,
409         .open    = obd_device_list_open,
410         .read    = seq_read,
411         .llseek  = seq_lseek,
412         .release = seq_release,
413 };
414 #endif
415
416 int class_procfs_init(void)
417 {
418 #ifdef __KERNEL__
419         int rc;
420         ENTRY;
421
422         obd_sysctl_init();
423         proc_lustre_root = lprocfs_register("fs/lustre", NULL,
424                                             lprocfs_base, NULL);
425         rc = lprocfs_seq_create(proc_lustre_root, "devices", 0444,
426                                 &obd_device_list_fops, NULL);
427         if (rc)
428                 CERROR("error adding /proc/fs/lustre/devices file\n");
429 #else
430         ENTRY;
431 #endif
432         RETURN(0);
433 }
434
435 #ifdef __KERNEL__
436 int class_procfs_clean(void)
437 {
438         ENTRY;
439         if (proc_lustre_root) {
440                 lprocfs_remove(&proc_lustre_root);
441         }
442         RETURN(0);
443 }
444
445
446 /* Check that we're building against the appropriate version of the Lustre
447  * kernel patch */
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
456 #endif
457 #endif
458 #endif