1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
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
28 #include <sys/types.h>
32 #include <sys/ioctl.h>
37 #include <libcfs/libcfsutil.h>
39 #include <lnet/api-support.h>
40 #include <lnet/lnetctl.h>
41 #include <libcfs/portals_utils.h>
44 static ioc_handler_t do_ioctl; /* forward ref */
45 static ioc_handler_t *current_ioc_handler = &do_ioctl;
48 const char * dev_name;
54 static struct ioc_dev ioc_dev_list[10];
65 set_ioc_handler (ioc_handler_t *handler)
68 current_ioc_handler = do_ioctl;
70 current_ioc_handler = handler;
73 /* Catamount has no <linux/kdev_t.h>, so just define it here */
75 # define MKDEV(a,b) (((a) << 8) | (b))
79 open_ioc_dev(int dev_id)
81 const char * dev_name;
84 dev_id >= sizeof(ioc_dev_list) / sizeof(ioc_dev_list[0]))
87 dev_name = ioc_dev_list[dev_id].dev_name;
88 if (dev_name == NULL) {
89 fprintf(stderr, "unknown device id: %d\n", dev_id);
93 if (ioc_dev_list[dev_id].dev_fd < 0) {
94 int fd = open(dev_name, O_RDWR);
96 /* Make the /dev/ node if we need to */
97 if (fd < 0 && errno == ENOENT) {
99 S_IFCHR|S_IWUSR|S_IRUSR,
100 MKDEV(ioc_dev_list[dev_id].dev_major,
101 ioc_dev_list[dev_id].dev_minor)) == 0)
102 fd = open(dev_name, O_RDWR);
104 fprintf(stderr, "mknod %s failed: %s\n",
105 dev_name, strerror(errno));
109 fprintf(stderr, "opening %s failed: %s\n"
110 "hint: the kernel modules may not be loaded\n",
111 dev_name, strerror(errno));
114 ioc_dev_list[dev_id].dev_fd = fd;
117 return ioc_dev_list[dev_id].dev_fd;
122 do_ioctl(int dev_id, unsigned int opc, void *buf)
126 fd = open_ioc_dev(dev_id);
130 rc = ioctl(fd, opc, buf);
140 if (!dump_filename) {
141 fprintf(stderr, "no dump filename\n");
143 fp = fopen(dump_filename, "a");
148 * The dump file should start with a description of which devices are
149 * used, but for now it will assumed whatever app reads the file will
150 * know what to do. */
152 dump(int dev_id, unsigned int opc, void *buf)
155 struct dump_hdr dump_hdr;
156 struct libcfs_ioctl_hdr * ioc_hdr = (struct libcfs_ioctl_hdr *) buf;
159 printf("dumping opc %x to %s\n", opc, dump_filename);
162 dump_hdr.magic = 0xdeadbeef;
163 dump_hdr.dev_id = dev_id;
166 fp = get_dump_file();
168 fprintf(stderr, "%s: %s\n", dump_filename,
173 rc = fwrite(&dump_hdr, sizeof(dump_hdr), 1, fp);
175 rc = fwrite(buf, ioc_hdr->ioc_len, 1, fp);
178 fprintf(stderr, "%s: %s\n", dump_filename,
186 /* register a device to send ioctls to. */
188 register_ioc_dev(int dev_id, const char * dev_name, int major, int minor)
192 dev_id >= sizeof(ioc_dev_list) / sizeof(ioc_dev_list[0]))
195 unregister_ioc_dev(dev_id);
197 ioc_dev_list[dev_id].dev_name = dev_name;
198 ioc_dev_list[dev_id].dev_fd = -1;
199 ioc_dev_list[dev_id].dev_major = major;
200 ioc_dev_list[dev_id].dev_minor = minor;
206 unregister_ioc_dev(int dev_id)
210 dev_id >= sizeof(ioc_dev_list) / sizeof(ioc_dev_list[0]))
212 if (ioc_dev_list[dev_id].dev_name != NULL &&
213 ioc_dev_list[dev_id].dev_fd >= 0)
214 close(ioc_dev_list[dev_id].dev_fd);
216 ioc_dev_list[dev_id].dev_name = NULL;
217 ioc_dev_list[dev_id].dev_fd = -1;
220 /* If this file is set, then all ioctl buffers will be
221 appended to the file. */
223 set_ioctl_dump(char * file)
228 dump_filename = strdup(file);
229 if (dump_filename == NULL)
232 set_ioc_handler(&dump);
237 l_ioctl(int dev_id, unsigned int opc, void *buf)
239 return current_ioc_handler(dev_id, opc, buf);
242 /* Read an ioctl dump file, and call the ioc_func for each ioctl buffer
243 * in the file. For example:
245 * parse_dump("lctl.dump", l_ioctl);
247 * Note: if using l_ioctl, then you also need to register_ioc_dev() for
248 * each device used in the dump.
251 parse_dump(char * dump_file, ioc_handler_t ioc_func)
255 char *start, *buf, *end;
264 fd = syscall(SYS_open, dump_file, O_RDONLY);
266 fprintf(stderr, "couldn't open %s: %s\n", dump_file,
271 if (fstat(fd, &st)) {
272 perror("stat fails");
276 if (st.st_size < 1) {
277 fprintf(stderr, "KML is empty\n");
281 start = buf = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE , fd, 0);
282 end = start + st.st_size;
284 if (start == MAP_FAILED) {
285 fprintf(stderr, "can't create file mapping\n");
289 fd = CreateFile(dump_file, GENERIC_READ, FILE_SHARE_READ, NULL,
290 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
291 size = GetFileSize(fd, NULL);
293 fprintf(stderr, "KML is empty\n");
297 hmap = CreateFileMapping(fd, NULL, PAGE_READONLY, 0,0, NULL);
298 start = buf = MapViewOfFile(hmap, FILE_MAP_READ, 0, 0, 0);
302 fprintf(stderr, "can't create file mapping\n");
305 #endif /* __CYGWIN__ */
308 struct dump_hdr *dump_hdr = (struct dump_hdr *) buf;
309 struct libcfs_ioctl_hdr * data;
315 data = (struct libcfs_ioctl_hdr *) (buf + sizeof(*dump_hdr));
316 if (buf + data->ioc_len > end ) {
317 fprintf(stderr, "dump file overflow, %p + %d > %p\n", buf,
322 printf ("dump_hdr: %lx data: %lx\n",
323 (unsigned long)dump_hdr - (unsigned long)buf, (unsigned long)data - (unsigned long)buf);
325 printf("%d: opcode %x len: %d ver: %x ", line, dump_hdr->opc,
326 data->ioc_len, data->ioc_version);
329 memcpy(tmp, data, data->ioc_len);
331 rc = ioc_func(dump_hdr->dev_id, dump_hdr->opc, tmp);
333 printf("failed: %d\n", rc);
337 buf += data->ioc_len + sizeof(*dump_hdr);
341 munmap(start, end - start);
343 UnmapViewOfFile(start);
351 jt_ioc_dump(int argc, char **argv)
354 fprintf(stderr, "usage: %s [hostname]\n", argv[0]);
357 printf("setting dumpfile to: %s\n", argv[1]);
359 set_ioctl_dump(argv[1]);
363 int libcfs_ioctl_pack(struct libcfs_ioctl_data *data, char **pbuf,
367 struct libcfs_ioctl_data *overlay;
368 data->ioc_len = libcfs_ioctl_packlen(data);
369 data->ioc_version = LIBCFS_IOCTL_VERSION;
371 if (*pbuf && libcfs_ioctl_packlen(data) > max)
374 *pbuf = malloc(data->ioc_len);
378 overlay = (struct libcfs_ioctl_data *)*pbuf;
379 memcpy(*pbuf, data, sizeof(*data));
381 ptr = overlay->ioc_bulk;
382 if (data->ioc_inlbuf1)
383 LOGL(data->ioc_inlbuf1, data->ioc_inllen1, ptr);
384 if (data->ioc_inlbuf2)
385 LOGL(data->ioc_inlbuf2, data->ioc_inllen2, ptr);
386 if (libcfs_ioctl_is_invalid(overlay))