Whamcloud - gitweb
LU-3963 libcfs: remove proc handler wrappers
[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 #define LNET_MAX_IOCTL_BUF_LEN (sizeof(struct lnet_ioctl_net_config) + \
39                                  sizeof(struct lnet_ioctl_config_data))
40
41 #include <libcfs/libcfs.h>
42 #include <libcfs/libcfs_crypto.h>
43 #include <lnet/lib-lnet.h>
44 #include <lnet/lib-dlc.h>
45 #include <lnet/lnet.h>
46 #include <lnet/nidstr.h>
47 #include "tracefile.h"
48
49 void
50 kportal_memhog_free (struct libcfs_device_userstate *ldu)
51 {
52         struct page **level0p = &ldu->ldu_memhog_root_page;
53         struct page **level1p;
54         struct page **level2p;
55         int           count1;
56         int           count2;
57
58         if (*level0p != NULL) {
59                 level1p = (struct page **)page_address(*level0p);
60                 count1 = 0;
61
62                 while (count1 < PAGE_CACHE_SIZE/sizeof(struct page *) &&
63                        *level1p != NULL) {
64
65                         level2p = (struct page **)page_address(*level1p);
66                         count2 = 0;
67
68                         while (count2 < PAGE_CACHE_SIZE/sizeof(struct page *) &&
69                                *level2p != NULL) {
70
71                                 __free_page(*level2p);
72                                 ldu->ldu_memhog_pages--;
73                                 level2p++;
74                                 count2++;
75                         }
76
77                         __free_page(*level1p);
78                         ldu->ldu_memhog_pages--;
79                         level1p++;
80                         count1++;
81                 }
82
83                 __free_page(*level0p);
84                 ldu->ldu_memhog_pages--;
85
86                 *level0p = NULL;
87         }
88
89         LASSERT(ldu->ldu_memhog_pages == 0);
90 }
91
92 int
93 kportal_memhog_alloc(struct libcfs_device_userstate *ldu, int npages,
94                      gfp_t flags)
95 {
96         struct page **level0p;
97         struct page **level1p;
98         struct page **level2p;
99         int           count1;
100         int           count2;
101
102         LASSERT(ldu->ldu_memhog_pages == 0);
103         LASSERT(ldu->ldu_memhog_root_page == NULL);
104
105         if (npages < 0)
106                 return -EINVAL;
107
108         if (npages == 0)
109                 return 0;
110
111         level0p = &ldu->ldu_memhog_root_page;
112         *level0p = alloc_page(flags);
113         if (*level0p == NULL)
114                 return -ENOMEM;
115         ldu->ldu_memhog_pages++;
116
117         level1p = (struct page **)page_address(*level0p);
118         count1 = 0;
119         memset(level1p, 0, PAGE_CACHE_SIZE);
120
121         while (ldu->ldu_memhog_pages < npages &&
122                count1 < PAGE_CACHE_SIZE/sizeof(struct page *)) {
123
124                 if (cfs_signal_pending())
125                         return -EINTR;
126
127                 *level1p = alloc_page(flags);
128                 if (*level1p == NULL)
129                         return -ENOMEM;
130                 ldu->ldu_memhog_pages++;
131
132                 level2p = (struct page **)page_address(*level1p);
133                 count2 = 0;
134                 memset(level2p, 0, PAGE_CACHE_SIZE);
135
136                 while (ldu->ldu_memhog_pages < npages &&
137                        count2 < PAGE_CACHE_SIZE/sizeof(struct page *)) {
138
139                         if (cfs_signal_pending())
140                                 return -EINTR;
141
142                         *level2p = alloc_page(flags);
143                         if (*level2p == NULL)
144                                 return -ENOMEM;
145                         ldu->ldu_memhog_pages++;
146
147                         level2p++;
148                         count2++;
149                 }
150
151                 level1p++;
152                 count1++;
153         }
154
155         return 0;
156 }
157
158 /* called when opening /dev/device */
159 static int libcfs_psdev_open(unsigned long flags, void *args)
160 {
161         struct libcfs_device_userstate *ldu;
162         ENTRY;
163
164         try_module_get(THIS_MODULE);
165
166         LIBCFS_ALLOC(ldu, sizeof(*ldu));
167         if (ldu != NULL) {
168                 ldu->ldu_memhog_pages = 0;
169                 ldu->ldu_memhog_root_page = NULL;
170         }
171         *(struct libcfs_device_userstate **)args = ldu;
172
173         RETURN(0);
174 }
175
176 /* called when closing /dev/device */
177 static int libcfs_psdev_release(unsigned long flags, void *args)
178 {
179         struct libcfs_device_userstate *ldu;
180         ENTRY;
181
182         ldu = (struct libcfs_device_userstate *)args;
183         if (ldu != NULL) {
184                 kportal_memhog_free(ldu);
185                 LIBCFS_FREE(ldu, sizeof(*ldu));
186         }
187
188         module_put(THIS_MODULE);
189         RETURN(0);
190 }
191
192 static struct rw_semaphore ioctl_list_sem;
193 static struct list_head ioctl_list;
194
195 int libcfs_register_ioctl(struct libcfs_ioctl_handler *hand)
196 {
197         int rc = 0;
198
199         down_write(&ioctl_list_sem);
200         if (!list_empty(&hand->item))
201                 rc = -EBUSY;
202         else
203                 list_add_tail(&hand->item, &ioctl_list);
204         up_write(&ioctl_list_sem);
205
206         return rc;
207 }
208 EXPORT_SYMBOL(libcfs_register_ioctl);
209
210 int libcfs_deregister_ioctl(struct libcfs_ioctl_handler *hand)
211 {
212         int rc = 0;
213
214         down_write(&ioctl_list_sem);
215         if (list_empty(&hand->item))
216                 rc = -ENOENT;
217         else
218                 list_del_init(&hand->item);
219         up_write(&ioctl_list_sem);
220
221         return rc;
222 }
223 EXPORT_SYMBOL(libcfs_deregister_ioctl);
224
225 static int libcfs_ioctl_handle(struct cfs_psdev_file *pfile, unsigned long cmd,
226                                void *arg, struct libcfs_ioctl_hdr *hdr)
227 {
228         struct libcfs_ioctl_data *data = NULL;
229         int err;
230         ENTRY;
231
232         /* The libcfs_ioctl_data_adjust() function performs adjustment
233          * operations on the libcfs_ioctl_data structure to make
234          * it usable by the code.  This doesn't need to be called
235          * for new data structures added. */
236         if (hdr->ioc_version == LIBCFS_IOCTL_VERSION) {
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 > LNET_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);