From patchwork Wed Jul 26 10:25:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hao Xu X-Patchwork-Id: 13327802 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 77625C0015E for ; Wed, 26 Jul 2023 10:26:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233801AbjGZK0X (ORCPT ); Wed, 26 Jul 2023 06:26:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53846 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233803AbjGZK0U (ORCPT ); Wed, 26 Jul 2023 06:26:20 -0400 Received: from out-3.mta1.migadu.com (out-3.mta1.migadu.com [95.215.58.3]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 484692129 for ; Wed, 26 Jul 2023 03:26:18 -0700 (PDT) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1690367177; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=5sGF3aDp5Ab6VJPIZgqoGrsmM2RUgLWSdrVdkpi2z8c=; b=xcrply0XV2HDyJsf7BcCe6bc9g/ddY1PH2ZmusoHEDQPxefX63K1P+9Tj0UN0hQij7rnz8 cdGMIIM/s1NT7GLPngIqMjtLXWsb3vtqurFwjVh1T4zyqSCKl2W6O9xjKmTYE5Ds+NLjn9 buPEIspqWwmb6CZmEVsFLk81/CHu2Zc= From: Hao Xu To: io-uring@vger.kernel.org, Jens Axboe Cc: Dominique Martinet , Pavel Begunkov , Christian Brauner , Alexander Viro , Stefan Roesch , Clay Harris , Dave Chinner , "Darrick J . Wong" , linux-fsdevel@vger.kernel.org, linux-xfs@vger.kernel.org, linux-ext4@vger.kernel.org, Wanpeng Li Subject: [PATCH 1/7] iomap: merge iomap_seek_hole() and iomap_seek_data() Date: Wed, 26 Jul 2023 18:25:57 +0800 Message-Id: <20230726102603.155522-2-hao.xu@linux.dev> In-Reply-To: <20230726102603.155522-1-hao.xu@linux.dev> References: <20230726102603.155522-1-hao.xu@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT Precedence: bulk List-ID: X-Mailing-List: io-uring@vger.kernel.org From: Hao Xu The two functions share almost same code, merge them together. No functional change in this patch. Signed-off-by: Hao Xu --- fs/ext4/file.c | 9 ++------- fs/gfs2/inode.c | 4 ++-- fs/iomap/seek.c | 40 ++++++++-------------------------------- fs/xfs/xfs_file.c | 5 ++--- include/linux/iomap.h | 6 ++---- 5 files changed, 16 insertions(+), 48 deletions(-) diff --git a/fs/ext4/file.c b/fs/ext4/file.c index c457c8517f0f..3d59993bce56 100644 --- a/fs/ext4/file.c +++ b/fs/ext4/file.c @@ -936,15 +936,10 @@ loff_t ext4_llseek(struct file *file, loff_t offset, int whence) return generic_file_llseek_size(file, offset, whence, maxbytes, i_size_read(inode)); case SEEK_HOLE: - inode_lock_shared(inode); - offset = iomap_seek_hole(inode, offset, - &ext4_iomap_report_ops); - inode_unlock_shared(inode); - break; case SEEK_DATA: inode_lock_shared(inode); - offset = iomap_seek_data(inode, offset, - &ext4_iomap_report_ops); + offset = iomap_seek(inode, offset, &ext4_iomap_report_ops, + whence == SEEK_HOLE); inode_unlock_shared(inode); break; } diff --git a/fs/gfs2/inode.c b/fs/gfs2/inode.c index 17c994a0c0d0..628f9d014491 100644 --- a/fs/gfs2/inode.c +++ b/fs/gfs2/inode.c @@ -2111,7 +2111,7 @@ loff_t gfs2_seek_data(struct file *file, loff_t offset) inode_lock_shared(inode); ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh); if (!ret) - ret = iomap_seek_data(inode, offset, &gfs2_iomap_ops); + ret = iomap_seek(inode, offset, &gfs2_iomap_ops, false); gfs2_glock_dq_uninit(&gh); inode_unlock_shared(inode); @@ -2130,7 +2130,7 @@ loff_t gfs2_seek_hole(struct file *file, loff_t offset) inode_lock_shared(inode); ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh); if (!ret) - ret = iomap_seek_hole(inode, offset, &gfs2_iomap_ops); + ret = iomap_seek(inode, offset, &gfs2_iomap_ops, true); gfs2_glock_dq_uninit(&gh); inode_unlock_shared(inode); diff --git a/fs/iomap/seek.c b/fs/iomap/seek.c index a845c012b50c..5e8641c5f0da 100644 --- a/fs/iomap/seek.c +++ b/fs/iomap/seek.c @@ -30,32 +30,6 @@ static loff_t iomap_seek_hole_iter(const struct iomap_iter *iter, } } -loff_t -iomap_seek_hole(struct inode *inode, loff_t pos, const struct iomap_ops *ops) -{ - loff_t size = i_size_read(inode); - struct iomap_iter iter = { - .inode = inode, - .pos = pos, - .flags = IOMAP_REPORT, - }; - int ret; - - /* Nothing to be found before or beyond the end of the file. */ - if (pos < 0 || pos >= size) - return -ENXIO; - - iter.len = size - pos; - while ((ret = iomap_iter(&iter, ops)) > 0) - iter.processed = iomap_seek_hole_iter(&iter, &pos); - if (ret < 0) - return ret; - if (iter.len) /* found hole before EOF */ - return pos; - return size; -} -EXPORT_SYMBOL_GPL(iomap_seek_hole); - static loff_t iomap_seek_data_iter(const struct iomap_iter *iter, loff_t *hole_pos) { @@ -77,7 +51,8 @@ static loff_t iomap_seek_data_iter(const struct iomap_iter *iter, } loff_t -iomap_seek_data(struct inode *inode, loff_t pos, const struct iomap_ops *ops) +iomap_seek(struct inode *inode, loff_t pos, const struct iomap_ops *ops, + bool hole) { loff_t size = i_size_read(inode); struct iomap_iter iter = { @@ -93,12 +68,13 @@ iomap_seek_data(struct inode *inode, loff_t pos, const struct iomap_ops *ops) iter.len = size - pos; while ((ret = iomap_iter(&iter, ops)) > 0) - iter.processed = iomap_seek_data_iter(&iter, &pos); + iter.processed = hole ? iomap_seek_hole_iter(&iter, &pos) : + iomap_seek_data_iter(&iter, &pos); if (ret < 0) return ret; - if (iter.len) /* found data before EOF */ + if (iter.len) /* found hole/data before EOF */ return pos; - /* We've reached the end of the file without finding data */ - return -ENXIO; + /* We've reached the end of the file without finding hole/data */ + return hole ? size : -ENXIO; } -EXPORT_SYMBOL_GPL(iomap_seek_data); +EXPORT_SYMBOL_GPL(iomap_seek); diff --git a/fs/xfs/xfs_file.c b/fs/xfs/xfs_file.c index 4f502219ae4f..d7d37f8fb6bc 100644 --- a/fs/xfs/xfs_file.c +++ b/fs/xfs/xfs_file.c @@ -1271,10 +1271,9 @@ xfs_file_llseek( default: return generic_file_llseek(file, offset, whence); case SEEK_HOLE: - offset = iomap_seek_hole(inode, offset, &xfs_seek_iomap_ops); - break; case SEEK_DATA: - offset = iomap_seek_data(inode, offset, &xfs_seek_iomap_ops); + offset = iomap_seek(inode, offset, &xfs_seek_iomap_ops, + whence == SEEK_HOLE); break; } diff --git a/include/linux/iomap.h b/include/linux/iomap.h index e2b836c2e119..22d5f9b19a22 100644 --- a/include/linux/iomap.h +++ b/include/linux/iomap.h @@ -274,10 +274,8 @@ vm_fault_t iomap_page_mkwrite(struct vm_fault *vmf, const struct iomap_ops *ops); int iomap_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, u64 start, u64 len, const struct iomap_ops *ops); -loff_t iomap_seek_hole(struct inode *inode, loff_t offset, - const struct iomap_ops *ops); -loff_t iomap_seek_data(struct inode *inode, loff_t offset, - const struct iomap_ops *ops); +loff_t iomap_seek(struct inode *inode, loff_t offset, + const struct iomap_ops *ops, bool hole); sector_t iomap_bmap(struct address_space *mapping, sector_t bno, const struct iomap_ops *ops); From patchwork Wed Jul 26 10:25:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hao Xu X-Patchwork-Id: 13327803 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 85095C0015E for ; Wed, 26 Jul 2023 10:26:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233821AbjGZK0b (ORCPT ); Wed, 26 Jul 2023 06:26:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53876 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233812AbjGZK00 (ORCPT ); Wed, 26 Jul 2023 06:26:26 -0400 Received: from out-21.mta1.migadu.com (out-21.mta1.migadu.com [IPv6:2001:41d0:203:375::15]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 15F81E7 for ; Wed, 26 Jul 2023 03:26:24 -0700 (PDT) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1690367182; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=t0lbx0okQPpnQcTB5T0YEuJud2LpJ8hVugIPpSWDV7w=; b=OKa1Ghl3A+ogwl4hhQ2WP9H85sDGeelCB9uN/0woiPW2QDvC0n/UU+xX3ZCc/i++HBvMFi bYy/udIPlmg0hmu2H0RZAT3RvuKNrOVZUqAT9aUbE3aC9BJnMhjgtF4Ezi1urVgNun+wGg h1ajFNXPk7IAGlZa+Fh+WOWOxn7qvzk= From: Hao Xu To: io-uring@vger.kernel.org, Jens Axboe Cc: Dominique Martinet , Pavel Begunkov , Christian Brauner , Alexander Viro , Stefan Roesch , Clay Harris , Dave Chinner , "Darrick J . Wong" , linux-fsdevel@vger.kernel.org, linux-xfs@vger.kernel.org, linux-ext4@vger.kernel.org, Wanpeng Li Subject: [PATCH 2/7] xfs: add nowait support for xfs_seek_iomap_begin() Date: Wed, 26 Jul 2023 18:25:58 +0800 Message-Id: <20230726102603.155522-3-hao.xu@linux.dev> In-Reply-To: <20230726102603.155522-1-hao.xu@linux.dev> References: <20230726102603.155522-1-hao.xu@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT Precedence: bulk List-ID: X-Mailing-List: io-uring@vger.kernel.org From: Hao Xu To support nowait llseek(), IOMAP_NOWAIT semantics should be respected. In xfs, xfs_seek_iomap_begin() is the only place which may be blocked by ilock and extent loading. Let's turn it into trylock logic just like what we've done in xfs_readdir(). Signed-off-by: Hao Xu --- fs/xfs/xfs_iomap.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/fs/xfs/xfs_iomap.c b/fs/xfs/xfs_iomap.c index 18c8f168b153..bbd7c6b27701 100644 --- a/fs/xfs/xfs_iomap.c +++ b/fs/xfs/xfs_iomap.c @@ -1294,7 +1294,9 @@ xfs_seek_iomap_begin( if (xfs_is_shutdown(mp)) return -EIO; - lockmode = xfs_ilock_data_map_shared(ip); + lockmode = xfs_ilock_data_map_shared_generic(ip, flags & IOMAP_NOWAIT); + if (!lockmode) + return -EAGAIN; error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK); if (error) goto out_unlock; From patchwork Wed Jul 26 10:25:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hao Xu X-Patchwork-Id: 13327804 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EEAD9C001DE for ; Wed, 26 Jul 2023 10:26:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233839AbjGZK0h (ORCPT ); Wed, 26 Jul 2023 06:26:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53962 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233815AbjGZK0c (ORCPT ); Wed, 26 Jul 2023 06:26:32 -0400 Received: from out-23.mta1.migadu.com (out-23.mta1.migadu.com [95.215.58.23]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 87F182683 for ; Wed, 26 Jul 2023 03:26:30 -0700 (PDT) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1690367188; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=XG23bwIETsfnqj7ej3G1Pf4FjZCp41aRmz5oYSwWIns=; b=psDaZVP3FSX50i9dl1HeTai8ASW5mklXthDlZcFCReJ1g80/nsQphphuQ91+icR5gMOESL vuV7A/r0N87q5CwVUvJFtmCY2rlHrvyu4UHp77lN4e/Vma/g8iBeU95bPIOShs865DRJPh Uav4hF6uSWQvrtJWZJFwiQ9xkYjh2Uc= From: Hao Xu To: io-uring@vger.kernel.org, Jens Axboe Cc: Dominique Martinet , Pavel Begunkov , Christian Brauner , Alexander Viro , Stefan Roesch , Clay Harris , Dave Chinner , "Darrick J . Wong" , linux-fsdevel@vger.kernel.org, linux-xfs@vger.kernel.org, linux-ext4@vger.kernel.org, Wanpeng Li Subject: [PATCH 3/7] add nowait parameter for iomap_seek() Date: Wed, 26 Jul 2023 18:25:59 +0800 Message-Id: <20230726102603.155522-4-hao.xu@linux.dev> In-Reply-To: <20230726102603.155522-1-hao.xu@linux.dev> References: <20230726102603.155522-1-hao.xu@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT Precedence: bulk List-ID: X-Mailing-List: io-uring@vger.kernel.org From: Hao Xu Add a nowait parameter for iomap_seek(), later IOMAP_NOWAIT is set according to this parameter's value. Signed-off-by: Hao Xu --- fs/ext4/file.c | 2 +- fs/gfs2/inode.c | 4 ++-- fs/iomap/seek.c | 4 +++- fs/xfs/xfs_file.c | 2 +- include/linux/iomap.h | 2 +- 5 files changed, 8 insertions(+), 6 deletions(-) diff --git a/fs/ext4/file.c b/fs/ext4/file.c index 3d59993bce56..c6c38c34148b 100644 --- a/fs/ext4/file.c +++ b/fs/ext4/file.c @@ -939,7 +939,7 @@ loff_t ext4_llseek(struct file *file, loff_t offset, int whence) case SEEK_DATA: inode_lock_shared(inode); offset = iomap_seek(inode, offset, &ext4_iomap_report_ops, - whence == SEEK_HOLE); + whence == SEEK_HOLE, false); inode_unlock_shared(inode); break; } diff --git a/fs/gfs2/inode.c b/fs/gfs2/inode.c index 628f9d014491..5d6e7471cb07 100644 --- a/fs/gfs2/inode.c +++ b/fs/gfs2/inode.c @@ -2111,7 +2111,7 @@ loff_t gfs2_seek_data(struct file *file, loff_t offset) inode_lock_shared(inode); ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh); if (!ret) - ret = iomap_seek(inode, offset, &gfs2_iomap_ops, false); + ret = iomap_seek(inode, offset, &gfs2_iomap_ops, false, false); gfs2_glock_dq_uninit(&gh); inode_unlock_shared(inode); @@ -2130,7 +2130,7 @@ loff_t gfs2_seek_hole(struct file *file, loff_t offset) inode_lock_shared(inode); ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh); if (!ret) - ret = iomap_seek(inode, offset, &gfs2_iomap_ops, true); + ret = iomap_seek(inode, offset, &gfs2_iomap_ops, true, false); gfs2_glock_dq_uninit(&gh); inode_unlock_shared(inode); diff --git a/fs/iomap/seek.c b/fs/iomap/seek.c index 5e8641c5f0da..319ea19fa90d 100644 --- a/fs/iomap/seek.c +++ b/fs/iomap/seek.c @@ -52,7 +52,7 @@ static loff_t iomap_seek_data_iter(const struct iomap_iter *iter, loff_t iomap_seek(struct inode *inode, loff_t pos, const struct iomap_ops *ops, - bool hole) + bool hole, bool nowait) { loff_t size = i_size_read(inode); struct iomap_iter iter = { @@ -66,6 +66,8 @@ iomap_seek(struct inode *inode, loff_t pos, const struct iomap_ops *ops, if (pos < 0 || pos >= size) return -ENXIO; + if (nowait) + iter.flags |= IOMAP_NOWAIT; iter.len = size - pos; while ((ret = iomap_iter(&iter, ops)) > 0) iter.processed = hole ? iomap_seek_hole_iter(&iter, &pos) : diff --git a/fs/xfs/xfs_file.c b/fs/xfs/xfs_file.c index d7d37f8fb6bc..73adc0aee2ff 100644 --- a/fs/xfs/xfs_file.c +++ b/fs/xfs/xfs_file.c @@ -1273,7 +1273,7 @@ xfs_file_llseek( case SEEK_HOLE: case SEEK_DATA: offset = iomap_seek(inode, offset, &xfs_seek_iomap_ops, - whence == SEEK_HOLE); + whence == SEEK_HOLE, nowait); break; } diff --git a/include/linux/iomap.h b/include/linux/iomap.h index 22d5f9b19a22..f99769d4fc42 100644 --- a/include/linux/iomap.h +++ b/include/linux/iomap.h @@ -275,7 +275,7 @@ vm_fault_t iomap_page_mkwrite(struct vm_fault *vmf, int iomap_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, u64 start, u64 len, const struct iomap_ops *ops); loff_t iomap_seek(struct inode *inode, loff_t offset, - const struct iomap_ops *ops, bool hole); + const struct iomap_ops *ops, bool hole, bool nowait); sector_t iomap_bmap(struct address_space *mapping, sector_t bno, const struct iomap_ops *ops); From patchwork Wed Jul 26 10:26:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hao Xu X-Patchwork-Id: 13327805 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A944BC41513 for ; Wed, 26 Jul 2023 10:26:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233873AbjGZK0z (ORCPT ); Wed, 26 Jul 2023 06:26:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53960 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233855AbjGZK0k (ORCPT ); Wed, 26 Jul 2023 06:26:40 -0400 Received: from out-5.mta1.migadu.com (out-5.mta1.migadu.com [95.215.58.5]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AA45D269A for ; Wed, 26 Jul 2023 03:26:36 -0700 (PDT) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1690367194; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Lwy1bya8EhY/7tZ1lMXE+4zXJK9aZUCgnxudXlZKm68=; b=ux551U6nhVvd09Fj1FVNymeGKcLT6zQsEBqhk225f+PUyMSHGeN5cY/do/fviplQGSDyEa wDLTA/5zkhT7BGUXE02iGDI3c9FhGSpI38RqZnn4nJtV4JEUJxp+DqWLdBDnHBPMLV6JSm NwF24t3Iddnn29aDeFdB1045RF83B/k= From: Hao Xu To: io-uring@vger.kernel.org, Jens Axboe Cc: Dominique Martinet , Pavel Begunkov , Christian Brauner , Alexander Viro , Stefan Roesch , Clay Harris , Dave Chinner , "Darrick J . Wong" , linux-fsdevel@vger.kernel.org, linux-xfs@vger.kernel.org, linux-ext4@vger.kernel.org, Wanpeng Li Subject: [PATCH 4/7] add llseek_nowait() for struct file_operations Date: Wed, 26 Jul 2023 18:26:00 +0800 Message-Id: <20230726102603.155522-5-hao.xu@linux.dev> In-Reply-To: <20230726102603.155522-1-hao.xu@linux.dev> References: <20230726102603.155522-1-hao.xu@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT Precedence: bulk List-ID: X-Mailing-List: io-uring@vger.kernel.org From: Hao Xu Add a new function member llseek_nowait() in struct file_operations for nowait llseek. It act just like llseek() but has an extra boolean parameter called nowait to indicate if it's a nowait try, avoid IO and locks if so. Signed-off-by: Hao Xu --- include/linux/fs.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/linux/fs.h b/include/linux/fs.h index f3e315e8efdd..d37290da2d7e 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -1823,6 +1823,7 @@ struct file_operations { int (*uring_cmd)(struct io_uring_cmd *ioucmd, unsigned int issue_flags); int (*uring_cmd_iopoll)(struct io_uring_cmd *, struct io_comp_batch *, unsigned int poll_flags); + loff_t (*llseek_nowait)(struct file *, loff_t, int, bool); } __randomize_layout; struct inode_operations { From patchwork Wed Jul 26 10:26:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hao Xu X-Patchwork-Id: 13327806 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 76208C0015E for ; Wed, 26 Jul 2023 10:27:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233894AbjGZK1B (ORCPT ); Wed, 26 Jul 2023 06:27:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54416 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233862AbjGZK0y (ORCPT ); Wed, 26 Jul 2023 06:26:54 -0400 Received: from out-20.mta1.migadu.com (out-20.mta1.migadu.com [95.215.58.20]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1911A2139 for ; Wed, 26 Jul 2023 03:26:41 -0700 (PDT) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1690367199; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=7e4RDFDm8jz3Kb6NZqecvXg19GC0WTDfvDBloY02gI4=; b=QYlhmmBwxtEiNsNPpGdT6VCwV0vXW9/eKFMcdHDqJiJzkKleyVrWcM+oS9lyM12dW3+GB6 Qx9bj4mCGDTyOvFg9zbPkndM62ZYNqhSN8X9ATCD6MtlHAbtw6zUJ/NimZ1V3t7+vga7PH OfFuDepZCG8K1oI21pkY+Tv3dNRBB1Y= From: Hao Xu To: io-uring@vger.kernel.org, Jens Axboe Cc: Dominique Martinet , Pavel Begunkov , Christian Brauner , Alexander Viro , Stefan Roesch , Clay Harris , Dave Chinner , "Darrick J . Wong" , linux-fsdevel@vger.kernel.org, linux-xfs@vger.kernel.org, linux-ext4@vger.kernel.org, Wanpeng Li Subject: [PATCH 5/7] add llseek_nowait support for xfs Date: Wed, 26 Jul 2023 18:26:01 +0800 Message-Id: <20230726102603.155522-6-hao.xu@linux.dev> In-Reply-To: <20230726102603.155522-1-hao.xu@linux.dev> References: <20230726102603.155522-1-hao.xu@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT Precedence: bulk List-ID: X-Mailing-List: io-uring@vger.kernel.org From: Hao Xu Add llseek_nowait() operation for xfs, it acts just like llseek(). The thing different is it delivers nowait parameter to iomap layer. Signed-off-by: Hao Xu --- fs/xfs/xfs_file.c | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/fs/xfs/xfs_file.c b/fs/xfs/xfs_file.c index 73adc0aee2ff..cba82264221d 100644 --- a/fs/xfs/xfs_file.c +++ b/fs/xfs/xfs_file.c @@ -1257,10 +1257,11 @@ xfs_file_readdir( } STATIC loff_t -xfs_file_llseek( +__xfs_file_llseek( struct file *file, loff_t offset, - int whence) + int whence, + bool nowait) { struct inode *inode = file->f_mapping->host; @@ -1282,6 +1283,28 @@ xfs_file_llseek( return vfs_setpos(file, offset, inode->i_sb->s_maxbytes); } +STATIC loff_t +xfs_file_llseek( + struct file *file, + loff_t offset, + int whence) +{ + return __xfs_file_llseek(file, offset, whence, false); +} + +STATIC loff_t +xfs_file_llseek_nowait( + struct file *file, + loff_t offset, + int whence, + bool nowait) +{ + if (file->f_op == &xfs_file_operations) + return __xfs_file_llseek(file, offset, whence, nowait); + else + return generic_file_llseek(file, offset, whence); +} + #ifdef CONFIG_FS_DAX static inline vm_fault_t xfs_dax_fault( @@ -1442,6 +1465,7 @@ xfs_file_mmap( const struct file_operations xfs_file_operations = { .llseek = xfs_file_llseek, + .llseek_nowait = xfs_file_llseek_nowait, .read_iter = xfs_file_read_iter, .write_iter = xfs_file_write_iter, .splice_read = xfs_file_splice_read, @@ -1467,6 +1491,7 @@ const struct file_operations xfs_dir_file_operations = { .read = generic_read_dir, .iterate_shared = xfs_file_readdir, .llseek = generic_file_llseek, + .llseek_nowait = xfs_file_llseek_nowait, .unlocked_ioctl = xfs_file_ioctl, #ifdef CONFIG_COMPAT .compat_ioctl = xfs_file_compat_ioctl, From patchwork Wed Jul 26 10:26:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hao Xu X-Patchwork-Id: 13327807 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3BB11C0015E for ; Wed, 26 Jul 2023 10:27:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233800AbjGZK1K (ORCPT ); Wed, 26 Jul 2023 06:27:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54386 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233829AbjGZK05 (ORCPT ); Wed, 26 Jul 2023 06:26:57 -0400 Received: from out-50.mta1.migadu.com (out-50.mta1.migadu.com [95.215.58.50]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 486E92716 for ; Wed, 26 Jul 2023 03:26:47 -0700 (PDT) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1690367205; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Pom+EUyMYYQeMzw7Ruff4vSIuJsxO1g2AFt2EmqIW7g=; b=fsU5snFl5wlkpiManVsyEaqkA3jtYtL56SiUXPkUd2iP9vfHwkwlJOE8O0ij0tynJqOi6S u2AAScVr21h+JW9OrGQSDOETHmX1xQd0ggkbmaQLOjwtEsyGae6SRkpvVpe6Em6ca2gLOa o2lPAr6Nf7qOVDfuuv28eZrhNR7FoQQ= From: Hao Xu To: io-uring@vger.kernel.org, Jens Axboe Cc: Dominique Martinet , Pavel Begunkov , Christian Brauner , Alexander Viro , Stefan Roesch , Clay Harris , Dave Chinner , "Darrick J . Wong" , linux-fsdevel@vger.kernel.org, linux-xfs@vger.kernel.org, linux-ext4@vger.kernel.org, Wanpeng Li Subject: [PATCH 6/7] add vfs_lseek_nowait() Date: Wed, 26 Jul 2023 18:26:02 +0800 Message-Id: <20230726102603.155522-7-hao.xu@linux.dev> In-Reply-To: <20230726102603.155522-1-hao.xu@linux.dev> References: <20230726102603.155522-1-hao.xu@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT Precedence: bulk List-ID: X-Mailing-List: io-uring@vger.kernel.org From: Hao Xu Add a new vfs wrapper for io_uring lseek usage. The reason is the current vfs_lseek() calls llseek() but what we need is llseek_nowait(). Signed-off-by: Hao Xu --- fs/read_write.c | 18 ++++++++++++++++++ include/linux/fs.h | 3 +++ 2 files changed, 21 insertions(+) diff --git a/fs/read_write.c b/fs/read_write.c index b07de77ef126..b4c3bcf706e2 100644 --- a/fs/read_write.c +++ b/fs/read_write.c @@ -290,6 +290,24 @@ loff_t vfs_llseek(struct file *file, loff_t offset, int whence) } EXPORT_SYMBOL(vfs_llseek); +loff_t vfs_lseek_nowait(struct file *file, off_t offset, + int whence, bool nowait) +{ + if (!(file->f_mode & FMODE_LSEEK)) + return -ESPIPE; + /* + * This function is only used by io_uring, thus + * returning -ENOTSUPP is not proper since doing + * nonblock lseek as the first try is asked internally + * by io_uring not by users. Return -ENOTSUPP to users + * is not sane. + */ + if (!file->f_op->llseek_nowait) + return -EAGAIN; + return file->f_op->llseek_nowait(file, offset, whence, nowait); +} +EXPORT_SYMBOL(vfs_lseek_nowait); + static off_t ksys_lseek(unsigned int fd, off_t offset, unsigned int whence) { off_t retval; diff --git a/include/linux/fs.h b/include/linux/fs.h index d37290da2d7e..cb804d1f1650 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -2654,6 +2654,9 @@ extern loff_t default_llseek(struct file *file, loff_t offset, int whence); extern loff_t vfs_llseek(struct file *file, loff_t offset, int whence); +extern loff_t vfs_lseek_nowait(struct file *file, off_t offset, + int whence, bool nowait); + extern int inode_init_always(struct super_block *, struct inode *); extern void inode_init_once(struct inode *); extern void address_space_init_once(struct address_space *mapping); From patchwork Wed Jul 26 10:26:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hao Xu X-Patchwork-Id: 13327820 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 890E1C04FE1 for ; Wed, 26 Jul 2023 10:27:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233869AbjGZK1W (ORCPT ); Wed, 26 Jul 2023 06:27:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54464 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233911AbjGZK1D (ORCPT ); Wed, 26 Jul 2023 06:27:03 -0400 Received: from out-52.mta1.migadu.com (out-52.mta1.migadu.com [IPv6:2001:41d0:203:375::34]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 83F322693 for ; Wed, 26 Jul 2023 03:26:54 -0700 (PDT) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1690367212; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=AIwkwyyOL4dDQylwCuzd8varDzvojejFt1MW7yy46Fo=; b=W5LXT952/mTYofbLKoAITkAYWh3kf3RddWb4B/Ld8Mct10G7vgOyX8zIJrQyqP00Sml7eR ehvm6KsBP0sfwmI2AzHE52Fm3pGaMsIc4CRBs/fJ7rHayUnM8Gn9UYwTvUlThHNZMCFYt9 tcVA8ihaRusHPt5NWeVN7U14OV9HphI= From: Hao Xu To: io-uring@vger.kernel.org, Jens Axboe Cc: Dominique Martinet , Pavel Begunkov , Christian Brauner , Alexander Viro , Stefan Roesch , Clay Harris , Dave Chinner , "Darrick J . Wong" , linux-fsdevel@vger.kernel.org, linux-xfs@vger.kernel.org, linux-ext4@vger.kernel.org, Wanpeng Li Subject: [PATCH 7/7] add lseek for io_uring Date: Wed, 26 Jul 2023 18:26:03 +0800 Message-Id: <20230726102603.155522-8-hao.xu@linux.dev> In-Reply-To: <20230726102603.155522-1-hao.xu@linux.dev> References: <20230726102603.155522-1-hao.xu@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT Precedence: bulk List-ID: X-Mailing-List: io-uring@vger.kernel.org From: Hao Xu This is related with previous io_uring getdents patchset, we need a way to rewind the cursor of file when it comes to the end of a file by getdents. Introduce lseek to io_uring for this, besides, it's also a common syscall users call. So it's good for coding consistency when users use io_uring as their main loop. Signed-off-by: Hao Xu --- include/uapi/linux/io_uring.h | 1 + io_uring/fs.c | 63 +++++++++++++++++++++++++++++++++++ io_uring/fs.h | 3 ++ io_uring/opdef.c | 8 +++++ 4 files changed, 75 insertions(+) diff --git a/include/uapi/linux/io_uring.h b/include/uapi/linux/io_uring.h index c3efe241e310..d445876d4afc 100644 --- a/include/uapi/linux/io_uring.h +++ b/include/uapi/linux/io_uring.h @@ -236,6 +236,7 @@ enum io_uring_op { IORING_OP_SEND_ZC, IORING_OP_SENDMSG_ZC, IORING_OP_GETDENTS, + IORING_OP_LSEEK, /* this goes last, obviously */ IORING_OP_LAST, diff --git a/io_uring/fs.c b/io_uring/fs.c index 793eceb562a7..3992a19195ff 100644 --- a/io_uring/fs.c +++ b/io_uring/fs.c @@ -53,6 +53,12 @@ struct io_getdents { unsigned int count; }; +struct io_lseek { + struct file *file; + off_t offset; + unsigned int whence; +}; + int io_renameat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) { struct io_rename *ren = io_kiocb_to_cmd(req, struct io_rename); @@ -348,3 +354,60 @@ int io_getdents(struct io_kiocb *req, unsigned int issue_flags) return 0; } +int io_lseek_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) +{ + struct io_lseek *lsk = io_kiocb_to_cmd(req, struct io_lseek); + + if (unlikely(req->flags & REQ_F_FIXED_FILE)) + return -EBADF; + + lsk->offset = READ_ONCE(sqe->addr); + lsk->whence = READ_ONCE(sqe->len); + + return 0; +} + +int io_lseek(struct io_kiocb *req, unsigned int issue_flags) +{ + struct io_lseek *lsk = io_kiocb_to_cmd(req, struct io_lseek); + struct file *file = req->file; + bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; + bool should_lock = file->f_mode & FMODE_ATOMIC_POS; + unsigned int whence = lsk->whence; + loff_t res; + off_t ret; + + if (whence > SEEK_MAX) + return -EINVAL; + + if (force_nonblock) { + if (!(file->f_flags & O_NONBLOCK) && + !(file->f_mode & FMODE_NOWAIT)) + return -EAGAIN; + } + + if (should_lock) { + if (!force_nonblock) + mutex_lock(&file->f_pos_lock); + else if (!mutex_trylock(&file->f_pos_lock)) + return -EAGAIN; + } + + res = vfs_lseek_nowait(file, lsk->offset, whence, force_nonblock); + if (res == -EAGAIN && force_nonblock) { + if (should_lock) + mutex_unlock(&file->f_pos_lock); + return -EAGAIN; + } + + ret = res; + if (res != (loff_t)ret) + ret = -EOVERFLOW; + + if (should_lock) + mutex_unlock(&file->f_pos_lock); + + io_req_set_res(req, ret, 0); + return 0; +} + diff --git a/io_uring/fs.h b/io_uring/fs.h index f83a6f3a678d..32a8441c5142 100644 --- a/io_uring/fs.h +++ b/io_uring/fs.h @@ -21,3 +21,6 @@ void io_link_cleanup(struct io_kiocb *req); int io_getdents_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe); int io_getdents(struct io_kiocb *req, unsigned int issue_flags); + +int io_lseek_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe); +int io_lseek(struct io_kiocb *req, unsigned int issue_flags); diff --git a/io_uring/opdef.c b/io_uring/opdef.c index 1bae6b2a8d0b..eb1f7ee4f079 100644 --- a/io_uring/opdef.c +++ b/io_uring/opdef.c @@ -433,6 +433,11 @@ const struct io_issue_def io_issue_defs[] = { .prep = io_getdents_prep, .issue = io_getdents, }, + [IORING_OP_LSEEK] = { + .needs_file = 1, + .prep = io_lseek_prep, + .issue = io_lseek, + }, }; @@ -656,6 +661,9 @@ const struct io_cold_def io_cold_defs[] = { [IORING_OP_GETDENTS] = { .name = "GETDENTS", }, + [IORING_OP_LSEEK] = { + .name = "LSEEK", + }, }; const char *io_uring_get_opcode(u8 opcode)