scrub.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527
  1. /*
  2. * Copyright (C) 2011 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 "check-integrity.h"
  28. #include "rcu-string.h"
  29. /*
  30. * This is only the first step towards a full-features scrub. It reads all
  31. * extent and super block and verifies the checksums. In case a bad checksum
  32. * is found or the extent cannot be read, good data will be written back if
  33. * any can be found.
  34. *
  35. * Future enhancements:
  36. * - In case an unrepairable extent is encountered, track which files are
  37. * affected and report them
  38. * - track and record media errors, throw out bad devices
  39. * - add a mode to also read unallocated space
  40. */
  41. struct scrub_block;
  42. struct scrub_ctx;
  43. #define SCRUB_PAGES_PER_BIO 16 /* 64k per bio */
  44. #define SCRUB_BIOS_PER_CTX 16 /* 1 MB per device in flight */
  45. /*
  46. * the following value times PAGE_SIZE needs to be large enough to match the
  47. * largest node/leaf/sector size that shall be supported.
  48. * Values larger than BTRFS_STRIPE_LEN are not supported.
  49. */
  50. #define SCRUB_MAX_PAGES_PER_BLOCK 16 /* 64k per node/leaf/sector */
  51. struct scrub_page {
  52. struct scrub_block *sblock;
  53. struct page *page;
  54. struct btrfs_device *dev;
  55. u64 flags; /* extent flags */
  56. u64 generation;
  57. u64 logical;
  58. u64 physical;
  59. atomic_t ref_count;
  60. struct {
  61. unsigned int mirror_num:8;
  62. unsigned int have_csum:1;
  63. unsigned int io_error:1;
  64. };
  65. u8 csum[BTRFS_CSUM_SIZE];
  66. };
  67. struct scrub_bio {
  68. int index;
  69. struct scrub_ctx *sctx;
  70. struct btrfs_device *dev;
  71. struct bio *bio;
  72. int err;
  73. u64 logical;
  74. u64 physical;
  75. struct scrub_page *pagev[SCRUB_PAGES_PER_BIO];
  76. int page_count;
  77. int next_free;
  78. struct btrfs_work work;
  79. };
  80. struct scrub_block {
  81. struct scrub_page *pagev[SCRUB_MAX_PAGES_PER_BLOCK];
  82. int page_count;
  83. atomic_t outstanding_pages;
  84. atomic_t ref_count; /* free mem on transition to zero */
  85. struct scrub_ctx *sctx;
  86. struct {
  87. unsigned int header_error:1;
  88. unsigned int checksum_error:1;
  89. unsigned int no_io_error_seen:1;
  90. unsigned int generation_error:1; /* also sets header_error */
  91. };
  92. };
  93. struct scrub_ctx {
  94. struct scrub_bio *bios[SCRUB_BIOS_PER_CTX];
  95. struct btrfs_root *dev_root;
  96. int first_free;
  97. int curr;
  98. atomic_t in_flight;
  99. atomic_t fixup_cnt;
  100. spinlock_t list_lock;
  101. wait_queue_head_t list_wait;
  102. u16 csum_size;
  103. struct list_head csum_list;
  104. atomic_t cancel_req;
  105. int readonly;
  106. int pages_per_bio; /* <= SCRUB_PAGES_PER_BIO */
  107. u32 sectorsize;
  108. u32 nodesize;
  109. u32 leafsize;
  110. /*
  111. * statistics
  112. */
  113. struct btrfs_scrub_progress stat;
  114. spinlock_t stat_lock;
  115. };
  116. struct scrub_fixup_nodatasum {
  117. struct scrub_ctx *sctx;
  118. struct btrfs_device *dev;
  119. u64 logical;
  120. struct btrfs_root *root;
  121. struct btrfs_work work;
  122. int mirror_num;
  123. };
  124. struct scrub_warning {
  125. struct btrfs_path *path;
  126. u64 extent_item_size;
  127. char *scratch_buf;
  128. char *msg_buf;
  129. const char *errstr;
  130. sector_t sector;
  131. u64 logical;
  132. struct btrfs_device *dev;
  133. int msg_bufsize;
  134. int scratch_bufsize;
  135. };
  136. static int scrub_handle_errored_block(struct scrub_block *sblock_to_check);
  137. static int scrub_setup_recheck_block(struct scrub_ctx *sctx,
  138. struct btrfs_mapping_tree *map_tree,
  139. u64 length, u64 logical,
  140. struct scrub_block *sblock);
  141. static int scrub_recheck_block(struct btrfs_fs_info *fs_info,
  142. struct scrub_block *sblock, int is_metadata,
  143. int have_csum, u8 *csum, u64 generation,
  144. u16 csum_size);
  145. static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
  146. struct scrub_block *sblock,
  147. int is_metadata, int have_csum,
  148. const u8 *csum, u64 generation,
  149. u16 csum_size);
  150. static void scrub_complete_bio_end_io(struct bio *bio, int err);
  151. static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
  152. struct scrub_block *sblock_good,
  153. int force_write);
  154. static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
  155. struct scrub_block *sblock_good,
  156. int page_num, int force_write);
  157. static int scrub_checksum_data(struct scrub_block *sblock);
  158. static int scrub_checksum_tree_block(struct scrub_block *sblock);
  159. static int scrub_checksum_super(struct scrub_block *sblock);
  160. static void scrub_block_get(struct scrub_block *sblock);
  161. static void scrub_block_put(struct scrub_block *sblock);
  162. static void scrub_page_get(struct scrub_page *spage);
  163. static void scrub_page_put(struct scrub_page *spage);
  164. static int scrub_add_page_to_bio(struct scrub_ctx *sctx,
  165. struct scrub_page *spage);
  166. static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
  167. u64 physical, struct btrfs_device *dev, u64 flags,
  168. u64 gen, int mirror_num, u8 *csum, int force);
  169. static void scrub_bio_end_io(struct bio *bio, int err);
  170. static void scrub_bio_end_io_worker(struct btrfs_work *work);
  171. static void scrub_block_complete(struct scrub_block *sblock);
  172. static void scrub_free_csums(struct scrub_ctx *sctx)
  173. {
  174. while (!list_empty(&sctx->csum_list)) {
  175. struct btrfs_ordered_sum *sum;
  176. sum = list_first_entry(&sctx->csum_list,
  177. struct btrfs_ordered_sum, list);
  178. list_del(&sum->list);
  179. kfree(sum);
  180. }
  181. }
  182. static noinline_for_stack void scrub_free_ctx(struct scrub_ctx *sctx)
  183. {
  184. int i;
  185. if (!sctx)
  186. return;
  187. /* this can happen when scrub is cancelled */
  188. if (sctx->curr != -1) {
  189. struct scrub_bio *sbio = sctx->bios[sctx->curr];
  190. for (i = 0; i < sbio->page_count; i++) {
  191. BUG_ON(!sbio->pagev[i]);
  192. BUG_ON(!sbio->pagev[i]->page);
  193. scrub_block_put(sbio->pagev[i]->sblock);
  194. }
  195. bio_put(sbio->bio);
  196. }
  197. for (i = 0; i < SCRUB_BIOS_PER_CTX; ++i) {
  198. struct scrub_bio *sbio = sctx->bios[i];
  199. if (!sbio)
  200. break;
  201. kfree(sbio);
  202. }
  203. scrub_free_csums(sctx);
  204. kfree(sctx);
  205. }
  206. static noinline_for_stack
  207. struct scrub_ctx *scrub_setup_ctx(struct btrfs_device *dev)
  208. {
  209. struct scrub_ctx *sctx;
  210. int i;
  211. struct btrfs_fs_info *fs_info = dev->dev_root->fs_info;
  212. int pages_per_bio;
  213. pages_per_bio = min_t(int, SCRUB_PAGES_PER_BIO,
  214. bio_get_nr_vecs(dev->bdev));
  215. sctx = kzalloc(sizeof(*sctx), GFP_NOFS);
  216. if (!sctx)
  217. goto nomem;
  218. sctx->pages_per_bio = pages_per_bio;
  219. sctx->curr = -1;
  220. sctx->dev_root = dev->dev_root;
  221. for (i = 0; i < SCRUB_BIOS_PER_CTX; ++i) {
  222. struct scrub_bio *sbio;
  223. sbio = kzalloc(sizeof(*sbio), GFP_NOFS);
  224. if (!sbio)
  225. goto nomem;
  226. sctx->bios[i] = sbio;
  227. sbio->index = i;
  228. sbio->sctx = sctx;
  229. sbio->page_count = 0;
  230. sbio->work.func = scrub_bio_end_io_worker;
  231. if (i != SCRUB_BIOS_PER_CTX - 1)
  232. sctx->bios[i]->next_free = i + 1;
  233. else
  234. sctx->bios[i]->next_free = -1;
  235. }
  236. sctx->first_free = 0;
  237. sctx->nodesize = dev->dev_root->nodesize;
  238. sctx->leafsize = dev->dev_root->leafsize;
  239. sctx->sectorsize = dev->dev_root->sectorsize;
  240. atomic_set(&sctx->in_flight, 0);
  241. atomic_set(&sctx->fixup_cnt, 0);
  242. atomic_set(&sctx->cancel_req, 0);
  243. sctx->csum_size = btrfs_super_csum_size(fs_info->super_copy);
  244. INIT_LIST_HEAD(&sctx->csum_list);
  245. spin_lock_init(&sctx->list_lock);
  246. spin_lock_init(&sctx->stat_lock);
  247. init_waitqueue_head(&sctx->list_wait);
  248. return sctx;
  249. nomem:
  250. scrub_free_ctx(sctx);
  251. return ERR_PTR(-ENOMEM);
  252. }
  253. static int scrub_print_warning_inode(u64 inum, u64 offset, u64 root, void *ctx)
  254. {
  255. u64 isize;
  256. u32 nlink;
  257. int ret;
  258. int i;
  259. struct extent_buffer *eb;
  260. struct btrfs_inode_item *inode_item;
  261. struct scrub_warning *swarn = ctx;
  262. struct btrfs_fs_info *fs_info = swarn->dev->dev_root->fs_info;
  263. struct inode_fs_paths *ipath = NULL;
  264. struct btrfs_root *local_root;
  265. struct btrfs_key root_key;
  266. root_key.objectid = root;
  267. root_key.type = BTRFS_ROOT_ITEM_KEY;
  268. root_key.offset = (u64)-1;
  269. local_root = btrfs_read_fs_root_no_name(fs_info, &root_key);
  270. if (IS_ERR(local_root)) {
  271. ret = PTR_ERR(local_root);
  272. goto err;
  273. }
  274. ret = inode_item_info(inum, 0, local_root, swarn->path);
  275. if (ret) {
  276. btrfs_release_path(swarn->path);
  277. goto err;
  278. }
  279. eb = swarn->path->nodes[0];
  280. inode_item = btrfs_item_ptr(eb, swarn->path->slots[0],
  281. struct btrfs_inode_item);
  282. isize = btrfs_inode_size(eb, inode_item);
  283. nlink = btrfs_inode_nlink(eb, inode_item);
  284. btrfs_release_path(swarn->path);
  285. ipath = init_ipath(4096, local_root, swarn->path);
  286. if (IS_ERR(ipath)) {
  287. ret = PTR_ERR(ipath);
  288. ipath = NULL;
  289. goto err;
  290. }
  291. ret = paths_from_inode(inum, ipath);
  292. if (ret < 0)
  293. goto err;
  294. /*
  295. * we deliberately ignore the bit ipath might have been too small to
  296. * hold all of the paths here
  297. */
  298. for (i = 0; i < ipath->fspath->elem_cnt; ++i)
  299. printk_in_rcu(KERN_WARNING "btrfs: %s at logical %llu on dev "
  300. "%s, sector %llu, root %llu, inode %llu, offset %llu, "
  301. "length %llu, links %u (path: %s)\n", swarn->errstr,
  302. swarn->logical, rcu_str_deref(swarn->dev->name),
  303. (unsigned long long)swarn->sector, root, inum, offset,
  304. min(isize - offset, (u64)PAGE_SIZE), nlink,
  305. (char *)(unsigned long)ipath->fspath->val[i]);
  306. free_ipath(ipath);
  307. return 0;
  308. err:
  309. printk_in_rcu(KERN_WARNING "btrfs: %s at logical %llu on dev "
  310. "%s, sector %llu, root %llu, inode %llu, offset %llu: path "
  311. "resolving failed with ret=%d\n", swarn->errstr,
  312. swarn->logical, rcu_str_deref(swarn->dev->name),
  313. (unsigned long long)swarn->sector, root, inum, offset, ret);
  314. free_ipath(ipath);
  315. return 0;
  316. }
  317. static void scrub_print_warning(const char *errstr, struct scrub_block *sblock)
  318. {
  319. struct btrfs_device *dev;
  320. struct btrfs_fs_info *fs_info;
  321. struct btrfs_path *path;
  322. struct btrfs_key found_key;
  323. struct extent_buffer *eb;
  324. struct btrfs_extent_item *ei;
  325. struct scrub_warning swarn;
  326. unsigned long ptr = 0;
  327. u64 extent_item_pos;
  328. u64 flags = 0;
  329. u64 ref_root;
  330. u32 item_size;
  331. u8 ref_level;
  332. const int bufsize = 4096;
  333. int ret;
  334. WARN_ON(sblock->page_count < 1);
  335. dev = sblock->pagev[0]->dev;
  336. fs_info = sblock->sctx->dev_root->fs_info;
  337. path = btrfs_alloc_path();
  338. swarn.scratch_buf = kmalloc(bufsize, GFP_NOFS);
  339. swarn.msg_buf = kmalloc(bufsize, GFP_NOFS);
  340. swarn.sector = (sblock->pagev[0]->physical) >> 9;
  341. swarn.logical = sblock->pagev[0]->logical;
  342. swarn.errstr = errstr;
  343. swarn.dev = NULL;
  344. swarn.msg_bufsize = bufsize;
  345. swarn.scratch_bufsize = bufsize;
  346. if (!path || !swarn.scratch_buf || !swarn.msg_buf)
  347. goto out;
  348. ret = extent_from_logical(fs_info, swarn.logical, path, &found_key,
  349. &flags);
  350. if (ret < 0)
  351. goto out;
  352. extent_item_pos = swarn.logical - found_key.objectid;
  353. swarn.extent_item_size = found_key.offset;
  354. eb = path->nodes[0];
  355. ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
  356. item_size = btrfs_item_size_nr(eb, path->slots[0]);
  357. btrfs_release_path(path);
  358. if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  359. do {
  360. ret = tree_backref_for_extent(&ptr, eb, ei, item_size,
  361. &ref_root, &ref_level);
  362. printk_in_rcu(KERN_WARNING
  363. "btrfs: %s at logical %llu on dev %s, "
  364. "sector %llu: metadata %s (level %d) in tree "
  365. "%llu\n", errstr, swarn.logical,
  366. rcu_str_deref(dev->name),
  367. (unsigned long long)swarn.sector,
  368. ref_level ? "node" : "leaf",
  369. ret < 0 ? -1 : ref_level,
  370. ret < 0 ? -1 : ref_root);
  371. } while (ret != 1);
  372. } else {
  373. swarn.path = path;
  374. swarn.dev = dev;
  375. iterate_extent_inodes(fs_info, found_key.objectid,
  376. extent_item_pos, 1,
  377. scrub_print_warning_inode, &swarn);
  378. }
  379. out:
  380. btrfs_free_path(path);
  381. kfree(swarn.scratch_buf);
  382. kfree(swarn.msg_buf);
  383. }
  384. static int scrub_fixup_readpage(u64 inum, u64 offset, u64 root, void *ctx)
  385. {
  386. struct page *page = NULL;
  387. unsigned long index;
  388. struct scrub_fixup_nodatasum *fixup = ctx;
  389. int ret;
  390. int corrected = 0;
  391. struct btrfs_key key;
  392. struct inode *inode = NULL;
  393. u64 end = offset + PAGE_SIZE - 1;
  394. struct btrfs_root *local_root;
  395. key.objectid = root;
  396. key.type = BTRFS_ROOT_ITEM_KEY;
  397. key.offset = (u64)-1;
  398. local_root = btrfs_read_fs_root_no_name(fixup->root->fs_info, &key);
  399. if (IS_ERR(local_root))
  400. return PTR_ERR(local_root);
  401. key.type = BTRFS_INODE_ITEM_KEY;
  402. key.objectid = inum;
  403. key.offset = 0;
  404. inode = btrfs_iget(fixup->root->fs_info->sb, &key, local_root, NULL);
  405. if (IS_ERR(inode))
  406. return PTR_ERR(inode);
  407. index = offset >> PAGE_CACHE_SHIFT;
  408. page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
  409. if (!page) {
  410. ret = -ENOMEM;
  411. goto out;
  412. }
  413. if (PageUptodate(page)) {
  414. struct btrfs_mapping_tree *map_tree;
  415. if (PageDirty(page)) {
  416. /*
  417. * we need to write the data to the defect sector. the
  418. * data that was in that sector is not in memory,
  419. * because the page was modified. we must not write the
  420. * modified page to that sector.
  421. *
  422. * TODO: what could be done here: wait for the delalloc
  423. * runner to write out that page (might involve
  424. * COW) and see whether the sector is still
  425. * referenced afterwards.
  426. *
  427. * For the meantime, we'll treat this error
  428. * incorrectable, although there is a chance that a
  429. * later scrub will find the bad sector again and that
  430. * there's no dirty page in memory, then.
  431. */
  432. ret = -EIO;
  433. goto out;
  434. }
  435. map_tree = &BTRFS_I(inode)->root->fs_info->mapping_tree;
  436. ret = repair_io_failure(map_tree, offset, PAGE_SIZE,
  437. fixup->logical, page,
  438. fixup->mirror_num);
  439. unlock_page(page);
  440. corrected = !ret;
  441. } else {
  442. /*
  443. * we need to get good data first. the general readpage path
  444. * will call repair_io_failure for us, we just have to make
  445. * sure we read the bad mirror.
  446. */
  447. ret = set_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
  448. EXTENT_DAMAGED, GFP_NOFS);
  449. if (ret) {
  450. /* set_extent_bits should give proper error */
  451. WARN_ON(ret > 0);
  452. if (ret > 0)
  453. ret = -EFAULT;
  454. goto out;
  455. }
  456. ret = extent_read_full_page(&BTRFS_I(inode)->io_tree, page,
  457. btrfs_get_extent,
  458. fixup->mirror_num);
  459. wait_on_page_locked(page);
  460. corrected = !test_range_bit(&BTRFS_I(inode)->io_tree, offset,
  461. end, EXTENT_DAMAGED, 0, NULL);
  462. if (!corrected)
  463. clear_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
  464. EXTENT_DAMAGED, GFP_NOFS);
  465. }
  466. out:
  467. if (page)
  468. put_page(page);
  469. if (inode)
  470. iput(inode);
  471. if (ret < 0)
  472. return ret;
  473. if (ret == 0 && corrected) {
  474. /*
  475. * we only need to call readpage for one of the inodes belonging
  476. * to this extent. so make iterate_extent_inodes stop
  477. */
  478. return 1;
  479. }
  480. return -EIO;
  481. }
  482. static void scrub_fixup_nodatasum(struct btrfs_work *work)
  483. {
  484. int ret;
  485. struct scrub_fixup_nodatasum *fixup;
  486. struct scrub_ctx *sctx;
  487. struct btrfs_trans_handle *trans = NULL;
  488. struct btrfs_fs_info *fs_info;
  489. struct btrfs_path *path;
  490. int uncorrectable = 0;
  491. fixup = container_of(work, struct scrub_fixup_nodatasum, work);
  492. sctx = fixup->sctx;
  493. fs_info = fixup->root->fs_info;
  494. path = btrfs_alloc_path();
  495. if (!path) {
  496. spin_lock(&sctx->stat_lock);
  497. ++sctx->stat.malloc_errors;
  498. spin_unlock(&sctx->stat_lock);
  499. uncorrectable = 1;
  500. goto out;
  501. }
  502. trans = btrfs_join_transaction(fixup->root);
  503. if (IS_ERR(trans)) {
  504. uncorrectable = 1;
  505. goto out;
  506. }
  507. /*
  508. * the idea is to trigger a regular read through the standard path. we
  509. * read a page from the (failed) logical address by specifying the
  510. * corresponding copynum of the failed sector. thus, that readpage is
  511. * expected to fail.
  512. * that is the point where on-the-fly error correction will kick in
  513. * (once it's finished) and rewrite the failed sector if a good copy
  514. * can be found.
  515. */
  516. ret = iterate_inodes_from_logical(fixup->logical, fixup->root->fs_info,
  517. path, scrub_fixup_readpage,
  518. fixup);
  519. if (ret < 0) {
  520. uncorrectable = 1;
  521. goto out;
  522. }
  523. WARN_ON(ret != 1);
  524. spin_lock(&sctx->stat_lock);
  525. ++sctx->stat.corrected_errors;
  526. spin_unlock(&sctx->stat_lock);
  527. out:
  528. if (trans && !IS_ERR(trans))
  529. btrfs_end_transaction(trans, fixup->root);
  530. if (uncorrectable) {
  531. spin_lock(&sctx->stat_lock);
  532. ++sctx->stat.uncorrectable_errors;
  533. spin_unlock(&sctx->stat_lock);
  534. printk_ratelimited_in_rcu(KERN_ERR
  535. "btrfs: unable to fixup (nodatasum) error at logical %llu on dev %s\n",
  536. (unsigned long long)fixup->logical,
  537. rcu_str_deref(fixup->dev->name));
  538. }
  539. btrfs_free_path(path);
  540. kfree(fixup);
  541. /* see caller why we're pretending to be paused in the scrub counters */
  542. mutex_lock(&fs_info->scrub_lock);
  543. atomic_dec(&fs_info->scrubs_running);
  544. atomic_dec(&fs_info->scrubs_paused);
  545. mutex_unlock(&fs_info->scrub_lock);
  546. atomic_dec(&sctx->fixup_cnt);
  547. wake_up(&fs_info->scrub_pause_wait);
  548. wake_up(&sctx->list_wait);
  549. }
  550. /*
  551. * scrub_handle_errored_block gets called when either verification of the
  552. * pages failed or the bio failed to read, e.g. with EIO. In the latter
  553. * case, this function handles all pages in the bio, even though only one
  554. * may be bad.
  555. * The goal of this function is to repair the errored block by using the
  556. * contents of one of the mirrors.
  557. */
  558. static int scrub_handle_errored_block(struct scrub_block *sblock_to_check)
  559. {
  560. struct scrub_ctx *sctx = sblock_to_check->sctx;
  561. struct btrfs_device *dev;
  562. struct btrfs_fs_info *fs_info;
  563. u64 length;
  564. u64 logical;
  565. u64 generation;
  566. unsigned int failed_mirror_index;
  567. unsigned int is_metadata;
  568. unsigned int have_csum;
  569. u8 *csum;
  570. struct scrub_block *sblocks_for_recheck; /* holds one for each mirror */
  571. struct scrub_block *sblock_bad;
  572. int ret;
  573. int mirror_index;
  574. int page_num;
  575. int success;
  576. static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
  577. DEFAULT_RATELIMIT_BURST);
  578. BUG_ON(sblock_to_check->page_count < 1);
  579. fs_info = sctx->dev_root->fs_info;
  580. length = sblock_to_check->page_count * PAGE_SIZE;
  581. logical = sblock_to_check->pagev[0]->logical;
  582. generation = sblock_to_check->pagev[0]->generation;
  583. BUG_ON(sblock_to_check->pagev[0]->mirror_num < 1);
  584. failed_mirror_index = sblock_to_check->pagev[0]->mirror_num - 1;
  585. is_metadata = !(sblock_to_check->pagev[0]->flags &
  586. BTRFS_EXTENT_FLAG_DATA);
  587. have_csum = sblock_to_check->pagev[0]->have_csum;
  588. csum = sblock_to_check->pagev[0]->csum;
  589. dev = sblock_to_check->pagev[0]->dev;
  590. /*
  591. * read all mirrors one after the other. This includes to
  592. * re-read the extent or metadata block that failed (that was
  593. * the cause that this fixup code is called) another time,
  594. * page by page this time in order to know which pages
  595. * caused I/O errors and which ones are good (for all mirrors).
  596. * It is the goal to handle the situation when more than one
  597. * mirror contains I/O errors, but the errors do not
  598. * overlap, i.e. the data can be repaired by selecting the
  599. * pages from those mirrors without I/O error on the
  600. * particular pages. One example (with blocks >= 2 * PAGE_SIZE)
  601. * would be that mirror #1 has an I/O error on the first page,
  602. * the second page is good, and mirror #2 has an I/O error on
  603. * the second page, but the first page is good.
  604. * Then the first page of the first mirror can be repaired by
  605. * taking the first page of the second mirror, and the
  606. * second page of the second mirror can be repaired by
  607. * copying the contents of the 2nd page of the 1st mirror.
  608. * One more note: if the pages of one mirror contain I/O
  609. * errors, the checksum cannot be verified. In order to get
  610. * the best data for repairing, the first attempt is to find
  611. * a mirror without I/O errors and with a validated checksum.
  612. * Only if this is not possible, the pages are picked from
  613. * mirrors with I/O errors without considering the checksum.
  614. * If the latter is the case, at the end, the checksum of the
  615. * repaired area is verified in order to correctly maintain
  616. * the statistics.
  617. */
  618. sblocks_for_recheck = kzalloc(BTRFS_MAX_MIRRORS *
  619. sizeof(*sblocks_for_recheck),
  620. GFP_NOFS);
  621. if (!sblocks_for_recheck) {
  622. spin_lock(&sctx->stat_lock);
  623. sctx->stat.malloc_errors++;
  624. sctx->stat.read_errors++;
  625. sctx->stat.uncorrectable_errors++;
  626. spin_unlock(&sctx->stat_lock);
  627. btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
  628. goto out;
  629. }
  630. /* setup the context, map the logical blocks and alloc the pages */
  631. ret = scrub_setup_recheck_block(sctx, &fs_info->mapping_tree, length,
  632. logical, sblocks_for_recheck);
  633. if (ret) {
  634. spin_lock(&sctx->stat_lock);
  635. sctx->stat.read_errors++;
  636. sctx->stat.uncorrectable_errors++;
  637. spin_unlock(&sctx->stat_lock);
  638. btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
  639. goto out;
  640. }
  641. BUG_ON(failed_mirror_index >= BTRFS_MAX_MIRRORS);
  642. sblock_bad = sblocks_for_recheck + failed_mirror_index;
  643. /* build and submit the bios for the failed mirror, check checksums */
  644. ret = scrub_recheck_block(fs_info, sblock_bad, is_metadata, have_csum,
  645. csum, generation, sctx->csum_size);
  646. if (ret) {
  647. spin_lock(&sctx->stat_lock);
  648. sctx->stat.read_errors++;
  649. sctx->stat.uncorrectable_errors++;
  650. spin_unlock(&sctx->stat_lock);
  651. btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
  652. goto out;
  653. }
  654. if (!sblock_bad->header_error && !sblock_bad->checksum_error &&
  655. sblock_bad->no_io_error_seen) {
  656. /*
  657. * the error disappeared after reading page by page, or
  658. * the area was part of a huge bio and other parts of the
  659. * bio caused I/O errors, or the block layer merged several
  660. * read requests into one and the error is caused by a
  661. * different bio (usually one of the two latter cases is
  662. * the cause)
  663. */
  664. spin_lock(&sctx->stat_lock);
  665. sctx->stat.unverified_errors++;
  666. spin_unlock(&sctx->stat_lock);
  667. goto out;
  668. }
  669. if (!sblock_bad->no_io_error_seen) {
  670. spin_lock(&sctx->stat_lock);
  671. sctx->stat.read_errors++;
  672. spin_unlock(&sctx->stat_lock);
  673. if (__ratelimit(&_rs))
  674. scrub_print_warning("i/o error", sblock_to_check);
  675. btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
  676. } else if (sblock_bad->checksum_error) {
  677. spin_lock(&sctx->stat_lock);
  678. sctx->stat.csum_errors++;
  679. spin_unlock(&sctx->stat_lock);
  680. if (__ratelimit(&_rs))
  681. scrub_print_warning("checksum error", sblock_to_check);
  682. btrfs_dev_stat_inc_and_print(dev,
  683. BTRFS_DEV_STAT_CORRUPTION_ERRS);
  684. } else if (sblock_bad->header_error) {
  685. spin_lock(&sctx->stat_lock);
  686. sctx->stat.verify_errors++;
  687. spin_unlock(&sctx->stat_lock);
  688. if (__ratelimit(&_rs))
  689. scrub_print_warning("checksum/header error",
  690. sblock_to_check);
  691. if (sblock_bad->generation_error)
  692. btrfs_dev_stat_inc_and_print(dev,
  693. BTRFS_DEV_STAT_GENERATION_ERRS);
  694. else
  695. btrfs_dev_stat_inc_and_print(dev,
  696. BTRFS_DEV_STAT_CORRUPTION_ERRS);
  697. }
  698. if (sctx->readonly)
  699. goto did_not_correct_error;
  700. if (!is_metadata && !have_csum) {
  701. struct scrub_fixup_nodatasum *fixup_nodatasum;
  702. /*
  703. * !is_metadata and !have_csum, this means that the data
  704. * might not be COW'ed, that it might be modified
  705. * concurrently. The general strategy to work on the
  706. * commit root does not help in the case when COW is not
  707. * used.
  708. */
  709. fixup_nodatasum = kzalloc(sizeof(*fixup_nodatasum), GFP_NOFS);
  710. if (!fixup_nodatasum)
  711. goto did_not_correct_error;
  712. fixup_nodatasum->sctx = sctx;
  713. fixup_nodatasum->dev = dev;
  714. fixup_nodatasum->logical = logical;
  715. fixup_nodatasum->root = fs_info->extent_root;
  716. fixup_nodatasum->mirror_num = failed_mirror_index + 1;
  717. /*
  718. * increment scrubs_running to prevent cancel requests from
  719. * completing as long as a fixup worker is running. we must also
  720. * increment scrubs_paused to prevent deadlocking on pause
  721. * requests used for transactions commits (as the worker uses a
  722. * transaction context). it is safe to regard the fixup worker
  723. * as paused for all matters practical. effectively, we only
  724. * avoid cancellation requests from completing.
  725. */
  726. mutex_lock(&fs_info->scrub_lock);
  727. atomic_inc(&fs_info->scrubs_running);
  728. atomic_inc(&fs_info->scrubs_paused);
  729. mutex_unlock(&fs_info->scrub_lock);
  730. atomic_inc(&sctx->fixup_cnt);
  731. fixup_nodatasum->work.func = scrub_fixup_nodatasum;
  732. btrfs_queue_worker(&fs_info->scrub_workers,
  733. &fixup_nodatasum->work);
  734. goto out;
  735. }
  736. /*
  737. * now build and submit the bios for the other mirrors, check
  738. * checksums
  739. */
  740. for (mirror_index = 0;
  741. mirror_index < BTRFS_MAX_MIRRORS &&
  742. sblocks_for_recheck[mirror_index].page_count > 0;
  743. mirror_index++) {
  744. if (mirror_index == failed_mirror_index)
  745. continue;
  746. /* build and submit the bios, check checksums */
  747. ret = scrub_recheck_block(fs_info,
  748. sblocks_for_recheck + mirror_index,
  749. is_metadata, have_csum, csum,
  750. generation, sctx->csum_size);
  751. if (ret)
  752. goto did_not_correct_error;
  753. }
  754. /*
  755. * first try to pick the mirror which is completely without I/O
  756. * errors and also does not have a checksum error.
  757. * If one is found, and if a checksum is present, the full block
  758. * that is known to contain an error is rewritten. Afterwards
  759. * the block is known to be corrected.
  760. * If a mirror is found which is completely correct, and no
  761. * checksum is present, only those pages are rewritten that had
  762. * an I/O error in the block to be repaired, since it cannot be
  763. * determined, which copy of the other pages is better (and it
  764. * could happen otherwise that a correct page would be
  765. * overwritten by a bad one).
  766. */
  767. for (mirror_index = 0;
  768. mirror_index < BTRFS_MAX_MIRRORS &&
  769. sblocks_for_recheck[mirror_index].page_count > 0;
  770. mirror_index++) {
  771. struct scrub_block *sblock_other = sblocks_for_recheck +
  772. mirror_index;
  773. if (!sblock_other->header_error &&
  774. !sblock_other->checksum_error &&
  775. sblock_other->no_io_error_seen) {
  776. int force_write = is_metadata || have_csum;
  777. ret = scrub_repair_block_from_good_copy(sblock_bad,
  778. sblock_other,
  779. force_write);
  780. if (0 == ret)
  781. goto corrected_error;
  782. }
  783. }
  784. /*
  785. * in case of I/O errors in the area that is supposed to be
  786. * repaired, continue by picking good copies of those pages.
  787. * Select the good pages from mirrors to rewrite bad pages from
  788. * the area to fix. Afterwards verify the checksum of the block
  789. * that is supposed to be repaired. This verification step is
  790. * only done for the purpose of statistic counting and for the
  791. * final scrub report, whether errors remain.
  792. * A perfect algorithm could make use of the checksum and try
  793. * all possible combinations of pages from the different mirrors
  794. * until the checksum verification succeeds. For example, when
  795. * the 2nd page of mirror #1 faces I/O errors, and the 2nd page
  796. * of mirror #2 is readable but the final checksum test fails,
  797. * then the 2nd page of mirror #3 could be tried, whether now
  798. * the final checksum succeedes. But this would be a rare
  799. * exception and is therefore not implemented. At least it is
  800. * avoided that the good copy is overwritten.
  801. * A more useful improvement would be to pick the sectors
  802. * without I/O error based on sector sizes (512 bytes on legacy
  803. * disks) instead of on PAGE_SIZE. Then maybe 512 byte of one
  804. * mirror could be repaired by taking 512 byte of a different
  805. * mirror, even if other 512 byte sectors in the same PAGE_SIZE
  806. * area are unreadable.
  807. */
  808. /* can only fix I/O errors from here on */
  809. if (sblock_bad->no_io_error_seen)
  810. goto did_not_correct_error;
  811. success = 1;
  812. for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
  813. struct scrub_page *page_bad = sblock_bad->pagev[page_num];
  814. if (!page_bad->io_error)
  815. continue;
  816. for (mirror_index = 0;
  817. mirror_index < BTRFS_MAX_MIRRORS &&
  818. sblocks_for_recheck[mirror_index].page_count > 0;
  819. mirror_index++) {
  820. struct scrub_block *sblock_other = sblocks_for_recheck +
  821. mirror_index;
  822. struct scrub_page *page_other = sblock_other->pagev[
  823. page_num];
  824. if (!page_other->io_error) {
  825. ret = scrub_repair_page_from_good_copy(
  826. sblock_bad, sblock_other, page_num, 0);
  827. if (0 == ret) {
  828. page_bad->io_error = 0;
  829. break; /* succeeded for this page */
  830. }
  831. }
  832. }
  833. if (page_bad->io_error) {
  834. /* did not find a mirror to copy the page from */
  835. success = 0;
  836. }
  837. }
  838. if (success) {
  839. if (is_metadata || have_csum) {
  840. /*
  841. * need to verify the checksum now that all
  842. * sectors on disk are repaired (the write
  843. * request for data to be repaired is on its way).
  844. * Just be lazy and use scrub_recheck_block()
  845. * which re-reads the data before the checksum
  846. * is verified, but most likely the data comes out
  847. * of the page cache.
  848. */
  849. ret = scrub_recheck_block(fs_info, sblock_bad,
  850. is_metadata, have_csum, csum,
  851. generation, sctx->csum_size);
  852. if (!ret && !sblock_bad->header_error &&
  853. !sblock_bad->checksum_error &&
  854. sblock_bad->no_io_error_seen)
  855. goto corrected_error;
  856. else
  857. goto did_not_correct_error;
  858. } else {
  859. corrected_error:
  860. spin_lock(&sctx->stat_lock);
  861. sctx->stat.corrected_errors++;
  862. spin_unlock(&sctx->stat_lock);
  863. printk_ratelimited_in_rcu(KERN_ERR
  864. "btrfs: fixed up error at logical %llu on dev %s\n",
  865. (unsigned long long)logical,
  866. rcu_str_deref(dev->name));
  867. }
  868. } else {
  869. did_not_correct_error:
  870. spin_lock(&sctx->stat_lock);
  871. sctx->stat.uncorrectable_errors++;
  872. spin_unlock(&sctx->stat_lock);
  873. printk_ratelimited_in_rcu(KERN_ERR
  874. "btrfs: unable to fixup (regular) error at logical %llu on dev %s\n",
  875. (unsigned long long)logical,
  876. rcu_str_deref(dev->name));
  877. }
  878. out:
  879. if (sblocks_for_recheck) {
  880. for (mirror_index = 0; mirror_index < BTRFS_MAX_MIRRORS;
  881. mirror_index++) {
  882. struct scrub_block *sblock = sblocks_for_recheck +
  883. mirror_index;
  884. int page_index;
  885. for (page_index = 0; page_index < sblock->page_count;
  886. page_index++) {
  887. sblock->pagev[page_index]->sblock = NULL;
  888. scrub_page_put(sblock->pagev[page_index]);
  889. }
  890. }
  891. kfree(sblocks_for_recheck);
  892. }
  893. return 0;
  894. }
  895. static int scrub_setup_recheck_block(struct scrub_ctx *sctx,
  896. struct btrfs_mapping_tree *map_tree,
  897. u64 length, u64 logical,
  898. struct scrub_block *sblocks_for_recheck)
  899. {
  900. int page_index;
  901. int mirror_index;
  902. int ret;
  903. /*
  904. * note: the two members ref_count and outstanding_pages
  905. * are not used (and not set) in the blocks that are used for
  906. * the recheck procedure
  907. */
  908. page_index = 0;
  909. while (length > 0) {
  910. u64 sublen = min_t(u64, length, PAGE_SIZE);
  911. u64 mapped_length = sublen;
  912. struct btrfs_bio *bbio = NULL;
  913. /*
  914. * with a length of PAGE_SIZE, each returned stripe
  915. * represents one mirror
  916. */
  917. ret = btrfs_map_block(map_tree, WRITE, logical, &mapped_length,
  918. &bbio, 0);
  919. if (ret || !bbio || mapped_length < sublen) {
  920. kfree(bbio);
  921. return -EIO;
  922. }
  923. BUG_ON(page_index >= SCRUB_PAGES_PER_BIO);
  924. for (mirror_index = 0; mirror_index < (int)bbio->num_stripes;
  925. mirror_index++) {
  926. struct scrub_block *sblock;
  927. struct scrub_page *page;
  928. if (mirror_index >= BTRFS_MAX_MIRRORS)
  929. continue;
  930. sblock = sblocks_for_recheck + mirror_index;
  931. sblock->sctx = sctx;
  932. page = kzalloc(sizeof(*page), GFP_NOFS);
  933. if (!page) {
  934. leave_nomem:
  935. spin_lock(&sctx->stat_lock);
  936. sctx->stat.malloc_errors++;
  937. spin_unlock(&sctx->stat_lock);
  938. kfree(bbio);
  939. return -ENOMEM;
  940. }
  941. scrub_page_get(page);
  942. sblock->pagev[page_index] = page;
  943. page->logical = logical;
  944. page->physical = bbio->stripes[mirror_index].physical;
  945. /* for missing devices, dev->bdev is NULL */
  946. page->dev = bbio->stripes[mirror_index].dev;
  947. page->mirror_num = mirror_index + 1;
  948. sblock->page_count++;
  949. page->page = alloc_page(GFP_NOFS);
  950. if (!page->page)
  951. goto leave_nomem;
  952. }
  953. kfree(bbio);
  954. length -= sublen;
  955. logical += sublen;
  956. page_index++;
  957. }
  958. return 0;
  959. }
  960. /*
  961. * this function will check the on disk data for checksum errors, header
  962. * errors and read I/O errors. If any I/O errors happen, the exact pages
  963. * which are errored are marked as being bad. The goal is to enable scrub
  964. * to take those pages that are not errored from all the mirrors so that
  965. * the pages that are errored in the just handled mirror can be repaired.
  966. */
  967. static int scrub_recheck_block(struct btrfs_fs_info *fs_info,
  968. struct scrub_block *sblock, int is_metadata,
  969. int have_csum, u8 *csum, u64 generation,
  970. u16 csum_size)
  971. {
  972. int page_num;
  973. sblock->no_io_error_seen = 1;
  974. sblock->header_error = 0;
  975. sblock->checksum_error = 0;
  976. for (page_num = 0; page_num < sblock->page_count; page_num++) {
  977. struct bio *bio;
  978. int ret;
  979. struct scrub_page *page = sblock->pagev[page_num];
  980. DECLARE_COMPLETION_ONSTACK(complete);
  981. if (page->dev->bdev == NULL) {
  982. page->io_error = 1;
  983. sblock->no_io_error_seen = 0;
  984. continue;
  985. }
  986. WARN_ON(!page->page);
  987. bio = bio_alloc(GFP_NOFS, 1);
  988. if (!bio)
  989. return -EIO;
  990. bio->bi_bdev = page->dev->bdev;
  991. bio->bi_sector = page->physical >> 9;
  992. bio->bi_end_io = scrub_complete_bio_end_io;
  993. bio->bi_private = &complete;
  994. ret = bio_add_page(bio, page->page, PAGE_SIZE, 0);
  995. if (PAGE_SIZE != ret) {
  996. bio_put(bio);
  997. return -EIO;
  998. }
  999. btrfsic_submit_bio(READ, bio);
  1000. /* this will also unplug the queue */
  1001. wait_for_completion(&complete);
  1002. page->io_error = !test_bit(BIO_UPTODATE, &bio->bi_flags);
  1003. if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
  1004. sblock->no_io_error_seen = 0;
  1005. bio_put(bio);
  1006. }
  1007. if (sblock->no_io_error_seen)
  1008. scrub_recheck_block_checksum(fs_info, sblock, is_metadata,
  1009. have_csum, csum, generation,
  1010. csum_size);
  1011. return 0;
  1012. }
  1013. static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
  1014. struct scrub_block *sblock,
  1015. int is_metadata, int have_csum,
  1016. const u8 *csum, u64 generation,
  1017. u16 csum_size)
  1018. {
  1019. int page_num;
  1020. u8 calculated_csum[BTRFS_CSUM_SIZE];
  1021. u32 crc = ~(u32)0;
  1022. struct btrfs_root *root = fs_info->extent_root;
  1023. void *mapped_buffer;
  1024. WARN_ON(!sblock->pagev[0]->page);
  1025. if (is_metadata) {
  1026. struct btrfs_header *h;
  1027. mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
  1028. h = (struct btrfs_header *)mapped_buffer;
  1029. if (sblock->pagev[0]->logical != le64_to_cpu(h->bytenr) ||
  1030. memcmp(h->fsid, fs_info->fsid, BTRFS_UUID_SIZE) ||
  1031. memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
  1032. BTRFS_UUID_SIZE)) {
  1033. sblock->header_error = 1;
  1034. } else if (generation != le64_to_cpu(h->generation)) {
  1035. sblock->header_error = 1;
  1036. sblock->generation_error = 1;
  1037. }
  1038. csum = h->csum;
  1039. } else {
  1040. if (!have_csum)
  1041. return;
  1042. mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
  1043. }
  1044. for (page_num = 0;;) {
  1045. if (page_num == 0 && is_metadata)
  1046. crc = btrfs_csum_data(root,
  1047. ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE,
  1048. crc, PAGE_SIZE - BTRFS_CSUM_SIZE);
  1049. else
  1050. crc = btrfs_csum_data(root, mapped_buffer, crc,
  1051. PAGE_SIZE);
  1052. kunmap_atomic(mapped_buffer);
  1053. page_num++;
  1054. if (page_num >= sblock->page_count)
  1055. break;
  1056. WARN_ON(!sblock->pagev[page_num]->page);
  1057. mapped_buffer = kmap_atomic(sblock->pagev[page_num]->page);
  1058. }
  1059. btrfs_csum_final(crc, calculated_csum);
  1060. if (memcmp(calculated_csum, csum, csum_size))
  1061. sblock->checksum_error = 1;
  1062. }
  1063. static void scrub_complete_bio_end_io(struct bio *bio, int err)
  1064. {
  1065. complete((struct completion *)bio->bi_private);
  1066. }
  1067. static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
  1068. struct scrub_block *sblock_good,
  1069. int force_write)
  1070. {
  1071. int page_num;
  1072. int ret = 0;
  1073. for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
  1074. int ret_sub;
  1075. ret_sub = scrub_repair_page_from_good_copy(sblock_bad,
  1076. sblock_good,
  1077. page_num,
  1078. force_write);
  1079. if (ret_sub)
  1080. ret = ret_sub;
  1081. }
  1082. return ret;
  1083. }
  1084. static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
  1085. struct scrub_block *sblock_good,
  1086. int page_num, int force_write)
  1087. {
  1088. struct scrub_page *page_bad = sblock_bad->pagev[page_num];
  1089. struct scrub_page *page_good = sblock_good->pagev[page_num];
  1090. BUG_ON(page_bad->page == NULL);
  1091. BUG_ON(page_good->page == NULL);
  1092. if (force_write || sblock_bad->header_error ||
  1093. sblock_bad->checksum_error || page_bad->io_error) {
  1094. struct bio *bio;
  1095. int ret;
  1096. DECLARE_COMPLETION_ONSTACK(complete);
  1097. bio = bio_alloc(GFP_NOFS, 1);
  1098. if (!bio)
  1099. return -EIO;
  1100. bio->bi_bdev = page_bad->dev->bdev;
  1101. bio->bi_sector = page_bad->physical >> 9;
  1102. bio->bi_end_io = scrub_complete_bio_end_io;
  1103. bio->bi_private = &complete;
  1104. ret = bio_add_page(bio, page_good->page, PAGE_SIZE, 0);
  1105. if (PAGE_SIZE != ret) {
  1106. bio_put(bio);
  1107. return -EIO;
  1108. }
  1109. btrfsic_submit_bio(WRITE, bio);
  1110. /* this will also unplug the queue */
  1111. wait_for_completion(&complete);
  1112. if (!bio_flagged(bio, BIO_UPTODATE)) {
  1113. btrfs_dev_stat_inc_and_print(page_bad->dev,
  1114. BTRFS_DEV_STAT_WRITE_ERRS);
  1115. bio_put(bio);
  1116. return -EIO;
  1117. }
  1118. bio_put(bio);
  1119. }
  1120. return 0;
  1121. }
  1122. static void scrub_checksum(struct scrub_block *sblock)
  1123. {
  1124. u64 flags;
  1125. int ret;
  1126. WARN_ON(sblock->page_count < 1);
  1127. flags = sblock->pagev[0]->flags;
  1128. ret = 0;
  1129. if (flags & BTRFS_EXTENT_FLAG_DATA)
  1130. ret = scrub_checksum_data(sblock);
  1131. else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
  1132. ret = scrub_checksum_tree_block(sblock);
  1133. else if (flags & BTRFS_EXTENT_FLAG_SUPER)
  1134. (void)scrub_checksum_super(sblock);
  1135. else
  1136. WARN_ON(1);
  1137. if (ret)
  1138. scrub_handle_errored_block(sblock);
  1139. }
  1140. static int scrub_checksum_data(struct scrub_block *sblock)
  1141. {
  1142. struct scrub_ctx *sctx = sblock->sctx;
  1143. u8 csum[BTRFS_CSUM_SIZE];
  1144. u8 *on_disk_csum;
  1145. struct page *page;
  1146. void *buffer;
  1147. u32 crc = ~(u32)0;
  1148. int fail = 0;
  1149. struct btrfs_root *root = sctx->dev_root;
  1150. u64 len;
  1151. int index;
  1152. BUG_ON(sblock->page_count < 1);
  1153. if (!sblock->pagev[0]->have_csum)
  1154. return 0;
  1155. on_disk_csum = sblock->pagev[0]->csum;
  1156. page = sblock->pagev[0]->page;
  1157. buffer = kmap_atomic(page);
  1158. len = sctx->sectorsize;
  1159. index = 0;
  1160. for (;;) {
  1161. u64 l = min_t(u64, len, PAGE_SIZE);
  1162. crc = btrfs_csum_data(root, buffer, crc, l);
  1163. kunmap_atomic(buffer);
  1164. len -= l;
  1165. if (len == 0)
  1166. break;
  1167. index++;
  1168. BUG_ON(index >= sblock->page_count);
  1169. BUG_ON(!sblock->pagev[index]->page);
  1170. page = sblock->pagev[index]->page;
  1171. buffer = kmap_atomic(page);
  1172. }
  1173. btrfs_csum_final(crc, csum);
  1174. if (memcmp(csum, on_disk_csum, sctx->csum_size))
  1175. fail = 1;
  1176. return fail;
  1177. }
  1178. static int scrub_checksum_tree_block(struct scrub_block *sblock)
  1179. {
  1180. struct scrub_ctx *sctx = sblock->sctx;
  1181. struct btrfs_header *h;
  1182. struct btrfs_root *root = sctx->dev_root;
  1183. struct btrfs_fs_info *fs_info = root->fs_info;
  1184. u8 calculated_csum[BTRFS_CSUM_SIZE];
  1185. u8 on_disk_csum[BTRFS_CSUM_SIZE];
  1186. struct page *page;
  1187. void *mapped_buffer;
  1188. u64 mapped_size;
  1189. void *p;
  1190. u32 crc = ~(u32)0;
  1191. int fail = 0;
  1192. int crc_fail = 0;
  1193. u64 len;
  1194. int index;
  1195. BUG_ON(sblock->page_count < 1);
  1196. page = sblock->pagev[0]->page;
  1197. mapped_buffer = kmap_atomic(page);
  1198. h = (struct btrfs_header *)mapped_buffer;
  1199. memcpy(on_disk_csum, h->csum, sctx->csum_size);
  1200. /*
  1201. * we don't use the getter functions here, as we
  1202. * a) don't have an extent buffer and
  1203. * b) the page is already kmapped
  1204. */
  1205. if (sblock->pagev[0]->logical != le64_to_cpu(h->bytenr))
  1206. ++fail;
  1207. if (sblock->pagev[0]->generation != le64_to_cpu(h->generation))
  1208. ++fail;
  1209. if (memcmp(h->fsid, fs_info->fsid, BTRFS_UUID_SIZE))
  1210. ++fail;
  1211. if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
  1212. BTRFS_UUID_SIZE))
  1213. ++fail;
  1214. BUG_ON(sctx->nodesize != sctx->leafsize);
  1215. len = sctx->nodesize - BTRFS_CSUM_SIZE;
  1216. mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
  1217. p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
  1218. index = 0;
  1219. for (;;) {
  1220. u64 l = min_t(u64, len, mapped_size);
  1221. crc = btrfs_csum_data(root, p, crc, l);
  1222. kunmap_atomic(mapped_buffer);
  1223. len -= l;
  1224. if (len == 0)
  1225. break;
  1226. index++;
  1227. BUG_ON(index >= sblock->page_count);
  1228. BUG_ON(!sblock->pagev[index]->page);
  1229. page = sblock->pagev[index]->page;
  1230. mapped_buffer = kmap_atomic(page);
  1231. mapped_size = PAGE_SIZE;
  1232. p = mapped_buffer;
  1233. }
  1234. btrfs_csum_final(crc, calculated_csum);
  1235. if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
  1236. ++crc_fail;
  1237. return fail || crc_fail;
  1238. }
  1239. static int scrub_checksum_super(struct scrub_block *sblock)
  1240. {
  1241. struct btrfs_super_block *s;
  1242. struct scrub_ctx *sctx = sblock->sctx;
  1243. struct btrfs_root *root = sctx->dev_root;
  1244. struct btrfs_fs_info *fs_info = root->fs_info;
  1245. u8 calculated_csum[BTRFS_CSUM_SIZE];
  1246. u8 on_disk_csum[BTRFS_CSUM_SIZE];
  1247. struct page *page;
  1248. void *mapped_buffer;
  1249. u64 mapped_size;
  1250. void *p;
  1251. u32 crc = ~(u32)0;
  1252. int fail_gen = 0;
  1253. int fail_cor = 0;
  1254. u64 len;
  1255. int index;
  1256. BUG_ON(sblock->page_count < 1);
  1257. page = sblock->pagev[0]->page;
  1258. mapped_buffer = kmap_atomic(page);
  1259. s = (struct btrfs_super_block *)mapped_buffer;
  1260. memcpy(on_disk_csum, s->csum, sctx->csum_size);
  1261. if (sblock->pagev[0]->logical != le64_to_cpu(s->bytenr))
  1262. ++fail_cor;
  1263. if (sblock->pagev[0]->generation != le64_to_cpu(s->generation))
  1264. ++fail_gen;
  1265. if (memcmp(s->fsid, fs_info->fsid, BTRFS_UUID_SIZE))
  1266. ++fail_cor;
  1267. len = BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE;
  1268. mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
  1269. p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
  1270. index = 0;
  1271. for (;;) {
  1272. u64 l = min_t(u64, len, mapped_size);
  1273. crc = btrfs_csum_data(root, p, crc, l);
  1274. kunmap_atomic(mapped_buffer);
  1275. len -= l;
  1276. if (len == 0)
  1277. break;
  1278. index++;
  1279. BUG_ON(index >= sblock->page_count);
  1280. BUG_ON(!sblock->pagev[index]->page);
  1281. page = sblock->pagev[index]->page;
  1282. mapped_buffer = kmap_atomic(page);
  1283. mapped_size = PAGE_SIZE;
  1284. p = mapped_buffer;
  1285. }
  1286. btrfs_csum_final(crc, calculated_csum);
  1287. if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
  1288. ++fail_cor;
  1289. if (fail_cor + fail_gen) {
  1290. /*
  1291. * if we find an error in a super block, we just report it.
  1292. * They will get written with the next transaction commit
  1293. * anyway
  1294. */
  1295. spin_lock(&sctx->stat_lock);
  1296. ++sctx->stat.super_errors;
  1297. spin_unlock(&sctx->stat_lock);
  1298. if (fail_cor)
  1299. btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
  1300. BTRFS_DEV_STAT_CORRUPTION_ERRS);
  1301. else
  1302. btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
  1303. BTRFS_DEV_STAT_GENERATION_ERRS);
  1304. }
  1305. return fail_cor + fail_gen;
  1306. }
  1307. static void scrub_block_get(struct scrub_block *sblock)
  1308. {
  1309. atomic_inc(&sblock->ref_count);
  1310. }
  1311. static void scrub_block_put(struct scrub_block *sblock)
  1312. {
  1313. if (atomic_dec_and_test(&sblock->ref_count)) {
  1314. int i;
  1315. for (i = 0; i < sblock->page_count; i++)
  1316. scrub_page_put(sblock->pagev[i]);
  1317. kfree(sblock);
  1318. }
  1319. }
  1320. static void scrub_page_get(struct scrub_page *spage)
  1321. {
  1322. atomic_inc(&spage->ref_count);
  1323. }
  1324. static void scrub_page_put(struct scrub_page *spage)
  1325. {
  1326. if (atomic_dec_and_test(&spage->ref_count)) {
  1327. if (spage->page)
  1328. __free_page(spage->page);
  1329. kfree(spage);
  1330. }
  1331. }
  1332. static void scrub_submit(struct scrub_ctx *sctx)
  1333. {
  1334. struct scrub_bio *sbio;
  1335. if (sctx->curr == -1)
  1336. return;
  1337. sbio = sctx->bios[sctx->curr];
  1338. sctx->curr = -1;
  1339. atomic_inc(&sctx->in_flight);
  1340. btrfsic_submit_bio(READ, sbio->bio);
  1341. }
  1342. static int scrub_add_page_to_bio(struct scrub_ctx *sctx,
  1343. struct scrub_page *spage)
  1344. {
  1345. struct scrub_block *sblock = spage->sblock;
  1346. struct scrub_bio *sbio;
  1347. int ret;
  1348. again:
  1349. /*
  1350. * grab a fresh bio or wait for one to become available
  1351. */
  1352. while (sctx->curr == -1) {
  1353. spin_lock(&sctx->list_lock);
  1354. sctx->curr = sctx->first_free;
  1355. if (sctx->curr != -1) {
  1356. sctx->first_free = sctx->bios[sctx->curr]->next_free;
  1357. sctx->bios[sctx->curr]->next_free = -1;
  1358. sctx->bios[sctx->curr]->page_count = 0;
  1359. spin_unlock(&sctx->list_lock);
  1360. } else {
  1361. spin_unlock(&sctx->list_lock);
  1362. wait_event(sctx->list_wait, sctx->first_free != -1);
  1363. }
  1364. }
  1365. sbio = sctx->bios[sctx->curr];
  1366. if (sbio->page_count == 0) {
  1367. struct bio *bio;
  1368. sbio->physical = spage->physical;
  1369. sbio->logical = spage->logical;
  1370. sbio->dev = spage->dev;
  1371. bio = sbio->bio;
  1372. if (!bio) {
  1373. bio = bio_alloc(GFP_NOFS, sctx->pages_per_bio);
  1374. if (!bio)
  1375. return -ENOMEM;
  1376. sbio->bio = bio;
  1377. }
  1378. bio->bi_private = sbio;
  1379. bio->bi_end_io = scrub_bio_end_io;
  1380. bio->bi_bdev = sbio->dev->bdev;
  1381. bio->bi_sector = sbio->physical >> 9;
  1382. sbio->err = 0;
  1383. } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
  1384. spage->physical ||
  1385. sbio->logical + sbio->page_count * PAGE_SIZE !=
  1386. spage->logical ||
  1387. sbio->dev != spage->dev) {
  1388. scrub_submit(sctx);
  1389. goto again;
  1390. }
  1391. sbio->pagev[sbio->page_count] = spage;
  1392. ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
  1393. if (ret != PAGE_SIZE) {
  1394. if (sbio->page_count < 1) {
  1395. bio_put(sbio->bio);
  1396. sbio->bio = NULL;
  1397. return -EIO;
  1398. }
  1399. scrub_submit(sctx);
  1400. goto again;
  1401. }
  1402. scrub_block_get(sblock); /* one for the added page */
  1403. atomic_inc(&sblock->outstanding_pages);
  1404. sbio->page_count++;
  1405. if (sbio->page_count == sctx->pages_per_bio)
  1406. scrub_submit(sctx);
  1407. return 0;
  1408. }
  1409. static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
  1410. u64 physical, struct btrfs_device *dev, u64 flags,
  1411. u64 gen, int mirror_num, u8 *csum, int force)
  1412. {
  1413. struct scrub_block *sblock;
  1414. int index;
  1415. sblock = kzalloc(sizeof(*sblock), GFP_NOFS);
  1416. if (!sblock) {
  1417. spin_lock(&sctx->stat_lock);
  1418. sctx->stat.malloc_errors++;
  1419. spin_unlock(&sctx->stat_lock);
  1420. return -ENOMEM;
  1421. }
  1422. /* one ref inside this function, plus one for each page added to
  1423. * a bio later on */
  1424. atomic_set(&sblock->ref_count, 1);
  1425. sblock->sctx = sctx;
  1426. sblock->no_io_error_seen = 1;
  1427. for (index = 0; len > 0; index++) {
  1428. struct scrub_page *spage;
  1429. u64 l = min_t(u64, len, PAGE_SIZE);
  1430. spage = kzalloc(sizeof(*spage), GFP_NOFS);
  1431. if (!spage) {
  1432. leave_nomem:
  1433. spin_lock(&sctx->stat_lock);
  1434. sctx->stat.malloc_errors++;
  1435. spin_unlock(&sctx->stat_lock);
  1436. scrub_block_put(sblock);
  1437. return -ENOMEM;
  1438. }
  1439. BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
  1440. scrub_page_get(spage);
  1441. sblock->pagev[index] = spage;
  1442. spage->sblock = sblock;
  1443. spage->dev = dev;
  1444. spage->flags = flags;
  1445. spage->generation = gen;
  1446. spage->logical = logical;
  1447. spage->physical = physical;
  1448. spage->mirror_num = mirror_num;
  1449. if (csum) {
  1450. spage->have_csum = 1;
  1451. memcpy(spage->csum, csum, sctx->csum_size);
  1452. } else {
  1453. spage->have_csum = 0;
  1454. }
  1455. sblock->page_count++;
  1456. spage->page = alloc_page(GFP_NOFS);
  1457. if (!spage->page)
  1458. goto leave_nomem;
  1459. len -= l;
  1460. logical += l;
  1461. physical += l;
  1462. }
  1463. WARN_ON(sblock->page_count == 0);
  1464. for (index = 0; index < sblock->page_count; index++) {
  1465. struct scrub_page *spage = sblock->pagev[index];
  1466. int ret;
  1467. ret = scrub_add_page_to_bio(sctx, spage);
  1468. if (ret) {
  1469. scrub_block_put(sblock);
  1470. return ret;
  1471. }
  1472. }
  1473. if (force)
  1474. scrub_submit(sctx);
  1475. /* last one frees, either here or in bio completion for last page */
  1476. scrub_block_put(sblock);
  1477. return 0;
  1478. }
  1479. static void scrub_bio_end_io(struct bio *bio, int err)
  1480. {
  1481. struct scrub_bio *sbio = bio->bi_private;
  1482. struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
  1483. sbio->err = err;
  1484. sbio->bio = bio;
  1485. btrfs_queue_worker(&fs_info->scrub_workers, &sbio->work);
  1486. }
  1487. static void scrub_bio_end_io_worker(struct btrfs_work *work)
  1488. {
  1489. struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
  1490. struct scrub_ctx *sctx = sbio->sctx;
  1491. int i;
  1492. BUG_ON(sbio->page_count > SCRUB_PAGES_PER_BIO);
  1493. if (sbio->err) {
  1494. for (i = 0; i < sbio->page_count; i++) {
  1495. struct scrub_page *spage = sbio->pagev[i];
  1496. spage->io_error = 1;
  1497. spage->sblock->no_io_error_seen = 0;
  1498. }
  1499. }
  1500. /* now complete the scrub_block items that have all pages completed */
  1501. for (i = 0; i < sbio->page_count; i++) {
  1502. struct scrub_page *spage = sbio->pagev[i];
  1503. struct scrub_block *sblock = spage->sblock;
  1504. if (atomic_dec_and_test(&sblock->outstanding_pages))
  1505. scrub_block_complete(sblock);
  1506. scrub_block_put(sblock);
  1507. }
  1508. bio_put(sbio->bio);
  1509. sbio->bio = NULL;
  1510. spin_lock(&sctx->list_lock);
  1511. sbio->next_free = sctx->first_free;
  1512. sctx->first_free = sbio->index;
  1513. spin_unlock(&sctx->list_lock);
  1514. atomic_dec(&sctx->in_flight);
  1515. wake_up(&sctx->list_wait);
  1516. }
  1517. static void scrub_block_complete(struct scrub_block *sblock)
  1518. {
  1519. if (!sblock->no_io_error_seen)
  1520. scrub_handle_errored_block(sblock);
  1521. else
  1522. scrub_checksum(sblock);
  1523. }
  1524. static int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u64 len,
  1525. u8 *csum)
  1526. {
  1527. struct btrfs_ordered_sum *sum = NULL;
  1528. int ret = 0;
  1529. unsigned long i;
  1530. unsigned long num_sectors;
  1531. while (!list_empty(&sctx->csum_list)) {
  1532. sum = list_first_entry(&sctx->csum_list,
  1533. struct btrfs_ordered_sum, list);
  1534. if (sum->bytenr > logical)
  1535. return 0;
  1536. if (sum->bytenr + sum->len > logical)
  1537. break;
  1538. ++sctx->stat.csum_discards;
  1539. list_del(&sum->list);
  1540. kfree(sum);
  1541. sum = NULL;
  1542. }
  1543. if (!sum)
  1544. return 0;
  1545. num_sectors = sum->len / sctx->sectorsize;
  1546. for (i = 0; i < num_sectors; ++i) {
  1547. if (sum->sums[i].bytenr == logical) {
  1548. memcpy(csum, &sum->sums[i].sum, sctx->csum_size);
  1549. ret = 1;
  1550. break;
  1551. }
  1552. }
  1553. if (ret && i == num_sectors - 1) {
  1554. list_del(&sum->list);
  1555. kfree(sum);
  1556. }
  1557. return ret;
  1558. }
  1559. /* scrub extent tries to collect up to 64 kB for each bio */
  1560. static int scrub_extent(struct scrub_ctx *sctx, u64 logical, u64 len,
  1561. u64 physical, struct btrfs_device *dev, u64 flags,
  1562. u64 gen, int mirror_num)
  1563. {
  1564. int ret;
  1565. u8 csum[BTRFS_CSUM_SIZE];
  1566. u32 blocksize;
  1567. if (flags & BTRFS_EXTENT_FLAG_DATA) {
  1568. blocksize = sctx->sectorsize;
  1569. spin_lock(&sctx->stat_lock);
  1570. sctx->stat.data_extents_scrubbed++;
  1571. sctx->stat.data_bytes_scrubbed += len;
  1572. spin_unlock(&sctx->stat_lock);
  1573. } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  1574. BUG_ON(sctx->nodesize != sctx->leafsize);
  1575. blocksize = sctx->nodesize;
  1576. spin_lock(&sctx->stat_lock);
  1577. sctx->stat.tree_extents_scrubbed++;
  1578. sctx->stat.tree_bytes_scrubbed += len;
  1579. spin_unlock(&sctx->stat_lock);
  1580. } else {
  1581. blocksize = sctx->sectorsize;
  1582. BUG_ON(1);
  1583. }
  1584. while (len) {
  1585. u64 l = min_t(u64, len, blocksize);
  1586. int have_csum = 0;
  1587. if (flags & BTRFS_EXTENT_FLAG_DATA) {
  1588. /* push csums to sbio */
  1589. have_csum = scrub_find_csum(sctx, logical, l, csum);
  1590. if (have_csum == 0)
  1591. ++sctx->stat.no_csum;
  1592. }
  1593. ret = scrub_pages(sctx, logical, l, physical, dev, flags, gen,
  1594. mirror_num, have_csum ? csum : NULL, 0);
  1595. if (ret)
  1596. return ret;
  1597. len -= l;
  1598. logical += l;
  1599. physical += l;
  1600. }
  1601. return 0;
  1602. }
  1603. static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
  1604. struct map_lookup *map,
  1605. struct btrfs_device *scrub_dev,
  1606. int num, u64 base, u64 length)
  1607. {
  1608. struct btrfs_path *path;
  1609. struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
  1610. struct btrfs_root *root = fs_info->extent_root;
  1611. struct btrfs_root *csum_root = fs_info->csum_root;
  1612. struct btrfs_extent_item *extent;
  1613. struct blk_plug plug;
  1614. u64 flags;
  1615. int ret;
  1616. int slot;
  1617. int i;
  1618. u64 nstripes;
  1619. struct extent_buffer *l;
  1620. struct btrfs_key key;
  1621. u64 physical;
  1622. u64 logical;
  1623. u64 generation;
  1624. int mirror_num;
  1625. struct reada_control *reada1;
  1626. struct reada_control *reada2;
  1627. struct btrfs_key key_start;
  1628. struct btrfs_key key_end;
  1629. u64 increment = map->stripe_len;
  1630. u64 offset;
  1631. nstripes = length;
  1632. offset = 0;
  1633. do_div(nstripes, map->stripe_len);
  1634. if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
  1635. offset = map->stripe_len * num;
  1636. increment = map->stripe_len * map->num_stripes;
  1637. mirror_num = 1;
  1638. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  1639. int factor = map->num_stripes / map->sub_stripes;
  1640. offset = map->stripe_len * (num / map->sub_stripes);
  1641. increment = map->stripe_len * factor;
  1642. mirror_num = num % map->sub_stripes + 1;
  1643. } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
  1644. increment = map->stripe_len;
  1645. mirror_num = num % map->num_stripes + 1;
  1646. } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
  1647. increment = map->stripe_len;
  1648. mirror_num = num % map->num_stripes + 1;
  1649. } else {
  1650. increment = map->stripe_len;
  1651. mirror_num = 1;
  1652. }
  1653. path = btrfs_alloc_path();
  1654. if (!path)
  1655. return -ENOMEM;
  1656. /*
  1657. * work on commit root. The related disk blocks are static as
  1658. * long as COW is applied. This means, it is save to rewrite
  1659. * them to repair disk errors without any race conditions
  1660. */
  1661. path->search_commit_root = 1;
  1662. path->skip_locking = 1;
  1663. /*
  1664. * trigger the readahead for extent tree csum tree and wait for
  1665. * completion. During readahead, the scrub is officially paused
  1666. * to not hold off transaction commits
  1667. */
  1668. logical = base + offset;
  1669. wait_event(sctx->list_wait,
  1670. atomic_read(&sctx->in_flight) == 0);
  1671. atomic_inc(&fs_info->scrubs_paused);
  1672. wake_up(&fs_info->scrub_pause_wait);
  1673. /* FIXME it might be better to start readahead at commit root */
  1674. key_start.objectid = logical;
  1675. key_start.type = BTRFS_EXTENT_ITEM_KEY;
  1676. key_start.offset = (u64)0;
  1677. key_end.objectid = base + offset + nstripes * increment;
  1678. key_end.type = BTRFS_EXTENT_ITEM_KEY;
  1679. key_end.offset = (u64)0;
  1680. reada1 = btrfs_reada_add(root, &key_start, &key_end);
  1681. key_start.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
  1682. key_start.type = BTRFS_EXTENT_CSUM_KEY;
  1683. key_start.offset = logical;
  1684. key_end.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
  1685. key_end.type = BTRFS_EXTENT_CSUM_KEY;
  1686. key_end.offset = base + offset + nstripes * increment;
  1687. reada2 = btrfs_reada_add(csum_root, &key_start, &key_end);
  1688. if (!IS_ERR(reada1))
  1689. btrfs_reada_wait(reada1);
  1690. if (!IS_ERR(reada2))
  1691. btrfs_reada_wait(reada2);
  1692. mutex_lock(&fs_info->scrub_lock);
  1693. while (atomic_read(&fs_info->scrub_pause_req)) {
  1694. mutex_unlock(&fs_info->scrub_lock);
  1695. wait_event(fs_info->scrub_pause_wait,
  1696. atomic_read(&fs_info->scrub_pause_req) == 0);
  1697. mutex_lock(&fs_info->scrub_lock);
  1698. }
  1699. atomic_dec(&fs_info->scrubs_paused);
  1700. mutex_unlock(&fs_info->scrub_lock);
  1701. wake_up(&fs_info->scrub_pause_wait);
  1702. /*
  1703. * collect all data csums for the stripe to avoid seeking during
  1704. * the scrub. This might currently (crc32) end up to be about 1MB
  1705. */
  1706. blk_start_plug(&plug);
  1707. /*
  1708. * now find all extents for each stripe and scrub them
  1709. */
  1710. logical = base + offset;
  1711. physical = map->stripes[num].physical;
  1712. ret = 0;
  1713. for (i = 0; i < nstripes; ++i) {
  1714. /*
  1715. * canceled?
  1716. */
  1717. if (atomic_read(&fs_info->scrub_cancel_req) ||
  1718. atomic_read(&sctx->cancel_req)) {
  1719. ret = -ECANCELED;
  1720. goto out;
  1721. }
  1722. /*
  1723. * check to see if we have to pause
  1724. */
  1725. if (atomic_read(&fs_info->scrub_pause_req)) {
  1726. /* push queued extents */
  1727. scrub_submit(sctx);
  1728. wait_event(sctx->list_wait,
  1729. atomic_read(&sctx->in_flight) == 0);
  1730. atomic_inc(&fs_info->scrubs_paused);
  1731. wake_up(&fs_info->scrub_pause_wait);
  1732. mutex_lock(&fs_info->scrub_lock);
  1733. while (atomic_read(&fs_info->scrub_pause_req)) {
  1734. mutex_unlock(&fs_info->scrub_lock);
  1735. wait_event(fs_info->scrub_pause_wait,
  1736. atomic_read(&fs_info->scrub_pause_req) == 0);
  1737. mutex_lock(&fs_info->scrub_lock);
  1738. }
  1739. atomic_dec(&fs_info->scrubs_paused);
  1740. mutex_unlock(&fs_info->scrub_lock);
  1741. wake_up(&fs_info->scrub_pause_wait);
  1742. }
  1743. ret = btrfs_lookup_csums_range(csum_root, logical,
  1744. logical + map->stripe_len - 1,
  1745. &sctx->csum_list, 1);
  1746. if (ret)
  1747. goto out;
  1748. key.objectid = logical;
  1749. key.type = BTRFS_EXTENT_ITEM_KEY;
  1750. key.offset = (u64)0;
  1751. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1752. if (ret < 0)
  1753. goto out;
  1754. if (ret > 0) {
  1755. ret = btrfs_previous_item(root, path, 0,
  1756. BTRFS_EXTENT_ITEM_KEY);
  1757. if (ret < 0)
  1758. goto out;
  1759. if (ret > 0) {
  1760. /* there's no smaller item, so stick with the
  1761. * larger one */
  1762. btrfs_release_path(path);
  1763. ret = btrfs_search_slot(NULL, root, &key,
  1764. path, 0, 0);
  1765. if (ret < 0)
  1766. goto out;
  1767. }
  1768. }
  1769. while (1) {
  1770. l = path->nodes[0];
  1771. slot = path->slots[0];
  1772. if (slot >= btrfs_header_nritems(l)) {
  1773. ret = btrfs_next_leaf(root, path);
  1774. if (ret == 0)
  1775. continue;
  1776. if (ret < 0)
  1777. goto out;
  1778. break;
  1779. }
  1780. btrfs_item_key_to_cpu(l, &key, slot);
  1781. if (key.objectid + key.offset <= logical)
  1782. goto next;
  1783. if (key.objectid >= logical + map->stripe_len)
  1784. break;
  1785. if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
  1786. goto next;
  1787. extent = btrfs_item_ptr(l, slot,
  1788. struct btrfs_extent_item);
  1789. flags = btrfs_extent_flags(l, extent);
  1790. generation = btrfs_extent_generation(l, extent);
  1791. if (key.objectid < logical &&
  1792. (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
  1793. printk(KERN_ERR
  1794. "btrfs scrub: tree block %llu spanning "
  1795. "stripes, ignored. logical=%llu\n",
  1796. (unsigned long long)key.objectid,
  1797. (unsigned long long)logical);
  1798. goto next;
  1799. }
  1800. /*
  1801. * trim extent to this stripe
  1802. */
  1803. if (key.objectid < logical) {
  1804. key.offset -= logical - key.objectid;
  1805. key.objectid = logical;
  1806. }
  1807. if (key.objectid + key.offset >
  1808. logical + map->stripe_len) {
  1809. key.offset = logical + map->stripe_len -
  1810. key.objectid;
  1811. }
  1812. ret = scrub_extent(sctx, key.objectid, key.offset,
  1813. key.objectid - logical + physical,
  1814. scrub_dev, flags, generation,
  1815. mirror_num);
  1816. if (ret)
  1817. goto out;
  1818. next:
  1819. path->slots[0]++;
  1820. }
  1821. btrfs_release_path(path);
  1822. logical += increment;
  1823. physical += map->stripe_len;
  1824. spin_lock(&sctx->stat_lock);
  1825. sctx->stat.last_physical = physical;
  1826. spin_unlock(&sctx->stat_lock);
  1827. }
  1828. /* push queued extents */
  1829. scrub_submit(sctx);
  1830. out:
  1831. blk_finish_plug(&plug);
  1832. btrfs_free_path(path);
  1833. return ret < 0 ? ret : 0;
  1834. }
  1835. static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
  1836. struct btrfs_device *scrub_dev,
  1837. u64 chunk_tree, u64 chunk_objectid,
  1838. u64 chunk_offset, u64 length,
  1839. u64 dev_offset)
  1840. {
  1841. struct btrfs_mapping_tree *map_tree =
  1842. &sctx->dev_root->fs_info->mapping_tree;
  1843. struct map_lookup *map;
  1844. struct extent_map *em;
  1845. int i;
  1846. int ret = -EINVAL;
  1847. read_lock(&map_tree->map_tree.lock);
  1848. em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
  1849. read_unlock(&map_tree->map_tree.lock);
  1850. if (!em)
  1851. return -EINVAL;
  1852. map = (struct map_lookup *)em->bdev;
  1853. if (em->start != chunk_offset)
  1854. goto out;
  1855. if (em->len < length)
  1856. goto out;
  1857. for (i = 0; i < map->num_stripes; ++i) {
  1858. if (map->stripes[i].dev->bdev == scrub_dev->bdev &&
  1859. map->stripes[i].physical == dev_offset) {
  1860. ret = scrub_stripe(sctx, map, scrub_dev, i,
  1861. chunk_offset, length);
  1862. if (ret)
  1863. goto out;
  1864. }
  1865. }
  1866. out:
  1867. free_extent_map(em);
  1868. return ret;
  1869. }
  1870. static noinline_for_stack
  1871. int scrub_enumerate_chunks(struct scrub_ctx *sctx,
  1872. struct btrfs_device *scrub_dev, u64 start, u64 end)
  1873. {
  1874. struct btrfs_dev_extent *dev_extent = NULL;
  1875. struct btrfs_path *path;
  1876. struct btrfs_root *root = sctx->dev_root;
  1877. struct btrfs_fs_info *fs_info = root->fs_info;
  1878. u64 length;
  1879. u64 chunk_tree;
  1880. u64 chunk_objectid;
  1881. u64 chunk_offset;
  1882. int ret;
  1883. int slot;
  1884. struct extent_buffer *l;
  1885. struct btrfs_key key;
  1886. struct btrfs_key found_key;
  1887. struct btrfs_block_group_cache *cache;
  1888. path = btrfs_alloc_path();
  1889. if (!path)
  1890. return -ENOMEM;
  1891. path->reada = 2;
  1892. path->search_commit_root = 1;
  1893. path->skip_locking = 1;
  1894. key.objectid = scrub_dev->devid;
  1895. key.offset = 0ull;
  1896. key.type = BTRFS_DEV_EXTENT_KEY;
  1897. while (1) {
  1898. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1899. if (ret < 0)
  1900. break;
  1901. if (ret > 0) {
  1902. if (path->slots[0] >=
  1903. btrfs_header_nritems(path->nodes[0])) {
  1904. ret = btrfs_next_leaf(root, path);
  1905. if (ret)
  1906. break;
  1907. }
  1908. }
  1909. l = path->nodes[0];
  1910. slot = path->slots[0];
  1911. btrfs_item_key_to_cpu(l, &found_key, slot);
  1912. if (found_key.objectid != scrub_dev->devid)
  1913. break;
  1914. if (btrfs_key_type(&found_key) != BTRFS_DEV_EXTENT_KEY)
  1915. break;
  1916. if (found_key.offset >= end)
  1917. break;
  1918. if (found_key.offset < key.offset)
  1919. break;
  1920. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  1921. length = btrfs_dev_extent_length(l, dev_extent);
  1922. if (found_key.offset + length <= start) {
  1923. key.offset = found_key.offset + length;
  1924. btrfs_release_path(path);
  1925. continue;
  1926. }
  1927. chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
  1928. chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
  1929. chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
  1930. /*
  1931. * get a reference on the corresponding block group to prevent
  1932. * the chunk from going away while we scrub it
  1933. */
  1934. cache = btrfs_lookup_block_group(fs_info, chunk_offset);
  1935. if (!cache) {
  1936. ret = -ENOENT;
  1937. break;
  1938. }
  1939. ret = scrub_chunk(sctx, scrub_dev, chunk_tree, chunk_objectid,
  1940. chunk_offset, length, found_key.offset);
  1941. btrfs_put_block_group(cache);
  1942. if (ret)
  1943. break;
  1944. key.offset = found_key.offset + length;
  1945. btrfs_release_path(path);
  1946. }
  1947. btrfs_free_path(path);
  1948. /*
  1949. * ret can still be 1 from search_slot or next_leaf,
  1950. * that's not an error
  1951. */
  1952. return ret < 0 ? ret : 0;
  1953. }
  1954. static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,
  1955. struct btrfs_device *scrub_dev)
  1956. {
  1957. int i;
  1958. u64 bytenr;
  1959. u64 gen;
  1960. int ret;
  1961. struct btrfs_root *root = sctx->dev_root;
  1962. if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR)
  1963. return -EIO;
  1964. gen = root->fs_info->last_trans_committed;
  1965. for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
  1966. bytenr = btrfs_sb_offset(i);
  1967. if (bytenr + BTRFS_SUPER_INFO_SIZE > scrub_dev->total_bytes)
  1968. break;
  1969. ret = scrub_pages(sctx, bytenr, BTRFS_SUPER_INFO_SIZE, bytenr,
  1970. scrub_dev, BTRFS_EXTENT_FLAG_SUPER, gen, i,
  1971. NULL, 1);
  1972. if (ret)
  1973. return ret;
  1974. }
  1975. wait_event(sctx->list_wait, atomic_read(&sctx->in_flight) == 0);
  1976. return 0;
  1977. }
  1978. /*
  1979. * get a reference count on fs_info->scrub_workers. start worker if necessary
  1980. */
  1981. static noinline_for_stack int scrub_workers_get(struct btrfs_root *root)
  1982. {
  1983. struct btrfs_fs_info *fs_info = root->fs_info;
  1984. int ret = 0;
  1985. mutex_lock(&fs_info->scrub_lock);
  1986. if (fs_info->scrub_workers_refcnt == 0) {
  1987. btrfs_init_workers(&fs_info->scrub_workers, "scrub",
  1988. fs_info->thread_pool_size, &fs_info->generic_worker);
  1989. fs_info->scrub_workers.idle_thresh = 4;
  1990. ret = btrfs_start_workers(&fs_info->scrub_workers);
  1991. if (ret)
  1992. goto out;
  1993. }
  1994. ++fs_info->scrub_workers_refcnt;
  1995. out:
  1996. mutex_unlock(&fs_info->scrub_lock);
  1997. return ret;
  1998. }
  1999. static noinline_for_stack void scrub_workers_put(struct btrfs_root *root)
  2000. {
  2001. struct btrfs_fs_info *fs_info = root->fs_info;
  2002. mutex_lock(&fs_info->scrub_lock);
  2003. if (--fs_info->scrub_workers_refcnt == 0)
  2004. btrfs_stop_workers(&fs_info->scrub_workers);
  2005. WARN_ON(fs_info->scrub_workers_refcnt < 0);
  2006. mutex_unlock(&fs_info->scrub_lock);
  2007. }
  2008. int btrfs_scrub_dev(struct btrfs_root *root, u64 devid, u64 start, u64 end,
  2009. struct btrfs_scrub_progress *progress, int readonly)
  2010. {
  2011. struct scrub_ctx *sctx;
  2012. struct btrfs_fs_info *fs_info = root->fs_info;
  2013. int ret;
  2014. struct btrfs_device *dev;
  2015. if (btrfs_fs_closing(root->fs_info))
  2016. return -EINVAL;
  2017. /*
  2018. * check some assumptions
  2019. */
  2020. if (root->nodesize != root->leafsize) {
  2021. printk(KERN_ERR
  2022. "btrfs_scrub: size assumption nodesize == leafsize (%d == %d) fails\n",
  2023. root->nodesize, root->leafsize);
  2024. return -EINVAL;
  2025. }
  2026. if (root->nodesize > BTRFS_STRIPE_LEN) {
  2027. /*
  2028. * in this case scrub is unable to calculate the checksum
  2029. * the way scrub is implemented. Do not handle this
  2030. * situation at all because it won't ever happen.
  2031. */
  2032. printk(KERN_ERR
  2033. "btrfs_scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails\n",
  2034. root->nodesize, BTRFS_STRIPE_LEN);
  2035. return -EINVAL;
  2036. }
  2037. if (root->sectorsize != PAGE_SIZE) {
  2038. /* not supported for data w/o checksums */
  2039. printk(KERN_ERR
  2040. "btrfs_scrub: size assumption sectorsize != PAGE_SIZE (%d != %lld) fails\n",
  2041. root->sectorsize, (unsigned long long)PAGE_SIZE);
  2042. return -EINVAL;
  2043. }
  2044. if (fs_info->chunk_root->nodesize >
  2045. PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK ||
  2046. fs_info->chunk_root->sectorsize >
  2047. PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK) {
  2048. /*
  2049. * would exhaust the array bounds of pagev member in
  2050. * struct scrub_block
  2051. */
  2052. pr_err("btrfs_scrub: size assumption nodesize and sectorsize <= SCRUB_MAX_PAGES_PER_BLOCK (%d <= %d && %d <= %d) fails\n",
  2053. fs_info->chunk_root->nodesize,
  2054. SCRUB_MAX_PAGES_PER_BLOCK,
  2055. fs_info->chunk_root->sectorsize,
  2056. SCRUB_MAX_PAGES_PER_BLOCK);
  2057. return -EINVAL;
  2058. }
  2059. ret = scrub_workers_get(root);
  2060. if (ret)
  2061. return ret;
  2062. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  2063. dev = btrfs_find_device(root, devid, NULL, NULL);
  2064. if (!dev || dev->missing) {
  2065. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  2066. scrub_workers_put(root);
  2067. return -ENODEV;
  2068. }
  2069. mutex_lock(&fs_info->scrub_lock);
  2070. if (!dev->in_fs_metadata) {
  2071. mutex_unlock(&fs_info->scrub_lock);
  2072. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  2073. scrub_workers_put(root);
  2074. return -ENODEV;
  2075. }
  2076. if (dev->scrub_device) {
  2077. mutex_unlock(&fs_info->scrub_lock);
  2078. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  2079. scrub_workers_put(root);
  2080. return -EINPROGRESS;
  2081. }
  2082. sctx = scrub_setup_ctx(dev);
  2083. if (IS_ERR(sctx)) {
  2084. mutex_unlock(&fs_info->scrub_lock);
  2085. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  2086. scrub_workers_put(root);
  2087. return PTR_ERR(sctx);
  2088. }
  2089. sctx->readonly = readonly;
  2090. dev->scrub_device = sctx;
  2091. atomic_inc(&fs_info->scrubs_running);
  2092. mutex_unlock(&fs_info->scrub_lock);
  2093. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  2094. down_read(&fs_info->scrub_super_lock);
  2095. ret = scrub_supers(sctx, dev);
  2096. up_read(&fs_info->scrub_super_lock);
  2097. if (!ret)
  2098. ret = scrub_enumerate_chunks(sctx, dev, start, end);
  2099. wait_event(sctx->list_wait, atomic_read(&sctx->in_flight) == 0);
  2100. atomic_dec(&fs_info->scrubs_running);
  2101. wake_up(&fs_info->scrub_pause_wait);
  2102. wait_event(sctx->list_wait, atomic_read(&sctx->fixup_cnt) == 0);
  2103. if (progress)
  2104. memcpy(progress, &sctx->stat, sizeof(*progress));
  2105. mutex_lock(&fs_info->scrub_lock);
  2106. dev->scrub_device = NULL;
  2107. mutex_unlock(&fs_info->scrub_lock);
  2108. scrub_free_ctx(sctx);
  2109. scrub_workers_put(root);
  2110. return ret;
  2111. }
  2112. void btrfs_scrub_pause(struct btrfs_root *root)
  2113. {
  2114. struct btrfs_fs_info *fs_info = root->fs_info;
  2115. mutex_lock(&fs_info->scrub_lock);
  2116. atomic_inc(&fs_info->scrub_pause_req);
  2117. while (atomic_read(&fs_info->scrubs_paused) !=
  2118. atomic_read(&fs_info->scrubs_running)) {
  2119. mutex_unlock(&fs_info->scrub_lock);
  2120. wait_event(fs_info->scrub_pause_wait,
  2121. atomic_read(&fs_info->scrubs_paused) ==
  2122. atomic_read(&fs_info->scrubs_running));
  2123. mutex_lock(&fs_info->scrub_lock);
  2124. }
  2125. mutex_unlock(&fs_info->scrub_lock);
  2126. }
  2127. void btrfs_scrub_continue(struct btrfs_root *root)
  2128. {
  2129. struct btrfs_fs_info *fs_info = root->fs_info;
  2130. atomic_dec(&fs_info->scrub_pause_req);
  2131. wake_up(&fs_info->scrub_pause_wait);
  2132. }
  2133. void btrfs_scrub_pause_super(struct btrfs_root *root)
  2134. {
  2135. down_write(&root->fs_info->scrub_super_lock);
  2136. }
  2137. void btrfs_scrub_continue_super(struct btrfs_root *root)
  2138. {
  2139. up_write(&root->fs_info->scrub_super_lock);
  2140. }
  2141. int __btrfs_scrub_cancel(struct btrfs_fs_info *fs_info)
  2142. {
  2143. mutex_lock(&fs_info->scrub_lock);
  2144. if (!atomic_read(&fs_info->scrubs_running)) {
  2145. mutex_unlock(&fs_info->scrub_lock);
  2146. return -ENOTCONN;
  2147. }
  2148. atomic_inc(&fs_info->scrub_cancel_req);
  2149. while (atomic_read(&fs_info->scrubs_running)) {
  2150. mutex_unlock(&fs_info->scrub_lock);
  2151. wait_event(fs_info->scrub_pause_wait,
  2152. atomic_read(&fs_info->scrubs_running) == 0);
  2153. mutex_lock(&fs_info->scrub_lock);
  2154. }
  2155. atomic_dec(&fs_info->scrub_cancel_req);
  2156. mutex_unlock(&fs_info->scrub_lock);
  2157. return 0;
  2158. }
  2159. int btrfs_scrub_cancel(struct btrfs_root *root)
  2160. {
  2161. return __btrfs_scrub_cancel(root->fs_info);
  2162. }
  2163. int btrfs_scrub_cancel_dev(struct btrfs_root *root, struct btrfs_device *dev)
  2164. {
  2165. struct btrfs_fs_info *fs_info = root->fs_info;
  2166. struct scrub_ctx *sctx;
  2167. mutex_lock(&fs_info->scrub_lock);
  2168. sctx = dev->scrub_device;
  2169. if (!sctx) {
  2170. mutex_unlock(&fs_info->scrub_lock);
  2171. return -ENOTCONN;
  2172. }
  2173. atomic_inc(&sctx->cancel_req);
  2174. while (dev->scrub_device) {
  2175. mutex_unlock(&fs_info->scrub_lock);
  2176. wait_event(fs_info->scrub_pause_wait,
  2177. dev->scrub_device == NULL);
  2178. mutex_lock(&fs_info->scrub_lock);
  2179. }
  2180. mutex_unlock(&fs_info->scrub_lock);
  2181. return 0;
  2182. }
  2183. int btrfs_scrub_cancel_devid(struct btrfs_root *root, u64 devid)
  2184. {
  2185. struct btrfs_fs_info *fs_info = root->fs_info;
  2186. struct btrfs_device *dev;
  2187. int ret;
  2188. /*
  2189. * we have to hold the device_list_mutex here so the device
  2190. * does not go away in cancel_dev. FIXME: find a better solution
  2191. */
  2192. mutex_lock(&fs_info->fs_devices->device_list_mutex);
  2193. dev = btrfs_find_device(root, devid, NULL, NULL);
  2194. if (!dev) {
  2195. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  2196. return -ENODEV;
  2197. }
  2198. ret = btrfs_scrub_cancel_dev(root, dev);
  2199. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  2200. return ret;
  2201. }
  2202. int btrfs_scrub_progress(struct btrfs_root *root, u64 devid,
  2203. struct btrfs_scrub_progress *progress)
  2204. {
  2205. struct btrfs_device *dev;
  2206. struct scrub_ctx *sctx = NULL;
  2207. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  2208. dev = btrfs_find_device(root, devid, NULL, NULL);
  2209. if (dev)
  2210. sctx = dev->scrub_device;
  2211. if (sctx)
  2212. memcpy(progress, &sctx->stat, sizeof(*progress));
  2213. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  2214. return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV;
  2215. }