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