scrub.c 33 KB

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