Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/hfsplus/extents.c |
| 3 | * |
| 4 | * Copyright (C) 2001 |
| 5 | * Brad Boyer (flar@allandria.com) |
| 6 | * (C) 2003 Ardis Technologies <roman@ardistech.com> |
| 7 | * |
| 8 | * Handling of Extents both in catalog and extents overflow trees |
| 9 | */ |
| 10 | |
| 11 | #include <linux/errno.h> |
| 12 | #include <linux/fs.h> |
| 13 | #include <linux/pagemap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | |
| 15 | #include "hfsplus_fs.h" |
| 16 | #include "hfsplus_raw.h" |
| 17 | |
| 18 | /* Compare two extents keys, returns 0 on same, pos/neg for difference */ |
David Elliott | 2179d37 | 2006-01-18 17:43:08 -0800 | [diff] [blame] | 19 | int hfsplus_ext_cmp_key(const hfsplus_btree_key *k1, |
| 20 | const hfsplus_btree_key *k2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | { |
| 22 | __be32 k1id, k2id; |
| 23 | __be32 k1s, k2s; |
| 24 | |
| 25 | k1id = k1->ext.cnid; |
| 26 | k2id = k2->ext.cnid; |
| 27 | if (k1id != k2id) |
| 28 | return be32_to_cpu(k1id) < be32_to_cpu(k2id) ? -1 : 1; |
| 29 | |
| 30 | if (k1->ext.fork_type != k2->ext.fork_type) |
| 31 | return k1->ext.fork_type < k2->ext.fork_type ? -1 : 1; |
| 32 | |
| 33 | k1s = k1->ext.start_block; |
| 34 | k2s = k2->ext.start_block; |
| 35 | if (k1s == k2s) |
| 36 | return 0; |
| 37 | return be32_to_cpu(k1s) < be32_to_cpu(k2s) ? -1 : 1; |
| 38 | } |
| 39 | |
| 40 | static void hfsplus_ext_build_key(hfsplus_btree_key *key, u32 cnid, |
| 41 | u32 block, u8 type) |
| 42 | { |
| 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; |
| 47 | key->ext.pad = 0; |
| 48 | } |
| 49 | |
| 50 | static u32 hfsplus_ext_find_block(struct hfsplus_extent *ext, u32 off) |
| 51 | { |
| 52 | int i; |
| 53 | u32 count; |
| 54 | |
| 55 | for (i = 0; i < 8; ext++, i++) { |
| 56 | count = be32_to_cpu(ext->block_count); |
| 57 | if (off < count) |
| 58 | return be32_to_cpu(ext->start_block) + off; |
| 59 | off -= count; |
| 60 | } |
| 61 | /* panic? */ |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | static int hfsplus_ext_block_count(struct hfsplus_extent *ext) |
| 66 | { |
| 67 | int i; |
| 68 | u32 count = 0; |
| 69 | |
| 70 | for (i = 0; i < 8; ext++, i++) |
| 71 | count += be32_to_cpu(ext->block_count); |
| 72 | return count; |
| 73 | } |
| 74 | |
| 75 | static u32 hfsplus_ext_lastblock(struct hfsplus_extent *ext) |
| 76 | { |
| 77 | int i; |
| 78 | |
| 79 | ext += 7; |
| 80 | for (i = 0; i < 7; ext--, i++) |
| 81 | if (ext->block_count) |
| 82 | break; |
| 83 | return be32_to_cpu(ext->start_block) + be32_to_cpu(ext->block_count); |
| 84 | } |
| 85 | |
| 86 | static void __hfsplus_ext_write_extent(struct inode *inode, struct hfs_find_data *fd) |
| 87 | { |
| 88 | int res; |
| 89 | |
| 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) { |
| 94 | if (res != -ENOENT) |
| 95 | return; |
| 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); |
| 98 | } else { |
| 99 | if (res) |
| 100 | return; |
| 101 | hfs_bnode_write(fd->bnode, HFSPLUS_I(inode).cached_extents, fd->entryoffset, fd->entrylength); |
| 102 | HFSPLUS_I(inode).flags &= ~HFSPLUS_FLG_EXT_DIRTY; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | void hfsplus_ext_write_extent(struct inode *inode) |
| 107 | { |
| 108 | if (HFSPLUS_I(inode).flags & HFSPLUS_FLG_EXT_DIRTY) { |
| 109 | struct hfs_find_data fd; |
| 110 | |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame^] | 111 | hfs_find_init(HFSPLUS_SB(inode->i_sb)->ext_tree, &fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | __hfsplus_ext_write_extent(inode, &fd); |
| 113 | hfs_find_exit(&fd); |
| 114 | } |
| 115 | } |
| 116 | |
| 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) |
| 120 | { |
| 121 | int res; |
| 122 | |
| 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) |
| 127 | return res; |
| 128 | if (fd->key->ext.cnid != fd->search_key->ext.cnid || |
| 129 | fd->key->ext.fork_type != fd->search_key->ext.fork_type) |
| 130 | return -ENOENT; |
| 131 | if (fd->entrylength != sizeof(hfsplus_extent_rec)) |
| 132 | return -EIO; |
| 133 | hfs_bnode_read(fd->bnode, extent, fd->entryoffset, sizeof(hfsplus_extent_rec)); |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | static inline int __hfsplus_ext_cache_extent(struct hfs_find_data *fd, struct inode *inode, u32 block) |
| 138 | { |
| 139 | int res; |
| 140 | |
| 141 | if (HFSPLUS_I(inode).flags & HFSPLUS_FLG_EXT_DIRTY) |
| 142 | __hfsplus_ext_write_extent(inode, fd); |
| 143 | |
| 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); |
| 146 | if (!res) { |
| 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); |
| 149 | } else { |
| 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); |
| 152 | } |
| 153 | return res; |
| 154 | } |
| 155 | |
| 156 | static int hfsplus_ext_read_extent(struct inode *inode, u32 block) |
| 157 | { |
| 158 | struct hfs_find_data fd; |
| 159 | int res; |
| 160 | |
| 161 | if (block >= HFSPLUS_I(inode).cached_start && |
| 162 | block < HFSPLUS_I(inode).cached_start + HFSPLUS_I(inode).cached_blocks) |
| 163 | return 0; |
| 164 | |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame^] | 165 | hfs_find_init(HFSPLUS_SB(inode->i_sb)->ext_tree, &fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | res = __hfsplus_ext_cache_extent(&fd, inode, block); |
| 167 | hfs_find_exit(&fd); |
| 168 | return res; |
| 169 | } |
| 170 | |
| 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) |
| 174 | { |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame^] | 175 | struct super_block *sb = inode->i_sb; |
| 176 | struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | int res = -EIO; |
| 178 | u32 ablock, dblock, mask; |
| 179 | int shift; |
| 180 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | /* Convert inode block to disk allocation block */ |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame^] | 182 | shift = sbi->alloc_blksz_shift - sb->s_blocksize_bits; |
| 183 | ablock = iblock >> sbi->fs_shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | |
| 185 | if (iblock >= HFSPLUS_I(inode).fs_blocks) { |
| 186 | if (iblock > HFSPLUS_I(inode).fs_blocks || !create) |
| 187 | return -EIO; |
| 188 | if (ablock >= HFSPLUS_I(inode).alloc_blocks) { |
| 189 | res = hfsplus_file_extend(inode); |
| 190 | if (res) |
| 191 | return res; |
| 192 | } |
| 193 | } else |
| 194 | create = 0; |
| 195 | |
| 196 | if (ablock < HFSPLUS_I(inode).first_blocks) { |
| 197 | dblock = hfsplus_ext_find_block(HFSPLUS_I(inode).first_extents, ablock); |
| 198 | goto done; |
| 199 | } |
| 200 | |
Eric Sesterhenn | 248736c | 2008-10-18 20:28:02 -0700 | [diff] [blame] | 201 | if (inode->i_ino == HFSPLUS_EXT_CNID) |
| 202 | return -EIO; |
| 203 | |
Matthias Kaehlcke | 895c23f | 2008-07-25 01:46:36 -0700 | [diff] [blame] | 204 | mutex_lock(&HFSPLUS_I(inode).extents_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | res = hfsplus_ext_read_extent(inode, ablock); |
| 206 | if (!res) { |
| 207 | dblock = hfsplus_ext_find_block(HFSPLUS_I(inode).cached_extents, ablock - |
| 208 | HFSPLUS_I(inode).cached_start); |
| 209 | } else { |
Matthias Kaehlcke | 895c23f | 2008-07-25 01:46:36 -0700 | [diff] [blame] | 210 | mutex_unlock(&HFSPLUS_I(inode).extents_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | return -EIO; |
| 212 | } |
Matthias Kaehlcke | 895c23f | 2008-07-25 01:46:36 -0700 | [diff] [blame] | 213 | mutex_unlock(&HFSPLUS_I(inode).extents_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | |
| 215 | done: |
| 216 | dprint(DBG_EXTENT, "get_block(%lu): %llu - %u\n", inode->i_ino, (long long)iblock, dblock); |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame^] | 217 | mask = (1 << sbi->fs_shift) - 1; |
| 218 | map_bh(bh_result, sb, (dblock << sbi->fs_shift) + sbi->blockoffset + (iblock & mask)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | if (create) { |
| 220 | set_buffer_new(bh_result); |
| 221 | HFSPLUS_I(inode).phys_size += sb->s_blocksize; |
| 222 | HFSPLUS_I(inode).fs_blocks++; |
| 223 | inode_add_bytes(inode, sb->s_blocksize); |
| 224 | mark_inode_dirty(inode); |
| 225 | } |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | static void hfsplus_dump_extent(struct hfsplus_extent *extent) |
| 230 | { |
| 231 | int i; |
| 232 | |
| 233 | dprint(DBG_EXTENT, " "); |
| 234 | for (i = 0; i < 8; i++) |
| 235 | dprint(DBG_EXTENT, " %u:%u", be32_to_cpu(extent[i].start_block), |
| 236 | be32_to_cpu(extent[i].block_count)); |
| 237 | dprint(DBG_EXTENT, "\n"); |
| 238 | } |
| 239 | |
| 240 | static int hfsplus_add_extent(struct hfsplus_extent *extent, u32 offset, |
| 241 | u32 alloc_block, u32 block_count) |
| 242 | { |
| 243 | u32 count, start; |
| 244 | int i; |
| 245 | |
| 246 | hfsplus_dump_extent(extent); |
| 247 | for (i = 0; i < 8; extent++, i++) { |
| 248 | count = be32_to_cpu(extent->block_count); |
| 249 | if (offset == count) { |
| 250 | start = be32_to_cpu(extent->start_block); |
| 251 | if (alloc_block != start + count) { |
| 252 | if (++i >= 8) |
| 253 | return -ENOSPC; |
| 254 | extent++; |
| 255 | extent->start_block = cpu_to_be32(alloc_block); |
| 256 | } else |
| 257 | block_count += count; |
| 258 | extent->block_count = cpu_to_be32(block_count); |
| 259 | return 0; |
| 260 | } else if (offset < count) |
| 261 | break; |
| 262 | offset -= count; |
| 263 | } |
| 264 | /* panic? */ |
| 265 | return -EIO; |
| 266 | } |
| 267 | |
| 268 | static int hfsplus_free_extents(struct super_block *sb, |
| 269 | struct hfsplus_extent *extent, |
| 270 | u32 offset, u32 block_nr) |
| 271 | { |
| 272 | u32 count, start; |
| 273 | int i; |
| 274 | |
| 275 | hfsplus_dump_extent(extent); |
| 276 | for (i = 0; i < 8; extent++, i++) { |
| 277 | count = be32_to_cpu(extent->block_count); |
| 278 | if (offset == count) |
| 279 | goto found; |
| 280 | else if (offset < count) |
| 281 | break; |
| 282 | offset -= count; |
| 283 | } |
| 284 | /* panic? */ |
| 285 | return -EIO; |
| 286 | found: |
| 287 | for (;;) { |
| 288 | start = be32_to_cpu(extent->start_block); |
| 289 | if (count <= block_nr) { |
| 290 | hfsplus_block_free(sb, start, count); |
| 291 | extent->block_count = 0; |
| 292 | extent->start_block = 0; |
| 293 | block_nr -= count; |
| 294 | } else { |
| 295 | count -= block_nr; |
| 296 | hfsplus_block_free(sb, start + count, block_nr); |
| 297 | extent->block_count = cpu_to_be32(count); |
| 298 | block_nr = 0; |
| 299 | } |
| 300 | if (!block_nr || !i) |
| 301 | return 0; |
| 302 | i--; |
| 303 | extent--; |
| 304 | count = be32_to_cpu(extent->block_count); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | int hfsplus_free_fork(struct super_block *sb, u32 cnid, struct hfsplus_fork_raw *fork, int type) |
| 309 | { |
| 310 | struct hfs_find_data fd; |
| 311 | hfsplus_extent_rec ext_entry; |
| 312 | u32 total_blocks, blocks, start; |
| 313 | int res, i; |
| 314 | |
| 315 | total_blocks = be32_to_cpu(fork->total_blocks); |
| 316 | if (!total_blocks) |
| 317 | return 0; |
| 318 | |
| 319 | blocks = 0; |
| 320 | for (i = 0; i < 8; i++) |
| 321 | blocks += be32_to_cpu(fork->extents[i].block_count); |
| 322 | |
| 323 | res = hfsplus_free_extents(sb, fork->extents, blocks, blocks); |
| 324 | if (res) |
| 325 | return res; |
| 326 | if (total_blocks == blocks) |
| 327 | return 0; |
| 328 | |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame^] | 329 | hfs_find_init(HFSPLUS_SB(sb)->ext_tree, &fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | do { |
| 331 | res = __hfsplus_ext_read_extent(&fd, ext_entry, cnid, |
| 332 | total_blocks, type); |
| 333 | if (res) |
| 334 | break; |
| 335 | start = be32_to_cpu(fd.key->ext.start_block); |
| 336 | hfsplus_free_extents(sb, ext_entry, |
| 337 | total_blocks - start, |
| 338 | total_blocks); |
| 339 | hfs_brec_remove(&fd); |
| 340 | total_blocks = start; |
| 341 | } while (total_blocks > blocks); |
| 342 | hfs_find_exit(&fd); |
| 343 | |
| 344 | return res; |
| 345 | } |
| 346 | |
| 347 | int hfsplus_file_extend(struct inode *inode) |
| 348 | { |
| 349 | struct super_block *sb = inode->i_sb; |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame^] | 350 | struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | u32 start, len, goal; |
| 352 | int res; |
| 353 | |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame^] | 354 | if (sbi->alloc_file->i_size * 8 < |
| 355 | sbi->total_blocks - sbi->free_blocks + 8) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | // extend alloc file |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame^] | 357 | printk(KERN_ERR "hfs: extend alloc file! (%Lu,%u,%u)\n", |
| 358 | sbi->alloc_file->i_size * 8, |
| 359 | sbi->total_blocks, sbi->free_blocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | return -ENOSPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Matthias Kaehlcke | 895c23f | 2008-07-25 01:46:36 -0700 | [diff] [blame] | 363 | mutex_lock(&HFSPLUS_I(inode).extents_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | if (HFSPLUS_I(inode).alloc_blocks == HFSPLUS_I(inode).first_blocks) |
| 365 | goal = hfsplus_ext_lastblock(HFSPLUS_I(inode).first_extents); |
| 366 | else { |
| 367 | res = hfsplus_ext_read_extent(inode, HFSPLUS_I(inode).alloc_blocks); |
| 368 | if (res) |
| 369 | goto out; |
| 370 | goal = hfsplus_ext_lastblock(HFSPLUS_I(inode).cached_extents); |
| 371 | } |
| 372 | |
| 373 | len = HFSPLUS_I(inode).clump_blocks; |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame^] | 374 | start = hfsplus_block_allocate(sb, sbi->total_blocks, goal, &len); |
| 375 | if (start >= sbi->total_blocks) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | start = hfsplus_block_allocate(sb, goal, 0, &len); |
| 377 | if (start >= goal) { |
| 378 | res = -ENOSPC; |
| 379 | goto out; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | dprint(DBG_EXTENT, "extend %lu: %u,%u\n", inode->i_ino, start, len); |
| 384 | if (HFSPLUS_I(inode).alloc_blocks <= HFSPLUS_I(inode).first_blocks) { |
| 385 | if (!HFSPLUS_I(inode).first_blocks) { |
| 386 | dprint(DBG_EXTENT, "first extents\n"); |
| 387 | /* no extents yet */ |
| 388 | HFSPLUS_I(inode).first_extents[0].start_block = cpu_to_be32(start); |
| 389 | HFSPLUS_I(inode).first_extents[0].block_count = cpu_to_be32(len); |
| 390 | res = 0; |
| 391 | } else { |
| 392 | /* try to append to extents in inode */ |
| 393 | res = hfsplus_add_extent(HFSPLUS_I(inode).first_extents, |
| 394 | HFSPLUS_I(inode).alloc_blocks, |
| 395 | start, len); |
| 396 | if (res == -ENOSPC) |
| 397 | goto insert_extent; |
| 398 | } |
| 399 | if (!res) { |
| 400 | hfsplus_dump_extent(HFSPLUS_I(inode).first_extents); |
| 401 | HFSPLUS_I(inode).first_blocks += len; |
| 402 | } |
| 403 | } else { |
| 404 | res = hfsplus_add_extent(HFSPLUS_I(inode).cached_extents, |
| 405 | HFSPLUS_I(inode).alloc_blocks - |
| 406 | HFSPLUS_I(inode).cached_start, |
| 407 | start, len); |
| 408 | if (!res) { |
| 409 | hfsplus_dump_extent(HFSPLUS_I(inode).cached_extents); |
| 410 | HFSPLUS_I(inode).flags |= HFSPLUS_FLG_EXT_DIRTY; |
| 411 | HFSPLUS_I(inode).cached_blocks += len; |
| 412 | } else if (res == -ENOSPC) |
| 413 | goto insert_extent; |
| 414 | } |
| 415 | out: |
Matthias Kaehlcke | 895c23f | 2008-07-25 01:46:36 -0700 | [diff] [blame] | 416 | mutex_unlock(&HFSPLUS_I(inode).extents_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | if (!res) { |
| 418 | HFSPLUS_I(inode).alloc_blocks += len; |
| 419 | mark_inode_dirty(inode); |
| 420 | } |
| 421 | return res; |
| 422 | |
| 423 | insert_extent: |
| 424 | dprint(DBG_EXTENT, "insert new extent\n"); |
| 425 | hfsplus_ext_write_extent(inode); |
| 426 | |
| 427 | memset(HFSPLUS_I(inode).cached_extents, 0, sizeof(hfsplus_extent_rec)); |
| 428 | HFSPLUS_I(inode).cached_extents[0].start_block = cpu_to_be32(start); |
| 429 | HFSPLUS_I(inode).cached_extents[0].block_count = cpu_to_be32(len); |
| 430 | hfsplus_dump_extent(HFSPLUS_I(inode).cached_extents); |
| 431 | HFSPLUS_I(inode).flags |= HFSPLUS_FLG_EXT_DIRTY | HFSPLUS_FLG_EXT_NEW; |
| 432 | HFSPLUS_I(inode).cached_start = HFSPLUS_I(inode).alloc_blocks; |
| 433 | HFSPLUS_I(inode).cached_blocks = len; |
| 434 | |
| 435 | res = 0; |
| 436 | goto out; |
| 437 | } |
| 438 | |
| 439 | void hfsplus_file_truncate(struct inode *inode) |
| 440 | { |
| 441 | struct super_block *sb = inode->i_sb; |
| 442 | struct hfs_find_data fd; |
| 443 | u32 alloc_cnt, blk_cnt, start; |
| 444 | int res; |
| 445 | |
| 446 | dprint(DBG_INODE, "truncate: %lu, %Lu -> %Lu\n", inode->i_ino, |
| 447 | (long long)HFSPLUS_I(inode).phys_size, inode->i_size); |
| 448 | if (inode->i_size > HFSPLUS_I(inode).phys_size) { |
| 449 | struct address_space *mapping = inode->i_mapping; |
| 450 | struct page *page; |
Nick Piggin | 7c0efc6 | 2007-10-16 01:25:09 -0700 | [diff] [blame] | 451 | void *fsdata; |
| 452 | u32 size = inode->i_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | int res; |
| 454 | |
Nick Piggin | 7c0efc6 | 2007-10-16 01:25:09 -0700 | [diff] [blame] | 455 | res = pagecache_write_begin(NULL, mapping, size, 0, |
| 456 | AOP_FLAG_UNINTERRUPTIBLE, |
| 457 | &page, &fsdata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | if (res) |
Nick Piggin | 7c0efc6 | 2007-10-16 01:25:09 -0700 | [diff] [blame] | 459 | return; |
| 460 | res = pagecache_write_end(NULL, mapping, size, 0, 0, page, fsdata); |
| 461 | if (res < 0) |
| 462 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | mark_inode_dirty(inode); |
| 464 | return; |
Roman Zippel | f76d28d | 2005-08-01 21:11:40 -0700 | [diff] [blame] | 465 | } else if (inode->i_size == HFSPLUS_I(inode).phys_size) |
| 466 | return; |
| 467 | |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame^] | 468 | blk_cnt = (inode->i_size + HFSPLUS_SB(sb)->alloc_blksz - 1) >> |
| 469 | HFSPLUS_SB(sb)->alloc_blksz_shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | alloc_cnt = HFSPLUS_I(inode).alloc_blocks; |
| 471 | if (blk_cnt == alloc_cnt) |
| 472 | goto out; |
| 473 | |
Matthias Kaehlcke | 895c23f | 2008-07-25 01:46:36 -0700 | [diff] [blame] | 474 | mutex_lock(&HFSPLUS_I(inode).extents_lock); |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame^] | 475 | hfs_find_init(HFSPLUS_SB(sb)->ext_tree, &fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | while (1) { |
| 477 | if (alloc_cnt == HFSPLUS_I(inode).first_blocks) { |
| 478 | hfsplus_free_extents(sb, HFSPLUS_I(inode).first_extents, |
| 479 | alloc_cnt, alloc_cnt - blk_cnt); |
| 480 | hfsplus_dump_extent(HFSPLUS_I(inode).first_extents); |
| 481 | HFSPLUS_I(inode).first_blocks = blk_cnt; |
| 482 | break; |
| 483 | } |
| 484 | res = __hfsplus_ext_cache_extent(&fd, inode, alloc_cnt); |
| 485 | if (res) |
| 486 | break; |
| 487 | start = HFSPLUS_I(inode).cached_start; |
| 488 | hfsplus_free_extents(sb, HFSPLUS_I(inode).cached_extents, |
| 489 | alloc_cnt - start, alloc_cnt - blk_cnt); |
| 490 | hfsplus_dump_extent(HFSPLUS_I(inode).cached_extents); |
| 491 | if (blk_cnt > start) { |
| 492 | HFSPLUS_I(inode).flags |= HFSPLUS_FLG_EXT_DIRTY; |
| 493 | break; |
| 494 | } |
| 495 | alloc_cnt = start; |
| 496 | HFSPLUS_I(inode).cached_start = HFSPLUS_I(inode).cached_blocks = 0; |
| 497 | HFSPLUS_I(inode).flags &= ~(HFSPLUS_FLG_EXT_DIRTY | HFSPLUS_FLG_EXT_NEW); |
| 498 | hfs_brec_remove(&fd); |
| 499 | } |
| 500 | hfs_find_exit(&fd); |
Matthias Kaehlcke | 895c23f | 2008-07-25 01:46:36 -0700 | [diff] [blame] | 501 | mutex_unlock(&HFSPLUS_I(inode).extents_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | |
| 503 | HFSPLUS_I(inode).alloc_blocks = blk_cnt; |
| 504 | out: |
| 505 | HFSPLUS_I(inode).phys_size = inode->i_size; |
| 506 | HFSPLUS_I(inode).fs_blocks = (inode->i_size + sb->s_blocksize - 1) >> sb->s_blocksize_bits; |
| 507 | inode_set_bytes(inode, HFSPLUS_I(inode).fs_blocks << sb->s_blocksize_bits); |
| 508 | mark_inode_dirty(inode); |
| 509 | } |