Whamcloud - gitweb
LU-1146 build: batch update copyright messages
[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  * Copyright (c) 2011, Whamcloud, Inc.
33  */
34 /*
35  * This file is part of Lustre, http://www.lustre.org/
36  * Lustre is a trademark of Sun Microsystems, Inc.
37  *
38  * lustre/obdclass/linux/linux-module.c
39  *
40  * Object Devices Class Driver
41  * These are the only exported functions, they provide some generic
42  * infrastructure for managing object devices
43  */
44
45 #define DEBUG_SUBSYSTEM S_CLASS
46 #ifndef EXPORT_SYMTAB
47 # define EXPORT_SYMTAB
48 #endif
49
50 #ifdef __KERNEL__
51 #ifndef AUTOCONF_INCLUDED
52 #include <linux/config.h> /* for CONFIG_PROC_FS */
53 #endif
54 #include <linux/module.h>
55 #include <linux/errno.h>
56 #include <linux/kernel.h>
57 #include <linux/major.h>
58 #include <linux/sched.h>
59 #include <linux/lp.h>
60 #include <linux/slab.h>
61 #include <linux/ioport.h>
62 #include <linux/fcntl.h>
63 #include <linux/delay.h>
64 #include <linux/skbuff.h>
65 #include <linux/proc_fs.h>
66 #include <linux/fs.h>
67 #include <linux/poll.h>
68 #include <linux/init.h>
69 #include <linux/list.h>
70 #include <linux/highmem.h>
71 #include <asm/io.h>
72 #include <asm/ioctls.h>
73 #include <asm/system.h>
74 #include <asm/poll.h>
75 #include <asm/uaccess.h>
76 #include <linux/miscdevice.h>
77 #include <linux/smp_lock.h>
78 #include <linux/seq_file.h>
79 #else
80 # include <liblustre.h>
81 #endif
82
83 #include <libcfs/libcfs.h>
84 #include <obd_support.h>
85 #include <obd_class.h>
86 #include <lnet/lnetctl.h>
87 #include <lprocfs_status.h>
88 #include <lustre_ver.h>
89 #include <lustre/lustre_build_version.h>
90 #ifdef __KERNEL__
91 #include <linux/lustre_version.h>
92
93 int proc_version;
94
95 /* buffer MUST be at least the size of obd_ioctl_hdr */
96 int obd_ioctl_getdata(char **buf, int *len, void *arg)
97 {
98         struct obd_ioctl_hdr hdr;
99         struct obd_ioctl_data *data;
100         int err;
101         int offset = 0;
102         ENTRY;
103
104         err = cfs_copy_from_user(&hdr, (void *)arg, sizeof(hdr));
105         if ( err )
106                 RETURN(err);
107
108         if (hdr.ioc_version != OBD_IOCTL_VERSION) {
109                 CERROR("Version mismatch kernel (%x) vs application (%x)\n",
110                        OBD_IOCTL_VERSION, hdr.ioc_version);
111                 RETURN(-EINVAL);
112         }
113
114         if (hdr.ioc_len > OBD_MAX_IOCTL_BUFFER) {
115                 CERROR("User buffer len %d exceeds %d max buffer\n",
116                        hdr.ioc_len, OBD_MAX_IOCTL_BUFFER);
117                 RETURN(-EINVAL);
118         }
119
120         if (hdr.ioc_len < sizeof(struct obd_ioctl_data)) {
121                 CERROR("User buffer too small for ioctl (%d)\n", hdr.ioc_len);
122                 RETURN(-EINVAL);
123         }
124
125         /* When there are lots of processes calling vmalloc on multi-core
126          * system, the high lock contention will hurt performance badly,
127          * obdfilter-survey is an example, which relies on ioctl. So we'd
128          * better avoid vmalloc on ioctl path. LU-66 */
129         OBD_ALLOC_LARGE(*buf, hdr.ioc_len);
130         if (*buf == NULL) {
131                 CERROR("Cannot allocate control buffer of len %d\n",
132                        hdr.ioc_len);
133                 RETURN(-EINVAL);
134         }
135         *len = hdr.ioc_len;
136         data = (struct obd_ioctl_data *)*buf;
137
138         err = cfs_copy_from_user(*buf, (void *)arg, hdr.ioc_len);
139         if ( err ) {
140                 OBD_FREE_LARGE(*buf, hdr.ioc_len);
141                 RETURN(err);
142         }
143
144         if (obd_ioctl_is_invalid(data)) {
145                 CERROR("ioctl not correctly formatted\n");
146                 OBD_FREE_LARGE(*buf, hdr.ioc_len);
147                 RETURN(-EINVAL);
148         }
149
150         if (data->ioc_inllen1) {
151                 data->ioc_inlbuf1 = &data->ioc_bulk[0];
152                 offset += cfs_size_round(data->ioc_inllen1);
153         }
154
155         if (data->ioc_inllen2) {
156                 data->ioc_inlbuf2 = &data->ioc_bulk[0] + offset;
157                 offset += cfs_size_round(data->ioc_inllen2);
158         }
159
160         if (data->ioc_inllen3) {
161                 data->ioc_inlbuf3 = &data->ioc_bulk[0] + offset;
162                 offset += cfs_size_round(data->ioc_inllen3);
163         }
164
165         if (data->ioc_inllen4) {
166                 data->ioc_inlbuf4 = &data->ioc_bulk[0] + offset;
167         }
168
169         EXIT;
170         return 0;
171 }
172
173 int obd_ioctl_popdata(void *arg, void *data, int len)
174 {
175         int err;
176
177         err = cfs_copy_to_user(arg, data, len);
178         if (err)
179                 err = -EFAULT;
180         return err;
181 }
182
183 EXPORT_SYMBOL(obd_ioctl_getdata);
184 EXPORT_SYMBOL(obd_ioctl_popdata);
185
186 /*  opening /dev/obd */
187 static int obd_class_open(struct inode * inode, struct file * file)
188 {
189         ENTRY;
190
191         PORTAL_MODULE_USE;
192         RETURN(0);
193 }
194
195 /*  closing /dev/obd */
196 static int obd_class_release(struct inode * inode, struct file * file)
197 {
198         ENTRY;
199
200         PORTAL_MODULE_UNUSE;
201         RETURN(0);
202 }
203
204 /* to control /dev/obd */
205 #ifdef HAVE_UNLOCKED_IOCTL
206 static long obd_class_ioctl(struct file *filp, unsigned int cmd,
207                             unsigned long arg)
208 #else
209 static int obd_class_ioctl(struct inode *inode, struct file *filp,
210                            unsigned int cmd, unsigned long arg)
211 #endif
212 {
213         int err = 0;
214         ENTRY;
215
216         /* Allow non-root access for OBD_IOC_PING_TARGET - used by lfs check */
217         if (!cfs_capable(CFS_CAP_SYS_ADMIN) && (cmd != OBD_IOC_PING_TARGET))
218                 RETURN(err = -EACCES);
219         if ((cmd & 0xffffff00) == ((int)'T') << 8) /* ignore all tty ioctls */
220                 RETURN(err = -ENOTTY);
221
222         err = class_handle_ioctl(cmd, (unsigned long)arg);
223
224         RETURN(err);
225 }
226
227 /* declare character device */
228 static struct file_operations obd_psdev_fops = {
229         .owner   = THIS_MODULE,
230 #if HAVE_UNLOCKED_IOCTL
231         .unlocked_ioctl = obd_class_ioctl, /* unlocked_ioctl */
232 #else
233         .ioctl   = obd_class_ioctl,     /* ioctl */
234 #endif
235         .open    = obd_class_open,      /* open */
236         .release = obd_class_release,   /* release */
237 };
238
239 /* modules setup */
240 cfs_psdev_t obd_psdev = {
241         .minor = OBD_DEV_MINOR,
242         .name  = OBD_DEV_NAME,
243         .fops  = &obd_psdev_fops,
244 };
245
246 #endif
247
248 #ifdef LPROCFS
249 int obd_proc_read_version(char *page, char **start, off_t off, int count,
250                           int *eof, void *data)
251 {
252         *eof = 1;
253         return snprintf(page, count, "lustre: %s\nkernel: %s\nbuild:  %s\n",
254                         LUSTRE_VERSION_STRING, "patchless_client",
255                         BUILD_VERSION);
256 }
257
258 int obd_proc_read_pinger(char *page, char **start, off_t off, int count,
259                          int *eof, void *data)
260 {
261         *eof = 1;
262         return snprintf(page, count, "%s\n",
263 #ifdef ENABLE_PINGER
264                         "on"
265 #else
266                         "off"
267 #endif
268                        );
269 }
270
271 /**
272  * Check all obd devices health
273  *
274  * \param page
275  * \param start
276  * \param off
277  * \param count
278  * \param eof
279  * \param data
280  *                  proc read function parameters, please refer to kernel
281  *                  code fs/proc/generic.c proc_file_read()
282  * \param data [in] unused
283  *
284  * \retval number of characters printed
285  */
286 static int obd_proc_read_health(char *page, char **start, off_t off,
287                                 int count, int *eof, void *data)
288 {
289         int rc = 0, i;
290         *eof = 1;
291
292         if (libcfs_catastrophe)
293                 rc += snprintf(page + rc, count - rc, "LBUG\n");
294
295         cfs_read_lock(&obd_dev_lock);
296         for (i = 0; i < class_devno_max(); i++) {
297                 struct obd_device *obd;
298
299                 obd = class_num2obd(i);
300                 if (obd == NULL || !obd->obd_attached || !obd->obd_set_up)
301                         continue;
302
303                 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
304                 if (obd->obd_stopping)
305                         continue;
306
307                 class_incref(obd, __FUNCTION__, cfs_current());
308                 cfs_read_unlock(&obd_dev_lock);
309
310                 if (obd_health_check(obd)) {
311                         rc += snprintf(page + rc, count - rc,
312                                        "device %s reported unhealthy\n",
313                                        obd->obd_name);
314                 }
315                 class_decref(obd, __FUNCTION__, cfs_current());
316                 cfs_read_lock(&obd_dev_lock);
317         }
318         cfs_read_unlock(&obd_dev_lock);
319
320         if (rc == 0)
321                 return snprintf(page, count, "healthy\n");
322
323         rc += snprintf(page + rc, count - rc, "NOT HEALTHY\n");
324         return rc;
325 }
326
327 /* Root for /proc/fs/lustre */
328 struct proc_dir_entry *proc_lustre_root = NULL;
329
330 struct lprocfs_vars lprocfs_base[] = {
331         { "version", obd_proc_read_version, NULL, NULL },
332         { "pinger", obd_proc_read_pinger, NULL, NULL },
333         { "health_check", obd_proc_read_health, NULL, NULL },
334         { 0 }
335 };
336 #else
337 #define lprocfs_base NULL
338 #endif /* LPROCFS */
339
340 #ifdef __KERNEL__
341 static void *obd_device_list_seq_start(struct seq_file *p, loff_t *pos)
342 {
343         if (*pos >= class_devno_max())
344                 return NULL;
345
346         return pos;
347 }
348
349 static void obd_device_list_seq_stop(struct seq_file *p, void *v)
350 {
351 }
352
353 static void *obd_device_list_seq_next(struct seq_file *p, void *v, loff_t *pos)
354 {
355         ++*pos;
356         if (*pos >= class_devno_max())
357                 return NULL;
358
359         return pos;
360 }
361
362 static int obd_device_list_seq_show(struct seq_file *p, void *v)
363 {
364         loff_t index = *(loff_t *)v;
365         struct obd_device *obd = class_num2obd((int)index);
366         char *status;
367
368         if (obd == NULL)
369                 return 0;
370
371         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
372         if (obd->obd_stopping)
373                 status = "ST";
374         else if (obd->obd_inactive)
375                 status = "IN";
376         else if (obd->obd_set_up)
377                 status = "UP";
378         else if (obd->obd_attached)
379                 status = "AT";
380         else
381                 status = "--";
382
383         return seq_printf(p, "%3d %s %s %s %s %d\n",
384                           (int)index, status, obd->obd_type->typ_name,
385                           obd->obd_name, obd->obd_uuid.uuid,
386                           cfs_atomic_read(&obd->obd_refcount));
387 }
388
389 struct seq_operations obd_device_list_sops = {
390         .start = obd_device_list_seq_start,
391         .stop = obd_device_list_seq_stop,
392         .next = obd_device_list_seq_next,
393         .show = obd_device_list_seq_show,
394 };
395
396 static int obd_device_list_open(struct inode *inode, struct file *file)
397 {
398         struct proc_dir_entry *dp = PDE(inode);
399         struct seq_file *seq;
400         int rc = seq_open(file, &obd_device_list_sops);
401
402         if (rc)
403                 return rc;
404
405         seq = file->private_data;
406         seq->private = dp->data;
407
408         return 0;
409 }
410
411 struct file_operations obd_device_list_fops = {
412         .owner   = THIS_MODULE,
413         .open    = obd_device_list_open,
414         .read    = seq_read,
415         .llseek  = seq_lseek,
416         .release = seq_release,
417 };
418 #endif
419
420 int class_procfs_init(void)
421 {
422 #ifdef __KERNEL__
423         int rc;
424         ENTRY;
425
426         obd_sysctl_init();
427         proc_lustre_root = lprocfs_register("fs/lustre", NULL,
428                                             lprocfs_base, NULL);
429         rc = lprocfs_seq_create(proc_lustre_root, "devices", 0444,
430                                 &obd_device_list_fops, NULL);
431         if (rc)
432                 CERROR("error adding /proc/fs/lustre/devices file\n");
433 #else
434         ENTRY;
435 #endif
436         RETURN(0);
437 }
438
439 #ifdef __KERNEL__
440 int class_procfs_clean(void)
441 {
442         ENTRY;
443         if (proc_lustre_root) {
444                 lprocfs_remove(&proc_lustre_root);
445         }
446         RETURN(0);
447 }
448
449
450 /* Check that we're building against the appropriate version of the Lustre
451  * kernel patch */
452 #include <linux/lustre_version.h>
453 #ifdef LUSTRE_KERNEL_VERSION
454 #define LUSTRE_MIN_VERSION 45
455 #define LUSTRE_MAX_VERSION 47
456 #if (LUSTRE_KERNEL_VERSION < LUSTRE_MIN_VERSION)
457 # error Cannot continue: Your Lustre kernel patch is older than the sources
458 #elif (LUSTRE_KERNEL_VERSION > LUSTRE_MAX_VERSION)
459 # error Cannot continue: Your Lustre sources are older than the kernel patch
460 #endif
461 #endif
462 #endif