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