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);
331 CERROR("invalid nal: %d, cmd: %d\n", nal, pcfg->pcfg_command);
337 EXPORT_SYMBOL(libcfs_nal_cmd);
339 static DECLARE_RWSEM(ioctl_list_sem);
340 static LIST_HEAD(ioctl_list);
342 int libcfs_register_ioctl(struct libcfs_ioctl_handler *hand)
345 down_read(&ioctl_list_sem);
346 if (!list_empty(&hand->item))
348 up_read(&ioctl_list_sem);
351 down_write(&ioctl_list_sem);
352 list_add_tail(&hand->item, &ioctl_list);
353 up_write(&ioctl_list_sem);
357 EXPORT_SYMBOL(libcfs_register_ioctl);
359 int libcfs_deregister_ioctl(struct libcfs_ioctl_handler *hand)
362 down_read(&ioctl_list_sem);
363 if (list_empty(&hand->item))
365 up_read(&ioctl_list_sem);
368 down_write(&ioctl_list_sem);
369 list_del_init(&hand->item);
370 up_write(&ioctl_list_sem);
374 EXPORT_SYMBOL(libcfs_deregister_ioctl);
376 static int libcfs_ioctl(struct inode *inode, struct file *file,
377 unsigned int cmd, unsigned long arg)
381 struct portal_ioctl_data *data;
384 if (current->fsuid != 0)
385 RETURN(err = -EACCES);
387 if ( _IOC_TYPE(cmd) != IOC_PORTAL_TYPE ||
388 _IOC_NR(cmd) < IOC_PORTAL_MIN_NR ||
389 _IOC_NR(cmd) > IOC_PORTAL_MAX_NR ) {
390 CDEBUG(D_IOCTL, "invalid ioctl ( type %d, nr %d, size %d )\n",
391 _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd));
395 if (portal_ioctl_getdata(buf, buf + 800, (void *)arg)) {
396 CERROR("PORTALS ioctl: data error\n");
400 data = (struct portal_ioctl_data *)buf;
403 case IOC_PORTAL_CLEAR_DEBUG:
404 portals_debug_clear_buffer();
406 case IOC_PORTAL_PANIC:
407 if (!capable (CAP_SYS_BOOT))
409 panic("debugctl-invoked panic");
411 case IOC_PORTAL_MARK_DEBUG:
412 if (data->ioc_inlbuf1 == NULL ||
413 data->ioc_inlbuf1[data->ioc_inllen1 - 1] != '\0')
415 portals_debug_mark_buffer(data->ioc_inlbuf1);
418 case IOC_PORTAL_LWT_CONTROL:
419 err = lwt_control (data->ioc_flags, data->ioc_misc);
422 case IOC_PORTAL_LWT_SNAPSHOT: {
427 err = lwt_snapshot (&now, &ncpu, &total_size,
428 data->ioc_pbuf1, data->ioc_plen1);
430 data->ioc_count = ncpu;
431 data->ioc_misc = total_size;
433 /* Hedge against broken user/kernel typedefs (e.g. cycles_t) */
434 data->ioc_nid2 = sizeof(lwt_event_t);
435 data->ioc_nid3 = offsetof(lwt_event_t, lwte_where);
438 copy_to_user((char *)arg, data, sizeof (*data)))
443 case IOC_PORTAL_LWT_LOOKUP_STRING:
444 err = lwt_lookup_string (&data->ioc_count, data->ioc_pbuf1,
445 data->ioc_pbuf2, data->ioc_plen2);
447 copy_to_user((char *)arg, data, sizeof (*data)))
451 case IOC_PORTAL_NAL_CMD: {
452 struct portals_cfg pcfg;
454 if (data->ioc_plen1 != sizeof(pcfg)) {
455 CERROR("Bad ioc_plen1 %d (wanted %d)\n",
456 data->ioc_plen1, sizeof(pcfg));
461 if (copy_from_user(&pcfg, (void *)data->ioc_pbuf1,
467 CDEBUG (D_IOCTL, "nal command nal %d cmd %d\n", pcfg.pcfg_nal,
469 err = libcfs_nal_cmd(&pcfg);
472 copy_to_user((char *)data->ioc_pbuf1, &pcfg,
478 case IOC_PORTAL_MEMHOG:
479 if (!capable (CAP_SYS_ADMIN))
481 else if (file->private_data == NULL) {
484 kportal_memhog_free(file->private_data);
485 err = kportal_memhog_alloc(file->private_data,
489 kportal_memhog_free(file->private_data);
494 struct libcfs_ioctl_handler *hand;
496 down_read(&ioctl_list_sem);
497 list_for_each_entry(hand, &ioctl_list, item) {
498 err = hand->handle_ioctl(data, cmd, arg);
502 up_read(&ioctl_list_sem);
510 static struct file_operations libcfs_fops = {
512 open: libcfs_psdev_open,
513 release: libcfs_psdev_release
517 static struct miscdevice libcfs_dev = {
523 extern int insert_proc(void);
524 extern void remove_proc(void);
525 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
526 MODULE_DESCRIPTION("Portals v3.1");
527 MODULE_LICENSE("GPL");
529 static int init_libcfs_module(void)
533 rc = portals_debug_init(5 * 1024 * 1024);
535 printk(KERN_ERR "LustreError: portals_debug_init: %d\n", rc);
542 CERROR("lwt_init: error %d\n", rc);
546 rc = misc_register(&libcfs_dev);
548 CERROR("misc_register: error %d\n", rc);
554 CERROR("insert_proc: error %d\n", rc);
555 goto cleanup_deregister;
558 CDEBUG (D_OTHER, "portals setup OK\n");
562 misc_deregister(&libcfs_dev);
568 portals_debug_cleanup();
572 static void exit_libcfs_module(void)
578 CDEBUG(D_MALLOC, "before Portals cleanup: kmem %d\n",
579 atomic_read(&portal_kmemory));
581 rc = misc_deregister(&libcfs_dev);
583 CERROR("misc_deregister error %d\n", rc);
589 if (atomic_read(&portal_kmemory) != 0)
590 CERROR("Portals memory leaked: %d bytes\n",
591 atomic_read(&portal_kmemory));
593 rc = portals_debug_cleanup();
595 printk(KERN_ERR "LustreError: portals_debug_cleanup: %d\n", rc);
598 EXPORT_SYMBOL(kportal_daemonize);
599 EXPORT_SYMBOL(kportal_blockallsigs);
600 EXPORT_SYMBOL(kportal_assertion_failed);
602 module_init(init_libcfs_module);
603 module_exit(exit_libcfs_module);