scrub.c 33 KB

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