compression.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  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);
  104. csum = btrfs_csum_data(root, kaddr, csum, PAGE_CACHE_SIZE);
  105. btrfs_csum_final(csum, (char *)&csum);
  106. kunmap_atomic(kaddr);
  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 void 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. }
  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 pg_index = 0;
  296. struct page *page;
  297. u64 first_byte = disk_start;
  298. struct block_device *bdev;
  299. int ret;
  300. int skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
  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 (pg_index = 0; pg_index < cb->nr_pages; pg_index++) {
  327. page = compressed_pages[pg_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); /* -ENOMEM */
  348. if (!skip_sum) {
  349. ret = btrfs_csum_one_bio(root, inode, bio,
  350. start, 1);
  351. BUG_ON(ret); /* -ENOMEM */
  352. }
  353. ret = btrfs_map_bio(root, WRITE, bio, 0, 1);
  354. BUG_ON(ret); /* -ENOMEM */
  355. bio_put(bio);
  356. bio = compressed_bio_alloc(bdev, first_byte, GFP_NOFS);
  357. BUG_ON(!bio);
  358. bio->bi_private = cb;
  359. bio->bi_end_io = end_compressed_bio_write;
  360. bio_add_page(bio, page, PAGE_CACHE_SIZE, 0);
  361. }
  362. if (bytes_left < PAGE_CACHE_SIZE) {
  363. printk("bytes left %lu compress len %lu nr %lu\n",
  364. bytes_left, cb->compressed_len, cb->nr_pages);
  365. }
  366. bytes_left -= PAGE_CACHE_SIZE;
  367. first_byte += PAGE_CACHE_SIZE;
  368. cond_resched();
  369. }
  370. bio_get(bio);
  371. ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
  372. BUG_ON(ret); /* -ENOMEM */
  373. if (!skip_sum) {
  374. ret = btrfs_csum_one_bio(root, inode, bio, start, 1);
  375. BUG_ON(ret); /* -ENOMEM */
  376. }
  377. ret = btrfs_map_bio(root, WRITE, bio, 0, 1);
  378. BUG_ON(ret); /* -ENOMEM */
  379. bio_put(bio);
  380. return 0;
  381. }
  382. static noinline int add_ra_bio_pages(struct inode *inode,
  383. u64 compressed_end,
  384. struct compressed_bio *cb)
  385. {
  386. unsigned long end_index;
  387. unsigned long pg_index;
  388. u64 last_offset;
  389. u64 isize = i_size_read(inode);
  390. int ret;
  391. struct page *page;
  392. unsigned long nr_pages = 0;
  393. struct extent_map *em;
  394. struct address_space *mapping = inode->i_mapping;
  395. struct extent_map_tree *em_tree;
  396. struct extent_io_tree *tree;
  397. u64 end;
  398. int misses = 0;
  399. page = cb->orig_bio->bi_io_vec[cb->orig_bio->bi_vcnt - 1].bv_page;
  400. last_offset = (page_offset(page) + PAGE_CACHE_SIZE);
  401. em_tree = &BTRFS_I(inode)->extent_tree;
  402. tree = &BTRFS_I(inode)->io_tree;
  403. if (isize == 0)
  404. return 0;
  405. end_index = (i_size_read(inode) - 1) >> PAGE_CACHE_SHIFT;
  406. while (last_offset < compressed_end) {
  407. pg_index = last_offset >> PAGE_CACHE_SHIFT;
  408. if (pg_index > end_index)
  409. break;
  410. rcu_read_lock();
  411. page = radix_tree_lookup(&mapping->page_tree, pg_index);
  412. rcu_read_unlock();
  413. if (page) {
  414. misses++;
  415. if (misses > 4)
  416. break;
  417. goto next;
  418. }
  419. page = __page_cache_alloc(mapping_gfp_mask(mapping) &
  420. ~__GFP_FS);
  421. if (!page)
  422. break;
  423. if (add_to_page_cache_lru(page, mapping, pg_index,
  424. GFP_NOFS)) {
  425. page_cache_release(page);
  426. goto next;
  427. }
  428. end = last_offset + PAGE_CACHE_SIZE - 1;
  429. /*
  430. * at this point, we have a locked page in the page cache
  431. * for these bytes in the file. But, we have to make
  432. * sure they map to this compressed extent on disk.
  433. */
  434. set_page_extent_mapped(page);
  435. lock_extent(tree, last_offset, end);
  436. read_lock(&em_tree->lock);
  437. em = lookup_extent_mapping(em_tree, last_offset,
  438. PAGE_CACHE_SIZE);
  439. read_unlock(&em_tree->lock);
  440. if (!em || last_offset < em->start ||
  441. (last_offset + PAGE_CACHE_SIZE > extent_map_end(em)) ||
  442. (em->block_start >> 9) != cb->orig_bio->bi_sector) {
  443. free_extent_map(em);
  444. unlock_extent(tree, last_offset, end);
  445. unlock_page(page);
  446. page_cache_release(page);
  447. break;
  448. }
  449. free_extent_map(em);
  450. if (page->index == end_index) {
  451. char *userpage;
  452. size_t zero_offset = isize & (PAGE_CACHE_SIZE - 1);
  453. if (zero_offset) {
  454. int zeros;
  455. zeros = PAGE_CACHE_SIZE - zero_offset;
  456. userpage = kmap_atomic(page);
  457. memset(userpage + zero_offset, 0, zeros);
  458. flush_dcache_page(page);
  459. kunmap_atomic(userpage);
  460. }
  461. }
  462. ret = bio_add_page(cb->orig_bio, page,
  463. PAGE_CACHE_SIZE, 0);
  464. if (ret == PAGE_CACHE_SIZE) {
  465. nr_pages++;
  466. page_cache_release(page);
  467. } else {
  468. unlock_extent(tree, last_offset, end);
  469. unlock_page(page);
  470. page_cache_release(page);
  471. break;
  472. }
  473. next:
  474. last_offset += PAGE_CACHE_SIZE;
  475. }
  476. return 0;
  477. }
  478. /*
  479. * for a compressed read, the bio we get passed has all the inode pages
  480. * in it. We don't actually do IO on those pages but allocate new ones
  481. * to hold the compressed pages on disk.
  482. *
  483. * bio->bi_sector points to the compressed extent on disk
  484. * bio->bi_io_vec points to all of the inode pages
  485. * bio->bi_vcnt is a count of pages
  486. *
  487. * After the compressed pages are read, we copy the bytes into the
  488. * bio we were passed and then call the bio end_io calls
  489. */
  490. int btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
  491. int mirror_num, unsigned long bio_flags)
  492. {
  493. struct extent_io_tree *tree;
  494. struct extent_map_tree *em_tree;
  495. struct compressed_bio *cb;
  496. struct btrfs_root *root = BTRFS_I(inode)->root;
  497. unsigned long uncompressed_len = bio->bi_vcnt * PAGE_CACHE_SIZE;
  498. unsigned long compressed_len;
  499. unsigned long nr_pages;
  500. unsigned long pg_index;
  501. struct page *page;
  502. struct block_device *bdev;
  503. struct bio *comp_bio;
  504. u64 cur_disk_byte = (u64)bio->bi_sector << 9;
  505. u64 em_len;
  506. u64 em_start;
  507. struct extent_map *em;
  508. int ret = -ENOMEM;
  509. u32 *sums;
  510. tree = &BTRFS_I(inode)->io_tree;
  511. em_tree = &BTRFS_I(inode)->extent_tree;
  512. /* we need the actual starting offset of this extent in the file */
  513. read_lock(&em_tree->lock);
  514. em = lookup_extent_mapping(em_tree,
  515. page_offset(bio->bi_io_vec->bv_page),
  516. PAGE_CACHE_SIZE);
  517. read_unlock(&em_tree->lock);
  518. if (!em)
  519. return -EIO;
  520. compressed_len = em->block_len;
  521. cb = kmalloc(compressed_bio_size(root, compressed_len), GFP_NOFS);
  522. if (!cb)
  523. goto out;
  524. atomic_set(&cb->pending_bios, 0);
  525. cb->errors = 0;
  526. cb->inode = inode;
  527. cb->mirror_num = mirror_num;
  528. sums = &cb->sums;
  529. cb->start = em->orig_start;
  530. em_len = em->len;
  531. em_start = em->start;
  532. free_extent_map(em);
  533. em = NULL;
  534. cb->len = uncompressed_len;
  535. cb->compressed_len = compressed_len;
  536. cb->compress_type = extent_compress_type(bio_flags);
  537. cb->orig_bio = bio;
  538. nr_pages = (compressed_len + PAGE_CACHE_SIZE - 1) /
  539. PAGE_CACHE_SIZE;
  540. cb->compressed_pages = kzalloc(sizeof(struct page *) * nr_pages,
  541. GFP_NOFS);
  542. if (!cb->compressed_pages)
  543. goto fail1;
  544. bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
  545. for (pg_index = 0; pg_index < nr_pages; pg_index++) {
  546. cb->compressed_pages[pg_index] = alloc_page(GFP_NOFS |
  547. __GFP_HIGHMEM);
  548. if (!cb->compressed_pages[pg_index])
  549. goto fail2;
  550. }
  551. cb->nr_pages = nr_pages;
  552. add_ra_bio_pages(inode, em_start + em_len, cb);
  553. /* include any pages we added in add_ra-bio_pages */
  554. uncompressed_len = bio->bi_vcnt * PAGE_CACHE_SIZE;
  555. cb->len = uncompressed_len;
  556. comp_bio = compressed_bio_alloc(bdev, cur_disk_byte, GFP_NOFS);
  557. if (!comp_bio)
  558. goto fail2;
  559. comp_bio->bi_private = cb;
  560. comp_bio->bi_end_io = end_compressed_bio_read;
  561. atomic_inc(&cb->pending_bios);
  562. for (pg_index = 0; pg_index < nr_pages; pg_index++) {
  563. page = cb->compressed_pages[pg_index];
  564. page->mapping = inode->i_mapping;
  565. page->index = em_start >> PAGE_CACHE_SHIFT;
  566. if (comp_bio->bi_size)
  567. ret = tree->ops->merge_bio_hook(page, 0,
  568. PAGE_CACHE_SIZE,
  569. comp_bio, 0);
  570. else
  571. ret = 0;
  572. page->mapping = NULL;
  573. if (ret || bio_add_page(comp_bio, page, PAGE_CACHE_SIZE, 0) <
  574. PAGE_CACHE_SIZE) {
  575. bio_get(comp_bio);
  576. ret = btrfs_bio_wq_end_io(root->fs_info, comp_bio, 0);
  577. BUG_ON(ret); /* -ENOMEM */
  578. /*
  579. * inc the count before we submit the bio so
  580. * we know the end IO handler won't happen before
  581. * we inc the count. Otherwise, the cb might get
  582. * freed before we're done setting it up
  583. */
  584. atomic_inc(&cb->pending_bios);
  585. if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
  586. ret = btrfs_lookup_bio_sums(root, inode,
  587. comp_bio, sums);
  588. BUG_ON(ret); /* -ENOMEM */
  589. }
  590. sums += (comp_bio->bi_size + root->sectorsize - 1) /
  591. root->sectorsize;
  592. ret = btrfs_map_bio(root, READ, comp_bio,
  593. mirror_num, 0);
  594. BUG_ON(ret); /* -ENOMEM */
  595. bio_put(comp_bio);
  596. comp_bio = compressed_bio_alloc(bdev, cur_disk_byte,
  597. GFP_NOFS);
  598. BUG_ON(!comp_bio);
  599. comp_bio->bi_private = cb;
  600. comp_bio->bi_end_io = end_compressed_bio_read;
  601. bio_add_page(comp_bio, page, PAGE_CACHE_SIZE, 0);
  602. }
  603. cur_disk_byte += PAGE_CACHE_SIZE;
  604. }
  605. bio_get(comp_bio);
  606. ret = btrfs_bio_wq_end_io(root->fs_info, comp_bio, 0);
  607. BUG_ON(ret); /* -ENOMEM */
  608. if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
  609. ret = btrfs_lookup_bio_sums(root, inode, comp_bio, sums);
  610. BUG_ON(ret); /* -ENOMEM */
  611. }
  612. ret = btrfs_map_bio(root, READ, comp_bio, mirror_num, 0);
  613. BUG_ON(ret); /* -ENOMEM */
  614. bio_put(comp_bio);
  615. return 0;
  616. fail2:
  617. for (pg_index = 0; pg_index < nr_pages; pg_index++)
  618. free_page((unsigned long)cb->compressed_pages[pg_index]);
  619. kfree(cb->compressed_pages);
  620. fail1:
  621. kfree(cb);
  622. out:
  623. free_extent_map(em);
  624. return ret;
  625. }
  626. static struct list_head comp_idle_workspace[BTRFS_COMPRESS_TYPES];
  627. static spinlock_t comp_workspace_lock[BTRFS_COMPRESS_TYPES];
  628. static int comp_num_workspace[BTRFS_COMPRESS_TYPES];
  629. static atomic_t comp_alloc_workspace[BTRFS_COMPRESS_TYPES];
  630. static wait_queue_head_t comp_workspace_wait[BTRFS_COMPRESS_TYPES];
  631. struct btrfs_compress_op *btrfs_compress_op[] = {
  632. &btrfs_zlib_compress,
  633. &btrfs_lzo_compress,
  634. };
  635. void __init btrfs_init_compress(void)
  636. {
  637. int i;
  638. for (i = 0; i < BTRFS_COMPRESS_TYPES; i++) {
  639. INIT_LIST_HEAD(&comp_idle_workspace[i]);
  640. spin_lock_init(&comp_workspace_lock[i]);
  641. atomic_set(&comp_alloc_workspace[i], 0);
  642. init_waitqueue_head(&comp_workspace_wait[i]);
  643. }
  644. }
  645. /*
  646. * this finds an available workspace or allocates a new one
  647. * ERR_PTR is returned if things go bad.
  648. */
  649. static struct list_head *find_workspace(int type)
  650. {
  651. struct list_head *workspace;
  652. int cpus = num_online_cpus();
  653. int idx = type - 1;
  654. struct list_head *idle_workspace = &comp_idle_workspace[idx];
  655. spinlock_t *workspace_lock = &comp_workspace_lock[idx];
  656. atomic_t *alloc_workspace = &comp_alloc_workspace[idx];
  657. wait_queue_head_t *workspace_wait = &comp_workspace_wait[idx];
  658. int *num_workspace = &comp_num_workspace[idx];
  659. again:
  660. spin_lock(workspace_lock);
  661. if (!list_empty(idle_workspace)) {
  662. workspace = idle_workspace->next;
  663. list_del(workspace);
  664. (*num_workspace)--;
  665. spin_unlock(workspace_lock);
  666. return workspace;
  667. }
  668. if (atomic_read(alloc_workspace) > cpus) {
  669. DEFINE_WAIT(wait);
  670. spin_unlock(workspace_lock);
  671. prepare_to_wait(workspace_wait, &wait, TASK_UNINTERRUPTIBLE);
  672. if (atomic_read(alloc_workspace) > cpus && !*num_workspace)
  673. schedule();
  674. finish_wait(workspace_wait, &wait);
  675. goto again;
  676. }
  677. atomic_inc(alloc_workspace);
  678. spin_unlock(workspace_lock);
  679. workspace = btrfs_compress_op[idx]->alloc_workspace();
  680. if (IS_ERR(workspace)) {
  681. atomic_dec(alloc_workspace);
  682. wake_up(workspace_wait);
  683. }
  684. return workspace;
  685. }
  686. /*
  687. * put a workspace struct back on the list or free it if we have enough
  688. * idle ones sitting around
  689. */
  690. static void free_workspace(int type, struct list_head *workspace)
  691. {
  692. int idx = type - 1;
  693. struct list_head *idle_workspace = &comp_idle_workspace[idx];
  694. spinlock_t *workspace_lock = &comp_workspace_lock[idx];
  695. atomic_t *alloc_workspace = &comp_alloc_workspace[idx];
  696. wait_queue_head_t *workspace_wait = &comp_workspace_wait[idx];
  697. int *num_workspace = &comp_num_workspace[idx];
  698. spin_lock(workspace_lock);
  699. if (*num_workspace < num_online_cpus()) {
  700. list_add_tail(workspace, idle_workspace);
  701. (*num_workspace)++;
  702. spin_unlock(workspace_lock);
  703. goto wake;
  704. }
  705. spin_unlock(workspace_lock);
  706. btrfs_compress_op[idx]->free_workspace(workspace);
  707. atomic_dec(alloc_workspace);
  708. wake:
  709. smp_mb();
  710. if (waitqueue_active(workspace_wait))
  711. wake_up(workspace_wait);
  712. }
  713. /*
  714. * cleanup function for module exit
  715. */
  716. static void free_workspaces(void)
  717. {
  718. struct list_head *workspace;
  719. int i;
  720. for (i = 0; i < BTRFS_COMPRESS_TYPES; i++) {
  721. while (!list_empty(&comp_idle_workspace[i])) {
  722. workspace = comp_idle_workspace[i].next;
  723. list_del(workspace);
  724. btrfs_compress_op[i]->free_workspace(workspace);
  725. atomic_dec(&comp_alloc_workspace[i]);
  726. }
  727. }
  728. }
  729. /*
  730. * given an address space and start/len, compress the bytes.
  731. *
  732. * pages are allocated to hold the compressed result and stored
  733. * in 'pages'
  734. *
  735. * out_pages is used to return the number of pages allocated. There
  736. * may be pages allocated even if we return an error
  737. *
  738. * total_in is used to return the number of bytes actually read. It
  739. * may be smaller then len if we had to exit early because we
  740. * ran out of room in the pages array or because we cross the
  741. * max_out threshold.
  742. *
  743. * total_out is used to return the total number of compressed bytes
  744. *
  745. * max_out tells us the max number of bytes that we're allowed to
  746. * stuff into pages
  747. */
  748. int btrfs_compress_pages(int type, struct address_space *mapping,
  749. u64 start, unsigned long len,
  750. struct page **pages,
  751. unsigned long nr_dest_pages,
  752. unsigned long *out_pages,
  753. unsigned long *total_in,
  754. unsigned long *total_out,
  755. unsigned long max_out)
  756. {
  757. struct list_head *workspace;
  758. int ret;
  759. workspace = find_workspace(type);
  760. if (IS_ERR(workspace))
  761. return -1;
  762. ret = btrfs_compress_op[type-1]->compress_pages(workspace, mapping,
  763. start, len, pages,
  764. nr_dest_pages, out_pages,
  765. total_in, total_out,
  766. max_out);
  767. free_workspace(type, workspace);
  768. return ret;
  769. }
  770. /*
  771. * pages_in is an array of pages with compressed data.
  772. *
  773. * disk_start is the starting logical offset of this array in the file
  774. *
  775. * bvec is a bio_vec of pages from the file that we want to decompress into
  776. *
  777. * vcnt is the count of pages in the biovec
  778. *
  779. * srclen is the number of bytes in pages_in
  780. *
  781. * The basic idea is that we have a bio that was created by readpages.
  782. * The pages in the bio are for the uncompressed data, and they may not
  783. * be contiguous. They all correspond to the range of bytes covered by
  784. * the compressed extent.
  785. */
  786. int btrfs_decompress_biovec(int type, struct page **pages_in, u64 disk_start,
  787. struct bio_vec *bvec, int vcnt, size_t srclen)
  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_biovec(workspace, pages_in,
  795. disk_start,
  796. bvec, vcnt, srclen);
  797. free_workspace(type, workspace);
  798. return ret;
  799. }
  800. /*
  801. * a less complex decompression routine. Our compressed data fits in a
  802. * single page, and we want to read a single page out of it.
  803. * start_byte tells us the offset into the compressed data we're interested in
  804. */
  805. int btrfs_decompress(int type, unsigned char *data_in, struct page *dest_page,
  806. unsigned long start_byte, size_t srclen, size_t destlen)
  807. {
  808. struct list_head *workspace;
  809. int ret;
  810. workspace = find_workspace(type);
  811. if (IS_ERR(workspace))
  812. return -ENOMEM;
  813. ret = btrfs_compress_op[type-1]->decompress(workspace, data_in,
  814. dest_page, start_byte,
  815. srclen, destlen);
  816. free_workspace(type, workspace);
  817. return ret;
  818. }
  819. void btrfs_exit_compress(void)
  820. {
  821. free_workspaces();
  822. }
  823. /*
  824. * Copy uncompressed data from working buffer to pages.
  825. *
  826. * buf_start is the byte offset we're of the start of our workspace buffer.
  827. *
  828. * total_out is the last byte of the buffer
  829. */
  830. int btrfs_decompress_buf2page(char *buf, unsigned long buf_start,
  831. unsigned long total_out, u64 disk_start,
  832. struct bio_vec *bvec, int vcnt,
  833. unsigned long *pg_index,
  834. unsigned long *pg_offset)
  835. {
  836. unsigned long buf_offset;
  837. unsigned long current_buf_start;
  838. unsigned long start_byte;
  839. unsigned long working_bytes = total_out - buf_start;
  840. unsigned long bytes;
  841. char *kaddr;
  842. struct page *page_out = bvec[*pg_index].bv_page;
  843. /*
  844. * start byte is the first byte of the page we're currently
  845. * copying into relative to the start of the compressed data.
  846. */
  847. start_byte = page_offset(page_out) - disk_start;
  848. /* we haven't yet hit data corresponding to this page */
  849. if (total_out <= start_byte)
  850. return 1;
  851. /*
  852. * the start of the data we care about is offset into
  853. * the middle of our working buffer
  854. */
  855. if (total_out > start_byte && buf_start < start_byte) {
  856. buf_offset = start_byte - buf_start;
  857. working_bytes -= buf_offset;
  858. } else {
  859. buf_offset = 0;
  860. }
  861. current_buf_start = buf_start;
  862. /* copy bytes from the working buffer into the pages */
  863. while (working_bytes > 0) {
  864. bytes = min(PAGE_CACHE_SIZE - *pg_offset,
  865. PAGE_CACHE_SIZE - buf_offset);
  866. bytes = min(bytes, working_bytes);
  867. kaddr = kmap_atomic(page_out);
  868. memcpy(kaddr + *pg_offset, buf + buf_offset, bytes);
  869. kunmap_atomic(kaddr);
  870. flush_dcache_page(page_out);
  871. *pg_offset += bytes;
  872. buf_offset += bytes;
  873. working_bytes -= bytes;
  874. current_buf_start += bytes;
  875. /* check if we need to pick another page */
  876. if (*pg_offset == PAGE_CACHE_SIZE) {
  877. (*pg_index)++;
  878. if (*pg_index >= vcnt)
  879. return 0;
  880. page_out = bvec[*pg_index].bv_page;
  881. *pg_offset = 0;
  882. start_byte = page_offset(page_out) - disk_start;
  883. /*
  884. * make sure our new page is covered by this
  885. * working buffer
  886. */
  887. if (total_out <= start_byte)
  888. return 1;
  889. /*
  890. * the next page in the biovec might not be adjacent
  891. * to the last page, but it might still be found
  892. * inside this working buffer. bump our offset pointer
  893. */
  894. if (total_out > start_byte &&
  895. current_buf_start < start_byte) {
  896. buf_offset = start_byte - buf_start;
  897. working_bytes = total_out - start_byte;
  898. current_buf_start = buf_start + buf_offset;
  899. }
  900. }
  901. }
  902. return 1;
  903. }