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