/* * xfs_mount_common * * Mount initialization code establishing various mount * fields from the superblock associated with the given * mount structure */ void xfs_sb_mount_common( struct xfs_mount *mp, struct xfs_sb *sbp) { mp->m_agfrotor = mp->m_agirotor = 0; spin_lock_init(&mp->m_agirotor_lock); mp->m_maxagi = mp->m_sb.sb_agcount; mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG; mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT; mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT; mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1; mp->m_agino_log = sbp->sb_inopblog + sbp->sb_agblklog; mp->m_blockmask = sbp->sb_blocksize - 1; mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG; mp->m_blockwmask = mp->m_blockwsize - 1; mp->m_alloc_mxr[0] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 1); mp->m_alloc_mxr[1] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 0); mp->m_alloc_mnr[0] = mp->m_alloc_mxr[0] / 2; mp->m_alloc_mnr[1] = mp->m_alloc_mxr[1] / 2; mp->m_inobt_mxr[0] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 1); mp->m_inobt_mxr[1] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 0); mp->m_inobt_mnr[0] = mp->m_inobt_mxr[0] / 2; mp->m_inobt_mnr[1] = mp->m_inobt_mxr[1] / 2; mp->m_bmap_dmxr[0] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 1); mp->m_bmap_dmxr[1] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 0); mp->m_bmap_dmnr[0] = mp->m_bmap_dmxr[0] / 2; mp->m_bmap_dmnr[1] = mp->m_bmap_dmxr[1] / 2; mp->m_bsize = XFS_FSB_TO_BB(mp, 1); mp->m_ialloc_inos = (int)MAX((__uint16_t)XFS_INODES_PER_CHUNK, sbp->sb_inopblock); mp->m_ialloc_blks = mp->m_ialloc_inos >> sbp->sb_inopblog; }
/* * Allocate an extent of length minlen<=len<=maxlen, with no position * specified. If we don't get maxlen then use prod to trim * the length, if given. The lengths are all in rtextents. */ STATIC int /* error */ xfs_rtallocate_extent_size( xfs_mount_t *mp, /* file system mount point */ xfs_trans_t *tp, /* transaction pointer */ xfs_extlen_t minlen, /* minimum length to allocate */ xfs_extlen_t maxlen, /* maximum length to allocate */ xfs_extlen_t *len, /* out: actual length allocated */ xfs_buf_t **rbpp, /* in/out: summary block buffer */ xfs_fsblock_t *rsb, /* in/out: summary block number */ xfs_extlen_t prod, /* extent product factor */ xfs_rtblock_t *rtblock) /* out: start block allocated */ { int error; /* error value */ int i; /* bitmap block number */ int l; /* level number (loop control) */ xfs_rtblock_t n; /* next block to be tried */ xfs_rtblock_t r; /* result block number */ xfs_suminfo_t sum; /* summary information for extents */ ASSERT(minlen % prod == 0 && maxlen % prod == 0); ASSERT(maxlen != 0); /* * Loop over all the levels starting with maxlen. * At each level, look at all the bitmap blocks, to see if there * are extents starting there that are long enough (>= maxlen). * Note, only on the initial level can the allocation fail if * the summary says there's an extent. */ for (l = xfs_highbit32(maxlen); l < mp->m_rsumlevels; l++) { /* * Loop over all the bitmap blocks. */ for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) { /* * Get the summary for this level/block. */ error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb, &sum); if (error) { return error; } /* * Nothing there, on to the next block. */ if (!sum) continue; /* * Try allocating the extent. */ error = xfs_rtallocate_extent_block(mp, tp, i, maxlen, maxlen, len, &n, rbpp, rsb, prod, &r); if (error) { return error; } /* * If it worked, return that. */ if (r != NULLRTBLOCK) { *rtblock = r; return 0; } /* * If the "next block to try" returned from the * allocator is beyond the next bitmap block, * skip to that bitmap block. */ if (XFS_BITTOBLOCK(mp, n) > i + 1) i = XFS_BITTOBLOCK(mp, n) - 1; } } /* * Didn't find any maxlen blocks. Try smaller ones, unless * we're asking for a fixed size extent. */ if (minlen > --maxlen) { *rtblock = NULLRTBLOCK; return 0; } ASSERT(minlen != 0); ASSERT(maxlen != 0); /* * Loop over sizes, from maxlen down to minlen. * This time, when we do the allocations, allow smaller ones * to succeed. */ for (l = xfs_highbit32(maxlen); l >= xfs_highbit32(minlen); l--) { /* * Loop over all the bitmap blocks, try an allocation * starting in that block. */ for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) { /* * Get the summary information for this level/block. */ error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb, &sum); if (error) { return error; } /* * If nothing there, go on to next. */ if (!sum) continue; /* * Try the allocation. Make sure the specified * minlen/maxlen are in the possible range for * this summary level. */ error = xfs_rtallocate_extent_block(mp, tp, i, XFS_RTMAX(minlen, 1 << l), XFS_RTMIN(maxlen, (1 << (l + 1)) - 1), len, &n, rbpp, rsb, prod, &r); if (error) { return error; } /* * If it worked, return that extent. */ if (r != NULLRTBLOCK) { *rtblock = r; return 0; } /* * If the "next block to try" returned from the * allocator is beyond the next bitmap block, * skip to that bitmap block. */ if (XFS_BITTOBLOCK(mp, n) > i + 1) i = XFS_BITTOBLOCK(mp, n) - 1; } } /* * Got nothing, return failure. */ *rtblock = NULLRTBLOCK; return 0; }
/* * Allocate an extent of length minlen<=len<=maxlen, starting as near * to bno as possible. If we don't get maxlen then use prod to trim * the length, if given. The lengths are all in rtextents. */ STATIC int /* error */ xfs_rtallocate_extent_near( xfs_mount_t *mp, /* file system mount point */ xfs_trans_t *tp, /* transaction pointer */ xfs_rtblock_t bno, /* starting block number to allocate */ xfs_extlen_t minlen, /* minimum length to allocate */ xfs_extlen_t maxlen, /* maximum length to allocate */ xfs_extlen_t *len, /* out: actual length allocated */ xfs_buf_t **rbpp, /* in/out: summary block buffer */ xfs_fsblock_t *rsb, /* in/out: summary block number */ xfs_extlen_t prod, /* extent product factor */ xfs_rtblock_t *rtblock) /* out: start block allocated */ { int any; /* any useful extents from summary */ xfs_rtblock_t bbno; /* bitmap block number */ int error; /* error value */ int i; /* bitmap block offset (loop control) */ int j; /* secondary loop control */ int log2len; /* log2 of minlen */ xfs_rtblock_t n; /* next block to try */ xfs_rtblock_t r; /* result block */ ASSERT(minlen % prod == 0 && maxlen % prod == 0); /* * If the block number given is off the end, silently set it to * the last block. */ if (bno >= mp->m_sb.sb_rextents) bno = mp->m_sb.sb_rextents - 1; /* * Try the exact allocation first. */ error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen, len, rbpp, rsb, prod, &r); if (error) { return error; } /* * If the exact allocation worked, return that. */ if (r != NULLRTBLOCK) { *rtblock = r; return 0; } bbno = XFS_BITTOBLOCK(mp, bno); i = 0; ASSERT(minlen != 0); log2len = xfs_highbit32(minlen); /* * Loop over all bitmap blocks (bbno + i is current block). */ for (;;) { /* * Get summary information of extents of all useful levels * starting in this bitmap block. */ error = xfs_rtany_summary(mp, tp, log2len, mp->m_rsumlevels - 1, bbno + i, rbpp, rsb, &any); if (error) { return error; } /* * If there are any useful extents starting here, try * allocating one. */ if (any) { /* * On the positive side of the starting location. */ if (i >= 0) { /* * Try to allocate an extent starting in * this block. */ error = xfs_rtallocate_extent_block(mp, tp, bbno + i, minlen, maxlen, len, &n, rbpp, rsb, prod, &r); if (error) { return error; } /* * If it worked, return it. */ if (r != NULLRTBLOCK) { *rtblock = r; return 0; } } /* * On the negative side of the starting location. */ else { /* i < 0 */ /* * Loop backwards through the bitmap blocks from * the starting point-1 up to where we are now. * There should be an extent which ends in this * bitmap block and is long enough. */ for (j = -1; j > i; j--) { /* * Grab the summary information for * this bitmap block. */ error = xfs_rtany_summary(mp, tp, log2len, mp->m_rsumlevels - 1, bbno + j, rbpp, rsb, &any); if (error) { return error; } /* * If there's no extent given in the * summary that means the extent we * found must carry over from an * earlier block. If there is an * extent given, we've already tried * that allocation, don't do it again. */ if (any) continue; error = xfs_rtallocate_extent_block(mp, tp, bbno + j, minlen, maxlen, len, &n, rbpp, rsb, prod, &r); if (error) { return error; } /* * If it works, return the extent. */ if (r != NULLRTBLOCK) { *rtblock = r; return 0; } } /* * There weren't intervening bitmap blocks * with a long enough extent, or the * allocation didn't work for some reason * (i.e. it's a little * too short). * Try to allocate from the summary block * that we found. */ error = xfs_rtallocate_extent_block(mp, tp, bbno + i, minlen, maxlen, len, &n, rbpp, rsb, prod, &r); if (error) { return error; } /* * If it works, return the extent. */ if (r != NULLRTBLOCK) { *rtblock = r; return 0; } } } /* * Loop control. If we were on the positive side, and there's * still more blocks on the negative side, go there. */ if (i > 0 && (int)bbno - i >= 0) i = -i; /* * If positive, and no more negative, but there are more * positive, go there. */ else if (i > 0 && (int)bbno + i < mp->m_sb.sb_rbmblocks - 1) i++; /* * If negative or 0 (just started), and there are positive * blocks to go, go there. The 0 case moves to block 1. */ else if (i <= 0 && (int)bbno - i < mp->m_sb.sb_rbmblocks - 1) i = 1 - i; /* * If negative or 0 and there are more negative blocks, * go there. */ else if (i <= 0 && (int)bbno + i > 0) i--; /* * Must be done. Return failure. */ else break; } *rtblock = NULLRTBLOCK; return 0; }