scrub.c 44 KB

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