compression.c 28 KB

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