super.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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 0x9123683E
  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->super_copy;
  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->root = tree_root;
  111. btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
  112. if (!inode) {
  113. err = -ENOMEM;
  114. goto fail_close;
  115. }
  116. if (inode->i_state & I_NEW) {
  117. btrfs_read_locked_inode(inode);
  118. unlock_new_inode(inode);
  119. }
  120. root_dentry = d_alloc_root(inode);
  121. if (!root_dentry) {
  122. iput(inode);
  123. err = -ENOMEM;
  124. goto fail_close;
  125. }
  126. /* this does the super kobj at the same time */
  127. err = btrfs_sysfs_add_super(tree_root->fs_info);
  128. if (err)
  129. goto fail_close;
  130. sb->s_root = root_dentry;
  131. btrfs_transaction_queue_work(tree_root, HZ * 30);
  132. return 0;
  133. fail_close:
  134. close_ctree(tree_root);
  135. return err;
  136. }
  137. static int btrfs_sync_fs(struct super_block *sb, int wait)
  138. {
  139. struct btrfs_trans_handle *trans;
  140. struct btrfs_root *root;
  141. int ret;
  142. root = btrfs_sb(sb);
  143. sb->s_dirt = 0;
  144. if (!wait) {
  145. filemap_flush(root->fs_info->btree_inode->i_mapping);
  146. return 0;
  147. }
  148. btrfs_clean_old_snapshots(root);
  149. mutex_lock(&root->fs_info->fs_mutex);
  150. btrfs_defrag_dirty_roots(root->fs_info);
  151. trans = btrfs_start_transaction(root, 1);
  152. ret = btrfs_commit_transaction(trans, root);
  153. sb->s_dirt = 0;
  154. mutex_unlock(&root->fs_info->fs_mutex);
  155. return ret;
  156. }
  157. static void btrfs_write_super(struct super_block *sb)
  158. {
  159. sb->s_dirt = 0;
  160. }
  161. /*
  162. * This is almost a copy of get_sb_bdev in fs/super.c.
  163. * We need the local copy to allow direct mounting of
  164. * subvolumes, but this could be easily integrated back
  165. * into the generic version. --hch
  166. */
  167. /* start copy & paste */
  168. static int set_bdev_super(struct super_block *s, void *data)
  169. {
  170. s->s_bdev = data;
  171. s->s_dev = s->s_bdev->bd_dev;
  172. return 0;
  173. }
  174. static int test_bdev_super(struct super_block *s, void *data)
  175. {
  176. return (void *)s->s_bdev == data;
  177. }
  178. int btrfs_get_sb_bdev(struct file_system_type *fs_type,
  179. int flags, const char *dev_name, void *data,
  180. int (*fill_super)(struct super_block *, void *, int),
  181. struct vfsmount *mnt, const char *subvol)
  182. {
  183. struct block_device *bdev = NULL;
  184. struct super_block *s;
  185. struct dentry *root;
  186. int error = 0;
  187. bdev = open_bdev_excl(dev_name, flags, fs_type);
  188. if (IS_ERR(bdev))
  189. return PTR_ERR(bdev);
  190. /*
  191. * once the super is inserted into the list by sget, s_umount
  192. * will protect the lockfs code from trying to start a snapshot
  193. * while we are mounting
  194. */
  195. down(&bdev->bd_mount_sem);
  196. s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
  197. up(&bdev->bd_mount_sem);
  198. if (IS_ERR(s))
  199. goto error_s;
  200. if (s->s_root) {
  201. if ((flags ^ s->s_flags) & MS_RDONLY) {
  202. up_write(&s->s_umount);
  203. deactivate_super(s);
  204. error = -EBUSY;
  205. goto error_bdev;
  206. }
  207. close_bdev_excl(bdev);
  208. } else {
  209. char b[BDEVNAME_SIZE];
  210. s->s_flags = flags;
  211. strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
  212. sb_set_blocksize(s, block_size(bdev));
  213. error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  214. if (error) {
  215. up_write(&s->s_umount);
  216. deactivate_super(s);
  217. goto error;
  218. }
  219. s->s_flags |= MS_ACTIVE;
  220. }
  221. if (subvol) {
  222. root = lookup_one_len(subvol, s->s_root, strlen(subvol));
  223. if (IS_ERR(root)) {
  224. up_write(&s->s_umount);
  225. deactivate_super(s);
  226. error = PTR_ERR(root);
  227. goto error;
  228. }
  229. if (!root->d_inode) {
  230. dput(root);
  231. up_write(&s->s_umount);
  232. deactivate_super(s);
  233. error = -ENXIO;
  234. goto error;
  235. }
  236. } else {
  237. root = dget(s->s_root);
  238. }
  239. mnt->mnt_sb = s;
  240. mnt->mnt_root = root;
  241. return 0;
  242. error_s:
  243. error = PTR_ERR(s);
  244. error_bdev:
  245. close_bdev_excl(bdev);
  246. error:
  247. printk("get_sb failed\n");
  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. printk("btrfs_get_sb returns %d\n", ret);
  261. return ret;
  262. }
  263. static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  264. {
  265. struct btrfs_root *root = btrfs_sb(dentry->d_sb);
  266. struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
  267. int bits = dentry->d_sb->s_blocksize_bits;
  268. buf->f_namelen = BTRFS_NAME_LEN;
  269. buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
  270. buf->f_bfree = buf->f_blocks -
  271. (btrfs_super_bytes_used(disk_super) >> bits);
  272. buf->f_bavail = buf->f_bfree;
  273. buf->f_bsize = dentry->d_sb->s_blocksize;
  274. buf->f_type = BTRFS_SUPER_MAGIC;
  275. return 0;
  276. }
  277. static struct file_system_type btrfs_fs_type = {
  278. .owner = THIS_MODULE,
  279. .name = "btrfs",
  280. .get_sb = btrfs_get_sb,
  281. .kill_sb = kill_block_super,
  282. .fs_flags = FS_REQUIRES_DEV,
  283. };
  284. static struct super_operations btrfs_super_ops = {
  285. .delete_inode = btrfs_delete_inode,
  286. .put_super = btrfs_put_super,
  287. .read_inode = btrfs_read_locked_inode,
  288. .write_super = btrfs_write_super,
  289. .sync_fs = btrfs_sync_fs,
  290. .write_inode = btrfs_write_inode,
  291. .dirty_inode = btrfs_dirty_inode,
  292. .alloc_inode = btrfs_alloc_inode,
  293. .destroy_inode = btrfs_destroy_inode,
  294. .statfs = btrfs_statfs,
  295. };
  296. static int __init init_btrfs_fs(void)
  297. {
  298. int err;
  299. err = btrfs_init_sysfs();
  300. if (err)
  301. return err;
  302. btrfs_init_transaction_sys();
  303. err = btrfs_init_cachep();
  304. if (err)
  305. return err;
  306. extent_map_init();
  307. return register_filesystem(&btrfs_fs_type);
  308. }
  309. static void __exit exit_btrfs_fs(void)
  310. {
  311. btrfs_exit_transaction_sys();
  312. btrfs_destroy_cachep();
  313. extent_map_exit();
  314. unregister_filesystem(&btrfs_fs_type);
  315. btrfs_exit_sysfs();
  316. }
  317. module_init(init_btrfs_fs)
  318. module_exit(exit_btrfs_fs)
  319. MODULE_LICENSE("GPL");