scrub.c 121 KB

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