disk-io.c 44 KB

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