Whamcloud - gitweb
7175e445fbe522e6a60d1592ab3f0048f35777fe
[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 int libcfs_ioctl_data_adjust(struct libcfs_ioctl_data *data)
44 {
45         ENTRY;
46
47         if (libcfs_ioctl_is_invalid(data)) {
48                 CERROR("libcfs ioctl: parameter not correctly formatted\n");
49                 RETURN(-EINVAL);
50         }
51
52         if (data->ioc_inllen1 != 0)
53                 data->ioc_inlbuf1 = &data->ioc_bulk[0];
54
55         if (data->ioc_inllen2 != 0)
56                 data->ioc_inlbuf2 = &data->ioc_bulk[0] +
57                                     cfs_size_round(data->ioc_inllen1);
58
59         RETURN(0);
60 }
61
62 int libcfs_ioctl_getdata(struct libcfs_ioctl_hdr **hdr_pp,
63                          struct libcfs_ioctl_hdr __user *uhdr)
64 {
65         struct libcfs_ioctl_hdr   hdr;
66         int err = 0;
67         ENTRY;
68
69         if (copy_from_user(&hdr, uhdr, sizeof(hdr)))
70                 RETURN(-EFAULT);
71
72         if (hdr.ioc_version != LIBCFS_IOCTL_VERSION &&
73             hdr.ioc_version != LIBCFS_IOCTL_VERSION2) {
74                 CERROR("libcfs ioctl: version mismatch expected %#x, got %#x\n",
75                        LIBCFS_IOCTL_VERSION, hdr.ioc_version);
76                 RETURN(-EINVAL);
77         }
78
79         if (hdr.ioc_len < sizeof(struct libcfs_ioctl_hdr)) {
80                 CERROR("libcfs ioctl: user buffer too small for ioctl\n");
81                 RETURN(-EINVAL);
82         }
83
84         if (hdr.ioc_len > LIBCFS_IOC_DATA_MAX) {
85                 CERROR("libcfs ioctl: user buffer is too large %d/%d\n",
86                        hdr.ioc_len, LIBCFS_IOC_DATA_MAX);
87                 RETURN(-EINVAL);
88         }
89
90         LIBCFS_ALLOC(*hdr_pp, hdr.ioc_len);
91         if (*hdr_pp == NULL)
92                 RETURN(-ENOMEM);
93
94         if (copy_from_user(*hdr_pp, uhdr, hdr.ioc_len))
95                 GOTO(failed, err = -EFAULT);
96
97         RETURN(0);
98 failed:
99         libcfs_ioctl_freedata(*hdr_pp);
100         RETURN(err);
101 }
102
103 static int
104 libcfs_psdev_open(struct inode * inode, struct file * file)
105 {
106         struct libcfs_device_userstate **pdu = NULL;
107         int    rc = 0;
108
109         if (!inode)
110                 return (-EINVAL);
111         pdu = (struct libcfs_device_userstate **)&file->private_data;
112         if (libcfs_psdev_ops.p_open != NULL)
113                 rc = libcfs_psdev_ops.p_open(0, (void *)pdu);
114         else
115                 return (-EPERM);
116         return rc;
117 }
118
119 /* called when closing /dev/device */
120 static int
121 libcfs_psdev_release(struct inode * inode, struct file * file)
122 {
123         struct libcfs_device_userstate *pdu;
124         int    rc = 0;
125
126         if (!inode)
127                 return (-EINVAL);
128         pdu = file->private_data;
129         if (libcfs_psdev_ops.p_close != NULL)
130                 rc = libcfs_psdev_ops.p_close(0, (void *)pdu);
131         else
132                 rc = -EPERM;
133         return rc;
134 }
135
136 static long libcfs_ioctl(struct file *file,
137                          unsigned int cmd, unsigned long arg)
138 {
139         struct cfs_psdev_file    pfile;
140         int    rc = 0;
141
142         if (!capable(CAP_SYS_ADMIN))
143                 return -EACCES;
144
145         if (_IOC_TYPE(cmd) != IOC_LIBCFS_TYPE ||
146             _IOC_NR(cmd) < IOC_LIBCFS_MIN_NR ||
147             _IOC_NR(cmd) > IOC_LIBCFS_MAX_NR) {
148                 CDEBUG(D_IOCTL, "invalid ioctl ( type %d, nr %d, size %d )\n",
149                        _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd));
150                 return (-EINVAL);
151         }
152
153         /* Handle platform-dependent IOC requests */
154         switch (cmd) {
155         case IOC_LIBCFS_PANIC:
156                 if (!cfs_capable(CFS_CAP_SYS_BOOT))
157                         return (-EPERM);
158                 panic("debugctl-invoked panic");
159                 return (0);
160         case IOC_LIBCFS_MEMHOG:
161                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
162                         return -EPERM;
163                 /* go thought */
164         }
165
166         pfile.off = 0;
167         pfile.private_data = file->private_data;
168         if (libcfs_psdev_ops.p_ioctl != NULL)
169                 rc = libcfs_psdev_ops.p_ioctl(&pfile, cmd, (void __user *)arg);
170         else
171                 rc = -EPERM;
172         return (rc);
173 }
174
175 static struct file_operations libcfs_fops = {
176         unlocked_ioctl: libcfs_ioctl,
177         open :          libcfs_psdev_open,
178         release :       libcfs_psdev_release
179 };
180
181 struct miscdevice libcfs_dev = {
182         LNET_MINOR,
183         "lnet",
184         &libcfs_fops
185 };