aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs/inode.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/btrfs/inode.c')
-rw-r--r--fs/btrfs/inode.c157
1 files changed, 4 insertions, 153 deletions
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index b6b612e14ed7..9d0eaa57d4ee 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -45,10 +45,10 @@
45#include "btrfs_inode.h" 45#include "btrfs_inode.h"
46#include "ioctl.h" 46#include "ioctl.h"
47#include "print-tree.h" 47#include "print-tree.h"
48#include "volumes.h"
49#include "ordered-data.h" 48#include "ordered-data.h"
50#include "xattr.h" 49#include "xattr.h"
51#include "tree-log.h" 50#include "tree-log.h"
51#include "volumes.h"
52#include "compression.h" 52#include "compression.h"
53#include "locking.h" 53#include "locking.h"
54#include "free-space-cache.h" 54#include "free-space-cache.h"
@@ -1823,153 +1823,9 @@ static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
1823} 1823}
1824 1824
1825/* 1825/*
1826 * When IO fails, either with EIO or csum verification fails, we
1827 * try other mirrors that might have a good copy of the data. This
1828 * io_failure_record is used to record state as we go through all the
1829 * mirrors. If another mirror has good data, the page is set up to date
1830 * and things continue. If a good mirror can't be found, the original
1831 * bio end_io callback is called to indicate things have failed.
1832 */
1833struct io_failure_record {
1834 struct page *page;
1835 u64 start;
1836 u64 len;
1837 u64 logical;
1838 unsigned long bio_flags;
1839 int last_mirror;
1840};
1841
1842static int btrfs_io_failed_hook(struct bio *failed_bio,
1843 struct page *page, u64 start, u64 end,
1844 struct extent_state *state)
1845{
1846 struct io_failure_record *failrec = NULL;
1847 u64 private;
1848 struct extent_map *em;
1849 struct inode *inode = page->mapping->host;
1850 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1851 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1852 struct bio *bio;
1853 int num_copies;
1854 int ret;
1855 int rw;
1856 u64 logical;
1857
1858 ret = get_state_private(failure_tree, start, &private);
1859 if (ret) {
1860 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
1861 if (!failrec)
1862 return -ENOMEM;
1863 failrec->start = start;
1864 failrec->len = end - start + 1;
1865 failrec->last_mirror = 0;
1866 failrec->bio_flags = 0;
1867
1868 read_lock(&em_tree->lock);
1869 em = lookup_extent_mapping(em_tree, start, failrec->len);
1870 if (em->start > start || em->start + em->len < start) {
1871 free_extent_map(em);
1872 em = NULL;
1873 }
1874 read_unlock(&em_tree->lock);
1875
1876 if (IS_ERR_OR_NULL(em)) {
1877 kfree(failrec);
1878 return -EIO;
1879 }
1880 logical = start - em->start;
1881 logical = em->block_start + logical;
1882 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
1883 logical = em->block_start;
1884 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
1885 extent_set_compress_type(&failrec->bio_flags,
1886 em->compress_type);
1887 }
1888 failrec->logical = logical;
1889 free_extent_map(em);
1890 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
1891 EXTENT_DIRTY, GFP_NOFS);
1892 set_state_private(failure_tree, start,
1893 (u64)(unsigned long)failrec);
1894 } else {
1895 failrec = (struct io_failure_record *)(unsigned long)private;
1896 }
1897 num_copies = btrfs_num_copies(
1898 &BTRFS_I(inode)->root->fs_info->mapping_tree,
1899 failrec->logical, failrec->len);
1900 failrec->last_mirror++;
1901 if (!state) {
1902 spin_lock(&BTRFS_I(inode)->io_tree.lock);
1903 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1904 failrec->start,
1905 EXTENT_LOCKED);
1906 if (state && state->start != failrec->start)
1907 state = NULL;
1908 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
1909 }
1910 if (!state || failrec->last_mirror > num_copies) {
1911 set_state_private(failure_tree, failrec->start, 0);
1912 clear_extent_bits(failure_tree, failrec->start,
1913 failrec->start + failrec->len - 1,
1914 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1915 kfree(failrec);
1916 return -EIO;
1917 }
1918 bio = bio_alloc(GFP_NOFS, 1);
1919 bio->bi_private = state;
1920 bio->bi_end_io = failed_bio->bi_end_io;
1921 bio->bi_sector = failrec->logical >> 9;
1922 bio->bi_bdev = failed_bio->bi_bdev;
1923 bio->bi_size = 0;
1924
1925 bio_add_page(bio, page, failrec->len, start - page_offset(page));
1926 if (failed_bio->bi_rw & REQ_WRITE)
1927 rw = WRITE;
1928 else
1929 rw = READ;
1930
1931 ret = BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
1932 failrec->last_mirror,
1933 failrec->bio_flags, 0);
1934 return ret;
1935}
1936
1937/*
1938 * each time an IO finishes, we do a fast check in the IO failure tree
1939 * to see if we need to process or clean up an io_failure_record
1940 */
1941static int btrfs_clean_io_failures(struct inode *inode, u64 start)
1942{
1943 u64 private;
1944 u64 private_failure;
1945 struct io_failure_record *failure;
1946 int ret;
1947
1948 private = 0;
1949 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1950 (u64)-1, 1, EXTENT_DIRTY, 0)) {
1951 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
1952 start, &private_failure);
1953 if (ret == 0) {
1954 failure = (struct io_failure_record *)(unsigned long)
1955 private_failure;
1956 set_state_private(&BTRFS_I(inode)->io_failure_tree,
1957 failure->start, 0);
1958 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
1959 failure->start,
1960 failure->start + failure->len - 1,
1961 EXTENT_DIRTY | EXTENT_LOCKED,
1962 GFP_NOFS);
1963 kfree(failure);
1964 }
1965 }
1966 return 0;
1967}
1968
1969/*
1970 * when reads are done, we need to check csums to verify the data is correct 1826 * when reads are done, we need to check csums to verify the data is correct
1971 * if there's a match, we allow the bio to finish. If not, we go through 1827 * if there's a match, we allow the bio to finish. If not, the code in
1972 * the io_failure_record routines to find good copies 1828 * extent_io.c will try to find good copies for us.
1973 */ 1829 */
1974static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end, 1830static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
1975 struct extent_state *state) 1831 struct extent_state *state)
@@ -2015,10 +1871,6 @@ static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
2015 1871
2016 kunmap_atomic(kaddr, KM_USER0); 1872 kunmap_atomic(kaddr, KM_USER0);
2017good: 1873good:
2018 /* if the io failure tree for this inode is non-empty,
2019 * check to see if we've recovered from a failed IO
2020 */
2021 btrfs_clean_io_failures(inode, start);
2022 return 0; 1874 return 0;
2023 1875
2024zeroit: 1876zeroit:
@@ -6273,7 +6125,7 @@ int btrfs_readpage(struct file *file, struct page *page)
6273{ 6125{
6274 struct extent_io_tree *tree; 6126 struct extent_io_tree *tree;
6275 tree = &BTRFS_I(page->mapping->host)->io_tree; 6127 tree = &BTRFS_I(page->mapping->host)->io_tree;
6276 return extent_read_full_page(tree, page, btrfs_get_extent); 6128 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
6277} 6129}
6278 6130
6279static int btrfs_writepage(struct page *page, struct writeback_control *wbc) 6131static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
@@ -7406,7 +7258,6 @@ static struct extent_io_ops btrfs_extent_io_ops = {
7406 .readpage_end_io_hook = btrfs_readpage_end_io_hook, 7258 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
7407 .writepage_end_io_hook = btrfs_writepage_end_io_hook, 7259 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
7408 .writepage_start_hook = btrfs_writepage_start_hook, 7260 .writepage_start_hook = btrfs_writepage_start_hook,
7409 .readpage_io_failed_hook = btrfs_io_failed_hook,
7410 .set_bit_hook = btrfs_set_bit_hook, 7261 .set_bit_hook = btrfs_set_bit_hook,
7411 .clear_bit_hook = btrfs_clear_bit_hook, 7262 .clear_bit_hook = btrfs_clear_bit_hook,
7412 .merge_extent_hook = btrfs_merge_extent_hook, 7263 .merge_extent_hook = btrfs_merge_extent_hook,