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 Lustre, http://www.lustre.org.
8 * Lustre 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 * Lustre 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 Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 # define EXPORT_SYMTAB
25 #define DEBUG_SUBSYSTEM S_PORTALS
27 #include <linux/config.h>
28 #include <linux/module.h>
29 #include <linux/kernel.h>
31 #include <linux/string.h>
32 #include <linux/stat.h>
33 #include <linux/init.h>
34 #include <linux/errno.h>
35 #include <linux/smp_lock.h>
36 #include <linux/unistd.h>
38 #include <asm/system.h>
39 #include <asm/uaccess.h>
42 #include <linux/stat.h>
43 #include <asm/uaccess.h>
44 #include <asm/segment.h>
45 #include <linux/miscdevice.h>
47 #include <portals/lib-p30.h>
48 #include <portals/p30.h>
49 #include <linux/kp30.h>
50 #include <linux/portals_compat25.h>
52 #define PORTAL_MINOR 240
54 struct nal_cmd_handler {
56 nal_cmd_handler_fn *nch_handler;
60 static struct nal_cmd_handler nal_cmd[16];
61 static DECLARE_MUTEX(nal_cmd_sem);
64 void kportal_assertion_failed(char *expr, char *file, const char *func,
67 portals_debug_msg(0, D_EMERG, file, func, line, CDEBUG_STACK,
68 "ASSERTION(%s) failed\n", expr);
69 LBUG_WITH_LOC(file, func, line);
74 kportal_daemonize (char *str)
76 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,63))
80 snprintf (current->comm, sizeof (current->comm), "%s", str);
85 kportal_memhog_free (struct portals_device_userstate *pdu)
87 struct page **level0p = &pdu->pdu_memhog_root_page;
88 struct page **level1p;
89 struct page **level2p;
93 if (*level0p != NULL) {
95 level1p = (struct page **)page_address(*level0p);
98 while (count1 < PAGE_SIZE/sizeof(struct page *) &&
101 level2p = (struct page **)page_address(*level1p);
104 while (count2 < PAGE_SIZE/sizeof(struct page *) &&
107 __free_page(*level2p);
108 pdu->pdu_memhog_pages--;
113 __free_page(*level1p);
114 pdu->pdu_memhog_pages--;
119 __free_page(*level0p);
120 pdu->pdu_memhog_pages--;
125 LASSERT (pdu->pdu_memhog_pages == 0);
129 kportal_memhog_alloc (struct portals_device_userstate *pdu, int npages, int flags)
131 struct page **level0p;
132 struct page **level1p;
133 struct page **level2p;
137 LASSERT (pdu->pdu_memhog_pages == 0);
138 LASSERT (pdu->pdu_memhog_root_page == NULL);
146 level0p = &pdu->pdu_memhog_root_page;
147 *level0p = alloc_page(flags);
148 if (*level0p == NULL)
150 pdu->pdu_memhog_pages++;
152 level1p = (struct page **)page_address(*level0p);
154 memset(level1p, 0, PAGE_SIZE);
156 while (pdu->pdu_memhog_pages < npages &&
157 count1 < PAGE_SIZE/sizeof(struct page *)) {
159 if (signal_pending(current))
162 *level1p = alloc_page(flags);
163 if (*level1p == NULL)
165 pdu->pdu_memhog_pages++;
167 level2p = (struct page **)page_address(*level1p);
169 memset(level2p, 0, PAGE_SIZE);
171 while (pdu->pdu_memhog_pages < npages &&
172 count2 < PAGE_SIZE/sizeof(struct page *)) {
174 if (signal_pending(current))
177 *level2p = alloc_page(flags);
178 if (*level2p == NULL)
180 pdu->pdu_memhog_pages++;
194 kportal_blockallsigs ()
198 SIGNAL_MASK_LOCK(current, flags);
199 sigfillset(¤t->blocked);
201 SIGNAL_MASK_UNLOCK(current, flags);
204 /* called when opening /dev/device */
205 static int libcfs_psdev_open(struct inode * inode, struct file * file)
207 struct portals_device_userstate *pdu;
215 PORTAL_ALLOC(pdu, sizeof(*pdu));
217 pdu->pdu_memhog_pages = 0;
218 pdu->pdu_memhog_root_page = NULL;
220 file->private_data = pdu;
225 /* called when closing /dev/device */
226 static int libcfs_psdev_release(struct inode * inode, struct file * file)
228 struct portals_device_userstate *pdu;
234 pdu = file->private_data;
236 kportal_memhog_free(pdu);
237 PORTAL_FREE(pdu, sizeof(*pdu));
244 static inline void freedata(void *data, int len)
246 PORTAL_FREE(data, len);
249 struct nal_cmd_handler *
250 libcfs_find_nal_cmd_handler(int nal)
254 for (i = 0; i < sizeof(nal_cmd)/sizeof(nal_cmd[0]); i++)
255 if (nal_cmd[i].nch_handler != NULL &&
256 nal_cmd[i].nch_number == nal)
257 return (&nal_cmd[i]);
263 libcfs_nal_cmd_register(int nal, nal_cmd_handler_fn *handler, void *private)
265 struct nal_cmd_handler *cmd;
269 CDEBUG(D_IOCTL, "Register NAL %d, handler: %p\n", nal, handler);
273 if (libcfs_find_nal_cmd_handler(nal) != NULL) {
279 for (i = 0; i < sizeof(nal_cmd)/sizeof(nal_cmd[0]); i++)
280 if (nal_cmd[i].nch_handler == NULL) {
289 cmd->nch_number = nal;
290 cmd->nch_handler = handler;
291 cmd->nch_private = private;
298 EXPORT_SYMBOL(libcfs_nal_cmd_register);
301 libcfs_nal_cmd_unregister(int nal)
303 struct nal_cmd_handler *cmd;
305 CDEBUG(D_IOCTL, "Unregister NAL %d\n", nal);
308 cmd = libcfs_find_nal_cmd_handler(nal);
309 LASSERT (cmd != NULL);
310 cmd->nch_handler = NULL;
311 cmd->nch_private = NULL;
314 EXPORT_SYMBOL(libcfs_nal_cmd_unregister);
317 libcfs_nal_cmd(struct portals_cfg *pcfg)
319 struct nal_cmd_handler *cmd;
320 __u32 nal = pcfg->pcfg_nal;
325 cmd = libcfs_find_nal_cmd_handler(nal);
327 CDEBUG(D_IOCTL, "calling handler nal: %d, cmd: %d\n", nal,
329 rc = cmd->nch_handler(pcfg, cmd->nch_private);
335 EXPORT_SYMBOL(libcfs_nal_cmd);
337 static DECLARE_RWSEM(ioctl_list_sem);
338 static LIST_HEAD(ioctl_list);
340 int libcfs_register_ioctl(struct libcfs_ioctl_handler *hand)
343 down_read(&ioctl_list_sem);
344 if (!list_empty(&hand->item))
346 up_read(&ioctl_list_sem);
349 down_write(&ioctl_list_sem);
350 list_add_tail(&hand->item, &ioctl_list);
351 up_write(&ioctl_list_sem);
355 EXPORT_SYMBOL(libcfs_register_ioctl);
357 int libcfs_deregister_ioctl(struct libcfs_ioctl_handler *hand)
360 down_read(&ioctl_list_sem);
361 if (list_empty(&hand->item))
363 up_read(&ioctl_list_sem);
366 down_write(&ioctl_list_sem);
367 list_del_init(&hand->item);
368 up_write(&ioctl_list_sem);
372 EXPORT_SYMBOL(libcfs_deregister_ioctl);
374 static int libcfs_ioctl(struct inode *inode, struct file *file,
375 unsigned int cmd, unsigned long arg)
379 struct portal_ioctl_data *data;
382 if (current->fsuid != 0)
383 RETURN(err = -EACCES);
385 if ( _IOC_TYPE(cmd) != IOC_PORTAL_TYPE ||
386 _IOC_NR(cmd) < IOC_PORTAL_MIN_NR ||
387 _IOC_NR(cmd) > IOC_PORTAL_MAX_NR ) {
388 CDEBUG(D_IOCTL, "invalid ioctl ( type %d, nr %d, size %d )\n",
389 _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd));
393 if (portal_ioctl_getdata(buf, buf + 800, (void *)arg)) {
394 CERROR("PORTALS ioctl: data error\n");
398 data = (struct portal_ioctl_data *)buf;
401 case IOC_PORTAL_SET_DAEMON:
402 RETURN (portals_debug_set_daemon (
403 (unsigned int) data->ioc_count,
404 (unsigned int) data->ioc_inllen1,
405 (char *) data->ioc_inlbuf1,
406 (unsigned int) data->ioc_misc));
407 case IOC_PORTAL_GET_DEBUG: {
408 __s32 size = portals_debug_copy_to_user(data->ioc_pbuf1,
414 data->ioc_size = size;
415 err = copy_to_user((char *)arg, data, sizeof(*data));
418 case IOC_PORTAL_CLEAR_DEBUG:
419 portals_debug_clear_buffer();
421 case IOC_PORTAL_PANIC:
422 if (!capable (CAP_SYS_BOOT))
424 panic("debugctl-invoked panic");
426 case IOC_PORTAL_MARK_DEBUG:
427 if (data->ioc_inlbuf1 == NULL ||
428 data->ioc_inlbuf1[data->ioc_inllen1 - 1] != '\0')
430 portals_debug_mark_buffer(data->ioc_inlbuf1);
433 case IOC_PORTAL_LWT_CONTROL:
434 err = lwt_control (data->ioc_flags, data->ioc_misc);
437 case IOC_PORTAL_LWT_SNAPSHOT: {
442 err = lwt_snapshot (&now, &ncpu, &total_size,
443 data->ioc_pbuf1, data->ioc_plen1);
445 data->ioc_count = ncpu;
446 data->ioc_misc = total_size;
449 copy_to_user((char *)arg, data, sizeof (*data)))
454 case IOC_PORTAL_LWT_LOOKUP_STRING:
455 err = lwt_lookup_string (&data->ioc_count, data->ioc_pbuf1,
456 data->ioc_pbuf2, data->ioc_plen2);
458 copy_to_user((char *)arg, data, sizeof (*data)))
462 case IOC_PORTAL_NAL_CMD: {
463 struct portals_cfg pcfg;
465 if (data->ioc_plen1 != sizeof(pcfg)) {
466 CERROR("Bad ioc_plen1 %d (wanted %d)\n",
467 data->ioc_plen1, sizeof(pcfg));
472 if (copy_from_user(&pcfg, (void *)data->ioc_pbuf1,
478 CDEBUG (D_IOCTL, "nal command nal %d cmd %d\n", pcfg.pcfg_nal,
480 err = libcfs_nal_cmd(&pcfg);
483 copy_to_user((char *)data->ioc_pbuf1, &pcfg,
489 case IOC_PORTAL_MEMHOG:
490 if (!capable (CAP_SYS_ADMIN))
492 else if (file->private_data == NULL) {
495 kportal_memhog_free(file->private_data);
496 err = kportal_memhog_alloc(file->private_data,
500 kportal_memhog_free(file->private_data);
505 struct libcfs_ioctl_handler *hand;
507 down_read(&ioctl_list_sem);
508 list_for_each_entry(hand, &ioctl_list, item) {
509 err = hand->handle_ioctl(data, cmd, arg);
513 up_read(&ioctl_list_sem);
521 static struct file_operations libcfs_fops = {
523 open: libcfs_psdev_open,
524 release: libcfs_psdev_release
528 static struct miscdevice libcfs_dev = {
534 extern int insert_proc(void);
535 extern void remove_proc(void);
536 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
537 MODULE_DESCRIPTION("Portals v3.1");
538 MODULE_LICENSE("GPL");
540 static int init_libcfs_module(void)
544 rc = portals_debug_init(5 * 1024 * 1024);
546 printk(KERN_ERR "LustreError: portals_debug_init: %d\n", rc);
553 CERROR("lwt_init: error %d\n", rc);
557 rc = misc_register(&libcfs_dev);
559 CERROR("misc_register: error %d\n", rc);
565 CERROR("insert_proc: error %d\n", rc);
566 goto cleanup_deregister;
569 CDEBUG (D_OTHER, "portals setup OK\n");
573 misc_deregister(&libcfs_dev);
579 portals_debug_cleanup();
583 static void exit_libcfs_module(void)
589 CDEBUG(D_MALLOC, "before Portals cleanup: kmem %d\n",
590 atomic_read(&portal_kmemory));
593 rc = misc_deregister(&libcfs_dev);
595 CERROR("misc_deregister error %d\n", rc);
601 if (atomic_read(&portal_kmemory) != 0)
602 CERROR("Portals memory leaked: %d bytes\n",
603 atomic_read(&portal_kmemory));
605 rc = portals_debug_cleanup();
607 printk(KERN_ERR "LustreError: portals_debug_cleanup: %d\n", rc);
610 EXPORT_SYMBOL(kportal_daemonize);
611 EXPORT_SYMBOL(kportal_blockallsigs);
612 EXPORT_SYMBOL(kportal_assertion_failed);
614 module_init(init_libcfs_module);
615 module_exit(exit_libcfs_module);