GNU Linux-libre 4.14.266-gnu1
[releases.git] / drivers / staging / lustre / 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, 2015, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32
33 #define DEBUG_SUBSYSTEM S_LNET
34
35 #include <linux/lnet/lib-lnet.h>
36 #include <uapi/linux/lnet/lnet-dlc.h>
37
38 static int config_on_load;
39 module_param(config_on_load, int, 0444);
40 MODULE_PARM_DESC(config_on_load, "configure network at module load");
41
42 static struct mutex lnet_config_mutex;
43
44 static int
45 lnet_configure(void *arg)
46 {
47         /* 'arg' only there so I can be passed to cfs_create_thread() */
48         int rc = 0;
49
50         mutex_lock(&lnet_config_mutex);
51
52         if (!the_lnet.ln_niinit_self) {
53                 rc = try_module_get(THIS_MODULE);
54
55                 if (rc != 1)
56                         goto out;
57
58                 rc = LNetNIInit(LNET_PID_LUSTRE);
59                 if (rc >= 0) {
60                         the_lnet.ln_niinit_self = 1;
61                         rc = 0;
62                 } else {
63                         module_put(THIS_MODULE);
64                 }
65         }
66
67 out:
68         mutex_unlock(&lnet_config_mutex);
69         return rc;
70 }
71
72 static int
73 lnet_unconfigure(void)
74 {
75         int refcount;
76
77         mutex_lock(&lnet_config_mutex);
78
79         if (the_lnet.ln_niinit_self) {
80                 the_lnet.ln_niinit_self = 0;
81                 LNetNIFini();
82                 module_put(THIS_MODULE);
83         }
84
85         mutex_lock(&the_lnet.ln_api_mutex);
86         refcount = the_lnet.ln_refcount;
87         mutex_unlock(&the_lnet.ln_api_mutex);
88
89         mutex_unlock(&lnet_config_mutex);
90         return !refcount ? 0 : -EBUSY;
91 }
92
93 static int
94 lnet_dyn_configure(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 = -EINVAL;
106                 goto out_unlock;
107         }
108         rc = lnet_dyn_add_ni(LNET_PID_LUSTRE, conf);
109 out_unlock:
110         mutex_unlock(&lnet_config_mutex);
111
112         return rc;
113 }
114
115 static int
116 lnet_dyn_unconfigure(struct libcfs_ioctl_hdr *hdr)
117 {
118         struct lnet_ioctl_config_data *conf =
119                 (struct lnet_ioctl_config_data *)hdr;
120         int rc;
121
122         if (conf->cfg_hdr.ioc_len < sizeof(*conf))
123                 return -EINVAL;
124
125         mutex_lock(&lnet_config_mutex);
126         if (!the_lnet.ln_niinit_self) {
127                 rc = -EINVAL;
128                 goto out_unlock;
129         }
130         rc = lnet_dyn_del_ni(conf->cfg_net);
131 out_unlock:
132         mutex_unlock(&lnet_config_mutex);
133
134         return rc;
135 }
136
137 static int
138 lnet_ioctl(unsigned int cmd, struct libcfs_ioctl_hdr *hdr)
139 {
140         int rc;
141
142         switch (cmd) {
143         case IOC_LIBCFS_CONFIGURE: {
144                 struct libcfs_ioctl_data *data =
145                         (struct libcfs_ioctl_data *)hdr;
146
147                 if (data->ioc_hdr.ioc_len < sizeof(*data))
148                         return -EINVAL;
149
150                 the_lnet.ln_nis_from_mod_params = data->ioc_flags;
151                 return lnet_configure(NULL);
152         }
153
154         case IOC_LIBCFS_UNCONFIGURE:
155                 return lnet_unconfigure();
156
157         case IOC_LIBCFS_ADD_NET:
158                 return lnet_dyn_configure(hdr);
159
160         case IOC_LIBCFS_DEL_NET:
161                 return lnet_dyn_unconfigure(hdr);
162
163         default:
164                 /*
165                  * Passing LNET_PID_ANY only gives me a ref if the net is up
166                  * already; I'll need it to ensure the net can't go down while
167                  * I'm called into it
168                  */
169                 rc = LNetNIInit(LNET_PID_ANY);
170                 if (rc >= 0) {
171                         rc = LNetCtl(cmd, hdr);
172                         LNetNIFini();
173                 }
174                 return rc;
175         }
176 }
177
178 static DECLARE_IOCTL_HANDLER(lnet_ioctl_handler, lnet_ioctl);
179
180 static int __init lnet_init(void)
181 {
182         int rc;
183
184         mutex_init(&lnet_config_mutex);
185
186         rc = lnet_lib_init();
187         if (rc) {
188                 CERROR("lnet_lib_init: error %d\n", rc);
189                 return rc;
190         }
191
192         rc = libcfs_register_ioctl(&lnet_ioctl_handler);
193         LASSERT(!rc);
194
195         if (config_on_load) {
196                 /*
197                  * Have to schedule a separate thread to avoid deadlocking
198                  * in modload
199                  */
200                 (void)kthread_run(lnet_configure, NULL, "lnet_initd");
201         }
202
203         return 0;
204 }
205
206 static void __exit lnet_exit(void)
207 {
208         int rc;
209
210         rc = libcfs_deregister_ioctl(&lnet_ioctl_handler);
211         LASSERT(!rc);
212
213         lnet_lib_exit();
214 }
215
216 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
217 MODULE_DESCRIPTION("Lustre Networking layer");
218 MODULE_VERSION(LNET_VERSION);
219 MODULE_LICENSE("GPL");
220
221 module_init(lnet_init);
222 module_exit(lnet_exit);