compression.c 27 KB

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