super.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25)
  319. save_mount_options(sb, data);
  320. #endif
  321. return 0;
  322. fail_close:
  323. close_ctree(tree_root);
  324. return err;
  325. }
  326. int btrfs_sync_fs(struct super_block *sb, int wait)
  327. {
  328. struct btrfs_trans_handle *trans;
  329. struct btrfs_root *root;
  330. int ret;
  331. root = btrfs_sb(sb);
  332. sb->s_dirt = 0;
  333. if (!wait) {
  334. filemap_flush(root->fs_info->btree_inode->i_mapping);
  335. return 0;
  336. }
  337. btrfs_clean_old_snapshots(root);
  338. trans = btrfs_start_transaction(root, 1);
  339. ret = btrfs_commit_transaction(trans, root);
  340. sb->s_dirt = 0;
  341. return ret;
  342. }
  343. static void btrfs_write_super(struct super_block *sb)
  344. {
  345. sb->s_dirt = 0;
  346. }
  347. static int btrfs_test_super(struct super_block *s, void *data)
  348. {
  349. struct btrfs_fs_devices *test_fs_devices = data;
  350. struct btrfs_root *root = btrfs_sb(s);
  351. return root->fs_info->fs_devices == test_fs_devices;
  352. }
  353. /*
  354. * Find a superblock for the given device / mount point.
  355. *
  356. * Note: This is based on get_sb_bdev from fs/super.c with a few additions
  357. * for multiple device setup. Make sure to keep it in sync.
  358. */
  359. static int btrfs_get_sb(struct file_system_type *fs_type, int flags,
  360. const char *dev_name, void *data, struct vfsmount *mnt)
  361. {
  362. char *subvol_name = NULL;
  363. struct block_device *bdev = NULL;
  364. struct super_block *s;
  365. struct dentry *root;
  366. struct btrfs_fs_devices *fs_devices = NULL;
  367. int error = 0;
  368. error = btrfs_parse_early_options(data, flags, fs_type,
  369. &subvol_name, &fs_devices);
  370. if (error)
  371. goto error;
  372. error = btrfs_scan_one_device(dev_name, flags, fs_type, &fs_devices);
  373. if (error)
  374. goto error_free_subvol_name;
  375. error = btrfs_open_devices(fs_devices, flags, fs_type);
  376. if (error)
  377. goto error_free_subvol_name;
  378. bdev = fs_devices->latest_bdev;
  379. s = sget(fs_type, btrfs_test_super, set_anon_super, fs_devices);
  380. if (IS_ERR(s))
  381. goto error_s;
  382. if (s->s_root) {
  383. if ((flags ^ s->s_flags) & MS_RDONLY) {
  384. up_write(&s->s_umount);
  385. deactivate_super(s);
  386. error = -EBUSY;
  387. goto error_bdev;
  388. }
  389. } else {
  390. char b[BDEVNAME_SIZE];
  391. s->s_flags = flags;
  392. strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
  393. error = btrfs_fill_super(s, fs_devices, data,
  394. flags & MS_SILENT ? 1 : 0);
  395. if (error) {
  396. up_write(&s->s_umount);
  397. deactivate_super(s);
  398. goto error;
  399. }
  400. btrfs_sb(s)->fs_info->bdev_holder = fs_type;
  401. s->s_flags |= MS_ACTIVE;
  402. }
  403. if (!strcmp(subvol_name, "."))
  404. root = dget(s->s_root);
  405. else {
  406. mutex_lock(&s->s_root->d_inode->i_mutex);
  407. root = lookup_one_len(subvol_name, s->s_root, strlen(subvol_name));
  408. mutex_unlock(&s->s_root->d_inode->i_mutex);
  409. if (IS_ERR(root)) {
  410. up_write(&s->s_umount);
  411. deactivate_super(s);
  412. error = PTR_ERR(root);
  413. goto error;
  414. }
  415. if (!root->d_inode) {
  416. dput(root);
  417. up_write(&s->s_umount);
  418. deactivate_super(s);
  419. error = -ENXIO;
  420. goto error;
  421. }
  422. }
  423. mnt->mnt_sb = s;
  424. mnt->mnt_root = root;
  425. kfree(subvol_name);
  426. return 0;
  427. error_s:
  428. error = PTR_ERR(s);
  429. error_bdev:
  430. btrfs_close_devices(fs_devices);
  431. error_free_subvol_name:
  432. kfree(subvol_name);
  433. error:
  434. return error;
  435. }
  436. static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  437. {
  438. struct btrfs_root *root = btrfs_sb(dentry->d_sb);
  439. struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
  440. int bits = dentry->d_sb->s_blocksize_bits;
  441. __be32 *fsid = (__be32 *)root->fs_info->fsid;
  442. buf->f_namelen = BTRFS_NAME_LEN;
  443. buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
  444. buf->f_bfree = buf->f_blocks -
  445. (btrfs_super_bytes_used(disk_super) >> bits);
  446. buf->f_bavail = buf->f_bfree;
  447. buf->f_bsize = dentry->d_sb->s_blocksize;
  448. buf->f_type = BTRFS_SUPER_MAGIC;
  449. /* We treat it as constant endianness (it doesn't matter _which_)
  450. because we want the fsid to come out the same whether mounted
  451. on a big-endian or little-endian host */
  452. buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
  453. buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
  454. /* Mask in the root object ID too, to disambiguate subvols */
  455. buf->f_fsid.val[0] ^= BTRFS_I(dentry->d_inode)->root->objectid >> 32;
  456. buf->f_fsid.val[1] ^= BTRFS_I(dentry->d_inode)->root->objectid;
  457. return 0;
  458. }
  459. static struct file_system_type btrfs_fs_type = {
  460. .owner = THIS_MODULE,
  461. .name = "btrfs",
  462. .get_sb = btrfs_get_sb,
  463. .kill_sb = kill_anon_super,
  464. .fs_flags = FS_REQUIRES_DEV,
  465. };
  466. static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
  467. unsigned long arg)
  468. {
  469. struct btrfs_ioctl_vol_args *vol;
  470. struct btrfs_fs_devices *fs_devices;
  471. int ret = 0;
  472. int len;
  473. vol = kmalloc(sizeof(*vol), GFP_KERNEL);
  474. if (copy_from_user(vol, (void __user *)arg, sizeof(*vol))) {
  475. ret = -EFAULT;
  476. goto out;
  477. }
  478. len = strnlen(vol->name, BTRFS_PATH_NAME_MAX);
  479. switch (cmd) {
  480. case BTRFS_IOC_SCAN_DEV:
  481. ret = btrfs_scan_one_device(vol->name, MS_RDONLY,
  482. &btrfs_fs_type, &fs_devices);
  483. break;
  484. }
  485. out:
  486. kfree(vol);
  487. return ret;
  488. }
  489. static void btrfs_write_super_lockfs(struct super_block *sb)
  490. {
  491. struct btrfs_root *root = btrfs_sb(sb);
  492. mutex_lock(&root->fs_info->transaction_kthread_mutex);
  493. mutex_lock(&root->fs_info->cleaner_mutex);
  494. }
  495. static void btrfs_unlockfs(struct super_block *sb)
  496. {
  497. struct btrfs_root *root = btrfs_sb(sb);
  498. mutex_unlock(&root->fs_info->cleaner_mutex);
  499. mutex_unlock(&root->fs_info->transaction_kthread_mutex);
  500. }
  501. static struct super_operations btrfs_super_ops = {
  502. .delete_inode = btrfs_delete_inode,
  503. .put_super = btrfs_put_super,
  504. .write_super = btrfs_write_super,
  505. .sync_fs = btrfs_sync_fs,
  506. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
  507. .read_inode = btrfs_read_locked_inode,
  508. #else
  509. .show_options = generic_show_options,
  510. #endif
  511. .write_inode = btrfs_write_inode,
  512. .dirty_inode = btrfs_dirty_inode,
  513. .alloc_inode = btrfs_alloc_inode,
  514. .destroy_inode = btrfs_destroy_inode,
  515. .statfs = btrfs_statfs,
  516. .write_super_lockfs = btrfs_write_super_lockfs,
  517. .unlockfs = btrfs_unlockfs,
  518. };
  519. static const struct file_operations btrfs_ctl_fops = {
  520. .unlocked_ioctl = btrfs_control_ioctl,
  521. .compat_ioctl = btrfs_control_ioctl,
  522. .owner = THIS_MODULE,
  523. };
  524. static struct miscdevice btrfs_misc = {
  525. .minor = MISC_DYNAMIC_MINOR,
  526. .name = "btrfs-control",
  527. .fops = &btrfs_ctl_fops
  528. };
  529. static int btrfs_interface_init(void)
  530. {
  531. return misc_register(&btrfs_misc);
  532. }
  533. void btrfs_interface_exit(void)
  534. {
  535. if (misc_deregister(&btrfs_misc) < 0)
  536. printk("misc_deregister failed for control device");
  537. }
  538. static int __init init_btrfs_fs(void)
  539. {
  540. int err;
  541. err = btrfs_init_sysfs();
  542. if (err)
  543. return err;
  544. err = btrfs_init_cachep();
  545. if (err)
  546. goto free_sysfs;
  547. err = extent_io_init();
  548. if (err)
  549. goto free_cachep;
  550. err = extent_map_init();
  551. if (err)
  552. goto free_extent_io;
  553. err = btrfs_interface_init();
  554. if (err)
  555. goto free_extent_map;
  556. err = register_filesystem(&btrfs_fs_type);
  557. if (err)
  558. goto unregister_ioctl;
  559. printk(KERN_INFO "%s loaded\n", BTRFS_BUILD_VERSION);
  560. return 0;
  561. unregister_ioctl:
  562. btrfs_interface_exit();
  563. free_extent_map:
  564. extent_map_exit();
  565. free_extent_io:
  566. extent_io_exit();
  567. free_cachep:
  568. btrfs_destroy_cachep();
  569. free_sysfs:
  570. btrfs_exit_sysfs();
  571. return err;
  572. }
  573. static void __exit exit_btrfs_fs(void)
  574. {
  575. btrfs_destroy_cachep();
  576. extent_map_exit();
  577. extent_io_exit();
  578. btrfs_interface_exit();
  579. unregister_filesystem(&btrfs_fs_type);
  580. btrfs_exit_sysfs();
  581. btrfs_cleanup_fs_uuids();
  582. }
  583. module_init(init_btrfs_fs)
  584. module_exit(exit_btrfs_fs)
  585. MODULE_LICENSE("GPL");