Whamcloud - gitweb
b=16098
[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 static int obd_proc_read_health(char *page, char **start, off_t off,
266                                 int count, int *eof, void *data)
267 {
268         int rc = 0, i;
269         *eof = 1;
270
271         if (libcfs_catastrophe)
272                 rc += snprintf(page + rc, count - rc, "LBUG\n");
273
274         spin_lock(&obd_dev_lock);
275         for (i = 0; i < class_devno_max(); i++) {
276                 struct obd_device *obd;
277
278                 obd = class_num2obd(i);
279                 if (obd == NULL)
280                         continue;
281
282                 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
283                 if (obd->obd_stopping)
284                         continue;
285
286                 class_incref(obd);
287                 spin_unlock(&obd_dev_lock);
288
289                 if (obd_health_check(obd)) {
290                         rc += snprintf(page + rc, count - rc,
291                                        "device %s reported unhealthy\n",
292                                        obd->obd_name);
293                 }
294                 class_decref(obd);
295                 spin_lock(&obd_dev_lock);
296         }
297         spin_unlock(&obd_dev_lock);
298
299         if (rc == 0)
300                 return snprintf(page, count, "healthy\n");
301
302         rc += snprintf(page + rc, count - rc, "NOT HEALTHY\n");
303         return rc;
304 }
305
306 /* Root for /proc/fs/lustre */
307 struct proc_dir_entry *proc_lustre_root = NULL;
308
309 struct lprocfs_vars lprocfs_base[] = {
310         { "version", obd_proc_read_version, NULL, NULL },
311         { "pinger", obd_proc_read_pinger, NULL, NULL },
312         { "health_check", obd_proc_read_health, NULL, NULL },
313         { 0 }
314 };
315 #else
316 #define lprocfs_base NULL
317 #endif /* LPROCFS */
318
319 #ifdef __KERNEL__
320 static void *obd_device_list_seq_start(struct seq_file *p, loff_t *pos)
321 {
322         if (*pos >= class_devno_max())
323                 return NULL;
324
325         return pos;
326 }
327
328 static void obd_device_list_seq_stop(struct seq_file *p, void *v)
329 {
330 }
331
332 static void *obd_device_list_seq_next(struct seq_file *p, void *v, loff_t *pos)
333 {      
334         ++*pos;
335         if (*pos >= class_devno_max())
336                 return NULL;
337
338         return pos;
339 }
340
341 static int obd_device_list_seq_show(struct seq_file *p, void *v)
342 {
343         loff_t index = *(loff_t *)v;
344         struct obd_device *obd = class_num2obd((int)index);
345         char *status;
346
347         if (obd == NULL)
348                 return 0;
349
350         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
351         if (obd->obd_stopping)
352                 status = "ST";
353         else if (obd->obd_inactive)
354                 status = "IN";
355         else if (obd->obd_set_up)
356                 status = "UP";
357         else if (obd->obd_attached)
358                 status = "AT";
359         else
360                 status = "--";
361
362         return seq_printf(p, "%3d %s %s %s %s %d\n",
363                           (int)index, status, obd->obd_type->typ_name,
364                           obd->obd_name, obd->obd_uuid.uuid,
365                           atomic_read(&obd->obd_refcount));
366 }
367
368 struct seq_operations obd_device_list_sops = {
369         .start = obd_device_list_seq_start,
370         .stop = obd_device_list_seq_stop,
371         .next = obd_device_list_seq_next,
372         .show = obd_device_list_seq_show,
373 };
374
375 static int obd_device_list_open(struct inode *inode, struct file *file)
376 {
377         struct proc_dir_entry *dp = PDE(inode);
378         struct seq_file *seq;
379         int rc = seq_open(file, &obd_device_list_sops);
380
381         if (rc)
382                 return rc;
383
384         seq = file->private_data;
385         seq->private = dp->data;
386
387         return 0;
388 }
389
390 struct file_operations obd_device_list_fops = {
391         .owner   = THIS_MODULE,
392         .open    = obd_device_list_open,
393         .read    = seq_read,
394         .llseek  = seq_lseek,
395         .release = seq_release,
396 };
397 #endif
398
399 int class_procfs_init(void)
400 {
401 #ifdef __KERNEL__
402         int rc;
403         ENTRY;
404
405         obd_sysctl_init();
406         proc_lustre_root = lprocfs_register("lustre", proc_root_fs,
407                                             lprocfs_base, NULL);
408         rc = lprocfs_seq_create(proc_lustre_root, "devices", 0444,
409                                 &obd_device_list_fops, NULL);
410         if (rc)
411                 CERROR("error adding /proc/fs/lustre/devices file\n");
412 #else
413         ENTRY;
414 #endif
415         RETURN(0);
416 }
417
418 #ifdef __KERNEL__
419 int class_procfs_clean(void)
420 {
421         ENTRY;
422         if (proc_lustre_root) {
423                 lprocfs_remove(&proc_lustre_root);
424         }
425         RETURN(0);
426 }
427
428
429 /* Check that we're building against the appropriate version of the Lustre
430  * kernel patch */
431 #include <linux/lustre_version.h>
432 #ifdef LUSTRE_KERNEL_VERSION
433 #define LUSTRE_MIN_VERSION 45
434 #define LUSTRE_MAX_VERSION 47
435 #if (LUSTRE_KERNEL_VERSION < LUSTRE_MIN_VERSION)
436 # error Cannot continue: Your Lustre kernel patch is older than the sources
437 #elif (LUSTRE_KERNEL_VERSION > LUSTRE_MAX_VERSION)
438 # error Cannot continue: Your Lustre sources are older than the kernel patch
439 #endif
440 #endif
441 #endif