Skip to content

Commit 73fa754

Browse files
Rich Felkerbrauner
authored andcommitted
vfs: add RWF_NOAPPEND flag for pwritev2
The pwrite function, originally defined by POSIX (thus the "p"), is defined to ignore O_APPEND and write at the offset passed as its argument. However, historically Linux honored O_APPEND if set and ignored the offset. This cannot be changed due to stability policy, but is documented in the man page as a bug. Now that there's a pwritev2 syscall providing a superset of the pwrite functionality that has a flags argument, the conforming behavior can be offered to userspace via a new flag. Since pwritev2 checks flag validity (in kiocb_set_rw_flags) and reports unknown ones with EOPNOTSUPP, callers will not get wrong behavior on old kernels that don't support the new flag; the error is reported and the caller can decide how to handle it. Signed-off-by: Rich Felker <dalias@libc.org> Link: https://lore.kernel.org/r/20200831153207.GO3265@brightrain.aerifal.cx Reviewed-by: Jann Horn <jannh@google.com> Signed-off-by: Christian Brauner <brauner@kernel.org>
1 parent 9e3f1c5 commit 73fa754

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

include/linux/fs.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3335,6 +3335,8 @@ static inline int kiocb_set_rw_flags(struct kiocb *ki, rwf_t flags)
33353335
return 0;
33363336
if (unlikely(flags & ~RWF_SUPPORTED))
33373337
return -EOPNOTSUPP;
3338+
if (unlikely((flags & RWF_APPEND) && (flags & RWF_NOAPPEND)))
3339+
return -EINVAL;
33383340

33393341
if (flags & RWF_NOWAIT) {
33403342
if (!(ki->ki_filp->f_mode & FMODE_NOWAIT))
@@ -3345,6 +3347,12 @@ static inline int kiocb_set_rw_flags(struct kiocb *ki, rwf_t flags)
33453347
if (flags & RWF_SYNC)
33463348
kiocb_flags |= IOCB_DSYNC;
33473349

3350+
if ((flags & RWF_NOAPPEND) && (ki->ki_flags & IOCB_APPEND)) {
3351+
if (IS_APPEND(file_inode(ki->ki_filp)))
3352+
return -EPERM;
3353+
ki->ki_flags &= ~IOCB_APPEND;
3354+
}
3355+
33483356
ki->ki_flags |= kiocb_flags;
33493357
return 0;
33503358
}

include/uapi/linux/fs.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,12 @@ typedef int __bitwise __kernel_rwf_t;
301301
/* per-IO O_APPEND */
302302
#define RWF_APPEND ((__force __kernel_rwf_t)0x00000010)
303303

304+
/* per-IO negation of O_APPEND */
305+
#define RWF_NOAPPEND ((__force __kernel_rwf_t)0x00000020)
306+
304307
/* mask of flags supported by the kernel */
305308
#define RWF_SUPPORTED (RWF_HIPRI | RWF_DSYNC | RWF_SYNC | RWF_NOWAIT |\
306-
RWF_APPEND)
309+
RWF_APPEND | RWF_NOAPPEND)
307310

308311
/* Pagemap ioctl */
309312
#define PAGEMAP_SCAN _IOWR('f', 16, struct pm_scan_arg)

0 commit comments

Comments
 (0)