scrub.c 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285
  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. length = sblock_to_check->page_count * PAGE_SIZE;
  705. logical = sblock_to_check->pagev[0]->logical;
  706. generation = sblock_to_check->pagev[0]->generation;
  707. BUG_ON(sblock_to_check->pagev[0]->mirror_num < 1);
  708. failed_mirror_index = sblock_to_check->pagev[0]->mirror_num - 1;
  709. is_metadata = !(sblock_to_check->pagev[0]->flags &
  710. BTRFS_EXTENT_FLAG_DATA);
  711. have_csum = sblock_to_check->pagev[0]->have_csum;
  712. csum = sblock_to_check->pagev[0]->csum;
  713. dev = sblock_to_check->pagev[0]->dev;
  714. if (sctx->is_dev_replace && !is_metadata && !have_csum) {
  715. sblocks_for_recheck = NULL;
  716. goto nodatasum_case;
  717. }
  718. /*
  719. * read all mirrors one after the other. This includes to
  720. * re-read the extent or metadata block that failed (that was
  721. * the cause that this fixup code is called) another time,
  722. * page by page this time in order to know which pages
  723. * caused I/O errors and which ones are good (for all mirrors).
  724. * It is the goal to handle the situation when more than one
  725. * mirror contains I/O errors, but the errors do not
  726. * overlap, i.e. the data can be repaired by selecting the
  727. * pages from those mirrors without I/O error on the
  728. * particular pages. One example (with blocks >= 2 * PAGE_SIZE)
  729. * would be that mirror #1 has an I/O error on the first page,
  730. * the second page is good, and mirror #2 has an I/O error on
  731. * the second page, but the first page is good.
  732. * Then the first page of the first mirror can be repaired by
  733. * taking the first page of the second mirror, and the
  734. * second page of the second mirror can be repaired by
  735. * copying the contents of the 2nd page of the 1st mirror.
  736. * One more note: if the pages of one mirror contain I/O
  737. * errors, the checksum cannot be verified. In order to get
  738. * the best data for repairing, the first attempt is to find
  739. * a mirror without I/O errors and with a validated checksum.
  740. * Only if this is not possible, the pages are picked from
  741. * mirrors with I/O errors without considering the checksum.
  742. * If the latter is the case, at the end, the checksum of the
  743. * repaired area is verified in order to correctly maintain
  744. * the statistics.
  745. */
  746. sblocks_for_recheck = kzalloc(BTRFS_MAX_MIRRORS *
  747. sizeof(*sblocks_for_recheck),
  748. GFP_NOFS);
  749. if (!sblocks_for_recheck) {
  750. spin_lock(&sctx->stat_lock);
  751. sctx->stat.malloc_errors++;
  752. sctx->stat.read_errors++;
  753. sctx->stat.uncorrectable_errors++;
  754. spin_unlock(&sctx->stat_lock);
  755. btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
  756. goto out;
  757. }
  758. /* setup the context, map the logical blocks and alloc the pages */
  759. ret = scrub_setup_recheck_block(sctx, fs_info, sblock_to_check, length,
  760. logical, sblocks_for_recheck);
  761. if (ret) {
  762. spin_lock(&sctx->stat_lock);
  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. BUG_ON(failed_mirror_index >= BTRFS_MAX_MIRRORS);
  770. sblock_bad = sblocks_for_recheck + failed_mirror_index;
  771. /* build and submit the bios for the failed mirror, check checksums */
  772. scrub_recheck_block(fs_info, sblock_bad, is_metadata, have_csum,
  773. csum, generation, sctx->csum_size);
  774. if (!sblock_bad->header_error && !sblock_bad->checksum_error &&
  775. sblock_bad->no_io_error_seen) {
  776. /*
  777. * the error disappeared after reading page by page, or
  778. * the area was part of a huge bio and other parts of the
  779. * bio caused I/O errors, or the block layer merged several
  780. * read requests into one and the error is caused by a
  781. * different bio (usually one of the two latter cases is
  782. * the cause)
  783. */
  784. spin_lock(&sctx->stat_lock);
  785. sctx->stat.unverified_errors++;
  786. spin_unlock(&sctx->stat_lock);
  787. if (sctx->is_dev_replace)
  788. scrub_write_block_to_dev_replace(sblock_bad);
  789. goto out;
  790. }
  791. if (!sblock_bad->no_io_error_seen) {
  792. spin_lock(&sctx->stat_lock);
  793. sctx->stat.read_errors++;
  794. spin_unlock(&sctx->stat_lock);
  795. if (__ratelimit(&_rs))
  796. scrub_print_warning("i/o error", sblock_to_check);
  797. btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
  798. } else if (sblock_bad->checksum_error) {
  799. spin_lock(&sctx->stat_lock);
  800. sctx->stat.csum_errors++;
  801. spin_unlock(&sctx->stat_lock);
  802. if (__ratelimit(&_rs))
  803. scrub_print_warning("checksum error", sblock_to_check);
  804. btrfs_dev_stat_inc_and_print(dev,
  805. BTRFS_DEV_STAT_CORRUPTION_ERRS);
  806. } else if (sblock_bad->header_error) {
  807. spin_lock(&sctx->stat_lock);
  808. sctx->stat.verify_errors++;
  809. spin_unlock(&sctx->stat_lock);
  810. if (__ratelimit(&_rs))
  811. scrub_print_warning("checksum/header error",
  812. sblock_to_check);
  813. if (sblock_bad->generation_error)
  814. btrfs_dev_stat_inc_and_print(dev,
  815. BTRFS_DEV_STAT_GENERATION_ERRS);
  816. else
  817. btrfs_dev_stat_inc_and_print(dev,
  818. BTRFS_DEV_STAT_CORRUPTION_ERRS);
  819. }
  820. if (sctx->readonly && !sctx->is_dev_replace)
  821. goto did_not_correct_error;
  822. if (!is_metadata && !have_csum) {
  823. struct scrub_fixup_nodatasum *fixup_nodatasum;
  824. nodatasum_case:
  825. WARN_ON(sctx->is_dev_replace);
  826. /*
  827. * !is_metadata and !have_csum, this means that the data
  828. * might not be COW'ed, that it might be modified
  829. * concurrently. The general strategy to work on the
  830. * commit root does not help in the case when COW is not
  831. * used.
  832. */
  833. fixup_nodatasum = kzalloc(sizeof(*fixup_nodatasum), GFP_NOFS);
  834. if (!fixup_nodatasum)
  835. goto did_not_correct_error;
  836. fixup_nodatasum->sctx = sctx;
  837. fixup_nodatasum->dev = dev;
  838. fixup_nodatasum->logical = logical;
  839. fixup_nodatasum->root = fs_info->extent_root;
  840. fixup_nodatasum->mirror_num = failed_mirror_index + 1;
  841. scrub_pending_trans_workers_inc(sctx);
  842. fixup_nodatasum->work.func = scrub_fixup_nodatasum;
  843. btrfs_queue_worker(&fs_info->scrub_workers,
  844. &fixup_nodatasum->work);
  845. goto out;
  846. }
  847. /*
  848. * now build and submit the bios for the other mirrors, check
  849. * checksums.
  850. * First try to pick the mirror which is completely without I/O
  851. * errors and also does not have a checksum error.
  852. * If one is found, and if a checksum is present, the full block
  853. * that is known to contain an error is rewritten. Afterwards
  854. * the block is known to be corrected.
  855. * If a mirror is found which is completely correct, and no
  856. * checksum is present, only those pages are rewritten that had
  857. * an I/O error in the block to be repaired, since it cannot be
  858. * determined, which copy of the other pages is better (and it
  859. * could happen otherwise that a correct page would be
  860. * overwritten by a bad one).
  861. */
  862. for (mirror_index = 0;
  863. mirror_index < BTRFS_MAX_MIRRORS &&
  864. sblocks_for_recheck[mirror_index].page_count > 0;
  865. mirror_index++) {
  866. struct scrub_block *sblock_other;
  867. if (mirror_index == failed_mirror_index)
  868. continue;
  869. sblock_other = sblocks_for_recheck + mirror_index;
  870. /* build and submit the bios, check checksums */
  871. scrub_recheck_block(fs_info, sblock_other, is_metadata,
  872. have_csum, csum, generation,
  873. sctx->csum_size);
  874. if (!sblock_other->header_error &&
  875. !sblock_other->checksum_error &&
  876. sblock_other->no_io_error_seen) {
  877. if (sctx->is_dev_replace) {
  878. scrub_write_block_to_dev_replace(sblock_other);
  879. } else {
  880. int force_write = is_metadata || have_csum;
  881. ret = scrub_repair_block_from_good_copy(
  882. sblock_bad, sblock_other,
  883. force_write);
  884. }
  885. if (0 == ret)
  886. goto corrected_error;
  887. }
  888. }
  889. /*
  890. * for dev_replace, pick good pages and write to the target device.
  891. */
  892. if (sctx->is_dev_replace) {
  893. success = 1;
  894. for (page_num = 0; page_num < sblock_bad->page_count;
  895. page_num++) {
  896. int sub_success;
  897. sub_success = 0;
  898. for (mirror_index = 0;
  899. mirror_index < BTRFS_MAX_MIRRORS &&
  900. sblocks_for_recheck[mirror_index].page_count > 0;
  901. mirror_index++) {
  902. struct scrub_block *sblock_other =
  903. sblocks_for_recheck + mirror_index;
  904. struct scrub_page *page_other =
  905. sblock_other->pagev[page_num];
  906. if (!page_other->io_error) {
  907. ret = scrub_write_page_to_dev_replace(
  908. sblock_other, page_num);
  909. if (ret == 0) {
  910. /* succeeded for this page */
  911. sub_success = 1;
  912. break;
  913. } else {
  914. btrfs_dev_replace_stats_inc(
  915. &sctx->dev_root->
  916. fs_info->dev_replace.
  917. num_write_errors);
  918. }
  919. }
  920. }
  921. if (!sub_success) {
  922. /*
  923. * did not find a mirror to fetch the page
  924. * from. scrub_write_page_to_dev_replace()
  925. * handles this case (page->io_error), by
  926. * filling the block with zeros before
  927. * submitting the write request
  928. */
  929. success = 0;
  930. ret = scrub_write_page_to_dev_replace(
  931. sblock_bad, page_num);
  932. if (ret)
  933. btrfs_dev_replace_stats_inc(
  934. &sctx->dev_root->fs_info->
  935. dev_replace.num_write_errors);
  936. }
  937. }
  938. goto out;
  939. }
  940. /*
  941. * for regular scrub, repair those pages that are errored.
  942. * In case of I/O errors in the area that is supposed to be
  943. * repaired, continue by picking good copies of those pages.
  944. * Select the good pages from mirrors to rewrite bad pages from
  945. * the area to fix. Afterwards verify the checksum of the block
  946. * that is supposed to be repaired. This verification step is
  947. * only done for the purpose of statistic counting and for the
  948. * final scrub report, whether errors remain.
  949. * A perfect algorithm could make use of the checksum and try
  950. * all possible combinations of pages from the different mirrors
  951. * until the checksum verification succeeds. For example, when
  952. * the 2nd page of mirror #1 faces I/O errors, and the 2nd page
  953. * of mirror #2 is readable but the final checksum test fails,
  954. * then the 2nd page of mirror #3 could be tried, whether now
  955. * the final checksum succeedes. But this would be a rare
  956. * exception and is therefore not implemented. At least it is
  957. * avoided that the good copy is overwritten.
  958. * A more useful improvement would be to pick the sectors
  959. * without I/O error based on sector sizes (512 bytes on legacy
  960. * disks) instead of on PAGE_SIZE. Then maybe 512 byte of one
  961. * mirror could be repaired by taking 512 byte of a different
  962. * mirror, even if other 512 byte sectors in the same PAGE_SIZE
  963. * area are unreadable.
  964. */
  965. /* can only fix I/O errors from here on */
  966. if (sblock_bad->no_io_error_seen)
  967. goto did_not_correct_error;
  968. success = 1;
  969. for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
  970. struct scrub_page *page_bad = sblock_bad->pagev[page_num];
  971. if (!page_bad->io_error)
  972. continue;
  973. for (mirror_index = 0;
  974. mirror_index < BTRFS_MAX_MIRRORS &&
  975. sblocks_for_recheck[mirror_index].page_count > 0;
  976. mirror_index++) {
  977. struct scrub_block *sblock_other = sblocks_for_recheck +
  978. mirror_index;
  979. struct scrub_page *page_other = sblock_other->pagev[
  980. page_num];
  981. if (!page_other->io_error) {
  982. ret = scrub_repair_page_from_good_copy(
  983. sblock_bad, sblock_other, page_num, 0);
  984. if (0 == ret) {
  985. page_bad->io_error = 0;
  986. break; /* succeeded for this page */
  987. }
  988. }
  989. }
  990. if (page_bad->io_error) {
  991. /* did not find a mirror to copy the page from */
  992. success = 0;
  993. }
  994. }
  995. if (success) {
  996. if (is_metadata || have_csum) {
  997. /*
  998. * need to verify the checksum now that all
  999. * sectors on disk are repaired (the write
  1000. * request for data to be repaired is on its way).
  1001. * Just be lazy and use scrub_recheck_block()
  1002. * which re-reads the data before the checksum
  1003. * is verified, but most likely the data comes out
  1004. * of the page cache.
  1005. */
  1006. scrub_recheck_block(fs_info, sblock_bad,
  1007. is_metadata, have_csum, csum,
  1008. generation, sctx->csum_size);
  1009. if (!sblock_bad->header_error &&
  1010. !sblock_bad->checksum_error &&
  1011. sblock_bad->no_io_error_seen)
  1012. goto corrected_error;
  1013. else
  1014. goto did_not_correct_error;
  1015. } else {
  1016. corrected_error:
  1017. spin_lock(&sctx->stat_lock);
  1018. sctx->stat.corrected_errors++;
  1019. spin_unlock(&sctx->stat_lock);
  1020. printk_ratelimited_in_rcu(KERN_ERR
  1021. "btrfs: fixed up error at logical %llu on dev %s\n",
  1022. (unsigned long long)logical,
  1023. rcu_str_deref(dev->name));
  1024. }
  1025. } else {
  1026. did_not_correct_error:
  1027. spin_lock(&sctx->stat_lock);
  1028. sctx->stat.uncorrectable_errors++;
  1029. spin_unlock(&sctx->stat_lock);
  1030. printk_ratelimited_in_rcu(KERN_ERR
  1031. "btrfs: unable to fixup (regular) error at logical %llu on dev %s\n",
  1032. (unsigned long long)logical,
  1033. rcu_str_deref(dev->name));
  1034. }
  1035. out:
  1036. if (sblocks_for_recheck) {
  1037. for (mirror_index = 0; mirror_index < BTRFS_MAX_MIRRORS;
  1038. mirror_index++) {
  1039. struct scrub_block *sblock = sblocks_for_recheck +
  1040. mirror_index;
  1041. int page_index;
  1042. for (page_index = 0; page_index < sblock->page_count;
  1043. page_index++) {
  1044. sblock->pagev[page_index]->sblock = NULL;
  1045. scrub_page_put(sblock->pagev[page_index]);
  1046. }
  1047. }
  1048. kfree(sblocks_for_recheck);
  1049. }
  1050. return 0;
  1051. }
  1052. static int scrub_setup_recheck_block(struct scrub_ctx *sctx,
  1053. struct btrfs_fs_info *fs_info,
  1054. struct scrub_block *original_sblock,
  1055. u64 length, u64 logical,
  1056. struct scrub_block *sblocks_for_recheck)
  1057. {
  1058. int page_index;
  1059. int mirror_index;
  1060. int ret;
  1061. /*
  1062. * note: the two members ref_count and outstanding_pages
  1063. * are not used (and not set) in the blocks that are used for
  1064. * the recheck procedure
  1065. */
  1066. page_index = 0;
  1067. while (length > 0) {
  1068. u64 sublen = min_t(u64, length, PAGE_SIZE);
  1069. u64 mapped_length = sublen;
  1070. struct btrfs_bio *bbio = NULL;
  1071. /*
  1072. * with a length of PAGE_SIZE, each returned stripe
  1073. * represents one mirror
  1074. */
  1075. ret = btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS, logical,
  1076. &mapped_length, &bbio, 0);
  1077. if (ret || !bbio || mapped_length < sublen) {
  1078. kfree(bbio);
  1079. return -EIO;
  1080. }
  1081. BUG_ON(page_index >= SCRUB_PAGES_PER_RD_BIO);
  1082. for (mirror_index = 0; mirror_index < (int)bbio->num_stripes;
  1083. mirror_index++) {
  1084. struct scrub_block *sblock;
  1085. struct scrub_page *page;
  1086. if (mirror_index >= BTRFS_MAX_MIRRORS)
  1087. continue;
  1088. sblock = sblocks_for_recheck + mirror_index;
  1089. sblock->sctx = sctx;
  1090. page = kzalloc(sizeof(*page), GFP_NOFS);
  1091. if (!page) {
  1092. leave_nomem:
  1093. spin_lock(&sctx->stat_lock);
  1094. sctx->stat.malloc_errors++;
  1095. spin_unlock(&sctx->stat_lock);
  1096. kfree(bbio);
  1097. return -ENOMEM;
  1098. }
  1099. scrub_page_get(page);
  1100. sblock->pagev[page_index] = page;
  1101. page->logical = logical;
  1102. page->physical = bbio->stripes[mirror_index].physical;
  1103. BUG_ON(page_index >= original_sblock->page_count);
  1104. page->physical_for_dev_replace =
  1105. original_sblock->pagev[page_index]->
  1106. physical_for_dev_replace;
  1107. /* for missing devices, dev->bdev is NULL */
  1108. page->dev = bbio->stripes[mirror_index].dev;
  1109. page->mirror_num = mirror_index + 1;
  1110. sblock->page_count++;
  1111. page->page = alloc_page(GFP_NOFS);
  1112. if (!page->page)
  1113. goto leave_nomem;
  1114. }
  1115. kfree(bbio);
  1116. length -= sublen;
  1117. logical += sublen;
  1118. page_index++;
  1119. }
  1120. return 0;
  1121. }
  1122. /*
  1123. * this function will check the on disk data for checksum errors, header
  1124. * errors and read I/O errors. If any I/O errors happen, the exact pages
  1125. * which are errored are marked as being bad. The goal is to enable scrub
  1126. * to take those pages that are not errored from all the mirrors so that
  1127. * the pages that are errored in the just handled mirror can be repaired.
  1128. */
  1129. static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
  1130. struct scrub_block *sblock, int is_metadata,
  1131. int have_csum, u8 *csum, u64 generation,
  1132. u16 csum_size)
  1133. {
  1134. int page_num;
  1135. sblock->no_io_error_seen = 1;
  1136. sblock->header_error = 0;
  1137. sblock->checksum_error = 0;
  1138. for (page_num = 0; page_num < sblock->page_count; page_num++) {
  1139. struct bio *bio;
  1140. struct scrub_page *page = sblock->pagev[page_num];
  1141. DECLARE_COMPLETION_ONSTACK(complete);
  1142. if (page->dev->bdev == NULL) {
  1143. page->io_error = 1;
  1144. sblock->no_io_error_seen = 0;
  1145. continue;
  1146. }
  1147. WARN_ON(!page->page);
  1148. bio = bio_alloc(GFP_NOFS, 1);
  1149. if (!bio) {
  1150. page->io_error = 1;
  1151. sblock->no_io_error_seen = 0;
  1152. continue;
  1153. }
  1154. bio->bi_bdev = page->dev->bdev;
  1155. bio->bi_sector = page->physical >> 9;
  1156. bio->bi_end_io = scrub_complete_bio_end_io;
  1157. bio->bi_private = &complete;
  1158. bio_add_page(bio, page->page, PAGE_SIZE, 0);
  1159. btrfsic_submit_bio(READ, bio);
  1160. /* this will also unplug the queue */
  1161. wait_for_completion(&complete);
  1162. page->io_error = !test_bit(BIO_UPTODATE, &bio->bi_flags);
  1163. if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
  1164. sblock->no_io_error_seen = 0;
  1165. bio_put(bio);
  1166. }
  1167. if (sblock->no_io_error_seen)
  1168. scrub_recheck_block_checksum(fs_info, sblock, is_metadata,
  1169. have_csum, csum, generation,
  1170. csum_size);
  1171. return;
  1172. }
  1173. static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
  1174. struct scrub_block *sblock,
  1175. int is_metadata, int have_csum,
  1176. const u8 *csum, u64 generation,
  1177. u16 csum_size)
  1178. {
  1179. int page_num;
  1180. u8 calculated_csum[BTRFS_CSUM_SIZE];
  1181. u32 crc = ~(u32)0;
  1182. struct btrfs_root *root = fs_info->extent_root;
  1183. void *mapped_buffer;
  1184. WARN_ON(!sblock->pagev[0]->page);
  1185. if (is_metadata) {
  1186. struct btrfs_header *h;
  1187. mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
  1188. h = (struct btrfs_header *)mapped_buffer;
  1189. if (sblock->pagev[0]->logical != le64_to_cpu(h->bytenr) ||
  1190. memcmp(h->fsid, fs_info->fsid, BTRFS_UUID_SIZE) ||
  1191. memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
  1192. BTRFS_UUID_SIZE)) {
  1193. sblock->header_error = 1;
  1194. } else if (generation != le64_to_cpu(h->generation)) {
  1195. sblock->header_error = 1;
  1196. sblock->generation_error = 1;
  1197. }
  1198. csum = h->csum;
  1199. } else {
  1200. if (!have_csum)
  1201. return;
  1202. mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
  1203. }
  1204. for (page_num = 0;;) {
  1205. if (page_num == 0 && is_metadata)
  1206. crc = btrfs_csum_data(root,
  1207. ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE,
  1208. crc, PAGE_SIZE - BTRFS_CSUM_SIZE);
  1209. else
  1210. crc = btrfs_csum_data(root, mapped_buffer, crc,
  1211. PAGE_SIZE);
  1212. kunmap_atomic(mapped_buffer);
  1213. page_num++;
  1214. if (page_num >= sblock->page_count)
  1215. break;
  1216. WARN_ON(!sblock->pagev[page_num]->page);
  1217. mapped_buffer = kmap_atomic(sblock->pagev[page_num]->page);
  1218. }
  1219. btrfs_csum_final(crc, calculated_csum);
  1220. if (memcmp(calculated_csum, csum, csum_size))
  1221. sblock->checksum_error = 1;
  1222. }
  1223. static void scrub_complete_bio_end_io(struct bio *bio, int err)
  1224. {
  1225. complete((struct completion *)bio->bi_private);
  1226. }
  1227. static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
  1228. struct scrub_block *sblock_good,
  1229. int force_write)
  1230. {
  1231. int page_num;
  1232. int ret = 0;
  1233. for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
  1234. int ret_sub;
  1235. ret_sub = scrub_repair_page_from_good_copy(sblock_bad,
  1236. sblock_good,
  1237. page_num,
  1238. force_write);
  1239. if (ret_sub)
  1240. ret = ret_sub;
  1241. }
  1242. return ret;
  1243. }
  1244. static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
  1245. struct scrub_block *sblock_good,
  1246. int page_num, int force_write)
  1247. {
  1248. struct scrub_page *page_bad = sblock_bad->pagev[page_num];
  1249. struct scrub_page *page_good = sblock_good->pagev[page_num];
  1250. BUG_ON(page_bad->page == NULL);
  1251. BUG_ON(page_good->page == NULL);
  1252. if (force_write || sblock_bad->header_error ||
  1253. sblock_bad->checksum_error || page_bad->io_error) {
  1254. struct bio *bio;
  1255. int ret;
  1256. DECLARE_COMPLETION_ONSTACK(complete);
  1257. if (!page_bad->dev->bdev) {
  1258. printk_ratelimited(KERN_WARNING
  1259. "btrfs: scrub_repair_page_from_good_copy(bdev == NULL) is unexpected!\n");
  1260. return -EIO;
  1261. }
  1262. bio = bio_alloc(GFP_NOFS, 1);
  1263. if (!bio)
  1264. return -EIO;
  1265. bio->bi_bdev = page_bad->dev->bdev;
  1266. bio->bi_sector = page_bad->physical >> 9;
  1267. bio->bi_end_io = scrub_complete_bio_end_io;
  1268. bio->bi_private = &complete;
  1269. ret = bio_add_page(bio, page_good->page, PAGE_SIZE, 0);
  1270. if (PAGE_SIZE != ret) {
  1271. bio_put(bio);
  1272. return -EIO;
  1273. }
  1274. btrfsic_submit_bio(WRITE, bio);
  1275. /* this will also unplug the queue */
  1276. wait_for_completion(&complete);
  1277. if (!bio_flagged(bio, BIO_UPTODATE)) {
  1278. btrfs_dev_stat_inc_and_print(page_bad->dev,
  1279. BTRFS_DEV_STAT_WRITE_ERRS);
  1280. btrfs_dev_replace_stats_inc(
  1281. &sblock_bad->sctx->dev_root->fs_info->
  1282. dev_replace.num_write_errors);
  1283. bio_put(bio);
  1284. return -EIO;
  1285. }
  1286. bio_put(bio);
  1287. }
  1288. return 0;
  1289. }
  1290. static void scrub_write_block_to_dev_replace(struct scrub_block *sblock)
  1291. {
  1292. int page_num;
  1293. for (page_num = 0; page_num < sblock->page_count; page_num++) {
  1294. int ret;
  1295. ret = scrub_write_page_to_dev_replace(sblock, page_num);
  1296. if (ret)
  1297. btrfs_dev_replace_stats_inc(
  1298. &sblock->sctx->dev_root->fs_info->dev_replace.
  1299. num_write_errors);
  1300. }
  1301. }
  1302. static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
  1303. int page_num)
  1304. {
  1305. struct scrub_page *spage = sblock->pagev[page_num];
  1306. BUG_ON(spage->page == NULL);
  1307. if (spage->io_error) {
  1308. void *mapped_buffer = kmap_atomic(spage->page);
  1309. memset(mapped_buffer, 0, PAGE_CACHE_SIZE);
  1310. flush_dcache_page(spage->page);
  1311. kunmap_atomic(mapped_buffer);
  1312. }
  1313. return scrub_add_page_to_wr_bio(sblock->sctx, spage);
  1314. }
  1315. static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
  1316. struct scrub_page *spage)
  1317. {
  1318. struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx;
  1319. struct scrub_bio *sbio;
  1320. int ret;
  1321. mutex_lock(&wr_ctx->wr_lock);
  1322. again:
  1323. if (!wr_ctx->wr_curr_bio) {
  1324. wr_ctx->wr_curr_bio = kzalloc(sizeof(*wr_ctx->wr_curr_bio),
  1325. GFP_NOFS);
  1326. if (!wr_ctx->wr_curr_bio) {
  1327. mutex_unlock(&wr_ctx->wr_lock);
  1328. return -ENOMEM;
  1329. }
  1330. wr_ctx->wr_curr_bio->sctx = sctx;
  1331. wr_ctx->wr_curr_bio->page_count = 0;
  1332. }
  1333. sbio = wr_ctx->wr_curr_bio;
  1334. if (sbio->page_count == 0) {
  1335. struct bio *bio;
  1336. sbio->physical = spage->physical_for_dev_replace;
  1337. sbio->logical = spage->logical;
  1338. sbio->dev = wr_ctx->tgtdev;
  1339. bio = sbio->bio;
  1340. if (!bio) {
  1341. bio = bio_alloc(GFP_NOFS, wr_ctx->pages_per_wr_bio);
  1342. if (!bio) {
  1343. mutex_unlock(&wr_ctx->wr_lock);
  1344. return -ENOMEM;
  1345. }
  1346. sbio->bio = bio;
  1347. }
  1348. bio->bi_private = sbio;
  1349. bio->bi_end_io = scrub_wr_bio_end_io;
  1350. bio->bi_bdev = sbio->dev->bdev;
  1351. bio->bi_sector = sbio->physical >> 9;
  1352. sbio->err = 0;
  1353. } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
  1354. spage->physical_for_dev_replace ||
  1355. sbio->logical + sbio->page_count * PAGE_SIZE !=
  1356. spage->logical) {
  1357. scrub_wr_submit(sctx);
  1358. goto again;
  1359. }
  1360. ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
  1361. if (ret != PAGE_SIZE) {
  1362. if (sbio->page_count < 1) {
  1363. bio_put(sbio->bio);
  1364. sbio->bio = NULL;
  1365. mutex_unlock(&wr_ctx->wr_lock);
  1366. return -EIO;
  1367. }
  1368. scrub_wr_submit(sctx);
  1369. goto again;
  1370. }
  1371. sbio->pagev[sbio->page_count] = spage;
  1372. scrub_page_get(spage);
  1373. sbio->page_count++;
  1374. if (sbio->page_count == wr_ctx->pages_per_wr_bio)
  1375. scrub_wr_submit(sctx);
  1376. mutex_unlock(&wr_ctx->wr_lock);
  1377. return 0;
  1378. }
  1379. static void scrub_wr_submit(struct scrub_ctx *sctx)
  1380. {
  1381. struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx;
  1382. struct scrub_bio *sbio;
  1383. if (!wr_ctx->wr_curr_bio)
  1384. return;
  1385. sbio = wr_ctx->wr_curr_bio;
  1386. wr_ctx->wr_curr_bio = NULL;
  1387. WARN_ON(!sbio->bio->bi_bdev);
  1388. scrub_pending_bio_inc(sctx);
  1389. /* process all writes in a single worker thread. Then the block layer
  1390. * orders the requests before sending them to the driver which
  1391. * doubled the write performance on spinning disks when measured
  1392. * with Linux 3.5 */
  1393. btrfsic_submit_bio(WRITE, sbio->bio);
  1394. }
  1395. static void scrub_wr_bio_end_io(struct bio *bio, int err)
  1396. {
  1397. struct scrub_bio *sbio = bio->bi_private;
  1398. struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
  1399. sbio->err = err;
  1400. sbio->bio = bio;
  1401. sbio->work.func = scrub_wr_bio_end_io_worker;
  1402. btrfs_queue_worker(&fs_info->scrub_wr_completion_workers, &sbio->work);
  1403. }
  1404. static void scrub_wr_bio_end_io_worker(struct btrfs_work *work)
  1405. {
  1406. struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
  1407. struct scrub_ctx *sctx = sbio->sctx;
  1408. int i;
  1409. WARN_ON(sbio->page_count > SCRUB_PAGES_PER_WR_BIO);
  1410. if (sbio->err) {
  1411. struct btrfs_dev_replace *dev_replace =
  1412. &sbio->sctx->dev_root->fs_info->dev_replace;
  1413. for (i = 0; i < sbio->page_count; i++) {
  1414. struct scrub_page *spage = sbio->pagev[i];
  1415. spage->io_error = 1;
  1416. btrfs_dev_replace_stats_inc(&dev_replace->
  1417. num_write_errors);
  1418. }
  1419. }
  1420. for (i = 0; i < sbio->page_count; i++)
  1421. scrub_page_put(sbio->pagev[i]);
  1422. bio_put(sbio->bio);
  1423. kfree(sbio);
  1424. scrub_pending_bio_dec(sctx);
  1425. }
  1426. static int scrub_checksum(struct scrub_block *sblock)
  1427. {
  1428. u64 flags;
  1429. int ret;
  1430. WARN_ON(sblock->page_count < 1);
  1431. flags = sblock->pagev[0]->flags;
  1432. ret = 0;
  1433. if (flags & BTRFS_EXTENT_FLAG_DATA)
  1434. ret = scrub_checksum_data(sblock);
  1435. else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
  1436. ret = scrub_checksum_tree_block(sblock);
  1437. else if (flags & BTRFS_EXTENT_FLAG_SUPER)
  1438. (void)scrub_checksum_super(sblock);
  1439. else
  1440. WARN_ON(1);
  1441. if (ret)
  1442. scrub_handle_errored_block(sblock);
  1443. return ret;
  1444. }
  1445. static int scrub_checksum_data(struct scrub_block *sblock)
  1446. {
  1447. struct scrub_ctx *sctx = sblock->sctx;
  1448. u8 csum[BTRFS_CSUM_SIZE];
  1449. u8 *on_disk_csum;
  1450. struct page *page;
  1451. void *buffer;
  1452. u32 crc = ~(u32)0;
  1453. int fail = 0;
  1454. struct btrfs_root *root = sctx->dev_root;
  1455. u64 len;
  1456. int index;
  1457. BUG_ON(sblock->page_count < 1);
  1458. if (!sblock->pagev[0]->have_csum)
  1459. return 0;
  1460. on_disk_csum = sblock->pagev[0]->csum;
  1461. page = sblock->pagev[0]->page;
  1462. buffer = kmap_atomic(page);
  1463. len = sctx->sectorsize;
  1464. index = 0;
  1465. for (;;) {
  1466. u64 l = min_t(u64, len, PAGE_SIZE);
  1467. crc = btrfs_csum_data(root, buffer, crc, l);
  1468. kunmap_atomic(buffer);
  1469. len -= l;
  1470. if (len == 0)
  1471. break;
  1472. index++;
  1473. BUG_ON(index >= sblock->page_count);
  1474. BUG_ON(!sblock->pagev[index]->page);
  1475. page = sblock->pagev[index]->page;
  1476. buffer = kmap_atomic(page);
  1477. }
  1478. btrfs_csum_final(crc, csum);
  1479. if (memcmp(csum, on_disk_csum, sctx->csum_size))
  1480. fail = 1;
  1481. return fail;
  1482. }
  1483. static int scrub_checksum_tree_block(struct scrub_block *sblock)
  1484. {
  1485. struct scrub_ctx *sctx = sblock->sctx;
  1486. struct btrfs_header *h;
  1487. struct btrfs_root *root = sctx->dev_root;
  1488. struct btrfs_fs_info *fs_info = root->fs_info;
  1489. u8 calculated_csum[BTRFS_CSUM_SIZE];
  1490. u8 on_disk_csum[BTRFS_CSUM_SIZE];
  1491. struct page *page;
  1492. void *mapped_buffer;
  1493. u64 mapped_size;
  1494. void *p;
  1495. u32 crc = ~(u32)0;
  1496. int fail = 0;
  1497. int crc_fail = 0;
  1498. u64 len;
  1499. int index;
  1500. BUG_ON(sblock->page_count < 1);
  1501. page = sblock->pagev[0]->page;
  1502. mapped_buffer = kmap_atomic(page);
  1503. h = (struct btrfs_header *)mapped_buffer;
  1504. memcpy(on_disk_csum, h->csum, sctx->csum_size);
  1505. /*
  1506. * we don't use the getter functions here, as we
  1507. * a) don't have an extent buffer and
  1508. * b) the page is already kmapped
  1509. */
  1510. if (sblock->pagev[0]->logical != le64_to_cpu(h->bytenr))
  1511. ++fail;
  1512. if (sblock->pagev[0]->generation != le64_to_cpu(h->generation))
  1513. ++fail;
  1514. if (memcmp(h->fsid, fs_info->fsid, BTRFS_UUID_SIZE))
  1515. ++fail;
  1516. if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
  1517. BTRFS_UUID_SIZE))
  1518. ++fail;
  1519. WARN_ON(sctx->nodesize != sctx->leafsize);
  1520. len = sctx->nodesize - BTRFS_CSUM_SIZE;
  1521. mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
  1522. p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
  1523. index = 0;
  1524. for (;;) {
  1525. u64 l = min_t(u64, len, mapped_size);
  1526. crc = btrfs_csum_data(root, p, crc, l);
  1527. kunmap_atomic(mapped_buffer);
  1528. len -= l;
  1529. if (len == 0)
  1530. break;
  1531. index++;
  1532. BUG_ON(index >= sblock->page_count);
  1533. BUG_ON(!sblock->pagev[index]->page);
  1534. page = sblock->pagev[index]->page;
  1535. mapped_buffer = kmap_atomic(page);
  1536. mapped_size = PAGE_SIZE;
  1537. p = mapped_buffer;
  1538. }
  1539. btrfs_csum_final(crc, calculated_csum);
  1540. if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
  1541. ++crc_fail;
  1542. return fail || crc_fail;
  1543. }
  1544. static int scrub_checksum_super(struct scrub_block *sblock)
  1545. {
  1546. struct btrfs_super_block *s;
  1547. struct scrub_ctx *sctx = sblock->sctx;
  1548. struct btrfs_root *root = sctx->dev_root;
  1549. struct btrfs_fs_info *fs_info = root->fs_info;
  1550. u8 calculated_csum[BTRFS_CSUM_SIZE];
  1551. u8 on_disk_csum[BTRFS_CSUM_SIZE];
  1552. struct page *page;
  1553. void *mapped_buffer;
  1554. u64 mapped_size;
  1555. void *p;
  1556. u32 crc = ~(u32)0;
  1557. int fail_gen = 0;
  1558. int fail_cor = 0;
  1559. u64 len;
  1560. int index;
  1561. BUG_ON(sblock->page_count < 1);
  1562. page = sblock->pagev[0]->page;
  1563. mapped_buffer = kmap_atomic(page);
  1564. s = (struct btrfs_super_block *)mapped_buffer;
  1565. memcpy(on_disk_csum, s->csum, sctx->csum_size);
  1566. if (sblock->pagev[0]->logical != le64_to_cpu(s->bytenr))
  1567. ++fail_cor;
  1568. if (sblock->pagev[0]->generation != le64_to_cpu(s->generation))
  1569. ++fail_gen;
  1570. if (memcmp(s->fsid, fs_info->fsid, BTRFS_UUID_SIZE))
  1571. ++fail_cor;
  1572. len = BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE;
  1573. mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
  1574. p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
  1575. index = 0;
  1576. for (;;) {
  1577. u64 l = min_t(u64, len, mapped_size);
  1578. crc = btrfs_csum_data(root, p, crc, l);
  1579. kunmap_atomic(mapped_buffer);
  1580. len -= l;
  1581. if (len == 0)
  1582. break;
  1583. index++;
  1584. BUG_ON(index >= sblock->page_count);
  1585. BUG_ON(!sblock->pagev[index]->page);
  1586. page = sblock->pagev[index]->page;
  1587. mapped_buffer = kmap_atomic(page);
  1588. mapped_size = PAGE_SIZE;
  1589. p = mapped_buffer;
  1590. }
  1591. btrfs_csum_final(crc, calculated_csum);
  1592. if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
  1593. ++fail_cor;
  1594. if (fail_cor + fail_gen) {
  1595. /*
  1596. * if we find an error in a super block, we just report it.
  1597. * They will get written with the next transaction commit
  1598. * anyway
  1599. */
  1600. spin_lock(&sctx->stat_lock);
  1601. ++sctx->stat.super_errors;
  1602. spin_unlock(&sctx->stat_lock);
  1603. if (fail_cor)
  1604. btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
  1605. BTRFS_DEV_STAT_CORRUPTION_ERRS);
  1606. else
  1607. btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
  1608. BTRFS_DEV_STAT_GENERATION_ERRS);
  1609. }
  1610. return fail_cor + fail_gen;
  1611. }
  1612. static void scrub_block_get(struct scrub_block *sblock)
  1613. {
  1614. atomic_inc(&sblock->ref_count);
  1615. }
  1616. static void scrub_block_put(struct scrub_block *sblock)
  1617. {
  1618. if (atomic_dec_and_test(&sblock->ref_count)) {
  1619. int i;
  1620. for (i = 0; i < sblock->page_count; i++)
  1621. scrub_page_put(sblock->pagev[i]);
  1622. kfree(sblock);
  1623. }
  1624. }
  1625. static void scrub_page_get(struct scrub_page *spage)
  1626. {
  1627. atomic_inc(&spage->ref_count);
  1628. }
  1629. static void scrub_page_put(struct scrub_page *spage)
  1630. {
  1631. if (atomic_dec_and_test(&spage->ref_count)) {
  1632. if (spage->page)
  1633. __free_page(spage->page);
  1634. kfree(spage);
  1635. }
  1636. }
  1637. static void scrub_submit(struct scrub_ctx *sctx)
  1638. {
  1639. struct scrub_bio *sbio;
  1640. if (sctx->curr == -1)
  1641. return;
  1642. sbio = sctx->bios[sctx->curr];
  1643. sctx->curr = -1;
  1644. scrub_pending_bio_inc(sctx);
  1645. if (!sbio->bio->bi_bdev) {
  1646. /*
  1647. * this case should not happen. If btrfs_map_block() is
  1648. * wrong, it could happen for dev-replace operations on
  1649. * missing devices when no mirrors are available, but in
  1650. * this case it should already fail the mount.
  1651. * This case is handled correctly (but _very_ slowly).
  1652. */
  1653. printk_ratelimited(KERN_WARNING
  1654. "btrfs: scrub_submit(bio bdev == NULL) is unexpected!\n");
  1655. bio_endio(sbio->bio, -EIO);
  1656. } else {
  1657. btrfsic_submit_bio(READ, sbio->bio);
  1658. }
  1659. }
  1660. static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
  1661. struct scrub_page *spage)
  1662. {
  1663. struct scrub_block *sblock = spage->sblock;
  1664. struct scrub_bio *sbio;
  1665. int ret;
  1666. again:
  1667. /*
  1668. * grab a fresh bio or wait for one to become available
  1669. */
  1670. while (sctx->curr == -1) {
  1671. spin_lock(&sctx->list_lock);
  1672. sctx->curr = sctx->first_free;
  1673. if (sctx->curr != -1) {
  1674. sctx->first_free = sctx->bios[sctx->curr]->next_free;
  1675. sctx->bios[sctx->curr]->next_free = -1;
  1676. sctx->bios[sctx->curr]->page_count = 0;
  1677. spin_unlock(&sctx->list_lock);
  1678. } else {
  1679. spin_unlock(&sctx->list_lock);
  1680. wait_event(sctx->list_wait, sctx->first_free != -1);
  1681. }
  1682. }
  1683. sbio = sctx->bios[sctx->curr];
  1684. if (sbio->page_count == 0) {
  1685. struct bio *bio;
  1686. sbio->physical = spage->physical;
  1687. sbio->logical = spage->logical;
  1688. sbio->dev = spage->dev;
  1689. bio = sbio->bio;
  1690. if (!bio) {
  1691. bio = bio_alloc(GFP_NOFS, sctx->pages_per_rd_bio);
  1692. if (!bio)
  1693. return -ENOMEM;
  1694. sbio->bio = bio;
  1695. }
  1696. bio->bi_private = sbio;
  1697. bio->bi_end_io = scrub_bio_end_io;
  1698. bio->bi_bdev = sbio->dev->bdev;
  1699. bio->bi_sector = sbio->physical >> 9;
  1700. sbio->err = 0;
  1701. } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
  1702. spage->physical ||
  1703. sbio->logical + sbio->page_count * PAGE_SIZE !=
  1704. spage->logical ||
  1705. sbio->dev != spage->dev) {
  1706. scrub_submit(sctx);
  1707. goto again;
  1708. }
  1709. sbio->pagev[sbio->page_count] = spage;
  1710. ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
  1711. if (ret != PAGE_SIZE) {
  1712. if (sbio->page_count < 1) {
  1713. bio_put(sbio->bio);
  1714. sbio->bio = NULL;
  1715. return -EIO;
  1716. }
  1717. scrub_submit(sctx);
  1718. goto again;
  1719. }
  1720. scrub_block_get(sblock); /* one for the page added to the bio */
  1721. atomic_inc(&sblock->outstanding_pages);
  1722. sbio->page_count++;
  1723. if (sbio->page_count == sctx->pages_per_rd_bio)
  1724. scrub_submit(sctx);
  1725. return 0;
  1726. }
  1727. static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
  1728. u64 physical, struct btrfs_device *dev, u64 flags,
  1729. u64 gen, int mirror_num, u8 *csum, int force,
  1730. u64 physical_for_dev_replace)
  1731. {
  1732. struct scrub_block *sblock;
  1733. int index;
  1734. sblock = kzalloc(sizeof(*sblock), GFP_NOFS);
  1735. if (!sblock) {
  1736. spin_lock(&sctx->stat_lock);
  1737. sctx->stat.malloc_errors++;
  1738. spin_unlock(&sctx->stat_lock);
  1739. return -ENOMEM;
  1740. }
  1741. /* one ref inside this function, plus one for each page added to
  1742. * a bio later on */
  1743. atomic_set(&sblock->ref_count, 1);
  1744. sblock->sctx = sctx;
  1745. sblock->no_io_error_seen = 1;
  1746. for (index = 0; len > 0; index++) {
  1747. struct scrub_page *spage;
  1748. u64 l = min_t(u64, len, PAGE_SIZE);
  1749. spage = kzalloc(sizeof(*spage), GFP_NOFS);
  1750. if (!spage) {
  1751. leave_nomem:
  1752. spin_lock(&sctx->stat_lock);
  1753. sctx->stat.malloc_errors++;
  1754. spin_unlock(&sctx->stat_lock);
  1755. scrub_block_put(sblock);
  1756. return -ENOMEM;
  1757. }
  1758. BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
  1759. scrub_page_get(spage);
  1760. sblock->pagev[index] = spage;
  1761. spage->sblock = sblock;
  1762. spage->dev = dev;
  1763. spage->flags = flags;
  1764. spage->generation = gen;
  1765. spage->logical = logical;
  1766. spage->physical = physical;
  1767. spage->physical_for_dev_replace = physical_for_dev_replace;
  1768. spage->mirror_num = mirror_num;
  1769. if (csum) {
  1770. spage->have_csum = 1;
  1771. memcpy(spage->csum, csum, sctx->csum_size);
  1772. } else {
  1773. spage->have_csum = 0;
  1774. }
  1775. sblock->page_count++;
  1776. spage->page = alloc_page(GFP_NOFS);
  1777. if (!spage->page)
  1778. goto leave_nomem;
  1779. len -= l;
  1780. logical += l;
  1781. physical += l;
  1782. physical_for_dev_replace += l;
  1783. }
  1784. WARN_ON(sblock->page_count == 0);
  1785. for (index = 0; index < sblock->page_count; index++) {
  1786. struct scrub_page *spage = sblock->pagev[index];
  1787. int ret;
  1788. ret = scrub_add_page_to_rd_bio(sctx, spage);
  1789. if (ret) {
  1790. scrub_block_put(sblock);
  1791. return ret;
  1792. }
  1793. }
  1794. if (force)
  1795. scrub_submit(sctx);
  1796. /* last one frees, either here or in bio completion for last page */
  1797. scrub_block_put(sblock);
  1798. return 0;
  1799. }
  1800. static void scrub_bio_end_io(struct bio *bio, int err)
  1801. {
  1802. struct scrub_bio *sbio = bio->bi_private;
  1803. struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
  1804. sbio->err = err;
  1805. sbio->bio = bio;
  1806. btrfs_queue_worker(&fs_info->scrub_workers, &sbio->work);
  1807. }
  1808. static void scrub_bio_end_io_worker(struct btrfs_work *work)
  1809. {
  1810. struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
  1811. struct scrub_ctx *sctx = sbio->sctx;
  1812. int i;
  1813. BUG_ON(sbio->page_count > SCRUB_PAGES_PER_RD_BIO);
  1814. if (sbio->err) {
  1815. for (i = 0; i < sbio->page_count; i++) {
  1816. struct scrub_page *spage = sbio->pagev[i];
  1817. spage->io_error = 1;
  1818. spage->sblock->no_io_error_seen = 0;
  1819. }
  1820. }
  1821. /* now complete the scrub_block items that have all pages completed */
  1822. for (i = 0; i < sbio->page_count; i++) {
  1823. struct scrub_page *spage = sbio->pagev[i];
  1824. struct scrub_block *sblock = spage->sblock;
  1825. if (atomic_dec_and_test(&sblock->outstanding_pages))
  1826. scrub_block_complete(sblock);
  1827. scrub_block_put(sblock);
  1828. }
  1829. bio_put(sbio->bio);
  1830. sbio->bio = NULL;
  1831. spin_lock(&sctx->list_lock);
  1832. sbio->next_free = sctx->first_free;
  1833. sctx->first_free = sbio->index;
  1834. spin_unlock(&sctx->list_lock);
  1835. if (sctx->is_dev_replace &&
  1836. atomic_read(&sctx->wr_ctx.flush_all_writes)) {
  1837. mutex_lock(&sctx->wr_ctx.wr_lock);
  1838. scrub_wr_submit(sctx);
  1839. mutex_unlock(&sctx->wr_ctx.wr_lock);
  1840. }
  1841. scrub_pending_bio_dec(sctx);
  1842. }
  1843. static void scrub_block_complete(struct scrub_block *sblock)
  1844. {
  1845. if (!sblock->no_io_error_seen) {
  1846. scrub_handle_errored_block(sblock);
  1847. } else {
  1848. /*
  1849. * if has checksum error, write via repair mechanism in
  1850. * dev replace case, otherwise write here in dev replace
  1851. * case.
  1852. */
  1853. if (!scrub_checksum(sblock) && sblock->sctx->is_dev_replace)
  1854. scrub_write_block_to_dev_replace(sblock);
  1855. }
  1856. }
  1857. static int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u64 len,
  1858. u8 *csum)
  1859. {
  1860. struct btrfs_ordered_sum *sum = NULL;
  1861. int ret = 0;
  1862. unsigned long i;
  1863. unsigned long num_sectors;
  1864. while (!list_empty(&sctx->csum_list)) {
  1865. sum = list_first_entry(&sctx->csum_list,
  1866. struct btrfs_ordered_sum, list);
  1867. if (sum->bytenr > logical)
  1868. return 0;
  1869. if (sum->bytenr + sum->len > logical)
  1870. break;
  1871. ++sctx->stat.csum_discards;
  1872. list_del(&sum->list);
  1873. kfree(sum);
  1874. sum = NULL;
  1875. }
  1876. if (!sum)
  1877. return 0;
  1878. num_sectors = sum->len / sctx->sectorsize;
  1879. for (i = 0; i < num_sectors; ++i) {
  1880. if (sum->sums[i].bytenr == logical) {
  1881. memcpy(csum, &sum->sums[i].sum, sctx->csum_size);
  1882. ret = 1;
  1883. break;
  1884. }
  1885. }
  1886. if (ret && i == num_sectors - 1) {
  1887. list_del(&sum->list);
  1888. kfree(sum);
  1889. }
  1890. return ret;
  1891. }
  1892. /* scrub extent tries to collect up to 64 kB for each bio */
  1893. static int scrub_extent(struct scrub_ctx *sctx, u64 logical, u64 len,
  1894. u64 physical, struct btrfs_device *dev, u64 flags,
  1895. u64 gen, int mirror_num, u64 physical_for_dev_replace)
  1896. {
  1897. int ret;
  1898. u8 csum[BTRFS_CSUM_SIZE];
  1899. u32 blocksize;
  1900. if (flags & BTRFS_EXTENT_FLAG_DATA) {
  1901. blocksize = sctx->sectorsize;
  1902. spin_lock(&sctx->stat_lock);
  1903. sctx->stat.data_extents_scrubbed++;
  1904. sctx->stat.data_bytes_scrubbed += len;
  1905. spin_unlock(&sctx->stat_lock);
  1906. } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  1907. WARN_ON(sctx->nodesize != sctx->leafsize);
  1908. blocksize = sctx->nodesize;
  1909. spin_lock(&sctx->stat_lock);
  1910. sctx->stat.tree_extents_scrubbed++;
  1911. sctx->stat.tree_bytes_scrubbed += len;
  1912. spin_unlock(&sctx->stat_lock);
  1913. } else {
  1914. blocksize = sctx->sectorsize;
  1915. WARN_ON(1);
  1916. }
  1917. while (len) {
  1918. u64 l = min_t(u64, len, blocksize);
  1919. int have_csum = 0;
  1920. if (flags & BTRFS_EXTENT_FLAG_DATA) {
  1921. /* push csums to sbio */
  1922. have_csum = scrub_find_csum(sctx, logical, l, csum);
  1923. if (have_csum == 0)
  1924. ++sctx->stat.no_csum;
  1925. if (sctx->is_dev_replace && !have_csum) {
  1926. ret = copy_nocow_pages(sctx, logical, l,
  1927. mirror_num,
  1928. physical_for_dev_replace);
  1929. goto behind_scrub_pages;
  1930. }
  1931. }
  1932. ret = scrub_pages(sctx, logical, l, physical, dev, flags, gen,
  1933. mirror_num, have_csum ? csum : NULL, 0,
  1934. physical_for_dev_replace);
  1935. behind_scrub_pages:
  1936. if (ret)
  1937. return ret;
  1938. len -= l;
  1939. logical += l;
  1940. physical += l;
  1941. physical_for_dev_replace += l;
  1942. }
  1943. return 0;
  1944. }
  1945. static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
  1946. struct map_lookup *map,
  1947. struct btrfs_device *scrub_dev,
  1948. int num, u64 base, u64 length,
  1949. int is_dev_replace)
  1950. {
  1951. struct btrfs_path *path;
  1952. struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
  1953. struct btrfs_root *root = fs_info->extent_root;
  1954. struct btrfs_root *csum_root = fs_info->csum_root;
  1955. struct btrfs_extent_item *extent;
  1956. struct blk_plug plug;
  1957. u64 flags;
  1958. int ret;
  1959. int slot;
  1960. int i;
  1961. u64 nstripes;
  1962. struct extent_buffer *l;
  1963. struct btrfs_key key;
  1964. u64 physical;
  1965. u64 logical;
  1966. u64 generation;
  1967. int mirror_num;
  1968. struct reada_control *reada1;
  1969. struct reada_control *reada2;
  1970. struct btrfs_key key_start;
  1971. struct btrfs_key key_end;
  1972. u64 increment = map->stripe_len;
  1973. u64 offset;
  1974. u64 extent_logical;
  1975. u64 extent_physical;
  1976. u64 extent_len;
  1977. struct btrfs_device *extent_dev;
  1978. int extent_mirror_num;
  1979. nstripes = length;
  1980. offset = 0;
  1981. do_div(nstripes, map->stripe_len);
  1982. if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
  1983. offset = map->stripe_len * num;
  1984. increment = map->stripe_len * map->num_stripes;
  1985. mirror_num = 1;
  1986. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  1987. int factor = map->num_stripes / map->sub_stripes;
  1988. offset = map->stripe_len * (num / map->sub_stripes);
  1989. increment = map->stripe_len * factor;
  1990. mirror_num = num % map->sub_stripes + 1;
  1991. } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
  1992. increment = map->stripe_len;
  1993. mirror_num = num % map->num_stripes + 1;
  1994. } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
  1995. increment = map->stripe_len;
  1996. mirror_num = num % map->num_stripes + 1;
  1997. } else {
  1998. increment = map->stripe_len;
  1999. mirror_num = 1;
  2000. }
  2001. path = btrfs_alloc_path();
  2002. if (!path)
  2003. return -ENOMEM;
  2004. /*
  2005. * work on commit root. The related disk blocks are static as
  2006. * long as COW is applied. This means, it is save to rewrite
  2007. * them to repair disk errors without any race conditions
  2008. */
  2009. path->search_commit_root = 1;
  2010. path->skip_locking = 1;
  2011. /*
  2012. * trigger the readahead for extent tree csum tree and wait for
  2013. * completion. During readahead, the scrub is officially paused
  2014. * to not hold off transaction commits
  2015. */
  2016. logical = base + offset;
  2017. wait_event(sctx->list_wait,
  2018. atomic_read(&sctx->bios_in_flight) == 0);
  2019. atomic_inc(&fs_info->scrubs_paused);
  2020. wake_up(&fs_info->scrub_pause_wait);
  2021. /* FIXME it might be better to start readahead at commit root */
  2022. key_start.objectid = logical;
  2023. key_start.type = BTRFS_EXTENT_ITEM_KEY;
  2024. key_start.offset = (u64)0;
  2025. key_end.objectid = base + offset + nstripes * increment;
  2026. key_end.type = BTRFS_EXTENT_ITEM_KEY;
  2027. key_end.offset = (u64)0;
  2028. reada1 = btrfs_reada_add(root, &key_start, &key_end);
  2029. key_start.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
  2030. key_start.type = BTRFS_EXTENT_CSUM_KEY;
  2031. key_start.offset = logical;
  2032. key_end.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
  2033. key_end.type = BTRFS_EXTENT_CSUM_KEY;
  2034. key_end.offset = base + offset + nstripes * increment;
  2035. reada2 = btrfs_reada_add(csum_root, &key_start, &key_end);
  2036. if (!IS_ERR(reada1))
  2037. btrfs_reada_wait(reada1);
  2038. if (!IS_ERR(reada2))
  2039. btrfs_reada_wait(reada2);
  2040. mutex_lock(&fs_info->scrub_lock);
  2041. while (atomic_read(&fs_info->scrub_pause_req)) {
  2042. mutex_unlock(&fs_info->scrub_lock);
  2043. wait_event(fs_info->scrub_pause_wait,
  2044. atomic_read(&fs_info->scrub_pause_req) == 0);
  2045. mutex_lock(&fs_info->scrub_lock);
  2046. }
  2047. atomic_dec(&fs_info->scrubs_paused);
  2048. mutex_unlock(&fs_info->scrub_lock);
  2049. wake_up(&fs_info->scrub_pause_wait);
  2050. /*
  2051. * collect all data csums for the stripe to avoid seeking during
  2052. * the scrub. This might currently (crc32) end up to be about 1MB
  2053. */
  2054. blk_start_plug(&plug);
  2055. /*
  2056. * now find all extents for each stripe and scrub them
  2057. */
  2058. logical = base + offset;
  2059. physical = map->stripes[num].physical;
  2060. ret = 0;
  2061. for (i = 0; i < nstripes; ++i) {
  2062. /*
  2063. * canceled?
  2064. */
  2065. if (atomic_read(&fs_info->scrub_cancel_req) ||
  2066. atomic_read(&sctx->cancel_req)) {
  2067. ret = -ECANCELED;
  2068. goto out;
  2069. }
  2070. /*
  2071. * check to see if we have to pause
  2072. */
  2073. if (atomic_read(&fs_info->scrub_pause_req)) {
  2074. /* push queued extents */
  2075. atomic_set(&sctx->wr_ctx.flush_all_writes, 1);
  2076. scrub_submit(sctx);
  2077. mutex_lock(&sctx->wr_ctx.wr_lock);
  2078. scrub_wr_submit(sctx);
  2079. mutex_unlock(&sctx->wr_ctx.wr_lock);
  2080. wait_event(sctx->list_wait,
  2081. atomic_read(&sctx->bios_in_flight) == 0);
  2082. atomic_set(&sctx->wr_ctx.flush_all_writes, 0);
  2083. atomic_inc(&fs_info->scrubs_paused);
  2084. wake_up(&fs_info->scrub_pause_wait);
  2085. mutex_lock(&fs_info->scrub_lock);
  2086. while (atomic_read(&fs_info->scrub_pause_req)) {
  2087. mutex_unlock(&fs_info->scrub_lock);
  2088. wait_event(fs_info->scrub_pause_wait,
  2089. atomic_read(&fs_info->scrub_pause_req) == 0);
  2090. mutex_lock(&fs_info->scrub_lock);
  2091. }
  2092. atomic_dec(&fs_info->scrubs_paused);
  2093. mutex_unlock(&fs_info->scrub_lock);
  2094. wake_up(&fs_info->scrub_pause_wait);
  2095. }
  2096. ret = btrfs_lookup_csums_range(csum_root, logical,
  2097. logical + map->stripe_len - 1,
  2098. &sctx->csum_list, 1);
  2099. if (ret)
  2100. goto out;
  2101. key.objectid = logical;
  2102. key.type = BTRFS_EXTENT_ITEM_KEY;
  2103. key.offset = (u64)0;
  2104. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2105. if (ret < 0)
  2106. goto out;
  2107. if (ret > 0) {
  2108. ret = btrfs_previous_item(root, path, 0,
  2109. BTRFS_EXTENT_ITEM_KEY);
  2110. if (ret < 0)
  2111. goto out;
  2112. if (ret > 0) {
  2113. /* there's no smaller item, so stick with the
  2114. * larger one */
  2115. btrfs_release_path(path);
  2116. ret = btrfs_search_slot(NULL, root, &key,
  2117. path, 0, 0);
  2118. if (ret < 0)
  2119. goto out;
  2120. }
  2121. }
  2122. while (1) {
  2123. l = path->nodes[0];
  2124. slot = path->slots[0];
  2125. if (slot >= btrfs_header_nritems(l)) {
  2126. ret = btrfs_next_leaf(root, path);
  2127. if (ret == 0)
  2128. continue;
  2129. if (ret < 0)
  2130. goto out;
  2131. break;
  2132. }
  2133. btrfs_item_key_to_cpu(l, &key, slot);
  2134. if (key.objectid + key.offset <= logical)
  2135. goto next;
  2136. if (key.objectid >= logical + map->stripe_len)
  2137. break;
  2138. if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
  2139. goto next;
  2140. extent = btrfs_item_ptr(l, slot,
  2141. struct btrfs_extent_item);
  2142. flags = btrfs_extent_flags(l, extent);
  2143. generation = btrfs_extent_generation(l, extent);
  2144. if (key.objectid < logical &&
  2145. (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
  2146. printk(KERN_ERR
  2147. "btrfs scrub: tree block %llu spanning "
  2148. "stripes, ignored. logical=%llu\n",
  2149. (unsigned long long)key.objectid,
  2150. (unsigned long long)logical);
  2151. goto next;
  2152. }
  2153. /*
  2154. * trim extent to this stripe
  2155. */
  2156. if (key.objectid < logical) {
  2157. key.offset -= logical - key.objectid;
  2158. key.objectid = logical;
  2159. }
  2160. if (key.objectid + key.offset >
  2161. logical + map->stripe_len) {
  2162. key.offset = logical + map->stripe_len -
  2163. key.objectid;
  2164. }
  2165. extent_logical = key.objectid;
  2166. extent_physical = key.objectid - logical + physical;
  2167. extent_len = key.offset;
  2168. extent_dev = scrub_dev;
  2169. extent_mirror_num = mirror_num;
  2170. if (is_dev_replace)
  2171. scrub_remap_extent(fs_info, extent_logical,
  2172. extent_len, &extent_physical,
  2173. &extent_dev,
  2174. &extent_mirror_num);
  2175. ret = scrub_extent(sctx, extent_logical, extent_len,
  2176. extent_physical, extent_dev, flags,
  2177. generation, extent_mirror_num,
  2178. key.objectid - logical + physical);
  2179. if (ret)
  2180. goto out;
  2181. next:
  2182. path->slots[0]++;
  2183. }
  2184. btrfs_release_path(path);
  2185. logical += increment;
  2186. physical += map->stripe_len;
  2187. spin_lock(&sctx->stat_lock);
  2188. sctx->stat.last_physical = physical;
  2189. spin_unlock(&sctx->stat_lock);
  2190. }
  2191. out:
  2192. /* push queued extents */
  2193. scrub_submit(sctx);
  2194. mutex_lock(&sctx->wr_ctx.wr_lock);
  2195. scrub_wr_submit(sctx);
  2196. mutex_unlock(&sctx->wr_ctx.wr_lock);
  2197. blk_finish_plug(&plug);
  2198. btrfs_free_path(path);
  2199. return ret < 0 ? ret : 0;
  2200. }
  2201. static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
  2202. struct btrfs_device *scrub_dev,
  2203. u64 chunk_tree, u64 chunk_objectid,
  2204. u64 chunk_offset, u64 length,
  2205. u64 dev_offset, int is_dev_replace)
  2206. {
  2207. struct btrfs_mapping_tree *map_tree =
  2208. &sctx->dev_root->fs_info->mapping_tree;
  2209. struct map_lookup *map;
  2210. struct extent_map *em;
  2211. int i;
  2212. int ret = 0;
  2213. read_lock(&map_tree->map_tree.lock);
  2214. em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
  2215. read_unlock(&map_tree->map_tree.lock);
  2216. if (!em)
  2217. return -EINVAL;
  2218. map = (struct map_lookup *)em->bdev;
  2219. if (em->start != chunk_offset)
  2220. goto out;
  2221. if (em->len < length)
  2222. goto out;
  2223. for (i = 0; i < map->num_stripes; ++i) {
  2224. if (map->stripes[i].dev->bdev == scrub_dev->bdev &&
  2225. map->stripes[i].physical == dev_offset) {
  2226. ret = scrub_stripe(sctx, map, scrub_dev, i,
  2227. chunk_offset, length,
  2228. is_dev_replace);
  2229. if (ret)
  2230. goto out;
  2231. }
  2232. }
  2233. out:
  2234. free_extent_map(em);
  2235. return ret;
  2236. }
  2237. static noinline_for_stack
  2238. int scrub_enumerate_chunks(struct scrub_ctx *sctx,
  2239. struct btrfs_device *scrub_dev, u64 start, u64 end,
  2240. int is_dev_replace)
  2241. {
  2242. struct btrfs_dev_extent *dev_extent = NULL;
  2243. struct btrfs_path *path;
  2244. struct btrfs_root *root = sctx->dev_root;
  2245. struct btrfs_fs_info *fs_info = root->fs_info;
  2246. u64 length;
  2247. u64 chunk_tree;
  2248. u64 chunk_objectid;
  2249. u64 chunk_offset;
  2250. int ret;
  2251. int slot;
  2252. struct extent_buffer *l;
  2253. struct btrfs_key key;
  2254. struct btrfs_key found_key;
  2255. struct btrfs_block_group_cache *cache;
  2256. struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
  2257. path = btrfs_alloc_path();
  2258. if (!path)
  2259. return -ENOMEM;
  2260. path->reada = 2;
  2261. path->search_commit_root = 1;
  2262. path->skip_locking = 1;
  2263. key.objectid = scrub_dev->devid;
  2264. key.offset = 0ull;
  2265. key.type = BTRFS_DEV_EXTENT_KEY;
  2266. while (1) {
  2267. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2268. if (ret < 0)
  2269. break;
  2270. if (ret > 0) {
  2271. if (path->slots[0] >=
  2272. btrfs_header_nritems(path->nodes[0])) {
  2273. ret = btrfs_next_leaf(root, path);
  2274. if (ret)
  2275. break;
  2276. }
  2277. }
  2278. l = path->nodes[0];
  2279. slot = path->slots[0];
  2280. btrfs_item_key_to_cpu(l, &found_key, slot);
  2281. if (found_key.objectid != scrub_dev->devid)
  2282. break;
  2283. if (btrfs_key_type(&found_key) != BTRFS_DEV_EXTENT_KEY)
  2284. break;
  2285. if (found_key.offset >= end)
  2286. break;
  2287. if (found_key.offset < key.offset)
  2288. break;
  2289. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  2290. length = btrfs_dev_extent_length(l, dev_extent);
  2291. if (found_key.offset + length <= start) {
  2292. key.offset = found_key.offset + length;
  2293. btrfs_release_path(path);
  2294. continue;
  2295. }
  2296. chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
  2297. chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
  2298. chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
  2299. /*
  2300. * get a reference on the corresponding block group to prevent
  2301. * the chunk from going away while we scrub it
  2302. */
  2303. cache = btrfs_lookup_block_group(fs_info, chunk_offset);
  2304. if (!cache) {
  2305. ret = -ENOENT;
  2306. break;
  2307. }
  2308. dev_replace->cursor_right = found_key.offset + length;
  2309. dev_replace->cursor_left = found_key.offset;
  2310. dev_replace->item_needs_writeback = 1;
  2311. ret = scrub_chunk(sctx, scrub_dev, chunk_tree, chunk_objectid,
  2312. chunk_offset, length, found_key.offset,
  2313. is_dev_replace);
  2314. /*
  2315. * flush, submit all pending read and write bios, afterwards
  2316. * wait for them.
  2317. * Note that in the dev replace case, a read request causes
  2318. * write requests that are submitted in the read completion
  2319. * worker. Therefore in the current situation, it is required
  2320. * that all write requests are flushed, so that all read and
  2321. * write requests are really completed when bios_in_flight
  2322. * changes to 0.
  2323. */
  2324. atomic_set(&sctx->wr_ctx.flush_all_writes, 1);
  2325. scrub_submit(sctx);
  2326. mutex_lock(&sctx->wr_ctx.wr_lock);
  2327. scrub_wr_submit(sctx);
  2328. mutex_unlock(&sctx->wr_ctx.wr_lock);
  2329. wait_event(sctx->list_wait,
  2330. atomic_read(&sctx->bios_in_flight) == 0);
  2331. atomic_set(&sctx->wr_ctx.flush_all_writes, 0);
  2332. atomic_inc(&fs_info->scrubs_paused);
  2333. wake_up(&fs_info->scrub_pause_wait);
  2334. wait_event(sctx->list_wait,
  2335. atomic_read(&sctx->workers_pending) == 0);
  2336. mutex_lock(&fs_info->scrub_lock);
  2337. while (atomic_read(&fs_info->scrub_pause_req)) {
  2338. mutex_unlock(&fs_info->scrub_lock);
  2339. wait_event(fs_info->scrub_pause_wait,
  2340. atomic_read(&fs_info->scrub_pause_req) == 0);
  2341. mutex_lock(&fs_info->scrub_lock);
  2342. }
  2343. atomic_dec(&fs_info->scrubs_paused);
  2344. mutex_unlock(&fs_info->scrub_lock);
  2345. wake_up(&fs_info->scrub_pause_wait);
  2346. dev_replace->cursor_left = dev_replace->cursor_right;
  2347. dev_replace->item_needs_writeback = 1;
  2348. btrfs_put_block_group(cache);
  2349. if (ret)
  2350. break;
  2351. if (atomic64_read(&dev_replace->num_write_errors) > 0) {
  2352. ret = -EIO;
  2353. break;
  2354. }
  2355. if (sctx->stat.malloc_errors > 0) {
  2356. ret = -ENOMEM;
  2357. break;
  2358. }
  2359. key.offset = found_key.offset + length;
  2360. btrfs_release_path(path);
  2361. }
  2362. btrfs_free_path(path);
  2363. /*
  2364. * ret can still be 1 from search_slot or next_leaf,
  2365. * that's not an error
  2366. */
  2367. return ret < 0 ? ret : 0;
  2368. }
  2369. static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,
  2370. struct btrfs_device *scrub_dev)
  2371. {
  2372. int i;
  2373. u64 bytenr;
  2374. u64 gen;
  2375. int ret;
  2376. struct btrfs_root *root = sctx->dev_root;
  2377. if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR)
  2378. return -EIO;
  2379. gen = root->fs_info->last_trans_committed;
  2380. for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
  2381. bytenr = btrfs_sb_offset(i);
  2382. if (bytenr + BTRFS_SUPER_INFO_SIZE > scrub_dev->total_bytes)
  2383. break;
  2384. ret = scrub_pages(sctx, bytenr, BTRFS_SUPER_INFO_SIZE, bytenr,
  2385. scrub_dev, BTRFS_EXTENT_FLAG_SUPER, gen, i,
  2386. NULL, 1, bytenr);
  2387. if (ret)
  2388. return ret;
  2389. }
  2390. wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
  2391. return 0;
  2392. }
  2393. /*
  2394. * get a reference count on fs_info->scrub_workers. start worker if necessary
  2395. */
  2396. static noinline_for_stack int scrub_workers_get(struct btrfs_fs_info *fs_info,
  2397. int is_dev_replace)
  2398. {
  2399. int ret = 0;
  2400. mutex_lock(&fs_info->scrub_lock);
  2401. if (fs_info->scrub_workers_refcnt == 0) {
  2402. if (is_dev_replace)
  2403. btrfs_init_workers(&fs_info->scrub_workers, "scrub", 1,
  2404. &fs_info->generic_worker);
  2405. else
  2406. btrfs_init_workers(&fs_info->scrub_workers, "scrub",
  2407. fs_info->thread_pool_size,
  2408. &fs_info->generic_worker);
  2409. fs_info->scrub_workers.idle_thresh = 4;
  2410. ret = btrfs_start_workers(&fs_info->scrub_workers);
  2411. if (ret)
  2412. goto out;
  2413. btrfs_init_workers(&fs_info->scrub_wr_completion_workers,
  2414. "scrubwrc",
  2415. fs_info->thread_pool_size,
  2416. &fs_info->generic_worker);
  2417. fs_info->scrub_wr_completion_workers.idle_thresh = 2;
  2418. ret = btrfs_start_workers(
  2419. &fs_info->scrub_wr_completion_workers);
  2420. if (ret)
  2421. goto out;
  2422. btrfs_init_workers(&fs_info->scrub_nocow_workers, "scrubnc", 1,
  2423. &fs_info->generic_worker);
  2424. ret = btrfs_start_workers(&fs_info->scrub_nocow_workers);
  2425. if (ret)
  2426. goto out;
  2427. }
  2428. ++fs_info->scrub_workers_refcnt;
  2429. out:
  2430. mutex_unlock(&fs_info->scrub_lock);
  2431. return ret;
  2432. }
  2433. static noinline_for_stack void scrub_workers_put(struct btrfs_fs_info *fs_info)
  2434. {
  2435. mutex_lock(&fs_info->scrub_lock);
  2436. if (--fs_info->scrub_workers_refcnt == 0) {
  2437. btrfs_stop_workers(&fs_info->scrub_workers);
  2438. btrfs_stop_workers(&fs_info->scrub_wr_completion_workers);
  2439. btrfs_stop_workers(&fs_info->scrub_nocow_workers);
  2440. }
  2441. WARN_ON(fs_info->scrub_workers_refcnt < 0);
  2442. mutex_unlock(&fs_info->scrub_lock);
  2443. }
  2444. int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
  2445. u64 end, struct btrfs_scrub_progress *progress,
  2446. int readonly, int is_dev_replace)
  2447. {
  2448. struct scrub_ctx *sctx;
  2449. int ret;
  2450. struct btrfs_device *dev;
  2451. if (btrfs_fs_closing(fs_info))
  2452. return -EINVAL;
  2453. /*
  2454. * check some assumptions
  2455. */
  2456. if (fs_info->chunk_root->nodesize != fs_info->chunk_root->leafsize) {
  2457. printk(KERN_ERR
  2458. "btrfs_scrub: size assumption nodesize == leafsize (%d == %d) fails\n",
  2459. fs_info->chunk_root->nodesize,
  2460. fs_info->chunk_root->leafsize);
  2461. return -EINVAL;
  2462. }
  2463. if (fs_info->chunk_root->nodesize > BTRFS_STRIPE_LEN) {
  2464. /*
  2465. * in this case scrub is unable to calculate the checksum
  2466. * the way scrub is implemented. Do not handle this
  2467. * situation at all because it won't ever happen.
  2468. */
  2469. printk(KERN_ERR
  2470. "btrfs_scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails\n",
  2471. fs_info->chunk_root->nodesize, BTRFS_STRIPE_LEN);
  2472. return -EINVAL;
  2473. }
  2474. if (fs_info->chunk_root->sectorsize != PAGE_SIZE) {
  2475. /* not supported for data w/o checksums */
  2476. printk(KERN_ERR
  2477. "btrfs_scrub: size assumption sectorsize != PAGE_SIZE (%d != %lld) fails\n",
  2478. fs_info->chunk_root->sectorsize,
  2479. (unsigned long long)PAGE_SIZE);
  2480. return -EINVAL;
  2481. }
  2482. if (fs_info->chunk_root->nodesize >
  2483. PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK ||
  2484. fs_info->chunk_root->sectorsize >
  2485. PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK) {
  2486. /*
  2487. * would exhaust the array bounds of pagev member in
  2488. * struct scrub_block
  2489. */
  2490. pr_err("btrfs_scrub: size assumption nodesize and sectorsize <= SCRUB_MAX_PAGES_PER_BLOCK (%d <= %d && %d <= %d) fails\n",
  2491. fs_info->chunk_root->nodesize,
  2492. SCRUB_MAX_PAGES_PER_BLOCK,
  2493. fs_info->chunk_root->sectorsize,
  2494. SCRUB_MAX_PAGES_PER_BLOCK);
  2495. return -EINVAL;
  2496. }
  2497. ret = scrub_workers_get(fs_info, is_dev_replace);
  2498. if (ret)
  2499. return ret;
  2500. mutex_lock(&fs_info->fs_devices->device_list_mutex);
  2501. dev = btrfs_find_device(fs_info, devid, NULL, NULL);
  2502. if (!dev || (dev->missing && !is_dev_replace)) {
  2503. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  2504. scrub_workers_put(fs_info);
  2505. return -ENODEV;
  2506. }
  2507. mutex_lock(&fs_info->scrub_lock);
  2508. if (!dev->in_fs_metadata || dev->is_tgtdev_for_dev_replace) {
  2509. mutex_unlock(&fs_info->scrub_lock);
  2510. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  2511. scrub_workers_put(fs_info);
  2512. return -EIO;
  2513. }
  2514. btrfs_dev_replace_lock(&fs_info->dev_replace);
  2515. if (dev->scrub_device ||
  2516. (!is_dev_replace &&
  2517. btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))) {
  2518. btrfs_dev_replace_unlock(&fs_info->dev_replace);
  2519. mutex_unlock(&fs_info->scrub_lock);
  2520. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  2521. scrub_workers_put(fs_info);
  2522. return -EINPROGRESS;
  2523. }
  2524. btrfs_dev_replace_unlock(&fs_info->dev_replace);
  2525. sctx = scrub_setup_ctx(dev, is_dev_replace);
  2526. if (IS_ERR(sctx)) {
  2527. mutex_unlock(&fs_info->scrub_lock);
  2528. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  2529. scrub_workers_put(fs_info);
  2530. return PTR_ERR(sctx);
  2531. }
  2532. sctx->readonly = readonly;
  2533. dev->scrub_device = sctx;
  2534. atomic_inc(&fs_info->scrubs_running);
  2535. mutex_unlock(&fs_info->scrub_lock);
  2536. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  2537. if (!is_dev_replace) {
  2538. down_read(&fs_info->scrub_super_lock);
  2539. ret = scrub_supers(sctx, dev);
  2540. up_read(&fs_info->scrub_super_lock);
  2541. }
  2542. if (!ret)
  2543. ret = scrub_enumerate_chunks(sctx, dev, start, end,
  2544. is_dev_replace);
  2545. wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
  2546. atomic_dec(&fs_info->scrubs_running);
  2547. wake_up(&fs_info->scrub_pause_wait);
  2548. wait_event(sctx->list_wait, atomic_read(&sctx->workers_pending) == 0);
  2549. if (progress)
  2550. memcpy(progress, &sctx->stat, sizeof(*progress));
  2551. mutex_lock(&fs_info->scrub_lock);
  2552. dev->scrub_device = NULL;
  2553. mutex_unlock(&fs_info->scrub_lock);
  2554. scrub_free_ctx(sctx);
  2555. scrub_workers_put(fs_info);
  2556. return ret;
  2557. }
  2558. void btrfs_scrub_pause(struct btrfs_root *root)
  2559. {
  2560. struct btrfs_fs_info *fs_info = root->fs_info;
  2561. mutex_lock(&fs_info->scrub_lock);
  2562. atomic_inc(&fs_info->scrub_pause_req);
  2563. while (atomic_read(&fs_info->scrubs_paused) !=
  2564. atomic_read(&fs_info->scrubs_running)) {
  2565. mutex_unlock(&fs_info->scrub_lock);
  2566. wait_event(fs_info->scrub_pause_wait,
  2567. atomic_read(&fs_info->scrubs_paused) ==
  2568. atomic_read(&fs_info->scrubs_running));
  2569. mutex_lock(&fs_info->scrub_lock);
  2570. }
  2571. mutex_unlock(&fs_info->scrub_lock);
  2572. }
  2573. void btrfs_scrub_continue(struct btrfs_root *root)
  2574. {
  2575. struct btrfs_fs_info *fs_info = root->fs_info;
  2576. atomic_dec(&fs_info->scrub_pause_req);
  2577. wake_up(&fs_info->scrub_pause_wait);
  2578. }
  2579. void btrfs_scrub_pause_super(struct btrfs_root *root)
  2580. {
  2581. down_write(&root->fs_info->scrub_super_lock);
  2582. }
  2583. void btrfs_scrub_continue_super(struct btrfs_root *root)
  2584. {
  2585. up_write(&root->fs_info->scrub_super_lock);
  2586. }
  2587. int btrfs_scrub_cancel(struct btrfs_fs_info *fs_info)
  2588. {
  2589. mutex_lock(&fs_info->scrub_lock);
  2590. if (!atomic_read(&fs_info->scrubs_running)) {
  2591. mutex_unlock(&fs_info->scrub_lock);
  2592. return -ENOTCONN;
  2593. }
  2594. atomic_inc(&fs_info->scrub_cancel_req);
  2595. while (atomic_read(&fs_info->scrubs_running)) {
  2596. mutex_unlock(&fs_info->scrub_lock);
  2597. wait_event(fs_info->scrub_pause_wait,
  2598. atomic_read(&fs_info->scrubs_running) == 0);
  2599. mutex_lock(&fs_info->scrub_lock);
  2600. }
  2601. atomic_dec(&fs_info->scrub_cancel_req);
  2602. mutex_unlock(&fs_info->scrub_lock);
  2603. return 0;
  2604. }
  2605. int btrfs_scrub_cancel_dev(struct btrfs_fs_info *fs_info,
  2606. struct btrfs_device *dev)
  2607. {
  2608. struct scrub_ctx *sctx;
  2609. mutex_lock(&fs_info->scrub_lock);
  2610. sctx = dev->scrub_device;
  2611. if (!sctx) {
  2612. mutex_unlock(&fs_info->scrub_lock);
  2613. return -ENOTCONN;
  2614. }
  2615. atomic_inc(&sctx->cancel_req);
  2616. while (dev->scrub_device) {
  2617. mutex_unlock(&fs_info->scrub_lock);
  2618. wait_event(fs_info->scrub_pause_wait,
  2619. dev->scrub_device == NULL);
  2620. mutex_lock(&fs_info->scrub_lock);
  2621. }
  2622. mutex_unlock(&fs_info->scrub_lock);
  2623. return 0;
  2624. }
  2625. int btrfs_scrub_cancel_devid(struct btrfs_root *root, u64 devid)
  2626. {
  2627. struct btrfs_fs_info *fs_info = root->fs_info;
  2628. struct btrfs_device *dev;
  2629. int ret;
  2630. /*
  2631. * we have to hold the device_list_mutex here so the device
  2632. * does not go away in cancel_dev. FIXME: find a better solution
  2633. */
  2634. mutex_lock(&fs_info->fs_devices->device_list_mutex);
  2635. dev = btrfs_find_device(fs_info, devid, NULL, NULL);
  2636. if (!dev) {
  2637. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  2638. return -ENODEV;
  2639. }
  2640. ret = btrfs_scrub_cancel_dev(fs_info, dev);
  2641. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  2642. return ret;
  2643. }
  2644. int btrfs_scrub_progress(struct btrfs_root *root, u64 devid,
  2645. struct btrfs_scrub_progress *progress)
  2646. {
  2647. struct btrfs_device *dev;
  2648. struct scrub_ctx *sctx = NULL;
  2649. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  2650. dev = btrfs_find_device(root->fs_info, devid, NULL, NULL);
  2651. if (dev)
  2652. sctx = dev->scrub_device;
  2653. if (sctx)
  2654. memcpy(progress, &sctx->stat, sizeof(*progress));
  2655. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  2656. return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV;
  2657. }
  2658. static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
  2659. u64 extent_logical, u64 extent_len,
  2660. u64 *extent_physical,
  2661. struct btrfs_device **extent_dev,
  2662. int *extent_mirror_num)
  2663. {
  2664. u64 mapped_length;
  2665. struct btrfs_bio *bbio = NULL;
  2666. int ret;
  2667. mapped_length = extent_len;
  2668. ret = btrfs_map_block(fs_info, READ, extent_logical,
  2669. &mapped_length, &bbio, 0);
  2670. if (ret || !bbio || mapped_length < extent_len ||
  2671. !bbio->stripes[0].dev->bdev) {
  2672. kfree(bbio);
  2673. return;
  2674. }
  2675. *extent_physical = bbio->stripes[0].physical;
  2676. *extent_mirror_num = bbio->mirror_num;
  2677. *extent_dev = bbio->stripes[0].dev;
  2678. kfree(bbio);
  2679. }
  2680. static int scrub_setup_wr_ctx(struct scrub_ctx *sctx,
  2681. struct scrub_wr_ctx *wr_ctx,
  2682. struct btrfs_fs_info *fs_info,
  2683. struct btrfs_device *dev,
  2684. int is_dev_replace)
  2685. {
  2686. WARN_ON(wr_ctx->wr_curr_bio != NULL);
  2687. mutex_init(&wr_ctx->wr_lock);
  2688. wr_ctx->wr_curr_bio = NULL;
  2689. if (!is_dev_replace)
  2690. return 0;
  2691. WARN_ON(!dev->bdev);
  2692. wr_ctx->pages_per_wr_bio = min_t(int, SCRUB_PAGES_PER_WR_BIO,
  2693. bio_get_nr_vecs(dev->bdev));
  2694. wr_ctx->tgtdev = dev;
  2695. atomic_set(&wr_ctx->flush_all_writes, 0);
  2696. return 0;
  2697. }
  2698. static void scrub_free_wr_ctx(struct scrub_wr_ctx *wr_ctx)
  2699. {
  2700. mutex_lock(&wr_ctx->wr_lock);
  2701. kfree(wr_ctx->wr_curr_bio);
  2702. wr_ctx->wr_curr_bio = NULL;
  2703. mutex_unlock(&wr_ctx->wr_lock);
  2704. }
  2705. static int copy_nocow_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
  2706. int mirror_num, u64 physical_for_dev_replace)
  2707. {
  2708. struct scrub_copy_nocow_ctx *nocow_ctx;
  2709. struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
  2710. nocow_ctx = kzalloc(sizeof(*nocow_ctx), GFP_NOFS);
  2711. if (!nocow_ctx) {
  2712. spin_lock(&sctx->stat_lock);
  2713. sctx->stat.malloc_errors++;
  2714. spin_unlock(&sctx->stat_lock);
  2715. return -ENOMEM;
  2716. }
  2717. scrub_pending_trans_workers_inc(sctx);
  2718. nocow_ctx->sctx = sctx;
  2719. nocow_ctx->logical = logical;
  2720. nocow_ctx->len = len;
  2721. nocow_ctx->mirror_num = mirror_num;
  2722. nocow_ctx->physical_for_dev_replace = physical_for_dev_replace;
  2723. nocow_ctx->work.func = copy_nocow_pages_worker;
  2724. btrfs_queue_worker(&fs_info->scrub_nocow_workers,
  2725. &nocow_ctx->work);
  2726. return 0;
  2727. }
  2728. static void copy_nocow_pages_worker(struct btrfs_work *work)
  2729. {
  2730. struct scrub_copy_nocow_ctx *nocow_ctx =
  2731. container_of(work, struct scrub_copy_nocow_ctx, work);
  2732. struct scrub_ctx *sctx = nocow_ctx->sctx;
  2733. u64 logical = nocow_ctx->logical;
  2734. u64 len = nocow_ctx->len;
  2735. int mirror_num = nocow_ctx->mirror_num;
  2736. u64 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
  2737. int ret;
  2738. struct btrfs_trans_handle *trans = NULL;
  2739. struct btrfs_fs_info *fs_info;
  2740. struct btrfs_path *path;
  2741. struct btrfs_root *root;
  2742. int not_written = 0;
  2743. fs_info = sctx->dev_root->fs_info;
  2744. root = fs_info->extent_root;
  2745. path = btrfs_alloc_path();
  2746. if (!path) {
  2747. spin_lock(&sctx->stat_lock);
  2748. sctx->stat.malloc_errors++;
  2749. spin_unlock(&sctx->stat_lock);
  2750. not_written = 1;
  2751. goto out;
  2752. }
  2753. trans = btrfs_join_transaction(root);
  2754. if (IS_ERR(trans)) {
  2755. not_written = 1;
  2756. goto out;
  2757. }
  2758. ret = iterate_inodes_from_logical(logical, fs_info, path,
  2759. copy_nocow_pages_for_inode,
  2760. nocow_ctx);
  2761. if (ret != 0 && ret != -ENOENT) {
  2762. pr_warn("iterate_inodes_from_logical() failed: log %llu, phys %llu, len %llu, mir %llu, ret %d\n",
  2763. (unsigned long long)logical,
  2764. (unsigned long long)physical_for_dev_replace,
  2765. (unsigned long long)len,
  2766. (unsigned long long)mirror_num, ret);
  2767. not_written = 1;
  2768. goto out;
  2769. }
  2770. out:
  2771. if (trans && !IS_ERR(trans))
  2772. btrfs_end_transaction(trans, root);
  2773. if (not_written)
  2774. btrfs_dev_replace_stats_inc(&fs_info->dev_replace.
  2775. num_uncorrectable_read_errors);
  2776. btrfs_free_path(path);
  2777. kfree(nocow_ctx);
  2778. scrub_pending_trans_workers_dec(sctx);
  2779. }
  2780. static int copy_nocow_pages_for_inode(u64 inum, u64 offset, u64 root, void *ctx)
  2781. {
  2782. unsigned long index;
  2783. struct scrub_copy_nocow_ctx *nocow_ctx = ctx;
  2784. int ret = 0;
  2785. struct btrfs_key key;
  2786. struct inode *inode = NULL;
  2787. struct btrfs_root *local_root;
  2788. u64 physical_for_dev_replace;
  2789. u64 len;
  2790. struct btrfs_fs_info *fs_info = nocow_ctx->sctx->dev_root->fs_info;
  2791. key.objectid = root;
  2792. key.type = BTRFS_ROOT_ITEM_KEY;
  2793. key.offset = (u64)-1;
  2794. local_root = btrfs_read_fs_root_no_name(fs_info, &key);
  2795. if (IS_ERR(local_root))
  2796. return PTR_ERR(local_root);
  2797. key.type = BTRFS_INODE_ITEM_KEY;
  2798. key.objectid = inum;
  2799. key.offset = 0;
  2800. inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
  2801. if (IS_ERR(inode))
  2802. return PTR_ERR(inode);
  2803. physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
  2804. len = nocow_ctx->len;
  2805. while (len >= PAGE_CACHE_SIZE) {
  2806. struct page *page = NULL;
  2807. int ret_sub;
  2808. index = offset >> PAGE_CACHE_SHIFT;
  2809. page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
  2810. if (!page) {
  2811. pr_err("find_or_create_page() failed\n");
  2812. ret = -ENOMEM;
  2813. goto next_page;
  2814. }
  2815. if (PageUptodate(page)) {
  2816. if (PageDirty(page))
  2817. goto next_page;
  2818. } else {
  2819. ClearPageError(page);
  2820. ret_sub = extent_read_full_page(&BTRFS_I(inode)->
  2821. io_tree,
  2822. page, btrfs_get_extent,
  2823. nocow_ctx->mirror_num);
  2824. if (ret_sub) {
  2825. ret = ret_sub;
  2826. goto next_page;
  2827. }
  2828. wait_on_page_locked(page);
  2829. if (!PageUptodate(page)) {
  2830. ret = -EIO;
  2831. goto next_page;
  2832. }
  2833. }
  2834. ret_sub = write_page_nocow(nocow_ctx->sctx,
  2835. physical_for_dev_replace, page);
  2836. if (ret_sub) {
  2837. ret = ret_sub;
  2838. goto next_page;
  2839. }
  2840. next_page:
  2841. if (page) {
  2842. unlock_page(page);
  2843. put_page(page);
  2844. }
  2845. offset += PAGE_CACHE_SIZE;
  2846. physical_for_dev_replace += PAGE_CACHE_SIZE;
  2847. len -= PAGE_CACHE_SIZE;
  2848. }
  2849. if (inode)
  2850. iput(inode);
  2851. return ret;
  2852. }
  2853. static int write_page_nocow(struct scrub_ctx *sctx,
  2854. u64 physical_for_dev_replace, struct page *page)
  2855. {
  2856. struct bio *bio;
  2857. struct btrfs_device *dev;
  2858. int ret;
  2859. DECLARE_COMPLETION_ONSTACK(compl);
  2860. dev = sctx->wr_ctx.tgtdev;
  2861. if (!dev)
  2862. return -EIO;
  2863. if (!dev->bdev) {
  2864. printk_ratelimited(KERN_WARNING
  2865. "btrfs: scrub write_page_nocow(bdev == NULL) is unexpected!\n");
  2866. return -EIO;
  2867. }
  2868. bio = bio_alloc(GFP_NOFS, 1);
  2869. if (!bio) {
  2870. spin_lock(&sctx->stat_lock);
  2871. sctx->stat.malloc_errors++;
  2872. spin_unlock(&sctx->stat_lock);
  2873. return -ENOMEM;
  2874. }
  2875. bio->bi_private = &compl;
  2876. bio->bi_end_io = scrub_complete_bio_end_io;
  2877. bio->bi_size = 0;
  2878. bio->bi_sector = physical_for_dev_replace >> 9;
  2879. bio->bi_bdev = dev->bdev;
  2880. ret = bio_add_page(bio, page, PAGE_CACHE_SIZE, 0);
  2881. if (ret != PAGE_CACHE_SIZE) {
  2882. leave_with_eio:
  2883. bio_put(bio);
  2884. btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
  2885. return -EIO;
  2886. }
  2887. btrfsic_submit_bio(WRITE_SYNC, bio);
  2888. wait_for_completion(&compl);
  2889. if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
  2890. goto leave_with_eio;
  2891. bio_put(bio);
  2892. return 0;
  2893. }