scrub.c 114 KB

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