super.c 22 KB

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