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