From: Hin-Tak Leung Subject: [PATCH] hfs: fix B-tree corruption after insertion at position 0 Date: Sun, 28 Jun 2015 02:39:30 +0100 Message-ID: <1435455570-8577-3-git-send-email-HinTak.Leung@gmail.com> References: <1435455570-8577-1-git-send-email-HinTak.Leung@gmail.com> Cc: Hin-Tak Leung , Sergei Antonov , Joe Perches , Al Viro , Christoph Hellwig , Andrew Morton To: linux-fsdevel@vger.kernel.org Return-path: Received: from mail-wi0-f177.google.com ([209.85.212.177]:35292 "EHLO mail-wi0-f177.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751712AbbF1Bjp (ORCPT ); Sat, 27 Jun 2015 21:39:45 -0400 Received: by wiga1 with SMTP id a1so44510741wig.0 for ; Sat, 27 Jun 2015 18:39:44 -0700 (PDT) In-Reply-To: <1435455570-8577-1-git-send-email-HinTak.Leung@gmail.com> Sender: linux-fsdevel-owner@vger.kernel.org List-ID: From: Hin-Tak Leung Fix B-tree corruption when a new record is inserted at position 0 in the node in hfs_brec_insert(). This is an identical change to the corresponding hfs b-tree code to Sergei Antonov's "hfsplus: fix B-tree corruption after insertion at position 0", to keep similar code paths in the hfs and hfsplus drivers in sync, where appropriate. Signed-off-by: Hin-Tak Leung Cc: Sergei Antonov Cc: Joe Perches Reviewed-by: Vyacheslav Dubeyko Anton Altaparmakov Cc: Al Viro Cc: Christoph Hellwig Cc: Andrew Morton --- fs/hfs/brec.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/fs/hfs/brec.c b/fs/hfs/brec.c index 9f4ee7f..6fc766d 100644 --- a/fs/hfs/brec.c +++ b/fs/hfs/brec.c @@ -131,13 +131,16 @@ skip: hfs_bnode_write(node, entry, data_off + key_len, entry_len); hfs_bnode_dump(node); - if (new_node) { - /* update parent key if we inserted a key - * at the start of the first node - */ - if (!rec && new_node != node) - hfs_brec_update_parent(fd); + /* + * update parent key if we inserted a key + * at the start of the node and it is not the new node + */ + if (!rec && new_node != node) { + hfs_bnode_read_key(node, fd->search_key, data_off + size); + hfs_brec_update_parent(fd); + } + if (new_node) { hfs_bnode_put(fd->bnode); if (!new_node->parent) { hfs_btree_inc_height(tree); @@ -166,9 +169,6 @@ skip: goto again; } - if (!rec) - hfs_brec_update_parent(fd); - return 0; } @@ -366,6 +366,8 @@ again: if (IS_ERR(parent)) return PTR_ERR(parent); __hfs_brec_find(parent, fd); + if (fd->record < 0) + return -ENOENT; hfs_bnode_dump(parent); rec = fd->record; -- 2.4.3