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 open_ioc_dev(int dev_id)
53 dev_id >= sizeof(ioc_dev_list) / sizeof(ioc_dev_list[0]))
56 dev_name = ioc_dev_list[dev_id].dev_name;
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;
77 int l_ioctl(int dev_id, unsigned int opc, void *buf)
81 fd = open_ioc_dev(dev_id);
85 rc = ioctl(fd, opc, buf);
90 /* register a device to send ioctls to. */
92 register_ioc_dev(int dev_id, const char *dev_name)
95 dev_id >= sizeof(ioc_dev_list) / sizeof(ioc_dev_list[0]))
98 unregister_ioc_dev(dev_id);
100 ioc_dev_list[dev_id].dev_name = dev_name;
101 ioc_dev_list[dev_id].dev_fd = -1;
107 unregister_ioc_dev(int dev_id)
110 dev_id >= sizeof(ioc_dev_list) / sizeof(ioc_dev_list[0]))
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);
117 ioc_dev_list[dev_id].dev_name = NULL;
118 ioc_dev_list[dev_id].dev_fd = -1;
121 static inline size_t libcfs_ioctl_packlen(struct libcfs_ioctl_data *data)
123 size_t len = sizeof(*data);
125 len += (data->ioc_inllen1 + 7) & ~7;
126 len += (data->ioc_inllen2 + 7) & ~7;
130 int libcfs_ioctl_pack(struct libcfs_ioctl_data *data, char **pbuf, int max)
133 struct libcfs_ioctl_data *overlay;
135 data->ioc_hdr.ioc_len = libcfs_ioctl_packlen(data);
136 data->ioc_hdr.ioc_version = LIBCFS_IOCTL_VERSION;
138 if (*pbuf && libcfs_ioctl_packlen(data) > max)
141 *pbuf = malloc(data->ioc_hdr.ioc_len);
144 overlay = (struct libcfs_ioctl_data *)*pbuf;
145 memcpy(*pbuf, data, sizeof(*data));
147 ptr = overlay->ioc_bulk;
148 if (data->ioc_inlbuf1) {
149 memcpy((char *)ptr, (const char *)data->ioc_inlbuf1,
151 ptr += ((data->ioc_inllen1 + 7) & ~7);
153 if (data->ioc_inlbuf2) {
154 memcpy((char *)ptr, (const char *)data->ioc_inlbuf2,
156 ptr += ((data->ioc_inllen2 + 7) & ~7);
163 libcfs_ioctl_unpack(struct libcfs_ioctl_data *data, char *pbuf)
165 struct libcfs_ioctl_data *overlay = (struct libcfs_ioctl_data *)pbuf;
168 /* Preserve the caller's buffer pointers */
169 overlay->ioc_inlbuf1 = data->ioc_inlbuf1;
170 overlay->ioc_inlbuf2 = data->ioc_inlbuf2;
172 memcpy(data, pbuf, sizeof(*data));
173 ptr = &overlay->ioc_bulk[0];
175 if (data->ioc_inlbuf1) {
176 memcpy((char *)data->ioc_inlbuf1, (const char *)ptr,
178 ptr += ((data->ioc_inllen1 + 7) & ~7);
180 if (data->ioc_inlbuf2) {
181 memcpy((char *)data->ioc_inlbuf2, (const char *)ptr,
183 ptr += ((data->ioc_inllen2 + 7) & ~7);