scrub.c 116 KB

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