Whamcloud - gitweb
Mass conversion of all copyright messages to Oracle.
[fs/lustre-release.git] / libcfs / libcfs / winnt / winnt-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 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37
38 #define DEBUG_SUBSYSTEM S_LNET
39
40 #include <libcfs/libcfs.h>
41
42 #define LIBCFS_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 = cfs_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("LIBCFS: version mismatch kernel vs application\n");
60                 RETURN(-EINVAL);
61         }
62
63         if (hdr->ioc_len + buf >= end) {
64                 CERROR("LIBCFS: user buffer exceeds kernel buffer\n");
65                 RETURN(-EINVAL);
66         }
67
68         if (hdr->ioc_len < sizeof(struct libcfs_ioctl_data)) {
69                 CERROR("LIBCFS: user buffer too small for ioctl\n");
70                 RETURN(-EINVAL);
71         }
72
73         err = cfs_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("LIBCFS: 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 (cfs_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 *in, cfs_file_t * file)
103
104         struct libcfs_device_userstate **pdu = NULL;
105         int    rc = 0;
106
107         pdu = (struct libcfs_device_userstate **)&file->private_data;
108         if (libcfs_psdev_ops.p_open != NULL)
109                 rc = libcfs_psdev_ops.p_open(0, (void *)pdu);
110         else
111                 return (-EPERM);
112         return rc;
113 }
114
115 /* called when closing /dev/device */
116 static int 
117 libcfs_psdev_release(struct inode *in, cfs_file_t * file)
118 {
119         struct libcfss_device_userstate *pdu;
120         int    rc = 0;
121
122         pdu = file->private_data;
123         if (libcfs_psdev_ops.p_close != NULL)
124                 rc = libcfs_psdev_ops.p_close(0, (void *)pdu);
125         else
126                 rc = -EPERM;
127         return rc;
128 }
129
130 static int 
131 libcfs_ioctl(cfs_file_t * file, unsigned int cmd, ulong_ptr_t arg)
132
133         struct cfs_psdev_file    pfile;
134         int    rc = 0;
135
136         if ( _IOC_TYPE(cmd) != IOC_LIBCFS_TYPE || 
137              _IOC_NR(cmd) < IOC_LIBCFS_MIN_NR  || 
138              _IOC_NR(cmd) > IOC_LIBCFS_MAX_NR ) { 
139                 CDEBUG(D_IOCTL, "invalid ioctl ( type %d, nr %d, size %d )\n", 
140                        _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd)); 
141                 return (-EINVAL); 
142         } 
143         
144         /* Handle platform-dependent IOC requests */
145         switch (cmd) { 
146         case IOC_LIBCFS_PANIC: 
147                 if (!cfs_capable(CFS_CAP_SYS_BOOT)) 
148                         return (-EPERM); 
149                 CERROR("debugctl-invoked panic");
150                 KeBugCheckEx('LUFS', (ULONG_PTR)libcfs_ioctl, (ULONG_PTR)NULL, (ULONG_PTR)NULL, (ULONG_PTR)NULL);
151
152                 return (0);
153         case IOC_LIBCFS_MEMHOG:
154
155                 if (!cfs_capable(CFS_CAP_SYS_ADMIN)) 
156                         return -EPERM;
157         break;
158         }
159
160         pfile.off = 0;
161         pfile.private_data = file->private_data;
162         if (libcfs_psdev_ops.p_ioctl != NULL) 
163                 rc = libcfs_psdev_ops.p_ioctl(&pfile, cmd, (void *)arg); 
164         else
165                 rc = -EPERM;
166         return (rc);
167 }
168
169 static struct file_operations libcfs_fops = {
170     /* owner */   THIS_MODULE,
171     /* lseek: */  NULL,
172     /* read: */   NULL,
173     /* write: */  NULL,
174     /* ioctl: */  libcfs_ioctl,
175     /* open: */   libcfs_psdev_open,
176     /* release:*/ libcfs_psdev_release
177 };
178
179 cfs_psdev_t libcfs_dev = { 
180         LIBCFS_MINOR, 
181         "lnet", 
182         &libcfs_fops
183 };