Whamcloud - gitweb
LU-6875 update: set st to NULL in error handler
[fs/lustre-release.git] / lnet / lnet / module.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, 2014, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #define DEBUG_SUBSYSTEM S_LNET
38 #include <lnet/lib-lnet.h>
39 #include <lnet/lib-dlc.h>
40
41 static int config_on_load = 0;
42 CFS_MODULE_PARM(config_on_load, "i", int, 0444,
43                 "configure network at module load");
44
45 static struct mutex lnet_config_mutex;
46
47 static int
48 lnet_configure(void *arg)
49 {
50         /* 'arg' only there so I can be passed to cfs_create_thread() */
51         int    rc = 0;
52
53         LNET_MUTEX_LOCK(&lnet_config_mutex);
54
55         if (!the_lnet.ln_niinit_self) {
56                 rc = try_module_get(THIS_MODULE);
57
58                 if (rc != 1)
59                         goto out;
60
61                 rc = LNetNIInit(LNET_PID_LUSTRE);
62                 if (rc >= 0) {
63                         the_lnet.ln_niinit_self = 1;
64                         rc = 0;
65                 } else {
66                         module_put(THIS_MODULE);
67                 }
68         }
69
70 out:
71         LNET_MUTEX_UNLOCK(&lnet_config_mutex);
72         return rc;
73 }
74
75 static int
76 lnet_unconfigure (void)
77 {
78         int   refcount;
79
80         LNET_MUTEX_LOCK(&lnet_config_mutex);
81
82         if (the_lnet.ln_niinit_self) {
83                 the_lnet.ln_niinit_self = 0;
84                 LNetNIFini();
85                 module_put(THIS_MODULE);
86         }
87
88         LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
89         refcount = the_lnet.ln_refcount;
90         LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
91
92         LNET_MUTEX_UNLOCK(&lnet_config_mutex);
93
94         return (refcount == 0) ? 0 : -EBUSY;
95 }
96
97 static int
98 lnet_dyn_configure(struct libcfs_ioctl_hdr *hdr)
99 {
100         struct lnet_ioctl_config_data *conf =
101           (struct lnet_ioctl_config_data *)hdr;
102         int                           rc;
103
104         LNET_MUTEX_LOCK(&lnet_config_mutex);
105         if (the_lnet.ln_niinit_self)
106                 rc = lnet_dyn_add_ni(LNET_PID_LUSTRE,
107                                      conf->cfg_config_u.cfg_net.net_intf,
108                                      conf->cfg_config_u.cfg_net.
109                                         net_peer_timeout,
110                                      conf->cfg_config_u.cfg_net.
111                                         net_peer_tx_credits,
112                                      conf->cfg_config_u.cfg_net.
113                                         net_peer_rtr_credits,
114                                      conf->cfg_config_u.cfg_net.
115                                         net_max_tx_credits);
116         else
117                 rc = -EINVAL;
118         LNET_MUTEX_UNLOCK(&lnet_config_mutex);
119         return rc;
120 }
121
122 static int
123 lnet_dyn_unconfigure(struct libcfs_ioctl_hdr *hdr)
124 {
125         struct lnet_ioctl_config_data *conf =
126           (struct lnet_ioctl_config_data *) hdr;
127         int                           rc;
128
129         LNET_MUTEX_LOCK(&lnet_config_mutex);
130         if (the_lnet.ln_niinit_self)
131                 rc = lnet_dyn_del_ni(conf->cfg_net);
132         else
133                 rc = -EINVAL;
134         LNET_MUTEX_UNLOCK(&lnet_config_mutex);
135
136         return rc;
137 }
138
139 static int
140 lnet_ioctl(unsigned int cmd, struct libcfs_ioctl_hdr *hdr)
141 {
142         int   rc;
143
144         switch (cmd) {
145         case IOC_LIBCFS_CONFIGURE: {
146                 struct libcfs_ioctl_data *data =
147                   (struct libcfs_ioctl_data *)hdr;
148                 the_lnet.ln_nis_from_mod_params = data->ioc_flags;
149                 return lnet_configure(NULL);
150         }
151
152         case IOC_LIBCFS_UNCONFIGURE:
153                 return lnet_unconfigure();
154
155         case IOC_LIBCFS_ADD_NET:
156                 return lnet_dyn_configure(hdr);
157
158         case IOC_LIBCFS_DEL_NET:
159                 return lnet_dyn_unconfigure(hdr);
160
161         default:
162                 /* Passing LNET_PID_ANY only gives me a ref if the net is up
163                  * already; I'll need it to ensure the net can't go down while
164                  * I'm called into it */
165                 rc = LNetNIInit(LNET_PID_ANY);
166                 if (rc >= 0) {
167                         rc = LNetCtl(cmd, hdr);
168                         LNetNIFini();
169                 }
170                 return rc;
171         }
172 }
173
174 DECLARE_IOCTL_HANDLER(lnet_ioctl_handler, lnet_ioctl);
175
176 static int
177 lnet_module_init(void)
178 {
179         int rc;
180         ENTRY;
181
182         mutex_init(&lnet_config_mutex);
183
184         rc = lnet_init();
185         if (rc != 0) {
186                 CERROR("lnet_init: error %d\n", rc);
187                 RETURN(rc);
188         }
189
190         rc = libcfs_register_ioctl(&lnet_ioctl_handler);
191         LASSERT(rc == 0);
192
193         if (config_on_load) {
194                 /* Have to schedule a separate thread to avoid deadlocking
195                  * in modload */
196                 (void) kthread_run(lnet_configure, NULL, "lnet_initd");
197         }
198
199         RETURN(0);
200 }
201
202 static void
203 lnet_module_exit(void)
204 {
205         int rc;
206
207         rc = libcfs_deregister_ioctl(&lnet_ioctl_handler);
208         LASSERT(rc == 0);
209
210         lnet_fini();
211 }
212
213 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
214 MODULE_DESCRIPTION("LNet v3.1");
215 MODULE_VERSION("1.0.0");
216 MODULE_LICENSE("GPL");
217
218 module_init(lnet_module_init);
219 module_exit(lnet_module_exit);