super.c 12 KB

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