scrub.c 89 KB

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