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. if (inode->i_ino == HFSPLUS_EXT_CNID)
  172. return -EIO;
  173. mutex_lock(&HFSPLUS_I(inode).extents_lock);
  174. res = hfsplus_ext_read_extent(inode, ablock);
  175. if (!res) {
  176. dblock = hfsplus_ext_find_block(HFSPLUS_I(inode).cached_extents, ablock -
  177. HFSPLUS_I(inode).cached_start);
  178. } else {
  179. mutex_unlock(&HFSPLUS_I(inode).extents_lock);
  180. return -EIO;
  181. }
  182. mutex_unlock(&HFSPLUS_I(inode).extents_lock);
  183. done:
  184. dprint(DBG_EXTENT, "get_block(%lu): %llu - %u\n", inode->i_ino, (long long)iblock, dblock);
  185. mask = (1 << HFSPLUS_SB(sb).fs_shift) - 1;
  186. map_bh(bh_result, sb, (dblock << HFSPLUS_SB(sb).fs_shift) + HFSPLUS_SB(sb).blockoffset + (iblock & mask));
  187. if (create) {
  188. set_buffer_new(bh_result);
  189. HFSPLUS_I(inode).phys_size += sb->s_blocksize;
  190. HFSPLUS_I(inode).fs_blocks++;
  191. inode_add_bytes(inode, sb->s_blocksize);
  192. mark_inode_dirty(inode);
  193. }
  194. return 0;
  195. }
  196. static void hfsplus_dump_extent(struct hfsplus_extent *extent)
  197. {
  198. int i;
  199. dprint(DBG_EXTENT, " ");
  200. for (i = 0; i < 8; i++)
  201. dprint(DBG_EXTENT, " %u:%u", be32_to_cpu(extent[i].start_block),
  202. be32_to_cpu(extent[i].block_count));
  203. dprint(DBG_EXTENT, "\n");
  204. }
  205. static int hfsplus_add_extent(struct hfsplus_extent *extent, u32 offset,
  206. u32 alloc_block, u32 block_count)
  207. {
  208. u32 count, start;
  209. int i;
  210. hfsplus_dump_extent(extent);
  211. for (i = 0; i < 8; extent++, i++) {
  212. count = be32_to_cpu(extent->block_count);
  213. if (offset == count) {
  214. start = be32_to_cpu(extent->start_block);
  215. if (alloc_block != start + count) {
  216. if (++i >= 8)
  217. return -ENOSPC;
  218. extent++;
  219. extent->start_block = cpu_to_be32(alloc_block);
  220. } else
  221. block_count += count;
  222. extent->block_count = cpu_to_be32(block_count);
  223. return 0;
  224. } else if (offset < count)
  225. break;
  226. offset -= count;
  227. }
  228. /* panic? */
  229. return -EIO;
  230. }
  231. static int hfsplus_free_extents(struct super_block *sb,
  232. struct hfsplus_extent *extent,
  233. u32 offset, u32 block_nr)
  234. {
  235. u32 count, start;
  236. int i;
  237. hfsplus_dump_extent(extent);
  238. for (i = 0; i < 8; extent++, i++) {
  239. count = be32_to_cpu(extent->block_count);
  240. if (offset == count)
  241. goto found;
  242. else if (offset < count)
  243. break;
  244. offset -= count;
  245. }
  246. /* panic? */
  247. return -EIO;
  248. found:
  249. for (;;) {
  250. start = be32_to_cpu(extent->start_block);
  251. if (count <= block_nr) {
  252. hfsplus_block_free(sb, start, count);
  253. extent->block_count = 0;
  254. extent->start_block = 0;
  255. block_nr -= count;
  256. } else {
  257. count -= block_nr;
  258. hfsplus_block_free(sb, start + count, block_nr);
  259. extent->block_count = cpu_to_be32(count);
  260. block_nr = 0;
  261. }
  262. if (!block_nr || !i)
  263. return 0;
  264. i--;
  265. extent--;
  266. count = be32_to_cpu(extent->block_count);
  267. }
  268. }
  269. int hfsplus_free_fork(struct super_block *sb, u32 cnid, struct hfsplus_fork_raw *fork, int type)
  270. {
  271. struct hfs_find_data fd;
  272. hfsplus_extent_rec ext_entry;
  273. u32 total_blocks, blocks, start;
  274. int res, i;
  275. total_blocks = be32_to_cpu(fork->total_blocks);
  276. if (!total_blocks)
  277. return 0;
  278. blocks = 0;
  279. for (i = 0; i < 8; i++)
  280. blocks += be32_to_cpu(fork->extents[i].block_count);
  281. res = hfsplus_free_extents(sb, fork->extents, blocks, blocks);
  282. if (res)
  283. return res;
  284. if (total_blocks == blocks)
  285. return 0;
  286. hfs_find_init(HFSPLUS_SB(sb).ext_tree, &fd);
  287. do {
  288. res = __hfsplus_ext_read_extent(&fd, ext_entry, cnid,
  289. total_blocks, type);
  290. if (res)
  291. break;
  292. start = be32_to_cpu(fd.key->ext.start_block);
  293. hfsplus_free_extents(sb, ext_entry,
  294. total_blocks - start,
  295. total_blocks);
  296. hfs_brec_remove(&fd);
  297. total_blocks = start;
  298. } while (total_blocks > blocks);
  299. hfs_find_exit(&fd);
  300. return res;
  301. }
  302. int hfsplus_file_extend(struct inode *inode)
  303. {
  304. struct super_block *sb = inode->i_sb;
  305. u32 start, len, goal;
  306. int res;
  307. if (HFSPLUS_SB(sb).alloc_file->i_size * 8 < HFSPLUS_SB(sb).total_blocks - HFSPLUS_SB(sb).free_blocks + 8) {
  308. // extend alloc file
  309. printk(KERN_ERR "hfs: extend alloc file! (%Lu,%u,%u)\n", HFSPLUS_SB(sb).alloc_file->i_size * 8,
  310. HFSPLUS_SB(sb).total_blocks, HFSPLUS_SB(sb).free_blocks);
  311. return -ENOSPC;
  312. }
  313. mutex_lock(&HFSPLUS_I(inode).extents_lock);
  314. if (HFSPLUS_I(inode).alloc_blocks == HFSPLUS_I(inode).first_blocks)
  315. goal = hfsplus_ext_lastblock(HFSPLUS_I(inode).first_extents);
  316. else {
  317. res = hfsplus_ext_read_extent(inode, HFSPLUS_I(inode).alloc_blocks);
  318. if (res)
  319. goto out;
  320. goal = hfsplus_ext_lastblock(HFSPLUS_I(inode).cached_extents);
  321. }
  322. len = HFSPLUS_I(inode).clump_blocks;
  323. start = hfsplus_block_allocate(sb, HFSPLUS_SB(sb).total_blocks, goal, &len);
  324. if (start >= HFSPLUS_SB(sb).total_blocks) {
  325. start = hfsplus_block_allocate(sb, goal, 0, &len);
  326. if (start >= goal) {
  327. res = -ENOSPC;
  328. goto out;
  329. }
  330. }
  331. dprint(DBG_EXTENT, "extend %lu: %u,%u\n", inode->i_ino, start, len);
  332. if (HFSPLUS_I(inode).alloc_blocks <= HFSPLUS_I(inode).first_blocks) {
  333. if (!HFSPLUS_I(inode).first_blocks) {
  334. dprint(DBG_EXTENT, "first extents\n");
  335. /* no extents yet */
  336. HFSPLUS_I(inode).first_extents[0].start_block = cpu_to_be32(start);
  337. HFSPLUS_I(inode).first_extents[0].block_count = cpu_to_be32(len);
  338. res = 0;
  339. } else {
  340. /* try to append to extents in inode */
  341. res = hfsplus_add_extent(HFSPLUS_I(inode).first_extents,
  342. HFSPLUS_I(inode).alloc_blocks,
  343. start, len);
  344. if (res == -ENOSPC)
  345. goto insert_extent;
  346. }
  347. if (!res) {
  348. hfsplus_dump_extent(HFSPLUS_I(inode).first_extents);
  349. HFSPLUS_I(inode).first_blocks += len;
  350. }
  351. } else {
  352. res = hfsplus_add_extent(HFSPLUS_I(inode).cached_extents,
  353. HFSPLUS_I(inode).alloc_blocks -
  354. HFSPLUS_I(inode).cached_start,
  355. start, len);
  356. if (!res) {
  357. hfsplus_dump_extent(HFSPLUS_I(inode).cached_extents);
  358. HFSPLUS_I(inode).flags |= HFSPLUS_FLG_EXT_DIRTY;
  359. HFSPLUS_I(inode).cached_blocks += len;
  360. } else if (res == -ENOSPC)
  361. goto insert_extent;
  362. }
  363. out:
  364. mutex_unlock(&HFSPLUS_I(inode).extents_lock);
  365. if (!res) {
  366. HFSPLUS_I(inode).alloc_blocks += len;
  367. mark_inode_dirty(inode);
  368. }
  369. return res;
  370. insert_extent:
  371. dprint(DBG_EXTENT, "insert new extent\n");
  372. hfsplus_ext_write_extent(inode);
  373. memset(HFSPLUS_I(inode).cached_extents, 0, sizeof(hfsplus_extent_rec));
  374. HFSPLUS_I(inode).cached_extents[0].start_block = cpu_to_be32(start);
  375. HFSPLUS_I(inode).cached_extents[0].block_count = cpu_to_be32(len);
  376. hfsplus_dump_extent(HFSPLUS_I(inode).cached_extents);
  377. HFSPLUS_I(inode).flags |= HFSPLUS_FLG_EXT_DIRTY | HFSPLUS_FLG_EXT_NEW;
  378. HFSPLUS_I(inode).cached_start = HFSPLUS_I(inode).alloc_blocks;
  379. HFSPLUS_I(inode).cached_blocks = len;
  380. res = 0;
  381. goto out;
  382. }
  383. void hfsplus_file_truncate(struct inode *inode)
  384. {
  385. struct super_block *sb = inode->i_sb;
  386. struct hfs_find_data fd;
  387. u32 alloc_cnt, blk_cnt, start;
  388. int res;
  389. dprint(DBG_INODE, "truncate: %lu, %Lu -> %Lu\n", inode->i_ino,
  390. (long long)HFSPLUS_I(inode).phys_size, inode->i_size);
  391. if (inode->i_size > HFSPLUS_I(inode).phys_size) {
  392. struct address_space *mapping = inode->i_mapping;
  393. struct page *page;
  394. void *fsdata;
  395. u32 size = inode->i_size;
  396. int res;
  397. res = pagecache_write_begin(NULL, mapping, size, 0,
  398. AOP_FLAG_UNINTERRUPTIBLE,
  399. &page, &fsdata);
  400. if (res)
  401. return;
  402. res = pagecache_write_end(NULL, mapping, size, 0, 0, page, fsdata);
  403. if (res < 0)
  404. return;
  405. mark_inode_dirty(inode);
  406. return;
  407. } else if (inode->i_size == HFSPLUS_I(inode).phys_size)
  408. return;
  409. blk_cnt = (inode->i_size + HFSPLUS_SB(sb).alloc_blksz - 1) >> HFSPLUS_SB(sb).alloc_blksz_shift;
  410. alloc_cnt = HFSPLUS_I(inode).alloc_blocks;
  411. if (blk_cnt == alloc_cnt)
  412. goto out;
  413. mutex_lock(&HFSPLUS_I(inode).extents_lock);
  414. hfs_find_init(HFSPLUS_SB(sb).ext_tree, &fd);
  415. while (1) {
  416. if (alloc_cnt == HFSPLUS_I(inode).first_blocks) {
  417. hfsplus_free_extents(sb, HFSPLUS_I(inode).first_extents,
  418. alloc_cnt, alloc_cnt - blk_cnt);
  419. hfsplus_dump_extent(HFSPLUS_I(inode).first_extents);
  420. HFSPLUS_I(inode).first_blocks = blk_cnt;
  421. break;
  422. }
  423. res = __hfsplus_ext_cache_extent(&fd, inode, alloc_cnt);
  424. if (res)
  425. break;
  426. start = HFSPLUS_I(inode).cached_start;
  427. hfsplus_free_extents(sb, HFSPLUS_I(inode).cached_extents,
  428. alloc_cnt - start, alloc_cnt - blk_cnt);
  429. hfsplus_dump_extent(HFSPLUS_I(inode).cached_extents);
  430. if (blk_cnt > start) {
  431. HFSPLUS_I(inode).flags |= HFSPLUS_FLG_EXT_DIRTY;
  432. break;
  433. }
  434. alloc_cnt = start;
  435. HFSPLUS_I(inode).cached_start = HFSPLUS_I(inode).cached_blocks = 0;
  436. HFSPLUS_I(inode).flags &= ~(HFSPLUS_FLG_EXT_DIRTY | HFSPLUS_FLG_EXT_NEW);
  437. hfs_brec_remove(&fd);
  438. }
  439. hfs_find_exit(&fd);
  440. mutex_unlock(&HFSPLUS_I(inode).extents_lock);
  441. HFSPLUS_I(inode).alloc_blocks = blk_cnt;
  442. out:
  443. HFSPLUS_I(inode).phys_size = inode->i_size;
  444. HFSPLUS_I(inode).fs_blocks = (inode->i_size + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
  445. inode_set_bytes(inode, HFSPLUS_I(inode).fs_blocks << sb->s_blocksize_bits);
  446. mark_inode_dirty(inode);
  447. }