super.c 19 KB

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