scrub.c 116 KB

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