scrub.c 68 KB

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