Whamcloud - gitweb
LU-147 avoid 8k obd device amount limit
[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 (c) 2007, 2010, Oracle and/or its affiliates. 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 <lnet/lnetctl.h>
85 #include <lprocfs_status.h>
86 #include <lustre_ver.h>
87 #include <lustre/lustre_build_version.h>
88 #ifdef __KERNEL__
89 #include <linux/lustre_version.h>
90
91 int proc_version;
92
93 /* buffer MUST be at least the size of obd_ioctl_hdr */
94 int obd_ioctl_getdata(char **buf, int *len, void *arg)
95 {
96         struct obd_ioctl_hdr hdr;
97         struct obd_ioctl_data *data;
98         int err;
99         int offset = 0;
100         ENTRY;
101
102         err = cfs_copy_from_user(&hdr, (void *)arg, sizeof(hdr));
103         if ( err )
104                 RETURN(err);
105
106         if (hdr.ioc_version != OBD_IOCTL_VERSION) {
107                 CERROR("Version mismatch kernel (%x) vs application (%x)\n",
108                        OBD_IOCTL_VERSION, hdr.ioc_version);
109                 RETURN(-EINVAL);
110         }
111
112         if (hdr.ioc_len > OBD_MAX_IOCTL_BUFFER) {
113                 CERROR("User buffer len %d exceeds %d max buffer\n",
114                        hdr.ioc_len, OBD_MAX_IOCTL_BUFFER);
115                 RETURN(-EINVAL);
116         }
117
118         if (hdr.ioc_len < sizeof(struct obd_ioctl_data)) {
119                 CERROR("User buffer too small for ioctl (%d)\n", hdr.ioc_len);
120                 RETURN(-EINVAL);
121         }
122
123         /* When there are lots of processes calling vmalloc on multi-core
124          * system, the high lock contention will hurt performance badly,
125          * obdfilter-survey is an example, which relies on ioctl. So we'd
126          * better avoid vmalloc on ioctl path. LU-66 */
127         OBD_ALLOC_LARGE(*buf, hdr.ioc_len);
128         if (*buf == NULL) {
129                 CERROR("Cannot allocate control buffer of len %d\n",
130                        hdr.ioc_len);
131                 RETURN(-EINVAL);
132         }
133         *len = hdr.ioc_len;
134         data = (struct obd_ioctl_data *)*buf;
135
136         err = cfs_copy_from_user(*buf, (void *)arg, hdr.ioc_len);
137         if ( err ) {
138                 OBD_FREE_LARGE(*buf, hdr.ioc_len);
139                 RETURN(err);
140         }
141
142         if (obd_ioctl_is_invalid(data)) {
143                 CERROR("ioctl not correctly formatted\n");
144                 OBD_FREE_LARGE(*buf, hdr.ioc_len);
145                 RETURN(-EINVAL);
146         }
147
148         if (data->ioc_inllen1) {
149                 data->ioc_inlbuf1 = &data->ioc_bulk[0];
150                 offset += cfs_size_round(data->ioc_inllen1);
151         }
152
153         if (data->ioc_inllen2) {
154                 data->ioc_inlbuf2 = &data->ioc_bulk[0] + offset;
155                 offset += cfs_size_round(data->ioc_inllen2);
156         }
157
158         if (data->ioc_inllen3) {
159                 data->ioc_inlbuf3 = &data->ioc_bulk[0] + offset;
160                 offset += cfs_size_round(data->ioc_inllen3);
161         }
162
163         if (data->ioc_inllen4) {
164                 data->ioc_inlbuf4 = &data->ioc_bulk[0] + offset;
165         }
166
167         EXIT;
168         return 0;
169 }
170
171 int obd_ioctl_popdata(void *arg, void *data, int len)
172 {
173         int err;
174
175         err = cfs_copy_to_user(arg, data, len);
176         if (err)
177                 err = -EFAULT;
178         return err;
179 }
180
181 EXPORT_SYMBOL(obd_ioctl_getdata);
182 EXPORT_SYMBOL(obd_ioctl_popdata);
183
184 /*  opening /dev/obd */
185 static int obd_class_open(struct inode * inode, struct file * file)
186 {
187         ENTRY;
188
189         PORTAL_MODULE_USE;
190         RETURN(0);
191 }
192
193 /*  closing /dev/obd */
194 static int obd_class_release(struct inode * inode, struct file * file)
195 {
196         ENTRY;
197
198         PORTAL_MODULE_UNUSE;
199         RETURN(0);
200 }
201
202 /* to control /dev/obd */
203 #ifdef HAVE_UNLOCKED_IOCTL
204 static long obd_class_ioctl(struct file *filp, unsigned int cmd,
205                             unsigned long arg)
206 #else
207 static int obd_class_ioctl(struct inode *inode, struct file *filp,
208                            unsigned int cmd, unsigned long arg)
209 #endif
210 {
211         int err = 0;
212         ENTRY;
213
214         /* Allow non-root access for OBD_IOC_PING_TARGET - used by lfs check */
215         if (!cfs_capable(CFS_CAP_SYS_ADMIN) && (cmd != OBD_IOC_PING_TARGET))
216                 RETURN(err = -EACCES);
217         if ((cmd & 0xffffff00) == ((int)'T') << 8) /* ignore all tty ioctls */
218                 RETURN(err = -ENOTTY);
219
220         err = class_handle_ioctl(cmd, (unsigned long)arg);
221
222         RETURN(err);
223 }
224
225 /* declare character device */
226 static struct file_operations obd_psdev_fops = {
227         .owner   = THIS_MODULE,
228 #if HAVE_UNLOCKED_IOCTL
229         .unlocked_ioctl = obd_class_ioctl, /* unlocked_ioctl */
230 #else
231         .ioctl   = obd_class_ioctl,     /* ioctl */
232 #endif
233         .open    = obd_class_open,      /* open */
234         .release = obd_class_release,   /* release */
235 };
236
237 /* modules setup */
238 cfs_psdev_t obd_psdev = {
239         .minor = OBD_DEV_MINOR,
240         .name  = OBD_DEV_NAME,
241         .fops  = &obd_psdev_fops,
242 };
243
244 #endif
245
246 #ifdef LPROCFS
247 int obd_proc_read_version(char *page, char **start, off_t off, int count,
248                           int *eof, void *data)
249 {
250         *eof = 1;
251         return snprintf(page, count, "lustre: %s\nkernel: %s\nbuild:  %s\n",
252                         LUSTRE_VERSION_STRING, "patchless_client",
253                         BUILD_VERSION);
254 }
255
256 int obd_proc_read_pinger(char *page, char **start, off_t off, int count,
257                          int *eof, void *data)
258 {
259         *eof = 1;
260         return snprintf(page, count, "%s\n",
261 #ifdef ENABLE_PINGER
262                         "on"
263 #else
264                         "off"
265 #endif
266                        );
267 }
268
269 /**
270  * Check all obd devices health
271  *
272  * \param page
273  * \param start
274  * \param off
275  * \param count
276  * \param eof
277  * \param data
278  *                  proc read function parameters, please refer to kernel
279  *                  code fs/proc/generic.c proc_file_read()
280  * \param data [in] unused
281  *
282  * \retval number of characters printed
283  */
284 static int obd_proc_read_health(char *page, char **start, off_t off,
285                                 int count, int *eof, void *data)
286 {
287         int rc = 0;
288         struct obd_device *obd;
289
290         *eof = 1;
291         if (libcfs_catastrophe)
292                 rc += snprintf(page + rc, count - rc, "LBUG\n");
293
294         for(obd_devlist_first(&obd);
295             obd != NULL;
296             obd_devlist_next(&obd)) {
297                 if (obd->obd_attached || !obd->obd_set_up)
298                         continue;
299
300                 if (obd->obd_stopping)
301                         continue;
302
303                 if (obd_health_check(obd)) {
304                         rc += snprintf(page + rc, count - rc,
305                                        "device %s reported unhealthy\n",
306                                        obd->obd_name);
307                 }
308         }
309
310         if (rc == 0)
311                 return snprintf(page, count, "healthy\n");
312
313         rc += snprintf(page + rc, count - rc, "NOT HEALTHY\n");
314         return rc;
315 }
316
317 /* Root for /proc/fs/lustre */
318 struct proc_dir_entry *proc_lustre_root = NULL;
319
320 struct lprocfs_vars lprocfs_base[] = {
321         { "version", obd_proc_read_version, NULL, NULL },
322         { "pinger", obd_proc_read_pinger, NULL, NULL },
323         { "health_check", obd_proc_read_health, NULL, NULL },
324         { 0 }
325 };
326 #else
327 #define lprocfs_base NULL
328 #endif /* LPROCFS */
329
330 #ifdef __KERNEL__
331
332 static void *obd_device_list_seq_start(struct seq_file *p, loff_t *pos)
333 {
334         struct obd_device *obd;
335         loff_t i = *pos;
336
337         for(obd_devlist_first(&obd);
338             i != 0 && obd != NULL;
339             obd_devlist_next(&obd)) {
340                 i --;
341         }
342
343         return obd;
344 }
345
346 static void obd_device_list_seq_stop(struct seq_file *p, void *v)
347 {
348         obd_devlist_last(v);
349 }
350
351 static void *obd_device_list_seq_next(struct seq_file *p, void *v, loff_t *pos)
352 {
353         struct obd_device *obd = v;
354
355         ++ *pos;
356         obd_devlist_next(&obd);
357         return obd;
358 }
359
360 static int obd_device_list_seq_show(struct seq_file *p, void *v)
361 {
362         struct obd_device *obd = v;
363         const char *status;
364
365         status = obd_dev_status(obd);
366         return seq_printf(p, "%3d %s %s %s %s %d\n",
367                           obd->obd_minor, status, obd->obd_type->typ_name,
368                           obd->obd_name, obd->obd_uuid.uuid,
369                           cfs_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         int rc;
407         ENTRY;
408
409         obd_sysctl_init();
410         proc_lustre_root = lprocfs_register("fs/lustre", NULL,
411                                             lprocfs_base, NULL);
412         rc = lprocfs_seq_create(proc_lustre_root, "devices", 0444,
413                                 &obd_device_list_fops, NULL);
414         if (rc)
415                 CERROR("error adding /proc/fs/lustre/devices file\n");
416 #else
417         ENTRY;
418 #endif
419         RETURN(0);
420 }
421
422 #ifdef __KERNEL__
423 int class_procfs_clean(void)
424 {
425         ENTRY;
426         if (proc_lustre_root) {
427                 lprocfs_remove(&proc_lustre_root);
428         }
429         RETURN(0);
430 }
431
432
433 /* Check that we're building against the appropriate version of the Lustre
434  * kernel patch */
435 #include <linux/lustre_version.h>
436 #ifdef LUSTRE_KERNEL_VERSION
437 #define LUSTRE_MIN_VERSION 45
438 #define LUSTRE_MAX_VERSION 47
439 #if (LUSTRE_KERNEL_VERSION < LUSTRE_MIN_VERSION)
440 # error Cannot continue: Your Lustre kernel patch is older than the sources
441 #elif (LUSTRE_KERNEL_VERSION > LUSTRE_MAX_VERSION)
442 # error Cannot continue: Your Lustre sources are older than the kernel patch
443 #endif
444 #endif
445 #endif