Whamcloud - gitweb
LU-6401 uapi: migrate remaining uapi headers to uapi directory
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2016, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/obdclass/linux/linux-module.c
33  *
34  * Object Devices Class Driver
35  * These are the only exported functions, they provide some generic
36  * infrastructure for managing object devices
37  */
38
39 #define DEBUG_SUBSYSTEM S_CLASS
40
41 #include <linux/module.h>
42 #include <linux/errno.h>
43 #include <linux/kernel.h>
44 #include <linux/major.h>
45 #include <linux/sched.h>
46 #include <linux/lp.h>
47 #include <linux/slab.h>
48 #include <linux/ioport.h>
49 #include <linux/fcntl.h>
50 #include <linux/delay.h>
51 #include <linux/skbuff.h>
52 #include <linux/proc_fs.h>
53 #include <linux/fs.h>
54 #include <linux/poll.h>
55 #include <linux/init.h>
56 #include <linux/list.h>
57 #include <linux/highmem.h>
58 #include <asm/io.h>
59 #include <asm/ioctls.h>
60 #include <asm/poll.h>
61 #include <asm/uaccess.h>
62 #include <linux/miscdevice.h>
63 #include <linux/seq_file.h>
64 #include <linux/kobject.h>
65
66 #include <libcfs/libcfs.h>
67 #include <obd_support.h>
68 #include <obd_class.h>
69 #include <lprocfs_status.h>
70 #include <uapi/linux/lnet/lnetctl.h>
71 #include <uapi/linux/lustre/lustre_ioctl.h>
72 #include <uapi/linux/lustre/lustre_ver.h>
73
74 #define OBD_MAX_IOCTL_BUFFER    8192
75
76 static int obd_ioctl_is_invalid(struct obd_ioctl_data *data)
77 {
78         if (data->ioc_len > BIT(30)) {
79                 CERROR("OBD ioctl: ioc_len larger than 1<<30\n");
80                 return 1;
81         }
82
83         if (data->ioc_inllen1 > BIT(30)) {
84                 CERROR("OBD ioctl: ioc_inllen1 larger than 1<<30\n");
85                 return 1;
86         }
87
88         if (data->ioc_inllen2 > BIT(30)) {
89                 CERROR("OBD ioctl: ioc_inllen2 larger than 1<<30\n");
90                 return 1;
91         }
92
93         if (data->ioc_inllen3 > BIT(30)) {
94                 CERROR("OBD ioctl: ioc_inllen3 larger than 1<<30\n");
95                 return 1;
96         }
97
98         if (data->ioc_inllen4 > BIT(30)) {
99                 CERROR("OBD ioctl: ioc_inllen4 larger than 1<<30\n");
100                 return 1;
101         }
102
103         if (data->ioc_inlbuf1 && data->ioc_inllen1 == 0) {
104                 CERROR("OBD ioctl: inlbuf1 pointer but 0 length\n");
105                 return 1;
106         }
107
108         if (data->ioc_inlbuf2 && data->ioc_inllen2 == 0) {
109                 CERROR("OBD ioctl: inlbuf2 pointer but 0 length\n");
110                 return 1;
111         }
112
113         if (data->ioc_inlbuf3 && data->ioc_inllen3 == 0) {
114                 CERROR("OBD ioctl: inlbuf3 pointer but 0 length\n");
115                 return 1;
116         }
117
118         if (data->ioc_inlbuf4 && data->ioc_inllen4 == 0) {
119                 CERROR("OBD ioctl: inlbuf4 pointer but 0 length\n");
120                 return 1;
121         }
122
123         if (data->ioc_pbuf1 && data->ioc_plen1 == 0) {
124                 CERROR("OBD ioctl: pbuf1 pointer but 0 length\n");
125                 return 1;
126         }
127
128         if (data->ioc_pbuf2 && data->ioc_plen2 == 0) {
129                 CERROR("OBD ioctl: pbuf2 pointer but 0 length\n");
130                 return 1;
131         }
132
133         if (!data->ioc_pbuf1 && data->ioc_plen1 != 0) {
134                 CERROR("OBD ioctl: plen1 set but NULL pointer\n");
135                 return 1;
136         }
137
138         if (!data->ioc_pbuf2 && data->ioc_plen2 != 0) {
139                 CERROR("OBD ioctl: plen2 set but NULL pointer\n");
140                 return 1;
141         }
142
143         if (obd_ioctl_packlen(data) > data->ioc_len) {
144                 CERROR("OBD ioctl: packlen exceeds ioc_len (%d > %d)\n",
145                        obd_ioctl_packlen(data), data->ioc_len);
146                 return 1;
147         }
148
149         return 0;
150 }
151
152 /* buffer MUST be at least the size of obd_ioctl_hdr */
153 int obd_ioctl_getdata(char **buf, int *len, void __user *arg)
154 {
155         struct obd_ioctl_hdr hdr;
156         struct obd_ioctl_data *data;
157         int offset = 0;
158         ENTRY;
159
160         if (copy_from_user(&hdr, arg, sizeof(hdr)))
161                 RETURN(-EFAULT);
162
163         if (hdr.ioc_version != OBD_IOCTL_VERSION) {
164                 CERROR("Version mismatch kernel (%x) vs application (%x)\n",
165                        OBD_IOCTL_VERSION, hdr.ioc_version);
166                 RETURN(-EINVAL);
167         }
168
169         if (hdr.ioc_len > OBD_MAX_IOCTL_BUFFER) {
170                 CERROR("User buffer len %d exceeds %d max buffer\n",
171                        hdr.ioc_len, OBD_MAX_IOCTL_BUFFER);
172                 RETURN(-EINVAL);
173         }
174
175         if (hdr.ioc_len < sizeof(struct obd_ioctl_data)) {
176                 CERROR("User buffer too small for ioctl (%d)\n", hdr.ioc_len);
177                 RETURN(-EINVAL);
178         }
179
180         /* When there are lots of processes calling vmalloc on multi-core
181          * system, the high lock contention will hurt performance badly,
182          * obdfilter-survey is an example, which relies on ioctl. So we'd
183          * better avoid vmalloc on ioctl path. LU-66 */
184         OBD_ALLOC_LARGE(*buf, hdr.ioc_len);
185         if (*buf == NULL) {
186                 CERROR("Cannot allocate control buffer of len %d\n",
187                        hdr.ioc_len);
188                 RETURN(-EINVAL);
189         }
190         *len = hdr.ioc_len;
191         data = (struct obd_ioctl_data *)*buf;
192
193         if (copy_from_user(*buf, arg, hdr.ioc_len)) {
194                 OBD_FREE_LARGE(*buf, hdr.ioc_len);
195                 RETURN(-EFAULT);
196         }
197
198         if (obd_ioctl_is_invalid(data)) {
199                 CERROR("ioctl not correctly formatted\n");
200                 OBD_FREE_LARGE(*buf, hdr.ioc_len);
201                 RETURN(-EINVAL);
202         }
203
204         if (data->ioc_inllen1) {
205                 data->ioc_inlbuf1 = &data->ioc_bulk[0];
206                 offset += cfs_size_round(data->ioc_inllen1);
207         }
208
209         if (data->ioc_inllen2) {
210                 data->ioc_inlbuf2 = &data->ioc_bulk[0] + offset;
211                 offset += cfs_size_round(data->ioc_inllen2);
212         }
213
214         if (data->ioc_inllen3) {
215                 data->ioc_inlbuf3 = &data->ioc_bulk[0] + offset;
216                 offset += cfs_size_round(data->ioc_inllen3);
217         }
218
219         if (data->ioc_inllen4)
220                 data->ioc_inlbuf4 = &data->ioc_bulk[0] + offset;
221
222         RETURN(0);
223 }
224 EXPORT_SYMBOL(obd_ioctl_getdata);
225
226 /*  opening /dev/obd */
227 static int obd_class_open(struct inode * inode, struct file * file)
228 {
229         ENTRY;
230
231         try_module_get(THIS_MODULE);
232         RETURN(0);
233 }
234
235 /*  closing /dev/obd */
236 static int obd_class_release(struct inode * inode, struct file * file)
237 {
238         ENTRY;
239
240         module_put(THIS_MODULE);
241         RETURN(0);
242 }
243
244 /* to control /dev/obd */
245 static long obd_class_ioctl(struct file *filp, unsigned int cmd,
246                             unsigned long arg)
247 {
248         int err = 0;
249         ENTRY;
250
251         /* Allow non-root access for OBD_IOC_PING_TARGET - used by lfs check */
252         if (!cfs_capable(CFS_CAP_SYS_ADMIN) && (cmd != OBD_IOC_PING_TARGET))
253                 RETURN(err = -EACCES);
254         if ((cmd & 0xffffff00) == ((int)'T') << 8) /* ignore all tty ioctls */
255                 RETURN(err = -ENOTTY);
256
257         err = class_handle_ioctl(cmd, (unsigned long)arg);
258
259         RETURN(err);
260 }
261
262 /* declare character device */
263 static struct file_operations obd_psdev_fops = {
264         .owner          = THIS_MODULE,
265         .unlocked_ioctl = obd_class_ioctl, /* unlocked_ioctl */
266         .open           = obd_class_open,      /* open */
267         .release        = obd_class_release,   /* release */
268 };
269
270 /* modules setup */
271 struct miscdevice obd_psdev = {
272         .minor = OBD_DEV_MINOR,
273         .name  = OBD_DEV_NAME,
274         .fops  = &obd_psdev_fops,
275 };
276
277 static ssize_t version_show(struct kobject *kobj, struct attribute *attr,
278                             char *buf)
279 {
280         return sprintf(buf, "%s\n", LUSTRE_VERSION_STRING);
281 }
282
283 static ssize_t pinger_show(struct kobject *kobj, struct attribute *attr,
284                            char *buf)
285 {
286 #ifdef ENABLE_PINGER
287         const char *state = "on";
288 #else
289         const char *state = "off";
290 #endif
291         return sprintf(buf, "%s\n", state);
292 }
293
294 /**
295  * Check all obd devices health
296  *
297  * \param kobj
298  * \param buf [in]
299  *
300  * \retval number of characters printed if healthy
301  */
302 static ssize_t
303 health_check_show(struct kobject *kobj, struct attribute *attr, char *buf)
304 {
305         bool healthy = true;
306         size_t len = 0;
307         int i;
308
309         if (libcfs_catastrophe) {
310                 len = sprintf(buf, "LBUG\n");
311                 healthy = false;
312         }
313
314         read_lock(&obd_dev_lock);
315         for (i = 0; i < class_devno_max(); i++) {
316                 struct obd_device *obd;
317
318                 obd = class_num2obd(i);
319                 if (obd == NULL || !obd->obd_attached || !obd->obd_set_up)
320                         continue;
321
322                 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
323                 if (obd->obd_stopping)
324                         continue;
325
326                 class_incref(obd, __FUNCTION__, current);
327                 read_unlock(&obd_dev_lock);
328
329                 if (obd_health_check(NULL, obd)) {
330                         len = sprintf(buf, "device %s reported unhealthy\n",
331                                       obd->obd_name);
332                         healthy = false;
333                 }
334                 class_decref(obd, __FUNCTION__, current);
335                 read_lock(&obd_dev_lock);
336         }
337         read_unlock(&obd_dev_lock);
338
339         if (healthy)
340                 len = sprintf(buf, "healthy\n");
341         else
342                 len = sprintf(buf, "NOT HEALTHY\n");
343
344         return len;
345 }
346
347 static ssize_t jobid_var_show(struct kobject *kobj, struct attribute *attr,
348                               char *buf)
349 {
350         int rc = 0;
351
352         if (strlen(obd_jobid_var))
353                 rc = snprintf(buf, PAGE_SIZE, "%s\n", obd_jobid_var);
354         return rc;
355 }
356
357 static ssize_t jobid_var_store(struct kobject *kobj, struct attribute *attr,
358                                const char *buffer, size_t count)
359 {
360         if (!count || count > JOBSTATS_JOBID_VAR_MAX_LEN)
361                 return -EINVAL;
362
363         memset(obd_jobid_var, 0, JOBSTATS_JOBID_VAR_MAX_LEN + 1);
364
365         memcpy(obd_jobid_var, buffer, count);
366
367         /* Trim the trailing '\n' if any */
368         if (obd_jobid_var[count - 1] == '\n')
369                 obd_jobid_var[count - 1] = 0;
370
371         return count;
372 }
373
374 static ssize_t jobid_name_show(struct kobject *kobj, struct attribute *attr,
375                                char *buf)
376 {
377         int rc = 0;
378
379         if (strlen(obd_jobid_node))
380                 rc = snprintf(buf, PAGE_SIZE, "%s\n", obd_jobid_node);
381         return rc;
382 }
383
384 static ssize_t jobid_name_store(struct kobject *kobj, struct attribute *attr,
385                                 const char *buffer, size_t count)
386 {
387         if (!count || count > LUSTRE_JOBID_SIZE)
388                 return -EINVAL;
389
390         /* clear previous value */
391         memset(obd_jobid_node, 0, LUSTRE_JOBID_SIZE);
392
393         memcpy(obd_jobid_node, buffer, count);
394
395         /* Trim the trailing '\n' if any */
396         if (obd_jobid_node[count - 1] == '\n') {
397                 /* Don't echo just a newline */
398                 if (count == 1)
399                         return -EINVAL;
400                 obd_jobid_node[count - 1] = 0;
401         }
402
403         return count;
404 }
405
406 /* Root for /sys/kernel/debug/lustre */
407 struct dentry *debugfs_lustre_root;
408 EXPORT_SYMBOL_GPL(debugfs_lustre_root);
409
410 #ifdef CONFIG_PROC_FS
411 /* Root for /proc/fs/lustre */
412 struct proc_dir_entry *proc_lustre_root = NULL;
413 EXPORT_SYMBOL(proc_lustre_root);
414 #else
415 #define lprocfs_base NULL
416 #endif /* CONFIG_PROC_FS */
417
418 LUSTRE_RO_ATTR(version);
419 LUSTRE_RO_ATTR(pinger);
420 LUSTRE_RO_ATTR(health_check);
421 LUSTRE_RW_ATTR(jobid_var);
422 LUSTRE_RW_ATTR(jobid_name);
423
424 static struct attribute *lustre_attrs[] = {
425         &lustre_attr_version.attr,
426         &lustre_attr_pinger.attr,
427         &lustre_attr_health_check.attr,
428         &lustre_attr_jobid_name.attr,
429         &lustre_attr_jobid_var.attr,
430         NULL,
431 };
432
433 static void *obd_device_list_seq_start(struct seq_file *p, loff_t *pos)
434 {
435         if (*pos >= class_devno_max())
436                 return NULL;
437
438         return pos;
439 }
440
441 static void obd_device_list_seq_stop(struct seq_file *p, void *v)
442 {
443 }
444
445 static void *obd_device_list_seq_next(struct seq_file *p, void *v, loff_t *pos)
446 {
447         ++*pos;
448         if (*pos >= class_devno_max())
449                 return NULL;
450
451         return pos;
452 }
453
454 static int obd_device_list_seq_show(struct seq_file *p, void *v)
455 {
456         loff_t index = *(loff_t *)v;
457         struct obd_device *obd = class_num2obd((int)index);
458         char *status;
459
460         if (obd == NULL)
461                 return 0;
462
463         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
464         if (obd->obd_stopping)
465                 status = "ST";
466         else if (obd->obd_inactive)
467                 status = "IN";
468         else if (obd->obd_set_up)
469                 status = "UP";
470         else if (obd->obd_attached)
471                 status = "AT";
472         else
473                 status = "--";
474
475         seq_printf(p, "%3d %s %s %s %s %d\n",
476                    (int)index, status, obd->obd_type->typ_name,
477                    obd->obd_name, obd->obd_uuid.uuid,
478                    atomic_read(&obd->obd_refcount));
479         return 0;
480 }
481
482 static const struct seq_operations obd_device_list_sops = {
483         .start = obd_device_list_seq_start,
484         .stop = obd_device_list_seq_stop,
485         .next = obd_device_list_seq_next,
486         .show = obd_device_list_seq_show,
487 };
488
489 static int obd_device_list_open(struct inode *inode, struct file *file)
490 {
491         struct seq_file *seq;
492         int rc = seq_open(file, &obd_device_list_sops);
493
494         if (rc)
495                 return rc;
496
497         seq = file->private_data;
498         seq->private = inode->i_private;
499         return 0;
500 }
501
502 static const struct file_operations obd_device_list_fops = {
503         .owner   = THIS_MODULE,
504         .open    = obd_device_list_open,
505         .read    = seq_read,
506         .llseek  = seq_lseek,
507         .release = seq_release,
508 };
509
510 struct kobject *lustre_kobj;
511 EXPORT_SYMBOL_GPL(lustre_kobj);
512
513 static struct attribute_group lustre_attr_group = {
514         .attrs = lustre_attrs,
515 };
516
517 int class_procfs_init(void)
518 {
519         struct proc_dir_entry *entry;
520         struct dentry *file;
521         int rc = -ENOMEM;
522         ENTRY;
523
524         lustre_kobj = kobject_create_and_add("lustre", fs_kobj);
525         if (lustre_kobj == NULL)
526                 goto out;
527
528         /* Create the files associated with this kobject */
529         rc = sysfs_create_group(lustre_kobj, &lustre_attr_group);
530         if (rc) {
531                 kobject_put(lustre_kobj);
532                 goto out;
533         }
534
535         rc = obd_sysctl_init();
536         if (rc) {
537                 kobject_put(lustre_kobj);
538                 goto out;
539         }
540
541         debugfs_lustre_root = debugfs_create_dir("lustre", NULL);
542         if (IS_ERR_OR_NULL(debugfs_lustre_root)) {
543                 rc = debugfs_lustre_root ? PTR_ERR(debugfs_lustre_root)
544                                          : -ENOMEM;
545                 debugfs_lustre_root = NULL;
546                 kobject_put(lustre_kobj);
547                 goto out;
548         }
549
550         file = debugfs_create_file("devices", 0444, debugfs_lustre_root, NULL,
551                                    &obd_device_list_fops);
552         if (IS_ERR_OR_NULL(file)) {
553                 rc = file ? PTR_ERR(file) : -ENOMEM;
554                 kobject_put(lustre_kobj);
555                 goto out;
556         }
557
558         entry = lprocfs_register("fs/lustre", NULL, NULL, NULL);
559         if (IS_ERR(entry)) {
560                 rc = PTR_ERR(entry);
561                 CERROR("cannot create '/proc/fs/lustre': rc = %d\n", rc);
562                 kobject_put(lustre_kobj);
563                 goto out;
564         }
565
566         proc_lustre_root = entry;
567 out:
568         RETURN(rc);
569 }
570
571 int class_procfs_clean(void)
572 {
573         ENTRY;
574
575         debugfs_remove_recursive(debugfs_lustre_root);
576
577         debugfs_lustre_root = NULL;
578
579         if (proc_lustre_root)
580                 lprocfs_remove(&proc_lustre_root);
581
582         kobject_put(lustre_kobj);
583
584         RETURN(0);
585 }