compression.c 28 KB

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