Whamcloud - gitweb
0d7f65623613327e09332f5c30bc9fc6d7d88380
[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, 2012, Intel Corporation.
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_ioctl.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 = 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 = 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 EXPORT_SYMBOL(obd_ioctl_getdata);
163
164 int obd_ioctl_popdata(void *arg, void *data, int len)
165 {
166         int err;
167
168         err = copy_to_user(arg, data, len);
169         if (err)
170                 err = -EFAULT;
171         return err;
172 }
173 EXPORT_SYMBOL(obd_ioctl_popdata);
174
175 /*  opening /dev/obd */
176 static int obd_class_open(struct inode * inode, struct file * file)
177 {
178         ENTRY;
179
180         try_module_get(THIS_MODULE);
181         RETURN(0);
182 }
183
184 /*  closing /dev/obd */
185 static int obd_class_release(struct inode * inode, struct file * file)
186 {
187         ENTRY;
188
189         module_put(THIS_MODULE);
190         RETURN(0);
191 }
192
193 /* to control /dev/obd */
194 static long obd_class_ioctl(struct file *filp, unsigned int cmd,
195                             unsigned long arg)
196 {
197         int err = 0;
198         ENTRY;
199
200         /* Allow non-root access for OBD_IOC_PING_TARGET - used by lfs check */
201         if (!cfs_capable(CFS_CAP_SYS_ADMIN) && (cmd != OBD_IOC_PING_TARGET))
202                 RETURN(err = -EACCES);
203         if ((cmd & 0xffffff00) == ((int)'T') << 8) /* ignore all tty ioctls */
204                 RETURN(err = -ENOTTY);
205
206         err = class_handle_ioctl(cmd, (unsigned long)arg);
207
208         RETURN(err);
209 }
210
211 /* declare character device */
212 static struct file_operations obd_psdev_fops = {
213         .owner          = THIS_MODULE,
214         .unlocked_ioctl = obd_class_ioctl, /* unlocked_ioctl */
215         .open           = obd_class_open,      /* open */
216         .release        = obd_class_release,   /* release */
217 };
218
219 /* modules setup */
220 struct miscdevice obd_psdev = {
221         .minor = OBD_DEV_MINOR,
222         .name  = OBD_DEV_NAME,
223         .fops  = &obd_psdev_fops,
224 };
225
226 #endif
227
228 #ifdef LPROCFS
229 static int obd_proc_version_seq_show(struct seq_file *m, void *v)
230 {
231         return seq_printf(m, "lustre: %s\nkernel: %s\nbuild:  %s\n",
232                           LUSTRE_VERSION_STRING, "patchless_client",
233                           BUILD_VERSION);
234 }
235 LPROC_SEQ_FOPS_RO(obd_proc_version);
236
237 static int obd_proc_pinger_seq_show(struct seq_file *m, void *v)
238 {
239         return seq_printf(m, "%s\n",
240 #ifdef ENABLE_PINGER
241                              "on"
242 #else
243                              "off"
244 #endif
245                          );
246 }
247 LPROC_SEQ_FOPS_RO(obd_proc_pinger);
248
249 /**
250  * Check all obd devices health
251  *
252  * \param seq_file
253  * \param data [in] unused
254  *
255  * \retval number of characters printed if healthy
256  */
257 static int obd_proc_health_seq_show(struct seq_file *m, void *data)
258 {
259         bool healthy = true;
260         int i;
261
262         if (libcfs_catastrophe)
263                 seq_printf(m, "LBUG\n");
264
265         read_lock(&obd_dev_lock);
266         for (i = 0; i < class_devno_max(); i++) {
267                 struct obd_device *obd;
268
269                 obd = class_num2obd(i);
270                 if (obd == NULL || !obd->obd_attached || !obd->obd_set_up)
271                         continue;
272
273                 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
274                 if (obd->obd_stopping)
275                         continue;
276
277                 class_incref(obd, __FUNCTION__, current);
278                 read_unlock(&obd_dev_lock);
279
280                 if (obd_health_check(NULL, obd)) {
281                         seq_printf(m, "device %s reported unhealthy\n",
282                                         obd->obd_name);
283                         healthy = false;
284                 }
285                 class_decref(obd, __FUNCTION__, current);
286                 read_lock(&obd_dev_lock);
287         }
288         read_unlock(&obd_dev_lock);
289
290         if (healthy)
291                 return seq_printf(m, "healthy\n");
292
293         seq_printf(m, "NOT HEALTHY\n");
294         return 0;
295 }
296 LPROC_SEQ_FOPS_RO(obd_proc_health);
297
298 static int obd_proc_jobid_var_seq_show(struct seq_file *m, void *v)
299 {
300         return seq_printf(m, "%s\n", obd_jobid_var);
301 }
302
303 static ssize_t
304 obd_proc_jobid_var_seq_write(struct file *file, const char __user *buffer,
305                              size_t count, loff_t *off)
306 {
307         if (!count || count > JOBSTATS_JOBID_VAR_MAX_LEN)
308                 return -EINVAL;
309
310         memset(obd_jobid_var, 0, JOBSTATS_JOBID_VAR_MAX_LEN + 1);
311
312         /* This might leave the var invalid on error, which is probably fine.*/
313         if (copy_from_user(obd_jobid_var, buffer, count))
314                 return -EFAULT;
315
316         /* Trim the trailing '\n' if any */
317         if (obd_jobid_var[count - 1] == '\n')
318                 obd_jobid_var[count - 1] = 0;
319
320         return count;
321 }
322 LPROC_SEQ_FOPS(obd_proc_jobid_var);
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_seq_vars lprocfs_base[] = {
329         { .name =       "version",
330           .fops =       &obd_proc_version_fops  },
331         { .name =       "pinger",
332           .fops =       &obd_proc_pinger_fops   },
333         { .name =       "health_check",
334           .fops =       &obd_proc_health_fops   },
335         { .name =       "jobid_var",
336           .fops =       &obd_proc_jobid_var_fops},
337         { 0 }
338 };
339 #else
340 #define lprocfs_base NULL
341 #endif /* LPROCFS */
342
343 #ifdef __KERNEL__
344 static void *obd_device_list_seq_start(struct seq_file *p, loff_t *pos)
345 {
346         if (*pos >= class_devno_max())
347                 return NULL;
348
349         return pos;
350 }
351
352 static void obd_device_list_seq_stop(struct seq_file *p, void *v)
353 {
354 }
355
356 static void *obd_device_list_seq_next(struct seq_file *p, void *v, loff_t *pos)
357 {
358         ++*pos;
359         if (*pos >= class_devno_max())
360                 return NULL;
361
362         return pos;
363 }
364
365 static int obd_device_list_seq_show(struct seq_file *p, void *v)
366 {
367         loff_t index = *(loff_t *)v;
368         struct obd_device *obd = class_num2obd((int)index);
369         char *status;
370
371         if (obd == NULL)
372                 return 0;
373
374         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
375         if (obd->obd_stopping)
376                 status = "ST";
377         else if (obd->obd_inactive)
378                 status = "IN";
379         else if (obd->obd_set_up)
380                 status = "UP";
381         else if (obd->obd_attached)
382                 status = "AT";
383         else
384                 status = "--";
385
386         return seq_printf(p, "%3d %s %s %s %s %d\n",
387                           (int)index, status, obd->obd_type->typ_name,
388                           obd->obd_name, obd->obd_uuid.uuid,
389                           atomic_read(&obd->obd_refcount));
390 }
391
392 struct seq_operations obd_device_list_sops = {
393         .start = obd_device_list_seq_start,
394         .stop = obd_device_list_seq_stop,
395         .next = obd_device_list_seq_next,
396         .show = obd_device_list_seq_show,
397 };
398
399 static int obd_device_list_open(struct inode *inode, struct file *file)
400 {
401         struct seq_file *seq;
402         int rc = seq_open(file, &obd_device_list_sops);
403
404         if (rc)
405                 return rc;
406
407         seq = file->private_data;
408         seq->private = PDE_DATA(inode);
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_seq_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