scrub.c 44 KB

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