super.c 8.2 KB

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