From patchwork Thu Jun 26 02:53:02 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gui Hecheng X-Patchwork-Id: 4425521 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id EDED99F387 for ; Thu, 26 Jun 2014 02:58:38 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id EE9C020379 for ; Thu, 26 Jun 2014 02:58:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id D3D952037F for ; Thu, 26 Jun 2014 02:58:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1757251AbaFZC6d (ORCPT ); Wed, 25 Jun 2014 22:58:33 -0400 Received: from cn.fujitsu.com ([59.151.112.132]:40402 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1755828AbaFZC6b (ORCPT ); Wed, 25 Jun 2014 22:58:31 -0400 X-IronPort-AV: E=Sophos;i="5.00,779,1396972800"; d="scan'208";a="32451595" Received: from unknown (HELO edo.cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 26 Jun 2014 10:55:48 +0800 Received: from G08CNEXCHPEKD01.g08.fujitsu.local (localhost.localdomain [127.0.0.1]) by edo.cn.fujitsu.com (8.14.3/8.13.1) with ESMTP id s5Q2wSnL027090 for ; Thu, 26 Jun 2014 10:58:28 +0800 Received: from localhost.localdomain (10.167.226.111) by G08CNEXCHPEKD01.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server (TLS) id 14.3.181.6; Thu, 26 Jun 2014 10:58:37 +0800 From: Gui Hecheng To: CC: Gui Hecheng Subject: [PATCH 2/6] btrfs-progs: deal with malloc failure in btrfs-image Date: Thu, 26 Jun 2014 10:53:02 +0800 Message-ID: <1403751186-31559-2-git-send-email-guihc.fnst@cn.fujitsu.com> X-Mailer: git-send-email 1.8.1.4 In-Reply-To: <1403751186-31559-1-git-send-email-guihc.fnst@cn.fujitsu.com> References: <1403751186-31559-1-git-send-email-guihc.fnst@cn.fujitsu.com> MIME-Version: 1.0 X-Originating-IP: [10.167.226.111] Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Handle the malloc failure for dump_worker in the same way as the restore worker. Signed-off-by: Gui Hecheng --- btrfs-image.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/btrfs-image.c b/btrfs-image.c index c90bca8..7131001 100644 --- a/btrfs-image.c +++ b/btrfs-image.c @@ -107,6 +107,8 @@ struct metadump_struct { int done; int data; int sanitize_names; + + int error; }; struct name { @@ -602,6 +604,14 @@ static void *dump_worker(void *data) async->bufsize = compressBound(async->size); async->buffer = malloc(async->bufsize); + if (!async->buffer) { + fprintf(stderr, "Error allocing buffer\n"); + pthread_mutex_lock(&md->mutex); + if (!md->error) + md->error = -ENOMEM; + pthread_mutex_unlock(&md->mutex); + pthread_exit(NULL); + } ret = compress2(async->buffer, (unsigned long *)&async->bufsize, @@ -736,7 +746,7 @@ static int write_buffers(struct metadump_struct *md, u64 *next) goto out; /* wait until all buffers are compressed */ - while (md->num_items > md->num_ready) { + while (!err && md->num_items > md->num_ready) { struct timespec ts = { .tv_sec = 0, .tv_nsec = 10000000, @@ -744,6 +754,13 @@ static int write_buffers(struct metadump_struct *md, u64 *next) pthread_mutex_unlock(&md->mutex); nanosleep(&ts, NULL); pthread_mutex_lock(&md->mutex); + err = md->error; + } + + if (err) { + fprintf(stderr, "One of the threads errored out %s\n", + strerror(err)); + goto out; } /* setup and write index block */