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