20 #include <linux/module.h>
25 #include <linux/time.h>
28 #include <linux/string.h>
37 #include <linux/ctype.h>
40 #include <linux/magic.h>
41 #include <linux/slab.h>
59 #define CREATE_TRACE_POINTS
72 errstr =
"IO failure";
75 errstr =
"Out of memory";
78 errstr =
"Readonly filesystem";
81 errstr =
"Object already exists";
85 if (
snprintf(nbuf, 16,
"error %d", -errno) >= 0)
105 __save_error_info(fs_info);
109 static void btrfs_handle_error(
struct btrfs_fs_info *fs_info)
130 unsigned int line,
int errno,
const char *
fmt, ...)
145 errstr = btrfs_decode_error(fs_info, errno, nbuf);
153 sb->
s_id,
function, line, errstr, &vaf);
156 sb->
s_id,
function, line, errstr);
161 save_error_info(fs_info);
162 btrfs_handle_error(fs_info);
167 static const char *
const logtypes[] = {
178 void btrfs_printk(
struct btrfs_fs_info *fs_info,
const char *fmt, ...)
184 const char *
type = logtypes[4];
189 kern_level = printk_get_level(fmt);
191 size_t size = printk_skip_level(fmt) -
fmt;
195 type = logtypes[kern_level -
'0'];
202 printk(
"%sBTRFS %s (device %s): %pV", lvl, type, sb->
s_id, &vaf);
210 unsigned int line,
int errno,
const char *fmt, ...)
223 save_error_info(fs_info);
224 btrfs_handle_error(fs_info);
243 struct btrfs_root *root,
const char *
function,
244 unsigned int line,
int errno)
254 errstr = btrfs_decode_error(root->
fs_info, errno, nbuf);
256 "%s:%d: Aborting unused transaction(%s).\n",
257 function, line, errstr);
268 unsigned int line,
int errno,
const char *fmt, ...)
271 char *
s_id =
"<unknown>";
277 s_id = fs_info->
sb->s_id;
282 errstr = btrfs_decode_error(fs_info, errno, nbuf);
285 s_id,
function, line, &vaf, errstr);
288 s_id,
function, line, &vaf, errstr);
293 static void btrfs_put_super(
struct super_block *sb)
372 bool compress_force =
false;
374 cache_gen = btrfs_super_cache_generation(root->
fs_info->super_copy);
430 compress_force =
true;
436 compress_type =
"zlib";
441 }
else if (
strcmp(args[0].from,
"lzo") == 0) {
442 compress_type =
"lzo";
448 }
else if (
strncmp(args[0].from,
"no", 2) == 0) {
449 compress_type =
"no";
453 compress_force =
false;
459 if (compress_force) {
461 pr_info(
"btrfs: force %s compression\n",
464 pr_info(
"btrfs: use %s compression\n",
473 "allocation scheme\n");
515 "btrfs: allocations start at %llu\n",
574 #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
577 " including extent data\n");
579 CHECK_INTEGRITY_INCLUDING_EXTENT_DATA);
590 info->check_integrity_print_mask = intarg;
592 " check_integrity_print_mask 0x%x\n",
593 info->check_integrity_print_mask);
601 " not compiled in!\n");
606 if (
strcmp(args[0].from,
"panic") == 0)
608 PANIC_ON_FATAL_ERROR);
609 else if (
strcmp(args[0].from,
"bug") == 0)
611 PANIC_ON_FATAL_ERROR);
640 void *holder,
char **subvol_name,
u64 *subvol_objectid,
680 *subvol_objectid = intarg;
692 *subvol_rootid = intarg;
702 flags, holder, fs_devices);
734 if (subvol_objectid) {
735 location.objectid = subvol_objectid;
751 dir_id = btrfs_super_root_dir(fs_info->
super_copy);
774 if (IS_ERR(new_root))
775 return ERR_CAST(new_root);
777 if (btrfs_root_refs(&new_root->
root_item) == 0)
780 dir_id = btrfs_root_dirid(&new_root->
root_item);
788 return ERR_CAST(inode);
795 if (!
new && sb->
s_root->d_inode == inode) {
803 static int btrfs_fill_super(
struct super_block *sb,
805 void *
data,
int silent)
814 sb->
s_op = &btrfs_super_ops;
819 #ifdef CONFIG_BTRFS_FS_POSIX_ACL
823 err =
open_ctree(sb, fs_devices, (
char *)data);
825 printk(
"btrfs: open_ctree failed\n");
834 err = PTR_ERR(inode);
845 cleancache_init_fs(sb);
860 trace_btrfs_sync_fs(wait);
872 if (PTR_ERR(trans) == -
ENOENT)
874 return PTR_ERR(trans);
904 compress_type =
"zlib";
906 compress_type =
"lzo";
908 seq_printf(seq,
",compress-force=%s", compress_type);
910 seq_printf(seq,
",compress=%s", compress_type);
933 seq_puts(seq,
",user_subvol_rm_allowed");
943 seq_puts(seq,
",fatal_errors=panic");
947 static int btrfs_test_super(
struct super_block *
s,
void *data)
955 static int btrfs_set_super(
struct super_block *
s,
void *data)
966 static inline int is_subvolume_inode(
struct inode *inode)
978 static char *setup_root_args(
char *args)
991 src =
strstr(args,
"subvol=");
1010 strcpy(dst,
"subvolid=0");
1011 dst +=
strlen(
"subvolid=0");
1024 static struct dentry *mount_subvol(
const char *subvol_name,
int flags,
1025 const char *device_name,
char *data)
1027 struct dentry *root;
1031 newargs = setup_root_args(data);
1038 return ERR_CAST(mnt);
1042 if (!IS_ERR(root) && !is_subvolume_inode(root->
d_inode)) {
1060 static struct dentry *btrfs_mount(
struct file_system_type *fs_type,
int flags,
1061 const char *device_name,
void *data)
1065 struct dentry *root;
1069 char *subvol_name =
NULL;
1070 u64 subvol_objectid = 0;
1071 u64 subvol_rootid = 0;
1077 error = btrfs_parse_early_options(data, mode, fs_type,
1078 &subvol_name, &subvol_objectid,
1079 &subvol_rootid, &fs_devices);
1082 return ERR_PTR(error);
1086 root = mount_subvol(subvol_name, flags, device_name, data);
1093 return ERR_PTR(error);
1118 if (!(flags & MS_RDONLY) && fs_devices->
rw_devices == 0) {
1120 goto error_close_devices;
1124 s =
sget(fs_type, btrfs_test_super, btrfs_set_super, flags |
MS_NOSEC,
1128 goto error_close_devices;
1133 free_fs_info(fs_info);
1134 if ((flags ^ s->
s_flags) & MS_RDONLY)
1140 btrfs_sb(s)->bdev_holder = fs_type;
1141 error = btrfs_fill_super(s, fs_devices, data,
1145 root = !error ? get_default_root(s, subvol_objectid) : ERR_PTR(error);
1151 error_close_devices:
1154 free_fs_info(fs_info);
1155 return ERR_PTR(error);
1160 spin_lock_irq(&workers->
lock);
1162 spin_unlock_irq(&workers->
lock);
1165 static void btrfs_resize_thread_pool(
struct btrfs_fs_info *fs_info,
1166 int new_pool_size,
int old_pool_size)
1168 if (new_pool_size == old_pool_size)
1174 old_pool_size, new_pool_size);
1177 btrfs_set_max_workers(&fs_info->
workers, new_pool_size);
1181 btrfs_set_max_workers(&fs_info->
fixup_workers, new_pool_size);
1182 btrfs_set_max_workers(&fs_info->
endio_workers, new_pool_size);
1189 btrfs_set_max_workers(&fs_info->
scrub_workers, new_pool_size);
1192 static int btrfs_remount(
struct super_block *sb,
int *flags,
char *data)
1196 unsigned old_flags = sb->
s_flags;
1197 unsigned long old_opts = fs_info->
mount_opt;
1211 btrfs_resize_thread_pool(fs_info,
1214 if ((*flags & MS_RDONLY) == (sb->
s_flags & MS_RDONLY))
1217 if (*flags & MS_RDONLY) {
1229 if (btrfs_super_log_root(fs_info->
super_copy) != 0) {
1261 btrfs_resize_thread_pool(fs_info,
1268 static int btrfs_cmp_device_free_bytes(
const void *dev_info1,
1269 const void *dev_info2)
1285 static inline void btrfs_descending_sort_devices(
1290 btrfs_cmp_device_free_bytes,
NULL);
1297 static int btrfs_calc_avail_data_space(
struct btrfs_root *root,
u64 *free_bytes)
1307 u64 min_stripe_size;
1309 int i = 0, nr_devices;
1312 nr_devices = fs_info->
fs_devices->open_devices;
1315 devices_info =
kmalloc(
sizeof(*devices_info) * nr_devices,
1353 skip_space = 1024 * 1024;
1368 kfree(devices_info);
1373 skip_space -= used_space;
1379 if (avail_space && avail_space >= skip_space)
1380 avail_space -= skip_space;
1384 if (avail_space < min_stripe_size)
1387 devices_info[
i].
dev = device;
1395 btrfs_descending_sort_devices(devices_info, nr_devices);
1399 while (nr_devices >= min_stripes) {
1403 if (devices_info[i].max_avail >= min_stripe_size) {
1410 devices_info[j].max_avail -= alloc_size;
1416 kfree(devices_info);
1417 *free_bytes = avail_space;
1421 static int btrfs_statfs(
struct dentry *dentry,
struct kstatfs *buf)
1428 u64 total_free_data = 0;
1429 int bits = dentry->
d_sb->s_blocksize_bits;
1436 list_for_each_entry_rcu(found, head,
list) {
1448 buf->
f_blocks = btrfs_super_total_bytes(disk_super) >>
bits;
1453 ret = btrfs_calc_avail_data_space(fs_info->
tree_root, &total_free_data);
1474 static void btrfs_kill_super(
struct super_block *sb)
1478 free_fs_info(fs_info);
1484 .mount = btrfs_mount,
1485 .kill_sb = btrfs_kill_super,
1492 static long btrfs_control_ioctl(
struct file *
file,
unsigned int cmd,
1502 vol =
memdup_user((
void __user *)arg,
sizeof(*vol));
1504 return PTR_ERR(vol);
1509 &btrfs_fs_type, &fs_devices);
1513 &btrfs_fs_type, &fs_devices);
1527 struct btrfs_root *root = btrfs_sb(sb)->tree_root;
1530 if (IS_ERR(trans)) {
1532 if (PTR_ERR(trans) == -
ENOENT)
1534 return PTR_ERR(trans);
1544 static int btrfs_show_devname(
struct seq_file *
m,
struct dentry *root)
1554 while (cur_devices) {
1559 if (!first_dev || dev->
devid < first_dev->
devid)
1562 cur_devices = cur_devices->
seed;
1580 .put_super = btrfs_put_super,
1582 .show_options = btrfs_show_options,
1583 .show_devname = btrfs_show_devname,
1587 .statfs = btrfs_statfs,
1588 .remount_fs = btrfs_remount,
1589 .freeze_fs = btrfs_freeze,
1590 .unfreeze_fs = btrfs_unfreeze,
1594 .unlocked_ioctl = btrfs_control_ioctl,
1595 .compat_ioctl = btrfs_control_ioctl,
1602 .name =
"btrfs-control",
1603 .fops = &btrfs_ctl_fops
1609 static int btrfs_interface_init(
void)
1614 static void btrfs_interface_exit(
void)
1617 printk(
KERN_INFO "btrfs: misc_deregister failed for control device\n");
1620 static int __init init_btrfs_fs(
void)
1640 goto free_extent_io;
1648 goto free_ordered_data;
1650 err = btrfs_interface_init();
1652 goto free_delayed_inode;
1656 goto unregister_ioctl;
1658 btrfs_init_lockdep();
1664 btrfs_interface_exit();
1681 static void __exit exit_btrfs_fs(
void)
1688 btrfs_interface_exit();