scrub.c 116 KB

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