scrub.c 90 KB

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