scrub.c 44 KB

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