Whamcloud - gitweb
LU-1347 build: remove the vim/emacs modelines
[fs/lustre-release.git] / libcfs / libcfs / winnt / winnt-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 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  */
34
35
36 #define DEBUG_SUBSYSTEM S_LNET
37
38 #include <libcfs/libcfs.h>
39
40 #define LIBCFS_MINOR 240
41
42 int libcfs_ioctl_getdata(char *buf, char *end, void *arg)
43 {
44         struct libcfs_ioctl_hdr *hdr;
45         struct libcfs_ioctl_data *data;
46         int err;
47         ENTRY;
48
49         hdr = (struct libcfs_ioctl_hdr *)buf;
50         data = (struct libcfs_ioctl_data *)buf;
51
52         err = cfs_copy_from_user(buf, (void *)arg, sizeof(*hdr));
53         if (err)
54                 RETURN(err);
55
56         if (hdr->ioc_version != LIBCFS_IOCTL_VERSION) {
57                 CERROR("LIBCFS: version mismatch kernel vs application\n");
58                 RETURN(-EINVAL);
59         }
60
61         if (hdr->ioc_len + buf >= end) {
62                 CERROR("LIBCFS: user buffer exceeds kernel buffer\n");
63                 RETURN(-EINVAL);
64         }
65
66         if (hdr->ioc_len < sizeof(struct libcfs_ioctl_data)) {
67                 CERROR("LIBCFS: user buffer too small for ioctl\n");
68                 RETURN(-EINVAL);
69         }
70
71         err = cfs_copy_from_user(buf, (void *)arg, hdr->ioc_len);
72         if (err)
73                 RETURN(err);
74
75         if (libcfs_ioctl_is_invalid(data)) {
76                 CERROR("LIBCFS: ioctl not correctly formatted\n");
77                 RETURN(-EINVAL);
78         }
79
80         if (data->ioc_inllen1)
81                 data->ioc_inlbuf1 = &data->ioc_bulk[0];
82
83         if (data->ioc_inllen2)
84                 data->ioc_inlbuf2 = &data->ioc_bulk[0] +
85                         cfs_size_round(data->ioc_inllen1);
86
87         RETURN(0);
88 }
89
90 int libcfs_ioctl_popdata(void *arg, void *data, int size)
91 {
92         if (cfs_copy_to_user((char *)arg, data, size))
93                 return -EFAULT;
94         return 0;
95 }
96                                                                                                                                                                        
97 extern struct cfs_psdev_ops          libcfs_psdev_ops;
98
99 static int 
100 libcfs_psdev_open(struct inode *in, cfs_file_t * file)
101
102         struct libcfs_device_userstate **pdu = NULL;
103         int    rc = 0;
104
105         pdu = (struct libcfs_device_userstate **)&file->private_data;
106         if (libcfs_psdev_ops.p_open != NULL)
107                 rc = libcfs_psdev_ops.p_open(0, (void *)pdu);
108         else
109                 return (-EPERM);
110         return rc;
111 }
112
113 /* called when closing /dev/device */
114 static int 
115 libcfs_psdev_release(struct inode *in, cfs_file_t * file)
116 {
117         struct libcfss_device_userstate *pdu;
118         int    rc = 0;
119
120         pdu = file->private_data;
121         if (libcfs_psdev_ops.p_close != NULL)
122                 rc = libcfs_psdev_ops.p_close(0, (void *)pdu);
123         else
124                 rc = -EPERM;
125         return rc;
126 }
127
128 static int 
129 libcfs_ioctl(cfs_file_t * file, unsigned int cmd, ulong_ptr_t arg)
130
131         struct cfs_psdev_file    pfile;
132         int    rc = 0;
133
134         if ( _IOC_TYPE(cmd) != IOC_LIBCFS_TYPE || 
135              _IOC_NR(cmd) < IOC_LIBCFS_MIN_NR  || 
136              _IOC_NR(cmd) > IOC_LIBCFS_MAX_NR ) { 
137                 CDEBUG(D_IOCTL, "invalid ioctl ( type %d, nr %d, size %d )\n", 
138                        _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd)); 
139                 return (-EINVAL); 
140         } 
141         
142         /* Handle platform-dependent IOC requests */
143         switch (cmd) { 
144         case IOC_LIBCFS_PANIC: 
145                 if (!cfs_capable(CFS_CAP_SYS_BOOT)) 
146                         return (-EPERM); 
147                 CERROR("debugctl-invoked panic");
148                 KeBugCheckEx('LUFS', (ULONG_PTR)libcfs_ioctl, (ULONG_PTR)NULL, (ULONG_PTR)NULL, (ULONG_PTR)NULL);
149
150                 return (0);
151         case IOC_LIBCFS_MEMHOG:
152
153                 if (!cfs_capable(CFS_CAP_SYS_ADMIN)) 
154                         return -EPERM;
155         break;
156         }
157
158         pfile.off = 0;
159         pfile.private_data = file->private_data;
160         if (libcfs_psdev_ops.p_ioctl != NULL) 
161                 rc = libcfs_psdev_ops.p_ioctl(&pfile, cmd, (void *)arg); 
162         else
163                 rc = -EPERM;
164         return (rc);
165 }
166
167 static struct file_operations libcfs_fops = {
168     /* owner */   THIS_MODULE,
169     /* lseek: */  NULL,
170     /* read: */   NULL,
171     /* write: */  NULL,
172     /* ioctl: */  libcfs_ioctl,
173     /* open: */   libcfs_psdev_open,
174     /* release:*/ libcfs_psdev_release
175 };
176
177 cfs_psdev_t libcfs_dev = { 
178         LIBCFS_MINOR, 
179         "lnet", 
180         &libcfs_fops
181 };