scrub.c 114 KB

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