From patchwork Mon Jun 10 19:51:31 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 2698901 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork1.kernel.org (Postfix) with ESMTP id 95046400E6 for ; Mon, 10 Jun 2013 19:52:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752336Ab3FJTwD (ORCPT ); Mon, 10 Jun 2013 15:52:03 -0400 Received: from mail-wg0-f47.google.com ([74.125.82.47]:58819 "EHLO mail-wg0-f47.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751647Ab3FJTv7 (ORCPT ); Mon, 10 Jun 2013 15:51:59 -0400 Received: by mail-wg0-f47.google.com with SMTP id l18so1724572wgh.26 for ; Mon, 10 Jun 2013 12:51:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; bh=jnuZVpamJ/jgQBe6G5BZ7Oiwgwu4VrC+5BmGrvzWQBo=; b=N0AAnbMUJfI5EwfQ2KGb7jfqAy1iAcwoBwKEfCuq2nEE7Gz/omj/5aa0FEPoOtupjl Z9q+662G6Sb8nYoaEQmZ5eWlZQpYWAPAEpR9/MNspMXP05hqPIisOsKxL1R900FsUqoE 20JXO6xwqAuch3U4A7nec4JnkkFLBl6iHjbnyx5gJad52kM2pIpIjb3SEbKD1dWW55+z dIwg6gqk67xCFzG+CTKN5mRwShxuQH/pyrDr82+UEzJUPbN4dTtsK575FgMjDUlyTxJO Gebc6au3Y1idxMX3vN6kZPWcfKv/MIql8Aws89UH6X/RadE/xe+x51xXWucHIUCmFc6R TUSQ== X-Received: by 10.180.198.110 with SMTP id jb14mr5539151wic.37.1370893918298; Mon, 10 Jun 2013 12:51:58 -0700 (PDT) Received: from storm-desktop.lan (bl9-171-73.dsl.telepac.pt. [85.242.171.73]) by mx.google.com with ESMTPSA id dj7sm13607906wib.6.2013.06.10.12.51.56 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 10 Jun 2013 12:51:57 -0700 (PDT) From: Filipe David Borba Manana To: linux-btrfs@vger.kernel.org Cc: Filipe David Borba Manana Subject: [PATCH 1/5] Btrfs-progs: fix closing of devices Date: Mon, 10 Jun 2013 20:51:31 +0100 Message-Id: <1370893895-24884-2-git-send-email-fdmanana@gmail.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1370893895-24884-1-git-send-email-fdmanana@gmail.com> References: <1370893895-24884-1-git-send-email-fdmanana@gmail.com> Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org If a device could not be opened in volumes.c:read_one_dev(), a btrfs_device instance was allocated and added to the list of devices of the fs - however this device instance had its fd, name and label fields not initialized. This is problematic in disk-io.c:close_all_devices() as it tries to close the (invalid) fd of the device and kfree() its name and label, which point to random memory locations. Thread 1 (Thread 0x7f0a3d2d1740 (LWP 23585)): #0 __GI___libc_free (mem=0xa5a5a5a5a5a5a5a5) at malloc.c:2970 #1 0x000000000042054b in close_all_devices (fs_info=0x1e92bf0) at disk-io.c:1276 #2 0x0000000000421dcd in close_ctree (root=) at disk-io.c:1336 #3 0x0000000000418cfa in cmd_check (argc=, argv=) at cmds-check.c:4171 #4 0x0000000000403ed4 in main (argc=2, argv=0x7fff9a583d28) at btrfs.c:295 Signed-off-by: Filipe David Borba Manana --- disk-io.c | 4 ++-- volumes.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/disk-io.c b/disk-io.c index 21b410d..bd9cf4e 100644 --- a/disk-io.c +++ b/disk-io.c @@ -1267,12 +1267,12 @@ static int close_all_devices(struct btrfs_fs_info *fs_info) while (!list_empty(list)) { device = list_entry(list->next, struct btrfs_device, dev_list); list_del_init(&device->dev_list); - if (device->fd) { + if (device->fd >= 0) { fsync(device->fd); if (posix_fadvise(device->fd, 0, 0, POSIX_FADV_DONTNEED)) fprintf(stderr, "Warning, could not drop caches\n"); + close(device->fd); } - close(device->fd); kfree(device->name); kfree(device->label); kfree(device); diff --git a/volumes.c b/volumes.c index d6f81f8..a84ded7 100644 --- a/volumes.c +++ b/volumes.c @@ -1628,10 +1628,10 @@ static int read_one_dev(struct btrfs_root *root, if (!device) { printk("warning devid %llu not found already\n", (unsigned long long)devid); - device = kmalloc(sizeof(*device), GFP_NOFS); + device = kzalloc(sizeof(*device), GFP_NOFS); if (!device) return -ENOMEM; - device->total_ios = 0; + device->fd = -1; list_add(&device->dev_list, &root->fs_info->fs_devices->devices); }