super.c 10 KB

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