From patchwork Tue Oct 22 14:50:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Harmstone X-Patchwork-Id: 13845786 Received: from mx0a-00082601.pphosted.com (mx0b-00082601.pphosted.com [67.231.153.30]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 71A851A0BD1 for ; Tue, 22 Oct 2024 14:50:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=67.231.153.30 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729608647; cv=none; b=DLncSS15OXW/ouMXgGeMXiELGkMd86ijG1lrVyDmDREmsM/clF00Ty9NjcFPW90ywUsm6uwrGtgMG6MyDgJYFTRnixliikYfywTxp6Tuxowi/H0M444fYVuCW3+75yWMj5rrsX3KuO7YSvnCVWMGOYOqcuyIvy9gECN5Fe2NZSc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729608647; c=relaxed/simple; bh=f9wbl/1eX7VxXVjF/NIOrNOYgbnJ+/NGVcPbU1q1Fm8=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=iois8rrPPqE+2HunUJLnkgAEcSkaFFl7StLB8B9R30xMa6dXomCQgxHomK8+Fqll+gjJeDOA1FT7oLTxtCz4PlcjTXEHatYNMp0Sl6SJBiHCR390sY0/YpiMeub7Q0tkUTekaID0APiYObyVprh55cqlIg+Asv0VzZY8Bdef+VI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=fb.com; spf=pass smtp.mailfrom=meta.com; dkim=pass (1024-bit key) header.d=fb.com header.i=@fb.com header.b=JqT0438M; arc=none smtp.client-ip=67.231.153.30 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=fb.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=meta.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=fb.com header.i=@fb.com header.b="JqT0438M" Received: from pps.filterd (m0001303.ppops.net [127.0.0.1]) by m0001303.ppops.net (8.18.1.2/8.18.1.2) with ESMTP id 49MDn5MB003018 for ; Tue, 22 Oct 2024 07:50:44 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=cc :content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=facebook; bh=3 gyl93d9CXuarbYsxeBNAg2bX1ItLNvyUtgtZltQC3k=; b=JqT0438MXUS7tUeWz iQLRPEGWPYT1f7g/jwC0xChz4QGmSFBmw8kN1CkJR4AWpZ54KUh6lV/zqIb42J/t fDW0J4Id6As77AsHvVbxpffGxIjJ/FwXzZWaL1HBHho6PG/CIGegLpcgjY//nqqh ZCY69ibb71db2vMdmaaX5flrvg= Received: from maileast.thefacebook.com ([163.114.135.16]) by m0001303.ppops.net (PPS) with ESMTPS id 42ea7hhgw1-3 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Tue, 22 Oct 2024 07:50:44 -0700 (PDT) Received: from twshared11671.02.ash9.facebook.com (2620:10d:c0a8:fe::f072) by mail.thefacebook.com (2620:10d:c0a9:6f::8fd4) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.2.1544.11; Tue, 22 Oct 2024 14:50:43 +0000 Received: by devbig276.nha1.facebook.com (Postfix, from userid 660015) id B2FCF7FDCDAB; Tue, 22 Oct 2024 15:50:32 +0100 (BST) From: Mark Harmstone To: CC: , Mark Harmstone Subject: [PATCH 3/5] btrfs: don't sleep in btrfs_encoded_read if IOCB_NOWAIT set Date: Tue, 22 Oct 2024 15:50:18 +0100 Message-ID: <20241022145024.1046883-4-maharmstone@fb.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20241022145024.1046883-1-maharmstone@fb.com> References: <20241022145024.1046883-1-maharmstone@fb.com> Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-FB-Internal: Safe X-Proofpoint-GUID: acHS66dZMVVphSb-ZKZ80XMJEeHLmk0a X-Proofpoint-ORIG-GUID: acHS66dZMVVphSb-ZKZ80XMJEeHLmk0a X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1051,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-10-05_03,2024-10-04_01,2024-09-30_01 Change btrfs_encoded_read so that it returns -EAGAIN rather than sleeps if IOCB_NOWAIT is set in iocb->ki_flags. Signed-off-by: Mark Harmstone --- fs/btrfs/inode.c | 54 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 43 insertions(+), 11 deletions(-) diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index 0a4dc85769c7..0c0753f20d54 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -8984,12 +8984,16 @@ static ssize_t btrfs_encoded_read_inline( unsigned long ptr; void *tmp; ssize_t ret; + bool nowait = iocb->ki_flags & IOCB_NOWAIT; path = btrfs_alloc_path(); if (!path) { ret = -ENOMEM; goto out; } + + path->nowait = !!nowait; + ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode), extent_start, 0); if (ret) { @@ -9200,11 +9204,15 @@ ssize_t btrfs_encoded_read(struct kiocb *iocb, struct iov_iter *iter, size_t count = iov_iter_count(iter); u64 start, lockend; struct extent_map *em; + bool nowait = iocb->ki_flags & IOCB_NOWAIT; bool unlocked = false; file_accessed(iocb->ki_filp); - btrfs_inode_lock(inode, BTRFS_ILOCK_SHARED); + ret = btrfs_inode_lock(inode, + BTRFS_ILOCK_SHARED | (nowait ? BTRFS_ILOCK_TRY : 0)); + if (ret) + return ret; if (iocb->ki_pos >= inode->vfs_inode.i_size) { btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED); @@ -9217,21 +9225,45 @@ ssize_t btrfs_encoded_read(struct kiocb *iocb, struct iov_iter *iter, */ lockend = start + BTRFS_MAX_UNCOMPRESSED - 1; - for (;;) { + if (nowait) { struct btrfs_ordered_extent *ordered; - ret = btrfs_wait_ordered_range(inode, start, - lockend - start + 1); - if (ret) + if (filemap_range_needs_writeback(inode->vfs_inode.i_mapping, + start, lockend)) { + ret = -EAGAIN; goto out_unlock_inode; - lock_extent(io_tree, start, lockend, cached_state); + } + + if (!try_lock_extent(io_tree, start, lockend, cached_state)) { + ret = -EAGAIN; + goto out_unlock_inode; + } + ordered = btrfs_lookup_ordered_range(inode, start, lockend - start + 1); - if (!ordered) - break; - btrfs_put_ordered_extent(ordered); - unlock_extent(io_tree, start, lockend, cached_state); - cond_resched(); + if (ordered) { + btrfs_put_ordered_extent(ordered); + unlock_extent(io_tree, start, lockend, cached_state); + ret = -EAGAIN; + goto out_unlock_inode; + } + } else { + for (;;) { + struct btrfs_ordered_extent *ordered; + + ret = btrfs_wait_ordered_range(inode, start, + lockend - start + 1); + if (ret) + goto out_unlock_inode; + lock_extent(io_tree, start, lockend, cached_state); + ordered = btrfs_lookup_ordered_range(inode, start, + lockend - start + 1); + if (!ordered) + break; + btrfs_put_ordered_extent(ordered); + unlock_extent(io_tree, start, lockend, cached_state); + cond_resched(); + } } em = btrfs_get_extent(inode, NULL, start, lockend - start + 1);