From patchwork Sat Feb 24 01:14:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Darrick J. Wong" X-Patchwork-Id: 205759 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp934499dyb; Fri, 23 Feb 2024 17:18:19 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXU2Mo4i4GaLGkQ7ZuWtgPC6Hbt4GUO7aAXja7j9/422CJXC2pdrAVnsyRUlEJhAUjivUy2jyTXvy3c/glEJN008RLLGw== X-Google-Smtp-Source: AGHT+IGFN6LIcauRBCGweo2DQ/mRWAvvDlLEA3Hmwz4M5sApfyaoacovWIc5ya6NW1DJ02EaeSrs X-Received: by 2002:a05:6359:a385:b0:17b:5676:19b2 with SMTP id ky5-20020a056359a38500b0017b567619b2mr1765276rwc.10.1708737499246; Fri, 23 Feb 2024 17:18:19 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708737499; cv=pass; d=google.com; s=arc-20160816; b=VL/kmGWHE0L55crh1bS7CkfBnDrVZ1dudSEHhtotaEg5qasjdlNlxrRlXMmhcAfO+3 VtwIcEg3vCvW1fSmK9TArvQA25gx/GImHxDUirwN0b1z45dfBKDdyLE6PD/pT/5oThF9 Vqd8U6LVvFKquJIMEIaRsXPgQ0w6Z3vBacn5NUSgkgVbTlRTqWjun2mEWWu4dlr9PlCu 1m2tYv22r/SrAMLODSUoRKOnIypKbb5dDcQuYs5cVeH24qgvV/9BcdnQu+kXRAu4O2Lh 3c3ASJQatQ1AHUDWg91geCV4YLqSLdbRWD8BEmqNC0bDsMs+YDUjLGRmLcz+uDuAtyls XDqw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:user-agent:references:in-reply-to :message-id:cc:to:from:subject:date:dkim-signature; bh=EN2dD0PbALxPZG5/TFOBMMvc0sfInemJPjZlWMARc48=; fh=eGotrG2wUAPCtJbO4MhMKF1jcndrbQy7pSZLLDRnPjs=; b=cW0iIQ4LUeAmfsMwHzCpGNFmDNx3F/Kk2DQTUMadRTYdDWmbXhcC/F/UJmWhyNFgv2 TsoSNgPh7N/idrSdN5avc7/6P3w6YobYxcFkP4yVXIPuiWBMqQHS/rf/bYsp6uVvHifP 0iaS2zdwBL2TVDm+pnMJFUzkmc7LOkbUHvdh6wz/OLlnO36tkhUtDHbvJ7uxQtTqM2sN 5SYG/XOqARdcASjrSxkKufO+CeKP9tiH5KpltIc7z7Q1GhwCzZg5k0j5AEs//vALDHc0 ktD9nloMw+KMtuAjyyU0LaGM1PxVvhyfEb+9C7K/OJ8woi1Yk8QX9G45x4UkzrYuZ4MX J7ew==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=rabPkwEp; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-79406-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-79406-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id w64-20020a636243000000b005d8c06e15e6si144987pgb.533.2024.02.23.17.18.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Feb 2024 17:18:19 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-79406-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=rabPkwEp; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-79406-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-79406-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id AF23EB25375 for ; Sat, 24 Feb 2024 01:15:37 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B36A9BE7F; Sat, 24 Feb 2024 01:14:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="rabPkwEp" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 1DB403C39; Sat, 24 Feb 2024 01:14:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708737288; cv=none; b=FekCrNqSVph9WfIYmitViIB/i/in7WN/1/RJdNlIEfDBtOYFE1ZzwQ6Eny6WMEPn3+uElO4oXq+ALdH+r+FtmW4UlPm57Li6/hli6lZSj52nJzsIcU62oOIlkP023yAZ3I47dfrfynG4CP9NK0WDWkag6FTtRmpUPLGSq+54Ktc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708737288; c=relaxed/simple; bh=WrJLvBrJN6c+u26aFr/AFgdi3noYUY58rrqupm7VACE=; h=Date:Subject:From:To:Cc:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Ta56BMH9rDbd5041QKwaYr/3h2/inTTipB9ckVAyVy+8Ze6QRW90oQpRH/UNWGrvhe3ftazB6hpLVUygdIxkWHI0AKPZXlD/ABpZLkKS1rIYy3Ved63/VdVhMdIgADtN1tVsBn1yW7oGcNeEOymS6ANTwyQDhwzJqH3w6bic+B4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=rabPkwEp; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8F876C433F1; Sat, 24 Feb 2024 01:14:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1708737287; bh=WrJLvBrJN6c+u26aFr/AFgdi3noYUY58rrqupm7VACE=; h=Date:Subject:From:To:Cc:In-Reply-To:References:From; b=rabPkwEpUOEP551icQaUSJYixrIFwuCuW8iGr8AKhmy2rZdJYpmTS3ohjLNTTbbG+ bMjmqziTVJVe+MpL/FPOIkTqPh5KE6a2MDlfbHP8fw8ZiJDB4HLRR1xwtoVs2KN70+ Xw+YZ9HhnyOkVjTqqru1Csa/DkKwLFRu6m6Fdyq5xP/h1GsMjSebjrmzQXRVr+8V4J iMt0fBR66fpj4EV1zdAIALki9SbXn/7wPVQHJCm0Rv/i6vU6lrH1pQyTDcgM4o7A+y Kj1FAtnw09D5dsQOZr96iORae+EhMCkmFELaohq0mS+ILZo7xEAui1hry13DcuPij3 TAr3jv1rdC+hw== Date: Fri, 23 Feb 2024 17:14:47 -0800 Subject: [PATCH 04/10] bcachefs: thread_with_stdio: fix bch2_stdio_redirect_readline() From: "Darrick J. Wong" To: akpm@linux-foundation.org, daniel@gluo.nz, kent.overstreet@linux.dev, djwong@kernel.org Cc: linux-xfs@vger.kernel.org, linux-bcachefs@vger.kernel.org, linux-kernel@vger.kernel.org Message-ID: <170873668950.1861398.18421058347092974314.stgit@frogsfrogsfrogs> In-Reply-To: <170873668859.1861398.2367011381778949840.stgit@frogsfrogsfrogs> References: <170873668859.1861398.2367011381778949840.stgit@frogsfrogsfrogs> User-Agent: StGit/0.19 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791741132378765426 X-GMAIL-MSGID: 1791741132378765426 From: Kent Overstreet This fixes a bug where we'd return data without waiting for a newline, if data was present but a newline was not. Signed-off-by: Kent Overstreet Reviewed-by: Darrick J. Wong Signed-off-by: Darrick J. Wong --- fs/bcachefs/thread_with_file.c | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/fs/bcachefs/thread_with_file.c b/fs/bcachefs/thread_with_file.c index eb8ab4c47a94b..830efb06ef0be 100644 --- a/fs/bcachefs/thread_with_file.c +++ b/fs/bcachefs/thread_with_file.c @@ -277,25 +277,36 @@ int bch2_stdio_redirect_read(struct stdio_redirect *stdio, char *ubuf, size_t le int bch2_stdio_redirect_readline(struct stdio_redirect *stdio, char *ubuf, size_t len) { struct stdio_buf *buf = &stdio->input; - + size_t copied = 0; + ssize_t ret = 0; +again: wait_event(buf->wait, stdio_redirect_has_input(stdio)); - if (stdio->done) - return -1; + if (stdio->done) { + ret = -1; + goto out; + } spin_lock(&buf->lock); - int ret = min(len, buf->buf.nr); - char *n = memchr(buf->buf.data, '\n', ret); - if (!n) - ret = min(ret, n + 1 - buf->buf.data); - buf->buf.nr -= ret; - memcpy(ubuf, buf->buf.data, ret); + size_t b = min(len, buf->buf.nr); + char *n = memchr(buf->buf.data, '\n', b); + if (n) + b = min_t(size_t, b, n + 1 - buf->buf.data); + buf->buf.nr -= b; + memcpy(ubuf, buf->buf.data, b); memmove(buf->buf.data, - buf->buf.data + ret, + buf->buf.data + b, buf->buf.nr); + ubuf += b; + len -= b; + copied += b; spin_unlock(&buf->lock); wake_up(&buf->wait); - return ret; + + if (!n && len) + goto again; +out: + return copied ?: ret; } __printf(3, 0)