2 * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
4 * This file is part of Portals, http://www.sf.net/projects/lustre/
6 * Portals is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
10 * Portals is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with Portals; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #define __USE_FILE_OFFSET64
23 #include <libcfs/libcfsutil.h>
24 #include <lnet/api-support.h>
25 #include <lnet/lnetctl.h>
27 static ioc_handler_t do_ioctl; /* forward ref */
28 static ioc_handler_t *current_ioc_handler = &do_ioctl;
31 const char * dev_name;
37 static struct ioc_dev ioc_dev_list[10];
48 set_ioc_handler (ioc_handler_t *handler)
51 current_ioc_handler = do_ioctl;
53 current_ioc_handler = handler;
56 /* Catamount has no <linux/kdev_t.h>, so just define it here */
58 # define MKDEV(a,b) (((a) << 8) | (b))
62 open_ioc_dev(int dev_id)
64 const char * dev_name;
67 dev_id >= sizeof(ioc_dev_list) / sizeof(ioc_dev_list[0]))
70 dev_name = ioc_dev_list[dev_id].dev_name;
71 if (dev_name == NULL) {
72 fprintf(stderr, "unknown device id: %d\n", dev_id);
76 if (ioc_dev_list[dev_id].dev_fd < 0) {
77 int fd = cfs_proc_open((char *)dev_name, O_RDWR);
79 /* Make the /dev/ node if we need to */
80 if (fd < 0 && errno == ENOENT) {
81 if (cfs_proc_mknod(dev_name,
82 S_IFCHR|S_IWUSR|S_IRUSR,
83 MKDEV(ioc_dev_list[dev_id].dev_major,
84 ioc_dev_list[dev_id].dev_minor)) == 0)
85 fd = cfs_proc_open((char *)dev_name, O_RDWR);
87 fprintf(stderr, "mknod %s failed: %s\n",
88 dev_name, strerror(errno));
92 fprintf(stderr, "opening %s failed: %s\n"
93 "hint: the kernel modules may not be loaded\n",
94 dev_name, strerror(errno));
97 ioc_dev_list[dev_id].dev_fd = fd;
100 return ioc_dev_list[dev_id].dev_fd;
105 do_ioctl(int dev_id, unsigned int opc, void *buf)
109 fd = open_ioc_dev(dev_id);
113 rc = cfs_proc_ioctl(fd, opc, buf);
123 if (!dump_filename) {
124 fprintf(stderr, "no dump filename\n");
126 fp = fopen(dump_filename, "a");
131 * The dump file should start with a description of which devices are
132 * used, but for now it will assumed whatever app reads the file will
133 * know what to do. */
135 dump(int dev_id, unsigned int opc, void *buf)
138 struct dump_hdr dump_hdr;
139 struct libcfs_ioctl_hdr * ioc_hdr = (struct libcfs_ioctl_hdr *) buf;
142 printf("dumping opc %x to %s\n", opc, dump_filename);
145 dump_hdr.magic = 0xdeadbeef;
146 dump_hdr.dev_id = dev_id;
149 fp = get_dump_file();
151 fprintf(stderr, "%s: %s\n", dump_filename,
156 rc = fwrite(&dump_hdr, sizeof(dump_hdr), 1, fp);
158 rc = fwrite(buf, ioc_hdr->ioc_len, 1, fp);
161 fprintf(stderr, "%s: %s\n", dump_filename,
169 /* register a device to send ioctls to. */
171 register_ioc_dev(int dev_id, const char * dev_name, int major, int minor)
175 dev_id >= sizeof(ioc_dev_list) / sizeof(ioc_dev_list[0]))
178 unregister_ioc_dev(dev_id);
180 ioc_dev_list[dev_id].dev_name = dev_name;
181 ioc_dev_list[dev_id].dev_fd = -1;
182 ioc_dev_list[dev_id].dev_major = major;
183 ioc_dev_list[dev_id].dev_minor = minor;
189 unregister_ioc_dev(int dev_id)
193 dev_id >= sizeof(ioc_dev_list) / sizeof(ioc_dev_list[0]))
195 if (ioc_dev_list[dev_id].dev_name != NULL &&
196 ioc_dev_list[dev_id].dev_fd >= 0)
197 cfs_proc_close(ioc_dev_list[dev_id].dev_fd);
199 ioc_dev_list[dev_id].dev_name = NULL;
200 ioc_dev_list[dev_id].dev_fd = -1;
203 /* If this file is set, then all ioctl buffers will be
204 appended to the file. */
206 set_ioctl_dump(char * file)
211 dump_filename = strdup(file);
212 if (dump_filename == NULL)
215 set_ioc_handler(&dump);
220 l_ioctl(int dev_id, unsigned int opc, void *buf)
222 return current_ioc_handler(dev_id, opc, buf);
225 /* Read an ioctl dump file, and call the ioc_func for each ioctl buffer
226 * in the file. For example:
228 * parse_dump("lctl.dump", l_ioctl);
230 * Note: if using l_ioctl, then you also need to register_ioc_dev() for
231 * each device used in the dump.
234 parse_dump(char * dump_file, ioc_handler_t ioc_func)
237 char *start, *buf, *end;
239 #if defined(__CYGWIN__) || defined(__WINNT__)
244 fd = CreateFile(dump_file, GENERIC_READ, FILE_SHARE_READ, NULL,
245 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
246 if (fd == INVALID_HANDLE_VALUE) {
247 fprintf(stderr, "couldn't open %s (error code: %u)\n",
248 dump_file, GetLastError());
251 size = GetFileSize(fd, NULL);
252 if (size < 1 || size == 0xFFFFFFFF) {
253 fprintf(stderr, "KML is empty\n");
258 hmap = CreateFileMapping(fd, NULL, PAGE_READONLY, 0,0, NULL);
260 fprintf(stderr, "can't create file mapping\n");
264 start = buf = MapViewOfFile(hmap, FILE_MAP_READ, 0, 0, 0);
266 fprintf(stderr, "can't map file content\n");
274 fprintf(stderr, "can't create file mapping\n");
275 UnmapViewOfFile(start);
284 fd = open(dump_file, O_RDONLY);
286 fprintf(stderr, "couldn't open %s: %s\n", dump_file,
291 if (fstat(fd, &st)) {
292 perror("stat fails");
296 if (st.st_size < 1) {
297 fprintf(stderr, "KML is empty\n");
301 start = buf = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE , fd, 0);
302 end = start + st.st_size;
304 if (start == MAP_FAILED) {
305 fprintf(stderr, "can't create file mapping\n");
311 struct dump_hdr *dump_hdr = (struct dump_hdr *) buf;
312 struct libcfs_ioctl_hdr * data;
318 data = (struct libcfs_ioctl_hdr *) (buf + sizeof(*dump_hdr));
319 if (buf + data->ioc_len > end ) {
320 fprintf(stderr, "dump file overflow, %p + %d > %p\n", buf,
325 printf ("dump_hdr: %lx data: %lx\n",
326 (unsigned long)dump_hdr - (unsigned long)buf, (unsigned long)data - (unsigned long)buf);
328 printf("%d: opcode %x len: %d ver: %x ", line, dump_hdr->opc,
329 data->ioc_len, data->ioc_version);
332 memcpy(tmp, data, data->ioc_len);
334 rc = ioc_func(dump_hdr->dev_id, dump_hdr->opc, tmp);
336 printf("failed: %d\n", rc);
340 buf += data->ioc_len + sizeof(*dump_hdr);
343 #if defined(__CYGWIN__) || defined(__WINNT__)
344 UnmapViewOfFile(start);
347 munmap(start, end - start);
354 jt_ioc_dump(int argc, char **argv)
357 fprintf(stderr, "usage: %s [hostname]\n", argv[0]);
360 printf("setting dumpfile to: %s\n", argv[1]);
362 set_ioctl_dump(argv[1]);
366 int libcfs_ioctl_pack(struct libcfs_ioctl_data *data, char **pbuf,
370 struct libcfs_ioctl_data *overlay;
371 data->ioc_len = libcfs_ioctl_packlen(data);
372 data->ioc_version = LIBCFS_IOCTL_VERSION;
374 if (*pbuf && libcfs_ioctl_packlen(data) > max)
377 *pbuf = malloc(data->ioc_len);
381 overlay = (struct libcfs_ioctl_data *)*pbuf;
382 memcpy(*pbuf, data, sizeof(*data));
384 ptr = overlay->ioc_bulk;
385 if (data->ioc_inlbuf1)
386 LOGL(data->ioc_inlbuf1, data->ioc_inllen1, ptr);
387 if (data->ioc_inlbuf2)
388 LOGL(data->ioc_inlbuf2, data->ioc_inllen2, ptr);
389 if (libcfs_ioctl_is_invalid(overlay))