disk-io.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846
  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. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
  29. # include <linux/freezer.h>
  30. #else
  31. # include <linux/sched.h>
  32. #endif
  33. #include "crc32c.h"
  34. #include "ctree.h"
  35. #include "disk-io.h"
  36. #include "transaction.h"
  37. #include "btrfs_inode.h"
  38. #include "volumes.h"
  39. #include "print-tree.h"
  40. #include "async-thread.h"
  41. #include "locking.h"
  42. #include "ref-cache.h"
  43. #if 0
  44. static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
  45. {
  46. if (extent_buffer_blocknr(buf) != btrfs_header_blocknr(buf)) {
  47. printk(KERN_CRIT "buf blocknr(buf) is %llu, header is %llu\n",
  48. (unsigned long long)extent_buffer_blocknr(buf),
  49. (unsigned long long)btrfs_header_blocknr(buf));
  50. return 1;
  51. }
  52. return 0;
  53. }
  54. #endif
  55. static struct extent_io_ops btree_extent_io_ops;
  56. static void end_workqueue_fn(struct btrfs_work *work);
  57. struct end_io_wq {
  58. struct bio *bio;
  59. bio_end_io_t *end_io;
  60. void *private;
  61. struct btrfs_fs_info *info;
  62. int error;
  63. int metadata;
  64. struct list_head list;
  65. struct btrfs_work work;
  66. };
  67. struct async_submit_bio {
  68. struct inode *inode;
  69. struct bio *bio;
  70. struct list_head list;
  71. extent_submit_bio_hook_t *submit_bio_hook;
  72. int rw;
  73. int mirror_num;
  74. struct btrfs_work work;
  75. };
  76. struct extent_map *btree_get_extent(struct inode *inode, struct page *page,
  77. size_t page_offset, u64 start, u64 len,
  78. int create)
  79. {
  80. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  81. struct extent_map *em;
  82. int ret;
  83. spin_lock(&em_tree->lock);
  84. em = lookup_extent_mapping(em_tree, start, len);
  85. if (em) {
  86. em->bdev =
  87. BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
  88. spin_unlock(&em_tree->lock);
  89. goto out;
  90. }
  91. spin_unlock(&em_tree->lock);
  92. em = alloc_extent_map(GFP_NOFS);
  93. if (!em) {
  94. em = ERR_PTR(-ENOMEM);
  95. goto out;
  96. }
  97. em->start = 0;
  98. em->len = (u64)-1;
  99. em->block_start = 0;
  100. em->bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
  101. spin_lock(&em_tree->lock);
  102. ret = add_extent_mapping(em_tree, em);
  103. if (ret == -EEXIST) {
  104. u64 failed_start = em->start;
  105. u64 failed_len = em->len;
  106. printk("failed to insert %Lu %Lu -> %Lu into tree\n",
  107. em->start, em->len, em->block_start);
  108. free_extent_map(em);
  109. em = lookup_extent_mapping(em_tree, start, len);
  110. if (em) {
  111. printk("after failing, found %Lu %Lu %Lu\n",
  112. em->start, em->len, em->block_start);
  113. ret = 0;
  114. } else {
  115. em = lookup_extent_mapping(em_tree, failed_start,
  116. failed_len);
  117. if (em) {
  118. printk("double failure lookup gives us "
  119. "%Lu %Lu -> %Lu\n", em->start,
  120. em->len, em->block_start);
  121. free_extent_map(em);
  122. }
  123. ret = -EIO;
  124. }
  125. } else if (ret) {
  126. free_extent_map(em);
  127. em = NULL;
  128. }
  129. spin_unlock(&em_tree->lock);
  130. if (ret)
  131. em = ERR_PTR(ret);
  132. out:
  133. return em;
  134. }
  135. u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
  136. {
  137. return btrfs_crc32c(seed, data, len);
  138. }
  139. void btrfs_csum_final(u32 crc, char *result)
  140. {
  141. *(__le32 *)result = ~cpu_to_le32(crc);
  142. }
  143. static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
  144. int verify)
  145. {
  146. char result[BTRFS_CRC32_SIZE];
  147. unsigned long len;
  148. unsigned long cur_len;
  149. unsigned long offset = BTRFS_CSUM_SIZE;
  150. char *map_token = NULL;
  151. char *kaddr;
  152. unsigned long map_start;
  153. unsigned long map_len;
  154. int err;
  155. u32 crc = ~(u32)0;
  156. len = buf->len - offset;
  157. while(len > 0) {
  158. err = map_private_extent_buffer(buf, offset, 32,
  159. &map_token, &kaddr,
  160. &map_start, &map_len, KM_USER0);
  161. if (err) {
  162. printk("failed to map extent buffer! %lu\n",
  163. offset);
  164. return 1;
  165. }
  166. cur_len = min(len, map_len - (offset - map_start));
  167. crc = btrfs_csum_data(root, kaddr + offset - map_start,
  168. crc, cur_len);
  169. len -= cur_len;
  170. offset += cur_len;
  171. unmap_extent_buffer(buf, map_token, KM_USER0);
  172. }
  173. btrfs_csum_final(crc, result);
  174. if (verify) {
  175. int from_this_trans = 0;
  176. if (root->fs_info->running_transaction &&
  177. btrfs_header_generation(buf) ==
  178. root->fs_info->running_transaction->transid)
  179. from_this_trans = 1;
  180. /* FIXME, this is not good */
  181. if (memcmp_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE)) {
  182. u32 val;
  183. u32 found = 0;
  184. memcpy(&found, result, BTRFS_CRC32_SIZE);
  185. read_extent_buffer(buf, &val, 0, BTRFS_CRC32_SIZE);
  186. printk("btrfs: %s checksum verify failed on %llu "
  187. "wanted %X found %X from_this_trans %d "
  188. "level %d\n",
  189. root->fs_info->sb->s_id,
  190. buf->start, val, found, from_this_trans,
  191. btrfs_header_level(buf));
  192. return 1;
  193. }
  194. } else {
  195. write_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE);
  196. }
  197. return 0;
  198. }
  199. static int verify_parent_transid(struct extent_io_tree *io_tree,
  200. struct extent_buffer *eb, u64 parent_transid)
  201. {
  202. int ret;
  203. if (!parent_transid || btrfs_header_generation(eb) == parent_transid)
  204. return 0;
  205. lock_extent(io_tree, eb->start, eb->start + eb->len - 1, GFP_NOFS);
  206. if (extent_buffer_uptodate(io_tree, eb) &&
  207. btrfs_header_generation(eb) == parent_transid) {
  208. ret = 0;
  209. goto out;
  210. }
  211. printk("parent transid verify failed on %llu wanted %llu found %llu\n",
  212. (unsigned long long)eb->start,
  213. (unsigned long long)parent_transid,
  214. (unsigned long long)btrfs_header_generation(eb));
  215. ret = 1;
  216. out:
  217. clear_extent_buffer_uptodate(io_tree, eb);
  218. unlock_extent(io_tree, eb->start, eb->start + eb->len - 1,
  219. GFP_NOFS);
  220. return ret;
  221. }
  222. static int btree_read_extent_buffer_pages(struct btrfs_root *root,
  223. struct extent_buffer *eb,
  224. u64 start, u64 parent_transid)
  225. {
  226. struct extent_io_tree *io_tree;
  227. int ret;
  228. int num_copies = 0;
  229. int mirror_num = 0;
  230. io_tree = &BTRFS_I(root->fs_info->btree_inode)->io_tree;
  231. while (1) {
  232. ret = read_extent_buffer_pages(io_tree, eb, start, 1,
  233. btree_get_extent, mirror_num);
  234. if (!ret &&
  235. !verify_parent_transid(io_tree, eb, parent_transid))
  236. return ret;
  237. num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
  238. eb->start, eb->len);
  239. if (num_copies == 1)
  240. return ret;
  241. mirror_num++;
  242. if (mirror_num > num_copies)
  243. return ret;
  244. }
  245. return -EIO;
  246. }
  247. int csum_dirty_buffer(struct btrfs_root *root, struct page *page)
  248. {
  249. struct extent_io_tree *tree;
  250. u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
  251. u64 found_start;
  252. int found_level;
  253. unsigned long len;
  254. struct extent_buffer *eb;
  255. int ret;
  256. tree = &BTRFS_I(page->mapping->host)->io_tree;
  257. if (page->private == EXTENT_PAGE_PRIVATE)
  258. goto out;
  259. if (!page->private)
  260. goto out;
  261. len = page->private >> 2;
  262. if (len == 0) {
  263. WARN_ON(1);
  264. }
  265. eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS);
  266. ret = btree_read_extent_buffer_pages(root, eb, start + PAGE_CACHE_SIZE,
  267. btrfs_header_generation(eb));
  268. BUG_ON(ret);
  269. found_start = btrfs_header_bytenr(eb);
  270. if (found_start != start) {
  271. printk("warning: eb start incorrect %Lu buffer %Lu len %lu\n",
  272. start, found_start, len);
  273. WARN_ON(1);
  274. goto err;
  275. }
  276. if (eb->first_page != page) {
  277. printk("bad first page %lu %lu\n", eb->first_page->index,
  278. page->index);
  279. WARN_ON(1);
  280. goto err;
  281. }
  282. if (!PageUptodate(page)) {
  283. printk("csum not up to date page %lu\n", page->index);
  284. WARN_ON(1);
  285. goto err;
  286. }
  287. found_level = btrfs_header_level(eb);
  288. spin_lock(&root->fs_info->hash_lock);
  289. btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
  290. spin_unlock(&root->fs_info->hash_lock);
  291. csum_tree_block(root, eb, 0);
  292. err:
  293. free_extent_buffer(eb);
  294. out:
  295. return 0;
  296. }
  297. static int btree_writepage_io_hook(struct page *page, u64 start, u64 end)
  298. {
  299. struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
  300. csum_dirty_buffer(root, page);
  301. return 0;
  302. }
  303. int btree_readpage_end_io_hook(struct page *page, u64 start, u64 end,
  304. struct extent_state *state)
  305. {
  306. struct extent_io_tree *tree;
  307. u64 found_start;
  308. int found_level;
  309. unsigned long len;
  310. struct extent_buffer *eb;
  311. struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
  312. int ret = 0;
  313. tree = &BTRFS_I(page->mapping->host)->io_tree;
  314. if (page->private == EXTENT_PAGE_PRIVATE)
  315. goto out;
  316. if (!page->private)
  317. goto out;
  318. len = page->private >> 2;
  319. if (len == 0) {
  320. WARN_ON(1);
  321. }
  322. eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS);
  323. found_start = btrfs_header_bytenr(eb);
  324. if (found_start != start) {
  325. ret = -EIO;
  326. goto err;
  327. }
  328. if (eb->first_page != page) {
  329. printk("bad first page %lu %lu\n", eb->first_page->index,
  330. page->index);
  331. WARN_ON(1);
  332. ret = -EIO;
  333. goto err;
  334. }
  335. if (memcmp_extent_buffer(eb, root->fs_info->fsid,
  336. (unsigned long)btrfs_header_fsid(eb),
  337. BTRFS_FSID_SIZE)) {
  338. printk("bad fsid on block %Lu\n", eb->start);
  339. ret = -EIO;
  340. goto err;
  341. }
  342. found_level = btrfs_header_level(eb);
  343. ret = csum_tree_block(root, eb, 1);
  344. if (ret)
  345. ret = -EIO;
  346. end = min_t(u64, eb->len, PAGE_CACHE_SIZE);
  347. end = eb->start + end - 1;
  348. err:
  349. free_extent_buffer(eb);
  350. out:
  351. return ret;
  352. }
  353. #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
  354. static void end_workqueue_bio(struct bio *bio, int err)
  355. #else
  356. static int end_workqueue_bio(struct bio *bio,
  357. unsigned int bytes_done, int err)
  358. #endif
  359. {
  360. struct end_io_wq *end_io_wq = bio->bi_private;
  361. struct btrfs_fs_info *fs_info;
  362. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
  363. if (bio->bi_size)
  364. return 1;
  365. #endif
  366. fs_info = end_io_wq->info;
  367. end_io_wq->error = err;
  368. end_io_wq->work.func = end_workqueue_fn;
  369. end_io_wq->work.flags = 0;
  370. if (bio->bi_rw & (1 << BIO_RW))
  371. btrfs_queue_worker(&fs_info->endio_write_workers,
  372. &end_io_wq->work);
  373. else
  374. btrfs_queue_worker(&fs_info->endio_workers, &end_io_wq->work);
  375. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
  376. return 0;
  377. #endif
  378. }
  379. int btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio,
  380. int metadata)
  381. {
  382. struct end_io_wq *end_io_wq;
  383. end_io_wq = kmalloc(sizeof(*end_io_wq), GFP_NOFS);
  384. if (!end_io_wq)
  385. return -ENOMEM;
  386. end_io_wq->private = bio->bi_private;
  387. end_io_wq->end_io = bio->bi_end_io;
  388. end_io_wq->info = info;
  389. end_io_wq->error = 0;
  390. end_io_wq->bio = bio;
  391. end_io_wq->metadata = metadata;
  392. bio->bi_private = end_io_wq;
  393. bio->bi_end_io = end_workqueue_bio;
  394. return 0;
  395. }
  396. static void run_one_async_submit(struct btrfs_work *work)
  397. {
  398. struct btrfs_fs_info *fs_info;
  399. struct async_submit_bio *async;
  400. async = container_of(work, struct async_submit_bio, work);
  401. fs_info = BTRFS_I(async->inode)->root->fs_info;
  402. atomic_dec(&fs_info->nr_async_submits);
  403. async->submit_bio_hook(async->inode, async->rw, async->bio,
  404. async->mirror_num);
  405. kfree(async);
  406. }
  407. int btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct inode *inode,
  408. int rw, struct bio *bio, int mirror_num,
  409. extent_submit_bio_hook_t *submit_bio_hook)
  410. {
  411. struct async_submit_bio *async;
  412. async = kmalloc(sizeof(*async), GFP_NOFS);
  413. if (!async)
  414. return -ENOMEM;
  415. async->inode = inode;
  416. async->rw = rw;
  417. async->bio = bio;
  418. async->mirror_num = mirror_num;
  419. async->submit_bio_hook = submit_bio_hook;
  420. async->work.func = run_one_async_submit;
  421. async->work.flags = 0;
  422. atomic_inc(&fs_info->nr_async_submits);
  423. btrfs_queue_worker(&fs_info->workers, &async->work);
  424. return 0;
  425. }
  426. static int __btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
  427. int mirror_num)
  428. {
  429. struct btrfs_root *root = BTRFS_I(inode)->root;
  430. u64 offset;
  431. int ret;
  432. offset = bio->bi_sector << 9;
  433. /*
  434. * when we're called for a write, we're already in the async
  435. * submission context. Just jump ingo btrfs_map_bio
  436. */
  437. if (rw & (1 << BIO_RW)) {
  438. return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio,
  439. mirror_num, 0);
  440. }
  441. /*
  442. * called for a read, do the setup so that checksum validation
  443. * can happen in the async kernel threads
  444. */
  445. ret = btrfs_bio_wq_end_io(root->fs_info, bio, 1);
  446. BUG_ON(ret);
  447. return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, mirror_num, 1);
  448. }
  449. static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
  450. int mirror_num)
  451. {
  452. /*
  453. * kthread helpers are used to submit writes so that checksumming
  454. * can happen in parallel across all CPUs
  455. */
  456. if (!(rw & (1 << BIO_RW))) {
  457. return __btree_submit_bio_hook(inode, rw, bio, mirror_num);
  458. }
  459. return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
  460. inode, rw, bio, mirror_num,
  461. __btree_submit_bio_hook);
  462. }
  463. static int btree_writepage(struct page *page, struct writeback_control *wbc)
  464. {
  465. struct extent_io_tree *tree;
  466. tree = &BTRFS_I(page->mapping->host)->io_tree;
  467. return extent_write_full_page(tree, page, btree_get_extent, wbc);
  468. }
  469. static int btree_writepages(struct address_space *mapping,
  470. struct writeback_control *wbc)
  471. {
  472. struct extent_io_tree *tree;
  473. tree = &BTRFS_I(mapping->host)->io_tree;
  474. if (wbc->sync_mode == WB_SYNC_NONE) {
  475. u64 num_dirty;
  476. u64 start = 0;
  477. unsigned long thresh = 96 * 1024 * 1024;
  478. if (wbc->for_kupdate)
  479. return 0;
  480. if (current_is_pdflush()) {
  481. thresh = 96 * 1024 * 1024;
  482. } else {
  483. thresh = 8 * 1024 * 1024;
  484. }
  485. num_dirty = count_range_bits(tree, &start, (u64)-1,
  486. thresh, EXTENT_DIRTY);
  487. if (num_dirty < thresh) {
  488. return 0;
  489. }
  490. }
  491. return extent_writepages(tree, mapping, btree_get_extent, wbc);
  492. }
  493. int btree_readpage(struct file *file, struct page *page)
  494. {
  495. struct extent_io_tree *tree;
  496. tree = &BTRFS_I(page->mapping->host)->io_tree;
  497. return extent_read_full_page(tree, page, btree_get_extent);
  498. }
  499. static int btree_releasepage(struct page *page, gfp_t gfp_flags)
  500. {
  501. struct extent_io_tree *tree;
  502. struct extent_map_tree *map;
  503. int ret;
  504. tree = &BTRFS_I(page->mapping->host)->io_tree;
  505. map = &BTRFS_I(page->mapping->host)->extent_tree;
  506. ret = try_release_extent_state(map, tree, page, gfp_flags);
  507. if (!ret) {
  508. return 0;
  509. }
  510. ret = try_release_extent_buffer(tree, page);
  511. if (ret == 1) {
  512. ClearPagePrivate(page);
  513. set_page_private(page, 0);
  514. page_cache_release(page);
  515. }
  516. return ret;
  517. }
  518. static void btree_invalidatepage(struct page *page, unsigned long offset)
  519. {
  520. struct extent_io_tree *tree;
  521. tree = &BTRFS_I(page->mapping->host)->io_tree;
  522. extent_invalidatepage(tree, page, offset);
  523. btree_releasepage(page, GFP_NOFS);
  524. if (PagePrivate(page)) {
  525. printk("warning page private not zero on page %Lu\n",
  526. page_offset(page));
  527. ClearPagePrivate(page);
  528. set_page_private(page, 0);
  529. page_cache_release(page);
  530. }
  531. }
  532. #if 0
  533. static int btree_writepage(struct page *page, struct writeback_control *wbc)
  534. {
  535. struct buffer_head *bh;
  536. struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
  537. struct buffer_head *head;
  538. if (!page_has_buffers(page)) {
  539. create_empty_buffers(page, root->fs_info->sb->s_blocksize,
  540. (1 << BH_Dirty)|(1 << BH_Uptodate));
  541. }
  542. head = page_buffers(page);
  543. bh = head;
  544. do {
  545. if (buffer_dirty(bh))
  546. csum_tree_block(root, bh, 0);
  547. bh = bh->b_this_page;
  548. } while (bh != head);
  549. return block_write_full_page(page, btree_get_block, wbc);
  550. }
  551. #endif
  552. static struct address_space_operations btree_aops = {
  553. .readpage = btree_readpage,
  554. .writepage = btree_writepage,
  555. .writepages = btree_writepages,
  556. .releasepage = btree_releasepage,
  557. .invalidatepage = btree_invalidatepage,
  558. .sync_page = block_sync_page,
  559. };
  560. int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize,
  561. u64 parent_transid)
  562. {
  563. struct extent_buffer *buf = NULL;
  564. struct inode *btree_inode = root->fs_info->btree_inode;
  565. int ret = 0;
  566. buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
  567. if (!buf)
  568. return 0;
  569. read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
  570. buf, 0, 0, btree_get_extent, 0);
  571. free_extent_buffer(buf);
  572. return ret;
  573. }
  574. struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
  575. u64 bytenr, u32 blocksize)
  576. {
  577. struct inode *btree_inode = root->fs_info->btree_inode;
  578. struct extent_buffer *eb;
  579. eb = find_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
  580. bytenr, blocksize, GFP_NOFS);
  581. return eb;
  582. }
  583. struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
  584. u64 bytenr, u32 blocksize)
  585. {
  586. struct inode *btree_inode = root->fs_info->btree_inode;
  587. struct extent_buffer *eb;
  588. eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
  589. bytenr, blocksize, NULL, GFP_NOFS);
  590. return eb;
  591. }
  592. struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
  593. u32 blocksize, u64 parent_transid)
  594. {
  595. struct extent_buffer *buf = NULL;
  596. struct inode *btree_inode = root->fs_info->btree_inode;
  597. struct extent_io_tree *io_tree;
  598. int ret;
  599. io_tree = &BTRFS_I(btree_inode)->io_tree;
  600. buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
  601. if (!buf)
  602. return NULL;
  603. ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid);
  604. if (ret == 0) {
  605. buf->flags |= EXTENT_UPTODATE;
  606. }
  607. return buf;
  608. }
  609. int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  610. struct extent_buffer *buf)
  611. {
  612. struct inode *btree_inode = root->fs_info->btree_inode;
  613. if (btrfs_header_generation(buf) ==
  614. root->fs_info->running_transaction->transid) {
  615. WARN_ON(!btrfs_tree_locked(buf));
  616. clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree,
  617. buf);
  618. }
  619. return 0;
  620. }
  621. int wait_on_tree_block_writeback(struct btrfs_root *root,
  622. struct extent_buffer *buf)
  623. {
  624. struct inode *btree_inode = root->fs_info->btree_inode;
  625. wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->io_tree,
  626. buf);
  627. return 0;
  628. }
  629. static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
  630. u32 stripesize, struct btrfs_root *root,
  631. struct btrfs_fs_info *fs_info,
  632. u64 objectid)
  633. {
  634. root->node = NULL;
  635. root->inode = NULL;
  636. root->commit_root = NULL;
  637. root->ref_tree = NULL;
  638. root->sectorsize = sectorsize;
  639. root->nodesize = nodesize;
  640. root->leafsize = leafsize;
  641. root->stripesize = stripesize;
  642. root->ref_cows = 0;
  643. root->track_dirty = 0;
  644. root->fs_info = fs_info;
  645. root->objectid = objectid;
  646. root->last_trans = 0;
  647. root->highest_inode = 0;
  648. root->last_inode_alloc = 0;
  649. root->name = NULL;
  650. root->in_sysfs = 0;
  651. INIT_LIST_HEAD(&root->dirty_list);
  652. INIT_LIST_HEAD(&root->orphan_list);
  653. spin_lock_init(&root->node_lock);
  654. spin_lock_init(&root->orphan_lock);
  655. mutex_init(&root->objectid_mutex);
  656. btrfs_leaf_ref_tree_init(&root->ref_tree_struct);
  657. root->ref_tree = &root->ref_tree_struct;
  658. memset(&root->root_key, 0, sizeof(root->root_key));
  659. memset(&root->root_item, 0, sizeof(root->root_item));
  660. memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
  661. memset(&root->root_kobj, 0, sizeof(root->root_kobj));
  662. root->defrag_trans_start = fs_info->generation;
  663. init_completion(&root->kobj_unregister);
  664. root->defrag_running = 0;
  665. root->defrag_level = 0;
  666. root->root_key.objectid = objectid;
  667. return 0;
  668. }
  669. static int find_and_setup_root(struct btrfs_root *tree_root,
  670. struct btrfs_fs_info *fs_info,
  671. u64 objectid,
  672. struct btrfs_root *root)
  673. {
  674. int ret;
  675. u32 blocksize;
  676. __setup_root(tree_root->nodesize, tree_root->leafsize,
  677. tree_root->sectorsize, tree_root->stripesize,
  678. root, fs_info, objectid);
  679. ret = btrfs_find_last_root(tree_root, objectid,
  680. &root->root_item, &root->root_key);
  681. BUG_ON(ret);
  682. blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
  683. root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
  684. blocksize, 0);
  685. BUG_ON(!root->node);
  686. return 0;
  687. }
  688. struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
  689. struct btrfs_key *location)
  690. {
  691. struct btrfs_root *root;
  692. struct btrfs_root *tree_root = fs_info->tree_root;
  693. struct btrfs_path *path;
  694. struct extent_buffer *l;
  695. u64 highest_inode;
  696. u32 blocksize;
  697. int ret = 0;
  698. root = kzalloc(sizeof(*root), GFP_NOFS);
  699. if (!root)
  700. return ERR_PTR(-ENOMEM);
  701. if (location->offset == (u64)-1) {
  702. ret = find_and_setup_root(tree_root, fs_info,
  703. location->objectid, root);
  704. if (ret) {
  705. kfree(root);
  706. return ERR_PTR(ret);
  707. }
  708. goto insert;
  709. }
  710. __setup_root(tree_root->nodesize, tree_root->leafsize,
  711. tree_root->sectorsize, tree_root->stripesize,
  712. root, fs_info, location->objectid);
  713. path = btrfs_alloc_path();
  714. BUG_ON(!path);
  715. ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
  716. if (ret != 0) {
  717. if (ret > 0)
  718. ret = -ENOENT;
  719. goto out;
  720. }
  721. l = path->nodes[0];
  722. read_extent_buffer(l, &root->root_item,
  723. btrfs_item_ptr_offset(l, path->slots[0]),
  724. sizeof(root->root_item));
  725. memcpy(&root->root_key, location, sizeof(*location));
  726. ret = 0;
  727. out:
  728. btrfs_release_path(root, path);
  729. btrfs_free_path(path);
  730. if (ret) {
  731. kfree(root);
  732. return ERR_PTR(ret);
  733. }
  734. blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
  735. root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
  736. blocksize, 0);
  737. BUG_ON(!root->node);
  738. insert:
  739. root->ref_cows = 1;
  740. ret = btrfs_find_highest_inode(root, &highest_inode);
  741. if (ret == 0) {
  742. root->highest_inode = highest_inode;
  743. root->last_inode_alloc = highest_inode;
  744. }
  745. return root;
  746. }
  747. struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info,
  748. u64 root_objectid)
  749. {
  750. struct btrfs_root *root;
  751. if (root_objectid == BTRFS_ROOT_TREE_OBJECTID)
  752. return fs_info->tree_root;
  753. if (root_objectid == BTRFS_EXTENT_TREE_OBJECTID)
  754. return fs_info->extent_root;
  755. root = radix_tree_lookup(&fs_info->fs_roots_radix,
  756. (unsigned long)root_objectid);
  757. return root;
  758. }
  759. struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info,
  760. struct btrfs_key *location)
  761. {
  762. struct btrfs_root *root;
  763. int ret;
  764. if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
  765. return fs_info->tree_root;
  766. if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
  767. return fs_info->extent_root;
  768. if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
  769. return fs_info->chunk_root;
  770. if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
  771. return fs_info->dev_root;
  772. root = radix_tree_lookup(&fs_info->fs_roots_radix,
  773. (unsigned long)location->objectid);
  774. if (root)
  775. return root;
  776. root = btrfs_read_fs_root_no_radix(fs_info, location);
  777. if (IS_ERR(root))
  778. return root;
  779. ret = radix_tree_insert(&fs_info->fs_roots_radix,
  780. (unsigned long)root->root_key.objectid,
  781. root);
  782. if (ret) {
  783. free_extent_buffer(root->node);
  784. kfree(root);
  785. return ERR_PTR(ret);
  786. }
  787. ret = btrfs_find_dead_roots(fs_info->tree_root,
  788. root->root_key.objectid, root);
  789. BUG_ON(ret);
  790. return root;
  791. }
  792. struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
  793. struct btrfs_key *location,
  794. const char *name, int namelen)
  795. {
  796. struct btrfs_root *root;
  797. int ret;
  798. root = btrfs_read_fs_root_no_name(fs_info, location);
  799. if (!root)
  800. return NULL;
  801. if (root->in_sysfs)
  802. return root;
  803. ret = btrfs_set_root_name(root, name, namelen);
  804. if (ret) {
  805. free_extent_buffer(root->node);
  806. kfree(root);
  807. return ERR_PTR(ret);
  808. }
  809. ret = btrfs_sysfs_add_root(root);
  810. if (ret) {
  811. free_extent_buffer(root->node);
  812. kfree(root->name);
  813. kfree(root);
  814. return ERR_PTR(ret);
  815. }
  816. root->in_sysfs = 1;
  817. return root;
  818. }
  819. #if 0
  820. static int add_hasher(struct btrfs_fs_info *info, char *type) {
  821. struct btrfs_hasher *hasher;
  822. hasher = kmalloc(sizeof(*hasher), GFP_NOFS);
  823. if (!hasher)
  824. return -ENOMEM;
  825. hasher->hash_tfm = crypto_alloc_hash(type, 0, CRYPTO_ALG_ASYNC);
  826. if (!hasher->hash_tfm) {
  827. kfree(hasher);
  828. return -EINVAL;
  829. }
  830. spin_lock(&info->hash_lock);
  831. list_add(&hasher->list, &info->hashers);
  832. spin_unlock(&info->hash_lock);
  833. return 0;
  834. }
  835. #endif
  836. static int btrfs_congested_fn(void *congested_data, int bdi_bits)
  837. {
  838. struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data;
  839. int ret = 0;
  840. int limit = 256 * info->fs_devices->open_devices;
  841. struct list_head *cur;
  842. struct btrfs_device *device;
  843. struct backing_dev_info *bdi;
  844. if ((bdi_bits & (1 << BDI_write_congested)) &&
  845. atomic_read(&info->nr_async_submits) > limit) {
  846. return 1;
  847. }
  848. list_for_each(cur, &info->fs_devices->devices) {
  849. device = list_entry(cur, struct btrfs_device, dev_list);
  850. if (!device->bdev)
  851. continue;
  852. bdi = blk_get_backing_dev_info(device->bdev);
  853. if (bdi && bdi_congested(bdi, bdi_bits)) {
  854. ret = 1;
  855. break;
  856. }
  857. }
  858. return ret;
  859. }
  860. /*
  861. * this unplugs every device on the box, and it is only used when page
  862. * is null
  863. */
  864. static void __unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
  865. {
  866. struct list_head *cur;
  867. struct btrfs_device *device;
  868. struct btrfs_fs_info *info;
  869. info = (struct btrfs_fs_info *)bdi->unplug_io_data;
  870. list_for_each(cur, &info->fs_devices->devices) {
  871. device = list_entry(cur, struct btrfs_device, dev_list);
  872. bdi = blk_get_backing_dev_info(device->bdev);
  873. if (bdi->unplug_io_fn) {
  874. bdi->unplug_io_fn(bdi, page);
  875. }
  876. }
  877. }
  878. void btrfs_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
  879. {
  880. struct inode *inode;
  881. struct extent_map_tree *em_tree;
  882. struct extent_map *em;
  883. struct address_space *mapping;
  884. u64 offset;
  885. /* the generic O_DIRECT read code does this */
  886. if (!page) {
  887. __unplug_io_fn(bdi, page);
  888. return;
  889. }
  890. /*
  891. * page->mapping may change at any time. Get a consistent copy
  892. * and use that for everything below
  893. */
  894. smp_mb();
  895. mapping = page->mapping;
  896. if (!mapping)
  897. return;
  898. inode = mapping->host;
  899. offset = page_offset(page);
  900. em_tree = &BTRFS_I(inode)->extent_tree;
  901. spin_lock(&em_tree->lock);
  902. em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
  903. spin_unlock(&em_tree->lock);
  904. if (!em) {
  905. __unplug_io_fn(bdi, page);
  906. return;
  907. }
  908. if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
  909. free_extent_map(em);
  910. __unplug_io_fn(bdi, page);
  911. return;
  912. }
  913. offset = offset - em->start;
  914. btrfs_unplug_page(&BTRFS_I(inode)->root->fs_info->mapping_tree,
  915. em->block_start + offset, page);
  916. free_extent_map(em);
  917. }
  918. static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi)
  919. {
  920. #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
  921. bdi_init(bdi);
  922. #endif
  923. bdi->ra_pages = default_backing_dev_info.ra_pages;
  924. bdi->state = 0;
  925. bdi->capabilities = default_backing_dev_info.capabilities;
  926. bdi->unplug_io_fn = btrfs_unplug_io_fn;
  927. bdi->unplug_io_data = info;
  928. bdi->congested_fn = btrfs_congested_fn;
  929. bdi->congested_data = info;
  930. return 0;
  931. }
  932. static int bio_ready_for_csum(struct bio *bio)
  933. {
  934. u64 length = 0;
  935. u64 buf_len = 0;
  936. u64 start = 0;
  937. struct page *page;
  938. struct extent_io_tree *io_tree = NULL;
  939. struct btrfs_fs_info *info = NULL;
  940. struct bio_vec *bvec;
  941. int i;
  942. int ret;
  943. bio_for_each_segment(bvec, bio, i) {
  944. page = bvec->bv_page;
  945. if (page->private == EXTENT_PAGE_PRIVATE) {
  946. length += bvec->bv_len;
  947. continue;
  948. }
  949. if (!page->private) {
  950. length += bvec->bv_len;
  951. continue;
  952. }
  953. length = bvec->bv_len;
  954. buf_len = page->private >> 2;
  955. start = page_offset(page) + bvec->bv_offset;
  956. io_tree = &BTRFS_I(page->mapping->host)->io_tree;
  957. info = BTRFS_I(page->mapping->host)->root->fs_info;
  958. }
  959. /* are we fully contained in this bio? */
  960. if (buf_len <= length)
  961. return 1;
  962. ret = extent_range_uptodate(io_tree, start + length,
  963. start + buf_len - 1);
  964. if (ret == 1)
  965. return ret;
  966. return ret;
  967. }
  968. /*
  969. * called by the kthread helper functions to finally call the bio end_io
  970. * functions. This is where read checksum verification actually happens
  971. */
  972. static void end_workqueue_fn(struct btrfs_work *work)
  973. {
  974. struct bio *bio;
  975. struct end_io_wq *end_io_wq;
  976. struct btrfs_fs_info *fs_info;
  977. int error;
  978. end_io_wq = container_of(work, struct end_io_wq, work);
  979. bio = end_io_wq->bio;
  980. fs_info = end_io_wq->info;
  981. /* metadata bios are special because the whole tree block must
  982. * be checksummed at once. This makes sure the entire block is in
  983. * ram and up to date before trying to verify things. For
  984. * blocksize <= pagesize, it is basically a noop
  985. */
  986. if (end_io_wq->metadata && !bio_ready_for_csum(bio)) {
  987. btrfs_queue_worker(&fs_info->endio_workers,
  988. &end_io_wq->work);
  989. return;
  990. }
  991. error = end_io_wq->error;
  992. bio->bi_private = end_io_wq->private;
  993. bio->bi_end_io = end_io_wq->end_io;
  994. kfree(end_io_wq);
  995. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
  996. bio_endio(bio, bio->bi_size, error);
  997. #else
  998. bio_endio(bio, error);
  999. #endif
  1000. }
  1001. static int cleaner_kthread(void *arg)
  1002. {
  1003. struct btrfs_root *root = arg;
  1004. do {
  1005. smp_mb();
  1006. if (root->fs_info->closing)
  1007. break;
  1008. vfs_check_frozen(root->fs_info->sb, SB_FREEZE_WRITE);
  1009. mutex_lock(&root->fs_info->cleaner_mutex);
  1010. btrfs_clean_old_snapshots(root);
  1011. mutex_unlock(&root->fs_info->cleaner_mutex);
  1012. if (freezing(current)) {
  1013. refrigerator();
  1014. } else {
  1015. smp_mb();
  1016. if (root->fs_info->closing)
  1017. break;
  1018. set_current_state(TASK_INTERRUPTIBLE);
  1019. schedule();
  1020. __set_current_state(TASK_RUNNING);
  1021. }
  1022. } while (!kthread_should_stop());
  1023. return 0;
  1024. }
  1025. static int transaction_kthread(void *arg)
  1026. {
  1027. struct btrfs_root *root = arg;
  1028. struct btrfs_trans_handle *trans;
  1029. struct btrfs_transaction *cur;
  1030. unsigned long now;
  1031. unsigned long delay;
  1032. int ret;
  1033. do {
  1034. smp_mb();
  1035. if (root->fs_info->closing)
  1036. break;
  1037. delay = HZ * 30;
  1038. vfs_check_frozen(root->fs_info->sb, SB_FREEZE_WRITE);
  1039. mutex_lock(&root->fs_info->transaction_kthread_mutex);
  1040. printk("btrfs: total reference cache size %Lu\n",
  1041. root->fs_info->total_ref_cache_size);
  1042. mutex_lock(&root->fs_info->trans_mutex);
  1043. cur = root->fs_info->running_transaction;
  1044. if (!cur) {
  1045. mutex_unlock(&root->fs_info->trans_mutex);
  1046. goto sleep;
  1047. }
  1048. now = get_seconds();
  1049. if (now < cur->start_time || now - cur->start_time < 30) {
  1050. mutex_unlock(&root->fs_info->trans_mutex);
  1051. delay = HZ * 5;
  1052. goto sleep;
  1053. }
  1054. mutex_unlock(&root->fs_info->trans_mutex);
  1055. trans = btrfs_start_transaction(root, 1);
  1056. ret = btrfs_commit_transaction(trans, root);
  1057. sleep:
  1058. wake_up_process(root->fs_info->cleaner_kthread);
  1059. mutex_unlock(&root->fs_info->transaction_kthread_mutex);
  1060. if (freezing(current)) {
  1061. refrigerator();
  1062. } else {
  1063. if (root->fs_info->closing)
  1064. break;
  1065. set_current_state(TASK_INTERRUPTIBLE);
  1066. schedule_timeout(delay);
  1067. __set_current_state(TASK_RUNNING);
  1068. }
  1069. } while (!kthread_should_stop());
  1070. return 0;
  1071. }
  1072. struct btrfs_root *open_ctree(struct super_block *sb,
  1073. struct btrfs_fs_devices *fs_devices,
  1074. char *options)
  1075. {
  1076. u32 sectorsize;
  1077. u32 nodesize;
  1078. u32 leafsize;
  1079. u32 blocksize;
  1080. u32 stripesize;
  1081. struct buffer_head *bh;
  1082. struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
  1083. GFP_NOFS);
  1084. struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
  1085. GFP_NOFS);
  1086. struct btrfs_fs_info *fs_info = kzalloc(sizeof(*fs_info),
  1087. GFP_NOFS);
  1088. struct btrfs_root *chunk_root = kmalloc(sizeof(struct btrfs_root),
  1089. GFP_NOFS);
  1090. struct btrfs_root *dev_root = kmalloc(sizeof(struct btrfs_root),
  1091. GFP_NOFS);
  1092. int ret;
  1093. int err = -EINVAL;
  1094. struct btrfs_super_block *disk_super;
  1095. if (!extent_root || !tree_root || !fs_info) {
  1096. err = -ENOMEM;
  1097. goto fail;
  1098. }
  1099. INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
  1100. INIT_LIST_HEAD(&fs_info->trans_list);
  1101. INIT_LIST_HEAD(&fs_info->dead_roots);
  1102. INIT_LIST_HEAD(&fs_info->hashers);
  1103. spin_lock_init(&fs_info->hash_lock);
  1104. spin_lock_init(&fs_info->delalloc_lock);
  1105. spin_lock_init(&fs_info->new_trans_lock);
  1106. spin_lock_init(&fs_info->ref_cache_lock);
  1107. init_completion(&fs_info->kobj_unregister);
  1108. fs_info->tree_root = tree_root;
  1109. fs_info->extent_root = extent_root;
  1110. fs_info->chunk_root = chunk_root;
  1111. fs_info->dev_root = dev_root;
  1112. fs_info->fs_devices = fs_devices;
  1113. INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
  1114. INIT_LIST_HEAD(&fs_info->space_info);
  1115. btrfs_mapping_init(&fs_info->mapping_tree);
  1116. atomic_set(&fs_info->nr_async_submits, 0);
  1117. atomic_set(&fs_info->throttles, 0);
  1118. fs_info->sb = sb;
  1119. fs_info->max_extent = (u64)-1;
  1120. fs_info->max_inline = 8192 * 1024;
  1121. setup_bdi(fs_info, &fs_info->bdi);
  1122. fs_info->btree_inode = new_inode(sb);
  1123. fs_info->btree_inode->i_ino = 1;
  1124. fs_info->btree_inode->i_nlink = 1;
  1125. fs_info->thread_pool_size = min(num_online_cpus() + 2, 8);
  1126. INIT_LIST_HEAD(&fs_info->ordered_extents);
  1127. spin_lock_init(&fs_info->ordered_extent_lock);
  1128. sb->s_blocksize = 4096;
  1129. sb->s_blocksize_bits = blksize_bits(4096);
  1130. /*
  1131. * we set the i_size on the btree inode to the max possible int.
  1132. * the real end of the address space is determined by all of
  1133. * the devices in the system
  1134. */
  1135. fs_info->btree_inode->i_size = OFFSET_MAX;
  1136. fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
  1137. fs_info->btree_inode->i_mapping->backing_dev_info = &fs_info->bdi;
  1138. extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree,
  1139. fs_info->btree_inode->i_mapping,
  1140. GFP_NOFS);
  1141. extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree,
  1142. GFP_NOFS);
  1143. BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops;
  1144. extent_io_tree_init(&fs_info->free_space_cache,
  1145. fs_info->btree_inode->i_mapping, GFP_NOFS);
  1146. extent_io_tree_init(&fs_info->block_group_cache,
  1147. fs_info->btree_inode->i_mapping, GFP_NOFS);
  1148. extent_io_tree_init(&fs_info->pinned_extents,
  1149. fs_info->btree_inode->i_mapping, GFP_NOFS);
  1150. extent_io_tree_init(&fs_info->pending_del,
  1151. fs_info->btree_inode->i_mapping, GFP_NOFS);
  1152. extent_io_tree_init(&fs_info->extent_ins,
  1153. fs_info->btree_inode->i_mapping, GFP_NOFS);
  1154. fs_info->do_barriers = 1;
  1155. BTRFS_I(fs_info->btree_inode)->root = tree_root;
  1156. memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
  1157. sizeof(struct btrfs_key));
  1158. insert_inode_hash(fs_info->btree_inode);
  1159. mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
  1160. mutex_init(&fs_info->trans_mutex);
  1161. mutex_init(&fs_info->drop_mutex);
  1162. mutex_init(&fs_info->alloc_mutex);
  1163. mutex_init(&fs_info->chunk_mutex);
  1164. mutex_init(&fs_info->transaction_kthread_mutex);
  1165. mutex_init(&fs_info->cleaner_mutex);
  1166. mutex_init(&fs_info->volume_mutex);
  1167. init_waitqueue_head(&fs_info->transaction_throttle);
  1168. init_waitqueue_head(&fs_info->transaction_wait);
  1169. #if 0
  1170. ret = add_hasher(fs_info, "crc32c");
  1171. if (ret) {
  1172. printk("btrfs: failed hash setup, modprobe cryptomgr?\n");
  1173. err = -ENOMEM;
  1174. goto fail_iput;
  1175. }
  1176. #endif
  1177. __setup_root(4096, 4096, 4096, 4096, tree_root,
  1178. fs_info, BTRFS_ROOT_TREE_OBJECTID);
  1179. bh = __bread(fs_devices->latest_bdev,
  1180. BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
  1181. if (!bh)
  1182. goto fail_iput;
  1183. memcpy(&fs_info->super_copy, bh->b_data, sizeof(fs_info->super_copy));
  1184. brelse(bh);
  1185. memcpy(fs_info->fsid, fs_info->super_copy.fsid, BTRFS_FSID_SIZE);
  1186. disk_super = &fs_info->super_copy;
  1187. if (!btrfs_super_root(disk_super))
  1188. goto fail_sb_buffer;
  1189. err = btrfs_parse_options(tree_root, options);
  1190. if (err)
  1191. goto fail_sb_buffer;
  1192. /*
  1193. * we need to start all the end_io workers up front because the
  1194. * queue work function gets called at interrupt time, and so it
  1195. * cannot dynamically grow.
  1196. */
  1197. btrfs_init_workers(&fs_info->workers, fs_info->thread_pool_size);
  1198. btrfs_init_workers(&fs_info->submit_workers, fs_info->thread_pool_size);
  1199. btrfs_init_workers(&fs_info->fixup_workers, 1);
  1200. btrfs_init_workers(&fs_info->endio_workers, fs_info->thread_pool_size);
  1201. btrfs_init_workers(&fs_info->endio_write_workers,
  1202. fs_info->thread_pool_size);
  1203. btrfs_start_workers(&fs_info->workers, 1);
  1204. btrfs_start_workers(&fs_info->submit_workers, 1);
  1205. btrfs_start_workers(&fs_info->fixup_workers, 1);
  1206. btrfs_start_workers(&fs_info->endio_workers, fs_info->thread_pool_size);
  1207. btrfs_start_workers(&fs_info->endio_write_workers,
  1208. fs_info->thread_pool_size);
  1209. err = -EINVAL;
  1210. if (btrfs_super_num_devices(disk_super) > fs_devices->open_devices) {
  1211. printk("Btrfs: wanted %llu devices, but found %llu\n",
  1212. (unsigned long long)btrfs_super_num_devices(disk_super),
  1213. (unsigned long long)fs_devices->open_devices);
  1214. if (btrfs_test_opt(tree_root, DEGRADED))
  1215. printk("continuing in degraded mode\n");
  1216. else {
  1217. goto fail_sb_buffer;
  1218. }
  1219. }
  1220. fs_info->bdi.ra_pages *= btrfs_super_num_devices(disk_super);
  1221. nodesize = btrfs_super_nodesize(disk_super);
  1222. leafsize = btrfs_super_leafsize(disk_super);
  1223. sectorsize = btrfs_super_sectorsize(disk_super);
  1224. stripesize = btrfs_super_stripesize(disk_super);
  1225. tree_root->nodesize = nodesize;
  1226. tree_root->leafsize = leafsize;
  1227. tree_root->sectorsize = sectorsize;
  1228. tree_root->stripesize = stripesize;
  1229. sb->s_blocksize = sectorsize;
  1230. sb->s_blocksize_bits = blksize_bits(sectorsize);
  1231. if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
  1232. sizeof(disk_super->magic))) {
  1233. printk("btrfs: valid FS not found on %s\n", sb->s_id);
  1234. goto fail_sb_buffer;
  1235. }
  1236. mutex_lock(&fs_info->chunk_mutex);
  1237. ret = btrfs_read_sys_array(tree_root);
  1238. mutex_unlock(&fs_info->chunk_mutex);
  1239. if (ret) {
  1240. printk("btrfs: failed to read the system array on %s\n",
  1241. sb->s_id);
  1242. goto fail_sys_array;
  1243. }
  1244. blocksize = btrfs_level_size(tree_root,
  1245. btrfs_super_chunk_root_level(disk_super));
  1246. __setup_root(nodesize, leafsize, sectorsize, stripesize,
  1247. chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
  1248. chunk_root->node = read_tree_block(chunk_root,
  1249. btrfs_super_chunk_root(disk_super),
  1250. blocksize, 0);
  1251. BUG_ON(!chunk_root->node);
  1252. read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid,
  1253. (unsigned long)btrfs_header_chunk_tree_uuid(chunk_root->node),
  1254. BTRFS_UUID_SIZE);
  1255. mutex_lock(&fs_info->chunk_mutex);
  1256. ret = btrfs_read_chunk_tree(chunk_root);
  1257. mutex_unlock(&fs_info->chunk_mutex);
  1258. BUG_ON(ret);
  1259. btrfs_close_extra_devices(fs_devices);
  1260. blocksize = btrfs_level_size(tree_root,
  1261. btrfs_super_root_level(disk_super));
  1262. tree_root->node = read_tree_block(tree_root,
  1263. btrfs_super_root(disk_super),
  1264. blocksize, 0);
  1265. if (!tree_root->node)
  1266. goto fail_sb_buffer;
  1267. ret = find_and_setup_root(tree_root, fs_info,
  1268. BTRFS_EXTENT_TREE_OBJECTID, extent_root);
  1269. if (ret)
  1270. goto fail_tree_root;
  1271. extent_root->track_dirty = 1;
  1272. ret = find_and_setup_root(tree_root, fs_info,
  1273. BTRFS_DEV_TREE_OBJECTID, dev_root);
  1274. dev_root->track_dirty = 1;
  1275. if (ret)
  1276. goto fail_extent_root;
  1277. btrfs_read_block_groups(extent_root);
  1278. fs_info->generation = btrfs_super_generation(disk_super) + 1;
  1279. fs_info->data_alloc_profile = (u64)-1;
  1280. fs_info->metadata_alloc_profile = (u64)-1;
  1281. fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
  1282. fs_info->cleaner_kthread = kthread_run(cleaner_kthread, tree_root,
  1283. "btrfs-cleaner");
  1284. if (!fs_info->cleaner_kthread)
  1285. goto fail_extent_root;
  1286. fs_info->transaction_kthread = kthread_run(transaction_kthread,
  1287. tree_root,
  1288. "btrfs-transaction");
  1289. if (!fs_info->transaction_kthread)
  1290. goto fail_cleaner;
  1291. return tree_root;
  1292. fail_cleaner:
  1293. kthread_stop(fs_info->cleaner_kthread);
  1294. fail_extent_root:
  1295. free_extent_buffer(extent_root->node);
  1296. fail_tree_root:
  1297. free_extent_buffer(tree_root->node);
  1298. fail_sys_array:
  1299. fail_sb_buffer:
  1300. btrfs_stop_workers(&fs_info->fixup_workers);
  1301. btrfs_stop_workers(&fs_info->workers);
  1302. btrfs_stop_workers(&fs_info->endio_workers);
  1303. btrfs_stop_workers(&fs_info->endio_write_workers);
  1304. btrfs_stop_workers(&fs_info->submit_workers);
  1305. fail_iput:
  1306. iput(fs_info->btree_inode);
  1307. fail:
  1308. btrfs_close_devices(fs_info->fs_devices);
  1309. btrfs_mapping_tree_free(&fs_info->mapping_tree);
  1310. kfree(extent_root);
  1311. kfree(tree_root);
  1312. #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
  1313. bdi_destroy(&fs_info->bdi);
  1314. #endif
  1315. kfree(fs_info);
  1316. return ERR_PTR(err);
  1317. }
  1318. static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate)
  1319. {
  1320. char b[BDEVNAME_SIZE];
  1321. if (uptodate) {
  1322. set_buffer_uptodate(bh);
  1323. } else {
  1324. if (!buffer_eopnotsupp(bh) && printk_ratelimit()) {
  1325. printk(KERN_WARNING "lost page write due to "
  1326. "I/O error on %s\n",
  1327. bdevname(bh->b_bdev, b));
  1328. }
  1329. /* note, we dont' set_buffer_write_io_error because we have
  1330. * our own ways of dealing with the IO errors
  1331. */
  1332. clear_buffer_uptodate(bh);
  1333. }
  1334. unlock_buffer(bh);
  1335. put_bh(bh);
  1336. }
  1337. int write_all_supers(struct btrfs_root *root)
  1338. {
  1339. struct list_head *cur;
  1340. struct list_head *head = &root->fs_info->fs_devices->devices;
  1341. struct btrfs_device *dev;
  1342. struct btrfs_super_block *sb;
  1343. struct btrfs_dev_item *dev_item;
  1344. struct buffer_head *bh;
  1345. int ret;
  1346. int do_barriers;
  1347. int max_errors;
  1348. int total_errors = 0;
  1349. u32 crc;
  1350. u64 flags;
  1351. max_errors = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
  1352. do_barriers = !btrfs_test_opt(root, NOBARRIER);
  1353. sb = &root->fs_info->super_for_commit;
  1354. dev_item = &sb->dev_item;
  1355. list_for_each(cur, head) {
  1356. dev = list_entry(cur, struct btrfs_device, dev_list);
  1357. if (!dev->bdev) {
  1358. total_errors++;
  1359. continue;
  1360. }
  1361. if (!dev->in_fs_metadata)
  1362. continue;
  1363. btrfs_set_stack_device_type(dev_item, dev->type);
  1364. btrfs_set_stack_device_id(dev_item, dev->devid);
  1365. btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes);
  1366. btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used);
  1367. btrfs_set_stack_device_io_align(dev_item, dev->io_align);
  1368. btrfs_set_stack_device_io_width(dev_item, dev->io_width);
  1369. btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
  1370. memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
  1371. flags = btrfs_super_flags(sb);
  1372. btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
  1373. crc = ~(u32)0;
  1374. crc = btrfs_csum_data(root, (char *)sb + BTRFS_CSUM_SIZE, crc,
  1375. BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
  1376. btrfs_csum_final(crc, sb->csum);
  1377. bh = __getblk(dev->bdev, BTRFS_SUPER_INFO_OFFSET / 4096,
  1378. BTRFS_SUPER_INFO_SIZE);
  1379. memcpy(bh->b_data, sb, BTRFS_SUPER_INFO_SIZE);
  1380. dev->pending_io = bh;
  1381. get_bh(bh);
  1382. set_buffer_uptodate(bh);
  1383. lock_buffer(bh);
  1384. bh->b_end_io = btrfs_end_buffer_write_sync;
  1385. if (do_barriers && dev->barriers) {
  1386. ret = submit_bh(WRITE_BARRIER, bh);
  1387. if (ret == -EOPNOTSUPP) {
  1388. printk("btrfs: disabling barriers on dev %s\n",
  1389. dev->name);
  1390. set_buffer_uptodate(bh);
  1391. dev->barriers = 0;
  1392. get_bh(bh);
  1393. lock_buffer(bh);
  1394. ret = submit_bh(WRITE, bh);
  1395. }
  1396. } else {
  1397. ret = submit_bh(WRITE, bh);
  1398. }
  1399. if (ret)
  1400. total_errors++;
  1401. }
  1402. if (total_errors > max_errors) {
  1403. printk("btrfs: %d errors while writing supers\n", total_errors);
  1404. BUG();
  1405. }
  1406. total_errors = 0;
  1407. list_for_each(cur, head) {
  1408. dev = list_entry(cur, struct btrfs_device, dev_list);
  1409. if (!dev->bdev)
  1410. continue;
  1411. if (!dev->in_fs_metadata)
  1412. continue;
  1413. BUG_ON(!dev->pending_io);
  1414. bh = dev->pending_io;
  1415. wait_on_buffer(bh);
  1416. if (!buffer_uptodate(dev->pending_io)) {
  1417. if (do_barriers && dev->barriers) {
  1418. printk("btrfs: disabling barriers on dev %s\n",
  1419. dev->name);
  1420. set_buffer_uptodate(bh);
  1421. get_bh(bh);
  1422. lock_buffer(bh);
  1423. dev->barriers = 0;
  1424. ret = submit_bh(WRITE, bh);
  1425. BUG_ON(ret);
  1426. wait_on_buffer(bh);
  1427. if (!buffer_uptodate(bh))
  1428. total_errors++;
  1429. } else {
  1430. total_errors++;
  1431. }
  1432. }
  1433. dev->pending_io = NULL;
  1434. brelse(bh);
  1435. }
  1436. if (total_errors > max_errors) {
  1437. printk("btrfs: %d errors while writing supers\n", total_errors);
  1438. BUG();
  1439. }
  1440. return 0;
  1441. }
  1442. int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
  1443. *root)
  1444. {
  1445. int ret;
  1446. ret = write_all_supers(root);
  1447. return ret;
  1448. }
  1449. int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
  1450. {
  1451. radix_tree_delete(&fs_info->fs_roots_radix,
  1452. (unsigned long)root->root_key.objectid);
  1453. if (root->in_sysfs)
  1454. btrfs_sysfs_del_root(root);
  1455. if (root->inode)
  1456. iput(root->inode);
  1457. if (root->node)
  1458. free_extent_buffer(root->node);
  1459. if (root->commit_root)
  1460. free_extent_buffer(root->commit_root);
  1461. if (root->name)
  1462. kfree(root->name);
  1463. kfree(root);
  1464. return 0;
  1465. }
  1466. static int del_fs_roots(struct btrfs_fs_info *fs_info)
  1467. {
  1468. int ret;
  1469. struct btrfs_root *gang[8];
  1470. int i;
  1471. while(1) {
  1472. ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
  1473. (void **)gang, 0,
  1474. ARRAY_SIZE(gang));
  1475. if (!ret)
  1476. break;
  1477. for (i = 0; i < ret; i++)
  1478. btrfs_free_fs_root(fs_info, gang[i]);
  1479. }
  1480. return 0;
  1481. }
  1482. int close_ctree(struct btrfs_root *root)
  1483. {
  1484. int ret;
  1485. struct btrfs_trans_handle *trans;
  1486. struct btrfs_fs_info *fs_info = root->fs_info;
  1487. fs_info->closing = 1;
  1488. smp_mb();
  1489. kthread_stop(root->fs_info->transaction_kthread);
  1490. kthread_stop(root->fs_info->cleaner_kthread);
  1491. btrfs_clean_old_snapshots(root);
  1492. trans = btrfs_start_transaction(root, 1);
  1493. ret = btrfs_commit_transaction(trans, root);
  1494. /* run commit again to drop the original snapshot */
  1495. trans = btrfs_start_transaction(root, 1);
  1496. btrfs_commit_transaction(trans, root);
  1497. ret = btrfs_write_and_wait_transaction(NULL, root);
  1498. BUG_ON(ret);
  1499. write_ctree_super(NULL, root);
  1500. if (fs_info->delalloc_bytes) {
  1501. printk("btrfs: at unmount delalloc count %Lu\n",
  1502. fs_info->delalloc_bytes);
  1503. }
  1504. if (fs_info->total_ref_cache_size) {
  1505. printk("btrfs: at umount reference cache size %Lu\n",
  1506. fs_info->total_ref_cache_size);
  1507. }
  1508. if (fs_info->extent_root->node)
  1509. free_extent_buffer(fs_info->extent_root->node);
  1510. if (fs_info->tree_root->node)
  1511. free_extent_buffer(fs_info->tree_root->node);
  1512. if (root->fs_info->chunk_root->node);
  1513. free_extent_buffer(root->fs_info->chunk_root->node);
  1514. if (root->fs_info->dev_root->node);
  1515. free_extent_buffer(root->fs_info->dev_root->node);
  1516. btrfs_free_block_groups(root->fs_info);
  1517. del_fs_roots(fs_info);
  1518. filemap_write_and_wait(fs_info->btree_inode->i_mapping);
  1519. truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
  1520. btrfs_stop_workers(&fs_info->fixup_workers);
  1521. btrfs_stop_workers(&fs_info->workers);
  1522. btrfs_stop_workers(&fs_info->endio_workers);
  1523. btrfs_stop_workers(&fs_info->endio_write_workers);
  1524. btrfs_stop_workers(&fs_info->submit_workers);
  1525. iput(fs_info->btree_inode);
  1526. #if 0
  1527. while(!list_empty(&fs_info->hashers)) {
  1528. struct btrfs_hasher *hasher;
  1529. hasher = list_entry(fs_info->hashers.next, struct btrfs_hasher,
  1530. hashers);
  1531. list_del(&hasher->hashers);
  1532. crypto_free_hash(&fs_info->hash_tfm);
  1533. kfree(hasher);
  1534. }
  1535. #endif
  1536. btrfs_close_devices(fs_info->fs_devices);
  1537. btrfs_mapping_tree_free(&fs_info->mapping_tree);
  1538. #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
  1539. bdi_destroy(&fs_info->bdi);
  1540. #endif
  1541. kfree(fs_info->extent_root);
  1542. kfree(fs_info->tree_root);
  1543. kfree(fs_info->chunk_root);
  1544. kfree(fs_info->dev_root);
  1545. return 0;
  1546. }
  1547. int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
  1548. {
  1549. int ret;
  1550. struct inode *btree_inode = buf->first_page->mapping->host;
  1551. ret = extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, buf);
  1552. if (!ret)
  1553. return ret;
  1554. ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf,
  1555. parent_transid);
  1556. return !ret;
  1557. }
  1558. int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
  1559. {
  1560. struct inode *btree_inode = buf->first_page->mapping->host;
  1561. return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree,
  1562. buf);
  1563. }
  1564. void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
  1565. {
  1566. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  1567. u64 transid = btrfs_header_generation(buf);
  1568. struct inode *btree_inode = root->fs_info->btree_inode;
  1569. WARN_ON(!btrfs_tree_locked(buf));
  1570. if (transid != root->fs_info->generation) {
  1571. printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n",
  1572. (unsigned long long)buf->start,
  1573. transid, root->fs_info->generation);
  1574. WARN_ON(1);
  1575. }
  1576. set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, buf);
  1577. }
  1578. void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
  1579. {
  1580. /*
  1581. * looks as though older kernels can get into trouble with
  1582. * this code, they end up stuck in balance_dirty_pages forever
  1583. */
  1584. struct extent_io_tree *tree;
  1585. u64 num_dirty;
  1586. u64 start = 0;
  1587. unsigned long thresh = 16 * 1024 * 1024;
  1588. tree = &BTRFS_I(root->fs_info->btree_inode)->io_tree;
  1589. if (current_is_pdflush())
  1590. return;
  1591. num_dirty = count_range_bits(tree, &start, (u64)-1,
  1592. thresh, EXTENT_DIRTY);
  1593. if (num_dirty > thresh) {
  1594. balance_dirty_pages_ratelimited_nr(
  1595. root->fs_info->btree_inode->i_mapping, 1);
  1596. }
  1597. return;
  1598. }
  1599. int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid)
  1600. {
  1601. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  1602. int ret;
  1603. ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid);
  1604. if (ret == 0) {
  1605. buf->flags |= EXTENT_UPTODATE;
  1606. }
  1607. return ret;
  1608. }
  1609. static struct extent_io_ops btree_extent_io_ops = {
  1610. .writepage_io_hook = btree_writepage_io_hook,
  1611. .readpage_end_io_hook = btree_readpage_end_io_hook,
  1612. .submit_bio_hook = btree_submit_bio_hook,
  1613. /* note we're sharing with inode.c for the merge bio hook */
  1614. .merge_bio_hook = btrfs_merge_bio_hook,
  1615. };