scrub.c 91 KB

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