disk-io.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. #include <linux/module.h>
  2. #include <linux/fs.h>
  3. #include <linux/blkdev.h>
  4. #include <linux/crypto.h>
  5. #include <linux/scatterlist.h>
  6. #include <linux/swap.h>
  7. #include <linux/radix-tree.h>
  8. #include "ctree.h"
  9. #include "disk-io.h"
  10. #include "transaction.h"
  11. #include "btrfs_inode.h"
  12. struct dev_lookup {
  13. u64 block_start;
  14. u64 num_blocks;
  15. struct block_device *bdev;
  16. };
  17. int btrfs_insert_dev_radix(struct btrfs_root *root,
  18. struct block_device *bdev,
  19. u64 block_start,
  20. u64 num_blocks)
  21. {
  22. struct dev_lookup *lookup;
  23. char b[BDEVNAME_SIZE];
  24. int ret;
  25. lookup = kmalloc(sizeof(*lookup), GFP_NOFS);
  26. if (!lookup)
  27. return -ENOMEM;
  28. lookup->block_start = block_start;
  29. lookup->num_blocks = num_blocks;
  30. lookup->bdev = bdev;
  31. printk("inserting %s into dev radix %Lu %Lu\n", bdevname(bdev, b), block_start, num_blocks);
  32. ret = radix_tree_insert(&root->fs_info->dev_radix, block_start +
  33. num_blocks - 1, lookup);
  34. return ret;
  35. }
  36. u64 bh_blocknr(struct buffer_head *bh)
  37. {
  38. int blkbits = bh->b_page->mapping->host->i_blkbits;
  39. u64 blocknr = bh->b_page->index << (PAGE_CACHE_SHIFT - blkbits);
  40. unsigned long offset;
  41. if (PageHighMem(bh->b_page))
  42. offset = (unsigned long)bh->b_data;
  43. else
  44. offset = bh->b_data - (char *)page_address(bh->b_page);
  45. blocknr += offset >> (PAGE_CACHE_SHIFT - blkbits);
  46. return blocknr;
  47. }
  48. static int check_tree_block(struct btrfs_root *root, struct buffer_head *buf)
  49. {
  50. struct btrfs_node *node = btrfs_buffer_node(buf);
  51. if (bh_blocknr(buf) != btrfs_header_blocknr(&node->header)) {
  52. printk(KERN_CRIT "bh_blocknr(buf) is %Lu, header is %Lu\n",
  53. bh_blocknr(buf), btrfs_header_blocknr(&node->header));
  54. BUG();
  55. }
  56. return 0;
  57. }
  58. struct buffer_head *btrfs_find_tree_block(struct btrfs_root *root, u64 blocknr)
  59. {
  60. struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
  61. int blockbits = root->fs_info->sb->s_blocksize_bits;
  62. unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
  63. struct page *page;
  64. struct buffer_head *bh;
  65. struct buffer_head *head;
  66. struct buffer_head *ret = NULL;
  67. page = find_lock_page(mapping, index);
  68. if (!page)
  69. return NULL;
  70. if (!page_has_buffers(page))
  71. goto out_unlock;
  72. head = page_buffers(page);
  73. bh = head;
  74. do {
  75. if (buffer_mapped(bh) && bh_blocknr(bh) == blocknr) {
  76. ret = bh;
  77. get_bh(bh);
  78. goto out_unlock;
  79. }
  80. bh = bh->b_this_page;
  81. } while (bh != head);
  82. out_unlock:
  83. unlock_page(page);
  84. if (ret) {
  85. touch_buffer(ret);
  86. }
  87. page_cache_release(page);
  88. return ret;
  89. }
  90. int btrfs_map_bh_to_logical(struct btrfs_root *root, struct buffer_head *bh,
  91. u64 logical)
  92. {
  93. struct dev_lookup *lookup[2];
  94. int ret;
  95. root = root->fs_info->dev_root;
  96. ret = radix_tree_gang_lookup(&root->fs_info->dev_radix,
  97. (void **)lookup,
  98. (unsigned long)logical,
  99. ARRAY_SIZE(lookup));
  100. if (ret == 0 || lookup[0]->block_start > logical ||
  101. lookup[0]->block_start + lookup[0]->num_blocks <= logical) {
  102. ret = -ENOENT;
  103. goto out;
  104. }
  105. bh->b_bdev = lookup[0]->bdev;
  106. bh->b_blocknr = logical - lookup[0]->block_start;
  107. set_buffer_mapped(bh);
  108. ret = 0;
  109. out:
  110. return ret;
  111. }
  112. struct buffer_head *btrfs_find_create_tree_block(struct btrfs_root *root,
  113. u64 blocknr)
  114. {
  115. struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
  116. int blockbits = root->fs_info->sb->s_blocksize_bits;
  117. unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
  118. struct page *page;
  119. struct buffer_head *bh;
  120. struct buffer_head *head;
  121. struct buffer_head *ret = NULL;
  122. int err;
  123. u64 first_block = index << (PAGE_CACHE_SHIFT - blockbits);
  124. page = grab_cache_page(mapping, index);
  125. if (!page)
  126. return NULL;
  127. if (!page_has_buffers(page))
  128. create_empty_buffers(page, root->fs_info->sb->s_blocksize, 0);
  129. head = page_buffers(page);
  130. bh = head;
  131. do {
  132. if (!buffer_mapped(bh)) {
  133. err = btrfs_map_bh_to_logical(root, bh, first_block);
  134. BUG_ON(err);
  135. }
  136. if (bh_blocknr(bh) == blocknr) {
  137. ret = bh;
  138. get_bh(bh);
  139. goto out_unlock;
  140. }
  141. bh = bh->b_this_page;
  142. first_block++;
  143. } while (bh != head);
  144. out_unlock:
  145. unlock_page(page);
  146. if (ret)
  147. touch_buffer(ret);
  148. page_cache_release(page);
  149. return ret;
  150. }
  151. static int btree_get_block(struct inode *inode, sector_t iblock,
  152. struct buffer_head *bh, int create)
  153. {
  154. int err;
  155. struct btrfs_root *root = BTRFS_I(bh->b_page->mapping->host)->root;
  156. err = btrfs_map_bh_to_logical(root, bh, iblock);
  157. return err;
  158. }
  159. int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len,
  160. char *result)
  161. {
  162. struct scatterlist sg;
  163. struct crypto_hash *tfm = root->fs_info->hash_tfm;
  164. struct hash_desc desc;
  165. int ret;
  166. desc.tfm = tfm;
  167. desc.flags = 0;
  168. sg_init_one(&sg, data, len);
  169. spin_lock(&root->fs_info->hash_lock);
  170. ret = crypto_hash_digest(&desc, &sg, 1, result);
  171. spin_unlock(&root->fs_info->hash_lock);
  172. if (ret) {
  173. printk("sha256 digest failed\n");
  174. }
  175. return ret;
  176. }
  177. static int csum_tree_block(struct btrfs_root *root, struct buffer_head *bh,
  178. int verify)
  179. {
  180. char result[BTRFS_CSUM_SIZE];
  181. int ret;
  182. struct btrfs_node *node;
  183. ret = btrfs_csum_data(root, bh->b_data + BTRFS_CSUM_SIZE,
  184. bh->b_size - BTRFS_CSUM_SIZE, result);
  185. if (ret)
  186. return ret;
  187. if (verify) {
  188. if (memcmp(bh->b_data, result, BTRFS_CSUM_SIZE)) {
  189. printk("checksum verify failed on %Lu\n",
  190. bh_blocknr(bh));
  191. return 1;
  192. }
  193. } else {
  194. node = btrfs_buffer_node(bh);
  195. memcpy(node->header.csum, result, BTRFS_CSUM_SIZE);
  196. }
  197. return 0;
  198. }
  199. static int btree_writepage(struct page *page, struct writeback_control *wbc)
  200. {
  201. struct buffer_head *bh;
  202. struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
  203. struct buffer_head *head;
  204. if (!page_has_buffers(page)) {
  205. create_empty_buffers(page, root->fs_info->sb->s_blocksize,
  206. (1 << BH_Dirty)|(1 << BH_Uptodate));
  207. }
  208. head = page_buffers(page);
  209. bh = head;
  210. do {
  211. if (buffer_dirty(bh))
  212. csum_tree_block(root, bh, 0);
  213. bh = bh->b_this_page;
  214. } while (bh != head);
  215. return block_write_full_page(page, btree_get_block, wbc);
  216. }
  217. static int btree_readpage(struct file * file, struct page * page)
  218. {
  219. return block_read_full_page(page, btree_get_block);
  220. }
  221. static struct address_space_operations btree_aops = {
  222. .readpage = btree_readpage,
  223. .writepage = btree_writepage,
  224. .sync_page = block_sync_page,
  225. };
  226. struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr)
  227. {
  228. struct buffer_head *bh = NULL;
  229. bh = btrfs_find_create_tree_block(root, blocknr);
  230. if (!bh)
  231. return bh;
  232. if (buffer_uptodate(bh))
  233. goto uptodate;
  234. lock_buffer(bh);
  235. if (!buffer_uptodate(bh)) {
  236. get_bh(bh);
  237. bh->b_end_io = end_buffer_read_sync;
  238. submit_bh(READ, bh);
  239. wait_on_buffer(bh);
  240. if (!buffer_uptodate(bh))
  241. goto fail;
  242. csum_tree_block(root, bh, 1);
  243. } else {
  244. unlock_buffer(bh);
  245. }
  246. uptodate:
  247. if (check_tree_block(root, bh))
  248. BUG();
  249. return bh;
  250. fail:
  251. brelse(bh);
  252. return NULL;
  253. }
  254. int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  255. struct buffer_head *buf)
  256. {
  257. WARN_ON(atomic_read(&buf->b_count) == 0);
  258. mark_buffer_dirty(buf);
  259. return 0;
  260. }
  261. int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  262. struct buffer_head *buf)
  263. {
  264. WARN_ON(atomic_read(&buf->b_count) == 0);
  265. clear_buffer_dirty(buf);
  266. return 0;
  267. }
  268. static int __setup_root(int blocksize,
  269. struct btrfs_root *root,
  270. struct btrfs_fs_info *fs_info,
  271. u64 objectid)
  272. {
  273. root->node = NULL;
  274. root->inode = NULL;
  275. root->commit_root = NULL;
  276. root->blocksize = blocksize;
  277. root->ref_cows = 0;
  278. root->fs_info = fs_info;
  279. root->objectid = objectid;
  280. root->last_trans = 0;
  281. root->highest_inode = 0;
  282. root->last_inode_alloc = 0;
  283. memset(&root->root_key, 0, sizeof(root->root_key));
  284. memset(&root->root_item, 0, sizeof(root->root_item));
  285. return 0;
  286. }
  287. static int find_and_setup_root(int blocksize,
  288. struct btrfs_root *tree_root,
  289. struct btrfs_fs_info *fs_info,
  290. u64 objectid,
  291. struct btrfs_root *root)
  292. {
  293. int ret;
  294. __setup_root(blocksize, root, fs_info, objectid);
  295. ret = btrfs_find_last_root(tree_root, objectid,
  296. &root->root_item, &root->root_key);
  297. BUG_ON(ret);
  298. root->node = read_tree_block(root,
  299. btrfs_root_blocknr(&root->root_item));
  300. BUG_ON(!root->node);
  301. return 0;
  302. }
  303. struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
  304. struct btrfs_key *location)
  305. {
  306. struct btrfs_root *root;
  307. struct btrfs_root *tree_root = fs_info->tree_root;
  308. struct btrfs_path *path;
  309. struct btrfs_leaf *l;
  310. u64 highest_inode;
  311. int ret = 0;
  312. printk("read_fs_root looking for %Lu %Lu %u\n", location->objectid, location->offset, location->flags);
  313. root = radix_tree_lookup(&fs_info->fs_roots_radix,
  314. (unsigned long)location->objectid);
  315. if (root) {
  316. printk("found %p in cache\n", root);
  317. return root;
  318. }
  319. root = kmalloc(sizeof(*root), GFP_NOFS);
  320. if (!root) {
  321. printk("failed1\n");
  322. return ERR_PTR(-ENOMEM);
  323. }
  324. if (location->offset == (u64)-1) {
  325. ret = find_and_setup_root(fs_info->sb->s_blocksize,
  326. fs_info->tree_root, fs_info,
  327. location->objectid, root);
  328. if (ret) {
  329. printk("failed2\n");
  330. kfree(root);
  331. return ERR_PTR(ret);
  332. }
  333. goto insert;
  334. }
  335. __setup_root(fs_info->sb->s_blocksize, root, fs_info,
  336. location->objectid);
  337. path = btrfs_alloc_path();
  338. BUG_ON(!path);
  339. ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
  340. if (ret != 0) {
  341. printk("internal search_slot gives us %d\n", ret);
  342. if (ret > 0)
  343. ret = -ENOENT;
  344. goto out;
  345. }
  346. l = btrfs_buffer_leaf(path->nodes[0]);
  347. memcpy(&root->root_item,
  348. btrfs_item_ptr(l, path->slots[0], struct btrfs_root_item),
  349. sizeof(root->root_item));
  350. memcpy(&root->root_key, location, sizeof(*location));
  351. ret = 0;
  352. out:
  353. btrfs_release_path(root, path);
  354. btrfs_free_path(path);
  355. if (ret) {
  356. kfree(root);
  357. return ERR_PTR(ret);
  358. }
  359. root->node = read_tree_block(root,
  360. btrfs_root_blocknr(&root->root_item));
  361. BUG_ON(!root->node);
  362. insert:
  363. printk("inserting %p\n", root);
  364. root->ref_cows = 1;
  365. ret = radix_tree_insert(&fs_info->fs_roots_radix,
  366. (unsigned long)root->root_key.objectid,
  367. root);
  368. if (ret) {
  369. printk("radix_tree_insert gives us %d\n", ret);
  370. brelse(root->node);
  371. kfree(root);
  372. return ERR_PTR(ret);
  373. }
  374. ret = btrfs_find_highest_inode(root, &highest_inode);
  375. if (ret == 0) {
  376. root->highest_inode = highest_inode;
  377. root->last_inode_alloc = highest_inode;
  378. printk("highest inode is %Lu\n", highest_inode);
  379. }
  380. printk("all worked\n");
  381. return root;
  382. }
  383. int btrfs_open_disk(struct btrfs_root *root, u64 block_start, u64 num_blocks,
  384. char *filename, int name_len)
  385. {
  386. char *null_filename;
  387. struct block_device *bdev;
  388. int ret;
  389. if (block_start == 0) {
  390. printk("skipping disk with block_start == 0\n");
  391. return 0;
  392. }
  393. null_filename = kmalloc(name_len + 1, GFP_NOFS);
  394. if (!null_filename)
  395. return -ENOMEM;
  396. memcpy(null_filename, filename, name_len);
  397. null_filename[name_len] = '\0';
  398. bdev = open_bdev_excl(null_filename, O_RDWR, root->fs_info->sb);
  399. if (IS_ERR(bdev)) {
  400. ret = PTR_ERR(bdev);
  401. goto out;
  402. }
  403. set_blocksize(bdev, root->fs_info->sb->s_blocksize);
  404. ret = btrfs_insert_dev_radix(root, bdev, block_start, num_blocks);
  405. BUG_ON(ret);
  406. ret = 0;
  407. out:
  408. kfree(null_filename);
  409. return ret;
  410. }
  411. static int read_device_info(struct btrfs_root *root)
  412. {
  413. struct btrfs_path *path;
  414. int ret;
  415. struct btrfs_key key;
  416. struct btrfs_leaf *leaf;
  417. struct btrfs_device_item *dev_item;
  418. int nritems;
  419. int slot;
  420. root = root->fs_info->dev_root;
  421. path = btrfs_alloc_path();
  422. if (!path)
  423. return -ENOMEM;
  424. key.objectid = 0;
  425. key.offset = 0;
  426. key.flags = 0;
  427. btrfs_set_key_type(&key, BTRFS_DEV_ITEM_KEY);
  428. mutex_lock(&root->fs_info->fs_mutex);
  429. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  430. leaf = btrfs_buffer_leaf(path->nodes[0]);
  431. nritems = btrfs_header_nritems(&leaf->header);
  432. while(1) {
  433. slot = path->slots[0];
  434. if (slot >= nritems) {
  435. ret = btrfs_next_leaf(root, path);
  436. if (ret)
  437. break;
  438. leaf = btrfs_buffer_leaf(path->nodes[0]);
  439. nritems = btrfs_header_nritems(&leaf->header);
  440. slot = path->slots[0];
  441. }
  442. btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
  443. if (btrfs_key_type(&key) != BTRFS_DEV_ITEM_KEY) {
  444. path->slots[0]++;
  445. continue;
  446. }
  447. dev_item = btrfs_item_ptr(leaf, slot, struct btrfs_device_item);
  448. printk("found key %Lu %Lu\n", key.objectid, key.offset);
  449. ret = btrfs_open_disk(root, key.objectid, key.offset,
  450. (char *)(dev_item + 1),
  451. btrfs_device_pathlen(dev_item));
  452. BUG_ON(ret);
  453. path->slots[0]++;
  454. }
  455. btrfs_free_path(path);
  456. mutex_unlock(&root->fs_info->fs_mutex);
  457. return 0;
  458. }
  459. struct btrfs_root *open_ctree(struct super_block *sb)
  460. {
  461. struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
  462. GFP_NOFS);
  463. struct btrfs_root *dev_root = kmalloc(sizeof(struct btrfs_root),
  464. GFP_NOFS);
  465. struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
  466. GFP_NOFS);
  467. struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
  468. GFP_NOFS);
  469. int ret;
  470. struct btrfs_super_block *disk_super;
  471. struct dev_lookup *dev_lookup;
  472. init_bit_radix(&fs_info->pinned_radix);
  473. init_bit_radix(&fs_info->pending_del_radix);
  474. INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
  475. INIT_RADIX_TREE(&fs_info->dev_radix, GFP_NOFS);
  476. sb_set_blocksize(sb, 4096);
  477. fs_info->running_transaction = NULL;
  478. fs_info->tree_root = tree_root;
  479. fs_info->extent_root = extent_root;
  480. fs_info->dev_root = dev_root;
  481. fs_info->sb = sb;
  482. fs_info->btree_inode = new_inode(sb);
  483. fs_info->btree_inode->i_ino = 1;
  484. fs_info->btree_inode->i_nlink = 1;
  485. fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
  486. fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
  487. BTRFS_I(fs_info->btree_inode)->root = tree_root;
  488. memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
  489. sizeof(struct btrfs_key));
  490. insert_inode_hash(fs_info->btree_inode);
  491. mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
  492. fs_info->hash_tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
  493. spin_lock_init(&fs_info->hash_lock);
  494. if (!fs_info->hash_tfm || IS_ERR(fs_info->hash_tfm)) {
  495. printk("failed to allocate sha256 hash\n");
  496. return NULL;
  497. }
  498. mutex_init(&fs_info->trans_mutex);
  499. mutex_init(&fs_info->fs_mutex);
  500. memset(&fs_info->current_insert, 0, sizeof(fs_info->current_insert));
  501. memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert));
  502. __setup_root(sb->s_blocksize, dev_root,
  503. fs_info, BTRFS_DEV_TREE_OBJECTID);
  504. __setup_root(sb->s_blocksize, tree_root,
  505. fs_info, BTRFS_ROOT_TREE_OBJECTID);
  506. dev_lookup = kmalloc(sizeof(*dev_lookup), GFP_NOFS);
  507. dev_lookup->block_start = 0;
  508. dev_lookup->num_blocks = (u32)-2;
  509. dev_lookup->bdev = sb->s_bdev;
  510. ret = radix_tree_insert(&fs_info->dev_radix, (u32)-2, dev_lookup);
  511. BUG_ON(ret);
  512. fs_info->sb_buffer = read_tree_block(tree_root,
  513. BTRFS_SUPER_INFO_OFFSET /
  514. sb->s_blocksize);
  515. if (!fs_info->sb_buffer)
  516. return NULL;
  517. disk_super = (struct btrfs_super_block *)fs_info->sb_buffer->b_data;
  518. if (!btrfs_super_root(disk_super))
  519. return NULL;
  520. i_size_write(fs_info->btree_inode,
  521. btrfs_super_total_blocks(disk_super) <<
  522. fs_info->btree_inode->i_blkbits);
  523. radix_tree_delete(&fs_info->dev_radix, (u32)-2);
  524. dev_lookup->block_start = btrfs_super_device_block_start(disk_super);
  525. dev_lookup->num_blocks = btrfs_super_device_num_blocks(disk_super);
  526. ret = radix_tree_insert(&fs_info->dev_radix,
  527. dev_lookup->block_start +
  528. dev_lookup->num_blocks - 1, dev_lookup);
  529. BUG_ON(ret);
  530. fs_info->disk_super = disk_super;
  531. dev_root->node = read_tree_block(tree_root,
  532. btrfs_super_device_root(disk_super));
  533. ret = read_device_info(dev_root);
  534. BUG_ON(ret);
  535. tree_root->node = read_tree_block(tree_root,
  536. btrfs_super_root(disk_super));
  537. BUG_ON(!tree_root->node);
  538. mutex_lock(&fs_info->fs_mutex);
  539. ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info,
  540. BTRFS_EXTENT_TREE_OBJECTID, extent_root);
  541. BUG_ON(ret);
  542. fs_info->generation = btrfs_super_generation(disk_super) + 1;
  543. memset(&fs_info->kobj, 0, sizeof(fs_info->kobj));
  544. kobj_set_kset_s(fs_info, btrfs_subsys);
  545. kobject_set_name(&fs_info->kobj, "%s", sb->s_id);
  546. kobject_register(&fs_info->kobj);
  547. mutex_unlock(&fs_info->fs_mutex);
  548. return tree_root;
  549. }
  550. int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
  551. *root)
  552. {
  553. struct buffer_head *bh = root->fs_info->sb_buffer;
  554. btrfs_set_super_root(root->fs_info->disk_super,
  555. bh_blocknr(root->fs_info->tree_root->node));
  556. lock_buffer(bh);
  557. WARN_ON(atomic_read(&bh->b_count) < 1);
  558. clear_buffer_dirty(bh);
  559. csum_tree_block(root, bh, 0);
  560. bh->b_end_io = end_buffer_write_sync;
  561. get_bh(bh);
  562. submit_bh(WRITE, bh);
  563. wait_on_buffer(bh);
  564. if (!buffer_uptodate(bh)) {
  565. WARN_ON(1);
  566. return -EIO;
  567. }
  568. return 0;
  569. }
  570. static int free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
  571. {
  572. radix_tree_delete(&fs_info->fs_roots_radix,
  573. (unsigned long)root->root_key.objectid);
  574. if (root->inode)
  575. iput(root->inode);
  576. if (root->node)
  577. brelse(root->node);
  578. if (root->commit_root)
  579. brelse(root->commit_root);
  580. kfree(root);
  581. return 0;
  582. }
  583. int del_fs_roots(struct btrfs_fs_info *fs_info)
  584. {
  585. int ret;
  586. struct btrfs_root *gang[8];
  587. int i;
  588. while(1) {
  589. ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
  590. (void **)gang, 0,
  591. ARRAY_SIZE(gang));
  592. if (!ret)
  593. break;
  594. for (i = 0; i < ret; i++)
  595. free_fs_root(fs_info, gang[i]);
  596. }
  597. return 0;
  598. }
  599. static int free_dev_radix(struct btrfs_fs_info *fs_info)
  600. {
  601. struct dev_lookup *lookup[8];
  602. struct block_device *super_bdev = fs_info->sb->s_bdev;
  603. int ret;
  604. int i;
  605. while(1) {
  606. ret = radix_tree_gang_lookup(&fs_info->dev_radix,
  607. (void **)lookup, 0,
  608. ARRAY_SIZE(lookup));
  609. if (!ret)
  610. break;
  611. for (i = 0; i < ret; i++) {
  612. if (lookup[i]->bdev != super_bdev)
  613. close_bdev_excl(lookup[i]->bdev);
  614. radix_tree_delete(&fs_info->dev_radix,
  615. lookup[i]->block_start +
  616. lookup[i]->num_blocks - 1);
  617. kfree(lookup[i]);
  618. }
  619. }
  620. return 0;
  621. }
  622. int close_ctree(struct btrfs_root *root)
  623. {
  624. int ret;
  625. struct btrfs_trans_handle *trans;
  626. struct btrfs_fs_info *fs_info = root->fs_info;
  627. mutex_lock(&fs_info->fs_mutex);
  628. trans = btrfs_start_transaction(root, 1);
  629. btrfs_commit_transaction(trans, root);
  630. /* run commit again to drop the original snapshot */
  631. trans = btrfs_start_transaction(root, 1);
  632. btrfs_commit_transaction(trans, root);
  633. ret = btrfs_write_and_wait_transaction(NULL, root);
  634. BUG_ON(ret);
  635. write_ctree_super(NULL, root);
  636. mutex_unlock(&fs_info->fs_mutex);
  637. if (fs_info->extent_root->node)
  638. btrfs_block_release(fs_info->extent_root,
  639. fs_info->extent_root->node);
  640. if (fs_info->dev_root->node)
  641. btrfs_block_release(fs_info->dev_root,
  642. fs_info->dev_root->node);
  643. if (fs_info->tree_root->node)
  644. btrfs_block_release(fs_info->tree_root,
  645. fs_info->tree_root->node);
  646. btrfs_block_release(root, fs_info->sb_buffer);
  647. crypto_free_hash(fs_info->hash_tfm);
  648. truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
  649. iput(fs_info->btree_inode);
  650. free_dev_radix(fs_info);
  651. del_fs_roots(fs_info);
  652. kfree(fs_info->extent_root);
  653. kfree(fs_info->tree_root);
  654. kobject_unregister(&fs_info->kobj);
  655. return 0;
  656. }
  657. void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf)
  658. {
  659. brelse(buf);
  660. }