Skip to contentSkip to conversation

io_uring: add support for ftruncate

PATCH Superseded Tony Solomonik 6 participants 32 messages Jan 24, 2024 – Feb 9, 2024 2 messages not in the archive

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v5 cover io_uring: add support for ftruncate

This patch adds support for doing truncate through io_uring, eliminating the need for applications to roll their own thread pool or offload mechanism to be able to do non-blocking truncates.

Tony Solomonik (2):
Add ftruncate_file that truncates a struct file
io_uring: add support for ftruncate

 fs/internal.h                 |  1 +
 fs/open.c                     | 53 ++++++++++++++++++-----------------
 include/uapi/linux/io_uring.h |  1 +
 io_uring/Makefile             |  2 +-
 io_uring/opdef.c              | 10 +++++++
 io_uring/truncate.c           | 48 +++++++++++++++++++++++++++++++
 io_uring/truncate.h           |  4 +++
 7 files changed, 93 insertions(+), 26 deletions(-)
 create mode 100644 io_uring/truncate.c
 create mode 100644 io_uring/truncate.h

base-commit: d3fa86b1a7b4cdc4367acacea16b72e0a200b3d7

Signature · 1 line
2.34.1
#1 lore raw

PATCH v5 1/2 Add ftruncate_file that truncates a struct file

do_sys_ftruncate receives a file descriptor, fgets the struct file, and finally actually truncates the file.

ftruncate_file allows for passing in a file directly, with the caller already holding a reference to it.

---
 fs/internal.h |  1 +
 fs/open.c     | 53 +++++++++++++++++++++++++++------------------------
 2 files changed, 29 insertions(+), 25 deletions(-)
fs/internal.h +1 -0
index 58e43341aebf..78a641ebd16e 100644
@@ -182,6 +182,7 @@ extern struct open_how build_open_how(int flags, umode_t mode);
 extern int build_open_flags(const struct open_how *how, struct open_flags *op);
 extern struct file *__close_fd_get_file(unsigned int fd);
 
+long ftruncate_file(struct file *file, loff_t length, int small);
 long do_sys_ftruncate(unsigned int fd, loff_t length, int small);
 int chmod_common(const struct path *path, umode_t mode);
 int do_fchownat(int dfd, const char __user *filename, uid_t user, gid_t group,
fs/open.c +28 -25

77 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer Acked-by on v5 1/2
#2 lore raw

Re: [PATCH v5 1/2] Add ftruncate_file that truncates a struct file

Quoted 29 lines · Tony Solomonik

do_sys_ftruncate receives a file descriptor, fgets the struct file, and finally actually truncates the file.

ftruncate_file allows for passing in a file directly, with the caller already holding a reference to it.

---
 fs/internal.h |  1 +
 fs/open.c     | 53 +++++++++++++++++++++++++++------------------------
 2 files changed, 29 insertions(+), 25 deletions(-)
fs/internal.h +1 -0
index 58e43341aebf..78a641ebd16e 100644
@@ -182,6 +182,7 @@ extern struct open_how build_open_how(int flags, umode_t mode);
 extern int build_open_flags(const struct open_how *how, struct open_flags *op);
 extern struct file *__close_fd_get_file(unsigned int fd);
 
+long ftruncate_file(struct file *file, loff_t length, int small);
 long do_sys_ftruncate(unsigned int fd, loff_t length, int small);
 int chmod_common(const struct path *path, umode_t mode);
 int do_fchownat(int dfd, const char __user *filename, uid_t user, gid_t group,
fs/open.c +0 -0
index 02dc608d40d8..649d38eecfe4 100644

fs/open.c COMPAT_SYSCALL_DEFINE2(truncate, const char __user *, path, compat_off_t, length · line 157

Quoted 5 lines
fs/open.c +1 -1
@@ -154,49 +154,52 @@ COMPAT_SYSCALL_DEFINE2(truncate, const char __user *, path, compat_off_t, length
 }
 #endif
 
-long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
+long ftruncate_file(struct file *file, loff_t length, int small)

All internal functions that io_uring currently calls are called do_*():

* do_rmdir()
* do_unlinkat()
* do_mkdirat()
* do_symlinkat()
* do_statx()

So I'd follow that pattern and just call that thing do_ftruncate(). Otherwise looks good to me,

#3 lore raw

Re: [PATCH v5 1/2] Add ftruncate_file that truncates a struct file

In reply to Tony Solomonik
Quoted 5 lines · Tony Solomonik
fs/open.c +1 -1
 	/* explicitly opened as large or we are on 64-bit box */
-	if (f.file->f_flags & O_LARGEFILE)
+	if (file->f_flags & O_LARGEFILE)
 		small = 0;

[...]

fs/open.c COMPAT_SYSCALL_DEFINE2(truncate, const char __user *, path, compat_off_t, length · line 174

Quoted 4 lines
fs/open.c +1 -1
 	/* Cannot ftruncate over 2^31 bytes without large file support */
 	if (small && length > MAX_NON_LFS)
-		goto out_putf;
+		return -EINVAL;

I'd leave this in do_sys_truncate(). No need for ioring to care about this ancient problem.

#4 lore raw

PATCH v5 2/2 io_uring: add support for ftruncate

In reply to Tony Solomonik

Adds support for doing truncate through io_uring, eliminating the need for applications to roll their own thread pool or offload mechanism to be able to do non-blocking truncates.

---
 include/uapi/linux/io_uring.h |  1 +
 io_uring/Makefile             |  2 +-
 io_uring/opdef.c              | 10 ++++++++
 io_uring/truncate.c           | 48 +++++++++++++++++++++++++++++++++++
 io_uring/truncate.h           |  4 +++
 5 files changed, 64 insertions(+), 1 deletion(-)
 create mode 100644 io_uring/truncate.c
 create mode 100644 io_uring/truncate.h
include/uapi/linux/io_uring.h +1 -0
index f1c16f817742..be682e000c94 100644
@@ -253,6 +253,7 @@ enum io_uring_op {
 	IORING_OP_FUTEX_WAIT,
 	IORING_OP_FUTEX_WAKE,
 	IORING_OP_FUTEX_WAITV,
+	IORING_OP_FTRUNCATE,
 
 	/* this goes last, obviously */
 	IORING_OP_LAST,
io_uring/Makefile +1 -1
index e5be47e4fc3b..4f8ed6530a29 100644
@@ -8,6 +8,6 @@ obj-$(CONFIG_IO_URING)		+= io_uring.o xattr.o nop.o fs.o splice.o \
 					statx.o net.o msg_ring.o timeout.o \
 					sqpoll.o fdinfo.o tctx.o poll.o \
 					cancel.o kbuf.o rsrc.o rw.o opdef.o \
-					notif.o waitid.o
+					notif.o waitid.o truncate.o
 obj-$(CONFIG_IO_WQ)		+= io-wq.o
 obj-$(CONFIG_FUTEX)		+= futex.o
io_uring/opdef.c +10 -0
index 799db44283c7..7a83b76c6ee7 100644
@@ -35,6 +35,7 @@
 #include "rw.h"
 #include "waitid.h"
 #include "futex.h"
+#include "truncate.h"
 
 static int io_no_issue(struct io_kiocb *req, unsigned int issue_flags)
 {
@@ -469,6 +470,12 @@ const struct io_issue_def io_issue_defs[] = {
 		.prep			= io_eopnotsupp_prep,
 #endif
 	},
+	[IORING_OP_FTRUNCATE] = {
+		.needs_file		= 1,
+		.hash_reg_file		= 1,
+		.prep			= io_ftruncate_prep,
+		.issue			= io_ftruncate,
+	},
 };
 
 const struct io_cold_def io_cold_defs[] = {
@@ -704,6 +711,9 @@ const struct io_cold_def io_cold_defs[] = {
 	[IORING_OP_FUTEX_WAITV] = {
 		.name			= "FUTEX_WAITV",
 	},
+	[IORING_OP_FTRUNCATE] = {
+		.name			= "FTRUNCATE",
+	},
 };
 
 const char *io_uring_get_opcode(u8 opcode)
io_uring/truncate.c new file +48 -0

48 lines not on this page · raw message

io_uring/truncate.h new file +4 -0
new file mode 100644
index 000000000000..ec088293a478
@@ -0,0 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0
+
+int io_ftruncate_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
+int io_ftruncate(struct io_kiocb *req, unsigned int issue_flags);
Signature · 1 line
2.34.1
#5 lore raw
In reply to Tony Solomonik
Quoted 21 lines · Tony Solomonik

This patch adds support for doing truncate through io_uring, eliminating the need for applications to roll their own thread pool or offload mechanism to be able to do non-blocking truncates.

Tony Solomonik (2):
Add ftruncate_file that truncates a struct file
io_uring: add support for ftruncate

 fs/internal.h                 |  1 +
 fs/open.c                     | 53 ++++++++++++++++++-----------------
 include/uapi/linux/io_uring.h |  1 +
 io_uring/Makefile             |  2 +-
 io_uring/opdef.c              | 10 +++++++
 io_uring/truncate.c           | 48 +++++++++++++++++++++++++++++++
 io_uring/truncate.h           |  4 +++
 7 files changed, 93 insertions(+), 26 deletions(-)
 create mode 100644 io_uring/truncate.c
 create mode 100644 io_uring/truncate.h
Quoted 1 line

base-commit: d3fa86b1a7b4cdc4367acacea16b72e0a200b3d7

Also fallocate() to punch holes, aka sparse files, must be implemented

Ced

Signature · 3 lines
Cedric Blancher <[email protected]>
[https://plus.google.com/u/0/+CedricBlancher/]
Institute Pasteur
maintainer
#6 lore raw
Quoted 22 lines · Cedric Blancher
Quoted 21 lines · Tony Solomonik

This patch adds support for doing truncate through io_uring, eliminating the need for applications to roll their own thread pool or offload mechanism to be able to do non-blocking truncates.

Tony Solomonik (2):
Add ftruncate_file that truncates a struct file
io_uring: add support for ftruncate

 fs/internal.h                 |  1 +
 fs/open.c                     | 53 ++++++++++++++++++-----------------
 include/uapi/linux/io_uring.h |  1 +
 io_uring/Makefile             |  2 +-
 io_uring/opdef.c              | 10 +++++++
 io_uring/truncate.c           | 48 +++++++++++++++++++++++++++++++
 io_uring/truncate.h           |  4 +++
 7 files changed, 93 insertions(+), 26 deletions(-)
 create mode 100644 io_uring/truncate.c
 create mode 100644 io_uring/truncate.h
Quoted 3 lines
Quoted 1 line

base-commit: d3fa86b1a7b4cdc4367acacea16b72e0a200b3d7

Also fallocate() to punch holes, aka sparse files, must be implemented

fallocate has been supported for years.

Signature · 1 line
Jens Axboe
#7 lore raw
Quoted 24 lines · Jens Axboe
Quoted 22 lines · Cedric Blancher
Quoted 21 lines · Tony Solomonik

This patch adds support for doing truncate through io_uring, eliminating the need for applications to roll their own thread pool or offload mechanism to be able to do non-blocking truncates.

Tony Solomonik (2):
Add ftruncate_file that truncates a struct file
io_uring: add support for ftruncate

 fs/internal.h                 |  1 +
 fs/open.c                     | 53 ++++++++++++++++++-----------------
 include/uapi/linux/io_uring.h |  1 +
 io_uring/Makefile             |  2 +-
 io_uring/opdef.c              | 10 +++++++
 io_uring/truncate.c           | 48 +++++++++++++++++++++++++++++++
 io_uring/truncate.h           |  4 +++
 7 files changed, 93 insertions(+), 26 deletions(-)
 create mode 100644 io_uring/truncate.c
 create mode 100644 io_uring/truncate.h
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

base-commit: d3fa86b1a7b4cdc4367acacea16b72e0a200b3d7

Also fallocate() to punch holes, aka sparse files, must be implemented

fallocate has been supported for years.

Does it support punching holes? Does lseek() with SEEK_HOLE and SEEK_DATA work, with more than one hole, and/or hole at the end?

Ced

Signature · 3 lines
Cedric Blancher <[email protected]>
[https://plus.google.com/u/0/+CedricBlancher/]
Institute Pasteur
maintainer
#8 lore raw
Quoted 25 lines · Cedric Blancher
Quoted 24 lines · Jens Axboe
Quoted 22 lines · Cedric Blancher
Quoted 21 lines · Tony Solomonik

This patch adds support for doing truncate through io_uring, eliminating the need for applications to roll their own thread pool or offload mechanism to be able to do non-blocking truncates.

Tony Solomonik (2):
Add ftruncate_file that truncates a struct file
io_uring: add support for ftruncate

 fs/internal.h                 |  1 +
 fs/open.c                     | 53 ++++++++++++++++++-----------------
 include/uapi/linux/io_uring.h |  1 +
 io_uring/Makefile             |  2 +-
 io_uring/opdef.c              | 10 +++++++
 io_uring/truncate.c           | 48 +++++++++++++++++++++++++++++++
 io_uring/truncate.h           |  4 +++
 7 files changed, 93 insertions(+), 26 deletions(-)
 create mode 100644 io_uring/truncate.c
 create mode 100644 io_uring/truncate.h
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

base-commit: d3fa86b1a7b4cdc4367acacea16b72e0a200b3d7

Also fallocate() to punch holes, aka sparse files, must be implemented

fallocate has been supported for years.

Does it support punching holes? Does lseek() with SEEK_HOLE and SEEK_DATA work, with more than one hole, and/or hole at the end?

It does anything that fallocate(2) will do.

Signature · 1 line
Jens Axboe
#9 lore raw

v6 cover io_uring: add support for ftruncate

In reply to Tony Solomonik

This patch adds support for doing truncate through io_uring, eliminating the need for applications to roll their own thread pool or offload mechanism to be able to do non-blocking truncates.

Tony Solomonik (2):
Add do_ftruncate that truncates a struct file
io_uring: add support for ftruncate

 fs/internal.h                 |  1 +
 fs/open.c                     | 57 ++++++++++++++++++-----------------
 include/uapi/linux/io_uring.h |  1 +
 io_uring/Makefile             |  2 +-
 io_uring/opdef.c              | 10 ++++++
 io_uring/truncate.c           | 48 +++++++++++++++++++++++++++++
 io_uring/truncate.h           |  4 +++
 7 files changed, 95 insertions(+), 28 deletions(-)
 create mode 100644 io_uring/truncate.c
 create mode 100644 io_uring/truncate.h

base-commit: d3fa86b1a7b4cdc4367acacea16b72e0a200b3d7

Signature · 1 line
2.34.1
#10 lore raw

PATCH v6 1/2 Add do_ftruncate that truncates a struct file

do_sys_ftruncate receives a file descriptor, fgets the struct file, and finally actually truncates the file.

do_ftruncate allows for passing in a file directly, with the caller already holding a reference to it.

---
 fs/internal.h |  1 +
 fs/open.c     | 57 +++++++++++++++++++++++++++------------------------
 2 files changed, 31 insertions(+), 27 deletions(-)
fs/internal.h +1 -0
index 58e43341aebf..bb3df26a9a13 100644
@@ -182,6 +182,7 @@ extern struct open_how build_open_how(int flags, umode_t mode);
 extern int build_open_flags(const struct open_how *how, struct open_flags *op);
 extern struct file *__close_fd_get_file(unsigned int fd);
 
+long do_ftruncate(struct file *file, loff_t length, int small);
 long do_sys_ftruncate(unsigned int fd, loff_t length, int small);
 int chmod_common(const struct path *path, umode_t mode);
 int do_fchownat(int dfd, const char __user *filename, uid_t user, gid_t group,
fs/open.c +30 -27

79 lines not on this page · raw message

Signature · 1 line
2.34.1
#11 lore raw

Re: [PATCH v6 1/2] Add do_ftruncate that truncates a struct file

Quoted 1 line · Tony Solomonik

+long do_ftruncate(struct file *file, loff_t length, int small);

Does my suggestion to drop the 'small' parameter from do_ftruncate not work?

#12 lore raw

PATCH v6 2/2 io_uring: add support for ftruncate

In reply to Tony Solomonik

Adds support for doing truncate through io_uring, eliminating the need for applications to roll their own thread pool or offload mechanism to be able to do non-blocking truncates.

---
 include/uapi/linux/io_uring.h |  1 +
 io_uring/Makefile             |  2 +-
 io_uring/opdef.c              | 10 ++++++++
 io_uring/truncate.c           | 48 +++++++++++++++++++++++++++++++++++
 io_uring/truncate.h           |  4 +++
 5 files changed, 64 insertions(+), 1 deletion(-)
 create mode 100644 io_uring/truncate.c
 create mode 100644 io_uring/truncate.h
include/uapi/linux/io_uring.h +1 -0
index f1c16f817742..be682e000c94 100644
@@ -253,6 +253,7 @@ enum io_uring_op {
 	IORING_OP_FUTEX_WAIT,
 	IORING_OP_FUTEX_WAKE,
 	IORING_OP_FUTEX_WAITV,
+	IORING_OP_FTRUNCATE,
 
 	/* this goes last, obviously */
 	IORING_OP_LAST,
io_uring/Makefile +1 -1
index e5be47e4fc3b..4f8ed6530a29 100644
@@ -8,6 +8,6 @@ obj-$(CONFIG_IO_URING)		+= io_uring.o xattr.o nop.o fs.o splice.o \
 					statx.o net.o msg_ring.o timeout.o \
 					sqpoll.o fdinfo.o tctx.o poll.o \
 					cancel.o kbuf.o rsrc.o rw.o opdef.o \
-					notif.o waitid.o
+					notif.o waitid.o truncate.o
 obj-$(CONFIG_IO_WQ)		+= io-wq.o
 obj-$(CONFIG_FUTEX)		+= futex.o
io_uring/opdef.c +10 -0
index 799db44283c7..7a83b76c6ee7 100644
@@ -35,6 +35,7 @@
 #include "rw.h"
 #include "waitid.h"
 #include "futex.h"
+#include "truncate.h"
 
 static int io_no_issue(struct io_kiocb *req, unsigned int issue_flags)
 {
@@ -469,6 +470,12 @@ const struct io_issue_def io_issue_defs[] = {
 		.prep			= io_eopnotsupp_prep,
 #endif
 	},
+	[IORING_OP_FTRUNCATE] = {
+		.needs_file		= 1,
+		.hash_reg_file		= 1,
+		.prep			= io_ftruncate_prep,
+		.issue			= io_ftruncate,
+	},
 };
 
 const struct io_cold_def io_cold_defs[] = {
@@ -704,6 +711,9 @@ const struct io_cold_def io_cold_defs[] = {
 	[IORING_OP_FUTEX_WAITV] = {
 		.name			= "FUTEX_WAITV",
 	},
+	[IORING_OP_FTRUNCATE] = {
+		.name			= "FTRUNCATE",
+	},
 };
 
 const char *io_uring_get_opcode(u8 opcode)
io_uring/truncate.c new file +48 -0

48 lines not on this page · raw message

io_uring/truncate.h new file +4 -0
new file mode 100644
index 000000000000..ec088293a478
@@ -0,0 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0
+
+int io_ftruncate_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
+int io_ftruncate(struct io_kiocb *req, unsigned int issue_flags);
Signature · 1 line
2.34.1
#13 lore raw

v7 cover io_uring: add support for ftruncate

In reply to Tony Solomonik

This patch adds support for doing truncate through io_uring, eliminating the need for applications to roll their own thread pool or offload mechanism to be able to do non-blocking truncates.

Tony Solomonik (2):
Add do_ftruncate that truncates a struct file
io_uring: add support for ftruncate

 fs/internal.h                 |  1 +
 fs/open.c                     | 53 ++++++++++++++++++-----------------
 include/uapi/linux/io_uring.h |  1 +
 io_uring/Makefile             |  2 +-
 io_uring/opdef.c              | 10 +++++++
 io_uring/truncate.c           | 48 +++++++++++++++++++++++++++++++
 io_uring/truncate.h           |  4 +++
 7 files changed, 93 insertions(+), 26 deletions(-)
 create mode 100644 io_uring/truncate.c
 create mode 100644 io_uring/truncate.h

base-commit: d3fa86b1a7b4cdc4367acacea16b72e0a200b3d7

Signature · 1 line
2.34.1
#14 lore raw

PATCH v7 1/2 Add do_ftruncate that truncates a struct file

do_sys_ftruncate receives a file descriptor, fgets the struct file, and finally actually truncates the file.

do_ftruncate allows for passing in a file directly, with the caller already holding a reference to it.

---
 fs/internal.h |  1 +
 fs/open.c     | 53 +++++++++++++++++++++++++++------------------------
 2 files changed, 29 insertions(+), 25 deletions(-)
fs/internal.h +1 -0
index 58e43341aebf..d35b1c05cf6d 100644
@@ -182,6 +182,7 @@ extern struct open_how build_open_how(int flags, umode_t mode);
 extern int build_open_flags(const struct open_how *how, struct open_flags *op);
 extern struct file *__close_fd_get_file(unsigned int fd);
 
+long do_ftruncate(struct file *file, loff_t length);
 long do_sys_ftruncate(unsigned int fd, loff_t length, int small);
 int chmod_common(const struct path *path, umode_t mode);
 int do_fchownat(int dfd, const char __user *filename, uid_t user, gid_t group,
fs/open.c +28 -25

77 lines not on this page · raw message

Signature · 1 line
2.34.1
#15 lore raw

Re: [PATCH v7 1/2] Add do_ftruncate that truncates a struct file

kernel test robot noticed the following build warnings:

Hi Tony,

kernel test robot noticed the following build warnings:

[auto build test WARNING on d3fa86b1a7b4cdc4367acacea16b72e0a200b3d7]

url:    https://github.com/intel-lab-lkp/linux/commits/Tony-Solomonik/Add-do_ftruncate-that-truncates-a-struct-file/20240126-235914
base:   d3fa86b1a7b4cdc4367acacea16b72e0a200b3d7
patch link:    https://lore.kernel.org/r/20240126155720.20385-2-tony.solomonik%40gmail.com
patch subject: [PATCH v7 1/2] Add do_ftruncate that truncates a struct file
config: i386-randconfig-141-20240129 (https://download.01.org/0day-ci/archive/20240129/[email protected]/config)
compiler: gcc-11 (Debian 11.3.0-12) 11.3.0

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

smatch warnings:
fs/open.c:178 do_ftruncate() warn: inconsistent indenting

vim +178 fs/open.c

156
157 long do_ftruncate(struct file *file, loff_t length)
158 {
159 struct inode *inode;
160 struct dentry *dentry;
161 int error;
162
163 dentry = file->f_path.dentry;
164 inode = dentry->d_inode;
165 if (!S_ISREG(inode->i_mode) || !(file->f_mode & FMODE_WRITE))
166 return -EINVAL;
167
168 /* Check IS_APPEND on real upper inode */
169 if (IS_APPEND(file_inode(file)))
170 return -EPERM;
171 sb_start_write(inode->i_sb);
172 error = security_file_truncate(file);
173 if (!error)
174 error = do_truncate(file_mnt_idmap(file), dentry, length,
175 ATTR_MTIME | ATTR_CTIME, file);
176 sb_end_write(inode->i_sb);
177

 > 178	  return error;

179 }
180

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#16 lore raw

Re: [PATCH v7 1/2] Add do_ftruncate that truncates a struct file

In reply to Tony Solomonik
Quoted 10 lines · Tony Solomonik
fs/open.c +1 -2
 	f = fdget(fd);
 	if (!f.file)
-		goto out;
+		return -EBADF;
 
 	/* explicitly opened as large or we are on 64-bit box */
 	if (f.file->f_flags & O_LARGEFILE)
 		small = 0;
 
-	dentry = f.file->f_path.dentry;

fs/open.c COMPAT_SYSCALL_DEFINE2(truncate, const char __user *, path, compat_off_t, length · line 198

Quoted 10 lines
fs/open.c +1 -7
-	inode = dentry->d_inode;
-	error = -EINVAL;
-	if (!S_ISREG(inode->i_mode) || !(f.file->f_mode & FMODE_WRITE))
-		goto out_putf;
-
-	error = -EINVAL;
 	/* Cannot ftruncate over 2^31 bytes without large file support */
 	if (small && length > MAX_NON_LFS)
-		goto out_putf;
+		return -EINVAL;

I think this is wrong -- you need to putf after this point..

#17 lore raw

PATCH v7 2/2 io_uring: add support for ftruncate

In reply to Tony Solomonik

Adds support for doing truncate through io_uring, eliminating the need for applications to roll their own thread pool or offload mechanism to be able to do non-blocking truncates.

---
 include/uapi/linux/io_uring.h |  1 +
 io_uring/Makefile             |  2 +-
 io_uring/opdef.c              | 10 ++++++++
 io_uring/truncate.c           | 48 +++++++++++++++++++++++++++++++++++
 io_uring/truncate.h           |  4 +++
 5 files changed, 64 insertions(+), 1 deletion(-)
 create mode 100644 io_uring/truncate.c
 create mode 100644 io_uring/truncate.h
include/uapi/linux/io_uring.h +1 -0
index f1c16f817742..be682e000c94 100644
@@ -253,6 +253,7 @@ enum io_uring_op {
 	IORING_OP_FUTEX_WAIT,
 	IORING_OP_FUTEX_WAKE,
 	IORING_OP_FUTEX_WAITV,
+	IORING_OP_FTRUNCATE,
 
 	/* this goes last, obviously */
 	IORING_OP_LAST,
io_uring/Makefile +1 -1
index e5be47e4fc3b..4f8ed6530a29 100644
@@ -8,6 +8,6 @@ obj-$(CONFIG_IO_URING)		+= io_uring.o xattr.o nop.o fs.o splice.o \
 					statx.o net.o msg_ring.o timeout.o \
 					sqpoll.o fdinfo.o tctx.o poll.o \
 					cancel.o kbuf.o rsrc.o rw.o opdef.o \
-					notif.o waitid.o
+					notif.o waitid.o truncate.o
 obj-$(CONFIG_IO_WQ)		+= io-wq.o
 obj-$(CONFIG_FUTEX)		+= futex.o
io_uring/opdef.c +10 -0
index 799db44283c7..7a83b76c6ee7 100644
@@ -35,6 +35,7 @@
 #include "rw.h"
 #include "waitid.h"
 #include "futex.h"
+#include "truncate.h"
 
 static int io_no_issue(struct io_kiocb *req, unsigned int issue_flags)
 {
@@ -469,6 +470,12 @@ const struct io_issue_def io_issue_defs[] = {
 		.prep			= io_eopnotsupp_prep,
 #endif
 	},
+	[IORING_OP_FTRUNCATE] = {
+		.needs_file		= 1,
+		.hash_reg_file		= 1,
+		.prep			= io_ftruncate_prep,
+		.issue			= io_ftruncate,
+	},
 };
 
 const struct io_cold_def io_cold_defs[] = {
@@ -704,6 +711,9 @@ const struct io_cold_def io_cold_defs[] = {
 	[IORING_OP_FUTEX_WAITV] = {
 		.name			= "FUTEX_WAITV",
 	},
+	[IORING_OP_FTRUNCATE] = {
+		.name			= "FTRUNCATE",
+	},
 };
 
 const char *io_uring_get_opcode(u8 opcode)
io_uring/truncate.c new file +48 -0

48 lines not on this page · raw message

io_uring/truncate.h new file +4 -0
new file mode 100644
index 000000000000..ec088293a478
@@ -0,0 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0
+
+int io_ftruncate_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
+int io_ftruncate(struct io_kiocb *req, unsigned int issue_flags);
Signature · 1 line
2.34.1
#18 lore raw

v8 cover io_uring: add support for ftruncate

In reply to Tony Solomonik

This patch adds support for doing truncate through io_uring, eliminating the need for applications to roll their own thread pool or offload mechanism to be able to do non-blocking truncates.

Tony Solomonik (2):
Add do_ftruncate that truncates a struct file
io_uring: add support for ftruncate

 fs/internal.h                 |  1 +
 fs/open.c                     | 52 +++++++++++++++++++----------------
 include/uapi/linux/io_uring.h |  1 +
 io_uring/Makefile             |  2 +-
 io_uring/opdef.c              | 10 +++++++
 io_uring/truncate.c           | 48 ++++++++++++++++++++++++++++++++
 io_uring/truncate.h           |  4 +++
 7 files changed, 94 insertions(+), 24 deletions(-)
 create mode 100644 io_uring/truncate.c
 create mode 100644 io_uring/truncate.h

base-commit: d3fa86b1a7b4cdc4367acacea16b72e0a200b3d7

Signature · 1 line
2.34.1
#19 lore raw

PATCH v8 1/2 Add do_ftruncate that truncates a struct file

do_sys_ftruncate receives a file descriptor, fgets the struct file, and finally actually truncates the file.

do_ftruncate allows for passing in a file directly, with the caller already holding a reference to it.

---
 fs/internal.h |  1 +
 fs/open.c     | 52 ++++++++++++++++++++++++++++-----------------------
 2 files changed, 30 insertions(+), 23 deletions(-)
fs/internal.h +1 -0
index 58e43341aebf..d35b1c05cf6d 100644
@@ -182,6 +182,7 @@ extern struct open_how build_open_how(int flags, umode_t mode);
 extern int build_open_flags(const struct open_how *how, struct open_flags *op);
 extern struct file *__close_fd_get_file(unsigned int fd);
 
+long do_ftruncate(struct file *file, loff_t length);
 long do_sys_ftruncate(unsigned int fd, loff_t length, int small);
 int chmod_common(const struct path *path, umode_t mode);
 int do_fchownat(int dfd, const char __user *filename, uid_t user, gid_t group,
fs/open.c +29 -23

78 lines not on this page · raw message

Signature · 1 line
2.34.1
#21 lore raw

Re: [PATCH v8 1/2] Add do_ftruncate that truncates a struct file

In reply to Tony Solomonik
Quoted 65 lines · Tony Solomonik

do_sys_ftruncate receives a file descriptor, fgets the struct file, and finally actually truncates the file.

do_ftruncate allows for passing in a file directly, with the caller already holding a reference to it.

---
 fs/internal.h |  1 +
 fs/open.c     | 52 ++++++++++++++++++++++++++++-----------------------
 2 files changed, 30 insertions(+), 23 deletions(-)
fs/internal.h +1 -0
index 58e43341aebf..d35b1c05cf6d 100644
@@ -182,6 +182,7 @@ extern struct open_how build_open_how(int flags, umode_t mode);
 extern int build_open_flags(const struct open_how *how, struct open_flags *op);
 extern struct file *__close_fd_get_file(unsigned int fd);
 
+long do_ftruncate(struct file *file, loff_t length);
 long do_sys_ftruncate(unsigned int fd, loff_t length, int small);
 int chmod_common(const struct path *path, umode_t mode);
 int do_fchownat(int dfd, const char __user *filename, uid_t user, gid_t group,
fs/open.c +23 -3
index 02dc608d40d8..6d608ff4a3f7 100644
@@ -154,49 +154,55 @@ COMPAT_SYSCALL_DEFINE2(truncate, const char __user *, path, compat_off_t, length
 }
 #endif
 
-long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
+long do_ftruncate(struct file *file, loff_t length)
 {
 	struct inode *inode;
 	struct dentry *dentry;
+	int error;
+
+	dentry = file->f_path.dentry;
+	inode = dentry->d_inode;
+	if (!S_ISREG(inode->i_mode) || !(file->f_mode & FMODE_WRITE))
+		return -EINVAL;
+
+	/* Check IS_APPEND on real upper inode */
+	if (IS_APPEND(file_inode(file)))
+		return -EPERM;
+	sb_start_write(inode->i_sb);
+	error = security_file_truncate(file);
+	if (!error)
+		error = do_truncate(file_mnt_idmap(file), dentry, length,
+				    ATTR_MTIME | ATTR_CTIME, file);
+	sb_end_write(inode->i_sb);
+
+	return error;
+}
+
+long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
+{
 	struct fd f;
 	int error;
 
-	error = -EINVAL;
 	if (length < 0)
-		goto out;

fs/open.c COMPAT_SYSCALL_DEFINE2(truncate, const char __user *, path, compat_off_t, length · line 194

Quoted 10 lines
fs/open.c +2 -2
-	error = -EBADF;
+		return -EINVAL;
 	f = fdget(fd);
 	if (!f.file)
-		goto out;
+		return -EBADF;
 
 	/* explicitly opened as large or we are on 64-bit box */
 	if (f.file->f_flags & O_LARGEFILE)
 		small = 0;

Why is the O_LARGEFILE handling not needed when used from io_uring?

A message that is not in the archive (sent off-list or not yet received): CAD62OrETm04q5F7ef8fpB5xF_vTKEHfas5W86QEssZ2ozyg0DQ@mail.gmail.com

maintainer
#23 lore raw

Re: [PATCH v8 1/2] Add do_ftruncate that truncates a struct file

Quoted 5 lines · Tony Solomonik

Actually, I'm not quite sure anymore, @Jens Axboe <mailto:[email protected]> is there any guarantee in io_uring that the file is always opened as LARGE / 64 bit? From looking at the code, it simply accepts a user made fd, so the user might have not opened it as LARGE on a 32bit system, which might be bad news.

Yeah, we probably want to retain that. Though it'd be a very odd case where an application using io_uring isn't opening "large" files by default, but we'd still have to ensure that it is.

Signature · 1 line
Jens Axboe
#24 lore raw

Re: [PATCH v8 1/2] Add do_ftruncate that truncates a struct file

Quoted 10 lines · Jens Axboe
Quoted 5 lines · Tony Solomonik

Actually, I'm not quite sure anymore, @Jens Axboe <mailto:[email protected]> is there any guarantee in io_uring that the file is always opened as LARGE / 64 bit? From looking at the code, it simply accepts a user made fd, so the user might have not opened it as LARGE on a 32bit system, which might be bad news.

Yeah, we probably want to retain that. Though it'd be a very odd case where an application using io_uring isn't opening "large" files by default, but we'd still have to ensure that it is.

Oh; my mistake. I thought we required O_LARGEFILE when using io_uring.

#25 lore raw

PATCH v8 2/2 io_uring: add support for ftruncate

In reply to Tony Solomonik

Adds support for doing truncate through io_uring, eliminating the need for applications to roll their own thread pool or offload mechanism to be able to do non-blocking truncates.

---
 include/uapi/linux/io_uring.h |  1 +
 io_uring/Makefile             |  2 +-
 io_uring/opdef.c              | 10 ++++++++
 io_uring/truncate.c           | 48 +++++++++++++++++++++++++++++++++++
 io_uring/truncate.h           |  4 +++
 5 files changed, 64 insertions(+), 1 deletion(-)
 create mode 100644 io_uring/truncate.c
 create mode 100644 io_uring/truncate.h
include/uapi/linux/io_uring.h +1 -0
index f1c16f817742..be682e000c94 100644
@@ -253,6 +253,7 @@ enum io_uring_op {
 	IORING_OP_FUTEX_WAIT,
 	IORING_OP_FUTEX_WAKE,
 	IORING_OP_FUTEX_WAITV,
+	IORING_OP_FTRUNCATE,
 
 	/* this goes last, obviously */
 	IORING_OP_LAST,
io_uring/Makefile +1 -1
index e5be47e4fc3b..4f8ed6530a29 100644
@@ -8,6 +8,6 @@ obj-$(CONFIG_IO_URING)		+= io_uring.o xattr.o nop.o fs.o splice.o \
 					statx.o net.o msg_ring.o timeout.o \
 					sqpoll.o fdinfo.o tctx.o poll.o \
 					cancel.o kbuf.o rsrc.o rw.o opdef.o \
-					notif.o waitid.o
+					notif.o waitid.o truncate.o
 obj-$(CONFIG_IO_WQ)		+= io-wq.o
 obj-$(CONFIG_FUTEX)		+= futex.o
io_uring/opdef.c +10 -0
index 799db44283c7..7a83b76c6ee7 100644
@@ -35,6 +35,7 @@
 #include "rw.h"
 #include "waitid.h"
 #include "futex.h"
+#include "truncate.h"
 
 static int io_no_issue(struct io_kiocb *req, unsigned int issue_flags)
 {
@@ -469,6 +470,12 @@ const struct io_issue_def io_issue_defs[] = {
 		.prep			= io_eopnotsupp_prep,
 #endif
 	},
+	[IORING_OP_FTRUNCATE] = {
+		.needs_file		= 1,
+		.hash_reg_file		= 1,
+		.prep			= io_ftruncate_prep,
+		.issue			= io_ftruncate,
+	},
 };
 
 const struct io_cold_def io_cold_defs[] = {
@@ -704,6 +711,9 @@ const struct io_cold_def io_cold_defs[] = {
 	[IORING_OP_FUTEX_WAITV] = {
 		.name			= "FUTEX_WAITV",
 	},
+	[IORING_OP_FTRUNCATE] = {
+		.name			= "FTRUNCATE",
+	},
 };
 
 const char *io_uring_get_opcode(u8 opcode)
io_uring/truncate.c new file +48 -0

48 lines not on this page · raw message

io_uring/truncate.h new file +4 -0
new file mode 100644
index 000000000000..ec088293a478
@@ -0,0 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0
+
+int io_ftruncate_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
+int io_ftruncate(struct io_kiocb *req, unsigned int issue_flags);
Signature · 1 line
2.34.1
#26 lore raw

v9 cover io_uring: add support for ftruncate

In reply to Tony Solomonik

This patch adds support for doing truncate through io_uring, eliminating the need for applications to roll their own thread pool or offload mechanism to be able to do non-blocking truncates.

Tony Solomonik (2):
Add do_ftruncate that truncates a struct file
io_uring: add support for ftruncate

 fs/internal.h                 |  1 +
 fs/open.c                     | 53 ++++++++++++++++++-----------------
 include/uapi/linux/io_uring.h |  1 +
 io_uring/Makefile             |  2 +-
 io_uring/opdef.c              | 10 +++++++
 io_uring/truncate.c           | 48 +++++++++++++++++++++++++++++++
 io_uring/truncate.h           |  4 +++
 7 files changed, 93 insertions(+), 26 deletions(-)
 create mode 100644 io_uring/truncate.c
 create mode 100644 io_uring/truncate.h

base-commit: d3fa86b1a7b4cdc4367acacea16b72e0a200b3d7

Signature · 1 line
2.34.1
#27 lore raw

PATCH v9 1/2 Add do_ftruncate that truncates a struct file

do_sys_ftruncate receives a file descriptor, fgets the struct file, and finally actually truncates the file.

do_ftruncate allows for passing in a file directly, with the caller already holding a reference to it.

---
 fs/internal.h |  1 +
 fs/open.c     | 53 +++++++++++++++++++++++++++------------------------
 2 files changed, 29 insertions(+), 25 deletions(-)
fs/internal.h +1 -0
index 58e43341aebf..bb3df26a9a13 100644
@@ -182,6 +182,7 @@ extern struct open_how build_open_how(int flags, umode_t mode);
 extern int build_open_flags(const struct open_how *how, struct open_flags *op);
 extern struct file *__close_fd_get_file(unsigned int fd);
 
+long do_ftruncate(struct file *file, loff_t length, int small);
 long do_sys_ftruncate(unsigned int fd, loff_t length, int small);
 int chmod_common(const struct path *path, umode_t mode);
 int do_fchownat(int dfd, const char __user *filename, uid_t user, gid_t group,
fs/open.c +28 -25

77 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer
#28 lore raw

Re: [PATCH v9 1/2] Add do_ftruncate that truncates a struct file

Quoted 5 lines · Tony Solomonik

do_sys_ftruncate receives a file descriptor, fgets the struct file, and finally actually truncates the file.

do_ftruncate allows for passing in a file directly, with the caller already holding a reference to it.

Christian, this looking good to you now?

Signature · 1 line
Jens Axboe
maintainer Reviewed-by
#29 lore raw

Re: [PATCH v9 1/2] Add do_ftruncate that truncates a struct file

Quoted 8 lines · Jens Axboe
Quoted 5 lines · Tony Solomonik

do_sys_ftruncate receives a file descriptor, fgets the struct file, and finally actually truncates the file.

do_ftruncate allows for passing in a file directly, with the caller already holding a reference to it.

Christian, this looking good to you now?

Seems good to me,

#30 lore raw

PATCH v9 2/2 io_uring: add support for ftruncate

In reply to Tony Solomonik

Adds support for doing truncate through io_uring, eliminating the need for applications to roll their own thread pool or offload mechanism to be able to do non-blocking truncates.

---
 include/uapi/linux/io_uring.h |  1 +
 io_uring/Makefile             |  2 +-
 io_uring/opdef.c              | 10 ++++++++
 io_uring/truncate.c           | 48 +++++++++++++++++++++++++++++++++++
 io_uring/truncate.h           |  4 +++
 5 files changed, 64 insertions(+), 1 deletion(-)
 create mode 100644 io_uring/truncate.c
 create mode 100644 io_uring/truncate.h
include/uapi/linux/io_uring.h +1 -0
index f1c16f817742..be682e000c94 100644
@@ -253,6 +253,7 @@ enum io_uring_op {
 	IORING_OP_FUTEX_WAIT,
 	IORING_OP_FUTEX_WAKE,
 	IORING_OP_FUTEX_WAITV,
+	IORING_OP_FTRUNCATE,
 
 	/* this goes last, obviously */
 	IORING_OP_LAST,
io_uring/Makefile +1 -1
index e5be47e4fc3b..4f8ed6530a29 100644
@@ -8,6 +8,6 @@ obj-$(CONFIG_IO_URING)		+= io_uring.o xattr.o nop.o fs.o splice.o \
 					statx.o net.o msg_ring.o timeout.o \
 					sqpoll.o fdinfo.o tctx.o poll.o \
 					cancel.o kbuf.o rsrc.o rw.o opdef.o \
-					notif.o waitid.o
+					notif.o waitid.o truncate.o
 obj-$(CONFIG_IO_WQ)		+= io-wq.o
 obj-$(CONFIG_FUTEX)		+= futex.o
io_uring/opdef.c +10 -0
index 799db44283c7..7a83b76c6ee7 100644
@@ -35,6 +35,7 @@
 #include "rw.h"
 #include "waitid.h"
 #include "futex.h"
+#include "truncate.h"
 
 static int io_no_issue(struct io_kiocb *req, unsigned int issue_flags)
 {
@@ -469,6 +470,12 @@ const struct io_issue_def io_issue_defs[] = {
 		.prep			= io_eopnotsupp_prep,
 #endif
 	},
+	[IORING_OP_FTRUNCATE] = {
+		.needs_file		= 1,
+		.hash_reg_file		= 1,
+		.prep			= io_ftruncate_prep,
+		.issue			= io_ftruncate,
+	},
 };
 
 const struct io_cold_def io_cold_defs[] = {
@@ -704,6 +711,9 @@ const struct io_cold_def io_cold_defs[] = {
 	[IORING_OP_FUTEX_WAITV] = {
 		.name			= "FUTEX_WAITV",
 	},
+	[IORING_OP_FTRUNCATE] = {
+		.name			= "FTRUNCATE",
+	},
 };
 
 const char *io_uring_get_opcode(u8 opcode)
io_uring/truncate.c new file +48 -0

48 lines not on this page · raw message

io_uring/truncate.h new file +4 -0
new file mode 100644
index 000000000000..ec088293a478
@@ -0,0 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0
+
+int io_ftruncate_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
+int io_ftruncate(struct io_kiocb *req, unsigned int issue_flags);
Signature · 1 line
2.34.1
maintainer Applied
#31 lore raw
In reply to Tony Solomonik
Quoted 9 lines · Tony Solomonik

This patch adds support for doing truncate through io_uring, eliminating the need for applications to roll their own thread pool or offload mechanism to be able to do non-blocking truncates.

Tony Solomonik (2):
Add do_ftruncate that truncates a struct file
io_uring: add support for ftruncate

[...]

Applied, thanks!

[1/2] Add do_ftruncate that truncates a struct file
(no commit info)
[2/2] io_uring: add support for ftruncate
(no commit info)

Best regards,

Signature · 1 line
Jens Axboe

A message that is not in the archive (sent off-list or not yet received): CAD62OrGiBX5YuKr_qRzCXPR5Cx_0Vw3Dei9f95Qww1rL45ejdA@mail.gmail.com

maintainer
#33 lore raw
Quoted 1 line · Tony Solomonik

🎉

Just a note that your base was older than the current one. Hence the opcode value for truncate shifted one down, FIXED_FD_INSTALL is in there now. So had to hand-apply both patches, but nothing major. I'll get the liburing side sorted too.

Thanks for working through this, and congrats on landing the patches :-)

Signature · 1 line
Jens Axboe