super.c 13 KB

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