scrub.c 66 KB

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