scrub.c 91 KB

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