super.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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/blkdev.h>
  19. #include <linux/module.h>
  20. #include <linux/buffer_head.h>
  21. #include <linux/fs.h>
  22. #include <linux/pagemap.h>
  23. #include <linux/highmem.h>
  24. #include <linux/time.h>
  25. #include <linux/init.h>
  26. #include <linux/string.h>
  27. #include <linux/smp_lock.h>
  28. #include <linux/backing-dev.h>
  29. #include <linux/mount.h>
  30. #include <linux/mpage.h>
  31. #include <linux/swap.h>
  32. #include <linux/writeback.h>
  33. #include <linux/statfs.h>
  34. #include <linux/compat.h>
  35. #include "ctree.h"
  36. #include "disk-io.h"
  37. #include "transaction.h"
  38. #include "btrfs_inode.h"
  39. #include "ioctl.h"
  40. #include "print-tree.h"
  41. #define BTRFS_SUPER_MAGIC 0x9123682E
  42. static struct super_operations btrfs_super_ops;
  43. static void btrfs_put_super (struct super_block * sb)
  44. {
  45. struct btrfs_root *root = btrfs_sb(sb);
  46. struct btrfs_fs_info *fs = root->fs_info;
  47. int ret;
  48. ret = close_ctree(root);
  49. if (ret) {
  50. printk("close ctree returns %d\n", ret);
  51. }
  52. btrfs_sysfs_del_super(fs);
  53. sb->s_fs_info = NULL;
  54. }
  55. static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
  56. {
  57. struct inode * inode;
  58. struct dentry * root_dentry;
  59. struct btrfs_super_block *disk_super;
  60. struct btrfs_root *tree_root;
  61. struct btrfs_inode *bi;
  62. int err;
  63. sb->s_maxbytes = MAX_LFS_FILESIZE;
  64. sb->s_magic = BTRFS_SUPER_MAGIC;
  65. sb->s_op = &btrfs_super_ops;
  66. sb->s_time_gran = 1;
  67. tree_root = open_ctree(sb);
  68. if (!tree_root || IS_ERR(tree_root)) {
  69. printk("btrfs: open_ctree failed\n");
  70. return -EIO;
  71. }
  72. sb->s_fs_info = tree_root;
  73. disk_super = tree_root->fs_info->disk_super;
  74. inode = btrfs_iget_locked(sb, btrfs_super_root_dir(disk_super),
  75. tree_root);
  76. bi = BTRFS_I(inode);
  77. bi->location.objectid = inode->i_ino;
  78. bi->location.offset = 0;
  79. bi->location.flags = 0;
  80. bi->root = tree_root;
  81. btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
  82. if (!inode) {
  83. err = -ENOMEM;
  84. goto fail_close;
  85. }
  86. if (inode->i_state & I_NEW) {
  87. btrfs_read_locked_inode(inode);
  88. unlock_new_inode(inode);
  89. }
  90. root_dentry = d_alloc_root(inode);
  91. if (!root_dentry) {
  92. iput(inode);
  93. err = -ENOMEM;
  94. goto fail_close;
  95. }
  96. /* this does the super kobj at the same time */
  97. err = btrfs_sysfs_add_super(tree_root->fs_info);
  98. if (err)
  99. goto fail_close;
  100. sb->s_root = root_dentry;
  101. btrfs_transaction_queue_work(tree_root, HZ * 30);
  102. return 0;
  103. fail_close:
  104. close_ctree(tree_root);
  105. return err;
  106. }
  107. static int btrfs_sync_fs(struct super_block *sb, int wait)
  108. {
  109. struct btrfs_trans_handle *trans;
  110. struct btrfs_root *root;
  111. int ret;
  112. root = btrfs_sb(sb);
  113. sb->s_dirt = 0;
  114. if (!wait) {
  115. filemap_flush(root->fs_info->btree_inode->i_mapping);
  116. return 0;
  117. }
  118. btrfs_clean_old_snapshots(root);
  119. mutex_lock(&root->fs_info->fs_mutex);
  120. btrfs_defrag_dirty_roots(root->fs_info);
  121. trans = btrfs_start_transaction(root, 1);
  122. ret = btrfs_commit_transaction(trans, root);
  123. sb->s_dirt = 0;
  124. mutex_unlock(&root->fs_info->fs_mutex);
  125. return ret;
  126. }
  127. static void btrfs_write_super(struct super_block *sb)
  128. {
  129. sb->s_dirt = 0;
  130. }
  131. /*
  132. * This is almost a copy of get_sb_bdev in fs/super.c.
  133. * We need the local copy to allow direct mounting of
  134. * subvolumes, but this could be easily integrated back
  135. * into the generic version. --hch
  136. */
  137. /* start copy & paste */
  138. static int set_bdev_super(struct super_block *s, void *data)
  139. {
  140. s->s_bdev = data;
  141. s->s_dev = s->s_bdev->bd_dev;
  142. return 0;
  143. }
  144. static int test_bdev_super(struct super_block *s, void *data)
  145. {
  146. return (void *)s->s_bdev == data;
  147. }
  148. int btrfs_get_sb_bdev(struct file_system_type *fs_type,
  149. int flags, const char *dev_name, void *data,
  150. int (*fill_super)(struct super_block *, void *, int),
  151. struct vfsmount *mnt, const char *subvol)
  152. {
  153. struct block_device *bdev = NULL;
  154. struct super_block *s;
  155. struct dentry *root;
  156. int error = 0;
  157. bdev = open_bdev_excl(dev_name, flags, fs_type);
  158. if (IS_ERR(bdev))
  159. return PTR_ERR(bdev);
  160. /*
  161. * once the super is inserted into the list by sget, s_umount
  162. * will protect the lockfs code from trying to start a snapshot
  163. * while we are mounting
  164. */
  165. down(&bdev->bd_mount_sem);
  166. s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
  167. up(&bdev->bd_mount_sem);
  168. if (IS_ERR(s))
  169. goto error_s;
  170. if (s->s_root) {
  171. if ((flags ^ s->s_flags) & MS_RDONLY) {
  172. up_write(&s->s_umount);
  173. deactivate_super(s);
  174. error = -EBUSY;
  175. goto error_bdev;
  176. }
  177. close_bdev_excl(bdev);
  178. } else {
  179. char b[BDEVNAME_SIZE];
  180. s->s_flags = flags;
  181. strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
  182. sb_set_blocksize(s, block_size(bdev));
  183. error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  184. if (error) {
  185. up_write(&s->s_umount);
  186. deactivate_super(s);
  187. goto error;
  188. }
  189. s->s_flags |= MS_ACTIVE;
  190. }
  191. if (subvol) {
  192. root = lookup_one_len(subvol, s->s_root, strlen(subvol));
  193. if (IS_ERR(root)) {
  194. up_write(&s->s_umount);
  195. deactivate_super(s);
  196. error = PTR_ERR(root);
  197. goto error;
  198. }
  199. if (!root->d_inode) {
  200. dput(root);
  201. up_write(&s->s_umount);
  202. deactivate_super(s);
  203. error = -ENXIO;
  204. goto error;
  205. }
  206. } else {
  207. root = dget(s->s_root);
  208. }
  209. mnt->mnt_sb = s;
  210. mnt->mnt_root = root;
  211. return 0;
  212. error_s:
  213. error = PTR_ERR(s);
  214. error_bdev:
  215. close_bdev_excl(bdev);
  216. error:
  217. return error;
  218. }
  219. /* end copy & paste */
  220. static int btrfs_get_sb(struct file_system_type *fs_type,
  221. int flags, const char *identifier, void *data, struct vfsmount *mnt)
  222. {
  223. int ret;
  224. char *_identifier = kstrdup(identifier, GFP_KERNEL);
  225. char *subvol_name;
  226. const char *dev_name;
  227. subvol_name = _identifier;
  228. dev_name = strsep(&subvol_name, ":");
  229. if (!dev_name)
  230. return -ENOMEM;
  231. ret = btrfs_get_sb_bdev(fs_type, flags, dev_name, data,
  232. btrfs_fill_super, mnt,
  233. subvol_name ? subvol_name : "default");
  234. kfree(_identifier);
  235. return ret;
  236. }
  237. static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  238. {
  239. struct btrfs_root *root = btrfs_sb(dentry->d_sb);
  240. struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
  241. buf->f_namelen = BTRFS_NAME_LEN;
  242. buf->f_blocks = btrfs_super_total_blocks(disk_super);
  243. buf->f_bfree = buf->f_blocks - btrfs_super_blocks_used(disk_super);
  244. buf->f_bavail = buf->f_bfree;
  245. buf->f_bsize = dentry->d_sb->s_blocksize;
  246. buf->f_type = BTRFS_SUPER_MAGIC;
  247. return 0;
  248. }
  249. static struct file_system_type btrfs_fs_type = {
  250. .owner = THIS_MODULE,
  251. .name = "btrfs",
  252. .get_sb = btrfs_get_sb,
  253. .kill_sb = kill_block_super,
  254. .fs_flags = FS_REQUIRES_DEV,
  255. };
  256. static struct super_operations btrfs_super_ops = {
  257. .delete_inode = btrfs_delete_inode,
  258. .put_super = btrfs_put_super,
  259. .read_inode = btrfs_read_locked_inode,
  260. .write_super = btrfs_write_super,
  261. .sync_fs = btrfs_sync_fs,
  262. .write_inode = btrfs_write_inode,
  263. .dirty_inode = btrfs_dirty_inode,
  264. .alloc_inode = btrfs_alloc_inode,
  265. .destroy_inode = btrfs_destroy_inode,
  266. .statfs = btrfs_statfs,
  267. };
  268. static int __init init_btrfs_fs(void)
  269. {
  270. int err;
  271. err = btrfs_init_sysfs();
  272. if (err)
  273. return err;
  274. btrfs_init_transaction_sys();
  275. err = btrfs_init_cachep();
  276. if (err)
  277. return err;
  278. extent_map_init();
  279. return register_filesystem(&btrfs_fs_type);
  280. }
  281. static void __exit exit_btrfs_fs(void)
  282. {
  283. btrfs_exit_transaction_sys();
  284. btrfs_destroy_cachep();
  285. extent_map_exit();
  286. unregister_filesystem(&btrfs_fs_type);
  287. btrfs_exit_sysfs();
  288. }
  289. module_init(init_btrfs_fs)
  290. module_exit(exit_btrfs_fs)
  291. MODULE_LICENSE("GPL");