compression.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. /*
  2. * Copyright (C) 2008 Oracle. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/bio.h>
  20. #include <linux/buffer_head.h>
  21. #include <linux/file.h>
  22. #include <linux/fs.h>
  23. #include <linux/pagemap.h>
  24. #include <linux/highmem.h>
  25. #include <linux/time.h>
  26. #include <linux/init.h>
  27. #include <linux/string.h>
  28. #include <linux/smp_lock.h>
  29. #include <linux/backing-dev.h>
  30. #include <linux/mpage.h>
  31. #include <linux/swap.h>
  32. #include <linux/writeback.h>
  33. #include <linux/bit_spinlock.h>
  34. #include <linux/version.h>
  35. #include <linux/pagevec.h>
  36. #include "compat.h"
  37. #include "ctree.h"
  38. #include "disk-io.h"
  39. #include "transaction.h"
  40. #include "btrfs_inode.h"
  41. #include "volumes.h"
  42. #include "ordered-data.h"
  43. #include "compression.h"
  44. #include "extent_io.h"
  45. #include "extent_map.h"
  46. struct compressed_bio {
  47. /* number of bios pending for this compressed extent */
  48. atomic_t pending_bios;
  49. /* the pages with the compressed data on them */
  50. struct page **compressed_pages;
  51. /* inode that owns this data */
  52. struct inode *inode;
  53. /* starting offset in the inode for our pages */
  54. u64 start;
  55. /* number of bytes in the inode we're working on */
  56. unsigned long len;
  57. /* number of bytes on disk */
  58. unsigned long compressed_len;
  59. /* number of compressed pages in the array */
  60. unsigned long nr_pages;
  61. /* IO errors */
  62. int errors;
  63. /* for reads, this is the bio we are copying the data into */
  64. struct bio *orig_bio;
  65. };
  66. static struct bio *compressed_bio_alloc(struct block_device *bdev,
  67. u64 first_byte, gfp_t gfp_flags)
  68. {
  69. struct bio *bio;
  70. int nr_vecs;
  71. nr_vecs = bio_get_nr_vecs(bdev);
  72. bio = bio_alloc(gfp_flags, nr_vecs);
  73. if (bio == NULL && (current->flags & PF_MEMALLOC)) {
  74. while (!bio && (nr_vecs /= 2))
  75. bio = bio_alloc(gfp_flags, nr_vecs);
  76. }
  77. if (bio) {
  78. bio->bi_size = 0;
  79. bio->bi_bdev = bdev;
  80. bio->bi_sector = first_byte >> 9;
  81. }
  82. return bio;
  83. }
  84. /* when we finish reading compressed pages from the disk, we
  85. * decompress them and then run the bio end_io routines on the
  86. * decompressed pages (in the inode address space).
  87. *
  88. * This allows the checksumming and other IO error handling routines
  89. * to work normally
  90. *
  91. * The compressed pages are freed here, and it must be run
  92. * in process context
  93. */
  94. static void end_compressed_bio_read(struct bio *bio, int err)
  95. {
  96. struct extent_io_tree *tree;
  97. struct compressed_bio *cb = bio->bi_private;
  98. struct inode *inode;
  99. struct page *page;
  100. unsigned long index;
  101. int ret;
  102. if (err)
  103. cb->errors = 1;
  104. /* if there are more bios still pending for this compressed
  105. * extent, just exit
  106. */
  107. if (!atomic_dec_and_test(&cb->pending_bios))
  108. goto out;
  109. /* ok, we're the last bio for this extent, lets start
  110. * the decompression.
  111. */
  112. inode = cb->inode;
  113. tree = &BTRFS_I(inode)->io_tree;
  114. ret = btrfs_zlib_decompress_biovec(cb->compressed_pages,
  115. cb->start,
  116. cb->orig_bio->bi_io_vec,
  117. cb->orig_bio->bi_vcnt,
  118. cb->compressed_len);
  119. if (ret)
  120. cb->errors = 1;
  121. /* release the compressed pages */
  122. index = 0;
  123. for (index = 0; index < cb->nr_pages; index++) {
  124. page = cb->compressed_pages[index];
  125. page->mapping = NULL;
  126. page_cache_release(page);
  127. }
  128. /* do io completion on the original bio */
  129. if (cb->errors) {
  130. bio_io_error(cb->orig_bio);
  131. } else
  132. bio_endio(cb->orig_bio, 0);
  133. /* finally free the cb struct */
  134. kfree(cb->compressed_pages);
  135. kfree(cb);
  136. out:
  137. bio_put(bio);
  138. }
  139. /*
  140. * Clear the writeback bits on all of the file
  141. * pages for a compressed write
  142. */
  143. static noinline int end_compressed_writeback(struct inode *inode, u64 start,
  144. unsigned long ram_size)
  145. {
  146. unsigned long index = start >> PAGE_CACHE_SHIFT;
  147. unsigned long end_index = (start + ram_size - 1) >> PAGE_CACHE_SHIFT;
  148. struct page *pages[16];
  149. unsigned long nr_pages = end_index - index + 1;
  150. int i;
  151. int ret;
  152. while(nr_pages > 0) {
  153. ret = find_get_pages_contig(inode->i_mapping, index,
  154. min_t(unsigned long,
  155. nr_pages, ARRAY_SIZE(pages)), pages);
  156. if (ret == 0) {
  157. nr_pages -= 1;
  158. index += 1;
  159. continue;
  160. }
  161. for (i = 0; i < ret; i++) {
  162. end_page_writeback(pages[i]);
  163. page_cache_release(pages[i]);
  164. }
  165. nr_pages -= ret;
  166. index += ret;
  167. }
  168. /* the inode may be gone now */
  169. return 0;
  170. }
  171. /*
  172. * do the cleanup once all the compressed pages hit the disk.
  173. * This will clear writeback on the file pages and free the compressed
  174. * pages.
  175. *
  176. * This also calls the writeback end hooks for the file pages so that
  177. * metadata and checksums can be updated in the file.
  178. */
  179. static void end_compressed_bio_write(struct bio *bio, int err)
  180. {
  181. struct extent_io_tree *tree;
  182. struct compressed_bio *cb = bio->bi_private;
  183. struct inode *inode;
  184. struct page *page;
  185. unsigned long index;
  186. if (err)
  187. cb->errors = 1;
  188. /* if there are more bios still pending for this compressed
  189. * extent, just exit
  190. */
  191. if (!atomic_dec_and_test(&cb->pending_bios))
  192. goto out;
  193. /* ok, we're the last bio for this extent, step one is to
  194. * call back into the FS and do all the end_io operations
  195. */
  196. inode = cb->inode;
  197. tree = &BTRFS_I(inode)->io_tree;
  198. cb->compressed_pages[0]->mapping = cb->inode->i_mapping;
  199. tree->ops->writepage_end_io_hook(cb->compressed_pages[0],
  200. cb->start,
  201. cb->start + cb->len - 1,
  202. NULL, 1);
  203. cb->compressed_pages[0]->mapping = NULL;
  204. end_compressed_writeback(inode, cb->start, cb->len);
  205. /* note, our inode could be gone now */
  206. /*
  207. * release the compressed pages, these came from alloc_page and
  208. * are not attached to the inode at all
  209. */
  210. index = 0;
  211. for (index = 0; index < cb->nr_pages; index++) {
  212. page = cb->compressed_pages[index];
  213. page->mapping = NULL;
  214. page_cache_release(page);
  215. }
  216. /* finally free the cb struct */
  217. kfree(cb->compressed_pages);
  218. kfree(cb);
  219. out:
  220. bio_put(bio);
  221. }
  222. /*
  223. * worker function to build and submit bios for previously compressed pages.
  224. * The corresponding pages in the inode should be marked for writeback
  225. * and the compressed pages should have a reference on them for dropping
  226. * when the IO is complete.
  227. *
  228. * This also checksums the file bytes and gets things ready for
  229. * the end io hooks.
  230. */
  231. int btrfs_submit_compressed_write(struct inode *inode, u64 start,
  232. unsigned long len, u64 disk_start,
  233. unsigned long compressed_len,
  234. struct page **compressed_pages,
  235. unsigned long nr_pages)
  236. {
  237. struct bio *bio = NULL;
  238. struct btrfs_root *root = BTRFS_I(inode)->root;
  239. struct compressed_bio *cb;
  240. unsigned long bytes_left;
  241. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  242. int page_index = 0;
  243. struct page *page;
  244. u64 first_byte = disk_start;
  245. struct block_device *bdev;
  246. int ret;
  247. WARN_ON(start & ((u64)PAGE_CACHE_SIZE - 1));
  248. cb = kmalloc(sizeof(*cb), GFP_NOFS);
  249. atomic_set(&cb->pending_bios, 0);
  250. cb->errors = 0;
  251. cb->inode = inode;
  252. cb->start = start;
  253. cb->len = len;
  254. cb->compressed_pages = compressed_pages;
  255. cb->compressed_len = compressed_len;
  256. cb->orig_bio = NULL;
  257. cb->nr_pages = nr_pages;
  258. bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
  259. ret = btrfs_csum_file_bytes(root, inode, start, len);
  260. BUG_ON(ret);
  261. bio = compressed_bio_alloc(bdev, first_byte, GFP_NOFS);
  262. bio->bi_private = cb;
  263. bio->bi_end_io = end_compressed_bio_write;
  264. atomic_inc(&cb->pending_bios);
  265. /* create and submit bios for the compressed pages */
  266. bytes_left = compressed_len;
  267. for (page_index = 0; page_index < cb->nr_pages; page_index++) {
  268. page = compressed_pages[page_index];
  269. page->mapping = inode->i_mapping;
  270. if (bio->bi_size)
  271. ret = io_tree->ops->merge_bio_hook(page, 0,
  272. PAGE_CACHE_SIZE,
  273. bio, 0);
  274. else
  275. ret = 0;
  276. page->mapping = NULL;
  277. if (ret || bio_add_page(bio, page, PAGE_CACHE_SIZE, 0) <
  278. PAGE_CACHE_SIZE) {
  279. bio_get(bio);
  280. /*
  281. * inc the count before we submit the bio so
  282. * we know the end IO handler won't happen before
  283. * we inc the count. Otherwise, the cb might get
  284. * freed before we're done setting it up
  285. */
  286. atomic_inc(&cb->pending_bios);
  287. ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
  288. BUG_ON(ret);
  289. ret = btrfs_map_bio(root, WRITE, bio, 0, 1);
  290. BUG_ON(ret);
  291. bio_put(bio);
  292. bio = compressed_bio_alloc(bdev, first_byte, GFP_NOFS);
  293. bio->bi_private = cb;
  294. bio->bi_end_io = end_compressed_bio_write;
  295. bio_add_page(bio, page, PAGE_CACHE_SIZE, 0);
  296. }
  297. if (bytes_left < PAGE_CACHE_SIZE) {
  298. printk("bytes left %lu compress len %lu nr %lu\n",
  299. bytes_left, cb->compressed_len, cb->nr_pages);
  300. }
  301. bytes_left -= PAGE_CACHE_SIZE;
  302. first_byte += PAGE_CACHE_SIZE;
  303. cond_resched();
  304. }
  305. bio_get(bio);
  306. ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
  307. BUG_ON(ret);
  308. ret = btrfs_map_bio(root, WRITE, bio, 0, 1);
  309. BUG_ON(ret);
  310. bio_put(bio);
  311. return 0;
  312. }
  313. static noinline int add_ra_bio_pages(struct inode *inode,
  314. u64 compressed_end,
  315. struct compressed_bio *cb)
  316. {
  317. unsigned long end_index;
  318. unsigned long page_index;
  319. u64 last_offset;
  320. u64 isize = i_size_read(inode);
  321. int ret;
  322. struct page *page;
  323. unsigned long nr_pages = 0;
  324. struct extent_map *em;
  325. struct address_space *mapping = inode->i_mapping;
  326. struct pagevec pvec;
  327. struct extent_map_tree *em_tree;
  328. struct extent_io_tree *tree;
  329. u64 end;
  330. int misses = 0;
  331. page = cb->orig_bio->bi_io_vec[cb->orig_bio->bi_vcnt - 1].bv_page;
  332. last_offset = (page_offset(page) + PAGE_CACHE_SIZE);
  333. em_tree = &BTRFS_I(inode)->extent_tree;
  334. tree = &BTRFS_I(inode)->io_tree;
  335. if (isize == 0)
  336. return 0;
  337. end_index = (i_size_read(inode) - 1) >> PAGE_CACHE_SHIFT;
  338. pagevec_init(&pvec, 0);
  339. while(last_offset < compressed_end) {
  340. page_index = last_offset >> PAGE_CACHE_SHIFT;
  341. if (page_index > end_index)
  342. break;
  343. rcu_read_lock();
  344. page = radix_tree_lookup(&mapping->page_tree, page_index);
  345. rcu_read_unlock();
  346. if (page) {
  347. misses++;
  348. if (misses > 4)
  349. break;
  350. goto next;
  351. }
  352. page = alloc_page(mapping_gfp_mask(mapping) | GFP_NOFS);
  353. if (!page)
  354. break;
  355. page->index = page_index;
  356. /*
  357. * what we want to do here is call add_to_page_cache_lru,
  358. * but that isn't exported, so we reproduce it here
  359. */
  360. if (add_to_page_cache(page, mapping,
  361. page->index, GFP_NOFS)) {
  362. page_cache_release(page);
  363. goto next;
  364. }
  365. /* open coding of lru_cache_add, also not exported */
  366. page_cache_get(page);
  367. if (!pagevec_add(&pvec, page))
  368. __pagevec_lru_add_file(&pvec);
  369. end = last_offset + PAGE_CACHE_SIZE - 1;
  370. /*
  371. * at this point, we have a locked page in the page cache
  372. * for these bytes in the file. But, we have to make
  373. * sure they map to this compressed extent on disk.
  374. */
  375. set_page_extent_mapped(page);
  376. lock_extent(tree, last_offset, end, GFP_NOFS);
  377. spin_lock(&em_tree->lock);
  378. em = lookup_extent_mapping(em_tree, last_offset,
  379. PAGE_CACHE_SIZE);
  380. spin_unlock(&em_tree->lock);
  381. if (!em || last_offset < em->start ||
  382. (last_offset + PAGE_CACHE_SIZE > extent_map_end(em)) ||
  383. (em->block_start >> 9) != cb->orig_bio->bi_sector) {
  384. free_extent_map(em);
  385. unlock_extent(tree, last_offset, end, GFP_NOFS);
  386. unlock_page(page);
  387. page_cache_release(page);
  388. break;
  389. }
  390. free_extent_map(em);
  391. if (page->index == end_index) {
  392. char *userpage;
  393. size_t zero_offset = isize & (PAGE_CACHE_SIZE - 1);
  394. if (zero_offset) {
  395. int zeros;
  396. zeros = PAGE_CACHE_SIZE - zero_offset;
  397. userpage = kmap_atomic(page, KM_USER0);
  398. memset(userpage + zero_offset, 0, zeros);
  399. flush_dcache_page(page);
  400. kunmap_atomic(userpage, KM_USER0);
  401. }
  402. }
  403. ret = bio_add_page(cb->orig_bio, page,
  404. PAGE_CACHE_SIZE, 0);
  405. if (ret == PAGE_CACHE_SIZE) {
  406. nr_pages++;
  407. page_cache_release(page);
  408. } else {
  409. unlock_extent(tree, last_offset, end, GFP_NOFS);
  410. unlock_page(page);
  411. page_cache_release(page);
  412. break;
  413. }
  414. next:
  415. last_offset += PAGE_CACHE_SIZE;
  416. }
  417. if (pagevec_count(&pvec))
  418. __pagevec_lru_add_file(&pvec);
  419. return 0;
  420. }
  421. /*
  422. * for a compressed read, the bio we get passed has all the inode pages
  423. * in it. We don't actually do IO on those pages but allocate new ones
  424. * to hold the compressed pages on disk.
  425. *
  426. * bio->bi_sector points to the compressed extent on disk
  427. * bio->bi_io_vec points to all of the inode pages
  428. * bio->bi_vcnt is a count of pages
  429. *
  430. * After the compressed pages are read, we copy the bytes into the
  431. * bio we were passed and then call the bio end_io calls
  432. */
  433. int btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
  434. int mirror_num, unsigned long bio_flags)
  435. {
  436. struct extent_io_tree *tree;
  437. struct extent_map_tree *em_tree;
  438. struct compressed_bio *cb;
  439. struct btrfs_root *root = BTRFS_I(inode)->root;
  440. unsigned long uncompressed_len = bio->bi_vcnt * PAGE_CACHE_SIZE;
  441. unsigned long compressed_len;
  442. unsigned long nr_pages;
  443. unsigned long page_index;
  444. struct page *page;
  445. struct block_device *bdev;
  446. struct bio *comp_bio;
  447. u64 cur_disk_byte = (u64)bio->bi_sector << 9;
  448. u64 em_len;
  449. u64 em_start;
  450. struct extent_map *em;
  451. int ret;
  452. tree = &BTRFS_I(inode)->io_tree;
  453. em_tree = &BTRFS_I(inode)->extent_tree;
  454. /* we need the actual starting offset of this extent in the file */
  455. spin_lock(&em_tree->lock);
  456. em = lookup_extent_mapping(em_tree,
  457. page_offset(bio->bi_io_vec->bv_page),
  458. PAGE_CACHE_SIZE);
  459. spin_unlock(&em_tree->lock);
  460. cb = kmalloc(sizeof(*cb), GFP_NOFS);
  461. atomic_set(&cb->pending_bios, 0);
  462. cb->errors = 0;
  463. cb->inode = inode;
  464. cb->start = em->orig_start;
  465. compressed_len = em->block_len;
  466. em_len = em->len;
  467. em_start = em->start;
  468. free_extent_map(em);
  469. em = NULL;
  470. cb->len = uncompressed_len;
  471. cb->compressed_len = compressed_len;
  472. cb->orig_bio = bio;
  473. nr_pages = (compressed_len + PAGE_CACHE_SIZE - 1) /
  474. PAGE_CACHE_SIZE;
  475. cb->compressed_pages = kmalloc(sizeof(struct page *) * nr_pages,
  476. GFP_NOFS);
  477. bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
  478. for (page_index = 0; page_index < nr_pages; page_index++) {
  479. cb->compressed_pages[page_index] = alloc_page(GFP_NOFS |
  480. __GFP_HIGHMEM);
  481. }
  482. cb->nr_pages = nr_pages;
  483. add_ra_bio_pages(inode, em_start + em_len, cb);
  484. if (!btrfs_test_opt(root, NODATASUM) &&
  485. !btrfs_test_flag(inode, NODATASUM)) {
  486. btrfs_lookup_bio_sums(root, inode, cb->orig_bio);
  487. }
  488. /* include any pages we added in add_ra-bio_pages */
  489. uncompressed_len = bio->bi_vcnt * PAGE_CACHE_SIZE;
  490. cb->len = uncompressed_len;
  491. comp_bio = compressed_bio_alloc(bdev, cur_disk_byte, GFP_NOFS);
  492. comp_bio->bi_private = cb;
  493. comp_bio->bi_end_io = end_compressed_bio_read;
  494. atomic_inc(&cb->pending_bios);
  495. for (page_index = 0; page_index < nr_pages; page_index++) {
  496. page = cb->compressed_pages[page_index];
  497. page->mapping = inode->i_mapping;
  498. if (comp_bio->bi_size)
  499. ret = tree->ops->merge_bio_hook(page, 0,
  500. PAGE_CACHE_SIZE,
  501. comp_bio, 0);
  502. else
  503. ret = 0;
  504. page->mapping = NULL;
  505. if (ret || bio_add_page(comp_bio, page, PAGE_CACHE_SIZE, 0) <
  506. PAGE_CACHE_SIZE) {
  507. bio_get(comp_bio);
  508. ret = btrfs_bio_wq_end_io(root->fs_info, comp_bio, 0);
  509. BUG_ON(ret);
  510. /*
  511. * inc the count before we submit the bio so
  512. * we know the end IO handler won't happen before
  513. * we inc the count. Otherwise, the cb might get
  514. * freed before we're done setting it up
  515. */
  516. atomic_inc(&cb->pending_bios);
  517. ret = btrfs_map_bio(root, READ, comp_bio, 0, 0);
  518. BUG_ON(ret);
  519. bio_put(comp_bio);
  520. comp_bio = compressed_bio_alloc(bdev, cur_disk_byte,
  521. GFP_NOFS);
  522. comp_bio->bi_private = cb;
  523. comp_bio->bi_end_io = end_compressed_bio_read;
  524. bio_add_page(comp_bio, page, PAGE_CACHE_SIZE, 0);
  525. }
  526. cur_disk_byte += PAGE_CACHE_SIZE;
  527. }
  528. bio_get(comp_bio);
  529. ret = btrfs_bio_wq_end_io(root->fs_info, comp_bio, 0);
  530. BUG_ON(ret);
  531. ret = btrfs_map_bio(root, READ, comp_bio, 0, 0);
  532. BUG_ON(ret);
  533. bio_put(comp_bio);
  534. return 0;
  535. }