130f712c9SDave Chinner /* 230f712c9SDave Chinner * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc. 330f712c9SDave Chinner * All Rights Reserved. 430f712c9SDave Chinner * 530f712c9SDave Chinner * This program is free software; you can redistribute it and/or 630f712c9SDave Chinner * modify it under the terms of the GNU General Public License as 730f712c9SDave Chinner * published by the Free Software Foundation. 830f712c9SDave Chinner * 930f712c9SDave Chinner * This program is distributed in the hope that it would be useful, 1030f712c9SDave Chinner * but WITHOUT ANY WARRANTY; without even the implied warranty of 1130f712c9SDave Chinner * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1230f712c9SDave Chinner * GNU General Public License for more details. 1330f712c9SDave Chinner * 1430f712c9SDave Chinner * You should have received a copy of the GNU General Public License 1530f712c9SDave Chinner * along with this program; if not, write the Free Software Foundation, 1630f712c9SDave Chinner * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 1730f712c9SDave Chinner */ 1830f712c9SDave Chinner #include "xfs.h" 1930f712c9SDave Chinner #include "xfs_fs.h" 2030f712c9SDave Chinner #include "xfs_shared.h" 2130f712c9SDave Chinner #include "xfs_format.h" 2230f712c9SDave Chinner #include "xfs_log_format.h" 2330f712c9SDave Chinner #include "xfs_trans_resv.h" 2430f712c9SDave Chinner #include "xfs_bit.h" 2530f712c9SDave Chinner #include "xfs_sb.h" 2630f712c9SDave Chinner #include "xfs_mount.h" 273ab78df2SDarrick J. Wong #include "xfs_defer.h" 2830f712c9SDave Chinner #include "xfs_inode.h" 2930f712c9SDave Chinner #include "xfs_btree.h" 3030f712c9SDave Chinner #include "xfs_ialloc.h" 3130f712c9SDave Chinner #include "xfs_ialloc_btree.h" 3230f712c9SDave Chinner #include "xfs_alloc.h" 3330f712c9SDave Chinner #include "xfs_rtalloc.h" 3430f712c9SDave Chinner #include "xfs_error.h" 3530f712c9SDave Chinner #include "xfs_bmap.h" 3630f712c9SDave Chinner #include "xfs_cksum.h" 3730f712c9SDave Chinner #include "xfs_trans.h" 3830f712c9SDave Chinner #include "xfs_buf_item.h" 3930f712c9SDave Chinner #include "xfs_icreate_item.h" 4030f712c9SDave Chinner #include "xfs_icache.h" 4130f712c9SDave Chinner #include "xfs_trace.h" 42a45086e2SBrian Foster #include "xfs_log.h" 43340785ccSDarrick J. Wong #include "xfs_rmap.h" 4430f712c9SDave Chinner 4530f712c9SDave Chinner 4630f712c9SDave Chinner /* 4730f712c9SDave Chinner * Allocation group level functions. 4830f712c9SDave Chinner */ 49*e936945eSDarrick J. Wong int 5030f712c9SDave Chinner xfs_ialloc_cluster_alignment( 517a1df156SDave Chinner struct xfs_mount *mp) 5230f712c9SDave Chinner { 537a1df156SDave Chinner if (xfs_sb_version_hasalign(&mp->m_sb) && 548ee9fdbeSChandan Rajendra mp->m_sb.sb_inoalignmt >= xfs_icluster_size_fsb(mp)) 557a1df156SDave Chinner return mp->m_sb.sb_inoalignmt; 5630f712c9SDave Chinner return 1; 5730f712c9SDave Chinner } 5830f712c9SDave Chinner 5930f712c9SDave Chinner /* 6030f712c9SDave Chinner * Lookup a record by ino in the btree given by cur. 6130f712c9SDave Chinner */ 6230f712c9SDave Chinner int /* error */ 6330f712c9SDave Chinner xfs_inobt_lookup( 6430f712c9SDave Chinner struct xfs_btree_cur *cur, /* btree cursor */ 6530f712c9SDave Chinner xfs_agino_t ino, /* starting inode of chunk */ 6630f712c9SDave Chinner xfs_lookup_t dir, /* <=, >=, == */ 6730f712c9SDave Chinner int *stat) /* success/failure */ 6830f712c9SDave Chinner { 6930f712c9SDave Chinner cur->bc_rec.i.ir_startino = ino; 705419040fSBrian Foster cur->bc_rec.i.ir_holemask = 0; 715419040fSBrian Foster cur->bc_rec.i.ir_count = 0; 7230f712c9SDave Chinner cur->bc_rec.i.ir_freecount = 0; 7330f712c9SDave Chinner cur->bc_rec.i.ir_free = 0; 7430f712c9SDave Chinner return xfs_btree_lookup(cur, dir, stat); 7530f712c9SDave Chinner } 7630f712c9SDave Chinner 7730f712c9SDave Chinner /* 7830f712c9SDave Chinner * Update the record referred to by cur to the value given. 7930f712c9SDave Chinner * This either works (return 0) or gets an EFSCORRUPTED error. 8030f712c9SDave Chinner */ 8130f712c9SDave Chinner STATIC int /* error */ 8230f712c9SDave Chinner xfs_inobt_update( 8330f712c9SDave Chinner struct xfs_btree_cur *cur, /* btree cursor */ 8430f712c9SDave Chinner xfs_inobt_rec_incore_t *irec) /* btree record */ 8530f712c9SDave Chinner { 8630f712c9SDave Chinner union xfs_btree_rec rec; 8730f712c9SDave Chinner 8830f712c9SDave Chinner rec.inobt.ir_startino = cpu_to_be32(irec->ir_startino); 895419040fSBrian Foster if (xfs_sb_version_hassparseinodes(&cur->bc_mp->m_sb)) { 905419040fSBrian Foster rec.inobt.ir_u.sp.ir_holemask = cpu_to_be16(irec->ir_holemask); 915419040fSBrian Foster rec.inobt.ir_u.sp.ir_count = irec->ir_count; 925419040fSBrian Foster rec.inobt.ir_u.sp.ir_freecount = irec->ir_freecount; 935419040fSBrian Foster } else { 945419040fSBrian Foster /* ir_holemask/ir_count not supported on-disk */ 955419040fSBrian Foster rec.inobt.ir_u.f.ir_freecount = cpu_to_be32(irec->ir_freecount); 965419040fSBrian Foster } 9730f712c9SDave Chinner rec.inobt.ir_free = cpu_to_be64(irec->ir_free); 9830f712c9SDave Chinner return xfs_btree_update(cur, &rec); 9930f712c9SDave Chinner } 10030f712c9SDave Chinner 101*e936945eSDarrick J. Wong /* Convert on-disk btree record to incore inobt record. */ 102*e936945eSDarrick J. Wong void 103*e936945eSDarrick J. Wong xfs_inobt_btrec_to_irec( 104*e936945eSDarrick J. Wong struct xfs_mount *mp, 105*e936945eSDarrick J. Wong union xfs_btree_rec *rec, 106*e936945eSDarrick J. Wong struct xfs_inobt_rec_incore *irec) 10730f712c9SDave Chinner { 1085419040fSBrian Foster irec->ir_startino = be32_to_cpu(rec->inobt.ir_startino); 109*e936945eSDarrick J. Wong if (xfs_sb_version_hassparseinodes(&mp->m_sb)) { 1105419040fSBrian Foster irec->ir_holemask = be16_to_cpu(rec->inobt.ir_u.sp.ir_holemask); 1115419040fSBrian Foster irec->ir_count = rec->inobt.ir_u.sp.ir_count; 1125419040fSBrian Foster irec->ir_freecount = rec->inobt.ir_u.sp.ir_freecount; 1135419040fSBrian Foster } else { 1145419040fSBrian Foster /* 1155419040fSBrian Foster * ir_holemask/ir_count not supported on-disk. Fill in hardcoded 1165419040fSBrian Foster * values for full inode chunks. 1175419040fSBrian Foster */ 1185419040fSBrian Foster irec->ir_holemask = XFS_INOBT_HOLEMASK_FULL; 1195419040fSBrian Foster irec->ir_count = XFS_INODES_PER_CHUNK; 1205419040fSBrian Foster irec->ir_freecount = 1215419040fSBrian Foster be32_to_cpu(rec->inobt.ir_u.f.ir_freecount); 1225419040fSBrian Foster } 1235419040fSBrian Foster irec->ir_free = be64_to_cpu(rec->inobt.ir_free); 124*e936945eSDarrick J. Wong } 125*e936945eSDarrick J. Wong 126*e936945eSDarrick J. Wong /* 127*e936945eSDarrick J. Wong * Get the data from the pointed-to record. 128*e936945eSDarrick J. Wong */ 129*e936945eSDarrick J. Wong int 130*e936945eSDarrick J. Wong xfs_inobt_get_rec( 131*e936945eSDarrick J. Wong struct xfs_btree_cur *cur, 132*e936945eSDarrick J. Wong struct xfs_inobt_rec_incore *irec, 133*e936945eSDarrick J. Wong int *stat) 134*e936945eSDarrick J. Wong { 135*e936945eSDarrick J. Wong union xfs_btree_rec *rec; 136*e936945eSDarrick J. Wong int error; 137*e936945eSDarrick J. Wong 138*e936945eSDarrick J. Wong error = xfs_btree_get_rec(cur, &rec, stat); 139*e936945eSDarrick J. Wong if (error || *stat == 0) 140*e936945eSDarrick J. Wong return error; 141*e936945eSDarrick J. Wong 142*e936945eSDarrick J. Wong xfs_inobt_btrec_to_irec(cur->bc_mp, rec, irec); 1435419040fSBrian Foster 1445419040fSBrian Foster return 0; 14530f712c9SDave Chinner } 14630f712c9SDave Chinner 14730f712c9SDave Chinner /* 14830f712c9SDave Chinner * Insert a single inobt record. Cursor must already point to desired location. 14930f712c9SDave Chinner */ 15030f712c9SDave Chinner STATIC int 15130f712c9SDave Chinner xfs_inobt_insert_rec( 15230f712c9SDave Chinner struct xfs_btree_cur *cur, 153c8ce540dSDarrick J. Wong uint16_t holemask, 154c8ce540dSDarrick J. Wong uint8_t count, 155c8ce540dSDarrick J. Wong int32_t freecount, 15630f712c9SDave Chinner xfs_inofree_t free, 15730f712c9SDave Chinner int *stat) 15830f712c9SDave Chinner { 1595419040fSBrian Foster cur->bc_rec.i.ir_holemask = holemask; 1605419040fSBrian Foster cur->bc_rec.i.ir_count = count; 16130f712c9SDave Chinner cur->bc_rec.i.ir_freecount = freecount; 16230f712c9SDave Chinner cur->bc_rec.i.ir_free = free; 16330f712c9SDave Chinner return xfs_btree_insert(cur, stat); 16430f712c9SDave Chinner } 16530f712c9SDave Chinner 16630f712c9SDave Chinner /* 16730f712c9SDave Chinner * Insert records describing a newly allocated inode chunk into the inobt. 16830f712c9SDave Chinner */ 16930f712c9SDave Chinner STATIC int 17030f712c9SDave Chinner xfs_inobt_insert( 17130f712c9SDave Chinner struct xfs_mount *mp, 17230f712c9SDave Chinner struct xfs_trans *tp, 17330f712c9SDave Chinner struct xfs_buf *agbp, 17430f712c9SDave Chinner xfs_agino_t newino, 17530f712c9SDave Chinner xfs_agino_t newlen, 17630f712c9SDave Chinner xfs_btnum_t btnum) 17730f712c9SDave Chinner { 17830f712c9SDave Chinner struct xfs_btree_cur *cur; 17930f712c9SDave Chinner struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp); 18030f712c9SDave Chinner xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno); 18130f712c9SDave Chinner xfs_agino_t thisino; 18230f712c9SDave Chinner int i; 18330f712c9SDave Chinner int error; 18430f712c9SDave Chinner 18530f712c9SDave Chinner cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, btnum); 18630f712c9SDave Chinner 18730f712c9SDave Chinner for (thisino = newino; 18830f712c9SDave Chinner thisino < newino + newlen; 18930f712c9SDave Chinner thisino += XFS_INODES_PER_CHUNK) { 19030f712c9SDave Chinner error = xfs_inobt_lookup(cur, thisino, XFS_LOOKUP_EQ, &i); 19130f712c9SDave Chinner if (error) { 19230f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); 19330f712c9SDave Chinner return error; 19430f712c9SDave Chinner } 19530f712c9SDave Chinner ASSERT(i == 0); 19630f712c9SDave Chinner 1975419040fSBrian Foster error = xfs_inobt_insert_rec(cur, XFS_INOBT_HOLEMASK_FULL, 1985419040fSBrian Foster XFS_INODES_PER_CHUNK, 1995419040fSBrian Foster XFS_INODES_PER_CHUNK, 20030f712c9SDave Chinner XFS_INOBT_ALL_FREE, &i); 20130f712c9SDave Chinner if (error) { 20230f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); 20330f712c9SDave Chinner return error; 20430f712c9SDave Chinner } 20530f712c9SDave Chinner ASSERT(i == 1); 20630f712c9SDave Chinner } 20730f712c9SDave Chinner 20830f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); 20930f712c9SDave Chinner 21030f712c9SDave Chinner return 0; 21130f712c9SDave Chinner } 21230f712c9SDave Chinner 21330f712c9SDave Chinner /* 21430f712c9SDave Chinner * Verify that the number of free inodes in the AGI is correct. 21530f712c9SDave Chinner */ 21630f712c9SDave Chinner #ifdef DEBUG 21730f712c9SDave Chinner STATIC int 21830f712c9SDave Chinner xfs_check_agi_freecount( 21930f712c9SDave Chinner struct xfs_btree_cur *cur, 22030f712c9SDave Chinner struct xfs_agi *agi) 22130f712c9SDave Chinner { 22230f712c9SDave Chinner if (cur->bc_nlevels == 1) { 22330f712c9SDave Chinner xfs_inobt_rec_incore_t rec; 22430f712c9SDave Chinner int freecount = 0; 22530f712c9SDave Chinner int error; 22630f712c9SDave Chinner int i; 22730f712c9SDave Chinner 22830f712c9SDave Chinner error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i); 22930f712c9SDave Chinner if (error) 23030f712c9SDave Chinner return error; 23130f712c9SDave Chinner 23230f712c9SDave Chinner do { 23330f712c9SDave Chinner error = xfs_inobt_get_rec(cur, &rec, &i); 23430f712c9SDave Chinner if (error) 23530f712c9SDave Chinner return error; 23630f712c9SDave Chinner 23730f712c9SDave Chinner if (i) { 23830f712c9SDave Chinner freecount += rec.ir_freecount; 23930f712c9SDave Chinner error = xfs_btree_increment(cur, 0, &i); 24030f712c9SDave Chinner if (error) 24130f712c9SDave Chinner return error; 24230f712c9SDave Chinner } 24330f712c9SDave Chinner } while (i == 1); 24430f712c9SDave Chinner 24530f712c9SDave Chinner if (!XFS_FORCED_SHUTDOWN(cur->bc_mp)) 24630f712c9SDave Chinner ASSERT(freecount == be32_to_cpu(agi->agi_freecount)); 24730f712c9SDave Chinner } 24830f712c9SDave Chinner return 0; 24930f712c9SDave Chinner } 25030f712c9SDave Chinner #else 25130f712c9SDave Chinner #define xfs_check_agi_freecount(cur, agi) 0 25230f712c9SDave Chinner #endif 25330f712c9SDave Chinner 25430f712c9SDave Chinner /* 25530f712c9SDave Chinner * Initialise a new set of inodes. When called without a transaction context 25630f712c9SDave Chinner * (e.g. from recovery) we initiate a delayed write of the inode buffers rather 25730f712c9SDave Chinner * than logging them (which in a transaction context puts them into the AIL 25830f712c9SDave Chinner * for writeback rather than the xfsbufd queue). 25930f712c9SDave Chinner */ 26030f712c9SDave Chinner int 26130f712c9SDave Chinner xfs_ialloc_inode_init( 26230f712c9SDave Chinner struct xfs_mount *mp, 26330f712c9SDave Chinner struct xfs_trans *tp, 26430f712c9SDave Chinner struct list_head *buffer_list, 265463958afSBrian Foster int icount, 26630f712c9SDave Chinner xfs_agnumber_t agno, 26730f712c9SDave Chinner xfs_agblock_t agbno, 26830f712c9SDave Chinner xfs_agblock_t length, 26930f712c9SDave Chinner unsigned int gen) 27030f712c9SDave Chinner { 27130f712c9SDave Chinner struct xfs_buf *fbuf; 27230f712c9SDave Chinner struct xfs_dinode *free; 27330f712c9SDave Chinner int nbufs, blks_per_cluster, inodes_per_cluster; 27430f712c9SDave Chinner int version; 27530f712c9SDave Chinner int i, j; 27630f712c9SDave Chinner xfs_daddr_t d; 27730f712c9SDave Chinner xfs_ino_t ino = 0; 27830f712c9SDave Chinner 27930f712c9SDave Chinner /* 28030f712c9SDave Chinner * Loop over the new block(s), filling in the inodes. For small block 28130f712c9SDave Chinner * sizes, manipulate the inodes in buffers which are multiples of the 28230f712c9SDave Chinner * blocks size. 28330f712c9SDave Chinner */ 28430f712c9SDave Chinner blks_per_cluster = xfs_icluster_size_fsb(mp); 28530f712c9SDave Chinner inodes_per_cluster = blks_per_cluster << mp->m_sb.sb_inopblog; 28630f712c9SDave Chinner nbufs = length / blks_per_cluster; 28730f712c9SDave Chinner 28830f712c9SDave Chinner /* 28930f712c9SDave Chinner * Figure out what version number to use in the inodes we create. If 29030f712c9SDave Chinner * the superblock version has caught up to the one that supports the new 29130f712c9SDave Chinner * inode format, then use the new inode version. Otherwise use the old 29230f712c9SDave Chinner * version so that old kernels will continue to be able to use the file 29330f712c9SDave Chinner * system. 29430f712c9SDave Chinner * 29530f712c9SDave Chinner * For v3 inodes, we also need to write the inode number into the inode, 29630f712c9SDave Chinner * so calculate the first inode number of the chunk here as 29730f712c9SDave Chinner * XFS_OFFBNO_TO_AGINO() only works within a filesystem block, not 29830f712c9SDave Chinner * across multiple filesystem blocks (such as a cluster) and so cannot 29930f712c9SDave Chinner * be used in the cluster buffer loop below. 30030f712c9SDave Chinner * 30130f712c9SDave Chinner * Further, because we are writing the inode directly into the buffer 30230f712c9SDave Chinner * and calculating a CRC on the entire inode, we have ot log the entire 30330f712c9SDave Chinner * inode so that the entire range the CRC covers is present in the log. 30430f712c9SDave Chinner * That means for v3 inode we log the entire buffer rather than just the 30530f712c9SDave Chinner * inode cores. 30630f712c9SDave Chinner */ 30730f712c9SDave Chinner if (xfs_sb_version_hascrc(&mp->m_sb)) { 30830f712c9SDave Chinner version = 3; 30930f712c9SDave Chinner ino = XFS_AGINO_TO_INO(mp, agno, 31030f712c9SDave Chinner XFS_OFFBNO_TO_AGINO(mp, agbno, 0)); 31130f712c9SDave Chinner 31230f712c9SDave Chinner /* 31330f712c9SDave Chinner * log the initialisation that is about to take place as an 31430f712c9SDave Chinner * logical operation. This means the transaction does not 31530f712c9SDave Chinner * need to log the physical changes to the inode buffers as log 31630f712c9SDave Chinner * recovery will know what initialisation is actually needed. 31730f712c9SDave Chinner * Hence we only need to log the buffers as "ordered" buffers so 31830f712c9SDave Chinner * they track in the AIL as if they were physically logged. 31930f712c9SDave Chinner */ 32030f712c9SDave Chinner if (tp) 321463958afSBrian Foster xfs_icreate_log(tp, agno, agbno, icount, 32230f712c9SDave Chinner mp->m_sb.sb_inodesize, length, gen); 32330f712c9SDave Chinner } else 32430f712c9SDave Chinner version = 2; 32530f712c9SDave Chinner 32630f712c9SDave Chinner for (j = 0; j < nbufs; j++) { 32730f712c9SDave Chinner /* 32830f712c9SDave Chinner * Get the block. 32930f712c9SDave Chinner */ 33030f712c9SDave Chinner d = XFS_AGB_TO_DADDR(mp, agno, agbno + (j * blks_per_cluster)); 33130f712c9SDave Chinner fbuf = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, 33230f712c9SDave Chinner mp->m_bsize * blks_per_cluster, 33330f712c9SDave Chinner XBF_UNMAPPED); 33430f712c9SDave Chinner if (!fbuf) 3352451337dSDave Chinner return -ENOMEM; 33630f712c9SDave Chinner 33730f712c9SDave Chinner /* Initialize the inode buffers and log them appropriately. */ 33830f712c9SDave Chinner fbuf->b_ops = &xfs_inode_buf_ops; 33930f712c9SDave Chinner xfs_buf_zero(fbuf, 0, BBTOB(fbuf->b_length)); 34030f712c9SDave Chinner for (i = 0; i < inodes_per_cluster; i++) { 34130f712c9SDave Chinner int ioffset = i << mp->m_sb.sb_inodelog; 34230f712c9SDave Chinner uint isize = xfs_dinode_size(version); 34330f712c9SDave Chinner 34430f712c9SDave Chinner free = xfs_make_iptr(mp, fbuf, i); 34530f712c9SDave Chinner free->di_magic = cpu_to_be16(XFS_DINODE_MAGIC); 34630f712c9SDave Chinner free->di_version = version; 34730f712c9SDave Chinner free->di_gen = cpu_to_be32(gen); 34830f712c9SDave Chinner free->di_next_unlinked = cpu_to_be32(NULLAGINO); 34930f712c9SDave Chinner 35030f712c9SDave Chinner if (version == 3) { 35130f712c9SDave Chinner free->di_ino = cpu_to_be64(ino); 35230f712c9SDave Chinner ino++; 353ce748eaaSEric Sandeen uuid_copy(&free->di_uuid, 354ce748eaaSEric Sandeen &mp->m_sb.sb_meta_uuid); 35530f712c9SDave Chinner xfs_dinode_calc_crc(mp, free); 35630f712c9SDave Chinner } else if (tp) { 35730f712c9SDave Chinner /* just log the inode core */ 35830f712c9SDave Chinner xfs_trans_log_buf(tp, fbuf, ioffset, 35930f712c9SDave Chinner ioffset + isize - 1); 36030f712c9SDave Chinner } 36130f712c9SDave Chinner } 36230f712c9SDave Chinner 36330f712c9SDave Chinner if (tp) { 36430f712c9SDave Chinner /* 36530f712c9SDave Chinner * Mark the buffer as an inode allocation buffer so it 36630f712c9SDave Chinner * sticks in AIL at the point of this allocation 36730f712c9SDave Chinner * transaction. This ensures the they are on disk before 36830f712c9SDave Chinner * the tail of the log can be moved past this 36930f712c9SDave Chinner * transaction (i.e. by preventing relogging from moving 37030f712c9SDave Chinner * it forward in the log). 37130f712c9SDave Chinner */ 37230f712c9SDave Chinner xfs_trans_inode_alloc_buf(tp, fbuf); 37330f712c9SDave Chinner if (version == 3) { 37430f712c9SDave Chinner /* 37530f712c9SDave Chinner * Mark the buffer as ordered so that they are 37630f712c9SDave Chinner * not physically logged in the transaction but 37730f712c9SDave Chinner * still tracked in the AIL as part of the 37830f712c9SDave Chinner * transaction and pin the log appropriately. 37930f712c9SDave Chinner */ 38030f712c9SDave Chinner xfs_trans_ordered_buf(tp, fbuf); 38130f712c9SDave Chinner xfs_trans_log_buf(tp, fbuf, 0, 38230f712c9SDave Chinner BBTOB(fbuf->b_length) - 1); 38330f712c9SDave Chinner } 38430f712c9SDave Chinner } else { 38530f712c9SDave Chinner fbuf->b_flags |= XBF_DONE; 38630f712c9SDave Chinner xfs_buf_delwri_queue(fbuf, buffer_list); 38730f712c9SDave Chinner xfs_buf_relse(fbuf); 38830f712c9SDave Chinner } 38930f712c9SDave Chinner } 39030f712c9SDave Chinner return 0; 39130f712c9SDave Chinner } 39230f712c9SDave Chinner 39330f712c9SDave Chinner /* 39456d1115cSBrian Foster * Align startino and allocmask for a recently allocated sparse chunk such that 39556d1115cSBrian Foster * they are fit for insertion (or merge) into the on-disk inode btrees. 39656d1115cSBrian Foster * 39756d1115cSBrian Foster * Background: 39856d1115cSBrian Foster * 39956d1115cSBrian Foster * When enabled, sparse inode support increases the inode alignment from cluster 40056d1115cSBrian Foster * size to inode chunk size. This means that the minimum range between two 40156d1115cSBrian Foster * non-adjacent inode records in the inobt is large enough for a full inode 40256d1115cSBrian Foster * record. This allows for cluster sized, cluster aligned block allocation 40356d1115cSBrian Foster * without need to worry about whether the resulting inode record overlaps with 40456d1115cSBrian Foster * another record in the tree. Without this basic rule, we would have to deal 40556d1115cSBrian Foster * with the consequences of overlap by potentially undoing recent allocations in 40656d1115cSBrian Foster * the inode allocation codepath. 40756d1115cSBrian Foster * 40856d1115cSBrian Foster * Because of this alignment rule (which is enforced on mount), there are two 40956d1115cSBrian Foster * inobt possibilities for newly allocated sparse chunks. One is that the 41056d1115cSBrian Foster * aligned inode record for the chunk covers a range of inodes not already 41156d1115cSBrian Foster * covered in the inobt (i.e., it is safe to insert a new sparse record). The 41256d1115cSBrian Foster * other is that a record already exists at the aligned startino that considers 41356d1115cSBrian Foster * the newly allocated range as sparse. In the latter case, record content is 41456d1115cSBrian Foster * merged in hope that sparse inode chunks fill to full chunks over time. 41556d1115cSBrian Foster */ 41656d1115cSBrian Foster STATIC void 41756d1115cSBrian Foster xfs_align_sparse_ino( 41856d1115cSBrian Foster struct xfs_mount *mp, 41956d1115cSBrian Foster xfs_agino_t *startino, 42056d1115cSBrian Foster uint16_t *allocmask) 42156d1115cSBrian Foster { 42256d1115cSBrian Foster xfs_agblock_t agbno; 42356d1115cSBrian Foster xfs_agblock_t mod; 42456d1115cSBrian Foster int offset; 42556d1115cSBrian Foster 42656d1115cSBrian Foster agbno = XFS_AGINO_TO_AGBNO(mp, *startino); 42756d1115cSBrian Foster mod = agbno % mp->m_sb.sb_inoalignmt; 42856d1115cSBrian Foster if (!mod) 42956d1115cSBrian Foster return; 43056d1115cSBrian Foster 43156d1115cSBrian Foster /* calculate the inode offset and align startino */ 43256d1115cSBrian Foster offset = mod << mp->m_sb.sb_inopblog; 43356d1115cSBrian Foster *startino -= offset; 43456d1115cSBrian Foster 43556d1115cSBrian Foster /* 43656d1115cSBrian Foster * Since startino has been aligned down, left shift allocmask such that 43756d1115cSBrian Foster * it continues to represent the same physical inodes relative to the 43856d1115cSBrian Foster * new startino. 43956d1115cSBrian Foster */ 44056d1115cSBrian Foster *allocmask <<= offset / XFS_INODES_PER_HOLEMASK_BIT; 44156d1115cSBrian Foster } 44256d1115cSBrian Foster 44356d1115cSBrian Foster /* 44456d1115cSBrian Foster * Determine whether the source inode record can merge into the target. Both 44556d1115cSBrian Foster * records must be sparse, the inode ranges must match and there must be no 44656d1115cSBrian Foster * allocation overlap between the records. 44756d1115cSBrian Foster */ 44856d1115cSBrian Foster STATIC bool 44956d1115cSBrian Foster __xfs_inobt_can_merge( 45056d1115cSBrian Foster struct xfs_inobt_rec_incore *trec, /* tgt record */ 45156d1115cSBrian Foster struct xfs_inobt_rec_incore *srec) /* src record */ 45256d1115cSBrian Foster { 45356d1115cSBrian Foster uint64_t talloc; 45456d1115cSBrian Foster uint64_t salloc; 45556d1115cSBrian Foster 45656d1115cSBrian Foster /* records must cover the same inode range */ 45756d1115cSBrian Foster if (trec->ir_startino != srec->ir_startino) 45856d1115cSBrian Foster return false; 45956d1115cSBrian Foster 46056d1115cSBrian Foster /* both records must be sparse */ 46156d1115cSBrian Foster if (!xfs_inobt_issparse(trec->ir_holemask) || 46256d1115cSBrian Foster !xfs_inobt_issparse(srec->ir_holemask)) 46356d1115cSBrian Foster return false; 46456d1115cSBrian Foster 46556d1115cSBrian Foster /* both records must track some inodes */ 46656d1115cSBrian Foster if (!trec->ir_count || !srec->ir_count) 46756d1115cSBrian Foster return false; 46856d1115cSBrian Foster 46956d1115cSBrian Foster /* can't exceed capacity of a full record */ 47056d1115cSBrian Foster if (trec->ir_count + srec->ir_count > XFS_INODES_PER_CHUNK) 47156d1115cSBrian Foster return false; 47256d1115cSBrian Foster 47356d1115cSBrian Foster /* verify there is no allocation overlap */ 47456d1115cSBrian Foster talloc = xfs_inobt_irec_to_allocmask(trec); 47556d1115cSBrian Foster salloc = xfs_inobt_irec_to_allocmask(srec); 47656d1115cSBrian Foster if (talloc & salloc) 47756d1115cSBrian Foster return false; 47856d1115cSBrian Foster 47956d1115cSBrian Foster return true; 48056d1115cSBrian Foster } 48156d1115cSBrian Foster 48256d1115cSBrian Foster /* 48356d1115cSBrian Foster * Merge the source inode record into the target. The caller must call 48456d1115cSBrian Foster * __xfs_inobt_can_merge() to ensure the merge is valid. 48556d1115cSBrian Foster */ 48656d1115cSBrian Foster STATIC void 48756d1115cSBrian Foster __xfs_inobt_rec_merge( 48856d1115cSBrian Foster struct xfs_inobt_rec_incore *trec, /* target */ 48956d1115cSBrian Foster struct xfs_inobt_rec_incore *srec) /* src */ 49056d1115cSBrian Foster { 49156d1115cSBrian Foster ASSERT(trec->ir_startino == srec->ir_startino); 49256d1115cSBrian Foster 49356d1115cSBrian Foster /* combine the counts */ 49456d1115cSBrian Foster trec->ir_count += srec->ir_count; 49556d1115cSBrian Foster trec->ir_freecount += srec->ir_freecount; 49656d1115cSBrian Foster 49756d1115cSBrian Foster /* 49856d1115cSBrian Foster * Merge the holemask and free mask. For both fields, 0 bits refer to 49956d1115cSBrian Foster * allocated inodes. We combine the allocated ranges with bitwise AND. 50056d1115cSBrian Foster */ 50156d1115cSBrian Foster trec->ir_holemask &= srec->ir_holemask; 50256d1115cSBrian Foster trec->ir_free &= srec->ir_free; 50356d1115cSBrian Foster } 50456d1115cSBrian Foster 50556d1115cSBrian Foster /* 50656d1115cSBrian Foster * Insert a new sparse inode chunk into the associated inode btree. The inode 50756d1115cSBrian Foster * record for the sparse chunk is pre-aligned to a startino that should match 50856d1115cSBrian Foster * any pre-existing sparse inode record in the tree. This allows sparse chunks 50956d1115cSBrian Foster * to fill over time. 51056d1115cSBrian Foster * 51156d1115cSBrian Foster * This function supports two modes of handling preexisting records depending on 51256d1115cSBrian Foster * the merge flag. If merge is true, the provided record is merged with the 51356d1115cSBrian Foster * existing record and updated in place. The merged record is returned in nrec. 51456d1115cSBrian Foster * If merge is false, an existing record is replaced with the provided record. 51556d1115cSBrian Foster * If no preexisting record exists, the provided record is always inserted. 51656d1115cSBrian Foster * 51756d1115cSBrian Foster * It is considered corruption if a merge is requested and not possible. Given 51856d1115cSBrian Foster * the sparse inode alignment constraints, this should never happen. 51956d1115cSBrian Foster */ 52056d1115cSBrian Foster STATIC int 52156d1115cSBrian Foster xfs_inobt_insert_sprec( 52256d1115cSBrian Foster struct xfs_mount *mp, 52356d1115cSBrian Foster struct xfs_trans *tp, 52456d1115cSBrian Foster struct xfs_buf *agbp, 52556d1115cSBrian Foster int btnum, 52656d1115cSBrian Foster struct xfs_inobt_rec_incore *nrec, /* in/out: new/merged rec. */ 52756d1115cSBrian Foster bool merge) /* merge or replace */ 52856d1115cSBrian Foster { 52956d1115cSBrian Foster struct xfs_btree_cur *cur; 53056d1115cSBrian Foster struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp); 53156d1115cSBrian Foster xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno); 53256d1115cSBrian Foster int error; 53356d1115cSBrian Foster int i; 53456d1115cSBrian Foster struct xfs_inobt_rec_incore rec; 53556d1115cSBrian Foster 53656d1115cSBrian Foster cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, btnum); 53756d1115cSBrian Foster 53856d1115cSBrian Foster /* the new record is pre-aligned so we know where to look */ 53956d1115cSBrian Foster error = xfs_inobt_lookup(cur, nrec->ir_startino, XFS_LOOKUP_EQ, &i); 54056d1115cSBrian Foster if (error) 54156d1115cSBrian Foster goto error; 54256d1115cSBrian Foster /* if nothing there, insert a new record and return */ 54356d1115cSBrian Foster if (i == 0) { 54456d1115cSBrian Foster error = xfs_inobt_insert_rec(cur, nrec->ir_holemask, 54556d1115cSBrian Foster nrec->ir_count, nrec->ir_freecount, 54656d1115cSBrian Foster nrec->ir_free, &i); 54756d1115cSBrian Foster if (error) 54856d1115cSBrian Foster goto error; 54956d1115cSBrian Foster XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error); 55056d1115cSBrian Foster 55156d1115cSBrian Foster goto out; 55256d1115cSBrian Foster } 55356d1115cSBrian Foster 55456d1115cSBrian Foster /* 55556d1115cSBrian Foster * A record exists at this startino. Merge or replace the record 55656d1115cSBrian Foster * depending on what we've been asked to do. 55756d1115cSBrian Foster */ 55856d1115cSBrian Foster if (merge) { 55956d1115cSBrian Foster error = xfs_inobt_get_rec(cur, &rec, &i); 56056d1115cSBrian Foster if (error) 56156d1115cSBrian Foster goto error; 56256d1115cSBrian Foster XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error); 56356d1115cSBrian Foster XFS_WANT_CORRUPTED_GOTO(mp, 56456d1115cSBrian Foster rec.ir_startino == nrec->ir_startino, 56556d1115cSBrian Foster error); 56656d1115cSBrian Foster 56756d1115cSBrian Foster /* 56856d1115cSBrian Foster * This should never fail. If we have coexisting records that 56956d1115cSBrian Foster * cannot merge, something is seriously wrong. 57056d1115cSBrian Foster */ 57156d1115cSBrian Foster XFS_WANT_CORRUPTED_GOTO(mp, __xfs_inobt_can_merge(nrec, &rec), 57256d1115cSBrian Foster error); 57356d1115cSBrian Foster 57456d1115cSBrian Foster trace_xfs_irec_merge_pre(mp, agno, rec.ir_startino, 57556d1115cSBrian Foster rec.ir_holemask, nrec->ir_startino, 57656d1115cSBrian Foster nrec->ir_holemask); 57756d1115cSBrian Foster 57856d1115cSBrian Foster /* merge to nrec to output the updated record */ 57956d1115cSBrian Foster __xfs_inobt_rec_merge(nrec, &rec); 58056d1115cSBrian Foster 58156d1115cSBrian Foster trace_xfs_irec_merge_post(mp, agno, nrec->ir_startino, 58256d1115cSBrian Foster nrec->ir_holemask); 58356d1115cSBrian Foster 58456d1115cSBrian Foster error = xfs_inobt_rec_check_count(mp, nrec); 58556d1115cSBrian Foster if (error) 58656d1115cSBrian Foster goto error; 58756d1115cSBrian Foster } 58856d1115cSBrian Foster 58956d1115cSBrian Foster error = xfs_inobt_update(cur, nrec); 59056d1115cSBrian Foster if (error) 59156d1115cSBrian Foster goto error; 59256d1115cSBrian Foster 59356d1115cSBrian Foster out: 59456d1115cSBrian Foster xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); 59556d1115cSBrian Foster return 0; 59656d1115cSBrian Foster error: 59756d1115cSBrian Foster xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); 59856d1115cSBrian Foster return error; 59956d1115cSBrian Foster } 60056d1115cSBrian Foster 60156d1115cSBrian Foster /* 60230f712c9SDave Chinner * Allocate new inodes in the allocation group specified by agbp. 60330f712c9SDave Chinner * Return 0 for success, else error code. 60430f712c9SDave Chinner */ 60530f712c9SDave Chinner STATIC int /* error code or 0 */ 60630f712c9SDave Chinner xfs_ialloc_ag_alloc( 60730f712c9SDave Chinner xfs_trans_t *tp, /* transaction pointer */ 60830f712c9SDave Chinner xfs_buf_t *agbp, /* alloc group buffer */ 60930f712c9SDave Chinner int *alloc) 61030f712c9SDave Chinner { 61130f712c9SDave Chinner xfs_agi_t *agi; /* allocation group header */ 61230f712c9SDave Chinner xfs_alloc_arg_t args; /* allocation argument structure */ 61330f712c9SDave Chinner xfs_agnumber_t agno; 61430f712c9SDave Chinner int error; 61530f712c9SDave Chinner xfs_agino_t newino; /* new first inode's number */ 61630f712c9SDave Chinner xfs_agino_t newlen; /* new number of inodes */ 61730f712c9SDave Chinner int isaligned = 0; /* inode allocation at stripe unit */ 61830f712c9SDave Chinner /* boundary */ 61956d1115cSBrian Foster uint16_t allocmask = (uint16_t) -1; /* init. to full chunk */ 62056d1115cSBrian Foster struct xfs_inobt_rec_incore rec; 62130f712c9SDave Chinner struct xfs_perag *pag; 6221cdadee1SBrian Foster int do_sparse = 0; 6231cdadee1SBrian Foster 62430f712c9SDave Chinner memset(&args, 0, sizeof(args)); 62530f712c9SDave Chinner args.tp = tp; 62630f712c9SDave Chinner args.mp = tp->t_mountp; 6271cdadee1SBrian Foster args.fsbno = NULLFSBLOCK; 628340785ccSDarrick J. Wong xfs_rmap_ag_owner(&args.oinfo, XFS_RMAP_OWN_INODES); 62930f712c9SDave Chinner 63046fc58daSBrian Foster #ifdef DEBUG 63146fc58daSBrian Foster /* randomly do sparse inode allocations */ 63246fc58daSBrian Foster if (xfs_sb_version_hassparseinodes(&tp->t_mountp->m_sb) && 63346fc58daSBrian Foster args.mp->m_ialloc_min_blks < args.mp->m_ialloc_blks) 63446fc58daSBrian Foster do_sparse = prandom_u32() & 1; 63546fc58daSBrian Foster #endif 63646fc58daSBrian Foster 63730f712c9SDave Chinner /* 63830f712c9SDave Chinner * Locking will ensure that we don't have two callers in here 63930f712c9SDave Chinner * at one time. 64030f712c9SDave Chinner */ 64130f712c9SDave Chinner newlen = args.mp->m_ialloc_inos; 64230f712c9SDave Chinner if (args.mp->m_maxicount && 64374f9ce1cSGeorge Wang percpu_counter_read_positive(&args.mp->m_icount) + newlen > 644501ab323SDave Chinner args.mp->m_maxicount) 6452451337dSDave Chinner return -ENOSPC; 64630f712c9SDave Chinner args.minlen = args.maxlen = args.mp->m_ialloc_blks; 64730f712c9SDave Chinner /* 64830f712c9SDave Chinner * First try to allocate inodes contiguous with the last-allocated 64930f712c9SDave Chinner * chunk of inodes. If the filesystem is striped, this will fill 65030f712c9SDave Chinner * an entire stripe unit with inodes. 65130f712c9SDave Chinner */ 65230f712c9SDave Chinner agi = XFS_BUF_TO_AGI(agbp); 65330f712c9SDave Chinner newino = be32_to_cpu(agi->agi_newino); 65430f712c9SDave Chinner agno = be32_to_cpu(agi->agi_seqno); 65530f712c9SDave Chinner args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) + 65630f712c9SDave Chinner args.mp->m_ialloc_blks; 6571cdadee1SBrian Foster if (do_sparse) 6581cdadee1SBrian Foster goto sparse_alloc; 65930f712c9SDave Chinner if (likely(newino != NULLAGINO && 66030f712c9SDave Chinner (args.agbno < be32_to_cpu(agi->agi_length)))) { 66130f712c9SDave Chinner args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno); 66230f712c9SDave Chinner args.type = XFS_ALLOCTYPE_THIS_BNO; 66330f712c9SDave Chinner args.prod = 1; 66430f712c9SDave Chinner 66530f712c9SDave Chinner /* 66630f712c9SDave Chinner * We need to take into account alignment here to ensure that 66730f712c9SDave Chinner * we don't modify the free list if we fail to have an exact 66830f712c9SDave Chinner * block. If we don't have an exact match, and every oher 66930f712c9SDave Chinner * attempt allocation attempt fails, we'll end up cancelling 67030f712c9SDave Chinner * a dirty transaction and shutting down. 67130f712c9SDave Chinner * 67230f712c9SDave Chinner * For an exact allocation, alignment must be 1, 67330f712c9SDave Chinner * however we need to take cluster alignment into account when 67430f712c9SDave Chinner * fixing up the freelist. Use the minalignslop field to 67530f712c9SDave Chinner * indicate that extra blocks might be required for alignment, 67630f712c9SDave Chinner * but not to use them in the actual exact allocation. 67730f712c9SDave Chinner */ 67830f712c9SDave Chinner args.alignment = 1; 6797a1df156SDave Chinner args.minalignslop = xfs_ialloc_cluster_alignment(args.mp) - 1; 68030f712c9SDave Chinner 68130f712c9SDave Chinner /* Allow space for the inode btree to split. */ 68230f712c9SDave Chinner args.minleft = args.mp->m_in_maxlevels - 1; 68330f712c9SDave Chinner if ((error = xfs_alloc_vextent(&args))) 68430f712c9SDave Chinner return error; 68530f712c9SDave Chinner 68630f712c9SDave Chinner /* 68730f712c9SDave Chinner * This request might have dirtied the transaction if the AG can 68830f712c9SDave Chinner * satisfy the request, but the exact block was not available. 68930f712c9SDave Chinner * If the allocation did fail, subsequent requests will relax 69030f712c9SDave Chinner * the exact agbno requirement and increase the alignment 69130f712c9SDave Chinner * instead. It is critical that the total size of the request 69230f712c9SDave Chinner * (len + alignment + slop) does not increase from this point 69330f712c9SDave Chinner * on, so reset minalignslop to ensure it is not included in 69430f712c9SDave Chinner * subsequent requests. 69530f712c9SDave Chinner */ 69630f712c9SDave Chinner args.minalignslop = 0; 6971cdadee1SBrian Foster } 69830f712c9SDave Chinner 69930f712c9SDave Chinner if (unlikely(args.fsbno == NULLFSBLOCK)) { 70030f712c9SDave Chinner /* 70130f712c9SDave Chinner * Set the alignment for the allocation. 70230f712c9SDave Chinner * If stripe alignment is turned on then align at stripe unit 70330f712c9SDave Chinner * boundary. 70430f712c9SDave Chinner * If the cluster size is smaller than a filesystem block 70530f712c9SDave Chinner * then we're doing I/O for inodes in filesystem block size 70630f712c9SDave Chinner * pieces, so don't need alignment anyway. 70730f712c9SDave Chinner */ 70830f712c9SDave Chinner isaligned = 0; 70930f712c9SDave Chinner if (args.mp->m_sinoalign) { 71030f712c9SDave Chinner ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN)); 71130f712c9SDave Chinner args.alignment = args.mp->m_dalign; 71230f712c9SDave Chinner isaligned = 1; 71330f712c9SDave Chinner } else 7147a1df156SDave Chinner args.alignment = xfs_ialloc_cluster_alignment(args.mp); 71530f712c9SDave Chinner /* 71630f712c9SDave Chinner * Need to figure out where to allocate the inode blocks. 71730f712c9SDave Chinner * Ideally they should be spaced out through the a.g. 71830f712c9SDave Chinner * For now, just allocate blocks up front. 71930f712c9SDave Chinner */ 72030f712c9SDave Chinner args.agbno = be32_to_cpu(agi->agi_root); 72130f712c9SDave Chinner args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno); 72230f712c9SDave Chinner /* 72330f712c9SDave Chinner * Allocate a fixed-size extent of inodes. 72430f712c9SDave Chinner */ 72530f712c9SDave Chinner args.type = XFS_ALLOCTYPE_NEAR_BNO; 72630f712c9SDave Chinner args.prod = 1; 72730f712c9SDave Chinner /* 72830f712c9SDave Chinner * Allow space for the inode btree to split. 72930f712c9SDave Chinner */ 73030f712c9SDave Chinner args.minleft = args.mp->m_in_maxlevels - 1; 73130f712c9SDave Chinner if ((error = xfs_alloc_vextent(&args))) 73230f712c9SDave Chinner return error; 73330f712c9SDave Chinner } 73430f712c9SDave Chinner 73530f712c9SDave Chinner /* 73630f712c9SDave Chinner * If stripe alignment is turned on, then try again with cluster 73730f712c9SDave Chinner * alignment. 73830f712c9SDave Chinner */ 73930f712c9SDave Chinner if (isaligned && args.fsbno == NULLFSBLOCK) { 74030f712c9SDave Chinner args.type = XFS_ALLOCTYPE_NEAR_BNO; 74130f712c9SDave Chinner args.agbno = be32_to_cpu(agi->agi_root); 74230f712c9SDave Chinner args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno); 7437a1df156SDave Chinner args.alignment = xfs_ialloc_cluster_alignment(args.mp); 74430f712c9SDave Chinner if ((error = xfs_alloc_vextent(&args))) 74530f712c9SDave Chinner return error; 74630f712c9SDave Chinner } 74730f712c9SDave Chinner 74856d1115cSBrian Foster /* 74956d1115cSBrian Foster * Finally, try a sparse allocation if the filesystem supports it and 75056d1115cSBrian Foster * the sparse allocation length is smaller than a full chunk. 75156d1115cSBrian Foster */ 75256d1115cSBrian Foster if (xfs_sb_version_hassparseinodes(&args.mp->m_sb) && 75356d1115cSBrian Foster args.mp->m_ialloc_min_blks < args.mp->m_ialloc_blks && 75456d1115cSBrian Foster args.fsbno == NULLFSBLOCK) { 7551cdadee1SBrian Foster sparse_alloc: 75656d1115cSBrian Foster args.type = XFS_ALLOCTYPE_NEAR_BNO; 75756d1115cSBrian Foster args.agbno = be32_to_cpu(agi->agi_root); 75856d1115cSBrian Foster args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno); 75956d1115cSBrian Foster args.alignment = args.mp->m_sb.sb_spino_align; 76056d1115cSBrian Foster args.prod = 1; 76156d1115cSBrian Foster 76256d1115cSBrian Foster args.minlen = args.mp->m_ialloc_min_blks; 76356d1115cSBrian Foster args.maxlen = args.minlen; 76456d1115cSBrian Foster 76556d1115cSBrian Foster /* 76656d1115cSBrian Foster * The inode record will be aligned to full chunk size. We must 76756d1115cSBrian Foster * prevent sparse allocation from AG boundaries that result in 76856d1115cSBrian Foster * invalid inode records, such as records that start at agbno 0 76956d1115cSBrian Foster * or extend beyond the AG. 77056d1115cSBrian Foster * 77156d1115cSBrian Foster * Set min agbno to the first aligned, non-zero agbno and max to 77256d1115cSBrian Foster * the last aligned agbno that is at least one full chunk from 77356d1115cSBrian Foster * the end of the AG. 77456d1115cSBrian Foster */ 77556d1115cSBrian Foster args.min_agbno = args.mp->m_sb.sb_inoalignmt; 77656d1115cSBrian Foster args.max_agbno = round_down(args.mp->m_sb.sb_agblocks, 77756d1115cSBrian Foster args.mp->m_sb.sb_inoalignmt) - 77856d1115cSBrian Foster args.mp->m_ialloc_blks; 77956d1115cSBrian Foster 78056d1115cSBrian Foster error = xfs_alloc_vextent(&args); 78156d1115cSBrian Foster if (error) 78256d1115cSBrian Foster return error; 78356d1115cSBrian Foster 78456d1115cSBrian Foster newlen = args.len << args.mp->m_sb.sb_inopblog; 78546fc58daSBrian Foster ASSERT(newlen <= XFS_INODES_PER_CHUNK); 78656d1115cSBrian Foster allocmask = (1 << (newlen / XFS_INODES_PER_HOLEMASK_BIT)) - 1; 78756d1115cSBrian Foster } 78856d1115cSBrian Foster 78930f712c9SDave Chinner if (args.fsbno == NULLFSBLOCK) { 79030f712c9SDave Chinner *alloc = 0; 79130f712c9SDave Chinner return 0; 79230f712c9SDave Chinner } 79330f712c9SDave Chinner ASSERT(args.len == args.minlen); 79430f712c9SDave Chinner 79530f712c9SDave Chinner /* 79630f712c9SDave Chinner * Stamp and write the inode buffers. 79730f712c9SDave Chinner * 79830f712c9SDave Chinner * Seed the new inode cluster with a random generation number. This 79930f712c9SDave Chinner * prevents short-term reuse of generation numbers if a chunk is 80030f712c9SDave Chinner * freed and then immediately reallocated. We use random numbers 80130f712c9SDave Chinner * rather than a linear progression to prevent the next generation 80230f712c9SDave Chinner * number from being easily guessable. 80330f712c9SDave Chinner */ 804463958afSBrian Foster error = xfs_ialloc_inode_init(args.mp, tp, NULL, newlen, agno, 805463958afSBrian Foster args.agbno, args.len, prandom_u32()); 80630f712c9SDave Chinner 80730f712c9SDave Chinner if (error) 80830f712c9SDave Chinner return error; 80930f712c9SDave Chinner /* 81030f712c9SDave Chinner * Convert the results. 81130f712c9SDave Chinner */ 81230f712c9SDave Chinner newino = XFS_OFFBNO_TO_AGINO(args.mp, args.agbno, 0); 81356d1115cSBrian Foster 81456d1115cSBrian Foster if (xfs_inobt_issparse(~allocmask)) { 81556d1115cSBrian Foster /* 81656d1115cSBrian Foster * We've allocated a sparse chunk. Align the startino and mask. 81756d1115cSBrian Foster */ 81856d1115cSBrian Foster xfs_align_sparse_ino(args.mp, &newino, &allocmask); 81956d1115cSBrian Foster 82056d1115cSBrian Foster rec.ir_startino = newino; 82156d1115cSBrian Foster rec.ir_holemask = ~allocmask; 82256d1115cSBrian Foster rec.ir_count = newlen; 82356d1115cSBrian Foster rec.ir_freecount = newlen; 82456d1115cSBrian Foster rec.ir_free = XFS_INOBT_ALL_FREE; 82556d1115cSBrian Foster 82656d1115cSBrian Foster /* 82756d1115cSBrian Foster * Insert the sparse record into the inobt and allow for a merge 82856d1115cSBrian Foster * if necessary. If a merge does occur, rec is updated to the 82956d1115cSBrian Foster * merged record. 83056d1115cSBrian Foster */ 83156d1115cSBrian Foster error = xfs_inobt_insert_sprec(args.mp, tp, agbp, XFS_BTNUM_INO, 83256d1115cSBrian Foster &rec, true); 83356d1115cSBrian Foster if (error == -EFSCORRUPTED) { 83456d1115cSBrian Foster xfs_alert(args.mp, 83556d1115cSBrian Foster "invalid sparse inode record: ino 0x%llx holemask 0x%x count %u", 83656d1115cSBrian Foster XFS_AGINO_TO_INO(args.mp, agno, 83756d1115cSBrian Foster rec.ir_startino), 83856d1115cSBrian Foster rec.ir_holemask, rec.ir_count); 83956d1115cSBrian Foster xfs_force_shutdown(args.mp, SHUTDOWN_CORRUPT_INCORE); 84056d1115cSBrian Foster } 84156d1115cSBrian Foster if (error) 84256d1115cSBrian Foster return error; 84356d1115cSBrian Foster 84456d1115cSBrian Foster /* 84556d1115cSBrian Foster * We can't merge the part we've just allocated as for the inobt 84656d1115cSBrian Foster * due to finobt semantics. The original record may or may not 84756d1115cSBrian Foster * exist independent of whether physical inodes exist in this 84856d1115cSBrian Foster * sparse chunk. 84956d1115cSBrian Foster * 85056d1115cSBrian Foster * We must update the finobt record based on the inobt record. 85156d1115cSBrian Foster * rec contains the fully merged and up to date inobt record 85256d1115cSBrian Foster * from the previous call. Set merge false to replace any 85356d1115cSBrian Foster * existing record with this one. 85456d1115cSBrian Foster */ 85556d1115cSBrian Foster if (xfs_sb_version_hasfinobt(&args.mp->m_sb)) { 85656d1115cSBrian Foster error = xfs_inobt_insert_sprec(args.mp, tp, agbp, 85756d1115cSBrian Foster XFS_BTNUM_FINO, &rec, 85856d1115cSBrian Foster false); 85956d1115cSBrian Foster if (error) 86056d1115cSBrian Foster return error; 86156d1115cSBrian Foster } 86256d1115cSBrian Foster } else { 86356d1115cSBrian Foster /* full chunk - insert new records to both btrees */ 86456d1115cSBrian Foster error = xfs_inobt_insert(args.mp, tp, agbp, newino, newlen, 86556d1115cSBrian Foster XFS_BTNUM_INO); 86656d1115cSBrian Foster if (error) 86756d1115cSBrian Foster return error; 86856d1115cSBrian Foster 86956d1115cSBrian Foster if (xfs_sb_version_hasfinobt(&args.mp->m_sb)) { 87056d1115cSBrian Foster error = xfs_inobt_insert(args.mp, tp, agbp, newino, 87156d1115cSBrian Foster newlen, XFS_BTNUM_FINO); 87256d1115cSBrian Foster if (error) 87356d1115cSBrian Foster return error; 87456d1115cSBrian Foster } 87556d1115cSBrian Foster } 87656d1115cSBrian Foster 87756d1115cSBrian Foster /* 87856d1115cSBrian Foster * Update AGI counts and newino. 87956d1115cSBrian Foster */ 88030f712c9SDave Chinner be32_add_cpu(&agi->agi_count, newlen); 88130f712c9SDave Chinner be32_add_cpu(&agi->agi_freecount, newlen); 88230f712c9SDave Chinner pag = xfs_perag_get(args.mp, agno); 88330f712c9SDave Chinner pag->pagi_freecount += newlen; 88430f712c9SDave Chinner xfs_perag_put(pag); 88530f712c9SDave Chinner agi->agi_newino = cpu_to_be32(newino); 88630f712c9SDave Chinner 88730f712c9SDave Chinner /* 88830f712c9SDave Chinner * Log allocation group header fields 88930f712c9SDave Chinner */ 89030f712c9SDave Chinner xfs_ialloc_log_agi(tp, agbp, 89130f712c9SDave Chinner XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO); 89230f712c9SDave Chinner /* 89330f712c9SDave Chinner * Modify/log superblock values for inode count and inode free count. 89430f712c9SDave Chinner */ 89530f712c9SDave Chinner xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen); 89630f712c9SDave Chinner xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen); 89730f712c9SDave Chinner *alloc = 1; 89830f712c9SDave Chinner return 0; 89930f712c9SDave Chinner } 90030f712c9SDave Chinner 90130f712c9SDave Chinner STATIC xfs_agnumber_t 90230f712c9SDave Chinner xfs_ialloc_next_ag( 90330f712c9SDave Chinner xfs_mount_t *mp) 90430f712c9SDave Chinner { 90530f712c9SDave Chinner xfs_agnumber_t agno; 90630f712c9SDave Chinner 90730f712c9SDave Chinner spin_lock(&mp->m_agirotor_lock); 90830f712c9SDave Chinner agno = mp->m_agirotor; 90930f712c9SDave Chinner if (++mp->m_agirotor >= mp->m_maxagi) 91030f712c9SDave Chinner mp->m_agirotor = 0; 91130f712c9SDave Chinner spin_unlock(&mp->m_agirotor_lock); 91230f712c9SDave Chinner 91330f712c9SDave Chinner return agno; 91430f712c9SDave Chinner } 91530f712c9SDave Chinner 91630f712c9SDave Chinner /* 91730f712c9SDave Chinner * Select an allocation group to look for a free inode in, based on the parent 91830f712c9SDave Chinner * inode and the mode. Return the allocation group buffer. 91930f712c9SDave Chinner */ 92030f712c9SDave Chinner STATIC xfs_agnumber_t 92130f712c9SDave Chinner xfs_ialloc_ag_select( 92230f712c9SDave Chinner xfs_trans_t *tp, /* transaction pointer */ 92330f712c9SDave Chinner xfs_ino_t parent, /* parent directory inode number */ 92430f712c9SDave Chinner umode_t mode, /* bits set to indicate file type */ 92530f712c9SDave Chinner int okalloc) /* ok to allocate more space */ 92630f712c9SDave Chinner { 92730f712c9SDave Chinner xfs_agnumber_t agcount; /* number of ag's in the filesystem */ 92830f712c9SDave Chinner xfs_agnumber_t agno; /* current ag number */ 92930f712c9SDave Chinner int flags; /* alloc buffer locking flags */ 93030f712c9SDave Chinner xfs_extlen_t ineed; /* blocks needed for inode allocation */ 93130f712c9SDave Chinner xfs_extlen_t longest = 0; /* longest extent available */ 93230f712c9SDave Chinner xfs_mount_t *mp; /* mount point structure */ 93330f712c9SDave Chinner int needspace; /* file mode implies space allocated */ 93430f712c9SDave Chinner xfs_perag_t *pag; /* per allocation group data */ 93530f712c9SDave Chinner xfs_agnumber_t pagno; /* parent (starting) ag number */ 93630f712c9SDave Chinner int error; 93730f712c9SDave Chinner 93830f712c9SDave Chinner /* 93930f712c9SDave Chinner * Files of these types need at least one block if length > 0 94030f712c9SDave Chinner * (and they won't fit in the inode, but that's hard to figure out). 94130f712c9SDave Chinner */ 94230f712c9SDave Chinner needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode); 94330f712c9SDave Chinner mp = tp->t_mountp; 94430f712c9SDave Chinner agcount = mp->m_maxagi; 94530f712c9SDave Chinner if (S_ISDIR(mode)) 94630f712c9SDave Chinner pagno = xfs_ialloc_next_ag(mp); 94730f712c9SDave Chinner else { 94830f712c9SDave Chinner pagno = XFS_INO_TO_AGNO(mp, parent); 94930f712c9SDave Chinner if (pagno >= agcount) 95030f712c9SDave Chinner pagno = 0; 95130f712c9SDave Chinner } 95230f712c9SDave Chinner 95330f712c9SDave Chinner ASSERT(pagno < agcount); 95430f712c9SDave Chinner 95530f712c9SDave Chinner /* 95630f712c9SDave Chinner * Loop through allocation groups, looking for one with a little 95730f712c9SDave Chinner * free space in it. Note we don't look for free inodes, exactly. 95830f712c9SDave Chinner * Instead, we include whether there is a need to allocate inodes 95930f712c9SDave Chinner * to mean that blocks must be allocated for them, 96030f712c9SDave Chinner * if none are currently free. 96130f712c9SDave Chinner */ 96230f712c9SDave Chinner agno = pagno; 96330f712c9SDave Chinner flags = XFS_ALLOC_FLAG_TRYLOCK; 96430f712c9SDave Chinner for (;;) { 96530f712c9SDave Chinner pag = xfs_perag_get(mp, agno); 96630f712c9SDave Chinner if (!pag->pagi_inodeok) { 96730f712c9SDave Chinner xfs_ialloc_next_ag(mp); 96830f712c9SDave Chinner goto nextag; 96930f712c9SDave Chinner } 97030f712c9SDave Chinner 97130f712c9SDave Chinner if (!pag->pagi_init) { 97230f712c9SDave Chinner error = xfs_ialloc_pagi_init(mp, tp, agno); 97330f712c9SDave Chinner if (error) 97430f712c9SDave Chinner goto nextag; 97530f712c9SDave Chinner } 97630f712c9SDave Chinner 97730f712c9SDave Chinner if (pag->pagi_freecount) { 97830f712c9SDave Chinner xfs_perag_put(pag); 97930f712c9SDave Chinner return agno; 98030f712c9SDave Chinner } 98130f712c9SDave Chinner 98230f712c9SDave Chinner if (!okalloc) 98330f712c9SDave Chinner goto nextag; 98430f712c9SDave Chinner 98530f712c9SDave Chinner if (!pag->pagf_init) { 98630f712c9SDave Chinner error = xfs_alloc_pagf_init(mp, tp, agno, flags); 98730f712c9SDave Chinner if (error) 98830f712c9SDave Chinner goto nextag; 98930f712c9SDave Chinner } 99030f712c9SDave Chinner 99130f712c9SDave Chinner /* 9927a1df156SDave Chinner * Check that there is enough free space for the file plus a 9937a1df156SDave Chinner * chunk of inodes if we need to allocate some. If this is the 9947a1df156SDave Chinner * first pass across the AGs, take into account the potential 9957a1df156SDave Chinner * space needed for alignment of inode chunks when checking the 9967a1df156SDave Chinner * longest contiguous free space in the AG - this prevents us 9977a1df156SDave Chinner * from getting ENOSPC because we have free space larger than 9987a1df156SDave Chinner * m_ialloc_blks but alignment constraints prevent us from using 9997a1df156SDave Chinner * it. 10007a1df156SDave Chinner * 10017a1df156SDave Chinner * If we can't find an AG with space for full alignment slack to 10027a1df156SDave Chinner * be taken into account, we must be near ENOSPC in all AGs. 10037a1df156SDave Chinner * Hence we don't include alignment for the second pass and so 10047a1df156SDave Chinner * if we fail allocation due to alignment issues then it is most 10057a1df156SDave Chinner * likely a real ENOSPC condition. 100630f712c9SDave Chinner */ 1007066a1884SBrian Foster ineed = mp->m_ialloc_min_blks; 10087a1df156SDave Chinner if (flags && ineed > 1) 10097a1df156SDave Chinner ineed += xfs_ialloc_cluster_alignment(mp); 101030f712c9SDave Chinner longest = pag->pagf_longest; 101130f712c9SDave Chinner if (!longest) 101230f712c9SDave Chinner longest = pag->pagf_flcount > 0; 101330f712c9SDave Chinner 101430f712c9SDave Chinner if (pag->pagf_freeblks >= needspace + ineed && 101530f712c9SDave Chinner longest >= ineed) { 101630f712c9SDave Chinner xfs_perag_put(pag); 101730f712c9SDave Chinner return agno; 101830f712c9SDave Chinner } 101930f712c9SDave Chinner nextag: 102030f712c9SDave Chinner xfs_perag_put(pag); 102130f712c9SDave Chinner /* 102230f712c9SDave Chinner * No point in iterating over the rest, if we're shutting 102330f712c9SDave Chinner * down. 102430f712c9SDave Chinner */ 102530f712c9SDave Chinner if (XFS_FORCED_SHUTDOWN(mp)) 102630f712c9SDave Chinner return NULLAGNUMBER; 102730f712c9SDave Chinner agno++; 102830f712c9SDave Chinner if (agno >= agcount) 102930f712c9SDave Chinner agno = 0; 103030f712c9SDave Chinner if (agno == pagno) { 103130f712c9SDave Chinner if (flags == 0) 103230f712c9SDave Chinner return NULLAGNUMBER; 103330f712c9SDave Chinner flags = 0; 103430f712c9SDave Chinner } 103530f712c9SDave Chinner } 103630f712c9SDave Chinner } 103730f712c9SDave Chinner 103830f712c9SDave Chinner /* 103930f712c9SDave Chinner * Try to retrieve the next record to the left/right from the current one. 104030f712c9SDave Chinner */ 104130f712c9SDave Chinner STATIC int 104230f712c9SDave Chinner xfs_ialloc_next_rec( 104330f712c9SDave Chinner struct xfs_btree_cur *cur, 104430f712c9SDave Chinner xfs_inobt_rec_incore_t *rec, 104530f712c9SDave Chinner int *done, 104630f712c9SDave Chinner int left) 104730f712c9SDave Chinner { 104830f712c9SDave Chinner int error; 104930f712c9SDave Chinner int i; 105030f712c9SDave Chinner 105130f712c9SDave Chinner if (left) 105230f712c9SDave Chinner error = xfs_btree_decrement(cur, 0, &i); 105330f712c9SDave Chinner else 105430f712c9SDave Chinner error = xfs_btree_increment(cur, 0, &i); 105530f712c9SDave Chinner 105630f712c9SDave Chinner if (error) 105730f712c9SDave Chinner return error; 105830f712c9SDave Chinner *done = !i; 105930f712c9SDave Chinner if (i) { 106030f712c9SDave Chinner error = xfs_inobt_get_rec(cur, rec, &i); 106130f712c9SDave Chinner if (error) 106230f712c9SDave Chinner return error; 10635fb5aeeeSEric Sandeen XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1); 106430f712c9SDave Chinner } 106530f712c9SDave Chinner 106630f712c9SDave Chinner return 0; 106730f712c9SDave Chinner } 106830f712c9SDave Chinner 106930f712c9SDave Chinner STATIC int 107030f712c9SDave Chinner xfs_ialloc_get_rec( 107130f712c9SDave Chinner struct xfs_btree_cur *cur, 107230f712c9SDave Chinner xfs_agino_t agino, 107330f712c9SDave Chinner xfs_inobt_rec_incore_t *rec, 107430f712c9SDave Chinner int *done) 107530f712c9SDave Chinner { 107630f712c9SDave Chinner int error; 107730f712c9SDave Chinner int i; 107830f712c9SDave Chinner 107930f712c9SDave Chinner error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_EQ, &i); 108030f712c9SDave Chinner if (error) 108130f712c9SDave Chinner return error; 108230f712c9SDave Chinner *done = !i; 108330f712c9SDave Chinner if (i) { 108430f712c9SDave Chinner error = xfs_inobt_get_rec(cur, rec, &i); 108530f712c9SDave Chinner if (error) 108630f712c9SDave Chinner return error; 10875fb5aeeeSEric Sandeen XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1); 108830f712c9SDave Chinner } 108930f712c9SDave Chinner 109030f712c9SDave Chinner return 0; 109130f712c9SDave Chinner } 109230f712c9SDave Chinner 109330f712c9SDave Chinner /* 109426dd5217SBrian Foster * Return the offset of the first free inode in the record. If the inode chunk 109526dd5217SBrian Foster * is sparsely allocated, we convert the record holemask to inode granularity 109626dd5217SBrian Foster * and mask off the unallocated regions from the inode free mask. 1097d4cc540bSBrian Foster */ 1098d4cc540bSBrian Foster STATIC int 1099d4cc540bSBrian Foster xfs_inobt_first_free_inode( 1100d4cc540bSBrian Foster struct xfs_inobt_rec_incore *rec) 1101d4cc540bSBrian Foster { 110226dd5217SBrian Foster xfs_inofree_t realfree; 110326dd5217SBrian Foster 110426dd5217SBrian Foster /* if there are no holes, return the first available offset */ 110526dd5217SBrian Foster if (!xfs_inobt_issparse(rec->ir_holemask)) 1106d4cc540bSBrian Foster return xfs_lowbit64(rec->ir_free); 110726dd5217SBrian Foster 110826dd5217SBrian Foster realfree = xfs_inobt_irec_to_allocmask(rec); 110926dd5217SBrian Foster realfree &= rec->ir_free; 111026dd5217SBrian Foster 111126dd5217SBrian Foster return xfs_lowbit64(realfree); 1112d4cc540bSBrian Foster } 1113d4cc540bSBrian Foster 1114d4cc540bSBrian Foster /* 111530f712c9SDave Chinner * Allocate an inode using the inobt-only algorithm. 111630f712c9SDave Chinner */ 111730f712c9SDave Chinner STATIC int 111830f712c9SDave Chinner xfs_dialloc_ag_inobt( 111930f712c9SDave Chinner struct xfs_trans *tp, 112030f712c9SDave Chinner struct xfs_buf *agbp, 112130f712c9SDave Chinner xfs_ino_t parent, 112230f712c9SDave Chinner xfs_ino_t *inop) 112330f712c9SDave Chinner { 112430f712c9SDave Chinner struct xfs_mount *mp = tp->t_mountp; 112530f712c9SDave Chinner struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp); 112630f712c9SDave Chinner xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno); 112730f712c9SDave Chinner xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent); 112830f712c9SDave Chinner xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent); 112930f712c9SDave Chinner struct xfs_perag *pag; 113030f712c9SDave Chinner struct xfs_btree_cur *cur, *tcur; 113130f712c9SDave Chinner struct xfs_inobt_rec_incore rec, trec; 113230f712c9SDave Chinner xfs_ino_t ino; 113330f712c9SDave Chinner int error; 113430f712c9SDave Chinner int offset; 113530f712c9SDave Chinner int i, j; 113630f712c9SDave Chinner 113730f712c9SDave Chinner pag = xfs_perag_get(mp, agno); 113830f712c9SDave Chinner 113930f712c9SDave Chinner ASSERT(pag->pagi_init); 114030f712c9SDave Chinner ASSERT(pag->pagi_inodeok); 114130f712c9SDave Chinner ASSERT(pag->pagi_freecount > 0); 114230f712c9SDave Chinner 114330f712c9SDave Chinner restart_pagno: 114430f712c9SDave Chinner cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO); 114530f712c9SDave Chinner /* 114630f712c9SDave Chinner * If pagino is 0 (this is the root inode allocation) use newino. 114730f712c9SDave Chinner * This must work because we've just allocated some. 114830f712c9SDave Chinner */ 114930f712c9SDave Chinner if (!pagino) 115030f712c9SDave Chinner pagino = be32_to_cpu(agi->agi_newino); 115130f712c9SDave Chinner 115230f712c9SDave Chinner error = xfs_check_agi_freecount(cur, agi); 115330f712c9SDave Chinner if (error) 115430f712c9SDave Chinner goto error0; 115530f712c9SDave Chinner 115630f712c9SDave Chinner /* 115730f712c9SDave Chinner * If in the same AG as the parent, try to get near the parent. 115830f712c9SDave Chinner */ 115930f712c9SDave Chinner if (pagno == agno) { 116030f712c9SDave Chinner int doneleft; /* done, to the left */ 116130f712c9SDave Chinner int doneright; /* done, to the right */ 116230f712c9SDave Chinner int searchdistance = 10; 116330f712c9SDave Chinner 116430f712c9SDave Chinner error = xfs_inobt_lookup(cur, pagino, XFS_LOOKUP_LE, &i); 116530f712c9SDave Chinner if (error) 116630f712c9SDave Chinner goto error0; 1167c29aad41SEric Sandeen XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0); 116830f712c9SDave Chinner 116930f712c9SDave Chinner error = xfs_inobt_get_rec(cur, &rec, &j); 117030f712c9SDave Chinner if (error) 117130f712c9SDave Chinner goto error0; 1172c29aad41SEric Sandeen XFS_WANT_CORRUPTED_GOTO(mp, j == 1, error0); 117330f712c9SDave Chinner 117430f712c9SDave Chinner if (rec.ir_freecount > 0) { 117530f712c9SDave Chinner /* 117630f712c9SDave Chinner * Found a free inode in the same chunk 117730f712c9SDave Chinner * as the parent, done. 117830f712c9SDave Chinner */ 117930f712c9SDave Chinner goto alloc_inode; 118030f712c9SDave Chinner } 118130f712c9SDave Chinner 118230f712c9SDave Chinner 118330f712c9SDave Chinner /* 118430f712c9SDave Chinner * In the same AG as parent, but parent's chunk is full. 118530f712c9SDave Chinner */ 118630f712c9SDave Chinner 118730f712c9SDave Chinner /* duplicate the cursor, search left & right simultaneously */ 118830f712c9SDave Chinner error = xfs_btree_dup_cursor(cur, &tcur); 118930f712c9SDave Chinner if (error) 119030f712c9SDave Chinner goto error0; 119130f712c9SDave Chinner 119230f712c9SDave Chinner /* 119330f712c9SDave Chinner * Skip to last blocks looked up if same parent inode. 119430f712c9SDave Chinner */ 119530f712c9SDave Chinner if (pagino != NULLAGINO && 119630f712c9SDave Chinner pag->pagl_pagino == pagino && 119730f712c9SDave Chinner pag->pagl_leftrec != NULLAGINO && 119830f712c9SDave Chinner pag->pagl_rightrec != NULLAGINO) { 119930f712c9SDave Chinner error = xfs_ialloc_get_rec(tcur, pag->pagl_leftrec, 120030f712c9SDave Chinner &trec, &doneleft); 120130f712c9SDave Chinner if (error) 120230f712c9SDave Chinner goto error1; 120330f712c9SDave Chinner 120430f712c9SDave Chinner error = xfs_ialloc_get_rec(cur, pag->pagl_rightrec, 120530f712c9SDave Chinner &rec, &doneright); 120630f712c9SDave Chinner if (error) 120730f712c9SDave Chinner goto error1; 120830f712c9SDave Chinner } else { 120930f712c9SDave Chinner /* search left with tcur, back up 1 record */ 121030f712c9SDave Chinner error = xfs_ialloc_next_rec(tcur, &trec, &doneleft, 1); 121130f712c9SDave Chinner if (error) 121230f712c9SDave Chinner goto error1; 121330f712c9SDave Chinner 121430f712c9SDave Chinner /* search right with cur, go forward 1 record. */ 121530f712c9SDave Chinner error = xfs_ialloc_next_rec(cur, &rec, &doneright, 0); 121630f712c9SDave Chinner if (error) 121730f712c9SDave Chinner goto error1; 121830f712c9SDave Chinner } 121930f712c9SDave Chinner 122030f712c9SDave Chinner /* 122130f712c9SDave Chinner * Loop until we find an inode chunk with a free inode. 122230f712c9SDave Chinner */ 122330f712c9SDave Chinner while (!doneleft || !doneright) { 122430f712c9SDave Chinner int useleft; /* using left inode chunk this time */ 122530f712c9SDave Chinner 122630f712c9SDave Chinner if (!--searchdistance) { 122730f712c9SDave Chinner /* 122830f712c9SDave Chinner * Not in range - save last search 122930f712c9SDave Chinner * location and allocate a new inode 123030f712c9SDave Chinner */ 123130f712c9SDave Chinner xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); 123230f712c9SDave Chinner pag->pagl_leftrec = trec.ir_startino; 123330f712c9SDave Chinner pag->pagl_rightrec = rec.ir_startino; 123430f712c9SDave Chinner pag->pagl_pagino = pagino; 123530f712c9SDave Chinner goto newino; 123630f712c9SDave Chinner } 123730f712c9SDave Chinner 123830f712c9SDave Chinner /* figure out the closer block if both are valid. */ 123930f712c9SDave Chinner if (!doneleft && !doneright) { 124030f712c9SDave Chinner useleft = pagino - 124130f712c9SDave Chinner (trec.ir_startino + XFS_INODES_PER_CHUNK - 1) < 124230f712c9SDave Chinner rec.ir_startino - pagino; 124330f712c9SDave Chinner } else { 124430f712c9SDave Chinner useleft = !doneleft; 124530f712c9SDave Chinner } 124630f712c9SDave Chinner 124730f712c9SDave Chinner /* free inodes to the left? */ 124830f712c9SDave Chinner if (useleft && trec.ir_freecount) { 124930f712c9SDave Chinner rec = trec; 125030f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); 125130f712c9SDave Chinner cur = tcur; 125230f712c9SDave Chinner 125330f712c9SDave Chinner pag->pagl_leftrec = trec.ir_startino; 125430f712c9SDave Chinner pag->pagl_rightrec = rec.ir_startino; 125530f712c9SDave Chinner pag->pagl_pagino = pagino; 125630f712c9SDave Chinner goto alloc_inode; 125730f712c9SDave Chinner } 125830f712c9SDave Chinner 125930f712c9SDave Chinner /* free inodes to the right? */ 126030f712c9SDave Chinner if (!useleft && rec.ir_freecount) { 126130f712c9SDave Chinner xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); 126230f712c9SDave Chinner 126330f712c9SDave Chinner pag->pagl_leftrec = trec.ir_startino; 126430f712c9SDave Chinner pag->pagl_rightrec = rec.ir_startino; 126530f712c9SDave Chinner pag->pagl_pagino = pagino; 126630f712c9SDave Chinner goto alloc_inode; 126730f712c9SDave Chinner } 126830f712c9SDave Chinner 126930f712c9SDave Chinner /* get next record to check */ 127030f712c9SDave Chinner if (useleft) { 127130f712c9SDave Chinner error = xfs_ialloc_next_rec(tcur, &trec, 127230f712c9SDave Chinner &doneleft, 1); 127330f712c9SDave Chinner } else { 127430f712c9SDave Chinner error = xfs_ialloc_next_rec(cur, &rec, 127530f712c9SDave Chinner &doneright, 0); 127630f712c9SDave Chinner } 127730f712c9SDave Chinner if (error) 127830f712c9SDave Chinner goto error1; 127930f712c9SDave Chinner } 128030f712c9SDave Chinner 128130f712c9SDave Chinner /* 128230f712c9SDave Chinner * We've reached the end of the btree. because 128330f712c9SDave Chinner * we are only searching a small chunk of the 128430f712c9SDave Chinner * btree each search, there is obviously free 128530f712c9SDave Chinner * inodes closer to the parent inode than we 128630f712c9SDave Chinner * are now. restart the search again. 128730f712c9SDave Chinner */ 128830f712c9SDave Chinner pag->pagl_pagino = NULLAGINO; 128930f712c9SDave Chinner pag->pagl_leftrec = NULLAGINO; 129030f712c9SDave Chinner pag->pagl_rightrec = NULLAGINO; 129130f712c9SDave Chinner xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); 129230f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); 129330f712c9SDave Chinner goto restart_pagno; 129430f712c9SDave Chinner } 129530f712c9SDave Chinner 129630f712c9SDave Chinner /* 129730f712c9SDave Chinner * In a different AG from the parent. 129830f712c9SDave Chinner * See if the most recently allocated block has any free. 129930f712c9SDave Chinner */ 130030f712c9SDave Chinner newino: 130130f712c9SDave Chinner if (agi->agi_newino != cpu_to_be32(NULLAGINO)) { 130230f712c9SDave Chinner error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino), 130330f712c9SDave Chinner XFS_LOOKUP_EQ, &i); 130430f712c9SDave Chinner if (error) 130530f712c9SDave Chinner goto error0; 130630f712c9SDave Chinner 130730f712c9SDave Chinner if (i == 1) { 130830f712c9SDave Chinner error = xfs_inobt_get_rec(cur, &rec, &j); 130930f712c9SDave Chinner if (error) 131030f712c9SDave Chinner goto error0; 131130f712c9SDave Chinner 131230f712c9SDave Chinner if (j == 1 && rec.ir_freecount > 0) { 131330f712c9SDave Chinner /* 131430f712c9SDave Chinner * The last chunk allocated in the group 131530f712c9SDave Chinner * still has a free inode. 131630f712c9SDave Chinner */ 131730f712c9SDave Chinner goto alloc_inode; 131830f712c9SDave Chinner } 131930f712c9SDave Chinner } 132030f712c9SDave Chinner } 132130f712c9SDave Chinner 132230f712c9SDave Chinner /* 132330f712c9SDave Chinner * None left in the last group, search the whole AG 132430f712c9SDave Chinner */ 132530f712c9SDave Chinner error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i); 132630f712c9SDave Chinner if (error) 132730f712c9SDave Chinner goto error0; 1328c29aad41SEric Sandeen XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0); 132930f712c9SDave Chinner 133030f712c9SDave Chinner for (;;) { 133130f712c9SDave Chinner error = xfs_inobt_get_rec(cur, &rec, &i); 133230f712c9SDave Chinner if (error) 133330f712c9SDave Chinner goto error0; 1334c29aad41SEric Sandeen XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0); 133530f712c9SDave Chinner if (rec.ir_freecount > 0) 133630f712c9SDave Chinner break; 133730f712c9SDave Chinner error = xfs_btree_increment(cur, 0, &i); 133830f712c9SDave Chinner if (error) 133930f712c9SDave Chinner goto error0; 1340c29aad41SEric Sandeen XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0); 134130f712c9SDave Chinner } 134230f712c9SDave Chinner 134330f712c9SDave Chinner alloc_inode: 1344d4cc540bSBrian Foster offset = xfs_inobt_first_free_inode(&rec); 134530f712c9SDave Chinner ASSERT(offset >= 0); 134630f712c9SDave Chinner ASSERT(offset < XFS_INODES_PER_CHUNK); 134730f712c9SDave Chinner ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) % 134830f712c9SDave Chinner XFS_INODES_PER_CHUNK) == 0); 134930f712c9SDave Chinner ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset); 135030f712c9SDave Chinner rec.ir_free &= ~XFS_INOBT_MASK(offset); 135130f712c9SDave Chinner rec.ir_freecount--; 135230f712c9SDave Chinner error = xfs_inobt_update(cur, &rec); 135330f712c9SDave Chinner if (error) 135430f712c9SDave Chinner goto error0; 135530f712c9SDave Chinner be32_add_cpu(&agi->agi_freecount, -1); 135630f712c9SDave Chinner xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT); 135730f712c9SDave Chinner pag->pagi_freecount--; 135830f712c9SDave Chinner 135930f712c9SDave Chinner error = xfs_check_agi_freecount(cur, agi); 136030f712c9SDave Chinner if (error) 136130f712c9SDave Chinner goto error0; 136230f712c9SDave Chinner 136330f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); 136430f712c9SDave Chinner xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1); 136530f712c9SDave Chinner xfs_perag_put(pag); 136630f712c9SDave Chinner *inop = ino; 136730f712c9SDave Chinner return 0; 136830f712c9SDave Chinner error1: 136930f712c9SDave Chinner xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR); 137030f712c9SDave Chinner error0: 137130f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); 137230f712c9SDave Chinner xfs_perag_put(pag); 137330f712c9SDave Chinner return error; 137430f712c9SDave Chinner } 137530f712c9SDave Chinner 137630f712c9SDave Chinner /* 137730f712c9SDave Chinner * Use the free inode btree to allocate an inode based on distance from the 137830f712c9SDave Chinner * parent. Note that the provided cursor may be deleted and replaced. 137930f712c9SDave Chinner */ 138030f712c9SDave Chinner STATIC int 138130f712c9SDave Chinner xfs_dialloc_ag_finobt_near( 138230f712c9SDave Chinner xfs_agino_t pagino, 138330f712c9SDave Chinner struct xfs_btree_cur **ocur, 138430f712c9SDave Chinner struct xfs_inobt_rec_incore *rec) 138530f712c9SDave Chinner { 138630f712c9SDave Chinner struct xfs_btree_cur *lcur = *ocur; /* left search cursor */ 138730f712c9SDave Chinner struct xfs_btree_cur *rcur; /* right search cursor */ 138830f712c9SDave Chinner struct xfs_inobt_rec_incore rrec; 138930f712c9SDave Chinner int error; 139030f712c9SDave Chinner int i, j; 139130f712c9SDave Chinner 139230f712c9SDave Chinner error = xfs_inobt_lookup(lcur, pagino, XFS_LOOKUP_LE, &i); 139330f712c9SDave Chinner if (error) 139430f712c9SDave Chinner return error; 139530f712c9SDave Chinner 139630f712c9SDave Chinner if (i == 1) { 139730f712c9SDave Chinner error = xfs_inobt_get_rec(lcur, rec, &i); 139830f712c9SDave Chinner if (error) 139930f712c9SDave Chinner return error; 14005fb5aeeeSEric Sandeen XFS_WANT_CORRUPTED_RETURN(lcur->bc_mp, i == 1); 140130f712c9SDave Chinner 140230f712c9SDave Chinner /* 140330f712c9SDave Chinner * See if we've landed in the parent inode record. The finobt 140430f712c9SDave Chinner * only tracks chunks with at least one free inode, so record 140530f712c9SDave Chinner * existence is enough. 140630f712c9SDave Chinner */ 140730f712c9SDave Chinner if (pagino >= rec->ir_startino && 140830f712c9SDave Chinner pagino < (rec->ir_startino + XFS_INODES_PER_CHUNK)) 140930f712c9SDave Chinner return 0; 141030f712c9SDave Chinner } 141130f712c9SDave Chinner 141230f712c9SDave Chinner error = xfs_btree_dup_cursor(lcur, &rcur); 141330f712c9SDave Chinner if (error) 141430f712c9SDave Chinner return error; 141530f712c9SDave Chinner 141630f712c9SDave Chinner error = xfs_inobt_lookup(rcur, pagino, XFS_LOOKUP_GE, &j); 141730f712c9SDave Chinner if (error) 141830f712c9SDave Chinner goto error_rcur; 141930f712c9SDave Chinner if (j == 1) { 142030f712c9SDave Chinner error = xfs_inobt_get_rec(rcur, &rrec, &j); 142130f712c9SDave Chinner if (error) 142230f712c9SDave Chinner goto error_rcur; 1423c29aad41SEric Sandeen XFS_WANT_CORRUPTED_GOTO(lcur->bc_mp, j == 1, error_rcur); 142430f712c9SDave Chinner } 142530f712c9SDave Chinner 1426c29aad41SEric Sandeen XFS_WANT_CORRUPTED_GOTO(lcur->bc_mp, i == 1 || j == 1, error_rcur); 142730f712c9SDave Chinner if (i == 1 && j == 1) { 142830f712c9SDave Chinner /* 142930f712c9SDave Chinner * Both the left and right records are valid. Choose the closer 143030f712c9SDave Chinner * inode chunk to the target. 143130f712c9SDave Chinner */ 143230f712c9SDave Chinner if ((pagino - rec->ir_startino + XFS_INODES_PER_CHUNK - 1) > 143330f712c9SDave Chinner (rrec.ir_startino - pagino)) { 143430f712c9SDave Chinner *rec = rrec; 143530f712c9SDave Chinner xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR); 143630f712c9SDave Chinner *ocur = rcur; 143730f712c9SDave Chinner } else { 143830f712c9SDave Chinner xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR); 143930f712c9SDave Chinner } 144030f712c9SDave Chinner } else if (j == 1) { 144130f712c9SDave Chinner /* only the right record is valid */ 144230f712c9SDave Chinner *rec = rrec; 144330f712c9SDave Chinner xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR); 144430f712c9SDave Chinner *ocur = rcur; 144530f712c9SDave Chinner } else if (i == 1) { 144630f712c9SDave Chinner /* only the left record is valid */ 144730f712c9SDave Chinner xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR); 144830f712c9SDave Chinner } 144930f712c9SDave Chinner 145030f712c9SDave Chinner return 0; 145130f712c9SDave Chinner 145230f712c9SDave Chinner error_rcur: 145330f712c9SDave Chinner xfs_btree_del_cursor(rcur, XFS_BTREE_ERROR); 145430f712c9SDave Chinner return error; 145530f712c9SDave Chinner } 145630f712c9SDave Chinner 145730f712c9SDave Chinner /* 145830f712c9SDave Chinner * Use the free inode btree to find a free inode based on a newino hint. If 145930f712c9SDave Chinner * the hint is NULL, find the first free inode in the AG. 146030f712c9SDave Chinner */ 146130f712c9SDave Chinner STATIC int 146230f712c9SDave Chinner xfs_dialloc_ag_finobt_newino( 146330f712c9SDave Chinner struct xfs_agi *agi, 146430f712c9SDave Chinner struct xfs_btree_cur *cur, 146530f712c9SDave Chinner struct xfs_inobt_rec_incore *rec) 146630f712c9SDave Chinner { 146730f712c9SDave Chinner int error; 146830f712c9SDave Chinner int i; 146930f712c9SDave Chinner 147030f712c9SDave Chinner if (agi->agi_newino != cpu_to_be32(NULLAGINO)) { 1471e68ed775SDave Chinner error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino), 1472e68ed775SDave Chinner XFS_LOOKUP_EQ, &i); 147330f712c9SDave Chinner if (error) 147430f712c9SDave Chinner return error; 147530f712c9SDave Chinner if (i == 1) { 147630f712c9SDave Chinner error = xfs_inobt_get_rec(cur, rec, &i); 147730f712c9SDave Chinner if (error) 147830f712c9SDave Chinner return error; 14795fb5aeeeSEric Sandeen XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1); 148030f712c9SDave Chinner return 0; 148130f712c9SDave Chinner } 148230f712c9SDave Chinner } 148330f712c9SDave Chinner 148430f712c9SDave Chinner /* 148530f712c9SDave Chinner * Find the first inode available in the AG. 148630f712c9SDave Chinner */ 148730f712c9SDave Chinner error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i); 148830f712c9SDave Chinner if (error) 148930f712c9SDave Chinner return error; 14905fb5aeeeSEric Sandeen XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1); 149130f712c9SDave Chinner 149230f712c9SDave Chinner error = xfs_inobt_get_rec(cur, rec, &i); 149330f712c9SDave Chinner if (error) 149430f712c9SDave Chinner return error; 14955fb5aeeeSEric Sandeen XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1); 149630f712c9SDave Chinner 149730f712c9SDave Chinner return 0; 149830f712c9SDave Chinner } 149930f712c9SDave Chinner 150030f712c9SDave Chinner /* 150130f712c9SDave Chinner * Update the inobt based on a modification made to the finobt. Also ensure that 150230f712c9SDave Chinner * the records from both trees are equivalent post-modification. 150330f712c9SDave Chinner */ 150430f712c9SDave Chinner STATIC int 150530f712c9SDave Chinner xfs_dialloc_ag_update_inobt( 150630f712c9SDave Chinner struct xfs_btree_cur *cur, /* inobt cursor */ 150730f712c9SDave Chinner struct xfs_inobt_rec_incore *frec, /* finobt record */ 150830f712c9SDave Chinner int offset) /* inode offset */ 150930f712c9SDave Chinner { 151030f712c9SDave Chinner struct xfs_inobt_rec_incore rec; 151130f712c9SDave Chinner int error; 151230f712c9SDave Chinner int i; 151330f712c9SDave Chinner 151430f712c9SDave Chinner error = xfs_inobt_lookup(cur, frec->ir_startino, XFS_LOOKUP_EQ, &i); 151530f712c9SDave Chinner if (error) 151630f712c9SDave Chinner return error; 15175fb5aeeeSEric Sandeen XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1); 151830f712c9SDave Chinner 151930f712c9SDave Chinner error = xfs_inobt_get_rec(cur, &rec, &i); 152030f712c9SDave Chinner if (error) 152130f712c9SDave Chinner return error; 15225fb5aeeeSEric Sandeen XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1); 152330f712c9SDave Chinner ASSERT((XFS_AGINO_TO_OFFSET(cur->bc_mp, rec.ir_startino) % 152430f712c9SDave Chinner XFS_INODES_PER_CHUNK) == 0); 152530f712c9SDave Chinner 152630f712c9SDave Chinner rec.ir_free &= ~XFS_INOBT_MASK(offset); 152730f712c9SDave Chinner rec.ir_freecount--; 152830f712c9SDave Chinner 15295fb5aeeeSEric Sandeen XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, (rec.ir_free == frec->ir_free) && 153030f712c9SDave Chinner (rec.ir_freecount == frec->ir_freecount)); 153130f712c9SDave Chinner 1532b72091f2Skbuild test robot return xfs_inobt_update(cur, &rec); 153330f712c9SDave Chinner } 153430f712c9SDave Chinner 153530f712c9SDave Chinner /* 153630f712c9SDave Chinner * Allocate an inode using the free inode btree, if available. Otherwise, fall 153730f712c9SDave Chinner * back to the inobt search algorithm. 153830f712c9SDave Chinner * 153930f712c9SDave Chinner * The caller selected an AG for us, and made sure that free inodes are 154030f712c9SDave Chinner * available. 154130f712c9SDave Chinner */ 154230f712c9SDave Chinner STATIC int 154330f712c9SDave Chinner xfs_dialloc_ag( 154430f712c9SDave Chinner struct xfs_trans *tp, 154530f712c9SDave Chinner struct xfs_buf *agbp, 154630f712c9SDave Chinner xfs_ino_t parent, 154730f712c9SDave Chinner xfs_ino_t *inop) 154830f712c9SDave Chinner { 154930f712c9SDave Chinner struct xfs_mount *mp = tp->t_mountp; 155030f712c9SDave Chinner struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp); 155130f712c9SDave Chinner xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno); 155230f712c9SDave Chinner xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent); 155330f712c9SDave Chinner xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent); 155430f712c9SDave Chinner struct xfs_perag *pag; 155530f712c9SDave Chinner struct xfs_btree_cur *cur; /* finobt cursor */ 155630f712c9SDave Chinner struct xfs_btree_cur *icur; /* inobt cursor */ 155730f712c9SDave Chinner struct xfs_inobt_rec_incore rec; 155830f712c9SDave Chinner xfs_ino_t ino; 155930f712c9SDave Chinner int error; 156030f712c9SDave Chinner int offset; 156130f712c9SDave Chinner int i; 156230f712c9SDave Chinner 156330f712c9SDave Chinner if (!xfs_sb_version_hasfinobt(&mp->m_sb)) 156430f712c9SDave Chinner return xfs_dialloc_ag_inobt(tp, agbp, parent, inop); 156530f712c9SDave Chinner 156630f712c9SDave Chinner pag = xfs_perag_get(mp, agno); 156730f712c9SDave Chinner 156830f712c9SDave Chinner /* 156930f712c9SDave Chinner * If pagino is 0 (this is the root inode allocation) use newino. 157030f712c9SDave Chinner * This must work because we've just allocated some. 157130f712c9SDave Chinner */ 157230f712c9SDave Chinner if (!pagino) 157330f712c9SDave Chinner pagino = be32_to_cpu(agi->agi_newino); 157430f712c9SDave Chinner 157530f712c9SDave Chinner cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_FINO); 157630f712c9SDave Chinner 157730f712c9SDave Chinner error = xfs_check_agi_freecount(cur, agi); 157830f712c9SDave Chinner if (error) 157930f712c9SDave Chinner goto error_cur; 158030f712c9SDave Chinner 158130f712c9SDave Chinner /* 158230f712c9SDave Chinner * The search algorithm depends on whether we're in the same AG as the 158330f712c9SDave Chinner * parent. If so, find the closest available inode to the parent. If 158430f712c9SDave Chinner * not, consider the agi hint or find the first free inode in the AG. 158530f712c9SDave Chinner */ 158630f712c9SDave Chinner if (agno == pagno) 158730f712c9SDave Chinner error = xfs_dialloc_ag_finobt_near(pagino, &cur, &rec); 158830f712c9SDave Chinner else 158930f712c9SDave Chinner error = xfs_dialloc_ag_finobt_newino(agi, cur, &rec); 159030f712c9SDave Chinner if (error) 159130f712c9SDave Chinner goto error_cur; 159230f712c9SDave Chinner 1593d4cc540bSBrian Foster offset = xfs_inobt_first_free_inode(&rec); 159430f712c9SDave Chinner ASSERT(offset >= 0); 159530f712c9SDave Chinner ASSERT(offset < XFS_INODES_PER_CHUNK); 159630f712c9SDave Chinner ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) % 159730f712c9SDave Chinner XFS_INODES_PER_CHUNK) == 0); 159830f712c9SDave Chinner ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset); 159930f712c9SDave Chinner 160030f712c9SDave Chinner /* 160130f712c9SDave Chinner * Modify or remove the finobt record. 160230f712c9SDave Chinner */ 160330f712c9SDave Chinner rec.ir_free &= ~XFS_INOBT_MASK(offset); 160430f712c9SDave Chinner rec.ir_freecount--; 160530f712c9SDave Chinner if (rec.ir_freecount) 160630f712c9SDave Chinner error = xfs_inobt_update(cur, &rec); 160730f712c9SDave Chinner else 160830f712c9SDave Chinner error = xfs_btree_delete(cur, &i); 160930f712c9SDave Chinner if (error) 161030f712c9SDave Chinner goto error_cur; 161130f712c9SDave Chinner 161230f712c9SDave Chinner /* 161330f712c9SDave Chinner * The finobt has now been updated appropriately. We haven't updated the 161430f712c9SDave Chinner * agi and superblock yet, so we can create an inobt cursor and validate 161530f712c9SDave Chinner * the original freecount. If all is well, make the equivalent update to 161630f712c9SDave Chinner * the inobt using the finobt record and offset information. 161730f712c9SDave Chinner */ 161830f712c9SDave Chinner icur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO); 161930f712c9SDave Chinner 162030f712c9SDave Chinner error = xfs_check_agi_freecount(icur, agi); 162130f712c9SDave Chinner if (error) 162230f712c9SDave Chinner goto error_icur; 162330f712c9SDave Chinner 162430f712c9SDave Chinner error = xfs_dialloc_ag_update_inobt(icur, &rec, offset); 162530f712c9SDave Chinner if (error) 162630f712c9SDave Chinner goto error_icur; 162730f712c9SDave Chinner 162830f712c9SDave Chinner /* 162930f712c9SDave Chinner * Both trees have now been updated. We must update the perag and 163030f712c9SDave Chinner * superblock before we can check the freecount for each btree. 163130f712c9SDave Chinner */ 163230f712c9SDave Chinner be32_add_cpu(&agi->agi_freecount, -1); 163330f712c9SDave Chinner xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT); 163430f712c9SDave Chinner pag->pagi_freecount--; 163530f712c9SDave Chinner 163630f712c9SDave Chinner xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1); 163730f712c9SDave Chinner 163830f712c9SDave Chinner error = xfs_check_agi_freecount(icur, agi); 163930f712c9SDave Chinner if (error) 164030f712c9SDave Chinner goto error_icur; 164130f712c9SDave Chinner error = xfs_check_agi_freecount(cur, agi); 164230f712c9SDave Chinner if (error) 164330f712c9SDave Chinner goto error_icur; 164430f712c9SDave Chinner 164530f712c9SDave Chinner xfs_btree_del_cursor(icur, XFS_BTREE_NOERROR); 164630f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); 164730f712c9SDave Chinner xfs_perag_put(pag); 164830f712c9SDave Chinner *inop = ino; 164930f712c9SDave Chinner return 0; 165030f712c9SDave Chinner 165130f712c9SDave Chinner error_icur: 165230f712c9SDave Chinner xfs_btree_del_cursor(icur, XFS_BTREE_ERROR); 165330f712c9SDave Chinner error_cur: 165430f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); 165530f712c9SDave Chinner xfs_perag_put(pag); 165630f712c9SDave Chinner return error; 165730f712c9SDave Chinner } 165830f712c9SDave Chinner 165930f712c9SDave Chinner /* 166030f712c9SDave Chinner * Allocate an inode on disk. 166130f712c9SDave Chinner * 166230f712c9SDave Chinner * Mode is used to tell whether the new inode will need space, and whether it 166330f712c9SDave Chinner * is a directory. 166430f712c9SDave Chinner * 166530f712c9SDave Chinner * This function is designed to be called twice if it has to do an allocation 166630f712c9SDave Chinner * to make more free inodes. On the first call, *IO_agbp should be set to NULL. 166730f712c9SDave Chinner * If an inode is available without having to performn an allocation, an inode 166830f712c9SDave Chinner * number is returned. In this case, *IO_agbp is set to NULL. If an allocation 166930f712c9SDave Chinner * needs to be done, xfs_dialloc returns the current AGI buffer in *IO_agbp. 167030f712c9SDave Chinner * The caller should then commit the current transaction, allocate a 167130f712c9SDave Chinner * new transaction, and call xfs_dialloc() again, passing in the previous value 167230f712c9SDave Chinner * of *IO_agbp. IO_agbp should be held across the transactions. Since the AGI 167330f712c9SDave Chinner * buffer is locked across the two calls, the second call is guaranteed to have 167430f712c9SDave Chinner * a free inode available. 167530f712c9SDave Chinner * 167630f712c9SDave Chinner * Once we successfully pick an inode its number is returned and the on-disk 167730f712c9SDave Chinner * data structures are updated. The inode itself is not read in, since doing so 167830f712c9SDave Chinner * would break ordering constraints with xfs_reclaim. 167930f712c9SDave Chinner */ 168030f712c9SDave Chinner int 168130f712c9SDave Chinner xfs_dialloc( 168230f712c9SDave Chinner struct xfs_trans *tp, 168330f712c9SDave Chinner xfs_ino_t parent, 168430f712c9SDave Chinner umode_t mode, 168530f712c9SDave Chinner int okalloc, 168630f712c9SDave Chinner struct xfs_buf **IO_agbp, 168730f712c9SDave Chinner xfs_ino_t *inop) 168830f712c9SDave Chinner { 168930f712c9SDave Chinner struct xfs_mount *mp = tp->t_mountp; 169030f712c9SDave Chinner struct xfs_buf *agbp; 169130f712c9SDave Chinner xfs_agnumber_t agno; 169230f712c9SDave Chinner int error; 169330f712c9SDave Chinner int ialloced; 169430f712c9SDave Chinner int noroom = 0; 169530f712c9SDave Chinner xfs_agnumber_t start_agno; 169630f712c9SDave Chinner struct xfs_perag *pag; 169730f712c9SDave Chinner 169830f712c9SDave Chinner if (*IO_agbp) { 169930f712c9SDave Chinner /* 170030f712c9SDave Chinner * If the caller passes in a pointer to the AGI buffer, 170130f712c9SDave Chinner * continue where we left off before. In this case, we 170230f712c9SDave Chinner * know that the allocation group has free inodes. 170330f712c9SDave Chinner */ 170430f712c9SDave Chinner agbp = *IO_agbp; 170530f712c9SDave Chinner goto out_alloc; 170630f712c9SDave Chinner } 170730f712c9SDave Chinner 170830f712c9SDave Chinner /* 170930f712c9SDave Chinner * We do not have an agbp, so select an initial allocation 171030f712c9SDave Chinner * group for inode allocation. 171130f712c9SDave Chinner */ 171230f712c9SDave Chinner start_agno = xfs_ialloc_ag_select(tp, parent, mode, okalloc); 171330f712c9SDave Chinner if (start_agno == NULLAGNUMBER) { 171430f712c9SDave Chinner *inop = NULLFSINO; 171530f712c9SDave Chinner return 0; 171630f712c9SDave Chinner } 171730f712c9SDave Chinner 171830f712c9SDave Chinner /* 171930f712c9SDave Chinner * If we have already hit the ceiling of inode blocks then clear 172030f712c9SDave Chinner * okalloc so we scan all available agi structures for a free 172130f712c9SDave Chinner * inode. 172274f9ce1cSGeorge Wang * 172374f9ce1cSGeorge Wang * Read rough value of mp->m_icount by percpu_counter_read_positive, 172474f9ce1cSGeorge Wang * which will sacrifice the preciseness but improve the performance. 172530f712c9SDave Chinner */ 172630f712c9SDave Chinner if (mp->m_maxicount && 172774f9ce1cSGeorge Wang percpu_counter_read_positive(&mp->m_icount) + mp->m_ialloc_inos 172874f9ce1cSGeorge Wang > mp->m_maxicount) { 172930f712c9SDave Chinner noroom = 1; 173030f712c9SDave Chinner okalloc = 0; 173130f712c9SDave Chinner } 173230f712c9SDave Chinner 173330f712c9SDave Chinner /* 173430f712c9SDave Chinner * Loop until we find an allocation group that either has free inodes 173530f712c9SDave Chinner * or in which we can allocate some inodes. Iterate through the 173630f712c9SDave Chinner * allocation groups upward, wrapping at the end. 173730f712c9SDave Chinner */ 173830f712c9SDave Chinner agno = start_agno; 173930f712c9SDave Chinner for (;;) { 174030f712c9SDave Chinner pag = xfs_perag_get(mp, agno); 174130f712c9SDave Chinner if (!pag->pagi_inodeok) { 174230f712c9SDave Chinner xfs_ialloc_next_ag(mp); 174330f712c9SDave Chinner goto nextag; 174430f712c9SDave Chinner } 174530f712c9SDave Chinner 174630f712c9SDave Chinner if (!pag->pagi_init) { 174730f712c9SDave Chinner error = xfs_ialloc_pagi_init(mp, tp, agno); 174830f712c9SDave Chinner if (error) 174930f712c9SDave Chinner goto out_error; 175030f712c9SDave Chinner } 175130f712c9SDave Chinner 175230f712c9SDave Chinner /* 175330f712c9SDave Chinner * Do a first racy fast path check if this AG is usable. 175430f712c9SDave Chinner */ 175530f712c9SDave Chinner if (!pag->pagi_freecount && !okalloc) 175630f712c9SDave Chinner goto nextag; 175730f712c9SDave Chinner 175830f712c9SDave Chinner /* 175930f712c9SDave Chinner * Then read in the AGI buffer and recheck with the AGI buffer 176030f712c9SDave Chinner * lock held. 176130f712c9SDave Chinner */ 176230f712c9SDave Chinner error = xfs_ialloc_read_agi(mp, tp, agno, &agbp); 176330f712c9SDave Chinner if (error) 176430f712c9SDave Chinner goto out_error; 176530f712c9SDave Chinner 176630f712c9SDave Chinner if (pag->pagi_freecount) { 176730f712c9SDave Chinner xfs_perag_put(pag); 176830f712c9SDave Chinner goto out_alloc; 176930f712c9SDave Chinner } 177030f712c9SDave Chinner 177130f712c9SDave Chinner if (!okalloc) 177230f712c9SDave Chinner goto nextag_relse_buffer; 177330f712c9SDave Chinner 177430f712c9SDave Chinner 177530f712c9SDave Chinner error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced); 177630f712c9SDave Chinner if (error) { 177730f712c9SDave Chinner xfs_trans_brelse(tp, agbp); 177830f712c9SDave Chinner 17792451337dSDave Chinner if (error != -ENOSPC) 178030f712c9SDave Chinner goto out_error; 178130f712c9SDave Chinner 178230f712c9SDave Chinner xfs_perag_put(pag); 178330f712c9SDave Chinner *inop = NULLFSINO; 178430f712c9SDave Chinner return 0; 178530f712c9SDave Chinner } 178630f712c9SDave Chinner 178730f712c9SDave Chinner if (ialloced) { 178830f712c9SDave Chinner /* 178930f712c9SDave Chinner * We successfully allocated some inodes, return 179030f712c9SDave Chinner * the current context to the caller so that it 179130f712c9SDave Chinner * can commit the current transaction and call 179230f712c9SDave Chinner * us again where we left off. 179330f712c9SDave Chinner */ 179430f712c9SDave Chinner ASSERT(pag->pagi_freecount > 0); 179530f712c9SDave Chinner xfs_perag_put(pag); 179630f712c9SDave Chinner 179730f712c9SDave Chinner *IO_agbp = agbp; 179830f712c9SDave Chinner *inop = NULLFSINO; 179930f712c9SDave Chinner return 0; 180030f712c9SDave Chinner } 180130f712c9SDave Chinner 180230f712c9SDave Chinner nextag_relse_buffer: 180330f712c9SDave Chinner xfs_trans_brelse(tp, agbp); 180430f712c9SDave Chinner nextag: 180530f712c9SDave Chinner xfs_perag_put(pag); 180630f712c9SDave Chinner if (++agno == mp->m_sb.sb_agcount) 180730f712c9SDave Chinner agno = 0; 180830f712c9SDave Chinner if (agno == start_agno) { 180930f712c9SDave Chinner *inop = NULLFSINO; 18102451337dSDave Chinner return noroom ? -ENOSPC : 0; 181130f712c9SDave Chinner } 181230f712c9SDave Chinner } 181330f712c9SDave Chinner 181430f712c9SDave Chinner out_alloc: 181530f712c9SDave Chinner *IO_agbp = NULL; 181630f712c9SDave Chinner return xfs_dialloc_ag(tp, agbp, parent, inop); 181730f712c9SDave Chinner out_error: 181830f712c9SDave Chinner xfs_perag_put(pag); 181930f712c9SDave Chinner return error; 182030f712c9SDave Chinner } 182130f712c9SDave Chinner 182210ae3dc7SBrian Foster /* 182310ae3dc7SBrian Foster * Free the blocks of an inode chunk. We must consider that the inode chunk 182410ae3dc7SBrian Foster * might be sparse and only free the regions that are allocated as part of the 182510ae3dc7SBrian Foster * chunk. 182610ae3dc7SBrian Foster */ 182710ae3dc7SBrian Foster STATIC void 182810ae3dc7SBrian Foster xfs_difree_inode_chunk( 182910ae3dc7SBrian Foster struct xfs_mount *mp, 183010ae3dc7SBrian Foster xfs_agnumber_t agno, 183110ae3dc7SBrian Foster struct xfs_inobt_rec_incore *rec, 18322c3234d1SDarrick J. Wong struct xfs_defer_ops *dfops) 183310ae3dc7SBrian Foster { 183410ae3dc7SBrian Foster xfs_agblock_t sagbno = XFS_AGINO_TO_AGBNO(mp, rec->ir_startino); 183510ae3dc7SBrian Foster int startidx, endidx; 183610ae3dc7SBrian Foster int nextbit; 183710ae3dc7SBrian Foster xfs_agblock_t agbno; 183810ae3dc7SBrian Foster int contigblk; 1839340785ccSDarrick J. Wong struct xfs_owner_info oinfo; 184010ae3dc7SBrian Foster DECLARE_BITMAP(holemask, XFS_INOBT_HOLEMASK_BITS); 1841340785ccSDarrick J. Wong xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_INODES); 184210ae3dc7SBrian Foster 184310ae3dc7SBrian Foster if (!xfs_inobt_issparse(rec->ir_holemask)) { 184410ae3dc7SBrian Foster /* not sparse, calculate extent info directly */ 18452c3234d1SDarrick J. Wong xfs_bmap_add_free(mp, dfops, XFS_AGB_TO_FSB(mp, agno, sagbno), 1846340785ccSDarrick J. Wong mp->m_ialloc_blks, &oinfo); 184710ae3dc7SBrian Foster return; 184810ae3dc7SBrian Foster } 184910ae3dc7SBrian Foster 185010ae3dc7SBrian Foster /* holemask is only 16-bits (fits in an unsigned long) */ 185110ae3dc7SBrian Foster ASSERT(sizeof(rec->ir_holemask) <= sizeof(holemask[0])); 185210ae3dc7SBrian Foster holemask[0] = rec->ir_holemask; 185310ae3dc7SBrian Foster 185410ae3dc7SBrian Foster /* 185510ae3dc7SBrian Foster * Find contiguous ranges of zeroes (i.e., allocated regions) in the 185610ae3dc7SBrian Foster * holemask and convert the start/end index of each range to an extent. 185710ae3dc7SBrian Foster * We start with the start and end index both pointing at the first 0 in 185810ae3dc7SBrian Foster * the mask. 185910ae3dc7SBrian Foster */ 186010ae3dc7SBrian Foster startidx = endidx = find_first_zero_bit(holemask, 186110ae3dc7SBrian Foster XFS_INOBT_HOLEMASK_BITS); 186210ae3dc7SBrian Foster nextbit = startidx + 1; 186310ae3dc7SBrian Foster while (startidx < XFS_INOBT_HOLEMASK_BITS) { 186410ae3dc7SBrian Foster nextbit = find_next_zero_bit(holemask, XFS_INOBT_HOLEMASK_BITS, 186510ae3dc7SBrian Foster nextbit); 186610ae3dc7SBrian Foster /* 186710ae3dc7SBrian Foster * If the next zero bit is contiguous, update the end index of 186810ae3dc7SBrian Foster * the current range and continue. 186910ae3dc7SBrian Foster */ 187010ae3dc7SBrian Foster if (nextbit != XFS_INOBT_HOLEMASK_BITS && 187110ae3dc7SBrian Foster nextbit == endidx + 1) { 187210ae3dc7SBrian Foster endidx = nextbit; 187310ae3dc7SBrian Foster goto next; 187410ae3dc7SBrian Foster } 187510ae3dc7SBrian Foster 187610ae3dc7SBrian Foster /* 187710ae3dc7SBrian Foster * nextbit is not contiguous with the current end index. Convert 187810ae3dc7SBrian Foster * the current start/end to an extent and add it to the free 187910ae3dc7SBrian Foster * list. 188010ae3dc7SBrian Foster */ 188110ae3dc7SBrian Foster agbno = sagbno + (startidx * XFS_INODES_PER_HOLEMASK_BIT) / 188210ae3dc7SBrian Foster mp->m_sb.sb_inopblock; 188310ae3dc7SBrian Foster contigblk = ((endidx - startidx + 1) * 188410ae3dc7SBrian Foster XFS_INODES_PER_HOLEMASK_BIT) / 188510ae3dc7SBrian Foster mp->m_sb.sb_inopblock; 188610ae3dc7SBrian Foster 188710ae3dc7SBrian Foster ASSERT(agbno % mp->m_sb.sb_spino_align == 0); 188810ae3dc7SBrian Foster ASSERT(contigblk % mp->m_sb.sb_spino_align == 0); 18892c3234d1SDarrick J. Wong xfs_bmap_add_free(mp, dfops, XFS_AGB_TO_FSB(mp, agno, agbno), 1890340785ccSDarrick J. Wong contigblk, &oinfo); 189110ae3dc7SBrian Foster 189210ae3dc7SBrian Foster /* reset range to current bit and carry on... */ 189310ae3dc7SBrian Foster startidx = endidx = nextbit; 189410ae3dc7SBrian Foster 189510ae3dc7SBrian Foster next: 189610ae3dc7SBrian Foster nextbit++; 189710ae3dc7SBrian Foster } 189810ae3dc7SBrian Foster } 189910ae3dc7SBrian Foster 190030f712c9SDave Chinner STATIC int 190130f712c9SDave Chinner xfs_difree_inobt( 190230f712c9SDave Chinner struct xfs_mount *mp, 190330f712c9SDave Chinner struct xfs_trans *tp, 190430f712c9SDave Chinner struct xfs_buf *agbp, 190530f712c9SDave Chinner xfs_agino_t agino, 19062c3234d1SDarrick J. Wong struct xfs_defer_ops *dfops, 190709b56604SBrian Foster struct xfs_icluster *xic, 190830f712c9SDave Chinner struct xfs_inobt_rec_incore *orec) 190930f712c9SDave Chinner { 191030f712c9SDave Chinner struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp); 191130f712c9SDave Chinner xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno); 191230f712c9SDave Chinner struct xfs_perag *pag; 191330f712c9SDave Chinner struct xfs_btree_cur *cur; 191430f712c9SDave Chinner struct xfs_inobt_rec_incore rec; 191530f712c9SDave Chinner int ilen; 191630f712c9SDave Chinner int error; 191730f712c9SDave Chinner int i; 191830f712c9SDave Chinner int off; 191930f712c9SDave Chinner 192030f712c9SDave Chinner ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC)); 192130f712c9SDave Chinner ASSERT(XFS_AGINO_TO_AGBNO(mp, agino) < be32_to_cpu(agi->agi_length)); 192230f712c9SDave Chinner 192330f712c9SDave Chinner /* 192430f712c9SDave Chinner * Initialize the cursor. 192530f712c9SDave Chinner */ 192630f712c9SDave Chinner cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO); 192730f712c9SDave Chinner 192830f712c9SDave Chinner error = xfs_check_agi_freecount(cur, agi); 192930f712c9SDave Chinner if (error) 193030f712c9SDave Chinner goto error0; 193130f712c9SDave Chinner 193230f712c9SDave Chinner /* 193330f712c9SDave Chinner * Look for the entry describing this inode. 193430f712c9SDave Chinner */ 193530f712c9SDave Chinner if ((error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i))) { 193630f712c9SDave Chinner xfs_warn(mp, "%s: xfs_inobt_lookup() returned error %d.", 193730f712c9SDave Chinner __func__, error); 193830f712c9SDave Chinner goto error0; 193930f712c9SDave Chinner } 1940c29aad41SEric Sandeen XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0); 194130f712c9SDave Chinner error = xfs_inobt_get_rec(cur, &rec, &i); 194230f712c9SDave Chinner if (error) { 194330f712c9SDave Chinner xfs_warn(mp, "%s: xfs_inobt_get_rec() returned error %d.", 194430f712c9SDave Chinner __func__, error); 194530f712c9SDave Chinner goto error0; 194630f712c9SDave Chinner } 1947c29aad41SEric Sandeen XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0); 194830f712c9SDave Chinner /* 194930f712c9SDave Chinner * Get the offset in the inode chunk. 195030f712c9SDave Chinner */ 195130f712c9SDave Chinner off = agino - rec.ir_startino; 195230f712c9SDave Chinner ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK); 195330f712c9SDave Chinner ASSERT(!(rec.ir_free & XFS_INOBT_MASK(off))); 195430f712c9SDave Chinner /* 195530f712c9SDave Chinner * Mark the inode free & increment the count. 195630f712c9SDave Chinner */ 195730f712c9SDave Chinner rec.ir_free |= XFS_INOBT_MASK(off); 195830f712c9SDave Chinner rec.ir_freecount++; 195930f712c9SDave Chinner 196030f712c9SDave Chinner /* 1961999633d3SBrian Foster * When an inode chunk is free, it becomes eligible for removal. Don't 1962999633d3SBrian Foster * remove the chunk if the block size is large enough for multiple inode 1963999633d3SBrian Foster * chunks (that might not be free). 196430f712c9SDave Chinner */ 196530f712c9SDave Chinner if (!(mp->m_flags & XFS_MOUNT_IKEEP) && 1966999633d3SBrian Foster rec.ir_free == XFS_INOBT_ALL_FREE && 1967999633d3SBrian Foster mp->m_sb.sb_inopblock <= XFS_INODES_PER_CHUNK) { 196809b56604SBrian Foster xic->deleted = 1; 196909b56604SBrian Foster xic->first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino); 197009b56604SBrian Foster xic->alloc = xfs_inobt_irec_to_allocmask(&rec); 197130f712c9SDave Chinner 197230f712c9SDave Chinner /* 197330f712c9SDave Chinner * Remove the inode cluster from the AGI B+Tree, adjust the 197430f712c9SDave Chinner * AGI and Superblock inode counts, and mark the disk space 197530f712c9SDave Chinner * to be freed when the transaction is committed. 197630f712c9SDave Chinner */ 1977999633d3SBrian Foster ilen = rec.ir_freecount; 197830f712c9SDave Chinner be32_add_cpu(&agi->agi_count, -ilen); 197930f712c9SDave Chinner be32_add_cpu(&agi->agi_freecount, -(ilen - 1)); 198030f712c9SDave Chinner xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT); 198130f712c9SDave Chinner pag = xfs_perag_get(mp, agno); 198230f712c9SDave Chinner pag->pagi_freecount -= ilen - 1; 198330f712c9SDave Chinner xfs_perag_put(pag); 198430f712c9SDave Chinner xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen); 198530f712c9SDave Chinner xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1)); 198630f712c9SDave Chinner 198730f712c9SDave Chinner if ((error = xfs_btree_delete(cur, &i))) { 198830f712c9SDave Chinner xfs_warn(mp, "%s: xfs_btree_delete returned error %d.", 198930f712c9SDave Chinner __func__, error); 199030f712c9SDave Chinner goto error0; 199130f712c9SDave Chinner } 199230f712c9SDave Chinner 19932c3234d1SDarrick J. Wong xfs_difree_inode_chunk(mp, agno, &rec, dfops); 199430f712c9SDave Chinner } else { 199509b56604SBrian Foster xic->deleted = 0; 199630f712c9SDave Chinner 199730f712c9SDave Chinner error = xfs_inobt_update(cur, &rec); 199830f712c9SDave Chinner if (error) { 199930f712c9SDave Chinner xfs_warn(mp, "%s: xfs_inobt_update returned error %d.", 200030f712c9SDave Chinner __func__, error); 200130f712c9SDave Chinner goto error0; 200230f712c9SDave Chinner } 200330f712c9SDave Chinner 200430f712c9SDave Chinner /* 200530f712c9SDave Chinner * Change the inode free counts and log the ag/sb changes. 200630f712c9SDave Chinner */ 200730f712c9SDave Chinner be32_add_cpu(&agi->agi_freecount, 1); 200830f712c9SDave Chinner xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT); 200930f712c9SDave Chinner pag = xfs_perag_get(mp, agno); 201030f712c9SDave Chinner pag->pagi_freecount++; 201130f712c9SDave Chinner xfs_perag_put(pag); 201230f712c9SDave Chinner xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1); 201330f712c9SDave Chinner } 201430f712c9SDave Chinner 201530f712c9SDave Chinner error = xfs_check_agi_freecount(cur, agi); 201630f712c9SDave Chinner if (error) 201730f712c9SDave Chinner goto error0; 201830f712c9SDave Chinner 201930f712c9SDave Chinner *orec = rec; 202030f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); 202130f712c9SDave Chinner return 0; 202230f712c9SDave Chinner 202330f712c9SDave Chinner error0: 202430f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); 202530f712c9SDave Chinner return error; 202630f712c9SDave Chinner } 202730f712c9SDave Chinner 202830f712c9SDave Chinner /* 202930f712c9SDave Chinner * Free an inode in the free inode btree. 203030f712c9SDave Chinner */ 203130f712c9SDave Chinner STATIC int 203230f712c9SDave Chinner xfs_difree_finobt( 203330f712c9SDave Chinner struct xfs_mount *mp, 203430f712c9SDave Chinner struct xfs_trans *tp, 203530f712c9SDave Chinner struct xfs_buf *agbp, 203630f712c9SDave Chinner xfs_agino_t agino, 203730f712c9SDave Chinner struct xfs_inobt_rec_incore *ibtrec) /* inobt record */ 203830f712c9SDave Chinner { 203930f712c9SDave Chinner struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp); 204030f712c9SDave Chinner xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno); 204130f712c9SDave Chinner struct xfs_btree_cur *cur; 204230f712c9SDave Chinner struct xfs_inobt_rec_incore rec; 204330f712c9SDave Chinner int offset = agino - ibtrec->ir_startino; 204430f712c9SDave Chinner int error; 204530f712c9SDave Chinner int i; 204630f712c9SDave Chinner 204730f712c9SDave Chinner cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_FINO); 204830f712c9SDave Chinner 204930f712c9SDave Chinner error = xfs_inobt_lookup(cur, ibtrec->ir_startino, XFS_LOOKUP_EQ, &i); 205030f712c9SDave Chinner if (error) 205130f712c9SDave Chinner goto error; 205230f712c9SDave Chinner if (i == 0) { 205330f712c9SDave Chinner /* 205430f712c9SDave Chinner * If the record does not exist in the finobt, we must have just 205530f712c9SDave Chinner * freed an inode in a previously fully allocated chunk. If not, 205630f712c9SDave Chinner * something is out of sync. 205730f712c9SDave Chinner */ 2058c29aad41SEric Sandeen XFS_WANT_CORRUPTED_GOTO(mp, ibtrec->ir_freecount == 1, error); 205930f712c9SDave Chinner 20605419040fSBrian Foster error = xfs_inobt_insert_rec(cur, ibtrec->ir_holemask, 20615419040fSBrian Foster ibtrec->ir_count, 20625419040fSBrian Foster ibtrec->ir_freecount, 206330f712c9SDave Chinner ibtrec->ir_free, &i); 206430f712c9SDave Chinner if (error) 206530f712c9SDave Chinner goto error; 206630f712c9SDave Chinner ASSERT(i == 1); 206730f712c9SDave Chinner 206830f712c9SDave Chinner goto out; 206930f712c9SDave Chinner } 207030f712c9SDave Chinner 207130f712c9SDave Chinner /* 207230f712c9SDave Chinner * Read and update the existing record. We could just copy the ibtrec 207330f712c9SDave Chinner * across here, but that would defeat the purpose of having redundant 207430f712c9SDave Chinner * metadata. By making the modifications independently, we can catch 207530f712c9SDave Chinner * corruptions that we wouldn't see if we just copied from one record 207630f712c9SDave Chinner * to another. 207730f712c9SDave Chinner */ 207830f712c9SDave Chinner error = xfs_inobt_get_rec(cur, &rec, &i); 207930f712c9SDave Chinner if (error) 208030f712c9SDave Chinner goto error; 2081c29aad41SEric Sandeen XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error); 208230f712c9SDave Chinner 208330f712c9SDave Chinner rec.ir_free |= XFS_INOBT_MASK(offset); 208430f712c9SDave Chinner rec.ir_freecount++; 208530f712c9SDave Chinner 2086c29aad41SEric Sandeen XFS_WANT_CORRUPTED_GOTO(mp, (rec.ir_free == ibtrec->ir_free) && 208730f712c9SDave Chinner (rec.ir_freecount == ibtrec->ir_freecount), 208830f712c9SDave Chinner error); 208930f712c9SDave Chinner 209030f712c9SDave Chinner /* 209130f712c9SDave Chinner * The content of inobt records should always match between the inobt 209230f712c9SDave Chinner * and finobt. The lifecycle of records in the finobt is different from 209330f712c9SDave Chinner * the inobt in that the finobt only tracks records with at least one 209430f712c9SDave Chinner * free inode. Hence, if all of the inodes are free and we aren't 209530f712c9SDave Chinner * keeping inode chunks permanently on disk, remove the record. 209630f712c9SDave Chinner * Otherwise, update the record with the new information. 2097999633d3SBrian Foster * 2098999633d3SBrian Foster * Note that we currently can't free chunks when the block size is large 2099999633d3SBrian Foster * enough for multiple chunks. Leave the finobt record to remain in sync 2100999633d3SBrian Foster * with the inobt. 210130f712c9SDave Chinner */ 2102999633d3SBrian Foster if (rec.ir_free == XFS_INOBT_ALL_FREE && 2103999633d3SBrian Foster mp->m_sb.sb_inopblock <= XFS_INODES_PER_CHUNK && 210430f712c9SDave Chinner !(mp->m_flags & XFS_MOUNT_IKEEP)) { 210530f712c9SDave Chinner error = xfs_btree_delete(cur, &i); 210630f712c9SDave Chinner if (error) 210730f712c9SDave Chinner goto error; 210830f712c9SDave Chinner ASSERT(i == 1); 210930f712c9SDave Chinner } else { 211030f712c9SDave Chinner error = xfs_inobt_update(cur, &rec); 211130f712c9SDave Chinner if (error) 211230f712c9SDave Chinner goto error; 211330f712c9SDave Chinner } 211430f712c9SDave Chinner 211530f712c9SDave Chinner out: 211630f712c9SDave Chinner error = xfs_check_agi_freecount(cur, agi); 211730f712c9SDave Chinner if (error) 211830f712c9SDave Chinner goto error; 211930f712c9SDave Chinner 212030f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); 212130f712c9SDave Chinner return 0; 212230f712c9SDave Chinner 212330f712c9SDave Chinner error: 212430f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); 212530f712c9SDave Chinner return error; 212630f712c9SDave Chinner } 212730f712c9SDave Chinner 212830f712c9SDave Chinner /* 212930f712c9SDave Chinner * Free disk inode. Carefully avoids touching the incore inode, all 213030f712c9SDave Chinner * manipulations incore are the caller's responsibility. 213130f712c9SDave Chinner * The on-disk inode is not changed by this operation, only the 213230f712c9SDave Chinner * btree (free inode mask) is changed. 213330f712c9SDave Chinner */ 213430f712c9SDave Chinner int 213530f712c9SDave Chinner xfs_difree( 213630f712c9SDave Chinner struct xfs_trans *tp, /* transaction pointer */ 213730f712c9SDave Chinner xfs_ino_t inode, /* inode to be freed */ 21382c3234d1SDarrick J. Wong struct xfs_defer_ops *dfops, /* extents to free */ 213909b56604SBrian Foster struct xfs_icluster *xic) /* cluster info if deleted */ 214030f712c9SDave Chinner { 214130f712c9SDave Chinner /* REFERENCED */ 214230f712c9SDave Chinner xfs_agblock_t agbno; /* block number containing inode */ 214330f712c9SDave Chinner struct xfs_buf *agbp; /* buffer for allocation group header */ 214430f712c9SDave Chinner xfs_agino_t agino; /* allocation group inode number */ 214530f712c9SDave Chinner xfs_agnumber_t agno; /* allocation group number */ 214630f712c9SDave Chinner int error; /* error return value */ 214730f712c9SDave Chinner struct xfs_mount *mp; /* mount structure for filesystem */ 214830f712c9SDave Chinner struct xfs_inobt_rec_incore rec;/* btree record */ 214930f712c9SDave Chinner 215030f712c9SDave Chinner mp = tp->t_mountp; 215130f712c9SDave Chinner 215230f712c9SDave Chinner /* 215330f712c9SDave Chinner * Break up inode number into its components. 215430f712c9SDave Chinner */ 215530f712c9SDave Chinner agno = XFS_INO_TO_AGNO(mp, inode); 215630f712c9SDave Chinner if (agno >= mp->m_sb.sb_agcount) { 215730f712c9SDave Chinner xfs_warn(mp, "%s: agno >= mp->m_sb.sb_agcount (%d >= %d).", 215830f712c9SDave Chinner __func__, agno, mp->m_sb.sb_agcount); 215930f712c9SDave Chinner ASSERT(0); 21602451337dSDave Chinner return -EINVAL; 216130f712c9SDave Chinner } 216230f712c9SDave Chinner agino = XFS_INO_TO_AGINO(mp, inode); 216330f712c9SDave Chinner if (inode != XFS_AGINO_TO_INO(mp, agno, agino)) { 216430f712c9SDave Chinner xfs_warn(mp, "%s: inode != XFS_AGINO_TO_INO() (%llu != %llu).", 216530f712c9SDave Chinner __func__, (unsigned long long)inode, 216630f712c9SDave Chinner (unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino)); 216730f712c9SDave Chinner ASSERT(0); 21682451337dSDave Chinner return -EINVAL; 216930f712c9SDave Chinner } 217030f712c9SDave Chinner agbno = XFS_AGINO_TO_AGBNO(mp, agino); 217130f712c9SDave Chinner if (agbno >= mp->m_sb.sb_agblocks) { 217230f712c9SDave Chinner xfs_warn(mp, "%s: agbno >= mp->m_sb.sb_agblocks (%d >= %d).", 217330f712c9SDave Chinner __func__, agbno, mp->m_sb.sb_agblocks); 217430f712c9SDave Chinner ASSERT(0); 21752451337dSDave Chinner return -EINVAL; 217630f712c9SDave Chinner } 217730f712c9SDave Chinner /* 217830f712c9SDave Chinner * Get the allocation group header. 217930f712c9SDave Chinner */ 218030f712c9SDave Chinner error = xfs_ialloc_read_agi(mp, tp, agno, &agbp); 218130f712c9SDave Chinner if (error) { 218230f712c9SDave Chinner xfs_warn(mp, "%s: xfs_ialloc_read_agi() returned error %d.", 218330f712c9SDave Chinner __func__, error); 218430f712c9SDave Chinner return error; 218530f712c9SDave Chinner } 218630f712c9SDave Chinner 218730f712c9SDave Chinner /* 218830f712c9SDave Chinner * Fix up the inode allocation btree. 218930f712c9SDave Chinner */ 21902c3234d1SDarrick J. Wong error = xfs_difree_inobt(mp, tp, agbp, agino, dfops, xic, &rec); 219130f712c9SDave Chinner if (error) 219230f712c9SDave Chinner goto error0; 219330f712c9SDave Chinner 219430f712c9SDave Chinner /* 219530f712c9SDave Chinner * Fix up the free inode btree. 219630f712c9SDave Chinner */ 219730f712c9SDave Chinner if (xfs_sb_version_hasfinobt(&mp->m_sb)) { 219830f712c9SDave Chinner error = xfs_difree_finobt(mp, tp, agbp, agino, &rec); 219930f712c9SDave Chinner if (error) 220030f712c9SDave Chinner goto error0; 220130f712c9SDave Chinner } 220230f712c9SDave Chinner 220330f712c9SDave Chinner return 0; 220430f712c9SDave Chinner 220530f712c9SDave Chinner error0: 220630f712c9SDave Chinner return error; 220730f712c9SDave Chinner } 220830f712c9SDave Chinner 220930f712c9SDave Chinner STATIC int 221030f712c9SDave Chinner xfs_imap_lookup( 221130f712c9SDave Chinner struct xfs_mount *mp, 221230f712c9SDave Chinner struct xfs_trans *tp, 221330f712c9SDave Chinner xfs_agnumber_t agno, 221430f712c9SDave Chinner xfs_agino_t agino, 221530f712c9SDave Chinner xfs_agblock_t agbno, 221630f712c9SDave Chinner xfs_agblock_t *chunk_agbno, 221730f712c9SDave Chinner xfs_agblock_t *offset_agbno, 221830f712c9SDave Chinner int flags) 221930f712c9SDave Chinner { 222030f712c9SDave Chinner struct xfs_inobt_rec_incore rec; 222130f712c9SDave Chinner struct xfs_btree_cur *cur; 222230f712c9SDave Chinner struct xfs_buf *agbp; 222330f712c9SDave Chinner int error; 222430f712c9SDave Chinner int i; 222530f712c9SDave Chinner 222630f712c9SDave Chinner error = xfs_ialloc_read_agi(mp, tp, agno, &agbp); 222730f712c9SDave Chinner if (error) { 222830f712c9SDave Chinner xfs_alert(mp, 222930f712c9SDave Chinner "%s: xfs_ialloc_read_agi() returned error %d, agno %d", 223030f712c9SDave Chinner __func__, error, agno); 223130f712c9SDave Chinner return error; 223230f712c9SDave Chinner } 223330f712c9SDave Chinner 223430f712c9SDave Chinner /* 223530f712c9SDave Chinner * Lookup the inode record for the given agino. If the record cannot be 223630f712c9SDave Chinner * found, then it's an invalid inode number and we should abort. Once 223730f712c9SDave Chinner * we have a record, we need to ensure it contains the inode number 223830f712c9SDave Chinner * we are looking up. 223930f712c9SDave Chinner */ 224030f712c9SDave Chinner cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO); 224130f712c9SDave Chinner error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i); 224230f712c9SDave Chinner if (!error) { 224330f712c9SDave Chinner if (i) 224430f712c9SDave Chinner error = xfs_inobt_get_rec(cur, &rec, &i); 224530f712c9SDave Chinner if (!error && i == 0) 22462451337dSDave Chinner error = -EINVAL; 224730f712c9SDave Chinner } 224830f712c9SDave Chinner 224930f712c9SDave Chinner xfs_trans_brelse(tp, agbp); 2250f307080aSBrian Foster xfs_btree_del_cursor(cur, error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); 225130f712c9SDave Chinner if (error) 225230f712c9SDave Chinner return error; 225330f712c9SDave Chinner 225430f712c9SDave Chinner /* check that the returned record contains the required inode */ 225530f712c9SDave Chinner if (rec.ir_startino > agino || 225630f712c9SDave Chinner rec.ir_startino + mp->m_ialloc_inos <= agino) 22572451337dSDave Chinner return -EINVAL; 225830f712c9SDave Chinner 225930f712c9SDave Chinner /* for untrusted inodes check it is allocated first */ 226030f712c9SDave Chinner if ((flags & XFS_IGET_UNTRUSTED) && 226130f712c9SDave Chinner (rec.ir_free & XFS_INOBT_MASK(agino - rec.ir_startino))) 22622451337dSDave Chinner return -EINVAL; 226330f712c9SDave Chinner 226430f712c9SDave Chinner *chunk_agbno = XFS_AGINO_TO_AGBNO(mp, rec.ir_startino); 226530f712c9SDave Chinner *offset_agbno = agbno - *chunk_agbno; 226630f712c9SDave Chinner return 0; 226730f712c9SDave Chinner } 226830f712c9SDave Chinner 226930f712c9SDave Chinner /* 227030f712c9SDave Chinner * Return the location of the inode in imap, for mapping it into a buffer. 227130f712c9SDave Chinner */ 227230f712c9SDave Chinner int 227330f712c9SDave Chinner xfs_imap( 227430f712c9SDave Chinner xfs_mount_t *mp, /* file system mount structure */ 227530f712c9SDave Chinner xfs_trans_t *tp, /* transaction pointer */ 227630f712c9SDave Chinner xfs_ino_t ino, /* inode to locate */ 227730f712c9SDave Chinner struct xfs_imap *imap, /* location map structure */ 227830f712c9SDave Chinner uint flags) /* flags for inode btree lookup */ 227930f712c9SDave Chinner { 228030f712c9SDave Chinner xfs_agblock_t agbno; /* block number of inode in the alloc group */ 228130f712c9SDave Chinner xfs_agino_t agino; /* inode number within alloc group */ 228230f712c9SDave Chinner xfs_agnumber_t agno; /* allocation group number */ 228330f712c9SDave Chinner int blks_per_cluster; /* num blocks per inode cluster */ 228430f712c9SDave Chinner xfs_agblock_t chunk_agbno; /* first block in inode chunk */ 228530f712c9SDave Chinner xfs_agblock_t cluster_agbno; /* first block in inode cluster */ 228630f712c9SDave Chinner int error; /* error code */ 228730f712c9SDave Chinner int offset; /* index of inode in its buffer */ 228830f712c9SDave Chinner xfs_agblock_t offset_agbno; /* blks from chunk start to inode */ 228930f712c9SDave Chinner 229030f712c9SDave Chinner ASSERT(ino != NULLFSINO); 229130f712c9SDave Chinner 229230f712c9SDave Chinner /* 229330f712c9SDave Chinner * Split up the inode number into its parts. 229430f712c9SDave Chinner */ 229530f712c9SDave Chinner agno = XFS_INO_TO_AGNO(mp, ino); 229630f712c9SDave Chinner agino = XFS_INO_TO_AGINO(mp, ino); 229730f712c9SDave Chinner agbno = XFS_AGINO_TO_AGBNO(mp, agino); 229830f712c9SDave Chinner if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks || 229930f712c9SDave Chinner ino != XFS_AGINO_TO_INO(mp, agno, agino)) { 230030f712c9SDave Chinner #ifdef DEBUG 230130f712c9SDave Chinner /* 230230f712c9SDave Chinner * Don't output diagnostic information for untrusted inodes 230330f712c9SDave Chinner * as they can be invalid without implying corruption. 230430f712c9SDave Chinner */ 230530f712c9SDave Chinner if (flags & XFS_IGET_UNTRUSTED) 23062451337dSDave Chinner return -EINVAL; 230730f712c9SDave Chinner if (agno >= mp->m_sb.sb_agcount) { 230830f712c9SDave Chinner xfs_alert(mp, 230930f712c9SDave Chinner "%s: agno (%d) >= mp->m_sb.sb_agcount (%d)", 231030f712c9SDave Chinner __func__, agno, mp->m_sb.sb_agcount); 231130f712c9SDave Chinner } 231230f712c9SDave Chinner if (agbno >= mp->m_sb.sb_agblocks) { 231330f712c9SDave Chinner xfs_alert(mp, 231430f712c9SDave Chinner "%s: agbno (0x%llx) >= mp->m_sb.sb_agblocks (0x%lx)", 231530f712c9SDave Chinner __func__, (unsigned long long)agbno, 231630f712c9SDave Chinner (unsigned long)mp->m_sb.sb_agblocks); 231730f712c9SDave Chinner } 231830f712c9SDave Chinner if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) { 231930f712c9SDave Chinner xfs_alert(mp, 232030f712c9SDave Chinner "%s: ino (0x%llx) != XFS_AGINO_TO_INO() (0x%llx)", 232130f712c9SDave Chinner __func__, ino, 232230f712c9SDave Chinner XFS_AGINO_TO_INO(mp, agno, agino)); 232330f712c9SDave Chinner } 232430f712c9SDave Chinner xfs_stack_trace(); 232530f712c9SDave Chinner #endif /* DEBUG */ 23262451337dSDave Chinner return -EINVAL; 232730f712c9SDave Chinner } 232830f712c9SDave Chinner 232930f712c9SDave Chinner blks_per_cluster = xfs_icluster_size_fsb(mp); 233030f712c9SDave Chinner 233130f712c9SDave Chinner /* 233230f712c9SDave Chinner * For bulkstat and handle lookups, we have an untrusted inode number 233330f712c9SDave Chinner * that we have to verify is valid. We cannot do this just by reading 233430f712c9SDave Chinner * the inode buffer as it may have been unlinked and removed leaving 233530f712c9SDave Chinner * inodes in stale state on disk. Hence we have to do a btree lookup 233630f712c9SDave Chinner * in all cases where an untrusted inode number is passed. 233730f712c9SDave Chinner */ 233830f712c9SDave Chinner if (flags & XFS_IGET_UNTRUSTED) { 233930f712c9SDave Chinner error = xfs_imap_lookup(mp, tp, agno, agino, agbno, 234030f712c9SDave Chinner &chunk_agbno, &offset_agbno, flags); 234130f712c9SDave Chinner if (error) 234230f712c9SDave Chinner return error; 234330f712c9SDave Chinner goto out_map; 234430f712c9SDave Chinner } 234530f712c9SDave Chinner 234630f712c9SDave Chinner /* 234730f712c9SDave Chinner * If the inode cluster size is the same as the blocksize or 234830f712c9SDave Chinner * smaller we get to the buffer by simple arithmetics. 234930f712c9SDave Chinner */ 235030f712c9SDave Chinner if (blks_per_cluster == 1) { 235130f712c9SDave Chinner offset = XFS_INO_TO_OFFSET(mp, ino); 235230f712c9SDave Chinner ASSERT(offset < mp->m_sb.sb_inopblock); 235330f712c9SDave Chinner 235430f712c9SDave Chinner imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, agbno); 235530f712c9SDave Chinner imap->im_len = XFS_FSB_TO_BB(mp, 1); 2356755c7bf5SDarrick J. Wong imap->im_boffset = (unsigned short)(offset << 2357755c7bf5SDarrick J. Wong mp->m_sb.sb_inodelog); 235830f712c9SDave Chinner return 0; 235930f712c9SDave Chinner } 236030f712c9SDave Chinner 236130f712c9SDave Chinner /* 236230f712c9SDave Chinner * If the inode chunks are aligned then use simple maths to 236330f712c9SDave Chinner * find the location. Otherwise we have to do a btree 236430f712c9SDave Chinner * lookup to find the location. 236530f712c9SDave Chinner */ 236630f712c9SDave Chinner if (mp->m_inoalign_mask) { 236730f712c9SDave Chinner offset_agbno = agbno & mp->m_inoalign_mask; 236830f712c9SDave Chinner chunk_agbno = agbno - offset_agbno; 236930f712c9SDave Chinner } else { 237030f712c9SDave Chinner error = xfs_imap_lookup(mp, tp, agno, agino, agbno, 237130f712c9SDave Chinner &chunk_agbno, &offset_agbno, flags); 237230f712c9SDave Chinner if (error) 237330f712c9SDave Chinner return error; 237430f712c9SDave Chinner } 237530f712c9SDave Chinner 237630f712c9SDave Chinner out_map: 237730f712c9SDave Chinner ASSERT(agbno >= chunk_agbno); 237830f712c9SDave Chinner cluster_agbno = chunk_agbno + 237930f712c9SDave Chinner ((offset_agbno / blks_per_cluster) * blks_per_cluster); 238030f712c9SDave Chinner offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) + 238130f712c9SDave Chinner XFS_INO_TO_OFFSET(mp, ino); 238230f712c9SDave Chinner 238330f712c9SDave Chinner imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, cluster_agbno); 238430f712c9SDave Chinner imap->im_len = XFS_FSB_TO_BB(mp, blks_per_cluster); 2385755c7bf5SDarrick J. Wong imap->im_boffset = (unsigned short)(offset << mp->m_sb.sb_inodelog); 238630f712c9SDave Chinner 238730f712c9SDave Chinner /* 238830f712c9SDave Chinner * If the inode number maps to a block outside the bounds 238930f712c9SDave Chinner * of the file system then return NULL rather than calling 239030f712c9SDave Chinner * read_buf and panicing when we get an error from the 239130f712c9SDave Chinner * driver. 239230f712c9SDave Chinner */ 239330f712c9SDave Chinner if ((imap->im_blkno + imap->im_len) > 239430f712c9SDave Chinner XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) { 239530f712c9SDave Chinner xfs_alert(mp, 239630f712c9SDave Chinner "%s: (im_blkno (0x%llx) + im_len (0x%llx)) > sb_dblocks (0x%llx)", 239730f712c9SDave Chinner __func__, (unsigned long long) imap->im_blkno, 239830f712c9SDave Chinner (unsigned long long) imap->im_len, 239930f712c9SDave Chinner XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)); 24002451337dSDave Chinner return -EINVAL; 240130f712c9SDave Chinner } 240230f712c9SDave Chinner return 0; 240330f712c9SDave Chinner } 240430f712c9SDave Chinner 240530f712c9SDave Chinner /* 240630f712c9SDave Chinner * Compute and fill in value of m_in_maxlevels. 240730f712c9SDave Chinner */ 240830f712c9SDave Chinner void 240930f712c9SDave Chinner xfs_ialloc_compute_maxlevels( 241030f712c9SDave Chinner xfs_mount_t *mp) /* file system mount structure */ 241130f712c9SDave Chinner { 241219b54ee6SDarrick J. Wong uint inodes; 241330f712c9SDave Chinner 241419b54ee6SDarrick J. Wong inodes = (1LL << XFS_INO_AGINO_BITS(mp)) >> XFS_INODES_PER_CHUNK_LOG; 241519b54ee6SDarrick J. Wong mp->m_in_maxlevels = xfs_btree_compute_maxlevels(mp, mp->m_inobt_mnr, 241619b54ee6SDarrick J. Wong inodes); 241730f712c9SDave Chinner } 241830f712c9SDave Chinner 241930f712c9SDave Chinner /* 242030f712c9SDave Chinner * Log specified fields for the ag hdr (inode section). The growth of the agi 242130f712c9SDave Chinner * structure over time requires that we interpret the buffer as two logical 242230f712c9SDave Chinner * regions delineated by the end of the unlinked list. This is due to the size 242330f712c9SDave Chinner * of the hash table and its location in the middle of the agi. 242430f712c9SDave Chinner * 242530f712c9SDave Chinner * For example, a request to log a field before agi_unlinked and a field after 242630f712c9SDave Chinner * agi_unlinked could cause us to log the entire hash table and use an excessive 242730f712c9SDave Chinner * amount of log space. To avoid this behavior, log the region up through 242830f712c9SDave Chinner * agi_unlinked in one call and the region after agi_unlinked through the end of 242930f712c9SDave Chinner * the structure in another. 243030f712c9SDave Chinner */ 243130f712c9SDave Chinner void 243230f712c9SDave Chinner xfs_ialloc_log_agi( 243330f712c9SDave Chinner xfs_trans_t *tp, /* transaction pointer */ 243430f712c9SDave Chinner xfs_buf_t *bp, /* allocation group header buffer */ 243530f712c9SDave Chinner int fields) /* bitmask of fields to log */ 243630f712c9SDave Chinner { 243730f712c9SDave Chinner int first; /* first byte number */ 243830f712c9SDave Chinner int last; /* last byte number */ 243930f712c9SDave Chinner static const short offsets[] = { /* field starting offsets */ 244030f712c9SDave Chinner /* keep in sync with bit definitions */ 244130f712c9SDave Chinner offsetof(xfs_agi_t, agi_magicnum), 244230f712c9SDave Chinner offsetof(xfs_agi_t, agi_versionnum), 244330f712c9SDave Chinner offsetof(xfs_agi_t, agi_seqno), 244430f712c9SDave Chinner offsetof(xfs_agi_t, agi_length), 244530f712c9SDave Chinner offsetof(xfs_agi_t, agi_count), 244630f712c9SDave Chinner offsetof(xfs_agi_t, agi_root), 244730f712c9SDave Chinner offsetof(xfs_agi_t, agi_level), 244830f712c9SDave Chinner offsetof(xfs_agi_t, agi_freecount), 244930f712c9SDave Chinner offsetof(xfs_agi_t, agi_newino), 245030f712c9SDave Chinner offsetof(xfs_agi_t, agi_dirino), 245130f712c9SDave Chinner offsetof(xfs_agi_t, agi_unlinked), 245230f712c9SDave Chinner offsetof(xfs_agi_t, agi_free_root), 245330f712c9SDave Chinner offsetof(xfs_agi_t, agi_free_level), 245430f712c9SDave Chinner sizeof(xfs_agi_t) 245530f712c9SDave Chinner }; 245630f712c9SDave Chinner #ifdef DEBUG 245730f712c9SDave Chinner xfs_agi_t *agi; /* allocation group header */ 245830f712c9SDave Chinner 245930f712c9SDave Chinner agi = XFS_BUF_TO_AGI(bp); 246030f712c9SDave Chinner ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC)); 246130f712c9SDave Chinner #endif 246230f712c9SDave Chinner 246330f712c9SDave Chinner /* 246430f712c9SDave Chinner * Compute byte offsets for the first and last fields in the first 246530f712c9SDave Chinner * region and log the agi buffer. This only logs up through 246630f712c9SDave Chinner * agi_unlinked. 246730f712c9SDave Chinner */ 246830f712c9SDave Chinner if (fields & XFS_AGI_ALL_BITS_R1) { 246930f712c9SDave Chinner xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R1, 247030f712c9SDave Chinner &first, &last); 247130f712c9SDave Chinner xfs_trans_log_buf(tp, bp, first, last); 247230f712c9SDave Chinner } 247330f712c9SDave Chinner 247430f712c9SDave Chinner /* 247530f712c9SDave Chinner * Mask off the bits in the first region and calculate the first and 247630f712c9SDave Chinner * last field offsets for any bits in the second region. 247730f712c9SDave Chinner */ 247830f712c9SDave Chinner fields &= ~XFS_AGI_ALL_BITS_R1; 247930f712c9SDave Chinner if (fields) { 248030f712c9SDave Chinner xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R2, 248130f712c9SDave Chinner &first, &last); 248230f712c9SDave Chinner xfs_trans_log_buf(tp, bp, first, last); 248330f712c9SDave Chinner } 248430f712c9SDave Chinner } 248530f712c9SDave Chinner 248630f712c9SDave Chinner #ifdef DEBUG 248730f712c9SDave Chinner STATIC void 248830f712c9SDave Chinner xfs_check_agi_unlinked( 248930f712c9SDave Chinner struct xfs_agi *agi) 249030f712c9SDave Chinner { 249130f712c9SDave Chinner int i; 249230f712c9SDave Chinner 249330f712c9SDave Chinner for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++) 249430f712c9SDave Chinner ASSERT(agi->agi_unlinked[i]); 249530f712c9SDave Chinner } 249630f712c9SDave Chinner #else 249730f712c9SDave Chinner #define xfs_check_agi_unlinked(agi) 249830f712c9SDave Chinner #endif 249930f712c9SDave Chinner 250030f712c9SDave Chinner static bool 250130f712c9SDave Chinner xfs_agi_verify( 250230f712c9SDave Chinner struct xfs_buf *bp) 250330f712c9SDave Chinner { 250430f712c9SDave Chinner struct xfs_mount *mp = bp->b_target->bt_mount; 250530f712c9SDave Chinner struct xfs_agi *agi = XFS_BUF_TO_AGI(bp); 250630f712c9SDave Chinner 2507a45086e2SBrian Foster if (xfs_sb_version_hascrc(&mp->m_sb)) { 2508a45086e2SBrian Foster if (!uuid_equal(&agi->agi_uuid, &mp->m_sb.sb_meta_uuid)) 250930f712c9SDave Chinner return false; 2510a45086e2SBrian Foster if (!xfs_log_check_lsn(mp, 2511a45086e2SBrian Foster be64_to_cpu(XFS_BUF_TO_AGI(bp)->agi_lsn))) 2512a45086e2SBrian Foster return false; 2513a45086e2SBrian Foster } 2514a45086e2SBrian Foster 251530f712c9SDave Chinner /* 251630f712c9SDave Chinner * Validate the magic number of the agi block. 251730f712c9SDave Chinner */ 251830f712c9SDave Chinner if (agi->agi_magicnum != cpu_to_be32(XFS_AGI_MAGIC)) 251930f712c9SDave Chinner return false; 252030f712c9SDave Chinner if (!XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum))) 252130f712c9SDave Chinner return false; 252230f712c9SDave Chinner 2523d2a047f3SDarrick J. Wong if (be32_to_cpu(agi->agi_level) < 1 || 2524d2a047f3SDarrick J. Wong be32_to_cpu(agi->agi_level) > XFS_BTREE_MAXLEVELS) 2525e1b05723SEric Sandeen return false; 2526d2a047f3SDarrick J. Wong 2527d2a047f3SDarrick J. Wong if (xfs_sb_version_hasfinobt(&mp->m_sb) && 2528d2a047f3SDarrick J. Wong (be32_to_cpu(agi->agi_free_level) < 1 || 2529d2a047f3SDarrick J. Wong be32_to_cpu(agi->agi_free_level) > XFS_BTREE_MAXLEVELS)) 2530d2a047f3SDarrick J. Wong return false; 2531d2a047f3SDarrick J. Wong 253230f712c9SDave Chinner /* 253330f712c9SDave Chinner * during growfs operations, the perag is not fully initialised, 253430f712c9SDave Chinner * so we can't use it for any useful checking. growfs ensures we can't 253530f712c9SDave Chinner * use it by using uncached buffers that don't have the perag attached 253630f712c9SDave Chinner * so we can detect and avoid this problem. 253730f712c9SDave Chinner */ 253830f712c9SDave Chinner if (bp->b_pag && be32_to_cpu(agi->agi_seqno) != bp->b_pag->pag_agno) 253930f712c9SDave Chinner return false; 254030f712c9SDave Chinner 254130f712c9SDave Chinner xfs_check_agi_unlinked(agi); 254230f712c9SDave Chinner return true; 254330f712c9SDave Chinner } 254430f712c9SDave Chinner 254530f712c9SDave Chinner static void 254630f712c9SDave Chinner xfs_agi_read_verify( 254730f712c9SDave Chinner struct xfs_buf *bp) 254830f712c9SDave Chinner { 254930f712c9SDave Chinner struct xfs_mount *mp = bp->b_target->bt_mount; 255030f712c9SDave Chinner 255130f712c9SDave Chinner if (xfs_sb_version_hascrc(&mp->m_sb) && 255230f712c9SDave Chinner !xfs_buf_verify_cksum(bp, XFS_AGI_CRC_OFF)) 25532451337dSDave Chinner xfs_buf_ioerror(bp, -EFSBADCRC); 255430f712c9SDave Chinner else if (XFS_TEST_ERROR(!xfs_agi_verify(bp), mp, 255530f712c9SDave Chinner XFS_ERRTAG_IALLOC_READ_AGI, 255630f712c9SDave Chinner XFS_RANDOM_IALLOC_READ_AGI)) 25572451337dSDave Chinner xfs_buf_ioerror(bp, -EFSCORRUPTED); 255830f712c9SDave Chinner 255930f712c9SDave Chinner if (bp->b_error) 256030f712c9SDave Chinner xfs_verifier_error(bp); 256130f712c9SDave Chinner } 256230f712c9SDave Chinner 256330f712c9SDave Chinner static void 256430f712c9SDave Chinner xfs_agi_write_verify( 256530f712c9SDave Chinner struct xfs_buf *bp) 256630f712c9SDave Chinner { 256730f712c9SDave Chinner struct xfs_mount *mp = bp->b_target->bt_mount; 256830f712c9SDave Chinner struct xfs_buf_log_item *bip = bp->b_fspriv; 256930f712c9SDave Chinner 257030f712c9SDave Chinner if (!xfs_agi_verify(bp)) { 25712451337dSDave Chinner xfs_buf_ioerror(bp, -EFSCORRUPTED); 257230f712c9SDave Chinner xfs_verifier_error(bp); 257330f712c9SDave Chinner return; 257430f712c9SDave Chinner } 257530f712c9SDave Chinner 257630f712c9SDave Chinner if (!xfs_sb_version_hascrc(&mp->m_sb)) 257730f712c9SDave Chinner return; 257830f712c9SDave Chinner 257930f712c9SDave Chinner if (bip) 258030f712c9SDave Chinner XFS_BUF_TO_AGI(bp)->agi_lsn = cpu_to_be64(bip->bli_item.li_lsn); 258130f712c9SDave Chinner xfs_buf_update_cksum(bp, XFS_AGI_CRC_OFF); 258230f712c9SDave Chinner } 258330f712c9SDave Chinner 258430f712c9SDave Chinner const struct xfs_buf_ops xfs_agi_buf_ops = { 2585233135b7SEric Sandeen .name = "xfs_agi", 258630f712c9SDave Chinner .verify_read = xfs_agi_read_verify, 258730f712c9SDave Chinner .verify_write = xfs_agi_write_verify, 258830f712c9SDave Chinner }; 258930f712c9SDave Chinner 259030f712c9SDave Chinner /* 259130f712c9SDave Chinner * Read in the allocation group header (inode allocation section) 259230f712c9SDave Chinner */ 259330f712c9SDave Chinner int 259430f712c9SDave Chinner xfs_read_agi( 259530f712c9SDave Chinner struct xfs_mount *mp, /* file system mount structure */ 259630f712c9SDave Chinner struct xfs_trans *tp, /* transaction pointer */ 259730f712c9SDave Chinner xfs_agnumber_t agno, /* allocation group number */ 259830f712c9SDave Chinner struct xfs_buf **bpp) /* allocation group hdr buf */ 259930f712c9SDave Chinner { 260030f712c9SDave Chinner int error; 260130f712c9SDave Chinner 260230f712c9SDave Chinner trace_xfs_read_agi(mp, agno); 260330f712c9SDave Chinner 260430f712c9SDave Chinner ASSERT(agno != NULLAGNUMBER); 260530f712c9SDave Chinner error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, 260630f712c9SDave Chinner XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)), 260730f712c9SDave Chinner XFS_FSS_TO_BB(mp, 1), 0, bpp, &xfs_agi_buf_ops); 260830f712c9SDave Chinner if (error) 260930f712c9SDave Chinner return error; 2610200237d6SEric Sandeen if (tp) 2611200237d6SEric Sandeen xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_AGI_BUF); 261230f712c9SDave Chinner 261330f712c9SDave Chinner xfs_buf_set_ref(*bpp, XFS_AGI_REF); 261430f712c9SDave Chinner return 0; 261530f712c9SDave Chinner } 261630f712c9SDave Chinner 261730f712c9SDave Chinner int 261830f712c9SDave Chinner xfs_ialloc_read_agi( 261930f712c9SDave Chinner struct xfs_mount *mp, /* file system mount structure */ 262030f712c9SDave Chinner struct xfs_trans *tp, /* transaction pointer */ 262130f712c9SDave Chinner xfs_agnumber_t agno, /* allocation group number */ 262230f712c9SDave Chinner struct xfs_buf **bpp) /* allocation group hdr buf */ 262330f712c9SDave Chinner { 262430f712c9SDave Chinner struct xfs_agi *agi; /* allocation group header */ 262530f712c9SDave Chinner struct xfs_perag *pag; /* per allocation group data */ 262630f712c9SDave Chinner int error; 262730f712c9SDave Chinner 262830f712c9SDave Chinner trace_xfs_ialloc_read_agi(mp, agno); 262930f712c9SDave Chinner 263030f712c9SDave Chinner error = xfs_read_agi(mp, tp, agno, bpp); 263130f712c9SDave Chinner if (error) 263230f712c9SDave Chinner return error; 263330f712c9SDave Chinner 263430f712c9SDave Chinner agi = XFS_BUF_TO_AGI(*bpp); 263530f712c9SDave Chinner pag = xfs_perag_get(mp, agno); 263630f712c9SDave Chinner if (!pag->pagi_init) { 263730f712c9SDave Chinner pag->pagi_freecount = be32_to_cpu(agi->agi_freecount); 263830f712c9SDave Chinner pag->pagi_count = be32_to_cpu(agi->agi_count); 263930f712c9SDave Chinner pag->pagi_init = 1; 264030f712c9SDave Chinner } 264130f712c9SDave Chinner 264230f712c9SDave Chinner /* 264330f712c9SDave Chinner * It's possible for these to be out of sync if 264430f712c9SDave Chinner * we are in the middle of a forced shutdown. 264530f712c9SDave Chinner */ 264630f712c9SDave Chinner ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) || 264730f712c9SDave Chinner XFS_FORCED_SHUTDOWN(mp)); 264830f712c9SDave Chinner xfs_perag_put(pag); 264930f712c9SDave Chinner return 0; 265030f712c9SDave Chinner } 265130f712c9SDave Chinner 265230f712c9SDave Chinner /* 265330f712c9SDave Chinner * Read in the agi to initialise the per-ag data in the mount structure 265430f712c9SDave Chinner */ 265530f712c9SDave Chinner int 265630f712c9SDave Chinner xfs_ialloc_pagi_init( 265730f712c9SDave Chinner xfs_mount_t *mp, /* file system mount structure */ 265830f712c9SDave Chinner xfs_trans_t *tp, /* transaction pointer */ 265930f712c9SDave Chinner xfs_agnumber_t agno) /* allocation group number */ 266030f712c9SDave Chinner { 266130f712c9SDave Chinner xfs_buf_t *bp = NULL; 266230f712c9SDave Chinner int error; 266330f712c9SDave Chinner 266430f712c9SDave Chinner error = xfs_ialloc_read_agi(mp, tp, agno, &bp); 266530f712c9SDave Chinner if (error) 266630f712c9SDave Chinner return error; 266730f712c9SDave Chinner if (bp) 266830f712c9SDave Chinner xfs_trans_brelse(tp, bp); 266930f712c9SDave Chinner return 0; 267030f712c9SDave Chinner } 2671