Skip to content

Commit dfbdbf0

Browse files
damien-lemoalgregkh
authored andcommitted
zonefs: Fix O_APPEND async write handling
commit ebfd68c upstream. zonefs updates the size of a sequential zone file inode only on completion of direct writes. When executing asynchronous append writes (with a file open with O_APPEND or using RWF_APPEND), the use of the current inode size in generic_write_checks() to set an iocb offset thus leads to unaligned write if an application issues an append write operation with another write already being executed. Fix this problem by introducing zonefs_write_checks() as a modified version of generic_write_checks() using the file inode wp_offset for an append write iocb offset. Also introduce zonefs_write_check_limits() to replace generic_write_check_limits() call. This zonefs special helper makes sure that the maximum file limit used is the maximum size of the file being accessed. Since zonefs_write_checks() already truncates the iov_iter, the calls to iov_iter_truncate() in zonefs_file_dio_write() and zonefs_file_buffered_write() are removed. Fixes: 8dcc1a9 ("fs: New zonefs file system") Cc: <stable@vger.kernel.org> Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com> Signed-off-by: Damien Le Moal <damien.lemoal@wdc.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 38c74f2 commit dfbdbf0

1 file changed

Lines changed: 68 additions & 10 deletions

File tree

‎fs/zonefs/super.c‎

Lines changed: 68 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,68 @@ static ssize_t zonefs_file_dio_append(struct kiocb *iocb, struct iov_iter *from)
719719
return ret;
720720
}
721721

722+
/*
723+
* Do not exceed the LFS limits nor the file zone size. If pos is under the
724+
* limit it becomes a short access. If it exceeds the limit, return -EFBIG.
725+
*/
726+
static loff_t zonefs_write_check_limits(struct file *file, loff_t pos,
727+
loff_t count)
728+
{
729+
struct inode *inode = file_inode(file);
730+
struct zonefs_inode_info *zi = ZONEFS_I(inode);
731+
loff_t limit = rlimit(RLIMIT_FSIZE);
732+
loff_t max_size = zi->i_max_size;
733+
734+
if (limit != RLIM_INFINITY) {
735+
if (pos >= limit) {
736+
send_sig(SIGXFSZ, current, 0);
737+
return -EFBIG;
738+
}
739+
count = min(count, limit - pos);
740+
}
741+
742+
if (!(file->f_flags & O_LARGEFILE))
743+
max_size = min_t(loff_t, MAX_NON_LFS, max_size);
744+
745+
if (unlikely(pos >= max_size))
746+
return -EFBIG;
747+
748+
return min(count, max_size - pos);
749+
}
750+
751+
static ssize_t zonefs_write_checks(struct kiocb *iocb, struct iov_iter *from)
752+
{
753+
struct file *file = iocb->ki_filp;
754+
struct inode *inode = file_inode(file);
755+
struct zonefs_inode_info *zi = ZONEFS_I(inode);
756+
loff_t count;
757+
758+
if (IS_SWAPFILE(inode))
759+
return -ETXTBSY;
760+
761+
if (!iov_iter_count(from))
762+
return 0;
763+
764+
if ((iocb->ki_flags & IOCB_NOWAIT) && !(iocb->ki_flags & IOCB_DIRECT))
765+
return -EINVAL;
766+
767+
if (iocb->ki_flags & IOCB_APPEND) {
768+
if (zi->i_ztype != ZONEFS_ZTYPE_SEQ)
769+
return -EINVAL;
770+
mutex_lock(&zi->i_truncate_mutex);
771+
iocb->ki_pos = zi->i_wpoffset;
772+
mutex_unlock(&zi->i_truncate_mutex);
773+
}
774+
775+
count = zonefs_write_check_limits(file, iocb->ki_pos,
776+
iov_iter_count(from));
777+
if (count < 0)
778+
return count;
779+
780+
iov_iter_truncate(from, count);
781+
return iov_iter_count(from);
782+
}
783+
722784
/*
723785
* Handle direct writes. For sequential zone files, this is the only possible
724786
* write path. For these files, check that the user is issuing writes
@@ -736,8 +798,7 @@ static ssize_t zonefs_file_dio_write(struct kiocb *iocb, struct iov_iter *from)
736798
struct super_block *sb = inode->i_sb;
737799
bool sync = is_sync_kiocb(iocb);
738800
bool append = false;
739-
size_t count;
740-
ssize_t ret;
801+
ssize_t ret, count;
741802

742803
/*
743804
* For async direct IOs to sequential zone files, refuse IOCB_NOWAIT
@@ -755,12 +816,11 @@ static ssize_t zonefs_file_dio_write(struct kiocb *iocb, struct iov_iter *from)
755816
inode_lock(inode);
756817
}
757818

758-
ret = generic_write_checks(iocb, from);
759-
if (ret <= 0)
819+
count = zonefs_write_checks(iocb, from);
820+
if (count <= 0) {
821+
ret = count;
760822
goto inode_unlock;
761-
762-
iov_iter_truncate(from, zi->i_max_size - iocb->ki_pos);
763-
count = iov_iter_count(from);
823+
}
764824

765825
if ((iocb->ki_pos | count) & (sb->s_blocksize - 1)) {
766826
ret = -EINVAL;
@@ -820,12 +880,10 @@ static ssize_t zonefs_file_buffered_write(struct kiocb *iocb,
820880
inode_lock(inode);
821881
}
822882

823-
ret = generic_write_checks(iocb, from);
883+
ret = zonefs_write_checks(iocb, from);
824884
if (ret <= 0)
825885
goto inode_unlock;
826886

827-
iov_iter_truncate(from, zi->i_max_size - iocb->ki_pos);
828-
829887
ret = iomap_file_buffered_write(iocb, from, &zonefs_iomap_ops);
830888
if (ret > 0)
831889
iocb->ki_pos += ret;

0 commit comments

Comments
 (0)