Whamcloud - gitweb
0d8ed9c6a929d7697779f98a17395960353d9b2f
[fs/lustre-release.git] / libcfs / libcfs / linux / linux-module.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  *
32  * Copyright (c) 2012, Whamcloud, Inc.
33  */
34 /*
35  * This file is part of Lustre, http://www.lustre.org/
36  * Lustre is a trademark of Sun Microsystems, Inc.
37  */
38
39 #define DEBUG_SUBSYSTEM S_LNET
40
41 #include <libcfs/libcfs.h>
42
43 #define LNET_MINOR 240
44
45 int libcfs_ioctl_getdata(char *buf, char *end, void *arg)
46 {
47         struct libcfs_ioctl_hdr   *hdr;
48         struct libcfs_ioctl_data  *data;
49         int err;
50         ENTRY;
51
52         hdr = (struct libcfs_ioctl_hdr *)buf;
53         data = (struct libcfs_ioctl_data *)buf;
54
55         err = copy_from_user(buf, (void *)arg, sizeof(*hdr));
56         if (err)
57                 RETURN(err);
58
59         if (hdr->ioc_version != LIBCFS_IOCTL_VERSION) {
60                 CERROR("PORTALS: version mismatch kernel vs application\n");
61                 RETURN(-EINVAL);
62         }
63
64         if (hdr->ioc_len + buf >= end) {
65                 CERROR("PORTALS: user buffer exceeds kernel buffer\n");
66                 RETURN(-EINVAL);
67         }
68
69
70         if (hdr->ioc_len < sizeof(struct libcfs_ioctl_data)) {
71                 CERROR("PORTALS: user buffer too small for ioctl\n");
72                 RETURN(-EINVAL);
73         }
74
75         err = copy_from_user(buf, (void *)arg, hdr->ioc_len);
76         if (err)
77                 RETURN(err);
78
79         if (libcfs_ioctl_is_invalid(data)) {
80                 CERROR("PORTALS: ioctl not correctly formatted\n");
81                 RETURN(-EINVAL);
82         }
83
84         if (data->ioc_inllen1)
85                 data->ioc_inlbuf1 = &data->ioc_bulk[0];
86
87         if (data->ioc_inllen2)
88                 data->ioc_inlbuf2 = &data->ioc_bulk[0] +
89                         cfs_size_round(data->ioc_inllen1);
90
91         RETURN(0);
92 }
93
94 int libcfs_ioctl_popdata(void *arg, void *data, int size)
95 {
96         if (copy_to_user((char *)arg, data, size))
97                 return -EFAULT;
98         return 0;
99 }
100
101 extern struct cfs_psdev_ops          libcfs_psdev_ops;
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
137 #ifdef HAVE_UNLOCKED_IOCTL
138 static long libcfs_ioctl(struct file *file,
139                          unsigned int cmd, unsigned long arg)
140 #else
141 static int  libcfs_ioctl(struct inode *inode, struct file *file,
142                          unsigned int cmd, unsigned long arg)
143 #endif
144 {
145         struct cfs_psdev_file    pfile;
146         int    rc = 0;
147
148         if (current_fsuid() != 0)
149                 return -EACCES;
150
151         if ( _IOC_TYPE(cmd) != IOC_LIBCFS_TYPE ||
152              _IOC_NR(cmd) < IOC_LIBCFS_MIN_NR  ||
153              _IOC_NR(cmd) > IOC_LIBCFS_MAX_NR ) {
154                 CDEBUG(D_IOCTL, "invalid ioctl ( type %d, nr %d, size %d )\n",
155                        _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd));
156                 return (-EINVAL);
157         }
158
159         /* Handle platform-dependent IOC requests */
160         switch (cmd) {
161         case IOC_LIBCFS_PANIC:
162                 if (!cfs_capable(CFS_CAP_SYS_BOOT))
163                         return (-EPERM);
164                 panic("debugctl-invoked panic");
165                 return (0);
166         case IOC_LIBCFS_MEMHOG:
167                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
168                         return -EPERM;
169                 /* go thought */
170         }
171
172         pfile.off = 0;
173         pfile.private_data = file->private_data;
174         if (libcfs_psdev_ops.p_ioctl != NULL)
175                 rc = libcfs_psdev_ops.p_ioctl(&pfile, cmd, (void *)arg);
176         else
177                 rc = -EPERM;
178         return (rc);
179 }
180
181 static struct file_operations libcfs_fops = {
182 #ifdef HAVE_UNLOCKED_IOCTL
183         unlocked_ioctl: libcfs_ioctl,
184 #else
185         ioctl:          libcfs_ioctl,
186 #endif
187         open :          libcfs_psdev_open,
188         release :       libcfs_psdev_release
189 };
190
191 cfs_psdev_t libcfs_dev = {
192         LNET_MINOR,
193         "lnet",
194         &libcfs_fops
195 };