scrub.c 109 KB

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