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>
16 #include "hfsplus_fs.h"
17 #include "hfsplus_raw.h"
19 /* Compare two extents keys, returns 0 on same, pos/neg for difference */
20 int hfsplus_ext_cmp_key(hfsplus_btree_key
*k1
, hfsplus_btree_key
*k2
)
28 return be32_to_cpu(k1id
) < be32_to_cpu(k2id
) ? -1 : 1;
30 if (k1
->ext
.fork_type
!= k2
->ext
.fork_type
)
31 return k1
->ext
.fork_type
< k2
->ext
.fork_type
? -1 : 1;
33 k1s
= k1
->ext
.start_block
;
34 k2s
= k2
->ext
.start_block
;
37 return be32_to_cpu(k1s
) < be32_to_cpu(k2s
) ? -1 : 1;
40 void hfsplus_ext_build_key(hfsplus_btree_key
*key
, u32 cnid
,
43 key
->key_len
= cpu_to_be16(HFSPLUS_EXT_KEYLEN
- 2);
44 key
->ext
.cnid
= cpu_to_be32(cnid
);
45 key
->ext
.start_block
= cpu_to_be32(block
);
46 key
->ext
.fork_type
= type
;
50 static u32
hfsplus_ext_find_block(struct hfsplus_extent
*ext
, u32 off
)
55 for (i
= 0; i
< 8; ext
++, i
++) {
56 count
= be32_to_cpu(ext
->block_count
);
58 return be32_to_cpu(ext
->start_block
) + off
;
65 static int hfsplus_ext_block_count(struct hfsplus_extent
*ext
)
70 for (i
= 0; i
< 8; ext
++, i
++)
71 count
+= be32_to_cpu(ext
->block_count
);
75 static u32
hfsplus_ext_lastblock(struct hfsplus_extent
*ext
)
80 for (i
= 0; i
< 7; ext
--, i
++)
83 return be32_to_cpu(ext
->start_block
) + be32_to_cpu(ext
->block_count
);
86 static void __hfsplus_ext_write_extent(struct inode
*inode
, struct hfs_find_data
*fd
)
90 hfsplus_ext_build_key(fd
->search_key
, inode
->i_ino
, HFSPLUS_I(inode
).cached_start
,
91 HFSPLUS_IS_RSRC(inode
) ? HFSPLUS_TYPE_RSRC
: HFSPLUS_TYPE_DATA
);
92 res
= hfs_brec_find(fd
);
93 if (HFSPLUS_I(inode
).flags
& HFSPLUS_FLG_EXT_NEW
) {
96 hfs_brec_insert(fd
, HFSPLUS_I(inode
).cached_extents
, sizeof(hfsplus_extent_rec
));
97 HFSPLUS_I(inode
).flags
&= ~(HFSPLUS_FLG_EXT_DIRTY
| HFSPLUS_FLG_EXT_NEW
);
101 hfs_bnode_write(fd
->bnode
, HFSPLUS_I(inode
).cached_extents
, fd
->entryoffset
, fd
->entrylength
);
102 HFSPLUS_I(inode
).flags
&= ~HFSPLUS_FLG_EXT_DIRTY
;
106 void hfsplus_ext_write_extent(struct inode
*inode
)
108 if (HFSPLUS_I(inode
).flags
& HFSPLUS_FLG_EXT_DIRTY
) {
109 struct hfs_find_data fd
;
111 hfs_find_init(HFSPLUS_SB(inode
->i_sb
).ext_tree
, &fd
);
112 __hfsplus_ext_write_extent(inode
, &fd
);
117 static inline int __hfsplus_ext_read_extent(struct hfs_find_data
*fd
,
118 struct hfsplus_extent
*extent
,
119 u32 cnid
, u32 block
, u8 type
)
123 hfsplus_ext_build_key(fd
->search_key
, cnid
, block
, type
);
124 fd
->key
->ext
.cnid
= 0;
125 res
= hfs_brec_find(fd
);
126 if (res
&& res
!= -ENOENT
)
128 if (fd
->key
->ext
.cnid
!= fd
->search_key
->ext
.cnid
||
129 fd
->key
->ext
.fork_type
!= fd
->search_key
->ext
.fork_type
)
131 if (fd
->entrylength
!= sizeof(hfsplus_extent_rec
))
133 hfs_bnode_read(fd
->bnode
, extent
, fd
->entryoffset
, sizeof(hfsplus_extent_rec
));
137 static inline int __hfsplus_ext_cache_extent(struct hfs_find_data
*fd
, struct inode
*inode
, u32 block
)
141 if (HFSPLUS_I(inode
).flags
& HFSPLUS_FLG_EXT_DIRTY
)
142 __hfsplus_ext_write_extent(inode
, fd
);
144 res
= __hfsplus_ext_read_extent(fd
, HFSPLUS_I(inode
).cached_extents
, inode
->i_ino
,
145 block
, HFSPLUS_IS_RSRC(inode
) ? HFSPLUS_TYPE_RSRC
: HFSPLUS_TYPE_DATA
);
147 HFSPLUS_I(inode
).cached_start
= be32_to_cpu(fd
->key
->ext
.start_block
);
148 HFSPLUS_I(inode
).cached_blocks
= hfsplus_ext_block_count(HFSPLUS_I(inode
).cached_extents
);
150 HFSPLUS_I(inode
).cached_start
= HFSPLUS_I(inode
).cached_blocks
= 0;
151 HFSPLUS_I(inode
).flags
&= ~(HFSPLUS_FLG_EXT_DIRTY
| HFSPLUS_FLG_EXT_NEW
);
156 static int hfsplus_ext_read_extent(struct inode
*inode
, u32 block
)
158 struct hfs_find_data fd
;
161 if (block
>= HFSPLUS_I(inode
).cached_start
&&
162 block
< HFSPLUS_I(inode
).cached_start
+ HFSPLUS_I(inode
).cached_blocks
)
165 hfs_find_init(HFSPLUS_SB(inode
->i_sb
).ext_tree
, &fd
);
166 res
= __hfsplus_ext_cache_extent(&fd
, inode
, block
);
171 /* Get a block at iblock for inode, possibly allocating if create */
172 int hfsplus_get_block(struct inode
*inode
, sector_t iblock
,
173 struct buffer_head
*bh_result
, int create
)
175 struct super_block
*sb
;
177 u32 ablock
, dblock
, mask
;
182 /* Convert inode block to disk allocation block */
183 shift
= HFSPLUS_SB(sb
).alloc_blksz_shift
- sb
->s_blocksize_bits
;
184 ablock
= iblock
>> HFSPLUS_SB(sb
).fs_shift
;
186 if (iblock
>= inode
->i_blocks
) {
187 if (iblock
> inode
->i_blocks
|| !create
)
189 if (ablock
>= HFSPLUS_I(inode
).alloc_blocks
) {
190 res
= hfsplus_file_extend(inode
);
197 if (ablock
< HFSPLUS_I(inode
).first_blocks
) {
198 dblock
= hfsplus_ext_find_block(HFSPLUS_I(inode
).first_extents
, ablock
);
202 down(&HFSPLUS_I(inode
).extents_lock
);
203 res
= hfsplus_ext_read_extent(inode
, ablock
);
205 dblock
= hfsplus_ext_find_block(HFSPLUS_I(inode
).cached_extents
, ablock
-
206 HFSPLUS_I(inode
).cached_start
);
208 up(&HFSPLUS_I(inode
).extents_lock
);
211 up(&HFSPLUS_I(inode
).extents_lock
);
214 dprint(DBG_EXTENT
, "get_block(%lu): %llu - %u\n", inode
->i_ino
, (long long)iblock
, dblock
);
215 mask
= (1 << HFSPLUS_SB(sb
).fs_shift
) - 1;
216 map_bh(bh_result
, sb
, (dblock
<< HFSPLUS_SB(sb
).fs_shift
) + HFSPLUS_SB(sb
).blockoffset
+ (iblock
& mask
));
218 set_buffer_new(bh_result
);
219 HFSPLUS_I(inode
).phys_size
+= sb
->s_blocksize
;
221 mark_inode_dirty(inode
);
226 static void hfsplus_dump_extent(struct hfsplus_extent
*extent
)
230 dprint(DBG_EXTENT
, " ");
231 for (i
= 0; i
< 8; i
++)
232 dprint(DBG_EXTENT
, " %u:%u", be32_to_cpu(extent
[i
].start_block
),
233 be32_to_cpu(extent
[i
].block_count
));
234 dprint(DBG_EXTENT
, "\n");
237 static int hfsplus_add_extent(struct hfsplus_extent
*extent
, u32 offset
,
238 u32 alloc_block
, u32 block_count
)
243 hfsplus_dump_extent(extent
);
244 for (i
= 0; i
< 8; extent
++, i
++) {
245 count
= be32_to_cpu(extent
->block_count
);
246 if (offset
== count
) {
247 start
= be32_to_cpu(extent
->start_block
);
248 if (alloc_block
!= start
+ count
) {
252 extent
->start_block
= cpu_to_be32(alloc_block
);
254 block_count
+= count
;
255 extent
->block_count
= cpu_to_be32(block_count
);
257 } else if (offset
< count
)
265 int hfsplus_free_extents(struct super_block
*sb
, struct hfsplus_extent
*extent
,
266 u32 offset
, u32 block_nr
)
271 hfsplus_dump_extent(extent
);
272 for (i
= 0; i
< 8; extent
++, i
++) {
273 count
= be32_to_cpu(extent
->block_count
);
276 else if (offset
< count
)
284 start
= be32_to_cpu(extent
->start_block
);
285 if (count
<= block_nr
) {
286 hfsplus_block_free(sb
, start
, count
);
287 extent
->block_count
= 0;
288 extent
->start_block
= 0;
292 hfsplus_block_free(sb
, start
+ count
, block_nr
);
293 extent
->block_count
= cpu_to_be32(count
);
300 count
= be32_to_cpu(extent
->block_count
);
304 int hfsplus_free_fork(struct super_block
*sb
, u32 cnid
, struct hfsplus_fork_raw
*fork
, int type
)
306 struct hfs_find_data fd
;
307 hfsplus_extent_rec ext_entry
;
308 u32 total_blocks
, blocks
, start
;
311 total_blocks
= be32_to_cpu(fork
->total_blocks
);
316 for (i
= 0; i
< 8; i
++)
317 blocks
+= be32_to_cpu(fork
->extents
[i
].block_count
);
319 res
= hfsplus_free_extents(sb
, fork
->extents
, blocks
, blocks
);
322 if (total_blocks
== blocks
)
325 hfs_find_init(HFSPLUS_SB(sb
).ext_tree
, &fd
);
327 res
= __hfsplus_ext_read_extent(&fd
, ext_entry
, cnid
,
331 start
= be32_to_cpu(fd
.key
->ext
.start_block
);
332 hfsplus_free_extents(sb
, ext_entry
,
333 total_blocks
- start
,
335 hfs_brec_remove(&fd
);
336 total_blocks
= start
;
337 } while (total_blocks
> blocks
);
343 int hfsplus_file_extend(struct inode
*inode
)
345 struct super_block
*sb
= inode
->i_sb
;
346 u32 start
, len
, goal
;
349 if (HFSPLUS_SB(sb
).alloc_file
->i_size
* 8 < HFSPLUS_SB(sb
).total_blocks
- HFSPLUS_SB(sb
).free_blocks
+ 8) {
351 printk("extend alloc file! (%Lu,%u,%u)\n", HFSPLUS_SB(sb
).alloc_file
->i_size
* 8,
352 HFSPLUS_SB(sb
).total_blocks
, HFSPLUS_SB(sb
).free_blocks
);
357 down(&HFSPLUS_I(inode
).extents_lock
);
358 if (HFSPLUS_I(inode
).alloc_blocks
== HFSPLUS_I(inode
).first_blocks
)
359 goal
= hfsplus_ext_lastblock(HFSPLUS_I(inode
).first_extents
);
361 res
= hfsplus_ext_read_extent(inode
, HFSPLUS_I(inode
).alloc_blocks
);
364 goal
= hfsplus_ext_lastblock(HFSPLUS_I(inode
).cached_extents
);
367 len
= HFSPLUS_I(inode
).clump_blocks
;
368 start
= hfsplus_block_allocate(sb
, HFSPLUS_SB(sb
).total_blocks
, goal
, &len
);
369 if (start
>= HFSPLUS_SB(sb
).total_blocks
) {
370 start
= hfsplus_block_allocate(sb
, goal
, 0, &len
);
377 dprint(DBG_EXTENT
, "extend %lu: %u,%u\n", inode
->i_ino
, start
, len
);
378 if (HFSPLUS_I(inode
).alloc_blocks
<= HFSPLUS_I(inode
).first_blocks
) {
379 if (!HFSPLUS_I(inode
).first_blocks
) {
380 dprint(DBG_EXTENT
, "first extents\n");
382 HFSPLUS_I(inode
).first_extents
[0].start_block
= cpu_to_be32(start
);
383 HFSPLUS_I(inode
).first_extents
[0].block_count
= cpu_to_be32(len
);
386 /* try to append to extents in inode */
387 res
= hfsplus_add_extent(HFSPLUS_I(inode
).first_extents
,
388 HFSPLUS_I(inode
).alloc_blocks
,
394 hfsplus_dump_extent(HFSPLUS_I(inode
).first_extents
);
395 HFSPLUS_I(inode
).first_blocks
+= len
;
398 res
= hfsplus_add_extent(HFSPLUS_I(inode
).cached_extents
,
399 HFSPLUS_I(inode
).alloc_blocks
-
400 HFSPLUS_I(inode
).cached_start
,
403 hfsplus_dump_extent(HFSPLUS_I(inode
).cached_extents
);
404 HFSPLUS_I(inode
).flags
|= HFSPLUS_FLG_EXT_DIRTY
;
405 HFSPLUS_I(inode
).cached_blocks
+= len
;
406 } else if (res
== -ENOSPC
)
410 up(&HFSPLUS_I(inode
).extents_lock
);
412 HFSPLUS_I(inode
).alloc_blocks
+= len
;
413 mark_inode_dirty(inode
);
418 dprint(DBG_EXTENT
, "insert new extent\n");
419 hfsplus_ext_write_extent(inode
);
421 memset(HFSPLUS_I(inode
).cached_extents
, 0, sizeof(hfsplus_extent_rec
));
422 HFSPLUS_I(inode
).cached_extents
[0].start_block
= cpu_to_be32(start
);
423 HFSPLUS_I(inode
).cached_extents
[0].block_count
= cpu_to_be32(len
);
424 hfsplus_dump_extent(HFSPLUS_I(inode
).cached_extents
);
425 HFSPLUS_I(inode
).flags
|= HFSPLUS_FLG_EXT_DIRTY
| HFSPLUS_FLG_EXT_NEW
;
426 HFSPLUS_I(inode
).cached_start
= HFSPLUS_I(inode
).alloc_blocks
;
427 HFSPLUS_I(inode
).cached_blocks
= len
;
433 void hfsplus_file_truncate(struct inode
*inode
)
435 struct super_block
*sb
= inode
->i_sb
;
436 struct hfs_find_data fd
;
437 u32 alloc_cnt
, blk_cnt
, start
;
440 dprint(DBG_INODE
, "truncate: %lu, %Lu -> %Lu\n", inode
->i_ino
,
441 (long long)HFSPLUS_I(inode
).phys_size
, inode
->i_size
);
442 if (inode
->i_size
> HFSPLUS_I(inode
).phys_size
) {
443 struct address_space
*mapping
= inode
->i_mapping
;
445 u32 size
= inode
->i_size
- 1;
448 page
= grab_cache_page(mapping
, size
>> PAGE_CACHE_SHIFT
);
451 size
&= PAGE_CACHE_SIZE
- 1;
453 res
= mapping
->a_ops
->prepare_write(NULL
, page
, size
, size
);
455 res
= mapping
->a_ops
->commit_write(NULL
, page
, size
, size
);
457 inode
->i_size
= HFSPLUS_I(inode
).phys_size
;
459 page_cache_release(page
);
460 mark_inode_dirty(inode
);
463 blk_cnt
= (inode
->i_size
+ HFSPLUS_SB(sb
).alloc_blksz
- 1) >> HFSPLUS_SB(sb
).alloc_blksz_shift
;
464 alloc_cnt
= HFSPLUS_I(inode
).alloc_blocks
;
465 if (blk_cnt
== alloc_cnt
)
468 down(&HFSPLUS_I(inode
).extents_lock
);
469 hfs_find_init(HFSPLUS_SB(sb
).ext_tree
, &fd
);
471 if (alloc_cnt
== HFSPLUS_I(inode
).first_blocks
) {
472 hfsplus_free_extents(sb
, HFSPLUS_I(inode
).first_extents
,
473 alloc_cnt
, alloc_cnt
- blk_cnt
);
474 hfsplus_dump_extent(HFSPLUS_I(inode
).first_extents
);
475 HFSPLUS_I(inode
).first_blocks
= blk_cnt
;
478 res
= __hfsplus_ext_cache_extent(&fd
, inode
, alloc_cnt
);
481 start
= HFSPLUS_I(inode
).cached_start
;
482 hfsplus_free_extents(sb
, HFSPLUS_I(inode
).cached_extents
,
483 alloc_cnt
- start
, alloc_cnt
- blk_cnt
);
484 hfsplus_dump_extent(HFSPLUS_I(inode
).cached_extents
);
485 if (blk_cnt
> start
) {
486 HFSPLUS_I(inode
).flags
|= HFSPLUS_FLG_EXT_DIRTY
;
490 HFSPLUS_I(inode
).cached_start
= HFSPLUS_I(inode
).cached_blocks
= 0;
491 HFSPLUS_I(inode
).flags
&= ~(HFSPLUS_FLG_EXT_DIRTY
| HFSPLUS_FLG_EXT_NEW
);
492 hfs_brec_remove(&fd
);
495 up(&HFSPLUS_I(inode
).extents_lock
);
497 HFSPLUS_I(inode
).alloc_blocks
= blk_cnt
;
499 HFSPLUS_I(inode
).phys_size
= inode
->i_size
;
500 mark_inode_dirty(inode
);
501 inode
->i_blocks
= (inode
->i_size
+ sb
->s_blocksize
- 1) >> sb
->s_blocksize_bits
;