scrub.c 114 KB

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