From patchwork Tue Jun 25 13:05:27 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wang Sheng-Hui X-Patchwork-Id: 2776541 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 4CDDF9F3A0 for ; Tue, 25 Jun 2013 13:05:49 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 94E66201ED for ; Tue, 25 Jun 2013 13:05:44 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id E6CC4201B9 for ; Tue, 25 Jun 2013 13:05:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751784Ab3FYNFd (ORCPT ); Tue, 25 Jun 2013 09:05:33 -0400 Received: from mail-pb0-f42.google.com ([209.85.160.42]:37776 "EHLO mail-pb0-f42.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751076Ab3FYNFc (ORCPT ); Tue, 25 Jun 2013 09:05:32 -0400 Received: by mail-pb0-f42.google.com with SMTP id un1so12555082pbc.29 for ; Tue, 25 Jun 2013 06:05:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject :content-type:content-transfer-encoding; bh=rstjrepuv6OsU+hvzNjtsWKcTmct4hNRxHVZ5Kqn5qQ=; b=ZgbywkXVOO0cW4W06N/8B9jsT5UIoQMNLJom1lzFhZLBplNUst2+qiBUVo4619dXam 8nPhHmB2cOsOeZj37eoir+H9z1hxTQ9CMjWAB4kGYqiw8v43S0f+Sx+gYDBwj/2G87er GlgrCh8NlyBqmKRN6Oug3N/LpRDpod4l/SIfWj4YyxbWk1f2igrt8v0B9Mz4o8Wp90qu NclwoaTD7an1UB4WhhirR0Y9Sf85zlpU58aRe4E9d9hJFj2OZI5PvQXOY/GmCa0nJsCE zsKuLU32ds2gF8hcdBVbynafgaPxrYrE+BwRP6oO/lcoOgtwBq01tRX1UYJix8abg2Fz 993Q== X-Received: by 10.68.253.161 with SMTP id ab1mr5301509pbd.76.1372165531463; Tue, 25 Jun 2013 06:05:31 -0700 (PDT) Received: from [192.168.1.101] ([221.217.177.114]) by mx.google.com with ESMTPSA id e2sm23146253pbc.23.2013.06.25.06.05.29 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 25 Jun 2013 06:05:30 -0700 (PDT) Message-ID: <51C99597.1090604@gmail.com> Date: Tue, 25 Jun 2013 21:05:27 +0800 From: Wang Sheng-Hui User-Agent: Mozilla/5.0 (X11; Linux i686; rv:17.0) Gecko/17.0 Thunderbird/17.0 MIME-Version: 1.0 To: Josef Bacik , chris.mason@fusionio.com, linux-btrfs@vger.kernel.org Subject: [RESEND] [PATCH] btrfs-progs: avoid memory leak in btrfs_close_devices Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Spam-Status: No, score=-8.1 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, T_DKIM_INVALID, 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 Three kind of structures need to be freed on close: * All struct btrfs_device managed by fs_devices * The name field for each struct btrfs_device * fs_devices structure itself The same ones for seed_devices. Signed-off-by: Wang Sheng-Hui --- volumes.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/volumes.c b/volumes.c index d6f81f8..257b740 100644 --- a/volumes.c +++ b/volumes.c @@ -153,6 +153,16 @@ static int device_list_add(const char *path, return 0; } +static void btrfs_close_device(struct btrfs_device *device) +{ + close(device->fd); + device->fd = -1; + device->writeable = 0; + if (device->name) + kfree(device->name); + kfree(device); +} + int btrfs_close_devices(struct btrfs_fs_devices *fs_devices) { struct btrfs_fs_devices *seed_devices; @@ -161,17 +171,17 @@ int btrfs_close_devices(struct btrfs_fs_devices *fs_devices) again: list_for_each(cur, &fs_devices->devices) { device = list_entry(cur, struct btrfs_device, dev_list); - close(device->fd); - device->fd = -1; - device->writeable = 0; + btrfs_close_device(device); } seed_devices = fs_devices->seed; fs_devices->seed = NULL; if (seed_devices) { + kfree(fs_devices); fs_devices = seed_devices; goto again; } + kfree(fs_devices); return 0; }