scrub.c 93 KB

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