]> git.ipfire.org Git - people/ms/linux.git/blame - fs/btrfs/super.c
Btrfs: add a "df" ioctl for btrfs
[people/ms/linux.git] / fs / btrfs / super.c
CommitLineData
6cbd5570
CM
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
4b82d6e4 19#include <linux/blkdev.h>
2e635a27 20#include <linux/module.h>
e20d96d6 21#include <linux/buffer_head.h>
2e635a27
CM
22#include <linux/fs.h>
23#include <linux/pagemap.h>
24#include <linux/highmem.h>
25#include <linux/time.h>
26#include <linux/init.h>
a9572a15 27#include <linux/seq_file.h>
2e635a27 28#include <linux/string.h>
2e635a27 29#include <linux/backing-dev.h>
4b82d6e4 30#include <linux/mount.h>
dee26a9f 31#include <linux/mpage.h>
75dfe396
CM
32#include <linux/swap.h>
33#include <linux/writeback.h>
8fd17795 34#include <linux/statfs.h>
08607c1b 35#include <linux/compat.h>
95e05289 36#include <linux/parser.h>
c59f8951 37#include <linux/ctype.h>
6da6abae 38#include <linux/namei.h>
a9218f6b 39#include <linux/miscdevice.h>
1bcbf313 40#include <linux/magic.h>
4b4e25f2 41#include "compat.h"
2e635a27 42#include "ctree.h"
e20d96d6 43#include "disk-io.h"
d5719762 44#include "transaction.h"
2c90e5d6 45#include "btrfs_inode.h"
c5739bba 46#include "ioctl.h"
3a686375 47#include "print-tree.h"
5103e947 48#include "xattr.h"
8a4b83cc 49#include "volumes.h"
b3c3da71 50#include "version.h"
be6e8dc0 51#include "export.h"
c8b97818 52#include "compression.h"
2e635a27 53
b87221de 54static const struct super_operations btrfs_super_ops;
75dfe396 55
d397712b 56static void btrfs_put_super(struct super_block *sb)
b18c6685 57{
39279cc3 58 struct btrfs_root *root = btrfs_sb(sb);
b18c6685 59 int ret;
b18c6685 60
39279cc3 61 ret = close_ctree(root);
39279cc3 62 sb->s_fs_info = NULL;
75dfe396
CM
63}
64
95e05289 65enum {
73f73415
JB
66 Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum,
67 Opt_nodatacow, Opt_max_extent, Opt_max_inline, Opt_alloc_start,
68 Opt_nobarrier, Opt_ssd, Opt_nossd, Opt_ssd_spread, Opt_thread_pool,
69 Opt_noacl, Opt_compress, Opt_compress_force, Opt_notreelog, Opt_ratio,
a555f810 70 Opt_flushoncommit,
e244a0ae 71 Opt_discard, Opt_err,
95e05289
CM
72};
73
74static match_table_t tokens = {
dfe25020 75 {Opt_degraded, "degraded"},
95e05289 76 {Opt_subvol, "subvol=%s"},
73f73415 77 {Opt_subvolid, "subvolid=%d"},
43e570b0 78 {Opt_device, "device=%s"},
b6cda9bc 79 {Opt_nodatasum, "nodatasum"},
be20aa9d 80 {Opt_nodatacow, "nodatacow"},
21ad10cf 81 {Opt_nobarrier, "nobarrier"},
c59f8951 82 {Opt_max_extent, "max_extent=%s"},
6f568d35 83 {Opt_max_inline, "max_inline=%s"},
8f662a76 84 {Opt_alloc_start, "alloc_start=%s"},
4543df7e 85 {Opt_thread_pool, "thread_pool=%d"},
c8b97818 86 {Opt_compress, "compress"},
a555f810 87 {Opt_compress_force, "compress-force"},
e18e4809 88 {Opt_ssd, "ssd"},
451d7585 89 {Opt_ssd_spread, "ssd_spread"},
3b30c22f 90 {Opt_nossd, "nossd"},
33268eaf 91 {Opt_noacl, "noacl"},
3a5e1404 92 {Opt_notreelog, "notreelog"},
dccae999 93 {Opt_flushoncommit, "flushoncommit"},
97e728d4 94 {Opt_ratio, "metadata_ratio=%d"},
e244a0ae 95 {Opt_discard, "discard"},
33268eaf 96 {Opt_err, NULL},
95e05289
CM
97};
98
edbd8d4e 99u64 btrfs_parse_size(char *str)
c59f8951 100{
edbd8d4e 101 u64 res;
c59f8951
CM
102 int mult = 1;
103 char *end;
104 char last;
105
106 res = simple_strtoul(str, &end, 10);
107
108 last = end[0];
109 if (isalpha(last)) {
110 last = tolower(last);
111 switch (last) {
112 case 'g':
113 mult *= 1024;
114 case 'm':
115 mult *= 1024;
116 case 'k':
117 mult *= 1024;
118 }
119 res = res * mult;
120 }
121 return res;
122}
123
edf24abe
CH
124/*
125 * Regular mount options parser. Everything that is needed only when
126 * reading in a new superblock is parsed here.
127 */
128int btrfs_parse_options(struct btrfs_root *root, char *options)
95e05289 129{
edf24abe 130 struct btrfs_fs_info *info = root->fs_info;
95e05289 131 substring_t args[MAX_OPT_ARGS];
da495ecc 132 char *p, *num, *orig;
4543df7e 133 int intarg;
a7a3f7ca 134 int ret = 0;
b6cda9bc 135
95e05289 136 if (!options)
edf24abe 137 return 0;
95e05289 138
be20aa9d
CM
139 /*
140 * strsep changes the string, duplicate it because parse_options
141 * gets called twice
142 */
143 options = kstrdup(options, GFP_NOFS);
144 if (!options)
145 return -ENOMEM;
146
da495ecc 147 orig = options;
be20aa9d 148
edf24abe 149 while ((p = strsep(&options, ",")) != NULL) {
95e05289
CM
150 int token;
151 if (!*p)
152 continue;
153
154 token = match_token(p, tokens, args);
155 switch (token) {
dfe25020 156 case Opt_degraded:
edf24abe
CH
157 printk(KERN_INFO "btrfs: allowing degraded mounts\n");
158 btrfs_set_opt(info->mount_opt, DEGRADED);
dfe25020 159 break;
95e05289 160 case Opt_subvol:
73f73415 161 case Opt_subvolid:
43e570b0 162 case Opt_device:
edf24abe 163 /*
43e570b0 164 * These are parsed by btrfs_parse_early_options
edf24abe
CH
165 * and can be happily ignored here.
166 */
b6cda9bc
CM
167 break;
168 case Opt_nodatasum:
067c28ad 169 printk(KERN_INFO "btrfs: setting nodatasum\n");
edf24abe 170 btrfs_set_opt(info->mount_opt, NODATASUM);
be20aa9d
CM
171 break;
172 case Opt_nodatacow:
edf24abe
CH
173 printk(KERN_INFO "btrfs: setting nodatacow\n");
174 btrfs_set_opt(info->mount_opt, NODATACOW);
175 btrfs_set_opt(info->mount_opt, NODATASUM);
95e05289 176 break;
c8b97818
CM
177 case Opt_compress:
178 printk(KERN_INFO "btrfs: use compression\n");
179 btrfs_set_opt(info->mount_opt, COMPRESS);
180 break;
a555f810
CM
181 case Opt_compress_force:
182 printk(KERN_INFO "btrfs: forcing compression\n");
183 btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
184 btrfs_set_opt(info->mount_opt, COMPRESS);
185 break;
e18e4809 186 case Opt_ssd:
edf24abe
CH
187 printk(KERN_INFO "btrfs: use ssd allocation scheme\n");
188 btrfs_set_opt(info->mount_opt, SSD);
e18e4809 189 break;
451d7585
CM
190 case Opt_ssd_spread:
191 printk(KERN_INFO "btrfs: use spread ssd "
192 "allocation scheme\n");
193 btrfs_set_opt(info->mount_opt, SSD);
194 btrfs_set_opt(info->mount_opt, SSD_SPREAD);
195 break;
3b30c22f 196 case Opt_nossd:
451d7585
CM
197 printk(KERN_INFO "btrfs: not using ssd allocation "
198 "scheme\n");
c289811c 199 btrfs_set_opt(info->mount_opt, NOSSD);
3b30c22f 200 btrfs_clear_opt(info->mount_opt, SSD);
451d7585 201 btrfs_clear_opt(info->mount_opt, SSD_SPREAD);
3b30c22f 202 break;
21ad10cf 203 case Opt_nobarrier:
edf24abe
CH
204 printk(KERN_INFO "btrfs: turning off barriers\n");
205 btrfs_set_opt(info->mount_opt, NOBARRIER);
21ad10cf 206 break;
4543df7e
CM
207 case Opt_thread_pool:
208 intarg = 0;
209 match_int(&args[0], &intarg);
210 if (intarg) {
211 info->thread_pool_size = intarg;
212 printk(KERN_INFO "btrfs: thread pool %d\n",
213 info->thread_pool_size);
214 }
215 break;
c59f8951 216 case Opt_max_extent:
edf24abe
CH
217 num = match_strdup(&args[0]);
218 if (num) {
219 info->max_extent = btrfs_parse_size(num);
220 kfree(num);
221
222 info->max_extent = max_t(u64,
223 info->max_extent, root->sectorsize);
224 printk(KERN_INFO "btrfs: max_extent at %llu\n",
21380931 225 (unsigned long long)info->max_extent);
c59f8951
CM
226 }
227 break;
6f568d35 228 case Opt_max_inline:
edf24abe
CH
229 num = match_strdup(&args[0]);
230 if (num) {
231 info->max_inline = btrfs_parse_size(num);
232 kfree(num);
233
15ada040
CM
234 if (info->max_inline) {
235 info->max_inline = max_t(u64,
236 info->max_inline,
237 root->sectorsize);
238 }
edf24abe 239 printk(KERN_INFO "btrfs: max_inline at %llu\n",
21380931 240 (unsigned long long)info->max_inline);
6f568d35
CM
241 }
242 break;
8f662a76 243 case Opt_alloc_start:
edf24abe
CH
244 num = match_strdup(&args[0]);
245 if (num) {
246 info->alloc_start = btrfs_parse_size(num);
247 kfree(num);
248 printk(KERN_INFO
249 "btrfs: allocations start at %llu\n",
21380931 250 (unsigned long long)info->alloc_start);
8f662a76
CM
251 }
252 break;
33268eaf
JB
253 case Opt_noacl:
254 root->fs_info->sb->s_flags &= ~MS_POSIXACL;
255 break;
3a5e1404
SW
256 case Opt_notreelog:
257 printk(KERN_INFO "btrfs: disabling tree log\n");
258 btrfs_set_opt(info->mount_opt, NOTREELOG);
259 break;
dccae999
SW
260 case Opt_flushoncommit:
261 printk(KERN_INFO "btrfs: turning on flush-on-commit\n");
262 btrfs_set_opt(info->mount_opt, FLUSHONCOMMIT);
263 break;
97e728d4
JB
264 case Opt_ratio:
265 intarg = 0;
266 match_int(&args[0], &intarg);
267 if (intarg) {
268 info->metadata_ratio = intarg;
269 printk(KERN_INFO "btrfs: metadata ratio %d\n",
270 info->metadata_ratio);
271 }
272 break;
e244a0ae
CH
273 case Opt_discard:
274 btrfs_set_opt(info->mount_opt, DISCARD);
275 break;
a7a3f7ca
SW
276 case Opt_err:
277 printk(KERN_INFO "btrfs: unrecognized mount option "
278 "'%s'\n", p);
279 ret = -EINVAL;
280 goto out;
95e05289 281 default:
be20aa9d 282 break;
95e05289
CM
283 }
284 }
a7a3f7ca 285out:
da495ecc 286 kfree(orig);
a7a3f7ca 287 return ret;
edf24abe
CH
288}
289
290/*
291 * Parse mount options that are required early in the mount process.
292 *
293 * All other options will be parsed on much later in the mount process and
294 * only when we need to allocate a new super block.
295 */
97288f2c 296static int btrfs_parse_early_options(const char *options, fmode_t flags,
73f73415 297 void *holder, char **subvol_name, u64 *subvol_objectid,
43e570b0 298 struct btrfs_fs_devices **fs_devices)
edf24abe
CH
299{
300 substring_t args[MAX_OPT_ARGS];
301 char *opts, *p;
302 int error = 0;
73f73415 303 int intarg;
edf24abe
CH
304
305 if (!options)
306 goto out;
307
308 /*
309 * strsep changes the string, duplicate it because parse_options
310 * gets called twice
311 */
312 opts = kstrdup(options, GFP_KERNEL);
313 if (!opts)
314 return -ENOMEM;
315
316 while ((p = strsep(&opts, ",")) != NULL) {
317 int token;
318 if (!*p)
319 continue;
320
321 token = match_token(p, tokens, args);
322 switch (token) {
323 case Opt_subvol:
324 *subvol_name = match_strdup(&args[0]);
325 break;
73f73415
JB
326 case Opt_subvolid:
327 intarg = 0;
4849f01d
JB
328 error = match_int(&args[0], &intarg);
329 if (!error) {
330 /* we want the original fs_tree */
331 if (!intarg)
332 *subvol_objectid =
333 BTRFS_FS_TREE_OBJECTID;
334 else
335 *subvol_objectid = intarg;
336 }
73f73415 337 break;
43e570b0
CH
338 case Opt_device:
339 error = btrfs_scan_one_device(match_strdup(&args[0]),
340 flags, holder, fs_devices);
341 if (error)
342 goto out_free_opts;
343 break;
edf24abe
CH
344 default:
345 break;
346 }
347 }
348
43e570b0 349 out_free_opts:
edf24abe
CH
350 kfree(opts);
351 out:
352 /*
353 * If no subvolume name is specified we use the default one. Allocate
3de4586c 354 * a copy of the string "." here so that code later in the
edf24abe
CH
355 * mount path doesn't care if it's the default volume or another one.
356 */
357 if (!*subvol_name) {
3de4586c 358 *subvol_name = kstrdup(".", GFP_KERNEL);
edf24abe
CH
359 if (!*subvol_name)
360 return -ENOMEM;
361 }
362 return error;
95e05289
CM
363}
364
73f73415
JB
365static struct dentry *get_default_root(struct super_block *sb,
366 u64 subvol_objectid)
367{
368 struct btrfs_root *root = sb->s_fs_info;
369 struct btrfs_root *new_root;
370 struct btrfs_dir_item *di;
371 struct btrfs_path *path;
372 struct btrfs_key location;
373 struct inode *inode;
374 struct dentry *dentry;
375 u64 dir_id;
376 int new = 0;
377
378 /*
379 * We have a specific subvol we want to mount, just setup location and
380 * go look up the root.
381 */
382 if (subvol_objectid) {
383 location.objectid = subvol_objectid;
384 location.type = BTRFS_ROOT_ITEM_KEY;
385 location.offset = (u64)-1;
386 goto find_root;
387 }
388
389 path = btrfs_alloc_path();
390 if (!path)
391 return ERR_PTR(-ENOMEM);
392 path->leave_spinning = 1;
393
394 /*
395 * Find the "default" dir item which points to the root item that we
396 * will mount by default if we haven't been given a specific subvolume
397 * to mount.
398 */
399 dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
400 di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0);
401 if (!di) {
402 /*
403 * Ok the default dir item isn't there. This is weird since
404 * it's always been there, but don't freak out, just try and
405 * mount to root most subvolume.
406 */
407 btrfs_free_path(path);
408 dir_id = BTRFS_FIRST_FREE_OBJECTID;
409 new_root = root->fs_info->fs_root;
410 goto setup_root;
411 }
412
413 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
414 btrfs_free_path(path);
415
416find_root:
417 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
418 if (IS_ERR(new_root))
419 return ERR_PTR(PTR_ERR(new_root));
420
421 if (btrfs_root_refs(&new_root->root_item) == 0)
422 return ERR_PTR(-ENOENT);
423
424 dir_id = btrfs_root_dirid(&new_root->root_item);
425setup_root:
426 location.objectid = dir_id;
427 location.type = BTRFS_INODE_ITEM_KEY;
428 location.offset = 0;
429
430 inode = btrfs_iget(sb, &location, new_root, &new);
431 if (!inode)
432 return ERR_PTR(-ENOMEM);
433
434 /*
435 * If we're just mounting the root most subvol put the inode and return
436 * a reference to the dentry. We will have already gotten a reference
437 * to the inode in btrfs_fill_super so we're good to go.
438 */
439 if (!new && sb->s_root->d_inode == inode) {
440 iput(inode);
441 return dget(sb->s_root);
442 }
443
444 if (new) {
445 const struct qstr name = { .name = "/", .len = 1 };
446
447 /*
448 * New inode, we need to make the dentry a sibling of s_root so
449 * everything gets cleaned up properly on unmount.
450 */
451 dentry = d_alloc(sb->s_root, &name);
452 if (!dentry) {
453 iput(inode);
454 return ERR_PTR(-ENOMEM);
455 }
456 d_splice_alias(inode, dentry);
457 } else {
458 /*
459 * We found the inode in cache, just find a dentry for it and
460 * put the reference to the inode we just got.
461 */
462 dentry = d_find_alias(inode);
463 iput(inode);
464 }
465
466 return dentry;
467}
468
d397712b 469static int btrfs_fill_super(struct super_block *sb,
8a4b83cc 470 struct btrfs_fs_devices *fs_devices,
d397712b 471 void *data, int silent)
75dfe396 472{
d397712b
CM
473 struct inode *inode;
474 struct dentry *root_dentry;
39279cc3
CM
475 struct btrfs_super_block *disk_super;
476 struct btrfs_root *tree_root;
5d4f98a2 477 struct btrfs_key key;
39279cc3 478 int err;
a429e513 479
39279cc3
CM
480 sb->s_maxbytes = MAX_LFS_FILESIZE;
481 sb->s_magic = BTRFS_SUPER_MAGIC;
482 sb->s_op = &btrfs_super_ops;
be6e8dc0 483 sb->s_export_op = &btrfs_export_ops;
5103e947 484 sb->s_xattr = btrfs_xattr_handlers;
39279cc3 485 sb->s_time_gran = 1;
0eda294d 486#ifdef CONFIG_BTRFS_FS_POSIX_ACL
33268eaf 487 sb->s_flags |= MS_POSIXACL;
49cf6f45 488#endif
a429e513 489
dfe25020 490 tree_root = open_ctree(sb, fs_devices, (char *)data);
6567e837 491
e58ca020 492 if (IS_ERR(tree_root)) {
39279cc3 493 printk("btrfs: open_ctree failed\n");
e58ca020 494 return PTR_ERR(tree_root);
a429e513 495 }
39279cc3 496 sb->s_fs_info = tree_root;
5f39d397 497 disk_super = &tree_root->fs_info->super_copy;
a429e513 498
5d4f98a2
YZ
499 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
500 key.type = BTRFS_INODE_ITEM_KEY;
501 key.offset = 0;
73f73415 502 inode = btrfs_iget(sb, &key, tree_root->fs_info->fs_root, NULL);
5d4f98a2
YZ
503 if (IS_ERR(inode)) {
504 err = PTR_ERR(inode);
39279cc3 505 goto fail_close;
f254e52c 506 }
f254e52c 507
39279cc3
CM
508 root_dentry = d_alloc_root(inode);
509 if (!root_dentry) {
510 iput(inode);
511 err = -ENOMEM;
512 goto fail_close;
f254e52c 513 }
58176a96 514
39279cc3 515 sb->s_root = root_dentry;
6885f308 516
6885f308 517 save_mount_options(sb, data);
2619ba1f 518 return 0;
39279cc3
CM
519
520fail_close:
521 close_ctree(tree_root);
522 return err;
2619ba1f
CM
523}
524
6bf13c0c 525int btrfs_sync_fs(struct super_block *sb, int wait)
c5739bba
CM
526{
527 struct btrfs_trans_handle *trans;
dccae999 528 struct btrfs_root *root = btrfs_sb(sb);
c5739bba 529 int ret;
2619ba1f 530
39279cc3
CM
531 if (!wait) {
532 filemap_flush(root->fs_info->btree_inode->i_mapping);
533 return 0;
534 }
771ed689 535
24bbcf04
YZ
536 btrfs_start_delalloc_inodes(root, 0);
537 btrfs_wait_ordered_extents(root, 0, 0);
771ed689 538
c5739bba 539 trans = btrfs_start_transaction(root, 1);
c5739bba 540 ret = btrfs_commit_transaction(trans, root);
54aa1f4d 541 return ret;
2c90e5d6
CM
542}
543
a9572a15
EP
544static int btrfs_show_options(struct seq_file *seq, struct vfsmount *vfs)
545{
546 struct btrfs_root *root = btrfs_sb(vfs->mnt_sb);
547 struct btrfs_fs_info *info = root->fs_info;
548
549 if (btrfs_test_opt(root, DEGRADED))
550 seq_puts(seq, ",degraded");
551 if (btrfs_test_opt(root, NODATASUM))
552 seq_puts(seq, ",nodatasum");
553 if (btrfs_test_opt(root, NODATACOW))
554 seq_puts(seq, ",nodatacow");
555 if (btrfs_test_opt(root, NOBARRIER))
556 seq_puts(seq, ",nobarrier");
557 if (info->max_extent != (u64)-1)
21380931
JB
558 seq_printf(seq, ",max_extent=%llu",
559 (unsigned long long)info->max_extent);
a9572a15 560 if (info->max_inline != 8192 * 1024)
21380931
JB
561 seq_printf(seq, ",max_inline=%llu",
562 (unsigned long long)info->max_inline);
a9572a15 563 if (info->alloc_start != 0)
21380931
JB
564 seq_printf(seq, ",alloc_start=%llu",
565 (unsigned long long)info->alloc_start);
a9572a15
EP
566 if (info->thread_pool_size != min_t(unsigned long,
567 num_online_cpus() + 2, 8))
568 seq_printf(seq, ",thread_pool=%d", info->thread_pool_size);
569 if (btrfs_test_opt(root, COMPRESS))
570 seq_puts(seq, ",compress");
c289811c
CM
571 if (btrfs_test_opt(root, NOSSD))
572 seq_puts(seq, ",nossd");
451d7585
CM
573 if (btrfs_test_opt(root, SSD_SPREAD))
574 seq_puts(seq, ",ssd_spread");
575 else if (btrfs_test_opt(root, SSD))
a9572a15 576 seq_puts(seq, ",ssd");
3a5e1404 577 if (btrfs_test_opt(root, NOTREELOG))
6b65c5c6 578 seq_puts(seq, ",notreelog");
dccae999 579 if (btrfs_test_opt(root, FLUSHONCOMMIT))
6b65c5c6 580 seq_puts(seq, ",flushoncommit");
20a5239a
MW
581 if (btrfs_test_opt(root, DISCARD))
582 seq_puts(seq, ",discard");
a9572a15
EP
583 if (!(root->fs_info->sb->s_flags & MS_POSIXACL))
584 seq_puts(seq, ",noacl");
585 return 0;
586}
587
a061fc8d 588static int btrfs_test_super(struct super_block *s, void *data)
4b82d6e4 589{
a061fc8d
CM
590 struct btrfs_fs_devices *test_fs_devices = data;
591 struct btrfs_root *root = btrfs_sb(s);
4b82d6e4 592
a061fc8d 593 return root->fs_info->fs_devices == test_fs_devices;
4b82d6e4
Y
594}
595
edf24abe
CH
596/*
597 * Find a superblock for the given device / mount point.
598 *
599 * Note: This is based on get_sb_bdev from fs/super.c with a few additions
600 * for multiple device setup. Make sure to keep it in sync.
601 */
602static int btrfs_get_sb(struct file_system_type *fs_type, int flags,
603 const char *dev_name, void *data, struct vfsmount *mnt)
4b82d6e4
Y
604{
605 struct block_device *bdev = NULL;
606 struct super_block *s;
607 struct dentry *root;
8a4b83cc 608 struct btrfs_fs_devices *fs_devices = NULL;
97288f2c 609 fmode_t mode = FMODE_READ;
73f73415
JB
610 char *subvol_name = NULL;
611 u64 subvol_objectid = 0;
4b82d6e4 612 int error = 0;
73f73415 613 int found = 0;
4b82d6e4 614
97288f2c
CH
615 if (!(flags & MS_RDONLY))
616 mode |= FMODE_WRITE;
617
618 error = btrfs_parse_early_options(data, mode, fs_type,
73f73415
JB
619 &subvol_name, &subvol_objectid,
620 &fs_devices);
edf24abe 621 if (error)
1f483660 622 return error;
edf24abe 623
97288f2c 624 error = btrfs_scan_one_device(dev_name, mode, fs_type, &fs_devices);
8a4b83cc 625 if (error)
edf24abe 626 goto error_free_subvol_name;
4b82d6e4 627
97288f2c 628 error = btrfs_open_devices(fs_devices, mode, fs_type);
8a4b83cc 629 if (error)
edf24abe 630 goto error_free_subvol_name;
8a4b83cc 631
2b82032c
YZ
632 if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) {
633 error = -EACCES;
634 goto error_close_devices;
635 }
636
dfe25020 637 bdev = fs_devices->latest_bdev;
a061fc8d 638 s = sget(fs_type, btrfs_test_super, set_anon_super, fs_devices);
4b82d6e4
Y
639 if (IS_ERR(s))
640 goto error_s;
641
642 if (s->s_root) {
643 if ((flags ^ s->s_flags) & MS_RDONLY) {
6f5bbff9 644 deactivate_locked_super(s);
4b82d6e4 645 error = -EBUSY;
c146afad 646 goto error_close_devices;
4b82d6e4
Y
647 }
648
73f73415 649 found = 1;
2b82032c 650 btrfs_close_devices(fs_devices);
4b82d6e4
Y
651 } else {
652 char b[BDEVNAME_SIZE];
653
654 s->s_flags = flags;
655 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
8a4b83cc
CM
656 error = btrfs_fill_super(s, fs_devices, data,
657 flags & MS_SILENT ? 1 : 0);
4b82d6e4 658 if (error) {
6f5bbff9 659 deactivate_locked_super(s);
1f483660 660 goto error_free_subvol_name;
4b82d6e4
Y
661 }
662
788f20eb 663 btrfs_sb(s)->fs_info->bdev_holder = fs_type;
4b82d6e4
Y
664 s->s_flags |= MS_ACTIVE;
665 }
666
73f73415
JB
667 root = get_default_root(s, subvol_objectid);
668 if (IS_ERR(root)) {
669 error = PTR_ERR(root);
670 deactivate_locked_super(s);
671 goto error;
672 }
673 /* if they gave us a subvolume name bind mount into that */
674 if (strcmp(subvol_name, ".")) {
675 struct dentry *new_root;
676 mutex_lock(&root->d_inode->i_mutex);
677 new_root = lookup_one_len(subvol_name, root,
d397712b 678 strlen(subvol_name));
73f73415 679 mutex_unlock(&root->d_inode->i_mutex);
d397712b 680
73f73415 681 if (IS_ERR(new_root)) {
6f5bbff9 682 deactivate_locked_super(s);
73f73415
JB
683 error = PTR_ERR(new_root);
684 dput(root);
685 goto error_close_devices;
76fcef19 686 }
73f73415 687 if (!new_root->d_inode) {
76fcef19 688 dput(root);
73f73415 689 dput(new_root);
6f5bbff9 690 deactivate_locked_super(s);
76fcef19 691 error = -ENXIO;
73f73415 692 goto error_close_devices;
76fcef19 693 }
73f73415
JB
694 dput(root);
695 root = new_root;
4b82d6e4
Y
696 }
697
698 mnt->mnt_sb = s;
699 mnt->mnt_root = root;
edf24abe
CH
700
701 kfree(subvol_name);
4b82d6e4
Y
702 return 0;
703
704error_s:
705 error = PTR_ERR(s);
c146afad 706error_close_devices:
8a4b83cc 707 btrfs_close_devices(fs_devices);
edf24abe
CH
708error_free_subvol_name:
709 kfree(subvol_name);
73f73415 710error:
4b82d6e4
Y
711 return error;
712}
2e635a27 713
c146afad
YZ
714static int btrfs_remount(struct super_block *sb, int *flags, char *data)
715{
716 struct btrfs_root *root = btrfs_sb(sb);
717 int ret;
718
b288052e
CM
719 ret = btrfs_parse_options(root, data);
720 if (ret)
721 return -EINVAL;
722
c146afad
YZ
723 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
724 return 0;
725
726 if (*flags & MS_RDONLY) {
727 sb->s_flags |= MS_RDONLY;
728
729 ret = btrfs_commit_super(root);
730 WARN_ON(ret);
731 } else {
2b82032c
YZ
732 if (root->fs_info->fs_devices->rw_devices == 0)
733 return -EACCES;
734
c146afad
YZ
735 if (btrfs_super_log_root(&root->fs_info->super_copy) != 0)
736 return -EINVAL;
737
5d4f98a2
YZ
738 /* recover relocation */
739 ret = btrfs_recover_relocation(root);
c146afad
YZ
740 WARN_ON(ret);
741
742 ret = btrfs_cleanup_fs_roots(root->fs_info);
743 WARN_ON(ret);
744
745 sb->s_flags &= ~MS_RDONLY;
746 }
747
748 return 0;
749}
750
8fd17795
CM
751static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
752{
753 struct btrfs_root *root = btrfs_sb(dentry->d_sb);
4b52dff6 754 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
bd4d1088
JB
755 struct list_head *head = &root->fs_info->space_info;
756 struct btrfs_space_info *found;
757 u64 total_used = 0;
758 u64 data_used = 0;
db94535d 759 int bits = dentry->d_sb->s_blocksize_bits;
9d03632e 760 __be32 *fsid = (__be32 *)root->fs_info->fsid;
8fd17795 761
bd4d1088
JB
762 rcu_read_lock();
763 list_for_each_entry_rcu(found, head, list) {
764 if (found->flags & (BTRFS_BLOCK_GROUP_DUP|
765 BTRFS_BLOCK_GROUP_RAID10|
766 BTRFS_BLOCK_GROUP_RAID1)) {
767 total_used += found->bytes_used;
768 if (found->flags & BTRFS_BLOCK_GROUP_DATA)
769 data_used += found->bytes_used;
770 else
771 data_used += found->total_bytes;
772 }
773
774 total_used += found->bytes_used;
775 if (found->flags & BTRFS_BLOCK_GROUP_DATA)
776 data_used += found->bytes_used;
777 else
778 data_used += found->total_bytes;
779 }
780 rcu_read_unlock();
781
8fd17795 782 buf->f_namelen = BTRFS_NAME_LEN;
db94535d 783 buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
bd4d1088
JB
784 buf->f_bfree = buf->f_blocks - (total_used >> bits);
785 buf->f_bavail = buf->f_blocks - (data_used >> bits);
8fd17795
CM
786 buf->f_bsize = dentry->d_sb->s_blocksize;
787 buf->f_type = BTRFS_SUPER_MAGIC;
d397712b 788
9d03632e 789 /* We treat it as constant endianness (it doesn't matter _which_)
d397712b 790 because we want the fsid to come out the same whether mounted
9d03632e
DW
791 on a big-endian or little-endian host */
792 buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
793 buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
32d48fa1
DW
794 /* Mask in the root object ID too, to disambiguate subvols */
795 buf->f_fsid.val[0] ^= BTRFS_I(dentry->d_inode)->root->objectid >> 32;
796 buf->f_fsid.val[1] ^= BTRFS_I(dentry->d_inode)->root->objectid;
797
8fd17795
CM
798 return 0;
799}
b5133862 800
2e635a27
CM
801static struct file_system_type btrfs_fs_type = {
802 .owner = THIS_MODULE,
803 .name = "btrfs",
804 .get_sb = btrfs_get_sb,
a061fc8d 805 .kill_sb = kill_anon_super,
2e635a27
CM
806 .fs_flags = FS_REQUIRES_DEV,
807};
a9218f6b 808
d352ac68
CM
809/*
810 * used by btrfsctl to scan devices when no FS is mounted
811 */
8a4b83cc
CM
812static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
813 unsigned long arg)
814{
815 struct btrfs_ioctl_vol_args *vol;
816 struct btrfs_fs_devices *fs_devices;
c071fcfd 817 int ret = -ENOTTY;
8a4b83cc 818
e441d54d
CM
819 if (!capable(CAP_SYS_ADMIN))
820 return -EPERM;
821
dae7b665
LZ
822 vol = memdup_user((void __user *)arg, sizeof(*vol));
823 if (IS_ERR(vol))
824 return PTR_ERR(vol);
c071fcfd 825
8a4b83cc
CM
826 switch (cmd) {
827 case BTRFS_IOC_SCAN_DEV:
97288f2c 828 ret = btrfs_scan_one_device(vol->name, FMODE_READ,
8a4b83cc
CM
829 &btrfs_fs_type, &fs_devices);
830 break;
831 }
dae7b665 832
8a4b83cc 833 kfree(vol);
f819d837 834 return ret;
8a4b83cc
CM
835}
836
0176260f 837static int btrfs_freeze(struct super_block *sb)
ed0dab6b
Y
838{
839 struct btrfs_root *root = btrfs_sb(sb);
a74a4b97
CM
840 mutex_lock(&root->fs_info->transaction_kthread_mutex);
841 mutex_lock(&root->fs_info->cleaner_mutex);
0176260f 842 return 0;
ed0dab6b
Y
843}
844
0176260f 845static int btrfs_unfreeze(struct super_block *sb)
ed0dab6b
Y
846{
847 struct btrfs_root *root = btrfs_sb(sb);
a74a4b97
CM
848 mutex_unlock(&root->fs_info->cleaner_mutex);
849 mutex_unlock(&root->fs_info->transaction_kthread_mutex);
0176260f 850 return 0;
ed0dab6b 851}
2e635a27 852
b87221de 853static const struct super_operations btrfs_super_ops = {
76dda93c 854 .drop_inode = btrfs_drop_inode,
134e9731 855 .delete_inode = btrfs_delete_inode,
e20d96d6 856 .put_super = btrfs_put_super,
d5719762 857 .sync_fs = btrfs_sync_fs,
a9572a15 858 .show_options = btrfs_show_options,
4730a4bc 859 .write_inode = btrfs_write_inode,
b5133862 860 .dirty_inode = btrfs_dirty_inode,
2c90e5d6
CM
861 .alloc_inode = btrfs_alloc_inode,
862 .destroy_inode = btrfs_destroy_inode,
8fd17795 863 .statfs = btrfs_statfs,
c146afad 864 .remount_fs = btrfs_remount,
0176260f
LT
865 .freeze_fs = btrfs_freeze,
866 .unfreeze_fs = btrfs_unfreeze,
e20d96d6 867};
a9218f6b
CM
868
869static const struct file_operations btrfs_ctl_fops = {
870 .unlocked_ioctl = btrfs_control_ioctl,
871 .compat_ioctl = btrfs_control_ioctl,
872 .owner = THIS_MODULE,
873};
874
875static struct miscdevice btrfs_misc = {
876 .minor = MISC_DYNAMIC_MINOR,
877 .name = "btrfs-control",
878 .fops = &btrfs_ctl_fops
879};
880
881static int btrfs_interface_init(void)
882{
883 return misc_register(&btrfs_misc);
884}
885
b2950863 886static void btrfs_interface_exit(void)
a9218f6b
CM
887{
888 if (misc_deregister(&btrfs_misc) < 0)
d397712b 889 printk(KERN_INFO "misc_deregister failed for control device");
a9218f6b
CM
890}
891
2e635a27
CM
892static int __init init_btrfs_fs(void)
893{
2c90e5d6 894 int err;
58176a96
JB
895
896 err = btrfs_init_sysfs();
897 if (err)
898 return err;
899
39279cc3 900 err = btrfs_init_cachep();
2c90e5d6 901 if (err)
a74a4b97 902 goto free_sysfs;
d1310b2e
CM
903
904 err = extent_io_init();
2f4cbe64
WB
905 if (err)
906 goto free_cachep;
907
d1310b2e
CM
908 err = extent_map_init();
909 if (err)
910 goto free_extent_io;
911
a9218f6b 912 err = btrfs_interface_init();
2f4cbe64
WB
913 if (err)
914 goto free_extent_map;
c8b97818 915
a9218f6b
CM
916 err = register_filesystem(&btrfs_fs_type);
917 if (err)
918 goto unregister_ioctl;
b3c3da71
CM
919
920 printk(KERN_INFO "%s loaded\n", BTRFS_BUILD_VERSION);
2f4cbe64
WB
921 return 0;
922
a9218f6b
CM
923unregister_ioctl:
924 btrfs_interface_exit();
2f4cbe64
WB
925free_extent_map:
926 extent_map_exit();
d1310b2e
CM
927free_extent_io:
928 extent_io_exit();
2f4cbe64
WB
929free_cachep:
930 btrfs_destroy_cachep();
a74a4b97 931free_sysfs:
2f4cbe64
WB
932 btrfs_exit_sysfs();
933 return err;
2e635a27
CM
934}
935
936static void __exit exit_btrfs_fs(void)
937{
39279cc3 938 btrfs_destroy_cachep();
a52d9a80 939 extent_map_exit();
d1310b2e 940 extent_io_exit();
a9218f6b 941 btrfs_interface_exit();
2e635a27 942 unregister_filesystem(&btrfs_fs_type);
58176a96 943 btrfs_exit_sysfs();
8a4b83cc 944 btrfs_cleanup_fs_uuids();
c8b97818 945 btrfs_zlib_exit();
2e635a27
CM
946}
947
948module_init(init_btrfs_fs)
949module_exit(exit_btrfs_fs)
950
951MODULE_LICENSE("GPL");