Whamcloud - gitweb
LU-15083 ldiskfs: disable xattr credits check
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  */
31
32 #define DEBUG_SUBSYSTEM S_LNET
33
34 #include <lnet/lib-lnet.h>
35 #include <uapi/linux/lnet/lnet-dlc.h>
36
37 static int config_on_load = 0;
38 module_param(config_on_load, int, 0444);
39 MODULE_PARM_DESC(config_on_load, "configure network at module load");
40
41 static DEFINE_MUTEX(lnet_config_mutex);
42
43 static int
44 lnet_configure(void *arg)
45 {
46         /* 'arg' only there so I can be passed to cfs_create_thread() */
47         int    rc = 0;
48
49         mutex_lock(&lnet_config_mutex);
50
51         if (!the_lnet.ln_niinit_self) {
52                 rc = try_module_get(THIS_MODULE);
53
54                 if (rc != 1)
55                         goto out;
56
57                 rc = LNetNIInit(LNET_PID_LUSTRE);
58                 if (rc >= 0) {
59                         the_lnet.ln_niinit_self = 1;
60                         rc = 0;
61                 } else {
62                         module_put(THIS_MODULE);
63                 }
64         }
65
66 out:
67         mutex_unlock(&lnet_config_mutex);
68         return rc;
69 }
70
71 static int
72 lnet_unconfigure (void)
73 {
74         int   refcount;
75
76         mutex_lock(&lnet_config_mutex);
77
78         if (the_lnet.ln_niinit_self) {
79                 the_lnet.ln_niinit_self = 0;
80                 LNetNIFini();
81                 module_put(THIS_MODULE);
82         }
83
84         mutex_lock(&the_lnet.ln_api_mutex);
85         refcount = the_lnet.ln_refcount;
86         mutex_unlock(&the_lnet.ln_api_mutex);
87
88         mutex_unlock(&lnet_config_mutex);
89
90         return (refcount == 0) ? 0 : -EBUSY;
91 }
92
93 static int
94 lnet_dyn_configure_net(struct libcfs_ioctl_hdr *hdr)
95 {
96         struct lnet_ioctl_config_data *conf =
97           (struct lnet_ioctl_config_data *)hdr;
98         int                           rc;
99
100         if (conf->cfg_hdr.ioc_len < sizeof(*conf))
101                 return -EINVAL;
102
103         mutex_lock(&lnet_config_mutex);
104         if (the_lnet.ln_niinit_self)
105                 rc = lnet_dyn_add_net(conf);
106         else
107                 rc = -EINVAL;
108         mutex_unlock(&lnet_config_mutex);
109
110         return rc;
111 }
112
113 static int
114 lnet_dyn_unconfigure_net(struct libcfs_ioctl_hdr *hdr)
115 {
116         struct lnet_ioctl_config_data *conf =
117           (struct lnet_ioctl_config_data *) hdr;
118         int                           rc;
119
120         if (conf->cfg_hdr.ioc_len < sizeof(*conf))
121                 return -EINVAL;
122
123         mutex_lock(&lnet_config_mutex);
124         if (the_lnet.ln_niinit_self)
125                 rc = lnet_dyn_del_net(conf->cfg_net);
126         else
127                 rc = -EINVAL;
128         mutex_unlock(&lnet_config_mutex);
129
130         return rc;
131 }
132
133 static int
134 lnet_dyn_configure_ni(struct libcfs_ioctl_hdr *hdr)
135 {
136         struct lnet_ioctl_config_ni *conf =
137           (struct lnet_ioctl_config_ni *)hdr;
138         int                           rc;
139
140         if (conf->lic_cfg_hdr.ioc_len < sizeof(*conf))
141                 return -EINVAL;
142
143         mutex_lock(&lnet_config_mutex);
144         if (the_lnet.ln_niinit_self)
145                 rc = lnet_dyn_add_ni(conf);
146         else
147                 rc = -EINVAL;
148         mutex_unlock(&lnet_config_mutex);
149
150         return rc;
151 }
152
153 static int
154 lnet_dyn_unconfigure_ni(struct libcfs_ioctl_hdr *hdr)
155 {
156         struct lnet_ioctl_config_ni *conf =
157           (struct lnet_ioctl_config_ni *) hdr;
158         int                           rc;
159
160         if (conf->lic_cfg_hdr.ioc_len < sizeof(*conf))
161                 return -EINVAL;
162
163         mutex_lock(&lnet_config_mutex);
164         if (the_lnet.ln_niinit_self)
165                 rc = lnet_dyn_del_ni(conf);
166         else
167                 rc = -EINVAL;
168         mutex_unlock(&lnet_config_mutex);
169
170         return rc;
171 }
172
173 static int
174 lnet_ioctl(struct notifier_block *nb,
175            unsigned long cmd, void *vdata)
176 {
177         struct libcfs_ioctl_hdr *hdr = vdata;
178         int rc;
179
180         switch (cmd) {
181         case IOC_LIBCFS_CONFIGURE: {
182                 struct libcfs_ioctl_data *data =
183                   (struct libcfs_ioctl_data *)hdr;
184
185                 if (data->ioc_hdr.ioc_len < sizeof(*data)) {
186                         rc = -EINVAL;
187                 } else {
188                         the_lnet.ln_nis_from_mod_params = data->ioc_flags;
189                         rc = lnet_configure(NULL);
190                 }
191                 break;
192         }
193
194         case IOC_LIBCFS_UNCONFIGURE:
195                 rc = lnet_unconfigure();
196                 break;
197
198         case IOC_LIBCFS_ADD_NET:
199                 rc = lnet_dyn_configure_net(hdr);
200                 break;
201
202         case IOC_LIBCFS_DEL_NET:
203                 rc = lnet_dyn_unconfigure_net(hdr);
204                 break;
205
206         case IOC_LIBCFS_ADD_LOCAL_NI:
207                 rc = lnet_dyn_configure_ni(hdr);
208                 break;
209
210         case IOC_LIBCFS_DEL_LOCAL_NI:
211                 rc = lnet_dyn_unconfigure_ni(hdr);
212                 break;
213
214         default:
215                 /* Passing LNET_PID_ANY only gives me a ref if the net is up
216                  * already; I'll need it to ensure the net can't go down while
217                  * I'm called into it */
218                 rc = LNetNIInit(LNET_PID_ANY);
219                 if (rc >= 0) {
220                         rc = LNetCtl(cmd, hdr);
221                         LNetNIFini();
222                 }
223                 break;
224         }
225         return notifier_from_ioctl_errno(rc);
226 }
227
228 static struct notifier_block lnet_ioctl_handler = {
229         .notifier_call = lnet_ioctl,
230 };
231
232 static int __init lnet_init(void)
233 {
234         int rc;
235         ENTRY;
236
237         rc = lnet_lib_init();
238         if (rc != 0) {
239                 CERROR("lnet_lib_init: error %d\n", rc);
240                 RETURN(rc);
241         }
242
243         if (live_router_check_interval != INT_MIN ||
244             dead_router_check_interval != INT_MIN)
245                 LCONSOLE_WARN("live_router_check_interval and dead_router_check_interval have been deprecated. Use alive_router_check_interval instead. Ignoring these deprecated parameters.\n");
246
247         rc = blocking_notifier_chain_register(&libcfs_ioctl_list,
248                                               &lnet_ioctl_handler);
249         LASSERT(rc == 0);
250
251         if (config_on_load) {
252                 /* Have to schedule a separate thread to avoid deadlocking
253                  * in modload */
254                 (void)kthread_run(lnet_configure, NULL, "lnet_initd");
255         }
256
257         RETURN(0);
258 }
259
260 static void __exit lnet_exit(void)
261 {
262         int rc;
263
264         rc = blocking_notifier_chain_unregister(&libcfs_ioctl_list,
265                                                 &lnet_ioctl_handler);
266         LASSERT(rc == 0);
267
268         lnet_lib_exit();
269 }
270
271 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
272 MODULE_DESCRIPTION("Lustre Networking layer");
273 MODULE_VERSION(LNET_VERSION);
274 MODULE_LICENSE("GPL");
275
276 module_init(lnet_init);
277 module_exit(lnet_exit);