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 if (pcfg->pcfg_version != PORTALS_CFG_VERSION) {
281 mutex_down(&nal_cmd_mutex);
282 cmd = libcfs_find_nal_cmd_handler(nal);
284 CDEBUG(D_IOCTL, "calling handler nal: %x, cmd: %d\n", nal,
286 rc = cmd->nch_handler(pcfg, cmd->nch_private);
288 CERROR("invalid nal: %x, cmd: %d\n", nal, pcfg->pcfg_command);
290 mutex_up(&nal_cmd_mutex);
295 EXPORT_SYMBOL(libcfs_nal_cmd);
297 static struct rw_semaphore ioctl_list_sem;
298 static struct list_head ioctl_list;
300 int libcfs_register_ioctl(struct libcfs_ioctl_handler *hand)
303 down_read(&ioctl_list_sem);
304 if (!list_empty(&hand->item))
306 up_read(&ioctl_list_sem);
309 down_write(&ioctl_list_sem);
310 list_add_tail(&hand->item, &ioctl_list);
311 up_write(&ioctl_list_sem);
315 EXPORT_SYMBOL(libcfs_register_ioctl);
317 int libcfs_deregister_ioctl(struct libcfs_ioctl_handler *hand)
320 down_read(&ioctl_list_sem);
321 if (list_empty(&hand->item))
323 up_read(&ioctl_list_sem);
326 down_write(&ioctl_list_sem);
327 list_del_init(&hand->item);
328 up_write(&ioctl_list_sem);
332 EXPORT_SYMBOL(libcfs_deregister_ioctl);
334 static int libcfs_ioctl(struct cfs_psdev_file *pfile, unsigned long cmd, void *arg)
338 struct portal_ioctl_data *data;
341 /* 'cmd' and permissions get checked in our arch-specific caller */
343 if (portal_ioctl_getdata(buf, buf + 800, (void *)arg)) {
344 CERROR("PORTALS ioctl: data error\n");
347 data = (struct portal_ioctl_data *)buf;
350 case IOC_PORTAL_CLEAR_DEBUG:
351 portals_debug_clear_buffer();
354 * case IOC_PORTAL_PANIC:
355 * Handled in arch/cfs_module.c
357 case IOC_PORTAL_MARK_DEBUG:
358 if (data->ioc_inlbuf1 == NULL ||
359 data->ioc_inlbuf1[data->ioc_inllen1 - 1] != '\0')
361 portals_debug_mark_buffer(data->ioc_inlbuf1);
364 case IOC_PORTAL_LWT_CONTROL:
365 err = lwt_control (data->ioc_flags, data->ioc_misc);
368 case IOC_PORTAL_LWT_SNAPSHOT: {
373 err = lwt_snapshot (&now, &ncpu, &total_size,
374 data->ioc_pbuf1, data->ioc_plen1);
376 data->ioc_count = ncpu;
377 data->ioc_misc = total_size;
379 /* Hedge against broken user/kernel typedefs (e.g. cycles_t) */
380 data->ioc_nid2 = sizeof(lwt_event_t);
381 data->ioc_nid3 = offsetof(lwt_event_t, lwte_where);
384 copy_to_user((char *)arg, data, sizeof (*data)))
389 case IOC_PORTAL_LWT_LOOKUP_STRING:
390 err = lwt_lookup_string (&data->ioc_count, data->ioc_pbuf1,
391 data->ioc_pbuf2, data->ioc_plen2);
393 copy_to_user((char *)arg, data, sizeof (*data)))
397 case IOC_PORTAL_NAL_CMD: {
398 struct portals_cfg pcfg;
400 if (data->ioc_plen1 != sizeof(pcfg)) {
401 CERROR("Bad ioc_plen1 %d (wanted "LPSZ")\n",
402 data->ioc_plen1, sizeof(pcfg));
407 if (copy_from_user(&pcfg, (void *)data->ioc_pbuf1,
413 CDEBUG (D_IOCTL, "nal command nal %x cmd %d\n", pcfg.pcfg_nal,
415 if (pcfg.pcfg_version != PORTALS_CFG_VERSION) {
416 /* set this so userspace can tell when they
417 * have an incompatible version and print a
418 * decent message to the user
420 pcfg.pcfg_version = PORTALS_CFG_VERSION;
421 if (copy_to_user((char *)data->ioc_pbuf1, &pcfg,
427 err = libcfs_nal_cmd(&pcfg);
430 copy_to_user((char *)data->ioc_pbuf1, &pcfg,
437 case IOC_PORTAL_MEMHOG:
438 if (pfile->private_data == NULL) {
441 kportal_memhog_free(pfile->private_data);
442 /* XXX The ioc_flags is not GFP flags now, need to be fixed */
443 err = kportal_memhog_alloc(pfile->private_data,
447 kportal_memhog_free(pfile->private_data);
452 struct libcfs_ioctl_handler *hand;
454 down_read(&ioctl_list_sem);
455 list_for_each_entry(hand, &ioctl_list, item) {
456 err = hand->handle_ioctl(data, cmd, (unsigned long)arg);
460 up_read(&ioctl_list_sem);
467 struct cfs_psdev_ops libcfs_psdev_ops = {
469 libcfs_psdev_release,
475 extern int insert_proc(void);
476 extern void remove_proc(void);
477 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
478 MODULE_DESCRIPTION("Portals v3.1");
479 MODULE_LICENSE("GPL");
481 extern cfs_psdev_t libcfs_dev;
482 extern struct rw_semaphore tracefile_sem;
483 extern struct semaphore trace_thread_sem;
485 extern int libcfs_arch_init(void);
486 extern void libcfs_arch_cleanup(void);
488 static int init_libcfs_module(void)
493 init_rwsem(&tracefile_sem);
494 init_mutex(&trace_thread_sem);
495 init_mutex(&nal_cmd_mutex);
496 init_rwsem(&ioctl_list_sem);
497 CFS_INIT_LIST_HEAD(&ioctl_list);
499 rc = portals_debug_init(5 * 1024 * 1024);
501 printk(KERN_ERR "LustreError: portals_debug_init: %d\n", rc);
508 CERROR("lwt_init: error %d\n", rc);
512 rc = cfs_psdev_register(&libcfs_dev);
514 CERROR("misc_register: error %d\n", rc);
520 CERROR("insert_proc: error %d\n", rc);
521 goto cleanup_deregister;
524 CDEBUG (D_OTHER, "portals setup OK\n");
528 cfs_psdev_deregister(&libcfs_dev);
534 portals_debug_cleanup();
538 static void exit_libcfs_module(void)
544 CDEBUG(D_MALLOC, "before Portals cleanup: kmem %d\n",
545 atomic_read(&portal_kmemory));
547 rc = cfs_psdev_deregister(&libcfs_dev);
549 CERROR("misc_deregister error %d\n", rc);
555 if (atomic_read(&portal_kmemory) != 0)
556 CERROR("Portals memory leaked: %d bytes\n",
557 atomic_read(&portal_kmemory));
559 rc = portals_debug_cleanup();
561 printk(KERN_ERR "LustreError: portals_debug_cleanup: %d\n", rc);
562 libcfs_arch_cleanup();
565 EXPORT_SYMBOL(kportal_assertion_failed);
567 cfs_module(libcfs, "1.0.0", init_libcfs_module, exit_libcfs_module);