Whamcloud - gitweb
b=22990 update copyright messages for Oracle
[fs/lustre-release.git] / lnet / 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  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
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 #include <libcfs/kp30.h>
41
42 #define LNET_MINOR 240
43
44 int libcfs_ioctl_getdata(char *buf, char *end, void *arg)
45 {
46         struct libcfs_ioctl_hdr   *hdr;
47         struct libcfs_ioctl_data  *data;
48         int err;
49         ENTRY;
50
51         hdr = (struct libcfs_ioctl_hdr *)buf;
52         data = (struct libcfs_ioctl_data *)buf;
53
54         err = copy_from_user(buf, (void *)arg, sizeof(*hdr));
55         if (err)
56                 RETURN(err);
57
58         if (hdr->ioc_version != LIBCFS_IOCTL_VERSION) {
59                 CERROR("PORTALS: version mismatch kernel vs application\n");
60                 RETURN(-EINVAL);
61         }
62
63         if (hdr->ioc_len + buf >= end) {
64                 CERROR("PORTALS: user buffer exceeds kernel buffer\n");
65                 RETURN(-EINVAL);
66         }
67
68
69         if (hdr->ioc_len < sizeof(struct libcfs_ioctl_data)) {
70                 CERROR("PORTALS: user buffer too small for ioctl\n");
71                 RETURN(-EINVAL);
72         }
73
74         err = copy_from_user(buf, (void *)arg, hdr->ioc_len);
75         if (err)
76                 RETURN(err);
77
78         if (libcfs_ioctl_is_invalid(data)) {
79                 CERROR("PORTALS: ioctl not correctly formatted\n");
80                 RETURN(-EINVAL);
81         }
82
83         if (data->ioc_inllen1)
84                 data->ioc_inlbuf1 = &data->ioc_bulk[0];
85
86         if (data->ioc_inllen2)
87                 data->ioc_inlbuf2 = &data->ioc_bulk[0] +
88                         size_round(data->ioc_inllen1);
89
90         RETURN(0);
91 }
92
93 int libcfs_ioctl_popdata(void *arg, void *data, int size)
94 {
95         if (copy_to_user((char *)arg, data, size))
96                 return -EFAULT;
97         return 0;
98 }
99
100 extern struct cfs_psdev_ops          libcfs_psdev_ops;
101
102 static int
103 libcfs_psdev_open(struct inode * inode, struct file * file)
104 {
105         struct libcfs_device_userstate **pdu = NULL;
106         int    rc = 0;
107
108         if (!inode)
109                 return (-EINVAL);
110         pdu = (struct libcfs_device_userstate **)&file->private_data;
111         if (libcfs_psdev_ops.p_open != NULL)
112                 rc = libcfs_psdev_ops.p_open(0, (void *)pdu);
113         else
114                 return (-EPERM);
115         return rc;
116 }
117
118 /* called when closing /dev/device */
119 static int
120 libcfs_psdev_release(struct inode * inode, struct file * file)
121 {
122         struct libcfs_device_userstate *pdu;
123         int    rc = 0;
124
125         if (!inode)
126                 return (-EINVAL);
127         pdu = file->private_data;
128         if (libcfs_psdev_ops.p_close != NULL)
129                 rc = libcfs_psdev_ops.p_close(0, (void *)pdu);
130         else
131                 rc = -EPERM;
132         return rc;
133 }
134
135 static int
136 libcfs_ioctl(struct inode *inode, struct file *file,
137              unsigned int cmd, unsigned long arg)
138 {
139         struct cfs_psdev_file    pfile;
140         int    rc = 0;
141
142         if (current_fsuid() != 0)
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 *)arg);
170         else
171                 rc = -EPERM;
172         return (rc);
173 }
174
175 static struct file_operations libcfs_fops = {
176         ioctl:   libcfs_ioctl,
177         open:    libcfs_psdev_open,
178         release: libcfs_psdev_release
179 };
180
181 cfs_psdev_t libcfs_dev = {
182         LNET_MINOR,
183         "lnet",
184         &libcfs_fops
185 };