Whamcloud - gitweb
8f0de46eac85e5a02132be50430ffd007cd0e271
[fs/lustre-release.git] / lnet / include / libcfs / linux / linux-mem.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lnet/include/libcfs/linux/linux-mem.h
37  *
38  * Basic library routines.
39  */
40
41 #ifndef __LIBCFS_LINUX_CFS_MEM_H__
42 #define __LIBCFS_LINUX_CFS_MEM_H__
43
44 #ifndef __LIBCFS_LIBCFS_H__
45 #error Do not #include this file directly. #include <libcfs/libcfs.h> instead
46 #endif
47
48 #ifdef __KERNEL__
49 # include <linux/mm.h>
50 # include <linux/vmalloc.h>
51 # include <linux/pagemap.h>
52 # include <linux/slab.h>
53 # ifdef HAVE_MM_INLINE
54 #  include <linux/mm_inline.h>
55 # endif
56
57 typedef struct page                     cfs_page_t;
58 #define CFS_PAGE_SIZE                   PAGE_CACHE_SIZE
59 #define CFS_PAGE_SHIFT                  PAGE_CACHE_SHIFT
60 #define CFS_PAGE_MASK                   (~((__u64)CFS_PAGE_SIZE-1))
61
62 static inline void *cfs_page_address(cfs_page_t *page)
63 {
64         /*
65          * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
66          * from here: this will lead to infinite recursion.
67          */
68         return page_address(page);
69 }
70
71 static inline void *cfs_kmap(cfs_page_t *page)
72 {
73         return kmap(page);
74 }
75
76 static inline void cfs_kunmap(cfs_page_t *page)
77 {
78         kunmap(page);
79 }
80
81 static inline void cfs_get_page(cfs_page_t *page)
82 {
83         get_page(page);
84 }
85
86 static inline int cfs_page_count(cfs_page_t *page)
87 {
88         return page_count(page);
89 }
90
91 #define cfs_page_index(p)       ((p)->index)
92
93 #define cfs_page_pin(page) page_cache_get(page)
94 #define cfs_page_unpin(page) page_cache_release(page)
95
96 /*
97  * Memory allocator
98  * XXX Liang: move these declare to public file
99  */
100 extern void *cfs_alloc(size_t nr_bytes, u_int32_t flags);
101 extern void  cfs_free(void *addr);
102
103 extern void *cfs_alloc_large(size_t nr_bytes);
104 extern void  cfs_free_large(void *addr);
105
106 extern cfs_page_t *cfs_alloc_pages(unsigned int flags, unsigned int order);
107 extern void __cfs_free_pages(cfs_page_t *page, unsigned int order);
108
109 #define cfs_alloc_page(flags)  cfs_alloc_pages(flags, 0)
110 #define __cfs_free_page(page)  __cfs_free_pages(page, 0)
111 #define cfs_free_page(p)       __free_pages(p, 0)
112
113 #define libcfs_memory_pressure_get() (current->flags & PF_MEMALLOC)
114 #define libcfs_memory_pressure_set() do { current->flags |= PF_MEMALLOC; } while(0)
115 #define libcfs_memory_pressure_clr() do { current->flags &= ~PF_MEMALLOC; } while (0)
116
117 /*
118  * In Linux there is no way to determine whether current execution context is
119  * blockable.
120  */
121 #define CFS_ALLOC_ATOMIC_TRY   CFS_ALLOC_ATOMIC
122
123 /*
124  * SLAB allocator
125  * XXX Liang: move these declare to public file
126  */
127 #ifdef HAVE_KMEM_CACHE
128 typedef struct kmem_cache cfs_mem_cache_t;
129 #else
130 typedef kmem_cache_t cfs_mem_cache_t;
131 #endif
132 extern cfs_mem_cache_t * cfs_mem_cache_create (const char *, size_t, size_t, unsigned long);
133 extern int cfs_mem_cache_destroy ( cfs_mem_cache_t * );
134 extern void *cfs_mem_cache_alloc ( cfs_mem_cache_t *, int);
135 extern void cfs_mem_cache_free ( cfs_mem_cache_t *, void *);
136
137 /*
138  */
139 #define CFS_DECL_MMSPACE                mm_segment_t __oldfs
140 #define CFS_MMSPACE_OPEN                do { __oldfs = get_fs(); set_fs(get_ds());} while(0)
141 #define CFS_MMSPACE_CLOSE               set_fs(__oldfs)
142
143 #else   /* !__KERNEL__ */
144 #ifdef HAVE_ASM_PAGE_H
145 #include <asm/page.h>           /* needed for PAGE_SIZE - rread */
146 #endif
147
148 #include <libcfs/user-prim.h>
149 /* __KERNEL__ */
150 #endif
151
152 #endif /* __LINUX_CFS_MEM_H__ */