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 <portals/api-support.h>
37 #include <portals/ptlctl.h>
38 #include <libcfs/portals_utils.h>
40 static ioc_handler_t do_ioctl; /* forward ref */
41 static ioc_handler_t *current_ioc_handler = &do_ioctl;
44 const char * dev_name;
48 static struct ioc_dev ioc_dev_list[10];
59 set_ioc_handler (ioc_handler_t *handler)
62 current_ioc_handler = do_ioctl;
64 current_ioc_handler = handler;
68 open_ioc_dev(int dev_id)
70 const char * dev_name;
72 if (dev_id < 0 || dev_id >= sizeof(ioc_dev_list))
75 dev_name = ioc_dev_list[dev_id].dev_name;
76 if (dev_name == NULL) {
77 fprintf(stderr, "unknown device id: %d\n", dev_id);
81 if (ioc_dev_list[dev_id].dev_fd < 0) {
82 int fd = open(dev_name, O_RDWR);
85 fprintf(stderr, "opening %s failed: %s\n"
86 "hint: the kernel modules may not be loaded\n",
87 dev_name, strerror(errno));
90 ioc_dev_list[dev_id].dev_fd = fd;
93 return ioc_dev_list[dev_id].dev_fd;
98 do_ioctl(int dev_id, unsigned int opc, void *buf)
102 fd = open_ioc_dev(dev_id);
106 rc = ioctl(fd, opc, buf);
116 if (!dump_filename) {
117 fprintf(stderr, "no dump filename\n");
119 fp = fopen(dump_filename, "a");
124 * The dump file should start with a description of which devices are
125 * used, but for now it will assumed whatever app reads the file will
126 * know what to do. */
128 dump(int dev_id, unsigned int opc, void *buf)
131 struct dump_hdr dump_hdr;
132 struct portal_ioctl_hdr * ioc_hdr = (struct portal_ioctl_hdr *) buf;
135 printf("dumping opc %x to %s\n", opc, dump_filename);
138 dump_hdr.magic = 0xdeadbeef;
139 dump_hdr.dev_id = dev_id;
142 fp = get_dump_file();
144 fprintf(stderr, "%s: %s\n", dump_filename,
149 rc = fwrite(&dump_hdr, sizeof(dump_hdr), 1, fp);
151 rc = fwrite(buf, ioc_hdr->ioc_len, 1, fp);
154 fprintf(stderr, "%s: %s\n", dump_filename,
162 /* register a device to send ioctls to. */
164 register_ioc_dev(int dev_id, const char * dev_name)
167 if (dev_id < 0 || dev_id >= sizeof(ioc_dev_list))
170 unregister_ioc_dev(dev_id);
172 ioc_dev_list[dev_id].dev_name = dev_name;
173 ioc_dev_list[dev_id].dev_fd = -1;
179 unregister_ioc_dev(int dev_id)
182 if (dev_id < 0 || dev_id >= sizeof(ioc_dev_list))
184 if (ioc_dev_list[dev_id].dev_name != NULL &&
185 ioc_dev_list[dev_id].dev_fd >= 0)
186 close(ioc_dev_list[dev_id].dev_fd);
188 ioc_dev_list[dev_id].dev_name = NULL;
189 ioc_dev_list[dev_id].dev_fd = -1;
192 /* If this file is set, then all ioctl buffers will be
193 appended to the file. */
195 set_ioctl_dump(char * file)
200 dump_filename = strdup(file);
201 if (dump_filename == NULL)
204 set_ioc_handler(&dump);
209 l_ioctl(int dev_id, unsigned int opc, void *buf)
211 return current_ioc_handler(dev_id, opc, buf);
214 /* Read an ioctl dump file, and call the ioc_func for each ioctl buffer
215 * in the file. For example:
217 * parse_dump("lctl.dump", l_ioctl);
219 * Note: if using l_ioctl, then you also need to register_ioc_dev() for
220 * each device used in the dump.
223 parse_dump(char * dump_file, ioc_handler_t ioc_func)
227 char *start, *buf, *end;
236 fd = syscall(SYS_open, dump_file, O_RDONLY);
238 fprintf(stderr, "couldn't open %s: %s\n", dump_file,
243 if (fstat(fd, &st)) {
244 perror("stat fails");
248 if (st.st_size < 1) {
249 fprintf(stderr, "KML is empty\n");
253 start = buf = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE , fd, 0);
254 end = start + st.st_size;
256 if (start == MAP_FAILED) {
257 fprintf(stderr, "can't create file mapping\n");
261 fd = CreateFile(dump_file, GENERIC_READ, FILE_SHARE_READ, NULL,
262 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
263 size = GetFileSize(fd, NULL);
265 fprintf(stderr, "KML is empty\n");
269 hmap = CreateFileMapping(fd, NULL, PAGE_READONLY, 0,0, NULL);
270 start = buf = MapViewOfFile(hmap, FILE_MAP_READ, 0, 0, 0);
274 fprintf(stderr, "can't create file mapping\n");
277 #endif /* __CYGWIN__ */
280 struct dump_hdr *dump_hdr = (struct dump_hdr *) buf;
281 struct portal_ioctl_hdr * data;
287 data = (struct portal_ioctl_hdr *) (buf + sizeof(*dump_hdr));
288 if (buf + data->ioc_len > end ) {
289 fprintf(stderr, "dump file overflow, %p + %d > %p\n", buf,
294 printf ("dump_hdr: %lx data: %lx\n",
295 (unsigned long)dump_hdr - (unsigned long)buf, (unsigned long)data - (unsigned long)buf);
297 printf("%d: opcode %x len: %d ver: %x ", line, dump_hdr->opc,
298 data->ioc_len, data->ioc_version);
301 memcpy(tmp, data, data->ioc_len);
303 rc = ioc_func(dump_hdr->dev_id, dump_hdr->opc, tmp);
305 printf("failed: %d\n", rc);
309 buf += data->ioc_len + sizeof(*dump_hdr);
313 munmap(start, end - start);
315 UnmapViewOfFile(start);
323 jt_ioc_dump(int argc, char **argv)
326 fprintf(stderr, "usage: %s [hostname]\n", argv[0]);
329 printf("setting dumpfile to: %s\n", argv[1]);
331 set_ioctl_dump(argv[1]);