From patchwork Fri Jan 26 15:57:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tony Solomonik X-Patchwork-Id: 13532864 Received: from mail-lf1-f54.google.com (mail-lf1-f54.google.com [209.85.167.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 282781CF90 for ; Fri, 26 Jan 2024 15:57:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706284649; cv=none; b=NnB9mA8niqRBqSg3WlrQnVEYD9YqtKHE6zKSYnkHaTgajYYHaitE7Z3DDXPLYNrLMMeCDOMEM9TPyAp+zO5gt4CjPYe3hMV0JuMMYna+m6dZPq0gldyf2I/ACiFklRC1vp6/xtQDZJXN+zrqmmUXa08VRPz2LBDticQI1hxRuGE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706284649; c=relaxed/simple; bh=0HunhnbcX9ZsuXcrYeri9pRIk8CN08ig1jG63LXX8cA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=X+4bRFQ/XqPI00GsZToz3GJekw/JgOhnv6QDA5DprBDBF/J/PkwOVhzZKndoX4ef+1KVmZRoIXQAcJQCaXK5GIZBkQAf8Zm2oVhHcX4QGswnkY+F96I9v8iTB4BQc6xW9evHfb/pd6iVJA311KGhhKVIUPsbb4FiOGVB0ek0otI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=SOcw1tme; arc=none smtp.client-ip=209.85.167.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="SOcw1tme" Received: by mail-lf1-f54.google.com with SMTP id 2adb3069b0e04-5101f2dfdadso1044629e87.2 for ; Fri, 26 Jan 2024 07:57:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706284646; x=1706889446; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=w6raLg7wD+sk36pQRjUXFDO6cKOlKJG0M4/2TT+m5hg=; b=SOcw1tmeizEK9FhjiYSKE9EK2WA55GbbjN+WVaC5ySGmljqCaMoGW9it6Nlk3jvcN4 kJr55sZ1Du4v+3zCrm/NaCBVPYMC8G2Sap2W35som9efNMxZ9GbQXn0C3qvM8/p7W4EO 4Agh9215rZvXdw+Bh4yFpznlYB8TNXtf6xGcKbRSUrTOlIlnLTwFbIZtQbCmO3JiIeQA iRLOGjusC6No5sbCsM8MLEuW1G8esoCEhYCnOX2ypw+lyeL/M8PbJL80gBSXAclPknRq EsnYL00Au6wDdrbu1OTCaykRR3DapIChAb9yiT94jOSSTTtdfrsE5ZkGNowuwXilH+sU bAKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706284646; x=1706889446; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=w6raLg7wD+sk36pQRjUXFDO6cKOlKJG0M4/2TT+m5hg=; b=Vpz3Pkr/CQLcoNcBQD+4T8xFT7X1pXHidr26C84cgb8XT6pvoiIxLToTXIbiw5Av3v VbWLLfw1uAKe9AHKT9ViRwH+V1VjxinCj2vOTcOr8itZJBWXA/edc7mFbgStg+2Nr+yq t3pmbBJ8Yo36z0pRRxXg86qH2alez9cQSSbReeOqsR292ST3PpLBPyxhxSpxA9pBTeQU u8BwigR5Z4FKkUPAtWXyOp3F840Xj9k90dHRM98+uJP6wyx4+Yo7zgsI/8s8eO2u5eDP q48trvai8Q+uvwHL/090nZ7WxV0kiSTiSdyQsbrOINTlFe3tneyTgOLMXmNO6GppZZnq E1ng== X-Gm-Message-State: AOJu0YzG/WOE6urr5vTMqhfPpVwwE5m1HpkmamadIacE2c/h1XJd39Iq HA3b7nWEOnB0L1G0jsxUPgzzmE9XE2E7nDD5L+Wb5s0hEXEM6xls X-Google-Smtp-Source: AGHT+IHtCIKW/8rRN2ks7tPh1m5WEKNzrV+kRkcGeOIPOG59QpbAROsERP5vqMJLDyFdcN7/sh/FCQ== X-Received: by 2002:a2e:a681:0:b0:2ce:fc69:47f2 with SMTP id q1-20020a2ea681000000b002cefc6947f2mr926568lje.39.1706284645910; Fri, 26 Jan 2024 07:57:25 -0800 (PST) Received: from localhost.localdomain ([147.235.201.119]) by smtp.gmail.com with ESMTPSA id f8-20020a2e6a08000000b002ccdb771df0sm188598ljc.108.2024.01.26.07.57.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jan 2024 07:57:25 -0800 (PST) From: Tony Solomonik To: Cc: willy@infradead.org, axboe@kernel.dk, linux-fsdevel@vger.kernel.org, brauner@kernel.org, Tony Solomonik Subject: [PATCH v7 1/2] Add do_ftruncate that truncates a struct file Date: Fri, 26 Jan 2024 17:57:19 +0200 Message-Id: <20240126155720.20385-2-tony.solomonik@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240126155720.20385-1-tony.solomonik@gmail.com> References: <20240124083301.8661-1-tony.solomonik@gmail.com> <20240126155720.20385-1-tony.solomonik@gmail.com> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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. Signed-off-by: Tony Solomonik --- fs/internal.h | 1 + fs/open.c | 53 +++++++++++++++++++++++++++------------------------ 2 files changed, 29 insertions(+), 25 deletions(-) diff --git a/fs/internal.h b/fs/internal.h index 58e43341aebf..d35b1c05cf6d 100644 --- a/fs/internal.h +++ b/fs/internal.h @@ -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, diff --git a/fs/open.c b/fs/open.c index 02dc608d40d8..9bbe8a73836b 100644 --- a/fs/open.c +++ b/fs/open.c @@ -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 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; - 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; - dentry = f.file->f_path.dentry; - 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; + + error = do_ftruncate(f.file, length); - error = -EPERM; - /* Check IS_APPEND on real upper inode */ - if (IS_APPEND(file_inode(f.file))) - goto out_putf; - sb_start_write(inode->i_sb); - error = security_file_truncate(f.file); - if (!error) - error = do_truncate(file_mnt_idmap(f.file), dentry, length, - ATTR_MTIME | ATTR_CTIME, f.file); - sb_end_write(inode->i_sb); -out_putf: fdput(f); -out: return error; } From patchwork Fri Jan 26 15:57:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tony Solomonik X-Patchwork-Id: 13532865 Received: from mail-lj1-f178.google.com (mail-lj1-f178.google.com [209.85.208.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 45A731D545 for ; Fri, 26 Jan 2024 15:57:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706284651; cv=none; b=mKSFj6Os/mjkpYgivxMsyz+u+VNHeEwk+EKWPIgAQgaOVkzS15HYDL/rdAHAZde85XGzlqMaeWxXaGg4a1O+2SsEQSDbPe88jPD8iJq6QPeSGlxOkKV9s5+AMMzQkrIoINvjec7cUxlZCHNnbA0mpf7T5WPz/+ss4E/7jxWJPIQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706284651; c=relaxed/simple; bh=fHwkyIdQT4SZ+pg11KW/vWNospg1pMcQWocpCSsy5Y8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=T49+CnFHenJQD4pYkRVt/dyIXNgYQwMR9vzhS97/VRG4vgraS1oyxJYC8ux8i0VDtbRBlqHYO70Jt76lKV9fRSeHUhR7KQUpmUh4Hu7qSfWFpmOtfD1NaN7JDXYDJCyrUCMh3JFlSwtJ0Qpj8R5l+n5tvlQB0E1j3p8i014LpjE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=QfsdxSGu; arc=none smtp.client-ip=209.85.208.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="QfsdxSGu" Received: by mail-lj1-f178.google.com with SMTP id 38308e7fff4ca-2cf1fd1cc5bso5855951fa.3 for ; Fri, 26 Jan 2024 07:57:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706284647; x=1706889447; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=wEoRLjZxsDql9htA9V4PRolq3lloZtO5EyOWZLDSizA=; b=QfsdxSGuMN42A/ncNNUlHNOPID7ELF2R5/BoD1FW6E11zv+rAqdO0jqUvK1hJU+O3j 9TKRGxmlpcwHZQTYD19Zq9euc96YG4JfetSgCzpHgXkTNGX5rAvwmaeOxLn5xqC608Bn KE/niFwE5xBYFokWX5l6V9rcS3ClADM5CxdsMbzoKZuGYZM5RA8D5e69UTWNWypRxUCD i+ZxNEUFLw8Xp19MJ+OzHyQKwMMR90W3XRHfGLbEDIbq+xrY5/sL6x0f4zp2Us710uDF pY5ijbKNEC5jp7xHYua0W4xMRhW90+Wna8P3VHoW5qGFgdhYnywIknxSJVLbMjVK92FH Kopw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706284647; x=1706889447; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=wEoRLjZxsDql9htA9V4PRolq3lloZtO5EyOWZLDSizA=; b=vUeYfAaM7zccoMxvJ6jAFQDnNmCOH3JyICXKT8DoFYUC2g431/tUPKyeuBm2XVy07M p5zToTkQa+jMe6hHGSonmCyk8iLa3z/ID4Jn0gIVKgrtp8rD5LHfTwupW0CXb377GhWq WSro4tL2AjyW+nRPoaXDA9qLDmQ1I69iqjD/GeioJesYK7sE3DX98J5A56H6j8q5zBWm fdvPVPaoOH0TcATmDBPSHrrmN8s82+XsB7DXkYdRJQScBJmaGT8RxRq20VqqxGPYodrn pHm8xljq8JD7UWitNa+Weroh0x0ppJQy3jYf9FOP7biiaWN2N14r5QwviBgprQePZfvy zpRQ== X-Gm-Message-State: AOJu0Yz9ei4kgOQPKU/zrFCV9BfIayq2eC5+6mFjp0cSegBLaUoRiKy7 Z9MC7xdI4AOMhmtlhsulv1ClKOaf/CeyoFgx4IWlCjqYdFL6wx+G X-Google-Smtp-Source: AGHT+IHiMn1bMLnNmyIOE99XayThFyvrspKHaof/6MnqK63aeBqCUPVxN9ifR/vIevC2GEoTy86Jmg== X-Received: by 2002:a2e:6a04:0:b0:2cf:1ae2:dcd with SMTP id f4-20020a2e6a04000000b002cf1ae20dcdmr1670744ljc.32.1706284647174; Fri, 26 Jan 2024 07:57:27 -0800 (PST) Received: from localhost.localdomain ([147.235.201.119]) by smtp.gmail.com with ESMTPSA id f8-20020a2e6a08000000b002ccdb771df0sm188598ljc.108.2024.01.26.07.57.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jan 2024 07:57:26 -0800 (PST) From: Tony Solomonik To: Cc: willy@infradead.org, axboe@kernel.dk, linux-fsdevel@vger.kernel.org, brauner@kernel.org, Tony Solomonik Subject: [PATCH v7 2/2] io_uring: add support for ftruncate Date: Fri, 26 Jan 2024 17:57:20 +0200 Message-Id: <20240126155720.20385-3-tony.solomonik@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240126155720.20385-1-tony.solomonik@gmail.com> References: <20240124083301.8661-1-tony.solomonik@gmail.com> <20240126155720.20385-1-tony.solomonik@gmail.com> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 --- 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 diff --git a/include/uapi/linux/io_uring.h b/include/uapi/linux/io_uring.h index f1c16f817742..be682e000c94 100644 --- a/include/uapi/linux/io_uring.h +++ b/include/uapi/linux/io_uring.h @@ -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, diff --git a/io_uring/Makefile b/io_uring/Makefile index e5be47e4fc3b..4f8ed6530a29 100644 --- a/io_uring/Makefile +++ b/io_uring/Makefile @@ -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 diff --git a/io_uring/opdef.c b/io_uring/opdef.c index 799db44283c7..7a83b76c6ee7 100644 --- a/io_uring/opdef.c +++ b/io_uring/opdef.c @@ -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) diff --git a/io_uring/truncate.c b/io_uring/truncate.c new file mode 100644 index 000000000000..9b0735890c62 --- /dev/null +++ b/io_uring/truncate.c @@ -0,0 +1,48 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "../fs/internal.h" + +#include "io_uring.h" +#include "truncate.h" + +struct io_ftrunc { + struct file *file; + loff_t len; +}; + +int io_ftruncate_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) +{ + struct io_ftrunc *ft = io_kiocb_to_cmd(req, struct io_ftrunc); + + if (sqe->rw_flags || sqe->addr || sqe->len || sqe->buf_index || + sqe->splice_fd_in || sqe->addr3) + return -EINVAL; + + ft->len = READ_ONCE(sqe->off); + + req->flags |= REQ_F_FORCE_ASYNC; + return 0; +} + +int io_ftruncate(struct io_kiocb *req, unsigned int issue_flags) +{ + struct io_ftrunc *ft = io_kiocb_to_cmd(req, struct io_ftrunc); + int ret; + + WARN_ON_ONCE(issue_flags & IO_URING_F_NONBLOCK); + + ret = do_ftruncate(req->file, ft->len); + + io_req_set_res(req, ret, 0); + return IOU_OK; +} diff --git a/io_uring/truncate.h b/io_uring/truncate.h new file mode 100644 index 000000000000..ec088293a478 --- /dev/null +++ b/io_uring/truncate.h @@ -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);