scrub.c 110 KB

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