super.c 11 KB

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