scrub.c 90 KB

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