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>
36 #include <lnet/api-support.h>
37 #include <lnet/lnetctl.h>
38 #include <libcfs/portals_utils.h>
41 static ioc_handler_t do_ioctl; /* forward ref */
42 static ioc_handler_t *current_ioc_handler = &do_ioctl;
45 const char * dev_name;
51 static struct ioc_dev ioc_dev_list[10];
62 set_ioc_handler (ioc_handler_t *handler)
65 current_ioc_handler = do_ioctl;
67 current_ioc_handler = handler;
70 /* Catamount has no <linux/kdev_t.h>, so just define it here */
72 # define MKDEV(a,b) (((a) << 8) | (b))
76 open_ioc_dev(int dev_id)
78 const char * dev_name;
81 dev_id >= sizeof(ioc_dev_list) / sizeof(ioc_dev_list[0]))
84 dev_name = ioc_dev_list[dev_id].dev_name;
85 if (dev_name == NULL) {
86 fprintf(stderr, "unknown device id: %d\n", dev_id);
90 if (ioc_dev_list[dev_id].dev_fd < 0) {
91 int fd = open(dev_name, O_RDWR);
93 /* Make the /dev/ node if we need to */
94 if (fd < 0 && errno == ENOENT) {
96 S_IFCHR|S_IWUSR|S_IRUSR,
97 MKDEV(ioc_dev_list[dev_id].dev_major,
98 ioc_dev_list[dev_id].dev_minor)) == 0)
99 fd = open(dev_name, O_RDWR);
101 fprintf(stderr, "mknod %s failed: %s\n",
102 dev_name, strerror(errno));
106 fprintf(stderr, "opening %s failed: %s\n"
107 "hint: the kernel modules may not be loaded\n",
108 dev_name, strerror(errno));
111 ioc_dev_list[dev_id].dev_fd = fd;
114 return ioc_dev_list[dev_id].dev_fd;
119 do_ioctl(int dev_id, unsigned int opc, void *buf)
123 fd = open_ioc_dev(dev_id);
127 rc = ioctl(fd, opc, buf);
137 if (!dump_filename) {
138 fprintf(stderr, "no dump filename\n");
140 fp = fopen(dump_filename, "a");
145 * The dump file should start with a description of which devices are
146 * used, but for now it will assumed whatever app reads the file will
147 * know what to do. */
149 dump(int dev_id, unsigned int opc, void *buf)
152 struct dump_hdr dump_hdr;
153 struct libcfs_ioctl_hdr * ioc_hdr = (struct libcfs_ioctl_hdr *) buf;
156 printf("dumping opc %x to %s\n", opc, dump_filename);
159 dump_hdr.magic = 0xdeadbeef;
160 dump_hdr.dev_id = dev_id;
163 fp = get_dump_file();
165 fprintf(stderr, "%s: %s\n", dump_filename,
170 rc = fwrite(&dump_hdr, sizeof(dump_hdr), 1, fp);
172 rc = fwrite(buf, ioc_hdr->ioc_len, 1, fp);
175 fprintf(stderr, "%s: %s\n", dump_filename,
183 /* register a device to send ioctls to. */
185 register_ioc_dev(int dev_id, const char * dev_name, int major, int minor)
189 dev_id >= sizeof(ioc_dev_list) / sizeof(ioc_dev_list[0]))
192 unregister_ioc_dev(dev_id);
194 ioc_dev_list[dev_id].dev_name = dev_name;
195 ioc_dev_list[dev_id].dev_fd = -1;
196 ioc_dev_list[dev_id].dev_major = major;
197 ioc_dev_list[dev_id].dev_minor = minor;
203 unregister_ioc_dev(int dev_id)
207 dev_id >= sizeof(ioc_dev_list) / sizeof(ioc_dev_list[0]))
209 if (ioc_dev_list[dev_id].dev_name != NULL &&
210 ioc_dev_list[dev_id].dev_fd >= 0)
211 close(ioc_dev_list[dev_id].dev_fd);
213 ioc_dev_list[dev_id].dev_name = NULL;
214 ioc_dev_list[dev_id].dev_fd = -1;
217 /* If this file is set, then all ioctl buffers will be
218 appended to the file. */
220 set_ioctl_dump(char * file)
225 dump_filename = strdup(file);
226 if (dump_filename == NULL)
229 set_ioc_handler(&dump);
234 l_ioctl(int dev_id, unsigned int opc, void *buf)
236 return current_ioc_handler(dev_id, opc, buf);
239 /* Read an ioctl dump file, and call the ioc_func for each ioctl buffer
240 * in the file. For example:
242 * parse_dump("lctl.dump", l_ioctl);
244 * Note: if using l_ioctl, then you also need to register_ioc_dev() for
245 * each device used in the dump.
248 parse_dump(char * dump_file, ioc_handler_t ioc_func)
252 char *start, *buf, *end;
261 fd = syscall(SYS_open, dump_file, O_RDONLY);
263 fprintf(stderr, "couldn't open %s: %s\n", dump_file,
268 if (fstat(fd, &st)) {
269 perror("stat fails");
273 if (st.st_size < 1) {
274 fprintf(stderr, "KML is empty\n");
278 start = buf = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE , fd, 0);
279 end = start + st.st_size;
281 if (start == MAP_FAILED) {
282 fprintf(stderr, "can't create file mapping\n");
286 fd = CreateFile(dump_file, GENERIC_READ, FILE_SHARE_READ, NULL,
287 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
288 size = GetFileSize(fd, NULL);
290 fprintf(stderr, "KML is empty\n");
294 hmap = CreateFileMapping(fd, NULL, PAGE_READONLY, 0,0, NULL);
295 start = buf = MapViewOfFile(hmap, FILE_MAP_READ, 0, 0, 0);
299 fprintf(stderr, "can't create file mapping\n");
302 #endif /* __CYGWIN__ */
305 struct dump_hdr *dump_hdr = (struct dump_hdr *) buf;
306 struct libcfs_ioctl_hdr * data;
312 data = (struct libcfs_ioctl_hdr *) (buf + sizeof(*dump_hdr));
313 if (buf + data->ioc_len > end ) {
314 fprintf(stderr, "dump file overflow, %p + %d > %p\n", buf,
319 printf ("dump_hdr: %lx data: %lx\n",
320 (unsigned long)dump_hdr - (unsigned long)buf, (unsigned long)data - (unsigned long)buf);
322 printf("%d: opcode %x len: %d ver: %x ", line, dump_hdr->opc,
323 data->ioc_len, data->ioc_version);
326 memcpy(tmp, data, data->ioc_len);
328 rc = ioc_func(dump_hdr->dev_id, dump_hdr->opc, tmp);
330 printf("failed: %d\n", rc);
334 buf += data->ioc_len + sizeof(*dump_hdr);
338 munmap(start, end - start);
340 UnmapViewOfFile(start);
348 jt_ioc_dump(int argc, char **argv)
351 fprintf(stderr, "usage: %s [hostname]\n", argv[0]);
354 printf("setting dumpfile to: %s\n", argv[1]);
356 set_ioctl_dump(argv[1]);