scrub.c 121 KB

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