scrub.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386
  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;
  699. l = path->nodes[0];
  700. slot = path->slots[0];
  701. btrfs_item_key_to_cpu(l, &key, slot);
  702. if (key.objectid != logical) {
  703. ret = btrfs_previous_item(root, path, 0,
  704. BTRFS_EXTENT_ITEM_KEY);
  705. if (ret < 0)
  706. goto out;
  707. }
  708. while (1) {
  709. l = path->nodes[0];
  710. slot = path->slots[0];
  711. if (slot >= btrfs_header_nritems(l)) {
  712. ret = btrfs_next_leaf(root, path);
  713. if (ret == 0)
  714. continue;
  715. if (ret < 0)
  716. goto out;
  717. break;
  718. }
  719. btrfs_item_key_to_cpu(l, &key, slot);
  720. if (key.objectid >= logical + map->stripe_len)
  721. break;
  722. path->slots[0]++;
  723. }
  724. btrfs_release_path(path);
  725. logical += increment;
  726. physical += map->stripe_len;
  727. cond_resched();
  728. }
  729. /*
  730. * collect all data csums for the stripe to avoid seeking during
  731. * the scrub. This might currently (crc32) end up to be about 1MB
  732. */
  733. start_stripe = 0;
  734. blk_start_plug(&plug);
  735. again:
  736. logical = base + offset + start_stripe * increment;
  737. for (i = start_stripe; i < nstripes; ++i) {
  738. ret = btrfs_lookup_csums_range(csum_root, logical,
  739. logical + map->stripe_len - 1,
  740. &sdev->csum_list, 1);
  741. if (ret)
  742. goto out;
  743. logical += increment;
  744. cond_resched();
  745. }
  746. /*
  747. * now find all extents for each stripe and scrub them
  748. */
  749. logical = base + offset + start_stripe * increment;
  750. physical = map->stripes[num].physical + start_stripe * map->stripe_len;
  751. ret = 0;
  752. for (i = start_stripe; i < nstripes; ++i) {
  753. /*
  754. * canceled?
  755. */
  756. if (atomic_read(&fs_info->scrub_cancel_req) ||
  757. atomic_read(&sdev->cancel_req)) {
  758. ret = -ECANCELED;
  759. goto out;
  760. }
  761. /*
  762. * check to see if we have to pause
  763. */
  764. if (atomic_read(&fs_info->scrub_pause_req)) {
  765. /* push queued extents */
  766. scrub_submit(sdev);
  767. wait_event(sdev->list_wait,
  768. atomic_read(&sdev->in_flight) == 0);
  769. atomic_inc(&fs_info->scrubs_paused);
  770. wake_up(&fs_info->scrub_pause_wait);
  771. mutex_lock(&fs_info->scrub_lock);
  772. while (atomic_read(&fs_info->scrub_pause_req)) {
  773. mutex_unlock(&fs_info->scrub_lock);
  774. wait_event(fs_info->scrub_pause_wait,
  775. atomic_read(&fs_info->scrub_pause_req) == 0);
  776. mutex_lock(&fs_info->scrub_lock);
  777. }
  778. atomic_dec(&fs_info->scrubs_paused);
  779. mutex_unlock(&fs_info->scrub_lock);
  780. wake_up(&fs_info->scrub_pause_wait);
  781. scrub_free_csums(sdev);
  782. start_stripe = i;
  783. goto again;
  784. }
  785. key.objectid = logical;
  786. key.type = BTRFS_EXTENT_ITEM_KEY;
  787. key.offset = (u64)0;
  788. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  789. if (ret < 0)
  790. goto out;
  791. l = path->nodes[0];
  792. slot = path->slots[0];
  793. btrfs_item_key_to_cpu(l, &key, slot);
  794. if (key.objectid != logical) {
  795. ret = btrfs_previous_item(root, path, 0,
  796. BTRFS_EXTENT_ITEM_KEY);
  797. if (ret < 0)
  798. goto out;
  799. }
  800. while (1) {
  801. l = path->nodes[0];
  802. slot = path->slots[0];
  803. if (slot >= btrfs_header_nritems(l)) {
  804. ret = btrfs_next_leaf(root, path);
  805. if (ret == 0)
  806. continue;
  807. if (ret < 0)
  808. goto out;
  809. break;
  810. }
  811. btrfs_item_key_to_cpu(l, &key, slot);
  812. if (key.objectid + key.offset <= logical)
  813. goto next;
  814. if (key.objectid >= logical + map->stripe_len)
  815. break;
  816. if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
  817. goto next;
  818. extent = btrfs_item_ptr(l, slot,
  819. struct btrfs_extent_item);
  820. flags = btrfs_extent_flags(l, extent);
  821. generation = btrfs_extent_generation(l, extent);
  822. if (key.objectid < logical &&
  823. (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
  824. printk(KERN_ERR
  825. "btrfs scrub: tree block %llu spanning "
  826. "stripes, ignored. logical=%llu\n",
  827. (unsigned long long)key.objectid,
  828. (unsigned long long)logical);
  829. goto next;
  830. }
  831. /*
  832. * trim extent to this stripe
  833. */
  834. if (key.objectid < logical) {
  835. key.offset -= logical - key.objectid;
  836. key.objectid = logical;
  837. }
  838. if (key.objectid + key.offset >
  839. logical + map->stripe_len) {
  840. key.offset = logical + map->stripe_len -
  841. key.objectid;
  842. }
  843. ret = scrub_extent(sdev, key.objectid, key.offset,
  844. key.objectid - logical + physical,
  845. flags, generation, mirror_num);
  846. if (ret)
  847. goto out;
  848. next:
  849. path->slots[0]++;
  850. }
  851. btrfs_release_path(path);
  852. logical += increment;
  853. physical += map->stripe_len;
  854. spin_lock(&sdev->stat_lock);
  855. sdev->stat.last_physical = physical;
  856. spin_unlock(&sdev->stat_lock);
  857. }
  858. /* push queued extents */
  859. scrub_submit(sdev);
  860. out:
  861. blk_finish_plug(&plug);
  862. btrfs_free_path(path);
  863. return ret < 0 ? ret : 0;
  864. }
  865. static noinline_for_stack int scrub_chunk(struct scrub_dev *sdev,
  866. u64 chunk_tree, u64 chunk_objectid, u64 chunk_offset, u64 length)
  867. {
  868. struct btrfs_mapping_tree *map_tree =
  869. &sdev->dev->dev_root->fs_info->mapping_tree;
  870. struct map_lookup *map;
  871. struct extent_map *em;
  872. int i;
  873. int ret = -EINVAL;
  874. read_lock(&map_tree->map_tree.lock);
  875. em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
  876. read_unlock(&map_tree->map_tree.lock);
  877. if (!em)
  878. return -EINVAL;
  879. map = (struct map_lookup *)em->bdev;
  880. if (em->start != chunk_offset)
  881. goto out;
  882. if (em->len < length)
  883. goto out;
  884. for (i = 0; i < map->num_stripes; ++i) {
  885. if (map->stripes[i].dev == sdev->dev) {
  886. ret = scrub_stripe(sdev, map, i, chunk_offset, length);
  887. if (ret)
  888. goto out;
  889. }
  890. }
  891. out:
  892. free_extent_map(em);
  893. return ret;
  894. }
  895. static noinline_for_stack
  896. int scrub_enumerate_chunks(struct scrub_dev *sdev, u64 start, u64 end)
  897. {
  898. struct btrfs_dev_extent *dev_extent = NULL;
  899. struct btrfs_path *path;
  900. struct btrfs_root *root = sdev->dev->dev_root;
  901. struct btrfs_fs_info *fs_info = root->fs_info;
  902. u64 length;
  903. u64 chunk_tree;
  904. u64 chunk_objectid;
  905. u64 chunk_offset;
  906. int ret;
  907. int slot;
  908. struct extent_buffer *l;
  909. struct btrfs_key key;
  910. struct btrfs_key found_key;
  911. struct btrfs_block_group_cache *cache;
  912. path = btrfs_alloc_path();
  913. if (!path)
  914. return -ENOMEM;
  915. path->reada = 2;
  916. path->search_commit_root = 1;
  917. path->skip_locking = 1;
  918. key.objectid = sdev->dev->devid;
  919. key.offset = 0ull;
  920. key.type = BTRFS_DEV_EXTENT_KEY;
  921. while (1) {
  922. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  923. if (ret < 0)
  924. goto out;
  925. ret = 0;
  926. l = path->nodes[0];
  927. slot = path->slots[0];
  928. btrfs_item_key_to_cpu(l, &found_key, slot);
  929. if (found_key.objectid != sdev->dev->devid)
  930. break;
  931. if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
  932. break;
  933. if (found_key.offset >= end)
  934. break;
  935. if (found_key.offset < key.offset)
  936. break;
  937. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  938. length = btrfs_dev_extent_length(l, dev_extent);
  939. if (found_key.offset + length <= start) {
  940. key.offset = found_key.offset + length;
  941. btrfs_release_path(path);
  942. continue;
  943. }
  944. chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
  945. chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
  946. chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
  947. /*
  948. * get a reference on the corresponding block group to prevent
  949. * the chunk from going away while we scrub it
  950. */
  951. cache = btrfs_lookup_block_group(fs_info, chunk_offset);
  952. if (!cache) {
  953. ret = -ENOENT;
  954. goto out;
  955. }
  956. ret = scrub_chunk(sdev, chunk_tree, chunk_objectid,
  957. chunk_offset, length);
  958. btrfs_put_block_group(cache);
  959. if (ret)
  960. break;
  961. key.offset = found_key.offset + length;
  962. btrfs_release_path(path);
  963. }
  964. out:
  965. btrfs_free_path(path);
  966. return ret;
  967. }
  968. static noinline_for_stack int scrub_supers(struct scrub_dev *sdev)
  969. {
  970. int i;
  971. u64 bytenr;
  972. u64 gen;
  973. int ret;
  974. struct btrfs_device *device = sdev->dev;
  975. struct btrfs_root *root = device->dev_root;
  976. gen = root->fs_info->last_trans_committed;
  977. for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
  978. bytenr = btrfs_sb_offset(i);
  979. if (bytenr + BTRFS_SUPER_INFO_SIZE >= device->total_bytes)
  980. break;
  981. ret = scrub_page(sdev, bytenr, PAGE_SIZE, bytenr,
  982. BTRFS_EXTENT_FLAG_SUPER, gen, i, NULL, 1);
  983. if (ret)
  984. return ret;
  985. }
  986. wait_event(sdev->list_wait, atomic_read(&sdev->in_flight) == 0);
  987. return 0;
  988. }
  989. /*
  990. * get a reference count on fs_info->scrub_workers. start worker if necessary
  991. */
  992. static noinline_for_stack int scrub_workers_get(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_start_workers(&fs_info->scrub_workers, 1);
  998. ++fs_info->scrub_workers_refcnt;
  999. mutex_unlock(&fs_info->scrub_lock);
  1000. return 0;
  1001. }
  1002. static noinline_for_stack void scrub_workers_put(struct btrfs_root *root)
  1003. {
  1004. struct btrfs_fs_info *fs_info = root->fs_info;
  1005. mutex_lock(&fs_info->scrub_lock);
  1006. if (--fs_info->scrub_workers_refcnt == 0)
  1007. btrfs_stop_workers(&fs_info->scrub_workers);
  1008. WARN_ON(fs_info->scrub_workers_refcnt < 0);
  1009. mutex_unlock(&fs_info->scrub_lock);
  1010. }
  1011. int btrfs_scrub_dev(struct btrfs_root *root, u64 devid, u64 start, u64 end,
  1012. struct btrfs_scrub_progress *progress, int readonly)
  1013. {
  1014. struct scrub_dev *sdev;
  1015. struct btrfs_fs_info *fs_info = root->fs_info;
  1016. int ret;
  1017. struct btrfs_device *dev;
  1018. if (btrfs_fs_closing(root->fs_info))
  1019. return -EINVAL;
  1020. /*
  1021. * check some assumptions
  1022. */
  1023. if (root->sectorsize != PAGE_SIZE ||
  1024. root->sectorsize != root->leafsize ||
  1025. root->sectorsize != root->nodesize) {
  1026. printk(KERN_ERR "btrfs_scrub: size assumptions fail\n");
  1027. return -EINVAL;
  1028. }
  1029. ret = scrub_workers_get(root);
  1030. if (ret)
  1031. return ret;
  1032. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  1033. dev = btrfs_find_device(root, devid, NULL, NULL);
  1034. if (!dev || dev->missing) {
  1035. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1036. scrub_workers_put(root);
  1037. return -ENODEV;
  1038. }
  1039. mutex_lock(&fs_info->scrub_lock);
  1040. if (!dev->in_fs_metadata) {
  1041. mutex_unlock(&fs_info->scrub_lock);
  1042. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1043. scrub_workers_put(root);
  1044. return -ENODEV;
  1045. }
  1046. if (dev->scrub_device) {
  1047. mutex_unlock(&fs_info->scrub_lock);
  1048. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1049. scrub_workers_put(root);
  1050. return -EINPROGRESS;
  1051. }
  1052. sdev = scrub_setup_dev(dev);
  1053. if (IS_ERR(sdev)) {
  1054. mutex_unlock(&fs_info->scrub_lock);
  1055. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1056. scrub_workers_put(root);
  1057. return PTR_ERR(sdev);
  1058. }
  1059. sdev->readonly = readonly;
  1060. dev->scrub_device = sdev;
  1061. atomic_inc(&fs_info->scrubs_running);
  1062. mutex_unlock(&fs_info->scrub_lock);
  1063. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1064. down_read(&fs_info->scrub_super_lock);
  1065. ret = scrub_supers(sdev);
  1066. up_read(&fs_info->scrub_super_lock);
  1067. if (!ret)
  1068. ret = scrub_enumerate_chunks(sdev, start, end);
  1069. wait_event(sdev->list_wait, atomic_read(&sdev->in_flight) == 0);
  1070. atomic_dec(&fs_info->scrubs_running);
  1071. wake_up(&fs_info->scrub_pause_wait);
  1072. if (progress)
  1073. memcpy(progress, &sdev->stat, sizeof(*progress));
  1074. mutex_lock(&fs_info->scrub_lock);
  1075. dev->scrub_device = NULL;
  1076. mutex_unlock(&fs_info->scrub_lock);
  1077. scrub_free_dev(sdev);
  1078. scrub_workers_put(root);
  1079. return ret;
  1080. }
  1081. int btrfs_scrub_pause(struct btrfs_root *root)
  1082. {
  1083. struct btrfs_fs_info *fs_info = root->fs_info;
  1084. mutex_lock(&fs_info->scrub_lock);
  1085. atomic_inc(&fs_info->scrub_pause_req);
  1086. while (atomic_read(&fs_info->scrubs_paused) !=
  1087. atomic_read(&fs_info->scrubs_running)) {
  1088. mutex_unlock(&fs_info->scrub_lock);
  1089. wait_event(fs_info->scrub_pause_wait,
  1090. atomic_read(&fs_info->scrubs_paused) ==
  1091. atomic_read(&fs_info->scrubs_running));
  1092. mutex_lock(&fs_info->scrub_lock);
  1093. }
  1094. mutex_unlock(&fs_info->scrub_lock);
  1095. return 0;
  1096. }
  1097. int btrfs_scrub_continue(struct btrfs_root *root)
  1098. {
  1099. struct btrfs_fs_info *fs_info = root->fs_info;
  1100. atomic_dec(&fs_info->scrub_pause_req);
  1101. wake_up(&fs_info->scrub_pause_wait);
  1102. return 0;
  1103. }
  1104. int btrfs_scrub_pause_super(struct btrfs_root *root)
  1105. {
  1106. down_write(&root->fs_info->scrub_super_lock);
  1107. return 0;
  1108. }
  1109. int btrfs_scrub_continue_super(struct btrfs_root *root)
  1110. {
  1111. up_write(&root->fs_info->scrub_super_lock);
  1112. return 0;
  1113. }
  1114. int btrfs_scrub_cancel(struct btrfs_root *root)
  1115. {
  1116. struct btrfs_fs_info *fs_info = root->fs_info;
  1117. mutex_lock(&fs_info->scrub_lock);
  1118. if (!atomic_read(&fs_info->scrubs_running)) {
  1119. mutex_unlock(&fs_info->scrub_lock);
  1120. return -ENOTCONN;
  1121. }
  1122. atomic_inc(&fs_info->scrub_cancel_req);
  1123. while (atomic_read(&fs_info->scrubs_running)) {
  1124. mutex_unlock(&fs_info->scrub_lock);
  1125. wait_event(fs_info->scrub_pause_wait,
  1126. atomic_read(&fs_info->scrubs_running) == 0);
  1127. mutex_lock(&fs_info->scrub_lock);
  1128. }
  1129. atomic_dec(&fs_info->scrub_cancel_req);
  1130. mutex_unlock(&fs_info->scrub_lock);
  1131. return 0;
  1132. }
  1133. int btrfs_scrub_cancel_dev(struct btrfs_root *root, struct btrfs_device *dev)
  1134. {
  1135. struct btrfs_fs_info *fs_info = root->fs_info;
  1136. struct scrub_dev *sdev;
  1137. mutex_lock(&fs_info->scrub_lock);
  1138. sdev = dev->scrub_device;
  1139. if (!sdev) {
  1140. mutex_unlock(&fs_info->scrub_lock);
  1141. return -ENOTCONN;
  1142. }
  1143. atomic_inc(&sdev->cancel_req);
  1144. while (dev->scrub_device) {
  1145. mutex_unlock(&fs_info->scrub_lock);
  1146. wait_event(fs_info->scrub_pause_wait,
  1147. dev->scrub_device == NULL);
  1148. mutex_lock(&fs_info->scrub_lock);
  1149. }
  1150. mutex_unlock(&fs_info->scrub_lock);
  1151. return 0;
  1152. }
  1153. int btrfs_scrub_cancel_devid(struct btrfs_root *root, u64 devid)
  1154. {
  1155. struct btrfs_fs_info *fs_info = root->fs_info;
  1156. struct btrfs_device *dev;
  1157. int ret;
  1158. /*
  1159. * we have to hold the device_list_mutex here so the device
  1160. * does not go away in cancel_dev. FIXME: find a better solution
  1161. */
  1162. mutex_lock(&fs_info->fs_devices->device_list_mutex);
  1163. dev = btrfs_find_device(root, devid, NULL, NULL);
  1164. if (!dev) {
  1165. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  1166. return -ENODEV;
  1167. }
  1168. ret = btrfs_scrub_cancel_dev(root, dev);
  1169. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  1170. return ret;
  1171. }
  1172. int btrfs_scrub_progress(struct btrfs_root *root, u64 devid,
  1173. struct btrfs_scrub_progress *progress)
  1174. {
  1175. struct btrfs_device *dev;
  1176. struct scrub_dev *sdev = NULL;
  1177. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  1178. dev = btrfs_find_device(root, devid, NULL, NULL);
  1179. if (dev)
  1180. sdev = dev->scrub_device;
  1181. if (sdev)
  1182. memcpy(progress, &sdev->stat, sizeof(*progress));
  1183. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1184. return dev ? (sdev ? 0 : -ENOTCONN) : -ENODEV;
  1185. }