aboutsummaryrefslogtreecommitdiffstats
path: root/fs/hfsplus/extents.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/hfsplus/extents.c')
-rw-r--r--fs/hfsplus/extents.c96
1 files changed, 62 insertions, 34 deletions
diff --git a/fs/hfsplus/extents.c b/fs/hfsplus/extents.c
index 0c9cb1820a52..52a0bcaa7b6d 100644
--- a/fs/hfsplus/extents.c
+++ b/fs/hfsplus/extents.c
@@ -83,7 +83,8 @@ static u32 hfsplus_ext_lastblock(struct hfsplus_extent *ext)
83 return be32_to_cpu(ext->start_block) + be32_to_cpu(ext->block_count); 83 return be32_to_cpu(ext->start_block) + be32_to_cpu(ext->block_count);
84} 84}
85 85
86static void __hfsplus_ext_write_extent(struct inode *inode, struct hfs_find_data *fd) 86static void __hfsplus_ext_write_extent(struct inode *inode,
87 struct hfs_find_data *fd)
87{ 88{
88 struct hfsplus_inode_info *hip = HFSPLUS_I(inode); 89 struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
89 int res; 90 int res;
@@ -95,24 +96,32 @@ static void __hfsplus_ext_write_extent(struct inode *inode, struct hfs_find_data
95 HFSPLUS_TYPE_RSRC : HFSPLUS_TYPE_DATA); 96 HFSPLUS_TYPE_RSRC : HFSPLUS_TYPE_DATA);
96 97
97 res = hfs_brec_find(fd); 98 res = hfs_brec_find(fd);
98 if (hip->flags & HFSPLUS_FLG_EXT_NEW) { 99 if (hip->extent_state & HFSPLUS_EXT_NEW) {
99 if (res != -ENOENT) 100 if (res != -ENOENT)
100 return; 101 return;
101 hfs_brec_insert(fd, hip->cached_extents, 102 hfs_brec_insert(fd, hip->cached_extents,
102 sizeof(hfsplus_extent_rec)); 103 sizeof(hfsplus_extent_rec));
103 hip->flags &= ~(HFSPLUS_FLG_EXT_DIRTY | HFSPLUS_FLG_EXT_NEW); 104 hip->extent_state &= ~(HFSPLUS_EXT_DIRTY | HFSPLUS_EXT_NEW);
104 } else { 105 } else {
105 if (res) 106 if (res)
106 return; 107 return;
107 hfs_bnode_write(fd->bnode, hip->cached_extents, 108 hfs_bnode_write(fd->bnode, hip->cached_extents,
108 fd->entryoffset, fd->entrylength); 109 fd->entryoffset, fd->entrylength);
109 hip->flags &= ~HFSPLUS_FLG_EXT_DIRTY; 110 hip->extent_state &= ~HFSPLUS_EXT_DIRTY;
110 } 111 }
112
113 /*
114 * We can't just use hfsplus_mark_inode_dirty here, because we
115 * also get called from hfsplus_write_inode, which should not
116 * redirty the inode. Instead the callers have to be careful
117 * to explicily mark the inode dirty, too.
118 */
119 set_bit(HFSPLUS_I_EXT_DIRTY, &hip->flags);
111} 120}
112 121
113static void hfsplus_ext_write_extent_locked(struct inode *inode) 122static void hfsplus_ext_write_extent_locked(struct inode *inode)
114{ 123{
115 if (HFSPLUS_I(inode)->flags & HFSPLUS_FLG_EXT_DIRTY) { 124 if (HFSPLUS_I(inode)->extent_state & HFSPLUS_EXT_DIRTY) {
116 struct hfs_find_data fd; 125 struct hfs_find_data fd;
117 126
118 hfs_find_init(HFSPLUS_SB(inode->i_sb)->ext_tree, &fd); 127 hfs_find_init(HFSPLUS_SB(inode->i_sb)->ext_tree, &fd);
@@ -144,18 +153,20 @@ static inline int __hfsplus_ext_read_extent(struct hfs_find_data *fd,
144 return -ENOENT; 153 return -ENOENT;
145 if (fd->entrylength != sizeof(hfsplus_extent_rec)) 154 if (fd->entrylength != sizeof(hfsplus_extent_rec))
146 return -EIO; 155 return -EIO;
147 hfs_bnode_read(fd->bnode, extent, fd->entryoffset, sizeof(hfsplus_extent_rec)); 156 hfs_bnode_read(fd->bnode, extent, fd->entryoffset,
157 sizeof(hfsplus_extent_rec));
148 return 0; 158 return 0;
149} 159}
150 160
151static inline int __hfsplus_ext_cache_extent(struct hfs_find_data *fd, struct inode *inode, u32 block) 161static inline int __hfsplus_ext_cache_extent(struct hfs_find_data *fd,
162 struct inode *inode, u32 block)
152{ 163{
153 struct hfsplus_inode_info *hip = HFSPLUS_I(inode); 164 struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
154 int res; 165 int res;
155 166
156 WARN_ON(!mutex_is_locked(&hip->extents_lock)); 167 WARN_ON(!mutex_is_locked(&hip->extents_lock));
157 168
158 if (hip->flags & HFSPLUS_FLG_EXT_DIRTY) 169 if (hip->extent_state & HFSPLUS_EXT_DIRTY)
159 __hfsplus_ext_write_extent(inode, fd); 170 __hfsplus_ext_write_extent(inode, fd);
160 171
161 res = __hfsplus_ext_read_extent(fd, hip->cached_extents, inode->i_ino, 172 res = __hfsplus_ext_read_extent(fd, hip->cached_extents, inode->i_ino,
@@ -164,10 +175,11 @@ static inline int __hfsplus_ext_cache_extent(struct hfs_find_data *fd, struct in
164 HFSPLUS_TYPE_DATA); 175 HFSPLUS_TYPE_DATA);
165 if (!res) { 176 if (!res) {
166 hip->cached_start = be32_to_cpu(fd->key->ext.start_block); 177 hip->cached_start = be32_to_cpu(fd->key->ext.start_block);
167 hip->cached_blocks = hfsplus_ext_block_count(hip->cached_extents); 178 hip->cached_blocks =
179 hfsplus_ext_block_count(hip->cached_extents);
168 } else { 180 } else {
169 hip->cached_start = hip->cached_blocks = 0; 181 hip->cached_start = hip->cached_blocks = 0;
170 hip->flags &= ~(HFSPLUS_FLG_EXT_DIRTY | HFSPLUS_FLG_EXT_NEW); 182 hip->extent_state &= ~(HFSPLUS_EXT_DIRTY | HFSPLUS_EXT_NEW);
171 } 183 }
172 return res; 184 return res;
173} 185}
@@ -197,6 +209,7 @@ int hfsplus_get_block(struct inode *inode, sector_t iblock,
197 struct hfsplus_inode_info *hip = HFSPLUS_I(inode); 209 struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
198 int res = -EIO; 210 int res = -EIO;
199 u32 ablock, dblock, mask; 211 u32 ablock, dblock, mask;
212 int was_dirty = 0;
200 int shift; 213 int shift;
201 214
202 /* Convert inode block to disk allocation block */ 215 /* Convert inode block to disk allocation block */
@@ -223,27 +236,37 @@ int hfsplus_get_block(struct inode *inode, sector_t iblock,
223 return -EIO; 236 return -EIO;
224 237
225 mutex_lock(&hip->extents_lock); 238 mutex_lock(&hip->extents_lock);
239
240 /*
241 * hfsplus_ext_read_extent will write out a cached extent into
242 * the extents btree. In that case we may have to mark the inode
243 * dirty even for a pure read of an extent here.
244 */
245 was_dirty = (hip->extent_state & HFSPLUS_EXT_DIRTY);
226 res = hfsplus_ext_read_extent(inode, ablock); 246 res = hfsplus_ext_read_extent(inode, ablock);
227 if (!res) { 247 if (res) {
228 dblock = hfsplus_ext_find_block(hip->cached_extents,
229 ablock - hip->cached_start);
230 } else {
231 mutex_unlock(&hip->extents_lock); 248 mutex_unlock(&hip->extents_lock);
232 return -EIO; 249 return -EIO;
233 } 250 }
251 dblock = hfsplus_ext_find_block(hip->cached_extents,
252 ablock - hip->cached_start);
234 mutex_unlock(&hip->extents_lock); 253 mutex_unlock(&hip->extents_lock);
235 254
236done: 255done:
237 dprint(DBG_EXTENT, "get_block(%lu): %llu - %u\n", inode->i_ino, (long long)iblock, dblock); 256 dprint(DBG_EXTENT, "get_block(%lu): %llu - %u\n",
257 inode->i_ino, (long long)iblock, dblock);
238 mask = (1 << sbi->fs_shift) - 1; 258 mask = (1 << sbi->fs_shift) - 1;
239 map_bh(bh_result, sb, (dblock << sbi->fs_shift) + sbi->blockoffset + (iblock & mask)); 259 map_bh(bh_result, sb,
260 (dblock << sbi->fs_shift) + sbi->blockoffset +
261 (iblock & mask));
240 if (create) { 262 if (create) {
241 set_buffer_new(bh_result); 263 set_buffer_new(bh_result);
242 hip->phys_size += sb->s_blocksize; 264 hip->phys_size += sb->s_blocksize;
243 hip->fs_blocks++; 265 hip->fs_blocks++;
244 inode_add_bytes(inode, sb->s_blocksize); 266 inode_add_bytes(inode, sb->s_blocksize);
245 mark_inode_dirty(inode);
246 } 267 }
268 if (create || was_dirty)
269 mark_inode_dirty(inode);
247 return 0; 270 return 0;
248} 271}
249 272
@@ -326,7 +349,8 @@ found:
326 } 349 }
327} 350}
328 351
329int hfsplus_free_fork(struct super_block *sb, u32 cnid, struct hfsplus_fork_raw *fork, int type) 352int hfsplus_free_fork(struct super_block *sb, u32 cnid,
353 struct hfsplus_fork_raw *fork, int type)
330{ 354{
331 struct hfs_find_data fd; 355 struct hfs_find_data fd;
332 hfsplus_extent_rec ext_entry; 356 hfsplus_extent_rec ext_entry;
@@ -373,12 +397,13 @@ int hfsplus_file_extend(struct inode *inode)
373 u32 start, len, goal; 397 u32 start, len, goal;
374 int res; 398 int res;
375 399
376 if (sbi->alloc_file->i_size * 8 < 400 if (sbi->total_blocks - sbi->free_blocks + 8 >
377 sbi->total_blocks - sbi->free_blocks + 8) { 401 sbi->alloc_file->i_size * 8) {
378 // extend alloc file 402 /* extend alloc file */
379 printk(KERN_ERR "hfs: extend alloc file! (%Lu,%u,%u)\n", 403 printk(KERN_ERR "hfs: extend alloc file! "
380 sbi->alloc_file->i_size * 8, 404 "(%llu,%u,%u)\n",
381 sbi->total_blocks, sbi->free_blocks); 405 sbi->alloc_file->i_size * 8,
406 sbi->total_blocks, sbi->free_blocks);
382 return -ENOSPC; 407 return -ENOSPC;
383 } 408 }
384 409
@@ -429,7 +454,7 @@ int hfsplus_file_extend(struct inode *inode)
429 start, len); 454 start, len);
430 if (!res) { 455 if (!res) {
431 hfsplus_dump_extent(hip->cached_extents); 456 hfsplus_dump_extent(hip->cached_extents);
432 hip->flags |= HFSPLUS_FLG_EXT_DIRTY; 457 hip->extent_state |= HFSPLUS_EXT_DIRTY;
433 hip->cached_blocks += len; 458 hip->cached_blocks += len;
434 } else if (res == -ENOSPC) 459 } else if (res == -ENOSPC)
435 goto insert_extent; 460 goto insert_extent;
@@ -438,7 +463,7 @@ out:
438 mutex_unlock(&hip->extents_lock); 463 mutex_unlock(&hip->extents_lock);
439 if (!res) { 464 if (!res) {
440 hip->alloc_blocks += len; 465 hip->alloc_blocks += len;
441 mark_inode_dirty(inode); 466 hfsplus_mark_inode_dirty(inode, HFSPLUS_I_ALLOC_DIRTY);
442 } 467 }
443 return res; 468 return res;
444 469
@@ -450,7 +475,7 @@ insert_extent:
450 hip->cached_extents[0].start_block = cpu_to_be32(start); 475 hip->cached_extents[0].start_block = cpu_to_be32(start);
451 hip->cached_extents[0].block_count = cpu_to_be32(len); 476 hip->cached_extents[0].block_count = cpu_to_be32(len);
452 hfsplus_dump_extent(hip->cached_extents); 477 hfsplus_dump_extent(hip->cached_extents);
453 hip->flags |= HFSPLUS_FLG_EXT_DIRTY | HFSPLUS_FLG_EXT_NEW; 478 hip->extent_state |= HFSPLUS_EXT_DIRTY | HFSPLUS_EXT_NEW;
454 hip->cached_start = hip->alloc_blocks; 479 hip->cached_start = hip->alloc_blocks;
455 hip->cached_blocks = len; 480 hip->cached_blocks = len;
456 481
@@ -466,8 +491,9 @@ void hfsplus_file_truncate(struct inode *inode)
466 u32 alloc_cnt, blk_cnt, start; 491 u32 alloc_cnt, blk_cnt, start;
467 int res; 492 int res;
468 493
469 dprint(DBG_INODE, "truncate: %lu, %Lu -> %Lu\n", 494 dprint(DBG_INODE, "truncate: %lu, %llu -> %llu\n",
470 inode->i_ino, (long long)hip->phys_size, inode->i_size); 495 inode->i_ino, (long long)hip->phys_size,
496 inode->i_size);
471 497
472 if (inode->i_size > hip->phys_size) { 498 if (inode->i_size > hip->phys_size) {
473 struct address_space *mapping = inode->i_mapping; 499 struct address_space *mapping = inode->i_mapping;
@@ -481,7 +507,8 @@ void hfsplus_file_truncate(struct inode *inode)
481 &page, &fsdata); 507 &page, &fsdata);
482 if (res) 508 if (res)
483 return; 509 return;
484 res = pagecache_write_end(NULL, mapping, size, 0, 0, page, fsdata); 510 res = pagecache_write_end(NULL, mapping, size,
511 0, 0, page, fsdata);
485 if (res < 0) 512 if (res < 0)
486 return; 513 return;
487 mark_inode_dirty(inode); 514 mark_inode_dirty(inode);
@@ -513,12 +540,12 @@ void hfsplus_file_truncate(struct inode *inode)
513 alloc_cnt - start, alloc_cnt - blk_cnt); 540 alloc_cnt - start, alloc_cnt - blk_cnt);
514 hfsplus_dump_extent(hip->cached_extents); 541 hfsplus_dump_extent(hip->cached_extents);
515 if (blk_cnt > start) { 542 if (blk_cnt > start) {
516 hip->flags |= HFSPLUS_FLG_EXT_DIRTY; 543 hip->extent_state |= HFSPLUS_EXT_DIRTY;
517 break; 544 break;
518 } 545 }
519 alloc_cnt = start; 546 alloc_cnt = start;
520 hip->cached_start = hip->cached_blocks = 0; 547 hip->cached_start = hip->cached_blocks = 0;
521 hip->flags &= ~(HFSPLUS_FLG_EXT_DIRTY | HFSPLUS_FLG_EXT_NEW); 548 hip->extent_state &= ~(HFSPLUS_EXT_DIRTY | HFSPLUS_EXT_NEW);
522 hfs_brec_remove(&fd); 549 hfs_brec_remove(&fd);
523 } 550 }
524 hfs_find_exit(&fd); 551 hfs_find_exit(&fd);
@@ -527,7 +554,8 @@ void hfsplus_file_truncate(struct inode *inode)
527 hip->alloc_blocks = blk_cnt; 554 hip->alloc_blocks = blk_cnt;
528out: 555out:
529 hip->phys_size = inode->i_size; 556 hip->phys_size = inode->i_size;
530 hip->fs_blocks = (inode->i_size + sb->s_blocksize - 1) >> sb->s_blocksize_bits; 557 hip->fs_blocks = (inode->i_size + sb->s_blocksize - 1) >>
558 sb->s_blocksize_bits;
531 inode_set_bytes(inode, hip->fs_blocks << sb->s_blocksize_bits); 559 inode_set_bytes(inode, hip->fs_blocks << sb->s_blocksize_bits);
532 mark_inode_dirty(inode); 560 hfsplus_mark_inode_dirty(inode, HFSPLUS_I_ALLOC_DIRTY);
533} 561}