super.c 19 KB

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