super.c 22 KB

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