2009-04-03 17:47:43 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2009 Oracle. All rights reserved.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public
|
|
|
|
* License v2 as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 021110-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __BTRFS_FREE_SPACE_CACHE
|
|
|
|
#define __BTRFS_FREE_SPACE_CACHE
|
|
|
|
|
Btrfs: use hybrid extents+bitmap rb tree for free space
Currently btrfs has a problem where it can use a ridiculous amount of RAM simply
tracking free space. As free space gets fragmented, we end up with thousands of
entries on an rb-tree per block group, which usually spans 1 gig of area. Since
we currently don't ever flush free space cache back to disk this gets to be a
bit unweildly on large fs's with lots of fragmentation.
This patch solves this problem by using PAGE_SIZE bitmaps for parts of the free
space cache. Initially we calculate a threshold of extent entries we can
handle, which is however many extent entries we can cram into 16k of ram. The
maximum amount of RAM that should ever be used to track 1 gigabyte of diskspace
will be 32k of RAM, which scales much better than we did before.
Once we pass the extent threshold, we start adding bitmaps and using those
instead for tracking the free space. This patch also makes it so that any free
space thats less than 4 * sectorsize we go ahead and put into a bitmap. This is
nice since we try and allocate out of the front of a block group, so if the
front of a block group is heavily fragmented and then has a huge chunk of free
space at the end, we go ahead and add the fragmented areas to bitmaps and use a
normal extent entry to track the big chunk at the back of the block group.
I've also taken the opportunity to revamp how we search for free space.
Previously we indexed free space via an offset indexed rb tree and a bytes
indexed rb tree. I've dropped the bytes indexed rb tree and use only the offset
indexed rb tree. This cuts the number of tree operations we were doing
previously down by half, and gives us a little bit of a better allocation
pattern since we will always start from a specific offset and search forward
from there, instead of searching for the size we need and try and get it as
close as possible to the offset we want.
I've given this a healthy amount of testing pre-new format stuff, as well as
post-new format stuff. I've booted up my fedora box which is installed on btrfs
with this patch and ran with it for a few days without issues. I've not seen
any performance regressions in any of my tests.
Since the last patch Yan Zheng fixed a problem where we could have overlapping
entries, so updating their offset inline would cause problems. Thanks,
Signed-off-by: Josef Bacik <jbacik@redhat.com>
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2009-07-14 05:29:25 +04:00
|
|
|
struct btrfs_free_space {
|
|
|
|
struct rb_node offset_index;
|
|
|
|
u64 offset;
|
|
|
|
u64 bytes;
|
2015-10-02 23:09:42 +03:00
|
|
|
u64 max_extent_size;
|
Btrfs: use hybrid extents+bitmap rb tree for free space
Currently btrfs has a problem where it can use a ridiculous amount of RAM simply
tracking free space. As free space gets fragmented, we end up with thousands of
entries on an rb-tree per block group, which usually spans 1 gig of area. Since
we currently don't ever flush free space cache back to disk this gets to be a
bit unweildly on large fs's with lots of fragmentation.
This patch solves this problem by using PAGE_SIZE bitmaps for parts of the free
space cache. Initially we calculate a threshold of extent entries we can
handle, which is however many extent entries we can cram into 16k of ram. The
maximum amount of RAM that should ever be used to track 1 gigabyte of diskspace
will be 32k of RAM, which scales much better than we did before.
Once we pass the extent threshold, we start adding bitmaps and using those
instead for tracking the free space. This patch also makes it so that any free
space thats less than 4 * sectorsize we go ahead and put into a bitmap. This is
nice since we try and allocate out of the front of a block group, so if the
front of a block group is heavily fragmented and then has a huge chunk of free
space at the end, we go ahead and add the fragmented areas to bitmaps and use a
normal extent entry to track the big chunk at the back of the block group.
I've also taken the opportunity to revamp how we search for free space.
Previously we indexed free space via an offset indexed rb tree and a bytes
indexed rb tree. I've dropped the bytes indexed rb tree and use only the offset
indexed rb tree. This cuts the number of tree operations we were doing
previously down by half, and gives us a little bit of a better allocation
pattern since we will always start from a specific offset and search forward
from there, instead of searching for the size we need and try and get it as
close as possible to the offset we want.
I've given this a healthy amount of testing pre-new format stuff, as well as
post-new format stuff. I've booted up my fedora box which is installed on btrfs
with this patch and ran with it for a few days without issues. I've not seen
any performance regressions in any of my tests.
Since the last patch Yan Zheng fixed a problem where we could have overlapping
entries, so updating their offset inline would cause problems. Thanks,
Signed-off-by: Josef Bacik <jbacik@redhat.com>
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2009-07-14 05:29:25 +04:00
|
|
|
unsigned long *bitmap;
|
|
|
|
struct list_head list;
|
|
|
|
};
|
|
|
|
|
2011-03-29 09:46:06 +04:00
|
|
|
struct btrfs_free_space_ctl {
|
|
|
|
spinlock_t tree_lock;
|
|
|
|
struct rb_root free_space_offset;
|
|
|
|
u64 free_space;
|
|
|
|
int extents_thresh;
|
|
|
|
int free_extents;
|
|
|
|
int total_bitmaps;
|
|
|
|
int unit;
|
|
|
|
u64 start;
|
2015-11-19 13:42:28 +03:00
|
|
|
const struct btrfs_free_space_op *op;
|
2011-03-29 09:46:06 +04:00
|
|
|
void *private;
|
Btrfs: fix race between writing free space cache and trimming
Trimming is completely transactionless, and the way it operates consists
of hiding free space entries from a block group, perform the trim/discard
and then make the free space entries visible again.
Therefore while a free space entry is being trimmed, we can have free space
cache writing running in parallel (as part of a transaction commit) which
will miss the free space entry. This means that an unmount (or crash/reboot)
after that transaction commit and mount again before another transaction
starts/commits after the discard finishes, we will have some free space
that won't be used again unless the free space cache is rebuilt. After the
unmount, fsck (btrfsck, btrfs check) reports the issue like the following
example:
*** fsck.btrfs output ***
checking extents
checking free space cache
There is no free space entry for 521764864-521781248
There is no free space entry for 521764864-1103101952
cache appears valid but isnt 29360128
Checking filesystem on /dev/sdc
UUID: b4789e27-4774-4626-98e9-ae8dfbfb0fb5
found 1235681286 bytes used err is -22
(...)
Another issue caused by this race is a crash while writing bitmap entries
to the cache, because while the cache writeout task accesses the bitmaps,
the trim task can be concurrently modifying the bitmap or worse might
be freeing the bitmap. The later case results in the following crash:
[55650.804460] general protection fault: 0000 [#1] SMP DEBUG_PAGEALLOC
[55650.804835] Modules linked in: btrfs dm_flakey dm_mod crc32c_generic xor raid6_pq nfsd auth_rpcgss oid_registry nfs_acl nfs lockd fscache sunrpc loop parport_pc parport i2c_piix4 psmouse evdev pcspkr microcode processor i2ccore serio_raw thermal_sys button ext4 crc16 jbd2 mbcache sg sd_mod crc_t10dif sr_mod cdrom crct10dif_generic crct10dif_common ata_generic virtio_scsi floppy ata_piix libata virtio_pci virtio_ring virtio scsi_mod e1000 [last unloaded: btrfs]
[55650.806169] CPU: 1 PID: 31002 Comm: btrfs-transacti Tainted: G W 3.17.0-rc5-btrfs-next-1+ #1
[55650.806493] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.7.5-0-ge51488c-20140602_164612-nilsson.home.kraxel.org 04/01/2014
[55650.806867] task: ffff8800b12f6410 ti: ffff880071538000 task.ti: ffff880071538000
[55650.807166] RIP: 0010:[<ffffffffa037cf45>] [<ffffffffa037cf45>] write_bitmap_entries+0x65/0xbb [btrfs]
[55650.807514] RSP: 0018:ffff88007153bc30 EFLAGS: 00010246
[55650.807687] RAX: 000000005d1ec000 RBX: ffff8800a665df08 RCX: 0000000000000400
[55650.807885] RDX: ffff88005d1ec000 RSI: 6b6b6b6b6b6b6b6b RDI: ffff88005d1ec000
[55650.808017] RBP: ffff88007153bc58 R08: 00000000ddd51536 R09: 00000000000001e0
[55650.808017] R10: 0000000000000000 R11: 0000000000000037 R12: 6b6b6b6b6b6b6b6b
[55650.808017] R13: ffff88007153bca8 R14: 6b6b6b6b6b6b6b6b R15: ffff88007153bc98
[55650.808017] FS: 0000000000000000(0000) GS:ffff88023ec80000(0000) knlGS:0000000000000000
[55650.808017] CS: 0010 DS: 0000 ES: 0000 CR0: 000000008005003b
[55650.808017] CR2: 0000000002273b88 CR3: 00000000b18f6000 CR4: 00000000000006e0
[55650.808017] Stack:
[55650.808017] ffff88020e834e00 ffff880172d68db0 0000000000000000 ffff88019257c800
[55650.808017] ffff8801d42ea720 ffff88007153bd10 ffffffffa037d2fa ffff880224e99180
[55650.808017] ffff8801469a6188 ffff880224e99140 ffff880172d68c50 00000003000000b7
[55650.808017] Call Trace:
[55650.808017] [<ffffffffa037d2fa>] __btrfs_write_out_cache+0x1ea/0x37f [btrfs]
[55650.808017] [<ffffffffa037d959>] btrfs_write_out_cache+0xa1/0xd8 [btrfs]
[55650.808017] [<ffffffffa033936b>] btrfs_write_dirty_block_groups+0x4b5/0x505 [btrfs]
[55650.808017] [<ffffffffa03aa98e>] commit_cowonly_roots+0x15e/0x1f7 [btrfs]
[55650.808017] [<ffffffff813eb9c7>] ? _raw_spin_lock+0xe/0x10
[55650.808017] [<ffffffffa0346e46>] btrfs_commit_transaction+0x411/0x882 [btrfs]
[55650.808017] [<ffffffffa03432a4>] transaction_kthread+0xf2/0x1a4 [btrfs]
[55650.808017] [<ffffffffa03431b2>] ? btrfs_cleanup_transaction+0x3d8/0x3d8 [btrfs]
[55650.808017] [<ffffffff8105966b>] kthread+0xb7/0xbf
[55650.808017] [<ffffffff810595b4>] ? __kthread_parkme+0x67/0x67
[55650.808017] [<ffffffff813ebeac>] ret_from_fork+0x7c/0xb0
[55650.808017] [<ffffffff810595b4>] ? __kthread_parkme+0x67/0x67
[55650.808017] Code: 4c 89 ef 8d 70 ff e8 d4 fc ff ff 41 8b 45 34 41 39 45 30 7d 5c 31 f6 4c 89 ef e8 80 f6 ff ff 49 8b 7d 00 4c 89 f6 b9 00 04 00 00 <f3> a5 4c 89 ef 41 8b 45 30 8d 70 ff e8 a3 fc ff ff 41 8b 45 34
[55650.808017] RIP [<ffffffffa037cf45>] write_bitmap_entries+0x65/0xbb [btrfs]
[55650.808017] RSP <ffff88007153bc30>
[55650.815725] ---[ end trace 1c032e96b149ff86 ]---
Fix this by serializing both tasks in such a way that cache writeout
doesn't wait for the trim/discard of free space entries to finish and
doesn't miss any free space entry.
Signed-off-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: Chris Mason <clm@fb.com>
2014-12-01 20:04:09 +03:00
|
|
|
struct mutex cache_writeout_mutex;
|
|
|
|
struct list_head trimming_ranges;
|
2011-03-29 09:46:06 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
struct btrfs_free_space_op {
|
|
|
|
void (*recalc_thresholds)(struct btrfs_free_space_ctl *ctl);
|
|
|
|
bool (*use_bitmap)(struct btrfs_free_space_ctl *ctl,
|
|
|
|
struct btrfs_free_space *info);
|
|
|
|
};
|
|
|
|
|
2015-04-05 03:14:42 +03:00
|
|
|
struct btrfs_io_ctl;
|
|
|
|
|
2010-06-21 22:48:16 +04:00
|
|
|
struct inode *lookup_free_space_inode(struct btrfs_root *root,
|
|
|
|
struct btrfs_block_group_cache
|
|
|
|
*block_group, struct btrfs_path *path);
|
|
|
|
int create_free_space_inode(struct btrfs_root *root,
|
|
|
|
struct btrfs_trans_handle *trans,
|
|
|
|
struct btrfs_block_group_cache *block_group,
|
|
|
|
struct btrfs_path *path);
|
2010-07-02 20:14:14 +04:00
|
|
|
|
2013-05-13 17:55:09 +04:00
|
|
|
int btrfs_check_trunc_cache_free_space(struct btrfs_root *root,
|
|
|
|
struct btrfs_block_rsv *rsv);
|
2010-06-21 22:48:16 +04:00
|
|
|
int btrfs_truncate_free_space_cache(struct btrfs_root *root,
|
|
|
|
struct btrfs_trans_handle *trans,
|
2015-04-06 22:46:08 +03:00
|
|
|
struct btrfs_block_group_cache *block_group,
|
2010-06-21 22:48:16 +04:00
|
|
|
struct inode *inode);
|
2010-08-26 00:54:15 +04:00
|
|
|
int load_free_space_cache(struct btrfs_fs_info *fs_info,
|
|
|
|
struct btrfs_block_group_cache *block_group);
|
2015-04-05 03:14:42 +03:00
|
|
|
int btrfs_wait_cache_io(struct btrfs_root *root,
|
|
|
|
struct btrfs_trans_handle *trans,
|
|
|
|
struct btrfs_block_group_cache *block_group,
|
|
|
|
struct btrfs_io_ctl *io_ctl,
|
|
|
|
struct btrfs_path *path, u64 offset);
|
2010-07-02 20:14:14 +04:00
|
|
|
int btrfs_write_out_cache(struct btrfs_root *root,
|
|
|
|
struct btrfs_trans_handle *trans,
|
|
|
|
struct btrfs_block_group_cache *block_group,
|
|
|
|
struct btrfs_path *path);
|
2011-04-20 06:33:24 +04:00
|
|
|
struct inode *lookup_free_ino_inode(struct btrfs_root *root,
|
|
|
|
struct btrfs_path *path);
|
|
|
|
int create_free_ino_inode(struct btrfs_root *root,
|
|
|
|
struct btrfs_trans_handle *trans,
|
|
|
|
struct btrfs_path *path);
|
|
|
|
int load_free_ino_cache(struct btrfs_fs_info *fs_info,
|
|
|
|
struct btrfs_root *root);
|
|
|
|
int btrfs_write_out_ino_cache(struct btrfs_root *root,
|
|
|
|
struct btrfs_trans_handle *trans,
|
2013-09-20 17:43:28 +04:00
|
|
|
struct btrfs_path *path,
|
|
|
|
struct inode *inode);
|
2011-04-20 06:33:24 +04:00
|
|
|
|
2011-03-29 09:46:06 +04:00
|
|
|
void btrfs_init_free_space_ctl(struct btrfs_block_group_cache *block_group);
|
Btrfs: Cache free inode numbers in memory
Currently btrfs stores the highest objectid of the fs tree, and it always
returns (highest+1) inode number when we create a file, so inode numbers
won't be reclaimed when we delete files, so we'll run out of inode numbers
as we keep create/delete files in 32bits machines.
This fixes it, and it works similarly to how we cache free space in block
cgroups.
We start a kernel thread to read the file tree. By scanning inode items,
we know which chunks of inode numbers are free, and we cache them in
an rb-tree.
Because we are searching the commit root, we have to carefully handle the
cross-transaction case.
The rb-tree is a hybrid extent+bitmap tree, so if we have too many small
chunks of inode numbers, we'll use bitmaps. Initially we allow 16K ram
of extents, and a bitmap will be used if we exceed this threshold. The
extents threshold is adjusted in runtime.
Signed-off-by: Li Zefan <lizf@cn.fujitsu.com>
2011-04-20 06:06:11 +04:00
|
|
|
int __btrfs_add_free_space(struct btrfs_free_space_ctl *ctl,
|
|
|
|
u64 bytenr, u64 size);
|
|
|
|
static inline int
|
|
|
|
btrfs_add_free_space(struct btrfs_block_group_cache *block_group,
|
|
|
|
u64 bytenr, u64 size)
|
|
|
|
{
|
|
|
|
return __btrfs_add_free_space(block_group->free_space_ctl,
|
|
|
|
bytenr, size);
|
|
|
|
}
|
2009-04-03 17:47:43 +04:00
|
|
|
int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
|
|
|
|
u64 bytenr, u64 size);
|
Btrfs: Cache free inode numbers in memory
Currently btrfs stores the highest objectid of the fs tree, and it always
returns (highest+1) inode number when we create a file, so inode numbers
won't be reclaimed when we delete files, so we'll run out of inode numbers
as we keep create/delete files in 32bits machines.
This fixes it, and it works similarly to how we cache free space in block
cgroups.
We start a kernel thread to read the file tree. By scanning inode items,
we know which chunks of inode numbers are free, and we cache them in
an rb-tree.
Because we are searching the commit root, we have to carefully handle the
cross-transaction case.
The rb-tree is a hybrid extent+bitmap tree, so if we have too many small
chunks of inode numbers, we'll use bitmaps. Initially we allow 16K ram
of extents, and a bitmap will be used if we exceed this threshold. The
extents threshold is adjusted in runtime.
Signed-off-by: Li Zefan <lizf@cn.fujitsu.com>
2011-04-20 06:06:11 +04:00
|
|
|
void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl);
|
2009-04-03 17:47:43 +04:00
|
|
|
void btrfs_remove_free_space_cache(struct btrfs_block_group_cache
|
Btrfs: Cache free inode numbers in memory
Currently btrfs stores the highest objectid of the fs tree, and it always
returns (highest+1) inode number when we create a file, so inode numbers
won't be reclaimed when we delete files, so we'll run out of inode numbers
as we keep create/delete files in 32bits machines.
This fixes it, and it works similarly to how we cache free space in block
cgroups.
We start a kernel thread to read the file tree. By scanning inode items,
we know which chunks of inode numbers are free, and we cache them in
an rb-tree.
Because we are searching the commit root, we have to carefully handle the
cross-transaction case.
The rb-tree is a hybrid extent+bitmap tree, so if we have too many small
chunks of inode numbers, we'll use bitmaps. Initially we allow 16K ram
of extents, and a bitmap will be used if we exceed this threshold. The
extents threshold is adjusted in runtime.
Signed-off-by: Li Zefan <lizf@cn.fujitsu.com>
2011-04-20 06:06:11 +04:00
|
|
|
*block_group);
|
2009-04-03 17:47:43 +04:00
|
|
|
u64 btrfs_find_space_for_alloc(struct btrfs_block_group_cache *block_group,
|
2013-09-09 09:19:42 +04:00
|
|
|
u64 offset, u64 bytes, u64 empty_size,
|
|
|
|
u64 *max_extent_size);
|
Btrfs: Cache free inode numbers in memory
Currently btrfs stores the highest objectid of the fs tree, and it always
returns (highest+1) inode number when we create a file, so inode numbers
won't be reclaimed when we delete files, so we'll run out of inode numbers
as we keep create/delete files in 32bits machines.
This fixes it, and it works similarly to how we cache free space in block
cgroups.
We start a kernel thread to read the file tree. By scanning inode items,
we know which chunks of inode numbers are free, and we cache them in
an rb-tree.
Because we are searching the commit root, we have to carefully handle the
cross-transaction case.
The rb-tree is a hybrid extent+bitmap tree, so if we have too many small
chunks of inode numbers, we'll use bitmaps. Initially we allow 16K ram
of extents, and a bitmap will be used if we exceed this threshold. The
extents threshold is adjusted in runtime.
Signed-off-by: Li Zefan <lizf@cn.fujitsu.com>
2011-04-20 06:06:11 +04:00
|
|
|
u64 btrfs_find_ino_for_alloc(struct btrfs_root *fs_root);
|
2009-04-03 17:47:43 +04:00
|
|
|
void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
|
|
|
|
u64 bytes);
|
2013-08-14 22:02:47 +04:00
|
|
|
int btrfs_find_space_cluster(struct btrfs_root *root,
|
2009-04-03 17:47:43 +04:00
|
|
|
struct btrfs_block_group_cache *block_group,
|
|
|
|
struct btrfs_free_cluster *cluster,
|
|
|
|
u64 offset, u64 bytes, u64 empty_size);
|
|
|
|
void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster);
|
|
|
|
u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,
|
|
|
|
struct btrfs_free_cluster *cluster, u64 bytes,
|
2013-09-09 09:19:42 +04:00
|
|
|
u64 min_start, u64 *max_extent_size);
|
2009-04-03 17:47:43 +04:00
|
|
|
int btrfs_return_cluster_to_free_space(
|
|
|
|
struct btrfs_block_group_cache *block_group,
|
|
|
|
struct btrfs_free_cluster *cluster);
|
2011-03-24 13:24:28 +03:00
|
|
|
int btrfs_trim_block_group(struct btrfs_block_group_cache *block_group,
|
|
|
|
u64 *trimmed, u64 start, u64 end, u64 minlen);
|
2013-03-15 17:47:08 +04:00
|
|
|
|
2013-08-14 23:05:12 +04:00
|
|
|
/* Support functions for runnint our sanity tests */
|
|
|
|
#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
|
|
|
|
int test_add_free_space_entry(struct btrfs_block_group_cache *cache,
|
|
|
|
u64 offset, u64 bytes, bool bitmap);
|
|
|
|
int test_check_exists(struct btrfs_block_group_cache *cache,
|
|
|
|
u64 offset, u64 bytes);
|
|
|
|
#endif
|
2013-03-15 17:47:08 +04:00
|
|
|
|
2009-04-03 17:47:43 +04:00
|
|
|
#endif
|