scrub.c 121 KB

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