super.c 14 KB

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