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