blob: c64a29cdfff3446835da2e29ed3774840a551b97 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
18#ifndef __XFS_SUPPORT_KMEM_H__
19#define __XFS_SUPPORT_KMEM_H__
20
21#include <linux/slab.h>
22#include <linux/sched.h>
23#include <linux/mm.h>
24
25/*
26 * memory management routines
27 */
Christoph Hellwig760dea62005-09-02 16:56:02 +100028#define KM_SLEEP 0x0001u
29#define KM_NOSLEEP 0x0002u
30#define KM_NOFS 0x0004u
31#define KM_MAYFAIL 0x0008u
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Pekka J Enberg2109a2d2005-11-07 00:58:01 -080033#define kmem_zone kmem_cache
34#define kmem_zone_t struct kmem_cache
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36typedef unsigned long xfs_pflags_t;
37
38#define PFLAGS_TEST_NOIO() (current->flags & PF_NOIO)
39#define PFLAGS_TEST_FSTRANS() (current->flags & PF_FSTRANS)
40
41#define PFLAGS_SET_NOIO() do { \
42 current->flags |= PF_NOIO; \
43} while (0)
44
45#define PFLAGS_CLEAR_NOIO() do { \
46 current->flags &= ~PF_NOIO; \
47} while (0)
48
49/* these could be nested, so we save state */
50#define PFLAGS_SET_FSTRANS(STATEP) do { \
51 *(STATEP) = current->flags; \
52 current->flags |= PF_FSTRANS; \
53} while (0)
54
55#define PFLAGS_CLEAR_FSTRANS(STATEP) do { \
56 *(STATEP) = current->flags; \
57 current->flags &= ~PF_FSTRANS; \
58} while (0)
59
60/* Restore the PF_FSTRANS state to what was saved in STATEP */
61#define PFLAGS_RESTORE_FSTRANS(STATEP) do { \
62 current->flags = ((current->flags & ~PF_FSTRANS) | \
63 (*(STATEP) & PF_FSTRANS)); \
64} while (0)
65
66#define PFLAGS_DUP(OSTATEP, NSTATEP) do { \
67 *(NSTATEP) = *(OSTATEP); \
68} while (0)
69
Al Viro27496a82005-10-21 03:20:48 -040070static __inline gfp_t kmem_flags_convert(unsigned int __nocast flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Nathan Scott7f248a82005-11-03 16:14:31 +110072 gfp_t lflags = __GFP_NOWARN; /* we'll report problems, if need be */
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74#ifdef DEBUG
75 if (unlikely(flags & ~(KM_SLEEP|KM_NOSLEEP|KM_NOFS|KM_MAYFAIL))) {
76 printk(KERN_WARNING
77 "XFS: memory allocation with wrong flags (%x)\n", flags);
78 BUG();
79 }
80#endif
81
82 if (flags & KM_NOSLEEP) {
83 lflags |= GFP_ATOMIC;
84 } else {
85 lflags |= GFP_KERNEL;
86
87 /* avoid recusive callbacks to filesystem during transactions */
88 if (PFLAGS_TEST_FSTRANS() || (flags & KM_NOFS))
89 lflags &= ~__GFP_FS;
90 }
Nathan Scott7f248a82005-11-03 16:14:31 +110091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 return lflags;
93}
94
95static __inline kmem_zone_t *
96kmem_zone_init(int size, char *zone_name)
97{
98 return kmem_cache_create(zone_name, size, 0, 0, NULL, NULL);
99}
100
101static __inline void
102kmem_zone_free(kmem_zone_t *zone, void *ptr)
103{
104 kmem_cache_free(zone, ptr);
105}
106
107static __inline void
108kmem_zone_destroy(kmem_zone_t *zone)
109{
110 if (zone && kmem_cache_destroy(zone))
111 BUG();
112}
113
Nathan Scott7f248a82005-11-03 16:14:31 +1100114extern void *kmem_zone_zalloc(kmem_zone_t *, unsigned int __nocast);
115extern void *kmem_zone_alloc(kmem_zone_t *, unsigned int __nocast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Nathan Scott7f248a82005-11-03 16:14:31 +1100117extern void *kmem_alloc(size_t, unsigned int __nocast);
118extern void *kmem_realloc(void *, size_t, size_t, unsigned int __nocast);
119extern void *kmem_zalloc(size_t, unsigned int __nocast);
120extern void kmem_free(void *, size_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122typedef struct shrinker *kmem_shaker_t;
Al Viro27496a82005-10-21 03:20:48 -0400123typedef int (*kmem_shake_func_t)(int, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125static __inline kmem_shaker_t
126kmem_shake_register(kmem_shake_func_t sfunc)
127{
128 return set_shrinker(DEFAULT_SEEKS, sfunc);
129}
130
131static __inline void
132kmem_shake_deregister(kmem_shaker_t shrinker)
133{
134 remove_shrinker(shrinker);
135}
136
137static __inline int
Al Viro27496a82005-10-21 03:20:48 -0400138kmem_shake_allow(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
140 return (gfp_mask & __GFP_WAIT);
141}
142
143#endif /* __XFS_SUPPORT_KMEM_H__ */