scrub.c 89 KB

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