From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Cyrus-Session-Id: sloti22d1t05-901460-1516769596-2-8879499460670395437 X-Sieve: CMU Sieve 3.0 X-Spam-known-sender: no X-Spam-score: 0.0 X-Spam-hits: BAYES_00 -1.9, HEADER_FROM_DIFFERENT_DOMAINS 0.25, RCVD_IN_DNSWL_HI -5, T_RP_MATCHES_RCVD -0.01, LANGUAGES en, BAYES_USED global, SA_VERSION 3.4.0 X-Spam-source: IP='209.132.180.67', Host='vger.kernel.org', Country='US', FromHeader='com', MailFrom='org', XOriginatingCountry='US' X-Spam-charsets: plain='iso-8859-1' X-Resolved-to: greg@kroah.com X-Delivered-to: greg@kroah.com X-Mail-from: stable-owner@vger.kernel.org ARC-Seal: i=1; a=rsa-sha256; cv=none; d=messagingengine.com; s=arctest; t=1516769596; b=E3qUxB4W0iBiwy5d3EYtzk6ON7RM7UvvnHQRjSSl4+nbwvJ kS5q8VN8SPw5D0rQAPS8lyYhHompwyJFryh57lFtLO2lzyINDsEiJktiixPM1rEW sMqxUvDawI6qfl6vYfzm4hEmqjtddFnN8ZJYwcMY9t1oNz0Vyms17FdILVEdtqyq ue2Ycyeq094OrKQ6Kzb6iGB28mfDoJ8KOxm+UP1q/ev87+FKYVUa3cOCBGRE8tPg 58tDgSwOUB1zV/GeGTEveacS0HDQGnGaFE2RSIilZrvxNiXoF5IYkgVWA75njbD8 xO8CwZpVl4BAVx43kCmRAwqbo7zzc1xz7LECLBw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=from:to:cc:subject:date:message-id :references:in-reply-to:content-type:content-transfer-encoding :mime-version:sender:list-id; s=arctest; t=1516769596; bh=hdWHlZ knW66b/ZNOw7/aycy6mG8llWtEjH0FPRRD4EA=; b=q9aEz2MDr7iDUQydbGt605 wVBcBimMJx+txgTUn9KFqFaGzHt35OE2beSfJcP9I8h2FvFyiC0lB4pW9fQ5QuKr XZ+hc9gpvnTN0yeqRNVZtvdSb2UrJ0fv7UjdA7Nb6EDgBdexfVwJ32qnoVOgiJNb golCRmvvRr90kYh/L32eJf3no7OM8kbFze7RXBD4cDMLIsLKsX2Vv4ebJvaMAOHM LUj6FWXEmTZHIWnsTLA6gSm8T8H7NaglmM797WI1AE5S0NvW62bpkeGYczF0vDie NPwJ1hIVJlVwqyaS5G2AeISEURFacbysZcSBwh+RZHBH9S3OoPTY10X2nKkFGr/Q == ARC-Authentication-Results: i=1; mx5.messagingengine.com; arc=none (no signatures found); dkim=pass (1024-bit rsa key sha256) header.d=microsoft.com header.i=@microsoft.com header.b=JgL2yqt7 x-bits=1024 x-keytype=rsa x-algorithm=sha256 x-selector=selector1; dmarc=pass (p=reject,has-list-id=yes,d=none) header.from=microsoft.com; iprev=pass policy.iprev=209.132.180.67 (vger.kernel.org); spf=none smtp.mailfrom=stable-owner@vger.kernel.org smtp.helo=vger.kernel.org; x-aligned-from=fail; x-ptr=pass x-ptr-helo=vger.kernel.org x-ptr-lookup=vger.kernel.org; x-return-mx=pass smtp.domain=vger.kernel.org smtp.result=pass smtp_org.domain=kernel.org smtp_org.result=pass smtp_is_org_domain=no header.domain=microsoft.com header.result=pass header_is_org_domain=yes Authentication-Results: mx5.messagingengine.com; arc=none (no signatures found); dkim=pass (1024-bit rsa key sha256) header.d=microsoft.com header.i=@microsoft.com header.b=JgL2yqt7 x-bits=1024 x-keytype=rsa x-algorithm=sha256 x-selector=selector1; dmarc=pass (p=reject,has-list-id=yes,d=none) header.from=microsoft.com; iprev=pass policy.iprev=209.132.180.67 (vger.kernel.org); spf=none smtp.mailfrom=stable-owner@vger.kernel.org smtp.helo=vger.kernel.org; x-aligned-from=fail; x-ptr=pass x-ptr-helo=vger.kernel.org x-ptr-lookup=vger.kernel.org; x-return-mx=pass smtp.domain=vger.kernel.org smtp.result=pass smtp_org.domain=kernel.org smtp_org.result=pass smtp_is_org_domain=no header.domain=microsoft.com header.result=pass header_is_org_domain=yes Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932485AbeAXExO (ORCPT ); Tue, 23 Jan 2018 23:53:14 -0500 Received: from mail-by2nam03on0132.outbound.protection.outlook.com ([104.47.42.132]:45413 "EHLO NAM03-BY2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S932431AbeAXEP6 (ORCPT ); Tue, 23 Jan 2018 23:15:58 -0500 From: Sasha Levin To: "linux-kernel@vger.kernel.org" , "stable@vger.kernel.org" CC: Filipe Manana , David Sterba , "Sasha Levin" Subject: [PATCH AUTOSEL for 4.14 069/100] Btrfs: incremental send, fix wrong unlink path after renaming file Thread-Topic: [PATCH AUTOSEL for 4.14 069/100] Btrfs: incremental send, fix wrong unlink path after renaming file Thread-Index: AQHTlMnmaAVGmD2oz06lnP/izJybMQ== Date: Wed, 24 Jan 2018 04:15:00 +0000 Message-ID: <20180124041414.32065-69-alexander.levin@microsoft.com> References: <20180124041414.32065-1-alexander.levin@microsoft.com> In-Reply-To: <20180124041414.32065-1-alexander.levin@microsoft.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [52.168.54.252] x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1;DM5PR2101MB1078;6:jsdMoXzYGX15+ofe+rnQMincw/HK2KRffrRq9hbE/fJovcMB/JdH1RNktnSFHPfEMpr/eMVHIXvkyQvje2gipUOsTBYgdfEHrZwHKqo6qo83whi5+6cqiv+uVVFLF2j6bQ0YUkFwuVVkm2Z9gcaYO8dzs5frbmyJfDJ85o6Y6Snwf11ZA5Jkc/hLNg9EpRdmenwcL0KM+wXN8UTqc0/H4Cheyl+fv2LVezJFD0VyvWCs4ezgnXQtbdKYYNVtzcJPMVV1g4WVGd3nz/gd4p527Y/W6G40qzVa6qsLE8uGDb6rnGxJAPH/nukoUIq41vqdP31HNUr4XkryQ9VEVPgzMTDWl3pdOLRgNT0cddruOIvPceFnGCva/BKvmYAn99c6;5:7rw7dCvJn5m0VNHcS5koosX4jdESKmC08biuRK5zyLHZhlnO29yQKr2gAFdnfAhmELZLEtrb2X1rqFlmtab1wJqA3ZxpEy9OIAjeW2sWpkuioEYKDaVKSAge7nsmWWfpSS5ZGk9FkGAYZ7GwkKzbajW7jwnU3Cut02BCXnxoCD8=;24:ZXG3IrkvR1Bxhsu/1EnFt4WvOw/C+ETTSGMRnxVC1kQAfMgi28UGGMNysAWmoJM1nyca8EZ2FHZ5Im+IRNr/VLS04yqru3B5CGcCwmE5LYc=;7:jhqM8FSxRTiSy74gaHRbX/wzKvqfiZPb11qoi4+hyYsjWkNP2VCUERpjabhnoEw/QePCqMwGtK+tu2sJFases0pha6sThYXG+UXrLQUaR75/d9DwqgzqDqOG0Lpb72r1TyWBEKw3frn4GUqawSCpKh4tqAwvr2fy/FE+5xapPhC7FuGz1eD+WyBNeGsWVErDP6j1IKuhoR/i0MDbX9dZ1Ks7H6zP+w0CnCbHWHlq7mJwZldYd8l2PNpcf3MhIoEA x-ms-office365-filtering-correlation-id: 2ee76ad1-3a8f-467f-6e9d-08d562e12a91 x-ms-office365-filtering-ht: Tenant x-microsoft-antispam: UriScan:;BCL:0;PCL:0;RULEID:(7020095)(4652020)(5600026)(4604075)(3008032)(4534165)(4627221)(201703031133081)(201702281549075)(48565401081)(2017052603307)(7193020);SRVR:DM5PR2101MB1078; x-ms-traffictypediagnostic: DM5PR2101MB1078: x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:(28532068793085)(89211679590171); x-exchange-antispam-report-cfa-test: BCL:0;PCL:0;RULEID:(61425038)(6040501)(2401047)(5005006)(8121501046)(93006095)(93001095)(3002001)(10201501046)(3231046)(2400081)(944501161)(6055026)(61426038)(61427038)(6041288)(20161123558120)(20161123562045)(20161123564045)(20161123560045)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(6072148)(201708071742011);SRVR:DM5PR2101MB1078;BCL:0;PCL:0;RULEID:;SRVR:DM5PR2101MB1078; x-forefront-prvs: 056297E276 x-forefront-antispam-report: SFV:NSPM;SFS:(10019020)(376002)(346002)(39860400002)(366004)(396003)(39380400002)(199004)(189003)(86612001)(22452003)(6436002)(106356001)(107886003)(102836004)(105586002)(305945005)(5250100002)(2501003)(14454004)(3280700002)(4326008)(7736002)(97736004)(59450400001)(6512007)(36756003)(8936002)(2950100002)(3846002)(6116002)(6506007)(86362001)(54906003)(76176011)(110136005)(2906002)(6666003)(53936002)(66066001)(72206003)(6346003)(5660300001)(8676002)(478600001)(10290500003)(68736007)(81156014)(10090500001)(1076002)(3660700001)(25786009)(316002)(2900100001)(99286004)(26005)(6486002)(81166006)(22906009)(217873001);DIR:OUT;SFP:1102;SCL:1;SRVR:DM5PR2101MB1078;H:DM5PR2101MB1032.namprd21.prod.outlook.com;FPR:;SPF:None;PTR:InfoNoRecords;A:1;MX:1;LANG:en; authentication-results: spf=none (sender IP is ) smtp.mailfrom=Alexander.Levin@microsoft.com; x-microsoft-antispam-message-info: G5zVyfmkpsoXzI9FL9ZAeQPhqqCpvKT/M0G9xG94z0ErLmyRQ2pZGNULldDWhmJLoZKBODAC5RbghPzzNT7Bpg== spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: microsoft.com X-MS-Exchange-CrossTenant-Network-Message-Id: 2ee76ad1-3a8f-467f-6e9d-08d562e12a91 X-MS-Exchange-CrossTenant-originalarrivaltime: 24 Jan 2018 04:15:00.3203 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 72f988bf-86f1-41af-91ab-2d7cd011db47 X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR2101MB1078 Sender: stable-owner@vger.kernel.org X-Mailing-List: stable@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-Mailing-List: linux-kernel@vger.kernel.org List-ID: From: Filipe Manana [ Upstream commit ea37d5998b50a72b9045ba60a132eeb20e1c4230 ] Under some circumstances, an incremental send operation can issue wrong paths for unlink commands related to files that have multiple hard links and some (or all) of those links were renamed between the parent and send snapshots. Consider the following example: Parent snapshot . (ino 256) |---- a/ (ino 257) | |---- b/ (ino 259) | | |---- c/ (ino 260) | | |---- f2 (ino 261) | | | |---- f2l1 (ino 261) | |---- d/ (ino 262) |---- f1l1_2 (ino 258) |---- f2l2 (ino 261) |---- f1_2 (ino 258) Send snapshot . (ino 256) |---- a/ (ino 257) | |---- f2l1/ (ino 263) | |---- b2/ (ino 259) | |---- c/ (ino 260) | | |---- d3 (ino 262) | | |---- f1l1_2 (ino 258) | | |---- f2l2_2 (ino 261) | | |---- f1_2 (ino 258) | | | |---- f2 (ino 261) | |---- f1l2 (ino 258) | |---- d (ino 261) When computing the incremental send stream the following steps happen: 1) When processing inode 261, a rename operation is issued that renames inode 262, which currently as a path of "d", to an orphan name of "o262-7-0". This is done because in the send snapshot, inode 261 has of its hard links with a path of "d" as well. 2) Two link operations are issued that create the new hard links for inode 261, whose names are "d" and "f2l2_2", at paths "/" and "o262-7-0/" respectively. 3) Still while processing inode 261, unlink operations are issued to remove the old hard links of inode 261, with names "f2l1" and "f2l2", at paths "a/" and "d/". However path "d/" does not correspond anymore to the directory inode 262 but corresponds instead to a hard link of inode 261 (link command issued in the previous step). This makes the receiver fail with a ENOTDIR error when attempting the unlink operation. The problem happens because before sending the unlink operation, we failed to detect that inode 262 was one of ancestors for inode 261 in the parent snapshot, and therefore we didn't recompute the path for inode 262 before issuing the unlink operation for the link named "f2l2" of inode 262. The detection failed because the function "is_ancestor()" only follows the first hard link it finds for an inode instead of all of its hard links (as it was originally created for being used with directories only, for which only one hard link exists). So fix this by making "is_ancestor()" follow all hard links of the input inode. A test case for fstests follows soon. Signed-off-by: Filipe Manana Signed-off-by: David Sterba Signed-off-by: Sasha Levin --- fs/btrfs/send.c | 124 ++++++++++++++++++++++++++++++++++++++++++++++++----= ---- 1 file changed, 106 insertions(+), 18 deletions(-) diff --git a/fs/btrfs/send.c b/fs/btrfs/send.c index 8fd195cfe81b..2c35717a3470 100644 --- a/fs/btrfs/send.c +++ b/fs/btrfs/send.c @@ -3527,7 +3527,40 @@ out: } =20 /* - * Check if ino ino1 is an ancestor of inode ino2 in the given root. + * Check if inode ino2, or any of its ancestors, is inode ino1. + * Return 1 if true, 0 if false and < 0 on error. + */ +static int check_ino_in_path(struct btrfs_root *root, + const u64 ino1, + const u64 ino1_gen, + const u64 ino2, + const u64 ino2_gen, + struct fs_path *fs_path) +{ + u64 ino =3D ino2; + + if (ino1 =3D=3D ino2) + return ino1_gen =3D=3D ino2_gen; + + while (ino > BTRFS_FIRST_FREE_OBJECTID) { + u64 parent; + u64 parent_gen; + int ret; + + fs_path_reset(fs_path); + ret =3D get_first_ref(root, ino, &parent, &parent_gen, fs_path); + if (ret < 0) + return ret; + if (parent =3D=3D ino1) + return parent_gen =3D=3D ino1_gen; + ino =3D parent; + } + return 0; +} + +/* + * Check if ino ino1 is an ancestor of inode ino2 in the given root for an= y + * possible path (in case ino2 is not a directory and has multiple hard li= nks). * Return 1 if true, 0 if false and < 0 on error. */ static int is_ancestor(struct btrfs_root *root, @@ -3536,36 +3569,91 @@ static int is_ancestor(struct btrfs_root *root, const u64 ino2, struct fs_path *fs_path) { - u64 ino =3D ino2; - bool free_path =3D false; + bool free_fs_path =3D false; int ret =3D 0; + struct btrfs_path *path =3D NULL; + struct btrfs_key key; =20 if (!fs_path) { fs_path =3D fs_path_alloc(); if (!fs_path) return -ENOMEM; - free_path =3D true; + free_fs_path =3D true; } =20 - while (ino > BTRFS_FIRST_FREE_OBJECTID) { - u64 parent; - u64 parent_gen; + path =3D alloc_path_for_send(); + if (!path) { + ret =3D -ENOMEM; + goto out; + } =20 - fs_path_reset(fs_path); - ret =3D get_first_ref(root, ino, &parent, &parent_gen, fs_path); - if (ret < 0) { - if (ret =3D=3D -ENOENT && ino =3D=3D ino2) - ret =3D 0; - goto out; + key.objectid =3D ino2; + key.type =3D BTRFS_INODE_REF_KEY; + key.offset =3D 0; + + ret =3D btrfs_search_slot(NULL, root, &key, path, 0, 0); + if (ret < 0) + goto out; + + while (true) { + struct extent_buffer *leaf =3D path->nodes[0]; + int slot =3D path->slots[0]; + u32 cur_offset =3D 0; + u32 item_size; + + if (slot >=3D btrfs_header_nritems(leaf)) { + ret =3D btrfs_next_leaf(root, path); + if (ret < 0) + goto out; + if (ret > 0) + break; + continue; } - if (parent =3D=3D ino1) { - ret =3D parent_gen =3D=3D ino1_gen ? 1 : 0; - goto out; + + btrfs_item_key_to_cpu(leaf, &key, slot); + if (key.objectid !=3D ino2) + break; + if (key.type !=3D BTRFS_INODE_REF_KEY && + key.type !=3D BTRFS_INODE_EXTREF_KEY) + break; + + item_size =3D btrfs_item_size_nr(leaf, slot); + while (cur_offset < item_size) { + u64 parent; + u64 parent_gen; + + if (key.type =3D=3D BTRFS_INODE_EXTREF_KEY) { + unsigned long ptr; + struct btrfs_inode_extref *extref; + + ptr =3D btrfs_item_ptr_offset(leaf, slot); + extref =3D (struct btrfs_inode_extref *) + (ptr + cur_offset); + parent =3D btrfs_inode_extref_parent(leaf, + extref); + cur_offset +=3D sizeof(*extref); + cur_offset +=3D btrfs_inode_extref_name_len(leaf, + extref); + } else { + parent =3D key.offset; + cur_offset =3D item_size; + } + + ret =3D get_inode_info(root, parent, NULL, &parent_gen, + NULL, NULL, NULL, NULL); + if (ret < 0) + goto out; + ret =3D check_ino_in_path(root, ino1, ino1_gen, + parent, parent_gen, fs_path); + if (ret) + goto out; } - ino =3D parent; + path->slots[0]++; } + ret =3D 0; out: - if (free_path) + btrfs_free_path(path); + if (free_fs_path) fs_path_free(fs_path); return ret; } --=20 2.11.0