Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 STRATO. 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 | |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 19 | #include <linux/blkdev.h> |
Jan Schmidt | 558540c | 2011-06-13 19:59:12 +0200 | [diff] [blame] | 20 | #include <linux/ratelimit.h> |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 21 | #include "ctree.h" |
| 22 | #include "volumes.h" |
| 23 | #include "disk-io.h" |
| 24 | #include "ordered-data.h" |
Jan Schmidt | 0ef8e45 | 2011-06-13 20:04:15 +0200 | [diff] [blame] | 25 | #include "transaction.h" |
Jan Schmidt | 558540c | 2011-06-13 19:59:12 +0200 | [diff] [blame] | 26 | #include "backref.h" |
Jan Schmidt | 5da6fcb | 2011-08-04 18:11:04 +0200 | [diff] [blame^] | 27 | #include "extent_io.h" |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 28 | |
| 29 | /* |
| 30 | * This is only the first step towards a full-features scrub. It reads all |
| 31 | * extent and super block and verifies the checksums. In case a bad checksum |
| 32 | * is found or the extent cannot be read, good data will be written back if |
| 33 | * any can be found. |
| 34 | * |
| 35 | * Future enhancements: |
| 36 | * - To enhance the performance, better read-ahead strategies for the |
| 37 | * extent-tree can be employed. |
| 38 | * - In case an unrepairable extent is encountered, track which files are |
| 39 | * affected and report them |
| 40 | * - In case of a read error on files with nodatasum, map the file and read |
| 41 | * the extent to trigger a writeback of the good copy |
| 42 | * - track and record media errors, throw out bad devices |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 43 | * - add a mode to also read unallocated space |
| 44 | * - make the prefetch cancellable |
| 45 | */ |
| 46 | |
| 47 | struct scrub_bio; |
| 48 | struct scrub_page; |
| 49 | struct scrub_dev; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 50 | static void scrub_bio_end_io(struct bio *bio, int err); |
| 51 | static void scrub_checksum(struct btrfs_work *work); |
| 52 | static int scrub_checksum_data(struct scrub_dev *sdev, |
| 53 | struct scrub_page *spag, void *buffer); |
| 54 | static int scrub_checksum_tree_block(struct scrub_dev *sdev, |
| 55 | struct scrub_page *spag, u64 logical, |
| 56 | void *buffer); |
| 57 | static int scrub_checksum_super(struct scrub_bio *sbio, void *buffer); |
Ilya Dryomov | 96e3692 | 2011-04-09 14:27:01 +0300 | [diff] [blame] | 58 | static int scrub_fixup_check(struct scrub_bio *sbio, int ix); |
| 59 | static void scrub_fixup_end_io(struct bio *bio, int err); |
| 60 | static int scrub_fixup_io(int rw, struct block_device *bdev, sector_t sector, |
| 61 | struct page *page); |
| 62 | static void scrub_fixup(struct scrub_bio *sbio, int ix); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 63 | |
| 64 | #define SCRUB_PAGES_PER_BIO 16 /* 64k per bio */ |
| 65 | #define SCRUB_BIOS_PER_DEV 16 /* 1 MB per device in flight */ |
| 66 | |
| 67 | struct scrub_page { |
| 68 | u64 flags; /* extent flags */ |
| 69 | u64 generation; |
Jan Schmidt | e12fa9c | 2011-06-17 15:55:21 +0200 | [diff] [blame] | 70 | int mirror_num; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 71 | int have_csum; |
| 72 | u8 csum[BTRFS_CSUM_SIZE]; |
| 73 | }; |
| 74 | |
| 75 | struct scrub_bio { |
| 76 | int index; |
| 77 | struct scrub_dev *sdev; |
| 78 | struct bio *bio; |
| 79 | int err; |
| 80 | u64 logical; |
| 81 | u64 physical; |
| 82 | struct scrub_page spag[SCRUB_PAGES_PER_BIO]; |
| 83 | u64 count; |
| 84 | int next_free; |
| 85 | struct btrfs_work work; |
| 86 | }; |
| 87 | |
| 88 | struct scrub_dev { |
| 89 | struct scrub_bio *bios[SCRUB_BIOS_PER_DEV]; |
| 90 | struct btrfs_device *dev; |
| 91 | int first_free; |
| 92 | int curr; |
| 93 | atomic_t in_flight; |
Jan Schmidt | 0ef8e45 | 2011-06-13 20:04:15 +0200 | [diff] [blame] | 94 | atomic_t fixup_cnt; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 95 | spinlock_t list_lock; |
| 96 | wait_queue_head_t list_wait; |
| 97 | u16 csum_size; |
| 98 | struct list_head csum_list; |
| 99 | atomic_t cancel_req; |
Arne Jansen | 8628764 | 2011-03-23 16:34:19 +0100 | [diff] [blame] | 100 | int readonly; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 101 | /* |
| 102 | * statistics |
| 103 | */ |
| 104 | struct btrfs_scrub_progress stat; |
| 105 | spinlock_t stat_lock; |
| 106 | }; |
| 107 | |
Jan Schmidt | 0ef8e45 | 2011-06-13 20:04:15 +0200 | [diff] [blame] | 108 | struct scrub_fixup_nodatasum { |
| 109 | struct scrub_dev *sdev; |
| 110 | u64 logical; |
| 111 | struct btrfs_root *root; |
| 112 | struct btrfs_work work; |
| 113 | int mirror_num; |
| 114 | }; |
| 115 | |
Jan Schmidt | 558540c | 2011-06-13 19:59:12 +0200 | [diff] [blame] | 116 | struct scrub_warning { |
| 117 | struct btrfs_path *path; |
| 118 | u64 extent_item_size; |
| 119 | char *scratch_buf; |
| 120 | char *msg_buf; |
| 121 | const char *errstr; |
| 122 | sector_t sector; |
| 123 | u64 logical; |
| 124 | struct btrfs_device *dev; |
| 125 | int msg_bufsize; |
| 126 | int scratch_bufsize; |
| 127 | }; |
| 128 | |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 129 | static void scrub_free_csums(struct scrub_dev *sdev) |
| 130 | { |
| 131 | while (!list_empty(&sdev->csum_list)) { |
| 132 | struct btrfs_ordered_sum *sum; |
| 133 | sum = list_first_entry(&sdev->csum_list, |
| 134 | struct btrfs_ordered_sum, list); |
| 135 | list_del(&sum->list); |
| 136 | kfree(sum); |
| 137 | } |
| 138 | } |
| 139 | |
Arne Jansen | 1bc8779 | 2011-05-28 21:57:55 +0200 | [diff] [blame] | 140 | static void scrub_free_bio(struct bio *bio) |
| 141 | { |
| 142 | int i; |
| 143 | struct page *last_page = NULL; |
| 144 | |
| 145 | if (!bio) |
| 146 | return; |
| 147 | |
| 148 | for (i = 0; i < bio->bi_vcnt; ++i) { |
| 149 | if (bio->bi_io_vec[i].bv_page == last_page) |
| 150 | continue; |
| 151 | last_page = bio->bi_io_vec[i].bv_page; |
| 152 | __free_page(last_page); |
| 153 | } |
| 154 | bio_put(bio); |
| 155 | } |
| 156 | |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 157 | static noinline_for_stack void scrub_free_dev(struct scrub_dev *sdev) |
| 158 | { |
| 159 | int i; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 160 | |
| 161 | if (!sdev) |
| 162 | return; |
| 163 | |
| 164 | for (i = 0; i < SCRUB_BIOS_PER_DEV; ++i) { |
| 165 | struct scrub_bio *sbio = sdev->bios[i]; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 166 | |
| 167 | if (!sbio) |
| 168 | break; |
| 169 | |
Arne Jansen | 1bc8779 | 2011-05-28 21:57:55 +0200 | [diff] [blame] | 170 | scrub_free_bio(sbio->bio); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 171 | kfree(sbio); |
| 172 | } |
| 173 | |
| 174 | scrub_free_csums(sdev); |
| 175 | kfree(sdev); |
| 176 | } |
| 177 | |
| 178 | static noinline_for_stack |
| 179 | struct scrub_dev *scrub_setup_dev(struct btrfs_device *dev) |
| 180 | { |
| 181 | struct scrub_dev *sdev; |
| 182 | int i; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 183 | struct btrfs_fs_info *fs_info = dev->dev_root->fs_info; |
| 184 | |
| 185 | sdev = kzalloc(sizeof(*sdev), GFP_NOFS); |
| 186 | if (!sdev) |
| 187 | goto nomem; |
| 188 | sdev->dev = dev; |
| 189 | for (i = 0; i < SCRUB_BIOS_PER_DEV; ++i) { |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 190 | struct scrub_bio *sbio; |
| 191 | |
| 192 | sbio = kzalloc(sizeof(*sbio), GFP_NOFS); |
| 193 | if (!sbio) |
| 194 | goto nomem; |
| 195 | sdev->bios[i] = sbio; |
| 196 | |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 197 | sbio->index = i; |
| 198 | sbio->sdev = sdev; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 199 | sbio->count = 0; |
| 200 | sbio->work.func = scrub_checksum; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 201 | |
| 202 | if (i != SCRUB_BIOS_PER_DEV-1) |
| 203 | sdev->bios[i]->next_free = i + 1; |
Jan Schmidt | 0ef8e45 | 2011-06-13 20:04:15 +0200 | [diff] [blame] | 204 | else |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 205 | sdev->bios[i]->next_free = -1; |
| 206 | } |
| 207 | sdev->first_free = 0; |
| 208 | sdev->curr = -1; |
| 209 | atomic_set(&sdev->in_flight, 0); |
Jan Schmidt | 0ef8e45 | 2011-06-13 20:04:15 +0200 | [diff] [blame] | 210 | atomic_set(&sdev->fixup_cnt, 0); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 211 | atomic_set(&sdev->cancel_req, 0); |
| 212 | sdev->csum_size = btrfs_super_csum_size(&fs_info->super_copy); |
| 213 | INIT_LIST_HEAD(&sdev->csum_list); |
| 214 | |
| 215 | spin_lock_init(&sdev->list_lock); |
| 216 | spin_lock_init(&sdev->stat_lock); |
| 217 | init_waitqueue_head(&sdev->list_wait); |
| 218 | return sdev; |
| 219 | |
| 220 | nomem: |
| 221 | scrub_free_dev(sdev); |
| 222 | return ERR_PTR(-ENOMEM); |
| 223 | } |
| 224 | |
Jan Schmidt | 558540c | 2011-06-13 19:59:12 +0200 | [diff] [blame] | 225 | static int scrub_print_warning_inode(u64 inum, u64 offset, u64 root, void *ctx) |
| 226 | { |
| 227 | u64 isize; |
| 228 | u32 nlink; |
| 229 | int ret; |
| 230 | int i; |
| 231 | struct extent_buffer *eb; |
| 232 | struct btrfs_inode_item *inode_item; |
| 233 | struct scrub_warning *swarn = ctx; |
| 234 | struct btrfs_fs_info *fs_info = swarn->dev->dev_root->fs_info; |
| 235 | struct inode_fs_paths *ipath = NULL; |
| 236 | struct btrfs_root *local_root; |
| 237 | struct btrfs_key root_key; |
| 238 | |
| 239 | root_key.objectid = root; |
| 240 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 241 | root_key.offset = (u64)-1; |
| 242 | local_root = btrfs_read_fs_root_no_name(fs_info, &root_key); |
| 243 | if (IS_ERR(local_root)) { |
| 244 | ret = PTR_ERR(local_root); |
| 245 | goto err; |
| 246 | } |
| 247 | |
| 248 | ret = inode_item_info(inum, 0, local_root, swarn->path); |
| 249 | if (ret) { |
| 250 | btrfs_release_path(swarn->path); |
| 251 | goto err; |
| 252 | } |
| 253 | |
| 254 | eb = swarn->path->nodes[0]; |
| 255 | inode_item = btrfs_item_ptr(eb, swarn->path->slots[0], |
| 256 | struct btrfs_inode_item); |
| 257 | isize = btrfs_inode_size(eb, inode_item); |
| 258 | nlink = btrfs_inode_nlink(eb, inode_item); |
| 259 | btrfs_release_path(swarn->path); |
| 260 | |
| 261 | ipath = init_ipath(4096, local_root, swarn->path); |
| 262 | ret = paths_from_inode(inum, ipath); |
| 263 | |
| 264 | if (ret < 0) |
| 265 | goto err; |
| 266 | |
| 267 | /* |
| 268 | * we deliberately ignore the bit ipath might have been too small to |
| 269 | * hold all of the paths here |
| 270 | */ |
| 271 | for (i = 0; i < ipath->fspath->elem_cnt; ++i) |
| 272 | printk(KERN_WARNING "btrfs: %s at logical %llu on dev " |
| 273 | "%s, sector %llu, root %llu, inode %llu, offset %llu, " |
| 274 | "length %llu, links %u (path: %s)\n", swarn->errstr, |
| 275 | swarn->logical, swarn->dev->name, |
| 276 | (unsigned long long)swarn->sector, root, inum, offset, |
| 277 | min(isize - offset, (u64)PAGE_SIZE), nlink, |
| 278 | ipath->fspath->str[i]); |
| 279 | |
| 280 | free_ipath(ipath); |
| 281 | return 0; |
| 282 | |
| 283 | err: |
| 284 | printk(KERN_WARNING "btrfs: %s at logical %llu on dev " |
| 285 | "%s, sector %llu, root %llu, inode %llu, offset %llu: path " |
| 286 | "resolving failed with ret=%d\n", swarn->errstr, |
| 287 | swarn->logical, swarn->dev->name, |
| 288 | (unsigned long long)swarn->sector, root, inum, offset, ret); |
| 289 | |
| 290 | free_ipath(ipath); |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | static void scrub_print_warning(const char *errstr, struct scrub_bio *sbio, |
| 295 | int ix) |
| 296 | { |
| 297 | struct btrfs_device *dev = sbio->sdev->dev; |
| 298 | struct btrfs_fs_info *fs_info = dev->dev_root->fs_info; |
| 299 | struct btrfs_path *path; |
| 300 | struct btrfs_key found_key; |
| 301 | struct extent_buffer *eb; |
| 302 | struct btrfs_extent_item *ei; |
| 303 | struct scrub_warning swarn; |
| 304 | u32 item_size; |
| 305 | int ret; |
| 306 | u64 ref_root; |
| 307 | u8 ref_level; |
| 308 | unsigned long ptr = 0; |
| 309 | const int bufsize = 4096; |
| 310 | u64 extent_offset; |
| 311 | |
| 312 | path = btrfs_alloc_path(); |
| 313 | |
| 314 | swarn.scratch_buf = kmalloc(bufsize, GFP_NOFS); |
| 315 | swarn.msg_buf = kmalloc(bufsize, GFP_NOFS); |
| 316 | swarn.sector = (sbio->physical + ix * PAGE_SIZE) >> 9; |
| 317 | swarn.logical = sbio->logical + ix * PAGE_SIZE; |
| 318 | swarn.errstr = errstr; |
| 319 | swarn.dev = dev; |
| 320 | swarn.msg_bufsize = bufsize; |
| 321 | swarn.scratch_bufsize = bufsize; |
| 322 | |
| 323 | if (!path || !swarn.scratch_buf || !swarn.msg_buf) |
| 324 | goto out; |
| 325 | |
| 326 | ret = extent_from_logical(fs_info, swarn.logical, path, &found_key); |
| 327 | if (ret < 0) |
| 328 | goto out; |
| 329 | |
| 330 | extent_offset = swarn.logical - found_key.objectid; |
| 331 | swarn.extent_item_size = found_key.offset; |
| 332 | |
| 333 | eb = path->nodes[0]; |
| 334 | ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item); |
| 335 | item_size = btrfs_item_size_nr(eb, path->slots[0]); |
| 336 | |
| 337 | if (ret & BTRFS_EXTENT_FLAG_TREE_BLOCK) { |
| 338 | do { |
| 339 | ret = tree_backref_for_extent(&ptr, eb, ei, item_size, |
| 340 | &ref_root, &ref_level); |
| 341 | printk(KERN_WARNING "%s at logical %llu on dev %s, " |
| 342 | "sector %llu: metadata %s (level %d) in tree " |
| 343 | "%llu\n", errstr, swarn.logical, dev->name, |
| 344 | (unsigned long long)swarn.sector, |
| 345 | ref_level ? "node" : "leaf", |
| 346 | ret < 0 ? -1 : ref_level, |
| 347 | ret < 0 ? -1 : ref_root); |
| 348 | } while (ret != 1); |
| 349 | } else { |
| 350 | swarn.path = path; |
| 351 | iterate_extent_inodes(fs_info, path, found_key.objectid, |
| 352 | extent_offset, |
| 353 | scrub_print_warning_inode, &swarn); |
| 354 | } |
| 355 | |
| 356 | out: |
| 357 | btrfs_free_path(path); |
| 358 | kfree(swarn.scratch_buf); |
| 359 | kfree(swarn.msg_buf); |
| 360 | } |
| 361 | |
Jan Schmidt | 0ef8e45 | 2011-06-13 20:04:15 +0200 | [diff] [blame] | 362 | static int scrub_fixup_readpage(u64 inum, u64 offset, u64 root, void *ctx) |
| 363 | { |
Jan Schmidt | 5da6fcb | 2011-08-04 18:11:04 +0200 | [diff] [blame^] | 364 | struct page *page = NULL; |
Jan Schmidt | 0ef8e45 | 2011-06-13 20:04:15 +0200 | [diff] [blame] | 365 | unsigned long index; |
| 366 | struct scrub_fixup_nodatasum *fixup = ctx; |
| 367 | int ret; |
Jan Schmidt | 5da6fcb | 2011-08-04 18:11:04 +0200 | [diff] [blame^] | 368 | int corrected = 0; |
Jan Schmidt | 0ef8e45 | 2011-06-13 20:04:15 +0200 | [diff] [blame] | 369 | struct btrfs_key key; |
Jan Schmidt | 5da6fcb | 2011-08-04 18:11:04 +0200 | [diff] [blame^] | 370 | struct inode *inode = NULL; |
Jan Schmidt | 0ef8e45 | 2011-06-13 20:04:15 +0200 | [diff] [blame] | 371 | u64 end = offset + PAGE_SIZE - 1; |
| 372 | struct btrfs_root *local_root; |
| 373 | |
| 374 | key.objectid = root; |
| 375 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 376 | key.offset = (u64)-1; |
| 377 | local_root = btrfs_read_fs_root_no_name(fixup->root->fs_info, &key); |
| 378 | if (IS_ERR(local_root)) |
| 379 | return PTR_ERR(local_root); |
| 380 | |
| 381 | key.type = BTRFS_INODE_ITEM_KEY; |
| 382 | key.objectid = inum; |
| 383 | key.offset = 0; |
| 384 | inode = btrfs_iget(fixup->root->fs_info->sb, &key, local_root, NULL); |
| 385 | if (IS_ERR(inode)) |
| 386 | return PTR_ERR(inode); |
| 387 | |
Jan Schmidt | 0ef8e45 | 2011-06-13 20:04:15 +0200 | [diff] [blame] | 388 | index = offset >> PAGE_CACHE_SHIFT; |
| 389 | |
| 390 | page = find_or_create_page(inode->i_mapping, index, GFP_NOFS); |
Jan Schmidt | 5da6fcb | 2011-08-04 18:11:04 +0200 | [diff] [blame^] | 391 | if (!page) { |
| 392 | ret = -ENOMEM; |
| 393 | goto out; |
| 394 | } |
Jan Schmidt | 0ef8e45 | 2011-06-13 20:04:15 +0200 | [diff] [blame] | 395 | |
Jan Schmidt | 5da6fcb | 2011-08-04 18:11:04 +0200 | [diff] [blame^] | 396 | if (PageUptodate(page)) { |
| 397 | struct btrfs_mapping_tree *map_tree; |
| 398 | if (PageDirty(page)) { |
| 399 | /* |
| 400 | * we need to write the data to the defect sector. the |
| 401 | * data that was in that sector is not in memory, |
| 402 | * because the page was modified. we must not write the |
| 403 | * modified page to that sector. |
| 404 | * |
| 405 | * TODO: what could be done here: wait for the delalloc |
| 406 | * runner to write out that page (might involve |
| 407 | * COW) and see whether the sector is still |
| 408 | * referenced afterwards. |
| 409 | * |
| 410 | * For the meantime, we'll treat this error |
| 411 | * incorrectable, although there is a chance that a |
| 412 | * later scrub will find the bad sector again and that |
| 413 | * there's no dirty page in memory, then. |
| 414 | */ |
| 415 | ret = -EIO; |
| 416 | goto out; |
| 417 | } |
| 418 | map_tree = &BTRFS_I(inode)->root->fs_info->mapping_tree; |
| 419 | ret = repair_io_failure(map_tree, offset, PAGE_SIZE, |
| 420 | fixup->logical, page, |
| 421 | fixup->mirror_num); |
| 422 | unlock_page(page); |
| 423 | corrected = !ret; |
| 424 | } else { |
| 425 | /* |
| 426 | * we need to get good data first. the general readpage path |
| 427 | * will call repair_io_failure for us, we just have to make |
| 428 | * sure we read the bad mirror. |
| 429 | */ |
| 430 | ret = set_extent_bits(&BTRFS_I(inode)->io_tree, offset, end, |
| 431 | EXTENT_DAMAGED, GFP_NOFS); |
| 432 | if (ret) { |
| 433 | /* set_extent_bits should give proper error */ |
| 434 | WARN_ON(ret > 0); |
| 435 | if (ret > 0) |
| 436 | ret = -EFAULT; |
| 437 | goto out; |
| 438 | } |
Jan Schmidt | 0ef8e45 | 2011-06-13 20:04:15 +0200 | [diff] [blame] | 439 | |
Jan Schmidt | 5da6fcb | 2011-08-04 18:11:04 +0200 | [diff] [blame^] | 440 | ret = extent_read_full_page(&BTRFS_I(inode)->io_tree, page, |
| 441 | btrfs_get_extent, |
| 442 | fixup->mirror_num); |
| 443 | wait_on_page_locked(page); |
Jan Schmidt | 0ef8e45 | 2011-06-13 20:04:15 +0200 | [diff] [blame] | 444 | |
Jan Schmidt | 5da6fcb | 2011-08-04 18:11:04 +0200 | [diff] [blame^] | 445 | corrected = !test_range_bit(&BTRFS_I(inode)->io_tree, offset, |
| 446 | end, EXTENT_DAMAGED, 0, NULL); |
| 447 | if (!corrected) |
| 448 | clear_extent_bits(&BTRFS_I(inode)->io_tree, offset, end, |
| 449 | EXTENT_DAMAGED, GFP_NOFS); |
| 450 | } |
| 451 | |
| 452 | out: |
| 453 | if (page) |
| 454 | put_page(page); |
| 455 | if (inode) |
| 456 | iput(inode); |
Jan Schmidt | 0ef8e45 | 2011-06-13 20:04:15 +0200 | [diff] [blame] | 457 | |
| 458 | if (ret < 0) |
| 459 | return ret; |
| 460 | |
| 461 | if (ret == 0 && corrected) { |
| 462 | /* |
| 463 | * we only need to call readpage for one of the inodes belonging |
| 464 | * to this extent. so make iterate_extent_inodes stop |
| 465 | */ |
| 466 | return 1; |
| 467 | } |
| 468 | |
| 469 | return -EIO; |
| 470 | } |
| 471 | |
| 472 | static void scrub_fixup_nodatasum(struct btrfs_work *work) |
| 473 | { |
| 474 | int ret; |
| 475 | struct scrub_fixup_nodatasum *fixup; |
| 476 | struct scrub_dev *sdev; |
| 477 | struct btrfs_trans_handle *trans = NULL; |
| 478 | struct btrfs_fs_info *fs_info; |
| 479 | struct btrfs_path *path; |
| 480 | int uncorrectable = 0; |
| 481 | |
| 482 | fixup = container_of(work, struct scrub_fixup_nodatasum, work); |
| 483 | sdev = fixup->sdev; |
| 484 | fs_info = fixup->root->fs_info; |
| 485 | |
| 486 | path = btrfs_alloc_path(); |
| 487 | if (!path) { |
| 488 | spin_lock(&sdev->stat_lock); |
| 489 | ++sdev->stat.malloc_errors; |
| 490 | spin_unlock(&sdev->stat_lock); |
| 491 | uncorrectable = 1; |
| 492 | goto out; |
| 493 | } |
| 494 | |
| 495 | trans = btrfs_join_transaction(fixup->root); |
| 496 | if (IS_ERR(trans)) { |
| 497 | uncorrectable = 1; |
| 498 | goto out; |
| 499 | } |
| 500 | |
| 501 | /* |
| 502 | * the idea is to trigger a regular read through the standard path. we |
| 503 | * read a page from the (failed) logical address by specifying the |
| 504 | * corresponding copynum of the failed sector. thus, that readpage is |
| 505 | * expected to fail. |
| 506 | * that is the point where on-the-fly error correction will kick in |
| 507 | * (once it's finished) and rewrite the failed sector if a good copy |
| 508 | * can be found. |
| 509 | */ |
| 510 | ret = iterate_inodes_from_logical(fixup->logical, fixup->root->fs_info, |
| 511 | path, scrub_fixup_readpage, |
| 512 | fixup); |
| 513 | if (ret < 0) { |
| 514 | uncorrectable = 1; |
| 515 | goto out; |
| 516 | } |
| 517 | WARN_ON(ret != 1); |
| 518 | |
| 519 | spin_lock(&sdev->stat_lock); |
| 520 | ++sdev->stat.corrected_errors; |
| 521 | spin_unlock(&sdev->stat_lock); |
| 522 | |
| 523 | out: |
| 524 | if (trans && !IS_ERR(trans)) |
| 525 | btrfs_end_transaction(trans, fixup->root); |
| 526 | if (uncorrectable) { |
| 527 | spin_lock(&sdev->stat_lock); |
| 528 | ++sdev->stat.uncorrectable_errors; |
| 529 | spin_unlock(&sdev->stat_lock); |
| 530 | printk_ratelimited(KERN_ERR "btrfs: unable to fixup " |
| 531 | "(nodatasum) error at logical %llu\n", |
| 532 | fixup->logical); |
| 533 | } |
| 534 | |
| 535 | btrfs_free_path(path); |
| 536 | kfree(fixup); |
| 537 | |
| 538 | /* see caller why we're pretending to be paused in the scrub counters */ |
| 539 | mutex_lock(&fs_info->scrub_lock); |
| 540 | atomic_dec(&fs_info->scrubs_running); |
| 541 | atomic_dec(&fs_info->scrubs_paused); |
| 542 | mutex_unlock(&fs_info->scrub_lock); |
| 543 | atomic_dec(&sdev->fixup_cnt); |
| 544 | wake_up(&fs_info->scrub_pause_wait); |
| 545 | wake_up(&sdev->list_wait); |
| 546 | } |
| 547 | |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 548 | /* |
| 549 | * scrub_recheck_error gets called when either verification of the page |
| 550 | * failed or the bio failed to read, e.g. with EIO. In the latter case, |
| 551 | * recheck_error gets called for every page in the bio, even though only |
| 552 | * one may be bad |
| 553 | */ |
Jan Schmidt | 13db62b | 2011-06-13 19:56:13 +0200 | [diff] [blame] | 554 | static int scrub_recheck_error(struct scrub_bio *sbio, int ix) |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 555 | { |
Jan Schmidt | 13db62b | 2011-06-13 19:56:13 +0200 | [diff] [blame] | 556 | struct scrub_dev *sdev = sbio->sdev; |
| 557 | u64 sector = (sbio->physical + ix * PAGE_SIZE) >> 9; |
Jan Schmidt | 558540c | 2011-06-13 19:59:12 +0200 | [diff] [blame] | 558 | static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL, |
| 559 | DEFAULT_RATELIMIT_BURST); |
Jan Schmidt | 13db62b | 2011-06-13 19:56:13 +0200 | [diff] [blame] | 560 | |
Ilya Dryomov | 96e3692 | 2011-04-09 14:27:01 +0300 | [diff] [blame] | 561 | if (sbio->err) { |
Jan Schmidt | 13db62b | 2011-06-13 19:56:13 +0200 | [diff] [blame] | 562 | if (scrub_fixup_io(READ, sbio->sdev->dev->bdev, sector, |
Ilya Dryomov | 96e3692 | 2011-04-09 14:27:01 +0300 | [diff] [blame] | 563 | sbio->bio->bi_io_vec[ix].bv_page) == 0) { |
| 564 | if (scrub_fixup_check(sbio, ix) == 0) |
Jan Schmidt | 13db62b | 2011-06-13 19:56:13 +0200 | [diff] [blame] | 565 | return 0; |
Ilya Dryomov | 96e3692 | 2011-04-09 14:27:01 +0300 | [diff] [blame] | 566 | } |
Jan Schmidt | 558540c | 2011-06-13 19:59:12 +0200 | [diff] [blame] | 567 | if (__ratelimit(&_rs)) |
| 568 | scrub_print_warning("i/o error", sbio, ix); |
| 569 | } else { |
| 570 | if (__ratelimit(&_rs)) |
| 571 | scrub_print_warning("checksum error", sbio, ix); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 572 | } |
| 573 | |
Jan Schmidt | 13db62b | 2011-06-13 19:56:13 +0200 | [diff] [blame] | 574 | spin_lock(&sdev->stat_lock); |
| 575 | ++sdev->stat.read_errors; |
| 576 | spin_unlock(&sdev->stat_lock); |
| 577 | |
Ilya Dryomov | 96e3692 | 2011-04-09 14:27:01 +0300 | [diff] [blame] | 578 | scrub_fixup(sbio, ix); |
Jan Schmidt | 13db62b | 2011-06-13 19:56:13 +0200 | [diff] [blame] | 579 | return 1; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 580 | } |
| 581 | |
Ilya Dryomov | 96e3692 | 2011-04-09 14:27:01 +0300 | [diff] [blame] | 582 | static int scrub_fixup_check(struct scrub_bio *sbio, int ix) |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 583 | { |
| 584 | int ret = 1; |
| 585 | struct page *page; |
| 586 | void *buffer; |
Ilya Dryomov | 96e3692 | 2011-04-09 14:27:01 +0300 | [diff] [blame] | 587 | u64 flags = sbio->spag[ix].flags; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 588 | |
Ilya Dryomov | 96e3692 | 2011-04-09 14:27:01 +0300 | [diff] [blame] | 589 | page = sbio->bio->bi_io_vec[ix].bv_page; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 590 | buffer = kmap_atomic(page, KM_USER0); |
| 591 | if (flags & BTRFS_EXTENT_FLAG_DATA) { |
Ilya Dryomov | 96e3692 | 2011-04-09 14:27:01 +0300 | [diff] [blame] | 592 | ret = scrub_checksum_data(sbio->sdev, |
| 593 | sbio->spag + ix, buffer); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 594 | } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { |
Ilya Dryomov | 96e3692 | 2011-04-09 14:27:01 +0300 | [diff] [blame] | 595 | ret = scrub_checksum_tree_block(sbio->sdev, |
| 596 | sbio->spag + ix, |
| 597 | sbio->logical + ix * PAGE_SIZE, |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 598 | buffer); |
| 599 | } else { |
| 600 | WARN_ON(1); |
| 601 | } |
| 602 | kunmap_atomic(buffer, KM_USER0); |
| 603 | |
| 604 | return ret; |
| 605 | } |
| 606 | |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 607 | static void scrub_fixup_end_io(struct bio *bio, int err) |
| 608 | { |
| 609 | complete((struct completion *)bio->bi_private); |
| 610 | } |
| 611 | |
Ilya Dryomov | 96e3692 | 2011-04-09 14:27:01 +0300 | [diff] [blame] | 612 | static void scrub_fixup(struct scrub_bio *sbio, int ix) |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 613 | { |
Ilya Dryomov | 96e3692 | 2011-04-09 14:27:01 +0300 | [diff] [blame] | 614 | struct scrub_dev *sdev = sbio->sdev; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 615 | struct btrfs_fs_info *fs_info = sdev->dev->dev_root->fs_info; |
| 616 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 617 | struct btrfs_bio *bbio = NULL; |
Jan Schmidt | 0ef8e45 | 2011-06-13 20:04:15 +0200 | [diff] [blame] | 618 | struct scrub_fixup_nodatasum *fixup; |
Ilya Dryomov | 96e3692 | 2011-04-09 14:27:01 +0300 | [diff] [blame] | 619 | u64 logical = sbio->logical + ix * PAGE_SIZE; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 620 | u64 length; |
| 621 | int i; |
| 622 | int ret; |
| 623 | DECLARE_COMPLETION_ONSTACK(complete); |
| 624 | |
Ilya Dryomov | 96e3692 | 2011-04-09 14:27:01 +0300 | [diff] [blame] | 625 | if ((sbio->spag[ix].flags & BTRFS_EXTENT_FLAG_DATA) && |
| 626 | (sbio->spag[ix].have_csum == 0)) { |
Jan Schmidt | 0ef8e45 | 2011-06-13 20:04:15 +0200 | [diff] [blame] | 627 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); |
| 628 | if (!fixup) |
| 629 | goto uncorrectable; |
| 630 | fixup->sdev = sdev; |
| 631 | fixup->logical = logical; |
| 632 | fixup->root = fs_info->extent_root; |
| 633 | fixup->mirror_num = sbio->spag[ix].mirror_num; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 634 | /* |
Jan Schmidt | 0ef8e45 | 2011-06-13 20:04:15 +0200 | [diff] [blame] | 635 | * increment scrubs_running to prevent cancel requests from |
| 636 | * completing as long as a fixup worker is running. we must also |
| 637 | * increment scrubs_paused to prevent deadlocking on pause |
| 638 | * requests used for transactions commits (as the worker uses a |
| 639 | * transaction context). it is safe to regard the fixup worker |
| 640 | * as paused for all matters practical. effectively, we only |
| 641 | * avoid cancellation requests from completing. |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 642 | */ |
Jan Schmidt | 0ef8e45 | 2011-06-13 20:04:15 +0200 | [diff] [blame] | 643 | mutex_lock(&fs_info->scrub_lock); |
| 644 | atomic_inc(&fs_info->scrubs_running); |
| 645 | atomic_inc(&fs_info->scrubs_paused); |
| 646 | mutex_unlock(&fs_info->scrub_lock); |
| 647 | atomic_inc(&sdev->fixup_cnt); |
| 648 | fixup->work.func = scrub_fixup_nodatasum; |
| 649 | btrfs_queue_worker(&fs_info->scrub_workers, &fixup->work); |
| 650 | return; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | length = PAGE_SIZE; |
Ilya Dryomov | 96e3692 | 2011-04-09 14:27:01 +0300 | [diff] [blame] | 654 | ret = btrfs_map_block(map_tree, REQ_WRITE, logical, &length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 655 | &bbio, 0); |
| 656 | if (ret || !bbio || length < PAGE_SIZE) { |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 657 | printk(KERN_ERR |
| 658 | "scrub_fixup: btrfs_map_block failed us for %llu\n", |
Ilya Dryomov | 96e3692 | 2011-04-09 14:27:01 +0300 | [diff] [blame] | 659 | (unsigned long long)logical); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 660 | WARN_ON(1); |
| 661 | return; |
| 662 | } |
| 663 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 664 | if (bbio->num_stripes == 1) |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 665 | /* there aren't any replicas */ |
| 666 | goto uncorrectable; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 667 | |
| 668 | /* |
| 669 | * first find a good copy |
| 670 | */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 671 | for (i = 0; i < bbio->num_stripes; ++i) { |
Jan Schmidt | 193ea74 | 2011-06-13 19:56:54 +0200 | [diff] [blame] | 672 | if (i + 1 == sbio->spag[ix].mirror_num) |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 673 | continue; |
| 674 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 675 | if (scrub_fixup_io(READ, bbio->stripes[i].dev->bdev, |
| 676 | bbio->stripes[i].physical >> 9, |
Ilya Dryomov | 96e3692 | 2011-04-09 14:27:01 +0300 | [diff] [blame] | 677 | sbio->bio->bi_io_vec[ix].bv_page)) { |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 678 | /* I/O-error, this is not a good copy */ |
| 679 | continue; |
Ilya Dryomov | 96e3692 | 2011-04-09 14:27:01 +0300 | [diff] [blame] | 680 | } |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 681 | |
Ilya Dryomov | 96e3692 | 2011-04-09 14:27:01 +0300 | [diff] [blame] | 682 | if (scrub_fixup_check(sbio, ix) == 0) |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 683 | break; |
| 684 | } |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 685 | if (i == bbio->num_stripes) |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 686 | goto uncorrectable; |
| 687 | |
Arne Jansen | 8628764 | 2011-03-23 16:34:19 +0100 | [diff] [blame] | 688 | if (!sdev->readonly) { |
| 689 | /* |
| 690 | * bi_io_vec[ix].bv_page now contains good data, write it back |
| 691 | */ |
| 692 | if (scrub_fixup_io(WRITE, sdev->dev->bdev, |
| 693 | (sbio->physical + ix * PAGE_SIZE) >> 9, |
| 694 | sbio->bio->bi_io_vec[ix].bv_page)) { |
| 695 | /* I/O-error, writeback failed, give up */ |
| 696 | goto uncorrectable; |
| 697 | } |
Ilya Dryomov | 96e3692 | 2011-04-09 14:27:01 +0300 | [diff] [blame] | 698 | } |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 699 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 700 | kfree(bbio); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 701 | spin_lock(&sdev->stat_lock); |
| 702 | ++sdev->stat.corrected_errors; |
| 703 | spin_unlock(&sdev->stat_lock); |
| 704 | |
Jan Schmidt | 558540c | 2011-06-13 19:59:12 +0200 | [diff] [blame] | 705 | printk_ratelimited(KERN_ERR "btrfs: fixed up error at logical %llu\n", |
| 706 | (unsigned long long)logical); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 707 | return; |
| 708 | |
| 709 | uncorrectable: |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 710 | kfree(bbio); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 711 | spin_lock(&sdev->stat_lock); |
| 712 | ++sdev->stat.uncorrectable_errors; |
| 713 | spin_unlock(&sdev->stat_lock); |
| 714 | |
Jan Schmidt | 558540c | 2011-06-13 19:59:12 +0200 | [diff] [blame] | 715 | printk_ratelimited(KERN_ERR "btrfs: unable to fixup (regular) error at " |
| 716 | "logical %llu\n", (unsigned long long)logical); |
Ilya Dryomov | 96e3692 | 2011-04-09 14:27:01 +0300 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | static int scrub_fixup_io(int rw, struct block_device *bdev, sector_t sector, |
| 720 | struct page *page) |
| 721 | { |
| 722 | struct bio *bio = NULL; |
| 723 | int ret; |
| 724 | DECLARE_COMPLETION_ONSTACK(complete); |
| 725 | |
Ilya Dryomov | 96e3692 | 2011-04-09 14:27:01 +0300 | [diff] [blame] | 726 | bio = bio_alloc(GFP_NOFS, 1); |
| 727 | bio->bi_bdev = bdev; |
| 728 | bio->bi_sector = sector; |
| 729 | bio_add_page(bio, page, PAGE_SIZE, 0); |
| 730 | bio->bi_end_io = scrub_fixup_end_io; |
| 731 | bio->bi_private = &complete; |
| 732 | submit_bio(rw, bio); |
| 733 | |
Arne Jansen | e7786c3 | 2011-05-28 20:58:38 +0000 | [diff] [blame] | 734 | /* this will also unplug the queue */ |
Ilya Dryomov | 96e3692 | 2011-04-09 14:27:01 +0300 | [diff] [blame] | 735 | wait_for_completion(&complete); |
| 736 | |
| 737 | ret = !test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 738 | bio_put(bio); |
| 739 | return ret; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | static void scrub_bio_end_io(struct bio *bio, int err) |
| 743 | { |
| 744 | struct scrub_bio *sbio = bio->bi_private; |
| 745 | struct scrub_dev *sdev = sbio->sdev; |
| 746 | struct btrfs_fs_info *fs_info = sdev->dev->dev_root->fs_info; |
| 747 | |
| 748 | sbio->err = err; |
Arne Jansen | 1bc8779 | 2011-05-28 21:57:55 +0200 | [diff] [blame] | 749 | sbio->bio = bio; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 750 | |
| 751 | btrfs_queue_worker(&fs_info->scrub_workers, &sbio->work); |
| 752 | } |
| 753 | |
| 754 | static void scrub_checksum(struct btrfs_work *work) |
| 755 | { |
| 756 | struct scrub_bio *sbio = container_of(work, struct scrub_bio, work); |
| 757 | struct scrub_dev *sdev = sbio->sdev; |
| 758 | struct page *page; |
| 759 | void *buffer; |
| 760 | int i; |
| 761 | u64 flags; |
| 762 | u64 logical; |
| 763 | int ret; |
| 764 | |
| 765 | if (sbio->err) { |
Jan Schmidt | 13db62b | 2011-06-13 19:56:13 +0200 | [diff] [blame] | 766 | ret = 0; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 767 | for (i = 0; i < sbio->count; ++i) |
Jan Schmidt | 13db62b | 2011-06-13 19:56:13 +0200 | [diff] [blame] | 768 | ret |= scrub_recheck_error(sbio, i); |
| 769 | if (!ret) { |
| 770 | spin_lock(&sdev->stat_lock); |
| 771 | ++sdev->stat.unverified_errors; |
| 772 | spin_unlock(&sdev->stat_lock); |
| 773 | } |
Ilya Dryomov | 96e3692 | 2011-04-09 14:27:01 +0300 | [diff] [blame] | 774 | |
| 775 | sbio->bio->bi_flags &= ~(BIO_POOL_MASK - 1); |
| 776 | sbio->bio->bi_flags |= 1 << BIO_UPTODATE; |
| 777 | sbio->bio->bi_phys_segments = 0; |
| 778 | sbio->bio->bi_idx = 0; |
| 779 | |
| 780 | for (i = 0; i < sbio->count; i++) { |
| 781 | struct bio_vec *bi; |
| 782 | bi = &sbio->bio->bi_io_vec[i]; |
| 783 | bi->bv_offset = 0; |
| 784 | bi->bv_len = PAGE_SIZE; |
| 785 | } |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 786 | goto out; |
| 787 | } |
| 788 | for (i = 0; i < sbio->count; ++i) { |
| 789 | page = sbio->bio->bi_io_vec[i].bv_page; |
| 790 | buffer = kmap_atomic(page, KM_USER0); |
| 791 | flags = sbio->spag[i].flags; |
| 792 | logical = sbio->logical + i * PAGE_SIZE; |
| 793 | ret = 0; |
| 794 | if (flags & BTRFS_EXTENT_FLAG_DATA) { |
| 795 | ret = scrub_checksum_data(sdev, sbio->spag + i, buffer); |
| 796 | } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { |
| 797 | ret = scrub_checksum_tree_block(sdev, sbio->spag + i, |
| 798 | logical, buffer); |
| 799 | } else if (flags & BTRFS_EXTENT_FLAG_SUPER) { |
| 800 | BUG_ON(i); |
| 801 | (void)scrub_checksum_super(sbio, buffer); |
| 802 | } else { |
| 803 | WARN_ON(1); |
| 804 | } |
| 805 | kunmap_atomic(buffer, KM_USER0); |
Jan Schmidt | 13db62b | 2011-06-13 19:56:13 +0200 | [diff] [blame] | 806 | if (ret) { |
| 807 | ret = scrub_recheck_error(sbio, i); |
| 808 | if (!ret) { |
| 809 | spin_lock(&sdev->stat_lock); |
| 810 | ++sdev->stat.unverified_errors; |
| 811 | spin_unlock(&sdev->stat_lock); |
| 812 | } |
| 813 | } |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | out: |
Arne Jansen | 1bc8779 | 2011-05-28 21:57:55 +0200 | [diff] [blame] | 817 | scrub_free_bio(sbio->bio); |
| 818 | sbio->bio = NULL; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 819 | spin_lock(&sdev->list_lock); |
| 820 | sbio->next_free = sdev->first_free; |
| 821 | sdev->first_free = sbio->index; |
| 822 | spin_unlock(&sdev->list_lock); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 823 | atomic_dec(&sdev->in_flight); |
| 824 | wake_up(&sdev->list_wait); |
| 825 | } |
| 826 | |
| 827 | static int scrub_checksum_data(struct scrub_dev *sdev, |
| 828 | struct scrub_page *spag, void *buffer) |
| 829 | { |
| 830 | u8 csum[BTRFS_CSUM_SIZE]; |
| 831 | u32 crc = ~(u32)0; |
| 832 | int fail = 0; |
| 833 | struct btrfs_root *root = sdev->dev->dev_root; |
| 834 | |
| 835 | if (!spag->have_csum) |
| 836 | return 0; |
| 837 | |
| 838 | crc = btrfs_csum_data(root, buffer, crc, PAGE_SIZE); |
| 839 | btrfs_csum_final(crc, csum); |
| 840 | if (memcmp(csum, spag->csum, sdev->csum_size)) |
| 841 | fail = 1; |
| 842 | |
| 843 | spin_lock(&sdev->stat_lock); |
| 844 | ++sdev->stat.data_extents_scrubbed; |
| 845 | sdev->stat.data_bytes_scrubbed += PAGE_SIZE; |
| 846 | if (fail) |
| 847 | ++sdev->stat.csum_errors; |
| 848 | spin_unlock(&sdev->stat_lock); |
| 849 | |
| 850 | return fail; |
| 851 | } |
| 852 | |
| 853 | static int scrub_checksum_tree_block(struct scrub_dev *sdev, |
| 854 | struct scrub_page *spag, u64 logical, |
| 855 | void *buffer) |
| 856 | { |
| 857 | struct btrfs_header *h; |
| 858 | struct btrfs_root *root = sdev->dev->dev_root; |
| 859 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 860 | u8 csum[BTRFS_CSUM_SIZE]; |
| 861 | u32 crc = ~(u32)0; |
| 862 | int fail = 0; |
| 863 | int crc_fail = 0; |
| 864 | |
| 865 | /* |
| 866 | * we don't use the getter functions here, as we |
| 867 | * a) don't have an extent buffer and |
| 868 | * b) the page is already kmapped |
| 869 | */ |
| 870 | h = (struct btrfs_header *)buffer; |
| 871 | |
| 872 | if (logical != le64_to_cpu(h->bytenr)) |
| 873 | ++fail; |
| 874 | |
| 875 | if (spag->generation != le64_to_cpu(h->generation)) |
| 876 | ++fail; |
| 877 | |
| 878 | if (memcmp(h->fsid, fs_info->fsid, BTRFS_UUID_SIZE)) |
| 879 | ++fail; |
| 880 | |
| 881 | if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid, |
| 882 | BTRFS_UUID_SIZE)) |
| 883 | ++fail; |
| 884 | |
| 885 | crc = btrfs_csum_data(root, buffer + BTRFS_CSUM_SIZE, crc, |
| 886 | PAGE_SIZE - BTRFS_CSUM_SIZE); |
| 887 | btrfs_csum_final(crc, csum); |
| 888 | if (memcmp(csum, h->csum, sdev->csum_size)) |
| 889 | ++crc_fail; |
| 890 | |
| 891 | spin_lock(&sdev->stat_lock); |
| 892 | ++sdev->stat.tree_extents_scrubbed; |
| 893 | sdev->stat.tree_bytes_scrubbed += PAGE_SIZE; |
| 894 | if (crc_fail) |
| 895 | ++sdev->stat.csum_errors; |
| 896 | if (fail) |
| 897 | ++sdev->stat.verify_errors; |
| 898 | spin_unlock(&sdev->stat_lock); |
| 899 | |
| 900 | return fail || crc_fail; |
| 901 | } |
| 902 | |
| 903 | static int scrub_checksum_super(struct scrub_bio *sbio, void *buffer) |
| 904 | { |
| 905 | struct btrfs_super_block *s; |
| 906 | u64 logical; |
| 907 | struct scrub_dev *sdev = sbio->sdev; |
| 908 | struct btrfs_root *root = sdev->dev->dev_root; |
| 909 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 910 | u8 csum[BTRFS_CSUM_SIZE]; |
| 911 | u32 crc = ~(u32)0; |
| 912 | int fail = 0; |
| 913 | |
| 914 | s = (struct btrfs_super_block *)buffer; |
| 915 | logical = sbio->logical; |
| 916 | |
| 917 | if (logical != le64_to_cpu(s->bytenr)) |
| 918 | ++fail; |
| 919 | |
| 920 | if (sbio->spag[0].generation != le64_to_cpu(s->generation)) |
| 921 | ++fail; |
| 922 | |
| 923 | if (memcmp(s->fsid, fs_info->fsid, BTRFS_UUID_SIZE)) |
| 924 | ++fail; |
| 925 | |
| 926 | crc = btrfs_csum_data(root, buffer + BTRFS_CSUM_SIZE, crc, |
| 927 | PAGE_SIZE - BTRFS_CSUM_SIZE); |
| 928 | btrfs_csum_final(crc, csum); |
| 929 | if (memcmp(csum, s->csum, sbio->sdev->csum_size)) |
| 930 | ++fail; |
| 931 | |
| 932 | if (fail) { |
| 933 | /* |
| 934 | * if we find an error in a super block, we just report it. |
| 935 | * They will get written with the next transaction commit |
| 936 | * anyway |
| 937 | */ |
| 938 | spin_lock(&sdev->stat_lock); |
| 939 | ++sdev->stat.super_errors; |
| 940 | spin_unlock(&sdev->stat_lock); |
| 941 | } |
| 942 | |
| 943 | return fail; |
| 944 | } |
| 945 | |
| 946 | static int scrub_submit(struct scrub_dev *sdev) |
| 947 | { |
| 948 | struct scrub_bio *sbio; |
Arne Jansen | 1bc8779 | 2011-05-28 21:57:55 +0200 | [diff] [blame] | 949 | struct bio *bio; |
| 950 | int i; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 951 | |
| 952 | if (sdev->curr == -1) |
| 953 | return 0; |
| 954 | |
| 955 | sbio = sdev->bios[sdev->curr]; |
| 956 | |
Arne Jansen | 1bc8779 | 2011-05-28 21:57:55 +0200 | [diff] [blame] | 957 | bio = bio_alloc(GFP_NOFS, sbio->count); |
| 958 | if (!bio) |
| 959 | goto nomem; |
| 960 | |
| 961 | bio->bi_private = sbio; |
| 962 | bio->bi_end_io = scrub_bio_end_io; |
| 963 | bio->bi_bdev = sdev->dev->bdev; |
| 964 | bio->bi_sector = sbio->physical >> 9; |
| 965 | |
| 966 | for (i = 0; i < sbio->count; ++i) { |
| 967 | struct page *page; |
| 968 | int ret; |
| 969 | |
| 970 | page = alloc_page(GFP_NOFS); |
| 971 | if (!page) |
| 972 | goto nomem; |
| 973 | |
| 974 | ret = bio_add_page(bio, page, PAGE_SIZE, 0); |
| 975 | if (!ret) { |
| 976 | __free_page(page); |
| 977 | goto nomem; |
| 978 | } |
| 979 | } |
| 980 | |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 981 | sbio->err = 0; |
| 982 | sdev->curr = -1; |
| 983 | atomic_inc(&sdev->in_flight); |
| 984 | |
Arne Jansen | 1bc8779 | 2011-05-28 21:57:55 +0200 | [diff] [blame] | 985 | submit_bio(READ, bio); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 986 | |
| 987 | return 0; |
Arne Jansen | 1bc8779 | 2011-05-28 21:57:55 +0200 | [diff] [blame] | 988 | |
| 989 | nomem: |
| 990 | scrub_free_bio(bio); |
| 991 | |
| 992 | return -ENOMEM; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 993 | } |
| 994 | |
| 995 | static int scrub_page(struct scrub_dev *sdev, u64 logical, u64 len, |
Jan Schmidt | e12fa9c | 2011-06-17 15:55:21 +0200 | [diff] [blame] | 996 | u64 physical, u64 flags, u64 gen, int mirror_num, |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 997 | u8 *csum, int force) |
| 998 | { |
| 999 | struct scrub_bio *sbio; |
| 1000 | |
| 1001 | again: |
| 1002 | /* |
| 1003 | * grab a fresh bio or wait for one to become available |
| 1004 | */ |
| 1005 | while (sdev->curr == -1) { |
| 1006 | spin_lock(&sdev->list_lock); |
| 1007 | sdev->curr = sdev->first_free; |
| 1008 | if (sdev->curr != -1) { |
| 1009 | sdev->first_free = sdev->bios[sdev->curr]->next_free; |
| 1010 | sdev->bios[sdev->curr]->next_free = -1; |
| 1011 | sdev->bios[sdev->curr]->count = 0; |
| 1012 | spin_unlock(&sdev->list_lock); |
| 1013 | } else { |
| 1014 | spin_unlock(&sdev->list_lock); |
| 1015 | wait_event(sdev->list_wait, sdev->first_free != -1); |
| 1016 | } |
| 1017 | } |
| 1018 | sbio = sdev->bios[sdev->curr]; |
| 1019 | if (sbio->count == 0) { |
| 1020 | sbio->physical = physical; |
| 1021 | sbio->logical = logical; |
Arne Jansen | 00d01bc | 2011-05-25 12:22:50 +0000 | [diff] [blame] | 1022 | } else if (sbio->physical + sbio->count * PAGE_SIZE != physical || |
| 1023 | sbio->logical + sbio->count * PAGE_SIZE != logical) { |
Arne Jansen | 1bc8779 | 2011-05-28 21:57:55 +0200 | [diff] [blame] | 1024 | int ret; |
| 1025 | |
| 1026 | ret = scrub_submit(sdev); |
| 1027 | if (ret) |
| 1028 | return ret; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1029 | goto again; |
| 1030 | } |
| 1031 | sbio->spag[sbio->count].flags = flags; |
| 1032 | sbio->spag[sbio->count].generation = gen; |
| 1033 | sbio->spag[sbio->count].have_csum = 0; |
| 1034 | sbio->spag[sbio->count].mirror_num = mirror_num; |
| 1035 | if (csum) { |
| 1036 | sbio->spag[sbio->count].have_csum = 1; |
| 1037 | memcpy(sbio->spag[sbio->count].csum, csum, sdev->csum_size); |
| 1038 | } |
| 1039 | ++sbio->count; |
Arne Jansen | 1bc8779 | 2011-05-28 21:57:55 +0200 | [diff] [blame] | 1040 | if (sbio->count == SCRUB_PAGES_PER_BIO || force) { |
| 1041 | int ret; |
| 1042 | |
| 1043 | ret = scrub_submit(sdev); |
| 1044 | if (ret) |
| 1045 | return ret; |
| 1046 | } |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1047 | |
| 1048 | return 0; |
| 1049 | } |
| 1050 | |
| 1051 | static int scrub_find_csum(struct scrub_dev *sdev, u64 logical, u64 len, |
| 1052 | u8 *csum) |
| 1053 | { |
| 1054 | struct btrfs_ordered_sum *sum = NULL; |
| 1055 | int ret = 0; |
| 1056 | unsigned long i; |
| 1057 | unsigned long num_sectors; |
| 1058 | u32 sectorsize = sdev->dev->dev_root->sectorsize; |
| 1059 | |
| 1060 | while (!list_empty(&sdev->csum_list)) { |
| 1061 | sum = list_first_entry(&sdev->csum_list, |
| 1062 | struct btrfs_ordered_sum, list); |
| 1063 | if (sum->bytenr > logical) |
| 1064 | return 0; |
| 1065 | if (sum->bytenr + sum->len > logical) |
| 1066 | break; |
| 1067 | |
| 1068 | ++sdev->stat.csum_discards; |
| 1069 | list_del(&sum->list); |
| 1070 | kfree(sum); |
| 1071 | sum = NULL; |
| 1072 | } |
| 1073 | if (!sum) |
| 1074 | return 0; |
| 1075 | |
| 1076 | num_sectors = sum->len / sectorsize; |
| 1077 | for (i = 0; i < num_sectors; ++i) { |
| 1078 | if (sum->sums[i].bytenr == logical) { |
| 1079 | memcpy(csum, &sum->sums[i].sum, sdev->csum_size); |
| 1080 | ret = 1; |
| 1081 | break; |
| 1082 | } |
| 1083 | } |
| 1084 | if (ret && i == num_sectors - 1) { |
| 1085 | list_del(&sum->list); |
| 1086 | kfree(sum); |
| 1087 | } |
| 1088 | return ret; |
| 1089 | } |
| 1090 | |
| 1091 | /* scrub extent tries to collect up to 64 kB for each bio */ |
| 1092 | static int scrub_extent(struct scrub_dev *sdev, u64 logical, u64 len, |
Jan Schmidt | e12fa9c | 2011-06-17 15:55:21 +0200 | [diff] [blame] | 1093 | u64 physical, u64 flags, u64 gen, int mirror_num) |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1094 | { |
| 1095 | int ret; |
| 1096 | u8 csum[BTRFS_CSUM_SIZE]; |
| 1097 | |
| 1098 | while (len) { |
| 1099 | u64 l = min_t(u64, len, PAGE_SIZE); |
| 1100 | int have_csum = 0; |
| 1101 | |
| 1102 | if (flags & BTRFS_EXTENT_FLAG_DATA) { |
| 1103 | /* push csums to sbio */ |
| 1104 | have_csum = scrub_find_csum(sdev, logical, l, csum); |
| 1105 | if (have_csum == 0) |
| 1106 | ++sdev->stat.no_csum; |
| 1107 | } |
| 1108 | ret = scrub_page(sdev, logical, l, physical, flags, gen, |
| 1109 | mirror_num, have_csum ? csum : NULL, 0); |
| 1110 | if (ret) |
| 1111 | return ret; |
| 1112 | len -= l; |
| 1113 | logical += l; |
| 1114 | physical += l; |
| 1115 | } |
| 1116 | return 0; |
| 1117 | } |
| 1118 | |
| 1119 | static noinline_for_stack int scrub_stripe(struct scrub_dev *sdev, |
| 1120 | struct map_lookup *map, int num, u64 base, u64 length) |
| 1121 | { |
| 1122 | struct btrfs_path *path; |
| 1123 | struct btrfs_fs_info *fs_info = sdev->dev->dev_root->fs_info; |
| 1124 | struct btrfs_root *root = fs_info->extent_root; |
| 1125 | struct btrfs_root *csum_root = fs_info->csum_root; |
| 1126 | struct btrfs_extent_item *extent; |
Arne Jansen | e7786c3 | 2011-05-28 20:58:38 +0000 | [diff] [blame] | 1127 | struct blk_plug plug; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1128 | u64 flags; |
| 1129 | int ret; |
| 1130 | int slot; |
| 1131 | int i; |
| 1132 | u64 nstripes; |
| 1133 | int start_stripe; |
| 1134 | struct extent_buffer *l; |
| 1135 | struct btrfs_key key; |
| 1136 | u64 physical; |
| 1137 | u64 logical; |
| 1138 | u64 generation; |
Jan Schmidt | e12fa9c | 2011-06-17 15:55:21 +0200 | [diff] [blame] | 1139 | int mirror_num; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1140 | |
| 1141 | u64 increment = map->stripe_len; |
| 1142 | u64 offset; |
| 1143 | |
| 1144 | nstripes = length; |
| 1145 | offset = 0; |
| 1146 | do_div(nstripes, map->stripe_len); |
| 1147 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
| 1148 | offset = map->stripe_len * num; |
| 1149 | increment = map->stripe_len * map->num_stripes; |
Jan Schmidt | 193ea74 | 2011-06-13 19:56:54 +0200 | [diff] [blame] | 1150 | mirror_num = 1; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1151 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
| 1152 | int factor = map->num_stripes / map->sub_stripes; |
| 1153 | offset = map->stripe_len * (num / map->sub_stripes); |
| 1154 | increment = map->stripe_len * factor; |
Jan Schmidt | 193ea74 | 2011-06-13 19:56:54 +0200 | [diff] [blame] | 1155 | mirror_num = num % map->sub_stripes + 1; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1156 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) { |
| 1157 | increment = map->stripe_len; |
Jan Schmidt | 193ea74 | 2011-06-13 19:56:54 +0200 | [diff] [blame] | 1158 | mirror_num = num % map->num_stripes + 1; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1159 | } else if (map->type & BTRFS_BLOCK_GROUP_DUP) { |
| 1160 | increment = map->stripe_len; |
Jan Schmidt | 193ea74 | 2011-06-13 19:56:54 +0200 | [diff] [blame] | 1161 | mirror_num = num % map->num_stripes + 1; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1162 | } else { |
| 1163 | increment = map->stripe_len; |
Jan Schmidt | 193ea74 | 2011-06-13 19:56:54 +0200 | [diff] [blame] | 1164 | mirror_num = 1; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1165 | } |
| 1166 | |
| 1167 | path = btrfs_alloc_path(); |
| 1168 | if (!path) |
| 1169 | return -ENOMEM; |
| 1170 | |
| 1171 | path->reada = 2; |
| 1172 | path->search_commit_root = 1; |
| 1173 | path->skip_locking = 1; |
| 1174 | |
| 1175 | /* |
| 1176 | * find all extents for each stripe and just read them to get |
| 1177 | * them into the page cache |
| 1178 | * FIXME: we can do better. build a more intelligent prefetching |
| 1179 | */ |
| 1180 | logical = base + offset; |
| 1181 | physical = map->stripes[num].physical; |
| 1182 | ret = 0; |
| 1183 | for (i = 0; i < nstripes; ++i) { |
| 1184 | key.objectid = logical; |
| 1185 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1186 | key.offset = (u64)0; |
| 1187 | |
| 1188 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 1189 | if (ret < 0) |
Arne Jansen | 8c51032 | 2011-06-03 10:09:26 +0200 | [diff] [blame] | 1190 | goto out_noplug; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1191 | |
Arne Jansen | 8c51032 | 2011-06-03 10:09:26 +0200 | [diff] [blame] | 1192 | /* |
| 1193 | * we might miss half an extent here, but that doesn't matter, |
| 1194 | * as it's only the prefetch |
| 1195 | */ |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1196 | while (1) { |
| 1197 | l = path->nodes[0]; |
| 1198 | slot = path->slots[0]; |
| 1199 | if (slot >= btrfs_header_nritems(l)) { |
| 1200 | ret = btrfs_next_leaf(root, path); |
| 1201 | if (ret == 0) |
| 1202 | continue; |
| 1203 | if (ret < 0) |
Arne Jansen | 8c51032 | 2011-06-03 10:09:26 +0200 | [diff] [blame] | 1204 | goto out_noplug; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1205 | |
| 1206 | break; |
| 1207 | } |
| 1208 | btrfs_item_key_to_cpu(l, &key, slot); |
| 1209 | |
| 1210 | if (key.objectid >= logical + map->stripe_len) |
| 1211 | break; |
| 1212 | |
| 1213 | path->slots[0]++; |
| 1214 | } |
Chris Mason | 7126733 | 2011-05-23 06:30:52 -0400 | [diff] [blame] | 1215 | btrfs_release_path(path); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1216 | logical += increment; |
| 1217 | physical += map->stripe_len; |
| 1218 | cond_resched(); |
| 1219 | } |
| 1220 | |
| 1221 | /* |
| 1222 | * collect all data csums for the stripe to avoid seeking during |
| 1223 | * the scrub. This might currently (crc32) end up to be about 1MB |
| 1224 | */ |
| 1225 | start_stripe = 0; |
Arne Jansen | e7786c3 | 2011-05-28 20:58:38 +0000 | [diff] [blame] | 1226 | blk_start_plug(&plug); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1227 | again: |
| 1228 | logical = base + offset + start_stripe * increment; |
| 1229 | for (i = start_stripe; i < nstripes; ++i) { |
| 1230 | ret = btrfs_lookup_csums_range(csum_root, logical, |
| 1231 | logical + map->stripe_len - 1, |
| 1232 | &sdev->csum_list, 1); |
| 1233 | if (ret) |
| 1234 | goto out; |
| 1235 | |
| 1236 | logical += increment; |
| 1237 | cond_resched(); |
| 1238 | } |
| 1239 | /* |
| 1240 | * now find all extents for each stripe and scrub them |
| 1241 | */ |
| 1242 | logical = base + offset + start_stripe * increment; |
| 1243 | physical = map->stripes[num].physical + start_stripe * map->stripe_len; |
| 1244 | ret = 0; |
| 1245 | for (i = start_stripe; i < nstripes; ++i) { |
| 1246 | /* |
| 1247 | * canceled? |
| 1248 | */ |
| 1249 | if (atomic_read(&fs_info->scrub_cancel_req) || |
| 1250 | atomic_read(&sdev->cancel_req)) { |
| 1251 | ret = -ECANCELED; |
| 1252 | goto out; |
| 1253 | } |
| 1254 | /* |
| 1255 | * check to see if we have to pause |
| 1256 | */ |
| 1257 | if (atomic_read(&fs_info->scrub_pause_req)) { |
| 1258 | /* push queued extents */ |
| 1259 | scrub_submit(sdev); |
| 1260 | wait_event(sdev->list_wait, |
| 1261 | atomic_read(&sdev->in_flight) == 0); |
| 1262 | atomic_inc(&fs_info->scrubs_paused); |
| 1263 | wake_up(&fs_info->scrub_pause_wait); |
| 1264 | mutex_lock(&fs_info->scrub_lock); |
| 1265 | while (atomic_read(&fs_info->scrub_pause_req)) { |
| 1266 | mutex_unlock(&fs_info->scrub_lock); |
| 1267 | wait_event(fs_info->scrub_pause_wait, |
| 1268 | atomic_read(&fs_info->scrub_pause_req) == 0); |
| 1269 | mutex_lock(&fs_info->scrub_lock); |
| 1270 | } |
| 1271 | atomic_dec(&fs_info->scrubs_paused); |
| 1272 | mutex_unlock(&fs_info->scrub_lock); |
| 1273 | wake_up(&fs_info->scrub_pause_wait); |
| 1274 | scrub_free_csums(sdev); |
| 1275 | start_stripe = i; |
| 1276 | goto again; |
| 1277 | } |
| 1278 | |
| 1279 | key.objectid = logical; |
| 1280 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1281 | key.offset = (u64)0; |
| 1282 | |
| 1283 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 1284 | if (ret < 0) |
| 1285 | goto out; |
Arne Jansen | 8c51032 | 2011-06-03 10:09:26 +0200 | [diff] [blame] | 1286 | if (ret > 0) { |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1287 | ret = btrfs_previous_item(root, path, 0, |
| 1288 | BTRFS_EXTENT_ITEM_KEY); |
| 1289 | if (ret < 0) |
| 1290 | goto out; |
Arne Jansen | 8c51032 | 2011-06-03 10:09:26 +0200 | [diff] [blame] | 1291 | if (ret > 0) { |
| 1292 | /* there's no smaller item, so stick with the |
| 1293 | * larger one */ |
| 1294 | btrfs_release_path(path); |
| 1295 | ret = btrfs_search_slot(NULL, root, &key, |
| 1296 | path, 0, 0); |
| 1297 | if (ret < 0) |
| 1298 | goto out; |
| 1299 | } |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1300 | } |
| 1301 | |
| 1302 | while (1) { |
| 1303 | l = path->nodes[0]; |
| 1304 | slot = path->slots[0]; |
| 1305 | if (slot >= btrfs_header_nritems(l)) { |
| 1306 | ret = btrfs_next_leaf(root, path); |
| 1307 | if (ret == 0) |
| 1308 | continue; |
| 1309 | if (ret < 0) |
| 1310 | goto out; |
| 1311 | |
| 1312 | break; |
| 1313 | } |
| 1314 | btrfs_item_key_to_cpu(l, &key, slot); |
| 1315 | |
| 1316 | if (key.objectid + key.offset <= logical) |
| 1317 | goto next; |
| 1318 | |
| 1319 | if (key.objectid >= logical + map->stripe_len) |
| 1320 | break; |
| 1321 | |
| 1322 | if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) |
| 1323 | goto next; |
| 1324 | |
| 1325 | extent = btrfs_item_ptr(l, slot, |
| 1326 | struct btrfs_extent_item); |
| 1327 | flags = btrfs_extent_flags(l, extent); |
| 1328 | generation = btrfs_extent_generation(l, extent); |
| 1329 | |
| 1330 | if (key.objectid < logical && |
| 1331 | (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) { |
| 1332 | printk(KERN_ERR |
| 1333 | "btrfs scrub: tree block %llu spanning " |
| 1334 | "stripes, ignored. logical=%llu\n", |
| 1335 | (unsigned long long)key.objectid, |
| 1336 | (unsigned long long)logical); |
| 1337 | goto next; |
| 1338 | } |
| 1339 | |
| 1340 | /* |
| 1341 | * trim extent to this stripe |
| 1342 | */ |
| 1343 | if (key.objectid < logical) { |
| 1344 | key.offset -= logical - key.objectid; |
| 1345 | key.objectid = logical; |
| 1346 | } |
| 1347 | if (key.objectid + key.offset > |
| 1348 | logical + map->stripe_len) { |
| 1349 | key.offset = logical + map->stripe_len - |
| 1350 | key.objectid; |
| 1351 | } |
| 1352 | |
| 1353 | ret = scrub_extent(sdev, key.objectid, key.offset, |
| 1354 | key.objectid - logical + physical, |
| 1355 | flags, generation, mirror_num); |
| 1356 | if (ret) |
| 1357 | goto out; |
| 1358 | |
| 1359 | next: |
| 1360 | path->slots[0]++; |
| 1361 | } |
Chris Mason | 7126733 | 2011-05-23 06:30:52 -0400 | [diff] [blame] | 1362 | btrfs_release_path(path); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1363 | logical += increment; |
| 1364 | physical += map->stripe_len; |
| 1365 | spin_lock(&sdev->stat_lock); |
| 1366 | sdev->stat.last_physical = physical; |
| 1367 | spin_unlock(&sdev->stat_lock); |
| 1368 | } |
| 1369 | /* push queued extents */ |
| 1370 | scrub_submit(sdev); |
| 1371 | |
| 1372 | out: |
Arne Jansen | e7786c3 | 2011-05-28 20:58:38 +0000 | [diff] [blame] | 1373 | blk_finish_plug(&plug); |
Arne Jansen | 8c51032 | 2011-06-03 10:09:26 +0200 | [diff] [blame] | 1374 | out_noplug: |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1375 | btrfs_free_path(path); |
| 1376 | return ret < 0 ? ret : 0; |
| 1377 | } |
| 1378 | |
| 1379 | static noinline_for_stack int scrub_chunk(struct scrub_dev *sdev, |
| 1380 | u64 chunk_tree, u64 chunk_objectid, u64 chunk_offset, u64 length) |
| 1381 | { |
| 1382 | struct btrfs_mapping_tree *map_tree = |
| 1383 | &sdev->dev->dev_root->fs_info->mapping_tree; |
| 1384 | struct map_lookup *map; |
| 1385 | struct extent_map *em; |
| 1386 | int i; |
| 1387 | int ret = -EINVAL; |
| 1388 | |
| 1389 | read_lock(&map_tree->map_tree.lock); |
| 1390 | em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1); |
| 1391 | read_unlock(&map_tree->map_tree.lock); |
| 1392 | |
| 1393 | if (!em) |
| 1394 | return -EINVAL; |
| 1395 | |
| 1396 | map = (struct map_lookup *)em->bdev; |
| 1397 | if (em->start != chunk_offset) |
| 1398 | goto out; |
| 1399 | |
| 1400 | if (em->len < length) |
| 1401 | goto out; |
| 1402 | |
| 1403 | for (i = 0; i < map->num_stripes; ++i) { |
| 1404 | if (map->stripes[i].dev == sdev->dev) { |
| 1405 | ret = scrub_stripe(sdev, map, i, chunk_offset, length); |
| 1406 | if (ret) |
| 1407 | goto out; |
| 1408 | } |
| 1409 | } |
| 1410 | out: |
| 1411 | free_extent_map(em); |
| 1412 | |
| 1413 | return ret; |
| 1414 | } |
| 1415 | |
| 1416 | static noinline_for_stack |
| 1417 | int scrub_enumerate_chunks(struct scrub_dev *sdev, u64 start, u64 end) |
| 1418 | { |
| 1419 | struct btrfs_dev_extent *dev_extent = NULL; |
| 1420 | struct btrfs_path *path; |
| 1421 | struct btrfs_root *root = sdev->dev->dev_root; |
| 1422 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 1423 | u64 length; |
| 1424 | u64 chunk_tree; |
| 1425 | u64 chunk_objectid; |
| 1426 | u64 chunk_offset; |
| 1427 | int ret; |
| 1428 | int slot; |
| 1429 | struct extent_buffer *l; |
| 1430 | struct btrfs_key key; |
| 1431 | struct btrfs_key found_key; |
| 1432 | struct btrfs_block_group_cache *cache; |
| 1433 | |
| 1434 | path = btrfs_alloc_path(); |
| 1435 | if (!path) |
| 1436 | return -ENOMEM; |
| 1437 | |
| 1438 | path->reada = 2; |
| 1439 | path->search_commit_root = 1; |
| 1440 | path->skip_locking = 1; |
| 1441 | |
| 1442 | key.objectid = sdev->dev->devid; |
| 1443 | key.offset = 0ull; |
| 1444 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 1445 | |
| 1446 | |
| 1447 | while (1) { |
| 1448 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 1449 | if (ret < 0) |
Arne Jansen | 8c51032 | 2011-06-03 10:09:26 +0200 | [diff] [blame] | 1450 | break; |
| 1451 | if (ret > 0) { |
| 1452 | if (path->slots[0] >= |
| 1453 | btrfs_header_nritems(path->nodes[0])) { |
| 1454 | ret = btrfs_next_leaf(root, path); |
| 1455 | if (ret) |
| 1456 | break; |
| 1457 | } |
| 1458 | } |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1459 | |
| 1460 | l = path->nodes[0]; |
| 1461 | slot = path->slots[0]; |
| 1462 | |
| 1463 | btrfs_item_key_to_cpu(l, &found_key, slot); |
| 1464 | |
| 1465 | if (found_key.objectid != sdev->dev->devid) |
| 1466 | break; |
| 1467 | |
Arne Jansen | 8c51032 | 2011-06-03 10:09:26 +0200 | [diff] [blame] | 1468 | if (btrfs_key_type(&found_key) != BTRFS_DEV_EXTENT_KEY) |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1469 | break; |
| 1470 | |
| 1471 | if (found_key.offset >= end) |
| 1472 | break; |
| 1473 | |
| 1474 | if (found_key.offset < key.offset) |
| 1475 | break; |
| 1476 | |
| 1477 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
| 1478 | length = btrfs_dev_extent_length(l, dev_extent); |
| 1479 | |
| 1480 | if (found_key.offset + length <= start) { |
| 1481 | key.offset = found_key.offset + length; |
Chris Mason | 7126733 | 2011-05-23 06:30:52 -0400 | [diff] [blame] | 1482 | btrfs_release_path(path); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1483 | continue; |
| 1484 | } |
| 1485 | |
| 1486 | chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent); |
| 1487 | chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent); |
| 1488 | chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent); |
| 1489 | |
| 1490 | /* |
| 1491 | * get a reference on the corresponding block group to prevent |
| 1492 | * the chunk from going away while we scrub it |
| 1493 | */ |
| 1494 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); |
| 1495 | if (!cache) { |
| 1496 | ret = -ENOENT; |
Arne Jansen | 8c51032 | 2011-06-03 10:09:26 +0200 | [diff] [blame] | 1497 | break; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1498 | } |
| 1499 | ret = scrub_chunk(sdev, chunk_tree, chunk_objectid, |
| 1500 | chunk_offset, length); |
| 1501 | btrfs_put_block_group(cache); |
| 1502 | if (ret) |
| 1503 | break; |
| 1504 | |
| 1505 | key.offset = found_key.offset + length; |
Chris Mason | 7126733 | 2011-05-23 06:30:52 -0400 | [diff] [blame] | 1506 | btrfs_release_path(path); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1507 | } |
| 1508 | |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1509 | btrfs_free_path(path); |
Arne Jansen | 8c51032 | 2011-06-03 10:09:26 +0200 | [diff] [blame] | 1510 | |
| 1511 | /* |
| 1512 | * ret can still be 1 from search_slot or next_leaf, |
| 1513 | * that's not an error |
| 1514 | */ |
| 1515 | return ret < 0 ? ret : 0; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1516 | } |
| 1517 | |
| 1518 | static noinline_for_stack int scrub_supers(struct scrub_dev *sdev) |
| 1519 | { |
| 1520 | int i; |
| 1521 | u64 bytenr; |
| 1522 | u64 gen; |
| 1523 | int ret; |
| 1524 | struct btrfs_device *device = sdev->dev; |
| 1525 | struct btrfs_root *root = device->dev_root; |
| 1526 | |
| 1527 | gen = root->fs_info->last_trans_committed; |
| 1528 | |
| 1529 | for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { |
| 1530 | bytenr = btrfs_sb_offset(i); |
| 1531 | if (bytenr + BTRFS_SUPER_INFO_SIZE >= device->total_bytes) |
| 1532 | break; |
| 1533 | |
| 1534 | ret = scrub_page(sdev, bytenr, PAGE_SIZE, bytenr, |
| 1535 | BTRFS_EXTENT_FLAG_SUPER, gen, i, NULL, 1); |
| 1536 | if (ret) |
| 1537 | return ret; |
| 1538 | } |
| 1539 | wait_event(sdev->list_wait, atomic_read(&sdev->in_flight) == 0); |
| 1540 | |
| 1541 | return 0; |
| 1542 | } |
| 1543 | |
| 1544 | /* |
| 1545 | * get a reference count on fs_info->scrub_workers. start worker if necessary |
| 1546 | */ |
| 1547 | static noinline_for_stack int scrub_workers_get(struct btrfs_root *root) |
| 1548 | { |
| 1549 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 1550 | |
| 1551 | mutex_lock(&fs_info->scrub_lock); |
Arne Jansen | 632dd77 | 2011-06-10 12:07:07 +0200 | [diff] [blame] | 1552 | if (fs_info->scrub_workers_refcnt == 0) { |
| 1553 | btrfs_init_workers(&fs_info->scrub_workers, "scrub", |
| 1554 | fs_info->thread_pool_size, &fs_info->generic_worker); |
| 1555 | fs_info->scrub_workers.idle_thresh = 4; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1556 | btrfs_start_workers(&fs_info->scrub_workers, 1); |
Arne Jansen | 632dd77 | 2011-06-10 12:07:07 +0200 | [diff] [blame] | 1557 | } |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1558 | ++fs_info->scrub_workers_refcnt; |
| 1559 | mutex_unlock(&fs_info->scrub_lock); |
| 1560 | |
| 1561 | return 0; |
| 1562 | } |
| 1563 | |
| 1564 | static noinline_for_stack void scrub_workers_put(struct btrfs_root *root) |
| 1565 | { |
| 1566 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 1567 | |
| 1568 | mutex_lock(&fs_info->scrub_lock); |
| 1569 | if (--fs_info->scrub_workers_refcnt == 0) |
| 1570 | btrfs_stop_workers(&fs_info->scrub_workers); |
| 1571 | WARN_ON(fs_info->scrub_workers_refcnt < 0); |
| 1572 | mutex_unlock(&fs_info->scrub_lock); |
| 1573 | } |
| 1574 | |
| 1575 | |
| 1576 | int btrfs_scrub_dev(struct btrfs_root *root, u64 devid, u64 start, u64 end, |
Arne Jansen | 8628764 | 2011-03-23 16:34:19 +0100 | [diff] [blame] | 1577 | struct btrfs_scrub_progress *progress, int readonly) |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1578 | { |
| 1579 | struct scrub_dev *sdev; |
| 1580 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 1581 | int ret; |
| 1582 | struct btrfs_device *dev; |
| 1583 | |
David Sterba | 7841cb2 | 2011-05-31 18:07:27 +0200 | [diff] [blame] | 1584 | if (btrfs_fs_closing(root->fs_info)) |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1585 | return -EINVAL; |
| 1586 | |
| 1587 | /* |
| 1588 | * check some assumptions |
| 1589 | */ |
| 1590 | if (root->sectorsize != PAGE_SIZE || |
| 1591 | root->sectorsize != root->leafsize || |
| 1592 | root->sectorsize != root->nodesize) { |
| 1593 | printk(KERN_ERR "btrfs_scrub: size assumptions fail\n"); |
| 1594 | return -EINVAL; |
| 1595 | } |
| 1596 | |
| 1597 | ret = scrub_workers_get(root); |
| 1598 | if (ret) |
| 1599 | return ret; |
| 1600 | |
| 1601 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
| 1602 | dev = btrfs_find_device(root, devid, NULL, NULL); |
| 1603 | if (!dev || dev->missing) { |
| 1604 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
| 1605 | scrub_workers_put(root); |
| 1606 | return -ENODEV; |
| 1607 | } |
| 1608 | mutex_lock(&fs_info->scrub_lock); |
| 1609 | |
| 1610 | if (!dev->in_fs_metadata) { |
| 1611 | mutex_unlock(&fs_info->scrub_lock); |
| 1612 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
| 1613 | scrub_workers_put(root); |
| 1614 | return -ENODEV; |
| 1615 | } |
| 1616 | |
| 1617 | if (dev->scrub_device) { |
| 1618 | mutex_unlock(&fs_info->scrub_lock); |
| 1619 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
| 1620 | scrub_workers_put(root); |
| 1621 | return -EINPROGRESS; |
| 1622 | } |
| 1623 | sdev = scrub_setup_dev(dev); |
| 1624 | if (IS_ERR(sdev)) { |
| 1625 | mutex_unlock(&fs_info->scrub_lock); |
| 1626 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
| 1627 | scrub_workers_put(root); |
| 1628 | return PTR_ERR(sdev); |
| 1629 | } |
Arne Jansen | 8628764 | 2011-03-23 16:34:19 +0100 | [diff] [blame] | 1630 | sdev->readonly = readonly; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1631 | dev->scrub_device = sdev; |
| 1632 | |
| 1633 | atomic_inc(&fs_info->scrubs_running); |
| 1634 | mutex_unlock(&fs_info->scrub_lock); |
| 1635 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
| 1636 | |
| 1637 | down_read(&fs_info->scrub_super_lock); |
| 1638 | ret = scrub_supers(sdev); |
| 1639 | up_read(&fs_info->scrub_super_lock); |
| 1640 | |
| 1641 | if (!ret) |
| 1642 | ret = scrub_enumerate_chunks(sdev, start, end); |
| 1643 | |
| 1644 | wait_event(sdev->list_wait, atomic_read(&sdev->in_flight) == 0); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1645 | atomic_dec(&fs_info->scrubs_running); |
| 1646 | wake_up(&fs_info->scrub_pause_wait); |
| 1647 | |
Jan Schmidt | 0ef8e45 | 2011-06-13 20:04:15 +0200 | [diff] [blame] | 1648 | wait_event(sdev->list_wait, atomic_read(&sdev->fixup_cnt) == 0); |
| 1649 | |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1650 | if (progress) |
| 1651 | memcpy(progress, &sdev->stat, sizeof(*progress)); |
| 1652 | |
| 1653 | mutex_lock(&fs_info->scrub_lock); |
| 1654 | dev->scrub_device = NULL; |
| 1655 | mutex_unlock(&fs_info->scrub_lock); |
| 1656 | |
| 1657 | scrub_free_dev(sdev); |
| 1658 | scrub_workers_put(root); |
| 1659 | |
| 1660 | return ret; |
| 1661 | } |
| 1662 | |
| 1663 | int btrfs_scrub_pause(struct btrfs_root *root) |
| 1664 | { |
| 1665 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 1666 | |
| 1667 | mutex_lock(&fs_info->scrub_lock); |
| 1668 | atomic_inc(&fs_info->scrub_pause_req); |
| 1669 | while (atomic_read(&fs_info->scrubs_paused) != |
| 1670 | atomic_read(&fs_info->scrubs_running)) { |
| 1671 | mutex_unlock(&fs_info->scrub_lock); |
| 1672 | wait_event(fs_info->scrub_pause_wait, |
| 1673 | atomic_read(&fs_info->scrubs_paused) == |
| 1674 | atomic_read(&fs_info->scrubs_running)); |
| 1675 | mutex_lock(&fs_info->scrub_lock); |
| 1676 | } |
| 1677 | mutex_unlock(&fs_info->scrub_lock); |
| 1678 | |
| 1679 | return 0; |
| 1680 | } |
| 1681 | |
| 1682 | int btrfs_scrub_continue(struct btrfs_root *root) |
| 1683 | { |
| 1684 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 1685 | |
| 1686 | atomic_dec(&fs_info->scrub_pause_req); |
| 1687 | wake_up(&fs_info->scrub_pause_wait); |
| 1688 | return 0; |
| 1689 | } |
| 1690 | |
| 1691 | int btrfs_scrub_pause_super(struct btrfs_root *root) |
| 1692 | { |
| 1693 | down_write(&root->fs_info->scrub_super_lock); |
| 1694 | return 0; |
| 1695 | } |
| 1696 | |
| 1697 | int btrfs_scrub_continue_super(struct btrfs_root *root) |
| 1698 | { |
| 1699 | up_write(&root->fs_info->scrub_super_lock); |
| 1700 | return 0; |
| 1701 | } |
| 1702 | |
| 1703 | int btrfs_scrub_cancel(struct btrfs_root *root) |
| 1704 | { |
| 1705 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 1706 | |
| 1707 | mutex_lock(&fs_info->scrub_lock); |
| 1708 | if (!atomic_read(&fs_info->scrubs_running)) { |
| 1709 | mutex_unlock(&fs_info->scrub_lock); |
| 1710 | return -ENOTCONN; |
| 1711 | } |
| 1712 | |
| 1713 | atomic_inc(&fs_info->scrub_cancel_req); |
| 1714 | while (atomic_read(&fs_info->scrubs_running)) { |
| 1715 | mutex_unlock(&fs_info->scrub_lock); |
| 1716 | wait_event(fs_info->scrub_pause_wait, |
| 1717 | atomic_read(&fs_info->scrubs_running) == 0); |
| 1718 | mutex_lock(&fs_info->scrub_lock); |
| 1719 | } |
| 1720 | atomic_dec(&fs_info->scrub_cancel_req); |
| 1721 | mutex_unlock(&fs_info->scrub_lock); |
| 1722 | |
| 1723 | return 0; |
| 1724 | } |
| 1725 | |
| 1726 | int btrfs_scrub_cancel_dev(struct btrfs_root *root, struct btrfs_device *dev) |
| 1727 | { |
| 1728 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 1729 | struct scrub_dev *sdev; |
| 1730 | |
| 1731 | mutex_lock(&fs_info->scrub_lock); |
| 1732 | sdev = dev->scrub_device; |
| 1733 | if (!sdev) { |
| 1734 | mutex_unlock(&fs_info->scrub_lock); |
| 1735 | return -ENOTCONN; |
| 1736 | } |
| 1737 | atomic_inc(&sdev->cancel_req); |
| 1738 | while (dev->scrub_device) { |
| 1739 | mutex_unlock(&fs_info->scrub_lock); |
| 1740 | wait_event(fs_info->scrub_pause_wait, |
| 1741 | dev->scrub_device == NULL); |
| 1742 | mutex_lock(&fs_info->scrub_lock); |
| 1743 | } |
| 1744 | mutex_unlock(&fs_info->scrub_lock); |
| 1745 | |
| 1746 | return 0; |
| 1747 | } |
| 1748 | int btrfs_scrub_cancel_devid(struct btrfs_root *root, u64 devid) |
| 1749 | { |
| 1750 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 1751 | struct btrfs_device *dev; |
| 1752 | int ret; |
| 1753 | |
| 1754 | /* |
| 1755 | * we have to hold the device_list_mutex here so the device |
| 1756 | * does not go away in cancel_dev. FIXME: find a better solution |
| 1757 | */ |
| 1758 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
| 1759 | dev = btrfs_find_device(root, devid, NULL, NULL); |
| 1760 | if (!dev) { |
| 1761 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
| 1762 | return -ENODEV; |
| 1763 | } |
| 1764 | ret = btrfs_scrub_cancel_dev(root, dev); |
| 1765 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
| 1766 | |
| 1767 | return ret; |
| 1768 | } |
| 1769 | |
| 1770 | int btrfs_scrub_progress(struct btrfs_root *root, u64 devid, |
| 1771 | struct btrfs_scrub_progress *progress) |
| 1772 | { |
| 1773 | struct btrfs_device *dev; |
| 1774 | struct scrub_dev *sdev = NULL; |
| 1775 | |
| 1776 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
| 1777 | dev = btrfs_find_device(root, devid, NULL, NULL); |
| 1778 | if (dev) |
| 1779 | sdev = dev->scrub_device; |
| 1780 | if (sdev) |
| 1781 | memcpy(progress, &sdev->stat, sizeof(*progress)); |
| 1782 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
| 1783 | |
| 1784 | return dev ? (sdev ? 0 : -ENOTCONN) : -ENODEV; |
| 1785 | } |