staging: lustre: lnet: migrate headers to lnet uapi directory
[sfrench/cifs-2.6.git] / drivers / staging / lustre / 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, 2012, 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/fs.h>
53 #include <linux/poll.h>
54 #include <linux/list.h>
55 #include <linux/highmem.h>
56 #include <linux/io.h>
57 #include <asm/ioctls.h>
58 #include <linux/uaccess.h>
59 #include <linux/miscdevice.h>
60 #include <linux/seq_file.h>
61 #include <linux/kobject.h>
62
63 #include "../../../include/linux/libcfs/libcfs.h"
64 #include "../../../include/uapi/linux/lnet/lnetctl.h"
65 #include "../../include/obd_support.h"
66 #include "../../include/obd_class.h"
67 #include "../../include/lprocfs_status.h"
68 #include "../../../include/uapi/linux/lustre/lustre_ioctl.h"
69 #include "../../include/lustre_ver.h"
70
71 static int obd_ioctl_is_invalid(struct obd_ioctl_data *data)
72 {
73         if (data->ioc_len > BIT(30)) {
74                 CERROR("OBD ioctl: ioc_len larger than 1<<30\n");
75                 return 1;
76         }
77
78         if (data->ioc_inllen1 > BIT(30)) {
79                 CERROR("OBD ioctl: ioc_inllen1 larger than 1<<30\n");
80                 return 1;
81         }
82
83         if (data->ioc_inllen2 > BIT(30)) {
84                 CERROR("OBD ioctl: ioc_inllen2 larger than 1<<30\n");
85                 return 1;
86         }
87
88         if (data->ioc_inllen3 > BIT(30)) {
89                 CERROR("OBD ioctl: ioc_inllen3 larger than 1<<30\n");
90                 return 1;
91         }
92
93         if (data->ioc_inllen4 > BIT(30)) {
94                 CERROR("OBD ioctl: ioc_inllen4 larger than 1<<30\n");
95                 return 1;
96         }
97
98         if (data->ioc_inlbuf1 && data->ioc_inllen1 == 0) {
99                 CERROR("OBD ioctl: inlbuf1 pointer but 0 length\n");
100                 return 1;
101         }
102
103         if (data->ioc_inlbuf2 && data->ioc_inllen2 == 0) {
104                 CERROR("OBD ioctl: inlbuf2 pointer but 0 length\n");
105                 return 1;
106         }
107
108         if (data->ioc_inlbuf3 && data->ioc_inllen3 == 0) {
109                 CERROR("OBD ioctl: inlbuf3 pointer but 0 length\n");
110                 return 1;
111         }
112
113         if (data->ioc_inlbuf4 && data->ioc_inllen4 == 0) {
114                 CERROR("OBD ioctl: inlbuf4 pointer but 0 length\n");
115                 return 1;
116         }
117
118         if (data->ioc_pbuf1 && data->ioc_plen1 == 0) {
119                 CERROR("OBD ioctl: pbuf1 pointer but 0 length\n");
120                 return 1;
121         }
122
123         if (data->ioc_pbuf2 && data->ioc_plen2 == 0) {
124                 CERROR("OBD ioctl: pbuf2 pointer but 0 length\n");
125                 return 1;
126         }
127
128         if (!data->ioc_pbuf1 && data->ioc_plen1 != 0) {
129                 CERROR("OBD ioctl: plen1 set but NULL pointer\n");
130                 return 1;
131         }
132
133         if (!data->ioc_pbuf2 && data->ioc_plen2 != 0) {
134                 CERROR("OBD ioctl: plen2 set but NULL pointer\n");
135                 return 1;
136         }
137
138         if (obd_ioctl_packlen(data) > data->ioc_len) {
139                 CERROR("OBD ioctl: packlen exceeds ioc_len (%d > %d)\n",
140                 obd_ioctl_packlen(data), data->ioc_len);
141                 return 1;
142         }
143
144         return 0;
145 }
146
147 /* buffer MUST be at least the size of obd_ioctl_hdr */
148 int obd_ioctl_getdata(char **buf, int *len, void __user *arg)
149 {
150         struct obd_ioctl_hdr hdr;
151         struct obd_ioctl_data *data;
152         int err;
153         int offset = 0;
154
155         if (copy_from_user(&hdr, arg, sizeof(hdr)))
156                 return -EFAULT;
157
158         if (hdr.ioc_version != OBD_IOCTL_VERSION) {
159                 CERROR("Version mismatch kernel (%x) vs application (%x)\n",
160                        OBD_IOCTL_VERSION, hdr.ioc_version);
161                 return -EINVAL;
162         }
163
164         if (hdr.ioc_len > OBD_MAX_IOCTL_BUFFER) {
165                 CERROR("User buffer len %d exceeds %d max buffer\n",
166                        hdr.ioc_len, OBD_MAX_IOCTL_BUFFER);
167                 return -EINVAL;
168         }
169
170         if (hdr.ioc_len < sizeof(struct obd_ioctl_data)) {
171                 CERROR("User buffer too small for ioctl (%d)\n", hdr.ioc_len);
172                 return -EINVAL;
173         }
174
175         /* When there are lots of processes calling vmalloc on multi-core
176          * system, the high lock contention will hurt performance badly,
177          * obdfilter-survey is an example, which relies on ioctl. So we'd
178          * better avoid vmalloc on ioctl path. LU-66
179          */
180         *buf = libcfs_kvzalloc(hdr.ioc_len, GFP_NOFS);
181         if (!*buf) {
182                 CERROR("Cannot allocate control buffer of len %d\n",
183                        hdr.ioc_len);
184                 return -EINVAL;
185         }
186         *len = hdr.ioc_len;
187         data = (struct obd_ioctl_data *)*buf;
188
189         if (copy_from_user(*buf, arg, hdr.ioc_len)) {
190                 err = -EFAULT;
191                 goto free_buf;
192         }
193         if (hdr.ioc_len != data->ioc_len) {
194                 err = -EINVAL;
195                 goto free_buf;
196         }
197
198         if (obd_ioctl_is_invalid(data)) {
199                 CERROR("ioctl not correctly formatted\n");
200                 err = -EINVAL;
201                 goto free_buf;
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 free_buf:
225         kvfree(*buf);
226         return err;
227 }
228 EXPORT_SYMBOL(obd_ioctl_getdata);
229
230 /*  opening /dev/obd */
231 static int obd_class_open(struct inode *inode, struct file *file)
232 {
233         try_module_get(THIS_MODULE);
234         return 0;
235 }
236
237 /*  closing /dev/obd */
238 static int obd_class_release(struct inode *inode, struct file *file)
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
250         /* Allow non-root access for OBD_IOC_PING_TARGET - used by lfs check */
251         if (!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 const 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 = OBD_DEV_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         return sprintf(buf, "%s\n", "on");
286 }
287
288 static ssize_t
289 health_check_show(struct kobject *kobj, struct attribute *attr, char *buf)
290 {
291         bool healthy = true;
292         int i;
293         size_t len = 0;
294
295         if (libcfs_catastrophe)
296                 return sprintf(buf, "LBUG\n");
297
298         read_lock(&obd_dev_lock);
299         for (i = 0; i < class_devno_max(); i++) {
300                 struct obd_device *obd;
301
302                 obd = class_num2obd(i);
303                 if (!obd || !obd->obd_attached || !obd->obd_set_up)
304                         continue;
305
306                 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
307                 if (obd->obd_stopping)
308                         continue;
309
310                 class_incref(obd, __func__, current);
311                 read_unlock(&obd_dev_lock);
312
313                 if (obd_health_check(NULL, obd))
314                         healthy = false;
315                 class_decref(obd, __func__, current);
316                 read_lock(&obd_dev_lock);
317         }
318         read_unlock(&obd_dev_lock);
319
320         if (healthy)
321                 len = sprintf(buf, "healthy\n");
322         else
323                 len = sprintf(buf, "NOT HEALTHY\n");
324
325         return len;
326 }
327
328 static ssize_t jobid_var_show(struct kobject *kobj, struct attribute *attr,
329                               char *buf)
330 {
331         return snprintf(buf, PAGE_SIZE, "%s\n", obd_jobid_var);
332 }
333
334 static ssize_t jobid_var_store(struct kobject *kobj, struct attribute *attr,
335                                const char *buffer,
336                                size_t count)
337 {
338         if (!count || count > JOBSTATS_JOBID_VAR_MAX_LEN)
339                 return -EINVAL;
340
341         memset(obd_jobid_var, 0, JOBSTATS_JOBID_VAR_MAX_LEN + 1);
342
343         memcpy(obd_jobid_var, buffer, count);
344
345         /* Trim the trailing '\n' if any */
346         if (obd_jobid_var[count - 1] == '\n')
347                 obd_jobid_var[count - 1] = 0;
348
349         return count;
350 }
351
352 static ssize_t jobid_name_show(struct kobject *kobj, struct attribute *attr,
353                                char *buf)
354 {
355         return snprintf(buf, PAGE_SIZE, "%s\n", obd_jobid_node);
356 }
357
358 static ssize_t jobid_name_store(struct kobject *kobj, struct attribute *attr,
359                                 const char *buffer,
360                                 size_t count)
361 {
362         if (!count || count > LUSTRE_JOBID_SIZE)
363                 return -EINVAL;
364
365         memcpy(obd_jobid_node, buffer, count);
366
367         obd_jobid_node[count] = 0;
368
369         /* Trim the trailing '\n' if any */
370         if (obd_jobid_node[count - 1] == '\n')
371                 obd_jobid_node[count - 1] = 0;
372
373         return count;
374 }
375
376 /* Root for /sys/kernel/debug/lustre */
377 struct dentry *debugfs_lustre_root;
378 EXPORT_SYMBOL_GPL(debugfs_lustre_root);
379
380 LUSTRE_RO_ATTR(version);
381 LUSTRE_RO_ATTR(pinger);
382 LUSTRE_RO_ATTR(health_check);
383 LUSTRE_RW_ATTR(jobid_var);
384 LUSTRE_RW_ATTR(jobid_name);
385
386 static struct attribute *lustre_attrs[] = {
387         &lustre_attr_version.attr,
388         &lustre_attr_pinger.attr,
389         &lustre_attr_health_check.attr,
390         &lustre_attr_jobid_name.attr,
391         &lustre_attr_jobid_var.attr,
392         NULL,
393 };
394
395 static void *obd_device_list_seq_start(struct seq_file *p, loff_t *pos)
396 {
397         if (*pos >= class_devno_max())
398                 return NULL;
399
400         return pos;
401 }
402
403 static void obd_device_list_seq_stop(struct seq_file *p, void *v)
404 {
405 }
406
407 static void *obd_device_list_seq_next(struct seq_file *p, void *v, loff_t *pos)
408 {
409         ++*pos;
410         if (*pos >= class_devno_max())
411                 return NULL;
412
413         return pos;
414 }
415
416 static int obd_device_list_seq_show(struct seq_file *p, void *v)
417 {
418         loff_t index = *(loff_t *)v;
419         struct obd_device *obd = class_num2obd((int)index);
420         char *status;
421
422         if (!obd)
423                 return 0;
424
425         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
426         if (obd->obd_stopping)
427                 status = "ST";
428         else if (obd->obd_inactive)
429                 status = "IN";
430         else if (obd->obd_set_up)
431                 status = "UP";
432         else if (obd->obd_attached)
433                 status = "AT";
434         else
435                 status = "--";
436
437         seq_printf(p, "%3d %s %s %s %s %d\n",
438                    (int)index, status, obd->obd_type->typ_name,
439                    obd->obd_name, obd->obd_uuid.uuid,
440                    atomic_read(&obd->obd_refcount));
441         return 0;
442 }
443
444 static const struct seq_operations obd_device_list_sops = {
445         .start = obd_device_list_seq_start,
446         .stop = obd_device_list_seq_stop,
447         .next = obd_device_list_seq_next,
448         .show = obd_device_list_seq_show,
449 };
450
451 static int obd_device_list_open(struct inode *inode, struct file *file)
452 {
453         struct seq_file *seq;
454         int rc = seq_open(file, &obd_device_list_sops);
455
456         if (rc)
457                 return rc;
458
459         seq = file->private_data;
460         seq->private = inode->i_private;
461
462         return 0;
463 }
464
465 static const struct file_operations obd_device_list_fops = {
466         .owner   = THIS_MODULE,
467         .open    = obd_device_list_open,
468         .read    = seq_read,
469         .llseek  = seq_lseek,
470         .release = seq_release,
471 };
472
473 struct kobject *lustre_kobj;
474 EXPORT_SYMBOL_GPL(lustre_kobj);
475
476 static const struct attribute_group lustre_attr_group = {
477         .attrs = lustre_attrs,
478 };
479
480 int class_procfs_init(void)
481 {
482         int rc = -ENOMEM;
483         struct dentry *file;
484
485         lustre_kobj = kobject_create_and_add("lustre", fs_kobj);
486         if (!lustre_kobj)
487                 goto out;
488
489         /* Create the files associated with this kobject */
490         rc = sysfs_create_group(lustre_kobj, &lustre_attr_group);
491         if (rc) {
492                 kobject_put(lustre_kobj);
493                 goto out;
494         }
495
496         debugfs_lustre_root = debugfs_create_dir("lustre", NULL);
497         if (IS_ERR_OR_NULL(debugfs_lustre_root)) {
498                 rc = debugfs_lustre_root ? PTR_ERR(debugfs_lustre_root)
499                                          : -ENOMEM;
500                 debugfs_lustre_root = NULL;
501                 kobject_put(lustre_kobj);
502                 goto out;
503         }
504
505         file = debugfs_create_file("devices", 0444, debugfs_lustre_root, NULL,
506                                    &obd_device_list_fops);
507         if (IS_ERR_OR_NULL(file)) {
508                 rc = file ? PTR_ERR(file) : -ENOMEM;
509                 kobject_put(lustre_kobj);
510                 goto out;
511         }
512 out:
513         return rc;
514 }
515
516 int class_procfs_clean(void)
517 {
518         debugfs_remove_recursive(debugfs_lustre_root);
519
520         debugfs_lustre_root = NULL;
521
522         kobject_put(lustre_kobj);
523
524         return 0;
525 }