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