Merge branch 'irq-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cascardo/linux.git] / fs / btrfs / extent_io.c
index 8c4a0a0..66a7551 100644 (file)
@@ -2120,8 +2120,9 @@ int clean_io_failure(struct inode *inode, u64 start, struct page *page,
 
        if (failrec->in_validation) {
                /* there was no real error, just free the record */
-               pr_debug("clean_io_failure: freeing dummy error at %llu\n",
-                        failrec->start);
+               btrfs_debug(fs_info,
+                       "clean_io_failure: freeing dummy error at %llu",
+                       failrec->start);
                goto out;
        }
        if (fs_info->sb->s_flags & MS_RDONLY)
@@ -2187,6 +2188,7 @@ void btrfs_free_io_failure_record(struct inode *inode, u64 start, u64 end)
 int btrfs_get_io_failure_record(struct inode *inode, u64 start, u64 end,
                struct io_failure_record **failrec_ret)
 {
+       struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
        struct io_failure_record *failrec;
        struct extent_map *em;
        struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
@@ -2234,8 +2236,9 @@ int btrfs_get_io_failure_record(struct inode *inode, u64 start, u64 end,
                                                 em->compress_type);
                }
 
-               pr_debug("Get IO Failure Record: (new) logical=%llu, start=%llu, len=%llu\n",
-                        logical, start, failrec->len);
+               btrfs_debug(fs_info,
+                       "Get IO Failure Record: (new) logical=%llu, start=%llu, len=%llu",
+                       logical, start, failrec->len);
 
                failrec->logical = logical;
                free_extent_map(em);
@@ -2253,9 +2256,10 @@ int btrfs_get_io_failure_record(struct inode *inode, u64 start, u64 end,
                        return ret;
                }
        } else {
-               pr_debug("Get IO Failure Record: (found) logical=%llu, start=%llu, len=%llu, validation=%d\n",
-                        failrec->logical, failrec->start, failrec->len,
-                        failrec->in_validation);
+               btrfs_debug(fs_info,
+                       "Get IO Failure Record: (found) logical=%llu, start=%llu, len=%llu, validation=%d",
+                       failrec->logical, failrec->start, failrec->len,
+                       failrec->in_validation);
                /*
                 * when data can be on disk more than twice, add to failrec here
                 * (e.g. with a list for failed_mirror) to make
@@ -2271,18 +2275,19 @@ int btrfs_get_io_failure_record(struct inode *inode, u64 start, u64 end,
 int btrfs_check_repairable(struct inode *inode, struct bio *failed_bio,
                           struct io_failure_record *failrec, int failed_mirror)
 {
+       struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
        int num_copies;
 
-       num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
-                                     failrec->logical, failrec->len);
+       num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
        if (num_copies == 1) {
                /*
                 * we only have a single copy of the data, so don't bother with
                 * all the retry and error correction code that follows. no
                 * matter what the error is, it is very likely to persist.
                 */
-               pr_debug("Check Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n",
-                        num_copies, failrec->this_mirror, failed_mirror);
+               btrfs_debug(fs_info,
+                       "Check Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
+                       num_copies, failrec->this_mirror, failed_mirror);
                return 0;
        }
 
@@ -2321,8 +2326,9 @@ int btrfs_check_repairable(struct inode *inode, struct bio *failed_bio,
        }
 
        if (failrec->this_mirror > num_copies) {
-               pr_debug("Check Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n",
-                        num_copies, failrec->this_mirror, failed_mirror);
+               btrfs_debug(fs_info,
+                       "Check Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
+                       num_copies, failrec->this_mirror, failed_mirror);
                return 0;
        }
 
@@ -2413,8 +2419,9 @@ static int bio_readpage_error(struct bio *failed_bio, u64 phy_offset,
        }
        bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
 
-       pr_debug("Repair Read Error: submitting new read[%#x] to this_mirror=%d, in_validation=%d\n",
-                read_mode, failrec->this_mirror, failrec->in_validation);
+       btrfs_debug(btrfs_sb(inode->i_sb),
+               "Repair Read Error: submitting new read[%#x] to this_mirror=%d, in_validation=%d",
+               read_mode, failrec->this_mirror, failrec->in_validation);
 
        ret = tree->ops->submit_bio_hook(inode, bio, failrec->this_mirror,
                                         failrec->bio_flags, 0);
@@ -2538,10 +2545,12 @@ static void end_bio_extent_readpage(struct bio *bio)
        bio_for_each_segment_all(bvec, bio, i) {
                struct page *page = bvec->bv_page;
                struct inode *inode = page->mapping->host;
+               struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
 
-               pr_debug("end_bio_extent_readpage: bi_sector=%llu, err=%d, mirror=%u\n",
-                        (u64)bio->bi_iter.bi_sector,
-                        bio->bi_error, io_bio->mirror_num);
+               btrfs_debug(fs_info,
+                       "end_bio_extent_readpage: bi_sector=%llu, err=%d, mirror=%u",
+                       (u64)bio->bi_iter.bi_sector, bio->bi_error,
+                       io_bio->mirror_num);
                tree = &BTRFS_I(inode)->io_tree;
 
                /* We always issue full-page reads, but if some block
@@ -2551,12 +2560,12 @@ static void end_bio_extent_readpage(struct bio *bio)
                 * if they don't add up to a full page.  */
                if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
                        if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
-                               btrfs_err(BTRFS_I(page->mapping->host)->root->fs_info,
-                                  "partial page read in btrfs with offset %u and length %u",
+                               btrfs_err(fs_info,
+                                       "partial page read in btrfs with offset %u and length %u",
                                        bvec->bv_offset, bvec->bv_len);
                        else
-                               btrfs_info(BTRFS_I(page->mapping->host)->root->fs_info,
-                                  "incomplete page read in btrfs with offset %u and length %u",
+                               btrfs_info(fs_info,
+                                       "incomplete page read in btrfs with offset %u and length %u",
                                        bvec->bv_offset, bvec->bv_len);
                }
 
@@ -3716,8 +3725,10 @@ static noinline_for_stack int write_one_eb(struct extent_buffer *eb,
        struct block_device *bdev = fs_info->fs_devices->latest_bdev;
        struct extent_io_tree *tree = &BTRFS_I(fs_info->btree_inode)->io_tree;
        u64 offset = eb->start;
+       u32 nritems;
        unsigned long i, num_pages;
        unsigned long bio_flags = 0;
+       unsigned long start, end;
        int write_flags = (epd->sync_io ? WRITE_SYNC : 0) | REQ_META;
        int ret = 0;
 
@@ -3727,15 +3738,21 @@ static noinline_for_stack int write_one_eb(struct extent_buffer *eb,
        if (btrfs_header_owner(eb) == BTRFS_TREE_LOG_OBJECTID)
                bio_flags = EXTENT_BIO_TREE_LOG;
 
-       /* set btree node beyond nritems with 0 to avoid stale content */
+       /* set btree blocks beyond nritems with 0 to avoid stale content. */
+       nritems = btrfs_header_nritems(eb);
        if (btrfs_header_level(eb) > 0) {
-               u32 nritems;
-               unsigned long end;
-
-               nritems = btrfs_header_nritems(eb);
                end = btrfs_node_key_ptr_offset(nritems);
 
                memset_extent_buffer(eb, 0, end, eb->len - end);
+       } else {
+               /*
+                * leaf:
+                * header 0 1 2 .. N ... data_N .. data_2 data_1 data_0
+                */
+               start = btrfs_item_nr_offset(nritems);
+               end = btrfs_leaf_data(eb) +
+                     leaf_data_end(fs_info->tree_root, eb);
+               memset_extent_buffer(eb, 0, start, end - start);
        }
 
        for (i = 0; i < num_pages; i++) {
@@ -5541,17 +5558,45 @@ void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
        }
 }
 
-/*
- * The extent buffer bitmap operations are done with byte granularity because
- * bitmap items are not guaranteed to be aligned to a word and therefore a
- * single word in a bitmap may straddle two pages in the extent buffer.
- */
-#define BIT_BYTE(nr) ((nr) / BITS_PER_BYTE)
-#define BYTE_MASK ((1 << BITS_PER_BYTE) - 1)
-#define BITMAP_FIRST_BYTE_MASK(start) \
-       ((BYTE_MASK << ((start) & (BITS_PER_BYTE - 1))) & BYTE_MASK)
-#define BITMAP_LAST_BYTE_MASK(nbits) \
-       (BYTE_MASK >> (-(nbits) & (BITS_PER_BYTE - 1)))
+void le_bitmap_set(u8 *map, unsigned int start, int len)
+{
+       u8 *p = map + BIT_BYTE(start);
+       const unsigned int size = start + len;
+       int bits_to_set = BITS_PER_BYTE - (start % BITS_PER_BYTE);
+       u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(start);
+
+       while (len - bits_to_set >= 0) {
+               *p |= mask_to_set;
+               len -= bits_to_set;
+               bits_to_set = BITS_PER_BYTE;
+               mask_to_set = ~(u8)0;
+               p++;
+       }
+       if (len) {
+               mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
+               *p |= mask_to_set;
+       }
+}
+
+void le_bitmap_clear(u8 *map, unsigned int start, int len)
+{
+       u8 *p = map + BIT_BYTE(start);
+       const unsigned int size = start + len;
+       int bits_to_clear = BITS_PER_BYTE - (start % BITS_PER_BYTE);
+       u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(start);
+
+       while (len - bits_to_clear >= 0) {
+               *p &= ~mask_to_clear;
+               len -= bits_to_clear;
+               bits_to_clear = BITS_PER_BYTE;
+               mask_to_clear = ~(u8)0;
+               p++;
+       }
+       if (len) {
+               mask_to_clear &= BITMAP_LAST_BYTE_MASK(size);
+               *p &= ~mask_to_clear;
+       }
+}
 
 /*
  * eb_bitmap_offset() - calculate the page and offset of the byte containing the
@@ -5595,7 +5640,7 @@ static inline void eb_bitmap_offset(struct extent_buffer *eb,
 int extent_buffer_test_bit(struct extent_buffer *eb, unsigned long start,
                           unsigned long nr)
 {
-       char *kaddr;
+       u8 *kaddr;
        struct page *page;
        unsigned long i;
        size_t offset;
@@ -5617,13 +5662,13 @@ int extent_buffer_test_bit(struct extent_buffer *eb, unsigned long start,
 void extent_buffer_bitmap_set(struct extent_buffer *eb, unsigned long start,
                              unsigned long pos, unsigned long len)
 {
-       char *kaddr;
+       u8 *kaddr;
        struct page *page;
        unsigned long i;
        size_t offset;
        const unsigned int size = pos + len;
        int bits_to_set = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
-       unsigned int mask_to_set = BITMAP_FIRST_BYTE_MASK(pos);
+       u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(pos);
 
        eb_bitmap_offset(eb, start, pos, &i, &offset);
        page = eb->pages[i];
@@ -5634,7 +5679,7 @@ void extent_buffer_bitmap_set(struct extent_buffer *eb, unsigned long start,
                kaddr[offset] |= mask_to_set;
                len -= bits_to_set;
                bits_to_set = BITS_PER_BYTE;
-               mask_to_set = ~0U;
+               mask_to_set = ~(u8)0;
                if (++offset >= PAGE_SIZE && len > 0) {
                        offset = 0;
                        page = eb->pages[++i];
@@ -5659,13 +5704,13 @@ void extent_buffer_bitmap_set(struct extent_buffer *eb, unsigned long start,
 void extent_buffer_bitmap_clear(struct extent_buffer *eb, unsigned long start,
                                unsigned long pos, unsigned long len)
 {
-       char *kaddr;
+       u8 *kaddr;
        struct page *page;
        unsigned long i;
        size_t offset;
        const unsigned int size = pos + len;
        int bits_to_clear = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
-       unsigned int mask_to_clear = BITMAP_FIRST_BYTE_MASK(pos);
+       u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(pos);
 
        eb_bitmap_offset(eb, start, pos, &i, &offset);
        page = eb->pages[i];
@@ -5676,7 +5721,7 @@ void extent_buffer_bitmap_clear(struct extent_buffer *eb, unsigned long start,
                kaddr[offset] &= ~mask_to_clear;
                len -= bits_to_clear;
                bits_to_clear = BITS_PER_BYTE;
-               mask_to_clear = ~0U;
+               mask_to_clear = ~(u8)0;
                if (++offset >= PAGE_SIZE && len > 0) {
                        offset = 0;
                        page = eb->pages[++i];