scrub.c 68 KB

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