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