From patchwork Mon Jan 29 15:15:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tony Solomonik X-Patchwork-Id: 13535856 Received: from mail-wm1-f41.google.com (mail-wm1-f41.google.com [209.85.128.41]) (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 C55D71534FC for ; Mon, 29 Jan 2024 15:15:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706541318; cv=none; b=A9couu0hudPFiapWa8Elr3VmW/Anjr9WlcNOR/3O/jmr9NhS8oixIOONIqOGff1bAz5msP3xD6leDSkZ48htC3dwrntIY3/v3+l5PKTU9d6IZ6x8C53y83S/FRLhqF5UemEsc4jD9kPzh5mEsMUSKgHtRCg0SvDCsRxX7TXSagE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706541318; c=relaxed/simple; bh=U7p7DBiwt36LOyfZjNcbX/+PeKY1AyT03L1jkN/LZPI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Z3QNM7M70yCNUoS0Rx/UjHz5wBduhq6rFy2o2a/ZJy7R8shHmbUYucemKzMmVYqce4MHStPJRnDPJN4dvIMP6VBFL+jThRhVSylpHnizGe2t3LwGmngO85Ma5nTOVLv9V7yRSDszSwyxkD1Kz7/GPA4+/BR7ORfSDW8iSmivdS0= 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=TpYvwfbO; arc=none smtp.client-ip=209.85.128.41 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="TpYvwfbO" Received: by mail-wm1-f41.google.com with SMTP id 5b1f17b1804b1-40e775695c6so25297265e9.3 for ; Mon, 29 Jan 2024 07:15:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706541315; x=1707146115; 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=v71HBMYaLKl569swmb2xVX3+a05lKxuFejdDN7gWubU=; b=TpYvwfbOHas0dMlSIajpszsamQ/PvtzM4JfloUhOEvcLV2+QLx7zn02OpM/6i5gxOy kiBBItF78qBg4ZiKX/xxMRrU5AlrjDXRyF+otmzmSFC8E/iZs0rW9fRqQiJaldDJT0Et R5EkM5dp/wP/sHCYNgeoYKD3hAQRuTyX6vr2EYi33jJxQIFeVrJpCb5QcqVZTGYHZF89 knUMC1GlbP8nhkY0RN/gLlTt2IB0hXwxisUOpY8Pqx3kaAdlhv7IEiDL8tK3uqK0zATV rzI5GsvPKfU0YJ9MaaWnvwYGkMW+pfP/qtwFgLEG1Meyp8OGA8y9ql97XA8XzXBb19dL Y33Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706541315; x=1707146115; 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=v71HBMYaLKl569swmb2xVX3+a05lKxuFejdDN7gWubU=; b=WnHF5DhQx11oXFzr95jXelCg5DnUEyEcC3WLiZYPxI7IXsgni3EyXerTbQ+D1lSRkN 94FMidNH2XMhsd64WFMxfjKzZfrDrDxy465pE35i7ZlCO20lrlxWNkYFqTD8odGfyOCA DGlDM27727OAmfzVTZdftlIspwa8rWqNX0l8VpywZoUgIjXyx4OfxqWtZBK7NibGsmAq tVQG8rgQmyMh3NnNFUT/whqaKII6CSMJD3QnMJ9i9WmJJxSKfIAOTHiygjU1j9bBOLFS 3MkGW9mvsPDdhPmS4WdOFi0kX4DJmstbIpHDpnTWftuY+ClESyibPA7X/iilpbcWB0Lv ecuA== X-Gm-Message-State: AOJu0YxebGc0xTj2Zmn4RVzEdjGAvL4BjIfH4Oqi5CE5rqOdok5v+hlw +t21zDETeU4pzNQzr8wq4tbxu1wZuCG01BpnFOQiw9unIbFO7mrc X-Google-Smtp-Source: AGHT+IFHBi0is3gZyF09Tsk9tKmWCYjEVQ/EtrVffTvXjS7+432bJ8oZP/Bri9eYQKfItyFEmYh5Og== X-Received: by 2002:adf:f18d:0:b0:33a:eae4:aaf7 with SMTP id h13-20020adff18d000000b0033aeae4aaf7mr3344009wro.48.1706541314990; Mon, 29 Jan 2024 07:15:14 -0800 (PST) Received: from localhost.localdomain ([147.235.201.119]) by smtp.gmail.com with ESMTPSA id r6-20020adfca86000000b0033aed7423e8sm3060353wrh.11.2024.01.29.07.15.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Jan 2024 07:15:14 -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 v8 1/2] Add do_ftruncate that truncates a struct file Date: Mon, 29 Jan 2024 17:15:06 +0200 Message-Id: <20240129151507.14885-2-tony.solomonik@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240129151507.14885-1-tony.solomonik@gmail.com> References: <20240124083301.8661-1-tony.solomonik@gmail.com> <20240129151507.14885-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 Reviewed-by: Matthew Wilcox (Oracle) --- fs/internal.h | 1 + fs/open.c | 52 ++++++++++++++++++++++++++++----------------------- 2 files changed, 30 insertions(+), 23 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..6d608ff4a3f7 100644 --- a/fs/open.c +++ b/fs/open.c @@ -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; - 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; + goto out; + + 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: + fdput(f); return error; } From patchwork Mon Jan 29 15:15:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tony Solomonik X-Patchwork-Id: 13535857 Received: from mail-wr1-f46.google.com (mail-wr1-f46.google.com [209.85.221.46]) (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 D76131534F9 for ; Mon, 29 Jan 2024 15:15:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706541319; cv=none; b=KG1TVRDldxBi/ljXV8sMviqlInysp0Mi3mGNST1lKQe1qAZXPgb1HoSXeUZooFcMtOnup4Ykihpg7YMaVZ0MJrgD4AY8s0QNxJily9yevxv/rbvvfTealjVOlors3X1zfHvDnF/Ph8cPT8qLoQDPNcFUomi6qy1J5mSyEk3MUOY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706541319; c=relaxed/simple; bh=fHwkyIdQT4SZ+pg11KW/vWNospg1pMcQWocpCSsy5Y8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=bDillibeferWU1Y7Jnz3PeVmWq8MDElW+TqEhZ2JGN090Eca3kkmU2k/qxacbBxil8lV39aYOlcciCDBZocqJrW6AMw23cRpGTSrG9R8xl1iUy6kw3938m5bR82y2MuDIZTGWgMPyK6yA2oghyAEmAi6959gOUqgqaj0aPQrMkg= 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=Ic5fyQPz; arc=none smtp.client-ip=209.85.221.46 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="Ic5fyQPz" Received: by mail-wr1-f46.google.com with SMTP id ffacd0b85a97d-33ae7ae1c32so1098050f8f.3 for ; Mon, 29 Jan 2024 07:15:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706541316; x=1707146116; 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=Ic5fyQPztDzULBE+Iv+WsMK/e78uZU4XvKe1HyOPCmMDyoHyzjP5Lg1muu0MgR4wbq kzvFNEOu9AfQQwo+fCNJYAc34K2mCBc2WHH0BpSKnxlwHv9u/uZuOVfZIdu4ukE0T+xq ku1SyfOZwuf+ucle8MCj/6yt98zkBuQ4Qt4etuDR2FvczTU532GzNcr3B74tZum5Y/4T kPOeSbLvNl64ejJWN1WU0bGm9tuxtHTe+MnOLgxL76T7gcGNRr3euUFqvPmQnv8PMeU9 FKFZPaHIUEBCcV5iBRYAFGZV2B6nDwFpBGn/mESF+7QunYuOGcsuseSdr4etyWyyhQ4F 1t9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706541316; x=1707146116; 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=Mv8dZkGzzWYCTuXan6c2qbOKugw9o1N23MhTIFZrKJYH48toIJZCNbycO3eBh0y/Dg N4RbOixRO+NKT8dNfF7kDkzwrbsAMRop1JzmATmUldAbVEjIlQWmlr1romw0AvYbfzbz upuC783O1Vb3z1Nr/urwxWJ7fW9z+eoJVi/Gfn72kVSF11gBYOT1nMYr1u6VyOrhLPBb NXq2FEDSabW9Zy0jyBVkng1zOFOzi6jZ1Fl5AZAi8GU0vFqeoF4FSiihVSDoe7P4bkQ2 FvFGL/nVN2R7XvI1tBT3mHZLiVpPK9W17Lt8GXZsAAT9HWZakZW1rAVI7dt4qL4y0mr9 S4yA== X-Gm-Message-State: AOJu0YwP2LElmbH7eVD6sEmiUfliK7h/PltJrI1CAdDrZtPgl0uQ3g6g h+Mye+roJRJHm6KgOLzedcSd7bRZGUJHo91OZKRR/TUG9XeuYQ+J X-Google-Smtp-Source: AGHT+IH/ld69boNVXVsZEbaoms5trL7VtC5JuV7+1mHP4+uKk8OxTg8VsnK4LY4JEvlrXCwMxrcidw== X-Received: by 2002:a05:6000:1d9c:b0:33a:eb7c:2bbb with SMTP id bk28-20020a0560001d9c00b0033aeb7c2bbbmr3340206wrb.11.1706541316072; Mon, 29 Jan 2024 07:15:16 -0800 (PST) Received: from localhost.localdomain ([147.235.201.119]) by smtp.gmail.com with ESMTPSA id r6-20020adfca86000000b0033aed7423e8sm3060353wrh.11.2024.01.29.07.15.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Jan 2024 07:15:15 -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 v8 2/2] io_uring: add support for ftruncate Date: Mon, 29 Jan 2024 17:15:07 +0200 Message-Id: <20240129151507.14885-3-tony.solomonik@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240129151507.14885-1-tony.solomonik@gmail.com> References: <20240124083301.8661-1-tony.solomonik@gmail.com> <20240129151507.14885-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);