Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp827544rwb; Wed, 26 Jul 2023 03:39:07 -0700 (PDT) X-Google-Smtp-Source: APBJJlH0bjMIcr8PivMueuPaK18AiKrWhmroO9TrxEkYIRp8FyEl7svRJAJWqq6erFg46qzIQ2fI X-Received: by 2002:a17:906:4d2:b0:997:c5c3:32cc with SMTP id g18-20020a17090604d200b00997c5c332ccmr1321186eja.66.1690367947145; Wed, 26 Jul 2023 03:39:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690367947; cv=none; d=google.com; s=arc-20160816; b=mXsu0jaAYKMkFbbS3UJwAj0D0FySwMZV57vli1lNQhKvchh7XmiIA3fH4SYPPoUEgb Qs3T4paRcwrp4P7e/mG6jw/FAvGigA4sbqxVgp5Rz8mGnC/3Bncq3nXo7In8HSAZucfN XAxWCklv8DELsEtdFh0qU/x2rso0zcCtKJa2i2H2YI0WCDCwi8dwhB7Ji4uebWX83m6k f/SoZ9fKhqDWBIqXhfyZobscAqdNKRtXjLyo980vKKrqb09iKDfrmJ0+6ao17W87B2wg jOr9TUWVAsLFZ1v4Z7UlZXvH27ctELcMK9ZmOv9QLZc/QjUXT+Ho852QpS22hMxISY5u NH7Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=AIwkwyyOL4dDQylwCuzd8varDzvojejFt1MW7yy46Fo=; fh=s3EkFveja4PFWbIac+xkRWU8Dk/FoQIVbxjqrCu6kgU=; b=aQKhT0M4Lv6QN5ceAI55uveBdTN1M0z765AzAcaC6zGAo8nHetJ8gUfOlOVqQrNMrp vemugKUOFfMUcG5AxITRD50bJnXMQB9bB7SaMAACvXD4gxNMMYa+7v25E2Yxy5gFwW7m XITYGoFPJ5GnwpUVJgE7hKuKZ33EGy4OsrN4bUR0VQIli3Yq6g+WXcFt0SngxgrF3/uq nOq81sSO6J5mbzgSh6PSASKudOBzraqSTv14FyZqeOXnmwkojxBFM2bav0TEemhTIYi7 i5gZnHaZviJmqpFYKLGIFlrEQVaO94jbBHM75IBDYsZoG4O7jkhbN0WZM5wDx3g4t0DM C5OA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=W5LXT952; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f20-20020a1709062c5400b00993211b85acsi9581169ejh.216.2023.07.26.03.38.42; Wed, 26 Jul 2023 03:39:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=W5LXT952; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233893AbjGZKdf (ORCPT + 99 others); Wed, 26 Jul 2023 06:33:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57766 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233985AbjGZKdS (ORCPT ); Wed, 26 Jul 2023 06:33:18 -0400 Received: from out-60.mta1.migadu.com (out-60.mta1.migadu.com [95.215.58.60]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7F9922126 for ; Wed, 26 Jul 2023 03:33:17 -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 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-ext4@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) -- 2.25.1