From patchwork Fri Aug 21 07:40:01 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Omar Sandoval X-Patchwork-Id: 11728535 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id EA4A91575 for ; Fri, 21 Aug 2020 07:41:51 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D34C7207DF for ; Fri, 21 Aug 2020 07:41:51 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=osandov-com.20150623.gappssmtp.com header.i=@osandov-com.20150623.gappssmtp.com header.b="SI6MZYPB" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728490AbgHUHlr (ORCPT ); Fri, 21 Aug 2020 03:41:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37272 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728350AbgHUHkk (ORCPT ); Fri, 21 Aug 2020 03:40:40 -0400 Received: from mail-pl1-x644.google.com (mail-pl1-x644.google.com [IPv6:2607:f8b0:4864:20::644]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1E271C06134C for ; Fri, 21 Aug 2020 00:40:40 -0700 (PDT) Received: by mail-pl1-x644.google.com with SMTP id v16so525472plo.1 for ; Fri, 21 Aug 2020 00:40:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=osandov-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=I8G1MRUtVdExAMhe+MbiWMvEjhI7+ElYPCDd1oRGomM=; b=SI6MZYPB1fRFi2ehv4X52h7DNtB8xTBk3fQ/uaGz9gGw59s9Y+3u9c/vLRYeLEuzvy OtCTXcwlGBTGA/PXyyF+0AUtQwWyb7HYdhWPM2YFw1Y5Y2oYh1HkXLuhApOnIRgWuYoR b02L/wW3fAQTnObroAeG9HdRfSNy+SJWO3bAPVZNIlDZ6BpqoFGiDpsG9lpQFlTn6Uvy 2jpun7jycF6PuRsMC6t89rQN5JHLgWyJ6qUzi+nLyOPjS/BKd+JDPmdKauFqVCcRULyz uidG0TosmM4QXAhL6WS0eYLDr/Rhh6BT9q24yJ0ts0efuS+NKxYSilvJ06HR5KRTHsjx b/Tg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=I8G1MRUtVdExAMhe+MbiWMvEjhI7+ElYPCDd1oRGomM=; b=nFhc8Q+nbUTArbTEJdyZEpHTYsGSEZQGlJJg3+MKQFrxS0LXXUHE3/HAb1FiyYD4A5 7AIaPJ7ZdcnrNFFDmHtCUiVwQF3gU4rykHvjuombEu2uKV1VRLw2kVNqc4XYwQfrqt6e haL8qmM4n6dyWHSrLrxsh/bCe5n0Ow276BO6tNbKY+/hKAai8RR4ye6nKoRb2W4xMwRe P49O8pHNWCtAKIjXNMSIi1m3zPyYU8L/RXWQDwIi5mBoFZm6LJ/ySSy+cr5US1XFRNcA cD7H2QfNLIohpPTVD/0WddXTnAd7BVveKj6LEyz21swv7eiMUKxA6uycQtZdnszhBjjG 4EUw== X-Gm-Message-State: AOAM530nwqKzmyDzE1iUUiR0HryupjyI7DisXWU9YUMCSAfWdw5TnBhA JkV5vc12gyi6he4ds0GfEmDgrQGcQ08TIw== X-Google-Smtp-Source: ABdhPJxdo4tE5unzj3qIsDUOhhOZlMwg+jIP3Bp56uYmmoR1m3YEO6XJaVQzDe4h7sZxmDJOWyI5Lw== X-Received: by 2002:a17:902:b701:: with SMTP id d1mr1478923pls.92.1597995639022; Fri, 21 Aug 2020 00:40:39 -0700 (PDT) Received: from exodia.tfbnw.net ([2620:10d:c090:400::5:f2a4]) by smtp.gmail.com with ESMTPSA id jb1sm1080875pjb.9.2020.08.21.00.40.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 21 Aug 2020 00:40:37 -0700 (PDT) From: Omar Sandoval To: linux-btrfs@vger.kernel.org Cc: linux-fsdevel@vger.kernel.org Subject: [PATCH 02/11] btrfs-progs: receive: dynamically allocate sctx->read_buf Date: Fri, 21 Aug 2020 00:40:01 -0700 Message-Id: <6e4ecaf9b4182ef9f1e3a18ad32c089d82f4d64c.1597994354.git.osandov@osandov.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: References: MIME-Version: 1.0 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org From: Boris Burkov In send stream v2, write commands can now be an arbitrary size. For that reason, we can no longer allocate a fixed array in sctx for read_cmd. Instead, read_cmd dynamically allocates sctx->read_buf. To avoid needless reallocations, we reuse read_buf between read_cmd calls by also keeping track of the size of the allocated buffer in sctx->read_buf_sz. We do the first allocation of the old default size at the start of processing the stream, and we only reallocate if we encounter a command that needs a larger buffer. Signed-off-by: Boris Burkov --- common/send-stream.c | 55 ++++++++++++++++++++++++++++---------------- send.h | 2 +- 2 files changed, 36 insertions(+), 21 deletions(-) diff --git a/common/send-stream.c b/common/send-stream.c index 3bd21d3f..51a6a94a 100644 --- a/common/send-stream.c +++ b/common/send-stream.c @@ -36,10 +36,10 @@ struct btrfs_send_attribute { struct btrfs_send_stream { int fd; - char read_buf[BTRFS_SEND_BUF_SIZE]; + char *read_buf; + size_t read_buf_sz; int cmd; - struct btrfs_cmd_header *cmd_hdr; struct btrfs_send_attribute cmd_attrs[BTRFS_SEND_A_MAX + 1]; u32 version; @@ -111,11 +111,12 @@ static int read_cmd(struct btrfs_send_stream *sctx) u32 pos; u32 crc; u32 crc2; + struct btrfs_cmd_header *cmd_hdr; + size_t buf_len; memset(sctx->cmd_attrs, 0, sizeof(sctx->cmd_attrs)); - ASSERT(sizeof(*sctx->cmd_hdr) <= sizeof(sctx->read_buf)); - ret = read_buf(sctx, sctx->read_buf, sizeof(*sctx->cmd_hdr)); + ret = read_buf(sctx, sctx->read_buf, sizeof(*cmd_hdr)); if (ret < 0) goto out; if (ret) { @@ -124,18 +125,22 @@ static int read_cmd(struct btrfs_send_stream *sctx) goto out; } - sctx->cmd_hdr = (struct btrfs_cmd_header *)sctx->read_buf; - cmd = le16_to_cpu(sctx->cmd_hdr->cmd); - cmd_len = le32_to_cpu(sctx->cmd_hdr->len); - - if (cmd_len + sizeof(*sctx->cmd_hdr) >= sizeof(sctx->read_buf)) { - ret = -EINVAL; - error("command length %u too big for buffer %zu", - cmd_len, sizeof(sctx->read_buf)); - goto out; + cmd_hdr = (struct btrfs_cmd_header *)sctx->read_buf; + cmd_len = le32_to_cpu(cmd_hdr->len); + cmd = le16_to_cpu(cmd_hdr->cmd); + buf_len = sizeof(*cmd_hdr) + cmd_len; + if (sctx->read_buf_sz < buf_len) { + sctx->read_buf = realloc(sctx->read_buf, buf_len); + if (!sctx->read_buf) { + ret = -ENOMEM; + error("failed to reallocate read buffer for cmd"); + goto out; + } + sctx->read_buf_sz = buf_len; + /* We need to reset cmd_hdr after realloc of sctx->read_buf */ + cmd_hdr = (struct btrfs_cmd_header *)sctx->read_buf; } - - data = sctx->read_buf + sizeof(*sctx->cmd_hdr); + data = sctx->read_buf + sizeof(*cmd_hdr); ret = read_buf(sctx, data, cmd_len); if (ret < 0) goto out; @@ -145,11 +150,12 @@ static int read_cmd(struct btrfs_send_stream *sctx) goto out; } - crc = le32_to_cpu(sctx->cmd_hdr->crc); - sctx->cmd_hdr->crc = 0; + crc = le32_to_cpu(cmd_hdr->crc); + /* in send, crc is computed with header crc = 0, replicate that */ + cmd_hdr->crc = 0; crc2 = crc32c(0, (unsigned char*)sctx->read_buf, - sizeof(*sctx->cmd_hdr) + cmd_len); + sizeof(*cmd_hdr) + cmd_len); if (crc != crc2) { ret = -EINVAL; @@ -524,19 +530,28 @@ int btrfs_read_and_process_send_stream(int fd, goto out; } + sctx.read_buf = malloc(BTRFS_SEND_BUF_SIZE_V1); + if (!sctx.read_buf) { + ret = -ENOMEM; + error("unable to allocate send stream read buffer"); + goto out; + } + sctx.read_buf_sz = BTRFS_SEND_BUF_SIZE_V1; + while (1) { ret = read_and_process_cmd(&sctx); if (ret < 0) { last_err = ret; errors++; if (max_errors > 0 && errors >= max_errors) - goto out; + break; } else if (ret > 0) { if (!honor_end_cmd) ret = 0; - goto out; + break; } } + free(sctx.read_buf); out: if (last_err && !ret) diff --git a/send.h b/send.h index 8dd865ec..228928a0 100644 --- a/send.h +++ b/send.h @@ -33,7 +33,7 @@ extern "C" { #define BTRFS_SEND_STREAM_MAGIC "btrfs-stream" #define BTRFS_SEND_STREAM_VERSION 1 -#define BTRFS_SEND_BUF_SIZE SZ_64K +#define BTRFS_SEND_BUF_SIZE_V1 SZ_64K #define BTRFS_SEND_READ_SIZE (1024 * 48) enum btrfs_tlv_type {