2 * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
4 * Copyright (c) 2014, Intel Corporation.
6 * This file is part of Portals, http://www.sf.net/projects/lustre/
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
25 #include <sys/ioctl.h>
27 #include <libcfs/libcfsutil.h>
28 #include <lnet/lnetctl.h>
30 static ioc_handler_t do_ioctl; /* forward ref */
31 static ioc_handler_t *current_ioc_handler = &do_ioctl;
34 const char * dev_name;
40 static struct ioc_dev ioc_dev_list[10];
51 set_ioc_handler (ioc_handler_t *handler)
54 current_ioc_handler = do_ioctl;
56 current_ioc_handler = handler;
59 /* Catamount has no <linux/kdev_t.h>, so just define it here */
61 # define MKDEV(a,b) (((a) << 8) | (b))
65 open_ioc_dev(int dev_id)
67 const char * dev_name;
70 dev_id >= sizeof(ioc_dev_list) / sizeof(ioc_dev_list[0]))
73 dev_name = ioc_dev_list[dev_id].dev_name;
74 if (dev_name == NULL) {
75 fprintf(stderr, "unknown device id: %d\n", dev_id);
79 if (ioc_dev_list[dev_id].dev_fd < 0) {
80 int fd = open(dev_name, O_RDWR);
82 /* Make the /dev/ node if we need to */
83 if (fd < 0 && errno == ENOENT) {
84 if (mknod(dev_name, S_IFCHR|S_IWUSR|S_IRUSR,
85 MKDEV(ioc_dev_list[dev_id].dev_major,
86 ioc_dev_list[dev_id].dev_minor)) == 0)
87 fd = open(dev_name, O_RDWR);
89 fprintf(stderr, "mknod %s failed: %s\n",
90 dev_name, strerror(errno));
94 fprintf(stderr, "opening %s failed: %s\n"
95 "hint: the kernel modules may not be loaded\n",
96 dev_name, strerror(errno));
99 ioc_dev_list[dev_id].dev_fd = fd;
102 return ioc_dev_list[dev_id].dev_fd;
107 do_ioctl(int dev_id, unsigned int opc, void *buf)
111 fd = open_ioc_dev(dev_id);
115 rc = ioctl(fd, opc, buf);
125 if (!dump_filename) {
126 fprintf(stderr, "no dump filename\n");
128 fp = fopen(dump_filename, "a");
133 * The dump file should start with a description of which devices are
134 * used, but for now it will assumed whatever app reads the file will
135 * know what to do. */
137 dump(int dev_id, unsigned int opc, void *buf)
140 struct dump_hdr dump_hdr;
141 struct libcfs_ioctl_hdr * ioc_hdr = (struct libcfs_ioctl_hdr *) buf;
144 printf("dumping opc %x to %s\n", opc, dump_filename);
147 dump_hdr.magic = 0xdeadbeef;
148 dump_hdr.dev_id = dev_id;
151 fp = get_dump_file();
153 fprintf(stderr, "%s: %s\n", dump_filename,
158 rc = fwrite(&dump_hdr, sizeof(dump_hdr), 1, fp);
160 rc = fwrite(buf, ioc_hdr->ioc_len, 1, fp);
163 fprintf(stderr, "%s: %s\n", dump_filename,
171 /* register a device to send ioctls to. */
173 register_ioc_dev(int dev_id, const char * dev_name, int major, int minor)
177 dev_id >= sizeof(ioc_dev_list) / sizeof(ioc_dev_list[0]))
180 unregister_ioc_dev(dev_id);
182 ioc_dev_list[dev_id].dev_name = dev_name;
183 ioc_dev_list[dev_id].dev_fd = -1;
184 ioc_dev_list[dev_id].dev_major = major;
185 ioc_dev_list[dev_id].dev_minor = minor;
191 unregister_ioc_dev(int dev_id)
194 dev_id >= sizeof(ioc_dev_list) / sizeof(ioc_dev_list[0]))
197 if (ioc_dev_list[dev_id].dev_name != NULL &&
198 ioc_dev_list[dev_id].dev_fd >= 0)
199 close(ioc_dev_list[dev_id].dev_fd);
201 ioc_dev_list[dev_id].dev_name = NULL;
202 ioc_dev_list[dev_id].dev_fd = -1;
205 /* If this file is set, then all ioctl buffers will be
206 appended to the file. */
208 set_ioctl_dump(char * file)
213 dump_filename = strdup(file);
214 if (dump_filename == NULL)
217 set_ioc_handler(&dump);
222 l_ioctl(int dev_id, unsigned int opc, void *buf)
224 return current_ioc_handler(dev_id, opc, buf);
227 /* Read an ioctl dump file, and call the ioc_func for each ioctl buffer
228 * in the file. For example:
230 * parse_dump("lctl.dump", l_ioctl);
232 * Note: if using l_ioctl, then you also need to register_ioc_dev() for
233 * each device used in the dump.
236 parse_dump(char * dump_file, ioc_handler_t ioc_func)
239 char *start, *buf, *end;
243 fd = open(dump_file, O_RDONLY);
245 fprintf(stderr, "couldn't open %s: %s\n", dump_file,
250 if (fstat(fd, &st)) {
251 perror("stat fails");
255 if (st.st_size < 1) {
256 fprintf(stderr, "KML is empty\n");
260 start = buf = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE , fd, 0);
261 end = start + st.st_size;
263 if (start == MAP_FAILED) {
264 fprintf(stderr, "can't create file mapping\n");
269 struct dump_hdr *dump_hdr = (struct dump_hdr *) buf;
270 struct libcfs_ioctl_hdr * data;
276 data = (struct libcfs_ioctl_hdr *) (buf + sizeof(*dump_hdr));
277 if (buf + data->ioc_len > end ) {
278 fprintf(stderr, "dump file overflow, %p + %d > %p\n", buf,
283 printf ("dump_hdr: %lx data: %lx\n",
284 (unsigned long)dump_hdr - (unsigned long)buf, (unsigned long)data - (unsigned long)buf);
286 printf("%d: opcode %x len: %d ver: %x ", line, dump_hdr->opc,
287 data->ioc_len, data->ioc_version);
290 memcpy(tmp, data, data->ioc_len);
292 rc = ioc_func(dump_hdr->dev_id, dump_hdr->opc, tmp);
294 printf("failed: %d\n", rc);
298 buf += data->ioc_len + sizeof(*dump_hdr);
301 munmap(start, end - start);
307 jt_ioc_dump(int argc, char **argv)
310 fprintf(stderr, "usage: %s [hostname]\n", argv[0]);
313 printf("setting dumpfile to: %s\n", argv[1]);
315 set_ioctl_dump(argv[1]);
319 int libcfs_ioctl_pack(struct libcfs_ioctl_data *data, char **pbuf,
323 struct libcfs_ioctl_data *overlay;
324 data->ioc_hdr.ioc_len = libcfs_ioctl_packlen(data);
325 data->ioc_hdr.ioc_version = LIBCFS_IOCTL_VERSION;
327 if (*pbuf != NULL && libcfs_ioctl_packlen(data) > max)
330 *pbuf = malloc(data->ioc_hdr.ioc_len);
333 overlay = (struct libcfs_ioctl_data *)*pbuf;
334 memcpy(*pbuf, data, sizeof(*data));
336 ptr = overlay->ioc_bulk;
337 if (data->ioc_inlbuf1 != NULL)
338 LOGL(data->ioc_inlbuf1, data->ioc_inllen1, ptr);
339 if (data->ioc_inlbuf2 != NULL)
340 LOGL(data->ioc_inlbuf2, data->ioc_inllen2, ptr);
341 if (libcfs_ioctl_is_invalid(overlay))
348 libcfs_ioctl_unpack(struct libcfs_ioctl_data *data, char *pbuf)
350 struct libcfs_ioctl_data *overlay = (struct libcfs_ioctl_data *)pbuf;
353 /* Preserve the caller's buffer pointers */
354 overlay->ioc_inlbuf1 = data->ioc_inlbuf1;
355 overlay->ioc_inlbuf2 = data->ioc_inlbuf2;
357 memcpy(data, pbuf, sizeof(*data));
358 ptr = &overlay->ioc_bulk[0];
360 if (data->ioc_inlbuf1 != NULL)
361 LOGU(data->ioc_inlbuf1, data->ioc_inllen1, ptr);
362 if (data->ioc_inlbuf2 != NULL)
363 LOGU(data->ioc_inlbuf2, data->ioc_inllen2, ptr);