super.c 23 KB

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