Whamcloud - gitweb
c3b7e1cd8fb17783c746113eee76b1a1c2994402
[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.hpdd.intel.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 == NULL) {
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
78 int l_ioctl(int dev_id, unsigned int opc, void *buf)
79 {
80         int fd, rc;
81
82         fd = open_ioc_dev(dev_id);
83         if (fd < 0)
84                 return fd;
85
86         rc = ioctl(fd, opc, buf);
87
88         return rc;
89 }
90
91 /* register a device to send ioctls to.  */
92 int
93 register_ioc_dev(int dev_id, const char *dev_name)
94 {
95         if (dev_id < 0 ||
96             dev_id >= sizeof(ioc_dev_list) / sizeof(ioc_dev_list[0]))
97                 return -EINVAL;
98
99         unregister_ioc_dev(dev_id);
100
101         ioc_dev_list[dev_id].dev_name = dev_name;
102         ioc_dev_list[dev_id].dev_fd = -1;
103
104         return dev_id;
105 }
106
107 void
108 unregister_ioc_dev(int dev_id)
109 {
110         if (dev_id < 0 ||
111             dev_id >= sizeof(ioc_dev_list) / sizeof(ioc_dev_list[0]))
112                 return;
113
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);
117
118         ioc_dev_list[dev_id].dev_name = NULL;
119         ioc_dev_list[dev_id].dev_fd = -1;
120 }
121
122 static inline size_t libcfs_ioctl_packlen(struct libcfs_ioctl_data *data)
123 {
124         size_t len = sizeof(*data);
125
126         len += (data->ioc_inllen1 + 7) & ~7;
127         len += (data->ioc_inllen2 + 7) & ~7;
128         return len;
129 }
130
131 int libcfs_ioctl_pack(struct libcfs_ioctl_data *data, char **pbuf,
132                                     int max)
133 {
134         char *ptr;
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;
138
139         if (*pbuf != NULL && libcfs_ioctl_packlen(data) > max)
140                 return 1;
141         if (*pbuf == NULL)
142                 *pbuf = malloc(data->ioc_hdr.ioc_len);
143         if (*pbuf == NULL)
144                 return 1;
145         overlay = (struct libcfs_ioctl_data *)*pbuf;
146         memcpy(*pbuf, data, sizeof(*data));
147
148         ptr = overlay->ioc_bulk;
149         if (data->ioc_inlbuf1 != NULL) {
150                 memcpy((char *)ptr, (const char *)data->ioc_inlbuf1,
151                        data->ioc_inllen1);
152                 ptr += ((data->ioc_inllen1 + 7) & ~7);
153         }
154         if (data->ioc_inlbuf2 != NULL) {
155                 memcpy((char *)ptr, (const char *)data->ioc_inlbuf2,
156                        data->ioc_inllen2);
157                 ptr += ((data->ioc_inllen2 + 7) & ~7);
158         }
159
160         return 0;
161 }
162
163 void
164 libcfs_ioctl_unpack(struct libcfs_ioctl_data *data, char *pbuf)
165 {
166         struct libcfs_ioctl_data *overlay = (struct libcfs_ioctl_data *)pbuf;
167         char *ptr;
168
169         /* Preserve the caller's buffer pointers */
170         overlay->ioc_inlbuf1 = data->ioc_inlbuf1;
171         overlay->ioc_inlbuf2 = data->ioc_inlbuf2;
172
173         memcpy(data, pbuf, sizeof(*data));
174         ptr = &overlay->ioc_bulk[0];
175
176         if (data->ioc_inlbuf1 != NULL) {
177                 memcpy((char *)data->ioc_inlbuf1, (const char *)ptr,
178                        data->ioc_inllen1);
179                 ptr += ((data->ioc_inllen1 + 7) & ~7);
180         }
181         if (data->ioc_inlbuf2 != NULL) {
182                 memcpy((char *)data->ioc_inlbuf2, (const char *)ptr,
183                        data->ioc_inllen2);
184                 ptr += ((data->ioc_inllen2 + 7) & ~7);
185         }
186 }