Whamcloud - gitweb
LU-2456 lnet: Dynamic LNet Configuration (DLC) IOCTL changes
[fs/lustre-release.git] / libcfs / libcfs / 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) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, 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 /* TODO - This will be completed in the subsequent patches.
39  * For this patch the MAX is hardcoded, in the next patch
40  * the value will be set to the largest data structure that
41  * can be sent from user space */
42 #define LIBCFS_MAX_IOCTL_BUF_LEN 2048
43
44 #include <libcfs/libcfs.h>
45 #include <libcfs/libcfs_crypto.h>
46 #include <lnet/lib-lnet.h>
47 #include <lnet/lnet.h>
48 #include "tracefile.h"
49
50 void
51 kportal_memhog_free (struct libcfs_device_userstate *ldu)
52 {
53         struct page **level0p = &ldu->ldu_memhog_root_page;
54         struct page **level1p;
55         struct page **level2p;
56         int           count1;
57         int           count2;
58
59         if (*level0p != NULL) {
60                 level1p = (struct page **)page_address(*level0p);
61                 count1 = 0;
62
63                 while (count1 < PAGE_CACHE_SIZE/sizeof(struct page *) &&
64                        *level1p != NULL) {
65
66                         level2p = (struct page **)page_address(*level1p);
67                         count2 = 0;
68
69                         while (count2 < PAGE_CACHE_SIZE/sizeof(struct page *) &&
70                                *level2p != NULL) {
71
72                                 __free_page(*level2p);
73                                 ldu->ldu_memhog_pages--;
74                                 level2p++;
75                                 count2++;
76                         }
77
78                         __free_page(*level1p);
79                         ldu->ldu_memhog_pages--;
80                         level1p++;
81                         count1++;
82                 }
83
84                 __free_page(*level0p);
85                 ldu->ldu_memhog_pages--;
86
87                 *level0p = NULL;
88         }
89
90         LASSERT(ldu->ldu_memhog_pages == 0);
91 }
92
93 int
94 kportal_memhog_alloc(struct libcfs_device_userstate *ldu, int npages,
95                      gfp_t flags)
96 {
97         struct page **level0p;
98         struct page **level1p;
99         struct page **level2p;
100         int           count1;
101         int           count2;
102
103         LASSERT(ldu->ldu_memhog_pages == 0);
104         LASSERT(ldu->ldu_memhog_root_page == NULL);
105
106         if (npages < 0)
107                 return -EINVAL;
108
109         if (npages == 0)
110                 return 0;
111
112         level0p = &ldu->ldu_memhog_root_page;
113         *level0p = alloc_page(flags);
114         if (*level0p == NULL)
115                 return -ENOMEM;
116         ldu->ldu_memhog_pages++;
117
118         level1p = (struct page **)page_address(*level0p);
119         count1 = 0;
120         memset(level1p, 0, PAGE_CACHE_SIZE);
121
122         while (ldu->ldu_memhog_pages < npages &&
123                count1 < PAGE_CACHE_SIZE/sizeof(struct page *)) {
124
125                 if (cfs_signal_pending())
126                         return -EINTR;
127
128                 *level1p = alloc_page(flags);
129                 if (*level1p == NULL)
130                         return -ENOMEM;
131                 ldu->ldu_memhog_pages++;
132
133                 level2p = (struct page **)page_address(*level1p);
134                 count2 = 0;
135                 memset(level2p, 0, PAGE_CACHE_SIZE);
136
137                 while (ldu->ldu_memhog_pages < npages &&
138                        count2 < PAGE_CACHE_SIZE/sizeof(struct page *)) {
139
140                         if (cfs_signal_pending())
141                                 return -EINTR;
142
143                         *level2p = alloc_page(flags);
144                         if (*level2p == NULL)
145                                 return -ENOMEM;
146                         ldu->ldu_memhog_pages++;
147
148                         level2p++;
149                         count2++;
150                 }
151
152                 level1p++;
153                 count1++;
154         }
155
156         return 0;
157 }
158
159 /* called when opening /dev/device */
160 static int libcfs_psdev_open(unsigned long flags, void *args)
161 {
162         struct libcfs_device_userstate *ldu;
163         ENTRY;
164
165         try_module_get(THIS_MODULE);
166
167         LIBCFS_ALLOC(ldu, sizeof(*ldu));
168         if (ldu != NULL) {
169                 ldu->ldu_memhog_pages = 0;
170                 ldu->ldu_memhog_root_page = NULL;
171         }
172         *(struct libcfs_device_userstate **)args = ldu;
173
174         RETURN(0);
175 }
176
177 /* called when closing /dev/device */
178 static int libcfs_psdev_release(unsigned long flags, void *args)
179 {
180         struct libcfs_device_userstate *ldu;
181         ENTRY;
182
183         ldu = (struct libcfs_device_userstate *)args;
184         if (ldu != NULL) {
185                 kportal_memhog_free(ldu);
186                 LIBCFS_FREE(ldu, sizeof(*ldu));
187         }
188
189         module_put(THIS_MODULE);
190         RETURN(0);
191 }
192
193 static struct rw_semaphore ioctl_list_sem;
194 static struct list_head ioctl_list;
195
196 int libcfs_register_ioctl(struct libcfs_ioctl_handler *hand)
197 {
198         int rc = 0;
199
200         down_write(&ioctl_list_sem);
201         if (!list_empty(&hand->item))
202                 rc = -EBUSY;
203         else
204                 list_add_tail(&hand->item, &ioctl_list);
205         up_write(&ioctl_list_sem);
206
207         return rc;
208 }
209 EXPORT_SYMBOL(libcfs_register_ioctl);
210
211 int libcfs_deregister_ioctl(struct libcfs_ioctl_handler *hand)
212 {
213         int rc = 0;
214
215         down_write(&ioctl_list_sem);
216         if (list_empty(&hand->item))
217                 rc = -ENOENT;
218         else
219                 list_del_init(&hand->item);
220         up_write(&ioctl_list_sem);
221
222         return rc;
223 }
224 EXPORT_SYMBOL(libcfs_deregister_ioctl);
225
226 static int libcfs_ioctl_handle(struct cfs_psdev_file *pfile, unsigned long cmd,
227                                void *arg, struct libcfs_ioctl_hdr *hdr)
228 {
229         struct libcfs_ioctl_data *data = NULL;
230         int err;
231         ENTRY;
232
233         /* TODO: this is going to change in subsequent patches
234          * to exclude messages which use the new data structures */
235         if ((cmd <= IOC_LIBCFS_LNETST) ||
236             (cmd >= IOC_LIBCFS_REGISTER_MYNID)) {
237                 data = container_of(hdr, struct libcfs_ioctl_data, ioc_hdr);
238                 err = libcfs_ioctl_data_adjust(data);
239                 if (err != 0) {
240                         RETURN(err);
241                 }
242         }
243
244         switch (cmd) {
245         case IOC_LIBCFS_CLEAR_DEBUG:
246                 libcfs_debug_clear_buffer();
247                 RETURN(0);
248         /*
249          * case IOC_LIBCFS_PANIC:
250          * Handled in arch/cfs_module.c
251          */
252         case IOC_LIBCFS_MARK_DEBUG:
253                 if (data->ioc_inlbuf1 == NULL ||
254                     data->ioc_inlbuf1[data->ioc_inllen1 - 1] != '\0')
255                         RETURN(-EINVAL);
256                 libcfs_debug_mark_buffer(data->ioc_inlbuf1);
257                 RETURN(0);
258         case IOC_LIBCFS_MEMHOG:
259                 if (pfile->private_data == NULL) {
260                         err = -EINVAL;
261                 } else {
262                         kportal_memhog_free(pfile->private_data);
263                         /* XXX The ioc_flags is not GFP flags now, need to
264                          * be fixed */
265                         err = kportal_memhog_alloc(pfile->private_data,
266                                                    data->ioc_count,
267                                                    data->ioc_flags);
268                         if (err != 0)
269                                 kportal_memhog_free(pfile->private_data);
270                 }
271                 break;
272
273         case IOC_LIBCFS_PING_TEST: {
274                 extern void (kping_client)(struct libcfs_ioctl_data *);
275                 void (*ping)(struct libcfs_ioctl_data *);
276
277                 CDEBUG(D_IOCTL, "doing %d pings to nid %s (%s)\n",
278                        data->ioc_count, libcfs_nid2str(data->ioc_nid),
279                        libcfs_nid2str(data->ioc_nid));
280                 ping = symbol_get(kping_client);
281                 if (!ping) {
282                         CERROR("symbol_get failed\n");
283                 } else {
284                         ping(data);
285                         symbol_put(kping_client);
286                 }
287                 RETURN(0);
288         }
289
290         default: {
291                 struct libcfs_ioctl_handler *hand;
292
293                 err = -EINVAL;
294                 down_read(&ioctl_list_sem);
295                 list_for_each_entry(hand, &ioctl_list, item) {
296                         err = hand->handle_ioctl(cmd, hdr);
297                         if (err != -EINVAL) {
298                                 if (err == 0)
299                                         err = libcfs_ioctl_popdata(arg,
300                                                         hdr, hdr->ioc_len);
301                                 break;
302                         }
303                 }
304                 up_read(&ioctl_list_sem);
305                 break;
306         }
307         }
308
309         RETURN(err);
310 }
311
312 static int libcfs_ioctl(struct cfs_psdev_file *pfile,
313                         unsigned long cmd, void *arg)
314 {
315         struct libcfs_ioctl_hdr *hdr;
316         int err = 0;
317         __u32 buf_len;
318         ENTRY;
319
320         err = libcfs_ioctl_getdata_len(arg, &buf_len);
321         if (err != 0)
322                 RETURN(err);
323
324         /*
325          * do a check here to restrict the size of the memory
326          * to allocate to guard against DoS attacks.
327          */
328         if (buf_len > LIBCFS_MAX_IOCTL_BUF_LEN) {
329                 CERROR("LNET: user buffer exceeds kernel buffer\n");
330                 RETURN(-EINVAL);
331         }
332
333         LIBCFS_ALLOC_GFP(hdr, buf_len, GFP_IOFS);
334         if (hdr == NULL)
335                 RETURN(-ENOMEM);
336
337         /* 'cmd' and permissions get checked in our arch-specific caller */
338         if (libcfs_ioctl_getdata(hdr, buf_len, arg)) {
339                 CERROR("LNET ioctl: data error\n");
340                 GOTO(out, err = -EINVAL);
341         }
342
343         err = libcfs_ioctl_handle(pfile, cmd, arg, hdr);
344
345 out:
346         LIBCFS_FREE(hdr, buf_len);
347         RETURN(err);
348 }
349
350
351 struct cfs_psdev_ops libcfs_psdev_ops = {
352         libcfs_psdev_open,
353         libcfs_psdev_release,
354         NULL,
355         NULL,
356         libcfs_ioctl
357 };
358
359 extern int insert_proc(void);
360 extern void remove_proc(void);
361 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
362 MODULE_DESCRIPTION("Portals v3.1");
363 MODULE_LICENSE("GPL");
364
365 extern struct miscdevice libcfs_dev;
366 extern struct rw_semaphore cfs_tracefile_sem;
367 extern struct mutex cfs_trace_thread_mutex;
368 extern struct cfs_wi_sched *cfs_sched_rehash;
369
370 extern void libcfs_init_nidstrings(void);
371 extern int libcfs_arch_init(void);
372 extern void libcfs_arch_cleanup(void);
373
374 static int init_libcfs_module(void)
375 {
376         int rc;
377
378         libcfs_arch_init();
379         libcfs_init_nidstrings();
380         init_rwsem(&cfs_tracefile_sem);
381         mutex_init(&cfs_trace_thread_mutex);
382         init_rwsem(&ioctl_list_sem);
383         INIT_LIST_HEAD(&ioctl_list);
384         init_waitqueue_head(&cfs_race_waitq);
385
386         rc = libcfs_debug_init(5 * 1024 * 1024);
387         if (rc < 0) {
388                 printk(KERN_ERR "LustreError: libcfs_debug_init: %d\n", rc);
389                 return (rc);
390         }
391
392         rc = cfs_cpu_init();
393         if (rc != 0)
394                 goto cleanup_debug;
395
396         rc = misc_register(&libcfs_dev);
397         if (rc) {
398                 CERROR("misc_register: error %d\n", rc);
399                 goto cleanup_cpu;
400         }
401
402         rc = cfs_wi_startup();
403         if (rc) {
404                 CERROR("initialize workitem: error %d\n", rc);
405                 goto cleanup_deregister;
406         }
407
408         /* max to 4 threads, should be enough for rehash */
409         rc = min(cfs_cpt_weight(cfs_cpt_table, CFS_CPT_ANY), 4);
410         rc = cfs_wi_sched_create("cfs_rh", cfs_cpt_table, CFS_CPT_ANY,
411                                  rc, &cfs_sched_rehash);
412         if (rc != 0) {
413                 CERROR("Startup workitem scheduler: error: %d\n", rc);
414                 goto cleanup_deregister;
415         }
416
417         rc = cfs_crypto_register();
418         if (rc) {
419                 CERROR("cfs_crypto_regster: error %d\n", rc);
420                 goto cleanup_wi;
421         }
422
423
424         rc = insert_proc();
425         if (rc) {
426                 CERROR("insert_proc: error %d\n", rc);
427                 goto cleanup_crypto;
428         }
429
430         CDEBUG (D_OTHER, "portals setup OK\n");
431         return 0;
432 cleanup_crypto:
433         cfs_crypto_unregister();
434 cleanup_wi:
435         cfs_wi_shutdown();
436 cleanup_deregister:
437         misc_deregister(&libcfs_dev);
438 cleanup_cpu:
439         cfs_cpu_fini();
440 cleanup_debug:
441         libcfs_debug_cleanup();
442         return rc;
443 }
444
445 static void exit_libcfs_module(void)
446 {
447         int rc;
448
449         remove_proc();
450
451         CDEBUG(D_MALLOC, "before Portals cleanup: kmem %d\n",
452                atomic_read(&libcfs_kmemory));
453
454         if (cfs_sched_rehash != NULL) {
455                 cfs_wi_sched_destroy(cfs_sched_rehash);
456                 cfs_sched_rehash = NULL;
457         }
458
459         cfs_crypto_unregister();
460         cfs_wi_shutdown();
461
462         rc = misc_deregister(&libcfs_dev);
463         if (rc)
464                 CERROR("misc_deregister error %d\n", rc);
465
466         cfs_cpu_fini();
467
468         if (atomic_read(&libcfs_kmemory) != 0)
469                 CERROR("Portals memory leaked: %d bytes\n",
470                        atomic_read(&libcfs_kmemory));
471
472         rc = libcfs_debug_cleanup();
473         if (rc)
474                 printk(KERN_ERR "LustreError: libcfs_debug_cleanup: %d\n",
475                        rc);
476
477         fini_rwsem(&ioctl_list_sem);
478         fini_rwsem(&cfs_tracefile_sem);
479
480         libcfs_arch_cleanup();
481 }
482
483 cfs_module(libcfs, "1.0.0", init_libcfs_module, exit_libcfs_module);