2 * linux/fs/hfsplus/extents.c
5 * Brad Boyer (flar@allandria.com)
6 * (C) 2003 Ardis Technologies <roman@ardistech.com>
8 * Handling of Extents both in catalog and extents overflow trees
11 #include <linux/errno.h>
13 #include <linux/pagemap.h>
14 #include <linux/version.h>
15 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
16 #include <linux/buffer_head.h>
19 #include "hfsplus_fs.h"
20 #include "hfsplus_raw.h"
22 /* Compare two extents keys, returns 0 on same, pos/neg for difference */
23 int hfsplus_cmp_ext_key(hfsplus_btree_key *k1, hfsplus_btree_key *k2)
31 return be32_to_cpu(k1id) < be32_to_cpu(k2id) ? -1 : 1;
33 if (k1->ext.fork_type != k2->ext.fork_type)
34 return k1->ext.fork_type < k2->ext.fork_type ? -1 : 1;
36 k1s = k1->ext.start_block;
37 k2s = k2->ext.start_block;
40 return be32_to_cpu(k1s) < be32_to_cpu(k2s) ? -1 : 1;
43 void hfsplus_fill_ext_key(hfsplus_btree_key *key, u32 cnid,
46 key->key_len = cpu_to_be16(HFSPLUS_EXT_KEYLEN - 2);
47 key->ext.cnid = cpu_to_be32(cnid);
48 key->ext.start_block = cpu_to_be32(block);
49 key->ext.fork_type = type;
53 static u32 hfsplus_find_extent(hfsplus_extent *extent, u32 off)
58 for (i = 0; i < 8; extent++, i++) {
59 count = be32_to_cpu(extent->block_count);
61 return be32_to_cpu(extent->start_block) + off;
68 static int hfsplus_find_extentry(struct hfsplus_find_data *fd,
69 hfsplus_extent *extent)
73 fd->key->ext.cnid = 0;
74 res = hfsplus_btree_find(fd);
75 if (res && res != -ENOENT)
77 if (fd->key->ext.cnid != fd->search_key->ext.cnid ||
78 fd->key->ext.fork_type != fd->search_key->ext.fork_type)
80 //if (fd->entrylength != ext_key_size)...
81 hfsplus_bnode_readbytes(fd->bnode, extent, fd->entryoffset, fd->entrylength);
85 /* Get a block at iblock for inode, possibly allocating if create */
86 int hfsplus_get_block(struct inode *inode, sector_t iblock,
87 struct buffer_head *bh_result, int create)
89 struct super_block *sb;
90 hfsplus_extent_rec ext_entry;
91 struct hfsplus_find_data fd;
93 u32 ablock, dblock = 0;
97 /* Convert inode block to disk allocation block */
100 if (ablock >= HFSPLUS_I(inode).total_blocks) {
101 if (ablock > HFSPLUS_I(inode).total_blocks || !create)
103 if (ablock >= HFSPLUS_I(inode).alloc_blocks) {
104 err = hfsplus_extend_file(inode);
108 HFSPLUS_I(inode).mmu_private += sb->s_blocksize;
109 HFSPLUS_I(inode).total_blocks++;
110 mark_inode_dirty(inode);
114 if (ablock < HFSPLUS_I(inode).extent_blocks) {
115 dblock = hfsplus_find_extent(HFSPLUS_I(inode).extents, ablock);
117 hfsplus_find_init(HFSPLUS_SB(sb).ext_tree, &fd);
118 hfsplus_fill_ext_key(fd.search_key, inode->i_ino, ablock, HFSPLUS_IS_RSRC(inode) ?
119 HFSPLUS_TYPE_RSRC : HFSPLUS_TYPE_DATA);
120 err = hfsplus_find_extentry(&fd, ext_entry);
122 dblock = hfsplus_find_extent(ext_entry, ablock -
123 be32_to_cpu(fd.key->ext.start_block));
124 hfsplus_find_exit(&fd);
131 dprint(DBG_EXTENT, "get_block(%lu): %lu - %u\n", inode->i_ino, iblock, dblock);
133 map_bh(bh_result, sb, dblock + HFSPLUS_SB(sb).blockoffset);
135 set_buffer_new(bh_result);
139 static void hfsplus_dump_extent(hfsplus_extent *extent)
143 dprint(DBG_EXTENT, " ");
144 for (i = 0; i < 8; i++)
145 dprint(DBG_EXTENT, " %u:%u", be32_to_cpu(extent[i].start_block),
146 be32_to_cpu(extent[i].block_count));
147 dprint(DBG_EXTENT, "\n");
150 static int hfsplus_add_extent(hfsplus_extent *extent, u32 offset,
151 u32 alloc_block, u32 block_count)
156 hfsplus_dump_extent(extent);
157 for (i = 0; i < 8; extent++, i++) {
158 count = be32_to_cpu(extent->block_count);
159 if (offset == count) {
160 start = be32_to_cpu(extent->start_block);
161 if (alloc_block != start + count) {
165 extent->start_block = cpu_to_be32(alloc_block);
167 block_count += count;
168 extent->block_count = cpu_to_be32(block_count);
170 } else if (offset < count)
178 void hfsplus_free_blocks(struct super_block *sb, u32 start, u32 count)
181 struct buffer_head *bh;
183 unsigned long *data, word, m;
185 anode = HFSPLUS_SB(sb).alloc_file;
186 size = sb->s_blocksize / sizeof(unsigned long);
187 blk = (start >> sb->s_blocksize_bits) / 8;
188 off = (start & (sb->s_blocksize * 8 - 1)) / 8 / sizeof(unsigned long);
189 m = 1 << (~start & (8 * sizeof(unsigned long) - 1));
191 HFSPLUS_SB(sb).free_blocks += count;
195 bh = hfsplus_getblk(anode, blk);
196 data = (unsigned long *)bh->b_data;
198 word = ~be32_to_cpu(data[off]);
201 printk("freeing free block %u\n", start);
205 data[off] = cpu_to_be32(~word);
211 data[off] = cpu_to_be32(~word);
212 m = 1UL << (8 * sizeof(unsigned long) - 1);
213 } while (++off < size);
215 mark_buffer_dirty_inode(bh, anode);
217 if (++blk >= anode->i_blocks)
222 printk("%u block left to free\n", count);
225 int hfsplus_free_extents(struct super_block *sb, hfsplus_extent *extent,
226 u32 offset, u32 block_nr)
231 hfsplus_dump_extent(extent);
232 for (i = 0; i < 8; extent++, i++) {
233 count = be32_to_cpu(extent->block_count);
236 else if (offset < count)
244 start = be32_to_cpu(extent->start_block);
245 if (count <= block_nr) {
246 hfsplus_free_blocks(sb, start, count);
247 extent->block_count = 0;
248 extent->start_block = 0;
252 hfsplus_free_blocks(sb, start + count, block_nr);
253 extent->block_count = cpu_to_be32(count);
260 count = be32_to_cpu(extent->block_count);
264 int hfsplus_free_fork(struct super_block *sb, u32 cnid, hfsplus_fork_raw *fork, int type)
266 struct hfsplus_find_data fd;
267 hfsplus_extent_rec ext_entry;
268 u32 total_blocks, blocks, start;
271 total_blocks = be32_to_cpu(fork->total_blocks);
276 for (i = 0; i < 8; i++)
277 blocks += be32_to_cpu(fork->extents[i].block_count);
279 res = hfsplus_free_extents(sb, fork->extents, blocks, blocks);
282 if (total_blocks == blocks)
285 hfsplus_find_init(HFSPLUS_SB(sb).ext_tree, &fd);
287 hfsplus_fill_ext_key(fd.search_key, cnid,
289 res = hfsplus_find_extentry(&fd, ext_entry);
292 start = be32_to_cpu(fd.key->ext.start_block);
293 hfsplus_free_extents(sb, ext_entry,
294 total_blocks - start,
296 hfsplus_bnode_remove_rec(&fd);
297 total_blocks = start;
298 } while (total_blocks > blocks);
299 hfsplus_find_exit(&fd);
304 int hfsplus_extend_file(struct inode *inode)
306 struct super_block *sb = inode->i_sb;
307 struct inode *anode = HFSPLUS_SB(sb).alloc_file;
308 struct hfsplus_find_data fd;
309 hfsplus_extent_rec ext_entry;
310 struct buffer_head *bh;
311 u32 blk, blk1, ablock, block_count;
312 unsigned long *data, word, m;
313 int res, size, off, i;
315 if (anode->i_size * 8 < HFSPLUS_SB(sb).total_blocks - HFSPLUS_SB(sb).free_blocks + 8) {
319 blk = blk1 = (HFSPLUS_SB(sb).next_alloc >> sb->s_blocksize_bits) / 8;
320 off = (HFSPLUS_SB(sb).next_alloc & (sb->s_blocksize * 8 - 1)) / sizeof(unsigned long) / 8;
323 // ignore first off for now...
325 bh = hfsplus_getblk(anode, blk);
326 if (blk == anode->i_blocks - 1)
327 size = (HFSPLUS_SB(sb).total_blocks &
328 (sb->s_blocksize * 8 - 1)) /
329 8 / sizeof(unsigned long);
331 size = sb->s_blocksize / sizeof(unsigned long);
332 data = (unsigned long *)bh->b_data;
334 word = be32_to_cpu(data[off]);
337 m = 1UL << (sizeof(unsigned long) * 8 - 1);
338 for (i = 0; m; i++, m >>= 1) {
341 ablock = (blk << sb->s_blocksize_bits) * 8 +
342 off * sizeof(unsigned long) * 8 + i;
346 while ((m >>= 1) && !(word & m)) {
351 data[off] = cpu_to_be32(word);
352 mark_buffer_dirty_inode(bh, anode);
356 } while (++off < size);
358 if (++blk >= anode->i_blocks)
365 dprint(DBG_EXTENT, "extend %lu: %u,%u\n", inode->i_ino, ablock, block_count);
366 if (HFSPLUS_I(inode).alloc_blocks <= HFSPLUS_I(inode).extent_blocks) {
367 if (!HFSPLUS_I(inode).extent_blocks) {
368 dprint(DBG_EXTENT, "first extents\n");
370 HFSPLUS_I(inode).extents[0].start_block = cpu_to_be32(ablock);
371 HFSPLUS_I(inode).extents[0].block_count = cpu_to_be32(block_count);
374 /* try to append to extents in inode */
375 res = hfsplus_add_extent(HFSPLUS_I(inode).extents,
376 HFSPLUS_I(inode).alloc_blocks,
377 ablock, block_count);
379 hfsplus_dump_extent(HFSPLUS_I(inode).extents);
380 HFSPLUS_I(inode).extent_blocks += block_count;
381 } else if (res == -ENOSPC) {
382 /* create new extent, so find place to insert it */
383 hfsplus_find_init(HFSPLUS_SB(sb).ext_tree, &fd);
384 hfsplus_fill_ext_key(fd.search_key, inode->i_ino,
385 HFSPLUS_I(inode).alloc_blocks,
386 HFSPLUS_IS_RSRC(inode) ?
387 HFSPLUS_TYPE_RSRC : HFSPLUS_TYPE_DATA);
388 res = hfsplus_find_extentry(&fd, ext_entry);
389 if (res && res != -ENOENT) {
390 hfsplus_find_exit(&fd);
396 hfsplus_find_init(HFSPLUS_SB(sb).ext_tree, &fd);
397 hfsplus_fill_ext_key(fd.search_key, inode->i_ino,
398 HFSPLUS_I(inode).alloc_blocks,
399 HFSPLUS_IS_RSRC(inode) ?
400 HFSPLUS_TYPE_RSRC : HFSPLUS_TYPE_DATA);
401 res = hfsplus_find_extentry(&fd, ext_entry);
403 hfsplus_find_exit(&fd);
407 res = hfsplus_add_extent(ext_entry, HFSPLUS_I(inode).alloc_blocks -
408 be32_to_cpu(fd.key->ext.start_block),
409 ablock, block_count);
411 hfsplus_dump_extent(ext_entry);
412 hfsplus_bnode_writebytes(fd.bnode, &ext_entry,
415 } else if (res == -ENOSPC)
417 hfsplus_find_exit(&fd);
421 HFSPLUS_I(inode).alloc_blocks += block_count;
422 mark_inode_dirty(inode);
423 HFSPLUS_SB(sb).free_blocks -= block_count;
429 dprint(DBG_EXTENT, "insert new extent\n");
430 memset(ext_entry, 0, sizeof(ext_entry));
431 ext_entry[0].start_block = cpu_to_be32(ablock);
432 ext_entry[0].block_count = cpu_to_be32(block_count);
434 hfsplus_bnode_insert_rec(&fd, ext_entry, sizeof(ext_entry));
435 hfsplus_find_exit(&fd);
440 void hfsplus_truncate(struct inode *inode)
442 struct super_block *sb = inode->i_sb;
443 struct hfsplus_find_data fd;
444 hfsplus_extent_rec ext_entry;
448 dprint(DBG_INODE, "truncate: %lu, %Lu -> %Lu\n", inode->i_ino,
449 (long long)HFSPLUS_I(inode).mmu_private, inode->i_size);
450 if (inode->i_size == HFSPLUS_I(inode).mmu_private)
452 if (inode->i_size > HFSPLUS_I(inode).mmu_private) {
453 struct address_space *mapping = inode->i_mapping;
455 u32 size = inode->i_size - 1;
458 page = grab_cache_page(mapping, size >> PAGE_CACHE_SHIFT);
461 size &= PAGE_CACHE_SIZE - 1;
463 res = mapping->a_ops->prepare_write(NULL, page, size, size);
465 res = mapping->a_ops->commit_write(NULL, page, size, size);
467 inode->i_size = HFSPLUS_I(inode).mmu_private;
469 page_cache_release(page);
470 mark_inode_dirty(inode);
473 blk_cnt = (inode->i_size + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
476 if (HFSPLUS_I(inode).alloc_blocks <= HFSPLUS_I(inode).extent_blocks) {
477 hfsplus_free_extents(sb, HFSPLUS_I(inode).extents,
478 HFSPLUS_I(inode).alloc_blocks,
479 HFSPLUS_I(inode).alloc_blocks - blk_cnt);
480 hfsplus_dump_extent(HFSPLUS_I(inode).extents);
481 HFSPLUS_I(inode).extent_blocks = blk_cnt;
484 hfsplus_find_init(HFSPLUS_SB(sb).ext_tree, &fd);
485 hfsplus_fill_ext_key(fd.search_key, inode->i_ino,
486 HFSPLUS_I(inode).alloc_blocks,
487 HFSPLUS_IS_RSRC(inode) ?
488 HFSPLUS_TYPE_RSRC : HFSPLUS_TYPE_DATA);
489 res = hfsplus_find_extentry(&fd, ext_entry);
491 hfsplus_find_exit(&fd);
494 start = be32_to_cpu(fd.key->ext.start_block);
495 hfsplus_free_extents(sb, ext_entry,
496 HFSPLUS_I(inode).alloc_blocks - start,
497 HFSPLUS_I(inode).alloc_blocks - blk_cnt);
498 hfsplus_dump_extent(ext_entry);
499 if (blk_cnt > start) {
500 hfsplus_bnode_writebytes(fd.bnode, &ext_entry,
503 hfsplus_find_exit(&fd);
506 HFSPLUS_I(inode).alloc_blocks = start;
507 hfsplus_bnode_remove_rec(&fd);
508 hfsplus_find_exit(&fd);
510 HFSPLUS_I(inode).mmu_private = inode->i_size;
511 HFSPLUS_I(inode).alloc_blocks = HFSPLUS_I(inode).total_blocks = blk_cnt;