Whamcloud - gitweb
Landing b_hd_newconfig on HEAD
[fs/lustre-release.git] / lnet / libcfs / linux / linux-module.c
1 #define DEBUG_SUBSYSTEM S_LNET
2
3 #include <libcfs/libcfs.h>
4 #include <libcfs/kp30.h>
5
6 #define LNET_MINOR 240
7
8 int libcfs_ioctl_getdata(char *buf, char *end, void *arg)
9 {
10         struct libcfs_ioctl_hdr   *hdr;
11         struct libcfs_ioctl_data  *data;
12         int err;
13         ENTRY;
14
15         hdr = (struct libcfs_ioctl_hdr *)buf;
16         data = (struct libcfs_ioctl_data *)buf;
17
18         err = copy_from_user(buf, (void *)arg, sizeof(*hdr));
19         if (err)
20                 RETURN(err);
21
22         if (hdr->ioc_version != LIBCFS_IOCTL_VERSION) {
23                 CERROR("PORTALS: version mismatch kernel vs application\n");
24                 RETURN(-EINVAL);
25         }
26
27         if (hdr->ioc_len + buf >= end) {
28                 CERROR("PORTALS: user buffer exceeds kernel buffer\n");
29                 RETURN(-EINVAL);
30         }
31
32
33         if (hdr->ioc_len < sizeof(struct libcfs_ioctl_data)) {
34                 CERROR("PORTALS: user buffer too small for ioctl\n");
35                 RETURN(-EINVAL);
36         }
37
38         err = copy_from_user(buf, (void *)arg, hdr->ioc_len);
39         if (err)
40                 RETURN(err);
41
42         if (libcfs_ioctl_is_invalid(data)) {
43                 CERROR("PORTALS: ioctl not correctly formatted\n");
44                 RETURN(-EINVAL);
45         }
46
47         if (data->ioc_inllen1)
48                 data->ioc_inlbuf1 = &data->ioc_bulk[0];
49
50         if (data->ioc_inllen2)
51                 data->ioc_inlbuf2 = &data->ioc_bulk[0] +
52                         size_round(data->ioc_inllen1);
53
54         RETURN(0);
55 }
56
57 int libcfs_ioctl_popdata(void *arg, void *data, int size)
58 {
59         if (copy_to_user((char *)arg, data, size))
60                 return -EFAULT;
61         return 0;
62 }
63
64 extern struct cfs_psdev_ops          libcfs_psdev_ops;
65
66 static int
67 libcfs_psdev_open(struct inode * inode, struct file * file)
68 {
69         struct libcfs_device_userstate **pdu = NULL;
70         int    rc = 0;
71
72         if (!inode)
73                 return (-EINVAL);
74         pdu = (struct libcfs_device_userstate **)&file->private_data;
75         if (libcfs_psdev_ops.p_open != NULL)
76                 rc = libcfs_psdev_ops.p_open(0, (void *)pdu);
77         else
78                 return (-EPERM);
79         return rc;
80 }
81
82 /* called when closing /dev/device */
83 static int
84 libcfs_psdev_release(struct inode * inode, struct file * file)
85 {
86         struct libcfs_device_userstate *pdu;
87         int    rc = 0;
88
89         if (!inode)
90                 return (-EINVAL);
91         pdu = file->private_data;
92         if (libcfs_psdev_ops.p_close != NULL)
93                 rc = libcfs_psdev_ops.p_close(0, (void *)pdu);
94         else
95                 rc = -EPERM;
96         return rc;
97 }
98
99 static int
100 libcfs_ioctl(struct inode *inode, struct file *file,
101              unsigned int cmd, unsigned long arg)
102 {
103         struct cfs_psdev_file    pfile;
104         int    rc = 0;
105
106         if (current->fsuid != 0)
107                 return -EACCES;
108
109         if ( _IOC_TYPE(cmd) != IOC_LIBCFS_TYPE ||
110              _IOC_NR(cmd) < IOC_LIBCFS_MIN_NR  ||
111              _IOC_NR(cmd) > IOC_LIBCFS_MAX_NR ) {
112                 CDEBUG(D_IOCTL, "invalid ioctl ( type %d, nr %d, size %d )\n",
113                        _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd));
114                 return (-EINVAL);
115         }
116
117         /* Handle platform-dependent IOC requests */
118         switch (cmd) {
119         case IOC_LIBCFS_PANIC:
120                 if (!capable (CAP_SYS_BOOT))
121                         return (-EPERM);
122                 panic("debugctl-invoked panic");
123                 return (0);
124         case IOC_LIBCFS_MEMHOG:
125                 if (!capable (CAP_SYS_ADMIN))
126                         return -EPERM;
127                 /* go thought */
128         }
129
130         pfile.off = 0;
131         pfile.private_data = file->private_data;
132         if (libcfs_psdev_ops.p_ioctl != NULL)
133                 rc = libcfs_psdev_ops.p_ioctl(&pfile, cmd, (void *)arg);
134         else
135                 rc = -EPERM;
136         return (rc);
137 }
138
139 static struct file_operations libcfs_fops = {
140         ioctl:   libcfs_ioctl,
141         open:    libcfs_psdev_open,
142         release: libcfs_psdev_release
143 };
144
145 cfs_psdev_t libcfs_dev = {
146         LNET_MINOR,
147         "lnet",
148         &libcfs_fops
149 };
150
151