2 * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
4 * Copyright (c) 2014, 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 <lnet/lnetctl.h>
45 static struct ioc_dev ioc_dev_list[10];
48 open_ioc_dev(int dev_id)
50 const char * dev_name;
53 dev_id >= sizeof(ioc_dev_list) / sizeof(ioc_dev_list[0]))
56 dev_name = ioc_dev_list[dev_id].dev_name;
57 if (dev_name == NULL) {
58 fprintf(stderr, "unknown device id: %d\n", dev_id);
62 if (ioc_dev_list[dev_id].dev_fd < 0) {
63 int fd = open(dev_name, O_RDWR);
66 fprintf(stderr, "opening %s failed: %s\n"
67 "hint: the kernel modules may not be loaded\n",
68 dev_name, strerror(errno));
71 ioc_dev_list[dev_id].dev_fd = fd;
74 return ioc_dev_list[dev_id].dev_fd;
78 int l_ioctl(int dev_id, unsigned int opc, void *buf)
82 fd = open_ioc_dev(dev_id);
86 rc = ioctl(fd, opc, buf);
91 /* register a device to send ioctls to. */
93 register_ioc_dev(int dev_id, const char *dev_name)
96 dev_id >= sizeof(ioc_dev_list) / sizeof(ioc_dev_list[0]))
99 unregister_ioc_dev(dev_id);
101 ioc_dev_list[dev_id].dev_name = dev_name;
102 ioc_dev_list[dev_id].dev_fd = -1;
108 unregister_ioc_dev(int dev_id)
111 dev_id >= sizeof(ioc_dev_list) / sizeof(ioc_dev_list[0]))
114 if (ioc_dev_list[dev_id].dev_name != NULL &&
115 ioc_dev_list[dev_id].dev_fd >= 0)
116 close(ioc_dev_list[dev_id].dev_fd);
118 ioc_dev_list[dev_id].dev_name = NULL;
119 ioc_dev_list[dev_id].dev_fd = -1;
122 static inline size_t libcfs_ioctl_packlen(struct libcfs_ioctl_data *data)
124 size_t len = sizeof(*data);
126 len += (data->ioc_inllen1 + 7) & ~7;
127 len += (data->ioc_inllen2 + 7) & ~7;
131 int libcfs_ioctl_pack(struct libcfs_ioctl_data *data, char **pbuf,
135 struct libcfs_ioctl_data *overlay;
136 data->ioc_hdr.ioc_len = libcfs_ioctl_packlen(data);
137 data->ioc_hdr.ioc_version = LIBCFS_IOCTL_VERSION;
139 if (*pbuf != NULL && libcfs_ioctl_packlen(data) > max)
142 *pbuf = malloc(data->ioc_hdr.ioc_len);
145 overlay = (struct libcfs_ioctl_data *)*pbuf;
146 memcpy(*pbuf, data, sizeof(*data));
148 ptr = overlay->ioc_bulk;
149 if (data->ioc_inlbuf1 != NULL) {
150 memcpy((char *)ptr, (const char *)data->ioc_inlbuf1,
152 ptr += ((data->ioc_inllen1 + 7) & ~7);
154 if (data->ioc_inlbuf2 != NULL) {
155 memcpy((char *)ptr, (const char *)data->ioc_inlbuf2,
157 ptr += ((data->ioc_inllen2 + 7) & ~7);
164 libcfs_ioctl_unpack(struct libcfs_ioctl_data *data, char *pbuf)
166 struct libcfs_ioctl_data *overlay = (struct libcfs_ioctl_data *)pbuf;
169 /* Preserve the caller's buffer pointers */
170 overlay->ioc_inlbuf1 = data->ioc_inlbuf1;
171 overlay->ioc_inlbuf2 = data->ioc_inlbuf2;
173 memcpy(data, pbuf, sizeof(*data));
174 ptr = &overlay->ioc_bulk[0];
176 if (data->ioc_inlbuf1 != NULL) {
177 memcpy((char *)data->ioc_inlbuf1, (const char *)ptr,
179 ptr += ((data->ioc_inllen1 + 7) & ~7);
181 if (data->ioc_inlbuf2 != NULL) {
182 memcpy((char *)data->ioc_inlbuf2, (const char *)ptr,
184 ptr += ((data->ioc_inllen2 + 7) & ~7);