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 %x, 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 %x\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)
320 /* pretend success */
323 struct nal_cmd_handler *cmd;
324 __u32 nal = pcfg->pcfg_nal;
329 cmd = libcfs_find_nal_cmd_handler(nal);
331 CDEBUG(D_IOCTL, "calling handler nal: %x, cmd: %d\n", nal,
333 rc = cmd->nch_handler(pcfg, cmd->nch_private);
335 CERROR("invalid nal: %x, cmd: %d\n", nal, pcfg->pcfg_command);
342 EXPORT_SYMBOL(libcfs_nal_cmd);
344 static DECLARE_RWSEM(ioctl_list_sem);
345 static LIST_HEAD(ioctl_list);
347 int libcfs_register_ioctl(struct libcfs_ioctl_handler *hand)
350 down_read(&ioctl_list_sem);
351 if (!list_empty(&hand->item))
353 up_read(&ioctl_list_sem);
356 down_write(&ioctl_list_sem);
357 list_add_tail(&hand->item, &ioctl_list);
358 up_write(&ioctl_list_sem);
362 EXPORT_SYMBOL(libcfs_register_ioctl);
364 int libcfs_deregister_ioctl(struct libcfs_ioctl_handler *hand)
367 down_read(&ioctl_list_sem);
368 if (list_empty(&hand->item))
370 up_read(&ioctl_list_sem);
373 down_write(&ioctl_list_sem);
374 list_del_init(&hand->item);
375 up_write(&ioctl_list_sem);
379 EXPORT_SYMBOL(libcfs_deregister_ioctl);
381 static int libcfs_ioctl(struct inode *inode, struct file *file,
382 unsigned int cmd, unsigned long arg)
386 struct portal_ioctl_data *data;
389 if (current->fsuid != 0)
390 RETURN(err = -EACCES);
392 if ( _IOC_TYPE(cmd) != IOC_PORTAL_TYPE ||
393 _IOC_NR(cmd) < IOC_PORTAL_MIN_NR ||
394 _IOC_NR(cmd) > IOC_PORTAL_MAX_NR ) {
395 CDEBUG(D_IOCTL, "invalid ioctl ( type %d, nr %d, size %d )\n",
396 _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd));
400 if (portal_ioctl_getdata(buf, buf + 800, (void *)arg)) {
401 CERROR("PORTALS ioctl: data error\n");
405 data = (struct portal_ioctl_data *)buf;
408 case IOC_PORTAL_CLEAR_DEBUG:
409 portals_debug_clear_buffer();
411 case IOC_PORTAL_PANIC:
412 if (!capable (CAP_SYS_BOOT))
414 panic("debugctl-invoked panic");
416 case IOC_PORTAL_MARK_DEBUG:
417 if (data->ioc_inlbuf1 == NULL ||
418 data->ioc_inlbuf1[data->ioc_inllen1 - 1] != '\0')
420 portals_debug_mark_buffer(data->ioc_inlbuf1);
423 case IOC_PORTAL_LWT_CONTROL:
424 err = lwt_control (data->ioc_flags, data->ioc_misc);
427 case IOC_PORTAL_LWT_SNAPSHOT: {
432 err = lwt_snapshot (&now, &ncpu, &total_size,
433 data->ioc_pbuf1, data->ioc_plen1);
435 data->ioc_count = ncpu;
436 data->ioc_misc = total_size;
438 /* Hedge against broken user/kernel typedefs (e.g. cycles_t) */
439 data->ioc_nid2 = sizeof(lwt_event_t);
440 data->ioc_nid3 = offsetof(lwt_event_t, lwte_where);
443 copy_to_user((char *)arg, data, sizeof (*data)))
448 case IOC_PORTAL_LWT_LOOKUP_STRING:
449 err = lwt_lookup_string (&data->ioc_count, data->ioc_pbuf1,
450 data->ioc_pbuf2, data->ioc_plen2);
452 copy_to_user((char *)arg, data, sizeof (*data)))
456 case IOC_PORTAL_NAL_CMD: {
457 struct portals_cfg pcfg;
459 if (data->ioc_plen1 != sizeof(pcfg)) {
460 CERROR("Bad ioc_plen1 %d (wanted %d)\n",
461 data->ioc_plen1, sizeof(pcfg));
466 if (copy_from_user(&pcfg, (void *)data->ioc_pbuf1,
472 CDEBUG (D_IOCTL, "nal command nal %x cmd %d\n", pcfg.pcfg_nal,
474 err = libcfs_nal_cmd(&pcfg);
477 copy_to_user((char *)data->ioc_pbuf1, &pcfg,
483 case IOC_PORTAL_MEMHOG:
484 if (!capable (CAP_SYS_ADMIN))
486 else if (file->private_data == NULL) {
489 kportal_memhog_free(file->private_data);
490 err = kportal_memhog_alloc(file->private_data,
494 kportal_memhog_free(file->private_data);
499 struct libcfs_ioctl_handler *hand;
501 down_read(&ioctl_list_sem);
502 list_for_each_entry(hand, &ioctl_list, item) {
503 err = hand->handle_ioctl(data, cmd, arg);
507 up_read(&ioctl_list_sem);
515 static struct file_operations libcfs_fops = {
517 open: libcfs_psdev_open,
518 release: libcfs_psdev_release
522 static struct miscdevice libcfs_dev = {
528 extern int insert_proc(void);
529 extern void remove_proc(void);
530 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
531 MODULE_DESCRIPTION("Portals v3.1");
532 MODULE_LICENSE("GPL");
534 static int init_libcfs_module(void)
538 rc = portals_debug_init(5 * 1024 * 1024);
540 printk(KERN_ERR "LustreError: portals_debug_init: %d\n", rc);
547 CERROR("lwt_init: error %d\n", rc);
551 rc = misc_register(&libcfs_dev);
553 CERROR("misc_register: error %d\n", rc);
559 CERROR("insert_proc: error %d\n", rc);
560 goto cleanup_deregister;
563 CDEBUG (D_OTHER, "portals setup OK\n");
567 misc_deregister(&libcfs_dev);
573 portals_debug_cleanup();
577 static void exit_libcfs_module(void)
583 CDEBUG(D_MALLOC, "before Portals cleanup: kmem %d\n",
584 atomic_read(&portal_kmemory));
586 rc = misc_deregister(&libcfs_dev);
588 CERROR("misc_deregister error %d\n", rc);
594 if (atomic_read(&portal_kmemory) != 0)
595 CERROR("Portals memory leaked: %d bytes\n",
596 atomic_read(&portal_kmemory));
598 rc = portals_debug_cleanup();
600 printk(KERN_ERR "LustreError: portals_debug_cleanup: %d\n", rc);
603 EXPORT_SYMBOL(kportal_daemonize);
604 EXPORT_SYMBOL(kportal_blockallsigs);
605 EXPORT_SYMBOL(kportal_assertion_failed);
607 module_init(init_libcfs_module);
608 module_exit(exit_libcfs_module);