compression.c 27 KB

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