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