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=-12.6 required=3.0 tests=BAYES_00,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_HELO_NONE,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 CEDBAC388F7 for ; Tue, 3 Nov 2020 15:07:43 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8D2E32242C for ; Tue, 3 Nov 2020 15:07:43 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="TteGliNL" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727742AbgKCPHm (ORCPT ); Tue, 3 Nov 2020 10:07:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53070 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728174AbgKCPG6 (ORCPT ); Tue, 3 Nov 2020 10:06:58 -0500 Received: from mail-pg1-x544.google.com (mail-pg1-x544.google.com [IPv6:2607:f8b0:4864:20::544]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2ED56C0613D1 for ; Tue, 3 Nov 2020 07:06:58 -0800 (PST) Received: by mail-pg1-x544.google.com with SMTP id g12so13888772pgm.8 for ; Tue, 03 Nov 2020 07:06:58 -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:in-reply-to:references :mime-version:content-transfer-encoding; bh=XmdKpghfLeanbz+IwL0BYKqdxi1X0QN/uaPdOmARGkM=; b=TteGliNLrGJSEYX5GyBB/K9nWZArrfxIwXaMYRMeYy7k+NGdZGC3Z8wAnF9RzcIePh EqJLoOCzKd+gg/iX5lUqclx7QdjpdjVwGFEXnkxdORJmFfhYv4ncqaZYjTRNrtANZyq+ O9aXqdblhBYO73up9D7bSA6UkxvppXx3f/y1ql3LG8Pn73uGq+4+ljSYC0uiaKHngVSh fPYI5hXO6mebgEgYgZb8wUL1f2l01mr28Mlz1h/gHXCYPUbPuJLDqQHijdWwEG52Db2b vCL+vdKoCcpq7UCgDOaA3Es4bXj4ox4/6KicPcIJCn4ghp086G8bOU+FWlBTUguvN7yE IBtg== 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=XmdKpghfLeanbz+IwL0BYKqdxi1X0QN/uaPdOmARGkM=; b=Ty/WQ/toKEVU8Ifp/uyhsnmdfNgHjUrhOXooTl1WQs8EKuimTWbdLWFbpyZ0UoL51s 6t/UW8r6zAPkIQaKfMMuO3w6C/BwM0BO875xbYo/8rX0Vb5r98y4+P8eAHlG7cY2lisn cdK4l/FVM98PtOn7dvK1lzCDRqGca7qd6H5YB3XJ9JMlWJHW1v54/t3vJQAdPBYF0MTo i/USy5HwRgRuKy4H9JO7N2TPJCcpgpGfdpOFR4jzotp9yFeK2J6mjv+nZV5AVDUjYAx+ 9cKuK2yDi0Je8huRr2tNbkfhqNUwGsWNUPUCBvtMISydO26N8JmOLLTtNlnArobCGZOI /oeg== X-Gm-Message-State: AOAM532dAFZRSZf5x2MD4tJRobIb61zJ056aNnRDK7PY+padh+4GoJXv WWIQAnaJ42KusRiI4LFJ/BftIvvEZTvHfw== X-Google-Smtp-Source: ABdhPJwTBh+YKIpeqpemzNITtF7PypnMnYm5zSKJoZN+iVJjIgf2KGmId2TSw6zAleXrmRm1mn0raw== X-Received: by 2002:a62:92c5:0:b029:156:6a7f:ccff with SMTP id o188-20020a6292c50000b02901566a7fccffmr25889003pfd.39.1604416017427; Tue, 03 Nov 2020 07:06:57 -0800 (PST) Received: from localhost.localdomain ([122.179.48.228]) by smtp.gmail.com with ESMTPSA id 15sm15936955pgs.52.2020.11.03.07.06.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Nov 2020 07:06:56 -0800 (PST) From: Chandan Babu R To: linux-xfs@vger.kernel.org Cc: Chandan Babu R , darrick.wong@oracle.com, david@fromorbit.com, hch@infradead.org, allison.henderson@oracle.com, Christoph Hellwig Subject: [PATCH V10 01/14] xfs: Add helper for checking per-inode extent count overflow Date: Tue, 3 Nov 2020 20:36:29 +0530 Message-Id: <20201103150642.2032284-2-chandanrlinux@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20201103150642.2032284-1-chandanrlinux@gmail.com> References: <20201103150642.2032284-1-chandanrlinux@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-xfs@vger.kernel.org XFS does not check for possible overflow of per-inode extent counter fields when adding extents to either data or attr fork. For e.g. 1. Insert 5 million xattrs (each having a value size of 255 bytes) and then delete 50% of them in an alternating manner. 2. On a 4k block sized XFS filesystem instance, the above causes 98511 extents to be created in the attr fork of the inode. xfsaild/loop0 2008 [003] 1475.127209: probe:xfs_inode_to_disk: (ffffffffa43fb6b0) if_nextents=98511 i_ino=131 3. The incore inode fork extent counter is a signed 32-bit quantity. However the on-disk extent counter is an unsigned 16-bit quantity and hence cannot hold 98511 extents. 4. The following incorrect value is stored in the attr extent counter, # xfs_db -f -c 'inode 131' -c 'print core.naextents' /dev/loop0 core.naextents = -32561 This commit adds a new helper function (i.e. xfs_iext_count_may_overflow()) to check for overflow of the per-inode data and xattr extent counters. Future patches will use this function to make sure that an FS operation won't cause the extent counter to overflow. Suggested-by: Darrick J. Wong Reviewed-by: Allison Henderson Reviewed-by: Christoph Hellwig Reviewed-by: Darrick J. Wong Signed-off-by: Chandan Babu R --- fs/xfs/libxfs/xfs_inode_fork.c | 23 +++++++++++++++++++++++ fs/xfs/libxfs/xfs_inode_fork.h | 2 ++ 2 files changed, 25 insertions(+) diff --git a/fs/xfs/libxfs/xfs_inode_fork.c b/fs/xfs/libxfs/xfs_inode_fork.c index 7575de5cecb1..8d48716547e5 100644 --- a/fs/xfs/libxfs/xfs_inode_fork.c +++ b/fs/xfs/libxfs/xfs_inode_fork.c @@ -23,6 +23,7 @@ #include "xfs_da_btree.h" #include "xfs_dir2_priv.h" #include "xfs_attr_leaf.h" +#include "xfs_types.h" kmem_zone_t *xfs_ifork_zone; @@ -728,3 +729,25 @@ xfs_ifork_verify_local_attr( return 0; } + +int +xfs_iext_count_may_overflow( + struct xfs_inode *ip, + int whichfork, + int nr_to_add) +{ + struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); + uint64_t max_exts; + uint64_t nr_exts; + + if (whichfork == XFS_COW_FORK) + return 0; + + max_exts = (whichfork == XFS_ATTR_FORK) ? MAXAEXTNUM : MAXEXTNUM; + + nr_exts = ifp->if_nextents + nr_to_add; + if (nr_exts < ifp->if_nextents || nr_exts > max_exts) + return -EFBIG; + + return 0; +} diff --git a/fs/xfs/libxfs/xfs_inode_fork.h b/fs/xfs/libxfs/xfs_inode_fork.h index a4953e95c4f3..0beb8e2a00be 100644 --- a/fs/xfs/libxfs/xfs_inode_fork.h +++ b/fs/xfs/libxfs/xfs_inode_fork.h @@ -172,5 +172,7 @@ extern void xfs_ifork_init_cow(struct xfs_inode *ip); int xfs_ifork_verify_local_data(struct xfs_inode *ip); int xfs_ifork_verify_local_attr(struct xfs_inode *ip); +int xfs_iext_count_may_overflow(struct xfs_inode *ip, int whichfork, + int nr_to_add); #endif /* __XFS_INODE_FORK_H__ */ -- 2.28.0