super.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  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_nossd, Opt_thread_pool, Opt_noacl, Opt_compress,
  65. Opt_notreelog, Opt_ratio, 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_nossd, "nossd"},
  81. {Opt_noacl, "noacl"},
  82. {Opt_notreelog, "notreelog"},
  83. {Opt_flushoncommit, "flushoncommit"},
  84. {Opt_ratio, "metadata_ratio=%d"},
  85. {Opt_err, NULL},
  86. };
  87. u64 btrfs_parse_size(char *str)
  88. {
  89. u64 res;
  90. int mult = 1;
  91. char *end;
  92. char last;
  93. res = simple_strtoul(str, &end, 10);
  94. last = end[0];
  95. if (isalpha(last)) {
  96. last = tolower(last);
  97. switch (last) {
  98. case 'g':
  99. mult *= 1024;
  100. case 'm':
  101. mult *= 1024;
  102. case 'k':
  103. mult *= 1024;
  104. }
  105. res = res * mult;
  106. }
  107. return res;
  108. }
  109. /*
  110. * Regular mount options parser. Everything that is needed only when
  111. * reading in a new superblock is parsed here.
  112. */
  113. int btrfs_parse_options(struct btrfs_root *root, char *options)
  114. {
  115. struct btrfs_fs_info *info = root->fs_info;
  116. substring_t args[MAX_OPT_ARGS];
  117. char *p, *num;
  118. int intarg;
  119. if (!options)
  120. return 0;
  121. /*
  122. * strsep changes the string, duplicate it because parse_options
  123. * gets called twice
  124. */
  125. options = kstrdup(options, GFP_NOFS);
  126. if (!options)
  127. return -ENOMEM;
  128. while ((p = strsep(&options, ",")) != NULL) {
  129. int token;
  130. if (!*p)
  131. continue;
  132. token = match_token(p, tokens, args);
  133. switch (token) {
  134. case Opt_degraded:
  135. printk(KERN_INFO "btrfs: allowing degraded mounts\n");
  136. btrfs_set_opt(info->mount_opt, DEGRADED);
  137. break;
  138. case Opt_subvol:
  139. case Opt_device:
  140. /*
  141. * These are parsed by btrfs_parse_early_options
  142. * and can be happily ignored here.
  143. */
  144. break;
  145. case Opt_nodatasum:
  146. printk(KERN_INFO "btrfs: setting nodatacsum\n");
  147. btrfs_set_opt(info->mount_opt, NODATASUM);
  148. break;
  149. case Opt_nodatacow:
  150. printk(KERN_INFO "btrfs: setting nodatacow\n");
  151. btrfs_set_opt(info->mount_opt, NODATACOW);
  152. btrfs_set_opt(info->mount_opt, NODATASUM);
  153. break;
  154. case Opt_compress:
  155. printk(KERN_INFO "btrfs: use compression\n");
  156. btrfs_set_opt(info->mount_opt, COMPRESS);
  157. break;
  158. case Opt_ssd:
  159. printk(KERN_INFO "btrfs: use ssd allocation scheme\n");
  160. btrfs_set_opt(info->mount_opt, SSD);
  161. break;
  162. case Opt_nossd:
  163. printk(KERN_INFO "btrfs: not using ssd allocation scheme\n");
  164. btrfs_clear_opt(info->mount_opt, SSD);
  165. break;
  166. case Opt_nobarrier:
  167. printk(KERN_INFO "btrfs: turning off barriers\n");
  168. btrfs_set_opt(info->mount_opt, NOBARRIER);
  169. break;
  170. case Opt_thread_pool:
  171. intarg = 0;
  172. match_int(&args[0], &intarg);
  173. if (intarg) {
  174. info->thread_pool_size = intarg;
  175. printk(KERN_INFO "btrfs: thread pool %d\n",
  176. info->thread_pool_size);
  177. }
  178. break;
  179. case Opt_max_extent:
  180. num = match_strdup(&args[0]);
  181. if (num) {
  182. info->max_extent = btrfs_parse_size(num);
  183. kfree(num);
  184. info->max_extent = max_t(u64,
  185. info->max_extent, root->sectorsize);
  186. printk(KERN_INFO "btrfs: max_extent at %llu\n",
  187. (unsigned long long)info->max_extent);
  188. }
  189. break;
  190. case Opt_max_inline:
  191. num = match_strdup(&args[0]);
  192. if (num) {
  193. info->max_inline = btrfs_parse_size(num);
  194. kfree(num);
  195. if (info->max_inline) {
  196. info->max_inline = max_t(u64,
  197. info->max_inline,
  198. root->sectorsize);
  199. }
  200. printk(KERN_INFO "btrfs: max_inline at %llu\n",
  201. (unsigned long long)info->max_inline);
  202. }
  203. break;
  204. case Opt_alloc_start:
  205. num = match_strdup(&args[0]);
  206. if (num) {
  207. info->alloc_start = btrfs_parse_size(num);
  208. kfree(num);
  209. printk(KERN_INFO
  210. "btrfs: allocations start at %llu\n",
  211. (unsigned long long)info->alloc_start);
  212. }
  213. break;
  214. case Opt_noacl:
  215. root->fs_info->sb->s_flags &= ~MS_POSIXACL;
  216. break;
  217. case Opt_notreelog:
  218. printk(KERN_INFO "btrfs: disabling tree log\n");
  219. btrfs_set_opt(info->mount_opt, NOTREELOG);
  220. break;
  221. case Opt_flushoncommit:
  222. printk(KERN_INFO "btrfs: turning on flush-on-commit\n");
  223. btrfs_set_opt(info->mount_opt, FLUSHONCOMMIT);
  224. break;
  225. case Opt_ratio:
  226. intarg = 0;
  227. match_int(&args[0], &intarg);
  228. if (intarg) {
  229. info->metadata_ratio = intarg;
  230. printk(KERN_INFO "btrfs: metadata ratio %d\n",
  231. info->metadata_ratio);
  232. }
  233. break;
  234. default:
  235. break;
  236. }
  237. }
  238. kfree(options);
  239. return 0;
  240. }
  241. /*
  242. * Parse mount options that are required early in the mount process.
  243. *
  244. * All other options will be parsed on much later in the mount process and
  245. * only when we need to allocate a new super block.
  246. */
  247. static int btrfs_parse_early_options(const char *options, fmode_t flags,
  248. void *holder, char **subvol_name,
  249. struct btrfs_fs_devices **fs_devices)
  250. {
  251. substring_t args[MAX_OPT_ARGS];
  252. char *opts, *p;
  253. int error = 0;
  254. if (!options)
  255. goto out;
  256. /*
  257. * strsep changes the string, duplicate it because parse_options
  258. * gets called twice
  259. */
  260. opts = kstrdup(options, GFP_KERNEL);
  261. if (!opts)
  262. return -ENOMEM;
  263. while ((p = strsep(&opts, ",")) != NULL) {
  264. int token;
  265. if (!*p)
  266. continue;
  267. token = match_token(p, tokens, args);
  268. switch (token) {
  269. case Opt_subvol:
  270. *subvol_name = match_strdup(&args[0]);
  271. break;
  272. case Opt_device:
  273. error = btrfs_scan_one_device(match_strdup(&args[0]),
  274. flags, holder, fs_devices);
  275. if (error)
  276. goto out_free_opts;
  277. break;
  278. default:
  279. break;
  280. }
  281. }
  282. out_free_opts:
  283. kfree(opts);
  284. out:
  285. /*
  286. * If no subvolume name is specified we use the default one. Allocate
  287. * a copy of the string "." here so that code later in the
  288. * mount path doesn't care if it's the default volume or another one.
  289. */
  290. if (!*subvol_name) {
  291. *subvol_name = kstrdup(".", GFP_KERNEL);
  292. if (!*subvol_name)
  293. return -ENOMEM;
  294. }
  295. return error;
  296. }
  297. static int btrfs_fill_super(struct super_block *sb,
  298. struct btrfs_fs_devices *fs_devices,
  299. void *data, int silent)
  300. {
  301. struct inode *inode;
  302. struct dentry *root_dentry;
  303. struct btrfs_super_block *disk_super;
  304. struct btrfs_root *tree_root;
  305. struct btrfs_key key;
  306. int err;
  307. sb->s_maxbytes = MAX_LFS_FILESIZE;
  308. sb->s_magic = BTRFS_SUPER_MAGIC;
  309. sb->s_op = &btrfs_super_ops;
  310. sb->s_export_op = &btrfs_export_ops;
  311. sb->s_xattr = btrfs_xattr_handlers;
  312. sb->s_time_gran = 1;
  313. sb->s_flags |= MS_POSIXACL;
  314. tree_root = open_ctree(sb, fs_devices, (char *)data);
  315. if (IS_ERR(tree_root)) {
  316. printk("btrfs: open_ctree failed\n");
  317. return PTR_ERR(tree_root);
  318. }
  319. sb->s_fs_info = tree_root;
  320. disk_super = &tree_root->fs_info->super_copy;
  321. key.objectid = BTRFS_FIRST_FREE_OBJECTID;
  322. key.type = BTRFS_INODE_ITEM_KEY;
  323. key.offset = 0;
  324. inode = btrfs_iget(sb, &key, tree_root->fs_info->fs_root);
  325. if (IS_ERR(inode)) {
  326. err = PTR_ERR(inode);
  327. goto fail_close;
  328. }
  329. root_dentry = d_alloc_root(inode);
  330. if (!root_dentry) {
  331. iput(inode);
  332. err = -ENOMEM;
  333. goto fail_close;
  334. }
  335. #if 0
  336. /* this does the super kobj at the same time */
  337. err = btrfs_sysfs_add_super(tree_root->fs_info);
  338. if (err)
  339. goto fail_close;
  340. #endif
  341. sb->s_root = root_dentry;
  342. save_mount_options(sb, data);
  343. return 0;
  344. fail_close:
  345. close_ctree(tree_root);
  346. return err;
  347. }
  348. int btrfs_sync_fs(struct super_block *sb, int wait)
  349. {
  350. struct btrfs_trans_handle *trans;
  351. struct btrfs_root *root = btrfs_sb(sb);
  352. int ret;
  353. if (sb->s_flags & MS_RDONLY)
  354. return 0;
  355. sb->s_dirt = 0;
  356. if (!wait) {
  357. filemap_flush(root->fs_info->btree_inode->i_mapping);
  358. return 0;
  359. }
  360. btrfs_start_delalloc_inodes(root);
  361. btrfs_wait_ordered_extents(root, 0);
  362. trans = btrfs_start_transaction(root, 1);
  363. ret = btrfs_commit_transaction(trans, root);
  364. sb->s_dirt = 0;
  365. return ret;
  366. }
  367. static int btrfs_show_options(struct seq_file *seq, struct vfsmount *vfs)
  368. {
  369. struct btrfs_root *root = btrfs_sb(vfs->mnt_sb);
  370. struct btrfs_fs_info *info = root->fs_info;
  371. if (btrfs_test_opt(root, DEGRADED))
  372. seq_puts(seq, ",degraded");
  373. if (btrfs_test_opt(root, NODATASUM))
  374. seq_puts(seq, ",nodatasum");
  375. if (btrfs_test_opt(root, NODATACOW))
  376. seq_puts(seq, ",nodatacow");
  377. if (btrfs_test_opt(root, NOBARRIER))
  378. seq_puts(seq, ",nobarrier");
  379. if (info->max_extent != (u64)-1)
  380. seq_printf(seq, ",max_extent=%llu",
  381. (unsigned long long)info->max_extent);
  382. if (info->max_inline != 8192 * 1024)
  383. seq_printf(seq, ",max_inline=%llu",
  384. (unsigned long long)info->max_inline);
  385. if (info->alloc_start != 0)
  386. seq_printf(seq, ",alloc_start=%llu",
  387. (unsigned long long)info->alloc_start);
  388. if (info->thread_pool_size != min_t(unsigned long,
  389. num_online_cpus() + 2, 8))
  390. seq_printf(seq, ",thread_pool=%d", info->thread_pool_size);
  391. if (btrfs_test_opt(root, COMPRESS))
  392. seq_puts(seq, ",compress");
  393. if (btrfs_test_opt(root, SSD))
  394. seq_puts(seq, ",ssd");
  395. if (btrfs_test_opt(root, NOTREELOG))
  396. seq_puts(seq, ",notreelog");
  397. if (btrfs_test_opt(root, FLUSHONCOMMIT))
  398. seq_puts(seq, ",flushoncommit");
  399. if (!(root->fs_info->sb->s_flags & MS_POSIXACL))
  400. seq_puts(seq, ",noacl");
  401. return 0;
  402. }
  403. static void btrfs_write_super(struct super_block *sb)
  404. {
  405. sb->s_dirt = 0;
  406. }
  407. static int btrfs_test_super(struct super_block *s, void *data)
  408. {
  409. struct btrfs_fs_devices *test_fs_devices = data;
  410. struct btrfs_root *root = btrfs_sb(s);
  411. return root->fs_info->fs_devices == test_fs_devices;
  412. }
  413. /*
  414. * Find a superblock for the given device / mount point.
  415. *
  416. * Note: This is based on get_sb_bdev from fs/super.c with a few additions
  417. * for multiple device setup. Make sure to keep it in sync.
  418. */
  419. static int btrfs_get_sb(struct file_system_type *fs_type, int flags,
  420. const char *dev_name, void *data, struct vfsmount *mnt)
  421. {
  422. char *subvol_name = NULL;
  423. struct block_device *bdev = NULL;
  424. struct super_block *s;
  425. struct dentry *root;
  426. struct btrfs_fs_devices *fs_devices = NULL;
  427. fmode_t mode = FMODE_READ;
  428. int error = 0;
  429. if (!(flags & MS_RDONLY))
  430. mode |= FMODE_WRITE;
  431. error = btrfs_parse_early_options(data, mode, fs_type,
  432. &subvol_name, &fs_devices);
  433. if (error)
  434. return error;
  435. error = btrfs_scan_one_device(dev_name, mode, fs_type, &fs_devices);
  436. if (error)
  437. goto error_free_subvol_name;
  438. error = btrfs_open_devices(fs_devices, mode, fs_type);
  439. if (error)
  440. goto error_free_subvol_name;
  441. if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) {
  442. error = -EACCES;
  443. goto error_close_devices;
  444. }
  445. bdev = fs_devices->latest_bdev;
  446. s = sget(fs_type, btrfs_test_super, set_anon_super, fs_devices);
  447. if (IS_ERR(s))
  448. goto error_s;
  449. if (s->s_root) {
  450. if ((flags ^ s->s_flags) & MS_RDONLY) {
  451. deactivate_locked_super(s);
  452. error = -EBUSY;
  453. goto error_close_devices;
  454. }
  455. btrfs_close_devices(fs_devices);
  456. } else {
  457. char b[BDEVNAME_SIZE];
  458. s->s_flags = flags;
  459. strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
  460. error = btrfs_fill_super(s, fs_devices, data,
  461. flags & MS_SILENT ? 1 : 0);
  462. if (error) {
  463. deactivate_locked_super(s);
  464. goto error_free_subvol_name;
  465. }
  466. btrfs_sb(s)->fs_info->bdev_holder = fs_type;
  467. s->s_flags |= MS_ACTIVE;
  468. }
  469. if (!strcmp(subvol_name, "."))
  470. root = dget(s->s_root);
  471. else {
  472. mutex_lock(&s->s_root->d_inode->i_mutex);
  473. root = lookup_one_len(subvol_name, s->s_root,
  474. strlen(subvol_name));
  475. mutex_unlock(&s->s_root->d_inode->i_mutex);
  476. if (IS_ERR(root)) {
  477. deactivate_locked_super(s);
  478. error = PTR_ERR(root);
  479. goto error_free_subvol_name;
  480. }
  481. if (!root->d_inode) {
  482. dput(root);
  483. deactivate_locked_super(s);
  484. error = -ENXIO;
  485. goto error_free_subvol_name;
  486. }
  487. }
  488. mnt->mnt_sb = s;
  489. mnt->mnt_root = root;
  490. kfree(subvol_name);
  491. return 0;
  492. error_s:
  493. error = PTR_ERR(s);
  494. error_close_devices:
  495. btrfs_close_devices(fs_devices);
  496. error_free_subvol_name:
  497. kfree(subvol_name);
  498. return error;
  499. }
  500. static int btrfs_remount(struct super_block *sb, int *flags, char *data)
  501. {
  502. struct btrfs_root *root = btrfs_sb(sb);
  503. int ret;
  504. ret = btrfs_parse_options(root, data);
  505. if (ret)
  506. return -EINVAL;
  507. if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
  508. return 0;
  509. if (*flags & MS_RDONLY) {
  510. sb->s_flags |= MS_RDONLY;
  511. ret = btrfs_commit_super(root);
  512. WARN_ON(ret);
  513. } else {
  514. if (root->fs_info->fs_devices->rw_devices == 0)
  515. return -EACCES;
  516. if (btrfs_super_log_root(&root->fs_info->super_copy) != 0)
  517. return -EINVAL;
  518. /* recover relocation */
  519. ret = btrfs_recover_relocation(root);
  520. WARN_ON(ret);
  521. ret = btrfs_cleanup_fs_roots(root->fs_info);
  522. WARN_ON(ret);
  523. sb->s_flags &= ~MS_RDONLY;
  524. }
  525. return 0;
  526. }
  527. static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  528. {
  529. struct btrfs_root *root = btrfs_sb(dentry->d_sb);
  530. struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
  531. int bits = dentry->d_sb->s_blocksize_bits;
  532. __be32 *fsid = (__be32 *)root->fs_info->fsid;
  533. buf->f_namelen = BTRFS_NAME_LEN;
  534. buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
  535. buf->f_bfree = buf->f_blocks -
  536. (btrfs_super_bytes_used(disk_super) >> bits);
  537. buf->f_bavail = buf->f_bfree;
  538. buf->f_bsize = dentry->d_sb->s_blocksize;
  539. buf->f_type = BTRFS_SUPER_MAGIC;
  540. /* We treat it as constant endianness (it doesn't matter _which_)
  541. because we want the fsid to come out the same whether mounted
  542. on a big-endian or little-endian host */
  543. buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
  544. buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
  545. /* Mask in the root object ID too, to disambiguate subvols */
  546. buf->f_fsid.val[0] ^= BTRFS_I(dentry->d_inode)->root->objectid >> 32;
  547. buf->f_fsid.val[1] ^= BTRFS_I(dentry->d_inode)->root->objectid;
  548. return 0;
  549. }
  550. static struct file_system_type btrfs_fs_type = {
  551. .owner = THIS_MODULE,
  552. .name = "btrfs",
  553. .get_sb = btrfs_get_sb,
  554. .kill_sb = kill_anon_super,
  555. .fs_flags = FS_REQUIRES_DEV,
  556. };
  557. /*
  558. * used by btrfsctl to scan devices when no FS is mounted
  559. */
  560. static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
  561. unsigned long arg)
  562. {
  563. struct btrfs_ioctl_vol_args *vol;
  564. struct btrfs_fs_devices *fs_devices;
  565. int ret = -ENOTTY;
  566. if (!capable(CAP_SYS_ADMIN))
  567. return -EPERM;
  568. vol = memdup_user((void __user *)arg, sizeof(*vol));
  569. if (IS_ERR(vol))
  570. return PTR_ERR(vol);
  571. switch (cmd) {
  572. case BTRFS_IOC_SCAN_DEV:
  573. ret = btrfs_scan_one_device(vol->name, FMODE_READ,
  574. &btrfs_fs_type, &fs_devices);
  575. break;
  576. }
  577. kfree(vol);
  578. return ret;
  579. }
  580. static int btrfs_freeze(struct super_block *sb)
  581. {
  582. struct btrfs_root *root = btrfs_sb(sb);
  583. mutex_lock(&root->fs_info->transaction_kthread_mutex);
  584. mutex_lock(&root->fs_info->cleaner_mutex);
  585. return 0;
  586. }
  587. static int btrfs_unfreeze(struct super_block *sb)
  588. {
  589. struct btrfs_root *root = btrfs_sb(sb);
  590. mutex_unlock(&root->fs_info->cleaner_mutex);
  591. mutex_unlock(&root->fs_info->transaction_kthread_mutex);
  592. return 0;
  593. }
  594. static struct super_operations btrfs_super_ops = {
  595. .delete_inode = btrfs_delete_inode,
  596. .put_super = btrfs_put_super,
  597. .write_super = btrfs_write_super,
  598. .sync_fs = btrfs_sync_fs,
  599. .show_options = btrfs_show_options,
  600. .write_inode = btrfs_write_inode,
  601. .dirty_inode = btrfs_dirty_inode,
  602. .alloc_inode = btrfs_alloc_inode,
  603. .destroy_inode = btrfs_destroy_inode,
  604. .statfs = btrfs_statfs,
  605. .remount_fs = btrfs_remount,
  606. .freeze_fs = btrfs_freeze,
  607. .unfreeze_fs = btrfs_unfreeze,
  608. };
  609. static const struct file_operations btrfs_ctl_fops = {
  610. .unlocked_ioctl = btrfs_control_ioctl,
  611. .compat_ioctl = btrfs_control_ioctl,
  612. .owner = THIS_MODULE,
  613. };
  614. static struct miscdevice btrfs_misc = {
  615. .minor = MISC_DYNAMIC_MINOR,
  616. .name = "btrfs-control",
  617. .fops = &btrfs_ctl_fops
  618. };
  619. static int btrfs_interface_init(void)
  620. {
  621. return misc_register(&btrfs_misc);
  622. }
  623. static void btrfs_interface_exit(void)
  624. {
  625. if (misc_deregister(&btrfs_misc) < 0)
  626. printk(KERN_INFO "misc_deregister failed for control device");
  627. }
  628. static int __init init_btrfs_fs(void)
  629. {
  630. int err;
  631. err = btrfs_init_sysfs();
  632. if (err)
  633. return err;
  634. err = btrfs_init_cachep();
  635. if (err)
  636. goto free_sysfs;
  637. err = extent_io_init();
  638. if (err)
  639. goto free_cachep;
  640. err = extent_map_init();
  641. if (err)
  642. goto free_extent_io;
  643. err = btrfs_interface_init();
  644. if (err)
  645. goto free_extent_map;
  646. err = register_filesystem(&btrfs_fs_type);
  647. if (err)
  648. goto unregister_ioctl;
  649. printk(KERN_INFO "%s loaded\n", BTRFS_BUILD_VERSION);
  650. return 0;
  651. unregister_ioctl:
  652. btrfs_interface_exit();
  653. free_extent_map:
  654. extent_map_exit();
  655. free_extent_io:
  656. extent_io_exit();
  657. free_cachep:
  658. btrfs_destroy_cachep();
  659. free_sysfs:
  660. btrfs_exit_sysfs();
  661. return err;
  662. }
  663. static void __exit exit_btrfs_fs(void)
  664. {
  665. btrfs_destroy_cachep();
  666. extent_map_exit();
  667. extent_io_exit();
  668. btrfs_interface_exit();
  669. unregister_filesystem(&btrfs_fs_type);
  670. btrfs_exit_sysfs();
  671. btrfs_cleanup_fs_uuids();
  672. btrfs_zlib_exit();
  673. }
  674. module_init(init_btrfs_fs)
  675. module_exit(exit_btrfs_fs)
  676. MODULE_LICENSE("GPL");