scrub.c 112 KB

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