blob: 7634551afed3ec3b65cddf9229a00d5a479d9c7e [file] [log] [blame]
Peng Taod7e09d02013-05-02 16:46:55 +08001/*
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
Greg Kroah-Hartman9fdaf8c2014-07-11 20:51:16 -070039#include "../../../include/linux/libcfs/libcfs.h"
Peng Taod7e09d02013-05-02 16:46:55 +080040
41#define LNET_MINOR 240
42
Amir Shehata12909322016-02-22 17:29:02 -050043int libcfs_ioctl_data_adjust(struct libcfs_ioctl_data *data)
Peng Taod7e09d02013-05-02 16:46:55 +080044{
Peng Taod7e09d02013-05-02 16:46:55 +080045 if (libcfs_ioctl_is_invalid(data)) {
Liang Zhen6d0aeaa32016-03-22 19:03:56 -040046 CERROR("libcfs ioctl: parameter not correctly formatted\n");
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +080047 return -EINVAL;
Peng Taod7e09d02013-05-02 16:46:55 +080048 }
49
50 if (data->ioc_inllen1)
51 data->ioc_inlbuf1 = &data->ioc_bulk[0];
52
53 if (data->ioc_inllen2)
54 data->ioc_inlbuf2 = &data->ioc_bulk[0] +
55 cfs_size_round(data->ioc_inllen1);
56
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +080057 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +080058}
59
Liang Zhene4efb342016-03-22 19:03:50 -040060int libcfs_ioctl_getdata(struct libcfs_ioctl_hdr **hdr_pp,
61 const struct libcfs_ioctl_hdr __user *uhdr)
Amir Shehata12909322016-02-22 17:29:02 -050062{
63 struct libcfs_ioctl_hdr hdr;
Liang Zhene4efb342016-03-22 19:03:50 -040064 int err = 0;
Amir Shehata12909322016-02-22 17:29:02 -050065
Liang Zhene4efb342016-03-22 19:03:50 -040066 if (copy_from_user(&hdr, uhdr, sizeof(uhdr)))
Amir Shehata12909322016-02-22 17:29:02 -050067 return -EFAULT;
68
Amir Shehata2e9a51b2016-02-22 17:29:03 -050069 if (hdr.ioc_version != LIBCFS_IOCTL_VERSION &&
70 hdr.ioc_version != LIBCFS_IOCTL_VERSION2) {
Liang Zhen6d0aeaa32016-03-22 19:03:56 -040071 CERROR("libcfs ioctl: version mismatch expected %#x, got %#x\n",
Amir Shehata12909322016-02-22 17:29:02 -050072 LIBCFS_IOCTL_VERSION, hdr.ioc_version);
73 return -EINVAL;
74 }
75
Liang Zhened2f5492016-03-22 19:03:57 -040076 if (hdr.ioc_len < sizeof(struct libcfs_ioctl_data)) {
77 CERROR("libcfs ioctl: user buffer too small for ioctl\n");
78 return -EINVAL;
79 }
80
Liang Zhene4efb342016-03-22 19:03:50 -040081 if (hdr.ioc_len > LIBCFS_IOC_DATA_MAX) {
82 CERROR("libcfs ioctl: user buffer is too large %d/%d\n",
83 hdr.ioc_len, LIBCFS_IOC_DATA_MAX);
84 return -EINVAL;
85 }
Amir Shehata12909322016-02-22 17:29:02 -050086
Liang Zhene4efb342016-03-22 19:03:50 -040087 LIBCFS_ALLOC(*hdr_pp, hdr.ioc_len);
88 if (!*hdr_pp)
89 return -ENOMEM;
90
91 if (copy_from_user(*hdr_pp, uhdr, hdr.ioc_len)) {
92 LIBCFS_FREE(*hdr_pp, hdr.ioc_len);
93 err = -EFAULT;
94 }
95 return err;
Amir Shehata12909322016-02-22 17:29:02 -050096}
97
Parinay Kondekarae272a42016-03-22 19:04:07 -040098static long
99libcfs_psdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Peng Taod7e09d02013-05-02 16:46:55 +0800100{
Peng Tao4b1a25f2013-07-15 22:27:14 +0800101 if (!capable(CAP_SYS_ADMIN))
Peng Taod7e09d02013-05-02 16:46:55 +0800102 return -EACCES;
103
Jessica Yu91a002c2014-07-28 06:33:13 -0700104 if (_IOC_TYPE(cmd) != IOC_LIBCFS_TYPE ||
Oleg Drokinae0b4832016-02-16 00:47:00 -0500105 _IOC_NR(cmd) < IOC_LIBCFS_MIN_NR ||
106 _IOC_NR(cmd) > IOC_LIBCFS_MAX_NR) {
Peng Taod7e09d02013-05-02 16:46:55 +0800107 CDEBUG(D_IOCTL, "invalid ioctl ( type %d, nr %d, size %d )\n",
108 _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd));
Julia Lawallfbe7c6c2014-08-26 22:00:33 +0200109 return -EINVAL;
Peng Taod7e09d02013-05-02 16:46:55 +0800110 }
111
Parinay Kondekar89010fb2016-03-22 19:04:08 -0400112 return libcfs_ioctl(cmd, (void __user *)arg);
Peng Taod7e09d02013-05-02 16:46:55 +0800113}
114
Jessica Yud81f9f52014-07-28 06:33:15 -0700115static const struct file_operations libcfs_fops = {
Parinay Kondekar4678d182016-03-22 19:04:06 -0400116 .owner = THIS_MODULE,
Parinay Kondekarae272a42016-03-22 19:04:07 -0400117 .unlocked_ioctl = libcfs_psdev_ioctl,
Peng Taod7e09d02013-05-02 16:46:55 +0800118};
119
Greg Kroah-Hartmanc0426cf2013-07-24 10:21:26 -0700120struct miscdevice libcfs_dev = {
121 .minor = LNET_MINOR,
122 .name = "lnet",
123 .fops = &libcfs_fops,
Peng Taod7e09d02013-05-02 16:46:55 +0800124};