disk-io.c 20 KB

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