extents.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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(hfsplus_btree_key *k1, hfsplus_btree_key *k2)
  17. {
  18. __be32 k1id, k2id;
  19. __be32 k1s, k2s;
  20. k1id = k1->ext.cnid;
  21. k2id = k2->ext.cnid;
  22. if (k1id != k2id)
  23. return be32_to_cpu(k1id) < be32_to_cpu(k2id) ? -1 : 1;
  24. if (k1->ext.fork_type != k2->ext.fork_type)
  25. return k1->ext.fork_type < k2->ext.fork_type ? -1 : 1;
  26. k1s = k1->ext.start_block;
  27. k2s = k2->ext.start_block;
  28. if (k1s == k2s)
  29. return 0;
  30. return be32_to_cpu(k1s) < be32_to_cpu(k2s) ? -1 : 1;
  31. }
  32. static void hfsplus_ext_build_key(hfsplus_btree_key *key, u32 cnid,
  33. u32 block, u8 type)
  34. {
  35. key->key_len = cpu_to_be16(HFSPLUS_EXT_KEYLEN - 2);
  36. key->ext.cnid = cpu_to_be32(cnid);
  37. key->ext.start_block = cpu_to_be32(block);
  38. key->ext.fork_type = type;
  39. key->ext.pad = 0;
  40. }
  41. static u32 hfsplus_ext_find_block(struct hfsplus_extent *ext, u32 off)
  42. {
  43. int i;
  44. u32 count;
  45. for (i = 0; i < 8; ext++, i++) {
  46. count = be32_to_cpu(ext->block_count);
  47. if (off < count)
  48. return be32_to_cpu(ext->start_block) + off;
  49. off -= count;
  50. }
  51. /* panic? */
  52. return 0;
  53. }
  54. static int hfsplus_ext_block_count(struct hfsplus_extent *ext)
  55. {
  56. int i;
  57. u32 count = 0;
  58. for (i = 0; i < 8; ext++, i++)
  59. count += be32_to_cpu(ext->block_count);
  60. return count;
  61. }
  62. static u32 hfsplus_ext_lastblock(struct hfsplus_extent *ext)
  63. {
  64. int i;
  65. ext += 7;
  66. for (i = 0; i < 7; ext--, i++)
  67. if (ext->block_count)
  68. break;
  69. return be32_to_cpu(ext->start_block) + be32_to_cpu(ext->block_count);
  70. }
  71. static void __hfsplus_ext_write_extent(struct inode *inode, struct hfs_find_data *fd)
  72. {
  73. int res;
  74. hfsplus_ext_build_key(fd->search_key, inode->i_ino, HFSPLUS_I(inode).cached_start,
  75. HFSPLUS_IS_RSRC(inode) ? HFSPLUS_TYPE_RSRC : HFSPLUS_TYPE_DATA);
  76. res = hfs_brec_find(fd);
  77. if (HFSPLUS_I(inode).flags & HFSPLUS_FLG_EXT_NEW) {
  78. if (res != -ENOENT)
  79. return;
  80. hfs_brec_insert(fd, HFSPLUS_I(inode).cached_extents, sizeof(hfsplus_extent_rec));
  81. HFSPLUS_I(inode).flags &= ~(HFSPLUS_FLG_EXT_DIRTY | HFSPLUS_FLG_EXT_NEW);
  82. } else {
  83. if (res)
  84. return;
  85. hfs_bnode_write(fd->bnode, HFSPLUS_I(inode).cached_extents, fd->entryoffset, fd->entrylength);
  86. HFSPLUS_I(inode).flags &= ~HFSPLUS_FLG_EXT_DIRTY;
  87. }
  88. }
  89. void hfsplus_ext_write_extent(struct inode *inode)
  90. {
  91. if (HFSPLUS_I(inode).flags & HFSPLUS_FLG_EXT_DIRTY) {
  92. struct hfs_find_data fd;
  93. hfs_find_init(HFSPLUS_SB(inode->i_sb).ext_tree, &fd);
  94. __hfsplus_ext_write_extent(inode, &fd);
  95. hfs_find_exit(&fd);
  96. }
  97. }
  98. static inline int __hfsplus_ext_read_extent(struct hfs_find_data *fd,
  99. struct hfsplus_extent *extent,
  100. u32 cnid, u32 block, u8 type)
  101. {
  102. int res;
  103. hfsplus_ext_build_key(fd->search_key, cnid, block, type);
  104. fd->key->ext.cnid = 0;
  105. res = hfs_brec_find(fd);
  106. if (res && res != -ENOENT)
  107. return res;
  108. if (fd->key->ext.cnid != fd->search_key->ext.cnid ||
  109. fd->key->ext.fork_type != fd->search_key->ext.fork_type)
  110. return -ENOENT;
  111. if (fd->entrylength != sizeof(hfsplus_extent_rec))
  112. return -EIO;
  113. hfs_bnode_read(fd->bnode, extent, fd->entryoffset, sizeof(hfsplus_extent_rec));
  114. return 0;
  115. }
  116. static inline int __hfsplus_ext_cache_extent(struct hfs_find_data *fd, struct inode *inode, u32 block)
  117. {
  118. int res;
  119. if (HFSPLUS_I(inode).flags & HFSPLUS_FLG_EXT_DIRTY)
  120. __hfsplus_ext_write_extent(inode, fd);
  121. res = __hfsplus_ext_read_extent(fd, HFSPLUS_I(inode).cached_extents, inode->i_ino,
  122. block, HFSPLUS_IS_RSRC(inode) ? HFSPLUS_TYPE_RSRC : HFSPLUS_TYPE_DATA);
  123. if (!res) {
  124. HFSPLUS_I(inode).cached_start = be32_to_cpu(fd->key->ext.start_block);
  125. HFSPLUS_I(inode).cached_blocks = hfsplus_ext_block_count(HFSPLUS_I(inode).cached_extents);
  126. } else {
  127. HFSPLUS_I(inode).cached_start = HFSPLUS_I(inode).cached_blocks = 0;
  128. HFSPLUS_I(inode).flags &= ~(HFSPLUS_FLG_EXT_DIRTY | HFSPLUS_FLG_EXT_NEW);
  129. }
  130. return res;
  131. }
  132. static int hfsplus_ext_read_extent(struct inode *inode, u32 block)
  133. {
  134. struct hfs_find_data fd;
  135. int res;
  136. if (block >= HFSPLUS_I(inode).cached_start &&
  137. block < HFSPLUS_I(inode).cached_start + HFSPLUS_I(inode).cached_blocks)
  138. return 0;
  139. hfs_find_init(HFSPLUS_SB(inode->i_sb).ext_tree, &fd);
  140. res = __hfsplus_ext_cache_extent(&fd, inode, block);
  141. hfs_find_exit(&fd);
  142. return res;
  143. }
  144. /* Get a block at iblock for inode, possibly allocating if create */
  145. int hfsplus_get_block(struct inode *inode, sector_t iblock,
  146. struct buffer_head *bh_result, int create)
  147. {
  148. struct super_block *sb;
  149. int res = -EIO;
  150. u32 ablock, dblock, mask;
  151. int shift;
  152. sb = inode->i_sb;
  153. /* Convert inode block to disk allocation block */
  154. shift = HFSPLUS_SB(sb).alloc_blksz_shift - sb->s_blocksize_bits;
  155. ablock = iblock >> HFSPLUS_SB(sb).fs_shift;
  156. if (iblock >= HFSPLUS_I(inode).fs_blocks) {
  157. if (iblock > HFSPLUS_I(inode).fs_blocks || !create)
  158. return -EIO;
  159. if (ablock >= HFSPLUS_I(inode).alloc_blocks) {
  160. res = hfsplus_file_extend(inode);
  161. if (res)
  162. return res;
  163. }
  164. } else
  165. create = 0;
  166. if (ablock < HFSPLUS_I(inode).first_blocks) {
  167. dblock = hfsplus_ext_find_block(HFSPLUS_I(inode).first_extents, ablock);
  168. goto done;
  169. }
  170. down(&HFSPLUS_I(inode).extents_lock);
  171. res = hfsplus_ext_read_extent(inode, ablock);
  172. if (!res) {
  173. dblock = hfsplus_ext_find_block(HFSPLUS_I(inode).cached_extents, ablock -
  174. HFSPLUS_I(inode).cached_start);
  175. } else {
  176. up(&HFSPLUS_I(inode).extents_lock);
  177. return -EIO;
  178. }
  179. up(&HFSPLUS_I(inode).extents_lock);
  180. done:
  181. dprint(DBG_EXTENT, "get_block(%lu): %llu - %u\n", inode->i_ino, (long long)iblock, dblock);
  182. mask = (1 << HFSPLUS_SB(sb).fs_shift) - 1;
  183. map_bh(bh_result, sb, (dblock << HFSPLUS_SB(sb).fs_shift) + HFSPLUS_SB(sb).blockoffset + (iblock & mask));
  184. if (create) {
  185. set_buffer_new(bh_result);
  186. HFSPLUS_I(inode).phys_size += sb->s_blocksize;
  187. HFSPLUS_I(inode).fs_blocks++;
  188. inode_add_bytes(inode, sb->s_blocksize);
  189. mark_inode_dirty(inode);
  190. }
  191. return 0;
  192. }
  193. static void hfsplus_dump_extent(struct hfsplus_extent *extent)
  194. {
  195. int i;
  196. dprint(DBG_EXTENT, " ");
  197. for (i = 0; i < 8; i++)
  198. dprint(DBG_EXTENT, " %u:%u", be32_to_cpu(extent[i].start_block),
  199. be32_to_cpu(extent[i].block_count));
  200. dprint(DBG_EXTENT, "\n");
  201. }
  202. static int hfsplus_add_extent(struct hfsplus_extent *extent, u32 offset,
  203. u32 alloc_block, u32 block_count)
  204. {
  205. u32 count, start;
  206. int i;
  207. hfsplus_dump_extent(extent);
  208. for (i = 0; i < 8; extent++, i++) {
  209. count = be32_to_cpu(extent->block_count);
  210. if (offset == count) {
  211. start = be32_to_cpu(extent->start_block);
  212. if (alloc_block != start + count) {
  213. if (++i >= 8)
  214. return -ENOSPC;
  215. extent++;
  216. extent->start_block = cpu_to_be32(alloc_block);
  217. } else
  218. block_count += count;
  219. extent->block_count = cpu_to_be32(block_count);
  220. return 0;
  221. } else if (offset < count)
  222. break;
  223. offset -= count;
  224. }
  225. /* panic? */
  226. return -EIO;
  227. }
  228. static int hfsplus_free_extents(struct super_block *sb,
  229. struct hfsplus_extent *extent,
  230. u32 offset, u32 block_nr)
  231. {
  232. u32 count, start;
  233. int i;
  234. hfsplus_dump_extent(extent);
  235. for (i = 0; i < 8; extent++, i++) {
  236. count = be32_to_cpu(extent->block_count);
  237. if (offset == count)
  238. goto found;
  239. else if (offset < count)
  240. break;
  241. offset -= count;
  242. }
  243. /* panic? */
  244. return -EIO;
  245. found:
  246. for (;;) {
  247. start = be32_to_cpu(extent->start_block);
  248. if (count <= block_nr) {
  249. hfsplus_block_free(sb, start, count);
  250. extent->block_count = 0;
  251. extent->start_block = 0;
  252. block_nr -= count;
  253. } else {
  254. count -= block_nr;
  255. hfsplus_block_free(sb, start + count, block_nr);
  256. extent->block_count = cpu_to_be32(count);
  257. block_nr = 0;
  258. }
  259. if (!block_nr || !i)
  260. return 0;
  261. i--;
  262. extent--;
  263. count = be32_to_cpu(extent->block_count);
  264. }
  265. }
  266. int hfsplus_free_fork(struct super_block *sb, u32 cnid, struct hfsplus_fork_raw *fork, int type)
  267. {
  268. struct hfs_find_data fd;
  269. hfsplus_extent_rec ext_entry;
  270. u32 total_blocks, blocks, start;
  271. int res, i;
  272. total_blocks = be32_to_cpu(fork->total_blocks);
  273. if (!total_blocks)
  274. return 0;
  275. blocks = 0;
  276. for (i = 0; i < 8; i++)
  277. blocks += be32_to_cpu(fork->extents[i].block_count);
  278. res = hfsplus_free_extents(sb, fork->extents, blocks, blocks);
  279. if (res)
  280. return res;
  281. if (total_blocks == blocks)
  282. return 0;
  283. hfs_find_init(HFSPLUS_SB(sb).ext_tree, &fd);
  284. do {
  285. res = __hfsplus_ext_read_extent(&fd, ext_entry, cnid,
  286. total_blocks, type);
  287. if (res)
  288. break;
  289. start = be32_to_cpu(fd.key->ext.start_block);
  290. hfsplus_free_extents(sb, ext_entry,
  291. total_blocks - start,
  292. total_blocks);
  293. hfs_brec_remove(&fd);
  294. total_blocks = start;
  295. } while (total_blocks > blocks);
  296. hfs_find_exit(&fd);
  297. return res;
  298. }
  299. int hfsplus_file_extend(struct inode *inode)
  300. {
  301. struct super_block *sb = inode->i_sb;
  302. u32 start, len, goal;
  303. int res;
  304. if (HFSPLUS_SB(sb).alloc_file->i_size * 8 < HFSPLUS_SB(sb).total_blocks - HFSPLUS_SB(sb).free_blocks + 8) {
  305. // extend alloc file
  306. printk("extend alloc file! (%Lu,%u,%u)\n", HFSPLUS_SB(sb).alloc_file->i_size * 8,
  307. HFSPLUS_SB(sb).total_blocks, HFSPLUS_SB(sb).free_blocks);
  308. return -ENOSPC;
  309. //BUG();
  310. }
  311. down(&HFSPLUS_I(inode).extents_lock);
  312. if (HFSPLUS_I(inode).alloc_blocks == HFSPLUS_I(inode).first_blocks)
  313. goal = hfsplus_ext_lastblock(HFSPLUS_I(inode).first_extents);
  314. else {
  315. res = hfsplus_ext_read_extent(inode, HFSPLUS_I(inode).alloc_blocks);
  316. if (res)
  317. goto out;
  318. goal = hfsplus_ext_lastblock(HFSPLUS_I(inode).cached_extents);
  319. }
  320. len = HFSPLUS_I(inode).clump_blocks;
  321. start = hfsplus_block_allocate(sb, HFSPLUS_SB(sb).total_blocks, goal, &len);
  322. if (start >= HFSPLUS_SB(sb).total_blocks) {
  323. start = hfsplus_block_allocate(sb, goal, 0, &len);
  324. if (start >= goal) {
  325. res = -ENOSPC;
  326. goto out;
  327. }
  328. }
  329. dprint(DBG_EXTENT, "extend %lu: %u,%u\n", inode->i_ino, start, len);
  330. if (HFSPLUS_I(inode).alloc_blocks <= HFSPLUS_I(inode).first_blocks) {
  331. if (!HFSPLUS_I(inode).first_blocks) {
  332. dprint(DBG_EXTENT, "first extents\n");
  333. /* no extents yet */
  334. HFSPLUS_I(inode).first_extents[0].start_block = cpu_to_be32(start);
  335. HFSPLUS_I(inode).first_extents[0].block_count = cpu_to_be32(len);
  336. res = 0;
  337. } else {
  338. /* try to append to extents in inode */
  339. res = hfsplus_add_extent(HFSPLUS_I(inode).first_extents,
  340. HFSPLUS_I(inode).alloc_blocks,
  341. start, len);
  342. if (res == -ENOSPC)
  343. goto insert_extent;
  344. }
  345. if (!res) {
  346. hfsplus_dump_extent(HFSPLUS_I(inode).first_extents);
  347. HFSPLUS_I(inode).first_blocks += len;
  348. }
  349. } else {
  350. res = hfsplus_add_extent(HFSPLUS_I(inode).cached_extents,
  351. HFSPLUS_I(inode).alloc_blocks -
  352. HFSPLUS_I(inode).cached_start,
  353. start, len);
  354. if (!res) {
  355. hfsplus_dump_extent(HFSPLUS_I(inode).cached_extents);
  356. HFSPLUS_I(inode).flags |= HFSPLUS_FLG_EXT_DIRTY;
  357. HFSPLUS_I(inode).cached_blocks += len;
  358. } else if (res == -ENOSPC)
  359. goto insert_extent;
  360. }
  361. out:
  362. up(&HFSPLUS_I(inode).extents_lock);
  363. if (!res) {
  364. HFSPLUS_I(inode).alloc_blocks += len;
  365. mark_inode_dirty(inode);
  366. }
  367. return res;
  368. insert_extent:
  369. dprint(DBG_EXTENT, "insert new extent\n");
  370. hfsplus_ext_write_extent(inode);
  371. memset(HFSPLUS_I(inode).cached_extents, 0, sizeof(hfsplus_extent_rec));
  372. HFSPLUS_I(inode).cached_extents[0].start_block = cpu_to_be32(start);
  373. HFSPLUS_I(inode).cached_extents[0].block_count = cpu_to_be32(len);
  374. hfsplus_dump_extent(HFSPLUS_I(inode).cached_extents);
  375. HFSPLUS_I(inode).flags |= HFSPLUS_FLG_EXT_DIRTY | HFSPLUS_FLG_EXT_NEW;
  376. HFSPLUS_I(inode).cached_start = HFSPLUS_I(inode).alloc_blocks;
  377. HFSPLUS_I(inode).cached_blocks = len;
  378. res = 0;
  379. goto out;
  380. }
  381. void hfsplus_file_truncate(struct inode *inode)
  382. {
  383. struct super_block *sb = inode->i_sb;
  384. struct hfs_find_data fd;
  385. u32 alloc_cnt, blk_cnt, start;
  386. int res;
  387. dprint(DBG_INODE, "truncate: %lu, %Lu -> %Lu\n", inode->i_ino,
  388. (long long)HFSPLUS_I(inode).phys_size, inode->i_size);
  389. if (inode->i_size > HFSPLUS_I(inode).phys_size) {
  390. struct address_space *mapping = inode->i_mapping;
  391. struct page *page;
  392. u32 size = inode->i_size - 1;
  393. int res;
  394. page = grab_cache_page(mapping, size >> PAGE_CACHE_SHIFT);
  395. if (!page)
  396. return;
  397. size &= PAGE_CACHE_SIZE - 1;
  398. size++;
  399. res = mapping->a_ops->prepare_write(NULL, page, size, size);
  400. if (!res)
  401. res = mapping->a_ops->commit_write(NULL, page, size, size);
  402. if (res)
  403. inode->i_size = HFSPLUS_I(inode).phys_size;
  404. unlock_page(page);
  405. page_cache_release(page);
  406. mark_inode_dirty(inode);
  407. return;
  408. } else if (inode->i_size == HFSPLUS_I(inode).phys_size)
  409. return;
  410. blk_cnt = (inode->i_size + HFSPLUS_SB(sb).alloc_blksz - 1) >> HFSPLUS_SB(sb).alloc_blksz_shift;
  411. alloc_cnt = HFSPLUS_I(inode).alloc_blocks;
  412. if (blk_cnt == alloc_cnt)
  413. goto out;
  414. down(&HFSPLUS_I(inode).extents_lock);
  415. hfs_find_init(HFSPLUS_SB(sb).ext_tree, &fd);
  416. while (1) {
  417. if (alloc_cnt == HFSPLUS_I(inode).first_blocks) {
  418. hfsplus_free_extents(sb, HFSPLUS_I(inode).first_extents,
  419. alloc_cnt, alloc_cnt - blk_cnt);
  420. hfsplus_dump_extent(HFSPLUS_I(inode).first_extents);
  421. HFSPLUS_I(inode).first_blocks = blk_cnt;
  422. break;
  423. }
  424. res = __hfsplus_ext_cache_extent(&fd, inode, alloc_cnt);
  425. if (res)
  426. break;
  427. start = HFSPLUS_I(inode).cached_start;
  428. hfsplus_free_extents(sb, HFSPLUS_I(inode).cached_extents,
  429. alloc_cnt - start, alloc_cnt - blk_cnt);
  430. hfsplus_dump_extent(HFSPLUS_I(inode).cached_extents);
  431. if (blk_cnt > start) {
  432. HFSPLUS_I(inode).flags |= HFSPLUS_FLG_EXT_DIRTY;
  433. break;
  434. }
  435. alloc_cnt = start;
  436. HFSPLUS_I(inode).cached_start = HFSPLUS_I(inode).cached_blocks = 0;
  437. HFSPLUS_I(inode).flags &= ~(HFSPLUS_FLG_EXT_DIRTY | HFSPLUS_FLG_EXT_NEW);
  438. hfs_brec_remove(&fd);
  439. }
  440. hfs_find_exit(&fd);
  441. up(&HFSPLUS_I(inode).extents_lock);
  442. HFSPLUS_I(inode).alloc_blocks = blk_cnt;
  443. out:
  444. HFSPLUS_I(inode).phys_size = inode->i_size;
  445. HFSPLUS_I(inode).fs_blocks = (inode->i_size + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
  446. inode_set_bytes(inode, HFSPLUS_I(inode).fs_blocks << sb->s_blocksize_bits);
  447. mark_inode_dirty(inode);
  448. }