scrub.c 111 KB

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