scrub.c 33 KB

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