Whamcloud - gitweb
Branch HEAD
[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  *
5  *  Copyright (c) 2004 Cluster File Systems, Inc.
6  *
7  *   This file is part of Lustre, http://www.lustre.org.
8  *
9  *   Lustre is free software; you can redistribute it and/or modify it under
10  *   the terms of version 2 of the GNU General Public License as published by
11  *   the Free Software Foundation. Lustre is distributed in the hope that it
12  *   will be useful, but WITHOUT ANY WARRANTY; without even the implied
13  *   warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details. You should have received a
15  *   copy of the GNU General Public License along with Lustre; if not, write
16  *   to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
17  *   USA.
18  */
19
20
21 #define DEBUG_SUBSYSTEM S_LIBCFS
22
23 #include <libcfs/libcfs.h>
24
25 #define LIBCFS_MINOR 240
26
27 int libcfs_ioctl_getdata(char *buf, char *end, void *arg)
28 {
29         struct libcfs_ioctl_hdr *hdr;
30         struct libcfs_ioctl_data *data;
31         int err;
32         ENTRY;
33
34         hdr = (struct libcfs_ioctl_hdr *)buf;
35         data = (struct libcfs_ioctl_data *)buf;
36
37         err = copy_from_user(buf, (void *)arg, sizeof(*hdr));
38         if (err)
39                 RETURN(err);
40
41         if (hdr->ioc_version != LIBCFS_IOCTL_VERSION) {
42                 CERROR(("LIBCFS: version mismatch kernel vs application\n"));
43                 RETURN(-EINVAL);
44         }
45
46         if (hdr->ioc_len + buf >= end) {
47                 CERROR(("LIBCFS: user buffer exceeds kernel buffer\n"));
48                 RETURN(-EINVAL);
49         }
50
51         if (hdr->ioc_len < sizeof(struct libcfs_ioctl_data)) {
52                 CERROR(("LIBCFS: user buffer too small for ioctl\n"));
53                 RETURN(-EINVAL);
54         }
55
56         err = copy_from_user(buf, (void *)arg, hdr->ioc_len);
57         if (err)
58                 RETURN(err);
59
60         if (libcfs_ioctl_is_invalid(data)) {
61                 CERROR(("LIBCFS: ioctl not correctly formatted\n"));
62                 RETURN(-EINVAL);
63         }
64
65         if (data->ioc_inllen1)
66                 data->ioc_inlbuf1 = &data->ioc_bulk[0];
67
68         if (data->ioc_inllen2)
69                 data->ioc_inlbuf2 = &data->ioc_bulk[0] +
70                         size_round(data->ioc_inllen1);
71
72         RETURN(0);
73 }
74                                                                                                                                                                         
75 extern struct cfs_psdev_ops          libcfs_psdev_ops;
76
77 static int 
78 libcfs_psdev_open(cfs_file_t * file)
79
80         struct libcfs_device_userstate **pdu = NULL;
81         int    rc = 0;
82
83         pdu = (struct libcfs_device_userstate **)&file->private_data;
84         if (libcfs_psdev_ops.p_open != NULL)
85                 rc = libcfs_psdev_ops.p_open(0, (void *)pdu);
86         else
87                 return (-EPERM);
88         return rc;
89 }
90
91 /* called when closing /dev/device */
92 static int 
93 libcfs_psdev_release(cfs_file_t * file)
94 {
95         struct libcfss_device_userstate *pdu;
96         int    rc = 0;
97
98         pdu = file->private_data;
99         if (libcfs_psdev_ops.p_close != NULL)
100                 rc = libcfs_psdev_ops.p_close(0, (void *)pdu);
101         else
102                 rc = -EPERM;
103         return rc;
104 }
105
106 static int 
107 libcfs_ioctl(cfs_file_t * file, unsigned int cmd, ulong_ptr arg)
108
109         struct cfs_psdev_file    pfile;
110         int    rc = 0;
111
112         if ( _IOC_TYPE(cmd) != IOC_LIBCFS_TYPE || 
113              _IOC_NR(cmd) < IOC_LIBCFS_MIN_NR  || 
114              _IOC_NR(cmd) > IOC_LIBCFS_MAX_NR ) { 
115                 CDEBUG(D_IOCTL, ("invalid ioctl ( type %d, nr %d, size %d )\n", 
116                        _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd))); 
117                 return (-EINVAL); 
118         } 
119         
120         /* Handle platform-dependent IOC requests */
121         switch (cmd) { 
122         case IOC_LIBCFS_PANIC: 
123                 if (!capable (CAP_SYS_BOOT)) 
124                         return (-EPERM); 
125                 CERROR(("debugctl-invoked panic"));
126         KeBugCheckEx('LUFS', (ULONG_PTR)libcfs_ioctl, (ULONG_PTR)NULL, (ULONG_PTR)NULL, (ULONG_PTR)NULL);
127
128                 return (0);
129         case IOC_LIBCFS_MEMHOG:
130
131                 if (!capable (CAP_SYS_ADMIN)) 
132                         return -EPERM;
133         break;
134         }
135
136         pfile.off = 0;
137         pfile.private_data = file->private_data;
138         if (libcfs_psdev_ops.p_ioctl != NULL) 
139                 rc = libcfs_psdev_ops.p_ioctl(&pfile, cmd, (void *)arg); 
140         else
141                 rc = -EPERM;
142         return (rc);
143 }
144
145 static struct file_operations libcfs_fops = {
146     /* lseek: */  NULL,
147     /* read: */   NULL,
148     /* write: */  NULL,
149     /* ioctl: */  libcfs_ioctl,
150     /* open: */   libcfs_psdev_open,
151     /* release:*/ libcfs_psdev_release
152 };
153
154 cfs_psdev_t libcfs_dev = { 
155         LIBCFS_MINOR, 
156         "lnet", 
157         &libcfs_fops
158 };
159