From patchwork Tue Jun 25 13:02:13 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: 2776531 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 92829C0AB1 for ; Tue, 25 Jun 2013 13:02:33 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 707AA201B9 for ; Tue, 25 Jun 2013 13:02:32 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 491B3201B4 for ; Tue, 25 Jun 2013 13:02:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751735Ab3FYNCY (ORCPT ); Tue, 25 Jun 2013 09:02:24 -0400 Received: from mail-pa0-f41.google.com ([209.85.220.41]:63663 "EHLO mail-pa0-f41.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751595Ab3FYNCV (ORCPT ); Tue, 25 Jun 2013 09:02:21 -0400 Received: by mail-pa0-f41.google.com with SMTP id bj3so12636646pad.14 for ; Tue, 25 Jun 2013 06:02:21 -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=bjGFN3JYdslaLsdtZTuA16TOH+K+dLTIjbiB0PmwdTY=; b=bGaI7lC+dt1I+Hbfx1CZLNnEPw41sLq6AtFHTrC8SEBTlL4xtJkGoNAcXdeaibqLYu nKacowGb57xJKfOwtlueTM86zhvbzh/WDx2F0v0o5l/XkeajESeRK8AEbH1Wz/l2FmdE 9F9KbYc+FwDNPspT1xpD18giFbaQxkfyAnG1X5lVeD2cg8VTAkSFp/uap84PsiO+qj3h GR5aOjJV3yedfftqHl3B8lkbL6/CIEyxr4KpQrX1dozx9fyNaGTGPrzs3tlXXcA68mTa hEmO8+yVDj5fmKliEQUm4GrHK7qSwbNnUjXcSyJDMbS2BAbgVLh+y2cB0BCk5x6FoxTz xj2g== X-Received: by 10.66.246.66 with SMTP id xu2mr5687516pac.134.1372165341431; Tue, 25 Jun 2013 06:02:21 -0700 (PDT) Received: from [192.168.1.101] ([221.217.177.114]) by mx.google.com with ESMTPSA id pe9sm23109033pbc.35.2013.06.25.06.02.18 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 25 Jun 2013 06:02:20 -0700 (PDT) Message-ID: <51C994D5.3070509@gmail.com> Date: Tue, 25 Jun 2013 21:02:13 +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: [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 * The above items 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; }