compression.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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 "ctree.h"
  37. #include "disk-io.h"
  38. #include "transaction.h"
  39. #include "btrfs_inode.h"
  40. #include "volumes.h"
  41. #include "ordered-data.h"
  42. #include "compat.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(nr_pages, ARRAY_SIZE(pages)), pages);
  155. if (ret == 0) {
  156. nr_pages -= 1;
  157. index += 1;
  158. continue;
  159. }
  160. for (i = 0; i < ret; i++) {
  161. end_page_writeback(pages[i]);
  162. page_cache_release(pages[i]);
  163. }
  164. nr_pages -= ret;
  165. index += ret;
  166. }
  167. /* the inode may be gone now */
  168. return 0;
  169. }
  170. /*
  171. * do the cleanup once all the compressed pages hit the disk.
  172. * This will clear writeback on the file pages and free the compressed
  173. * pages.
  174. *
  175. * This also calls the writeback end hooks for the file pages so that
  176. * metadata and checksums can be updated in the file.
  177. */
  178. static void end_compressed_bio_write(struct bio *bio, int err)
  179. {
  180. struct extent_io_tree *tree;
  181. struct compressed_bio *cb = bio->bi_private;
  182. struct inode *inode;
  183. struct page *page;
  184. unsigned long index;
  185. if (err)
  186. cb->errors = 1;
  187. /* if there are more bios still pending for this compressed
  188. * extent, just exit
  189. */
  190. if (!atomic_dec_and_test(&cb->pending_bios))
  191. goto out;
  192. /* ok, we're the last bio for this extent, step one is to
  193. * call back into the FS and do all the end_io operations
  194. */
  195. inode = cb->inode;
  196. tree = &BTRFS_I(inode)->io_tree;
  197. cb->compressed_pages[0]->mapping = cb->inode->i_mapping;
  198. tree->ops->writepage_end_io_hook(cb->compressed_pages[0],
  199. cb->start,
  200. cb->start + cb->len - 1,
  201. NULL, 1);
  202. cb->compressed_pages[0]->mapping = NULL;
  203. end_compressed_writeback(inode, cb->start, cb->len);
  204. /* note, our inode could be gone now */
  205. /*
  206. * release the compressed pages, these came from alloc_page and
  207. * are not attached to the inode at all
  208. */
  209. index = 0;
  210. for (index = 0; index < cb->nr_pages; index++) {
  211. page = cb->compressed_pages[index];
  212. page->mapping = NULL;
  213. page_cache_release(page);
  214. }
  215. /* finally free the cb struct */
  216. kfree(cb->compressed_pages);
  217. kfree(cb);
  218. out:
  219. bio_put(bio);
  220. }
  221. /*
  222. * worker function to build and submit bios for previously compressed pages.
  223. * The corresponding pages in the inode should be marked for writeback
  224. * and the compressed pages should have a reference on them for dropping
  225. * when the IO is complete.
  226. *
  227. * This also checksums the file bytes and gets things ready for
  228. * the end io hooks.
  229. */
  230. int btrfs_submit_compressed_write(struct inode *inode, u64 start,
  231. unsigned long len, u64 disk_start,
  232. unsigned long compressed_len,
  233. struct page **compressed_pages,
  234. unsigned long nr_pages)
  235. {
  236. struct bio *bio = NULL;
  237. struct btrfs_root *root = BTRFS_I(inode)->root;
  238. struct compressed_bio *cb;
  239. unsigned long bytes_left;
  240. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  241. int page_index = 0;
  242. struct page *page;
  243. u64 first_byte = disk_start;
  244. struct block_device *bdev;
  245. int ret;
  246. WARN_ON(start & ((u64)PAGE_CACHE_SIZE - 1));
  247. cb = kmalloc(sizeof(*cb), GFP_NOFS);
  248. atomic_set(&cb->pending_bios, 0);
  249. cb->errors = 0;
  250. cb->inode = inode;
  251. cb->start = start;
  252. cb->len = len;
  253. cb->compressed_pages = compressed_pages;
  254. cb->compressed_len = compressed_len;
  255. cb->orig_bio = NULL;
  256. cb->nr_pages = nr_pages;
  257. bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
  258. ret = btrfs_csum_file_bytes(root, inode, start, len);
  259. BUG_ON(ret);
  260. bio = compressed_bio_alloc(bdev, first_byte, GFP_NOFS);
  261. bio->bi_private = cb;
  262. bio->bi_end_io = end_compressed_bio_write;
  263. atomic_inc(&cb->pending_bios);
  264. /* create and submit bios for the compressed pages */
  265. bytes_left = compressed_len;
  266. for (page_index = 0; page_index < cb->nr_pages; page_index++) {
  267. page = compressed_pages[page_index];
  268. page->mapping = inode->i_mapping;
  269. if (bio->bi_size)
  270. ret = io_tree->ops->merge_bio_hook(page, 0,
  271. PAGE_CACHE_SIZE,
  272. bio, 0);
  273. else
  274. ret = 0;
  275. page->mapping = NULL;
  276. if (ret || bio_add_page(bio, page, PAGE_CACHE_SIZE, 0) <
  277. PAGE_CACHE_SIZE) {
  278. bio_get(bio);
  279. /*
  280. * inc the count before we submit the bio so
  281. * we know the end IO handler won't happen before
  282. * we inc the count. Otherwise, the cb might get
  283. * freed before we're done setting it up
  284. */
  285. atomic_inc(&cb->pending_bios);
  286. ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
  287. BUG_ON(ret);
  288. ret = btrfs_map_bio(root, WRITE, bio, 0, 1);
  289. BUG_ON(ret);
  290. bio_put(bio);
  291. bio = compressed_bio_alloc(bdev, first_byte, GFP_NOFS);
  292. bio->bi_private = cb;
  293. bio->bi_end_io = end_compressed_bio_write;
  294. bio_add_page(bio, page, PAGE_CACHE_SIZE, 0);
  295. }
  296. if (bytes_left < PAGE_CACHE_SIZE) {
  297. printk("bytes left %lu compress len %lu nr %lu\n",
  298. bytes_left, cb->compressed_len, cb->nr_pages);
  299. }
  300. bytes_left -= PAGE_CACHE_SIZE;
  301. first_byte += PAGE_CACHE_SIZE;
  302. cond_resched();
  303. }
  304. bio_get(bio);
  305. ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
  306. BUG_ON(ret);
  307. ret = btrfs_map_bio(root, WRITE, bio, 0, 1);
  308. BUG_ON(ret);
  309. bio_put(bio);
  310. return 0;
  311. }
  312. static noinline int add_ra_bio_pages(struct inode *inode,
  313. u64 compressed_end,
  314. struct compressed_bio *cb)
  315. {
  316. unsigned long end_index;
  317. unsigned long page_index;
  318. u64 last_offset;
  319. u64 isize = i_size_read(inode);
  320. int ret;
  321. struct page *page;
  322. unsigned long nr_pages = 0;
  323. struct extent_map *em;
  324. struct address_space *mapping = inode->i_mapping;
  325. struct pagevec pvec;
  326. struct extent_map_tree *em_tree;
  327. struct extent_io_tree *tree;
  328. u64 end;
  329. int misses = 0;
  330. page = cb->orig_bio->bi_io_vec[cb->orig_bio->bi_vcnt - 1].bv_page;
  331. last_offset = (page_offset(page) + PAGE_CACHE_SIZE);
  332. em_tree = &BTRFS_I(inode)->extent_tree;
  333. tree = &BTRFS_I(inode)->io_tree;
  334. if (isize == 0)
  335. return 0;
  336. end_index = (i_size_read(inode) - 1) >> PAGE_CACHE_SHIFT;
  337. pagevec_init(&pvec, 0);
  338. while(last_offset < compressed_end) {
  339. page_index = last_offset >> PAGE_CACHE_SHIFT;
  340. if (page_index > end_index)
  341. break;
  342. rcu_read_lock();
  343. page = radix_tree_lookup(&mapping->page_tree, page_index);
  344. rcu_read_unlock();
  345. if (page) {
  346. misses++;
  347. if (misses > 4)
  348. break;
  349. goto next;
  350. }
  351. page = alloc_page(mapping_gfp_mask(mapping) | GFP_NOFS);
  352. if (!page)
  353. break;
  354. page->index = page_index;
  355. /*
  356. * what we want to do here is call add_to_page_cache_lru,
  357. * but that isn't exported, so we reproduce it here
  358. */
  359. if (add_to_page_cache(page, mapping,
  360. page->index, GFP_NOFS)) {
  361. page_cache_release(page);
  362. goto next;
  363. }
  364. /* open coding of lru_cache_add, also not exported */
  365. page_cache_get(page);
  366. if (!pagevec_add(&pvec, page))
  367. __pagevec_lru_add(&pvec);
  368. end = last_offset + PAGE_CACHE_SIZE - 1;
  369. /*
  370. * at this point, we have a locked page in the page cache
  371. * for these bytes in the file. But, we have to make
  372. * sure they map to this compressed extent on disk.
  373. */
  374. set_page_extent_mapped(page);
  375. lock_extent(tree, last_offset, end, GFP_NOFS);
  376. spin_lock(&em_tree->lock);
  377. em = lookup_extent_mapping(em_tree, last_offset,
  378. PAGE_CACHE_SIZE);
  379. spin_unlock(&em_tree->lock);
  380. if (!em || last_offset < em->start ||
  381. (last_offset + PAGE_CACHE_SIZE > extent_map_end(em)) ||
  382. (em->block_start >> 9) != cb->orig_bio->bi_sector) {
  383. free_extent_map(em);
  384. unlock_extent(tree, last_offset, end, GFP_NOFS);
  385. unlock_page(page);
  386. page_cache_release(page);
  387. break;
  388. }
  389. free_extent_map(em);
  390. if (page->index == end_index) {
  391. char *userpage;
  392. size_t zero_offset = isize & (PAGE_CACHE_SIZE - 1);
  393. if (zero_offset) {
  394. int zeros;
  395. zeros = PAGE_CACHE_SIZE - zero_offset;
  396. userpage = kmap_atomic(page, KM_USER0);
  397. memset(userpage + zero_offset, 0, zeros);
  398. flush_dcache_page(page);
  399. kunmap_atomic(userpage, KM_USER0);
  400. }
  401. }
  402. ret = bio_add_page(cb->orig_bio, page,
  403. PAGE_CACHE_SIZE, 0);
  404. if (ret == PAGE_CACHE_SIZE) {
  405. nr_pages++;
  406. page_cache_release(page);
  407. } else {
  408. unlock_extent(tree, last_offset, end, GFP_NOFS);
  409. unlock_page(page);
  410. page_cache_release(page);
  411. break;
  412. }
  413. next:
  414. last_offset += PAGE_CACHE_SIZE;
  415. }
  416. if (pagevec_count(&pvec))
  417. __pagevec_lru_add(&pvec);
  418. return 0;
  419. }
  420. /*
  421. * for a compressed read, the bio we get passed has all the inode pages
  422. * in it. We don't actually do IO on those pages but allocate new ones
  423. * to hold the compressed pages on disk.
  424. *
  425. * bio->bi_sector points to the compressed extent on disk
  426. * bio->bi_io_vec points to all of the inode pages
  427. * bio->bi_vcnt is a count of pages
  428. *
  429. * After the compressed pages are read, we copy the bytes into the
  430. * bio we were passed and then call the bio end_io calls
  431. */
  432. int btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
  433. int mirror_num, unsigned long bio_flags)
  434. {
  435. struct extent_io_tree *tree;
  436. struct extent_map_tree *em_tree;
  437. struct compressed_bio *cb;
  438. struct btrfs_root *root = BTRFS_I(inode)->root;
  439. unsigned long uncompressed_len = bio->bi_vcnt * PAGE_CACHE_SIZE;
  440. unsigned long compressed_len;
  441. unsigned long nr_pages;
  442. unsigned long page_index;
  443. struct page *page;
  444. struct block_device *bdev;
  445. struct bio *comp_bio;
  446. u64 cur_disk_byte = (u64)bio->bi_sector << 9;
  447. u64 em_len;
  448. struct extent_map *em;
  449. int ret;
  450. tree = &BTRFS_I(inode)->io_tree;
  451. em_tree = &BTRFS_I(inode)->extent_tree;
  452. /* we need the actual starting offset of this extent in the file */
  453. spin_lock(&em_tree->lock);
  454. em = lookup_extent_mapping(em_tree,
  455. page_offset(bio->bi_io_vec->bv_page),
  456. PAGE_CACHE_SIZE);
  457. spin_unlock(&em_tree->lock);
  458. cb = kmalloc(sizeof(*cb), GFP_NOFS);
  459. atomic_set(&cb->pending_bios, 0);
  460. cb->errors = 0;
  461. cb->inode = inode;
  462. cb->start = em->start;
  463. compressed_len = em->block_len;
  464. em_len = em->len;
  465. free_extent_map(em);
  466. cb->len = uncompressed_len;
  467. cb->compressed_len = compressed_len;
  468. cb->orig_bio = bio;
  469. nr_pages = (compressed_len + PAGE_CACHE_SIZE - 1) /
  470. PAGE_CACHE_SIZE;
  471. cb->compressed_pages = kmalloc(sizeof(struct page *) * nr_pages,
  472. GFP_NOFS);
  473. bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
  474. for (page_index = 0; page_index < nr_pages; page_index++) {
  475. cb->compressed_pages[page_index] = alloc_page(GFP_NOFS |
  476. __GFP_HIGHMEM);
  477. }
  478. cb->nr_pages = nr_pages;
  479. add_ra_bio_pages(inode, cb->start + em_len, cb);
  480. if (!btrfs_test_opt(root, NODATASUM) &&
  481. !btrfs_test_flag(inode, NODATASUM)) {
  482. btrfs_lookup_bio_sums(root, inode, cb->orig_bio);
  483. }
  484. /* include any pages we added in add_ra-bio_pages */
  485. uncompressed_len = bio->bi_vcnt * PAGE_CACHE_SIZE;
  486. cb->len = uncompressed_len;
  487. comp_bio = compressed_bio_alloc(bdev, cur_disk_byte, GFP_NOFS);
  488. comp_bio->bi_private = cb;
  489. comp_bio->bi_end_io = end_compressed_bio_read;
  490. atomic_inc(&cb->pending_bios);
  491. for (page_index = 0; page_index < nr_pages; page_index++) {
  492. page = cb->compressed_pages[page_index];
  493. page->mapping = inode->i_mapping;
  494. if (comp_bio->bi_size)
  495. ret = tree->ops->merge_bio_hook(page, 0,
  496. PAGE_CACHE_SIZE,
  497. comp_bio, 0);
  498. else
  499. ret = 0;
  500. page->mapping = NULL;
  501. if (ret || bio_add_page(comp_bio, page, PAGE_CACHE_SIZE, 0) <
  502. PAGE_CACHE_SIZE) {
  503. bio_get(comp_bio);
  504. ret = btrfs_bio_wq_end_io(root->fs_info, comp_bio, 0);
  505. BUG_ON(ret);
  506. /*
  507. * inc the count before we submit the bio so
  508. * we know the end IO handler won't happen before
  509. * we inc the count. Otherwise, the cb might get
  510. * freed before we're done setting it up
  511. */
  512. atomic_inc(&cb->pending_bios);
  513. ret = btrfs_map_bio(root, READ, comp_bio, 0, 0);
  514. BUG_ON(ret);
  515. bio_put(comp_bio);
  516. comp_bio = compressed_bio_alloc(bdev, cur_disk_byte,
  517. GFP_NOFS);
  518. comp_bio->bi_private = cb;
  519. comp_bio->bi_end_io = end_compressed_bio_read;
  520. bio_add_page(comp_bio, page, PAGE_CACHE_SIZE, 0);
  521. }
  522. cur_disk_byte += PAGE_CACHE_SIZE;
  523. }
  524. bio_get(comp_bio);
  525. ret = btrfs_bio_wq_end_io(root->fs_info, comp_bio, 0);
  526. BUG_ON(ret);
  527. ret = btrfs_map_bio(root, READ, comp_bio, 0, 0);
  528. BUG_ON(ret);
  529. bio_put(comp_bio);
  530. return 0;
  531. }