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