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 extern void (kping_client)(struct portal_ioctl_data *);
57 void kportal_assertion_failed(char *expr, char *file, const char *func,
60 portals_debug_msg(0, D_EMERG, file, func, line, CDEBUG_STACK,
61 "ASSERTION(%s) failed\n", expr);
62 LBUG_WITH_LOC(file, func, line);
67 kportal_daemonize (char *str)
69 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,63))
73 snprintf (current->comm, sizeof (current->comm), "%s", str);
78 kportal_memhog_free (struct portals_device_userstate *pdu)
80 struct page **level0p = &pdu->pdu_memhog_root_page;
81 struct page **level1p;
82 struct page **level2p;
86 if (*level0p != NULL) {
88 level1p = (struct page **)page_address(*level0p);
91 while (count1 < PAGE_SIZE/sizeof(struct page *) &&
94 level2p = (struct page **)page_address(*level1p);
97 while (count2 < PAGE_SIZE/sizeof(struct page *) &&
100 __free_page(*level2p);
101 pdu->pdu_memhog_pages--;
106 __free_page(*level1p);
107 pdu->pdu_memhog_pages--;
112 __free_page(*level0p);
113 pdu->pdu_memhog_pages--;
118 LASSERT (pdu->pdu_memhog_pages == 0);
122 kportal_memhog_alloc (struct portals_device_userstate *pdu, int npages, int flags)
124 struct page **level0p;
125 struct page **level1p;
126 struct page **level2p;
130 LASSERT (pdu->pdu_memhog_pages == 0);
131 LASSERT (pdu->pdu_memhog_root_page == NULL);
139 level0p = &pdu->pdu_memhog_root_page;
140 *level0p = alloc_page(flags);
141 if (*level0p == NULL)
143 pdu->pdu_memhog_pages++;
145 level1p = (struct page **)page_address(*level0p);
147 memset(level1p, 0, PAGE_SIZE);
149 while (pdu->pdu_memhog_pages < npages &&
150 count1 < PAGE_SIZE/sizeof(struct page *)) {
152 if (signal_pending(current))
155 *level1p = alloc_page(flags);
156 if (*level1p == NULL)
158 pdu->pdu_memhog_pages++;
160 level2p = (struct page **)page_address(*level1p);
162 memset(level2p, 0, PAGE_SIZE);
164 while (pdu->pdu_memhog_pages < npages &&
165 count2 < PAGE_SIZE/sizeof(struct page *)) {
167 if (signal_pending(current))
170 *level2p = alloc_page(flags);
171 if (*level2p == NULL)
173 pdu->pdu_memhog_pages++;
187 kportal_blockallsigs ()
191 SIGNAL_MASK_LOCK(current, flags);
192 sigfillset(¤t->blocked);
194 SIGNAL_MASK_UNLOCK(current, flags);
197 /* called when opening /dev/device */
198 static int libcfs_psdev_open(struct inode * inode, struct file * file)
200 struct portals_device_userstate *pdu;
208 PORTAL_ALLOC(pdu, sizeof(*pdu));
210 pdu->pdu_memhog_pages = 0;
211 pdu->pdu_memhog_root_page = NULL;
213 file->private_data = pdu;
218 /* called when closing /dev/device */
219 static int libcfs_psdev_release(struct inode * inode, struct file * file)
221 struct portals_device_userstate *pdu;
227 pdu = file->private_data;
229 kportal_memhog_free(pdu);
230 PORTAL_FREE(pdu, sizeof(*pdu));
237 static inline void freedata(void *data, int len)
239 PORTAL_FREE(data, len);
242 static DECLARE_RWSEM(ioctl_list_sem);
243 static LIST_HEAD(ioctl_list);
245 int libcfs_register_ioctl(struct libcfs_ioctl_handler *hand)
248 down_read(&ioctl_list_sem);
249 if (!list_empty(&hand->item))
251 up_read(&ioctl_list_sem);
254 down_write(&ioctl_list_sem);
255 list_add_tail(&hand->item, &ioctl_list);
256 up_write(&ioctl_list_sem);
260 EXPORT_SYMBOL(libcfs_register_ioctl);
262 int libcfs_deregister_ioctl(struct libcfs_ioctl_handler *hand)
265 down_read(&ioctl_list_sem);
266 if (list_empty(&hand->item))
268 up_read(&ioctl_list_sem);
271 down_write(&ioctl_list_sem);
272 list_del_init(&hand->item);
273 up_write(&ioctl_list_sem);
277 EXPORT_SYMBOL(libcfs_deregister_ioctl);
279 static int libcfs_ioctl(struct inode *inode, struct file *file,
280 unsigned int cmd, unsigned long arg)
284 struct portal_ioctl_data *data;
287 if (current->fsuid != 0)
288 RETURN(err = -EACCES);
290 if ( _IOC_TYPE(cmd) != IOC_PORTAL_TYPE ||
291 _IOC_NR(cmd) < IOC_PORTAL_MIN_NR ||
292 _IOC_NR(cmd) > IOC_PORTAL_MAX_NR ) {
293 CDEBUG(D_IOCTL, "invalid ioctl ( type %d, nr %d, size %d )\n",
294 _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd));
298 if (portal_ioctl_getdata(buf, buf + 800, (void *)arg)) {
299 CERROR("PORTALS ioctl: data error\n");
303 data = (struct portal_ioctl_data *)buf;
306 case IOC_PORTAL_SET_DAEMON:
307 RETURN (portals_debug_set_daemon (
308 (unsigned int) data->ioc_count,
309 (unsigned int) data->ioc_inllen1,
310 (char *) data->ioc_inlbuf1,
311 (unsigned int) data->ioc_misc));
312 case IOC_PORTAL_GET_DEBUG: {
313 __s32 size = portals_debug_copy_to_user(data->ioc_pbuf1,
319 data->ioc_size = size;
320 err = copy_to_user((char *)arg, data, sizeof(*data));
323 case IOC_PORTAL_CLEAR_DEBUG:
324 portals_debug_clear_buffer();
326 case IOC_PORTAL_PANIC:
327 if (!capable (CAP_SYS_BOOT))
329 panic("debugctl-invoked panic");
331 case IOC_PORTAL_MARK_DEBUG:
332 if (data->ioc_inlbuf1 == NULL ||
333 data->ioc_inlbuf1[data->ioc_inllen1 - 1] != '\0')
335 portals_debug_mark_buffer(data->ioc_inlbuf1);
338 case IOC_PORTAL_LWT_CONTROL:
339 err = lwt_control (data->ioc_flags, data->ioc_misc);
342 case IOC_PORTAL_LWT_SNAPSHOT:
343 err = lwt_snapshot (&data->ioc_nid,
344 &data->ioc_count, &data->ioc_misc,
345 data->ioc_pbuf1, data->ioc_plen1);
347 copy_to_user((char *)arg, data, sizeof (*data)))
351 case IOC_PORTAL_LWT_LOOKUP_STRING:
352 err = lwt_lookup_string (&data->ioc_count, data->ioc_pbuf1,
353 data->ioc_pbuf2, data->ioc_plen2);
355 copy_to_user((char *)arg, data, sizeof (*data)))
359 case IOC_PORTAL_MEMHOG:
360 if (!capable (CAP_SYS_ADMIN))
362 else if (file->private_data == NULL) {
365 kportal_memhog_free(file->private_data);
366 err = kportal_memhog_alloc(file->private_data,
370 kportal_memhog_free(file->private_data);
375 struct libcfs_ioctl_handler *hand;
377 down_read(&ioctl_list_sem);
378 list_for_each_entry(hand, &ioctl_list, item) {
379 err = hand->handle_ioctl(data, cmd, arg);
383 up_read(&ioctl_list_sem);
391 static struct file_operations libcfs_fops = {
393 open: libcfs_psdev_open,
394 release: libcfs_psdev_release
398 static struct miscdevice libcfs_dev = {
404 extern int insert_proc(void);
405 extern void remove_proc(void);
406 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
407 MODULE_DESCRIPTION("Portals v3.1");
408 MODULE_LICENSE("GPL");
410 static int init_libcfs_module(void)
414 rc = portals_debug_init(5 * 1024 * 1024);
416 printk(KERN_ERR "LustreError: portals_debug_init: %d\n", rc);
423 CERROR("lwt_init: error %d\n", rc);
427 rc = misc_register(&libcfs_dev);
429 CERROR("misc_register: error %d\n", rc);
435 CERROR("insert_proc: error %d\n", rc);
436 goto cleanup_deregister;
439 CDEBUG (D_OTHER, "portals setup OK\n");
443 misc_deregister(&libcfs_dev);
449 portals_debug_cleanup();
453 static void exit_libcfs_module(void)
459 CDEBUG(D_MALLOC, "before Portals cleanup: kmem %d\n",
460 atomic_read(&portal_kmemory));
463 rc = misc_deregister(&libcfs_dev);
465 CERROR("misc_deregister error %d\n", rc);
471 if (atomic_read(&portal_kmemory) != 0)
472 CERROR("Portals memory leaked: %d bytes\n",
473 atomic_read(&portal_kmemory));
475 rc = portals_debug_cleanup();
477 printk(KERN_ERR "LustreError: portals_debug_cleanup: %d\n", rc);
480 EXPORT_SYMBOL(kportal_daemonize);
481 EXPORT_SYMBOL(kportal_blockallsigs);
482 EXPORT_SYMBOL(kportal_assertion_failed);
484 module_init(init_libcfs_module);
485 module_exit(exit_libcfs_module);