super.c 15 KB

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