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)
20 struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
22 struct address_space *mapping;
23 __be32 *pptr, *curr, *end;
24 u32 mask, start, len, n;
32 dprint(DBG_BITMAP, "block_allocate: %u,%u,%u\n", size, offset, len);
33 mutex_lock(&sbi->alloc_mutex);
34 mapping = sbi->alloc_file->i_mapping;
35 page = read_mapping_page(mapping, offset / PAGE_CACHE_BITS, NULL);
41 curr = pptr + (offset & (PAGE_CACHE_BITS - 1)) / 32;
43 offset &= ~(PAGE_CACHE_BITS - 1);
44 if ((size ^ offset) / PAGE_CACHE_BITS)
45 end = pptr + PAGE_CACHE_BITS / 32;
47 end = pptr + ((size + 31) & (PAGE_CACHE_BITS - 1)) / 32;
49 /* scan the first partial u32 for zero bits */
53 mask = (1U << 31) >> i;
54 for (; i < 32; mask >>= 1, i++) {
61 /* scan complete u32s for the first zero bit */
68 for (i = 0; i < 32; mask >>= 1, i++) {
76 offset += PAGE_CACHE_BITS;
79 page = read_mapping_page(mapping, offset / PAGE_CACHE_BITS,
85 curr = pptr = kmap(page);
86 if ((size ^ offset) / PAGE_CACHE_BITS)
87 end = pptr + PAGE_CACHE_BITS / 32;
89 end = pptr + ((size + 31) & (PAGE_CACHE_BITS - 1)) / 32;
91 dprint(DBG_BITMAP, "bitmap full\n");
96 start = offset + (curr - pptr) * 32 + i;
98 dprint(DBG_BITMAP, "bitmap full\n");
101 /* do any partial u32 at the start */
102 len = min(size - start, len);
108 if (!--len || n & mask)
113 *curr++ = cpu_to_be32(n);
117 n = be32_to_cpu(*curr);
124 *curr++ = cpu_to_be32(0xffffffff);
127 set_page_dirty(page);
129 offset += PAGE_CACHE_BITS;
130 page = read_mapping_page(mapping, offset / PAGE_CACHE_BITS,
138 end = pptr + PAGE_CACHE_BITS / 32;
141 /* do any partial u32 at end */
143 for (i = 0; i < len; i++) {
150 *curr = cpu_to_be32(n);
151 set_page_dirty(page);
153 *max = offset + (curr - pptr) * 32 + i - start;
154 sbi->free_blocks -= *max;
156 dprint(DBG_BITMAP, "-> %u,%u\n", start, *max);
158 mutex_unlock(&sbi->alloc_mutex);
162 int hfsplus_block_free(struct super_block *sb, u32 offset, u32 count)
164 struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
166 struct address_space *mapping;
167 __be32 *pptr, *curr, *end;
171 /* is there any actual work to be done? */
175 dprint(DBG_BITMAP, "block_free: %u,%u\n", offset, count);
176 /* are all of the bits in range? */
177 if ((offset + count) > sbi->total_blocks)
180 mutex_lock(&sbi->alloc_mutex);
181 mapping = sbi->alloc_file->i_mapping;
182 pnr = offset / PAGE_CACHE_BITS;
183 page = read_mapping_page(mapping, pnr, NULL);
185 curr = pptr + (offset & (PAGE_CACHE_BITS - 1)) / 32;
186 end = pptr + PAGE_CACHE_BITS / 32;
189 /* do any partial u32 at the start */
193 mask = 0xffffffffU << j;
195 mask |= 0xffffffffU >> (i + count);
196 *curr++ &= cpu_to_be32(mask);
199 *curr++ &= cpu_to_be32(mask);
213 set_page_dirty(page);
215 page = read_mapping_page(mapping, ++pnr, NULL);
218 end = pptr + PAGE_CACHE_BITS / 32;
221 /* do any partial u32 at end */
223 mask = 0xffffffffU >> count;
224 *curr &= cpu_to_be32(mask);
227 set_page_dirty(page);
229 sbi->free_blocks += len;
231 mutex_unlock(&sbi->alloc_mutex);