super.c 14 KB

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