super.c 13 KB

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