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