extents.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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 void __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);
  82. if (hip->extent_state & HFSPLUS_EXT_NEW) {
  83. if (res != -ENOENT)
  84. return;
  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;
  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. }
  103. static int hfsplus_ext_write_extent_locked(struct inode *inode)
  104. {
  105. int res;
  106. if (HFSPLUS_I(inode)->extent_state & HFSPLUS_EXT_DIRTY) {
  107. struct hfs_find_data fd;
  108. res = hfs_find_init(HFSPLUS_SB(inode->i_sb)->ext_tree, &fd);
  109. if (res)
  110. return res;
  111. __hfsplus_ext_write_extent(inode, &fd);
  112. hfs_find_exit(&fd);
  113. }
  114. return 0;
  115. }
  116. int hfsplus_ext_write_extent(struct inode *inode)
  117. {
  118. int res;
  119. mutex_lock(&HFSPLUS_I(inode)->extents_lock);
  120. res = hfsplus_ext_write_extent_locked(inode);
  121. mutex_unlock(&HFSPLUS_I(inode)->extents_lock);
  122. return res;
  123. }
  124. static inline int __hfsplus_ext_read_extent(struct hfs_find_data *fd,
  125. struct hfsplus_extent *extent,
  126. u32 cnid, u32 block, u8 type)
  127. {
  128. int res;
  129. hfsplus_ext_build_key(fd->search_key, cnid, block, type);
  130. fd->key->ext.cnid = 0;
  131. res = hfs_brec_find(fd);
  132. if (res && res != -ENOENT)
  133. return res;
  134. if (fd->key->ext.cnid != fd->search_key->ext.cnid ||
  135. fd->key->ext.fork_type != fd->search_key->ext.fork_type)
  136. return -ENOENT;
  137. if (fd->entrylength != sizeof(hfsplus_extent_rec))
  138. return -EIO;
  139. hfs_bnode_read(fd->bnode, extent, fd->entryoffset,
  140. sizeof(hfsplus_extent_rec));
  141. return 0;
  142. }
  143. static inline int __hfsplus_ext_cache_extent(struct hfs_find_data *fd,
  144. struct inode *inode, u32 block)
  145. {
  146. struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
  147. int res;
  148. WARN_ON(!mutex_is_locked(&hip->extents_lock));
  149. if (hip->extent_state & HFSPLUS_EXT_DIRTY)
  150. __hfsplus_ext_write_extent(inode, fd);
  151. res = __hfsplus_ext_read_extent(fd, hip->cached_extents, inode->i_ino,
  152. block, HFSPLUS_IS_RSRC(inode) ?
  153. HFSPLUS_TYPE_RSRC :
  154. HFSPLUS_TYPE_DATA);
  155. if (!res) {
  156. hip->cached_start = be32_to_cpu(fd->key->ext.start_block);
  157. hip->cached_blocks =
  158. hfsplus_ext_block_count(hip->cached_extents);
  159. } else {
  160. hip->cached_start = hip->cached_blocks = 0;
  161. hip->extent_state &= ~(HFSPLUS_EXT_DIRTY | HFSPLUS_EXT_NEW);
  162. }
  163. return res;
  164. }
  165. static int hfsplus_ext_read_extent(struct inode *inode, u32 block)
  166. {
  167. struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
  168. struct hfs_find_data fd;
  169. int res;
  170. if (block >= hip->cached_start &&
  171. block < hip->cached_start + hip->cached_blocks)
  172. return 0;
  173. res = hfs_find_init(HFSPLUS_SB(inode->i_sb)->ext_tree, &fd);
  174. if (!res) {
  175. res = __hfsplus_ext_cache_extent(&fd, inode, block);
  176. hfs_find_exit(&fd);
  177. }
  178. return res;
  179. }
  180. /* Get a block at iblock for inode, possibly allocating if create */
  181. int hfsplus_get_block(struct inode *inode, sector_t iblock,
  182. struct buffer_head *bh_result, int create)
  183. {
  184. struct super_block *sb = inode->i_sb;
  185. struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
  186. struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
  187. int res = -EIO;
  188. u32 ablock, dblock, mask;
  189. sector_t sector;
  190. int was_dirty = 0;
  191. int shift;
  192. /* Convert inode block to disk allocation block */
  193. shift = sbi->alloc_blksz_shift - sb->s_blocksize_bits;
  194. ablock = iblock >> sbi->fs_shift;
  195. if (iblock >= hip->fs_blocks) {
  196. if (iblock > hip->fs_blocks || !create)
  197. return -EIO;
  198. if (ablock >= hip->alloc_blocks) {
  199. res = hfsplus_file_extend(inode);
  200. if (res)
  201. return res;
  202. }
  203. } else
  204. create = 0;
  205. if (ablock < hip->first_blocks) {
  206. dblock = hfsplus_ext_find_block(hip->first_extents, ablock);
  207. goto done;
  208. }
  209. if (inode->i_ino == HFSPLUS_EXT_CNID)
  210. return -EIO;
  211. mutex_lock(&hip->extents_lock);
  212. /*
  213. * hfsplus_ext_read_extent will write out a cached extent into
  214. * the extents btree. In that case we may have to mark the inode
  215. * dirty even for a pure read of an extent here.
  216. */
  217. was_dirty = (hip->extent_state & HFSPLUS_EXT_DIRTY);
  218. res = hfsplus_ext_read_extent(inode, ablock);
  219. if (res) {
  220. mutex_unlock(&hip->extents_lock);
  221. return -EIO;
  222. }
  223. dblock = hfsplus_ext_find_block(hip->cached_extents,
  224. ablock - hip->cached_start);
  225. mutex_unlock(&hip->extents_lock);
  226. done:
  227. dprint(DBG_EXTENT, "get_block(%lu): %llu - %u\n",
  228. inode->i_ino, (long long)iblock, dblock);
  229. mask = (1 << sbi->fs_shift) - 1;
  230. sector = ((sector_t)dblock << sbi->fs_shift) +
  231. sbi->blockoffset + (iblock & mask);
  232. map_bh(bh_result, sb, sector);
  233. if (create) {
  234. set_buffer_new(bh_result);
  235. hip->phys_size += sb->s_blocksize;
  236. hip->fs_blocks++;
  237. inode_add_bytes(inode, sb->s_blocksize);
  238. }
  239. if (create || was_dirty)
  240. mark_inode_dirty(inode);
  241. return 0;
  242. }
  243. static void hfsplus_dump_extent(struct hfsplus_extent *extent)
  244. {
  245. int i;
  246. dprint(DBG_EXTENT, " ");
  247. for (i = 0; i < 8; i++)
  248. dprint(DBG_EXTENT, " %u:%u", be32_to_cpu(extent[i].start_block),
  249. be32_to_cpu(extent[i].block_count));
  250. dprint(DBG_EXTENT, "\n");
  251. }
  252. static int hfsplus_add_extent(struct hfsplus_extent *extent, u32 offset,
  253. u32 alloc_block, u32 block_count)
  254. {
  255. u32 count, start;
  256. int i;
  257. hfsplus_dump_extent(extent);
  258. for (i = 0; i < 8; extent++, i++) {
  259. count = be32_to_cpu(extent->block_count);
  260. if (offset == count) {
  261. start = be32_to_cpu(extent->start_block);
  262. if (alloc_block != start + count) {
  263. if (++i >= 8)
  264. return -ENOSPC;
  265. extent++;
  266. extent->start_block = cpu_to_be32(alloc_block);
  267. } else
  268. block_count += count;
  269. extent->block_count = cpu_to_be32(block_count);
  270. return 0;
  271. } else if (offset < count)
  272. break;
  273. offset -= count;
  274. }
  275. /* panic? */
  276. return -EIO;
  277. }
  278. static int hfsplus_free_extents(struct super_block *sb,
  279. struct hfsplus_extent *extent,
  280. u32 offset, u32 block_nr)
  281. {
  282. u32 count, start;
  283. int i;
  284. hfsplus_dump_extent(extent);
  285. for (i = 0; i < 8; extent++, i++) {
  286. count = be32_to_cpu(extent->block_count);
  287. if (offset == count)
  288. goto found;
  289. else if (offset < count)
  290. break;
  291. offset -= count;
  292. }
  293. /* panic? */
  294. return -EIO;
  295. found:
  296. for (;;) {
  297. start = be32_to_cpu(extent->start_block);
  298. if (count <= block_nr) {
  299. hfsplus_block_free(sb, start, count);
  300. extent->block_count = 0;
  301. extent->start_block = 0;
  302. block_nr -= count;
  303. } else {
  304. count -= block_nr;
  305. hfsplus_block_free(sb, start + count, block_nr);
  306. extent->block_count = cpu_to_be32(count);
  307. block_nr = 0;
  308. }
  309. if (!block_nr || !i)
  310. return 0;
  311. i--;
  312. extent--;
  313. count = be32_to_cpu(extent->block_count);
  314. }
  315. }
  316. int hfsplus_free_fork(struct super_block *sb, u32 cnid,
  317. struct hfsplus_fork_raw *fork, int type)
  318. {
  319. struct hfs_find_data fd;
  320. hfsplus_extent_rec ext_entry;
  321. u32 total_blocks, blocks, start;
  322. int res, i;
  323. total_blocks = be32_to_cpu(fork->total_blocks);
  324. if (!total_blocks)
  325. return 0;
  326. blocks = 0;
  327. for (i = 0; i < 8; i++)
  328. blocks += be32_to_cpu(fork->extents[i].block_count);
  329. res = hfsplus_free_extents(sb, fork->extents, blocks, blocks);
  330. if (res)
  331. return res;
  332. if (total_blocks == blocks)
  333. return 0;
  334. res = hfs_find_init(HFSPLUS_SB(sb)->ext_tree, &fd);
  335. if (res)
  336. return res;
  337. do {
  338. res = __hfsplus_ext_read_extent(&fd, ext_entry, cnid,
  339. total_blocks, type);
  340. if (res)
  341. break;
  342. start = be32_to_cpu(fd.key->ext.start_block);
  343. hfsplus_free_extents(sb, ext_entry,
  344. total_blocks - start,
  345. total_blocks);
  346. hfs_brec_remove(&fd);
  347. total_blocks = start;
  348. } while (total_blocks > blocks);
  349. hfs_find_exit(&fd);
  350. return res;
  351. }
  352. int hfsplus_file_extend(struct inode *inode)
  353. {
  354. struct super_block *sb = inode->i_sb;
  355. struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
  356. struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
  357. u32 start, len, goal;
  358. int res;
  359. if (sbi->alloc_file->i_size * 8 <
  360. sbi->total_blocks - sbi->free_blocks + 8) {
  361. /* extend alloc file */
  362. printk(KERN_ERR "hfs: extend alloc file! "
  363. "(%llu,%u,%u)\n",
  364. sbi->alloc_file->i_size * 8,
  365. sbi->total_blocks, sbi->free_blocks);
  366. return -ENOSPC;
  367. }
  368. mutex_lock(&hip->extents_lock);
  369. if (hip->alloc_blocks == hip->first_blocks)
  370. goal = hfsplus_ext_lastblock(hip->first_extents);
  371. else {
  372. res = hfsplus_ext_read_extent(inode, hip->alloc_blocks);
  373. if (res)
  374. goto out;
  375. goal = hfsplus_ext_lastblock(hip->cached_extents);
  376. }
  377. len = hip->clump_blocks;
  378. start = hfsplus_block_allocate(sb, sbi->total_blocks, goal, &len);
  379. if (start >= sbi->total_blocks) {
  380. start = hfsplus_block_allocate(sb, goal, 0, &len);
  381. if (start >= goal) {
  382. res = -ENOSPC;
  383. goto out;
  384. }
  385. }
  386. dprint(DBG_EXTENT, "extend %lu: %u,%u\n", inode->i_ino, start, len);
  387. if (hip->alloc_blocks <= hip->first_blocks) {
  388. if (!hip->first_blocks) {
  389. dprint(DBG_EXTENT, "first extents\n");
  390. /* no extents yet */
  391. hip->first_extents[0].start_block = cpu_to_be32(start);
  392. hip->first_extents[0].block_count = cpu_to_be32(len);
  393. res = 0;
  394. } else {
  395. /* try to append to extents in inode */
  396. res = hfsplus_add_extent(hip->first_extents,
  397. hip->alloc_blocks,
  398. start, len);
  399. if (res == -ENOSPC)
  400. goto insert_extent;
  401. }
  402. if (!res) {
  403. hfsplus_dump_extent(hip->first_extents);
  404. hip->first_blocks += len;
  405. }
  406. } else {
  407. res = hfsplus_add_extent(hip->cached_extents,
  408. hip->alloc_blocks - hip->cached_start,
  409. start, len);
  410. if (!res) {
  411. hfsplus_dump_extent(hip->cached_extents);
  412. hip->extent_state |= HFSPLUS_EXT_DIRTY;
  413. hip->cached_blocks += len;
  414. } else if (res == -ENOSPC)
  415. goto insert_extent;
  416. }
  417. out:
  418. mutex_unlock(&hip->extents_lock);
  419. if (!res) {
  420. hip->alloc_blocks += len;
  421. hfsplus_mark_inode_dirty(inode, HFSPLUS_I_ALLOC_DIRTY);
  422. }
  423. return res;
  424. insert_extent:
  425. dprint(DBG_EXTENT, "insert new extent\n");
  426. res = hfsplus_ext_write_extent_locked(inode);
  427. if (res)
  428. goto out;
  429. memset(hip->cached_extents, 0, sizeof(hfsplus_extent_rec));
  430. hip->cached_extents[0].start_block = cpu_to_be32(start);
  431. hip->cached_extents[0].block_count = cpu_to_be32(len);
  432. hfsplus_dump_extent(hip->cached_extents);
  433. hip->extent_state |= HFSPLUS_EXT_DIRTY | HFSPLUS_EXT_NEW;
  434. hip->cached_start = hip->alloc_blocks;
  435. hip->cached_blocks = len;
  436. res = 0;
  437. goto out;
  438. }
  439. void hfsplus_file_truncate(struct inode *inode)
  440. {
  441. struct super_block *sb = inode->i_sb;
  442. struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
  443. struct hfs_find_data fd;
  444. u32 alloc_cnt, blk_cnt, start;
  445. int res;
  446. dprint(DBG_INODE, "truncate: %lu, %llu -> %llu\n",
  447. inode->i_ino, (long long)hip->phys_size,
  448. inode->i_size);
  449. if (inode->i_size > hip->phys_size) {
  450. struct address_space *mapping = inode->i_mapping;
  451. struct page *page;
  452. void *fsdata;
  453. loff_t size = inode->i_size;
  454. res = pagecache_write_begin(NULL, mapping, size, 0,
  455. AOP_FLAG_UNINTERRUPTIBLE,
  456. &page, &fsdata);
  457. if (res)
  458. return;
  459. res = pagecache_write_end(NULL, mapping, size,
  460. 0, 0, page, fsdata);
  461. if (res < 0)
  462. return;
  463. mark_inode_dirty(inode);
  464. return;
  465. } else if (inode->i_size == hip->phys_size)
  466. return;
  467. blk_cnt = (inode->i_size + HFSPLUS_SB(sb)->alloc_blksz - 1) >>
  468. HFSPLUS_SB(sb)->alloc_blksz_shift;
  469. alloc_cnt = hip->alloc_blocks;
  470. if (blk_cnt == alloc_cnt)
  471. goto out;
  472. mutex_lock(&hip->extents_lock);
  473. res = hfs_find_init(HFSPLUS_SB(sb)->ext_tree, &fd);
  474. if (res) {
  475. mutex_unlock(&hip->extents_lock);
  476. /* XXX: We lack error handling of hfsplus_file_truncate() */
  477. return;
  478. }
  479. while (1) {
  480. if (alloc_cnt == hip->first_blocks) {
  481. hfsplus_free_extents(sb, hip->first_extents,
  482. alloc_cnt, alloc_cnt - blk_cnt);
  483. hfsplus_dump_extent(hip->first_extents);
  484. hip->first_blocks = blk_cnt;
  485. break;
  486. }
  487. res = __hfsplus_ext_cache_extent(&fd, inode, alloc_cnt);
  488. if (res)
  489. break;
  490. start = hip->cached_start;
  491. hfsplus_free_extents(sb, hip->cached_extents,
  492. alloc_cnt - start, alloc_cnt - blk_cnt);
  493. hfsplus_dump_extent(hip->cached_extents);
  494. if (blk_cnt > start) {
  495. hip->extent_state |= HFSPLUS_EXT_DIRTY;
  496. break;
  497. }
  498. alloc_cnt = start;
  499. hip->cached_start = hip->cached_blocks = 0;
  500. hip->extent_state &= ~(HFSPLUS_EXT_DIRTY | HFSPLUS_EXT_NEW);
  501. hfs_brec_remove(&fd);
  502. }
  503. hfs_find_exit(&fd);
  504. mutex_unlock(&hip->extents_lock);
  505. hip->alloc_blocks = blk_cnt;
  506. out:
  507. hip->phys_size = inode->i_size;
  508. hip->fs_blocks = (inode->i_size + sb->s_blocksize - 1) >>
  509. sb->s_blocksize_bits;
  510. inode_set_bytes(inode, hip->fs_blocks << sb->s_blocksize_bits);
  511. hfsplus_mark_inode_dirty(inode, HFSPLUS_I_ALLOC_DIRTY);
  512. }