Whamcloud - gitweb
29ca9d28a3181837a18496496f53ea2c0290f5d6
[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
39 #include <libcfs/libcfs.h>
40 #include <libcfs/libcfs_crypto.h>
41 #include <lnet/lib-lnet.h>
42 #include <lnet/lib-dlc.h>
43 #include <lnet/lnet.h>
44 #include <lnet/nidstr.h>
45 #include "tracefile.h"
46
47 static void
48 kportal_memhog_free (struct libcfs_device_userstate *ldu)
49 {
50         struct page **level0p = &ldu->ldu_memhog_root_page;
51         struct page **level1p;
52         struct page **level2p;
53         int           count1;
54         int           count2;
55
56         if (*level0p != NULL) {
57                 level1p = (struct page **)page_address(*level0p);
58                 count1 = 0;
59
60                 while (count1 < PAGE_CACHE_SIZE/sizeof(struct page *) &&
61                        *level1p != NULL) {
62
63                         level2p = (struct page **)page_address(*level1p);
64                         count2 = 0;
65
66                         while (count2 < PAGE_CACHE_SIZE/sizeof(struct page *) &&
67                                *level2p != NULL) {
68
69                                 __free_page(*level2p);
70                                 ldu->ldu_memhog_pages--;
71                                 level2p++;
72                                 count2++;
73                         }
74
75                         __free_page(*level1p);
76                         ldu->ldu_memhog_pages--;
77                         level1p++;
78                         count1++;
79                 }
80
81                 __free_page(*level0p);
82                 ldu->ldu_memhog_pages--;
83
84                 *level0p = NULL;
85         }
86
87         LASSERT(ldu->ldu_memhog_pages == 0);
88 }
89
90 static int
91 kportal_memhog_alloc(struct libcfs_device_userstate *ldu, int npages,
92                      gfp_t flags)
93 {
94         struct page **level0p;
95         struct page **level1p;
96         struct page **level2p;
97         int           count1;
98         int           count2;
99
100         LASSERT(ldu->ldu_memhog_pages == 0);
101         LASSERT(ldu->ldu_memhog_root_page == NULL);
102
103         if (npages < 0)
104                 return -EINVAL;
105
106         if (npages == 0)
107                 return 0;
108
109         level0p = &ldu->ldu_memhog_root_page;
110         *level0p = alloc_page(flags);
111         if (*level0p == NULL)
112                 return -ENOMEM;
113         ldu->ldu_memhog_pages++;
114
115         level1p = (struct page **)page_address(*level0p);
116         count1 = 0;
117         memset(level1p, 0, PAGE_CACHE_SIZE);
118
119         while (ldu->ldu_memhog_pages < npages &&
120                count1 < PAGE_CACHE_SIZE/sizeof(struct page *)) {
121
122                 if (cfs_signal_pending())
123                         return -EINTR;
124
125                 *level1p = alloc_page(flags);
126                 if (*level1p == NULL)
127                         return -ENOMEM;
128                 ldu->ldu_memhog_pages++;
129
130                 level2p = (struct page **)page_address(*level1p);
131                 count2 = 0;
132                 memset(level2p, 0, PAGE_CACHE_SIZE);
133
134                 while (ldu->ldu_memhog_pages < npages &&
135                        count2 < PAGE_CACHE_SIZE/sizeof(struct page *)) {
136
137                         if (cfs_signal_pending())
138                                 return -EINTR;
139
140                         *level2p = alloc_page(flags);
141                         if (*level2p == NULL)
142                                 return -ENOMEM;
143                         ldu->ldu_memhog_pages++;
144
145                         level2p++;
146                         count2++;
147                 }
148
149                 level1p++;
150                 count1++;
151         }
152
153         return 0;
154 }
155
156 /* called when opening /dev/device */
157 static int libcfs_psdev_open(unsigned long flags, void *args)
158 {
159         struct libcfs_device_userstate *ldu;
160         ENTRY;
161
162         try_module_get(THIS_MODULE);
163
164         LIBCFS_ALLOC(ldu, sizeof(*ldu));
165         if (ldu != NULL) {
166                 ldu->ldu_memhog_pages = 0;
167                 ldu->ldu_memhog_root_page = NULL;
168         }
169         *(struct libcfs_device_userstate **)args = ldu;
170
171         RETURN(0);
172 }
173
174 /* called when closing /dev/device */
175 static int libcfs_psdev_release(unsigned long flags, void *args)
176 {
177         struct libcfs_device_userstate *ldu;
178         ENTRY;
179
180         ldu = (struct libcfs_device_userstate *)args;
181         if (ldu != NULL) {
182                 kportal_memhog_free(ldu);
183                 LIBCFS_FREE(ldu, sizeof(*ldu));
184         }
185
186         module_put(THIS_MODULE);
187         RETURN(0);
188 }
189
190 static struct rw_semaphore ioctl_list_sem;
191 static struct list_head ioctl_list;
192
193 int libcfs_register_ioctl(struct libcfs_ioctl_handler *hand)
194 {
195         int rc = 0;
196
197         down_write(&ioctl_list_sem);
198         if (!list_empty(&hand->item))
199                 rc = -EBUSY;
200         else
201                 list_add_tail(&hand->item, &ioctl_list);
202         up_write(&ioctl_list_sem);
203
204         return rc;
205 }
206 EXPORT_SYMBOL(libcfs_register_ioctl);
207
208 int libcfs_deregister_ioctl(struct libcfs_ioctl_handler *hand)
209 {
210         int rc = 0;
211
212         down_write(&ioctl_list_sem);
213         if (list_empty(&hand->item))
214                 rc = -ENOENT;
215         else
216                 list_del_init(&hand->item);
217         up_write(&ioctl_list_sem);
218
219         return rc;
220 }
221 EXPORT_SYMBOL(libcfs_deregister_ioctl);
222
223 static int libcfs_ioctl(struct cfs_psdev_file *pfile,
224                         unsigned long cmd, void __user *uparam)
225 {
226         struct libcfs_ioctl_data *data = NULL;
227         struct libcfs_ioctl_hdr  *hdr;
228         int                       err;
229         ENTRY;
230
231         /* 'cmd' and permissions get checked in our arch-specific caller */
232         err = libcfs_ioctl_getdata(&hdr, uparam);
233         if (err != 0) {
234                 CDEBUG_LIMIT(D_ERROR,
235                              "libcfs ioctl: data header error %d\n", err);
236                 RETURN(err);
237         }
238
239         if (hdr->ioc_version == LIBCFS_IOCTL_VERSION) {
240                 /* The libcfs_ioctl_data_adjust() function performs adjustment
241                  * operations on the libcfs_ioctl_data structure to make
242                  * it usable by the code.  This doesn't need to be called
243                  * for new data structures added. */
244                 data = container_of(hdr, struct libcfs_ioctl_data, ioc_hdr);
245                 err = libcfs_ioctl_data_adjust(data);
246                 if (err != 0)
247                         GOTO(out, err);
248         }
249
250         CDEBUG(D_IOCTL, "libcfs ioctl cmd %lu\n", cmd);
251         switch (cmd) {
252         case IOC_LIBCFS_CLEAR_DEBUG:
253                 libcfs_debug_clear_buffer();
254                 break;
255         /*
256          * case IOC_LIBCFS_PANIC:
257          * Handled in arch/cfs_module.c
258          */
259         case IOC_LIBCFS_MARK_DEBUG:
260                 if (data == NULL ||
261                     data->ioc_inlbuf1 == NULL ||
262                     data->ioc_inlbuf1[data->ioc_inllen1 - 1] != '\0')
263                         GOTO(out, err = -EINVAL);
264
265                 libcfs_debug_mark_buffer(data->ioc_inlbuf1);
266                 break;
267
268         case IOC_LIBCFS_MEMHOG:
269                 if (data == NULL)
270                         GOTO(out, err = -EINVAL);
271
272                 if (pfile->private_data == NULL)
273                         GOTO(out, err = -EINVAL);
274
275                 kportal_memhog_free(pfile->private_data);
276                 err = kportal_memhog_alloc(pfile->private_data,
277                                            data->ioc_count, data->ioc_flags);
278                 if (err != 0)
279                         kportal_memhog_free(pfile->private_data);
280                 break;
281
282         default: {
283                 struct libcfs_ioctl_handler *hand;
284
285                 err = -EINVAL;
286                 down_read(&ioctl_list_sem);
287                 list_for_each_entry(hand, &ioctl_list, item) {
288                         err = hand->handle_ioctl(cmd, hdr);
289                         if (err == -EINVAL)
290                                 continue;
291
292                         if (err == 0)
293                                 err = libcfs_ioctl_popdata(hdr, uparam);
294                         break;
295                 }
296                 up_read(&ioctl_list_sem);
297                 break; }
298         }
299 out:
300         libcfs_ioctl_freedata(hdr);
301         RETURN(err);
302 }
303
304 struct cfs_psdev_ops libcfs_psdev_ops = {
305         libcfs_psdev_open,
306         libcfs_psdev_release,
307         NULL,
308         NULL,
309         libcfs_ioctl
310 };
311
312 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
313 MODULE_DESCRIPTION("Portals v3.1");
314 MODULE_LICENSE("GPL");
315
316 static int init_libcfs_module(void)
317 {
318         int rc;
319
320         libcfs_arch_init();
321         libcfs_init_nidstrings();
322         init_rwsem(&cfs_tracefile_sem);
323         mutex_init(&cfs_trace_thread_mutex);
324         init_rwsem(&ioctl_list_sem);
325         INIT_LIST_HEAD(&ioctl_list);
326         init_waitqueue_head(&cfs_race_waitq);
327
328         rc = libcfs_debug_init(5 * 1024 * 1024);
329         if (rc < 0) {
330                 printk(KERN_ERR "LustreError: libcfs_debug_init: %d\n", rc);
331                 return (rc);
332         }
333
334         rc = cfs_cpu_init();
335         if (rc != 0)
336                 goto cleanup_debug;
337
338         rc = misc_register(&libcfs_dev);
339         if (rc) {
340                 CERROR("misc_register: error %d\n", rc);
341                 goto cleanup_cpu;
342         }
343
344         rc = cfs_wi_startup();
345         if (rc) {
346                 CERROR("initialize workitem: error %d\n", rc);
347                 goto cleanup_deregister;
348         }
349
350         /* max to 4 threads, should be enough for rehash */
351         rc = min(cfs_cpt_weight(cfs_cpt_table, CFS_CPT_ANY), 4);
352         rc = cfs_wi_sched_create("cfs_rh", cfs_cpt_table, CFS_CPT_ANY,
353                                  rc, &cfs_sched_rehash);
354         if (rc != 0) {
355                 CERROR("Startup workitem scheduler: error: %d\n", rc);
356                 goto cleanup_deregister;
357         }
358
359         rc = cfs_crypto_register();
360         if (rc) {
361                 CERROR("cfs_crypto_regster: error %d\n", rc);
362                 goto cleanup_wi;
363         }
364
365
366         rc = insert_proc();
367         if (rc) {
368                 CERROR("insert_proc: error %d\n", rc);
369                 goto cleanup_crypto;
370         }
371
372         CDEBUG (D_OTHER, "portals setup OK\n");
373         return 0;
374 cleanup_crypto:
375         cfs_crypto_unregister();
376 cleanup_wi:
377         cfs_wi_shutdown();
378 cleanup_deregister:
379         misc_deregister(&libcfs_dev);
380 cleanup_cpu:
381         cfs_cpu_fini();
382 cleanup_debug:
383         libcfs_debug_cleanup();
384         return rc;
385 }
386
387 static void exit_libcfs_module(void)
388 {
389         int rc;
390
391         remove_proc();
392
393         CDEBUG(D_MALLOC, "before Portals cleanup: kmem %d\n",
394                atomic_read(&libcfs_kmemory));
395
396         if (cfs_sched_rehash != NULL) {
397                 cfs_wi_sched_destroy(cfs_sched_rehash);
398                 cfs_sched_rehash = NULL;
399         }
400
401         cfs_crypto_unregister();
402         cfs_wi_shutdown();
403
404         rc = misc_deregister(&libcfs_dev);
405         if (rc)
406                 CERROR("misc_deregister error %d\n", rc);
407
408         cfs_cpu_fini();
409
410         if (atomic_read(&libcfs_kmemory) != 0)
411                 CERROR("Portals memory leaked: %d bytes\n",
412                        atomic_read(&libcfs_kmemory));
413
414         rc = libcfs_debug_cleanup();
415         if (rc)
416                 printk(KERN_ERR "LustreError: libcfs_debug_cleanup: %d\n",
417                        rc);
418
419         fini_rwsem(&ioctl_list_sem);
420         fini_rwsem(&cfs_tracefile_sem);
421
422         libcfs_arch_cleanup();
423 }
424
425 cfs_module(libcfs, "1.0.0", init_libcfs_module, exit_libcfs_module);