Whamcloud - gitweb
LU-15093 libcfs: Check if param_set_uint_minmax is provided
[fs/lustre-release.git] / libcfs / libcfs / util / l_ioctl.c
1 /*
2  * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
3  *
4  * Copyright (c) 2014, 2017, Intel Corporation.
5  *
6  *   This file is part of Lustre, https://wiki.whamcloud.com/
7  *
8  *   Portals is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Portals is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Portals; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22
23 #define __USE_FILE_OFFSET64
24
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/ioctl.h>
31 #include <sys/mman.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <unistd.h>
35 #include <linux/types.h>
36
37 #include <libcfs/util/ioctl.h>
38 #include <linux/lnet/lnetctl.h>
39
40 struct ioc_dev {
41         const char *dev_name;
42         int dev_fd;
43 };
44
45 static struct ioc_dev ioc_dev_list[10];
46
47 static int
48 open_ioc_dev(int dev_id)
49 {
50         const char *dev_name;
51
52         if (dev_id < 0 ||
53             dev_id >= sizeof(ioc_dev_list) / sizeof(ioc_dev_list[0]))
54                 return -EINVAL;
55
56         dev_name = ioc_dev_list[dev_id].dev_name;
57         if (!dev_name) {
58                 fprintf(stderr, "unknown device id: %d\n", dev_id);
59                 return -EINVAL;
60         }
61
62         if (ioc_dev_list[dev_id].dev_fd < 0) {
63                 int fd = open(dev_name, O_RDWR);
64
65                 if (fd < 0) {
66                         fprintf(stderr, "opening %s failed: %s\n"
67                                 "hint: the kernel modules may not be loaded\n",
68                                 dev_name, strerror(errno));
69                         return fd;
70                 }
71                 ioc_dev_list[dev_id].dev_fd = fd;
72         }
73
74         return ioc_dev_list[dev_id].dev_fd;
75 }
76
77 int l_ioctl(int dev_id, unsigned int opc, void *buf)
78 {
79         int fd, rc;
80
81         fd = open_ioc_dev(dev_id);
82         if (fd < 0)
83                 return fd;
84
85         rc = ioctl(fd, opc, buf);
86
87         return rc;
88 }
89
90 /* register a device to send ioctls to. */
91 int
92 register_ioc_dev(int dev_id, const char *dev_name)
93 {
94         if (dev_id < 0 ||
95             dev_id >= sizeof(ioc_dev_list) / sizeof(ioc_dev_list[0]))
96                 return -EINVAL;
97
98         unregister_ioc_dev(dev_id);
99
100         ioc_dev_list[dev_id].dev_name = dev_name;
101         ioc_dev_list[dev_id].dev_fd = -1;
102
103         return dev_id;
104 }
105
106 void
107 unregister_ioc_dev(int dev_id)
108 {
109         if (dev_id < 0 ||
110             dev_id >= sizeof(ioc_dev_list) / sizeof(ioc_dev_list[0]))
111                 return;
112
113         if (ioc_dev_list[dev_id].dev_name &&
114             ioc_dev_list[dev_id].dev_fd >= 0)
115                 close(ioc_dev_list[dev_id].dev_fd);
116
117         ioc_dev_list[dev_id].dev_name = NULL;
118         ioc_dev_list[dev_id].dev_fd = -1;
119 }
120
121 static inline size_t libcfs_ioctl_packlen(struct libcfs_ioctl_data *data)
122 {
123         size_t len = sizeof(*data);
124
125         len += (data->ioc_inllen1 + 7) & ~7;
126         len += (data->ioc_inllen2 + 7) & ~7;
127         return len;
128 }
129
130 int libcfs_ioctl_pack(struct libcfs_ioctl_data *data, char **pbuf, int max)
131 {
132         char *ptr;
133         struct libcfs_ioctl_data *overlay;
134
135         data->ioc_hdr.ioc_len = libcfs_ioctl_packlen(data);
136         data->ioc_hdr.ioc_version = LIBCFS_IOCTL_VERSION;
137
138         if (*pbuf && libcfs_ioctl_packlen(data) > max)
139                 return 1;
140         if (!*pbuf)
141                 *pbuf = malloc(data->ioc_hdr.ioc_len);
142         if (!*pbuf)
143                 return 1;
144         overlay = (struct libcfs_ioctl_data *)*pbuf;
145         memcpy(*pbuf, data, sizeof(*data));
146
147         ptr = overlay->ioc_bulk;
148         if (data->ioc_inlbuf1) {
149                 memcpy((char *)ptr, (const char *)data->ioc_inlbuf1,
150                        data->ioc_inllen1);
151                 ptr += ((data->ioc_inllen1 + 7) & ~7);
152         }
153         if (data->ioc_inlbuf2) {
154                 memcpy((char *)ptr, (const char *)data->ioc_inlbuf2,
155                        data->ioc_inllen2);
156                 ptr += ((data->ioc_inllen2 + 7) & ~7);
157         }
158
159         return 0;
160 }
161
162 void
163 libcfs_ioctl_unpack(struct libcfs_ioctl_data *data, char *pbuf)
164 {
165         struct libcfs_ioctl_data *overlay = (struct libcfs_ioctl_data *)pbuf;
166         char *ptr;
167
168         /* Preserve the caller's buffer pointers */
169         overlay->ioc_inlbuf1 = data->ioc_inlbuf1;
170         overlay->ioc_inlbuf2 = data->ioc_inlbuf2;
171
172         memcpy(data, pbuf, sizeof(*data));
173         ptr = &overlay->ioc_bulk[0];
174
175         if (data->ioc_inlbuf1) {
176                 memcpy((char *)data->ioc_inlbuf1, (const char *)ptr,
177                        data->ioc_inllen1);
178                 ptr += ((data->ioc_inllen1 + 7) & ~7);
179         }
180         if (data->ioc_inlbuf2) {
181                 memcpy((char *)data->ioc_inlbuf2, (const char *)ptr,
182                        data->ioc_inllen2);
183                 ptr += ((data->ioc_inllen2 + 7) & ~7);
184         }
185 }