scrub.c 113 KB

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