compression.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  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_biovec(int type, struct page **pages_in,
  72. u64 disk_start, struct bio_vec *bvec,
  73. int vcnt, size_t srclen);
  74. static inline int compressed_bio_size(struct btrfs_root *root,
  75. unsigned long disk_size)
  76. {
  77. u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
  78. return sizeof(struct compressed_bio) +
  79. (DIV_ROUND_UP(disk_size, root->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. int nr_vecs;
  85. nr_vecs = bio_get_nr_vecs(bdev);
  86. return btrfs_bio_alloc(bdev, first_byte >> 9, nr_vecs, gfp_flags);
  87. }
  88. static int check_compressed_csum(struct inode *inode,
  89. struct compressed_bio *cb,
  90. u64 disk_start)
  91. {
  92. int ret;
  93. struct page *page;
  94. unsigned long i;
  95. char *kaddr;
  96. u32 csum;
  97. u32 *cb_sum = &cb->sums;
  98. if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
  99. return 0;
  100. for (i = 0; i < cb->nr_pages; i++) {
  101. page = cb->compressed_pages[i];
  102. csum = ~(u32)0;
  103. kaddr = kmap_atomic(page);
  104. csum = btrfs_csum_data(kaddr, csum, PAGE_CACHE_SIZE);
  105. btrfs_csum_final(csum, (char *)&csum);
  106. kunmap_atomic(kaddr);
  107. if (csum != *cb_sum) {
  108. btrfs_info(BTRFS_I(inode)->root->fs_info,
  109. "csum failed ino %llu extent %llu csum %u wanted %u mirror %d",
  110. btrfs_ino(inode), disk_start, csum, *cb_sum,
  111. cb->mirror_num);
  112. ret = -EIO;
  113. goto fail;
  114. }
  115. cb_sum++;
  116. }
  117. ret = 0;
  118. fail:
  119. return ret;
  120. }
  121. /* when we finish reading compressed pages from the disk, we
  122. * decompress them and then run the bio end_io routines on the
  123. * decompressed pages (in the inode address space).
  124. *
  125. * This allows the checksumming and other IO error handling routines
  126. * to work normally
  127. *
  128. * The compressed pages are freed here, and it must be run
  129. * in process context
  130. */
  131. static void end_compressed_bio_read(struct bio *bio, int err)
  132. {
  133. struct compressed_bio *cb = bio->bi_private;
  134. struct inode *inode;
  135. struct page *page;
  136. unsigned long index;
  137. int ret;
  138. if (err)
  139. cb->errors = 1;
  140. /* if there are more bios still pending for this compressed
  141. * extent, just exit
  142. */
  143. if (!atomic_dec_and_test(&cb->pending_bios))
  144. goto out;
  145. inode = cb->inode;
  146. ret = check_compressed_csum(inode, cb,
  147. (u64)bio->bi_iter.bi_sector << 9);
  148. if (ret)
  149. goto csum_failed;
  150. /* ok, we're the last bio for this extent, lets start
  151. * the decompression.
  152. */
  153. ret = btrfs_decompress_biovec(cb->compress_type,
  154. cb->compressed_pages,
  155. cb->start,
  156. cb->orig_bio->bi_io_vec,
  157. cb->orig_bio->bi_vcnt,
  158. cb->compressed_len);
  159. csum_failed:
  160. if (ret)
  161. cb->errors = 1;
  162. /* release the compressed pages */
  163. index = 0;
  164. for (index = 0; index < cb->nr_pages; index++) {
  165. page = cb->compressed_pages[index];
  166. page->mapping = NULL;
  167. page_cache_release(page);
  168. }
  169. /* do io completion on the original bio */
  170. if (cb->errors) {
  171. bio_io_error(cb->orig_bio);
  172. } else {
  173. int i;
  174. struct bio_vec *bvec;
  175. /*
  176. * we have verified the checksum already, set page
  177. * checked so the end_io handlers know about it
  178. */
  179. bio_for_each_segment_all(bvec, cb->orig_bio, i)
  180. SetPageChecked(bvec->bv_page);
  181. bio_endio(cb->orig_bio, 0);
  182. }
  183. /* finally free the cb struct */
  184. kfree(cb->compressed_pages);
  185. kfree(cb);
  186. out:
  187. bio_put(bio);
  188. }
  189. /*
  190. * Clear the writeback bits on all of the file
  191. * pages for a compressed write
  192. */
  193. static noinline void end_compressed_writeback(struct inode *inode, u64 start,
  194. unsigned long ram_size)
  195. {
  196. unsigned long index = start >> PAGE_CACHE_SHIFT;
  197. unsigned long end_index = (start + ram_size - 1) >> PAGE_CACHE_SHIFT;
  198. struct page *pages[16];
  199. unsigned long nr_pages = end_index - index + 1;
  200. int i;
  201. int ret;
  202. while (nr_pages > 0) {
  203. ret = find_get_pages_contig(inode->i_mapping, index,
  204. min_t(unsigned long,
  205. nr_pages, ARRAY_SIZE(pages)), pages);
  206. if (ret == 0) {
  207. nr_pages -= 1;
  208. index += 1;
  209. continue;
  210. }
  211. for (i = 0; i < ret; i++) {
  212. end_page_writeback(pages[i]);
  213. page_cache_release(pages[i]);
  214. }
  215. nr_pages -= ret;
  216. index += ret;
  217. }
  218. /* the inode may be gone now */
  219. }
  220. /*
  221. * do the cleanup once all the compressed pages hit the disk.
  222. * This will clear writeback on the file pages and free the compressed
  223. * pages.
  224. *
  225. * This also calls the writeback end hooks for the file pages so that
  226. * metadata and checksums can be updated in the file.
  227. */
  228. static void end_compressed_bio_write(struct bio *bio, int err)
  229. {
  230. struct extent_io_tree *tree;
  231. struct compressed_bio *cb = bio->bi_private;
  232. struct inode *inode;
  233. struct page *page;
  234. unsigned long index;
  235. if (err)
  236. cb->errors = 1;
  237. /* if there are more bios still pending for this compressed
  238. * extent, just exit
  239. */
  240. if (!atomic_dec_and_test(&cb->pending_bios))
  241. goto out;
  242. /* ok, we're the last bio for this extent, step one is to
  243. * call back into the FS and do all the end_io operations
  244. */
  245. inode = cb->inode;
  246. tree = &BTRFS_I(inode)->io_tree;
  247. cb->compressed_pages[0]->mapping = cb->inode->i_mapping;
  248. tree->ops->writepage_end_io_hook(cb->compressed_pages[0],
  249. cb->start,
  250. cb->start + cb->len - 1,
  251. NULL, 1);
  252. cb->compressed_pages[0]->mapping = NULL;
  253. end_compressed_writeback(inode, cb->start, cb->len);
  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. page_cache_release(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 bio *bio = NULL;
  287. struct btrfs_root *root = BTRFS_I(inode)->root;
  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_CACHE_SIZE - 1));
  298. cb = kmalloc(compressed_bio_size(root, 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 = BTRFS_I(inode)->root->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->bi_private = cb;
  318. bio->bi_end_io = end_compressed_bio_write;
  319. atomic_inc(&cb->pending_bios);
  320. /* create and submit bios for the compressed pages */
  321. bytes_left = compressed_len;
  322. for (pg_index = 0; pg_index < cb->nr_pages; pg_index++) {
  323. page = compressed_pages[pg_index];
  324. page->mapping = inode->i_mapping;
  325. if (bio->bi_iter.bi_size)
  326. ret = io_tree->ops->merge_bio_hook(WRITE, page, 0,
  327. PAGE_CACHE_SIZE,
  328. bio, 0);
  329. else
  330. ret = 0;
  331. page->mapping = NULL;
  332. if (ret || bio_add_page(bio, page, PAGE_CACHE_SIZE, 0) <
  333. PAGE_CACHE_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. atomic_inc(&cb->pending_bios);
  342. ret = btrfs_bio_wq_end_io(root->fs_info, bio,
  343. BTRFS_WQ_ENDIO_DATA);
  344. BUG_ON(ret); /* -ENOMEM */
  345. if (!skip_sum) {
  346. ret = btrfs_csum_one_bio(root, inode, bio,
  347. start, 1);
  348. BUG_ON(ret); /* -ENOMEM */
  349. }
  350. ret = btrfs_map_bio(root, WRITE, bio, 0, 1);
  351. BUG_ON(ret); /* -ENOMEM */
  352. bio_put(bio);
  353. bio = compressed_bio_alloc(bdev, first_byte, GFP_NOFS);
  354. BUG_ON(!bio);
  355. bio->bi_private = cb;
  356. bio->bi_end_io = end_compressed_bio_write;
  357. bio_add_page(bio, page, PAGE_CACHE_SIZE, 0);
  358. }
  359. if (bytes_left < PAGE_CACHE_SIZE) {
  360. btrfs_info(BTRFS_I(inode)->root->fs_info,
  361. "bytes left %lu compress len %lu nr %lu",
  362. bytes_left, cb->compressed_len, cb->nr_pages);
  363. }
  364. bytes_left -= PAGE_CACHE_SIZE;
  365. first_byte += PAGE_CACHE_SIZE;
  366. cond_resched();
  367. }
  368. bio_get(bio);
  369. ret = btrfs_bio_wq_end_io(root->fs_info, bio, BTRFS_WQ_ENDIO_DATA);
  370. BUG_ON(ret); /* -ENOMEM */
  371. if (!skip_sum) {
  372. ret = btrfs_csum_one_bio(root, inode, bio, start, 1);
  373. BUG_ON(ret); /* -ENOMEM */
  374. }
  375. ret = btrfs_map_bio(root, WRITE, bio, 0, 1);
  376. BUG_ON(ret); /* -ENOMEM */
  377. bio_put(bio);
  378. return 0;
  379. }
  380. static noinline int add_ra_bio_pages(struct inode *inode,
  381. u64 compressed_end,
  382. struct compressed_bio *cb)
  383. {
  384. unsigned long end_index;
  385. unsigned long pg_index;
  386. u64 last_offset;
  387. u64 isize = i_size_read(inode);
  388. int ret;
  389. struct page *page;
  390. unsigned long nr_pages = 0;
  391. struct extent_map *em;
  392. struct address_space *mapping = inode->i_mapping;
  393. struct extent_map_tree *em_tree;
  394. struct extent_io_tree *tree;
  395. u64 end;
  396. int misses = 0;
  397. page = cb->orig_bio->bi_io_vec[cb->orig_bio->bi_vcnt - 1].bv_page;
  398. last_offset = (page_offset(page) + PAGE_CACHE_SIZE);
  399. em_tree = &BTRFS_I(inode)->extent_tree;
  400. tree = &BTRFS_I(inode)->io_tree;
  401. if (isize == 0)
  402. return 0;
  403. end_index = (i_size_read(inode) - 1) >> PAGE_CACHE_SHIFT;
  404. while (last_offset < compressed_end) {
  405. pg_index = last_offset >> PAGE_CACHE_SHIFT;
  406. if (pg_index > end_index)
  407. break;
  408. rcu_read_lock();
  409. page = radix_tree_lookup(&mapping->page_tree, pg_index);
  410. rcu_read_unlock();
  411. if (page && !radix_tree_exceptional_entry(page)) {
  412. misses++;
  413. if (misses > 4)
  414. break;
  415. goto next;
  416. }
  417. page = __page_cache_alloc(mapping_gfp_mask(mapping) &
  418. ~__GFP_FS);
  419. if (!page)
  420. break;
  421. if (add_to_page_cache_lru(page, mapping, pg_index,
  422. GFP_NOFS)) {
  423. page_cache_release(page);
  424. goto next;
  425. }
  426. end = last_offset + PAGE_CACHE_SIZE - 1;
  427. /*
  428. * at this point, we have a locked page in the page cache
  429. * for these bytes in the file. But, we have to make
  430. * sure they map to this compressed extent on disk.
  431. */
  432. set_page_extent_mapped(page);
  433. lock_extent(tree, last_offset, end);
  434. read_lock(&em_tree->lock);
  435. em = lookup_extent_mapping(em_tree, last_offset,
  436. PAGE_CACHE_SIZE);
  437. read_unlock(&em_tree->lock);
  438. if (!em || last_offset < em->start ||
  439. (last_offset + PAGE_CACHE_SIZE > extent_map_end(em)) ||
  440. (em->block_start >> 9) != cb->orig_bio->bi_iter.bi_sector) {
  441. free_extent_map(em);
  442. unlock_extent(tree, last_offset, end);
  443. unlock_page(page);
  444. page_cache_release(page);
  445. break;
  446. }
  447. free_extent_map(em);
  448. if (page->index == end_index) {
  449. char *userpage;
  450. size_t zero_offset = isize & (PAGE_CACHE_SIZE - 1);
  451. if (zero_offset) {
  452. int zeros;
  453. zeros = PAGE_CACHE_SIZE - zero_offset;
  454. userpage = kmap_atomic(page);
  455. memset(userpage + zero_offset, 0, zeros);
  456. flush_dcache_page(page);
  457. kunmap_atomic(userpage);
  458. }
  459. }
  460. ret = bio_add_page(cb->orig_bio, page,
  461. PAGE_CACHE_SIZE, 0);
  462. if (ret == PAGE_CACHE_SIZE) {
  463. nr_pages++;
  464. page_cache_release(page);
  465. } else {
  466. unlock_extent(tree, last_offset, end);
  467. unlock_page(page);
  468. page_cache_release(page);
  469. break;
  470. }
  471. next:
  472. last_offset += PAGE_CACHE_SIZE;
  473. }
  474. return 0;
  475. }
  476. /*
  477. * for a compressed read, the bio we get passed has all the inode pages
  478. * in it. We don't actually do IO on those pages but allocate new ones
  479. * to hold the compressed pages on disk.
  480. *
  481. * bio->bi_iter.bi_sector points to the compressed extent on disk
  482. * bio->bi_io_vec points to all of the inode pages
  483. * bio->bi_vcnt is a count of pages
  484. *
  485. * After the compressed pages are read, we copy the bytes into the
  486. * bio we were passed and then call the bio end_io calls
  487. */
  488. int btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
  489. int mirror_num, unsigned long bio_flags)
  490. {
  491. struct extent_io_tree *tree;
  492. struct extent_map_tree *em_tree;
  493. struct compressed_bio *cb;
  494. struct btrfs_root *root = BTRFS_I(inode)->root;
  495. unsigned long uncompressed_len = bio->bi_vcnt * PAGE_CACHE_SIZE;
  496. unsigned long compressed_len;
  497. unsigned long nr_pages;
  498. unsigned long pg_index;
  499. struct page *page;
  500. struct block_device *bdev;
  501. struct bio *comp_bio;
  502. u64 cur_disk_byte = (u64)bio->bi_iter.bi_sector << 9;
  503. u64 em_len;
  504. u64 em_start;
  505. struct extent_map *em;
  506. int ret = -ENOMEM;
  507. int faili = 0;
  508. u32 *sums;
  509. tree = &BTRFS_I(inode)->io_tree;
  510. em_tree = &BTRFS_I(inode)->extent_tree;
  511. /* we need the actual starting offset of this extent in the file */
  512. read_lock(&em_tree->lock);
  513. em = lookup_extent_mapping(em_tree,
  514. page_offset(bio->bi_io_vec->bv_page),
  515. PAGE_CACHE_SIZE);
  516. read_unlock(&em_tree->lock);
  517. if (!em)
  518. return -EIO;
  519. compressed_len = em->block_len;
  520. cb = kmalloc(compressed_bio_size(root, compressed_len), GFP_NOFS);
  521. if (!cb)
  522. goto out;
  523. atomic_set(&cb->pending_bios, 0);
  524. cb->errors = 0;
  525. cb->inode = inode;
  526. cb->mirror_num = mirror_num;
  527. sums = &cb->sums;
  528. cb->start = em->orig_start;
  529. em_len = em->len;
  530. em_start = em->start;
  531. free_extent_map(em);
  532. em = NULL;
  533. cb->len = uncompressed_len;
  534. cb->compressed_len = compressed_len;
  535. cb->compress_type = extent_compress_type(bio_flags);
  536. cb->orig_bio = bio;
  537. nr_pages = DIV_ROUND_UP(compressed_len, PAGE_CACHE_SIZE);
  538. cb->compressed_pages = kzalloc(sizeof(struct page *) * nr_pages,
  539. GFP_NOFS);
  540. if (!cb->compressed_pages)
  541. goto fail1;
  542. bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
  543. for (pg_index = 0; pg_index < nr_pages; pg_index++) {
  544. cb->compressed_pages[pg_index] = alloc_page(GFP_NOFS |
  545. __GFP_HIGHMEM);
  546. if (!cb->compressed_pages[pg_index]) {
  547. faili = pg_index - 1;
  548. ret = -ENOMEM;
  549. goto fail2;
  550. }
  551. }
  552. faili = nr_pages - 1;
  553. cb->nr_pages = nr_pages;
  554. /* In the parent-locked case, we only locked the range we are
  555. * interested in. In all other cases, we can opportunistically
  556. * cache decompressed data that goes beyond the requested range. */
  557. if (!(bio_flags & EXTENT_BIO_PARENT_LOCKED))
  558. add_ra_bio_pages(inode, em_start + em_len, cb);
  559. /* include any pages we added in add_ra-bio_pages */
  560. uncompressed_len = bio->bi_vcnt * PAGE_CACHE_SIZE;
  561. cb->len = uncompressed_len;
  562. comp_bio = compressed_bio_alloc(bdev, cur_disk_byte, GFP_NOFS);
  563. if (!comp_bio)
  564. goto fail2;
  565. comp_bio->bi_private = cb;
  566. comp_bio->bi_end_io = end_compressed_bio_read;
  567. atomic_inc(&cb->pending_bios);
  568. for (pg_index = 0; pg_index < nr_pages; pg_index++) {
  569. page = cb->compressed_pages[pg_index];
  570. page->mapping = inode->i_mapping;
  571. page->index = em_start >> PAGE_CACHE_SHIFT;
  572. if (comp_bio->bi_iter.bi_size)
  573. ret = tree->ops->merge_bio_hook(READ, page, 0,
  574. PAGE_CACHE_SIZE,
  575. comp_bio, 0);
  576. else
  577. ret = 0;
  578. page->mapping = NULL;
  579. if (ret || bio_add_page(comp_bio, page, PAGE_CACHE_SIZE, 0) <
  580. PAGE_CACHE_SIZE) {
  581. bio_get(comp_bio);
  582. ret = btrfs_bio_wq_end_io(root->fs_info, comp_bio,
  583. BTRFS_WQ_ENDIO_DATA);
  584. BUG_ON(ret); /* -ENOMEM */
  585. /*
  586. * inc the count before we submit the bio so
  587. * we know the end IO handler won't happen before
  588. * we inc the count. Otherwise, the cb might get
  589. * freed before we're done setting it up
  590. */
  591. atomic_inc(&cb->pending_bios);
  592. if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
  593. ret = btrfs_lookup_bio_sums(root, inode,
  594. comp_bio, sums);
  595. BUG_ON(ret); /* -ENOMEM */
  596. }
  597. sums += DIV_ROUND_UP(comp_bio->bi_iter.bi_size,
  598. root->sectorsize);
  599. ret = btrfs_map_bio(root, READ, comp_bio,
  600. mirror_num, 0);
  601. if (ret)
  602. bio_endio(comp_bio, ret);
  603. bio_put(comp_bio);
  604. comp_bio = compressed_bio_alloc(bdev, cur_disk_byte,
  605. GFP_NOFS);
  606. BUG_ON(!comp_bio);
  607. comp_bio->bi_private = cb;
  608. comp_bio->bi_end_io = end_compressed_bio_read;
  609. bio_add_page(comp_bio, page, PAGE_CACHE_SIZE, 0);
  610. }
  611. cur_disk_byte += PAGE_CACHE_SIZE;
  612. }
  613. bio_get(comp_bio);
  614. ret = btrfs_bio_wq_end_io(root->fs_info, comp_bio,
  615. BTRFS_WQ_ENDIO_DATA);
  616. BUG_ON(ret); /* -ENOMEM */
  617. if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
  618. ret = btrfs_lookup_bio_sums(root, inode, comp_bio, sums);
  619. BUG_ON(ret); /* -ENOMEM */
  620. }
  621. ret = btrfs_map_bio(root, READ, comp_bio, mirror_num, 0);
  622. if (ret)
  623. bio_endio(comp_bio, ret);
  624. bio_put(comp_bio);
  625. return 0;
  626. fail2:
  627. while (faili >= 0) {
  628. __free_page(cb->compressed_pages[faili]);
  629. faili--;
  630. }
  631. kfree(cb->compressed_pages);
  632. fail1:
  633. kfree(cb);
  634. out:
  635. free_extent_map(em);
  636. return ret;
  637. }
  638. static struct list_head comp_idle_workspace[BTRFS_COMPRESS_TYPES];
  639. static spinlock_t comp_workspace_lock[BTRFS_COMPRESS_TYPES];
  640. static int comp_num_workspace[BTRFS_COMPRESS_TYPES];
  641. static atomic_t comp_alloc_workspace[BTRFS_COMPRESS_TYPES];
  642. static wait_queue_head_t comp_workspace_wait[BTRFS_COMPRESS_TYPES];
  643. static struct btrfs_compress_op *btrfs_compress_op[] = {
  644. &btrfs_zlib_compress,
  645. &btrfs_lzo_compress,
  646. };
  647. void __init btrfs_init_compress(void)
  648. {
  649. int i;
  650. for (i = 0; i < BTRFS_COMPRESS_TYPES; i++) {
  651. INIT_LIST_HEAD(&comp_idle_workspace[i]);
  652. spin_lock_init(&comp_workspace_lock[i]);
  653. atomic_set(&comp_alloc_workspace[i], 0);
  654. init_waitqueue_head(&comp_workspace_wait[i]);
  655. }
  656. }
  657. /*
  658. * this finds an available workspace or allocates a new one
  659. * ERR_PTR is returned if things go bad.
  660. */
  661. static struct list_head *find_workspace(int type)
  662. {
  663. struct list_head *workspace;
  664. int cpus = num_online_cpus();
  665. int idx = type - 1;
  666. struct list_head *idle_workspace = &comp_idle_workspace[idx];
  667. spinlock_t *workspace_lock = &comp_workspace_lock[idx];
  668. atomic_t *alloc_workspace = &comp_alloc_workspace[idx];
  669. wait_queue_head_t *workspace_wait = &comp_workspace_wait[idx];
  670. int *num_workspace = &comp_num_workspace[idx];
  671. again:
  672. spin_lock(workspace_lock);
  673. if (!list_empty(idle_workspace)) {
  674. workspace = idle_workspace->next;
  675. list_del(workspace);
  676. (*num_workspace)--;
  677. spin_unlock(workspace_lock);
  678. return workspace;
  679. }
  680. if (atomic_read(alloc_workspace) > cpus) {
  681. DEFINE_WAIT(wait);
  682. spin_unlock(workspace_lock);
  683. prepare_to_wait(workspace_wait, &wait, TASK_UNINTERRUPTIBLE);
  684. if (atomic_read(alloc_workspace) > cpus && !*num_workspace)
  685. schedule();
  686. finish_wait(workspace_wait, &wait);
  687. goto again;
  688. }
  689. atomic_inc(alloc_workspace);
  690. spin_unlock(workspace_lock);
  691. workspace = btrfs_compress_op[idx]->alloc_workspace();
  692. if (IS_ERR(workspace)) {
  693. atomic_dec(alloc_workspace);
  694. wake_up(workspace_wait);
  695. }
  696. return workspace;
  697. }
  698. /*
  699. * put a workspace struct back on the list or free it if we have enough
  700. * idle ones sitting around
  701. */
  702. static void free_workspace(int type, struct list_head *workspace)
  703. {
  704. int idx = type - 1;
  705. struct list_head *idle_workspace = &comp_idle_workspace[idx];
  706. spinlock_t *workspace_lock = &comp_workspace_lock[idx];
  707. atomic_t *alloc_workspace = &comp_alloc_workspace[idx];
  708. wait_queue_head_t *workspace_wait = &comp_workspace_wait[idx];
  709. int *num_workspace = &comp_num_workspace[idx];
  710. spin_lock(workspace_lock);
  711. if (*num_workspace < num_online_cpus()) {
  712. list_add(workspace, idle_workspace);
  713. (*num_workspace)++;
  714. spin_unlock(workspace_lock);
  715. goto wake;
  716. }
  717. spin_unlock(workspace_lock);
  718. btrfs_compress_op[idx]->free_workspace(workspace);
  719. atomic_dec(alloc_workspace);
  720. wake:
  721. smp_mb();
  722. if (waitqueue_active(workspace_wait))
  723. wake_up(workspace_wait);
  724. }
  725. /*
  726. * cleanup function for module exit
  727. */
  728. static void free_workspaces(void)
  729. {
  730. struct list_head *workspace;
  731. int i;
  732. for (i = 0; i < BTRFS_COMPRESS_TYPES; i++) {
  733. while (!list_empty(&comp_idle_workspace[i])) {
  734. workspace = comp_idle_workspace[i].next;
  735. list_del(workspace);
  736. btrfs_compress_op[i]->free_workspace(workspace);
  737. atomic_dec(&comp_alloc_workspace[i]);
  738. }
  739. }
  740. }
  741. /*
  742. * given an address space and start/len, compress the bytes.
  743. *
  744. * pages are allocated to hold the compressed result and stored
  745. * in 'pages'
  746. *
  747. * out_pages is used to return the number of pages allocated. There
  748. * may be pages allocated even if we return an error
  749. *
  750. * total_in is used to return the number of bytes actually read. It
  751. * may be smaller then len if we had to exit early because we
  752. * ran out of room in the pages array or because we cross the
  753. * max_out threshold.
  754. *
  755. * total_out is used to return the total number of compressed bytes
  756. *
  757. * max_out tells us the max number of bytes that we're allowed to
  758. * stuff into pages
  759. */
  760. int btrfs_compress_pages(int type, struct address_space *mapping,
  761. u64 start, unsigned long len,
  762. struct page **pages,
  763. unsigned long nr_dest_pages,
  764. unsigned long *out_pages,
  765. unsigned long *total_in,
  766. unsigned long *total_out,
  767. unsigned long max_out)
  768. {
  769. struct list_head *workspace;
  770. int ret;
  771. workspace = find_workspace(type);
  772. if (IS_ERR(workspace))
  773. return PTR_ERR(workspace);
  774. ret = btrfs_compress_op[type-1]->compress_pages(workspace, mapping,
  775. start, len, pages,
  776. nr_dest_pages, out_pages,
  777. total_in, total_out,
  778. max_out);
  779. free_workspace(type, workspace);
  780. return ret;
  781. }
  782. /*
  783. * pages_in is an array of pages with compressed data.
  784. *
  785. * disk_start is the starting logical offset of this array in the file
  786. *
  787. * bvec is a bio_vec of pages from the file that we want to decompress into
  788. *
  789. * vcnt is the count of pages in the biovec
  790. *
  791. * srclen is the number of bytes in pages_in
  792. *
  793. * The basic idea is that we have a bio that was created by readpages.
  794. * The pages in the bio are for the uncompressed data, and they may not
  795. * be contiguous. They all correspond to the range of bytes covered by
  796. * the compressed extent.
  797. */
  798. static int btrfs_decompress_biovec(int type, struct page **pages_in,
  799. u64 disk_start, struct bio_vec *bvec,
  800. int vcnt, size_t srclen)
  801. {
  802. struct list_head *workspace;
  803. int ret;
  804. workspace = find_workspace(type);
  805. if (IS_ERR(workspace))
  806. return PTR_ERR(workspace);
  807. ret = btrfs_compress_op[type-1]->decompress_biovec(workspace, pages_in,
  808. disk_start,
  809. bvec, vcnt, srclen);
  810. free_workspace(type, workspace);
  811. return ret;
  812. }
  813. /*
  814. * a less complex decompression routine. Our compressed data fits in a
  815. * single page, and we want to read a single page out of it.
  816. * start_byte tells us the offset into the compressed data we're interested in
  817. */
  818. int btrfs_decompress(int type, unsigned char *data_in, struct page *dest_page,
  819. unsigned long start_byte, size_t srclen, size_t destlen)
  820. {
  821. struct list_head *workspace;
  822. int ret;
  823. workspace = find_workspace(type);
  824. if (IS_ERR(workspace))
  825. return PTR_ERR(workspace);
  826. ret = btrfs_compress_op[type-1]->decompress(workspace, data_in,
  827. dest_page, start_byte,
  828. srclen, destlen);
  829. free_workspace(type, workspace);
  830. return ret;
  831. }
  832. void btrfs_exit_compress(void)
  833. {
  834. free_workspaces();
  835. }
  836. /*
  837. * Copy uncompressed data from working buffer to pages.
  838. *
  839. * buf_start is the byte offset we're of the start of our workspace buffer.
  840. *
  841. * total_out is the last byte of the buffer
  842. */
  843. int btrfs_decompress_buf2page(char *buf, unsigned long buf_start,
  844. unsigned long total_out, u64 disk_start,
  845. struct bio_vec *bvec, int vcnt,
  846. unsigned long *pg_index,
  847. unsigned long *pg_offset)
  848. {
  849. unsigned long buf_offset;
  850. unsigned long current_buf_start;
  851. unsigned long start_byte;
  852. unsigned long working_bytes = total_out - buf_start;
  853. unsigned long bytes;
  854. char *kaddr;
  855. struct page *page_out = bvec[*pg_index].bv_page;
  856. /*
  857. * start byte is the first byte of the page we're currently
  858. * copying into relative to the start of the compressed data.
  859. */
  860. start_byte = page_offset(page_out) - disk_start;
  861. /* we haven't yet hit data corresponding to this page */
  862. if (total_out <= start_byte)
  863. return 1;
  864. /*
  865. * the start of the data we care about is offset into
  866. * the middle of our working buffer
  867. */
  868. if (total_out > start_byte && buf_start < start_byte) {
  869. buf_offset = start_byte - buf_start;
  870. working_bytes -= buf_offset;
  871. } else {
  872. buf_offset = 0;
  873. }
  874. current_buf_start = buf_start;
  875. /* copy bytes from the working buffer into the pages */
  876. while (working_bytes > 0) {
  877. bytes = min(PAGE_CACHE_SIZE - *pg_offset,
  878. PAGE_CACHE_SIZE - buf_offset);
  879. bytes = min(bytes, working_bytes);
  880. kaddr = kmap_atomic(page_out);
  881. memcpy(kaddr + *pg_offset, buf + buf_offset, bytes);
  882. kunmap_atomic(kaddr);
  883. flush_dcache_page(page_out);
  884. *pg_offset += bytes;
  885. buf_offset += bytes;
  886. working_bytes -= bytes;
  887. current_buf_start += bytes;
  888. /* check if we need to pick another page */
  889. if (*pg_offset == PAGE_CACHE_SIZE) {
  890. (*pg_index)++;
  891. if (*pg_index >= vcnt)
  892. return 0;
  893. page_out = bvec[*pg_index].bv_page;
  894. *pg_offset = 0;
  895. start_byte = page_offset(page_out) - disk_start;
  896. /*
  897. * make sure our new page is covered by this
  898. * working buffer
  899. */
  900. if (total_out <= start_byte)
  901. return 1;
  902. /*
  903. * the next page in the biovec might not be adjacent
  904. * to the last page, but it might still be found
  905. * inside this working buffer. bump our offset pointer
  906. */
  907. if (total_out > start_byte &&
  908. current_buf_start < start_byte) {
  909. buf_offset = start_byte - buf_start;
  910. working_bytes = total_out - start_byte;
  911. current_buf_start = buf_start + buf_offset;
  912. }
  913. }
  914. }
  915. return 1;
  916. }
  917. /*
  918. * When uncompressing data, we need to make sure and zero any parts of
  919. * the biovec that were not filled in by the decompression code. pg_index
  920. * and pg_offset indicate the last page and the last offset of that page
  921. * that have been filled in. This will zero everything remaining in the
  922. * biovec.
  923. */
  924. void btrfs_clear_biovec_end(struct bio_vec *bvec, int vcnt,
  925. unsigned long pg_index,
  926. unsigned long pg_offset)
  927. {
  928. while (pg_index < vcnt) {
  929. struct page *page = bvec[pg_index].bv_page;
  930. unsigned long off = bvec[pg_index].bv_offset;
  931. unsigned long len = bvec[pg_index].bv_len;
  932. if (pg_offset < off)
  933. pg_offset = off;
  934. if (pg_offset < off + len) {
  935. unsigned long bytes = off + len - pg_offset;
  936. char *kaddr;
  937. kaddr = kmap_atomic(page);
  938. memset(kaddr + pg_offset, 0, bytes);
  939. kunmap_atomic(kaddr);
  940. }
  941. pg_index++;
  942. pg_offset = 0;
  943. }
  944. }