scrub.c 89 KB

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