compression.c 26 KB

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