4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2014, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
33 #define DEBUG_SUBSYSTEM S_LNET
35 #include <linux/miscdevice.h>
36 #include <libcfs/libcfs.h>
38 static inline size_t libcfs_ioctl_packlen(struct libcfs_ioctl_data *data)
40 size_t len = sizeof(*data);
42 len += (data->ioc_inllen1 + 7) & ~7;
43 len += (data->ioc_inllen2 + 7) & ~7;
47 static bool libcfs_ioctl_is_invalid(struct libcfs_ioctl_data *data)
49 if (data->ioc_hdr.ioc_len > BIT(30))
52 if (data->ioc_inllen1 > BIT(30))
55 if (data->ioc_inllen2 > BIT(30))
58 if (data->ioc_inlbuf1 && !data->ioc_inllen1)
61 if (data->ioc_inlbuf2 && !data->ioc_inllen2)
64 if (data->ioc_pbuf1 && !data->ioc_plen1)
67 if (data->ioc_pbuf2 && !data->ioc_plen2)
70 if (data->ioc_plen1 && !data->ioc_pbuf1)
73 if (data->ioc_plen2 && !data->ioc_pbuf2)
76 if (libcfs_ioctl_packlen(data) != data->ioc_hdr.ioc_len)
79 if (data->ioc_inllen1 &&
80 data->ioc_bulk[((data->ioc_inllen1 + 7) & ~7) +
81 data->ioc_inllen2 - 1] != '\0')
87 int libcfs_ioctl_data_adjust(struct libcfs_ioctl_data *data)
91 if (libcfs_ioctl_is_invalid(data)) {
92 CERROR("libcfs ioctl: parameter not correctly formatted\n");
96 if (data->ioc_inllen1 != 0)
97 data->ioc_inlbuf1 = &data->ioc_bulk[0];
99 if (data->ioc_inllen2 != 0)
100 data->ioc_inlbuf2 = &data->ioc_bulk[0] +
101 cfs_size_round(data->ioc_inllen1);
106 int libcfs_ioctl_getdata(struct libcfs_ioctl_hdr **hdr_pp,
107 struct libcfs_ioctl_hdr __user *uhdr)
109 struct libcfs_ioctl_hdr hdr;
113 if (copy_from_user(&hdr, uhdr, sizeof(hdr)))
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);
123 if (hdr.ioc_len < sizeof(struct libcfs_ioctl_hdr)) {
124 CERROR("libcfs ioctl: user buffer too small for ioctl\n");
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);
134 LIBCFS_ALLOC(*hdr_pp, hdr.ioc_len);
138 if (copy_from_user(*hdr_pp, uhdr, hdr.ioc_len))
139 GOTO(failed, err = -EFAULT);
143 LIBCFS_FREE(*hdr_pp, hdr.ioc_len);
148 libcfs_psdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
150 if (!capable(CAP_SYS_ADMIN))
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));
161 return libcfs_ioctl(cmd, (void __user *)arg);
164 static struct file_operations libcfs_fops = {
165 .owner = THIS_MODULE,
166 .unlocked_ioctl = libcfs_psdev_ioctl,
169 struct miscdevice libcfs_dev = {
170 .minor = MISC_DYNAMIC_MINOR,