scrub.c 89 KB

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