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