compression.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  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. };
  611. void __init btrfs_init_compress(void)
  612. {
  613. int i;
  614. for (i = 0; i < BTRFS_COMPRESS_TYPES; i++) {
  615. struct list_head *workspace;
  616. INIT_LIST_HEAD(&btrfs_comp_ws[i].idle_ws);
  617. spin_lock_init(&btrfs_comp_ws[i].ws_lock);
  618. atomic_set(&btrfs_comp_ws[i].total_ws, 0);
  619. init_waitqueue_head(&btrfs_comp_ws[i].ws_wait);
  620. /*
  621. * Preallocate one workspace for each compression type so
  622. * we can guarantee forward progress in the worst case
  623. */
  624. workspace = btrfs_compress_op[i]->alloc_workspace();
  625. if (IS_ERR(workspace)) {
  626. pr_warn("BTRFS: cannot preallocate compression workspace, will try later\n");
  627. } else {
  628. atomic_set(&btrfs_comp_ws[i].total_ws, 1);
  629. btrfs_comp_ws[i].free_ws = 1;
  630. list_add(workspace, &btrfs_comp_ws[i].idle_ws);
  631. }
  632. }
  633. }
  634. /*
  635. * This finds an available workspace or allocates a new one.
  636. * If it's not possible to allocate a new one, waits until there's one.
  637. * Preallocation makes a forward progress guarantees and we do not return
  638. * errors.
  639. */
  640. static struct list_head *find_workspace(int type)
  641. {
  642. struct list_head *workspace;
  643. int cpus = num_online_cpus();
  644. int idx = type - 1;
  645. unsigned nofs_flag;
  646. struct list_head *idle_ws = &btrfs_comp_ws[idx].idle_ws;
  647. spinlock_t *ws_lock = &btrfs_comp_ws[idx].ws_lock;
  648. atomic_t *total_ws = &btrfs_comp_ws[idx].total_ws;
  649. wait_queue_head_t *ws_wait = &btrfs_comp_ws[idx].ws_wait;
  650. int *free_ws = &btrfs_comp_ws[idx].free_ws;
  651. again:
  652. spin_lock(ws_lock);
  653. if (!list_empty(idle_ws)) {
  654. workspace = idle_ws->next;
  655. list_del(workspace);
  656. (*free_ws)--;
  657. spin_unlock(ws_lock);
  658. return workspace;
  659. }
  660. if (atomic_read(total_ws) > cpus) {
  661. DEFINE_WAIT(wait);
  662. spin_unlock(ws_lock);
  663. prepare_to_wait(ws_wait, &wait, TASK_UNINTERRUPTIBLE);
  664. if (atomic_read(total_ws) > cpus && !*free_ws)
  665. schedule();
  666. finish_wait(ws_wait, &wait);
  667. goto again;
  668. }
  669. atomic_inc(total_ws);
  670. spin_unlock(ws_lock);
  671. /*
  672. * Allocation helpers call vmalloc that can't use GFP_NOFS, so we have
  673. * to turn it off here because we might get called from the restricted
  674. * context of btrfs_compress_bio/btrfs_compress_pages
  675. */
  676. nofs_flag = memalloc_nofs_save();
  677. workspace = btrfs_compress_op[idx]->alloc_workspace();
  678. memalloc_nofs_restore(nofs_flag);
  679. if (IS_ERR(workspace)) {
  680. atomic_dec(total_ws);
  681. wake_up(ws_wait);
  682. /*
  683. * Do not return the error but go back to waiting. There's a
  684. * workspace preallocated for each type and the compression
  685. * time is bounded so we get to a workspace eventually. This
  686. * makes our caller's life easier.
  687. *
  688. * To prevent silent and low-probability deadlocks (when the
  689. * initial preallocation fails), check if there are any
  690. * workspaces at all.
  691. */
  692. if (atomic_read(total_ws) == 0) {
  693. static DEFINE_RATELIMIT_STATE(_rs,
  694. /* once per minute */ 60 * HZ,
  695. /* no burst */ 1);
  696. if (__ratelimit(&_rs)) {
  697. pr_warn("BTRFS: no compression workspaces, low memory, retrying\n");
  698. }
  699. }
  700. goto again;
  701. }
  702. return workspace;
  703. }
  704. /*
  705. * put a workspace struct back on the list or free it if we have enough
  706. * idle ones sitting around
  707. */
  708. static void free_workspace(int type, struct list_head *workspace)
  709. {
  710. int idx = type - 1;
  711. struct list_head *idle_ws = &btrfs_comp_ws[idx].idle_ws;
  712. spinlock_t *ws_lock = &btrfs_comp_ws[idx].ws_lock;
  713. atomic_t *total_ws = &btrfs_comp_ws[idx].total_ws;
  714. wait_queue_head_t *ws_wait = &btrfs_comp_ws[idx].ws_wait;
  715. int *free_ws = &btrfs_comp_ws[idx].free_ws;
  716. spin_lock(ws_lock);
  717. if (*free_ws < num_online_cpus()) {
  718. list_add(workspace, idle_ws);
  719. (*free_ws)++;
  720. spin_unlock(ws_lock);
  721. goto wake;
  722. }
  723. spin_unlock(ws_lock);
  724. btrfs_compress_op[idx]->free_workspace(workspace);
  725. atomic_dec(total_ws);
  726. wake:
  727. /*
  728. * Make sure counter is updated before we wake up waiters.
  729. */
  730. smp_mb();
  731. if (waitqueue_active(ws_wait))
  732. wake_up(ws_wait);
  733. }
  734. /*
  735. * cleanup function for module exit
  736. */
  737. static void free_workspaces(void)
  738. {
  739. struct list_head *workspace;
  740. int i;
  741. for (i = 0; i < BTRFS_COMPRESS_TYPES; i++) {
  742. while (!list_empty(&btrfs_comp_ws[i].idle_ws)) {
  743. workspace = btrfs_comp_ws[i].idle_ws.next;
  744. list_del(workspace);
  745. btrfs_compress_op[i]->free_workspace(workspace);
  746. atomic_dec(&btrfs_comp_ws[i].total_ws);
  747. }
  748. }
  749. }
  750. /*
  751. * Given an address space and start and length, compress the bytes into @pages
  752. * that are allocated on demand.
  753. *
  754. * @out_pages is an in/out parameter, holds maximum number of pages to allocate
  755. * and returns number of actually allocated pages
  756. *
  757. * @total_in is used to return the number of bytes actually read. It
  758. * may be smaller than the input length if we had to exit early because we
  759. * ran out of room in the pages array or because we cross the
  760. * max_out threshold.
  761. *
  762. * @total_out is an in/out parameter, must be set to the input length and will
  763. * be also used to return the total number of compressed bytes
  764. *
  765. * @max_out tells us the max number of bytes that we're allowed to
  766. * stuff into pages
  767. */
  768. int btrfs_compress_pages(int type, struct address_space *mapping,
  769. u64 start, struct page **pages,
  770. unsigned long *out_pages,
  771. unsigned long *total_in,
  772. unsigned long *total_out)
  773. {
  774. struct list_head *workspace;
  775. int ret;
  776. workspace = find_workspace(type);
  777. ret = btrfs_compress_op[type-1]->compress_pages(workspace, mapping,
  778. start, pages,
  779. out_pages,
  780. total_in, total_out);
  781. free_workspace(type, workspace);
  782. return ret;
  783. }
  784. /*
  785. * pages_in is an array of pages with compressed data.
  786. *
  787. * disk_start is the starting logical offset of this array in the file
  788. *
  789. * orig_bio contains the pages from the file that we want to decompress into
  790. *
  791. * srclen is the number of bytes in pages_in
  792. *
  793. * The basic idea is that we have a bio that was created by readpages.
  794. * The pages in the bio are for the uncompressed data, and they may not
  795. * be contiguous. They all correspond to the range of bytes covered by
  796. * the compressed extent.
  797. */
  798. static int btrfs_decompress_bio(struct compressed_bio *cb)
  799. {
  800. struct list_head *workspace;
  801. int ret;
  802. int type = cb->compress_type;
  803. workspace = find_workspace(type);
  804. ret = btrfs_compress_op[type - 1]->decompress_bio(workspace, cb);
  805. free_workspace(type, workspace);
  806. return ret;
  807. }
  808. /*
  809. * a less complex decompression routine. Our compressed data fits in a
  810. * single page, and we want to read a single page out of it.
  811. * start_byte tells us the offset into the compressed data we're interested in
  812. */
  813. int btrfs_decompress(int type, unsigned char *data_in, struct page *dest_page,
  814. unsigned long start_byte, size_t srclen, size_t destlen)
  815. {
  816. struct list_head *workspace;
  817. int ret;
  818. workspace = find_workspace(type);
  819. ret = btrfs_compress_op[type-1]->decompress(workspace, data_in,
  820. dest_page, start_byte,
  821. srclen, destlen);
  822. free_workspace(type, workspace);
  823. return ret;
  824. }
  825. void btrfs_exit_compress(void)
  826. {
  827. free_workspaces();
  828. }
  829. /*
  830. * Copy uncompressed data from working buffer to pages.
  831. *
  832. * buf_start is the byte offset we're of the start of our workspace buffer.
  833. *
  834. * total_out is the last byte of the buffer
  835. */
  836. int btrfs_decompress_buf2page(const char *buf, unsigned long buf_start,
  837. unsigned long total_out, u64 disk_start,
  838. struct bio *bio)
  839. {
  840. unsigned long buf_offset;
  841. unsigned long current_buf_start;
  842. unsigned long start_byte;
  843. unsigned long prev_start_byte;
  844. unsigned long working_bytes = total_out - buf_start;
  845. unsigned long bytes;
  846. char *kaddr;
  847. struct bio_vec bvec = bio_iter_iovec(bio, bio->bi_iter);
  848. /*
  849. * start byte is the first byte of the page we're currently
  850. * copying into relative to the start of the compressed data.
  851. */
  852. start_byte = page_offset(bvec.bv_page) - disk_start;
  853. /* we haven't yet hit data corresponding to this page */
  854. if (total_out <= start_byte)
  855. return 1;
  856. /*
  857. * the start of the data we care about is offset into
  858. * the middle of our working buffer
  859. */
  860. if (total_out > start_byte && buf_start < start_byte) {
  861. buf_offset = start_byte - buf_start;
  862. working_bytes -= buf_offset;
  863. } else {
  864. buf_offset = 0;
  865. }
  866. current_buf_start = buf_start;
  867. /* copy bytes from the working buffer into the pages */
  868. while (working_bytes > 0) {
  869. bytes = min_t(unsigned long, bvec.bv_len,
  870. PAGE_SIZE - buf_offset);
  871. bytes = min(bytes, working_bytes);
  872. kaddr = kmap_atomic(bvec.bv_page);
  873. memcpy(kaddr + bvec.bv_offset, buf + buf_offset, bytes);
  874. kunmap_atomic(kaddr);
  875. flush_dcache_page(bvec.bv_page);
  876. buf_offset += bytes;
  877. working_bytes -= bytes;
  878. current_buf_start += bytes;
  879. /* check if we need to pick another page */
  880. bio_advance(bio, bytes);
  881. if (!bio->bi_iter.bi_size)
  882. return 0;
  883. bvec = bio_iter_iovec(bio, bio->bi_iter);
  884. prev_start_byte = start_byte;
  885. start_byte = page_offset(bvec.bv_page) - disk_start;
  886. /*
  887. * We need to make sure we're only adjusting
  888. * our offset into compression working buffer when
  889. * we're switching pages. Otherwise we can incorrectly
  890. * keep copying when we were actually done.
  891. */
  892. if (start_byte != prev_start_byte) {
  893. /*
  894. * make sure our new page is covered by this
  895. * working buffer
  896. */
  897. if (total_out <= start_byte)
  898. return 1;
  899. /*
  900. * the next page in the biovec might not be adjacent
  901. * to the last page, but it might still be found
  902. * inside this working buffer. bump our offset pointer
  903. */
  904. if (total_out > start_byte &&
  905. current_buf_start < start_byte) {
  906. buf_offset = start_byte - buf_start;
  907. working_bytes = total_out - start_byte;
  908. current_buf_start = buf_start + buf_offset;
  909. }
  910. }
  911. }
  912. return 1;
  913. }