compression.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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/smp_lock.h>
  29. #include <linux/backing-dev.h>
  30. #include <linux/mpage.h>
  31. #include <linux/swap.h>
  32. #include <linux/writeback.h>
  33. #include <linux/bit_spinlock.h>
  34. #include <linux/version.h>
  35. #include "ctree.h"
  36. #include "disk-io.h"
  37. #include "transaction.h"
  38. #include "btrfs_inode.h"
  39. #include "volumes.h"
  40. #include "ordered-data.h"
  41. #include "compat.h"
  42. #include "compression.h"
  43. #include "extent_io.h"
  44. #include "extent_map.h"
  45. struct compressed_bio {
  46. /* number of bios pending for this compressed extent */
  47. atomic_t pending_bios;
  48. /* the pages with the compressed data on them */
  49. struct page **compressed_pages;
  50. /* inode that owns this data */
  51. struct inode *inode;
  52. /* starting offset in the inode for our pages */
  53. u64 start;
  54. /* number of bytes in the inode we're working on */
  55. unsigned long len;
  56. /* number of bytes on disk */
  57. unsigned long compressed_len;
  58. /* number of compressed pages in the array */
  59. unsigned long nr_pages;
  60. /* IO errors */
  61. int errors;
  62. /* for reads, this is the bio we are copying the data into */
  63. struct bio *orig_bio;
  64. };
  65. static struct bio *compressed_bio_alloc(struct block_device *bdev,
  66. u64 first_byte, gfp_t gfp_flags)
  67. {
  68. struct bio *bio;
  69. int nr_vecs;
  70. nr_vecs = bio_get_nr_vecs(bdev);
  71. bio = bio_alloc(gfp_flags, nr_vecs);
  72. if (bio == NULL && (current->flags & PF_MEMALLOC)) {
  73. while (!bio && (nr_vecs /= 2))
  74. bio = bio_alloc(gfp_flags, nr_vecs);
  75. }
  76. if (bio) {
  77. bio->bi_size = 0;
  78. bio->bi_bdev = bdev;
  79. bio->bi_sector = first_byte >> 9;
  80. }
  81. return bio;
  82. }
  83. /* when we finish reading compressed pages from the disk, we
  84. * decompress them and then run the bio end_io routines on the
  85. * decompressed pages (in the inode address space).
  86. *
  87. * This allows the checksumming and other IO error handling routines
  88. * to work normally
  89. *
  90. * The compressed pages are freed here, and it must be run
  91. * in process context
  92. */
  93. static void end_compressed_bio_read(struct bio *bio, int err)
  94. {
  95. struct extent_io_tree *tree;
  96. struct compressed_bio *cb = bio->bi_private;
  97. struct inode *inode;
  98. struct page *page;
  99. unsigned long index;
  100. int ret;
  101. if (err)
  102. cb->errors = 1;
  103. /* if there are more bios still pending for this compressed
  104. * extent, just exit
  105. */
  106. if (!atomic_dec_and_test(&cb->pending_bios))
  107. goto out;
  108. /* ok, we're the last bio for this extent, lets start
  109. * the decompression.
  110. */
  111. inode = cb->inode;
  112. tree = &BTRFS_I(inode)->io_tree;
  113. ret = btrfs_zlib_decompress_biovec(cb->compressed_pages,
  114. cb->start,
  115. cb->orig_bio->bi_io_vec,
  116. cb->orig_bio->bi_vcnt,
  117. cb->compressed_len);
  118. if (ret)
  119. cb->errors = 1;
  120. /* release the compressed pages */
  121. index = 0;
  122. for (index = 0; index < cb->nr_pages; index++) {
  123. page = cb->compressed_pages[index];
  124. page->mapping = NULL;
  125. page_cache_release(page);
  126. }
  127. /* do io completion on the original bio */
  128. if (cb->errors)
  129. bio_io_error(cb->orig_bio);
  130. else
  131. bio_endio(cb->orig_bio, 0);
  132. /* finally free the cb struct */
  133. kfree(cb->compressed_pages);
  134. kfree(cb);
  135. out:
  136. bio_put(bio);
  137. }
  138. /*
  139. * Clear the writeback bits on all of the file
  140. * pages for a compressed write
  141. */
  142. static noinline int end_compressed_writeback(struct inode *inode, u64 start,
  143. unsigned long ram_size)
  144. {
  145. unsigned long index = start >> PAGE_CACHE_SHIFT;
  146. unsigned long end_index = (start + ram_size - 1) >> PAGE_CACHE_SHIFT;
  147. struct page *pages[16];
  148. unsigned long nr_pages = end_index - index + 1;
  149. int i;
  150. int ret;
  151. while(nr_pages > 0) {
  152. ret = find_get_pages_contig(inode->i_mapping, index,
  153. min(nr_pages, ARRAY_SIZE(pages)), pages);
  154. if (ret == 0) {
  155. nr_pages -= 1;
  156. index += 1;
  157. continue;
  158. }
  159. for (i = 0; i < ret; i++) {
  160. end_page_writeback(pages[i]);
  161. page_cache_release(pages[i]);
  162. }
  163. nr_pages -= ret;
  164. index += ret;
  165. }
  166. /* the inode may be gone now */
  167. return 0;
  168. }
  169. /*
  170. * do the cleanup once all the compressed pages hit the disk.
  171. * This will clear writeback on the file pages and free the compressed
  172. * pages.
  173. *
  174. * This also calls the writeback end hooks for the file pages so that
  175. * metadata and checksums can be updated in the file.
  176. */
  177. static void end_compressed_bio_write(struct bio *bio, int err)
  178. {
  179. struct extent_io_tree *tree;
  180. struct compressed_bio *cb = bio->bi_private;
  181. struct inode *inode;
  182. struct page *page;
  183. unsigned long index;
  184. if (err)
  185. cb->errors = 1;
  186. /* if there are more bios still pending for this compressed
  187. * extent, just exit
  188. */
  189. if (!atomic_dec_and_test(&cb->pending_bios))
  190. goto out;
  191. /* ok, we're the last bio for this extent, step one is to
  192. * call back into the FS and do all the end_io operations
  193. */
  194. inode = cb->inode;
  195. tree = &BTRFS_I(inode)->io_tree;
  196. tree->ops->writepage_end_io_hook(cb->compressed_pages[0],
  197. cb->start,
  198. cb->start + cb->len - 1,
  199. NULL, 1);
  200. end_compressed_writeback(inode, cb->start, cb->len);
  201. /* note, our inode could be gone now */
  202. /*
  203. * release the compressed pages, these came from alloc_page and
  204. * are not attached to the inode at all
  205. */
  206. index = 0;
  207. for (index = 0; index < cb->nr_pages; index++) {
  208. page = cb->compressed_pages[index];
  209. page->mapping = NULL;
  210. page_cache_release(page);
  211. }
  212. /* finally free the cb struct */
  213. kfree(cb->compressed_pages);
  214. kfree(cb);
  215. out:
  216. bio_put(bio);
  217. }
  218. /*
  219. * worker function to build and submit bios for previously compressed pages.
  220. * The corresponding pages in the inode should be marked for writeback
  221. * and the compressed pages should have a reference on them for dropping
  222. * when the IO is complete.
  223. *
  224. * This also checksums the file bytes and gets things ready for
  225. * the end io hooks.
  226. */
  227. int btrfs_submit_compressed_write(struct inode *inode, u64 start,
  228. unsigned long len, u64 disk_start,
  229. unsigned long compressed_len,
  230. struct page **compressed_pages,
  231. unsigned long nr_pages)
  232. {
  233. struct bio *bio = NULL;
  234. struct btrfs_root *root = BTRFS_I(inode)->root;
  235. struct compressed_bio *cb;
  236. unsigned long bytes_left;
  237. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  238. int page_index = 0;
  239. struct page *page;
  240. u64 first_byte = disk_start;
  241. struct block_device *bdev;
  242. int ret;
  243. WARN_ON(start & ((u64)PAGE_CACHE_SIZE - 1));
  244. cb = kmalloc(sizeof(*cb), GFP_NOFS);
  245. atomic_set(&cb->pending_bios, 0);
  246. cb->errors = 0;
  247. cb->inode = inode;
  248. cb->start = start;
  249. cb->len = len;
  250. cb->compressed_pages = compressed_pages;
  251. cb->compressed_len = compressed_len;
  252. cb->orig_bio = NULL;
  253. cb->nr_pages = nr_pages;
  254. bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
  255. ret = btrfs_csum_file_bytes(root, inode, start, len);
  256. BUG_ON(ret);
  257. bio = compressed_bio_alloc(bdev, first_byte, GFP_NOFS);
  258. bio->bi_private = cb;
  259. bio->bi_end_io = end_compressed_bio_write;
  260. atomic_inc(&cb->pending_bios);
  261. /* create and submit bios for the compressed pages */
  262. bytes_left = compressed_len;
  263. while(bytes_left > 0) {
  264. page = compressed_pages[page_index];
  265. page->mapping = inode->i_mapping;
  266. if (bio->bi_size)
  267. ret = io_tree->ops->merge_bio_hook(page, 0,
  268. PAGE_CACHE_SIZE,
  269. bio, 0);
  270. else
  271. ret = 0;
  272. if (ret || bio_add_page(bio, page, PAGE_CACHE_SIZE, 0) <
  273. PAGE_CACHE_SIZE) {
  274. bio_get(bio);
  275. ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
  276. BUG_ON(ret);
  277. ret = btrfs_map_bio(root, WRITE, bio, 0, 1);
  278. BUG_ON(ret);
  279. bio_put(bio);
  280. bio = compressed_bio_alloc(bdev, first_byte, GFP_NOFS);
  281. atomic_inc(&cb->pending_bios);
  282. bio->bi_private = cb;
  283. bio->bi_end_io = end_compressed_bio_write;
  284. bio_add_page(bio, page, PAGE_CACHE_SIZE, 0);
  285. }
  286. page_index++;
  287. bytes_left -= PAGE_CACHE_SIZE;
  288. first_byte += PAGE_CACHE_SIZE;
  289. }
  290. bio_get(bio);
  291. ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
  292. BUG_ON(ret);
  293. ret = btrfs_map_bio(root, WRITE, bio, 0, 1);
  294. BUG_ON(ret);
  295. bio_put(bio);
  296. return 0;
  297. }
  298. /*
  299. * for a compressed read, the bio we get passed has all the inode pages
  300. * in it. We don't actually do IO on those pages but allocate new ones
  301. * to hold the compressed pages on disk.
  302. *
  303. * bio->bi_sector points to the compressed extent on disk
  304. * bio->bi_io_vec points to all of the inode pages
  305. * bio->bi_vcnt is a count of pages
  306. *
  307. * After the compressed pages are read, we copy the bytes into the
  308. * bio we were passed and then call the bio end_io calls
  309. */
  310. int btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
  311. int mirror_num, unsigned long bio_flags)
  312. {
  313. struct extent_io_tree *tree;
  314. struct extent_map_tree *em_tree;
  315. struct compressed_bio *cb;
  316. struct btrfs_root *root = BTRFS_I(inode)->root;
  317. unsigned long uncompressed_len = bio->bi_vcnt * PAGE_CACHE_SIZE;
  318. unsigned long compressed_len;
  319. unsigned long nr_pages;
  320. unsigned long page_index;
  321. struct page *page;
  322. struct block_device *bdev;
  323. struct bio *comp_bio;
  324. u64 cur_disk_byte = (u64)bio->bi_sector << 9;
  325. struct extent_map *em;
  326. int ret;
  327. tree = &BTRFS_I(inode)->io_tree;
  328. em_tree = &BTRFS_I(inode)->extent_tree;
  329. /* we need the actual starting offset of this extent in the file */
  330. spin_lock(&em_tree->lock);
  331. em = lookup_extent_mapping(em_tree,
  332. page_offset(bio->bi_io_vec->bv_page),
  333. PAGE_CACHE_SIZE);
  334. spin_unlock(&em_tree->lock);
  335. cb = kmalloc(sizeof(*cb), GFP_NOFS);
  336. atomic_set(&cb->pending_bios, 0);
  337. cb->errors = 0;
  338. cb->inode = inode;
  339. cb->start = em->start;
  340. compressed_len = em->block_len;
  341. free_extent_map(em);
  342. cb->len = uncompressed_len;
  343. cb->compressed_len = compressed_len;
  344. cb->orig_bio = bio;
  345. nr_pages = (compressed_len + PAGE_CACHE_SIZE - 1) /
  346. PAGE_CACHE_SIZE;
  347. cb->compressed_pages = kmalloc(sizeof(struct page *) * nr_pages,
  348. GFP_NOFS);
  349. bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
  350. for (page_index = 0; page_index < nr_pages; page_index++) {
  351. cb->compressed_pages[page_index] = alloc_page(GFP_NOFS |
  352. __GFP_HIGHMEM);
  353. }
  354. cb->nr_pages = nr_pages;
  355. comp_bio = compressed_bio_alloc(bdev, cur_disk_byte, GFP_NOFS);
  356. comp_bio->bi_private = cb;
  357. comp_bio->bi_end_io = end_compressed_bio_read;
  358. atomic_inc(&cb->pending_bios);
  359. for (page_index = 0; page_index < nr_pages; page_index++) {
  360. page = cb->compressed_pages[page_index];
  361. page->mapping = inode->i_mapping;
  362. if (comp_bio->bi_size)
  363. ret = tree->ops->merge_bio_hook(page, 0,
  364. PAGE_CACHE_SIZE,
  365. comp_bio, 0);
  366. else
  367. ret = 0;
  368. if (ret || bio_add_page(comp_bio, page, PAGE_CACHE_SIZE, 0) <
  369. PAGE_CACHE_SIZE) {
  370. bio_get(comp_bio);
  371. ret = btrfs_bio_wq_end_io(root->fs_info, comp_bio, 0);
  372. BUG_ON(ret);
  373. ret = btrfs_map_bio(root, READ, comp_bio, 0, 0);
  374. BUG_ON(ret);
  375. bio_put(comp_bio);
  376. comp_bio = compressed_bio_alloc(bdev, cur_disk_byte,
  377. GFP_NOFS);
  378. atomic_inc(&cb->pending_bios);
  379. bio->bi_private = cb;
  380. bio->bi_end_io = end_compressed_bio_write;
  381. bio_add_page(bio, page, PAGE_CACHE_SIZE, 0);
  382. }
  383. cur_disk_byte += PAGE_CACHE_SIZE;
  384. }
  385. bio_get(comp_bio);
  386. ret = btrfs_bio_wq_end_io(root->fs_info, comp_bio, 0);
  387. BUG_ON(ret);
  388. ret = btrfs_map_bio(root, READ, comp_bio, 0, 0);
  389. BUG_ON(ret);
  390. bio_put(comp_bio);
  391. return 0;
  392. }