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_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"},
198 * Regular mount options parser. Everything that is needed only when
199 * reading in a new superblock is parsed here.
201 int btrfs_parse_options(struct btrfs_root
*root
, char *options
)
203 struct btrfs_fs_info
*info
= root
->fs_info
;
204 substring_t args
[MAX_OPT_ARGS
];
205 char *p
, *num
, *orig
;
209 bool compress_force
= false;
215 * strsep changes the string, duplicate it because parse_options
218 options
= kstrdup(options
, GFP_NOFS
);
224 while ((p
= strsep(&options
, ",")) != NULL
) {
229 token
= match_token(p
, tokens
, args
);
232 printk(KERN_INFO
"btrfs: allowing degraded mounts\n");
233 btrfs_set_opt(info
->mount_opt
, DEGRADED
);
237 case Opt_subvolrootid
:
240 * These are parsed by btrfs_parse_early_options
241 * and can be happily ignored here.
245 printk(KERN_INFO
"btrfs: setting nodatasum\n");
246 btrfs_set_opt(info
->mount_opt
, NODATASUM
);
249 printk(KERN_INFO
"btrfs: setting nodatacow\n");
250 btrfs_set_opt(info
->mount_opt
, NODATACOW
);
251 btrfs_set_opt(info
->mount_opt
, NODATASUM
);
253 case Opt_compress_force
:
254 case Opt_compress_force_type
:
255 compress_force
= true;
257 case Opt_compress_type
:
258 if (token
== Opt_compress
||
259 token
== Opt_compress_force
||
260 strcmp(args
[0].from
, "zlib") == 0) {
261 compress_type
= "zlib";
262 info
->compress_type
= BTRFS_COMPRESS_ZLIB
;
263 } else if (strcmp(args
[0].from
, "lzo") == 0) {
264 compress_type
= "lzo";
265 info
->compress_type
= BTRFS_COMPRESS_LZO
;
271 btrfs_set_opt(info
->mount_opt
, COMPRESS
);
272 if (compress_force
) {
273 btrfs_set_opt(info
->mount_opt
, FORCE_COMPRESS
);
274 pr_info("btrfs: force %s compression\n",
277 pr_info("btrfs: use %s compression\n",
281 printk(KERN_INFO
"btrfs: use ssd allocation scheme\n");
282 btrfs_set_opt(info
->mount_opt
, SSD
);
285 printk(KERN_INFO
"btrfs: use spread ssd "
286 "allocation scheme\n");
287 btrfs_set_opt(info
->mount_opt
, SSD
);
288 btrfs_set_opt(info
->mount_opt
, SSD_SPREAD
);
291 printk(KERN_INFO
"btrfs: not using ssd allocation "
293 btrfs_set_opt(info
->mount_opt
, NOSSD
);
294 btrfs_clear_opt(info
->mount_opt
, SSD
);
295 btrfs_clear_opt(info
->mount_opt
, SSD_SPREAD
);
298 printk(KERN_INFO
"btrfs: turning off barriers\n");
299 btrfs_set_opt(info
->mount_opt
, NOBARRIER
);
301 case Opt_thread_pool
:
303 match_int(&args
[0], &intarg
);
305 info
->thread_pool_size
= intarg
;
306 printk(KERN_INFO
"btrfs: thread pool %d\n",
307 info
->thread_pool_size
);
311 num
= match_strdup(&args
[0]);
313 info
->max_inline
= memparse(num
, NULL
);
316 if (info
->max_inline
) {
317 info
->max_inline
= max_t(u64
,
321 printk(KERN_INFO
"btrfs: max_inline at %llu\n",
322 (unsigned long long)info
->max_inline
);
325 case Opt_alloc_start
:
326 num
= match_strdup(&args
[0]);
328 info
->alloc_start
= memparse(num
, NULL
);
331 "btrfs: allocations start at %llu\n",
332 (unsigned long long)info
->alloc_start
);
336 root
->fs_info
->sb
->s_flags
&= ~MS_POSIXACL
;
339 printk(KERN_INFO
"btrfs: disabling tree log\n");
340 btrfs_set_opt(info
->mount_opt
, NOTREELOG
);
342 case Opt_flushoncommit
:
343 printk(KERN_INFO
"btrfs: turning on flush-on-commit\n");
344 btrfs_set_opt(info
->mount_opt
, FLUSHONCOMMIT
);
348 match_int(&args
[0], &intarg
);
350 info
->metadata_ratio
= intarg
;
351 printk(KERN_INFO
"btrfs: metadata ratio %d\n",
352 info
->metadata_ratio
);
356 btrfs_set_opt(info
->mount_opt
, DISCARD
);
358 case Opt_space_cache
:
359 printk(KERN_INFO
"btrfs: enabling disk space caching\n");
360 btrfs_set_opt(info
->mount_opt
, SPACE_CACHE
);
362 case Opt_clear_cache
:
363 printk(KERN_INFO
"btrfs: force clearing of disk cache\n");
364 btrfs_set_opt(info
->mount_opt
, CLEAR_CACHE
);
366 case Opt_user_subvol_rm_allowed
:
367 btrfs_set_opt(info
->mount_opt
, USER_SUBVOL_RM_ALLOWED
);
369 case Opt_enospc_debug
:
370 btrfs_set_opt(info
->mount_opt
, ENOSPC_DEBUG
);
373 printk(KERN_INFO
"btrfs: unrecognized mount option "
387 * Parse mount options that are required early in the mount process.
389 * All other options will be parsed on much later in the mount process and
390 * only when we need to allocate a new super block.
392 static int btrfs_parse_early_options(const char *options
, fmode_t flags
,
393 void *holder
, char **subvol_name
, u64
*subvol_objectid
,
394 u64
*subvol_rootid
, struct btrfs_fs_devices
**fs_devices
)
396 substring_t args
[MAX_OPT_ARGS
];
397 char *opts
, *orig
, *p
;
405 * strsep changes the string, duplicate it because parse_options
408 opts
= kstrdup(options
, GFP_KERNEL
);
413 while ((p
= strsep(&opts
, ",")) != NULL
) {
418 token
= match_token(p
, tokens
, args
);
421 *subvol_name
= match_strdup(&args
[0]);
425 error
= match_int(&args
[0], &intarg
);
427 /* we want the original fs_tree */
430 BTRFS_FS_TREE_OBJECTID
;
432 *subvol_objectid
= intarg
;
435 case Opt_subvolrootid
:
437 error
= match_int(&args
[0], &intarg
);
439 /* we want the original fs_tree */
442 BTRFS_FS_TREE_OBJECTID
;
444 *subvol_rootid
= intarg
;
448 error
= btrfs_scan_one_device(match_strdup(&args
[0]),
449 flags
, holder
, fs_devices
);
462 * If no subvolume name is specified we use the default one. Allocate
463 * a copy of the string "." here so that code later in the
464 * mount path doesn't care if it's the default volume or another one.
467 *subvol_name
= kstrdup(".", GFP_KERNEL
);
474 static struct dentry
*get_default_root(struct super_block
*sb
,
477 struct btrfs_root
*root
= sb
->s_fs_info
;
478 struct btrfs_root
*new_root
;
479 struct btrfs_dir_item
*di
;
480 struct btrfs_path
*path
;
481 struct btrfs_key location
;
483 struct dentry
*dentry
;
488 * We have a specific subvol we want to mount, just setup location and
489 * go look up the root.
491 if (subvol_objectid
) {
492 location
.objectid
= subvol_objectid
;
493 location
.type
= BTRFS_ROOT_ITEM_KEY
;
494 location
.offset
= (u64
)-1;
498 path
= btrfs_alloc_path();
500 return ERR_PTR(-ENOMEM
);
501 path
->leave_spinning
= 1;
504 * Find the "default" dir item which points to the root item that we
505 * will mount by default if we haven't been given a specific subvolume
508 dir_id
= btrfs_super_root_dir(&root
->fs_info
->super_copy
);
509 di
= btrfs_lookup_dir_item(NULL
, root
, path
, dir_id
, "default", 7, 0);
511 btrfs_free_path(path
);
516 * Ok the default dir item isn't there. This is weird since
517 * it's always been there, but don't freak out, just try and
518 * mount to root most subvolume.
520 btrfs_free_path(path
);
521 dir_id
= BTRFS_FIRST_FREE_OBJECTID
;
522 new_root
= root
->fs_info
->fs_root
;
526 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, &location
);
527 btrfs_free_path(path
);
530 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &location
);
531 if (IS_ERR(new_root
))
532 return ERR_CAST(new_root
);
534 if (btrfs_root_refs(&new_root
->root_item
) == 0)
535 return ERR_PTR(-ENOENT
);
537 dir_id
= btrfs_root_dirid(&new_root
->root_item
);
539 location
.objectid
= dir_id
;
540 location
.type
= BTRFS_INODE_ITEM_KEY
;
543 inode
= btrfs_iget(sb
, &location
, new_root
, &new);
545 return ERR_CAST(inode
);
548 * If we're just mounting the root most subvol put the inode and return
549 * a reference to the dentry. We will have already gotten a reference
550 * to the inode in btrfs_fill_super so we're good to go.
552 if (!new && sb
->s_root
->d_inode
== inode
) {
554 return dget(sb
->s_root
);
558 const struct qstr name
= { .name
= "/", .len
= 1 };
561 * New inode, we need to make the dentry a sibling of s_root so
562 * everything gets cleaned up properly on unmount.
564 dentry
= d_alloc(sb
->s_root
, &name
);
567 return ERR_PTR(-ENOMEM
);
569 d_splice_alias(inode
, dentry
);
572 * We found the inode in cache, just find a dentry for it and
573 * put the reference to the inode we just got.
575 dentry
= d_find_alias(inode
);
582 static int btrfs_fill_super(struct super_block
*sb
,
583 struct btrfs_fs_devices
*fs_devices
,
584 void *data
, int silent
)
587 struct dentry
*root_dentry
;
588 struct btrfs_root
*tree_root
;
589 struct btrfs_key key
;
592 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
593 sb
->s_magic
= BTRFS_SUPER_MAGIC
;
594 sb
->s_op
= &btrfs_super_ops
;
595 sb
->s_d_op
= &btrfs_dentry_operations
;
596 sb
->s_export_op
= &btrfs_export_ops
;
597 sb
->s_xattr
= btrfs_xattr_handlers
;
599 #ifdef CONFIG_BTRFS_FS_POSIX_ACL
600 sb
->s_flags
|= MS_POSIXACL
;
603 tree_root
= open_ctree(sb
, fs_devices
, (char *)data
);
605 if (IS_ERR(tree_root
)) {
606 printk("btrfs: open_ctree failed\n");
607 return PTR_ERR(tree_root
);
609 sb
->s_fs_info
= tree_root
;
611 key
.objectid
= BTRFS_FIRST_FREE_OBJECTID
;
612 key
.type
= BTRFS_INODE_ITEM_KEY
;
614 inode
= btrfs_iget(sb
, &key
, tree_root
->fs_info
->fs_root
, NULL
);
616 err
= PTR_ERR(inode
);
620 root_dentry
= d_alloc_root(inode
);
627 sb
->s_root
= root_dentry
;
629 save_mount_options(sb
, data
);
633 close_ctree(tree_root
);
637 int btrfs_sync_fs(struct super_block
*sb
, int wait
)
639 struct btrfs_trans_handle
*trans
;
640 struct btrfs_root
*root
= btrfs_sb(sb
);
643 trace_btrfs_sync_fs(wait
);
646 filemap_flush(root
->fs_info
->btree_inode
->i_mapping
);
650 btrfs_start_delalloc_inodes(root
, 0);
651 btrfs_wait_ordered_extents(root
, 0, 0);
653 trans
= btrfs_start_transaction(root
, 0);
655 return PTR_ERR(trans
);
656 ret
= btrfs_commit_transaction(trans
, root
);
660 static int btrfs_show_options(struct seq_file
*seq
, struct vfsmount
*vfs
)
662 struct btrfs_root
*root
= btrfs_sb(vfs
->mnt_sb
);
663 struct btrfs_fs_info
*info
= root
->fs_info
;
666 if (btrfs_test_opt(root
, DEGRADED
))
667 seq_puts(seq
, ",degraded");
668 if (btrfs_test_opt(root
, NODATASUM
))
669 seq_puts(seq
, ",nodatasum");
670 if (btrfs_test_opt(root
, NODATACOW
))
671 seq_puts(seq
, ",nodatacow");
672 if (btrfs_test_opt(root
, NOBARRIER
))
673 seq_puts(seq
, ",nobarrier");
674 if (info
->max_inline
!= 8192 * 1024)
675 seq_printf(seq
, ",max_inline=%llu",
676 (unsigned long long)info
->max_inline
);
677 if (info
->alloc_start
!= 0)
678 seq_printf(seq
, ",alloc_start=%llu",
679 (unsigned long long)info
->alloc_start
);
680 if (info
->thread_pool_size
!= min_t(unsigned long,
681 num_online_cpus() + 2, 8))
682 seq_printf(seq
, ",thread_pool=%d", info
->thread_pool_size
);
683 if (btrfs_test_opt(root
, COMPRESS
)) {
684 if (info
->compress_type
== BTRFS_COMPRESS_ZLIB
)
685 compress_type
= "zlib";
687 compress_type
= "lzo";
688 if (btrfs_test_opt(root
, FORCE_COMPRESS
))
689 seq_printf(seq
, ",compress-force=%s", compress_type
);
691 seq_printf(seq
, ",compress=%s", compress_type
);
693 if (btrfs_test_opt(root
, NOSSD
))
694 seq_puts(seq
, ",nossd");
695 if (btrfs_test_opt(root
, SSD_SPREAD
))
696 seq_puts(seq
, ",ssd_spread");
697 else if (btrfs_test_opt(root
, SSD
))
698 seq_puts(seq
, ",ssd");
699 if (btrfs_test_opt(root
, NOTREELOG
))
700 seq_puts(seq
, ",notreelog");
701 if (btrfs_test_opt(root
, FLUSHONCOMMIT
))
702 seq_puts(seq
, ",flushoncommit");
703 if (btrfs_test_opt(root
, DISCARD
))
704 seq_puts(seq
, ",discard");
705 if (!(root
->fs_info
->sb
->s_flags
& MS_POSIXACL
))
706 seq_puts(seq
, ",noacl");
707 if (btrfs_test_opt(root
, SPACE_CACHE
))
708 seq_puts(seq
, ",space_cache");
709 if (btrfs_test_opt(root
, CLEAR_CACHE
))
710 seq_puts(seq
, ",clear_cache");
711 if (btrfs_test_opt(root
, USER_SUBVOL_RM_ALLOWED
))
712 seq_puts(seq
, ",user_subvol_rm_allowed");
716 static int btrfs_test_super(struct super_block
*s
, void *data
)
718 struct btrfs_root
*test_root
= data
;
719 struct btrfs_root
*root
= btrfs_sb(s
);
722 * If this super block is going away, return false as it
723 * can't match as an existing super block.
725 if (!atomic_read(&s
->s_active
))
727 return root
->fs_info
->fs_devices
== test_root
->fs_info
->fs_devices
;
730 static int btrfs_set_super(struct super_block
*s
, void *data
)
734 return set_anon_super(s
, data
);
739 * Find a superblock for the given device / mount point.
741 * Note: This is based on get_sb_bdev from fs/super.c with a few additions
742 * for multiple device setup. Make sure to keep it in sync.
744 static struct dentry
*btrfs_mount(struct file_system_type
*fs_type
, int flags
,
745 const char *device_name
, void *data
)
747 struct block_device
*bdev
= NULL
;
748 struct super_block
*s
;
750 struct btrfs_fs_devices
*fs_devices
= NULL
;
751 struct btrfs_root
*tree_root
= NULL
;
752 struct btrfs_fs_info
*fs_info
= NULL
;
753 fmode_t mode
= FMODE_READ
;
754 char *subvol_name
= NULL
;
755 u64 subvol_objectid
= 0;
756 u64 subvol_rootid
= 0;
759 if (!(flags
& MS_RDONLY
))
762 error
= btrfs_parse_early_options(data
, mode
, fs_type
,
763 &subvol_name
, &subvol_objectid
,
764 &subvol_rootid
, &fs_devices
);
766 return ERR_PTR(error
);
768 error
= btrfs_scan_one_device(device_name
, mode
, fs_type
, &fs_devices
);
770 goto error_free_subvol_name
;
772 error
= btrfs_open_devices(fs_devices
, mode
, fs_type
);
774 goto error_free_subvol_name
;
776 if (!(flags
& MS_RDONLY
) && fs_devices
->rw_devices
== 0) {
778 goto error_close_devices
;
782 * Setup a dummy root and fs_info for test/set super. This is because
783 * we don't actually fill this stuff out until open_ctree, but we need
784 * it for searching for existing supers, so this lets us do that and
785 * then open_ctree will properly initialize everything later.
787 fs_info
= kzalloc(sizeof(struct btrfs_fs_info
), GFP_NOFS
);
788 tree_root
= kzalloc(sizeof(struct btrfs_root
), GFP_NOFS
);
789 if (!fs_info
|| !tree_root
) {
791 goto error_close_devices
;
793 fs_info
->tree_root
= tree_root
;
794 fs_info
->fs_devices
= fs_devices
;
795 tree_root
->fs_info
= fs_info
;
797 bdev
= fs_devices
->latest_bdev
;
798 s
= sget(fs_type
, btrfs_test_super
, btrfs_set_super
, tree_root
);
803 if ((flags
^ s
->s_flags
) & MS_RDONLY
) {
804 deactivate_locked_super(s
);
806 goto error_close_devices
;
809 btrfs_close_devices(fs_devices
);
813 char b
[BDEVNAME_SIZE
];
816 strlcpy(s
->s_id
, bdevname(bdev
, b
), sizeof(s
->s_id
));
817 error
= btrfs_fill_super(s
, fs_devices
, data
,
818 flags
& MS_SILENT
? 1 : 0);
820 deactivate_locked_super(s
);
821 goto error_free_subvol_name
;
824 btrfs_sb(s
)->fs_info
->bdev_holder
= fs_type
;
825 s
->s_flags
|= MS_ACTIVE
;
828 /* if they gave us a subvolume name bind mount into that */
829 if (strcmp(subvol_name
, ".")) {
830 struct dentry
*new_root
;
832 root
= get_default_root(s
, subvol_rootid
);
834 error
= PTR_ERR(root
);
835 deactivate_locked_super(s
);
836 goto error_free_subvol_name
;
839 mutex_lock(&root
->d_inode
->i_mutex
);
840 new_root
= lookup_one_len(subvol_name
, root
,
841 strlen(subvol_name
));
842 mutex_unlock(&root
->d_inode
->i_mutex
);
844 if (IS_ERR(new_root
)) {
846 deactivate_locked_super(s
);
847 error
= PTR_ERR(new_root
);
848 goto error_free_subvol_name
;
850 if (!new_root
->d_inode
) {
853 deactivate_locked_super(s
);
855 goto error_free_subvol_name
;
860 root
= get_default_root(s
, subvol_objectid
);
862 error
= PTR_ERR(root
);
863 deactivate_locked_super(s
);
864 goto error_free_subvol_name
;
874 btrfs_close_devices(fs_devices
);
877 error_free_subvol_name
:
879 return ERR_PTR(error
);
882 static int btrfs_remount(struct super_block
*sb
, int *flags
, char *data
)
884 struct btrfs_root
*root
= btrfs_sb(sb
);
887 ret
= btrfs_parse_options(root
, data
);
891 if ((*flags
& MS_RDONLY
) == (sb
->s_flags
& MS_RDONLY
))
894 if (*flags
& MS_RDONLY
) {
895 sb
->s_flags
|= MS_RDONLY
;
897 ret
= btrfs_commit_super(root
);
900 if (root
->fs_info
->fs_devices
->rw_devices
== 0)
903 if (btrfs_super_log_root(&root
->fs_info
->super_copy
) != 0)
906 ret
= btrfs_cleanup_fs_roots(root
->fs_info
);
909 /* recover relocation */
910 ret
= btrfs_recover_relocation(root
);
913 sb
->s_flags
&= ~MS_RDONLY
;
919 /* Used to sort the devices by max_avail(descending sort) */
920 static int btrfs_cmp_device_free_bytes(const void *dev_info1
,
921 const void *dev_info2
)
923 if (((struct btrfs_device_info
*)dev_info1
)->max_avail
>
924 ((struct btrfs_device_info
*)dev_info2
)->max_avail
)
926 else if (((struct btrfs_device_info
*)dev_info1
)->max_avail
<
927 ((struct btrfs_device_info
*)dev_info2
)->max_avail
)
934 * sort the devices by max_avail, in which max free extent size of each device
935 * is stored.(Descending Sort)
937 static inline void btrfs_descending_sort_devices(
938 struct btrfs_device_info
*devices
,
941 sort(devices
, nr_devices
, sizeof(struct btrfs_device_info
),
942 btrfs_cmp_device_free_bytes
, NULL
);
946 * The helper to calc the free space on the devices that can be used to store
949 static int btrfs_calc_avail_data_space(struct btrfs_root
*root
, u64
*free_bytes
)
951 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
952 struct btrfs_device_info
*devices_info
;
953 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
954 struct btrfs_device
*device
;
961 int i
= 0, nr_devices
;
964 nr_devices
= fs_info
->fs_devices
->rw_devices
;
967 devices_info
= kmalloc(sizeof(*devices_info
) * nr_devices
,
972 /* calc min stripe number for data space alloction */
973 type
= btrfs_get_alloc_profile(root
, 1);
974 if (type
& BTRFS_BLOCK_GROUP_RAID0
)
976 else if (type
& BTRFS_BLOCK_GROUP_RAID1
)
978 else if (type
& BTRFS_BLOCK_GROUP_RAID10
)
981 if (type
& BTRFS_BLOCK_GROUP_DUP
)
982 min_stripe_size
= 2 * BTRFS_STRIPE_LEN
;
984 min_stripe_size
= BTRFS_STRIPE_LEN
;
986 list_for_each_entry(device
, &fs_devices
->alloc_list
, dev_alloc_list
) {
987 if (!device
->in_fs_metadata
)
990 avail_space
= device
->total_bytes
- device
->bytes_used
;
992 /* align with stripe_len */
993 do_div(avail_space
, BTRFS_STRIPE_LEN
);
994 avail_space
*= BTRFS_STRIPE_LEN
;
997 * In order to avoid overwritting the superblock on the drive,
998 * btrfs starts at an offset of at least 1MB when doing chunk
1001 skip_space
= 1024 * 1024;
1003 /* user can set the offset in fs_info->alloc_start. */
1004 if (fs_info
->alloc_start
+ BTRFS_STRIPE_LEN
<=
1005 device
->total_bytes
)
1006 skip_space
= max(fs_info
->alloc_start
, skip_space
);
1009 * btrfs can not use the free space in [0, skip_space - 1],
1010 * we must subtract it from the total. In order to implement
1011 * it, we account the used space in this range first.
1013 ret
= btrfs_account_dev_extents_size(device
, 0, skip_space
- 1,
1016 kfree(devices_info
);
1020 /* calc the free space in [0, skip_space - 1] */
1021 skip_space
-= used_space
;
1024 * we can use the free space in [0, skip_space - 1], subtract
1025 * it from the total.
1027 if (avail_space
&& avail_space
>= skip_space
)
1028 avail_space
-= skip_space
;
1032 if (avail_space
< min_stripe_size
)
1035 devices_info
[i
].dev
= device
;
1036 devices_info
[i
].max_avail
= avail_space
;
1043 btrfs_descending_sort_devices(devices_info
, nr_devices
);
1047 while (nr_devices
>= min_stripes
) {
1048 if (devices_info
[i
].max_avail
>= min_stripe_size
) {
1052 avail_space
+= devices_info
[i
].max_avail
* min_stripes
;
1053 alloc_size
= devices_info
[i
].max_avail
;
1054 for (j
= i
+ 1 - min_stripes
; j
<= i
; j
++)
1055 devices_info
[j
].max_avail
-= alloc_size
;
1061 kfree(devices_info
);
1062 *free_bytes
= avail_space
;
1066 static int btrfs_statfs(struct dentry
*dentry
, struct kstatfs
*buf
)
1068 struct btrfs_root
*root
= btrfs_sb(dentry
->d_sb
);
1069 struct btrfs_super_block
*disk_super
= &root
->fs_info
->super_copy
;
1070 struct list_head
*head
= &root
->fs_info
->space_info
;
1071 struct btrfs_space_info
*found
;
1073 u64 total_free_data
= 0;
1074 int bits
= dentry
->d_sb
->s_blocksize_bits
;
1075 __be32
*fsid
= (__be32
*)root
->fs_info
->fsid
;
1078 /* holding chunk_muext to avoid allocating new chunks */
1079 mutex_lock(&root
->fs_info
->chunk_mutex
);
1081 list_for_each_entry_rcu(found
, head
, list
) {
1082 if (found
->flags
& BTRFS_BLOCK_GROUP_DATA
) {
1083 total_free_data
+= found
->disk_total
- found
->disk_used
;
1085 btrfs_account_ro_block_groups_free_space(found
);
1088 total_used
+= found
->disk_used
;
1092 buf
->f_namelen
= BTRFS_NAME_LEN
;
1093 buf
->f_blocks
= btrfs_super_total_bytes(disk_super
) >> bits
;
1094 buf
->f_bfree
= buf
->f_blocks
- (total_used
>> bits
);
1095 buf
->f_bsize
= dentry
->d_sb
->s_blocksize
;
1096 buf
->f_type
= BTRFS_SUPER_MAGIC
;
1097 buf
->f_bavail
= total_free_data
;
1098 ret
= btrfs_calc_avail_data_space(root
, &total_free_data
);
1100 mutex_unlock(&root
->fs_info
->chunk_mutex
);
1103 buf
->f_bavail
+= total_free_data
;
1104 buf
->f_bavail
= buf
->f_bavail
>> bits
;
1105 mutex_unlock(&root
->fs_info
->chunk_mutex
);
1107 /* We treat it as constant endianness (it doesn't matter _which_)
1108 because we want the fsid to come out the same whether mounted
1109 on a big-endian or little-endian host */
1110 buf
->f_fsid
.val
[0] = be32_to_cpu(fsid
[0]) ^ be32_to_cpu(fsid
[2]);
1111 buf
->f_fsid
.val
[1] = be32_to_cpu(fsid
[1]) ^ be32_to_cpu(fsid
[3]);
1112 /* Mask in the root object ID too, to disambiguate subvols */
1113 buf
->f_fsid
.val
[0] ^= BTRFS_I(dentry
->d_inode
)->root
->objectid
>> 32;
1114 buf
->f_fsid
.val
[1] ^= BTRFS_I(dentry
->d_inode
)->root
->objectid
;
1119 static struct file_system_type btrfs_fs_type
= {
1120 .owner
= THIS_MODULE
,
1122 .mount
= btrfs_mount
,
1123 .kill_sb
= kill_anon_super
,
1124 .fs_flags
= FS_REQUIRES_DEV
,
1128 * used by btrfsctl to scan devices when no FS is mounted
1130 static long btrfs_control_ioctl(struct file
*file
, unsigned int cmd
,
1133 struct btrfs_ioctl_vol_args
*vol
;
1134 struct btrfs_fs_devices
*fs_devices
;
1137 if (!capable(CAP_SYS_ADMIN
))
1140 vol
= memdup_user((void __user
*)arg
, sizeof(*vol
));
1142 return PTR_ERR(vol
);
1145 case BTRFS_IOC_SCAN_DEV
:
1146 ret
= btrfs_scan_one_device(vol
->name
, FMODE_READ
,
1147 &btrfs_fs_type
, &fs_devices
);
1155 static int btrfs_freeze(struct super_block
*sb
)
1157 struct btrfs_root
*root
= btrfs_sb(sb
);
1158 mutex_lock(&root
->fs_info
->transaction_kthread_mutex
);
1159 mutex_lock(&root
->fs_info
->cleaner_mutex
);
1163 static int btrfs_unfreeze(struct super_block
*sb
)
1165 struct btrfs_root
*root
= btrfs_sb(sb
);
1166 mutex_unlock(&root
->fs_info
->cleaner_mutex
);
1167 mutex_unlock(&root
->fs_info
->transaction_kthread_mutex
);
1171 static const struct super_operations btrfs_super_ops
= {
1172 .drop_inode
= btrfs_drop_inode
,
1173 .evict_inode
= btrfs_evict_inode
,
1174 .put_super
= btrfs_put_super
,
1175 .sync_fs
= btrfs_sync_fs
,
1176 .show_options
= btrfs_show_options
,
1177 .write_inode
= btrfs_write_inode
,
1178 .dirty_inode
= btrfs_dirty_inode
,
1179 .alloc_inode
= btrfs_alloc_inode
,
1180 .destroy_inode
= btrfs_destroy_inode
,
1181 .statfs
= btrfs_statfs
,
1182 .remount_fs
= btrfs_remount
,
1183 .freeze_fs
= btrfs_freeze
,
1184 .unfreeze_fs
= btrfs_unfreeze
,
1187 static const struct file_operations btrfs_ctl_fops
= {
1188 .unlocked_ioctl
= btrfs_control_ioctl
,
1189 .compat_ioctl
= btrfs_control_ioctl
,
1190 .owner
= THIS_MODULE
,
1191 .llseek
= noop_llseek
,
1194 static struct miscdevice btrfs_misc
= {
1195 .minor
= BTRFS_MINOR
,
1196 .name
= "btrfs-control",
1197 .fops
= &btrfs_ctl_fops
1200 MODULE_ALIAS_MISCDEV(BTRFS_MINOR
);
1201 MODULE_ALIAS("devname:btrfs-control");
1203 static int btrfs_interface_init(void)
1205 return misc_register(&btrfs_misc
);
1208 static void btrfs_interface_exit(void)
1210 if (misc_deregister(&btrfs_misc
) < 0)
1211 printk(KERN_INFO
"misc_deregister failed for control device");
1214 static int __init
init_btrfs_fs(void)
1218 err
= btrfs_init_sysfs();
1222 err
= btrfs_init_compress();
1226 err
= btrfs_init_cachep();
1230 err
= extent_io_init();
1234 err
= extent_map_init();
1236 goto free_extent_io
;
1238 err
= btrfs_delayed_inode_init();
1240 goto free_extent_map
;
1242 err
= btrfs_interface_init();
1244 goto free_delayed_inode
;
1246 err
= register_filesystem(&btrfs_fs_type
);
1248 goto unregister_ioctl
;
1250 printk(KERN_INFO
"%s loaded\n", BTRFS_BUILD_VERSION
);
1254 btrfs_interface_exit();
1256 btrfs_delayed_inode_exit();
1262 btrfs_destroy_cachep();
1264 btrfs_exit_compress();
1270 static void __exit
exit_btrfs_fs(void)
1272 btrfs_destroy_cachep();
1273 btrfs_delayed_inode_exit();
1276 btrfs_interface_exit();
1277 unregister_filesystem(&btrfs_fs_type
);
1279 btrfs_cleanup_fs_uuids();
1280 btrfs_exit_compress();
1283 module_init(init_btrfs_fs
)
1284 module_exit(exit_btrfs_fs
)
1286 MODULE_LICENSE("GPL");