scrub.c 121 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645
  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. atomic_t flush_all_writes;
  160. struct btrfs_device *wr_tgtdev;
  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. sector_t sector;
  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. atomic_set(&sctx->flush_all_writes, 0);
  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, sector %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. (unsigned long long)swarn->sector,
  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, sector %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. (unsigned long long)swarn->sector,
  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.sector = (sblock->pagev[0]->physical) >> 9;
  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, sector %llu: metadata %s (level %d) in tree %llu",
  763. errstr, swarn.logical,
  764. rcu_str_deref(dev->name),
  765. (unsigned long long)swarn.sector,
  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);
  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);
  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(&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->bi_bdev = 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_UUID_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->bi_bdev = 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->bi_bdev = 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_bdev);
  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->bi_bdev = 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 &&
  2104. atomic_read(&sctx->flush_all_writes)) {
  2105. mutex_lock(&sctx->wr_lock);
  2106. scrub_wr_submit(sctx);
  2107. mutex_unlock(&sctx->wr_lock);
  2108. }
  2109. scrub_pending_bio_dec(sctx);
  2110. }
  2111. static void scrub_missing_raid56_pages(struct scrub_block *sblock)
  2112. {
  2113. struct scrub_ctx *sctx = sblock->sctx;
  2114. struct btrfs_fs_info *fs_info = sctx->fs_info;
  2115. u64 length = sblock->page_count * PAGE_SIZE;
  2116. u64 logical = sblock->pagev[0]->logical;
  2117. struct btrfs_bio *bbio = NULL;
  2118. struct bio *bio;
  2119. struct btrfs_raid_bio *rbio;
  2120. int ret;
  2121. int i;
  2122. btrfs_bio_counter_inc_blocked(fs_info);
  2123. ret = btrfs_map_sblock(fs_info, BTRFS_MAP_GET_READ_MIRRORS, logical,
  2124. &length, &bbio);
  2125. if (ret || !bbio || !bbio->raid_map)
  2126. goto bbio_out;
  2127. if (WARN_ON(!sctx->is_dev_replace ||
  2128. !(bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK))) {
  2129. /*
  2130. * We shouldn't be scrubbing a missing device. Even for dev
  2131. * replace, we should only get here for RAID 5/6. We either
  2132. * managed to mount something with no mirrors remaining or
  2133. * there's a bug in scrub_remap_extent()/btrfs_map_block().
  2134. */
  2135. goto bbio_out;
  2136. }
  2137. bio = btrfs_io_bio_alloc(0);
  2138. bio->bi_iter.bi_sector = logical >> 9;
  2139. bio->bi_private = sblock;
  2140. bio->bi_end_io = scrub_missing_raid56_end_io;
  2141. rbio = raid56_alloc_missing_rbio(fs_info, bio, bbio, length);
  2142. if (!rbio)
  2143. goto rbio_out;
  2144. for (i = 0; i < sblock->page_count; i++) {
  2145. struct scrub_page *spage = sblock->pagev[i];
  2146. raid56_add_scrub_pages(rbio, spage->page, spage->logical);
  2147. }
  2148. btrfs_init_work(&sblock->work, btrfs_scrub_helper,
  2149. scrub_missing_raid56_worker, NULL, NULL);
  2150. scrub_block_get(sblock);
  2151. scrub_pending_bio_inc(sctx);
  2152. raid56_submit_missing_rbio(rbio);
  2153. return;
  2154. rbio_out:
  2155. bio_put(bio);
  2156. bbio_out:
  2157. btrfs_bio_counter_dec(fs_info);
  2158. btrfs_put_bbio(bbio);
  2159. spin_lock(&sctx->stat_lock);
  2160. sctx->stat.malloc_errors++;
  2161. spin_unlock(&sctx->stat_lock);
  2162. }
  2163. static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
  2164. u64 physical, struct btrfs_device *dev, u64 flags,
  2165. u64 gen, int mirror_num, u8 *csum, int force,
  2166. u64 physical_for_dev_replace)
  2167. {
  2168. struct scrub_block *sblock;
  2169. int index;
  2170. sblock = kzalloc(sizeof(*sblock), GFP_KERNEL);
  2171. if (!sblock) {
  2172. spin_lock(&sctx->stat_lock);
  2173. sctx->stat.malloc_errors++;
  2174. spin_unlock(&sctx->stat_lock);
  2175. return -ENOMEM;
  2176. }
  2177. /* one ref inside this function, plus one for each page added to
  2178. * a bio later on */
  2179. refcount_set(&sblock->refs, 1);
  2180. sblock->sctx = sctx;
  2181. sblock->no_io_error_seen = 1;
  2182. for (index = 0; len > 0; index++) {
  2183. struct scrub_page *spage;
  2184. u64 l = min_t(u64, len, PAGE_SIZE);
  2185. spage = kzalloc(sizeof(*spage), GFP_KERNEL);
  2186. if (!spage) {
  2187. leave_nomem:
  2188. spin_lock(&sctx->stat_lock);
  2189. sctx->stat.malloc_errors++;
  2190. spin_unlock(&sctx->stat_lock);
  2191. scrub_block_put(sblock);
  2192. return -ENOMEM;
  2193. }
  2194. BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
  2195. scrub_page_get(spage);
  2196. sblock->pagev[index] = spage;
  2197. spage->sblock = sblock;
  2198. spage->dev = dev;
  2199. spage->flags = flags;
  2200. spage->generation = gen;
  2201. spage->logical = logical;
  2202. spage->physical = physical;
  2203. spage->physical_for_dev_replace = physical_for_dev_replace;
  2204. spage->mirror_num = mirror_num;
  2205. if (csum) {
  2206. spage->have_csum = 1;
  2207. memcpy(spage->csum, csum, sctx->csum_size);
  2208. } else {
  2209. spage->have_csum = 0;
  2210. }
  2211. sblock->page_count++;
  2212. spage->page = alloc_page(GFP_KERNEL);
  2213. if (!spage->page)
  2214. goto leave_nomem;
  2215. len -= l;
  2216. logical += l;
  2217. physical += l;
  2218. physical_for_dev_replace += l;
  2219. }
  2220. WARN_ON(sblock->page_count == 0);
  2221. if (dev->missing) {
  2222. /*
  2223. * This case should only be hit for RAID 5/6 device replace. See
  2224. * the comment in scrub_missing_raid56_pages() for details.
  2225. */
  2226. scrub_missing_raid56_pages(sblock);
  2227. } else {
  2228. for (index = 0; index < sblock->page_count; index++) {
  2229. struct scrub_page *spage = sblock->pagev[index];
  2230. int ret;
  2231. ret = scrub_add_page_to_rd_bio(sctx, spage);
  2232. if (ret) {
  2233. scrub_block_put(sblock);
  2234. return ret;
  2235. }
  2236. }
  2237. if (force)
  2238. scrub_submit(sctx);
  2239. }
  2240. /* last one frees, either here or in bio completion for last page */
  2241. scrub_block_put(sblock);
  2242. return 0;
  2243. }
  2244. static void scrub_bio_end_io(struct bio *bio)
  2245. {
  2246. struct scrub_bio *sbio = bio->bi_private;
  2247. struct btrfs_fs_info *fs_info = sbio->dev->fs_info;
  2248. sbio->status = bio->bi_status;
  2249. sbio->bio = bio;
  2250. btrfs_queue_work(fs_info->scrub_workers, &sbio->work);
  2251. }
  2252. static void scrub_bio_end_io_worker(struct btrfs_work *work)
  2253. {
  2254. struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
  2255. struct scrub_ctx *sctx = sbio->sctx;
  2256. int i;
  2257. BUG_ON(sbio->page_count > SCRUB_PAGES_PER_RD_BIO);
  2258. if (sbio->status) {
  2259. for (i = 0; i < sbio->page_count; i++) {
  2260. struct scrub_page *spage = sbio->pagev[i];
  2261. spage->io_error = 1;
  2262. spage->sblock->no_io_error_seen = 0;
  2263. }
  2264. }
  2265. /* now complete the scrub_block items that have all pages completed */
  2266. for (i = 0; i < sbio->page_count; i++) {
  2267. struct scrub_page *spage = sbio->pagev[i];
  2268. struct scrub_block *sblock = spage->sblock;
  2269. if (atomic_dec_and_test(&sblock->outstanding_pages))
  2270. scrub_block_complete(sblock);
  2271. scrub_block_put(sblock);
  2272. }
  2273. bio_put(sbio->bio);
  2274. sbio->bio = NULL;
  2275. spin_lock(&sctx->list_lock);
  2276. sbio->next_free = sctx->first_free;
  2277. sctx->first_free = sbio->index;
  2278. spin_unlock(&sctx->list_lock);
  2279. if (sctx->is_dev_replace &&
  2280. atomic_read(&sctx->flush_all_writes)) {
  2281. mutex_lock(&sctx->wr_lock);
  2282. scrub_wr_submit(sctx);
  2283. mutex_unlock(&sctx->wr_lock);
  2284. }
  2285. scrub_pending_bio_dec(sctx);
  2286. }
  2287. static inline void __scrub_mark_bitmap(struct scrub_parity *sparity,
  2288. unsigned long *bitmap,
  2289. u64 start, u64 len)
  2290. {
  2291. u64 offset;
  2292. int nsectors;
  2293. int sectorsize = sparity->sctx->fs_info->sectorsize;
  2294. if (len >= sparity->stripe_len) {
  2295. bitmap_set(bitmap, 0, sparity->nsectors);
  2296. return;
  2297. }
  2298. start -= sparity->logic_start;
  2299. start = div64_u64_rem(start, sparity->stripe_len, &offset);
  2300. offset = div_u64(offset, sectorsize);
  2301. nsectors = (int)len / sectorsize;
  2302. if (offset + nsectors <= sparity->nsectors) {
  2303. bitmap_set(bitmap, offset, nsectors);
  2304. return;
  2305. }
  2306. bitmap_set(bitmap, offset, sparity->nsectors - offset);
  2307. bitmap_set(bitmap, 0, nsectors - (sparity->nsectors - offset));
  2308. }
  2309. static inline void scrub_parity_mark_sectors_error(struct scrub_parity *sparity,
  2310. u64 start, u64 len)
  2311. {
  2312. __scrub_mark_bitmap(sparity, sparity->ebitmap, start, len);
  2313. }
  2314. static inline void scrub_parity_mark_sectors_data(struct scrub_parity *sparity,
  2315. u64 start, u64 len)
  2316. {
  2317. __scrub_mark_bitmap(sparity, sparity->dbitmap, start, len);
  2318. }
  2319. static void scrub_block_complete(struct scrub_block *sblock)
  2320. {
  2321. int corrupted = 0;
  2322. if (!sblock->no_io_error_seen) {
  2323. corrupted = 1;
  2324. scrub_handle_errored_block(sblock);
  2325. } else {
  2326. /*
  2327. * if has checksum error, write via repair mechanism in
  2328. * dev replace case, otherwise write here in dev replace
  2329. * case.
  2330. */
  2331. corrupted = scrub_checksum(sblock);
  2332. if (!corrupted && sblock->sctx->is_dev_replace)
  2333. scrub_write_block_to_dev_replace(sblock);
  2334. }
  2335. if (sblock->sparity && corrupted && !sblock->data_corrected) {
  2336. u64 start = sblock->pagev[0]->logical;
  2337. u64 end = sblock->pagev[sblock->page_count - 1]->logical +
  2338. PAGE_SIZE;
  2339. scrub_parity_mark_sectors_error(sblock->sparity,
  2340. start, end - start);
  2341. }
  2342. }
  2343. static int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u8 *csum)
  2344. {
  2345. struct btrfs_ordered_sum *sum = NULL;
  2346. unsigned long index;
  2347. unsigned long num_sectors;
  2348. while (!list_empty(&sctx->csum_list)) {
  2349. sum = list_first_entry(&sctx->csum_list,
  2350. struct btrfs_ordered_sum, list);
  2351. if (sum->bytenr > logical)
  2352. return 0;
  2353. if (sum->bytenr + sum->len > logical)
  2354. break;
  2355. ++sctx->stat.csum_discards;
  2356. list_del(&sum->list);
  2357. kfree(sum);
  2358. sum = NULL;
  2359. }
  2360. if (!sum)
  2361. return 0;
  2362. index = ((u32)(logical - sum->bytenr)) / sctx->fs_info->sectorsize;
  2363. num_sectors = sum->len / sctx->fs_info->sectorsize;
  2364. memcpy(csum, sum->sums + index, sctx->csum_size);
  2365. if (index == num_sectors - 1) {
  2366. list_del(&sum->list);
  2367. kfree(sum);
  2368. }
  2369. return 1;
  2370. }
  2371. /* scrub extent tries to collect up to 64 kB for each bio */
  2372. static int scrub_extent(struct scrub_ctx *sctx, u64 logical, u64 len,
  2373. u64 physical, struct btrfs_device *dev, u64 flags,
  2374. u64 gen, int mirror_num, u64 physical_for_dev_replace)
  2375. {
  2376. int ret;
  2377. u8 csum[BTRFS_CSUM_SIZE];
  2378. u32 blocksize;
  2379. if (flags & BTRFS_EXTENT_FLAG_DATA) {
  2380. blocksize = sctx->fs_info->sectorsize;
  2381. spin_lock(&sctx->stat_lock);
  2382. sctx->stat.data_extents_scrubbed++;
  2383. sctx->stat.data_bytes_scrubbed += len;
  2384. spin_unlock(&sctx->stat_lock);
  2385. } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  2386. blocksize = sctx->fs_info->nodesize;
  2387. spin_lock(&sctx->stat_lock);
  2388. sctx->stat.tree_extents_scrubbed++;
  2389. sctx->stat.tree_bytes_scrubbed += len;
  2390. spin_unlock(&sctx->stat_lock);
  2391. } else {
  2392. blocksize = sctx->fs_info->sectorsize;
  2393. WARN_ON(1);
  2394. }
  2395. while (len) {
  2396. u64 l = min_t(u64, len, blocksize);
  2397. int have_csum = 0;
  2398. if (flags & BTRFS_EXTENT_FLAG_DATA) {
  2399. /* push csums to sbio */
  2400. have_csum = scrub_find_csum(sctx, logical, csum);
  2401. if (have_csum == 0)
  2402. ++sctx->stat.no_csum;
  2403. if (sctx->is_dev_replace && !have_csum) {
  2404. ret = copy_nocow_pages(sctx, logical, l,
  2405. mirror_num,
  2406. physical_for_dev_replace);
  2407. goto behind_scrub_pages;
  2408. }
  2409. }
  2410. ret = scrub_pages(sctx, logical, l, physical, dev, flags, gen,
  2411. mirror_num, have_csum ? csum : NULL, 0,
  2412. physical_for_dev_replace);
  2413. behind_scrub_pages:
  2414. if (ret)
  2415. return ret;
  2416. len -= l;
  2417. logical += l;
  2418. physical += l;
  2419. physical_for_dev_replace += l;
  2420. }
  2421. return 0;
  2422. }
  2423. static int scrub_pages_for_parity(struct scrub_parity *sparity,
  2424. u64 logical, u64 len,
  2425. u64 physical, struct btrfs_device *dev,
  2426. u64 flags, u64 gen, int mirror_num, u8 *csum)
  2427. {
  2428. struct scrub_ctx *sctx = sparity->sctx;
  2429. struct scrub_block *sblock;
  2430. int index;
  2431. sblock = kzalloc(sizeof(*sblock), GFP_KERNEL);
  2432. if (!sblock) {
  2433. spin_lock(&sctx->stat_lock);
  2434. sctx->stat.malloc_errors++;
  2435. spin_unlock(&sctx->stat_lock);
  2436. return -ENOMEM;
  2437. }
  2438. /* one ref inside this function, plus one for each page added to
  2439. * a bio later on */
  2440. refcount_set(&sblock->refs, 1);
  2441. sblock->sctx = sctx;
  2442. sblock->no_io_error_seen = 1;
  2443. sblock->sparity = sparity;
  2444. scrub_parity_get(sparity);
  2445. for (index = 0; len > 0; index++) {
  2446. struct scrub_page *spage;
  2447. u64 l = min_t(u64, len, PAGE_SIZE);
  2448. spage = kzalloc(sizeof(*spage), GFP_KERNEL);
  2449. if (!spage) {
  2450. leave_nomem:
  2451. spin_lock(&sctx->stat_lock);
  2452. sctx->stat.malloc_errors++;
  2453. spin_unlock(&sctx->stat_lock);
  2454. scrub_block_put(sblock);
  2455. return -ENOMEM;
  2456. }
  2457. BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
  2458. /* For scrub block */
  2459. scrub_page_get(spage);
  2460. sblock->pagev[index] = spage;
  2461. /* For scrub parity */
  2462. scrub_page_get(spage);
  2463. list_add_tail(&spage->list, &sparity->spages);
  2464. spage->sblock = sblock;
  2465. spage->dev = dev;
  2466. spage->flags = flags;
  2467. spage->generation = gen;
  2468. spage->logical = logical;
  2469. spage->physical = physical;
  2470. spage->mirror_num = mirror_num;
  2471. if (csum) {
  2472. spage->have_csum = 1;
  2473. memcpy(spage->csum, csum, sctx->csum_size);
  2474. } else {
  2475. spage->have_csum = 0;
  2476. }
  2477. sblock->page_count++;
  2478. spage->page = alloc_page(GFP_KERNEL);
  2479. if (!spage->page)
  2480. goto leave_nomem;
  2481. len -= l;
  2482. logical += l;
  2483. physical += l;
  2484. }
  2485. WARN_ON(sblock->page_count == 0);
  2486. for (index = 0; index < sblock->page_count; index++) {
  2487. struct scrub_page *spage = sblock->pagev[index];
  2488. int ret;
  2489. ret = scrub_add_page_to_rd_bio(sctx, spage);
  2490. if (ret) {
  2491. scrub_block_put(sblock);
  2492. return ret;
  2493. }
  2494. }
  2495. /* last one frees, either here or in bio completion for last page */
  2496. scrub_block_put(sblock);
  2497. return 0;
  2498. }
  2499. static int scrub_extent_for_parity(struct scrub_parity *sparity,
  2500. u64 logical, u64 len,
  2501. u64 physical, struct btrfs_device *dev,
  2502. u64 flags, u64 gen, int mirror_num)
  2503. {
  2504. struct scrub_ctx *sctx = sparity->sctx;
  2505. int ret;
  2506. u8 csum[BTRFS_CSUM_SIZE];
  2507. u32 blocksize;
  2508. if (dev->missing) {
  2509. scrub_parity_mark_sectors_error(sparity, logical, len);
  2510. return 0;
  2511. }
  2512. if (flags & BTRFS_EXTENT_FLAG_DATA) {
  2513. blocksize = sctx->fs_info->sectorsize;
  2514. } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  2515. blocksize = sctx->fs_info->nodesize;
  2516. } else {
  2517. blocksize = sctx->fs_info->sectorsize;
  2518. WARN_ON(1);
  2519. }
  2520. while (len) {
  2521. u64 l = min_t(u64, len, blocksize);
  2522. int have_csum = 0;
  2523. if (flags & BTRFS_EXTENT_FLAG_DATA) {
  2524. /* push csums to sbio */
  2525. have_csum = scrub_find_csum(sctx, logical, csum);
  2526. if (have_csum == 0)
  2527. goto skip;
  2528. }
  2529. ret = scrub_pages_for_parity(sparity, logical, l, physical, dev,
  2530. flags, gen, mirror_num,
  2531. have_csum ? csum : NULL);
  2532. if (ret)
  2533. return ret;
  2534. skip:
  2535. len -= l;
  2536. logical += l;
  2537. physical += l;
  2538. }
  2539. return 0;
  2540. }
  2541. /*
  2542. * Given a physical address, this will calculate it's
  2543. * logical offset. if this is a parity stripe, it will return
  2544. * the most left data stripe's logical offset.
  2545. *
  2546. * return 0 if it is a data stripe, 1 means parity stripe.
  2547. */
  2548. static int get_raid56_logic_offset(u64 physical, int num,
  2549. struct map_lookup *map, u64 *offset,
  2550. u64 *stripe_start)
  2551. {
  2552. int i;
  2553. int j = 0;
  2554. u64 stripe_nr;
  2555. u64 last_offset;
  2556. u32 stripe_index;
  2557. u32 rot;
  2558. last_offset = (physical - map->stripes[num].physical) *
  2559. nr_data_stripes(map);
  2560. if (stripe_start)
  2561. *stripe_start = last_offset;
  2562. *offset = last_offset;
  2563. for (i = 0; i < nr_data_stripes(map); i++) {
  2564. *offset = last_offset + i * map->stripe_len;
  2565. stripe_nr = div64_u64(*offset, map->stripe_len);
  2566. stripe_nr = div_u64(stripe_nr, nr_data_stripes(map));
  2567. /* Work out the disk rotation on this stripe-set */
  2568. stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, &rot);
  2569. /* calculate which stripe this data locates */
  2570. rot += i;
  2571. stripe_index = rot % map->num_stripes;
  2572. if (stripe_index == num)
  2573. return 0;
  2574. if (stripe_index < num)
  2575. j++;
  2576. }
  2577. *offset = last_offset + j * map->stripe_len;
  2578. return 1;
  2579. }
  2580. static void scrub_free_parity(struct scrub_parity *sparity)
  2581. {
  2582. struct scrub_ctx *sctx = sparity->sctx;
  2583. struct scrub_page *curr, *next;
  2584. int nbits;
  2585. nbits = bitmap_weight(sparity->ebitmap, sparity->nsectors);
  2586. if (nbits) {
  2587. spin_lock(&sctx->stat_lock);
  2588. sctx->stat.read_errors += nbits;
  2589. sctx->stat.uncorrectable_errors += nbits;
  2590. spin_unlock(&sctx->stat_lock);
  2591. }
  2592. list_for_each_entry_safe(curr, next, &sparity->spages, list) {
  2593. list_del_init(&curr->list);
  2594. scrub_page_put(curr);
  2595. }
  2596. kfree(sparity);
  2597. }
  2598. static void scrub_parity_bio_endio_worker(struct btrfs_work *work)
  2599. {
  2600. struct scrub_parity *sparity = container_of(work, struct scrub_parity,
  2601. work);
  2602. struct scrub_ctx *sctx = sparity->sctx;
  2603. scrub_free_parity(sparity);
  2604. scrub_pending_bio_dec(sctx);
  2605. }
  2606. static void scrub_parity_bio_endio(struct bio *bio)
  2607. {
  2608. struct scrub_parity *sparity = (struct scrub_parity *)bio->bi_private;
  2609. struct btrfs_fs_info *fs_info = sparity->sctx->fs_info;
  2610. if (bio->bi_status)
  2611. bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
  2612. sparity->nsectors);
  2613. bio_put(bio);
  2614. btrfs_init_work(&sparity->work, btrfs_scrubparity_helper,
  2615. scrub_parity_bio_endio_worker, NULL, NULL);
  2616. btrfs_queue_work(fs_info->scrub_parity_workers, &sparity->work);
  2617. }
  2618. static void scrub_parity_check_and_repair(struct scrub_parity *sparity)
  2619. {
  2620. struct scrub_ctx *sctx = sparity->sctx;
  2621. struct btrfs_fs_info *fs_info = sctx->fs_info;
  2622. struct bio *bio;
  2623. struct btrfs_raid_bio *rbio;
  2624. struct btrfs_bio *bbio = NULL;
  2625. u64 length;
  2626. int ret;
  2627. if (!bitmap_andnot(sparity->dbitmap, sparity->dbitmap, sparity->ebitmap,
  2628. sparity->nsectors))
  2629. goto out;
  2630. length = sparity->logic_end - sparity->logic_start;
  2631. btrfs_bio_counter_inc_blocked(fs_info);
  2632. ret = btrfs_map_sblock(fs_info, BTRFS_MAP_WRITE, sparity->logic_start,
  2633. &length, &bbio);
  2634. if (ret || !bbio || !bbio->raid_map)
  2635. goto bbio_out;
  2636. bio = btrfs_io_bio_alloc(0);
  2637. bio->bi_iter.bi_sector = sparity->logic_start >> 9;
  2638. bio->bi_private = sparity;
  2639. bio->bi_end_io = scrub_parity_bio_endio;
  2640. rbio = raid56_parity_alloc_scrub_rbio(fs_info, bio, bbio,
  2641. length, sparity->scrub_dev,
  2642. sparity->dbitmap,
  2643. sparity->nsectors);
  2644. if (!rbio)
  2645. goto rbio_out;
  2646. scrub_pending_bio_inc(sctx);
  2647. raid56_parity_submit_scrub_rbio(rbio);
  2648. return;
  2649. rbio_out:
  2650. bio_put(bio);
  2651. bbio_out:
  2652. btrfs_bio_counter_dec(fs_info);
  2653. btrfs_put_bbio(bbio);
  2654. bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
  2655. sparity->nsectors);
  2656. spin_lock(&sctx->stat_lock);
  2657. sctx->stat.malloc_errors++;
  2658. spin_unlock(&sctx->stat_lock);
  2659. out:
  2660. scrub_free_parity(sparity);
  2661. }
  2662. static inline int scrub_calc_parity_bitmap_len(int nsectors)
  2663. {
  2664. return DIV_ROUND_UP(nsectors, BITS_PER_LONG) * sizeof(long);
  2665. }
  2666. static void scrub_parity_get(struct scrub_parity *sparity)
  2667. {
  2668. refcount_inc(&sparity->refs);
  2669. }
  2670. static void scrub_parity_put(struct scrub_parity *sparity)
  2671. {
  2672. if (!refcount_dec_and_test(&sparity->refs))
  2673. return;
  2674. scrub_parity_check_and_repair(sparity);
  2675. }
  2676. static noinline_for_stack int scrub_raid56_parity(struct scrub_ctx *sctx,
  2677. struct map_lookup *map,
  2678. struct btrfs_device *sdev,
  2679. struct btrfs_path *path,
  2680. u64 logic_start,
  2681. u64 logic_end)
  2682. {
  2683. struct btrfs_fs_info *fs_info = sctx->fs_info;
  2684. struct btrfs_root *root = fs_info->extent_root;
  2685. struct btrfs_root *csum_root = fs_info->csum_root;
  2686. struct btrfs_extent_item *extent;
  2687. struct btrfs_bio *bbio = NULL;
  2688. u64 flags;
  2689. int ret;
  2690. int slot;
  2691. struct extent_buffer *l;
  2692. struct btrfs_key key;
  2693. u64 generation;
  2694. u64 extent_logical;
  2695. u64 extent_physical;
  2696. u64 extent_len;
  2697. u64 mapped_length;
  2698. struct btrfs_device *extent_dev;
  2699. struct scrub_parity *sparity;
  2700. int nsectors;
  2701. int bitmap_len;
  2702. int extent_mirror_num;
  2703. int stop_loop = 0;
  2704. nsectors = div_u64(map->stripe_len, fs_info->sectorsize);
  2705. bitmap_len = scrub_calc_parity_bitmap_len(nsectors);
  2706. sparity = kzalloc(sizeof(struct scrub_parity) + 2 * bitmap_len,
  2707. GFP_NOFS);
  2708. if (!sparity) {
  2709. spin_lock(&sctx->stat_lock);
  2710. sctx->stat.malloc_errors++;
  2711. spin_unlock(&sctx->stat_lock);
  2712. return -ENOMEM;
  2713. }
  2714. sparity->stripe_len = map->stripe_len;
  2715. sparity->nsectors = nsectors;
  2716. sparity->sctx = sctx;
  2717. sparity->scrub_dev = sdev;
  2718. sparity->logic_start = logic_start;
  2719. sparity->logic_end = logic_end;
  2720. refcount_set(&sparity->refs, 1);
  2721. INIT_LIST_HEAD(&sparity->spages);
  2722. sparity->dbitmap = sparity->bitmap;
  2723. sparity->ebitmap = (void *)sparity->bitmap + bitmap_len;
  2724. ret = 0;
  2725. while (logic_start < logic_end) {
  2726. if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
  2727. key.type = BTRFS_METADATA_ITEM_KEY;
  2728. else
  2729. key.type = BTRFS_EXTENT_ITEM_KEY;
  2730. key.objectid = logic_start;
  2731. key.offset = (u64)-1;
  2732. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2733. if (ret < 0)
  2734. goto out;
  2735. if (ret > 0) {
  2736. ret = btrfs_previous_extent_item(root, path, 0);
  2737. if (ret < 0)
  2738. goto out;
  2739. if (ret > 0) {
  2740. btrfs_release_path(path);
  2741. ret = btrfs_search_slot(NULL, root, &key,
  2742. path, 0, 0);
  2743. if (ret < 0)
  2744. goto out;
  2745. }
  2746. }
  2747. stop_loop = 0;
  2748. while (1) {
  2749. u64 bytes;
  2750. l = path->nodes[0];
  2751. slot = path->slots[0];
  2752. if (slot >= btrfs_header_nritems(l)) {
  2753. ret = btrfs_next_leaf(root, path);
  2754. if (ret == 0)
  2755. continue;
  2756. if (ret < 0)
  2757. goto out;
  2758. stop_loop = 1;
  2759. break;
  2760. }
  2761. btrfs_item_key_to_cpu(l, &key, slot);
  2762. if (key.type != BTRFS_EXTENT_ITEM_KEY &&
  2763. key.type != BTRFS_METADATA_ITEM_KEY)
  2764. goto next;
  2765. if (key.type == BTRFS_METADATA_ITEM_KEY)
  2766. bytes = fs_info->nodesize;
  2767. else
  2768. bytes = key.offset;
  2769. if (key.objectid + bytes <= logic_start)
  2770. goto next;
  2771. if (key.objectid >= logic_end) {
  2772. stop_loop = 1;
  2773. break;
  2774. }
  2775. while (key.objectid >= logic_start + map->stripe_len)
  2776. logic_start += map->stripe_len;
  2777. extent = btrfs_item_ptr(l, slot,
  2778. struct btrfs_extent_item);
  2779. flags = btrfs_extent_flags(l, extent);
  2780. generation = btrfs_extent_generation(l, extent);
  2781. if ((flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) &&
  2782. (key.objectid < logic_start ||
  2783. key.objectid + bytes >
  2784. logic_start + map->stripe_len)) {
  2785. btrfs_err(fs_info,
  2786. "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
  2787. key.objectid, logic_start);
  2788. spin_lock(&sctx->stat_lock);
  2789. sctx->stat.uncorrectable_errors++;
  2790. spin_unlock(&sctx->stat_lock);
  2791. goto next;
  2792. }
  2793. again:
  2794. extent_logical = key.objectid;
  2795. extent_len = bytes;
  2796. if (extent_logical < logic_start) {
  2797. extent_len -= logic_start - extent_logical;
  2798. extent_logical = logic_start;
  2799. }
  2800. if (extent_logical + extent_len >
  2801. logic_start + map->stripe_len)
  2802. extent_len = logic_start + map->stripe_len -
  2803. extent_logical;
  2804. scrub_parity_mark_sectors_data(sparity, extent_logical,
  2805. extent_len);
  2806. mapped_length = extent_len;
  2807. bbio = NULL;
  2808. ret = btrfs_map_block(fs_info, BTRFS_MAP_READ,
  2809. extent_logical, &mapped_length, &bbio,
  2810. 0);
  2811. if (!ret) {
  2812. if (!bbio || mapped_length < extent_len)
  2813. ret = -EIO;
  2814. }
  2815. if (ret) {
  2816. btrfs_put_bbio(bbio);
  2817. goto out;
  2818. }
  2819. extent_physical = bbio->stripes[0].physical;
  2820. extent_mirror_num = bbio->mirror_num;
  2821. extent_dev = bbio->stripes[0].dev;
  2822. btrfs_put_bbio(bbio);
  2823. ret = btrfs_lookup_csums_range(csum_root,
  2824. extent_logical,
  2825. extent_logical + extent_len - 1,
  2826. &sctx->csum_list, 1);
  2827. if (ret)
  2828. goto out;
  2829. ret = scrub_extent_for_parity(sparity, extent_logical,
  2830. extent_len,
  2831. extent_physical,
  2832. extent_dev, flags,
  2833. generation,
  2834. extent_mirror_num);
  2835. scrub_free_csums(sctx);
  2836. if (ret)
  2837. goto out;
  2838. if (extent_logical + extent_len <
  2839. key.objectid + bytes) {
  2840. logic_start += map->stripe_len;
  2841. if (logic_start >= logic_end) {
  2842. stop_loop = 1;
  2843. break;
  2844. }
  2845. if (logic_start < key.objectid + bytes) {
  2846. cond_resched();
  2847. goto again;
  2848. }
  2849. }
  2850. next:
  2851. path->slots[0]++;
  2852. }
  2853. btrfs_release_path(path);
  2854. if (stop_loop)
  2855. break;
  2856. logic_start += map->stripe_len;
  2857. }
  2858. out:
  2859. if (ret < 0)
  2860. scrub_parity_mark_sectors_error(sparity, logic_start,
  2861. logic_end - logic_start);
  2862. scrub_parity_put(sparity);
  2863. scrub_submit(sctx);
  2864. mutex_lock(&sctx->wr_lock);
  2865. scrub_wr_submit(sctx);
  2866. mutex_unlock(&sctx->wr_lock);
  2867. btrfs_release_path(path);
  2868. return ret < 0 ? ret : 0;
  2869. }
  2870. static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
  2871. struct map_lookup *map,
  2872. struct btrfs_device *scrub_dev,
  2873. int num, u64 base, u64 length,
  2874. int is_dev_replace)
  2875. {
  2876. struct btrfs_path *path, *ppath;
  2877. struct btrfs_fs_info *fs_info = sctx->fs_info;
  2878. struct btrfs_root *root = fs_info->extent_root;
  2879. struct btrfs_root *csum_root = fs_info->csum_root;
  2880. struct btrfs_extent_item *extent;
  2881. struct blk_plug plug;
  2882. u64 flags;
  2883. int ret;
  2884. int slot;
  2885. u64 nstripes;
  2886. struct extent_buffer *l;
  2887. u64 physical;
  2888. u64 logical;
  2889. u64 logic_end;
  2890. u64 physical_end;
  2891. u64 generation;
  2892. int mirror_num;
  2893. struct reada_control *reada1;
  2894. struct reada_control *reada2;
  2895. struct btrfs_key key;
  2896. struct btrfs_key key_end;
  2897. u64 increment = map->stripe_len;
  2898. u64 offset;
  2899. u64 extent_logical;
  2900. u64 extent_physical;
  2901. u64 extent_len;
  2902. u64 stripe_logical;
  2903. u64 stripe_end;
  2904. struct btrfs_device *extent_dev;
  2905. int extent_mirror_num;
  2906. int stop_loop = 0;
  2907. physical = map->stripes[num].physical;
  2908. offset = 0;
  2909. nstripes = div64_u64(length, map->stripe_len);
  2910. if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
  2911. offset = map->stripe_len * num;
  2912. increment = map->stripe_len * map->num_stripes;
  2913. mirror_num = 1;
  2914. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  2915. int factor = map->num_stripes / map->sub_stripes;
  2916. offset = map->stripe_len * (num / map->sub_stripes);
  2917. increment = map->stripe_len * factor;
  2918. mirror_num = num % map->sub_stripes + 1;
  2919. } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
  2920. increment = map->stripe_len;
  2921. mirror_num = num % map->num_stripes + 1;
  2922. } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
  2923. increment = map->stripe_len;
  2924. mirror_num = num % map->num_stripes + 1;
  2925. } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
  2926. get_raid56_logic_offset(physical, num, map, &offset, NULL);
  2927. increment = map->stripe_len * nr_data_stripes(map);
  2928. mirror_num = 1;
  2929. } else {
  2930. increment = map->stripe_len;
  2931. mirror_num = 1;
  2932. }
  2933. path = btrfs_alloc_path();
  2934. if (!path)
  2935. return -ENOMEM;
  2936. ppath = btrfs_alloc_path();
  2937. if (!ppath) {
  2938. btrfs_free_path(path);
  2939. return -ENOMEM;
  2940. }
  2941. /*
  2942. * work on commit root. The related disk blocks are static as
  2943. * long as COW is applied. This means, it is save to rewrite
  2944. * them to repair disk errors without any race conditions
  2945. */
  2946. path->search_commit_root = 1;
  2947. path->skip_locking = 1;
  2948. ppath->search_commit_root = 1;
  2949. ppath->skip_locking = 1;
  2950. /*
  2951. * trigger the readahead for extent tree csum tree and wait for
  2952. * completion. During readahead, the scrub is officially paused
  2953. * to not hold off transaction commits
  2954. */
  2955. logical = base + offset;
  2956. physical_end = physical + nstripes * map->stripe_len;
  2957. if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
  2958. get_raid56_logic_offset(physical_end, num,
  2959. map, &logic_end, NULL);
  2960. logic_end += base;
  2961. } else {
  2962. logic_end = logical + increment * nstripes;
  2963. }
  2964. wait_event(sctx->list_wait,
  2965. atomic_read(&sctx->bios_in_flight) == 0);
  2966. scrub_blocked_if_needed(fs_info);
  2967. /* FIXME it might be better to start readahead at commit root */
  2968. key.objectid = logical;
  2969. key.type = BTRFS_EXTENT_ITEM_KEY;
  2970. key.offset = (u64)0;
  2971. key_end.objectid = logic_end;
  2972. key_end.type = BTRFS_METADATA_ITEM_KEY;
  2973. key_end.offset = (u64)-1;
  2974. reada1 = btrfs_reada_add(root, &key, &key_end);
  2975. key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
  2976. key.type = BTRFS_EXTENT_CSUM_KEY;
  2977. key.offset = logical;
  2978. key_end.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
  2979. key_end.type = BTRFS_EXTENT_CSUM_KEY;
  2980. key_end.offset = logic_end;
  2981. reada2 = btrfs_reada_add(csum_root, &key, &key_end);
  2982. if (!IS_ERR(reada1))
  2983. btrfs_reada_wait(reada1);
  2984. if (!IS_ERR(reada2))
  2985. btrfs_reada_wait(reada2);
  2986. /*
  2987. * collect all data csums for the stripe to avoid seeking during
  2988. * the scrub. This might currently (crc32) end up to be about 1MB
  2989. */
  2990. blk_start_plug(&plug);
  2991. /*
  2992. * now find all extents for each stripe and scrub them
  2993. */
  2994. ret = 0;
  2995. while (physical < physical_end) {
  2996. /*
  2997. * canceled?
  2998. */
  2999. if (atomic_read(&fs_info->scrub_cancel_req) ||
  3000. atomic_read(&sctx->cancel_req)) {
  3001. ret = -ECANCELED;
  3002. goto out;
  3003. }
  3004. /*
  3005. * check to see if we have to pause
  3006. */
  3007. if (atomic_read(&fs_info->scrub_pause_req)) {
  3008. /* push queued extents */
  3009. atomic_set(&sctx->flush_all_writes, 1);
  3010. scrub_submit(sctx);
  3011. mutex_lock(&sctx->wr_lock);
  3012. scrub_wr_submit(sctx);
  3013. mutex_unlock(&sctx->wr_lock);
  3014. wait_event(sctx->list_wait,
  3015. atomic_read(&sctx->bios_in_flight) == 0);
  3016. atomic_set(&sctx->flush_all_writes, 0);
  3017. scrub_blocked_if_needed(fs_info);
  3018. }
  3019. if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
  3020. ret = get_raid56_logic_offset(physical, num, map,
  3021. &logical,
  3022. &stripe_logical);
  3023. logical += base;
  3024. if (ret) {
  3025. /* it is parity strip */
  3026. stripe_logical += base;
  3027. stripe_end = stripe_logical + increment;
  3028. ret = scrub_raid56_parity(sctx, map, scrub_dev,
  3029. ppath, stripe_logical,
  3030. stripe_end);
  3031. if (ret)
  3032. goto out;
  3033. goto skip;
  3034. }
  3035. }
  3036. if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
  3037. key.type = BTRFS_METADATA_ITEM_KEY;
  3038. else
  3039. key.type = BTRFS_EXTENT_ITEM_KEY;
  3040. key.objectid = logical;
  3041. key.offset = (u64)-1;
  3042. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3043. if (ret < 0)
  3044. goto out;
  3045. if (ret > 0) {
  3046. ret = btrfs_previous_extent_item(root, path, 0);
  3047. if (ret < 0)
  3048. goto out;
  3049. if (ret > 0) {
  3050. /* there's no smaller item, so stick with the
  3051. * larger one */
  3052. btrfs_release_path(path);
  3053. ret = btrfs_search_slot(NULL, root, &key,
  3054. path, 0, 0);
  3055. if (ret < 0)
  3056. goto out;
  3057. }
  3058. }
  3059. stop_loop = 0;
  3060. while (1) {
  3061. u64 bytes;
  3062. l = path->nodes[0];
  3063. slot = path->slots[0];
  3064. if (slot >= btrfs_header_nritems(l)) {
  3065. ret = btrfs_next_leaf(root, path);
  3066. if (ret == 0)
  3067. continue;
  3068. if (ret < 0)
  3069. goto out;
  3070. stop_loop = 1;
  3071. break;
  3072. }
  3073. btrfs_item_key_to_cpu(l, &key, slot);
  3074. if (key.type != BTRFS_EXTENT_ITEM_KEY &&
  3075. key.type != BTRFS_METADATA_ITEM_KEY)
  3076. goto next;
  3077. if (key.type == BTRFS_METADATA_ITEM_KEY)
  3078. bytes = fs_info->nodesize;
  3079. else
  3080. bytes = key.offset;
  3081. if (key.objectid + bytes <= logical)
  3082. goto next;
  3083. if (key.objectid >= logical + map->stripe_len) {
  3084. /* out of this device extent */
  3085. if (key.objectid >= logic_end)
  3086. stop_loop = 1;
  3087. break;
  3088. }
  3089. extent = btrfs_item_ptr(l, slot,
  3090. struct btrfs_extent_item);
  3091. flags = btrfs_extent_flags(l, extent);
  3092. generation = btrfs_extent_generation(l, extent);
  3093. if ((flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) &&
  3094. (key.objectid < logical ||
  3095. key.objectid + bytes >
  3096. logical + map->stripe_len)) {
  3097. btrfs_err(fs_info,
  3098. "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
  3099. key.objectid, logical);
  3100. spin_lock(&sctx->stat_lock);
  3101. sctx->stat.uncorrectable_errors++;
  3102. spin_unlock(&sctx->stat_lock);
  3103. goto next;
  3104. }
  3105. again:
  3106. extent_logical = key.objectid;
  3107. extent_len = bytes;
  3108. /*
  3109. * trim extent to this stripe
  3110. */
  3111. if (extent_logical < logical) {
  3112. extent_len -= logical - extent_logical;
  3113. extent_logical = logical;
  3114. }
  3115. if (extent_logical + extent_len >
  3116. logical + map->stripe_len) {
  3117. extent_len = logical + map->stripe_len -
  3118. extent_logical;
  3119. }
  3120. extent_physical = extent_logical - logical + physical;
  3121. extent_dev = scrub_dev;
  3122. extent_mirror_num = mirror_num;
  3123. if (is_dev_replace)
  3124. scrub_remap_extent(fs_info, extent_logical,
  3125. extent_len, &extent_physical,
  3126. &extent_dev,
  3127. &extent_mirror_num);
  3128. ret = btrfs_lookup_csums_range(csum_root,
  3129. extent_logical,
  3130. extent_logical +
  3131. extent_len - 1,
  3132. &sctx->csum_list, 1);
  3133. if (ret)
  3134. goto out;
  3135. ret = scrub_extent(sctx, extent_logical, extent_len,
  3136. extent_physical, extent_dev, flags,
  3137. generation, extent_mirror_num,
  3138. extent_logical - logical + physical);
  3139. scrub_free_csums(sctx);
  3140. if (ret)
  3141. goto out;
  3142. if (extent_logical + extent_len <
  3143. key.objectid + bytes) {
  3144. if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
  3145. /*
  3146. * loop until we find next data stripe
  3147. * or we have finished all stripes.
  3148. */
  3149. loop:
  3150. physical += map->stripe_len;
  3151. ret = get_raid56_logic_offset(physical,
  3152. num, map, &logical,
  3153. &stripe_logical);
  3154. logical += base;
  3155. if (ret && physical < physical_end) {
  3156. stripe_logical += base;
  3157. stripe_end = stripe_logical +
  3158. increment;
  3159. ret = scrub_raid56_parity(sctx,
  3160. map, scrub_dev, ppath,
  3161. stripe_logical,
  3162. stripe_end);
  3163. if (ret)
  3164. goto out;
  3165. goto loop;
  3166. }
  3167. } else {
  3168. physical += map->stripe_len;
  3169. logical += increment;
  3170. }
  3171. if (logical < key.objectid + bytes) {
  3172. cond_resched();
  3173. goto again;
  3174. }
  3175. if (physical >= physical_end) {
  3176. stop_loop = 1;
  3177. break;
  3178. }
  3179. }
  3180. next:
  3181. path->slots[0]++;
  3182. }
  3183. btrfs_release_path(path);
  3184. skip:
  3185. logical += increment;
  3186. physical += map->stripe_len;
  3187. spin_lock(&sctx->stat_lock);
  3188. if (stop_loop)
  3189. sctx->stat.last_physical = map->stripes[num].physical +
  3190. length;
  3191. else
  3192. sctx->stat.last_physical = physical;
  3193. spin_unlock(&sctx->stat_lock);
  3194. if (stop_loop)
  3195. break;
  3196. }
  3197. out:
  3198. /* push queued extents */
  3199. scrub_submit(sctx);
  3200. mutex_lock(&sctx->wr_lock);
  3201. scrub_wr_submit(sctx);
  3202. mutex_unlock(&sctx->wr_lock);
  3203. blk_finish_plug(&plug);
  3204. btrfs_free_path(path);
  3205. btrfs_free_path(ppath);
  3206. return ret < 0 ? ret : 0;
  3207. }
  3208. static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
  3209. struct btrfs_device *scrub_dev,
  3210. u64 chunk_offset, u64 length,
  3211. u64 dev_offset,
  3212. struct btrfs_block_group_cache *cache,
  3213. int is_dev_replace)
  3214. {
  3215. struct btrfs_fs_info *fs_info = sctx->fs_info;
  3216. struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
  3217. struct map_lookup *map;
  3218. struct extent_map *em;
  3219. int i;
  3220. int ret = 0;
  3221. read_lock(&map_tree->map_tree.lock);
  3222. em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
  3223. read_unlock(&map_tree->map_tree.lock);
  3224. if (!em) {
  3225. /*
  3226. * Might have been an unused block group deleted by the cleaner
  3227. * kthread or relocation.
  3228. */
  3229. spin_lock(&cache->lock);
  3230. if (!cache->removed)
  3231. ret = -EINVAL;
  3232. spin_unlock(&cache->lock);
  3233. return ret;
  3234. }
  3235. map = em->map_lookup;
  3236. if (em->start != chunk_offset)
  3237. goto out;
  3238. if (em->len < length)
  3239. goto out;
  3240. for (i = 0; i < map->num_stripes; ++i) {
  3241. if (map->stripes[i].dev->bdev == scrub_dev->bdev &&
  3242. map->stripes[i].physical == dev_offset) {
  3243. ret = scrub_stripe(sctx, map, scrub_dev, i,
  3244. chunk_offset, length,
  3245. is_dev_replace);
  3246. if (ret)
  3247. goto out;
  3248. }
  3249. }
  3250. out:
  3251. free_extent_map(em);
  3252. return ret;
  3253. }
  3254. static noinline_for_stack
  3255. int scrub_enumerate_chunks(struct scrub_ctx *sctx,
  3256. struct btrfs_device *scrub_dev, u64 start, u64 end,
  3257. int is_dev_replace)
  3258. {
  3259. struct btrfs_dev_extent *dev_extent = NULL;
  3260. struct btrfs_path *path;
  3261. struct btrfs_fs_info *fs_info = sctx->fs_info;
  3262. struct btrfs_root *root = fs_info->dev_root;
  3263. u64 length;
  3264. u64 chunk_offset;
  3265. int ret = 0;
  3266. int ro_set;
  3267. int slot;
  3268. struct extent_buffer *l;
  3269. struct btrfs_key key;
  3270. struct btrfs_key found_key;
  3271. struct btrfs_block_group_cache *cache;
  3272. struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
  3273. path = btrfs_alloc_path();
  3274. if (!path)
  3275. return -ENOMEM;
  3276. path->reada = READA_FORWARD;
  3277. path->search_commit_root = 1;
  3278. path->skip_locking = 1;
  3279. key.objectid = scrub_dev->devid;
  3280. key.offset = 0ull;
  3281. key.type = BTRFS_DEV_EXTENT_KEY;
  3282. while (1) {
  3283. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3284. if (ret < 0)
  3285. break;
  3286. if (ret > 0) {
  3287. if (path->slots[0] >=
  3288. btrfs_header_nritems(path->nodes[0])) {
  3289. ret = btrfs_next_leaf(root, path);
  3290. if (ret < 0)
  3291. break;
  3292. if (ret > 0) {
  3293. ret = 0;
  3294. break;
  3295. }
  3296. } else {
  3297. ret = 0;
  3298. }
  3299. }
  3300. l = path->nodes[0];
  3301. slot = path->slots[0];
  3302. btrfs_item_key_to_cpu(l, &found_key, slot);
  3303. if (found_key.objectid != scrub_dev->devid)
  3304. break;
  3305. if (found_key.type != BTRFS_DEV_EXTENT_KEY)
  3306. break;
  3307. if (found_key.offset >= end)
  3308. break;
  3309. if (found_key.offset < key.offset)
  3310. break;
  3311. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  3312. length = btrfs_dev_extent_length(l, dev_extent);
  3313. if (found_key.offset + length <= start)
  3314. goto skip;
  3315. chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
  3316. /*
  3317. * get a reference on the corresponding block group to prevent
  3318. * the chunk from going away while we scrub it
  3319. */
  3320. cache = btrfs_lookup_block_group(fs_info, chunk_offset);
  3321. /* some chunks are removed but not committed to disk yet,
  3322. * continue scrubbing */
  3323. if (!cache)
  3324. goto skip;
  3325. /*
  3326. * we need call btrfs_inc_block_group_ro() with scrubs_paused,
  3327. * to avoid deadlock caused by:
  3328. * btrfs_inc_block_group_ro()
  3329. * -> btrfs_wait_for_commit()
  3330. * -> btrfs_commit_transaction()
  3331. * -> btrfs_scrub_pause()
  3332. */
  3333. scrub_pause_on(fs_info);
  3334. ret = btrfs_inc_block_group_ro(fs_info, cache);
  3335. if (!ret && is_dev_replace) {
  3336. /*
  3337. * If we are doing a device replace wait for any tasks
  3338. * that started dellaloc right before we set the block
  3339. * group to RO mode, as they might have just allocated
  3340. * an extent from it or decided they could do a nocow
  3341. * write. And if any such tasks did that, wait for their
  3342. * ordered extents to complete and then commit the
  3343. * current transaction, so that we can later see the new
  3344. * extent items in the extent tree - the ordered extents
  3345. * create delayed data references (for cow writes) when
  3346. * they complete, which will be run and insert the
  3347. * corresponding extent items into the extent tree when
  3348. * we commit the transaction they used when running
  3349. * inode.c:btrfs_finish_ordered_io(). We later use
  3350. * the commit root of the extent tree to find extents
  3351. * to copy from the srcdev into the tgtdev, and we don't
  3352. * want to miss any new extents.
  3353. */
  3354. btrfs_wait_block_group_reservations(cache);
  3355. btrfs_wait_nocow_writers(cache);
  3356. ret = btrfs_wait_ordered_roots(fs_info, U64_MAX,
  3357. cache->key.objectid,
  3358. cache->key.offset);
  3359. if (ret > 0) {
  3360. struct btrfs_trans_handle *trans;
  3361. trans = btrfs_join_transaction(root);
  3362. if (IS_ERR(trans))
  3363. ret = PTR_ERR(trans);
  3364. else
  3365. ret = btrfs_commit_transaction(trans);
  3366. if (ret) {
  3367. scrub_pause_off(fs_info);
  3368. btrfs_put_block_group(cache);
  3369. break;
  3370. }
  3371. }
  3372. }
  3373. scrub_pause_off(fs_info);
  3374. if (ret == 0) {
  3375. ro_set = 1;
  3376. } else if (ret == -ENOSPC) {
  3377. /*
  3378. * btrfs_inc_block_group_ro return -ENOSPC when it
  3379. * failed in creating new chunk for metadata.
  3380. * It is not a problem for scrub/replace, because
  3381. * metadata are always cowed, and our scrub paused
  3382. * commit_transactions.
  3383. */
  3384. ro_set = 0;
  3385. } else {
  3386. btrfs_warn(fs_info,
  3387. "failed setting block group ro, ret=%d\n",
  3388. ret);
  3389. btrfs_put_block_group(cache);
  3390. break;
  3391. }
  3392. btrfs_dev_replace_lock(&fs_info->dev_replace, 1);
  3393. dev_replace->cursor_right = found_key.offset + length;
  3394. dev_replace->cursor_left = found_key.offset;
  3395. dev_replace->item_needs_writeback = 1;
  3396. btrfs_dev_replace_unlock(&fs_info->dev_replace, 1);
  3397. ret = scrub_chunk(sctx, scrub_dev, chunk_offset, length,
  3398. found_key.offset, cache, is_dev_replace);
  3399. /*
  3400. * flush, submit all pending read and write bios, afterwards
  3401. * wait for them.
  3402. * Note that in the dev replace case, a read request causes
  3403. * write requests that are submitted in the read completion
  3404. * worker. Therefore in the current situation, it is required
  3405. * that all write requests are flushed, so that all read and
  3406. * write requests are really completed when bios_in_flight
  3407. * changes to 0.
  3408. */
  3409. atomic_set(&sctx->flush_all_writes, 1);
  3410. scrub_submit(sctx);
  3411. mutex_lock(&sctx->wr_lock);
  3412. scrub_wr_submit(sctx);
  3413. mutex_unlock(&sctx->wr_lock);
  3414. wait_event(sctx->list_wait,
  3415. atomic_read(&sctx->bios_in_flight) == 0);
  3416. scrub_pause_on(fs_info);
  3417. /*
  3418. * must be called before we decrease @scrub_paused.
  3419. * make sure we don't block transaction commit while
  3420. * we are waiting pending workers finished.
  3421. */
  3422. wait_event(sctx->list_wait,
  3423. atomic_read(&sctx->workers_pending) == 0);
  3424. atomic_set(&sctx->flush_all_writes, 0);
  3425. scrub_pause_off(fs_info);
  3426. btrfs_dev_replace_lock(&fs_info->dev_replace, 1);
  3427. dev_replace->cursor_left = dev_replace->cursor_right;
  3428. dev_replace->item_needs_writeback = 1;
  3429. btrfs_dev_replace_unlock(&fs_info->dev_replace, 1);
  3430. if (ro_set)
  3431. btrfs_dec_block_group_ro(cache);
  3432. /*
  3433. * We might have prevented the cleaner kthread from deleting
  3434. * this block group if it was already unused because we raced
  3435. * and set it to RO mode first. So add it back to the unused
  3436. * list, otherwise it might not ever be deleted unless a manual
  3437. * balance is triggered or it becomes used and unused again.
  3438. */
  3439. spin_lock(&cache->lock);
  3440. if (!cache->removed && !cache->ro && cache->reserved == 0 &&
  3441. btrfs_block_group_used(&cache->item) == 0) {
  3442. spin_unlock(&cache->lock);
  3443. spin_lock(&fs_info->unused_bgs_lock);
  3444. if (list_empty(&cache->bg_list)) {
  3445. btrfs_get_block_group(cache);
  3446. list_add_tail(&cache->bg_list,
  3447. &fs_info->unused_bgs);
  3448. }
  3449. spin_unlock(&fs_info->unused_bgs_lock);
  3450. } else {
  3451. spin_unlock(&cache->lock);
  3452. }
  3453. btrfs_put_block_group(cache);
  3454. if (ret)
  3455. break;
  3456. if (is_dev_replace &&
  3457. atomic64_read(&dev_replace->num_write_errors) > 0) {
  3458. ret = -EIO;
  3459. break;
  3460. }
  3461. if (sctx->stat.malloc_errors > 0) {
  3462. ret = -ENOMEM;
  3463. break;
  3464. }
  3465. skip:
  3466. key.offset = found_key.offset + length;
  3467. btrfs_release_path(path);
  3468. }
  3469. btrfs_free_path(path);
  3470. return ret;
  3471. }
  3472. static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,
  3473. struct btrfs_device *scrub_dev)
  3474. {
  3475. int i;
  3476. u64 bytenr;
  3477. u64 gen;
  3478. int ret;
  3479. struct btrfs_fs_info *fs_info = sctx->fs_info;
  3480. if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
  3481. return -EIO;
  3482. /* Seed devices of a new filesystem has their own generation. */
  3483. if (scrub_dev->fs_devices != fs_info->fs_devices)
  3484. gen = scrub_dev->generation;
  3485. else
  3486. gen = fs_info->last_trans_committed;
  3487. for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
  3488. bytenr = btrfs_sb_offset(i);
  3489. if (bytenr + BTRFS_SUPER_INFO_SIZE >
  3490. scrub_dev->commit_total_bytes)
  3491. break;
  3492. ret = scrub_pages(sctx, bytenr, BTRFS_SUPER_INFO_SIZE, bytenr,
  3493. scrub_dev, BTRFS_EXTENT_FLAG_SUPER, gen, i,
  3494. NULL, 1, bytenr);
  3495. if (ret)
  3496. return ret;
  3497. }
  3498. wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
  3499. return 0;
  3500. }
  3501. /*
  3502. * get a reference count on fs_info->scrub_workers. start worker if necessary
  3503. */
  3504. static noinline_for_stack int scrub_workers_get(struct btrfs_fs_info *fs_info,
  3505. int is_dev_replace)
  3506. {
  3507. unsigned int flags = WQ_FREEZABLE | WQ_UNBOUND;
  3508. int max_active = fs_info->thread_pool_size;
  3509. if (fs_info->scrub_workers_refcnt == 0) {
  3510. if (is_dev_replace)
  3511. fs_info->scrub_workers =
  3512. btrfs_alloc_workqueue(fs_info, "scrub", flags,
  3513. 1, 4);
  3514. else
  3515. fs_info->scrub_workers =
  3516. btrfs_alloc_workqueue(fs_info, "scrub", flags,
  3517. max_active, 4);
  3518. if (!fs_info->scrub_workers)
  3519. goto fail_scrub_workers;
  3520. fs_info->scrub_wr_completion_workers =
  3521. btrfs_alloc_workqueue(fs_info, "scrubwrc", flags,
  3522. max_active, 2);
  3523. if (!fs_info->scrub_wr_completion_workers)
  3524. goto fail_scrub_wr_completion_workers;
  3525. fs_info->scrub_nocow_workers =
  3526. btrfs_alloc_workqueue(fs_info, "scrubnc", flags, 1, 0);
  3527. if (!fs_info->scrub_nocow_workers)
  3528. goto fail_scrub_nocow_workers;
  3529. fs_info->scrub_parity_workers =
  3530. btrfs_alloc_workqueue(fs_info, "scrubparity", flags,
  3531. max_active, 2);
  3532. if (!fs_info->scrub_parity_workers)
  3533. goto fail_scrub_parity_workers;
  3534. }
  3535. ++fs_info->scrub_workers_refcnt;
  3536. return 0;
  3537. fail_scrub_parity_workers:
  3538. btrfs_destroy_workqueue(fs_info->scrub_nocow_workers);
  3539. fail_scrub_nocow_workers:
  3540. btrfs_destroy_workqueue(fs_info->scrub_wr_completion_workers);
  3541. fail_scrub_wr_completion_workers:
  3542. btrfs_destroy_workqueue(fs_info->scrub_workers);
  3543. fail_scrub_workers:
  3544. return -ENOMEM;
  3545. }
  3546. static noinline_for_stack void scrub_workers_put(struct btrfs_fs_info *fs_info)
  3547. {
  3548. if (--fs_info->scrub_workers_refcnt == 0) {
  3549. btrfs_destroy_workqueue(fs_info->scrub_workers);
  3550. btrfs_destroy_workqueue(fs_info->scrub_wr_completion_workers);
  3551. btrfs_destroy_workqueue(fs_info->scrub_nocow_workers);
  3552. btrfs_destroy_workqueue(fs_info->scrub_parity_workers);
  3553. }
  3554. WARN_ON(fs_info->scrub_workers_refcnt < 0);
  3555. }
  3556. int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
  3557. u64 end, struct btrfs_scrub_progress *progress,
  3558. int readonly, int is_dev_replace)
  3559. {
  3560. struct scrub_ctx *sctx;
  3561. int ret;
  3562. struct btrfs_device *dev;
  3563. struct rcu_string *name;
  3564. if (btrfs_fs_closing(fs_info))
  3565. return -EINVAL;
  3566. if (fs_info->nodesize > BTRFS_STRIPE_LEN) {
  3567. /*
  3568. * in this case scrub is unable to calculate the checksum
  3569. * the way scrub is implemented. Do not handle this
  3570. * situation at all because it won't ever happen.
  3571. */
  3572. btrfs_err(fs_info,
  3573. "scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails",
  3574. fs_info->nodesize,
  3575. BTRFS_STRIPE_LEN);
  3576. return -EINVAL;
  3577. }
  3578. if (fs_info->sectorsize != PAGE_SIZE) {
  3579. /* not supported for data w/o checksums */
  3580. btrfs_err_rl(fs_info,
  3581. "scrub: size assumption sectorsize != PAGE_SIZE (%d != %lu) fails",
  3582. fs_info->sectorsize, PAGE_SIZE);
  3583. return -EINVAL;
  3584. }
  3585. if (fs_info->nodesize >
  3586. PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK ||
  3587. fs_info->sectorsize > PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK) {
  3588. /*
  3589. * would exhaust the array bounds of pagev member in
  3590. * struct scrub_block
  3591. */
  3592. btrfs_err(fs_info,
  3593. "scrub: size assumption nodesize and sectorsize <= SCRUB_MAX_PAGES_PER_BLOCK (%d <= %d && %d <= %d) fails",
  3594. fs_info->nodesize,
  3595. SCRUB_MAX_PAGES_PER_BLOCK,
  3596. fs_info->sectorsize,
  3597. SCRUB_MAX_PAGES_PER_BLOCK);
  3598. return -EINVAL;
  3599. }
  3600. mutex_lock(&fs_info->fs_devices->device_list_mutex);
  3601. dev = btrfs_find_device(fs_info, devid, NULL, NULL);
  3602. if (!dev || (dev->missing && !is_dev_replace)) {
  3603. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  3604. return -ENODEV;
  3605. }
  3606. if (!is_dev_replace && !readonly && !dev->writeable) {
  3607. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  3608. rcu_read_lock();
  3609. name = rcu_dereference(dev->name);
  3610. btrfs_err(fs_info, "scrub: device %s is not writable",
  3611. name->str);
  3612. rcu_read_unlock();
  3613. return -EROFS;
  3614. }
  3615. mutex_lock(&fs_info->scrub_lock);
  3616. if (!dev->in_fs_metadata || dev->is_tgtdev_for_dev_replace) {
  3617. mutex_unlock(&fs_info->scrub_lock);
  3618. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  3619. return -EIO;
  3620. }
  3621. btrfs_dev_replace_lock(&fs_info->dev_replace, 0);
  3622. if (dev->scrub_device ||
  3623. (!is_dev_replace &&
  3624. btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))) {
  3625. btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
  3626. mutex_unlock(&fs_info->scrub_lock);
  3627. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  3628. return -EINPROGRESS;
  3629. }
  3630. btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
  3631. ret = scrub_workers_get(fs_info, is_dev_replace);
  3632. if (ret) {
  3633. mutex_unlock(&fs_info->scrub_lock);
  3634. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  3635. return ret;
  3636. }
  3637. sctx = scrub_setup_ctx(dev, is_dev_replace);
  3638. if (IS_ERR(sctx)) {
  3639. mutex_unlock(&fs_info->scrub_lock);
  3640. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  3641. scrub_workers_put(fs_info);
  3642. return PTR_ERR(sctx);
  3643. }
  3644. sctx->readonly = readonly;
  3645. dev->scrub_device = sctx;
  3646. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  3647. /*
  3648. * checking @scrub_pause_req here, we can avoid
  3649. * race between committing transaction and scrubbing.
  3650. */
  3651. __scrub_blocked_if_needed(fs_info);
  3652. atomic_inc(&fs_info->scrubs_running);
  3653. mutex_unlock(&fs_info->scrub_lock);
  3654. if (!is_dev_replace) {
  3655. /*
  3656. * by holding device list mutex, we can
  3657. * kick off writing super in log tree sync.
  3658. */
  3659. mutex_lock(&fs_info->fs_devices->device_list_mutex);
  3660. ret = scrub_supers(sctx, dev);
  3661. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  3662. }
  3663. if (!ret)
  3664. ret = scrub_enumerate_chunks(sctx, dev, start, end,
  3665. is_dev_replace);
  3666. wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
  3667. atomic_dec(&fs_info->scrubs_running);
  3668. wake_up(&fs_info->scrub_pause_wait);
  3669. wait_event(sctx->list_wait, atomic_read(&sctx->workers_pending) == 0);
  3670. if (progress)
  3671. memcpy(progress, &sctx->stat, sizeof(*progress));
  3672. mutex_lock(&fs_info->scrub_lock);
  3673. dev->scrub_device = NULL;
  3674. scrub_workers_put(fs_info);
  3675. mutex_unlock(&fs_info->scrub_lock);
  3676. scrub_put_ctx(sctx);
  3677. return ret;
  3678. }
  3679. void btrfs_scrub_pause(struct btrfs_fs_info *fs_info)
  3680. {
  3681. mutex_lock(&fs_info->scrub_lock);
  3682. atomic_inc(&fs_info->scrub_pause_req);
  3683. while (atomic_read(&fs_info->scrubs_paused) !=
  3684. atomic_read(&fs_info->scrubs_running)) {
  3685. mutex_unlock(&fs_info->scrub_lock);
  3686. wait_event(fs_info->scrub_pause_wait,
  3687. atomic_read(&fs_info->scrubs_paused) ==
  3688. atomic_read(&fs_info->scrubs_running));
  3689. mutex_lock(&fs_info->scrub_lock);
  3690. }
  3691. mutex_unlock(&fs_info->scrub_lock);
  3692. }
  3693. void btrfs_scrub_continue(struct btrfs_fs_info *fs_info)
  3694. {
  3695. atomic_dec(&fs_info->scrub_pause_req);
  3696. wake_up(&fs_info->scrub_pause_wait);
  3697. }
  3698. int btrfs_scrub_cancel(struct btrfs_fs_info *fs_info)
  3699. {
  3700. mutex_lock(&fs_info->scrub_lock);
  3701. if (!atomic_read(&fs_info->scrubs_running)) {
  3702. mutex_unlock(&fs_info->scrub_lock);
  3703. return -ENOTCONN;
  3704. }
  3705. atomic_inc(&fs_info->scrub_cancel_req);
  3706. while (atomic_read(&fs_info->scrubs_running)) {
  3707. mutex_unlock(&fs_info->scrub_lock);
  3708. wait_event(fs_info->scrub_pause_wait,
  3709. atomic_read(&fs_info->scrubs_running) == 0);
  3710. mutex_lock(&fs_info->scrub_lock);
  3711. }
  3712. atomic_dec(&fs_info->scrub_cancel_req);
  3713. mutex_unlock(&fs_info->scrub_lock);
  3714. return 0;
  3715. }
  3716. int btrfs_scrub_cancel_dev(struct btrfs_fs_info *fs_info,
  3717. struct btrfs_device *dev)
  3718. {
  3719. struct scrub_ctx *sctx;
  3720. mutex_lock(&fs_info->scrub_lock);
  3721. sctx = dev->scrub_device;
  3722. if (!sctx) {
  3723. mutex_unlock(&fs_info->scrub_lock);
  3724. return -ENOTCONN;
  3725. }
  3726. atomic_inc(&sctx->cancel_req);
  3727. while (dev->scrub_device) {
  3728. mutex_unlock(&fs_info->scrub_lock);
  3729. wait_event(fs_info->scrub_pause_wait,
  3730. dev->scrub_device == NULL);
  3731. mutex_lock(&fs_info->scrub_lock);
  3732. }
  3733. mutex_unlock(&fs_info->scrub_lock);
  3734. return 0;
  3735. }
  3736. int btrfs_scrub_progress(struct btrfs_fs_info *fs_info, u64 devid,
  3737. struct btrfs_scrub_progress *progress)
  3738. {
  3739. struct btrfs_device *dev;
  3740. struct scrub_ctx *sctx = NULL;
  3741. mutex_lock(&fs_info->fs_devices->device_list_mutex);
  3742. dev = btrfs_find_device(fs_info, devid, NULL, NULL);
  3743. if (dev)
  3744. sctx = dev->scrub_device;
  3745. if (sctx)
  3746. memcpy(progress, &sctx->stat, sizeof(*progress));
  3747. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  3748. return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV;
  3749. }
  3750. static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
  3751. u64 extent_logical, u64 extent_len,
  3752. u64 *extent_physical,
  3753. struct btrfs_device **extent_dev,
  3754. int *extent_mirror_num)
  3755. {
  3756. u64 mapped_length;
  3757. struct btrfs_bio *bbio = NULL;
  3758. int ret;
  3759. mapped_length = extent_len;
  3760. ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, extent_logical,
  3761. &mapped_length, &bbio, 0);
  3762. if (ret || !bbio || mapped_length < extent_len ||
  3763. !bbio->stripes[0].dev->bdev) {
  3764. btrfs_put_bbio(bbio);
  3765. return;
  3766. }
  3767. *extent_physical = bbio->stripes[0].physical;
  3768. *extent_mirror_num = bbio->mirror_num;
  3769. *extent_dev = bbio->stripes[0].dev;
  3770. btrfs_put_bbio(bbio);
  3771. }
  3772. static int copy_nocow_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
  3773. int mirror_num, u64 physical_for_dev_replace)
  3774. {
  3775. struct scrub_copy_nocow_ctx *nocow_ctx;
  3776. struct btrfs_fs_info *fs_info = sctx->fs_info;
  3777. nocow_ctx = kzalloc(sizeof(*nocow_ctx), GFP_NOFS);
  3778. if (!nocow_ctx) {
  3779. spin_lock(&sctx->stat_lock);
  3780. sctx->stat.malloc_errors++;
  3781. spin_unlock(&sctx->stat_lock);
  3782. return -ENOMEM;
  3783. }
  3784. scrub_pending_trans_workers_inc(sctx);
  3785. nocow_ctx->sctx = sctx;
  3786. nocow_ctx->logical = logical;
  3787. nocow_ctx->len = len;
  3788. nocow_ctx->mirror_num = mirror_num;
  3789. nocow_ctx->physical_for_dev_replace = physical_for_dev_replace;
  3790. btrfs_init_work(&nocow_ctx->work, btrfs_scrubnc_helper,
  3791. copy_nocow_pages_worker, NULL, NULL);
  3792. INIT_LIST_HEAD(&nocow_ctx->inodes);
  3793. btrfs_queue_work(fs_info->scrub_nocow_workers,
  3794. &nocow_ctx->work);
  3795. return 0;
  3796. }
  3797. static int record_inode_for_nocow(u64 inum, u64 offset, u64 root, void *ctx)
  3798. {
  3799. struct scrub_copy_nocow_ctx *nocow_ctx = ctx;
  3800. struct scrub_nocow_inode *nocow_inode;
  3801. nocow_inode = kzalloc(sizeof(*nocow_inode), GFP_NOFS);
  3802. if (!nocow_inode)
  3803. return -ENOMEM;
  3804. nocow_inode->inum = inum;
  3805. nocow_inode->offset = offset;
  3806. nocow_inode->root = root;
  3807. list_add_tail(&nocow_inode->list, &nocow_ctx->inodes);
  3808. return 0;
  3809. }
  3810. #define COPY_COMPLETE 1
  3811. static void copy_nocow_pages_worker(struct btrfs_work *work)
  3812. {
  3813. struct scrub_copy_nocow_ctx *nocow_ctx =
  3814. container_of(work, struct scrub_copy_nocow_ctx, work);
  3815. struct scrub_ctx *sctx = nocow_ctx->sctx;
  3816. struct btrfs_fs_info *fs_info = sctx->fs_info;
  3817. struct btrfs_root *root = fs_info->extent_root;
  3818. u64 logical = nocow_ctx->logical;
  3819. u64 len = nocow_ctx->len;
  3820. int mirror_num = nocow_ctx->mirror_num;
  3821. u64 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
  3822. int ret;
  3823. struct btrfs_trans_handle *trans = NULL;
  3824. struct btrfs_path *path;
  3825. int not_written = 0;
  3826. path = btrfs_alloc_path();
  3827. if (!path) {
  3828. spin_lock(&sctx->stat_lock);
  3829. sctx->stat.malloc_errors++;
  3830. spin_unlock(&sctx->stat_lock);
  3831. not_written = 1;
  3832. goto out;
  3833. }
  3834. trans = btrfs_join_transaction(root);
  3835. if (IS_ERR(trans)) {
  3836. not_written = 1;
  3837. goto out;
  3838. }
  3839. ret = iterate_inodes_from_logical(logical, fs_info, path,
  3840. record_inode_for_nocow, nocow_ctx);
  3841. if (ret != 0 && ret != -ENOENT) {
  3842. btrfs_warn(fs_info,
  3843. "iterate_inodes_from_logical() failed: log %llu, phys %llu, len %llu, mir %u, ret %d",
  3844. logical, physical_for_dev_replace, len, mirror_num,
  3845. ret);
  3846. not_written = 1;
  3847. goto out;
  3848. }
  3849. btrfs_end_transaction(trans);
  3850. trans = NULL;
  3851. while (!list_empty(&nocow_ctx->inodes)) {
  3852. struct scrub_nocow_inode *entry;
  3853. entry = list_first_entry(&nocow_ctx->inodes,
  3854. struct scrub_nocow_inode,
  3855. list);
  3856. list_del_init(&entry->list);
  3857. ret = copy_nocow_pages_for_inode(entry->inum, entry->offset,
  3858. entry->root, nocow_ctx);
  3859. kfree(entry);
  3860. if (ret == COPY_COMPLETE) {
  3861. ret = 0;
  3862. break;
  3863. } else if (ret) {
  3864. break;
  3865. }
  3866. }
  3867. out:
  3868. while (!list_empty(&nocow_ctx->inodes)) {
  3869. struct scrub_nocow_inode *entry;
  3870. entry = list_first_entry(&nocow_ctx->inodes,
  3871. struct scrub_nocow_inode,
  3872. list);
  3873. list_del_init(&entry->list);
  3874. kfree(entry);
  3875. }
  3876. if (trans && !IS_ERR(trans))
  3877. btrfs_end_transaction(trans);
  3878. if (not_written)
  3879. btrfs_dev_replace_stats_inc(&fs_info->dev_replace.
  3880. num_uncorrectable_read_errors);
  3881. btrfs_free_path(path);
  3882. kfree(nocow_ctx);
  3883. scrub_pending_trans_workers_dec(sctx);
  3884. }
  3885. static int check_extent_to_block(struct btrfs_inode *inode, u64 start, u64 len,
  3886. u64 logical)
  3887. {
  3888. struct extent_state *cached_state = NULL;
  3889. struct btrfs_ordered_extent *ordered;
  3890. struct extent_io_tree *io_tree;
  3891. struct extent_map *em;
  3892. u64 lockstart = start, lockend = start + len - 1;
  3893. int ret = 0;
  3894. io_tree = &inode->io_tree;
  3895. lock_extent_bits(io_tree, lockstart, lockend, &cached_state);
  3896. ordered = btrfs_lookup_ordered_range(inode, lockstart, len);
  3897. if (ordered) {
  3898. btrfs_put_ordered_extent(ordered);
  3899. ret = 1;
  3900. goto out_unlock;
  3901. }
  3902. em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
  3903. if (IS_ERR(em)) {
  3904. ret = PTR_ERR(em);
  3905. goto out_unlock;
  3906. }
  3907. /*
  3908. * This extent does not actually cover the logical extent anymore,
  3909. * move on to the next inode.
  3910. */
  3911. if (em->block_start > logical ||
  3912. em->block_start + em->block_len < logical + len) {
  3913. free_extent_map(em);
  3914. ret = 1;
  3915. goto out_unlock;
  3916. }
  3917. free_extent_map(em);
  3918. out_unlock:
  3919. unlock_extent_cached(io_tree, lockstart, lockend, &cached_state,
  3920. GFP_NOFS);
  3921. return ret;
  3922. }
  3923. static int copy_nocow_pages_for_inode(u64 inum, u64 offset, u64 root,
  3924. struct scrub_copy_nocow_ctx *nocow_ctx)
  3925. {
  3926. struct btrfs_fs_info *fs_info = nocow_ctx->sctx->fs_info;
  3927. struct btrfs_key key;
  3928. struct inode *inode;
  3929. struct page *page;
  3930. struct btrfs_root *local_root;
  3931. struct extent_io_tree *io_tree;
  3932. u64 physical_for_dev_replace;
  3933. u64 nocow_ctx_logical;
  3934. u64 len = nocow_ctx->len;
  3935. unsigned long index;
  3936. int srcu_index;
  3937. int ret = 0;
  3938. int err = 0;
  3939. key.objectid = root;
  3940. key.type = BTRFS_ROOT_ITEM_KEY;
  3941. key.offset = (u64)-1;
  3942. srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
  3943. local_root = btrfs_read_fs_root_no_name(fs_info, &key);
  3944. if (IS_ERR(local_root)) {
  3945. srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
  3946. return PTR_ERR(local_root);
  3947. }
  3948. key.type = BTRFS_INODE_ITEM_KEY;
  3949. key.objectid = inum;
  3950. key.offset = 0;
  3951. inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
  3952. srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
  3953. if (IS_ERR(inode))
  3954. return PTR_ERR(inode);
  3955. /* Avoid truncate/dio/punch hole.. */
  3956. inode_lock(inode);
  3957. inode_dio_wait(inode);
  3958. physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
  3959. io_tree = &BTRFS_I(inode)->io_tree;
  3960. nocow_ctx_logical = nocow_ctx->logical;
  3961. ret = check_extent_to_block(BTRFS_I(inode), offset, len,
  3962. nocow_ctx_logical);
  3963. if (ret) {
  3964. ret = ret > 0 ? 0 : ret;
  3965. goto out;
  3966. }
  3967. while (len >= PAGE_SIZE) {
  3968. index = offset >> PAGE_SHIFT;
  3969. again:
  3970. page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
  3971. if (!page) {
  3972. btrfs_err(fs_info, "find_or_create_page() failed");
  3973. ret = -ENOMEM;
  3974. goto out;
  3975. }
  3976. if (PageUptodate(page)) {
  3977. if (PageDirty(page))
  3978. goto next_page;
  3979. } else {
  3980. ClearPageError(page);
  3981. err = extent_read_full_page(io_tree, page,
  3982. btrfs_get_extent,
  3983. nocow_ctx->mirror_num);
  3984. if (err) {
  3985. ret = err;
  3986. goto next_page;
  3987. }
  3988. lock_page(page);
  3989. /*
  3990. * If the page has been remove from the page cache,
  3991. * the data on it is meaningless, because it may be
  3992. * old one, the new data may be written into the new
  3993. * page in the page cache.
  3994. */
  3995. if (page->mapping != inode->i_mapping) {
  3996. unlock_page(page);
  3997. put_page(page);
  3998. goto again;
  3999. }
  4000. if (!PageUptodate(page)) {
  4001. ret = -EIO;
  4002. goto next_page;
  4003. }
  4004. }
  4005. ret = check_extent_to_block(BTRFS_I(inode), offset, len,
  4006. nocow_ctx_logical);
  4007. if (ret) {
  4008. ret = ret > 0 ? 0 : ret;
  4009. goto next_page;
  4010. }
  4011. err = write_page_nocow(nocow_ctx->sctx,
  4012. physical_for_dev_replace, page);
  4013. if (err)
  4014. ret = err;
  4015. next_page:
  4016. unlock_page(page);
  4017. put_page(page);
  4018. if (ret)
  4019. break;
  4020. offset += PAGE_SIZE;
  4021. physical_for_dev_replace += PAGE_SIZE;
  4022. nocow_ctx_logical += PAGE_SIZE;
  4023. len -= PAGE_SIZE;
  4024. }
  4025. ret = COPY_COMPLETE;
  4026. out:
  4027. inode_unlock(inode);
  4028. iput(inode);
  4029. return ret;
  4030. }
  4031. static int write_page_nocow(struct scrub_ctx *sctx,
  4032. u64 physical_for_dev_replace, struct page *page)
  4033. {
  4034. struct bio *bio;
  4035. struct btrfs_device *dev;
  4036. int ret;
  4037. dev = sctx->wr_tgtdev;
  4038. if (!dev)
  4039. return -EIO;
  4040. if (!dev->bdev) {
  4041. btrfs_warn_rl(dev->fs_info,
  4042. "scrub write_page_nocow(bdev == NULL) is unexpected");
  4043. return -EIO;
  4044. }
  4045. bio = btrfs_io_bio_alloc(1);
  4046. bio->bi_iter.bi_size = 0;
  4047. bio->bi_iter.bi_sector = physical_for_dev_replace >> 9;
  4048. bio->bi_bdev = dev->bdev;
  4049. bio->bi_opf = REQ_OP_WRITE | REQ_SYNC;
  4050. ret = bio_add_page(bio, page, PAGE_SIZE, 0);
  4051. if (ret != PAGE_SIZE) {
  4052. leave_with_eio:
  4053. bio_put(bio);
  4054. btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
  4055. return -EIO;
  4056. }
  4057. if (btrfsic_submit_bio_wait(bio))
  4058. goto leave_with_eio;
  4059. bio_put(bio);
  4060. return 0;
  4061. }