super.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. static unsigned long parse_size(char *str)
  70. {
  71. unsigned long 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 = parse_size(num);
  138. kfree(num);
  139. info->max_extent = max_t(u64,
  140. info->max_extent,
  141. root->sectorsize);
  142. printk("btrfs: max_extent at %Lu\n",
  143. info->max_extent);
  144. }
  145. }
  146. break;
  147. default:
  148. break;
  149. }
  150. }
  151. kfree(options);
  152. return 1;
  153. }
  154. static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
  155. {
  156. struct inode * inode;
  157. struct dentry * root_dentry;
  158. struct btrfs_super_block *disk_super;
  159. struct btrfs_root *tree_root;
  160. struct btrfs_inode *bi;
  161. int err;
  162. sb->s_maxbytes = MAX_LFS_FILESIZE;
  163. sb->s_magic = BTRFS_SUPER_MAGIC;
  164. sb->s_op = &btrfs_super_ops;
  165. sb->s_xattr = btrfs_xattr_handlers;
  166. sb->s_time_gran = 1;
  167. tree_root = open_ctree(sb);
  168. if (!tree_root || IS_ERR(tree_root)) {
  169. printk("btrfs: open_ctree failed\n");
  170. return -EIO;
  171. }
  172. sb->s_fs_info = tree_root;
  173. disk_super = &tree_root->fs_info->super_copy;
  174. inode = btrfs_iget_locked(sb, btrfs_super_root_dir(disk_super),
  175. tree_root);
  176. bi = BTRFS_I(inode);
  177. bi->location.objectid = inode->i_ino;
  178. bi->location.offset = 0;
  179. bi->root = tree_root;
  180. btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
  181. if (!inode) {
  182. err = -ENOMEM;
  183. goto fail_close;
  184. }
  185. if (inode->i_state & I_NEW) {
  186. btrfs_read_locked_inode(inode);
  187. unlock_new_inode(inode);
  188. }
  189. root_dentry = d_alloc_root(inode);
  190. if (!root_dentry) {
  191. iput(inode);
  192. err = -ENOMEM;
  193. goto fail_close;
  194. }
  195. parse_options((char *)data, tree_root, NULL);
  196. /* this does the super kobj at the same time */
  197. err = btrfs_sysfs_add_super(tree_root->fs_info);
  198. if (err)
  199. goto fail_close;
  200. sb->s_root = root_dentry;
  201. btrfs_transaction_queue_work(tree_root, HZ * 30);
  202. return 0;
  203. fail_close:
  204. close_ctree(tree_root);
  205. return err;
  206. }
  207. static int btrfs_sync_fs(struct super_block *sb, int wait)
  208. {
  209. struct btrfs_trans_handle *trans;
  210. struct btrfs_root *root;
  211. int ret;
  212. root = btrfs_sb(sb);
  213. sb->s_dirt = 0;
  214. if (!wait) {
  215. filemap_flush(root->fs_info->btree_inode->i_mapping);
  216. return 0;
  217. }
  218. btrfs_clean_old_snapshots(root);
  219. mutex_lock(&root->fs_info->fs_mutex);
  220. btrfs_defrag_dirty_roots(root->fs_info);
  221. trans = btrfs_start_transaction(root, 1);
  222. ret = btrfs_commit_transaction(trans, root);
  223. sb->s_dirt = 0;
  224. mutex_unlock(&root->fs_info->fs_mutex);
  225. return ret;
  226. }
  227. static void btrfs_write_super(struct super_block *sb)
  228. {
  229. sb->s_dirt = 0;
  230. }
  231. /*
  232. * This is almost a copy of get_sb_bdev in fs/super.c.
  233. * We need the local copy to allow direct mounting of
  234. * subvolumes, but this could be easily integrated back
  235. * into the generic version. --hch
  236. */
  237. /* start copy & paste */
  238. static int set_bdev_super(struct super_block *s, void *data)
  239. {
  240. s->s_bdev = data;
  241. s->s_dev = s->s_bdev->bd_dev;
  242. return 0;
  243. }
  244. static int test_bdev_super(struct super_block *s, void *data)
  245. {
  246. return (void *)s->s_bdev == data;
  247. }
  248. int btrfs_get_sb_bdev(struct file_system_type *fs_type,
  249. int flags, const char *dev_name, void *data,
  250. int (*fill_super)(struct super_block *, void *, int),
  251. struct vfsmount *mnt, const char *subvol)
  252. {
  253. struct block_device *bdev = NULL;
  254. struct super_block *s;
  255. struct dentry *root;
  256. int error = 0;
  257. bdev = open_bdev_excl(dev_name, flags, fs_type);
  258. if (IS_ERR(bdev))
  259. return PTR_ERR(bdev);
  260. /*
  261. * once the super is inserted into the list by sget, s_umount
  262. * will protect the lockfs code from trying to start a snapshot
  263. * while we are mounting
  264. */
  265. down(&bdev->bd_mount_sem);
  266. s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
  267. up(&bdev->bd_mount_sem);
  268. if (IS_ERR(s))
  269. goto error_s;
  270. if (s->s_root) {
  271. if ((flags ^ s->s_flags) & MS_RDONLY) {
  272. up_write(&s->s_umount);
  273. deactivate_super(s);
  274. error = -EBUSY;
  275. goto error_bdev;
  276. }
  277. close_bdev_excl(bdev);
  278. } else {
  279. char b[BDEVNAME_SIZE];
  280. s->s_flags = flags;
  281. strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
  282. sb_set_blocksize(s, block_size(bdev));
  283. error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  284. if (error) {
  285. up_write(&s->s_umount);
  286. deactivate_super(s);
  287. goto error;
  288. }
  289. s->s_flags |= MS_ACTIVE;
  290. }
  291. if (subvol) {
  292. root = lookup_one_len(subvol, s->s_root, strlen(subvol));
  293. if (IS_ERR(root)) {
  294. up_write(&s->s_umount);
  295. deactivate_super(s);
  296. error = PTR_ERR(root);
  297. goto error;
  298. }
  299. if (!root->d_inode) {
  300. dput(root);
  301. up_write(&s->s_umount);
  302. deactivate_super(s);
  303. error = -ENXIO;
  304. goto error;
  305. }
  306. } else {
  307. root = dget(s->s_root);
  308. }
  309. mnt->mnt_sb = s;
  310. mnt->mnt_root = root;
  311. return 0;
  312. error_s:
  313. error = PTR_ERR(s);
  314. error_bdev:
  315. close_bdev_excl(bdev);
  316. error:
  317. return error;
  318. }
  319. /* end copy & paste */
  320. static int btrfs_get_sb(struct file_system_type *fs_type,
  321. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  322. {
  323. int ret;
  324. char *subvol_name = NULL;
  325. parse_options((char *)data, NULL, &subvol_name);
  326. ret = btrfs_get_sb_bdev(fs_type, flags, dev_name, data,
  327. btrfs_fill_super, mnt,
  328. subvol_name ? subvol_name : "default");
  329. if (subvol_name)
  330. kfree(subvol_name);
  331. return ret;
  332. }
  333. static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  334. {
  335. struct btrfs_root *root = btrfs_sb(dentry->d_sb);
  336. struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
  337. int bits = dentry->d_sb->s_blocksize_bits;
  338. buf->f_namelen = BTRFS_NAME_LEN;
  339. buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
  340. buf->f_bfree = buf->f_blocks -
  341. (btrfs_super_bytes_used(disk_super) >> bits);
  342. buf->f_bavail = buf->f_bfree;
  343. buf->f_bsize = dentry->d_sb->s_blocksize;
  344. buf->f_type = BTRFS_SUPER_MAGIC;
  345. return 0;
  346. }
  347. static struct file_system_type btrfs_fs_type = {
  348. .owner = THIS_MODULE,
  349. .name = "btrfs",
  350. .get_sb = btrfs_get_sb,
  351. .kill_sb = kill_block_super,
  352. .fs_flags = FS_REQUIRES_DEV,
  353. };
  354. static struct super_operations btrfs_super_ops = {
  355. .delete_inode = btrfs_delete_inode,
  356. .put_super = btrfs_put_super,
  357. .read_inode = btrfs_read_locked_inode,
  358. .write_super = btrfs_write_super,
  359. .sync_fs = btrfs_sync_fs,
  360. .write_inode = btrfs_write_inode,
  361. .dirty_inode = btrfs_dirty_inode,
  362. .alloc_inode = btrfs_alloc_inode,
  363. .destroy_inode = btrfs_destroy_inode,
  364. .statfs = btrfs_statfs,
  365. };
  366. static int __init init_btrfs_fs(void)
  367. {
  368. int err;
  369. err = btrfs_init_sysfs();
  370. if (err)
  371. return err;
  372. btrfs_init_transaction_sys();
  373. err = btrfs_init_cachep();
  374. if (err)
  375. goto free_transaction_sys;
  376. err = extent_map_init();
  377. if (err)
  378. goto free_cachep;
  379. err = register_filesystem(&btrfs_fs_type);
  380. if (err)
  381. goto free_extent_map;
  382. return 0;
  383. free_extent_map:
  384. extent_map_exit();
  385. free_cachep:
  386. btrfs_destroy_cachep();
  387. free_transaction_sys:
  388. btrfs_exit_transaction_sys();
  389. btrfs_exit_sysfs();
  390. return err;
  391. }
  392. static void __exit exit_btrfs_fs(void)
  393. {
  394. btrfs_exit_transaction_sys();
  395. btrfs_destroy_cachep();
  396. extent_map_exit();
  397. unregister_filesystem(&btrfs_fs_type);
  398. btrfs_exit_sysfs();
  399. }
  400. module_init(init_btrfs_fs)
  401. module_exit(exit_btrfs_fs)
  402. MODULE_LICENSE("GPL");