From patchwork Wed Jun 26 16:41:36 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 2787291 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 E69B4C0AB1 for ; Wed, 26 Jun 2013 16:42:12 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id B8A0C205CC for ; Wed, 26 Jun 2013 16:42:11 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 0D9C2205C9 for ; Wed, 26 Jun 2013 16:42:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751731Ab3FZQmD (ORCPT ); Wed, 26 Jun 2013 12:42:03 -0400 Received: from mail-we0-f171.google.com ([74.125.82.171]:42088 "EHLO mail-we0-f171.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751359Ab3FZQmB (ORCPT ); Wed, 26 Jun 2013 12:42:01 -0400 Received: by mail-we0-f171.google.com with SMTP id m46so10589584wev.30 for ; Wed, 26 Jun 2013 09:41:59 -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=qTmJHW3zOLLjZiEfEv6u2ox8lvySpy4FTlPyt0pKbgA=; b=qo+cdUHL7alo/RxUSofUw9eDlwr3B9H05/ma0frnXJqsslUO9DZcLuub+iGnje4OiL aR1TcNeLJ981a5ojgs4inFtTxnrKxq3zd9HhTW30mGLN7iVvrmna98/IC3C+NB96mOM1 8bQCTJRuikRn1vPjz3w5mDLlGx7ouRuAmkem0Wyjnwg6K6zwi463zBPAvRPVOzQ4GAZi fAQ7H049Fo4EDfOROf+EMtBh2roHcWZpyDcfcUMRwfgoATuV45cUDBgiYFFnLcNnaRo0 IUI1i0R/OWrOoGYi0OMlb+vBDHcOibYFvbRKvgmo6dtokd3cpZ2IK4D4r/FkuoboFbg4 d45g== X-Received: by 10.180.185.175 with SMTP id fd15mr13080071wic.34.1372264919927; Wed, 26 Jun 2013 09:41:59 -0700 (PDT) Received: from storm-desktop.lan (bl11-94-62.dsl.telepac.pt. [85.244.94.62]) by mx.google.com with ESMTPSA id z6sm8110150wiv.11.2013.06.26.09.41.58 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 26 Jun 2013 09:41:59 -0700 (PDT) From: Filipe David Borba Manana To: linux-btrfs@vger.kernel.org Cc: Filipe David Borba Manana Subject: [PATCH v2 1/5] Btrfs-progs: fix closing of devices Date: Wed, 26 Jun 2013 17:41:36 +0100 Message-Id: <1372264896-10184-1-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 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 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 tried to sync, fadvise and close the (invalid) fd of the device, and kfree() its name and label, which pointed 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 v2: Added Liu Bo's review mention. Reviewed-by: Liu Bo Signed-off-by: Filipe David Borba Manana --- disk-io.c | 4 ++-- volumes.c | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/disk-io.c b/disk-io.c index 9ffe6e4..768adda 100644 --- a/disk-io.c +++ b/disk-io.c @@ -1270,12 +1270,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..061f094 100644 --- a/volumes.c +++ b/volumes.c @@ -116,6 +116,7 @@ static int device_list_add(const char *path, /* we can safely leave the fs_devices entry around */ return -ENOMEM; } + device->fd = -1; device->devid = devid; memcpy(device->uuid, disk_super->dev_item.uuid, BTRFS_UUID_SIZE); @@ -1628,10 +1629,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); }