Whamcloud - gitweb
a8eaac526110baafe78c32eba46490b270409c39
[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, 2016, 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 #include <lnet/lib-lnet.h>
35 #include <lnet/lib-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 struct 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(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         mutex_lock(&lnet_config_mutex);
101         if (the_lnet.ln_niinit_self)
102                 rc = lnet_dyn_add_ni(LNET_PID_LUSTRE, conf);
103         else
104                 rc = -EINVAL;
105         mutex_unlock(&lnet_config_mutex);
106         return rc;
107 }
108
109 static int
110 lnet_dyn_unconfigure(struct libcfs_ioctl_hdr *hdr)
111 {
112         struct lnet_ioctl_config_data *conf =
113           (struct lnet_ioctl_config_data *) hdr;
114         int                           rc;
115
116         mutex_lock(&lnet_config_mutex);
117         if (the_lnet.ln_niinit_self)
118                 rc = lnet_dyn_del_ni(conf->cfg_net);
119         else
120                 rc = -EINVAL;
121         mutex_unlock(&lnet_config_mutex);
122
123         return rc;
124 }
125
126 static int
127 lnet_ioctl(unsigned int cmd, struct libcfs_ioctl_hdr *hdr)
128 {
129         int   rc;
130
131         switch (cmd) {
132         case IOC_LIBCFS_CONFIGURE: {
133                 struct libcfs_ioctl_data *data =
134                   (struct libcfs_ioctl_data *)hdr;
135                 the_lnet.ln_nis_from_mod_params = data->ioc_flags;
136                 return lnet_configure(NULL);
137         }
138
139         case IOC_LIBCFS_UNCONFIGURE:
140                 return lnet_unconfigure();
141
142         case IOC_LIBCFS_ADD_NET:
143                 return lnet_dyn_configure(hdr);
144
145         case IOC_LIBCFS_DEL_NET:
146                 return lnet_dyn_unconfigure(hdr);
147
148         default:
149                 /* Passing LNET_PID_ANY only gives me a ref if the net is up
150                  * already; I'll need it to ensure the net can't go down while
151                  * I'm called into it */
152                 rc = LNetNIInit(LNET_PID_ANY);
153                 if (rc >= 0) {
154                         rc = LNetCtl(cmd, hdr);
155                         LNetNIFini();
156                 }
157                 return rc;
158         }
159 }
160
161 DECLARE_IOCTL_HANDLER(lnet_ioctl_handler, lnet_ioctl);
162
163 static int __init lnet_init(void)
164 {
165         int rc;
166         ENTRY;
167
168         mutex_init(&lnet_config_mutex);
169
170         rc = lnet_lib_init();
171         if (rc != 0) {
172                 CERROR("lnet_lib_init: error %d\n", rc);
173                 RETURN(rc);
174         }
175
176         rc = libcfs_register_ioctl(&lnet_ioctl_handler);
177         LASSERT(rc == 0);
178
179         if (config_on_load) {
180                 /* Have to schedule a separate thread to avoid deadlocking
181                  * in modload */
182                 (void)kthread_run(lnet_configure, NULL, "lnet_initd");
183         }
184
185         RETURN(0);
186 }
187
188 static void __exit lnet_exit(void)
189 {
190         int rc;
191
192         rc = libcfs_deregister_ioctl(&lnet_ioctl_handler);
193         LASSERT(rc == 0);
194
195         lnet_lib_exit();
196 }
197
198 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
199 MODULE_DESCRIPTION("Lustre Networking layer");
200 MODULE_VERSION(LNET_VERSION);
201 MODULE_LICENSE("GPL");
202
203 module_init(lnet_init);
204 module_exit(lnet_exit);