super.c 23 KB

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