super.c 9.2 KB

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