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.sun.com/software/products/lustre/docs/GPLv2.pdf
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
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, 2014, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #define DEBUG_SUBSYSTEM S_LNET
39 #include <libcfs/libcfs.h>
41 #define LNET_MINOR 240
43 static inline size_t libcfs_ioctl_packlen(struct libcfs_ioctl_data *data)
45 size_t len = sizeof(*data);
47 len += (data->ioc_inllen1 + 7) & ~7;
48 len += (data->ioc_inllen2 + 7) & ~7;
52 static bool libcfs_ioctl_is_invalid(struct libcfs_ioctl_data *data)
54 if (data->ioc_hdr.ioc_len > BIT(30))
57 if (data->ioc_inllen1 > BIT(30))
60 if (data->ioc_inllen2 > BIT(30))
63 if (data->ioc_inlbuf1 && !data->ioc_inllen1)
66 if (data->ioc_inlbuf2 && !data->ioc_inllen2)
69 if (data->ioc_pbuf1 && !data->ioc_plen1)
72 if (data->ioc_pbuf2 && !data->ioc_plen2)
75 if (data->ioc_plen1 && !data->ioc_pbuf1)
78 if (data->ioc_plen2 && !data->ioc_pbuf2)
81 if (libcfs_ioctl_packlen(data) != data->ioc_hdr.ioc_len)
84 if (data->ioc_inllen1 &&
85 data->ioc_bulk[((data->ioc_inllen1 + 7) & ~7) +
86 data->ioc_inllen2 - 1] != '\0')
92 int libcfs_ioctl_data_adjust(struct libcfs_ioctl_data *data)
96 if (libcfs_ioctl_is_invalid(data)) {
97 CERROR("libcfs ioctl: parameter not correctly formatted\n");
101 if (data->ioc_inllen1 != 0)
102 data->ioc_inlbuf1 = &data->ioc_bulk[0];
104 if (data->ioc_inllen2 != 0)
105 data->ioc_inlbuf2 = &data->ioc_bulk[0] +
106 cfs_size_round(data->ioc_inllen1);
111 int libcfs_ioctl_getdata(struct libcfs_ioctl_hdr **hdr_pp,
112 struct libcfs_ioctl_hdr __user *uhdr)
114 struct libcfs_ioctl_hdr hdr;
118 if (copy_from_user(&hdr, uhdr, sizeof(hdr)))
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);
128 if (hdr.ioc_len < sizeof(struct libcfs_ioctl_hdr)) {
129 CERROR("libcfs ioctl: user buffer too small for ioctl\n");
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);
139 LIBCFS_ALLOC(*hdr_pp, hdr.ioc_len);
143 if (copy_from_user(*hdr_pp, uhdr, hdr.ioc_len))
144 GOTO(failed, err = -EFAULT);
148 LIBCFS_FREE(*hdr_pp, hdr.ioc_len);
153 libcfs_psdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
155 if (!capable(CAP_SYS_ADMIN))
158 if (_IOC_TYPE(cmd) != IOC_LIBCFS_TYPE ||
159 _IOC_NR(cmd) < IOC_LIBCFS_MIN_NR ||
160 _IOC_NR(cmd) > IOC_LIBCFS_MAX_NR) {
161 CDEBUG(D_IOCTL, "invalid ioctl ( type %d, nr %d, size %d )\n",
162 _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd));
166 return libcfs_ioctl(cmd, (void __user *)arg);
169 static struct file_operations libcfs_fops = {
170 .owner = THIS_MODULE,
171 .unlocked_ioctl = libcfs_psdev_ioctl,
174 struct miscdevice libcfs_dev = {