scrub.c 66 KB

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