1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
4 * Copyright (C) 2017 Oracle.
9 #include "xfs_format.h"
10 #include "xfs_shared.h"
11 #include "xfs_trans_resv.h"
13 #include "xfs_mount.h"
16 /* Find the size of the AG, in blocks. */
22 ASSERT(agno < mp->m_sb.sb_agcount);
24 if (agno < mp->m_sb.sb_agcount - 1)
25 return mp->m_sb.sb_agblocks;
26 return mp->m_sb.sb_dblocks - (agno * mp->m_sb.sb_agblocks);
30 * Verify that an AG block number pointer neither points outside the AG
31 * nor points at static metadata.
41 eoag = xfs_ag_block_count(mp, agno);
44 if (agbno <= XFS_AGFL_BLOCK(mp))
50 * Verify that an FS block number pointer neither points outside the
51 * filesystem nor points at static AG metadata.
58 xfs_agnumber_t agno = XFS_FSB_TO_AGNO(mp, fsbno);
60 if (agno >= mp->m_sb.sb_agcount)
62 return xfs_verify_agbno(mp, agno, XFS_FSB_TO_AGBNO(mp, fsbno));
66 * Verify that a data device extent is fully contained inside the filesystem,
67 * does not cross an AG boundary, and does not point at static metadata.
75 if (fsbno + len <= fsbno)
78 if (!xfs_verify_fsbno(mp, fsbno))
81 if (!xfs_verify_fsbno(mp, fsbno + len - 1))
84 return XFS_FSB_TO_AGNO(mp, fsbno) ==
85 XFS_FSB_TO_AGNO(mp, fsbno + len - 1);
88 /* Calculate the first and last possible inode number in an AG. */
99 eoag = xfs_ag_block_count(mp, agno);
102 * Calculate the first inode, which will be in the first
103 * cluster-aligned block after the AGFL.
105 bno = round_up(XFS_AGFL_BLOCK(mp) + 1, M_IGEO(mp)->cluster_align);
106 *first = XFS_AGB_TO_AGINO(mp, bno);
109 * Calculate the last inode, which will be at the end of the
110 * last (aligned) cluster that can be allocated in the AG.
112 bno = round_down(eoag, M_IGEO(mp)->cluster_align);
113 *last = XFS_AGB_TO_AGINO(mp, bno) - 1;
117 * Verify that an AG inode number pointer neither points outside the AG
118 * nor points at static metadata.
122 struct xfs_mount *mp,
129 xfs_agino_range(mp, agno, &first, &last);
130 return agino >= first && agino <= last;
134 * Verify that an AG inode number pointer neither points outside the AG
135 * nor points at static metadata, or is NULLAGINO.
138 xfs_verify_agino_or_null(
139 struct xfs_mount *mp,
143 return agino == NULLAGINO || xfs_verify_agino(mp, agno, agino);
147 * Verify that an FS inode number pointer neither points outside the
148 * filesystem nor points at static AG metadata.
152 struct xfs_mount *mp,
155 xfs_agnumber_t agno = XFS_INO_TO_AGNO(mp, ino);
156 xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ino);
158 if (agno >= mp->m_sb.sb_agcount)
160 if (XFS_AGINO_TO_INO(mp, agno, agino) != ino)
162 return xfs_verify_agino(mp, agno, agino);
165 /* Is this an internal inode number? */
168 struct xfs_mount *mp,
171 return ino == mp->m_sb.sb_rbmino || ino == mp->m_sb.sb_rsumino ||
172 (xfs_sb_version_hasquota(&mp->m_sb) &&
173 xfs_is_quota_inode(&mp->m_sb, ino));
177 * Verify that a directory entry's inode number doesn't point at an internal
178 * inode, empty space, or static AG metadata.
182 struct xfs_mount *mp,
185 if (xfs_internal_inum(mp, ino))
187 return xfs_verify_ino(mp, ino);
191 * Verify that an realtime block number pointer doesn't point off the
192 * end of the realtime device.
196 struct xfs_mount *mp,
199 return rtbno < mp->m_sb.sb_rblocks;
202 /* Verify that a realtime device extent is fully contained inside the volume. */
205 struct xfs_mount *mp,
209 if (rtbno + len <= rtbno)
212 if (!xfs_verify_rtbno(mp, rtbno))
215 return xfs_verify_rtbno(mp, rtbno + len - 1);
218 /* Calculate the range of valid icount values. */
221 struct xfs_mount *mp,
222 unsigned long long *min,
223 unsigned long long *max)
225 unsigned long long nr_inos = 0;
226 struct xfs_perag *pag;
229 /* root, rtbitmap, rtsum all live in the first chunk */
230 *min = XFS_INODES_PER_CHUNK;
232 for_each_perag(mp, agno, pag) {
233 xfs_agino_t first, last;
235 xfs_agino_range(mp, agno, &first, &last);
236 nr_inos += last - first + 1;
241 /* Sanity-checking of inode counts. */
244 struct xfs_mount *mp,
245 unsigned long long icount)
247 unsigned long long min, max;
249 xfs_icount_range(mp, &min, &max);
250 return icount >= min && icount <= max;
253 /* Sanity-checking of dir/attr block offsets. */
256 struct xfs_mount *mp,
259 xfs_dablk_t max_dablk = -1U;
261 return dabno <= max_dablk;
264 /* Check that a file block offset does not exceed the maximum. */
267 struct xfs_mount *mp,
270 return off <= XFS_MAX_FILEOFF;
273 /* Check that a range of file block offsets do not exceed the maximum. */
276 struct xfs_mount *mp,
280 if (off + len <= off)
283 if (!xfs_verify_fileoff(mp, off))
286 return xfs_verify_fileoff(mp, off + len - 1);