super.c 15 KB

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