compression.c 27 KB

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