1 /*
2  * Copyright (c) 2000-2004 Silicon Graphics, Inc.  All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of version 2 of the GNU General Public License as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it would be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  *
12  * Further, this software is distributed without any warranty that it is
13  * free of the rightful claim of any third person regarding infringement
14  * or the like.  Any license provided herein, whether implied or
15  * otherwise, applies only to this software file.  Patent licenses, if
16  * any, provided herein do not apply to combinations of this program with
17  * other software, or any other product whatsoever.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write the Free Software Foundation, Inc., 59
21  * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22  *
23  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24  * Mountain View, CA  94043, or:
25  *
26  * http://www.sgi.com
27  *
28  * For further information regarding this notice, see:
29  *
30  * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31  */
32 #ifndef __XFS_SUPPORT_KMEM_H__
33 #define __XFS_SUPPORT_KMEM_H__
34 
35 #include <linux/slab.h>
36 
37 /*
38  * memory management routines
39  */
40 #define KM_SLEEP	0x0001
41 #define KM_NOSLEEP	0x0002
42 #define KM_NOFS		0x0004
43 #define KM_MAYFAIL	0x0008
44 
45 #define	kmem_zone	kmem_cache_s
46 #define kmem_zone_t	kmem_cache_t
47 
48 typedef unsigned long xfs_pflags_t;
49 
50 #define PFLAGS_TEST_NOIO()              (current->flags & PF_NOIO)
51 #define PFLAGS_TEST_FSTRANS()           (current->flags & PF_FSTRANS)
52 
53 #define PFLAGS_SET_NOIO() do {		\
54 	current->flags |= PF_NOIO;	\
55 } while (0)
56 
57 #define PFLAGS_CLEAR_NOIO() do {	\
58 	current->flags &= ~PF_NOIO;	\
59 } while (0)
60 
61 /* these could be nested, so we save state */
62 #define PFLAGS_SET_FSTRANS(STATEP) do {	\
63 	*(STATEP) = current->flags;	\
64 	current->flags |= PF_FSTRANS;	\
65 } while (0)
66 
67 #define PFLAGS_CLEAR_FSTRANS(STATEP) do { \
68 	*(STATEP) = current->flags;	\
69 	current->flags &= ~PF_FSTRANS;	\
70 } while (0)
71 
72 /* Restore the PF_FSTRANS state to what was saved in STATEP */
73 #define PFLAGS_RESTORE_FSTRANS(STATEP) do {     		\
74 	current->flags = ((current->flags & ~PF_FSTRANS) |	\
75 			  (*(STATEP) & PF_FSTRANS));		\
76 } while (0)
77 
78 #define PFLAGS_DUP(OSTATEP, NSTATEP) do { \
79 	*(NSTATEP) = *(OSTATEP);	\
80 } while (0)
81 
kmem_flags_convert(int flags)82 static __inline unsigned int kmem_flags_convert(int flags)
83 {
84         int lflags;
85 
86 #ifdef DEBUG
87 	if (unlikely(flags & ~(KM_SLEEP|KM_NOSLEEP|KM_NOFS|KM_MAYFAIL))) {
88 		printk(KERN_WARNING
89 		    "XFS: memory allocation with wrong flags (%x)\n", flags);
90 		BUG();
91 	}
92 #endif
93 
94 	if (flags & KM_NOSLEEP) {
95 		lflags = GFP_ATOMIC;
96 	} else {
97 		lflags = GFP_KERNEL;
98 
99 		/* avoid recusive callbacks to filesystem during transactions */
100 		if (PFLAGS_TEST_FSTRANS() || (flags & KM_NOFS))
101 			lflags &= ~__GFP_FS;
102 	}
103 
104         return lflags;
105 }
106 
107 static __inline kmem_zone_t *
kmem_zone_init(int size,char * zone_name)108 kmem_zone_init(int size, char *zone_name)
109 {
110 	return kmem_cache_create(zone_name, size, 0, 0, NULL, NULL);
111 }
112 
113 static __inline void
kmem_zone_free(kmem_zone_t * zone,void * ptr)114 kmem_zone_free(kmem_zone_t *zone, void *ptr)
115 {
116 	kmem_cache_free(zone, ptr);
117 }
118 
119 static __inline void
kmem_zone_destroy(kmem_zone_t * zone)120 kmem_zone_destroy(kmem_zone_t *zone)
121 {
122 	if (zone && kmem_cache_destroy(zone))
123 		BUG();
124 }
125 
126 static __inline int
kmem_zone_shrink(kmem_zone_t * zone)127 kmem_zone_shrink(kmem_zone_t *zone)
128 {
129 	return kmem_cache_shrink(zone);
130 }
131 
132 extern void	    *kmem_zone_zalloc(kmem_zone_t *, int);
133 extern void	    *kmem_zone_alloc(kmem_zone_t *, int);
134 
135 extern void	    *kmem_alloc(size_t, int);
136 extern void	    *kmem_realloc(void *, size_t, size_t, int);
137 extern void	    *kmem_zalloc(size_t, int);
138 extern void         kmem_free(void *, size_t);
139 
140 typedef void	    *kmem_shaker_t;
141 typedef int	    (*kmem_shake_func_t)(int, unsigned int);
142 
143 extern kmem_shaker_t	kmem_shake_register(kmem_shake_func_t);
144 extern void	    kmem_shake_deregister(kmem_shaker_t);
145 
146 static __inline int
kmem_shake_allow(unsigned int gfp_mask)147 kmem_shake_allow(unsigned int gfp_mask)
148 {
149 	return (gfp_mask & __GFP_WAIT);
150 }
151 
152 #endif /* __XFS_SUPPORT_KMEM_H__ */
153