compression.c 28 KB

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