scrub.c 122 KB

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