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