scrub.c 43 KB

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