scrub.c 44 KB

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