super.c 13 KB

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