Skip to contentSkip to conversation

Btrfs: fix ENOSPC accounting when max_extent is not maxed out V2

PATCH In review Josef Bacik 2 participants 4 messages Mar 18, 2010 – Mar 20, 2010 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Btrfs: fix ENOSPC accounting when max_extent is not maxed out V2

A user reported a bug a few weeks back where if he set max_extent=1m and then did a dd and then stopped it, we would panic. This is because I miscalculated how many extents would be needed for splits/merges. Turns out I didn't actually take max_extent into account properly, since we only ever add 1 extent for a write, which isn't quite right for the case that say max_extent is 4k and we do 8k writes. That would result in more than 1 extent. So this patch makes us properly figure out how many extents are needed for the amount of data that is being written, and deals with splitting and merging better. I've tested this ad nauseum and it works well. This version doesn't depend on my per-cpu stuff. Thanks,

---
 fs/btrfs/ctree.h        |    8 ++++
 fs/btrfs/extent-tree.c  |    4 +-
 fs/btrfs/file.c         |    5 +-
 fs/btrfs/inode.c        |   95 +++++++++++++++++++++++++++++++---------------
 fs/btrfs/ordered-data.c |    6 ++-
 5 files changed, 81 insertions(+), 37 deletions(-)
fs/btrfs/ctree.h +8 -0
index 1111584..7d478b4 100644
@@ -1960,6 +1960,14 @@ static inline struct dentry *fdentry(struct file *file)
 	return file->f_path.dentry;
 }
 
+static inline int calculate_extents(struct btrfs_root *root, u64 bytes)
+{
+	if (bytes <= root->fs_info->max_extent)
+		return 1;
+	return (int)div64_u64(bytes + root->fs_info->max_extent -1,
+			      root->fs_info->max_extent);
+}
+
 /* extent-tree.c */
 void btrfs_put_block_group(struct btrfs_block_group_cache *cache);
 int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
fs/btrfs/extent-tree.c +2 -2
index 826120f..7bb97de 100644
@@ -2848,7 +2848,7 @@ int btrfs_unreserve_metadata_for_delalloc(struct btrfs_root *root,
 	}
 	spin_unlock(&BTRFS_I(inode)->accounting_lock);
 
-	BTRFS_I(inode)->reserved_extents--;
+	BTRFS_I(inode)->reserved_extents -= num_items;
 	BUG_ON(BTRFS_I(inode)->reserved_extents < 0);
 
 	if (meta_sinfo->bytes_delalloc < num_bytes) {
@@ -3110,7 +3110,7 @@ again:
 		return -ENOSPC;
 	}
 
-	BTRFS_I(inode)->reserved_extents++;
+	BTRFS_I(inode)->reserved_extents += num_items;
 	check_force_delalloc(meta_sinfo);
 	spin_unlock(&meta_sinfo->lock);
 
fs/btrfs/file.c +3 -2
index d146dde..a457a94 100644
@@ -838,6 +838,7 @@ static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
 	unsigned long first_index;
 	unsigned long last_index;
 	int will_write;
+	int reserved = calculate_extents(root, count);
 
 	will_write = ((file->f_flags & O_SYNC) || IS_SYNC(inode) ||
 		      (file->f_flags & O_DIRECT));
@@ -855,7 +856,7 @@ static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
 	/* do the reserve before the mutex lock in case we have to do some
 	 * flushing.  We wouldn't deadlock, but this is more polite.
 	 */
-	err = btrfs_reserve_metadata_for_delalloc(root, inode, 1);
+	err = btrfs_reserve_metadata_for_delalloc(root, inode, reserved);
 	if (err)
 		goto out_nolock;
 
@@ -975,7 +976,7 @@ out:
 	mutex_unlock(&inode->i_mutex);
 	if (ret)
 		err = ret;
-	btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
+	btrfs_unreserve_metadata_for_delalloc(root, inode, reserved);
 
 out_nolock:
 	kfree(pages);
fs/btrfs/inode.c +64 -31

178 lines not on this page · raw message

fs/btrfs/ordered-data.c +4 -2
index a8ffecd..5c99882 100644
@@ -303,6 +303,7 @@ static int __btrfs_remove_ordered_extent(struct inode *inode,
 				struct btrfs_ordered_extent *entry)
 {
 	struct btrfs_ordered_inode_tree *tree;
+	struct btrfs_root *root = BTRFS_I(inode)->root;
 	struct rb_node *node;
 
 	tree = &BTRFS_I(inode)->ordered_tree;
@@ -312,12 +313,13 @@ static int __btrfs_remove_ordered_extent(struct inode *inode,
 	set_bit(BTRFS_ORDERED_COMPLETE, &entry->flags);
 
 	spin_lock(&BTRFS_I(inode)->accounting_lock);
+	WARN_ON(!BTRFS_I(inode)->outstanding_extents);
 	BTRFS_I(inode)->outstanding_extents--;
 	spin_unlock(&BTRFS_I(inode)->accounting_lock);
 	btrfs_unreserve_metadata_for_delalloc(BTRFS_I(inode)->root,
 					      inode, 1);
 
-	spin_lock(&BTRFS_I(inode)->root->fs_info->ordered_extent_lock);
+	spin_lock(&root->fs_info->ordered_extent_lock);
 	list_del_init(&entry->root_extent_list);
 
 	/*
@@ -329,7 +331,7 @@ static int __btrfs_remove_ordered_extent(struct inode *inode,
 	    !mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY)) {
 		list_del_init(&BTRFS_I(inode)->ordered_operations);
 	}
-	spin_unlock(&BTRFS_I(inode)->root->fs_info->ordered_extent_lock);
+	spin_unlock(&root->fs_info->ordered_extent_lock);
 
 	return 0;
 }
Signature · 1 line
1.6.6
#1 lore raw
Quoted 1 line · Josef Bacik

A user reported a bug a few weeks back where if he set max_extent=3D1=

m and then

Quoted 1 line

did a dd and then stopped it, we would panic. =A0This is because I mi=

scalculated

Quoted 1 line

how many extents would be needed for splits/merges. =A0Turns out I di=

dn't actually

Quoted 1 line

take max_extent into account properly, since we only ever add 1 exten=

t for a

Quoted 1 line

write, which isn't quite right for the case that say max_extent is 4k=

and we do

Quoted 1 line

8k writes. =A0That would result in more than 1 extent. =A0So this pat=

ch makes us

Quoted 1 line

properly figure out how many extents are needed for the amount of dat=

a that is

Quoted 1 line

being written, and deals with splitting and merging better. =A0I've t=

ested this ad

Quoted 1 line

nauseum and it works well. =A0This version doesn't depend on my per-c=

pu stuff.

Quoted 1 line

Thanks,

Why not remove the the max_extent check. The max length of file extent is also affected by fragmentation level of free space. It doesn't make = sense
to introduce complex code to address one factor while lose sight of ano= ther
factor. I think reserving one unit of metadata for each delalloc extent=
in the
extent IO tree should be OK. because even a delalloc extent ends up wit= h
multiple file extents, these file extents are adjacency in the b-tree.

Regards
Yan, Zheng

Quoted 7 lines

---
=A0fs/btrfs/ctree.h =A0 =A0 =A0 =A0| =A0 =A08 ++++ =A0fs/btrfs/extent-tree.c =A0| =A0 =A04 +- =A0fs/btrfs/file.c =A0 =A0 =A0 =A0 | =A0 =A05 +- =A0fs/btrfs/inode.c =A0 =A0 =A0 =A0| =A0 95 +++++++++++++++++++++++++=

++++++---------------

fs/btrfs/ctree.h static inline struct dentry *fdentry(struct fi=

Quoted 8 lines

=A0fs/btrfs/ordered-data.c | =A0 =A06 ++- =A05 files changed, 81 insertions(+), 37 deletions(-)

fs/btrfs/ctree.h +0 -0
index 1111584..7d478b4 100644
@@ -1960,6 +1960,14 @@ static inline struct dentry *fdentry(struct fi=

le *file)

fs/btrfs/ctree.h static inline struct dentry *fdentry(struct fi= · line 1963

Quoted 4 lines
fs/btrfs/ctree.h +1 -0
 =A0 =A0 =A0 =A0return file->f_path.dentry;
 =A0}
 
+static inline int calculate_extents(struct btrfs_root *root, u64 byt=

es)

fs/btrfs/ctree.h static inline struct dentry *fdentry(struct fi= · line 1967

Quoted 4 lines
fs/btrfs/ctree.h +4 -0
+{
+ =A0 =A0 =A0 if (bytes <=3D root->fs_info->max_extent)
+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 return 1;
+ =A0 =A0 =A0 return (int)div64_u64(bytes + root->fs_info->max_extent=

-1,

fs/btrfs/ctree.h static inline struct dentry *fdentry(struct fi= · line 1968

Quoted 1 line
fs/btrfs/ctree.h +1 -0
+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 root->fs_in=

fo->max_extent);

fs/btrfs/extent-tree.c int btrfs_unreserve_metadata_for_delalloc(struc=

Quoted 10 lines
fs/btrfs/ctree.h +2 -0
+}
+

=A0/* extent-tree.c */
=A0void btrfs_put_block_group(struct btrfs_block_group_cache *cache); =A0int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,

fs/btrfs/extent-tree.c +0 -0
index 826120f..7bb97de 100644
@@ -2848,7 +2848,7 @@ int btrfs_unreserve_metadata_for_delalloc(struc=

t btrfs_root *root,

fs/btrfs/file.c static ssize_t btrfs_file_write(struct file *file=

Quoted 22 lines
fs/btrfs/extent-tree.c +2 -2
 =A0 =A0 =A0 =A0}
 =A0 =A0 =A0 =A0spin_unlock(&BTRFS_I(inode)->accounting_lock);
 
- =A0 =A0 =A0 BTRFS_I(inode)->reserved_extents--;
+ =A0 =A0 =A0 BTRFS_I(inode)->reserved_extents -=3D num_items;
 =A0 =A0 =A0 =A0BUG_ON(BTRFS_I(inode)->reserved_extents < 0);
 
 =A0 =A0 =A0 =A0if (meta_sinfo->bytes_delalloc < num_bytes) {
@@ -3110,7 +3110,7 @@ again:
 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0return -ENOSPC;
 =A0 =A0 =A0 =A0}
 
- =A0 =A0 =A0 BTRFS_I(inode)->reserved_extents++;
+ =A0 =A0 =A0 BTRFS_I(inode)->reserved_extents +=3D num_items;
 =A0 =A0 =A0 =A0check_force_delalloc(meta_sinfo);
 =A0 =A0 =A0 =A0spin_unlock(&meta_sinfo->lock);
 
fs/btrfs/file.c +0 -0
index d146dde..a457a94 100644
@@ -838,6 +838,7 @@ static ssize_t btrfs_file_write(struct file *file=

, const char __user *buf,

Quoted 6 lines
fs/btrfs/file.c +1 -0
 =A0 =A0 =A0 =A0unsigned long first_index;
 =A0 =A0 =A0 =A0unsigned long last_index;
 =A0 =A0 =A0 =A0int will_write;
+ =A0 =A0 =A0 int reserved =3D calculate_extents(root, count);
 

=A0 =A0 =A0 =A0will_write =3D ((file->f_flags & O_SYNC) || IS_SYNC(in=

ode) ||

Quoted 1 line

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0(file->f_flags & O_DIRECT)=

);

fs/btrfs/file.c static ssize_t btrfs_file_write(struct file *file=

Quoted 1 line
fs/btrfs/file.c +0 -0
@@ -855,7 +856,7 @@ static ssize_t btrfs_file_write(struct file *file=

, const char __user *buf,

Quoted 1 line

=A0 =A0 =A0 =A0/* do the reserve before the mutex lock in case we hav=

e to do some

Quoted 1 line

=A0 =A0 =A0 =A0 * flushing. =A0We wouldn't deadlock, but this is more=

polite.

Quoted 2 lines

=A0 =A0 =A0 =A0 */
- =A0 =A0 =A0 err =3D btrfs_reserve_metadata_for_delalloc(root, inode=

, 1);

Quoted 1 line

+ =A0 =A0 =A0 err =3D btrfs_reserve_metadata_for_delalloc(root, inode=

, reserved);

fs/btrfs/file.c out: · line 979

Quoted 9 lines

=A0 =A0 =A0 =A0if (err)
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0goto out_nolock;

fs/btrfs/file.c +1 -1
@@ -975,7 +976,7 @@ out:
 =A0 =A0 =A0 =A0mutex_unlock(&inode->i_mutex);
 =A0 =A0 =A0 =A0if (ret)
 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0err =3D ret;
- =A0 =A0 =A0 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
+ =A0 =A0 =A0 btrfs_unreserve_metadata_for_delalloc(root, inode, rese=

rved);

fs/btrfs/inode.c static int btrfs_split_extent_hook(struct ino=

Quoted 8 lines

=A0out_nolock:
=A0 =A0 =A0 =A0kfree(pages);

fs/btrfs/inode.c +0 -0
index 2a337a0..52cb47f 100644
@@ -1235,19 +1235,37 @@ static int btrfs_split_extent_hook(struct ino=

de *inode,

fs/btrfs/inode.c static int btrfs_split_extent_hook(struct ino= · line 1241

Quoted 11 lines
fs/btrfs/inode.c +3 -4
 =A0 =A0 =A0 =A0size =3D orig->end - orig->start + 1;
 =A0 =A0 =A0 =A0if (size > root->fs_info->max_extent) {
- =A0 =A0 =A0 =A0 =A0 =A0 =A0 u64 num_extents;
- =A0 =A0 =A0 =A0 =A0 =A0 =A0 u64 new_size;
+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 u64 left_extents, right_extents;
+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 u64 orig_extents;
+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 u64 left_size, right_size;
 
- =A0 =A0 =A0 =A0 =A0 =A0 =A0 new_size =3D orig->end - split + 1;
- =A0 =A0 =A0 =A0 =A0 =A0 =A0 num_extents =3D div64_u64(size + root->=

fs_info->max_extent - 1,

fs/btrfs/inode.c static int btrfs_split_extent_hook(struct ino= · line 1242

Quoted 1 line
fs/btrfs/inode.c +0 -1
- =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=

=A0 =A0 root->fs_info->max_extent);

fs/btrfs/inode.c static int btrfs_split_extent_hook(struct ino= · line 1243

Quoted 3 lines
fs/btrfs/inode.c +3 -0
+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 left_size =3D orig->end - split + 1;
+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 right_size =3D split - orig->start;
+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 left_extents =3D calculate_extents(root=

, left_size);

fs/btrfs/inode.c static int btrfs_split_extent_hook(struct ino= · line 1244

Quoted 1 line
fs/btrfs/inode.c +1 -0
+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 right_extents =3D calculate_extents(roo=

t, right_size);

fs/btrfs/inode.c static int btrfs_split_extent_hook(struct ino= · line 1245

Quoted 1 line
fs/btrfs/inode.c +1 -0
+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 orig_extents =3D calculate_extents(root=

, size);

Quoted 3 lines

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0/*
- =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0* if we break a large extent up then=

leave oustanding_extents

Quoted 1 line

- =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0* be, since we've already accounted =

for the large extent.

Quoted 1 line

+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0* If we are splitting off a max_exte=

nt multiple of space, then

Quoted 1 line

+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0* we don't need to account for anoth=

er extent. =A0However if we

Quoted 1 line

+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0* take off less than a max_extent, w=

e need to add another

Quoted 1 line

+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0* outstanding_extent, because we wil=

l likely still have the

Quoted 1 line

+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0* original amount of extents needed =

for the big chunk left

Quoted 3 lines

+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0* over.
+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0*
+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0* Think of it this way. =A0We have 4=

outstanding extents for the

Quoted 1 line

+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0* original giant chunk of data. =A0I=

f we split off a max_extent

Quoted 1 line

+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0* sized chunk, then the remaining ch=

unk has 3 outstanding

Quoted 1 line

+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0* extens, and the new little chunk h=

as 1 outstanding extents

Quoted 3 lines

+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0* worth, so we're even.
+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0*
+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0* But if we carve off < max_extent, =

thats its own extent still.

Quoted 1 line

+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0* So the left over chunk still needs=

4 extents to describe it

Quoted 1 line

+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0* on disk, and the chunk we just spl=

it off needs 1 extent, so

Quoted 1 line

+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0* thats 5 total extents, so we need =

to add 1 to

Quoted 5 lines

+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0* outstanding_extents.
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 */
- =A0 =A0 =A0 =A0 =A0 =A0 =A0 if (div64_u64(new_size + root->fs_info-= max_extent - 1,
- =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 root->fs_in=

fo->max_extent) < num_extents)

Quoted 1 line

+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 if (left_extents + right_extents =3D=3D=

orig_extents)

fs/btrfs/inode.c static int btrfs_merge_extent_hook(struct ino=

Quoted 4 lines

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0return 0; =A0 =A0 =A0 =A0}

fs/btrfs/inode.c +0 -0
@@ -1269,18 +1287,16 @@ static int btrfs_merge_extent_hook(struct ino=

de *inode,

Quoted 1 line

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 s=

truct extent_state *other)

Quoted 7 lines

=A0{
=A0 =A0 =A0 =A0struct btrfs_root *root =3D BTRFS_I(inode)->root;
- =A0 =A0 =A0 u64 new_size, old_size;
- =A0 =A0 =A0 u64 num_extents;
+ =A0 =A0 =A0 u64 new_size, size1, size2;
+ =A0 =A0 =A0 u64 extents1, extents2;

Quoted 7 lines

=A0 =A0 =A0 =A0/* not delalloc, ignore it */ =A0 =A0 =A0 =A0if (!(other->state & EXTENT_DELALLOC)) =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0return 0;

- =A0 =A0 =A0 old_size =3D other->end - other->start + 1;
- =A0 =A0 =A0 if (new->start < other->start)
- =A0 =A0 =A0 =A0 =A0 =A0 =A0 new_size =3D other->end - new->start + =

1;

Quoted 2 lines

- =A0 =A0 =A0 else
- =A0 =A0 =A0 =A0 =A0 =A0 =A0 new_size =3D new->end - other->start + =

1;

Quoted 5 lines

+ =A0 =A0 =A0 size1 =3D other->end - other->start + 1;
+ =A0 =A0 =A0 size2 =3D new->end - new->start + 1;
+ =A0 =A0 =A0 new_size =3D size1 + size2;

=A0 =A0 =A0 =A0/* we're not bigger than the max, unreserve the space =

and go */

fs/btrfs/inode.c static int btrfs_merge_extent_hook(struct ino=

Quoted 2 lines

=A0 =A0 =A0 =A0if (new_size <=3D root->fs_info->max_extent) {

fs/btrfs/inode.c +0 -0
@@ -1290,14 +1306,20 @@ static int btrfs_merge_extent_hook(struct ino=

de *inode,

Quoted 8 lines
fs/btrfs/inode.c +3 -0
 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0return 0;
 =A0 =A0 =A0 =A0}
 
+ =A0 =A0 =A0 extents1 =3D calculate_extents(root, size1);
+ =A0 =A0 =A0 extents2 =3D calculate_extents(root, size2);
+

=A0 =A0 =A0 =A0/*
- =A0 =A0 =A0 =A0* If we grew by another max_extent, just return, we =

want to keep that

Quoted 2 lines

- =A0 =A0 =A0 =A0* reserved amount.
+ =A0 =A0 =A0 =A0* So if the number of extents required for the two c=

hunks of space we

Quoted 1 line

+ =A0 =A0 =A0 =A0* are merging equals the number of extents we need f=

or the entire space

Quoted 3 lines

+ =A0 =A0 =A0 =A0* we now have, just return.
+ =A0 =A0 =A0 =A0*
+ =A0 =A0 =A0 =A0* The idea here is if max extent is say 1M, and the =

first chunk was

Quoted 1 line

+ =A0 =A0 =A0 =A0* exactly 1M and the second chunk was 4k, we now nee=

d 2 extents to

Quoted 1 line

+ =A0 =A0 =A0 =A0* cover that area, so we keep the reservation we got=

from adding the 4k

Quoted 3 lines

+ =A0 =A0 =A0 =A0* extent.
=A0 =A0 =A0 =A0 */
- =A0 =A0 =A0 num_extents =3D div64_u64(old_size + root->fs_info->max=

_extent - 1,

Quoted 1 line

- =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 root->f=

s_info->max_extent);

Quoted 4 lines

- =A0 =A0 =A0 if (div64_u64(new_size + root->fs_info->max_extent - 1,
- =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 root->fs_info->max_extent) = num_extents)
+ =A0 =A0 =A0 if (calculate_extents(root, new_size) =3D=3D extents1 +=

extents2)

fs/btrfs/inode.c static int btrfs_set_bit_hook(struct inode *i=

Quoted 4 lines

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0return 0;

=A0 =A0 =A0 =A0spin_lock(&BTRFS_I(inode)->accounting_lock);

fs/btrfs/inode.c +0 -0
@@ -1323,11 +1345,13 @@ static int btrfs_set_bit_hook(struct inode *i=

node, u64 start, u64 end,

Quoted 2 lines

=A0 =A0 =A0 =A0 */
=A0 =A0 =A0 =A0if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLO=

C)) {

Quoted 1 line

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0struct btrfs_root *root =3D BTRFS_I(in=

ode)->root;

Quoted 1 line

+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 int extents =3D calculate_extents(root,=

end - start + 1);

Quoted 2 lines

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0spin_lock(&BTRFS_I(inode)->accounting_=

lock);

Quoted 2 lines

- =A0 =A0 =A0 =A0 =A0 =A0 =A0 BTRFS_I(inode)->outstanding_extents++;
+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 BTRFS_I(inode)->outstanding_extents +=3D=

extents;

Quoted 1 line

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0spin_unlock(&BTRFS_I(inode)->accountin=

g_lock);

Quoted 1 line

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0btrfs_delalloc_reserve_space(root, ino=

de, end - start + 1);

Quoted 2 lines

+
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0spin_lock(&root->fs_info->delalloc_loc=

k);

Quoted 1 line

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0BTRFS_I(inode)->delalloc_bytes +=3D en=

d - start + 1;

Quoted 1 line

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0root->fs_info->delalloc_bytes +=3D end=

- start + 1;

fs/btrfs/inode.c static int btrfs_clear_bit_hook(struct inode =

Quoted 1 line
fs/btrfs/inode.c +0 -0
@@ -1355,10 +1379,15 @@ static int btrfs_clear_bit_hook(struct inode =

*inode,

Quoted 1 line

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0struct btrfs_root *root =3D BTRFS_I(in=

ode)->root;

Quoted 3 lines

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0if (bits & EXTENT_DO_ACCOUNTING) {
+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 int extents =3D calcula=

te_extents(root, state->end -

Quoted 1 line

+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 state->start + 1);

Quoted 2 lines

+
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0spin_lock(&BTRFS_I(ino=

de)->accounting_lock);

Quoted 1 line

- =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 BTRFS_I(inode)->outstan=

ding_extents--;

Quoted 3 lines

+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 WARN_ON(BTRFS_I(inode)-= outstanding_extents < extents);
+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 BTRFS_I(inode)->outstan=

ding_extents -=3D extents;

Quoted 1 line

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0spin_unlock(&BTRFS_I(i=

node)->accounting_lock);

Quoted 1 line

- =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 btrfs_unreserve_metadat=

a_for_delalloc(root, inode, 1);

Quoted 1 line

+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 btrfs_unreserve_metadat=

a_for_delalloc(root, inode,

Quoted 1 line

+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 extents);

Quoted 3 lines

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0}

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0spin_lock(&root->fs_info->delalloc_loc=

k);

fs/btrfs/inode.c static int btrfs_truncate_page(struct address_s=

Quoted 1 line
fs/btrfs/inode.c +0 -0
@@ -3106,7 +3135,8 @@ static int btrfs_truncate_page(struct address_s=

pace *mapping, loff_t from)

fs/btrfs/inode.c static int btrfs_truncate_page(struct address_s= · line 3109

Quoted 4 lines
fs/btrfs/inode.c +0 -1
 =A0 =A0 =A0 =A0if (ret)
 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0goto out;
 
- =A0 =A0 =A0 ret =3D btrfs_reserve_metadata_for_delalloc(root, inode=

, 1);

fs/btrfs/inode.c static int btrfs_truncate_page(struct address_s= · line 3138

Quoted 1 line
fs/btrfs/inode.c +1 -0
+ =A0 =A0 =A0 ret =3D btrfs_reserve_metadata_for_delalloc(root, inode=

,

fs/btrfs/inode.c static int btrfs_truncate_page(struct address_s= · line 3139

Quoted 1 line
fs/btrfs/inode.c +1 -0
+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 calcula=

te_extents(root, PAGE_CACHE_SIZE));

Quoted 7 lines
fs/btrfs/inode.c +0 -0
 =A0 =A0 =A0 =A0if (ret)
 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0goto out;
 
@@ -3115,7 +3145,8 @@ again:
 =A0 =A0 =A0 =A0page =3D grab_cache_page(mapping, index);
 =A0 =A0 =A0 =A0if (!page) {

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0btrfs_free_reserved_data_space(root, i=

node, PAGE_CACHE_SIZE);

Quoted 1 line

- =A0 =A0 =A0 =A0 =A0 =A0 =A0 btrfs_unreserve_metadata_for_delalloc(r=

oot, inode, 1);

Quoted 1 line

+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 btrfs_unreserve_metadata_for_delalloc(r=

oot, inode,

Quoted 1 line

+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 calcula=

te_extents(root, PAGE_CACHE_SIZE));

Quoted 7 lines

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0goto out;
=A0 =A0 =A0 =A0}

fs/btrfs/inode.c +0 -0
@@ -3179,7 +3210,8 @@ again:

=A0out_unlock:
=A0 =A0 =A0 =A0if (ret)
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0btrfs_free_reserved_data_space(root, i=

node, PAGE_CACHE_SIZE);

Quoted 3 lines

- =A0 =A0 =A0 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
+ =A0 =A0 =A0 btrfs_unreserve_metadata_for_delalloc(root, inode,
+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 calcula=

te_extents(root, PAGE_CACHE_SIZE));

fs/btrfs/inode.c int btrfs_page_mkwrite(struct vm_area_struct *v=

Quoted 4 lines

=A0 =A0 =A0 =A0unlock_page(page);
=A0 =A0 =A0 =A0page_cache_release(page);
=A0out:

fs/btrfs/inode.c +0 -0
@@ -5098,7 +5130,8 @@ int btrfs_page_mkwrite(struct vm_area_struct *v=

ma, struct vm_fault *vmf)

fs/btrfs/inode.c int btrfs_page_mkwrite(struct vm_area_struct *v= · line 5101

Quoted 4 lines
fs/btrfs/inode.c +0 -1
 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0goto out;
 =A0 =A0 =A0 =A0}
 
- =A0 =A0 =A0 ret =3D btrfs_reserve_metadata_for_delalloc(root, inode=

, 1);

fs/btrfs/inode.c int btrfs_page_mkwrite(struct vm_area_struct *v= · line 5133

Quoted 1 line
fs/btrfs/inode.c +1 -0
+ =A0 =A0 =A0 ret =3D btrfs_reserve_metadata_for_delalloc(root, inode=

,

fs/btrfs/inode.c int btrfs_page_mkwrite(struct vm_area_struct *v= · line 5134

Quoted 1 line
fs/btrfs/inode.c +1 -0
+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 calcula=

te_extents(root, PAGE_CACHE_SIZE));

Quoted 2 lines
fs/btrfs/inode.c +0 -0
 =A0 =A0 =A0 =A0if (ret) {

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0btrfs_free_reserved_data_space(root, i=

node, PAGE_CACHE_SIZE);

Quoted 3 lines

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0ret =3D VM_FAULT_SIGBUS;

fs/btrfs/inode.c +0 -0
@@ -5182,7 +5215,8 @@ again:

=A0 =A0 =A0 =A0unlock_extent_cached(io_tree, page_start, page_end, &c=

ached_state, GFP_NOFS);

Quoted 5 lines

=A0out_unlock:
- =A0 =A0 =A0 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
+ =A0 =A0 =A0 btrfs_unreserve_metadata_for_delalloc(root, inode,
+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 calcula=

te_extents(root, PAGE_CACHE_SIZE));

Quoted 9 lines

=A0 =A0 =A0 =A0if (!ret)
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0return VM_FAULT_LOCKED; =A0 =A0 =A0 =A0unlock_page(page);

fs/btrfs/inode.c +0 -1
@@ -5384,7 +5418,6 @@ free:
 =A0void btrfs_drop_inode(struct inode *inode)
 =A0{
 =A0 =A0 =A0 =A0struct btrfs_root *root =3D BTRFS_I(inode)->root;
-

=A0 =A0 =A0 =A0if (inode->i_nlink > 0 && btrfs_root_refs(&root->root_=

item) =3D=3D 0)

fs/btrfs/ordered-data.c static int __btrfs_remove_ordered_extent(struct i=

Quoted 7 lines

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0generic_delete_inode(inode); =A0 =A0 =A0 =A0else

fs/btrfs/ordered-data.c +0 -0
index a8ffecd..5c99882 100644
@@ -303,6 +303,7 @@ static int __btrfs_remove_ordered_extent(struct i=

node *inode,

Quoted 1 line

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0struct=

btrfs_ordered_extent *entry)

fs/btrfs/ordered-data.c static int __btrfs_remove_ordered_extent(struct=

Quoted 7 lines

=A0{
=A0 =A0 =A0 =A0struct btrfs_ordered_inode_tree *tree;
+ =A0 =A0 =A0 struct btrfs_root *root =3D BTRFS_I(inode)->root; =A0 =A0 =A0 =A0struct rb_node *node;

=A0 =A0 =A0 =A0tree =3D &BTRFS_I(inode)->ordered_tree;

fs/btrfs/ordered-data.c +0 -0
@@ -312,12 +313,13 @@ static int __btrfs_remove_ordered_extent(struct=

inode *inode,

Quoted 7 lines
fs/btrfs/ordered-data.c +1 -0
 =A0 =A0 =A0 =A0set_bit(BTRFS_ORDERED_COMPLETE, &entry->flags);
 
 =A0 =A0 =A0 =A0spin_lock(&BTRFS_I(inode)->accounting_lock);
+ =A0 =A0 =A0 WARN_ON(!BTRFS_I(inode)->outstanding_extents);
 =A0 =A0 =A0 =A0BTRFS_I(inode)->outstanding_extents--;
 =A0 =A0 =A0 =A0spin_unlock(&BTRFS_I(inode)->accounting_lock);

=A0 =A0 =A0 =A0btrfs_unreserve_metadata_for_delalloc(BTRFS_I(inode)->=

root,

Quoted 1 line

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=

=A0 =A0 =A0 =A0 =A0inode, 1);

Quoted 2 lines

- =A0 =A0 =A0 spin_lock(&BTRFS_I(inode)->root->fs_info->ordered_exten=

t_lock);

fs/btrfs/ordered-data.c static int __btrfs_remove_ordered_extent(struct i=

Quoted 5 lines

+ =A0 =A0 =A0 spin_lock(&root->fs_info->ordered_extent_lock); =A0 =A0 =A0 =A0list_del_init(&entry->root_extent_list);

=A0 =A0 =A0 =A0/*

fs/btrfs/ordered-data.c +0 -0
@@ -329,7 +331,7 @@ static int __btrfs_remove_ordered_extent(struct i=

node *inode,

Quoted 1 line

=A0 =A0 =A0 =A0 =A0 =A0!mapping_tagged(inode->i_mapping, PAGECACHE_TA=

G_DIRTY)) {

Quoted 1 line

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0list_del_init(&BTRFS_I(inode)->ordered=

_operations);

Quoted 2 lines

=A0 =A0 =A0 =A0}
- =A0 =A0 =A0 spin_unlock(&BTRFS_I(inode)->root->fs_info->ordered_ext=

ent_lock);

Quoted 9 lines

+ =A0 =A0 =A0 spin_unlock(&root->fs_info->ordered_extent_lock);

=A0 =A0 =A0 =A0return 0;
=A0}
--
1.6.6

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs=

" in

Quoted 3 lines

the body of a message to [email protected]
More majordomo info at =A0http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" = in
the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#2 lore raw
Quoted 1 line · Zheng

On Thu, Mar 18, 2010 at 11:47 PM, Josef Bacik <[email protected]> wrot=

e:

Quoted 1 line
Quoted 1 line

A user reported a bug a few weeks back where if he set max_extent=3D=

1m and then

Quoted 1 line
Quoted 1 line

did a dd and then stopped it, we would panic. =A0This is because I =

miscalculated

Quoted 1 line
Quoted 1 line

how many extents would be needed for splits/merges. =A0Turns out I =

didn't actually

Quoted 1 line
Quoted 1 line

take max_extent into account properly, since we only ever add 1 ext=

ent for a

Quoted 1 line
Quoted 1 line

write, which isn't quite right for the case that say max_extent is =

4k and we do

Quoted 1 line
Quoted 1 line

8k writes. =A0That would result in more than 1 extent. =A0So this p=

atch makes us

Quoted 1 line
Quoted 1 line

properly figure out how many extents are needed for the amount of d=

ata that is

Quoted 1 line
Quoted 1 line

being written, and deals with splitting and merging better. =A0I've=

tested this ad

Quoted 1 line
Quoted 1 line

nauseum and it works well. =A0This version doesn't depend on my per=

-cpu stuff.

Quoted 3 lines
Quoted 1 line

Thanks,

=20
Why not remove the the max_extent check. The max length of file exten=

t

Quoted 1 line

is also affected by fragmentation level of free space. It doesn't mak=

e sense

Quoted 1 line

to introduce complex code to address one factor while lose sight of a=

nother

Quoted 1 line

factor. I think reserving one unit of metadata for each delalloc exte=

nt in the

Quoted 1 line

extent IO tree should be OK. because even a delalloc extent ends up w=

ith

Quoted 1 line

multiple file extents, these file extents are adjacency in the b-tree=

=2E

Quoted 1 line

=20

Do you mean remove the option for max_extent altogether, or just remove=
all of
my code for taking it into account? Thanks,

Josef
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" = in
the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#3 lore raw
Quoted 2 lines · Josef Bacik
Quoted 1 line · On Fri, Mar 19, 2010 at 11:09:25AM +0800, Yan, Zheng =A0wrote:

On Thu, Mar 18, 2010 at 11:47 PM, Josef Bacik <[email protected]> wro=

te:

Quoted 1 line
Quoted 1 line
Quoted 1 line

A user reported a bug a few weeks back where if he set max_extent=3D=

1m and then

Quoted 1 line
Quoted 1 line
Quoted 1 line

did a dd and then stopped it, we would panic. =A0This is because I=

miscalculated

Quoted 1 line
Quoted 1 line
Quoted 1 line

how many extents would be needed for splits/merges. =A0Turns out I=

didn't actually

Quoted 1 line
Quoted 1 line
Quoted 1 line

take max_extent into account properly, since we only ever add 1 ex=

tent for a

Quoted 1 line
Quoted 1 line
Quoted 1 line

write, which isn't quite right for the case that say max_extent is=

4k and we do

Quoted 1 line
Quoted 1 line
Quoted 1 line

8k writes. =A0That would result in more than 1 extent. =A0So this =

patch makes us

Quoted 1 line
Quoted 1 line
Quoted 1 line

properly figure out how many extents are needed for the amount of =

data that is

Quoted 1 line
Quoted 1 line
Quoted 1 line

being written, and deals with splitting and merging better. =A0I'v=

e tested this ad

Quoted 1 line
Quoted 1 line
Quoted 1 line

nauseum and it works well. =A0This version doesn't depend on my pe=

r-cpu stuff.

Quoted 3 lines
Quoted 3 lines
Quoted 1 line

Thanks,

Why not remove the the max_extent check. The max length of file exte=

nt

Quoted 1 line
Quoted 1 line

is also affected by fragmentation level of free space. It doesn't ma=

ke sense

Quoted 1 line
Quoted 1 line

to introduce complex code to address one factor while lose sight of =

another

Quoted 1 line
Quoted 1 line

factor. I think reserving one unit of metadata for each delalloc ext=

ent in the

Quoted 1 line
Quoted 1 line

extent IO tree should be OK. because even a delalloc extent ends up =

with

Quoted 1 line
Quoted 1 line

multiple file extents, these file extents are adjacency in the b-tre=

e.

Quoted 3 lines
Quoted 1 line

Do you mean remove the option for max_extent altogether, or just remo=

ve all of

Quoted 2 lines

my code for taking it into account? =A0Thanks,

all of the code for taking max_extent into account
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" = in
the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html