extents.c 14 KB

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