btrfs scrub: added unverified_errors
[firefly-linux-kernel-4.4.55.git] / fs / btrfs / scrub.c
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
19 #include <linux/blkdev.h>
20 #include "ctree.h"
21 #include "volumes.h"
22 #include "disk-io.h"
23 #include "ordered-data.h"
24
25 /*
26  * This is only the first step towards a full-features scrub. It reads all
27  * extent and super block and verifies the checksums. In case a bad checksum
28  * is found or the extent cannot be read, good data will be written back if
29  * any can be found.
30  *
31  * Future enhancements:
32  *  - To enhance the performance, better read-ahead strategies for the
33  *    extent-tree can be employed.
34  *  - In case an unrepairable extent is encountered, track which files are
35  *    affected and report them
36  *  - In case of a read error on files with nodatasum, map the file and read
37  *    the extent to trigger a writeback of the good copy
38  *  - track and record media errors, throw out bad devices
39  *  - add a mode to also read unallocated space
40  *  - make the prefetch cancellable
41  */
42
43 struct scrub_bio;
44 struct scrub_page;
45 struct scrub_dev;
46 static void scrub_bio_end_io(struct bio *bio, int err);
47 static void scrub_checksum(struct btrfs_work *work);
48 static int scrub_checksum_data(struct scrub_dev *sdev,
49                                struct scrub_page *spag, void *buffer);
50 static int scrub_checksum_tree_block(struct scrub_dev *sdev,
51                                      struct scrub_page *spag, u64 logical,
52                                      void *buffer);
53 static int scrub_checksum_super(struct scrub_bio *sbio, void *buffer);
54 static int scrub_fixup_check(struct scrub_bio *sbio, int ix);
55 static void scrub_fixup_end_io(struct bio *bio, int err);
56 static int scrub_fixup_io(int rw, struct block_device *bdev, sector_t sector,
57                           struct page *page);
58 static void scrub_fixup(struct scrub_bio *sbio, int ix);
59
60 #define SCRUB_PAGES_PER_BIO     16      /* 64k per bio */
61 #define SCRUB_BIOS_PER_DEV      16      /* 1 MB per device in flight */
62
63 struct scrub_page {
64         u64                     flags;  /* extent flags */
65         u64                     generation;
66         u64                     mirror_num;
67         int                     have_csum;
68         u8                      csum[BTRFS_CSUM_SIZE];
69 };
70
71 struct scrub_bio {
72         int                     index;
73         struct scrub_dev        *sdev;
74         struct bio              *bio;
75         int                     err;
76         u64                     logical;
77         u64                     physical;
78         struct scrub_page       spag[SCRUB_PAGES_PER_BIO];
79         u64                     count;
80         int                     next_free;
81         struct btrfs_work       work;
82 };
83
84 struct scrub_dev {
85         struct scrub_bio        *bios[SCRUB_BIOS_PER_DEV];
86         struct btrfs_device     *dev;
87         int                     first_free;
88         int                     curr;
89         atomic_t                in_flight;
90         spinlock_t              list_lock;
91         wait_queue_head_t       list_wait;
92         u16                     csum_size;
93         struct list_head        csum_list;
94         atomic_t                cancel_req;
95         int                     readonly;
96         /*
97          * statistics
98          */
99         struct btrfs_scrub_progress stat;
100         spinlock_t              stat_lock;
101 };
102
103 static void scrub_free_csums(struct scrub_dev *sdev)
104 {
105         while (!list_empty(&sdev->csum_list)) {
106                 struct btrfs_ordered_sum *sum;
107                 sum = list_first_entry(&sdev->csum_list,
108                                        struct btrfs_ordered_sum, list);
109                 list_del(&sum->list);
110                 kfree(sum);
111         }
112 }
113
114 static void scrub_free_bio(struct bio *bio)
115 {
116         int i;
117         struct page *last_page = NULL;
118
119         if (!bio)
120                 return;
121
122         for (i = 0; i < bio->bi_vcnt; ++i) {
123                 if (bio->bi_io_vec[i].bv_page == last_page)
124                         continue;
125                 last_page = bio->bi_io_vec[i].bv_page;
126                 __free_page(last_page);
127         }
128         bio_put(bio);
129 }
130
131 static noinline_for_stack void scrub_free_dev(struct scrub_dev *sdev)
132 {
133         int i;
134
135         if (!sdev)
136                 return;
137
138         for (i = 0; i < SCRUB_BIOS_PER_DEV; ++i) {
139                 struct scrub_bio *sbio = sdev->bios[i];
140
141                 if (!sbio)
142                         break;
143
144                 scrub_free_bio(sbio->bio);
145                 kfree(sbio);
146         }
147
148         scrub_free_csums(sdev);
149         kfree(sdev);
150 }
151
152 static noinline_for_stack
153 struct scrub_dev *scrub_setup_dev(struct btrfs_device *dev)
154 {
155         struct scrub_dev *sdev;
156         int             i;
157         struct btrfs_fs_info *fs_info = dev->dev_root->fs_info;
158
159         sdev = kzalloc(sizeof(*sdev), GFP_NOFS);
160         if (!sdev)
161                 goto nomem;
162         sdev->dev = dev;
163         for (i = 0; i < SCRUB_BIOS_PER_DEV; ++i) {
164                 struct scrub_bio *sbio;
165
166                 sbio = kzalloc(sizeof(*sbio), GFP_NOFS);
167                 if (!sbio)
168                         goto nomem;
169                 sdev->bios[i] = sbio;
170
171                 sbio->index = i;
172                 sbio->sdev = sdev;
173                 sbio->count = 0;
174                 sbio->work.func = scrub_checksum;
175
176                 if (i != SCRUB_BIOS_PER_DEV-1)
177                         sdev->bios[i]->next_free = i + 1;
178                  else
179                         sdev->bios[i]->next_free = -1;
180         }
181         sdev->first_free = 0;
182         sdev->curr = -1;
183         atomic_set(&sdev->in_flight, 0);
184         atomic_set(&sdev->cancel_req, 0);
185         sdev->csum_size = btrfs_super_csum_size(&fs_info->super_copy);
186         INIT_LIST_HEAD(&sdev->csum_list);
187
188         spin_lock_init(&sdev->list_lock);
189         spin_lock_init(&sdev->stat_lock);
190         init_waitqueue_head(&sdev->list_wait);
191         return sdev;
192
193 nomem:
194         scrub_free_dev(sdev);
195         return ERR_PTR(-ENOMEM);
196 }
197
198 /*
199  * scrub_recheck_error gets called when either verification of the page
200  * failed or the bio failed to read, e.g. with EIO. In the latter case,
201  * recheck_error gets called for every page in the bio, even though only
202  * one may be bad
203  */
204 static int scrub_recheck_error(struct scrub_bio *sbio, int ix)
205 {
206         struct scrub_dev *sdev = sbio->sdev;
207         u64 sector = (sbio->physical + ix * PAGE_SIZE) >> 9;
208
209         if (sbio->err) {
210                 if (scrub_fixup_io(READ, sbio->sdev->dev->bdev, sector,
211                                    sbio->bio->bi_io_vec[ix].bv_page) == 0) {
212                         if (scrub_fixup_check(sbio, ix) == 0)
213                                 return 0;
214                 }
215         }
216
217         spin_lock(&sdev->stat_lock);
218         ++sdev->stat.read_errors;
219         spin_unlock(&sdev->stat_lock);
220
221         scrub_fixup(sbio, ix);
222         return 1;
223 }
224
225 static int scrub_fixup_check(struct scrub_bio *sbio, int ix)
226 {
227         int ret = 1;
228         struct page *page;
229         void *buffer;
230         u64 flags = sbio->spag[ix].flags;
231
232         page = sbio->bio->bi_io_vec[ix].bv_page;
233         buffer = kmap_atomic(page, KM_USER0);
234         if (flags & BTRFS_EXTENT_FLAG_DATA) {
235                 ret = scrub_checksum_data(sbio->sdev,
236                                           sbio->spag + ix, buffer);
237         } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
238                 ret = scrub_checksum_tree_block(sbio->sdev,
239                                                 sbio->spag + ix,
240                                                 sbio->logical + ix * PAGE_SIZE,
241                                                 buffer);
242         } else {
243                 WARN_ON(1);
244         }
245         kunmap_atomic(buffer, KM_USER0);
246
247         return ret;
248 }
249
250 static void scrub_fixup_end_io(struct bio *bio, int err)
251 {
252         complete((struct completion *)bio->bi_private);
253 }
254
255 static void scrub_fixup(struct scrub_bio *sbio, int ix)
256 {
257         struct scrub_dev *sdev = sbio->sdev;
258         struct btrfs_fs_info *fs_info = sdev->dev->dev_root->fs_info;
259         struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
260         struct btrfs_multi_bio *multi = NULL;
261         u64 logical = sbio->logical + ix * PAGE_SIZE;
262         u64 length;
263         int i;
264         int ret;
265         DECLARE_COMPLETION_ONSTACK(complete);
266
267         if ((sbio->spag[ix].flags & BTRFS_EXTENT_FLAG_DATA) &&
268             (sbio->spag[ix].have_csum == 0)) {
269                 /*
270                  * nodatasum, don't try to fix anything
271                  * FIXME: we can do better, open the inode and trigger a
272                  * writeback
273                  */
274                 goto uncorrectable;
275         }
276
277         length = PAGE_SIZE;
278         ret = btrfs_map_block(map_tree, REQ_WRITE, logical, &length,
279                               &multi, 0);
280         if (ret || !multi || length < PAGE_SIZE) {
281                 printk(KERN_ERR
282                        "scrub_fixup: btrfs_map_block failed us for %llu\n",
283                        (unsigned long long)logical);
284                 WARN_ON(1);
285                 return;
286         }
287
288         if (multi->num_stripes == 1)
289                 /* there aren't any replicas */
290                 goto uncorrectable;
291
292         /*
293          * first find a good copy
294          */
295         for (i = 0; i < multi->num_stripes; ++i) {
296                 if (i == sbio->spag[ix].mirror_num)
297                         continue;
298
299                 if (scrub_fixup_io(READ, multi->stripes[i].dev->bdev,
300                                    multi->stripes[i].physical >> 9,
301                                    sbio->bio->bi_io_vec[ix].bv_page)) {
302                         /* I/O-error, this is not a good copy */
303                         continue;
304                 }
305
306                 if (scrub_fixup_check(sbio, ix) == 0)
307                         break;
308         }
309         if (i == multi->num_stripes)
310                 goto uncorrectable;
311
312         if (!sdev->readonly) {
313                 /*
314                  * bi_io_vec[ix].bv_page now contains good data, write it back
315                  */
316                 if (scrub_fixup_io(WRITE, sdev->dev->bdev,
317                                    (sbio->physical + ix * PAGE_SIZE) >> 9,
318                                    sbio->bio->bi_io_vec[ix].bv_page)) {
319                         /* I/O-error, writeback failed, give up */
320                         goto uncorrectable;
321                 }
322         }
323
324         kfree(multi);
325         spin_lock(&sdev->stat_lock);
326         ++sdev->stat.corrected_errors;
327         spin_unlock(&sdev->stat_lock);
328
329         if (printk_ratelimit())
330                 printk(KERN_ERR "btrfs: fixed up at %llu\n",
331                        (unsigned long long)logical);
332         return;
333
334 uncorrectable:
335         kfree(multi);
336         spin_lock(&sdev->stat_lock);
337         ++sdev->stat.uncorrectable_errors;
338         spin_unlock(&sdev->stat_lock);
339
340         if (printk_ratelimit())
341                 printk(KERN_ERR "btrfs: unable to fixup at %llu\n",
342                          (unsigned long long)logical);
343 }
344
345 static int scrub_fixup_io(int rw, struct block_device *bdev, sector_t sector,
346                          struct page *page)
347 {
348         struct bio *bio = NULL;
349         int ret;
350         DECLARE_COMPLETION_ONSTACK(complete);
351
352         bio = bio_alloc(GFP_NOFS, 1);
353         bio->bi_bdev = bdev;
354         bio->bi_sector = sector;
355         bio_add_page(bio, page, PAGE_SIZE, 0);
356         bio->bi_end_io = scrub_fixup_end_io;
357         bio->bi_private = &complete;
358         submit_bio(rw, bio);
359
360         /* this will also unplug the queue */
361         wait_for_completion(&complete);
362
363         ret = !test_bit(BIO_UPTODATE, &bio->bi_flags);
364         bio_put(bio);
365         return ret;
366 }
367
368 static void scrub_bio_end_io(struct bio *bio, int err)
369 {
370         struct scrub_bio *sbio = bio->bi_private;
371         struct scrub_dev *sdev = sbio->sdev;
372         struct btrfs_fs_info *fs_info = sdev->dev->dev_root->fs_info;
373
374         sbio->err = err;
375         sbio->bio = bio;
376
377         btrfs_queue_worker(&fs_info->scrub_workers, &sbio->work);
378 }
379
380 static void scrub_checksum(struct btrfs_work *work)
381 {
382         struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
383         struct scrub_dev *sdev = sbio->sdev;
384         struct page *page;
385         void *buffer;
386         int i;
387         u64 flags;
388         u64 logical;
389         int ret;
390
391         if (sbio->err) {
392                 ret = 0;
393                 for (i = 0; i < sbio->count; ++i)
394                         ret |= scrub_recheck_error(sbio, i);
395                 if (!ret) {
396                         spin_lock(&sdev->stat_lock);
397                         ++sdev->stat.unverified_errors;
398                         spin_unlock(&sdev->stat_lock);
399                 }
400
401                 sbio->bio->bi_flags &= ~(BIO_POOL_MASK - 1);
402                 sbio->bio->bi_flags |= 1 << BIO_UPTODATE;
403                 sbio->bio->bi_phys_segments = 0;
404                 sbio->bio->bi_idx = 0;
405
406                 for (i = 0; i < sbio->count; i++) {
407                         struct bio_vec *bi;
408                         bi = &sbio->bio->bi_io_vec[i];
409                         bi->bv_offset = 0;
410                         bi->bv_len = PAGE_SIZE;
411                 }
412                 goto out;
413         }
414         for (i = 0; i < sbio->count; ++i) {
415                 page = sbio->bio->bi_io_vec[i].bv_page;
416                 buffer = kmap_atomic(page, KM_USER0);
417                 flags = sbio->spag[i].flags;
418                 logical = sbio->logical + i * PAGE_SIZE;
419                 ret = 0;
420                 if (flags & BTRFS_EXTENT_FLAG_DATA) {
421                         ret = scrub_checksum_data(sdev, sbio->spag + i, buffer);
422                 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
423                         ret = scrub_checksum_tree_block(sdev, sbio->spag + i,
424                                                         logical, buffer);
425                 } else if (flags & BTRFS_EXTENT_FLAG_SUPER) {
426                         BUG_ON(i);
427                         (void)scrub_checksum_super(sbio, buffer);
428                 } else {
429                         WARN_ON(1);
430                 }
431                 kunmap_atomic(buffer, KM_USER0);
432                 if (ret) {
433                         ret = scrub_recheck_error(sbio, i);
434                         if (!ret) {
435                                 spin_lock(&sdev->stat_lock);
436                                 ++sdev->stat.unverified_errors;
437                                 spin_unlock(&sdev->stat_lock);
438                         }
439                 }
440         }
441
442 out:
443         scrub_free_bio(sbio->bio);
444         sbio->bio = NULL;
445         spin_lock(&sdev->list_lock);
446         sbio->next_free = sdev->first_free;
447         sdev->first_free = sbio->index;
448         spin_unlock(&sdev->list_lock);
449         atomic_dec(&sdev->in_flight);
450         wake_up(&sdev->list_wait);
451 }
452
453 static int scrub_checksum_data(struct scrub_dev *sdev,
454                                struct scrub_page *spag, void *buffer)
455 {
456         u8 csum[BTRFS_CSUM_SIZE];
457         u32 crc = ~(u32)0;
458         int fail = 0;
459         struct btrfs_root *root = sdev->dev->dev_root;
460
461         if (!spag->have_csum)
462                 return 0;
463
464         crc = btrfs_csum_data(root, buffer, crc, PAGE_SIZE);
465         btrfs_csum_final(crc, csum);
466         if (memcmp(csum, spag->csum, sdev->csum_size))
467                 fail = 1;
468
469         spin_lock(&sdev->stat_lock);
470         ++sdev->stat.data_extents_scrubbed;
471         sdev->stat.data_bytes_scrubbed += PAGE_SIZE;
472         if (fail)
473                 ++sdev->stat.csum_errors;
474         spin_unlock(&sdev->stat_lock);
475
476         return fail;
477 }
478
479 static int scrub_checksum_tree_block(struct scrub_dev *sdev,
480                                      struct scrub_page *spag, u64 logical,
481                                      void *buffer)
482 {
483         struct btrfs_header *h;
484         struct btrfs_root *root = sdev->dev->dev_root;
485         struct btrfs_fs_info *fs_info = root->fs_info;
486         u8 csum[BTRFS_CSUM_SIZE];
487         u32 crc = ~(u32)0;
488         int fail = 0;
489         int crc_fail = 0;
490
491         /*
492          * we don't use the getter functions here, as we
493          * a) don't have an extent buffer and
494          * b) the page is already kmapped
495          */
496         h = (struct btrfs_header *)buffer;
497
498         if (logical != le64_to_cpu(h->bytenr))
499                 ++fail;
500
501         if (spag->generation != le64_to_cpu(h->generation))
502                 ++fail;
503
504         if (memcmp(h->fsid, fs_info->fsid, BTRFS_UUID_SIZE))
505                 ++fail;
506
507         if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
508                    BTRFS_UUID_SIZE))
509                 ++fail;
510
511         crc = btrfs_csum_data(root, buffer + BTRFS_CSUM_SIZE, crc,
512                               PAGE_SIZE - BTRFS_CSUM_SIZE);
513         btrfs_csum_final(crc, csum);
514         if (memcmp(csum, h->csum, sdev->csum_size))
515                 ++crc_fail;
516
517         spin_lock(&sdev->stat_lock);
518         ++sdev->stat.tree_extents_scrubbed;
519         sdev->stat.tree_bytes_scrubbed += PAGE_SIZE;
520         if (crc_fail)
521                 ++sdev->stat.csum_errors;
522         if (fail)
523                 ++sdev->stat.verify_errors;
524         spin_unlock(&sdev->stat_lock);
525
526         return fail || crc_fail;
527 }
528
529 static int scrub_checksum_super(struct scrub_bio *sbio, void *buffer)
530 {
531         struct btrfs_super_block *s;
532         u64 logical;
533         struct scrub_dev *sdev = sbio->sdev;
534         struct btrfs_root *root = sdev->dev->dev_root;
535         struct btrfs_fs_info *fs_info = root->fs_info;
536         u8 csum[BTRFS_CSUM_SIZE];
537         u32 crc = ~(u32)0;
538         int fail = 0;
539
540         s = (struct btrfs_super_block *)buffer;
541         logical = sbio->logical;
542
543         if (logical != le64_to_cpu(s->bytenr))
544                 ++fail;
545
546         if (sbio->spag[0].generation != le64_to_cpu(s->generation))
547                 ++fail;
548
549         if (memcmp(s->fsid, fs_info->fsid, BTRFS_UUID_SIZE))
550                 ++fail;
551
552         crc = btrfs_csum_data(root, buffer + BTRFS_CSUM_SIZE, crc,
553                               PAGE_SIZE - BTRFS_CSUM_SIZE);
554         btrfs_csum_final(crc, csum);
555         if (memcmp(csum, s->csum, sbio->sdev->csum_size))
556                 ++fail;
557
558         if (fail) {
559                 /*
560                  * if we find an error in a super block, we just report it.
561                  * They will get written with the next transaction commit
562                  * anyway
563                  */
564                 spin_lock(&sdev->stat_lock);
565                 ++sdev->stat.super_errors;
566                 spin_unlock(&sdev->stat_lock);
567         }
568
569         return fail;
570 }
571
572 static int scrub_submit(struct scrub_dev *sdev)
573 {
574         struct scrub_bio *sbio;
575         struct bio *bio;
576         int i;
577
578         if (sdev->curr == -1)
579                 return 0;
580
581         sbio = sdev->bios[sdev->curr];
582
583         bio = bio_alloc(GFP_NOFS, sbio->count);
584         if (!bio)
585                 goto nomem;
586
587         bio->bi_private = sbio;
588         bio->bi_end_io = scrub_bio_end_io;
589         bio->bi_bdev = sdev->dev->bdev;
590         bio->bi_sector = sbio->physical >> 9;
591
592         for (i = 0; i < sbio->count; ++i) {
593                 struct page *page;
594                 int ret;
595
596                 page = alloc_page(GFP_NOFS);
597                 if (!page)
598                         goto nomem;
599
600                 ret = bio_add_page(bio, page, PAGE_SIZE, 0);
601                 if (!ret) {
602                         __free_page(page);
603                         goto nomem;
604                 }
605         }
606
607         sbio->err = 0;
608         sdev->curr = -1;
609         atomic_inc(&sdev->in_flight);
610
611         submit_bio(READ, bio);
612
613         return 0;
614
615 nomem:
616         scrub_free_bio(bio);
617
618         return -ENOMEM;
619 }
620
621 static int scrub_page(struct scrub_dev *sdev, u64 logical, u64 len,
622                       u64 physical, u64 flags, u64 gen, u64 mirror_num,
623                       u8 *csum, int force)
624 {
625         struct scrub_bio *sbio;
626
627 again:
628         /*
629          * grab a fresh bio or wait for one to become available
630          */
631         while (sdev->curr == -1) {
632                 spin_lock(&sdev->list_lock);
633                 sdev->curr = sdev->first_free;
634                 if (sdev->curr != -1) {
635                         sdev->first_free = sdev->bios[sdev->curr]->next_free;
636                         sdev->bios[sdev->curr]->next_free = -1;
637                         sdev->bios[sdev->curr]->count = 0;
638                         spin_unlock(&sdev->list_lock);
639                 } else {
640                         spin_unlock(&sdev->list_lock);
641                         wait_event(sdev->list_wait, sdev->first_free != -1);
642                 }
643         }
644         sbio = sdev->bios[sdev->curr];
645         if (sbio->count == 0) {
646                 sbio->physical = physical;
647                 sbio->logical = logical;
648         } else if (sbio->physical + sbio->count * PAGE_SIZE != physical ||
649                    sbio->logical + sbio->count * PAGE_SIZE != logical) {
650                 int ret;
651
652                 ret = scrub_submit(sdev);
653                 if (ret)
654                         return ret;
655                 goto again;
656         }
657         sbio->spag[sbio->count].flags = flags;
658         sbio->spag[sbio->count].generation = gen;
659         sbio->spag[sbio->count].have_csum = 0;
660         sbio->spag[sbio->count].mirror_num = mirror_num;
661         if (csum) {
662                 sbio->spag[sbio->count].have_csum = 1;
663                 memcpy(sbio->spag[sbio->count].csum, csum, sdev->csum_size);
664         }
665         ++sbio->count;
666         if (sbio->count == SCRUB_PAGES_PER_BIO || force) {
667                 int ret;
668
669                 ret = scrub_submit(sdev);
670                 if (ret)
671                         return ret;
672         }
673
674         return 0;
675 }
676
677 static int scrub_find_csum(struct scrub_dev *sdev, u64 logical, u64 len,
678                            u8 *csum)
679 {
680         struct btrfs_ordered_sum *sum = NULL;
681         int ret = 0;
682         unsigned long i;
683         unsigned long num_sectors;
684         u32 sectorsize = sdev->dev->dev_root->sectorsize;
685
686         while (!list_empty(&sdev->csum_list)) {
687                 sum = list_first_entry(&sdev->csum_list,
688                                        struct btrfs_ordered_sum, list);
689                 if (sum->bytenr > logical)
690                         return 0;
691                 if (sum->bytenr + sum->len > logical)
692                         break;
693
694                 ++sdev->stat.csum_discards;
695                 list_del(&sum->list);
696                 kfree(sum);
697                 sum = NULL;
698         }
699         if (!sum)
700                 return 0;
701
702         num_sectors = sum->len / sectorsize;
703         for (i = 0; i < num_sectors; ++i) {
704                 if (sum->sums[i].bytenr == logical) {
705                         memcpy(csum, &sum->sums[i].sum, sdev->csum_size);
706                         ret = 1;
707                         break;
708                 }
709         }
710         if (ret && i == num_sectors - 1) {
711                 list_del(&sum->list);
712                 kfree(sum);
713         }
714         return ret;
715 }
716
717 /* scrub extent tries to collect up to 64 kB for each bio */
718 static int scrub_extent(struct scrub_dev *sdev, u64 logical, u64 len,
719                         u64 physical, u64 flags, u64 gen, u64 mirror_num)
720 {
721         int ret;
722         u8 csum[BTRFS_CSUM_SIZE];
723
724         while (len) {
725                 u64 l = min_t(u64, len, PAGE_SIZE);
726                 int have_csum = 0;
727
728                 if (flags & BTRFS_EXTENT_FLAG_DATA) {
729                         /* push csums to sbio */
730                         have_csum = scrub_find_csum(sdev, logical, l, csum);
731                         if (have_csum == 0)
732                                 ++sdev->stat.no_csum;
733                 }
734                 ret = scrub_page(sdev, logical, l, physical, flags, gen,
735                                  mirror_num, have_csum ? csum : NULL, 0);
736                 if (ret)
737                         return ret;
738                 len -= l;
739                 logical += l;
740                 physical += l;
741         }
742         return 0;
743 }
744
745 static noinline_for_stack int scrub_stripe(struct scrub_dev *sdev,
746         struct map_lookup *map, int num, u64 base, u64 length)
747 {
748         struct btrfs_path *path;
749         struct btrfs_fs_info *fs_info = sdev->dev->dev_root->fs_info;
750         struct btrfs_root *root = fs_info->extent_root;
751         struct btrfs_root *csum_root = fs_info->csum_root;
752         struct btrfs_extent_item *extent;
753         struct blk_plug plug;
754         u64 flags;
755         int ret;
756         int slot;
757         int i;
758         u64 nstripes;
759         int start_stripe;
760         struct extent_buffer *l;
761         struct btrfs_key key;
762         u64 physical;
763         u64 logical;
764         u64 generation;
765         u64 mirror_num;
766
767         u64 increment = map->stripe_len;
768         u64 offset;
769
770         nstripes = length;
771         offset = 0;
772         do_div(nstripes, map->stripe_len);
773         if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
774                 offset = map->stripe_len * num;
775                 increment = map->stripe_len * map->num_stripes;
776                 mirror_num = 0;
777         } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
778                 int factor = map->num_stripes / map->sub_stripes;
779                 offset = map->stripe_len * (num / map->sub_stripes);
780                 increment = map->stripe_len * factor;
781                 mirror_num = num % map->sub_stripes;
782         } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
783                 increment = map->stripe_len;
784                 mirror_num = num % map->num_stripes;
785         } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
786                 increment = map->stripe_len;
787                 mirror_num = num % map->num_stripes;
788         } else {
789                 increment = map->stripe_len;
790                 mirror_num = 0;
791         }
792
793         path = btrfs_alloc_path();
794         if (!path)
795                 return -ENOMEM;
796
797         path->reada = 2;
798         path->search_commit_root = 1;
799         path->skip_locking = 1;
800
801         /*
802          * find all extents for each stripe and just read them to get
803          * them into the page cache
804          * FIXME: we can do better. build a more intelligent prefetching
805          */
806         logical = base + offset;
807         physical = map->stripes[num].physical;
808         ret = 0;
809         for (i = 0; i < nstripes; ++i) {
810                 key.objectid = logical;
811                 key.type = BTRFS_EXTENT_ITEM_KEY;
812                 key.offset = (u64)0;
813
814                 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
815                 if (ret < 0)
816                         goto out_noplug;
817
818                 /*
819                  * we might miss half an extent here, but that doesn't matter,
820                  * as it's only the prefetch
821                  */
822                 while (1) {
823                         l = path->nodes[0];
824                         slot = path->slots[0];
825                         if (slot >= btrfs_header_nritems(l)) {
826                                 ret = btrfs_next_leaf(root, path);
827                                 if (ret == 0)
828                                         continue;
829                                 if (ret < 0)
830                                         goto out_noplug;
831
832                                 break;
833                         }
834                         btrfs_item_key_to_cpu(l, &key, slot);
835
836                         if (key.objectid >= logical + map->stripe_len)
837                                 break;
838
839                         path->slots[0]++;
840                 }
841                 btrfs_release_path(path);
842                 logical += increment;
843                 physical += map->stripe_len;
844                 cond_resched();
845         }
846
847         /*
848          * collect all data csums for the stripe to avoid seeking during
849          * the scrub. This might currently (crc32) end up to be about 1MB
850          */
851         start_stripe = 0;
852         blk_start_plug(&plug);
853 again:
854         logical = base + offset + start_stripe * increment;
855         for (i = start_stripe; i < nstripes; ++i) {
856                 ret = btrfs_lookup_csums_range(csum_root, logical,
857                                                logical + map->stripe_len - 1,
858                                                &sdev->csum_list, 1);
859                 if (ret)
860                         goto out;
861
862                 logical += increment;
863                 cond_resched();
864         }
865         /*
866          * now find all extents for each stripe and scrub them
867          */
868         logical = base + offset + start_stripe * increment;
869         physical = map->stripes[num].physical + start_stripe * map->stripe_len;
870         ret = 0;
871         for (i = start_stripe; i < nstripes; ++i) {
872                 /*
873                  * canceled?
874                  */
875                 if (atomic_read(&fs_info->scrub_cancel_req) ||
876                     atomic_read(&sdev->cancel_req)) {
877                         ret = -ECANCELED;
878                         goto out;
879                 }
880                 /*
881                  * check to see if we have to pause
882                  */
883                 if (atomic_read(&fs_info->scrub_pause_req)) {
884                         /* push queued extents */
885                         scrub_submit(sdev);
886                         wait_event(sdev->list_wait,
887                                    atomic_read(&sdev->in_flight) == 0);
888                         atomic_inc(&fs_info->scrubs_paused);
889                         wake_up(&fs_info->scrub_pause_wait);
890                         mutex_lock(&fs_info->scrub_lock);
891                         while (atomic_read(&fs_info->scrub_pause_req)) {
892                                 mutex_unlock(&fs_info->scrub_lock);
893                                 wait_event(fs_info->scrub_pause_wait,
894                                    atomic_read(&fs_info->scrub_pause_req) == 0);
895                                 mutex_lock(&fs_info->scrub_lock);
896                         }
897                         atomic_dec(&fs_info->scrubs_paused);
898                         mutex_unlock(&fs_info->scrub_lock);
899                         wake_up(&fs_info->scrub_pause_wait);
900                         scrub_free_csums(sdev);
901                         start_stripe = i;
902                         goto again;
903                 }
904
905                 key.objectid = logical;
906                 key.type = BTRFS_EXTENT_ITEM_KEY;
907                 key.offset = (u64)0;
908
909                 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
910                 if (ret < 0)
911                         goto out;
912                 if (ret > 0) {
913                         ret = btrfs_previous_item(root, path, 0,
914                                                   BTRFS_EXTENT_ITEM_KEY);
915                         if (ret < 0)
916                                 goto out;
917                         if (ret > 0) {
918                                 /* there's no smaller item, so stick with the
919                                  * larger one */
920                                 btrfs_release_path(path);
921                                 ret = btrfs_search_slot(NULL, root, &key,
922                                                         path, 0, 0);
923                                 if (ret < 0)
924                                         goto out;
925                         }
926                 }
927
928                 while (1) {
929                         l = path->nodes[0];
930                         slot = path->slots[0];
931                         if (slot >= btrfs_header_nritems(l)) {
932                                 ret = btrfs_next_leaf(root, path);
933                                 if (ret == 0)
934                                         continue;
935                                 if (ret < 0)
936                                         goto out;
937
938                                 break;
939                         }
940                         btrfs_item_key_to_cpu(l, &key, slot);
941
942                         if (key.objectid + key.offset <= logical)
943                                 goto next;
944
945                         if (key.objectid >= logical + map->stripe_len)
946                                 break;
947
948                         if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
949                                 goto next;
950
951                         extent = btrfs_item_ptr(l, slot,
952                                                 struct btrfs_extent_item);
953                         flags = btrfs_extent_flags(l, extent);
954                         generation = btrfs_extent_generation(l, extent);
955
956                         if (key.objectid < logical &&
957                             (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
958                                 printk(KERN_ERR
959                                        "btrfs scrub: tree block %llu spanning "
960                                        "stripes, ignored. logical=%llu\n",
961                                        (unsigned long long)key.objectid,
962                                        (unsigned long long)logical);
963                                 goto next;
964                         }
965
966                         /*
967                          * trim extent to this stripe
968                          */
969                         if (key.objectid < logical) {
970                                 key.offset -= logical - key.objectid;
971                                 key.objectid = logical;
972                         }
973                         if (key.objectid + key.offset >
974                             logical + map->stripe_len) {
975                                 key.offset = logical + map->stripe_len -
976                                              key.objectid;
977                         }
978
979                         ret = scrub_extent(sdev, key.objectid, key.offset,
980                                            key.objectid - logical + physical,
981                                            flags, generation, mirror_num);
982                         if (ret)
983                                 goto out;
984
985 next:
986                         path->slots[0]++;
987                 }
988                 btrfs_release_path(path);
989                 logical += increment;
990                 physical += map->stripe_len;
991                 spin_lock(&sdev->stat_lock);
992                 sdev->stat.last_physical = physical;
993                 spin_unlock(&sdev->stat_lock);
994         }
995         /* push queued extents */
996         scrub_submit(sdev);
997
998 out:
999         blk_finish_plug(&plug);
1000 out_noplug:
1001         btrfs_free_path(path);
1002         return ret < 0 ? ret : 0;
1003 }
1004
1005 static noinline_for_stack int scrub_chunk(struct scrub_dev *sdev,
1006         u64 chunk_tree, u64 chunk_objectid, u64 chunk_offset, u64 length)
1007 {
1008         struct btrfs_mapping_tree *map_tree =
1009                 &sdev->dev->dev_root->fs_info->mapping_tree;
1010         struct map_lookup *map;
1011         struct extent_map *em;
1012         int i;
1013         int ret = -EINVAL;
1014
1015         read_lock(&map_tree->map_tree.lock);
1016         em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
1017         read_unlock(&map_tree->map_tree.lock);
1018
1019         if (!em)
1020                 return -EINVAL;
1021
1022         map = (struct map_lookup *)em->bdev;
1023         if (em->start != chunk_offset)
1024                 goto out;
1025
1026         if (em->len < length)
1027                 goto out;
1028
1029         for (i = 0; i < map->num_stripes; ++i) {
1030                 if (map->stripes[i].dev == sdev->dev) {
1031                         ret = scrub_stripe(sdev, map, i, chunk_offset, length);
1032                         if (ret)
1033                                 goto out;
1034                 }
1035         }
1036 out:
1037         free_extent_map(em);
1038
1039         return ret;
1040 }
1041
1042 static noinline_for_stack
1043 int scrub_enumerate_chunks(struct scrub_dev *sdev, u64 start, u64 end)
1044 {
1045         struct btrfs_dev_extent *dev_extent = NULL;
1046         struct btrfs_path *path;
1047         struct btrfs_root *root = sdev->dev->dev_root;
1048         struct btrfs_fs_info *fs_info = root->fs_info;
1049         u64 length;
1050         u64 chunk_tree;
1051         u64 chunk_objectid;
1052         u64 chunk_offset;
1053         int ret;
1054         int slot;
1055         struct extent_buffer *l;
1056         struct btrfs_key key;
1057         struct btrfs_key found_key;
1058         struct btrfs_block_group_cache *cache;
1059
1060         path = btrfs_alloc_path();
1061         if (!path)
1062                 return -ENOMEM;
1063
1064         path->reada = 2;
1065         path->search_commit_root = 1;
1066         path->skip_locking = 1;
1067
1068         key.objectid = sdev->dev->devid;
1069         key.offset = 0ull;
1070         key.type = BTRFS_DEV_EXTENT_KEY;
1071
1072
1073         while (1) {
1074                 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1075                 if (ret < 0)
1076                         break;
1077                 if (ret > 0) {
1078                         if (path->slots[0] >=
1079                             btrfs_header_nritems(path->nodes[0])) {
1080                                 ret = btrfs_next_leaf(root, path);
1081                                 if (ret)
1082                                         break;
1083                         }
1084                 }
1085
1086                 l = path->nodes[0];
1087                 slot = path->slots[0];
1088
1089                 btrfs_item_key_to_cpu(l, &found_key, slot);
1090
1091                 if (found_key.objectid != sdev->dev->devid)
1092                         break;
1093
1094                 if (btrfs_key_type(&found_key) != BTRFS_DEV_EXTENT_KEY)
1095                         break;
1096
1097                 if (found_key.offset >= end)
1098                         break;
1099
1100                 if (found_key.offset < key.offset)
1101                         break;
1102
1103                 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
1104                 length = btrfs_dev_extent_length(l, dev_extent);
1105
1106                 if (found_key.offset + length <= start) {
1107                         key.offset = found_key.offset + length;
1108                         btrfs_release_path(path);
1109                         continue;
1110                 }
1111
1112                 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
1113                 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
1114                 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
1115
1116                 /*
1117                  * get a reference on the corresponding block group to prevent
1118                  * the chunk from going away while we scrub it
1119                  */
1120                 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
1121                 if (!cache) {
1122                         ret = -ENOENT;
1123                         break;
1124                 }
1125                 ret = scrub_chunk(sdev, chunk_tree, chunk_objectid,
1126                                   chunk_offset, length);
1127                 btrfs_put_block_group(cache);
1128                 if (ret)
1129                         break;
1130
1131                 key.offset = found_key.offset + length;
1132                 btrfs_release_path(path);
1133         }
1134
1135         btrfs_free_path(path);
1136
1137         /*
1138          * ret can still be 1 from search_slot or next_leaf,
1139          * that's not an error
1140          */
1141         return ret < 0 ? ret : 0;
1142 }
1143
1144 static noinline_for_stack int scrub_supers(struct scrub_dev *sdev)
1145 {
1146         int     i;
1147         u64     bytenr;
1148         u64     gen;
1149         int     ret;
1150         struct btrfs_device *device = sdev->dev;
1151         struct btrfs_root *root = device->dev_root;
1152
1153         gen = root->fs_info->last_trans_committed;
1154
1155         for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1156                 bytenr = btrfs_sb_offset(i);
1157                 if (bytenr + BTRFS_SUPER_INFO_SIZE >= device->total_bytes)
1158                         break;
1159
1160                 ret = scrub_page(sdev, bytenr, PAGE_SIZE, bytenr,
1161                                  BTRFS_EXTENT_FLAG_SUPER, gen, i, NULL, 1);
1162                 if (ret)
1163                         return ret;
1164         }
1165         wait_event(sdev->list_wait, atomic_read(&sdev->in_flight) == 0);
1166
1167         return 0;
1168 }
1169
1170 /*
1171  * get a reference count on fs_info->scrub_workers. start worker if necessary
1172  */
1173 static noinline_for_stack int scrub_workers_get(struct btrfs_root *root)
1174 {
1175         struct btrfs_fs_info *fs_info = root->fs_info;
1176
1177         mutex_lock(&fs_info->scrub_lock);
1178         if (fs_info->scrub_workers_refcnt == 0) {
1179                 btrfs_init_workers(&fs_info->scrub_workers, "scrub",
1180                            fs_info->thread_pool_size, &fs_info->generic_worker);
1181                 fs_info->scrub_workers.idle_thresh = 4;
1182                 btrfs_start_workers(&fs_info->scrub_workers, 1);
1183         }
1184         ++fs_info->scrub_workers_refcnt;
1185         mutex_unlock(&fs_info->scrub_lock);
1186
1187         return 0;
1188 }
1189
1190 static noinline_for_stack void scrub_workers_put(struct btrfs_root *root)
1191 {
1192         struct btrfs_fs_info *fs_info = root->fs_info;
1193
1194         mutex_lock(&fs_info->scrub_lock);
1195         if (--fs_info->scrub_workers_refcnt == 0)
1196                 btrfs_stop_workers(&fs_info->scrub_workers);
1197         WARN_ON(fs_info->scrub_workers_refcnt < 0);
1198         mutex_unlock(&fs_info->scrub_lock);
1199 }
1200
1201
1202 int btrfs_scrub_dev(struct btrfs_root *root, u64 devid, u64 start, u64 end,
1203                     struct btrfs_scrub_progress *progress, int readonly)
1204 {
1205         struct scrub_dev *sdev;
1206         struct btrfs_fs_info *fs_info = root->fs_info;
1207         int ret;
1208         struct btrfs_device *dev;
1209
1210         if (btrfs_fs_closing(root->fs_info))
1211                 return -EINVAL;
1212
1213         /*
1214          * check some assumptions
1215          */
1216         if (root->sectorsize != PAGE_SIZE ||
1217             root->sectorsize != root->leafsize ||
1218             root->sectorsize != root->nodesize) {
1219                 printk(KERN_ERR "btrfs_scrub: size assumptions fail\n");
1220                 return -EINVAL;
1221         }
1222
1223         ret = scrub_workers_get(root);
1224         if (ret)
1225                 return ret;
1226
1227         mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1228         dev = btrfs_find_device(root, devid, NULL, NULL);
1229         if (!dev || dev->missing) {
1230                 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1231                 scrub_workers_put(root);
1232                 return -ENODEV;
1233         }
1234         mutex_lock(&fs_info->scrub_lock);
1235
1236         if (!dev->in_fs_metadata) {
1237                 mutex_unlock(&fs_info->scrub_lock);
1238                 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1239                 scrub_workers_put(root);
1240                 return -ENODEV;
1241         }
1242
1243         if (dev->scrub_device) {
1244                 mutex_unlock(&fs_info->scrub_lock);
1245                 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1246                 scrub_workers_put(root);
1247                 return -EINPROGRESS;
1248         }
1249         sdev = scrub_setup_dev(dev);
1250         if (IS_ERR(sdev)) {
1251                 mutex_unlock(&fs_info->scrub_lock);
1252                 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1253                 scrub_workers_put(root);
1254                 return PTR_ERR(sdev);
1255         }
1256         sdev->readonly = readonly;
1257         dev->scrub_device = sdev;
1258
1259         atomic_inc(&fs_info->scrubs_running);
1260         mutex_unlock(&fs_info->scrub_lock);
1261         mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1262
1263         down_read(&fs_info->scrub_super_lock);
1264         ret = scrub_supers(sdev);
1265         up_read(&fs_info->scrub_super_lock);
1266
1267         if (!ret)
1268                 ret = scrub_enumerate_chunks(sdev, start, end);
1269
1270         wait_event(sdev->list_wait, atomic_read(&sdev->in_flight) == 0);
1271
1272         atomic_dec(&fs_info->scrubs_running);
1273         wake_up(&fs_info->scrub_pause_wait);
1274
1275         if (progress)
1276                 memcpy(progress, &sdev->stat, sizeof(*progress));
1277
1278         mutex_lock(&fs_info->scrub_lock);
1279         dev->scrub_device = NULL;
1280         mutex_unlock(&fs_info->scrub_lock);
1281
1282         scrub_free_dev(sdev);
1283         scrub_workers_put(root);
1284
1285         return ret;
1286 }
1287
1288 int btrfs_scrub_pause(struct btrfs_root *root)
1289 {
1290         struct btrfs_fs_info *fs_info = root->fs_info;
1291
1292         mutex_lock(&fs_info->scrub_lock);
1293         atomic_inc(&fs_info->scrub_pause_req);
1294         while (atomic_read(&fs_info->scrubs_paused) !=
1295                atomic_read(&fs_info->scrubs_running)) {
1296                 mutex_unlock(&fs_info->scrub_lock);
1297                 wait_event(fs_info->scrub_pause_wait,
1298                            atomic_read(&fs_info->scrubs_paused) ==
1299                            atomic_read(&fs_info->scrubs_running));
1300                 mutex_lock(&fs_info->scrub_lock);
1301         }
1302         mutex_unlock(&fs_info->scrub_lock);
1303
1304         return 0;
1305 }
1306
1307 int btrfs_scrub_continue(struct btrfs_root *root)
1308 {
1309         struct btrfs_fs_info *fs_info = root->fs_info;
1310
1311         atomic_dec(&fs_info->scrub_pause_req);
1312         wake_up(&fs_info->scrub_pause_wait);
1313         return 0;
1314 }
1315
1316 int btrfs_scrub_pause_super(struct btrfs_root *root)
1317 {
1318         down_write(&root->fs_info->scrub_super_lock);
1319         return 0;
1320 }
1321
1322 int btrfs_scrub_continue_super(struct btrfs_root *root)
1323 {
1324         up_write(&root->fs_info->scrub_super_lock);
1325         return 0;
1326 }
1327
1328 int btrfs_scrub_cancel(struct btrfs_root *root)
1329 {
1330         struct btrfs_fs_info *fs_info = root->fs_info;
1331
1332         mutex_lock(&fs_info->scrub_lock);
1333         if (!atomic_read(&fs_info->scrubs_running)) {
1334                 mutex_unlock(&fs_info->scrub_lock);
1335                 return -ENOTCONN;
1336         }
1337
1338         atomic_inc(&fs_info->scrub_cancel_req);
1339         while (atomic_read(&fs_info->scrubs_running)) {
1340                 mutex_unlock(&fs_info->scrub_lock);
1341                 wait_event(fs_info->scrub_pause_wait,
1342                            atomic_read(&fs_info->scrubs_running) == 0);
1343                 mutex_lock(&fs_info->scrub_lock);
1344         }
1345         atomic_dec(&fs_info->scrub_cancel_req);
1346         mutex_unlock(&fs_info->scrub_lock);
1347
1348         return 0;
1349 }
1350
1351 int btrfs_scrub_cancel_dev(struct btrfs_root *root, struct btrfs_device *dev)
1352 {
1353         struct btrfs_fs_info *fs_info = root->fs_info;
1354         struct scrub_dev *sdev;
1355
1356         mutex_lock(&fs_info->scrub_lock);
1357         sdev = dev->scrub_device;
1358         if (!sdev) {
1359                 mutex_unlock(&fs_info->scrub_lock);
1360                 return -ENOTCONN;
1361         }
1362         atomic_inc(&sdev->cancel_req);
1363         while (dev->scrub_device) {
1364                 mutex_unlock(&fs_info->scrub_lock);
1365                 wait_event(fs_info->scrub_pause_wait,
1366                            dev->scrub_device == NULL);
1367                 mutex_lock(&fs_info->scrub_lock);
1368         }
1369         mutex_unlock(&fs_info->scrub_lock);
1370
1371         return 0;
1372 }
1373 int btrfs_scrub_cancel_devid(struct btrfs_root *root, u64 devid)
1374 {
1375         struct btrfs_fs_info *fs_info = root->fs_info;
1376         struct btrfs_device *dev;
1377         int ret;
1378
1379         /*
1380          * we have to hold the device_list_mutex here so the device
1381          * does not go away in cancel_dev. FIXME: find a better solution
1382          */
1383         mutex_lock(&fs_info->fs_devices->device_list_mutex);
1384         dev = btrfs_find_device(root, devid, NULL, NULL);
1385         if (!dev) {
1386                 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
1387                 return -ENODEV;
1388         }
1389         ret = btrfs_scrub_cancel_dev(root, dev);
1390         mutex_unlock(&fs_info->fs_devices->device_list_mutex);
1391
1392         return ret;
1393 }
1394
1395 int btrfs_scrub_progress(struct btrfs_root *root, u64 devid,
1396                          struct btrfs_scrub_progress *progress)
1397 {
1398         struct btrfs_device *dev;
1399         struct scrub_dev *sdev = NULL;
1400
1401         mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1402         dev = btrfs_find_device(root, devid, NULL, NULL);
1403         if (dev)
1404                 sdev = dev->scrub_device;
1405         if (sdev)
1406                 memcpy(progress, &sdev->stat, sizeof(*progress));
1407         mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1408
1409         return dev ? (sdev ? 0 : -ENOTCONN) : -ENODEV;
1410 }