extents.c 13 KB

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