compression.c 28 KB

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