super.c 10 KB

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