super.c 22 KB

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