Whamcloud - gitweb
137449cf8bb4ca3cc33198e9c4303690143489c3
[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 [sun.com URL with a
20  * copy of GPLv2].
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         if (current->fsuid != 0)
208                 RETURN(err = -EACCES);
209         if ((cmd & 0xffffff00) == ((int)'T') << 8) /* ignore all tty ioctls */
210                 RETURN(err = -ENOTTY);
211
212         if (obd_psdev_ops.p_ioctl != NULL)
213                 err = obd_psdev_ops.p_ioctl(NULL, cmd, (void *)arg);
214         else
215                 err = -EPERM;
216
217         RETURN(err);
218 }
219
220 /* declare character device */
221 static struct file_operations obd_psdev_fops = {
222         .owner   = THIS_MODULE,
223         .ioctl   = obd_class_ioctl,     /* ioctl */
224         .open    = obd_class_open,      /* open */
225         .release = obd_class_release,   /* release */
226 };
227
228 /* modules setup */
229 cfs_psdev_t obd_psdev = {
230         .minor = OBD_MINOR,
231         .name  = "obd_psdev",
232         .fops  = &obd_psdev_fops,
233 };
234
235 #endif
236
237 #ifdef LPROCFS
238 int obd_proc_read_version(char *page, char **start, off_t off, int count,
239                           int *eof, void *data)
240 {
241         *eof = 1;
242 #ifdef HAVE_VFS_INTENT_PATCHES
243         return snprintf(page, count, "lustre: %s\nkernel: %u\nbuild:  %s\n",
244                         LUSTRE_VERSION_STRING, LUSTRE_KERNEL_VERSION,
245                         BUILD_VERSION);
246 #else
247         return snprintf(page, count, "lustre: %s\nkernel: %s\nbuild:  %s\n",
248                         LUSTRE_VERSION_STRING, "patchless", BUILD_VERSION);
249 #endif
250 }
251
252 int obd_proc_read_pinger(char *page, char **start, off_t off, int count,
253                          int *eof, void *data)
254 {
255         *eof = 1;
256         return snprintf(page, count, "%s\n",
257 #ifdef ENABLE_PINGER
258                         "on"
259 #else
260                         "off"
261 #endif
262                        );
263 }
264
265 /**
266  * Check all obd devices health
267  *
268  * \param page
269  * \param start
270  * \param off
271  * \param count
272  * \param eof
273  * \param data
274  *                  proc read function parameters, please refer to kernel
275  *                  code fs/proc/generic.c proc_file_read()
276  * \param data [in] unused
277  *
278  * \retval number of characters printed
279  */
280 static int obd_proc_read_health(char *page, char **start, off_t off,
281                                 int count, int *eof, void *data)
282 {
283         int rc = 0, i;
284         *eof = 1;
285
286         if (libcfs_catastrophe)
287                 rc += snprintf(page + rc, count - rc, "LBUG\n");
288
289         spin_lock(&obd_dev_lock);
290         for (i = 0; i < class_devno_max(); i++) {
291                 struct obd_device *obd;
292
293                 obd = class_num2obd(i);
294                 if (obd == NULL || !obd->obd_attached || !obd->obd_set_up)
295                         continue;
296
297                 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
298                 if (obd->obd_stopping)
299                         continue;
300
301                 class_incref(obd);
302                 spin_unlock(&obd_dev_lock);
303
304                 if (obd_health_check(obd)) {
305                         rc += snprintf(page + rc, count - rc,
306                                        "device %s reported unhealthy\n",
307                                        obd->obd_name);
308                 }
309                 class_decref(obd);
310                 spin_lock(&obd_dev_lock);
311         }
312         spin_unlock(&obd_dev_lock);
313
314         if (rc == 0)
315                 return snprintf(page, count, "healthy\n");
316
317         rc += snprintf(page + rc, count - rc, "NOT HEALTHY\n");
318         return rc;
319 }
320
321 /* Root for /proc/fs/lustre */
322 struct proc_dir_entry *proc_lustre_root = NULL;
323
324 struct lprocfs_vars lprocfs_base[] = {
325         { "version", obd_proc_read_version, NULL, NULL },
326         { "pinger", obd_proc_read_pinger, NULL, NULL },
327         { "health_check", obd_proc_read_health, NULL, NULL },
328         { 0 }
329 };
330 #else
331 #define lprocfs_base NULL
332 #endif /* LPROCFS */
333
334 #ifdef __KERNEL__
335 static void *obd_device_list_seq_start(struct seq_file *p, loff_t *pos)
336 {
337         if (*pos >= class_devno_max())
338                 return NULL;
339
340         return pos;
341 }
342
343 static void obd_device_list_seq_stop(struct seq_file *p, void *v)
344 {
345 }
346
347 static void *obd_device_list_seq_next(struct seq_file *p, void *v, loff_t *pos)
348 {      
349         ++*pos;
350         if (*pos >= class_devno_max())
351                 return NULL;
352
353         return pos;
354 }
355
356 static int obd_device_list_seq_show(struct seq_file *p, void *v)
357 {
358         loff_t index = *(loff_t *)v;
359         struct obd_device *obd = class_num2obd((int)index);
360         char *status;
361
362         if (obd == NULL)
363                 return 0;
364
365         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
366         if (obd->obd_stopping)
367                 status = "ST";
368         else if (obd->obd_inactive)
369                 status = "IN";
370         else if (obd->obd_set_up)
371                 status = "UP";
372         else if (obd->obd_attached)
373                 status = "AT";
374         else
375                 status = "--";
376
377         return seq_printf(p, "%3d %s %s %s %s %d\n",
378                           (int)index, status, obd->obd_type->typ_name,
379                           obd->obd_name, obd->obd_uuid.uuid,
380                           atomic_read(&obd->obd_refcount));
381 }
382
383 struct seq_operations obd_device_list_sops = {
384         .start = obd_device_list_seq_start,
385         .stop = obd_device_list_seq_stop,
386         .next = obd_device_list_seq_next,
387         .show = obd_device_list_seq_show,
388 };
389
390 static int obd_device_list_open(struct inode *inode, struct file *file)
391 {
392         struct proc_dir_entry *dp = PDE(inode);
393         struct seq_file *seq;
394         int rc = seq_open(file, &obd_device_list_sops);
395
396         if (rc)
397                 return rc;
398
399         seq = file->private_data;
400         seq->private = dp->data;
401
402         return 0;
403 }
404
405 struct file_operations obd_device_list_fops = {
406         .owner   = THIS_MODULE,
407         .open    = obd_device_list_open,
408         .read    = seq_read,
409         .llseek  = seq_lseek,
410         .release = seq_release,
411 };
412 #endif
413
414 int class_procfs_init(void)
415 {
416 #ifdef __KERNEL__
417         int rc;
418         ENTRY;
419
420         obd_sysctl_init();
421         proc_lustre_root = lprocfs_register("lustre", proc_root_fs,
422                                             lprocfs_base, NULL);
423         rc = lprocfs_seq_create(proc_lustre_root, "devices", 0444,
424                                 &obd_device_list_fops, NULL);
425         if (rc)
426                 CERROR("error adding /proc/fs/lustre/devices file\n");
427 #else
428         ENTRY;
429 #endif
430         RETURN(0);
431 }
432
433 #ifdef __KERNEL__
434 int class_procfs_clean(void)
435 {
436         ENTRY;
437         if (proc_lustre_root) {
438                 lprocfs_remove(&proc_lustre_root);
439         }
440         RETURN(0);
441 }
442
443
444 /* Check that we're building against the appropriate version of the Lustre
445  * kernel patch */
446 #include <linux/lustre_version.h>
447 #ifdef LUSTRE_KERNEL_VERSION
448 #define LUSTRE_MIN_VERSION 45
449 #define LUSTRE_MAX_VERSION 47
450 #if (LUSTRE_KERNEL_VERSION < LUSTRE_MIN_VERSION)
451 # error Cannot continue: Your Lustre kernel patch is older than the sources
452 #elif (LUSTRE_KERNEL_VERSION > LUSTRE_MAX_VERSION)
453 # error Cannot continue: Your Lustre sources are older than the kernel patch
454 #endif
455 #endif
456 #endif