scrub.c 92 KB

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