]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/lustre/lustre/libcfs/linux/linux-module.c
staging/lustre/libcfs: Adjust NULL comparison codestyle
[karo-tx-linux.git] / drivers / staging / lustre / lustre / libcfs / linux / linux-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  * Copyright (c) 2012, Intel Corporation.
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 "../../../include/linux/libcfs/libcfs.h"
40
41 #define LNET_MINOR 240
42
43 int libcfs_ioctl_getdata(char *buf, char *end, void __user *arg)
44 {
45         struct libcfs_ioctl_hdr   *hdr;
46         struct libcfs_ioctl_data  *data;
47         int orig_len;
48
49         hdr = (struct libcfs_ioctl_hdr *)buf;
50         data = (struct libcfs_ioctl_data *)buf;
51
52         if (copy_from_user(buf, arg, sizeof(*hdr)))
53                 return -EFAULT;
54
55         if (hdr->ioc_version != LIBCFS_IOCTL_VERSION) {
56                 CERROR("PORTALS: version mismatch kernel vs application\n");
57                 return -EINVAL;
58         }
59
60         if (hdr->ioc_len >= end - buf) {
61                 CERROR("PORTALS: user buffer exceeds kernel buffer\n");
62                 return -EINVAL;
63         }
64
65         if (hdr->ioc_len < sizeof(struct libcfs_ioctl_data)) {
66                 CERROR("PORTALS: user buffer too small for ioctl\n");
67                 return -EINVAL;
68         }
69
70         orig_len = hdr->ioc_len;
71         if (copy_from_user(buf, arg, hdr->ioc_len))
72                 return -EFAULT;
73         if (orig_len != data->ioc_len)
74                 return -EINVAL;
75
76         if (libcfs_ioctl_is_invalid(data)) {
77                 CERROR("PORTALS: ioctl not correctly formatted\n");
78                 return -EINVAL;
79         }
80
81         if (data->ioc_inllen1)
82                 data->ioc_inlbuf1 = &data->ioc_bulk[0];
83
84         if (data->ioc_inllen2)
85                 data->ioc_inlbuf2 = &data->ioc_bulk[0] +
86                         cfs_size_round(data->ioc_inllen1);
87
88         return 0;
89 }
90
91 int libcfs_ioctl_popdata(void __user *arg, void *data, int size)
92 {
93         if (copy_to_user(arg, data, size))
94                 return -EFAULT;
95         return 0;
96 }
97
98 static int
99 libcfs_psdev_open(struct inode *inode, struct file *file)
100 {
101         int    rc = 0;
102
103         if (!inode)
104                 return -EINVAL;
105         if (libcfs_psdev_ops.p_open)
106                 rc = libcfs_psdev_ops.p_open(0, NULL);
107         else
108                 return -EPERM;
109         return rc;
110 }
111
112 /* called when closing /dev/device */
113 static int
114 libcfs_psdev_release(struct inode *inode, struct file *file)
115 {
116         int    rc = 0;
117
118         if (!inode)
119                 return -EINVAL;
120         if (libcfs_psdev_ops.p_close)
121                 rc = libcfs_psdev_ops.p_close(0, NULL);
122         else
123                 rc = -EPERM;
124         return rc;
125 }
126
127 static long libcfs_ioctl(struct file *file,
128                          unsigned int cmd, unsigned long arg)
129 {
130         struct cfs_psdev_file    pfile;
131         int    rc = 0;
132
133         if (!capable(CAP_SYS_ADMIN))
134                 return -EACCES;
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 (!capable(CFS_CAP_SYS_BOOT))
148                         return -EPERM;
149                 panic("debugctl-invoked panic");
150                 return 0;
151         }
152
153         if (libcfs_psdev_ops.p_ioctl)
154                 rc = libcfs_psdev_ops.p_ioctl(&pfile, cmd, (void __user *)arg);
155         else
156                 rc = -EPERM;
157         return rc;
158 }
159
160 static const struct file_operations libcfs_fops = {
161         .unlocked_ioctl = libcfs_ioctl,
162         .open           = libcfs_psdev_open,
163         .release        = libcfs_psdev_release,
164 };
165
166 struct miscdevice libcfs_dev = {
167         .minor = LNET_MINOR,
168         .name = "lnet",
169         .fops = &libcfs_fops,
170 };