compression.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  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_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. put_page(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_SHIFT;
  195. unsigned long end_index = (cb->start + cb->len - 1) >> PAGE_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. put_page(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. put_page(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_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_set_op_attrs(bio, REQ_OP_WRITE, 0);
  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_iter.bi_size)
  330. ret = io_tree->ops->merge_bio_hook(page, 0,
  331. PAGE_SIZE,
  332. bio, 0);
  333. else
  334. ret = 0;
  335. page->mapping = NULL;
  336. if (ret || bio_add_page(bio, page, PAGE_SIZE, 0) <
  337. PAGE_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,
  347. BTRFS_WQ_ENDIO_DATA);
  348. BUG_ON(ret); /* -ENOMEM */
  349. if (!skip_sum) {
  350. ret = btrfs_csum_one_bio(root, inode, bio,
  351. start, 1);
  352. BUG_ON(ret); /* -ENOMEM */
  353. }
  354. ret = btrfs_map_bio(root, bio, 0, 1);
  355. if (ret) {
  356. bio->bi_error = ret;
  357. bio_endio(bio);
  358. }
  359. bio_put(bio);
  360. bio = compressed_bio_alloc(bdev, first_byte, GFP_NOFS);
  361. BUG_ON(!bio);
  362. bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
  363. bio->bi_private = cb;
  364. bio->bi_end_io = end_compressed_bio_write;
  365. bio_add_page(bio, page, PAGE_SIZE, 0);
  366. }
  367. if (bytes_left < PAGE_SIZE) {
  368. btrfs_info(BTRFS_I(inode)->root->fs_info,
  369. "bytes left %lu compress len %lu nr %lu",
  370. bytes_left, cb->compressed_len, cb->nr_pages);
  371. }
  372. bytes_left -= PAGE_SIZE;
  373. first_byte += PAGE_SIZE;
  374. cond_resched();
  375. }
  376. bio_get(bio);
  377. ret = btrfs_bio_wq_end_io(root->fs_info, bio, BTRFS_WQ_ENDIO_DATA);
  378. BUG_ON(ret); /* -ENOMEM */
  379. if (!skip_sum) {
  380. ret = btrfs_csum_one_bio(root, inode, bio, start, 1);
  381. BUG_ON(ret); /* -ENOMEM */
  382. }
  383. ret = btrfs_map_bio(root, bio, 0, 1);
  384. if (ret) {
  385. bio->bi_error = ret;
  386. bio_endio(bio);
  387. }
  388. bio_put(bio);
  389. return 0;
  390. }
  391. static noinline int add_ra_bio_pages(struct inode *inode,
  392. u64 compressed_end,
  393. struct compressed_bio *cb)
  394. {
  395. unsigned long end_index;
  396. unsigned long pg_index;
  397. u64 last_offset;
  398. u64 isize = i_size_read(inode);
  399. int ret;
  400. struct page *page;
  401. unsigned long nr_pages = 0;
  402. struct extent_map *em;
  403. struct address_space *mapping = inode->i_mapping;
  404. struct extent_map_tree *em_tree;
  405. struct extent_io_tree *tree;
  406. u64 end;
  407. int misses = 0;
  408. page = cb->orig_bio->bi_io_vec[cb->orig_bio->bi_vcnt - 1].bv_page;
  409. last_offset = (page_offset(page) + PAGE_SIZE);
  410. em_tree = &BTRFS_I(inode)->extent_tree;
  411. tree = &BTRFS_I(inode)->io_tree;
  412. if (isize == 0)
  413. return 0;
  414. end_index = (i_size_read(inode) - 1) >> PAGE_SHIFT;
  415. while (last_offset < compressed_end) {
  416. pg_index = last_offset >> PAGE_SHIFT;
  417. if (pg_index > end_index)
  418. break;
  419. rcu_read_lock();
  420. page = radix_tree_lookup(&mapping->page_tree, pg_index);
  421. rcu_read_unlock();
  422. if (page && !radix_tree_exceptional_entry(page)) {
  423. misses++;
  424. if (misses > 4)
  425. break;
  426. goto next;
  427. }
  428. page = __page_cache_alloc(mapping_gfp_constraint(mapping,
  429. ~__GFP_FS));
  430. if (!page)
  431. break;
  432. if (add_to_page_cache_lru(page, mapping, pg_index, GFP_NOFS)) {
  433. put_page(page);
  434. goto next;
  435. }
  436. end = last_offset + PAGE_SIZE - 1;
  437. /*
  438. * at this point, we have a locked page in the page cache
  439. * for these bytes in the file. But, we have to make
  440. * sure they map to this compressed extent on disk.
  441. */
  442. set_page_extent_mapped(page);
  443. lock_extent(tree, last_offset, end);
  444. read_lock(&em_tree->lock);
  445. em = lookup_extent_mapping(em_tree, last_offset,
  446. PAGE_SIZE);
  447. read_unlock(&em_tree->lock);
  448. if (!em || last_offset < em->start ||
  449. (last_offset + PAGE_SIZE > extent_map_end(em)) ||
  450. (em->block_start >> 9) != cb->orig_bio->bi_iter.bi_sector) {
  451. free_extent_map(em);
  452. unlock_extent(tree, last_offset, end);
  453. unlock_page(page);
  454. put_page(page);
  455. break;
  456. }
  457. free_extent_map(em);
  458. if (page->index == end_index) {
  459. char *userpage;
  460. size_t zero_offset = isize & (PAGE_SIZE - 1);
  461. if (zero_offset) {
  462. int zeros;
  463. zeros = PAGE_SIZE - zero_offset;
  464. userpage = kmap_atomic(page);
  465. memset(userpage + zero_offset, 0, zeros);
  466. flush_dcache_page(page);
  467. kunmap_atomic(userpage);
  468. }
  469. }
  470. ret = bio_add_page(cb->orig_bio, page,
  471. PAGE_SIZE, 0);
  472. if (ret == PAGE_SIZE) {
  473. nr_pages++;
  474. put_page(page);
  475. } else {
  476. unlock_extent(tree, last_offset, end);
  477. unlock_page(page);
  478. put_page(page);
  479. break;
  480. }
  481. next:
  482. last_offset += PAGE_SIZE;
  483. }
  484. return 0;
  485. }
  486. /*
  487. * for a compressed read, the bio we get passed has all the inode pages
  488. * in it. We don't actually do IO on those pages but allocate new ones
  489. * to hold the compressed pages on disk.
  490. *
  491. * bio->bi_iter.bi_sector points to the compressed extent on disk
  492. * bio->bi_io_vec points to all of the inode pages
  493. * bio->bi_vcnt is a count of pages
  494. *
  495. * After the compressed pages are read, we copy the bytes into the
  496. * bio we were passed and then call the bio end_io calls
  497. */
  498. int btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
  499. int mirror_num, unsigned long bio_flags)
  500. {
  501. struct extent_io_tree *tree;
  502. struct extent_map_tree *em_tree;
  503. struct compressed_bio *cb;
  504. struct btrfs_root *root = BTRFS_I(inode)->root;
  505. unsigned long uncompressed_len = bio->bi_vcnt * PAGE_SIZE;
  506. unsigned long compressed_len;
  507. unsigned long nr_pages;
  508. unsigned long pg_index;
  509. struct page *page;
  510. struct block_device *bdev;
  511. struct bio *comp_bio;
  512. u64 cur_disk_byte = (u64)bio->bi_iter.bi_sector << 9;
  513. u64 em_len;
  514. u64 em_start;
  515. struct extent_map *em;
  516. int ret = -ENOMEM;
  517. int faili = 0;
  518. u32 *sums;
  519. tree = &BTRFS_I(inode)->io_tree;
  520. em_tree = &BTRFS_I(inode)->extent_tree;
  521. /* we need the actual starting offset of this extent in the file */
  522. read_lock(&em_tree->lock);
  523. em = lookup_extent_mapping(em_tree,
  524. page_offset(bio->bi_io_vec->bv_page),
  525. PAGE_SIZE);
  526. read_unlock(&em_tree->lock);
  527. if (!em)
  528. return -EIO;
  529. compressed_len = em->block_len;
  530. cb = kmalloc(compressed_bio_size(root, compressed_len), GFP_NOFS);
  531. if (!cb)
  532. goto out;
  533. atomic_set(&cb->pending_bios, 0);
  534. cb->errors = 0;
  535. cb->inode = inode;
  536. cb->mirror_num = mirror_num;
  537. sums = &cb->sums;
  538. cb->start = em->orig_start;
  539. em_len = em->len;
  540. em_start = em->start;
  541. free_extent_map(em);
  542. em = NULL;
  543. cb->len = uncompressed_len;
  544. cb->compressed_len = compressed_len;
  545. cb->compress_type = extent_compress_type(bio_flags);
  546. cb->orig_bio = bio;
  547. nr_pages = DIV_ROUND_UP(compressed_len, PAGE_SIZE);
  548. cb->compressed_pages = kcalloc(nr_pages, sizeof(struct page *),
  549. GFP_NOFS);
  550. if (!cb->compressed_pages)
  551. goto fail1;
  552. bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
  553. for (pg_index = 0; pg_index < nr_pages; pg_index++) {
  554. cb->compressed_pages[pg_index] = alloc_page(GFP_NOFS |
  555. __GFP_HIGHMEM);
  556. if (!cb->compressed_pages[pg_index]) {
  557. faili = pg_index - 1;
  558. ret = -ENOMEM;
  559. goto fail2;
  560. }
  561. }
  562. faili = nr_pages - 1;
  563. cb->nr_pages = nr_pages;
  564. add_ra_bio_pages(inode, em_start + em_len, cb);
  565. /* include any pages we added in add_ra-bio_pages */
  566. uncompressed_len = bio->bi_vcnt * PAGE_SIZE;
  567. cb->len = uncompressed_len;
  568. comp_bio = compressed_bio_alloc(bdev, cur_disk_byte, GFP_NOFS);
  569. if (!comp_bio)
  570. goto fail2;
  571. bio_set_op_attrs (comp_bio, REQ_OP_READ, 0);
  572. comp_bio->bi_private = cb;
  573. comp_bio->bi_end_io = end_compressed_bio_read;
  574. atomic_inc(&cb->pending_bios);
  575. for (pg_index = 0; pg_index < nr_pages; pg_index++) {
  576. page = cb->compressed_pages[pg_index];
  577. page->mapping = inode->i_mapping;
  578. page->index = em_start >> PAGE_SHIFT;
  579. if (comp_bio->bi_iter.bi_size)
  580. ret = tree->ops->merge_bio_hook(page, 0,
  581. PAGE_SIZE,
  582. comp_bio, 0);
  583. else
  584. ret = 0;
  585. page->mapping = NULL;
  586. if (ret || bio_add_page(comp_bio, page, PAGE_SIZE, 0) <
  587. PAGE_SIZE) {
  588. bio_get(comp_bio);
  589. ret = btrfs_bio_wq_end_io(root->fs_info, comp_bio,
  590. BTRFS_WQ_ENDIO_DATA);
  591. BUG_ON(ret); /* -ENOMEM */
  592. /*
  593. * inc the count before we submit the bio so
  594. * we know the end IO handler won't happen before
  595. * we inc the count. Otherwise, the cb might get
  596. * freed before we're done setting it up
  597. */
  598. atomic_inc(&cb->pending_bios);
  599. if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
  600. ret = btrfs_lookup_bio_sums(root, inode,
  601. comp_bio, sums);
  602. BUG_ON(ret); /* -ENOMEM */
  603. }
  604. sums += DIV_ROUND_UP(comp_bio->bi_iter.bi_size,
  605. root->sectorsize);
  606. ret = btrfs_map_bio(root, comp_bio, mirror_num, 0);
  607. if (ret) {
  608. comp_bio->bi_error = ret;
  609. bio_endio(comp_bio);
  610. }
  611. bio_put(comp_bio);
  612. comp_bio = compressed_bio_alloc(bdev, cur_disk_byte,
  613. GFP_NOFS);
  614. BUG_ON(!comp_bio);
  615. bio_set_op_attrs(comp_bio, REQ_OP_READ, 0);
  616. comp_bio->bi_private = cb;
  617. comp_bio->bi_end_io = end_compressed_bio_read;
  618. bio_add_page(comp_bio, page, PAGE_SIZE, 0);
  619. }
  620. cur_disk_byte += PAGE_SIZE;
  621. }
  622. bio_get(comp_bio);
  623. ret = btrfs_bio_wq_end_io(root->fs_info, comp_bio,
  624. BTRFS_WQ_ENDIO_DATA);
  625. BUG_ON(ret); /* -ENOMEM */
  626. if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
  627. ret = btrfs_lookup_bio_sums(root, inode, comp_bio, sums);
  628. BUG_ON(ret); /* -ENOMEM */
  629. }
  630. ret = btrfs_map_bio(root, comp_bio, mirror_num, 0);
  631. if (ret) {
  632. comp_bio->bi_error = ret;
  633. bio_endio(comp_bio);
  634. }
  635. bio_put(comp_bio);
  636. return 0;
  637. fail2:
  638. while (faili >= 0) {
  639. __free_page(cb->compressed_pages[faili]);
  640. faili--;
  641. }
  642. kfree(cb->compressed_pages);
  643. fail1:
  644. kfree(cb);
  645. out:
  646. free_extent_map(em);
  647. return ret;
  648. }
  649. static struct {
  650. struct list_head idle_ws;
  651. spinlock_t ws_lock;
  652. /* Number of free workspaces */
  653. int free_ws;
  654. /* Total number of allocated workspaces */
  655. atomic_t total_ws;
  656. /* Waiters for a free workspace */
  657. wait_queue_head_t ws_wait;
  658. } btrfs_comp_ws[BTRFS_COMPRESS_TYPES];
  659. static const struct btrfs_compress_op * const btrfs_compress_op[] = {
  660. &btrfs_zlib_compress,
  661. &btrfs_lzo_compress,
  662. };
  663. void __init btrfs_init_compress(void)
  664. {
  665. int i;
  666. for (i = 0; i < BTRFS_COMPRESS_TYPES; i++) {
  667. struct list_head *workspace;
  668. INIT_LIST_HEAD(&btrfs_comp_ws[i].idle_ws);
  669. spin_lock_init(&btrfs_comp_ws[i].ws_lock);
  670. atomic_set(&btrfs_comp_ws[i].total_ws, 0);
  671. init_waitqueue_head(&btrfs_comp_ws[i].ws_wait);
  672. /*
  673. * Preallocate one workspace for each compression type so
  674. * we can guarantee forward progress in the worst case
  675. */
  676. workspace = btrfs_compress_op[i]->alloc_workspace();
  677. if (IS_ERR(workspace)) {
  678. pr_warn("BTRFS: cannot preallocate compression workspace, will try later\n");
  679. } else {
  680. atomic_set(&btrfs_comp_ws[i].total_ws, 1);
  681. btrfs_comp_ws[i].free_ws = 1;
  682. list_add(workspace, &btrfs_comp_ws[i].idle_ws);
  683. }
  684. }
  685. }
  686. /*
  687. * This finds an available workspace or allocates a new one.
  688. * If it's not possible to allocate a new one, waits until there's one.
  689. * Preallocation makes a forward progress guarantees and we do not return
  690. * errors.
  691. */
  692. static struct list_head *find_workspace(int type)
  693. {
  694. struct list_head *workspace;
  695. int cpus = num_online_cpus();
  696. int idx = type - 1;
  697. struct list_head *idle_ws = &btrfs_comp_ws[idx].idle_ws;
  698. spinlock_t *ws_lock = &btrfs_comp_ws[idx].ws_lock;
  699. atomic_t *total_ws = &btrfs_comp_ws[idx].total_ws;
  700. wait_queue_head_t *ws_wait = &btrfs_comp_ws[idx].ws_wait;
  701. int *free_ws = &btrfs_comp_ws[idx].free_ws;
  702. again:
  703. spin_lock(ws_lock);
  704. if (!list_empty(idle_ws)) {
  705. workspace = idle_ws->next;
  706. list_del(workspace);
  707. (*free_ws)--;
  708. spin_unlock(ws_lock);
  709. return workspace;
  710. }
  711. if (atomic_read(total_ws) > cpus) {
  712. DEFINE_WAIT(wait);
  713. spin_unlock(ws_lock);
  714. prepare_to_wait(ws_wait, &wait, TASK_UNINTERRUPTIBLE);
  715. if (atomic_read(total_ws) > cpus && !*free_ws)
  716. schedule();
  717. finish_wait(ws_wait, &wait);
  718. goto again;
  719. }
  720. atomic_inc(total_ws);
  721. spin_unlock(ws_lock);
  722. workspace = btrfs_compress_op[idx]->alloc_workspace();
  723. if (IS_ERR(workspace)) {
  724. atomic_dec(total_ws);
  725. wake_up(ws_wait);
  726. /*
  727. * Do not return the error but go back to waiting. There's a
  728. * workspace preallocated for each type and the compression
  729. * time is bounded so we get to a workspace eventually. This
  730. * makes our caller's life easier.
  731. *
  732. * To prevent silent and low-probability deadlocks (when the
  733. * initial preallocation fails), check if there are any
  734. * workspaces at all.
  735. */
  736. if (atomic_read(total_ws) == 0) {
  737. static DEFINE_RATELIMIT_STATE(_rs,
  738. /* once per minute */ 60 * HZ,
  739. /* no burst */ 1);
  740. if (__ratelimit(&_rs)) {
  741. pr_warn("BTRFS: no compression workspaces, low memory, retrying\n");
  742. }
  743. }
  744. goto again;
  745. }
  746. return workspace;
  747. }
  748. /*
  749. * put a workspace struct back on the list or free it if we have enough
  750. * idle ones sitting around
  751. */
  752. static void free_workspace(int type, struct list_head *workspace)
  753. {
  754. int idx = type - 1;
  755. struct list_head *idle_ws = &btrfs_comp_ws[idx].idle_ws;
  756. spinlock_t *ws_lock = &btrfs_comp_ws[idx].ws_lock;
  757. atomic_t *total_ws = &btrfs_comp_ws[idx].total_ws;
  758. wait_queue_head_t *ws_wait = &btrfs_comp_ws[idx].ws_wait;
  759. int *free_ws = &btrfs_comp_ws[idx].free_ws;
  760. spin_lock(ws_lock);
  761. if (*free_ws < num_online_cpus()) {
  762. list_add(workspace, idle_ws);
  763. (*free_ws)++;
  764. spin_unlock(ws_lock);
  765. goto wake;
  766. }
  767. spin_unlock(ws_lock);
  768. btrfs_compress_op[idx]->free_workspace(workspace);
  769. atomic_dec(total_ws);
  770. wake:
  771. /*
  772. * Make sure counter is updated before we wake up waiters.
  773. */
  774. smp_mb();
  775. if (waitqueue_active(ws_wait))
  776. wake_up(ws_wait);
  777. }
  778. /*
  779. * cleanup function for module exit
  780. */
  781. static void free_workspaces(void)
  782. {
  783. struct list_head *workspace;
  784. int i;
  785. for (i = 0; i < BTRFS_COMPRESS_TYPES; i++) {
  786. while (!list_empty(&btrfs_comp_ws[i].idle_ws)) {
  787. workspace = btrfs_comp_ws[i].idle_ws.next;
  788. list_del(workspace);
  789. btrfs_compress_op[i]->free_workspace(workspace);
  790. atomic_dec(&btrfs_comp_ws[i].total_ws);
  791. }
  792. }
  793. }
  794. /*
  795. * given an address space and start/len, compress the bytes.
  796. *
  797. * pages are allocated to hold the compressed result and stored
  798. * in 'pages'
  799. *
  800. * out_pages is used to return the number of pages allocated. There
  801. * may be pages allocated even if we return an error
  802. *
  803. * total_in is used to return the number of bytes actually read. It
  804. * may be smaller then len if we had to exit early because we
  805. * ran out of room in the pages array or because we cross the
  806. * max_out threshold.
  807. *
  808. * total_out is used to return the total number of compressed bytes
  809. *
  810. * max_out tells us the max number of bytes that we're allowed to
  811. * stuff into pages
  812. */
  813. int btrfs_compress_pages(int type, struct address_space *mapping,
  814. u64 start, unsigned long len,
  815. struct page **pages,
  816. unsigned long nr_dest_pages,
  817. unsigned long *out_pages,
  818. unsigned long *total_in,
  819. unsigned long *total_out,
  820. unsigned long max_out)
  821. {
  822. struct list_head *workspace;
  823. int ret;
  824. workspace = find_workspace(type);
  825. ret = btrfs_compress_op[type-1]->compress_pages(workspace, mapping,
  826. start, len, pages,
  827. nr_dest_pages, out_pages,
  828. total_in, total_out,
  829. max_out);
  830. free_workspace(type, workspace);
  831. return ret;
  832. }
  833. /*
  834. * pages_in is an array of pages with compressed data.
  835. *
  836. * disk_start is the starting logical offset of this array in the file
  837. *
  838. * bvec is a bio_vec of pages from the file that we want to decompress into
  839. *
  840. * vcnt is the count of pages in the biovec
  841. *
  842. * srclen is the number of bytes in pages_in
  843. *
  844. * The basic idea is that we have a bio that was created by readpages.
  845. * The pages in the bio are for the uncompressed data, and they may not
  846. * be contiguous. They all correspond to the range of bytes covered by
  847. * the compressed extent.
  848. */
  849. static int btrfs_decompress_biovec(int type, struct page **pages_in,
  850. u64 disk_start, struct bio_vec *bvec,
  851. int vcnt, size_t srclen)
  852. {
  853. struct list_head *workspace;
  854. int ret;
  855. workspace = find_workspace(type);
  856. ret = btrfs_compress_op[type-1]->decompress_biovec(workspace, pages_in,
  857. disk_start,
  858. bvec, vcnt, srclen);
  859. free_workspace(type, workspace);
  860. return ret;
  861. }
  862. /*
  863. * a less complex decompression routine. Our compressed data fits in a
  864. * single page, and we want to read a single page out of it.
  865. * start_byte tells us the offset into the compressed data we're interested in
  866. */
  867. int btrfs_decompress(int type, unsigned char *data_in, struct page *dest_page,
  868. unsigned long start_byte, size_t srclen, size_t destlen)
  869. {
  870. struct list_head *workspace;
  871. int ret;
  872. workspace = find_workspace(type);
  873. ret = btrfs_compress_op[type-1]->decompress(workspace, data_in,
  874. dest_page, start_byte,
  875. srclen, destlen);
  876. free_workspace(type, workspace);
  877. return ret;
  878. }
  879. void btrfs_exit_compress(void)
  880. {
  881. free_workspaces();
  882. }
  883. /*
  884. * Copy uncompressed data from working buffer to pages.
  885. *
  886. * buf_start is the byte offset we're of the start of our workspace buffer.
  887. *
  888. * total_out is the last byte of the buffer
  889. */
  890. int btrfs_decompress_buf2page(char *buf, unsigned long buf_start,
  891. unsigned long total_out, u64 disk_start,
  892. struct bio_vec *bvec, int vcnt,
  893. unsigned long *pg_index,
  894. unsigned long *pg_offset)
  895. {
  896. unsigned long buf_offset;
  897. unsigned long current_buf_start;
  898. unsigned long start_byte;
  899. unsigned long working_bytes = total_out - buf_start;
  900. unsigned long bytes;
  901. char *kaddr;
  902. struct page *page_out = bvec[*pg_index].bv_page;
  903. /*
  904. * start byte is the first byte of the page we're currently
  905. * copying into relative to the start of the compressed data.
  906. */
  907. start_byte = page_offset(page_out) - disk_start;
  908. /* we haven't yet hit data corresponding to this page */
  909. if (total_out <= start_byte)
  910. return 1;
  911. /*
  912. * the start of the data we care about is offset into
  913. * the middle of our working buffer
  914. */
  915. if (total_out > start_byte && buf_start < start_byte) {
  916. buf_offset = start_byte - buf_start;
  917. working_bytes -= buf_offset;
  918. } else {
  919. buf_offset = 0;
  920. }
  921. current_buf_start = buf_start;
  922. /* copy bytes from the working buffer into the pages */
  923. while (working_bytes > 0) {
  924. bytes = min(PAGE_SIZE - *pg_offset,
  925. PAGE_SIZE - buf_offset);
  926. bytes = min(bytes, working_bytes);
  927. kaddr = kmap_atomic(page_out);
  928. memcpy(kaddr + *pg_offset, buf + buf_offset, bytes);
  929. kunmap_atomic(kaddr);
  930. flush_dcache_page(page_out);
  931. *pg_offset += bytes;
  932. buf_offset += bytes;
  933. working_bytes -= bytes;
  934. current_buf_start += bytes;
  935. /* check if we need to pick another page */
  936. if (*pg_offset == PAGE_SIZE) {
  937. (*pg_index)++;
  938. if (*pg_index >= vcnt)
  939. return 0;
  940. page_out = bvec[*pg_index].bv_page;
  941. *pg_offset = 0;
  942. start_byte = page_offset(page_out) - disk_start;
  943. /*
  944. * make sure our new page is covered by this
  945. * working buffer
  946. */
  947. if (total_out <= start_byte)
  948. return 1;
  949. /*
  950. * the next page in the biovec might not be adjacent
  951. * to the last page, but it might still be found
  952. * inside this working buffer. bump our offset pointer
  953. */
  954. if (total_out > start_byte &&
  955. current_buf_start < start_byte) {
  956. buf_offset = start_byte - buf_start;
  957. working_bytes = total_out - start_byte;
  958. current_buf_start = buf_start + buf_offset;
  959. }
  960. }
  961. }
  962. return 1;
  963. }
  964. /*
  965. * When uncompressing data, we need to make sure and zero any parts of
  966. * the biovec that were not filled in by the decompression code. pg_index
  967. * and pg_offset indicate the last page and the last offset of that page
  968. * that have been filled in. This will zero everything remaining in the
  969. * biovec.
  970. */
  971. void btrfs_clear_biovec_end(struct bio_vec *bvec, int vcnt,
  972. unsigned long pg_index,
  973. unsigned long pg_offset)
  974. {
  975. while (pg_index < vcnt) {
  976. struct page *page = bvec[pg_index].bv_page;
  977. unsigned long off = bvec[pg_index].bv_offset;
  978. unsigned long len = bvec[pg_index].bv_len;
  979. if (pg_offset < off)
  980. pg_offset = off;
  981. if (pg_offset < off + len) {
  982. unsigned long bytes = off + len - pg_offset;
  983. char *kaddr;
  984. kaddr = kmap_atomic(page);
  985. memset(kaddr + pg_offset, 0, bytes);
  986. kunmap_atomic(kaddr);
  987. }
  988. pg_index++;
  989. pg_offset = 0;
  990. }
  991. }