scrub.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444
  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 <linux/ratelimit.h>
  20. #include "ctree.h"
  21. #include "volumes.h"
  22. #include "disk-io.h"
  23. #include "ordered-data.h"
  24. #include "transaction.h"
  25. #include "backref.h"
  26. #include "extent_io.h"
  27. #include "check-integrity.h"
  28. /*
  29. * This is only the first step towards a full-features scrub. It reads all
  30. * extent and super block and verifies the checksums. In case a bad checksum
  31. * is found or the extent cannot be read, good data will be written back if
  32. * any can be found.
  33. *
  34. * Future enhancements:
  35. * - In case an unrepairable extent is encountered, track which files are
  36. * affected and report them
  37. * - track and record media errors, throw out bad devices
  38. * - add a mode to also read unallocated space
  39. */
  40. struct scrub_block;
  41. struct scrub_dev;
  42. #define SCRUB_PAGES_PER_BIO 16 /* 64k per bio */
  43. #define SCRUB_BIOS_PER_DEV 16 /* 1 MB per device in flight */
  44. #define SCRUB_MAX_PAGES_PER_BLOCK 16 /* 64k per node/leaf/sector */
  45. struct scrub_page {
  46. struct scrub_block *sblock;
  47. struct page *page;
  48. struct block_device *bdev;
  49. u64 flags; /* extent flags */
  50. u64 generation;
  51. u64 logical;
  52. u64 physical;
  53. struct {
  54. unsigned int mirror_num:8;
  55. unsigned int have_csum:1;
  56. unsigned int io_error:1;
  57. };
  58. u8 csum[BTRFS_CSUM_SIZE];
  59. };
  60. struct scrub_bio {
  61. int index;
  62. struct scrub_dev *sdev;
  63. struct bio *bio;
  64. int err;
  65. u64 logical;
  66. u64 physical;
  67. struct scrub_page *pagev[SCRUB_PAGES_PER_BIO];
  68. int page_count;
  69. int next_free;
  70. struct btrfs_work work;
  71. };
  72. struct scrub_block {
  73. struct scrub_page pagev[SCRUB_MAX_PAGES_PER_BLOCK];
  74. int page_count;
  75. atomic_t outstanding_pages;
  76. atomic_t ref_count; /* free mem on transition to zero */
  77. struct scrub_dev *sdev;
  78. struct {
  79. unsigned int header_error:1;
  80. unsigned int checksum_error:1;
  81. unsigned int no_io_error_seen:1;
  82. };
  83. };
  84. struct scrub_dev {
  85. struct scrub_bio *bios[SCRUB_BIOS_PER_DEV];
  86. struct btrfs_device *dev;
  87. int first_free;
  88. int curr;
  89. atomic_t in_flight;
  90. atomic_t fixup_cnt;
  91. spinlock_t list_lock;
  92. wait_queue_head_t list_wait;
  93. u16 csum_size;
  94. struct list_head csum_list;
  95. atomic_t cancel_req;
  96. int readonly;
  97. int pages_per_bio; /* <= SCRUB_PAGES_PER_BIO */
  98. u32 sectorsize;
  99. u32 nodesize;
  100. u32 leafsize;
  101. /*
  102. * statistics
  103. */
  104. struct btrfs_scrub_progress stat;
  105. spinlock_t stat_lock;
  106. };
  107. struct scrub_fixup_nodatasum {
  108. struct scrub_dev *sdev;
  109. u64 logical;
  110. struct btrfs_root *root;
  111. struct btrfs_work work;
  112. int mirror_num;
  113. };
  114. struct scrub_warning {
  115. struct btrfs_path *path;
  116. u64 extent_item_size;
  117. char *scratch_buf;
  118. char *msg_buf;
  119. const char *errstr;
  120. sector_t sector;
  121. u64 logical;
  122. struct btrfs_device *dev;
  123. int msg_bufsize;
  124. int scratch_bufsize;
  125. };
  126. static int scrub_handle_errored_block(struct scrub_block *sblock_to_check);
  127. static int scrub_setup_recheck_block(struct scrub_dev *sdev,
  128. struct btrfs_mapping_tree *map_tree,
  129. u64 length, u64 logical,
  130. struct scrub_block *sblock);
  131. static int scrub_recheck_block(struct btrfs_fs_info *fs_info,
  132. struct scrub_block *sblock, int is_metadata,
  133. int have_csum, u8 *csum, u64 generation,
  134. u16 csum_size);
  135. static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
  136. struct scrub_block *sblock,
  137. int is_metadata, int have_csum,
  138. const u8 *csum, u64 generation,
  139. u16 csum_size);
  140. static void scrub_complete_bio_end_io(struct bio *bio, int err);
  141. static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
  142. struct scrub_block *sblock_good,
  143. int force_write);
  144. static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
  145. struct scrub_block *sblock_good,
  146. int page_num, int force_write);
  147. static int scrub_checksum_data(struct scrub_block *sblock);
  148. static int scrub_checksum_tree_block(struct scrub_block *sblock);
  149. static int scrub_checksum_super(struct scrub_block *sblock);
  150. static void scrub_block_get(struct scrub_block *sblock);
  151. static void scrub_block_put(struct scrub_block *sblock);
  152. static int scrub_add_page_to_bio(struct scrub_dev *sdev,
  153. struct scrub_page *spage);
  154. static int scrub_pages(struct scrub_dev *sdev, u64 logical, u64 len,
  155. u64 physical, u64 flags, u64 gen, int mirror_num,
  156. u8 *csum, int force);
  157. static void scrub_bio_end_io(struct bio *bio, int err);
  158. static void scrub_bio_end_io_worker(struct btrfs_work *work);
  159. static void scrub_block_complete(struct scrub_block *sblock);
  160. static void scrub_free_csums(struct scrub_dev *sdev)
  161. {
  162. while (!list_empty(&sdev->csum_list)) {
  163. struct btrfs_ordered_sum *sum;
  164. sum = list_first_entry(&sdev->csum_list,
  165. struct btrfs_ordered_sum, list);
  166. list_del(&sum->list);
  167. kfree(sum);
  168. }
  169. }
  170. static noinline_for_stack void scrub_free_dev(struct scrub_dev *sdev)
  171. {
  172. int i;
  173. if (!sdev)
  174. return;
  175. /* this can happen when scrub is cancelled */
  176. if (sdev->curr != -1) {
  177. struct scrub_bio *sbio = sdev->bios[sdev->curr];
  178. for (i = 0; i < sbio->page_count; i++) {
  179. BUG_ON(!sbio->pagev[i]);
  180. BUG_ON(!sbio->pagev[i]->page);
  181. scrub_block_put(sbio->pagev[i]->sblock);
  182. }
  183. bio_put(sbio->bio);
  184. }
  185. for (i = 0; i < SCRUB_BIOS_PER_DEV; ++i) {
  186. struct scrub_bio *sbio = sdev->bios[i];
  187. if (!sbio)
  188. break;
  189. kfree(sbio);
  190. }
  191. scrub_free_csums(sdev);
  192. kfree(sdev);
  193. }
  194. static noinline_for_stack
  195. struct scrub_dev *scrub_setup_dev(struct btrfs_device *dev)
  196. {
  197. struct scrub_dev *sdev;
  198. int i;
  199. struct btrfs_fs_info *fs_info = dev->dev_root->fs_info;
  200. int pages_per_bio;
  201. pages_per_bio = min_t(int, SCRUB_PAGES_PER_BIO,
  202. bio_get_nr_vecs(dev->bdev));
  203. sdev = kzalloc(sizeof(*sdev), GFP_NOFS);
  204. if (!sdev)
  205. goto nomem;
  206. sdev->dev = dev;
  207. sdev->pages_per_bio = pages_per_bio;
  208. sdev->curr = -1;
  209. for (i = 0; i < SCRUB_BIOS_PER_DEV; ++i) {
  210. struct scrub_bio *sbio;
  211. sbio = kzalloc(sizeof(*sbio), GFP_NOFS);
  212. if (!sbio)
  213. goto nomem;
  214. sdev->bios[i] = sbio;
  215. sbio->index = i;
  216. sbio->sdev = sdev;
  217. sbio->page_count = 0;
  218. sbio->work.func = scrub_bio_end_io_worker;
  219. if (i != SCRUB_BIOS_PER_DEV-1)
  220. sdev->bios[i]->next_free = i + 1;
  221. else
  222. sdev->bios[i]->next_free = -1;
  223. }
  224. sdev->first_free = 0;
  225. sdev->nodesize = dev->dev_root->nodesize;
  226. sdev->leafsize = dev->dev_root->leafsize;
  227. sdev->sectorsize = dev->dev_root->sectorsize;
  228. atomic_set(&sdev->in_flight, 0);
  229. atomic_set(&sdev->fixup_cnt, 0);
  230. atomic_set(&sdev->cancel_req, 0);
  231. sdev->csum_size = btrfs_super_csum_size(fs_info->super_copy);
  232. INIT_LIST_HEAD(&sdev->csum_list);
  233. spin_lock_init(&sdev->list_lock);
  234. spin_lock_init(&sdev->stat_lock);
  235. init_waitqueue_head(&sdev->list_wait);
  236. return sdev;
  237. nomem:
  238. scrub_free_dev(sdev);
  239. return ERR_PTR(-ENOMEM);
  240. }
  241. static int scrub_print_warning_inode(u64 inum, u64 offset, u64 root, void *ctx)
  242. {
  243. u64 isize;
  244. u32 nlink;
  245. int ret;
  246. int i;
  247. struct extent_buffer *eb;
  248. struct btrfs_inode_item *inode_item;
  249. struct scrub_warning *swarn = ctx;
  250. struct btrfs_fs_info *fs_info = swarn->dev->dev_root->fs_info;
  251. struct inode_fs_paths *ipath = NULL;
  252. struct btrfs_root *local_root;
  253. struct btrfs_key root_key;
  254. root_key.objectid = root;
  255. root_key.type = BTRFS_ROOT_ITEM_KEY;
  256. root_key.offset = (u64)-1;
  257. local_root = btrfs_read_fs_root_no_name(fs_info, &root_key);
  258. if (IS_ERR(local_root)) {
  259. ret = PTR_ERR(local_root);
  260. goto err;
  261. }
  262. ret = inode_item_info(inum, 0, local_root, swarn->path);
  263. if (ret) {
  264. btrfs_release_path(swarn->path);
  265. goto err;
  266. }
  267. eb = swarn->path->nodes[0];
  268. inode_item = btrfs_item_ptr(eb, swarn->path->slots[0],
  269. struct btrfs_inode_item);
  270. isize = btrfs_inode_size(eb, inode_item);
  271. nlink = btrfs_inode_nlink(eb, inode_item);
  272. btrfs_release_path(swarn->path);
  273. ipath = init_ipath(4096, local_root, swarn->path);
  274. if (IS_ERR(ipath)) {
  275. ret = PTR_ERR(ipath);
  276. ipath = NULL;
  277. goto err;
  278. }
  279. ret = paths_from_inode(inum, ipath);
  280. if (ret < 0)
  281. goto err;
  282. /*
  283. * we deliberately ignore the bit ipath might have been too small to
  284. * hold all of the paths here
  285. */
  286. for (i = 0; i < ipath->fspath->elem_cnt; ++i)
  287. printk(KERN_WARNING "btrfs: %s at logical %llu on dev "
  288. "%s, sector %llu, root %llu, inode %llu, offset %llu, "
  289. "length %llu, links %u (path: %s)\n", swarn->errstr,
  290. swarn->logical, swarn->dev->name,
  291. (unsigned long long)swarn->sector, root, inum, offset,
  292. min(isize - offset, (u64)PAGE_SIZE), nlink,
  293. (char *)(unsigned long)ipath->fspath->val[i]);
  294. free_ipath(ipath);
  295. return 0;
  296. err:
  297. printk(KERN_WARNING "btrfs: %s at logical %llu on dev "
  298. "%s, sector %llu, root %llu, inode %llu, offset %llu: path "
  299. "resolving failed with ret=%d\n", swarn->errstr,
  300. swarn->logical, swarn->dev->name,
  301. (unsigned long long)swarn->sector, root, inum, offset, ret);
  302. free_ipath(ipath);
  303. return 0;
  304. }
  305. static void scrub_print_warning(const char *errstr, struct scrub_block *sblock)
  306. {
  307. struct btrfs_device *dev = sblock->sdev->dev;
  308. struct btrfs_fs_info *fs_info = dev->dev_root->fs_info;
  309. struct btrfs_path *path;
  310. struct btrfs_key found_key;
  311. struct extent_buffer *eb;
  312. struct btrfs_extent_item *ei;
  313. struct scrub_warning swarn;
  314. u32 item_size;
  315. int ret;
  316. u64 ref_root;
  317. u8 ref_level;
  318. unsigned long ptr = 0;
  319. const int bufsize = 4096;
  320. u64 extent_item_pos;
  321. path = btrfs_alloc_path();
  322. swarn.scratch_buf = kmalloc(bufsize, GFP_NOFS);
  323. swarn.msg_buf = kmalloc(bufsize, GFP_NOFS);
  324. BUG_ON(sblock->page_count < 1);
  325. swarn.sector = (sblock->pagev[0].physical) >> 9;
  326. swarn.logical = sblock->pagev[0].logical;
  327. swarn.errstr = errstr;
  328. swarn.dev = dev;
  329. swarn.msg_bufsize = bufsize;
  330. swarn.scratch_bufsize = bufsize;
  331. if (!path || !swarn.scratch_buf || !swarn.msg_buf)
  332. goto out;
  333. ret = extent_from_logical(fs_info, swarn.logical, path, &found_key);
  334. if (ret < 0)
  335. goto out;
  336. extent_item_pos = swarn.logical - found_key.objectid;
  337. swarn.extent_item_size = found_key.offset;
  338. eb = path->nodes[0];
  339. ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
  340. item_size = btrfs_item_size_nr(eb, path->slots[0]);
  341. btrfs_release_path(path);
  342. if (ret & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  343. do {
  344. ret = tree_backref_for_extent(&ptr, eb, ei, item_size,
  345. &ref_root, &ref_level);
  346. printk(KERN_WARNING
  347. "btrfs: %s at logical %llu on dev %s, "
  348. "sector %llu: metadata %s (level %d) in tree "
  349. "%llu\n", errstr, swarn.logical, dev->name,
  350. (unsigned long long)swarn.sector,
  351. ref_level ? "node" : "leaf",
  352. ret < 0 ? -1 : ref_level,
  353. ret < 0 ? -1 : ref_root);
  354. } while (ret != 1);
  355. } else {
  356. swarn.path = path;
  357. iterate_extent_inodes(fs_info, found_key.objectid,
  358. extent_item_pos, 1,
  359. scrub_print_warning_inode, &swarn);
  360. }
  361. out:
  362. btrfs_free_path(path);
  363. kfree(swarn.scratch_buf);
  364. kfree(swarn.msg_buf);
  365. }
  366. static int scrub_fixup_readpage(u64 inum, u64 offset, u64 root, void *ctx)
  367. {
  368. struct page *page = NULL;
  369. unsigned long index;
  370. struct scrub_fixup_nodatasum *fixup = ctx;
  371. int ret;
  372. int corrected = 0;
  373. struct btrfs_key key;
  374. struct inode *inode = NULL;
  375. u64 end = offset + PAGE_SIZE - 1;
  376. struct btrfs_root *local_root;
  377. key.objectid = root;
  378. key.type = BTRFS_ROOT_ITEM_KEY;
  379. key.offset = (u64)-1;
  380. local_root = btrfs_read_fs_root_no_name(fixup->root->fs_info, &key);
  381. if (IS_ERR(local_root))
  382. return PTR_ERR(local_root);
  383. key.type = BTRFS_INODE_ITEM_KEY;
  384. key.objectid = inum;
  385. key.offset = 0;
  386. inode = btrfs_iget(fixup->root->fs_info->sb, &key, local_root, NULL);
  387. if (IS_ERR(inode))
  388. return PTR_ERR(inode);
  389. index = offset >> PAGE_CACHE_SHIFT;
  390. page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
  391. if (!page) {
  392. ret = -ENOMEM;
  393. goto out;
  394. }
  395. if (PageUptodate(page)) {
  396. struct btrfs_mapping_tree *map_tree;
  397. if (PageDirty(page)) {
  398. /*
  399. * we need to write the data to the defect sector. the
  400. * data that was in that sector is not in memory,
  401. * because the page was modified. we must not write the
  402. * modified page to that sector.
  403. *
  404. * TODO: what could be done here: wait for the delalloc
  405. * runner to write out that page (might involve
  406. * COW) and see whether the sector is still
  407. * referenced afterwards.
  408. *
  409. * For the meantime, we'll treat this error
  410. * incorrectable, although there is a chance that a
  411. * later scrub will find the bad sector again and that
  412. * there's no dirty page in memory, then.
  413. */
  414. ret = -EIO;
  415. goto out;
  416. }
  417. map_tree = &BTRFS_I(inode)->root->fs_info->mapping_tree;
  418. ret = repair_io_failure(map_tree, offset, PAGE_SIZE,
  419. fixup->logical, page,
  420. fixup->mirror_num);
  421. unlock_page(page);
  422. corrected = !ret;
  423. } else {
  424. /*
  425. * we need to get good data first. the general readpage path
  426. * will call repair_io_failure for us, we just have to make
  427. * sure we read the bad mirror.
  428. */
  429. ret = set_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
  430. EXTENT_DAMAGED, GFP_NOFS);
  431. if (ret) {
  432. /* set_extent_bits should give proper error */
  433. WARN_ON(ret > 0);
  434. if (ret > 0)
  435. ret = -EFAULT;
  436. goto out;
  437. }
  438. ret = extent_read_full_page(&BTRFS_I(inode)->io_tree, page,
  439. btrfs_get_extent,
  440. fixup->mirror_num);
  441. wait_on_page_locked(page);
  442. corrected = !test_range_bit(&BTRFS_I(inode)->io_tree, offset,
  443. end, EXTENT_DAMAGED, 0, NULL);
  444. if (!corrected)
  445. clear_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
  446. EXTENT_DAMAGED, GFP_NOFS);
  447. }
  448. out:
  449. if (page)
  450. put_page(page);
  451. if (inode)
  452. iput(inode);
  453. if (ret < 0)
  454. return ret;
  455. if (ret == 0 && corrected) {
  456. /*
  457. * we only need to call readpage for one of the inodes belonging
  458. * to this extent. so make iterate_extent_inodes stop
  459. */
  460. return 1;
  461. }
  462. return -EIO;
  463. }
  464. static void scrub_fixup_nodatasum(struct btrfs_work *work)
  465. {
  466. int ret;
  467. struct scrub_fixup_nodatasum *fixup;
  468. struct scrub_dev *sdev;
  469. struct btrfs_trans_handle *trans = NULL;
  470. struct btrfs_fs_info *fs_info;
  471. struct btrfs_path *path;
  472. int uncorrectable = 0;
  473. fixup = container_of(work, struct scrub_fixup_nodatasum, work);
  474. sdev = fixup->sdev;
  475. fs_info = fixup->root->fs_info;
  476. path = btrfs_alloc_path();
  477. if (!path) {
  478. spin_lock(&sdev->stat_lock);
  479. ++sdev->stat.malloc_errors;
  480. spin_unlock(&sdev->stat_lock);
  481. uncorrectable = 1;
  482. goto out;
  483. }
  484. trans = btrfs_join_transaction(fixup->root);
  485. if (IS_ERR(trans)) {
  486. uncorrectable = 1;
  487. goto out;
  488. }
  489. /*
  490. * the idea is to trigger a regular read through the standard path. we
  491. * read a page from the (failed) logical address by specifying the
  492. * corresponding copynum of the failed sector. thus, that readpage is
  493. * expected to fail.
  494. * that is the point where on-the-fly error correction will kick in
  495. * (once it's finished) and rewrite the failed sector if a good copy
  496. * can be found.
  497. */
  498. ret = iterate_inodes_from_logical(fixup->logical, fixup->root->fs_info,
  499. path, scrub_fixup_readpage,
  500. fixup);
  501. if (ret < 0) {
  502. uncorrectable = 1;
  503. goto out;
  504. }
  505. WARN_ON(ret != 1);
  506. spin_lock(&sdev->stat_lock);
  507. ++sdev->stat.corrected_errors;
  508. spin_unlock(&sdev->stat_lock);
  509. out:
  510. if (trans && !IS_ERR(trans))
  511. btrfs_end_transaction(trans, fixup->root);
  512. if (uncorrectable) {
  513. spin_lock(&sdev->stat_lock);
  514. ++sdev->stat.uncorrectable_errors;
  515. spin_unlock(&sdev->stat_lock);
  516. printk_ratelimited(KERN_ERR
  517. "btrfs: unable to fixup (nodatasum) error at logical %llu on dev %s\n",
  518. (unsigned long long)fixup->logical, sdev->dev->name);
  519. }
  520. btrfs_free_path(path);
  521. kfree(fixup);
  522. /* see caller why we're pretending to be paused in the scrub counters */
  523. mutex_lock(&fs_info->scrub_lock);
  524. atomic_dec(&fs_info->scrubs_running);
  525. atomic_dec(&fs_info->scrubs_paused);
  526. mutex_unlock(&fs_info->scrub_lock);
  527. atomic_dec(&sdev->fixup_cnt);
  528. wake_up(&fs_info->scrub_pause_wait);
  529. wake_up(&sdev->list_wait);
  530. }
  531. /*
  532. * scrub_handle_errored_block gets called when either verification of the
  533. * pages failed or the bio failed to read, e.g. with EIO. In the latter
  534. * case, this function handles all pages in the bio, even though only one
  535. * may be bad.
  536. * The goal of this function is to repair the errored block by using the
  537. * contents of one of the mirrors.
  538. */
  539. static int scrub_handle_errored_block(struct scrub_block *sblock_to_check)
  540. {
  541. struct scrub_dev *sdev = sblock_to_check->sdev;
  542. struct btrfs_fs_info *fs_info;
  543. u64 length;
  544. u64 logical;
  545. u64 generation;
  546. unsigned int failed_mirror_index;
  547. unsigned int is_metadata;
  548. unsigned int have_csum;
  549. u8 *csum;
  550. struct scrub_block *sblocks_for_recheck; /* holds one for each mirror */
  551. struct scrub_block *sblock_bad;
  552. int ret;
  553. int mirror_index;
  554. int page_num;
  555. int success;
  556. static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
  557. DEFAULT_RATELIMIT_BURST);
  558. BUG_ON(sblock_to_check->page_count < 1);
  559. fs_info = sdev->dev->dev_root->fs_info;
  560. length = sblock_to_check->page_count * PAGE_SIZE;
  561. logical = sblock_to_check->pagev[0].logical;
  562. generation = sblock_to_check->pagev[0].generation;
  563. BUG_ON(sblock_to_check->pagev[0].mirror_num < 1);
  564. failed_mirror_index = sblock_to_check->pagev[0].mirror_num - 1;
  565. is_metadata = !(sblock_to_check->pagev[0].flags &
  566. BTRFS_EXTENT_FLAG_DATA);
  567. have_csum = sblock_to_check->pagev[0].have_csum;
  568. csum = sblock_to_check->pagev[0].csum;
  569. /*
  570. * read all mirrors one after the other. This includes to
  571. * re-read the extent or metadata block that failed (that was
  572. * the cause that this fixup code is called) another time,
  573. * page by page this time in order to know which pages
  574. * caused I/O errors and which ones are good (for all mirrors).
  575. * It is the goal to handle the situation when more than one
  576. * mirror contains I/O errors, but the errors do not
  577. * overlap, i.e. the data can be repaired by selecting the
  578. * pages from those mirrors without I/O error on the
  579. * particular pages. One example (with blocks >= 2 * PAGE_SIZE)
  580. * would be that mirror #1 has an I/O error on the first page,
  581. * the second page is good, and mirror #2 has an I/O error on
  582. * the second page, but the first page is good.
  583. * Then the first page of the first mirror can be repaired by
  584. * taking the first page of the second mirror, and the
  585. * second page of the second mirror can be repaired by
  586. * copying the contents of the 2nd page of the 1st mirror.
  587. * One more note: if the pages of one mirror contain I/O
  588. * errors, the checksum cannot be verified. In order to get
  589. * the best data for repairing, the first attempt is to find
  590. * a mirror without I/O errors and with a validated checksum.
  591. * Only if this is not possible, the pages are picked from
  592. * mirrors with I/O errors without considering the checksum.
  593. * If the latter is the case, at the end, the checksum of the
  594. * repaired area is verified in order to correctly maintain
  595. * the statistics.
  596. */
  597. sblocks_for_recheck = kzalloc(BTRFS_MAX_MIRRORS *
  598. sizeof(*sblocks_for_recheck),
  599. GFP_NOFS);
  600. if (!sblocks_for_recheck) {
  601. spin_lock(&sdev->stat_lock);
  602. sdev->stat.malloc_errors++;
  603. sdev->stat.read_errors++;
  604. sdev->stat.uncorrectable_errors++;
  605. spin_unlock(&sdev->stat_lock);
  606. goto out;
  607. }
  608. /* setup the context, map the logical blocks and alloc the pages */
  609. ret = scrub_setup_recheck_block(sdev, &fs_info->mapping_tree, length,
  610. logical, sblocks_for_recheck);
  611. if (ret) {
  612. spin_lock(&sdev->stat_lock);
  613. sdev->stat.read_errors++;
  614. sdev->stat.uncorrectable_errors++;
  615. spin_unlock(&sdev->stat_lock);
  616. goto out;
  617. }
  618. BUG_ON(failed_mirror_index >= BTRFS_MAX_MIRRORS);
  619. sblock_bad = sblocks_for_recheck + failed_mirror_index;
  620. /* build and submit the bios for the failed mirror, check checksums */
  621. ret = scrub_recheck_block(fs_info, sblock_bad, is_metadata, have_csum,
  622. csum, generation, sdev->csum_size);
  623. if (ret) {
  624. spin_lock(&sdev->stat_lock);
  625. sdev->stat.read_errors++;
  626. sdev->stat.uncorrectable_errors++;
  627. spin_unlock(&sdev->stat_lock);
  628. goto out;
  629. }
  630. if (!sblock_bad->header_error && !sblock_bad->checksum_error &&
  631. sblock_bad->no_io_error_seen) {
  632. /*
  633. * the error disappeared after reading page by page, or
  634. * the area was part of a huge bio and other parts of the
  635. * bio caused I/O errors, or the block layer merged several
  636. * read requests into one and the error is caused by a
  637. * different bio (usually one of the two latter cases is
  638. * the cause)
  639. */
  640. spin_lock(&sdev->stat_lock);
  641. sdev->stat.unverified_errors++;
  642. spin_unlock(&sdev->stat_lock);
  643. goto out;
  644. }
  645. if (!sblock_bad->no_io_error_seen) {
  646. spin_lock(&sdev->stat_lock);
  647. sdev->stat.read_errors++;
  648. spin_unlock(&sdev->stat_lock);
  649. if (__ratelimit(&_rs))
  650. scrub_print_warning("i/o error", sblock_to_check);
  651. } else if (sblock_bad->checksum_error) {
  652. spin_lock(&sdev->stat_lock);
  653. sdev->stat.csum_errors++;
  654. spin_unlock(&sdev->stat_lock);
  655. if (__ratelimit(&_rs))
  656. scrub_print_warning("checksum error", sblock_to_check);
  657. } else if (sblock_bad->header_error) {
  658. spin_lock(&sdev->stat_lock);
  659. sdev->stat.verify_errors++;
  660. spin_unlock(&sdev->stat_lock);
  661. if (__ratelimit(&_rs))
  662. scrub_print_warning("checksum/header error",
  663. sblock_to_check);
  664. }
  665. if (sdev->readonly)
  666. goto did_not_correct_error;
  667. if (!is_metadata && !have_csum) {
  668. struct scrub_fixup_nodatasum *fixup_nodatasum;
  669. /*
  670. * !is_metadata and !have_csum, this means that the data
  671. * might not be COW'ed, that it might be modified
  672. * concurrently. The general strategy to work on the
  673. * commit root does not help in the case when COW is not
  674. * used.
  675. */
  676. fixup_nodatasum = kzalloc(sizeof(*fixup_nodatasum), GFP_NOFS);
  677. if (!fixup_nodatasum)
  678. goto did_not_correct_error;
  679. fixup_nodatasum->sdev = sdev;
  680. fixup_nodatasum->logical = logical;
  681. fixup_nodatasum->root = fs_info->extent_root;
  682. fixup_nodatasum->mirror_num = failed_mirror_index + 1;
  683. /*
  684. * increment scrubs_running to prevent cancel requests from
  685. * completing as long as a fixup worker is running. we must also
  686. * increment scrubs_paused to prevent deadlocking on pause
  687. * requests used for transactions commits (as the worker uses a
  688. * transaction context). it is safe to regard the fixup worker
  689. * as paused for all matters practical. effectively, we only
  690. * avoid cancellation requests from completing.
  691. */
  692. mutex_lock(&fs_info->scrub_lock);
  693. atomic_inc(&fs_info->scrubs_running);
  694. atomic_inc(&fs_info->scrubs_paused);
  695. mutex_unlock(&fs_info->scrub_lock);
  696. atomic_inc(&sdev->fixup_cnt);
  697. fixup_nodatasum->work.func = scrub_fixup_nodatasum;
  698. btrfs_queue_worker(&fs_info->scrub_workers,
  699. &fixup_nodatasum->work);
  700. goto out;
  701. }
  702. /*
  703. * now build and submit the bios for the other mirrors, check
  704. * checksums
  705. */
  706. for (mirror_index = 0;
  707. mirror_index < BTRFS_MAX_MIRRORS &&
  708. sblocks_for_recheck[mirror_index].page_count > 0;
  709. mirror_index++) {
  710. if (mirror_index == failed_mirror_index)
  711. continue;
  712. /* build and submit the bios, check checksums */
  713. ret = scrub_recheck_block(fs_info,
  714. sblocks_for_recheck + mirror_index,
  715. is_metadata, have_csum, csum,
  716. generation, sdev->csum_size);
  717. if (ret)
  718. goto did_not_correct_error;
  719. }
  720. /*
  721. * first try to pick the mirror which is completely without I/O
  722. * errors and also does not have a checksum error.
  723. * If one is found, and if a checksum is present, the full block
  724. * that is known to contain an error is rewritten. Afterwards
  725. * the block is known to be corrected.
  726. * If a mirror is found which is completely correct, and no
  727. * checksum is present, only those pages are rewritten that had
  728. * an I/O error in the block to be repaired, since it cannot be
  729. * determined, which copy of the other pages is better (and it
  730. * could happen otherwise that a correct page would be
  731. * overwritten by a bad one).
  732. */
  733. for (mirror_index = 0;
  734. mirror_index < BTRFS_MAX_MIRRORS &&
  735. sblocks_for_recheck[mirror_index].page_count > 0;
  736. mirror_index++) {
  737. struct scrub_block *sblock_other = sblocks_for_recheck +
  738. mirror_index;
  739. if (!sblock_other->header_error &&
  740. !sblock_other->checksum_error &&
  741. sblock_other->no_io_error_seen) {
  742. int force_write = is_metadata || have_csum;
  743. ret = scrub_repair_block_from_good_copy(sblock_bad,
  744. sblock_other,
  745. force_write);
  746. if (0 == ret)
  747. goto corrected_error;
  748. }
  749. }
  750. /*
  751. * in case of I/O errors in the area that is supposed to be
  752. * repaired, continue by picking good copies of those pages.
  753. * Select the good pages from mirrors to rewrite bad pages from
  754. * the area to fix. Afterwards verify the checksum of the block
  755. * that is supposed to be repaired. This verification step is
  756. * only done for the purpose of statistic counting and for the
  757. * final scrub report, whether errors remain.
  758. * A perfect algorithm could make use of the checksum and try
  759. * all possible combinations of pages from the different mirrors
  760. * until the checksum verification succeeds. For example, when
  761. * the 2nd page of mirror #1 faces I/O errors, and the 2nd page
  762. * of mirror #2 is readable but the final checksum test fails,
  763. * then the 2nd page of mirror #3 could be tried, whether now
  764. * the final checksum succeedes. But this would be a rare
  765. * exception and is therefore not implemented. At least it is
  766. * avoided that the good copy is overwritten.
  767. * A more useful improvement would be to pick the sectors
  768. * without I/O error based on sector sizes (512 bytes on legacy
  769. * disks) instead of on PAGE_SIZE. Then maybe 512 byte of one
  770. * mirror could be repaired by taking 512 byte of a different
  771. * mirror, even if other 512 byte sectors in the same PAGE_SIZE
  772. * area are unreadable.
  773. */
  774. /* can only fix I/O errors from here on */
  775. if (sblock_bad->no_io_error_seen)
  776. goto did_not_correct_error;
  777. success = 1;
  778. for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
  779. struct scrub_page *page_bad = sblock_bad->pagev + page_num;
  780. if (!page_bad->io_error)
  781. continue;
  782. for (mirror_index = 0;
  783. mirror_index < BTRFS_MAX_MIRRORS &&
  784. sblocks_for_recheck[mirror_index].page_count > 0;
  785. mirror_index++) {
  786. struct scrub_block *sblock_other = sblocks_for_recheck +
  787. mirror_index;
  788. struct scrub_page *page_other = sblock_other->pagev +
  789. page_num;
  790. if (!page_other->io_error) {
  791. ret = scrub_repair_page_from_good_copy(
  792. sblock_bad, sblock_other, page_num, 0);
  793. if (0 == ret) {
  794. page_bad->io_error = 0;
  795. break; /* succeeded for this page */
  796. }
  797. }
  798. }
  799. if (page_bad->io_error) {
  800. /* did not find a mirror to copy the page from */
  801. success = 0;
  802. }
  803. }
  804. if (success) {
  805. if (is_metadata || have_csum) {
  806. /*
  807. * need to verify the checksum now that all
  808. * sectors on disk are repaired (the write
  809. * request for data to be repaired is on its way).
  810. * Just be lazy and use scrub_recheck_block()
  811. * which re-reads the data before the checksum
  812. * is verified, but most likely the data comes out
  813. * of the page cache.
  814. */
  815. ret = scrub_recheck_block(fs_info, sblock_bad,
  816. is_metadata, have_csum, csum,
  817. generation, sdev->csum_size);
  818. if (!ret && !sblock_bad->header_error &&
  819. !sblock_bad->checksum_error &&
  820. sblock_bad->no_io_error_seen)
  821. goto corrected_error;
  822. else
  823. goto did_not_correct_error;
  824. } else {
  825. corrected_error:
  826. spin_lock(&sdev->stat_lock);
  827. sdev->stat.corrected_errors++;
  828. spin_unlock(&sdev->stat_lock);
  829. printk_ratelimited(KERN_ERR
  830. "btrfs: fixed up error at logical %llu on dev %s\n",
  831. (unsigned long long)logical, sdev->dev->name);
  832. }
  833. } else {
  834. did_not_correct_error:
  835. spin_lock(&sdev->stat_lock);
  836. sdev->stat.uncorrectable_errors++;
  837. spin_unlock(&sdev->stat_lock);
  838. printk_ratelimited(KERN_ERR
  839. "btrfs: unable to fixup (regular) error at logical %llu on dev %s\n",
  840. (unsigned long long)logical, sdev->dev->name);
  841. }
  842. out:
  843. if (sblocks_for_recheck) {
  844. for (mirror_index = 0; mirror_index < BTRFS_MAX_MIRRORS;
  845. mirror_index++) {
  846. struct scrub_block *sblock = sblocks_for_recheck +
  847. mirror_index;
  848. int page_index;
  849. for (page_index = 0; page_index < SCRUB_PAGES_PER_BIO;
  850. page_index++)
  851. if (sblock->pagev[page_index].page)
  852. __free_page(
  853. sblock->pagev[page_index].page);
  854. }
  855. kfree(sblocks_for_recheck);
  856. }
  857. return 0;
  858. }
  859. static int scrub_setup_recheck_block(struct scrub_dev *sdev,
  860. struct btrfs_mapping_tree *map_tree,
  861. u64 length, u64 logical,
  862. struct scrub_block *sblocks_for_recheck)
  863. {
  864. int page_index;
  865. int mirror_index;
  866. int ret;
  867. /*
  868. * note: the three members sdev, ref_count and outstanding_pages
  869. * are not used (and not set) in the blocks that are used for
  870. * the recheck procedure
  871. */
  872. page_index = 0;
  873. while (length > 0) {
  874. u64 sublen = min_t(u64, length, PAGE_SIZE);
  875. u64 mapped_length = sublen;
  876. struct btrfs_bio *bbio = NULL;
  877. /*
  878. * with a length of PAGE_SIZE, each returned stripe
  879. * represents one mirror
  880. */
  881. ret = btrfs_map_block(map_tree, WRITE, logical, &mapped_length,
  882. &bbio, 0);
  883. if (ret || !bbio || mapped_length < sublen) {
  884. kfree(bbio);
  885. return -EIO;
  886. }
  887. BUG_ON(page_index >= SCRUB_PAGES_PER_BIO);
  888. for (mirror_index = 0; mirror_index < (int)bbio->num_stripes;
  889. mirror_index++) {
  890. struct scrub_block *sblock;
  891. struct scrub_page *page;
  892. if (mirror_index >= BTRFS_MAX_MIRRORS)
  893. continue;
  894. sblock = sblocks_for_recheck + mirror_index;
  895. page = sblock->pagev + page_index;
  896. page->logical = logical;
  897. page->physical = bbio->stripes[mirror_index].physical;
  898. page->bdev = bbio->stripes[mirror_index].dev->bdev;
  899. page->mirror_num = mirror_index + 1;
  900. page->page = alloc_page(GFP_NOFS);
  901. if (!page->page) {
  902. spin_lock(&sdev->stat_lock);
  903. sdev->stat.malloc_errors++;
  904. spin_unlock(&sdev->stat_lock);
  905. return -ENOMEM;
  906. }
  907. sblock->page_count++;
  908. }
  909. kfree(bbio);
  910. length -= sublen;
  911. logical += sublen;
  912. page_index++;
  913. }
  914. return 0;
  915. }
  916. /*
  917. * this function will check the on disk data for checksum errors, header
  918. * errors and read I/O errors. If any I/O errors happen, the exact pages
  919. * which are errored are marked as being bad. The goal is to enable scrub
  920. * to take those pages that are not errored from all the mirrors so that
  921. * the pages that are errored in the just handled mirror can be repaired.
  922. */
  923. static int scrub_recheck_block(struct btrfs_fs_info *fs_info,
  924. struct scrub_block *sblock, int is_metadata,
  925. int have_csum, u8 *csum, u64 generation,
  926. u16 csum_size)
  927. {
  928. int page_num;
  929. sblock->no_io_error_seen = 1;
  930. sblock->header_error = 0;
  931. sblock->checksum_error = 0;
  932. for (page_num = 0; page_num < sblock->page_count; page_num++) {
  933. struct bio *bio;
  934. int ret;
  935. struct scrub_page *page = sblock->pagev + page_num;
  936. DECLARE_COMPLETION_ONSTACK(complete);
  937. BUG_ON(!page->page);
  938. bio = bio_alloc(GFP_NOFS, 1);
  939. bio->bi_bdev = page->bdev;
  940. bio->bi_sector = page->physical >> 9;
  941. bio->bi_end_io = scrub_complete_bio_end_io;
  942. bio->bi_private = &complete;
  943. ret = bio_add_page(bio, page->page, PAGE_SIZE, 0);
  944. if (PAGE_SIZE != ret) {
  945. bio_put(bio);
  946. return -EIO;
  947. }
  948. btrfsic_submit_bio(READ, bio);
  949. /* this will also unplug the queue */
  950. wait_for_completion(&complete);
  951. page->io_error = !test_bit(BIO_UPTODATE, &bio->bi_flags);
  952. if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
  953. sblock->no_io_error_seen = 0;
  954. bio_put(bio);
  955. }
  956. if (sblock->no_io_error_seen)
  957. scrub_recheck_block_checksum(fs_info, sblock, is_metadata,
  958. have_csum, csum, generation,
  959. csum_size);
  960. return 0;
  961. }
  962. static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
  963. struct scrub_block *sblock,
  964. int is_metadata, int have_csum,
  965. const u8 *csum, u64 generation,
  966. u16 csum_size)
  967. {
  968. int page_num;
  969. u8 calculated_csum[BTRFS_CSUM_SIZE];
  970. u32 crc = ~(u32)0;
  971. struct btrfs_root *root = fs_info->extent_root;
  972. void *mapped_buffer;
  973. BUG_ON(!sblock->pagev[0].page);
  974. if (is_metadata) {
  975. struct btrfs_header *h;
  976. mapped_buffer = kmap_atomic(sblock->pagev[0].page);
  977. h = (struct btrfs_header *)mapped_buffer;
  978. if (sblock->pagev[0].logical != le64_to_cpu(h->bytenr) ||
  979. generation != le64_to_cpu(h->generation) ||
  980. memcmp(h->fsid, fs_info->fsid, BTRFS_UUID_SIZE) ||
  981. memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
  982. BTRFS_UUID_SIZE))
  983. sblock->header_error = 1;
  984. csum = h->csum;
  985. } else {
  986. if (!have_csum)
  987. return;
  988. mapped_buffer = kmap_atomic(sblock->pagev[0].page);
  989. }
  990. for (page_num = 0;;) {
  991. if (page_num == 0 && is_metadata)
  992. crc = btrfs_csum_data(root,
  993. ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE,
  994. crc, PAGE_SIZE - BTRFS_CSUM_SIZE);
  995. else
  996. crc = btrfs_csum_data(root, mapped_buffer, crc,
  997. PAGE_SIZE);
  998. kunmap_atomic(mapped_buffer);
  999. page_num++;
  1000. if (page_num >= sblock->page_count)
  1001. break;
  1002. BUG_ON(!sblock->pagev[page_num].page);
  1003. mapped_buffer = kmap_atomic(sblock->pagev[page_num].page);
  1004. }
  1005. btrfs_csum_final(crc, calculated_csum);
  1006. if (memcmp(calculated_csum, csum, csum_size))
  1007. sblock->checksum_error = 1;
  1008. }
  1009. static void scrub_complete_bio_end_io(struct bio *bio, int err)
  1010. {
  1011. complete((struct completion *)bio->bi_private);
  1012. }
  1013. static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
  1014. struct scrub_block *sblock_good,
  1015. int force_write)
  1016. {
  1017. int page_num;
  1018. int ret = 0;
  1019. for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
  1020. int ret_sub;
  1021. ret_sub = scrub_repair_page_from_good_copy(sblock_bad,
  1022. sblock_good,
  1023. page_num,
  1024. force_write);
  1025. if (ret_sub)
  1026. ret = ret_sub;
  1027. }
  1028. return ret;
  1029. }
  1030. static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
  1031. struct scrub_block *sblock_good,
  1032. int page_num, int force_write)
  1033. {
  1034. struct scrub_page *page_bad = sblock_bad->pagev + page_num;
  1035. struct scrub_page *page_good = sblock_good->pagev + page_num;
  1036. BUG_ON(sblock_bad->pagev[page_num].page == NULL);
  1037. BUG_ON(sblock_good->pagev[page_num].page == NULL);
  1038. if (force_write || sblock_bad->header_error ||
  1039. sblock_bad->checksum_error || page_bad->io_error) {
  1040. struct bio *bio;
  1041. int ret;
  1042. DECLARE_COMPLETION_ONSTACK(complete);
  1043. bio = bio_alloc(GFP_NOFS, 1);
  1044. bio->bi_bdev = page_bad->bdev;
  1045. bio->bi_sector = page_bad->physical >> 9;
  1046. bio->bi_end_io = scrub_complete_bio_end_io;
  1047. bio->bi_private = &complete;
  1048. ret = bio_add_page(bio, page_good->page, PAGE_SIZE, 0);
  1049. if (PAGE_SIZE != ret) {
  1050. bio_put(bio);
  1051. return -EIO;
  1052. }
  1053. btrfsic_submit_bio(WRITE, bio);
  1054. /* this will also unplug the queue */
  1055. wait_for_completion(&complete);
  1056. bio_put(bio);
  1057. }
  1058. return 0;
  1059. }
  1060. static void scrub_checksum(struct scrub_block *sblock)
  1061. {
  1062. u64 flags;
  1063. int ret;
  1064. BUG_ON(sblock->page_count < 1);
  1065. flags = sblock->pagev[0].flags;
  1066. ret = 0;
  1067. if (flags & BTRFS_EXTENT_FLAG_DATA)
  1068. ret = scrub_checksum_data(sblock);
  1069. else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
  1070. ret = scrub_checksum_tree_block(sblock);
  1071. else if (flags & BTRFS_EXTENT_FLAG_SUPER)
  1072. (void)scrub_checksum_super(sblock);
  1073. else
  1074. WARN_ON(1);
  1075. if (ret)
  1076. scrub_handle_errored_block(sblock);
  1077. }
  1078. static int scrub_checksum_data(struct scrub_block *sblock)
  1079. {
  1080. struct scrub_dev *sdev = sblock->sdev;
  1081. u8 csum[BTRFS_CSUM_SIZE];
  1082. u8 *on_disk_csum;
  1083. struct page *page;
  1084. void *buffer;
  1085. u32 crc = ~(u32)0;
  1086. int fail = 0;
  1087. struct btrfs_root *root = sdev->dev->dev_root;
  1088. u64 len;
  1089. int index;
  1090. BUG_ON(sblock->page_count < 1);
  1091. if (!sblock->pagev[0].have_csum)
  1092. return 0;
  1093. on_disk_csum = sblock->pagev[0].csum;
  1094. page = sblock->pagev[0].page;
  1095. buffer = kmap_atomic(page);
  1096. len = sdev->sectorsize;
  1097. index = 0;
  1098. for (;;) {
  1099. u64 l = min_t(u64, len, PAGE_SIZE);
  1100. crc = btrfs_csum_data(root, buffer, crc, l);
  1101. kunmap_atomic(buffer);
  1102. len -= l;
  1103. if (len == 0)
  1104. break;
  1105. index++;
  1106. BUG_ON(index >= sblock->page_count);
  1107. BUG_ON(!sblock->pagev[index].page);
  1108. page = sblock->pagev[index].page;
  1109. buffer = kmap_atomic(page);
  1110. }
  1111. btrfs_csum_final(crc, csum);
  1112. if (memcmp(csum, on_disk_csum, sdev->csum_size))
  1113. fail = 1;
  1114. if (fail) {
  1115. spin_lock(&sdev->stat_lock);
  1116. ++sdev->stat.csum_errors;
  1117. spin_unlock(&sdev->stat_lock);
  1118. }
  1119. return fail;
  1120. }
  1121. static int scrub_checksum_tree_block(struct scrub_block *sblock)
  1122. {
  1123. struct scrub_dev *sdev = sblock->sdev;
  1124. struct btrfs_header *h;
  1125. struct btrfs_root *root = sdev->dev->dev_root;
  1126. struct btrfs_fs_info *fs_info = root->fs_info;
  1127. u8 calculated_csum[BTRFS_CSUM_SIZE];
  1128. u8 on_disk_csum[BTRFS_CSUM_SIZE];
  1129. struct page *page;
  1130. void *mapped_buffer;
  1131. u64 mapped_size;
  1132. void *p;
  1133. u32 crc = ~(u32)0;
  1134. int fail = 0;
  1135. int crc_fail = 0;
  1136. u64 len;
  1137. int index;
  1138. BUG_ON(sblock->page_count < 1);
  1139. page = sblock->pagev[0].page;
  1140. mapped_buffer = kmap_atomic(page);
  1141. h = (struct btrfs_header *)mapped_buffer;
  1142. memcpy(on_disk_csum, h->csum, sdev->csum_size);
  1143. /*
  1144. * we don't use the getter functions here, as we
  1145. * a) don't have an extent buffer and
  1146. * b) the page is already kmapped
  1147. */
  1148. if (sblock->pagev[0].logical != le64_to_cpu(h->bytenr))
  1149. ++fail;
  1150. if (sblock->pagev[0].generation != le64_to_cpu(h->generation))
  1151. ++fail;
  1152. if (memcmp(h->fsid, fs_info->fsid, BTRFS_UUID_SIZE))
  1153. ++fail;
  1154. if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
  1155. BTRFS_UUID_SIZE))
  1156. ++fail;
  1157. BUG_ON(sdev->nodesize != sdev->leafsize);
  1158. len = sdev->nodesize - BTRFS_CSUM_SIZE;
  1159. mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
  1160. p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
  1161. index = 0;
  1162. for (;;) {
  1163. u64 l = min_t(u64, len, mapped_size);
  1164. crc = btrfs_csum_data(root, p, crc, l);
  1165. kunmap_atomic(mapped_buffer);
  1166. len -= l;
  1167. if (len == 0)
  1168. break;
  1169. index++;
  1170. BUG_ON(index >= sblock->page_count);
  1171. BUG_ON(!sblock->pagev[index].page);
  1172. page = sblock->pagev[index].page;
  1173. mapped_buffer = kmap_atomic(page);
  1174. mapped_size = PAGE_SIZE;
  1175. p = mapped_buffer;
  1176. }
  1177. btrfs_csum_final(crc, calculated_csum);
  1178. if (memcmp(calculated_csum, on_disk_csum, sdev->csum_size))
  1179. ++crc_fail;
  1180. if (crc_fail || fail) {
  1181. spin_lock(&sdev->stat_lock);
  1182. if (crc_fail)
  1183. ++sdev->stat.csum_errors;
  1184. if (fail)
  1185. ++sdev->stat.verify_errors;
  1186. spin_unlock(&sdev->stat_lock);
  1187. }
  1188. return fail || crc_fail;
  1189. }
  1190. static int scrub_checksum_super(struct scrub_block *sblock)
  1191. {
  1192. struct btrfs_super_block *s;
  1193. struct scrub_dev *sdev = sblock->sdev;
  1194. struct btrfs_root *root = sdev->dev->dev_root;
  1195. struct btrfs_fs_info *fs_info = root->fs_info;
  1196. u8 calculated_csum[BTRFS_CSUM_SIZE];
  1197. u8 on_disk_csum[BTRFS_CSUM_SIZE];
  1198. struct page *page;
  1199. void *mapped_buffer;
  1200. u64 mapped_size;
  1201. void *p;
  1202. u32 crc = ~(u32)0;
  1203. int fail = 0;
  1204. u64 len;
  1205. int index;
  1206. BUG_ON(sblock->page_count < 1);
  1207. page = sblock->pagev[0].page;
  1208. mapped_buffer = kmap_atomic(page);
  1209. s = (struct btrfs_super_block *)mapped_buffer;
  1210. memcpy(on_disk_csum, s->csum, sdev->csum_size);
  1211. if (sblock->pagev[0].logical != le64_to_cpu(s->bytenr))
  1212. ++fail;
  1213. if (sblock->pagev[0].generation != le64_to_cpu(s->generation))
  1214. ++fail;
  1215. if (memcmp(s->fsid, fs_info->fsid, BTRFS_UUID_SIZE))
  1216. ++fail;
  1217. len = BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE;
  1218. mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
  1219. p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
  1220. index = 0;
  1221. for (;;) {
  1222. u64 l = min_t(u64, len, mapped_size);
  1223. crc = btrfs_csum_data(root, p, crc, l);
  1224. kunmap_atomic(mapped_buffer);
  1225. len -= l;
  1226. if (len == 0)
  1227. break;
  1228. index++;
  1229. BUG_ON(index >= sblock->page_count);
  1230. BUG_ON(!sblock->pagev[index].page);
  1231. page = sblock->pagev[index].page;
  1232. mapped_buffer = kmap_atomic(page);
  1233. mapped_size = PAGE_SIZE;
  1234. p = mapped_buffer;
  1235. }
  1236. btrfs_csum_final(crc, calculated_csum);
  1237. if (memcmp(calculated_csum, on_disk_csum, sdev->csum_size))
  1238. ++fail;
  1239. if (fail) {
  1240. /*
  1241. * if we find an error in a super block, we just report it.
  1242. * They will get written with the next transaction commit
  1243. * anyway
  1244. */
  1245. spin_lock(&sdev->stat_lock);
  1246. ++sdev->stat.super_errors;
  1247. spin_unlock(&sdev->stat_lock);
  1248. }
  1249. return fail;
  1250. }
  1251. static void scrub_block_get(struct scrub_block *sblock)
  1252. {
  1253. atomic_inc(&sblock->ref_count);
  1254. }
  1255. static void scrub_block_put(struct scrub_block *sblock)
  1256. {
  1257. if (atomic_dec_and_test(&sblock->ref_count)) {
  1258. int i;
  1259. for (i = 0; i < sblock->page_count; i++)
  1260. if (sblock->pagev[i].page)
  1261. __free_page(sblock->pagev[i].page);
  1262. kfree(sblock);
  1263. }
  1264. }
  1265. static void scrub_submit(struct scrub_dev *sdev)
  1266. {
  1267. struct scrub_bio *sbio;
  1268. if (sdev->curr == -1)
  1269. return;
  1270. sbio = sdev->bios[sdev->curr];
  1271. sdev->curr = -1;
  1272. atomic_inc(&sdev->in_flight);
  1273. btrfsic_submit_bio(READ, sbio->bio);
  1274. }
  1275. static int scrub_add_page_to_bio(struct scrub_dev *sdev,
  1276. struct scrub_page *spage)
  1277. {
  1278. struct scrub_block *sblock = spage->sblock;
  1279. struct scrub_bio *sbio;
  1280. int ret;
  1281. again:
  1282. /*
  1283. * grab a fresh bio or wait for one to become available
  1284. */
  1285. while (sdev->curr == -1) {
  1286. spin_lock(&sdev->list_lock);
  1287. sdev->curr = sdev->first_free;
  1288. if (sdev->curr != -1) {
  1289. sdev->first_free = sdev->bios[sdev->curr]->next_free;
  1290. sdev->bios[sdev->curr]->next_free = -1;
  1291. sdev->bios[sdev->curr]->page_count = 0;
  1292. spin_unlock(&sdev->list_lock);
  1293. } else {
  1294. spin_unlock(&sdev->list_lock);
  1295. wait_event(sdev->list_wait, sdev->first_free != -1);
  1296. }
  1297. }
  1298. sbio = sdev->bios[sdev->curr];
  1299. if (sbio->page_count == 0) {
  1300. struct bio *bio;
  1301. sbio->physical = spage->physical;
  1302. sbio->logical = spage->logical;
  1303. bio = sbio->bio;
  1304. if (!bio) {
  1305. bio = bio_alloc(GFP_NOFS, sdev->pages_per_bio);
  1306. if (!bio)
  1307. return -ENOMEM;
  1308. sbio->bio = bio;
  1309. }
  1310. bio->bi_private = sbio;
  1311. bio->bi_end_io = scrub_bio_end_io;
  1312. bio->bi_bdev = sdev->dev->bdev;
  1313. bio->bi_sector = spage->physical >> 9;
  1314. sbio->err = 0;
  1315. } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
  1316. spage->physical ||
  1317. sbio->logical + sbio->page_count * PAGE_SIZE !=
  1318. spage->logical) {
  1319. scrub_submit(sdev);
  1320. goto again;
  1321. }
  1322. sbio->pagev[sbio->page_count] = spage;
  1323. ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
  1324. if (ret != PAGE_SIZE) {
  1325. if (sbio->page_count < 1) {
  1326. bio_put(sbio->bio);
  1327. sbio->bio = NULL;
  1328. return -EIO;
  1329. }
  1330. scrub_submit(sdev);
  1331. goto again;
  1332. }
  1333. scrub_block_get(sblock); /* one for the added page */
  1334. atomic_inc(&sblock->outstanding_pages);
  1335. sbio->page_count++;
  1336. if (sbio->page_count == sdev->pages_per_bio)
  1337. scrub_submit(sdev);
  1338. return 0;
  1339. }
  1340. static int scrub_pages(struct scrub_dev *sdev, u64 logical, u64 len,
  1341. u64 physical, u64 flags, u64 gen, int mirror_num,
  1342. u8 *csum, int force)
  1343. {
  1344. struct scrub_block *sblock;
  1345. int index;
  1346. sblock = kzalloc(sizeof(*sblock), GFP_NOFS);
  1347. if (!sblock) {
  1348. spin_lock(&sdev->stat_lock);
  1349. sdev->stat.malloc_errors++;
  1350. spin_unlock(&sdev->stat_lock);
  1351. return -ENOMEM;
  1352. }
  1353. /* one ref inside this function, plus one for each page later on */
  1354. atomic_set(&sblock->ref_count, 1);
  1355. sblock->sdev = sdev;
  1356. sblock->no_io_error_seen = 1;
  1357. for (index = 0; len > 0; index++) {
  1358. struct scrub_page *spage = sblock->pagev + index;
  1359. u64 l = min_t(u64, len, PAGE_SIZE);
  1360. BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
  1361. spage->page = alloc_page(GFP_NOFS);
  1362. if (!spage->page) {
  1363. spin_lock(&sdev->stat_lock);
  1364. sdev->stat.malloc_errors++;
  1365. spin_unlock(&sdev->stat_lock);
  1366. while (index > 0) {
  1367. index--;
  1368. __free_page(sblock->pagev[index].page);
  1369. }
  1370. kfree(sblock);
  1371. return -ENOMEM;
  1372. }
  1373. spage->sblock = sblock;
  1374. spage->bdev = sdev->dev->bdev;
  1375. spage->flags = flags;
  1376. spage->generation = gen;
  1377. spage->logical = logical;
  1378. spage->physical = physical;
  1379. spage->mirror_num = mirror_num;
  1380. if (csum) {
  1381. spage->have_csum = 1;
  1382. memcpy(spage->csum, csum, sdev->csum_size);
  1383. } else {
  1384. spage->have_csum = 0;
  1385. }
  1386. sblock->page_count++;
  1387. len -= l;
  1388. logical += l;
  1389. physical += l;
  1390. }
  1391. BUG_ON(sblock->page_count == 0);
  1392. for (index = 0; index < sblock->page_count; index++) {
  1393. struct scrub_page *spage = sblock->pagev + index;
  1394. int ret;
  1395. ret = scrub_add_page_to_bio(sdev, spage);
  1396. if (ret) {
  1397. scrub_block_put(sblock);
  1398. return ret;
  1399. }
  1400. }
  1401. if (force)
  1402. scrub_submit(sdev);
  1403. /* last one frees, either here or in bio completion for last page */
  1404. scrub_block_put(sblock);
  1405. return 0;
  1406. }
  1407. static void scrub_bio_end_io(struct bio *bio, int err)
  1408. {
  1409. struct scrub_bio *sbio = bio->bi_private;
  1410. struct scrub_dev *sdev = sbio->sdev;
  1411. struct btrfs_fs_info *fs_info = sdev->dev->dev_root->fs_info;
  1412. sbio->err = err;
  1413. sbio->bio = bio;
  1414. btrfs_queue_worker(&fs_info->scrub_workers, &sbio->work);
  1415. }
  1416. static void scrub_bio_end_io_worker(struct btrfs_work *work)
  1417. {
  1418. struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
  1419. struct scrub_dev *sdev = sbio->sdev;
  1420. int i;
  1421. BUG_ON(sbio->page_count > SCRUB_PAGES_PER_BIO);
  1422. if (sbio->err) {
  1423. for (i = 0; i < sbio->page_count; i++) {
  1424. struct scrub_page *spage = sbio->pagev[i];
  1425. spage->io_error = 1;
  1426. spage->sblock->no_io_error_seen = 0;
  1427. }
  1428. }
  1429. /* now complete the scrub_block items that have all pages completed */
  1430. for (i = 0; i < sbio->page_count; i++) {
  1431. struct scrub_page *spage = sbio->pagev[i];
  1432. struct scrub_block *sblock = spage->sblock;
  1433. if (atomic_dec_and_test(&sblock->outstanding_pages))
  1434. scrub_block_complete(sblock);
  1435. scrub_block_put(sblock);
  1436. }
  1437. if (sbio->err) {
  1438. /* what is this good for??? */
  1439. sbio->bio->bi_flags &= ~(BIO_POOL_MASK - 1);
  1440. sbio->bio->bi_flags |= 1 << BIO_UPTODATE;
  1441. sbio->bio->bi_phys_segments = 0;
  1442. sbio->bio->bi_idx = 0;
  1443. for (i = 0; i < sbio->page_count; i++) {
  1444. struct bio_vec *bi;
  1445. bi = &sbio->bio->bi_io_vec[i];
  1446. bi->bv_offset = 0;
  1447. bi->bv_len = PAGE_SIZE;
  1448. }
  1449. }
  1450. bio_put(sbio->bio);
  1451. sbio->bio = NULL;
  1452. spin_lock(&sdev->list_lock);
  1453. sbio->next_free = sdev->first_free;
  1454. sdev->first_free = sbio->index;
  1455. spin_unlock(&sdev->list_lock);
  1456. atomic_dec(&sdev->in_flight);
  1457. wake_up(&sdev->list_wait);
  1458. }
  1459. static void scrub_block_complete(struct scrub_block *sblock)
  1460. {
  1461. if (!sblock->no_io_error_seen)
  1462. scrub_handle_errored_block(sblock);
  1463. else
  1464. scrub_checksum(sblock);
  1465. }
  1466. static int scrub_find_csum(struct scrub_dev *sdev, u64 logical, u64 len,
  1467. u8 *csum)
  1468. {
  1469. struct btrfs_ordered_sum *sum = NULL;
  1470. int ret = 0;
  1471. unsigned long i;
  1472. unsigned long num_sectors;
  1473. while (!list_empty(&sdev->csum_list)) {
  1474. sum = list_first_entry(&sdev->csum_list,
  1475. struct btrfs_ordered_sum, list);
  1476. if (sum->bytenr > logical)
  1477. return 0;
  1478. if (sum->bytenr + sum->len > logical)
  1479. break;
  1480. ++sdev->stat.csum_discards;
  1481. list_del(&sum->list);
  1482. kfree(sum);
  1483. sum = NULL;
  1484. }
  1485. if (!sum)
  1486. return 0;
  1487. num_sectors = sum->len / sdev->sectorsize;
  1488. for (i = 0; i < num_sectors; ++i) {
  1489. if (sum->sums[i].bytenr == logical) {
  1490. memcpy(csum, &sum->sums[i].sum, sdev->csum_size);
  1491. ret = 1;
  1492. break;
  1493. }
  1494. }
  1495. if (ret && i == num_sectors - 1) {
  1496. list_del(&sum->list);
  1497. kfree(sum);
  1498. }
  1499. return ret;
  1500. }
  1501. /* scrub extent tries to collect up to 64 kB for each bio */
  1502. static int scrub_extent(struct scrub_dev *sdev, u64 logical, u64 len,
  1503. u64 physical, u64 flags, u64 gen, int mirror_num)
  1504. {
  1505. int ret;
  1506. u8 csum[BTRFS_CSUM_SIZE];
  1507. u32 blocksize;
  1508. if (flags & BTRFS_EXTENT_FLAG_DATA) {
  1509. blocksize = sdev->sectorsize;
  1510. spin_lock(&sdev->stat_lock);
  1511. sdev->stat.data_extents_scrubbed++;
  1512. sdev->stat.data_bytes_scrubbed += len;
  1513. spin_unlock(&sdev->stat_lock);
  1514. } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  1515. BUG_ON(sdev->nodesize != sdev->leafsize);
  1516. blocksize = sdev->nodesize;
  1517. spin_lock(&sdev->stat_lock);
  1518. sdev->stat.tree_extents_scrubbed++;
  1519. sdev->stat.tree_bytes_scrubbed += len;
  1520. spin_unlock(&sdev->stat_lock);
  1521. } else {
  1522. blocksize = sdev->sectorsize;
  1523. BUG_ON(1);
  1524. }
  1525. while (len) {
  1526. u64 l = min_t(u64, len, blocksize);
  1527. int have_csum = 0;
  1528. if (flags & BTRFS_EXTENT_FLAG_DATA) {
  1529. /* push csums to sbio */
  1530. have_csum = scrub_find_csum(sdev, logical, l, csum);
  1531. if (have_csum == 0)
  1532. ++sdev->stat.no_csum;
  1533. }
  1534. ret = scrub_pages(sdev, logical, l, physical, flags, gen,
  1535. mirror_num, have_csum ? csum : NULL, 0);
  1536. if (ret)
  1537. return ret;
  1538. len -= l;
  1539. logical += l;
  1540. physical += l;
  1541. }
  1542. return 0;
  1543. }
  1544. static noinline_for_stack int scrub_stripe(struct scrub_dev *sdev,
  1545. struct map_lookup *map, int num, u64 base, u64 length)
  1546. {
  1547. struct btrfs_path *path;
  1548. struct btrfs_fs_info *fs_info = sdev->dev->dev_root->fs_info;
  1549. struct btrfs_root *root = fs_info->extent_root;
  1550. struct btrfs_root *csum_root = fs_info->csum_root;
  1551. struct btrfs_extent_item *extent;
  1552. struct blk_plug plug;
  1553. u64 flags;
  1554. int ret;
  1555. int slot;
  1556. int i;
  1557. u64 nstripes;
  1558. struct extent_buffer *l;
  1559. struct btrfs_key key;
  1560. u64 physical;
  1561. u64 logical;
  1562. u64 generation;
  1563. int mirror_num;
  1564. struct reada_control *reada1;
  1565. struct reada_control *reada2;
  1566. struct btrfs_key key_start;
  1567. struct btrfs_key key_end;
  1568. u64 increment = map->stripe_len;
  1569. u64 offset;
  1570. nstripes = length;
  1571. offset = 0;
  1572. do_div(nstripes, map->stripe_len);
  1573. if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
  1574. offset = map->stripe_len * num;
  1575. increment = map->stripe_len * map->num_stripes;
  1576. mirror_num = 1;
  1577. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  1578. int factor = map->num_stripes / map->sub_stripes;
  1579. offset = map->stripe_len * (num / map->sub_stripes);
  1580. increment = map->stripe_len * factor;
  1581. mirror_num = num % map->sub_stripes + 1;
  1582. } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
  1583. increment = map->stripe_len;
  1584. mirror_num = num % map->num_stripes + 1;
  1585. } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
  1586. increment = map->stripe_len;
  1587. mirror_num = num % map->num_stripes + 1;
  1588. } else {
  1589. increment = map->stripe_len;
  1590. mirror_num = 1;
  1591. }
  1592. path = btrfs_alloc_path();
  1593. if (!path)
  1594. return -ENOMEM;
  1595. /*
  1596. * work on commit root. The related disk blocks are static as
  1597. * long as COW is applied. This means, it is save to rewrite
  1598. * them to repair disk errors without any race conditions
  1599. */
  1600. path->search_commit_root = 1;
  1601. path->skip_locking = 1;
  1602. /*
  1603. * trigger the readahead for extent tree csum tree and wait for
  1604. * completion. During readahead, the scrub is officially paused
  1605. * to not hold off transaction commits
  1606. */
  1607. logical = base + offset;
  1608. wait_event(sdev->list_wait,
  1609. atomic_read(&sdev->in_flight) == 0);
  1610. atomic_inc(&fs_info->scrubs_paused);
  1611. wake_up(&fs_info->scrub_pause_wait);
  1612. /* FIXME it might be better to start readahead at commit root */
  1613. key_start.objectid = logical;
  1614. key_start.type = BTRFS_EXTENT_ITEM_KEY;
  1615. key_start.offset = (u64)0;
  1616. key_end.objectid = base + offset + nstripes * increment;
  1617. key_end.type = BTRFS_EXTENT_ITEM_KEY;
  1618. key_end.offset = (u64)0;
  1619. reada1 = btrfs_reada_add(root, &key_start, &key_end);
  1620. key_start.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
  1621. key_start.type = BTRFS_EXTENT_CSUM_KEY;
  1622. key_start.offset = logical;
  1623. key_end.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
  1624. key_end.type = BTRFS_EXTENT_CSUM_KEY;
  1625. key_end.offset = base + offset + nstripes * increment;
  1626. reada2 = btrfs_reada_add(csum_root, &key_start, &key_end);
  1627. if (!IS_ERR(reada1))
  1628. btrfs_reada_wait(reada1);
  1629. if (!IS_ERR(reada2))
  1630. btrfs_reada_wait(reada2);
  1631. mutex_lock(&fs_info->scrub_lock);
  1632. while (atomic_read(&fs_info->scrub_pause_req)) {
  1633. mutex_unlock(&fs_info->scrub_lock);
  1634. wait_event(fs_info->scrub_pause_wait,
  1635. atomic_read(&fs_info->scrub_pause_req) == 0);
  1636. mutex_lock(&fs_info->scrub_lock);
  1637. }
  1638. atomic_dec(&fs_info->scrubs_paused);
  1639. mutex_unlock(&fs_info->scrub_lock);
  1640. wake_up(&fs_info->scrub_pause_wait);
  1641. /*
  1642. * collect all data csums for the stripe to avoid seeking during
  1643. * the scrub. This might currently (crc32) end up to be about 1MB
  1644. */
  1645. blk_start_plug(&plug);
  1646. /*
  1647. * now find all extents for each stripe and scrub them
  1648. */
  1649. logical = base + offset;
  1650. physical = map->stripes[num].physical;
  1651. ret = 0;
  1652. for (i = 0; i < nstripes; ++i) {
  1653. /*
  1654. * canceled?
  1655. */
  1656. if (atomic_read(&fs_info->scrub_cancel_req) ||
  1657. atomic_read(&sdev->cancel_req)) {
  1658. ret = -ECANCELED;
  1659. goto out;
  1660. }
  1661. /*
  1662. * check to see if we have to pause
  1663. */
  1664. if (atomic_read(&fs_info->scrub_pause_req)) {
  1665. /* push queued extents */
  1666. scrub_submit(sdev);
  1667. wait_event(sdev->list_wait,
  1668. atomic_read(&sdev->in_flight) == 0);
  1669. atomic_inc(&fs_info->scrubs_paused);
  1670. wake_up(&fs_info->scrub_pause_wait);
  1671. mutex_lock(&fs_info->scrub_lock);
  1672. while (atomic_read(&fs_info->scrub_pause_req)) {
  1673. mutex_unlock(&fs_info->scrub_lock);
  1674. wait_event(fs_info->scrub_pause_wait,
  1675. atomic_read(&fs_info->scrub_pause_req) == 0);
  1676. mutex_lock(&fs_info->scrub_lock);
  1677. }
  1678. atomic_dec(&fs_info->scrubs_paused);
  1679. mutex_unlock(&fs_info->scrub_lock);
  1680. wake_up(&fs_info->scrub_pause_wait);
  1681. }
  1682. ret = btrfs_lookup_csums_range(csum_root, logical,
  1683. logical + map->stripe_len - 1,
  1684. &sdev->csum_list, 1);
  1685. if (ret)
  1686. goto out;
  1687. key.objectid = logical;
  1688. key.type = BTRFS_EXTENT_ITEM_KEY;
  1689. key.offset = (u64)0;
  1690. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1691. if (ret < 0)
  1692. goto out;
  1693. if (ret > 0) {
  1694. ret = btrfs_previous_item(root, path, 0,
  1695. BTRFS_EXTENT_ITEM_KEY);
  1696. if (ret < 0)
  1697. goto out;
  1698. if (ret > 0) {
  1699. /* there's no smaller item, so stick with the
  1700. * larger one */
  1701. btrfs_release_path(path);
  1702. ret = btrfs_search_slot(NULL, root, &key,
  1703. path, 0, 0);
  1704. if (ret < 0)
  1705. goto out;
  1706. }
  1707. }
  1708. while (1) {
  1709. l = path->nodes[0];
  1710. slot = path->slots[0];
  1711. if (slot >= btrfs_header_nritems(l)) {
  1712. ret = btrfs_next_leaf(root, path);
  1713. if (ret == 0)
  1714. continue;
  1715. if (ret < 0)
  1716. goto out;
  1717. break;
  1718. }
  1719. btrfs_item_key_to_cpu(l, &key, slot);
  1720. if (key.objectid + key.offset <= logical)
  1721. goto next;
  1722. if (key.objectid >= logical + map->stripe_len)
  1723. break;
  1724. if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
  1725. goto next;
  1726. extent = btrfs_item_ptr(l, slot,
  1727. struct btrfs_extent_item);
  1728. flags = btrfs_extent_flags(l, extent);
  1729. generation = btrfs_extent_generation(l, extent);
  1730. if (key.objectid < logical &&
  1731. (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
  1732. printk(KERN_ERR
  1733. "btrfs scrub: tree block %llu spanning "
  1734. "stripes, ignored. logical=%llu\n",
  1735. (unsigned long long)key.objectid,
  1736. (unsigned long long)logical);
  1737. goto next;
  1738. }
  1739. /*
  1740. * trim extent to this stripe
  1741. */
  1742. if (key.objectid < logical) {
  1743. key.offset -= logical - key.objectid;
  1744. key.objectid = logical;
  1745. }
  1746. if (key.objectid + key.offset >
  1747. logical + map->stripe_len) {
  1748. key.offset = logical + map->stripe_len -
  1749. key.objectid;
  1750. }
  1751. ret = scrub_extent(sdev, key.objectid, key.offset,
  1752. key.objectid - logical + physical,
  1753. flags, generation, mirror_num);
  1754. if (ret)
  1755. goto out;
  1756. next:
  1757. path->slots[0]++;
  1758. }
  1759. btrfs_release_path(path);
  1760. logical += increment;
  1761. physical += map->stripe_len;
  1762. spin_lock(&sdev->stat_lock);
  1763. sdev->stat.last_physical = physical;
  1764. spin_unlock(&sdev->stat_lock);
  1765. }
  1766. /* push queued extents */
  1767. scrub_submit(sdev);
  1768. out:
  1769. blk_finish_plug(&plug);
  1770. btrfs_free_path(path);
  1771. return ret < 0 ? ret : 0;
  1772. }
  1773. static noinline_for_stack int scrub_chunk(struct scrub_dev *sdev,
  1774. u64 chunk_tree, u64 chunk_objectid, u64 chunk_offset, u64 length,
  1775. u64 dev_offset)
  1776. {
  1777. struct btrfs_mapping_tree *map_tree =
  1778. &sdev->dev->dev_root->fs_info->mapping_tree;
  1779. struct map_lookup *map;
  1780. struct extent_map *em;
  1781. int i;
  1782. int ret = -EINVAL;
  1783. read_lock(&map_tree->map_tree.lock);
  1784. em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
  1785. read_unlock(&map_tree->map_tree.lock);
  1786. if (!em)
  1787. return -EINVAL;
  1788. map = (struct map_lookup *)em->bdev;
  1789. if (em->start != chunk_offset)
  1790. goto out;
  1791. if (em->len < length)
  1792. goto out;
  1793. for (i = 0; i < map->num_stripes; ++i) {
  1794. if (map->stripes[i].dev == sdev->dev &&
  1795. map->stripes[i].physical == dev_offset) {
  1796. ret = scrub_stripe(sdev, map, i, chunk_offset, length);
  1797. if (ret)
  1798. goto out;
  1799. }
  1800. }
  1801. out:
  1802. free_extent_map(em);
  1803. return ret;
  1804. }
  1805. static noinline_for_stack
  1806. int scrub_enumerate_chunks(struct scrub_dev *sdev, u64 start, u64 end)
  1807. {
  1808. struct btrfs_dev_extent *dev_extent = NULL;
  1809. struct btrfs_path *path;
  1810. struct btrfs_root *root = sdev->dev->dev_root;
  1811. struct btrfs_fs_info *fs_info = root->fs_info;
  1812. u64 length;
  1813. u64 chunk_tree;
  1814. u64 chunk_objectid;
  1815. u64 chunk_offset;
  1816. int ret;
  1817. int slot;
  1818. struct extent_buffer *l;
  1819. struct btrfs_key key;
  1820. struct btrfs_key found_key;
  1821. struct btrfs_block_group_cache *cache;
  1822. path = btrfs_alloc_path();
  1823. if (!path)
  1824. return -ENOMEM;
  1825. path->reada = 2;
  1826. path->search_commit_root = 1;
  1827. path->skip_locking = 1;
  1828. key.objectid = sdev->dev->devid;
  1829. key.offset = 0ull;
  1830. key.type = BTRFS_DEV_EXTENT_KEY;
  1831. while (1) {
  1832. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1833. if (ret < 0)
  1834. break;
  1835. if (ret > 0) {
  1836. if (path->slots[0] >=
  1837. btrfs_header_nritems(path->nodes[0])) {
  1838. ret = btrfs_next_leaf(root, path);
  1839. if (ret)
  1840. break;
  1841. }
  1842. }
  1843. l = path->nodes[0];
  1844. slot = path->slots[0];
  1845. btrfs_item_key_to_cpu(l, &found_key, slot);
  1846. if (found_key.objectid != sdev->dev->devid)
  1847. break;
  1848. if (btrfs_key_type(&found_key) != BTRFS_DEV_EXTENT_KEY)
  1849. break;
  1850. if (found_key.offset >= end)
  1851. break;
  1852. if (found_key.offset < key.offset)
  1853. break;
  1854. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  1855. length = btrfs_dev_extent_length(l, dev_extent);
  1856. if (found_key.offset + length <= start) {
  1857. key.offset = found_key.offset + length;
  1858. btrfs_release_path(path);
  1859. continue;
  1860. }
  1861. chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
  1862. chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
  1863. chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
  1864. /*
  1865. * get a reference on the corresponding block group to prevent
  1866. * the chunk from going away while we scrub it
  1867. */
  1868. cache = btrfs_lookup_block_group(fs_info, chunk_offset);
  1869. if (!cache) {
  1870. ret = -ENOENT;
  1871. break;
  1872. }
  1873. ret = scrub_chunk(sdev, chunk_tree, chunk_objectid,
  1874. chunk_offset, length, found_key.offset);
  1875. btrfs_put_block_group(cache);
  1876. if (ret)
  1877. break;
  1878. key.offset = found_key.offset + length;
  1879. btrfs_release_path(path);
  1880. }
  1881. btrfs_free_path(path);
  1882. /*
  1883. * ret can still be 1 from search_slot or next_leaf,
  1884. * that's not an error
  1885. */
  1886. return ret < 0 ? ret : 0;
  1887. }
  1888. static noinline_for_stack int scrub_supers(struct scrub_dev *sdev)
  1889. {
  1890. int i;
  1891. u64 bytenr;
  1892. u64 gen;
  1893. int ret;
  1894. struct btrfs_device *device = sdev->dev;
  1895. struct btrfs_root *root = device->dev_root;
  1896. if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR)
  1897. return -EIO;
  1898. gen = root->fs_info->last_trans_committed;
  1899. for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
  1900. bytenr = btrfs_sb_offset(i);
  1901. if (bytenr + BTRFS_SUPER_INFO_SIZE > device->total_bytes)
  1902. break;
  1903. ret = scrub_pages(sdev, bytenr, BTRFS_SUPER_INFO_SIZE, bytenr,
  1904. BTRFS_EXTENT_FLAG_SUPER, gen, i, NULL, 1);
  1905. if (ret)
  1906. return ret;
  1907. }
  1908. wait_event(sdev->list_wait, atomic_read(&sdev->in_flight) == 0);
  1909. return 0;
  1910. }
  1911. /*
  1912. * get a reference count on fs_info->scrub_workers. start worker if necessary
  1913. */
  1914. static noinline_for_stack int scrub_workers_get(struct btrfs_root *root)
  1915. {
  1916. struct btrfs_fs_info *fs_info = root->fs_info;
  1917. int ret = 0;
  1918. mutex_lock(&fs_info->scrub_lock);
  1919. if (fs_info->scrub_workers_refcnt == 0) {
  1920. btrfs_init_workers(&fs_info->scrub_workers, "scrub",
  1921. fs_info->thread_pool_size, &fs_info->generic_worker);
  1922. fs_info->scrub_workers.idle_thresh = 4;
  1923. ret = btrfs_start_workers(&fs_info->scrub_workers);
  1924. if (ret)
  1925. goto out;
  1926. }
  1927. ++fs_info->scrub_workers_refcnt;
  1928. out:
  1929. mutex_unlock(&fs_info->scrub_lock);
  1930. return ret;
  1931. }
  1932. static noinline_for_stack void scrub_workers_put(struct btrfs_root *root)
  1933. {
  1934. struct btrfs_fs_info *fs_info = root->fs_info;
  1935. mutex_lock(&fs_info->scrub_lock);
  1936. if (--fs_info->scrub_workers_refcnt == 0)
  1937. btrfs_stop_workers(&fs_info->scrub_workers);
  1938. WARN_ON(fs_info->scrub_workers_refcnt < 0);
  1939. mutex_unlock(&fs_info->scrub_lock);
  1940. }
  1941. int btrfs_scrub_dev(struct btrfs_root *root, u64 devid, u64 start, u64 end,
  1942. struct btrfs_scrub_progress *progress, int readonly)
  1943. {
  1944. struct scrub_dev *sdev;
  1945. struct btrfs_fs_info *fs_info = root->fs_info;
  1946. int ret;
  1947. struct btrfs_device *dev;
  1948. if (btrfs_fs_closing(root->fs_info))
  1949. return -EINVAL;
  1950. /*
  1951. * check some assumptions
  1952. */
  1953. if (root->nodesize != root->leafsize) {
  1954. printk(KERN_ERR
  1955. "btrfs_scrub: size assumption nodesize == leafsize (%d == %d) fails\n",
  1956. root->nodesize, root->leafsize);
  1957. return -EINVAL;
  1958. }
  1959. if (root->nodesize > BTRFS_STRIPE_LEN) {
  1960. /*
  1961. * in this case scrub is unable to calculate the checksum
  1962. * the way scrub is implemented. Do not handle this
  1963. * situation at all because it won't ever happen.
  1964. */
  1965. printk(KERN_ERR
  1966. "btrfs_scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails\n",
  1967. root->nodesize, BTRFS_STRIPE_LEN);
  1968. return -EINVAL;
  1969. }
  1970. if (root->sectorsize != PAGE_SIZE) {
  1971. /* not supported for data w/o checksums */
  1972. printk(KERN_ERR
  1973. "btrfs_scrub: size assumption sectorsize != PAGE_SIZE (%d != %lld) fails\n",
  1974. root->sectorsize, (unsigned long long)PAGE_SIZE);
  1975. return -EINVAL;
  1976. }
  1977. ret = scrub_workers_get(root);
  1978. if (ret)
  1979. return ret;
  1980. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  1981. dev = btrfs_find_device(root, devid, NULL, NULL);
  1982. if (!dev || dev->missing) {
  1983. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1984. scrub_workers_put(root);
  1985. return -ENODEV;
  1986. }
  1987. mutex_lock(&fs_info->scrub_lock);
  1988. if (!dev->in_fs_metadata) {
  1989. mutex_unlock(&fs_info->scrub_lock);
  1990. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1991. scrub_workers_put(root);
  1992. return -ENODEV;
  1993. }
  1994. if (dev->scrub_device) {
  1995. mutex_unlock(&fs_info->scrub_lock);
  1996. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1997. scrub_workers_put(root);
  1998. return -EINPROGRESS;
  1999. }
  2000. sdev = scrub_setup_dev(dev);
  2001. if (IS_ERR(sdev)) {
  2002. mutex_unlock(&fs_info->scrub_lock);
  2003. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  2004. scrub_workers_put(root);
  2005. return PTR_ERR(sdev);
  2006. }
  2007. sdev->readonly = readonly;
  2008. dev->scrub_device = sdev;
  2009. atomic_inc(&fs_info->scrubs_running);
  2010. mutex_unlock(&fs_info->scrub_lock);
  2011. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  2012. down_read(&fs_info->scrub_super_lock);
  2013. ret = scrub_supers(sdev);
  2014. up_read(&fs_info->scrub_super_lock);
  2015. if (!ret)
  2016. ret = scrub_enumerate_chunks(sdev, start, end);
  2017. wait_event(sdev->list_wait, atomic_read(&sdev->in_flight) == 0);
  2018. atomic_dec(&fs_info->scrubs_running);
  2019. wake_up(&fs_info->scrub_pause_wait);
  2020. wait_event(sdev->list_wait, atomic_read(&sdev->fixup_cnt) == 0);
  2021. if (progress)
  2022. memcpy(progress, &sdev->stat, sizeof(*progress));
  2023. mutex_lock(&fs_info->scrub_lock);
  2024. dev->scrub_device = NULL;
  2025. mutex_unlock(&fs_info->scrub_lock);
  2026. scrub_free_dev(sdev);
  2027. scrub_workers_put(root);
  2028. return ret;
  2029. }
  2030. void btrfs_scrub_pause(struct btrfs_root *root)
  2031. {
  2032. struct btrfs_fs_info *fs_info = root->fs_info;
  2033. mutex_lock(&fs_info->scrub_lock);
  2034. atomic_inc(&fs_info->scrub_pause_req);
  2035. while (atomic_read(&fs_info->scrubs_paused) !=
  2036. atomic_read(&fs_info->scrubs_running)) {
  2037. mutex_unlock(&fs_info->scrub_lock);
  2038. wait_event(fs_info->scrub_pause_wait,
  2039. atomic_read(&fs_info->scrubs_paused) ==
  2040. atomic_read(&fs_info->scrubs_running));
  2041. mutex_lock(&fs_info->scrub_lock);
  2042. }
  2043. mutex_unlock(&fs_info->scrub_lock);
  2044. }
  2045. void btrfs_scrub_continue(struct btrfs_root *root)
  2046. {
  2047. struct btrfs_fs_info *fs_info = root->fs_info;
  2048. atomic_dec(&fs_info->scrub_pause_req);
  2049. wake_up(&fs_info->scrub_pause_wait);
  2050. }
  2051. void btrfs_scrub_pause_super(struct btrfs_root *root)
  2052. {
  2053. down_write(&root->fs_info->scrub_super_lock);
  2054. }
  2055. void btrfs_scrub_continue_super(struct btrfs_root *root)
  2056. {
  2057. up_write(&root->fs_info->scrub_super_lock);
  2058. }
  2059. int __btrfs_scrub_cancel(struct btrfs_fs_info *fs_info)
  2060. {
  2061. mutex_lock(&fs_info->scrub_lock);
  2062. if (!atomic_read(&fs_info->scrubs_running)) {
  2063. mutex_unlock(&fs_info->scrub_lock);
  2064. return -ENOTCONN;
  2065. }
  2066. atomic_inc(&fs_info->scrub_cancel_req);
  2067. while (atomic_read(&fs_info->scrubs_running)) {
  2068. mutex_unlock(&fs_info->scrub_lock);
  2069. wait_event(fs_info->scrub_pause_wait,
  2070. atomic_read(&fs_info->scrubs_running) == 0);
  2071. mutex_lock(&fs_info->scrub_lock);
  2072. }
  2073. atomic_dec(&fs_info->scrub_cancel_req);
  2074. mutex_unlock(&fs_info->scrub_lock);
  2075. return 0;
  2076. }
  2077. int btrfs_scrub_cancel(struct btrfs_root *root)
  2078. {
  2079. return __btrfs_scrub_cancel(root->fs_info);
  2080. }
  2081. int btrfs_scrub_cancel_dev(struct btrfs_root *root, struct btrfs_device *dev)
  2082. {
  2083. struct btrfs_fs_info *fs_info = root->fs_info;
  2084. struct scrub_dev *sdev;
  2085. mutex_lock(&fs_info->scrub_lock);
  2086. sdev = dev->scrub_device;
  2087. if (!sdev) {
  2088. mutex_unlock(&fs_info->scrub_lock);
  2089. return -ENOTCONN;
  2090. }
  2091. atomic_inc(&sdev->cancel_req);
  2092. while (dev->scrub_device) {
  2093. mutex_unlock(&fs_info->scrub_lock);
  2094. wait_event(fs_info->scrub_pause_wait,
  2095. dev->scrub_device == NULL);
  2096. mutex_lock(&fs_info->scrub_lock);
  2097. }
  2098. mutex_unlock(&fs_info->scrub_lock);
  2099. return 0;
  2100. }
  2101. int btrfs_scrub_cancel_devid(struct btrfs_root *root, u64 devid)
  2102. {
  2103. struct btrfs_fs_info *fs_info = root->fs_info;
  2104. struct btrfs_device *dev;
  2105. int ret;
  2106. /*
  2107. * we have to hold the device_list_mutex here so the device
  2108. * does not go away in cancel_dev. FIXME: find a better solution
  2109. */
  2110. mutex_lock(&fs_info->fs_devices->device_list_mutex);
  2111. dev = btrfs_find_device(root, devid, NULL, NULL);
  2112. if (!dev) {
  2113. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  2114. return -ENODEV;
  2115. }
  2116. ret = btrfs_scrub_cancel_dev(root, dev);
  2117. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  2118. return ret;
  2119. }
  2120. int btrfs_scrub_progress(struct btrfs_root *root, u64 devid,
  2121. struct btrfs_scrub_progress *progress)
  2122. {
  2123. struct btrfs_device *dev;
  2124. struct scrub_dev *sdev = NULL;
  2125. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  2126. dev = btrfs_find_device(root, devid, NULL, NULL);
  2127. if (dev)
  2128. sdev = dev->scrub_device;
  2129. if (sdev)
  2130. memcpy(progress, &sdev->stat, sizeof(*progress));
  2131. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  2132. return dev ? (sdev ? 0 : -ENOTCONN) : -ENODEV;
  2133. }