From patchwork Thu Oct 24 02:51:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dave Chinner X-Patchwork-Id: 13848261 Received: from mail-pf1-f169.google.com (mail-pf1-f169.google.com [209.85.210.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E3D531494DF for ; Thu, 24 Oct 2024 02:51:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729738310; cv=none; b=Nymdhl/Chk50Vl24bLtwYLRVROkeiS7Qc5vfYrqXjRsLmTaYSjI/IfWkE9Y1Tny5ywKRYqHHyf0NVAo2s1rDQmiHZ+FsuUIFamtUDgO74uCGzaL4QJtonwdRbh+Bg9CsKGdRcpSha2o4SRtYfLAD8So5xA7Z58fVrj0QvINNnoM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729738310; c=relaxed/simple; bh=7p9qOVab9mUqKiCrMpPfasqlhXIZO9XRO05iSOCNdPE=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=NQGSPI6wjCMNGNilaO+SdM2HNk/YqoVw0xfX6vwulDokG1XY4himJIhw0UYfduRbXtAm7AiaVmoWUjxDzd1PVoB5Qih8c/HlAgaW1fuS5cpA4rJXAqif6Ddfl84Jj0L+DivZJryTaMlaoucUpz6Ub6Eewysd3bO8s7R6+6fLmUk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=fromorbit.com; spf=pass smtp.mailfrom=fromorbit.com; dkim=pass (2048-bit key) header.d=fromorbit-com.20230601.gappssmtp.com header.i=@fromorbit-com.20230601.gappssmtp.com header.b=LH/Mk+fl; arc=none smtp.client-ip=209.85.210.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=fromorbit.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=fromorbit.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=fromorbit-com.20230601.gappssmtp.com header.i=@fromorbit-com.20230601.gappssmtp.com header.b="LH/Mk+fl" Received: by mail-pf1-f169.google.com with SMTP id d2e1a72fcca58-71e6cec7227so374897b3a.0 for ; Wed, 23 Oct 2024 19:51:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fromorbit-com.20230601.gappssmtp.com; s=20230601; t=1729738307; x=1730343107; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=Pv876Dy8Ajy7g5mfHuh133EW6I1k95G47jQYc8OWOCQ=; b=LH/Mk+flJtIc2ZwujaaU/bWKawm+hVZ6Dpyj552P142DBfWgiwL1hHLoAL8mCqF7bq vV+Gg+jf3Zm28DtsyrxbPKcq6WzrLhMDBHD0ArQS2lroaKkvNw5KfYoDJWn60BJHA7e9 y78PkVQaaywioL964PxQYsaUFP9DxyC9TjImcOF1VIZ4xNVEKg33oYbJxSWEeCTPQjMZ MBZjCN3SkZNr45j/2w3W1EVUtWEyC2hR5nA7+kG2GidQM/R6Jt43fwaGPfeQcICsru0n OdUW+MHLBGOliQdFyqvDuO11kWk9byR880mOaiTML0YgrRww/A2AsxzXvDsjSrLp5xmW 6UGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729738307; x=1730343107; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Pv876Dy8Ajy7g5mfHuh133EW6I1k95G47jQYc8OWOCQ=; b=Zgf09WlluwEQE6xt9y8sKvgK25DKQTqYK0jRsHWtxha0aYImSZ0+Tco1mY+WC95K31 +eWOfsxCG/DO5aoiLdPi3uIdSpzp1YuBwZTyF7lxdyZIUnVyOTmytj+voK/pfkdqhKE8 tfbRvvaHXuXcL94Cs3oW4iRyaSEeu6x1U695OA2+QhBZqe35hFfFb5xhaqKRfKYp+26N HwbMl3/jk9EfPD/BscJYX7zPdtnEXgANmYJtclK4N3bcrLL2Jd8zSkNUq/L3nIAeZloW VN0+wdI3Gomdf2zJoI5hIsReEFyqcumHGMSWX4OXhNpLicJb5E+J8VgxNb2IkkP6V7wr /ptA== X-Gm-Message-State: AOJu0YxLTDGv5vVch9zM2nUE7dKc0sewtvGXoC+969bdD4q9pTCu/Ce+ SwRILwBZ6B4KkOB6uvqF7dmjD6uHBFpVbV2Yk88MGBojVVk6w6+vMOECrqQetrBvw4gY6PeH59K X X-Google-Smtp-Source: AGHT+IF9qnwKd51AhWgQlwnc1x4YpN+HIERSmRT3j3Dytoc6wKC49NAJGkL/InP8b0X29Fw2dtgkdQ== X-Received: by 2002:a05:6a00:895:b0:71e:768b:700a with SMTP id d2e1a72fcca58-72045f88494mr656769b3a.23.1729738307182; Wed, 23 Oct 2024 19:51:47 -0700 (PDT) Received: from dread.disaster.area (pa49-186-86-168.pa.vic.optusnet.com.au. [49.186.86.168]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7eaeabb9009sm7676216a12.59.2024.10.23.19.51.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Oct 2024 19:51:46 -0700 (PDT) Received: from [192.168.253.23] (helo=devoid.disaster.area) by dread.disaster.area with esmtp (Exim 4.96) (envelope-from ) id 1t3nwt-004xNr-2k for linux-xfs@vger.kernel.org; Thu, 24 Oct 2024 13:51:44 +1100 Received: from dave by devoid.disaster.area with local (Exim 4.98) (envelope-from ) id 1t3nwu-0000000H7zU-1Bgn for linux-xfs@vger.kernel.org; Thu, 24 Oct 2024 13:51:44 +1100 From: Dave Chinner To: linux-xfs@vger.kernel.org Subject: [PATCH 1/3] xfs: fix sparse inode limits on runt AG Date: Thu, 24 Oct 2024 13:51:03 +1100 Message-ID: <20241024025142.4082218-2-david@fromorbit.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241024025142.4082218-1-david@fromorbit.com> References: <20241024025142.4082218-1-david@fromorbit.com> Precedence: bulk X-Mailing-List: linux-xfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Dave Chinner The runt AG at the end of a filesystem is almost always smaller than the mp->m_sb.sb_agblocks. Unfortunately, when setting the max_agbno limit for the inode chunk allocation, we do not take this into account. This means we can allocate a sparse inode chunk that overlaps beyond the end of an AG. When we go to allocate an inode from that sparse chunk, the irec fails validation because the agbno of the start of the irec is beyond valid limits for the runt AG. Prevent this from happening by taking into account the size of the runt AG when allocating inode chunks. Also convert the various checks for valid inode chunk agbnos to use xfs_ag_block_count() so that they will also catch such issues in the future. Fixes: 56d1115c9bc7 ("xfs: allocate sparse inode chunks on full chunk allocation failure") Signed-off-by: Dave Chinner --- fs/xfs/libxfs/xfs_ialloc.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/fs/xfs/libxfs/xfs_ialloc.c b/fs/xfs/libxfs/xfs_ialloc.c index 271855227514..6258527315f2 100644 --- a/fs/xfs/libxfs/xfs_ialloc.c +++ b/fs/xfs/libxfs/xfs_ialloc.c @@ -855,7 +855,8 @@ xfs_ialloc_ag_alloc( * the end of the AG. */ args.min_agbno = args.mp->m_sb.sb_inoalignmt; - args.max_agbno = round_down(args.mp->m_sb.sb_agblocks, + args.max_agbno = round_down(xfs_ag_block_count(args.mp, + pag->pag_agno), args.mp->m_sb.sb_inoalignmt) - igeo->ialloc_blks; @@ -2332,9 +2333,9 @@ xfs_difree( return -EINVAL; } agbno = XFS_AGINO_TO_AGBNO(mp, agino); - if (agbno >= mp->m_sb.sb_agblocks) { - xfs_warn(mp, "%s: agbno >= mp->m_sb.sb_agblocks (%d >= %d).", - __func__, agbno, mp->m_sb.sb_agblocks); + if (agbno >= xfs_ag_block_count(mp, pag->pag_agno)) { + xfs_warn(mp, "%s: agbno >= xfs_ag_block_count (%d >= %d).", + __func__, agbno, xfs_ag_block_count(mp, pag->pag_agno)); ASSERT(0); return -EINVAL; } @@ -2457,7 +2458,7 @@ xfs_imap( */ agino = XFS_INO_TO_AGINO(mp, ino); agbno = XFS_AGINO_TO_AGBNO(mp, agino); - if (agbno >= mp->m_sb.sb_agblocks || + if (agbno >= xfs_ag_block_count(mp, pag->pag_agno) || ino != XFS_AGINO_TO_INO(mp, pag->pag_agno, agino)) { error = -EINVAL; #ifdef DEBUG @@ -2467,11 +2468,12 @@ xfs_imap( */ if (flags & XFS_IGET_UNTRUSTED) return error; - if (agbno >= mp->m_sb.sb_agblocks) { + if (agbno >= xfs_ag_block_count(mp, pag->pag_agno)) { xfs_alert(mp, "%s: agbno (0x%llx) >= mp->m_sb.sb_agblocks (0x%lx)", __func__, (unsigned long long)agbno, - (unsigned long)mp->m_sb.sb_agblocks); + (unsigned long)xfs_ag_block_count(mp, + pag->pag_agno)); } if (ino != XFS_AGINO_TO_INO(mp, pag->pag_agno, agino)) { xfs_alert(mp, From patchwork Thu Oct 24 02:51:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dave Chinner X-Patchwork-Id: 13848263 Received: from mail-oi1-f175.google.com (mail-oi1-f175.google.com [209.85.167.175]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AFEB41547C6 for ; Thu, 24 Oct 2024 02:51:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729738312; cv=none; b=gEanIYC7U0Rrdf79TzVBOpjG9lkyOZsK9Eyoz4UmDHt+pWH1yP4RZUcjIGLsODDyO5oHSl7P2UZkqmDNPjHWgiDz14A4wKuHkLnjmFPflqV6fWwllIxPfvkuqTfx1BRzMGwr2etZPYRmqv2Q7l/YFIdx4MxKPJAd2a3lqrYYONw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729738312; c=relaxed/simple; bh=6jq2gU/jN94LmlN4G/Za7a6lZP2o/0dQHBy8nEVt0nM=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lKFRdjCO/mP+C+epODdPFSQ+CKEqYLTz1eb2FaHIN5UD8r6O9AQvNy4sim87eyfcXvqQ4leMDZvRlIdFwPv5uB1m20/41P7Y88rFCkEzB7YeZENP+YsJD7oV+7SJGFnyh+f+1o+fwxCa72/8M9QvginrDfyj5coxLAh86n9Gf8o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=fromorbit.com; spf=pass smtp.mailfrom=fromorbit.com; dkim=pass (2048-bit key) header.d=fromorbit-com.20230601.gappssmtp.com header.i=@fromorbit-com.20230601.gappssmtp.com header.b=h+vzINAm; arc=none smtp.client-ip=209.85.167.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=fromorbit.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=fromorbit.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=fromorbit-com.20230601.gappssmtp.com header.i=@fromorbit-com.20230601.gappssmtp.com header.b="h+vzINAm" Received: by mail-oi1-f175.google.com with SMTP id 5614622812f47-3e5fa17a79dso298953b6e.1 for ; Wed, 23 Oct 2024 19:51:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fromorbit-com.20230601.gappssmtp.com; s=20230601; t=1729738309; x=1730343109; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=MFxVxs6IH1+Tc5yC3ZO5VgmG5LseHjOqVxmONJI2gjI=; b=h+vzINAmUAQyx/HeVIlpapCD3yG1VQ6gyjC+62pilqBjur7U6MvzihzqGqJZVPi1WO RmGSj9t8NwfpWYTZCWDwJ1aohpuPQyLeuo20lpiTKD9/aCd1cj5VRlt7CKh/EQsRg7gV r/F93ZujCrewxQia3ZTNm4FFiFYIxudTpNBPCuGFfx5zxj8GyRdz55BJY/r5n58yhFsP 9yuu7XSVzXf2c8gPln3hS+MmKgqzUVRdr+LQABvZJ9X98jU16f1Ptgk4GQuxooNsddH7 KVy4n4YGiLBS8eU5LAwIg1RCGrHaoECc1nA6swxD3dPHaMhkPm/Y0lR0NfvvjIfO1lWD yd0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729738309; x=1730343109; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=MFxVxs6IH1+Tc5yC3ZO5VgmG5LseHjOqVxmONJI2gjI=; b=khAF09OZw3Jbl40eJ0qXmM88UwGaXnLytl2KDWmYG19ScIYE3KMnC28/148w2XDcCk H9y8b3taewQcUV/tUzRsDURx5+w2nNBDqVo4OsQWcB9JDXx20MmCU8B9S7EG6sad/fOF GFvRI6gcphJd+J6WDc0RpY+k0ss+MlmX/lXhmevLGWdGTUcUpSte10DLEbhDG7t3tw39 V3SJ02+xlZ2SgusTPQVebe8Wn5pa/w/ABNLJ2Tkd6yNIde6hbnnKWqVATl/AbFopQbDD +Sxnth/cLPlGqJYKDCiKB9TSNywWjcOlHOZTkLhVXmT56d/kT9mYcZbwMP8x0DSI4pKS 3Lxg== X-Gm-Message-State: AOJu0YzeVlNPUF97WmS/l6OvoLbfaBcUaiI7sJJqrmIgP0kDMft54zjC J6hkwA5jvOZDFTw1nF6jJFnvRIj3fVMpBJqLDPWr05vQ4wABGt7fqi7nGNJakmWpg68wOfutZvY A X-Google-Smtp-Source: AGHT+IF1bjWdl9UYFCEgfmQpILmWpC5lCCX1+/J6URLTrAtRJD7HoNe0uu6XJixd3Sc0sRIvoLeSIw== X-Received: by 2002:a05:6808:18a9:b0:3e4:d3f6:6c97 with SMTP id 5614622812f47-3e6245c873cmr4410263b6e.46.1729738308603; Wed, 23 Oct 2024 19:51:48 -0700 (PDT) Received: from dread.disaster.area (pa49-186-86-168.pa.vic.optusnet.com.au. [49.186.86.168]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7eaeabeedaasm7477984a12.94.2024.10.23.19.51.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Oct 2024 19:51:48 -0700 (PDT) Received: from [192.168.253.23] (helo=devoid.disaster.area) by dread.disaster.area with esmtp (Exim 4.96) (envelope-from ) id 1t3nwt-004xNs-2n for linux-xfs@vger.kernel.org; Thu, 24 Oct 2024 13:51:44 +1100 Received: from dave by devoid.disaster.area with local (Exim 4.98) (envelope-from ) id 1t3nwu-0000000H7zY-1L0x for linux-xfs@vger.kernel.org; Thu, 24 Oct 2024 13:51:44 +1100 From: Dave Chinner To: linux-xfs@vger.kernel.org Subject: [PATCH 2/3] xfs: allow sparse inode records at the end of runt AGs Date: Thu, 24 Oct 2024 13:51:04 +1100 Message-ID: <20241024025142.4082218-3-david@fromorbit.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241024025142.4082218-1-david@fromorbit.com> References: <20241024025142.4082218-1-david@fromorbit.com> Precedence: bulk X-Mailing-List: linux-xfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Dave Chinner Due to the failure to correctly limit sparse inode chunk allocation in runt AGs, we now have many production filesystems with sparse inode chunks allocated across the end of the runt AG. xfs_repair or a growfs is needed to fix this situation, neither of which are particularly appealing. The on disk layout from the metadump shows AG 12 as a runt that is 1031 blocks in length and the last inode chunk allocated on disk at agino 8192. $ xfs_db -c "agi 12" -c "p length" -c "p newino"a \ > -c "convert agno 12 agino 8192 agbno" \ > -c "a free_root" -c p /mnt/scratch/t.img length = 1031 newino = 8192 0x400 (1024) magic = 0x46494233 level = 0 numrecs = 3 leftsib = null rightsib = null bno = 62902208 lsn = 0xb5500001849 uuid = e941c927-8697-4c16-a828-bc98e3878f7d owner = 12 crc = 0xfe0a5c41 (correct) recs[1-3] = [startino,holemask,count,freecount,free] 1:[128,0,64,11,0xc1ff00] 2:[256,0,64,3,0xb] 3:[8192,0xff00,32,32,0xffffffffffffffff] The agbno of the inode chunk is 0x400 (1024), but there are only 7 blocks from there to the end of the AG. No inode cluster should have been allocated there, but the bug fixed in the previous commit allowed that. We can see from the finobt record #3 that there is a sparse inode record at agbno 1024 that is for 32 inodes - 4 blocks worth of inodes. Hence we have a valid inode cluster from agbno 1024-1027 on disk, and we are trying to allocation inodes from it. This is due to the sparse inode feature requiring sb->sb_spino_align being set to the inode cluster size, whilst the sb->sb_inoalignmt is set to the full chunk size. The args.max_agbno bug in sparse inode alignment allows an inode cluster at the start of the irec which is sb_spino_align aligned and sized, but the remainder of the irec to be beyond EOAG. There is actually nothing wrong with having a sparse inode cluster that ends up overlapping the end of the runt AG - it just means that attempts to make it non-sparse will fail because there's no contiguous space available to fill out the chunk. However, we can't even get that far because xfs_inobt_get_rec() will validate the irec->ir_startino and xfs_verify_agino() will fail on an irec that spans beyond the end of the AG: XFS (loop0): finobt record corruption in AG 12 detected at xfs_inobt_check_irec+0x44/0xb0! XFS (loop0): start inode 0x2000, count 0x20, free 0x20 freemask 0xffffffffffffffff, holemask 0xff00 Hence the actual maximum agino we could allocate is the size of the AG rounded down by the size of of an inode cluster, not the size of a full inode chunk. Modify __xfs_agino_range() code to take this sparse inode case into account and hence allow us of the already allocated sparse inode chunk at the end of a runt AG. That change, alone, however, is not sufficient, as xfs_inobt_get_rec() hard codes the maximum inode number in the chunk and attempts to verify the last inode number in the chunk. This fails because the of the sparse inode record is beyond the end of the AG. Hence we have to look at the hole mask in the sparse inode record to determine where the highest allocated inode is. We then use the calculated high inode number to determine if the allocated sparse inode cluster fits within the AG. With this, inode allocation on a sparse inode cluster at the end of a runt AG now succeeds. Hence any filesystem that has allocated a cluster in this location will no longer fail allocation and issue corruption warnings. Fixes: 56d1115c9bc7 ("xfs: allocate sparse inode chunks on full chunk allocation failure") Signed-off-by: Dave Chinner --- fs/xfs/libxfs/xfs_ag.c | 47 ++++++++++++++++++++++++++++++-------- fs/xfs/libxfs/xfs_ialloc.c | 20 +++++++++++++--- 2 files changed, 54 insertions(+), 13 deletions(-) diff --git a/fs/xfs/libxfs/xfs_ag.c b/fs/xfs/libxfs/xfs_ag.c index 5ca8d0106827..33290af6ab01 100644 --- a/fs/xfs/libxfs/xfs_ag.c +++ b/fs/xfs/libxfs/xfs_ag.c @@ -238,15 +238,36 @@ xfs_ag_block_count( mp->m_sb.sb_dblocks); } -/* Calculate the first and last possible inode number in an AG. */ +/* + * Calculate the first and last possible inode number in an AG. + * + * Due to a bug in sparse inode allocation for the runt AG at the end of the + * filesystem, we can have a valid sparse inode chunk on disk that spans beyond + * the end of the AG. Sparse inode chunks have special alignment - the full + * chunk must always be naturally aligned, and the regions that are allocated + * sparsely are cluster sized and aligned. + * + * The result of this is that for sparse inode setups, sb->sb_inoalignmt is + * always the size of the chunk, and that means M_IGEO(mp)->cluster_align isn't + * actually cluster alignment, it is chunk alignment. That means a sparse inode + * cluster that overlaps the end of the AG can never be valid based on "cluster + * alignment" even though all the inodes allocated within the sparse chunk at + * within the valid bounds of the AG and so can be used. + * + * Hence for the runt AG, the valid maximum inode number is based on sparse + * inode cluster alignment (sb->sb_spino_align) and not the "cluster alignment" + * value. + */ static void __xfs_agino_range( struct xfs_mount *mp, + xfs_agnumber_t agno, xfs_agblock_t eoag, xfs_agino_t *first, xfs_agino_t *last) { xfs_agblock_t bno; + xfs_agblock_t end_align; /* * Calculate the first inode, which will be in the first @@ -259,7 +280,12 @@ __xfs_agino_range( * Calculate the last inode, which will be at the end of the * last (aligned) cluster that can be allocated in the AG. */ - bno = round_down(eoag, M_IGEO(mp)->cluster_align); + if (xfs_has_sparseinodes(mp) && agno == mp->m_sb.sb_agcount - 1) + end_align = mp->m_sb.sb_spino_align; + else + end_align = M_IGEO(mp)->cluster_align; + + bno = round_down(eoag, end_align); *last = XFS_AGB_TO_AGINO(mp, bno) - 1; } @@ -270,7 +296,8 @@ xfs_agino_range( xfs_agino_t *first, xfs_agino_t *last) { - return __xfs_agino_range(mp, xfs_ag_block_count(mp, agno), first, last); + return __xfs_agino_range(mp, agno, xfs_ag_block_count(mp, agno), + first, last); } int @@ -284,7 +311,7 @@ xfs_update_last_ag_size( return -EFSCORRUPTED; pag->block_count = __xfs_ag_block_count(mp, prev_agcount - 1, mp->m_sb.sb_agcount, mp->m_sb.sb_dblocks); - __xfs_agino_range(mp, pag->block_count, &pag->agino_min, + __xfs_agino_range(mp, pag->pag_agno, pag->block_count, &pag->agino_min, &pag->agino_max); xfs_perag_rele(pag); return 0; @@ -345,8 +372,8 @@ xfs_initialize_perag( pag->block_count = __xfs_ag_block_count(mp, index, new_agcount, dblocks); pag->min_block = XFS_AGFL_BLOCK(mp); - __xfs_agino_range(mp, pag->block_count, &pag->agino_min, - &pag->agino_max); + __xfs_agino_range(mp, pag->pag_agno, pag->block_count, + &pag->agino_min, &pag->agino_max); } index = xfs_set_inode_alloc(mp, new_agcount); @@ -932,8 +959,8 @@ xfs_ag_shrink_space( /* Update perag geometry */ pag->block_count -= delta; - __xfs_agino_range(pag->pag_mount, pag->block_count, &pag->agino_min, - &pag->agino_max); + __xfs_agino_range(mp, pag->pag_agno, pag->block_count, + &pag->agino_min, &pag->agino_max); xfs_ialloc_log_agi(*tpp, agibp, XFS_AGI_LENGTH); xfs_alloc_log_agf(*tpp, agfbp, XFS_AGF_LENGTH); @@ -1003,8 +1030,8 @@ xfs_ag_extend_space( /* Update perag geometry */ pag->block_count = be32_to_cpu(agf->agf_length); - __xfs_agino_range(pag->pag_mount, pag->block_count, &pag->agino_min, - &pag->agino_max); + __xfs_agino_range(pag->pag_mount, pag->pag_agno, pag->block_count, + &pag->agino_min, &pag->agino_max); return 0; } diff --git a/fs/xfs/libxfs/xfs_ialloc.c b/fs/xfs/libxfs/xfs_ialloc.c index 6258527315f2..d68b53334990 100644 --- a/fs/xfs/libxfs/xfs_ialloc.c +++ b/fs/xfs/libxfs/xfs_ialloc.c @@ -108,22 +108,36 @@ xfs_inobt_rec_freecount( return hweight64(realfree); } +/* Compute the highest allocated inode in an incore inode record. */ +static xfs_agino_t +xfs_inobt_rec_highino( + const struct xfs_inobt_rec_incore *irec) +{ + if (xfs_inobt_issparse(irec->ir_holemask)) + return xfs_highbit64(xfs_inobt_irec_to_allocmask(irec)); + return XFS_INODES_PER_CHUNK; +} + /* Simple checks for inode records. */ xfs_failaddr_t xfs_inobt_check_irec( struct xfs_perag *pag, const struct xfs_inobt_rec_incore *irec) { + xfs_agino_t high_ino = xfs_inobt_rec_highino(irec); + /* Record has to be properly aligned within the AG. */ if (!xfs_verify_agino(pag, irec->ir_startino)) return __this_address; - if (!xfs_verify_agino(pag, - irec->ir_startino + XFS_INODES_PER_CHUNK - 1)) + + if (!xfs_verify_agino(pag, irec->ir_startino + high_ino - 1)) return __this_address; + if (irec->ir_count < XFS_INODES_PER_HOLEMASK_BIT || irec->ir_count > XFS_INODES_PER_CHUNK) return __this_address; - if (irec->ir_freecount > XFS_INODES_PER_CHUNK) + + if (irec->ir_freecount > irec->ir_count) return __this_address; if (xfs_inobt_rec_freecount(irec) != irec->ir_freecount) From patchwork Thu Oct 24 02:51:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dave Chinner X-Patchwork-Id: 13848260 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2EDF3156F30 for ; Thu, 24 Oct 2024 02:51:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729738310; cv=none; b=K0Alfl37xrxsz0HeKm+I1psQ8aqoM96s+YFIwFExvHRDPOWPVUs4B0BjSUEGq+rPuy5T/eqDQ9BBPaDNwCZm1yDJrMXvhQxkEvbPVVh1JngCbeH4/gFcSZdodiTnnPKj08ogIsF3O6sd+yICDlGMOQow5UlAzHd6Z/pMXdZr0pU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729738310; c=relaxed/simple; bh=nyxaXjxibPLkxtfS15iO+RIFH200dLD8L047DHbKGE0=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dA7YfsjDaddptuVq+PU8Ig+glHXZBJHm8trJB7UIblUc8gxuirULC323NnkQ6qFx29MGz7Tq7Z0PN2wbKSxljAKz7VvhLRE4JDM1yg+6t2ZTwxDvNA9GxVtqUeVANaXziNNPVv8rYhPbBqvJK4fu3B8VSu6VlzTVBChVxujwFLA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=fromorbit.com; spf=pass smtp.mailfrom=fromorbit.com; dkim=pass (2048-bit key) header.d=fromorbit-com.20230601.gappssmtp.com header.i=@fromorbit-com.20230601.gappssmtp.com header.b=J5fxeqxg; arc=none smtp.client-ip=209.85.214.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=fromorbit.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=fromorbit.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=fromorbit-com.20230601.gappssmtp.com header.i=@fromorbit-com.20230601.gappssmtp.com header.b="J5fxeqxg" Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-20c803787abso3551815ad.0 for ; Wed, 23 Oct 2024 19:51:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fromorbit-com.20230601.gappssmtp.com; s=20230601; t=1729738307; x=1730343107; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=FkRyxCjHYDM6g1n8M11Ib1ZjfR2KRFWOb8n4TyMPoGQ=; b=J5fxeqxgElpj6nXNjT1hKBK2ncpAA/azTj55TnTyd2HAMzaV6li+HeObG9pgILrREq R09RbMA7hvfuGWPAKhzvvnWJdqqYkbirdni5yvCYZ3/Dm3Hb9rGcM1SBqb1cdRX12a87 mSLz7/UcEmFNNkA1/DbbOhDNxzghejrvFBYkIKWFRWdexOe4wdvR6C/EXVXB5ioyjGlK 2ovmohMxqGZiaUylGx5YJNjTPlG5r6YDUL5j+buGK203SMNCTOpa/yxyhDS8vJqXskx/ OlXcpohGC3icNOBXBxAIhSXu1lDTrTg/HXIzYkyByurk5dAgNLBoLCw4jdJ+xFLWsURc twVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729738307; x=1730343107; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=FkRyxCjHYDM6g1n8M11Ib1ZjfR2KRFWOb8n4TyMPoGQ=; b=ggcoO3O45GwTmC1CjDyE8hWpaVcA+YjeaDh+sQ2II73LHSlTAMblnpxCcYkMiS7XW3 CTjZuxm4+LvyZj1BmeLx1pCi821hun/hE5liWfujDJ9jX4ub8aoydvdCFEEjyS6hkUlq LBKrFrTP9Kt3VBzHz8xmcRbXrDoXY14h/iC33HZZz2YOPE7JrpQHQmZIboc6FPDi0DEn D17papgk/a53h+Bp6Q4WvKMo9VQqlZkBNUlaUFUPR89FwEvFYMsqb4s0NxhhqxdeqTSJ tZgXoy8J9n9u9IewESfXP1Sd2aMIcoa0vWBTjjZh4LEd/to5im0shO7QsLgFqZqlesJ2 7mxg== X-Gm-Message-State: AOJu0YxM1P/eswzvJoX5l+PteFFndCG2AOU99X8Tgddcyb41uQ2BY+Cj ei9Vz+A1fh/e5NmcaArj4Dggz90YFBsh2RDrD3ISvIICSFZQo1O9nvKFludGz7tJFX4AiF0jcJX 2 X-Google-Smtp-Source: AGHT+IGdMgQwsXbKWAPTlElAjy5H6OV4BIorh3qmkYX0770YfkAwM/wyWHqqqwc0/qCmKh+qsgy8+g== X-Received: by 2002:a17:903:1cf:b0:20c:ad30:c891 with SMTP id d9443c01a7336-20fb88f3190mr7005045ad.10.1729738307416; Wed, 23 Oct 2024 19:51:47 -0700 (PDT) Received: from dread.disaster.area (pa49-186-86-168.pa.vic.optusnet.com.au. [49.186.86.168]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20e7f0f3ff5sm63596405ad.280.2024.10.23.19.51.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Oct 2024 19:51:46 -0700 (PDT) Received: from [192.168.253.23] (helo=devoid.disaster.area) by dread.disaster.area with esmtp (Exim 4.96) (envelope-from ) id 1t3nwt-004xNv-2r for linux-xfs@vger.kernel.org; Thu, 24 Oct 2024 13:51:44 +1100 Received: from dave by devoid.disaster.area with local (Exim 4.98) (envelope-from ) id 1t3nwu-0000000H7zc-1T2r for linux-xfs@vger.kernel.org; Thu, 24 Oct 2024 13:51:44 +1100 From: Dave Chinner To: linux-xfs@vger.kernel.org Subject: [PATCH 3/3] xfs: sb_spino_align is not verified Date: Thu, 24 Oct 2024 13:51:05 +1100 Message-ID: <20241024025142.4082218-4-david@fromorbit.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241024025142.4082218-1-david@fromorbit.com> References: <20241024025142.4082218-1-david@fromorbit.com> Precedence: bulk X-Mailing-List: linux-xfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Dave Chinner It's just read in from the superblock and used without doing any validity checks at all on the value. Fixes: fb4f2b4e5a82 ("xfs: add sparse inode chunk alignment superblock field") Signed-off-by: Dave Chinner Reviewed-by: Darrick J. Wong --- fs/xfs/libxfs/xfs_sb.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/fs/xfs/libxfs/xfs_sb.c b/fs/xfs/libxfs/xfs_sb.c index d95409f3cba6..0d181bc140f0 100644 --- a/fs/xfs/libxfs/xfs_sb.c +++ b/fs/xfs/libxfs/xfs_sb.c @@ -398,6 +398,20 @@ xfs_validate_sb_common( sbp->sb_inoalignmt, align); return -EINVAL; } + + if (!sbp->sb_spino_align || + sbp->sb_spino_align > sbp->sb_inoalignmt || + (sbp->sb_inoalignmt % sbp->sb_spino_align) != 0) { + xfs_warn(mp, + "Sparse inode alignment (%u) is invalid.", + sbp->sb_spino_align); + return -EINVAL; + } + } else if (sbp->sb_spino_align) { + xfs_warn(mp, + "Sparse inode alignment (%u) should be zero.", + sbp->sb_spino_align); + return -EINVAL; } } else if (sbp->sb_qflags & (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD | XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD)) {