scrub.c 116 KB

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