Whamcloud - gitweb
LU-1347 build: remove the vim/emacs modelines
[fs/lustre-release.git] / libcfs / libcfs / util / l_ioctl.c
1 /*
2  * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
3  *
4  *   This file is part of Portals, http://www.sf.net/projects/lustre/
5  *
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.
9  *
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.
14  *
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.
18  *
19  */
20
21 #define __USE_FILE_OFFSET64
22
23 #include <libcfs/libcfsutil.h>
24 #include <lnet/api-support.h>
25 #include <lnet/lnetctl.h>
26
27 static ioc_handler_t  do_ioctl;                 /* forward ref */
28 static ioc_handler_t *current_ioc_handler = &do_ioctl;
29
30 struct ioc_dev {
31         const char * dev_name;
32         int dev_fd;
33         int dev_major;
34         int dev_minor;
35 };
36
37 static struct ioc_dev ioc_dev_list[10];
38
39 struct dump_hdr {
40         int magic;
41         int dev_id;
42         unsigned int opc;
43 };
44
45 char *dump_filename;
46
47 void
48 set_ioc_handler (ioc_handler_t *handler)
49 {
50         if (handler == NULL)
51                 current_ioc_handler = do_ioctl;
52         else
53                 current_ioc_handler = handler;
54 }
55
56 /* Catamount has no <linux/kdev_t.h>, so just define it here */
57 #ifndef MKDEV
58 # define MKDEV(a,b) (((a) << 8) | (b))
59 #endif
60
61 static int
62 open_ioc_dev(int dev_id) 
63 {
64         const char * dev_name;
65
66         if (dev_id < 0 || 
67             dev_id >= sizeof(ioc_dev_list) / sizeof(ioc_dev_list[0]))
68                 return -EINVAL;
69
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);
73                 return -EINVAL;
74         }
75
76         if (ioc_dev_list[dev_id].dev_fd < 0) {
77                 int fd = cfs_proc_open((char *)dev_name, O_RDWR);
78
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);
86                         else
87                                 fprintf(stderr, "mknod %s failed: %s\n",
88                                         dev_name, strerror(errno));
89                 }
90
91                 if (fd < 0) {
92                         fprintf(stderr, "opening %s failed: %s\n"
93                                 "hint: the kernel modules may not be loaded\n",
94                                 dev_name, strerror(errno));
95                         return fd;
96                 }
97                 ioc_dev_list[dev_id].dev_fd = fd;
98         }
99
100         return ioc_dev_list[dev_id].dev_fd;
101 }
102
103
104 static int 
105 do_ioctl(int dev_id, unsigned int opc, void *buf)
106 {
107         int fd, rc;
108         
109         fd = open_ioc_dev(dev_id);
110         if (fd < 0) 
111                 return fd;
112
113         rc = cfs_proc_ioctl(fd, opc, buf);
114         return rc;
115         
116 }
117
118 static FILE *
119 get_dump_file() 
120 {
121         FILE *fp = NULL;
122         
123         if (!dump_filename) {
124                 fprintf(stderr, "no dump filename\n");
125         } else 
126                 fp = fopen(dump_filename, "a");
127         return fp;
128 }
129
130 /*
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. */
134 int 
135 dump(int dev_id, unsigned int opc, void *buf)
136 {
137         FILE *fp;
138         struct dump_hdr dump_hdr;
139         struct libcfs_ioctl_hdr * ioc_hdr = (struct  libcfs_ioctl_hdr *) buf;
140         int rc;
141         
142         printf("dumping opc %x to %s\n", opc, dump_filename);
143         
144
145         dump_hdr.magic = 0xdeadbeef;
146         dump_hdr.dev_id = dev_id;
147         dump_hdr.opc = opc;
148
149         fp = get_dump_file();
150         if (fp == NULL) {
151                 fprintf(stderr, "%s: %s\n", dump_filename, 
152                         strerror(errno));
153                 return -EINVAL;
154         }
155         
156         rc = fwrite(&dump_hdr, sizeof(dump_hdr), 1, fp);
157         if (rc == 1)
158                 rc = fwrite(buf, ioc_hdr->ioc_len, 1, fp);
159         fclose(fp);
160         if (rc != 1) {
161                 fprintf(stderr, "%s: %s\n", dump_filename,
162                         strerror(errno));
163                 return -EINVAL;
164         }
165
166         return 0;
167 }
168
169 /* register a device to send ioctls to.  */
170 int 
171 register_ioc_dev(int dev_id, const char * dev_name, int major, int minor) 
172 {
173
174         if (dev_id < 0 || 
175             dev_id >= sizeof(ioc_dev_list) / sizeof(ioc_dev_list[0]))
176                 return -EINVAL;
177
178         unregister_ioc_dev(dev_id);
179
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;
184  
185         return dev_id;
186 }
187
188 void
189 unregister_ioc_dev(int dev_id) 
190 {
191
192         if (dev_id < 0 || 
193             dev_id >= sizeof(ioc_dev_list) / sizeof(ioc_dev_list[0]))
194                 return;
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);
198
199         ioc_dev_list[dev_id].dev_name = NULL;
200         ioc_dev_list[dev_id].dev_fd = -1;
201 }
202
203 /* If this file is set, then all ioctl buffers will be 
204    appended to the file. */
205 int
206 set_ioctl_dump(char * file)
207 {
208         if (dump_filename)
209                 free(dump_filename);
210         
211         dump_filename = strdup(file);
212         if (dump_filename == NULL)
213                 abort();
214
215         set_ioc_handler(&dump);
216         return 0;
217 }
218
219 int
220 l_ioctl(int dev_id, unsigned int opc, void *buf)
221 {
222         return current_ioc_handler(dev_id, opc, buf);
223 }
224
225 /* Read an ioctl dump file, and call the ioc_func for each ioctl buffer
226  * in the file.  For example:
227  *
228  * parse_dump("lctl.dump", l_ioctl);
229  *
230  * Note: if using l_ioctl, then you also need to register_ioc_dev() for 
231  * each device used in the dump.
232  */
233 int 
234 parse_dump(char * dump_file, ioc_handler_t ioc_func)
235 {
236         int line =0;
237         char *start, *buf, *end;
238
239 #if defined(__CYGWIN__) || defined(__WINNT__)
240
241         HANDLE fd, hmap;
242         DWORD size;
243
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());
249                 exit(1);
250         }
251         size = GetFileSize(fd, NULL);
252         if (size < 1 || size == 0xFFFFFFFF) {
253                 fprintf(stderr, "KML is empty\n");
254                 CloseHandle(fd);
255                 exit(1);
256         }
257
258         hmap = CreateFileMapping(fd, NULL, PAGE_READONLY, 0,0, NULL);
259         if (hmap == NULL) {
260                 fprintf(stderr, "can't create file mapping\n");
261                 CloseHandle(fd);
262                 exit(1);
263         }
264         start = buf = MapViewOfFile(hmap, FILE_MAP_READ, 0, 0, 0);
265         if (start == NULL) {
266                 fprintf(stderr, "can't map file content\n");
267                 CloseHandle(hmap);
268                 CloseHandle(fd);
269                 exit(1);
270         }
271         end = buf + size;
272         CloseHandle(fd);
273         if (start == NULL) {
274                 fprintf(stderr, "can't create file mapping\n");
275                 UnmapViewOfFile(start);
276                 CloseHandle(hmap);
277                 exit(1);
278         }
279 #else
280
281         struct stat st;
282         int fd;
283
284         fd = open(dump_file, O_RDONLY);
285         if (fd < 0) {
286                 fprintf(stderr, "couldn't open %s: %s\n", dump_file, 
287                         strerror(errno));
288                 exit(1);
289         }
290
291         if (fstat(fd, &st)) { 
292                 perror("stat fails");
293                 exit(1);
294         }
295
296         if (st.st_size < 1) {
297                 fprintf(stderr, "KML is empty\n");
298                 exit(1);
299         }
300
301         start = buf = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE , fd, 0);
302         end = start + st.st_size;
303         close(fd);
304         if (start == MAP_FAILED) {
305                 fprintf(stderr, "can't create file mapping\n");
306                 exit(1);
307         }
308 #endif
309
310         while (buf < end) {
311                 struct dump_hdr *dump_hdr = (struct dump_hdr *) buf;
312                 struct libcfs_ioctl_hdr * data;
313                 char tmp[8096];
314                 int rc;
315
316                 line++;
317
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,
321                                 data->ioc_len, end);
322                         return -1;
323                 }
324 #if 0
325                 printf ("dump_hdr: %lx data: %lx\n",
326                         (unsigned long)dump_hdr - (unsigned long)buf, (unsigned long)data - (unsigned long)buf);
327
328                 printf("%d: opcode %x len: %d  ver: %x ", line, dump_hdr->opc,
329                        data->ioc_len, data->ioc_version);
330 #endif
331
332                 memcpy(tmp, data, data->ioc_len);
333
334                 rc = ioc_func(dump_hdr->dev_id, dump_hdr->opc, tmp);
335                 if (rc) {
336                         printf("failed: %d\n", rc);
337                         exit(1);
338                 }
339
340                 buf += data->ioc_len + sizeof(*dump_hdr);
341         }
342
343 #if defined(__CYGWIN__) || defined(__WINNT__)
344         UnmapViewOfFile(start);
345         CloseHandle(hmap);
346 #else
347         munmap(start, end - start);
348 #endif
349
350         return 0;
351 }
352
353 int 
354 jt_ioc_dump(int argc, char **argv)
355 {
356         if (argc > 2) {
357                 fprintf(stderr, "usage: %s [hostname]\n", argv[0]);
358                 return 0;
359         }
360         printf("setting dumpfile to: %s\n", argv[1]);
361         
362         set_ioctl_dump(argv[1]);
363         return 0;
364 }
365
366 int libcfs_ioctl_pack(struct libcfs_ioctl_data *data, char **pbuf,
367                                     int max)
368 {
369         char *ptr;
370         struct libcfs_ioctl_data *overlay;
371         data->ioc_len = libcfs_ioctl_packlen(data);
372         data->ioc_version = LIBCFS_IOCTL_VERSION;
373
374         if (*pbuf && libcfs_ioctl_packlen(data) > max)
375                 return 1;
376         if (*pbuf == NULL) {
377                 *pbuf = malloc(data->ioc_len);
378         }
379         if (!*pbuf)
380                 return 1;
381         overlay = (struct libcfs_ioctl_data *)*pbuf;
382         memcpy(*pbuf, data, sizeof(*data));
383
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))
390                 return 1;
391
392         return 0;
393 }
394