scrub.c 90 KB

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