scrub.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441
  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. * - track and record media errors, throw out bad devices
  38. * - add a mode to also read unallocated space
  39. */
  40. struct scrub_block;
  41. struct scrub_dev;
  42. #define SCRUB_PAGES_PER_BIO 16 /* 64k per bio */
  43. #define SCRUB_BIOS_PER_DEV 16 /* 1 MB per device in flight */
  44. #define SCRUB_MAX_PAGES_PER_BLOCK 16 /* 64k per node/leaf/sector */
  45. struct scrub_page {
  46. struct scrub_block *sblock;
  47. struct page *page;
  48. struct block_device *bdev;
  49. u64 flags; /* extent flags */
  50. u64 generation;
  51. u64 logical;
  52. u64 physical;
  53. struct {
  54. unsigned int mirror_num:8;
  55. unsigned int have_csum:1;
  56. unsigned int io_error:1;
  57. };
  58. u8 csum[BTRFS_CSUM_SIZE];
  59. };
  60. struct scrub_bio {
  61. int index;
  62. struct scrub_dev *sdev;
  63. struct bio *bio;
  64. int err;
  65. u64 logical;
  66. u64 physical;
  67. struct scrub_page *pagev[SCRUB_PAGES_PER_BIO];
  68. int page_count;
  69. int next_free;
  70. struct btrfs_work work;
  71. };
  72. struct scrub_block {
  73. struct scrub_page pagev[SCRUB_MAX_PAGES_PER_BLOCK];
  74. int page_count;
  75. atomic_t outstanding_pages;
  76. atomic_t ref_count; /* free mem on transition to zero */
  77. struct scrub_dev *sdev;
  78. struct {
  79. unsigned int header_error:1;
  80. unsigned int checksum_error:1;
  81. unsigned int no_io_error_seen:1;
  82. };
  83. };
  84. struct scrub_dev {
  85. struct scrub_bio *bios[SCRUB_BIOS_PER_DEV];
  86. struct btrfs_device *dev;
  87. int first_free;
  88. int curr;
  89. atomic_t in_flight;
  90. atomic_t fixup_cnt;
  91. spinlock_t list_lock;
  92. wait_queue_head_t list_wait;
  93. u16 csum_size;
  94. struct list_head csum_list;
  95. atomic_t cancel_req;
  96. int readonly;
  97. int pages_per_bio; /* <= SCRUB_PAGES_PER_BIO */
  98. u32 sectorsize;
  99. u32 nodesize;
  100. u32 leafsize;
  101. /*
  102. * statistics
  103. */
  104. struct btrfs_scrub_progress stat;
  105. spinlock_t stat_lock;
  106. };
  107. struct scrub_fixup_nodatasum {
  108. struct scrub_dev *sdev;
  109. u64 logical;
  110. struct btrfs_root *root;
  111. struct btrfs_work work;
  112. int mirror_num;
  113. };
  114. struct scrub_warning {
  115. struct btrfs_path *path;
  116. u64 extent_item_size;
  117. char *scratch_buf;
  118. char *msg_buf;
  119. const char *errstr;
  120. sector_t sector;
  121. u64 logical;
  122. struct btrfs_device *dev;
  123. int msg_bufsize;
  124. int scratch_bufsize;
  125. };
  126. static int scrub_handle_errored_block(struct scrub_block *sblock_to_check);
  127. static int scrub_setup_recheck_block(struct scrub_dev *sdev,
  128. struct btrfs_mapping_tree *map_tree,
  129. u64 length, u64 logical,
  130. struct scrub_block *sblock);
  131. static int scrub_recheck_block(struct btrfs_fs_info *fs_info,
  132. struct scrub_block *sblock, int is_metadata,
  133. int have_csum, u8 *csum, u64 generation,
  134. u16 csum_size);
  135. static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
  136. struct scrub_block *sblock,
  137. int is_metadata, int have_csum,
  138. const u8 *csum, u64 generation,
  139. u16 csum_size);
  140. static void scrub_complete_bio_end_io(struct bio *bio, int err);
  141. static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
  142. struct scrub_block *sblock_good,
  143. int force_write);
  144. static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
  145. struct scrub_block *sblock_good,
  146. int page_num, int force_write);
  147. static int scrub_checksum_data(struct scrub_block *sblock);
  148. static int scrub_checksum_tree_block(struct scrub_block *sblock);
  149. static int scrub_checksum_super(struct scrub_block *sblock);
  150. static void scrub_block_get(struct scrub_block *sblock);
  151. static void scrub_block_put(struct scrub_block *sblock);
  152. static int scrub_add_page_to_bio(struct scrub_dev *sdev,
  153. struct scrub_page *spage);
  154. static int scrub_pages(struct scrub_dev *sdev, u64 logical, u64 len,
  155. u64 physical, u64 flags, u64 gen, int mirror_num,
  156. u8 *csum, int force);
  157. static void scrub_bio_end_io(struct bio *bio, int err);
  158. static void scrub_bio_end_io_worker(struct btrfs_work *work);
  159. static void scrub_block_complete(struct scrub_block *sblock);
  160. static void scrub_free_csums(struct scrub_dev *sdev)
  161. {
  162. while (!list_empty(&sdev->csum_list)) {
  163. struct btrfs_ordered_sum *sum;
  164. sum = list_first_entry(&sdev->csum_list,
  165. struct btrfs_ordered_sum, list);
  166. list_del(&sum->list);
  167. kfree(sum);
  168. }
  169. }
  170. static noinline_for_stack void scrub_free_dev(struct scrub_dev *sdev)
  171. {
  172. int i;
  173. if (!sdev)
  174. return;
  175. /* this can happen when scrub is cancelled */
  176. if (sdev->curr != -1) {
  177. struct scrub_bio *sbio = sdev->bios[sdev->curr];
  178. for (i = 0; i < sbio->page_count; i++) {
  179. BUG_ON(!sbio->pagev[i]);
  180. BUG_ON(!sbio->pagev[i]->page);
  181. scrub_block_put(sbio->pagev[i]->sblock);
  182. }
  183. bio_put(sbio->bio);
  184. }
  185. for (i = 0; i < SCRUB_BIOS_PER_DEV; ++i) {
  186. struct scrub_bio *sbio = sdev->bios[i];
  187. if (!sbio)
  188. break;
  189. kfree(sbio);
  190. }
  191. scrub_free_csums(sdev);
  192. kfree(sdev);
  193. }
  194. static noinline_for_stack
  195. struct scrub_dev *scrub_setup_dev(struct btrfs_device *dev)
  196. {
  197. struct scrub_dev *sdev;
  198. int i;
  199. struct btrfs_fs_info *fs_info = dev->dev_root->fs_info;
  200. int pages_per_bio;
  201. pages_per_bio = min_t(int, SCRUB_PAGES_PER_BIO,
  202. bio_get_nr_vecs(dev->bdev));
  203. sdev = kzalloc(sizeof(*sdev), GFP_NOFS);
  204. if (!sdev)
  205. goto nomem;
  206. sdev->dev = dev;
  207. sdev->pages_per_bio = pages_per_bio;
  208. sdev->curr = -1;
  209. for (i = 0; i < SCRUB_BIOS_PER_DEV; ++i) {
  210. struct scrub_bio *sbio;
  211. sbio = kzalloc(sizeof(*sbio), GFP_NOFS);
  212. if (!sbio)
  213. goto nomem;
  214. sdev->bios[i] = sbio;
  215. sbio->index = i;
  216. sbio->sdev = sdev;
  217. sbio->page_count = 0;
  218. sbio->work.func = scrub_bio_end_io_worker;
  219. if (i != SCRUB_BIOS_PER_DEV-1)
  220. sdev->bios[i]->next_free = i + 1;
  221. else
  222. sdev->bios[i]->next_free = -1;
  223. }
  224. sdev->first_free = 0;
  225. sdev->nodesize = dev->dev_root->nodesize;
  226. sdev->leafsize = dev->dev_root->leafsize;
  227. sdev->sectorsize = dev->dev_root->sectorsize;
  228. atomic_set(&sdev->in_flight, 0);
  229. atomic_set(&sdev->fixup_cnt, 0);
  230. atomic_set(&sdev->cancel_req, 0);
  231. sdev->csum_size = btrfs_super_csum_size(fs_info->super_copy);
  232. INIT_LIST_HEAD(&sdev->csum_list);
  233. spin_lock_init(&sdev->list_lock);
  234. spin_lock_init(&sdev->stat_lock);
  235. init_waitqueue_head(&sdev->list_wait);
  236. return sdev;
  237. nomem:
  238. scrub_free_dev(sdev);
  239. return ERR_PTR(-ENOMEM);
  240. }
  241. static int scrub_print_warning_inode(u64 inum, u64 offset, u64 root, void *ctx)
  242. {
  243. u64 isize;
  244. u32 nlink;
  245. int ret;
  246. int i;
  247. struct extent_buffer *eb;
  248. struct btrfs_inode_item *inode_item;
  249. struct scrub_warning *swarn = ctx;
  250. struct btrfs_fs_info *fs_info = swarn->dev->dev_root->fs_info;
  251. struct inode_fs_paths *ipath = NULL;
  252. struct btrfs_root *local_root;
  253. struct btrfs_key root_key;
  254. root_key.objectid = root;
  255. root_key.type = BTRFS_ROOT_ITEM_KEY;
  256. root_key.offset = (u64)-1;
  257. local_root = btrfs_read_fs_root_no_name(fs_info, &root_key);
  258. if (IS_ERR(local_root)) {
  259. ret = PTR_ERR(local_root);
  260. goto err;
  261. }
  262. ret = inode_item_info(inum, 0, local_root, swarn->path);
  263. if (ret) {
  264. btrfs_release_path(swarn->path);
  265. goto err;
  266. }
  267. eb = swarn->path->nodes[0];
  268. inode_item = btrfs_item_ptr(eb, swarn->path->slots[0],
  269. struct btrfs_inode_item);
  270. isize = btrfs_inode_size(eb, inode_item);
  271. nlink = btrfs_inode_nlink(eb, inode_item);
  272. btrfs_release_path(swarn->path);
  273. ipath = init_ipath(4096, local_root, swarn->path);
  274. if (IS_ERR(ipath)) {
  275. ret = PTR_ERR(ipath);
  276. ipath = NULL;
  277. goto err;
  278. }
  279. ret = paths_from_inode(inum, ipath);
  280. if (ret < 0)
  281. goto err;
  282. /*
  283. * we deliberately ignore the bit ipath might have been too small to
  284. * hold all of the paths here
  285. */
  286. for (i = 0; i < ipath->fspath->elem_cnt; ++i)
  287. printk(KERN_WARNING "btrfs: %s at logical %llu on dev "
  288. "%s, sector %llu, root %llu, inode %llu, offset %llu, "
  289. "length %llu, links %u (path: %s)\n", swarn->errstr,
  290. swarn->logical, swarn->dev->name,
  291. (unsigned long long)swarn->sector, root, inum, offset,
  292. min(isize - offset, (u64)PAGE_SIZE), nlink,
  293. (char *)(unsigned long)ipath->fspath->val[i]);
  294. free_ipath(ipath);
  295. return 0;
  296. err:
  297. printk(KERN_WARNING "btrfs: %s at logical %llu on dev "
  298. "%s, sector %llu, root %llu, inode %llu, offset %llu: path "
  299. "resolving failed with ret=%d\n", swarn->errstr,
  300. swarn->logical, swarn->dev->name,
  301. (unsigned long long)swarn->sector, root, inum, offset, ret);
  302. free_ipath(ipath);
  303. return 0;
  304. }
  305. static void scrub_print_warning(const char *errstr, struct scrub_block *sblock)
  306. {
  307. struct btrfs_device *dev = sblock->sdev->dev;
  308. struct btrfs_fs_info *fs_info = dev->dev_root->fs_info;
  309. struct btrfs_path *path;
  310. struct btrfs_key found_key;
  311. struct extent_buffer *eb;
  312. struct btrfs_extent_item *ei;
  313. struct scrub_warning swarn;
  314. u32 item_size;
  315. int ret;
  316. u64 ref_root;
  317. u8 ref_level;
  318. unsigned long ptr = 0;
  319. const int bufsize = 4096;
  320. u64 extent_item_pos;
  321. path = btrfs_alloc_path();
  322. swarn.scratch_buf = kmalloc(bufsize, GFP_NOFS);
  323. swarn.msg_buf = kmalloc(bufsize, GFP_NOFS);
  324. BUG_ON(sblock->page_count < 1);
  325. swarn.sector = (sblock->pagev[0].physical) >> 9;
  326. swarn.logical = sblock->pagev[0].logical;
  327. swarn.errstr = errstr;
  328. swarn.dev = dev;
  329. swarn.msg_bufsize = bufsize;
  330. swarn.scratch_bufsize = bufsize;
  331. if (!path || !swarn.scratch_buf || !swarn.msg_buf)
  332. goto out;
  333. ret = extent_from_logical(fs_info, swarn.logical, path, &found_key);
  334. if (ret < 0)
  335. goto out;
  336. extent_item_pos = swarn.logical - found_key.objectid;
  337. swarn.extent_item_size = found_key.offset;
  338. eb = path->nodes[0];
  339. ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
  340. item_size = btrfs_item_size_nr(eb, path->slots[0]);
  341. btrfs_release_path(path);
  342. if (ret & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  343. do {
  344. ret = tree_backref_for_extent(&ptr, eb, ei, item_size,
  345. &ref_root, &ref_level);
  346. printk(KERN_WARNING
  347. "btrfs: %s at logical %llu on dev %s, "
  348. "sector %llu: metadata %s (level %d) in tree "
  349. "%llu\n", errstr, swarn.logical, dev->name,
  350. (unsigned long long)swarn.sector,
  351. ref_level ? "node" : "leaf",
  352. ret < 0 ? -1 : ref_level,
  353. ret < 0 ? -1 : ref_root);
  354. } while (ret != 1);
  355. } else {
  356. swarn.path = path;
  357. iterate_extent_inodes(fs_info, found_key.objectid,
  358. extent_item_pos, 1,
  359. scrub_print_warning_inode, &swarn);
  360. }
  361. out:
  362. btrfs_free_path(path);
  363. kfree(swarn.scratch_buf);
  364. kfree(swarn.msg_buf);
  365. }
  366. static int scrub_fixup_readpage(u64 inum, u64 offset, u64 root, void *ctx)
  367. {
  368. struct page *page = NULL;
  369. unsigned long index;
  370. struct scrub_fixup_nodatasum *fixup = ctx;
  371. int ret;
  372. int corrected = 0;
  373. struct btrfs_key key;
  374. struct inode *inode = NULL;
  375. u64 end = offset + PAGE_SIZE - 1;
  376. struct btrfs_root *local_root;
  377. key.objectid = root;
  378. key.type = BTRFS_ROOT_ITEM_KEY;
  379. key.offset = (u64)-1;
  380. local_root = btrfs_read_fs_root_no_name(fixup->root->fs_info, &key);
  381. if (IS_ERR(local_root))
  382. return PTR_ERR(local_root);
  383. key.type = BTRFS_INODE_ITEM_KEY;
  384. key.objectid = inum;
  385. key.offset = 0;
  386. inode = btrfs_iget(fixup->root->fs_info->sb, &key, local_root, NULL);
  387. if (IS_ERR(inode))
  388. return PTR_ERR(inode);
  389. index = offset >> PAGE_CACHE_SHIFT;
  390. page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
  391. if (!page) {
  392. ret = -ENOMEM;
  393. goto out;
  394. }
  395. if (PageUptodate(page)) {
  396. struct btrfs_mapping_tree *map_tree;
  397. if (PageDirty(page)) {
  398. /*
  399. * we need to write the data to the defect sector. the
  400. * data that was in that sector is not in memory,
  401. * because the page was modified. we must not write the
  402. * modified page to that sector.
  403. *
  404. * TODO: what could be done here: wait for the delalloc
  405. * runner to write out that page (might involve
  406. * COW) and see whether the sector is still
  407. * referenced afterwards.
  408. *
  409. * For the meantime, we'll treat this error
  410. * incorrectable, although there is a chance that a
  411. * later scrub will find the bad sector again and that
  412. * there's no dirty page in memory, then.
  413. */
  414. ret = -EIO;
  415. goto out;
  416. }
  417. map_tree = &BTRFS_I(inode)->root->fs_info->mapping_tree;
  418. ret = repair_io_failure(map_tree, offset, PAGE_SIZE,
  419. fixup->logical, page,
  420. fixup->mirror_num);
  421. unlock_page(page);
  422. corrected = !ret;
  423. } else {
  424. /*
  425. * we need to get good data first. the general readpage path
  426. * will call repair_io_failure for us, we just have to make
  427. * sure we read the bad mirror.
  428. */
  429. ret = set_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
  430. EXTENT_DAMAGED, GFP_NOFS);
  431. if (ret) {
  432. /* set_extent_bits should give proper error */
  433. WARN_ON(ret > 0);
  434. if (ret > 0)
  435. ret = -EFAULT;
  436. goto out;
  437. }
  438. ret = extent_read_full_page(&BTRFS_I(inode)->io_tree, page,
  439. btrfs_get_extent,
  440. fixup->mirror_num);
  441. wait_on_page_locked(page);
  442. corrected = !test_range_bit(&BTRFS_I(inode)->io_tree, offset,
  443. end, EXTENT_DAMAGED, 0, NULL);
  444. if (!corrected)
  445. clear_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
  446. EXTENT_DAMAGED, GFP_NOFS);
  447. }
  448. out:
  449. if (page)
  450. put_page(page);
  451. if (inode)
  452. iput(inode);
  453. if (ret < 0)
  454. return ret;
  455. if (ret == 0 && corrected) {
  456. /*
  457. * we only need to call readpage for one of the inodes belonging
  458. * to this extent. so make iterate_extent_inodes stop
  459. */
  460. return 1;
  461. }
  462. return -EIO;
  463. }
  464. static void scrub_fixup_nodatasum(struct btrfs_work *work)
  465. {
  466. int ret;
  467. struct scrub_fixup_nodatasum *fixup;
  468. struct scrub_dev *sdev;
  469. struct btrfs_trans_handle *trans = NULL;
  470. struct btrfs_fs_info *fs_info;
  471. struct btrfs_path *path;
  472. int uncorrectable = 0;
  473. fixup = container_of(work, struct scrub_fixup_nodatasum, work);
  474. sdev = fixup->sdev;
  475. fs_info = fixup->root->fs_info;
  476. path = btrfs_alloc_path();
  477. if (!path) {
  478. spin_lock(&sdev->stat_lock);
  479. ++sdev->stat.malloc_errors;
  480. spin_unlock(&sdev->stat_lock);
  481. uncorrectable = 1;
  482. goto out;
  483. }
  484. trans = btrfs_join_transaction(fixup->root);
  485. if (IS_ERR(trans)) {
  486. uncorrectable = 1;
  487. goto out;
  488. }
  489. /*
  490. * the idea is to trigger a regular read through the standard path. we
  491. * read a page from the (failed) logical address by specifying the
  492. * corresponding copynum of the failed sector. thus, that readpage is
  493. * expected to fail.
  494. * that is the point where on-the-fly error correction will kick in
  495. * (once it's finished) and rewrite the failed sector if a good copy
  496. * can be found.
  497. */
  498. ret = iterate_inodes_from_logical(fixup->logical, fixup->root->fs_info,
  499. path, scrub_fixup_readpage,
  500. fixup);
  501. if (ret < 0) {
  502. uncorrectable = 1;
  503. goto out;
  504. }
  505. WARN_ON(ret != 1);
  506. spin_lock(&sdev->stat_lock);
  507. ++sdev->stat.corrected_errors;
  508. spin_unlock(&sdev->stat_lock);
  509. out:
  510. if (trans && !IS_ERR(trans))
  511. btrfs_end_transaction(trans, fixup->root);
  512. if (uncorrectable) {
  513. spin_lock(&sdev->stat_lock);
  514. ++sdev->stat.uncorrectable_errors;
  515. spin_unlock(&sdev->stat_lock);
  516. printk_ratelimited(KERN_ERR
  517. "btrfs: unable to fixup (nodatasum) error at logical %llu on dev %s\n",
  518. (unsigned long long)fixup->logical, sdev->dev->name);
  519. }
  520. btrfs_free_path(path);
  521. kfree(fixup);
  522. /* see caller why we're pretending to be paused in the scrub counters */
  523. mutex_lock(&fs_info->scrub_lock);
  524. atomic_dec(&fs_info->scrubs_running);
  525. atomic_dec(&fs_info->scrubs_paused);
  526. mutex_unlock(&fs_info->scrub_lock);
  527. atomic_dec(&sdev->fixup_cnt);
  528. wake_up(&fs_info->scrub_pause_wait);
  529. wake_up(&sdev->list_wait);
  530. }
  531. /*
  532. * scrub_handle_errored_block gets called when either verification of the
  533. * pages failed or the bio failed to read, e.g. with EIO. In the latter
  534. * case, this function handles all pages in the bio, even though only one
  535. * may be bad.
  536. * The goal of this function is to repair the errored block by using the
  537. * contents of one of the mirrors.
  538. */
  539. static int scrub_handle_errored_block(struct scrub_block *sblock_to_check)
  540. {
  541. struct scrub_dev *sdev = sblock_to_check->sdev;
  542. struct btrfs_fs_info *fs_info;
  543. u64 length;
  544. u64 logical;
  545. u64 generation;
  546. unsigned int failed_mirror_index;
  547. unsigned int is_metadata;
  548. unsigned int have_csum;
  549. u8 *csum;
  550. struct scrub_block *sblocks_for_recheck; /* holds one for each mirror */
  551. struct scrub_block *sblock_bad;
  552. int ret;
  553. int mirror_index;
  554. int page_num;
  555. int success;
  556. static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
  557. DEFAULT_RATELIMIT_BURST);
  558. BUG_ON(sblock_to_check->page_count < 1);
  559. fs_info = sdev->dev->dev_root->fs_info;
  560. length = sblock_to_check->page_count * PAGE_SIZE;
  561. logical = sblock_to_check->pagev[0].logical;
  562. generation = sblock_to_check->pagev[0].generation;
  563. BUG_ON(sblock_to_check->pagev[0].mirror_num < 1);
  564. failed_mirror_index = sblock_to_check->pagev[0].mirror_num - 1;
  565. is_metadata = !(sblock_to_check->pagev[0].flags &
  566. BTRFS_EXTENT_FLAG_DATA);
  567. have_csum = sblock_to_check->pagev[0].have_csum;
  568. csum = sblock_to_check->pagev[0].csum;
  569. /*
  570. * read all mirrors one after the other. This includes to
  571. * re-read the extent or metadata block that failed (that was
  572. * the cause that this fixup code is called) another time,
  573. * page by page this time in order to know which pages
  574. * caused I/O errors and which ones are good (for all mirrors).
  575. * It is the goal to handle the situation when more than one
  576. * mirror contains I/O errors, but the errors do not
  577. * overlap, i.e. the data can be repaired by selecting the
  578. * pages from those mirrors without I/O error on the
  579. * particular pages. One example (with blocks >= 2 * PAGE_SIZE)
  580. * would be that mirror #1 has an I/O error on the first page,
  581. * the second page is good, and mirror #2 has an I/O error on
  582. * the second page, but the first page is good.
  583. * Then the first page of the first mirror can be repaired by
  584. * taking the first page of the second mirror, and the
  585. * second page of the second mirror can be repaired by
  586. * copying the contents of the 2nd page of the 1st mirror.
  587. * One more note: if the pages of one mirror contain I/O
  588. * errors, the checksum cannot be verified. In order to get
  589. * the best data for repairing, the first attempt is to find
  590. * a mirror without I/O errors and with a validated checksum.
  591. * Only if this is not possible, the pages are picked from
  592. * mirrors with I/O errors without considering the checksum.
  593. * If the latter is the case, at the end, the checksum of the
  594. * repaired area is verified in order to correctly maintain
  595. * the statistics.
  596. */
  597. sblocks_for_recheck = kzalloc(BTRFS_MAX_MIRRORS *
  598. sizeof(*sblocks_for_recheck),
  599. GFP_NOFS);
  600. if (!sblocks_for_recheck) {
  601. spin_lock(&sdev->stat_lock);
  602. sdev->stat.malloc_errors++;
  603. sdev->stat.read_errors++;
  604. sdev->stat.uncorrectable_errors++;
  605. spin_unlock(&sdev->stat_lock);
  606. goto out;
  607. }
  608. /* setup the context, map the logical blocks and alloc the pages */
  609. ret = scrub_setup_recheck_block(sdev, &fs_info->mapping_tree, length,
  610. logical, sblocks_for_recheck);
  611. if (ret) {
  612. spin_lock(&sdev->stat_lock);
  613. sdev->stat.read_errors++;
  614. sdev->stat.uncorrectable_errors++;
  615. spin_unlock(&sdev->stat_lock);
  616. goto out;
  617. }
  618. BUG_ON(failed_mirror_index >= BTRFS_MAX_MIRRORS);
  619. sblock_bad = sblocks_for_recheck + failed_mirror_index;
  620. /* build and submit the bios for the failed mirror, check checksums */
  621. ret = scrub_recheck_block(fs_info, sblock_bad, is_metadata, have_csum,
  622. csum, generation, sdev->csum_size);
  623. if (ret) {
  624. spin_lock(&sdev->stat_lock);
  625. sdev->stat.read_errors++;
  626. sdev->stat.uncorrectable_errors++;
  627. spin_unlock(&sdev->stat_lock);
  628. goto out;
  629. }
  630. if (!sblock_bad->header_error && !sblock_bad->checksum_error &&
  631. sblock_bad->no_io_error_seen) {
  632. /*
  633. * the error disappeared after reading page by page, or
  634. * the area was part of a huge bio and other parts of the
  635. * bio caused I/O errors, or the block layer merged several
  636. * read requests into one and the error is caused by a
  637. * different bio (usually one of the two latter cases is
  638. * the cause)
  639. */
  640. spin_lock(&sdev->stat_lock);
  641. sdev->stat.unverified_errors++;
  642. spin_unlock(&sdev->stat_lock);
  643. goto out;
  644. }
  645. if (!sblock_bad->no_io_error_seen) {
  646. spin_lock(&sdev->stat_lock);
  647. sdev->stat.read_errors++;
  648. spin_unlock(&sdev->stat_lock);
  649. if (__ratelimit(&_rs))
  650. scrub_print_warning("i/o error", sblock_to_check);
  651. } else if (sblock_bad->checksum_error) {
  652. spin_lock(&sdev->stat_lock);
  653. sdev->stat.csum_errors++;
  654. spin_unlock(&sdev->stat_lock);
  655. if (__ratelimit(&_rs))
  656. scrub_print_warning("checksum error", sblock_to_check);
  657. } else if (sblock_bad->header_error) {
  658. spin_lock(&sdev->stat_lock);
  659. sdev->stat.verify_errors++;
  660. spin_unlock(&sdev->stat_lock);
  661. if (__ratelimit(&_rs))
  662. scrub_print_warning("checksum/header error",
  663. sblock_to_check);
  664. }
  665. if (sdev->readonly)
  666. goto did_not_correct_error;
  667. if (!is_metadata && !have_csum) {
  668. struct scrub_fixup_nodatasum *fixup_nodatasum;
  669. /*
  670. * !is_metadata and !have_csum, this means that the data
  671. * might not be COW'ed, that it might be modified
  672. * concurrently. The general strategy to work on the
  673. * commit root does not help in the case when COW is not
  674. * used.
  675. */
  676. fixup_nodatasum = kzalloc(sizeof(*fixup_nodatasum), GFP_NOFS);
  677. if (!fixup_nodatasum)
  678. goto did_not_correct_error;
  679. fixup_nodatasum->sdev = sdev;
  680. fixup_nodatasum->logical = logical;
  681. fixup_nodatasum->root = fs_info->extent_root;
  682. fixup_nodatasum->mirror_num = failed_mirror_index + 1;
  683. /*
  684. * increment scrubs_running to prevent cancel requests from
  685. * completing as long as a fixup worker is running. we must also
  686. * increment scrubs_paused to prevent deadlocking on pause
  687. * requests used for transactions commits (as the worker uses a
  688. * transaction context). it is safe to regard the fixup worker
  689. * as paused for all matters practical. effectively, we only
  690. * avoid cancellation requests from completing.
  691. */
  692. mutex_lock(&fs_info->scrub_lock);
  693. atomic_inc(&fs_info->scrubs_running);
  694. atomic_inc(&fs_info->scrubs_paused);
  695. mutex_unlock(&fs_info->scrub_lock);
  696. atomic_inc(&sdev->fixup_cnt);
  697. fixup_nodatasum->work.func = scrub_fixup_nodatasum;
  698. btrfs_queue_worker(&fs_info->scrub_workers,
  699. &fixup_nodatasum->work);
  700. goto out;
  701. }
  702. /*
  703. * now build and submit the bios for the other mirrors, check
  704. * checksums
  705. */
  706. for (mirror_index = 0;
  707. mirror_index < BTRFS_MAX_MIRRORS &&
  708. sblocks_for_recheck[mirror_index].page_count > 0;
  709. mirror_index++) {
  710. if (mirror_index == failed_mirror_index)
  711. continue;
  712. /* build and submit the bios, check checksums */
  713. ret = scrub_recheck_block(fs_info,
  714. sblocks_for_recheck + mirror_index,
  715. is_metadata, have_csum, csum,
  716. generation, sdev->csum_size);
  717. if (ret)
  718. goto did_not_correct_error;
  719. }
  720. /*
  721. * first try to pick the mirror which is completely without I/O
  722. * errors and also does not have a checksum error.
  723. * If one is found, and if a checksum is present, the full block
  724. * that is known to contain an error is rewritten. Afterwards
  725. * the block is known to be corrected.
  726. * If a mirror is found which is completely correct, and no
  727. * checksum is present, only those pages are rewritten that had
  728. * an I/O error in the block to be repaired, since it cannot be
  729. * determined, which copy of the other pages is better (and it
  730. * could happen otherwise that a correct page would be
  731. * overwritten by a bad one).
  732. */
  733. for (mirror_index = 0;
  734. mirror_index < BTRFS_MAX_MIRRORS &&
  735. sblocks_for_recheck[mirror_index].page_count > 0;
  736. mirror_index++) {
  737. struct scrub_block *sblock_other = sblocks_for_recheck +
  738. mirror_index;
  739. if (!sblock_other->header_error &&
  740. !sblock_other->checksum_error &&
  741. sblock_other->no_io_error_seen) {
  742. int force_write = is_metadata || have_csum;
  743. ret = scrub_repair_block_from_good_copy(sblock_bad,
  744. sblock_other,
  745. force_write);
  746. if (0 == ret)
  747. goto corrected_error;
  748. }
  749. }
  750. /*
  751. * in case of I/O errors in the area that is supposed to be
  752. * repaired, continue by picking good copies of those pages.
  753. * Select the good pages from mirrors to rewrite bad pages from
  754. * the area to fix. Afterwards verify the checksum of the block
  755. * that is supposed to be repaired. This verification step is
  756. * only done for the purpose of statistic counting and for the
  757. * final scrub report, whether errors remain.
  758. * A perfect algorithm could make use of the checksum and try
  759. * all possible combinations of pages from the different mirrors
  760. * until the checksum verification succeeds. For example, when
  761. * the 2nd page of mirror #1 faces I/O errors, and the 2nd page
  762. * of mirror #2 is readable but the final checksum test fails,
  763. * then the 2nd page of mirror #3 could be tried, whether now
  764. * the final checksum succeedes. But this would be a rare
  765. * exception and is therefore not implemented. At least it is
  766. * avoided that the good copy is overwritten.
  767. * A more useful improvement would be to pick the sectors
  768. * without I/O error based on sector sizes (512 bytes on legacy
  769. * disks) instead of on PAGE_SIZE. Then maybe 512 byte of one
  770. * mirror could be repaired by taking 512 byte of a different
  771. * mirror, even if other 512 byte sectors in the same PAGE_SIZE
  772. * area are unreadable.
  773. */
  774. /* can only fix I/O errors from here on */
  775. if (sblock_bad->no_io_error_seen)
  776. goto did_not_correct_error;
  777. success = 1;
  778. for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
  779. struct scrub_page *page_bad = sblock_bad->pagev + page_num;
  780. if (!page_bad->io_error)
  781. continue;
  782. for (mirror_index = 0;
  783. mirror_index < BTRFS_MAX_MIRRORS &&
  784. sblocks_for_recheck[mirror_index].page_count > 0;
  785. mirror_index++) {
  786. struct scrub_block *sblock_other = sblocks_for_recheck +
  787. mirror_index;
  788. struct scrub_page *page_other = sblock_other->pagev +
  789. page_num;
  790. if (!page_other->io_error) {
  791. ret = scrub_repair_page_from_good_copy(
  792. sblock_bad, sblock_other, page_num, 0);
  793. if (0 == ret) {
  794. page_bad->io_error = 0;
  795. break; /* succeeded for this page */
  796. }
  797. }
  798. }
  799. if (page_bad->io_error) {
  800. /* did not find a mirror to copy the page from */
  801. success = 0;
  802. }
  803. }
  804. if (success) {
  805. if (is_metadata || have_csum) {
  806. /*
  807. * need to verify the checksum now that all
  808. * sectors on disk are repaired (the write
  809. * request for data to be repaired is on its way).
  810. * Just be lazy and use scrub_recheck_block()
  811. * which re-reads the data before the checksum
  812. * is verified, but most likely the data comes out
  813. * of the page cache.
  814. */
  815. ret = scrub_recheck_block(fs_info, sblock_bad,
  816. is_metadata, have_csum, csum,
  817. generation, sdev->csum_size);
  818. if (!ret && !sblock_bad->header_error &&
  819. !sblock_bad->checksum_error &&
  820. sblock_bad->no_io_error_seen)
  821. goto corrected_error;
  822. else
  823. goto did_not_correct_error;
  824. } else {
  825. corrected_error:
  826. spin_lock(&sdev->stat_lock);
  827. sdev->stat.corrected_errors++;
  828. spin_unlock(&sdev->stat_lock);
  829. printk_ratelimited(KERN_ERR
  830. "btrfs: fixed up error at logical %llu on dev %s\n",
  831. (unsigned long long)logical, sdev->dev->name);
  832. }
  833. } else {
  834. did_not_correct_error:
  835. spin_lock(&sdev->stat_lock);
  836. sdev->stat.uncorrectable_errors++;
  837. spin_unlock(&sdev->stat_lock);
  838. printk_ratelimited(KERN_ERR
  839. "btrfs: unable to fixup (regular) error at logical %llu on dev %s\n",
  840. (unsigned long long)logical, sdev->dev->name);
  841. }
  842. out:
  843. if (sblocks_for_recheck) {
  844. for (mirror_index = 0; mirror_index < BTRFS_MAX_MIRRORS;
  845. mirror_index++) {
  846. struct scrub_block *sblock = sblocks_for_recheck +
  847. mirror_index;
  848. int page_index;
  849. for (page_index = 0; page_index < SCRUB_PAGES_PER_BIO;
  850. page_index++)
  851. if (sblock->pagev[page_index].page)
  852. __free_page(
  853. sblock->pagev[page_index].page);
  854. }
  855. kfree(sblocks_for_recheck);
  856. }
  857. return 0;
  858. }
  859. static int scrub_setup_recheck_block(struct scrub_dev *sdev,
  860. struct btrfs_mapping_tree *map_tree,
  861. u64 length, u64 logical,
  862. struct scrub_block *sblocks_for_recheck)
  863. {
  864. int page_index;
  865. int mirror_index;
  866. int ret;
  867. /*
  868. * note: the three members sdev, ref_count and outstanding_pages
  869. * are not used (and not set) in the blocks that are used for
  870. * the recheck procedure
  871. */
  872. page_index = 0;
  873. while (length > 0) {
  874. u64 sublen = min_t(u64, length, PAGE_SIZE);
  875. u64 mapped_length = sublen;
  876. struct btrfs_bio *bbio = NULL;
  877. /*
  878. * with a length of PAGE_SIZE, each returned stripe
  879. * represents one mirror
  880. */
  881. ret = btrfs_map_block(map_tree, WRITE, logical, &mapped_length,
  882. &bbio, 0);
  883. if (ret || !bbio || mapped_length < sublen) {
  884. kfree(bbio);
  885. return -EIO;
  886. }
  887. BUG_ON(page_index >= SCRUB_PAGES_PER_BIO);
  888. for (mirror_index = 0; mirror_index < (int)bbio->num_stripes;
  889. mirror_index++) {
  890. struct scrub_block *sblock;
  891. struct scrub_page *page;
  892. if (mirror_index >= BTRFS_MAX_MIRRORS)
  893. continue;
  894. sblock = sblocks_for_recheck + mirror_index;
  895. page = sblock->pagev + page_index;
  896. page->logical = logical;
  897. page->physical = bbio->stripes[mirror_index].physical;
  898. /* for missing devices, bdev is NULL */
  899. page->bdev = bbio->stripes[mirror_index].dev->bdev;
  900. page->mirror_num = mirror_index + 1;
  901. page->page = alloc_page(GFP_NOFS);
  902. if (!page->page) {
  903. spin_lock(&sdev->stat_lock);
  904. sdev->stat.malloc_errors++;
  905. spin_unlock(&sdev->stat_lock);
  906. return -ENOMEM;
  907. }
  908. sblock->page_count++;
  909. }
  910. kfree(bbio);
  911. length -= sublen;
  912. logical += sublen;
  913. page_index++;
  914. }
  915. return 0;
  916. }
  917. /*
  918. * this function will check the on disk data for checksum errors, header
  919. * errors and read I/O errors. If any I/O errors happen, the exact pages
  920. * which are errored are marked as being bad. The goal is to enable scrub
  921. * to take those pages that are not errored from all the mirrors so that
  922. * the pages that are errored in the just handled mirror can be repaired.
  923. */
  924. static int scrub_recheck_block(struct btrfs_fs_info *fs_info,
  925. struct scrub_block *sblock, int is_metadata,
  926. int have_csum, u8 *csum, u64 generation,
  927. u16 csum_size)
  928. {
  929. int page_num;
  930. sblock->no_io_error_seen = 1;
  931. sblock->header_error = 0;
  932. sblock->checksum_error = 0;
  933. for (page_num = 0; page_num < sblock->page_count; page_num++) {
  934. struct bio *bio;
  935. int ret;
  936. struct scrub_page *page = sblock->pagev + page_num;
  937. DECLARE_COMPLETION_ONSTACK(complete);
  938. if (page->bdev == NULL) {
  939. page->io_error = 1;
  940. sblock->no_io_error_seen = 0;
  941. continue;
  942. }
  943. BUG_ON(!page->page);
  944. bio = bio_alloc(GFP_NOFS, 1);
  945. if (!bio)
  946. return -EIO;
  947. bio->bi_bdev = page->bdev;
  948. bio->bi_sector = page->physical >> 9;
  949. bio->bi_end_io = scrub_complete_bio_end_io;
  950. bio->bi_private = &complete;
  951. ret = bio_add_page(bio, page->page, PAGE_SIZE, 0);
  952. if (PAGE_SIZE != ret) {
  953. bio_put(bio);
  954. return -EIO;
  955. }
  956. btrfsic_submit_bio(READ, bio);
  957. /* this will also unplug the queue */
  958. wait_for_completion(&complete);
  959. page->io_error = !test_bit(BIO_UPTODATE, &bio->bi_flags);
  960. if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
  961. sblock->no_io_error_seen = 0;
  962. bio_put(bio);
  963. }
  964. if (sblock->no_io_error_seen)
  965. scrub_recheck_block_checksum(fs_info, sblock, is_metadata,
  966. have_csum, csum, generation,
  967. csum_size);
  968. return 0;
  969. }
  970. static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
  971. struct scrub_block *sblock,
  972. int is_metadata, int have_csum,
  973. const u8 *csum, u64 generation,
  974. u16 csum_size)
  975. {
  976. int page_num;
  977. u8 calculated_csum[BTRFS_CSUM_SIZE];
  978. u32 crc = ~(u32)0;
  979. struct btrfs_root *root = fs_info->extent_root;
  980. void *mapped_buffer;
  981. BUG_ON(!sblock->pagev[0].page);
  982. if (is_metadata) {
  983. struct btrfs_header *h;
  984. mapped_buffer = kmap_atomic(sblock->pagev[0].page, KM_USER0);
  985. h = (struct btrfs_header *)mapped_buffer;
  986. if (sblock->pagev[0].logical != le64_to_cpu(h->bytenr) ||
  987. generation != le64_to_cpu(h->generation) ||
  988. memcmp(h->fsid, fs_info->fsid, BTRFS_UUID_SIZE) ||
  989. memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
  990. BTRFS_UUID_SIZE))
  991. sblock->header_error = 1;
  992. csum = h->csum;
  993. } else {
  994. if (!have_csum)
  995. return;
  996. mapped_buffer = kmap_atomic(sblock->pagev[0].page, KM_USER0);
  997. }
  998. for (page_num = 0;;) {
  999. if (page_num == 0 && is_metadata)
  1000. crc = btrfs_csum_data(root,
  1001. ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE,
  1002. crc, PAGE_SIZE - BTRFS_CSUM_SIZE);
  1003. else
  1004. crc = btrfs_csum_data(root, mapped_buffer, crc,
  1005. PAGE_SIZE);
  1006. kunmap_atomic(mapped_buffer, KM_USER0);
  1007. page_num++;
  1008. if (page_num >= sblock->page_count)
  1009. break;
  1010. BUG_ON(!sblock->pagev[page_num].page);
  1011. mapped_buffer = kmap_atomic(sblock->pagev[page_num].page,
  1012. KM_USER0);
  1013. }
  1014. btrfs_csum_final(crc, calculated_csum);
  1015. if (memcmp(calculated_csum, csum, csum_size))
  1016. sblock->checksum_error = 1;
  1017. }
  1018. static void scrub_complete_bio_end_io(struct bio *bio, int err)
  1019. {
  1020. complete((struct completion *)bio->bi_private);
  1021. }
  1022. static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
  1023. struct scrub_block *sblock_good,
  1024. int force_write)
  1025. {
  1026. int page_num;
  1027. int ret = 0;
  1028. for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
  1029. int ret_sub;
  1030. ret_sub = scrub_repair_page_from_good_copy(sblock_bad,
  1031. sblock_good,
  1032. page_num,
  1033. force_write);
  1034. if (ret_sub)
  1035. ret = ret_sub;
  1036. }
  1037. return ret;
  1038. }
  1039. static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
  1040. struct scrub_block *sblock_good,
  1041. int page_num, int force_write)
  1042. {
  1043. struct scrub_page *page_bad = sblock_bad->pagev + page_num;
  1044. struct scrub_page *page_good = sblock_good->pagev + page_num;
  1045. BUG_ON(sblock_bad->pagev[page_num].page == NULL);
  1046. BUG_ON(sblock_good->pagev[page_num].page == NULL);
  1047. if (force_write || sblock_bad->header_error ||
  1048. sblock_bad->checksum_error || page_bad->io_error) {
  1049. struct bio *bio;
  1050. int ret;
  1051. DECLARE_COMPLETION_ONSTACK(complete);
  1052. bio = bio_alloc(GFP_NOFS, 1);
  1053. if (!bio)
  1054. return -EIO;
  1055. bio->bi_bdev = page_bad->bdev;
  1056. bio->bi_sector = page_bad->physical >> 9;
  1057. bio->bi_end_io = scrub_complete_bio_end_io;
  1058. bio->bi_private = &complete;
  1059. ret = bio_add_page(bio, page_good->page, PAGE_SIZE, 0);
  1060. if (PAGE_SIZE != ret) {
  1061. bio_put(bio);
  1062. return -EIO;
  1063. }
  1064. btrfsic_submit_bio(WRITE, bio);
  1065. /* this will also unplug the queue */
  1066. wait_for_completion(&complete);
  1067. bio_put(bio);
  1068. }
  1069. return 0;
  1070. }
  1071. static void scrub_checksum(struct scrub_block *sblock)
  1072. {
  1073. u64 flags;
  1074. int ret;
  1075. BUG_ON(sblock->page_count < 1);
  1076. flags = sblock->pagev[0].flags;
  1077. ret = 0;
  1078. if (flags & BTRFS_EXTENT_FLAG_DATA)
  1079. ret = scrub_checksum_data(sblock);
  1080. else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
  1081. ret = scrub_checksum_tree_block(sblock);
  1082. else if (flags & BTRFS_EXTENT_FLAG_SUPER)
  1083. (void)scrub_checksum_super(sblock);
  1084. else
  1085. WARN_ON(1);
  1086. if (ret)
  1087. scrub_handle_errored_block(sblock);
  1088. }
  1089. static int scrub_checksum_data(struct scrub_block *sblock)
  1090. {
  1091. struct scrub_dev *sdev = sblock->sdev;
  1092. u8 csum[BTRFS_CSUM_SIZE];
  1093. u8 *on_disk_csum;
  1094. struct page *page;
  1095. void *buffer;
  1096. u32 crc = ~(u32)0;
  1097. int fail = 0;
  1098. struct btrfs_root *root = sdev->dev->dev_root;
  1099. u64 len;
  1100. int index;
  1101. BUG_ON(sblock->page_count < 1);
  1102. if (!sblock->pagev[0].have_csum)
  1103. return 0;
  1104. on_disk_csum = sblock->pagev[0].csum;
  1105. page = sblock->pagev[0].page;
  1106. buffer = kmap_atomic(page, KM_USER0);
  1107. len = sdev->sectorsize;
  1108. index = 0;
  1109. for (;;) {
  1110. u64 l = min_t(u64, len, PAGE_SIZE);
  1111. crc = btrfs_csum_data(root, buffer, crc, l);
  1112. kunmap_atomic(buffer, KM_USER0);
  1113. len -= l;
  1114. if (len == 0)
  1115. break;
  1116. index++;
  1117. BUG_ON(index >= sblock->page_count);
  1118. BUG_ON(!sblock->pagev[index].page);
  1119. page = sblock->pagev[index].page;
  1120. buffer = kmap_atomic(page, KM_USER0);
  1121. }
  1122. btrfs_csum_final(crc, csum);
  1123. if (memcmp(csum, on_disk_csum, sdev->csum_size))
  1124. fail = 1;
  1125. return fail;
  1126. }
  1127. static int scrub_checksum_tree_block(struct scrub_block *sblock)
  1128. {
  1129. struct scrub_dev *sdev = sblock->sdev;
  1130. struct btrfs_header *h;
  1131. struct btrfs_root *root = sdev->dev->dev_root;
  1132. struct btrfs_fs_info *fs_info = root->fs_info;
  1133. u8 calculated_csum[BTRFS_CSUM_SIZE];
  1134. u8 on_disk_csum[BTRFS_CSUM_SIZE];
  1135. struct page *page;
  1136. void *mapped_buffer;
  1137. u64 mapped_size;
  1138. void *p;
  1139. u32 crc = ~(u32)0;
  1140. int fail = 0;
  1141. int crc_fail = 0;
  1142. u64 len;
  1143. int index;
  1144. BUG_ON(sblock->page_count < 1);
  1145. page = sblock->pagev[0].page;
  1146. mapped_buffer = kmap_atomic(page, KM_USER0);
  1147. h = (struct btrfs_header *)mapped_buffer;
  1148. memcpy(on_disk_csum, h->csum, sdev->csum_size);
  1149. /*
  1150. * we don't use the getter functions here, as we
  1151. * a) don't have an extent buffer and
  1152. * b) the page is already kmapped
  1153. */
  1154. if (sblock->pagev[0].logical != le64_to_cpu(h->bytenr))
  1155. ++fail;
  1156. if (sblock->pagev[0].generation != le64_to_cpu(h->generation))
  1157. ++fail;
  1158. if (memcmp(h->fsid, fs_info->fsid, BTRFS_UUID_SIZE))
  1159. ++fail;
  1160. if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
  1161. BTRFS_UUID_SIZE))
  1162. ++fail;
  1163. BUG_ON(sdev->nodesize != sdev->leafsize);
  1164. len = sdev->nodesize - BTRFS_CSUM_SIZE;
  1165. mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
  1166. p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
  1167. index = 0;
  1168. for (;;) {
  1169. u64 l = min_t(u64, len, mapped_size);
  1170. crc = btrfs_csum_data(root, p, crc, l);
  1171. kunmap_atomic(mapped_buffer, KM_USER0);
  1172. len -= l;
  1173. if (len == 0)
  1174. break;
  1175. index++;
  1176. BUG_ON(index >= sblock->page_count);
  1177. BUG_ON(!sblock->pagev[index].page);
  1178. page = sblock->pagev[index].page;
  1179. mapped_buffer = kmap_atomic(page, KM_USER0);
  1180. mapped_size = PAGE_SIZE;
  1181. p = mapped_buffer;
  1182. }
  1183. btrfs_csum_final(crc, calculated_csum);
  1184. if (memcmp(calculated_csum, on_disk_csum, sdev->csum_size))
  1185. ++crc_fail;
  1186. return fail || crc_fail;
  1187. }
  1188. static int scrub_checksum_super(struct scrub_block *sblock)
  1189. {
  1190. struct btrfs_super_block *s;
  1191. struct scrub_dev *sdev = sblock->sdev;
  1192. struct btrfs_root *root = sdev->dev->dev_root;
  1193. struct btrfs_fs_info *fs_info = root->fs_info;
  1194. u8 calculated_csum[BTRFS_CSUM_SIZE];
  1195. u8 on_disk_csum[BTRFS_CSUM_SIZE];
  1196. struct page *page;
  1197. void *mapped_buffer;
  1198. u64 mapped_size;
  1199. void *p;
  1200. u32 crc = ~(u32)0;
  1201. int fail = 0;
  1202. u64 len;
  1203. int index;
  1204. BUG_ON(sblock->page_count < 1);
  1205. page = sblock->pagev[0].page;
  1206. mapped_buffer = kmap_atomic(page, KM_USER0);
  1207. s = (struct btrfs_super_block *)mapped_buffer;
  1208. memcpy(on_disk_csum, s->csum, sdev->csum_size);
  1209. if (sblock->pagev[0].logical != le64_to_cpu(s->bytenr))
  1210. ++fail;
  1211. if (sblock->pagev[0].generation != le64_to_cpu(s->generation))
  1212. ++fail;
  1213. if (memcmp(s->fsid, fs_info->fsid, BTRFS_UUID_SIZE))
  1214. ++fail;
  1215. len = BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE;
  1216. mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
  1217. p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
  1218. index = 0;
  1219. for (;;) {
  1220. u64 l = min_t(u64, len, mapped_size);
  1221. crc = btrfs_csum_data(root, p, crc, l);
  1222. kunmap_atomic(mapped_buffer, KM_USER0);
  1223. len -= l;
  1224. if (len == 0)
  1225. break;
  1226. index++;
  1227. BUG_ON(index >= sblock->page_count);
  1228. BUG_ON(!sblock->pagev[index].page);
  1229. page = sblock->pagev[index].page;
  1230. mapped_buffer = kmap_atomic(page, KM_USER0);
  1231. mapped_size = PAGE_SIZE;
  1232. p = mapped_buffer;
  1233. }
  1234. btrfs_csum_final(crc, calculated_csum);
  1235. if (memcmp(calculated_csum, on_disk_csum, sdev->csum_size))
  1236. ++fail;
  1237. if (fail) {
  1238. /*
  1239. * if we find an error in a super block, we just report it.
  1240. * They will get written with the next transaction commit
  1241. * anyway
  1242. */
  1243. spin_lock(&sdev->stat_lock);
  1244. ++sdev->stat.super_errors;
  1245. spin_unlock(&sdev->stat_lock);
  1246. }
  1247. return fail;
  1248. }
  1249. static void scrub_block_get(struct scrub_block *sblock)
  1250. {
  1251. atomic_inc(&sblock->ref_count);
  1252. }
  1253. static void scrub_block_put(struct scrub_block *sblock)
  1254. {
  1255. if (atomic_dec_and_test(&sblock->ref_count)) {
  1256. int i;
  1257. for (i = 0; i < sblock->page_count; i++)
  1258. if (sblock->pagev[i].page)
  1259. __free_page(sblock->pagev[i].page);
  1260. kfree(sblock);
  1261. }
  1262. }
  1263. static void scrub_submit(struct scrub_dev *sdev)
  1264. {
  1265. struct scrub_bio *sbio;
  1266. if (sdev->curr == -1)
  1267. return;
  1268. sbio = sdev->bios[sdev->curr];
  1269. sdev->curr = -1;
  1270. atomic_inc(&sdev->in_flight);
  1271. btrfsic_submit_bio(READ, sbio->bio);
  1272. }
  1273. static int scrub_add_page_to_bio(struct scrub_dev *sdev,
  1274. struct scrub_page *spage)
  1275. {
  1276. struct scrub_block *sblock = spage->sblock;
  1277. struct scrub_bio *sbio;
  1278. int ret;
  1279. again:
  1280. /*
  1281. * grab a fresh bio or wait for one to become available
  1282. */
  1283. while (sdev->curr == -1) {
  1284. spin_lock(&sdev->list_lock);
  1285. sdev->curr = sdev->first_free;
  1286. if (sdev->curr != -1) {
  1287. sdev->first_free = sdev->bios[sdev->curr]->next_free;
  1288. sdev->bios[sdev->curr]->next_free = -1;
  1289. sdev->bios[sdev->curr]->page_count = 0;
  1290. spin_unlock(&sdev->list_lock);
  1291. } else {
  1292. spin_unlock(&sdev->list_lock);
  1293. wait_event(sdev->list_wait, sdev->first_free != -1);
  1294. }
  1295. }
  1296. sbio = sdev->bios[sdev->curr];
  1297. if (sbio->page_count == 0) {
  1298. struct bio *bio;
  1299. sbio->physical = spage->physical;
  1300. sbio->logical = spage->logical;
  1301. bio = sbio->bio;
  1302. if (!bio) {
  1303. bio = bio_alloc(GFP_NOFS, sdev->pages_per_bio);
  1304. if (!bio)
  1305. return -ENOMEM;
  1306. sbio->bio = bio;
  1307. }
  1308. bio->bi_private = sbio;
  1309. bio->bi_end_io = scrub_bio_end_io;
  1310. bio->bi_bdev = sdev->dev->bdev;
  1311. bio->bi_sector = spage->physical >> 9;
  1312. sbio->err = 0;
  1313. } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
  1314. spage->physical ||
  1315. sbio->logical + sbio->page_count * PAGE_SIZE !=
  1316. spage->logical) {
  1317. scrub_submit(sdev);
  1318. goto again;
  1319. }
  1320. sbio->pagev[sbio->page_count] = spage;
  1321. ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
  1322. if (ret != PAGE_SIZE) {
  1323. if (sbio->page_count < 1) {
  1324. bio_put(sbio->bio);
  1325. sbio->bio = NULL;
  1326. return -EIO;
  1327. }
  1328. scrub_submit(sdev);
  1329. goto again;
  1330. }
  1331. scrub_block_get(sblock); /* one for the added page */
  1332. atomic_inc(&sblock->outstanding_pages);
  1333. sbio->page_count++;
  1334. if (sbio->page_count == sdev->pages_per_bio)
  1335. scrub_submit(sdev);
  1336. return 0;
  1337. }
  1338. static int scrub_pages(struct scrub_dev *sdev, u64 logical, u64 len,
  1339. u64 physical, u64 flags, u64 gen, int mirror_num,
  1340. u8 *csum, int force)
  1341. {
  1342. struct scrub_block *sblock;
  1343. int index;
  1344. sblock = kzalloc(sizeof(*sblock), GFP_NOFS);
  1345. if (!sblock) {
  1346. spin_lock(&sdev->stat_lock);
  1347. sdev->stat.malloc_errors++;
  1348. spin_unlock(&sdev->stat_lock);
  1349. return -ENOMEM;
  1350. }
  1351. /* one ref inside this function, plus one for each page later on */
  1352. atomic_set(&sblock->ref_count, 1);
  1353. sblock->sdev = sdev;
  1354. sblock->no_io_error_seen = 1;
  1355. for (index = 0; len > 0; index++) {
  1356. struct scrub_page *spage = sblock->pagev + index;
  1357. u64 l = min_t(u64, len, PAGE_SIZE);
  1358. BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
  1359. spage->page = alloc_page(GFP_NOFS);
  1360. if (!spage->page) {
  1361. spin_lock(&sdev->stat_lock);
  1362. sdev->stat.malloc_errors++;
  1363. spin_unlock(&sdev->stat_lock);
  1364. while (index > 0) {
  1365. index--;
  1366. __free_page(sblock->pagev[index].page);
  1367. }
  1368. kfree(sblock);
  1369. return -ENOMEM;
  1370. }
  1371. spage->sblock = sblock;
  1372. spage->bdev = sdev->dev->bdev;
  1373. spage->flags = flags;
  1374. spage->generation = gen;
  1375. spage->logical = logical;
  1376. spage->physical = physical;
  1377. spage->mirror_num = mirror_num;
  1378. if (csum) {
  1379. spage->have_csum = 1;
  1380. memcpy(spage->csum, csum, sdev->csum_size);
  1381. } else {
  1382. spage->have_csum = 0;
  1383. }
  1384. sblock->page_count++;
  1385. len -= l;
  1386. logical += l;
  1387. physical += l;
  1388. }
  1389. BUG_ON(sblock->page_count == 0);
  1390. for (index = 0; index < sblock->page_count; index++) {
  1391. struct scrub_page *spage = sblock->pagev + index;
  1392. int ret;
  1393. ret = scrub_add_page_to_bio(sdev, spage);
  1394. if (ret) {
  1395. scrub_block_put(sblock);
  1396. return ret;
  1397. }
  1398. }
  1399. if (force)
  1400. scrub_submit(sdev);
  1401. /* last one frees, either here or in bio completion for last page */
  1402. scrub_block_put(sblock);
  1403. return 0;
  1404. }
  1405. static void scrub_bio_end_io(struct bio *bio, int err)
  1406. {
  1407. struct scrub_bio *sbio = bio->bi_private;
  1408. struct scrub_dev *sdev = sbio->sdev;
  1409. struct btrfs_fs_info *fs_info = sdev->dev->dev_root->fs_info;
  1410. sbio->err = err;
  1411. sbio->bio = bio;
  1412. btrfs_queue_worker(&fs_info->scrub_workers, &sbio->work);
  1413. }
  1414. static void scrub_bio_end_io_worker(struct btrfs_work *work)
  1415. {
  1416. struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
  1417. struct scrub_dev *sdev = sbio->sdev;
  1418. int i;
  1419. BUG_ON(sbio->page_count > SCRUB_PAGES_PER_BIO);
  1420. if (sbio->err) {
  1421. for (i = 0; i < sbio->page_count; i++) {
  1422. struct scrub_page *spage = sbio->pagev[i];
  1423. spage->io_error = 1;
  1424. spage->sblock->no_io_error_seen = 0;
  1425. }
  1426. }
  1427. /* now complete the scrub_block items that have all pages completed */
  1428. for (i = 0; i < sbio->page_count; i++) {
  1429. struct scrub_page *spage = sbio->pagev[i];
  1430. struct scrub_block *sblock = spage->sblock;
  1431. if (atomic_dec_and_test(&sblock->outstanding_pages))
  1432. scrub_block_complete(sblock);
  1433. scrub_block_put(sblock);
  1434. }
  1435. if (sbio->err) {
  1436. /* what is this good for??? */
  1437. sbio->bio->bi_flags &= ~(BIO_POOL_MASK - 1);
  1438. sbio->bio->bi_flags |= 1 << BIO_UPTODATE;
  1439. sbio->bio->bi_phys_segments = 0;
  1440. sbio->bio->bi_idx = 0;
  1441. for (i = 0; i < sbio->page_count; i++) {
  1442. struct bio_vec *bi;
  1443. bi = &sbio->bio->bi_io_vec[i];
  1444. bi->bv_offset = 0;
  1445. bi->bv_len = PAGE_SIZE;
  1446. }
  1447. }
  1448. bio_put(sbio->bio);
  1449. sbio->bio = NULL;
  1450. spin_lock(&sdev->list_lock);
  1451. sbio->next_free = sdev->first_free;
  1452. sdev->first_free = sbio->index;
  1453. spin_unlock(&sdev->list_lock);
  1454. atomic_dec(&sdev->in_flight);
  1455. wake_up(&sdev->list_wait);
  1456. }
  1457. static void scrub_block_complete(struct scrub_block *sblock)
  1458. {
  1459. if (!sblock->no_io_error_seen)
  1460. scrub_handle_errored_block(sblock);
  1461. else
  1462. scrub_checksum(sblock);
  1463. }
  1464. static int scrub_find_csum(struct scrub_dev *sdev, u64 logical, u64 len,
  1465. u8 *csum)
  1466. {
  1467. struct btrfs_ordered_sum *sum = NULL;
  1468. int ret = 0;
  1469. unsigned long i;
  1470. unsigned long num_sectors;
  1471. while (!list_empty(&sdev->csum_list)) {
  1472. sum = list_first_entry(&sdev->csum_list,
  1473. struct btrfs_ordered_sum, list);
  1474. if (sum->bytenr > logical)
  1475. return 0;
  1476. if (sum->bytenr + sum->len > logical)
  1477. break;
  1478. ++sdev->stat.csum_discards;
  1479. list_del(&sum->list);
  1480. kfree(sum);
  1481. sum = NULL;
  1482. }
  1483. if (!sum)
  1484. return 0;
  1485. num_sectors = sum->len / sdev->sectorsize;
  1486. for (i = 0; i < num_sectors; ++i) {
  1487. if (sum->sums[i].bytenr == logical) {
  1488. memcpy(csum, &sum->sums[i].sum, sdev->csum_size);
  1489. ret = 1;
  1490. break;
  1491. }
  1492. }
  1493. if (ret && i == num_sectors - 1) {
  1494. list_del(&sum->list);
  1495. kfree(sum);
  1496. }
  1497. return ret;
  1498. }
  1499. /* scrub extent tries to collect up to 64 kB for each bio */
  1500. static int scrub_extent(struct scrub_dev *sdev, u64 logical, u64 len,
  1501. u64 physical, u64 flags, u64 gen, int mirror_num)
  1502. {
  1503. int ret;
  1504. u8 csum[BTRFS_CSUM_SIZE];
  1505. u32 blocksize;
  1506. if (flags & BTRFS_EXTENT_FLAG_DATA) {
  1507. blocksize = sdev->sectorsize;
  1508. spin_lock(&sdev->stat_lock);
  1509. sdev->stat.data_extents_scrubbed++;
  1510. sdev->stat.data_bytes_scrubbed += len;
  1511. spin_unlock(&sdev->stat_lock);
  1512. } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  1513. BUG_ON(sdev->nodesize != sdev->leafsize);
  1514. blocksize = sdev->nodesize;
  1515. spin_lock(&sdev->stat_lock);
  1516. sdev->stat.tree_extents_scrubbed++;
  1517. sdev->stat.tree_bytes_scrubbed += len;
  1518. spin_unlock(&sdev->stat_lock);
  1519. } else {
  1520. blocksize = sdev->sectorsize;
  1521. BUG_ON(1);
  1522. }
  1523. while (len) {
  1524. u64 l = min_t(u64, len, blocksize);
  1525. int have_csum = 0;
  1526. if (flags & BTRFS_EXTENT_FLAG_DATA) {
  1527. /* push csums to sbio */
  1528. have_csum = scrub_find_csum(sdev, logical, l, csum);
  1529. if (have_csum == 0)
  1530. ++sdev->stat.no_csum;
  1531. }
  1532. ret = scrub_pages(sdev, logical, l, physical, flags, gen,
  1533. mirror_num, have_csum ? csum : NULL, 0);
  1534. if (ret)
  1535. return ret;
  1536. len -= l;
  1537. logical += l;
  1538. physical += l;
  1539. }
  1540. return 0;
  1541. }
  1542. static noinline_for_stack int scrub_stripe(struct scrub_dev *sdev,
  1543. struct map_lookup *map, int num, u64 base, u64 length)
  1544. {
  1545. struct btrfs_path *path;
  1546. struct btrfs_fs_info *fs_info = sdev->dev->dev_root->fs_info;
  1547. struct btrfs_root *root = fs_info->extent_root;
  1548. struct btrfs_root *csum_root = fs_info->csum_root;
  1549. struct btrfs_extent_item *extent;
  1550. struct blk_plug plug;
  1551. u64 flags;
  1552. int ret;
  1553. int slot;
  1554. int i;
  1555. u64 nstripes;
  1556. struct extent_buffer *l;
  1557. struct btrfs_key key;
  1558. u64 physical;
  1559. u64 logical;
  1560. u64 generation;
  1561. int mirror_num;
  1562. struct reada_control *reada1;
  1563. struct reada_control *reada2;
  1564. struct btrfs_key key_start;
  1565. struct btrfs_key key_end;
  1566. u64 increment = map->stripe_len;
  1567. u64 offset;
  1568. nstripes = length;
  1569. offset = 0;
  1570. do_div(nstripes, map->stripe_len);
  1571. if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
  1572. offset = map->stripe_len * num;
  1573. increment = map->stripe_len * map->num_stripes;
  1574. mirror_num = 1;
  1575. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  1576. int factor = map->num_stripes / map->sub_stripes;
  1577. offset = map->stripe_len * (num / map->sub_stripes);
  1578. increment = map->stripe_len * factor;
  1579. mirror_num = num % map->sub_stripes + 1;
  1580. } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
  1581. increment = map->stripe_len;
  1582. mirror_num = num % map->num_stripes + 1;
  1583. } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
  1584. increment = map->stripe_len;
  1585. mirror_num = num % map->num_stripes + 1;
  1586. } else {
  1587. increment = map->stripe_len;
  1588. mirror_num = 1;
  1589. }
  1590. path = btrfs_alloc_path();
  1591. if (!path)
  1592. return -ENOMEM;
  1593. /*
  1594. * work on commit root. The related disk blocks are static as
  1595. * long as COW is applied. This means, it is save to rewrite
  1596. * them to repair disk errors without any race conditions
  1597. */
  1598. path->search_commit_root = 1;
  1599. path->skip_locking = 1;
  1600. /*
  1601. * trigger the readahead for extent tree csum tree and wait for
  1602. * completion. During readahead, the scrub is officially paused
  1603. * to not hold off transaction commits
  1604. */
  1605. logical = base + offset;
  1606. wait_event(sdev->list_wait,
  1607. atomic_read(&sdev->in_flight) == 0);
  1608. atomic_inc(&fs_info->scrubs_paused);
  1609. wake_up(&fs_info->scrub_pause_wait);
  1610. /* FIXME it might be better to start readahead at commit root */
  1611. key_start.objectid = logical;
  1612. key_start.type = BTRFS_EXTENT_ITEM_KEY;
  1613. key_start.offset = (u64)0;
  1614. key_end.objectid = base + offset + nstripes * increment;
  1615. key_end.type = BTRFS_EXTENT_ITEM_KEY;
  1616. key_end.offset = (u64)0;
  1617. reada1 = btrfs_reada_add(root, &key_start, &key_end);
  1618. key_start.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
  1619. key_start.type = BTRFS_EXTENT_CSUM_KEY;
  1620. key_start.offset = logical;
  1621. key_end.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
  1622. key_end.type = BTRFS_EXTENT_CSUM_KEY;
  1623. key_end.offset = base + offset + nstripes * increment;
  1624. reada2 = btrfs_reada_add(csum_root, &key_start, &key_end);
  1625. if (!IS_ERR(reada1))
  1626. btrfs_reada_wait(reada1);
  1627. if (!IS_ERR(reada2))
  1628. btrfs_reada_wait(reada2);
  1629. mutex_lock(&fs_info->scrub_lock);
  1630. while (atomic_read(&fs_info->scrub_pause_req)) {
  1631. mutex_unlock(&fs_info->scrub_lock);
  1632. wait_event(fs_info->scrub_pause_wait,
  1633. atomic_read(&fs_info->scrub_pause_req) == 0);
  1634. mutex_lock(&fs_info->scrub_lock);
  1635. }
  1636. atomic_dec(&fs_info->scrubs_paused);
  1637. mutex_unlock(&fs_info->scrub_lock);
  1638. wake_up(&fs_info->scrub_pause_wait);
  1639. /*
  1640. * collect all data csums for the stripe to avoid seeking during
  1641. * the scrub. This might currently (crc32) end up to be about 1MB
  1642. */
  1643. blk_start_plug(&plug);
  1644. /*
  1645. * now find all extents for each stripe and scrub them
  1646. */
  1647. logical = base + offset;
  1648. physical = map->stripes[num].physical;
  1649. ret = 0;
  1650. for (i = 0; i < nstripes; ++i) {
  1651. /*
  1652. * canceled?
  1653. */
  1654. if (atomic_read(&fs_info->scrub_cancel_req) ||
  1655. atomic_read(&sdev->cancel_req)) {
  1656. ret = -ECANCELED;
  1657. goto out;
  1658. }
  1659. /*
  1660. * check to see if we have to pause
  1661. */
  1662. if (atomic_read(&fs_info->scrub_pause_req)) {
  1663. /* push queued extents */
  1664. scrub_submit(sdev);
  1665. wait_event(sdev->list_wait,
  1666. atomic_read(&sdev->in_flight) == 0);
  1667. atomic_inc(&fs_info->scrubs_paused);
  1668. wake_up(&fs_info->scrub_pause_wait);
  1669. mutex_lock(&fs_info->scrub_lock);
  1670. while (atomic_read(&fs_info->scrub_pause_req)) {
  1671. mutex_unlock(&fs_info->scrub_lock);
  1672. wait_event(fs_info->scrub_pause_wait,
  1673. atomic_read(&fs_info->scrub_pause_req) == 0);
  1674. mutex_lock(&fs_info->scrub_lock);
  1675. }
  1676. atomic_dec(&fs_info->scrubs_paused);
  1677. mutex_unlock(&fs_info->scrub_lock);
  1678. wake_up(&fs_info->scrub_pause_wait);
  1679. }
  1680. ret = btrfs_lookup_csums_range(csum_root, logical,
  1681. logical + map->stripe_len - 1,
  1682. &sdev->csum_list, 1);
  1683. if (ret)
  1684. goto out;
  1685. key.objectid = logical;
  1686. key.type = BTRFS_EXTENT_ITEM_KEY;
  1687. key.offset = (u64)0;
  1688. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1689. if (ret < 0)
  1690. goto out;
  1691. if (ret > 0) {
  1692. ret = btrfs_previous_item(root, path, 0,
  1693. BTRFS_EXTENT_ITEM_KEY);
  1694. if (ret < 0)
  1695. goto out;
  1696. if (ret > 0) {
  1697. /* there's no smaller item, so stick with the
  1698. * larger one */
  1699. btrfs_release_path(path);
  1700. ret = btrfs_search_slot(NULL, root, &key,
  1701. path, 0, 0);
  1702. if (ret < 0)
  1703. goto out;
  1704. }
  1705. }
  1706. while (1) {
  1707. l = path->nodes[0];
  1708. slot = path->slots[0];
  1709. if (slot >= btrfs_header_nritems(l)) {
  1710. ret = btrfs_next_leaf(root, path);
  1711. if (ret == 0)
  1712. continue;
  1713. if (ret < 0)
  1714. goto out;
  1715. break;
  1716. }
  1717. btrfs_item_key_to_cpu(l, &key, slot);
  1718. if (key.objectid + key.offset <= logical)
  1719. goto next;
  1720. if (key.objectid >= logical + map->stripe_len)
  1721. break;
  1722. if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
  1723. goto next;
  1724. extent = btrfs_item_ptr(l, slot,
  1725. struct btrfs_extent_item);
  1726. flags = btrfs_extent_flags(l, extent);
  1727. generation = btrfs_extent_generation(l, extent);
  1728. if (key.objectid < logical &&
  1729. (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
  1730. printk(KERN_ERR
  1731. "btrfs scrub: tree block %llu spanning "
  1732. "stripes, ignored. logical=%llu\n",
  1733. (unsigned long long)key.objectid,
  1734. (unsigned long long)logical);
  1735. goto next;
  1736. }
  1737. /*
  1738. * trim extent to this stripe
  1739. */
  1740. if (key.objectid < logical) {
  1741. key.offset -= logical - key.objectid;
  1742. key.objectid = logical;
  1743. }
  1744. if (key.objectid + key.offset >
  1745. logical + map->stripe_len) {
  1746. key.offset = logical + map->stripe_len -
  1747. key.objectid;
  1748. }
  1749. ret = scrub_extent(sdev, key.objectid, key.offset,
  1750. key.objectid - logical + physical,
  1751. flags, generation, mirror_num);
  1752. if (ret)
  1753. goto out;
  1754. next:
  1755. path->slots[0]++;
  1756. }
  1757. btrfs_release_path(path);
  1758. logical += increment;
  1759. physical += map->stripe_len;
  1760. spin_lock(&sdev->stat_lock);
  1761. sdev->stat.last_physical = physical;
  1762. spin_unlock(&sdev->stat_lock);
  1763. }
  1764. /* push queued extents */
  1765. scrub_submit(sdev);
  1766. out:
  1767. blk_finish_plug(&plug);
  1768. btrfs_free_path(path);
  1769. return ret < 0 ? ret : 0;
  1770. }
  1771. static noinline_for_stack int scrub_chunk(struct scrub_dev *sdev,
  1772. u64 chunk_tree, u64 chunk_objectid, u64 chunk_offset, u64 length,
  1773. u64 dev_offset)
  1774. {
  1775. struct btrfs_mapping_tree *map_tree =
  1776. &sdev->dev->dev_root->fs_info->mapping_tree;
  1777. struct map_lookup *map;
  1778. struct extent_map *em;
  1779. int i;
  1780. int ret = -EINVAL;
  1781. read_lock(&map_tree->map_tree.lock);
  1782. em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
  1783. read_unlock(&map_tree->map_tree.lock);
  1784. if (!em)
  1785. return -EINVAL;
  1786. map = (struct map_lookup *)em->bdev;
  1787. if (em->start != chunk_offset)
  1788. goto out;
  1789. if (em->len < length)
  1790. goto out;
  1791. for (i = 0; i < map->num_stripes; ++i) {
  1792. if (map->stripes[i].dev == sdev->dev &&
  1793. map->stripes[i].physical == dev_offset) {
  1794. ret = scrub_stripe(sdev, map, i, chunk_offset, length);
  1795. if (ret)
  1796. goto out;
  1797. }
  1798. }
  1799. out:
  1800. free_extent_map(em);
  1801. return ret;
  1802. }
  1803. static noinline_for_stack
  1804. int scrub_enumerate_chunks(struct scrub_dev *sdev, u64 start, u64 end)
  1805. {
  1806. struct btrfs_dev_extent *dev_extent = NULL;
  1807. struct btrfs_path *path;
  1808. struct btrfs_root *root = sdev->dev->dev_root;
  1809. struct btrfs_fs_info *fs_info = root->fs_info;
  1810. u64 length;
  1811. u64 chunk_tree;
  1812. u64 chunk_objectid;
  1813. u64 chunk_offset;
  1814. int ret;
  1815. int slot;
  1816. struct extent_buffer *l;
  1817. struct btrfs_key key;
  1818. struct btrfs_key found_key;
  1819. struct btrfs_block_group_cache *cache;
  1820. path = btrfs_alloc_path();
  1821. if (!path)
  1822. return -ENOMEM;
  1823. path->reada = 2;
  1824. path->search_commit_root = 1;
  1825. path->skip_locking = 1;
  1826. key.objectid = sdev->dev->devid;
  1827. key.offset = 0ull;
  1828. key.type = BTRFS_DEV_EXTENT_KEY;
  1829. while (1) {
  1830. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1831. if (ret < 0)
  1832. break;
  1833. if (ret > 0) {
  1834. if (path->slots[0] >=
  1835. btrfs_header_nritems(path->nodes[0])) {
  1836. ret = btrfs_next_leaf(root, path);
  1837. if (ret)
  1838. break;
  1839. }
  1840. }
  1841. l = path->nodes[0];
  1842. slot = path->slots[0];
  1843. btrfs_item_key_to_cpu(l, &found_key, slot);
  1844. if (found_key.objectid != sdev->dev->devid)
  1845. break;
  1846. if (btrfs_key_type(&found_key) != BTRFS_DEV_EXTENT_KEY)
  1847. break;
  1848. if (found_key.offset >= end)
  1849. break;
  1850. if (found_key.offset < key.offset)
  1851. break;
  1852. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  1853. length = btrfs_dev_extent_length(l, dev_extent);
  1854. if (found_key.offset + length <= start) {
  1855. key.offset = found_key.offset + length;
  1856. btrfs_release_path(path);
  1857. continue;
  1858. }
  1859. chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
  1860. chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
  1861. chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
  1862. /*
  1863. * get a reference on the corresponding block group to prevent
  1864. * the chunk from going away while we scrub it
  1865. */
  1866. cache = btrfs_lookup_block_group(fs_info, chunk_offset);
  1867. if (!cache) {
  1868. ret = -ENOENT;
  1869. break;
  1870. }
  1871. ret = scrub_chunk(sdev, chunk_tree, chunk_objectid,
  1872. chunk_offset, length, found_key.offset);
  1873. btrfs_put_block_group(cache);
  1874. if (ret)
  1875. break;
  1876. key.offset = found_key.offset + length;
  1877. btrfs_release_path(path);
  1878. }
  1879. btrfs_free_path(path);
  1880. /*
  1881. * ret can still be 1 from search_slot or next_leaf,
  1882. * that's not an error
  1883. */
  1884. return ret < 0 ? ret : 0;
  1885. }
  1886. static noinline_for_stack int scrub_supers(struct scrub_dev *sdev)
  1887. {
  1888. int i;
  1889. u64 bytenr;
  1890. u64 gen;
  1891. int ret;
  1892. struct btrfs_device *device = sdev->dev;
  1893. struct btrfs_root *root = device->dev_root;
  1894. if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR)
  1895. return -EIO;
  1896. gen = root->fs_info->last_trans_committed;
  1897. for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
  1898. bytenr = btrfs_sb_offset(i);
  1899. if (bytenr + BTRFS_SUPER_INFO_SIZE > device->total_bytes)
  1900. break;
  1901. ret = scrub_pages(sdev, bytenr, BTRFS_SUPER_INFO_SIZE, bytenr,
  1902. BTRFS_EXTENT_FLAG_SUPER, gen, i, NULL, 1);
  1903. if (ret)
  1904. return ret;
  1905. }
  1906. wait_event(sdev->list_wait, atomic_read(&sdev->in_flight) == 0);
  1907. return 0;
  1908. }
  1909. /*
  1910. * get a reference count on fs_info->scrub_workers. start worker if necessary
  1911. */
  1912. static noinline_for_stack int scrub_workers_get(struct btrfs_root *root)
  1913. {
  1914. struct btrfs_fs_info *fs_info = root->fs_info;
  1915. int ret = 0;
  1916. mutex_lock(&fs_info->scrub_lock);
  1917. if (fs_info->scrub_workers_refcnt == 0) {
  1918. btrfs_init_workers(&fs_info->scrub_workers, "scrub",
  1919. fs_info->thread_pool_size, &fs_info->generic_worker);
  1920. fs_info->scrub_workers.idle_thresh = 4;
  1921. ret = btrfs_start_workers(&fs_info->scrub_workers);
  1922. if (ret)
  1923. goto out;
  1924. }
  1925. ++fs_info->scrub_workers_refcnt;
  1926. out:
  1927. mutex_unlock(&fs_info->scrub_lock);
  1928. return ret;
  1929. }
  1930. static noinline_for_stack void scrub_workers_put(struct btrfs_root *root)
  1931. {
  1932. struct btrfs_fs_info *fs_info = root->fs_info;
  1933. mutex_lock(&fs_info->scrub_lock);
  1934. if (--fs_info->scrub_workers_refcnt == 0)
  1935. btrfs_stop_workers(&fs_info->scrub_workers);
  1936. WARN_ON(fs_info->scrub_workers_refcnt < 0);
  1937. mutex_unlock(&fs_info->scrub_lock);
  1938. }
  1939. int btrfs_scrub_dev(struct btrfs_root *root, u64 devid, u64 start, u64 end,
  1940. struct btrfs_scrub_progress *progress, int readonly)
  1941. {
  1942. struct scrub_dev *sdev;
  1943. struct btrfs_fs_info *fs_info = root->fs_info;
  1944. int ret;
  1945. struct btrfs_device *dev;
  1946. if (btrfs_fs_closing(root->fs_info))
  1947. return -EINVAL;
  1948. /*
  1949. * check some assumptions
  1950. */
  1951. if (root->nodesize != root->leafsize) {
  1952. printk(KERN_ERR
  1953. "btrfs_scrub: size assumption nodesize == leafsize (%d == %d) fails\n",
  1954. root->nodesize, root->leafsize);
  1955. return -EINVAL;
  1956. }
  1957. if (root->nodesize > BTRFS_STRIPE_LEN) {
  1958. /*
  1959. * in this case scrub is unable to calculate the checksum
  1960. * the way scrub is implemented. Do not handle this
  1961. * situation at all because it won't ever happen.
  1962. */
  1963. printk(KERN_ERR
  1964. "btrfs_scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails\n",
  1965. root->nodesize, BTRFS_STRIPE_LEN);
  1966. return -EINVAL;
  1967. }
  1968. if (root->sectorsize != PAGE_SIZE) {
  1969. /* not supported for data w/o checksums */
  1970. printk(KERN_ERR
  1971. "btrfs_scrub: size assumption sectorsize != PAGE_SIZE (%d != %lld) fails\n",
  1972. root->sectorsize, (unsigned long long)PAGE_SIZE);
  1973. return -EINVAL;
  1974. }
  1975. ret = scrub_workers_get(root);
  1976. if (ret)
  1977. return ret;
  1978. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  1979. dev = btrfs_find_device(root, devid, NULL, NULL);
  1980. if (!dev || dev->missing) {
  1981. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1982. scrub_workers_put(root);
  1983. return -ENODEV;
  1984. }
  1985. mutex_lock(&fs_info->scrub_lock);
  1986. if (!dev->in_fs_metadata) {
  1987. mutex_unlock(&fs_info->scrub_lock);
  1988. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1989. scrub_workers_put(root);
  1990. return -ENODEV;
  1991. }
  1992. if (dev->scrub_device) {
  1993. mutex_unlock(&fs_info->scrub_lock);
  1994. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1995. scrub_workers_put(root);
  1996. return -EINPROGRESS;
  1997. }
  1998. sdev = scrub_setup_dev(dev);
  1999. if (IS_ERR(sdev)) {
  2000. mutex_unlock(&fs_info->scrub_lock);
  2001. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  2002. scrub_workers_put(root);
  2003. return PTR_ERR(sdev);
  2004. }
  2005. sdev->readonly = readonly;
  2006. dev->scrub_device = sdev;
  2007. atomic_inc(&fs_info->scrubs_running);
  2008. mutex_unlock(&fs_info->scrub_lock);
  2009. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  2010. down_read(&fs_info->scrub_super_lock);
  2011. ret = scrub_supers(sdev);
  2012. up_read(&fs_info->scrub_super_lock);
  2013. if (!ret)
  2014. ret = scrub_enumerate_chunks(sdev, start, end);
  2015. wait_event(sdev->list_wait, atomic_read(&sdev->in_flight) == 0);
  2016. atomic_dec(&fs_info->scrubs_running);
  2017. wake_up(&fs_info->scrub_pause_wait);
  2018. wait_event(sdev->list_wait, atomic_read(&sdev->fixup_cnt) == 0);
  2019. if (progress)
  2020. memcpy(progress, &sdev->stat, sizeof(*progress));
  2021. mutex_lock(&fs_info->scrub_lock);
  2022. dev->scrub_device = NULL;
  2023. mutex_unlock(&fs_info->scrub_lock);
  2024. scrub_free_dev(sdev);
  2025. scrub_workers_put(root);
  2026. return ret;
  2027. }
  2028. void btrfs_scrub_pause(struct btrfs_root *root)
  2029. {
  2030. struct btrfs_fs_info *fs_info = root->fs_info;
  2031. mutex_lock(&fs_info->scrub_lock);
  2032. atomic_inc(&fs_info->scrub_pause_req);
  2033. while (atomic_read(&fs_info->scrubs_paused) !=
  2034. atomic_read(&fs_info->scrubs_running)) {
  2035. mutex_unlock(&fs_info->scrub_lock);
  2036. wait_event(fs_info->scrub_pause_wait,
  2037. atomic_read(&fs_info->scrubs_paused) ==
  2038. atomic_read(&fs_info->scrubs_running));
  2039. mutex_lock(&fs_info->scrub_lock);
  2040. }
  2041. mutex_unlock(&fs_info->scrub_lock);
  2042. }
  2043. void btrfs_scrub_continue(struct btrfs_root *root)
  2044. {
  2045. struct btrfs_fs_info *fs_info = root->fs_info;
  2046. atomic_dec(&fs_info->scrub_pause_req);
  2047. wake_up(&fs_info->scrub_pause_wait);
  2048. }
  2049. void btrfs_scrub_pause_super(struct btrfs_root *root)
  2050. {
  2051. down_write(&root->fs_info->scrub_super_lock);
  2052. }
  2053. void btrfs_scrub_continue_super(struct btrfs_root *root)
  2054. {
  2055. up_write(&root->fs_info->scrub_super_lock);
  2056. }
  2057. int __btrfs_scrub_cancel(struct btrfs_fs_info *fs_info)
  2058. {
  2059. mutex_lock(&fs_info->scrub_lock);
  2060. if (!atomic_read(&fs_info->scrubs_running)) {
  2061. mutex_unlock(&fs_info->scrub_lock);
  2062. return -ENOTCONN;
  2063. }
  2064. atomic_inc(&fs_info->scrub_cancel_req);
  2065. while (atomic_read(&fs_info->scrubs_running)) {
  2066. mutex_unlock(&fs_info->scrub_lock);
  2067. wait_event(fs_info->scrub_pause_wait,
  2068. atomic_read(&fs_info->scrubs_running) == 0);
  2069. mutex_lock(&fs_info->scrub_lock);
  2070. }
  2071. atomic_dec(&fs_info->scrub_cancel_req);
  2072. mutex_unlock(&fs_info->scrub_lock);
  2073. return 0;
  2074. }
  2075. int btrfs_scrub_cancel(struct btrfs_root *root)
  2076. {
  2077. return __btrfs_scrub_cancel(root->fs_info);
  2078. }
  2079. int btrfs_scrub_cancel_dev(struct btrfs_root *root, struct btrfs_device *dev)
  2080. {
  2081. struct btrfs_fs_info *fs_info = root->fs_info;
  2082. struct scrub_dev *sdev;
  2083. mutex_lock(&fs_info->scrub_lock);
  2084. sdev = dev->scrub_device;
  2085. if (!sdev) {
  2086. mutex_unlock(&fs_info->scrub_lock);
  2087. return -ENOTCONN;
  2088. }
  2089. atomic_inc(&sdev->cancel_req);
  2090. while (dev->scrub_device) {
  2091. mutex_unlock(&fs_info->scrub_lock);
  2092. wait_event(fs_info->scrub_pause_wait,
  2093. dev->scrub_device == NULL);
  2094. mutex_lock(&fs_info->scrub_lock);
  2095. }
  2096. mutex_unlock(&fs_info->scrub_lock);
  2097. return 0;
  2098. }
  2099. int btrfs_scrub_cancel_devid(struct btrfs_root *root, u64 devid)
  2100. {
  2101. struct btrfs_fs_info *fs_info = root->fs_info;
  2102. struct btrfs_device *dev;
  2103. int ret;
  2104. /*
  2105. * we have to hold the device_list_mutex here so the device
  2106. * does not go away in cancel_dev. FIXME: find a better solution
  2107. */
  2108. mutex_lock(&fs_info->fs_devices->device_list_mutex);
  2109. dev = btrfs_find_device(root, devid, NULL, NULL);
  2110. if (!dev) {
  2111. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  2112. return -ENODEV;
  2113. }
  2114. ret = btrfs_scrub_cancel_dev(root, dev);
  2115. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  2116. return ret;
  2117. }
  2118. int btrfs_scrub_progress(struct btrfs_root *root, u64 devid,
  2119. struct btrfs_scrub_progress *progress)
  2120. {
  2121. struct btrfs_device *dev;
  2122. struct scrub_dev *sdev = NULL;
  2123. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  2124. dev = btrfs_find_device(root, devid, NULL, NULL);
  2125. if (dev)
  2126. sdev = dev->scrub_device;
  2127. if (sdev)
  2128. memcpy(progress, &sdev->stat, sizeof(*progress));
  2129. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  2130. return dev ? (sdev ? 0 : -ENOTCONN) : -ENODEV;
  2131. }