scrub.c 89 KB

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