scrub.c 34 KB

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