super.c 15 KB

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