scrub.c 98 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2011, 2012 STRATO. All rights reserved.
  4. */
  5. #include <linux/blkdev.h>
  6. #include <linux/ratelimit.h>
  7. #include <linux/sched/mm.h>
  8. #include "ctree.h"
  9. #include "discard.h"
  10. #include "volumes.h"
  11. #include "disk-io.h"
  12. #include "ordered-data.h"
  13. #include "transaction.h"
  14. #include "backref.h"
  15. #include "extent_io.h"
  16. #include "dev-replace.h"
  17. #include "raid56.h"
  18. #include "block-group.h"
  19. #include "zoned.h"
  20. #include "fs.h"
  21. #include "accessors.h"
  22. #include "file-item.h"
  23. #include "scrub.h"
  24. #include "raid-stripe-tree.h"
  25. /*
  26. * This is only the first step towards a full-features scrub. It reads all
  27. * extent and super block and verifies the checksums. In case a bad checksum
  28. * is found or the extent cannot be read, good data will be written back if
  29. * any can be found.
  30. *
  31. * Future enhancements:
  32. * - In case an unrepairable extent is encountered, track which files are
  33. * affected and report them
  34. * - track and record media errors, throw out bad devices
  35. * - add a mode to also read unallocated space
  36. */
  37. struct scrub_ctx;
  38. /*
  39. * The following value only influences the performance.
  40. *
  41. * This determines how many stripes would be submitted in one go,
  42. * which is 512KiB (BTRFS_STRIPE_LEN * SCRUB_STRIPES_PER_GROUP).
  43. */
  44. #define SCRUB_STRIPES_PER_GROUP 8
  45. /*
  46. * How many groups we have for each sctx.
  47. *
  48. * This would be 8M per device, the same value as the old scrub in-flight bios
  49. * size limit.
  50. */
  51. #define SCRUB_GROUPS_PER_SCTX 16
  52. #define SCRUB_TOTAL_STRIPES (SCRUB_GROUPS_PER_SCTX * SCRUB_STRIPES_PER_GROUP)
  53. /*
  54. * The following value times PAGE_SIZE needs to be large enough to match the
  55. * largest node/leaf/sector size that shall be supported.
  56. */
  57. #define SCRUB_MAX_SECTORS_PER_BLOCK (BTRFS_MAX_METADATA_BLOCKSIZE / SZ_4K)
  58. /* Represent one sector and its needed info to verify the content. */
  59. struct scrub_sector_verification {
  60. union {
  61. /*
  62. * Csum pointer for data csum verification. Should point to a
  63. * sector csum inside scrub_stripe::csums.
  64. *
  65. * NULL if this data sector has no csum.
  66. */
  67. u8 *csum;
  68. /*
  69. * Extra info for metadata verification. All sectors inside a
  70. * tree block share the same generation.
  71. */
  72. u64 generation;
  73. };
  74. };
  75. enum scrub_stripe_flags {
  76. /* Set when @mirror_num, @dev, @physical and @logical are set. */
  77. SCRUB_STRIPE_FLAG_INITIALIZED,
  78. /* Set when the read-repair is finished. */
  79. SCRUB_STRIPE_FLAG_REPAIR_DONE,
  80. /*
  81. * Set for data stripes if it's triggered from P/Q stripe.
  82. * During such scrub, we should not report errors in data stripes, nor
  83. * update the accounting.
  84. */
  85. SCRUB_STRIPE_FLAG_NO_REPORT,
  86. };
  87. /*
  88. * We have multiple bitmaps for one scrub_stripe.
  89. * However each bitmap has at most (BTRFS_STRIPE_LEN / blocksize) bits,
  90. * which is normally 16, and much smaller than BITS_PER_LONG (32 or 64).
  91. *
  92. * So to reduce memory usage for each scrub_stripe, we pack those bitmaps
  93. * into a larger one.
  94. *
  95. * These enum records where the sub-bitmap are inside the larger one.
  96. * Each subbitmap starts at scrub_bitmap_nr_##name * nr_sectors bit.
  97. */
  98. enum {
  99. /* Which blocks are covered by extent items. */
  100. scrub_bitmap_nr_has_extent = 0,
  101. /* Which blocks are metadata. */
  102. scrub_bitmap_nr_is_metadata,
  103. /*
  104. * Which blocks have errors, including IO, csum, and metadata
  105. * errors.
  106. * This sub-bitmap is the OR results of the next few error related
  107. * sub-bitmaps.
  108. */
  109. scrub_bitmap_nr_error,
  110. scrub_bitmap_nr_io_error,
  111. scrub_bitmap_nr_csum_error,
  112. scrub_bitmap_nr_meta_error,
  113. scrub_bitmap_nr_meta_gen_error,
  114. scrub_bitmap_nr_last,
  115. };
  116. #define SCRUB_STRIPE_MAX_FOLIOS (BTRFS_STRIPE_LEN / PAGE_SIZE)
  117. /*
  118. * Represent one contiguous range with a length of BTRFS_STRIPE_LEN.
  119. */
  120. struct scrub_stripe {
  121. struct scrub_ctx *sctx;
  122. struct btrfs_block_group *bg;
  123. struct folio *folios[SCRUB_STRIPE_MAX_FOLIOS];
  124. struct scrub_sector_verification *sectors;
  125. struct btrfs_device *dev;
  126. u64 logical;
  127. u64 physical;
  128. u16 mirror_num;
  129. /* Should be BTRFS_STRIPE_LEN / sectorsize. */
  130. u16 nr_sectors;
  131. /*
  132. * How many data/meta extents are in this stripe. Only for scrub status
  133. * reporting purposes.
  134. */
  135. u16 nr_data_extents;
  136. u16 nr_meta_extents;
  137. atomic_t pending_io;
  138. wait_queue_head_t io_wait;
  139. wait_queue_head_t repair_wait;
  140. /*
  141. * Indicate the states of the stripe. Bits are defined in
  142. * scrub_stripe_flags enum.
  143. */
  144. unsigned long state;
  145. /* The large bitmap contains all the sub-bitmaps. */
  146. unsigned long bitmaps[BITS_TO_LONGS(scrub_bitmap_nr_last *
  147. (BTRFS_STRIPE_LEN / BTRFS_MIN_BLOCKSIZE))];
  148. /*
  149. * For writeback (repair or replace) error reporting.
  150. * This one is protected by a spinlock, thus can not be packed into
  151. * the larger bitmap.
  152. */
  153. unsigned long write_error_bitmap;
  154. /* Writeback can be concurrent, thus we need to protect the bitmap. */
  155. spinlock_t write_error_lock;
  156. /*
  157. * Checksum for the whole stripe if this stripe is inside a data block
  158. * group.
  159. */
  160. u8 *csums;
  161. struct work_struct work;
  162. };
  163. struct scrub_ctx {
  164. struct scrub_stripe stripes[SCRUB_TOTAL_STRIPES];
  165. struct scrub_stripe *raid56_data_stripes;
  166. struct btrfs_fs_info *fs_info;
  167. struct btrfs_path extent_path;
  168. struct btrfs_path csum_path;
  169. int first_free;
  170. int cur_stripe;
  171. atomic_t cancel_req;
  172. int readonly;
  173. /* State of IO submission throttling affecting the associated device */
  174. ktime_t throttle_deadline;
  175. u64 throttle_sent;
  176. bool is_dev_replace;
  177. u64 write_pointer;
  178. struct mutex wr_lock;
  179. struct btrfs_device *wr_tgtdev;
  180. /*
  181. * statistics
  182. */
  183. struct btrfs_scrub_progress stat;
  184. spinlock_t stat_lock;
  185. /*
  186. * Use a ref counter to avoid use-after-free issues. Scrub workers
  187. * decrement bios_in_flight and workers_pending and then do a wakeup
  188. * on the list_wait wait queue. We must ensure the main scrub task
  189. * doesn't free the scrub context before or while the workers are
  190. * doing the wakeup() call.
  191. */
  192. refcount_t refs;
  193. };
  194. #define scrub_calc_start_bit(stripe, name, block_nr) \
  195. ({ \
  196. unsigned int __start_bit; \
  197. \
  198. ASSERT(block_nr < stripe->nr_sectors, \
  199. "nr_sectors=%u block_nr=%u", stripe->nr_sectors, block_nr); \
  200. __start_bit = scrub_bitmap_nr_##name * stripe->nr_sectors + block_nr; \
  201. __start_bit; \
  202. })
  203. #define IMPLEMENT_SCRUB_BITMAP_OPS(name) \
  204. static inline void scrub_bitmap_set_##name(struct scrub_stripe *stripe, \
  205. unsigned int block_nr, \
  206. unsigned int nr_blocks) \
  207. { \
  208. const unsigned int start_bit = scrub_calc_start_bit(stripe, \
  209. name, block_nr); \
  210. \
  211. bitmap_set(stripe->bitmaps, start_bit, nr_blocks); \
  212. } \
  213. static inline void scrub_bitmap_clear_##name(struct scrub_stripe *stripe, \
  214. unsigned int block_nr, \
  215. unsigned int nr_blocks) \
  216. { \
  217. const unsigned int start_bit = scrub_calc_start_bit(stripe, name, \
  218. block_nr); \
  219. \
  220. bitmap_clear(stripe->bitmaps, start_bit, nr_blocks); \
  221. } \
  222. static inline bool scrub_bitmap_test_bit_##name(struct scrub_stripe *stripe, \
  223. unsigned int block_nr) \
  224. { \
  225. const unsigned int start_bit = scrub_calc_start_bit(stripe, name, \
  226. block_nr); \
  227. \
  228. return test_bit(start_bit, stripe->bitmaps); \
  229. } \
  230. static inline void scrub_bitmap_set_bit_##name(struct scrub_stripe *stripe, \
  231. unsigned int block_nr) \
  232. { \
  233. const unsigned int start_bit = scrub_calc_start_bit(stripe, name, \
  234. block_nr); \
  235. \
  236. set_bit(start_bit, stripe->bitmaps); \
  237. } \
  238. static inline void scrub_bitmap_clear_bit_##name(struct scrub_stripe *stripe, \
  239. unsigned int block_nr) \
  240. { \
  241. const unsigned int start_bit = scrub_calc_start_bit(stripe, name, \
  242. block_nr); \
  243. \
  244. clear_bit(start_bit, stripe->bitmaps); \
  245. } \
  246. static inline unsigned long scrub_bitmap_read_##name(struct scrub_stripe *stripe) \
  247. { \
  248. const unsigned int nr_blocks = stripe->nr_sectors; \
  249. \
  250. ASSERT(nr_blocks > 0 && nr_blocks <= BITS_PER_LONG, \
  251. "nr_blocks=%u BITS_PER_LONG=%u", \
  252. nr_blocks, BITS_PER_LONG); \
  253. \
  254. return bitmap_read(stripe->bitmaps, nr_blocks * scrub_bitmap_nr_##name, \
  255. stripe->nr_sectors); \
  256. } \
  257. static inline bool scrub_bitmap_empty_##name(struct scrub_stripe *stripe) \
  258. { \
  259. unsigned long bitmap = scrub_bitmap_read_##name(stripe); \
  260. \
  261. return bitmap_empty(&bitmap, stripe->nr_sectors); \
  262. } \
  263. static inline unsigned int scrub_bitmap_weight_##name(struct scrub_stripe *stripe) \
  264. { \
  265. unsigned long bitmap = scrub_bitmap_read_##name(stripe); \
  266. \
  267. return bitmap_weight(&bitmap, stripe->nr_sectors); \
  268. }
  269. IMPLEMENT_SCRUB_BITMAP_OPS(has_extent);
  270. IMPLEMENT_SCRUB_BITMAP_OPS(is_metadata);
  271. IMPLEMENT_SCRUB_BITMAP_OPS(error);
  272. IMPLEMENT_SCRUB_BITMAP_OPS(io_error);
  273. IMPLEMENT_SCRUB_BITMAP_OPS(csum_error);
  274. IMPLEMENT_SCRUB_BITMAP_OPS(meta_error);
  275. IMPLEMENT_SCRUB_BITMAP_OPS(meta_gen_error);
  276. struct scrub_warning {
  277. struct btrfs_path *path;
  278. u64 extent_item_size;
  279. const char *errstr;
  280. u64 physical;
  281. u64 logical;
  282. struct btrfs_device *dev;
  283. };
  284. struct scrub_error_records {
  285. /*
  286. * Bitmap recording which blocks hit errors (IO/csum/...) during the
  287. * initial read.
  288. */
  289. unsigned long init_error_bitmap;
  290. unsigned int nr_io_errors;
  291. unsigned int nr_csum_errors;
  292. unsigned int nr_meta_errors;
  293. unsigned int nr_meta_gen_errors;
  294. };
  295. static void release_scrub_stripe(struct scrub_stripe *stripe)
  296. {
  297. if (!stripe)
  298. return;
  299. for (int i = 0; i < SCRUB_STRIPE_MAX_FOLIOS; i++) {
  300. if (stripe->folios[i])
  301. folio_put(stripe->folios[i]);
  302. stripe->folios[i] = NULL;
  303. }
  304. kfree(stripe->sectors);
  305. kfree(stripe->csums);
  306. stripe->sectors = NULL;
  307. stripe->csums = NULL;
  308. stripe->sctx = NULL;
  309. stripe->state = 0;
  310. }
  311. static int init_scrub_stripe(struct btrfs_fs_info *fs_info,
  312. struct scrub_stripe *stripe)
  313. {
  314. const u32 min_folio_shift = PAGE_SHIFT + fs_info->block_min_order;
  315. int ret;
  316. memset(stripe, 0, sizeof(*stripe));
  317. stripe->nr_sectors = BTRFS_STRIPE_LEN >> fs_info->sectorsize_bits;
  318. stripe->state = 0;
  319. init_waitqueue_head(&stripe->io_wait);
  320. init_waitqueue_head(&stripe->repair_wait);
  321. atomic_set(&stripe->pending_io, 0);
  322. spin_lock_init(&stripe->write_error_lock);
  323. ASSERT(BTRFS_STRIPE_LEN >> min_folio_shift <= SCRUB_STRIPE_MAX_FOLIOS);
  324. ret = btrfs_alloc_folio_array(BTRFS_STRIPE_LEN >> min_folio_shift,
  325. fs_info->block_min_order, stripe->folios);
  326. if (ret < 0)
  327. goto error;
  328. stripe->sectors = kzalloc_objs(struct scrub_sector_verification,
  329. stripe->nr_sectors);
  330. if (!stripe->sectors)
  331. goto error;
  332. stripe->csums = kcalloc(BTRFS_STRIPE_LEN >> fs_info->sectorsize_bits,
  333. fs_info->csum_size, GFP_KERNEL);
  334. if (!stripe->csums)
  335. goto error;
  336. return 0;
  337. error:
  338. release_scrub_stripe(stripe);
  339. return -ENOMEM;
  340. }
  341. static void wait_scrub_stripe_io(struct scrub_stripe *stripe)
  342. {
  343. wait_event(stripe->io_wait, atomic_read(&stripe->pending_io) == 0);
  344. }
  345. static void scrub_put_ctx(struct scrub_ctx *sctx);
  346. static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
  347. {
  348. while (atomic_read(&fs_info->scrub_pause_req)) {
  349. mutex_unlock(&fs_info->scrub_lock);
  350. wait_event(fs_info->scrub_pause_wait,
  351. atomic_read(&fs_info->scrub_pause_req) == 0);
  352. mutex_lock(&fs_info->scrub_lock);
  353. }
  354. }
  355. static void scrub_pause_on(struct btrfs_fs_info *fs_info)
  356. {
  357. atomic_inc(&fs_info->scrubs_paused);
  358. wake_up(&fs_info->scrub_pause_wait);
  359. }
  360. static void scrub_pause_off(struct btrfs_fs_info *fs_info)
  361. {
  362. mutex_lock(&fs_info->scrub_lock);
  363. __scrub_blocked_if_needed(fs_info);
  364. atomic_dec(&fs_info->scrubs_paused);
  365. mutex_unlock(&fs_info->scrub_lock);
  366. wake_up(&fs_info->scrub_pause_wait);
  367. }
  368. static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
  369. {
  370. scrub_pause_on(fs_info);
  371. scrub_pause_off(fs_info);
  372. }
  373. static noinline_for_stack void scrub_free_ctx(struct scrub_ctx *sctx)
  374. {
  375. int i;
  376. if (!sctx)
  377. return;
  378. for (i = 0; i < SCRUB_TOTAL_STRIPES; i++)
  379. release_scrub_stripe(&sctx->stripes[i]);
  380. kvfree(sctx);
  381. }
  382. static void scrub_put_ctx(struct scrub_ctx *sctx)
  383. {
  384. if (refcount_dec_and_test(&sctx->refs))
  385. scrub_free_ctx(sctx);
  386. }
  387. static noinline_for_stack struct scrub_ctx *scrub_setup_ctx(
  388. struct btrfs_fs_info *fs_info, bool is_dev_replace)
  389. {
  390. struct scrub_ctx *sctx;
  391. int i;
  392. /* Since sctx has inline 128 stripes, it can go beyond 64K easily. Use
  393. * kvzalloc().
  394. */
  395. sctx = kvzalloc_obj(*sctx);
  396. if (!sctx)
  397. goto nomem;
  398. refcount_set(&sctx->refs, 1);
  399. sctx->is_dev_replace = is_dev_replace;
  400. sctx->fs_info = fs_info;
  401. sctx->extent_path.search_commit_root = true;
  402. sctx->extent_path.skip_locking = true;
  403. sctx->csum_path.search_commit_root = true;
  404. sctx->csum_path.skip_locking = true;
  405. for (i = 0; i < SCRUB_TOTAL_STRIPES; i++) {
  406. int ret;
  407. ret = init_scrub_stripe(fs_info, &sctx->stripes[i]);
  408. if (ret < 0)
  409. goto nomem;
  410. sctx->stripes[i].sctx = sctx;
  411. }
  412. sctx->first_free = 0;
  413. atomic_set(&sctx->cancel_req, 0);
  414. spin_lock_init(&sctx->stat_lock);
  415. sctx->throttle_deadline = 0;
  416. mutex_init(&sctx->wr_lock);
  417. if (is_dev_replace) {
  418. WARN_ON(!fs_info->dev_replace.tgtdev);
  419. sctx->wr_tgtdev = fs_info->dev_replace.tgtdev;
  420. }
  421. return sctx;
  422. nomem:
  423. scrub_free_ctx(sctx);
  424. return ERR_PTR(-ENOMEM);
  425. }
  426. static int scrub_print_warning_inode(u64 inum, u64 offset, u64 num_bytes,
  427. u64 root, void *warn_ctx)
  428. {
  429. u32 nlink;
  430. int ret;
  431. int i;
  432. unsigned nofs_flag;
  433. struct extent_buffer *eb;
  434. struct btrfs_inode_item *inode_item;
  435. struct scrub_warning *swarn = warn_ctx;
  436. struct btrfs_fs_info *fs_info = swarn->dev->fs_info;
  437. struct inode_fs_paths *ipath __free(inode_fs_paths) = NULL;
  438. struct btrfs_root *local_root;
  439. struct btrfs_key key;
  440. local_root = btrfs_get_fs_root(fs_info, root, true);
  441. if (IS_ERR(local_root)) {
  442. ret = PTR_ERR(local_root);
  443. goto err;
  444. }
  445. /*
  446. * this makes the path point to (inum INODE_ITEM ioff)
  447. */
  448. key.objectid = inum;
  449. key.type = BTRFS_INODE_ITEM_KEY;
  450. key.offset = 0;
  451. ret = btrfs_search_slot(NULL, local_root, &key, swarn->path, 0, 0);
  452. if (ret) {
  453. btrfs_put_root(local_root);
  454. btrfs_release_path(swarn->path);
  455. goto err;
  456. }
  457. eb = swarn->path->nodes[0];
  458. inode_item = btrfs_item_ptr(eb, swarn->path->slots[0],
  459. struct btrfs_inode_item);
  460. nlink = btrfs_inode_nlink(eb, inode_item);
  461. btrfs_release_path(swarn->path);
  462. /*
  463. * init_path might indirectly call vmalloc, or use GFP_KERNEL. Scrub
  464. * uses GFP_NOFS in this context, so we keep it consistent but it does
  465. * not seem to be strictly necessary.
  466. */
  467. nofs_flag = memalloc_nofs_save();
  468. ipath = init_ipath(4096, local_root, swarn->path);
  469. memalloc_nofs_restore(nofs_flag);
  470. if (IS_ERR(ipath)) {
  471. btrfs_put_root(local_root);
  472. ret = PTR_ERR(ipath);
  473. ipath = NULL;
  474. goto err;
  475. }
  476. ret = paths_from_inode(inum, ipath);
  477. if (ret < 0)
  478. goto err;
  479. /*
  480. * we deliberately ignore the bit ipath might have been too small to
  481. * hold all of the paths here
  482. */
  483. for (i = 0; i < ipath->fspath->elem_cnt; ++i)
  484. btrfs_warn(fs_info,
  485. "scrub: %s at logical %llu on dev %s, physical %llu root %llu inode %llu offset %llu length %u links %u (path: %s)",
  486. swarn->errstr, swarn->logical,
  487. btrfs_dev_name(swarn->dev),
  488. swarn->physical,
  489. root, inum, offset,
  490. fs_info->sectorsize, nlink,
  491. (char *)(unsigned long)ipath->fspath->val[i]);
  492. btrfs_put_root(local_root);
  493. return 0;
  494. err:
  495. btrfs_warn(fs_info,
  496. "scrub: %s at logical %llu on dev %s, physical %llu root %llu inode %llu offset %llu: path resolving failed with ret=%d",
  497. swarn->errstr, swarn->logical,
  498. btrfs_dev_name(swarn->dev),
  499. swarn->physical,
  500. root, inum, offset, ret);
  501. return 0;
  502. }
  503. static void scrub_print_common_warning(const char *errstr, struct btrfs_device *dev,
  504. bool is_super, u64 logical, u64 physical)
  505. {
  506. struct btrfs_fs_info *fs_info = dev->fs_info;
  507. BTRFS_PATH_AUTO_FREE(path);
  508. struct btrfs_key found_key;
  509. struct extent_buffer *eb;
  510. struct btrfs_extent_item *ei;
  511. struct scrub_warning swarn;
  512. u64 flags = 0;
  513. u32 item_size;
  514. int ret;
  515. /* Super block error, no need to search extent tree. */
  516. if (is_super) {
  517. btrfs_warn(fs_info, "scrub: %s on device %s, physical %llu",
  518. errstr, btrfs_dev_name(dev), physical);
  519. return;
  520. }
  521. path = btrfs_alloc_path();
  522. if (!path)
  523. return;
  524. swarn.physical = physical;
  525. swarn.logical = logical;
  526. swarn.errstr = errstr;
  527. swarn.dev = NULL;
  528. ret = extent_from_logical(fs_info, swarn.logical, path, &found_key,
  529. &flags);
  530. if (ret < 0)
  531. return;
  532. swarn.extent_item_size = found_key.offset;
  533. eb = path->nodes[0];
  534. ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
  535. item_size = btrfs_item_size(eb, path->slots[0]);
  536. if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  537. unsigned long ptr = 0;
  538. u8 ref_level;
  539. u64 ref_root;
  540. while (true) {
  541. ret = tree_backref_for_extent(&ptr, eb, &found_key, ei,
  542. item_size, &ref_root,
  543. &ref_level);
  544. if (ret < 0) {
  545. btrfs_warn(fs_info,
  546. "scrub: failed to resolve tree backref for logical %llu: %d",
  547. swarn.logical, ret);
  548. break;
  549. }
  550. if (ret > 0)
  551. break;
  552. btrfs_warn(fs_info,
  553. "scrub: %s at logical %llu on dev %s, physical %llu: metadata %s (level %d) in tree %llu",
  554. errstr, swarn.logical, btrfs_dev_name(dev),
  555. swarn.physical, (ref_level ? "node" : "leaf"),
  556. ref_level, ref_root);
  557. }
  558. btrfs_release_path(path);
  559. } else {
  560. struct btrfs_backref_walk_ctx ctx = { 0 };
  561. btrfs_release_path(path);
  562. ctx.bytenr = found_key.objectid;
  563. ctx.extent_item_pos = swarn.logical - found_key.objectid;
  564. ctx.fs_info = fs_info;
  565. swarn.path = path;
  566. swarn.dev = dev;
  567. iterate_extent_inodes(&ctx, true, scrub_print_warning_inode, &swarn);
  568. }
  569. }
  570. static int fill_writer_pointer_gap(struct scrub_ctx *sctx, u64 physical)
  571. {
  572. int ret = 0;
  573. u64 length;
  574. if (!btrfs_is_zoned(sctx->fs_info))
  575. return 0;
  576. if (!btrfs_dev_is_sequential(sctx->wr_tgtdev, physical))
  577. return 0;
  578. if (sctx->write_pointer < physical) {
  579. length = physical - sctx->write_pointer;
  580. ret = btrfs_zoned_issue_zeroout(sctx->wr_tgtdev,
  581. sctx->write_pointer, length);
  582. if (!ret)
  583. sctx->write_pointer = physical;
  584. }
  585. return ret;
  586. }
  587. static void *scrub_stripe_get_kaddr(struct scrub_stripe *stripe, int sector_nr)
  588. {
  589. struct btrfs_fs_info *fs_info = stripe->bg->fs_info;
  590. const u32 min_folio_shift = PAGE_SHIFT + fs_info->block_min_order;
  591. u32 offset = (sector_nr << fs_info->sectorsize_bits);
  592. const struct folio *folio = stripe->folios[offset >> min_folio_shift];
  593. /* stripe->folios[] is allocated by us and no highmem is allowed. */
  594. ASSERT(folio);
  595. ASSERT(!folio_test_highmem(folio));
  596. return folio_address(folio) + offset_in_folio(folio, offset);
  597. }
  598. static phys_addr_t scrub_stripe_get_paddr(struct scrub_stripe *stripe, int sector_nr)
  599. {
  600. struct btrfs_fs_info *fs_info = stripe->bg->fs_info;
  601. const u32 min_folio_shift = PAGE_SHIFT + fs_info->block_min_order;
  602. u32 offset = (sector_nr << fs_info->sectorsize_bits);
  603. const struct folio *folio = stripe->folios[offset >> min_folio_shift];
  604. /* stripe->folios[] is allocated by us and no highmem is allowed. */
  605. ASSERT(folio);
  606. ASSERT(!folio_test_highmem(folio));
  607. /* And the range must be contained inside the folio. */
  608. ASSERT(offset_in_folio(folio, offset) + fs_info->sectorsize <= folio_size(folio));
  609. return page_to_phys(folio_page(folio, 0)) + offset_in_folio(folio, offset);
  610. }
  611. static void scrub_verify_one_metadata(struct scrub_stripe *stripe, int sector_nr)
  612. {
  613. struct btrfs_fs_info *fs_info = stripe->bg->fs_info;
  614. const u32 sectors_per_tree = fs_info->nodesize >> fs_info->sectorsize_bits;
  615. const u64 logical = stripe->logical + (sector_nr << fs_info->sectorsize_bits);
  616. void *first_kaddr = scrub_stripe_get_kaddr(stripe, sector_nr);
  617. struct btrfs_header *header = first_kaddr;
  618. struct btrfs_csum_ctx csum;
  619. u8 on_disk_csum[BTRFS_CSUM_SIZE];
  620. u8 calculated_csum[BTRFS_CSUM_SIZE];
  621. /*
  622. * Here we don't have a good way to attach the pages (and subpages)
  623. * to a dummy extent buffer, thus we have to directly grab the members
  624. * from pages.
  625. */
  626. memcpy(on_disk_csum, header->csum, fs_info->csum_size);
  627. if (logical != btrfs_stack_header_bytenr(header)) {
  628. scrub_bitmap_set_meta_error(stripe, sector_nr, sectors_per_tree);
  629. scrub_bitmap_set_error(stripe, sector_nr, sectors_per_tree);
  630. btrfs_warn_rl(fs_info,
  631. "scrub: tree block %llu mirror %u has bad bytenr, has %llu want %llu",
  632. logical, stripe->mirror_num,
  633. btrfs_stack_header_bytenr(header), logical);
  634. return;
  635. }
  636. if (memcmp(header->fsid, fs_info->fs_devices->metadata_uuid,
  637. BTRFS_FSID_SIZE) != 0) {
  638. scrub_bitmap_set_meta_error(stripe, sector_nr, sectors_per_tree);
  639. scrub_bitmap_set_error(stripe, sector_nr, sectors_per_tree);
  640. btrfs_warn_rl(fs_info,
  641. "scrub: tree block %llu mirror %u has bad fsid, has %pU want %pU",
  642. logical, stripe->mirror_num,
  643. header->fsid, fs_info->fs_devices->metadata_uuid);
  644. return;
  645. }
  646. if (memcmp(header->chunk_tree_uuid, fs_info->chunk_tree_uuid,
  647. BTRFS_UUID_SIZE) != 0) {
  648. scrub_bitmap_set_meta_error(stripe, sector_nr, sectors_per_tree);
  649. scrub_bitmap_set_error(stripe, sector_nr, sectors_per_tree);
  650. btrfs_warn_rl(fs_info,
  651. "scrub: tree block %llu mirror %u has bad chunk tree uuid, has %pU want %pU",
  652. logical, stripe->mirror_num,
  653. header->chunk_tree_uuid, fs_info->chunk_tree_uuid);
  654. return;
  655. }
  656. /* Now check tree block csum. */
  657. btrfs_csum_init(&csum, fs_info->csum_type);
  658. btrfs_csum_update(&csum, first_kaddr + BTRFS_CSUM_SIZE,
  659. fs_info->sectorsize - BTRFS_CSUM_SIZE);
  660. for (int i = sector_nr + 1; i < sector_nr + sectors_per_tree; i++) {
  661. btrfs_csum_update(&csum, scrub_stripe_get_kaddr(stripe, i),
  662. fs_info->sectorsize);
  663. }
  664. btrfs_csum_final(&csum, calculated_csum);
  665. if (memcmp(calculated_csum, on_disk_csum, fs_info->csum_size) != 0) {
  666. scrub_bitmap_set_meta_error(stripe, sector_nr, sectors_per_tree);
  667. scrub_bitmap_set_error(stripe, sector_nr, sectors_per_tree);
  668. btrfs_warn_rl(fs_info,
  669. "scrub: tree block %llu mirror %u has bad csum, has " BTRFS_CSUM_FMT " want " BTRFS_CSUM_FMT,
  670. logical, stripe->mirror_num,
  671. BTRFS_CSUM_FMT_VALUE(fs_info->csum_size, on_disk_csum),
  672. BTRFS_CSUM_FMT_VALUE(fs_info->csum_size, calculated_csum));
  673. return;
  674. }
  675. if (stripe->sectors[sector_nr].generation !=
  676. btrfs_stack_header_generation(header)) {
  677. scrub_bitmap_set_meta_gen_error(stripe, sector_nr, sectors_per_tree);
  678. scrub_bitmap_set_error(stripe, sector_nr, sectors_per_tree);
  679. btrfs_warn_rl(fs_info,
  680. "scrub: tree block %llu mirror %u has bad generation, has %llu want %llu",
  681. logical, stripe->mirror_num,
  682. btrfs_stack_header_generation(header),
  683. stripe->sectors[sector_nr].generation);
  684. return;
  685. }
  686. scrub_bitmap_clear_error(stripe, sector_nr, sectors_per_tree);
  687. scrub_bitmap_clear_csum_error(stripe, sector_nr, sectors_per_tree);
  688. scrub_bitmap_clear_meta_error(stripe, sector_nr, sectors_per_tree);
  689. scrub_bitmap_clear_meta_gen_error(stripe, sector_nr, sectors_per_tree);
  690. }
  691. static void scrub_verify_one_sector(struct scrub_stripe *stripe, int sector_nr)
  692. {
  693. struct btrfs_fs_info *fs_info = stripe->bg->fs_info;
  694. struct scrub_sector_verification *sector = &stripe->sectors[sector_nr];
  695. const u32 sectors_per_tree = fs_info->nodesize >> fs_info->sectorsize_bits;
  696. phys_addr_t paddr = scrub_stripe_get_paddr(stripe, sector_nr);
  697. u8 csum_buf[BTRFS_CSUM_SIZE];
  698. int ret;
  699. ASSERT(sector_nr >= 0 && sector_nr < stripe->nr_sectors);
  700. /* Sector not utilized, skip it. */
  701. if (!scrub_bitmap_test_bit_has_extent(stripe, sector_nr))
  702. return;
  703. /* IO error, no need to check. */
  704. if (scrub_bitmap_test_bit_io_error(stripe, sector_nr))
  705. return;
  706. /* Metadata, verify the full tree block. */
  707. if (scrub_bitmap_test_bit_is_metadata(stripe, sector_nr)) {
  708. /*
  709. * Check if the tree block crosses the stripe boundary. If
  710. * crossed the boundary, we cannot verify it but only give a
  711. * warning.
  712. *
  713. * This can only happen on a very old filesystem where chunks
  714. * are not ensured to be stripe aligned.
  715. */
  716. if (unlikely(sector_nr + sectors_per_tree > stripe->nr_sectors)) {
  717. btrfs_warn_rl(fs_info,
  718. "scrub: tree block at %llu crosses stripe boundary %llu",
  719. stripe->logical +
  720. (sector_nr << fs_info->sectorsize_bits),
  721. stripe->logical);
  722. return;
  723. }
  724. scrub_verify_one_metadata(stripe, sector_nr);
  725. return;
  726. }
  727. /*
  728. * Data is easier, we just verify the data csum (if we have it). For
  729. * cases without csum, we have no other choice but to trust it.
  730. */
  731. if (!sector->csum) {
  732. scrub_bitmap_clear_bit_error(stripe, sector_nr);
  733. return;
  734. }
  735. ret = btrfs_check_block_csum(fs_info, paddr, csum_buf, sector->csum);
  736. if (ret < 0) {
  737. scrub_bitmap_set_bit_csum_error(stripe, sector_nr);
  738. scrub_bitmap_set_bit_error(stripe, sector_nr);
  739. } else {
  740. scrub_bitmap_clear_bit_csum_error(stripe, sector_nr);
  741. scrub_bitmap_clear_bit_error(stripe, sector_nr);
  742. }
  743. }
  744. /* Verify specified sectors of a stripe. */
  745. static void scrub_verify_one_stripe(struct scrub_stripe *stripe, unsigned long bitmap)
  746. {
  747. struct btrfs_fs_info *fs_info = stripe->bg->fs_info;
  748. const u32 sectors_per_tree = fs_info->nodesize >> fs_info->sectorsize_bits;
  749. int sector_nr;
  750. for_each_set_bit(sector_nr, &bitmap, stripe->nr_sectors) {
  751. scrub_verify_one_sector(stripe, sector_nr);
  752. if (scrub_bitmap_test_bit_is_metadata(stripe, sector_nr))
  753. sector_nr += sectors_per_tree - 1;
  754. }
  755. }
  756. static int calc_sector_number(struct scrub_stripe *stripe, struct bio_vec *first_bvec)
  757. {
  758. int i;
  759. for (i = 0; i < stripe->nr_sectors; i++) {
  760. if (scrub_stripe_get_kaddr(stripe, i) == bvec_virt(first_bvec))
  761. break;
  762. }
  763. ASSERT(i < stripe->nr_sectors);
  764. return i;
  765. }
  766. /*
  767. * Repair read is different to the regular read:
  768. *
  769. * - Only reads the failed sectors
  770. * - May have extra blocksize limits
  771. */
  772. static void scrub_repair_read_endio(struct btrfs_bio *bbio)
  773. {
  774. struct scrub_stripe *stripe = bbio->private;
  775. struct btrfs_fs_info *fs_info = stripe->bg->fs_info;
  776. struct bio_vec *bvec;
  777. int sector_nr = calc_sector_number(stripe, bio_first_bvec_all(&bbio->bio));
  778. u32 bio_size = 0;
  779. int i;
  780. ASSERT(sector_nr < stripe->nr_sectors);
  781. bio_for_each_bvec_all(bvec, &bbio->bio, i)
  782. bio_size += bvec->bv_len;
  783. if (bbio->bio.bi_status) {
  784. scrub_bitmap_set_io_error(stripe, sector_nr,
  785. bio_size >> fs_info->sectorsize_bits);
  786. scrub_bitmap_set_error(stripe, sector_nr,
  787. bio_size >> fs_info->sectorsize_bits);
  788. } else {
  789. scrub_bitmap_clear_io_error(stripe, sector_nr,
  790. bio_size >> fs_info->sectorsize_bits);
  791. }
  792. bio_put(&bbio->bio);
  793. if (atomic_dec_and_test(&stripe->pending_io))
  794. wake_up(&stripe->io_wait);
  795. }
  796. static int calc_next_mirror(int mirror, int num_copies)
  797. {
  798. ASSERT(mirror <= num_copies);
  799. return (mirror + 1 > num_copies) ? 1 : mirror + 1;
  800. }
  801. static void scrub_bio_add_sector(struct btrfs_bio *bbio, struct scrub_stripe *stripe,
  802. int sector_nr)
  803. {
  804. struct btrfs_fs_info *fs_info = bbio->inode->root->fs_info;
  805. void *kaddr = scrub_stripe_get_kaddr(stripe, sector_nr);
  806. int ret;
  807. ret = bio_add_page(&bbio->bio, virt_to_page(kaddr), fs_info->sectorsize,
  808. offset_in_page(kaddr));
  809. /*
  810. * Caller should ensure the bbio has enough size.
  811. * And we cannot use __bio_add_page(), which doesn't do any merge.
  812. *
  813. * Meanwhile for scrub_submit_initial_read() we fully rely on the merge
  814. * to create the minimal amount of bio vectors, for fs block size < page
  815. * size cases.
  816. */
  817. ASSERT(ret == fs_info->sectorsize);
  818. }
  819. static struct btrfs_bio *alloc_scrub_bbio(struct btrfs_fs_info *fs_info,
  820. unsigned int nr_vecs, blk_opf_t opf,
  821. u64 logical,
  822. btrfs_bio_end_io_t end_io, void *private)
  823. {
  824. struct btrfs_bio *bbio;
  825. bbio = btrfs_bio_alloc(nr_vecs, opf, BTRFS_I(fs_info->btree_inode),
  826. logical, end_io, private);
  827. bbio->is_scrub = true;
  828. bbio->bio.bi_iter.bi_sector = logical >> SECTOR_SHIFT;
  829. return bbio;
  830. }
  831. static void scrub_stripe_submit_repair_read(struct scrub_stripe *stripe,
  832. int mirror, int blocksize, bool wait)
  833. {
  834. struct btrfs_fs_info *fs_info = stripe->bg->fs_info;
  835. struct btrfs_bio *bbio = NULL;
  836. const unsigned long old_error_bitmap = scrub_bitmap_read_error(stripe);
  837. int i;
  838. ASSERT(stripe->mirror_num >= 1, "stripe->mirror_num=%d", stripe->mirror_num);
  839. ASSERT(atomic_read(&stripe->pending_io) == 0,
  840. "atomic_read(&stripe->pending_io)=%d", atomic_read(&stripe->pending_io));
  841. for_each_set_bit(i, &old_error_bitmap, stripe->nr_sectors) {
  842. /* The current sector cannot be merged, submit the bio. */
  843. if (bbio && ((i > 0 && !test_bit(i - 1, &old_error_bitmap)) ||
  844. bbio->bio.bi_iter.bi_size >= blocksize)) {
  845. ASSERT(bbio->bio.bi_iter.bi_size);
  846. atomic_inc(&stripe->pending_io);
  847. btrfs_submit_bbio(bbio, mirror);
  848. if (wait)
  849. wait_scrub_stripe_io(stripe);
  850. bbio = NULL;
  851. }
  852. if (!bbio)
  853. bbio = alloc_scrub_bbio(fs_info, stripe->nr_sectors, REQ_OP_READ,
  854. stripe->logical + (i << fs_info->sectorsize_bits),
  855. scrub_repair_read_endio, stripe);
  856. scrub_bio_add_sector(bbio, stripe, i);
  857. }
  858. if (bbio) {
  859. ASSERT(bbio->bio.bi_iter.bi_size);
  860. atomic_inc(&stripe->pending_io);
  861. btrfs_submit_bbio(bbio, mirror);
  862. if (wait)
  863. wait_scrub_stripe_io(stripe);
  864. }
  865. }
  866. static void scrub_stripe_report_errors(struct scrub_ctx *sctx,
  867. struct scrub_stripe *stripe,
  868. const struct scrub_error_records *errors)
  869. {
  870. static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL,
  871. DEFAULT_RATELIMIT_BURST);
  872. struct btrfs_fs_info *fs_info = sctx->fs_info;
  873. struct btrfs_device *dev = NULL;
  874. const unsigned long extent_bitmap = scrub_bitmap_read_has_extent(stripe);
  875. const unsigned long error_bitmap = scrub_bitmap_read_error(stripe);
  876. u64 physical = 0;
  877. int nr_data_sectors = 0;
  878. int nr_meta_sectors = 0;
  879. int nr_nodatacsum_sectors = 0;
  880. int nr_repaired_sectors = 0;
  881. int sector_nr;
  882. if (test_bit(SCRUB_STRIPE_FLAG_NO_REPORT, &stripe->state))
  883. return;
  884. /*
  885. * Init needed infos for error reporting.
  886. *
  887. * Although our scrub_stripe infrastructure is mostly based on btrfs_submit_bio()
  888. * thus no need for dev/physical, error reporting still needs dev and physical.
  889. */
  890. if (!bitmap_empty(&errors->init_error_bitmap, stripe->nr_sectors)) {
  891. u64 mapped_len = fs_info->sectorsize;
  892. struct btrfs_io_context *bioc = NULL;
  893. int stripe_index = stripe->mirror_num - 1;
  894. int ret;
  895. /* For scrub, our mirror_num should always start at 1. */
  896. ASSERT(stripe->mirror_num >= 1, "stripe->mirror_num=%d", stripe->mirror_num);
  897. ret = btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS,
  898. stripe->logical, &mapped_len, &bioc,
  899. NULL, NULL);
  900. /*
  901. * If we failed, dev will be NULL, and later detailed reports
  902. * will just be skipped.
  903. */
  904. if (ret < 0)
  905. goto skip;
  906. physical = bioc->stripes[stripe_index].physical;
  907. dev = bioc->stripes[stripe_index].dev;
  908. btrfs_put_bioc(bioc);
  909. }
  910. skip:
  911. for_each_set_bit(sector_nr, &extent_bitmap, stripe->nr_sectors) {
  912. bool repaired = false;
  913. if (scrub_bitmap_test_bit_is_metadata(stripe, sector_nr)) {
  914. nr_meta_sectors++;
  915. } else {
  916. nr_data_sectors++;
  917. if (!stripe->sectors[sector_nr].csum)
  918. nr_nodatacsum_sectors++;
  919. }
  920. if (test_bit(sector_nr, &errors->init_error_bitmap) &&
  921. !test_bit(sector_nr, &error_bitmap)) {
  922. nr_repaired_sectors++;
  923. repaired = true;
  924. }
  925. /* Good sector from the beginning, nothing need to be done. */
  926. if (!test_bit(sector_nr, &errors->init_error_bitmap))
  927. continue;
  928. /*
  929. * Report error for the corrupted sectors. If repaired, just
  930. * output the message of repaired message.
  931. */
  932. if (repaired) {
  933. if (dev) {
  934. btrfs_err_rl(fs_info,
  935. "scrub: fixed up error at logical %llu on dev %s physical %llu",
  936. stripe->logical, btrfs_dev_name(dev),
  937. physical);
  938. } else {
  939. btrfs_err_rl(fs_info,
  940. "scrub: fixed up error at logical %llu on mirror %u",
  941. stripe->logical, stripe->mirror_num);
  942. }
  943. continue;
  944. }
  945. /* The remaining are all for unrepaired. */
  946. if (dev) {
  947. btrfs_err_rl(fs_info,
  948. "scrub: unable to fixup (regular) error at logical %llu on dev %s physical %llu",
  949. stripe->logical, btrfs_dev_name(dev),
  950. physical);
  951. } else {
  952. btrfs_err_rl(fs_info,
  953. "scrub: unable to fixup (regular) error at logical %llu on mirror %u",
  954. stripe->logical, stripe->mirror_num);
  955. }
  956. if (scrub_bitmap_test_bit_io_error(stripe, sector_nr))
  957. if (__ratelimit(&rs) && dev)
  958. scrub_print_common_warning("i/o error", dev, false,
  959. stripe->logical, physical);
  960. if (scrub_bitmap_test_bit_csum_error(stripe, sector_nr))
  961. if (__ratelimit(&rs) && dev)
  962. scrub_print_common_warning("checksum error", dev, false,
  963. stripe->logical, physical);
  964. if (scrub_bitmap_test_bit_meta_error(stripe, sector_nr))
  965. if (__ratelimit(&rs) && dev)
  966. scrub_print_common_warning("header error", dev, false,
  967. stripe->logical, physical);
  968. if (scrub_bitmap_test_bit_meta_gen_error(stripe, sector_nr))
  969. if (__ratelimit(&rs) && dev)
  970. scrub_print_common_warning("generation error", dev, false,
  971. stripe->logical, physical);
  972. }
  973. /* Update the device stats. */
  974. for (int i = 0; i < errors->nr_io_errors; i++)
  975. btrfs_dev_stat_inc_and_print(stripe->dev, BTRFS_DEV_STAT_READ_ERRS);
  976. for (int i = 0; i < errors->nr_csum_errors; i++)
  977. btrfs_dev_stat_inc_and_print(stripe->dev, BTRFS_DEV_STAT_CORRUPTION_ERRS);
  978. /* Generation mismatch error is based on each metadata, not each block. */
  979. for (int i = 0; i < errors->nr_meta_gen_errors;
  980. i += (fs_info->nodesize >> fs_info->sectorsize_bits))
  981. btrfs_dev_stat_inc_and_print(stripe->dev, BTRFS_DEV_STAT_GENERATION_ERRS);
  982. spin_lock(&sctx->stat_lock);
  983. sctx->stat.data_extents_scrubbed += stripe->nr_data_extents;
  984. sctx->stat.tree_extents_scrubbed += stripe->nr_meta_extents;
  985. sctx->stat.data_bytes_scrubbed += nr_data_sectors << fs_info->sectorsize_bits;
  986. sctx->stat.tree_bytes_scrubbed += nr_meta_sectors << fs_info->sectorsize_bits;
  987. sctx->stat.no_csum += nr_nodatacsum_sectors;
  988. sctx->stat.read_errors += errors->nr_io_errors;
  989. sctx->stat.csum_errors += errors->nr_csum_errors;
  990. sctx->stat.verify_errors += errors->nr_meta_errors +
  991. errors->nr_meta_gen_errors;
  992. sctx->stat.uncorrectable_errors +=
  993. bitmap_weight(&error_bitmap, stripe->nr_sectors);
  994. sctx->stat.corrected_errors += nr_repaired_sectors;
  995. spin_unlock(&sctx->stat_lock);
  996. }
  997. static void scrub_write_sectors(struct scrub_ctx *sctx, struct scrub_stripe *stripe,
  998. unsigned long write_bitmap, bool dev_replace);
  999. /*
  1000. * The main entrance for all read related scrub work, including:
  1001. *
  1002. * - Wait for the initial read to finish
  1003. * - Verify and locate any bad sectors
  1004. * - Go through the remaining mirrors and try to read as large blocksize as
  1005. * possible
  1006. * - Go through all mirrors (including the failed mirror) sector-by-sector
  1007. * - Submit writeback for repaired sectors
  1008. *
  1009. * Writeback for dev-replace does not happen here, it needs extra
  1010. * synchronization for zoned devices.
  1011. */
  1012. static void scrub_stripe_read_repair_worker(struct work_struct *work)
  1013. {
  1014. struct scrub_stripe *stripe = container_of(work, struct scrub_stripe, work);
  1015. struct scrub_ctx *sctx = stripe->sctx;
  1016. struct btrfs_fs_info *fs_info = sctx->fs_info;
  1017. struct scrub_error_records errors = { 0 };
  1018. int num_copies = btrfs_num_copies(fs_info, stripe->bg->start,
  1019. stripe->bg->length);
  1020. unsigned long repaired;
  1021. unsigned long error;
  1022. int mirror;
  1023. int i;
  1024. ASSERT(stripe->mirror_num >= 1, "stripe->mirror_num=%d", stripe->mirror_num);
  1025. wait_scrub_stripe_io(stripe);
  1026. scrub_verify_one_stripe(stripe, scrub_bitmap_read_has_extent(stripe));
  1027. /* Save the initial failed bitmap for later repair and report usage. */
  1028. errors.init_error_bitmap = scrub_bitmap_read_error(stripe);
  1029. errors.nr_io_errors = scrub_bitmap_weight_io_error(stripe);
  1030. errors.nr_csum_errors = scrub_bitmap_weight_csum_error(stripe);
  1031. errors.nr_meta_errors = scrub_bitmap_weight_meta_error(stripe);
  1032. errors.nr_meta_gen_errors = scrub_bitmap_weight_meta_gen_error(stripe);
  1033. if (bitmap_empty(&errors.init_error_bitmap, stripe->nr_sectors))
  1034. goto out;
  1035. /*
  1036. * Try all remaining mirrors.
  1037. *
  1038. * Here we still try to read as large block as possible, as this is
  1039. * faster and we have extra safety nets to rely on.
  1040. */
  1041. for (mirror = calc_next_mirror(stripe->mirror_num, num_copies);
  1042. mirror != stripe->mirror_num;
  1043. mirror = calc_next_mirror(mirror, num_copies)) {
  1044. const unsigned long old_error_bitmap = scrub_bitmap_read_error(stripe);
  1045. scrub_stripe_submit_repair_read(stripe, mirror,
  1046. BTRFS_STRIPE_LEN, false);
  1047. wait_scrub_stripe_io(stripe);
  1048. scrub_verify_one_stripe(stripe, old_error_bitmap);
  1049. if (scrub_bitmap_empty_error(stripe))
  1050. goto out;
  1051. }
  1052. /*
  1053. * Last safety net, try re-checking all mirrors, including the failed
  1054. * one, sector-by-sector.
  1055. *
  1056. * As if one sector failed the drive's internal csum, the whole read
  1057. * containing the offending sector would be marked as error.
  1058. * Thus here we do sector-by-sector read.
  1059. *
  1060. * This can be slow, thus we only try it as the last resort.
  1061. */
  1062. for (i = 0, mirror = stripe->mirror_num;
  1063. i < num_copies;
  1064. i++, mirror = calc_next_mirror(mirror, num_copies)) {
  1065. const unsigned long old_error_bitmap = scrub_bitmap_read_error(stripe);
  1066. scrub_stripe_submit_repair_read(stripe, mirror,
  1067. fs_info->sectorsize, true);
  1068. wait_scrub_stripe_io(stripe);
  1069. scrub_verify_one_stripe(stripe, old_error_bitmap);
  1070. if (scrub_bitmap_empty_error(stripe))
  1071. goto out;
  1072. }
  1073. out:
  1074. error = scrub_bitmap_read_error(stripe);
  1075. /*
  1076. * Submit the repaired sectors. For zoned case, we cannot do repair
  1077. * in-place, but queue the bg to be relocated.
  1078. */
  1079. bitmap_andnot(&repaired, &errors.init_error_bitmap, &error,
  1080. stripe->nr_sectors);
  1081. if (!sctx->readonly && !bitmap_empty(&repaired, stripe->nr_sectors)) {
  1082. if (btrfs_is_zoned(fs_info)) {
  1083. btrfs_repair_one_zone(fs_info, sctx->stripes[0].bg->start);
  1084. } else {
  1085. scrub_write_sectors(sctx, stripe, repaired, false);
  1086. wait_scrub_stripe_io(stripe);
  1087. }
  1088. }
  1089. scrub_stripe_report_errors(sctx, stripe, &errors);
  1090. set_bit(SCRUB_STRIPE_FLAG_REPAIR_DONE, &stripe->state);
  1091. wake_up(&stripe->repair_wait);
  1092. }
  1093. static void scrub_read_endio(struct btrfs_bio *bbio)
  1094. {
  1095. struct scrub_stripe *stripe = bbio->private;
  1096. struct bio_vec *bvec;
  1097. int sector_nr = calc_sector_number(stripe, bio_first_bvec_all(&bbio->bio));
  1098. int num_sectors;
  1099. u32 bio_size = 0;
  1100. int i;
  1101. ASSERT(sector_nr < stripe->nr_sectors);
  1102. bio_for_each_bvec_all(bvec, &bbio->bio, i)
  1103. bio_size += bvec->bv_len;
  1104. num_sectors = bio_size >> stripe->bg->fs_info->sectorsize_bits;
  1105. if (bbio->bio.bi_status) {
  1106. scrub_bitmap_set_io_error(stripe, sector_nr, num_sectors);
  1107. scrub_bitmap_set_error(stripe, sector_nr, num_sectors);
  1108. } else {
  1109. scrub_bitmap_clear_io_error(stripe, sector_nr, num_sectors);
  1110. }
  1111. bio_put(&bbio->bio);
  1112. if (atomic_dec_and_test(&stripe->pending_io)) {
  1113. wake_up(&stripe->io_wait);
  1114. INIT_WORK(&stripe->work, scrub_stripe_read_repair_worker);
  1115. queue_work(stripe->bg->fs_info->scrub_workers, &stripe->work);
  1116. }
  1117. }
  1118. static void scrub_write_endio(struct btrfs_bio *bbio)
  1119. {
  1120. struct scrub_stripe *stripe = bbio->private;
  1121. struct btrfs_fs_info *fs_info = stripe->bg->fs_info;
  1122. struct bio_vec *bvec;
  1123. int sector_nr = calc_sector_number(stripe, bio_first_bvec_all(&bbio->bio));
  1124. u32 bio_size = 0;
  1125. int i;
  1126. bio_for_each_bvec_all(bvec, &bbio->bio, i)
  1127. bio_size += bvec->bv_len;
  1128. if (bbio->bio.bi_status) {
  1129. unsigned long flags;
  1130. spin_lock_irqsave(&stripe->write_error_lock, flags);
  1131. bitmap_set(&stripe->write_error_bitmap, sector_nr,
  1132. bio_size >> fs_info->sectorsize_bits);
  1133. spin_unlock_irqrestore(&stripe->write_error_lock, flags);
  1134. for (i = 0; i < (bio_size >> fs_info->sectorsize_bits); i++)
  1135. btrfs_dev_stat_inc_and_print(stripe->dev,
  1136. BTRFS_DEV_STAT_WRITE_ERRS);
  1137. }
  1138. bio_put(&bbio->bio);
  1139. if (atomic_dec_and_test(&stripe->pending_io))
  1140. wake_up(&stripe->io_wait);
  1141. }
  1142. static void scrub_submit_write_bio(struct scrub_ctx *sctx,
  1143. struct scrub_stripe *stripe,
  1144. struct btrfs_bio *bbio, bool dev_replace)
  1145. {
  1146. struct btrfs_fs_info *fs_info = sctx->fs_info;
  1147. u32 bio_len = bbio->bio.bi_iter.bi_size;
  1148. u32 bio_off = (bbio->bio.bi_iter.bi_sector << SECTOR_SHIFT) -
  1149. stripe->logical;
  1150. fill_writer_pointer_gap(sctx, stripe->physical + bio_off);
  1151. atomic_inc(&stripe->pending_io);
  1152. btrfs_submit_repair_write(bbio, stripe->mirror_num, dev_replace);
  1153. if (!btrfs_is_zoned(fs_info))
  1154. return;
  1155. /*
  1156. * For zoned writeback, queue depth must be 1, thus we must wait for
  1157. * the write to finish before the next write.
  1158. */
  1159. wait_scrub_stripe_io(stripe);
  1160. /*
  1161. * And also need to update the write pointer if write finished
  1162. * successfully.
  1163. */
  1164. if (!test_bit(bio_off >> fs_info->sectorsize_bits,
  1165. &stripe->write_error_bitmap))
  1166. sctx->write_pointer += bio_len;
  1167. }
  1168. /*
  1169. * Submit the write bio(s) for the sectors specified by @write_bitmap.
  1170. *
  1171. * Here we utilize btrfs_submit_repair_write(), which has some extra benefits:
  1172. *
  1173. * - Only needs logical bytenr and mirror_num
  1174. * Just like the scrub read path
  1175. *
  1176. * - Would only result in writes to the specified mirror
  1177. * Unlike the regular writeback path, which would write back to all stripes
  1178. *
  1179. * - Handle dev-replace and read-repair writeback differently
  1180. */
  1181. static void scrub_write_sectors(struct scrub_ctx *sctx, struct scrub_stripe *stripe,
  1182. unsigned long write_bitmap, bool dev_replace)
  1183. {
  1184. struct btrfs_fs_info *fs_info = stripe->bg->fs_info;
  1185. struct btrfs_bio *bbio = NULL;
  1186. int sector_nr;
  1187. for_each_set_bit(sector_nr, &write_bitmap, stripe->nr_sectors) {
  1188. /* We should only writeback sectors covered by an extent. */
  1189. ASSERT(scrub_bitmap_test_bit_has_extent(stripe, sector_nr));
  1190. /* Cannot merge with previous sector, submit the current one. */
  1191. if (bbio && sector_nr && !test_bit(sector_nr - 1, &write_bitmap)) {
  1192. scrub_submit_write_bio(sctx, stripe, bbio, dev_replace);
  1193. bbio = NULL;
  1194. }
  1195. if (!bbio)
  1196. bbio = alloc_scrub_bbio(fs_info, stripe->nr_sectors, REQ_OP_WRITE,
  1197. stripe->logical + (sector_nr << fs_info->sectorsize_bits),
  1198. scrub_write_endio, stripe);
  1199. scrub_bio_add_sector(bbio, stripe, sector_nr);
  1200. }
  1201. if (bbio)
  1202. scrub_submit_write_bio(sctx, stripe, bbio, dev_replace);
  1203. }
  1204. /*
  1205. * Throttling of IO submission, bandwidth-limit based, the timeslice is 1
  1206. * second. Limit can be set via /sys/fs/UUID/devinfo/devid/scrub_speed_max.
  1207. */
  1208. static void scrub_throttle_dev_io(struct scrub_ctx *sctx, struct btrfs_device *device,
  1209. unsigned int bio_size)
  1210. {
  1211. const int time_slice = 1000;
  1212. s64 delta;
  1213. ktime_t now;
  1214. u32 div;
  1215. u64 bwlimit;
  1216. bwlimit = READ_ONCE(device->scrub_speed_max);
  1217. if (bwlimit == 0)
  1218. return;
  1219. /*
  1220. * Slice is divided into intervals when the IO is submitted, adjust by
  1221. * bwlimit and maximum of 64 intervals.
  1222. */
  1223. div = clamp(bwlimit / (16 * 1024 * 1024), 1, 64);
  1224. /* Start new epoch, set deadline */
  1225. now = ktime_get();
  1226. if (sctx->throttle_deadline == 0) {
  1227. sctx->throttle_deadline = ktime_add_ms(now, time_slice / div);
  1228. sctx->throttle_sent = 0;
  1229. }
  1230. /* Still in the time to send? */
  1231. if (ktime_before(now, sctx->throttle_deadline)) {
  1232. /* If current bio is within the limit, send it */
  1233. sctx->throttle_sent += bio_size;
  1234. if (sctx->throttle_sent <= div_u64(bwlimit, div))
  1235. return;
  1236. /* We're over the limit, sleep until the rest of the slice */
  1237. delta = ktime_ms_delta(sctx->throttle_deadline, now);
  1238. } else {
  1239. /* New request after deadline, start new epoch */
  1240. delta = 0;
  1241. }
  1242. if (delta) {
  1243. long timeout;
  1244. timeout = div_u64(delta * HZ, 1000);
  1245. schedule_timeout_interruptible(timeout);
  1246. }
  1247. /* Next call will start the deadline period */
  1248. sctx->throttle_deadline = 0;
  1249. }
  1250. /*
  1251. * Given a physical address, this will calculate it's
  1252. * logical offset. if this is a parity stripe, it will return
  1253. * the most left data stripe's logical offset.
  1254. *
  1255. * return 0 if it is a data stripe, 1 means parity stripe.
  1256. */
  1257. static int get_raid56_logic_offset(u64 physical, int num,
  1258. struct btrfs_chunk_map *map, u64 *offset,
  1259. u64 *stripe_start)
  1260. {
  1261. int i;
  1262. int j = 0;
  1263. u64 last_offset;
  1264. const int data_stripes = nr_data_stripes(map);
  1265. last_offset = (physical - map->stripes[num].physical) * data_stripes;
  1266. if (stripe_start)
  1267. *stripe_start = last_offset;
  1268. *offset = last_offset;
  1269. for (i = 0; i < data_stripes; i++) {
  1270. u32 stripe_nr;
  1271. u32 stripe_index;
  1272. u32 rot;
  1273. *offset = last_offset + btrfs_stripe_nr_to_offset(i);
  1274. stripe_nr = (u32)(*offset >> BTRFS_STRIPE_LEN_SHIFT) / data_stripes;
  1275. /* Work out the disk rotation on this stripe-set */
  1276. rot = stripe_nr % map->num_stripes;
  1277. /* calculate which stripe this data locates */
  1278. rot += i;
  1279. stripe_index = rot % map->num_stripes;
  1280. if (stripe_index == num)
  1281. return 0;
  1282. if (stripe_index < num)
  1283. j++;
  1284. }
  1285. *offset = last_offset + btrfs_stripe_nr_to_offset(j);
  1286. return 1;
  1287. }
  1288. /*
  1289. * Return 0 if the extent item range covers any byte of the range.
  1290. * Return <0 if the extent item is before @search_start.
  1291. * Return >0 if the extent item is after @start_start + @search_len.
  1292. */
  1293. static int compare_extent_item_range(struct btrfs_path *path,
  1294. u64 search_start, u64 search_len)
  1295. {
  1296. struct btrfs_fs_info *fs_info = path->nodes[0]->fs_info;
  1297. u64 len;
  1298. struct btrfs_key key;
  1299. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1300. ASSERT(key.type == BTRFS_EXTENT_ITEM_KEY ||
  1301. key.type == BTRFS_METADATA_ITEM_KEY, "key.type=%u", key.type);
  1302. if (key.type == BTRFS_METADATA_ITEM_KEY)
  1303. len = fs_info->nodesize;
  1304. else
  1305. len = key.offset;
  1306. if (key.objectid + len <= search_start)
  1307. return -1;
  1308. if (key.objectid >= search_start + search_len)
  1309. return 1;
  1310. return 0;
  1311. }
  1312. /*
  1313. * Locate one extent item which covers any byte in range
  1314. * [@search_start, @search_start + @search_length)
  1315. *
  1316. * If the path is not initialized, we will initialize the search by doing
  1317. * a btrfs_search_slot().
  1318. * If the path is already initialized, we will use the path as the initial
  1319. * slot, to avoid duplicated btrfs_search_slot() calls.
  1320. *
  1321. * NOTE: If an extent item starts before @search_start, we will still
  1322. * return the extent item. This is for data extent crossing stripe boundary.
  1323. *
  1324. * Return 0 if we found such extent item, and @path will point to the extent item.
  1325. * Return >0 if no such extent item can be found, and @path will be released.
  1326. * Return <0 if hit fatal error, and @path will be released.
  1327. */
  1328. static int find_first_extent_item(struct btrfs_root *extent_root,
  1329. struct btrfs_path *path,
  1330. u64 search_start, u64 search_len)
  1331. {
  1332. struct btrfs_fs_info *fs_info = extent_root->fs_info;
  1333. struct btrfs_key key;
  1334. int ret;
  1335. /* Continue using the existing path */
  1336. if (path->nodes[0])
  1337. goto search_forward;
  1338. key.objectid = search_start;
  1339. if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
  1340. key.type = BTRFS_METADATA_ITEM_KEY;
  1341. else
  1342. key.type = BTRFS_EXTENT_ITEM_KEY;
  1343. key.offset = (u64)-1;
  1344. ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
  1345. if (ret < 0)
  1346. return ret;
  1347. if (unlikely(ret == 0)) {
  1348. /*
  1349. * Key with offset -1 found, there would have to exist an extent
  1350. * item with such offset, but this is out of the valid range.
  1351. */
  1352. btrfs_release_path(path);
  1353. return -EUCLEAN;
  1354. }
  1355. /*
  1356. * Here we intentionally pass 0 as @min_objectid, as there could be
  1357. * an extent item starting before @search_start.
  1358. */
  1359. ret = btrfs_previous_extent_item(extent_root, path, 0);
  1360. if (ret < 0)
  1361. return ret;
  1362. /*
  1363. * No matter whether we have found an extent item, the next loop will
  1364. * properly do every check on the key.
  1365. */
  1366. search_forward:
  1367. while (true) {
  1368. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1369. if (key.objectid >= search_start + search_len)
  1370. break;
  1371. if (key.type != BTRFS_METADATA_ITEM_KEY &&
  1372. key.type != BTRFS_EXTENT_ITEM_KEY)
  1373. goto next;
  1374. ret = compare_extent_item_range(path, search_start, search_len);
  1375. if (ret == 0)
  1376. return ret;
  1377. if (ret > 0)
  1378. break;
  1379. next:
  1380. ret = btrfs_next_item(extent_root, path);
  1381. if (ret) {
  1382. /* Either no more items or a fatal error. */
  1383. btrfs_release_path(path);
  1384. return ret;
  1385. }
  1386. }
  1387. btrfs_release_path(path);
  1388. return 1;
  1389. }
  1390. static void get_extent_info(struct btrfs_path *path, u64 *extent_start_ret,
  1391. u64 *size_ret, u64 *flags_ret, u64 *generation_ret)
  1392. {
  1393. struct btrfs_key key;
  1394. struct btrfs_extent_item *ei;
  1395. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1396. ASSERT(key.type == BTRFS_METADATA_ITEM_KEY ||
  1397. key.type == BTRFS_EXTENT_ITEM_KEY, "key.type=%u", key.type);
  1398. *extent_start_ret = key.objectid;
  1399. if (key.type == BTRFS_METADATA_ITEM_KEY)
  1400. *size_ret = path->nodes[0]->fs_info->nodesize;
  1401. else
  1402. *size_ret = key.offset;
  1403. ei = btrfs_item_ptr(path->nodes[0], path->slots[0], struct btrfs_extent_item);
  1404. *flags_ret = btrfs_extent_flags(path->nodes[0], ei);
  1405. *generation_ret = btrfs_extent_generation(path->nodes[0], ei);
  1406. }
  1407. static int sync_write_pointer_for_zoned(struct scrub_ctx *sctx, u64 logical,
  1408. u64 physical, u64 physical_end)
  1409. {
  1410. struct btrfs_fs_info *fs_info = sctx->fs_info;
  1411. int ret = 0;
  1412. if (!btrfs_is_zoned(fs_info))
  1413. return 0;
  1414. mutex_lock(&sctx->wr_lock);
  1415. if (sctx->write_pointer < physical_end) {
  1416. ret = btrfs_sync_zone_write_pointer(sctx->wr_tgtdev, logical,
  1417. physical,
  1418. sctx->write_pointer);
  1419. if (ret)
  1420. btrfs_err(fs_info, "scrub: zoned: failed to recover write pointer");
  1421. }
  1422. mutex_unlock(&sctx->wr_lock);
  1423. btrfs_dev_clear_zone_empty(sctx->wr_tgtdev, physical);
  1424. return ret;
  1425. }
  1426. static void fill_one_extent_info(struct btrfs_fs_info *fs_info,
  1427. struct scrub_stripe *stripe,
  1428. u64 extent_start, u64 extent_len,
  1429. u64 extent_flags, u64 extent_gen)
  1430. {
  1431. for (u64 cur_logical = max(stripe->logical, extent_start);
  1432. cur_logical < min(stripe->logical + BTRFS_STRIPE_LEN,
  1433. extent_start + extent_len);
  1434. cur_logical += fs_info->sectorsize) {
  1435. const int nr_sector = (cur_logical - stripe->logical) >>
  1436. fs_info->sectorsize_bits;
  1437. struct scrub_sector_verification *sector =
  1438. &stripe->sectors[nr_sector];
  1439. scrub_bitmap_set_bit_has_extent(stripe, nr_sector);
  1440. if (extent_flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  1441. scrub_bitmap_set_bit_is_metadata(stripe, nr_sector);
  1442. sector->generation = extent_gen;
  1443. }
  1444. }
  1445. }
  1446. static void scrub_stripe_reset_bitmaps(struct scrub_stripe *stripe)
  1447. {
  1448. ASSERT(stripe->nr_sectors);
  1449. bitmap_zero(stripe->bitmaps, scrub_bitmap_nr_last * stripe->nr_sectors);
  1450. }
  1451. /*
  1452. * Locate one stripe which has at least one extent in its range.
  1453. *
  1454. * Return 0 if found such stripe, and store its info into @stripe.
  1455. * Return >0 if there is no such stripe in the specified range.
  1456. * Return <0 for error.
  1457. */
  1458. static int scrub_find_fill_first_stripe(struct btrfs_block_group *bg,
  1459. struct btrfs_path *extent_path,
  1460. struct btrfs_path *csum_path,
  1461. struct btrfs_device *dev, u64 physical,
  1462. int mirror_num, u64 logical_start,
  1463. u32 logical_len,
  1464. struct scrub_stripe *stripe)
  1465. {
  1466. struct btrfs_fs_info *fs_info = bg->fs_info;
  1467. struct btrfs_root *extent_root = btrfs_extent_root(fs_info, bg->start);
  1468. struct btrfs_root *csum_root = btrfs_csum_root(fs_info, bg->start);
  1469. const u64 logical_end = logical_start + logical_len;
  1470. u64 cur_logical = logical_start;
  1471. u64 stripe_end;
  1472. u64 extent_start;
  1473. u64 extent_len;
  1474. u64 extent_flags;
  1475. u64 extent_gen;
  1476. int ret;
  1477. if (unlikely(!extent_root || !csum_root)) {
  1478. btrfs_err(fs_info, "scrub: no valid extent or csum root found");
  1479. return -EUCLEAN;
  1480. }
  1481. memset(stripe->sectors, 0, sizeof(struct scrub_sector_verification) *
  1482. stripe->nr_sectors);
  1483. scrub_stripe_reset_bitmaps(stripe);
  1484. /* The range must be inside the bg. */
  1485. ASSERT(logical_start >= bg->start && logical_end <= btrfs_block_group_end(bg),
  1486. "bg->start=%llu logical_start=%llu logical_end=%llu end=%llu",
  1487. bg->start, logical_start, logical_end, btrfs_block_group_end(bg));
  1488. ret = find_first_extent_item(extent_root, extent_path, logical_start,
  1489. logical_len);
  1490. /* Either error or not found. */
  1491. if (ret)
  1492. return ret;
  1493. get_extent_info(extent_path, &extent_start, &extent_len, &extent_flags,
  1494. &extent_gen);
  1495. if (extent_flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
  1496. stripe->nr_meta_extents++;
  1497. if (extent_flags & BTRFS_EXTENT_FLAG_DATA)
  1498. stripe->nr_data_extents++;
  1499. cur_logical = max(extent_start, cur_logical);
  1500. /*
  1501. * Round down to stripe boundary.
  1502. *
  1503. * The extra calculation against bg->start is to handle block groups
  1504. * whose logical bytenr is not BTRFS_STRIPE_LEN aligned.
  1505. */
  1506. stripe->logical = round_down(cur_logical - bg->start, BTRFS_STRIPE_LEN) +
  1507. bg->start;
  1508. stripe->physical = physical + stripe->logical - logical_start;
  1509. stripe->dev = dev;
  1510. stripe->bg = bg;
  1511. stripe->mirror_num = mirror_num;
  1512. stripe_end = stripe->logical + BTRFS_STRIPE_LEN - 1;
  1513. /* Fill the first extent info into stripe->sectors[] array. */
  1514. fill_one_extent_info(fs_info, stripe, extent_start, extent_len,
  1515. extent_flags, extent_gen);
  1516. cur_logical = extent_start + extent_len;
  1517. /* Fill the extent info for the remaining sectors. */
  1518. while (cur_logical <= stripe_end) {
  1519. ret = find_first_extent_item(extent_root, extent_path, cur_logical,
  1520. stripe_end - cur_logical + 1);
  1521. if (ret < 0)
  1522. return ret;
  1523. if (ret > 0) {
  1524. ret = 0;
  1525. break;
  1526. }
  1527. get_extent_info(extent_path, &extent_start, &extent_len,
  1528. &extent_flags, &extent_gen);
  1529. if (extent_flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
  1530. stripe->nr_meta_extents++;
  1531. if (extent_flags & BTRFS_EXTENT_FLAG_DATA)
  1532. stripe->nr_data_extents++;
  1533. fill_one_extent_info(fs_info, stripe, extent_start, extent_len,
  1534. extent_flags, extent_gen);
  1535. cur_logical = extent_start + extent_len;
  1536. }
  1537. /* Now fill the data csum. */
  1538. if (bg->flags & BTRFS_BLOCK_GROUP_DATA) {
  1539. int sector_nr;
  1540. unsigned long csum_bitmap = 0;
  1541. /* Csum space should have already been allocated. */
  1542. ASSERT(stripe->csums);
  1543. /*
  1544. * Our csum bitmap should be large enough, as BTRFS_STRIPE_LEN
  1545. * should contain at most 16 sectors.
  1546. */
  1547. ASSERT(BITS_PER_LONG >= BTRFS_STRIPE_LEN >> fs_info->sectorsize_bits);
  1548. ret = btrfs_lookup_csums_bitmap(csum_root, csum_path,
  1549. stripe->logical, stripe_end,
  1550. stripe->csums, &csum_bitmap);
  1551. if (ret < 0)
  1552. return ret;
  1553. if (ret > 0)
  1554. ret = 0;
  1555. for_each_set_bit(sector_nr, &csum_bitmap, stripe->nr_sectors) {
  1556. stripe->sectors[sector_nr].csum = stripe->csums +
  1557. sector_nr * fs_info->csum_size;
  1558. }
  1559. }
  1560. set_bit(SCRUB_STRIPE_FLAG_INITIALIZED, &stripe->state);
  1561. return ret;
  1562. }
  1563. static void scrub_reset_stripe(struct scrub_stripe *stripe)
  1564. {
  1565. scrub_stripe_reset_bitmaps(stripe);
  1566. stripe->nr_meta_extents = 0;
  1567. stripe->nr_data_extents = 0;
  1568. stripe->state = 0;
  1569. for (int i = 0; i < stripe->nr_sectors; i++) {
  1570. stripe->sectors[i].csum = NULL;
  1571. stripe->sectors[i].generation = 0;
  1572. }
  1573. }
  1574. static u32 stripe_length(const struct scrub_stripe *stripe)
  1575. {
  1576. ASSERT(stripe->bg);
  1577. return min(BTRFS_STRIPE_LEN,
  1578. stripe->bg->start + stripe->bg->length - stripe->logical);
  1579. }
  1580. static void scrub_submit_extent_sector_read(struct scrub_stripe *stripe)
  1581. {
  1582. struct btrfs_fs_info *fs_info = stripe->bg->fs_info;
  1583. struct btrfs_bio *bbio = NULL;
  1584. unsigned int nr_sectors = stripe_length(stripe) >> fs_info->sectorsize_bits;
  1585. const unsigned long has_extent = scrub_bitmap_read_has_extent(stripe);
  1586. u64 stripe_len = BTRFS_STRIPE_LEN;
  1587. int mirror = stripe->mirror_num;
  1588. int i;
  1589. atomic_inc(&stripe->pending_io);
  1590. for_each_set_bit(i, &has_extent, stripe->nr_sectors) {
  1591. /* We're beyond the chunk boundary, no need to read anymore. */
  1592. if (i >= nr_sectors)
  1593. break;
  1594. /* The current sector cannot be merged, submit the bio. */
  1595. if (bbio &&
  1596. ((i > 0 && !test_bit(i - 1, &has_extent)) ||
  1597. bbio->bio.bi_iter.bi_size >= stripe_len)) {
  1598. ASSERT(bbio->bio.bi_iter.bi_size);
  1599. atomic_inc(&stripe->pending_io);
  1600. btrfs_submit_bbio(bbio, mirror);
  1601. bbio = NULL;
  1602. }
  1603. if (!bbio) {
  1604. struct btrfs_io_stripe io_stripe = {};
  1605. struct btrfs_io_context *bioc = NULL;
  1606. const u64 logical = stripe->logical +
  1607. (i << fs_info->sectorsize_bits);
  1608. int ret;
  1609. io_stripe.rst_search_commit_root = true;
  1610. stripe_len = (nr_sectors - i) << fs_info->sectorsize_bits;
  1611. /*
  1612. * For RST cases, we need to manually split the bbio to
  1613. * follow the RST boundary.
  1614. */
  1615. ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, logical,
  1616. &stripe_len, &bioc, &io_stripe, &mirror);
  1617. btrfs_put_bioc(bioc);
  1618. if (ret < 0) {
  1619. if (ret != -ENODATA) {
  1620. /*
  1621. * Earlier btrfs_get_raid_extent_offset()
  1622. * returned -ENODATA, which means there's
  1623. * no entry for the corresponding range
  1624. * in the stripe tree. But if it's in
  1625. * the extent tree, then it's a preallocated
  1626. * extent and not an error.
  1627. */
  1628. scrub_bitmap_set_bit_io_error(stripe, i);
  1629. scrub_bitmap_set_bit_error(stripe, i);
  1630. }
  1631. continue;
  1632. }
  1633. bbio = alloc_scrub_bbio(fs_info, stripe->nr_sectors, REQ_OP_READ,
  1634. logical, scrub_read_endio, stripe);
  1635. }
  1636. scrub_bio_add_sector(bbio, stripe, i);
  1637. }
  1638. if (bbio) {
  1639. ASSERT(bbio->bio.bi_iter.bi_size);
  1640. atomic_inc(&stripe->pending_io);
  1641. btrfs_submit_bbio(bbio, mirror);
  1642. }
  1643. if (atomic_dec_and_test(&stripe->pending_io)) {
  1644. wake_up(&stripe->io_wait);
  1645. INIT_WORK(&stripe->work, scrub_stripe_read_repair_worker);
  1646. queue_work(stripe->bg->fs_info->scrub_workers, &stripe->work);
  1647. }
  1648. }
  1649. static void scrub_submit_initial_read(struct scrub_ctx *sctx,
  1650. struct scrub_stripe *stripe)
  1651. {
  1652. struct btrfs_fs_info *fs_info = sctx->fs_info;
  1653. struct btrfs_bio *bbio;
  1654. const u32 min_folio_shift = PAGE_SHIFT + fs_info->block_min_order;
  1655. unsigned int nr_sectors = stripe_length(stripe) >> fs_info->sectorsize_bits;
  1656. int mirror = stripe->mirror_num;
  1657. ASSERT(stripe->bg);
  1658. ASSERT(stripe->mirror_num > 0);
  1659. ASSERT(test_bit(SCRUB_STRIPE_FLAG_INITIALIZED, &stripe->state));
  1660. if (btrfs_need_stripe_tree_update(fs_info, stripe->bg->flags)) {
  1661. scrub_submit_extent_sector_read(stripe);
  1662. return;
  1663. }
  1664. bbio = alloc_scrub_bbio(fs_info, BTRFS_STRIPE_LEN >> min_folio_shift, REQ_OP_READ,
  1665. stripe->logical, scrub_read_endio, stripe);
  1666. /* Read the whole range inside the chunk boundary. */
  1667. for (unsigned int cur = 0; cur < nr_sectors; cur++)
  1668. scrub_bio_add_sector(bbio, stripe, cur);
  1669. atomic_inc(&stripe->pending_io);
  1670. /*
  1671. * For dev-replace, either user asks to avoid the source dev, or
  1672. * the device is missing, we try the next mirror instead.
  1673. */
  1674. if (sctx->is_dev_replace &&
  1675. (fs_info->dev_replace.cont_reading_from_srcdev_mode ==
  1676. BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID ||
  1677. !stripe->dev->bdev)) {
  1678. int num_copies = btrfs_num_copies(fs_info, stripe->bg->start,
  1679. stripe->bg->length);
  1680. mirror = calc_next_mirror(mirror, num_copies);
  1681. }
  1682. btrfs_submit_bbio(bbio, mirror);
  1683. }
  1684. static bool stripe_has_metadata_error(struct scrub_stripe *stripe)
  1685. {
  1686. const unsigned long error = scrub_bitmap_read_error(stripe);
  1687. int i;
  1688. for_each_set_bit(i, &error, stripe->nr_sectors) {
  1689. if (scrub_bitmap_test_bit_is_metadata(stripe, i)) {
  1690. struct btrfs_fs_info *fs_info = stripe->bg->fs_info;
  1691. btrfs_err(fs_info,
  1692. "scrub: stripe %llu has unrepaired metadata sector at logical %llu",
  1693. stripe->logical,
  1694. stripe->logical + (i << fs_info->sectorsize_bits));
  1695. return true;
  1696. }
  1697. }
  1698. return false;
  1699. }
  1700. static void submit_initial_group_read(struct scrub_ctx *sctx,
  1701. unsigned int first_slot,
  1702. unsigned int nr_stripes)
  1703. {
  1704. struct blk_plug plug;
  1705. ASSERT(first_slot < SCRUB_TOTAL_STRIPES);
  1706. ASSERT(first_slot + nr_stripes <= SCRUB_TOTAL_STRIPES);
  1707. scrub_throttle_dev_io(sctx, sctx->stripes[0].dev,
  1708. btrfs_stripe_nr_to_offset(nr_stripes));
  1709. blk_start_plug(&plug);
  1710. for (int i = 0; i < nr_stripes; i++) {
  1711. struct scrub_stripe *stripe = &sctx->stripes[first_slot + i];
  1712. /* Those stripes should be initialized. */
  1713. ASSERT(test_bit(SCRUB_STRIPE_FLAG_INITIALIZED, &stripe->state));
  1714. scrub_submit_initial_read(sctx, stripe);
  1715. }
  1716. blk_finish_plug(&plug);
  1717. }
  1718. static int flush_scrub_stripes(struct scrub_ctx *sctx)
  1719. {
  1720. struct btrfs_fs_info *fs_info = sctx->fs_info;
  1721. struct scrub_stripe *stripe;
  1722. const int nr_stripes = sctx->cur_stripe;
  1723. int ret = 0;
  1724. if (!nr_stripes)
  1725. return 0;
  1726. ASSERT(test_bit(SCRUB_STRIPE_FLAG_INITIALIZED, &sctx->stripes[0].state));
  1727. /* Submit the stripes which are populated but not submitted. */
  1728. if (nr_stripes % SCRUB_STRIPES_PER_GROUP) {
  1729. const int first_slot = round_down(nr_stripes, SCRUB_STRIPES_PER_GROUP);
  1730. submit_initial_group_read(sctx, first_slot, nr_stripes - first_slot);
  1731. }
  1732. for (int i = 0; i < nr_stripes; i++) {
  1733. stripe = &sctx->stripes[i];
  1734. wait_event(stripe->repair_wait,
  1735. test_bit(SCRUB_STRIPE_FLAG_REPAIR_DONE, &stripe->state));
  1736. }
  1737. /* Submit for dev-replace. */
  1738. if (sctx->is_dev_replace) {
  1739. /*
  1740. * For dev-replace, if we know there is something wrong with
  1741. * metadata, we should immediately abort.
  1742. */
  1743. for (int i = 0; i < nr_stripes; i++) {
  1744. if (unlikely(stripe_has_metadata_error(&sctx->stripes[i]))) {
  1745. ret = -EIO;
  1746. goto out;
  1747. }
  1748. }
  1749. for (int i = 0; i < nr_stripes; i++) {
  1750. unsigned long good;
  1751. unsigned long has_extent;
  1752. unsigned long error;
  1753. stripe = &sctx->stripes[i];
  1754. ASSERT(stripe->dev == fs_info->dev_replace.srcdev);
  1755. has_extent = scrub_bitmap_read_has_extent(stripe);
  1756. error = scrub_bitmap_read_error(stripe);
  1757. bitmap_andnot(&good, &has_extent, &error, stripe->nr_sectors);
  1758. scrub_write_sectors(sctx, stripe, good, true);
  1759. }
  1760. }
  1761. /* Wait for the above writebacks to finish. */
  1762. for (int i = 0; i < nr_stripes; i++) {
  1763. stripe = &sctx->stripes[i];
  1764. wait_scrub_stripe_io(stripe);
  1765. spin_lock(&sctx->stat_lock);
  1766. sctx->stat.last_physical = stripe->physical + stripe_length(stripe);
  1767. spin_unlock(&sctx->stat_lock);
  1768. scrub_reset_stripe(stripe);
  1769. }
  1770. out:
  1771. sctx->cur_stripe = 0;
  1772. return ret;
  1773. }
  1774. static void raid56_scrub_wait_endio(struct bio *bio)
  1775. {
  1776. complete(bio->bi_private);
  1777. }
  1778. static int queue_scrub_stripe(struct scrub_ctx *sctx, struct btrfs_block_group *bg,
  1779. struct btrfs_device *dev, int mirror_num,
  1780. u64 logical, u32 length, u64 physical,
  1781. u64 *found_logical_ret)
  1782. {
  1783. struct scrub_stripe *stripe;
  1784. int ret;
  1785. /*
  1786. * There should always be one slot left, as caller filling the last
  1787. * slot should flush them all.
  1788. */
  1789. ASSERT(sctx->cur_stripe < SCRUB_TOTAL_STRIPES);
  1790. /* @found_logical_ret must be specified. */
  1791. ASSERT(found_logical_ret);
  1792. stripe = &sctx->stripes[sctx->cur_stripe];
  1793. scrub_reset_stripe(stripe);
  1794. ret = scrub_find_fill_first_stripe(bg, &sctx->extent_path,
  1795. &sctx->csum_path, dev, physical,
  1796. mirror_num, logical, length, stripe);
  1797. /* Either >0 as no more extents or <0 for error. */
  1798. if (ret)
  1799. return ret;
  1800. *found_logical_ret = stripe->logical;
  1801. sctx->cur_stripe++;
  1802. /* We filled one group, submit it. */
  1803. if (sctx->cur_stripe % SCRUB_STRIPES_PER_GROUP == 0) {
  1804. const int first_slot = sctx->cur_stripe - SCRUB_STRIPES_PER_GROUP;
  1805. submit_initial_group_read(sctx, first_slot, SCRUB_STRIPES_PER_GROUP);
  1806. }
  1807. /* Last slot used, flush them all. */
  1808. if (sctx->cur_stripe == SCRUB_TOTAL_STRIPES)
  1809. return flush_scrub_stripes(sctx);
  1810. return 0;
  1811. }
  1812. /*
  1813. * Return 0 if we should not cancel the scrub.
  1814. * Return <0 if we need to cancel the scrub, returned value will
  1815. * indicate the reason:
  1816. * - -ECANCELED - Being explicitly canceled through ioctl.
  1817. * - -EINTR - Being interrupted by signal or fs/process freezing.
  1818. */
  1819. static int should_cancel_scrub(const struct scrub_ctx *sctx)
  1820. {
  1821. struct btrfs_fs_info *fs_info = sctx->fs_info;
  1822. if (atomic_read(&fs_info->scrub_cancel_req) ||
  1823. atomic_read(&sctx->cancel_req))
  1824. return -ECANCELED;
  1825. /*
  1826. * The user (e.g. fsfreeze command) or power management (PM)
  1827. * suspend/hibernate can freeze the fs. And PM suspend/hibernate will
  1828. * also freeze all user processes.
  1829. *
  1830. * A user process can only be frozen when it is in user space, thus we
  1831. * have to cancel the run so that the process can return to the user
  1832. * space.
  1833. *
  1834. * Furthermore we have to check both filesystem and process freezing,
  1835. * as PM can be configured to freeze the filesystems before processes.
  1836. *
  1837. * If we only check fs freezing, then suspend without fs freezing
  1838. * will timeout, as the process is still in kernel space.
  1839. *
  1840. * If we only check process freezing, then suspend with fs freezing
  1841. * will timeout, as the running scrub will prevent the fs from being frozen.
  1842. */
  1843. if (fs_info->sb->s_writers.frozen > SB_UNFROZEN ||
  1844. freezing(current) || signal_pending(current))
  1845. return -EINTR;
  1846. return 0;
  1847. }
  1848. static int scrub_raid56_cached_parity(struct scrub_ctx *sctx,
  1849. struct btrfs_device *scrub_dev,
  1850. struct btrfs_chunk_map *map,
  1851. u64 full_stripe_start,
  1852. unsigned long *extent_bitmap)
  1853. {
  1854. DECLARE_COMPLETION_ONSTACK(io_done);
  1855. struct btrfs_fs_info *fs_info = sctx->fs_info;
  1856. struct btrfs_io_context *bioc = NULL;
  1857. struct btrfs_raid_bio *rbio;
  1858. struct bio bio;
  1859. const int data_stripes = nr_data_stripes(map);
  1860. u64 length = btrfs_stripe_nr_to_offset(data_stripes);
  1861. int ret;
  1862. bio_init(&bio, NULL, NULL, 0, REQ_OP_READ);
  1863. bio.bi_iter.bi_sector = full_stripe_start >> SECTOR_SHIFT;
  1864. bio.bi_private = &io_done;
  1865. bio.bi_end_io = raid56_scrub_wait_endio;
  1866. btrfs_bio_counter_inc_blocked(fs_info);
  1867. ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, full_stripe_start,
  1868. &length, &bioc, NULL, NULL);
  1869. if (ret < 0)
  1870. goto out;
  1871. /* For RAID56 write there must be an @bioc allocated. */
  1872. ASSERT(bioc);
  1873. rbio = raid56_parity_alloc_scrub_rbio(&bio, bioc, scrub_dev, extent_bitmap,
  1874. BTRFS_STRIPE_LEN >> fs_info->sectorsize_bits);
  1875. btrfs_put_bioc(bioc);
  1876. if (!rbio) {
  1877. ret = -ENOMEM;
  1878. goto out;
  1879. }
  1880. /* Use the recovered stripes as cache to avoid read them from disk again. */
  1881. for (int i = 0; i < data_stripes; i++) {
  1882. struct scrub_stripe *stripe = &sctx->raid56_data_stripes[i];
  1883. raid56_parity_cache_data_folios(rbio, stripe->folios,
  1884. full_stripe_start + (i << BTRFS_STRIPE_LEN_SHIFT));
  1885. }
  1886. raid56_parity_submit_scrub_rbio(rbio);
  1887. wait_for_completion_io(&io_done);
  1888. ret = blk_status_to_errno(bio.bi_status);
  1889. out:
  1890. btrfs_bio_counter_dec(fs_info);
  1891. bio_uninit(&bio);
  1892. return ret;
  1893. }
  1894. static int scrub_raid56_parity_stripe(struct scrub_ctx *sctx,
  1895. struct btrfs_device *scrub_dev,
  1896. struct btrfs_block_group *bg,
  1897. struct btrfs_chunk_map *map,
  1898. u64 full_stripe_start)
  1899. {
  1900. struct btrfs_fs_info *fs_info = sctx->fs_info;
  1901. BTRFS_PATH_AUTO_RELEASE(extent_path);
  1902. BTRFS_PATH_AUTO_RELEASE(csum_path);
  1903. struct scrub_stripe *stripe;
  1904. bool all_empty = true;
  1905. const int data_stripes = nr_data_stripes(map);
  1906. unsigned long extent_bitmap = 0;
  1907. int ret;
  1908. ASSERT(sctx->raid56_data_stripes);
  1909. ret = should_cancel_scrub(sctx);
  1910. if (ret < 0)
  1911. return ret;
  1912. if (atomic_read(&fs_info->scrub_pause_req))
  1913. scrub_blocked_if_needed(fs_info);
  1914. spin_lock(&bg->lock);
  1915. if (test_bit(BLOCK_GROUP_FLAG_REMOVED, &bg->runtime_flags)) {
  1916. spin_unlock(&bg->lock);
  1917. return 0;
  1918. }
  1919. spin_unlock(&bg->lock);
  1920. /*
  1921. * For data stripe search, we cannot reuse the same extent/csum paths,
  1922. * as the data stripe bytenr may be smaller than previous extent. Thus
  1923. * we have to use our own extent/csum paths.
  1924. */
  1925. extent_path.search_commit_root = true;
  1926. extent_path.skip_locking = true;
  1927. csum_path.search_commit_root = true;
  1928. csum_path.skip_locking = true;
  1929. for (int i = 0; i < data_stripes; i++) {
  1930. int stripe_index;
  1931. int rot;
  1932. u64 physical;
  1933. stripe = &sctx->raid56_data_stripes[i];
  1934. rot = div_u64(full_stripe_start - bg->start,
  1935. data_stripes) >> BTRFS_STRIPE_LEN_SHIFT;
  1936. stripe_index = (i + rot) % map->num_stripes;
  1937. physical = map->stripes[stripe_index].physical +
  1938. btrfs_stripe_nr_to_offset(rot);
  1939. scrub_reset_stripe(stripe);
  1940. set_bit(SCRUB_STRIPE_FLAG_NO_REPORT, &stripe->state);
  1941. ret = scrub_find_fill_first_stripe(bg, &extent_path, &csum_path,
  1942. map->stripes[stripe_index].dev, physical, 1,
  1943. full_stripe_start + btrfs_stripe_nr_to_offset(i),
  1944. BTRFS_STRIPE_LEN, stripe);
  1945. if (ret < 0)
  1946. return ret;
  1947. /*
  1948. * No extent in this data stripe, need to manually mark them
  1949. * initialized to make later read submission happy.
  1950. */
  1951. if (ret > 0) {
  1952. stripe->logical = full_stripe_start +
  1953. btrfs_stripe_nr_to_offset(i);
  1954. stripe->dev = map->stripes[stripe_index].dev;
  1955. stripe->mirror_num = 1;
  1956. set_bit(SCRUB_STRIPE_FLAG_INITIALIZED, &stripe->state);
  1957. }
  1958. }
  1959. /* Check if all data stripes are empty. */
  1960. for (int i = 0; i < data_stripes; i++) {
  1961. stripe = &sctx->raid56_data_stripes[i];
  1962. if (!scrub_bitmap_empty_has_extent(stripe)) {
  1963. all_empty = false;
  1964. break;
  1965. }
  1966. }
  1967. if (all_empty)
  1968. return 0;
  1969. for (int i = 0; i < data_stripes; i++) {
  1970. stripe = &sctx->raid56_data_stripes[i];
  1971. scrub_submit_initial_read(sctx, stripe);
  1972. }
  1973. for (int i = 0; i < data_stripes; i++) {
  1974. stripe = &sctx->raid56_data_stripes[i];
  1975. wait_event(stripe->repair_wait,
  1976. test_bit(SCRUB_STRIPE_FLAG_REPAIR_DONE, &stripe->state));
  1977. }
  1978. /* For now, no zoned support for RAID56. */
  1979. ASSERT(!btrfs_is_zoned(sctx->fs_info));
  1980. /*
  1981. * Now all data stripes are properly verified. Check if we have any
  1982. * unrepaired, if so abort immediately or we could further corrupt the
  1983. * P/Q stripes.
  1984. *
  1985. * During the loop, also populate extent_bitmap.
  1986. */
  1987. for (int i = 0; i < data_stripes; i++) {
  1988. unsigned long error;
  1989. unsigned long has_extent;
  1990. stripe = &sctx->raid56_data_stripes[i];
  1991. error = scrub_bitmap_read_error(stripe);
  1992. has_extent = scrub_bitmap_read_has_extent(stripe);
  1993. /*
  1994. * We should only check the errors where there is an extent.
  1995. * As we may hit an empty data stripe while it's missing.
  1996. */
  1997. bitmap_and(&error, &error, &has_extent, stripe->nr_sectors);
  1998. if (unlikely(!bitmap_empty(&error, stripe->nr_sectors))) {
  1999. btrfs_err(fs_info,
  2000. "scrub: unrepaired sectors detected, full stripe %llu data stripe %u errors %*pbl",
  2001. full_stripe_start, i, stripe->nr_sectors,
  2002. &error);
  2003. return ret;
  2004. }
  2005. bitmap_or(&extent_bitmap, &extent_bitmap, &has_extent,
  2006. stripe->nr_sectors);
  2007. }
  2008. /* Now we can check and regenerate the P/Q stripe. */
  2009. return scrub_raid56_cached_parity(sctx, scrub_dev, map, full_stripe_start,
  2010. &extent_bitmap);
  2011. }
  2012. /*
  2013. * Scrub one range which can only has simple mirror based profile.
  2014. * (Including all range in SINGLE/DUP/RAID1/RAID1C*, and each stripe in
  2015. * RAID0/RAID10).
  2016. *
  2017. * Since we may need to handle a subset of block group, we need @logical_start
  2018. * and @logical_length parameter.
  2019. */
  2020. static int scrub_simple_mirror(struct scrub_ctx *sctx,
  2021. struct btrfs_block_group *bg,
  2022. u64 logical_start, u64 logical_length,
  2023. struct btrfs_device *device,
  2024. u64 physical, int mirror_num)
  2025. {
  2026. struct btrfs_fs_info *fs_info = sctx->fs_info;
  2027. const u64 logical_end = logical_start + logical_length;
  2028. u64 cur_logical = logical_start;
  2029. int ret = 0;
  2030. /* The range must be inside the bg */
  2031. ASSERT(logical_start >= bg->start && logical_end <= btrfs_block_group_end(bg));
  2032. /* Go through each extent items inside the logical range */
  2033. while (cur_logical < logical_end) {
  2034. u64 found_logical = U64_MAX;
  2035. u64 cur_physical = physical + cur_logical - logical_start;
  2036. ret = should_cancel_scrub(sctx);
  2037. if (ret < 0)
  2038. break;
  2039. if (atomic_read(&fs_info->scrub_pause_req))
  2040. scrub_blocked_if_needed(fs_info);
  2041. spin_lock(&bg->lock);
  2042. if (test_bit(BLOCK_GROUP_FLAG_REMOVED, &bg->runtime_flags)) {
  2043. spin_unlock(&bg->lock);
  2044. ret = 0;
  2045. break;
  2046. }
  2047. spin_unlock(&bg->lock);
  2048. ret = queue_scrub_stripe(sctx, bg, device, mirror_num,
  2049. cur_logical, logical_end - cur_logical,
  2050. cur_physical, &found_logical);
  2051. if (ret > 0) {
  2052. /* No more extent, just update the accounting */
  2053. spin_lock(&sctx->stat_lock);
  2054. sctx->stat.last_physical = physical + logical_length;
  2055. spin_unlock(&sctx->stat_lock);
  2056. ret = 0;
  2057. break;
  2058. }
  2059. if (ret < 0)
  2060. break;
  2061. /* queue_scrub_stripe() returned 0, @found_logical must be updated. */
  2062. ASSERT(found_logical != U64_MAX);
  2063. cur_logical = found_logical + BTRFS_STRIPE_LEN;
  2064. /* Don't hold CPU for too long time */
  2065. cond_resched();
  2066. }
  2067. return ret;
  2068. }
  2069. /* Calculate the full stripe length for simple stripe based profiles */
  2070. static u64 simple_stripe_full_stripe_len(const struct btrfs_chunk_map *map)
  2071. {
  2072. ASSERT(map->type & (BTRFS_BLOCK_GROUP_RAID0 |
  2073. BTRFS_BLOCK_GROUP_RAID10));
  2074. return btrfs_stripe_nr_to_offset(map->num_stripes / map->sub_stripes);
  2075. }
  2076. /* Get the logical bytenr for the stripe */
  2077. static u64 simple_stripe_get_logical(struct btrfs_chunk_map *map,
  2078. struct btrfs_block_group *bg,
  2079. int stripe_index)
  2080. {
  2081. ASSERT(map->type & (BTRFS_BLOCK_GROUP_RAID0 |
  2082. BTRFS_BLOCK_GROUP_RAID10));
  2083. ASSERT(stripe_index < map->num_stripes);
  2084. /*
  2085. * (stripe_index / sub_stripes) gives how many data stripes we need to
  2086. * skip.
  2087. */
  2088. return btrfs_stripe_nr_to_offset(stripe_index / map->sub_stripes) +
  2089. bg->start;
  2090. }
  2091. /* Get the mirror number for the stripe */
  2092. static int simple_stripe_mirror_num(struct btrfs_chunk_map *map, int stripe_index)
  2093. {
  2094. ASSERT(map->type & (BTRFS_BLOCK_GROUP_RAID0 |
  2095. BTRFS_BLOCK_GROUP_RAID10));
  2096. ASSERT(stripe_index < map->num_stripes);
  2097. /* For RAID0, it's fixed to 1, for RAID10 it's 0,1,0,1... */
  2098. return stripe_index % map->sub_stripes + 1;
  2099. }
  2100. static int scrub_simple_stripe(struct scrub_ctx *sctx,
  2101. struct btrfs_block_group *bg,
  2102. struct btrfs_chunk_map *map,
  2103. struct btrfs_device *device,
  2104. int stripe_index)
  2105. {
  2106. const u64 logical_increment = simple_stripe_full_stripe_len(map);
  2107. const u64 orig_logical = simple_stripe_get_logical(map, bg, stripe_index);
  2108. const u64 orig_physical = map->stripes[stripe_index].physical;
  2109. const u64 end = btrfs_block_group_end(bg);
  2110. const int mirror_num = simple_stripe_mirror_num(map, stripe_index);
  2111. u64 cur_logical = orig_logical;
  2112. u64 cur_physical = orig_physical;
  2113. int ret = 0;
  2114. while (cur_logical < end) {
  2115. /*
  2116. * Inside each stripe, RAID0 is just SINGLE, and RAID10 is
  2117. * just RAID1, so we can reuse scrub_simple_mirror() to scrub
  2118. * this stripe.
  2119. */
  2120. ret = scrub_simple_mirror(sctx, bg, cur_logical,
  2121. BTRFS_STRIPE_LEN, device, cur_physical,
  2122. mirror_num);
  2123. if (ret)
  2124. return ret;
  2125. /* Skip to next stripe which belongs to the target device */
  2126. cur_logical += logical_increment;
  2127. /* For physical offset, we just go to next stripe */
  2128. cur_physical += BTRFS_STRIPE_LEN;
  2129. }
  2130. return ret;
  2131. }
  2132. static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
  2133. struct btrfs_block_group *bg,
  2134. struct btrfs_chunk_map *map,
  2135. struct btrfs_device *scrub_dev,
  2136. int stripe_index)
  2137. {
  2138. struct btrfs_fs_info *fs_info = sctx->fs_info;
  2139. const u64 profile = map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK;
  2140. const u64 chunk_logical = bg->start;
  2141. int ret;
  2142. int ret2;
  2143. u64 physical = map->stripes[stripe_index].physical;
  2144. const u64 dev_stripe_len = btrfs_calc_stripe_length(map);
  2145. const u64 physical_end = physical + dev_stripe_len;
  2146. u64 logical;
  2147. u64 logic_end;
  2148. /* The logical increment after finishing one stripe */
  2149. u64 increment;
  2150. /* Offset inside the chunk */
  2151. u64 offset;
  2152. u64 stripe_logical;
  2153. /* Extent_path should be released by now. */
  2154. ASSERT(sctx->extent_path.nodes[0] == NULL);
  2155. scrub_blocked_if_needed(fs_info);
  2156. if (sctx->is_dev_replace &&
  2157. btrfs_dev_is_sequential(sctx->wr_tgtdev, physical)) {
  2158. mutex_lock(&sctx->wr_lock);
  2159. sctx->write_pointer = physical;
  2160. mutex_unlock(&sctx->wr_lock);
  2161. }
  2162. /* Prepare the extra data stripes used by RAID56. */
  2163. if (profile & BTRFS_BLOCK_GROUP_RAID56_MASK) {
  2164. ASSERT(sctx->raid56_data_stripes == NULL);
  2165. sctx->raid56_data_stripes = kzalloc_objs(struct scrub_stripe,
  2166. nr_data_stripes(map));
  2167. if (!sctx->raid56_data_stripes) {
  2168. ret = -ENOMEM;
  2169. goto out;
  2170. }
  2171. for (int i = 0; i < nr_data_stripes(map); i++) {
  2172. ret = init_scrub_stripe(fs_info,
  2173. &sctx->raid56_data_stripes[i]);
  2174. if (ret < 0)
  2175. goto out;
  2176. sctx->raid56_data_stripes[i].bg = bg;
  2177. sctx->raid56_data_stripes[i].sctx = sctx;
  2178. }
  2179. }
  2180. /*
  2181. * There used to be a big double loop to handle all profiles using the
  2182. * same routine, which grows larger and more gross over time.
  2183. *
  2184. * So here we handle each profile differently, so simpler profiles
  2185. * have simpler scrubbing function.
  2186. */
  2187. if (!(profile & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10 |
  2188. BTRFS_BLOCK_GROUP_RAID56_MASK))) {
  2189. /*
  2190. * Above check rules out all complex profile, the remaining
  2191. * profiles are SINGLE|DUP|RAID1|RAID1C*, which is simple
  2192. * mirrored duplication without stripe.
  2193. *
  2194. * Only @physical and @mirror_num needs to calculated using
  2195. * @stripe_index.
  2196. */
  2197. ret = scrub_simple_mirror(sctx, bg, bg->start, bg->length,
  2198. scrub_dev, map->stripes[stripe_index].physical,
  2199. stripe_index + 1);
  2200. offset = 0;
  2201. goto out;
  2202. }
  2203. if (profile & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
  2204. ret = scrub_simple_stripe(sctx, bg, map, scrub_dev, stripe_index);
  2205. offset = btrfs_stripe_nr_to_offset(stripe_index / map->sub_stripes);
  2206. goto out;
  2207. }
  2208. /* Only RAID56 goes through the old code */
  2209. ASSERT(map->type & BTRFS_BLOCK_GROUP_RAID56_MASK);
  2210. ret = 0;
  2211. /* Calculate the logical end of the stripe */
  2212. get_raid56_logic_offset(physical_end, stripe_index,
  2213. map, &logic_end, NULL);
  2214. logic_end += chunk_logical;
  2215. /* Initialize @offset in case we need to go to out: label */
  2216. get_raid56_logic_offset(physical, stripe_index, map, &offset, NULL);
  2217. increment = btrfs_stripe_nr_to_offset(nr_data_stripes(map));
  2218. /*
  2219. * Due to the rotation, for RAID56 it's better to iterate each stripe
  2220. * using their physical offset.
  2221. */
  2222. while (physical < physical_end) {
  2223. ret = get_raid56_logic_offset(physical, stripe_index, map,
  2224. &logical, &stripe_logical);
  2225. logical += chunk_logical;
  2226. if (ret) {
  2227. /* it is parity strip */
  2228. stripe_logical += chunk_logical;
  2229. ret = scrub_raid56_parity_stripe(sctx, scrub_dev, bg,
  2230. map, stripe_logical);
  2231. spin_lock(&sctx->stat_lock);
  2232. sctx->stat.last_physical = min(physical + BTRFS_STRIPE_LEN,
  2233. physical_end);
  2234. spin_unlock(&sctx->stat_lock);
  2235. if (ret)
  2236. goto out;
  2237. goto next;
  2238. }
  2239. /*
  2240. * Now we're at a data stripe, scrub each extents in the range.
  2241. *
  2242. * At this stage, if we ignore the repair part, inside each data
  2243. * stripe it is no different than SINGLE profile.
  2244. * We can reuse scrub_simple_mirror() here, as the repair part
  2245. * is still based on @mirror_num.
  2246. */
  2247. ret = scrub_simple_mirror(sctx, bg, logical, BTRFS_STRIPE_LEN,
  2248. scrub_dev, physical, 1);
  2249. if (ret < 0)
  2250. goto out;
  2251. next:
  2252. logical += increment;
  2253. physical += BTRFS_STRIPE_LEN;
  2254. spin_lock(&sctx->stat_lock);
  2255. sctx->stat.last_physical = physical;
  2256. spin_unlock(&sctx->stat_lock);
  2257. }
  2258. out:
  2259. ret2 = flush_scrub_stripes(sctx);
  2260. if (!ret)
  2261. ret = ret2;
  2262. btrfs_release_path(&sctx->extent_path);
  2263. btrfs_release_path(&sctx->csum_path);
  2264. if (sctx->raid56_data_stripes) {
  2265. for (int i = 0; i < nr_data_stripes(map); i++)
  2266. release_scrub_stripe(&sctx->raid56_data_stripes[i]);
  2267. kfree(sctx->raid56_data_stripes);
  2268. sctx->raid56_data_stripes = NULL;
  2269. }
  2270. if (sctx->is_dev_replace && ret >= 0) {
  2271. ret2 = sync_write_pointer_for_zoned(sctx,
  2272. chunk_logical + offset,
  2273. map->stripes[stripe_index].physical,
  2274. physical_end);
  2275. if (ret2)
  2276. ret = ret2;
  2277. }
  2278. return ret < 0 ? ret : 0;
  2279. }
  2280. static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
  2281. struct btrfs_block_group *bg,
  2282. struct btrfs_device *scrub_dev,
  2283. u64 dev_offset,
  2284. u64 dev_extent_len)
  2285. {
  2286. struct btrfs_fs_info *fs_info = sctx->fs_info;
  2287. struct btrfs_chunk_map *map;
  2288. int i;
  2289. int ret = 0;
  2290. map = btrfs_find_chunk_map(fs_info, bg->start, bg->length);
  2291. if (!map) {
  2292. /*
  2293. * Might have been an unused block group deleted by the cleaner
  2294. * kthread or relocation.
  2295. */
  2296. spin_lock(&bg->lock);
  2297. if (!test_bit(BLOCK_GROUP_FLAG_REMOVED, &bg->runtime_flags))
  2298. ret = -EINVAL;
  2299. spin_unlock(&bg->lock);
  2300. return ret;
  2301. }
  2302. if (map->start != bg->start)
  2303. goto out;
  2304. if (map->chunk_len < dev_extent_len)
  2305. goto out;
  2306. for (i = 0; i < map->num_stripes; ++i) {
  2307. if (map->stripes[i].dev->bdev == scrub_dev->bdev &&
  2308. map->stripes[i].physical == dev_offset) {
  2309. ret = scrub_stripe(sctx, bg, map, scrub_dev, i);
  2310. if (ret)
  2311. goto out;
  2312. }
  2313. }
  2314. out:
  2315. btrfs_free_chunk_map(map);
  2316. return ret;
  2317. }
  2318. static int finish_extent_writes_for_zoned(struct btrfs_root *root,
  2319. struct btrfs_block_group *cache)
  2320. {
  2321. struct btrfs_fs_info *fs_info = cache->fs_info;
  2322. if (!btrfs_is_zoned(fs_info))
  2323. return 0;
  2324. btrfs_wait_block_group_reservations(cache);
  2325. btrfs_wait_nocow_writers(cache);
  2326. btrfs_wait_ordered_roots(fs_info, U64_MAX, cache);
  2327. return btrfs_commit_current_transaction(root);
  2328. }
  2329. static noinline_for_stack
  2330. int scrub_enumerate_chunks(struct scrub_ctx *sctx,
  2331. struct btrfs_device *scrub_dev, u64 start, u64 end)
  2332. {
  2333. struct btrfs_dev_extent *dev_extent = NULL;
  2334. BTRFS_PATH_AUTO_FREE(path);
  2335. struct btrfs_fs_info *fs_info = sctx->fs_info;
  2336. struct btrfs_root *root = fs_info->dev_root;
  2337. u64 chunk_offset;
  2338. int ret = 0;
  2339. int ro_set;
  2340. int slot;
  2341. struct extent_buffer *l;
  2342. struct btrfs_key key;
  2343. struct btrfs_key found_key;
  2344. struct btrfs_block_group *cache;
  2345. struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
  2346. path = btrfs_alloc_path();
  2347. if (!path)
  2348. return -ENOMEM;
  2349. path->reada = READA_FORWARD;
  2350. path->search_commit_root = true;
  2351. path->skip_locking = true;
  2352. key.objectid = scrub_dev->devid;
  2353. key.type = BTRFS_DEV_EXTENT_KEY;
  2354. key.offset = 0ull;
  2355. while (1) {
  2356. u64 dev_extent_len;
  2357. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2358. if (ret < 0)
  2359. break;
  2360. if (ret > 0) {
  2361. if (path->slots[0] >=
  2362. btrfs_header_nritems(path->nodes[0])) {
  2363. ret = btrfs_next_leaf(root, path);
  2364. if (ret < 0)
  2365. break;
  2366. if (ret > 0) {
  2367. ret = 0;
  2368. break;
  2369. }
  2370. } else {
  2371. ret = 0;
  2372. }
  2373. }
  2374. l = path->nodes[0];
  2375. slot = path->slots[0];
  2376. btrfs_item_key_to_cpu(l, &found_key, slot);
  2377. if (found_key.objectid != scrub_dev->devid)
  2378. break;
  2379. if (found_key.type != BTRFS_DEV_EXTENT_KEY)
  2380. break;
  2381. if (found_key.offset >= end)
  2382. break;
  2383. if (found_key.offset < key.offset)
  2384. break;
  2385. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  2386. dev_extent_len = btrfs_dev_extent_length(l, dev_extent);
  2387. if (found_key.offset + dev_extent_len <= start)
  2388. goto skip;
  2389. chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
  2390. /*
  2391. * get a reference on the corresponding block group to prevent
  2392. * the chunk from going away while we scrub it
  2393. */
  2394. cache = btrfs_lookup_block_group(fs_info, chunk_offset);
  2395. /* some chunks are removed but not committed to disk yet,
  2396. * continue scrubbing */
  2397. if (!cache)
  2398. goto skip;
  2399. ASSERT(cache->start <= chunk_offset);
  2400. /*
  2401. * We are using the commit root to search for device extents, so
  2402. * that means we could have found a device extent item from a
  2403. * block group that was deleted in the current transaction. The
  2404. * logical start offset of the deleted block group, stored at
  2405. * @chunk_offset, might be part of the logical address range of
  2406. * a new block group (which uses different physical extents).
  2407. * In this case btrfs_lookup_block_group() has returned the new
  2408. * block group, and its start address is less than @chunk_offset.
  2409. *
  2410. * We skip such new block groups, because it's pointless to
  2411. * process them, as we won't find their extents because we search
  2412. * for them using the commit root of the extent tree. For a device
  2413. * replace it's also fine to skip it, we won't miss copying them
  2414. * to the target device because we have the write duplication
  2415. * setup through the regular write path (by btrfs_map_block()),
  2416. * and we have committed a transaction when we started the device
  2417. * replace, right after setting up the device replace state.
  2418. */
  2419. if (cache->start < chunk_offset) {
  2420. btrfs_put_block_group(cache);
  2421. goto skip;
  2422. }
  2423. if (sctx->is_dev_replace && btrfs_is_zoned(fs_info)) {
  2424. if (!test_bit(BLOCK_GROUP_FLAG_TO_COPY, &cache->runtime_flags)) {
  2425. btrfs_put_block_group(cache);
  2426. goto skip;
  2427. }
  2428. }
  2429. /*
  2430. * Make sure that while we are scrubbing the corresponding block
  2431. * group doesn't get its logical address and its device extents
  2432. * reused for another block group, which can possibly be of a
  2433. * different type and different profile. We do this to prevent
  2434. * false error detections and crashes due to bogus attempts to
  2435. * repair extents.
  2436. */
  2437. spin_lock(&cache->lock);
  2438. if (test_bit(BLOCK_GROUP_FLAG_REMOVED, &cache->runtime_flags)) {
  2439. spin_unlock(&cache->lock);
  2440. btrfs_put_block_group(cache);
  2441. goto skip;
  2442. }
  2443. btrfs_freeze_block_group(cache);
  2444. spin_unlock(&cache->lock);
  2445. /*
  2446. * we need call btrfs_inc_block_group_ro() with scrubs_paused,
  2447. * to avoid deadlock caused by:
  2448. * btrfs_inc_block_group_ro()
  2449. * -> btrfs_wait_for_commit()
  2450. * -> btrfs_commit_transaction()
  2451. * -> btrfs_scrub_pause()
  2452. */
  2453. scrub_pause_on(fs_info);
  2454. /*
  2455. * Don't do chunk preallocation for scrub.
  2456. *
  2457. * This is especially important for SYSTEM bgs, or we can hit
  2458. * -EFBIG from btrfs_finish_chunk_alloc() like:
  2459. * 1. The only SYSTEM bg is marked RO.
  2460. * Since SYSTEM bg is small, that's pretty common.
  2461. * 2. New SYSTEM bg will be allocated
  2462. * Due to regular version will allocate new chunk.
  2463. * 3. New SYSTEM bg is empty and will get cleaned up
  2464. * Before cleanup really happens, it's marked RO again.
  2465. * 4. Empty SYSTEM bg get scrubbed
  2466. * We go back to 2.
  2467. *
  2468. * This can easily boost the amount of SYSTEM chunks if cleaner
  2469. * thread can't be triggered fast enough, and use up all space
  2470. * of btrfs_super_block::sys_chunk_array
  2471. *
  2472. * While for dev replace, we need to try our best to mark block
  2473. * group RO, to prevent race between:
  2474. * - Write duplication
  2475. * Contains latest data
  2476. * - Scrub copy
  2477. * Contains data from commit tree
  2478. *
  2479. * If target block group is not marked RO, nocow writes can
  2480. * be overwritten by scrub copy, causing data corruption.
  2481. * So for dev-replace, it's not allowed to continue if a block
  2482. * group is not RO.
  2483. */
  2484. ret = btrfs_inc_block_group_ro(cache, sctx->is_dev_replace);
  2485. if (!ret && sctx->is_dev_replace) {
  2486. ret = finish_extent_writes_for_zoned(root, cache);
  2487. if (ret) {
  2488. btrfs_dec_block_group_ro(cache);
  2489. scrub_pause_off(fs_info);
  2490. btrfs_put_block_group(cache);
  2491. break;
  2492. }
  2493. }
  2494. if (ret == 0) {
  2495. ro_set = 1;
  2496. } else if (ret == -ENOSPC && !sctx->is_dev_replace &&
  2497. !(cache->flags & BTRFS_BLOCK_GROUP_RAID56_MASK)) {
  2498. /*
  2499. * btrfs_inc_block_group_ro return -ENOSPC when it
  2500. * failed in creating new chunk for metadata.
  2501. * It is not a problem for scrub, because
  2502. * metadata are always cowed, and our scrub paused
  2503. * commit_transactions.
  2504. *
  2505. * For RAID56 chunks, we have to mark them read-only
  2506. * for scrub, as later we would use our own cache
  2507. * out of RAID56 realm.
  2508. * Thus we want the RAID56 bg to be marked RO to
  2509. * prevent RMW from screwing up out cache.
  2510. */
  2511. ro_set = 0;
  2512. } else if (ret == -ETXTBSY) {
  2513. btrfs_warn(fs_info,
  2514. "scrub: skipping scrub of block group %llu due to active swapfile",
  2515. cache->start);
  2516. scrub_pause_off(fs_info);
  2517. ret = 0;
  2518. goto skip_unfreeze;
  2519. } else {
  2520. btrfs_warn(fs_info, "scrub: failed setting block group ro: %d",
  2521. ret);
  2522. btrfs_unfreeze_block_group(cache);
  2523. btrfs_put_block_group(cache);
  2524. scrub_pause_off(fs_info);
  2525. break;
  2526. }
  2527. /*
  2528. * Now the target block is marked RO, wait for nocow writes to
  2529. * finish before dev-replace.
  2530. * COW is fine, as COW never overwrites extents in commit tree.
  2531. */
  2532. if (sctx->is_dev_replace) {
  2533. btrfs_wait_nocow_writers(cache);
  2534. btrfs_wait_ordered_roots(fs_info, U64_MAX, cache);
  2535. }
  2536. scrub_pause_off(fs_info);
  2537. down_write(&dev_replace->rwsem);
  2538. dev_replace->cursor_right = found_key.offset + dev_extent_len;
  2539. dev_replace->cursor_left = found_key.offset;
  2540. dev_replace->item_needs_writeback = 1;
  2541. up_write(&dev_replace->rwsem);
  2542. ret = scrub_chunk(sctx, cache, scrub_dev, found_key.offset,
  2543. dev_extent_len);
  2544. if (sctx->is_dev_replace &&
  2545. !btrfs_finish_block_group_to_copy(dev_replace->srcdev,
  2546. cache, found_key.offset))
  2547. ro_set = 0;
  2548. down_write(&dev_replace->rwsem);
  2549. dev_replace->cursor_left = dev_replace->cursor_right;
  2550. dev_replace->item_needs_writeback = 1;
  2551. up_write(&dev_replace->rwsem);
  2552. if (ro_set)
  2553. btrfs_dec_block_group_ro(cache);
  2554. /*
  2555. * We might have prevented the cleaner kthread from deleting
  2556. * this block group if it was already unused because we raced
  2557. * and set it to RO mode first. So add it back to the unused
  2558. * list, otherwise it might not ever be deleted unless a manual
  2559. * balance is triggered or it becomes used and unused again.
  2560. */
  2561. spin_lock(&cache->lock);
  2562. if (!test_bit(BLOCK_GROUP_FLAG_REMOVED, &cache->runtime_flags) &&
  2563. !cache->ro && cache->reserved == 0 && cache->used == 0) {
  2564. spin_unlock(&cache->lock);
  2565. if (btrfs_test_opt(fs_info, DISCARD_ASYNC))
  2566. btrfs_discard_queue_work(&fs_info->discard_ctl,
  2567. cache);
  2568. else
  2569. btrfs_mark_bg_unused(cache);
  2570. } else {
  2571. spin_unlock(&cache->lock);
  2572. }
  2573. skip_unfreeze:
  2574. btrfs_unfreeze_block_group(cache);
  2575. btrfs_put_block_group(cache);
  2576. if (ret)
  2577. break;
  2578. if (unlikely(sctx->is_dev_replace &&
  2579. atomic64_read(&dev_replace->num_write_errors) > 0)) {
  2580. ret = -EIO;
  2581. break;
  2582. }
  2583. if (sctx->stat.malloc_errors > 0) {
  2584. ret = -ENOMEM;
  2585. break;
  2586. }
  2587. skip:
  2588. key.offset = found_key.offset + dev_extent_len;
  2589. btrfs_release_path(path);
  2590. }
  2591. return ret;
  2592. }
  2593. static int scrub_one_super(struct scrub_ctx *sctx, struct btrfs_device *dev,
  2594. struct page *page, u64 physical, u64 generation)
  2595. {
  2596. struct btrfs_fs_info *fs_info = sctx->fs_info;
  2597. struct btrfs_super_block *sb = page_address(page);
  2598. int ret;
  2599. ret = bdev_rw_virt(dev->bdev, physical >> SECTOR_SHIFT, sb,
  2600. BTRFS_SUPER_INFO_SIZE, REQ_OP_READ);
  2601. if (ret < 0)
  2602. return ret;
  2603. ret = btrfs_check_super_csum(fs_info, sb);
  2604. if (unlikely(ret != 0)) {
  2605. btrfs_err_rl(fs_info,
  2606. "scrub: super block at physical %llu devid %llu has bad csum",
  2607. physical, dev->devid);
  2608. return -EIO;
  2609. }
  2610. if (unlikely(btrfs_super_generation(sb) != generation)) {
  2611. btrfs_err_rl(fs_info,
  2612. "scrub: super block at physical %llu devid %llu has bad generation %llu expect %llu",
  2613. physical, dev->devid,
  2614. btrfs_super_generation(sb), generation);
  2615. return -EUCLEAN;
  2616. }
  2617. return btrfs_validate_super(fs_info, sb, -1);
  2618. }
  2619. static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,
  2620. struct btrfs_device *scrub_dev)
  2621. {
  2622. int i;
  2623. u64 bytenr;
  2624. u64 gen;
  2625. int ret = 0;
  2626. struct page *page;
  2627. struct btrfs_fs_info *fs_info = sctx->fs_info;
  2628. if (BTRFS_FS_ERROR(fs_info))
  2629. return -EROFS;
  2630. page = alloc_page(GFP_KERNEL);
  2631. if (!page) {
  2632. spin_lock(&sctx->stat_lock);
  2633. sctx->stat.malloc_errors++;
  2634. spin_unlock(&sctx->stat_lock);
  2635. return -ENOMEM;
  2636. }
  2637. /* Seed devices of a new filesystem has their own generation. */
  2638. if (scrub_dev->fs_devices != fs_info->fs_devices)
  2639. gen = scrub_dev->generation;
  2640. else
  2641. gen = btrfs_get_last_trans_committed(fs_info);
  2642. for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
  2643. ret = btrfs_sb_log_location(scrub_dev, i, 0, &bytenr);
  2644. if (ret == -ENOENT)
  2645. break;
  2646. if (ret) {
  2647. spin_lock(&sctx->stat_lock);
  2648. sctx->stat.super_errors++;
  2649. spin_unlock(&sctx->stat_lock);
  2650. continue;
  2651. }
  2652. if (bytenr + BTRFS_SUPER_INFO_SIZE >
  2653. scrub_dev->commit_total_bytes)
  2654. break;
  2655. if (!btrfs_check_super_location(scrub_dev, bytenr))
  2656. continue;
  2657. ret = scrub_one_super(sctx, scrub_dev, page, bytenr, gen);
  2658. if (ret) {
  2659. spin_lock(&sctx->stat_lock);
  2660. sctx->stat.super_errors++;
  2661. spin_unlock(&sctx->stat_lock);
  2662. }
  2663. }
  2664. __free_page(page);
  2665. return 0;
  2666. }
  2667. static void scrub_workers_put(struct btrfs_fs_info *fs_info)
  2668. {
  2669. if (refcount_dec_and_mutex_lock(&fs_info->scrub_workers_refcnt,
  2670. &fs_info->scrub_lock)) {
  2671. struct workqueue_struct *scrub_workers = fs_info->scrub_workers;
  2672. fs_info->scrub_workers = NULL;
  2673. mutex_unlock(&fs_info->scrub_lock);
  2674. if (scrub_workers)
  2675. destroy_workqueue(scrub_workers);
  2676. }
  2677. }
  2678. /*
  2679. * get a reference count on fs_info->scrub_workers. start worker if necessary
  2680. */
  2681. static noinline_for_stack int scrub_workers_get(struct btrfs_fs_info *fs_info)
  2682. {
  2683. struct workqueue_struct *scrub_workers = NULL;
  2684. unsigned int flags = WQ_FREEZABLE | WQ_UNBOUND;
  2685. int max_active = fs_info->thread_pool_size;
  2686. int ret = -ENOMEM;
  2687. if (refcount_inc_not_zero(&fs_info->scrub_workers_refcnt))
  2688. return 0;
  2689. scrub_workers = alloc_workqueue("btrfs-scrub", flags, max_active);
  2690. if (!scrub_workers)
  2691. return -ENOMEM;
  2692. mutex_lock(&fs_info->scrub_lock);
  2693. if (refcount_read(&fs_info->scrub_workers_refcnt) == 0) {
  2694. ASSERT(fs_info->scrub_workers == NULL);
  2695. fs_info->scrub_workers = scrub_workers;
  2696. refcount_set(&fs_info->scrub_workers_refcnt, 1);
  2697. mutex_unlock(&fs_info->scrub_lock);
  2698. return 0;
  2699. }
  2700. /* Other thread raced in and created the workers for us */
  2701. refcount_inc(&fs_info->scrub_workers_refcnt);
  2702. mutex_unlock(&fs_info->scrub_lock);
  2703. ret = 0;
  2704. destroy_workqueue(scrub_workers);
  2705. return ret;
  2706. }
  2707. int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
  2708. u64 end, struct btrfs_scrub_progress *progress,
  2709. bool readonly, bool is_dev_replace)
  2710. {
  2711. struct btrfs_dev_lookup_args args = { .devid = devid };
  2712. struct scrub_ctx *sctx;
  2713. int ret;
  2714. struct btrfs_device *dev;
  2715. unsigned int nofs_flag;
  2716. bool need_commit = false;
  2717. /* Set the basic fallback @last_physical before we got a sctx. */
  2718. if (progress)
  2719. progress->last_physical = start;
  2720. if (btrfs_fs_closing(fs_info))
  2721. return -EAGAIN;
  2722. /* At mount time we have ensured nodesize is in the range of [4K, 64K]. */
  2723. ASSERT(fs_info->nodesize <= BTRFS_STRIPE_LEN);
  2724. /*
  2725. * SCRUB_MAX_SECTORS_PER_BLOCK is calculated using the largest possible
  2726. * value (max nodesize / min sectorsize), thus nodesize should always
  2727. * be fine.
  2728. */
  2729. ASSERT(fs_info->nodesize <=
  2730. SCRUB_MAX_SECTORS_PER_BLOCK << fs_info->sectorsize_bits);
  2731. /* Allocate outside of device_list_mutex */
  2732. sctx = scrub_setup_ctx(fs_info, is_dev_replace);
  2733. if (IS_ERR(sctx))
  2734. return PTR_ERR(sctx);
  2735. sctx->stat.last_physical = start;
  2736. ret = scrub_workers_get(fs_info);
  2737. if (ret)
  2738. goto out_free_ctx;
  2739. mutex_lock(&fs_info->fs_devices->device_list_mutex);
  2740. dev = btrfs_find_device(fs_info->fs_devices, &args);
  2741. if (!dev || (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) &&
  2742. !is_dev_replace)) {
  2743. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  2744. ret = -ENODEV;
  2745. goto out;
  2746. }
  2747. if (!is_dev_replace && !readonly &&
  2748. !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) {
  2749. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  2750. btrfs_err(fs_info,
  2751. "scrub: devid %llu: filesystem on %s is not writable",
  2752. devid, btrfs_dev_name(dev));
  2753. ret = -EROFS;
  2754. goto out;
  2755. }
  2756. mutex_lock(&fs_info->scrub_lock);
  2757. if (unlikely(!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
  2758. test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &dev->dev_state))) {
  2759. mutex_unlock(&fs_info->scrub_lock);
  2760. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  2761. ret = -EIO;
  2762. goto out;
  2763. }
  2764. down_read(&fs_info->dev_replace.rwsem);
  2765. if (dev->scrub_ctx ||
  2766. (!is_dev_replace &&
  2767. btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))) {
  2768. up_read(&fs_info->dev_replace.rwsem);
  2769. mutex_unlock(&fs_info->scrub_lock);
  2770. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  2771. ret = -EINPROGRESS;
  2772. goto out;
  2773. }
  2774. up_read(&fs_info->dev_replace.rwsem);
  2775. sctx->readonly = readonly;
  2776. dev->scrub_ctx = sctx;
  2777. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  2778. /*
  2779. * checking @scrub_pause_req here, we can avoid
  2780. * race between committing transaction and scrubbing.
  2781. */
  2782. __scrub_blocked_if_needed(fs_info);
  2783. atomic_inc(&fs_info->scrubs_running);
  2784. mutex_unlock(&fs_info->scrub_lock);
  2785. /*
  2786. * In order to avoid deadlock with reclaim when there is a transaction
  2787. * trying to pause scrub, make sure we use GFP_NOFS for all the
  2788. * allocations done at btrfs_scrub_sectors() and scrub_sectors_for_parity()
  2789. * invoked by our callees. The pausing request is done when the
  2790. * transaction commit starts, and it blocks the transaction until scrub
  2791. * is paused (done at specific points at scrub_stripe() or right above
  2792. * before incrementing fs_info->scrubs_running).
  2793. */
  2794. nofs_flag = memalloc_nofs_save();
  2795. if (!is_dev_replace) {
  2796. u64 old_super_errors;
  2797. spin_lock(&sctx->stat_lock);
  2798. old_super_errors = sctx->stat.super_errors;
  2799. spin_unlock(&sctx->stat_lock);
  2800. btrfs_info(fs_info, "scrub: started on devid %llu", devid);
  2801. /*
  2802. * by holding device list mutex, we can
  2803. * kick off writing super in log tree sync.
  2804. */
  2805. mutex_lock(&fs_info->fs_devices->device_list_mutex);
  2806. ret = scrub_supers(sctx, dev);
  2807. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  2808. spin_lock(&sctx->stat_lock);
  2809. /*
  2810. * Super block errors found, but we can not commit transaction
  2811. * at current context, since btrfs_commit_transaction() needs
  2812. * to pause the current running scrub (hold by ourselves).
  2813. */
  2814. if (sctx->stat.super_errors > old_super_errors && !sctx->readonly)
  2815. need_commit = true;
  2816. spin_unlock(&sctx->stat_lock);
  2817. }
  2818. if (!ret)
  2819. ret = scrub_enumerate_chunks(sctx, dev, start, end);
  2820. memalloc_nofs_restore(nofs_flag);
  2821. atomic_dec(&fs_info->scrubs_running);
  2822. wake_up(&fs_info->scrub_pause_wait);
  2823. if (progress)
  2824. memcpy(progress, &sctx->stat, sizeof(*progress));
  2825. if (!is_dev_replace)
  2826. btrfs_info(fs_info, "scrub: %s on devid %llu with status: %d",
  2827. ret ? "not finished" : "finished", devid, ret);
  2828. mutex_lock(&fs_info->scrub_lock);
  2829. dev->scrub_ctx = NULL;
  2830. mutex_unlock(&fs_info->scrub_lock);
  2831. scrub_workers_put(fs_info);
  2832. scrub_put_ctx(sctx);
  2833. /*
  2834. * We found some super block errors before, now try to force a
  2835. * transaction commit, as scrub has finished.
  2836. */
  2837. if (need_commit) {
  2838. struct btrfs_trans_handle *trans;
  2839. trans = btrfs_start_transaction(fs_info->tree_root, 0);
  2840. if (IS_ERR(trans)) {
  2841. ret = PTR_ERR(trans);
  2842. btrfs_err(fs_info,
  2843. "scrub: failed to start transaction to fix super block errors: %d", ret);
  2844. return ret;
  2845. }
  2846. ret = btrfs_commit_transaction(trans);
  2847. if (ret < 0)
  2848. btrfs_err(fs_info,
  2849. "scrub: failed to commit transaction to fix super block errors: %d", ret);
  2850. }
  2851. return ret;
  2852. out:
  2853. scrub_workers_put(fs_info);
  2854. out_free_ctx:
  2855. scrub_free_ctx(sctx);
  2856. return ret;
  2857. }
  2858. void btrfs_scrub_pause(struct btrfs_fs_info *fs_info)
  2859. {
  2860. mutex_lock(&fs_info->scrub_lock);
  2861. atomic_inc(&fs_info->scrub_pause_req);
  2862. while (atomic_read(&fs_info->scrubs_paused) !=
  2863. atomic_read(&fs_info->scrubs_running)) {
  2864. mutex_unlock(&fs_info->scrub_lock);
  2865. wait_event(fs_info->scrub_pause_wait,
  2866. atomic_read(&fs_info->scrubs_paused) ==
  2867. atomic_read(&fs_info->scrubs_running));
  2868. mutex_lock(&fs_info->scrub_lock);
  2869. }
  2870. mutex_unlock(&fs_info->scrub_lock);
  2871. }
  2872. void btrfs_scrub_continue(struct btrfs_fs_info *fs_info)
  2873. {
  2874. atomic_dec(&fs_info->scrub_pause_req);
  2875. wake_up(&fs_info->scrub_pause_wait);
  2876. }
  2877. int btrfs_scrub_cancel(struct btrfs_fs_info *fs_info)
  2878. {
  2879. mutex_lock(&fs_info->scrub_lock);
  2880. if (!atomic_read(&fs_info->scrubs_running)) {
  2881. mutex_unlock(&fs_info->scrub_lock);
  2882. return -ENOTCONN;
  2883. }
  2884. atomic_inc(&fs_info->scrub_cancel_req);
  2885. while (atomic_read(&fs_info->scrubs_running)) {
  2886. mutex_unlock(&fs_info->scrub_lock);
  2887. wait_event(fs_info->scrub_pause_wait,
  2888. atomic_read(&fs_info->scrubs_running) == 0);
  2889. mutex_lock(&fs_info->scrub_lock);
  2890. }
  2891. atomic_dec(&fs_info->scrub_cancel_req);
  2892. mutex_unlock(&fs_info->scrub_lock);
  2893. return 0;
  2894. }
  2895. int btrfs_scrub_cancel_dev(struct btrfs_device *dev)
  2896. {
  2897. struct btrfs_fs_info *fs_info = dev->fs_info;
  2898. struct scrub_ctx *sctx;
  2899. mutex_lock(&fs_info->scrub_lock);
  2900. sctx = dev->scrub_ctx;
  2901. if (!sctx) {
  2902. mutex_unlock(&fs_info->scrub_lock);
  2903. return -ENOTCONN;
  2904. }
  2905. atomic_inc(&sctx->cancel_req);
  2906. while (dev->scrub_ctx) {
  2907. mutex_unlock(&fs_info->scrub_lock);
  2908. wait_event(fs_info->scrub_pause_wait,
  2909. dev->scrub_ctx == NULL);
  2910. mutex_lock(&fs_info->scrub_lock);
  2911. }
  2912. mutex_unlock(&fs_info->scrub_lock);
  2913. return 0;
  2914. }
  2915. int btrfs_scrub_progress(struct btrfs_fs_info *fs_info, u64 devid,
  2916. struct btrfs_scrub_progress *progress)
  2917. {
  2918. struct btrfs_dev_lookup_args args = { .devid = devid };
  2919. struct btrfs_device *dev;
  2920. struct scrub_ctx *sctx = NULL;
  2921. mutex_lock(&fs_info->fs_devices->device_list_mutex);
  2922. dev = btrfs_find_device(fs_info->fs_devices, &args);
  2923. if (dev)
  2924. sctx = dev->scrub_ctx;
  2925. if (sctx)
  2926. memcpy(progress, &sctx->stat, sizeof(*progress));
  2927. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  2928. return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV;
  2929. }