From patchwork Mon Feb 9 20:03:07 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 5803111 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.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id DC3A3BF6C3 for ; Mon, 9 Feb 2015 20:03:42 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 1247D2011D for ; Mon, 9 Feb 2015 20:03:42 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 16BE520125 for ; Mon, 9 Feb 2015 20:03:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1760755AbbBIUDh (ORCPT ); Mon, 9 Feb 2015 15:03:37 -0500 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:3382 "EHLO mx0b-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1760733AbbBIUDf (ORCPT ); Mon, 9 Feb 2015 15:03:35 -0500 Received: from pps.filterd (m0004060 [127.0.0.1]) by mx0b-00082601.pphosted.com (8.14.5/8.14.5) with SMTP id t19K0k1M024252; Mon, 9 Feb 2015 12:03:32 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=wit.ai; h=from : to : subject : date : message-id : in-reply-to : references : mime-version : content-type; s=mx2; bh=fHcInKlrn+PZKWDF4K2PcsGg1wzK3drz3R2sf651vAg=; b=C1n9yUUr3poaEGMOy1jfHDMOO+OKl7FpWtU2UdCLo9fxolURsLU6/8N+C1EWX+GseOG+ 5Nd+uttpi0x4eG/XJ+rS9Ycm51dzvopcnc0zUw6Jxbp2Nl2WgxE7C1Y+7H77oW0iCpZu YkpSqcH3OSaSnYBX/+Fa5J9SHUrdZTTlYuZM0VICk0+1vy9+j0Q8vgvY05932V/NNKVT 2SIvkLijbEtFgPpbCsM4NCKKOG4rAbVba6U/Cp2e8fjKgd2hgH3vGfFE9qSiPLHuxVaW P1YQJhJ+piGeQ20rtPzDX+Kt/RzhZLiFZTbm39dLTMgwnJ9QshS1xeatH5eybv88zKKd 1Q== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=fb.com; h=from : to : subject : date : message-id : in-reply-to : references : mime-version : content-type; s=facebook; bh=fHcInKlrn+PZKWDF4K2PcsGg1wzK3drz3R2sf651vAg=; b=W4gBUEEXyrbaT9O5KH+4mA0b0xNBuIwvEIVyzJpVBRzngT+Bn7mwu/U8GocMr+fXfumn 14IOyLZyYIuM83plDMrbMx5qxCV8tQ3tKyAnsGrz2Vry60U+/8+COyHrOdRA8XkUO1YZ FyXu/a99vH6UCpADzr9dLdqQ/YjRzdMducI= Received: from mail.thefacebook.com ([199.201.64.23]) by mx0b-00082601.pphosted.com with ESMTP id 1sf303853r-1 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NOT); Mon, 09 Feb 2015 12:03:32 -0800 Received: from localhost (192.168.57.29) by mail.thefacebook.com (192.168.16.22) with Microsoft SMTP Server (TLS) id 14.3.195.1; Mon, 9 Feb 2015 12:03:30 -0800 From: Josef Bacik To: , Subject: [PATCH 04/16] Btrfs-progs: read super properly in btrfs-image Date: Mon, 9 Feb 2015 15:03:07 -0500 Message-ID: <1423512199-16552-5-git-send-email-jbacik@fb.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1423512199-16552-1-git-send-email-jbacik@fb.com> References: <1423512199-16552-1-git-send-email-jbacik@fb.com> MIME-Version: 1.0 X-Originating-IP: [192.168.57.29] X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:5.13.68, 1.0.33, 0.0.0000 definitions=2015-02-09_02:2015-02-09, 2015-02-08, 1970-01-01 signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 kscore.is_bulkscore=0 kscore.compositescore=0 circleOfTrustscore=2.00421954885187 compositescore=0.980601336099369 urlsuspect_oldscore=0.980601336099369 suspectscore=2 recipient_domain_to_sender_totalscore=0 phishscore=0 bulkscore=0 kscore.is_spamscore=0 recipient_to_sender_totalscore=0 recipient_domain_to_sender_domain_totalscore=62764 rbsscore=0.980601336099369 spamscore=0 recipient_to_sender_domain_totalscore=0 urlsuspectscore=0.9 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=7.0.1-1402240000 definitions=main-1502090197 X-FB-Internal: deliver 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.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID,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 When btrfs-image makes a metadump it'll map all the blocks from their logical address to their physical. This works out fine with the exception of the super block, which is the physical offset. Normally this just works, but if the user has balanced their fs it'll either crash btrfs-image or it'll copy some completely arbitrary data. This forces btrfs-image to read the super directly from the disk. Thanks, Signed-off-by: Josef Bacik --- btrfs-image.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/btrfs-image.c b/btrfs-image.c index f6347f3..4bcaf6c 100644 --- a/btrfs-image.c +++ b/btrfs-image.c @@ -868,6 +868,15 @@ static int read_data_extent(struct metadump_struct *md, return 0; } +static int get_dev_fd(struct btrfs_root *root) +{ + struct btrfs_device *dev; + + dev = list_first_entry(&root->fs_info->fs_devices->devices, + struct btrfs_device, dev_list); + return dev->fd; +} + static int flush_pending(struct metadump_struct *md, int done) { struct async_work *async = NULL; @@ -904,6 +913,24 @@ static int flush_pending(struct metadump_struct *md, int done) } } + /* + * Balance can make the mapping not cover the super block, so + * just copy directly from one of the devices. + */ + if (start == BTRFS_SUPER_INFO_OFFSET) { + int fd = get_dev_fd(md->root); + + ret = pread64(fd, async->buffer, size, start); + if (ret < size) { + free(async->buffer); + free(async); + fprintf(stderr, "Error reading superblock\n"); + return -EIO; + } + size = 0; + ret = 0; + } + while (!md->data && size > 0) { u64 this_read = min(blocksize, size); eb = read_tree_block(md->root, start, this_read, 0);