scrub.c 105 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2011, 2012 STRATO. All rights reserved.
  4. */
  5. #include <linux/blkdev.h>
  6. #include <linux/ratelimit.h>
  7. #include <linux/sched/mm.h>
  8. #include "ctree.h"
  9. #include "volumes.h"
  10. #include "disk-io.h"
  11. #include "ordered-data.h"
  12. #include "transaction.h"
  13. #include "backref.h"
  14. #include "extent_io.h"
  15. #include "dev-replace.h"
  16. #include "check-integrity.h"
  17. #include "rcu-string.h"
  18. #include "raid56.h"
  19. /*
  20. * This is only the first step towards a full-features scrub. It reads all
  21. * extent and super block and verifies the checksums. In case a bad checksum
  22. * is found or the extent cannot be read, good data will be written back if
  23. * any can be found.
  24. *
  25. * Future enhancements:
  26. * - In case an unrepairable extent is encountered, track which files are
  27. * affected and report them
  28. * - track and record media errors, throw out bad devices
  29. * - add a mode to also read unallocated space
  30. */
  31. struct scrub_block;
  32. struct scrub_ctx;
  33. /*
  34. * the following three values only influence the performance.
  35. * The last one configures the number of parallel and outstanding I/O
  36. * operations. The first two values configure an upper limit for the number
  37. * of (dynamically allocated) pages that are added to a bio.
  38. */
  39. #define SCRUB_PAGES_PER_RD_BIO 32 /* 128k per bio */
  40. #define SCRUB_PAGES_PER_WR_BIO 32 /* 128k per bio */
  41. #define SCRUB_BIOS_PER_SCTX 64 /* 8MB per device in flight */
  42. /*
  43. * the following value times PAGE_SIZE needs to be large enough to match the
  44. * largest node/leaf/sector size that shall be supported.
  45. * Values larger than BTRFS_STRIPE_LEN are not supported.
  46. */
  47. #define SCRUB_MAX_PAGES_PER_BLOCK 16 /* 64k per node/leaf/sector */
  48. struct scrub_recover {
  49. refcount_t refs;
  50. struct btrfs_bio *bbio;
  51. u64 map_length;
  52. };
  53. struct scrub_page {
  54. struct scrub_block *sblock;
  55. struct page *page;
  56. struct btrfs_device *dev;
  57. struct list_head list;
  58. u64 flags; /* extent flags */
  59. u64 generation;
  60. u64 logical;
  61. u64 physical;
  62. u64 physical_for_dev_replace;
  63. atomic_t refs;
  64. struct {
  65. unsigned int mirror_num:8;
  66. unsigned int have_csum:1;
  67. unsigned int io_error:1;
  68. };
  69. u8 csum[BTRFS_CSUM_SIZE];
  70. struct scrub_recover *recover;
  71. };
  72. struct scrub_bio {
  73. int index;
  74. struct scrub_ctx *sctx;
  75. struct btrfs_device *dev;
  76. struct bio *bio;
  77. blk_status_t status;
  78. u64 logical;
  79. u64 physical;
  80. #if SCRUB_PAGES_PER_WR_BIO >= SCRUB_PAGES_PER_RD_BIO
  81. struct scrub_page *pagev[SCRUB_PAGES_PER_WR_BIO];
  82. #else
  83. struct scrub_page *pagev[SCRUB_PAGES_PER_RD_BIO];
  84. #endif
  85. int page_count;
  86. int next_free;
  87. struct btrfs_work work;
  88. };
  89. struct scrub_block {
  90. struct scrub_page *pagev[SCRUB_MAX_PAGES_PER_BLOCK];
  91. int page_count;
  92. atomic_t outstanding_pages;
  93. refcount_t refs; /* free mem on transition to zero */
  94. struct scrub_ctx *sctx;
  95. struct scrub_parity *sparity;
  96. struct {
  97. unsigned int header_error:1;
  98. unsigned int checksum_error:1;
  99. unsigned int no_io_error_seen:1;
  100. unsigned int generation_error:1; /* also sets header_error */
  101. /* The following is for the data used to check parity */
  102. /* It is for the data with checksum */
  103. unsigned int data_corrected:1;
  104. };
  105. struct btrfs_work work;
  106. };
  107. /* Used for the chunks with parity stripe such RAID5/6 */
  108. struct scrub_parity {
  109. struct scrub_ctx *sctx;
  110. struct btrfs_device *scrub_dev;
  111. u64 logic_start;
  112. u64 logic_end;
  113. int nsectors;
  114. u64 stripe_len;
  115. refcount_t refs;
  116. struct list_head spages;
  117. /* Work of parity check and repair */
  118. struct btrfs_work work;
  119. /* Mark the parity blocks which have data */
  120. unsigned long *dbitmap;
  121. /*
  122. * Mark the parity blocks which have data, but errors happen when
  123. * read data or check data
  124. */
  125. unsigned long *ebitmap;
  126. unsigned long bitmap[0];
  127. };
  128. struct scrub_ctx {
  129. struct scrub_bio *bios[SCRUB_BIOS_PER_SCTX];
  130. struct btrfs_fs_info *fs_info;
  131. int first_free;
  132. int curr;
  133. atomic_t bios_in_flight;
  134. atomic_t workers_pending;
  135. spinlock_t list_lock;
  136. wait_queue_head_t list_wait;
  137. u16 csum_size;
  138. struct list_head csum_list;
  139. atomic_t cancel_req;
  140. int readonly;
  141. int pages_per_rd_bio;
  142. int is_dev_replace;
  143. struct scrub_bio *wr_curr_bio;
  144. struct mutex wr_lock;
  145. int pages_per_wr_bio; /* <= SCRUB_PAGES_PER_WR_BIO */
  146. struct btrfs_device *wr_tgtdev;
  147. bool flush_all_writes;
  148. /*
  149. * statistics
  150. */
  151. struct btrfs_scrub_progress stat;
  152. spinlock_t stat_lock;
  153. /*
  154. * Use a ref counter to avoid use-after-free issues. Scrub workers
  155. * decrement bios_in_flight and workers_pending and then do a wakeup
  156. * on the list_wait wait queue. We must ensure the main scrub task
  157. * doesn't free the scrub context before or while the workers are
  158. * doing the wakeup() call.
  159. */
  160. refcount_t refs;
  161. };
  162. struct scrub_warning {
  163. struct btrfs_path *path;
  164. u64 extent_item_size;
  165. const char *errstr;
  166. u64 physical;
  167. u64 logical;
  168. struct btrfs_device *dev;
  169. };
  170. struct full_stripe_lock {
  171. struct rb_node node;
  172. u64 logical;
  173. u64 refs;
  174. struct mutex mutex;
  175. };
  176. static void scrub_pending_bio_inc(struct scrub_ctx *sctx);
  177. static void scrub_pending_bio_dec(struct scrub_ctx *sctx);
  178. static int scrub_handle_errored_block(struct scrub_block *sblock_to_check);
  179. static int scrub_setup_recheck_block(struct scrub_block *original_sblock,
  180. struct scrub_block *sblocks_for_recheck);
  181. static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
  182. struct scrub_block *sblock,
  183. int retry_failed_mirror);
  184. static void scrub_recheck_block_checksum(struct scrub_block *sblock);
  185. static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
  186. struct scrub_block *sblock_good);
  187. static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
  188. struct scrub_block *sblock_good,
  189. int page_num, int force_write);
  190. static void scrub_write_block_to_dev_replace(struct scrub_block *sblock);
  191. static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
  192. int page_num);
  193. static int scrub_checksum_data(struct scrub_block *sblock);
  194. static int scrub_checksum_tree_block(struct scrub_block *sblock);
  195. static int scrub_checksum_super(struct scrub_block *sblock);
  196. static void scrub_block_get(struct scrub_block *sblock);
  197. static void scrub_block_put(struct scrub_block *sblock);
  198. static void scrub_page_get(struct scrub_page *spage);
  199. static void scrub_page_put(struct scrub_page *spage);
  200. static void scrub_parity_get(struct scrub_parity *sparity);
  201. static void scrub_parity_put(struct scrub_parity *sparity);
  202. static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
  203. struct scrub_page *spage);
  204. static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
  205. u64 physical, struct btrfs_device *dev, u64 flags,
  206. u64 gen, int mirror_num, u8 *csum, int force,
  207. u64 physical_for_dev_replace);
  208. static void scrub_bio_end_io(struct bio *bio);
  209. static void scrub_bio_end_io_worker(struct btrfs_work *work);
  210. static void scrub_block_complete(struct scrub_block *sblock);
  211. static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
  212. u64 extent_logical, u64 extent_len,
  213. u64 *extent_physical,
  214. struct btrfs_device **extent_dev,
  215. int *extent_mirror_num);
  216. static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
  217. struct scrub_page *spage);
  218. static void scrub_wr_submit(struct scrub_ctx *sctx);
  219. static void scrub_wr_bio_end_io(struct bio *bio);
  220. static void scrub_wr_bio_end_io_worker(struct btrfs_work *work);
  221. static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info);
  222. static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info);
  223. static void scrub_put_ctx(struct scrub_ctx *sctx);
  224. static inline int scrub_is_page_on_raid56(struct scrub_page *page)
  225. {
  226. return page->recover &&
  227. (page->recover->bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK);
  228. }
  229. static void scrub_pending_bio_inc(struct scrub_ctx *sctx)
  230. {
  231. refcount_inc(&sctx->refs);
  232. atomic_inc(&sctx->bios_in_flight);
  233. }
  234. static void scrub_pending_bio_dec(struct scrub_ctx *sctx)
  235. {
  236. atomic_dec(&sctx->bios_in_flight);
  237. wake_up(&sctx->list_wait);
  238. scrub_put_ctx(sctx);
  239. }
  240. static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
  241. {
  242. while (atomic_read(&fs_info->scrub_pause_req)) {
  243. mutex_unlock(&fs_info->scrub_lock);
  244. wait_event(fs_info->scrub_pause_wait,
  245. atomic_read(&fs_info->scrub_pause_req) == 0);
  246. mutex_lock(&fs_info->scrub_lock);
  247. }
  248. }
  249. static void scrub_pause_on(struct btrfs_fs_info *fs_info)
  250. {
  251. atomic_inc(&fs_info->scrubs_paused);
  252. wake_up(&fs_info->scrub_pause_wait);
  253. }
  254. static void scrub_pause_off(struct btrfs_fs_info *fs_info)
  255. {
  256. mutex_lock(&fs_info->scrub_lock);
  257. __scrub_blocked_if_needed(fs_info);
  258. atomic_dec(&fs_info->scrubs_paused);
  259. mutex_unlock(&fs_info->scrub_lock);
  260. wake_up(&fs_info->scrub_pause_wait);
  261. }
  262. static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
  263. {
  264. scrub_pause_on(fs_info);
  265. scrub_pause_off(fs_info);
  266. }
  267. /*
  268. * Insert new full stripe lock into full stripe locks tree
  269. *
  270. * Return pointer to existing or newly inserted full_stripe_lock structure if
  271. * everything works well.
  272. * Return ERR_PTR(-ENOMEM) if we failed to allocate memory
  273. *
  274. * NOTE: caller must hold full_stripe_locks_root->lock before calling this
  275. * function
  276. */
  277. static struct full_stripe_lock *insert_full_stripe_lock(
  278. struct btrfs_full_stripe_locks_tree *locks_root,
  279. u64 fstripe_logical)
  280. {
  281. struct rb_node **p;
  282. struct rb_node *parent = NULL;
  283. struct full_stripe_lock *entry;
  284. struct full_stripe_lock *ret;
  285. lockdep_assert_held(&locks_root->lock);
  286. p = &locks_root->root.rb_node;
  287. while (*p) {
  288. parent = *p;
  289. entry = rb_entry(parent, struct full_stripe_lock, node);
  290. if (fstripe_logical < entry->logical) {
  291. p = &(*p)->rb_left;
  292. } else if (fstripe_logical > entry->logical) {
  293. p = &(*p)->rb_right;
  294. } else {
  295. entry->refs++;
  296. return entry;
  297. }
  298. }
  299. /* Insert new lock */
  300. ret = kmalloc(sizeof(*ret), GFP_KERNEL);
  301. if (!ret)
  302. return ERR_PTR(-ENOMEM);
  303. ret->logical = fstripe_logical;
  304. ret->refs = 1;
  305. mutex_init(&ret->mutex);
  306. rb_link_node(&ret->node, parent, p);
  307. rb_insert_color(&ret->node, &locks_root->root);
  308. return ret;
  309. }
  310. /*
  311. * Search for a full stripe lock of a block group
  312. *
  313. * Return pointer to existing full stripe lock if found
  314. * Return NULL if not found
  315. */
  316. static struct full_stripe_lock *search_full_stripe_lock(
  317. struct btrfs_full_stripe_locks_tree *locks_root,
  318. u64 fstripe_logical)
  319. {
  320. struct rb_node *node;
  321. struct full_stripe_lock *entry;
  322. lockdep_assert_held(&locks_root->lock);
  323. node = locks_root->root.rb_node;
  324. while (node) {
  325. entry = rb_entry(node, struct full_stripe_lock, node);
  326. if (fstripe_logical < entry->logical)
  327. node = node->rb_left;
  328. else if (fstripe_logical > entry->logical)
  329. node = node->rb_right;
  330. else
  331. return entry;
  332. }
  333. return NULL;
  334. }
  335. /*
  336. * Helper to get full stripe logical from a normal bytenr.
  337. *
  338. * Caller must ensure @cache is a RAID56 block group.
  339. */
  340. static u64 get_full_stripe_logical(struct btrfs_block_group_cache *cache,
  341. u64 bytenr)
  342. {
  343. u64 ret;
  344. /*
  345. * Due to chunk item size limit, full stripe length should not be
  346. * larger than U32_MAX. Just a sanity check here.
  347. */
  348. WARN_ON_ONCE(cache->full_stripe_len >= U32_MAX);
  349. /*
  350. * round_down() can only handle power of 2, while RAID56 full
  351. * stripe length can be 64KiB * n, so we need to manually round down.
  352. */
  353. ret = div64_u64(bytenr - cache->key.objectid, cache->full_stripe_len) *
  354. cache->full_stripe_len + cache->key.objectid;
  355. return ret;
  356. }
  357. /*
  358. * Lock a full stripe to avoid concurrency of recovery and read
  359. *
  360. * It's only used for profiles with parities (RAID5/6), for other profiles it
  361. * does nothing.
  362. *
  363. * Return 0 if we locked full stripe covering @bytenr, with a mutex held.
  364. * So caller must call unlock_full_stripe() at the same context.
  365. *
  366. * Return <0 if encounters error.
  367. */
  368. static int lock_full_stripe(struct btrfs_fs_info *fs_info, u64 bytenr,
  369. bool *locked_ret)
  370. {
  371. struct btrfs_block_group_cache *bg_cache;
  372. struct btrfs_full_stripe_locks_tree *locks_root;
  373. struct full_stripe_lock *existing;
  374. u64 fstripe_start;
  375. int ret = 0;
  376. *locked_ret = false;
  377. bg_cache = btrfs_lookup_block_group(fs_info, bytenr);
  378. if (!bg_cache) {
  379. ASSERT(0);
  380. return -ENOENT;
  381. }
  382. /* Profiles not based on parity don't need full stripe lock */
  383. if (!(bg_cache->flags & BTRFS_BLOCK_GROUP_RAID56_MASK))
  384. goto out;
  385. locks_root = &bg_cache->full_stripe_locks_root;
  386. fstripe_start = get_full_stripe_logical(bg_cache, bytenr);
  387. /* Now insert the full stripe lock */
  388. mutex_lock(&locks_root->lock);
  389. existing = insert_full_stripe_lock(locks_root, fstripe_start);
  390. mutex_unlock(&locks_root->lock);
  391. if (IS_ERR(existing)) {
  392. ret = PTR_ERR(existing);
  393. goto out;
  394. }
  395. mutex_lock(&existing->mutex);
  396. *locked_ret = true;
  397. out:
  398. btrfs_put_block_group(bg_cache);
  399. return ret;
  400. }
  401. /*
  402. * Unlock a full stripe.
  403. *
  404. * NOTE: Caller must ensure it's the same context calling corresponding
  405. * lock_full_stripe().
  406. *
  407. * Return 0 if we unlock full stripe without problem.
  408. * Return <0 for error
  409. */
  410. static int unlock_full_stripe(struct btrfs_fs_info *fs_info, u64 bytenr,
  411. bool locked)
  412. {
  413. struct btrfs_block_group_cache *bg_cache;
  414. struct btrfs_full_stripe_locks_tree *locks_root;
  415. struct full_stripe_lock *fstripe_lock;
  416. u64 fstripe_start;
  417. bool freeit = false;
  418. int ret = 0;
  419. /* If we didn't acquire full stripe lock, no need to continue */
  420. if (!locked)
  421. return 0;
  422. bg_cache = btrfs_lookup_block_group(fs_info, bytenr);
  423. if (!bg_cache) {
  424. ASSERT(0);
  425. return -ENOENT;
  426. }
  427. if (!(bg_cache->flags & BTRFS_BLOCK_GROUP_RAID56_MASK))
  428. goto out;
  429. locks_root = &bg_cache->full_stripe_locks_root;
  430. fstripe_start = get_full_stripe_logical(bg_cache, bytenr);
  431. mutex_lock(&locks_root->lock);
  432. fstripe_lock = search_full_stripe_lock(locks_root, fstripe_start);
  433. /* Unpaired unlock_full_stripe() detected */
  434. if (!fstripe_lock) {
  435. WARN_ON(1);
  436. ret = -ENOENT;
  437. mutex_unlock(&locks_root->lock);
  438. goto out;
  439. }
  440. if (fstripe_lock->refs == 0) {
  441. WARN_ON(1);
  442. btrfs_warn(fs_info, "full stripe lock at %llu refcount underflow",
  443. fstripe_lock->logical);
  444. } else {
  445. fstripe_lock->refs--;
  446. }
  447. if (fstripe_lock->refs == 0) {
  448. rb_erase(&fstripe_lock->node, &locks_root->root);
  449. freeit = true;
  450. }
  451. mutex_unlock(&locks_root->lock);
  452. mutex_unlock(&fstripe_lock->mutex);
  453. if (freeit)
  454. kfree(fstripe_lock);
  455. out:
  456. btrfs_put_block_group(bg_cache);
  457. return ret;
  458. }
  459. static void scrub_free_csums(struct scrub_ctx *sctx)
  460. {
  461. while (!list_empty(&sctx->csum_list)) {
  462. struct btrfs_ordered_sum *sum;
  463. sum = list_first_entry(&sctx->csum_list,
  464. struct btrfs_ordered_sum, list);
  465. list_del(&sum->list);
  466. kfree(sum);
  467. }
  468. }
  469. static noinline_for_stack void scrub_free_ctx(struct scrub_ctx *sctx)
  470. {
  471. int i;
  472. if (!sctx)
  473. return;
  474. /* this can happen when scrub is cancelled */
  475. if (sctx->curr != -1) {
  476. struct scrub_bio *sbio = sctx->bios[sctx->curr];
  477. for (i = 0; i < sbio->page_count; i++) {
  478. WARN_ON(!sbio->pagev[i]->page);
  479. scrub_block_put(sbio->pagev[i]->sblock);
  480. }
  481. bio_put(sbio->bio);
  482. }
  483. for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
  484. struct scrub_bio *sbio = sctx->bios[i];
  485. if (!sbio)
  486. break;
  487. kfree(sbio);
  488. }
  489. kfree(sctx->wr_curr_bio);
  490. scrub_free_csums(sctx);
  491. kfree(sctx);
  492. }
  493. static void scrub_put_ctx(struct scrub_ctx *sctx)
  494. {
  495. if (refcount_dec_and_test(&sctx->refs))
  496. scrub_free_ctx(sctx);
  497. }
  498. static noinline_for_stack
  499. struct scrub_ctx *scrub_setup_ctx(struct btrfs_device *dev, int is_dev_replace)
  500. {
  501. struct scrub_ctx *sctx;
  502. int i;
  503. struct btrfs_fs_info *fs_info = dev->fs_info;
  504. sctx = kzalloc(sizeof(*sctx), GFP_KERNEL);
  505. if (!sctx)
  506. goto nomem;
  507. refcount_set(&sctx->refs, 1);
  508. sctx->is_dev_replace = is_dev_replace;
  509. sctx->pages_per_rd_bio = SCRUB_PAGES_PER_RD_BIO;
  510. sctx->curr = -1;
  511. sctx->fs_info = dev->fs_info;
  512. for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
  513. struct scrub_bio *sbio;
  514. sbio = kzalloc(sizeof(*sbio), GFP_KERNEL);
  515. if (!sbio)
  516. goto nomem;
  517. sctx->bios[i] = sbio;
  518. sbio->index = i;
  519. sbio->sctx = sctx;
  520. sbio->page_count = 0;
  521. btrfs_init_work(&sbio->work, btrfs_scrub_helper,
  522. scrub_bio_end_io_worker, NULL, NULL);
  523. if (i != SCRUB_BIOS_PER_SCTX - 1)
  524. sctx->bios[i]->next_free = i + 1;
  525. else
  526. sctx->bios[i]->next_free = -1;
  527. }
  528. sctx->first_free = 0;
  529. atomic_set(&sctx->bios_in_flight, 0);
  530. atomic_set(&sctx->workers_pending, 0);
  531. atomic_set(&sctx->cancel_req, 0);
  532. sctx->csum_size = btrfs_super_csum_size(fs_info->super_copy);
  533. INIT_LIST_HEAD(&sctx->csum_list);
  534. spin_lock_init(&sctx->list_lock);
  535. spin_lock_init(&sctx->stat_lock);
  536. init_waitqueue_head(&sctx->list_wait);
  537. WARN_ON(sctx->wr_curr_bio != NULL);
  538. mutex_init(&sctx->wr_lock);
  539. sctx->wr_curr_bio = NULL;
  540. if (is_dev_replace) {
  541. WARN_ON(!fs_info->dev_replace.tgtdev);
  542. sctx->pages_per_wr_bio = SCRUB_PAGES_PER_WR_BIO;
  543. sctx->wr_tgtdev = fs_info->dev_replace.tgtdev;
  544. sctx->flush_all_writes = false;
  545. }
  546. return sctx;
  547. nomem:
  548. scrub_free_ctx(sctx);
  549. return ERR_PTR(-ENOMEM);
  550. }
  551. static int scrub_print_warning_inode(u64 inum, u64 offset, u64 root,
  552. void *warn_ctx)
  553. {
  554. u64 isize;
  555. u32 nlink;
  556. int ret;
  557. int i;
  558. unsigned nofs_flag;
  559. struct extent_buffer *eb;
  560. struct btrfs_inode_item *inode_item;
  561. struct scrub_warning *swarn = warn_ctx;
  562. struct btrfs_fs_info *fs_info = swarn->dev->fs_info;
  563. struct inode_fs_paths *ipath = NULL;
  564. struct btrfs_root *local_root;
  565. struct btrfs_key root_key;
  566. struct btrfs_key key;
  567. root_key.objectid = root;
  568. root_key.type = BTRFS_ROOT_ITEM_KEY;
  569. root_key.offset = (u64)-1;
  570. local_root = btrfs_read_fs_root_no_name(fs_info, &root_key);
  571. if (IS_ERR(local_root)) {
  572. ret = PTR_ERR(local_root);
  573. goto err;
  574. }
  575. /*
  576. * this makes the path point to (inum INODE_ITEM ioff)
  577. */
  578. key.objectid = inum;
  579. key.type = BTRFS_INODE_ITEM_KEY;
  580. key.offset = 0;
  581. ret = btrfs_search_slot(NULL, local_root, &key, swarn->path, 0, 0);
  582. if (ret) {
  583. btrfs_release_path(swarn->path);
  584. goto err;
  585. }
  586. eb = swarn->path->nodes[0];
  587. inode_item = btrfs_item_ptr(eb, swarn->path->slots[0],
  588. struct btrfs_inode_item);
  589. isize = btrfs_inode_size(eb, inode_item);
  590. nlink = btrfs_inode_nlink(eb, inode_item);
  591. btrfs_release_path(swarn->path);
  592. /*
  593. * init_path might indirectly call vmalloc, or use GFP_KERNEL. Scrub
  594. * uses GFP_NOFS in this context, so we keep it consistent but it does
  595. * not seem to be strictly necessary.
  596. */
  597. nofs_flag = memalloc_nofs_save();
  598. ipath = init_ipath(4096, local_root, swarn->path);
  599. memalloc_nofs_restore(nofs_flag);
  600. if (IS_ERR(ipath)) {
  601. ret = PTR_ERR(ipath);
  602. ipath = NULL;
  603. goto err;
  604. }
  605. ret = paths_from_inode(inum, ipath);
  606. if (ret < 0)
  607. goto err;
  608. /*
  609. * we deliberately ignore the bit ipath might have been too small to
  610. * hold all of the paths here
  611. */
  612. for (i = 0; i < ipath->fspath->elem_cnt; ++i)
  613. btrfs_warn_in_rcu(fs_info,
  614. "%s at logical %llu on dev %s, physical %llu, root %llu, inode %llu, offset %llu, length %llu, links %u (path: %s)",
  615. swarn->errstr, swarn->logical,
  616. rcu_str_deref(swarn->dev->name),
  617. swarn->physical,
  618. root, inum, offset,
  619. min(isize - offset, (u64)PAGE_SIZE), nlink,
  620. (char *)(unsigned long)ipath->fspath->val[i]);
  621. free_ipath(ipath);
  622. return 0;
  623. err:
  624. btrfs_warn_in_rcu(fs_info,
  625. "%s at logical %llu on dev %s, physical %llu, root %llu, inode %llu, offset %llu: path resolving failed with ret=%d",
  626. swarn->errstr, swarn->logical,
  627. rcu_str_deref(swarn->dev->name),
  628. swarn->physical,
  629. root, inum, offset, ret);
  630. free_ipath(ipath);
  631. return 0;
  632. }
  633. static void scrub_print_warning(const char *errstr, struct scrub_block *sblock)
  634. {
  635. struct btrfs_device *dev;
  636. struct btrfs_fs_info *fs_info;
  637. struct btrfs_path *path;
  638. struct btrfs_key found_key;
  639. struct extent_buffer *eb;
  640. struct btrfs_extent_item *ei;
  641. struct scrub_warning swarn;
  642. unsigned long ptr = 0;
  643. u64 extent_item_pos;
  644. u64 flags = 0;
  645. u64 ref_root;
  646. u32 item_size;
  647. u8 ref_level = 0;
  648. int ret;
  649. WARN_ON(sblock->page_count < 1);
  650. dev = sblock->pagev[0]->dev;
  651. fs_info = sblock->sctx->fs_info;
  652. path = btrfs_alloc_path();
  653. if (!path)
  654. return;
  655. swarn.physical = sblock->pagev[0]->physical;
  656. swarn.logical = sblock->pagev[0]->logical;
  657. swarn.errstr = errstr;
  658. swarn.dev = NULL;
  659. ret = extent_from_logical(fs_info, swarn.logical, path, &found_key,
  660. &flags);
  661. if (ret < 0)
  662. goto out;
  663. extent_item_pos = swarn.logical - found_key.objectid;
  664. swarn.extent_item_size = found_key.offset;
  665. eb = path->nodes[0];
  666. ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
  667. item_size = btrfs_item_size_nr(eb, path->slots[0]);
  668. if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  669. do {
  670. ret = tree_backref_for_extent(&ptr, eb, &found_key, ei,
  671. item_size, &ref_root,
  672. &ref_level);
  673. btrfs_warn_in_rcu(fs_info,
  674. "%s at logical %llu on dev %s, physical %llu: metadata %s (level %d) in tree %llu",
  675. errstr, swarn.logical,
  676. rcu_str_deref(dev->name),
  677. swarn.physical,
  678. ref_level ? "node" : "leaf",
  679. ret < 0 ? -1 : ref_level,
  680. ret < 0 ? -1 : ref_root);
  681. } while (ret != 1);
  682. btrfs_release_path(path);
  683. } else {
  684. btrfs_release_path(path);
  685. swarn.path = path;
  686. swarn.dev = dev;
  687. iterate_extent_inodes(fs_info, found_key.objectid,
  688. extent_item_pos, 1,
  689. scrub_print_warning_inode, &swarn, false);
  690. }
  691. out:
  692. btrfs_free_path(path);
  693. }
  694. static inline void scrub_get_recover(struct scrub_recover *recover)
  695. {
  696. refcount_inc(&recover->refs);
  697. }
  698. static inline void scrub_put_recover(struct btrfs_fs_info *fs_info,
  699. struct scrub_recover *recover)
  700. {
  701. if (refcount_dec_and_test(&recover->refs)) {
  702. btrfs_bio_counter_dec(fs_info);
  703. btrfs_put_bbio(recover->bbio);
  704. kfree(recover);
  705. }
  706. }
  707. /*
  708. * scrub_handle_errored_block gets called when either verification of the
  709. * pages failed or the bio failed to read, e.g. with EIO. In the latter
  710. * case, this function handles all pages in the bio, even though only one
  711. * may be bad.
  712. * The goal of this function is to repair the errored block by using the
  713. * contents of one of the mirrors.
  714. */
  715. static int scrub_handle_errored_block(struct scrub_block *sblock_to_check)
  716. {
  717. struct scrub_ctx *sctx = sblock_to_check->sctx;
  718. struct btrfs_device *dev;
  719. struct btrfs_fs_info *fs_info;
  720. u64 logical;
  721. unsigned int failed_mirror_index;
  722. unsigned int is_metadata;
  723. unsigned int have_csum;
  724. struct scrub_block *sblocks_for_recheck; /* holds one for each mirror */
  725. struct scrub_block *sblock_bad;
  726. int ret;
  727. int mirror_index;
  728. int page_num;
  729. int success;
  730. bool full_stripe_locked;
  731. static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
  732. DEFAULT_RATELIMIT_BURST);
  733. BUG_ON(sblock_to_check->page_count < 1);
  734. fs_info = sctx->fs_info;
  735. if (sblock_to_check->pagev[0]->flags & BTRFS_EXTENT_FLAG_SUPER) {
  736. /*
  737. * if we find an error in a super block, we just report it.
  738. * They will get written with the next transaction commit
  739. * anyway
  740. */
  741. spin_lock(&sctx->stat_lock);
  742. ++sctx->stat.super_errors;
  743. spin_unlock(&sctx->stat_lock);
  744. return 0;
  745. }
  746. logical = sblock_to_check->pagev[0]->logical;
  747. BUG_ON(sblock_to_check->pagev[0]->mirror_num < 1);
  748. failed_mirror_index = sblock_to_check->pagev[0]->mirror_num - 1;
  749. is_metadata = !(sblock_to_check->pagev[0]->flags &
  750. BTRFS_EXTENT_FLAG_DATA);
  751. have_csum = sblock_to_check->pagev[0]->have_csum;
  752. dev = sblock_to_check->pagev[0]->dev;
  753. /*
  754. * For RAID5/6, race can happen for a different device scrub thread.
  755. * For data corruption, Parity and Data threads will both try
  756. * to recovery the data.
  757. * Race can lead to doubly added csum error, or even unrecoverable
  758. * error.
  759. */
  760. ret = lock_full_stripe(fs_info, logical, &full_stripe_locked);
  761. if (ret < 0) {
  762. spin_lock(&sctx->stat_lock);
  763. if (ret == -ENOMEM)
  764. sctx->stat.malloc_errors++;
  765. sctx->stat.read_errors++;
  766. sctx->stat.uncorrectable_errors++;
  767. spin_unlock(&sctx->stat_lock);
  768. return ret;
  769. }
  770. /*
  771. * read all mirrors one after the other. This includes to
  772. * re-read the extent or metadata block that failed (that was
  773. * the cause that this fixup code is called) another time,
  774. * page by page this time in order to know which pages
  775. * caused I/O errors and which ones are good (for all mirrors).
  776. * It is the goal to handle the situation when more than one
  777. * mirror contains I/O errors, but the errors do not
  778. * overlap, i.e. the data can be repaired by selecting the
  779. * pages from those mirrors without I/O error on the
  780. * particular pages. One example (with blocks >= 2 * PAGE_SIZE)
  781. * would be that mirror #1 has an I/O error on the first page,
  782. * the second page is good, and mirror #2 has an I/O error on
  783. * the second page, but the first page is good.
  784. * Then the first page of the first mirror can be repaired by
  785. * taking the first page of the second mirror, and the
  786. * second page of the second mirror can be repaired by
  787. * copying the contents of the 2nd page of the 1st mirror.
  788. * One more note: if the pages of one mirror contain I/O
  789. * errors, the checksum cannot be verified. In order to get
  790. * the best data for repairing, the first attempt is to find
  791. * a mirror without I/O errors and with a validated checksum.
  792. * Only if this is not possible, the pages are picked from
  793. * mirrors with I/O errors without considering the checksum.
  794. * If the latter is the case, at the end, the checksum of the
  795. * repaired area is verified in order to correctly maintain
  796. * the statistics.
  797. */
  798. sblocks_for_recheck = kcalloc(BTRFS_MAX_MIRRORS,
  799. sizeof(*sblocks_for_recheck), GFP_NOFS);
  800. if (!sblocks_for_recheck) {
  801. spin_lock(&sctx->stat_lock);
  802. sctx->stat.malloc_errors++;
  803. sctx->stat.read_errors++;
  804. sctx->stat.uncorrectable_errors++;
  805. spin_unlock(&sctx->stat_lock);
  806. btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
  807. goto out;
  808. }
  809. /* setup the context, map the logical blocks and alloc the pages */
  810. ret = scrub_setup_recheck_block(sblock_to_check, sblocks_for_recheck);
  811. if (ret) {
  812. spin_lock(&sctx->stat_lock);
  813. sctx->stat.read_errors++;
  814. sctx->stat.uncorrectable_errors++;
  815. spin_unlock(&sctx->stat_lock);
  816. btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
  817. goto out;
  818. }
  819. BUG_ON(failed_mirror_index >= BTRFS_MAX_MIRRORS);
  820. sblock_bad = sblocks_for_recheck + failed_mirror_index;
  821. /* build and submit the bios for the failed mirror, check checksums */
  822. scrub_recheck_block(fs_info, sblock_bad, 1);
  823. if (!sblock_bad->header_error && !sblock_bad->checksum_error &&
  824. sblock_bad->no_io_error_seen) {
  825. /*
  826. * the error disappeared after reading page by page, or
  827. * the area was part of a huge bio and other parts of the
  828. * bio caused I/O errors, or the block layer merged several
  829. * read requests into one and the error is caused by a
  830. * different bio (usually one of the two latter cases is
  831. * the cause)
  832. */
  833. spin_lock(&sctx->stat_lock);
  834. sctx->stat.unverified_errors++;
  835. sblock_to_check->data_corrected = 1;
  836. spin_unlock(&sctx->stat_lock);
  837. if (sctx->is_dev_replace)
  838. scrub_write_block_to_dev_replace(sblock_bad);
  839. goto out;
  840. }
  841. if (!sblock_bad->no_io_error_seen) {
  842. spin_lock(&sctx->stat_lock);
  843. sctx->stat.read_errors++;
  844. spin_unlock(&sctx->stat_lock);
  845. if (__ratelimit(&_rs))
  846. scrub_print_warning("i/o error", sblock_to_check);
  847. btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
  848. } else if (sblock_bad->checksum_error) {
  849. spin_lock(&sctx->stat_lock);
  850. sctx->stat.csum_errors++;
  851. spin_unlock(&sctx->stat_lock);
  852. if (__ratelimit(&_rs))
  853. scrub_print_warning("checksum error", sblock_to_check);
  854. btrfs_dev_stat_inc_and_print(dev,
  855. BTRFS_DEV_STAT_CORRUPTION_ERRS);
  856. } else if (sblock_bad->header_error) {
  857. spin_lock(&sctx->stat_lock);
  858. sctx->stat.verify_errors++;
  859. spin_unlock(&sctx->stat_lock);
  860. if (__ratelimit(&_rs))
  861. scrub_print_warning("checksum/header error",
  862. sblock_to_check);
  863. if (sblock_bad->generation_error)
  864. btrfs_dev_stat_inc_and_print(dev,
  865. BTRFS_DEV_STAT_GENERATION_ERRS);
  866. else
  867. btrfs_dev_stat_inc_and_print(dev,
  868. BTRFS_DEV_STAT_CORRUPTION_ERRS);
  869. }
  870. if (sctx->readonly) {
  871. ASSERT(!sctx->is_dev_replace);
  872. goto out;
  873. }
  874. /*
  875. * now build and submit the bios for the other mirrors, check
  876. * checksums.
  877. * First try to pick the mirror which is completely without I/O
  878. * errors and also does not have a checksum error.
  879. * If one is found, and if a checksum is present, the full block
  880. * that is known to contain an error is rewritten. Afterwards
  881. * the block is known to be corrected.
  882. * If a mirror is found which is completely correct, and no
  883. * checksum is present, only those pages are rewritten that had
  884. * an I/O error in the block to be repaired, since it cannot be
  885. * determined, which copy of the other pages is better (and it
  886. * could happen otherwise that a correct page would be
  887. * overwritten by a bad one).
  888. */
  889. for (mirror_index = 0; ;mirror_index++) {
  890. struct scrub_block *sblock_other;
  891. if (mirror_index == failed_mirror_index)
  892. continue;
  893. /* raid56's mirror can be more than BTRFS_MAX_MIRRORS */
  894. if (!scrub_is_page_on_raid56(sblock_bad->pagev[0])) {
  895. if (mirror_index >= BTRFS_MAX_MIRRORS)
  896. break;
  897. if (!sblocks_for_recheck[mirror_index].page_count)
  898. break;
  899. sblock_other = sblocks_for_recheck + mirror_index;
  900. } else {
  901. struct scrub_recover *r = sblock_bad->pagev[0]->recover;
  902. int max_allowed = r->bbio->num_stripes -
  903. r->bbio->num_tgtdevs;
  904. if (mirror_index >= max_allowed)
  905. break;
  906. if (!sblocks_for_recheck[1].page_count)
  907. break;
  908. ASSERT(failed_mirror_index == 0);
  909. sblock_other = sblocks_for_recheck + 1;
  910. sblock_other->pagev[0]->mirror_num = 1 + mirror_index;
  911. }
  912. /* build and submit the bios, check checksums */
  913. scrub_recheck_block(fs_info, sblock_other, 0);
  914. if (!sblock_other->header_error &&
  915. !sblock_other->checksum_error &&
  916. sblock_other->no_io_error_seen) {
  917. if (sctx->is_dev_replace) {
  918. scrub_write_block_to_dev_replace(sblock_other);
  919. goto corrected_error;
  920. } else {
  921. ret = scrub_repair_block_from_good_copy(
  922. sblock_bad, sblock_other);
  923. if (!ret)
  924. goto corrected_error;
  925. }
  926. }
  927. }
  928. if (sblock_bad->no_io_error_seen && !sctx->is_dev_replace)
  929. goto did_not_correct_error;
  930. /*
  931. * In case of I/O errors in the area that is supposed to be
  932. * repaired, continue by picking good copies of those pages.
  933. * Select the good pages from mirrors to rewrite bad pages from
  934. * the area to fix. Afterwards verify the checksum of the block
  935. * that is supposed to be repaired. This verification step is
  936. * only done for the purpose of statistic counting and for the
  937. * final scrub report, whether errors remain.
  938. * A perfect algorithm could make use of the checksum and try
  939. * all possible combinations of pages from the different mirrors
  940. * until the checksum verification succeeds. For example, when
  941. * the 2nd page of mirror #1 faces I/O errors, and the 2nd page
  942. * of mirror #2 is readable but the final checksum test fails,
  943. * then the 2nd page of mirror #3 could be tried, whether now
  944. * the final checksum succeeds. But this would be a rare
  945. * exception and is therefore not implemented. At least it is
  946. * avoided that the good copy is overwritten.
  947. * A more useful improvement would be to pick the sectors
  948. * without I/O error based on sector sizes (512 bytes on legacy
  949. * disks) instead of on PAGE_SIZE. Then maybe 512 byte of one
  950. * mirror could be repaired by taking 512 byte of a different
  951. * mirror, even if other 512 byte sectors in the same PAGE_SIZE
  952. * area are unreadable.
  953. */
  954. success = 1;
  955. for (page_num = 0; page_num < sblock_bad->page_count;
  956. page_num++) {
  957. struct scrub_page *page_bad = sblock_bad->pagev[page_num];
  958. struct scrub_block *sblock_other = NULL;
  959. /* skip no-io-error page in scrub */
  960. if (!page_bad->io_error && !sctx->is_dev_replace)
  961. continue;
  962. if (scrub_is_page_on_raid56(sblock_bad->pagev[0])) {
  963. /*
  964. * In case of dev replace, if raid56 rebuild process
  965. * didn't work out correct data, then copy the content
  966. * in sblock_bad to make sure target device is identical
  967. * to source device, instead of writing garbage data in
  968. * sblock_for_recheck array to target device.
  969. */
  970. sblock_other = NULL;
  971. } else if (page_bad->io_error) {
  972. /* try to find no-io-error page in mirrors */
  973. for (mirror_index = 0;
  974. mirror_index < BTRFS_MAX_MIRRORS &&
  975. sblocks_for_recheck[mirror_index].page_count > 0;
  976. mirror_index++) {
  977. if (!sblocks_for_recheck[mirror_index].
  978. pagev[page_num]->io_error) {
  979. sblock_other = sblocks_for_recheck +
  980. mirror_index;
  981. break;
  982. }
  983. }
  984. if (!sblock_other)
  985. success = 0;
  986. }
  987. if (sctx->is_dev_replace) {
  988. /*
  989. * did not find a mirror to fetch the page
  990. * from. scrub_write_page_to_dev_replace()
  991. * handles this case (page->io_error), by
  992. * filling the block with zeros before
  993. * submitting the write request
  994. */
  995. if (!sblock_other)
  996. sblock_other = sblock_bad;
  997. if (scrub_write_page_to_dev_replace(sblock_other,
  998. page_num) != 0) {
  999. atomic64_inc(
  1000. &fs_info->dev_replace.num_write_errors);
  1001. success = 0;
  1002. }
  1003. } else if (sblock_other) {
  1004. ret = scrub_repair_page_from_good_copy(sblock_bad,
  1005. sblock_other,
  1006. page_num, 0);
  1007. if (0 == ret)
  1008. page_bad->io_error = 0;
  1009. else
  1010. success = 0;
  1011. }
  1012. }
  1013. if (success && !sctx->is_dev_replace) {
  1014. if (is_metadata || have_csum) {
  1015. /*
  1016. * need to verify the checksum now that all
  1017. * sectors on disk are repaired (the write
  1018. * request for data to be repaired is on its way).
  1019. * Just be lazy and use scrub_recheck_block()
  1020. * which re-reads the data before the checksum
  1021. * is verified, but most likely the data comes out
  1022. * of the page cache.
  1023. */
  1024. scrub_recheck_block(fs_info, sblock_bad, 1);
  1025. if (!sblock_bad->header_error &&
  1026. !sblock_bad->checksum_error &&
  1027. sblock_bad->no_io_error_seen)
  1028. goto corrected_error;
  1029. else
  1030. goto did_not_correct_error;
  1031. } else {
  1032. corrected_error:
  1033. spin_lock(&sctx->stat_lock);
  1034. sctx->stat.corrected_errors++;
  1035. sblock_to_check->data_corrected = 1;
  1036. spin_unlock(&sctx->stat_lock);
  1037. btrfs_err_rl_in_rcu(fs_info,
  1038. "fixed up error at logical %llu on dev %s",
  1039. logical, rcu_str_deref(dev->name));
  1040. }
  1041. } else {
  1042. did_not_correct_error:
  1043. spin_lock(&sctx->stat_lock);
  1044. sctx->stat.uncorrectable_errors++;
  1045. spin_unlock(&sctx->stat_lock);
  1046. btrfs_err_rl_in_rcu(fs_info,
  1047. "unable to fixup (regular) error at logical %llu on dev %s",
  1048. logical, rcu_str_deref(dev->name));
  1049. }
  1050. out:
  1051. if (sblocks_for_recheck) {
  1052. for (mirror_index = 0; mirror_index < BTRFS_MAX_MIRRORS;
  1053. mirror_index++) {
  1054. struct scrub_block *sblock = sblocks_for_recheck +
  1055. mirror_index;
  1056. struct scrub_recover *recover;
  1057. int page_index;
  1058. for (page_index = 0; page_index < sblock->page_count;
  1059. page_index++) {
  1060. sblock->pagev[page_index]->sblock = NULL;
  1061. recover = sblock->pagev[page_index]->recover;
  1062. if (recover) {
  1063. scrub_put_recover(fs_info, recover);
  1064. sblock->pagev[page_index]->recover =
  1065. NULL;
  1066. }
  1067. scrub_page_put(sblock->pagev[page_index]);
  1068. }
  1069. }
  1070. kfree(sblocks_for_recheck);
  1071. }
  1072. ret = unlock_full_stripe(fs_info, logical, full_stripe_locked);
  1073. if (ret < 0)
  1074. return ret;
  1075. return 0;
  1076. }
  1077. static inline int scrub_nr_raid_mirrors(struct btrfs_bio *bbio)
  1078. {
  1079. if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID5)
  1080. return 2;
  1081. else if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID6)
  1082. return 3;
  1083. else
  1084. return (int)bbio->num_stripes;
  1085. }
  1086. static inline void scrub_stripe_index_and_offset(u64 logical, u64 map_type,
  1087. u64 *raid_map,
  1088. u64 mapped_length,
  1089. int nstripes, int mirror,
  1090. int *stripe_index,
  1091. u64 *stripe_offset)
  1092. {
  1093. int i;
  1094. if (map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
  1095. /* RAID5/6 */
  1096. for (i = 0; i < nstripes; i++) {
  1097. if (raid_map[i] == RAID6_Q_STRIPE ||
  1098. raid_map[i] == RAID5_P_STRIPE)
  1099. continue;
  1100. if (logical >= raid_map[i] &&
  1101. logical < raid_map[i] + mapped_length)
  1102. break;
  1103. }
  1104. *stripe_index = i;
  1105. *stripe_offset = logical - raid_map[i];
  1106. } else {
  1107. /* The other RAID type */
  1108. *stripe_index = mirror;
  1109. *stripe_offset = 0;
  1110. }
  1111. }
  1112. static int scrub_setup_recheck_block(struct scrub_block *original_sblock,
  1113. struct scrub_block *sblocks_for_recheck)
  1114. {
  1115. struct scrub_ctx *sctx = original_sblock->sctx;
  1116. struct btrfs_fs_info *fs_info = sctx->fs_info;
  1117. u64 length = original_sblock->page_count * PAGE_SIZE;
  1118. u64 logical = original_sblock->pagev[0]->logical;
  1119. u64 generation = original_sblock->pagev[0]->generation;
  1120. u64 flags = original_sblock->pagev[0]->flags;
  1121. u64 have_csum = original_sblock->pagev[0]->have_csum;
  1122. struct scrub_recover *recover;
  1123. struct btrfs_bio *bbio;
  1124. u64 sublen;
  1125. u64 mapped_length;
  1126. u64 stripe_offset;
  1127. int stripe_index;
  1128. int page_index = 0;
  1129. int mirror_index;
  1130. int nmirrors;
  1131. int ret;
  1132. /*
  1133. * note: the two members refs and outstanding_pages
  1134. * are not used (and not set) in the blocks that are used for
  1135. * the recheck procedure
  1136. */
  1137. while (length > 0) {
  1138. sublen = min_t(u64, length, PAGE_SIZE);
  1139. mapped_length = sublen;
  1140. bbio = NULL;
  1141. /*
  1142. * with a length of PAGE_SIZE, each returned stripe
  1143. * represents one mirror
  1144. */
  1145. btrfs_bio_counter_inc_blocked(fs_info);
  1146. ret = btrfs_map_sblock(fs_info, BTRFS_MAP_GET_READ_MIRRORS,
  1147. logical, &mapped_length, &bbio);
  1148. if (ret || !bbio || mapped_length < sublen) {
  1149. btrfs_put_bbio(bbio);
  1150. btrfs_bio_counter_dec(fs_info);
  1151. return -EIO;
  1152. }
  1153. recover = kzalloc(sizeof(struct scrub_recover), GFP_NOFS);
  1154. if (!recover) {
  1155. btrfs_put_bbio(bbio);
  1156. btrfs_bio_counter_dec(fs_info);
  1157. return -ENOMEM;
  1158. }
  1159. refcount_set(&recover->refs, 1);
  1160. recover->bbio = bbio;
  1161. recover->map_length = mapped_length;
  1162. BUG_ON(page_index >= SCRUB_MAX_PAGES_PER_BLOCK);
  1163. nmirrors = min(scrub_nr_raid_mirrors(bbio), BTRFS_MAX_MIRRORS);
  1164. for (mirror_index = 0; mirror_index < nmirrors;
  1165. mirror_index++) {
  1166. struct scrub_block *sblock;
  1167. struct scrub_page *page;
  1168. sblock = sblocks_for_recheck + mirror_index;
  1169. sblock->sctx = sctx;
  1170. page = kzalloc(sizeof(*page), GFP_NOFS);
  1171. if (!page) {
  1172. leave_nomem:
  1173. spin_lock(&sctx->stat_lock);
  1174. sctx->stat.malloc_errors++;
  1175. spin_unlock(&sctx->stat_lock);
  1176. scrub_put_recover(fs_info, recover);
  1177. return -ENOMEM;
  1178. }
  1179. scrub_page_get(page);
  1180. sblock->pagev[page_index] = page;
  1181. page->sblock = sblock;
  1182. page->flags = flags;
  1183. page->generation = generation;
  1184. page->logical = logical;
  1185. page->have_csum = have_csum;
  1186. if (have_csum)
  1187. memcpy(page->csum,
  1188. original_sblock->pagev[0]->csum,
  1189. sctx->csum_size);
  1190. scrub_stripe_index_and_offset(logical,
  1191. bbio->map_type,
  1192. bbio->raid_map,
  1193. mapped_length,
  1194. bbio->num_stripes -
  1195. bbio->num_tgtdevs,
  1196. mirror_index,
  1197. &stripe_index,
  1198. &stripe_offset);
  1199. page->physical = bbio->stripes[stripe_index].physical +
  1200. stripe_offset;
  1201. page->dev = bbio->stripes[stripe_index].dev;
  1202. BUG_ON(page_index >= original_sblock->page_count);
  1203. page->physical_for_dev_replace =
  1204. original_sblock->pagev[page_index]->
  1205. physical_for_dev_replace;
  1206. /* for missing devices, dev->bdev is NULL */
  1207. page->mirror_num = mirror_index + 1;
  1208. sblock->page_count++;
  1209. page->page = alloc_page(GFP_NOFS);
  1210. if (!page->page)
  1211. goto leave_nomem;
  1212. scrub_get_recover(recover);
  1213. page->recover = recover;
  1214. }
  1215. scrub_put_recover(fs_info, recover);
  1216. length -= sublen;
  1217. logical += sublen;
  1218. page_index++;
  1219. }
  1220. return 0;
  1221. }
  1222. static void scrub_bio_wait_endio(struct bio *bio)
  1223. {
  1224. complete(bio->bi_private);
  1225. }
  1226. static int scrub_submit_raid56_bio_wait(struct btrfs_fs_info *fs_info,
  1227. struct bio *bio,
  1228. struct scrub_page *page)
  1229. {
  1230. DECLARE_COMPLETION_ONSTACK(done);
  1231. int ret;
  1232. int mirror_num;
  1233. bio->bi_iter.bi_sector = page->logical >> 9;
  1234. bio->bi_private = &done;
  1235. bio->bi_end_io = scrub_bio_wait_endio;
  1236. mirror_num = page->sblock->pagev[0]->mirror_num;
  1237. ret = raid56_parity_recover(fs_info, bio, page->recover->bbio,
  1238. page->recover->map_length,
  1239. mirror_num, 0);
  1240. if (ret)
  1241. return ret;
  1242. wait_for_completion_io(&done);
  1243. return blk_status_to_errno(bio->bi_status);
  1244. }
  1245. static void scrub_recheck_block_on_raid56(struct btrfs_fs_info *fs_info,
  1246. struct scrub_block *sblock)
  1247. {
  1248. struct scrub_page *first_page = sblock->pagev[0];
  1249. struct bio *bio;
  1250. int page_num;
  1251. /* All pages in sblock belong to the same stripe on the same device. */
  1252. ASSERT(first_page->dev);
  1253. if (!first_page->dev->bdev)
  1254. goto out;
  1255. bio = btrfs_io_bio_alloc(BIO_MAX_PAGES);
  1256. bio_set_dev(bio, first_page->dev->bdev);
  1257. for (page_num = 0; page_num < sblock->page_count; page_num++) {
  1258. struct scrub_page *page = sblock->pagev[page_num];
  1259. WARN_ON(!page->page);
  1260. bio_add_page(bio, page->page, PAGE_SIZE, 0);
  1261. }
  1262. if (scrub_submit_raid56_bio_wait(fs_info, bio, first_page)) {
  1263. bio_put(bio);
  1264. goto out;
  1265. }
  1266. bio_put(bio);
  1267. scrub_recheck_block_checksum(sblock);
  1268. return;
  1269. out:
  1270. for (page_num = 0; page_num < sblock->page_count; page_num++)
  1271. sblock->pagev[page_num]->io_error = 1;
  1272. sblock->no_io_error_seen = 0;
  1273. }
  1274. /*
  1275. * this function will check the on disk data for checksum errors, header
  1276. * errors and read I/O errors. If any I/O errors happen, the exact pages
  1277. * which are errored are marked as being bad. The goal is to enable scrub
  1278. * to take those pages that are not errored from all the mirrors so that
  1279. * the pages that are errored in the just handled mirror can be repaired.
  1280. */
  1281. static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
  1282. struct scrub_block *sblock,
  1283. int retry_failed_mirror)
  1284. {
  1285. int page_num;
  1286. sblock->no_io_error_seen = 1;
  1287. /* short cut for raid56 */
  1288. if (!retry_failed_mirror && scrub_is_page_on_raid56(sblock->pagev[0]))
  1289. return scrub_recheck_block_on_raid56(fs_info, sblock);
  1290. for (page_num = 0; page_num < sblock->page_count; page_num++) {
  1291. struct bio *bio;
  1292. struct scrub_page *page = sblock->pagev[page_num];
  1293. if (page->dev->bdev == NULL) {
  1294. page->io_error = 1;
  1295. sblock->no_io_error_seen = 0;
  1296. continue;
  1297. }
  1298. WARN_ON(!page->page);
  1299. bio = btrfs_io_bio_alloc(1);
  1300. bio_set_dev(bio, page->dev->bdev);
  1301. bio_add_page(bio, page->page, PAGE_SIZE, 0);
  1302. bio->bi_iter.bi_sector = page->physical >> 9;
  1303. bio->bi_opf = REQ_OP_READ;
  1304. if (btrfsic_submit_bio_wait(bio)) {
  1305. page->io_error = 1;
  1306. sblock->no_io_error_seen = 0;
  1307. }
  1308. bio_put(bio);
  1309. }
  1310. if (sblock->no_io_error_seen)
  1311. scrub_recheck_block_checksum(sblock);
  1312. }
  1313. static inline int scrub_check_fsid(u8 fsid[],
  1314. struct scrub_page *spage)
  1315. {
  1316. struct btrfs_fs_devices *fs_devices = spage->dev->fs_devices;
  1317. int ret;
  1318. ret = memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
  1319. return !ret;
  1320. }
  1321. static void scrub_recheck_block_checksum(struct scrub_block *sblock)
  1322. {
  1323. sblock->header_error = 0;
  1324. sblock->checksum_error = 0;
  1325. sblock->generation_error = 0;
  1326. if (sblock->pagev[0]->flags & BTRFS_EXTENT_FLAG_DATA)
  1327. scrub_checksum_data(sblock);
  1328. else
  1329. scrub_checksum_tree_block(sblock);
  1330. }
  1331. static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
  1332. struct scrub_block *sblock_good)
  1333. {
  1334. int page_num;
  1335. int ret = 0;
  1336. for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
  1337. int ret_sub;
  1338. ret_sub = scrub_repair_page_from_good_copy(sblock_bad,
  1339. sblock_good,
  1340. page_num, 1);
  1341. if (ret_sub)
  1342. ret = ret_sub;
  1343. }
  1344. return ret;
  1345. }
  1346. static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
  1347. struct scrub_block *sblock_good,
  1348. int page_num, int force_write)
  1349. {
  1350. struct scrub_page *page_bad = sblock_bad->pagev[page_num];
  1351. struct scrub_page *page_good = sblock_good->pagev[page_num];
  1352. struct btrfs_fs_info *fs_info = sblock_bad->sctx->fs_info;
  1353. BUG_ON(page_bad->page == NULL);
  1354. BUG_ON(page_good->page == NULL);
  1355. if (force_write || sblock_bad->header_error ||
  1356. sblock_bad->checksum_error || page_bad->io_error) {
  1357. struct bio *bio;
  1358. int ret;
  1359. if (!page_bad->dev->bdev) {
  1360. btrfs_warn_rl(fs_info,
  1361. "scrub_repair_page_from_good_copy(bdev == NULL) is unexpected");
  1362. return -EIO;
  1363. }
  1364. bio = btrfs_io_bio_alloc(1);
  1365. bio_set_dev(bio, page_bad->dev->bdev);
  1366. bio->bi_iter.bi_sector = page_bad->physical >> 9;
  1367. bio->bi_opf = REQ_OP_WRITE;
  1368. ret = bio_add_page(bio, page_good->page, PAGE_SIZE, 0);
  1369. if (PAGE_SIZE != ret) {
  1370. bio_put(bio);
  1371. return -EIO;
  1372. }
  1373. if (btrfsic_submit_bio_wait(bio)) {
  1374. btrfs_dev_stat_inc_and_print(page_bad->dev,
  1375. BTRFS_DEV_STAT_WRITE_ERRS);
  1376. atomic64_inc(&fs_info->dev_replace.num_write_errors);
  1377. bio_put(bio);
  1378. return -EIO;
  1379. }
  1380. bio_put(bio);
  1381. }
  1382. return 0;
  1383. }
  1384. static void scrub_write_block_to_dev_replace(struct scrub_block *sblock)
  1385. {
  1386. struct btrfs_fs_info *fs_info = sblock->sctx->fs_info;
  1387. int page_num;
  1388. /*
  1389. * This block is used for the check of the parity on the source device,
  1390. * so the data needn't be written into the destination device.
  1391. */
  1392. if (sblock->sparity)
  1393. return;
  1394. for (page_num = 0; page_num < sblock->page_count; page_num++) {
  1395. int ret;
  1396. ret = scrub_write_page_to_dev_replace(sblock, page_num);
  1397. if (ret)
  1398. atomic64_inc(&fs_info->dev_replace.num_write_errors);
  1399. }
  1400. }
  1401. static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
  1402. int page_num)
  1403. {
  1404. struct scrub_page *spage = sblock->pagev[page_num];
  1405. BUG_ON(spage->page == NULL);
  1406. if (spage->io_error) {
  1407. void *mapped_buffer = kmap_atomic(spage->page);
  1408. clear_page(mapped_buffer);
  1409. flush_dcache_page(spage->page);
  1410. kunmap_atomic(mapped_buffer);
  1411. }
  1412. return scrub_add_page_to_wr_bio(sblock->sctx, spage);
  1413. }
  1414. static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
  1415. struct scrub_page *spage)
  1416. {
  1417. struct scrub_bio *sbio;
  1418. int ret;
  1419. mutex_lock(&sctx->wr_lock);
  1420. again:
  1421. if (!sctx->wr_curr_bio) {
  1422. sctx->wr_curr_bio = kzalloc(sizeof(*sctx->wr_curr_bio),
  1423. GFP_KERNEL);
  1424. if (!sctx->wr_curr_bio) {
  1425. mutex_unlock(&sctx->wr_lock);
  1426. return -ENOMEM;
  1427. }
  1428. sctx->wr_curr_bio->sctx = sctx;
  1429. sctx->wr_curr_bio->page_count = 0;
  1430. }
  1431. sbio = sctx->wr_curr_bio;
  1432. if (sbio->page_count == 0) {
  1433. struct bio *bio;
  1434. sbio->physical = spage->physical_for_dev_replace;
  1435. sbio->logical = spage->logical;
  1436. sbio->dev = sctx->wr_tgtdev;
  1437. bio = sbio->bio;
  1438. if (!bio) {
  1439. bio = btrfs_io_bio_alloc(sctx->pages_per_wr_bio);
  1440. sbio->bio = bio;
  1441. }
  1442. bio->bi_private = sbio;
  1443. bio->bi_end_io = scrub_wr_bio_end_io;
  1444. bio_set_dev(bio, sbio->dev->bdev);
  1445. bio->bi_iter.bi_sector = sbio->physical >> 9;
  1446. bio->bi_opf = REQ_OP_WRITE;
  1447. sbio->status = 0;
  1448. } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
  1449. spage->physical_for_dev_replace ||
  1450. sbio->logical + sbio->page_count * PAGE_SIZE !=
  1451. spage->logical) {
  1452. scrub_wr_submit(sctx);
  1453. goto again;
  1454. }
  1455. ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
  1456. if (ret != PAGE_SIZE) {
  1457. if (sbio->page_count < 1) {
  1458. bio_put(sbio->bio);
  1459. sbio->bio = NULL;
  1460. mutex_unlock(&sctx->wr_lock);
  1461. return -EIO;
  1462. }
  1463. scrub_wr_submit(sctx);
  1464. goto again;
  1465. }
  1466. sbio->pagev[sbio->page_count] = spage;
  1467. scrub_page_get(spage);
  1468. sbio->page_count++;
  1469. if (sbio->page_count == sctx->pages_per_wr_bio)
  1470. scrub_wr_submit(sctx);
  1471. mutex_unlock(&sctx->wr_lock);
  1472. return 0;
  1473. }
  1474. static void scrub_wr_submit(struct scrub_ctx *sctx)
  1475. {
  1476. struct scrub_bio *sbio;
  1477. if (!sctx->wr_curr_bio)
  1478. return;
  1479. sbio = sctx->wr_curr_bio;
  1480. sctx->wr_curr_bio = NULL;
  1481. WARN_ON(!sbio->bio->bi_disk);
  1482. scrub_pending_bio_inc(sctx);
  1483. /* process all writes in a single worker thread. Then the block layer
  1484. * orders the requests before sending them to the driver which
  1485. * doubled the write performance on spinning disks when measured
  1486. * with Linux 3.5 */
  1487. btrfsic_submit_bio(sbio->bio);
  1488. }
  1489. static void scrub_wr_bio_end_io(struct bio *bio)
  1490. {
  1491. struct scrub_bio *sbio = bio->bi_private;
  1492. struct btrfs_fs_info *fs_info = sbio->dev->fs_info;
  1493. sbio->status = bio->bi_status;
  1494. sbio->bio = bio;
  1495. btrfs_init_work(&sbio->work, btrfs_scrubwrc_helper,
  1496. scrub_wr_bio_end_io_worker, NULL, NULL);
  1497. btrfs_queue_work(fs_info->scrub_wr_completion_workers, &sbio->work);
  1498. }
  1499. static void scrub_wr_bio_end_io_worker(struct btrfs_work *work)
  1500. {
  1501. struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
  1502. struct scrub_ctx *sctx = sbio->sctx;
  1503. int i;
  1504. WARN_ON(sbio->page_count > SCRUB_PAGES_PER_WR_BIO);
  1505. if (sbio->status) {
  1506. struct btrfs_dev_replace *dev_replace =
  1507. &sbio->sctx->fs_info->dev_replace;
  1508. for (i = 0; i < sbio->page_count; i++) {
  1509. struct scrub_page *spage = sbio->pagev[i];
  1510. spage->io_error = 1;
  1511. atomic64_inc(&dev_replace->num_write_errors);
  1512. }
  1513. }
  1514. for (i = 0; i < sbio->page_count; i++)
  1515. scrub_page_put(sbio->pagev[i]);
  1516. bio_put(sbio->bio);
  1517. kfree(sbio);
  1518. scrub_pending_bio_dec(sctx);
  1519. }
  1520. static int scrub_checksum(struct scrub_block *sblock)
  1521. {
  1522. u64 flags;
  1523. int ret;
  1524. /*
  1525. * No need to initialize these stats currently,
  1526. * because this function only use return value
  1527. * instead of these stats value.
  1528. *
  1529. * Todo:
  1530. * always use stats
  1531. */
  1532. sblock->header_error = 0;
  1533. sblock->generation_error = 0;
  1534. sblock->checksum_error = 0;
  1535. WARN_ON(sblock->page_count < 1);
  1536. flags = sblock->pagev[0]->flags;
  1537. ret = 0;
  1538. if (flags & BTRFS_EXTENT_FLAG_DATA)
  1539. ret = scrub_checksum_data(sblock);
  1540. else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
  1541. ret = scrub_checksum_tree_block(sblock);
  1542. else if (flags & BTRFS_EXTENT_FLAG_SUPER)
  1543. (void)scrub_checksum_super(sblock);
  1544. else
  1545. WARN_ON(1);
  1546. if (ret)
  1547. scrub_handle_errored_block(sblock);
  1548. return ret;
  1549. }
  1550. static int scrub_checksum_data(struct scrub_block *sblock)
  1551. {
  1552. struct scrub_ctx *sctx = sblock->sctx;
  1553. u8 csum[BTRFS_CSUM_SIZE];
  1554. u8 *on_disk_csum;
  1555. struct page *page;
  1556. void *buffer;
  1557. u32 crc = ~(u32)0;
  1558. u64 len;
  1559. int index;
  1560. BUG_ON(sblock->page_count < 1);
  1561. if (!sblock->pagev[0]->have_csum)
  1562. return 0;
  1563. on_disk_csum = sblock->pagev[0]->csum;
  1564. page = sblock->pagev[0]->page;
  1565. buffer = kmap_atomic(page);
  1566. len = sctx->fs_info->sectorsize;
  1567. index = 0;
  1568. for (;;) {
  1569. u64 l = min_t(u64, len, PAGE_SIZE);
  1570. crc = btrfs_csum_data(buffer, crc, l);
  1571. kunmap_atomic(buffer);
  1572. len -= l;
  1573. if (len == 0)
  1574. break;
  1575. index++;
  1576. BUG_ON(index >= sblock->page_count);
  1577. BUG_ON(!sblock->pagev[index]->page);
  1578. page = sblock->pagev[index]->page;
  1579. buffer = kmap_atomic(page);
  1580. }
  1581. btrfs_csum_final(crc, csum);
  1582. if (memcmp(csum, on_disk_csum, sctx->csum_size))
  1583. sblock->checksum_error = 1;
  1584. return sblock->checksum_error;
  1585. }
  1586. static int scrub_checksum_tree_block(struct scrub_block *sblock)
  1587. {
  1588. struct scrub_ctx *sctx = sblock->sctx;
  1589. struct btrfs_header *h;
  1590. struct btrfs_fs_info *fs_info = sctx->fs_info;
  1591. u8 calculated_csum[BTRFS_CSUM_SIZE];
  1592. u8 on_disk_csum[BTRFS_CSUM_SIZE];
  1593. struct page *page;
  1594. void *mapped_buffer;
  1595. u64 mapped_size;
  1596. void *p;
  1597. u32 crc = ~(u32)0;
  1598. u64 len;
  1599. int index;
  1600. BUG_ON(sblock->page_count < 1);
  1601. page = sblock->pagev[0]->page;
  1602. mapped_buffer = kmap_atomic(page);
  1603. h = (struct btrfs_header *)mapped_buffer;
  1604. memcpy(on_disk_csum, h->csum, sctx->csum_size);
  1605. /*
  1606. * we don't use the getter functions here, as we
  1607. * a) don't have an extent buffer and
  1608. * b) the page is already kmapped
  1609. */
  1610. if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h))
  1611. sblock->header_error = 1;
  1612. if (sblock->pagev[0]->generation != btrfs_stack_header_generation(h)) {
  1613. sblock->header_error = 1;
  1614. sblock->generation_error = 1;
  1615. }
  1616. if (!scrub_check_fsid(h->fsid, sblock->pagev[0]))
  1617. sblock->header_error = 1;
  1618. if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
  1619. BTRFS_UUID_SIZE))
  1620. sblock->header_error = 1;
  1621. len = sctx->fs_info->nodesize - BTRFS_CSUM_SIZE;
  1622. mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
  1623. p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
  1624. index = 0;
  1625. for (;;) {
  1626. u64 l = min_t(u64, len, mapped_size);
  1627. crc = btrfs_csum_data(p, crc, l);
  1628. kunmap_atomic(mapped_buffer);
  1629. len -= l;
  1630. if (len == 0)
  1631. break;
  1632. index++;
  1633. BUG_ON(index >= sblock->page_count);
  1634. BUG_ON(!sblock->pagev[index]->page);
  1635. page = sblock->pagev[index]->page;
  1636. mapped_buffer = kmap_atomic(page);
  1637. mapped_size = PAGE_SIZE;
  1638. p = mapped_buffer;
  1639. }
  1640. btrfs_csum_final(crc, calculated_csum);
  1641. if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
  1642. sblock->checksum_error = 1;
  1643. return sblock->header_error || sblock->checksum_error;
  1644. }
  1645. static int scrub_checksum_super(struct scrub_block *sblock)
  1646. {
  1647. struct btrfs_super_block *s;
  1648. struct scrub_ctx *sctx = sblock->sctx;
  1649. u8 calculated_csum[BTRFS_CSUM_SIZE];
  1650. u8 on_disk_csum[BTRFS_CSUM_SIZE];
  1651. struct page *page;
  1652. void *mapped_buffer;
  1653. u64 mapped_size;
  1654. void *p;
  1655. u32 crc = ~(u32)0;
  1656. int fail_gen = 0;
  1657. int fail_cor = 0;
  1658. u64 len;
  1659. int index;
  1660. BUG_ON(sblock->page_count < 1);
  1661. page = sblock->pagev[0]->page;
  1662. mapped_buffer = kmap_atomic(page);
  1663. s = (struct btrfs_super_block *)mapped_buffer;
  1664. memcpy(on_disk_csum, s->csum, sctx->csum_size);
  1665. if (sblock->pagev[0]->logical != btrfs_super_bytenr(s))
  1666. ++fail_cor;
  1667. if (sblock->pagev[0]->generation != btrfs_super_generation(s))
  1668. ++fail_gen;
  1669. if (!scrub_check_fsid(s->fsid, sblock->pagev[0]))
  1670. ++fail_cor;
  1671. len = BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE;
  1672. mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
  1673. p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
  1674. index = 0;
  1675. for (;;) {
  1676. u64 l = min_t(u64, len, mapped_size);
  1677. crc = btrfs_csum_data(p, crc, l);
  1678. kunmap_atomic(mapped_buffer);
  1679. len -= l;
  1680. if (len == 0)
  1681. break;
  1682. index++;
  1683. BUG_ON(index >= sblock->page_count);
  1684. BUG_ON(!sblock->pagev[index]->page);
  1685. page = sblock->pagev[index]->page;
  1686. mapped_buffer = kmap_atomic(page);
  1687. mapped_size = PAGE_SIZE;
  1688. p = mapped_buffer;
  1689. }
  1690. btrfs_csum_final(crc, calculated_csum);
  1691. if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
  1692. ++fail_cor;
  1693. if (fail_cor + fail_gen) {
  1694. /*
  1695. * if we find an error in a super block, we just report it.
  1696. * They will get written with the next transaction commit
  1697. * anyway
  1698. */
  1699. spin_lock(&sctx->stat_lock);
  1700. ++sctx->stat.super_errors;
  1701. spin_unlock(&sctx->stat_lock);
  1702. if (fail_cor)
  1703. btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
  1704. BTRFS_DEV_STAT_CORRUPTION_ERRS);
  1705. else
  1706. btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
  1707. BTRFS_DEV_STAT_GENERATION_ERRS);
  1708. }
  1709. return fail_cor + fail_gen;
  1710. }
  1711. static void scrub_block_get(struct scrub_block *sblock)
  1712. {
  1713. refcount_inc(&sblock->refs);
  1714. }
  1715. static void scrub_block_put(struct scrub_block *sblock)
  1716. {
  1717. if (refcount_dec_and_test(&sblock->refs)) {
  1718. int i;
  1719. if (sblock->sparity)
  1720. scrub_parity_put(sblock->sparity);
  1721. for (i = 0; i < sblock->page_count; i++)
  1722. scrub_page_put(sblock->pagev[i]);
  1723. kfree(sblock);
  1724. }
  1725. }
  1726. static void scrub_page_get(struct scrub_page *spage)
  1727. {
  1728. atomic_inc(&spage->refs);
  1729. }
  1730. static void scrub_page_put(struct scrub_page *spage)
  1731. {
  1732. if (atomic_dec_and_test(&spage->refs)) {
  1733. if (spage->page)
  1734. __free_page(spage->page);
  1735. kfree(spage);
  1736. }
  1737. }
  1738. static void scrub_submit(struct scrub_ctx *sctx)
  1739. {
  1740. struct scrub_bio *sbio;
  1741. if (sctx->curr == -1)
  1742. return;
  1743. sbio = sctx->bios[sctx->curr];
  1744. sctx->curr = -1;
  1745. scrub_pending_bio_inc(sctx);
  1746. btrfsic_submit_bio(sbio->bio);
  1747. }
  1748. static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
  1749. struct scrub_page *spage)
  1750. {
  1751. struct scrub_block *sblock = spage->sblock;
  1752. struct scrub_bio *sbio;
  1753. int ret;
  1754. again:
  1755. /*
  1756. * grab a fresh bio or wait for one to become available
  1757. */
  1758. while (sctx->curr == -1) {
  1759. spin_lock(&sctx->list_lock);
  1760. sctx->curr = sctx->first_free;
  1761. if (sctx->curr != -1) {
  1762. sctx->first_free = sctx->bios[sctx->curr]->next_free;
  1763. sctx->bios[sctx->curr]->next_free = -1;
  1764. sctx->bios[sctx->curr]->page_count = 0;
  1765. spin_unlock(&sctx->list_lock);
  1766. } else {
  1767. spin_unlock(&sctx->list_lock);
  1768. wait_event(sctx->list_wait, sctx->first_free != -1);
  1769. }
  1770. }
  1771. sbio = sctx->bios[sctx->curr];
  1772. if (sbio->page_count == 0) {
  1773. struct bio *bio;
  1774. sbio->physical = spage->physical;
  1775. sbio->logical = spage->logical;
  1776. sbio->dev = spage->dev;
  1777. bio = sbio->bio;
  1778. if (!bio) {
  1779. bio = btrfs_io_bio_alloc(sctx->pages_per_rd_bio);
  1780. sbio->bio = bio;
  1781. }
  1782. bio->bi_private = sbio;
  1783. bio->bi_end_io = scrub_bio_end_io;
  1784. bio_set_dev(bio, sbio->dev->bdev);
  1785. bio->bi_iter.bi_sector = sbio->physical >> 9;
  1786. bio->bi_opf = REQ_OP_READ;
  1787. sbio->status = 0;
  1788. } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
  1789. spage->physical ||
  1790. sbio->logical + sbio->page_count * PAGE_SIZE !=
  1791. spage->logical ||
  1792. sbio->dev != spage->dev) {
  1793. scrub_submit(sctx);
  1794. goto again;
  1795. }
  1796. sbio->pagev[sbio->page_count] = spage;
  1797. ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
  1798. if (ret != PAGE_SIZE) {
  1799. if (sbio->page_count < 1) {
  1800. bio_put(sbio->bio);
  1801. sbio->bio = NULL;
  1802. return -EIO;
  1803. }
  1804. scrub_submit(sctx);
  1805. goto again;
  1806. }
  1807. scrub_block_get(sblock); /* one for the page added to the bio */
  1808. atomic_inc(&sblock->outstanding_pages);
  1809. sbio->page_count++;
  1810. if (sbio->page_count == sctx->pages_per_rd_bio)
  1811. scrub_submit(sctx);
  1812. return 0;
  1813. }
  1814. static void scrub_missing_raid56_end_io(struct bio *bio)
  1815. {
  1816. struct scrub_block *sblock = bio->bi_private;
  1817. struct btrfs_fs_info *fs_info = sblock->sctx->fs_info;
  1818. if (bio->bi_status)
  1819. sblock->no_io_error_seen = 0;
  1820. bio_put(bio);
  1821. btrfs_queue_work(fs_info->scrub_workers, &sblock->work);
  1822. }
  1823. static void scrub_missing_raid56_worker(struct btrfs_work *work)
  1824. {
  1825. struct scrub_block *sblock = container_of(work, struct scrub_block, work);
  1826. struct scrub_ctx *sctx = sblock->sctx;
  1827. struct btrfs_fs_info *fs_info = sctx->fs_info;
  1828. u64 logical;
  1829. struct btrfs_device *dev;
  1830. logical = sblock->pagev[0]->logical;
  1831. dev = sblock->pagev[0]->dev;
  1832. if (sblock->no_io_error_seen)
  1833. scrub_recheck_block_checksum(sblock);
  1834. if (!sblock->no_io_error_seen) {
  1835. spin_lock(&sctx->stat_lock);
  1836. sctx->stat.read_errors++;
  1837. spin_unlock(&sctx->stat_lock);
  1838. btrfs_err_rl_in_rcu(fs_info,
  1839. "IO error rebuilding logical %llu for dev %s",
  1840. logical, rcu_str_deref(dev->name));
  1841. } else if (sblock->header_error || sblock->checksum_error) {
  1842. spin_lock(&sctx->stat_lock);
  1843. sctx->stat.uncorrectable_errors++;
  1844. spin_unlock(&sctx->stat_lock);
  1845. btrfs_err_rl_in_rcu(fs_info,
  1846. "failed to rebuild valid logical %llu for dev %s",
  1847. logical, rcu_str_deref(dev->name));
  1848. } else {
  1849. scrub_write_block_to_dev_replace(sblock);
  1850. }
  1851. scrub_block_put(sblock);
  1852. if (sctx->is_dev_replace && sctx->flush_all_writes) {
  1853. mutex_lock(&sctx->wr_lock);
  1854. scrub_wr_submit(sctx);
  1855. mutex_unlock(&sctx->wr_lock);
  1856. }
  1857. scrub_pending_bio_dec(sctx);
  1858. }
  1859. static void scrub_missing_raid56_pages(struct scrub_block *sblock)
  1860. {
  1861. struct scrub_ctx *sctx = sblock->sctx;
  1862. struct btrfs_fs_info *fs_info = sctx->fs_info;
  1863. u64 length = sblock->page_count * PAGE_SIZE;
  1864. u64 logical = sblock->pagev[0]->logical;
  1865. struct btrfs_bio *bbio = NULL;
  1866. struct bio *bio;
  1867. struct btrfs_raid_bio *rbio;
  1868. int ret;
  1869. int i;
  1870. btrfs_bio_counter_inc_blocked(fs_info);
  1871. ret = btrfs_map_sblock(fs_info, BTRFS_MAP_GET_READ_MIRRORS, logical,
  1872. &length, &bbio);
  1873. if (ret || !bbio || !bbio->raid_map)
  1874. goto bbio_out;
  1875. if (WARN_ON(!sctx->is_dev_replace ||
  1876. !(bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK))) {
  1877. /*
  1878. * We shouldn't be scrubbing a missing device. Even for dev
  1879. * replace, we should only get here for RAID 5/6. We either
  1880. * managed to mount something with no mirrors remaining or
  1881. * there's a bug in scrub_remap_extent()/btrfs_map_block().
  1882. */
  1883. goto bbio_out;
  1884. }
  1885. bio = btrfs_io_bio_alloc(0);
  1886. bio->bi_iter.bi_sector = logical >> 9;
  1887. bio->bi_private = sblock;
  1888. bio->bi_end_io = scrub_missing_raid56_end_io;
  1889. rbio = raid56_alloc_missing_rbio(fs_info, bio, bbio, length);
  1890. if (!rbio)
  1891. goto rbio_out;
  1892. for (i = 0; i < sblock->page_count; i++) {
  1893. struct scrub_page *spage = sblock->pagev[i];
  1894. raid56_add_scrub_pages(rbio, spage->page, spage->logical);
  1895. }
  1896. btrfs_init_work(&sblock->work, btrfs_scrub_helper,
  1897. scrub_missing_raid56_worker, NULL, NULL);
  1898. scrub_block_get(sblock);
  1899. scrub_pending_bio_inc(sctx);
  1900. raid56_submit_missing_rbio(rbio);
  1901. return;
  1902. rbio_out:
  1903. bio_put(bio);
  1904. bbio_out:
  1905. btrfs_bio_counter_dec(fs_info);
  1906. btrfs_put_bbio(bbio);
  1907. spin_lock(&sctx->stat_lock);
  1908. sctx->stat.malloc_errors++;
  1909. spin_unlock(&sctx->stat_lock);
  1910. }
  1911. static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
  1912. u64 physical, struct btrfs_device *dev, u64 flags,
  1913. u64 gen, int mirror_num, u8 *csum, int force,
  1914. u64 physical_for_dev_replace)
  1915. {
  1916. struct scrub_block *sblock;
  1917. int index;
  1918. sblock = kzalloc(sizeof(*sblock), GFP_KERNEL);
  1919. if (!sblock) {
  1920. spin_lock(&sctx->stat_lock);
  1921. sctx->stat.malloc_errors++;
  1922. spin_unlock(&sctx->stat_lock);
  1923. return -ENOMEM;
  1924. }
  1925. /* one ref inside this function, plus one for each page added to
  1926. * a bio later on */
  1927. refcount_set(&sblock->refs, 1);
  1928. sblock->sctx = sctx;
  1929. sblock->no_io_error_seen = 1;
  1930. for (index = 0; len > 0; index++) {
  1931. struct scrub_page *spage;
  1932. u64 l = min_t(u64, len, PAGE_SIZE);
  1933. spage = kzalloc(sizeof(*spage), GFP_KERNEL);
  1934. if (!spage) {
  1935. leave_nomem:
  1936. spin_lock(&sctx->stat_lock);
  1937. sctx->stat.malloc_errors++;
  1938. spin_unlock(&sctx->stat_lock);
  1939. scrub_block_put(sblock);
  1940. return -ENOMEM;
  1941. }
  1942. BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
  1943. scrub_page_get(spage);
  1944. sblock->pagev[index] = spage;
  1945. spage->sblock = sblock;
  1946. spage->dev = dev;
  1947. spage->flags = flags;
  1948. spage->generation = gen;
  1949. spage->logical = logical;
  1950. spage->physical = physical;
  1951. spage->physical_for_dev_replace = physical_for_dev_replace;
  1952. spage->mirror_num = mirror_num;
  1953. if (csum) {
  1954. spage->have_csum = 1;
  1955. memcpy(spage->csum, csum, sctx->csum_size);
  1956. } else {
  1957. spage->have_csum = 0;
  1958. }
  1959. sblock->page_count++;
  1960. spage->page = alloc_page(GFP_KERNEL);
  1961. if (!spage->page)
  1962. goto leave_nomem;
  1963. len -= l;
  1964. logical += l;
  1965. physical += l;
  1966. physical_for_dev_replace += l;
  1967. }
  1968. WARN_ON(sblock->page_count == 0);
  1969. if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) {
  1970. /*
  1971. * This case should only be hit for RAID 5/6 device replace. See
  1972. * the comment in scrub_missing_raid56_pages() for details.
  1973. */
  1974. scrub_missing_raid56_pages(sblock);
  1975. } else {
  1976. for (index = 0; index < sblock->page_count; index++) {
  1977. struct scrub_page *spage = sblock->pagev[index];
  1978. int ret;
  1979. ret = scrub_add_page_to_rd_bio(sctx, spage);
  1980. if (ret) {
  1981. scrub_block_put(sblock);
  1982. return ret;
  1983. }
  1984. }
  1985. if (force)
  1986. scrub_submit(sctx);
  1987. }
  1988. /* last one frees, either here or in bio completion for last page */
  1989. scrub_block_put(sblock);
  1990. return 0;
  1991. }
  1992. static void scrub_bio_end_io(struct bio *bio)
  1993. {
  1994. struct scrub_bio *sbio = bio->bi_private;
  1995. struct btrfs_fs_info *fs_info = sbio->dev->fs_info;
  1996. sbio->status = bio->bi_status;
  1997. sbio->bio = bio;
  1998. btrfs_queue_work(fs_info->scrub_workers, &sbio->work);
  1999. }
  2000. static void scrub_bio_end_io_worker(struct btrfs_work *work)
  2001. {
  2002. struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
  2003. struct scrub_ctx *sctx = sbio->sctx;
  2004. int i;
  2005. BUG_ON(sbio->page_count > SCRUB_PAGES_PER_RD_BIO);
  2006. if (sbio->status) {
  2007. for (i = 0; i < sbio->page_count; i++) {
  2008. struct scrub_page *spage = sbio->pagev[i];
  2009. spage->io_error = 1;
  2010. spage->sblock->no_io_error_seen = 0;
  2011. }
  2012. }
  2013. /* now complete the scrub_block items that have all pages completed */
  2014. for (i = 0; i < sbio->page_count; i++) {
  2015. struct scrub_page *spage = sbio->pagev[i];
  2016. struct scrub_block *sblock = spage->sblock;
  2017. if (atomic_dec_and_test(&sblock->outstanding_pages))
  2018. scrub_block_complete(sblock);
  2019. scrub_block_put(sblock);
  2020. }
  2021. bio_put(sbio->bio);
  2022. sbio->bio = NULL;
  2023. spin_lock(&sctx->list_lock);
  2024. sbio->next_free = sctx->first_free;
  2025. sctx->first_free = sbio->index;
  2026. spin_unlock(&sctx->list_lock);
  2027. if (sctx->is_dev_replace && sctx->flush_all_writes) {
  2028. mutex_lock(&sctx->wr_lock);
  2029. scrub_wr_submit(sctx);
  2030. mutex_unlock(&sctx->wr_lock);
  2031. }
  2032. scrub_pending_bio_dec(sctx);
  2033. }
  2034. static inline void __scrub_mark_bitmap(struct scrub_parity *sparity,
  2035. unsigned long *bitmap,
  2036. u64 start, u64 len)
  2037. {
  2038. u64 offset;
  2039. u64 nsectors64;
  2040. u32 nsectors;
  2041. int sectorsize = sparity->sctx->fs_info->sectorsize;
  2042. if (len >= sparity->stripe_len) {
  2043. bitmap_set(bitmap, 0, sparity->nsectors);
  2044. return;
  2045. }
  2046. start -= sparity->logic_start;
  2047. start = div64_u64_rem(start, sparity->stripe_len, &offset);
  2048. offset = div_u64(offset, sectorsize);
  2049. nsectors64 = div_u64(len, sectorsize);
  2050. ASSERT(nsectors64 < UINT_MAX);
  2051. nsectors = (u32)nsectors64;
  2052. if (offset + nsectors <= sparity->nsectors) {
  2053. bitmap_set(bitmap, offset, nsectors);
  2054. return;
  2055. }
  2056. bitmap_set(bitmap, offset, sparity->nsectors - offset);
  2057. bitmap_set(bitmap, 0, nsectors - (sparity->nsectors - offset));
  2058. }
  2059. static inline void scrub_parity_mark_sectors_error(struct scrub_parity *sparity,
  2060. u64 start, u64 len)
  2061. {
  2062. __scrub_mark_bitmap(sparity, sparity->ebitmap, start, len);
  2063. }
  2064. static inline void scrub_parity_mark_sectors_data(struct scrub_parity *sparity,
  2065. u64 start, u64 len)
  2066. {
  2067. __scrub_mark_bitmap(sparity, sparity->dbitmap, start, len);
  2068. }
  2069. static void scrub_block_complete(struct scrub_block *sblock)
  2070. {
  2071. int corrupted = 0;
  2072. if (!sblock->no_io_error_seen) {
  2073. corrupted = 1;
  2074. scrub_handle_errored_block(sblock);
  2075. } else {
  2076. /*
  2077. * if has checksum error, write via repair mechanism in
  2078. * dev replace case, otherwise write here in dev replace
  2079. * case.
  2080. */
  2081. corrupted = scrub_checksum(sblock);
  2082. if (!corrupted && sblock->sctx->is_dev_replace)
  2083. scrub_write_block_to_dev_replace(sblock);
  2084. }
  2085. if (sblock->sparity && corrupted && !sblock->data_corrected) {
  2086. u64 start = sblock->pagev[0]->logical;
  2087. u64 end = sblock->pagev[sblock->page_count - 1]->logical +
  2088. PAGE_SIZE;
  2089. scrub_parity_mark_sectors_error(sblock->sparity,
  2090. start, end - start);
  2091. }
  2092. }
  2093. static int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u8 *csum)
  2094. {
  2095. struct btrfs_ordered_sum *sum = NULL;
  2096. unsigned long index;
  2097. unsigned long num_sectors;
  2098. while (!list_empty(&sctx->csum_list)) {
  2099. sum = list_first_entry(&sctx->csum_list,
  2100. struct btrfs_ordered_sum, list);
  2101. if (sum->bytenr > logical)
  2102. return 0;
  2103. if (sum->bytenr + sum->len > logical)
  2104. break;
  2105. ++sctx->stat.csum_discards;
  2106. list_del(&sum->list);
  2107. kfree(sum);
  2108. sum = NULL;
  2109. }
  2110. if (!sum)
  2111. return 0;
  2112. index = div_u64(logical - sum->bytenr, sctx->fs_info->sectorsize);
  2113. ASSERT(index < UINT_MAX);
  2114. num_sectors = sum->len / sctx->fs_info->sectorsize;
  2115. memcpy(csum, sum->sums + index, sctx->csum_size);
  2116. if (index == num_sectors - 1) {
  2117. list_del(&sum->list);
  2118. kfree(sum);
  2119. }
  2120. return 1;
  2121. }
  2122. /* scrub extent tries to collect up to 64 kB for each bio */
  2123. static int scrub_extent(struct scrub_ctx *sctx, struct map_lookup *map,
  2124. u64 logical, u64 len,
  2125. u64 physical, struct btrfs_device *dev, u64 flags,
  2126. u64 gen, int mirror_num, u64 physical_for_dev_replace)
  2127. {
  2128. int ret;
  2129. u8 csum[BTRFS_CSUM_SIZE];
  2130. u32 blocksize;
  2131. if (flags & BTRFS_EXTENT_FLAG_DATA) {
  2132. if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
  2133. blocksize = map->stripe_len;
  2134. else
  2135. blocksize = sctx->fs_info->sectorsize;
  2136. spin_lock(&sctx->stat_lock);
  2137. sctx->stat.data_extents_scrubbed++;
  2138. sctx->stat.data_bytes_scrubbed += len;
  2139. spin_unlock(&sctx->stat_lock);
  2140. } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  2141. if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
  2142. blocksize = map->stripe_len;
  2143. else
  2144. blocksize = sctx->fs_info->nodesize;
  2145. spin_lock(&sctx->stat_lock);
  2146. sctx->stat.tree_extents_scrubbed++;
  2147. sctx->stat.tree_bytes_scrubbed += len;
  2148. spin_unlock(&sctx->stat_lock);
  2149. } else {
  2150. blocksize = sctx->fs_info->sectorsize;
  2151. WARN_ON(1);
  2152. }
  2153. while (len) {
  2154. u64 l = min_t(u64, len, blocksize);
  2155. int have_csum = 0;
  2156. if (flags & BTRFS_EXTENT_FLAG_DATA) {
  2157. /* push csums to sbio */
  2158. have_csum = scrub_find_csum(sctx, logical, csum);
  2159. if (have_csum == 0)
  2160. ++sctx->stat.no_csum;
  2161. }
  2162. ret = scrub_pages(sctx, logical, l, physical, dev, flags, gen,
  2163. mirror_num, have_csum ? csum : NULL, 0,
  2164. physical_for_dev_replace);
  2165. if (ret)
  2166. return ret;
  2167. len -= l;
  2168. logical += l;
  2169. physical += l;
  2170. physical_for_dev_replace += l;
  2171. }
  2172. return 0;
  2173. }
  2174. static int scrub_pages_for_parity(struct scrub_parity *sparity,
  2175. u64 logical, u64 len,
  2176. u64 physical, struct btrfs_device *dev,
  2177. u64 flags, u64 gen, int mirror_num, u8 *csum)
  2178. {
  2179. struct scrub_ctx *sctx = sparity->sctx;
  2180. struct scrub_block *sblock;
  2181. int index;
  2182. sblock = kzalloc(sizeof(*sblock), GFP_KERNEL);
  2183. if (!sblock) {
  2184. spin_lock(&sctx->stat_lock);
  2185. sctx->stat.malloc_errors++;
  2186. spin_unlock(&sctx->stat_lock);
  2187. return -ENOMEM;
  2188. }
  2189. /* one ref inside this function, plus one for each page added to
  2190. * a bio later on */
  2191. refcount_set(&sblock->refs, 1);
  2192. sblock->sctx = sctx;
  2193. sblock->no_io_error_seen = 1;
  2194. sblock->sparity = sparity;
  2195. scrub_parity_get(sparity);
  2196. for (index = 0; len > 0; index++) {
  2197. struct scrub_page *spage;
  2198. u64 l = min_t(u64, len, PAGE_SIZE);
  2199. spage = kzalloc(sizeof(*spage), GFP_KERNEL);
  2200. if (!spage) {
  2201. leave_nomem:
  2202. spin_lock(&sctx->stat_lock);
  2203. sctx->stat.malloc_errors++;
  2204. spin_unlock(&sctx->stat_lock);
  2205. scrub_block_put(sblock);
  2206. return -ENOMEM;
  2207. }
  2208. BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
  2209. /* For scrub block */
  2210. scrub_page_get(spage);
  2211. sblock->pagev[index] = spage;
  2212. /* For scrub parity */
  2213. scrub_page_get(spage);
  2214. list_add_tail(&spage->list, &sparity->spages);
  2215. spage->sblock = sblock;
  2216. spage->dev = dev;
  2217. spage->flags = flags;
  2218. spage->generation = gen;
  2219. spage->logical = logical;
  2220. spage->physical = physical;
  2221. spage->mirror_num = mirror_num;
  2222. if (csum) {
  2223. spage->have_csum = 1;
  2224. memcpy(spage->csum, csum, sctx->csum_size);
  2225. } else {
  2226. spage->have_csum = 0;
  2227. }
  2228. sblock->page_count++;
  2229. spage->page = alloc_page(GFP_KERNEL);
  2230. if (!spage->page)
  2231. goto leave_nomem;
  2232. len -= l;
  2233. logical += l;
  2234. physical += l;
  2235. }
  2236. WARN_ON(sblock->page_count == 0);
  2237. for (index = 0; index < sblock->page_count; index++) {
  2238. struct scrub_page *spage = sblock->pagev[index];
  2239. int ret;
  2240. ret = scrub_add_page_to_rd_bio(sctx, spage);
  2241. if (ret) {
  2242. scrub_block_put(sblock);
  2243. return ret;
  2244. }
  2245. }
  2246. /* last one frees, either here or in bio completion for last page */
  2247. scrub_block_put(sblock);
  2248. return 0;
  2249. }
  2250. static int scrub_extent_for_parity(struct scrub_parity *sparity,
  2251. u64 logical, u64 len,
  2252. u64 physical, struct btrfs_device *dev,
  2253. u64 flags, u64 gen, int mirror_num)
  2254. {
  2255. struct scrub_ctx *sctx = sparity->sctx;
  2256. int ret;
  2257. u8 csum[BTRFS_CSUM_SIZE];
  2258. u32 blocksize;
  2259. if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) {
  2260. scrub_parity_mark_sectors_error(sparity, logical, len);
  2261. return 0;
  2262. }
  2263. if (flags & BTRFS_EXTENT_FLAG_DATA) {
  2264. blocksize = sparity->stripe_len;
  2265. } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  2266. blocksize = sparity->stripe_len;
  2267. } else {
  2268. blocksize = sctx->fs_info->sectorsize;
  2269. WARN_ON(1);
  2270. }
  2271. while (len) {
  2272. u64 l = min_t(u64, len, blocksize);
  2273. int have_csum = 0;
  2274. if (flags & BTRFS_EXTENT_FLAG_DATA) {
  2275. /* push csums to sbio */
  2276. have_csum = scrub_find_csum(sctx, logical, csum);
  2277. if (have_csum == 0)
  2278. goto skip;
  2279. }
  2280. ret = scrub_pages_for_parity(sparity, logical, l, physical, dev,
  2281. flags, gen, mirror_num,
  2282. have_csum ? csum : NULL);
  2283. if (ret)
  2284. return ret;
  2285. skip:
  2286. len -= l;
  2287. logical += l;
  2288. physical += l;
  2289. }
  2290. return 0;
  2291. }
  2292. /*
  2293. * Given a physical address, this will calculate it's
  2294. * logical offset. if this is a parity stripe, it will return
  2295. * the most left data stripe's logical offset.
  2296. *
  2297. * return 0 if it is a data stripe, 1 means parity stripe.
  2298. */
  2299. static int get_raid56_logic_offset(u64 physical, int num,
  2300. struct map_lookup *map, u64 *offset,
  2301. u64 *stripe_start)
  2302. {
  2303. int i;
  2304. int j = 0;
  2305. u64 stripe_nr;
  2306. u64 last_offset;
  2307. u32 stripe_index;
  2308. u32 rot;
  2309. last_offset = (physical - map->stripes[num].physical) *
  2310. nr_data_stripes(map);
  2311. if (stripe_start)
  2312. *stripe_start = last_offset;
  2313. *offset = last_offset;
  2314. for (i = 0; i < nr_data_stripes(map); i++) {
  2315. *offset = last_offset + i * map->stripe_len;
  2316. stripe_nr = div64_u64(*offset, map->stripe_len);
  2317. stripe_nr = div_u64(stripe_nr, nr_data_stripes(map));
  2318. /* Work out the disk rotation on this stripe-set */
  2319. stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, &rot);
  2320. /* calculate which stripe this data locates */
  2321. rot += i;
  2322. stripe_index = rot % map->num_stripes;
  2323. if (stripe_index == num)
  2324. return 0;
  2325. if (stripe_index < num)
  2326. j++;
  2327. }
  2328. *offset = last_offset + j * map->stripe_len;
  2329. return 1;
  2330. }
  2331. static void scrub_free_parity(struct scrub_parity *sparity)
  2332. {
  2333. struct scrub_ctx *sctx = sparity->sctx;
  2334. struct scrub_page *curr, *next;
  2335. int nbits;
  2336. nbits = bitmap_weight(sparity->ebitmap, sparity->nsectors);
  2337. if (nbits) {
  2338. spin_lock(&sctx->stat_lock);
  2339. sctx->stat.read_errors += nbits;
  2340. sctx->stat.uncorrectable_errors += nbits;
  2341. spin_unlock(&sctx->stat_lock);
  2342. }
  2343. list_for_each_entry_safe(curr, next, &sparity->spages, list) {
  2344. list_del_init(&curr->list);
  2345. scrub_page_put(curr);
  2346. }
  2347. kfree(sparity);
  2348. }
  2349. static void scrub_parity_bio_endio_worker(struct btrfs_work *work)
  2350. {
  2351. struct scrub_parity *sparity = container_of(work, struct scrub_parity,
  2352. work);
  2353. struct scrub_ctx *sctx = sparity->sctx;
  2354. scrub_free_parity(sparity);
  2355. scrub_pending_bio_dec(sctx);
  2356. }
  2357. static void scrub_parity_bio_endio(struct bio *bio)
  2358. {
  2359. struct scrub_parity *sparity = (struct scrub_parity *)bio->bi_private;
  2360. struct btrfs_fs_info *fs_info = sparity->sctx->fs_info;
  2361. if (bio->bi_status)
  2362. bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
  2363. sparity->nsectors);
  2364. bio_put(bio);
  2365. btrfs_init_work(&sparity->work, btrfs_scrubparity_helper,
  2366. scrub_parity_bio_endio_worker, NULL, NULL);
  2367. btrfs_queue_work(fs_info->scrub_parity_workers, &sparity->work);
  2368. }
  2369. static void scrub_parity_check_and_repair(struct scrub_parity *sparity)
  2370. {
  2371. struct scrub_ctx *sctx = sparity->sctx;
  2372. struct btrfs_fs_info *fs_info = sctx->fs_info;
  2373. struct bio *bio;
  2374. struct btrfs_raid_bio *rbio;
  2375. struct btrfs_bio *bbio = NULL;
  2376. u64 length;
  2377. int ret;
  2378. if (!bitmap_andnot(sparity->dbitmap, sparity->dbitmap, sparity->ebitmap,
  2379. sparity->nsectors))
  2380. goto out;
  2381. length = sparity->logic_end - sparity->logic_start;
  2382. btrfs_bio_counter_inc_blocked(fs_info);
  2383. ret = btrfs_map_sblock(fs_info, BTRFS_MAP_WRITE, sparity->logic_start,
  2384. &length, &bbio);
  2385. if (ret || !bbio || !bbio->raid_map)
  2386. goto bbio_out;
  2387. bio = btrfs_io_bio_alloc(0);
  2388. bio->bi_iter.bi_sector = sparity->logic_start >> 9;
  2389. bio->bi_private = sparity;
  2390. bio->bi_end_io = scrub_parity_bio_endio;
  2391. rbio = raid56_parity_alloc_scrub_rbio(fs_info, bio, bbio,
  2392. length, sparity->scrub_dev,
  2393. sparity->dbitmap,
  2394. sparity->nsectors);
  2395. if (!rbio)
  2396. goto rbio_out;
  2397. scrub_pending_bio_inc(sctx);
  2398. raid56_parity_submit_scrub_rbio(rbio);
  2399. return;
  2400. rbio_out:
  2401. bio_put(bio);
  2402. bbio_out:
  2403. btrfs_bio_counter_dec(fs_info);
  2404. btrfs_put_bbio(bbio);
  2405. bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
  2406. sparity->nsectors);
  2407. spin_lock(&sctx->stat_lock);
  2408. sctx->stat.malloc_errors++;
  2409. spin_unlock(&sctx->stat_lock);
  2410. out:
  2411. scrub_free_parity(sparity);
  2412. }
  2413. static inline int scrub_calc_parity_bitmap_len(int nsectors)
  2414. {
  2415. return DIV_ROUND_UP(nsectors, BITS_PER_LONG) * sizeof(long);
  2416. }
  2417. static void scrub_parity_get(struct scrub_parity *sparity)
  2418. {
  2419. refcount_inc(&sparity->refs);
  2420. }
  2421. static void scrub_parity_put(struct scrub_parity *sparity)
  2422. {
  2423. if (!refcount_dec_and_test(&sparity->refs))
  2424. return;
  2425. scrub_parity_check_and_repair(sparity);
  2426. }
  2427. static noinline_for_stack int scrub_raid56_parity(struct scrub_ctx *sctx,
  2428. struct map_lookup *map,
  2429. struct btrfs_device *sdev,
  2430. struct btrfs_path *path,
  2431. u64 logic_start,
  2432. u64 logic_end)
  2433. {
  2434. struct btrfs_fs_info *fs_info = sctx->fs_info;
  2435. struct btrfs_root *root = fs_info->extent_root;
  2436. struct btrfs_root *csum_root = fs_info->csum_root;
  2437. struct btrfs_extent_item *extent;
  2438. struct btrfs_bio *bbio = NULL;
  2439. u64 flags;
  2440. int ret;
  2441. int slot;
  2442. struct extent_buffer *l;
  2443. struct btrfs_key key;
  2444. u64 generation;
  2445. u64 extent_logical;
  2446. u64 extent_physical;
  2447. u64 extent_len;
  2448. u64 mapped_length;
  2449. struct btrfs_device *extent_dev;
  2450. struct scrub_parity *sparity;
  2451. int nsectors;
  2452. int bitmap_len;
  2453. int extent_mirror_num;
  2454. int stop_loop = 0;
  2455. nsectors = div_u64(map->stripe_len, fs_info->sectorsize);
  2456. bitmap_len = scrub_calc_parity_bitmap_len(nsectors);
  2457. sparity = kzalloc(sizeof(struct scrub_parity) + 2 * bitmap_len,
  2458. GFP_NOFS);
  2459. if (!sparity) {
  2460. spin_lock(&sctx->stat_lock);
  2461. sctx->stat.malloc_errors++;
  2462. spin_unlock(&sctx->stat_lock);
  2463. return -ENOMEM;
  2464. }
  2465. sparity->stripe_len = map->stripe_len;
  2466. sparity->nsectors = nsectors;
  2467. sparity->sctx = sctx;
  2468. sparity->scrub_dev = sdev;
  2469. sparity->logic_start = logic_start;
  2470. sparity->logic_end = logic_end;
  2471. refcount_set(&sparity->refs, 1);
  2472. INIT_LIST_HEAD(&sparity->spages);
  2473. sparity->dbitmap = sparity->bitmap;
  2474. sparity->ebitmap = (void *)sparity->bitmap + bitmap_len;
  2475. ret = 0;
  2476. while (logic_start < logic_end) {
  2477. if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
  2478. key.type = BTRFS_METADATA_ITEM_KEY;
  2479. else
  2480. key.type = BTRFS_EXTENT_ITEM_KEY;
  2481. key.objectid = logic_start;
  2482. key.offset = (u64)-1;
  2483. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2484. if (ret < 0)
  2485. goto out;
  2486. if (ret > 0) {
  2487. ret = btrfs_previous_extent_item(root, path, 0);
  2488. if (ret < 0)
  2489. goto out;
  2490. if (ret > 0) {
  2491. btrfs_release_path(path);
  2492. ret = btrfs_search_slot(NULL, root, &key,
  2493. path, 0, 0);
  2494. if (ret < 0)
  2495. goto out;
  2496. }
  2497. }
  2498. stop_loop = 0;
  2499. while (1) {
  2500. u64 bytes;
  2501. l = path->nodes[0];
  2502. slot = path->slots[0];
  2503. if (slot >= btrfs_header_nritems(l)) {
  2504. ret = btrfs_next_leaf(root, path);
  2505. if (ret == 0)
  2506. continue;
  2507. if (ret < 0)
  2508. goto out;
  2509. stop_loop = 1;
  2510. break;
  2511. }
  2512. btrfs_item_key_to_cpu(l, &key, slot);
  2513. if (key.type != BTRFS_EXTENT_ITEM_KEY &&
  2514. key.type != BTRFS_METADATA_ITEM_KEY)
  2515. goto next;
  2516. if (key.type == BTRFS_METADATA_ITEM_KEY)
  2517. bytes = fs_info->nodesize;
  2518. else
  2519. bytes = key.offset;
  2520. if (key.objectid + bytes <= logic_start)
  2521. goto next;
  2522. if (key.objectid >= logic_end) {
  2523. stop_loop = 1;
  2524. break;
  2525. }
  2526. while (key.objectid >= logic_start + map->stripe_len)
  2527. logic_start += map->stripe_len;
  2528. extent = btrfs_item_ptr(l, slot,
  2529. struct btrfs_extent_item);
  2530. flags = btrfs_extent_flags(l, extent);
  2531. generation = btrfs_extent_generation(l, extent);
  2532. if ((flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) &&
  2533. (key.objectid < logic_start ||
  2534. key.objectid + bytes >
  2535. logic_start + map->stripe_len)) {
  2536. btrfs_err(fs_info,
  2537. "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
  2538. key.objectid, logic_start);
  2539. spin_lock(&sctx->stat_lock);
  2540. sctx->stat.uncorrectable_errors++;
  2541. spin_unlock(&sctx->stat_lock);
  2542. goto next;
  2543. }
  2544. again:
  2545. extent_logical = key.objectid;
  2546. extent_len = bytes;
  2547. if (extent_logical < logic_start) {
  2548. extent_len -= logic_start - extent_logical;
  2549. extent_logical = logic_start;
  2550. }
  2551. if (extent_logical + extent_len >
  2552. logic_start + map->stripe_len)
  2553. extent_len = logic_start + map->stripe_len -
  2554. extent_logical;
  2555. scrub_parity_mark_sectors_data(sparity, extent_logical,
  2556. extent_len);
  2557. mapped_length = extent_len;
  2558. bbio = NULL;
  2559. ret = btrfs_map_block(fs_info, BTRFS_MAP_READ,
  2560. extent_logical, &mapped_length, &bbio,
  2561. 0);
  2562. if (!ret) {
  2563. if (!bbio || mapped_length < extent_len)
  2564. ret = -EIO;
  2565. }
  2566. if (ret) {
  2567. btrfs_put_bbio(bbio);
  2568. goto out;
  2569. }
  2570. extent_physical = bbio->stripes[0].physical;
  2571. extent_mirror_num = bbio->mirror_num;
  2572. extent_dev = bbio->stripes[0].dev;
  2573. btrfs_put_bbio(bbio);
  2574. ret = btrfs_lookup_csums_range(csum_root,
  2575. extent_logical,
  2576. extent_logical + extent_len - 1,
  2577. &sctx->csum_list, 1);
  2578. if (ret)
  2579. goto out;
  2580. ret = scrub_extent_for_parity(sparity, extent_logical,
  2581. extent_len,
  2582. extent_physical,
  2583. extent_dev, flags,
  2584. generation,
  2585. extent_mirror_num);
  2586. scrub_free_csums(sctx);
  2587. if (ret)
  2588. goto out;
  2589. if (extent_logical + extent_len <
  2590. key.objectid + bytes) {
  2591. logic_start += map->stripe_len;
  2592. if (logic_start >= logic_end) {
  2593. stop_loop = 1;
  2594. break;
  2595. }
  2596. if (logic_start < key.objectid + bytes) {
  2597. cond_resched();
  2598. goto again;
  2599. }
  2600. }
  2601. next:
  2602. path->slots[0]++;
  2603. }
  2604. btrfs_release_path(path);
  2605. if (stop_loop)
  2606. break;
  2607. logic_start += map->stripe_len;
  2608. }
  2609. out:
  2610. if (ret < 0)
  2611. scrub_parity_mark_sectors_error(sparity, logic_start,
  2612. logic_end - logic_start);
  2613. scrub_parity_put(sparity);
  2614. scrub_submit(sctx);
  2615. mutex_lock(&sctx->wr_lock);
  2616. scrub_wr_submit(sctx);
  2617. mutex_unlock(&sctx->wr_lock);
  2618. btrfs_release_path(path);
  2619. return ret < 0 ? ret : 0;
  2620. }
  2621. static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
  2622. struct map_lookup *map,
  2623. struct btrfs_device *scrub_dev,
  2624. int num, u64 base, u64 length)
  2625. {
  2626. struct btrfs_path *path, *ppath;
  2627. struct btrfs_fs_info *fs_info = sctx->fs_info;
  2628. struct btrfs_root *root = fs_info->extent_root;
  2629. struct btrfs_root *csum_root = fs_info->csum_root;
  2630. struct btrfs_extent_item *extent;
  2631. struct blk_plug plug;
  2632. u64 flags;
  2633. int ret;
  2634. int slot;
  2635. u64 nstripes;
  2636. struct extent_buffer *l;
  2637. u64 physical;
  2638. u64 logical;
  2639. u64 logic_end;
  2640. u64 physical_end;
  2641. u64 generation;
  2642. int mirror_num;
  2643. struct reada_control *reada1;
  2644. struct reada_control *reada2;
  2645. struct btrfs_key key;
  2646. struct btrfs_key key_end;
  2647. u64 increment = map->stripe_len;
  2648. u64 offset;
  2649. u64 extent_logical;
  2650. u64 extent_physical;
  2651. u64 extent_len;
  2652. u64 stripe_logical;
  2653. u64 stripe_end;
  2654. struct btrfs_device *extent_dev;
  2655. int extent_mirror_num;
  2656. int stop_loop = 0;
  2657. physical = map->stripes[num].physical;
  2658. offset = 0;
  2659. nstripes = div64_u64(length, map->stripe_len);
  2660. if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
  2661. offset = map->stripe_len * num;
  2662. increment = map->stripe_len * map->num_stripes;
  2663. mirror_num = 1;
  2664. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  2665. int factor = map->num_stripes / map->sub_stripes;
  2666. offset = map->stripe_len * (num / map->sub_stripes);
  2667. increment = map->stripe_len * factor;
  2668. mirror_num = num % map->sub_stripes + 1;
  2669. } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
  2670. increment = map->stripe_len;
  2671. mirror_num = num % map->num_stripes + 1;
  2672. } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
  2673. increment = map->stripe_len;
  2674. mirror_num = num % map->num_stripes + 1;
  2675. } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
  2676. get_raid56_logic_offset(physical, num, map, &offset, NULL);
  2677. increment = map->stripe_len * nr_data_stripes(map);
  2678. mirror_num = 1;
  2679. } else {
  2680. increment = map->stripe_len;
  2681. mirror_num = 1;
  2682. }
  2683. path = btrfs_alloc_path();
  2684. if (!path)
  2685. return -ENOMEM;
  2686. ppath = btrfs_alloc_path();
  2687. if (!ppath) {
  2688. btrfs_free_path(path);
  2689. return -ENOMEM;
  2690. }
  2691. /*
  2692. * work on commit root. The related disk blocks are static as
  2693. * long as COW is applied. This means, it is save to rewrite
  2694. * them to repair disk errors without any race conditions
  2695. */
  2696. path->search_commit_root = 1;
  2697. path->skip_locking = 1;
  2698. ppath->search_commit_root = 1;
  2699. ppath->skip_locking = 1;
  2700. /*
  2701. * trigger the readahead for extent tree csum tree and wait for
  2702. * completion. During readahead, the scrub is officially paused
  2703. * to not hold off transaction commits
  2704. */
  2705. logical = base + offset;
  2706. physical_end = physical + nstripes * map->stripe_len;
  2707. if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
  2708. get_raid56_logic_offset(physical_end, num,
  2709. map, &logic_end, NULL);
  2710. logic_end += base;
  2711. } else {
  2712. logic_end = logical + increment * nstripes;
  2713. }
  2714. wait_event(sctx->list_wait,
  2715. atomic_read(&sctx->bios_in_flight) == 0);
  2716. scrub_blocked_if_needed(fs_info);
  2717. /* FIXME it might be better to start readahead at commit root */
  2718. key.objectid = logical;
  2719. key.type = BTRFS_EXTENT_ITEM_KEY;
  2720. key.offset = (u64)0;
  2721. key_end.objectid = logic_end;
  2722. key_end.type = BTRFS_METADATA_ITEM_KEY;
  2723. key_end.offset = (u64)-1;
  2724. reada1 = btrfs_reada_add(root, &key, &key_end);
  2725. key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
  2726. key.type = BTRFS_EXTENT_CSUM_KEY;
  2727. key.offset = logical;
  2728. key_end.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
  2729. key_end.type = BTRFS_EXTENT_CSUM_KEY;
  2730. key_end.offset = logic_end;
  2731. reada2 = btrfs_reada_add(csum_root, &key, &key_end);
  2732. if (!IS_ERR(reada1))
  2733. btrfs_reada_wait(reada1);
  2734. if (!IS_ERR(reada2))
  2735. btrfs_reada_wait(reada2);
  2736. /*
  2737. * collect all data csums for the stripe to avoid seeking during
  2738. * the scrub. This might currently (crc32) end up to be about 1MB
  2739. */
  2740. blk_start_plug(&plug);
  2741. /*
  2742. * now find all extents for each stripe and scrub them
  2743. */
  2744. ret = 0;
  2745. while (physical < physical_end) {
  2746. /*
  2747. * canceled?
  2748. */
  2749. if (atomic_read(&fs_info->scrub_cancel_req) ||
  2750. atomic_read(&sctx->cancel_req)) {
  2751. ret = -ECANCELED;
  2752. goto out;
  2753. }
  2754. /*
  2755. * check to see if we have to pause
  2756. */
  2757. if (atomic_read(&fs_info->scrub_pause_req)) {
  2758. /* push queued extents */
  2759. sctx->flush_all_writes = true;
  2760. scrub_submit(sctx);
  2761. mutex_lock(&sctx->wr_lock);
  2762. scrub_wr_submit(sctx);
  2763. mutex_unlock(&sctx->wr_lock);
  2764. wait_event(sctx->list_wait,
  2765. atomic_read(&sctx->bios_in_flight) == 0);
  2766. sctx->flush_all_writes = false;
  2767. scrub_blocked_if_needed(fs_info);
  2768. }
  2769. if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
  2770. ret = get_raid56_logic_offset(physical, num, map,
  2771. &logical,
  2772. &stripe_logical);
  2773. logical += base;
  2774. if (ret) {
  2775. /* it is parity strip */
  2776. stripe_logical += base;
  2777. stripe_end = stripe_logical + increment;
  2778. ret = scrub_raid56_parity(sctx, map, scrub_dev,
  2779. ppath, stripe_logical,
  2780. stripe_end);
  2781. if (ret)
  2782. goto out;
  2783. goto skip;
  2784. }
  2785. }
  2786. if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
  2787. key.type = BTRFS_METADATA_ITEM_KEY;
  2788. else
  2789. key.type = BTRFS_EXTENT_ITEM_KEY;
  2790. key.objectid = logical;
  2791. key.offset = (u64)-1;
  2792. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2793. if (ret < 0)
  2794. goto out;
  2795. if (ret > 0) {
  2796. ret = btrfs_previous_extent_item(root, path, 0);
  2797. if (ret < 0)
  2798. goto out;
  2799. if (ret > 0) {
  2800. /* there's no smaller item, so stick with the
  2801. * larger one */
  2802. btrfs_release_path(path);
  2803. ret = btrfs_search_slot(NULL, root, &key,
  2804. path, 0, 0);
  2805. if (ret < 0)
  2806. goto out;
  2807. }
  2808. }
  2809. stop_loop = 0;
  2810. while (1) {
  2811. u64 bytes;
  2812. l = path->nodes[0];
  2813. slot = path->slots[0];
  2814. if (slot >= btrfs_header_nritems(l)) {
  2815. ret = btrfs_next_leaf(root, path);
  2816. if (ret == 0)
  2817. continue;
  2818. if (ret < 0)
  2819. goto out;
  2820. stop_loop = 1;
  2821. break;
  2822. }
  2823. btrfs_item_key_to_cpu(l, &key, slot);
  2824. if (key.type != BTRFS_EXTENT_ITEM_KEY &&
  2825. key.type != BTRFS_METADATA_ITEM_KEY)
  2826. goto next;
  2827. if (key.type == BTRFS_METADATA_ITEM_KEY)
  2828. bytes = fs_info->nodesize;
  2829. else
  2830. bytes = key.offset;
  2831. if (key.objectid + bytes <= logical)
  2832. goto next;
  2833. if (key.objectid >= logical + map->stripe_len) {
  2834. /* out of this device extent */
  2835. if (key.objectid >= logic_end)
  2836. stop_loop = 1;
  2837. break;
  2838. }
  2839. extent = btrfs_item_ptr(l, slot,
  2840. struct btrfs_extent_item);
  2841. flags = btrfs_extent_flags(l, extent);
  2842. generation = btrfs_extent_generation(l, extent);
  2843. if ((flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) &&
  2844. (key.objectid < logical ||
  2845. key.objectid + bytes >
  2846. logical + map->stripe_len)) {
  2847. btrfs_err(fs_info,
  2848. "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
  2849. key.objectid, logical);
  2850. spin_lock(&sctx->stat_lock);
  2851. sctx->stat.uncorrectable_errors++;
  2852. spin_unlock(&sctx->stat_lock);
  2853. goto next;
  2854. }
  2855. again:
  2856. extent_logical = key.objectid;
  2857. extent_len = bytes;
  2858. /*
  2859. * trim extent to this stripe
  2860. */
  2861. if (extent_logical < logical) {
  2862. extent_len -= logical - extent_logical;
  2863. extent_logical = logical;
  2864. }
  2865. if (extent_logical + extent_len >
  2866. logical + map->stripe_len) {
  2867. extent_len = logical + map->stripe_len -
  2868. extent_logical;
  2869. }
  2870. extent_physical = extent_logical - logical + physical;
  2871. extent_dev = scrub_dev;
  2872. extent_mirror_num = mirror_num;
  2873. if (sctx->is_dev_replace)
  2874. scrub_remap_extent(fs_info, extent_logical,
  2875. extent_len, &extent_physical,
  2876. &extent_dev,
  2877. &extent_mirror_num);
  2878. ret = btrfs_lookup_csums_range(csum_root,
  2879. extent_logical,
  2880. extent_logical +
  2881. extent_len - 1,
  2882. &sctx->csum_list, 1);
  2883. if (ret)
  2884. goto out;
  2885. ret = scrub_extent(sctx, map, extent_logical, extent_len,
  2886. extent_physical, extent_dev, flags,
  2887. generation, extent_mirror_num,
  2888. extent_logical - logical + physical);
  2889. scrub_free_csums(sctx);
  2890. if (ret)
  2891. goto out;
  2892. if (extent_logical + extent_len <
  2893. key.objectid + bytes) {
  2894. if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
  2895. /*
  2896. * loop until we find next data stripe
  2897. * or we have finished all stripes.
  2898. */
  2899. loop:
  2900. physical += map->stripe_len;
  2901. ret = get_raid56_logic_offset(physical,
  2902. num, map, &logical,
  2903. &stripe_logical);
  2904. logical += base;
  2905. if (ret && physical < physical_end) {
  2906. stripe_logical += base;
  2907. stripe_end = stripe_logical +
  2908. increment;
  2909. ret = scrub_raid56_parity(sctx,
  2910. map, scrub_dev, ppath,
  2911. stripe_logical,
  2912. stripe_end);
  2913. if (ret)
  2914. goto out;
  2915. goto loop;
  2916. }
  2917. } else {
  2918. physical += map->stripe_len;
  2919. logical += increment;
  2920. }
  2921. if (logical < key.objectid + bytes) {
  2922. cond_resched();
  2923. goto again;
  2924. }
  2925. if (physical >= physical_end) {
  2926. stop_loop = 1;
  2927. break;
  2928. }
  2929. }
  2930. next:
  2931. path->slots[0]++;
  2932. }
  2933. btrfs_release_path(path);
  2934. skip:
  2935. logical += increment;
  2936. physical += map->stripe_len;
  2937. spin_lock(&sctx->stat_lock);
  2938. if (stop_loop)
  2939. sctx->stat.last_physical = map->stripes[num].physical +
  2940. length;
  2941. else
  2942. sctx->stat.last_physical = physical;
  2943. spin_unlock(&sctx->stat_lock);
  2944. if (stop_loop)
  2945. break;
  2946. }
  2947. out:
  2948. /* push queued extents */
  2949. scrub_submit(sctx);
  2950. mutex_lock(&sctx->wr_lock);
  2951. scrub_wr_submit(sctx);
  2952. mutex_unlock(&sctx->wr_lock);
  2953. blk_finish_plug(&plug);
  2954. btrfs_free_path(path);
  2955. btrfs_free_path(ppath);
  2956. return ret < 0 ? ret : 0;
  2957. }
  2958. static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
  2959. struct btrfs_device *scrub_dev,
  2960. u64 chunk_offset, u64 length,
  2961. u64 dev_offset,
  2962. struct btrfs_block_group_cache *cache)
  2963. {
  2964. struct btrfs_fs_info *fs_info = sctx->fs_info;
  2965. struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
  2966. struct map_lookup *map;
  2967. struct extent_map *em;
  2968. int i;
  2969. int ret = 0;
  2970. read_lock(&map_tree->map_tree.lock);
  2971. em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
  2972. read_unlock(&map_tree->map_tree.lock);
  2973. if (!em) {
  2974. /*
  2975. * Might have been an unused block group deleted by the cleaner
  2976. * kthread or relocation.
  2977. */
  2978. spin_lock(&cache->lock);
  2979. if (!cache->removed)
  2980. ret = -EINVAL;
  2981. spin_unlock(&cache->lock);
  2982. return ret;
  2983. }
  2984. map = em->map_lookup;
  2985. if (em->start != chunk_offset)
  2986. goto out;
  2987. if (em->len < length)
  2988. goto out;
  2989. for (i = 0; i < map->num_stripes; ++i) {
  2990. if (map->stripes[i].dev->bdev == scrub_dev->bdev &&
  2991. map->stripes[i].physical == dev_offset) {
  2992. ret = scrub_stripe(sctx, map, scrub_dev, i,
  2993. chunk_offset, length);
  2994. if (ret)
  2995. goto out;
  2996. }
  2997. }
  2998. out:
  2999. free_extent_map(em);
  3000. return ret;
  3001. }
  3002. static noinline_for_stack
  3003. int scrub_enumerate_chunks(struct scrub_ctx *sctx,
  3004. struct btrfs_device *scrub_dev, u64 start, u64 end)
  3005. {
  3006. struct btrfs_dev_extent *dev_extent = NULL;
  3007. struct btrfs_path *path;
  3008. struct btrfs_fs_info *fs_info = sctx->fs_info;
  3009. struct btrfs_root *root = fs_info->dev_root;
  3010. u64 length;
  3011. u64 chunk_offset;
  3012. int ret = 0;
  3013. int ro_set;
  3014. int slot;
  3015. struct extent_buffer *l;
  3016. struct btrfs_key key;
  3017. struct btrfs_key found_key;
  3018. struct btrfs_block_group_cache *cache;
  3019. struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
  3020. path = btrfs_alloc_path();
  3021. if (!path)
  3022. return -ENOMEM;
  3023. path->reada = READA_FORWARD;
  3024. path->search_commit_root = 1;
  3025. path->skip_locking = 1;
  3026. key.objectid = scrub_dev->devid;
  3027. key.offset = 0ull;
  3028. key.type = BTRFS_DEV_EXTENT_KEY;
  3029. while (1) {
  3030. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3031. if (ret < 0)
  3032. break;
  3033. if (ret > 0) {
  3034. if (path->slots[0] >=
  3035. btrfs_header_nritems(path->nodes[0])) {
  3036. ret = btrfs_next_leaf(root, path);
  3037. if (ret < 0)
  3038. break;
  3039. if (ret > 0) {
  3040. ret = 0;
  3041. break;
  3042. }
  3043. } else {
  3044. ret = 0;
  3045. }
  3046. }
  3047. l = path->nodes[0];
  3048. slot = path->slots[0];
  3049. btrfs_item_key_to_cpu(l, &found_key, slot);
  3050. if (found_key.objectid != scrub_dev->devid)
  3051. break;
  3052. if (found_key.type != BTRFS_DEV_EXTENT_KEY)
  3053. break;
  3054. if (found_key.offset >= end)
  3055. break;
  3056. if (found_key.offset < key.offset)
  3057. break;
  3058. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  3059. length = btrfs_dev_extent_length(l, dev_extent);
  3060. if (found_key.offset + length <= start)
  3061. goto skip;
  3062. chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
  3063. /*
  3064. * get a reference on the corresponding block group to prevent
  3065. * the chunk from going away while we scrub it
  3066. */
  3067. cache = btrfs_lookup_block_group(fs_info, chunk_offset);
  3068. /* some chunks are removed but not committed to disk yet,
  3069. * continue scrubbing */
  3070. if (!cache)
  3071. goto skip;
  3072. /*
  3073. * we need call btrfs_inc_block_group_ro() with scrubs_paused,
  3074. * to avoid deadlock caused by:
  3075. * btrfs_inc_block_group_ro()
  3076. * -> btrfs_wait_for_commit()
  3077. * -> btrfs_commit_transaction()
  3078. * -> btrfs_scrub_pause()
  3079. */
  3080. scrub_pause_on(fs_info);
  3081. ret = btrfs_inc_block_group_ro(cache);
  3082. if (!ret && sctx->is_dev_replace) {
  3083. /*
  3084. * If we are doing a device replace wait for any tasks
  3085. * that started dellaloc right before we set the block
  3086. * group to RO mode, as they might have just allocated
  3087. * an extent from it or decided they could do a nocow
  3088. * write. And if any such tasks did that, wait for their
  3089. * ordered extents to complete and then commit the
  3090. * current transaction, so that we can later see the new
  3091. * extent items in the extent tree - the ordered extents
  3092. * create delayed data references (for cow writes) when
  3093. * they complete, which will be run and insert the
  3094. * corresponding extent items into the extent tree when
  3095. * we commit the transaction they used when running
  3096. * inode.c:btrfs_finish_ordered_io(). We later use
  3097. * the commit root of the extent tree to find extents
  3098. * to copy from the srcdev into the tgtdev, and we don't
  3099. * want to miss any new extents.
  3100. */
  3101. btrfs_wait_block_group_reservations(cache);
  3102. btrfs_wait_nocow_writers(cache);
  3103. ret = btrfs_wait_ordered_roots(fs_info, U64_MAX,
  3104. cache->key.objectid,
  3105. cache->key.offset);
  3106. if (ret > 0) {
  3107. struct btrfs_trans_handle *trans;
  3108. trans = btrfs_join_transaction(root);
  3109. if (IS_ERR(trans))
  3110. ret = PTR_ERR(trans);
  3111. else
  3112. ret = btrfs_commit_transaction(trans);
  3113. if (ret) {
  3114. scrub_pause_off(fs_info);
  3115. btrfs_put_block_group(cache);
  3116. break;
  3117. }
  3118. }
  3119. }
  3120. scrub_pause_off(fs_info);
  3121. if (ret == 0) {
  3122. ro_set = 1;
  3123. } else if (ret == -ENOSPC) {
  3124. /*
  3125. * btrfs_inc_block_group_ro return -ENOSPC when it
  3126. * failed in creating new chunk for metadata.
  3127. * It is not a problem for scrub/replace, because
  3128. * metadata are always cowed, and our scrub paused
  3129. * commit_transactions.
  3130. */
  3131. ro_set = 0;
  3132. } else {
  3133. btrfs_warn(fs_info,
  3134. "failed setting block group ro: %d", ret);
  3135. btrfs_put_block_group(cache);
  3136. break;
  3137. }
  3138. btrfs_dev_replace_write_lock(&fs_info->dev_replace);
  3139. dev_replace->cursor_right = found_key.offset + length;
  3140. dev_replace->cursor_left = found_key.offset;
  3141. dev_replace->item_needs_writeback = 1;
  3142. btrfs_dev_replace_write_unlock(&fs_info->dev_replace);
  3143. ret = scrub_chunk(sctx, scrub_dev, chunk_offset, length,
  3144. found_key.offset, cache);
  3145. /*
  3146. * flush, submit all pending read and write bios, afterwards
  3147. * wait for them.
  3148. * Note that in the dev replace case, a read request causes
  3149. * write requests that are submitted in the read completion
  3150. * worker. Therefore in the current situation, it is required
  3151. * that all write requests are flushed, so that all read and
  3152. * write requests are really completed when bios_in_flight
  3153. * changes to 0.
  3154. */
  3155. sctx->flush_all_writes = true;
  3156. scrub_submit(sctx);
  3157. mutex_lock(&sctx->wr_lock);
  3158. scrub_wr_submit(sctx);
  3159. mutex_unlock(&sctx->wr_lock);
  3160. wait_event(sctx->list_wait,
  3161. atomic_read(&sctx->bios_in_flight) == 0);
  3162. scrub_pause_on(fs_info);
  3163. /*
  3164. * must be called before we decrease @scrub_paused.
  3165. * make sure we don't block transaction commit while
  3166. * we are waiting pending workers finished.
  3167. */
  3168. wait_event(sctx->list_wait,
  3169. atomic_read(&sctx->workers_pending) == 0);
  3170. sctx->flush_all_writes = false;
  3171. scrub_pause_off(fs_info);
  3172. btrfs_dev_replace_write_lock(&fs_info->dev_replace);
  3173. dev_replace->cursor_left = dev_replace->cursor_right;
  3174. dev_replace->item_needs_writeback = 1;
  3175. btrfs_dev_replace_write_unlock(&fs_info->dev_replace);
  3176. if (ro_set)
  3177. btrfs_dec_block_group_ro(cache);
  3178. /*
  3179. * We might have prevented the cleaner kthread from deleting
  3180. * this block group if it was already unused because we raced
  3181. * and set it to RO mode first. So add it back to the unused
  3182. * list, otherwise it might not ever be deleted unless a manual
  3183. * balance is triggered or it becomes used and unused again.
  3184. */
  3185. spin_lock(&cache->lock);
  3186. if (!cache->removed && !cache->ro && cache->reserved == 0 &&
  3187. btrfs_block_group_used(&cache->item) == 0) {
  3188. spin_unlock(&cache->lock);
  3189. btrfs_mark_bg_unused(cache);
  3190. } else {
  3191. spin_unlock(&cache->lock);
  3192. }
  3193. btrfs_put_block_group(cache);
  3194. if (ret)
  3195. break;
  3196. if (sctx->is_dev_replace &&
  3197. atomic64_read(&dev_replace->num_write_errors) > 0) {
  3198. ret = -EIO;
  3199. break;
  3200. }
  3201. if (sctx->stat.malloc_errors > 0) {
  3202. ret = -ENOMEM;
  3203. break;
  3204. }
  3205. skip:
  3206. key.offset = found_key.offset + length;
  3207. btrfs_release_path(path);
  3208. }
  3209. btrfs_free_path(path);
  3210. return ret;
  3211. }
  3212. static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,
  3213. struct btrfs_device *scrub_dev)
  3214. {
  3215. int i;
  3216. u64 bytenr;
  3217. u64 gen;
  3218. int ret;
  3219. struct btrfs_fs_info *fs_info = sctx->fs_info;
  3220. if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
  3221. return -EIO;
  3222. /* Seed devices of a new filesystem has their own generation. */
  3223. if (scrub_dev->fs_devices != fs_info->fs_devices)
  3224. gen = scrub_dev->generation;
  3225. else
  3226. gen = fs_info->last_trans_committed;
  3227. for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
  3228. bytenr = btrfs_sb_offset(i);
  3229. if (bytenr + BTRFS_SUPER_INFO_SIZE >
  3230. scrub_dev->commit_total_bytes)
  3231. break;
  3232. ret = scrub_pages(sctx, bytenr, BTRFS_SUPER_INFO_SIZE, bytenr,
  3233. scrub_dev, BTRFS_EXTENT_FLAG_SUPER, gen, i,
  3234. NULL, 1, bytenr);
  3235. if (ret)
  3236. return ret;
  3237. }
  3238. wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
  3239. return 0;
  3240. }
  3241. /*
  3242. * get a reference count on fs_info->scrub_workers. start worker if necessary
  3243. */
  3244. static noinline_for_stack int scrub_workers_get(struct btrfs_fs_info *fs_info,
  3245. int is_dev_replace)
  3246. {
  3247. unsigned int flags = WQ_FREEZABLE | WQ_UNBOUND;
  3248. int max_active = fs_info->thread_pool_size;
  3249. if (fs_info->scrub_workers_refcnt == 0) {
  3250. fs_info->scrub_workers = btrfs_alloc_workqueue(fs_info, "scrub",
  3251. flags, is_dev_replace ? 1 : max_active, 4);
  3252. if (!fs_info->scrub_workers)
  3253. goto fail_scrub_workers;
  3254. fs_info->scrub_wr_completion_workers =
  3255. btrfs_alloc_workqueue(fs_info, "scrubwrc", flags,
  3256. max_active, 2);
  3257. if (!fs_info->scrub_wr_completion_workers)
  3258. goto fail_scrub_wr_completion_workers;
  3259. fs_info->scrub_parity_workers =
  3260. btrfs_alloc_workqueue(fs_info, "scrubparity", flags,
  3261. max_active, 2);
  3262. if (!fs_info->scrub_parity_workers)
  3263. goto fail_scrub_parity_workers;
  3264. }
  3265. ++fs_info->scrub_workers_refcnt;
  3266. return 0;
  3267. fail_scrub_parity_workers:
  3268. btrfs_destroy_workqueue(fs_info->scrub_wr_completion_workers);
  3269. fail_scrub_wr_completion_workers:
  3270. btrfs_destroy_workqueue(fs_info->scrub_workers);
  3271. fail_scrub_workers:
  3272. return -ENOMEM;
  3273. }
  3274. static noinline_for_stack void scrub_workers_put(struct btrfs_fs_info *fs_info)
  3275. {
  3276. if (--fs_info->scrub_workers_refcnt == 0) {
  3277. btrfs_destroy_workqueue(fs_info->scrub_workers);
  3278. btrfs_destroy_workqueue(fs_info->scrub_wr_completion_workers);
  3279. btrfs_destroy_workqueue(fs_info->scrub_parity_workers);
  3280. }
  3281. WARN_ON(fs_info->scrub_workers_refcnt < 0);
  3282. }
  3283. int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
  3284. u64 end, struct btrfs_scrub_progress *progress,
  3285. int readonly, int is_dev_replace)
  3286. {
  3287. struct scrub_ctx *sctx;
  3288. int ret;
  3289. struct btrfs_device *dev;
  3290. if (btrfs_fs_closing(fs_info))
  3291. return -EINVAL;
  3292. if (fs_info->nodesize > BTRFS_STRIPE_LEN) {
  3293. /*
  3294. * in this case scrub is unable to calculate the checksum
  3295. * the way scrub is implemented. Do not handle this
  3296. * situation at all because it won't ever happen.
  3297. */
  3298. btrfs_err(fs_info,
  3299. "scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails",
  3300. fs_info->nodesize,
  3301. BTRFS_STRIPE_LEN);
  3302. return -EINVAL;
  3303. }
  3304. if (fs_info->sectorsize != PAGE_SIZE) {
  3305. /* not supported for data w/o checksums */
  3306. btrfs_err_rl(fs_info,
  3307. "scrub: size assumption sectorsize != PAGE_SIZE (%d != %lu) fails",
  3308. fs_info->sectorsize, PAGE_SIZE);
  3309. return -EINVAL;
  3310. }
  3311. if (fs_info->nodesize >
  3312. PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK ||
  3313. fs_info->sectorsize > PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK) {
  3314. /*
  3315. * would exhaust the array bounds of pagev member in
  3316. * struct scrub_block
  3317. */
  3318. btrfs_err(fs_info,
  3319. "scrub: size assumption nodesize and sectorsize <= SCRUB_MAX_PAGES_PER_BLOCK (%d <= %d && %d <= %d) fails",
  3320. fs_info->nodesize,
  3321. SCRUB_MAX_PAGES_PER_BLOCK,
  3322. fs_info->sectorsize,
  3323. SCRUB_MAX_PAGES_PER_BLOCK);
  3324. return -EINVAL;
  3325. }
  3326. mutex_lock(&fs_info->fs_devices->device_list_mutex);
  3327. dev = btrfs_find_device(fs_info, devid, NULL, NULL);
  3328. if (!dev || (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) &&
  3329. !is_dev_replace)) {
  3330. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  3331. return -ENODEV;
  3332. }
  3333. if (!is_dev_replace && !readonly &&
  3334. !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) {
  3335. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  3336. btrfs_err_in_rcu(fs_info, "scrub: device %s is not writable",
  3337. rcu_str_deref(dev->name));
  3338. return -EROFS;
  3339. }
  3340. mutex_lock(&fs_info->scrub_lock);
  3341. if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
  3342. test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &dev->dev_state)) {
  3343. mutex_unlock(&fs_info->scrub_lock);
  3344. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  3345. return -EIO;
  3346. }
  3347. btrfs_dev_replace_read_lock(&fs_info->dev_replace);
  3348. if (dev->scrub_ctx ||
  3349. (!is_dev_replace &&
  3350. btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))) {
  3351. btrfs_dev_replace_read_unlock(&fs_info->dev_replace);
  3352. mutex_unlock(&fs_info->scrub_lock);
  3353. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  3354. return -EINPROGRESS;
  3355. }
  3356. btrfs_dev_replace_read_unlock(&fs_info->dev_replace);
  3357. ret = scrub_workers_get(fs_info, is_dev_replace);
  3358. if (ret) {
  3359. mutex_unlock(&fs_info->scrub_lock);
  3360. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  3361. return ret;
  3362. }
  3363. sctx = scrub_setup_ctx(dev, is_dev_replace);
  3364. if (IS_ERR(sctx)) {
  3365. mutex_unlock(&fs_info->scrub_lock);
  3366. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  3367. scrub_workers_put(fs_info);
  3368. return PTR_ERR(sctx);
  3369. }
  3370. sctx->readonly = readonly;
  3371. dev->scrub_ctx = sctx;
  3372. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  3373. /*
  3374. * checking @scrub_pause_req here, we can avoid
  3375. * race between committing transaction and scrubbing.
  3376. */
  3377. __scrub_blocked_if_needed(fs_info);
  3378. atomic_inc(&fs_info->scrubs_running);
  3379. mutex_unlock(&fs_info->scrub_lock);
  3380. if (!is_dev_replace) {
  3381. /*
  3382. * by holding device list mutex, we can
  3383. * kick off writing super in log tree sync.
  3384. */
  3385. mutex_lock(&fs_info->fs_devices->device_list_mutex);
  3386. ret = scrub_supers(sctx, dev);
  3387. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  3388. }
  3389. if (!ret)
  3390. ret = scrub_enumerate_chunks(sctx, dev, start, end);
  3391. wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
  3392. atomic_dec(&fs_info->scrubs_running);
  3393. wake_up(&fs_info->scrub_pause_wait);
  3394. wait_event(sctx->list_wait, atomic_read(&sctx->workers_pending) == 0);
  3395. if (progress)
  3396. memcpy(progress, &sctx->stat, sizeof(*progress));
  3397. mutex_lock(&fs_info->scrub_lock);
  3398. dev->scrub_ctx = NULL;
  3399. scrub_workers_put(fs_info);
  3400. mutex_unlock(&fs_info->scrub_lock);
  3401. scrub_put_ctx(sctx);
  3402. return ret;
  3403. }
  3404. void btrfs_scrub_pause(struct btrfs_fs_info *fs_info)
  3405. {
  3406. mutex_lock(&fs_info->scrub_lock);
  3407. atomic_inc(&fs_info->scrub_pause_req);
  3408. while (atomic_read(&fs_info->scrubs_paused) !=
  3409. atomic_read(&fs_info->scrubs_running)) {
  3410. mutex_unlock(&fs_info->scrub_lock);
  3411. wait_event(fs_info->scrub_pause_wait,
  3412. atomic_read(&fs_info->scrubs_paused) ==
  3413. atomic_read(&fs_info->scrubs_running));
  3414. mutex_lock(&fs_info->scrub_lock);
  3415. }
  3416. mutex_unlock(&fs_info->scrub_lock);
  3417. }
  3418. void btrfs_scrub_continue(struct btrfs_fs_info *fs_info)
  3419. {
  3420. atomic_dec(&fs_info->scrub_pause_req);
  3421. wake_up(&fs_info->scrub_pause_wait);
  3422. }
  3423. int btrfs_scrub_cancel(struct btrfs_fs_info *fs_info)
  3424. {
  3425. mutex_lock(&fs_info->scrub_lock);
  3426. if (!atomic_read(&fs_info->scrubs_running)) {
  3427. mutex_unlock(&fs_info->scrub_lock);
  3428. return -ENOTCONN;
  3429. }
  3430. atomic_inc(&fs_info->scrub_cancel_req);
  3431. while (atomic_read(&fs_info->scrubs_running)) {
  3432. mutex_unlock(&fs_info->scrub_lock);
  3433. wait_event(fs_info->scrub_pause_wait,
  3434. atomic_read(&fs_info->scrubs_running) == 0);
  3435. mutex_lock(&fs_info->scrub_lock);
  3436. }
  3437. atomic_dec(&fs_info->scrub_cancel_req);
  3438. mutex_unlock(&fs_info->scrub_lock);
  3439. return 0;
  3440. }
  3441. int btrfs_scrub_cancel_dev(struct btrfs_fs_info *fs_info,
  3442. struct btrfs_device *dev)
  3443. {
  3444. struct scrub_ctx *sctx;
  3445. mutex_lock(&fs_info->scrub_lock);
  3446. sctx = dev->scrub_ctx;
  3447. if (!sctx) {
  3448. mutex_unlock(&fs_info->scrub_lock);
  3449. return -ENOTCONN;
  3450. }
  3451. atomic_inc(&sctx->cancel_req);
  3452. while (dev->scrub_ctx) {
  3453. mutex_unlock(&fs_info->scrub_lock);
  3454. wait_event(fs_info->scrub_pause_wait,
  3455. dev->scrub_ctx == NULL);
  3456. mutex_lock(&fs_info->scrub_lock);
  3457. }
  3458. mutex_unlock(&fs_info->scrub_lock);
  3459. return 0;
  3460. }
  3461. int btrfs_scrub_progress(struct btrfs_fs_info *fs_info, u64 devid,
  3462. struct btrfs_scrub_progress *progress)
  3463. {
  3464. struct btrfs_device *dev;
  3465. struct scrub_ctx *sctx = NULL;
  3466. mutex_lock(&fs_info->fs_devices->device_list_mutex);
  3467. dev = btrfs_find_device(fs_info, devid, NULL, NULL);
  3468. if (dev)
  3469. sctx = dev->scrub_ctx;
  3470. if (sctx)
  3471. memcpy(progress, &sctx->stat, sizeof(*progress));
  3472. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  3473. return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV;
  3474. }
  3475. static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
  3476. u64 extent_logical, u64 extent_len,
  3477. u64 *extent_physical,
  3478. struct btrfs_device **extent_dev,
  3479. int *extent_mirror_num)
  3480. {
  3481. u64 mapped_length;
  3482. struct btrfs_bio *bbio = NULL;
  3483. int ret;
  3484. mapped_length = extent_len;
  3485. ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, extent_logical,
  3486. &mapped_length, &bbio, 0);
  3487. if (ret || !bbio || mapped_length < extent_len ||
  3488. !bbio->stripes[0].dev->bdev) {
  3489. btrfs_put_bbio(bbio);
  3490. return;
  3491. }
  3492. *extent_physical = bbio->stripes[0].physical;
  3493. *extent_mirror_num = bbio->mirror_num;
  3494. *extent_dev = bbio->stripes[0].dev;
  3495. btrfs_put_bbio(bbio);
  3496. }