Whamcloud - gitweb
fbe459ff7289548374b92d39e717dce956b06d9b
[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, 2014, 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 #include <linux/module.h>
46 #include <linux/errno.h>
47 #include <linux/kernel.h>
48 #include <linux/major.h>
49 #include <linux/sched.h>
50 #include <linux/lp.h>
51 #include <linux/slab.h>
52 #include <linux/ioport.h>
53 #include <linux/fcntl.h>
54 #include <linux/delay.h>
55 #include <linux/skbuff.h>
56 #include <linux/proc_fs.h>
57 #include <linux/fs.h>
58 #include <linux/poll.h>
59 #include <linux/init.h>
60 #include <linux/list.h>
61 #include <linux/highmem.h>
62 #include <asm/io.h>
63 #include <asm/ioctls.h>
64 #include <asm/poll.h>
65 #include <asm/uaccess.h>
66 #include <linux/miscdevice.h>
67 #include <linux/seq_file.h>
68
69 #include <libcfs/libcfs.h>
70 #include <obd_support.h>
71 #include <obd_class.h>
72 #include <lnet/lnetctl.h>
73 #include <lprocfs_status.h>
74 #include <lustre_ioctl.h>
75 #include <lustre_ver.h>
76
77 int proc_version;
78
79 /* buffer MUST be at least the size of obd_ioctl_hdr */
80 int obd_ioctl_getdata(char **buf, int *len, void __user *arg)
81 {
82         struct obd_ioctl_hdr hdr;
83         struct obd_ioctl_data *data;
84         int offset = 0;
85         ENTRY;
86
87         if (copy_from_user(&hdr, arg, sizeof(hdr)))
88                 RETURN(-EFAULT);
89
90         if (hdr.ioc_version != OBD_IOCTL_VERSION) {
91                 CERROR("Version mismatch kernel (%x) vs application (%x)\n",
92                        OBD_IOCTL_VERSION, hdr.ioc_version);
93                 RETURN(-EINVAL);
94         }
95
96         if (hdr.ioc_len > OBD_MAX_IOCTL_BUFFER) {
97                 CERROR("User buffer len %d exceeds %d max buffer\n",
98                        hdr.ioc_len, OBD_MAX_IOCTL_BUFFER);
99                 RETURN(-EINVAL);
100         }
101
102         if (hdr.ioc_len < sizeof(struct obd_ioctl_data)) {
103                 CERROR("User buffer too small for ioctl (%d)\n", hdr.ioc_len);
104                 RETURN(-EINVAL);
105         }
106
107         /* When there are lots of processes calling vmalloc on multi-core
108          * system, the high lock contention will hurt performance badly,
109          * obdfilter-survey is an example, which relies on ioctl. So we'd
110          * better avoid vmalloc on ioctl path. LU-66 */
111         OBD_ALLOC_LARGE(*buf, hdr.ioc_len);
112         if (*buf == NULL) {
113                 CERROR("Cannot allocate control buffer of len %d\n",
114                        hdr.ioc_len);
115                 RETURN(-EINVAL);
116         }
117         *len = hdr.ioc_len;
118         data = (struct obd_ioctl_data *)*buf;
119
120         if (copy_from_user(*buf, arg, hdr.ioc_len)) {
121                 OBD_FREE_LARGE(*buf, hdr.ioc_len);
122                 RETURN(-EFAULT);
123         }
124
125         if (obd_ioctl_is_invalid(data)) {
126                 CERROR("ioctl not correctly formatted\n");
127                 OBD_FREE_LARGE(*buf, hdr.ioc_len);
128                 RETURN(-EINVAL);
129         }
130
131         if (data->ioc_inllen1) {
132                 data->ioc_inlbuf1 = &data->ioc_bulk[0];
133                 offset += cfs_size_round(data->ioc_inllen1);
134         }
135
136         if (data->ioc_inllen2) {
137                 data->ioc_inlbuf2 = &data->ioc_bulk[0] + offset;
138                 offset += cfs_size_round(data->ioc_inllen2);
139         }
140
141         if (data->ioc_inllen3) {
142                 data->ioc_inlbuf3 = &data->ioc_bulk[0] + offset;
143                 offset += cfs_size_round(data->ioc_inllen3);
144         }
145
146         if (data->ioc_inllen4)
147                 data->ioc_inlbuf4 = &data->ioc_bulk[0] + offset;
148
149         RETURN(0);
150 }
151 EXPORT_SYMBOL(obd_ioctl_getdata);
152
153 int obd_ioctl_popdata(void __user *arg, void *data, int len)
154 {
155         int err;
156         ENTRY;
157
158         err = copy_to_user(arg, data, len) ? -EFAULT : 0;
159         RETURN(err);
160 }
161
162 /*  opening /dev/obd */
163 static int obd_class_open(struct inode * inode, struct file * file)
164 {
165         ENTRY;
166
167         try_module_get(THIS_MODULE);
168         RETURN(0);
169 }
170
171 /*  closing /dev/obd */
172 static int obd_class_release(struct inode * inode, struct file * file)
173 {
174         ENTRY;
175
176         module_put(THIS_MODULE);
177         RETURN(0);
178 }
179
180 /* to control /dev/obd */
181 static long obd_class_ioctl(struct file *filp, unsigned int cmd,
182                             unsigned long arg)
183 {
184         int err = 0;
185         ENTRY;
186
187         /* Allow non-root access for OBD_IOC_PING_TARGET - used by lfs check */
188         if (!cfs_capable(CFS_CAP_SYS_ADMIN) && (cmd != OBD_IOC_PING_TARGET))
189                 RETURN(err = -EACCES);
190         if ((cmd & 0xffffff00) == ((int)'T') << 8) /* ignore all tty ioctls */
191                 RETURN(err = -ENOTTY);
192
193         err = class_handle_ioctl(cmd, (unsigned long)arg);
194
195         RETURN(err);
196 }
197
198 /* declare character device */
199 static struct file_operations obd_psdev_fops = {
200         .owner          = THIS_MODULE,
201         .unlocked_ioctl = obd_class_ioctl, /* unlocked_ioctl */
202         .open           = obd_class_open,      /* open */
203         .release        = obd_class_release,   /* release */
204 };
205
206 /* modules setup */
207 struct miscdevice obd_psdev = {
208         .minor = OBD_DEV_MINOR,
209         .name  = OBD_DEV_NAME,
210         .fops  = &obd_psdev_fops,
211 };
212
213
214 #ifdef CONFIG_PROC_FS
215 static int obd_proc_version_seq_show(struct seq_file *m, void *v)
216 {
217         seq_printf(m, "lustre: %s", LUSTRE_VERSION_STRING);
218         return 0;
219 }
220 LPROC_SEQ_FOPS_RO(obd_proc_version);
221
222 static int obd_proc_pinger_seq_show(struct seq_file *m, void *v)
223 {
224         seq_printf(m, "%s\n",
225 #ifdef ENABLE_PINGER
226                    "on"
227 #else
228                    "off"
229 #endif
230                  );
231         return 0;
232 }
233 LPROC_SEQ_FOPS_RO(obd_proc_pinger);
234
235 /**
236  * Check all obd devices health
237  *
238  * \param seq_file
239  * \param data [in] unused
240  *
241  * \retval number of characters printed if healthy
242  */
243 static int obd_proc_health_seq_show(struct seq_file *m, void *data)
244 {
245         bool healthy = true;
246         int i;
247
248         if (libcfs_catastrophe)
249                 seq_printf(m, "LBUG\n");
250
251         read_lock(&obd_dev_lock);
252         for (i = 0; i < class_devno_max(); i++) {
253                 struct obd_device *obd;
254
255                 obd = class_num2obd(i);
256                 if (obd == NULL || !obd->obd_attached || !obd->obd_set_up)
257                         continue;
258
259                 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
260                 if (obd->obd_stopping)
261                         continue;
262
263                 class_incref(obd, __FUNCTION__, current);
264                 read_unlock(&obd_dev_lock);
265
266                 if (obd_health_check(NULL, obd)) {
267                         seq_printf(m, "device %s reported unhealthy\n",
268                                    obd->obd_name);
269                         healthy = false;
270                 }
271                 class_decref(obd, __FUNCTION__, current);
272                 read_lock(&obd_dev_lock);
273         }
274         read_unlock(&obd_dev_lock);
275
276         if (healthy)
277                 seq_puts(m, "healthy\n");
278         else
279                 seq_puts(m, "NOT HEALTHY\n");
280         return 0;
281 }
282 LPROC_SEQ_FOPS_RO(obd_proc_health);
283
284 static int obd_proc_jobid_var_seq_show(struct seq_file *m, void *v)
285 {
286         if (strlen(obd_jobid_var) != 0)
287                 seq_printf(m, "%s\n", obd_jobid_var);
288         return 0;
289 }
290
291 static ssize_t
292 obd_proc_jobid_var_seq_write(struct file *file, const char __user *buffer,
293                              size_t count, loff_t *off)
294 {
295         if (!count || count > JOBSTATS_JOBID_VAR_MAX_LEN)
296                 return -EINVAL;
297
298         memset(obd_jobid_var, 0, JOBSTATS_JOBID_VAR_MAX_LEN + 1);
299
300         /* This might leave the var invalid on error, which is probably fine.*/
301         if (copy_from_user(obd_jobid_var, buffer, count))
302                 return -EFAULT;
303
304         /* Trim the trailing '\n' if any */
305         if (obd_jobid_var[count - 1] == '\n')
306                 obd_jobid_var[count - 1] = 0;
307
308         return count;
309 }
310 LPROC_SEQ_FOPS(obd_proc_jobid_var);
311
312 static int obd_proc_jobid_name_seq_show(struct seq_file *m, void *v)
313 {
314         if (strlen(obd_jobid_node) != 0)
315                 seq_printf(m, "%s\n", obd_jobid_node);
316         return 0;
317 }
318
319 static ssize_t obd_proc_jobid_name_seq_write(struct file *file,
320                                              const char __user *buffer,
321                                              size_t count, loff_t *off)
322 {
323         if (count == 0 || count > LUSTRE_JOBID_SIZE)
324                 return -EINVAL;
325
326         /* clear previous value */
327         memset(obd_jobid_node, 0, LUSTRE_JOBID_SIZE);
328
329         if (copy_from_user(obd_jobid_node, buffer, count))
330                 return -EFAULT;
331
332         /* Trim the trailing '\n' if any */
333         if (obd_jobid_node[count - 1] == '\n') {
334                 /* Don't echo just a newline */
335                 if (count == 1)
336                         return -EINVAL;
337                 obd_jobid_node[count - 1] = 0;
338         }
339
340         return count;
341 }
342 LPROC_SEQ_FOPS(obd_proc_jobid_name);
343
344 /* Root for /proc/fs/lustre */
345 struct proc_dir_entry *proc_lustre_root = NULL;
346 EXPORT_SYMBOL(proc_lustre_root);
347
348 static struct lprocfs_vars lprocfs_base[] = {
349         { .name =       "version",
350           .fops =       &obd_proc_version_fops  },
351         { .name =       "pinger",
352           .fops =       &obd_proc_pinger_fops   },
353         { .name =       "health_check",
354           .fops =       &obd_proc_health_fops   },
355         { .name =       "jobid_var",
356           .fops =       &obd_proc_jobid_var_fops},
357         { .name =       "jobid_name",
358           .fops =       &obd_proc_jobid_name_fops},
359         { NULL }
360 };
361 #else
362 #define lprocfs_base NULL
363 #endif /* CONFIG_PROC_FS */
364
365 static void *obd_device_list_seq_start(struct seq_file *p, loff_t *pos)
366 {
367         if (*pos >= class_devno_max())
368                 return NULL;
369
370         return pos;
371 }
372
373 static void obd_device_list_seq_stop(struct seq_file *p, void *v)
374 {
375 }
376
377 static void *obd_device_list_seq_next(struct seq_file *p, void *v, loff_t *pos)
378 {
379         ++*pos;
380         if (*pos >= class_devno_max())
381                 return NULL;
382
383         return pos;
384 }
385
386 static int obd_device_list_seq_show(struct seq_file *p, void *v)
387 {
388         loff_t index = *(loff_t *)v;
389         struct obd_device *obd = class_num2obd((int)index);
390         char *status;
391
392         if (obd == NULL)
393                 return 0;
394
395         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
396         if (obd->obd_stopping)
397                 status = "ST";
398         else if (obd->obd_inactive)
399                 status = "IN";
400         else if (obd->obd_set_up)
401                 status = "UP";
402         else if (obd->obd_attached)
403                 status = "AT";
404         else
405                 status = "--";
406
407         seq_printf(p, "%3d %s %s %s %s %d\n",
408                    (int)index, status, obd->obd_type->typ_name,
409                    obd->obd_name, obd->obd_uuid.uuid,
410                    atomic_read(&obd->obd_refcount));
411         return 0;
412 }
413
414 static const struct seq_operations obd_device_list_sops = {
415         .start = obd_device_list_seq_start,
416         .stop = obd_device_list_seq_stop,
417         .next = obd_device_list_seq_next,
418         .show = obd_device_list_seq_show,
419 };
420
421 static int obd_device_list_open(struct inode *inode, struct file *file)
422 {
423         struct seq_file *seq;
424         int rc = seq_open(file, &obd_device_list_sops);
425
426         if (rc)
427                 return rc;
428
429         seq = file->private_data;
430         seq->private = PDE_DATA(inode);
431         return 0;
432 }
433
434 static const struct file_operations obd_device_list_fops = {
435         .owner   = THIS_MODULE,
436         .open    = obd_device_list_open,
437         .read    = seq_read,
438         .llseek  = seq_lseek,
439         .release = seq_release,
440 };
441
442 int class_procfs_init(void)
443 {
444         struct proc_dir_entry *entry;
445         int rc;
446         ENTRY;
447
448         obd_sysctl_init();
449
450         entry = lprocfs_register("fs/lustre", NULL, lprocfs_base, NULL);
451         if (IS_ERR(entry)) {
452                 rc = PTR_ERR(entry);
453                 CERROR("cannot create '/proc/fs/lustre': rc = %d\n", rc);
454                 RETURN(rc);
455         }
456
457         proc_lustre_root = entry;
458
459         rc = lprocfs_seq_create(proc_lustre_root, "devices", 0444,
460                                 &obd_device_list_fops, NULL);
461         if (rc < 0) {
462                 CERROR("cannot create '/proc/fs/lustre/devices': rc = %d\n",
463                        rc);
464                 GOTO(out_proc, rc);
465         }
466
467         RETURN(rc);
468
469 out_proc:
470         lprocfs_remove(&proc_lustre_root);
471
472         RETURN(rc);
473 }
474
475 int class_procfs_clean(void)
476 {
477         ENTRY;
478         if (proc_lustre_root) {
479                 lprocfs_remove(&proc_lustre_root);
480         }
481         RETURN(0);
482 }