scrub.c 116 KB

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