Skip to content

Commit b4bb190

Browse files
tontintonaxboe
authored andcommitted
io_uring: add support for ftruncate
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. Signed-off-by: Tony Solomonik <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent 5f0d594 commit b4bb190

File tree

5 files changed

+64
-1
lines changed

5 files changed

+64
-1
lines changed

include/uapi/linux/io_uring.h

+1
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ enum io_uring_op {
255255
IORING_OP_FUTEX_WAKE,
256256
IORING_OP_FUTEX_WAITV,
257257
IORING_OP_FIXED_FD_INSTALL,
258+
IORING_OP_FTRUNCATE,
258259

259260
/* this goes last, obviously */
260261
IORING_OP_LAST,

io_uring/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ obj-$(CONFIG_IO_URING) += io_uring.o xattr.o nop.o fs.o splice.o \
88
statx.o net.o msg_ring.o timeout.o \
99
sqpoll.o fdinfo.o tctx.o poll.o \
1010
cancel.o kbuf.o rsrc.o rw.o opdef.o \
11-
notif.o waitid.o register.o
11+
notif.o waitid.o register.o truncate.o
1212
obj-$(CONFIG_IO_WQ) += io-wq.o
1313
obj-$(CONFIG_FUTEX) += futex.o

io_uring/opdef.c

+10
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "rw.h"
3636
#include "waitid.h"
3737
#include "futex.h"
38+
#include "truncate.h"
3839

3940
static int io_no_issue(struct io_kiocb *req, unsigned int issue_flags)
4041
{
@@ -474,6 +475,12 @@ const struct io_issue_def io_issue_defs[] = {
474475
.prep = io_install_fixed_fd_prep,
475476
.issue = io_install_fixed_fd,
476477
},
478+
[IORING_OP_FTRUNCATE] = {
479+
.needs_file = 1,
480+
.hash_reg_file = 1,
481+
.prep = io_ftruncate_prep,
482+
.issue = io_ftruncate,
483+
},
477484
};
478485

479486
const struct io_cold_def io_cold_defs[] = {
@@ -712,6 +719,9 @@ const struct io_cold_def io_cold_defs[] = {
712719
[IORING_OP_FIXED_FD_INSTALL] = {
713720
.name = "FIXED_FD_INSTALL",
714721
},
722+
[IORING_OP_FTRUNCATE] = {
723+
.name = "FTRUNCATE",
724+
},
715725
};
716726

717727
const char *io_uring_get_opcode(u8 opcode)

io_uring/truncate.c

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include <linux/kernel.h>
3+
#include <linux/errno.h>
4+
#include <linux/fs.h>
5+
#include <linux/file.h>
6+
#include <linux/mm.h>
7+
#include <linux/slab.h>
8+
#include <linux/syscalls.h>
9+
#include <linux/io_uring.h>
10+
11+
#include <uapi/linux/io_uring.h>
12+
13+
#include "../fs/internal.h"
14+
15+
#include "io_uring.h"
16+
#include "truncate.h"
17+
18+
struct io_ftrunc {
19+
struct file *file;
20+
loff_t len;
21+
};
22+
23+
int io_ftruncate_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
24+
{
25+
struct io_ftrunc *ft = io_kiocb_to_cmd(req, struct io_ftrunc);
26+
27+
if (sqe->rw_flags || sqe->addr || sqe->len || sqe->buf_index ||
28+
sqe->splice_fd_in || sqe->addr3)
29+
return -EINVAL;
30+
31+
ft->len = READ_ONCE(sqe->off);
32+
33+
req->flags |= REQ_F_FORCE_ASYNC;
34+
return 0;
35+
}
36+
37+
int io_ftruncate(struct io_kiocb *req, unsigned int issue_flags)
38+
{
39+
struct io_ftrunc *ft = io_kiocb_to_cmd(req, struct io_ftrunc);
40+
int ret;
41+
42+
WARN_ON_ONCE(issue_flags & IO_URING_F_NONBLOCK);
43+
44+
ret = do_ftruncate(req->file, ft->len, 1);
45+
46+
io_req_set_res(req, ret, 0);
47+
return IOU_OK;
48+
}

io_uring/truncate.h

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
int io_ftruncate_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
4+
int io_ftruncate(struct io_kiocb *req, unsigned int issue_flags);

0 commit comments

Comments
 (0)