disk-io.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  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/crc32c.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 "ctree.h"
  27. #include "disk-io.h"
  28. #include "transaction.h"
  29. #include "btrfs_inode.h"
  30. #include "volumes.h"
  31. #include "print-tree.h"
  32. #if 0
  33. static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
  34. {
  35. if (extent_buffer_blocknr(buf) != btrfs_header_blocknr(buf)) {
  36. printk(KERN_CRIT "buf blocknr(buf) is %llu, header is %llu\n",
  37. (unsigned long long)extent_buffer_blocknr(buf),
  38. (unsigned long long)btrfs_header_blocknr(buf));
  39. return 1;
  40. }
  41. return 0;
  42. }
  43. #endif
  44. static struct extent_io_ops btree_extent_io_ops;
  45. struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
  46. u64 bytenr, u32 blocksize)
  47. {
  48. struct inode *btree_inode = root->fs_info->btree_inode;
  49. struct extent_buffer *eb;
  50. eb = find_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
  51. bytenr, blocksize, GFP_NOFS);
  52. return eb;
  53. }
  54. struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
  55. u64 bytenr, u32 blocksize)
  56. {
  57. struct inode *btree_inode = root->fs_info->btree_inode;
  58. struct extent_buffer *eb;
  59. eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
  60. bytenr, blocksize, NULL, GFP_NOFS);
  61. return eb;
  62. }
  63. struct extent_map *btree_get_extent(struct inode *inode, struct page *page,
  64. size_t page_offset, u64 start, u64 len,
  65. int create)
  66. {
  67. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  68. struct extent_map *em;
  69. int ret;
  70. again:
  71. spin_lock(&em_tree->lock);
  72. em = lookup_extent_mapping(em_tree, start, len);
  73. spin_unlock(&em_tree->lock);
  74. if (em) {
  75. goto out;
  76. }
  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 = i_size_read(inode);
  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. spin_unlock(&em_tree->lock);
  89. if (ret == -EEXIST) {
  90. free_extent_map(em);
  91. em = NULL;
  92. goto again;
  93. } else if (ret) {
  94. em = ERR_PTR(ret);
  95. }
  96. out:
  97. return em;
  98. }
  99. u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
  100. {
  101. return crc32c(seed, data, len);
  102. }
  103. void btrfs_csum_final(u32 crc, char *result)
  104. {
  105. *(__le32 *)result = ~cpu_to_le32(crc);
  106. }
  107. static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
  108. int verify)
  109. {
  110. char result[BTRFS_CRC32_SIZE];
  111. unsigned long len;
  112. unsigned long cur_len;
  113. unsigned long offset = BTRFS_CSUM_SIZE;
  114. char *map_token = NULL;
  115. char *kaddr;
  116. unsigned long map_start;
  117. unsigned long map_len;
  118. int err;
  119. u32 crc = ~(u32)0;
  120. len = buf->len - offset;
  121. while(len > 0) {
  122. err = map_private_extent_buffer(buf, offset, 32,
  123. &map_token, &kaddr,
  124. &map_start, &map_len, KM_USER0);
  125. if (err) {
  126. printk("failed to map extent buffer! %lu\n",
  127. offset);
  128. return 1;
  129. }
  130. cur_len = min(len, map_len - (offset - map_start));
  131. crc = btrfs_csum_data(root, kaddr + offset - map_start,
  132. crc, cur_len);
  133. len -= cur_len;
  134. offset += cur_len;
  135. unmap_extent_buffer(buf, map_token, KM_USER0);
  136. }
  137. btrfs_csum_final(crc, result);
  138. if (verify) {
  139. int from_this_trans = 0;
  140. if (root->fs_info->running_transaction &&
  141. btrfs_header_generation(buf) ==
  142. root->fs_info->running_transaction->transid)
  143. from_this_trans = 1;
  144. /* FIXME, this is not good */
  145. if (from_this_trans == 0 &&
  146. memcmp_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE)) {
  147. u32 val;
  148. u32 found = 0;
  149. memcpy(&found, result, BTRFS_CRC32_SIZE);
  150. read_extent_buffer(buf, &val, 0, BTRFS_CRC32_SIZE);
  151. printk("btrfs: %s checksum verify failed on %llu "
  152. "wanted %X found %X from_this_trans %d\n",
  153. root->fs_info->sb->s_id,
  154. buf->start, val, found, from_this_trans);
  155. return 1;
  156. }
  157. } else {
  158. write_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE);
  159. }
  160. return 0;
  161. }
  162. int csum_dirty_buffer(struct btrfs_root *root, struct page *page)
  163. {
  164. struct extent_io_tree *tree;
  165. u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
  166. u64 found_start;
  167. int found_level;
  168. unsigned long len;
  169. struct extent_buffer *eb;
  170. tree = &BTRFS_I(page->mapping->host)->io_tree;
  171. if (page->private == EXTENT_PAGE_PRIVATE)
  172. goto out;
  173. if (!page->private)
  174. goto out;
  175. len = page->private >> 2;
  176. if (len == 0) {
  177. WARN_ON(1);
  178. }
  179. eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS);
  180. read_extent_buffer_pages(tree, eb, start + PAGE_CACHE_SIZE, 1,
  181. btree_get_extent);
  182. btrfs_clear_buffer_defrag(eb);
  183. found_start = btrfs_header_bytenr(eb);
  184. if (found_start != start) {
  185. printk("warning: eb start incorrect %Lu buffer %Lu len %lu\n",
  186. start, found_start, len);
  187. WARN_ON(1);
  188. goto err;
  189. }
  190. if (eb->first_page != page) {
  191. printk("bad first page %lu %lu\n", eb->first_page->index,
  192. page->index);
  193. WARN_ON(1);
  194. goto err;
  195. }
  196. if (!PageUptodate(page)) {
  197. printk("csum not up to date page %lu\n", page->index);
  198. WARN_ON(1);
  199. goto err;
  200. }
  201. found_level = btrfs_header_level(eb);
  202. csum_tree_block(root, eb, 0);
  203. err:
  204. free_extent_buffer(eb);
  205. out:
  206. return 0;
  207. }
  208. static int btree_writepage_io_hook(struct page *page, u64 start, u64 end)
  209. {
  210. struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
  211. csum_dirty_buffer(root, page);
  212. return 0;
  213. }
  214. static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio)
  215. {
  216. struct btrfs_root *root = BTRFS_I(inode)->root;
  217. u64 offset;
  218. offset = bio->bi_sector << 9;
  219. if (offset == BTRFS_SUPER_INFO_OFFSET) {
  220. bio->bi_bdev = root->fs_info->sb->s_bdev;
  221. submit_bio(rw, bio);
  222. return 0;
  223. }
  224. return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio);
  225. }
  226. static int btree_writepage(struct page *page, struct writeback_control *wbc)
  227. {
  228. struct extent_io_tree *tree;
  229. tree = &BTRFS_I(page->mapping->host)->io_tree;
  230. return extent_write_full_page(tree, page, btree_get_extent, wbc);
  231. }
  232. static int btree_writepages(struct address_space *mapping,
  233. struct writeback_control *wbc)
  234. {
  235. struct extent_io_tree *tree;
  236. tree = &BTRFS_I(mapping->host)->io_tree;
  237. if (wbc->sync_mode == WB_SYNC_NONE) {
  238. u64 num_dirty;
  239. u64 start = 0;
  240. unsigned long thresh = 96 * 1024 * 1024;
  241. if (wbc->for_kupdate)
  242. return 0;
  243. if (current_is_pdflush()) {
  244. thresh = 96 * 1024 * 1024;
  245. } else {
  246. thresh = 8 * 1024 * 1024;
  247. }
  248. num_dirty = count_range_bits(tree, &start, (u64)-1,
  249. thresh, EXTENT_DIRTY);
  250. if (num_dirty < thresh) {
  251. return 0;
  252. }
  253. }
  254. return extent_writepages(tree, mapping, btree_get_extent, wbc);
  255. }
  256. int btree_readpage(struct file *file, struct page *page)
  257. {
  258. struct extent_io_tree *tree;
  259. tree = &BTRFS_I(page->mapping->host)->io_tree;
  260. return extent_read_full_page(tree, page, btree_get_extent);
  261. }
  262. static int btree_releasepage(struct page *page, gfp_t gfp_flags)
  263. {
  264. struct extent_io_tree *tree;
  265. struct extent_map_tree *map;
  266. int ret;
  267. tree = &BTRFS_I(page->mapping->host)->io_tree;
  268. map = &BTRFS_I(page->mapping->host)->extent_tree;
  269. ret = try_release_extent_mapping(map, tree, page, gfp_flags);
  270. if (ret == 1) {
  271. ClearPagePrivate(page);
  272. set_page_private(page, 0);
  273. page_cache_release(page);
  274. }
  275. return ret;
  276. }
  277. static void btree_invalidatepage(struct page *page, unsigned long offset)
  278. {
  279. struct extent_io_tree *tree;
  280. tree = &BTRFS_I(page->mapping->host)->io_tree;
  281. extent_invalidatepage(tree, page, offset);
  282. btree_releasepage(page, GFP_NOFS);
  283. }
  284. #if 0
  285. static int btree_writepage(struct page *page, struct writeback_control *wbc)
  286. {
  287. struct buffer_head *bh;
  288. struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
  289. struct buffer_head *head;
  290. if (!page_has_buffers(page)) {
  291. create_empty_buffers(page, root->fs_info->sb->s_blocksize,
  292. (1 << BH_Dirty)|(1 << BH_Uptodate));
  293. }
  294. head = page_buffers(page);
  295. bh = head;
  296. do {
  297. if (buffer_dirty(bh))
  298. csum_tree_block(root, bh, 0);
  299. bh = bh->b_this_page;
  300. } while (bh != head);
  301. return block_write_full_page(page, btree_get_block, wbc);
  302. }
  303. #endif
  304. static struct address_space_operations btree_aops = {
  305. .readpage = btree_readpage,
  306. .writepage = btree_writepage,
  307. .writepages = btree_writepages,
  308. .releasepage = btree_releasepage,
  309. .invalidatepage = btree_invalidatepage,
  310. .sync_page = block_sync_page,
  311. };
  312. int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize)
  313. {
  314. struct extent_buffer *buf = NULL;
  315. struct inode *btree_inode = root->fs_info->btree_inode;
  316. int ret = 0;
  317. buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
  318. if (!buf)
  319. return 0;
  320. read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
  321. buf, 0, 0, btree_get_extent);
  322. free_extent_buffer(buf);
  323. return ret;
  324. }
  325. static int close_all_devices(struct btrfs_fs_info *fs_info)
  326. {
  327. struct list_head *list;
  328. struct list_head *next;
  329. struct btrfs_device *device;
  330. list = &fs_info->fs_devices->devices;
  331. list_for_each(next, list) {
  332. device = list_entry(next, struct btrfs_device, dev_list);
  333. if (device->bdev && device->bdev != fs_info->sb->s_bdev)
  334. close_bdev_excl(device->bdev);
  335. device->bdev = NULL;
  336. }
  337. return 0;
  338. }
  339. struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
  340. u32 blocksize)
  341. {
  342. struct extent_buffer *buf = NULL;
  343. struct inode *btree_inode = root->fs_info->btree_inode;
  344. struct extent_io_tree *io_tree;
  345. u64 end;
  346. int ret;
  347. io_tree = &BTRFS_I(btree_inode)->io_tree;
  348. buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
  349. if (!buf)
  350. return NULL;
  351. read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree, buf, 0, 1,
  352. btree_get_extent);
  353. if (buf->flags & EXTENT_CSUM)
  354. return buf;
  355. end = buf->start + PAGE_CACHE_SIZE - 1;
  356. if (test_range_bit(io_tree, buf->start, end, EXTENT_CSUM, 1)) {
  357. buf->flags |= EXTENT_CSUM;
  358. return buf;
  359. }
  360. lock_extent(io_tree, buf->start, end, GFP_NOFS);
  361. if (test_range_bit(io_tree, buf->start, end, EXTENT_CSUM, 1)) {
  362. buf->flags |= EXTENT_CSUM;
  363. goto out_unlock;
  364. }
  365. ret = csum_tree_block(root, buf, 1);
  366. set_extent_bits(io_tree, buf->start, end, EXTENT_CSUM, GFP_NOFS);
  367. buf->flags |= EXTENT_CSUM;
  368. out_unlock:
  369. unlock_extent(io_tree, buf->start, end, GFP_NOFS);
  370. return buf;
  371. }
  372. int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  373. struct extent_buffer *buf)
  374. {
  375. struct inode *btree_inode = root->fs_info->btree_inode;
  376. if (btrfs_header_generation(buf) ==
  377. root->fs_info->running_transaction->transid)
  378. clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree,
  379. buf);
  380. return 0;
  381. }
  382. int wait_on_tree_block_writeback(struct btrfs_root *root,
  383. struct extent_buffer *buf)
  384. {
  385. struct inode *btree_inode = root->fs_info->btree_inode;
  386. wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->io_tree,
  387. buf);
  388. return 0;
  389. }
  390. static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
  391. u32 stripesize, struct btrfs_root *root,
  392. struct btrfs_fs_info *fs_info,
  393. u64 objectid)
  394. {
  395. root->node = NULL;
  396. root->inode = NULL;
  397. root->commit_root = NULL;
  398. root->sectorsize = sectorsize;
  399. root->nodesize = nodesize;
  400. root->leafsize = leafsize;
  401. root->stripesize = stripesize;
  402. root->ref_cows = 0;
  403. root->track_dirty = 0;
  404. root->fs_info = fs_info;
  405. root->objectid = objectid;
  406. root->last_trans = 0;
  407. root->highest_inode = 0;
  408. root->last_inode_alloc = 0;
  409. root->name = NULL;
  410. root->in_sysfs = 0;
  411. INIT_LIST_HEAD(&root->dirty_list);
  412. memset(&root->root_key, 0, sizeof(root->root_key));
  413. memset(&root->root_item, 0, sizeof(root->root_item));
  414. memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
  415. memset(&root->root_kobj, 0, sizeof(root->root_kobj));
  416. init_completion(&root->kobj_unregister);
  417. root->defrag_running = 0;
  418. root->defrag_level = 0;
  419. root->root_key.objectid = objectid;
  420. return 0;
  421. }
  422. static int find_and_setup_root(struct btrfs_root *tree_root,
  423. struct btrfs_fs_info *fs_info,
  424. u64 objectid,
  425. struct btrfs_root *root)
  426. {
  427. int ret;
  428. u32 blocksize;
  429. __setup_root(tree_root->nodesize, tree_root->leafsize,
  430. tree_root->sectorsize, tree_root->stripesize,
  431. root, fs_info, objectid);
  432. ret = btrfs_find_last_root(tree_root, objectid,
  433. &root->root_item, &root->root_key);
  434. BUG_ON(ret);
  435. blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
  436. root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
  437. blocksize);
  438. BUG_ON(!root->node);
  439. return 0;
  440. }
  441. struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
  442. struct btrfs_key *location)
  443. {
  444. struct btrfs_root *root;
  445. struct btrfs_root *tree_root = fs_info->tree_root;
  446. struct btrfs_path *path;
  447. struct extent_buffer *l;
  448. u64 highest_inode;
  449. u32 blocksize;
  450. int ret = 0;
  451. root = kzalloc(sizeof(*root), GFP_NOFS);
  452. if (!root)
  453. return ERR_PTR(-ENOMEM);
  454. if (location->offset == (u64)-1) {
  455. ret = find_and_setup_root(tree_root, fs_info,
  456. location->objectid, root);
  457. if (ret) {
  458. kfree(root);
  459. return ERR_PTR(ret);
  460. }
  461. goto insert;
  462. }
  463. __setup_root(tree_root->nodesize, tree_root->leafsize,
  464. tree_root->sectorsize, tree_root->stripesize,
  465. root, fs_info, location->objectid);
  466. path = btrfs_alloc_path();
  467. BUG_ON(!path);
  468. ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
  469. if (ret != 0) {
  470. if (ret > 0)
  471. ret = -ENOENT;
  472. goto out;
  473. }
  474. l = path->nodes[0];
  475. read_extent_buffer(l, &root->root_item,
  476. btrfs_item_ptr_offset(l, path->slots[0]),
  477. sizeof(root->root_item));
  478. memcpy(&root->root_key, location, sizeof(*location));
  479. ret = 0;
  480. out:
  481. btrfs_release_path(root, path);
  482. btrfs_free_path(path);
  483. if (ret) {
  484. kfree(root);
  485. return ERR_PTR(ret);
  486. }
  487. blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
  488. root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
  489. blocksize);
  490. BUG_ON(!root->node);
  491. insert:
  492. root->ref_cows = 1;
  493. ret = btrfs_find_highest_inode(root, &highest_inode);
  494. if (ret == 0) {
  495. root->highest_inode = highest_inode;
  496. root->last_inode_alloc = highest_inode;
  497. }
  498. return root;
  499. }
  500. struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info,
  501. u64 root_objectid)
  502. {
  503. struct btrfs_root *root;
  504. if (root_objectid == BTRFS_ROOT_TREE_OBJECTID)
  505. return fs_info->tree_root;
  506. if (root_objectid == BTRFS_EXTENT_TREE_OBJECTID)
  507. return fs_info->extent_root;
  508. root = radix_tree_lookup(&fs_info->fs_roots_radix,
  509. (unsigned long)root_objectid);
  510. return root;
  511. }
  512. struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info,
  513. struct btrfs_key *location)
  514. {
  515. struct btrfs_root *root;
  516. int ret;
  517. if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
  518. return fs_info->tree_root;
  519. if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
  520. return fs_info->extent_root;
  521. root = radix_tree_lookup(&fs_info->fs_roots_radix,
  522. (unsigned long)location->objectid);
  523. if (root)
  524. return root;
  525. root = btrfs_read_fs_root_no_radix(fs_info, location);
  526. if (IS_ERR(root))
  527. return root;
  528. ret = radix_tree_insert(&fs_info->fs_roots_radix,
  529. (unsigned long)root->root_key.objectid,
  530. root);
  531. if (ret) {
  532. free_extent_buffer(root->node);
  533. kfree(root);
  534. return ERR_PTR(ret);
  535. }
  536. ret = btrfs_find_dead_roots(fs_info->tree_root,
  537. root->root_key.objectid, root);
  538. BUG_ON(ret);
  539. return root;
  540. }
  541. struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
  542. struct btrfs_key *location,
  543. const char *name, int namelen)
  544. {
  545. struct btrfs_root *root;
  546. int ret;
  547. root = btrfs_read_fs_root_no_name(fs_info, location);
  548. if (!root)
  549. return NULL;
  550. if (root->in_sysfs)
  551. return root;
  552. ret = btrfs_set_root_name(root, name, namelen);
  553. if (ret) {
  554. free_extent_buffer(root->node);
  555. kfree(root);
  556. return ERR_PTR(ret);
  557. }
  558. ret = btrfs_sysfs_add_root(root);
  559. if (ret) {
  560. free_extent_buffer(root->node);
  561. kfree(root->name);
  562. kfree(root);
  563. return ERR_PTR(ret);
  564. }
  565. root->in_sysfs = 1;
  566. return root;
  567. }
  568. #if 0
  569. static int add_hasher(struct btrfs_fs_info *info, char *type) {
  570. struct btrfs_hasher *hasher;
  571. hasher = kmalloc(sizeof(*hasher), GFP_NOFS);
  572. if (!hasher)
  573. return -ENOMEM;
  574. hasher->hash_tfm = crypto_alloc_hash(type, 0, CRYPTO_ALG_ASYNC);
  575. if (!hasher->hash_tfm) {
  576. kfree(hasher);
  577. return -EINVAL;
  578. }
  579. spin_lock(&info->hash_lock);
  580. list_add(&hasher->list, &info->hashers);
  581. spin_unlock(&info->hash_lock);
  582. return 0;
  583. }
  584. #endif
  585. struct btrfs_root *open_ctree(struct super_block *sb,
  586. struct btrfs_fs_devices *fs_devices)
  587. {
  588. u32 sectorsize;
  589. u32 nodesize;
  590. u32 leafsize;
  591. u32 blocksize;
  592. u32 stripesize;
  593. struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
  594. GFP_NOFS);
  595. struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
  596. GFP_NOFS);
  597. struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
  598. GFP_NOFS);
  599. struct btrfs_root *chunk_root = kmalloc(sizeof(struct btrfs_root),
  600. GFP_NOFS);
  601. struct btrfs_root *dev_root = kmalloc(sizeof(struct btrfs_root),
  602. GFP_NOFS);
  603. int ret;
  604. int err = -EIO;
  605. struct btrfs_super_block *disk_super;
  606. if (!extent_root || !tree_root || !fs_info) {
  607. err = -ENOMEM;
  608. goto fail;
  609. }
  610. INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
  611. INIT_LIST_HEAD(&fs_info->trans_list);
  612. INIT_LIST_HEAD(&fs_info->dead_roots);
  613. INIT_LIST_HEAD(&fs_info->hashers);
  614. spin_lock_init(&fs_info->hash_lock);
  615. spin_lock_init(&fs_info->delalloc_lock);
  616. spin_lock_init(&fs_info->new_trans_lock);
  617. memset(&fs_info->super_kobj, 0, sizeof(fs_info->super_kobj));
  618. init_completion(&fs_info->kobj_unregister);
  619. sb_set_blocksize(sb, 4096);
  620. fs_info->running_transaction = NULL;
  621. fs_info->last_trans_committed = 0;
  622. fs_info->tree_root = tree_root;
  623. fs_info->extent_root = extent_root;
  624. fs_info->chunk_root = chunk_root;
  625. fs_info->dev_root = dev_root;
  626. fs_info->fs_devices = fs_devices;
  627. INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
  628. INIT_LIST_HEAD(&fs_info->space_info);
  629. btrfs_mapping_init(&fs_info->mapping_tree);
  630. fs_info->sb = sb;
  631. fs_info->throttles = 0;
  632. fs_info->mount_opt = 0;
  633. fs_info->max_extent = (u64)-1;
  634. fs_info->max_inline = 8192 * 1024;
  635. fs_info->delalloc_bytes = 0;
  636. fs_info->btree_inode = new_inode(sb);
  637. fs_info->btree_inode->i_ino = 1;
  638. fs_info->btree_inode->i_nlink = 1;
  639. fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
  640. fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
  641. extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree,
  642. fs_info->btree_inode->i_mapping,
  643. GFP_NOFS);
  644. extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree,
  645. GFP_NOFS);
  646. BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops;
  647. extent_io_tree_init(&fs_info->free_space_cache,
  648. fs_info->btree_inode->i_mapping, GFP_NOFS);
  649. extent_io_tree_init(&fs_info->block_group_cache,
  650. fs_info->btree_inode->i_mapping, GFP_NOFS);
  651. extent_io_tree_init(&fs_info->pinned_extents,
  652. fs_info->btree_inode->i_mapping, GFP_NOFS);
  653. extent_io_tree_init(&fs_info->pending_del,
  654. fs_info->btree_inode->i_mapping, GFP_NOFS);
  655. extent_io_tree_init(&fs_info->extent_ins,
  656. fs_info->btree_inode->i_mapping, GFP_NOFS);
  657. fs_info->do_barriers = 1;
  658. fs_info->closing = 0;
  659. fs_info->total_pinned = 0;
  660. fs_info->last_alloc = 0;
  661. fs_info->last_data_alloc = 0;
  662. fs_info->extra_alloc_bits = 0;
  663. fs_info->extra_data_alloc_bits = 0;
  664. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
  665. INIT_WORK(&fs_info->trans_work, btrfs_transaction_cleaner, fs_info);
  666. #else
  667. INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner);
  668. #endif
  669. BTRFS_I(fs_info->btree_inode)->root = tree_root;
  670. memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
  671. sizeof(struct btrfs_key));
  672. insert_inode_hash(fs_info->btree_inode);
  673. mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
  674. mutex_init(&fs_info->trans_mutex);
  675. mutex_init(&fs_info->fs_mutex);
  676. #if 0
  677. ret = add_hasher(fs_info, "crc32c");
  678. if (ret) {
  679. printk("btrfs: failed hash setup, modprobe cryptomgr?\n");
  680. err = -ENOMEM;
  681. goto fail_iput;
  682. }
  683. #endif
  684. __setup_root(4096, 4096, 4096, 4096, tree_root,
  685. fs_info, BTRFS_ROOT_TREE_OBJECTID);
  686. fs_info->sb_buffer = read_tree_block(tree_root,
  687. BTRFS_SUPER_INFO_OFFSET,
  688. 4096);
  689. if (!fs_info->sb_buffer)
  690. goto fail_iput;
  691. read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0,
  692. sizeof(fs_info->super_copy));
  693. read_extent_buffer(fs_info->sb_buffer, fs_info->fsid,
  694. (unsigned long)btrfs_super_fsid(fs_info->sb_buffer),
  695. BTRFS_FSID_SIZE);
  696. disk_super = &fs_info->super_copy;
  697. if (!btrfs_super_root(disk_super))
  698. goto fail_sb_buffer;
  699. if (btrfs_super_num_devices(disk_super) != fs_devices->num_devices) {
  700. printk("Btrfs: wanted %llu devices, but found %llu\n",
  701. (unsigned long long)btrfs_super_num_devices(disk_super),
  702. (unsigned long long)fs_devices->num_devices);
  703. goto fail_sb_buffer;
  704. }
  705. nodesize = btrfs_super_nodesize(disk_super);
  706. leafsize = btrfs_super_leafsize(disk_super);
  707. sectorsize = btrfs_super_sectorsize(disk_super);
  708. stripesize = btrfs_super_stripesize(disk_super);
  709. tree_root->nodesize = nodesize;
  710. tree_root->leafsize = leafsize;
  711. tree_root->sectorsize = sectorsize;
  712. tree_root->stripesize = stripesize;
  713. sb_set_blocksize(sb, sectorsize);
  714. i_size_write(fs_info->btree_inode,
  715. btrfs_super_total_bytes(disk_super));
  716. if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
  717. sizeof(disk_super->magic))) {
  718. printk("btrfs: valid FS not found on %s\n", sb->s_id);
  719. goto fail_sb_buffer;
  720. }
  721. mutex_lock(&fs_info->fs_mutex);
  722. ret = btrfs_read_sys_array(tree_root);
  723. BUG_ON(ret);
  724. blocksize = btrfs_level_size(tree_root,
  725. btrfs_super_chunk_root_level(disk_super));
  726. __setup_root(nodesize, leafsize, sectorsize, stripesize,
  727. chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
  728. chunk_root->node = read_tree_block(chunk_root,
  729. btrfs_super_chunk_root(disk_super),
  730. blocksize);
  731. BUG_ON(!chunk_root->node);
  732. ret = btrfs_read_chunk_tree(chunk_root);
  733. BUG_ON(ret);
  734. blocksize = btrfs_level_size(tree_root,
  735. btrfs_super_root_level(disk_super));
  736. tree_root->node = read_tree_block(tree_root,
  737. btrfs_super_root(disk_super),
  738. blocksize);
  739. if (!tree_root->node)
  740. goto fail_sb_buffer;
  741. ret = find_and_setup_root(tree_root, fs_info,
  742. BTRFS_EXTENT_TREE_OBJECTID, extent_root);
  743. if (ret)
  744. goto fail_tree_root;
  745. extent_root->track_dirty = 1;
  746. ret = find_and_setup_root(tree_root, fs_info,
  747. BTRFS_DEV_TREE_OBJECTID, dev_root);
  748. dev_root->track_dirty = 1;
  749. if (ret)
  750. goto fail_extent_root;
  751. btrfs_read_block_groups(extent_root);
  752. fs_info->generation = btrfs_super_generation(disk_super) + 1;
  753. mutex_unlock(&fs_info->fs_mutex);
  754. return tree_root;
  755. fail_extent_root:
  756. free_extent_buffer(extent_root->node);
  757. fail_tree_root:
  758. mutex_unlock(&fs_info->fs_mutex);
  759. free_extent_buffer(tree_root->node);
  760. fail_sb_buffer:
  761. free_extent_buffer(fs_info->sb_buffer);
  762. fail_iput:
  763. iput(fs_info->btree_inode);
  764. fail:
  765. close_all_devices(fs_info);
  766. kfree(extent_root);
  767. kfree(tree_root);
  768. kfree(fs_info);
  769. return ERR_PTR(err);
  770. }
  771. int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
  772. *root)
  773. {
  774. int ret;
  775. struct extent_buffer *super = root->fs_info->sb_buffer;
  776. struct inode *btree_inode = root->fs_info->btree_inode;
  777. struct super_block *sb = root->fs_info->sb;
  778. if (!btrfs_test_opt(root, NOBARRIER))
  779. blkdev_issue_flush(sb->s_bdev, NULL);
  780. set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, super);
  781. ret = sync_page_range_nolock(btree_inode, btree_inode->i_mapping,
  782. super->start, super->len);
  783. if (!btrfs_test_opt(root, NOBARRIER))
  784. blkdev_issue_flush(sb->s_bdev, NULL);
  785. return ret;
  786. }
  787. int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
  788. {
  789. radix_tree_delete(&fs_info->fs_roots_radix,
  790. (unsigned long)root->root_key.objectid);
  791. if (root->in_sysfs)
  792. btrfs_sysfs_del_root(root);
  793. if (root->inode)
  794. iput(root->inode);
  795. if (root->node)
  796. free_extent_buffer(root->node);
  797. if (root->commit_root)
  798. free_extent_buffer(root->commit_root);
  799. if (root->name)
  800. kfree(root->name);
  801. kfree(root);
  802. return 0;
  803. }
  804. static int del_fs_roots(struct btrfs_fs_info *fs_info)
  805. {
  806. int ret;
  807. struct btrfs_root *gang[8];
  808. int i;
  809. while(1) {
  810. ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
  811. (void **)gang, 0,
  812. ARRAY_SIZE(gang));
  813. if (!ret)
  814. break;
  815. for (i = 0; i < ret; i++)
  816. btrfs_free_fs_root(fs_info, gang[i]);
  817. }
  818. return 0;
  819. }
  820. int close_ctree(struct btrfs_root *root)
  821. {
  822. int ret;
  823. struct btrfs_trans_handle *trans;
  824. struct btrfs_fs_info *fs_info = root->fs_info;
  825. fs_info->closing = 1;
  826. btrfs_transaction_flush_work(root);
  827. mutex_lock(&fs_info->fs_mutex);
  828. btrfs_defrag_dirty_roots(root->fs_info);
  829. trans = btrfs_start_transaction(root, 1);
  830. ret = btrfs_commit_transaction(trans, root);
  831. /* run commit again to drop the original snapshot */
  832. trans = btrfs_start_transaction(root, 1);
  833. btrfs_commit_transaction(trans, root);
  834. ret = btrfs_write_and_wait_transaction(NULL, root);
  835. BUG_ON(ret);
  836. write_ctree_super(NULL, root);
  837. mutex_unlock(&fs_info->fs_mutex);
  838. if (fs_info->delalloc_bytes) {
  839. printk("btrfs: at unmount delalloc count %Lu\n",
  840. fs_info->delalloc_bytes);
  841. }
  842. if (fs_info->extent_root->node)
  843. free_extent_buffer(fs_info->extent_root->node);
  844. if (fs_info->tree_root->node)
  845. free_extent_buffer(fs_info->tree_root->node);
  846. if (root->fs_info->chunk_root->node);
  847. free_extent_buffer(root->fs_info->chunk_root->node);
  848. if (root->fs_info->dev_root->node);
  849. free_extent_buffer(root->fs_info->dev_root->node);
  850. free_extent_buffer(fs_info->sb_buffer);
  851. btrfs_free_block_groups(root->fs_info);
  852. del_fs_roots(fs_info);
  853. filemap_write_and_wait(fs_info->btree_inode->i_mapping);
  854. extent_io_tree_empty_lru(&fs_info->free_space_cache);
  855. extent_io_tree_empty_lru(&fs_info->block_group_cache);
  856. extent_io_tree_empty_lru(&fs_info->pinned_extents);
  857. extent_io_tree_empty_lru(&fs_info->pending_del);
  858. extent_io_tree_empty_lru(&fs_info->extent_ins);
  859. extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree);
  860. truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
  861. iput(fs_info->btree_inode);
  862. #if 0
  863. while(!list_empty(&fs_info->hashers)) {
  864. struct btrfs_hasher *hasher;
  865. hasher = list_entry(fs_info->hashers.next, struct btrfs_hasher,
  866. hashers);
  867. list_del(&hasher->hashers);
  868. crypto_free_hash(&fs_info->hash_tfm);
  869. kfree(hasher);
  870. }
  871. #endif
  872. close_all_devices(fs_info);
  873. btrfs_mapping_tree_free(&fs_info->mapping_tree);
  874. kfree(fs_info->extent_root);
  875. kfree(fs_info->tree_root);
  876. kfree(fs_info->chunk_root);
  877. kfree(fs_info->dev_root);
  878. return 0;
  879. }
  880. int btrfs_buffer_uptodate(struct extent_buffer *buf)
  881. {
  882. struct inode *btree_inode = buf->first_page->mapping->host;
  883. return extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, buf);
  884. }
  885. int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
  886. {
  887. struct inode *btree_inode = buf->first_page->mapping->host;
  888. return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree,
  889. buf);
  890. }
  891. void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
  892. {
  893. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  894. u64 transid = btrfs_header_generation(buf);
  895. struct inode *btree_inode = root->fs_info->btree_inode;
  896. if (transid != root->fs_info->generation) {
  897. printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n",
  898. (unsigned long long)buf->start,
  899. transid, root->fs_info->generation);
  900. WARN_ON(1);
  901. }
  902. set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, buf);
  903. }
  904. void btrfs_throttle(struct btrfs_root *root)
  905. {
  906. struct backing_dev_info *bdi;
  907. bdi = root->fs_info->sb->s_bdev->bd_inode->i_mapping->backing_dev_info;
  908. if (root->fs_info->throttles && bdi_write_congested(bdi)) {
  909. #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
  910. congestion_wait(WRITE, HZ/20);
  911. #else
  912. blk_congestion_wait(WRITE, HZ/20);
  913. #endif
  914. }
  915. }
  916. void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
  917. {
  918. balance_dirty_pages_ratelimited_nr(
  919. root->fs_info->btree_inode->i_mapping, 1);
  920. }
  921. void btrfs_set_buffer_defrag(struct extent_buffer *buf)
  922. {
  923. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  924. struct inode *btree_inode = root->fs_info->btree_inode;
  925. set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start,
  926. buf->start + buf->len - 1, EXTENT_DEFRAG, GFP_NOFS);
  927. }
  928. void btrfs_set_buffer_defrag_done(struct extent_buffer *buf)
  929. {
  930. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  931. struct inode *btree_inode = root->fs_info->btree_inode;
  932. set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start,
  933. buf->start + buf->len - 1, EXTENT_DEFRAG_DONE,
  934. GFP_NOFS);
  935. }
  936. int btrfs_buffer_defrag(struct extent_buffer *buf)
  937. {
  938. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  939. struct inode *btree_inode = root->fs_info->btree_inode;
  940. return test_range_bit(&BTRFS_I(btree_inode)->io_tree,
  941. buf->start, buf->start + buf->len - 1, EXTENT_DEFRAG, 0);
  942. }
  943. int btrfs_buffer_defrag_done(struct extent_buffer *buf)
  944. {
  945. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  946. struct inode *btree_inode = root->fs_info->btree_inode;
  947. return test_range_bit(&BTRFS_I(btree_inode)->io_tree,
  948. buf->start, buf->start + buf->len - 1,
  949. EXTENT_DEFRAG_DONE, 0);
  950. }
  951. int btrfs_clear_buffer_defrag_done(struct extent_buffer *buf)
  952. {
  953. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  954. struct inode *btree_inode = root->fs_info->btree_inode;
  955. return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree,
  956. buf->start, buf->start + buf->len - 1,
  957. EXTENT_DEFRAG_DONE, GFP_NOFS);
  958. }
  959. int btrfs_clear_buffer_defrag(struct extent_buffer *buf)
  960. {
  961. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  962. struct inode *btree_inode = root->fs_info->btree_inode;
  963. return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree,
  964. buf->start, buf->start + buf->len - 1,
  965. EXTENT_DEFRAG, GFP_NOFS);
  966. }
  967. int btrfs_read_buffer(struct extent_buffer *buf)
  968. {
  969. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  970. struct inode *btree_inode = root->fs_info->btree_inode;
  971. return read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
  972. buf, 0, 1, btree_get_extent);
  973. }
  974. static struct extent_io_ops btree_extent_io_ops = {
  975. .writepage_io_hook = btree_writepage_io_hook,
  976. .submit_bio_hook = btree_submit_bio_hook,
  977. /* note we're sharing with inode.c for the merge bio hook */
  978. .merge_bio_hook = btrfs_merge_bio_hook,
  979. };