Whamcloud - gitweb
LU-10086 libcfs: use dynamic minors for /dev/{lnet,obd}
[fs/lustre-release.git] / libcfs / libcfs / 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) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2014, 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
33 #define DEBUG_SUBSYSTEM S_LNET
34
35 #include <linux/miscdevice.h>
36 #include <libcfs/libcfs.h>
37
38 static inline size_t libcfs_ioctl_packlen(struct libcfs_ioctl_data *data)
39 {
40         size_t len = sizeof(*data);
41
42         len += (data->ioc_inllen1 + 7) & ~7;
43         len += (data->ioc_inllen2 + 7) & ~7;
44         return len;
45 }
46
47 static bool libcfs_ioctl_is_invalid(struct libcfs_ioctl_data *data)
48 {
49         if (data->ioc_hdr.ioc_len > BIT(30))
50                 return true;
51
52         if (data->ioc_inllen1 > BIT(30))
53                 return true;
54
55         if (data->ioc_inllen2 > BIT(30))
56                 return true;
57
58         if (data->ioc_inlbuf1 && !data->ioc_inllen1)
59                 return true;
60
61         if (data->ioc_inlbuf2 && !data->ioc_inllen2)
62                 return true;
63
64         if (data->ioc_pbuf1 && !data->ioc_plen1)
65                 return true;
66
67         if (data->ioc_pbuf2 && !data->ioc_plen2)
68                 return true;
69
70         if (data->ioc_plen1 && !data->ioc_pbuf1)
71                 return true;
72
73         if (data->ioc_plen2 && !data->ioc_pbuf2)
74                 return true;
75
76         if (libcfs_ioctl_packlen(data) != data->ioc_hdr.ioc_len)
77                 return true;
78
79         if (data->ioc_inllen1 &&
80             data->ioc_bulk[((data->ioc_inllen1 + 7) & ~7) +
81                              data->ioc_inllen2 - 1] != '\0')
82                 return true;
83
84         return false;
85 }
86
87 int libcfs_ioctl_data_adjust(struct libcfs_ioctl_data *data)
88 {
89         ENTRY;
90
91         if (libcfs_ioctl_is_invalid(data)) {
92                 CERROR("libcfs ioctl: parameter not correctly formatted\n");
93                 RETURN(-EINVAL);
94         }
95
96         if (data->ioc_inllen1 != 0)
97                 data->ioc_inlbuf1 = &data->ioc_bulk[0];
98
99         if (data->ioc_inllen2 != 0)
100                 data->ioc_inlbuf2 = &data->ioc_bulk[0] +
101                                     cfs_size_round(data->ioc_inllen1);
102
103         RETURN(0);
104 }
105
106 int libcfs_ioctl_getdata(struct libcfs_ioctl_hdr **hdr_pp,
107                          struct libcfs_ioctl_hdr __user *uhdr)
108 {
109         struct libcfs_ioctl_hdr   hdr;
110         int err = 0;
111         ENTRY;
112
113         if (copy_from_user(&hdr, uhdr, sizeof(hdr)))
114                 RETURN(-EFAULT);
115
116         if (hdr.ioc_version != LIBCFS_IOCTL_VERSION &&
117             hdr.ioc_version != LIBCFS_IOCTL_VERSION2) {
118                 CERROR("libcfs ioctl: version mismatch expected %#x, got %#x\n",
119                        LIBCFS_IOCTL_VERSION, hdr.ioc_version);
120                 RETURN(-EINVAL);
121         }
122
123         if (hdr.ioc_len < sizeof(struct libcfs_ioctl_hdr)) {
124                 CERROR("libcfs ioctl: user buffer too small for ioctl\n");
125                 RETURN(-EINVAL);
126         }
127
128         if (hdr.ioc_len > LIBCFS_IOC_DATA_MAX) {
129                 CERROR("libcfs ioctl: user buffer is too large %d/%d\n",
130                        hdr.ioc_len, LIBCFS_IOC_DATA_MAX);
131                 RETURN(-EINVAL);
132         }
133
134         LIBCFS_ALLOC(*hdr_pp, hdr.ioc_len);
135         if (*hdr_pp == NULL)
136                 RETURN(-ENOMEM);
137
138         if (copy_from_user(*hdr_pp, uhdr, hdr.ioc_len))
139                 GOTO(failed, err = -EFAULT);
140
141         RETURN(0);
142 failed:
143         LIBCFS_FREE(*hdr_pp, hdr.ioc_len);
144         RETURN(err);
145 }
146
147 static long
148 libcfs_psdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
149 {
150         if (!capable(CAP_SYS_ADMIN))
151                 return -EACCES;
152
153         if (_IOC_TYPE(cmd) != IOC_LIBCFS_TYPE ||
154             _IOC_NR(cmd) < IOC_LIBCFS_MIN_NR ||
155             _IOC_NR(cmd) > IOC_LIBCFS_MAX_NR) {
156                 CDEBUG(D_IOCTL, "invalid ioctl ( type %d, nr %d, size %d )\n",
157                        _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd));
158                 return -EINVAL;
159         }
160
161         return libcfs_ioctl(cmd, (void __user *)arg);
162 }
163
164 static struct file_operations libcfs_fops = {
165         .owner          = THIS_MODULE,
166         .unlocked_ioctl = libcfs_psdev_ioctl,
167 };
168
169 struct miscdevice libcfs_dev = {
170         .minor  = MISC_DYNAMIC_MINOR,
171         .name   = "lnet",
172         .fops   = &libcfs_fops
173 };