scrub.c 105 KB

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