scrub.c 66 KB

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