f2fs: introduce F2FS_IOC_RELEASE_COMPRESS_BLOCKS
There are still reserved blocks on compressed inode, this patch introduce a new ioctl to help release reserved blocks back to filesystem, so that userspace can reuse those freed space. ---- Daeho fixed a bug like below. Now, if writing pages and releasing compress blocks occur simultaneously, and releasing cblocks is executed more than one time to a file, then total block count of filesystem and block count of the file could be incorrect and damaged. We have to execute releasing compress blocks only one time for a file without being interfered by writepages path. --- Signed-off-by: Chao Yu <yuchao0@huawei.com> Signed-off-by: Daeho Jeong <daehojeong@google.com> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
This commit is contained in:
parent
042be373ad
commit
ef8d563f18
@ -428,6 +428,8 @@ static inline bool __has_cursum_space(struct f2fs_journal *journal,
|
||||
#define F2FS_IOC_PRECACHE_EXTENTS _IO(F2FS_IOCTL_MAGIC, 15)
|
||||
#define F2FS_IOC_RESIZE_FS _IOW(F2FS_IOCTL_MAGIC, 16, __u64)
|
||||
#define F2FS_IOC_GET_COMPRESS_BLOCKS _IOR(F2FS_IOCTL_MAGIC, 17, __u64)
|
||||
#define F2FS_IOC_RELEASE_COMPRESS_BLOCKS \
|
||||
_IOR(F2FS_IOCTL_MAGIC, 18, __u64)
|
||||
|
||||
#define F2FS_IOC_GET_VOLUME_NAME FS_IOC_GETFSLABEL
|
||||
#define F2FS_IOC_SET_VOLUME_NAME FS_IOC_SETFSLABEL
|
||||
@ -4048,6 +4050,10 @@ static inline void f2fs_i_compr_blocks_update(struct inode *inode,
|
||||
{
|
||||
int diff = F2FS_I(inode)->i_cluster_size - blocks;
|
||||
|
||||
/* don't update i_compr_blocks if saved blocks were released */
|
||||
if (!add && !F2FS_I(inode)->i_compr_blocks)
|
||||
return;
|
||||
|
||||
if (add) {
|
||||
F2FS_I(inode)->i_compr_blocks += diff;
|
||||
stat_add_compr_blocks(inode, diff);
|
||||
|
169
fs/f2fs/file.c
169
fs/f2fs/file.c
@ -563,6 +563,7 @@ void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
|
||||
bool compressed_cluster = false;
|
||||
int cluster_index = 0, valid_blocks = 0;
|
||||
int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
|
||||
bool released = !F2FS_I(dn->inode)->i_compr_blocks;
|
||||
|
||||
if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
|
||||
base = get_extra_isize(dn->inode);
|
||||
@ -601,7 +602,9 @@ void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
|
||||
clear_inode_flag(dn->inode, FI_FIRST_BLOCK_WRITTEN);
|
||||
|
||||
f2fs_invalidate_blocks(sbi, blkaddr);
|
||||
nr_free++;
|
||||
|
||||
if (!released || blkaddr != COMPRESS_ADDR)
|
||||
nr_free++;
|
||||
}
|
||||
|
||||
if (compressed_cluster)
|
||||
@ -3434,6 +3437,167 @@ static int f2fs_get_compress_blocks(struct file *filp, unsigned long arg)
|
||||
return put_user(blocks, (u64 __user *)arg);
|
||||
}
|
||||
|
||||
static int release_compress_blocks(struct dnode_of_data *dn, pgoff_t count)
|
||||
{
|
||||
struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
|
||||
unsigned int released_blocks = 0;
|
||||
int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
|
||||
block_t blkaddr;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < count; i++) {
|
||||
blkaddr = data_blkaddr(dn->inode, dn->node_page,
|
||||
dn->ofs_in_node + i);
|
||||
|
||||
if (!__is_valid_data_blkaddr(blkaddr))
|
||||
continue;
|
||||
if (unlikely(!f2fs_is_valid_blkaddr(sbi, blkaddr,
|
||||
DATA_GENERIC_ENHANCE)))
|
||||
return -EFSCORRUPTED;
|
||||
}
|
||||
|
||||
while (count) {
|
||||
int compr_blocks = 0;
|
||||
|
||||
for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) {
|
||||
blkaddr = f2fs_data_blkaddr(dn);
|
||||
|
||||
if (i == 0) {
|
||||
if (blkaddr == COMPRESS_ADDR)
|
||||
continue;
|
||||
dn->ofs_in_node += cluster_size;
|
||||
goto next;
|
||||
}
|
||||
|
||||
if (__is_valid_data_blkaddr(blkaddr))
|
||||
compr_blocks++;
|
||||
|
||||
if (blkaddr != NEW_ADDR)
|
||||
continue;
|
||||
|
||||
dn->data_blkaddr = NULL_ADDR;
|
||||
f2fs_set_data_blkaddr(dn);
|
||||
}
|
||||
|
||||
f2fs_i_compr_blocks_update(dn->inode, compr_blocks, false);
|
||||
dec_valid_block_count(sbi, dn->inode,
|
||||
cluster_size - compr_blocks);
|
||||
|
||||
released_blocks += cluster_size - compr_blocks;
|
||||
next:
|
||||
count -= cluster_size;
|
||||
}
|
||||
|
||||
return released_blocks;
|
||||
}
|
||||
|
||||
static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)
|
||||
{
|
||||
struct inode *inode = file_inode(filp);
|
||||
struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
||||
pgoff_t page_idx = 0, last_idx;
|
||||
unsigned int released_blocks = 0;
|
||||
int ret;
|
||||
int writecount;
|
||||
|
||||
if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
|
||||
return -EOPNOTSUPP;
|
||||
|
||||
if (!f2fs_compressed_file(inode))
|
||||
return -EINVAL;
|
||||
|
||||
if (f2fs_readonly(sbi->sb))
|
||||
return -EROFS;
|
||||
|
||||
ret = mnt_want_write_file(filp);
|
||||
if (ret)
|
||||
return ret;
|
||||
|
||||
f2fs_balance_fs(F2FS_I_SB(inode), true);
|
||||
|
||||
inode_lock(inode);
|
||||
|
||||
writecount = atomic_read(&inode->i_writecount);
|
||||
if ((filp->f_mode & FMODE_WRITE && writecount != 1) || writecount) {
|
||||
ret = -EBUSY;
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (IS_IMMUTABLE(inode)) {
|
||||
ret = -EINVAL;
|
||||
goto out;
|
||||
}
|
||||
|
||||
ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
|
||||
if (ret)
|
||||
goto out;
|
||||
|
||||
if (!F2FS_I(inode)->i_compr_blocks)
|
||||
goto out;
|
||||
|
||||
F2FS_I(inode)->i_flags |= F2FS_IMMUTABLE_FL;
|
||||
f2fs_set_inode_flags(inode);
|
||||
inode->i_ctime = current_time(inode);
|
||||
f2fs_mark_inode_dirty_sync(inode, true);
|
||||
|
||||
down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
|
||||
down_write(&F2FS_I(inode)->i_mmap_sem);
|
||||
|
||||
last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
|
||||
|
||||
while (page_idx < last_idx) {
|
||||
struct dnode_of_data dn;
|
||||
pgoff_t end_offset, count;
|
||||
|
||||
set_new_dnode(&dn, inode, NULL, NULL, 0);
|
||||
ret = f2fs_get_dnode_of_data(&dn, page_idx, LOOKUP_NODE);
|
||||
if (ret) {
|
||||
if (ret == -ENOENT) {
|
||||
page_idx = f2fs_get_next_page_offset(&dn,
|
||||
page_idx);
|
||||
ret = 0;
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
|
||||
count = min(end_offset - dn.ofs_in_node, last_idx - page_idx);
|
||||
count = roundup(count, F2FS_I(inode)->i_cluster_size);
|
||||
|
||||
ret = release_compress_blocks(&dn, count);
|
||||
|
||||
f2fs_put_dnode(&dn);
|
||||
|
||||
if (ret < 0)
|
||||
break;
|
||||
|
||||
page_idx += count;
|
||||
released_blocks += ret;
|
||||
}
|
||||
|
||||
up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
|
||||
up_write(&F2FS_I(inode)->i_mmap_sem);
|
||||
out:
|
||||
inode_unlock(inode);
|
||||
|
||||
mnt_drop_write_file(filp);
|
||||
|
||||
if (ret >= 0) {
|
||||
ret = put_user(released_blocks, (u64 __user *)arg);
|
||||
} else if (released_blocks && F2FS_I(inode)->i_compr_blocks) {
|
||||
set_sbi_flag(sbi, SBI_NEED_FSCK);
|
||||
f2fs_warn(sbi, "%s: partial blocks were released i_ino=%lx "
|
||||
"iblocks=%llu, released=%u, compr_blocks=%llu, "
|
||||
"run fsck to fix.",
|
||||
__func__, inode->i_ino, inode->i_blocks,
|
||||
released_blocks,
|
||||
F2FS_I(inode)->i_compr_blocks);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
|
||||
{
|
||||
if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(filp)))))
|
||||
@ -3516,6 +3680,8 @@ long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
|
||||
return f2fs_set_volume_name(filp, arg);
|
||||
case F2FS_IOC_GET_COMPRESS_BLOCKS:
|
||||
return f2fs_get_compress_blocks(filp, arg);
|
||||
case F2FS_IOC_RELEASE_COMPRESS_BLOCKS:
|
||||
return f2fs_release_compress_blocks(filp, arg);
|
||||
default:
|
||||
return -ENOTTY;
|
||||
}
|
||||
@ -3683,6 +3849,7 @@ long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
|
||||
case F2FS_IOC_GET_VOLUME_NAME:
|
||||
case F2FS_IOC_SET_VOLUME_NAME:
|
||||
case F2FS_IOC_GET_COMPRESS_BLOCKS:
|
||||
case F2FS_IOC_RELEASE_COMPRESS_BLOCKS:
|
||||
break;
|
||||
default:
|
||||
return -ENOIOCTLCMD;
|
||||
|
Loading…
Reference in New Issue
Block a user