scrub.c 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442
  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. if (ret & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  342. do {
  343. ret = tree_backref_for_extent(&ptr, eb, ei, item_size,
  344. &ref_root, &ref_level);
  345. printk(KERN_WARNING
  346. "btrfs: %s at logical %llu on dev %s, "
  347. "sector %llu: metadata %s (level %d) in tree "
  348. "%llu\n", errstr, swarn.logical, dev->name,
  349. (unsigned long long)swarn.sector,
  350. ref_level ? "node" : "leaf",
  351. ret < 0 ? -1 : ref_level,
  352. ret < 0 ? -1 : ref_root);
  353. } while (ret != 1);
  354. btrfs_release_path(path);
  355. } else {
  356. btrfs_release_path(path);
  357. swarn.path = path;
  358. iterate_extent_inodes(fs_info, found_key.objectid,
  359. extent_item_pos, 1,
  360. scrub_print_warning_inode, &swarn);
  361. }
  362. out:
  363. btrfs_free_path(path);
  364. kfree(swarn.scratch_buf);
  365. kfree(swarn.msg_buf);
  366. }
  367. static int scrub_fixup_readpage(u64 inum, u64 offset, u64 root, void *ctx)
  368. {
  369. struct page *page = NULL;
  370. unsigned long index;
  371. struct scrub_fixup_nodatasum *fixup = ctx;
  372. int ret;
  373. int corrected = 0;
  374. struct btrfs_key key;
  375. struct inode *inode = NULL;
  376. u64 end = offset + PAGE_SIZE - 1;
  377. struct btrfs_root *local_root;
  378. key.objectid = root;
  379. key.type = BTRFS_ROOT_ITEM_KEY;
  380. key.offset = (u64)-1;
  381. local_root = btrfs_read_fs_root_no_name(fixup->root->fs_info, &key);
  382. if (IS_ERR(local_root))
  383. return PTR_ERR(local_root);
  384. key.type = BTRFS_INODE_ITEM_KEY;
  385. key.objectid = inum;
  386. key.offset = 0;
  387. inode = btrfs_iget(fixup->root->fs_info->sb, &key, local_root, NULL);
  388. if (IS_ERR(inode))
  389. return PTR_ERR(inode);
  390. index = offset >> PAGE_CACHE_SHIFT;
  391. page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
  392. if (!page) {
  393. ret = -ENOMEM;
  394. goto out;
  395. }
  396. if (PageUptodate(page)) {
  397. struct btrfs_mapping_tree *map_tree;
  398. if (PageDirty(page)) {
  399. /*
  400. * we need to write the data to the defect sector. the
  401. * data that was in that sector is not in memory,
  402. * because the page was modified. we must not write the
  403. * modified page to that sector.
  404. *
  405. * TODO: what could be done here: wait for the delalloc
  406. * runner to write out that page (might involve
  407. * COW) and see whether the sector is still
  408. * referenced afterwards.
  409. *
  410. * For the meantime, we'll treat this error
  411. * incorrectable, although there is a chance that a
  412. * later scrub will find the bad sector again and that
  413. * there's no dirty page in memory, then.
  414. */
  415. ret = -EIO;
  416. goto out;
  417. }
  418. map_tree = &BTRFS_I(inode)->root->fs_info->mapping_tree;
  419. ret = repair_io_failure(map_tree, offset, PAGE_SIZE,
  420. fixup->logical, page,
  421. fixup->mirror_num);
  422. unlock_page(page);
  423. corrected = !ret;
  424. } else {
  425. /*
  426. * we need to get good data first. the general readpage path
  427. * will call repair_io_failure for us, we just have to make
  428. * sure we read the bad mirror.
  429. */
  430. ret = set_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
  431. EXTENT_DAMAGED, GFP_NOFS);
  432. if (ret) {
  433. /* set_extent_bits should give proper error */
  434. WARN_ON(ret > 0);
  435. if (ret > 0)
  436. ret = -EFAULT;
  437. goto out;
  438. }
  439. ret = extent_read_full_page(&BTRFS_I(inode)->io_tree, page,
  440. btrfs_get_extent,
  441. fixup->mirror_num);
  442. wait_on_page_locked(page);
  443. corrected = !test_range_bit(&BTRFS_I(inode)->io_tree, offset,
  444. end, EXTENT_DAMAGED, 0, NULL);
  445. if (!corrected)
  446. clear_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
  447. EXTENT_DAMAGED, GFP_NOFS);
  448. }
  449. out:
  450. if (page)
  451. put_page(page);
  452. if (inode)
  453. iput(inode);
  454. if (ret < 0)
  455. return ret;
  456. if (ret == 0 && corrected) {
  457. /*
  458. * we only need to call readpage for one of the inodes belonging
  459. * to this extent. so make iterate_extent_inodes stop
  460. */
  461. return 1;
  462. }
  463. return -EIO;
  464. }
  465. static void scrub_fixup_nodatasum(struct btrfs_work *work)
  466. {
  467. int ret;
  468. struct scrub_fixup_nodatasum *fixup;
  469. struct scrub_dev *sdev;
  470. struct btrfs_trans_handle *trans = NULL;
  471. struct btrfs_fs_info *fs_info;
  472. struct btrfs_path *path;
  473. int uncorrectable = 0;
  474. fixup = container_of(work, struct scrub_fixup_nodatasum, work);
  475. sdev = fixup->sdev;
  476. fs_info = fixup->root->fs_info;
  477. path = btrfs_alloc_path();
  478. if (!path) {
  479. spin_lock(&sdev->stat_lock);
  480. ++sdev->stat.malloc_errors;
  481. spin_unlock(&sdev->stat_lock);
  482. uncorrectable = 1;
  483. goto out;
  484. }
  485. trans = btrfs_join_transaction(fixup->root);
  486. if (IS_ERR(trans)) {
  487. uncorrectable = 1;
  488. goto out;
  489. }
  490. /*
  491. * the idea is to trigger a regular read through the standard path. we
  492. * read a page from the (failed) logical address by specifying the
  493. * corresponding copynum of the failed sector. thus, that readpage is
  494. * expected to fail.
  495. * that is the point where on-the-fly error correction will kick in
  496. * (once it's finished) and rewrite the failed sector if a good copy
  497. * can be found.
  498. */
  499. ret = iterate_inodes_from_logical(fixup->logical, fixup->root->fs_info,
  500. path, scrub_fixup_readpage,
  501. fixup);
  502. if (ret < 0) {
  503. uncorrectable = 1;
  504. goto out;
  505. }
  506. WARN_ON(ret != 1);
  507. spin_lock(&sdev->stat_lock);
  508. ++sdev->stat.corrected_errors;
  509. spin_unlock(&sdev->stat_lock);
  510. out:
  511. if (trans && !IS_ERR(trans))
  512. btrfs_end_transaction(trans, fixup->root);
  513. if (uncorrectable) {
  514. spin_lock(&sdev->stat_lock);
  515. ++sdev->stat.uncorrectable_errors;
  516. spin_unlock(&sdev->stat_lock);
  517. printk_ratelimited(KERN_ERR
  518. "btrfs: unable to fixup (nodatasum) error at logical %llu on dev %s\n",
  519. (unsigned long long)fixup->logical, sdev->dev->name);
  520. }
  521. btrfs_free_path(path);
  522. kfree(fixup);
  523. /* see caller why we're pretending to be paused in the scrub counters */
  524. mutex_lock(&fs_info->scrub_lock);
  525. atomic_dec(&fs_info->scrubs_running);
  526. atomic_dec(&fs_info->scrubs_paused);
  527. mutex_unlock(&fs_info->scrub_lock);
  528. atomic_dec(&sdev->fixup_cnt);
  529. wake_up(&fs_info->scrub_pause_wait);
  530. wake_up(&sdev->list_wait);
  531. }
  532. /*
  533. * scrub_handle_errored_block gets called when either verification of the
  534. * pages failed or the bio failed to read, e.g. with EIO. In the latter
  535. * case, this function handles all pages in the bio, even though only one
  536. * may be bad.
  537. * The goal of this function is to repair the errored block by using the
  538. * contents of one of the mirrors.
  539. */
  540. static int scrub_handle_errored_block(struct scrub_block *sblock_to_check)
  541. {
  542. struct scrub_dev *sdev = sblock_to_check->sdev;
  543. struct btrfs_fs_info *fs_info;
  544. u64 length;
  545. u64 logical;
  546. u64 generation;
  547. unsigned int failed_mirror_index;
  548. unsigned int is_metadata;
  549. unsigned int have_csum;
  550. u8 *csum;
  551. struct scrub_block *sblocks_for_recheck; /* holds one for each mirror */
  552. struct scrub_block *sblock_bad;
  553. int ret;
  554. int mirror_index;
  555. int page_num;
  556. int success;
  557. static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
  558. DEFAULT_RATELIMIT_BURST);
  559. BUG_ON(sblock_to_check->page_count < 1);
  560. fs_info = sdev->dev->dev_root->fs_info;
  561. length = sblock_to_check->page_count * PAGE_SIZE;
  562. logical = sblock_to_check->pagev[0].logical;
  563. generation = sblock_to_check->pagev[0].generation;
  564. BUG_ON(sblock_to_check->pagev[0].mirror_num < 1);
  565. failed_mirror_index = sblock_to_check->pagev[0].mirror_num - 1;
  566. is_metadata = !(sblock_to_check->pagev[0].flags &
  567. BTRFS_EXTENT_FLAG_DATA);
  568. have_csum = sblock_to_check->pagev[0].have_csum;
  569. csum = sblock_to_check->pagev[0].csum;
  570. /*
  571. * read all mirrors one after the other. This includes to
  572. * re-read the extent or metadata block that failed (that was
  573. * the cause that this fixup code is called) another time,
  574. * page by page this time in order to know which pages
  575. * caused I/O errors and which ones are good (for all mirrors).
  576. * It is the goal to handle the situation when more than one
  577. * mirror contains I/O errors, but the errors do not
  578. * overlap, i.e. the data can be repaired by selecting the
  579. * pages from those mirrors without I/O error on the
  580. * particular pages. One example (with blocks >= 2 * PAGE_SIZE)
  581. * would be that mirror #1 has an I/O error on the first page,
  582. * the second page is good, and mirror #2 has an I/O error on
  583. * the second page, but the first page is good.
  584. * Then the first page of the first mirror can be repaired by
  585. * taking the first page of the second mirror, and the
  586. * second page of the second mirror can be repaired by
  587. * copying the contents of the 2nd page of the 1st mirror.
  588. * One more note: if the pages of one mirror contain I/O
  589. * errors, the checksum cannot be verified. In order to get
  590. * the best data for repairing, the first attempt is to find
  591. * a mirror without I/O errors and with a validated checksum.
  592. * Only if this is not possible, the pages are picked from
  593. * mirrors with I/O errors without considering the checksum.
  594. * If the latter is the case, at the end, the checksum of the
  595. * repaired area is verified in order to correctly maintain
  596. * the statistics.
  597. */
  598. sblocks_for_recheck = kzalloc(BTRFS_MAX_MIRRORS *
  599. sizeof(*sblocks_for_recheck),
  600. GFP_NOFS);
  601. if (!sblocks_for_recheck) {
  602. spin_lock(&sdev->stat_lock);
  603. sdev->stat.malloc_errors++;
  604. sdev->stat.read_errors++;
  605. sdev->stat.uncorrectable_errors++;
  606. spin_unlock(&sdev->stat_lock);
  607. goto out;
  608. }
  609. /* setup the context, map the logical blocks and alloc the pages */
  610. ret = scrub_setup_recheck_block(sdev, &fs_info->mapping_tree, length,
  611. logical, sblocks_for_recheck);
  612. if (ret) {
  613. spin_lock(&sdev->stat_lock);
  614. sdev->stat.read_errors++;
  615. sdev->stat.uncorrectable_errors++;
  616. spin_unlock(&sdev->stat_lock);
  617. goto out;
  618. }
  619. BUG_ON(failed_mirror_index >= BTRFS_MAX_MIRRORS);
  620. sblock_bad = sblocks_for_recheck + failed_mirror_index;
  621. /* build and submit the bios for the failed mirror, check checksums */
  622. ret = scrub_recheck_block(fs_info, sblock_bad, is_metadata, have_csum,
  623. csum, generation, sdev->csum_size);
  624. if (ret) {
  625. spin_lock(&sdev->stat_lock);
  626. sdev->stat.read_errors++;
  627. sdev->stat.uncorrectable_errors++;
  628. spin_unlock(&sdev->stat_lock);
  629. goto out;
  630. }
  631. if (!sblock_bad->header_error && !sblock_bad->checksum_error &&
  632. sblock_bad->no_io_error_seen) {
  633. /*
  634. * the error disappeared after reading page by page, or
  635. * the area was part of a huge bio and other parts of the
  636. * bio caused I/O errors, or the block layer merged several
  637. * read requests into one and the error is caused by a
  638. * different bio (usually one of the two latter cases is
  639. * the cause)
  640. */
  641. spin_lock(&sdev->stat_lock);
  642. sdev->stat.unverified_errors++;
  643. spin_unlock(&sdev->stat_lock);
  644. goto out;
  645. }
  646. if (!sblock_bad->no_io_error_seen) {
  647. spin_lock(&sdev->stat_lock);
  648. sdev->stat.read_errors++;
  649. spin_unlock(&sdev->stat_lock);
  650. if (__ratelimit(&_rs))
  651. scrub_print_warning("i/o error", sblock_to_check);
  652. } else if (sblock_bad->checksum_error) {
  653. spin_lock(&sdev->stat_lock);
  654. sdev->stat.csum_errors++;
  655. spin_unlock(&sdev->stat_lock);
  656. if (__ratelimit(&_rs))
  657. scrub_print_warning("checksum error", sblock_to_check);
  658. } else if (sblock_bad->header_error) {
  659. spin_lock(&sdev->stat_lock);
  660. sdev->stat.verify_errors++;
  661. spin_unlock(&sdev->stat_lock);
  662. if (__ratelimit(&_rs))
  663. scrub_print_warning("checksum/header error",
  664. sblock_to_check);
  665. }
  666. if (sdev->readonly)
  667. goto did_not_correct_error;
  668. if (!is_metadata && !have_csum) {
  669. struct scrub_fixup_nodatasum *fixup_nodatasum;
  670. /*
  671. * !is_metadata and !have_csum, this means that the data
  672. * might not be COW'ed, that it might be modified
  673. * concurrently. The general strategy to work on the
  674. * commit root does not help in the case when COW is not
  675. * used.
  676. */
  677. fixup_nodatasum = kzalloc(sizeof(*fixup_nodatasum), GFP_NOFS);
  678. if (!fixup_nodatasum)
  679. goto did_not_correct_error;
  680. fixup_nodatasum->sdev = sdev;
  681. fixup_nodatasum->logical = logical;
  682. fixup_nodatasum->root = fs_info->extent_root;
  683. fixup_nodatasum->mirror_num = failed_mirror_index + 1;
  684. /*
  685. * increment scrubs_running to prevent cancel requests from
  686. * completing as long as a fixup worker is running. we must also
  687. * increment scrubs_paused to prevent deadlocking on pause
  688. * requests used for transactions commits (as the worker uses a
  689. * transaction context). it is safe to regard the fixup worker
  690. * as paused for all matters practical. effectively, we only
  691. * avoid cancellation requests from completing.
  692. */
  693. mutex_lock(&fs_info->scrub_lock);
  694. atomic_inc(&fs_info->scrubs_running);
  695. atomic_inc(&fs_info->scrubs_paused);
  696. mutex_unlock(&fs_info->scrub_lock);
  697. atomic_inc(&sdev->fixup_cnt);
  698. fixup_nodatasum->work.func = scrub_fixup_nodatasum;
  699. btrfs_queue_worker(&fs_info->scrub_workers,
  700. &fixup_nodatasum->work);
  701. goto out;
  702. }
  703. /*
  704. * now build and submit the bios for the other mirrors, check
  705. * checksums
  706. */
  707. for (mirror_index = 0;
  708. mirror_index < BTRFS_MAX_MIRRORS &&
  709. sblocks_for_recheck[mirror_index].page_count > 0;
  710. mirror_index++) {
  711. if (mirror_index == failed_mirror_index)
  712. continue;
  713. /* build and submit the bios, check checksums */
  714. ret = scrub_recheck_block(fs_info,
  715. sblocks_for_recheck + mirror_index,
  716. is_metadata, have_csum, csum,
  717. generation, sdev->csum_size);
  718. if (ret)
  719. goto did_not_correct_error;
  720. }
  721. /*
  722. * first try to pick the mirror which is completely without I/O
  723. * errors and also does not have a checksum error.
  724. * If one is found, and if a checksum is present, the full block
  725. * that is known to contain an error is rewritten. Afterwards
  726. * the block is known to be corrected.
  727. * If a mirror is found which is completely correct, and no
  728. * checksum is present, only those pages are rewritten that had
  729. * an I/O error in the block to be repaired, since it cannot be
  730. * determined, which copy of the other pages is better (and it
  731. * could happen otherwise that a correct page would be
  732. * overwritten by a bad one).
  733. */
  734. for (mirror_index = 0;
  735. mirror_index < BTRFS_MAX_MIRRORS &&
  736. sblocks_for_recheck[mirror_index].page_count > 0;
  737. mirror_index++) {
  738. struct scrub_block *sblock_other = sblocks_for_recheck +
  739. mirror_index;
  740. if (!sblock_other->header_error &&
  741. !sblock_other->checksum_error &&
  742. sblock_other->no_io_error_seen) {
  743. int force_write = is_metadata || have_csum;
  744. ret = scrub_repair_block_from_good_copy(sblock_bad,
  745. sblock_other,
  746. force_write);
  747. if (0 == ret)
  748. goto corrected_error;
  749. }
  750. }
  751. /*
  752. * in case of I/O errors in the area that is supposed to be
  753. * repaired, continue by picking good copies of those pages.
  754. * Select the good pages from mirrors to rewrite bad pages from
  755. * the area to fix. Afterwards verify the checksum of the block
  756. * that is supposed to be repaired. This verification step is
  757. * only done for the purpose of statistic counting and for the
  758. * final scrub report, whether errors remain.
  759. * A perfect algorithm could make use of the checksum and try
  760. * all possible combinations of pages from the different mirrors
  761. * until the checksum verification succeeds. For example, when
  762. * the 2nd page of mirror #1 faces I/O errors, and the 2nd page
  763. * of mirror #2 is readable but the final checksum test fails,
  764. * then the 2nd page of mirror #3 could be tried, whether now
  765. * the final checksum succeedes. But this would be a rare
  766. * exception and is therefore not implemented. At least it is
  767. * avoided that the good copy is overwritten.
  768. * A more useful improvement would be to pick the sectors
  769. * without I/O error based on sector sizes (512 bytes on legacy
  770. * disks) instead of on PAGE_SIZE. Then maybe 512 byte of one
  771. * mirror could be repaired by taking 512 byte of a different
  772. * mirror, even if other 512 byte sectors in the same PAGE_SIZE
  773. * area are unreadable.
  774. */
  775. /* can only fix I/O errors from here on */
  776. if (sblock_bad->no_io_error_seen)
  777. goto did_not_correct_error;
  778. success = 1;
  779. for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
  780. struct scrub_page *page_bad = sblock_bad->pagev + page_num;
  781. if (!page_bad->io_error)
  782. continue;
  783. for (mirror_index = 0;
  784. mirror_index < BTRFS_MAX_MIRRORS &&
  785. sblocks_for_recheck[mirror_index].page_count > 0;
  786. mirror_index++) {
  787. struct scrub_block *sblock_other = sblocks_for_recheck +
  788. mirror_index;
  789. struct scrub_page *page_other = sblock_other->pagev +
  790. page_num;
  791. if (!page_other->io_error) {
  792. ret = scrub_repair_page_from_good_copy(
  793. sblock_bad, sblock_other, page_num, 0);
  794. if (0 == ret) {
  795. page_bad->io_error = 0;
  796. break; /* succeeded for this page */
  797. }
  798. }
  799. }
  800. if (page_bad->io_error) {
  801. /* did not find a mirror to copy the page from */
  802. success = 0;
  803. }
  804. }
  805. if (success) {
  806. if (is_metadata || have_csum) {
  807. /*
  808. * need to verify the checksum now that all
  809. * sectors on disk are repaired (the write
  810. * request for data to be repaired is on its way).
  811. * Just be lazy and use scrub_recheck_block()
  812. * which re-reads the data before the checksum
  813. * is verified, but most likely the data comes out
  814. * of the page cache.
  815. */
  816. ret = scrub_recheck_block(fs_info, sblock_bad,
  817. is_metadata, have_csum, csum,
  818. generation, sdev->csum_size);
  819. if (!ret && !sblock_bad->header_error &&
  820. !sblock_bad->checksum_error &&
  821. sblock_bad->no_io_error_seen)
  822. goto corrected_error;
  823. else
  824. goto did_not_correct_error;
  825. } else {
  826. corrected_error:
  827. spin_lock(&sdev->stat_lock);
  828. sdev->stat.corrected_errors++;
  829. spin_unlock(&sdev->stat_lock);
  830. printk_ratelimited(KERN_ERR
  831. "btrfs: fixed up error at logical %llu on dev %s\n",
  832. (unsigned long long)logical, sdev->dev->name);
  833. }
  834. } else {
  835. did_not_correct_error:
  836. spin_lock(&sdev->stat_lock);
  837. sdev->stat.uncorrectable_errors++;
  838. spin_unlock(&sdev->stat_lock);
  839. printk_ratelimited(KERN_ERR
  840. "btrfs: unable to fixup (regular) error at logical %llu on dev %s\n",
  841. (unsigned long long)logical, sdev->dev->name);
  842. }
  843. out:
  844. if (sblocks_for_recheck) {
  845. for (mirror_index = 0; mirror_index < BTRFS_MAX_MIRRORS;
  846. mirror_index++) {
  847. struct scrub_block *sblock = sblocks_for_recheck +
  848. mirror_index;
  849. int page_index;
  850. for (page_index = 0; page_index < SCRUB_PAGES_PER_BIO;
  851. page_index++)
  852. if (sblock->pagev[page_index].page)
  853. __free_page(
  854. sblock->pagev[page_index].page);
  855. }
  856. kfree(sblocks_for_recheck);
  857. }
  858. return 0;
  859. }
  860. static int scrub_setup_recheck_block(struct scrub_dev *sdev,
  861. struct btrfs_mapping_tree *map_tree,
  862. u64 length, u64 logical,
  863. struct scrub_block *sblocks_for_recheck)
  864. {
  865. int page_index;
  866. int mirror_index;
  867. int ret;
  868. /*
  869. * note: the three members sdev, ref_count and outstanding_pages
  870. * are not used (and not set) in the blocks that are used for
  871. * the recheck procedure
  872. */
  873. page_index = 0;
  874. while (length > 0) {
  875. u64 sublen = min_t(u64, length, PAGE_SIZE);
  876. u64 mapped_length = sublen;
  877. struct btrfs_bio *bbio = NULL;
  878. /*
  879. * with a length of PAGE_SIZE, each returned stripe
  880. * represents one mirror
  881. */
  882. ret = btrfs_map_block(map_tree, WRITE, logical, &mapped_length,
  883. &bbio, 0);
  884. if (ret || !bbio || mapped_length < sublen) {
  885. kfree(bbio);
  886. return -EIO;
  887. }
  888. BUG_ON(page_index >= SCRUB_PAGES_PER_BIO);
  889. for (mirror_index = 0; mirror_index < (int)bbio->num_stripes;
  890. mirror_index++) {
  891. struct scrub_block *sblock;
  892. struct scrub_page *page;
  893. if (mirror_index >= BTRFS_MAX_MIRRORS)
  894. continue;
  895. sblock = sblocks_for_recheck + mirror_index;
  896. page = sblock->pagev + page_index;
  897. page->logical = logical;
  898. page->physical = bbio->stripes[mirror_index].physical;
  899. /* for missing devices, bdev is NULL */
  900. page->bdev = bbio->stripes[mirror_index].dev->bdev;
  901. page->mirror_num = mirror_index + 1;
  902. page->page = alloc_page(GFP_NOFS);
  903. if (!page->page) {
  904. spin_lock(&sdev->stat_lock);
  905. sdev->stat.malloc_errors++;
  906. spin_unlock(&sdev->stat_lock);
  907. return -ENOMEM;
  908. }
  909. sblock->page_count++;
  910. }
  911. kfree(bbio);
  912. length -= sublen;
  913. logical += sublen;
  914. page_index++;
  915. }
  916. return 0;
  917. }
  918. /*
  919. * this function will check the on disk data for checksum errors, header
  920. * errors and read I/O errors. If any I/O errors happen, the exact pages
  921. * which are errored are marked as being bad. The goal is to enable scrub
  922. * to take those pages that are not errored from all the mirrors so that
  923. * the pages that are errored in the just handled mirror can be repaired.
  924. */
  925. static int scrub_recheck_block(struct btrfs_fs_info *fs_info,
  926. struct scrub_block *sblock, int is_metadata,
  927. int have_csum, u8 *csum, u64 generation,
  928. u16 csum_size)
  929. {
  930. int page_num;
  931. sblock->no_io_error_seen = 1;
  932. sblock->header_error = 0;
  933. sblock->checksum_error = 0;
  934. for (page_num = 0; page_num < sblock->page_count; page_num++) {
  935. struct bio *bio;
  936. int ret;
  937. struct scrub_page *page = sblock->pagev + page_num;
  938. DECLARE_COMPLETION_ONSTACK(complete);
  939. if (page->bdev == NULL) {
  940. page->io_error = 1;
  941. sblock->no_io_error_seen = 0;
  942. continue;
  943. }
  944. BUG_ON(!page->page);
  945. bio = bio_alloc(GFP_NOFS, 1);
  946. if (!bio)
  947. return -EIO;
  948. bio->bi_bdev = page->bdev;
  949. bio->bi_sector = page->physical >> 9;
  950. bio->bi_end_io = scrub_complete_bio_end_io;
  951. bio->bi_private = &complete;
  952. ret = bio_add_page(bio, page->page, PAGE_SIZE, 0);
  953. if (PAGE_SIZE != ret) {
  954. bio_put(bio);
  955. return -EIO;
  956. }
  957. btrfsic_submit_bio(READ, bio);
  958. /* this will also unplug the queue */
  959. wait_for_completion(&complete);
  960. page->io_error = !test_bit(BIO_UPTODATE, &bio->bi_flags);
  961. if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
  962. sblock->no_io_error_seen = 0;
  963. bio_put(bio);
  964. }
  965. if (sblock->no_io_error_seen)
  966. scrub_recheck_block_checksum(fs_info, sblock, is_metadata,
  967. have_csum, csum, generation,
  968. csum_size);
  969. return 0;
  970. }
  971. static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
  972. struct scrub_block *sblock,
  973. int is_metadata, int have_csum,
  974. const u8 *csum, u64 generation,
  975. u16 csum_size)
  976. {
  977. int page_num;
  978. u8 calculated_csum[BTRFS_CSUM_SIZE];
  979. u32 crc = ~(u32)0;
  980. struct btrfs_root *root = fs_info->extent_root;
  981. void *mapped_buffer;
  982. BUG_ON(!sblock->pagev[0].page);
  983. if (is_metadata) {
  984. struct btrfs_header *h;
  985. mapped_buffer = kmap_atomic(sblock->pagev[0].page);
  986. h = (struct btrfs_header *)mapped_buffer;
  987. if (sblock->pagev[0].logical != le64_to_cpu(h->bytenr) ||
  988. generation != le64_to_cpu(h->generation) ||
  989. memcmp(h->fsid, fs_info->fsid, BTRFS_UUID_SIZE) ||
  990. memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
  991. BTRFS_UUID_SIZE))
  992. sblock->header_error = 1;
  993. csum = h->csum;
  994. } else {
  995. if (!have_csum)
  996. return;
  997. mapped_buffer = kmap_atomic(sblock->pagev[0].page);
  998. }
  999. for (page_num = 0;;) {
  1000. if (page_num == 0 && is_metadata)
  1001. crc = btrfs_csum_data(root,
  1002. ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE,
  1003. crc, PAGE_SIZE - BTRFS_CSUM_SIZE);
  1004. else
  1005. crc = btrfs_csum_data(root, mapped_buffer, crc,
  1006. PAGE_SIZE);
  1007. kunmap_atomic(mapped_buffer);
  1008. page_num++;
  1009. if (page_num >= sblock->page_count)
  1010. break;
  1011. BUG_ON(!sblock->pagev[page_num].page);
  1012. mapped_buffer = kmap_atomic(sblock->pagev[page_num].page);
  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);
  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);
  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);
  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);
  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);
  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);
  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);
  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);
  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);
  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. }