2 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
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.
19 #include <linux/blkdev.h>
20 #include <linux/module.h>
21 #include <linux/buffer_head.h>
23 #include <linux/pagemap.h>
24 #include <linux/highmem.h>
25 #include <linux/time.h>
26 #include <linux/init.h>
27 #include <linux/seq_file.h>
28 #include <linux/string.h>
29 #include <linux/backing-dev.h>
30 #include <linux/mount.h>
31 #include <linux/mpage.h>
32 #include <linux/swap.h>
33 #include <linux/writeback.h>
34 #include <linux/statfs.h>
35 #include <linux/compat.h>
36 #include <linux/parser.h>
37 #include <linux/ctype.h>
38 #include <linux/namei.h>
39 #include <linux/miscdevice.h>
40 #include <linux/magic.h>
41 #include <linux/slab.h>
43 #include "delayed-inode.h"
46 #include "transaction.h"
47 #include "btrfs_inode.h"
49 #include "print-tree.h"
54 #include "compression.h"
56 #define CREATE_TRACE_POINTS
57 #include <trace/events/btrfs.h>
59 static const struct super_operations btrfs_super_ops
;
61 static const char *btrfs_decode_error(struct btrfs_fs_info
*fs_info
, int errno
,
68 errstr
= "IO failure";
71 errstr
= "Out of memory";
74 errstr
= "Readonly filesystem";
78 if (snprintf(nbuf
, 16, "error %d", -errno
) >= 0)
87 static void __save_error_info(struct btrfs_fs_info
*fs_info
)
90 * today we only save the error info into ram. Long term we'll
91 * also send it down to the disk
93 fs_info
->fs_state
= BTRFS_SUPER_FLAG_ERROR
;
97 * We move write_super stuff at umount in order to avoid deadlock
98 * for umount hold all lock.
100 static void save_error_info(struct btrfs_fs_info
*fs_info
)
102 __save_error_info(fs_info
);
105 /* btrfs handle error by forcing the filesystem readonly */
106 static void btrfs_handle_error(struct btrfs_fs_info
*fs_info
)
108 struct super_block
*sb
= fs_info
->sb
;
110 if (sb
->s_flags
& MS_RDONLY
)
113 if (fs_info
->fs_state
& BTRFS_SUPER_FLAG_ERROR
) {
114 sb
->s_flags
|= MS_RDONLY
;
115 printk(KERN_INFO
"btrfs is forced readonly\n");
120 * __btrfs_std_error decodes expected errors from the caller and
121 * invokes the approciate error response.
123 void __btrfs_std_error(struct btrfs_fs_info
*fs_info
, const char *function
,
124 unsigned int line
, int errno
)
126 struct super_block
*sb
= fs_info
->sb
;
131 * Special case: if the error is EROFS, and we're already
132 * under MS_RDONLY, then it is safe here.
134 if (errno
== -EROFS
&& (sb
->s_flags
& MS_RDONLY
))
137 errstr
= btrfs_decode_error(fs_info
, errno
, nbuf
);
138 printk(KERN_CRIT
"BTRFS error (device %s) in %s:%d: %s\n",
139 sb
->s_id
, function
, line
, errstr
);
140 save_error_info(fs_info
);
142 btrfs_handle_error(fs_info
);
145 static void btrfs_put_super(struct super_block
*sb
)
147 struct btrfs_root
*root
= btrfs_sb(sb
);
150 ret
= close_ctree(root
);
151 sb
->s_fs_info
= NULL
;
153 (void)ret
; /* FIXME: need to fix VFS to return error? */
157 Opt_degraded
, Opt_subvol
, Opt_subvolid
, Opt_device
, Opt_nodatasum
,
158 Opt_nodatacow
, Opt_max_inline
, Opt_alloc_start
, Opt_nobarrier
, Opt_ssd
,
159 Opt_nossd
, Opt_ssd_spread
, Opt_thread_pool
, Opt_noacl
, Opt_compress
,
160 Opt_compress_type
, Opt_compress_force
, Opt_compress_force_type
,
161 Opt_notreelog
, Opt_ratio
, Opt_flushoncommit
, Opt_discard
,
162 Opt_space_cache
, Opt_clear_cache
, Opt_user_subvol_rm_allowed
,
163 Opt_enospc_debug
, Opt_subvolrootid
, Opt_defrag
, Opt_err
,
166 static match_table_t tokens
= {
167 {Opt_degraded
, "degraded"},
168 {Opt_subvol
, "subvol=%s"},
169 {Opt_subvolid
, "subvolid=%d"},
170 {Opt_device
, "device=%s"},
171 {Opt_nodatasum
, "nodatasum"},
172 {Opt_nodatacow
, "nodatacow"},
173 {Opt_nobarrier
, "nobarrier"},
174 {Opt_max_inline
, "max_inline=%s"},
175 {Opt_alloc_start
, "alloc_start=%s"},
176 {Opt_thread_pool
, "thread_pool=%d"},
177 {Opt_compress
, "compress"},
178 {Opt_compress_type
, "compress=%s"},
179 {Opt_compress_force
, "compress-force"},
180 {Opt_compress_force_type
, "compress-force=%s"},
182 {Opt_ssd_spread
, "ssd_spread"},
183 {Opt_nossd
, "nossd"},
184 {Opt_noacl
, "noacl"},
185 {Opt_notreelog
, "notreelog"},
186 {Opt_flushoncommit
, "flushoncommit"},
187 {Opt_ratio
, "metadata_ratio=%d"},
188 {Opt_discard
, "discard"},
189 {Opt_space_cache
, "space_cache"},
190 {Opt_clear_cache
, "clear_cache"},
191 {Opt_user_subvol_rm_allowed
, "user_subvol_rm_allowed"},
192 {Opt_enospc_debug
, "enospc_debug"},
193 {Opt_subvolrootid
, "subvolrootid=%d"},
194 {Opt_defrag
, "autodefrag"},
199 * Regular mount options parser. Everything that is needed only when
200 * reading in a new superblock is parsed here.
202 int btrfs_parse_options(struct btrfs_root
*root
, char *options
)
204 struct btrfs_fs_info
*info
= root
->fs_info
;
205 substring_t args
[MAX_OPT_ARGS
];
206 char *p
, *num
, *orig
;
210 bool compress_force
= false;
216 * strsep changes the string, duplicate it because parse_options
219 options
= kstrdup(options
, GFP_NOFS
);
225 while ((p
= strsep(&options
, ",")) != NULL
) {
230 token
= match_token(p
, tokens
, args
);
233 printk(KERN_INFO
"btrfs: allowing degraded mounts\n");
234 btrfs_set_opt(info
->mount_opt
, DEGRADED
);
238 case Opt_subvolrootid
:
241 * These are parsed by btrfs_parse_early_options
242 * and can be happily ignored here.
246 printk(KERN_INFO
"btrfs: setting nodatasum\n");
247 btrfs_set_opt(info
->mount_opt
, NODATASUM
);
250 printk(KERN_INFO
"btrfs: setting nodatacow\n");
251 btrfs_set_opt(info
->mount_opt
, NODATACOW
);
252 btrfs_set_opt(info
->mount_opt
, NODATASUM
);
254 case Opt_compress_force
:
255 case Opt_compress_force_type
:
256 compress_force
= true;
258 case Opt_compress_type
:
259 if (token
== Opt_compress
||
260 token
== Opt_compress_force
||
261 strcmp(args
[0].from
, "zlib") == 0) {
262 compress_type
= "zlib";
263 info
->compress_type
= BTRFS_COMPRESS_ZLIB
;
264 } else if (strcmp(args
[0].from
, "lzo") == 0) {
265 compress_type
= "lzo";
266 info
->compress_type
= BTRFS_COMPRESS_LZO
;
272 btrfs_set_opt(info
->mount_opt
, COMPRESS
);
273 if (compress_force
) {
274 btrfs_set_opt(info
->mount_opt
, FORCE_COMPRESS
);
275 pr_info("btrfs: force %s compression\n",
278 pr_info("btrfs: use %s compression\n",
282 printk(KERN_INFO
"btrfs: use ssd allocation scheme\n");
283 btrfs_set_opt(info
->mount_opt
, SSD
);
286 printk(KERN_INFO
"btrfs: use spread ssd "
287 "allocation scheme\n");
288 btrfs_set_opt(info
->mount_opt
, SSD
);
289 btrfs_set_opt(info
->mount_opt
, SSD_SPREAD
);
292 printk(KERN_INFO
"btrfs: not using ssd allocation "
294 btrfs_set_opt(info
->mount_opt
, NOSSD
);
295 btrfs_clear_opt(info
->mount_opt
, SSD
);
296 btrfs_clear_opt(info
->mount_opt
, SSD_SPREAD
);
299 printk(KERN_INFO
"btrfs: turning off barriers\n");
300 btrfs_set_opt(info
->mount_opt
, NOBARRIER
);
302 case Opt_thread_pool
:
304 match_int(&args
[0], &intarg
);
306 info
->thread_pool_size
= intarg
;
307 printk(KERN_INFO
"btrfs: thread pool %d\n",
308 info
->thread_pool_size
);
312 num
= match_strdup(&args
[0]);
314 info
->max_inline
= memparse(num
, NULL
);
317 if (info
->max_inline
) {
318 info
->max_inline
= max_t(u64
,
322 printk(KERN_INFO
"btrfs: max_inline at %llu\n",
323 (unsigned long long)info
->max_inline
);
326 case Opt_alloc_start
:
327 num
= match_strdup(&args
[0]);
329 info
->alloc_start
= memparse(num
, NULL
);
332 "btrfs: allocations start at %llu\n",
333 (unsigned long long)info
->alloc_start
);
337 root
->fs_info
->sb
->s_flags
&= ~MS_POSIXACL
;
340 printk(KERN_INFO
"btrfs: disabling tree log\n");
341 btrfs_set_opt(info
->mount_opt
, NOTREELOG
);
343 case Opt_flushoncommit
:
344 printk(KERN_INFO
"btrfs: turning on flush-on-commit\n");
345 btrfs_set_opt(info
->mount_opt
, FLUSHONCOMMIT
);
349 match_int(&args
[0], &intarg
);
351 info
->metadata_ratio
= intarg
;
352 printk(KERN_INFO
"btrfs: metadata ratio %d\n",
353 info
->metadata_ratio
);
357 btrfs_set_opt(info
->mount_opt
, DISCARD
);
359 case Opt_space_cache
:
360 printk(KERN_INFO
"btrfs: enabling disk space caching\n");
361 btrfs_set_opt(info
->mount_opt
, SPACE_CACHE
);
363 case Opt_clear_cache
:
364 printk(KERN_INFO
"btrfs: force clearing of disk cache\n");
365 btrfs_set_opt(info
->mount_opt
, CLEAR_CACHE
);
367 case Opt_user_subvol_rm_allowed
:
368 btrfs_set_opt(info
->mount_opt
, USER_SUBVOL_RM_ALLOWED
);
370 case Opt_enospc_debug
:
371 btrfs_set_opt(info
->mount_opt
, ENOSPC_DEBUG
);
374 printk(KERN_INFO
"btrfs: enabling auto defrag");
375 btrfs_set_opt(info
->mount_opt
, AUTO_DEFRAG
);
378 printk(KERN_INFO
"btrfs: unrecognized mount option "
392 * Parse mount options that are required early in the mount process.
394 * All other options will be parsed on much later in the mount process and
395 * only when we need to allocate a new super block.
397 static int btrfs_parse_early_options(const char *options
, fmode_t flags
,
398 void *holder
, char **subvol_name
, u64
*subvol_objectid
,
399 u64
*subvol_rootid
, struct btrfs_fs_devices
**fs_devices
)
401 substring_t args
[MAX_OPT_ARGS
];
402 char *opts
, *orig
, *p
;
410 * strsep changes the string, duplicate it because parse_options
413 opts
= kstrdup(options
, GFP_KERNEL
);
418 while ((p
= strsep(&opts
, ",")) != NULL
) {
423 token
= match_token(p
, tokens
, args
);
426 *subvol_name
= match_strdup(&args
[0]);
430 error
= match_int(&args
[0], &intarg
);
432 /* we want the original fs_tree */
435 BTRFS_FS_TREE_OBJECTID
;
437 *subvol_objectid
= intarg
;
440 case Opt_subvolrootid
:
442 error
= match_int(&args
[0], &intarg
);
444 /* we want the original fs_tree */
447 BTRFS_FS_TREE_OBJECTID
;
449 *subvol_rootid
= intarg
;
453 error
= btrfs_scan_one_device(match_strdup(&args
[0]),
454 flags
, holder
, fs_devices
);
467 * If no subvolume name is specified we use the default one. Allocate
468 * a copy of the string "." here so that code later in the
469 * mount path doesn't care if it's the default volume or another one.
472 *subvol_name
= kstrdup(".", GFP_KERNEL
);
479 static struct dentry
*get_default_root(struct super_block
*sb
,
482 struct btrfs_root
*root
= sb
->s_fs_info
;
483 struct btrfs_root
*new_root
;
484 struct btrfs_dir_item
*di
;
485 struct btrfs_path
*path
;
486 struct btrfs_key location
;
488 struct dentry
*dentry
;
493 * We have a specific subvol we want to mount, just setup location and
494 * go look up the root.
496 if (subvol_objectid
) {
497 location
.objectid
= subvol_objectid
;
498 location
.type
= BTRFS_ROOT_ITEM_KEY
;
499 location
.offset
= (u64
)-1;
503 path
= btrfs_alloc_path();
505 return ERR_PTR(-ENOMEM
);
506 path
->leave_spinning
= 1;
509 * Find the "default" dir item which points to the root item that we
510 * will mount by default if we haven't been given a specific subvolume
513 dir_id
= btrfs_super_root_dir(&root
->fs_info
->super_copy
);
514 di
= btrfs_lookup_dir_item(NULL
, root
, path
, dir_id
, "default", 7, 0);
516 btrfs_free_path(path
);
521 * Ok the default dir item isn't there. This is weird since
522 * it's always been there, but don't freak out, just try and
523 * mount to root most subvolume.
525 btrfs_free_path(path
);
526 dir_id
= BTRFS_FIRST_FREE_OBJECTID
;
527 new_root
= root
->fs_info
->fs_root
;
531 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, &location
);
532 btrfs_free_path(path
);
535 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &location
);
536 if (IS_ERR(new_root
))
537 return ERR_CAST(new_root
);
539 if (btrfs_root_refs(&new_root
->root_item
) == 0)
540 return ERR_PTR(-ENOENT
);
542 dir_id
= btrfs_root_dirid(&new_root
->root_item
);
544 location
.objectid
= dir_id
;
545 location
.type
= BTRFS_INODE_ITEM_KEY
;
548 inode
= btrfs_iget(sb
, &location
, new_root
, &new);
550 return ERR_CAST(inode
);
553 * If we're just mounting the root most subvol put the inode and return
554 * a reference to the dentry. We will have already gotten a reference
555 * to the inode in btrfs_fill_super so we're good to go.
557 if (!new && sb
->s_root
->d_inode
== inode
) {
559 return dget(sb
->s_root
);
563 const struct qstr name
= { .name
= "/", .len
= 1 };
566 * New inode, we need to make the dentry a sibling of s_root so
567 * everything gets cleaned up properly on unmount.
569 dentry
= d_alloc(sb
->s_root
, &name
);
572 return ERR_PTR(-ENOMEM
);
574 d_splice_alias(inode
, dentry
);
577 * We found the inode in cache, just find a dentry for it and
578 * put the reference to the inode we just got.
580 dentry
= d_find_alias(inode
);
587 static int btrfs_fill_super(struct super_block
*sb
,
588 struct btrfs_fs_devices
*fs_devices
,
589 void *data
, int silent
)
592 struct dentry
*root_dentry
;
593 struct btrfs_root
*tree_root
;
594 struct btrfs_key key
;
597 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
598 sb
->s_magic
= BTRFS_SUPER_MAGIC
;
599 sb
->s_op
= &btrfs_super_ops
;
600 sb
->s_d_op
= &btrfs_dentry_operations
;
601 sb
->s_export_op
= &btrfs_export_ops
;
602 sb
->s_xattr
= btrfs_xattr_handlers
;
604 #ifdef CONFIG_BTRFS_FS_POSIX_ACL
605 sb
->s_flags
|= MS_POSIXACL
;
608 tree_root
= open_ctree(sb
, fs_devices
, (char *)data
);
610 if (IS_ERR(tree_root
)) {
611 printk("btrfs: open_ctree failed\n");
612 return PTR_ERR(tree_root
);
614 sb
->s_fs_info
= tree_root
;
616 key
.objectid
= BTRFS_FIRST_FREE_OBJECTID
;
617 key
.type
= BTRFS_INODE_ITEM_KEY
;
619 inode
= btrfs_iget(sb
, &key
, tree_root
->fs_info
->fs_root
, NULL
);
621 err
= PTR_ERR(inode
);
625 root_dentry
= d_alloc_root(inode
);
632 sb
->s_root
= root_dentry
;
634 save_mount_options(sb
, data
);
638 close_ctree(tree_root
);
642 int btrfs_sync_fs(struct super_block
*sb
, int wait
)
644 struct btrfs_trans_handle
*trans
;
645 struct btrfs_root
*root
= btrfs_sb(sb
);
648 trace_btrfs_sync_fs(wait
);
651 filemap_flush(root
->fs_info
->btree_inode
->i_mapping
);
655 btrfs_start_delalloc_inodes(root
, 0);
656 btrfs_wait_ordered_extents(root
, 0, 0);
658 trans
= btrfs_start_transaction(root
, 0);
660 return PTR_ERR(trans
);
661 ret
= btrfs_commit_transaction(trans
, root
);
665 static int btrfs_show_options(struct seq_file
*seq
, struct vfsmount
*vfs
)
667 struct btrfs_root
*root
= btrfs_sb(vfs
->mnt_sb
);
668 struct btrfs_fs_info
*info
= root
->fs_info
;
671 if (btrfs_test_opt(root
, DEGRADED
))
672 seq_puts(seq
, ",degraded");
673 if (btrfs_test_opt(root
, NODATASUM
))
674 seq_puts(seq
, ",nodatasum");
675 if (btrfs_test_opt(root
, NODATACOW
))
676 seq_puts(seq
, ",nodatacow");
677 if (btrfs_test_opt(root
, NOBARRIER
))
678 seq_puts(seq
, ",nobarrier");
679 if (info
->max_inline
!= 8192 * 1024)
680 seq_printf(seq
, ",max_inline=%llu",
681 (unsigned long long)info
->max_inline
);
682 if (info
->alloc_start
!= 0)
683 seq_printf(seq
, ",alloc_start=%llu",
684 (unsigned long long)info
->alloc_start
);
685 if (info
->thread_pool_size
!= min_t(unsigned long,
686 num_online_cpus() + 2, 8))
687 seq_printf(seq
, ",thread_pool=%d", info
->thread_pool_size
);
688 if (btrfs_test_opt(root
, COMPRESS
)) {
689 if (info
->compress_type
== BTRFS_COMPRESS_ZLIB
)
690 compress_type
= "zlib";
692 compress_type
= "lzo";
693 if (btrfs_test_opt(root
, FORCE_COMPRESS
))
694 seq_printf(seq
, ",compress-force=%s", compress_type
);
696 seq_printf(seq
, ",compress=%s", compress_type
);
698 if (btrfs_test_opt(root
, NOSSD
))
699 seq_puts(seq
, ",nossd");
700 if (btrfs_test_opt(root
, SSD_SPREAD
))
701 seq_puts(seq
, ",ssd_spread");
702 else if (btrfs_test_opt(root
, SSD
))
703 seq_puts(seq
, ",ssd");
704 if (btrfs_test_opt(root
, NOTREELOG
))
705 seq_puts(seq
, ",notreelog");
706 if (btrfs_test_opt(root
, FLUSHONCOMMIT
))
707 seq_puts(seq
, ",flushoncommit");
708 if (btrfs_test_opt(root
, DISCARD
))
709 seq_puts(seq
, ",discard");
710 if (!(root
->fs_info
->sb
->s_flags
& MS_POSIXACL
))
711 seq_puts(seq
, ",noacl");
712 if (btrfs_test_opt(root
, SPACE_CACHE
))
713 seq_puts(seq
, ",space_cache");
714 if (btrfs_test_opt(root
, CLEAR_CACHE
))
715 seq_puts(seq
, ",clear_cache");
716 if (btrfs_test_opt(root
, USER_SUBVOL_RM_ALLOWED
))
717 seq_puts(seq
, ",user_subvol_rm_allowed");
721 static int btrfs_test_super(struct super_block
*s
, void *data
)
723 struct btrfs_root
*test_root
= data
;
724 struct btrfs_root
*root
= btrfs_sb(s
);
727 * If this super block is going away, return false as it
728 * can't match as an existing super block.
730 if (!atomic_read(&s
->s_active
))
732 return root
->fs_info
->fs_devices
== test_root
->fs_info
->fs_devices
;
735 static int btrfs_set_super(struct super_block
*s
, void *data
)
739 return set_anon_super(s
, data
);
744 * Find a superblock for the given device / mount point.
746 * Note: This is based on get_sb_bdev from fs/super.c with a few additions
747 * for multiple device setup. Make sure to keep it in sync.
749 static struct dentry
*btrfs_mount(struct file_system_type
*fs_type
, int flags
,
750 const char *device_name
, void *data
)
752 struct block_device
*bdev
= NULL
;
753 struct super_block
*s
;
755 struct btrfs_fs_devices
*fs_devices
= NULL
;
756 struct btrfs_root
*tree_root
= NULL
;
757 struct btrfs_fs_info
*fs_info
= NULL
;
758 fmode_t mode
= FMODE_READ
;
759 char *subvol_name
= NULL
;
760 u64 subvol_objectid
= 0;
761 u64 subvol_rootid
= 0;
764 if (!(flags
& MS_RDONLY
))
767 error
= btrfs_parse_early_options(data
, mode
, fs_type
,
768 &subvol_name
, &subvol_objectid
,
769 &subvol_rootid
, &fs_devices
);
771 return ERR_PTR(error
);
773 error
= btrfs_scan_one_device(device_name
, mode
, fs_type
, &fs_devices
);
775 goto error_free_subvol_name
;
777 error
= btrfs_open_devices(fs_devices
, mode
, fs_type
);
779 goto error_free_subvol_name
;
781 if (!(flags
& MS_RDONLY
) && fs_devices
->rw_devices
== 0) {
783 goto error_close_devices
;
787 * Setup a dummy root and fs_info for test/set super. This is because
788 * we don't actually fill this stuff out until open_ctree, but we need
789 * it for searching for existing supers, so this lets us do that and
790 * then open_ctree will properly initialize everything later.
792 fs_info
= kzalloc(sizeof(struct btrfs_fs_info
), GFP_NOFS
);
793 tree_root
= kzalloc(sizeof(struct btrfs_root
), GFP_NOFS
);
794 if (!fs_info
|| !tree_root
) {
796 goto error_close_devices
;
798 fs_info
->tree_root
= tree_root
;
799 fs_info
->fs_devices
= fs_devices
;
800 tree_root
->fs_info
= fs_info
;
802 bdev
= fs_devices
->latest_bdev
;
803 s
= sget(fs_type
, btrfs_test_super
, btrfs_set_super
, tree_root
);
808 if ((flags
^ s
->s_flags
) & MS_RDONLY
) {
809 deactivate_locked_super(s
);
811 goto error_close_devices
;
814 btrfs_close_devices(fs_devices
);
818 char b
[BDEVNAME_SIZE
];
821 strlcpy(s
->s_id
, bdevname(bdev
, b
), sizeof(s
->s_id
));
822 error
= btrfs_fill_super(s
, fs_devices
, data
,
823 flags
& MS_SILENT
? 1 : 0);
825 deactivate_locked_super(s
);
826 goto error_free_subvol_name
;
829 btrfs_sb(s
)->fs_info
->bdev_holder
= fs_type
;
830 s
->s_flags
|= MS_ACTIVE
;
833 /* if they gave us a subvolume name bind mount into that */
834 if (strcmp(subvol_name
, ".")) {
835 struct dentry
*new_root
;
837 root
= get_default_root(s
, subvol_rootid
);
839 error
= PTR_ERR(root
);
840 deactivate_locked_super(s
);
841 goto error_free_subvol_name
;
844 mutex_lock(&root
->d_inode
->i_mutex
);
845 new_root
= lookup_one_len(subvol_name
, root
,
846 strlen(subvol_name
));
847 mutex_unlock(&root
->d_inode
->i_mutex
);
849 if (IS_ERR(new_root
)) {
851 deactivate_locked_super(s
);
852 error
= PTR_ERR(new_root
);
853 goto error_free_subvol_name
;
855 if (!new_root
->d_inode
) {
858 deactivate_locked_super(s
);
860 goto error_free_subvol_name
;
865 root
= get_default_root(s
, subvol_objectid
);
867 error
= PTR_ERR(root
);
868 deactivate_locked_super(s
);
869 goto error_free_subvol_name
;
879 btrfs_close_devices(fs_devices
);
882 error_free_subvol_name
:
884 return ERR_PTR(error
);
887 static int btrfs_remount(struct super_block
*sb
, int *flags
, char *data
)
889 struct btrfs_root
*root
= btrfs_sb(sb
);
892 ret
= btrfs_parse_options(root
, data
);
896 if ((*flags
& MS_RDONLY
) == (sb
->s_flags
& MS_RDONLY
))
899 if (*flags
& MS_RDONLY
) {
900 sb
->s_flags
|= MS_RDONLY
;
902 ret
= btrfs_commit_super(root
);
905 if (root
->fs_info
->fs_devices
->rw_devices
== 0)
908 if (btrfs_super_log_root(&root
->fs_info
->super_copy
) != 0)
911 ret
= btrfs_cleanup_fs_roots(root
->fs_info
);
914 /* recover relocation */
915 ret
= btrfs_recover_relocation(root
);
918 sb
->s_flags
&= ~MS_RDONLY
;
924 /* Used to sort the devices by max_avail(descending sort) */
925 static int btrfs_cmp_device_free_bytes(const void *dev_info1
,
926 const void *dev_info2
)
928 if (((struct btrfs_device_info
*)dev_info1
)->max_avail
>
929 ((struct btrfs_device_info
*)dev_info2
)->max_avail
)
931 else if (((struct btrfs_device_info
*)dev_info1
)->max_avail
<
932 ((struct btrfs_device_info
*)dev_info2
)->max_avail
)
939 * sort the devices by max_avail, in which max free extent size of each device
940 * is stored.(Descending Sort)
942 static inline void btrfs_descending_sort_devices(
943 struct btrfs_device_info
*devices
,
946 sort(devices
, nr_devices
, sizeof(struct btrfs_device_info
),
947 btrfs_cmp_device_free_bytes
, NULL
);
951 * The helper to calc the free space on the devices that can be used to store
954 static int btrfs_calc_avail_data_space(struct btrfs_root
*root
, u64
*free_bytes
)
956 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
957 struct btrfs_device_info
*devices_info
;
958 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
959 struct btrfs_device
*device
;
966 int i
= 0, nr_devices
;
969 nr_devices
= fs_info
->fs_devices
->rw_devices
;
972 devices_info
= kmalloc(sizeof(*devices_info
) * nr_devices
,
977 /* calc min stripe number for data space alloction */
978 type
= btrfs_get_alloc_profile(root
, 1);
979 if (type
& BTRFS_BLOCK_GROUP_RAID0
)
981 else if (type
& BTRFS_BLOCK_GROUP_RAID1
)
983 else if (type
& BTRFS_BLOCK_GROUP_RAID10
)
986 if (type
& BTRFS_BLOCK_GROUP_DUP
)
987 min_stripe_size
= 2 * BTRFS_STRIPE_LEN
;
989 min_stripe_size
= BTRFS_STRIPE_LEN
;
991 list_for_each_entry(device
, &fs_devices
->alloc_list
, dev_alloc_list
) {
992 if (!device
->in_fs_metadata
)
995 avail_space
= device
->total_bytes
- device
->bytes_used
;
997 /* align with stripe_len */
998 do_div(avail_space
, BTRFS_STRIPE_LEN
);
999 avail_space
*= BTRFS_STRIPE_LEN
;
1002 * In order to avoid overwritting the superblock on the drive,
1003 * btrfs starts at an offset of at least 1MB when doing chunk
1006 skip_space
= 1024 * 1024;
1008 /* user can set the offset in fs_info->alloc_start. */
1009 if (fs_info
->alloc_start
+ BTRFS_STRIPE_LEN
<=
1010 device
->total_bytes
)
1011 skip_space
= max(fs_info
->alloc_start
, skip_space
);
1014 * btrfs can not use the free space in [0, skip_space - 1],
1015 * we must subtract it from the total. In order to implement
1016 * it, we account the used space in this range first.
1018 ret
= btrfs_account_dev_extents_size(device
, 0, skip_space
- 1,
1021 kfree(devices_info
);
1025 /* calc the free space in [0, skip_space - 1] */
1026 skip_space
-= used_space
;
1029 * we can use the free space in [0, skip_space - 1], subtract
1030 * it from the total.
1032 if (avail_space
&& avail_space
>= skip_space
)
1033 avail_space
-= skip_space
;
1037 if (avail_space
< min_stripe_size
)
1040 devices_info
[i
].dev
= device
;
1041 devices_info
[i
].max_avail
= avail_space
;
1048 btrfs_descending_sort_devices(devices_info
, nr_devices
);
1052 while (nr_devices
>= min_stripes
) {
1053 if (devices_info
[i
].max_avail
>= min_stripe_size
) {
1057 avail_space
+= devices_info
[i
].max_avail
* min_stripes
;
1058 alloc_size
= devices_info
[i
].max_avail
;
1059 for (j
= i
+ 1 - min_stripes
; j
<= i
; j
++)
1060 devices_info
[j
].max_avail
-= alloc_size
;
1066 kfree(devices_info
);
1067 *free_bytes
= avail_space
;
1071 static int btrfs_statfs(struct dentry
*dentry
, struct kstatfs
*buf
)
1073 struct btrfs_root
*root
= btrfs_sb(dentry
->d_sb
);
1074 struct btrfs_super_block
*disk_super
= &root
->fs_info
->super_copy
;
1075 struct list_head
*head
= &root
->fs_info
->space_info
;
1076 struct btrfs_space_info
*found
;
1078 u64 total_free_data
= 0;
1079 int bits
= dentry
->d_sb
->s_blocksize_bits
;
1080 __be32
*fsid
= (__be32
*)root
->fs_info
->fsid
;
1083 /* holding chunk_muext to avoid allocating new chunks */
1084 mutex_lock(&root
->fs_info
->chunk_mutex
);
1086 list_for_each_entry_rcu(found
, head
, list
) {
1087 if (found
->flags
& BTRFS_BLOCK_GROUP_DATA
) {
1088 total_free_data
+= found
->disk_total
- found
->disk_used
;
1090 btrfs_account_ro_block_groups_free_space(found
);
1093 total_used
+= found
->disk_used
;
1097 buf
->f_namelen
= BTRFS_NAME_LEN
;
1098 buf
->f_blocks
= btrfs_super_total_bytes(disk_super
) >> bits
;
1099 buf
->f_bfree
= buf
->f_blocks
- (total_used
>> bits
);
1100 buf
->f_bsize
= dentry
->d_sb
->s_blocksize
;
1101 buf
->f_type
= BTRFS_SUPER_MAGIC
;
1102 buf
->f_bavail
= total_free_data
;
1103 ret
= btrfs_calc_avail_data_space(root
, &total_free_data
);
1105 mutex_unlock(&root
->fs_info
->chunk_mutex
);
1108 buf
->f_bavail
+= total_free_data
;
1109 buf
->f_bavail
= buf
->f_bavail
>> bits
;
1110 mutex_unlock(&root
->fs_info
->chunk_mutex
);
1112 /* We treat it as constant endianness (it doesn't matter _which_)
1113 because we want the fsid to come out the same whether mounted
1114 on a big-endian or little-endian host */
1115 buf
->f_fsid
.val
[0] = be32_to_cpu(fsid
[0]) ^ be32_to_cpu(fsid
[2]);
1116 buf
->f_fsid
.val
[1] = be32_to_cpu(fsid
[1]) ^ be32_to_cpu(fsid
[3]);
1117 /* Mask in the root object ID too, to disambiguate subvols */
1118 buf
->f_fsid
.val
[0] ^= BTRFS_I(dentry
->d_inode
)->root
->objectid
>> 32;
1119 buf
->f_fsid
.val
[1] ^= BTRFS_I(dentry
->d_inode
)->root
->objectid
;
1124 static struct file_system_type btrfs_fs_type
= {
1125 .owner
= THIS_MODULE
,
1127 .mount
= btrfs_mount
,
1128 .kill_sb
= kill_anon_super
,
1129 .fs_flags
= FS_REQUIRES_DEV
,
1133 * used by btrfsctl to scan devices when no FS is mounted
1135 static long btrfs_control_ioctl(struct file
*file
, unsigned int cmd
,
1138 struct btrfs_ioctl_vol_args
*vol
;
1139 struct btrfs_fs_devices
*fs_devices
;
1142 if (!capable(CAP_SYS_ADMIN
))
1145 vol
= memdup_user((void __user
*)arg
, sizeof(*vol
));
1147 return PTR_ERR(vol
);
1150 case BTRFS_IOC_SCAN_DEV
:
1151 ret
= btrfs_scan_one_device(vol
->name
, FMODE_READ
,
1152 &btrfs_fs_type
, &fs_devices
);
1160 static int btrfs_freeze(struct super_block
*sb
)
1162 struct btrfs_root
*root
= btrfs_sb(sb
);
1163 mutex_lock(&root
->fs_info
->transaction_kthread_mutex
);
1164 mutex_lock(&root
->fs_info
->cleaner_mutex
);
1168 static int btrfs_unfreeze(struct super_block
*sb
)
1170 struct btrfs_root
*root
= btrfs_sb(sb
);
1171 mutex_unlock(&root
->fs_info
->cleaner_mutex
);
1172 mutex_unlock(&root
->fs_info
->transaction_kthread_mutex
);
1176 static const struct super_operations btrfs_super_ops
= {
1177 .drop_inode
= btrfs_drop_inode
,
1178 .evict_inode
= btrfs_evict_inode
,
1179 .put_super
= btrfs_put_super
,
1180 .sync_fs
= btrfs_sync_fs
,
1181 .show_options
= btrfs_show_options
,
1182 .write_inode
= btrfs_write_inode
,
1183 .dirty_inode
= btrfs_dirty_inode
,
1184 .alloc_inode
= btrfs_alloc_inode
,
1185 .destroy_inode
= btrfs_destroy_inode
,
1186 .statfs
= btrfs_statfs
,
1187 .remount_fs
= btrfs_remount
,
1188 .freeze_fs
= btrfs_freeze
,
1189 .unfreeze_fs
= btrfs_unfreeze
,
1192 static const struct file_operations btrfs_ctl_fops
= {
1193 .unlocked_ioctl
= btrfs_control_ioctl
,
1194 .compat_ioctl
= btrfs_control_ioctl
,
1195 .owner
= THIS_MODULE
,
1196 .llseek
= noop_llseek
,
1199 static struct miscdevice btrfs_misc
= {
1200 .minor
= BTRFS_MINOR
,
1201 .name
= "btrfs-control",
1202 .fops
= &btrfs_ctl_fops
1205 MODULE_ALIAS_MISCDEV(BTRFS_MINOR
);
1206 MODULE_ALIAS("devname:btrfs-control");
1208 static int btrfs_interface_init(void)
1210 return misc_register(&btrfs_misc
);
1213 static void btrfs_interface_exit(void)
1215 if (misc_deregister(&btrfs_misc
) < 0)
1216 printk(KERN_INFO
"misc_deregister failed for control device");
1219 static int __init
init_btrfs_fs(void)
1223 err
= btrfs_init_sysfs();
1227 err
= btrfs_init_compress();
1231 err
= btrfs_init_cachep();
1235 err
= extent_io_init();
1239 err
= extent_map_init();
1241 goto free_extent_io
;
1243 err
= btrfs_delayed_inode_init();
1245 goto free_extent_map
;
1247 err
= btrfs_interface_init();
1249 goto free_delayed_inode
;
1251 err
= register_filesystem(&btrfs_fs_type
);
1253 goto unregister_ioctl
;
1255 printk(KERN_INFO
"%s loaded\n", BTRFS_BUILD_VERSION
);
1259 btrfs_interface_exit();
1261 btrfs_delayed_inode_exit();
1267 btrfs_destroy_cachep();
1269 btrfs_exit_compress();
1275 static void __exit
exit_btrfs_fs(void)
1277 btrfs_destroy_cachep();
1278 btrfs_delayed_inode_exit();
1281 btrfs_interface_exit();
1282 unregister_filesystem(&btrfs_fs_type
);
1284 btrfs_cleanup_fs_uuids();
1285 btrfs_exit_compress();
1288 module_init(init_btrfs_fs
)
1289 module_exit(exit_btrfs_fs
)
1291 MODULE_LICENSE("GPL");