extents.c 14 KB

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