scrub.c 65 KB

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