Whamcloud - gitweb
LU-1347 style: removes obsolete EXPORT_SYMTAB macros
[fs/lustre-release.git] / lustre / obdclass / linux / linux-module.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, Whamcloud, Inc.
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
45 #ifdef __KERNEL__
46 #ifndef AUTOCONF_INCLUDED
47 #include <linux/config.h> /* for CONFIG_PROC_FS */
48 #endif
49 #include <linux/module.h>
50 #include <linux/errno.h>
51 #include <linux/kernel.h>
52 #include <linux/major.h>
53 #include <linux/sched.h>
54 #include <linux/lp.h>
55 #include <linux/slab.h>
56 #include <linux/ioport.h>
57 #include <linux/fcntl.h>
58 #include <linux/delay.h>
59 #include <linux/skbuff.h>
60 #include <linux/proc_fs.h>
61 #include <linux/fs.h>
62 #include <linux/poll.h>
63 #include <linux/init.h>
64 #include <linux/list.h>
65 #include <linux/highmem.h>
66 #include <asm/io.h>
67 #include <asm/ioctls.h>
68 #include <asm/system.h>
69 #include <asm/poll.h>
70 #include <asm/uaccess.h>
71 #include <linux/miscdevice.h>
72 #include <linux/smp_lock.h>
73 #include <linux/seq_file.h>
74 #else
75 # include <liblustre.h>
76 #endif
77
78 #include <libcfs/libcfs.h>
79 #include <obd_support.h>
80 #include <obd_class.h>
81 #include <lnet/lnetctl.h>
82 #include <lprocfs_status.h>
83 #include <lustre_ver.h>
84 #include <lustre/lustre_build_version.h>
85 #ifdef __KERNEL__
86
87 int proc_version;
88
89 /* buffer MUST be at least the size of obd_ioctl_hdr */
90 int obd_ioctl_getdata(char **buf, int *len, void *arg)
91 {
92         struct obd_ioctl_hdr hdr;
93         struct obd_ioctl_data *data;
94         int err;
95         int offset = 0;
96         ENTRY;
97
98         err = cfs_copy_from_user(&hdr, (void *)arg, sizeof(hdr));
99         if ( err )
100                 RETURN(err);
101
102         if (hdr.ioc_version != OBD_IOCTL_VERSION) {
103                 CERROR("Version mismatch kernel (%x) vs application (%x)\n",
104                        OBD_IOCTL_VERSION, hdr.ioc_version);
105                 RETURN(-EINVAL);
106         }
107
108         if (hdr.ioc_len > OBD_MAX_IOCTL_BUFFER) {
109                 CERROR("User buffer len %d exceeds %d max buffer\n",
110                        hdr.ioc_len, OBD_MAX_IOCTL_BUFFER);
111                 RETURN(-EINVAL);
112         }
113
114         if (hdr.ioc_len < sizeof(struct obd_ioctl_data)) {
115                 CERROR("User buffer too small for ioctl (%d)\n", hdr.ioc_len);
116                 RETURN(-EINVAL);
117         }
118
119         /* When there are lots of processes calling vmalloc on multi-core
120          * system, the high lock contention will hurt performance badly,
121          * obdfilter-survey is an example, which relies on ioctl. So we'd
122          * better avoid vmalloc on ioctl path. LU-66 */
123         OBD_ALLOC_LARGE(*buf, hdr.ioc_len);
124         if (*buf == NULL) {
125                 CERROR("Cannot allocate control buffer of len %d\n",
126                        hdr.ioc_len);
127                 RETURN(-EINVAL);
128         }
129         *len = hdr.ioc_len;
130         data = (struct obd_ioctl_data *)*buf;
131
132         err = cfs_copy_from_user(*buf, (void *)arg, hdr.ioc_len);
133         if ( err ) {
134                 OBD_FREE_LARGE(*buf, hdr.ioc_len);
135                 RETURN(err);
136         }
137
138         if (obd_ioctl_is_invalid(data)) {
139                 CERROR("ioctl not correctly formatted\n");
140                 OBD_FREE_LARGE(*buf, hdr.ioc_len);
141                 RETURN(-EINVAL);
142         }
143
144         if (data->ioc_inllen1) {
145                 data->ioc_inlbuf1 = &data->ioc_bulk[0];
146                 offset += cfs_size_round(data->ioc_inllen1);
147         }
148
149         if (data->ioc_inllen2) {
150                 data->ioc_inlbuf2 = &data->ioc_bulk[0] + offset;
151                 offset += cfs_size_round(data->ioc_inllen2);
152         }
153
154         if (data->ioc_inllen3) {
155                 data->ioc_inlbuf3 = &data->ioc_bulk[0] + offset;
156                 offset += cfs_size_round(data->ioc_inllen3);
157         }
158
159         if (data->ioc_inllen4) {
160                 data->ioc_inlbuf4 = &data->ioc_bulk[0] + offset;
161         }
162
163         EXIT;
164         return 0;
165 }
166
167 int obd_ioctl_popdata(void *arg, void *data, int len)
168 {
169         int err;
170
171         err = cfs_copy_to_user(arg, data, len);
172         if (err)
173                 err = -EFAULT;
174         return err;
175 }
176
177 EXPORT_SYMBOL(obd_ioctl_getdata);
178 EXPORT_SYMBOL(obd_ioctl_popdata);
179
180 /*  opening /dev/obd */
181 static int obd_class_open(struct inode * inode, struct file * file)
182 {
183         ENTRY;
184
185         PORTAL_MODULE_USE;
186         RETURN(0);
187 }
188
189 /*  closing /dev/obd */
190 static int obd_class_release(struct inode * inode, struct file * file)
191 {
192         ENTRY;
193
194         PORTAL_MODULE_UNUSE;
195         RETURN(0);
196 }
197
198 /* to control /dev/obd */
199 #ifdef HAVE_UNLOCKED_IOCTL
200 static long obd_class_ioctl(struct file *filp, unsigned int cmd,
201                             unsigned long arg)
202 #else
203 static int obd_class_ioctl(struct inode *inode, struct file *filp,
204                            unsigned int cmd, unsigned long arg)
205 #endif
206 {
207         int err = 0;
208         ENTRY;
209
210         /* Allow non-root access for OBD_IOC_PING_TARGET - used by lfs check */
211         if (!cfs_capable(CFS_CAP_SYS_ADMIN) && (cmd != OBD_IOC_PING_TARGET))
212                 RETURN(err = -EACCES);
213         if ((cmd & 0xffffff00) == ((int)'T') << 8) /* ignore all tty ioctls */
214                 RETURN(err = -ENOTTY);
215
216         err = class_handle_ioctl(cmd, (unsigned long)arg);
217
218         RETURN(err);
219 }
220
221 /* declare character device */
222 static struct file_operations obd_psdev_fops = {
223         .owner   = THIS_MODULE,
224 #if HAVE_UNLOCKED_IOCTL
225         .unlocked_ioctl = obd_class_ioctl, /* unlocked_ioctl */
226 #else
227         .ioctl   = obd_class_ioctl,     /* ioctl */
228 #endif
229         .open    = obd_class_open,      /* open */
230         .release = obd_class_release,   /* release */
231 };
232
233 /* modules setup */
234 cfs_psdev_t obd_psdev = {
235         .minor = OBD_DEV_MINOR,
236         .name  = OBD_DEV_NAME,
237         .fops  = &obd_psdev_fops,
238 };
239
240 #endif
241
242 #ifdef LPROCFS
243 int obd_proc_read_version(char *page, char **start, off_t off, int count,
244                           int *eof, void *data)
245 {
246         *eof = 1;
247         return snprintf(page, count, "lustre: %s\nkernel: %s\nbuild:  %s\n",
248                         LUSTRE_VERSION_STRING, "patchless_client",
249                         BUILD_VERSION);
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 /**
266  * Check all obd devices health
267  *
268  * \param page
269  * \param start
270  * \param off
271  * \param count
272  * \param eof
273  * \param data
274  *                  proc read function parameters, please refer to kernel
275  *                  code fs/proc/generic.c proc_file_read()
276  * \param data [in] unused
277  *
278  * \retval number of characters printed
279  */
280 static int obd_proc_read_health(char *page, char **start, off_t off,
281                                 int count, int *eof, void *data)
282 {
283         int rc = 0, i;
284         *eof = 1;
285
286         if (libcfs_catastrophe)
287                 rc += snprintf(page + rc, count - rc, "LBUG\n");
288
289         cfs_read_lock(&obd_dev_lock);
290         for (i = 0; i < class_devno_max(); i++) {
291                 struct obd_device *obd;
292
293                 obd = class_num2obd(i);
294                 if (obd == NULL || !obd->obd_attached || !obd->obd_set_up)
295                         continue;
296
297                 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
298                 if (obd->obd_stopping)
299                         continue;
300
301                 class_incref(obd, __FUNCTION__, cfs_current());
302                 cfs_read_unlock(&obd_dev_lock);
303
304                 if (obd_health_check(NULL, obd)) {
305                         rc += snprintf(page + rc, count - rc,
306                                        "device %s reported unhealthy\n",
307                                        obd->obd_name);
308                 }
309                 class_decref(obd, __FUNCTION__, cfs_current());
310                 cfs_read_lock(&obd_dev_lock);
311         }
312         cfs_read_unlock(&obd_dev_lock);
313
314         if (rc == 0)
315                 return snprintf(page, count, "healthy\n");
316
317         rc += snprintf(page + rc, count - rc, "NOT HEALTHY\n");
318         return rc;
319 }
320
321 static int obd_proc_rd_jobid_var(char *page, char **start, off_t off,
322                                 int count, int *eof, void *data)
323 {
324         return snprintf(page, count, "%s\n", obd_jobid_var);
325 }
326
327 static int obd_proc_wr_jobid_var(struct file *file, const char *buffer,
328                                 unsigned long count, void *data)
329 {
330         if (!count || count > JOBSTATS_JOBID_VAR_MAX_LEN)
331                 return -EINVAL;
332
333         memset(obd_jobid_var, 0, JOBSTATS_JOBID_VAR_MAX_LEN + 1);
334         /* Trim the trailing '\n' if any */
335         memcpy(obd_jobid_var, buffer, count - (buffer[count - 1] == '\n'));
336         return count;
337 }
338
339 /* Root for /proc/fs/lustre */
340 struct proc_dir_entry *proc_lustre_root = NULL;
341
342 struct lprocfs_vars lprocfs_base[] = {
343         { "version", obd_proc_read_version, NULL, NULL },
344         { "pinger", obd_proc_read_pinger, NULL, NULL },
345         { "health_check", obd_proc_read_health, NULL, NULL },
346         { "jobid_var", obd_proc_rd_jobid_var,
347                        obd_proc_wr_jobid_var, NULL },
348         { 0 }
349 };
350 #else
351 #define lprocfs_base NULL
352 #endif /* LPROCFS */
353
354 #ifdef __KERNEL__
355 static void *obd_device_list_seq_start(struct seq_file *p, loff_t *pos)
356 {
357         if (*pos >= class_devno_max())
358                 return NULL;
359
360         return pos;
361 }
362
363 static void obd_device_list_seq_stop(struct seq_file *p, void *v)
364 {
365 }
366
367 static void *obd_device_list_seq_next(struct seq_file *p, void *v, loff_t *pos)
368 {
369         ++*pos;
370         if (*pos >= class_devno_max())
371                 return NULL;
372
373         return pos;
374 }
375
376 static int obd_device_list_seq_show(struct seq_file *p, void *v)
377 {
378         loff_t index = *(loff_t *)v;
379         struct obd_device *obd = class_num2obd((int)index);
380         char *status;
381
382         if (obd == NULL)
383                 return 0;
384
385         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
386         if (obd->obd_stopping)
387                 status = "ST";
388         else if (obd->obd_inactive)
389                 status = "IN";
390         else if (obd->obd_set_up)
391                 status = "UP";
392         else if (obd->obd_attached)
393                 status = "AT";
394         else
395                 status = "--";
396
397         return seq_printf(p, "%3d %s %s %s %s %d\n",
398                           (int)index, status, obd->obd_type->typ_name,
399                           obd->obd_name, obd->obd_uuid.uuid,
400                           cfs_atomic_read(&obd->obd_refcount));
401 }
402
403 struct seq_operations obd_device_list_sops = {
404         .start = obd_device_list_seq_start,
405         .stop = obd_device_list_seq_stop,
406         .next = obd_device_list_seq_next,
407         .show = obd_device_list_seq_show,
408 };
409
410 static int obd_device_list_open(struct inode *inode, struct file *file)
411 {
412         struct proc_dir_entry *dp = PDE(inode);
413         struct seq_file *seq;
414         int rc = seq_open(file, &obd_device_list_sops);
415
416         if (rc)
417                 return rc;
418
419         seq = file->private_data;
420         seq->private = dp->data;
421
422         return 0;
423 }
424
425 struct file_operations obd_device_list_fops = {
426         .owner   = THIS_MODULE,
427         .open    = obd_device_list_open,
428         .read    = seq_read,
429         .llseek  = seq_lseek,
430         .release = seq_release,
431 };
432 #endif
433
434 int class_procfs_init(void)
435 {
436 #ifdef __KERNEL__
437         int rc;
438         ENTRY;
439
440         obd_sysctl_init();
441         proc_lustre_root = lprocfs_register("fs/lustre", NULL,
442                                             lprocfs_base, NULL);
443         rc = lprocfs_seq_create(proc_lustre_root, "devices", 0444,
444                                 &obd_device_list_fops, NULL);
445         if (rc)
446                 CERROR("error adding /proc/fs/lustre/devices file\n");
447 #else
448         ENTRY;
449 #endif
450         RETURN(0);
451 }
452
453 #ifdef __KERNEL__
454 int class_procfs_clean(void)
455 {
456         ENTRY;
457         if (proc_lustre_root) {
458                 lprocfs_remove(&proc_lustre_root);
459         }
460         RETURN(0);
461 }
462 #endif