aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs/super.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/btrfs/super.c')
-rw-r--r--fs/btrfs/super.c296
1 files changed, 276 insertions, 20 deletions
diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
index 883c6fa1367e..d39a9895d932 100644
--- a/fs/btrfs/super.c
+++ b/fs/btrfs/super.c
@@ -54,6 +54,90 @@
54 54
55static const struct super_operations btrfs_super_ops; 55static const struct super_operations btrfs_super_ops;
56 56
57static const char *btrfs_decode_error(struct btrfs_fs_info *fs_info, int errno,
58 char nbuf[16])
59{
60 char *errstr = NULL;
61
62 switch (errno) {
63 case -EIO:
64 errstr = "IO failure";
65 break;
66 case -ENOMEM:
67 errstr = "Out of memory";
68 break;
69 case -EROFS:
70 errstr = "Readonly filesystem";
71 break;
72 default:
73 if (nbuf) {
74 if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
75 errstr = nbuf;
76 }
77 break;
78 }
79
80 return errstr;
81}
82
83static void __save_error_info(struct btrfs_fs_info *fs_info)
84{
85 /*
86 * today we only save the error info into ram. Long term we'll
87 * also send it down to the disk
88 */
89 fs_info->fs_state = BTRFS_SUPER_FLAG_ERROR;
90}
91
92/* NOTE:
93 * We move write_super stuff at umount in order to avoid deadlock
94 * for umount hold all lock.
95 */
96static void save_error_info(struct btrfs_fs_info *fs_info)
97{
98 __save_error_info(fs_info);
99}
100
101/* btrfs handle error by forcing the filesystem readonly */
102static void btrfs_handle_error(struct btrfs_fs_info *fs_info)
103{
104 struct super_block *sb = fs_info->sb;
105
106 if (sb->s_flags & MS_RDONLY)
107 return;
108
109 if (fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
110 sb->s_flags |= MS_RDONLY;
111 printk(KERN_INFO "btrfs is forced readonly\n");
112 }
113}
114
115/*
116 * __btrfs_std_error decodes expected errors from the caller and
117 * invokes the approciate error response.
118 */
119void __btrfs_std_error(struct btrfs_fs_info *fs_info, const char *function,
120 unsigned int line, int errno)
121{
122 struct super_block *sb = fs_info->sb;
123 char nbuf[16];
124 const char *errstr;
125
126 /*
127 * Special case: if the error is EROFS, and we're already
128 * under MS_RDONLY, then it is safe here.
129 */
130 if (errno == -EROFS && (sb->s_flags & MS_RDONLY))
131 return;
132
133 errstr = btrfs_decode_error(fs_info, errno, nbuf);
134 printk(KERN_CRIT "BTRFS error (device %s) in %s:%d: %s\n",
135 sb->s_id, function, line, errstr);
136 save_error_info(fs_info);
137
138 btrfs_handle_error(fs_info);
139}
140
57static void btrfs_put_super(struct super_block *sb) 141static void btrfs_put_super(struct super_block *sb)
58{ 142{
59 struct btrfs_root *root = btrfs_sb(sb); 143 struct btrfs_root *root = btrfs_sb(sb);
@@ -69,9 +153,10 @@ enum {
69 Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum, 153 Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum,
70 Opt_nodatacow, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd, 154 Opt_nodatacow, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd,
71 Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, Opt_compress, 155 Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, Opt_compress,
72 Opt_compress_force, Opt_notreelog, Opt_ratio, Opt_flushoncommit, 156 Opt_compress_type, Opt_compress_force, Opt_compress_force_type,
73 Opt_discard, Opt_space_cache, Opt_clear_cache, Opt_err, 157 Opt_notreelog, Opt_ratio, Opt_flushoncommit, Opt_discard,
74 Opt_user_subvol_rm_allowed, 158 Opt_space_cache, Opt_clear_cache, Opt_user_subvol_rm_allowed,
159 Opt_enospc_debug, Opt_err,
75}; 160};
76 161
77static match_table_t tokens = { 162static match_table_t tokens = {
@@ -86,7 +171,9 @@ static match_table_t tokens = {
86 {Opt_alloc_start, "alloc_start=%s"}, 171 {Opt_alloc_start, "alloc_start=%s"},
87 {Opt_thread_pool, "thread_pool=%d"}, 172 {Opt_thread_pool, "thread_pool=%d"},
88 {Opt_compress, "compress"}, 173 {Opt_compress, "compress"},
174 {Opt_compress_type, "compress=%s"},
89 {Opt_compress_force, "compress-force"}, 175 {Opt_compress_force, "compress-force"},
176 {Opt_compress_force_type, "compress-force=%s"},
90 {Opt_ssd, "ssd"}, 177 {Opt_ssd, "ssd"},
91 {Opt_ssd_spread, "ssd_spread"}, 178 {Opt_ssd_spread, "ssd_spread"},
92 {Opt_nossd, "nossd"}, 179 {Opt_nossd, "nossd"},
@@ -98,6 +185,7 @@ static match_table_t tokens = {
98 {Opt_space_cache, "space_cache"}, 185 {Opt_space_cache, "space_cache"},
99 {Opt_clear_cache, "clear_cache"}, 186 {Opt_clear_cache, "clear_cache"},
100 {Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"}, 187 {Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"},
188 {Opt_enospc_debug, "enospc_debug"},
101 {Opt_err, NULL}, 189 {Opt_err, NULL},
102}; 190};
103 191
@@ -112,6 +200,8 @@ int btrfs_parse_options(struct btrfs_root *root, char *options)
112 char *p, *num, *orig; 200 char *p, *num, *orig;
113 int intarg; 201 int intarg;
114 int ret = 0; 202 int ret = 0;
203 char *compress_type;
204 bool compress_force = false;
115 205
116 if (!options) 206 if (!options)
117 return 0; 207 return 0;
@@ -154,14 +244,32 @@ int btrfs_parse_options(struct btrfs_root *root, char *options)
154 btrfs_set_opt(info->mount_opt, NODATACOW); 244 btrfs_set_opt(info->mount_opt, NODATACOW);
155 btrfs_set_opt(info->mount_opt, NODATASUM); 245 btrfs_set_opt(info->mount_opt, NODATASUM);
156 break; 246 break;
157 case Opt_compress:
158 printk(KERN_INFO "btrfs: use compression\n");
159 btrfs_set_opt(info->mount_opt, COMPRESS);
160 break;
161 case Opt_compress_force: 247 case Opt_compress_force:
162 printk(KERN_INFO "btrfs: forcing compression\n"); 248 case Opt_compress_force_type:
163 btrfs_set_opt(info->mount_opt, FORCE_COMPRESS); 249 compress_force = true;
250 case Opt_compress:
251 case Opt_compress_type:
252 if (token == Opt_compress ||
253 token == Opt_compress_force ||
254 strcmp(args[0].from, "zlib") == 0) {
255 compress_type = "zlib";
256 info->compress_type = BTRFS_COMPRESS_ZLIB;
257 } else if (strcmp(args[0].from, "lzo") == 0) {
258 compress_type = "lzo";
259 info->compress_type = BTRFS_COMPRESS_LZO;
260 } else {
261 ret = -EINVAL;
262 goto out;
263 }
264
164 btrfs_set_opt(info->mount_opt, COMPRESS); 265 btrfs_set_opt(info->mount_opt, COMPRESS);
266 if (compress_force) {
267 btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
268 pr_info("btrfs: force %s compression\n",
269 compress_type);
270 } else
271 pr_info("btrfs: use %s compression\n",
272 compress_type);
165 break; 273 break;
166 case Opt_ssd: 274 case Opt_ssd:
167 printk(KERN_INFO "btrfs: use ssd allocation scheme\n"); 275 printk(KERN_INFO "btrfs: use ssd allocation scheme\n");
@@ -252,6 +360,9 @@ int btrfs_parse_options(struct btrfs_root *root, char *options)
252 case Opt_user_subvol_rm_allowed: 360 case Opt_user_subvol_rm_allowed:
253 btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED); 361 btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED);
254 break; 362 break;
363 case Opt_enospc_debug:
364 btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG);
365 break;
255 case Opt_err: 366 case Opt_err:
256 printk(KERN_INFO "btrfs: unrecognized mount option " 367 printk(KERN_INFO "btrfs: unrecognized mount option "
257 "'%s'\n", p); 368 "'%s'\n", p);
@@ -277,7 +388,7 @@ static int btrfs_parse_early_options(const char *options, fmode_t flags,
277 struct btrfs_fs_devices **fs_devices) 388 struct btrfs_fs_devices **fs_devices)
278{ 389{
279 substring_t args[MAX_OPT_ARGS]; 390 substring_t args[MAX_OPT_ARGS];
280 char *opts, *p; 391 char *opts, *orig, *p;
281 int error = 0; 392 int error = 0;
282 int intarg; 393 int intarg;
283 394
@@ -291,6 +402,7 @@ static int btrfs_parse_early_options(const char *options, fmode_t flags,
291 opts = kstrdup(options, GFP_KERNEL); 402 opts = kstrdup(options, GFP_KERNEL);
292 if (!opts) 403 if (!opts)
293 return -ENOMEM; 404 return -ENOMEM;
405 orig = opts;
294 406
295 while ((p = strsep(&opts, ",")) != NULL) { 407 while ((p = strsep(&opts, ",")) != NULL) {
296 int token; 408 int token;
@@ -326,7 +438,7 @@ static int btrfs_parse_early_options(const char *options, fmode_t flags,
326 } 438 }
327 439
328 out_free_opts: 440 out_free_opts:
329 kfree(opts); 441 kfree(orig);
330 out: 442 out:
331 /* 443 /*
332 * If no subvolume name is specified we use the default one. Allocate 444 * If no subvolume name is specified we use the default one. Allocate
@@ -460,6 +572,7 @@ static int btrfs_fill_super(struct super_block *sb,
460 sb->s_maxbytes = MAX_LFS_FILESIZE; 572 sb->s_maxbytes = MAX_LFS_FILESIZE;
461 sb->s_magic = BTRFS_SUPER_MAGIC; 573 sb->s_magic = BTRFS_SUPER_MAGIC;
462 sb->s_op = &btrfs_super_ops; 574 sb->s_op = &btrfs_super_ops;
575 sb->s_d_op = &btrfs_dentry_operations;
463 sb->s_export_op = &btrfs_export_ops; 576 sb->s_export_op = &btrfs_export_ops;
464 sb->s_xattr = btrfs_xattr_handlers; 577 sb->s_xattr = btrfs_xattr_handlers;
465 sb->s_time_gran = 1; 578 sb->s_time_gran = 1;
@@ -516,6 +629,8 @@ int btrfs_sync_fs(struct super_block *sb, int wait)
516 btrfs_wait_ordered_extents(root, 0, 0); 629 btrfs_wait_ordered_extents(root, 0, 0);
517 630
518 trans = btrfs_start_transaction(root, 0); 631 trans = btrfs_start_transaction(root, 0);
632 if (IS_ERR(trans))
633 return PTR_ERR(trans);
519 ret = btrfs_commit_transaction(trans, root); 634 ret = btrfs_commit_transaction(trans, root);
520 return ret; 635 return ret;
521} 636}
@@ -654,6 +769,8 @@ static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
654 } 769 }
655 770
656 btrfs_close_devices(fs_devices); 771 btrfs_close_devices(fs_devices);
772 kfree(fs_info);
773 kfree(tree_root);
657 } else { 774 } else {
658 char b[BDEVNAME_SIZE]; 775 char b[BDEVNAME_SIZE];
659 776
@@ -752,6 +869,127 @@ static int btrfs_remount(struct super_block *sb, int *flags, char *data)
752 return 0; 869 return 0;
753} 870}
754 871
872/*
873 * The helper to calc the free space on the devices that can be used to store
874 * file data.
875 */
876static int btrfs_calc_avail_data_space(struct btrfs_root *root, u64 *free_bytes)
877{
878 struct btrfs_fs_info *fs_info = root->fs_info;
879 struct btrfs_device_info *devices_info;
880 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
881 struct btrfs_device *device;
882 u64 skip_space;
883 u64 type;
884 u64 avail_space;
885 u64 used_space;
886 u64 min_stripe_size;
887 int min_stripes = 1;
888 int i = 0, nr_devices;
889 int ret;
890
891 nr_devices = fs_info->fs_devices->rw_devices;
892 BUG_ON(!nr_devices);
893
894 devices_info = kmalloc(sizeof(*devices_info) * nr_devices,
895 GFP_NOFS);
896 if (!devices_info)
897 return -ENOMEM;
898
899 /* calc min stripe number for data space alloction */
900 type = btrfs_get_alloc_profile(root, 1);
901 if (type & BTRFS_BLOCK_GROUP_RAID0)
902 min_stripes = 2;
903 else if (type & BTRFS_BLOCK_GROUP_RAID1)
904 min_stripes = 2;
905 else if (type & BTRFS_BLOCK_GROUP_RAID10)
906 min_stripes = 4;
907
908 if (type & BTRFS_BLOCK_GROUP_DUP)
909 min_stripe_size = 2 * BTRFS_STRIPE_LEN;
910 else
911 min_stripe_size = BTRFS_STRIPE_LEN;
912
913 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
914 if (!device->in_fs_metadata)
915 continue;
916
917 avail_space = device->total_bytes - device->bytes_used;
918
919 /* align with stripe_len */
920 do_div(avail_space, BTRFS_STRIPE_LEN);
921 avail_space *= BTRFS_STRIPE_LEN;
922
923 /*
924 * In order to avoid overwritting the superblock on the drive,
925 * btrfs starts at an offset of at least 1MB when doing chunk
926 * allocation.
927 */
928 skip_space = 1024 * 1024;
929
930 /* user can set the offset in fs_info->alloc_start. */
931 if (fs_info->alloc_start + BTRFS_STRIPE_LEN <=
932 device->total_bytes)
933 skip_space = max(fs_info->alloc_start, skip_space);
934
935 /*
936 * btrfs can not use the free space in [0, skip_space - 1],
937 * we must subtract it from the total. In order to implement
938 * it, we account the used space in this range first.
939 */
940 ret = btrfs_account_dev_extents_size(device, 0, skip_space - 1,
941 &used_space);
942 if (ret) {
943 kfree(devices_info);
944 return ret;
945 }
946
947 /* calc the free space in [0, skip_space - 1] */
948 skip_space -= used_space;
949
950 /*
951 * we can use the free space in [0, skip_space - 1], subtract
952 * it from the total.
953 */
954 if (avail_space && avail_space >= skip_space)
955 avail_space -= skip_space;
956 else
957 avail_space = 0;
958
959 if (avail_space < min_stripe_size)
960 continue;
961
962 devices_info[i].dev = device;
963 devices_info[i].max_avail = avail_space;
964
965 i++;
966 }
967
968 nr_devices = i;
969
970 btrfs_descending_sort_devices(devices_info, nr_devices);
971
972 i = nr_devices - 1;
973 avail_space = 0;
974 while (nr_devices >= min_stripes) {
975 if (devices_info[i].max_avail >= min_stripe_size) {
976 int j;
977 u64 alloc_size;
978
979 avail_space += devices_info[i].max_avail * min_stripes;
980 alloc_size = devices_info[i].max_avail;
981 for (j = i + 1 - min_stripes; j <= i; j++)
982 devices_info[j].max_avail -= alloc_size;
983 }
984 i--;
985 nr_devices--;
986 }
987
988 kfree(devices_info);
989 *free_bytes = avail_space;
990 return 0;
991}
992
755static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf) 993static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
756{ 994{
757 struct btrfs_root *root = btrfs_sb(dentry->d_sb); 995 struct btrfs_root *root = btrfs_sb(dentry->d_sb);
@@ -759,17 +997,21 @@ static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
759 struct list_head *head = &root->fs_info->space_info; 997 struct list_head *head = &root->fs_info->space_info;
760 struct btrfs_space_info *found; 998 struct btrfs_space_info *found;
761 u64 total_used = 0; 999 u64 total_used = 0;
762 u64 total_used_data = 0; 1000 u64 total_free_data = 0;
763 int bits = dentry->d_sb->s_blocksize_bits; 1001 int bits = dentry->d_sb->s_blocksize_bits;
764 __be32 *fsid = (__be32 *)root->fs_info->fsid; 1002 __be32 *fsid = (__be32 *)root->fs_info->fsid;
1003 int ret;
765 1004
1005 /* holding chunk_muext to avoid allocating new chunks */
1006 mutex_lock(&root->fs_info->chunk_mutex);
766 rcu_read_lock(); 1007 rcu_read_lock();
767 list_for_each_entry_rcu(found, head, list) { 1008 list_for_each_entry_rcu(found, head, list) {
768 if (found->flags & (BTRFS_BLOCK_GROUP_METADATA | 1009 if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
769 BTRFS_BLOCK_GROUP_SYSTEM)) 1010 total_free_data += found->disk_total - found->disk_used;
770 total_used_data += found->disk_total; 1011 total_free_data -=
771 else 1012 btrfs_account_ro_block_groups_free_space(found);
772 total_used_data += found->disk_used; 1013 }
1014
773 total_used += found->disk_used; 1015 total_used += found->disk_used;
774 } 1016 }
775 rcu_read_unlock(); 1017 rcu_read_unlock();
@@ -777,9 +1019,17 @@ static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
777 buf->f_namelen = BTRFS_NAME_LEN; 1019 buf->f_namelen = BTRFS_NAME_LEN;
778 buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits; 1020 buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
779 buf->f_bfree = buf->f_blocks - (total_used >> bits); 1021 buf->f_bfree = buf->f_blocks - (total_used >> bits);
780 buf->f_bavail = buf->f_blocks - (total_used_data >> bits);
781 buf->f_bsize = dentry->d_sb->s_blocksize; 1022 buf->f_bsize = dentry->d_sb->s_blocksize;
782 buf->f_type = BTRFS_SUPER_MAGIC; 1023 buf->f_type = BTRFS_SUPER_MAGIC;
1024 buf->f_bavail = total_free_data;
1025 ret = btrfs_calc_avail_data_space(root, &total_free_data);
1026 if (ret) {
1027 mutex_unlock(&root->fs_info->chunk_mutex);
1028 return ret;
1029 }
1030 buf->f_bavail += total_free_data;
1031 buf->f_bavail = buf->f_bavail >> bits;
1032 mutex_unlock(&root->fs_info->chunk_mutex);
783 1033
784 /* We treat it as constant endianness (it doesn't matter _which_) 1034 /* We treat it as constant endianness (it doesn't matter _which_)
785 because we want the fsid to come out the same whether mounted 1035 because we want the fsid to come out the same whether mounted
@@ -896,10 +1146,14 @@ static int __init init_btrfs_fs(void)
896 if (err) 1146 if (err)
897 return err; 1147 return err;
898 1148
899 err = btrfs_init_cachep(); 1149 err = btrfs_init_compress();
900 if (err) 1150 if (err)
901 goto free_sysfs; 1151 goto free_sysfs;
902 1152
1153 err = btrfs_init_cachep();
1154 if (err)
1155 goto free_compress;
1156
903 err = extent_io_init(); 1157 err = extent_io_init();
904 if (err) 1158 if (err)
905 goto free_cachep; 1159 goto free_cachep;
@@ -927,6 +1181,8 @@ free_extent_io:
927 extent_io_exit(); 1181 extent_io_exit();
928free_cachep: 1182free_cachep:
929 btrfs_destroy_cachep(); 1183 btrfs_destroy_cachep();
1184free_compress:
1185 btrfs_exit_compress();
930free_sysfs: 1186free_sysfs:
931 btrfs_exit_sysfs(); 1187 btrfs_exit_sysfs();
932 return err; 1188 return err;
@@ -941,7 +1197,7 @@ static void __exit exit_btrfs_fs(void)
941 unregister_filesystem(&btrfs_fs_type); 1197 unregister_filesystem(&btrfs_fs_type);
942 btrfs_exit_sysfs(); 1198 btrfs_exit_sysfs();
943 btrfs_cleanup_fs_uuids(); 1199 btrfs_cleanup_fs_uuids();
944 btrfs_zlib_exit(); 1200 btrfs_exit_compress();
945} 1201}
946 1202
947module_init(init_btrfs_fs) 1203module_init(init_btrfs_fs)