disk-io.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401
  1. /*
  2. * Copyright (C) 2007 Oracle. 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/version.h>
  19. #include <linux/fs.h>
  20. #include <linux/blkdev.h>
  21. #include <linux/scatterlist.h>
  22. #include <linux/swap.h>
  23. #include <linux/radix-tree.h>
  24. #include <linux/writeback.h>
  25. #include <linux/buffer_head.h> // for block_sync_page
  26. #include <linux/workqueue.h>
  27. #include <linux/kthread.h>
  28. #include <linux/freezer.h>
  29. #include "compat.h"
  30. #include "crc32c.h"
  31. #include "ctree.h"
  32. #include "disk-io.h"
  33. #include "transaction.h"
  34. #include "btrfs_inode.h"
  35. #include "volumes.h"
  36. #include "print-tree.h"
  37. #include "async-thread.h"
  38. #include "locking.h"
  39. #include "ref-cache.h"
  40. #include "tree-log.h"
  41. #if 0
  42. static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
  43. {
  44. if (extent_buffer_blocknr(buf) != btrfs_header_blocknr(buf)) {
  45. printk(KERN_CRIT "buf blocknr(buf) is %llu, header is %llu\n",
  46. (unsigned long long)extent_buffer_blocknr(buf),
  47. (unsigned long long)btrfs_header_blocknr(buf));
  48. return 1;
  49. }
  50. return 0;
  51. }
  52. #endif
  53. static struct extent_io_ops btree_extent_io_ops;
  54. static void end_workqueue_fn(struct btrfs_work *work);
  55. /*
  56. * end_io_wq structs are used to do processing in task context when an IO is
  57. * complete. This is used during reads to verify checksums, and it is used
  58. * by writes to insert metadata for new file extents after IO is complete.
  59. */
  60. struct end_io_wq {
  61. struct bio *bio;
  62. bio_end_io_t *end_io;
  63. void *private;
  64. struct btrfs_fs_info *info;
  65. int error;
  66. int metadata;
  67. struct list_head list;
  68. struct btrfs_work work;
  69. };
  70. /*
  71. * async submit bios are used to offload expensive checksumming
  72. * onto the worker threads. They checksum file and metadata bios
  73. * just before they are sent down the IO stack.
  74. */
  75. struct async_submit_bio {
  76. struct inode *inode;
  77. struct bio *bio;
  78. struct list_head list;
  79. extent_submit_bio_hook_t *submit_bio_start;
  80. extent_submit_bio_hook_t *submit_bio_done;
  81. int rw;
  82. int mirror_num;
  83. unsigned long bio_flags;
  84. struct btrfs_work work;
  85. };
  86. /*
  87. * extents on the btree inode are pretty simple, there's one extent
  88. * that covers the entire device
  89. */
  90. static struct extent_map *btree_get_extent(struct inode *inode,
  91. struct page *page, size_t page_offset, u64 start, u64 len,
  92. int create)
  93. {
  94. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  95. struct extent_map *em;
  96. int ret;
  97. spin_lock(&em_tree->lock);
  98. em = lookup_extent_mapping(em_tree, start, len);
  99. if (em) {
  100. em->bdev =
  101. BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
  102. spin_unlock(&em_tree->lock);
  103. goto out;
  104. }
  105. spin_unlock(&em_tree->lock);
  106. em = alloc_extent_map(GFP_NOFS);
  107. if (!em) {
  108. em = ERR_PTR(-ENOMEM);
  109. goto out;
  110. }
  111. em->start = 0;
  112. em->len = (u64)-1;
  113. em->block_len = (u64)-1;
  114. em->block_start = 0;
  115. em->bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
  116. spin_lock(&em_tree->lock);
  117. ret = add_extent_mapping(em_tree, em);
  118. if (ret == -EEXIST) {
  119. u64 failed_start = em->start;
  120. u64 failed_len = em->len;
  121. printk("failed to insert %Lu %Lu -> %Lu into tree\n",
  122. em->start, em->len, em->block_start);
  123. free_extent_map(em);
  124. em = lookup_extent_mapping(em_tree, start, len);
  125. if (em) {
  126. printk("after failing, found %Lu %Lu %Lu\n",
  127. em->start, em->len, em->block_start);
  128. ret = 0;
  129. } else {
  130. em = lookup_extent_mapping(em_tree, failed_start,
  131. failed_len);
  132. if (em) {
  133. printk("double failure lookup gives us "
  134. "%Lu %Lu -> %Lu\n", em->start,
  135. em->len, em->block_start);
  136. free_extent_map(em);
  137. }
  138. ret = -EIO;
  139. }
  140. } else if (ret) {
  141. free_extent_map(em);
  142. em = NULL;
  143. }
  144. spin_unlock(&em_tree->lock);
  145. if (ret)
  146. em = ERR_PTR(ret);
  147. out:
  148. return em;
  149. }
  150. u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
  151. {
  152. return btrfs_crc32c(seed, data, len);
  153. }
  154. void btrfs_csum_final(u32 crc, char *result)
  155. {
  156. *(__le32 *)result = ~cpu_to_le32(crc);
  157. }
  158. /*
  159. * compute the csum for a btree block, and either verify it or write it
  160. * into the csum field of the block.
  161. */
  162. static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
  163. int verify)
  164. {
  165. u16 csum_size =
  166. btrfs_super_csum_size(&root->fs_info->super_copy);
  167. char *result = NULL;
  168. unsigned long len;
  169. unsigned long cur_len;
  170. unsigned long offset = BTRFS_CSUM_SIZE;
  171. char *map_token = NULL;
  172. char *kaddr;
  173. unsigned long map_start;
  174. unsigned long map_len;
  175. int err;
  176. u32 crc = ~(u32)0;
  177. unsigned long inline_result;
  178. len = buf->len - offset;
  179. while(len > 0) {
  180. err = map_private_extent_buffer(buf, offset, 32,
  181. &map_token, &kaddr,
  182. &map_start, &map_len, KM_USER0);
  183. if (err) {
  184. printk("failed to map extent buffer! %lu\n",
  185. offset);
  186. return 1;
  187. }
  188. cur_len = min(len, map_len - (offset - map_start));
  189. crc = btrfs_csum_data(root, kaddr + offset - map_start,
  190. crc, cur_len);
  191. len -= cur_len;
  192. offset += cur_len;
  193. unmap_extent_buffer(buf, map_token, KM_USER0);
  194. }
  195. if (csum_size > sizeof(inline_result)) {
  196. result = kzalloc(csum_size * sizeof(char), GFP_NOFS);
  197. if (!result)
  198. return 1;
  199. } else {
  200. result = (char *)&inline_result;
  201. }
  202. btrfs_csum_final(crc, result);
  203. if (verify) {
  204. /* FIXME, this is not good */
  205. if (memcmp_extent_buffer(buf, result, 0, csum_size)) {
  206. u32 val;
  207. u32 found = 0;
  208. memcpy(&found, result, csum_size);
  209. read_extent_buffer(buf, &val, 0, csum_size);
  210. printk("btrfs: %s checksum verify failed on %llu "
  211. "wanted %X found %X level %d\n",
  212. root->fs_info->sb->s_id,
  213. buf->start, val, found, btrfs_header_level(buf));
  214. if (result != (char *)&inline_result)
  215. kfree(result);
  216. return 1;
  217. }
  218. } else {
  219. write_extent_buffer(buf, result, 0, csum_size);
  220. }
  221. if (result != (char *)&inline_result)
  222. kfree(result);
  223. return 0;
  224. }
  225. /*
  226. * we can't consider a given block up to date unless the transid of the
  227. * block matches the transid in the parent node's pointer. This is how we
  228. * detect blocks that either didn't get written at all or got written
  229. * in the wrong place.
  230. */
  231. static int verify_parent_transid(struct extent_io_tree *io_tree,
  232. struct extent_buffer *eb, u64 parent_transid)
  233. {
  234. int ret;
  235. if (!parent_transid || btrfs_header_generation(eb) == parent_transid)
  236. return 0;
  237. lock_extent(io_tree, eb->start, eb->start + eb->len - 1, GFP_NOFS);
  238. if (extent_buffer_uptodate(io_tree, eb) &&
  239. btrfs_header_generation(eb) == parent_transid) {
  240. ret = 0;
  241. goto out;
  242. }
  243. printk("parent transid verify failed on %llu wanted %llu found %llu\n",
  244. (unsigned long long)eb->start,
  245. (unsigned long long)parent_transid,
  246. (unsigned long long)btrfs_header_generation(eb));
  247. ret = 1;
  248. clear_extent_buffer_uptodate(io_tree, eb);
  249. out:
  250. unlock_extent(io_tree, eb->start, eb->start + eb->len - 1,
  251. GFP_NOFS);
  252. return ret;
  253. }
  254. /*
  255. * helper to read a given tree block, doing retries as required when
  256. * the checksums don't match and we have alternate mirrors to try.
  257. */
  258. static int btree_read_extent_buffer_pages(struct btrfs_root *root,
  259. struct extent_buffer *eb,
  260. u64 start, u64 parent_transid)
  261. {
  262. struct extent_io_tree *io_tree;
  263. int ret;
  264. int num_copies = 0;
  265. int mirror_num = 0;
  266. io_tree = &BTRFS_I(root->fs_info->btree_inode)->io_tree;
  267. while (1) {
  268. ret = read_extent_buffer_pages(io_tree, eb, start, 1,
  269. btree_get_extent, mirror_num);
  270. if (!ret &&
  271. !verify_parent_transid(io_tree, eb, parent_transid))
  272. return ret;
  273. printk("read extent buffer pages failed with ret %d mirror no %d\n", ret, mirror_num);
  274. num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
  275. eb->start, eb->len);
  276. if (num_copies == 1)
  277. return ret;
  278. mirror_num++;
  279. if (mirror_num > num_copies)
  280. return ret;
  281. }
  282. return -EIO;
  283. }
  284. /*
  285. * checksum a dirty tree block before IO. This has extra checks to make
  286. * sure we only fill in the checksum field in the first page of a multi-page block
  287. */
  288. static int csum_dirty_buffer(struct btrfs_root *root, struct page *page)
  289. {
  290. struct extent_io_tree *tree;
  291. u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
  292. u64 found_start;
  293. int found_level;
  294. unsigned long len;
  295. struct extent_buffer *eb;
  296. int ret;
  297. tree = &BTRFS_I(page->mapping->host)->io_tree;
  298. if (page->private == EXTENT_PAGE_PRIVATE)
  299. goto out;
  300. if (!page->private)
  301. goto out;
  302. len = page->private >> 2;
  303. if (len == 0) {
  304. WARN_ON(1);
  305. }
  306. eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS);
  307. ret = btree_read_extent_buffer_pages(root, eb, start + PAGE_CACHE_SIZE,
  308. btrfs_header_generation(eb));
  309. BUG_ON(ret);
  310. found_start = btrfs_header_bytenr(eb);
  311. if (found_start != start) {
  312. printk("warning: eb start incorrect %Lu buffer %Lu len %lu\n",
  313. start, found_start, len);
  314. WARN_ON(1);
  315. goto err;
  316. }
  317. if (eb->first_page != page) {
  318. printk("bad first page %lu %lu\n", eb->first_page->index,
  319. page->index);
  320. WARN_ON(1);
  321. goto err;
  322. }
  323. if (!PageUptodate(page)) {
  324. printk("csum not up to date page %lu\n", page->index);
  325. WARN_ON(1);
  326. goto err;
  327. }
  328. found_level = btrfs_header_level(eb);
  329. csum_tree_block(root, eb, 0);
  330. err:
  331. free_extent_buffer(eb);
  332. out:
  333. return 0;
  334. }
  335. static int check_tree_block_fsid(struct btrfs_root *root,
  336. struct extent_buffer *eb)
  337. {
  338. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  339. u8 fsid[BTRFS_UUID_SIZE];
  340. int ret = 1;
  341. read_extent_buffer(eb, fsid, (unsigned long)btrfs_header_fsid(eb),
  342. BTRFS_FSID_SIZE);
  343. while (fs_devices) {
  344. if (!memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE)) {
  345. ret = 0;
  346. break;
  347. }
  348. fs_devices = fs_devices->seed;
  349. }
  350. return ret;
  351. }
  352. static int btree_readpage_end_io_hook(struct page *page, u64 start, u64 end,
  353. struct extent_state *state)
  354. {
  355. struct extent_io_tree *tree;
  356. u64 found_start;
  357. int found_level;
  358. unsigned long len;
  359. struct extent_buffer *eb;
  360. struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
  361. int ret = 0;
  362. tree = &BTRFS_I(page->mapping->host)->io_tree;
  363. if (page->private == EXTENT_PAGE_PRIVATE)
  364. goto out;
  365. if (!page->private)
  366. goto out;
  367. len = page->private >> 2;
  368. if (len == 0) {
  369. WARN_ON(1);
  370. }
  371. eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS);
  372. found_start = btrfs_header_bytenr(eb);
  373. if (found_start != start) {
  374. printk("bad tree block start %llu %llu\n",
  375. (unsigned long long)found_start,
  376. (unsigned long long)eb->start);
  377. ret = -EIO;
  378. goto err;
  379. }
  380. if (eb->first_page != page) {
  381. printk("bad first page %lu %lu\n", eb->first_page->index,
  382. page->index);
  383. WARN_ON(1);
  384. ret = -EIO;
  385. goto err;
  386. }
  387. if (check_tree_block_fsid(root, eb)) {
  388. printk("bad fsid on block %Lu\n", eb->start);
  389. ret = -EIO;
  390. goto err;
  391. }
  392. found_level = btrfs_header_level(eb);
  393. ret = csum_tree_block(root, eb, 1);
  394. if (ret)
  395. ret = -EIO;
  396. end = min_t(u64, eb->len, PAGE_CACHE_SIZE);
  397. end = eb->start + end - 1;
  398. err:
  399. free_extent_buffer(eb);
  400. out:
  401. return ret;
  402. }
  403. static void end_workqueue_bio(struct bio *bio, int err)
  404. {
  405. struct end_io_wq *end_io_wq = bio->bi_private;
  406. struct btrfs_fs_info *fs_info;
  407. fs_info = end_io_wq->info;
  408. end_io_wq->error = err;
  409. end_io_wq->work.func = end_workqueue_fn;
  410. end_io_wq->work.flags = 0;
  411. if (bio->bi_rw & (1 << BIO_RW)) {
  412. if (end_io_wq->metadata)
  413. btrfs_queue_worker(&fs_info->endio_meta_write_workers,
  414. &end_io_wq->work);
  415. else
  416. btrfs_queue_worker(&fs_info->endio_write_workers,
  417. &end_io_wq->work);
  418. } else {
  419. if (end_io_wq->metadata)
  420. btrfs_queue_worker(&fs_info->endio_meta_workers,
  421. &end_io_wq->work);
  422. else
  423. btrfs_queue_worker(&fs_info->endio_workers,
  424. &end_io_wq->work);
  425. }
  426. }
  427. int btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio,
  428. int metadata)
  429. {
  430. struct end_io_wq *end_io_wq;
  431. end_io_wq = kmalloc(sizeof(*end_io_wq), GFP_NOFS);
  432. if (!end_io_wq)
  433. return -ENOMEM;
  434. end_io_wq->private = bio->bi_private;
  435. end_io_wq->end_io = bio->bi_end_io;
  436. end_io_wq->info = info;
  437. end_io_wq->error = 0;
  438. end_io_wq->bio = bio;
  439. end_io_wq->metadata = metadata;
  440. bio->bi_private = end_io_wq;
  441. bio->bi_end_io = end_workqueue_bio;
  442. return 0;
  443. }
  444. unsigned long btrfs_async_submit_limit(struct btrfs_fs_info *info)
  445. {
  446. unsigned long limit = min_t(unsigned long,
  447. info->workers.max_workers,
  448. info->fs_devices->open_devices);
  449. return 256 * limit;
  450. }
  451. int btrfs_congested_async(struct btrfs_fs_info *info, int iodone)
  452. {
  453. return atomic_read(&info->nr_async_bios) >
  454. btrfs_async_submit_limit(info);
  455. }
  456. static void run_one_async_start(struct btrfs_work *work)
  457. {
  458. struct btrfs_fs_info *fs_info;
  459. struct async_submit_bio *async;
  460. async = container_of(work, struct async_submit_bio, work);
  461. fs_info = BTRFS_I(async->inode)->root->fs_info;
  462. async->submit_bio_start(async->inode, async->rw, async->bio,
  463. async->mirror_num, async->bio_flags);
  464. }
  465. static void run_one_async_done(struct btrfs_work *work)
  466. {
  467. struct btrfs_fs_info *fs_info;
  468. struct async_submit_bio *async;
  469. int limit;
  470. async = container_of(work, struct async_submit_bio, work);
  471. fs_info = BTRFS_I(async->inode)->root->fs_info;
  472. limit = btrfs_async_submit_limit(fs_info);
  473. limit = limit * 2 / 3;
  474. atomic_dec(&fs_info->nr_async_submits);
  475. if (atomic_read(&fs_info->nr_async_submits) < limit &&
  476. waitqueue_active(&fs_info->async_submit_wait))
  477. wake_up(&fs_info->async_submit_wait);
  478. async->submit_bio_done(async->inode, async->rw, async->bio,
  479. async->mirror_num, async->bio_flags);
  480. }
  481. static void run_one_async_free(struct btrfs_work *work)
  482. {
  483. struct async_submit_bio *async;
  484. async = container_of(work, struct async_submit_bio, work);
  485. kfree(async);
  486. }
  487. int btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct inode *inode,
  488. int rw, struct bio *bio, int mirror_num,
  489. unsigned long bio_flags,
  490. extent_submit_bio_hook_t *submit_bio_start,
  491. extent_submit_bio_hook_t *submit_bio_done)
  492. {
  493. struct async_submit_bio *async;
  494. async = kmalloc(sizeof(*async), GFP_NOFS);
  495. if (!async)
  496. return -ENOMEM;
  497. async->inode = inode;
  498. async->rw = rw;
  499. async->bio = bio;
  500. async->mirror_num = mirror_num;
  501. async->submit_bio_start = submit_bio_start;
  502. async->submit_bio_done = submit_bio_done;
  503. async->work.func = run_one_async_start;
  504. async->work.ordered_func = run_one_async_done;
  505. async->work.ordered_free = run_one_async_free;
  506. async->work.flags = 0;
  507. async->bio_flags = bio_flags;
  508. atomic_inc(&fs_info->nr_async_submits);
  509. btrfs_queue_worker(&fs_info->workers, &async->work);
  510. #if 0
  511. int limit = btrfs_async_submit_limit(fs_info);
  512. if (atomic_read(&fs_info->nr_async_submits) > limit) {
  513. wait_event_timeout(fs_info->async_submit_wait,
  514. (atomic_read(&fs_info->nr_async_submits) < limit),
  515. HZ/10);
  516. wait_event_timeout(fs_info->async_submit_wait,
  517. (atomic_read(&fs_info->nr_async_bios) < limit),
  518. HZ/10);
  519. }
  520. #endif
  521. while(atomic_read(&fs_info->async_submit_draining) &&
  522. atomic_read(&fs_info->nr_async_submits)) {
  523. wait_event(fs_info->async_submit_wait,
  524. (atomic_read(&fs_info->nr_async_submits) == 0));
  525. }
  526. return 0;
  527. }
  528. static int btree_csum_one_bio(struct bio *bio)
  529. {
  530. struct bio_vec *bvec = bio->bi_io_vec;
  531. int bio_index = 0;
  532. struct btrfs_root *root;
  533. WARN_ON(bio->bi_vcnt <= 0);
  534. while(bio_index < bio->bi_vcnt) {
  535. root = BTRFS_I(bvec->bv_page->mapping->host)->root;
  536. csum_dirty_buffer(root, bvec->bv_page);
  537. bio_index++;
  538. bvec++;
  539. }
  540. return 0;
  541. }
  542. static int __btree_submit_bio_start(struct inode *inode, int rw,
  543. struct bio *bio, int mirror_num,
  544. unsigned long bio_flags)
  545. {
  546. /*
  547. * when we're called for a write, we're already in the async
  548. * submission context. Just jump into btrfs_map_bio
  549. */
  550. btree_csum_one_bio(bio);
  551. return 0;
  552. }
  553. static int __btree_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
  554. int mirror_num, unsigned long bio_flags)
  555. {
  556. /*
  557. * when we're called for a write, we're already in the async
  558. * submission context. Just jump into btrfs_map_bio
  559. */
  560. return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, mirror_num, 1);
  561. }
  562. static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
  563. int mirror_num, unsigned long bio_flags)
  564. {
  565. int ret;
  566. ret = btrfs_bio_wq_end_io(BTRFS_I(inode)->root->fs_info,
  567. bio, 1);
  568. BUG_ON(ret);
  569. if (!(rw & (1 << BIO_RW))) {
  570. /*
  571. * called for a read, do the setup so that checksum validation
  572. * can happen in the async kernel threads
  573. */
  574. return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio,
  575. mirror_num, 0);
  576. }
  577. /*
  578. * kthread helpers are used to submit writes so that checksumming
  579. * can happen in parallel across all CPUs
  580. */
  581. return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
  582. inode, rw, bio, mirror_num, 0,
  583. __btree_submit_bio_start,
  584. __btree_submit_bio_done);
  585. }
  586. static int btree_writepage(struct page *page, struct writeback_control *wbc)
  587. {
  588. struct extent_io_tree *tree;
  589. tree = &BTRFS_I(page->mapping->host)->io_tree;
  590. if (current->flags & PF_MEMALLOC) {
  591. redirty_page_for_writepage(wbc, page);
  592. unlock_page(page);
  593. return 0;
  594. }
  595. return extent_write_full_page(tree, page, btree_get_extent, wbc);
  596. }
  597. static int btree_writepages(struct address_space *mapping,
  598. struct writeback_control *wbc)
  599. {
  600. struct extent_io_tree *tree;
  601. tree = &BTRFS_I(mapping->host)->io_tree;
  602. if (wbc->sync_mode == WB_SYNC_NONE) {
  603. u64 num_dirty;
  604. u64 start = 0;
  605. unsigned long thresh = 32 * 1024 * 1024;
  606. if (wbc->for_kupdate)
  607. return 0;
  608. num_dirty = count_range_bits(tree, &start, (u64)-1,
  609. thresh, EXTENT_DIRTY);
  610. if (num_dirty < thresh) {
  611. return 0;
  612. }
  613. }
  614. return extent_writepages(tree, mapping, btree_get_extent, wbc);
  615. }
  616. static int btree_readpage(struct file *file, struct page *page)
  617. {
  618. struct extent_io_tree *tree;
  619. tree = &BTRFS_I(page->mapping->host)->io_tree;
  620. return extent_read_full_page(tree, page, btree_get_extent);
  621. }
  622. static int btree_releasepage(struct page *page, gfp_t gfp_flags)
  623. {
  624. struct extent_io_tree *tree;
  625. struct extent_map_tree *map;
  626. int ret;
  627. if (PageWriteback(page) || PageDirty(page))
  628. return 0;
  629. tree = &BTRFS_I(page->mapping->host)->io_tree;
  630. map = &BTRFS_I(page->mapping->host)->extent_tree;
  631. ret = try_release_extent_state(map, tree, page, gfp_flags);
  632. if (!ret) {
  633. return 0;
  634. }
  635. ret = try_release_extent_buffer(tree, page);
  636. if (ret == 1) {
  637. ClearPagePrivate(page);
  638. set_page_private(page, 0);
  639. page_cache_release(page);
  640. }
  641. return ret;
  642. }
  643. static void btree_invalidatepage(struct page *page, unsigned long offset)
  644. {
  645. struct extent_io_tree *tree;
  646. tree = &BTRFS_I(page->mapping->host)->io_tree;
  647. extent_invalidatepage(tree, page, offset);
  648. btree_releasepage(page, GFP_NOFS);
  649. if (PagePrivate(page)) {
  650. printk("warning page private not zero on page %Lu\n",
  651. page_offset(page));
  652. ClearPagePrivate(page);
  653. set_page_private(page, 0);
  654. page_cache_release(page);
  655. }
  656. }
  657. #if 0
  658. static int btree_writepage(struct page *page, struct writeback_control *wbc)
  659. {
  660. struct buffer_head *bh;
  661. struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
  662. struct buffer_head *head;
  663. if (!page_has_buffers(page)) {
  664. create_empty_buffers(page, root->fs_info->sb->s_blocksize,
  665. (1 << BH_Dirty)|(1 << BH_Uptodate));
  666. }
  667. head = page_buffers(page);
  668. bh = head;
  669. do {
  670. if (buffer_dirty(bh))
  671. csum_tree_block(root, bh, 0);
  672. bh = bh->b_this_page;
  673. } while (bh != head);
  674. return block_write_full_page(page, btree_get_block, wbc);
  675. }
  676. #endif
  677. static struct address_space_operations btree_aops = {
  678. .readpage = btree_readpage,
  679. .writepage = btree_writepage,
  680. .writepages = btree_writepages,
  681. .releasepage = btree_releasepage,
  682. .invalidatepage = btree_invalidatepage,
  683. .sync_page = block_sync_page,
  684. };
  685. int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize,
  686. u64 parent_transid)
  687. {
  688. struct extent_buffer *buf = NULL;
  689. struct inode *btree_inode = root->fs_info->btree_inode;
  690. int ret = 0;
  691. buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
  692. if (!buf)
  693. return 0;
  694. read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
  695. buf, 0, 0, btree_get_extent, 0);
  696. free_extent_buffer(buf);
  697. return ret;
  698. }
  699. struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
  700. u64 bytenr, u32 blocksize)
  701. {
  702. struct inode *btree_inode = root->fs_info->btree_inode;
  703. struct extent_buffer *eb;
  704. eb = find_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
  705. bytenr, blocksize, GFP_NOFS);
  706. return eb;
  707. }
  708. struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
  709. u64 bytenr, u32 blocksize)
  710. {
  711. struct inode *btree_inode = root->fs_info->btree_inode;
  712. struct extent_buffer *eb;
  713. eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
  714. bytenr, blocksize, NULL, GFP_NOFS);
  715. return eb;
  716. }
  717. int btrfs_write_tree_block(struct extent_buffer *buf)
  718. {
  719. return btrfs_fdatawrite_range(buf->first_page->mapping, buf->start,
  720. buf->start + buf->len - 1, WB_SYNC_ALL);
  721. }
  722. int btrfs_wait_tree_block_writeback(struct extent_buffer *buf)
  723. {
  724. return btrfs_wait_on_page_writeback_range(buf->first_page->mapping,
  725. buf->start, buf->start + buf->len -1);
  726. }
  727. struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
  728. u32 blocksize, u64 parent_transid)
  729. {
  730. struct extent_buffer *buf = NULL;
  731. struct inode *btree_inode = root->fs_info->btree_inode;
  732. struct extent_io_tree *io_tree;
  733. int ret;
  734. io_tree = &BTRFS_I(btree_inode)->io_tree;
  735. buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
  736. if (!buf)
  737. return NULL;
  738. ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid);
  739. if (ret == 0) {
  740. buf->flags |= EXTENT_UPTODATE;
  741. } else {
  742. WARN_ON(1);
  743. }
  744. return buf;
  745. }
  746. int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  747. struct extent_buffer *buf)
  748. {
  749. struct inode *btree_inode = root->fs_info->btree_inode;
  750. if (btrfs_header_generation(buf) ==
  751. root->fs_info->running_transaction->transid) {
  752. WARN_ON(!btrfs_tree_locked(buf));
  753. clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree,
  754. buf);
  755. }
  756. return 0;
  757. }
  758. static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
  759. u32 stripesize, struct btrfs_root *root,
  760. struct btrfs_fs_info *fs_info,
  761. u64 objectid)
  762. {
  763. root->node = NULL;
  764. root->commit_root = NULL;
  765. root->ref_tree = NULL;
  766. root->sectorsize = sectorsize;
  767. root->nodesize = nodesize;
  768. root->leafsize = leafsize;
  769. root->stripesize = stripesize;
  770. root->ref_cows = 0;
  771. root->track_dirty = 0;
  772. root->fs_info = fs_info;
  773. root->objectid = objectid;
  774. root->last_trans = 0;
  775. root->highest_inode = 0;
  776. root->last_inode_alloc = 0;
  777. root->name = NULL;
  778. root->in_sysfs = 0;
  779. INIT_LIST_HEAD(&root->dirty_list);
  780. INIT_LIST_HEAD(&root->orphan_list);
  781. INIT_LIST_HEAD(&root->dead_list);
  782. spin_lock_init(&root->node_lock);
  783. spin_lock_init(&root->list_lock);
  784. mutex_init(&root->objectid_mutex);
  785. mutex_init(&root->log_mutex);
  786. extent_io_tree_init(&root->dirty_log_pages,
  787. fs_info->btree_inode->i_mapping, GFP_NOFS);
  788. btrfs_leaf_ref_tree_init(&root->ref_tree_struct);
  789. root->ref_tree = &root->ref_tree_struct;
  790. memset(&root->root_key, 0, sizeof(root->root_key));
  791. memset(&root->root_item, 0, sizeof(root->root_item));
  792. memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
  793. memset(&root->root_kobj, 0, sizeof(root->root_kobj));
  794. root->defrag_trans_start = fs_info->generation;
  795. init_completion(&root->kobj_unregister);
  796. root->defrag_running = 0;
  797. root->defrag_level = 0;
  798. root->root_key.objectid = objectid;
  799. root->anon_super.s_root = NULL;
  800. root->anon_super.s_dev = 0;
  801. INIT_LIST_HEAD(&root->anon_super.s_list);
  802. INIT_LIST_HEAD(&root->anon_super.s_instances);
  803. init_rwsem(&root->anon_super.s_umount);
  804. return 0;
  805. }
  806. static int find_and_setup_root(struct btrfs_root *tree_root,
  807. struct btrfs_fs_info *fs_info,
  808. u64 objectid,
  809. struct btrfs_root *root)
  810. {
  811. int ret;
  812. u32 blocksize;
  813. u64 generation;
  814. __setup_root(tree_root->nodesize, tree_root->leafsize,
  815. tree_root->sectorsize, tree_root->stripesize,
  816. root, fs_info, objectid);
  817. ret = btrfs_find_last_root(tree_root, objectid,
  818. &root->root_item, &root->root_key);
  819. BUG_ON(ret);
  820. generation = btrfs_root_generation(&root->root_item);
  821. blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
  822. root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
  823. blocksize, generation);
  824. BUG_ON(!root->node);
  825. return 0;
  826. }
  827. int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
  828. struct btrfs_fs_info *fs_info)
  829. {
  830. struct extent_buffer *eb;
  831. struct btrfs_root *log_root_tree = fs_info->log_root_tree;
  832. u64 start = 0;
  833. u64 end = 0;
  834. int ret;
  835. if (!log_root_tree)
  836. return 0;
  837. while(1) {
  838. ret = find_first_extent_bit(&log_root_tree->dirty_log_pages,
  839. 0, &start, &end, EXTENT_DIRTY);
  840. if (ret)
  841. break;
  842. clear_extent_dirty(&log_root_tree->dirty_log_pages,
  843. start, end, GFP_NOFS);
  844. }
  845. eb = fs_info->log_root_tree->node;
  846. WARN_ON(btrfs_header_level(eb) != 0);
  847. WARN_ON(btrfs_header_nritems(eb) != 0);
  848. ret = btrfs_free_reserved_extent(fs_info->tree_root,
  849. eb->start, eb->len);
  850. BUG_ON(ret);
  851. free_extent_buffer(eb);
  852. kfree(fs_info->log_root_tree);
  853. fs_info->log_root_tree = NULL;
  854. return 0;
  855. }
  856. int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans,
  857. struct btrfs_fs_info *fs_info)
  858. {
  859. struct btrfs_root *root;
  860. struct btrfs_root *tree_root = fs_info->tree_root;
  861. root = kzalloc(sizeof(*root), GFP_NOFS);
  862. if (!root)
  863. return -ENOMEM;
  864. __setup_root(tree_root->nodesize, tree_root->leafsize,
  865. tree_root->sectorsize, tree_root->stripesize,
  866. root, fs_info, BTRFS_TREE_LOG_OBJECTID);
  867. root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID;
  868. root->root_key.type = BTRFS_ROOT_ITEM_KEY;
  869. root->root_key.offset = BTRFS_TREE_LOG_OBJECTID;
  870. root->ref_cows = 0;
  871. root->node = btrfs_alloc_free_block(trans, root, root->leafsize,
  872. 0, BTRFS_TREE_LOG_OBJECTID,
  873. trans->transid, 0, 0, 0);
  874. btrfs_set_header_nritems(root->node, 0);
  875. btrfs_set_header_level(root->node, 0);
  876. btrfs_set_header_bytenr(root->node, root->node->start);
  877. btrfs_set_header_generation(root->node, trans->transid);
  878. btrfs_set_header_owner(root->node, BTRFS_TREE_LOG_OBJECTID);
  879. write_extent_buffer(root->node, root->fs_info->fsid,
  880. (unsigned long)btrfs_header_fsid(root->node),
  881. BTRFS_FSID_SIZE);
  882. btrfs_mark_buffer_dirty(root->node);
  883. btrfs_tree_unlock(root->node);
  884. fs_info->log_root_tree = root;
  885. return 0;
  886. }
  887. struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_root *tree_root,
  888. struct btrfs_key *location)
  889. {
  890. struct btrfs_root *root;
  891. struct btrfs_fs_info *fs_info = tree_root->fs_info;
  892. struct btrfs_path *path;
  893. struct extent_buffer *l;
  894. u64 highest_inode;
  895. u64 generation;
  896. u32 blocksize;
  897. int ret = 0;
  898. root = kzalloc(sizeof(*root), GFP_NOFS);
  899. if (!root)
  900. return ERR_PTR(-ENOMEM);
  901. if (location->offset == (u64)-1) {
  902. ret = find_and_setup_root(tree_root, fs_info,
  903. location->objectid, root);
  904. if (ret) {
  905. kfree(root);
  906. return ERR_PTR(ret);
  907. }
  908. goto insert;
  909. }
  910. __setup_root(tree_root->nodesize, tree_root->leafsize,
  911. tree_root->sectorsize, tree_root->stripesize,
  912. root, fs_info, location->objectid);
  913. path = btrfs_alloc_path();
  914. BUG_ON(!path);
  915. ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
  916. if (ret != 0) {
  917. if (ret > 0)
  918. ret = -ENOENT;
  919. goto out;
  920. }
  921. l = path->nodes[0];
  922. read_extent_buffer(l, &root->root_item,
  923. btrfs_item_ptr_offset(l, path->slots[0]),
  924. sizeof(root->root_item));
  925. memcpy(&root->root_key, location, sizeof(*location));
  926. ret = 0;
  927. out:
  928. btrfs_release_path(root, path);
  929. btrfs_free_path(path);
  930. if (ret) {
  931. kfree(root);
  932. return ERR_PTR(ret);
  933. }
  934. generation = btrfs_root_generation(&root->root_item);
  935. blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
  936. root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
  937. blocksize, generation);
  938. BUG_ON(!root->node);
  939. insert:
  940. if (location->objectid != BTRFS_TREE_LOG_OBJECTID) {
  941. root->ref_cows = 1;
  942. ret = btrfs_find_highest_inode(root, &highest_inode);
  943. if (ret == 0) {
  944. root->highest_inode = highest_inode;
  945. root->last_inode_alloc = highest_inode;
  946. }
  947. }
  948. return root;
  949. }
  950. struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info,
  951. u64 root_objectid)
  952. {
  953. struct btrfs_root *root;
  954. if (root_objectid == BTRFS_ROOT_TREE_OBJECTID)
  955. return fs_info->tree_root;
  956. if (root_objectid == BTRFS_EXTENT_TREE_OBJECTID)
  957. return fs_info->extent_root;
  958. root = radix_tree_lookup(&fs_info->fs_roots_radix,
  959. (unsigned long)root_objectid);
  960. return root;
  961. }
  962. struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info,
  963. struct btrfs_key *location)
  964. {
  965. struct btrfs_root *root;
  966. int ret;
  967. if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
  968. return fs_info->tree_root;
  969. if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
  970. return fs_info->extent_root;
  971. if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
  972. return fs_info->chunk_root;
  973. if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
  974. return fs_info->dev_root;
  975. if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
  976. return fs_info->csum_root;
  977. root = radix_tree_lookup(&fs_info->fs_roots_radix,
  978. (unsigned long)location->objectid);
  979. if (root)
  980. return root;
  981. root = btrfs_read_fs_root_no_radix(fs_info->tree_root, location);
  982. if (IS_ERR(root))
  983. return root;
  984. set_anon_super(&root->anon_super, NULL);
  985. ret = radix_tree_insert(&fs_info->fs_roots_radix,
  986. (unsigned long)root->root_key.objectid,
  987. root);
  988. if (ret) {
  989. free_extent_buffer(root->node);
  990. kfree(root);
  991. return ERR_PTR(ret);
  992. }
  993. if (!(fs_info->sb->s_flags & MS_RDONLY)) {
  994. ret = btrfs_find_dead_roots(fs_info->tree_root,
  995. root->root_key.objectid, root);
  996. BUG_ON(ret);
  997. btrfs_orphan_cleanup(root);
  998. }
  999. return root;
  1000. }
  1001. struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
  1002. struct btrfs_key *location,
  1003. const char *name, int namelen)
  1004. {
  1005. struct btrfs_root *root;
  1006. int ret;
  1007. root = btrfs_read_fs_root_no_name(fs_info, location);
  1008. if (!root)
  1009. return NULL;
  1010. if (root->in_sysfs)
  1011. return root;
  1012. ret = btrfs_set_root_name(root, name, namelen);
  1013. if (ret) {
  1014. free_extent_buffer(root->node);
  1015. kfree(root);
  1016. return ERR_PTR(ret);
  1017. }
  1018. #if 0
  1019. ret = btrfs_sysfs_add_root(root);
  1020. if (ret) {
  1021. free_extent_buffer(root->node);
  1022. kfree(root->name);
  1023. kfree(root);
  1024. return ERR_PTR(ret);
  1025. }
  1026. #endif
  1027. root->in_sysfs = 1;
  1028. return root;
  1029. }
  1030. #if 0
  1031. static int add_hasher(struct btrfs_fs_info *info, char *type) {
  1032. struct btrfs_hasher *hasher;
  1033. hasher = kmalloc(sizeof(*hasher), GFP_NOFS);
  1034. if (!hasher)
  1035. return -ENOMEM;
  1036. hasher->hash_tfm = crypto_alloc_hash(type, 0, CRYPTO_ALG_ASYNC);
  1037. if (!hasher->hash_tfm) {
  1038. kfree(hasher);
  1039. return -EINVAL;
  1040. }
  1041. spin_lock(&info->hash_lock);
  1042. list_add(&hasher->list, &info->hashers);
  1043. spin_unlock(&info->hash_lock);
  1044. return 0;
  1045. }
  1046. #endif
  1047. static int btrfs_congested_fn(void *congested_data, int bdi_bits)
  1048. {
  1049. struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data;
  1050. int ret = 0;
  1051. struct list_head *cur;
  1052. struct btrfs_device *device;
  1053. struct backing_dev_info *bdi;
  1054. #if 0
  1055. if ((bdi_bits & (1 << BDI_write_congested)) &&
  1056. btrfs_congested_async(info, 0))
  1057. return 1;
  1058. #endif
  1059. list_for_each(cur, &info->fs_devices->devices) {
  1060. device = list_entry(cur, struct btrfs_device, dev_list);
  1061. if (!device->bdev)
  1062. continue;
  1063. bdi = blk_get_backing_dev_info(device->bdev);
  1064. if (bdi && bdi_congested(bdi, bdi_bits)) {
  1065. ret = 1;
  1066. break;
  1067. }
  1068. }
  1069. return ret;
  1070. }
  1071. /*
  1072. * this unplugs every device on the box, and it is only used when page
  1073. * is null
  1074. */
  1075. static void __unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
  1076. {
  1077. struct list_head *cur;
  1078. struct btrfs_device *device;
  1079. struct btrfs_fs_info *info;
  1080. info = (struct btrfs_fs_info *)bdi->unplug_io_data;
  1081. list_for_each(cur, &info->fs_devices->devices) {
  1082. device = list_entry(cur, struct btrfs_device, dev_list);
  1083. if (!device->bdev)
  1084. continue;
  1085. bdi = blk_get_backing_dev_info(device->bdev);
  1086. if (bdi->unplug_io_fn) {
  1087. bdi->unplug_io_fn(bdi, page);
  1088. }
  1089. }
  1090. }
  1091. static void btrfs_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
  1092. {
  1093. struct inode *inode;
  1094. struct extent_map_tree *em_tree;
  1095. struct extent_map *em;
  1096. struct address_space *mapping;
  1097. u64 offset;
  1098. /* the generic O_DIRECT read code does this */
  1099. if (1 || !page) {
  1100. __unplug_io_fn(bdi, page);
  1101. return;
  1102. }
  1103. /*
  1104. * page->mapping may change at any time. Get a consistent copy
  1105. * and use that for everything below
  1106. */
  1107. smp_mb();
  1108. mapping = page->mapping;
  1109. if (!mapping)
  1110. return;
  1111. inode = mapping->host;
  1112. /*
  1113. * don't do the expensive searching for a small number of
  1114. * devices
  1115. */
  1116. if (BTRFS_I(inode)->root->fs_info->fs_devices->open_devices <= 2) {
  1117. __unplug_io_fn(bdi, page);
  1118. return;
  1119. }
  1120. offset = page_offset(page);
  1121. em_tree = &BTRFS_I(inode)->extent_tree;
  1122. spin_lock(&em_tree->lock);
  1123. em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
  1124. spin_unlock(&em_tree->lock);
  1125. if (!em) {
  1126. __unplug_io_fn(bdi, page);
  1127. return;
  1128. }
  1129. if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
  1130. free_extent_map(em);
  1131. __unplug_io_fn(bdi, page);
  1132. return;
  1133. }
  1134. offset = offset - em->start;
  1135. btrfs_unplug_page(&BTRFS_I(inode)->root->fs_info->mapping_tree,
  1136. em->block_start + offset, page);
  1137. free_extent_map(em);
  1138. }
  1139. static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi)
  1140. {
  1141. bdi_init(bdi);
  1142. bdi->ra_pages = default_backing_dev_info.ra_pages;
  1143. bdi->state = 0;
  1144. bdi->capabilities = default_backing_dev_info.capabilities;
  1145. bdi->unplug_io_fn = btrfs_unplug_io_fn;
  1146. bdi->unplug_io_data = info;
  1147. bdi->congested_fn = btrfs_congested_fn;
  1148. bdi->congested_data = info;
  1149. return 0;
  1150. }
  1151. static int bio_ready_for_csum(struct bio *bio)
  1152. {
  1153. u64 length = 0;
  1154. u64 buf_len = 0;
  1155. u64 start = 0;
  1156. struct page *page;
  1157. struct extent_io_tree *io_tree = NULL;
  1158. struct btrfs_fs_info *info = NULL;
  1159. struct bio_vec *bvec;
  1160. int i;
  1161. int ret;
  1162. bio_for_each_segment(bvec, bio, i) {
  1163. page = bvec->bv_page;
  1164. if (page->private == EXTENT_PAGE_PRIVATE) {
  1165. length += bvec->bv_len;
  1166. continue;
  1167. }
  1168. if (!page->private) {
  1169. length += bvec->bv_len;
  1170. continue;
  1171. }
  1172. length = bvec->bv_len;
  1173. buf_len = page->private >> 2;
  1174. start = page_offset(page) + bvec->bv_offset;
  1175. io_tree = &BTRFS_I(page->mapping->host)->io_tree;
  1176. info = BTRFS_I(page->mapping->host)->root->fs_info;
  1177. }
  1178. /* are we fully contained in this bio? */
  1179. if (buf_len <= length)
  1180. return 1;
  1181. ret = extent_range_uptodate(io_tree, start + length,
  1182. start + buf_len - 1);
  1183. if (ret == 1)
  1184. return ret;
  1185. return ret;
  1186. }
  1187. /*
  1188. * called by the kthread helper functions to finally call the bio end_io
  1189. * functions. This is where read checksum verification actually happens
  1190. */
  1191. static void end_workqueue_fn(struct btrfs_work *work)
  1192. {
  1193. struct bio *bio;
  1194. struct end_io_wq *end_io_wq;
  1195. struct btrfs_fs_info *fs_info;
  1196. int error;
  1197. end_io_wq = container_of(work, struct end_io_wq, work);
  1198. bio = end_io_wq->bio;
  1199. fs_info = end_io_wq->info;
  1200. /* metadata bio reads are special because the whole tree block must
  1201. * be checksummed at once. This makes sure the entire block is in
  1202. * ram and up to date before trying to verify things. For
  1203. * blocksize <= pagesize, it is basically a noop
  1204. */
  1205. if (!(bio->bi_rw & (1 << BIO_RW)) && end_io_wq->metadata &&
  1206. !bio_ready_for_csum(bio)) {
  1207. btrfs_queue_worker(&fs_info->endio_meta_workers,
  1208. &end_io_wq->work);
  1209. return;
  1210. }
  1211. error = end_io_wq->error;
  1212. bio->bi_private = end_io_wq->private;
  1213. bio->bi_end_io = end_io_wq->end_io;
  1214. kfree(end_io_wq);
  1215. bio_endio(bio, error);
  1216. }
  1217. static int cleaner_kthread(void *arg)
  1218. {
  1219. struct btrfs_root *root = arg;
  1220. do {
  1221. smp_mb();
  1222. if (root->fs_info->closing)
  1223. break;
  1224. vfs_check_frozen(root->fs_info->sb, SB_FREEZE_WRITE);
  1225. mutex_lock(&root->fs_info->cleaner_mutex);
  1226. btrfs_clean_old_snapshots(root);
  1227. mutex_unlock(&root->fs_info->cleaner_mutex);
  1228. if (freezing(current)) {
  1229. refrigerator();
  1230. } else {
  1231. smp_mb();
  1232. if (root->fs_info->closing)
  1233. break;
  1234. set_current_state(TASK_INTERRUPTIBLE);
  1235. schedule();
  1236. __set_current_state(TASK_RUNNING);
  1237. }
  1238. } while (!kthread_should_stop());
  1239. return 0;
  1240. }
  1241. static int transaction_kthread(void *arg)
  1242. {
  1243. struct btrfs_root *root = arg;
  1244. struct btrfs_trans_handle *trans;
  1245. struct btrfs_transaction *cur;
  1246. unsigned long now;
  1247. unsigned long delay;
  1248. int ret;
  1249. do {
  1250. smp_mb();
  1251. if (root->fs_info->closing)
  1252. break;
  1253. delay = HZ * 30;
  1254. vfs_check_frozen(root->fs_info->sb, SB_FREEZE_WRITE);
  1255. mutex_lock(&root->fs_info->transaction_kthread_mutex);
  1256. if (root->fs_info->total_ref_cache_size > 20 * 1024 * 1024) {
  1257. printk("btrfs: total reference cache size %Lu\n",
  1258. root->fs_info->total_ref_cache_size);
  1259. }
  1260. mutex_lock(&root->fs_info->trans_mutex);
  1261. cur = root->fs_info->running_transaction;
  1262. if (!cur) {
  1263. mutex_unlock(&root->fs_info->trans_mutex);
  1264. goto sleep;
  1265. }
  1266. now = get_seconds();
  1267. if (now < cur->start_time || now - cur->start_time < 30) {
  1268. mutex_unlock(&root->fs_info->trans_mutex);
  1269. delay = HZ * 5;
  1270. goto sleep;
  1271. }
  1272. mutex_unlock(&root->fs_info->trans_mutex);
  1273. trans = btrfs_start_transaction(root, 1);
  1274. ret = btrfs_commit_transaction(trans, root);
  1275. sleep:
  1276. wake_up_process(root->fs_info->cleaner_kthread);
  1277. mutex_unlock(&root->fs_info->transaction_kthread_mutex);
  1278. if (freezing(current)) {
  1279. refrigerator();
  1280. } else {
  1281. if (root->fs_info->closing)
  1282. break;
  1283. set_current_state(TASK_INTERRUPTIBLE);
  1284. schedule_timeout(delay);
  1285. __set_current_state(TASK_RUNNING);
  1286. }
  1287. } while (!kthread_should_stop());
  1288. return 0;
  1289. }
  1290. struct btrfs_root *open_ctree(struct super_block *sb,
  1291. struct btrfs_fs_devices *fs_devices,
  1292. char *options)
  1293. {
  1294. u32 sectorsize;
  1295. u32 nodesize;
  1296. u32 leafsize;
  1297. u32 blocksize;
  1298. u32 stripesize;
  1299. u64 generation;
  1300. u64 features;
  1301. struct btrfs_key location;
  1302. struct buffer_head *bh;
  1303. struct btrfs_root *extent_root = kzalloc(sizeof(struct btrfs_root),
  1304. GFP_NOFS);
  1305. struct btrfs_root *csum_root = kzalloc(sizeof(struct btrfs_root),
  1306. GFP_NOFS);
  1307. struct btrfs_root *tree_root = kzalloc(sizeof(struct btrfs_root),
  1308. GFP_NOFS);
  1309. struct btrfs_fs_info *fs_info = kzalloc(sizeof(*fs_info),
  1310. GFP_NOFS);
  1311. struct btrfs_root *chunk_root = kzalloc(sizeof(struct btrfs_root),
  1312. GFP_NOFS);
  1313. struct btrfs_root *dev_root = kzalloc(sizeof(struct btrfs_root),
  1314. GFP_NOFS);
  1315. struct btrfs_root *log_tree_root;
  1316. int ret;
  1317. int err = -EINVAL;
  1318. struct btrfs_super_block *disk_super;
  1319. if (!extent_root || !tree_root || !fs_info ||
  1320. !chunk_root || !dev_root || !csum_root) {
  1321. err = -ENOMEM;
  1322. goto fail;
  1323. }
  1324. INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
  1325. INIT_LIST_HEAD(&fs_info->trans_list);
  1326. INIT_LIST_HEAD(&fs_info->dead_roots);
  1327. INIT_LIST_HEAD(&fs_info->hashers);
  1328. INIT_LIST_HEAD(&fs_info->delalloc_inodes);
  1329. spin_lock_init(&fs_info->hash_lock);
  1330. spin_lock_init(&fs_info->delalloc_lock);
  1331. spin_lock_init(&fs_info->new_trans_lock);
  1332. spin_lock_init(&fs_info->ref_cache_lock);
  1333. init_completion(&fs_info->kobj_unregister);
  1334. fs_info->tree_root = tree_root;
  1335. fs_info->extent_root = extent_root;
  1336. fs_info->csum_root = csum_root;
  1337. fs_info->chunk_root = chunk_root;
  1338. fs_info->dev_root = dev_root;
  1339. fs_info->fs_devices = fs_devices;
  1340. INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
  1341. INIT_LIST_HEAD(&fs_info->space_info);
  1342. btrfs_mapping_init(&fs_info->mapping_tree);
  1343. atomic_set(&fs_info->nr_async_submits, 0);
  1344. atomic_set(&fs_info->async_delalloc_pages, 0);
  1345. atomic_set(&fs_info->async_submit_draining, 0);
  1346. atomic_set(&fs_info->nr_async_bios, 0);
  1347. atomic_set(&fs_info->throttles, 0);
  1348. atomic_set(&fs_info->throttle_gen, 0);
  1349. fs_info->sb = sb;
  1350. fs_info->max_extent = (u64)-1;
  1351. fs_info->max_inline = 8192 * 1024;
  1352. setup_bdi(fs_info, &fs_info->bdi);
  1353. fs_info->btree_inode = new_inode(sb);
  1354. fs_info->btree_inode->i_ino = 1;
  1355. fs_info->btree_inode->i_nlink = 1;
  1356. fs_info->thread_pool_size = min(num_online_cpus() + 2, 8);
  1357. INIT_LIST_HEAD(&fs_info->ordered_extents);
  1358. spin_lock_init(&fs_info->ordered_extent_lock);
  1359. sb->s_blocksize = 4096;
  1360. sb->s_blocksize_bits = blksize_bits(4096);
  1361. /*
  1362. * we set the i_size on the btree inode to the max possible int.
  1363. * the real end of the address space is determined by all of
  1364. * the devices in the system
  1365. */
  1366. fs_info->btree_inode->i_size = OFFSET_MAX;
  1367. fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
  1368. fs_info->btree_inode->i_mapping->backing_dev_info = &fs_info->bdi;
  1369. extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree,
  1370. fs_info->btree_inode->i_mapping,
  1371. GFP_NOFS);
  1372. extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree,
  1373. GFP_NOFS);
  1374. BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops;
  1375. spin_lock_init(&fs_info->block_group_cache_lock);
  1376. fs_info->block_group_cache_tree.rb_node = NULL;
  1377. extent_io_tree_init(&fs_info->pinned_extents,
  1378. fs_info->btree_inode->i_mapping, GFP_NOFS);
  1379. extent_io_tree_init(&fs_info->pending_del,
  1380. fs_info->btree_inode->i_mapping, GFP_NOFS);
  1381. extent_io_tree_init(&fs_info->extent_ins,
  1382. fs_info->btree_inode->i_mapping, GFP_NOFS);
  1383. fs_info->do_barriers = 1;
  1384. INIT_LIST_HEAD(&fs_info->dead_reloc_roots);
  1385. btrfs_leaf_ref_tree_init(&fs_info->reloc_ref_tree);
  1386. btrfs_leaf_ref_tree_init(&fs_info->shared_ref_tree);
  1387. BTRFS_I(fs_info->btree_inode)->root = tree_root;
  1388. memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
  1389. sizeof(struct btrfs_key));
  1390. insert_inode_hash(fs_info->btree_inode);
  1391. mutex_init(&fs_info->trans_mutex);
  1392. mutex_init(&fs_info->tree_log_mutex);
  1393. mutex_init(&fs_info->drop_mutex);
  1394. mutex_init(&fs_info->extent_ins_mutex);
  1395. mutex_init(&fs_info->pinned_mutex);
  1396. mutex_init(&fs_info->chunk_mutex);
  1397. mutex_init(&fs_info->transaction_kthread_mutex);
  1398. mutex_init(&fs_info->cleaner_mutex);
  1399. mutex_init(&fs_info->volume_mutex);
  1400. mutex_init(&fs_info->tree_reloc_mutex);
  1401. init_waitqueue_head(&fs_info->transaction_throttle);
  1402. init_waitqueue_head(&fs_info->transaction_wait);
  1403. init_waitqueue_head(&fs_info->async_submit_wait);
  1404. init_waitqueue_head(&fs_info->tree_log_wait);
  1405. atomic_set(&fs_info->tree_log_commit, 0);
  1406. atomic_set(&fs_info->tree_log_writers, 0);
  1407. fs_info->tree_log_transid = 0;
  1408. #if 0
  1409. ret = add_hasher(fs_info, "crc32c");
  1410. if (ret) {
  1411. printk("btrfs: failed hash setup, modprobe cryptomgr?\n");
  1412. err = -ENOMEM;
  1413. goto fail_iput;
  1414. }
  1415. #endif
  1416. __setup_root(4096, 4096, 4096, 4096, tree_root,
  1417. fs_info, BTRFS_ROOT_TREE_OBJECTID);
  1418. bh = btrfs_read_dev_super(fs_devices->latest_bdev);
  1419. if (!bh)
  1420. goto fail_iput;
  1421. memcpy(&fs_info->super_copy, bh->b_data, sizeof(fs_info->super_copy));
  1422. brelse(bh);
  1423. memcpy(fs_info->fsid, fs_info->super_copy.fsid, BTRFS_FSID_SIZE);
  1424. disk_super = &fs_info->super_copy;
  1425. if (!btrfs_super_root(disk_super))
  1426. goto fail_iput;
  1427. ret = btrfs_parse_options(tree_root, options);
  1428. if (ret) {
  1429. err = ret;
  1430. goto fail_iput;
  1431. }
  1432. features = btrfs_super_incompat_flags(disk_super) &
  1433. ~BTRFS_FEATURE_INCOMPAT_SUPP;
  1434. if (features) {
  1435. printk(KERN_ERR "BTRFS: couldn't mount because of "
  1436. "unsupported optional features (%Lx).\n",
  1437. features);
  1438. err = -EINVAL;
  1439. goto fail_iput;
  1440. }
  1441. features = btrfs_super_compat_ro_flags(disk_super) &
  1442. ~BTRFS_FEATURE_COMPAT_RO_SUPP;
  1443. if (!(sb->s_flags & MS_RDONLY) && features) {
  1444. printk(KERN_ERR "BTRFS: couldn't mount RDWR because of "
  1445. "unsupported option features (%Lx).\n",
  1446. features);
  1447. err = -EINVAL;
  1448. goto fail_iput;
  1449. }
  1450. /*
  1451. * we need to start all the end_io workers up front because the
  1452. * queue work function gets called at interrupt time, and so it
  1453. * cannot dynamically grow.
  1454. */
  1455. btrfs_init_workers(&fs_info->workers, "worker",
  1456. fs_info->thread_pool_size);
  1457. btrfs_init_workers(&fs_info->delalloc_workers, "delalloc",
  1458. fs_info->thread_pool_size);
  1459. btrfs_init_workers(&fs_info->submit_workers, "submit",
  1460. min_t(u64, fs_devices->num_devices,
  1461. fs_info->thread_pool_size));
  1462. /* a higher idle thresh on the submit workers makes it much more
  1463. * likely that bios will be send down in a sane order to the
  1464. * devices
  1465. */
  1466. fs_info->submit_workers.idle_thresh = 64;
  1467. fs_info->workers.idle_thresh = 16;
  1468. fs_info->workers.ordered = 1;
  1469. fs_info->delalloc_workers.idle_thresh = 2;
  1470. fs_info->delalloc_workers.ordered = 1;
  1471. btrfs_init_workers(&fs_info->fixup_workers, "fixup", 1);
  1472. btrfs_init_workers(&fs_info->endio_workers, "endio",
  1473. fs_info->thread_pool_size);
  1474. btrfs_init_workers(&fs_info->endio_meta_workers, "endio-meta",
  1475. fs_info->thread_pool_size);
  1476. btrfs_init_workers(&fs_info->endio_meta_write_workers,
  1477. "endio-meta-write", fs_info->thread_pool_size);
  1478. btrfs_init_workers(&fs_info->endio_write_workers, "endio-write",
  1479. fs_info->thread_pool_size);
  1480. /*
  1481. * endios are largely parallel and should have a very
  1482. * low idle thresh
  1483. */
  1484. fs_info->endio_workers.idle_thresh = 4;
  1485. fs_info->endio_write_workers.idle_thresh = 64;
  1486. fs_info->endio_meta_write_workers.idle_thresh = 64;
  1487. btrfs_start_workers(&fs_info->workers, 1);
  1488. btrfs_start_workers(&fs_info->submit_workers, 1);
  1489. btrfs_start_workers(&fs_info->delalloc_workers, 1);
  1490. btrfs_start_workers(&fs_info->fixup_workers, 1);
  1491. btrfs_start_workers(&fs_info->endio_workers, fs_info->thread_pool_size);
  1492. btrfs_start_workers(&fs_info->endio_meta_workers,
  1493. fs_info->thread_pool_size);
  1494. btrfs_start_workers(&fs_info->endio_meta_write_workers,
  1495. fs_info->thread_pool_size);
  1496. btrfs_start_workers(&fs_info->endio_write_workers,
  1497. fs_info->thread_pool_size);
  1498. fs_info->bdi.ra_pages *= btrfs_super_num_devices(disk_super);
  1499. fs_info->bdi.ra_pages = max(fs_info->bdi.ra_pages,
  1500. 4 * 1024 * 1024 / PAGE_CACHE_SIZE);
  1501. nodesize = btrfs_super_nodesize(disk_super);
  1502. leafsize = btrfs_super_leafsize(disk_super);
  1503. sectorsize = btrfs_super_sectorsize(disk_super);
  1504. stripesize = btrfs_super_stripesize(disk_super);
  1505. tree_root->nodesize = nodesize;
  1506. tree_root->leafsize = leafsize;
  1507. tree_root->sectorsize = sectorsize;
  1508. tree_root->stripesize = stripesize;
  1509. sb->s_blocksize = sectorsize;
  1510. sb->s_blocksize_bits = blksize_bits(sectorsize);
  1511. if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
  1512. sizeof(disk_super->magic))) {
  1513. printk("btrfs: valid FS not found on %s\n", sb->s_id);
  1514. goto fail_sb_buffer;
  1515. }
  1516. mutex_lock(&fs_info->chunk_mutex);
  1517. ret = btrfs_read_sys_array(tree_root);
  1518. mutex_unlock(&fs_info->chunk_mutex);
  1519. if (ret) {
  1520. printk("btrfs: failed to read the system array on %s\n",
  1521. sb->s_id);
  1522. goto fail_sys_array;
  1523. }
  1524. blocksize = btrfs_level_size(tree_root,
  1525. btrfs_super_chunk_root_level(disk_super));
  1526. generation = btrfs_super_chunk_root_generation(disk_super);
  1527. __setup_root(nodesize, leafsize, sectorsize, stripesize,
  1528. chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
  1529. chunk_root->node = read_tree_block(chunk_root,
  1530. btrfs_super_chunk_root(disk_super),
  1531. blocksize, generation);
  1532. BUG_ON(!chunk_root->node);
  1533. read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid,
  1534. (unsigned long)btrfs_header_chunk_tree_uuid(chunk_root->node),
  1535. BTRFS_UUID_SIZE);
  1536. mutex_lock(&fs_info->chunk_mutex);
  1537. ret = btrfs_read_chunk_tree(chunk_root);
  1538. mutex_unlock(&fs_info->chunk_mutex);
  1539. if (ret) {
  1540. printk("btrfs: failed to read chunk tree on %s\n", sb->s_id);
  1541. goto fail_chunk_root;
  1542. }
  1543. btrfs_close_extra_devices(fs_devices);
  1544. blocksize = btrfs_level_size(tree_root,
  1545. btrfs_super_root_level(disk_super));
  1546. generation = btrfs_super_generation(disk_super);
  1547. tree_root->node = read_tree_block(tree_root,
  1548. btrfs_super_root(disk_super),
  1549. blocksize, generation);
  1550. if (!tree_root->node)
  1551. goto fail_chunk_root;
  1552. ret = find_and_setup_root(tree_root, fs_info,
  1553. BTRFS_EXTENT_TREE_OBJECTID, extent_root);
  1554. if (ret)
  1555. goto fail_tree_root;
  1556. extent_root->track_dirty = 1;
  1557. ret = find_and_setup_root(tree_root, fs_info,
  1558. BTRFS_DEV_TREE_OBJECTID, dev_root);
  1559. dev_root->track_dirty = 1;
  1560. if (ret)
  1561. goto fail_extent_root;
  1562. ret = find_and_setup_root(tree_root, fs_info,
  1563. BTRFS_CSUM_TREE_OBJECTID, csum_root);
  1564. if (ret)
  1565. goto fail_extent_root;
  1566. csum_root->track_dirty = 1;
  1567. btrfs_read_block_groups(extent_root);
  1568. fs_info->generation = generation + 1;
  1569. fs_info->last_trans_committed = generation;
  1570. fs_info->data_alloc_profile = (u64)-1;
  1571. fs_info->metadata_alloc_profile = (u64)-1;
  1572. fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
  1573. fs_info->cleaner_kthread = kthread_run(cleaner_kthread, tree_root,
  1574. "btrfs-cleaner");
  1575. if (!fs_info->cleaner_kthread)
  1576. goto fail_csum_root;
  1577. fs_info->transaction_kthread = kthread_run(transaction_kthread,
  1578. tree_root,
  1579. "btrfs-transaction");
  1580. if (!fs_info->transaction_kthread)
  1581. goto fail_cleaner;
  1582. if (btrfs_super_log_root(disk_super) != 0) {
  1583. u64 bytenr = btrfs_super_log_root(disk_super);
  1584. if (fs_devices->rw_devices == 0) {
  1585. printk("Btrfs log replay required on RO media\n");
  1586. err = -EIO;
  1587. goto fail_trans_kthread;
  1588. }
  1589. blocksize =
  1590. btrfs_level_size(tree_root,
  1591. btrfs_super_log_root_level(disk_super));
  1592. log_tree_root = kzalloc(sizeof(struct btrfs_root),
  1593. GFP_NOFS);
  1594. __setup_root(nodesize, leafsize, sectorsize, stripesize,
  1595. log_tree_root, fs_info, BTRFS_TREE_LOG_OBJECTID);
  1596. log_tree_root->node = read_tree_block(tree_root, bytenr,
  1597. blocksize,
  1598. generation + 1);
  1599. ret = btrfs_recover_log_trees(log_tree_root);
  1600. BUG_ON(ret);
  1601. if (sb->s_flags & MS_RDONLY) {
  1602. ret = btrfs_commit_super(tree_root);
  1603. BUG_ON(ret);
  1604. }
  1605. }
  1606. if (!(sb->s_flags & MS_RDONLY)) {
  1607. ret = btrfs_cleanup_reloc_trees(tree_root);
  1608. BUG_ON(ret);
  1609. }
  1610. location.objectid = BTRFS_FS_TREE_OBJECTID;
  1611. location.type = BTRFS_ROOT_ITEM_KEY;
  1612. location.offset = (u64)-1;
  1613. fs_info->fs_root = btrfs_read_fs_root_no_name(fs_info, &location);
  1614. if (!fs_info->fs_root)
  1615. goto fail_trans_kthread;
  1616. return tree_root;
  1617. fail_trans_kthread:
  1618. kthread_stop(fs_info->transaction_kthread);
  1619. fail_cleaner:
  1620. kthread_stop(fs_info->cleaner_kthread);
  1621. /*
  1622. * make sure we're done with the btree inode before we stop our
  1623. * kthreads
  1624. */
  1625. filemap_write_and_wait(fs_info->btree_inode->i_mapping);
  1626. invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
  1627. fail_csum_root:
  1628. free_extent_buffer(csum_root->node);
  1629. fail_extent_root:
  1630. free_extent_buffer(extent_root->node);
  1631. fail_tree_root:
  1632. free_extent_buffer(tree_root->node);
  1633. fail_chunk_root:
  1634. free_extent_buffer(chunk_root->node);
  1635. fail_sys_array:
  1636. free_extent_buffer(dev_root->node);
  1637. fail_sb_buffer:
  1638. btrfs_stop_workers(&fs_info->fixup_workers);
  1639. btrfs_stop_workers(&fs_info->delalloc_workers);
  1640. btrfs_stop_workers(&fs_info->workers);
  1641. btrfs_stop_workers(&fs_info->endio_workers);
  1642. btrfs_stop_workers(&fs_info->endio_meta_workers);
  1643. btrfs_stop_workers(&fs_info->endio_meta_write_workers);
  1644. btrfs_stop_workers(&fs_info->endio_write_workers);
  1645. btrfs_stop_workers(&fs_info->submit_workers);
  1646. fail_iput:
  1647. invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
  1648. iput(fs_info->btree_inode);
  1649. fail:
  1650. btrfs_close_devices(fs_info->fs_devices);
  1651. btrfs_mapping_tree_free(&fs_info->mapping_tree);
  1652. kfree(extent_root);
  1653. kfree(tree_root);
  1654. bdi_destroy(&fs_info->bdi);
  1655. kfree(fs_info);
  1656. kfree(chunk_root);
  1657. kfree(dev_root);
  1658. kfree(csum_root);
  1659. return ERR_PTR(err);
  1660. }
  1661. static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate)
  1662. {
  1663. char b[BDEVNAME_SIZE];
  1664. if (uptodate) {
  1665. set_buffer_uptodate(bh);
  1666. } else {
  1667. if (!buffer_eopnotsupp(bh) && printk_ratelimit()) {
  1668. printk(KERN_WARNING "lost page write due to "
  1669. "I/O error on %s\n",
  1670. bdevname(bh->b_bdev, b));
  1671. }
  1672. /* note, we dont' set_buffer_write_io_error because we have
  1673. * our own ways of dealing with the IO errors
  1674. */
  1675. clear_buffer_uptodate(bh);
  1676. }
  1677. unlock_buffer(bh);
  1678. put_bh(bh);
  1679. }
  1680. struct buffer_head *btrfs_read_dev_super(struct block_device *bdev)
  1681. {
  1682. struct buffer_head *bh;
  1683. struct buffer_head *latest = NULL;
  1684. struct btrfs_super_block *super;
  1685. int i;
  1686. u64 transid = 0;
  1687. u64 bytenr;
  1688. /* we would like to check all the supers, but that would make
  1689. * a btrfs mount succeed after a mkfs from a different FS.
  1690. * So, we need to add a special mount option to scan for
  1691. * later supers, using BTRFS_SUPER_MIRROR_MAX instead
  1692. */
  1693. for (i = 0; i < 1; i++) {
  1694. bytenr = btrfs_sb_offset(i);
  1695. if (bytenr + 4096 >= i_size_read(bdev->bd_inode))
  1696. break;
  1697. bh = __bread(bdev, bytenr / 4096, 4096);
  1698. if (!bh)
  1699. continue;
  1700. super = (struct btrfs_super_block *)bh->b_data;
  1701. if (btrfs_super_bytenr(super) != bytenr ||
  1702. strncmp((char *)(&super->magic), BTRFS_MAGIC,
  1703. sizeof(super->magic))) {
  1704. brelse(bh);
  1705. continue;
  1706. }
  1707. if (!latest || btrfs_super_generation(super) > transid) {
  1708. brelse(latest);
  1709. latest = bh;
  1710. transid = btrfs_super_generation(super);
  1711. } else {
  1712. brelse(bh);
  1713. }
  1714. }
  1715. return latest;
  1716. }
  1717. static int write_dev_supers(struct btrfs_device *device,
  1718. struct btrfs_super_block *sb,
  1719. int do_barriers, int wait, int max_mirrors)
  1720. {
  1721. struct buffer_head *bh;
  1722. int i;
  1723. int ret;
  1724. int errors = 0;
  1725. u32 crc;
  1726. u64 bytenr;
  1727. int last_barrier = 0;
  1728. if (max_mirrors == 0)
  1729. max_mirrors = BTRFS_SUPER_MIRROR_MAX;
  1730. /* make sure only the last submit_bh does a barrier */
  1731. if (do_barriers) {
  1732. for (i = 0; i < max_mirrors; i++) {
  1733. bytenr = btrfs_sb_offset(i);
  1734. if (bytenr + BTRFS_SUPER_INFO_SIZE >=
  1735. device->total_bytes)
  1736. break;
  1737. last_barrier = i;
  1738. }
  1739. }
  1740. for (i = 0; i < max_mirrors; i++) {
  1741. bytenr = btrfs_sb_offset(i);
  1742. if (bytenr + BTRFS_SUPER_INFO_SIZE >= device->total_bytes)
  1743. break;
  1744. if (wait) {
  1745. bh = __find_get_block(device->bdev, bytenr / 4096,
  1746. BTRFS_SUPER_INFO_SIZE);
  1747. BUG_ON(!bh);
  1748. brelse(bh);
  1749. wait_on_buffer(bh);
  1750. if (buffer_uptodate(bh)) {
  1751. brelse(bh);
  1752. continue;
  1753. }
  1754. } else {
  1755. btrfs_set_super_bytenr(sb, bytenr);
  1756. crc = ~(u32)0;
  1757. crc = btrfs_csum_data(NULL, (char *)sb +
  1758. BTRFS_CSUM_SIZE, crc,
  1759. BTRFS_SUPER_INFO_SIZE -
  1760. BTRFS_CSUM_SIZE);
  1761. btrfs_csum_final(crc, sb->csum);
  1762. bh = __getblk(device->bdev, bytenr / 4096,
  1763. BTRFS_SUPER_INFO_SIZE);
  1764. memcpy(bh->b_data, sb, BTRFS_SUPER_INFO_SIZE);
  1765. set_buffer_uptodate(bh);
  1766. get_bh(bh);
  1767. lock_buffer(bh);
  1768. bh->b_end_io = btrfs_end_buffer_write_sync;
  1769. }
  1770. if (i == last_barrier && do_barriers && device->barriers) {
  1771. ret = submit_bh(WRITE_BARRIER, bh);
  1772. if (ret == -EOPNOTSUPP) {
  1773. printk("btrfs: disabling barriers on dev %s\n",
  1774. device->name);
  1775. set_buffer_uptodate(bh);
  1776. device->barriers = 0;
  1777. get_bh(bh);
  1778. lock_buffer(bh);
  1779. ret = submit_bh(WRITE, bh);
  1780. }
  1781. } else {
  1782. ret = submit_bh(WRITE, bh);
  1783. }
  1784. if (!ret && wait) {
  1785. wait_on_buffer(bh);
  1786. if (!buffer_uptodate(bh))
  1787. errors++;
  1788. } else if (ret) {
  1789. errors++;
  1790. }
  1791. if (wait)
  1792. brelse(bh);
  1793. }
  1794. return errors < i ? 0 : -1;
  1795. }
  1796. int write_all_supers(struct btrfs_root *root, int max_mirrors)
  1797. {
  1798. struct list_head *cur;
  1799. struct list_head *head = &root->fs_info->fs_devices->devices;
  1800. struct btrfs_device *dev;
  1801. struct btrfs_super_block *sb;
  1802. struct btrfs_dev_item *dev_item;
  1803. int ret;
  1804. int do_barriers;
  1805. int max_errors;
  1806. int total_errors = 0;
  1807. u64 flags;
  1808. max_errors = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
  1809. do_barriers = !btrfs_test_opt(root, NOBARRIER);
  1810. sb = &root->fs_info->super_for_commit;
  1811. dev_item = &sb->dev_item;
  1812. list_for_each(cur, head) {
  1813. dev = list_entry(cur, struct btrfs_device, dev_list);
  1814. if (!dev->bdev) {
  1815. total_errors++;
  1816. continue;
  1817. }
  1818. if (!dev->in_fs_metadata || !dev->writeable)
  1819. continue;
  1820. btrfs_set_stack_device_generation(dev_item, 0);
  1821. btrfs_set_stack_device_type(dev_item, dev->type);
  1822. btrfs_set_stack_device_id(dev_item, dev->devid);
  1823. btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes);
  1824. btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used);
  1825. btrfs_set_stack_device_io_align(dev_item, dev->io_align);
  1826. btrfs_set_stack_device_io_width(dev_item, dev->io_width);
  1827. btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
  1828. memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
  1829. memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE);
  1830. flags = btrfs_super_flags(sb);
  1831. btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
  1832. ret = write_dev_supers(dev, sb, do_barriers, 0, max_mirrors);
  1833. if (ret)
  1834. total_errors++;
  1835. }
  1836. if (total_errors > max_errors) {
  1837. printk("btrfs: %d errors while writing supers\n", total_errors);
  1838. BUG();
  1839. }
  1840. total_errors = 0;
  1841. list_for_each(cur, head) {
  1842. dev = list_entry(cur, struct btrfs_device, dev_list);
  1843. if (!dev->bdev)
  1844. continue;
  1845. if (!dev->in_fs_metadata || !dev->writeable)
  1846. continue;
  1847. ret = write_dev_supers(dev, sb, do_barriers, 1, max_mirrors);
  1848. if (ret)
  1849. total_errors++;
  1850. }
  1851. if (total_errors > max_errors) {
  1852. printk("btrfs: %d errors while writing supers\n", total_errors);
  1853. BUG();
  1854. }
  1855. return 0;
  1856. }
  1857. int write_ctree_super(struct btrfs_trans_handle *trans,
  1858. struct btrfs_root *root, int max_mirrors)
  1859. {
  1860. int ret;
  1861. ret = write_all_supers(root, max_mirrors);
  1862. return ret;
  1863. }
  1864. int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
  1865. {
  1866. radix_tree_delete(&fs_info->fs_roots_radix,
  1867. (unsigned long)root->root_key.objectid);
  1868. if (root->anon_super.s_dev) {
  1869. down_write(&root->anon_super.s_umount);
  1870. kill_anon_super(&root->anon_super);
  1871. }
  1872. #if 0
  1873. if (root->in_sysfs)
  1874. btrfs_sysfs_del_root(root);
  1875. #endif
  1876. if (root->node)
  1877. free_extent_buffer(root->node);
  1878. if (root->commit_root)
  1879. free_extent_buffer(root->commit_root);
  1880. if (root->name)
  1881. kfree(root->name);
  1882. kfree(root);
  1883. return 0;
  1884. }
  1885. static int del_fs_roots(struct btrfs_fs_info *fs_info)
  1886. {
  1887. int ret;
  1888. struct btrfs_root *gang[8];
  1889. int i;
  1890. while(1) {
  1891. ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
  1892. (void **)gang, 0,
  1893. ARRAY_SIZE(gang));
  1894. if (!ret)
  1895. break;
  1896. for (i = 0; i < ret; i++)
  1897. btrfs_free_fs_root(fs_info, gang[i]);
  1898. }
  1899. return 0;
  1900. }
  1901. int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info)
  1902. {
  1903. u64 root_objectid = 0;
  1904. struct btrfs_root *gang[8];
  1905. int i;
  1906. int ret;
  1907. while (1) {
  1908. ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
  1909. (void **)gang, root_objectid,
  1910. ARRAY_SIZE(gang));
  1911. if (!ret)
  1912. break;
  1913. for (i = 0; i < ret; i++) {
  1914. root_objectid = gang[i]->root_key.objectid;
  1915. ret = btrfs_find_dead_roots(fs_info->tree_root,
  1916. root_objectid, gang[i]);
  1917. BUG_ON(ret);
  1918. btrfs_orphan_cleanup(gang[i]);
  1919. }
  1920. root_objectid++;
  1921. }
  1922. return 0;
  1923. }
  1924. int btrfs_commit_super(struct btrfs_root *root)
  1925. {
  1926. struct btrfs_trans_handle *trans;
  1927. int ret;
  1928. mutex_lock(&root->fs_info->cleaner_mutex);
  1929. btrfs_clean_old_snapshots(root);
  1930. mutex_unlock(&root->fs_info->cleaner_mutex);
  1931. trans = btrfs_start_transaction(root, 1);
  1932. ret = btrfs_commit_transaction(trans, root);
  1933. BUG_ON(ret);
  1934. /* run commit again to drop the original snapshot */
  1935. trans = btrfs_start_transaction(root, 1);
  1936. btrfs_commit_transaction(trans, root);
  1937. ret = btrfs_write_and_wait_transaction(NULL, root);
  1938. BUG_ON(ret);
  1939. ret = write_ctree_super(NULL, root, 0);
  1940. return ret;
  1941. }
  1942. int close_ctree(struct btrfs_root *root)
  1943. {
  1944. struct btrfs_fs_info *fs_info = root->fs_info;
  1945. int ret;
  1946. fs_info->closing = 1;
  1947. smp_mb();
  1948. kthread_stop(root->fs_info->transaction_kthread);
  1949. kthread_stop(root->fs_info->cleaner_kthread);
  1950. if (!(fs_info->sb->s_flags & MS_RDONLY)) {
  1951. ret = btrfs_commit_super(root);
  1952. if (ret) {
  1953. printk("btrfs: commit super returns %d\n", ret);
  1954. }
  1955. }
  1956. if (fs_info->delalloc_bytes) {
  1957. printk("btrfs: at unmount delalloc count %Lu\n",
  1958. fs_info->delalloc_bytes);
  1959. }
  1960. if (fs_info->total_ref_cache_size) {
  1961. printk("btrfs: at umount reference cache size %Lu\n",
  1962. fs_info->total_ref_cache_size);
  1963. }
  1964. if (fs_info->extent_root->node)
  1965. free_extent_buffer(fs_info->extent_root->node);
  1966. if (fs_info->tree_root->node)
  1967. free_extent_buffer(fs_info->tree_root->node);
  1968. if (root->fs_info->chunk_root->node);
  1969. free_extent_buffer(root->fs_info->chunk_root->node);
  1970. if (root->fs_info->dev_root->node);
  1971. free_extent_buffer(root->fs_info->dev_root->node);
  1972. if (root->fs_info->csum_root->node);
  1973. free_extent_buffer(root->fs_info->csum_root->node);
  1974. btrfs_free_block_groups(root->fs_info);
  1975. del_fs_roots(fs_info);
  1976. iput(fs_info->btree_inode);
  1977. btrfs_stop_workers(&fs_info->fixup_workers);
  1978. btrfs_stop_workers(&fs_info->delalloc_workers);
  1979. btrfs_stop_workers(&fs_info->workers);
  1980. btrfs_stop_workers(&fs_info->endio_workers);
  1981. btrfs_stop_workers(&fs_info->endio_meta_workers);
  1982. btrfs_stop_workers(&fs_info->endio_meta_write_workers);
  1983. btrfs_stop_workers(&fs_info->endio_write_workers);
  1984. btrfs_stop_workers(&fs_info->submit_workers);
  1985. #if 0
  1986. while(!list_empty(&fs_info->hashers)) {
  1987. struct btrfs_hasher *hasher;
  1988. hasher = list_entry(fs_info->hashers.next, struct btrfs_hasher,
  1989. hashers);
  1990. list_del(&hasher->hashers);
  1991. crypto_free_hash(&fs_info->hash_tfm);
  1992. kfree(hasher);
  1993. }
  1994. #endif
  1995. btrfs_close_devices(fs_info->fs_devices);
  1996. btrfs_mapping_tree_free(&fs_info->mapping_tree);
  1997. bdi_destroy(&fs_info->bdi);
  1998. kfree(fs_info->extent_root);
  1999. kfree(fs_info->tree_root);
  2000. kfree(fs_info->chunk_root);
  2001. kfree(fs_info->dev_root);
  2002. kfree(fs_info->csum_root);
  2003. return 0;
  2004. }
  2005. int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
  2006. {
  2007. int ret;
  2008. struct inode *btree_inode = buf->first_page->mapping->host;
  2009. ret = extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, buf);
  2010. if (!ret)
  2011. return ret;
  2012. ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf,
  2013. parent_transid);
  2014. return !ret;
  2015. }
  2016. int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
  2017. {
  2018. struct inode *btree_inode = buf->first_page->mapping->host;
  2019. return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree,
  2020. buf);
  2021. }
  2022. void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
  2023. {
  2024. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  2025. u64 transid = btrfs_header_generation(buf);
  2026. struct inode *btree_inode = root->fs_info->btree_inode;
  2027. WARN_ON(!btrfs_tree_locked(buf));
  2028. if (transid != root->fs_info->generation) {
  2029. printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n",
  2030. (unsigned long long)buf->start,
  2031. transid, root->fs_info->generation);
  2032. WARN_ON(1);
  2033. }
  2034. set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, buf);
  2035. }
  2036. void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
  2037. {
  2038. /*
  2039. * looks as though older kernels can get into trouble with
  2040. * this code, they end up stuck in balance_dirty_pages forever
  2041. */
  2042. struct extent_io_tree *tree;
  2043. u64 num_dirty;
  2044. u64 start = 0;
  2045. unsigned long thresh = 32 * 1024 * 1024;
  2046. tree = &BTRFS_I(root->fs_info->btree_inode)->io_tree;
  2047. if (current_is_pdflush() || current->flags & PF_MEMALLOC)
  2048. return;
  2049. num_dirty = count_range_bits(tree, &start, (u64)-1,
  2050. thresh, EXTENT_DIRTY);
  2051. if (num_dirty > thresh) {
  2052. balance_dirty_pages_ratelimited_nr(
  2053. root->fs_info->btree_inode->i_mapping, 1);
  2054. }
  2055. return;
  2056. }
  2057. int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid)
  2058. {
  2059. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  2060. int ret;
  2061. ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid);
  2062. if (ret == 0) {
  2063. buf->flags |= EXTENT_UPTODATE;
  2064. }
  2065. return ret;
  2066. }
  2067. int btree_lock_page_hook(struct page *page)
  2068. {
  2069. struct inode *inode = page->mapping->host;
  2070. struct btrfs_root *root = BTRFS_I(inode)->root;
  2071. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  2072. struct extent_buffer *eb;
  2073. unsigned long len;
  2074. u64 bytenr = page_offset(page);
  2075. if (page->private == EXTENT_PAGE_PRIVATE)
  2076. goto out;
  2077. len = page->private >> 2;
  2078. eb = find_extent_buffer(io_tree, bytenr, len, GFP_NOFS);
  2079. if (!eb)
  2080. goto out;
  2081. btrfs_tree_lock(eb);
  2082. spin_lock(&root->fs_info->hash_lock);
  2083. btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
  2084. spin_unlock(&root->fs_info->hash_lock);
  2085. btrfs_tree_unlock(eb);
  2086. free_extent_buffer(eb);
  2087. out:
  2088. lock_page(page);
  2089. return 0;
  2090. }
  2091. static struct extent_io_ops btree_extent_io_ops = {
  2092. .write_cache_pages_lock_hook = btree_lock_page_hook,
  2093. .readpage_end_io_hook = btree_readpage_end_io_hook,
  2094. .submit_bio_hook = btree_submit_bio_hook,
  2095. /* note we're sharing with inode.c for the merge bio hook */
  2096. .merge_bio_hook = btrfs_merge_bio_hook,
  2097. };