Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/hid
[cascardo/linux.git] / drivers / staging / lustre / lnet / 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, 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 "../../../include/linux/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 += cfs_size_round(data->ioc_inllen1);
48         len += cfs_size_round(data->ioc_inllen2);
49         return len;
50 }
51
52 static inline bool libcfs_ioctl_is_invalid(struct libcfs_ioctl_data *data)
53 {
54         if (data->ioc_hdr.ioc_len > BIT(30)) {
55                 CERROR("LIBCFS ioctl: ioc_len larger than 1<<30\n");
56                 return true;
57         }
58         if (data->ioc_inllen1 > BIT(30)) {
59                 CERROR("LIBCFS ioctl: ioc_inllen1 larger than 1<<30\n");
60                 return true;
61         }
62         if (data->ioc_inllen2 > BIT(30)) {
63                 CERROR("LIBCFS ioctl: ioc_inllen2 larger than 1<<30\n");
64                 return true;
65         }
66         if (data->ioc_inlbuf1 && !data->ioc_inllen1) {
67                 CERROR("LIBCFS ioctl: inlbuf1 pointer but 0 length\n");
68                 return true;
69         }
70         if (data->ioc_inlbuf2 && !data->ioc_inllen2) {
71                 CERROR("LIBCFS ioctl: inlbuf2 pointer but 0 length\n");
72                 return true;
73         }
74         if (data->ioc_pbuf1 && !data->ioc_plen1) {
75                 CERROR("LIBCFS ioctl: pbuf1 pointer but 0 length\n");
76                 return true;
77         }
78         if (data->ioc_pbuf2 && !data->ioc_plen2) {
79                 CERROR("LIBCFS ioctl: pbuf2 pointer but 0 length\n");
80                 return true;
81         }
82         if (data->ioc_plen1 && !data->ioc_pbuf1) {
83                 CERROR("LIBCFS ioctl: plen1 nonzero but no pbuf1 pointer\n");
84                 return true;
85         }
86         if (data->ioc_plen2 && !data->ioc_pbuf2) {
87                 CERROR("LIBCFS ioctl: plen2 nonzero but no pbuf2 pointer\n");
88                 return true;
89         }
90         if ((__u32)libcfs_ioctl_packlen(data) != data->ioc_hdr.ioc_len) {
91                 CERROR("LIBCFS ioctl: packlen != ioc_len\n");
92                 return true;
93         }
94         if (data->ioc_inllen1 &&
95             data->ioc_bulk[data->ioc_inllen1 - 1] != '\0') {
96                 CERROR("LIBCFS ioctl: inlbuf1 not 0 terminated\n");
97                 return true;
98         }
99         if (data->ioc_inllen2 &&
100             data->ioc_bulk[cfs_size_round(data->ioc_inllen1) +
101                            data->ioc_inllen2 - 1] != '\0') {
102                 CERROR("LIBCFS ioctl: inlbuf2 not 0 terminated\n");
103                 return true;
104         }
105         return false;
106 }
107
108 int libcfs_ioctl_data_adjust(struct libcfs_ioctl_data *data)
109 {
110         if (libcfs_ioctl_is_invalid(data)) {
111                 CERROR("libcfs ioctl: parameter not correctly formatted\n");
112                 return -EINVAL;
113         }
114
115         if (data->ioc_inllen1)
116                 data->ioc_inlbuf1 = &data->ioc_bulk[0];
117
118         if (data->ioc_inllen2)
119                 data->ioc_inlbuf2 = &data->ioc_bulk[0] +
120                         cfs_size_round(data->ioc_inllen1);
121
122         return 0;
123 }
124
125 int libcfs_ioctl_getdata(struct libcfs_ioctl_hdr **hdr_pp,
126                          const struct libcfs_ioctl_hdr __user *uhdr)
127 {
128         struct libcfs_ioctl_hdr hdr;
129         int err = 0;
130
131         if (copy_from_user(&hdr, uhdr, sizeof(hdr)))
132                 return -EFAULT;
133
134         if (hdr.ioc_version != LIBCFS_IOCTL_VERSION &&
135             hdr.ioc_version != LIBCFS_IOCTL_VERSION2) {
136                 CERROR("libcfs ioctl: version mismatch expected %#x, got %#x\n",
137                        LIBCFS_IOCTL_VERSION, hdr.ioc_version);
138                 return -EINVAL;
139         }
140
141         if (hdr.ioc_len < sizeof(struct libcfs_ioctl_data)) {
142                 CERROR("libcfs ioctl: user buffer too small for ioctl\n");
143                 return -EINVAL;
144         }
145
146         if (hdr.ioc_len > LIBCFS_IOC_DATA_MAX) {
147                 CERROR("libcfs ioctl: user buffer is too large %d/%d\n",
148                        hdr.ioc_len, LIBCFS_IOC_DATA_MAX);
149                 return -EINVAL;
150         }
151
152         LIBCFS_ALLOC(*hdr_pp, hdr.ioc_len);
153         if (!*hdr_pp)
154                 return -ENOMEM;
155
156         if (copy_from_user(*hdr_pp, uhdr, hdr.ioc_len)) {
157                 LIBCFS_FREE(*hdr_pp, hdr.ioc_len);
158                 err = -EFAULT;
159         }
160         return err;
161 }
162
163 static long
164 libcfs_psdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
165 {
166         if (!capable(CAP_SYS_ADMIN))
167                 return -EACCES;
168
169         if (_IOC_TYPE(cmd) != IOC_LIBCFS_TYPE ||
170             _IOC_NR(cmd) < IOC_LIBCFS_MIN_NR  ||
171             _IOC_NR(cmd) > IOC_LIBCFS_MAX_NR) {
172                 CDEBUG(D_IOCTL, "invalid ioctl ( type %d, nr %d, size %d )\n",
173                        _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd));
174                 return -EINVAL;
175         }
176
177         return libcfs_ioctl(cmd, (void __user *)arg);
178 }
179
180 static const struct file_operations libcfs_fops = {
181         .owner          = THIS_MODULE,
182         .unlocked_ioctl = libcfs_psdev_ioctl,
183 };
184
185 struct miscdevice libcfs_dev = {
186         .minor = LNET_MINOR,
187         .name = "lnet",
188         .fops = &libcfs_fops,
189 };