compression.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  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/backing-dev.h>
  29. #include <linux/mpage.h>
  30. #include <linux/swap.h>
  31. #include <linux/writeback.h>
  32. #include <linux/bit_spinlock.h>
  33. #include <linux/slab.h>
  34. #include "compat.h"
  35. #include "ctree.h"
  36. #include "disk-io.h"
  37. #include "transaction.h"
  38. #include "btrfs_inode.h"
  39. #include "volumes.h"
  40. #include "ordered-data.h"
  41. #include "compression.h"
  42. #include "extent_io.h"
  43. #include "extent_map.h"
  44. struct compressed_bio {
  45. /* number of bios pending for this compressed extent */
  46. atomic_t pending_bios;
  47. /* the pages with the compressed data on them */
  48. struct page **compressed_pages;
  49. /* inode that owns this data */
  50. struct inode *inode;
  51. /* starting offset in the inode for our pages */
  52. u64 start;
  53. /* number of bytes in the inode we're working on */
  54. unsigned long len;
  55. /* number of bytes on disk */
  56. unsigned long compressed_len;
  57. /* the compression algorithm for this bio */
  58. int compress_type;
  59. /* number of compressed pages in the array */
  60. unsigned long nr_pages;
  61. /* IO errors */
  62. int errors;
  63. int mirror_num;
  64. /* for reads, this is the bio we are copying the data into */
  65. struct bio *orig_bio;
  66. /*
  67. * the start of a variable length array of checksums only
  68. * used by reads
  69. */
  70. u32 sums;
  71. };
  72. static int btrfs_decompress_biovec(int type, struct page **pages_in,
  73. u64 disk_start, struct bio_vec *bvec,
  74. int vcnt, size_t srclen);
  75. static inline int compressed_bio_size(struct btrfs_root *root,
  76. unsigned long disk_size)
  77. {
  78. u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
  79. return sizeof(struct compressed_bio) +
  80. ((disk_size + root->sectorsize - 1) / root->sectorsize) *
  81. csum_size;
  82. }
  83. static struct bio *compressed_bio_alloc(struct block_device *bdev,
  84. u64 first_byte, gfp_t gfp_flags)
  85. {
  86. int nr_vecs;
  87. nr_vecs = bio_get_nr_vecs(bdev);
  88. return btrfs_bio_alloc(bdev, first_byte >> 9, nr_vecs, gfp_flags);
  89. }
  90. static int check_compressed_csum(struct inode *inode,
  91. struct compressed_bio *cb,
  92. u64 disk_start)
  93. {
  94. int ret;
  95. struct page *page;
  96. unsigned long i;
  97. char *kaddr;
  98. u32 csum;
  99. u32 *cb_sum = &cb->sums;
  100. if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
  101. return 0;
  102. for (i = 0; i < cb->nr_pages; i++) {
  103. page = cb->compressed_pages[i];
  104. csum = ~(u32)0;
  105. kaddr = kmap_atomic(page);
  106. csum = btrfs_csum_data(kaddr, csum, PAGE_CACHE_SIZE);
  107. btrfs_csum_final(csum, (char *)&csum);
  108. kunmap_atomic(kaddr);
  109. if (csum != *cb_sum) {
  110. printk(KERN_INFO "btrfs csum failed ino %llu "
  111. "extent %llu csum %u "
  112. "wanted %u mirror %d\n",
  113. (unsigned long long)btrfs_ino(inode),
  114. (unsigned long long)disk_start,
  115. csum, *cb_sum, cb->mirror_num);
  116. ret = -EIO;
  117. goto fail;
  118. }
  119. cb_sum++;
  120. }
  121. ret = 0;
  122. fail:
  123. return ret;
  124. }
  125. /* when we finish reading compressed pages from the disk, we
  126. * decompress them and then run the bio end_io routines on the
  127. * decompressed pages (in the inode address space).
  128. *
  129. * This allows the checksumming and other IO error handling routines
  130. * to work normally
  131. *
  132. * The compressed pages are freed here, and it must be run
  133. * in process context
  134. */
  135. static void end_compressed_bio_read(struct bio *bio, int err)
  136. {
  137. struct compressed_bio *cb = bio->bi_private;
  138. struct inode *inode;
  139. struct page *page;
  140. unsigned long index;
  141. int ret;
  142. if (err)
  143. cb->errors = 1;
  144. /* if there are more bios still pending for this compressed
  145. * extent, just exit
  146. */
  147. if (!atomic_dec_and_test(&cb->pending_bios))
  148. goto out;
  149. inode = cb->inode;
  150. ret = check_compressed_csum(inode, cb, (u64)bio->bi_sector << 9);
  151. if (ret)
  152. goto csum_failed;
  153. /* ok, we're the last bio for this extent, lets start
  154. * the decompression.
  155. */
  156. ret = btrfs_decompress_biovec(cb->compress_type,
  157. cb->compressed_pages,
  158. cb->start,
  159. cb->orig_bio->bi_io_vec,
  160. cb->orig_bio->bi_vcnt,
  161. cb->compressed_len);
  162. csum_failed:
  163. if (ret)
  164. cb->errors = 1;
  165. /* release the compressed pages */
  166. index = 0;
  167. for (index = 0; index < cb->nr_pages; index++) {
  168. page = cb->compressed_pages[index];
  169. page->mapping = NULL;
  170. page_cache_release(page);
  171. }
  172. /* do io completion on the original bio */
  173. if (cb->errors) {
  174. bio_io_error(cb->orig_bio);
  175. } else {
  176. int bio_index = 0;
  177. struct bio_vec *bvec = cb->orig_bio->bi_io_vec;
  178. /*
  179. * we have verified the checksum already, set page
  180. * checked so the end_io handlers know about it
  181. */
  182. while (bio_index < cb->orig_bio->bi_vcnt) {
  183. SetPageChecked(bvec->bv_page);
  184. bvec++;
  185. bio_index++;
  186. }
  187. bio_endio(cb->orig_bio, 0);
  188. }
  189. /* finally free the cb struct */
  190. kfree(cb->compressed_pages);
  191. kfree(cb);
  192. out:
  193. bio_put(bio);
  194. }
  195. /*
  196. * Clear the writeback bits on all of the file
  197. * pages for a compressed write
  198. */
  199. static noinline void end_compressed_writeback(struct inode *inode, u64 start,
  200. unsigned long ram_size)
  201. {
  202. unsigned long index = start >> PAGE_CACHE_SHIFT;
  203. unsigned long end_index = (start + ram_size - 1) >> PAGE_CACHE_SHIFT;
  204. struct page *pages[16];
  205. unsigned long nr_pages = end_index - index + 1;
  206. int i;
  207. int ret;
  208. while (nr_pages > 0) {
  209. ret = find_get_pages_contig(inode->i_mapping, index,
  210. min_t(unsigned long,
  211. nr_pages, ARRAY_SIZE(pages)), pages);
  212. if (ret == 0) {
  213. nr_pages -= 1;
  214. index += 1;
  215. continue;
  216. }
  217. for (i = 0; i < ret; i++) {
  218. end_page_writeback(pages[i]);
  219. page_cache_release(pages[i]);
  220. }
  221. nr_pages -= ret;
  222. index += ret;
  223. }
  224. /* the inode may be gone now */
  225. }
  226. /*
  227. * do the cleanup once all the compressed pages hit the disk.
  228. * This will clear writeback on the file pages and free the compressed
  229. * pages.
  230. *
  231. * This also calls the writeback end hooks for the file pages so that
  232. * metadata and checksums can be updated in the file.
  233. */
  234. static void end_compressed_bio_write(struct bio *bio, int err)
  235. {
  236. struct extent_io_tree *tree;
  237. struct compressed_bio *cb = bio->bi_private;
  238. struct inode *inode;
  239. struct page *page;
  240. unsigned long index;
  241. if (err)
  242. cb->errors = 1;
  243. /* if there are more bios still pending for this compressed
  244. * extent, just exit
  245. */
  246. if (!atomic_dec_and_test(&cb->pending_bios))
  247. goto out;
  248. /* ok, we're the last bio for this extent, step one is to
  249. * call back into the FS and do all the end_io operations
  250. */
  251. inode = cb->inode;
  252. tree = &BTRFS_I(inode)->io_tree;
  253. cb->compressed_pages[0]->mapping = cb->inode->i_mapping;
  254. tree->ops->writepage_end_io_hook(cb->compressed_pages[0],
  255. cb->start,
  256. cb->start + cb->len - 1,
  257. NULL, 1);
  258. cb->compressed_pages[0]->mapping = NULL;
  259. end_compressed_writeback(inode, cb->start, cb->len);
  260. /* note, our inode could be gone now */
  261. /*
  262. * release the compressed pages, these came from alloc_page and
  263. * are not attached to the inode at all
  264. */
  265. index = 0;
  266. for (index = 0; index < cb->nr_pages; index++) {
  267. page = cb->compressed_pages[index];
  268. page->mapping = NULL;
  269. page_cache_release(page);
  270. }
  271. /* finally free the cb struct */
  272. kfree(cb->compressed_pages);
  273. kfree(cb);
  274. out:
  275. bio_put(bio);
  276. }
  277. /*
  278. * worker function to build and submit bios for previously compressed pages.
  279. * The corresponding pages in the inode should be marked for writeback
  280. * and the compressed pages should have a reference on them for dropping
  281. * when the IO is complete.
  282. *
  283. * This also checksums the file bytes and gets things ready for
  284. * the end io hooks.
  285. */
  286. int btrfs_submit_compressed_write(struct inode *inode, u64 start,
  287. unsigned long len, u64 disk_start,
  288. unsigned long compressed_len,
  289. struct page **compressed_pages,
  290. unsigned long nr_pages)
  291. {
  292. struct bio *bio = NULL;
  293. struct btrfs_root *root = BTRFS_I(inode)->root;
  294. struct compressed_bio *cb;
  295. unsigned long bytes_left;
  296. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  297. int pg_index = 0;
  298. struct page *page;
  299. u64 first_byte = disk_start;
  300. struct block_device *bdev;
  301. int ret;
  302. int skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
  303. WARN_ON(start & ((u64)PAGE_CACHE_SIZE - 1));
  304. cb = kmalloc(compressed_bio_size(root, compressed_len), GFP_NOFS);
  305. if (!cb)
  306. return -ENOMEM;
  307. atomic_set(&cb->pending_bios, 0);
  308. cb->errors = 0;
  309. cb->inode = inode;
  310. cb->start = start;
  311. cb->len = len;
  312. cb->mirror_num = 0;
  313. cb->compressed_pages = compressed_pages;
  314. cb->compressed_len = compressed_len;
  315. cb->orig_bio = NULL;
  316. cb->nr_pages = nr_pages;
  317. bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
  318. bio = compressed_bio_alloc(bdev, first_byte, GFP_NOFS);
  319. if(!bio) {
  320. kfree(cb);
  321. return -ENOMEM;
  322. }
  323. bio->bi_private = cb;
  324. bio->bi_end_io = end_compressed_bio_write;
  325. atomic_inc(&cb->pending_bios);
  326. /* create and submit bios for the compressed pages */
  327. bytes_left = compressed_len;
  328. for (pg_index = 0; pg_index < cb->nr_pages; pg_index++) {
  329. page = compressed_pages[pg_index];
  330. page->mapping = inode->i_mapping;
  331. if (bio->bi_size)
  332. ret = io_tree->ops->merge_bio_hook(WRITE, page, 0,
  333. PAGE_CACHE_SIZE,
  334. bio, 0);
  335. else
  336. ret = 0;
  337. page->mapping = NULL;
  338. if (ret || bio_add_page(bio, page, PAGE_CACHE_SIZE, 0) <
  339. PAGE_CACHE_SIZE) {
  340. bio_get(bio);
  341. /*
  342. * inc the count before we submit the bio so
  343. * we know the end IO handler won't happen before
  344. * we inc the count. Otherwise, the cb might get
  345. * freed before we're done setting it up
  346. */
  347. atomic_inc(&cb->pending_bios);
  348. ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
  349. BUG_ON(ret); /* -ENOMEM */
  350. if (!skip_sum) {
  351. ret = btrfs_csum_one_bio(root, inode, bio,
  352. start, 1);
  353. BUG_ON(ret); /* -ENOMEM */
  354. }
  355. ret = btrfs_map_bio(root, WRITE, bio, 0, 1);
  356. BUG_ON(ret); /* -ENOMEM */
  357. bio_put(bio);
  358. bio = compressed_bio_alloc(bdev, first_byte, GFP_NOFS);
  359. BUG_ON(!bio);
  360. bio->bi_private = cb;
  361. bio->bi_end_io = end_compressed_bio_write;
  362. bio_add_page(bio, page, PAGE_CACHE_SIZE, 0);
  363. }
  364. if (bytes_left < PAGE_CACHE_SIZE) {
  365. printk("bytes left %lu compress len %lu nr %lu\n",
  366. bytes_left, cb->compressed_len, cb->nr_pages);
  367. }
  368. bytes_left -= PAGE_CACHE_SIZE;
  369. first_byte += PAGE_CACHE_SIZE;
  370. cond_resched();
  371. }
  372. bio_get(bio);
  373. ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
  374. BUG_ON(ret); /* -ENOMEM */
  375. if (!skip_sum) {
  376. ret = btrfs_csum_one_bio(root, inode, bio, start, 1);
  377. BUG_ON(ret); /* -ENOMEM */
  378. }
  379. ret = btrfs_map_bio(root, WRITE, bio, 0, 1);
  380. BUG_ON(ret); /* -ENOMEM */
  381. bio_put(bio);
  382. return 0;
  383. }
  384. static noinline int add_ra_bio_pages(struct inode *inode,
  385. u64 compressed_end,
  386. struct compressed_bio *cb)
  387. {
  388. unsigned long end_index;
  389. unsigned long pg_index;
  390. u64 last_offset;
  391. u64 isize = i_size_read(inode);
  392. int ret;
  393. struct page *page;
  394. unsigned long nr_pages = 0;
  395. struct extent_map *em;
  396. struct address_space *mapping = inode->i_mapping;
  397. struct extent_map_tree *em_tree;
  398. struct extent_io_tree *tree;
  399. u64 end;
  400. int misses = 0;
  401. page = cb->orig_bio->bi_io_vec[cb->orig_bio->bi_vcnt - 1].bv_page;
  402. last_offset = (page_offset(page) + PAGE_CACHE_SIZE);
  403. em_tree = &BTRFS_I(inode)->extent_tree;
  404. tree = &BTRFS_I(inode)->io_tree;
  405. if (isize == 0)
  406. return 0;
  407. end_index = (i_size_read(inode) - 1) >> PAGE_CACHE_SHIFT;
  408. while (last_offset < compressed_end) {
  409. pg_index = last_offset >> PAGE_CACHE_SHIFT;
  410. if (pg_index > end_index)
  411. break;
  412. rcu_read_lock();
  413. page = radix_tree_lookup(&mapping->page_tree, pg_index);
  414. rcu_read_unlock();
  415. if (page) {
  416. misses++;
  417. if (misses > 4)
  418. break;
  419. goto next;
  420. }
  421. page = __page_cache_alloc(mapping_gfp_mask(mapping) &
  422. ~__GFP_FS);
  423. if (!page)
  424. break;
  425. if (add_to_page_cache_lru(page, mapping, pg_index,
  426. GFP_NOFS)) {
  427. page_cache_release(page);
  428. goto next;
  429. }
  430. end = last_offset + PAGE_CACHE_SIZE - 1;
  431. /*
  432. * at this point, we have a locked page in the page cache
  433. * for these bytes in the file. But, we have to make
  434. * sure they map to this compressed extent on disk.
  435. */
  436. set_page_extent_mapped(page);
  437. lock_extent(tree, last_offset, end);
  438. read_lock(&em_tree->lock);
  439. em = lookup_extent_mapping(em_tree, last_offset,
  440. PAGE_CACHE_SIZE);
  441. read_unlock(&em_tree->lock);
  442. if (!em || last_offset < em->start ||
  443. (last_offset + PAGE_CACHE_SIZE > extent_map_end(em)) ||
  444. (em->block_start >> 9) != cb->orig_bio->bi_sector) {
  445. free_extent_map(em);
  446. unlock_extent(tree, last_offset, end);
  447. unlock_page(page);
  448. page_cache_release(page);
  449. break;
  450. }
  451. free_extent_map(em);
  452. if (page->index == end_index) {
  453. char *userpage;
  454. size_t zero_offset = isize & (PAGE_CACHE_SIZE - 1);
  455. if (zero_offset) {
  456. int zeros;
  457. zeros = PAGE_CACHE_SIZE - zero_offset;
  458. userpage = kmap_atomic(page);
  459. memset(userpage + zero_offset, 0, zeros);
  460. flush_dcache_page(page);
  461. kunmap_atomic(userpage);
  462. }
  463. }
  464. ret = bio_add_page(cb->orig_bio, page,
  465. PAGE_CACHE_SIZE, 0);
  466. if (ret == PAGE_CACHE_SIZE) {
  467. nr_pages++;
  468. page_cache_release(page);
  469. } else {
  470. unlock_extent(tree, last_offset, end);
  471. unlock_page(page);
  472. page_cache_release(page);
  473. break;
  474. }
  475. next:
  476. last_offset += PAGE_CACHE_SIZE;
  477. }
  478. return 0;
  479. }
  480. /*
  481. * for a compressed read, the bio we get passed has all the inode pages
  482. * in it. We don't actually do IO on those pages but allocate new ones
  483. * to hold the compressed pages on disk.
  484. *
  485. * bio->bi_sector points to the compressed extent on disk
  486. * bio->bi_io_vec points to all of the inode pages
  487. * bio->bi_vcnt is a count of pages
  488. *
  489. * After the compressed pages are read, we copy the bytes into the
  490. * bio we were passed and then call the bio end_io calls
  491. */
  492. int btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
  493. int mirror_num, unsigned long bio_flags)
  494. {
  495. struct extent_io_tree *tree;
  496. struct extent_map_tree *em_tree;
  497. struct compressed_bio *cb;
  498. struct btrfs_root *root = BTRFS_I(inode)->root;
  499. unsigned long uncompressed_len = bio->bi_vcnt * PAGE_CACHE_SIZE;
  500. unsigned long compressed_len;
  501. unsigned long nr_pages;
  502. unsigned long pg_index;
  503. struct page *page;
  504. struct block_device *bdev;
  505. struct bio *comp_bio;
  506. u64 cur_disk_byte = (u64)bio->bi_sector << 9;
  507. u64 em_len;
  508. u64 em_start;
  509. struct extent_map *em;
  510. int ret = -ENOMEM;
  511. int faili = 0;
  512. u32 *sums;
  513. tree = &BTRFS_I(inode)->io_tree;
  514. em_tree = &BTRFS_I(inode)->extent_tree;
  515. /* we need the actual starting offset of this extent in the file */
  516. read_lock(&em_tree->lock);
  517. em = lookup_extent_mapping(em_tree,
  518. page_offset(bio->bi_io_vec->bv_page),
  519. PAGE_CACHE_SIZE);
  520. read_unlock(&em_tree->lock);
  521. if (!em)
  522. return -EIO;
  523. compressed_len = em->block_len;
  524. cb = kmalloc(compressed_bio_size(root, compressed_len), GFP_NOFS);
  525. if (!cb)
  526. goto out;
  527. atomic_set(&cb->pending_bios, 0);
  528. cb->errors = 0;
  529. cb->inode = inode;
  530. cb->mirror_num = mirror_num;
  531. sums = &cb->sums;
  532. cb->start = em->orig_start;
  533. em_len = em->len;
  534. em_start = em->start;
  535. free_extent_map(em);
  536. em = NULL;
  537. cb->len = uncompressed_len;
  538. cb->compressed_len = compressed_len;
  539. cb->compress_type = extent_compress_type(bio_flags);
  540. cb->orig_bio = bio;
  541. nr_pages = (compressed_len + PAGE_CACHE_SIZE - 1) /
  542. PAGE_CACHE_SIZE;
  543. cb->compressed_pages = kzalloc(sizeof(struct page *) * nr_pages,
  544. GFP_NOFS);
  545. if (!cb->compressed_pages)
  546. goto fail1;
  547. bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
  548. for (pg_index = 0; pg_index < nr_pages; pg_index++) {
  549. cb->compressed_pages[pg_index] = alloc_page(GFP_NOFS |
  550. __GFP_HIGHMEM);
  551. if (!cb->compressed_pages[pg_index]) {
  552. faili = pg_index - 1;
  553. ret = -ENOMEM;
  554. goto fail2;
  555. }
  556. }
  557. faili = nr_pages - 1;
  558. cb->nr_pages = nr_pages;
  559. /* In the parent-locked case, we only locked the range we are
  560. * interested in. In all other cases, we can opportunistically
  561. * cache decompressed data that goes beyond the requested range. */
  562. if (!(bio_flags & EXTENT_BIO_PARENT_LOCKED))
  563. add_ra_bio_pages(inode, em_start + em_len, cb);
  564. /* include any pages we added in add_ra-bio_pages */
  565. uncompressed_len = bio->bi_vcnt * PAGE_CACHE_SIZE;
  566. cb->len = uncompressed_len;
  567. comp_bio = compressed_bio_alloc(bdev, cur_disk_byte, GFP_NOFS);
  568. if (!comp_bio)
  569. goto fail2;
  570. comp_bio->bi_private = cb;
  571. comp_bio->bi_end_io = end_compressed_bio_read;
  572. atomic_inc(&cb->pending_bios);
  573. for (pg_index = 0; pg_index < nr_pages; pg_index++) {
  574. page = cb->compressed_pages[pg_index];
  575. page->mapping = inode->i_mapping;
  576. page->index = em_start >> PAGE_CACHE_SHIFT;
  577. if (comp_bio->bi_size)
  578. ret = tree->ops->merge_bio_hook(READ, page, 0,
  579. PAGE_CACHE_SIZE,
  580. comp_bio, 0);
  581. else
  582. ret = 0;
  583. page->mapping = NULL;
  584. if (ret || bio_add_page(comp_bio, page, PAGE_CACHE_SIZE, 0) <
  585. PAGE_CACHE_SIZE) {
  586. bio_get(comp_bio);
  587. ret = btrfs_bio_wq_end_io(root->fs_info, comp_bio, 0);
  588. BUG_ON(ret); /* -ENOMEM */
  589. /*
  590. * inc the count before we submit the bio so
  591. * we know the end IO handler won't happen before
  592. * we inc the count. Otherwise, the cb might get
  593. * freed before we're done setting it up
  594. */
  595. atomic_inc(&cb->pending_bios);
  596. if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
  597. ret = btrfs_lookup_bio_sums(root, inode,
  598. comp_bio, sums);
  599. BUG_ON(ret); /* -ENOMEM */
  600. }
  601. sums += (comp_bio->bi_size + root->sectorsize - 1) /
  602. root->sectorsize;
  603. ret = btrfs_map_bio(root, READ, comp_bio,
  604. mirror_num, 0);
  605. if (ret)
  606. bio_endio(comp_bio, ret);
  607. bio_put(comp_bio);
  608. comp_bio = compressed_bio_alloc(bdev, cur_disk_byte,
  609. GFP_NOFS);
  610. BUG_ON(!comp_bio);
  611. comp_bio->bi_private = cb;
  612. comp_bio->bi_end_io = end_compressed_bio_read;
  613. bio_add_page(comp_bio, page, PAGE_CACHE_SIZE, 0);
  614. }
  615. cur_disk_byte += PAGE_CACHE_SIZE;
  616. }
  617. bio_get(comp_bio);
  618. ret = btrfs_bio_wq_end_io(root->fs_info, comp_bio, 0);
  619. BUG_ON(ret); /* -ENOMEM */
  620. if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
  621. ret = btrfs_lookup_bio_sums(root, inode, comp_bio, sums);
  622. BUG_ON(ret); /* -ENOMEM */
  623. }
  624. ret = btrfs_map_bio(root, READ, comp_bio, mirror_num, 0);
  625. if (ret)
  626. bio_endio(comp_bio, ret);
  627. bio_put(comp_bio);
  628. return 0;
  629. fail2:
  630. while (faili >= 0) {
  631. __free_page(cb->compressed_pages[faili]);
  632. faili--;
  633. }
  634. kfree(cb->compressed_pages);
  635. fail1:
  636. kfree(cb);
  637. out:
  638. free_extent_map(em);
  639. return ret;
  640. }
  641. static struct list_head comp_idle_workspace[BTRFS_COMPRESS_TYPES];
  642. static spinlock_t comp_workspace_lock[BTRFS_COMPRESS_TYPES];
  643. static int comp_num_workspace[BTRFS_COMPRESS_TYPES];
  644. static atomic_t comp_alloc_workspace[BTRFS_COMPRESS_TYPES];
  645. static wait_queue_head_t comp_workspace_wait[BTRFS_COMPRESS_TYPES];
  646. static struct btrfs_compress_op *btrfs_compress_op[] = {
  647. &btrfs_zlib_compress,
  648. &btrfs_lzo_compress,
  649. };
  650. void __init btrfs_init_compress(void)
  651. {
  652. int i;
  653. for (i = 0; i < BTRFS_COMPRESS_TYPES; i++) {
  654. INIT_LIST_HEAD(&comp_idle_workspace[i]);
  655. spin_lock_init(&comp_workspace_lock[i]);
  656. atomic_set(&comp_alloc_workspace[i], 0);
  657. init_waitqueue_head(&comp_workspace_wait[i]);
  658. }
  659. }
  660. /*
  661. * this finds an available workspace or allocates a new one
  662. * ERR_PTR is returned if things go bad.
  663. */
  664. static struct list_head *find_workspace(int type)
  665. {
  666. struct list_head *workspace;
  667. int cpus = num_online_cpus();
  668. int idx = type - 1;
  669. struct list_head *idle_workspace = &comp_idle_workspace[idx];
  670. spinlock_t *workspace_lock = &comp_workspace_lock[idx];
  671. atomic_t *alloc_workspace = &comp_alloc_workspace[idx];
  672. wait_queue_head_t *workspace_wait = &comp_workspace_wait[idx];
  673. int *num_workspace = &comp_num_workspace[idx];
  674. again:
  675. spin_lock(workspace_lock);
  676. if (!list_empty(idle_workspace)) {
  677. workspace = idle_workspace->next;
  678. list_del(workspace);
  679. (*num_workspace)--;
  680. spin_unlock(workspace_lock);
  681. return workspace;
  682. }
  683. if (atomic_read(alloc_workspace) > cpus) {
  684. DEFINE_WAIT(wait);
  685. spin_unlock(workspace_lock);
  686. prepare_to_wait(workspace_wait, &wait, TASK_UNINTERRUPTIBLE);
  687. if (atomic_read(alloc_workspace) > cpus && !*num_workspace)
  688. schedule();
  689. finish_wait(workspace_wait, &wait);
  690. goto again;
  691. }
  692. atomic_inc(alloc_workspace);
  693. spin_unlock(workspace_lock);
  694. workspace = btrfs_compress_op[idx]->alloc_workspace();
  695. if (IS_ERR(workspace)) {
  696. atomic_dec(alloc_workspace);
  697. wake_up(workspace_wait);
  698. }
  699. return workspace;
  700. }
  701. /*
  702. * put a workspace struct back on the list or free it if we have enough
  703. * idle ones sitting around
  704. */
  705. static void free_workspace(int type, struct list_head *workspace)
  706. {
  707. int idx = type - 1;
  708. struct list_head *idle_workspace = &comp_idle_workspace[idx];
  709. spinlock_t *workspace_lock = &comp_workspace_lock[idx];
  710. atomic_t *alloc_workspace = &comp_alloc_workspace[idx];
  711. wait_queue_head_t *workspace_wait = &comp_workspace_wait[idx];
  712. int *num_workspace = &comp_num_workspace[idx];
  713. spin_lock(workspace_lock);
  714. if (*num_workspace < num_online_cpus()) {
  715. list_add_tail(workspace, idle_workspace);
  716. (*num_workspace)++;
  717. spin_unlock(workspace_lock);
  718. goto wake;
  719. }
  720. spin_unlock(workspace_lock);
  721. btrfs_compress_op[idx]->free_workspace(workspace);
  722. atomic_dec(alloc_workspace);
  723. wake:
  724. smp_mb();
  725. if (waitqueue_active(workspace_wait))
  726. wake_up(workspace_wait);
  727. }
  728. /*
  729. * cleanup function for module exit
  730. */
  731. static void free_workspaces(void)
  732. {
  733. struct list_head *workspace;
  734. int i;
  735. for (i = 0; i < BTRFS_COMPRESS_TYPES; i++) {
  736. while (!list_empty(&comp_idle_workspace[i])) {
  737. workspace = comp_idle_workspace[i].next;
  738. list_del(workspace);
  739. btrfs_compress_op[i]->free_workspace(workspace);
  740. atomic_dec(&comp_alloc_workspace[i]);
  741. }
  742. }
  743. }
  744. /*
  745. * given an address space and start/len, compress the bytes.
  746. *
  747. * pages are allocated to hold the compressed result and stored
  748. * in 'pages'
  749. *
  750. * out_pages is used to return the number of pages allocated. There
  751. * may be pages allocated even if we return an error
  752. *
  753. * total_in is used to return the number of bytes actually read. It
  754. * may be smaller then len if we had to exit early because we
  755. * ran out of room in the pages array or because we cross the
  756. * max_out threshold.
  757. *
  758. * total_out is used to return the total number of compressed bytes
  759. *
  760. * max_out tells us the max number of bytes that we're allowed to
  761. * stuff into pages
  762. */
  763. int btrfs_compress_pages(int type, struct address_space *mapping,
  764. u64 start, unsigned long len,
  765. struct page **pages,
  766. unsigned long nr_dest_pages,
  767. unsigned long *out_pages,
  768. unsigned long *total_in,
  769. unsigned long *total_out,
  770. unsigned long max_out)
  771. {
  772. struct list_head *workspace;
  773. int ret;
  774. workspace = find_workspace(type);
  775. if (IS_ERR(workspace))
  776. return -1;
  777. ret = btrfs_compress_op[type-1]->compress_pages(workspace, mapping,
  778. start, len, pages,
  779. nr_dest_pages, out_pages,
  780. total_in, total_out,
  781. max_out);
  782. free_workspace(type, workspace);
  783. return ret;
  784. }
  785. /*
  786. * pages_in is an array of pages with compressed data.
  787. *
  788. * disk_start is the starting logical offset of this array in the file
  789. *
  790. * bvec is a bio_vec of pages from the file that we want to decompress into
  791. *
  792. * vcnt is the count of pages in the biovec
  793. *
  794. * srclen is the number of bytes in pages_in
  795. *
  796. * The basic idea is that we have a bio that was created by readpages.
  797. * The pages in the bio are for the uncompressed data, and they may not
  798. * be contiguous. They all correspond to the range of bytes covered by
  799. * the compressed extent.
  800. */
  801. static int btrfs_decompress_biovec(int type, struct page **pages_in,
  802. u64 disk_start, struct bio_vec *bvec,
  803. int vcnt, size_t srclen)
  804. {
  805. struct list_head *workspace;
  806. int ret;
  807. workspace = find_workspace(type);
  808. if (IS_ERR(workspace))
  809. return -ENOMEM;
  810. ret = btrfs_compress_op[type-1]->decompress_biovec(workspace, pages_in,
  811. disk_start,
  812. bvec, vcnt, srclen);
  813. free_workspace(type, workspace);
  814. return ret;
  815. }
  816. /*
  817. * a less complex decompression routine. Our compressed data fits in a
  818. * single page, and we want to read a single page out of it.
  819. * start_byte tells us the offset into the compressed data we're interested in
  820. */
  821. int btrfs_decompress(int type, unsigned char *data_in, struct page *dest_page,
  822. unsigned long start_byte, size_t srclen, size_t destlen)
  823. {
  824. struct list_head *workspace;
  825. int ret;
  826. workspace = find_workspace(type);
  827. if (IS_ERR(workspace))
  828. return -ENOMEM;
  829. ret = btrfs_compress_op[type-1]->decompress(workspace, data_in,
  830. dest_page, start_byte,
  831. srclen, destlen);
  832. free_workspace(type, workspace);
  833. return ret;
  834. }
  835. void btrfs_exit_compress(void)
  836. {
  837. free_workspaces();
  838. }
  839. /*
  840. * Copy uncompressed data from working buffer to pages.
  841. *
  842. * buf_start is the byte offset we're of the start of our workspace buffer.
  843. *
  844. * total_out is the last byte of the buffer
  845. */
  846. int btrfs_decompress_buf2page(char *buf, unsigned long buf_start,
  847. unsigned long total_out, u64 disk_start,
  848. struct bio_vec *bvec, int vcnt,
  849. unsigned long *pg_index,
  850. unsigned long *pg_offset)
  851. {
  852. unsigned long buf_offset;
  853. unsigned long current_buf_start;
  854. unsigned long start_byte;
  855. unsigned long working_bytes = total_out - buf_start;
  856. unsigned long bytes;
  857. char *kaddr;
  858. struct page *page_out = bvec[*pg_index].bv_page;
  859. /*
  860. * start byte is the first byte of the page we're currently
  861. * copying into relative to the start of the compressed data.
  862. */
  863. start_byte = page_offset(page_out) - disk_start;
  864. /* we haven't yet hit data corresponding to this page */
  865. if (total_out <= start_byte)
  866. return 1;
  867. /*
  868. * the start of the data we care about is offset into
  869. * the middle of our working buffer
  870. */
  871. if (total_out > start_byte && buf_start < start_byte) {
  872. buf_offset = start_byte - buf_start;
  873. working_bytes -= buf_offset;
  874. } else {
  875. buf_offset = 0;
  876. }
  877. current_buf_start = buf_start;
  878. /* copy bytes from the working buffer into the pages */
  879. while (working_bytes > 0) {
  880. bytes = min(PAGE_CACHE_SIZE - *pg_offset,
  881. PAGE_CACHE_SIZE - buf_offset);
  882. bytes = min(bytes, working_bytes);
  883. kaddr = kmap_atomic(page_out);
  884. memcpy(kaddr + *pg_offset, buf + buf_offset, bytes);
  885. kunmap_atomic(kaddr);
  886. flush_dcache_page(page_out);
  887. *pg_offset += bytes;
  888. buf_offset += bytes;
  889. working_bytes -= bytes;
  890. current_buf_start += bytes;
  891. /* check if we need to pick another page */
  892. if (*pg_offset == PAGE_CACHE_SIZE) {
  893. (*pg_index)++;
  894. if (*pg_index >= vcnt)
  895. return 0;
  896. page_out = bvec[*pg_index].bv_page;
  897. *pg_offset = 0;
  898. start_byte = page_offset(page_out) - disk_start;
  899. /*
  900. * make sure our new page is covered by this
  901. * working buffer
  902. */
  903. if (total_out <= start_byte)
  904. return 1;
  905. /*
  906. * the next page in the biovec might not be adjacent
  907. * to the last page, but it might still be found
  908. * inside this working buffer. bump our offset pointer
  909. */
  910. if (total_out > start_byte &&
  911. current_buf_start < start_byte) {
  912. buf_offset = start_byte - buf_start;
  913. working_bytes = total_out - start_byte;
  914. current_buf_start = buf_start + buf_offset;
  915. }
  916. }
  917. }
  918. return 1;
  919. }