scrub.c 110 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231
  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 = kzalloc(BTRFS_MAX_MIRRORS *
  853. sizeof(*sblocks_for_recheck),
  854. GFP_NOFS);
  855. if (!sblocks_for_recheck) {
  856. spin_lock(&sctx->stat_lock);
  857. sctx->stat.malloc_errors++;
  858. sctx->stat.read_errors++;
  859. sctx->stat.uncorrectable_errors++;
  860. spin_unlock(&sctx->stat_lock);
  861. btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
  862. goto out;
  863. }
  864. /* setup the context, map the logical blocks and alloc the pages */
  865. ret = scrub_setup_recheck_block(sblock_to_check, sblocks_for_recheck);
  866. if (ret) {
  867. spin_lock(&sctx->stat_lock);
  868. sctx->stat.read_errors++;
  869. sctx->stat.uncorrectable_errors++;
  870. spin_unlock(&sctx->stat_lock);
  871. btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
  872. goto out;
  873. }
  874. BUG_ON(failed_mirror_index >= BTRFS_MAX_MIRRORS);
  875. sblock_bad = sblocks_for_recheck + failed_mirror_index;
  876. /* build and submit the bios for the failed mirror, check checksums */
  877. scrub_recheck_block(fs_info, sblock_bad, is_metadata, have_csum,
  878. csum, generation, sctx->csum_size, 1);
  879. if (!sblock_bad->header_error && !sblock_bad->checksum_error &&
  880. sblock_bad->no_io_error_seen) {
  881. /*
  882. * the error disappeared after reading page by page, or
  883. * the area was part of a huge bio and other parts of the
  884. * bio caused I/O errors, or the block layer merged several
  885. * read requests into one and the error is caused by a
  886. * different bio (usually one of the two latter cases is
  887. * the cause)
  888. */
  889. spin_lock(&sctx->stat_lock);
  890. sctx->stat.unverified_errors++;
  891. sblock_to_check->data_corrected = 1;
  892. spin_unlock(&sctx->stat_lock);
  893. if (sctx->is_dev_replace)
  894. scrub_write_block_to_dev_replace(sblock_bad);
  895. goto out;
  896. }
  897. if (!sblock_bad->no_io_error_seen) {
  898. spin_lock(&sctx->stat_lock);
  899. sctx->stat.read_errors++;
  900. spin_unlock(&sctx->stat_lock);
  901. if (__ratelimit(&_rs))
  902. scrub_print_warning("i/o error", sblock_to_check);
  903. btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
  904. } else if (sblock_bad->checksum_error) {
  905. spin_lock(&sctx->stat_lock);
  906. sctx->stat.csum_errors++;
  907. spin_unlock(&sctx->stat_lock);
  908. if (__ratelimit(&_rs))
  909. scrub_print_warning("checksum error", sblock_to_check);
  910. btrfs_dev_stat_inc_and_print(dev,
  911. BTRFS_DEV_STAT_CORRUPTION_ERRS);
  912. } else if (sblock_bad->header_error) {
  913. spin_lock(&sctx->stat_lock);
  914. sctx->stat.verify_errors++;
  915. spin_unlock(&sctx->stat_lock);
  916. if (__ratelimit(&_rs))
  917. scrub_print_warning("checksum/header error",
  918. sblock_to_check);
  919. if (sblock_bad->generation_error)
  920. btrfs_dev_stat_inc_and_print(dev,
  921. BTRFS_DEV_STAT_GENERATION_ERRS);
  922. else
  923. btrfs_dev_stat_inc_and_print(dev,
  924. BTRFS_DEV_STAT_CORRUPTION_ERRS);
  925. }
  926. if (sctx->readonly) {
  927. ASSERT(!sctx->is_dev_replace);
  928. goto out;
  929. }
  930. if (!is_metadata && !have_csum) {
  931. struct scrub_fixup_nodatasum *fixup_nodatasum;
  932. WARN_ON(sctx->is_dev_replace);
  933. nodatasum_case:
  934. /*
  935. * !is_metadata and !have_csum, this means that the data
  936. * might not be COW'ed, that it might be modified
  937. * concurrently. The general strategy to work on the
  938. * commit root does not help in the case when COW is not
  939. * used.
  940. */
  941. fixup_nodatasum = kzalloc(sizeof(*fixup_nodatasum), GFP_NOFS);
  942. if (!fixup_nodatasum)
  943. goto did_not_correct_error;
  944. fixup_nodatasum->sctx = sctx;
  945. fixup_nodatasum->dev = dev;
  946. fixup_nodatasum->logical = logical;
  947. fixup_nodatasum->root = fs_info->extent_root;
  948. fixup_nodatasum->mirror_num = failed_mirror_index + 1;
  949. scrub_pending_trans_workers_inc(sctx);
  950. btrfs_init_work(&fixup_nodatasum->work, btrfs_scrub_helper,
  951. scrub_fixup_nodatasum, NULL, NULL);
  952. btrfs_queue_work(fs_info->scrub_workers,
  953. &fixup_nodatasum->work);
  954. goto out;
  955. }
  956. /*
  957. * now build and submit the bios for the other mirrors, check
  958. * checksums.
  959. * First try to pick the mirror which is completely without I/O
  960. * errors and also does not have a checksum error.
  961. * If one is found, and if a checksum is present, the full block
  962. * that is known to contain an error is rewritten. Afterwards
  963. * the block is known to be corrected.
  964. * If a mirror is found which is completely correct, and no
  965. * checksum is present, only those pages are rewritten that had
  966. * an I/O error in the block to be repaired, since it cannot be
  967. * determined, which copy of the other pages is better (and it
  968. * could happen otherwise that a correct page would be
  969. * overwritten by a bad one).
  970. */
  971. for (mirror_index = 0;
  972. mirror_index < BTRFS_MAX_MIRRORS &&
  973. sblocks_for_recheck[mirror_index].page_count > 0;
  974. mirror_index++) {
  975. struct scrub_block *sblock_other;
  976. if (mirror_index == failed_mirror_index)
  977. continue;
  978. sblock_other = sblocks_for_recheck + mirror_index;
  979. /* build and submit the bios, check checksums */
  980. scrub_recheck_block(fs_info, sblock_other, is_metadata,
  981. have_csum, csum, generation,
  982. sctx->csum_size, 0);
  983. if (!sblock_other->header_error &&
  984. !sblock_other->checksum_error &&
  985. sblock_other->no_io_error_seen) {
  986. if (sctx->is_dev_replace) {
  987. scrub_write_block_to_dev_replace(sblock_other);
  988. goto corrected_error;
  989. } else {
  990. ret = scrub_repair_block_from_good_copy(
  991. sblock_bad, sblock_other);
  992. if (!ret)
  993. goto corrected_error;
  994. }
  995. }
  996. }
  997. if (sblock_bad->no_io_error_seen && !sctx->is_dev_replace)
  998. goto did_not_correct_error;
  999. /*
  1000. * In case of I/O errors in the area that is supposed to be
  1001. * repaired, continue by picking good copies of those pages.
  1002. * Select the good pages from mirrors to rewrite bad pages from
  1003. * the area to fix. Afterwards verify the checksum of the block
  1004. * that is supposed to be repaired. This verification step is
  1005. * only done for the purpose of statistic counting and for the
  1006. * final scrub report, whether errors remain.
  1007. * A perfect algorithm could make use of the checksum and try
  1008. * all possible combinations of pages from the different mirrors
  1009. * until the checksum verification succeeds. For example, when
  1010. * the 2nd page of mirror #1 faces I/O errors, and the 2nd page
  1011. * of mirror #2 is readable but the final checksum test fails,
  1012. * then the 2nd page of mirror #3 could be tried, whether now
  1013. * the final checksum succeedes. But this would be a rare
  1014. * exception and is therefore not implemented. At least it is
  1015. * avoided that the good copy is overwritten.
  1016. * A more useful improvement would be to pick the sectors
  1017. * without I/O error based on sector sizes (512 bytes on legacy
  1018. * disks) instead of on PAGE_SIZE. Then maybe 512 byte of one
  1019. * mirror could be repaired by taking 512 byte of a different
  1020. * mirror, even if other 512 byte sectors in the same PAGE_SIZE
  1021. * area are unreadable.
  1022. */
  1023. success = 1;
  1024. for (page_num = 0; page_num < sblock_bad->page_count;
  1025. page_num++) {
  1026. struct scrub_page *page_bad = sblock_bad->pagev[page_num];
  1027. struct scrub_block *sblock_other = NULL;
  1028. /* skip no-io-error page in scrub */
  1029. if (!page_bad->io_error && !sctx->is_dev_replace)
  1030. continue;
  1031. /* try to find no-io-error page in mirrors */
  1032. if (page_bad->io_error) {
  1033. for (mirror_index = 0;
  1034. mirror_index < BTRFS_MAX_MIRRORS &&
  1035. sblocks_for_recheck[mirror_index].page_count > 0;
  1036. mirror_index++) {
  1037. if (!sblocks_for_recheck[mirror_index].
  1038. pagev[page_num]->io_error) {
  1039. sblock_other = sblocks_for_recheck +
  1040. mirror_index;
  1041. break;
  1042. }
  1043. }
  1044. if (!sblock_other)
  1045. success = 0;
  1046. }
  1047. if (sctx->is_dev_replace) {
  1048. /*
  1049. * did not find a mirror to fetch the page
  1050. * from. scrub_write_page_to_dev_replace()
  1051. * handles this case (page->io_error), by
  1052. * filling the block with zeros before
  1053. * submitting the write request
  1054. */
  1055. if (!sblock_other)
  1056. sblock_other = sblock_bad;
  1057. if (scrub_write_page_to_dev_replace(sblock_other,
  1058. page_num) != 0) {
  1059. btrfs_dev_replace_stats_inc(
  1060. &sctx->dev_root->
  1061. fs_info->dev_replace.
  1062. num_write_errors);
  1063. success = 0;
  1064. }
  1065. } else if (sblock_other) {
  1066. ret = scrub_repair_page_from_good_copy(sblock_bad,
  1067. sblock_other,
  1068. page_num, 0);
  1069. if (0 == ret)
  1070. page_bad->io_error = 0;
  1071. else
  1072. success = 0;
  1073. }
  1074. }
  1075. if (success && !sctx->is_dev_replace) {
  1076. if (is_metadata || have_csum) {
  1077. /*
  1078. * need to verify the checksum now that all
  1079. * sectors on disk are repaired (the write
  1080. * request for data to be repaired is on its way).
  1081. * Just be lazy and use scrub_recheck_block()
  1082. * which re-reads the data before the checksum
  1083. * is verified, but most likely the data comes out
  1084. * of the page cache.
  1085. */
  1086. scrub_recheck_block(fs_info, sblock_bad,
  1087. is_metadata, have_csum, csum,
  1088. generation, sctx->csum_size, 1);
  1089. if (!sblock_bad->header_error &&
  1090. !sblock_bad->checksum_error &&
  1091. sblock_bad->no_io_error_seen)
  1092. goto corrected_error;
  1093. else
  1094. goto did_not_correct_error;
  1095. } else {
  1096. corrected_error:
  1097. spin_lock(&sctx->stat_lock);
  1098. sctx->stat.corrected_errors++;
  1099. sblock_to_check->data_corrected = 1;
  1100. spin_unlock(&sctx->stat_lock);
  1101. printk_ratelimited_in_rcu(KERN_ERR
  1102. "BTRFS: fixed up error at logical %llu on dev %s\n",
  1103. logical, rcu_str_deref(dev->name));
  1104. }
  1105. } else {
  1106. did_not_correct_error:
  1107. spin_lock(&sctx->stat_lock);
  1108. sctx->stat.uncorrectable_errors++;
  1109. spin_unlock(&sctx->stat_lock);
  1110. printk_ratelimited_in_rcu(KERN_ERR
  1111. "BTRFS: unable to fixup (regular) error at logical %llu on dev %s\n",
  1112. logical, rcu_str_deref(dev->name));
  1113. }
  1114. out:
  1115. if (sblocks_for_recheck) {
  1116. for (mirror_index = 0; mirror_index < BTRFS_MAX_MIRRORS;
  1117. mirror_index++) {
  1118. struct scrub_block *sblock = sblocks_for_recheck +
  1119. mirror_index;
  1120. struct scrub_recover *recover;
  1121. int page_index;
  1122. for (page_index = 0; page_index < sblock->page_count;
  1123. page_index++) {
  1124. sblock->pagev[page_index]->sblock = NULL;
  1125. recover = sblock->pagev[page_index]->recover;
  1126. if (recover) {
  1127. scrub_put_recover(recover);
  1128. sblock->pagev[page_index]->recover =
  1129. NULL;
  1130. }
  1131. scrub_page_put(sblock->pagev[page_index]);
  1132. }
  1133. }
  1134. kfree(sblocks_for_recheck);
  1135. }
  1136. return 0;
  1137. }
  1138. static inline int scrub_nr_raid_mirrors(struct btrfs_bio *bbio)
  1139. {
  1140. if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID5)
  1141. return 2;
  1142. else if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID6)
  1143. return 3;
  1144. else
  1145. return (int)bbio->num_stripes;
  1146. }
  1147. static inline void scrub_stripe_index_and_offset(u64 logical, u64 map_type,
  1148. u64 *raid_map,
  1149. u64 mapped_length,
  1150. int nstripes, int mirror,
  1151. int *stripe_index,
  1152. u64 *stripe_offset)
  1153. {
  1154. int i;
  1155. if (map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
  1156. /* RAID5/6 */
  1157. for (i = 0; i < nstripes; i++) {
  1158. if (raid_map[i] == RAID6_Q_STRIPE ||
  1159. raid_map[i] == RAID5_P_STRIPE)
  1160. continue;
  1161. if (logical >= raid_map[i] &&
  1162. logical < raid_map[i] + mapped_length)
  1163. break;
  1164. }
  1165. *stripe_index = i;
  1166. *stripe_offset = logical - raid_map[i];
  1167. } else {
  1168. /* The other RAID type */
  1169. *stripe_index = mirror;
  1170. *stripe_offset = 0;
  1171. }
  1172. }
  1173. static int scrub_setup_recheck_block(struct scrub_block *original_sblock,
  1174. struct scrub_block *sblocks_for_recheck)
  1175. {
  1176. struct scrub_ctx *sctx = original_sblock->sctx;
  1177. struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
  1178. u64 length = original_sblock->page_count * PAGE_SIZE;
  1179. u64 logical = original_sblock->pagev[0]->logical;
  1180. struct scrub_recover *recover;
  1181. struct btrfs_bio *bbio;
  1182. u64 sublen;
  1183. u64 mapped_length;
  1184. u64 stripe_offset;
  1185. int stripe_index;
  1186. int page_index = 0;
  1187. int mirror_index;
  1188. int nmirrors;
  1189. int ret;
  1190. /*
  1191. * note: the two members refs and outstanding_pages
  1192. * are not used (and not set) in the blocks that are used for
  1193. * the recheck procedure
  1194. */
  1195. while (length > 0) {
  1196. sublen = min_t(u64, length, PAGE_SIZE);
  1197. mapped_length = sublen;
  1198. bbio = NULL;
  1199. /*
  1200. * with a length of PAGE_SIZE, each returned stripe
  1201. * represents one mirror
  1202. */
  1203. ret = btrfs_map_sblock(fs_info, REQ_GET_READ_MIRRORS, logical,
  1204. &mapped_length, &bbio, 0, 1);
  1205. if (ret || !bbio || mapped_length < sublen) {
  1206. btrfs_put_bbio(bbio);
  1207. return -EIO;
  1208. }
  1209. recover = kzalloc(sizeof(struct scrub_recover), GFP_NOFS);
  1210. if (!recover) {
  1211. btrfs_put_bbio(bbio);
  1212. return -ENOMEM;
  1213. }
  1214. atomic_set(&recover->refs, 1);
  1215. recover->bbio = bbio;
  1216. recover->map_length = mapped_length;
  1217. BUG_ON(page_index >= SCRUB_PAGES_PER_RD_BIO);
  1218. nmirrors = min(scrub_nr_raid_mirrors(bbio), BTRFS_MAX_MIRRORS);
  1219. for (mirror_index = 0; mirror_index < nmirrors;
  1220. mirror_index++) {
  1221. struct scrub_block *sblock;
  1222. struct scrub_page *page;
  1223. sblock = sblocks_for_recheck + mirror_index;
  1224. sblock->sctx = sctx;
  1225. page = kzalloc(sizeof(*page), GFP_NOFS);
  1226. if (!page) {
  1227. leave_nomem:
  1228. spin_lock(&sctx->stat_lock);
  1229. sctx->stat.malloc_errors++;
  1230. spin_unlock(&sctx->stat_lock);
  1231. scrub_put_recover(recover);
  1232. return -ENOMEM;
  1233. }
  1234. scrub_page_get(page);
  1235. sblock->pagev[page_index] = page;
  1236. page->logical = logical;
  1237. scrub_stripe_index_and_offset(logical,
  1238. bbio->map_type,
  1239. bbio->raid_map,
  1240. mapped_length,
  1241. bbio->num_stripes -
  1242. bbio->num_tgtdevs,
  1243. mirror_index,
  1244. &stripe_index,
  1245. &stripe_offset);
  1246. page->physical = bbio->stripes[stripe_index].physical +
  1247. stripe_offset;
  1248. page->dev = bbio->stripes[stripe_index].dev;
  1249. BUG_ON(page_index >= original_sblock->page_count);
  1250. page->physical_for_dev_replace =
  1251. original_sblock->pagev[page_index]->
  1252. physical_for_dev_replace;
  1253. /* for missing devices, dev->bdev is NULL */
  1254. page->mirror_num = mirror_index + 1;
  1255. sblock->page_count++;
  1256. page->page = alloc_page(GFP_NOFS);
  1257. if (!page->page)
  1258. goto leave_nomem;
  1259. scrub_get_recover(recover);
  1260. page->recover = recover;
  1261. }
  1262. scrub_put_recover(recover);
  1263. length -= sublen;
  1264. logical += sublen;
  1265. page_index++;
  1266. }
  1267. return 0;
  1268. }
  1269. struct scrub_bio_ret {
  1270. struct completion event;
  1271. int error;
  1272. };
  1273. static void scrub_bio_wait_endio(struct bio *bio, int error)
  1274. {
  1275. struct scrub_bio_ret *ret = bio->bi_private;
  1276. ret->error = error;
  1277. complete(&ret->event);
  1278. }
  1279. static inline int scrub_is_page_on_raid56(struct scrub_page *page)
  1280. {
  1281. return page->recover &&
  1282. (page->recover->bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK);
  1283. }
  1284. static int scrub_submit_raid56_bio_wait(struct btrfs_fs_info *fs_info,
  1285. struct bio *bio,
  1286. struct scrub_page *page)
  1287. {
  1288. struct scrub_bio_ret done;
  1289. int ret;
  1290. init_completion(&done.event);
  1291. done.error = 0;
  1292. bio->bi_iter.bi_sector = page->logical >> 9;
  1293. bio->bi_private = &done;
  1294. bio->bi_end_io = scrub_bio_wait_endio;
  1295. ret = raid56_parity_recover(fs_info->fs_root, bio, page->recover->bbio,
  1296. page->recover->map_length,
  1297. page->mirror_num, 0);
  1298. if (ret)
  1299. return ret;
  1300. wait_for_completion(&done.event);
  1301. if (done.error)
  1302. return -EIO;
  1303. return 0;
  1304. }
  1305. /*
  1306. * this function will check the on disk data for checksum errors, header
  1307. * errors and read I/O errors. If any I/O errors happen, the exact pages
  1308. * which are errored are marked as being bad. The goal is to enable scrub
  1309. * to take those pages that are not errored from all the mirrors so that
  1310. * the pages that are errored in the just handled mirror can be repaired.
  1311. */
  1312. static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
  1313. struct scrub_block *sblock, int is_metadata,
  1314. int have_csum, u8 *csum, u64 generation,
  1315. u16 csum_size, int retry_failed_mirror)
  1316. {
  1317. int page_num;
  1318. sblock->no_io_error_seen = 1;
  1319. sblock->header_error = 0;
  1320. sblock->checksum_error = 0;
  1321. for (page_num = 0; page_num < sblock->page_count; page_num++) {
  1322. struct bio *bio;
  1323. struct scrub_page *page = sblock->pagev[page_num];
  1324. if (page->dev->bdev == NULL) {
  1325. page->io_error = 1;
  1326. sblock->no_io_error_seen = 0;
  1327. continue;
  1328. }
  1329. WARN_ON(!page->page);
  1330. bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
  1331. if (!bio) {
  1332. page->io_error = 1;
  1333. sblock->no_io_error_seen = 0;
  1334. continue;
  1335. }
  1336. bio->bi_bdev = page->dev->bdev;
  1337. bio_add_page(bio, page->page, PAGE_SIZE, 0);
  1338. if (!retry_failed_mirror && scrub_is_page_on_raid56(page)) {
  1339. if (scrub_submit_raid56_bio_wait(fs_info, bio, page))
  1340. sblock->no_io_error_seen = 0;
  1341. } else {
  1342. bio->bi_iter.bi_sector = page->physical >> 9;
  1343. if (btrfsic_submit_bio_wait(READ, bio))
  1344. sblock->no_io_error_seen = 0;
  1345. }
  1346. bio_put(bio);
  1347. }
  1348. if (sblock->no_io_error_seen)
  1349. scrub_recheck_block_checksum(fs_info, sblock, is_metadata,
  1350. have_csum, csum, generation,
  1351. csum_size);
  1352. return;
  1353. }
  1354. static inline int scrub_check_fsid(u8 fsid[],
  1355. struct scrub_page *spage)
  1356. {
  1357. struct btrfs_fs_devices *fs_devices = spage->dev->fs_devices;
  1358. int ret;
  1359. ret = memcmp(fsid, fs_devices->fsid, BTRFS_UUID_SIZE);
  1360. return !ret;
  1361. }
  1362. static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
  1363. struct scrub_block *sblock,
  1364. int is_metadata, int have_csum,
  1365. const u8 *csum, u64 generation,
  1366. u16 csum_size)
  1367. {
  1368. int page_num;
  1369. u8 calculated_csum[BTRFS_CSUM_SIZE];
  1370. u32 crc = ~(u32)0;
  1371. void *mapped_buffer;
  1372. WARN_ON(!sblock->pagev[0]->page);
  1373. if (is_metadata) {
  1374. struct btrfs_header *h;
  1375. mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
  1376. h = (struct btrfs_header *)mapped_buffer;
  1377. if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h) ||
  1378. !scrub_check_fsid(h->fsid, sblock->pagev[0]) ||
  1379. memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
  1380. BTRFS_UUID_SIZE)) {
  1381. sblock->header_error = 1;
  1382. } else if (generation != btrfs_stack_header_generation(h)) {
  1383. sblock->header_error = 1;
  1384. sblock->generation_error = 1;
  1385. }
  1386. csum = h->csum;
  1387. } else {
  1388. if (!have_csum)
  1389. return;
  1390. mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
  1391. }
  1392. for (page_num = 0;;) {
  1393. if (page_num == 0 && is_metadata)
  1394. crc = btrfs_csum_data(
  1395. ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE,
  1396. crc, PAGE_SIZE - BTRFS_CSUM_SIZE);
  1397. else
  1398. crc = btrfs_csum_data(mapped_buffer, crc, PAGE_SIZE);
  1399. kunmap_atomic(mapped_buffer);
  1400. page_num++;
  1401. if (page_num >= sblock->page_count)
  1402. break;
  1403. WARN_ON(!sblock->pagev[page_num]->page);
  1404. mapped_buffer = kmap_atomic(sblock->pagev[page_num]->page);
  1405. }
  1406. btrfs_csum_final(crc, calculated_csum);
  1407. if (memcmp(calculated_csum, csum, csum_size))
  1408. sblock->checksum_error = 1;
  1409. }
  1410. static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
  1411. struct scrub_block *sblock_good)
  1412. {
  1413. int page_num;
  1414. int ret = 0;
  1415. for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
  1416. int ret_sub;
  1417. ret_sub = scrub_repair_page_from_good_copy(sblock_bad,
  1418. sblock_good,
  1419. page_num, 1);
  1420. if (ret_sub)
  1421. ret = ret_sub;
  1422. }
  1423. return ret;
  1424. }
  1425. static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
  1426. struct scrub_block *sblock_good,
  1427. int page_num, int force_write)
  1428. {
  1429. struct scrub_page *page_bad = sblock_bad->pagev[page_num];
  1430. struct scrub_page *page_good = sblock_good->pagev[page_num];
  1431. BUG_ON(page_bad->page == NULL);
  1432. BUG_ON(page_good->page == NULL);
  1433. if (force_write || sblock_bad->header_error ||
  1434. sblock_bad->checksum_error || page_bad->io_error) {
  1435. struct bio *bio;
  1436. int ret;
  1437. if (!page_bad->dev->bdev) {
  1438. printk_ratelimited(KERN_WARNING "BTRFS: "
  1439. "scrub_repair_page_from_good_copy(bdev == NULL) "
  1440. "is unexpected!\n");
  1441. return -EIO;
  1442. }
  1443. bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
  1444. if (!bio)
  1445. return -EIO;
  1446. bio->bi_bdev = page_bad->dev->bdev;
  1447. bio->bi_iter.bi_sector = page_bad->physical >> 9;
  1448. ret = bio_add_page(bio, page_good->page, PAGE_SIZE, 0);
  1449. if (PAGE_SIZE != ret) {
  1450. bio_put(bio);
  1451. return -EIO;
  1452. }
  1453. if (btrfsic_submit_bio_wait(WRITE, bio)) {
  1454. btrfs_dev_stat_inc_and_print(page_bad->dev,
  1455. BTRFS_DEV_STAT_WRITE_ERRS);
  1456. btrfs_dev_replace_stats_inc(
  1457. &sblock_bad->sctx->dev_root->fs_info->
  1458. dev_replace.num_write_errors);
  1459. bio_put(bio);
  1460. return -EIO;
  1461. }
  1462. bio_put(bio);
  1463. }
  1464. return 0;
  1465. }
  1466. static void scrub_write_block_to_dev_replace(struct scrub_block *sblock)
  1467. {
  1468. int page_num;
  1469. /*
  1470. * This block is used for the check of the parity on the source device,
  1471. * so the data needn't be written into the destination device.
  1472. */
  1473. if (sblock->sparity)
  1474. return;
  1475. for (page_num = 0; page_num < sblock->page_count; page_num++) {
  1476. int ret;
  1477. ret = scrub_write_page_to_dev_replace(sblock, page_num);
  1478. if (ret)
  1479. btrfs_dev_replace_stats_inc(
  1480. &sblock->sctx->dev_root->fs_info->dev_replace.
  1481. num_write_errors);
  1482. }
  1483. }
  1484. static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
  1485. int page_num)
  1486. {
  1487. struct scrub_page *spage = sblock->pagev[page_num];
  1488. BUG_ON(spage->page == NULL);
  1489. if (spage->io_error) {
  1490. void *mapped_buffer = kmap_atomic(spage->page);
  1491. memset(mapped_buffer, 0, PAGE_CACHE_SIZE);
  1492. flush_dcache_page(spage->page);
  1493. kunmap_atomic(mapped_buffer);
  1494. }
  1495. return scrub_add_page_to_wr_bio(sblock->sctx, spage);
  1496. }
  1497. static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
  1498. struct scrub_page *spage)
  1499. {
  1500. struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx;
  1501. struct scrub_bio *sbio;
  1502. int ret;
  1503. mutex_lock(&wr_ctx->wr_lock);
  1504. again:
  1505. if (!wr_ctx->wr_curr_bio) {
  1506. wr_ctx->wr_curr_bio = kzalloc(sizeof(*wr_ctx->wr_curr_bio),
  1507. GFP_NOFS);
  1508. if (!wr_ctx->wr_curr_bio) {
  1509. mutex_unlock(&wr_ctx->wr_lock);
  1510. return -ENOMEM;
  1511. }
  1512. wr_ctx->wr_curr_bio->sctx = sctx;
  1513. wr_ctx->wr_curr_bio->page_count = 0;
  1514. }
  1515. sbio = wr_ctx->wr_curr_bio;
  1516. if (sbio->page_count == 0) {
  1517. struct bio *bio;
  1518. sbio->physical = spage->physical_for_dev_replace;
  1519. sbio->logical = spage->logical;
  1520. sbio->dev = wr_ctx->tgtdev;
  1521. bio = sbio->bio;
  1522. if (!bio) {
  1523. bio = btrfs_io_bio_alloc(GFP_NOFS, wr_ctx->pages_per_wr_bio);
  1524. if (!bio) {
  1525. mutex_unlock(&wr_ctx->wr_lock);
  1526. return -ENOMEM;
  1527. }
  1528. sbio->bio = bio;
  1529. }
  1530. bio->bi_private = sbio;
  1531. bio->bi_end_io = scrub_wr_bio_end_io;
  1532. bio->bi_bdev = sbio->dev->bdev;
  1533. bio->bi_iter.bi_sector = sbio->physical >> 9;
  1534. sbio->err = 0;
  1535. } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
  1536. spage->physical_for_dev_replace ||
  1537. sbio->logical + sbio->page_count * PAGE_SIZE !=
  1538. spage->logical) {
  1539. scrub_wr_submit(sctx);
  1540. goto again;
  1541. }
  1542. ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
  1543. if (ret != PAGE_SIZE) {
  1544. if (sbio->page_count < 1) {
  1545. bio_put(sbio->bio);
  1546. sbio->bio = NULL;
  1547. mutex_unlock(&wr_ctx->wr_lock);
  1548. return -EIO;
  1549. }
  1550. scrub_wr_submit(sctx);
  1551. goto again;
  1552. }
  1553. sbio->pagev[sbio->page_count] = spage;
  1554. scrub_page_get(spage);
  1555. sbio->page_count++;
  1556. if (sbio->page_count == wr_ctx->pages_per_wr_bio)
  1557. scrub_wr_submit(sctx);
  1558. mutex_unlock(&wr_ctx->wr_lock);
  1559. return 0;
  1560. }
  1561. static void scrub_wr_submit(struct scrub_ctx *sctx)
  1562. {
  1563. struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx;
  1564. struct scrub_bio *sbio;
  1565. if (!wr_ctx->wr_curr_bio)
  1566. return;
  1567. sbio = wr_ctx->wr_curr_bio;
  1568. wr_ctx->wr_curr_bio = NULL;
  1569. WARN_ON(!sbio->bio->bi_bdev);
  1570. scrub_pending_bio_inc(sctx);
  1571. /* process all writes in a single worker thread. Then the block layer
  1572. * orders the requests before sending them to the driver which
  1573. * doubled the write performance on spinning disks when measured
  1574. * with Linux 3.5 */
  1575. btrfsic_submit_bio(WRITE, sbio->bio);
  1576. }
  1577. static void scrub_wr_bio_end_io(struct bio *bio, int err)
  1578. {
  1579. struct scrub_bio *sbio = bio->bi_private;
  1580. struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
  1581. sbio->err = err;
  1582. sbio->bio = bio;
  1583. btrfs_init_work(&sbio->work, btrfs_scrubwrc_helper,
  1584. scrub_wr_bio_end_io_worker, NULL, NULL);
  1585. btrfs_queue_work(fs_info->scrub_wr_completion_workers, &sbio->work);
  1586. }
  1587. static void scrub_wr_bio_end_io_worker(struct btrfs_work *work)
  1588. {
  1589. struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
  1590. struct scrub_ctx *sctx = sbio->sctx;
  1591. int i;
  1592. WARN_ON(sbio->page_count > SCRUB_PAGES_PER_WR_BIO);
  1593. if (sbio->err) {
  1594. struct btrfs_dev_replace *dev_replace =
  1595. &sbio->sctx->dev_root->fs_info->dev_replace;
  1596. for (i = 0; i < sbio->page_count; i++) {
  1597. struct scrub_page *spage = sbio->pagev[i];
  1598. spage->io_error = 1;
  1599. btrfs_dev_replace_stats_inc(&dev_replace->
  1600. num_write_errors);
  1601. }
  1602. }
  1603. for (i = 0; i < sbio->page_count; i++)
  1604. scrub_page_put(sbio->pagev[i]);
  1605. bio_put(sbio->bio);
  1606. kfree(sbio);
  1607. scrub_pending_bio_dec(sctx);
  1608. }
  1609. static int scrub_checksum(struct scrub_block *sblock)
  1610. {
  1611. u64 flags;
  1612. int ret;
  1613. WARN_ON(sblock->page_count < 1);
  1614. flags = sblock->pagev[0]->flags;
  1615. ret = 0;
  1616. if (flags & BTRFS_EXTENT_FLAG_DATA)
  1617. ret = scrub_checksum_data(sblock);
  1618. else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
  1619. ret = scrub_checksum_tree_block(sblock);
  1620. else if (flags & BTRFS_EXTENT_FLAG_SUPER)
  1621. (void)scrub_checksum_super(sblock);
  1622. else
  1623. WARN_ON(1);
  1624. if (ret)
  1625. scrub_handle_errored_block(sblock);
  1626. return ret;
  1627. }
  1628. static int scrub_checksum_data(struct scrub_block *sblock)
  1629. {
  1630. struct scrub_ctx *sctx = sblock->sctx;
  1631. u8 csum[BTRFS_CSUM_SIZE];
  1632. u8 *on_disk_csum;
  1633. struct page *page;
  1634. void *buffer;
  1635. u32 crc = ~(u32)0;
  1636. int fail = 0;
  1637. u64 len;
  1638. int index;
  1639. BUG_ON(sblock->page_count < 1);
  1640. if (!sblock->pagev[0]->have_csum)
  1641. return 0;
  1642. on_disk_csum = sblock->pagev[0]->csum;
  1643. page = sblock->pagev[0]->page;
  1644. buffer = kmap_atomic(page);
  1645. len = sctx->sectorsize;
  1646. index = 0;
  1647. for (;;) {
  1648. u64 l = min_t(u64, len, PAGE_SIZE);
  1649. crc = btrfs_csum_data(buffer, crc, l);
  1650. kunmap_atomic(buffer);
  1651. len -= l;
  1652. if (len == 0)
  1653. break;
  1654. index++;
  1655. BUG_ON(index >= sblock->page_count);
  1656. BUG_ON(!sblock->pagev[index]->page);
  1657. page = sblock->pagev[index]->page;
  1658. buffer = kmap_atomic(page);
  1659. }
  1660. btrfs_csum_final(crc, csum);
  1661. if (memcmp(csum, on_disk_csum, sctx->csum_size))
  1662. fail = 1;
  1663. return fail;
  1664. }
  1665. static int scrub_checksum_tree_block(struct scrub_block *sblock)
  1666. {
  1667. struct scrub_ctx *sctx = sblock->sctx;
  1668. struct btrfs_header *h;
  1669. struct btrfs_root *root = sctx->dev_root;
  1670. struct btrfs_fs_info *fs_info = root->fs_info;
  1671. u8 calculated_csum[BTRFS_CSUM_SIZE];
  1672. u8 on_disk_csum[BTRFS_CSUM_SIZE];
  1673. struct page *page;
  1674. void *mapped_buffer;
  1675. u64 mapped_size;
  1676. void *p;
  1677. u32 crc = ~(u32)0;
  1678. int fail = 0;
  1679. int crc_fail = 0;
  1680. u64 len;
  1681. int index;
  1682. BUG_ON(sblock->page_count < 1);
  1683. page = sblock->pagev[0]->page;
  1684. mapped_buffer = kmap_atomic(page);
  1685. h = (struct btrfs_header *)mapped_buffer;
  1686. memcpy(on_disk_csum, h->csum, sctx->csum_size);
  1687. /*
  1688. * we don't use the getter functions here, as we
  1689. * a) don't have an extent buffer and
  1690. * b) the page is already kmapped
  1691. */
  1692. if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h))
  1693. ++fail;
  1694. if (sblock->pagev[0]->generation != btrfs_stack_header_generation(h))
  1695. ++fail;
  1696. if (!scrub_check_fsid(h->fsid, sblock->pagev[0]))
  1697. ++fail;
  1698. if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
  1699. BTRFS_UUID_SIZE))
  1700. ++fail;
  1701. len = sctx->nodesize - BTRFS_CSUM_SIZE;
  1702. mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
  1703. p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
  1704. index = 0;
  1705. for (;;) {
  1706. u64 l = min_t(u64, len, mapped_size);
  1707. crc = btrfs_csum_data(p, crc, l);
  1708. kunmap_atomic(mapped_buffer);
  1709. len -= l;
  1710. if (len == 0)
  1711. break;
  1712. index++;
  1713. BUG_ON(index >= sblock->page_count);
  1714. BUG_ON(!sblock->pagev[index]->page);
  1715. page = sblock->pagev[index]->page;
  1716. mapped_buffer = kmap_atomic(page);
  1717. mapped_size = PAGE_SIZE;
  1718. p = mapped_buffer;
  1719. }
  1720. btrfs_csum_final(crc, calculated_csum);
  1721. if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
  1722. ++crc_fail;
  1723. return fail || crc_fail;
  1724. }
  1725. static int scrub_checksum_super(struct scrub_block *sblock)
  1726. {
  1727. struct btrfs_super_block *s;
  1728. struct scrub_ctx *sctx = sblock->sctx;
  1729. u8 calculated_csum[BTRFS_CSUM_SIZE];
  1730. u8 on_disk_csum[BTRFS_CSUM_SIZE];
  1731. struct page *page;
  1732. void *mapped_buffer;
  1733. u64 mapped_size;
  1734. void *p;
  1735. u32 crc = ~(u32)0;
  1736. int fail_gen = 0;
  1737. int fail_cor = 0;
  1738. u64 len;
  1739. int index;
  1740. BUG_ON(sblock->page_count < 1);
  1741. page = sblock->pagev[0]->page;
  1742. mapped_buffer = kmap_atomic(page);
  1743. s = (struct btrfs_super_block *)mapped_buffer;
  1744. memcpy(on_disk_csum, s->csum, sctx->csum_size);
  1745. if (sblock->pagev[0]->logical != btrfs_super_bytenr(s))
  1746. ++fail_cor;
  1747. if (sblock->pagev[0]->generation != btrfs_super_generation(s))
  1748. ++fail_gen;
  1749. if (!scrub_check_fsid(s->fsid, sblock->pagev[0]))
  1750. ++fail_cor;
  1751. len = BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE;
  1752. mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
  1753. p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
  1754. index = 0;
  1755. for (;;) {
  1756. u64 l = min_t(u64, len, mapped_size);
  1757. crc = btrfs_csum_data(p, crc, l);
  1758. kunmap_atomic(mapped_buffer);
  1759. len -= l;
  1760. if (len == 0)
  1761. break;
  1762. index++;
  1763. BUG_ON(index >= sblock->page_count);
  1764. BUG_ON(!sblock->pagev[index]->page);
  1765. page = sblock->pagev[index]->page;
  1766. mapped_buffer = kmap_atomic(page);
  1767. mapped_size = PAGE_SIZE;
  1768. p = mapped_buffer;
  1769. }
  1770. btrfs_csum_final(crc, calculated_csum);
  1771. if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
  1772. ++fail_cor;
  1773. if (fail_cor + fail_gen) {
  1774. /*
  1775. * if we find an error in a super block, we just report it.
  1776. * They will get written with the next transaction commit
  1777. * anyway
  1778. */
  1779. spin_lock(&sctx->stat_lock);
  1780. ++sctx->stat.super_errors;
  1781. spin_unlock(&sctx->stat_lock);
  1782. if (fail_cor)
  1783. btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
  1784. BTRFS_DEV_STAT_CORRUPTION_ERRS);
  1785. else
  1786. btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
  1787. BTRFS_DEV_STAT_GENERATION_ERRS);
  1788. }
  1789. return fail_cor + fail_gen;
  1790. }
  1791. static void scrub_block_get(struct scrub_block *sblock)
  1792. {
  1793. atomic_inc(&sblock->refs);
  1794. }
  1795. static void scrub_block_put(struct scrub_block *sblock)
  1796. {
  1797. if (atomic_dec_and_test(&sblock->refs)) {
  1798. int i;
  1799. if (sblock->sparity)
  1800. scrub_parity_put(sblock->sparity);
  1801. for (i = 0; i < sblock->page_count; i++)
  1802. scrub_page_put(sblock->pagev[i]);
  1803. kfree(sblock);
  1804. }
  1805. }
  1806. static void scrub_page_get(struct scrub_page *spage)
  1807. {
  1808. atomic_inc(&spage->refs);
  1809. }
  1810. static void scrub_page_put(struct scrub_page *spage)
  1811. {
  1812. if (atomic_dec_and_test(&spage->refs)) {
  1813. if (spage->page)
  1814. __free_page(spage->page);
  1815. kfree(spage);
  1816. }
  1817. }
  1818. static void scrub_submit(struct scrub_ctx *sctx)
  1819. {
  1820. struct scrub_bio *sbio;
  1821. if (sctx->curr == -1)
  1822. return;
  1823. sbio = sctx->bios[sctx->curr];
  1824. sctx->curr = -1;
  1825. scrub_pending_bio_inc(sctx);
  1826. if (!sbio->bio->bi_bdev) {
  1827. /*
  1828. * this case should not happen. If btrfs_map_block() is
  1829. * wrong, it could happen for dev-replace operations on
  1830. * missing devices when no mirrors are available, but in
  1831. * this case it should already fail the mount.
  1832. * This case is handled correctly (but _very_ slowly).
  1833. */
  1834. printk_ratelimited(KERN_WARNING
  1835. "BTRFS: scrub_submit(bio bdev == NULL) is unexpected!\n");
  1836. bio_endio(sbio->bio, -EIO);
  1837. } else {
  1838. btrfsic_submit_bio(READ, sbio->bio);
  1839. }
  1840. }
  1841. static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
  1842. struct scrub_page *spage)
  1843. {
  1844. struct scrub_block *sblock = spage->sblock;
  1845. struct scrub_bio *sbio;
  1846. int ret;
  1847. again:
  1848. /*
  1849. * grab a fresh bio or wait for one to become available
  1850. */
  1851. while (sctx->curr == -1) {
  1852. spin_lock(&sctx->list_lock);
  1853. sctx->curr = sctx->first_free;
  1854. if (sctx->curr != -1) {
  1855. sctx->first_free = sctx->bios[sctx->curr]->next_free;
  1856. sctx->bios[sctx->curr]->next_free = -1;
  1857. sctx->bios[sctx->curr]->page_count = 0;
  1858. spin_unlock(&sctx->list_lock);
  1859. } else {
  1860. spin_unlock(&sctx->list_lock);
  1861. wait_event(sctx->list_wait, sctx->first_free != -1);
  1862. }
  1863. }
  1864. sbio = sctx->bios[sctx->curr];
  1865. if (sbio->page_count == 0) {
  1866. struct bio *bio;
  1867. sbio->physical = spage->physical;
  1868. sbio->logical = spage->logical;
  1869. sbio->dev = spage->dev;
  1870. bio = sbio->bio;
  1871. if (!bio) {
  1872. bio = btrfs_io_bio_alloc(GFP_NOFS, sctx->pages_per_rd_bio);
  1873. if (!bio)
  1874. return -ENOMEM;
  1875. sbio->bio = bio;
  1876. }
  1877. bio->bi_private = sbio;
  1878. bio->bi_end_io = scrub_bio_end_io;
  1879. bio->bi_bdev = sbio->dev->bdev;
  1880. bio->bi_iter.bi_sector = sbio->physical >> 9;
  1881. sbio->err = 0;
  1882. } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
  1883. spage->physical ||
  1884. sbio->logical + sbio->page_count * PAGE_SIZE !=
  1885. spage->logical ||
  1886. sbio->dev != spage->dev) {
  1887. scrub_submit(sctx);
  1888. goto again;
  1889. }
  1890. sbio->pagev[sbio->page_count] = spage;
  1891. ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
  1892. if (ret != PAGE_SIZE) {
  1893. if (sbio->page_count < 1) {
  1894. bio_put(sbio->bio);
  1895. sbio->bio = NULL;
  1896. return -EIO;
  1897. }
  1898. scrub_submit(sctx);
  1899. goto again;
  1900. }
  1901. scrub_block_get(sblock); /* one for the page added to the bio */
  1902. atomic_inc(&sblock->outstanding_pages);
  1903. sbio->page_count++;
  1904. if (sbio->page_count == sctx->pages_per_rd_bio)
  1905. scrub_submit(sctx);
  1906. return 0;
  1907. }
  1908. static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
  1909. u64 physical, struct btrfs_device *dev, u64 flags,
  1910. u64 gen, int mirror_num, u8 *csum, int force,
  1911. u64 physical_for_dev_replace)
  1912. {
  1913. struct scrub_block *sblock;
  1914. int index;
  1915. sblock = kzalloc(sizeof(*sblock), GFP_NOFS);
  1916. if (!sblock) {
  1917. spin_lock(&sctx->stat_lock);
  1918. sctx->stat.malloc_errors++;
  1919. spin_unlock(&sctx->stat_lock);
  1920. return -ENOMEM;
  1921. }
  1922. /* one ref inside this function, plus one for each page added to
  1923. * a bio later on */
  1924. atomic_set(&sblock->refs, 1);
  1925. sblock->sctx = sctx;
  1926. sblock->no_io_error_seen = 1;
  1927. for (index = 0; len > 0; index++) {
  1928. struct scrub_page *spage;
  1929. u64 l = min_t(u64, len, PAGE_SIZE);
  1930. spage = kzalloc(sizeof(*spage), GFP_NOFS);
  1931. if (!spage) {
  1932. leave_nomem:
  1933. spin_lock(&sctx->stat_lock);
  1934. sctx->stat.malloc_errors++;
  1935. spin_unlock(&sctx->stat_lock);
  1936. scrub_block_put(sblock);
  1937. return -ENOMEM;
  1938. }
  1939. BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
  1940. scrub_page_get(spage);
  1941. sblock->pagev[index] = spage;
  1942. spage->sblock = sblock;
  1943. spage->dev = dev;
  1944. spage->flags = flags;
  1945. spage->generation = gen;
  1946. spage->logical = logical;
  1947. spage->physical = physical;
  1948. spage->physical_for_dev_replace = physical_for_dev_replace;
  1949. spage->mirror_num = mirror_num;
  1950. if (csum) {
  1951. spage->have_csum = 1;
  1952. memcpy(spage->csum, csum, sctx->csum_size);
  1953. } else {
  1954. spage->have_csum = 0;
  1955. }
  1956. sblock->page_count++;
  1957. spage->page = alloc_page(GFP_NOFS);
  1958. if (!spage->page)
  1959. goto leave_nomem;
  1960. len -= l;
  1961. logical += l;
  1962. physical += l;
  1963. physical_for_dev_replace += l;
  1964. }
  1965. WARN_ON(sblock->page_count == 0);
  1966. for (index = 0; index < sblock->page_count; index++) {
  1967. struct scrub_page *spage = sblock->pagev[index];
  1968. int ret;
  1969. ret = scrub_add_page_to_rd_bio(sctx, spage);
  1970. if (ret) {
  1971. scrub_block_put(sblock);
  1972. return ret;
  1973. }
  1974. }
  1975. if (force)
  1976. scrub_submit(sctx);
  1977. /* last one frees, either here or in bio completion for last page */
  1978. scrub_block_put(sblock);
  1979. return 0;
  1980. }
  1981. static void scrub_bio_end_io(struct bio *bio, int err)
  1982. {
  1983. struct scrub_bio *sbio = bio->bi_private;
  1984. struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
  1985. sbio->err = err;
  1986. sbio->bio = bio;
  1987. btrfs_queue_work(fs_info->scrub_workers, &sbio->work);
  1988. }
  1989. static void scrub_bio_end_io_worker(struct btrfs_work *work)
  1990. {
  1991. struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
  1992. struct scrub_ctx *sctx = sbio->sctx;
  1993. int i;
  1994. BUG_ON(sbio->page_count > SCRUB_PAGES_PER_RD_BIO);
  1995. if (sbio->err) {
  1996. for (i = 0; i < sbio->page_count; i++) {
  1997. struct scrub_page *spage = sbio->pagev[i];
  1998. spage->io_error = 1;
  1999. spage->sblock->no_io_error_seen = 0;
  2000. }
  2001. }
  2002. /* now complete the scrub_block items that have all pages completed */
  2003. for (i = 0; i < sbio->page_count; i++) {
  2004. struct scrub_page *spage = sbio->pagev[i];
  2005. struct scrub_block *sblock = spage->sblock;
  2006. if (atomic_dec_and_test(&sblock->outstanding_pages))
  2007. scrub_block_complete(sblock);
  2008. scrub_block_put(sblock);
  2009. }
  2010. bio_put(sbio->bio);
  2011. sbio->bio = NULL;
  2012. spin_lock(&sctx->list_lock);
  2013. sbio->next_free = sctx->first_free;
  2014. sctx->first_free = sbio->index;
  2015. spin_unlock(&sctx->list_lock);
  2016. if (sctx->is_dev_replace &&
  2017. atomic_read(&sctx->wr_ctx.flush_all_writes)) {
  2018. mutex_lock(&sctx->wr_ctx.wr_lock);
  2019. scrub_wr_submit(sctx);
  2020. mutex_unlock(&sctx->wr_ctx.wr_lock);
  2021. }
  2022. scrub_pending_bio_dec(sctx);
  2023. }
  2024. static inline void __scrub_mark_bitmap(struct scrub_parity *sparity,
  2025. unsigned long *bitmap,
  2026. u64 start, u64 len)
  2027. {
  2028. int offset;
  2029. int nsectors;
  2030. int sectorsize = sparity->sctx->dev_root->sectorsize;
  2031. if (len >= sparity->stripe_len) {
  2032. bitmap_set(bitmap, 0, sparity->nsectors);
  2033. return;
  2034. }
  2035. start -= sparity->logic_start;
  2036. offset = (int)do_div(start, sparity->stripe_len);
  2037. offset /= sectorsize;
  2038. nsectors = (int)len / sectorsize;
  2039. if (offset + nsectors <= sparity->nsectors) {
  2040. bitmap_set(bitmap, offset, nsectors);
  2041. return;
  2042. }
  2043. bitmap_set(bitmap, offset, sparity->nsectors - offset);
  2044. bitmap_set(bitmap, 0, nsectors - (sparity->nsectors - offset));
  2045. }
  2046. static inline void scrub_parity_mark_sectors_error(struct scrub_parity *sparity,
  2047. u64 start, u64 len)
  2048. {
  2049. __scrub_mark_bitmap(sparity, sparity->ebitmap, start, len);
  2050. }
  2051. static inline void scrub_parity_mark_sectors_data(struct scrub_parity *sparity,
  2052. u64 start, u64 len)
  2053. {
  2054. __scrub_mark_bitmap(sparity, sparity->dbitmap, start, len);
  2055. }
  2056. static void scrub_block_complete(struct scrub_block *sblock)
  2057. {
  2058. int corrupted = 0;
  2059. if (!sblock->no_io_error_seen) {
  2060. corrupted = 1;
  2061. scrub_handle_errored_block(sblock);
  2062. } else {
  2063. /*
  2064. * if has checksum error, write via repair mechanism in
  2065. * dev replace case, otherwise write here in dev replace
  2066. * case.
  2067. */
  2068. corrupted = scrub_checksum(sblock);
  2069. if (!corrupted && sblock->sctx->is_dev_replace)
  2070. scrub_write_block_to_dev_replace(sblock);
  2071. }
  2072. if (sblock->sparity && corrupted && !sblock->data_corrected) {
  2073. u64 start = sblock->pagev[0]->logical;
  2074. u64 end = sblock->pagev[sblock->page_count - 1]->logical +
  2075. PAGE_SIZE;
  2076. scrub_parity_mark_sectors_error(sblock->sparity,
  2077. start, end - start);
  2078. }
  2079. }
  2080. static int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u64 len,
  2081. u8 *csum)
  2082. {
  2083. struct btrfs_ordered_sum *sum = NULL;
  2084. unsigned long index;
  2085. unsigned long num_sectors;
  2086. while (!list_empty(&sctx->csum_list)) {
  2087. sum = list_first_entry(&sctx->csum_list,
  2088. struct btrfs_ordered_sum, list);
  2089. if (sum->bytenr > logical)
  2090. return 0;
  2091. if (sum->bytenr + sum->len > logical)
  2092. break;
  2093. ++sctx->stat.csum_discards;
  2094. list_del(&sum->list);
  2095. kfree(sum);
  2096. sum = NULL;
  2097. }
  2098. if (!sum)
  2099. return 0;
  2100. index = ((u32)(logical - sum->bytenr)) / sctx->sectorsize;
  2101. num_sectors = sum->len / sctx->sectorsize;
  2102. memcpy(csum, sum->sums + index, sctx->csum_size);
  2103. if (index == num_sectors - 1) {
  2104. list_del(&sum->list);
  2105. kfree(sum);
  2106. }
  2107. return 1;
  2108. }
  2109. /* scrub extent tries to collect up to 64 kB for each bio */
  2110. static int scrub_extent(struct scrub_ctx *sctx, u64 logical, u64 len,
  2111. u64 physical, struct btrfs_device *dev, u64 flags,
  2112. u64 gen, int mirror_num, u64 physical_for_dev_replace)
  2113. {
  2114. int ret;
  2115. u8 csum[BTRFS_CSUM_SIZE];
  2116. u32 blocksize;
  2117. if (flags & BTRFS_EXTENT_FLAG_DATA) {
  2118. blocksize = sctx->sectorsize;
  2119. spin_lock(&sctx->stat_lock);
  2120. sctx->stat.data_extents_scrubbed++;
  2121. sctx->stat.data_bytes_scrubbed += len;
  2122. spin_unlock(&sctx->stat_lock);
  2123. } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  2124. blocksize = sctx->nodesize;
  2125. spin_lock(&sctx->stat_lock);
  2126. sctx->stat.tree_extents_scrubbed++;
  2127. sctx->stat.tree_bytes_scrubbed += len;
  2128. spin_unlock(&sctx->stat_lock);
  2129. } else {
  2130. blocksize = sctx->sectorsize;
  2131. WARN_ON(1);
  2132. }
  2133. while (len) {
  2134. u64 l = min_t(u64, len, blocksize);
  2135. int have_csum = 0;
  2136. if (flags & BTRFS_EXTENT_FLAG_DATA) {
  2137. /* push csums to sbio */
  2138. have_csum = scrub_find_csum(sctx, logical, l, csum);
  2139. if (have_csum == 0)
  2140. ++sctx->stat.no_csum;
  2141. if (sctx->is_dev_replace && !have_csum) {
  2142. ret = copy_nocow_pages(sctx, logical, l,
  2143. mirror_num,
  2144. physical_for_dev_replace);
  2145. goto behind_scrub_pages;
  2146. }
  2147. }
  2148. ret = scrub_pages(sctx, logical, l, physical, dev, flags, gen,
  2149. mirror_num, have_csum ? csum : NULL, 0,
  2150. physical_for_dev_replace);
  2151. behind_scrub_pages:
  2152. if (ret)
  2153. return ret;
  2154. len -= l;
  2155. logical += l;
  2156. physical += l;
  2157. physical_for_dev_replace += l;
  2158. }
  2159. return 0;
  2160. }
  2161. static int scrub_pages_for_parity(struct scrub_parity *sparity,
  2162. u64 logical, u64 len,
  2163. u64 physical, struct btrfs_device *dev,
  2164. u64 flags, u64 gen, int mirror_num, u8 *csum)
  2165. {
  2166. struct scrub_ctx *sctx = sparity->sctx;
  2167. struct scrub_block *sblock;
  2168. int index;
  2169. sblock = kzalloc(sizeof(*sblock), GFP_NOFS);
  2170. if (!sblock) {
  2171. spin_lock(&sctx->stat_lock);
  2172. sctx->stat.malloc_errors++;
  2173. spin_unlock(&sctx->stat_lock);
  2174. return -ENOMEM;
  2175. }
  2176. /* one ref inside this function, plus one for each page added to
  2177. * a bio later on */
  2178. atomic_set(&sblock->refs, 1);
  2179. sblock->sctx = sctx;
  2180. sblock->no_io_error_seen = 1;
  2181. sblock->sparity = sparity;
  2182. scrub_parity_get(sparity);
  2183. for (index = 0; len > 0; index++) {
  2184. struct scrub_page *spage;
  2185. u64 l = min_t(u64, len, PAGE_SIZE);
  2186. spage = kzalloc(sizeof(*spage), GFP_NOFS);
  2187. if (!spage) {
  2188. leave_nomem:
  2189. spin_lock(&sctx->stat_lock);
  2190. sctx->stat.malloc_errors++;
  2191. spin_unlock(&sctx->stat_lock);
  2192. scrub_block_put(sblock);
  2193. return -ENOMEM;
  2194. }
  2195. BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
  2196. /* For scrub block */
  2197. scrub_page_get(spage);
  2198. sblock->pagev[index] = spage;
  2199. /* For scrub parity */
  2200. scrub_page_get(spage);
  2201. list_add_tail(&spage->list, &sparity->spages);
  2202. spage->sblock = sblock;
  2203. spage->dev = dev;
  2204. spage->flags = flags;
  2205. spage->generation = gen;
  2206. spage->logical = logical;
  2207. spage->physical = physical;
  2208. spage->mirror_num = mirror_num;
  2209. if (csum) {
  2210. spage->have_csum = 1;
  2211. memcpy(spage->csum, csum, sctx->csum_size);
  2212. } else {
  2213. spage->have_csum = 0;
  2214. }
  2215. sblock->page_count++;
  2216. spage->page = alloc_page(GFP_NOFS);
  2217. if (!spage->page)
  2218. goto leave_nomem;
  2219. len -= l;
  2220. logical += l;
  2221. physical += l;
  2222. }
  2223. WARN_ON(sblock->page_count == 0);
  2224. for (index = 0; index < sblock->page_count; index++) {
  2225. struct scrub_page *spage = sblock->pagev[index];
  2226. int ret;
  2227. ret = scrub_add_page_to_rd_bio(sctx, spage);
  2228. if (ret) {
  2229. scrub_block_put(sblock);
  2230. return ret;
  2231. }
  2232. }
  2233. /* last one frees, either here or in bio completion for last page */
  2234. scrub_block_put(sblock);
  2235. return 0;
  2236. }
  2237. static int scrub_extent_for_parity(struct scrub_parity *sparity,
  2238. u64 logical, u64 len,
  2239. u64 physical, struct btrfs_device *dev,
  2240. u64 flags, u64 gen, int mirror_num)
  2241. {
  2242. struct scrub_ctx *sctx = sparity->sctx;
  2243. int ret;
  2244. u8 csum[BTRFS_CSUM_SIZE];
  2245. u32 blocksize;
  2246. if (flags & BTRFS_EXTENT_FLAG_DATA) {
  2247. blocksize = sctx->sectorsize;
  2248. } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  2249. blocksize = sctx->nodesize;
  2250. } else {
  2251. blocksize = sctx->sectorsize;
  2252. WARN_ON(1);
  2253. }
  2254. while (len) {
  2255. u64 l = min_t(u64, len, blocksize);
  2256. int have_csum = 0;
  2257. if (flags & BTRFS_EXTENT_FLAG_DATA) {
  2258. /* push csums to sbio */
  2259. have_csum = scrub_find_csum(sctx, logical, l, csum);
  2260. if (have_csum == 0)
  2261. goto skip;
  2262. }
  2263. ret = scrub_pages_for_parity(sparity, logical, l, physical, dev,
  2264. flags, gen, mirror_num,
  2265. have_csum ? csum : NULL);
  2266. if (ret)
  2267. return ret;
  2268. skip:
  2269. len -= l;
  2270. logical += l;
  2271. physical += l;
  2272. }
  2273. return 0;
  2274. }
  2275. /*
  2276. * Given a physical address, this will calculate it's
  2277. * logical offset. if this is a parity stripe, it will return
  2278. * the most left data stripe's logical offset.
  2279. *
  2280. * return 0 if it is a data stripe, 1 means parity stripe.
  2281. */
  2282. static int get_raid56_logic_offset(u64 physical, int num,
  2283. struct map_lookup *map, u64 *offset,
  2284. u64 *stripe_start)
  2285. {
  2286. int i;
  2287. int j = 0;
  2288. u64 stripe_nr;
  2289. u64 last_offset;
  2290. int stripe_index;
  2291. int rot;
  2292. last_offset = (physical - map->stripes[num].physical) *
  2293. nr_data_stripes(map);
  2294. if (stripe_start)
  2295. *stripe_start = last_offset;
  2296. *offset = last_offset;
  2297. for (i = 0; i < nr_data_stripes(map); i++) {
  2298. *offset = last_offset + i * map->stripe_len;
  2299. stripe_nr = *offset;
  2300. do_div(stripe_nr, map->stripe_len);
  2301. do_div(stripe_nr, nr_data_stripes(map));
  2302. /* Work out the disk rotation on this stripe-set */
  2303. rot = do_div(stripe_nr, map->num_stripes);
  2304. /* calculate which stripe this data locates */
  2305. rot += i;
  2306. stripe_index = rot % map->num_stripes;
  2307. if (stripe_index == num)
  2308. return 0;
  2309. if (stripe_index < num)
  2310. j++;
  2311. }
  2312. *offset = last_offset + j * map->stripe_len;
  2313. return 1;
  2314. }
  2315. static void scrub_free_parity(struct scrub_parity *sparity)
  2316. {
  2317. struct scrub_ctx *sctx = sparity->sctx;
  2318. struct scrub_page *curr, *next;
  2319. int nbits;
  2320. nbits = bitmap_weight(sparity->ebitmap, sparity->nsectors);
  2321. if (nbits) {
  2322. spin_lock(&sctx->stat_lock);
  2323. sctx->stat.read_errors += nbits;
  2324. sctx->stat.uncorrectable_errors += nbits;
  2325. spin_unlock(&sctx->stat_lock);
  2326. }
  2327. list_for_each_entry_safe(curr, next, &sparity->spages, list) {
  2328. list_del_init(&curr->list);
  2329. scrub_page_put(curr);
  2330. }
  2331. kfree(sparity);
  2332. }
  2333. static void scrub_parity_bio_endio(struct bio *bio, int error)
  2334. {
  2335. struct scrub_parity *sparity = (struct scrub_parity *)bio->bi_private;
  2336. struct scrub_ctx *sctx = sparity->sctx;
  2337. if (error)
  2338. bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
  2339. sparity->nsectors);
  2340. scrub_free_parity(sparity);
  2341. scrub_pending_bio_dec(sctx);
  2342. bio_put(bio);
  2343. }
  2344. static void scrub_parity_check_and_repair(struct scrub_parity *sparity)
  2345. {
  2346. struct scrub_ctx *sctx = sparity->sctx;
  2347. struct bio *bio;
  2348. struct btrfs_raid_bio *rbio;
  2349. struct scrub_page *spage;
  2350. struct btrfs_bio *bbio = NULL;
  2351. u64 length;
  2352. int ret;
  2353. if (!bitmap_andnot(sparity->dbitmap, sparity->dbitmap, sparity->ebitmap,
  2354. sparity->nsectors))
  2355. goto out;
  2356. length = sparity->logic_end - sparity->logic_start + 1;
  2357. ret = btrfs_map_sblock(sctx->dev_root->fs_info, WRITE,
  2358. sparity->logic_start,
  2359. &length, &bbio, 0, 1);
  2360. if (ret || !bbio || !bbio->raid_map)
  2361. goto bbio_out;
  2362. bio = btrfs_io_bio_alloc(GFP_NOFS, 0);
  2363. if (!bio)
  2364. goto bbio_out;
  2365. bio->bi_iter.bi_sector = sparity->logic_start >> 9;
  2366. bio->bi_private = sparity;
  2367. bio->bi_end_io = scrub_parity_bio_endio;
  2368. rbio = raid56_parity_alloc_scrub_rbio(sctx->dev_root, bio, bbio,
  2369. length, sparity->scrub_dev,
  2370. sparity->dbitmap,
  2371. sparity->nsectors);
  2372. if (!rbio)
  2373. goto rbio_out;
  2374. list_for_each_entry(spage, &sparity->spages, list)
  2375. raid56_parity_add_scrub_pages(rbio, spage->page,
  2376. spage->logical);
  2377. scrub_pending_bio_inc(sctx);
  2378. raid56_parity_submit_scrub_rbio(rbio);
  2379. return;
  2380. rbio_out:
  2381. bio_put(bio);
  2382. bbio_out:
  2383. btrfs_put_bbio(bbio);
  2384. bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
  2385. sparity->nsectors);
  2386. spin_lock(&sctx->stat_lock);
  2387. sctx->stat.malloc_errors++;
  2388. spin_unlock(&sctx->stat_lock);
  2389. out:
  2390. scrub_free_parity(sparity);
  2391. }
  2392. static inline int scrub_calc_parity_bitmap_len(int nsectors)
  2393. {
  2394. return DIV_ROUND_UP(nsectors, BITS_PER_LONG) * (BITS_PER_LONG / 8);
  2395. }
  2396. static void scrub_parity_get(struct scrub_parity *sparity)
  2397. {
  2398. atomic_inc(&sparity->refs);
  2399. }
  2400. static void scrub_parity_put(struct scrub_parity *sparity)
  2401. {
  2402. if (!atomic_dec_and_test(&sparity->refs))
  2403. return;
  2404. scrub_parity_check_and_repair(sparity);
  2405. }
  2406. static noinline_for_stack int scrub_raid56_parity(struct scrub_ctx *sctx,
  2407. struct map_lookup *map,
  2408. struct btrfs_device *sdev,
  2409. struct btrfs_path *path,
  2410. u64 logic_start,
  2411. u64 logic_end)
  2412. {
  2413. struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
  2414. struct btrfs_root *root = fs_info->extent_root;
  2415. struct btrfs_root *csum_root = fs_info->csum_root;
  2416. struct btrfs_extent_item *extent;
  2417. u64 flags;
  2418. int ret;
  2419. int slot;
  2420. struct extent_buffer *l;
  2421. struct btrfs_key key;
  2422. u64 generation;
  2423. u64 extent_logical;
  2424. u64 extent_physical;
  2425. u64 extent_len;
  2426. struct btrfs_device *extent_dev;
  2427. struct scrub_parity *sparity;
  2428. int nsectors;
  2429. int bitmap_len;
  2430. int extent_mirror_num;
  2431. int stop_loop = 0;
  2432. nsectors = map->stripe_len / root->sectorsize;
  2433. bitmap_len = scrub_calc_parity_bitmap_len(nsectors);
  2434. sparity = kzalloc(sizeof(struct scrub_parity) + 2 * bitmap_len,
  2435. GFP_NOFS);
  2436. if (!sparity) {
  2437. spin_lock(&sctx->stat_lock);
  2438. sctx->stat.malloc_errors++;
  2439. spin_unlock(&sctx->stat_lock);
  2440. return -ENOMEM;
  2441. }
  2442. sparity->stripe_len = map->stripe_len;
  2443. sparity->nsectors = nsectors;
  2444. sparity->sctx = sctx;
  2445. sparity->scrub_dev = sdev;
  2446. sparity->logic_start = logic_start;
  2447. sparity->logic_end = logic_end;
  2448. atomic_set(&sparity->refs, 1);
  2449. INIT_LIST_HEAD(&sparity->spages);
  2450. sparity->dbitmap = sparity->bitmap;
  2451. sparity->ebitmap = (void *)sparity->bitmap + bitmap_len;
  2452. ret = 0;
  2453. while (logic_start < logic_end) {
  2454. if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
  2455. key.type = BTRFS_METADATA_ITEM_KEY;
  2456. else
  2457. key.type = BTRFS_EXTENT_ITEM_KEY;
  2458. key.objectid = logic_start;
  2459. key.offset = (u64)-1;
  2460. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2461. if (ret < 0)
  2462. goto out;
  2463. if (ret > 0) {
  2464. ret = btrfs_previous_extent_item(root, path, 0);
  2465. if (ret < 0)
  2466. goto out;
  2467. if (ret > 0) {
  2468. btrfs_release_path(path);
  2469. ret = btrfs_search_slot(NULL, root, &key,
  2470. path, 0, 0);
  2471. if (ret < 0)
  2472. goto out;
  2473. }
  2474. }
  2475. stop_loop = 0;
  2476. while (1) {
  2477. u64 bytes;
  2478. l = path->nodes[0];
  2479. slot = path->slots[0];
  2480. if (slot >= btrfs_header_nritems(l)) {
  2481. ret = btrfs_next_leaf(root, path);
  2482. if (ret == 0)
  2483. continue;
  2484. if (ret < 0)
  2485. goto out;
  2486. stop_loop = 1;
  2487. break;
  2488. }
  2489. btrfs_item_key_to_cpu(l, &key, slot);
  2490. if (key.type == BTRFS_METADATA_ITEM_KEY)
  2491. bytes = root->nodesize;
  2492. else
  2493. bytes = key.offset;
  2494. if (key.objectid + bytes <= logic_start)
  2495. goto next;
  2496. if (key.type != BTRFS_EXTENT_ITEM_KEY &&
  2497. key.type != BTRFS_METADATA_ITEM_KEY)
  2498. goto next;
  2499. if (key.objectid > logic_end) {
  2500. stop_loop = 1;
  2501. break;
  2502. }
  2503. while (key.objectid >= logic_start + map->stripe_len)
  2504. logic_start += map->stripe_len;
  2505. extent = btrfs_item_ptr(l, slot,
  2506. struct btrfs_extent_item);
  2507. flags = btrfs_extent_flags(l, extent);
  2508. generation = btrfs_extent_generation(l, extent);
  2509. if (key.objectid < logic_start &&
  2510. (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
  2511. btrfs_err(fs_info,
  2512. "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
  2513. key.objectid, logic_start);
  2514. goto next;
  2515. }
  2516. again:
  2517. extent_logical = key.objectid;
  2518. extent_len = bytes;
  2519. if (extent_logical < logic_start) {
  2520. extent_len -= logic_start - extent_logical;
  2521. extent_logical = logic_start;
  2522. }
  2523. if (extent_logical + extent_len >
  2524. logic_start + map->stripe_len)
  2525. extent_len = logic_start + map->stripe_len -
  2526. extent_logical;
  2527. scrub_parity_mark_sectors_data(sparity, extent_logical,
  2528. extent_len);
  2529. scrub_remap_extent(fs_info, extent_logical,
  2530. extent_len, &extent_physical,
  2531. &extent_dev,
  2532. &extent_mirror_num);
  2533. ret = btrfs_lookup_csums_range(csum_root,
  2534. extent_logical,
  2535. extent_logical + extent_len - 1,
  2536. &sctx->csum_list, 1);
  2537. if (ret)
  2538. goto out;
  2539. ret = scrub_extent_for_parity(sparity, extent_logical,
  2540. extent_len,
  2541. extent_physical,
  2542. extent_dev, flags,
  2543. generation,
  2544. extent_mirror_num);
  2545. if (ret)
  2546. goto out;
  2547. scrub_free_csums(sctx);
  2548. if (extent_logical + extent_len <
  2549. key.objectid + bytes) {
  2550. logic_start += map->stripe_len;
  2551. if (logic_start >= logic_end) {
  2552. stop_loop = 1;
  2553. break;
  2554. }
  2555. if (logic_start < key.objectid + bytes) {
  2556. cond_resched();
  2557. goto again;
  2558. }
  2559. }
  2560. next:
  2561. path->slots[0]++;
  2562. }
  2563. btrfs_release_path(path);
  2564. if (stop_loop)
  2565. break;
  2566. logic_start += map->stripe_len;
  2567. }
  2568. out:
  2569. if (ret < 0)
  2570. scrub_parity_mark_sectors_error(sparity, logic_start,
  2571. logic_end - logic_start + 1);
  2572. scrub_parity_put(sparity);
  2573. scrub_submit(sctx);
  2574. mutex_lock(&sctx->wr_ctx.wr_lock);
  2575. scrub_wr_submit(sctx);
  2576. mutex_unlock(&sctx->wr_ctx.wr_lock);
  2577. btrfs_release_path(path);
  2578. return ret < 0 ? ret : 0;
  2579. }
  2580. static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
  2581. struct map_lookup *map,
  2582. struct btrfs_device *scrub_dev,
  2583. int num, u64 base, u64 length,
  2584. int is_dev_replace)
  2585. {
  2586. struct btrfs_path *path, *ppath;
  2587. struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
  2588. struct btrfs_root *root = fs_info->extent_root;
  2589. struct btrfs_root *csum_root = fs_info->csum_root;
  2590. struct btrfs_extent_item *extent;
  2591. struct blk_plug plug;
  2592. u64 flags;
  2593. int ret;
  2594. int slot;
  2595. u64 nstripes;
  2596. struct extent_buffer *l;
  2597. struct btrfs_key key;
  2598. u64 physical;
  2599. u64 logical;
  2600. u64 logic_end;
  2601. u64 physical_end;
  2602. u64 generation;
  2603. int mirror_num;
  2604. struct reada_control *reada1;
  2605. struct reada_control *reada2;
  2606. struct btrfs_key key_start;
  2607. struct btrfs_key key_end;
  2608. u64 increment = map->stripe_len;
  2609. u64 offset;
  2610. u64 extent_logical;
  2611. u64 extent_physical;
  2612. u64 extent_len;
  2613. u64 stripe_logical;
  2614. u64 stripe_end;
  2615. struct btrfs_device *extent_dev;
  2616. int extent_mirror_num;
  2617. int stop_loop = 0;
  2618. nstripes = length;
  2619. physical = map->stripes[num].physical;
  2620. offset = 0;
  2621. do_div(nstripes, map->stripe_len);
  2622. if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
  2623. offset = map->stripe_len * num;
  2624. increment = map->stripe_len * map->num_stripes;
  2625. mirror_num = 1;
  2626. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  2627. int factor = map->num_stripes / map->sub_stripes;
  2628. offset = map->stripe_len * (num / map->sub_stripes);
  2629. increment = map->stripe_len * factor;
  2630. mirror_num = num % map->sub_stripes + 1;
  2631. } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
  2632. increment = map->stripe_len;
  2633. mirror_num = num % map->num_stripes + 1;
  2634. } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
  2635. increment = map->stripe_len;
  2636. mirror_num = num % map->num_stripes + 1;
  2637. } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
  2638. get_raid56_logic_offset(physical, num, map, &offset, NULL);
  2639. increment = map->stripe_len * nr_data_stripes(map);
  2640. mirror_num = 1;
  2641. } else {
  2642. increment = map->stripe_len;
  2643. mirror_num = 1;
  2644. }
  2645. path = btrfs_alloc_path();
  2646. if (!path)
  2647. return -ENOMEM;
  2648. ppath = btrfs_alloc_path();
  2649. if (!ppath) {
  2650. btrfs_free_path(path);
  2651. return -ENOMEM;
  2652. }
  2653. /*
  2654. * work on commit root. The related disk blocks are static as
  2655. * long as COW is applied. This means, it is save to rewrite
  2656. * them to repair disk errors without any race conditions
  2657. */
  2658. path->search_commit_root = 1;
  2659. path->skip_locking = 1;
  2660. ppath->search_commit_root = 1;
  2661. ppath->skip_locking = 1;
  2662. /*
  2663. * trigger the readahead for extent tree csum tree and wait for
  2664. * completion. During readahead, the scrub is officially paused
  2665. * to not hold off transaction commits
  2666. */
  2667. logical = base + offset;
  2668. physical_end = physical + nstripes * map->stripe_len;
  2669. if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
  2670. get_raid56_logic_offset(physical_end, num,
  2671. map, &logic_end, NULL);
  2672. logic_end += base;
  2673. } else {
  2674. logic_end = logical + increment * nstripes;
  2675. }
  2676. wait_event(sctx->list_wait,
  2677. atomic_read(&sctx->bios_in_flight) == 0);
  2678. scrub_blocked_if_needed(fs_info);
  2679. /* FIXME it might be better to start readahead at commit root */
  2680. key_start.objectid = logical;
  2681. key_start.type = BTRFS_EXTENT_ITEM_KEY;
  2682. key_start.offset = (u64)0;
  2683. key_end.objectid = logic_end;
  2684. key_end.type = BTRFS_METADATA_ITEM_KEY;
  2685. key_end.offset = (u64)-1;
  2686. reada1 = btrfs_reada_add(root, &key_start, &key_end);
  2687. key_start.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
  2688. key_start.type = BTRFS_EXTENT_CSUM_KEY;
  2689. key_start.offset = logical;
  2690. key_end.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
  2691. key_end.type = BTRFS_EXTENT_CSUM_KEY;
  2692. key_end.offset = logic_end;
  2693. reada2 = btrfs_reada_add(csum_root, &key_start, &key_end);
  2694. if (!IS_ERR(reada1))
  2695. btrfs_reada_wait(reada1);
  2696. if (!IS_ERR(reada2))
  2697. btrfs_reada_wait(reada2);
  2698. /*
  2699. * collect all data csums for the stripe to avoid seeking during
  2700. * the scrub. This might currently (crc32) end up to be about 1MB
  2701. */
  2702. blk_start_plug(&plug);
  2703. /*
  2704. * now find all extents for each stripe and scrub them
  2705. */
  2706. ret = 0;
  2707. while (physical < physical_end) {
  2708. /* for raid56, we skip parity stripe */
  2709. if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
  2710. ret = get_raid56_logic_offset(physical, num,
  2711. map, &logical, &stripe_logical);
  2712. logical += base;
  2713. if (ret) {
  2714. stripe_logical += base;
  2715. stripe_end = stripe_logical + increment - 1;
  2716. ret = scrub_raid56_parity(sctx, map, scrub_dev,
  2717. ppath, stripe_logical,
  2718. stripe_end);
  2719. if (ret)
  2720. goto out;
  2721. goto skip;
  2722. }
  2723. }
  2724. /*
  2725. * canceled?
  2726. */
  2727. if (atomic_read(&fs_info->scrub_cancel_req) ||
  2728. atomic_read(&sctx->cancel_req)) {
  2729. ret = -ECANCELED;
  2730. goto out;
  2731. }
  2732. /*
  2733. * check to see if we have to pause
  2734. */
  2735. if (atomic_read(&fs_info->scrub_pause_req)) {
  2736. /* push queued extents */
  2737. atomic_set(&sctx->wr_ctx.flush_all_writes, 1);
  2738. scrub_submit(sctx);
  2739. mutex_lock(&sctx->wr_ctx.wr_lock);
  2740. scrub_wr_submit(sctx);
  2741. mutex_unlock(&sctx->wr_ctx.wr_lock);
  2742. wait_event(sctx->list_wait,
  2743. atomic_read(&sctx->bios_in_flight) == 0);
  2744. atomic_set(&sctx->wr_ctx.flush_all_writes, 0);
  2745. scrub_blocked_if_needed(fs_info);
  2746. }
  2747. if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
  2748. key.type = BTRFS_METADATA_ITEM_KEY;
  2749. else
  2750. key.type = BTRFS_EXTENT_ITEM_KEY;
  2751. key.objectid = logical;
  2752. key.offset = (u64)-1;
  2753. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2754. if (ret < 0)
  2755. goto out;
  2756. if (ret > 0) {
  2757. ret = btrfs_previous_extent_item(root, path, 0);
  2758. if (ret < 0)
  2759. goto out;
  2760. if (ret > 0) {
  2761. /* there's no smaller item, so stick with the
  2762. * larger one */
  2763. btrfs_release_path(path);
  2764. ret = btrfs_search_slot(NULL, root, &key,
  2765. path, 0, 0);
  2766. if (ret < 0)
  2767. goto out;
  2768. }
  2769. }
  2770. stop_loop = 0;
  2771. while (1) {
  2772. u64 bytes;
  2773. l = path->nodes[0];
  2774. slot = path->slots[0];
  2775. if (slot >= btrfs_header_nritems(l)) {
  2776. ret = btrfs_next_leaf(root, path);
  2777. if (ret == 0)
  2778. continue;
  2779. if (ret < 0)
  2780. goto out;
  2781. stop_loop = 1;
  2782. break;
  2783. }
  2784. btrfs_item_key_to_cpu(l, &key, slot);
  2785. if (key.type == BTRFS_METADATA_ITEM_KEY)
  2786. bytes = root->nodesize;
  2787. else
  2788. bytes = key.offset;
  2789. if (key.objectid + bytes <= logical)
  2790. goto next;
  2791. if (key.type != BTRFS_EXTENT_ITEM_KEY &&
  2792. key.type != BTRFS_METADATA_ITEM_KEY)
  2793. goto next;
  2794. if (key.objectid >= logical + map->stripe_len) {
  2795. /* out of this device extent */
  2796. if (key.objectid >= logic_end)
  2797. stop_loop = 1;
  2798. break;
  2799. }
  2800. extent = btrfs_item_ptr(l, slot,
  2801. struct btrfs_extent_item);
  2802. flags = btrfs_extent_flags(l, extent);
  2803. generation = btrfs_extent_generation(l, extent);
  2804. if (key.objectid < logical &&
  2805. (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
  2806. btrfs_err(fs_info,
  2807. "scrub: tree block %llu spanning "
  2808. "stripes, ignored. logical=%llu",
  2809. key.objectid, logical);
  2810. goto next;
  2811. }
  2812. again:
  2813. extent_logical = key.objectid;
  2814. extent_len = bytes;
  2815. /*
  2816. * trim extent to this stripe
  2817. */
  2818. if (extent_logical < logical) {
  2819. extent_len -= logical - extent_logical;
  2820. extent_logical = logical;
  2821. }
  2822. if (extent_logical + extent_len >
  2823. logical + map->stripe_len) {
  2824. extent_len = logical + map->stripe_len -
  2825. extent_logical;
  2826. }
  2827. extent_physical = extent_logical - logical + physical;
  2828. extent_dev = scrub_dev;
  2829. extent_mirror_num = mirror_num;
  2830. if (is_dev_replace)
  2831. scrub_remap_extent(fs_info, extent_logical,
  2832. extent_len, &extent_physical,
  2833. &extent_dev,
  2834. &extent_mirror_num);
  2835. ret = btrfs_lookup_csums_range(csum_root, logical,
  2836. logical + map->stripe_len - 1,
  2837. &sctx->csum_list, 1);
  2838. if (ret)
  2839. goto out;
  2840. ret = scrub_extent(sctx, extent_logical, extent_len,
  2841. extent_physical, extent_dev, flags,
  2842. generation, extent_mirror_num,
  2843. extent_logical - logical + physical);
  2844. if (ret)
  2845. goto out;
  2846. scrub_free_csums(sctx);
  2847. if (extent_logical + extent_len <
  2848. key.objectid + bytes) {
  2849. if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
  2850. /*
  2851. * loop until we find next data stripe
  2852. * or we have finished all stripes.
  2853. */
  2854. loop:
  2855. physical += map->stripe_len;
  2856. ret = get_raid56_logic_offset(physical,
  2857. num, map, &logical,
  2858. &stripe_logical);
  2859. logical += base;
  2860. if (ret && physical < physical_end) {
  2861. stripe_logical += base;
  2862. stripe_end = stripe_logical +
  2863. increment - 1;
  2864. ret = scrub_raid56_parity(sctx,
  2865. map, scrub_dev, ppath,
  2866. stripe_logical,
  2867. stripe_end);
  2868. if (ret)
  2869. goto out;
  2870. goto loop;
  2871. }
  2872. } else {
  2873. physical += map->stripe_len;
  2874. logical += increment;
  2875. }
  2876. if (logical < key.objectid + bytes) {
  2877. cond_resched();
  2878. goto again;
  2879. }
  2880. if (physical >= physical_end) {
  2881. stop_loop = 1;
  2882. break;
  2883. }
  2884. }
  2885. next:
  2886. path->slots[0]++;
  2887. }
  2888. btrfs_release_path(path);
  2889. skip:
  2890. logical += increment;
  2891. physical += map->stripe_len;
  2892. spin_lock(&sctx->stat_lock);
  2893. if (stop_loop)
  2894. sctx->stat.last_physical = map->stripes[num].physical +
  2895. length;
  2896. else
  2897. sctx->stat.last_physical = physical;
  2898. spin_unlock(&sctx->stat_lock);
  2899. if (stop_loop)
  2900. break;
  2901. }
  2902. out:
  2903. /* push queued extents */
  2904. scrub_submit(sctx);
  2905. mutex_lock(&sctx->wr_ctx.wr_lock);
  2906. scrub_wr_submit(sctx);
  2907. mutex_unlock(&sctx->wr_ctx.wr_lock);
  2908. blk_finish_plug(&plug);
  2909. btrfs_free_path(path);
  2910. btrfs_free_path(ppath);
  2911. return ret < 0 ? ret : 0;
  2912. }
  2913. static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
  2914. struct btrfs_device *scrub_dev,
  2915. u64 chunk_tree, u64 chunk_objectid,
  2916. u64 chunk_offset, u64 length,
  2917. u64 dev_offset, int is_dev_replace)
  2918. {
  2919. struct btrfs_mapping_tree *map_tree =
  2920. &sctx->dev_root->fs_info->mapping_tree;
  2921. struct map_lookup *map;
  2922. struct extent_map *em;
  2923. int i;
  2924. int ret = 0;
  2925. read_lock(&map_tree->map_tree.lock);
  2926. em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
  2927. read_unlock(&map_tree->map_tree.lock);
  2928. if (!em)
  2929. return -EINVAL;
  2930. map = (struct map_lookup *)em->bdev;
  2931. if (em->start != chunk_offset)
  2932. goto out;
  2933. if (em->len < length)
  2934. goto out;
  2935. for (i = 0; i < map->num_stripes; ++i) {
  2936. if (map->stripes[i].dev->bdev == scrub_dev->bdev &&
  2937. map->stripes[i].physical == dev_offset) {
  2938. ret = scrub_stripe(sctx, map, scrub_dev, i,
  2939. chunk_offset, length,
  2940. is_dev_replace);
  2941. if (ret)
  2942. goto out;
  2943. }
  2944. }
  2945. out:
  2946. free_extent_map(em);
  2947. return ret;
  2948. }
  2949. static noinline_for_stack
  2950. int scrub_enumerate_chunks(struct scrub_ctx *sctx,
  2951. struct btrfs_device *scrub_dev, u64 start, u64 end,
  2952. int is_dev_replace)
  2953. {
  2954. struct btrfs_dev_extent *dev_extent = NULL;
  2955. struct btrfs_path *path;
  2956. struct btrfs_root *root = sctx->dev_root;
  2957. struct btrfs_fs_info *fs_info = root->fs_info;
  2958. u64 length;
  2959. u64 chunk_tree;
  2960. u64 chunk_objectid;
  2961. u64 chunk_offset;
  2962. int ret;
  2963. int slot;
  2964. struct extent_buffer *l;
  2965. struct btrfs_key key;
  2966. struct btrfs_key found_key;
  2967. struct btrfs_block_group_cache *cache;
  2968. struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
  2969. path = btrfs_alloc_path();
  2970. if (!path)
  2971. return -ENOMEM;
  2972. path->reada = 2;
  2973. path->search_commit_root = 1;
  2974. path->skip_locking = 1;
  2975. key.objectid = scrub_dev->devid;
  2976. key.offset = 0ull;
  2977. key.type = BTRFS_DEV_EXTENT_KEY;
  2978. while (1) {
  2979. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2980. if (ret < 0)
  2981. break;
  2982. if (ret > 0) {
  2983. if (path->slots[0] >=
  2984. btrfs_header_nritems(path->nodes[0])) {
  2985. ret = btrfs_next_leaf(root, path);
  2986. if (ret)
  2987. break;
  2988. }
  2989. }
  2990. l = path->nodes[0];
  2991. slot = path->slots[0];
  2992. btrfs_item_key_to_cpu(l, &found_key, slot);
  2993. if (found_key.objectid != scrub_dev->devid)
  2994. break;
  2995. if (found_key.type != BTRFS_DEV_EXTENT_KEY)
  2996. break;
  2997. if (found_key.offset >= end)
  2998. break;
  2999. if (found_key.offset < key.offset)
  3000. break;
  3001. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  3002. length = btrfs_dev_extent_length(l, dev_extent);
  3003. if (found_key.offset + length <= start)
  3004. goto skip;
  3005. chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
  3006. chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
  3007. chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
  3008. /*
  3009. * get a reference on the corresponding block group to prevent
  3010. * the chunk from going away while we scrub it
  3011. */
  3012. cache = btrfs_lookup_block_group(fs_info, chunk_offset);
  3013. /* some chunks are removed but not committed to disk yet,
  3014. * continue scrubbing */
  3015. if (!cache)
  3016. goto skip;
  3017. dev_replace->cursor_right = found_key.offset + length;
  3018. dev_replace->cursor_left = found_key.offset;
  3019. dev_replace->item_needs_writeback = 1;
  3020. ret = scrub_chunk(sctx, scrub_dev, chunk_tree, chunk_objectid,
  3021. chunk_offset, length, found_key.offset,
  3022. is_dev_replace);
  3023. /*
  3024. * flush, submit all pending read and write bios, afterwards
  3025. * wait for them.
  3026. * Note that in the dev replace case, a read request causes
  3027. * write requests that are submitted in the read completion
  3028. * worker. Therefore in the current situation, it is required
  3029. * that all write requests are flushed, so that all read and
  3030. * write requests are really completed when bios_in_flight
  3031. * changes to 0.
  3032. */
  3033. atomic_set(&sctx->wr_ctx.flush_all_writes, 1);
  3034. scrub_submit(sctx);
  3035. mutex_lock(&sctx->wr_ctx.wr_lock);
  3036. scrub_wr_submit(sctx);
  3037. mutex_unlock(&sctx->wr_ctx.wr_lock);
  3038. wait_event(sctx->list_wait,
  3039. atomic_read(&sctx->bios_in_flight) == 0);
  3040. atomic_inc(&fs_info->scrubs_paused);
  3041. wake_up(&fs_info->scrub_pause_wait);
  3042. /*
  3043. * must be called before we decrease @scrub_paused.
  3044. * make sure we don't block transaction commit while
  3045. * we are waiting pending workers finished.
  3046. */
  3047. wait_event(sctx->list_wait,
  3048. atomic_read(&sctx->workers_pending) == 0);
  3049. atomic_set(&sctx->wr_ctx.flush_all_writes, 0);
  3050. mutex_lock(&fs_info->scrub_lock);
  3051. __scrub_blocked_if_needed(fs_info);
  3052. atomic_dec(&fs_info->scrubs_paused);
  3053. mutex_unlock(&fs_info->scrub_lock);
  3054. wake_up(&fs_info->scrub_pause_wait);
  3055. btrfs_put_block_group(cache);
  3056. if (ret)
  3057. break;
  3058. if (is_dev_replace &&
  3059. atomic64_read(&dev_replace->num_write_errors) > 0) {
  3060. ret = -EIO;
  3061. break;
  3062. }
  3063. if (sctx->stat.malloc_errors > 0) {
  3064. ret = -ENOMEM;
  3065. break;
  3066. }
  3067. dev_replace->cursor_left = dev_replace->cursor_right;
  3068. dev_replace->item_needs_writeback = 1;
  3069. skip:
  3070. key.offset = found_key.offset + length;
  3071. btrfs_release_path(path);
  3072. }
  3073. btrfs_free_path(path);
  3074. /*
  3075. * ret can still be 1 from search_slot or next_leaf,
  3076. * that's not an error
  3077. */
  3078. return ret < 0 ? ret : 0;
  3079. }
  3080. static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,
  3081. struct btrfs_device *scrub_dev)
  3082. {
  3083. int i;
  3084. u64 bytenr;
  3085. u64 gen;
  3086. int ret;
  3087. struct btrfs_root *root = sctx->dev_root;
  3088. if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
  3089. return -EIO;
  3090. /* Seed devices of a new filesystem has their own generation. */
  3091. if (scrub_dev->fs_devices != root->fs_info->fs_devices)
  3092. gen = scrub_dev->generation;
  3093. else
  3094. gen = root->fs_info->last_trans_committed;
  3095. for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
  3096. bytenr = btrfs_sb_offset(i);
  3097. if (bytenr + BTRFS_SUPER_INFO_SIZE >
  3098. scrub_dev->commit_total_bytes)
  3099. break;
  3100. ret = scrub_pages(sctx, bytenr, BTRFS_SUPER_INFO_SIZE, bytenr,
  3101. scrub_dev, BTRFS_EXTENT_FLAG_SUPER, gen, i,
  3102. NULL, 1, bytenr);
  3103. if (ret)
  3104. return ret;
  3105. }
  3106. wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
  3107. return 0;
  3108. }
  3109. /*
  3110. * get a reference count on fs_info->scrub_workers. start worker if necessary
  3111. */
  3112. static noinline_for_stack int scrub_workers_get(struct btrfs_fs_info *fs_info,
  3113. int is_dev_replace)
  3114. {
  3115. int ret = 0;
  3116. int flags = WQ_FREEZABLE | WQ_UNBOUND;
  3117. int max_active = fs_info->thread_pool_size;
  3118. if (fs_info->scrub_workers_refcnt == 0) {
  3119. if (is_dev_replace)
  3120. fs_info->scrub_workers =
  3121. btrfs_alloc_workqueue("btrfs-scrub", flags,
  3122. 1, 4);
  3123. else
  3124. fs_info->scrub_workers =
  3125. btrfs_alloc_workqueue("btrfs-scrub", flags,
  3126. max_active, 4);
  3127. if (!fs_info->scrub_workers) {
  3128. ret = -ENOMEM;
  3129. goto out;
  3130. }
  3131. fs_info->scrub_wr_completion_workers =
  3132. btrfs_alloc_workqueue("btrfs-scrubwrc", flags,
  3133. max_active, 2);
  3134. if (!fs_info->scrub_wr_completion_workers) {
  3135. ret = -ENOMEM;
  3136. goto out;
  3137. }
  3138. fs_info->scrub_nocow_workers =
  3139. btrfs_alloc_workqueue("btrfs-scrubnc", flags, 1, 0);
  3140. if (!fs_info->scrub_nocow_workers) {
  3141. ret = -ENOMEM;
  3142. goto out;
  3143. }
  3144. }
  3145. ++fs_info->scrub_workers_refcnt;
  3146. out:
  3147. return ret;
  3148. }
  3149. static noinline_for_stack void scrub_workers_put(struct btrfs_fs_info *fs_info)
  3150. {
  3151. if (--fs_info->scrub_workers_refcnt == 0) {
  3152. btrfs_destroy_workqueue(fs_info->scrub_workers);
  3153. btrfs_destroy_workqueue(fs_info->scrub_wr_completion_workers);
  3154. btrfs_destroy_workqueue(fs_info->scrub_nocow_workers);
  3155. }
  3156. WARN_ON(fs_info->scrub_workers_refcnt < 0);
  3157. }
  3158. int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
  3159. u64 end, struct btrfs_scrub_progress *progress,
  3160. int readonly, int is_dev_replace)
  3161. {
  3162. struct scrub_ctx *sctx;
  3163. int ret;
  3164. struct btrfs_device *dev;
  3165. struct rcu_string *name;
  3166. if (btrfs_fs_closing(fs_info))
  3167. return -EINVAL;
  3168. if (fs_info->chunk_root->nodesize > BTRFS_STRIPE_LEN) {
  3169. /*
  3170. * in this case scrub is unable to calculate the checksum
  3171. * the way scrub is implemented. Do not handle this
  3172. * situation at all because it won't ever happen.
  3173. */
  3174. btrfs_err(fs_info,
  3175. "scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails",
  3176. fs_info->chunk_root->nodesize, BTRFS_STRIPE_LEN);
  3177. return -EINVAL;
  3178. }
  3179. if (fs_info->chunk_root->sectorsize != PAGE_SIZE) {
  3180. /* not supported for data w/o checksums */
  3181. btrfs_err(fs_info,
  3182. "scrub: size assumption sectorsize != PAGE_SIZE "
  3183. "(%d != %lu) fails",
  3184. fs_info->chunk_root->sectorsize, PAGE_SIZE);
  3185. return -EINVAL;
  3186. }
  3187. if (fs_info->chunk_root->nodesize >
  3188. PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK ||
  3189. fs_info->chunk_root->sectorsize >
  3190. PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK) {
  3191. /*
  3192. * would exhaust the array bounds of pagev member in
  3193. * struct scrub_block
  3194. */
  3195. btrfs_err(fs_info, "scrub: size assumption nodesize and sectorsize "
  3196. "<= SCRUB_MAX_PAGES_PER_BLOCK (%d <= %d && %d <= %d) fails",
  3197. fs_info->chunk_root->nodesize,
  3198. SCRUB_MAX_PAGES_PER_BLOCK,
  3199. fs_info->chunk_root->sectorsize,
  3200. SCRUB_MAX_PAGES_PER_BLOCK);
  3201. return -EINVAL;
  3202. }
  3203. mutex_lock(&fs_info->fs_devices->device_list_mutex);
  3204. dev = btrfs_find_device(fs_info, devid, NULL, NULL);
  3205. if (!dev || (dev->missing && !is_dev_replace)) {
  3206. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  3207. return -ENODEV;
  3208. }
  3209. if (!is_dev_replace && !readonly && !dev->writeable) {
  3210. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  3211. rcu_read_lock();
  3212. name = rcu_dereference(dev->name);
  3213. btrfs_err(fs_info, "scrub: device %s is not writable",
  3214. name->str);
  3215. rcu_read_unlock();
  3216. return -EROFS;
  3217. }
  3218. mutex_lock(&fs_info->scrub_lock);
  3219. if (!dev->in_fs_metadata || dev->is_tgtdev_for_dev_replace) {
  3220. mutex_unlock(&fs_info->scrub_lock);
  3221. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  3222. return -EIO;
  3223. }
  3224. btrfs_dev_replace_lock(&fs_info->dev_replace);
  3225. if (dev->scrub_device ||
  3226. (!is_dev_replace &&
  3227. btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))) {
  3228. btrfs_dev_replace_unlock(&fs_info->dev_replace);
  3229. mutex_unlock(&fs_info->scrub_lock);
  3230. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  3231. return -EINPROGRESS;
  3232. }
  3233. btrfs_dev_replace_unlock(&fs_info->dev_replace);
  3234. ret = scrub_workers_get(fs_info, is_dev_replace);
  3235. if (ret) {
  3236. mutex_unlock(&fs_info->scrub_lock);
  3237. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  3238. return ret;
  3239. }
  3240. sctx = scrub_setup_ctx(dev, is_dev_replace);
  3241. if (IS_ERR(sctx)) {
  3242. mutex_unlock(&fs_info->scrub_lock);
  3243. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  3244. scrub_workers_put(fs_info);
  3245. return PTR_ERR(sctx);
  3246. }
  3247. sctx->readonly = readonly;
  3248. dev->scrub_device = sctx;
  3249. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  3250. /*
  3251. * checking @scrub_pause_req here, we can avoid
  3252. * race between committing transaction and scrubbing.
  3253. */
  3254. __scrub_blocked_if_needed(fs_info);
  3255. atomic_inc(&fs_info->scrubs_running);
  3256. mutex_unlock(&fs_info->scrub_lock);
  3257. if (!is_dev_replace) {
  3258. /*
  3259. * by holding device list mutex, we can
  3260. * kick off writing super in log tree sync.
  3261. */
  3262. mutex_lock(&fs_info->fs_devices->device_list_mutex);
  3263. ret = scrub_supers(sctx, dev);
  3264. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  3265. }
  3266. if (!ret)
  3267. ret = scrub_enumerate_chunks(sctx, dev, start, end,
  3268. is_dev_replace);
  3269. wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
  3270. atomic_dec(&fs_info->scrubs_running);
  3271. wake_up(&fs_info->scrub_pause_wait);
  3272. wait_event(sctx->list_wait, atomic_read(&sctx->workers_pending) == 0);
  3273. if (progress)
  3274. memcpy(progress, &sctx->stat, sizeof(*progress));
  3275. mutex_lock(&fs_info->scrub_lock);
  3276. dev->scrub_device = NULL;
  3277. scrub_workers_put(fs_info);
  3278. mutex_unlock(&fs_info->scrub_lock);
  3279. scrub_put_ctx(sctx);
  3280. return ret;
  3281. }
  3282. void btrfs_scrub_pause(struct btrfs_root *root)
  3283. {
  3284. struct btrfs_fs_info *fs_info = root->fs_info;
  3285. mutex_lock(&fs_info->scrub_lock);
  3286. atomic_inc(&fs_info->scrub_pause_req);
  3287. while (atomic_read(&fs_info->scrubs_paused) !=
  3288. atomic_read(&fs_info->scrubs_running)) {
  3289. mutex_unlock(&fs_info->scrub_lock);
  3290. wait_event(fs_info->scrub_pause_wait,
  3291. atomic_read(&fs_info->scrubs_paused) ==
  3292. atomic_read(&fs_info->scrubs_running));
  3293. mutex_lock(&fs_info->scrub_lock);
  3294. }
  3295. mutex_unlock(&fs_info->scrub_lock);
  3296. }
  3297. void btrfs_scrub_continue(struct btrfs_root *root)
  3298. {
  3299. struct btrfs_fs_info *fs_info = root->fs_info;
  3300. atomic_dec(&fs_info->scrub_pause_req);
  3301. wake_up(&fs_info->scrub_pause_wait);
  3302. }
  3303. int btrfs_scrub_cancel(struct btrfs_fs_info *fs_info)
  3304. {
  3305. mutex_lock(&fs_info->scrub_lock);
  3306. if (!atomic_read(&fs_info->scrubs_running)) {
  3307. mutex_unlock(&fs_info->scrub_lock);
  3308. return -ENOTCONN;
  3309. }
  3310. atomic_inc(&fs_info->scrub_cancel_req);
  3311. while (atomic_read(&fs_info->scrubs_running)) {
  3312. mutex_unlock(&fs_info->scrub_lock);
  3313. wait_event(fs_info->scrub_pause_wait,
  3314. atomic_read(&fs_info->scrubs_running) == 0);
  3315. mutex_lock(&fs_info->scrub_lock);
  3316. }
  3317. atomic_dec(&fs_info->scrub_cancel_req);
  3318. mutex_unlock(&fs_info->scrub_lock);
  3319. return 0;
  3320. }
  3321. int btrfs_scrub_cancel_dev(struct btrfs_fs_info *fs_info,
  3322. struct btrfs_device *dev)
  3323. {
  3324. struct scrub_ctx *sctx;
  3325. mutex_lock(&fs_info->scrub_lock);
  3326. sctx = dev->scrub_device;
  3327. if (!sctx) {
  3328. mutex_unlock(&fs_info->scrub_lock);
  3329. return -ENOTCONN;
  3330. }
  3331. atomic_inc(&sctx->cancel_req);
  3332. while (dev->scrub_device) {
  3333. mutex_unlock(&fs_info->scrub_lock);
  3334. wait_event(fs_info->scrub_pause_wait,
  3335. dev->scrub_device == NULL);
  3336. mutex_lock(&fs_info->scrub_lock);
  3337. }
  3338. mutex_unlock(&fs_info->scrub_lock);
  3339. return 0;
  3340. }
  3341. int btrfs_scrub_progress(struct btrfs_root *root, u64 devid,
  3342. struct btrfs_scrub_progress *progress)
  3343. {
  3344. struct btrfs_device *dev;
  3345. struct scrub_ctx *sctx = NULL;
  3346. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  3347. dev = btrfs_find_device(root->fs_info, devid, NULL, NULL);
  3348. if (dev)
  3349. sctx = dev->scrub_device;
  3350. if (sctx)
  3351. memcpy(progress, &sctx->stat, sizeof(*progress));
  3352. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  3353. return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV;
  3354. }
  3355. static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
  3356. u64 extent_logical, u64 extent_len,
  3357. u64 *extent_physical,
  3358. struct btrfs_device **extent_dev,
  3359. int *extent_mirror_num)
  3360. {
  3361. u64 mapped_length;
  3362. struct btrfs_bio *bbio = NULL;
  3363. int ret;
  3364. mapped_length = extent_len;
  3365. ret = btrfs_map_block(fs_info, READ, extent_logical,
  3366. &mapped_length, &bbio, 0);
  3367. if (ret || !bbio || mapped_length < extent_len ||
  3368. !bbio->stripes[0].dev->bdev) {
  3369. btrfs_put_bbio(bbio);
  3370. return;
  3371. }
  3372. *extent_physical = bbio->stripes[0].physical;
  3373. *extent_mirror_num = bbio->mirror_num;
  3374. *extent_dev = bbio->stripes[0].dev;
  3375. btrfs_put_bbio(bbio);
  3376. }
  3377. static int scrub_setup_wr_ctx(struct scrub_ctx *sctx,
  3378. struct scrub_wr_ctx *wr_ctx,
  3379. struct btrfs_fs_info *fs_info,
  3380. struct btrfs_device *dev,
  3381. int is_dev_replace)
  3382. {
  3383. WARN_ON(wr_ctx->wr_curr_bio != NULL);
  3384. mutex_init(&wr_ctx->wr_lock);
  3385. wr_ctx->wr_curr_bio = NULL;
  3386. if (!is_dev_replace)
  3387. return 0;
  3388. WARN_ON(!dev->bdev);
  3389. wr_ctx->pages_per_wr_bio = min_t(int, SCRUB_PAGES_PER_WR_BIO,
  3390. bio_get_nr_vecs(dev->bdev));
  3391. wr_ctx->tgtdev = dev;
  3392. atomic_set(&wr_ctx->flush_all_writes, 0);
  3393. return 0;
  3394. }
  3395. static void scrub_free_wr_ctx(struct scrub_wr_ctx *wr_ctx)
  3396. {
  3397. mutex_lock(&wr_ctx->wr_lock);
  3398. kfree(wr_ctx->wr_curr_bio);
  3399. wr_ctx->wr_curr_bio = NULL;
  3400. mutex_unlock(&wr_ctx->wr_lock);
  3401. }
  3402. static int copy_nocow_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
  3403. int mirror_num, u64 physical_for_dev_replace)
  3404. {
  3405. struct scrub_copy_nocow_ctx *nocow_ctx;
  3406. struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
  3407. nocow_ctx = kzalloc(sizeof(*nocow_ctx), GFP_NOFS);
  3408. if (!nocow_ctx) {
  3409. spin_lock(&sctx->stat_lock);
  3410. sctx->stat.malloc_errors++;
  3411. spin_unlock(&sctx->stat_lock);
  3412. return -ENOMEM;
  3413. }
  3414. scrub_pending_trans_workers_inc(sctx);
  3415. nocow_ctx->sctx = sctx;
  3416. nocow_ctx->logical = logical;
  3417. nocow_ctx->len = len;
  3418. nocow_ctx->mirror_num = mirror_num;
  3419. nocow_ctx->physical_for_dev_replace = physical_for_dev_replace;
  3420. btrfs_init_work(&nocow_ctx->work, btrfs_scrubnc_helper,
  3421. copy_nocow_pages_worker, NULL, NULL);
  3422. INIT_LIST_HEAD(&nocow_ctx->inodes);
  3423. btrfs_queue_work(fs_info->scrub_nocow_workers,
  3424. &nocow_ctx->work);
  3425. return 0;
  3426. }
  3427. static int record_inode_for_nocow(u64 inum, u64 offset, u64 root, void *ctx)
  3428. {
  3429. struct scrub_copy_nocow_ctx *nocow_ctx = ctx;
  3430. struct scrub_nocow_inode *nocow_inode;
  3431. nocow_inode = kzalloc(sizeof(*nocow_inode), GFP_NOFS);
  3432. if (!nocow_inode)
  3433. return -ENOMEM;
  3434. nocow_inode->inum = inum;
  3435. nocow_inode->offset = offset;
  3436. nocow_inode->root = root;
  3437. list_add_tail(&nocow_inode->list, &nocow_ctx->inodes);
  3438. return 0;
  3439. }
  3440. #define COPY_COMPLETE 1
  3441. static void copy_nocow_pages_worker(struct btrfs_work *work)
  3442. {
  3443. struct scrub_copy_nocow_ctx *nocow_ctx =
  3444. container_of(work, struct scrub_copy_nocow_ctx, work);
  3445. struct scrub_ctx *sctx = nocow_ctx->sctx;
  3446. u64 logical = nocow_ctx->logical;
  3447. u64 len = nocow_ctx->len;
  3448. int mirror_num = nocow_ctx->mirror_num;
  3449. u64 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
  3450. int ret;
  3451. struct btrfs_trans_handle *trans = NULL;
  3452. struct btrfs_fs_info *fs_info;
  3453. struct btrfs_path *path;
  3454. struct btrfs_root *root;
  3455. int not_written = 0;
  3456. fs_info = sctx->dev_root->fs_info;
  3457. root = fs_info->extent_root;
  3458. path = btrfs_alloc_path();
  3459. if (!path) {
  3460. spin_lock(&sctx->stat_lock);
  3461. sctx->stat.malloc_errors++;
  3462. spin_unlock(&sctx->stat_lock);
  3463. not_written = 1;
  3464. goto out;
  3465. }
  3466. trans = btrfs_join_transaction(root);
  3467. if (IS_ERR(trans)) {
  3468. not_written = 1;
  3469. goto out;
  3470. }
  3471. ret = iterate_inodes_from_logical(logical, fs_info, path,
  3472. record_inode_for_nocow, nocow_ctx);
  3473. if (ret != 0 && ret != -ENOENT) {
  3474. btrfs_warn(fs_info, "iterate_inodes_from_logical() failed: log %llu, "
  3475. "phys %llu, len %llu, mir %u, ret %d",
  3476. logical, physical_for_dev_replace, len, mirror_num,
  3477. ret);
  3478. not_written = 1;
  3479. goto out;
  3480. }
  3481. btrfs_end_transaction(trans, root);
  3482. trans = NULL;
  3483. while (!list_empty(&nocow_ctx->inodes)) {
  3484. struct scrub_nocow_inode *entry;
  3485. entry = list_first_entry(&nocow_ctx->inodes,
  3486. struct scrub_nocow_inode,
  3487. list);
  3488. list_del_init(&entry->list);
  3489. ret = copy_nocow_pages_for_inode(entry->inum, entry->offset,
  3490. entry->root, nocow_ctx);
  3491. kfree(entry);
  3492. if (ret == COPY_COMPLETE) {
  3493. ret = 0;
  3494. break;
  3495. } else if (ret) {
  3496. break;
  3497. }
  3498. }
  3499. out:
  3500. while (!list_empty(&nocow_ctx->inodes)) {
  3501. struct scrub_nocow_inode *entry;
  3502. entry = list_first_entry(&nocow_ctx->inodes,
  3503. struct scrub_nocow_inode,
  3504. list);
  3505. list_del_init(&entry->list);
  3506. kfree(entry);
  3507. }
  3508. if (trans && !IS_ERR(trans))
  3509. btrfs_end_transaction(trans, root);
  3510. if (not_written)
  3511. btrfs_dev_replace_stats_inc(&fs_info->dev_replace.
  3512. num_uncorrectable_read_errors);
  3513. btrfs_free_path(path);
  3514. kfree(nocow_ctx);
  3515. scrub_pending_trans_workers_dec(sctx);
  3516. }
  3517. static int check_extent_to_block(struct inode *inode, u64 start, u64 len,
  3518. u64 logical)
  3519. {
  3520. struct extent_state *cached_state = NULL;
  3521. struct btrfs_ordered_extent *ordered;
  3522. struct extent_io_tree *io_tree;
  3523. struct extent_map *em;
  3524. u64 lockstart = start, lockend = start + len - 1;
  3525. int ret = 0;
  3526. io_tree = &BTRFS_I(inode)->io_tree;
  3527. lock_extent_bits(io_tree, lockstart, lockend, 0, &cached_state);
  3528. ordered = btrfs_lookup_ordered_range(inode, lockstart, len);
  3529. if (ordered) {
  3530. btrfs_put_ordered_extent(ordered);
  3531. ret = 1;
  3532. goto out_unlock;
  3533. }
  3534. em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
  3535. if (IS_ERR(em)) {
  3536. ret = PTR_ERR(em);
  3537. goto out_unlock;
  3538. }
  3539. /*
  3540. * This extent does not actually cover the logical extent anymore,
  3541. * move on to the next inode.
  3542. */
  3543. if (em->block_start > logical ||
  3544. em->block_start + em->block_len < logical + len) {
  3545. free_extent_map(em);
  3546. ret = 1;
  3547. goto out_unlock;
  3548. }
  3549. free_extent_map(em);
  3550. out_unlock:
  3551. unlock_extent_cached(io_tree, lockstart, lockend, &cached_state,
  3552. GFP_NOFS);
  3553. return ret;
  3554. }
  3555. static int copy_nocow_pages_for_inode(u64 inum, u64 offset, u64 root,
  3556. struct scrub_copy_nocow_ctx *nocow_ctx)
  3557. {
  3558. struct btrfs_fs_info *fs_info = nocow_ctx->sctx->dev_root->fs_info;
  3559. struct btrfs_key key;
  3560. struct inode *inode;
  3561. struct page *page;
  3562. struct btrfs_root *local_root;
  3563. struct extent_io_tree *io_tree;
  3564. u64 physical_for_dev_replace;
  3565. u64 nocow_ctx_logical;
  3566. u64 len = nocow_ctx->len;
  3567. unsigned long index;
  3568. int srcu_index;
  3569. int ret = 0;
  3570. int err = 0;
  3571. key.objectid = root;
  3572. key.type = BTRFS_ROOT_ITEM_KEY;
  3573. key.offset = (u64)-1;
  3574. srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
  3575. local_root = btrfs_read_fs_root_no_name(fs_info, &key);
  3576. if (IS_ERR(local_root)) {
  3577. srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
  3578. return PTR_ERR(local_root);
  3579. }
  3580. key.type = BTRFS_INODE_ITEM_KEY;
  3581. key.objectid = inum;
  3582. key.offset = 0;
  3583. inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
  3584. srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
  3585. if (IS_ERR(inode))
  3586. return PTR_ERR(inode);
  3587. /* Avoid truncate/dio/punch hole.. */
  3588. mutex_lock(&inode->i_mutex);
  3589. inode_dio_wait(inode);
  3590. physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
  3591. io_tree = &BTRFS_I(inode)->io_tree;
  3592. nocow_ctx_logical = nocow_ctx->logical;
  3593. ret = check_extent_to_block(inode, offset, len, nocow_ctx_logical);
  3594. if (ret) {
  3595. ret = ret > 0 ? 0 : ret;
  3596. goto out;
  3597. }
  3598. while (len >= PAGE_CACHE_SIZE) {
  3599. index = offset >> PAGE_CACHE_SHIFT;
  3600. again:
  3601. page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
  3602. if (!page) {
  3603. btrfs_err(fs_info, "find_or_create_page() failed");
  3604. ret = -ENOMEM;
  3605. goto out;
  3606. }
  3607. if (PageUptodate(page)) {
  3608. if (PageDirty(page))
  3609. goto next_page;
  3610. } else {
  3611. ClearPageError(page);
  3612. err = extent_read_full_page(io_tree, page,
  3613. btrfs_get_extent,
  3614. nocow_ctx->mirror_num);
  3615. if (err) {
  3616. ret = err;
  3617. goto next_page;
  3618. }
  3619. lock_page(page);
  3620. /*
  3621. * If the page has been remove from the page cache,
  3622. * the data on it is meaningless, because it may be
  3623. * old one, the new data may be written into the new
  3624. * page in the page cache.
  3625. */
  3626. if (page->mapping != inode->i_mapping) {
  3627. unlock_page(page);
  3628. page_cache_release(page);
  3629. goto again;
  3630. }
  3631. if (!PageUptodate(page)) {
  3632. ret = -EIO;
  3633. goto next_page;
  3634. }
  3635. }
  3636. ret = check_extent_to_block(inode, offset, len,
  3637. nocow_ctx_logical);
  3638. if (ret) {
  3639. ret = ret > 0 ? 0 : ret;
  3640. goto next_page;
  3641. }
  3642. err = write_page_nocow(nocow_ctx->sctx,
  3643. physical_for_dev_replace, page);
  3644. if (err)
  3645. ret = err;
  3646. next_page:
  3647. unlock_page(page);
  3648. page_cache_release(page);
  3649. if (ret)
  3650. break;
  3651. offset += PAGE_CACHE_SIZE;
  3652. physical_for_dev_replace += PAGE_CACHE_SIZE;
  3653. nocow_ctx_logical += PAGE_CACHE_SIZE;
  3654. len -= PAGE_CACHE_SIZE;
  3655. }
  3656. ret = COPY_COMPLETE;
  3657. out:
  3658. mutex_unlock(&inode->i_mutex);
  3659. iput(inode);
  3660. return ret;
  3661. }
  3662. static int write_page_nocow(struct scrub_ctx *sctx,
  3663. u64 physical_for_dev_replace, struct page *page)
  3664. {
  3665. struct bio *bio;
  3666. struct btrfs_device *dev;
  3667. int ret;
  3668. dev = sctx->wr_ctx.tgtdev;
  3669. if (!dev)
  3670. return -EIO;
  3671. if (!dev->bdev) {
  3672. printk_ratelimited(KERN_WARNING
  3673. "BTRFS: scrub write_page_nocow(bdev == NULL) is unexpected!\n");
  3674. return -EIO;
  3675. }
  3676. bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
  3677. if (!bio) {
  3678. spin_lock(&sctx->stat_lock);
  3679. sctx->stat.malloc_errors++;
  3680. spin_unlock(&sctx->stat_lock);
  3681. return -ENOMEM;
  3682. }
  3683. bio->bi_iter.bi_size = 0;
  3684. bio->bi_iter.bi_sector = physical_for_dev_replace >> 9;
  3685. bio->bi_bdev = dev->bdev;
  3686. ret = bio_add_page(bio, page, PAGE_CACHE_SIZE, 0);
  3687. if (ret != PAGE_CACHE_SIZE) {
  3688. leave_with_eio:
  3689. bio_put(bio);
  3690. btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
  3691. return -EIO;
  3692. }
  3693. if (btrfsic_submit_bio_wait(WRITE_SYNC, bio))
  3694. goto leave_with_eio;
  3695. bio_put(bio);
  3696. return 0;
  3697. }