scrub.c 66 KB

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