super.c 19 KB

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