extents.c 15 KB

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