scrub.c 33 KB

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