scrub.c 44 KB

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