compression.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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. ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
  280. BUG_ON(ret);
  281. ret = btrfs_map_bio(root, WRITE, bio, 0, 1);
  282. BUG_ON(ret);
  283. bio_put(bio);
  284. bio = compressed_bio_alloc(bdev, first_byte, GFP_NOFS);
  285. atomic_inc(&cb->pending_bios);
  286. bio->bi_private = cb;
  287. bio->bi_end_io = end_compressed_bio_write;
  288. bio_add_page(bio, page, PAGE_CACHE_SIZE, 0);
  289. }
  290. if (bytes_left < PAGE_CACHE_SIZE) {
  291. printk("bytes left %lu compress len %lu nr %lu\n",
  292. bytes_left, cb->compressed_len, cb->nr_pages);
  293. }
  294. bytes_left -= PAGE_CACHE_SIZE;
  295. first_byte += PAGE_CACHE_SIZE;
  296. cond_resched();
  297. }
  298. bio_get(bio);
  299. ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
  300. BUG_ON(ret);
  301. ret = btrfs_map_bio(root, WRITE, bio, 0, 1);
  302. BUG_ON(ret);
  303. bio_put(bio);
  304. return 0;
  305. }
  306. static noinline int add_ra_bio_pages(struct inode *inode,
  307. u64 compressed_end,
  308. struct compressed_bio *cb)
  309. {
  310. unsigned long end_index;
  311. unsigned long page_index;
  312. u64 last_offset;
  313. u64 isize = i_size_read(inode);
  314. int ret;
  315. struct page *page;
  316. unsigned long nr_pages = 0;
  317. struct extent_map *em;
  318. struct address_space *mapping = inode->i_mapping;
  319. struct pagevec pvec;
  320. struct extent_map_tree *em_tree;
  321. struct extent_io_tree *tree;
  322. u64 end;
  323. int misses = 0;
  324. page = cb->orig_bio->bi_io_vec[cb->orig_bio->bi_vcnt - 1].bv_page;
  325. last_offset = (page_offset(page) + PAGE_CACHE_SIZE);
  326. em_tree = &BTRFS_I(inode)->extent_tree;
  327. tree = &BTRFS_I(inode)->io_tree;
  328. if (isize == 0)
  329. return 0;
  330. end_index = (i_size_read(inode) - 1) >> PAGE_CACHE_SHIFT;
  331. pagevec_init(&pvec, 0);
  332. while(last_offset < compressed_end) {
  333. page_index = last_offset >> PAGE_CACHE_SHIFT;
  334. if (page_index > end_index)
  335. break;
  336. rcu_read_lock();
  337. page = radix_tree_lookup(&mapping->page_tree, page_index);
  338. rcu_read_unlock();
  339. if (page) {
  340. misses++;
  341. if (misses > 4)
  342. break;
  343. goto next;
  344. }
  345. page = alloc_page(mapping_gfp_mask(mapping) | GFP_NOFS);
  346. if (!page)
  347. break;
  348. page->index = page_index;
  349. /*
  350. * what we want to do here is call add_to_page_cache_lru,
  351. * but that isn't exported, so we reproduce it here
  352. */
  353. if (add_to_page_cache(page, mapping,
  354. page->index, GFP_NOFS)) {
  355. page_cache_release(page);
  356. goto next;
  357. }
  358. /* open coding of lru_cache_add, also not exported */
  359. page_cache_get(page);
  360. if (!pagevec_add(&pvec, page))
  361. __pagevec_lru_add(&pvec);
  362. end = last_offset + PAGE_CACHE_SIZE - 1;
  363. /*
  364. * at this point, we have a locked page in the page cache
  365. * for these bytes in the file. But, we have to make
  366. * sure they map to this compressed extent on disk.
  367. */
  368. set_page_extent_mapped(page);
  369. lock_extent(tree, last_offset, end, GFP_NOFS);
  370. spin_lock(&em_tree->lock);
  371. em = lookup_extent_mapping(em_tree, last_offset,
  372. PAGE_CACHE_SIZE);
  373. spin_unlock(&em_tree->lock);
  374. if (!em || last_offset < em->start ||
  375. (last_offset + PAGE_CACHE_SIZE > extent_map_end(em)) ||
  376. (em->block_start >> 9) != cb->orig_bio->bi_sector) {
  377. free_extent_map(em);
  378. unlock_extent(tree, last_offset, end, GFP_NOFS);
  379. unlock_page(page);
  380. page_cache_release(page);
  381. break;
  382. }
  383. free_extent_map(em);
  384. if (page->index == end_index) {
  385. char *userpage;
  386. size_t zero_offset = isize & (PAGE_CACHE_SIZE - 1);
  387. if (zero_offset) {
  388. int zeros;
  389. zeros = PAGE_CACHE_SIZE - zero_offset;
  390. userpage = kmap_atomic(page, KM_USER0);
  391. memset(userpage + zero_offset, 0, zeros);
  392. flush_dcache_page(page);
  393. kunmap_atomic(userpage, KM_USER0);
  394. }
  395. }
  396. ret = bio_add_page(cb->orig_bio, page,
  397. PAGE_CACHE_SIZE, 0);
  398. if (ret == PAGE_CACHE_SIZE) {
  399. nr_pages++;
  400. page_cache_release(page);
  401. } else {
  402. unlock_extent(tree, last_offset, end, GFP_NOFS);
  403. unlock_page(page);
  404. page_cache_release(page);
  405. break;
  406. }
  407. next:
  408. last_offset += PAGE_CACHE_SIZE;
  409. }
  410. if (pagevec_count(&pvec))
  411. __pagevec_lru_add(&pvec);
  412. return 0;
  413. }
  414. /*
  415. * for a compressed read, the bio we get passed has all the inode pages
  416. * in it. We don't actually do IO on those pages but allocate new ones
  417. * to hold the compressed pages on disk.
  418. *
  419. * bio->bi_sector points to the compressed extent on disk
  420. * bio->bi_io_vec points to all of the inode pages
  421. * bio->bi_vcnt is a count of pages
  422. *
  423. * After the compressed pages are read, we copy the bytes into the
  424. * bio we were passed and then call the bio end_io calls
  425. */
  426. int btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
  427. int mirror_num, unsigned long bio_flags)
  428. {
  429. struct extent_io_tree *tree;
  430. struct extent_map_tree *em_tree;
  431. struct compressed_bio *cb;
  432. struct btrfs_root *root = BTRFS_I(inode)->root;
  433. unsigned long uncompressed_len = bio->bi_vcnt * PAGE_CACHE_SIZE;
  434. unsigned long compressed_len;
  435. unsigned long nr_pages;
  436. unsigned long page_index;
  437. struct page *page;
  438. struct block_device *bdev;
  439. struct bio *comp_bio;
  440. u64 cur_disk_byte = (u64)bio->bi_sector << 9;
  441. u64 em_len;
  442. struct extent_map *em;
  443. int ret;
  444. tree = &BTRFS_I(inode)->io_tree;
  445. em_tree = &BTRFS_I(inode)->extent_tree;
  446. /* we need the actual starting offset of this extent in the file */
  447. spin_lock(&em_tree->lock);
  448. em = lookup_extent_mapping(em_tree,
  449. page_offset(bio->bi_io_vec->bv_page),
  450. PAGE_CACHE_SIZE);
  451. spin_unlock(&em_tree->lock);
  452. cb = kmalloc(sizeof(*cb), GFP_NOFS);
  453. atomic_set(&cb->pending_bios, 0);
  454. cb->errors = 0;
  455. cb->inode = inode;
  456. cb->start = em->start;
  457. compressed_len = em->block_len;
  458. em_len = em->len;
  459. free_extent_map(em);
  460. cb->len = uncompressed_len;
  461. cb->compressed_len = compressed_len;
  462. cb->orig_bio = bio;
  463. nr_pages = (compressed_len + PAGE_CACHE_SIZE - 1) /
  464. PAGE_CACHE_SIZE;
  465. cb->compressed_pages = kmalloc(sizeof(struct page *) * nr_pages,
  466. GFP_NOFS);
  467. bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
  468. for (page_index = 0; page_index < nr_pages; page_index++) {
  469. cb->compressed_pages[page_index] = alloc_page(GFP_NOFS |
  470. __GFP_HIGHMEM);
  471. }
  472. cb->nr_pages = nr_pages;
  473. add_ra_bio_pages(inode, cb->start + em_len, cb);
  474. if (!btrfs_test_opt(root, NODATASUM) &&
  475. !btrfs_test_flag(inode, NODATASUM)) {
  476. btrfs_lookup_bio_sums(root, inode, cb->orig_bio);
  477. }
  478. /* include any pages we added in add_ra-bio_pages */
  479. uncompressed_len = bio->bi_vcnt * PAGE_CACHE_SIZE;
  480. cb->len = uncompressed_len;
  481. comp_bio = compressed_bio_alloc(bdev, cur_disk_byte, GFP_NOFS);
  482. comp_bio->bi_private = cb;
  483. comp_bio->bi_end_io = end_compressed_bio_read;
  484. atomic_inc(&cb->pending_bios);
  485. for (page_index = 0; page_index < nr_pages; page_index++) {
  486. page = cb->compressed_pages[page_index];
  487. page->mapping = inode->i_mapping;
  488. if (comp_bio->bi_size)
  489. ret = tree->ops->merge_bio_hook(page, 0,
  490. PAGE_CACHE_SIZE,
  491. comp_bio, 0);
  492. else
  493. ret = 0;
  494. page->mapping = NULL;
  495. if (ret || bio_add_page(comp_bio, page, PAGE_CACHE_SIZE, 0) <
  496. PAGE_CACHE_SIZE) {
  497. bio_get(comp_bio);
  498. ret = btrfs_bio_wq_end_io(root->fs_info, comp_bio, 0);
  499. BUG_ON(ret);
  500. ret = btrfs_map_bio(root, READ, comp_bio, 0, 0);
  501. BUG_ON(ret);
  502. bio_put(comp_bio);
  503. comp_bio = compressed_bio_alloc(bdev, cur_disk_byte,
  504. GFP_NOFS);
  505. atomic_inc(&cb->pending_bios);
  506. comp_bio->bi_private = cb;
  507. comp_bio->bi_end_io = end_compressed_bio_read;
  508. bio_add_page(comp_bio, page, PAGE_CACHE_SIZE, 0);
  509. }
  510. cur_disk_byte += PAGE_CACHE_SIZE;
  511. }
  512. bio_get(comp_bio);
  513. ret = btrfs_bio_wq_end_io(root->fs_info, comp_bio, 0);
  514. BUG_ON(ret);
  515. ret = btrfs_map_bio(root, READ, comp_bio, 0, 0);
  516. BUG_ON(ret);
  517. bio_put(comp_bio);
  518. return 0;
  519. }