scrub.c 44 KB

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