super.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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/module.h>
  19. #include <linux/buffer_head.h>
  20. #include <linux/fs.h>
  21. #include <linux/pagemap.h>
  22. #include <linux/highmem.h>
  23. #include <linux/time.h>
  24. #include <linux/init.h>
  25. #include <linux/string.h>
  26. #include <linux/smp_lock.h>
  27. #include <linux/backing-dev.h>
  28. #include <linux/mpage.h>
  29. #include <linux/swap.h>
  30. #include <linux/writeback.h>
  31. #include <linux/statfs.h>
  32. #include <linux/compat.h>
  33. #include "ctree.h"
  34. #include "disk-io.h"
  35. #include "transaction.h"
  36. #include "btrfs_inode.h"
  37. #include "ioctl.h"
  38. #include "print-tree.h"
  39. #define BTRFS_SUPER_MAGIC 0x9123682E
  40. static struct super_operations btrfs_super_ops;
  41. static void btrfs_put_super (struct super_block * sb)
  42. {
  43. struct btrfs_root *root = btrfs_sb(sb);
  44. int ret;
  45. ret = close_ctree(root);
  46. if (ret) {
  47. printk("close ctree returns %d\n", ret);
  48. }
  49. sb->s_fs_info = NULL;
  50. }
  51. static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
  52. {
  53. struct inode * inode;
  54. struct dentry * root_dentry;
  55. struct btrfs_super_block *disk_super;
  56. struct btrfs_root *tree_root;
  57. struct btrfs_inode *bi;
  58. int err;
  59. sb->s_maxbytes = MAX_LFS_FILESIZE;
  60. sb->s_magic = BTRFS_SUPER_MAGIC;
  61. sb->s_op = &btrfs_super_ops;
  62. sb->s_time_gran = 1;
  63. tree_root = open_ctree(sb);
  64. if (!tree_root || IS_ERR(tree_root)) {
  65. printk("btrfs: open_ctree failed\n");
  66. return -EIO;
  67. }
  68. sb->s_fs_info = tree_root;
  69. disk_super = tree_root->fs_info->disk_super;
  70. inode = btrfs_iget_locked(sb, btrfs_super_root_dir(disk_super),
  71. tree_root);
  72. bi = BTRFS_I(inode);
  73. bi->location.objectid = inode->i_ino;
  74. bi->location.offset = 0;
  75. bi->location.flags = 0;
  76. bi->root = tree_root;
  77. btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
  78. if (!inode) {
  79. err = -ENOMEM;
  80. goto fail_close;
  81. }
  82. if (inode->i_state & I_NEW) {
  83. btrfs_read_locked_inode(inode);
  84. unlock_new_inode(inode);
  85. }
  86. root_dentry = d_alloc_root(inode);
  87. if (!root_dentry) {
  88. iput(inode);
  89. err = -ENOMEM;
  90. goto fail_close;
  91. }
  92. sb->s_root = root_dentry;
  93. btrfs_transaction_queue_work(tree_root, HZ * 30);
  94. return 0;
  95. fail_close:
  96. close_ctree(tree_root);
  97. return err;
  98. }
  99. static int btrfs_sync_fs(struct super_block *sb, int wait)
  100. {
  101. struct btrfs_trans_handle *trans;
  102. struct btrfs_root *root;
  103. int ret;
  104. root = btrfs_sb(sb);
  105. sb->s_dirt = 0;
  106. if (!wait) {
  107. filemap_flush(root->fs_info->btree_inode->i_mapping);
  108. return 0;
  109. }
  110. mutex_lock(&root->fs_info->fs_mutex);
  111. trans = btrfs_start_transaction(root, 1);
  112. ret = btrfs_commit_transaction(trans, root);
  113. sb->s_dirt = 0;
  114. mutex_unlock(&root->fs_info->fs_mutex);
  115. return ret;
  116. }
  117. static void btrfs_write_super(struct super_block *sb)
  118. {
  119. sb->s_dirt = 0;
  120. }
  121. static int btrfs_get_sb(struct file_system_type *fs_type,
  122. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  123. {
  124. return get_sb_bdev(fs_type, flags, dev_name, data,
  125. btrfs_fill_super, mnt);
  126. }
  127. static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  128. {
  129. struct btrfs_root *root = btrfs_sb(dentry->d_sb);
  130. struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
  131. buf->f_namelen = BTRFS_NAME_LEN;
  132. buf->f_blocks = btrfs_super_total_blocks(disk_super);
  133. buf->f_bfree = buf->f_blocks - btrfs_super_blocks_used(disk_super);
  134. buf->f_bavail = buf->f_bfree;
  135. buf->f_bsize = dentry->d_sb->s_blocksize;
  136. buf->f_type = BTRFS_SUPER_MAGIC;
  137. return 0;
  138. }
  139. static struct file_system_type btrfs_fs_type = {
  140. .owner = THIS_MODULE,
  141. .name = "btrfs",
  142. .get_sb = btrfs_get_sb,
  143. .kill_sb = kill_block_super,
  144. .fs_flags = FS_REQUIRES_DEV,
  145. };
  146. static struct super_operations btrfs_super_ops = {
  147. .delete_inode = btrfs_delete_inode,
  148. .put_super = btrfs_put_super,
  149. .read_inode = btrfs_read_locked_inode,
  150. .write_super = btrfs_write_super,
  151. .sync_fs = btrfs_sync_fs,
  152. .write_inode = btrfs_write_inode,
  153. .dirty_inode = btrfs_dirty_inode,
  154. .alloc_inode = btrfs_alloc_inode,
  155. .destroy_inode = btrfs_destroy_inode,
  156. .statfs = btrfs_statfs,
  157. };
  158. static int __init init_btrfs_fs(void)
  159. {
  160. int err;
  161. btrfs_init_transaction_sys();
  162. err = btrfs_init_cachep();
  163. if (err)
  164. return err;
  165. return register_filesystem(&btrfs_fs_type);
  166. }
  167. static void __exit exit_btrfs_fs(void)
  168. {
  169. btrfs_exit_transaction_sys();
  170. btrfs_destroy_cachep();
  171. unregister_filesystem(&btrfs_fs_type);
  172. }
  173. module_init(init_btrfs_fs)
  174. module_exit(exit_btrfs_fs)
  175. MODULE_LICENSE("GPL");