From 2e238ab6645cf1aee982c9c9d2457efa9a83dffe Mon Sep 17 00:00:00 2001 From: nathan Date: Thu, 27 Oct 2005 23:24:45 +0000 Subject: [PATCH] b=9501 r=adilger automatically create /dev/lnet, /dev/obd when needed. --- lnet/include/libcfs/lltrace.h | 19 ++-- lnet/include/lnet/lnetctl.h | 8 +- lnet/utils/debugctl.c | 3 +- lnet/utils/l_ioctl.c | 235 ++++++++++++++++++++++-------------------- lnet/utils/portals.c | 7 +- 5 files changed, 149 insertions(+), 123 deletions(-) diff --git a/lnet/include/libcfs/lltrace.h b/lnet/include/libcfs/lltrace.h index 68d2708..0240459 100644 --- a/lnet/include/libcfs/lltrace.h +++ b/lnet/include/libcfs/lltrace.h @@ -84,7 +84,8 @@ static inline int ltrace_start() int rc = 0; dbg_initialize(0, NULL); #ifdef LNET_DEV_ID - rc = register_ioc_dev(LNET_DEV_ID, LNET_DEV_PATH); + rc = register_ioc_dev(LNET_DEV_ID, LNET_DEV_PATH, + LNET_DEV_MAJOR, LNET_DEV_MINOR); #endif ltrace_filter("class"); ltrace_filter("nal"); @@ -117,14 +118,14 @@ static inline int not_uml() * 1 when run on host * <0 when lookup failed */ - struct stat buf; - int rc = stat("/dev/ubd", &buf); - rc = ((rc<0) && (errno == ENOENT)) ? 1 : rc; - if (rc<0) { - fprintf(stderr, "Cannot stat /dev/ubd: %s\n", strerror(errno)); - rc = 1; /* Assume host */ - } - return rc; + struct stat buf; + int rc = stat("/dev/ubd", &buf); + rc = ((rc<0) && (errno == ENOENT)) ? 1 : rc; + if (rc<0) { + fprintf(stderr, "Cannot stat /dev/ubd: %s\n", strerror(errno)); + rc = 1; /* Assume host */ + } + return rc; } #define LTRACE_MAX_NOB 256 diff --git a/lnet/include/lnet/lnetctl.h b/lnet/include/lnet/lnetctl.h index 8e36474..b14e484 100644 --- a/lnet/include/lnet/lnetctl.h +++ b/lnet/include/lnet/lnetctl.h @@ -27,10 +27,16 @@ #define LNET_DEV_ID 0 #define LNET_DEV_PATH "/dev/lnet" +#define LNET_DEV_MAJOR 10 +#define LNET_DEV_MINOR 240 #define OBD_DEV_ID 1 #define OBD_DEV_PATH "/dev/obd" +#define OBD_DEV_MAJOR 10 +#define OBD_DEV_MINOR 241 #define SMFS_DEV_ID 2 #define SMFS_DEV_PATH "/dev/snapdev" +#define SMFS_DEV_MAJOR 10 +#define SMFS_DEV_MINOR 242 int ptl_initialize(int argc, char **argv); int jt_ptl_network(int argc, char **argv); @@ -75,7 +81,7 @@ int jt_dbg_panic(int argc, char **argv); /* l_ioctl.c */ typedef int (ioc_handler_t)(int dev_id, unsigned int opc, void *buf); void set_ioc_handler(ioc_handler_t *handler); -int register_ioc_dev(int dev_id, const char * dev_name); +int register_ioc_dev(int dev_id, const char * dev_name, int major, int minor); void unregister_ioc_dev(int dev_id); int set_ioctl_dump(char * file); int l_ioctl(int dev_id, unsigned int opc, void *buf); diff --git a/lnet/utils/debugctl.c b/lnet/utils/debugctl.c index c0abb2a..cf70fd8 100644 --- a/lnet/utils/debugctl.c +++ b/lnet/utils/debugctl.c @@ -53,7 +53,8 @@ int main(int argc, char **argv) if (dbg_initialize(argc, argv) < 0) exit(2); - register_ioc_dev(LNET_DEV_ID, LNET_DEV_PATH); + register_ioc_dev(LNET_DEV_ID, LNET_DEV_PATH, + LNET_DEV_MAJOR, LNET_DEV_MINOR); Parser_init("debugctl > ", list); if (argc > 1) diff --git a/lnet/utils/l_ioctl.c b/lnet/utils/l_ioctl.c index c913b75..728e81a 100644 --- a/lnet/utils/l_ioctl.c +++ b/lnet/utils/l_ioctl.c @@ -36,20 +36,23 @@ #include #include #include +#include /* for MKDEV */ static ioc_handler_t do_ioctl; /* forward ref */ static ioc_handler_t *current_ioc_handler = &do_ioctl; struct ioc_dev { - const char * dev_name; - int dev_fd; + const char * dev_name; + int dev_fd; + int dev_major; + int dev_minor; }; static struct ioc_dev ioc_dev_list[10]; struct dump_hdr { - int magic; - int dev_id; + int magic; + int dev_id; unsigned int opc; }; @@ -67,57 +70,69 @@ set_ioc_handler (ioc_handler_t *handler) static int open_ioc_dev(int dev_id) { - const char * dev_name; + const char * dev_name; - if (dev_id < 0 || dev_id >= sizeof(ioc_dev_list)) - return -EINVAL; + if (dev_id < 0 || dev_id >= sizeof(ioc_dev_list)) + return -EINVAL; - dev_name = ioc_dev_list[dev_id].dev_name; - if (dev_name == NULL) { + dev_name = ioc_dev_list[dev_id].dev_name; + if (dev_name == NULL) { fprintf(stderr, "unknown device id: %d\n", dev_id); - return -EINVAL; - } - - if (ioc_dev_list[dev_id].dev_fd < 0) { - int fd = open(dev_name, O_RDWR); - - if (fd < 0) { - fprintf(stderr, "opening %s failed: %s\n" - "hint: the kernel modules may not be loaded\n", - dev_name, strerror(errno)); - return fd; - } - ioc_dev_list[dev_id].dev_fd = fd; - } - - return ioc_dev_list[dev_id].dev_fd; + return -EINVAL; + } + + if (ioc_dev_list[dev_id].dev_fd < 0) { + int fd = open(dev_name, O_RDWR); + + /* Make the /dev/ node if we need to */ + if (fd < 0 && errno == ENOENT) { + if (mknod(dev_name, + S_IFCHR|S_IWUSR|S_IRUSR, + MKDEV(ioc_dev_list[dev_id].dev_major, + ioc_dev_list[dev_id].dev_minor)) == 0) + fd = open(dev_name, O_RDWR); + else + fprintf(stderr, "mknod %s failed: %s\n", + dev_name, strerror(errno)); + } + + if (fd < 0) { + fprintf(stderr, "opening %s failed: %s\n" + "hint: the kernel modules may not be loaded\n", + dev_name, strerror(errno)); + return fd; + } + ioc_dev_list[dev_id].dev_fd = fd; + } + + return ioc_dev_list[dev_id].dev_fd; } static int do_ioctl(int dev_id, unsigned int opc, void *buf) { - int fd, rc; - - fd = open_ioc_dev(dev_id); - if (fd < 0) - return fd; - - rc = ioctl(fd, opc, buf); - return rc; - + int fd, rc; + + fd = open_ioc_dev(dev_id); + if (fd < 0) + return fd; + + rc = ioctl(fd, opc, buf); + return rc; + } static FILE * get_dump_file() { - FILE *fp = NULL; - - if (!dump_filename) { - fprintf(stderr, "no dump filename\n"); - } else - fp = fopen(dump_filename, "a"); - return fp; + FILE *fp = NULL; + + if (!dump_filename) { + fprintf(stderr, "no dump filename\n"); + } else + fp = fopen(dump_filename, "a"); + return fp; } /* @@ -127,25 +142,25 @@ get_dump_file() int dump(int dev_id, unsigned int opc, void *buf) { - FILE *fp; - struct dump_hdr dump_hdr; + FILE *fp; + struct dump_hdr dump_hdr; struct libcfs_ioctl_hdr * ioc_hdr = (struct libcfs_ioctl_hdr *) buf; - int rc; - - printf("dumping opc %x to %s\n", opc, dump_filename); - - - dump_hdr.magic = 0xdeadbeef; - dump_hdr.dev_id = dev_id; - dump_hdr.opc = opc; - - fp = get_dump_file(); - if (fp == NULL) { - fprintf(stderr, "%s: %s\n", dump_filename, - strerror(errno)); - return -EINVAL; - } - + int rc; + + printf("dumping opc %x to %s\n", opc, dump_filename); + + + dump_hdr.magic = 0xdeadbeef; + dump_hdr.dev_id = dev_id; + dump_hdr.opc = opc; + + fp = get_dump_file(); + if (fp == NULL) { + fprintf(stderr, "%s: %s\n", dump_filename, + strerror(errno)); + return -EINVAL; + } + rc = fwrite(&dump_hdr, sizeof(dump_hdr), 1, fp); if (rc == 1) rc = fwrite(buf, ioc_hdr->ioc_len, 1, fp); @@ -161,32 +176,34 @@ dump(int dev_id, unsigned int opc, void *buf) /* register a device to send ioctls to. */ int -register_ioc_dev(int dev_id, const char * dev_name) +register_ioc_dev(int dev_id, const char * dev_name, int major, int minor) { - if (dev_id < 0 || dev_id >= sizeof(ioc_dev_list)) - return -EINVAL; - - unregister_ioc_dev(dev_id); + if (dev_id < 0 || dev_id >= sizeof(ioc_dev_list)) + return -EINVAL; - ioc_dev_list[dev_id].dev_name = dev_name; - ioc_dev_list[dev_id].dev_fd = -1; + unregister_ioc_dev(dev_id); - return dev_id; + ioc_dev_list[dev_id].dev_name = dev_name; + ioc_dev_list[dev_id].dev_fd = -1; + ioc_dev_list[dev_id].dev_major = major; + ioc_dev_list[dev_id].dev_minor = minor; + + return dev_id; } void unregister_ioc_dev(int dev_id) { - if (dev_id < 0 || dev_id >= sizeof(ioc_dev_list)) - return; - if (ioc_dev_list[dev_id].dev_name != NULL && - ioc_dev_list[dev_id].dev_fd >= 0) - close(ioc_dev_list[dev_id].dev_fd); + if (dev_id < 0 || dev_id >= sizeof(ioc_dev_list)) + return; + if (ioc_dev_list[dev_id].dev_name != NULL && + ioc_dev_list[dev_id].dev_fd >= 0) + close(ioc_dev_list[dev_id].dev_fd); - ioc_dev_list[dev_id].dev_name = NULL; - ioc_dev_list[dev_id].dev_fd = -1; + ioc_dev_list[dev_id].dev_name = NULL; + ioc_dev_list[dev_id].dev_fd = -1; } /* If this file is set, then all ioctl buffers will be @@ -194,15 +211,15 @@ unregister_ioc_dev(int dev_id) int set_ioctl_dump(char * file) { - if (dump_filename) - free(dump_filename); - - dump_filename = strdup(file); + if (dump_filename) + free(dump_filename); + + dump_filename = strdup(file); if (dump_filename == NULL) abort(); set_ioc_handler(&dump); - return 0; + return 0; } int @@ -222,61 +239,61 @@ l_ioctl(int dev_id, unsigned int opc, void *buf) int parse_dump(char * dump_file, ioc_handler_t ioc_func) { - int line =0; - struct stat st; - char *start, *buf, *end; + int line =0; + struct stat st; + char *start, *buf, *end; #ifndef __CYGWIN__ int fd; #else HANDLE fd, hmap; DWORD size; #endif - + #ifndef __CYGWIN__ - fd = syscall(SYS_open, dump_file, O_RDONLY); + fd = syscall(SYS_open, dump_file, O_RDONLY); if (fd < 0) { fprintf(stderr, "couldn't open %s: %s\n", dump_file, strerror(errno)); exit(1); } - if (fstat(fd, &st)) { - perror("stat fails"); - exit(1); - } + if (fstat(fd, &st)) { + perror("stat fails"); + exit(1); + } - if (st.st_size < 1) { - fprintf(stderr, "KML is empty\n"); - exit(1); - } + if (st.st_size < 1) { + fprintf(stderr, "KML is empty\n"); + exit(1); + } - start = buf = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE , fd, 0); - end = start + st.st_size; - close(fd); + start = buf = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE , fd, 0); + end = start + st.st_size; + close(fd); if (start == MAP_FAILED) { - fprintf(stderr, "can't create file mapping\n"); - exit(1); + fprintf(stderr, "can't create file mapping\n"); + exit(1); } #else fd = CreateFile(dump_file, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); size = GetFileSize(fd, NULL); if (size < 1) { - fprintf(stderr, "KML is empty\n"); - exit(1); - } + fprintf(stderr, "KML is empty\n"); + exit(1); + } hmap = CreateFileMapping(fd, NULL, PAGE_READONLY, 0,0, NULL); start = buf = MapViewOfFile(hmap, FILE_MAP_READ, 0, 0, 0); end = buf + size; CloseHandle(fd); if (start == NULL) { - fprintf(stderr, "can't create file mapping\n"); - exit(1); + fprintf(stderr, "can't create file mapping\n"); + exit(1); } #endif /* __CYGWIN__ */ - while (buf < end) { + while (buf < end) { struct dump_hdr *dump_hdr = (struct dump_hdr *) buf; struct libcfs_ioctl_hdr * data; char tmp[8096]; @@ -307,7 +324,7 @@ parse_dump(char * dump_file, ioc_handler_t ioc_func) } buf += data->ioc_len + sizeof(*dump_hdr); - } + } #ifndef __CYGWIN__ munmap(start, end - start); @@ -316,7 +333,7 @@ parse_dump(char * dump_file, ioc_handler_t ioc_func) CloseHandle(hmap); #endif - return 0; + return 0; } int @@ -326,8 +343,8 @@ jt_ioc_dump(int argc, char **argv) fprintf(stderr, "usage: %s [hostname]\n", argv[0]); return 0; } - printf("setting dumpfile to: %s\n", argv[1]); - - set_ioctl_dump(argv[1]); - return 0; + printf("setting dumpfile to: %s\n", argv[1]); + + set_ioctl_dump(argv[1]); + return 0; } diff --git a/lnet/utils/portals.c b/lnet/utils/portals.c index ed3aa9f..39f9d1c 100644 --- a/lnet/utils/portals.c +++ b/lnet/utils/portals.c @@ -264,7 +264,8 @@ int g_net_is_compatible (char *cmd, ...) int ptl_initialize(int argc, char **argv) { - register_ioc_dev(LNET_DEV_ID, LNET_DEV_PATH); + register_ioc_dev(LNET_DEV_ID, LNET_DEV_PATH, + LNET_DEV_MAJOR, LNET_DEV_MINOR); return 0; } @@ -1204,8 +1205,8 @@ jt_ptl_print_routes (int argc, char **argv) struct libcfs_ioctl_data data; int rc; int index; - __u32 net; - lnet_nid_t nid; + __u32 net; + lnet_nid_t nid; unsigned int hops; int alive; -- 1.8.3.1