super.c 13 KB

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