Whamcloud - gitweb
land b1_5 onto 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  * Object Devices Class Driver
5  *
6  *  Copyright (C) 2001-2003 Cluster File Systems, Inc.
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  * These are the only exported functions, they provide some generic
24  * infrastructure for managing object devices
25  */
26 #define DEBUG_SUBSYSTEM S_CLASS
27 #ifndef EXPORT_SYMTAB
28 # define EXPORT_SYMTAB
29 #endif
30
31 #ifdef __KERNEL__
32 #ifdef HAVE_KERNEL_CONFIG_H
33 #include <linux/config.h> /* for CONFIG_PROC_FS */
34 #endif
35 #include <linux/module.h>
36 #include <linux/errno.h>
37 #include <linux/kernel.h>
38 #include <linux/major.h>
39 #include <linux/sched.h>
40 #include <linux/lp.h>
41 #include <linux/slab.h>
42 #include <linux/ioport.h>
43 #include <linux/fcntl.h>
44 #include <linux/delay.h>
45 #include <linux/skbuff.h>
46 #include <linux/proc_fs.h>
47 #include <linux/fs.h>
48 #include <linux/poll.h>
49 #include <linux/init.h>
50 #include <linux/list.h>
51 #include <linux/highmem.h>
52 #include <asm/io.h>
53 #include <asm/ioctls.h>
54 #include <asm/system.h>
55 #include <asm/poll.h>
56 #include <asm/uaccess.h>
57 #include <linux/miscdevice.h>
58 #include <linux/smp_lock.h>
59 #include <linux/seq_file.h>
60 #else
61 # include <liblustre.h>
62 #endif
63
64 #include <libcfs/libcfs.h>
65 #include <obd_support.h>
66 #include <obd_class.h>
67 #include <lprocfs_status.h>
68 #include <lustre_ver.h>
69 #ifdef __KERNEL__
70 #include <linux/lustre_build_version.h>
71 #include <linux/lustre_version.h>
72
73 int proc_version;
74
75 /* buffer MUST be at least the size of obd_ioctl_hdr */
76 int obd_ioctl_getdata(char **buf, int *len, void *arg)
77 {
78         struct obd_ioctl_hdr hdr;
79         struct obd_ioctl_data *data;
80         int err;
81         int offset = 0;
82         ENTRY;
83
84         err = copy_from_user(&hdr, (void *)arg, sizeof(hdr));
85         if ( err ) 
86                 RETURN(err);
87
88         if (hdr.ioc_version != OBD_IOCTL_VERSION) {
89                 CERROR("Version mismatch kernel (%x) vs application (%x)\n",
90                        OBD_IOCTL_VERSION, hdr.ioc_version);
91                 RETURN(-EINVAL);
92         }
93
94         if (hdr.ioc_len > OBD_MAX_IOCTL_BUFFER) {
95                 CERROR("User buffer len %d exceeds %d max buffer\n",
96                        hdr.ioc_len, OBD_MAX_IOCTL_BUFFER);
97                 RETURN(-EINVAL);
98         }
99
100         if (hdr.ioc_len < sizeof(struct obd_ioctl_data)) {
101                 CERROR("User buffer too small for ioctl (%d)\n", hdr.ioc_len);
102                 RETURN(-EINVAL);
103         }
104
105         /* XXX allocate this more intelligently, using kmalloc when
106          * appropriate */
107         OBD_VMALLOC(*buf, hdr.ioc_len);
108         if (*buf == NULL) {
109                 CERROR("Cannot allocate control buffer of len %d\n",
110                        hdr.ioc_len);
111                 RETURN(-EINVAL);
112         }
113         *len = hdr.ioc_len;
114         data = (struct obd_ioctl_data *)*buf;
115
116         err = copy_from_user(*buf, (void *)arg, hdr.ioc_len);
117         if ( err ) {
118                 OBD_VFREE(*buf, hdr.ioc_len);
119                 RETURN(err);
120         }
121
122         if (obd_ioctl_is_invalid(data)) {
123                 CERROR("ioctl not correctly formatted\n");
124                 OBD_VFREE(*buf, hdr.ioc_len);
125                 RETURN(-EINVAL);
126         }
127
128         if (data->ioc_inllen1) {
129                 data->ioc_inlbuf1 = &data->ioc_bulk[0];
130                 offset += size_round(data->ioc_inllen1);
131         }
132
133         if (data->ioc_inllen2) {
134                 data->ioc_inlbuf2 = &data->ioc_bulk[0] + offset;
135                 offset += size_round(data->ioc_inllen2);
136         }
137
138         if (data->ioc_inllen3) {
139                 data->ioc_inlbuf3 = &data->ioc_bulk[0] + offset;
140                 offset += size_round(data->ioc_inllen3);
141         }
142
143         if (data->ioc_inllen4) {
144                 data->ioc_inlbuf4 = &data->ioc_bulk[0] + offset;
145         }
146
147         EXIT;
148         return 0;
149 }
150
151 int obd_ioctl_popdata(void *arg, void *data, int len)
152 {
153         int err; 
154         
155         err = copy_to_user(arg, data, len);
156         if (err)
157                 err = -EFAULT;
158         return err;
159 }
160
161 EXPORT_SYMBOL(obd_ioctl_getdata);
162 EXPORT_SYMBOL(obd_ioctl_popdata);
163
164 #define OBD_MINOR 241
165 extern struct cfs_psdev_ops          obd_psdev_ops;
166
167 /*  opening /dev/obd */
168 static int obd_class_open(struct inode * inode, struct file * file)
169 {
170         if (obd_psdev_ops.p_open != NULL)
171                 return obd_psdev_ops.p_open(0, NULL);
172         return -EPERM;
173 }
174
175 /*  closing /dev/obd */
176 static int obd_class_release(struct inode * inode, struct file * file)
177 {
178         if (obd_psdev_ops.p_close != NULL)
179                 return obd_psdev_ops.p_close(0, NULL);
180         return -EPERM;
181 }
182
183 /* to control /dev/obd */
184 static int obd_class_ioctl(struct inode *inode, struct file *filp,
185                            unsigned int cmd, unsigned long arg)
186 {
187         int err = 0;
188         ENTRY;
189
190         if (current->fsuid != 0)
191                 RETURN(err = -EACCES);
192         if ((cmd & 0xffffff00) == ((int)'T') << 8) /* ignore all tty ioctls */
193                 RETURN(err = -ENOTTY);
194
195         if (obd_psdev_ops.p_ioctl != NULL)
196                 err = obd_psdev_ops.p_ioctl(NULL, cmd, (void *)arg);
197         else
198                 err = -EPERM;
199
200         RETURN(err);
201 }
202
203 /* declare character device */
204 static struct file_operations obd_psdev_fops = {
205         .owner   = THIS_MODULE,
206         .ioctl   = obd_class_ioctl,     /* ioctl */
207         .open    = obd_class_open,      /* open */
208         .release = obd_class_release,   /* release */
209 };
210
211 /* modules setup */
212 cfs_psdev_t obd_psdev = {
213         .minor = OBD_MINOR,
214         .name  = "obd_psdev",
215         .fops  = &obd_psdev_fops,
216 };
217
218 #endif
219
220 #ifdef LPROCFS
221 int obd_proc_read_version(char *page, char **start, off_t off, int count,
222                           int *eof, void *data)
223 {
224         *eof = 1;
225 #ifdef LUSTRE_KERNEL_VERSION
226         return snprintf(page, count, "lustre: %s\nkernel: %u\nbuild:  %s\n",
227                         LUSTRE_VERSION_STRING, LUSTRE_KERNEL_VERSION,
228                         BUILD_VERSION);
229 #else
230         return snprintf(page, count, "lustre: %s\nkernel: %s\nbuild:  %s\n",
231                         LUSTRE_VERSION_STRING, "patchless", BUILD_VERSION);
232 #endif
233 }
234
235 int obd_proc_read_pinger(char *page, char **start, off_t off, int count,
236                          int *eof, void *data)
237 {
238         *eof = 1;
239         return snprintf(page, count, "%s\n",
240 #ifdef ENABLE_PINGER
241                         "on"
242 #else
243                         "off"
244 #endif
245                        );
246 }
247
248 static int obd_proc_read_health(char *page, char **start, off_t off,
249                                 int count, int *eof, void *data)
250 {
251         int rc = 0, i;
252         *eof = 1;
253
254         if (libcfs_catastrophe)
255                 rc += snprintf(page + rc, count - rc, "LBUG\n");
256
257         spin_lock(&obd_dev_lock);
258         for (i = 0; i < class_devno_max(); i++) {
259                 struct obd_device *obd;
260
261                 obd = class_num2obd(i);
262                 if (obd == NULL)
263                         continue;
264
265                 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
266                 if (obd->obd_stopping)
267                         continue;
268
269                 class_incref(obd);
270                 spin_unlock(&obd_dev_lock);
271
272                 if (obd_health_check(obd)) {
273                         rc += snprintf(page + rc, count - rc,
274                                        "device %s reported unhealthy\n",
275                                        obd->obd_name);
276                 }
277                 class_decref(obd);
278                 spin_lock(&obd_dev_lock);
279         }
280         spin_unlock(&obd_dev_lock);
281
282         if (rc == 0)
283                 return snprintf(page, count, "healthy\n");
284
285         rc += snprintf(page + rc, count - rc, "NOT HEALTHY\n");
286         return rc;
287 }
288
289 static int obd_proc_rd_health_timeout(char *page, char **start, off_t off,
290                                       int count, int *eof, void *data)
291 {
292         *eof = 1;
293         return snprintf(page, count, "%d\n", obd_health_check_timeout);
294 }
295
296 static int obd_proc_wr_health_timeout(struct file *file, const char *buffer,
297                                       unsigned long count, void *data)
298 {
299         int val, rc;
300
301         rc = lprocfs_write_helper(buffer, count, &val);
302         if (rc)
303                 return rc;
304
305         obd_health_check_timeout = val;
306
307         return count;
308 }
309
310 /* Root for /proc/fs/lustre */
311 struct proc_dir_entry *proc_lustre_root = NULL;
312
313 struct lprocfs_vars lprocfs_base[] = {
314         { "version", obd_proc_read_version, NULL, NULL },
315         { "pinger", obd_proc_read_pinger, NULL, NULL },
316         { "health_check", obd_proc_read_health, NULL, NULL },
317         { "health_check_timeout", obd_proc_rd_health_timeout,
318            obd_proc_wr_health_timeout, NULL },
319         { 0 }
320 };
321 #else
322 #define lprocfs_base NULL
323 #endif /* LPROCFS */
324
325 #ifdef __KERNEL__
326 static void *obd_device_list_seq_start(struct seq_file *p, loff_t*pos)
327 {
328         if (*pos >= class_devno_max())
329                 return NULL;
330
331         return pos;
332 }
333
334 static void obd_device_list_seq_stop(struct seq_file *p, void *v)
335 {
336 }
337
338 static void *obd_device_list_seq_next(struct seq_file *p, void *v, loff_t *pos)
339 {      
340         ++*pos;
341         if (*pos >= class_devno_max())
342                 return NULL;
343
344         return pos;
345 }
346
347 static int obd_device_list_seq_show(struct seq_file *p, void *v)
348 {
349         int index = *(int*)v;
350         struct obd_device *obd = class_num2obd(index);
351         char *status;
352
353         if (obd == NULL)
354                 return 0;
355
356         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
357         if (obd->obd_stopping)
358                 status = "ST";
359         else if (obd->obd_set_up)
360                 status = "UP";
361         else if (obd->obd_attached)
362                 status = "AT";
363         else
364                 status = "--";
365
366         return seq_printf(p, "%3d %s %s %s %s %d\n",
367                           (int)index, status, obd->obd_type->typ_name,
368                           obd->obd_name, obd->obd_uuid.uuid,
369                           atomic_read(&obd->obd_refcount));
370 }
371
372 struct seq_operations obd_device_list_sops = {
373         .start = obd_device_list_seq_start,
374         .stop = obd_device_list_seq_stop,
375         .next = obd_device_list_seq_next,
376         .show = obd_device_list_seq_show,
377 };
378
379 static int obd_device_list_open(struct inode *inode, struct file *file)
380 {
381         struct proc_dir_entry *dp = PDE(inode);
382         struct seq_file *seq;
383         int rc = seq_open(file, &obd_device_list_sops);
384
385         if (rc)
386                 return rc;
387
388         seq = file->private_data;
389         seq->private = dp->data;
390
391         return 0;
392 }
393
394 struct file_operations obd_device_list_fops = {
395         .owner   = THIS_MODULE,
396         .open    = obd_device_list_open,
397         .read    = seq_read,
398         .llseek  = seq_lseek,
399         .release = seq_release,
400 };
401 #endif
402
403 int class_procfs_init(void)
404 {
405 #ifdef __KERNEL__
406         struct proc_dir_entry *entry;
407         ENTRY;
408
409         obd_sysctl_init();
410         proc_lustre_root = proc_mkdir("lustre", proc_root_fs);
411         if (!proc_lustre_root) {
412                 printk(KERN_ERR
413                        "LustreError: error registering /proc/fs/lustre\n");
414                 RETURN(-ENOMEM);
415         }
416         proc_version = lprocfs_add_vars(proc_lustre_root, lprocfs_base, NULL);
417         entry = create_proc_entry("devices", 0444, proc_lustre_root);
418         if (entry == NULL) {
419                 CERROR("error registering /proc/fs/lustre/devices\n");
420                 lprocfs_remove(&proc_lustre_root);
421                 RETURN(-ENOMEM);
422         }
423         entry->proc_fops = &obd_device_list_fops;
424 #else
425         ENTRY;
426 #endif
427         RETURN(0);
428 }
429
430 #ifdef __KERNEL__
431 int class_procfs_clean(void)
432 {
433         ENTRY;
434         if (proc_lustre_root) 
435                 lprocfs_remove(&proc_lustre_root);
436         RETURN(0);
437 }
438
439
440 /* Check that we're building against the appropriate version of the Lustre
441  * kernel patch */
442 #include <linux/lustre_version.h>
443 #ifdef LUSTRE_KERNEL_VERSION
444 #define LUSTRE_MIN_VERSION 37
445 #define LUSTRE_MAX_VERSION 47
446 #if (LUSTRE_KERNEL_VERSION < LUSTRE_MIN_VERSION)
447 # error Cannot continue: Your Lustre kernel patch is older than the sources
448 #elif (LUSTRE_KERNEL_VERSION > LUSTRE_MAX_VERSION)
449 # error Cannot continue: Your Lustre sources are older than the kernel patch
450 #endif
451 #endif
452 #endif