Whamcloud - gitweb
Use the newer libsysio tags.
[fs/lustre-release.git] / lustre / portals / knals / lonal / lonal.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2004 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
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.
11  *
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.
16  *
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.
20  */
21
22 #include "lonal.h"
23
24 nal_t                   klonal_api;
25 klonal_data_t           klonal_data;
26 ptl_handle_ni_t         klonal_ni;
27
28
29 int
30 klonal_cmd (struct portals_cfg *pcfg, void *private)
31 {
32         LASSERT (pcfg != NULL);
33         
34         switch (pcfg->pcfg_command) {
35         case NAL_CMD_REGISTER_MYNID:
36                 CDEBUG (D_IOCTL, "setting NID to "LPX64" (was "LPX64")\n",
37                         pcfg->pcfg_nid, klonal_lib.libnal_ni.ni_pid.nid);
38                 klonal_lib.libnal_ni.ni_pid.nid = pcfg->pcfg_nid;
39                 return (0);
40                 
41         default:
42                 return (-EINVAL);
43         }
44 }
45
46 static void
47 klonal_shutdown(nal_t *nal)
48 {
49         /* NB The first ref was this module! */
50         if (nal->nal_refct != 0)
51                 return;
52
53         CDEBUG (D_NET, "shutdown\n");
54         LASSERT (nal == &klonal_api);
55
56         switch (klonal_data.klo_init)
57         {
58         default:
59                 LASSERT (0);
60
61         case KLO_INIT_ALL:
62                 libcfs_nal_cmd_unregister(LONAL);
63                 /* fall through */
64
65         case KLO_INIT_LIB:
66                 lib_fini (&klonal_lib);
67                 break;
68
69         case KLO_INIT_NOTHING:
70                 return;
71         }
72
73         memset(&klonal_data, 0, sizeof (klonal_data));
74
75         CDEBUG (D_MALLOC, "done kmem %d\n", atomic_read(&portal_kmemory));
76
77         printk (KERN_INFO "Lustre: LO NAL unloaded (final mem %d)\n",
78                 atomic_read(&portal_kmemory));
79         PORTAL_MODULE_UNUSE;
80 }
81
82 static int
83 klonal_startup (nal_t *nal, ptl_pid_t requested_pid,
84                 ptl_ni_limits_t *requested_limits, 
85                 ptl_ni_limits_t *actual_limits)
86 {
87         int               rc;
88         ptl_process_id_t  my_process_id;
89         int               pkmem = atomic_read(&portal_kmemory);
90
91         LASSERT (nal == &klonal_api);
92
93         if (nal->nal_refct != 0) {
94                 if (actual_limits != NULL)
95                         *actual_limits = klonal_lib.libnal_ni.ni_actual_limits;
96                 return (PTL_OK);
97         }
98
99         LASSERT (klonal_data.klo_init == KLO_INIT_NOTHING);
100
101         CDEBUG (D_MALLOC, "start kmem %d\n", atomic_read(&portal_kmemory));
102
103         /* ensure all pointers NULL etc */
104         memset (&klonal_data, 0, sizeof (klonal_data));
105
106         my_process_id.nid = 0;
107         my_process_id.pid = requested_pid;
108
109         rc = lib_init(&klonal_lib, nal, my_process_id,
110                       requested_limits, actual_limits);
111         if (rc != PTL_OK) {
112                 CERROR ("lib_init failed %d\n", rc);
113                 klonal_shutdown (nal);
114                 return (rc);
115         }
116
117         klonal_data.klo_init = KLO_INIT_LIB;
118
119         rc = libcfs_nal_cmd_register (LONAL, &klonal_cmd, NULL);
120         if (rc != 0) {
121                 CERROR ("Can't initialise command interface (rc = %d)\n", rc);
122                 klonal_shutdown (nal);
123                 return (PTL_FAIL);
124         }
125
126         klonal_data.klo_init = KLO_INIT_ALL;
127
128         printk(KERN_INFO "Lustre: LO NAL (initial mem %d)\n", pkmem);
129         PORTAL_MODULE_USE;
130
131         return (PTL_OK);
132 }
133
134 void __exit
135 klonal_finalise (void)
136 {
137         PtlNIFini(klonal_ni);
138
139         ptl_unregister_nal(LONAL);
140 }
141
142 static int __init
143 klonal_initialise (void)
144 {
145         int   rc;
146
147         klonal_api.nal_ni_init = klonal_startup;
148         klonal_api.nal_ni_fini = klonal_shutdown;
149
150         rc = ptl_register_nal(LONAL, &klonal_api);
151         if (rc != PTL_OK) {
152                 CERROR("Can't register LONAL: %d\n", rc);
153                 return (-ENOMEM);               /* or something... */
154         }
155
156         return (0);
157 }
158
159 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
160 MODULE_DESCRIPTION("Loopback NAL v0.01");
161 MODULE_LICENSE("GPL");
162
163 module_init (klonal_initialise);
164 module_exit (klonal_finalise);