compression.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  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_bio(int type, struct page **pages_in,
  72. u64 disk_start, struct bio *orig_bio,
  73. size_t srclen);
  74. static inline int compressed_bio_size(struct btrfs_root *root,
  75. unsigned long disk_size)
  76. {
  77. struct btrfs_fs_info *fs_info = root->fs_info;
  78. u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
  79. return sizeof(struct compressed_bio) +
  80. (DIV_ROUND_UP(disk_size, fs_info->sectorsize)) * csum_size;
  81. }
  82. static struct bio *compressed_bio_alloc(struct block_device *bdev,
  83. u64 first_byte, gfp_t gfp_flags)
  84. {
  85. return btrfs_bio_alloc(bdev, first_byte >> 9, BIO_MAX_PAGES, 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 page *page;
  93. unsigned long i;
  94. char *kaddr;
  95. u32 csum;
  96. u32 *cb_sum = &cb->sums;
  97. if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
  98. return 0;
  99. for (i = 0; i < cb->nr_pages; i++) {
  100. page = cb->compressed_pages[i];
  101. csum = ~(u32)0;
  102. kaddr = kmap_atomic(page);
  103. csum = btrfs_csum_data(kaddr, csum, PAGE_SIZE);
  104. btrfs_csum_final(csum, (u8 *)&csum);
  105. kunmap_atomic(kaddr);
  106. if (csum != *cb_sum) {
  107. btrfs_info(BTRFS_I(inode)->root->fs_info,
  108. "csum failed ino %llu extent %llu csum %u wanted %u mirror %d",
  109. btrfs_ino(inode), disk_start, csum, *cb_sum,
  110. cb->mirror_num);
  111. ret = -EIO;
  112. goto fail;
  113. }
  114. cb_sum++;
  115. }
  116. ret = 0;
  117. fail:
  118. return ret;
  119. }
  120. /* when we finish reading compressed pages from the disk, we
  121. * decompress them and then run the bio end_io routines on the
  122. * decompressed pages (in the inode address space).
  123. *
  124. * This allows the checksumming and other IO error handling routines
  125. * to work normally
  126. *
  127. * The compressed pages are freed here, and it must be run
  128. * in process context
  129. */
  130. static void end_compressed_bio_read(struct bio *bio)
  131. {
  132. struct compressed_bio *cb = bio->bi_private;
  133. struct inode *inode;
  134. struct page *page;
  135. unsigned long index;
  136. int ret;
  137. if (bio->bi_error)
  138. cb->errors = 1;
  139. /* if there are more bios still pending for this compressed
  140. * extent, just exit
  141. */
  142. if (!atomic_dec_and_test(&cb->pending_bios))
  143. goto out;
  144. inode = cb->inode;
  145. ret = check_compressed_csum(inode, cb,
  146. (u64)bio->bi_iter.bi_sector << 9);
  147. if (ret)
  148. goto csum_failed;
  149. /* ok, we're the last bio for this extent, lets start
  150. * the decompression.
  151. */
  152. ret = btrfs_decompress_bio(cb->compress_type,
  153. cb->compressed_pages,
  154. cb->start,
  155. cb->orig_bio,
  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 btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  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_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 = 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_set_op_attrs(bio, REQ_OP_WRITE, 0);
  322. bio->bi_private = cb;
  323. bio->bi_end_io = end_compressed_bio_write;
  324. atomic_inc(&cb->pending_bios);
  325. /* create and submit bios for the compressed pages */
  326. bytes_left = compressed_len;
  327. for (pg_index = 0; pg_index < cb->nr_pages; pg_index++) {
  328. page = compressed_pages[pg_index];
  329. page->mapping = inode->i_mapping;
  330. if (bio->bi_iter.bi_size)
  331. ret = io_tree->ops->merge_bio_hook(page, 0,
  332. PAGE_SIZE,
  333. bio, 0);
  334. else
  335. ret = 0;
  336. page->mapping = NULL;
  337. if (ret || bio_add_page(bio, page, PAGE_SIZE, 0) <
  338. PAGE_SIZE) {
  339. bio_get(bio);
  340. /*
  341. * inc the count before we submit the bio so
  342. * we know the end IO handler won't happen before
  343. * we inc the count. Otherwise, the cb might get
  344. * freed before we're done setting it up
  345. */
  346. atomic_inc(&cb->pending_bios);
  347. ret = btrfs_bio_wq_end_io(fs_info, bio,
  348. BTRFS_WQ_ENDIO_DATA);
  349. BUG_ON(ret); /* -ENOMEM */
  350. if (!skip_sum) {
  351. ret = btrfs_csum_one_bio(root, inode, bio,
  352. start, 1);
  353. BUG_ON(ret); /* -ENOMEM */
  354. }
  355. ret = btrfs_map_bio(root, bio, 0, 1);
  356. if (ret) {
  357. bio->bi_error = ret;
  358. bio_endio(bio);
  359. }
  360. bio_put(bio);
  361. bio = compressed_bio_alloc(bdev, first_byte, GFP_NOFS);
  362. BUG_ON(!bio);
  363. bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
  364. bio->bi_private = cb;
  365. bio->bi_end_io = end_compressed_bio_write;
  366. bio_add_page(bio, page, PAGE_SIZE, 0);
  367. }
  368. if (bytes_left < PAGE_SIZE) {
  369. btrfs_info(fs_info,
  370. "bytes left %lu compress len %lu nr %lu",
  371. bytes_left, cb->compressed_len, cb->nr_pages);
  372. }
  373. bytes_left -= PAGE_SIZE;
  374. first_byte += PAGE_SIZE;
  375. cond_resched();
  376. }
  377. bio_get(bio);
  378. ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
  379. BUG_ON(ret); /* -ENOMEM */
  380. if (!skip_sum) {
  381. ret = btrfs_csum_one_bio(root, inode, bio, start, 1);
  382. BUG_ON(ret); /* -ENOMEM */
  383. }
  384. ret = btrfs_map_bio(root, bio, 0, 1);
  385. if (ret) {
  386. bio->bi_error = ret;
  387. bio_endio(bio);
  388. }
  389. bio_put(bio);
  390. return 0;
  391. }
  392. static u64 bio_end_offset(struct bio *bio)
  393. {
  394. struct bio_vec *last = &bio->bi_io_vec[bio->bi_vcnt - 1];
  395. return page_offset(last->bv_page) + last->bv_len + last->bv_offset;
  396. }
  397. static noinline int add_ra_bio_pages(struct inode *inode,
  398. u64 compressed_end,
  399. struct compressed_bio *cb)
  400. {
  401. unsigned long end_index;
  402. unsigned long pg_index;
  403. u64 last_offset;
  404. u64 isize = i_size_read(inode);
  405. int ret;
  406. struct page *page;
  407. unsigned long nr_pages = 0;
  408. struct extent_map *em;
  409. struct address_space *mapping = inode->i_mapping;
  410. struct extent_map_tree *em_tree;
  411. struct extent_io_tree *tree;
  412. u64 end;
  413. int misses = 0;
  414. last_offset = bio_end_offset(cb->orig_bio);
  415. em_tree = &BTRFS_I(inode)->extent_tree;
  416. tree = &BTRFS_I(inode)->io_tree;
  417. if (isize == 0)
  418. return 0;
  419. end_index = (i_size_read(inode) - 1) >> PAGE_SHIFT;
  420. while (last_offset < compressed_end) {
  421. pg_index = last_offset >> PAGE_SHIFT;
  422. if (pg_index > end_index)
  423. break;
  424. rcu_read_lock();
  425. page = radix_tree_lookup(&mapping->page_tree, pg_index);
  426. rcu_read_unlock();
  427. if (page && !radix_tree_exceptional_entry(page)) {
  428. misses++;
  429. if (misses > 4)
  430. break;
  431. goto next;
  432. }
  433. page = __page_cache_alloc(mapping_gfp_constraint(mapping,
  434. ~__GFP_FS));
  435. if (!page)
  436. break;
  437. if (add_to_page_cache_lru(page, mapping, pg_index, GFP_NOFS)) {
  438. put_page(page);
  439. goto next;
  440. }
  441. end = last_offset + PAGE_SIZE - 1;
  442. /*
  443. * at this point, we have a locked page in the page cache
  444. * for these bytes in the file. But, we have to make
  445. * sure they map to this compressed extent on disk.
  446. */
  447. set_page_extent_mapped(page);
  448. lock_extent(tree, last_offset, end);
  449. read_lock(&em_tree->lock);
  450. em = lookup_extent_mapping(em_tree, last_offset,
  451. PAGE_SIZE);
  452. read_unlock(&em_tree->lock);
  453. if (!em || last_offset < em->start ||
  454. (last_offset + PAGE_SIZE > extent_map_end(em)) ||
  455. (em->block_start >> 9) != cb->orig_bio->bi_iter.bi_sector) {
  456. free_extent_map(em);
  457. unlock_extent(tree, last_offset, end);
  458. unlock_page(page);
  459. put_page(page);
  460. break;
  461. }
  462. free_extent_map(em);
  463. if (page->index == end_index) {
  464. char *userpage;
  465. size_t zero_offset = isize & (PAGE_SIZE - 1);
  466. if (zero_offset) {
  467. int zeros;
  468. zeros = PAGE_SIZE - zero_offset;
  469. userpage = kmap_atomic(page);
  470. memset(userpage + zero_offset, 0, zeros);
  471. flush_dcache_page(page);
  472. kunmap_atomic(userpage);
  473. }
  474. }
  475. ret = bio_add_page(cb->orig_bio, page,
  476. PAGE_SIZE, 0);
  477. if (ret == PAGE_SIZE) {
  478. nr_pages++;
  479. put_page(page);
  480. } else {
  481. unlock_extent(tree, last_offset, end);
  482. unlock_page(page);
  483. put_page(page);
  484. break;
  485. }
  486. next:
  487. last_offset += PAGE_SIZE;
  488. }
  489. return 0;
  490. }
  491. /*
  492. * for a compressed read, the bio we get passed has all the inode pages
  493. * in it. We don't actually do IO on those pages but allocate new ones
  494. * to hold the compressed pages on disk.
  495. *
  496. * bio->bi_iter.bi_sector points to the compressed extent on disk
  497. * bio->bi_io_vec points to all of the inode pages
  498. *
  499. * After the compressed pages are read, we copy the bytes into the
  500. * bio we were passed and then call the bio end_io calls
  501. */
  502. int btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
  503. int mirror_num, unsigned long bio_flags)
  504. {
  505. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  506. struct extent_io_tree *tree;
  507. struct extent_map_tree *em_tree;
  508. struct compressed_bio *cb;
  509. struct btrfs_root *root = BTRFS_I(inode)->root;
  510. unsigned long compressed_len;
  511. unsigned long nr_pages;
  512. unsigned long pg_index;
  513. struct page *page;
  514. struct block_device *bdev;
  515. struct bio *comp_bio;
  516. u64 cur_disk_byte = (u64)bio->bi_iter.bi_sector << 9;
  517. u64 em_len;
  518. u64 em_start;
  519. struct extent_map *em;
  520. int ret = -ENOMEM;
  521. int faili = 0;
  522. u32 *sums;
  523. tree = &BTRFS_I(inode)->io_tree;
  524. em_tree = &BTRFS_I(inode)->extent_tree;
  525. /* we need the actual starting offset of this extent in the file */
  526. read_lock(&em_tree->lock);
  527. em = lookup_extent_mapping(em_tree,
  528. page_offset(bio->bi_io_vec->bv_page),
  529. PAGE_SIZE);
  530. read_unlock(&em_tree->lock);
  531. if (!em)
  532. return -EIO;
  533. compressed_len = em->block_len;
  534. cb = kmalloc(compressed_bio_size(root, compressed_len), GFP_NOFS);
  535. if (!cb)
  536. goto out;
  537. atomic_set(&cb->pending_bios, 0);
  538. cb->errors = 0;
  539. cb->inode = inode;
  540. cb->mirror_num = mirror_num;
  541. sums = &cb->sums;
  542. cb->start = em->orig_start;
  543. em_len = em->len;
  544. em_start = em->start;
  545. free_extent_map(em);
  546. em = NULL;
  547. cb->len = bio->bi_iter.bi_size;
  548. cb->compressed_len = compressed_len;
  549. cb->compress_type = extent_compress_type(bio_flags);
  550. cb->orig_bio = bio;
  551. nr_pages = DIV_ROUND_UP(compressed_len, PAGE_SIZE);
  552. cb->compressed_pages = kcalloc(nr_pages, sizeof(struct page *),
  553. GFP_NOFS);
  554. if (!cb->compressed_pages)
  555. goto fail1;
  556. bdev = fs_info->fs_devices->latest_bdev;
  557. for (pg_index = 0; pg_index < nr_pages; pg_index++) {
  558. cb->compressed_pages[pg_index] = alloc_page(GFP_NOFS |
  559. __GFP_HIGHMEM);
  560. if (!cb->compressed_pages[pg_index]) {
  561. faili = pg_index - 1;
  562. ret = -ENOMEM;
  563. goto fail2;
  564. }
  565. }
  566. faili = nr_pages - 1;
  567. cb->nr_pages = nr_pages;
  568. add_ra_bio_pages(inode, em_start + em_len, cb);
  569. /* include any pages we added in add_ra-bio_pages */
  570. cb->len = bio->bi_iter.bi_size;
  571. comp_bio = compressed_bio_alloc(bdev, cur_disk_byte, GFP_NOFS);
  572. if (!comp_bio)
  573. goto fail2;
  574. bio_set_op_attrs (comp_bio, REQ_OP_READ, 0);
  575. comp_bio->bi_private = cb;
  576. comp_bio->bi_end_io = end_compressed_bio_read;
  577. atomic_inc(&cb->pending_bios);
  578. for (pg_index = 0; pg_index < nr_pages; pg_index++) {
  579. page = cb->compressed_pages[pg_index];
  580. page->mapping = inode->i_mapping;
  581. page->index = em_start >> PAGE_SHIFT;
  582. if (comp_bio->bi_iter.bi_size)
  583. ret = tree->ops->merge_bio_hook(page, 0,
  584. PAGE_SIZE,
  585. comp_bio, 0);
  586. else
  587. ret = 0;
  588. page->mapping = NULL;
  589. if (ret || bio_add_page(comp_bio, page, PAGE_SIZE, 0) <
  590. PAGE_SIZE) {
  591. bio_get(comp_bio);
  592. ret = btrfs_bio_wq_end_io(fs_info, comp_bio,
  593. BTRFS_WQ_ENDIO_DATA);
  594. BUG_ON(ret); /* -ENOMEM */
  595. /*
  596. * inc the count before we submit the bio so
  597. * we know the end IO handler won't happen before
  598. * we inc the count. Otherwise, the cb might get
  599. * freed before we're done setting it up
  600. */
  601. atomic_inc(&cb->pending_bios);
  602. if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
  603. ret = btrfs_lookup_bio_sums(root, inode,
  604. comp_bio, sums);
  605. BUG_ON(ret); /* -ENOMEM */
  606. }
  607. sums += DIV_ROUND_UP(comp_bio->bi_iter.bi_size,
  608. fs_info->sectorsize);
  609. ret = btrfs_map_bio(root, comp_bio, mirror_num, 0);
  610. if (ret) {
  611. comp_bio->bi_error = ret;
  612. bio_endio(comp_bio);
  613. }
  614. bio_put(comp_bio);
  615. comp_bio = compressed_bio_alloc(bdev, cur_disk_byte,
  616. GFP_NOFS);
  617. BUG_ON(!comp_bio);
  618. bio_set_op_attrs(comp_bio, REQ_OP_READ, 0);
  619. comp_bio->bi_private = cb;
  620. comp_bio->bi_end_io = end_compressed_bio_read;
  621. bio_add_page(comp_bio, page, PAGE_SIZE, 0);
  622. }
  623. cur_disk_byte += PAGE_SIZE;
  624. }
  625. bio_get(comp_bio);
  626. ret = btrfs_bio_wq_end_io(fs_info, comp_bio, BTRFS_WQ_ENDIO_DATA);
  627. BUG_ON(ret); /* -ENOMEM */
  628. if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
  629. ret = btrfs_lookup_bio_sums(root, inode, comp_bio, sums);
  630. BUG_ON(ret); /* -ENOMEM */
  631. }
  632. ret = btrfs_map_bio(root, comp_bio, mirror_num, 0);
  633. if (ret) {
  634. comp_bio->bi_error = ret;
  635. bio_endio(comp_bio);
  636. }
  637. bio_put(comp_bio);
  638. return 0;
  639. fail2:
  640. while (faili >= 0) {
  641. __free_page(cb->compressed_pages[faili]);
  642. faili--;
  643. }
  644. kfree(cb->compressed_pages);
  645. fail1:
  646. kfree(cb);
  647. out:
  648. free_extent_map(em);
  649. return ret;
  650. }
  651. static struct {
  652. struct list_head idle_ws;
  653. spinlock_t ws_lock;
  654. /* Number of free workspaces */
  655. int free_ws;
  656. /* Total number of allocated workspaces */
  657. atomic_t total_ws;
  658. /* Waiters for a free workspace */
  659. wait_queue_head_t ws_wait;
  660. } btrfs_comp_ws[BTRFS_COMPRESS_TYPES];
  661. static const struct btrfs_compress_op * const btrfs_compress_op[] = {
  662. &btrfs_zlib_compress,
  663. &btrfs_lzo_compress,
  664. };
  665. void __init btrfs_init_compress(void)
  666. {
  667. int i;
  668. for (i = 0; i < BTRFS_COMPRESS_TYPES; i++) {
  669. struct list_head *workspace;
  670. INIT_LIST_HEAD(&btrfs_comp_ws[i].idle_ws);
  671. spin_lock_init(&btrfs_comp_ws[i].ws_lock);
  672. atomic_set(&btrfs_comp_ws[i].total_ws, 0);
  673. init_waitqueue_head(&btrfs_comp_ws[i].ws_wait);
  674. /*
  675. * Preallocate one workspace for each compression type so
  676. * we can guarantee forward progress in the worst case
  677. */
  678. workspace = btrfs_compress_op[i]->alloc_workspace();
  679. if (IS_ERR(workspace)) {
  680. pr_warn("BTRFS: cannot preallocate compression workspace, will try later\n");
  681. } else {
  682. atomic_set(&btrfs_comp_ws[i].total_ws, 1);
  683. btrfs_comp_ws[i].free_ws = 1;
  684. list_add(workspace, &btrfs_comp_ws[i].idle_ws);
  685. }
  686. }
  687. }
  688. /*
  689. * This finds an available workspace or allocates a new one.
  690. * If it's not possible to allocate a new one, waits until there's one.
  691. * Preallocation makes a forward progress guarantees and we do not return
  692. * errors.
  693. */
  694. static struct list_head *find_workspace(int type)
  695. {
  696. struct list_head *workspace;
  697. int cpus = num_online_cpus();
  698. int idx = type - 1;
  699. struct list_head *idle_ws = &btrfs_comp_ws[idx].idle_ws;
  700. spinlock_t *ws_lock = &btrfs_comp_ws[idx].ws_lock;
  701. atomic_t *total_ws = &btrfs_comp_ws[idx].total_ws;
  702. wait_queue_head_t *ws_wait = &btrfs_comp_ws[idx].ws_wait;
  703. int *free_ws = &btrfs_comp_ws[idx].free_ws;
  704. again:
  705. spin_lock(ws_lock);
  706. if (!list_empty(idle_ws)) {
  707. workspace = idle_ws->next;
  708. list_del(workspace);
  709. (*free_ws)--;
  710. spin_unlock(ws_lock);
  711. return workspace;
  712. }
  713. if (atomic_read(total_ws) > cpus) {
  714. DEFINE_WAIT(wait);
  715. spin_unlock(ws_lock);
  716. prepare_to_wait(ws_wait, &wait, TASK_UNINTERRUPTIBLE);
  717. if (atomic_read(total_ws) > cpus && !*free_ws)
  718. schedule();
  719. finish_wait(ws_wait, &wait);
  720. goto again;
  721. }
  722. atomic_inc(total_ws);
  723. spin_unlock(ws_lock);
  724. workspace = btrfs_compress_op[idx]->alloc_workspace();
  725. if (IS_ERR(workspace)) {
  726. atomic_dec(total_ws);
  727. wake_up(ws_wait);
  728. /*
  729. * Do not return the error but go back to waiting. There's a
  730. * workspace preallocated for each type and the compression
  731. * time is bounded so we get to a workspace eventually. This
  732. * makes our caller's life easier.
  733. *
  734. * To prevent silent and low-probability deadlocks (when the
  735. * initial preallocation fails), check if there are any
  736. * workspaces at all.
  737. */
  738. if (atomic_read(total_ws) == 0) {
  739. static DEFINE_RATELIMIT_STATE(_rs,
  740. /* once per minute */ 60 * HZ,
  741. /* no burst */ 1);
  742. if (__ratelimit(&_rs)) {
  743. pr_warn("BTRFS: no compression workspaces, low memory, retrying\n");
  744. }
  745. }
  746. goto again;
  747. }
  748. return workspace;
  749. }
  750. /*
  751. * put a workspace struct back on the list or free it if we have enough
  752. * idle ones sitting around
  753. */
  754. static void free_workspace(int type, struct list_head *workspace)
  755. {
  756. int idx = type - 1;
  757. struct list_head *idle_ws = &btrfs_comp_ws[idx].idle_ws;
  758. spinlock_t *ws_lock = &btrfs_comp_ws[idx].ws_lock;
  759. atomic_t *total_ws = &btrfs_comp_ws[idx].total_ws;
  760. wait_queue_head_t *ws_wait = &btrfs_comp_ws[idx].ws_wait;
  761. int *free_ws = &btrfs_comp_ws[idx].free_ws;
  762. spin_lock(ws_lock);
  763. if (*free_ws < num_online_cpus()) {
  764. list_add(workspace, idle_ws);
  765. (*free_ws)++;
  766. spin_unlock(ws_lock);
  767. goto wake;
  768. }
  769. spin_unlock(ws_lock);
  770. btrfs_compress_op[idx]->free_workspace(workspace);
  771. atomic_dec(total_ws);
  772. wake:
  773. /*
  774. * Make sure counter is updated before we wake up waiters.
  775. */
  776. smp_mb();
  777. if (waitqueue_active(ws_wait))
  778. wake_up(ws_wait);
  779. }
  780. /*
  781. * cleanup function for module exit
  782. */
  783. static void free_workspaces(void)
  784. {
  785. struct list_head *workspace;
  786. int i;
  787. for (i = 0; i < BTRFS_COMPRESS_TYPES; i++) {
  788. while (!list_empty(&btrfs_comp_ws[i].idle_ws)) {
  789. workspace = btrfs_comp_ws[i].idle_ws.next;
  790. list_del(workspace);
  791. btrfs_compress_op[i]->free_workspace(workspace);
  792. atomic_dec(&btrfs_comp_ws[i].total_ws);
  793. }
  794. }
  795. }
  796. /*
  797. * given an address space and start/len, compress the bytes.
  798. *
  799. * pages are allocated to hold the compressed result and stored
  800. * in 'pages'
  801. *
  802. * out_pages is used to return the number of pages allocated. There
  803. * may be pages allocated even if we return an error
  804. *
  805. * total_in is used to return the number of bytes actually read. It
  806. * may be smaller then len if we had to exit early because we
  807. * ran out of room in the pages array or because we cross the
  808. * max_out threshold.
  809. *
  810. * total_out is used to return the total number of compressed bytes
  811. *
  812. * max_out tells us the max number of bytes that we're allowed to
  813. * stuff into pages
  814. */
  815. int btrfs_compress_pages(int type, struct address_space *mapping,
  816. u64 start, unsigned long len,
  817. struct page **pages,
  818. unsigned long nr_dest_pages,
  819. unsigned long *out_pages,
  820. unsigned long *total_in,
  821. unsigned long *total_out,
  822. unsigned long max_out)
  823. {
  824. struct list_head *workspace;
  825. int ret;
  826. workspace = find_workspace(type);
  827. ret = btrfs_compress_op[type-1]->compress_pages(workspace, mapping,
  828. start, len, pages,
  829. nr_dest_pages, out_pages,
  830. total_in, total_out,
  831. max_out);
  832. free_workspace(type, workspace);
  833. return ret;
  834. }
  835. /*
  836. * pages_in is an array of pages with compressed data.
  837. *
  838. * disk_start is the starting logical offset of this array in the file
  839. *
  840. * orig_bio contains the pages from the file that we want to decompress into
  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_bio(int type, struct page **pages_in,
  850. u64 disk_start, struct bio *orig_bio,
  851. 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_bio(workspace, pages_in,
  857. disk_start, orig_bio,
  858. 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 *bio)
  893. {
  894. unsigned long buf_offset;
  895. unsigned long current_buf_start;
  896. unsigned long start_byte;
  897. unsigned long working_bytes = total_out - buf_start;
  898. unsigned long bytes;
  899. char *kaddr;
  900. struct bio_vec bvec = bio_iter_iovec(bio, bio->bi_iter);
  901. /*
  902. * start byte is the first byte of the page we're currently
  903. * copying into relative to the start of the compressed data.
  904. */
  905. start_byte = page_offset(bvec.bv_page) - disk_start;
  906. /* we haven't yet hit data corresponding to this page */
  907. if (total_out <= start_byte)
  908. return 1;
  909. /*
  910. * the start of the data we care about is offset into
  911. * the middle of our working buffer
  912. */
  913. if (total_out > start_byte && buf_start < start_byte) {
  914. buf_offset = start_byte - buf_start;
  915. working_bytes -= buf_offset;
  916. } else {
  917. buf_offset = 0;
  918. }
  919. current_buf_start = buf_start;
  920. /* copy bytes from the working buffer into the pages */
  921. while (working_bytes > 0) {
  922. bytes = min_t(unsigned long, bvec.bv_len,
  923. PAGE_SIZE - buf_offset);
  924. bytes = min(bytes, working_bytes);
  925. kaddr = kmap_atomic(bvec.bv_page);
  926. memcpy(kaddr + bvec.bv_offset, buf + buf_offset, bytes);
  927. kunmap_atomic(kaddr);
  928. flush_dcache_page(bvec.bv_page);
  929. buf_offset += bytes;
  930. working_bytes -= bytes;
  931. current_buf_start += bytes;
  932. /* check if we need to pick another page */
  933. bio_advance(bio, bytes);
  934. if (!bio->bi_iter.bi_size)
  935. return 0;
  936. bvec = bio_iter_iovec(bio, bio->bi_iter);
  937. start_byte = page_offset(bvec.bv_page) - disk_start;
  938. /*
  939. * make sure our new page is covered by this
  940. * working buffer
  941. */
  942. if (total_out <= start_byte)
  943. return 1;
  944. /*
  945. * the next page in the biovec might not be adjacent
  946. * to the last page, but it might still be found
  947. * inside this working buffer. bump our offset pointer
  948. */
  949. if (total_out > start_byte &&
  950. current_buf_start < start_byte) {
  951. buf_offset = start_byte - buf_start;
  952. working_bytes = total_out - start_byte;
  953. current_buf_start = buf_start + buf_offset;
  954. }
  955. }
  956. return 1;
  957. }