Whamcloud - gitweb
LU-1337 build: remove unnecessary includings of system.h
[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 #include <linux/module.h>
47 #include <linux/errno.h>
48 #include <linux/kernel.h>
49 #include <linux/major.h>
50 #include <linux/sched.h>
51 #include <linux/lp.h>
52 #include <linux/slab.h>
53 #include <linux/ioport.h>
54 #include <linux/fcntl.h>
55 #include <linux/delay.h>
56 #include <linux/skbuff.h>
57 #include <linux/proc_fs.h>
58 #include <linux/fs.h>
59 #include <linux/poll.h>
60 #include <linux/init.h>
61 #include <linux/list.h>
62 #include <linux/highmem.h>
63 #include <asm/io.h>
64 #include <asm/ioctls.h>
65 #include <asm/poll.h>
66 #include <asm/uaccess.h>
67 #include <linux/miscdevice.h>
68 #include <linux/seq_file.h>
69 #else
70 # include <liblustre.h>
71 #endif
72
73 #include <libcfs/libcfs.h>
74 #include <obd_support.h>
75 #include <obd_class.h>
76 #include <lnet/lnetctl.h>
77 #include <lprocfs_status.h>
78 #include <lustre_ver.h>
79 #include <lustre/lustre_build_version.h>
80 #ifdef __KERNEL__
81
82 int proc_version;
83
84 /* buffer MUST be at least the size of obd_ioctl_hdr */
85 int obd_ioctl_getdata(char **buf, int *len, void *arg)
86 {
87         struct obd_ioctl_hdr hdr;
88         struct obd_ioctl_data *data;
89         int err;
90         int offset = 0;
91         ENTRY;
92
93         err = cfs_copy_from_user(&hdr, (void *)arg, sizeof(hdr));
94         if ( err )
95                 RETURN(err);
96
97         if (hdr.ioc_version != OBD_IOCTL_VERSION) {
98                 CERROR("Version mismatch kernel (%x) vs application (%x)\n",
99                        OBD_IOCTL_VERSION, hdr.ioc_version);
100                 RETURN(-EINVAL);
101         }
102
103         if (hdr.ioc_len > OBD_MAX_IOCTL_BUFFER) {
104                 CERROR("User buffer len %d exceeds %d max buffer\n",
105                        hdr.ioc_len, OBD_MAX_IOCTL_BUFFER);
106                 RETURN(-EINVAL);
107         }
108
109         if (hdr.ioc_len < sizeof(struct obd_ioctl_data)) {
110                 CERROR("User buffer too small for ioctl (%d)\n", hdr.ioc_len);
111                 RETURN(-EINVAL);
112         }
113
114         /* When there are lots of processes calling vmalloc on multi-core
115          * system, the high lock contention will hurt performance badly,
116          * obdfilter-survey is an example, which relies on ioctl. So we'd
117          * better avoid vmalloc on ioctl path. LU-66 */
118         OBD_ALLOC_LARGE(*buf, hdr.ioc_len);
119         if (*buf == NULL) {
120                 CERROR("Cannot allocate control buffer of len %d\n",
121                        hdr.ioc_len);
122                 RETURN(-EINVAL);
123         }
124         *len = hdr.ioc_len;
125         data = (struct obd_ioctl_data *)*buf;
126
127         err = cfs_copy_from_user(*buf, (void *)arg, hdr.ioc_len);
128         if ( err ) {
129                 OBD_FREE_LARGE(*buf, hdr.ioc_len);
130                 RETURN(err);
131         }
132
133         if (obd_ioctl_is_invalid(data)) {
134                 CERROR("ioctl not correctly formatted\n");
135                 OBD_FREE_LARGE(*buf, hdr.ioc_len);
136                 RETURN(-EINVAL);
137         }
138
139         if (data->ioc_inllen1) {
140                 data->ioc_inlbuf1 = &data->ioc_bulk[0];
141                 offset += cfs_size_round(data->ioc_inllen1);
142         }
143
144         if (data->ioc_inllen2) {
145                 data->ioc_inlbuf2 = &data->ioc_bulk[0] + offset;
146                 offset += cfs_size_round(data->ioc_inllen2);
147         }
148
149         if (data->ioc_inllen3) {
150                 data->ioc_inlbuf3 = &data->ioc_bulk[0] + offset;
151                 offset += cfs_size_round(data->ioc_inllen3);
152         }
153
154         if (data->ioc_inllen4) {
155                 data->ioc_inlbuf4 = &data->ioc_bulk[0] + offset;
156         }
157
158         EXIT;
159         return 0;
160 }
161 EXPORT_SYMBOL(obd_ioctl_getdata);
162
163 int obd_ioctl_popdata(void *arg, void *data, int len)
164 {
165         int err;
166
167         err = cfs_copy_to_user(arg, data, len);
168         if (err)
169                 err = -EFAULT;
170         return err;
171 }
172 EXPORT_SYMBOL(obd_ioctl_popdata);
173
174 /*  opening /dev/obd */
175 static int obd_class_open(struct inode * inode, struct file * file)
176 {
177         ENTRY;
178
179         PORTAL_MODULE_USE;
180         RETURN(0);
181 }
182
183 /*  closing /dev/obd */
184 static int obd_class_release(struct inode * inode, struct file * file)
185 {
186         ENTRY;
187
188         PORTAL_MODULE_UNUSE;
189         RETURN(0);
190 }
191
192 /* to control /dev/obd */
193 static long obd_class_ioctl(struct file *filp, unsigned int cmd,
194                             unsigned long arg)
195 {
196         int err = 0;
197         ENTRY;
198
199         /* Allow non-root access for OBD_IOC_PING_TARGET - used by lfs check */
200         if (!cfs_capable(CFS_CAP_SYS_ADMIN) && (cmd != OBD_IOC_PING_TARGET))
201                 RETURN(err = -EACCES);
202         if ((cmd & 0xffffff00) == ((int)'T') << 8) /* ignore all tty ioctls */
203                 RETURN(err = -ENOTTY);
204
205         err = class_handle_ioctl(cmd, (unsigned long)arg);
206
207         RETURN(err);
208 }
209
210 /* declare character device */
211 static struct file_operations obd_psdev_fops = {
212         .owner          = THIS_MODULE,
213         .unlocked_ioctl = obd_class_ioctl, /* unlocked_ioctl */
214         .open           = obd_class_open,      /* open */
215         .release        = obd_class_release,   /* release */
216 };
217
218 /* modules setup */
219 cfs_psdev_t obd_psdev = {
220         .minor = OBD_DEV_MINOR,
221         .name  = OBD_DEV_NAME,
222         .fops  = &obd_psdev_fops,
223 };
224
225 #endif
226
227 #ifdef LPROCFS
228 int obd_proc_read_version(char *page, char **start, off_t off, int count,
229                           int *eof, void *data)
230 {
231         *eof = 1;
232         return snprintf(page, count, "lustre: %s\nkernel: %s\nbuild:  %s\n",
233                         LUSTRE_VERSION_STRING, "patchless_client",
234                         BUILD_VERSION);
235 }
236
237 int obd_proc_read_pinger(char *page, char **start, off_t off, int count,
238                          int *eof, void *data)
239 {
240         *eof = 1;
241         return snprintf(page, count, "%s\n",
242 #ifdef ENABLE_PINGER
243                         "on"
244 #else
245                         "off"
246 #endif
247                        );
248 }
249
250 /**
251  * Check all obd devices health
252  *
253  * \param page
254  * \param start
255  * \param off
256  * \param count
257  * \param eof
258  * \param data
259  *                  proc read function parameters, please refer to kernel
260  *                  code fs/proc/generic.c proc_file_read()
261  * \param data [in] unused
262  *
263  * \retval number of characters printed
264  */
265 static int obd_proc_read_health(char *page, char **start, off_t off,
266                                 int count, int *eof, void *data)
267 {
268         int rc = 0, i;
269         *eof = 1;
270
271         if (libcfs_catastrophe)
272                 rc += snprintf(page + rc, count - rc, "LBUG\n");
273
274         cfs_read_lock(&obd_dev_lock);
275         for (i = 0; i < class_devno_max(); i++) {
276                 struct obd_device *obd;
277
278                 obd = class_num2obd(i);
279                 if (obd == NULL || !obd->obd_attached || !obd->obd_set_up)
280                         continue;
281
282                 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
283                 if (obd->obd_stopping)
284                         continue;
285
286                 class_incref(obd, __FUNCTION__, cfs_current());
287                 cfs_read_unlock(&obd_dev_lock);
288
289                 if (obd_health_check(NULL, obd)) {
290                         rc += snprintf(page + rc, count - rc,
291                                        "device %s reported unhealthy\n",
292                                        obd->obd_name);
293                 }
294                 class_decref(obd, __FUNCTION__, cfs_current());
295                 cfs_read_lock(&obd_dev_lock);
296         }
297         cfs_read_unlock(&obd_dev_lock);
298
299         if (rc == 0)
300                 return snprintf(page, count, "healthy\n");
301
302         rc += snprintf(page + rc, count - rc, "NOT HEALTHY\n");
303         return rc;
304 }
305
306 static int obd_proc_rd_jobid_var(char *page, char **start, off_t off,
307                                 int count, int *eof, void *data)
308 {
309         return snprintf(page, count, "%s\n", obd_jobid_var);
310 }
311
312 static int obd_proc_wr_jobid_var(struct file *file, const char *buffer,
313                                 unsigned long count, void *data)
314 {
315         if (!count || count > JOBSTATS_JOBID_VAR_MAX_LEN)
316                 return -EINVAL;
317
318         memset(obd_jobid_var, 0, JOBSTATS_JOBID_VAR_MAX_LEN + 1);
319         /* Trim the trailing '\n' if any */
320         memcpy(obd_jobid_var, buffer, count - (buffer[count - 1] == '\n'));
321         return count;
322 }
323
324 /* Root for /proc/fs/lustre */
325 struct proc_dir_entry *proc_lustre_root = NULL;
326 EXPORT_SYMBOL(proc_lustre_root);
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         { "jobid_var", obd_proc_rd_jobid_var,
333                        obd_proc_wr_jobid_var, 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 #endif