1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * linux/fs/hfsplus/extents.c 4 * 5 * Copyright (C) 2001 6 * Brad Boyer (flar@allandria.com) 7 * (C) 2003 Ardis Technologies <roman@ardistech.com> 8 * 9 * Handling of Extents both in catalog and extents overflow trees 10 */ 11 12 #include <linux/errno.h> 13 #include <linux/fs.h> 14 #include <linux/pagemap.h> 15 16 #include "hfsplus_fs.h" 17 #include "hfsplus_raw.h" 18 19 /* Compare two extents keys, returns 0 on same, pos/neg for difference */ 20 int hfsplus_ext_cmp_key(const hfsplus_btree_key *k1, 21 const hfsplus_btree_key *k2) 22 { 23 __be32 k1id, k2id; 24 __be32 k1s, k2s; 25 26 k1id = k1->ext.cnid; 27 k2id = k2->ext.cnid; 28 if (k1id != k2id) 29 return be32_to_cpu(k1id) < be32_to_cpu(k2id) ? -1 : 1; 30 31 if (k1->ext.fork_type != k2->ext.fork_type) 32 return k1->ext.fork_type < k2->ext.fork_type ? -1 : 1; 33 34 k1s = k1->ext.start_block; 35 k2s = k2->ext.start_block; 36 if (k1s == k2s) 37 return 0; 38 return be32_to_cpu(k1s) < be32_to_cpu(k2s) ? -1 : 1; 39 } 40 41 static void hfsplus_ext_build_key(hfsplus_btree_key *key, u32 cnid, 42 u32 block, u8 type) 43 { 44 key->key_len = cpu_to_be16(HFSPLUS_EXT_KEYLEN - 2); 45 key->ext.cnid = cpu_to_be32(cnid); 46 key->ext.start_block = cpu_to_be32(block); 47 key->ext.fork_type = type; 48 key->ext.pad = 0; 49 } 50 51 static u32 hfsplus_ext_find_block(struct hfsplus_extent *ext, u32 off) 52 { 53 int i; 54 u32 count; 55 56 for (i = 0; i < 8; ext++, i++) { 57 count = be32_to_cpu(ext->block_count); 58 if (off < count) 59 return be32_to_cpu(ext->start_block) + off; 60 off -= count; 61 } 62 /* panic? */ 63 return 0; 64 } 65 66 static int hfsplus_ext_block_count(struct hfsplus_extent *ext) 67 { 68 int i; 69 u32 count = 0; 70 71 for (i = 0; i < 8; ext++, i++) 72 count += be32_to_cpu(ext->block_count); 73 return count; 74 } 75 76 static u32 hfsplus_ext_lastblock(struct hfsplus_extent *ext) 77 { 78 int i; 79 80 ext += 7; 81 for (i = 0; i < 7; ext--, i++) 82 if (ext->block_count) 83 break; 84 return be32_to_cpu(ext->start_block) + be32_to_cpu(ext->block_count); 85 } 86 87 static int __hfsplus_ext_write_extent(struct inode *inode, 88 struct hfs_find_data *fd) 89 { 90 struct hfsplus_inode_info *hip = HFSPLUS_I(inode); 91 int res; 92 93 WARN_ON(!mutex_is_locked(&hip->extents_lock)); 94 95 hfsplus_ext_build_key(fd->search_key, inode->i_ino, hip->cached_start, 96 HFSPLUS_IS_RSRC(inode) ? 97 HFSPLUS_TYPE_RSRC : HFSPLUS_TYPE_DATA); 98 99 res = hfs_brec_find(fd, hfs_find_rec_by_key); 100 if (hip->extent_state & HFSPLUS_EXT_NEW) { 101 if (res != -ENOENT) 102 return res; 103 /* Fail early and avoid ENOSPC during the btree operation */ 104 res = hfs_bmap_reserve(fd->tree, fd->tree->depth + 1); 105 if (res) 106 return res; 107 hfs_brec_insert(fd, hip->cached_extents, 108 sizeof(hfsplus_extent_rec)); 109 hip->extent_state &= ~(HFSPLUS_EXT_DIRTY | HFSPLUS_EXT_NEW); 110 } else { 111 if (res) 112 return res; 113 hfs_bnode_write(fd->bnode, hip->cached_extents, 114 fd->entryoffset, fd->entrylength); 115 hip->extent_state &= ~HFSPLUS_EXT_DIRTY; 116 } 117 118 /* 119 * We can't just use hfsplus_mark_inode_dirty here, because we 120 * also get called from hfsplus_write_inode, which should not 121 * redirty the inode. Instead the callers have to be careful 122 * to explicily mark the inode dirty, too. 123 */ 124 set_bit(HFSPLUS_I_EXT_DIRTY, 125 &HFSPLUS_I(HFSPLUS_EXT_TREE_I(inode->i_sb))->flags); 126 set_bit(HFSPLUS_I_EXT_DIRTY, &hip->flags); 127 128 return 0; 129 } 130 131 static int hfsplus_ext_write_extent_locked(struct inode *inode) 132 { 133 int res = 0; 134 135 if (HFSPLUS_I(inode)->extent_state & HFSPLUS_EXT_DIRTY) { 136 struct hfs_find_data fd; 137 138 res = hfs_find_init(HFSPLUS_SB(inode->i_sb)->ext_tree, &fd); 139 if (res) 140 return res; 141 res = __hfsplus_ext_write_extent(inode, &fd); 142 hfs_find_exit(&fd); 143 } 144 return res; 145 } 146 147 int hfsplus_ext_write_extent(struct inode *inode) 148 { 149 int res; 150 151 mutex_lock(&HFSPLUS_I(inode)->extents_lock); 152 res = hfsplus_ext_write_extent_locked(inode); 153 mutex_unlock(&HFSPLUS_I(inode)->extents_lock); 154 155 return res; 156 } 157 158 static inline int __hfsplus_ext_read_extent(struct hfs_find_data *fd, 159 struct hfsplus_extent *extent, 160 u32 cnid, u32 block, u8 type) 161 { 162 int res; 163 164 hfsplus_ext_build_key(fd->search_key, cnid, block, type); 165 fd->key->ext.cnid = 0; 166 res = hfs_brec_find(fd, hfs_find_rec_by_key); 167 if (res && res != -ENOENT) 168 return res; 169 if (fd->key->ext.cnid != fd->search_key->ext.cnid || 170 fd->key->ext.fork_type != fd->search_key->ext.fork_type) 171 return -ENOENT; 172 if (fd->entrylength != sizeof(hfsplus_extent_rec)) 173 return -EIO; 174 hfs_bnode_read(fd->bnode, extent, fd->entryoffset, 175 sizeof(hfsplus_extent_rec)); 176 return 0; 177 } 178 179 static inline int __hfsplus_ext_cache_extent(struct hfs_find_data *fd, 180 struct inode *inode, u32 block) 181 { 182 struct hfsplus_inode_info *hip = HFSPLUS_I(inode); 183 int res; 184 185 WARN_ON(!mutex_is_locked(&hip->extents_lock)); 186 187 if (hip->extent_state & HFSPLUS_EXT_DIRTY) { 188 res = __hfsplus_ext_write_extent(inode, fd); 189 if (res) 190 return res; 191 } 192 193 res = __hfsplus_ext_read_extent(fd, hip->cached_extents, inode->i_ino, 194 block, HFSPLUS_IS_RSRC(inode) ? 195 HFSPLUS_TYPE_RSRC : 196 HFSPLUS_TYPE_DATA); 197 if (!res) { 198 hip->cached_start = be32_to_cpu(fd->key->ext.start_block); 199 hip->cached_blocks = 200 hfsplus_ext_block_count(hip->cached_extents); 201 } else { 202 hip->cached_start = hip->cached_blocks = 0; 203 hip->extent_state &= ~(HFSPLUS_EXT_DIRTY | HFSPLUS_EXT_NEW); 204 } 205 return res; 206 } 207 208 static int hfsplus_ext_read_extent(struct inode *inode, u32 block) 209 { 210 struct hfsplus_inode_info *hip = HFSPLUS_I(inode); 211 struct hfs_find_data fd; 212 int res; 213 214 if (block >= hip->cached_start && 215 block < hip->cached_start + hip->cached_blocks) 216 return 0; 217 218 res = hfs_find_init(HFSPLUS_SB(inode->i_sb)->ext_tree, &fd); 219 if (!res) { 220 res = __hfsplus_ext_cache_extent(&fd, inode, block); 221 hfs_find_exit(&fd); 222 } 223 return res; 224 } 225 226 /* Get a block at iblock for inode, possibly allocating if create */ 227 int hfsplus_get_block(struct inode *inode, sector_t iblock, 228 struct buffer_head *bh_result, int create) 229 { 230 struct super_block *sb = inode->i_sb; 231 struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb); 232 struct hfsplus_inode_info *hip = HFSPLUS_I(inode); 233 int res = -EIO; 234 u32 ablock, dblock, mask; 235 sector_t sector; 236 int was_dirty = 0; 237 238 /* Convert inode block to disk allocation block */ 239 ablock = iblock >> sbi->fs_shift; 240 241 if (iblock >= hip->fs_blocks) { 242 if (!create) 243 return 0; 244 if (iblock > hip->fs_blocks) 245 return -EIO; 246 if (ablock >= hip->alloc_blocks) { 247 res = hfsplus_file_extend(inode, false); 248 if (res) 249 return res; 250 } 251 } else 252 create = 0; 253 254 if (ablock < hip->first_blocks) { 255 dblock = hfsplus_ext_find_block(hip->first_extents, ablock); 256 goto done; 257 } 258 259 if (inode->i_ino == HFSPLUS_EXT_CNID) 260 return -EIO; 261 262 mutex_lock(&hip->extents_lock); 263 264 /* 265 * hfsplus_ext_read_extent will write out a cached extent into 266 * the extents btree. In that case we may have to mark the inode 267 * dirty even for a pure read of an extent here. 268 */ 269 was_dirty = (hip->extent_state & HFSPLUS_EXT_DIRTY); 270 res = hfsplus_ext_read_extent(inode, ablock); 271 if (res) { 272 mutex_unlock(&hip->extents_lock); 273 return -EIO; 274 } 275 dblock = hfsplus_ext_find_block(hip->cached_extents, 276 ablock - hip->cached_start); 277 mutex_unlock(&hip->extents_lock); 278 279 done: 280 hfs_dbg("ino %llu, iblock %llu - dblock %u\n", 281 inode->i_ino, (long long)iblock, dblock); 282 283 mask = (1 << sbi->fs_shift) - 1; 284 sector = ((sector_t)dblock << sbi->fs_shift) + 285 sbi->blockoffset + (iblock & mask); 286 map_bh(bh_result, sb, sector); 287 288 if (create) { 289 set_buffer_new(bh_result); 290 hip->phys_size += sb->s_blocksize; 291 hip->fs_blocks++; 292 inode_add_bytes(inode, sb->s_blocksize); 293 } 294 if (create || was_dirty) 295 mark_inode_dirty(inode); 296 return 0; 297 } 298 299 static void hfsplus_dump_extent(struct hfsplus_extent *extent) 300 { 301 int i; 302 303 hfs_dbg("extent "); 304 for (i = 0; i < 8; i++) 305 hfs_dbg(" start_block %u, block_count %u", 306 be32_to_cpu(extent[i].start_block), 307 be32_to_cpu(extent[i].block_count)); 308 hfs_dbg("\n"); 309 } 310 311 static int hfsplus_add_extent(struct hfsplus_extent *extent, u32 offset, 312 u32 alloc_block, u32 block_count) 313 { 314 u32 count, start; 315 int i; 316 317 hfsplus_dump_extent(extent); 318 for (i = 0; i < 8; extent++, i++) { 319 count = be32_to_cpu(extent->block_count); 320 if (offset == count) { 321 start = be32_to_cpu(extent->start_block); 322 if (alloc_block != start + count) { 323 if (++i >= 8) 324 return -ENOSPC; 325 extent++; 326 extent->start_block = cpu_to_be32(alloc_block); 327 } else 328 block_count += count; 329 extent->block_count = cpu_to_be32(block_count); 330 return 0; 331 } else if (offset < count) 332 break; 333 offset -= count; 334 } 335 /* panic? */ 336 return -EIO; 337 } 338 339 static int hfsplus_free_extents(struct super_block *sb, 340 struct hfsplus_extent *extent, 341 u32 offset, u32 block_nr) 342 { 343 u32 count, start; 344 int i; 345 int err = 0; 346 347 hfsplus_dump_extent(extent); 348 for (i = 0; i < 8; extent++, i++) { 349 count = be32_to_cpu(extent->block_count); 350 if (offset == count) 351 goto found; 352 else if (offset < count) 353 break; 354 offset -= count; 355 } 356 /* panic? */ 357 return -EIO; 358 found: 359 for (;;) { 360 start = be32_to_cpu(extent->start_block); 361 if (count <= block_nr) { 362 err = hfsplus_block_free(sb, start, count); 363 if (err) { 364 pr_err("can't free extent: start %u, count %u\n", 365 start, count); 366 } 367 extent->block_count = 0; 368 extent->start_block = 0; 369 block_nr -= count; 370 } else { 371 count -= block_nr; 372 err = hfsplus_block_free(sb, start + count, block_nr); 373 if (err) { 374 pr_err("can't free extent: start %u, count %u\n", 375 start, count); 376 } 377 extent->block_count = cpu_to_be32(count); 378 block_nr = 0; 379 } 380 if (!block_nr || !i) { 381 /* 382 * Try to free all extents and 383 * return only last error 384 */ 385 return err; 386 } 387 i--; 388 extent--; 389 count = be32_to_cpu(extent->block_count); 390 } 391 } 392 393 int hfsplus_free_fork(struct super_block *sb, u32 cnid, 394 struct hfsplus_fork_raw *fork, int type) 395 { 396 struct hfs_find_data fd; 397 hfsplus_extent_rec ext_entry; 398 u32 total_blocks, blocks, start; 399 int res, i; 400 401 total_blocks = be32_to_cpu(fork->total_blocks); 402 if (!total_blocks) 403 return 0; 404 405 blocks = 0; 406 for (i = 0; i < 8; i++) 407 blocks += be32_to_cpu(fork->extents[i].block_count); 408 409 res = hfsplus_free_extents(sb, fork->extents, blocks, blocks); 410 if (res) 411 return res; 412 if (total_blocks == blocks) 413 return 0; 414 415 res = hfs_find_init(HFSPLUS_SB(sb)->ext_tree, &fd); 416 if (res) 417 return res; 418 do { 419 res = __hfsplus_ext_read_extent(&fd, ext_entry, cnid, 420 total_blocks, type); 421 if (res) 422 break; 423 start = be32_to_cpu(fd.key->ext.start_block); 424 hfs_brec_remove(&fd); 425 426 mutex_unlock(&fd.tree->tree_lock); 427 hfsplus_free_extents(sb, ext_entry, total_blocks - start, 428 total_blocks); 429 total_blocks = start; 430 mutex_lock_nested(&fd.tree->tree_lock, 431 hfsplus_btree_lock_class(fd.tree)); 432 } while (total_blocks > blocks); 433 hfs_find_exit(&fd); 434 435 return res; 436 } 437 438 int hfsplus_file_extend(struct inode *inode, bool zeroout) 439 { 440 struct super_block *sb = inode->i_sb; 441 struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb); 442 struct hfsplus_inode_info *hip = HFSPLUS_I(inode); 443 u32 start, len, goal; 444 int res; 445 446 if (sbi->alloc_file->i_size * 8 < 447 sbi->total_blocks - sbi->free_blocks + 8) { 448 /* extend alloc file */ 449 pr_err_ratelimited("extend alloc file! (%llu,%u,%u)\n", 450 sbi->alloc_file->i_size * 8, 451 sbi->total_blocks, sbi->free_blocks); 452 return -ENOSPC; 453 } 454 455 mutex_lock(&hip->extents_lock); 456 if (hip->alloc_blocks == hip->first_blocks) 457 goal = hfsplus_ext_lastblock(hip->first_extents); 458 else { 459 res = hfsplus_ext_read_extent(inode, hip->alloc_blocks); 460 if (res) 461 goto out; 462 goal = hfsplus_ext_lastblock(hip->cached_extents); 463 } 464 465 len = hip->clump_blocks; 466 start = hfsplus_block_allocate(sb, sbi->total_blocks, goal, &len); 467 if (start >= sbi->total_blocks) { 468 start = hfsplus_block_allocate(sb, goal, 0, &len); 469 if (start >= goal) { 470 res = -ENOSPC; 471 goto out; 472 } 473 } 474 475 if (zeroout) { 476 res = sb_issue_zeroout(sb, start, len, GFP_NOFS); 477 if (res) 478 goto out; 479 } 480 481 hfs_dbg("ino %llu, start %u, len %u\n", inode->i_ino, start, len); 482 483 if (hip->alloc_blocks <= hip->first_blocks) { 484 if (!hip->first_blocks) { 485 hfs_dbg("first_extent: start %u, len %u\n", 486 start, len); 487 /* no extents yet */ 488 hip->first_extents[0].start_block = cpu_to_be32(start); 489 hip->first_extents[0].block_count = cpu_to_be32(len); 490 res = 0; 491 } else { 492 /* try to append to extents in inode */ 493 res = hfsplus_add_extent(hip->first_extents, 494 hip->alloc_blocks, 495 start, len); 496 if (res == -ENOSPC) 497 goto insert_extent; 498 } 499 if (!res) { 500 hfsplus_dump_extent(hip->first_extents); 501 hip->first_blocks += len; 502 } 503 } else { 504 res = hfsplus_add_extent(hip->cached_extents, 505 hip->alloc_blocks - hip->cached_start, 506 start, len); 507 if (!res) { 508 hfsplus_dump_extent(hip->cached_extents); 509 hip->extent_state |= HFSPLUS_EXT_DIRTY; 510 hip->cached_blocks += len; 511 } else if (res == -ENOSPC) 512 goto insert_extent; 513 } 514 out: 515 if (!res) { 516 hip->alloc_blocks += len; 517 mutex_unlock(&hip->extents_lock); 518 hfsplus_mark_inode_dirty(HFSPLUS_SB(sb)->alloc_file, 519 HFSPLUS_I_ALLOC_DIRTY); 520 hfsplus_mark_inode_dirty(inode, HFSPLUS_I_ALLOC_DIRTY); 521 return 0; 522 } 523 mutex_unlock(&hip->extents_lock); 524 return res; 525 526 insert_extent: 527 hfs_dbg("insert new extent\n"); 528 res = hfsplus_ext_write_extent_locked(inode); 529 if (res) 530 goto out; 531 532 memset(hip->cached_extents, 0, sizeof(hfsplus_extent_rec)); 533 hip->cached_extents[0].start_block = cpu_to_be32(start); 534 hip->cached_extents[0].block_count = cpu_to_be32(len); 535 hfsplus_dump_extent(hip->cached_extents); 536 hip->extent_state |= HFSPLUS_EXT_DIRTY | HFSPLUS_EXT_NEW; 537 hip->cached_start = hip->alloc_blocks; 538 hip->cached_blocks = len; 539 540 res = 0; 541 goto out; 542 } 543 544 void hfsplus_file_truncate(struct inode *inode) 545 { 546 struct super_block *sb = inode->i_sb; 547 struct hfsplus_inode_info *hip = HFSPLUS_I(inode); 548 struct hfs_find_data fd; 549 u32 alloc_cnt, blk_cnt, start; 550 int res; 551 552 hfs_dbg("ino %llu, phys_size %llu -> i_size %llu\n", 553 inode->i_ino, (long long)hip->phys_size, inode->i_size); 554 555 if (inode->i_size > hip->phys_size) { 556 struct address_space *mapping = inode->i_mapping; 557 struct folio *folio; 558 void *fsdata = NULL; 559 loff_t size = inode->i_size; 560 561 res = hfsplus_write_begin(NULL, mapping, size, 0, 562 &folio, &fsdata); 563 if (res) 564 return; 565 res = generic_write_end(NULL, mapping, size, 0, 0, 566 folio, fsdata); 567 if (res < 0) 568 return; 569 mark_inode_dirty(inode); 570 return; 571 } else if (inode->i_size == hip->phys_size) 572 return; 573 574 blk_cnt = (inode->i_size + HFSPLUS_SB(sb)->alloc_blksz - 1) >> 575 HFSPLUS_SB(sb)->alloc_blksz_shift; 576 577 mutex_lock(&hip->extents_lock); 578 579 alloc_cnt = hip->alloc_blocks; 580 if (blk_cnt == alloc_cnt) 581 goto out_unlock; 582 583 res = hfs_find_init(HFSPLUS_SB(sb)->ext_tree, &fd); 584 if (res) { 585 mutex_unlock(&hip->extents_lock); 586 /* XXX: We lack error handling of hfsplus_file_truncate() */ 587 return; 588 } 589 590 while (1) { 591 if (alloc_cnt == hip->first_blocks) { 592 mutex_unlock(&fd.tree->tree_lock); 593 hfsplus_free_extents(sb, hip->first_extents, 594 alloc_cnt, alloc_cnt - blk_cnt); 595 hfsplus_dump_extent(hip->first_extents); 596 hip->first_blocks = blk_cnt; 597 mutex_lock_nested(&fd.tree->tree_lock, 598 hfsplus_btree_lock_class(fd.tree)); 599 break; 600 } 601 res = __hfsplus_ext_cache_extent(&fd, inode, alloc_cnt); 602 if (res) 603 break; 604 605 start = hip->cached_start; 606 if (blk_cnt <= start) 607 hfs_brec_remove(&fd); 608 mutex_unlock(&fd.tree->tree_lock); 609 hfsplus_free_extents(sb, hip->cached_extents, 610 alloc_cnt - start, alloc_cnt - blk_cnt); 611 hfsplus_dump_extent(hip->cached_extents); 612 mutex_lock_nested(&fd.tree->tree_lock, 613 hfsplus_btree_lock_class(fd.tree)); 614 if (blk_cnt > start) { 615 hip->extent_state |= HFSPLUS_EXT_DIRTY; 616 break; 617 } 618 alloc_cnt = start; 619 hip->cached_start = hip->cached_blocks = 0; 620 hip->extent_state &= ~(HFSPLUS_EXT_DIRTY | HFSPLUS_EXT_NEW); 621 } 622 hfs_find_exit(&fd); 623 624 hip->alloc_blocks = blk_cnt; 625 out_unlock: 626 mutex_unlock(&hip->extents_lock); 627 hip->phys_size = inode->i_size; 628 hip->fs_blocks = (inode->i_size + sb->s_blocksize - 1) >> 629 sb->s_blocksize_bits; 630 inode_set_bytes(inode, hip->fs_blocks << sb->s_blocksize_bits); 631 hfsplus_mark_inode_dirty(HFSPLUS_SB(sb)->alloc_file, 632 HFSPLUS_I_ALLOC_DIRTY); 633 hfsplus_mark_inode_dirty(inode, HFSPLUS_I_ALLOC_DIRTY); 634 } 635