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(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, struct hfs_find_data *fd)
  73. {
  74. int res;
  75. hfsplus_ext_build_key(fd->search_key, inode->i_ino, HFSPLUS_I(inode).cached_start,
  76. HFSPLUS_IS_RSRC(inode) ? HFSPLUS_TYPE_RSRC : HFSPLUS_TYPE_DATA);
  77. res = hfs_brec_find(fd);
  78. if (HFSPLUS_I(inode).flags & HFSPLUS_FLG_EXT_NEW) {
  79. if (res != -ENOENT)
  80. return;
  81. hfs_brec_insert(fd, HFSPLUS_I(inode).cached_extents, sizeof(hfsplus_extent_rec));
  82. HFSPLUS_I(inode).flags &= ~(HFSPLUS_FLG_EXT_DIRTY | HFSPLUS_FLG_EXT_NEW);
  83. } else {
  84. if (res)
  85. return;
  86. hfs_bnode_write(fd->bnode, HFSPLUS_I(inode).cached_extents, fd->entryoffset, fd->entrylength);
  87. HFSPLUS_I(inode).flags &= ~HFSPLUS_FLG_EXT_DIRTY;
  88. }
  89. }
  90. void hfsplus_ext_write_extent(struct inode *inode)
  91. {
  92. if (HFSPLUS_I(inode).flags & HFSPLUS_FLG_EXT_DIRTY) {
  93. struct hfs_find_data fd;
  94. hfs_find_init(HFSPLUS_SB(inode->i_sb).ext_tree, &fd);
  95. __hfsplus_ext_write_extent(inode, &fd);
  96. hfs_find_exit(&fd);
  97. }
  98. }
  99. static inline int __hfsplus_ext_read_extent(struct hfs_find_data *fd,
  100. struct hfsplus_extent *extent,
  101. u32 cnid, u32 block, u8 type)
  102. {
  103. int res;
  104. hfsplus_ext_build_key(fd->search_key, cnid, block, type);
  105. fd->key->ext.cnid = 0;
  106. res = hfs_brec_find(fd);
  107. if (res && res != -ENOENT)
  108. return res;
  109. if (fd->key->ext.cnid != fd->search_key->ext.cnid ||
  110. fd->key->ext.fork_type != fd->search_key->ext.fork_type)
  111. return -ENOENT;
  112. if (fd->entrylength != sizeof(hfsplus_extent_rec))
  113. return -EIO;
  114. hfs_bnode_read(fd->bnode, extent, fd->entryoffset, sizeof(hfsplus_extent_rec));
  115. return 0;
  116. }
  117. static inline int __hfsplus_ext_cache_extent(struct hfs_find_data *fd, struct inode *inode, u32 block)
  118. {
  119. int res;
  120. if (HFSPLUS_I(inode).flags & HFSPLUS_FLG_EXT_DIRTY)
  121. __hfsplus_ext_write_extent(inode, fd);
  122. res = __hfsplus_ext_read_extent(fd, HFSPLUS_I(inode).cached_extents, inode->i_ino,
  123. block, HFSPLUS_IS_RSRC(inode) ? HFSPLUS_TYPE_RSRC : HFSPLUS_TYPE_DATA);
  124. if (!res) {
  125. HFSPLUS_I(inode).cached_start = be32_to_cpu(fd->key->ext.start_block);
  126. HFSPLUS_I(inode).cached_blocks = hfsplus_ext_block_count(HFSPLUS_I(inode).cached_extents);
  127. } else {
  128. HFSPLUS_I(inode).cached_start = HFSPLUS_I(inode).cached_blocks = 0;
  129. HFSPLUS_I(inode).flags &= ~(HFSPLUS_FLG_EXT_DIRTY | HFSPLUS_FLG_EXT_NEW);
  130. }
  131. return res;
  132. }
  133. static int hfsplus_ext_read_extent(struct inode *inode, u32 block)
  134. {
  135. struct hfs_find_data fd;
  136. int res;
  137. if (block >= HFSPLUS_I(inode).cached_start &&
  138. block < HFSPLUS_I(inode).cached_start + HFSPLUS_I(inode).cached_blocks)
  139. return 0;
  140. hfs_find_init(HFSPLUS_SB(inode->i_sb).ext_tree, &fd);
  141. res = __hfsplus_ext_cache_extent(&fd, inode, block);
  142. hfs_find_exit(&fd);
  143. return res;
  144. }
  145. /* Get a block at iblock for inode, possibly allocating if create */
  146. int hfsplus_get_block(struct inode *inode, sector_t iblock,
  147. struct buffer_head *bh_result, int create)
  148. {
  149. struct super_block *sb;
  150. int res = -EIO;
  151. u32 ablock, dblock, mask;
  152. int shift;
  153. sb = inode->i_sb;
  154. /* Convert inode block to disk allocation block */
  155. shift = HFSPLUS_SB(sb).alloc_blksz_shift - sb->s_blocksize_bits;
  156. ablock = iblock >> HFSPLUS_SB(sb).fs_shift;
  157. if (iblock >= HFSPLUS_I(inode).fs_blocks) {
  158. if (iblock > HFSPLUS_I(inode).fs_blocks || !create)
  159. return -EIO;
  160. if (ablock >= HFSPLUS_I(inode).alloc_blocks) {
  161. res = hfsplus_file_extend(inode);
  162. if (res)
  163. return res;
  164. }
  165. } else
  166. create = 0;
  167. if (ablock < HFSPLUS_I(inode).first_blocks) {
  168. dblock = hfsplus_ext_find_block(HFSPLUS_I(inode).first_extents, ablock);
  169. goto done;
  170. }
  171. down(&HFSPLUS_I(inode).extents_lock);
  172. res = hfsplus_ext_read_extent(inode, ablock);
  173. if (!res) {
  174. dblock = hfsplus_ext_find_block(HFSPLUS_I(inode).cached_extents, ablock -
  175. HFSPLUS_I(inode).cached_start);
  176. } else {
  177. up(&HFSPLUS_I(inode).extents_lock);
  178. return -EIO;
  179. }
  180. up(&HFSPLUS_I(inode).extents_lock);
  181. done:
  182. dprint(DBG_EXTENT, "get_block(%lu): %llu - %u\n", inode->i_ino, (long long)iblock, dblock);
  183. mask = (1 << HFSPLUS_SB(sb).fs_shift) - 1;
  184. map_bh(bh_result, sb, (dblock << HFSPLUS_SB(sb).fs_shift) + HFSPLUS_SB(sb).blockoffset + (iblock & mask));
  185. if (create) {
  186. set_buffer_new(bh_result);
  187. HFSPLUS_I(inode).phys_size += sb->s_blocksize;
  188. HFSPLUS_I(inode).fs_blocks++;
  189. inode_add_bytes(inode, sb->s_blocksize);
  190. mark_inode_dirty(inode);
  191. }
  192. return 0;
  193. }
  194. static void hfsplus_dump_extent(struct hfsplus_extent *extent)
  195. {
  196. int i;
  197. dprint(DBG_EXTENT, " ");
  198. for (i = 0; i < 8; i++)
  199. dprint(DBG_EXTENT, " %u:%u", be32_to_cpu(extent[i].start_block),
  200. be32_to_cpu(extent[i].block_count));
  201. dprint(DBG_EXTENT, "\n");
  202. }
  203. static int hfsplus_add_extent(struct hfsplus_extent *extent, u32 offset,
  204. u32 alloc_block, u32 block_count)
  205. {
  206. u32 count, start;
  207. int i;
  208. hfsplus_dump_extent(extent);
  209. for (i = 0; i < 8; extent++, i++) {
  210. count = be32_to_cpu(extent->block_count);
  211. if (offset == count) {
  212. start = be32_to_cpu(extent->start_block);
  213. if (alloc_block != start + count) {
  214. if (++i >= 8)
  215. return -ENOSPC;
  216. extent++;
  217. extent->start_block = cpu_to_be32(alloc_block);
  218. } else
  219. block_count += count;
  220. extent->block_count = cpu_to_be32(block_count);
  221. return 0;
  222. } else if (offset < count)
  223. break;
  224. offset -= count;
  225. }
  226. /* panic? */
  227. return -EIO;
  228. }
  229. static int hfsplus_free_extents(struct super_block *sb,
  230. struct hfsplus_extent *extent,
  231. u32 offset, u32 block_nr)
  232. {
  233. u32 count, start;
  234. int i;
  235. hfsplus_dump_extent(extent);
  236. for (i = 0; i < 8; extent++, i++) {
  237. count = be32_to_cpu(extent->block_count);
  238. if (offset == count)
  239. goto found;
  240. else if (offset < count)
  241. break;
  242. offset -= count;
  243. }
  244. /* panic? */
  245. return -EIO;
  246. found:
  247. for (;;) {
  248. start = be32_to_cpu(extent->start_block);
  249. if (count <= block_nr) {
  250. hfsplus_block_free(sb, start, count);
  251. extent->block_count = 0;
  252. extent->start_block = 0;
  253. block_nr -= count;
  254. } else {
  255. count -= block_nr;
  256. hfsplus_block_free(sb, start + count, block_nr);
  257. extent->block_count = cpu_to_be32(count);
  258. block_nr = 0;
  259. }
  260. if (!block_nr || !i)
  261. return 0;
  262. i--;
  263. extent--;
  264. count = be32_to_cpu(extent->block_count);
  265. }
  266. }
  267. int hfsplus_free_fork(struct super_block *sb, u32 cnid, struct hfsplus_fork_raw *fork, int type)
  268. {
  269. struct hfs_find_data fd;
  270. hfsplus_extent_rec ext_entry;
  271. u32 total_blocks, blocks, start;
  272. int res, i;
  273. total_blocks = be32_to_cpu(fork->total_blocks);
  274. if (!total_blocks)
  275. return 0;
  276. blocks = 0;
  277. for (i = 0; i < 8; i++)
  278. blocks += be32_to_cpu(fork->extents[i].block_count);
  279. res = hfsplus_free_extents(sb, fork->extents, blocks, blocks);
  280. if (res)
  281. return res;
  282. if (total_blocks == blocks)
  283. return 0;
  284. hfs_find_init(HFSPLUS_SB(sb).ext_tree, &fd);
  285. do {
  286. res = __hfsplus_ext_read_extent(&fd, ext_entry, cnid,
  287. total_blocks, type);
  288. if (res)
  289. break;
  290. start = be32_to_cpu(fd.key->ext.start_block);
  291. hfsplus_free_extents(sb, ext_entry,
  292. total_blocks - start,
  293. total_blocks);
  294. hfs_brec_remove(&fd);
  295. total_blocks = start;
  296. } while (total_blocks > blocks);
  297. hfs_find_exit(&fd);
  298. return res;
  299. }
  300. int hfsplus_file_extend(struct inode *inode)
  301. {
  302. struct super_block *sb = inode->i_sb;
  303. u32 start, len, goal;
  304. int res;
  305. if (HFSPLUS_SB(sb).alloc_file->i_size * 8 < HFSPLUS_SB(sb).total_blocks - HFSPLUS_SB(sb).free_blocks + 8) {
  306. // extend alloc file
  307. printk(KERN_ERR "hfs: extend alloc file! (%Lu,%u,%u)\n", HFSPLUS_SB(sb).alloc_file->i_size * 8,
  308. HFSPLUS_SB(sb).total_blocks, HFSPLUS_SB(sb).free_blocks);
  309. return -ENOSPC;
  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. }