scrub.c 93 KB

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