2 * linux/fs/hfsplus/bitmap.c
5 * Brad Boyer (flar@allandria.com)
6 * (C) 2003 Ardis Technologies <roman@ardistech.com>
8 * Handling of allocation file
11 #include <linux/pagemap.h>
13 #include "hfsplus_fs.h"
14 #include "hfsplus_raw.h"
16 #define PAGE_CACHE_BITS (PAGE_CACHE_SIZE * 8)
18 int hfsplus_block_allocate(struct super_block *sb, u32 size, u32 offset, u32 *max)
21 struct address_space *mapping;
22 __be32 *pptr, *curr, *end;
23 u32 mask, start, len, n;
31 dprint(DBG_BITMAP, "block_allocate: %u,%u,%u\n", size, offset, len);
32 mutex_lock(&HFSPLUS_SB(sb).alloc_file->i_mutex);
33 mapping = HFSPLUS_SB(sb).alloc_file->i_mapping;
34 page = read_mapping_page(mapping, offset / PAGE_CACHE_BITS, NULL);
36 curr = pptr + (offset & (PAGE_CACHE_BITS - 1)) / 32;
38 offset &= ~(PAGE_CACHE_BITS - 1);
39 if ((size ^ offset) / PAGE_CACHE_BITS)
40 end = pptr + PAGE_CACHE_BITS / 32;
42 end = pptr + ((size + 31) & (PAGE_CACHE_BITS - 1)) / 32;
44 /* scan the first partial u32 for zero bits */
48 mask = (1U << 31) >> i;
49 for (; i < 32; mask >>= 1, i++) {
56 /* scan complete u32s for the first zero bit */
63 for (i = 0; i < 32; mask >>= 1, i++) {
71 offset += PAGE_CACHE_BITS;
74 page = read_mapping_page(mapping, offset / PAGE_CACHE_BITS,
76 curr = pptr = kmap(page);
77 if ((size ^ offset) / PAGE_CACHE_BITS)
78 end = pptr + PAGE_CACHE_BITS / 32;
80 end = pptr + ((size + 31) & (PAGE_CACHE_BITS - 1)) / 32;
82 dprint(DBG_BITMAP, "bitmap full\n");
87 start = offset + (curr - pptr) * 32 + i;
89 dprint(DBG_BITMAP, "bitmap full\n");
92 /* do any partial u32 at the start */
93 len = min(size - start, len);
99 if (!--len || n & mask)
104 *curr++ = cpu_to_be32(n);
108 n = be32_to_cpu(*curr);
115 *curr++ = cpu_to_be32(0xffffffff);
118 set_page_dirty(page);
120 offset += PAGE_CACHE_BITS;
121 page = read_mapping_page(mapping, offset / PAGE_CACHE_BITS,
125 end = pptr + PAGE_CACHE_BITS / 32;
128 /* do any partial u32 at end */
130 for (i = 0; i < len; i++) {
137 *curr = cpu_to_be32(n);
138 set_page_dirty(page);
140 *max = offset + (curr - pptr) * 32 + i - start;
141 HFSPLUS_SB(sb).free_blocks -= *max;
143 dprint(DBG_BITMAP, "-> %u,%u\n", start, *max);
145 mutex_unlock(&HFSPLUS_SB(sb).alloc_file->i_mutex);
149 int hfsplus_block_free(struct super_block *sb, u32 offset, u32 count)
152 struct address_space *mapping;
153 __be32 *pptr, *curr, *end;
157 /* is there any actual work to be done? */
161 dprint(DBG_BITMAP, "block_free: %u,%u\n", offset, count);
162 /* are all of the bits in range? */
163 if ((offset + count) > HFSPLUS_SB(sb).total_blocks)
166 mutex_lock(&HFSPLUS_SB(sb).alloc_file->i_mutex);
167 mapping = HFSPLUS_SB(sb).alloc_file->i_mapping;
168 pnr = offset / PAGE_CACHE_BITS;
169 page = read_mapping_page(mapping, pnr, NULL);
171 curr = pptr + (offset & (PAGE_CACHE_BITS - 1)) / 32;
172 end = pptr + PAGE_CACHE_BITS / 32;
175 /* do any partial u32 at the start */
179 mask = 0xffffffffU << j;
181 mask |= 0xffffffffU >> (i + count);
182 *curr++ &= cpu_to_be32(mask);
185 *curr++ &= cpu_to_be32(mask);
199 set_page_dirty(page);
201 page = read_mapping_page(mapping, ++pnr, NULL);
204 end = pptr + PAGE_CACHE_BITS / 32;
207 /* do any partial u32 at end */
209 mask = 0xffffffffU >> count;
210 *curr &= cpu_to_be32(mask);
213 set_page_dirty(page);
215 HFSPLUS_SB(sb).free_blocks += len;
217 mutex_unlock(&HFSPLUS_SB(sb).alloc_file->i_mutex);