scrub.c 110 KB

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