super.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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/seq_file.h>
  27. #include <linux/string.h>
  28. #include <linux/smp_lock.h>
  29. #include <linux/backing-dev.h>
  30. #include <linux/mount.h>
  31. #include <linux/mpage.h>
  32. #include <linux/swap.h>
  33. #include <linux/writeback.h>
  34. #include <linux/statfs.h>
  35. #include <linux/compat.h>
  36. #include <linux/parser.h>
  37. #include <linux/ctype.h>
  38. #include <linux/namei.h>
  39. #include <linux/miscdevice.h>
  40. #include <linux/magic.h>
  41. #include "compat.h"
  42. #include "ctree.h"
  43. #include "disk-io.h"
  44. #include "transaction.h"
  45. #include "btrfs_inode.h"
  46. #include "ioctl.h"
  47. #include "print-tree.h"
  48. #include "xattr.h"
  49. #include "volumes.h"
  50. #include "version.h"
  51. #include "export.h"
  52. #include "compression.h"
  53. static struct super_operations btrfs_super_ops;
  54. static void btrfs_put_super(struct super_block *sb)
  55. {
  56. struct btrfs_root *root = btrfs_sb(sb);
  57. int ret;
  58. ret = close_ctree(root);
  59. sb->s_fs_info = NULL;
  60. }
  61. enum {
  62. Opt_degraded, Opt_subvol, Opt_device, Opt_nodatasum, Opt_nodatacow,
  63. Opt_max_extent, Opt_max_inline, Opt_alloc_start, Opt_nobarrier,
  64. Opt_ssd, Opt_thread_pool, Opt_noacl, Opt_compress, Opt_notreelog,
  65. Opt_flushoncommit, 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_compress, "compress"},
  79. {Opt_ssd, "ssd"},
  80. {Opt_noacl, "noacl"},
  81. {Opt_notreelog, "notreelog"},
  82. {Opt_flushoncommit, "flushoncommit"},
  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. case Opt_notreelog:
  212. printk(KERN_INFO "btrfs: disabling tree log\n");
  213. btrfs_set_opt(info->mount_opt, NOTREELOG);
  214. break;
  215. case Opt_flushoncommit:
  216. printk(KERN_INFO "btrfs: turning on flush-on-commit\n");
  217. btrfs_set_opt(info->mount_opt, FLUSHONCOMMIT);
  218. break;
  219. default:
  220. break;
  221. }
  222. }
  223. kfree(options);
  224. return 0;
  225. }
  226. /*
  227. * Parse mount options that are required early in the mount process.
  228. *
  229. * All other options will be parsed on much later in the mount process and
  230. * only when we need to allocate a new super block.
  231. */
  232. static int btrfs_parse_early_options(const char *options, fmode_t flags,
  233. void *holder, char **subvol_name,
  234. struct btrfs_fs_devices **fs_devices)
  235. {
  236. substring_t args[MAX_OPT_ARGS];
  237. char *opts, *p;
  238. int error = 0;
  239. if (!options)
  240. goto out;
  241. /*
  242. * strsep changes the string, duplicate it because parse_options
  243. * gets called twice
  244. */
  245. opts = kstrdup(options, GFP_KERNEL);
  246. if (!opts)
  247. return -ENOMEM;
  248. while ((p = strsep(&opts, ",")) != NULL) {
  249. int token;
  250. if (!*p)
  251. continue;
  252. token = match_token(p, tokens, args);
  253. switch (token) {
  254. case Opt_subvol:
  255. *subvol_name = match_strdup(&args[0]);
  256. break;
  257. case Opt_device:
  258. error = btrfs_scan_one_device(match_strdup(&args[0]),
  259. flags, holder, fs_devices);
  260. if (error)
  261. goto out_free_opts;
  262. break;
  263. default:
  264. break;
  265. }
  266. }
  267. out_free_opts:
  268. kfree(opts);
  269. out:
  270. /*
  271. * If no subvolume name is specified we use the default one. Allocate
  272. * a copy of the string "." here so that code later in the
  273. * mount path doesn't care if it's the default volume or another one.
  274. */
  275. if (!*subvol_name) {
  276. *subvol_name = kstrdup(".", GFP_KERNEL);
  277. if (!*subvol_name)
  278. return -ENOMEM;
  279. }
  280. return error;
  281. }
  282. static int btrfs_fill_super(struct super_block *sb,
  283. struct btrfs_fs_devices *fs_devices,
  284. void *data, int silent)
  285. {
  286. struct inode *inode;
  287. struct dentry *root_dentry;
  288. struct btrfs_super_block *disk_super;
  289. struct btrfs_root *tree_root;
  290. struct btrfs_inode *bi;
  291. int err;
  292. sb->s_maxbytes = MAX_LFS_FILESIZE;
  293. sb->s_magic = BTRFS_SUPER_MAGIC;
  294. sb->s_op = &btrfs_super_ops;
  295. sb->s_export_op = &btrfs_export_ops;
  296. sb->s_xattr = btrfs_xattr_handlers;
  297. sb->s_time_gran = 1;
  298. sb->s_flags |= MS_POSIXACL;
  299. tree_root = open_ctree(sb, fs_devices, (char *)data);
  300. if (IS_ERR(tree_root)) {
  301. printk("btrfs: open_ctree failed\n");
  302. return PTR_ERR(tree_root);
  303. }
  304. sb->s_fs_info = tree_root;
  305. disk_super = &tree_root->fs_info->super_copy;
  306. inode = btrfs_iget_locked(sb, BTRFS_FIRST_FREE_OBJECTID,
  307. tree_root->fs_info->fs_root);
  308. bi = BTRFS_I(inode);
  309. bi->location.objectid = inode->i_ino;
  310. bi->location.offset = 0;
  311. bi->root = tree_root->fs_info->fs_root;
  312. btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
  313. if (!inode) {
  314. err = -ENOMEM;
  315. goto fail_close;
  316. }
  317. if (inode->i_state & I_NEW) {
  318. btrfs_read_locked_inode(inode);
  319. unlock_new_inode(inode);
  320. }
  321. root_dentry = d_alloc_root(inode);
  322. if (!root_dentry) {
  323. iput(inode);
  324. err = -ENOMEM;
  325. goto fail_close;
  326. }
  327. #if 0
  328. /* this does the super kobj at the same time */
  329. err = btrfs_sysfs_add_super(tree_root->fs_info);
  330. if (err)
  331. goto fail_close;
  332. #endif
  333. sb->s_root = root_dentry;
  334. save_mount_options(sb, data);
  335. return 0;
  336. fail_close:
  337. close_ctree(tree_root);
  338. return err;
  339. }
  340. int btrfs_sync_fs(struct super_block *sb, int wait)
  341. {
  342. struct btrfs_trans_handle *trans;
  343. struct btrfs_root *root = btrfs_sb(sb);
  344. int ret;
  345. if (sb->s_flags & MS_RDONLY)
  346. return 0;
  347. sb->s_dirt = 0;
  348. if (!wait) {
  349. filemap_flush(root->fs_info->btree_inode->i_mapping);
  350. return 0;
  351. }
  352. btrfs_start_delalloc_inodes(root);
  353. btrfs_wait_ordered_extents(root, 0);
  354. trans = btrfs_start_transaction(root, 1);
  355. ret = btrfs_commit_transaction(trans, root);
  356. sb->s_dirt = 0;
  357. return ret;
  358. }
  359. static int btrfs_show_options(struct seq_file *seq, struct vfsmount *vfs)
  360. {
  361. struct btrfs_root *root = btrfs_sb(vfs->mnt_sb);
  362. struct btrfs_fs_info *info = root->fs_info;
  363. if (btrfs_test_opt(root, DEGRADED))
  364. seq_puts(seq, ",degraded");
  365. if (btrfs_test_opt(root, NODATASUM))
  366. seq_puts(seq, ",nodatasum");
  367. if (btrfs_test_opt(root, NODATACOW))
  368. seq_puts(seq, ",nodatacow");
  369. if (btrfs_test_opt(root, NOBARRIER))
  370. seq_puts(seq, ",nobarrier");
  371. if (info->max_extent != (u64)-1)
  372. seq_printf(seq, ",max_extent=%llu", info->max_extent);
  373. if (info->max_inline != 8192 * 1024)
  374. seq_printf(seq, ",max_inline=%llu", info->max_inline);
  375. if (info->alloc_start != 0)
  376. seq_printf(seq, ",alloc_start=%llu", info->alloc_start);
  377. if (info->thread_pool_size != min_t(unsigned long,
  378. num_online_cpus() + 2, 8))
  379. seq_printf(seq, ",thread_pool=%d", info->thread_pool_size);
  380. if (btrfs_test_opt(root, COMPRESS))
  381. seq_puts(seq, ",compress");
  382. if (btrfs_test_opt(root, SSD))
  383. seq_puts(seq, ",ssd");
  384. if (btrfs_test_opt(root, NOTREELOG))
  385. seq_puts(seq, ",no-treelog");
  386. if (btrfs_test_opt(root, FLUSHONCOMMIT))
  387. seq_puts(seq, ",flush-on-commit");
  388. if (!(root->fs_info->sb->s_flags & MS_POSIXACL))
  389. seq_puts(seq, ",noacl");
  390. return 0;
  391. }
  392. static void btrfs_write_super(struct super_block *sb)
  393. {
  394. sb->s_dirt = 0;
  395. }
  396. static int btrfs_test_super(struct super_block *s, void *data)
  397. {
  398. struct btrfs_fs_devices *test_fs_devices = data;
  399. struct btrfs_root *root = btrfs_sb(s);
  400. return root->fs_info->fs_devices == test_fs_devices;
  401. }
  402. /*
  403. * Find a superblock for the given device / mount point.
  404. *
  405. * Note: This is based on get_sb_bdev from fs/super.c with a few additions
  406. * for multiple device setup. Make sure to keep it in sync.
  407. */
  408. static int btrfs_get_sb(struct file_system_type *fs_type, int flags,
  409. const char *dev_name, void *data, struct vfsmount *mnt)
  410. {
  411. char *subvol_name = NULL;
  412. struct block_device *bdev = NULL;
  413. struct super_block *s;
  414. struct dentry *root;
  415. struct btrfs_fs_devices *fs_devices = NULL;
  416. fmode_t mode = FMODE_READ;
  417. int error = 0;
  418. if (!(flags & MS_RDONLY))
  419. mode |= FMODE_WRITE;
  420. error = btrfs_parse_early_options(data, mode, fs_type,
  421. &subvol_name, &fs_devices);
  422. if (error)
  423. return error;
  424. error = btrfs_scan_one_device(dev_name, mode, fs_type, &fs_devices);
  425. if (error)
  426. goto error_free_subvol_name;
  427. error = btrfs_open_devices(fs_devices, mode, fs_type);
  428. if (error)
  429. goto error_free_subvol_name;
  430. if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) {
  431. error = -EACCES;
  432. goto error_close_devices;
  433. }
  434. bdev = fs_devices->latest_bdev;
  435. s = sget(fs_type, btrfs_test_super, set_anon_super, fs_devices);
  436. if (IS_ERR(s))
  437. goto error_s;
  438. if (s->s_root) {
  439. if ((flags ^ s->s_flags) & MS_RDONLY) {
  440. up_write(&s->s_umount);
  441. deactivate_super(s);
  442. error = -EBUSY;
  443. goto error_close_devices;
  444. }
  445. btrfs_close_devices(fs_devices);
  446. } else {
  447. char b[BDEVNAME_SIZE];
  448. s->s_flags = flags;
  449. strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
  450. error = btrfs_fill_super(s, fs_devices, data,
  451. flags & MS_SILENT ? 1 : 0);
  452. if (error) {
  453. up_write(&s->s_umount);
  454. deactivate_super(s);
  455. goto error_free_subvol_name;
  456. }
  457. btrfs_sb(s)->fs_info->bdev_holder = fs_type;
  458. s->s_flags |= MS_ACTIVE;
  459. }
  460. if (!strcmp(subvol_name, "."))
  461. root = dget(s->s_root);
  462. else {
  463. mutex_lock(&s->s_root->d_inode->i_mutex);
  464. root = lookup_one_len(subvol_name, s->s_root,
  465. strlen(subvol_name));
  466. mutex_unlock(&s->s_root->d_inode->i_mutex);
  467. if (IS_ERR(root)) {
  468. up_write(&s->s_umount);
  469. deactivate_super(s);
  470. error = PTR_ERR(root);
  471. goto error_free_subvol_name;
  472. }
  473. if (!root->d_inode) {
  474. dput(root);
  475. up_write(&s->s_umount);
  476. deactivate_super(s);
  477. error = -ENXIO;
  478. goto error_free_subvol_name;
  479. }
  480. }
  481. mnt->mnt_sb = s;
  482. mnt->mnt_root = root;
  483. kfree(subvol_name);
  484. return 0;
  485. error_s:
  486. error = PTR_ERR(s);
  487. error_close_devices:
  488. btrfs_close_devices(fs_devices);
  489. error_free_subvol_name:
  490. kfree(subvol_name);
  491. return error;
  492. }
  493. static int btrfs_remount(struct super_block *sb, int *flags, char *data)
  494. {
  495. struct btrfs_root *root = btrfs_sb(sb);
  496. int ret;
  497. ret = btrfs_parse_options(root, data);
  498. if (ret)
  499. return -EINVAL;
  500. if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
  501. return 0;
  502. if (*flags & MS_RDONLY) {
  503. sb->s_flags |= MS_RDONLY;
  504. ret = btrfs_commit_super(root);
  505. WARN_ON(ret);
  506. } else {
  507. if (root->fs_info->fs_devices->rw_devices == 0)
  508. return -EACCES;
  509. if (btrfs_super_log_root(&root->fs_info->super_copy) != 0)
  510. return -EINVAL;
  511. ret = btrfs_cleanup_reloc_trees(root);
  512. WARN_ON(ret);
  513. ret = btrfs_cleanup_fs_roots(root->fs_info);
  514. WARN_ON(ret);
  515. sb->s_flags &= ~MS_RDONLY;
  516. }
  517. return 0;
  518. }
  519. static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  520. {
  521. struct btrfs_root *root = btrfs_sb(dentry->d_sb);
  522. struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
  523. int bits = dentry->d_sb->s_blocksize_bits;
  524. __be32 *fsid = (__be32 *)root->fs_info->fsid;
  525. buf->f_namelen = BTRFS_NAME_LEN;
  526. buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
  527. buf->f_bfree = buf->f_blocks -
  528. (btrfs_super_bytes_used(disk_super) >> bits);
  529. buf->f_bavail = buf->f_bfree;
  530. buf->f_bsize = dentry->d_sb->s_blocksize;
  531. buf->f_type = BTRFS_SUPER_MAGIC;
  532. /* We treat it as constant endianness (it doesn't matter _which_)
  533. because we want the fsid to come out the same whether mounted
  534. on a big-endian or little-endian host */
  535. buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
  536. buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
  537. /* Mask in the root object ID too, to disambiguate subvols */
  538. buf->f_fsid.val[0] ^= BTRFS_I(dentry->d_inode)->root->objectid >> 32;
  539. buf->f_fsid.val[1] ^= BTRFS_I(dentry->d_inode)->root->objectid;
  540. return 0;
  541. }
  542. static struct file_system_type btrfs_fs_type = {
  543. .owner = THIS_MODULE,
  544. .name = "btrfs",
  545. .get_sb = btrfs_get_sb,
  546. .kill_sb = kill_anon_super,
  547. .fs_flags = FS_REQUIRES_DEV,
  548. };
  549. /*
  550. * used by btrfsctl to scan devices when no FS is mounted
  551. */
  552. static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
  553. unsigned long arg)
  554. {
  555. struct btrfs_ioctl_vol_args *vol;
  556. struct btrfs_fs_devices *fs_devices;
  557. int ret = -ENOTTY;
  558. if (!capable(CAP_SYS_ADMIN))
  559. return -EPERM;
  560. vol = kmalloc(sizeof(*vol), GFP_KERNEL);
  561. if (!vol)
  562. return -ENOMEM;
  563. if (copy_from_user(vol, (void __user *)arg, sizeof(*vol))) {
  564. ret = -EFAULT;
  565. goto out;
  566. }
  567. switch (cmd) {
  568. case BTRFS_IOC_SCAN_DEV:
  569. ret = btrfs_scan_one_device(vol->name, FMODE_READ,
  570. &btrfs_fs_type, &fs_devices);
  571. break;
  572. }
  573. out:
  574. kfree(vol);
  575. return ret;
  576. }
  577. static int btrfs_freeze(struct super_block *sb)
  578. {
  579. struct btrfs_root *root = btrfs_sb(sb);
  580. mutex_lock(&root->fs_info->transaction_kthread_mutex);
  581. mutex_lock(&root->fs_info->cleaner_mutex);
  582. return 0;
  583. }
  584. static int btrfs_unfreeze(struct super_block *sb)
  585. {
  586. struct btrfs_root *root = btrfs_sb(sb);
  587. mutex_unlock(&root->fs_info->cleaner_mutex);
  588. mutex_unlock(&root->fs_info->transaction_kthread_mutex);
  589. return 0;
  590. }
  591. static struct super_operations btrfs_super_ops = {
  592. .delete_inode = btrfs_delete_inode,
  593. .put_super = btrfs_put_super,
  594. .write_super = btrfs_write_super,
  595. .sync_fs = btrfs_sync_fs,
  596. .show_options = btrfs_show_options,
  597. .write_inode = btrfs_write_inode,
  598. .dirty_inode = btrfs_dirty_inode,
  599. .alloc_inode = btrfs_alloc_inode,
  600. .destroy_inode = btrfs_destroy_inode,
  601. .statfs = btrfs_statfs,
  602. .remount_fs = btrfs_remount,
  603. .freeze_fs = btrfs_freeze,
  604. .unfreeze_fs = btrfs_unfreeze,
  605. };
  606. static const struct file_operations btrfs_ctl_fops = {
  607. .unlocked_ioctl = btrfs_control_ioctl,
  608. .compat_ioctl = btrfs_control_ioctl,
  609. .owner = THIS_MODULE,
  610. };
  611. static struct miscdevice btrfs_misc = {
  612. .minor = MISC_DYNAMIC_MINOR,
  613. .name = "btrfs-control",
  614. .fops = &btrfs_ctl_fops
  615. };
  616. static int btrfs_interface_init(void)
  617. {
  618. return misc_register(&btrfs_misc);
  619. }
  620. static void btrfs_interface_exit(void)
  621. {
  622. if (misc_deregister(&btrfs_misc) < 0)
  623. printk(KERN_INFO "misc_deregister failed for control device");
  624. }
  625. static int __init init_btrfs_fs(void)
  626. {
  627. int err;
  628. err = btrfs_init_sysfs();
  629. if (err)
  630. return err;
  631. err = btrfs_init_cachep();
  632. if (err)
  633. goto free_sysfs;
  634. err = extent_io_init();
  635. if (err)
  636. goto free_cachep;
  637. err = extent_map_init();
  638. if (err)
  639. goto free_extent_io;
  640. err = btrfs_interface_init();
  641. if (err)
  642. goto free_extent_map;
  643. err = register_filesystem(&btrfs_fs_type);
  644. if (err)
  645. goto unregister_ioctl;
  646. printk(KERN_INFO "%s loaded\n", BTRFS_BUILD_VERSION);
  647. return 0;
  648. unregister_ioctl:
  649. btrfs_interface_exit();
  650. free_extent_map:
  651. extent_map_exit();
  652. free_extent_io:
  653. extent_io_exit();
  654. free_cachep:
  655. btrfs_destroy_cachep();
  656. free_sysfs:
  657. btrfs_exit_sysfs();
  658. return err;
  659. }
  660. static void __exit exit_btrfs_fs(void)
  661. {
  662. btrfs_destroy_cachep();
  663. extent_map_exit();
  664. extent_io_exit();
  665. btrfs_interface_exit();
  666. unregister_filesystem(&btrfs_fs_type);
  667. btrfs_exit_sysfs();
  668. btrfs_cleanup_fs_uuids();
  669. btrfs_zlib_exit();
  670. }
  671. module_init(init_btrfs_fs)
  672. module_exit(exit_btrfs_fs)
  673. MODULE_LICENSE("GPL");