extents.c 15 KB

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