super.c 24 KB

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