Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp8242330imu; Fri, 28 Dec 2018 13:31:26 -0800 (PST) X-Google-Smtp-Source: ALg8bN7HpTK/Azg/Skk7YMtjv/vHNJPCFEq+Ahdy6LxmjHjvZ0HLOoaQRcpQ0/7h/K4QP10Qk7hU X-Received: by 2002:a65:448a:: with SMTP id l10mr27842102pgq.387.1546032686069; Fri, 28 Dec 2018 13:31:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1546032686; cv=none; d=google.com; s=arc-20160816; b=QSdeIgW0rfpBX13TAbbNUutfKaALqbzhOg9d4oKk9ayzTuXm8GMZmWsiYLhpUDnMPl 3wMlMWXhDV6AOB3zobT7/yrsVf2AVpKJ+lEsYFHuiyBc0kyc0YVtSF8m8xvMRJ6bcVoK jh4O5NbWQGEOPORh/0wgBreNZ8JEy6/RCzKFT3GiUauvckyIbzolXtFwr6ymsJoKZMIn jWXbTFNUg40b5A6TeM6TwKysvGlnvinNh4FuM2nMXF9dIIEbRjW1buMpnWxq64+XK8Wq r/yFIM0+0cOGRWJHW4CT5eaSPviKaQMF7zxQIAkudIftwgsvAdr7O10ZqZaP8QTxEHDn Az7Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=57XTzm4akdHJoUlg8/uMG5w7VgqE/5cJlgTw/WStw/g=; b=Jfp/fh5L8UYVF6Nb+aVNpReV8UY5xDf+Kjz4FxgN7h0awMcMXmcPwflC7XtQuYkADO db9DuCO/7UEvzlLM0rGhjw5MNbOqkP5lsAwML9jCJu0FHXf+a8kkq9EpZrZ41z3aTtp+ EBSV5T4JUuAspuAqagfqeYR/inX2xQH7BUMZ1MEOpwE+qKHn8y4ZJnqd1CpAVO1o3PnP /UxIBmGYByJp2RXXgeU+xNlGou36lXv8a78Z7CstOL9LATvQOEBBl27VihRwZWfMzGbe RHCLn8a67shJvdRnY2RIywe/2F7dowYtRNYzWUwpc8SlbLg74xurnECaLKWgDAINc0tI 9yYw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=0Tg1KGAQ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z14si25057051pgz.180.2018.12.28.13.31.10; Fri, 28 Dec 2018 13:31:26 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=0Tg1KGAQ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731076AbeL1Lyr (ORCPT + 99 others); Fri, 28 Dec 2018 06:54:47 -0500 Received: from mail.kernel.org ([198.145.29.99]:52984 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727861AbeL1LxN (ORCPT ); Fri, 28 Dec 2018 06:53:13 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id D3B3020879; Fri, 28 Dec 2018 11:53:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1545997992; bh=B8j22I0KA4+BK//NemH6qTz54PQvB0mpWDIJI22vuRc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=0Tg1KGAQdcnkJjUeq4rUf7D+iF31jUNvXvZIgpHcnwvz82Yt/N39M7S9cINujjuUg QbkAfcgp5U/yIqRRIkGV8QNqtXVmVBAgnUsp7rmQgT/U8bLlgF3vCWc0ZqXv5sTNgd o8P9B6bXuGNUzClNbOgc6K+/D7ZVMpaIyALInpng= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Russell Senior , =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= , =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= , Richard Weinberger Subject: [PATCH 4.19 11/46] ubifs: Handle re-linking of inodes correctly while recovery Date: Fri, 28 Dec 2018 12:52:05 +0100 Message-Id: <20181228113125.452002715@linuxfoundation.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20181228113124.971620049@linuxfoundation.org> References: <20181228113124.971620049@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.19-stable review patch. If anyone has any objections, please let me know. ------------------ From: Richard Weinberger commit e58725d51fa8da9133f3f1c54170aa2e43056b91 upstream. UBIFS's recovery code strictly assumes that a deleted inode will never come back, therefore it removes all data which belongs to that inode as soon it faces an inode with link count 0 in the replay list. Before O_TMPFILE this assumption was perfectly fine. With O_TMPFILE it can lead to data loss upon a power-cut. Consider a journal with entries like: 0: inode X (nlink = 0) /* O_TMPFILE was created */ 1: data for inode X /* Someone writes to the temp file */ 2: inode X (nlink = 0) /* inode was changed, xattr, chmod, … */ 3: inode X (nlink = 1) /* inode was re-linked via linkat() */ Upon replay of entry #2 UBIFS will drop all data that belongs to inode X, this will lead to an empty file after mounting. As solution for this problem, scan the replay list for a re-link entry before dropping data. Fixes: 474b93704f32 ("ubifs: Implement O_TMPFILE") Cc: stable@vger.kernel.org Cc: Russell Senior Cc: Rafał Miłecki Reported-by: Russell Senior Reported-by: Rafał Miłecki Tested-by: Rafał Miłecki Signed-off-by: Richard Weinberger Signed-off-by: Greg Kroah-Hartman --- fs/ubifs/replay.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) --- a/fs/ubifs/replay.c +++ b/fs/ubifs/replay.c @@ -210,6 +210,38 @@ static int trun_remove_range(struct ubif } /** + * inode_still_linked - check whether inode in question will be re-linked. + * @c: UBIFS file-system description object + * @rino: replay entry to test + * + * O_TMPFILE files can be re-linked, this means link count goes from 0 to 1. + * This case needs special care, otherwise all references to the inode will + * be removed upon the first replay entry of an inode with link count 0 + * is found. + */ +static bool inode_still_linked(struct ubifs_info *c, struct replay_entry *rino) +{ + struct replay_entry *r; + + ubifs_assert(c, rino->deletion); + ubifs_assert(c, key_type(c, &rino->key) == UBIFS_INO_KEY); + + /* + * Find the most recent entry for the inode behind @rino and check + * whether it is a deletion. + */ + list_for_each_entry_reverse(r, &c->replay_list, list) { + ubifs_assert(c, r->sqnum >= rino->sqnum); + if (key_inum(c, &r->key) == key_inum(c, &rino->key)) + return r->deletion == 0; + + } + + ubifs_assert(c, 0); + return false; +} + +/** * apply_replay_entry - apply a replay entry to the TNC. * @c: UBIFS file-system description object * @r: replay entry to apply @@ -236,6 +268,11 @@ static int apply_replay_entry(struct ubi { ino_t inum = key_inum(c, &r->key); + if (inode_still_linked(c, r)) { + err = 0; + break; + } + err = ubifs_tnc_remove_ino(c, inum); break; }