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