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 <portals/lib-p30.h>
28 #include <portals/p30.h>
29 #include <libcfs/kp30.h>
31 struct nal_cmd_handler {
33 nal_cmd_handler_fn *nch_handler;
37 static struct nal_cmd_handler nal_cmd[16];
38 struct semaphore nal_cmd_mutex;
41 void kportal_assertion_failed(char *expr, char *file, const char *func,
44 portals_debug_msg(0, D_EMERG, file, func, line, CDEBUG_STACK,
45 "ASSERTION(%s) failed\n", expr);
46 LBUG_WITH_LOC(file, func, line);
51 kportal_memhog_free (struct portals_device_userstate *pdu)
53 cfs_page_t **level0p = &pdu->pdu_memhog_root_page;
59 if (*level0p != NULL) {
61 level1p = (cfs_page_t **)cfs_page_address(*level0p);
64 while (count1 < CFS_PAGE_SIZE/sizeof(cfs_page_t *) &&
67 level2p = (cfs_page_t **)cfs_page_address(*level1p);
70 while (count2 < CFS_PAGE_SIZE/sizeof(cfs_page_t *) &&
73 cfs_free_page(*level2p);
74 pdu->pdu_memhog_pages--;
79 cfs_free_page(*level1p);
80 pdu->pdu_memhog_pages--;
85 cfs_free_page(*level0p);
86 pdu->pdu_memhog_pages--;
91 LASSERT (pdu->pdu_memhog_pages == 0);
95 kportal_memhog_alloc (struct portals_device_userstate *pdu, int npages, int flags)
103 LASSERT (pdu->pdu_memhog_pages == 0);
104 LASSERT (pdu->pdu_memhog_root_page == NULL);
112 level0p = &pdu->pdu_memhog_root_page;
113 *level0p = cfs_alloc_page(flags);
114 if (*level0p == NULL)
116 pdu->pdu_memhog_pages++;
118 level1p = (cfs_page_t **)cfs_page_address(*level0p);
120 memset(level1p, 0, CFS_PAGE_SIZE);
122 while (pdu->pdu_memhog_pages < npages &&
123 count1 < CFS_PAGE_SIZE/sizeof(cfs_page_t *)) {
125 if (cfs_signal_pending(cfs_current()))
128 *level1p = cfs_alloc_page(flags);
129 if (*level1p == NULL)
131 pdu->pdu_memhog_pages++;
133 level2p = (cfs_page_t **)cfs_page_address(*level1p);
135 memset(level2p, 0, CFS_PAGE_SIZE);
137 while (pdu->pdu_memhog_pages < npages &&
138 count2 < CFS_PAGE_SIZE/sizeof(cfs_page_t *)) {
140 if (cfs_signal_pending(cfs_current()))
143 *level2p = cfs_alloc_page(flags);
144 if (*level2p == NULL)
146 pdu->pdu_memhog_pages++;
159 /* called when opening /dev/device */
160 static int libcfs_psdev_open(unsigned long flags, void *args)
162 struct portals_device_userstate *pdu;
167 PORTAL_ALLOC(pdu, sizeof(*pdu));
169 pdu->pdu_memhog_pages = 0;
170 pdu->pdu_memhog_root_page = NULL;
172 *(struct portals_device_userstate **)args = pdu;
177 /* called when closing /dev/device */
178 static int libcfs_psdev_release(unsigned long flags, void *args)
180 struct portals_device_userstate *pdu;
183 pdu = (struct portals_device_userstate *)args;
185 kportal_memhog_free(pdu);
186 PORTAL_FREE(pdu, sizeof(*pdu));
193 static inline void freedata(void *data, int len)
195 PORTAL_FREE(data, len);
198 struct nal_cmd_handler *
199 libcfs_find_nal_cmd_handler(int nal)
203 for (i = 0; i < sizeof(nal_cmd)/sizeof(nal_cmd[0]); i++)
204 if (nal_cmd[i].nch_handler != NULL &&
205 nal_cmd[i].nch_number == nal)
206 return (&nal_cmd[i]);
212 libcfs_nal_cmd_register(int nal, nal_cmd_handler_fn *handler, void *private)
214 struct nal_cmd_handler *cmd;
218 CDEBUG(D_IOCTL, "Register NAL %x, handler: %p\n", nal, handler);
220 mutex_down(&nal_cmd_mutex);
222 if (libcfs_find_nal_cmd_handler(nal) != NULL) {
223 mutex_up (&nal_cmd_mutex);
228 for (i = 0; i < sizeof(nal_cmd)/sizeof(nal_cmd[0]); i++)
229 if (nal_cmd[i].nch_handler == NULL) {
238 cmd->nch_number = nal;
239 cmd->nch_handler = handler;
240 cmd->nch_private = private;
243 mutex_up(&nal_cmd_mutex);
247 EXPORT_SYMBOL(libcfs_nal_cmd_register);
250 libcfs_nal_cmd_unregister(int nal)
252 struct nal_cmd_handler *cmd;
254 CDEBUG(D_IOCTL, "Unregister NAL %x\n", nal);
256 mutex_down(&nal_cmd_mutex);
257 cmd = libcfs_find_nal_cmd_handler(nal);
258 LASSERT (cmd != NULL);
259 cmd->nch_handler = NULL;
260 cmd->nch_private = NULL;
261 mutex_up(&nal_cmd_mutex);
263 EXPORT_SYMBOL(libcfs_nal_cmd_unregister);
266 libcfs_nal_cmd(struct portals_cfg *pcfg)
269 /* pretend success */
272 struct nal_cmd_handler *cmd;
273 __u32 nal = pcfg->pcfg_nal;
277 mutex_down(&nal_cmd_mutex);
278 cmd = libcfs_find_nal_cmd_handler(nal);
280 CDEBUG(D_IOCTL, "calling handler nal: %x, cmd: %d\n", nal,
282 rc = cmd->nch_handler(pcfg, cmd->nch_private);
284 CERROR("invalid nal: %x, cmd: %d\n", nal, pcfg->pcfg_command);
286 mutex_up(&nal_cmd_mutex);
291 EXPORT_SYMBOL(libcfs_nal_cmd);
293 static struct rw_semaphore ioctl_list_sem;
294 static struct list_head ioctl_list;
296 int libcfs_register_ioctl(struct libcfs_ioctl_handler *hand)
299 down_read(&ioctl_list_sem);
300 if (!list_empty(&hand->item))
302 up_read(&ioctl_list_sem);
305 down_write(&ioctl_list_sem);
306 list_add_tail(&hand->item, &ioctl_list);
307 up_write(&ioctl_list_sem);
311 EXPORT_SYMBOL(libcfs_register_ioctl);
313 int libcfs_deregister_ioctl(struct libcfs_ioctl_handler *hand)
316 down_read(&ioctl_list_sem);
317 if (list_empty(&hand->item))
319 up_read(&ioctl_list_sem);
322 down_write(&ioctl_list_sem);
323 list_del_init(&hand->item);
324 up_write(&ioctl_list_sem);
328 EXPORT_SYMBOL(libcfs_deregister_ioctl);
330 static int libcfs_ioctl(struct cfs_psdev_file *pfile, unsigned long cmd, void *arg)
334 struct portal_ioctl_data *data;
337 /* 'cmd' and permissions get checked in our arch-specific caller */
339 if (portal_ioctl_getdata(buf, buf + 800, (void *)arg)) {
340 CERROR("PORTALS ioctl: data error\n");
343 data = (struct portal_ioctl_data *)buf;
346 case IOC_PORTAL_CLEAR_DEBUG:
347 portals_debug_clear_buffer();
350 * case IOC_PORTAL_PANIC:
351 * Handled in arch/cfs_module.c
353 case IOC_PORTAL_MARK_DEBUG:
354 if (data->ioc_inlbuf1 == NULL ||
355 data->ioc_inlbuf1[data->ioc_inllen1 - 1] != '\0')
357 portals_debug_mark_buffer(data->ioc_inlbuf1);
360 case IOC_PORTAL_LWT_CONTROL:
361 err = lwt_control (data->ioc_flags, data->ioc_misc);
364 case IOC_PORTAL_LWT_SNAPSHOT: {
369 err = lwt_snapshot (&now, &ncpu, &total_size,
370 data->ioc_pbuf1, data->ioc_plen1);
372 data->ioc_count = ncpu;
373 data->ioc_misc = total_size;
375 /* Hedge against broken user/kernel typedefs (e.g. cycles_t) */
376 data->ioc_nid2 = sizeof(lwt_event_t);
377 data->ioc_nid3 = offsetof(lwt_event_t, lwte_where);
380 copy_to_user((char *)arg, data, sizeof (*data)))
385 case IOC_PORTAL_LWT_LOOKUP_STRING:
386 err = lwt_lookup_string (&data->ioc_count, data->ioc_pbuf1,
387 data->ioc_pbuf2, data->ioc_plen2);
389 copy_to_user((char *)arg, data, sizeof (*data)))
393 case IOC_PORTAL_NAL_CMD: {
394 struct portals_cfg pcfg;
396 if (data->ioc_plen1 != sizeof(pcfg)) {
397 CERROR("Bad ioc_plen1 %d (wanted "LPSZ")\n",
398 data->ioc_plen1, sizeof(pcfg));
403 if (copy_from_user(&pcfg, (void *)data->ioc_pbuf1,
409 CDEBUG (D_IOCTL, "nal command nal %x cmd %d\n", pcfg.pcfg_nal,
411 err = libcfs_nal_cmd(&pcfg);
414 copy_to_user((char *)data->ioc_pbuf1, &pcfg,
420 case IOC_PORTAL_MEMHOG:
421 if (pfile->private_data == NULL) {
424 kportal_memhog_free(pfile->private_data);
425 /* XXX The ioc_flags is not GFP flags now, need to be fixed */
426 err = kportal_memhog_alloc(pfile->private_data,
430 kportal_memhog_free(pfile->private_data);
435 struct libcfs_ioctl_handler *hand;
437 down_read(&ioctl_list_sem);
438 list_for_each_entry(hand, &ioctl_list, item) {
439 err = hand->handle_ioctl(data, cmd, (unsigned long)arg);
443 up_read(&ioctl_list_sem);
450 struct cfs_psdev_ops libcfs_psdev_ops = {
452 libcfs_psdev_release,
458 extern int insert_proc(void);
459 extern void remove_proc(void);
460 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
461 MODULE_DESCRIPTION("Portals v3.1");
462 MODULE_LICENSE("GPL");
464 extern cfs_psdev_t libcfs_dev;
465 extern struct rw_semaphore tracefile_sem;
466 extern struct semaphore trace_thread_sem;
468 extern int libcfs_arch_init(void);
469 extern void libcfs_arch_cleanup(void);
471 static int init_libcfs_module(void)
476 init_rwsem(&tracefile_sem);
477 init_mutex(&trace_thread_sem);
478 init_mutex(&nal_cmd_mutex);
479 init_rwsem(&ioctl_list_sem);
480 CFS_INIT_LIST_HEAD(&ioctl_list);
482 rc = portals_debug_init(5 * 1024 * 1024);
484 printk(KERN_ERR "LustreError: portals_debug_init: %d\n", rc);
491 CERROR("lwt_init: error %d\n", rc);
495 rc = cfs_psdev_register(&libcfs_dev);
497 CERROR("misc_register: error %d\n", rc);
503 CERROR("insert_proc: error %d\n", rc);
504 goto cleanup_deregister;
507 CDEBUG (D_OTHER, "portals setup OK\n");
511 cfs_psdev_deregister(&libcfs_dev);
517 portals_debug_cleanup();
521 static void exit_libcfs_module(void)
527 CDEBUG(D_MALLOC, "before Portals cleanup: kmem %d\n",
528 atomic_read(&portal_kmemory));
530 rc = cfs_psdev_deregister(&libcfs_dev);
532 CERROR("misc_deregister error %d\n", rc);
538 if (atomic_read(&portal_kmemory) != 0)
539 CERROR("Portals memory leaked: %d bytes\n",
540 atomic_read(&portal_kmemory));
542 rc = portals_debug_cleanup();
544 printk(KERN_ERR "LustreError: portals_debug_cleanup: %d\n", rc);
545 libcfs_arch_cleanup();
548 EXPORT_SYMBOL(kportal_assertion_failed);
550 cfs_module(libcfs, "1.0.0", init_libcfs_module, exit_libcfs_module);