compression.c 29 KB

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