scrub.c 110 KB

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