Whamcloud - gitweb
LU-6245 libcfs: make libcfs_ioctl.h and lnetctl.h uapi compliant
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, 2014, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #define DEBUG_SUBSYSTEM S_LNET
38
39 #include <libcfs/libcfs.h>
40
41 #define LNET_MINOR 240
42
43 static inline size_t libcfs_ioctl_packlen(struct libcfs_ioctl_data *data)
44 {
45         size_t len = sizeof(*data);
46
47         len += (data->ioc_inllen1 + 7) & ~7;
48         len += (data->ioc_inllen2 + 7) & ~7;
49         return len;
50 }
51
52 static bool libcfs_ioctl_is_invalid(struct libcfs_ioctl_data *data)
53 {
54         if (data->ioc_hdr.ioc_len > BIT(30))
55                 return true;
56
57         if (data->ioc_inllen1 > BIT(30))
58                 return true;
59
60         if (data->ioc_inllen2 > BIT(30))
61                 return true;
62
63         if (data->ioc_inlbuf1 && !data->ioc_inllen1)
64                 return true;
65
66         if (data->ioc_inlbuf2 && !data->ioc_inllen2)
67                 return true;
68
69         if (data->ioc_pbuf1 && !data->ioc_plen1)
70                 return true;
71
72         if (data->ioc_pbuf2 && !data->ioc_plen2)
73                 return true;
74
75         if (data->ioc_plen1 && !data->ioc_pbuf1)
76                 return true;
77
78         if (data->ioc_plen2 && !data->ioc_pbuf2)
79                 return true;
80
81         if (libcfs_ioctl_packlen(data) != data->ioc_hdr.ioc_len)
82                 return true;
83
84         if (data->ioc_inllen1 &&
85             data->ioc_bulk[((data->ioc_inllen1 + 7) & ~7) +
86                              data->ioc_inllen2 - 1] != '\0')
87                 return true;
88
89         return false;
90 }
91
92 int libcfs_ioctl_data_adjust(struct libcfs_ioctl_data *data)
93 {
94         ENTRY;
95
96         if (libcfs_ioctl_is_invalid(data)) {
97                 CERROR("libcfs ioctl: parameter not correctly formatted\n");
98                 RETURN(-EINVAL);
99         }
100
101         if (data->ioc_inllen1 != 0)
102                 data->ioc_inlbuf1 = &data->ioc_bulk[0];
103
104         if (data->ioc_inllen2 != 0)
105                 data->ioc_inlbuf2 = &data->ioc_bulk[0] +
106                                     cfs_size_round(data->ioc_inllen1);
107
108         RETURN(0);
109 }
110
111 int libcfs_ioctl_getdata(struct libcfs_ioctl_hdr **hdr_pp,
112                          struct libcfs_ioctl_hdr __user *uhdr)
113 {
114         struct libcfs_ioctl_hdr   hdr;
115         int err = 0;
116         ENTRY;
117
118         if (copy_from_user(&hdr, uhdr, sizeof(hdr)))
119                 RETURN(-EFAULT);
120
121         if (hdr.ioc_version != LIBCFS_IOCTL_VERSION &&
122             hdr.ioc_version != LIBCFS_IOCTL_VERSION2) {
123                 CERROR("libcfs ioctl: version mismatch expected %#x, got %#x\n",
124                        LIBCFS_IOCTL_VERSION, hdr.ioc_version);
125                 RETURN(-EINVAL);
126         }
127
128         if (hdr.ioc_len < sizeof(struct libcfs_ioctl_hdr)) {
129                 CERROR("libcfs ioctl: user buffer too small for ioctl\n");
130                 RETURN(-EINVAL);
131         }
132
133         if (hdr.ioc_len > LIBCFS_IOC_DATA_MAX) {
134                 CERROR("libcfs ioctl: user buffer is too large %d/%d\n",
135                        hdr.ioc_len, LIBCFS_IOC_DATA_MAX);
136                 RETURN(-EINVAL);
137         }
138
139         LIBCFS_ALLOC(*hdr_pp, hdr.ioc_len);
140         if (*hdr_pp == NULL)
141                 RETURN(-ENOMEM);
142
143         if (copy_from_user(*hdr_pp, uhdr, hdr.ioc_len))
144                 GOTO(failed, err = -EFAULT);
145
146         RETURN(0);
147 failed:
148         LIBCFS_FREE(*hdr_pp, hdr.ioc_len);
149         RETURN(err);
150 }
151
152 static int
153 libcfs_psdev_open(struct inode * inode, struct file * file)
154 {
155         if (!inode)
156                 return -EINVAL;
157
158         try_module_get(THIS_MODULE);
159         return 0;
160 }
161
162 /* called when closing /dev/device */
163 static int
164 libcfs_psdev_release(struct inode * inode, struct file * file)
165 {
166         if (!inode)
167                 return -EINVAL;
168
169         module_put(THIS_MODULE);
170         return 0;
171 }
172
173 static long
174 libcfs_psdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
175 {
176         if (!capable(CAP_SYS_ADMIN))
177                 return -EACCES;
178
179         if (_IOC_TYPE(cmd) != IOC_LIBCFS_TYPE ||
180             _IOC_NR(cmd) < IOC_LIBCFS_MIN_NR ||
181             _IOC_NR(cmd) > IOC_LIBCFS_MAX_NR) {
182                 CDEBUG(D_IOCTL, "invalid ioctl ( type %d, nr %d, size %d )\n",
183                        _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd));
184                 return -EINVAL;
185         }
186
187         return libcfs_ioctl(cmd, (void __user *)arg);
188 }
189
190 static struct file_operations libcfs_fops = {
191         .unlocked_ioctl = libcfs_psdev_ioctl,
192         .open           = libcfs_psdev_open,
193         .release        = libcfs_psdev_release
194 };
195
196 struct miscdevice libcfs_dev = {
197         LNET_MINOR,
198         "lnet",
199         &libcfs_fops
200 };