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 *);
56 struct nal_cmd_handler {
57 nal_cmd_handler_t nch_handler;
61 static struct nal_cmd_handler nal_cmd[NAL_MAX_NR + 1];
62 struct semaphore nal_cmd_sem;
65 void kportal_assertion_failed(char *expr, char *file, const char *func,
68 portals_debug_msg(0, D_EMERG, file, func, line, CDEBUG_STACK,
69 "ASSERTION(%s) failed\n", expr);
70 LBUG_WITH_LOC(file, func, line);
75 kportal_daemonize (char *str)
77 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,63))
81 snprintf (current->comm, sizeof (current->comm), "%s", str);
86 kportal_memhog_free (struct portals_device_userstate *pdu)
88 struct page **level0p = &pdu->pdu_memhog_root_page;
89 struct page **level1p;
90 struct page **level2p;
94 if (*level0p != NULL) {
96 level1p = (struct page **)page_address(*level0p);
99 while (count1 < PAGE_SIZE/sizeof(struct page *) &&
102 level2p = (struct page **)page_address(*level1p);
105 while (count2 < PAGE_SIZE/sizeof(struct page *) &&
108 __free_page(*level2p);
109 pdu->pdu_memhog_pages--;
114 __free_page(*level1p);
115 pdu->pdu_memhog_pages--;
120 __free_page(*level0p);
121 pdu->pdu_memhog_pages--;
126 LASSERT (pdu->pdu_memhog_pages == 0);
130 kportal_memhog_alloc (struct portals_device_userstate *pdu, int npages, int flags)
132 struct page **level0p;
133 struct page **level1p;
134 struct page **level2p;
138 LASSERT (pdu->pdu_memhog_pages == 0);
139 LASSERT (pdu->pdu_memhog_root_page == NULL);
147 level0p = &pdu->pdu_memhog_root_page;
148 *level0p = alloc_page(flags);
149 if (*level0p == NULL)
151 pdu->pdu_memhog_pages++;
153 level1p = (struct page **)page_address(*level0p);
155 memset(level1p, 0, PAGE_SIZE);
157 while (pdu->pdu_memhog_pages < npages &&
158 count1 < PAGE_SIZE/sizeof(struct page *)) {
160 if (signal_pending(current))
163 *level1p = alloc_page(flags);
164 if (*level1p == NULL)
166 pdu->pdu_memhog_pages++;
168 level2p = (struct page **)page_address(*level1p);
170 memset(level2p, 0, PAGE_SIZE);
172 while (pdu->pdu_memhog_pages < npages &&
173 count2 < PAGE_SIZE/sizeof(struct page *)) {
175 if (signal_pending(current))
178 *level2p = alloc_page(flags);
179 if (*level2p == NULL)
181 pdu->pdu_memhog_pages++;
195 kportal_blockallsigs ()
199 SIGNAL_MASK_LOCK(current, flags);
200 sigfillset(¤t->blocked);
202 SIGNAL_MASK_UNLOCK(current, flags);
205 /* called when opening /dev/device */
206 static int kportal_psdev_open(struct inode * inode, struct file * file)
208 struct portals_device_userstate *pdu;
216 PORTAL_ALLOC(pdu, sizeof(*pdu));
218 pdu->pdu_memhog_pages = 0;
219 pdu->pdu_memhog_root_page = NULL;
221 file->private_data = pdu;
226 /* called when closing /dev/device */
227 static int kportal_psdev_release(struct inode * inode, struct file * file)
229 struct portals_device_userstate *pdu;
235 pdu = file->private_data;
237 kportal_memhog_free(pdu);
238 PORTAL_FREE(pdu, sizeof(*pdu));
245 static inline void freedata(void *data, int len)
247 PORTAL_FREE(data, len);
251 kportal_add_route(int gateway_nalid, ptl_nid_t gateway_nid,
252 ptl_nid_t lo_nid, ptl_nid_t hi_nid)
255 kpr_control_interface_t *ci;
257 ci = (kpr_control_interface_t *) PORTAL_SYMBOL_GET (kpr_control_interface);
261 rc = ci->kprci_add_route (gateway_nalid, gateway_nid, lo_nid, hi_nid);
263 PORTAL_SYMBOL_PUT(kpr_control_interface);
268 kportal_del_route(int gw_nalid, ptl_nid_t gw_nid,
269 ptl_nid_t lo, ptl_nid_t hi)
272 kpr_control_interface_t *ci;
274 ci = (kpr_control_interface_t *)PORTAL_SYMBOL_GET(kpr_control_interface);
278 rc = ci->kprci_del_route (gw_nalid, gw_nid, lo, hi);
280 PORTAL_SYMBOL_PUT(kpr_control_interface);
285 kportal_notify_router (int gw_nalid, ptl_nid_t gw_nid,
286 int alive, time_t when)
289 kpr_control_interface_t *ci;
291 /* No error if router not preset. Sysadmin is allowed to notify
292 * _everywhere_ when a NID boots or crashes, even if they know
293 * nothing of the peer. */
294 ci = (kpr_control_interface_t *)PORTAL_SYMBOL_GET(kpr_control_interface);
298 rc = ci->kprci_notify (gw_nalid, gw_nid, alive, when);
300 PORTAL_SYMBOL_PUT(kpr_control_interface);
305 kportal_get_route(int index, __u32 *gateway_nalidp, ptl_nid_t *gateway_nidp,
306 ptl_nid_t *lo_nidp, ptl_nid_t *hi_nidp, int *alivep)
309 ptl_nid_t gateway_nid;
314 kpr_control_interface_t *ci;
316 ci = (kpr_control_interface_t *) PORTAL_SYMBOL_GET(kpr_control_interface);
320 rc = ci->kprci_get_route(index, &gateway_nalid, &gateway_nid,
321 &lo_nid, &hi_nid, &alive);
324 CDEBUG(D_IOCTL, "got route [%d] %d "LPX64":"LPX64" - "LPX64", %s\n",
325 index, gateway_nalid, gateway_nid, lo_nid, hi_nid,
326 alive ? "up" : "down");
328 *gateway_nalidp = (__u32)gateway_nalid;
329 *gateway_nidp = gateway_nid;
335 PORTAL_SYMBOL_PUT (kpr_control_interface);
340 kportal_router_cmd(struct portals_cfg *pcfg, void * private)
345 switch(pcfg->pcfg_command) {
347 CDEBUG(D_IOCTL, "Inappropriate cmd: %d\n", pcfg->pcfg_command);
350 case NAL_CMD_ADD_ROUTE:
351 CDEBUG(D_IOCTL, "Adding route: [%d] "LPU64" : "LPU64" - "LPU64"\n",
352 pcfg->pcfg_nal, pcfg->pcfg_nid,
353 pcfg->pcfg_nid2, pcfg->pcfg_nid3);
354 err = kportal_add_route(pcfg->pcfg_gw_nal, pcfg->pcfg_nid,
355 pcfg->pcfg_nid2, pcfg->pcfg_nid3);
358 case NAL_CMD_DEL_ROUTE:
359 CDEBUG (D_IOCTL, "Removing routes via [%d] "LPU64" : "LPU64" - "LPU64"\n",
360 pcfg->pcfg_gw_nal, pcfg->pcfg_nid,
361 pcfg->pcfg_nid2, pcfg->pcfg_nid3);
362 err = kportal_del_route (pcfg->pcfg_gw_nal, pcfg->pcfg_nid,
363 pcfg->pcfg_nid2, pcfg->pcfg_nid3);
366 case NAL_CMD_NOTIFY_ROUTER: {
367 CDEBUG (D_IOCTL, "Notifying peer [%d] "LPU64" %s @ %ld\n",
368 pcfg->pcfg_gw_nal, pcfg->pcfg_nid,
369 pcfg->pcfg_flags ? "Enabling" : "Disabling",
370 (time_t)pcfg->pcfg_nid3);
372 err = kportal_notify_router (pcfg->pcfg_gw_nal, pcfg->pcfg_nid,
374 (time_t)pcfg->pcfg_nid3);
378 case NAL_CMD_GET_ROUTE:
379 CDEBUG (D_IOCTL, "Getting route [%d]\n", pcfg->pcfg_count);
380 err = kportal_get_route(pcfg->pcfg_count, &pcfg->pcfg_gw_nal,
382 &pcfg->pcfg_nid2, &pcfg->pcfg_nid3,
390 kportal_nal_cmd(struct portals_cfg *pcfg)
392 __u32 nal = pcfg->pcfg_nal;
398 if (nal > 0 && nal <= NAL_MAX_NR && nal_cmd[nal].nch_handler) {
399 CDEBUG(D_IOCTL, "calling handler nal: %d, cmd: %d\n", nal,
401 rc = nal_cmd[nal].nch_handler(pcfg, nal_cmd[nal].nch_private);
408 kportal_get_ni (int nal)
414 return (PORTAL_SYMBOL_GET(kqswnal_ni));
416 return (PORTAL_SYMBOL_GET(ksocknal_ni));
418 return (PORTAL_SYMBOL_GET(ktoenal_ni));
420 return (PORTAL_SYMBOL_GET(kgmnal_ni));
422 return (PORTAL_SYMBOL_GET(kibnal_ni));
427 return (PORTAL_SYMBOL_GET(kscimacnal_ni));
429 /* A warning to a naive caller */
430 CERROR ("unknown nal: %d\n", nal);
436 kportal_put_ni (int nal)
442 PORTAL_SYMBOL_PUT(kqswnal_ni);
445 PORTAL_SYMBOL_PUT(ksocknal_ni);
448 PORTAL_SYMBOL_PUT(ktoenal_ni);
451 PORTAL_SYMBOL_PUT(kgmnal_ni);
454 PORTAL_SYMBOL_PUT(kibnal_ni);
457 /* A lesson to a malicious caller */
460 PORTAL_SYMBOL_PUT(kscimacnal_ni);
463 CERROR ("unknown nal: %d\n", nal);
468 kportal_nal_register(int nal, nal_cmd_handler_t handler, void * private)
472 CDEBUG(D_IOCTL, "Register NAL %d, handler: %p\n", nal, handler);
474 if (nal > 0 && nal <= NAL_MAX_NR) {
476 if (nal_cmd[nal].nch_handler != NULL)
479 nal_cmd[nal].nch_handler = handler;
480 nal_cmd[nal].nch_private = private;
488 kportal_nal_unregister(int nal)
492 CDEBUG(D_IOCTL, "Unregister NAL %d\n", nal);
494 if (nal > 0 && nal <= NAL_MAX_NR) {
496 nal_cmd[nal].nch_handler = NULL;
497 nal_cmd[nal].nch_private = NULL;
504 static int kportal_ioctl(struct inode *inode, struct file *file,
505 unsigned int cmd, unsigned long arg)
509 struct portal_ioctl_data *data;
510 char str[PTL_NALFMT_SIZE];
514 if (current->fsuid != 0)
515 RETURN(err = -EACCES);
517 if ( _IOC_TYPE(cmd) != IOC_PORTAL_TYPE ||
518 _IOC_NR(cmd) < IOC_PORTAL_MIN_NR ||
519 _IOC_NR(cmd) > IOC_PORTAL_MAX_NR ) {
520 CDEBUG(D_IOCTL, "invalid ioctl ( type %d, nr %d, size %d )\n",
521 _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd));
525 if (portal_ioctl_getdata(buf, buf + 800, (void *)arg)) {
526 CERROR("PORTALS ioctl: data error\n");
530 data = (struct portal_ioctl_data *)buf;
533 case IOC_PORTAL_SET_DAEMON:
534 RETURN (portals_debug_set_daemon (
535 (unsigned int) data->ioc_count,
536 (unsigned int) data->ioc_inllen1,
537 (char *) data->ioc_inlbuf1,
538 (unsigned int) data->ioc_misc));
539 case IOC_PORTAL_GET_DEBUG: {
540 __s32 size = portals_debug_copy_to_user(data->ioc_pbuf1,
546 data->ioc_size = size;
547 err = copy_to_user((char *)arg, data, sizeof(*data));
550 case IOC_PORTAL_CLEAR_DEBUG:
551 portals_debug_clear_buffer();
553 case IOC_PORTAL_PANIC:
554 if (!capable (CAP_SYS_BOOT))
556 panic("debugctl-invoked panic");
558 case IOC_PORTAL_MARK_DEBUG:
559 if (data->ioc_inlbuf1 == NULL ||
560 data->ioc_inlbuf1[data->ioc_inllen1 - 1] != '\0')
562 portals_debug_mark_buffer(data->ioc_inlbuf1);
564 case IOC_PORTAL_PING: {
565 void (*ping)(struct portal_ioctl_data *);
567 CDEBUG(D_IOCTL, "doing %d pings to nid "LPX64" (%s)\n",
568 data->ioc_count, data->ioc_nid,
569 portals_nid2str(data->ioc_nal, data->ioc_nid, str));
570 ping = PORTAL_SYMBOL_GET(kping_client);
572 CERROR("PORTAL_SYMBOL_GET failed\n");
575 PORTAL_SYMBOL_PUT(kping_client);
580 case IOC_PORTAL_GET_NID: {
581 const ptl_handle_ni_t *nip;
582 ptl_process_id_t pid;
584 CDEBUG (D_IOCTL, "Getting nid for nal [%d]\n", data->ioc_nal);
586 nip = kportal_get_ni (data->ioc_nal);
590 err = PtlGetId (*nip, &pid);
591 LASSERT (err == PTL_OK);
592 kportal_put_ni (data->ioc_nal);
594 data->ioc_nid = pid.nid;
595 if (copy_to_user ((char *)arg, data, sizeof (*data)))
600 case IOC_PORTAL_NAL_CMD: {
601 struct portals_cfg pcfg;
603 LASSERT (data->ioc_plen1 == sizeof(pcfg));
604 err = copy_from_user(&pcfg, (void *)data->ioc_pbuf1,
611 CDEBUG (D_IOCTL, "nal command nal %d cmd %d\n", pcfg.pcfg_nal,
613 err = kportal_nal_cmd(&pcfg);
615 if (copy_to_user((char *)data->ioc_pbuf1, &pcfg,
618 if (copy_to_user((char *)arg, data, sizeof (*data)))
623 case IOC_PORTAL_FAIL_NID: {
624 const ptl_handle_ni_t *nip;
626 CDEBUG (D_IOCTL, "fail nid: [%d] "LPU64" count %d\n",
627 data->ioc_nal, data->ioc_nid, data->ioc_count);
629 nip = kportal_get_ni (data->ioc_nal);
633 err = PtlFailNid (*nip, data->ioc_nid, data->ioc_count);
634 kportal_put_ni (data->ioc_nal);
638 case IOC_PORTAL_LWT_CONTROL:
639 err = lwt_control (data->ioc_flags, data->ioc_misc);
642 case IOC_PORTAL_LWT_SNAPSHOT:
643 err = lwt_snapshot (&data->ioc_nid,
644 &data->ioc_count, &data->ioc_misc,
645 data->ioc_pbuf1, data->ioc_plen1);
647 copy_to_user((char *)arg, data, sizeof (*data)))
651 case IOC_PORTAL_LWT_LOOKUP_STRING:
652 err = lwt_lookup_string (&data->ioc_count, data->ioc_pbuf1,
653 data->ioc_pbuf2, data->ioc_plen2);
655 copy_to_user((char *)arg, data, sizeof (*data)))
659 case IOC_PORTAL_MEMHOG:
660 if (!capable (CAP_SYS_ADMIN))
662 else if (file->private_data == NULL) {
665 kportal_memhog_free(file->private_data);
666 err = kportal_memhog_alloc(file->private_data,
670 kportal_memhog_free(file->private_data);
683 static struct file_operations portalsdev_fops = {
684 ioctl: kportal_ioctl,
685 open: kportal_psdev_open,
686 release: kportal_psdev_release
690 static struct miscdevice portal_dev = {
696 extern int insert_proc(void);
697 extern void remove_proc(void);
698 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
699 MODULE_DESCRIPTION("Portals v3.1");
700 MODULE_LICENSE("GPL");
702 static int init_kportals_module(void)
706 rc = portals_debug_init(5 * 1024 * 1024);
708 printk(KERN_ERR "LustreError: portals_debug_init: %d\n", rc);
715 CERROR("lwt_init: error %d\n", rc);
719 sema_init(&nal_cmd_sem, 1);
721 rc = misc_register(&portal_dev);
723 CERROR("misc_register: error %d\n", rc);
729 CERROR("PtlInit: error %d\n", rc);
730 goto cleanup_deregister;
735 CERROR("insert_proc: error %d\n", rc);
739 rc = kportal_nal_register(ROUTER, kportal_router_cmd, NULL);
741 CERROR("kportal_nal_registre: ROUTER error %d\n", rc);
745 CDEBUG (D_OTHER, "portals setup OK\n");
753 misc_deregister(&portal_dev);
759 portals_debug_cleanup();
763 static void exit_kportals_module(void)
767 kportal_nal_unregister(ROUTER);
771 CDEBUG(D_MALLOC, "before Portals cleanup: kmem %d\n",
772 atomic_read(&portal_kmemory));
775 rc = misc_deregister(&portal_dev);
777 CERROR("misc_deregister error %d\n", rc);
783 if (atomic_read(&portal_kmemory) != 0)
784 CERROR("Portals memory leaked: %d bytes\n",
785 atomic_read(&portal_kmemory));
787 rc = portals_debug_cleanup();
789 printk(KERN_ERR "LustreError: portals_debug_cleanup: %d\n", rc);
792 EXPORT_SYMBOL(lib_dispatch);
793 EXPORT_SYMBOL(PtlMEAttach);
794 EXPORT_SYMBOL(PtlMEInsert);
795 EXPORT_SYMBOL(PtlMEUnlink);
796 EXPORT_SYMBOL(PtlEQAlloc);
797 EXPORT_SYMBOL(PtlMDAttach);
798 EXPORT_SYMBOL(PtlMDUnlink);
799 EXPORT_SYMBOL(PtlNIInit);
800 EXPORT_SYMBOL(PtlNIFini);
801 EXPORT_SYMBOL(PtlNIDebug);
802 EXPORT_SYMBOL(PtlInit);
803 EXPORT_SYMBOL(PtlFini);
804 EXPORT_SYMBOL(PtlPut);
805 EXPORT_SYMBOL(PtlGet);
806 EXPORT_SYMBOL(ptl_err_str);
807 EXPORT_SYMBOL(portal_subsystem_debug);
808 EXPORT_SYMBOL(portal_debug);
809 EXPORT_SYMBOL(portal_stack);
810 EXPORT_SYMBOL(portal_printk);
811 EXPORT_SYMBOL(portal_cerror);
812 EXPORT_SYMBOL(PtlEQWait);
813 EXPORT_SYMBOL(PtlEQFree);
814 EXPORT_SYMBOL(PtlEQGet);
815 EXPORT_SYMBOL(PtlGetId);
816 EXPORT_SYMBOL(PtlMDBind);
817 EXPORT_SYMBOL(lib_iov_nob);
818 EXPORT_SYMBOL(lib_copy_iov2buf);
819 EXPORT_SYMBOL(lib_copy_buf2iov);
820 EXPORT_SYMBOL(lib_kiov_nob);
821 EXPORT_SYMBOL(lib_copy_kiov2buf);
822 EXPORT_SYMBOL(lib_copy_buf2kiov);
823 EXPORT_SYMBOL(lib_finalize);
824 EXPORT_SYMBOL(lib_parse);
825 EXPORT_SYMBOL(lib_fake_reply_msg);
826 EXPORT_SYMBOL(lib_init);
827 EXPORT_SYMBOL(lib_fini);
828 EXPORT_SYMBOL(portal_kmemory);
829 EXPORT_SYMBOL(kportal_daemonize);
830 EXPORT_SYMBOL(kportal_blockallsigs);
831 EXPORT_SYMBOL(kportal_nal_register);
832 EXPORT_SYMBOL(kportal_nal_unregister);
833 EXPORT_SYMBOL(kportal_assertion_failed);
834 EXPORT_SYMBOL(dispatch_name);
835 EXPORT_SYMBOL(kportal_get_ni);
836 EXPORT_SYMBOL(kportal_put_ni);
837 EXPORT_SYMBOL(kportal_nal_cmd);
839 module_init(init_kportals_module);
840 module_exit (exit_kportals_module);