Whamcloud - gitweb
Landing b_hd_newconfig on HEAD
[fs/lustre-release.git] / lnet / libcfs / linux / linux-mem.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21 #define DEBUG_SUBSYSTEM S_LNET
22
23 #include <linux/mm.h>
24 #include <linux/vmalloc.h>
25 #include <linux/slab.h>
26 #include <linux/highmem.h>
27 #include <libcfs/libcfs.h>
28
29 static unsigned int cfs_alloc_flags_to_gfp(u_int32_t flags)
30 {
31         unsigned int mflags = 0;
32
33 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
34         if (flags & CFS_ALLOC_ATOMIC)
35                 mflags |= __GFP_HIGH;
36         else if (flags & CFS_ALLOC_WAIT)
37                 mflags |= __GFP_WAIT;
38         else
39                 mflags |= (__GFP_HIGH | __GFP_WAIT);
40         if (flags & CFS_ALLOC_IO)
41                 mflags |= __GFP_IO | __GFP_HIGHIO;
42 #else
43         if (flags & CFS_ALLOC_ATOMIC)
44                 mflags |= __GFP_HIGH;
45         else
46                 mflags |= __GFP_WAIT;
47         if (flags & CFS_ALLOC_NOWARN)
48                 mflags |= __GFP_NOWARN;
49         if (flags & CFS_ALLOC_IO)
50                 mflags |= __GFP_IO;
51 #endif
52         if (flags & CFS_ALLOC_FS)
53                 mflags |= __GFP_FS;
54         return mflags;
55 }
56
57 void *
58 cfs_alloc(size_t nr_bytes, u_int32_t flags)
59 {
60         void *ptr = NULL;
61
62         ptr = kmalloc(nr_bytes, cfs_alloc_flags_to_gfp(flags));
63         if (ptr != NULL && (flags & CFS_ALLOC_ZERO))
64                 memset(ptr, 0, nr_bytes);
65         return ptr;
66 }
67
68 void
69 cfs_free(void *addr)
70 {
71         kfree(addr);
72 }
73
74 void *
75 cfs_alloc_large(size_t nr_bytes)
76 {
77         return vmalloc(nr_bytes);
78 }
79
80 void
81 cfs_free_large(void *addr)
82 {
83         vfree(addr);
84 }
85
86 cfs_page_t *cfs_alloc_page(unsigned int flags)
87 {
88         /*
89          * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
90          * from here: this will lead to infinite recursion.
91          */
92         return alloc_pages(cfs_alloc_flags_to_gfp(flags), 0);
93 }
94
95 cfs_mem_cache_t *
96 cfs_mem_cache_create (const char *name, size_t size, size_t offset,
97                       unsigned long flags)
98 {
99         return kmem_cache_create(name, size, offset, flags, NULL, NULL);
100 }
101
102 int
103 cfs_mem_cache_destroy (cfs_mem_cache_t * cachep)
104 {
105 #ifdef HAVE_KMEM_CACHE_DESTROY_INT
106         return kmem_cache_destroy(cachep);
107 #else
108         kmem_cache_destroy(cachep);
109         return 0;
110 #endif
111 }
112
113 void *
114 cfs_mem_cache_alloc(cfs_mem_cache_t *cachep, int flags)
115 {
116         return kmem_cache_alloc(cachep, cfs_alloc_flags_to_gfp(flags));
117 }
118
119 void
120 cfs_mem_cache_free(cfs_mem_cache_t *cachep, void *objp)
121 {
122         return kmem_cache_free(cachep, objp);
123 }
124
125 EXPORT_SYMBOL(cfs_alloc);
126 EXPORT_SYMBOL(cfs_free);
127 EXPORT_SYMBOL(cfs_alloc_large);
128 EXPORT_SYMBOL(cfs_free_large);
129 EXPORT_SYMBOL(cfs_alloc_page);
130 EXPORT_SYMBOL(cfs_mem_cache_create);
131 EXPORT_SYMBOL(cfs_mem_cache_destroy);
132 EXPORT_SYMBOL(cfs_mem_cache_alloc);
133 EXPORT_SYMBOL(cfs_mem_cache_free);