2 * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
4 * Copyright (c) 2014, 2017, Intel Corporation.
6 * This file is part of Lustre, https://wiki.whamcloud.com/
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.
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.
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.
23 #define __USE_FILE_OFFSET64
30 #include <sys/ioctl.h>
32 #include <sys/types.h>
35 #include <linux/types.h>
37 #include <libcfs/util/ioctl.h>
38 #include <linux/lnet/lnetctl.h>
45 static struct ioc_dev ioc_dev_list[10];
48 # define ARRAY_SIZE(a) ((sizeof(a)) / (sizeof((a)[0])))
49 #endif /* !ARRAY_SIZE */
52 open_ioc_dev(int dev_id)
56 if (dev_id < 0 || dev_id >= ARRAY_SIZE(ioc_dev_list)) {
61 dev_name = ioc_dev_list[dev_id].dev_name;
63 fprintf(stderr, "unknown device id: %d\n", dev_id);
68 if (ioc_dev_list[dev_id].dev_fd < 0) {
69 int fd = open(dev_name, O_RDWR);
72 fprintf(stderr, "opening %s failed: %s\n"
73 "hint: the kernel modules may not be loaded\n",
74 dev_name, strerror(errno));
77 ioc_dev_list[dev_id].dev_fd = fd;
80 return ioc_dev_list[dev_id].dev_fd;
83 int l_ioctl(int dev_id, unsigned int opc, void *buf)
87 fd = open_ioc_dev(dev_id);
91 rc = ioctl(fd, opc, buf);
93 return rc < 0 ? -errno : rc;
96 /* register a device to send ioctls to. */
98 register_ioc_dev(int dev_id, const char *dev_name)
101 dev_id >= sizeof(ioc_dev_list) / sizeof(ioc_dev_list[0]))
104 unregister_ioc_dev(dev_id);
106 ioc_dev_list[dev_id].dev_name = dev_name;
107 ioc_dev_list[dev_id].dev_fd = -1;
113 unregister_ioc_dev(int dev_id)
116 dev_id >= sizeof(ioc_dev_list) / sizeof(ioc_dev_list[0]))
119 if (ioc_dev_list[dev_id].dev_name &&
120 ioc_dev_list[dev_id].dev_fd >= 0)
121 close(ioc_dev_list[dev_id].dev_fd);
123 ioc_dev_list[dev_id].dev_name = NULL;
124 ioc_dev_list[dev_id].dev_fd = -1;
127 static inline size_t libcfs_ioctl_packlen(struct libcfs_ioctl_data *data)
129 size_t len = sizeof(*data);
131 len += (data->ioc_inllen1 + 7) & ~7;
132 len += (data->ioc_inllen2 + 7) & ~7;
136 int libcfs_ioctl_pack(struct libcfs_ioctl_data *data, char **pbuf, int max)
139 struct libcfs_ioctl_data *overlay;
141 data->ioc_hdr.ioc_len = libcfs_ioctl_packlen(data);
142 data->ioc_hdr.ioc_version = LNET_IOCTL_VERSION;
144 if (*pbuf && libcfs_ioctl_packlen(data) > max)
147 *pbuf = malloc(data->ioc_hdr.ioc_len);
150 overlay = (struct libcfs_ioctl_data *)*pbuf;
151 memcpy(*pbuf, data, sizeof(*data));
153 ptr = overlay->ioc_bulk;
154 if (data->ioc_inlbuf1) {
155 memcpy((char *)ptr, (const char *)data->ioc_inlbuf1,
157 ptr += ((data->ioc_inllen1 + 7) & ~7);
159 if (data->ioc_inlbuf2) {
160 memcpy((char *)ptr, (const char *)data->ioc_inlbuf2,
162 ptr += ((data->ioc_inllen2 + 7) & ~7);
169 libcfs_ioctl_unpack(struct libcfs_ioctl_data *data, char *pbuf)
171 struct libcfs_ioctl_data *overlay = (struct libcfs_ioctl_data *)pbuf;
174 /* Preserve the caller's buffer pointers */
175 overlay->ioc_inlbuf1 = data->ioc_inlbuf1;
176 overlay->ioc_inlbuf2 = data->ioc_inlbuf2;
178 memcpy(data, pbuf, sizeof(*data));
179 ptr = &overlay->ioc_bulk[0];
181 if (data->ioc_inlbuf1) {
182 memcpy((char *)data->ioc_inlbuf1, (const char *)ptr,
184 ptr += ((data->ioc_inllen1 + 7) & ~7);
186 if (data->ioc_inlbuf2) {
187 memcpy((char *)data->ioc_inlbuf2, (const char *)ptr,
189 ptr += ((data->ioc_inllen2 + 7) & ~7);