super.c 14 KB

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