compression.c 26 KB

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