super.c 24 KB

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