Whamcloud - gitweb
b58263f932c849052706771dd1867e25e27bf8ad
[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 <linux/miscdevice.h>
35 #include <lnet/lib-lnet.h>
36 #include <uapi/linux/lnet/lnet-dlc.h>
37
38 static int config_on_load = 0;
39 module_param(config_on_load, int, 0444);
40 MODULE_PARM_DESC(config_on_load, "configure network at module load");
41
42 static DEFINE_MUTEX(lnet_config_mutex);
43
44 int 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 int lnet_unconfigure(void)
72 {
73         int refcount;
74
75         mutex_lock(&lnet_config_mutex);
76
77         if (the_lnet.ln_niinit_self) {
78                 the_lnet.ln_niinit_self = 0;
79                 LNetNIFini();
80                 module_put(THIS_MODULE);
81         }
82
83         mutex_lock(&the_lnet.ln_api_mutex);
84         refcount = the_lnet.ln_refcount;
85         mutex_unlock(&the_lnet.ln_api_mutex);
86
87         mutex_unlock(&lnet_config_mutex);
88
89         return (refcount == 0) ? 0 : -EBUSY;
90 }
91
92 static int
93 lnet_dyn_configure_net(struct libcfs_ioctl_hdr *hdr)
94 {
95         struct lnet_ioctl_config_data *conf =
96           (struct lnet_ioctl_config_data *)hdr;
97         int                           rc;
98
99         if (conf->cfg_hdr.ioc_len < sizeof(*conf))
100                 return -EINVAL;
101
102         mutex_lock(&lnet_config_mutex);
103         if (the_lnet.ln_niinit_self)
104                 rc = lnet_dyn_add_net(conf);
105         else
106                 rc = -EINVAL;
107         mutex_unlock(&lnet_config_mutex);
108
109         return rc;
110 }
111
112 static int
113 lnet_dyn_unconfigure_net(struct libcfs_ioctl_hdr *hdr)
114 {
115         struct lnet_ioctl_config_data *conf =
116           (struct lnet_ioctl_config_data *) hdr;
117         int                           rc;
118
119         if (conf->cfg_hdr.ioc_len < sizeof(*conf))
120                 return -EINVAL;
121
122         mutex_lock(&lnet_config_mutex);
123         if (the_lnet.ln_niinit_self)
124                 rc = lnet_dyn_del_net(conf->cfg_net);
125         else
126                 rc = -EINVAL;
127         mutex_unlock(&lnet_config_mutex);
128
129         return rc;
130 }
131
132 static int
133 lnet_dyn_configure_ni(struct libcfs_ioctl_hdr *hdr)
134 {
135         struct lnet_ioctl_config_ni *conf =
136           (struct lnet_ioctl_config_ni *)hdr;
137         int rc = -EINVAL;
138
139         if (conf->lic_cfg_hdr.ioc_len < sizeof(*conf))
140                 return rc;
141
142         mutex_lock(&lnet_config_mutex);
143         if (the_lnet.ln_niinit_self) {
144                 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
145                 struct lnet_nid nid;
146                 u32 net_id;
147
148                 /* get the tunables if they are available */
149                 if (conf->lic_cfg_hdr.ioc_len >=
150                     sizeof(*conf) + sizeof(*tun))
151                         tun = (struct lnet_ioctl_config_lnd_tunables *) conf->lic_bulk;
152
153                 lnet_nid4_to_nid(conf->lic_nid, &nid);
154                 net_id = LNET_NID_NET(&nid);
155                 rc = lnet_dyn_add_ni(conf, net_id, &LNET_ANY_NID, tun);
156         }
157         mutex_unlock(&lnet_config_mutex);
158
159         return rc;
160 }
161
162 static int
163 lnet_dyn_unconfigure_ni(struct libcfs_ioctl_hdr *hdr)
164 {
165         struct lnet_ioctl_config_ni *conf =
166           (struct lnet_ioctl_config_ni *) hdr;
167         struct lnet_nid nid;
168         int rc = -EINVAL;
169
170         if (conf->lic_cfg_hdr.ioc_len < sizeof(*conf) ||
171             !the_lnet.ln_niinit_self)
172                 return rc;
173
174         lnet_nid4_to_nid(conf->lic_nid, &nid);
175         mutex_lock(&lnet_config_mutex);
176         if (the_lnet.ln_niinit_self)
177                 rc = lnet_dyn_del_ni(&nid);
178         else
179                 rc = -EINVAL;
180         mutex_unlock(&lnet_config_mutex);
181
182         return rc;
183 }
184
185 static int
186 lnet_ioctl(unsigned int cmd, struct libcfs_ioctl_hdr *hdr)
187 {
188         int rc;
189
190         switch (cmd) {
191         case IOC_LIBCFS_CONFIGURE: {
192                 struct libcfs_ioctl_data *data =
193                   (struct libcfs_ioctl_data *)hdr;
194
195                 if (data->ioc_hdr.ioc_len < sizeof(*data)) {
196                         rc = -EINVAL;
197                 } else {
198                         the_lnet.ln_nis_from_mod_params = data->ioc_flags;
199                         rc = lnet_configure(NULL);
200                 }
201                 break;
202         }
203
204         case IOC_LIBCFS_UNCONFIGURE:
205                 rc = lnet_unconfigure();
206                 break;
207
208         case IOC_LIBCFS_ADD_NET:
209                 rc = lnet_dyn_configure_net(hdr);
210                 break;
211
212         case IOC_LIBCFS_DEL_NET:
213                 rc = lnet_dyn_unconfigure_net(hdr);
214                 break;
215
216         case IOC_LIBCFS_ADD_LOCAL_NI:
217                 rc = lnet_dyn_configure_ni(hdr);
218                 break;
219
220         case IOC_LIBCFS_DEL_LOCAL_NI:
221                 rc = lnet_dyn_unconfigure_ni(hdr);
222                 break;
223
224         default:
225                 /* Passing LNET_PID_ANY only gives me a ref if the net is up
226                  * already; I'll need it to ensure the net can't go down while
227                  * I'm called into it */
228                 rc = LNetNIInit(LNET_PID_ANY);
229                 if (rc >= 0) {
230                         rc = LNetCtl(cmd, hdr);
231                         LNetNIFini();
232                 }
233                 break;
234         }
235         return rc;
236 }
237 BLOCKING_NOTIFIER_HEAD(lnet_ioctl_list);
238 EXPORT_SYMBOL(lnet_ioctl_list);
239
240 static inline size_t lnet_ioctl_packlen(struct libcfs_ioctl_data *data)
241 {
242         size_t len = sizeof(*data);
243
244         len += (data->ioc_inllen1 + 7) & ~7;
245         len += (data->ioc_inllen2 + 7) & ~7;
246         return len;
247 }
248
249 static bool lnet_ioctl_is_invalid(struct libcfs_ioctl_data *data)
250 {
251         const int maxlen = 1 << 30;
252
253         if (data->ioc_hdr.ioc_len > maxlen)
254                 return true;
255
256         if (data->ioc_inllen1 > maxlen)
257                 return true;
258
259         if (data->ioc_inllen2 > maxlen)
260                 return true;
261
262         if (data->ioc_inlbuf1 && !data->ioc_inllen1)
263                 return true;
264
265         if (data->ioc_inlbuf2 && !data->ioc_inllen2)
266                 return true;
267
268         if (data->ioc_pbuf1 && !data->ioc_plen1)
269                 return true;
270
271         if (data->ioc_pbuf2 && !data->ioc_plen2)
272                 return true;
273
274         if (data->ioc_plen1 && !data->ioc_pbuf1)
275                 return true;
276
277         if (data->ioc_plen2 && !data->ioc_pbuf2)
278                 return true;
279
280         if (lnet_ioctl_packlen(data) != data->ioc_hdr.ioc_len)
281                 return true;
282
283         if (data->ioc_inllen1 &&
284                 data->ioc_bulk[((data->ioc_inllen1 + 7) & ~7) +
285                                data->ioc_inllen2 - 1] != '\0')
286                 return true;
287
288         return false;
289 }
290
291 static int lnet_ioctl_data_adjust(struct libcfs_ioctl_data *data)
292 {
293         ENTRY;
294
295         if (lnet_ioctl_is_invalid(data)) {
296                 CERROR("lnet ioctl: parameter not correctly formatted\n");
297                 RETURN(-EINVAL);
298         }
299
300         if (data->ioc_inllen1 != 0)
301                 data->ioc_inlbuf1 = &data->ioc_bulk[0];
302
303         if (data->ioc_inllen2 != 0)
304                 data->ioc_inlbuf2 = (&data->ioc_bulk[0] +
305                                      round_up(data->ioc_inllen1, 8));
306
307         RETURN(0);
308 }
309
310 static int lnet_ioctl_getdata(struct libcfs_ioctl_hdr **hdr_pp,
311                               struct libcfs_ioctl_hdr __user *uhdr)
312 {
313         struct libcfs_ioctl_hdr hdr;
314         int err;
315
316         ENTRY;
317         if (copy_from_user(&hdr, uhdr, sizeof(hdr)))
318                 RETURN(-EFAULT);
319
320         if (hdr.ioc_version != LNET_IOCTL_VERSION &&
321             hdr.ioc_version != LNET_IOCTL_VERSION2) {
322                 CERROR("lnet ioctl: version mismatch expected %#x, got %#x\n",
323                        LNET_IOCTL_VERSION, hdr.ioc_version);
324                 RETURN(-EINVAL);
325         }
326
327         if (hdr.ioc_len < sizeof(struct libcfs_ioctl_hdr)) {
328                 CERROR("lnet ioctl: user buffer too small for ioctl\n");
329                 RETURN(-EINVAL);
330         }
331
332         if (hdr.ioc_len > LIBCFS_IOC_DATA_MAX) {
333                 CERROR("lnet ioctl: user buffer is too large %d/%d\n",
334                        hdr.ioc_len, LIBCFS_IOC_DATA_MAX);
335                 RETURN(-EINVAL);
336         }
337
338         LIBCFS_ALLOC(*hdr_pp, hdr.ioc_len);
339         if (*hdr_pp == NULL)
340                 RETURN(-ENOMEM);
341
342         if (copy_from_user(*hdr_pp, uhdr, hdr.ioc_len))
343                 GOTO(free, err = -EFAULT);
344
345         if ((*hdr_pp)->ioc_version != hdr.ioc_version ||
346                 (*hdr_pp)->ioc_len != hdr.ioc_len) {
347                 GOTO(free, err = -EINVAL);
348         }
349
350         RETURN(0);
351
352 free:
353         LIBCFS_FREE(*hdr_pp, hdr.ioc_len);
354         RETURN(err);
355 }
356
357 static long
358 lnet_psdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
359 {
360         struct libcfs_ioctl_data *data = NULL;
361         struct libcfs_ioctl_hdr  *hdr;
362         int                       err;
363         void __user              *uparam = (void __user *)arg;
364
365         ENTRY;
366         if (!capable(CAP_SYS_ADMIN))
367                 return -EACCES;
368
369         if (_IOC_TYPE(cmd) != IOC_LIBCFS_TYPE ||
370             _IOC_NR(cmd) < IOC_LIBCFS_MIN_NR  ||
371             _IOC_NR(cmd) > IOC_LIBCFS_MAX_NR) {
372                 CDEBUG(D_IOCTL, "invalid ioctl ( type %d, nr %d, size %d )\n",
373                        _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd));
374                 return -EINVAL;
375         }
376
377         /* 'cmd' and permissions get checked in our arch-specific caller */
378         err = lnet_ioctl_getdata(&hdr, uparam);
379         if (err != 0) {
380                 CDEBUG_LIMIT(D_ERROR,
381                              "lnet ioctl: data header error %d\n", err);
382                 RETURN(err);
383         }
384
385         if (hdr->ioc_version == LNET_IOCTL_VERSION) {
386                 /* The lnet_ioctl_data_adjust() function performs adjustment
387                  * operations on the libcfs_ioctl_data structure to make
388                  * it usable by the code.  This doesn't need to be called
389                  * for new data structures added.
390                  */
391                 data = container_of(hdr, struct libcfs_ioctl_data, ioc_hdr);
392                 err = lnet_ioctl_data_adjust(data);
393                 if (err != 0)
394                         GOTO(out, err);
395         }
396
397         CDEBUG(D_IOCTL, "lnet ioctl cmd %u\n", cmd);
398
399         err = libcfs_ioctl(cmd, data);
400         if (err == -EINVAL)
401                 err = lnet_ioctl(cmd, hdr);
402         if (err == -EINVAL) {
403                 err = blocking_notifier_call_chain(&lnet_ioctl_list,
404                                                    cmd, hdr);
405                 if (!(err & NOTIFY_STOP_MASK))
406                         /* No-one claimed the ioctl */
407                         err = -EINVAL;
408                 else
409                         err = notifier_to_errno(err);
410         }
411         if (copy_to_user(uparam, hdr, hdr->ioc_len) && !err)
412                 err = -EFAULT;
413 out:
414         LIBCFS_FREE(hdr, hdr->ioc_len);
415         RETURN(err);
416 }
417
418 static const struct file_operations lnet_fops = {
419         .owner                  = THIS_MODULE,
420         .unlocked_ioctl         = lnet_psdev_ioctl,
421 };
422
423 static struct miscdevice lnet_dev = {
424         .minor                  = MISC_DYNAMIC_MINOR,
425         .name                   = "lnet",
426         .fops                   = &lnet_fops,
427 };
428
429 static int __init lnet_init(void)
430 {
431         int rc;
432
433         ENTRY;
434         rc = libcfs_setup();
435         if (rc)
436                 return rc;
437
438         rc = cfs_cpu_init();
439         if (rc < 0) {
440                 CERROR("cfs_cpu_init: rc = %d\n", rc);
441                 RETURN(rc);
442         }
443
444         rc = lnet_lib_init();
445         if (rc != 0) {
446                 CERROR("lnet_lib_init: rc = %d\n", rc);
447                 cfs_cpu_fini();
448                 RETURN(rc);
449         }
450
451         rc = misc_register(&lnet_dev);
452         if (rc) {
453                 CERROR("misc_register: rc = %d\n", rc);
454                 cfs_cpu_fini();
455                 RETURN(rc);
456         }
457
458         if (live_router_check_interval != INT_MIN ||
459             dead_router_check_interval != INT_MIN)
460                 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");
461
462         if (config_on_load) {
463                 /* Have to schedule a separate thread to avoid deadlocking
464                  * in modload */
465                 (void)kthread_run(lnet_configure, NULL, "lnet_initd");
466         }
467
468         RETURN(0);
469 }
470
471 static void __exit lnet_exit(void)
472 {
473         misc_deregister(&lnet_dev);
474
475         lnet_router_exit();
476         lnet_lib_exit();
477         cfs_cpu_fini();
478 }
479
480 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
481 MODULE_DESCRIPTION("Lustre Networking layer");
482 MODULE_VERSION(LNET_VERSION);
483 MODULE_LICENSE("GPL");
484
485 module_init(lnet_init);
486 module_exit(lnet_exit);