scrub.c 106 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2011, 2012 STRATO. All rights reserved.
  4. */
  5. #include <linux/blkdev.h>
  6. #include <linux/ratelimit.h>
  7. #include <linux/sched/mm.h>
  8. #include "ctree.h"
  9. #include "volumes.h"
  10. #include "disk-io.h"
  11. #include "ordered-data.h"
  12. #include "transaction.h"
  13. #include "backref.h"
  14. #include "extent_io.h"
  15. #include "dev-replace.h"
  16. #include "check-integrity.h"
  17. #include "rcu-string.h"
  18. #include "raid56.h"
  19. /*
  20. * This is only the first step towards a full-features scrub. It reads all
  21. * extent and super block and verifies the checksums. In case a bad checksum
  22. * is found or the extent cannot be read, good data will be written back if
  23. * any can be found.
  24. *
  25. * Future enhancements:
  26. * - In case an unrepairable extent is encountered, track which files are
  27. * affected and report them
  28. * - track and record media errors, throw out bad devices
  29. * - add a mode to also read unallocated space
  30. */
  31. struct scrub_block;
  32. struct scrub_ctx;
  33. /*
  34. * the following three values only influence the performance.
  35. * The last one configures the number of parallel and outstanding I/O
  36. * operations. The first two values configure an upper limit for the number
  37. * of (dynamically allocated) pages that are added to a bio.
  38. */
  39. #define SCRUB_PAGES_PER_RD_BIO 32 /* 128k per bio */
  40. #define SCRUB_PAGES_PER_WR_BIO 32 /* 128k per bio */
  41. #define SCRUB_BIOS_PER_SCTX 64 /* 8MB per device in flight */
  42. /*
  43. * the following value times PAGE_SIZE needs to be large enough to match the
  44. * largest node/leaf/sector size that shall be supported.
  45. * Values larger than BTRFS_STRIPE_LEN are not supported.
  46. */
  47. #define SCRUB_MAX_PAGES_PER_BLOCK 16 /* 64k per node/leaf/sector */
  48. struct scrub_recover {
  49. refcount_t refs;
  50. struct btrfs_bio *bbio;
  51. u64 map_length;
  52. };
  53. struct scrub_page {
  54. struct scrub_block *sblock;
  55. struct page *page;
  56. struct btrfs_device *dev;
  57. struct list_head list;
  58. u64 flags; /* extent flags */
  59. u64 generation;
  60. u64 logical;
  61. u64 physical;
  62. u64 physical_for_dev_replace;
  63. atomic_t refs;
  64. struct {
  65. unsigned int mirror_num:8;
  66. unsigned int have_csum:1;
  67. unsigned int io_error:1;
  68. };
  69. u8 csum[BTRFS_CSUM_SIZE];
  70. struct scrub_recover *recover;
  71. };
  72. struct scrub_bio {
  73. int index;
  74. struct scrub_ctx *sctx;
  75. struct btrfs_device *dev;
  76. struct bio *bio;
  77. blk_status_t status;
  78. u64 logical;
  79. u64 physical;
  80. #if SCRUB_PAGES_PER_WR_BIO >= SCRUB_PAGES_PER_RD_BIO
  81. struct scrub_page *pagev[SCRUB_PAGES_PER_WR_BIO];
  82. #else
  83. struct scrub_page *pagev[SCRUB_PAGES_PER_RD_BIO];
  84. #endif
  85. int page_count;
  86. int next_free;
  87. struct btrfs_work work;
  88. };
  89. struct scrub_block {
  90. struct scrub_page *pagev[SCRUB_MAX_PAGES_PER_BLOCK];
  91. int page_count;
  92. atomic_t outstanding_pages;
  93. refcount_t refs; /* free mem on transition to zero */
  94. struct scrub_ctx *sctx;
  95. struct scrub_parity *sparity;
  96. struct {
  97. unsigned int header_error:1;
  98. unsigned int checksum_error:1;
  99. unsigned int no_io_error_seen:1;
  100. unsigned int generation_error:1; /* also sets header_error */
  101. /* The following is for the data used to check parity */
  102. /* It is for the data with checksum */
  103. unsigned int data_corrected:1;
  104. };
  105. struct btrfs_work work;
  106. };
  107. /* Used for the chunks with parity stripe such RAID5/6 */
  108. struct scrub_parity {
  109. struct scrub_ctx *sctx;
  110. struct btrfs_device *scrub_dev;
  111. u64 logic_start;
  112. u64 logic_end;
  113. int nsectors;
  114. u64 stripe_len;
  115. refcount_t refs;
  116. struct list_head spages;
  117. /* Work of parity check and repair */
  118. struct btrfs_work work;
  119. /* Mark the parity blocks which have data */
  120. unsigned long *dbitmap;
  121. /*
  122. * Mark the parity blocks which have data, but errors happen when
  123. * read data or check data
  124. */
  125. unsigned long *ebitmap;
  126. unsigned long bitmap[0];
  127. };
  128. struct scrub_ctx {
  129. struct scrub_bio *bios[SCRUB_BIOS_PER_SCTX];
  130. struct btrfs_fs_info *fs_info;
  131. int first_free;
  132. int curr;
  133. atomic_t bios_in_flight;
  134. atomic_t workers_pending;
  135. spinlock_t list_lock;
  136. wait_queue_head_t list_wait;
  137. u16 csum_size;
  138. struct list_head csum_list;
  139. atomic_t cancel_req;
  140. int readonly;
  141. int pages_per_rd_bio;
  142. int is_dev_replace;
  143. struct scrub_bio *wr_curr_bio;
  144. struct mutex wr_lock;
  145. int pages_per_wr_bio; /* <= SCRUB_PAGES_PER_WR_BIO */
  146. struct btrfs_device *wr_tgtdev;
  147. bool flush_all_writes;
  148. /*
  149. * statistics
  150. */
  151. struct btrfs_scrub_progress stat;
  152. spinlock_t stat_lock;
  153. /*
  154. * Use a ref counter to avoid use-after-free issues. Scrub workers
  155. * decrement bios_in_flight and workers_pending and then do a wakeup
  156. * on the list_wait wait queue. We must ensure the main scrub task
  157. * doesn't free the scrub context before or while the workers are
  158. * doing the wakeup() call.
  159. */
  160. refcount_t refs;
  161. };
  162. struct scrub_warning {
  163. struct btrfs_path *path;
  164. u64 extent_item_size;
  165. const char *errstr;
  166. u64 physical;
  167. u64 logical;
  168. struct btrfs_device *dev;
  169. };
  170. struct full_stripe_lock {
  171. struct rb_node node;
  172. u64 logical;
  173. u64 refs;
  174. struct mutex mutex;
  175. };
  176. static void scrub_pending_bio_inc(struct scrub_ctx *sctx);
  177. static void scrub_pending_bio_dec(struct scrub_ctx *sctx);
  178. static int scrub_handle_errored_block(struct scrub_block *sblock_to_check);
  179. static int scrub_setup_recheck_block(struct scrub_block *original_sblock,
  180. struct scrub_block *sblocks_for_recheck);
  181. static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
  182. struct scrub_block *sblock,
  183. int retry_failed_mirror);
  184. static void scrub_recheck_block_checksum(struct scrub_block *sblock);
  185. static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
  186. struct scrub_block *sblock_good);
  187. static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
  188. struct scrub_block *sblock_good,
  189. int page_num, int force_write);
  190. static void scrub_write_block_to_dev_replace(struct scrub_block *sblock);
  191. static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
  192. int page_num);
  193. static int scrub_checksum_data(struct scrub_block *sblock);
  194. static int scrub_checksum_tree_block(struct scrub_block *sblock);
  195. static int scrub_checksum_super(struct scrub_block *sblock);
  196. static void scrub_block_get(struct scrub_block *sblock);
  197. static void scrub_block_put(struct scrub_block *sblock);
  198. static void scrub_page_get(struct scrub_page *spage);
  199. static void scrub_page_put(struct scrub_page *spage);
  200. static void scrub_parity_get(struct scrub_parity *sparity);
  201. static void scrub_parity_put(struct scrub_parity *sparity);
  202. static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
  203. struct scrub_page *spage);
  204. static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
  205. u64 physical, struct btrfs_device *dev, u64 flags,
  206. u64 gen, int mirror_num, u8 *csum, int force,
  207. u64 physical_for_dev_replace);
  208. static void scrub_bio_end_io(struct bio *bio);
  209. static void scrub_bio_end_io_worker(struct btrfs_work *work);
  210. static void scrub_block_complete(struct scrub_block *sblock);
  211. static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
  212. u64 extent_logical, u64 extent_len,
  213. u64 *extent_physical,
  214. struct btrfs_device **extent_dev,
  215. int *extent_mirror_num);
  216. static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
  217. struct scrub_page *spage);
  218. static void scrub_wr_submit(struct scrub_ctx *sctx);
  219. static void scrub_wr_bio_end_io(struct bio *bio);
  220. static void scrub_wr_bio_end_io_worker(struct btrfs_work *work);
  221. static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info);
  222. static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info);
  223. static void scrub_put_ctx(struct scrub_ctx *sctx);
  224. static inline int scrub_is_page_on_raid56(struct scrub_page *page)
  225. {
  226. return page->recover &&
  227. (page->recover->bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK);
  228. }
  229. static void scrub_pending_bio_inc(struct scrub_ctx *sctx)
  230. {
  231. refcount_inc(&sctx->refs);
  232. atomic_inc(&sctx->bios_in_flight);
  233. }
  234. static void scrub_pending_bio_dec(struct scrub_ctx *sctx)
  235. {
  236. atomic_dec(&sctx->bios_in_flight);
  237. wake_up(&sctx->list_wait);
  238. scrub_put_ctx(sctx);
  239. }
  240. static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
  241. {
  242. while (atomic_read(&fs_info->scrub_pause_req)) {
  243. mutex_unlock(&fs_info->scrub_lock);
  244. wait_event(fs_info->scrub_pause_wait,
  245. atomic_read(&fs_info->scrub_pause_req) == 0);
  246. mutex_lock(&fs_info->scrub_lock);
  247. }
  248. }
  249. static void scrub_pause_on(struct btrfs_fs_info *fs_info)
  250. {
  251. atomic_inc(&fs_info->scrubs_paused);
  252. wake_up(&fs_info->scrub_pause_wait);
  253. }
  254. static void scrub_pause_off(struct btrfs_fs_info *fs_info)
  255. {
  256. mutex_lock(&fs_info->scrub_lock);
  257. __scrub_blocked_if_needed(fs_info);
  258. atomic_dec(&fs_info->scrubs_paused);
  259. mutex_unlock(&fs_info->scrub_lock);
  260. wake_up(&fs_info->scrub_pause_wait);
  261. }
  262. static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
  263. {
  264. scrub_pause_on(fs_info);
  265. scrub_pause_off(fs_info);
  266. }
  267. /*
  268. * Insert new full stripe lock into full stripe locks tree
  269. *
  270. * Return pointer to existing or newly inserted full_stripe_lock structure if
  271. * everything works well.
  272. * Return ERR_PTR(-ENOMEM) if we failed to allocate memory
  273. *
  274. * NOTE: caller must hold full_stripe_locks_root->lock before calling this
  275. * function
  276. */
  277. static struct full_stripe_lock *insert_full_stripe_lock(
  278. struct btrfs_full_stripe_locks_tree *locks_root,
  279. u64 fstripe_logical)
  280. {
  281. struct rb_node **p;
  282. struct rb_node *parent = NULL;
  283. struct full_stripe_lock *entry;
  284. struct full_stripe_lock *ret;
  285. unsigned int nofs_flag;
  286. lockdep_assert_held(&locks_root->lock);
  287. p = &locks_root->root.rb_node;
  288. while (*p) {
  289. parent = *p;
  290. entry = rb_entry(parent, struct full_stripe_lock, node);
  291. if (fstripe_logical < entry->logical) {
  292. p = &(*p)->rb_left;
  293. } else if (fstripe_logical > entry->logical) {
  294. p = &(*p)->rb_right;
  295. } else {
  296. entry->refs++;
  297. return entry;
  298. }
  299. }
  300. /*
  301. * Insert new lock.
  302. *
  303. * We must use GFP_NOFS because the scrub task might be waiting for a
  304. * worker task executing this function and in turn a transaction commit
  305. * might be waiting the scrub task to pause (which needs to wait for all
  306. * the worker tasks to complete before pausing).
  307. */
  308. nofs_flag = memalloc_nofs_save();
  309. ret = kmalloc(sizeof(*ret), GFP_KERNEL);
  310. memalloc_nofs_restore(nofs_flag);
  311. if (!ret)
  312. return ERR_PTR(-ENOMEM);
  313. ret->logical = fstripe_logical;
  314. ret->refs = 1;
  315. mutex_init(&ret->mutex);
  316. rb_link_node(&ret->node, parent, p);
  317. rb_insert_color(&ret->node, &locks_root->root);
  318. return ret;
  319. }
  320. /*
  321. * Search for a full stripe lock of a block group
  322. *
  323. * Return pointer to existing full stripe lock if found
  324. * Return NULL if not found
  325. */
  326. static struct full_stripe_lock *search_full_stripe_lock(
  327. struct btrfs_full_stripe_locks_tree *locks_root,
  328. u64 fstripe_logical)
  329. {
  330. struct rb_node *node;
  331. struct full_stripe_lock *entry;
  332. lockdep_assert_held(&locks_root->lock);
  333. node = locks_root->root.rb_node;
  334. while (node) {
  335. entry = rb_entry(node, struct full_stripe_lock, node);
  336. if (fstripe_logical < entry->logical)
  337. node = node->rb_left;
  338. else if (fstripe_logical > entry->logical)
  339. node = node->rb_right;
  340. else
  341. return entry;
  342. }
  343. return NULL;
  344. }
  345. /*
  346. * Helper to get full stripe logical from a normal bytenr.
  347. *
  348. * Caller must ensure @cache is a RAID56 block group.
  349. */
  350. static u64 get_full_stripe_logical(struct btrfs_block_group_cache *cache,
  351. u64 bytenr)
  352. {
  353. u64 ret;
  354. /*
  355. * Due to chunk item size limit, full stripe length should not be
  356. * larger than U32_MAX. Just a sanity check here.
  357. */
  358. WARN_ON_ONCE(cache->full_stripe_len >= U32_MAX);
  359. /*
  360. * round_down() can only handle power of 2, while RAID56 full
  361. * stripe length can be 64KiB * n, so we need to manually round down.
  362. */
  363. ret = div64_u64(bytenr - cache->key.objectid, cache->full_stripe_len) *
  364. cache->full_stripe_len + cache->key.objectid;
  365. return ret;
  366. }
  367. /*
  368. * Lock a full stripe to avoid concurrency of recovery and read
  369. *
  370. * It's only used for profiles with parities (RAID5/6), for other profiles it
  371. * does nothing.
  372. *
  373. * Return 0 if we locked full stripe covering @bytenr, with a mutex held.
  374. * So caller must call unlock_full_stripe() at the same context.
  375. *
  376. * Return <0 if encounters error.
  377. */
  378. static int lock_full_stripe(struct btrfs_fs_info *fs_info, u64 bytenr,
  379. bool *locked_ret)
  380. {
  381. struct btrfs_block_group_cache *bg_cache;
  382. struct btrfs_full_stripe_locks_tree *locks_root;
  383. struct full_stripe_lock *existing;
  384. u64 fstripe_start;
  385. int ret = 0;
  386. *locked_ret = false;
  387. bg_cache = btrfs_lookup_block_group(fs_info, bytenr);
  388. if (!bg_cache) {
  389. ASSERT(0);
  390. return -ENOENT;
  391. }
  392. /* Profiles not based on parity don't need full stripe lock */
  393. if (!(bg_cache->flags & BTRFS_BLOCK_GROUP_RAID56_MASK))
  394. goto out;
  395. locks_root = &bg_cache->full_stripe_locks_root;
  396. fstripe_start = get_full_stripe_logical(bg_cache, bytenr);
  397. /* Now insert the full stripe lock */
  398. mutex_lock(&locks_root->lock);
  399. existing = insert_full_stripe_lock(locks_root, fstripe_start);
  400. mutex_unlock(&locks_root->lock);
  401. if (IS_ERR(existing)) {
  402. ret = PTR_ERR(existing);
  403. goto out;
  404. }
  405. mutex_lock(&existing->mutex);
  406. *locked_ret = true;
  407. out:
  408. btrfs_put_block_group(bg_cache);
  409. return ret;
  410. }
  411. /*
  412. * Unlock a full stripe.
  413. *
  414. * NOTE: Caller must ensure it's the same context calling corresponding
  415. * lock_full_stripe().
  416. *
  417. * Return 0 if we unlock full stripe without problem.
  418. * Return <0 for error
  419. */
  420. static int unlock_full_stripe(struct btrfs_fs_info *fs_info, u64 bytenr,
  421. bool locked)
  422. {
  423. struct btrfs_block_group_cache *bg_cache;
  424. struct btrfs_full_stripe_locks_tree *locks_root;
  425. struct full_stripe_lock *fstripe_lock;
  426. u64 fstripe_start;
  427. bool freeit = false;
  428. int ret = 0;
  429. /* If we didn't acquire full stripe lock, no need to continue */
  430. if (!locked)
  431. return 0;
  432. bg_cache = btrfs_lookup_block_group(fs_info, bytenr);
  433. if (!bg_cache) {
  434. ASSERT(0);
  435. return -ENOENT;
  436. }
  437. if (!(bg_cache->flags & BTRFS_BLOCK_GROUP_RAID56_MASK))
  438. goto out;
  439. locks_root = &bg_cache->full_stripe_locks_root;
  440. fstripe_start = get_full_stripe_logical(bg_cache, bytenr);
  441. mutex_lock(&locks_root->lock);
  442. fstripe_lock = search_full_stripe_lock(locks_root, fstripe_start);
  443. /* Unpaired unlock_full_stripe() detected */
  444. if (!fstripe_lock) {
  445. WARN_ON(1);
  446. ret = -ENOENT;
  447. mutex_unlock(&locks_root->lock);
  448. goto out;
  449. }
  450. if (fstripe_lock->refs == 0) {
  451. WARN_ON(1);
  452. btrfs_warn(fs_info, "full stripe lock at %llu refcount underflow",
  453. fstripe_lock->logical);
  454. } else {
  455. fstripe_lock->refs--;
  456. }
  457. if (fstripe_lock->refs == 0) {
  458. rb_erase(&fstripe_lock->node, &locks_root->root);
  459. freeit = true;
  460. }
  461. mutex_unlock(&locks_root->lock);
  462. mutex_unlock(&fstripe_lock->mutex);
  463. if (freeit)
  464. kfree(fstripe_lock);
  465. out:
  466. btrfs_put_block_group(bg_cache);
  467. return ret;
  468. }
  469. static void scrub_free_csums(struct scrub_ctx *sctx)
  470. {
  471. while (!list_empty(&sctx->csum_list)) {
  472. struct btrfs_ordered_sum *sum;
  473. sum = list_first_entry(&sctx->csum_list,
  474. struct btrfs_ordered_sum, list);
  475. list_del(&sum->list);
  476. kfree(sum);
  477. }
  478. }
  479. static noinline_for_stack void scrub_free_ctx(struct scrub_ctx *sctx)
  480. {
  481. int i;
  482. if (!sctx)
  483. return;
  484. /* this can happen when scrub is cancelled */
  485. if (sctx->curr != -1) {
  486. struct scrub_bio *sbio = sctx->bios[sctx->curr];
  487. for (i = 0; i < sbio->page_count; i++) {
  488. WARN_ON(!sbio->pagev[i]->page);
  489. scrub_block_put(sbio->pagev[i]->sblock);
  490. }
  491. bio_put(sbio->bio);
  492. }
  493. for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
  494. struct scrub_bio *sbio = sctx->bios[i];
  495. if (!sbio)
  496. break;
  497. kfree(sbio);
  498. }
  499. kfree(sctx->wr_curr_bio);
  500. scrub_free_csums(sctx);
  501. kfree(sctx);
  502. }
  503. static void scrub_put_ctx(struct scrub_ctx *sctx)
  504. {
  505. if (refcount_dec_and_test(&sctx->refs))
  506. scrub_free_ctx(sctx);
  507. }
  508. static noinline_for_stack struct scrub_ctx *scrub_setup_ctx(
  509. struct btrfs_fs_info *fs_info, int is_dev_replace)
  510. {
  511. struct scrub_ctx *sctx;
  512. int i;
  513. sctx = kzalloc(sizeof(*sctx), GFP_KERNEL);
  514. if (!sctx)
  515. goto nomem;
  516. refcount_set(&sctx->refs, 1);
  517. sctx->is_dev_replace = is_dev_replace;
  518. sctx->pages_per_rd_bio = SCRUB_PAGES_PER_RD_BIO;
  519. sctx->curr = -1;
  520. sctx->fs_info = fs_info;
  521. INIT_LIST_HEAD(&sctx->csum_list);
  522. for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
  523. struct scrub_bio *sbio;
  524. sbio = kzalloc(sizeof(*sbio), GFP_KERNEL);
  525. if (!sbio)
  526. goto nomem;
  527. sctx->bios[i] = sbio;
  528. sbio->index = i;
  529. sbio->sctx = sctx;
  530. sbio->page_count = 0;
  531. btrfs_init_work(&sbio->work, btrfs_scrub_helper,
  532. scrub_bio_end_io_worker, NULL, NULL);
  533. if (i != SCRUB_BIOS_PER_SCTX - 1)
  534. sctx->bios[i]->next_free = i + 1;
  535. else
  536. sctx->bios[i]->next_free = -1;
  537. }
  538. sctx->first_free = 0;
  539. atomic_set(&sctx->bios_in_flight, 0);
  540. atomic_set(&sctx->workers_pending, 0);
  541. atomic_set(&sctx->cancel_req, 0);
  542. sctx->csum_size = btrfs_super_csum_size(fs_info->super_copy);
  543. spin_lock_init(&sctx->list_lock);
  544. spin_lock_init(&sctx->stat_lock);
  545. init_waitqueue_head(&sctx->list_wait);
  546. WARN_ON(sctx->wr_curr_bio != NULL);
  547. mutex_init(&sctx->wr_lock);
  548. sctx->wr_curr_bio = NULL;
  549. if (is_dev_replace) {
  550. WARN_ON(!fs_info->dev_replace.tgtdev);
  551. sctx->pages_per_wr_bio = SCRUB_PAGES_PER_WR_BIO;
  552. sctx->wr_tgtdev = fs_info->dev_replace.tgtdev;
  553. sctx->flush_all_writes = false;
  554. }
  555. return sctx;
  556. nomem:
  557. scrub_free_ctx(sctx);
  558. return ERR_PTR(-ENOMEM);
  559. }
  560. static int scrub_print_warning_inode(u64 inum, u64 offset, u64 root,
  561. void *warn_ctx)
  562. {
  563. u64 isize;
  564. u32 nlink;
  565. int ret;
  566. int i;
  567. unsigned nofs_flag;
  568. struct extent_buffer *eb;
  569. struct btrfs_inode_item *inode_item;
  570. struct scrub_warning *swarn = warn_ctx;
  571. struct btrfs_fs_info *fs_info = swarn->dev->fs_info;
  572. struct inode_fs_paths *ipath = NULL;
  573. struct btrfs_root *local_root;
  574. struct btrfs_key root_key;
  575. struct btrfs_key key;
  576. root_key.objectid = root;
  577. root_key.type = BTRFS_ROOT_ITEM_KEY;
  578. root_key.offset = (u64)-1;
  579. local_root = btrfs_read_fs_root_no_name(fs_info, &root_key);
  580. if (IS_ERR(local_root)) {
  581. ret = PTR_ERR(local_root);
  582. goto err;
  583. }
  584. /*
  585. * this makes the path point to (inum INODE_ITEM ioff)
  586. */
  587. key.objectid = inum;
  588. key.type = BTRFS_INODE_ITEM_KEY;
  589. key.offset = 0;
  590. ret = btrfs_search_slot(NULL, local_root, &key, swarn->path, 0, 0);
  591. if (ret) {
  592. btrfs_release_path(swarn->path);
  593. goto err;
  594. }
  595. eb = swarn->path->nodes[0];
  596. inode_item = btrfs_item_ptr(eb, swarn->path->slots[0],
  597. struct btrfs_inode_item);
  598. isize = btrfs_inode_size(eb, inode_item);
  599. nlink = btrfs_inode_nlink(eb, inode_item);
  600. btrfs_release_path(swarn->path);
  601. /*
  602. * init_path might indirectly call vmalloc, or use GFP_KERNEL. Scrub
  603. * uses GFP_NOFS in this context, so we keep it consistent but it does
  604. * not seem to be strictly necessary.
  605. */
  606. nofs_flag = memalloc_nofs_save();
  607. ipath = init_ipath(4096, local_root, swarn->path);
  608. memalloc_nofs_restore(nofs_flag);
  609. if (IS_ERR(ipath)) {
  610. ret = PTR_ERR(ipath);
  611. ipath = NULL;
  612. goto err;
  613. }
  614. ret = paths_from_inode(inum, ipath);
  615. if (ret < 0)
  616. goto err;
  617. /*
  618. * we deliberately ignore the bit ipath might have been too small to
  619. * hold all of the paths here
  620. */
  621. for (i = 0; i < ipath->fspath->elem_cnt; ++i)
  622. btrfs_warn_in_rcu(fs_info,
  623. "%s at logical %llu on dev %s, physical %llu, root %llu, inode %llu, offset %llu, length %llu, links %u (path: %s)",
  624. swarn->errstr, swarn->logical,
  625. rcu_str_deref(swarn->dev->name),
  626. swarn->physical,
  627. root, inum, offset,
  628. min(isize - offset, (u64)PAGE_SIZE), nlink,
  629. (char *)(unsigned long)ipath->fspath->val[i]);
  630. free_ipath(ipath);
  631. return 0;
  632. err:
  633. btrfs_warn_in_rcu(fs_info,
  634. "%s at logical %llu on dev %s, physical %llu, root %llu, inode %llu, offset %llu: path resolving failed with ret=%d",
  635. swarn->errstr, swarn->logical,
  636. rcu_str_deref(swarn->dev->name),
  637. swarn->physical,
  638. root, inum, offset, ret);
  639. free_ipath(ipath);
  640. return 0;
  641. }
  642. static void scrub_print_warning(const char *errstr, struct scrub_block *sblock)
  643. {
  644. struct btrfs_device *dev;
  645. struct btrfs_fs_info *fs_info;
  646. struct btrfs_path *path;
  647. struct btrfs_key found_key;
  648. struct extent_buffer *eb;
  649. struct btrfs_extent_item *ei;
  650. struct scrub_warning swarn;
  651. unsigned long ptr = 0;
  652. u64 extent_item_pos;
  653. u64 flags = 0;
  654. u64 ref_root;
  655. u32 item_size;
  656. u8 ref_level = 0;
  657. int ret;
  658. WARN_ON(sblock->page_count < 1);
  659. dev = sblock->pagev[0]->dev;
  660. fs_info = sblock->sctx->fs_info;
  661. path = btrfs_alloc_path();
  662. if (!path)
  663. return;
  664. swarn.physical = sblock->pagev[0]->physical;
  665. swarn.logical = sblock->pagev[0]->logical;
  666. swarn.errstr = errstr;
  667. swarn.dev = NULL;
  668. ret = extent_from_logical(fs_info, swarn.logical, path, &found_key,
  669. &flags);
  670. if (ret < 0)
  671. goto out;
  672. extent_item_pos = swarn.logical - found_key.objectid;
  673. swarn.extent_item_size = found_key.offset;
  674. eb = path->nodes[0];
  675. ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
  676. item_size = btrfs_item_size_nr(eb, path->slots[0]);
  677. if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  678. do {
  679. ret = tree_backref_for_extent(&ptr, eb, &found_key, ei,
  680. item_size, &ref_root,
  681. &ref_level);
  682. btrfs_warn_in_rcu(fs_info,
  683. "%s at logical %llu on dev %s, physical %llu: metadata %s (level %d) in tree %llu",
  684. errstr, swarn.logical,
  685. rcu_str_deref(dev->name),
  686. swarn.physical,
  687. ref_level ? "node" : "leaf",
  688. ret < 0 ? -1 : ref_level,
  689. ret < 0 ? -1 : ref_root);
  690. } while (ret != 1);
  691. btrfs_release_path(path);
  692. } else {
  693. btrfs_release_path(path);
  694. swarn.path = path;
  695. swarn.dev = dev;
  696. iterate_extent_inodes(fs_info, found_key.objectid,
  697. extent_item_pos, 1,
  698. scrub_print_warning_inode, &swarn, false);
  699. }
  700. out:
  701. btrfs_free_path(path);
  702. }
  703. static inline void scrub_get_recover(struct scrub_recover *recover)
  704. {
  705. refcount_inc(&recover->refs);
  706. }
  707. static inline void scrub_put_recover(struct btrfs_fs_info *fs_info,
  708. struct scrub_recover *recover)
  709. {
  710. if (refcount_dec_and_test(&recover->refs)) {
  711. btrfs_bio_counter_dec(fs_info);
  712. btrfs_put_bbio(recover->bbio);
  713. kfree(recover);
  714. }
  715. }
  716. /*
  717. * scrub_handle_errored_block gets called when either verification of the
  718. * pages failed or the bio failed to read, e.g. with EIO. In the latter
  719. * case, this function handles all pages in the bio, even though only one
  720. * may be bad.
  721. * The goal of this function is to repair the errored block by using the
  722. * contents of one of the mirrors.
  723. */
  724. static int scrub_handle_errored_block(struct scrub_block *sblock_to_check)
  725. {
  726. struct scrub_ctx *sctx = sblock_to_check->sctx;
  727. struct btrfs_device *dev;
  728. struct btrfs_fs_info *fs_info;
  729. u64 logical;
  730. unsigned int failed_mirror_index;
  731. unsigned int is_metadata;
  732. unsigned int have_csum;
  733. struct scrub_block *sblocks_for_recheck; /* holds one for each mirror */
  734. struct scrub_block *sblock_bad;
  735. int ret;
  736. int mirror_index;
  737. int page_num;
  738. int success;
  739. bool full_stripe_locked;
  740. static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
  741. DEFAULT_RATELIMIT_BURST);
  742. BUG_ON(sblock_to_check->page_count < 1);
  743. fs_info = sctx->fs_info;
  744. if (sblock_to_check->pagev[0]->flags & BTRFS_EXTENT_FLAG_SUPER) {
  745. /*
  746. * if we find an error in a super block, we just report it.
  747. * They will get written with the next transaction commit
  748. * anyway
  749. */
  750. spin_lock(&sctx->stat_lock);
  751. ++sctx->stat.super_errors;
  752. spin_unlock(&sctx->stat_lock);
  753. return 0;
  754. }
  755. logical = sblock_to_check->pagev[0]->logical;
  756. BUG_ON(sblock_to_check->pagev[0]->mirror_num < 1);
  757. failed_mirror_index = sblock_to_check->pagev[0]->mirror_num - 1;
  758. is_metadata = !(sblock_to_check->pagev[0]->flags &
  759. BTRFS_EXTENT_FLAG_DATA);
  760. have_csum = sblock_to_check->pagev[0]->have_csum;
  761. dev = sblock_to_check->pagev[0]->dev;
  762. /*
  763. * For RAID5/6, race can happen for a different device scrub thread.
  764. * For data corruption, Parity and Data threads will both try
  765. * to recovery the data.
  766. * Race can lead to doubly added csum error, or even unrecoverable
  767. * error.
  768. */
  769. ret = lock_full_stripe(fs_info, logical, &full_stripe_locked);
  770. if (ret < 0) {
  771. spin_lock(&sctx->stat_lock);
  772. if (ret == -ENOMEM)
  773. sctx->stat.malloc_errors++;
  774. sctx->stat.read_errors++;
  775. sctx->stat.uncorrectable_errors++;
  776. spin_unlock(&sctx->stat_lock);
  777. return ret;
  778. }
  779. /*
  780. * read all mirrors one after the other. This includes to
  781. * re-read the extent or metadata block that failed (that was
  782. * the cause that this fixup code is called) another time,
  783. * page by page this time in order to know which pages
  784. * caused I/O errors and which ones are good (for all mirrors).
  785. * It is the goal to handle the situation when more than one
  786. * mirror contains I/O errors, but the errors do not
  787. * overlap, i.e. the data can be repaired by selecting the
  788. * pages from those mirrors without I/O error on the
  789. * particular pages. One example (with blocks >= 2 * PAGE_SIZE)
  790. * would be that mirror #1 has an I/O error on the first page,
  791. * the second page is good, and mirror #2 has an I/O error on
  792. * the second page, but the first page is good.
  793. * Then the first page of the first mirror can be repaired by
  794. * taking the first page of the second mirror, and the
  795. * second page of the second mirror can be repaired by
  796. * copying the contents of the 2nd page of the 1st mirror.
  797. * One more note: if the pages of one mirror contain I/O
  798. * errors, the checksum cannot be verified. In order to get
  799. * the best data for repairing, the first attempt is to find
  800. * a mirror without I/O errors and with a validated checksum.
  801. * Only if this is not possible, the pages are picked from
  802. * mirrors with I/O errors without considering the checksum.
  803. * If the latter is the case, at the end, the checksum of the
  804. * repaired area is verified in order to correctly maintain
  805. * the statistics.
  806. */
  807. sblocks_for_recheck = kcalloc(BTRFS_MAX_MIRRORS,
  808. sizeof(*sblocks_for_recheck), GFP_NOFS);
  809. if (!sblocks_for_recheck) {
  810. spin_lock(&sctx->stat_lock);
  811. sctx->stat.malloc_errors++;
  812. sctx->stat.read_errors++;
  813. sctx->stat.uncorrectable_errors++;
  814. spin_unlock(&sctx->stat_lock);
  815. btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
  816. goto out;
  817. }
  818. /* setup the context, map the logical blocks and alloc the pages */
  819. ret = scrub_setup_recheck_block(sblock_to_check, sblocks_for_recheck);
  820. if (ret) {
  821. spin_lock(&sctx->stat_lock);
  822. sctx->stat.read_errors++;
  823. sctx->stat.uncorrectable_errors++;
  824. spin_unlock(&sctx->stat_lock);
  825. btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
  826. goto out;
  827. }
  828. BUG_ON(failed_mirror_index >= BTRFS_MAX_MIRRORS);
  829. sblock_bad = sblocks_for_recheck + failed_mirror_index;
  830. /* build and submit the bios for the failed mirror, check checksums */
  831. scrub_recheck_block(fs_info, sblock_bad, 1);
  832. if (!sblock_bad->header_error && !sblock_bad->checksum_error &&
  833. sblock_bad->no_io_error_seen) {
  834. /*
  835. * the error disappeared after reading page by page, or
  836. * the area was part of a huge bio and other parts of the
  837. * bio caused I/O errors, or the block layer merged several
  838. * read requests into one and the error is caused by a
  839. * different bio (usually one of the two latter cases is
  840. * the cause)
  841. */
  842. spin_lock(&sctx->stat_lock);
  843. sctx->stat.unverified_errors++;
  844. sblock_to_check->data_corrected = 1;
  845. spin_unlock(&sctx->stat_lock);
  846. if (sctx->is_dev_replace)
  847. scrub_write_block_to_dev_replace(sblock_bad);
  848. goto out;
  849. }
  850. if (!sblock_bad->no_io_error_seen) {
  851. spin_lock(&sctx->stat_lock);
  852. sctx->stat.read_errors++;
  853. spin_unlock(&sctx->stat_lock);
  854. if (__ratelimit(&_rs))
  855. scrub_print_warning("i/o error", sblock_to_check);
  856. btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
  857. } else if (sblock_bad->checksum_error) {
  858. spin_lock(&sctx->stat_lock);
  859. sctx->stat.csum_errors++;
  860. spin_unlock(&sctx->stat_lock);
  861. if (__ratelimit(&_rs))
  862. scrub_print_warning("checksum error", sblock_to_check);
  863. btrfs_dev_stat_inc_and_print(dev,
  864. BTRFS_DEV_STAT_CORRUPTION_ERRS);
  865. } else if (sblock_bad->header_error) {
  866. spin_lock(&sctx->stat_lock);
  867. sctx->stat.verify_errors++;
  868. spin_unlock(&sctx->stat_lock);
  869. if (__ratelimit(&_rs))
  870. scrub_print_warning("checksum/header error",
  871. sblock_to_check);
  872. if (sblock_bad->generation_error)
  873. btrfs_dev_stat_inc_and_print(dev,
  874. BTRFS_DEV_STAT_GENERATION_ERRS);
  875. else
  876. btrfs_dev_stat_inc_and_print(dev,
  877. BTRFS_DEV_STAT_CORRUPTION_ERRS);
  878. }
  879. if (sctx->readonly) {
  880. ASSERT(!sctx->is_dev_replace);
  881. goto out;
  882. }
  883. /*
  884. * now build and submit the bios for the other mirrors, check
  885. * checksums.
  886. * First try to pick the mirror which is completely without I/O
  887. * errors and also does not have a checksum error.
  888. * If one is found, and if a checksum is present, the full block
  889. * that is known to contain an error is rewritten. Afterwards
  890. * the block is known to be corrected.
  891. * If a mirror is found which is completely correct, and no
  892. * checksum is present, only those pages are rewritten that had
  893. * an I/O error in the block to be repaired, since it cannot be
  894. * determined, which copy of the other pages is better (and it
  895. * could happen otherwise that a correct page would be
  896. * overwritten by a bad one).
  897. */
  898. for (mirror_index = 0; ;mirror_index++) {
  899. struct scrub_block *sblock_other;
  900. if (mirror_index == failed_mirror_index)
  901. continue;
  902. /* raid56's mirror can be more than BTRFS_MAX_MIRRORS */
  903. if (!scrub_is_page_on_raid56(sblock_bad->pagev[0])) {
  904. if (mirror_index >= BTRFS_MAX_MIRRORS)
  905. break;
  906. if (!sblocks_for_recheck[mirror_index].page_count)
  907. break;
  908. sblock_other = sblocks_for_recheck + mirror_index;
  909. } else {
  910. struct scrub_recover *r = sblock_bad->pagev[0]->recover;
  911. int max_allowed = r->bbio->num_stripes -
  912. r->bbio->num_tgtdevs;
  913. if (mirror_index >= max_allowed)
  914. break;
  915. if (!sblocks_for_recheck[1].page_count)
  916. break;
  917. ASSERT(failed_mirror_index == 0);
  918. sblock_other = sblocks_for_recheck + 1;
  919. sblock_other->pagev[0]->mirror_num = 1 + mirror_index;
  920. }
  921. /* build and submit the bios, check checksums */
  922. scrub_recheck_block(fs_info, sblock_other, 0);
  923. if (!sblock_other->header_error &&
  924. !sblock_other->checksum_error &&
  925. sblock_other->no_io_error_seen) {
  926. if (sctx->is_dev_replace) {
  927. scrub_write_block_to_dev_replace(sblock_other);
  928. goto corrected_error;
  929. } else {
  930. ret = scrub_repair_block_from_good_copy(
  931. sblock_bad, sblock_other);
  932. if (!ret)
  933. goto corrected_error;
  934. }
  935. }
  936. }
  937. if (sblock_bad->no_io_error_seen && !sctx->is_dev_replace)
  938. goto did_not_correct_error;
  939. /*
  940. * In case of I/O errors in the area that is supposed to be
  941. * repaired, continue by picking good copies of those pages.
  942. * Select the good pages from mirrors to rewrite bad pages from
  943. * the area to fix. Afterwards verify the checksum of the block
  944. * that is supposed to be repaired. This verification step is
  945. * only done for the purpose of statistic counting and for the
  946. * final scrub report, whether errors remain.
  947. * A perfect algorithm could make use of the checksum and try
  948. * all possible combinations of pages from the different mirrors
  949. * until the checksum verification succeeds. For example, when
  950. * the 2nd page of mirror #1 faces I/O errors, and the 2nd page
  951. * of mirror #2 is readable but the final checksum test fails,
  952. * then the 2nd page of mirror #3 could be tried, whether now
  953. * the final checksum succeeds. But this would be a rare
  954. * exception and is therefore not implemented. At least it is
  955. * avoided that the good copy is overwritten.
  956. * A more useful improvement would be to pick the sectors
  957. * without I/O error based on sector sizes (512 bytes on legacy
  958. * disks) instead of on PAGE_SIZE. Then maybe 512 byte of one
  959. * mirror could be repaired by taking 512 byte of a different
  960. * mirror, even if other 512 byte sectors in the same PAGE_SIZE
  961. * area are unreadable.
  962. */
  963. success = 1;
  964. for (page_num = 0; page_num < sblock_bad->page_count;
  965. page_num++) {
  966. struct scrub_page *page_bad = sblock_bad->pagev[page_num];
  967. struct scrub_block *sblock_other = NULL;
  968. /* skip no-io-error page in scrub */
  969. if (!page_bad->io_error && !sctx->is_dev_replace)
  970. continue;
  971. if (scrub_is_page_on_raid56(sblock_bad->pagev[0])) {
  972. /*
  973. * In case of dev replace, if raid56 rebuild process
  974. * didn't work out correct data, then copy the content
  975. * in sblock_bad to make sure target device is identical
  976. * to source device, instead of writing garbage data in
  977. * sblock_for_recheck array to target device.
  978. */
  979. sblock_other = NULL;
  980. } else if (page_bad->io_error) {
  981. /* try to find no-io-error page in mirrors */
  982. for (mirror_index = 0;
  983. mirror_index < BTRFS_MAX_MIRRORS &&
  984. sblocks_for_recheck[mirror_index].page_count > 0;
  985. mirror_index++) {
  986. if (!sblocks_for_recheck[mirror_index].
  987. pagev[page_num]->io_error) {
  988. sblock_other = sblocks_for_recheck +
  989. mirror_index;
  990. break;
  991. }
  992. }
  993. if (!sblock_other)
  994. success = 0;
  995. }
  996. if (sctx->is_dev_replace) {
  997. /*
  998. * did not find a mirror to fetch the page
  999. * from. scrub_write_page_to_dev_replace()
  1000. * handles this case (page->io_error), by
  1001. * filling the block with zeros before
  1002. * submitting the write request
  1003. */
  1004. if (!sblock_other)
  1005. sblock_other = sblock_bad;
  1006. if (scrub_write_page_to_dev_replace(sblock_other,
  1007. page_num) != 0) {
  1008. btrfs_dev_replace_stats_inc(
  1009. &fs_info->dev_replace.num_write_errors);
  1010. success = 0;
  1011. }
  1012. } else if (sblock_other) {
  1013. ret = scrub_repair_page_from_good_copy(sblock_bad,
  1014. sblock_other,
  1015. page_num, 0);
  1016. if (0 == ret)
  1017. page_bad->io_error = 0;
  1018. else
  1019. success = 0;
  1020. }
  1021. }
  1022. if (success && !sctx->is_dev_replace) {
  1023. if (is_metadata || have_csum) {
  1024. /*
  1025. * need to verify the checksum now that all
  1026. * sectors on disk are repaired (the write
  1027. * request for data to be repaired is on its way).
  1028. * Just be lazy and use scrub_recheck_block()
  1029. * which re-reads the data before the checksum
  1030. * is verified, but most likely the data comes out
  1031. * of the page cache.
  1032. */
  1033. scrub_recheck_block(fs_info, sblock_bad, 1);
  1034. if (!sblock_bad->header_error &&
  1035. !sblock_bad->checksum_error &&
  1036. sblock_bad->no_io_error_seen)
  1037. goto corrected_error;
  1038. else
  1039. goto did_not_correct_error;
  1040. } else {
  1041. corrected_error:
  1042. spin_lock(&sctx->stat_lock);
  1043. sctx->stat.corrected_errors++;
  1044. sblock_to_check->data_corrected = 1;
  1045. spin_unlock(&sctx->stat_lock);
  1046. btrfs_err_rl_in_rcu(fs_info,
  1047. "fixed up error at logical %llu on dev %s",
  1048. logical, rcu_str_deref(dev->name));
  1049. }
  1050. } else {
  1051. did_not_correct_error:
  1052. spin_lock(&sctx->stat_lock);
  1053. sctx->stat.uncorrectable_errors++;
  1054. spin_unlock(&sctx->stat_lock);
  1055. btrfs_err_rl_in_rcu(fs_info,
  1056. "unable to fixup (regular) error at logical %llu on dev %s",
  1057. logical, rcu_str_deref(dev->name));
  1058. }
  1059. out:
  1060. if (sblocks_for_recheck) {
  1061. for (mirror_index = 0; mirror_index < BTRFS_MAX_MIRRORS;
  1062. mirror_index++) {
  1063. struct scrub_block *sblock = sblocks_for_recheck +
  1064. mirror_index;
  1065. struct scrub_recover *recover;
  1066. int page_index;
  1067. for (page_index = 0; page_index < sblock->page_count;
  1068. page_index++) {
  1069. sblock->pagev[page_index]->sblock = NULL;
  1070. recover = sblock->pagev[page_index]->recover;
  1071. if (recover) {
  1072. scrub_put_recover(fs_info, recover);
  1073. sblock->pagev[page_index]->recover =
  1074. NULL;
  1075. }
  1076. scrub_page_put(sblock->pagev[page_index]);
  1077. }
  1078. }
  1079. kfree(sblocks_for_recheck);
  1080. }
  1081. ret = unlock_full_stripe(fs_info, logical, full_stripe_locked);
  1082. if (ret < 0)
  1083. return ret;
  1084. return 0;
  1085. }
  1086. static inline int scrub_nr_raid_mirrors(struct btrfs_bio *bbio)
  1087. {
  1088. if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID5)
  1089. return 2;
  1090. else if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID6)
  1091. return 3;
  1092. else
  1093. return (int)bbio->num_stripes;
  1094. }
  1095. static inline void scrub_stripe_index_and_offset(u64 logical, u64 map_type,
  1096. u64 *raid_map,
  1097. u64 mapped_length,
  1098. int nstripes, int mirror,
  1099. int *stripe_index,
  1100. u64 *stripe_offset)
  1101. {
  1102. int i;
  1103. if (map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
  1104. /* RAID5/6 */
  1105. for (i = 0; i < nstripes; i++) {
  1106. if (raid_map[i] == RAID6_Q_STRIPE ||
  1107. raid_map[i] == RAID5_P_STRIPE)
  1108. continue;
  1109. if (logical >= raid_map[i] &&
  1110. logical < raid_map[i] + mapped_length)
  1111. break;
  1112. }
  1113. *stripe_index = i;
  1114. *stripe_offset = logical - raid_map[i];
  1115. } else {
  1116. /* The other RAID type */
  1117. *stripe_index = mirror;
  1118. *stripe_offset = 0;
  1119. }
  1120. }
  1121. static int scrub_setup_recheck_block(struct scrub_block *original_sblock,
  1122. struct scrub_block *sblocks_for_recheck)
  1123. {
  1124. struct scrub_ctx *sctx = original_sblock->sctx;
  1125. struct btrfs_fs_info *fs_info = sctx->fs_info;
  1126. u64 length = original_sblock->page_count * PAGE_SIZE;
  1127. u64 logical = original_sblock->pagev[0]->logical;
  1128. u64 generation = original_sblock->pagev[0]->generation;
  1129. u64 flags = original_sblock->pagev[0]->flags;
  1130. u64 have_csum = original_sblock->pagev[0]->have_csum;
  1131. struct scrub_recover *recover;
  1132. struct btrfs_bio *bbio;
  1133. u64 sublen;
  1134. u64 mapped_length;
  1135. u64 stripe_offset;
  1136. int stripe_index;
  1137. int page_index = 0;
  1138. int mirror_index;
  1139. int nmirrors;
  1140. int ret;
  1141. /*
  1142. * note: the two members refs and outstanding_pages
  1143. * are not used (and not set) in the blocks that are used for
  1144. * the recheck procedure
  1145. */
  1146. while (length > 0) {
  1147. sublen = min_t(u64, length, PAGE_SIZE);
  1148. mapped_length = sublen;
  1149. bbio = NULL;
  1150. /*
  1151. * with a length of PAGE_SIZE, each returned stripe
  1152. * represents one mirror
  1153. */
  1154. btrfs_bio_counter_inc_blocked(fs_info);
  1155. ret = btrfs_map_sblock(fs_info, BTRFS_MAP_GET_READ_MIRRORS,
  1156. logical, &mapped_length, &bbio);
  1157. if (ret || !bbio || mapped_length < sublen) {
  1158. btrfs_put_bbio(bbio);
  1159. btrfs_bio_counter_dec(fs_info);
  1160. return -EIO;
  1161. }
  1162. recover = kzalloc(sizeof(struct scrub_recover), GFP_NOFS);
  1163. if (!recover) {
  1164. btrfs_put_bbio(bbio);
  1165. btrfs_bio_counter_dec(fs_info);
  1166. return -ENOMEM;
  1167. }
  1168. refcount_set(&recover->refs, 1);
  1169. recover->bbio = bbio;
  1170. recover->map_length = mapped_length;
  1171. BUG_ON(page_index >= SCRUB_MAX_PAGES_PER_BLOCK);
  1172. nmirrors = min(scrub_nr_raid_mirrors(bbio), BTRFS_MAX_MIRRORS);
  1173. for (mirror_index = 0; mirror_index < nmirrors;
  1174. mirror_index++) {
  1175. struct scrub_block *sblock;
  1176. struct scrub_page *page;
  1177. sblock = sblocks_for_recheck + mirror_index;
  1178. sblock->sctx = sctx;
  1179. page = kzalloc(sizeof(*page), GFP_NOFS);
  1180. if (!page) {
  1181. leave_nomem:
  1182. spin_lock(&sctx->stat_lock);
  1183. sctx->stat.malloc_errors++;
  1184. spin_unlock(&sctx->stat_lock);
  1185. scrub_put_recover(fs_info, recover);
  1186. return -ENOMEM;
  1187. }
  1188. scrub_page_get(page);
  1189. sblock->pagev[page_index] = page;
  1190. page->sblock = sblock;
  1191. page->flags = flags;
  1192. page->generation = generation;
  1193. page->logical = logical;
  1194. page->have_csum = have_csum;
  1195. if (have_csum)
  1196. memcpy(page->csum,
  1197. original_sblock->pagev[0]->csum,
  1198. sctx->csum_size);
  1199. scrub_stripe_index_and_offset(logical,
  1200. bbio->map_type,
  1201. bbio->raid_map,
  1202. mapped_length,
  1203. bbio->num_stripes -
  1204. bbio->num_tgtdevs,
  1205. mirror_index,
  1206. &stripe_index,
  1207. &stripe_offset);
  1208. page->physical = bbio->stripes[stripe_index].physical +
  1209. stripe_offset;
  1210. page->dev = bbio->stripes[stripe_index].dev;
  1211. BUG_ON(page_index >= original_sblock->page_count);
  1212. page->physical_for_dev_replace =
  1213. original_sblock->pagev[page_index]->
  1214. physical_for_dev_replace;
  1215. /* for missing devices, dev->bdev is NULL */
  1216. page->mirror_num = mirror_index + 1;
  1217. sblock->page_count++;
  1218. page->page = alloc_page(GFP_NOFS);
  1219. if (!page->page)
  1220. goto leave_nomem;
  1221. scrub_get_recover(recover);
  1222. page->recover = recover;
  1223. }
  1224. scrub_put_recover(fs_info, recover);
  1225. length -= sublen;
  1226. logical += sublen;
  1227. page_index++;
  1228. }
  1229. return 0;
  1230. }
  1231. static void scrub_bio_wait_endio(struct bio *bio)
  1232. {
  1233. complete(bio->bi_private);
  1234. }
  1235. static int scrub_submit_raid56_bio_wait(struct btrfs_fs_info *fs_info,
  1236. struct bio *bio,
  1237. struct scrub_page *page)
  1238. {
  1239. DECLARE_COMPLETION_ONSTACK(done);
  1240. int ret;
  1241. int mirror_num;
  1242. bio->bi_iter.bi_sector = page->logical >> 9;
  1243. bio->bi_private = &done;
  1244. bio->bi_end_io = scrub_bio_wait_endio;
  1245. mirror_num = page->sblock->pagev[0]->mirror_num;
  1246. ret = raid56_parity_recover(fs_info, bio, page->recover->bbio,
  1247. page->recover->map_length,
  1248. mirror_num, 0);
  1249. if (ret)
  1250. return ret;
  1251. wait_for_completion_io(&done);
  1252. return blk_status_to_errno(bio->bi_status);
  1253. }
  1254. static void scrub_recheck_block_on_raid56(struct btrfs_fs_info *fs_info,
  1255. struct scrub_block *sblock)
  1256. {
  1257. struct scrub_page *first_page = sblock->pagev[0];
  1258. struct bio *bio;
  1259. int page_num;
  1260. /* All pages in sblock belong to the same stripe on the same device. */
  1261. ASSERT(first_page->dev);
  1262. if (!first_page->dev->bdev)
  1263. goto out;
  1264. bio = btrfs_io_bio_alloc(BIO_MAX_PAGES);
  1265. bio_set_dev(bio, first_page->dev->bdev);
  1266. for (page_num = 0; page_num < sblock->page_count; page_num++) {
  1267. struct scrub_page *page = sblock->pagev[page_num];
  1268. WARN_ON(!page->page);
  1269. bio_add_page(bio, page->page, PAGE_SIZE, 0);
  1270. }
  1271. if (scrub_submit_raid56_bio_wait(fs_info, bio, first_page)) {
  1272. bio_put(bio);
  1273. goto out;
  1274. }
  1275. bio_put(bio);
  1276. scrub_recheck_block_checksum(sblock);
  1277. return;
  1278. out:
  1279. for (page_num = 0; page_num < sblock->page_count; page_num++)
  1280. sblock->pagev[page_num]->io_error = 1;
  1281. sblock->no_io_error_seen = 0;
  1282. }
  1283. /*
  1284. * this function will check the on disk data for checksum errors, header
  1285. * errors and read I/O errors. If any I/O errors happen, the exact pages
  1286. * which are errored are marked as being bad. The goal is to enable scrub
  1287. * to take those pages that are not errored from all the mirrors so that
  1288. * the pages that are errored in the just handled mirror can be repaired.
  1289. */
  1290. static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
  1291. struct scrub_block *sblock,
  1292. int retry_failed_mirror)
  1293. {
  1294. int page_num;
  1295. sblock->no_io_error_seen = 1;
  1296. /* short cut for raid56 */
  1297. if (!retry_failed_mirror && scrub_is_page_on_raid56(sblock->pagev[0]))
  1298. return scrub_recheck_block_on_raid56(fs_info, sblock);
  1299. for (page_num = 0; page_num < sblock->page_count; page_num++) {
  1300. struct bio *bio;
  1301. struct scrub_page *page = sblock->pagev[page_num];
  1302. if (page->dev->bdev == NULL) {
  1303. page->io_error = 1;
  1304. sblock->no_io_error_seen = 0;
  1305. continue;
  1306. }
  1307. WARN_ON(!page->page);
  1308. bio = btrfs_io_bio_alloc(1);
  1309. bio_set_dev(bio, page->dev->bdev);
  1310. bio_add_page(bio, page->page, PAGE_SIZE, 0);
  1311. bio->bi_iter.bi_sector = page->physical >> 9;
  1312. bio->bi_opf = REQ_OP_READ;
  1313. if (btrfsic_submit_bio_wait(bio)) {
  1314. page->io_error = 1;
  1315. sblock->no_io_error_seen = 0;
  1316. }
  1317. bio_put(bio);
  1318. }
  1319. if (sblock->no_io_error_seen)
  1320. scrub_recheck_block_checksum(sblock);
  1321. }
  1322. static inline int scrub_check_fsid(u8 fsid[],
  1323. struct scrub_page *spage)
  1324. {
  1325. struct btrfs_fs_devices *fs_devices = spage->dev->fs_devices;
  1326. int ret;
  1327. ret = memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
  1328. return !ret;
  1329. }
  1330. static void scrub_recheck_block_checksum(struct scrub_block *sblock)
  1331. {
  1332. sblock->header_error = 0;
  1333. sblock->checksum_error = 0;
  1334. sblock->generation_error = 0;
  1335. if (sblock->pagev[0]->flags & BTRFS_EXTENT_FLAG_DATA)
  1336. scrub_checksum_data(sblock);
  1337. else
  1338. scrub_checksum_tree_block(sblock);
  1339. }
  1340. static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
  1341. struct scrub_block *sblock_good)
  1342. {
  1343. int page_num;
  1344. int ret = 0;
  1345. for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
  1346. int ret_sub;
  1347. ret_sub = scrub_repair_page_from_good_copy(sblock_bad,
  1348. sblock_good,
  1349. page_num, 1);
  1350. if (ret_sub)
  1351. ret = ret_sub;
  1352. }
  1353. return ret;
  1354. }
  1355. static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
  1356. struct scrub_block *sblock_good,
  1357. int page_num, int force_write)
  1358. {
  1359. struct scrub_page *page_bad = sblock_bad->pagev[page_num];
  1360. struct scrub_page *page_good = sblock_good->pagev[page_num];
  1361. struct btrfs_fs_info *fs_info = sblock_bad->sctx->fs_info;
  1362. BUG_ON(page_bad->page == NULL);
  1363. BUG_ON(page_good->page == NULL);
  1364. if (force_write || sblock_bad->header_error ||
  1365. sblock_bad->checksum_error || page_bad->io_error) {
  1366. struct bio *bio;
  1367. int ret;
  1368. if (!page_bad->dev->bdev) {
  1369. btrfs_warn_rl(fs_info,
  1370. "scrub_repair_page_from_good_copy(bdev == NULL) is unexpected");
  1371. return -EIO;
  1372. }
  1373. bio = btrfs_io_bio_alloc(1);
  1374. bio_set_dev(bio, page_bad->dev->bdev);
  1375. bio->bi_iter.bi_sector = page_bad->physical >> 9;
  1376. bio->bi_opf = REQ_OP_WRITE;
  1377. ret = bio_add_page(bio, page_good->page, PAGE_SIZE, 0);
  1378. if (PAGE_SIZE != ret) {
  1379. bio_put(bio);
  1380. return -EIO;
  1381. }
  1382. if (btrfsic_submit_bio_wait(bio)) {
  1383. btrfs_dev_stat_inc_and_print(page_bad->dev,
  1384. BTRFS_DEV_STAT_WRITE_ERRS);
  1385. btrfs_dev_replace_stats_inc(
  1386. &fs_info->dev_replace.num_write_errors);
  1387. bio_put(bio);
  1388. return -EIO;
  1389. }
  1390. bio_put(bio);
  1391. }
  1392. return 0;
  1393. }
  1394. static void scrub_write_block_to_dev_replace(struct scrub_block *sblock)
  1395. {
  1396. struct btrfs_fs_info *fs_info = sblock->sctx->fs_info;
  1397. int page_num;
  1398. /*
  1399. * This block is used for the check of the parity on the source device,
  1400. * so the data needn't be written into the destination device.
  1401. */
  1402. if (sblock->sparity)
  1403. return;
  1404. for (page_num = 0; page_num < sblock->page_count; page_num++) {
  1405. int ret;
  1406. ret = scrub_write_page_to_dev_replace(sblock, page_num);
  1407. if (ret)
  1408. btrfs_dev_replace_stats_inc(
  1409. &fs_info->dev_replace.num_write_errors);
  1410. }
  1411. }
  1412. static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
  1413. int page_num)
  1414. {
  1415. struct scrub_page *spage = sblock->pagev[page_num];
  1416. BUG_ON(spage->page == NULL);
  1417. if (spage->io_error) {
  1418. void *mapped_buffer = kmap_atomic(spage->page);
  1419. clear_page(mapped_buffer);
  1420. flush_dcache_page(spage->page);
  1421. kunmap_atomic(mapped_buffer);
  1422. }
  1423. return scrub_add_page_to_wr_bio(sblock->sctx, spage);
  1424. }
  1425. static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
  1426. struct scrub_page *spage)
  1427. {
  1428. struct scrub_bio *sbio;
  1429. int ret;
  1430. mutex_lock(&sctx->wr_lock);
  1431. again:
  1432. if (!sctx->wr_curr_bio) {
  1433. unsigned int nofs_flag;
  1434. /*
  1435. * We must use GFP_NOFS because the scrub task might be waiting
  1436. * for a worker task executing this function and in turn a
  1437. * transaction commit might be waiting the scrub task to pause
  1438. * (which needs to wait for all the worker tasks to complete
  1439. * before pausing).
  1440. */
  1441. nofs_flag = memalloc_nofs_save();
  1442. sctx->wr_curr_bio = kzalloc(sizeof(*sctx->wr_curr_bio),
  1443. GFP_KERNEL);
  1444. memalloc_nofs_restore(nofs_flag);
  1445. if (!sctx->wr_curr_bio) {
  1446. mutex_unlock(&sctx->wr_lock);
  1447. return -ENOMEM;
  1448. }
  1449. sctx->wr_curr_bio->sctx = sctx;
  1450. sctx->wr_curr_bio->page_count = 0;
  1451. }
  1452. sbio = sctx->wr_curr_bio;
  1453. if (sbio->page_count == 0) {
  1454. struct bio *bio;
  1455. sbio->physical = spage->physical_for_dev_replace;
  1456. sbio->logical = spage->logical;
  1457. sbio->dev = sctx->wr_tgtdev;
  1458. bio = sbio->bio;
  1459. if (!bio) {
  1460. bio = btrfs_io_bio_alloc(sctx->pages_per_wr_bio);
  1461. sbio->bio = bio;
  1462. }
  1463. bio->bi_private = sbio;
  1464. bio->bi_end_io = scrub_wr_bio_end_io;
  1465. bio_set_dev(bio, sbio->dev->bdev);
  1466. bio->bi_iter.bi_sector = sbio->physical >> 9;
  1467. bio->bi_opf = REQ_OP_WRITE;
  1468. sbio->status = 0;
  1469. } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
  1470. spage->physical_for_dev_replace ||
  1471. sbio->logical + sbio->page_count * PAGE_SIZE !=
  1472. spage->logical) {
  1473. scrub_wr_submit(sctx);
  1474. goto again;
  1475. }
  1476. ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
  1477. if (ret != PAGE_SIZE) {
  1478. if (sbio->page_count < 1) {
  1479. bio_put(sbio->bio);
  1480. sbio->bio = NULL;
  1481. mutex_unlock(&sctx->wr_lock);
  1482. return -EIO;
  1483. }
  1484. scrub_wr_submit(sctx);
  1485. goto again;
  1486. }
  1487. sbio->pagev[sbio->page_count] = spage;
  1488. scrub_page_get(spage);
  1489. sbio->page_count++;
  1490. if (sbio->page_count == sctx->pages_per_wr_bio)
  1491. scrub_wr_submit(sctx);
  1492. mutex_unlock(&sctx->wr_lock);
  1493. return 0;
  1494. }
  1495. static void scrub_wr_submit(struct scrub_ctx *sctx)
  1496. {
  1497. struct scrub_bio *sbio;
  1498. if (!sctx->wr_curr_bio)
  1499. return;
  1500. sbio = sctx->wr_curr_bio;
  1501. sctx->wr_curr_bio = NULL;
  1502. WARN_ON(!sbio->bio->bi_disk);
  1503. scrub_pending_bio_inc(sctx);
  1504. /* process all writes in a single worker thread. Then the block layer
  1505. * orders the requests before sending them to the driver which
  1506. * doubled the write performance on spinning disks when measured
  1507. * with Linux 3.5 */
  1508. btrfsic_submit_bio(sbio->bio);
  1509. }
  1510. static void scrub_wr_bio_end_io(struct bio *bio)
  1511. {
  1512. struct scrub_bio *sbio = bio->bi_private;
  1513. struct btrfs_fs_info *fs_info = sbio->dev->fs_info;
  1514. sbio->status = bio->bi_status;
  1515. sbio->bio = bio;
  1516. btrfs_init_work(&sbio->work, btrfs_scrubwrc_helper,
  1517. scrub_wr_bio_end_io_worker, NULL, NULL);
  1518. btrfs_queue_work(fs_info->scrub_wr_completion_workers, &sbio->work);
  1519. }
  1520. static void scrub_wr_bio_end_io_worker(struct btrfs_work *work)
  1521. {
  1522. struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
  1523. struct scrub_ctx *sctx = sbio->sctx;
  1524. int i;
  1525. WARN_ON(sbio->page_count > SCRUB_PAGES_PER_WR_BIO);
  1526. if (sbio->status) {
  1527. struct btrfs_dev_replace *dev_replace =
  1528. &sbio->sctx->fs_info->dev_replace;
  1529. for (i = 0; i < sbio->page_count; i++) {
  1530. struct scrub_page *spage = sbio->pagev[i];
  1531. spage->io_error = 1;
  1532. btrfs_dev_replace_stats_inc(&dev_replace->
  1533. num_write_errors);
  1534. }
  1535. }
  1536. for (i = 0; i < sbio->page_count; i++)
  1537. scrub_page_put(sbio->pagev[i]);
  1538. bio_put(sbio->bio);
  1539. kfree(sbio);
  1540. scrub_pending_bio_dec(sctx);
  1541. }
  1542. static int scrub_checksum(struct scrub_block *sblock)
  1543. {
  1544. u64 flags;
  1545. int ret;
  1546. /*
  1547. * No need to initialize these stats currently,
  1548. * because this function only use return value
  1549. * instead of these stats value.
  1550. *
  1551. * Todo:
  1552. * always use stats
  1553. */
  1554. sblock->header_error = 0;
  1555. sblock->generation_error = 0;
  1556. sblock->checksum_error = 0;
  1557. WARN_ON(sblock->page_count < 1);
  1558. flags = sblock->pagev[0]->flags;
  1559. ret = 0;
  1560. if (flags & BTRFS_EXTENT_FLAG_DATA)
  1561. ret = scrub_checksum_data(sblock);
  1562. else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
  1563. ret = scrub_checksum_tree_block(sblock);
  1564. else if (flags & BTRFS_EXTENT_FLAG_SUPER)
  1565. (void)scrub_checksum_super(sblock);
  1566. else
  1567. WARN_ON(1);
  1568. if (ret)
  1569. scrub_handle_errored_block(sblock);
  1570. return ret;
  1571. }
  1572. static int scrub_checksum_data(struct scrub_block *sblock)
  1573. {
  1574. struct scrub_ctx *sctx = sblock->sctx;
  1575. u8 csum[BTRFS_CSUM_SIZE];
  1576. u8 *on_disk_csum;
  1577. struct page *page;
  1578. void *buffer;
  1579. u32 crc = ~(u32)0;
  1580. u64 len;
  1581. int index;
  1582. BUG_ON(sblock->page_count < 1);
  1583. if (!sblock->pagev[0]->have_csum)
  1584. return 0;
  1585. on_disk_csum = sblock->pagev[0]->csum;
  1586. page = sblock->pagev[0]->page;
  1587. buffer = kmap_atomic(page);
  1588. len = sctx->fs_info->sectorsize;
  1589. index = 0;
  1590. for (;;) {
  1591. u64 l = min_t(u64, len, PAGE_SIZE);
  1592. crc = btrfs_csum_data(buffer, crc, l);
  1593. kunmap_atomic(buffer);
  1594. len -= l;
  1595. if (len == 0)
  1596. break;
  1597. index++;
  1598. BUG_ON(index >= sblock->page_count);
  1599. BUG_ON(!sblock->pagev[index]->page);
  1600. page = sblock->pagev[index]->page;
  1601. buffer = kmap_atomic(page);
  1602. }
  1603. btrfs_csum_final(crc, csum);
  1604. if (memcmp(csum, on_disk_csum, sctx->csum_size))
  1605. sblock->checksum_error = 1;
  1606. return sblock->checksum_error;
  1607. }
  1608. static int scrub_checksum_tree_block(struct scrub_block *sblock)
  1609. {
  1610. struct scrub_ctx *sctx = sblock->sctx;
  1611. struct btrfs_header *h;
  1612. struct btrfs_fs_info *fs_info = sctx->fs_info;
  1613. u8 calculated_csum[BTRFS_CSUM_SIZE];
  1614. u8 on_disk_csum[BTRFS_CSUM_SIZE];
  1615. struct page *page;
  1616. void *mapped_buffer;
  1617. u64 mapped_size;
  1618. void *p;
  1619. u32 crc = ~(u32)0;
  1620. u64 len;
  1621. int index;
  1622. BUG_ON(sblock->page_count < 1);
  1623. page = sblock->pagev[0]->page;
  1624. mapped_buffer = kmap_atomic(page);
  1625. h = (struct btrfs_header *)mapped_buffer;
  1626. memcpy(on_disk_csum, h->csum, sctx->csum_size);
  1627. /*
  1628. * we don't use the getter functions here, as we
  1629. * a) don't have an extent buffer and
  1630. * b) the page is already kmapped
  1631. */
  1632. if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h))
  1633. sblock->header_error = 1;
  1634. if (sblock->pagev[0]->generation != btrfs_stack_header_generation(h)) {
  1635. sblock->header_error = 1;
  1636. sblock->generation_error = 1;
  1637. }
  1638. if (!scrub_check_fsid(h->fsid, sblock->pagev[0]))
  1639. sblock->header_error = 1;
  1640. if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
  1641. BTRFS_UUID_SIZE))
  1642. sblock->header_error = 1;
  1643. len = sctx->fs_info->nodesize - BTRFS_CSUM_SIZE;
  1644. mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
  1645. p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
  1646. index = 0;
  1647. for (;;) {
  1648. u64 l = min_t(u64, len, mapped_size);
  1649. crc = btrfs_csum_data(p, crc, l);
  1650. kunmap_atomic(mapped_buffer);
  1651. len -= l;
  1652. if (len == 0)
  1653. break;
  1654. index++;
  1655. BUG_ON(index >= sblock->page_count);
  1656. BUG_ON(!sblock->pagev[index]->page);
  1657. page = sblock->pagev[index]->page;
  1658. mapped_buffer = kmap_atomic(page);
  1659. mapped_size = PAGE_SIZE;
  1660. p = mapped_buffer;
  1661. }
  1662. btrfs_csum_final(crc, calculated_csum);
  1663. if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
  1664. sblock->checksum_error = 1;
  1665. return sblock->header_error || sblock->checksum_error;
  1666. }
  1667. static int scrub_checksum_super(struct scrub_block *sblock)
  1668. {
  1669. struct btrfs_super_block *s;
  1670. struct scrub_ctx *sctx = sblock->sctx;
  1671. u8 calculated_csum[BTRFS_CSUM_SIZE];
  1672. u8 on_disk_csum[BTRFS_CSUM_SIZE];
  1673. struct page *page;
  1674. void *mapped_buffer;
  1675. u64 mapped_size;
  1676. void *p;
  1677. u32 crc = ~(u32)0;
  1678. int fail_gen = 0;
  1679. int fail_cor = 0;
  1680. u64 len;
  1681. int index;
  1682. BUG_ON(sblock->page_count < 1);
  1683. page = sblock->pagev[0]->page;
  1684. mapped_buffer = kmap_atomic(page);
  1685. s = (struct btrfs_super_block *)mapped_buffer;
  1686. memcpy(on_disk_csum, s->csum, sctx->csum_size);
  1687. if (sblock->pagev[0]->logical != btrfs_super_bytenr(s))
  1688. ++fail_cor;
  1689. if (sblock->pagev[0]->generation != btrfs_super_generation(s))
  1690. ++fail_gen;
  1691. if (!scrub_check_fsid(s->fsid, sblock->pagev[0]))
  1692. ++fail_cor;
  1693. len = BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE;
  1694. mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
  1695. p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
  1696. index = 0;
  1697. for (;;) {
  1698. u64 l = min_t(u64, len, mapped_size);
  1699. crc = btrfs_csum_data(p, crc, l);
  1700. kunmap_atomic(mapped_buffer);
  1701. len -= l;
  1702. if (len == 0)
  1703. break;
  1704. index++;
  1705. BUG_ON(index >= sblock->page_count);
  1706. BUG_ON(!sblock->pagev[index]->page);
  1707. page = sblock->pagev[index]->page;
  1708. mapped_buffer = kmap_atomic(page);
  1709. mapped_size = PAGE_SIZE;
  1710. p = mapped_buffer;
  1711. }
  1712. btrfs_csum_final(crc, calculated_csum);
  1713. if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
  1714. ++fail_cor;
  1715. if (fail_cor + fail_gen) {
  1716. /*
  1717. * if we find an error in a super block, we just report it.
  1718. * They will get written with the next transaction commit
  1719. * anyway
  1720. */
  1721. spin_lock(&sctx->stat_lock);
  1722. ++sctx->stat.super_errors;
  1723. spin_unlock(&sctx->stat_lock);
  1724. if (fail_cor)
  1725. btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
  1726. BTRFS_DEV_STAT_CORRUPTION_ERRS);
  1727. else
  1728. btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
  1729. BTRFS_DEV_STAT_GENERATION_ERRS);
  1730. }
  1731. return fail_cor + fail_gen;
  1732. }
  1733. static void scrub_block_get(struct scrub_block *sblock)
  1734. {
  1735. refcount_inc(&sblock->refs);
  1736. }
  1737. static void scrub_block_put(struct scrub_block *sblock)
  1738. {
  1739. if (refcount_dec_and_test(&sblock->refs)) {
  1740. int i;
  1741. if (sblock->sparity)
  1742. scrub_parity_put(sblock->sparity);
  1743. for (i = 0; i < sblock->page_count; i++)
  1744. scrub_page_put(sblock->pagev[i]);
  1745. kfree(sblock);
  1746. }
  1747. }
  1748. static void scrub_page_get(struct scrub_page *spage)
  1749. {
  1750. atomic_inc(&spage->refs);
  1751. }
  1752. static void scrub_page_put(struct scrub_page *spage)
  1753. {
  1754. if (atomic_dec_and_test(&spage->refs)) {
  1755. if (spage->page)
  1756. __free_page(spage->page);
  1757. kfree(spage);
  1758. }
  1759. }
  1760. static void scrub_submit(struct scrub_ctx *sctx)
  1761. {
  1762. struct scrub_bio *sbio;
  1763. if (sctx->curr == -1)
  1764. return;
  1765. sbio = sctx->bios[sctx->curr];
  1766. sctx->curr = -1;
  1767. scrub_pending_bio_inc(sctx);
  1768. btrfsic_submit_bio(sbio->bio);
  1769. }
  1770. static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
  1771. struct scrub_page *spage)
  1772. {
  1773. struct scrub_block *sblock = spage->sblock;
  1774. struct scrub_bio *sbio;
  1775. int ret;
  1776. again:
  1777. /*
  1778. * grab a fresh bio or wait for one to become available
  1779. */
  1780. while (sctx->curr == -1) {
  1781. spin_lock(&sctx->list_lock);
  1782. sctx->curr = sctx->first_free;
  1783. if (sctx->curr != -1) {
  1784. sctx->first_free = sctx->bios[sctx->curr]->next_free;
  1785. sctx->bios[sctx->curr]->next_free = -1;
  1786. sctx->bios[sctx->curr]->page_count = 0;
  1787. spin_unlock(&sctx->list_lock);
  1788. } else {
  1789. spin_unlock(&sctx->list_lock);
  1790. wait_event(sctx->list_wait, sctx->first_free != -1);
  1791. }
  1792. }
  1793. sbio = sctx->bios[sctx->curr];
  1794. if (sbio->page_count == 0) {
  1795. struct bio *bio;
  1796. sbio->physical = spage->physical;
  1797. sbio->logical = spage->logical;
  1798. sbio->dev = spage->dev;
  1799. bio = sbio->bio;
  1800. if (!bio) {
  1801. bio = btrfs_io_bio_alloc(sctx->pages_per_rd_bio);
  1802. sbio->bio = bio;
  1803. }
  1804. bio->bi_private = sbio;
  1805. bio->bi_end_io = scrub_bio_end_io;
  1806. bio_set_dev(bio, sbio->dev->bdev);
  1807. bio->bi_iter.bi_sector = sbio->physical >> 9;
  1808. bio->bi_opf = REQ_OP_READ;
  1809. sbio->status = 0;
  1810. } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
  1811. spage->physical ||
  1812. sbio->logical + sbio->page_count * PAGE_SIZE !=
  1813. spage->logical ||
  1814. sbio->dev != spage->dev) {
  1815. scrub_submit(sctx);
  1816. goto again;
  1817. }
  1818. sbio->pagev[sbio->page_count] = spage;
  1819. ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
  1820. if (ret != PAGE_SIZE) {
  1821. if (sbio->page_count < 1) {
  1822. bio_put(sbio->bio);
  1823. sbio->bio = NULL;
  1824. return -EIO;
  1825. }
  1826. scrub_submit(sctx);
  1827. goto again;
  1828. }
  1829. scrub_block_get(sblock); /* one for the page added to the bio */
  1830. atomic_inc(&sblock->outstanding_pages);
  1831. sbio->page_count++;
  1832. if (sbio->page_count == sctx->pages_per_rd_bio)
  1833. scrub_submit(sctx);
  1834. return 0;
  1835. }
  1836. static void scrub_missing_raid56_end_io(struct bio *bio)
  1837. {
  1838. struct scrub_block *sblock = bio->bi_private;
  1839. struct btrfs_fs_info *fs_info = sblock->sctx->fs_info;
  1840. if (bio->bi_status)
  1841. sblock->no_io_error_seen = 0;
  1842. bio_put(bio);
  1843. btrfs_queue_work(fs_info->scrub_workers, &sblock->work);
  1844. }
  1845. static void scrub_missing_raid56_worker(struct btrfs_work *work)
  1846. {
  1847. struct scrub_block *sblock = container_of(work, struct scrub_block, work);
  1848. struct scrub_ctx *sctx = sblock->sctx;
  1849. struct btrfs_fs_info *fs_info = sctx->fs_info;
  1850. u64 logical;
  1851. struct btrfs_device *dev;
  1852. logical = sblock->pagev[0]->logical;
  1853. dev = sblock->pagev[0]->dev;
  1854. if (sblock->no_io_error_seen)
  1855. scrub_recheck_block_checksum(sblock);
  1856. if (!sblock->no_io_error_seen) {
  1857. spin_lock(&sctx->stat_lock);
  1858. sctx->stat.read_errors++;
  1859. spin_unlock(&sctx->stat_lock);
  1860. btrfs_err_rl_in_rcu(fs_info,
  1861. "IO error rebuilding logical %llu for dev %s",
  1862. logical, rcu_str_deref(dev->name));
  1863. } else if (sblock->header_error || sblock->checksum_error) {
  1864. spin_lock(&sctx->stat_lock);
  1865. sctx->stat.uncorrectable_errors++;
  1866. spin_unlock(&sctx->stat_lock);
  1867. btrfs_err_rl_in_rcu(fs_info,
  1868. "failed to rebuild valid logical %llu for dev %s",
  1869. logical, rcu_str_deref(dev->name));
  1870. } else {
  1871. scrub_write_block_to_dev_replace(sblock);
  1872. }
  1873. if (sctx->is_dev_replace && sctx->flush_all_writes) {
  1874. mutex_lock(&sctx->wr_lock);
  1875. scrub_wr_submit(sctx);
  1876. mutex_unlock(&sctx->wr_lock);
  1877. }
  1878. scrub_block_put(sblock);
  1879. scrub_pending_bio_dec(sctx);
  1880. }
  1881. static void scrub_missing_raid56_pages(struct scrub_block *sblock)
  1882. {
  1883. struct scrub_ctx *sctx = sblock->sctx;
  1884. struct btrfs_fs_info *fs_info = sctx->fs_info;
  1885. u64 length = sblock->page_count * PAGE_SIZE;
  1886. u64 logical = sblock->pagev[0]->logical;
  1887. struct btrfs_bio *bbio = NULL;
  1888. struct bio *bio;
  1889. struct btrfs_raid_bio *rbio;
  1890. int ret;
  1891. int i;
  1892. btrfs_bio_counter_inc_blocked(fs_info);
  1893. ret = btrfs_map_sblock(fs_info, BTRFS_MAP_GET_READ_MIRRORS, logical,
  1894. &length, &bbio);
  1895. if (ret || !bbio || !bbio->raid_map)
  1896. goto bbio_out;
  1897. if (WARN_ON(!sctx->is_dev_replace ||
  1898. !(bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK))) {
  1899. /*
  1900. * We shouldn't be scrubbing a missing device. Even for dev
  1901. * replace, we should only get here for RAID 5/6. We either
  1902. * managed to mount something with no mirrors remaining or
  1903. * there's a bug in scrub_remap_extent()/btrfs_map_block().
  1904. */
  1905. goto bbio_out;
  1906. }
  1907. bio = btrfs_io_bio_alloc(0);
  1908. bio->bi_iter.bi_sector = logical >> 9;
  1909. bio->bi_private = sblock;
  1910. bio->bi_end_io = scrub_missing_raid56_end_io;
  1911. rbio = raid56_alloc_missing_rbio(fs_info, bio, bbio, length);
  1912. if (!rbio)
  1913. goto rbio_out;
  1914. for (i = 0; i < sblock->page_count; i++) {
  1915. struct scrub_page *spage = sblock->pagev[i];
  1916. raid56_add_scrub_pages(rbio, spage->page, spage->logical);
  1917. }
  1918. btrfs_init_work(&sblock->work, btrfs_scrub_helper,
  1919. scrub_missing_raid56_worker, NULL, NULL);
  1920. scrub_block_get(sblock);
  1921. scrub_pending_bio_inc(sctx);
  1922. raid56_submit_missing_rbio(rbio);
  1923. return;
  1924. rbio_out:
  1925. bio_put(bio);
  1926. bbio_out:
  1927. btrfs_bio_counter_dec(fs_info);
  1928. btrfs_put_bbio(bbio);
  1929. spin_lock(&sctx->stat_lock);
  1930. sctx->stat.malloc_errors++;
  1931. spin_unlock(&sctx->stat_lock);
  1932. }
  1933. static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
  1934. u64 physical, struct btrfs_device *dev, u64 flags,
  1935. u64 gen, int mirror_num, u8 *csum, int force,
  1936. u64 physical_for_dev_replace)
  1937. {
  1938. struct scrub_block *sblock;
  1939. int index;
  1940. sblock = kzalloc(sizeof(*sblock), GFP_KERNEL);
  1941. if (!sblock) {
  1942. spin_lock(&sctx->stat_lock);
  1943. sctx->stat.malloc_errors++;
  1944. spin_unlock(&sctx->stat_lock);
  1945. return -ENOMEM;
  1946. }
  1947. /* one ref inside this function, plus one for each page added to
  1948. * a bio later on */
  1949. refcount_set(&sblock->refs, 1);
  1950. sblock->sctx = sctx;
  1951. sblock->no_io_error_seen = 1;
  1952. for (index = 0; len > 0; index++) {
  1953. struct scrub_page *spage;
  1954. u64 l = min_t(u64, len, PAGE_SIZE);
  1955. spage = kzalloc(sizeof(*spage), GFP_KERNEL);
  1956. if (!spage) {
  1957. leave_nomem:
  1958. spin_lock(&sctx->stat_lock);
  1959. sctx->stat.malloc_errors++;
  1960. spin_unlock(&sctx->stat_lock);
  1961. scrub_block_put(sblock);
  1962. return -ENOMEM;
  1963. }
  1964. BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
  1965. scrub_page_get(spage);
  1966. sblock->pagev[index] = spage;
  1967. spage->sblock = sblock;
  1968. spage->dev = dev;
  1969. spage->flags = flags;
  1970. spage->generation = gen;
  1971. spage->logical = logical;
  1972. spage->physical = physical;
  1973. spage->physical_for_dev_replace = physical_for_dev_replace;
  1974. spage->mirror_num = mirror_num;
  1975. if (csum) {
  1976. spage->have_csum = 1;
  1977. memcpy(spage->csum, csum, sctx->csum_size);
  1978. } else {
  1979. spage->have_csum = 0;
  1980. }
  1981. sblock->page_count++;
  1982. spage->page = alloc_page(GFP_KERNEL);
  1983. if (!spage->page)
  1984. goto leave_nomem;
  1985. len -= l;
  1986. logical += l;
  1987. physical += l;
  1988. physical_for_dev_replace += l;
  1989. }
  1990. WARN_ON(sblock->page_count == 0);
  1991. if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) {
  1992. /*
  1993. * This case should only be hit for RAID 5/6 device replace. See
  1994. * the comment in scrub_missing_raid56_pages() for details.
  1995. */
  1996. scrub_missing_raid56_pages(sblock);
  1997. } else {
  1998. for (index = 0; index < sblock->page_count; index++) {
  1999. struct scrub_page *spage = sblock->pagev[index];
  2000. int ret;
  2001. ret = scrub_add_page_to_rd_bio(sctx, spage);
  2002. if (ret) {
  2003. scrub_block_put(sblock);
  2004. return ret;
  2005. }
  2006. }
  2007. if (force)
  2008. scrub_submit(sctx);
  2009. }
  2010. /* last one frees, either here or in bio completion for last page */
  2011. scrub_block_put(sblock);
  2012. return 0;
  2013. }
  2014. static void scrub_bio_end_io(struct bio *bio)
  2015. {
  2016. struct scrub_bio *sbio = bio->bi_private;
  2017. struct btrfs_fs_info *fs_info = sbio->dev->fs_info;
  2018. sbio->status = bio->bi_status;
  2019. sbio->bio = bio;
  2020. btrfs_queue_work(fs_info->scrub_workers, &sbio->work);
  2021. }
  2022. static void scrub_bio_end_io_worker(struct btrfs_work *work)
  2023. {
  2024. struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
  2025. struct scrub_ctx *sctx = sbio->sctx;
  2026. int i;
  2027. BUG_ON(sbio->page_count > SCRUB_PAGES_PER_RD_BIO);
  2028. if (sbio->status) {
  2029. for (i = 0; i < sbio->page_count; i++) {
  2030. struct scrub_page *spage = sbio->pagev[i];
  2031. spage->io_error = 1;
  2032. spage->sblock->no_io_error_seen = 0;
  2033. }
  2034. }
  2035. /* now complete the scrub_block items that have all pages completed */
  2036. for (i = 0; i < sbio->page_count; i++) {
  2037. struct scrub_page *spage = sbio->pagev[i];
  2038. struct scrub_block *sblock = spage->sblock;
  2039. if (atomic_dec_and_test(&sblock->outstanding_pages))
  2040. scrub_block_complete(sblock);
  2041. scrub_block_put(sblock);
  2042. }
  2043. bio_put(sbio->bio);
  2044. sbio->bio = NULL;
  2045. spin_lock(&sctx->list_lock);
  2046. sbio->next_free = sctx->first_free;
  2047. sctx->first_free = sbio->index;
  2048. spin_unlock(&sctx->list_lock);
  2049. if (sctx->is_dev_replace && sctx->flush_all_writes) {
  2050. mutex_lock(&sctx->wr_lock);
  2051. scrub_wr_submit(sctx);
  2052. mutex_unlock(&sctx->wr_lock);
  2053. }
  2054. scrub_pending_bio_dec(sctx);
  2055. }
  2056. static inline void __scrub_mark_bitmap(struct scrub_parity *sparity,
  2057. unsigned long *bitmap,
  2058. u64 start, u64 len)
  2059. {
  2060. u64 offset;
  2061. u64 nsectors64;
  2062. u32 nsectors;
  2063. int sectorsize = sparity->sctx->fs_info->sectorsize;
  2064. if (len >= sparity->stripe_len) {
  2065. bitmap_set(bitmap, 0, sparity->nsectors);
  2066. return;
  2067. }
  2068. start -= sparity->logic_start;
  2069. start = div64_u64_rem(start, sparity->stripe_len, &offset);
  2070. offset = div_u64(offset, sectorsize);
  2071. nsectors64 = div_u64(len, sectorsize);
  2072. ASSERT(nsectors64 < UINT_MAX);
  2073. nsectors = (u32)nsectors64;
  2074. if (offset + nsectors <= sparity->nsectors) {
  2075. bitmap_set(bitmap, offset, nsectors);
  2076. return;
  2077. }
  2078. bitmap_set(bitmap, offset, sparity->nsectors - offset);
  2079. bitmap_set(bitmap, 0, nsectors - (sparity->nsectors - offset));
  2080. }
  2081. static inline void scrub_parity_mark_sectors_error(struct scrub_parity *sparity,
  2082. u64 start, u64 len)
  2083. {
  2084. __scrub_mark_bitmap(sparity, sparity->ebitmap, start, len);
  2085. }
  2086. static inline void scrub_parity_mark_sectors_data(struct scrub_parity *sparity,
  2087. u64 start, u64 len)
  2088. {
  2089. __scrub_mark_bitmap(sparity, sparity->dbitmap, start, len);
  2090. }
  2091. static void scrub_block_complete(struct scrub_block *sblock)
  2092. {
  2093. int corrupted = 0;
  2094. if (!sblock->no_io_error_seen) {
  2095. corrupted = 1;
  2096. scrub_handle_errored_block(sblock);
  2097. } else {
  2098. /*
  2099. * if has checksum error, write via repair mechanism in
  2100. * dev replace case, otherwise write here in dev replace
  2101. * case.
  2102. */
  2103. corrupted = scrub_checksum(sblock);
  2104. if (!corrupted && sblock->sctx->is_dev_replace)
  2105. scrub_write_block_to_dev_replace(sblock);
  2106. }
  2107. if (sblock->sparity && corrupted && !sblock->data_corrected) {
  2108. u64 start = sblock->pagev[0]->logical;
  2109. u64 end = sblock->pagev[sblock->page_count - 1]->logical +
  2110. PAGE_SIZE;
  2111. scrub_parity_mark_sectors_error(sblock->sparity,
  2112. start, end - start);
  2113. }
  2114. }
  2115. static int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u8 *csum)
  2116. {
  2117. struct btrfs_ordered_sum *sum = NULL;
  2118. unsigned long index;
  2119. unsigned long num_sectors;
  2120. while (!list_empty(&sctx->csum_list)) {
  2121. sum = list_first_entry(&sctx->csum_list,
  2122. struct btrfs_ordered_sum, list);
  2123. if (sum->bytenr > logical)
  2124. return 0;
  2125. if (sum->bytenr + sum->len > logical)
  2126. break;
  2127. ++sctx->stat.csum_discards;
  2128. list_del(&sum->list);
  2129. kfree(sum);
  2130. sum = NULL;
  2131. }
  2132. if (!sum)
  2133. return 0;
  2134. index = div_u64(logical - sum->bytenr, sctx->fs_info->sectorsize);
  2135. ASSERT(index < UINT_MAX);
  2136. num_sectors = sum->len / sctx->fs_info->sectorsize;
  2137. memcpy(csum, sum->sums + index, sctx->csum_size);
  2138. if (index == num_sectors - 1) {
  2139. list_del(&sum->list);
  2140. kfree(sum);
  2141. }
  2142. return 1;
  2143. }
  2144. /* scrub extent tries to collect up to 64 kB for each bio */
  2145. static int scrub_extent(struct scrub_ctx *sctx, struct map_lookup *map,
  2146. u64 logical, u64 len,
  2147. u64 physical, struct btrfs_device *dev, u64 flags,
  2148. u64 gen, int mirror_num, u64 physical_for_dev_replace)
  2149. {
  2150. int ret;
  2151. u8 csum[BTRFS_CSUM_SIZE];
  2152. u32 blocksize;
  2153. if (flags & BTRFS_EXTENT_FLAG_DATA) {
  2154. if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
  2155. blocksize = map->stripe_len;
  2156. else
  2157. blocksize = sctx->fs_info->sectorsize;
  2158. spin_lock(&sctx->stat_lock);
  2159. sctx->stat.data_extents_scrubbed++;
  2160. sctx->stat.data_bytes_scrubbed += len;
  2161. spin_unlock(&sctx->stat_lock);
  2162. } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  2163. if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
  2164. blocksize = map->stripe_len;
  2165. else
  2166. blocksize = sctx->fs_info->nodesize;
  2167. spin_lock(&sctx->stat_lock);
  2168. sctx->stat.tree_extents_scrubbed++;
  2169. sctx->stat.tree_bytes_scrubbed += len;
  2170. spin_unlock(&sctx->stat_lock);
  2171. } else {
  2172. blocksize = sctx->fs_info->sectorsize;
  2173. WARN_ON(1);
  2174. }
  2175. while (len) {
  2176. u64 l = min_t(u64, len, blocksize);
  2177. int have_csum = 0;
  2178. if (flags & BTRFS_EXTENT_FLAG_DATA) {
  2179. /* push csums to sbio */
  2180. have_csum = scrub_find_csum(sctx, logical, csum);
  2181. if (have_csum == 0)
  2182. ++sctx->stat.no_csum;
  2183. }
  2184. ret = scrub_pages(sctx, logical, l, physical, dev, flags, gen,
  2185. mirror_num, have_csum ? csum : NULL, 0,
  2186. physical_for_dev_replace);
  2187. if (ret)
  2188. return ret;
  2189. len -= l;
  2190. logical += l;
  2191. physical += l;
  2192. physical_for_dev_replace += l;
  2193. }
  2194. return 0;
  2195. }
  2196. static int scrub_pages_for_parity(struct scrub_parity *sparity,
  2197. u64 logical, u64 len,
  2198. u64 physical, struct btrfs_device *dev,
  2199. u64 flags, u64 gen, int mirror_num, u8 *csum)
  2200. {
  2201. struct scrub_ctx *sctx = sparity->sctx;
  2202. struct scrub_block *sblock;
  2203. int index;
  2204. sblock = kzalloc(sizeof(*sblock), GFP_KERNEL);
  2205. if (!sblock) {
  2206. spin_lock(&sctx->stat_lock);
  2207. sctx->stat.malloc_errors++;
  2208. spin_unlock(&sctx->stat_lock);
  2209. return -ENOMEM;
  2210. }
  2211. /* one ref inside this function, plus one for each page added to
  2212. * a bio later on */
  2213. refcount_set(&sblock->refs, 1);
  2214. sblock->sctx = sctx;
  2215. sblock->no_io_error_seen = 1;
  2216. sblock->sparity = sparity;
  2217. scrub_parity_get(sparity);
  2218. for (index = 0; len > 0; index++) {
  2219. struct scrub_page *spage;
  2220. u64 l = min_t(u64, len, PAGE_SIZE);
  2221. spage = kzalloc(sizeof(*spage), GFP_KERNEL);
  2222. if (!spage) {
  2223. leave_nomem:
  2224. spin_lock(&sctx->stat_lock);
  2225. sctx->stat.malloc_errors++;
  2226. spin_unlock(&sctx->stat_lock);
  2227. scrub_block_put(sblock);
  2228. return -ENOMEM;
  2229. }
  2230. BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
  2231. /* For scrub block */
  2232. scrub_page_get(spage);
  2233. sblock->pagev[index] = spage;
  2234. /* For scrub parity */
  2235. scrub_page_get(spage);
  2236. list_add_tail(&spage->list, &sparity->spages);
  2237. spage->sblock = sblock;
  2238. spage->dev = dev;
  2239. spage->flags = flags;
  2240. spage->generation = gen;
  2241. spage->logical = logical;
  2242. spage->physical = physical;
  2243. spage->mirror_num = mirror_num;
  2244. if (csum) {
  2245. spage->have_csum = 1;
  2246. memcpy(spage->csum, csum, sctx->csum_size);
  2247. } else {
  2248. spage->have_csum = 0;
  2249. }
  2250. sblock->page_count++;
  2251. spage->page = alloc_page(GFP_KERNEL);
  2252. if (!spage->page)
  2253. goto leave_nomem;
  2254. len -= l;
  2255. logical += l;
  2256. physical += l;
  2257. }
  2258. WARN_ON(sblock->page_count == 0);
  2259. for (index = 0; index < sblock->page_count; index++) {
  2260. struct scrub_page *spage = sblock->pagev[index];
  2261. int ret;
  2262. ret = scrub_add_page_to_rd_bio(sctx, spage);
  2263. if (ret) {
  2264. scrub_block_put(sblock);
  2265. return ret;
  2266. }
  2267. }
  2268. /* last one frees, either here or in bio completion for last page */
  2269. scrub_block_put(sblock);
  2270. return 0;
  2271. }
  2272. static int scrub_extent_for_parity(struct scrub_parity *sparity,
  2273. u64 logical, u64 len,
  2274. u64 physical, struct btrfs_device *dev,
  2275. u64 flags, u64 gen, int mirror_num)
  2276. {
  2277. struct scrub_ctx *sctx = sparity->sctx;
  2278. int ret;
  2279. u8 csum[BTRFS_CSUM_SIZE];
  2280. u32 blocksize;
  2281. if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) {
  2282. scrub_parity_mark_sectors_error(sparity, logical, len);
  2283. return 0;
  2284. }
  2285. if (flags & BTRFS_EXTENT_FLAG_DATA) {
  2286. blocksize = sparity->stripe_len;
  2287. } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  2288. blocksize = sparity->stripe_len;
  2289. } else {
  2290. blocksize = sctx->fs_info->sectorsize;
  2291. WARN_ON(1);
  2292. }
  2293. while (len) {
  2294. u64 l = min_t(u64, len, blocksize);
  2295. int have_csum = 0;
  2296. if (flags & BTRFS_EXTENT_FLAG_DATA) {
  2297. /* push csums to sbio */
  2298. have_csum = scrub_find_csum(sctx, logical, csum);
  2299. if (have_csum == 0)
  2300. goto skip;
  2301. }
  2302. ret = scrub_pages_for_parity(sparity, logical, l, physical, dev,
  2303. flags, gen, mirror_num,
  2304. have_csum ? csum : NULL);
  2305. if (ret)
  2306. return ret;
  2307. skip:
  2308. len -= l;
  2309. logical += l;
  2310. physical += l;
  2311. }
  2312. return 0;
  2313. }
  2314. /*
  2315. * Given a physical address, this will calculate it's
  2316. * logical offset. if this is a parity stripe, it will return
  2317. * the most left data stripe's logical offset.
  2318. *
  2319. * return 0 if it is a data stripe, 1 means parity stripe.
  2320. */
  2321. static int get_raid56_logic_offset(u64 physical, int num,
  2322. struct map_lookup *map, u64 *offset,
  2323. u64 *stripe_start)
  2324. {
  2325. int i;
  2326. int j = 0;
  2327. u64 stripe_nr;
  2328. u64 last_offset;
  2329. u32 stripe_index;
  2330. u32 rot;
  2331. last_offset = (physical - map->stripes[num].physical) *
  2332. nr_data_stripes(map);
  2333. if (stripe_start)
  2334. *stripe_start = last_offset;
  2335. *offset = last_offset;
  2336. for (i = 0; i < nr_data_stripes(map); i++) {
  2337. *offset = last_offset + i * map->stripe_len;
  2338. stripe_nr = div64_u64(*offset, map->stripe_len);
  2339. stripe_nr = div_u64(stripe_nr, nr_data_stripes(map));
  2340. /* Work out the disk rotation on this stripe-set */
  2341. stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, &rot);
  2342. /* calculate which stripe this data locates */
  2343. rot += i;
  2344. stripe_index = rot % map->num_stripes;
  2345. if (stripe_index == num)
  2346. return 0;
  2347. if (stripe_index < num)
  2348. j++;
  2349. }
  2350. *offset = last_offset + j * map->stripe_len;
  2351. return 1;
  2352. }
  2353. static void scrub_free_parity(struct scrub_parity *sparity)
  2354. {
  2355. struct scrub_ctx *sctx = sparity->sctx;
  2356. struct scrub_page *curr, *next;
  2357. int nbits;
  2358. nbits = bitmap_weight(sparity->ebitmap, sparity->nsectors);
  2359. if (nbits) {
  2360. spin_lock(&sctx->stat_lock);
  2361. sctx->stat.read_errors += nbits;
  2362. sctx->stat.uncorrectable_errors += nbits;
  2363. spin_unlock(&sctx->stat_lock);
  2364. }
  2365. list_for_each_entry_safe(curr, next, &sparity->spages, list) {
  2366. list_del_init(&curr->list);
  2367. scrub_page_put(curr);
  2368. }
  2369. kfree(sparity);
  2370. }
  2371. static void scrub_parity_bio_endio_worker(struct btrfs_work *work)
  2372. {
  2373. struct scrub_parity *sparity = container_of(work, struct scrub_parity,
  2374. work);
  2375. struct scrub_ctx *sctx = sparity->sctx;
  2376. scrub_free_parity(sparity);
  2377. scrub_pending_bio_dec(sctx);
  2378. }
  2379. static void scrub_parity_bio_endio(struct bio *bio)
  2380. {
  2381. struct scrub_parity *sparity = (struct scrub_parity *)bio->bi_private;
  2382. struct btrfs_fs_info *fs_info = sparity->sctx->fs_info;
  2383. if (bio->bi_status)
  2384. bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
  2385. sparity->nsectors);
  2386. bio_put(bio);
  2387. btrfs_init_work(&sparity->work, btrfs_scrubparity_helper,
  2388. scrub_parity_bio_endio_worker, NULL, NULL);
  2389. btrfs_queue_work(fs_info->scrub_parity_workers, &sparity->work);
  2390. }
  2391. static void scrub_parity_check_and_repair(struct scrub_parity *sparity)
  2392. {
  2393. struct scrub_ctx *sctx = sparity->sctx;
  2394. struct btrfs_fs_info *fs_info = sctx->fs_info;
  2395. struct bio *bio;
  2396. struct btrfs_raid_bio *rbio;
  2397. struct btrfs_bio *bbio = NULL;
  2398. u64 length;
  2399. int ret;
  2400. if (!bitmap_andnot(sparity->dbitmap, sparity->dbitmap, sparity->ebitmap,
  2401. sparity->nsectors))
  2402. goto out;
  2403. length = sparity->logic_end - sparity->logic_start;
  2404. btrfs_bio_counter_inc_blocked(fs_info);
  2405. ret = btrfs_map_sblock(fs_info, BTRFS_MAP_WRITE, sparity->logic_start,
  2406. &length, &bbio);
  2407. if (ret || !bbio || !bbio->raid_map)
  2408. goto bbio_out;
  2409. bio = btrfs_io_bio_alloc(0);
  2410. bio->bi_iter.bi_sector = sparity->logic_start >> 9;
  2411. bio->bi_private = sparity;
  2412. bio->bi_end_io = scrub_parity_bio_endio;
  2413. rbio = raid56_parity_alloc_scrub_rbio(fs_info, bio, bbio,
  2414. length, sparity->scrub_dev,
  2415. sparity->dbitmap,
  2416. sparity->nsectors);
  2417. if (!rbio)
  2418. goto rbio_out;
  2419. scrub_pending_bio_inc(sctx);
  2420. raid56_parity_submit_scrub_rbio(rbio);
  2421. return;
  2422. rbio_out:
  2423. bio_put(bio);
  2424. bbio_out:
  2425. btrfs_bio_counter_dec(fs_info);
  2426. btrfs_put_bbio(bbio);
  2427. bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
  2428. sparity->nsectors);
  2429. spin_lock(&sctx->stat_lock);
  2430. sctx->stat.malloc_errors++;
  2431. spin_unlock(&sctx->stat_lock);
  2432. out:
  2433. scrub_free_parity(sparity);
  2434. }
  2435. static inline int scrub_calc_parity_bitmap_len(int nsectors)
  2436. {
  2437. return DIV_ROUND_UP(nsectors, BITS_PER_LONG) * sizeof(long);
  2438. }
  2439. static void scrub_parity_get(struct scrub_parity *sparity)
  2440. {
  2441. refcount_inc(&sparity->refs);
  2442. }
  2443. static void scrub_parity_put(struct scrub_parity *sparity)
  2444. {
  2445. if (!refcount_dec_and_test(&sparity->refs))
  2446. return;
  2447. scrub_parity_check_and_repair(sparity);
  2448. }
  2449. static noinline_for_stack int scrub_raid56_parity(struct scrub_ctx *sctx,
  2450. struct map_lookup *map,
  2451. struct btrfs_device *sdev,
  2452. struct btrfs_path *path,
  2453. u64 logic_start,
  2454. u64 logic_end)
  2455. {
  2456. struct btrfs_fs_info *fs_info = sctx->fs_info;
  2457. struct btrfs_root *root = fs_info->extent_root;
  2458. struct btrfs_root *csum_root = fs_info->csum_root;
  2459. struct btrfs_extent_item *extent;
  2460. struct btrfs_bio *bbio = NULL;
  2461. u64 flags;
  2462. int ret;
  2463. int slot;
  2464. struct extent_buffer *l;
  2465. struct btrfs_key key;
  2466. u64 generation;
  2467. u64 extent_logical;
  2468. u64 extent_physical;
  2469. u64 extent_len;
  2470. u64 mapped_length;
  2471. struct btrfs_device *extent_dev;
  2472. struct scrub_parity *sparity;
  2473. int nsectors;
  2474. int bitmap_len;
  2475. int extent_mirror_num;
  2476. int stop_loop = 0;
  2477. nsectors = div_u64(map->stripe_len, fs_info->sectorsize);
  2478. bitmap_len = scrub_calc_parity_bitmap_len(nsectors);
  2479. sparity = kzalloc(sizeof(struct scrub_parity) + 2 * bitmap_len,
  2480. GFP_NOFS);
  2481. if (!sparity) {
  2482. spin_lock(&sctx->stat_lock);
  2483. sctx->stat.malloc_errors++;
  2484. spin_unlock(&sctx->stat_lock);
  2485. return -ENOMEM;
  2486. }
  2487. sparity->stripe_len = map->stripe_len;
  2488. sparity->nsectors = nsectors;
  2489. sparity->sctx = sctx;
  2490. sparity->scrub_dev = sdev;
  2491. sparity->logic_start = logic_start;
  2492. sparity->logic_end = logic_end;
  2493. refcount_set(&sparity->refs, 1);
  2494. INIT_LIST_HEAD(&sparity->spages);
  2495. sparity->dbitmap = sparity->bitmap;
  2496. sparity->ebitmap = (void *)sparity->bitmap + bitmap_len;
  2497. ret = 0;
  2498. while (logic_start < logic_end) {
  2499. if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
  2500. key.type = BTRFS_METADATA_ITEM_KEY;
  2501. else
  2502. key.type = BTRFS_EXTENT_ITEM_KEY;
  2503. key.objectid = logic_start;
  2504. key.offset = (u64)-1;
  2505. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2506. if (ret < 0)
  2507. goto out;
  2508. if (ret > 0) {
  2509. ret = btrfs_previous_extent_item(root, path, 0);
  2510. if (ret < 0)
  2511. goto out;
  2512. if (ret > 0) {
  2513. btrfs_release_path(path);
  2514. ret = btrfs_search_slot(NULL, root, &key,
  2515. path, 0, 0);
  2516. if (ret < 0)
  2517. goto out;
  2518. }
  2519. }
  2520. stop_loop = 0;
  2521. while (1) {
  2522. u64 bytes;
  2523. l = path->nodes[0];
  2524. slot = path->slots[0];
  2525. if (slot >= btrfs_header_nritems(l)) {
  2526. ret = btrfs_next_leaf(root, path);
  2527. if (ret == 0)
  2528. continue;
  2529. if (ret < 0)
  2530. goto out;
  2531. stop_loop = 1;
  2532. break;
  2533. }
  2534. btrfs_item_key_to_cpu(l, &key, slot);
  2535. if (key.type != BTRFS_EXTENT_ITEM_KEY &&
  2536. key.type != BTRFS_METADATA_ITEM_KEY)
  2537. goto next;
  2538. if (key.type == BTRFS_METADATA_ITEM_KEY)
  2539. bytes = fs_info->nodesize;
  2540. else
  2541. bytes = key.offset;
  2542. if (key.objectid + bytes <= logic_start)
  2543. goto next;
  2544. if (key.objectid >= logic_end) {
  2545. stop_loop = 1;
  2546. break;
  2547. }
  2548. while (key.objectid >= logic_start + map->stripe_len)
  2549. logic_start += map->stripe_len;
  2550. extent = btrfs_item_ptr(l, slot,
  2551. struct btrfs_extent_item);
  2552. flags = btrfs_extent_flags(l, extent);
  2553. generation = btrfs_extent_generation(l, extent);
  2554. if ((flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) &&
  2555. (key.objectid < logic_start ||
  2556. key.objectid + bytes >
  2557. logic_start + map->stripe_len)) {
  2558. btrfs_err(fs_info,
  2559. "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
  2560. key.objectid, logic_start);
  2561. spin_lock(&sctx->stat_lock);
  2562. sctx->stat.uncorrectable_errors++;
  2563. spin_unlock(&sctx->stat_lock);
  2564. goto next;
  2565. }
  2566. again:
  2567. extent_logical = key.objectid;
  2568. extent_len = bytes;
  2569. if (extent_logical < logic_start) {
  2570. extent_len -= logic_start - extent_logical;
  2571. extent_logical = logic_start;
  2572. }
  2573. if (extent_logical + extent_len >
  2574. logic_start + map->stripe_len)
  2575. extent_len = logic_start + map->stripe_len -
  2576. extent_logical;
  2577. scrub_parity_mark_sectors_data(sparity, extent_logical,
  2578. extent_len);
  2579. mapped_length = extent_len;
  2580. bbio = NULL;
  2581. ret = btrfs_map_block(fs_info, BTRFS_MAP_READ,
  2582. extent_logical, &mapped_length, &bbio,
  2583. 0);
  2584. if (!ret) {
  2585. if (!bbio || mapped_length < extent_len)
  2586. ret = -EIO;
  2587. }
  2588. if (ret) {
  2589. btrfs_put_bbio(bbio);
  2590. goto out;
  2591. }
  2592. extent_physical = bbio->stripes[0].physical;
  2593. extent_mirror_num = bbio->mirror_num;
  2594. extent_dev = bbio->stripes[0].dev;
  2595. btrfs_put_bbio(bbio);
  2596. ret = btrfs_lookup_csums_range(csum_root,
  2597. extent_logical,
  2598. extent_logical + extent_len - 1,
  2599. &sctx->csum_list, 1);
  2600. if (ret)
  2601. goto out;
  2602. ret = scrub_extent_for_parity(sparity, extent_logical,
  2603. extent_len,
  2604. extent_physical,
  2605. extent_dev, flags,
  2606. generation,
  2607. extent_mirror_num);
  2608. scrub_free_csums(sctx);
  2609. if (ret)
  2610. goto out;
  2611. if (extent_logical + extent_len <
  2612. key.objectid + bytes) {
  2613. logic_start += map->stripe_len;
  2614. if (logic_start >= logic_end) {
  2615. stop_loop = 1;
  2616. break;
  2617. }
  2618. if (logic_start < key.objectid + bytes) {
  2619. cond_resched();
  2620. goto again;
  2621. }
  2622. }
  2623. next:
  2624. path->slots[0]++;
  2625. }
  2626. btrfs_release_path(path);
  2627. if (stop_loop)
  2628. break;
  2629. logic_start += map->stripe_len;
  2630. }
  2631. out:
  2632. if (ret < 0)
  2633. scrub_parity_mark_sectors_error(sparity, logic_start,
  2634. logic_end - logic_start);
  2635. scrub_parity_put(sparity);
  2636. scrub_submit(sctx);
  2637. mutex_lock(&sctx->wr_lock);
  2638. scrub_wr_submit(sctx);
  2639. mutex_unlock(&sctx->wr_lock);
  2640. btrfs_release_path(path);
  2641. return ret < 0 ? ret : 0;
  2642. }
  2643. static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
  2644. struct map_lookup *map,
  2645. struct btrfs_device *scrub_dev,
  2646. int num, u64 base, u64 length)
  2647. {
  2648. struct btrfs_path *path, *ppath;
  2649. struct btrfs_fs_info *fs_info = sctx->fs_info;
  2650. struct btrfs_root *root = fs_info->extent_root;
  2651. struct btrfs_root *csum_root = fs_info->csum_root;
  2652. struct btrfs_extent_item *extent;
  2653. struct blk_plug plug;
  2654. u64 flags;
  2655. int ret;
  2656. int slot;
  2657. u64 nstripes;
  2658. struct extent_buffer *l;
  2659. u64 physical;
  2660. u64 logical;
  2661. u64 logic_end;
  2662. u64 physical_end;
  2663. u64 generation;
  2664. int mirror_num;
  2665. struct reada_control *reada1;
  2666. struct reada_control *reada2;
  2667. struct btrfs_key key;
  2668. struct btrfs_key key_end;
  2669. u64 increment = map->stripe_len;
  2670. u64 offset;
  2671. u64 extent_logical;
  2672. u64 extent_physical;
  2673. u64 extent_len;
  2674. u64 stripe_logical;
  2675. u64 stripe_end;
  2676. struct btrfs_device *extent_dev;
  2677. int extent_mirror_num;
  2678. int stop_loop = 0;
  2679. physical = map->stripes[num].physical;
  2680. offset = 0;
  2681. nstripes = div64_u64(length, map->stripe_len);
  2682. if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
  2683. offset = map->stripe_len * num;
  2684. increment = map->stripe_len * map->num_stripes;
  2685. mirror_num = 1;
  2686. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  2687. int factor = map->num_stripes / map->sub_stripes;
  2688. offset = map->stripe_len * (num / map->sub_stripes);
  2689. increment = map->stripe_len * factor;
  2690. mirror_num = num % map->sub_stripes + 1;
  2691. } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
  2692. increment = map->stripe_len;
  2693. mirror_num = num % map->num_stripes + 1;
  2694. } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
  2695. increment = map->stripe_len;
  2696. mirror_num = num % map->num_stripes + 1;
  2697. } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
  2698. get_raid56_logic_offset(physical, num, map, &offset, NULL);
  2699. increment = map->stripe_len * nr_data_stripes(map);
  2700. mirror_num = 1;
  2701. } else {
  2702. increment = map->stripe_len;
  2703. mirror_num = 1;
  2704. }
  2705. path = btrfs_alloc_path();
  2706. if (!path)
  2707. return -ENOMEM;
  2708. ppath = btrfs_alloc_path();
  2709. if (!ppath) {
  2710. btrfs_free_path(path);
  2711. return -ENOMEM;
  2712. }
  2713. /*
  2714. * work on commit root. The related disk blocks are static as
  2715. * long as COW is applied. This means, it is save to rewrite
  2716. * them to repair disk errors without any race conditions
  2717. */
  2718. path->search_commit_root = 1;
  2719. path->skip_locking = 1;
  2720. ppath->search_commit_root = 1;
  2721. ppath->skip_locking = 1;
  2722. /*
  2723. * trigger the readahead for extent tree csum tree and wait for
  2724. * completion. During readahead, the scrub is officially paused
  2725. * to not hold off transaction commits
  2726. */
  2727. logical = base + offset;
  2728. physical_end = physical + nstripes * map->stripe_len;
  2729. if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
  2730. get_raid56_logic_offset(physical_end, num,
  2731. map, &logic_end, NULL);
  2732. logic_end += base;
  2733. } else {
  2734. logic_end = logical + increment * nstripes;
  2735. }
  2736. wait_event(sctx->list_wait,
  2737. atomic_read(&sctx->bios_in_flight) == 0);
  2738. scrub_blocked_if_needed(fs_info);
  2739. /* FIXME it might be better to start readahead at commit root */
  2740. key.objectid = logical;
  2741. key.type = BTRFS_EXTENT_ITEM_KEY;
  2742. key.offset = (u64)0;
  2743. key_end.objectid = logic_end;
  2744. key_end.type = BTRFS_METADATA_ITEM_KEY;
  2745. key_end.offset = (u64)-1;
  2746. reada1 = btrfs_reada_add(root, &key, &key_end);
  2747. key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
  2748. key.type = BTRFS_EXTENT_CSUM_KEY;
  2749. key.offset = logical;
  2750. key_end.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
  2751. key_end.type = BTRFS_EXTENT_CSUM_KEY;
  2752. key_end.offset = logic_end;
  2753. reada2 = btrfs_reada_add(csum_root, &key, &key_end);
  2754. if (!IS_ERR(reada1))
  2755. btrfs_reada_wait(reada1);
  2756. if (!IS_ERR(reada2))
  2757. btrfs_reada_wait(reada2);
  2758. /*
  2759. * collect all data csums for the stripe to avoid seeking during
  2760. * the scrub. This might currently (crc32) end up to be about 1MB
  2761. */
  2762. blk_start_plug(&plug);
  2763. /*
  2764. * now find all extents for each stripe and scrub them
  2765. */
  2766. ret = 0;
  2767. while (physical < physical_end) {
  2768. /*
  2769. * canceled?
  2770. */
  2771. if (atomic_read(&fs_info->scrub_cancel_req) ||
  2772. atomic_read(&sctx->cancel_req)) {
  2773. ret = -ECANCELED;
  2774. goto out;
  2775. }
  2776. /*
  2777. * check to see if we have to pause
  2778. */
  2779. if (atomic_read(&fs_info->scrub_pause_req)) {
  2780. /* push queued extents */
  2781. sctx->flush_all_writes = true;
  2782. scrub_submit(sctx);
  2783. mutex_lock(&sctx->wr_lock);
  2784. scrub_wr_submit(sctx);
  2785. mutex_unlock(&sctx->wr_lock);
  2786. wait_event(sctx->list_wait,
  2787. atomic_read(&sctx->bios_in_flight) == 0);
  2788. sctx->flush_all_writes = false;
  2789. scrub_blocked_if_needed(fs_info);
  2790. }
  2791. if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
  2792. ret = get_raid56_logic_offset(physical, num, map,
  2793. &logical,
  2794. &stripe_logical);
  2795. logical += base;
  2796. if (ret) {
  2797. /* it is parity strip */
  2798. stripe_logical += base;
  2799. stripe_end = stripe_logical + increment;
  2800. ret = scrub_raid56_parity(sctx, map, scrub_dev,
  2801. ppath, stripe_logical,
  2802. stripe_end);
  2803. if (ret)
  2804. goto out;
  2805. goto skip;
  2806. }
  2807. }
  2808. if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
  2809. key.type = BTRFS_METADATA_ITEM_KEY;
  2810. else
  2811. key.type = BTRFS_EXTENT_ITEM_KEY;
  2812. key.objectid = logical;
  2813. key.offset = (u64)-1;
  2814. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2815. if (ret < 0)
  2816. goto out;
  2817. if (ret > 0) {
  2818. ret = btrfs_previous_extent_item(root, path, 0);
  2819. if (ret < 0)
  2820. goto out;
  2821. if (ret > 0) {
  2822. /* there's no smaller item, so stick with the
  2823. * larger one */
  2824. btrfs_release_path(path);
  2825. ret = btrfs_search_slot(NULL, root, &key,
  2826. path, 0, 0);
  2827. if (ret < 0)
  2828. goto out;
  2829. }
  2830. }
  2831. stop_loop = 0;
  2832. while (1) {
  2833. u64 bytes;
  2834. l = path->nodes[0];
  2835. slot = path->slots[0];
  2836. if (slot >= btrfs_header_nritems(l)) {
  2837. ret = btrfs_next_leaf(root, path);
  2838. if (ret == 0)
  2839. continue;
  2840. if (ret < 0)
  2841. goto out;
  2842. stop_loop = 1;
  2843. break;
  2844. }
  2845. btrfs_item_key_to_cpu(l, &key, slot);
  2846. if (key.type != BTRFS_EXTENT_ITEM_KEY &&
  2847. key.type != BTRFS_METADATA_ITEM_KEY)
  2848. goto next;
  2849. if (key.type == BTRFS_METADATA_ITEM_KEY)
  2850. bytes = fs_info->nodesize;
  2851. else
  2852. bytes = key.offset;
  2853. if (key.objectid + bytes <= logical)
  2854. goto next;
  2855. if (key.objectid >= logical + map->stripe_len) {
  2856. /* out of this device extent */
  2857. if (key.objectid >= logic_end)
  2858. stop_loop = 1;
  2859. break;
  2860. }
  2861. extent = btrfs_item_ptr(l, slot,
  2862. struct btrfs_extent_item);
  2863. flags = btrfs_extent_flags(l, extent);
  2864. generation = btrfs_extent_generation(l, extent);
  2865. if ((flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) &&
  2866. (key.objectid < logical ||
  2867. key.objectid + bytes >
  2868. logical + map->stripe_len)) {
  2869. btrfs_err(fs_info,
  2870. "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
  2871. key.objectid, logical);
  2872. spin_lock(&sctx->stat_lock);
  2873. sctx->stat.uncorrectable_errors++;
  2874. spin_unlock(&sctx->stat_lock);
  2875. goto next;
  2876. }
  2877. again:
  2878. extent_logical = key.objectid;
  2879. extent_len = bytes;
  2880. /*
  2881. * trim extent to this stripe
  2882. */
  2883. if (extent_logical < logical) {
  2884. extent_len -= logical - extent_logical;
  2885. extent_logical = logical;
  2886. }
  2887. if (extent_logical + extent_len >
  2888. logical + map->stripe_len) {
  2889. extent_len = logical + map->stripe_len -
  2890. extent_logical;
  2891. }
  2892. extent_physical = extent_logical - logical + physical;
  2893. extent_dev = scrub_dev;
  2894. extent_mirror_num = mirror_num;
  2895. if (sctx->is_dev_replace)
  2896. scrub_remap_extent(fs_info, extent_logical,
  2897. extent_len, &extent_physical,
  2898. &extent_dev,
  2899. &extent_mirror_num);
  2900. ret = btrfs_lookup_csums_range(csum_root,
  2901. extent_logical,
  2902. extent_logical +
  2903. extent_len - 1,
  2904. &sctx->csum_list, 1);
  2905. if (ret)
  2906. goto out;
  2907. ret = scrub_extent(sctx, map, extent_logical, extent_len,
  2908. extent_physical, extent_dev, flags,
  2909. generation, extent_mirror_num,
  2910. extent_logical - logical + physical);
  2911. scrub_free_csums(sctx);
  2912. if (ret)
  2913. goto out;
  2914. if (extent_logical + extent_len <
  2915. key.objectid + bytes) {
  2916. if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
  2917. /*
  2918. * loop until we find next data stripe
  2919. * or we have finished all stripes.
  2920. */
  2921. loop:
  2922. physical += map->stripe_len;
  2923. ret = get_raid56_logic_offset(physical,
  2924. num, map, &logical,
  2925. &stripe_logical);
  2926. logical += base;
  2927. if (ret && physical < physical_end) {
  2928. stripe_logical += base;
  2929. stripe_end = stripe_logical +
  2930. increment;
  2931. ret = scrub_raid56_parity(sctx,
  2932. map, scrub_dev, ppath,
  2933. stripe_logical,
  2934. stripe_end);
  2935. if (ret)
  2936. goto out;
  2937. goto loop;
  2938. }
  2939. } else {
  2940. physical += map->stripe_len;
  2941. logical += increment;
  2942. }
  2943. if (logical < key.objectid + bytes) {
  2944. cond_resched();
  2945. goto again;
  2946. }
  2947. if (physical >= physical_end) {
  2948. stop_loop = 1;
  2949. break;
  2950. }
  2951. }
  2952. next:
  2953. path->slots[0]++;
  2954. }
  2955. btrfs_release_path(path);
  2956. skip:
  2957. logical += increment;
  2958. physical += map->stripe_len;
  2959. spin_lock(&sctx->stat_lock);
  2960. if (stop_loop)
  2961. sctx->stat.last_physical = map->stripes[num].physical +
  2962. length;
  2963. else
  2964. sctx->stat.last_physical = physical;
  2965. spin_unlock(&sctx->stat_lock);
  2966. if (stop_loop)
  2967. break;
  2968. }
  2969. out:
  2970. /* push queued extents */
  2971. scrub_submit(sctx);
  2972. mutex_lock(&sctx->wr_lock);
  2973. scrub_wr_submit(sctx);
  2974. mutex_unlock(&sctx->wr_lock);
  2975. blk_finish_plug(&plug);
  2976. btrfs_free_path(path);
  2977. btrfs_free_path(ppath);
  2978. return ret < 0 ? ret : 0;
  2979. }
  2980. static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
  2981. struct btrfs_device *scrub_dev,
  2982. u64 chunk_offset, u64 length,
  2983. u64 dev_offset,
  2984. struct btrfs_block_group_cache *cache)
  2985. {
  2986. struct btrfs_fs_info *fs_info = sctx->fs_info;
  2987. struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
  2988. struct map_lookup *map;
  2989. struct extent_map *em;
  2990. int i;
  2991. int ret = 0;
  2992. read_lock(&map_tree->map_tree.lock);
  2993. em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
  2994. read_unlock(&map_tree->map_tree.lock);
  2995. if (!em) {
  2996. /*
  2997. * Might have been an unused block group deleted by the cleaner
  2998. * kthread or relocation.
  2999. */
  3000. spin_lock(&cache->lock);
  3001. if (!cache->removed)
  3002. ret = -EINVAL;
  3003. spin_unlock(&cache->lock);
  3004. return ret;
  3005. }
  3006. map = em->map_lookup;
  3007. if (em->start != chunk_offset)
  3008. goto out;
  3009. if (em->len < length)
  3010. goto out;
  3011. for (i = 0; i < map->num_stripes; ++i) {
  3012. if (map->stripes[i].dev->bdev == scrub_dev->bdev &&
  3013. map->stripes[i].physical == dev_offset) {
  3014. ret = scrub_stripe(sctx, map, scrub_dev, i,
  3015. chunk_offset, length);
  3016. if (ret)
  3017. goto out;
  3018. }
  3019. }
  3020. out:
  3021. free_extent_map(em);
  3022. return ret;
  3023. }
  3024. static noinline_for_stack
  3025. int scrub_enumerate_chunks(struct scrub_ctx *sctx,
  3026. struct btrfs_device *scrub_dev, u64 start, u64 end)
  3027. {
  3028. struct btrfs_dev_extent *dev_extent = NULL;
  3029. struct btrfs_path *path;
  3030. struct btrfs_fs_info *fs_info = sctx->fs_info;
  3031. struct btrfs_root *root = fs_info->dev_root;
  3032. u64 length;
  3033. u64 chunk_offset;
  3034. int ret = 0;
  3035. int ro_set;
  3036. int slot;
  3037. struct extent_buffer *l;
  3038. struct btrfs_key key;
  3039. struct btrfs_key found_key;
  3040. struct btrfs_block_group_cache *cache;
  3041. struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
  3042. path = btrfs_alloc_path();
  3043. if (!path)
  3044. return -ENOMEM;
  3045. path->reada = READA_FORWARD;
  3046. path->search_commit_root = 1;
  3047. path->skip_locking = 1;
  3048. key.objectid = scrub_dev->devid;
  3049. key.offset = 0ull;
  3050. key.type = BTRFS_DEV_EXTENT_KEY;
  3051. while (1) {
  3052. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3053. if (ret < 0)
  3054. break;
  3055. if (ret > 0) {
  3056. if (path->slots[0] >=
  3057. btrfs_header_nritems(path->nodes[0])) {
  3058. ret = btrfs_next_leaf(root, path);
  3059. if (ret < 0)
  3060. break;
  3061. if (ret > 0) {
  3062. ret = 0;
  3063. break;
  3064. }
  3065. } else {
  3066. ret = 0;
  3067. }
  3068. }
  3069. l = path->nodes[0];
  3070. slot = path->slots[0];
  3071. btrfs_item_key_to_cpu(l, &found_key, slot);
  3072. if (found_key.objectid != scrub_dev->devid)
  3073. break;
  3074. if (found_key.type != BTRFS_DEV_EXTENT_KEY)
  3075. break;
  3076. if (found_key.offset >= end)
  3077. break;
  3078. if (found_key.offset < key.offset)
  3079. break;
  3080. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  3081. length = btrfs_dev_extent_length(l, dev_extent);
  3082. if (found_key.offset + length <= start)
  3083. goto skip;
  3084. chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
  3085. /*
  3086. * get a reference on the corresponding block group to prevent
  3087. * the chunk from going away while we scrub it
  3088. */
  3089. cache = btrfs_lookup_block_group(fs_info, chunk_offset);
  3090. /* some chunks are removed but not committed to disk yet,
  3091. * continue scrubbing */
  3092. if (!cache)
  3093. goto skip;
  3094. /*
  3095. * we need call btrfs_inc_block_group_ro() with scrubs_paused,
  3096. * to avoid deadlock caused by:
  3097. * btrfs_inc_block_group_ro()
  3098. * -> btrfs_wait_for_commit()
  3099. * -> btrfs_commit_transaction()
  3100. * -> btrfs_scrub_pause()
  3101. */
  3102. scrub_pause_on(fs_info);
  3103. ret = btrfs_inc_block_group_ro(cache);
  3104. if (!ret && sctx->is_dev_replace) {
  3105. /*
  3106. * If we are doing a device replace wait for any tasks
  3107. * that started dellaloc right before we set the block
  3108. * group to RO mode, as they might have just allocated
  3109. * an extent from it or decided they could do a nocow
  3110. * write. And if any such tasks did that, wait for their
  3111. * ordered extents to complete and then commit the
  3112. * current transaction, so that we can later see the new
  3113. * extent items in the extent tree - the ordered extents
  3114. * create delayed data references (for cow writes) when
  3115. * they complete, which will be run and insert the
  3116. * corresponding extent items into the extent tree when
  3117. * we commit the transaction they used when running
  3118. * inode.c:btrfs_finish_ordered_io(). We later use
  3119. * the commit root of the extent tree to find extents
  3120. * to copy from the srcdev into the tgtdev, and we don't
  3121. * want to miss any new extents.
  3122. */
  3123. btrfs_wait_block_group_reservations(cache);
  3124. btrfs_wait_nocow_writers(cache);
  3125. ret = btrfs_wait_ordered_roots(fs_info, U64_MAX,
  3126. cache->key.objectid,
  3127. cache->key.offset);
  3128. if (ret > 0) {
  3129. struct btrfs_trans_handle *trans;
  3130. trans = btrfs_join_transaction(root);
  3131. if (IS_ERR(trans))
  3132. ret = PTR_ERR(trans);
  3133. else
  3134. ret = btrfs_commit_transaction(trans);
  3135. if (ret) {
  3136. scrub_pause_off(fs_info);
  3137. btrfs_put_block_group(cache);
  3138. break;
  3139. }
  3140. }
  3141. }
  3142. scrub_pause_off(fs_info);
  3143. if (ret == 0) {
  3144. ro_set = 1;
  3145. } else if (ret == -ENOSPC) {
  3146. /*
  3147. * btrfs_inc_block_group_ro return -ENOSPC when it
  3148. * failed in creating new chunk for metadata.
  3149. * It is not a problem for scrub/replace, because
  3150. * metadata are always cowed, and our scrub paused
  3151. * commit_transactions.
  3152. */
  3153. ro_set = 0;
  3154. } else {
  3155. btrfs_warn(fs_info,
  3156. "failed setting block group ro: %d", ret);
  3157. btrfs_put_block_group(cache);
  3158. break;
  3159. }
  3160. btrfs_dev_replace_write_lock(&fs_info->dev_replace);
  3161. dev_replace->cursor_right = found_key.offset + length;
  3162. dev_replace->cursor_left = found_key.offset;
  3163. dev_replace->item_needs_writeback = 1;
  3164. btrfs_dev_replace_write_unlock(&fs_info->dev_replace);
  3165. ret = scrub_chunk(sctx, scrub_dev, chunk_offset, length,
  3166. found_key.offset, cache);
  3167. /*
  3168. * flush, submit all pending read and write bios, afterwards
  3169. * wait for them.
  3170. * Note that in the dev replace case, a read request causes
  3171. * write requests that are submitted in the read completion
  3172. * worker. Therefore in the current situation, it is required
  3173. * that all write requests are flushed, so that all read and
  3174. * write requests are really completed when bios_in_flight
  3175. * changes to 0.
  3176. */
  3177. sctx->flush_all_writes = true;
  3178. scrub_submit(sctx);
  3179. mutex_lock(&sctx->wr_lock);
  3180. scrub_wr_submit(sctx);
  3181. mutex_unlock(&sctx->wr_lock);
  3182. wait_event(sctx->list_wait,
  3183. atomic_read(&sctx->bios_in_flight) == 0);
  3184. scrub_pause_on(fs_info);
  3185. /*
  3186. * must be called before we decrease @scrub_paused.
  3187. * make sure we don't block transaction commit while
  3188. * we are waiting pending workers finished.
  3189. */
  3190. wait_event(sctx->list_wait,
  3191. atomic_read(&sctx->workers_pending) == 0);
  3192. sctx->flush_all_writes = false;
  3193. scrub_pause_off(fs_info);
  3194. btrfs_dev_replace_write_lock(&fs_info->dev_replace);
  3195. dev_replace->cursor_left = dev_replace->cursor_right;
  3196. dev_replace->item_needs_writeback = 1;
  3197. btrfs_dev_replace_write_unlock(&fs_info->dev_replace);
  3198. if (ro_set)
  3199. btrfs_dec_block_group_ro(cache);
  3200. /*
  3201. * We might have prevented the cleaner kthread from deleting
  3202. * this block group if it was already unused because we raced
  3203. * and set it to RO mode first. So add it back to the unused
  3204. * list, otherwise it might not ever be deleted unless a manual
  3205. * balance is triggered or it becomes used and unused again.
  3206. */
  3207. spin_lock(&cache->lock);
  3208. if (!cache->removed && !cache->ro && cache->reserved == 0 &&
  3209. btrfs_block_group_used(&cache->item) == 0) {
  3210. spin_unlock(&cache->lock);
  3211. btrfs_mark_bg_unused(cache);
  3212. } else {
  3213. spin_unlock(&cache->lock);
  3214. }
  3215. btrfs_put_block_group(cache);
  3216. if (ret)
  3217. break;
  3218. if (sctx->is_dev_replace &&
  3219. atomic64_read(&dev_replace->num_write_errors) > 0) {
  3220. ret = -EIO;
  3221. break;
  3222. }
  3223. if (sctx->stat.malloc_errors > 0) {
  3224. ret = -ENOMEM;
  3225. break;
  3226. }
  3227. skip:
  3228. key.offset = found_key.offset + length;
  3229. btrfs_release_path(path);
  3230. }
  3231. btrfs_free_path(path);
  3232. return ret;
  3233. }
  3234. static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,
  3235. struct btrfs_device *scrub_dev)
  3236. {
  3237. int i;
  3238. u64 bytenr;
  3239. u64 gen;
  3240. int ret;
  3241. struct btrfs_fs_info *fs_info = sctx->fs_info;
  3242. if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
  3243. return -EIO;
  3244. /* Seed devices of a new filesystem has their own generation. */
  3245. if (scrub_dev->fs_devices != fs_info->fs_devices)
  3246. gen = scrub_dev->generation;
  3247. else
  3248. gen = fs_info->last_trans_committed;
  3249. for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
  3250. bytenr = btrfs_sb_offset(i);
  3251. if (bytenr + BTRFS_SUPER_INFO_SIZE >
  3252. scrub_dev->commit_total_bytes)
  3253. break;
  3254. ret = scrub_pages(sctx, bytenr, BTRFS_SUPER_INFO_SIZE, bytenr,
  3255. scrub_dev, BTRFS_EXTENT_FLAG_SUPER, gen, i,
  3256. NULL, 1, bytenr);
  3257. if (ret)
  3258. return ret;
  3259. }
  3260. wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
  3261. return 0;
  3262. }
  3263. /*
  3264. * get a reference count on fs_info->scrub_workers. start worker if necessary
  3265. */
  3266. static noinline_for_stack int scrub_workers_get(struct btrfs_fs_info *fs_info,
  3267. int is_dev_replace)
  3268. {
  3269. unsigned int flags = WQ_FREEZABLE | WQ_UNBOUND;
  3270. int max_active = fs_info->thread_pool_size;
  3271. if (fs_info->scrub_workers_refcnt == 0) {
  3272. fs_info->scrub_workers = btrfs_alloc_workqueue(fs_info, "scrub",
  3273. flags, is_dev_replace ? 1 : max_active, 4);
  3274. if (!fs_info->scrub_workers)
  3275. goto fail_scrub_workers;
  3276. fs_info->scrub_wr_completion_workers =
  3277. btrfs_alloc_workqueue(fs_info, "scrubwrc", flags,
  3278. max_active, 2);
  3279. if (!fs_info->scrub_wr_completion_workers)
  3280. goto fail_scrub_wr_completion_workers;
  3281. fs_info->scrub_parity_workers =
  3282. btrfs_alloc_workqueue(fs_info, "scrubparity", flags,
  3283. max_active, 2);
  3284. if (!fs_info->scrub_parity_workers)
  3285. goto fail_scrub_parity_workers;
  3286. }
  3287. ++fs_info->scrub_workers_refcnt;
  3288. return 0;
  3289. fail_scrub_parity_workers:
  3290. btrfs_destroy_workqueue(fs_info->scrub_wr_completion_workers);
  3291. fail_scrub_wr_completion_workers:
  3292. btrfs_destroy_workqueue(fs_info->scrub_workers);
  3293. fail_scrub_workers:
  3294. return -ENOMEM;
  3295. }
  3296. int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
  3297. u64 end, struct btrfs_scrub_progress *progress,
  3298. int readonly, int is_dev_replace)
  3299. {
  3300. struct scrub_ctx *sctx;
  3301. int ret;
  3302. struct btrfs_device *dev;
  3303. unsigned int nofs_flag;
  3304. struct btrfs_workqueue *scrub_workers = NULL;
  3305. struct btrfs_workqueue *scrub_wr_comp = NULL;
  3306. struct btrfs_workqueue *scrub_parity = NULL;
  3307. if (btrfs_fs_closing(fs_info))
  3308. return -EINVAL;
  3309. if (fs_info->nodesize > BTRFS_STRIPE_LEN) {
  3310. /*
  3311. * in this case scrub is unable to calculate the checksum
  3312. * the way scrub is implemented. Do not handle this
  3313. * situation at all because it won't ever happen.
  3314. */
  3315. btrfs_err(fs_info,
  3316. "scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails",
  3317. fs_info->nodesize,
  3318. BTRFS_STRIPE_LEN);
  3319. return -EINVAL;
  3320. }
  3321. if (fs_info->sectorsize != PAGE_SIZE) {
  3322. /* not supported for data w/o checksums */
  3323. btrfs_err_rl(fs_info,
  3324. "scrub: size assumption sectorsize != PAGE_SIZE (%d != %lu) fails",
  3325. fs_info->sectorsize, PAGE_SIZE);
  3326. return -EINVAL;
  3327. }
  3328. if (fs_info->nodesize >
  3329. PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK ||
  3330. fs_info->sectorsize > PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK) {
  3331. /*
  3332. * would exhaust the array bounds of pagev member in
  3333. * struct scrub_block
  3334. */
  3335. btrfs_err(fs_info,
  3336. "scrub: size assumption nodesize and sectorsize <= SCRUB_MAX_PAGES_PER_BLOCK (%d <= %d && %d <= %d) fails",
  3337. fs_info->nodesize,
  3338. SCRUB_MAX_PAGES_PER_BLOCK,
  3339. fs_info->sectorsize,
  3340. SCRUB_MAX_PAGES_PER_BLOCK);
  3341. return -EINVAL;
  3342. }
  3343. /* Allocate outside of device_list_mutex */
  3344. sctx = scrub_setup_ctx(fs_info, is_dev_replace);
  3345. if (IS_ERR(sctx))
  3346. return PTR_ERR(sctx);
  3347. mutex_lock(&fs_info->fs_devices->device_list_mutex);
  3348. dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true);
  3349. if (!dev || (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) &&
  3350. !is_dev_replace)) {
  3351. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  3352. ret = -ENODEV;
  3353. goto out_free_ctx;
  3354. }
  3355. if (!is_dev_replace && !readonly &&
  3356. !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) {
  3357. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  3358. btrfs_err_in_rcu(fs_info, "scrub: device %s is not writable",
  3359. rcu_str_deref(dev->name));
  3360. ret = -EROFS;
  3361. goto out_free_ctx;
  3362. }
  3363. mutex_lock(&fs_info->scrub_lock);
  3364. if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
  3365. test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &dev->dev_state)) {
  3366. mutex_unlock(&fs_info->scrub_lock);
  3367. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  3368. ret = -EIO;
  3369. goto out_free_ctx;
  3370. }
  3371. btrfs_dev_replace_read_lock(&fs_info->dev_replace);
  3372. if (dev->scrub_ctx ||
  3373. (!is_dev_replace &&
  3374. btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))) {
  3375. btrfs_dev_replace_read_unlock(&fs_info->dev_replace);
  3376. mutex_unlock(&fs_info->scrub_lock);
  3377. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  3378. ret = -EINPROGRESS;
  3379. goto out_free_ctx;
  3380. }
  3381. btrfs_dev_replace_read_unlock(&fs_info->dev_replace);
  3382. ret = scrub_workers_get(fs_info, is_dev_replace);
  3383. if (ret) {
  3384. mutex_unlock(&fs_info->scrub_lock);
  3385. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  3386. goto out_free_ctx;
  3387. }
  3388. sctx->readonly = readonly;
  3389. dev->scrub_ctx = sctx;
  3390. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  3391. /*
  3392. * checking @scrub_pause_req here, we can avoid
  3393. * race between committing transaction and scrubbing.
  3394. */
  3395. __scrub_blocked_if_needed(fs_info);
  3396. atomic_inc(&fs_info->scrubs_running);
  3397. mutex_unlock(&fs_info->scrub_lock);
  3398. /*
  3399. * In order to avoid deadlock with reclaim when there is a transaction
  3400. * trying to pause scrub, make sure we use GFP_NOFS for all the
  3401. * allocations done at btrfs_scrub_pages() and scrub_pages_for_parity()
  3402. * invoked by our callees. The pausing request is done when the
  3403. * transaction commit starts, and it blocks the transaction until scrub
  3404. * is paused (done at specific points at scrub_stripe() or right above
  3405. * before incrementing fs_info->scrubs_running).
  3406. */
  3407. nofs_flag = memalloc_nofs_save();
  3408. if (!is_dev_replace) {
  3409. /*
  3410. * by holding device list mutex, we can
  3411. * kick off writing super in log tree sync.
  3412. */
  3413. mutex_lock(&fs_info->fs_devices->device_list_mutex);
  3414. ret = scrub_supers(sctx, dev);
  3415. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  3416. }
  3417. if (!ret)
  3418. ret = scrub_enumerate_chunks(sctx, dev, start, end);
  3419. memalloc_nofs_restore(nofs_flag);
  3420. wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
  3421. atomic_dec(&fs_info->scrubs_running);
  3422. wake_up(&fs_info->scrub_pause_wait);
  3423. wait_event(sctx->list_wait, atomic_read(&sctx->workers_pending) == 0);
  3424. if (progress)
  3425. memcpy(progress, &sctx->stat, sizeof(*progress));
  3426. mutex_lock(&fs_info->scrub_lock);
  3427. dev->scrub_ctx = NULL;
  3428. if (--fs_info->scrub_workers_refcnt == 0) {
  3429. scrub_workers = fs_info->scrub_workers;
  3430. scrub_wr_comp = fs_info->scrub_wr_completion_workers;
  3431. scrub_parity = fs_info->scrub_parity_workers;
  3432. }
  3433. mutex_unlock(&fs_info->scrub_lock);
  3434. btrfs_destroy_workqueue(scrub_workers);
  3435. btrfs_destroy_workqueue(scrub_wr_comp);
  3436. btrfs_destroy_workqueue(scrub_parity);
  3437. scrub_put_ctx(sctx);
  3438. return ret;
  3439. out_free_ctx:
  3440. scrub_free_ctx(sctx);
  3441. return ret;
  3442. }
  3443. void btrfs_scrub_pause(struct btrfs_fs_info *fs_info)
  3444. {
  3445. mutex_lock(&fs_info->scrub_lock);
  3446. atomic_inc(&fs_info->scrub_pause_req);
  3447. while (atomic_read(&fs_info->scrubs_paused) !=
  3448. atomic_read(&fs_info->scrubs_running)) {
  3449. mutex_unlock(&fs_info->scrub_lock);
  3450. wait_event(fs_info->scrub_pause_wait,
  3451. atomic_read(&fs_info->scrubs_paused) ==
  3452. atomic_read(&fs_info->scrubs_running));
  3453. mutex_lock(&fs_info->scrub_lock);
  3454. }
  3455. mutex_unlock(&fs_info->scrub_lock);
  3456. }
  3457. void btrfs_scrub_continue(struct btrfs_fs_info *fs_info)
  3458. {
  3459. atomic_dec(&fs_info->scrub_pause_req);
  3460. wake_up(&fs_info->scrub_pause_wait);
  3461. }
  3462. int btrfs_scrub_cancel(struct btrfs_fs_info *fs_info)
  3463. {
  3464. mutex_lock(&fs_info->scrub_lock);
  3465. if (!atomic_read(&fs_info->scrubs_running)) {
  3466. mutex_unlock(&fs_info->scrub_lock);
  3467. return -ENOTCONN;
  3468. }
  3469. atomic_inc(&fs_info->scrub_cancel_req);
  3470. while (atomic_read(&fs_info->scrubs_running)) {
  3471. mutex_unlock(&fs_info->scrub_lock);
  3472. wait_event(fs_info->scrub_pause_wait,
  3473. atomic_read(&fs_info->scrubs_running) == 0);
  3474. mutex_lock(&fs_info->scrub_lock);
  3475. }
  3476. atomic_dec(&fs_info->scrub_cancel_req);
  3477. mutex_unlock(&fs_info->scrub_lock);
  3478. return 0;
  3479. }
  3480. int btrfs_scrub_cancel_dev(struct btrfs_fs_info *fs_info,
  3481. struct btrfs_device *dev)
  3482. {
  3483. struct scrub_ctx *sctx;
  3484. mutex_lock(&fs_info->scrub_lock);
  3485. sctx = dev->scrub_ctx;
  3486. if (!sctx) {
  3487. mutex_unlock(&fs_info->scrub_lock);
  3488. return -ENOTCONN;
  3489. }
  3490. atomic_inc(&sctx->cancel_req);
  3491. while (dev->scrub_ctx) {
  3492. mutex_unlock(&fs_info->scrub_lock);
  3493. wait_event(fs_info->scrub_pause_wait,
  3494. dev->scrub_ctx == NULL);
  3495. mutex_lock(&fs_info->scrub_lock);
  3496. }
  3497. mutex_unlock(&fs_info->scrub_lock);
  3498. return 0;
  3499. }
  3500. int btrfs_scrub_progress(struct btrfs_fs_info *fs_info, u64 devid,
  3501. struct btrfs_scrub_progress *progress)
  3502. {
  3503. struct btrfs_device *dev;
  3504. struct scrub_ctx *sctx = NULL;
  3505. mutex_lock(&fs_info->fs_devices->device_list_mutex);
  3506. dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true);
  3507. if (dev)
  3508. sctx = dev->scrub_ctx;
  3509. if (sctx)
  3510. memcpy(progress, &sctx->stat, sizeof(*progress));
  3511. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  3512. return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV;
  3513. }
  3514. static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
  3515. u64 extent_logical, u64 extent_len,
  3516. u64 *extent_physical,
  3517. struct btrfs_device **extent_dev,
  3518. int *extent_mirror_num)
  3519. {
  3520. u64 mapped_length;
  3521. struct btrfs_bio *bbio = NULL;
  3522. int ret;
  3523. mapped_length = extent_len;
  3524. ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, extent_logical,
  3525. &mapped_length, &bbio, 0);
  3526. if (ret || !bbio || mapped_length < extent_len ||
  3527. !bbio->stripes[0].dev->bdev) {
  3528. btrfs_put_bbio(bbio);
  3529. return;
  3530. }
  3531. *extent_physical = bbio->stripes[0].physical;
  3532. *extent_mirror_num = bbio->mirror_num;
  3533. *extent_dev = bbio->stripes[0].dev;
  3534. btrfs_put_bbio(bbio);
  3535. }