scrub.c 33 KB

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