scrub.c 89 KB

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