1 ext4: miscellaneous partial cluster cleanups
3 From: Eric Whitney <enwlinux@gmail.com>
5 Add some casts and rearrange a few statements for improved readability.
6 Some code can also be simplified and made more readable if we set
7 partial_cluster to 0 rather than to a negative value when we can tell
8 we've hit the left edge of the punched region.
10 Signed-off-by: Eric Whitney <enwlinux@gmail.com>
11 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
13 fs/ext4/extents.c | 39 +++++++++++++++++++++------------------
14 1 file changed, 21 insertions(+), 18 deletions(-)
16 diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c
17 index 859ab37..841adf0 100644
18 --- a/fs/ext4/extents.c
19 +++ b/fs/ext4/extents.c
20 @@ -2481,7 +2481,7 @@ static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
21 ext4_lblk_t from, ext4_lblk_t to)
23 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
24 - unsigned short ee_len = ext4_ext_get_actual_len(ex);
25 + unsigned short ee_len = ext4_ext_get_actual_len(ex);
27 int flags = get_default_free_blocks_flags(inode);
29 @@ -2490,7 +2490,7 @@ static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
30 * at the beginning of the extent. Instead, we make a note
31 * that we tried freeing the cluster, and check to see if we
32 * need to free it on a subsequent call to ext4_remove_blocks,
33 - * or at the end of the ext4_truncate() operation.
34 + * or at the end of ext4_ext_rm_leaf or ext4_ext_remove_space.
36 flags |= EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER;
38 @@ -2501,8 +2501,8 @@ static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
39 * partial cluster here.
41 pblk = ext4_ext_pblock(ex) + ee_len - 1;
42 - if ((*partial_cluster > 0) &&
43 - (EXT4_B2C(sbi, pblk) != *partial_cluster)) {
44 + if (*partial_cluster > 0 &&
45 + *partial_cluster != (long long) EXT4_B2C(sbi, pblk)) {
46 ext4_free_blocks(handle, inode, NULL,
47 EXT4_C2B(sbi, *partial_cluster),
48 sbi->s_cluster_ratio, flags);
49 @@ -2528,7 +2528,7 @@ static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
50 && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
53 - unsigned int unaligned;
54 + long long first_cluster;
56 num = le32_to_cpu(ex->ee_block) + ee_len - from;
57 pblk = ext4_ext_pblock(ex) + ee_len - num;
58 @@ -2538,7 +2538,7 @@ static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
59 * used by any other extent (partial_cluster is negative).
61 if (*partial_cluster < 0 &&
62 - -(*partial_cluster) == EXT4_B2C(sbi, pblk + num - 1))
63 + *partial_cluster == -(long long) EXT4_B2C(sbi, pblk+num-1))
64 flags |= EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER;
66 ext_debug("free last %u blocks starting %llu partial %lld\n",
67 @@ -2549,21 +2549,24 @@ static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
68 * beginning of a cluster, and we removed the entire
69 * extent and the cluster is not used by any other extent,
70 * save the partial cluster here, since we might need to
71 - * delete if we determine that the truncate operation has
72 - * removed all of the blocks in the cluster.
73 + * delete if we determine that the truncate or punch hole
74 + * operation has removed all of the blocks in the cluster.
75 + * If that cluster is used by another extent, preserve its
76 + * negative value so it isn't freed later on.
78 - * On the other hand, if we did not manage to free the whole
79 - * extent, we have to mark the cluster as used (store negative
80 - * cluster number in partial_cluster).
81 + * If the whole extent wasn't freed, we've reached the
82 + * start of the truncated/punched region and have finished
83 + * removing blocks. If there's a partial cluster here it's
84 + * shared with the remainder of the extent and is no longer
85 + * a candidate for removal.
87 - unaligned = EXT4_PBLK_COFF(sbi, pblk);
88 - if (unaligned && (ee_len == num) &&
89 - (*partial_cluster != -((long long)EXT4_B2C(sbi, pblk))))
90 - *partial_cluster = EXT4_B2C(sbi, pblk);
92 - *partial_cluster = -((long long)EXT4_B2C(sbi, pblk));
93 - else if (*partial_cluster > 0)
94 + if (EXT4_PBLK_COFF(sbi, pblk) && ee_len == num) {
95 + first_cluster = (long long) EXT4_B2C(sbi, pblk);
96 + if (first_cluster != -*partial_cluster)
97 + *partial_cluster = first_cluster;
102 ext4_error(sbi->s_sb, "strange request: removal(2) "
103 "%u-%u from %u:%u\n",