compression.c 28 KB

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