scrub.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567
  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 "backref.h"
  25. /*
  26. * This is only the first step towards a full-features scrub. It reads all
  27. * extent and super block and verifies the checksums. In case a bad checksum
  28. * is found or the extent cannot be read, good data will be written back if
  29. * any can be found.
  30. *
  31. * Future enhancements:
  32. * - To enhance the performance, better read-ahead strategies for the
  33. * extent-tree can be employed.
  34. * - In case an unrepairable extent is encountered, track which files are
  35. * affected and report them
  36. * - In case of a read error on files with nodatasum, map the file and read
  37. * the extent to trigger a writeback of the good copy
  38. * - track and record media errors, throw out bad devices
  39. * - add a mode to also read unallocated space
  40. * - make the prefetch cancellable
  41. */
  42. struct scrub_bio;
  43. struct scrub_page;
  44. struct scrub_dev;
  45. static void scrub_bio_end_io(struct bio *bio, int err);
  46. static void scrub_checksum(struct btrfs_work *work);
  47. static int scrub_checksum_data(struct scrub_dev *sdev,
  48. struct scrub_page *spag, void *buffer);
  49. static int scrub_checksum_tree_block(struct scrub_dev *sdev,
  50. struct scrub_page *spag, u64 logical,
  51. void *buffer);
  52. static int scrub_checksum_super(struct scrub_bio *sbio, void *buffer);
  53. static int scrub_fixup_check(struct scrub_bio *sbio, int ix);
  54. static void scrub_fixup_end_io(struct bio *bio, int err);
  55. static int scrub_fixup_io(int rw, struct block_device *bdev, sector_t sector,
  56. struct page *page);
  57. static void scrub_fixup(struct scrub_bio *sbio, int ix);
  58. #define SCRUB_PAGES_PER_BIO 16 /* 64k per bio */
  59. #define SCRUB_BIOS_PER_DEV 16 /* 1 MB per device in flight */
  60. struct scrub_page {
  61. u64 flags; /* extent flags */
  62. u64 generation;
  63. u64 mirror_num;
  64. int have_csum;
  65. u8 csum[BTRFS_CSUM_SIZE];
  66. };
  67. struct scrub_bio {
  68. int index;
  69. struct scrub_dev *sdev;
  70. struct bio *bio;
  71. int err;
  72. u64 logical;
  73. u64 physical;
  74. struct scrub_page spag[SCRUB_PAGES_PER_BIO];
  75. u64 count;
  76. int next_free;
  77. struct btrfs_work work;
  78. };
  79. struct scrub_dev {
  80. struct scrub_bio *bios[SCRUB_BIOS_PER_DEV];
  81. struct btrfs_device *dev;
  82. int first_free;
  83. int curr;
  84. atomic_t in_flight;
  85. spinlock_t list_lock;
  86. wait_queue_head_t list_wait;
  87. u16 csum_size;
  88. struct list_head csum_list;
  89. atomic_t cancel_req;
  90. int readonly;
  91. /*
  92. * statistics
  93. */
  94. struct btrfs_scrub_progress stat;
  95. spinlock_t stat_lock;
  96. };
  97. struct scrub_warning {
  98. struct btrfs_path *path;
  99. u64 extent_item_size;
  100. char *scratch_buf;
  101. char *msg_buf;
  102. const char *errstr;
  103. sector_t sector;
  104. u64 logical;
  105. struct btrfs_device *dev;
  106. int msg_bufsize;
  107. int scratch_bufsize;
  108. };
  109. static void scrub_free_csums(struct scrub_dev *sdev)
  110. {
  111. while (!list_empty(&sdev->csum_list)) {
  112. struct btrfs_ordered_sum *sum;
  113. sum = list_first_entry(&sdev->csum_list,
  114. struct btrfs_ordered_sum, list);
  115. list_del(&sum->list);
  116. kfree(sum);
  117. }
  118. }
  119. static void scrub_free_bio(struct bio *bio)
  120. {
  121. int i;
  122. struct page *last_page = NULL;
  123. if (!bio)
  124. return;
  125. for (i = 0; i < bio->bi_vcnt; ++i) {
  126. if (bio->bi_io_vec[i].bv_page == last_page)
  127. continue;
  128. last_page = bio->bi_io_vec[i].bv_page;
  129. __free_page(last_page);
  130. }
  131. bio_put(bio);
  132. }
  133. static noinline_for_stack void scrub_free_dev(struct scrub_dev *sdev)
  134. {
  135. int i;
  136. if (!sdev)
  137. return;
  138. for (i = 0; i < SCRUB_BIOS_PER_DEV; ++i) {
  139. struct scrub_bio *sbio = sdev->bios[i];
  140. if (!sbio)
  141. break;
  142. scrub_free_bio(sbio->bio);
  143. kfree(sbio);
  144. }
  145. scrub_free_csums(sdev);
  146. kfree(sdev);
  147. }
  148. static noinline_for_stack
  149. struct scrub_dev *scrub_setup_dev(struct btrfs_device *dev)
  150. {
  151. struct scrub_dev *sdev;
  152. int i;
  153. struct btrfs_fs_info *fs_info = dev->dev_root->fs_info;
  154. sdev = kzalloc(sizeof(*sdev), GFP_NOFS);
  155. if (!sdev)
  156. goto nomem;
  157. sdev->dev = dev;
  158. for (i = 0; i < SCRUB_BIOS_PER_DEV; ++i) {
  159. struct scrub_bio *sbio;
  160. sbio = kzalloc(sizeof(*sbio), GFP_NOFS);
  161. if (!sbio)
  162. goto nomem;
  163. sdev->bios[i] = sbio;
  164. sbio->index = i;
  165. sbio->sdev = sdev;
  166. sbio->count = 0;
  167. sbio->work.func = scrub_checksum;
  168. if (i != SCRUB_BIOS_PER_DEV-1)
  169. sdev->bios[i]->next_free = i + 1;
  170. else
  171. sdev->bios[i]->next_free = -1;
  172. }
  173. sdev->first_free = 0;
  174. sdev->curr = -1;
  175. atomic_set(&sdev->in_flight, 0);
  176. atomic_set(&sdev->cancel_req, 0);
  177. sdev->csum_size = btrfs_super_csum_size(&fs_info->super_copy);
  178. INIT_LIST_HEAD(&sdev->csum_list);
  179. spin_lock_init(&sdev->list_lock);
  180. spin_lock_init(&sdev->stat_lock);
  181. init_waitqueue_head(&sdev->list_wait);
  182. return sdev;
  183. nomem:
  184. scrub_free_dev(sdev);
  185. return ERR_PTR(-ENOMEM);
  186. }
  187. static int scrub_print_warning_inode(u64 inum, u64 offset, u64 root, void *ctx)
  188. {
  189. u64 isize;
  190. u32 nlink;
  191. int ret;
  192. int i;
  193. struct extent_buffer *eb;
  194. struct btrfs_inode_item *inode_item;
  195. struct scrub_warning *swarn = ctx;
  196. struct btrfs_fs_info *fs_info = swarn->dev->dev_root->fs_info;
  197. struct inode_fs_paths *ipath = NULL;
  198. struct btrfs_root *local_root;
  199. struct btrfs_key root_key;
  200. root_key.objectid = root;
  201. root_key.type = BTRFS_ROOT_ITEM_KEY;
  202. root_key.offset = (u64)-1;
  203. local_root = btrfs_read_fs_root_no_name(fs_info, &root_key);
  204. if (IS_ERR(local_root)) {
  205. ret = PTR_ERR(local_root);
  206. goto err;
  207. }
  208. ret = inode_item_info(inum, 0, local_root, swarn->path);
  209. if (ret) {
  210. btrfs_release_path(swarn->path);
  211. goto err;
  212. }
  213. eb = swarn->path->nodes[0];
  214. inode_item = btrfs_item_ptr(eb, swarn->path->slots[0],
  215. struct btrfs_inode_item);
  216. isize = btrfs_inode_size(eb, inode_item);
  217. nlink = btrfs_inode_nlink(eb, inode_item);
  218. btrfs_release_path(swarn->path);
  219. ipath = init_ipath(4096, local_root, swarn->path);
  220. ret = paths_from_inode(inum, ipath);
  221. if (ret < 0)
  222. goto err;
  223. /*
  224. * we deliberately ignore the bit ipath might have been too small to
  225. * hold all of the paths here
  226. */
  227. for (i = 0; i < ipath->fspath->elem_cnt; ++i)
  228. printk(KERN_WARNING "btrfs: %s at logical %llu on dev "
  229. "%s, sector %llu, root %llu, inode %llu, offset %llu, "
  230. "length %llu, links %u (path: %s)\n", swarn->errstr,
  231. swarn->logical, swarn->dev->name,
  232. (unsigned long long)swarn->sector, root, inum, offset,
  233. min(isize - offset, (u64)PAGE_SIZE), nlink,
  234. ipath->fspath->str[i]);
  235. free_ipath(ipath);
  236. return 0;
  237. err:
  238. printk(KERN_WARNING "btrfs: %s at logical %llu on dev "
  239. "%s, sector %llu, root %llu, inode %llu, offset %llu: path "
  240. "resolving failed with ret=%d\n", swarn->errstr,
  241. swarn->logical, swarn->dev->name,
  242. (unsigned long long)swarn->sector, root, inum, offset, ret);
  243. free_ipath(ipath);
  244. return 0;
  245. }
  246. static void scrub_print_warning(const char *errstr, struct scrub_bio *sbio,
  247. int ix)
  248. {
  249. struct btrfs_device *dev = sbio->sdev->dev;
  250. struct btrfs_fs_info *fs_info = dev->dev_root->fs_info;
  251. struct btrfs_path *path;
  252. struct btrfs_key found_key;
  253. struct extent_buffer *eb;
  254. struct btrfs_extent_item *ei;
  255. struct scrub_warning swarn;
  256. u32 item_size;
  257. int ret;
  258. u64 ref_root;
  259. u8 ref_level;
  260. unsigned long ptr = 0;
  261. const int bufsize = 4096;
  262. u64 extent_offset;
  263. path = btrfs_alloc_path();
  264. swarn.scratch_buf = kmalloc(bufsize, GFP_NOFS);
  265. swarn.msg_buf = kmalloc(bufsize, GFP_NOFS);
  266. swarn.sector = (sbio->physical + ix * PAGE_SIZE) >> 9;
  267. swarn.logical = sbio->logical + ix * PAGE_SIZE;
  268. swarn.errstr = errstr;
  269. swarn.dev = dev;
  270. swarn.msg_bufsize = bufsize;
  271. swarn.scratch_bufsize = bufsize;
  272. if (!path || !swarn.scratch_buf || !swarn.msg_buf)
  273. goto out;
  274. ret = extent_from_logical(fs_info, swarn.logical, path, &found_key);
  275. if (ret < 0)
  276. goto out;
  277. extent_offset = swarn.logical - found_key.objectid;
  278. swarn.extent_item_size = found_key.offset;
  279. eb = path->nodes[0];
  280. ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
  281. item_size = btrfs_item_size_nr(eb, path->slots[0]);
  282. if (ret & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  283. do {
  284. ret = tree_backref_for_extent(&ptr, eb, ei, item_size,
  285. &ref_root, &ref_level);
  286. printk(KERN_WARNING "%s at logical %llu on dev %s, "
  287. "sector %llu: metadata %s (level %d) in tree "
  288. "%llu\n", errstr, swarn.logical, dev->name,
  289. (unsigned long long)swarn.sector,
  290. ref_level ? "node" : "leaf",
  291. ret < 0 ? -1 : ref_level,
  292. ret < 0 ? -1 : ref_root);
  293. } while (ret != 1);
  294. } else {
  295. swarn.path = path;
  296. iterate_extent_inodes(fs_info, path, found_key.objectid,
  297. extent_offset,
  298. scrub_print_warning_inode, &swarn);
  299. }
  300. out:
  301. btrfs_free_path(path);
  302. kfree(swarn.scratch_buf);
  303. kfree(swarn.msg_buf);
  304. }
  305. /*
  306. * scrub_recheck_error gets called when either verification of the page
  307. * failed or the bio failed to read, e.g. with EIO. In the latter case,
  308. * recheck_error gets called for every page in the bio, even though only
  309. * one may be bad
  310. */
  311. static int scrub_recheck_error(struct scrub_bio *sbio, int ix)
  312. {
  313. struct scrub_dev *sdev = sbio->sdev;
  314. u64 sector = (sbio->physical + ix * PAGE_SIZE) >> 9;
  315. static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
  316. DEFAULT_RATELIMIT_BURST);
  317. if (sbio->err) {
  318. if (scrub_fixup_io(READ, sbio->sdev->dev->bdev, sector,
  319. sbio->bio->bi_io_vec[ix].bv_page) == 0) {
  320. if (scrub_fixup_check(sbio, ix) == 0)
  321. return 0;
  322. }
  323. if (__ratelimit(&_rs))
  324. scrub_print_warning("i/o error", sbio, ix);
  325. } else {
  326. if (__ratelimit(&_rs))
  327. scrub_print_warning("checksum error", sbio, ix);
  328. }
  329. spin_lock(&sdev->stat_lock);
  330. ++sdev->stat.read_errors;
  331. spin_unlock(&sdev->stat_lock);
  332. scrub_fixup(sbio, ix);
  333. return 1;
  334. }
  335. static int scrub_fixup_check(struct scrub_bio *sbio, int ix)
  336. {
  337. int ret = 1;
  338. struct page *page;
  339. void *buffer;
  340. u64 flags = sbio->spag[ix].flags;
  341. page = sbio->bio->bi_io_vec[ix].bv_page;
  342. buffer = kmap_atomic(page, KM_USER0);
  343. if (flags & BTRFS_EXTENT_FLAG_DATA) {
  344. ret = scrub_checksum_data(sbio->sdev,
  345. sbio->spag + ix, buffer);
  346. } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  347. ret = scrub_checksum_tree_block(sbio->sdev,
  348. sbio->spag + ix,
  349. sbio->logical + ix * PAGE_SIZE,
  350. buffer);
  351. } else {
  352. WARN_ON(1);
  353. }
  354. kunmap_atomic(buffer, KM_USER0);
  355. return ret;
  356. }
  357. static void scrub_fixup_end_io(struct bio *bio, int err)
  358. {
  359. complete((struct completion *)bio->bi_private);
  360. }
  361. static void scrub_fixup(struct scrub_bio *sbio, int ix)
  362. {
  363. struct scrub_dev *sdev = sbio->sdev;
  364. struct btrfs_fs_info *fs_info = sdev->dev->dev_root->fs_info;
  365. struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
  366. struct btrfs_multi_bio *multi = NULL;
  367. u64 logical = sbio->logical + ix * PAGE_SIZE;
  368. u64 length;
  369. int i;
  370. int ret;
  371. DECLARE_COMPLETION_ONSTACK(complete);
  372. if ((sbio->spag[ix].flags & BTRFS_EXTENT_FLAG_DATA) &&
  373. (sbio->spag[ix].have_csum == 0)) {
  374. /*
  375. * nodatasum, don't try to fix anything
  376. * FIXME: we can do better, open the inode and trigger a
  377. * writeback
  378. */
  379. goto uncorrectable;
  380. }
  381. length = PAGE_SIZE;
  382. ret = btrfs_map_block(map_tree, REQ_WRITE, logical, &length,
  383. &multi, 0);
  384. if (ret || !multi || length < PAGE_SIZE) {
  385. printk(KERN_ERR
  386. "scrub_fixup: btrfs_map_block failed us for %llu\n",
  387. (unsigned long long)logical);
  388. WARN_ON(1);
  389. return;
  390. }
  391. if (multi->num_stripes == 1)
  392. /* there aren't any replicas */
  393. goto uncorrectable;
  394. /*
  395. * first find a good copy
  396. */
  397. for (i = 0; i < multi->num_stripes; ++i) {
  398. if (i + 1 == sbio->spag[ix].mirror_num)
  399. continue;
  400. if (scrub_fixup_io(READ, multi->stripes[i].dev->bdev,
  401. multi->stripes[i].physical >> 9,
  402. sbio->bio->bi_io_vec[ix].bv_page)) {
  403. /* I/O-error, this is not a good copy */
  404. continue;
  405. }
  406. if (scrub_fixup_check(sbio, ix) == 0)
  407. break;
  408. }
  409. if (i == multi->num_stripes)
  410. goto uncorrectable;
  411. if (!sdev->readonly) {
  412. /*
  413. * bi_io_vec[ix].bv_page now contains good data, write it back
  414. */
  415. if (scrub_fixup_io(WRITE, sdev->dev->bdev,
  416. (sbio->physical + ix * PAGE_SIZE) >> 9,
  417. sbio->bio->bi_io_vec[ix].bv_page)) {
  418. /* I/O-error, writeback failed, give up */
  419. goto uncorrectable;
  420. }
  421. }
  422. kfree(multi);
  423. spin_lock(&sdev->stat_lock);
  424. ++sdev->stat.corrected_errors;
  425. spin_unlock(&sdev->stat_lock);
  426. printk_ratelimited(KERN_ERR "btrfs: fixed up error at logical %llu\n",
  427. (unsigned long long)logical);
  428. return;
  429. uncorrectable:
  430. kfree(multi);
  431. spin_lock(&sdev->stat_lock);
  432. ++sdev->stat.uncorrectable_errors;
  433. spin_unlock(&sdev->stat_lock);
  434. printk_ratelimited(KERN_ERR "btrfs: unable to fixup (regular) error at "
  435. "logical %llu\n", (unsigned long long)logical);
  436. }
  437. static int scrub_fixup_io(int rw, struct block_device *bdev, sector_t sector,
  438. struct page *page)
  439. {
  440. struct bio *bio = NULL;
  441. int ret;
  442. DECLARE_COMPLETION_ONSTACK(complete);
  443. bio = bio_alloc(GFP_NOFS, 1);
  444. bio->bi_bdev = bdev;
  445. bio->bi_sector = sector;
  446. bio_add_page(bio, page, PAGE_SIZE, 0);
  447. bio->bi_end_io = scrub_fixup_end_io;
  448. bio->bi_private = &complete;
  449. submit_bio(rw, bio);
  450. /* this will also unplug the queue */
  451. wait_for_completion(&complete);
  452. ret = !test_bit(BIO_UPTODATE, &bio->bi_flags);
  453. bio_put(bio);
  454. return ret;
  455. }
  456. static void scrub_bio_end_io(struct bio *bio, int err)
  457. {
  458. struct scrub_bio *sbio = bio->bi_private;
  459. struct scrub_dev *sdev = sbio->sdev;
  460. struct btrfs_fs_info *fs_info = sdev->dev->dev_root->fs_info;
  461. sbio->err = err;
  462. sbio->bio = bio;
  463. btrfs_queue_worker(&fs_info->scrub_workers, &sbio->work);
  464. }
  465. static void scrub_checksum(struct btrfs_work *work)
  466. {
  467. struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
  468. struct scrub_dev *sdev = sbio->sdev;
  469. struct page *page;
  470. void *buffer;
  471. int i;
  472. u64 flags;
  473. u64 logical;
  474. int ret;
  475. if (sbio->err) {
  476. ret = 0;
  477. for (i = 0; i < sbio->count; ++i)
  478. ret |= scrub_recheck_error(sbio, i);
  479. if (!ret) {
  480. spin_lock(&sdev->stat_lock);
  481. ++sdev->stat.unverified_errors;
  482. spin_unlock(&sdev->stat_lock);
  483. }
  484. sbio->bio->bi_flags &= ~(BIO_POOL_MASK - 1);
  485. sbio->bio->bi_flags |= 1 << BIO_UPTODATE;
  486. sbio->bio->bi_phys_segments = 0;
  487. sbio->bio->bi_idx = 0;
  488. for (i = 0; i < sbio->count; i++) {
  489. struct bio_vec *bi;
  490. bi = &sbio->bio->bi_io_vec[i];
  491. bi->bv_offset = 0;
  492. bi->bv_len = PAGE_SIZE;
  493. }
  494. goto out;
  495. }
  496. for (i = 0; i < sbio->count; ++i) {
  497. page = sbio->bio->bi_io_vec[i].bv_page;
  498. buffer = kmap_atomic(page, KM_USER0);
  499. flags = sbio->spag[i].flags;
  500. logical = sbio->logical + i * PAGE_SIZE;
  501. ret = 0;
  502. if (flags & BTRFS_EXTENT_FLAG_DATA) {
  503. ret = scrub_checksum_data(sdev, sbio->spag + i, buffer);
  504. } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  505. ret = scrub_checksum_tree_block(sdev, sbio->spag + i,
  506. logical, buffer);
  507. } else if (flags & BTRFS_EXTENT_FLAG_SUPER) {
  508. BUG_ON(i);
  509. (void)scrub_checksum_super(sbio, buffer);
  510. } else {
  511. WARN_ON(1);
  512. }
  513. kunmap_atomic(buffer, KM_USER0);
  514. if (ret) {
  515. ret = scrub_recheck_error(sbio, i);
  516. if (!ret) {
  517. spin_lock(&sdev->stat_lock);
  518. ++sdev->stat.unverified_errors;
  519. spin_unlock(&sdev->stat_lock);
  520. }
  521. }
  522. }
  523. out:
  524. scrub_free_bio(sbio->bio);
  525. sbio->bio = NULL;
  526. spin_lock(&sdev->list_lock);
  527. sbio->next_free = sdev->first_free;
  528. sdev->first_free = sbio->index;
  529. spin_unlock(&sdev->list_lock);
  530. atomic_dec(&sdev->in_flight);
  531. wake_up(&sdev->list_wait);
  532. }
  533. static int scrub_checksum_data(struct scrub_dev *sdev,
  534. struct scrub_page *spag, void *buffer)
  535. {
  536. u8 csum[BTRFS_CSUM_SIZE];
  537. u32 crc = ~(u32)0;
  538. int fail = 0;
  539. struct btrfs_root *root = sdev->dev->dev_root;
  540. if (!spag->have_csum)
  541. return 0;
  542. crc = btrfs_csum_data(root, buffer, crc, PAGE_SIZE);
  543. btrfs_csum_final(crc, csum);
  544. if (memcmp(csum, spag->csum, sdev->csum_size))
  545. fail = 1;
  546. spin_lock(&sdev->stat_lock);
  547. ++sdev->stat.data_extents_scrubbed;
  548. sdev->stat.data_bytes_scrubbed += PAGE_SIZE;
  549. if (fail)
  550. ++sdev->stat.csum_errors;
  551. spin_unlock(&sdev->stat_lock);
  552. return fail;
  553. }
  554. static int scrub_checksum_tree_block(struct scrub_dev *sdev,
  555. struct scrub_page *spag, u64 logical,
  556. void *buffer)
  557. {
  558. struct btrfs_header *h;
  559. struct btrfs_root *root = sdev->dev->dev_root;
  560. struct btrfs_fs_info *fs_info = root->fs_info;
  561. u8 csum[BTRFS_CSUM_SIZE];
  562. u32 crc = ~(u32)0;
  563. int fail = 0;
  564. int crc_fail = 0;
  565. /*
  566. * we don't use the getter functions here, as we
  567. * a) don't have an extent buffer and
  568. * b) the page is already kmapped
  569. */
  570. h = (struct btrfs_header *)buffer;
  571. if (logical != le64_to_cpu(h->bytenr))
  572. ++fail;
  573. if (spag->generation != le64_to_cpu(h->generation))
  574. ++fail;
  575. if (memcmp(h->fsid, fs_info->fsid, BTRFS_UUID_SIZE))
  576. ++fail;
  577. if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
  578. BTRFS_UUID_SIZE))
  579. ++fail;
  580. crc = btrfs_csum_data(root, buffer + BTRFS_CSUM_SIZE, crc,
  581. PAGE_SIZE - BTRFS_CSUM_SIZE);
  582. btrfs_csum_final(crc, csum);
  583. if (memcmp(csum, h->csum, sdev->csum_size))
  584. ++crc_fail;
  585. spin_lock(&sdev->stat_lock);
  586. ++sdev->stat.tree_extents_scrubbed;
  587. sdev->stat.tree_bytes_scrubbed += PAGE_SIZE;
  588. if (crc_fail)
  589. ++sdev->stat.csum_errors;
  590. if (fail)
  591. ++sdev->stat.verify_errors;
  592. spin_unlock(&sdev->stat_lock);
  593. return fail || crc_fail;
  594. }
  595. static int scrub_checksum_super(struct scrub_bio *sbio, void *buffer)
  596. {
  597. struct btrfs_super_block *s;
  598. u64 logical;
  599. struct scrub_dev *sdev = sbio->sdev;
  600. struct btrfs_root *root = sdev->dev->dev_root;
  601. struct btrfs_fs_info *fs_info = root->fs_info;
  602. u8 csum[BTRFS_CSUM_SIZE];
  603. u32 crc = ~(u32)0;
  604. int fail = 0;
  605. s = (struct btrfs_super_block *)buffer;
  606. logical = sbio->logical;
  607. if (logical != le64_to_cpu(s->bytenr))
  608. ++fail;
  609. if (sbio->spag[0].generation != le64_to_cpu(s->generation))
  610. ++fail;
  611. if (memcmp(s->fsid, fs_info->fsid, BTRFS_UUID_SIZE))
  612. ++fail;
  613. crc = btrfs_csum_data(root, buffer + BTRFS_CSUM_SIZE, crc,
  614. PAGE_SIZE - BTRFS_CSUM_SIZE);
  615. btrfs_csum_final(crc, csum);
  616. if (memcmp(csum, s->csum, sbio->sdev->csum_size))
  617. ++fail;
  618. if (fail) {
  619. /*
  620. * if we find an error in a super block, we just report it.
  621. * They will get written with the next transaction commit
  622. * anyway
  623. */
  624. spin_lock(&sdev->stat_lock);
  625. ++sdev->stat.super_errors;
  626. spin_unlock(&sdev->stat_lock);
  627. }
  628. return fail;
  629. }
  630. static int scrub_submit(struct scrub_dev *sdev)
  631. {
  632. struct scrub_bio *sbio;
  633. struct bio *bio;
  634. int i;
  635. if (sdev->curr == -1)
  636. return 0;
  637. sbio = sdev->bios[sdev->curr];
  638. bio = bio_alloc(GFP_NOFS, sbio->count);
  639. if (!bio)
  640. goto nomem;
  641. bio->bi_private = sbio;
  642. bio->bi_end_io = scrub_bio_end_io;
  643. bio->bi_bdev = sdev->dev->bdev;
  644. bio->bi_sector = sbio->physical >> 9;
  645. for (i = 0; i < sbio->count; ++i) {
  646. struct page *page;
  647. int ret;
  648. page = alloc_page(GFP_NOFS);
  649. if (!page)
  650. goto nomem;
  651. ret = bio_add_page(bio, page, PAGE_SIZE, 0);
  652. if (!ret) {
  653. __free_page(page);
  654. goto nomem;
  655. }
  656. }
  657. sbio->err = 0;
  658. sdev->curr = -1;
  659. atomic_inc(&sdev->in_flight);
  660. submit_bio(READ, bio);
  661. return 0;
  662. nomem:
  663. scrub_free_bio(bio);
  664. return -ENOMEM;
  665. }
  666. static int scrub_page(struct scrub_dev *sdev, u64 logical, u64 len,
  667. u64 physical, u64 flags, u64 gen, u64 mirror_num,
  668. u8 *csum, int force)
  669. {
  670. struct scrub_bio *sbio;
  671. again:
  672. /*
  673. * grab a fresh bio or wait for one to become available
  674. */
  675. while (sdev->curr == -1) {
  676. spin_lock(&sdev->list_lock);
  677. sdev->curr = sdev->first_free;
  678. if (sdev->curr != -1) {
  679. sdev->first_free = sdev->bios[sdev->curr]->next_free;
  680. sdev->bios[sdev->curr]->next_free = -1;
  681. sdev->bios[sdev->curr]->count = 0;
  682. spin_unlock(&sdev->list_lock);
  683. } else {
  684. spin_unlock(&sdev->list_lock);
  685. wait_event(sdev->list_wait, sdev->first_free != -1);
  686. }
  687. }
  688. sbio = sdev->bios[sdev->curr];
  689. if (sbio->count == 0) {
  690. sbio->physical = physical;
  691. sbio->logical = logical;
  692. } else if (sbio->physical + sbio->count * PAGE_SIZE != physical ||
  693. sbio->logical + sbio->count * PAGE_SIZE != logical) {
  694. int ret;
  695. ret = scrub_submit(sdev);
  696. if (ret)
  697. return ret;
  698. goto again;
  699. }
  700. sbio->spag[sbio->count].flags = flags;
  701. sbio->spag[sbio->count].generation = gen;
  702. sbio->spag[sbio->count].have_csum = 0;
  703. sbio->spag[sbio->count].mirror_num = mirror_num;
  704. if (csum) {
  705. sbio->spag[sbio->count].have_csum = 1;
  706. memcpy(sbio->spag[sbio->count].csum, csum, sdev->csum_size);
  707. }
  708. ++sbio->count;
  709. if (sbio->count == SCRUB_PAGES_PER_BIO || force) {
  710. int ret;
  711. ret = scrub_submit(sdev);
  712. if (ret)
  713. return ret;
  714. }
  715. return 0;
  716. }
  717. static int scrub_find_csum(struct scrub_dev *sdev, u64 logical, u64 len,
  718. u8 *csum)
  719. {
  720. struct btrfs_ordered_sum *sum = NULL;
  721. int ret = 0;
  722. unsigned long i;
  723. unsigned long num_sectors;
  724. u32 sectorsize = sdev->dev->dev_root->sectorsize;
  725. while (!list_empty(&sdev->csum_list)) {
  726. sum = list_first_entry(&sdev->csum_list,
  727. struct btrfs_ordered_sum, list);
  728. if (sum->bytenr > logical)
  729. return 0;
  730. if (sum->bytenr + sum->len > logical)
  731. break;
  732. ++sdev->stat.csum_discards;
  733. list_del(&sum->list);
  734. kfree(sum);
  735. sum = NULL;
  736. }
  737. if (!sum)
  738. return 0;
  739. num_sectors = sum->len / sectorsize;
  740. for (i = 0; i < num_sectors; ++i) {
  741. if (sum->sums[i].bytenr == logical) {
  742. memcpy(csum, &sum->sums[i].sum, sdev->csum_size);
  743. ret = 1;
  744. break;
  745. }
  746. }
  747. if (ret && i == num_sectors - 1) {
  748. list_del(&sum->list);
  749. kfree(sum);
  750. }
  751. return ret;
  752. }
  753. /* scrub extent tries to collect up to 64 kB for each bio */
  754. static int scrub_extent(struct scrub_dev *sdev, u64 logical, u64 len,
  755. u64 physical, u64 flags, u64 gen, u64 mirror_num)
  756. {
  757. int ret;
  758. u8 csum[BTRFS_CSUM_SIZE];
  759. while (len) {
  760. u64 l = min_t(u64, len, PAGE_SIZE);
  761. int have_csum = 0;
  762. if (flags & BTRFS_EXTENT_FLAG_DATA) {
  763. /* push csums to sbio */
  764. have_csum = scrub_find_csum(sdev, logical, l, csum);
  765. if (have_csum == 0)
  766. ++sdev->stat.no_csum;
  767. }
  768. ret = scrub_page(sdev, logical, l, physical, flags, gen,
  769. mirror_num, have_csum ? csum : NULL, 0);
  770. if (ret)
  771. return ret;
  772. len -= l;
  773. logical += l;
  774. physical += l;
  775. }
  776. return 0;
  777. }
  778. static noinline_for_stack int scrub_stripe(struct scrub_dev *sdev,
  779. struct map_lookup *map, int num, u64 base, u64 length)
  780. {
  781. struct btrfs_path *path;
  782. struct btrfs_fs_info *fs_info = sdev->dev->dev_root->fs_info;
  783. struct btrfs_root *root = fs_info->extent_root;
  784. struct btrfs_root *csum_root = fs_info->csum_root;
  785. struct btrfs_extent_item *extent;
  786. struct blk_plug plug;
  787. u64 flags;
  788. int ret;
  789. int slot;
  790. int i;
  791. u64 nstripes;
  792. int start_stripe;
  793. struct extent_buffer *l;
  794. struct btrfs_key key;
  795. u64 physical;
  796. u64 logical;
  797. u64 generation;
  798. u64 mirror_num;
  799. u64 increment = map->stripe_len;
  800. u64 offset;
  801. nstripes = length;
  802. offset = 0;
  803. do_div(nstripes, map->stripe_len);
  804. if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
  805. offset = map->stripe_len * num;
  806. increment = map->stripe_len * map->num_stripes;
  807. mirror_num = 1;
  808. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  809. int factor = map->num_stripes / map->sub_stripes;
  810. offset = map->stripe_len * (num / map->sub_stripes);
  811. increment = map->stripe_len * factor;
  812. mirror_num = num % map->sub_stripes + 1;
  813. } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
  814. increment = map->stripe_len;
  815. mirror_num = num % map->num_stripes + 1;
  816. } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
  817. increment = map->stripe_len;
  818. mirror_num = num % map->num_stripes + 1;
  819. } else {
  820. increment = map->stripe_len;
  821. mirror_num = 1;
  822. }
  823. path = btrfs_alloc_path();
  824. if (!path)
  825. return -ENOMEM;
  826. path->reada = 2;
  827. path->search_commit_root = 1;
  828. path->skip_locking = 1;
  829. /*
  830. * find all extents for each stripe and just read them to get
  831. * them into the page cache
  832. * FIXME: we can do better. build a more intelligent prefetching
  833. */
  834. logical = base + offset;
  835. physical = map->stripes[num].physical;
  836. ret = 0;
  837. for (i = 0; i < nstripes; ++i) {
  838. key.objectid = logical;
  839. key.type = BTRFS_EXTENT_ITEM_KEY;
  840. key.offset = (u64)0;
  841. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  842. if (ret < 0)
  843. goto out_noplug;
  844. /*
  845. * we might miss half an extent here, but that doesn't matter,
  846. * as it's only the prefetch
  847. */
  848. while (1) {
  849. l = path->nodes[0];
  850. slot = path->slots[0];
  851. if (slot >= btrfs_header_nritems(l)) {
  852. ret = btrfs_next_leaf(root, path);
  853. if (ret == 0)
  854. continue;
  855. if (ret < 0)
  856. goto out_noplug;
  857. break;
  858. }
  859. btrfs_item_key_to_cpu(l, &key, slot);
  860. if (key.objectid >= logical + map->stripe_len)
  861. break;
  862. path->slots[0]++;
  863. }
  864. btrfs_release_path(path);
  865. logical += increment;
  866. physical += map->stripe_len;
  867. cond_resched();
  868. }
  869. /*
  870. * collect all data csums for the stripe to avoid seeking during
  871. * the scrub. This might currently (crc32) end up to be about 1MB
  872. */
  873. start_stripe = 0;
  874. blk_start_plug(&plug);
  875. again:
  876. logical = base + offset + start_stripe * increment;
  877. for (i = start_stripe; i < nstripes; ++i) {
  878. ret = btrfs_lookup_csums_range(csum_root, logical,
  879. logical + map->stripe_len - 1,
  880. &sdev->csum_list, 1);
  881. if (ret)
  882. goto out;
  883. logical += increment;
  884. cond_resched();
  885. }
  886. /*
  887. * now find all extents for each stripe and scrub them
  888. */
  889. logical = base + offset + start_stripe * increment;
  890. physical = map->stripes[num].physical + start_stripe * map->stripe_len;
  891. ret = 0;
  892. for (i = start_stripe; i < nstripes; ++i) {
  893. /*
  894. * canceled?
  895. */
  896. if (atomic_read(&fs_info->scrub_cancel_req) ||
  897. atomic_read(&sdev->cancel_req)) {
  898. ret = -ECANCELED;
  899. goto out;
  900. }
  901. /*
  902. * check to see if we have to pause
  903. */
  904. if (atomic_read(&fs_info->scrub_pause_req)) {
  905. /* push queued extents */
  906. scrub_submit(sdev);
  907. wait_event(sdev->list_wait,
  908. atomic_read(&sdev->in_flight) == 0);
  909. atomic_inc(&fs_info->scrubs_paused);
  910. wake_up(&fs_info->scrub_pause_wait);
  911. mutex_lock(&fs_info->scrub_lock);
  912. while (atomic_read(&fs_info->scrub_pause_req)) {
  913. mutex_unlock(&fs_info->scrub_lock);
  914. wait_event(fs_info->scrub_pause_wait,
  915. atomic_read(&fs_info->scrub_pause_req) == 0);
  916. mutex_lock(&fs_info->scrub_lock);
  917. }
  918. atomic_dec(&fs_info->scrubs_paused);
  919. mutex_unlock(&fs_info->scrub_lock);
  920. wake_up(&fs_info->scrub_pause_wait);
  921. scrub_free_csums(sdev);
  922. start_stripe = i;
  923. goto again;
  924. }
  925. key.objectid = logical;
  926. key.type = BTRFS_EXTENT_ITEM_KEY;
  927. key.offset = (u64)0;
  928. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  929. if (ret < 0)
  930. goto out;
  931. if (ret > 0) {
  932. ret = btrfs_previous_item(root, path, 0,
  933. BTRFS_EXTENT_ITEM_KEY);
  934. if (ret < 0)
  935. goto out;
  936. if (ret > 0) {
  937. /* there's no smaller item, so stick with the
  938. * larger one */
  939. btrfs_release_path(path);
  940. ret = btrfs_search_slot(NULL, root, &key,
  941. path, 0, 0);
  942. if (ret < 0)
  943. goto out;
  944. }
  945. }
  946. while (1) {
  947. l = path->nodes[0];
  948. slot = path->slots[0];
  949. if (slot >= btrfs_header_nritems(l)) {
  950. ret = btrfs_next_leaf(root, path);
  951. if (ret == 0)
  952. continue;
  953. if (ret < 0)
  954. goto out;
  955. break;
  956. }
  957. btrfs_item_key_to_cpu(l, &key, slot);
  958. if (key.objectid + key.offset <= logical)
  959. goto next;
  960. if (key.objectid >= logical + map->stripe_len)
  961. break;
  962. if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
  963. goto next;
  964. extent = btrfs_item_ptr(l, slot,
  965. struct btrfs_extent_item);
  966. flags = btrfs_extent_flags(l, extent);
  967. generation = btrfs_extent_generation(l, extent);
  968. if (key.objectid < logical &&
  969. (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
  970. printk(KERN_ERR
  971. "btrfs scrub: tree block %llu spanning "
  972. "stripes, ignored. logical=%llu\n",
  973. (unsigned long long)key.objectid,
  974. (unsigned long long)logical);
  975. goto next;
  976. }
  977. /*
  978. * trim extent to this stripe
  979. */
  980. if (key.objectid < logical) {
  981. key.offset -= logical - key.objectid;
  982. key.objectid = logical;
  983. }
  984. if (key.objectid + key.offset >
  985. logical + map->stripe_len) {
  986. key.offset = logical + map->stripe_len -
  987. key.objectid;
  988. }
  989. ret = scrub_extent(sdev, key.objectid, key.offset,
  990. key.objectid - logical + physical,
  991. flags, generation, mirror_num);
  992. if (ret)
  993. goto out;
  994. next:
  995. path->slots[0]++;
  996. }
  997. btrfs_release_path(path);
  998. logical += increment;
  999. physical += map->stripe_len;
  1000. spin_lock(&sdev->stat_lock);
  1001. sdev->stat.last_physical = physical;
  1002. spin_unlock(&sdev->stat_lock);
  1003. }
  1004. /* push queued extents */
  1005. scrub_submit(sdev);
  1006. out:
  1007. blk_finish_plug(&plug);
  1008. out_noplug:
  1009. btrfs_free_path(path);
  1010. return ret < 0 ? ret : 0;
  1011. }
  1012. static noinline_for_stack int scrub_chunk(struct scrub_dev *sdev,
  1013. u64 chunk_tree, u64 chunk_objectid, u64 chunk_offset, u64 length)
  1014. {
  1015. struct btrfs_mapping_tree *map_tree =
  1016. &sdev->dev->dev_root->fs_info->mapping_tree;
  1017. struct map_lookup *map;
  1018. struct extent_map *em;
  1019. int i;
  1020. int ret = -EINVAL;
  1021. read_lock(&map_tree->map_tree.lock);
  1022. em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
  1023. read_unlock(&map_tree->map_tree.lock);
  1024. if (!em)
  1025. return -EINVAL;
  1026. map = (struct map_lookup *)em->bdev;
  1027. if (em->start != chunk_offset)
  1028. goto out;
  1029. if (em->len < length)
  1030. goto out;
  1031. for (i = 0; i < map->num_stripes; ++i) {
  1032. if (map->stripes[i].dev == sdev->dev) {
  1033. ret = scrub_stripe(sdev, map, i, chunk_offset, length);
  1034. if (ret)
  1035. goto out;
  1036. }
  1037. }
  1038. out:
  1039. free_extent_map(em);
  1040. return ret;
  1041. }
  1042. static noinline_for_stack
  1043. int scrub_enumerate_chunks(struct scrub_dev *sdev, u64 start, u64 end)
  1044. {
  1045. struct btrfs_dev_extent *dev_extent = NULL;
  1046. struct btrfs_path *path;
  1047. struct btrfs_root *root = sdev->dev->dev_root;
  1048. struct btrfs_fs_info *fs_info = root->fs_info;
  1049. u64 length;
  1050. u64 chunk_tree;
  1051. u64 chunk_objectid;
  1052. u64 chunk_offset;
  1053. int ret;
  1054. int slot;
  1055. struct extent_buffer *l;
  1056. struct btrfs_key key;
  1057. struct btrfs_key found_key;
  1058. struct btrfs_block_group_cache *cache;
  1059. path = btrfs_alloc_path();
  1060. if (!path)
  1061. return -ENOMEM;
  1062. path->reada = 2;
  1063. path->search_commit_root = 1;
  1064. path->skip_locking = 1;
  1065. key.objectid = sdev->dev->devid;
  1066. key.offset = 0ull;
  1067. key.type = BTRFS_DEV_EXTENT_KEY;
  1068. while (1) {
  1069. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1070. if (ret < 0)
  1071. break;
  1072. if (ret > 0) {
  1073. if (path->slots[0] >=
  1074. btrfs_header_nritems(path->nodes[0])) {
  1075. ret = btrfs_next_leaf(root, path);
  1076. if (ret)
  1077. break;
  1078. }
  1079. }
  1080. l = path->nodes[0];
  1081. slot = path->slots[0];
  1082. btrfs_item_key_to_cpu(l, &found_key, slot);
  1083. if (found_key.objectid != sdev->dev->devid)
  1084. break;
  1085. if (btrfs_key_type(&found_key) != BTRFS_DEV_EXTENT_KEY)
  1086. break;
  1087. if (found_key.offset >= end)
  1088. break;
  1089. if (found_key.offset < key.offset)
  1090. break;
  1091. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  1092. length = btrfs_dev_extent_length(l, dev_extent);
  1093. if (found_key.offset + length <= start) {
  1094. key.offset = found_key.offset + length;
  1095. btrfs_release_path(path);
  1096. continue;
  1097. }
  1098. chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
  1099. chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
  1100. chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
  1101. /*
  1102. * get a reference on the corresponding block group to prevent
  1103. * the chunk from going away while we scrub it
  1104. */
  1105. cache = btrfs_lookup_block_group(fs_info, chunk_offset);
  1106. if (!cache) {
  1107. ret = -ENOENT;
  1108. break;
  1109. }
  1110. ret = scrub_chunk(sdev, chunk_tree, chunk_objectid,
  1111. chunk_offset, length);
  1112. btrfs_put_block_group(cache);
  1113. if (ret)
  1114. break;
  1115. key.offset = found_key.offset + length;
  1116. btrfs_release_path(path);
  1117. }
  1118. btrfs_free_path(path);
  1119. /*
  1120. * ret can still be 1 from search_slot or next_leaf,
  1121. * that's not an error
  1122. */
  1123. return ret < 0 ? ret : 0;
  1124. }
  1125. static noinline_for_stack int scrub_supers(struct scrub_dev *sdev)
  1126. {
  1127. int i;
  1128. u64 bytenr;
  1129. u64 gen;
  1130. int ret;
  1131. struct btrfs_device *device = sdev->dev;
  1132. struct btrfs_root *root = device->dev_root;
  1133. gen = root->fs_info->last_trans_committed;
  1134. for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
  1135. bytenr = btrfs_sb_offset(i);
  1136. if (bytenr + BTRFS_SUPER_INFO_SIZE >= device->total_bytes)
  1137. break;
  1138. ret = scrub_page(sdev, bytenr, PAGE_SIZE, bytenr,
  1139. BTRFS_EXTENT_FLAG_SUPER, gen, i, NULL, 1);
  1140. if (ret)
  1141. return ret;
  1142. }
  1143. wait_event(sdev->list_wait, atomic_read(&sdev->in_flight) == 0);
  1144. return 0;
  1145. }
  1146. /*
  1147. * get a reference count on fs_info->scrub_workers. start worker if necessary
  1148. */
  1149. static noinline_for_stack int scrub_workers_get(struct btrfs_root *root)
  1150. {
  1151. struct btrfs_fs_info *fs_info = root->fs_info;
  1152. mutex_lock(&fs_info->scrub_lock);
  1153. if (fs_info->scrub_workers_refcnt == 0) {
  1154. btrfs_init_workers(&fs_info->scrub_workers, "scrub",
  1155. fs_info->thread_pool_size, &fs_info->generic_worker);
  1156. fs_info->scrub_workers.idle_thresh = 4;
  1157. btrfs_start_workers(&fs_info->scrub_workers, 1);
  1158. }
  1159. ++fs_info->scrub_workers_refcnt;
  1160. mutex_unlock(&fs_info->scrub_lock);
  1161. return 0;
  1162. }
  1163. static noinline_for_stack void scrub_workers_put(struct btrfs_root *root)
  1164. {
  1165. struct btrfs_fs_info *fs_info = root->fs_info;
  1166. mutex_lock(&fs_info->scrub_lock);
  1167. if (--fs_info->scrub_workers_refcnt == 0)
  1168. btrfs_stop_workers(&fs_info->scrub_workers);
  1169. WARN_ON(fs_info->scrub_workers_refcnt < 0);
  1170. mutex_unlock(&fs_info->scrub_lock);
  1171. }
  1172. int btrfs_scrub_dev(struct btrfs_root *root, u64 devid, u64 start, u64 end,
  1173. struct btrfs_scrub_progress *progress, int readonly)
  1174. {
  1175. struct scrub_dev *sdev;
  1176. struct btrfs_fs_info *fs_info = root->fs_info;
  1177. int ret;
  1178. struct btrfs_device *dev;
  1179. if (btrfs_fs_closing(root->fs_info))
  1180. return -EINVAL;
  1181. /*
  1182. * check some assumptions
  1183. */
  1184. if (root->sectorsize != PAGE_SIZE ||
  1185. root->sectorsize != root->leafsize ||
  1186. root->sectorsize != root->nodesize) {
  1187. printk(KERN_ERR "btrfs_scrub: size assumptions fail\n");
  1188. return -EINVAL;
  1189. }
  1190. ret = scrub_workers_get(root);
  1191. if (ret)
  1192. return ret;
  1193. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  1194. dev = btrfs_find_device(root, devid, NULL, NULL);
  1195. if (!dev || dev->missing) {
  1196. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1197. scrub_workers_put(root);
  1198. return -ENODEV;
  1199. }
  1200. mutex_lock(&fs_info->scrub_lock);
  1201. if (!dev->in_fs_metadata) {
  1202. mutex_unlock(&fs_info->scrub_lock);
  1203. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1204. scrub_workers_put(root);
  1205. return -ENODEV;
  1206. }
  1207. if (dev->scrub_device) {
  1208. mutex_unlock(&fs_info->scrub_lock);
  1209. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1210. scrub_workers_put(root);
  1211. return -EINPROGRESS;
  1212. }
  1213. sdev = scrub_setup_dev(dev);
  1214. if (IS_ERR(sdev)) {
  1215. mutex_unlock(&fs_info->scrub_lock);
  1216. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1217. scrub_workers_put(root);
  1218. return PTR_ERR(sdev);
  1219. }
  1220. sdev->readonly = readonly;
  1221. dev->scrub_device = sdev;
  1222. atomic_inc(&fs_info->scrubs_running);
  1223. mutex_unlock(&fs_info->scrub_lock);
  1224. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1225. down_read(&fs_info->scrub_super_lock);
  1226. ret = scrub_supers(sdev);
  1227. up_read(&fs_info->scrub_super_lock);
  1228. if (!ret)
  1229. ret = scrub_enumerate_chunks(sdev, start, end);
  1230. wait_event(sdev->list_wait, atomic_read(&sdev->in_flight) == 0);
  1231. atomic_dec(&fs_info->scrubs_running);
  1232. wake_up(&fs_info->scrub_pause_wait);
  1233. if (progress)
  1234. memcpy(progress, &sdev->stat, sizeof(*progress));
  1235. mutex_lock(&fs_info->scrub_lock);
  1236. dev->scrub_device = NULL;
  1237. mutex_unlock(&fs_info->scrub_lock);
  1238. scrub_free_dev(sdev);
  1239. scrub_workers_put(root);
  1240. return ret;
  1241. }
  1242. int btrfs_scrub_pause(struct btrfs_root *root)
  1243. {
  1244. struct btrfs_fs_info *fs_info = root->fs_info;
  1245. mutex_lock(&fs_info->scrub_lock);
  1246. atomic_inc(&fs_info->scrub_pause_req);
  1247. while (atomic_read(&fs_info->scrubs_paused) !=
  1248. atomic_read(&fs_info->scrubs_running)) {
  1249. mutex_unlock(&fs_info->scrub_lock);
  1250. wait_event(fs_info->scrub_pause_wait,
  1251. atomic_read(&fs_info->scrubs_paused) ==
  1252. atomic_read(&fs_info->scrubs_running));
  1253. mutex_lock(&fs_info->scrub_lock);
  1254. }
  1255. mutex_unlock(&fs_info->scrub_lock);
  1256. return 0;
  1257. }
  1258. int btrfs_scrub_continue(struct btrfs_root *root)
  1259. {
  1260. struct btrfs_fs_info *fs_info = root->fs_info;
  1261. atomic_dec(&fs_info->scrub_pause_req);
  1262. wake_up(&fs_info->scrub_pause_wait);
  1263. return 0;
  1264. }
  1265. int btrfs_scrub_pause_super(struct btrfs_root *root)
  1266. {
  1267. down_write(&root->fs_info->scrub_super_lock);
  1268. return 0;
  1269. }
  1270. int btrfs_scrub_continue_super(struct btrfs_root *root)
  1271. {
  1272. up_write(&root->fs_info->scrub_super_lock);
  1273. return 0;
  1274. }
  1275. int btrfs_scrub_cancel(struct btrfs_root *root)
  1276. {
  1277. struct btrfs_fs_info *fs_info = root->fs_info;
  1278. mutex_lock(&fs_info->scrub_lock);
  1279. if (!atomic_read(&fs_info->scrubs_running)) {
  1280. mutex_unlock(&fs_info->scrub_lock);
  1281. return -ENOTCONN;
  1282. }
  1283. atomic_inc(&fs_info->scrub_cancel_req);
  1284. while (atomic_read(&fs_info->scrubs_running)) {
  1285. mutex_unlock(&fs_info->scrub_lock);
  1286. wait_event(fs_info->scrub_pause_wait,
  1287. atomic_read(&fs_info->scrubs_running) == 0);
  1288. mutex_lock(&fs_info->scrub_lock);
  1289. }
  1290. atomic_dec(&fs_info->scrub_cancel_req);
  1291. mutex_unlock(&fs_info->scrub_lock);
  1292. return 0;
  1293. }
  1294. int btrfs_scrub_cancel_dev(struct btrfs_root *root, struct btrfs_device *dev)
  1295. {
  1296. struct btrfs_fs_info *fs_info = root->fs_info;
  1297. struct scrub_dev *sdev;
  1298. mutex_lock(&fs_info->scrub_lock);
  1299. sdev = dev->scrub_device;
  1300. if (!sdev) {
  1301. mutex_unlock(&fs_info->scrub_lock);
  1302. return -ENOTCONN;
  1303. }
  1304. atomic_inc(&sdev->cancel_req);
  1305. while (dev->scrub_device) {
  1306. mutex_unlock(&fs_info->scrub_lock);
  1307. wait_event(fs_info->scrub_pause_wait,
  1308. dev->scrub_device == NULL);
  1309. mutex_lock(&fs_info->scrub_lock);
  1310. }
  1311. mutex_unlock(&fs_info->scrub_lock);
  1312. return 0;
  1313. }
  1314. int btrfs_scrub_cancel_devid(struct btrfs_root *root, u64 devid)
  1315. {
  1316. struct btrfs_fs_info *fs_info = root->fs_info;
  1317. struct btrfs_device *dev;
  1318. int ret;
  1319. /*
  1320. * we have to hold the device_list_mutex here so the device
  1321. * does not go away in cancel_dev. FIXME: find a better solution
  1322. */
  1323. mutex_lock(&fs_info->fs_devices->device_list_mutex);
  1324. dev = btrfs_find_device(root, devid, NULL, NULL);
  1325. if (!dev) {
  1326. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  1327. return -ENODEV;
  1328. }
  1329. ret = btrfs_scrub_cancel_dev(root, dev);
  1330. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  1331. return ret;
  1332. }
  1333. int btrfs_scrub_progress(struct btrfs_root *root, u64 devid,
  1334. struct btrfs_scrub_progress *progress)
  1335. {
  1336. struct btrfs_device *dev;
  1337. struct scrub_dev *sdev = NULL;
  1338. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  1339. dev = btrfs_find_device(root, devid, NULL, NULL);
  1340. if (dev)
  1341. sdev = dev->scrub_device;
  1342. if (sdev)
  1343. memcpy(progress, &sdev->stat, sizeof(*progress));
  1344. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1345. return dev ? (sdev ? 0 : -ENOTCONN) : -ENODEV;
  1346. }