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 {
55 nal_cmd_handler_fn *nch_handler;
59 static struct nal_cmd_handler nal_cmd[NAL_MAX_NR + 1];
60 static DECLARE_MUTEX(nal_cmd_sem);
63 void kportal_assertion_failed(char *expr, char *file, const char *func,
66 portals_debug_msg(0, D_EMERG, file, func, line, CDEBUG_STACK,
67 "ASSERTION(%s) failed\n", expr);
68 LBUG_WITH_LOC(file, func, line);
73 kportal_daemonize (char *str)
75 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,63))
79 snprintf (current->comm, sizeof (current->comm), "%s", str);
84 kportal_memhog_free (struct portals_device_userstate *pdu)
86 struct page **level0p = &pdu->pdu_memhog_root_page;
87 struct page **level1p;
88 struct page **level2p;
92 if (*level0p != NULL) {
94 level1p = (struct page **)page_address(*level0p);
97 while (count1 < PAGE_SIZE/sizeof(struct page *) &&
100 level2p = (struct page **)page_address(*level1p);
103 while (count2 < PAGE_SIZE/sizeof(struct page *) &&
106 __free_page(*level2p);
107 pdu->pdu_memhog_pages--;
112 __free_page(*level1p);
113 pdu->pdu_memhog_pages--;
118 __free_page(*level0p);
119 pdu->pdu_memhog_pages--;
124 LASSERT (pdu->pdu_memhog_pages == 0);
128 kportal_memhog_alloc (struct portals_device_userstate *pdu, int npages, int flags)
130 struct page **level0p;
131 struct page **level1p;
132 struct page **level2p;
136 LASSERT (pdu->pdu_memhog_pages == 0);
137 LASSERT (pdu->pdu_memhog_root_page == NULL);
145 level0p = &pdu->pdu_memhog_root_page;
146 *level0p = alloc_page(flags);
147 if (*level0p == NULL)
149 pdu->pdu_memhog_pages++;
151 level1p = (struct page **)page_address(*level0p);
153 memset(level1p, 0, PAGE_SIZE);
155 while (pdu->pdu_memhog_pages < npages &&
156 count1 < PAGE_SIZE/sizeof(struct page *)) {
158 if (signal_pending(current))
161 *level1p = alloc_page(flags);
162 if (*level1p == NULL)
164 pdu->pdu_memhog_pages++;
166 level2p = (struct page **)page_address(*level1p);
168 memset(level2p, 0, PAGE_SIZE);
170 while (pdu->pdu_memhog_pages < npages &&
171 count2 < PAGE_SIZE/sizeof(struct page *)) {
173 if (signal_pending(current))
176 *level2p = alloc_page(flags);
177 if (*level2p == NULL)
179 pdu->pdu_memhog_pages++;
193 kportal_blockallsigs ()
197 SIGNAL_MASK_LOCK(current, flags);
198 sigfillset(¤t->blocked);
200 SIGNAL_MASK_UNLOCK(current, flags);
203 /* called when opening /dev/device */
204 static int libcfs_psdev_open(struct inode * inode, struct file * file)
206 struct portals_device_userstate *pdu;
214 PORTAL_ALLOC(pdu, sizeof(*pdu));
216 pdu->pdu_memhog_pages = 0;
217 pdu->pdu_memhog_root_page = NULL;
219 file->private_data = pdu;
224 /* called when closing /dev/device */
225 static int libcfs_psdev_release(struct inode * inode, struct file * file)
227 struct portals_device_userstate *pdu;
233 pdu = file->private_data;
235 kportal_memhog_free(pdu);
236 PORTAL_FREE(pdu, sizeof(*pdu));
243 static inline void freedata(void *data, int len)
245 PORTAL_FREE(data, len);
249 libcfs_nal_cmd_register(int nal, nal_cmd_handler_fn *handler, void *private)
253 CDEBUG(D_IOCTL, "Register NAL %d, handler: %p\n", nal, handler);
255 if (nal > 0 && nal <= NAL_MAX_NR) {
257 if (nal_cmd[nal].nch_handler != NULL)
260 nal_cmd[nal].nch_handler = handler;
261 nal_cmd[nal].nch_private = private;
267 EXPORT_SYMBOL(libcfs_nal_cmd_register);
270 libcfs_nal_cmd_unregister(int nal)
272 CDEBUG(D_IOCTL, "Unregister NAL %d\n", nal);
274 LASSERT(nal > 0 && nal <= NAL_MAX_NR);
275 LASSERT(nal_cmd[nal].nch_handler != NULL);
278 nal_cmd[nal].nch_handler = NULL;
279 nal_cmd[nal].nch_private = NULL;
282 EXPORT_SYMBOL(libcfs_nal_cmd_unregister);
285 libcfs_nal_cmd(struct portals_cfg *pcfg)
287 __u32 nal = pcfg->pcfg_nal;
292 if (nal > 0 && nal <= NAL_MAX_NR &&
293 nal_cmd[nal].nch_handler != NULL) {
294 CDEBUG(D_IOCTL, "calling handler nal: %d, cmd: %d\n", nal,
296 rc = nal_cmd[nal].nch_handler(pcfg, nal_cmd[nal].nch_private);
302 EXPORT_SYMBOL(libcfs_nal_cmd);
304 static DECLARE_RWSEM(ioctl_list_sem);
305 static LIST_HEAD(ioctl_list);
307 int libcfs_register_ioctl(struct libcfs_ioctl_handler *hand)
310 down_read(&ioctl_list_sem);
311 if (!list_empty(&hand->item))
313 up_read(&ioctl_list_sem);
316 down_write(&ioctl_list_sem);
317 list_add_tail(&hand->item, &ioctl_list);
318 up_write(&ioctl_list_sem);
322 EXPORT_SYMBOL(libcfs_register_ioctl);
324 int libcfs_deregister_ioctl(struct libcfs_ioctl_handler *hand)
327 down_read(&ioctl_list_sem);
328 if (list_empty(&hand->item))
330 up_read(&ioctl_list_sem);
333 down_write(&ioctl_list_sem);
334 list_del_init(&hand->item);
335 up_write(&ioctl_list_sem);
339 EXPORT_SYMBOL(libcfs_deregister_ioctl);
341 static int libcfs_ioctl(struct inode *inode, struct file *file,
342 unsigned int cmd, unsigned long arg)
346 struct portal_ioctl_data *data;
349 if (current->fsuid != 0)
350 RETURN(err = -EACCES);
352 if ( _IOC_TYPE(cmd) != IOC_PORTAL_TYPE ||
353 _IOC_NR(cmd) < IOC_PORTAL_MIN_NR ||
354 _IOC_NR(cmd) > IOC_PORTAL_MAX_NR ) {
355 CDEBUG(D_IOCTL, "invalid ioctl ( type %d, nr %d, size %d )\n",
356 _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd));
360 if (portal_ioctl_getdata(buf, buf + 800, (void *)arg)) {
361 CERROR("PORTALS ioctl: data error\n");
365 data = (struct portal_ioctl_data *)buf;
368 case IOC_PORTAL_SET_DAEMON:
369 RETURN (portals_debug_set_daemon (
370 (unsigned int) data->ioc_count,
371 (unsigned int) data->ioc_inllen1,
372 (char *) data->ioc_inlbuf1,
373 (unsigned int) data->ioc_misc));
374 case IOC_PORTAL_GET_DEBUG: {
375 __s32 size = portals_debug_copy_to_user(data->ioc_pbuf1,
381 data->ioc_size = size;
382 err = copy_to_user((char *)arg, data, sizeof(*data));
385 case IOC_PORTAL_CLEAR_DEBUG:
386 portals_debug_clear_buffer();
388 case IOC_PORTAL_PANIC:
389 if (!capable (CAP_SYS_BOOT))
391 panic("debugctl-invoked panic");
393 case IOC_PORTAL_MARK_DEBUG:
394 if (data->ioc_inlbuf1 == NULL ||
395 data->ioc_inlbuf1[data->ioc_inllen1 - 1] != '\0')
397 portals_debug_mark_buffer(data->ioc_inlbuf1);
400 case IOC_PORTAL_LWT_CONTROL:
401 err = lwt_control (data->ioc_flags, data->ioc_misc);
404 case IOC_PORTAL_LWT_SNAPSHOT:
405 err = lwt_snapshot (&data->ioc_nid,
406 &data->ioc_count, &data->ioc_misc,
407 data->ioc_pbuf1, data->ioc_plen1);
409 copy_to_user((char *)arg, data, sizeof (*data)))
413 case IOC_PORTAL_LWT_LOOKUP_STRING:
414 err = lwt_lookup_string (&data->ioc_count, data->ioc_pbuf1,
415 data->ioc_pbuf2, data->ioc_plen2);
417 copy_to_user((char *)arg, data, sizeof (*data)))
421 case IOC_PORTAL_NAL_CMD: {
422 struct portals_cfg pcfg;
424 LASSERT (data->ioc_plen1 == sizeof(pcfg));
425 if (copy_from_user(&pcfg, (void *)data->ioc_pbuf1,
431 CDEBUG (D_IOCTL, "nal command nal %d cmd %d\n", pcfg.pcfg_nal,
433 err = libcfs_nal_cmd(&pcfg);
436 copy_to_user((char *)data->ioc_pbuf1, &pcfg,
442 case IOC_PORTAL_MEMHOG:
443 if (!capable (CAP_SYS_ADMIN))
445 else if (file->private_data == NULL) {
448 kportal_memhog_free(file->private_data);
449 err = kportal_memhog_alloc(file->private_data,
453 kportal_memhog_free(file->private_data);
458 struct libcfs_ioctl_handler *hand;
460 down_read(&ioctl_list_sem);
461 list_for_each_entry(hand, &ioctl_list, item) {
462 err = hand->handle_ioctl(data, cmd, arg);
466 up_read(&ioctl_list_sem);
474 static struct file_operations libcfs_fops = {
476 open: libcfs_psdev_open,
477 release: libcfs_psdev_release
481 static struct miscdevice libcfs_dev = {
487 extern int insert_proc(void);
488 extern void remove_proc(void);
489 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
490 MODULE_DESCRIPTION("Portals v3.1");
491 MODULE_LICENSE("GPL");
493 static int init_libcfs_module(void)
497 rc = portals_debug_init(5 * 1024 * 1024);
499 printk(KERN_ERR "LustreError: portals_debug_init: %d\n", rc);
506 CERROR("lwt_init: error %d\n", rc);
510 rc = misc_register(&libcfs_dev);
512 CERROR("misc_register: error %d\n", rc);
518 CERROR("insert_proc: error %d\n", rc);
519 goto cleanup_deregister;
522 CDEBUG (D_OTHER, "portals setup OK\n");
526 misc_deregister(&libcfs_dev);
532 portals_debug_cleanup();
536 static void exit_libcfs_module(void)
542 CDEBUG(D_MALLOC, "before Portals cleanup: kmem %d\n",
543 atomic_read(&portal_kmemory));
546 rc = misc_deregister(&libcfs_dev);
548 CERROR("misc_deregister error %d\n", rc);
554 if (atomic_read(&portal_kmemory) != 0)
555 CERROR("Portals memory leaked: %d bytes\n",
556 atomic_read(&portal_kmemory));
558 rc = portals_debug_cleanup();
560 printk(KERN_ERR "LustreError: portals_debug_cleanup: %d\n", rc);
563 EXPORT_SYMBOL(kportal_daemonize);
564 EXPORT_SYMBOL(kportal_blockallsigs);
565 EXPORT_SYMBOL(kportal_assertion_failed);
567 module_init(init_libcfs_module);
568 module_exit(exit_libcfs_module);