From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.1 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4A0DDC43387 for ; Wed, 26 Dec 2018 12:32:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 13B57214AE for ; Wed, 26 Dec 2018 12:32:22 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Q/wtXfdl" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726716AbeLZMcW (ORCPT ); Wed, 26 Dec 2018 07:32:22 -0500 Received: from mail-lj1-f196.google.com ([209.85.208.196]:35773 "EHLO mail-lj1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726713AbeLZMcW (ORCPT ); Wed, 26 Dec 2018 07:32:22 -0500 Received: by mail-lj1-f196.google.com with SMTP id x85-v6so13781761ljb.2 for ; Wed, 26 Dec 2018 04:32:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=WfAX+F3zx31OthIAlb8SIz5b1LGCq7BLFzjEKtHjjLM=; b=Q/wtXfdlpRHzHu5CyuHLZZUEErOoC2sQ6R4aEfAJurPHb4K1WHSVDjZ1dZkflUhTEB eKGf+ymfxuxE+cLno7mb9vc29scKT3DmRXAmBeNPutQpJKfWgPTnDi/iVfPTIpzKxmp0 Gogl8ys1Up0dL1dtSAN+ZkBd9YH/CTkuVmycxchIHKaPw/JfxsWPSVaIr+PKJf7/qyod iz4IkjxPLtSFjy2lV0HsVM9BTWClTaWjbikcRl0JPrClKJwwmEum76aILQE0GaehP4by NvwRSklck6r0TlQEIJu6e4nq5e8f7ODeJr3xjgTodWXFMJ9aDiC7Wv6/MungX5QIo0DL 43KA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=WfAX+F3zx31OthIAlb8SIz5b1LGCq7BLFzjEKtHjjLM=; b=ruAviJgz6/NJ0rmGXWCy92eRbGkxOGxN58NcGJssO1InfnFuXdh+G9Vw/G8auSkp6U uMb6/4MBLLfzbiL3v0Ch+zQTL4JJ4w0Dw+iQ32yQJMgE2aYbGf3crx3Y+S6VhGa1SGXf esVFxyMl0zLIG3N4T3dm/Bcu9ubW+5tA5h4INvZiXBtkhB09MqUv7HG6/l4FaYmIU0by swgaHgFoKJbTPuHiYdL2rUBqYcfctl0YSx+/Bl2VpM2y5hlgcLVzuaDgv3BFNY7+b/ip rGkPiIqE4mMinOEjd1dz825OUXUK3PnSXrVOu6Z0rcgYicYq2TjwAGni8D/jGCcHgjaF Lv7A== X-Gm-Message-State: AJcUukftvE/75icZUimI8VRynhV2ZSsmqn3PCzweXRlBo6Cp2MuGsuWe Hry0k+9coW5+otP0yW66YSR9d7mi X-Google-Smtp-Source: ALg8bN6X2n6v2jKuuTNXDMEJ4i9C0qOh7fj4NoSkaoy0Oj437QQhUuL5gDQxlUyMUPtcfW/Ex5sYsw== X-Received: by 2002:a2e:8786:: with SMTP id n6-v6mr421260lji.100.1545827538418; Wed, 26 Dec 2018 04:32:18 -0800 (PST) Received: from linux-veee.lan (ip-194-187-74-233.konfederacka.maverick.com.pl. [194.187.74.233]) by smtp.gmail.com with ESMTPSA id e19-v6sm7558359ljf.67.2018.12.26.04.32.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 26 Dec 2018 04:32:17 -0800 (PST) From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , Richard Weinberger , =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= , Russell Senior , =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= Subject: [PATCH stable] ubifs: Handle re-linking of inodes correctly while recovery Date: Wed, 26 Dec 2018 13:32:11 +0100 Message-Id: <20181226123211.24969-1-zajec5@gmail.com> X-Mailer: git-send-email 2.13.7 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: stable-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org 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 # 4.9-4.18 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 [rmilecki: update ubifs_assert() calls to compile with 4.18 and older] Signed-off-by: Rafał Miłecki (cherry picked from commit e58725d51fa8da9133f3f1c54170aa2e43056b91) --- fs/ubifs/replay.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/fs/ubifs/replay.c b/fs/ubifs/replay.c index ae5c02f22f3e..d998fbf7de30 100644 --- a/fs/ubifs/replay.c +++ b/fs/ubifs/replay.c @@ -210,6 +210,38 @@ static int trun_remove_range(struct ubifs_info *c, struct replay_entry *r) } /** + * 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(rino->deletion); + ubifs_assert(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(r->sqnum >= rino->sqnum); + if (key_inum(c, &r->key) == key_inum(c, &rino->key)) + return r->deletion == 0; + + } + + ubifs_assert(0); + return false; +} + +/** * apply_replay_entry - apply a replay entry to the TNC. * @c: UBIFS file-system description object * @r: replay entry to apply @@ -239,6 +271,11 @@ static int apply_replay_entry(struct ubifs_info *c, struct replay_entry *r) { 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; } -- 2.13.7