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" 2730f712c9SDave Chinner #include "xfs_inode.h" 2830f712c9SDave Chinner #include "xfs_btree.h" 2930f712c9SDave Chinner #include "xfs_ialloc.h" 3030f712c9SDave Chinner #include "xfs_ialloc_btree.h" 3130f712c9SDave Chinner #include "xfs_alloc.h" 3230f712c9SDave Chinner #include "xfs_rtalloc.h" 3330f712c9SDave Chinner #include "xfs_error.h" 3430f712c9SDave Chinner #include "xfs_bmap.h" 3530f712c9SDave Chinner #include "xfs_cksum.h" 3630f712c9SDave Chinner #include "xfs_trans.h" 3730f712c9SDave Chinner #include "xfs_buf_item.h" 3830f712c9SDave Chinner #include "xfs_icreate_item.h" 3930f712c9SDave Chinner #include "xfs_icache.h" 4030f712c9SDave Chinner #include "xfs_trace.h" 4130f712c9SDave Chinner 4230f712c9SDave Chinner 4330f712c9SDave Chinner /* 4430f712c9SDave Chinner * Allocation group level functions. 4530f712c9SDave Chinner */ 4630f712c9SDave Chinner static inline int 4730f712c9SDave Chinner xfs_ialloc_cluster_alignment( 487a1df156SDave Chinner struct xfs_mount *mp) 4930f712c9SDave Chinner { 507a1df156SDave Chinner if (xfs_sb_version_hasalign(&mp->m_sb) && 517a1df156SDave Chinner mp->m_sb.sb_inoalignmt >= 527a1df156SDave Chinner XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size)) 537a1df156SDave Chinner return mp->m_sb.sb_inoalignmt; 5430f712c9SDave Chinner return 1; 5530f712c9SDave Chinner } 5630f712c9SDave Chinner 5730f712c9SDave Chinner /* 5830f712c9SDave Chinner * Lookup a record by ino in the btree given by cur. 5930f712c9SDave Chinner */ 6030f712c9SDave Chinner int /* error */ 6130f712c9SDave Chinner xfs_inobt_lookup( 6230f712c9SDave Chinner struct xfs_btree_cur *cur, /* btree cursor */ 6330f712c9SDave Chinner xfs_agino_t ino, /* starting inode of chunk */ 6430f712c9SDave Chinner xfs_lookup_t dir, /* <=, >=, == */ 6530f712c9SDave Chinner int *stat) /* success/failure */ 6630f712c9SDave Chinner { 6730f712c9SDave Chinner cur->bc_rec.i.ir_startino = ino; 685419040fSBrian Foster cur->bc_rec.i.ir_holemask = 0; 695419040fSBrian Foster cur->bc_rec.i.ir_count = 0; 7030f712c9SDave Chinner cur->bc_rec.i.ir_freecount = 0; 7130f712c9SDave Chinner cur->bc_rec.i.ir_free = 0; 7230f712c9SDave Chinner return xfs_btree_lookup(cur, dir, stat); 7330f712c9SDave Chinner } 7430f712c9SDave Chinner 7530f712c9SDave Chinner /* 7630f712c9SDave Chinner * Update the record referred to by cur to the value given. 7730f712c9SDave Chinner * This either works (return 0) or gets an EFSCORRUPTED error. 7830f712c9SDave Chinner */ 7930f712c9SDave Chinner STATIC int /* error */ 8030f712c9SDave Chinner xfs_inobt_update( 8130f712c9SDave Chinner struct xfs_btree_cur *cur, /* btree cursor */ 8230f712c9SDave Chinner xfs_inobt_rec_incore_t *irec) /* btree record */ 8330f712c9SDave Chinner { 8430f712c9SDave Chinner union xfs_btree_rec rec; 8530f712c9SDave Chinner 8630f712c9SDave Chinner rec.inobt.ir_startino = cpu_to_be32(irec->ir_startino); 875419040fSBrian Foster if (xfs_sb_version_hassparseinodes(&cur->bc_mp->m_sb)) { 885419040fSBrian Foster rec.inobt.ir_u.sp.ir_holemask = cpu_to_be16(irec->ir_holemask); 895419040fSBrian Foster rec.inobt.ir_u.sp.ir_count = irec->ir_count; 905419040fSBrian Foster rec.inobt.ir_u.sp.ir_freecount = irec->ir_freecount; 915419040fSBrian Foster } else { 925419040fSBrian Foster /* ir_holemask/ir_count not supported on-disk */ 935419040fSBrian Foster rec.inobt.ir_u.f.ir_freecount = cpu_to_be32(irec->ir_freecount); 945419040fSBrian Foster } 9530f712c9SDave Chinner rec.inobt.ir_free = cpu_to_be64(irec->ir_free); 9630f712c9SDave Chinner return xfs_btree_update(cur, &rec); 9730f712c9SDave Chinner } 9830f712c9SDave Chinner 9930f712c9SDave Chinner /* 10030f712c9SDave Chinner * Get the data from the pointed-to record. 10130f712c9SDave Chinner */ 10230f712c9SDave Chinner int /* error */ 10330f712c9SDave Chinner xfs_inobt_get_rec( 10430f712c9SDave Chinner struct xfs_btree_cur *cur, /* btree cursor */ 10530f712c9SDave Chinner xfs_inobt_rec_incore_t *irec, /* btree record */ 10630f712c9SDave Chinner int *stat) /* output: success/failure */ 10730f712c9SDave Chinner { 10830f712c9SDave Chinner union xfs_btree_rec *rec; 10930f712c9SDave Chinner int error; 11030f712c9SDave Chinner 11130f712c9SDave Chinner error = xfs_btree_get_rec(cur, &rec, stat); 1125419040fSBrian Foster if (error || *stat == 0) 11330f712c9SDave Chinner return error; 1145419040fSBrian Foster 1155419040fSBrian Foster irec->ir_startino = be32_to_cpu(rec->inobt.ir_startino); 1165419040fSBrian Foster if (xfs_sb_version_hassparseinodes(&cur->bc_mp->m_sb)) { 1175419040fSBrian Foster irec->ir_holemask = be16_to_cpu(rec->inobt.ir_u.sp.ir_holemask); 1185419040fSBrian Foster irec->ir_count = rec->inobt.ir_u.sp.ir_count; 1195419040fSBrian Foster irec->ir_freecount = rec->inobt.ir_u.sp.ir_freecount; 1205419040fSBrian Foster } else { 1215419040fSBrian Foster /* 1225419040fSBrian Foster * ir_holemask/ir_count not supported on-disk. Fill in hardcoded 1235419040fSBrian Foster * values for full inode chunks. 1245419040fSBrian Foster */ 1255419040fSBrian Foster irec->ir_holemask = XFS_INOBT_HOLEMASK_FULL; 1265419040fSBrian Foster irec->ir_count = XFS_INODES_PER_CHUNK; 1275419040fSBrian Foster irec->ir_freecount = 1285419040fSBrian Foster be32_to_cpu(rec->inobt.ir_u.f.ir_freecount); 1295419040fSBrian Foster } 1305419040fSBrian Foster irec->ir_free = be64_to_cpu(rec->inobt.ir_free); 1315419040fSBrian Foster 1325419040fSBrian Foster return 0; 13330f712c9SDave Chinner } 13430f712c9SDave Chinner 13530f712c9SDave Chinner /* 13630f712c9SDave Chinner * Insert a single inobt record. Cursor must already point to desired location. 13730f712c9SDave Chinner */ 13830f712c9SDave Chinner STATIC int 13930f712c9SDave Chinner xfs_inobt_insert_rec( 14030f712c9SDave Chinner struct xfs_btree_cur *cur, 1415419040fSBrian Foster __uint16_t holemask, 1425419040fSBrian Foster __uint8_t count, 14330f712c9SDave Chinner __int32_t freecount, 14430f712c9SDave Chinner xfs_inofree_t free, 14530f712c9SDave Chinner int *stat) 14630f712c9SDave Chinner { 1475419040fSBrian Foster cur->bc_rec.i.ir_holemask = holemask; 1485419040fSBrian Foster cur->bc_rec.i.ir_count = count; 14930f712c9SDave Chinner cur->bc_rec.i.ir_freecount = freecount; 15030f712c9SDave Chinner cur->bc_rec.i.ir_free = free; 15130f712c9SDave Chinner return xfs_btree_insert(cur, stat); 15230f712c9SDave Chinner } 15330f712c9SDave Chinner 15430f712c9SDave Chinner /* 15530f712c9SDave Chinner * Insert records describing a newly allocated inode chunk into the inobt. 15630f712c9SDave Chinner */ 15730f712c9SDave Chinner STATIC int 15830f712c9SDave Chinner xfs_inobt_insert( 15930f712c9SDave Chinner struct xfs_mount *mp, 16030f712c9SDave Chinner struct xfs_trans *tp, 16130f712c9SDave Chinner struct xfs_buf *agbp, 16230f712c9SDave Chinner xfs_agino_t newino, 16330f712c9SDave Chinner xfs_agino_t newlen, 16430f712c9SDave Chinner xfs_btnum_t btnum) 16530f712c9SDave Chinner { 16630f712c9SDave Chinner struct xfs_btree_cur *cur; 16730f712c9SDave Chinner struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp); 16830f712c9SDave Chinner xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno); 16930f712c9SDave Chinner xfs_agino_t thisino; 17030f712c9SDave Chinner int i; 17130f712c9SDave Chinner int error; 17230f712c9SDave Chinner 17330f712c9SDave Chinner cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, btnum); 17430f712c9SDave Chinner 17530f712c9SDave Chinner for (thisino = newino; 17630f712c9SDave Chinner thisino < newino + newlen; 17730f712c9SDave Chinner thisino += XFS_INODES_PER_CHUNK) { 17830f712c9SDave Chinner error = xfs_inobt_lookup(cur, thisino, XFS_LOOKUP_EQ, &i); 17930f712c9SDave Chinner if (error) { 18030f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); 18130f712c9SDave Chinner return error; 18230f712c9SDave Chinner } 18330f712c9SDave Chinner ASSERT(i == 0); 18430f712c9SDave Chinner 1855419040fSBrian Foster error = xfs_inobt_insert_rec(cur, XFS_INOBT_HOLEMASK_FULL, 1865419040fSBrian Foster XFS_INODES_PER_CHUNK, 1875419040fSBrian Foster XFS_INODES_PER_CHUNK, 18830f712c9SDave Chinner XFS_INOBT_ALL_FREE, &i); 18930f712c9SDave Chinner if (error) { 19030f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); 19130f712c9SDave Chinner return error; 19230f712c9SDave Chinner } 19330f712c9SDave Chinner ASSERT(i == 1); 19430f712c9SDave Chinner } 19530f712c9SDave Chinner 19630f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); 19730f712c9SDave Chinner 19830f712c9SDave Chinner return 0; 19930f712c9SDave Chinner } 20030f712c9SDave Chinner 20130f712c9SDave Chinner /* 20230f712c9SDave Chinner * Verify that the number of free inodes in the AGI is correct. 20330f712c9SDave Chinner */ 20430f712c9SDave Chinner #ifdef DEBUG 20530f712c9SDave Chinner STATIC int 20630f712c9SDave Chinner xfs_check_agi_freecount( 20730f712c9SDave Chinner struct xfs_btree_cur *cur, 20830f712c9SDave Chinner struct xfs_agi *agi) 20930f712c9SDave Chinner { 21030f712c9SDave Chinner if (cur->bc_nlevels == 1) { 21130f712c9SDave Chinner xfs_inobt_rec_incore_t rec; 21230f712c9SDave Chinner int freecount = 0; 21330f712c9SDave Chinner int error; 21430f712c9SDave Chinner int i; 21530f712c9SDave Chinner 21630f712c9SDave Chinner error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i); 21730f712c9SDave Chinner if (error) 21830f712c9SDave Chinner return error; 21930f712c9SDave Chinner 22030f712c9SDave Chinner do { 22130f712c9SDave Chinner error = xfs_inobt_get_rec(cur, &rec, &i); 22230f712c9SDave Chinner if (error) 22330f712c9SDave Chinner return error; 22430f712c9SDave Chinner 22530f712c9SDave Chinner if (i) { 22630f712c9SDave Chinner freecount += rec.ir_freecount; 22730f712c9SDave Chinner error = xfs_btree_increment(cur, 0, &i); 22830f712c9SDave Chinner if (error) 22930f712c9SDave Chinner return error; 23030f712c9SDave Chinner } 23130f712c9SDave Chinner } while (i == 1); 23230f712c9SDave Chinner 23330f712c9SDave Chinner if (!XFS_FORCED_SHUTDOWN(cur->bc_mp)) 23430f712c9SDave Chinner ASSERT(freecount == be32_to_cpu(agi->agi_freecount)); 23530f712c9SDave Chinner } 23630f712c9SDave Chinner return 0; 23730f712c9SDave Chinner } 23830f712c9SDave Chinner #else 23930f712c9SDave Chinner #define xfs_check_agi_freecount(cur, agi) 0 24030f712c9SDave Chinner #endif 24130f712c9SDave Chinner 24230f712c9SDave Chinner /* 24330f712c9SDave Chinner * Initialise a new set of inodes. When called without a transaction context 24430f712c9SDave Chinner * (e.g. from recovery) we initiate a delayed write of the inode buffers rather 24530f712c9SDave Chinner * than logging them (which in a transaction context puts them into the AIL 24630f712c9SDave Chinner * for writeback rather than the xfsbufd queue). 24730f712c9SDave Chinner */ 24830f712c9SDave Chinner int 24930f712c9SDave Chinner xfs_ialloc_inode_init( 25030f712c9SDave Chinner struct xfs_mount *mp, 25130f712c9SDave Chinner struct xfs_trans *tp, 25230f712c9SDave Chinner struct list_head *buffer_list, 253463958afSBrian Foster int icount, 25430f712c9SDave Chinner xfs_agnumber_t agno, 25530f712c9SDave Chinner xfs_agblock_t agbno, 25630f712c9SDave Chinner xfs_agblock_t length, 25730f712c9SDave Chinner unsigned int gen) 25830f712c9SDave Chinner { 25930f712c9SDave Chinner struct xfs_buf *fbuf; 26030f712c9SDave Chinner struct xfs_dinode *free; 26130f712c9SDave Chinner int nbufs, blks_per_cluster, inodes_per_cluster; 26230f712c9SDave Chinner int version; 26330f712c9SDave Chinner int i, j; 26430f712c9SDave Chinner xfs_daddr_t d; 26530f712c9SDave Chinner xfs_ino_t ino = 0; 26630f712c9SDave Chinner 26730f712c9SDave Chinner /* 26830f712c9SDave Chinner * Loop over the new block(s), filling in the inodes. For small block 26930f712c9SDave Chinner * sizes, manipulate the inodes in buffers which are multiples of the 27030f712c9SDave Chinner * blocks size. 27130f712c9SDave Chinner */ 27230f712c9SDave Chinner blks_per_cluster = xfs_icluster_size_fsb(mp); 27330f712c9SDave Chinner inodes_per_cluster = blks_per_cluster << mp->m_sb.sb_inopblog; 27430f712c9SDave Chinner nbufs = length / blks_per_cluster; 27530f712c9SDave Chinner 27630f712c9SDave Chinner /* 27730f712c9SDave Chinner * Figure out what version number to use in the inodes we create. If 27830f712c9SDave Chinner * the superblock version has caught up to the one that supports the new 27930f712c9SDave Chinner * inode format, then use the new inode version. Otherwise use the old 28030f712c9SDave Chinner * version so that old kernels will continue to be able to use the file 28130f712c9SDave Chinner * system. 28230f712c9SDave Chinner * 28330f712c9SDave Chinner * For v3 inodes, we also need to write the inode number into the inode, 28430f712c9SDave Chinner * so calculate the first inode number of the chunk here as 28530f712c9SDave Chinner * XFS_OFFBNO_TO_AGINO() only works within a filesystem block, not 28630f712c9SDave Chinner * across multiple filesystem blocks (such as a cluster) and so cannot 28730f712c9SDave Chinner * be used in the cluster buffer loop below. 28830f712c9SDave Chinner * 28930f712c9SDave Chinner * Further, because we are writing the inode directly into the buffer 29030f712c9SDave Chinner * and calculating a CRC on the entire inode, we have ot log the entire 29130f712c9SDave Chinner * inode so that the entire range the CRC covers is present in the log. 29230f712c9SDave Chinner * That means for v3 inode we log the entire buffer rather than just the 29330f712c9SDave Chinner * inode cores. 29430f712c9SDave Chinner */ 29530f712c9SDave Chinner if (xfs_sb_version_hascrc(&mp->m_sb)) { 29630f712c9SDave Chinner version = 3; 29730f712c9SDave Chinner ino = XFS_AGINO_TO_INO(mp, agno, 29830f712c9SDave Chinner XFS_OFFBNO_TO_AGINO(mp, agbno, 0)); 29930f712c9SDave Chinner 30030f712c9SDave Chinner /* 30130f712c9SDave Chinner * log the initialisation that is about to take place as an 30230f712c9SDave Chinner * logical operation. This means the transaction does not 30330f712c9SDave Chinner * need to log the physical changes to the inode buffers as log 30430f712c9SDave Chinner * recovery will know what initialisation is actually needed. 30530f712c9SDave Chinner * Hence we only need to log the buffers as "ordered" buffers so 30630f712c9SDave Chinner * they track in the AIL as if they were physically logged. 30730f712c9SDave Chinner */ 30830f712c9SDave Chinner if (tp) 309463958afSBrian Foster xfs_icreate_log(tp, agno, agbno, icount, 31030f712c9SDave Chinner mp->m_sb.sb_inodesize, length, gen); 31130f712c9SDave Chinner } else 31230f712c9SDave Chinner version = 2; 31330f712c9SDave Chinner 31430f712c9SDave Chinner for (j = 0; j < nbufs; j++) { 31530f712c9SDave Chinner /* 31630f712c9SDave Chinner * Get the block. 31730f712c9SDave Chinner */ 31830f712c9SDave Chinner d = XFS_AGB_TO_DADDR(mp, agno, agbno + (j * blks_per_cluster)); 31930f712c9SDave Chinner fbuf = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, 32030f712c9SDave Chinner mp->m_bsize * blks_per_cluster, 32130f712c9SDave Chinner XBF_UNMAPPED); 32230f712c9SDave Chinner if (!fbuf) 3232451337dSDave Chinner return -ENOMEM; 32430f712c9SDave Chinner 32530f712c9SDave Chinner /* Initialize the inode buffers and log them appropriately. */ 32630f712c9SDave Chinner fbuf->b_ops = &xfs_inode_buf_ops; 32730f712c9SDave Chinner xfs_buf_zero(fbuf, 0, BBTOB(fbuf->b_length)); 32830f712c9SDave Chinner for (i = 0; i < inodes_per_cluster; i++) { 32930f712c9SDave Chinner int ioffset = i << mp->m_sb.sb_inodelog; 33030f712c9SDave Chinner uint isize = xfs_dinode_size(version); 33130f712c9SDave Chinner 33230f712c9SDave Chinner free = xfs_make_iptr(mp, fbuf, i); 33330f712c9SDave Chinner free->di_magic = cpu_to_be16(XFS_DINODE_MAGIC); 33430f712c9SDave Chinner free->di_version = version; 33530f712c9SDave Chinner free->di_gen = cpu_to_be32(gen); 33630f712c9SDave Chinner free->di_next_unlinked = cpu_to_be32(NULLAGINO); 33730f712c9SDave Chinner 33830f712c9SDave Chinner if (version == 3) { 33930f712c9SDave Chinner free->di_ino = cpu_to_be64(ino); 34030f712c9SDave Chinner ino++; 34130f712c9SDave Chinner uuid_copy(&free->di_uuid, &mp->m_sb.sb_uuid); 34230f712c9SDave Chinner xfs_dinode_calc_crc(mp, free); 34330f712c9SDave Chinner } else if (tp) { 34430f712c9SDave Chinner /* just log the inode core */ 34530f712c9SDave Chinner xfs_trans_log_buf(tp, fbuf, ioffset, 34630f712c9SDave Chinner ioffset + isize - 1); 34730f712c9SDave Chinner } 34830f712c9SDave Chinner } 34930f712c9SDave Chinner 35030f712c9SDave Chinner if (tp) { 35130f712c9SDave Chinner /* 35230f712c9SDave Chinner * Mark the buffer as an inode allocation buffer so it 35330f712c9SDave Chinner * sticks in AIL at the point of this allocation 35430f712c9SDave Chinner * transaction. This ensures the they are on disk before 35530f712c9SDave Chinner * the tail of the log can be moved past this 35630f712c9SDave Chinner * transaction (i.e. by preventing relogging from moving 35730f712c9SDave Chinner * it forward in the log). 35830f712c9SDave Chinner */ 35930f712c9SDave Chinner xfs_trans_inode_alloc_buf(tp, fbuf); 36030f712c9SDave Chinner if (version == 3) { 36130f712c9SDave Chinner /* 36230f712c9SDave Chinner * Mark the buffer as ordered so that they are 36330f712c9SDave Chinner * not physically logged in the transaction but 36430f712c9SDave Chinner * still tracked in the AIL as part of the 36530f712c9SDave Chinner * transaction and pin the log appropriately. 36630f712c9SDave Chinner */ 36730f712c9SDave Chinner xfs_trans_ordered_buf(tp, fbuf); 36830f712c9SDave Chinner xfs_trans_log_buf(tp, fbuf, 0, 36930f712c9SDave Chinner BBTOB(fbuf->b_length) - 1); 37030f712c9SDave Chinner } 37130f712c9SDave Chinner } else { 37230f712c9SDave Chinner fbuf->b_flags |= XBF_DONE; 37330f712c9SDave Chinner xfs_buf_delwri_queue(fbuf, buffer_list); 37430f712c9SDave Chinner xfs_buf_relse(fbuf); 37530f712c9SDave Chinner } 37630f712c9SDave Chinner } 37730f712c9SDave Chinner return 0; 37830f712c9SDave Chinner } 37930f712c9SDave Chinner 38030f712c9SDave Chinner /* 38156d1115cSBrian Foster * Align startino and allocmask for a recently allocated sparse chunk such that 38256d1115cSBrian Foster * they are fit for insertion (or merge) into the on-disk inode btrees. 38356d1115cSBrian Foster * 38456d1115cSBrian Foster * Background: 38556d1115cSBrian Foster * 38656d1115cSBrian Foster * When enabled, sparse inode support increases the inode alignment from cluster 38756d1115cSBrian Foster * size to inode chunk size. This means that the minimum range between two 38856d1115cSBrian Foster * non-adjacent inode records in the inobt is large enough for a full inode 38956d1115cSBrian Foster * record. This allows for cluster sized, cluster aligned block allocation 39056d1115cSBrian Foster * without need to worry about whether the resulting inode record overlaps with 39156d1115cSBrian Foster * another record in the tree. Without this basic rule, we would have to deal 39256d1115cSBrian Foster * with the consequences of overlap by potentially undoing recent allocations in 39356d1115cSBrian Foster * the inode allocation codepath. 39456d1115cSBrian Foster * 39556d1115cSBrian Foster * Because of this alignment rule (which is enforced on mount), there are two 39656d1115cSBrian Foster * inobt possibilities for newly allocated sparse chunks. One is that the 39756d1115cSBrian Foster * aligned inode record for the chunk covers a range of inodes not already 39856d1115cSBrian Foster * covered in the inobt (i.e., it is safe to insert a new sparse record). The 39956d1115cSBrian Foster * other is that a record already exists at the aligned startino that considers 40056d1115cSBrian Foster * the newly allocated range as sparse. In the latter case, record content is 40156d1115cSBrian Foster * merged in hope that sparse inode chunks fill to full chunks over time. 40256d1115cSBrian Foster */ 40356d1115cSBrian Foster STATIC void 40456d1115cSBrian Foster xfs_align_sparse_ino( 40556d1115cSBrian Foster struct xfs_mount *mp, 40656d1115cSBrian Foster xfs_agino_t *startino, 40756d1115cSBrian Foster uint16_t *allocmask) 40856d1115cSBrian Foster { 40956d1115cSBrian Foster xfs_agblock_t agbno; 41056d1115cSBrian Foster xfs_agblock_t mod; 41156d1115cSBrian Foster int offset; 41256d1115cSBrian Foster 41356d1115cSBrian Foster agbno = XFS_AGINO_TO_AGBNO(mp, *startino); 41456d1115cSBrian Foster mod = agbno % mp->m_sb.sb_inoalignmt; 41556d1115cSBrian Foster if (!mod) 41656d1115cSBrian Foster return; 41756d1115cSBrian Foster 41856d1115cSBrian Foster /* calculate the inode offset and align startino */ 41956d1115cSBrian Foster offset = mod << mp->m_sb.sb_inopblog; 42056d1115cSBrian Foster *startino -= offset; 42156d1115cSBrian Foster 42256d1115cSBrian Foster /* 42356d1115cSBrian Foster * Since startino has been aligned down, left shift allocmask such that 42456d1115cSBrian Foster * it continues to represent the same physical inodes relative to the 42556d1115cSBrian Foster * new startino. 42656d1115cSBrian Foster */ 42756d1115cSBrian Foster *allocmask <<= offset / XFS_INODES_PER_HOLEMASK_BIT; 42856d1115cSBrian Foster } 42956d1115cSBrian Foster 43056d1115cSBrian Foster /* 43156d1115cSBrian Foster * Determine whether the source inode record can merge into the target. Both 43256d1115cSBrian Foster * records must be sparse, the inode ranges must match and there must be no 43356d1115cSBrian Foster * allocation overlap between the records. 43456d1115cSBrian Foster */ 43556d1115cSBrian Foster STATIC bool 43656d1115cSBrian Foster __xfs_inobt_can_merge( 43756d1115cSBrian Foster struct xfs_inobt_rec_incore *trec, /* tgt record */ 43856d1115cSBrian Foster struct xfs_inobt_rec_incore *srec) /* src record */ 43956d1115cSBrian Foster { 44056d1115cSBrian Foster uint64_t talloc; 44156d1115cSBrian Foster uint64_t salloc; 44256d1115cSBrian Foster 44356d1115cSBrian Foster /* records must cover the same inode range */ 44456d1115cSBrian Foster if (trec->ir_startino != srec->ir_startino) 44556d1115cSBrian Foster return false; 44656d1115cSBrian Foster 44756d1115cSBrian Foster /* both records must be sparse */ 44856d1115cSBrian Foster if (!xfs_inobt_issparse(trec->ir_holemask) || 44956d1115cSBrian Foster !xfs_inobt_issparse(srec->ir_holemask)) 45056d1115cSBrian Foster return false; 45156d1115cSBrian Foster 45256d1115cSBrian Foster /* both records must track some inodes */ 45356d1115cSBrian Foster if (!trec->ir_count || !srec->ir_count) 45456d1115cSBrian Foster return false; 45556d1115cSBrian Foster 45656d1115cSBrian Foster /* can't exceed capacity of a full record */ 45756d1115cSBrian Foster if (trec->ir_count + srec->ir_count > XFS_INODES_PER_CHUNK) 45856d1115cSBrian Foster return false; 45956d1115cSBrian Foster 46056d1115cSBrian Foster /* verify there is no allocation overlap */ 46156d1115cSBrian Foster talloc = xfs_inobt_irec_to_allocmask(trec); 46256d1115cSBrian Foster salloc = xfs_inobt_irec_to_allocmask(srec); 46356d1115cSBrian Foster if (talloc & salloc) 46456d1115cSBrian Foster return false; 46556d1115cSBrian Foster 46656d1115cSBrian Foster return true; 46756d1115cSBrian Foster } 46856d1115cSBrian Foster 46956d1115cSBrian Foster /* 47056d1115cSBrian Foster * Merge the source inode record into the target. The caller must call 47156d1115cSBrian Foster * __xfs_inobt_can_merge() to ensure the merge is valid. 47256d1115cSBrian Foster */ 47356d1115cSBrian Foster STATIC void 47456d1115cSBrian Foster __xfs_inobt_rec_merge( 47556d1115cSBrian Foster struct xfs_inobt_rec_incore *trec, /* target */ 47656d1115cSBrian Foster struct xfs_inobt_rec_incore *srec) /* src */ 47756d1115cSBrian Foster { 47856d1115cSBrian Foster ASSERT(trec->ir_startino == srec->ir_startino); 47956d1115cSBrian Foster 48056d1115cSBrian Foster /* combine the counts */ 48156d1115cSBrian Foster trec->ir_count += srec->ir_count; 48256d1115cSBrian Foster trec->ir_freecount += srec->ir_freecount; 48356d1115cSBrian Foster 48456d1115cSBrian Foster /* 48556d1115cSBrian Foster * Merge the holemask and free mask. For both fields, 0 bits refer to 48656d1115cSBrian Foster * allocated inodes. We combine the allocated ranges with bitwise AND. 48756d1115cSBrian Foster */ 48856d1115cSBrian Foster trec->ir_holemask &= srec->ir_holemask; 48956d1115cSBrian Foster trec->ir_free &= srec->ir_free; 49056d1115cSBrian Foster } 49156d1115cSBrian Foster 49256d1115cSBrian Foster /* 49356d1115cSBrian Foster * Insert a new sparse inode chunk into the associated inode btree. The inode 49456d1115cSBrian Foster * record for the sparse chunk is pre-aligned to a startino that should match 49556d1115cSBrian Foster * any pre-existing sparse inode record in the tree. This allows sparse chunks 49656d1115cSBrian Foster * to fill over time. 49756d1115cSBrian Foster * 49856d1115cSBrian Foster * This function supports two modes of handling preexisting records depending on 49956d1115cSBrian Foster * the merge flag. If merge is true, the provided record is merged with the 50056d1115cSBrian Foster * existing record and updated in place. The merged record is returned in nrec. 50156d1115cSBrian Foster * If merge is false, an existing record is replaced with the provided record. 50256d1115cSBrian Foster * If no preexisting record exists, the provided record is always inserted. 50356d1115cSBrian Foster * 50456d1115cSBrian Foster * It is considered corruption if a merge is requested and not possible. Given 50556d1115cSBrian Foster * the sparse inode alignment constraints, this should never happen. 50656d1115cSBrian Foster */ 50756d1115cSBrian Foster STATIC int 50856d1115cSBrian Foster xfs_inobt_insert_sprec( 50956d1115cSBrian Foster struct xfs_mount *mp, 51056d1115cSBrian Foster struct xfs_trans *tp, 51156d1115cSBrian Foster struct xfs_buf *agbp, 51256d1115cSBrian Foster int btnum, 51356d1115cSBrian Foster struct xfs_inobt_rec_incore *nrec, /* in/out: new/merged rec. */ 51456d1115cSBrian Foster bool merge) /* merge or replace */ 51556d1115cSBrian Foster { 51656d1115cSBrian Foster struct xfs_btree_cur *cur; 51756d1115cSBrian Foster struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp); 51856d1115cSBrian Foster xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno); 51956d1115cSBrian Foster int error; 52056d1115cSBrian Foster int i; 52156d1115cSBrian Foster struct xfs_inobt_rec_incore rec; 52256d1115cSBrian Foster 52356d1115cSBrian Foster cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, btnum); 52456d1115cSBrian Foster 52556d1115cSBrian Foster /* the new record is pre-aligned so we know where to look */ 52656d1115cSBrian Foster error = xfs_inobt_lookup(cur, nrec->ir_startino, XFS_LOOKUP_EQ, &i); 52756d1115cSBrian Foster if (error) 52856d1115cSBrian Foster goto error; 52956d1115cSBrian Foster /* if nothing there, insert a new record and return */ 53056d1115cSBrian Foster if (i == 0) { 53156d1115cSBrian Foster error = xfs_inobt_insert_rec(cur, nrec->ir_holemask, 53256d1115cSBrian Foster nrec->ir_count, nrec->ir_freecount, 53356d1115cSBrian Foster nrec->ir_free, &i); 53456d1115cSBrian Foster if (error) 53556d1115cSBrian Foster goto error; 53656d1115cSBrian Foster XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error); 53756d1115cSBrian Foster 53856d1115cSBrian Foster goto out; 53956d1115cSBrian Foster } 54056d1115cSBrian Foster 54156d1115cSBrian Foster /* 54256d1115cSBrian Foster * A record exists at this startino. Merge or replace the record 54356d1115cSBrian Foster * depending on what we've been asked to do. 54456d1115cSBrian Foster */ 54556d1115cSBrian Foster if (merge) { 54656d1115cSBrian Foster error = xfs_inobt_get_rec(cur, &rec, &i); 54756d1115cSBrian Foster if (error) 54856d1115cSBrian Foster goto error; 54956d1115cSBrian Foster XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error); 55056d1115cSBrian Foster XFS_WANT_CORRUPTED_GOTO(mp, 55156d1115cSBrian Foster rec.ir_startino == nrec->ir_startino, 55256d1115cSBrian Foster error); 55356d1115cSBrian Foster 55456d1115cSBrian Foster /* 55556d1115cSBrian Foster * This should never fail. If we have coexisting records that 55656d1115cSBrian Foster * cannot merge, something is seriously wrong. 55756d1115cSBrian Foster */ 55856d1115cSBrian Foster XFS_WANT_CORRUPTED_GOTO(mp, __xfs_inobt_can_merge(nrec, &rec), 55956d1115cSBrian Foster error); 56056d1115cSBrian Foster 56156d1115cSBrian Foster trace_xfs_irec_merge_pre(mp, agno, rec.ir_startino, 56256d1115cSBrian Foster rec.ir_holemask, nrec->ir_startino, 56356d1115cSBrian Foster nrec->ir_holemask); 56456d1115cSBrian Foster 56556d1115cSBrian Foster /* merge to nrec to output the updated record */ 56656d1115cSBrian Foster __xfs_inobt_rec_merge(nrec, &rec); 56756d1115cSBrian Foster 56856d1115cSBrian Foster trace_xfs_irec_merge_post(mp, agno, nrec->ir_startino, 56956d1115cSBrian Foster nrec->ir_holemask); 57056d1115cSBrian Foster 57156d1115cSBrian Foster error = xfs_inobt_rec_check_count(mp, nrec); 57256d1115cSBrian Foster if (error) 57356d1115cSBrian Foster goto error; 57456d1115cSBrian Foster } 57556d1115cSBrian Foster 57656d1115cSBrian Foster error = xfs_inobt_update(cur, nrec); 57756d1115cSBrian Foster if (error) 57856d1115cSBrian Foster goto error; 57956d1115cSBrian Foster 58056d1115cSBrian Foster out: 58156d1115cSBrian Foster xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); 58256d1115cSBrian Foster return 0; 58356d1115cSBrian Foster error: 58456d1115cSBrian Foster xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); 58556d1115cSBrian Foster return error; 58656d1115cSBrian Foster } 58756d1115cSBrian Foster 58856d1115cSBrian Foster /* 58930f712c9SDave Chinner * Allocate new inodes in the allocation group specified by agbp. 59030f712c9SDave Chinner * Return 0 for success, else error code. 59130f712c9SDave Chinner */ 59230f712c9SDave Chinner STATIC int /* error code or 0 */ 59330f712c9SDave Chinner xfs_ialloc_ag_alloc( 59430f712c9SDave Chinner xfs_trans_t *tp, /* transaction pointer */ 59530f712c9SDave Chinner xfs_buf_t *agbp, /* alloc group buffer */ 59630f712c9SDave Chinner int *alloc) 59730f712c9SDave Chinner { 59830f712c9SDave Chinner xfs_agi_t *agi; /* allocation group header */ 59930f712c9SDave Chinner xfs_alloc_arg_t args; /* allocation argument structure */ 60030f712c9SDave Chinner xfs_agnumber_t agno; 60130f712c9SDave Chinner int error; 60230f712c9SDave Chinner xfs_agino_t newino; /* new first inode's number */ 60330f712c9SDave Chinner xfs_agino_t newlen; /* new number of inodes */ 60430f712c9SDave Chinner int isaligned = 0; /* inode allocation at stripe unit */ 60530f712c9SDave Chinner /* boundary */ 60656d1115cSBrian Foster uint16_t allocmask = (uint16_t) -1; /* init. to full chunk */ 60756d1115cSBrian Foster struct xfs_inobt_rec_incore rec; 60830f712c9SDave Chinner struct xfs_perag *pag; 6091cdadee1SBrian Foster int do_sparse = 0; 6101cdadee1SBrian Foster 61130f712c9SDave Chinner memset(&args, 0, sizeof(args)); 61230f712c9SDave Chinner args.tp = tp; 61330f712c9SDave Chinner args.mp = tp->t_mountp; 6141cdadee1SBrian Foster args.fsbno = NULLFSBLOCK; 61530f712c9SDave Chinner 61646fc58daSBrian Foster #ifdef DEBUG 61746fc58daSBrian Foster /* randomly do sparse inode allocations */ 61846fc58daSBrian Foster if (xfs_sb_version_hassparseinodes(&tp->t_mountp->m_sb) && 61946fc58daSBrian Foster args.mp->m_ialloc_min_blks < args.mp->m_ialloc_blks) 62046fc58daSBrian Foster do_sparse = prandom_u32() & 1; 62146fc58daSBrian Foster #endif 62246fc58daSBrian Foster 62330f712c9SDave Chinner /* 62430f712c9SDave Chinner * Locking will ensure that we don't have two callers in here 62530f712c9SDave Chinner * at one time. 62630f712c9SDave Chinner */ 62730f712c9SDave Chinner newlen = args.mp->m_ialloc_inos; 62830f712c9SDave Chinner if (args.mp->m_maxicount && 62974f9ce1cSGeorge Wang percpu_counter_read_positive(&args.mp->m_icount) + newlen > 630501ab323SDave Chinner args.mp->m_maxicount) 6312451337dSDave Chinner return -ENOSPC; 63230f712c9SDave Chinner args.minlen = args.maxlen = args.mp->m_ialloc_blks; 63330f712c9SDave Chinner /* 63430f712c9SDave Chinner * First try to allocate inodes contiguous with the last-allocated 63530f712c9SDave Chinner * chunk of inodes. If the filesystem is striped, this will fill 63630f712c9SDave Chinner * an entire stripe unit with inodes. 63730f712c9SDave Chinner */ 63830f712c9SDave Chinner agi = XFS_BUF_TO_AGI(agbp); 63930f712c9SDave Chinner newino = be32_to_cpu(agi->agi_newino); 64030f712c9SDave Chinner agno = be32_to_cpu(agi->agi_seqno); 64130f712c9SDave Chinner args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) + 64230f712c9SDave Chinner args.mp->m_ialloc_blks; 6431cdadee1SBrian Foster if (do_sparse) 6441cdadee1SBrian Foster goto sparse_alloc; 64530f712c9SDave Chinner if (likely(newino != NULLAGINO && 64630f712c9SDave Chinner (args.agbno < be32_to_cpu(agi->agi_length)))) { 64730f712c9SDave Chinner args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno); 64830f712c9SDave Chinner args.type = XFS_ALLOCTYPE_THIS_BNO; 64930f712c9SDave Chinner args.prod = 1; 65030f712c9SDave Chinner 65130f712c9SDave Chinner /* 65230f712c9SDave Chinner * We need to take into account alignment here to ensure that 65330f712c9SDave Chinner * we don't modify the free list if we fail to have an exact 65430f712c9SDave Chinner * block. If we don't have an exact match, and every oher 65530f712c9SDave Chinner * attempt allocation attempt fails, we'll end up cancelling 65630f712c9SDave Chinner * a dirty transaction and shutting down. 65730f712c9SDave Chinner * 65830f712c9SDave Chinner * For an exact allocation, alignment must be 1, 65930f712c9SDave Chinner * however we need to take cluster alignment into account when 66030f712c9SDave Chinner * fixing up the freelist. Use the minalignslop field to 66130f712c9SDave Chinner * indicate that extra blocks might be required for alignment, 66230f712c9SDave Chinner * but not to use them in the actual exact allocation. 66330f712c9SDave Chinner */ 66430f712c9SDave Chinner args.alignment = 1; 6657a1df156SDave Chinner args.minalignslop = xfs_ialloc_cluster_alignment(args.mp) - 1; 66630f712c9SDave Chinner 66730f712c9SDave Chinner /* Allow space for the inode btree to split. */ 66830f712c9SDave Chinner args.minleft = args.mp->m_in_maxlevels - 1; 66930f712c9SDave Chinner if ((error = xfs_alloc_vextent(&args))) 67030f712c9SDave Chinner return error; 67130f712c9SDave Chinner 67230f712c9SDave Chinner /* 67330f712c9SDave Chinner * This request might have dirtied the transaction if the AG can 67430f712c9SDave Chinner * satisfy the request, but the exact block was not available. 67530f712c9SDave Chinner * If the allocation did fail, subsequent requests will relax 67630f712c9SDave Chinner * the exact agbno requirement and increase the alignment 67730f712c9SDave Chinner * instead. It is critical that the total size of the request 67830f712c9SDave Chinner * (len + alignment + slop) does not increase from this point 67930f712c9SDave Chinner * on, so reset minalignslop to ensure it is not included in 68030f712c9SDave Chinner * subsequent requests. 68130f712c9SDave Chinner */ 68230f712c9SDave Chinner args.minalignslop = 0; 6831cdadee1SBrian Foster } 68430f712c9SDave Chinner 68530f712c9SDave Chinner if (unlikely(args.fsbno == NULLFSBLOCK)) { 68630f712c9SDave Chinner /* 68730f712c9SDave Chinner * Set the alignment for the allocation. 68830f712c9SDave Chinner * If stripe alignment is turned on then align at stripe unit 68930f712c9SDave Chinner * boundary. 69030f712c9SDave Chinner * If the cluster size is smaller than a filesystem block 69130f712c9SDave Chinner * then we're doing I/O for inodes in filesystem block size 69230f712c9SDave Chinner * pieces, so don't need alignment anyway. 69330f712c9SDave Chinner */ 69430f712c9SDave Chinner isaligned = 0; 69530f712c9SDave Chinner if (args.mp->m_sinoalign) { 69630f712c9SDave Chinner ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN)); 69730f712c9SDave Chinner args.alignment = args.mp->m_dalign; 69830f712c9SDave Chinner isaligned = 1; 69930f712c9SDave Chinner } else 7007a1df156SDave Chinner args.alignment = xfs_ialloc_cluster_alignment(args.mp); 70130f712c9SDave Chinner /* 70230f712c9SDave Chinner * Need to figure out where to allocate the inode blocks. 70330f712c9SDave Chinner * Ideally they should be spaced out through the a.g. 70430f712c9SDave Chinner * For now, just allocate blocks up front. 70530f712c9SDave Chinner */ 70630f712c9SDave Chinner args.agbno = be32_to_cpu(agi->agi_root); 70730f712c9SDave Chinner args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno); 70830f712c9SDave Chinner /* 70930f712c9SDave Chinner * Allocate a fixed-size extent of inodes. 71030f712c9SDave Chinner */ 71130f712c9SDave Chinner args.type = XFS_ALLOCTYPE_NEAR_BNO; 71230f712c9SDave Chinner args.prod = 1; 71330f712c9SDave Chinner /* 71430f712c9SDave Chinner * Allow space for the inode btree to split. 71530f712c9SDave Chinner */ 71630f712c9SDave Chinner args.minleft = args.mp->m_in_maxlevels - 1; 71730f712c9SDave Chinner if ((error = xfs_alloc_vextent(&args))) 71830f712c9SDave Chinner return error; 71930f712c9SDave Chinner } 72030f712c9SDave Chinner 72130f712c9SDave Chinner /* 72230f712c9SDave Chinner * If stripe alignment is turned on, then try again with cluster 72330f712c9SDave Chinner * alignment. 72430f712c9SDave Chinner */ 72530f712c9SDave Chinner if (isaligned && args.fsbno == NULLFSBLOCK) { 72630f712c9SDave Chinner args.type = XFS_ALLOCTYPE_NEAR_BNO; 72730f712c9SDave Chinner args.agbno = be32_to_cpu(agi->agi_root); 72830f712c9SDave Chinner args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno); 7297a1df156SDave Chinner args.alignment = xfs_ialloc_cluster_alignment(args.mp); 73030f712c9SDave Chinner if ((error = xfs_alloc_vextent(&args))) 73130f712c9SDave Chinner return error; 73230f712c9SDave Chinner } 73330f712c9SDave Chinner 73456d1115cSBrian Foster /* 73556d1115cSBrian Foster * Finally, try a sparse allocation if the filesystem supports it and 73656d1115cSBrian Foster * the sparse allocation length is smaller than a full chunk. 73756d1115cSBrian Foster */ 73856d1115cSBrian Foster if (xfs_sb_version_hassparseinodes(&args.mp->m_sb) && 73956d1115cSBrian Foster args.mp->m_ialloc_min_blks < args.mp->m_ialloc_blks && 74056d1115cSBrian Foster args.fsbno == NULLFSBLOCK) { 7411cdadee1SBrian Foster sparse_alloc: 74256d1115cSBrian Foster args.type = XFS_ALLOCTYPE_NEAR_BNO; 74356d1115cSBrian Foster args.agbno = be32_to_cpu(agi->agi_root); 74456d1115cSBrian Foster args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno); 74556d1115cSBrian Foster args.alignment = args.mp->m_sb.sb_spino_align; 74656d1115cSBrian Foster args.prod = 1; 74756d1115cSBrian Foster 74856d1115cSBrian Foster args.minlen = args.mp->m_ialloc_min_blks; 74956d1115cSBrian Foster args.maxlen = args.minlen; 75056d1115cSBrian Foster 75156d1115cSBrian Foster /* 75256d1115cSBrian Foster * The inode record will be aligned to full chunk size. We must 75356d1115cSBrian Foster * prevent sparse allocation from AG boundaries that result in 75456d1115cSBrian Foster * invalid inode records, such as records that start at agbno 0 75556d1115cSBrian Foster * or extend beyond the AG. 75656d1115cSBrian Foster * 75756d1115cSBrian Foster * Set min agbno to the first aligned, non-zero agbno and max to 75856d1115cSBrian Foster * the last aligned agbno that is at least one full chunk from 75956d1115cSBrian Foster * the end of the AG. 76056d1115cSBrian Foster */ 76156d1115cSBrian Foster args.min_agbno = args.mp->m_sb.sb_inoalignmt; 76256d1115cSBrian Foster args.max_agbno = round_down(args.mp->m_sb.sb_agblocks, 76356d1115cSBrian Foster args.mp->m_sb.sb_inoalignmt) - 76456d1115cSBrian Foster args.mp->m_ialloc_blks; 76556d1115cSBrian Foster 76656d1115cSBrian Foster error = xfs_alloc_vextent(&args); 76756d1115cSBrian Foster if (error) 76856d1115cSBrian Foster return error; 76956d1115cSBrian Foster 77056d1115cSBrian Foster newlen = args.len << args.mp->m_sb.sb_inopblog; 77146fc58daSBrian Foster ASSERT(newlen <= XFS_INODES_PER_CHUNK); 77256d1115cSBrian Foster allocmask = (1 << (newlen / XFS_INODES_PER_HOLEMASK_BIT)) - 1; 77356d1115cSBrian Foster } 77456d1115cSBrian Foster 77530f712c9SDave Chinner if (args.fsbno == NULLFSBLOCK) { 77630f712c9SDave Chinner *alloc = 0; 77730f712c9SDave Chinner return 0; 77830f712c9SDave Chinner } 77930f712c9SDave Chinner ASSERT(args.len == args.minlen); 78030f712c9SDave Chinner 78130f712c9SDave Chinner /* 78230f712c9SDave Chinner * Stamp and write the inode buffers. 78330f712c9SDave Chinner * 78430f712c9SDave Chinner * Seed the new inode cluster with a random generation number. This 78530f712c9SDave Chinner * prevents short-term reuse of generation numbers if a chunk is 78630f712c9SDave Chinner * freed and then immediately reallocated. We use random numbers 78730f712c9SDave Chinner * rather than a linear progression to prevent the next generation 78830f712c9SDave Chinner * number from being easily guessable. 78930f712c9SDave Chinner */ 790463958afSBrian Foster error = xfs_ialloc_inode_init(args.mp, tp, NULL, newlen, agno, 791463958afSBrian Foster args.agbno, args.len, prandom_u32()); 79230f712c9SDave Chinner 79330f712c9SDave Chinner if (error) 79430f712c9SDave Chinner return error; 79530f712c9SDave Chinner /* 79630f712c9SDave Chinner * Convert the results. 79730f712c9SDave Chinner */ 79830f712c9SDave Chinner newino = XFS_OFFBNO_TO_AGINO(args.mp, args.agbno, 0); 79956d1115cSBrian Foster 80056d1115cSBrian Foster if (xfs_inobt_issparse(~allocmask)) { 80156d1115cSBrian Foster /* 80256d1115cSBrian Foster * We've allocated a sparse chunk. Align the startino and mask. 80356d1115cSBrian Foster */ 80456d1115cSBrian Foster xfs_align_sparse_ino(args.mp, &newino, &allocmask); 80556d1115cSBrian Foster 80656d1115cSBrian Foster rec.ir_startino = newino; 80756d1115cSBrian Foster rec.ir_holemask = ~allocmask; 80856d1115cSBrian Foster rec.ir_count = newlen; 80956d1115cSBrian Foster rec.ir_freecount = newlen; 81056d1115cSBrian Foster rec.ir_free = XFS_INOBT_ALL_FREE; 81156d1115cSBrian Foster 81256d1115cSBrian Foster /* 81356d1115cSBrian Foster * Insert the sparse record into the inobt and allow for a merge 81456d1115cSBrian Foster * if necessary. If a merge does occur, rec is updated to the 81556d1115cSBrian Foster * merged record. 81656d1115cSBrian Foster */ 81756d1115cSBrian Foster error = xfs_inobt_insert_sprec(args.mp, tp, agbp, XFS_BTNUM_INO, 81856d1115cSBrian Foster &rec, true); 81956d1115cSBrian Foster if (error == -EFSCORRUPTED) { 82056d1115cSBrian Foster xfs_alert(args.mp, 82156d1115cSBrian Foster "invalid sparse inode record: ino 0x%llx holemask 0x%x count %u", 82256d1115cSBrian Foster XFS_AGINO_TO_INO(args.mp, agno, 82356d1115cSBrian Foster rec.ir_startino), 82456d1115cSBrian Foster rec.ir_holemask, rec.ir_count); 82556d1115cSBrian Foster xfs_force_shutdown(args.mp, SHUTDOWN_CORRUPT_INCORE); 82656d1115cSBrian Foster } 82756d1115cSBrian Foster if (error) 82856d1115cSBrian Foster return error; 82956d1115cSBrian Foster 83056d1115cSBrian Foster /* 83156d1115cSBrian Foster * We can't merge the part we've just allocated as for the inobt 83256d1115cSBrian Foster * due to finobt semantics. The original record may or may not 83356d1115cSBrian Foster * exist independent of whether physical inodes exist in this 83456d1115cSBrian Foster * sparse chunk. 83556d1115cSBrian Foster * 83656d1115cSBrian Foster * We must update the finobt record based on the inobt record. 83756d1115cSBrian Foster * rec contains the fully merged and up to date inobt record 83856d1115cSBrian Foster * from the previous call. Set merge false to replace any 83956d1115cSBrian Foster * existing record with this one. 84056d1115cSBrian Foster */ 84156d1115cSBrian Foster if (xfs_sb_version_hasfinobt(&args.mp->m_sb)) { 84256d1115cSBrian Foster error = xfs_inobt_insert_sprec(args.mp, tp, agbp, 84356d1115cSBrian Foster XFS_BTNUM_FINO, &rec, 84456d1115cSBrian Foster false); 84556d1115cSBrian Foster if (error) 84656d1115cSBrian Foster return error; 84756d1115cSBrian Foster } 84856d1115cSBrian Foster } else { 84956d1115cSBrian Foster /* full chunk - insert new records to both btrees */ 85056d1115cSBrian Foster error = xfs_inobt_insert(args.mp, tp, agbp, newino, newlen, 85156d1115cSBrian Foster XFS_BTNUM_INO); 85256d1115cSBrian Foster if (error) 85356d1115cSBrian Foster return error; 85456d1115cSBrian Foster 85556d1115cSBrian Foster if (xfs_sb_version_hasfinobt(&args.mp->m_sb)) { 85656d1115cSBrian Foster error = xfs_inobt_insert(args.mp, tp, agbp, newino, 85756d1115cSBrian Foster newlen, XFS_BTNUM_FINO); 85856d1115cSBrian Foster if (error) 85956d1115cSBrian Foster return error; 86056d1115cSBrian Foster } 86156d1115cSBrian Foster } 86256d1115cSBrian Foster 86356d1115cSBrian Foster /* 86456d1115cSBrian Foster * Update AGI counts and newino. 86556d1115cSBrian Foster */ 86630f712c9SDave Chinner be32_add_cpu(&agi->agi_count, newlen); 86730f712c9SDave Chinner be32_add_cpu(&agi->agi_freecount, newlen); 86830f712c9SDave Chinner pag = xfs_perag_get(args.mp, agno); 86930f712c9SDave Chinner pag->pagi_freecount += newlen; 87030f712c9SDave Chinner xfs_perag_put(pag); 87130f712c9SDave Chinner agi->agi_newino = cpu_to_be32(newino); 87230f712c9SDave Chinner 87330f712c9SDave Chinner /* 87430f712c9SDave Chinner * Log allocation group header fields 87530f712c9SDave Chinner */ 87630f712c9SDave Chinner xfs_ialloc_log_agi(tp, agbp, 87730f712c9SDave Chinner XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO); 87830f712c9SDave Chinner /* 87930f712c9SDave Chinner * Modify/log superblock values for inode count and inode free count. 88030f712c9SDave Chinner */ 88130f712c9SDave Chinner xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen); 88230f712c9SDave Chinner xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen); 88330f712c9SDave Chinner *alloc = 1; 88430f712c9SDave Chinner return 0; 88530f712c9SDave Chinner } 88630f712c9SDave Chinner 88730f712c9SDave Chinner STATIC xfs_agnumber_t 88830f712c9SDave Chinner xfs_ialloc_next_ag( 88930f712c9SDave Chinner xfs_mount_t *mp) 89030f712c9SDave Chinner { 89130f712c9SDave Chinner xfs_agnumber_t agno; 89230f712c9SDave Chinner 89330f712c9SDave Chinner spin_lock(&mp->m_agirotor_lock); 89430f712c9SDave Chinner agno = mp->m_agirotor; 89530f712c9SDave Chinner if (++mp->m_agirotor >= mp->m_maxagi) 89630f712c9SDave Chinner mp->m_agirotor = 0; 89730f712c9SDave Chinner spin_unlock(&mp->m_agirotor_lock); 89830f712c9SDave Chinner 89930f712c9SDave Chinner return agno; 90030f712c9SDave Chinner } 90130f712c9SDave Chinner 90230f712c9SDave Chinner /* 90330f712c9SDave Chinner * Select an allocation group to look for a free inode in, based on the parent 90430f712c9SDave Chinner * inode and the mode. Return the allocation group buffer. 90530f712c9SDave Chinner */ 90630f712c9SDave Chinner STATIC xfs_agnumber_t 90730f712c9SDave Chinner xfs_ialloc_ag_select( 90830f712c9SDave Chinner xfs_trans_t *tp, /* transaction pointer */ 90930f712c9SDave Chinner xfs_ino_t parent, /* parent directory inode number */ 91030f712c9SDave Chinner umode_t mode, /* bits set to indicate file type */ 91130f712c9SDave Chinner int okalloc) /* ok to allocate more space */ 91230f712c9SDave Chinner { 91330f712c9SDave Chinner xfs_agnumber_t agcount; /* number of ag's in the filesystem */ 91430f712c9SDave Chinner xfs_agnumber_t agno; /* current ag number */ 91530f712c9SDave Chinner int flags; /* alloc buffer locking flags */ 91630f712c9SDave Chinner xfs_extlen_t ineed; /* blocks needed for inode allocation */ 91730f712c9SDave Chinner xfs_extlen_t longest = 0; /* longest extent available */ 91830f712c9SDave Chinner xfs_mount_t *mp; /* mount point structure */ 91930f712c9SDave Chinner int needspace; /* file mode implies space allocated */ 92030f712c9SDave Chinner xfs_perag_t *pag; /* per allocation group data */ 92130f712c9SDave Chinner xfs_agnumber_t pagno; /* parent (starting) ag number */ 92230f712c9SDave Chinner int error; 92330f712c9SDave Chinner 92430f712c9SDave Chinner /* 92530f712c9SDave Chinner * Files of these types need at least one block if length > 0 92630f712c9SDave Chinner * (and they won't fit in the inode, but that's hard to figure out). 92730f712c9SDave Chinner */ 92830f712c9SDave Chinner needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode); 92930f712c9SDave Chinner mp = tp->t_mountp; 93030f712c9SDave Chinner agcount = mp->m_maxagi; 93130f712c9SDave Chinner if (S_ISDIR(mode)) 93230f712c9SDave Chinner pagno = xfs_ialloc_next_ag(mp); 93330f712c9SDave Chinner else { 93430f712c9SDave Chinner pagno = XFS_INO_TO_AGNO(mp, parent); 93530f712c9SDave Chinner if (pagno >= agcount) 93630f712c9SDave Chinner pagno = 0; 93730f712c9SDave Chinner } 93830f712c9SDave Chinner 93930f712c9SDave Chinner ASSERT(pagno < agcount); 94030f712c9SDave Chinner 94130f712c9SDave Chinner /* 94230f712c9SDave Chinner * Loop through allocation groups, looking for one with a little 94330f712c9SDave Chinner * free space in it. Note we don't look for free inodes, exactly. 94430f712c9SDave Chinner * Instead, we include whether there is a need to allocate inodes 94530f712c9SDave Chinner * to mean that blocks must be allocated for them, 94630f712c9SDave Chinner * if none are currently free. 94730f712c9SDave Chinner */ 94830f712c9SDave Chinner agno = pagno; 94930f712c9SDave Chinner flags = XFS_ALLOC_FLAG_TRYLOCK; 95030f712c9SDave Chinner for (;;) { 95130f712c9SDave Chinner pag = xfs_perag_get(mp, agno); 95230f712c9SDave Chinner if (!pag->pagi_inodeok) { 95330f712c9SDave Chinner xfs_ialloc_next_ag(mp); 95430f712c9SDave Chinner goto nextag; 95530f712c9SDave Chinner } 95630f712c9SDave Chinner 95730f712c9SDave Chinner if (!pag->pagi_init) { 95830f712c9SDave Chinner error = xfs_ialloc_pagi_init(mp, tp, agno); 95930f712c9SDave Chinner if (error) 96030f712c9SDave Chinner goto nextag; 96130f712c9SDave Chinner } 96230f712c9SDave Chinner 96330f712c9SDave Chinner if (pag->pagi_freecount) { 96430f712c9SDave Chinner xfs_perag_put(pag); 96530f712c9SDave Chinner return agno; 96630f712c9SDave Chinner } 96730f712c9SDave Chinner 96830f712c9SDave Chinner if (!okalloc) 96930f712c9SDave Chinner goto nextag; 97030f712c9SDave Chinner 97130f712c9SDave Chinner if (!pag->pagf_init) { 97230f712c9SDave Chinner error = xfs_alloc_pagf_init(mp, tp, agno, flags); 97330f712c9SDave Chinner if (error) 97430f712c9SDave Chinner goto nextag; 97530f712c9SDave Chinner } 97630f712c9SDave Chinner 97730f712c9SDave Chinner /* 9787a1df156SDave Chinner * Check that there is enough free space for the file plus a 9797a1df156SDave Chinner * chunk of inodes if we need to allocate some. If this is the 9807a1df156SDave Chinner * first pass across the AGs, take into account the potential 9817a1df156SDave Chinner * space needed for alignment of inode chunks when checking the 9827a1df156SDave Chinner * longest contiguous free space in the AG - this prevents us 9837a1df156SDave Chinner * from getting ENOSPC because we have free space larger than 9847a1df156SDave Chinner * m_ialloc_blks but alignment constraints prevent us from using 9857a1df156SDave Chinner * it. 9867a1df156SDave Chinner * 9877a1df156SDave Chinner * If we can't find an AG with space for full alignment slack to 9887a1df156SDave Chinner * be taken into account, we must be near ENOSPC in all AGs. 9897a1df156SDave Chinner * Hence we don't include alignment for the second pass and so 9907a1df156SDave Chinner * if we fail allocation due to alignment issues then it is most 9917a1df156SDave Chinner * likely a real ENOSPC condition. 99230f712c9SDave Chinner */ 993066a1884SBrian Foster ineed = mp->m_ialloc_min_blks; 9947a1df156SDave Chinner if (flags && ineed > 1) 9957a1df156SDave Chinner ineed += xfs_ialloc_cluster_alignment(mp); 99630f712c9SDave Chinner longest = pag->pagf_longest; 99730f712c9SDave Chinner if (!longest) 99830f712c9SDave Chinner longest = pag->pagf_flcount > 0; 99930f712c9SDave Chinner 100030f712c9SDave Chinner if (pag->pagf_freeblks >= needspace + ineed && 100130f712c9SDave Chinner longest >= ineed) { 100230f712c9SDave Chinner xfs_perag_put(pag); 100330f712c9SDave Chinner return agno; 100430f712c9SDave Chinner } 100530f712c9SDave Chinner nextag: 100630f712c9SDave Chinner xfs_perag_put(pag); 100730f712c9SDave Chinner /* 100830f712c9SDave Chinner * No point in iterating over the rest, if we're shutting 100930f712c9SDave Chinner * down. 101030f712c9SDave Chinner */ 101130f712c9SDave Chinner if (XFS_FORCED_SHUTDOWN(mp)) 101230f712c9SDave Chinner return NULLAGNUMBER; 101330f712c9SDave Chinner agno++; 101430f712c9SDave Chinner if (agno >= agcount) 101530f712c9SDave Chinner agno = 0; 101630f712c9SDave Chinner if (agno == pagno) { 101730f712c9SDave Chinner if (flags == 0) 101830f712c9SDave Chinner return NULLAGNUMBER; 101930f712c9SDave Chinner flags = 0; 102030f712c9SDave Chinner } 102130f712c9SDave Chinner } 102230f712c9SDave Chinner } 102330f712c9SDave Chinner 102430f712c9SDave Chinner /* 102530f712c9SDave Chinner * Try to retrieve the next record to the left/right from the current one. 102630f712c9SDave Chinner */ 102730f712c9SDave Chinner STATIC int 102830f712c9SDave Chinner xfs_ialloc_next_rec( 102930f712c9SDave Chinner struct xfs_btree_cur *cur, 103030f712c9SDave Chinner xfs_inobt_rec_incore_t *rec, 103130f712c9SDave Chinner int *done, 103230f712c9SDave Chinner int left) 103330f712c9SDave Chinner { 103430f712c9SDave Chinner int error; 103530f712c9SDave Chinner int i; 103630f712c9SDave Chinner 103730f712c9SDave Chinner if (left) 103830f712c9SDave Chinner error = xfs_btree_decrement(cur, 0, &i); 103930f712c9SDave Chinner else 104030f712c9SDave Chinner error = xfs_btree_increment(cur, 0, &i); 104130f712c9SDave Chinner 104230f712c9SDave Chinner if (error) 104330f712c9SDave Chinner return error; 104430f712c9SDave Chinner *done = !i; 104530f712c9SDave Chinner if (i) { 104630f712c9SDave Chinner error = xfs_inobt_get_rec(cur, rec, &i); 104730f712c9SDave Chinner if (error) 104830f712c9SDave Chinner return error; 10495fb5aeeeSEric Sandeen XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1); 105030f712c9SDave Chinner } 105130f712c9SDave Chinner 105230f712c9SDave Chinner return 0; 105330f712c9SDave Chinner } 105430f712c9SDave Chinner 105530f712c9SDave Chinner STATIC int 105630f712c9SDave Chinner xfs_ialloc_get_rec( 105730f712c9SDave Chinner struct xfs_btree_cur *cur, 105830f712c9SDave Chinner xfs_agino_t agino, 105930f712c9SDave Chinner xfs_inobt_rec_incore_t *rec, 106030f712c9SDave Chinner int *done) 106130f712c9SDave Chinner { 106230f712c9SDave Chinner int error; 106330f712c9SDave Chinner int i; 106430f712c9SDave Chinner 106530f712c9SDave Chinner error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_EQ, &i); 106630f712c9SDave Chinner if (error) 106730f712c9SDave Chinner return error; 106830f712c9SDave Chinner *done = !i; 106930f712c9SDave Chinner if (i) { 107030f712c9SDave Chinner error = xfs_inobt_get_rec(cur, rec, &i); 107130f712c9SDave Chinner if (error) 107230f712c9SDave Chinner return error; 10735fb5aeeeSEric Sandeen XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1); 107430f712c9SDave Chinner } 107530f712c9SDave Chinner 107630f712c9SDave Chinner return 0; 107730f712c9SDave Chinner } 107830f712c9SDave Chinner 107930f712c9SDave Chinner /* 108026dd5217SBrian Foster * Return the offset of the first free inode in the record. If the inode chunk 108126dd5217SBrian Foster * is sparsely allocated, we convert the record holemask to inode granularity 108226dd5217SBrian Foster * and mask off the unallocated regions from the inode free mask. 1083d4cc540bSBrian Foster */ 1084d4cc540bSBrian Foster STATIC int 1085d4cc540bSBrian Foster xfs_inobt_first_free_inode( 1086d4cc540bSBrian Foster struct xfs_inobt_rec_incore *rec) 1087d4cc540bSBrian Foster { 108826dd5217SBrian Foster xfs_inofree_t realfree; 108926dd5217SBrian Foster 109026dd5217SBrian Foster /* if there are no holes, return the first available offset */ 109126dd5217SBrian Foster if (!xfs_inobt_issparse(rec->ir_holemask)) 1092d4cc540bSBrian Foster return xfs_lowbit64(rec->ir_free); 109326dd5217SBrian Foster 109426dd5217SBrian Foster realfree = xfs_inobt_irec_to_allocmask(rec); 109526dd5217SBrian Foster realfree &= rec->ir_free; 109626dd5217SBrian Foster 109726dd5217SBrian Foster return xfs_lowbit64(realfree); 1098d4cc540bSBrian Foster } 1099d4cc540bSBrian Foster 1100d4cc540bSBrian Foster /* 110130f712c9SDave Chinner * Allocate an inode using the inobt-only algorithm. 110230f712c9SDave Chinner */ 110330f712c9SDave Chinner STATIC int 110430f712c9SDave Chinner xfs_dialloc_ag_inobt( 110530f712c9SDave Chinner struct xfs_trans *tp, 110630f712c9SDave Chinner struct xfs_buf *agbp, 110730f712c9SDave Chinner xfs_ino_t parent, 110830f712c9SDave Chinner xfs_ino_t *inop) 110930f712c9SDave Chinner { 111030f712c9SDave Chinner struct xfs_mount *mp = tp->t_mountp; 111130f712c9SDave Chinner struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp); 111230f712c9SDave Chinner xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno); 111330f712c9SDave Chinner xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent); 111430f712c9SDave Chinner xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent); 111530f712c9SDave Chinner struct xfs_perag *pag; 111630f712c9SDave Chinner struct xfs_btree_cur *cur, *tcur; 111730f712c9SDave Chinner struct xfs_inobt_rec_incore rec, trec; 111830f712c9SDave Chinner xfs_ino_t ino; 111930f712c9SDave Chinner int error; 112030f712c9SDave Chinner int offset; 112130f712c9SDave Chinner int i, j; 112230f712c9SDave Chinner 112330f712c9SDave Chinner pag = xfs_perag_get(mp, agno); 112430f712c9SDave Chinner 112530f712c9SDave Chinner ASSERT(pag->pagi_init); 112630f712c9SDave Chinner ASSERT(pag->pagi_inodeok); 112730f712c9SDave Chinner ASSERT(pag->pagi_freecount > 0); 112830f712c9SDave Chinner 112930f712c9SDave Chinner restart_pagno: 113030f712c9SDave Chinner cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO); 113130f712c9SDave Chinner /* 113230f712c9SDave Chinner * If pagino is 0 (this is the root inode allocation) use newino. 113330f712c9SDave Chinner * This must work because we've just allocated some. 113430f712c9SDave Chinner */ 113530f712c9SDave Chinner if (!pagino) 113630f712c9SDave Chinner pagino = be32_to_cpu(agi->agi_newino); 113730f712c9SDave Chinner 113830f712c9SDave Chinner error = xfs_check_agi_freecount(cur, agi); 113930f712c9SDave Chinner if (error) 114030f712c9SDave Chinner goto error0; 114130f712c9SDave Chinner 114230f712c9SDave Chinner /* 114330f712c9SDave Chinner * If in the same AG as the parent, try to get near the parent. 114430f712c9SDave Chinner */ 114530f712c9SDave Chinner if (pagno == agno) { 114630f712c9SDave Chinner int doneleft; /* done, to the left */ 114730f712c9SDave Chinner int doneright; /* done, to the right */ 114830f712c9SDave Chinner int searchdistance = 10; 114930f712c9SDave Chinner 115030f712c9SDave Chinner error = xfs_inobt_lookup(cur, pagino, XFS_LOOKUP_LE, &i); 115130f712c9SDave Chinner if (error) 115230f712c9SDave Chinner goto error0; 1153c29aad41SEric Sandeen XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0); 115430f712c9SDave Chinner 115530f712c9SDave Chinner error = xfs_inobt_get_rec(cur, &rec, &j); 115630f712c9SDave Chinner if (error) 115730f712c9SDave Chinner goto error0; 1158c29aad41SEric Sandeen XFS_WANT_CORRUPTED_GOTO(mp, j == 1, error0); 115930f712c9SDave Chinner 116030f712c9SDave Chinner if (rec.ir_freecount > 0) { 116130f712c9SDave Chinner /* 116230f712c9SDave Chinner * Found a free inode in the same chunk 116330f712c9SDave Chinner * as the parent, done. 116430f712c9SDave Chinner */ 116530f712c9SDave Chinner goto alloc_inode; 116630f712c9SDave Chinner } 116730f712c9SDave Chinner 116830f712c9SDave Chinner 116930f712c9SDave Chinner /* 117030f712c9SDave Chinner * In the same AG as parent, but parent's chunk is full. 117130f712c9SDave Chinner */ 117230f712c9SDave Chinner 117330f712c9SDave Chinner /* duplicate the cursor, search left & right simultaneously */ 117430f712c9SDave Chinner error = xfs_btree_dup_cursor(cur, &tcur); 117530f712c9SDave Chinner if (error) 117630f712c9SDave Chinner goto error0; 117730f712c9SDave Chinner 117830f712c9SDave Chinner /* 117930f712c9SDave Chinner * Skip to last blocks looked up if same parent inode. 118030f712c9SDave Chinner */ 118130f712c9SDave Chinner if (pagino != NULLAGINO && 118230f712c9SDave Chinner pag->pagl_pagino == pagino && 118330f712c9SDave Chinner pag->pagl_leftrec != NULLAGINO && 118430f712c9SDave Chinner pag->pagl_rightrec != NULLAGINO) { 118530f712c9SDave Chinner error = xfs_ialloc_get_rec(tcur, pag->pagl_leftrec, 118630f712c9SDave Chinner &trec, &doneleft); 118730f712c9SDave Chinner if (error) 118830f712c9SDave Chinner goto error1; 118930f712c9SDave Chinner 119030f712c9SDave Chinner error = xfs_ialloc_get_rec(cur, pag->pagl_rightrec, 119130f712c9SDave Chinner &rec, &doneright); 119230f712c9SDave Chinner if (error) 119330f712c9SDave Chinner goto error1; 119430f712c9SDave Chinner } else { 119530f712c9SDave Chinner /* search left with tcur, back up 1 record */ 119630f712c9SDave Chinner error = xfs_ialloc_next_rec(tcur, &trec, &doneleft, 1); 119730f712c9SDave Chinner if (error) 119830f712c9SDave Chinner goto error1; 119930f712c9SDave Chinner 120030f712c9SDave Chinner /* search right with cur, go forward 1 record. */ 120130f712c9SDave Chinner error = xfs_ialloc_next_rec(cur, &rec, &doneright, 0); 120230f712c9SDave Chinner if (error) 120330f712c9SDave Chinner goto error1; 120430f712c9SDave Chinner } 120530f712c9SDave Chinner 120630f712c9SDave Chinner /* 120730f712c9SDave Chinner * Loop until we find an inode chunk with a free inode. 120830f712c9SDave Chinner */ 120930f712c9SDave Chinner while (!doneleft || !doneright) { 121030f712c9SDave Chinner int useleft; /* using left inode chunk this time */ 121130f712c9SDave Chinner 121230f712c9SDave Chinner if (!--searchdistance) { 121330f712c9SDave Chinner /* 121430f712c9SDave Chinner * Not in range - save last search 121530f712c9SDave Chinner * location and allocate a new inode 121630f712c9SDave Chinner */ 121730f712c9SDave Chinner xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); 121830f712c9SDave Chinner pag->pagl_leftrec = trec.ir_startino; 121930f712c9SDave Chinner pag->pagl_rightrec = rec.ir_startino; 122030f712c9SDave Chinner pag->pagl_pagino = pagino; 122130f712c9SDave Chinner goto newino; 122230f712c9SDave Chinner } 122330f712c9SDave Chinner 122430f712c9SDave Chinner /* figure out the closer block if both are valid. */ 122530f712c9SDave Chinner if (!doneleft && !doneright) { 122630f712c9SDave Chinner useleft = pagino - 122730f712c9SDave Chinner (trec.ir_startino + XFS_INODES_PER_CHUNK - 1) < 122830f712c9SDave Chinner rec.ir_startino - pagino; 122930f712c9SDave Chinner } else { 123030f712c9SDave Chinner useleft = !doneleft; 123130f712c9SDave Chinner } 123230f712c9SDave Chinner 123330f712c9SDave Chinner /* free inodes to the left? */ 123430f712c9SDave Chinner if (useleft && trec.ir_freecount) { 123530f712c9SDave Chinner rec = trec; 123630f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); 123730f712c9SDave Chinner cur = tcur; 123830f712c9SDave Chinner 123930f712c9SDave Chinner pag->pagl_leftrec = trec.ir_startino; 124030f712c9SDave Chinner pag->pagl_rightrec = rec.ir_startino; 124130f712c9SDave Chinner pag->pagl_pagino = pagino; 124230f712c9SDave Chinner goto alloc_inode; 124330f712c9SDave Chinner } 124430f712c9SDave Chinner 124530f712c9SDave Chinner /* free inodes to the right? */ 124630f712c9SDave Chinner if (!useleft && rec.ir_freecount) { 124730f712c9SDave Chinner xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); 124830f712c9SDave Chinner 124930f712c9SDave Chinner pag->pagl_leftrec = trec.ir_startino; 125030f712c9SDave Chinner pag->pagl_rightrec = rec.ir_startino; 125130f712c9SDave Chinner pag->pagl_pagino = pagino; 125230f712c9SDave Chinner goto alloc_inode; 125330f712c9SDave Chinner } 125430f712c9SDave Chinner 125530f712c9SDave Chinner /* get next record to check */ 125630f712c9SDave Chinner if (useleft) { 125730f712c9SDave Chinner error = xfs_ialloc_next_rec(tcur, &trec, 125830f712c9SDave Chinner &doneleft, 1); 125930f712c9SDave Chinner } else { 126030f712c9SDave Chinner error = xfs_ialloc_next_rec(cur, &rec, 126130f712c9SDave Chinner &doneright, 0); 126230f712c9SDave Chinner } 126330f712c9SDave Chinner if (error) 126430f712c9SDave Chinner goto error1; 126530f712c9SDave Chinner } 126630f712c9SDave Chinner 126730f712c9SDave Chinner /* 126830f712c9SDave Chinner * We've reached the end of the btree. because 126930f712c9SDave Chinner * we are only searching a small chunk of the 127030f712c9SDave Chinner * btree each search, there is obviously free 127130f712c9SDave Chinner * inodes closer to the parent inode than we 127230f712c9SDave Chinner * are now. restart the search again. 127330f712c9SDave Chinner */ 127430f712c9SDave Chinner pag->pagl_pagino = NULLAGINO; 127530f712c9SDave Chinner pag->pagl_leftrec = NULLAGINO; 127630f712c9SDave Chinner pag->pagl_rightrec = NULLAGINO; 127730f712c9SDave Chinner xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); 127830f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); 127930f712c9SDave Chinner goto restart_pagno; 128030f712c9SDave Chinner } 128130f712c9SDave Chinner 128230f712c9SDave Chinner /* 128330f712c9SDave Chinner * In a different AG from the parent. 128430f712c9SDave Chinner * See if the most recently allocated block has any free. 128530f712c9SDave Chinner */ 128630f712c9SDave Chinner newino: 128730f712c9SDave Chinner if (agi->agi_newino != cpu_to_be32(NULLAGINO)) { 128830f712c9SDave Chinner error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino), 128930f712c9SDave Chinner XFS_LOOKUP_EQ, &i); 129030f712c9SDave Chinner if (error) 129130f712c9SDave Chinner goto error0; 129230f712c9SDave Chinner 129330f712c9SDave Chinner if (i == 1) { 129430f712c9SDave Chinner error = xfs_inobt_get_rec(cur, &rec, &j); 129530f712c9SDave Chinner if (error) 129630f712c9SDave Chinner goto error0; 129730f712c9SDave Chinner 129830f712c9SDave Chinner if (j == 1 && rec.ir_freecount > 0) { 129930f712c9SDave Chinner /* 130030f712c9SDave Chinner * The last chunk allocated in the group 130130f712c9SDave Chinner * still has a free inode. 130230f712c9SDave Chinner */ 130330f712c9SDave Chinner goto alloc_inode; 130430f712c9SDave Chinner } 130530f712c9SDave Chinner } 130630f712c9SDave Chinner } 130730f712c9SDave Chinner 130830f712c9SDave Chinner /* 130930f712c9SDave Chinner * None left in the last group, search the whole AG 131030f712c9SDave Chinner */ 131130f712c9SDave Chinner error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i); 131230f712c9SDave Chinner if (error) 131330f712c9SDave Chinner goto error0; 1314c29aad41SEric Sandeen XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0); 131530f712c9SDave Chinner 131630f712c9SDave Chinner for (;;) { 131730f712c9SDave Chinner error = xfs_inobt_get_rec(cur, &rec, &i); 131830f712c9SDave Chinner if (error) 131930f712c9SDave Chinner goto error0; 1320c29aad41SEric Sandeen XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0); 132130f712c9SDave Chinner if (rec.ir_freecount > 0) 132230f712c9SDave Chinner break; 132330f712c9SDave Chinner error = xfs_btree_increment(cur, 0, &i); 132430f712c9SDave Chinner if (error) 132530f712c9SDave Chinner goto error0; 1326c29aad41SEric Sandeen XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0); 132730f712c9SDave Chinner } 132830f712c9SDave Chinner 132930f712c9SDave Chinner alloc_inode: 1330d4cc540bSBrian Foster offset = xfs_inobt_first_free_inode(&rec); 133130f712c9SDave Chinner ASSERT(offset >= 0); 133230f712c9SDave Chinner ASSERT(offset < XFS_INODES_PER_CHUNK); 133330f712c9SDave Chinner ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) % 133430f712c9SDave Chinner XFS_INODES_PER_CHUNK) == 0); 133530f712c9SDave Chinner ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset); 133630f712c9SDave Chinner rec.ir_free &= ~XFS_INOBT_MASK(offset); 133730f712c9SDave Chinner rec.ir_freecount--; 133830f712c9SDave Chinner error = xfs_inobt_update(cur, &rec); 133930f712c9SDave Chinner if (error) 134030f712c9SDave Chinner goto error0; 134130f712c9SDave Chinner be32_add_cpu(&agi->agi_freecount, -1); 134230f712c9SDave Chinner xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT); 134330f712c9SDave Chinner pag->pagi_freecount--; 134430f712c9SDave Chinner 134530f712c9SDave Chinner error = xfs_check_agi_freecount(cur, agi); 134630f712c9SDave Chinner if (error) 134730f712c9SDave Chinner goto error0; 134830f712c9SDave Chinner 134930f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); 135030f712c9SDave Chinner xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1); 135130f712c9SDave Chinner xfs_perag_put(pag); 135230f712c9SDave Chinner *inop = ino; 135330f712c9SDave Chinner return 0; 135430f712c9SDave Chinner error1: 135530f712c9SDave Chinner xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR); 135630f712c9SDave Chinner error0: 135730f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); 135830f712c9SDave Chinner xfs_perag_put(pag); 135930f712c9SDave Chinner return error; 136030f712c9SDave Chinner } 136130f712c9SDave Chinner 136230f712c9SDave Chinner /* 136330f712c9SDave Chinner * Use the free inode btree to allocate an inode based on distance from the 136430f712c9SDave Chinner * parent. Note that the provided cursor may be deleted and replaced. 136530f712c9SDave Chinner */ 136630f712c9SDave Chinner STATIC int 136730f712c9SDave Chinner xfs_dialloc_ag_finobt_near( 136830f712c9SDave Chinner xfs_agino_t pagino, 136930f712c9SDave Chinner struct xfs_btree_cur **ocur, 137030f712c9SDave Chinner struct xfs_inobt_rec_incore *rec) 137130f712c9SDave Chinner { 137230f712c9SDave Chinner struct xfs_btree_cur *lcur = *ocur; /* left search cursor */ 137330f712c9SDave Chinner struct xfs_btree_cur *rcur; /* right search cursor */ 137430f712c9SDave Chinner struct xfs_inobt_rec_incore rrec; 137530f712c9SDave Chinner int error; 137630f712c9SDave Chinner int i, j; 137730f712c9SDave Chinner 137830f712c9SDave Chinner error = xfs_inobt_lookup(lcur, pagino, XFS_LOOKUP_LE, &i); 137930f712c9SDave Chinner if (error) 138030f712c9SDave Chinner return error; 138130f712c9SDave Chinner 138230f712c9SDave Chinner if (i == 1) { 138330f712c9SDave Chinner error = xfs_inobt_get_rec(lcur, rec, &i); 138430f712c9SDave Chinner if (error) 138530f712c9SDave Chinner return error; 13865fb5aeeeSEric Sandeen XFS_WANT_CORRUPTED_RETURN(lcur->bc_mp, i == 1); 138730f712c9SDave Chinner 138830f712c9SDave Chinner /* 138930f712c9SDave Chinner * See if we've landed in the parent inode record. The finobt 139030f712c9SDave Chinner * only tracks chunks with at least one free inode, so record 139130f712c9SDave Chinner * existence is enough. 139230f712c9SDave Chinner */ 139330f712c9SDave Chinner if (pagino >= rec->ir_startino && 139430f712c9SDave Chinner pagino < (rec->ir_startino + XFS_INODES_PER_CHUNK)) 139530f712c9SDave Chinner return 0; 139630f712c9SDave Chinner } 139730f712c9SDave Chinner 139830f712c9SDave Chinner error = xfs_btree_dup_cursor(lcur, &rcur); 139930f712c9SDave Chinner if (error) 140030f712c9SDave Chinner return error; 140130f712c9SDave Chinner 140230f712c9SDave Chinner error = xfs_inobt_lookup(rcur, pagino, XFS_LOOKUP_GE, &j); 140330f712c9SDave Chinner if (error) 140430f712c9SDave Chinner goto error_rcur; 140530f712c9SDave Chinner if (j == 1) { 140630f712c9SDave Chinner error = xfs_inobt_get_rec(rcur, &rrec, &j); 140730f712c9SDave Chinner if (error) 140830f712c9SDave Chinner goto error_rcur; 1409c29aad41SEric Sandeen XFS_WANT_CORRUPTED_GOTO(lcur->bc_mp, j == 1, error_rcur); 141030f712c9SDave Chinner } 141130f712c9SDave Chinner 1412c29aad41SEric Sandeen XFS_WANT_CORRUPTED_GOTO(lcur->bc_mp, i == 1 || j == 1, error_rcur); 141330f712c9SDave Chinner if (i == 1 && j == 1) { 141430f712c9SDave Chinner /* 141530f712c9SDave Chinner * Both the left and right records are valid. Choose the closer 141630f712c9SDave Chinner * inode chunk to the target. 141730f712c9SDave Chinner */ 141830f712c9SDave Chinner if ((pagino - rec->ir_startino + XFS_INODES_PER_CHUNK - 1) > 141930f712c9SDave Chinner (rrec.ir_startino - pagino)) { 142030f712c9SDave Chinner *rec = rrec; 142130f712c9SDave Chinner xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR); 142230f712c9SDave Chinner *ocur = rcur; 142330f712c9SDave Chinner } else { 142430f712c9SDave Chinner xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR); 142530f712c9SDave Chinner } 142630f712c9SDave Chinner } else if (j == 1) { 142730f712c9SDave Chinner /* only the right record is valid */ 142830f712c9SDave Chinner *rec = rrec; 142930f712c9SDave Chinner xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR); 143030f712c9SDave Chinner *ocur = rcur; 143130f712c9SDave Chinner } else if (i == 1) { 143230f712c9SDave Chinner /* only the left record is valid */ 143330f712c9SDave Chinner xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR); 143430f712c9SDave Chinner } 143530f712c9SDave Chinner 143630f712c9SDave Chinner return 0; 143730f712c9SDave Chinner 143830f712c9SDave Chinner error_rcur: 143930f712c9SDave Chinner xfs_btree_del_cursor(rcur, XFS_BTREE_ERROR); 144030f712c9SDave Chinner return error; 144130f712c9SDave Chinner } 144230f712c9SDave Chinner 144330f712c9SDave Chinner /* 144430f712c9SDave Chinner * Use the free inode btree to find a free inode based on a newino hint. If 144530f712c9SDave Chinner * the hint is NULL, find the first free inode in the AG. 144630f712c9SDave Chinner */ 144730f712c9SDave Chinner STATIC int 144830f712c9SDave Chinner xfs_dialloc_ag_finobt_newino( 144930f712c9SDave Chinner struct xfs_agi *agi, 145030f712c9SDave Chinner struct xfs_btree_cur *cur, 145130f712c9SDave Chinner struct xfs_inobt_rec_incore *rec) 145230f712c9SDave Chinner { 145330f712c9SDave Chinner int error; 145430f712c9SDave Chinner int i; 145530f712c9SDave Chinner 145630f712c9SDave Chinner if (agi->agi_newino != cpu_to_be32(NULLAGINO)) { 1457e68ed775SDave Chinner error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino), 1458e68ed775SDave Chinner XFS_LOOKUP_EQ, &i); 145930f712c9SDave Chinner if (error) 146030f712c9SDave Chinner return error; 146130f712c9SDave Chinner if (i == 1) { 146230f712c9SDave Chinner error = xfs_inobt_get_rec(cur, rec, &i); 146330f712c9SDave Chinner if (error) 146430f712c9SDave Chinner return error; 14655fb5aeeeSEric Sandeen XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1); 146630f712c9SDave Chinner return 0; 146730f712c9SDave Chinner } 146830f712c9SDave Chinner } 146930f712c9SDave Chinner 147030f712c9SDave Chinner /* 147130f712c9SDave Chinner * Find the first inode available in the AG. 147230f712c9SDave Chinner */ 147330f712c9SDave Chinner error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i); 147430f712c9SDave Chinner if (error) 147530f712c9SDave Chinner return error; 14765fb5aeeeSEric Sandeen XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1); 147730f712c9SDave Chinner 147830f712c9SDave Chinner error = xfs_inobt_get_rec(cur, rec, &i); 147930f712c9SDave Chinner if (error) 148030f712c9SDave Chinner return error; 14815fb5aeeeSEric Sandeen XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1); 148230f712c9SDave Chinner 148330f712c9SDave Chinner return 0; 148430f712c9SDave Chinner } 148530f712c9SDave Chinner 148630f712c9SDave Chinner /* 148730f712c9SDave Chinner * Update the inobt based on a modification made to the finobt. Also ensure that 148830f712c9SDave Chinner * the records from both trees are equivalent post-modification. 148930f712c9SDave Chinner */ 149030f712c9SDave Chinner STATIC int 149130f712c9SDave Chinner xfs_dialloc_ag_update_inobt( 149230f712c9SDave Chinner struct xfs_btree_cur *cur, /* inobt cursor */ 149330f712c9SDave Chinner struct xfs_inobt_rec_incore *frec, /* finobt record */ 149430f712c9SDave Chinner int offset) /* inode offset */ 149530f712c9SDave Chinner { 149630f712c9SDave Chinner struct xfs_inobt_rec_incore rec; 149730f712c9SDave Chinner int error; 149830f712c9SDave Chinner int i; 149930f712c9SDave Chinner 150030f712c9SDave Chinner error = xfs_inobt_lookup(cur, frec->ir_startino, XFS_LOOKUP_EQ, &i); 150130f712c9SDave Chinner if (error) 150230f712c9SDave Chinner return error; 15035fb5aeeeSEric Sandeen XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1); 150430f712c9SDave Chinner 150530f712c9SDave Chinner error = xfs_inobt_get_rec(cur, &rec, &i); 150630f712c9SDave Chinner if (error) 150730f712c9SDave Chinner return error; 15085fb5aeeeSEric Sandeen XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1); 150930f712c9SDave Chinner ASSERT((XFS_AGINO_TO_OFFSET(cur->bc_mp, rec.ir_startino) % 151030f712c9SDave Chinner XFS_INODES_PER_CHUNK) == 0); 151130f712c9SDave Chinner 151230f712c9SDave Chinner rec.ir_free &= ~XFS_INOBT_MASK(offset); 151330f712c9SDave Chinner rec.ir_freecount--; 151430f712c9SDave Chinner 15155fb5aeeeSEric Sandeen XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, (rec.ir_free == frec->ir_free) && 151630f712c9SDave Chinner (rec.ir_freecount == frec->ir_freecount)); 151730f712c9SDave Chinner 1518b72091f2Skbuild test robot return xfs_inobt_update(cur, &rec); 151930f712c9SDave Chinner } 152030f712c9SDave Chinner 152130f712c9SDave Chinner /* 152230f712c9SDave Chinner * Allocate an inode using the free inode btree, if available. Otherwise, fall 152330f712c9SDave Chinner * back to the inobt search algorithm. 152430f712c9SDave Chinner * 152530f712c9SDave Chinner * The caller selected an AG for us, and made sure that free inodes are 152630f712c9SDave Chinner * available. 152730f712c9SDave Chinner */ 152830f712c9SDave Chinner STATIC int 152930f712c9SDave Chinner xfs_dialloc_ag( 153030f712c9SDave Chinner struct xfs_trans *tp, 153130f712c9SDave Chinner struct xfs_buf *agbp, 153230f712c9SDave Chinner xfs_ino_t parent, 153330f712c9SDave Chinner xfs_ino_t *inop) 153430f712c9SDave Chinner { 153530f712c9SDave Chinner struct xfs_mount *mp = tp->t_mountp; 153630f712c9SDave Chinner struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp); 153730f712c9SDave Chinner xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno); 153830f712c9SDave Chinner xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent); 153930f712c9SDave Chinner xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent); 154030f712c9SDave Chinner struct xfs_perag *pag; 154130f712c9SDave Chinner struct xfs_btree_cur *cur; /* finobt cursor */ 154230f712c9SDave Chinner struct xfs_btree_cur *icur; /* inobt cursor */ 154330f712c9SDave Chinner struct xfs_inobt_rec_incore rec; 154430f712c9SDave Chinner xfs_ino_t ino; 154530f712c9SDave Chinner int error; 154630f712c9SDave Chinner int offset; 154730f712c9SDave Chinner int i; 154830f712c9SDave Chinner 154930f712c9SDave Chinner if (!xfs_sb_version_hasfinobt(&mp->m_sb)) 155030f712c9SDave Chinner return xfs_dialloc_ag_inobt(tp, agbp, parent, inop); 155130f712c9SDave Chinner 155230f712c9SDave Chinner pag = xfs_perag_get(mp, agno); 155330f712c9SDave Chinner 155430f712c9SDave Chinner /* 155530f712c9SDave Chinner * If pagino is 0 (this is the root inode allocation) use newino. 155630f712c9SDave Chinner * This must work because we've just allocated some. 155730f712c9SDave Chinner */ 155830f712c9SDave Chinner if (!pagino) 155930f712c9SDave Chinner pagino = be32_to_cpu(agi->agi_newino); 156030f712c9SDave Chinner 156130f712c9SDave Chinner cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_FINO); 156230f712c9SDave Chinner 156330f712c9SDave Chinner error = xfs_check_agi_freecount(cur, agi); 156430f712c9SDave Chinner if (error) 156530f712c9SDave Chinner goto error_cur; 156630f712c9SDave Chinner 156730f712c9SDave Chinner /* 156830f712c9SDave Chinner * The search algorithm depends on whether we're in the same AG as the 156930f712c9SDave Chinner * parent. If so, find the closest available inode to the parent. If 157030f712c9SDave Chinner * not, consider the agi hint or find the first free inode in the AG. 157130f712c9SDave Chinner */ 157230f712c9SDave Chinner if (agno == pagno) 157330f712c9SDave Chinner error = xfs_dialloc_ag_finobt_near(pagino, &cur, &rec); 157430f712c9SDave Chinner else 157530f712c9SDave Chinner error = xfs_dialloc_ag_finobt_newino(agi, cur, &rec); 157630f712c9SDave Chinner if (error) 157730f712c9SDave Chinner goto error_cur; 157830f712c9SDave Chinner 1579d4cc540bSBrian Foster offset = xfs_inobt_first_free_inode(&rec); 158030f712c9SDave Chinner ASSERT(offset >= 0); 158130f712c9SDave Chinner ASSERT(offset < XFS_INODES_PER_CHUNK); 158230f712c9SDave Chinner ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) % 158330f712c9SDave Chinner XFS_INODES_PER_CHUNK) == 0); 158430f712c9SDave Chinner ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset); 158530f712c9SDave Chinner 158630f712c9SDave Chinner /* 158730f712c9SDave Chinner * Modify or remove the finobt record. 158830f712c9SDave Chinner */ 158930f712c9SDave Chinner rec.ir_free &= ~XFS_INOBT_MASK(offset); 159030f712c9SDave Chinner rec.ir_freecount--; 159130f712c9SDave Chinner if (rec.ir_freecount) 159230f712c9SDave Chinner error = xfs_inobt_update(cur, &rec); 159330f712c9SDave Chinner else 159430f712c9SDave Chinner error = xfs_btree_delete(cur, &i); 159530f712c9SDave Chinner if (error) 159630f712c9SDave Chinner goto error_cur; 159730f712c9SDave Chinner 159830f712c9SDave Chinner /* 159930f712c9SDave Chinner * The finobt has now been updated appropriately. We haven't updated the 160030f712c9SDave Chinner * agi and superblock yet, so we can create an inobt cursor and validate 160130f712c9SDave Chinner * the original freecount. If all is well, make the equivalent update to 160230f712c9SDave Chinner * the inobt using the finobt record and offset information. 160330f712c9SDave Chinner */ 160430f712c9SDave Chinner icur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO); 160530f712c9SDave Chinner 160630f712c9SDave Chinner error = xfs_check_agi_freecount(icur, agi); 160730f712c9SDave Chinner if (error) 160830f712c9SDave Chinner goto error_icur; 160930f712c9SDave Chinner 161030f712c9SDave Chinner error = xfs_dialloc_ag_update_inobt(icur, &rec, offset); 161130f712c9SDave Chinner if (error) 161230f712c9SDave Chinner goto error_icur; 161330f712c9SDave Chinner 161430f712c9SDave Chinner /* 161530f712c9SDave Chinner * Both trees have now been updated. We must update the perag and 161630f712c9SDave Chinner * superblock before we can check the freecount for each btree. 161730f712c9SDave Chinner */ 161830f712c9SDave Chinner be32_add_cpu(&agi->agi_freecount, -1); 161930f712c9SDave Chinner xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT); 162030f712c9SDave Chinner pag->pagi_freecount--; 162130f712c9SDave Chinner 162230f712c9SDave Chinner xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1); 162330f712c9SDave Chinner 162430f712c9SDave Chinner error = xfs_check_agi_freecount(icur, agi); 162530f712c9SDave Chinner if (error) 162630f712c9SDave Chinner goto error_icur; 162730f712c9SDave Chinner error = xfs_check_agi_freecount(cur, agi); 162830f712c9SDave Chinner if (error) 162930f712c9SDave Chinner goto error_icur; 163030f712c9SDave Chinner 163130f712c9SDave Chinner xfs_btree_del_cursor(icur, XFS_BTREE_NOERROR); 163230f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); 163330f712c9SDave Chinner xfs_perag_put(pag); 163430f712c9SDave Chinner *inop = ino; 163530f712c9SDave Chinner return 0; 163630f712c9SDave Chinner 163730f712c9SDave Chinner error_icur: 163830f712c9SDave Chinner xfs_btree_del_cursor(icur, XFS_BTREE_ERROR); 163930f712c9SDave Chinner error_cur: 164030f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); 164130f712c9SDave Chinner xfs_perag_put(pag); 164230f712c9SDave Chinner return error; 164330f712c9SDave Chinner } 164430f712c9SDave Chinner 164530f712c9SDave Chinner /* 164630f712c9SDave Chinner * Allocate an inode on disk. 164730f712c9SDave Chinner * 164830f712c9SDave Chinner * Mode is used to tell whether the new inode will need space, and whether it 164930f712c9SDave Chinner * is a directory. 165030f712c9SDave Chinner * 165130f712c9SDave Chinner * This function is designed to be called twice if it has to do an allocation 165230f712c9SDave Chinner * to make more free inodes. On the first call, *IO_agbp should be set to NULL. 165330f712c9SDave Chinner * If an inode is available without having to performn an allocation, an inode 165430f712c9SDave Chinner * number is returned. In this case, *IO_agbp is set to NULL. If an allocation 165530f712c9SDave Chinner * needs to be done, xfs_dialloc returns the current AGI buffer in *IO_agbp. 165630f712c9SDave Chinner * The caller should then commit the current transaction, allocate a 165730f712c9SDave Chinner * new transaction, and call xfs_dialloc() again, passing in the previous value 165830f712c9SDave Chinner * of *IO_agbp. IO_agbp should be held across the transactions. Since the AGI 165930f712c9SDave Chinner * buffer is locked across the two calls, the second call is guaranteed to have 166030f712c9SDave Chinner * a free inode available. 166130f712c9SDave Chinner * 166230f712c9SDave Chinner * Once we successfully pick an inode its number is returned and the on-disk 166330f712c9SDave Chinner * data structures are updated. The inode itself is not read in, since doing so 166430f712c9SDave Chinner * would break ordering constraints with xfs_reclaim. 166530f712c9SDave Chinner */ 166630f712c9SDave Chinner int 166730f712c9SDave Chinner xfs_dialloc( 166830f712c9SDave Chinner struct xfs_trans *tp, 166930f712c9SDave Chinner xfs_ino_t parent, 167030f712c9SDave Chinner umode_t mode, 167130f712c9SDave Chinner int okalloc, 167230f712c9SDave Chinner struct xfs_buf **IO_agbp, 167330f712c9SDave Chinner xfs_ino_t *inop) 167430f712c9SDave Chinner { 167530f712c9SDave Chinner struct xfs_mount *mp = tp->t_mountp; 167630f712c9SDave Chinner struct xfs_buf *agbp; 167730f712c9SDave Chinner xfs_agnumber_t agno; 167830f712c9SDave Chinner int error; 167930f712c9SDave Chinner int ialloced; 168030f712c9SDave Chinner int noroom = 0; 168130f712c9SDave Chinner xfs_agnumber_t start_agno; 168230f712c9SDave Chinner struct xfs_perag *pag; 168330f712c9SDave Chinner 168430f712c9SDave Chinner if (*IO_agbp) { 168530f712c9SDave Chinner /* 168630f712c9SDave Chinner * If the caller passes in a pointer to the AGI buffer, 168730f712c9SDave Chinner * continue where we left off before. In this case, we 168830f712c9SDave Chinner * know that the allocation group has free inodes. 168930f712c9SDave Chinner */ 169030f712c9SDave Chinner agbp = *IO_agbp; 169130f712c9SDave Chinner goto out_alloc; 169230f712c9SDave Chinner } 169330f712c9SDave Chinner 169430f712c9SDave Chinner /* 169530f712c9SDave Chinner * We do not have an agbp, so select an initial allocation 169630f712c9SDave Chinner * group for inode allocation. 169730f712c9SDave Chinner */ 169830f712c9SDave Chinner start_agno = xfs_ialloc_ag_select(tp, parent, mode, okalloc); 169930f712c9SDave Chinner if (start_agno == NULLAGNUMBER) { 170030f712c9SDave Chinner *inop = NULLFSINO; 170130f712c9SDave Chinner return 0; 170230f712c9SDave Chinner } 170330f712c9SDave Chinner 170430f712c9SDave Chinner /* 170530f712c9SDave Chinner * If we have already hit the ceiling of inode blocks then clear 170630f712c9SDave Chinner * okalloc so we scan all available agi structures for a free 170730f712c9SDave Chinner * inode. 170874f9ce1cSGeorge Wang * 170974f9ce1cSGeorge Wang * Read rough value of mp->m_icount by percpu_counter_read_positive, 171074f9ce1cSGeorge Wang * which will sacrifice the preciseness but improve the performance. 171130f712c9SDave Chinner */ 171230f712c9SDave Chinner if (mp->m_maxicount && 171374f9ce1cSGeorge Wang percpu_counter_read_positive(&mp->m_icount) + mp->m_ialloc_inos 171474f9ce1cSGeorge Wang > mp->m_maxicount) { 171530f712c9SDave Chinner noroom = 1; 171630f712c9SDave Chinner okalloc = 0; 171730f712c9SDave Chinner } 171830f712c9SDave Chinner 171930f712c9SDave Chinner /* 172030f712c9SDave Chinner * Loop until we find an allocation group that either has free inodes 172130f712c9SDave Chinner * or in which we can allocate some inodes. Iterate through the 172230f712c9SDave Chinner * allocation groups upward, wrapping at the end. 172330f712c9SDave Chinner */ 172430f712c9SDave Chinner agno = start_agno; 172530f712c9SDave Chinner for (;;) { 172630f712c9SDave Chinner pag = xfs_perag_get(mp, agno); 172730f712c9SDave Chinner if (!pag->pagi_inodeok) { 172830f712c9SDave Chinner xfs_ialloc_next_ag(mp); 172930f712c9SDave Chinner goto nextag; 173030f712c9SDave Chinner } 173130f712c9SDave Chinner 173230f712c9SDave Chinner if (!pag->pagi_init) { 173330f712c9SDave Chinner error = xfs_ialloc_pagi_init(mp, tp, agno); 173430f712c9SDave Chinner if (error) 173530f712c9SDave Chinner goto out_error; 173630f712c9SDave Chinner } 173730f712c9SDave Chinner 173830f712c9SDave Chinner /* 173930f712c9SDave Chinner * Do a first racy fast path check if this AG is usable. 174030f712c9SDave Chinner */ 174130f712c9SDave Chinner if (!pag->pagi_freecount && !okalloc) 174230f712c9SDave Chinner goto nextag; 174330f712c9SDave Chinner 174430f712c9SDave Chinner /* 174530f712c9SDave Chinner * Then read in the AGI buffer and recheck with the AGI buffer 174630f712c9SDave Chinner * lock held. 174730f712c9SDave Chinner */ 174830f712c9SDave Chinner error = xfs_ialloc_read_agi(mp, tp, agno, &agbp); 174930f712c9SDave Chinner if (error) 175030f712c9SDave Chinner goto out_error; 175130f712c9SDave Chinner 175230f712c9SDave Chinner if (pag->pagi_freecount) { 175330f712c9SDave Chinner xfs_perag_put(pag); 175430f712c9SDave Chinner goto out_alloc; 175530f712c9SDave Chinner } 175630f712c9SDave Chinner 175730f712c9SDave Chinner if (!okalloc) 175830f712c9SDave Chinner goto nextag_relse_buffer; 175930f712c9SDave Chinner 176030f712c9SDave Chinner 176130f712c9SDave Chinner error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced); 176230f712c9SDave Chinner if (error) { 176330f712c9SDave Chinner xfs_trans_brelse(tp, agbp); 176430f712c9SDave Chinner 17652451337dSDave Chinner if (error != -ENOSPC) 176630f712c9SDave Chinner goto out_error; 176730f712c9SDave Chinner 176830f712c9SDave Chinner xfs_perag_put(pag); 176930f712c9SDave Chinner *inop = NULLFSINO; 177030f712c9SDave Chinner return 0; 177130f712c9SDave Chinner } 177230f712c9SDave Chinner 177330f712c9SDave Chinner if (ialloced) { 177430f712c9SDave Chinner /* 177530f712c9SDave Chinner * We successfully allocated some inodes, return 177630f712c9SDave Chinner * the current context to the caller so that it 177730f712c9SDave Chinner * can commit the current transaction and call 177830f712c9SDave Chinner * us again where we left off. 177930f712c9SDave Chinner */ 178030f712c9SDave Chinner ASSERT(pag->pagi_freecount > 0); 178130f712c9SDave Chinner xfs_perag_put(pag); 178230f712c9SDave Chinner 178330f712c9SDave Chinner *IO_agbp = agbp; 178430f712c9SDave Chinner *inop = NULLFSINO; 178530f712c9SDave Chinner return 0; 178630f712c9SDave Chinner } 178730f712c9SDave Chinner 178830f712c9SDave Chinner nextag_relse_buffer: 178930f712c9SDave Chinner xfs_trans_brelse(tp, agbp); 179030f712c9SDave Chinner nextag: 179130f712c9SDave Chinner xfs_perag_put(pag); 179230f712c9SDave Chinner if (++agno == mp->m_sb.sb_agcount) 179330f712c9SDave Chinner agno = 0; 179430f712c9SDave Chinner if (agno == start_agno) { 179530f712c9SDave Chinner *inop = NULLFSINO; 17962451337dSDave Chinner return noroom ? -ENOSPC : 0; 179730f712c9SDave Chinner } 179830f712c9SDave Chinner } 179930f712c9SDave Chinner 180030f712c9SDave Chinner out_alloc: 180130f712c9SDave Chinner *IO_agbp = NULL; 180230f712c9SDave Chinner return xfs_dialloc_ag(tp, agbp, parent, inop); 180330f712c9SDave Chinner out_error: 180430f712c9SDave Chinner xfs_perag_put(pag); 180530f712c9SDave Chinner return error; 180630f712c9SDave Chinner } 180730f712c9SDave Chinner 180810ae3dc7SBrian Foster /* 180910ae3dc7SBrian Foster * Free the blocks of an inode chunk. We must consider that the inode chunk 181010ae3dc7SBrian Foster * might be sparse and only free the regions that are allocated as part of the 181110ae3dc7SBrian Foster * chunk. 181210ae3dc7SBrian Foster */ 181310ae3dc7SBrian Foster STATIC void 181410ae3dc7SBrian Foster xfs_difree_inode_chunk( 181510ae3dc7SBrian Foster struct xfs_mount *mp, 181610ae3dc7SBrian Foster xfs_agnumber_t agno, 181710ae3dc7SBrian Foster struct xfs_inobt_rec_incore *rec, 181810ae3dc7SBrian Foster struct xfs_bmap_free *flist) 181910ae3dc7SBrian Foster { 182010ae3dc7SBrian Foster xfs_agblock_t sagbno = XFS_AGINO_TO_AGBNO(mp, rec->ir_startino); 182110ae3dc7SBrian Foster int startidx, endidx; 182210ae3dc7SBrian Foster int nextbit; 182310ae3dc7SBrian Foster xfs_agblock_t agbno; 182410ae3dc7SBrian Foster int contigblk; 182510ae3dc7SBrian Foster DECLARE_BITMAP(holemask, XFS_INOBT_HOLEMASK_BITS); 182610ae3dc7SBrian Foster 182710ae3dc7SBrian Foster if (!xfs_inobt_issparse(rec->ir_holemask)) { 182810ae3dc7SBrian Foster /* not sparse, calculate extent info directly */ 182910ae3dc7SBrian Foster xfs_bmap_add_free(XFS_AGB_TO_FSB(mp, agno, 183010ae3dc7SBrian Foster XFS_AGINO_TO_AGBNO(mp, rec->ir_startino)), 183110ae3dc7SBrian Foster mp->m_ialloc_blks, flist, mp); 183210ae3dc7SBrian Foster return; 183310ae3dc7SBrian Foster } 183410ae3dc7SBrian Foster 183510ae3dc7SBrian Foster /* holemask is only 16-bits (fits in an unsigned long) */ 183610ae3dc7SBrian Foster ASSERT(sizeof(rec->ir_holemask) <= sizeof(holemask[0])); 183710ae3dc7SBrian Foster holemask[0] = rec->ir_holemask; 183810ae3dc7SBrian Foster 183910ae3dc7SBrian Foster /* 184010ae3dc7SBrian Foster * Find contiguous ranges of zeroes (i.e., allocated regions) in the 184110ae3dc7SBrian Foster * holemask and convert the start/end index of each range to an extent. 184210ae3dc7SBrian Foster * We start with the start and end index both pointing at the first 0 in 184310ae3dc7SBrian Foster * the mask. 184410ae3dc7SBrian Foster */ 184510ae3dc7SBrian Foster startidx = endidx = find_first_zero_bit(holemask, 184610ae3dc7SBrian Foster XFS_INOBT_HOLEMASK_BITS); 184710ae3dc7SBrian Foster nextbit = startidx + 1; 184810ae3dc7SBrian Foster while (startidx < XFS_INOBT_HOLEMASK_BITS) { 184910ae3dc7SBrian Foster nextbit = find_next_zero_bit(holemask, XFS_INOBT_HOLEMASK_BITS, 185010ae3dc7SBrian Foster nextbit); 185110ae3dc7SBrian Foster /* 185210ae3dc7SBrian Foster * If the next zero bit is contiguous, update the end index of 185310ae3dc7SBrian Foster * the current range and continue. 185410ae3dc7SBrian Foster */ 185510ae3dc7SBrian Foster if (nextbit != XFS_INOBT_HOLEMASK_BITS && 185610ae3dc7SBrian Foster nextbit == endidx + 1) { 185710ae3dc7SBrian Foster endidx = nextbit; 185810ae3dc7SBrian Foster goto next; 185910ae3dc7SBrian Foster } 186010ae3dc7SBrian Foster 186110ae3dc7SBrian Foster /* 186210ae3dc7SBrian Foster * nextbit is not contiguous with the current end index. Convert 186310ae3dc7SBrian Foster * the current start/end to an extent and add it to the free 186410ae3dc7SBrian Foster * list. 186510ae3dc7SBrian Foster */ 186610ae3dc7SBrian Foster agbno = sagbno + (startidx * XFS_INODES_PER_HOLEMASK_BIT) / 186710ae3dc7SBrian Foster mp->m_sb.sb_inopblock; 186810ae3dc7SBrian Foster contigblk = ((endidx - startidx + 1) * 186910ae3dc7SBrian Foster XFS_INODES_PER_HOLEMASK_BIT) / 187010ae3dc7SBrian Foster mp->m_sb.sb_inopblock; 187110ae3dc7SBrian Foster 187210ae3dc7SBrian Foster ASSERT(agbno % mp->m_sb.sb_spino_align == 0); 187310ae3dc7SBrian Foster ASSERT(contigblk % mp->m_sb.sb_spino_align == 0); 187410ae3dc7SBrian Foster xfs_bmap_add_free(XFS_AGB_TO_FSB(mp, agno, agbno), contigblk, 187510ae3dc7SBrian Foster flist, mp); 187610ae3dc7SBrian Foster 187710ae3dc7SBrian Foster /* reset range to current bit and carry on... */ 187810ae3dc7SBrian Foster startidx = endidx = nextbit; 187910ae3dc7SBrian Foster 188010ae3dc7SBrian Foster next: 188110ae3dc7SBrian Foster nextbit++; 188210ae3dc7SBrian Foster } 188310ae3dc7SBrian Foster } 188410ae3dc7SBrian Foster 188530f712c9SDave Chinner STATIC int 188630f712c9SDave Chinner xfs_difree_inobt( 188730f712c9SDave Chinner struct xfs_mount *mp, 188830f712c9SDave Chinner struct xfs_trans *tp, 188930f712c9SDave Chinner struct xfs_buf *agbp, 189030f712c9SDave Chinner xfs_agino_t agino, 189130f712c9SDave Chinner struct xfs_bmap_free *flist, 189209b56604SBrian Foster struct xfs_icluster *xic, 189330f712c9SDave Chinner struct xfs_inobt_rec_incore *orec) 189430f712c9SDave Chinner { 189530f712c9SDave Chinner struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp); 189630f712c9SDave Chinner xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno); 189730f712c9SDave Chinner struct xfs_perag *pag; 189830f712c9SDave Chinner struct xfs_btree_cur *cur; 189930f712c9SDave Chinner struct xfs_inobt_rec_incore rec; 190030f712c9SDave Chinner int ilen; 190130f712c9SDave Chinner int error; 190230f712c9SDave Chinner int i; 190330f712c9SDave Chinner int off; 190430f712c9SDave Chinner 190530f712c9SDave Chinner ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC)); 190630f712c9SDave Chinner ASSERT(XFS_AGINO_TO_AGBNO(mp, agino) < be32_to_cpu(agi->agi_length)); 190730f712c9SDave Chinner 190830f712c9SDave Chinner /* 190930f712c9SDave Chinner * Initialize the cursor. 191030f712c9SDave Chinner */ 191130f712c9SDave Chinner cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO); 191230f712c9SDave Chinner 191330f712c9SDave Chinner error = xfs_check_agi_freecount(cur, agi); 191430f712c9SDave Chinner if (error) 191530f712c9SDave Chinner goto error0; 191630f712c9SDave Chinner 191730f712c9SDave Chinner /* 191830f712c9SDave Chinner * Look for the entry describing this inode. 191930f712c9SDave Chinner */ 192030f712c9SDave Chinner if ((error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i))) { 192130f712c9SDave Chinner xfs_warn(mp, "%s: xfs_inobt_lookup() returned error %d.", 192230f712c9SDave Chinner __func__, error); 192330f712c9SDave Chinner goto error0; 192430f712c9SDave Chinner } 1925c29aad41SEric Sandeen XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0); 192630f712c9SDave Chinner error = xfs_inobt_get_rec(cur, &rec, &i); 192730f712c9SDave Chinner if (error) { 192830f712c9SDave Chinner xfs_warn(mp, "%s: xfs_inobt_get_rec() returned error %d.", 192930f712c9SDave Chinner __func__, error); 193030f712c9SDave Chinner goto error0; 193130f712c9SDave Chinner } 1932c29aad41SEric Sandeen XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0); 193330f712c9SDave Chinner /* 193430f712c9SDave Chinner * Get the offset in the inode chunk. 193530f712c9SDave Chinner */ 193630f712c9SDave Chinner off = agino - rec.ir_startino; 193730f712c9SDave Chinner ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK); 193830f712c9SDave Chinner ASSERT(!(rec.ir_free & XFS_INOBT_MASK(off))); 193930f712c9SDave Chinner /* 194030f712c9SDave Chinner * Mark the inode free & increment the count. 194130f712c9SDave Chinner */ 194230f712c9SDave Chinner rec.ir_free |= XFS_INOBT_MASK(off); 194330f712c9SDave Chinner rec.ir_freecount++; 194430f712c9SDave Chinner 194530f712c9SDave Chinner /* 1946999633d3SBrian Foster * When an inode chunk is free, it becomes eligible for removal. Don't 1947999633d3SBrian Foster * remove the chunk if the block size is large enough for multiple inode 1948999633d3SBrian Foster * chunks (that might not be free). 194930f712c9SDave Chinner */ 195030f712c9SDave Chinner if (!(mp->m_flags & XFS_MOUNT_IKEEP) && 1951999633d3SBrian Foster rec.ir_free == XFS_INOBT_ALL_FREE && 1952999633d3SBrian Foster mp->m_sb.sb_inopblock <= XFS_INODES_PER_CHUNK) { 195309b56604SBrian Foster xic->deleted = 1; 195409b56604SBrian Foster xic->first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino); 195509b56604SBrian Foster xic->alloc = xfs_inobt_irec_to_allocmask(&rec); 195630f712c9SDave Chinner 195730f712c9SDave Chinner /* 195830f712c9SDave Chinner * Remove the inode cluster from the AGI B+Tree, adjust the 195930f712c9SDave Chinner * AGI and Superblock inode counts, and mark the disk space 196030f712c9SDave Chinner * to be freed when the transaction is committed. 196130f712c9SDave Chinner */ 1962999633d3SBrian Foster ilen = rec.ir_freecount; 196330f712c9SDave Chinner be32_add_cpu(&agi->agi_count, -ilen); 196430f712c9SDave Chinner be32_add_cpu(&agi->agi_freecount, -(ilen - 1)); 196530f712c9SDave Chinner xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT); 196630f712c9SDave Chinner pag = xfs_perag_get(mp, agno); 196730f712c9SDave Chinner pag->pagi_freecount -= ilen - 1; 196830f712c9SDave Chinner xfs_perag_put(pag); 196930f712c9SDave Chinner xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen); 197030f712c9SDave Chinner xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1)); 197130f712c9SDave Chinner 197230f712c9SDave Chinner if ((error = xfs_btree_delete(cur, &i))) { 197330f712c9SDave Chinner xfs_warn(mp, "%s: xfs_btree_delete returned error %d.", 197430f712c9SDave Chinner __func__, error); 197530f712c9SDave Chinner goto error0; 197630f712c9SDave Chinner } 197730f712c9SDave Chinner 197810ae3dc7SBrian Foster xfs_difree_inode_chunk(mp, agno, &rec, flist); 197930f712c9SDave Chinner } else { 198009b56604SBrian Foster xic->deleted = 0; 198130f712c9SDave Chinner 198230f712c9SDave Chinner error = xfs_inobt_update(cur, &rec); 198330f712c9SDave Chinner if (error) { 198430f712c9SDave Chinner xfs_warn(mp, "%s: xfs_inobt_update returned error %d.", 198530f712c9SDave Chinner __func__, error); 198630f712c9SDave Chinner goto error0; 198730f712c9SDave Chinner } 198830f712c9SDave Chinner 198930f712c9SDave Chinner /* 199030f712c9SDave Chinner * Change the inode free counts and log the ag/sb changes. 199130f712c9SDave Chinner */ 199230f712c9SDave Chinner be32_add_cpu(&agi->agi_freecount, 1); 199330f712c9SDave Chinner xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT); 199430f712c9SDave Chinner pag = xfs_perag_get(mp, agno); 199530f712c9SDave Chinner pag->pagi_freecount++; 199630f712c9SDave Chinner xfs_perag_put(pag); 199730f712c9SDave Chinner xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1); 199830f712c9SDave Chinner } 199930f712c9SDave Chinner 200030f712c9SDave Chinner error = xfs_check_agi_freecount(cur, agi); 200130f712c9SDave Chinner if (error) 200230f712c9SDave Chinner goto error0; 200330f712c9SDave Chinner 200430f712c9SDave Chinner *orec = rec; 200530f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); 200630f712c9SDave Chinner return 0; 200730f712c9SDave Chinner 200830f712c9SDave Chinner error0: 200930f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); 201030f712c9SDave Chinner return error; 201130f712c9SDave Chinner } 201230f712c9SDave Chinner 201330f712c9SDave Chinner /* 201430f712c9SDave Chinner * Free an inode in the free inode btree. 201530f712c9SDave Chinner */ 201630f712c9SDave Chinner STATIC int 201730f712c9SDave Chinner xfs_difree_finobt( 201830f712c9SDave Chinner struct xfs_mount *mp, 201930f712c9SDave Chinner struct xfs_trans *tp, 202030f712c9SDave Chinner struct xfs_buf *agbp, 202130f712c9SDave Chinner xfs_agino_t agino, 202230f712c9SDave Chinner struct xfs_inobt_rec_incore *ibtrec) /* inobt record */ 202330f712c9SDave Chinner { 202430f712c9SDave Chinner struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp); 202530f712c9SDave Chinner xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno); 202630f712c9SDave Chinner struct xfs_btree_cur *cur; 202730f712c9SDave Chinner struct xfs_inobt_rec_incore rec; 202830f712c9SDave Chinner int offset = agino - ibtrec->ir_startino; 202930f712c9SDave Chinner int error; 203030f712c9SDave Chinner int i; 203130f712c9SDave Chinner 203230f712c9SDave Chinner cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_FINO); 203330f712c9SDave Chinner 203430f712c9SDave Chinner error = xfs_inobt_lookup(cur, ibtrec->ir_startino, XFS_LOOKUP_EQ, &i); 203530f712c9SDave Chinner if (error) 203630f712c9SDave Chinner goto error; 203730f712c9SDave Chinner if (i == 0) { 203830f712c9SDave Chinner /* 203930f712c9SDave Chinner * If the record does not exist in the finobt, we must have just 204030f712c9SDave Chinner * freed an inode in a previously fully allocated chunk. If not, 204130f712c9SDave Chinner * something is out of sync. 204230f712c9SDave Chinner */ 2043c29aad41SEric Sandeen XFS_WANT_CORRUPTED_GOTO(mp, ibtrec->ir_freecount == 1, error); 204430f712c9SDave Chinner 20455419040fSBrian Foster error = xfs_inobt_insert_rec(cur, ibtrec->ir_holemask, 20465419040fSBrian Foster ibtrec->ir_count, 20475419040fSBrian Foster ibtrec->ir_freecount, 204830f712c9SDave Chinner ibtrec->ir_free, &i); 204930f712c9SDave Chinner if (error) 205030f712c9SDave Chinner goto error; 205130f712c9SDave Chinner ASSERT(i == 1); 205230f712c9SDave Chinner 205330f712c9SDave Chinner goto out; 205430f712c9SDave Chinner } 205530f712c9SDave Chinner 205630f712c9SDave Chinner /* 205730f712c9SDave Chinner * Read and update the existing record. We could just copy the ibtrec 205830f712c9SDave Chinner * across here, but that would defeat the purpose of having redundant 205930f712c9SDave Chinner * metadata. By making the modifications independently, we can catch 206030f712c9SDave Chinner * corruptions that we wouldn't see if we just copied from one record 206130f712c9SDave Chinner * to another. 206230f712c9SDave Chinner */ 206330f712c9SDave Chinner error = xfs_inobt_get_rec(cur, &rec, &i); 206430f712c9SDave Chinner if (error) 206530f712c9SDave Chinner goto error; 2066c29aad41SEric Sandeen XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error); 206730f712c9SDave Chinner 206830f712c9SDave Chinner rec.ir_free |= XFS_INOBT_MASK(offset); 206930f712c9SDave Chinner rec.ir_freecount++; 207030f712c9SDave Chinner 2071c29aad41SEric Sandeen XFS_WANT_CORRUPTED_GOTO(mp, (rec.ir_free == ibtrec->ir_free) && 207230f712c9SDave Chinner (rec.ir_freecount == ibtrec->ir_freecount), 207330f712c9SDave Chinner error); 207430f712c9SDave Chinner 207530f712c9SDave Chinner /* 207630f712c9SDave Chinner * The content of inobt records should always match between the inobt 207730f712c9SDave Chinner * and finobt. The lifecycle of records in the finobt is different from 207830f712c9SDave Chinner * the inobt in that the finobt only tracks records with at least one 207930f712c9SDave Chinner * free inode. Hence, if all of the inodes are free and we aren't 208030f712c9SDave Chinner * keeping inode chunks permanently on disk, remove the record. 208130f712c9SDave Chinner * Otherwise, update the record with the new information. 2082999633d3SBrian Foster * 2083999633d3SBrian Foster * Note that we currently can't free chunks when the block size is large 2084999633d3SBrian Foster * enough for multiple chunks. Leave the finobt record to remain in sync 2085999633d3SBrian Foster * with the inobt. 208630f712c9SDave Chinner */ 2087999633d3SBrian Foster if (rec.ir_free == XFS_INOBT_ALL_FREE && 2088999633d3SBrian Foster mp->m_sb.sb_inopblock <= XFS_INODES_PER_CHUNK && 208930f712c9SDave Chinner !(mp->m_flags & XFS_MOUNT_IKEEP)) { 209030f712c9SDave Chinner error = xfs_btree_delete(cur, &i); 209130f712c9SDave Chinner if (error) 209230f712c9SDave Chinner goto error; 209330f712c9SDave Chinner ASSERT(i == 1); 209430f712c9SDave Chinner } else { 209530f712c9SDave Chinner error = xfs_inobt_update(cur, &rec); 209630f712c9SDave Chinner if (error) 209730f712c9SDave Chinner goto error; 209830f712c9SDave Chinner } 209930f712c9SDave Chinner 210030f712c9SDave Chinner out: 210130f712c9SDave Chinner error = xfs_check_agi_freecount(cur, agi); 210230f712c9SDave Chinner if (error) 210330f712c9SDave Chinner goto error; 210430f712c9SDave Chinner 210530f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); 210630f712c9SDave Chinner return 0; 210730f712c9SDave Chinner 210830f712c9SDave Chinner error: 210930f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); 211030f712c9SDave Chinner return error; 211130f712c9SDave Chinner } 211230f712c9SDave Chinner 211330f712c9SDave Chinner /* 211430f712c9SDave Chinner * Free disk inode. Carefully avoids touching the incore inode, all 211530f712c9SDave Chinner * manipulations incore are the caller's responsibility. 211630f712c9SDave Chinner * The on-disk inode is not changed by this operation, only the 211730f712c9SDave Chinner * btree (free inode mask) is changed. 211830f712c9SDave Chinner */ 211930f712c9SDave Chinner int 212030f712c9SDave Chinner xfs_difree( 212130f712c9SDave Chinner struct xfs_trans *tp, /* transaction pointer */ 212230f712c9SDave Chinner xfs_ino_t inode, /* inode to be freed */ 212330f712c9SDave Chinner struct xfs_bmap_free *flist, /* extents to free */ 212409b56604SBrian Foster struct xfs_icluster *xic) /* cluster info if deleted */ 212530f712c9SDave Chinner { 212630f712c9SDave Chinner /* REFERENCED */ 212730f712c9SDave Chinner xfs_agblock_t agbno; /* block number containing inode */ 212830f712c9SDave Chinner struct xfs_buf *agbp; /* buffer for allocation group header */ 212930f712c9SDave Chinner xfs_agino_t agino; /* allocation group inode number */ 213030f712c9SDave Chinner xfs_agnumber_t agno; /* allocation group number */ 213130f712c9SDave Chinner int error; /* error return value */ 213230f712c9SDave Chinner struct xfs_mount *mp; /* mount structure for filesystem */ 213330f712c9SDave Chinner struct xfs_inobt_rec_incore rec;/* btree record */ 213430f712c9SDave Chinner 213530f712c9SDave Chinner mp = tp->t_mountp; 213630f712c9SDave Chinner 213730f712c9SDave Chinner /* 213830f712c9SDave Chinner * Break up inode number into its components. 213930f712c9SDave Chinner */ 214030f712c9SDave Chinner agno = XFS_INO_TO_AGNO(mp, inode); 214130f712c9SDave Chinner if (agno >= mp->m_sb.sb_agcount) { 214230f712c9SDave Chinner xfs_warn(mp, "%s: agno >= mp->m_sb.sb_agcount (%d >= %d).", 214330f712c9SDave Chinner __func__, agno, mp->m_sb.sb_agcount); 214430f712c9SDave Chinner ASSERT(0); 21452451337dSDave Chinner return -EINVAL; 214630f712c9SDave Chinner } 214730f712c9SDave Chinner agino = XFS_INO_TO_AGINO(mp, inode); 214830f712c9SDave Chinner if (inode != XFS_AGINO_TO_INO(mp, agno, agino)) { 214930f712c9SDave Chinner xfs_warn(mp, "%s: inode != XFS_AGINO_TO_INO() (%llu != %llu).", 215030f712c9SDave Chinner __func__, (unsigned long long)inode, 215130f712c9SDave Chinner (unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino)); 215230f712c9SDave Chinner ASSERT(0); 21532451337dSDave Chinner return -EINVAL; 215430f712c9SDave Chinner } 215530f712c9SDave Chinner agbno = XFS_AGINO_TO_AGBNO(mp, agino); 215630f712c9SDave Chinner if (agbno >= mp->m_sb.sb_agblocks) { 215730f712c9SDave Chinner xfs_warn(mp, "%s: agbno >= mp->m_sb.sb_agblocks (%d >= %d).", 215830f712c9SDave Chinner __func__, agbno, mp->m_sb.sb_agblocks); 215930f712c9SDave Chinner ASSERT(0); 21602451337dSDave Chinner return -EINVAL; 216130f712c9SDave Chinner } 216230f712c9SDave Chinner /* 216330f712c9SDave Chinner * Get the allocation group header. 216430f712c9SDave Chinner */ 216530f712c9SDave Chinner error = xfs_ialloc_read_agi(mp, tp, agno, &agbp); 216630f712c9SDave Chinner if (error) { 216730f712c9SDave Chinner xfs_warn(mp, "%s: xfs_ialloc_read_agi() returned error %d.", 216830f712c9SDave Chinner __func__, error); 216930f712c9SDave Chinner return error; 217030f712c9SDave Chinner } 217130f712c9SDave Chinner 217230f712c9SDave Chinner /* 217330f712c9SDave Chinner * Fix up the inode allocation btree. 217430f712c9SDave Chinner */ 217509b56604SBrian Foster error = xfs_difree_inobt(mp, tp, agbp, agino, flist, xic, &rec); 217630f712c9SDave Chinner if (error) 217730f712c9SDave Chinner goto error0; 217830f712c9SDave Chinner 217930f712c9SDave Chinner /* 218030f712c9SDave Chinner * Fix up the free inode btree. 218130f712c9SDave Chinner */ 218230f712c9SDave Chinner if (xfs_sb_version_hasfinobt(&mp->m_sb)) { 218330f712c9SDave Chinner error = xfs_difree_finobt(mp, tp, agbp, agino, &rec); 218430f712c9SDave Chinner if (error) 218530f712c9SDave Chinner goto error0; 218630f712c9SDave Chinner } 218730f712c9SDave Chinner 218830f712c9SDave Chinner return 0; 218930f712c9SDave Chinner 219030f712c9SDave Chinner error0: 219130f712c9SDave Chinner return error; 219230f712c9SDave Chinner } 219330f712c9SDave Chinner 219430f712c9SDave Chinner STATIC int 219530f712c9SDave Chinner xfs_imap_lookup( 219630f712c9SDave Chinner struct xfs_mount *mp, 219730f712c9SDave Chinner struct xfs_trans *tp, 219830f712c9SDave Chinner xfs_agnumber_t agno, 219930f712c9SDave Chinner xfs_agino_t agino, 220030f712c9SDave Chinner xfs_agblock_t agbno, 220130f712c9SDave Chinner xfs_agblock_t *chunk_agbno, 220230f712c9SDave Chinner xfs_agblock_t *offset_agbno, 220330f712c9SDave Chinner int flags) 220430f712c9SDave Chinner { 220530f712c9SDave Chinner struct xfs_inobt_rec_incore rec; 220630f712c9SDave Chinner struct xfs_btree_cur *cur; 220730f712c9SDave Chinner struct xfs_buf *agbp; 220830f712c9SDave Chinner int error; 220930f712c9SDave Chinner int i; 221030f712c9SDave Chinner 221130f712c9SDave Chinner error = xfs_ialloc_read_agi(mp, tp, agno, &agbp); 221230f712c9SDave Chinner if (error) { 221330f712c9SDave Chinner xfs_alert(mp, 221430f712c9SDave Chinner "%s: xfs_ialloc_read_agi() returned error %d, agno %d", 221530f712c9SDave Chinner __func__, error, agno); 221630f712c9SDave Chinner return error; 221730f712c9SDave Chinner } 221830f712c9SDave Chinner 221930f712c9SDave Chinner /* 222030f712c9SDave Chinner * Lookup the inode record for the given agino. If the record cannot be 222130f712c9SDave Chinner * found, then it's an invalid inode number and we should abort. Once 222230f712c9SDave Chinner * we have a record, we need to ensure it contains the inode number 222330f712c9SDave Chinner * we are looking up. 222430f712c9SDave Chinner */ 222530f712c9SDave Chinner cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO); 222630f712c9SDave Chinner error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i); 222730f712c9SDave Chinner if (!error) { 222830f712c9SDave Chinner if (i) 222930f712c9SDave Chinner error = xfs_inobt_get_rec(cur, &rec, &i); 223030f712c9SDave Chinner if (!error && i == 0) 22312451337dSDave Chinner error = -EINVAL; 223230f712c9SDave Chinner } 223330f712c9SDave Chinner 223430f712c9SDave Chinner xfs_trans_brelse(tp, agbp); 2235*f307080aSBrian Foster xfs_btree_del_cursor(cur, error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); 223630f712c9SDave Chinner if (error) 223730f712c9SDave Chinner return error; 223830f712c9SDave Chinner 223930f712c9SDave Chinner /* check that the returned record contains the required inode */ 224030f712c9SDave Chinner if (rec.ir_startino > agino || 224130f712c9SDave Chinner rec.ir_startino + mp->m_ialloc_inos <= agino) 22422451337dSDave Chinner return -EINVAL; 224330f712c9SDave Chinner 224430f712c9SDave Chinner /* for untrusted inodes check it is allocated first */ 224530f712c9SDave Chinner if ((flags & XFS_IGET_UNTRUSTED) && 224630f712c9SDave Chinner (rec.ir_free & XFS_INOBT_MASK(agino - rec.ir_startino))) 22472451337dSDave Chinner return -EINVAL; 224830f712c9SDave Chinner 224930f712c9SDave Chinner *chunk_agbno = XFS_AGINO_TO_AGBNO(mp, rec.ir_startino); 225030f712c9SDave Chinner *offset_agbno = agbno - *chunk_agbno; 225130f712c9SDave Chinner return 0; 225230f712c9SDave Chinner } 225330f712c9SDave Chinner 225430f712c9SDave Chinner /* 225530f712c9SDave Chinner * Return the location of the inode in imap, for mapping it into a buffer. 225630f712c9SDave Chinner */ 225730f712c9SDave Chinner int 225830f712c9SDave Chinner xfs_imap( 225930f712c9SDave Chinner xfs_mount_t *mp, /* file system mount structure */ 226030f712c9SDave Chinner xfs_trans_t *tp, /* transaction pointer */ 226130f712c9SDave Chinner xfs_ino_t ino, /* inode to locate */ 226230f712c9SDave Chinner struct xfs_imap *imap, /* location map structure */ 226330f712c9SDave Chinner uint flags) /* flags for inode btree lookup */ 226430f712c9SDave Chinner { 226530f712c9SDave Chinner xfs_agblock_t agbno; /* block number of inode in the alloc group */ 226630f712c9SDave Chinner xfs_agino_t agino; /* inode number within alloc group */ 226730f712c9SDave Chinner xfs_agnumber_t agno; /* allocation group number */ 226830f712c9SDave Chinner int blks_per_cluster; /* num blocks per inode cluster */ 226930f712c9SDave Chinner xfs_agblock_t chunk_agbno; /* first block in inode chunk */ 227030f712c9SDave Chinner xfs_agblock_t cluster_agbno; /* first block in inode cluster */ 227130f712c9SDave Chinner int error; /* error code */ 227230f712c9SDave Chinner int offset; /* index of inode in its buffer */ 227330f712c9SDave Chinner xfs_agblock_t offset_agbno; /* blks from chunk start to inode */ 227430f712c9SDave Chinner 227530f712c9SDave Chinner ASSERT(ino != NULLFSINO); 227630f712c9SDave Chinner 227730f712c9SDave Chinner /* 227830f712c9SDave Chinner * Split up the inode number into its parts. 227930f712c9SDave Chinner */ 228030f712c9SDave Chinner agno = XFS_INO_TO_AGNO(mp, ino); 228130f712c9SDave Chinner agino = XFS_INO_TO_AGINO(mp, ino); 228230f712c9SDave Chinner agbno = XFS_AGINO_TO_AGBNO(mp, agino); 228330f712c9SDave Chinner if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks || 228430f712c9SDave Chinner ino != XFS_AGINO_TO_INO(mp, agno, agino)) { 228530f712c9SDave Chinner #ifdef DEBUG 228630f712c9SDave Chinner /* 228730f712c9SDave Chinner * Don't output diagnostic information for untrusted inodes 228830f712c9SDave Chinner * as they can be invalid without implying corruption. 228930f712c9SDave Chinner */ 229030f712c9SDave Chinner if (flags & XFS_IGET_UNTRUSTED) 22912451337dSDave Chinner return -EINVAL; 229230f712c9SDave Chinner if (agno >= mp->m_sb.sb_agcount) { 229330f712c9SDave Chinner xfs_alert(mp, 229430f712c9SDave Chinner "%s: agno (%d) >= mp->m_sb.sb_agcount (%d)", 229530f712c9SDave Chinner __func__, agno, mp->m_sb.sb_agcount); 229630f712c9SDave Chinner } 229730f712c9SDave Chinner if (agbno >= mp->m_sb.sb_agblocks) { 229830f712c9SDave Chinner xfs_alert(mp, 229930f712c9SDave Chinner "%s: agbno (0x%llx) >= mp->m_sb.sb_agblocks (0x%lx)", 230030f712c9SDave Chinner __func__, (unsigned long long)agbno, 230130f712c9SDave Chinner (unsigned long)mp->m_sb.sb_agblocks); 230230f712c9SDave Chinner } 230330f712c9SDave Chinner if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) { 230430f712c9SDave Chinner xfs_alert(mp, 230530f712c9SDave Chinner "%s: ino (0x%llx) != XFS_AGINO_TO_INO() (0x%llx)", 230630f712c9SDave Chinner __func__, ino, 230730f712c9SDave Chinner XFS_AGINO_TO_INO(mp, agno, agino)); 230830f712c9SDave Chinner } 230930f712c9SDave Chinner xfs_stack_trace(); 231030f712c9SDave Chinner #endif /* DEBUG */ 23112451337dSDave Chinner return -EINVAL; 231230f712c9SDave Chinner } 231330f712c9SDave Chinner 231430f712c9SDave Chinner blks_per_cluster = xfs_icluster_size_fsb(mp); 231530f712c9SDave Chinner 231630f712c9SDave Chinner /* 231730f712c9SDave Chinner * For bulkstat and handle lookups, we have an untrusted inode number 231830f712c9SDave Chinner * that we have to verify is valid. We cannot do this just by reading 231930f712c9SDave Chinner * the inode buffer as it may have been unlinked and removed leaving 232030f712c9SDave Chinner * inodes in stale state on disk. Hence we have to do a btree lookup 232130f712c9SDave Chinner * in all cases where an untrusted inode number is passed. 232230f712c9SDave Chinner */ 232330f712c9SDave Chinner if (flags & XFS_IGET_UNTRUSTED) { 232430f712c9SDave Chinner error = xfs_imap_lookup(mp, tp, agno, agino, agbno, 232530f712c9SDave Chinner &chunk_agbno, &offset_agbno, flags); 232630f712c9SDave Chinner if (error) 232730f712c9SDave Chinner return error; 232830f712c9SDave Chinner goto out_map; 232930f712c9SDave Chinner } 233030f712c9SDave Chinner 233130f712c9SDave Chinner /* 233230f712c9SDave Chinner * If the inode cluster size is the same as the blocksize or 233330f712c9SDave Chinner * smaller we get to the buffer by simple arithmetics. 233430f712c9SDave Chinner */ 233530f712c9SDave Chinner if (blks_per_cluster == 1) { 233630f712c9SDave Chinner offset = XFS_INO_TO_OFFSET(mp, ino); 233730f712c9SDave Chinner ASSERT(offset < mp->m_sb.sb_inopblock); 233830f712c9SDave Chinner 233930f712c9SDave Chinner imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, agbno); 234030f712c9SDave Chinner imap->im_len = XFS_FSB_TO_BB(mp, 1); 234130f712c9SDave Chinner imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog); 234230f712c9SDave Chinner return 0; 234330f712c9SDave Chinner } 234430f712c9SDave Chinner 234530f712c9SDave Chinner /* 234630f712c9SDave Chinner * If the inode chunks are aligned then use simple maths to 234730f712c9SDave Chinner * find the location. Otherwise we have to do a btree 234830f712c9SDave Chinner * lookup to find the location. 234930f712c9SDave Chinner */ 235030f712c9SDave Chinner if (mp->m_inoalign_mask) { 235130f712c9SDave Chinner offset_agbno = agbno & mp->m_inoalign_mask; 235230f712c9SDave Chinner chunk_agbno = agbno - offset_agbno; 235330f712c9SDave Chinner } else { 235430f712c9SDave Chinner error = xfs_imap_lookup(mp, tp, agno, agino, agbno, 235530f712c9SDave Chinner &chunk_agbno, &offset_agbno, flags); 235630f712c9SDave Chinner if (error) 235730f712c9SDave Chinner return error; 235830f712c9SDave Chinner } 235930f712c9SDave Chinner 236030f712c9SDave Chinner out_map: 236130f712c9SDave Chinner ASSERT(agbno >= chunk_agbno); 236230f712c9SDave Chinner cluster_agbno = chunk_agbno + 236330f712c9SDave Chinner ((offset_agbno / blks_per_cluster) * blks_per_cluster); 236430f712c9SDave Chinner offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) + 236530f712c9SDave Chinner XFS_INO_TO_OFFSET(mp, ino); 236630f712c9SDave Chinner 236730f712c9SDave Chinner imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, cluster_agbno); 236830f712c9SDave Chinner imap->im_len = XFS_FSB_TO_BB(mp, blks_per_cluster); 236930f712c9SDave Chinner imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog); 237030f712c9SDave Chinner 237130f712c9SDave Chinner /* 237230f712c9SDave Chinner * If the inode number maps to a block outside the bounds 237330f712c9SDave Chinner * of the file system then return NULL rather than calling 237430f712c9SDave Chinner * read_buf and panicing when we get an error from the 237530f712c9SDave Chinner * driver. 237630f712c9SDave Chinner */ 237730f712c9SDave Chinner if ((imap->im_blkno + imap->im_len) > 237830f712c9SDave Chinner XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) { 237930f712c9SDave Chinner xfs_alert(mp, 238030f712c9SDave Chinner "%s: (im_blkno (0x%llx) + im_len (0x%llx)) > sb_dblocks (0x%llx)", 238130f712c9SDave Chinner __func__, (unsigned long long) imap->im_blkno, 238230f712c9SDave Chinner (unsigned long long) imap->im_len, 238330f712c9SDave Chinner XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)); 23842451337dSDave Chinner return -EINVAL; 238530f712c9SDave Chinner } 238630f712c9SDave Chinner return 0; 238730f712c9SDave Chinner } 238830f712c9SDave Chinner 238930f712c9SDave Chinner /* 239030f712c9SDave Chinner * Compute and fill in value of m_in_maxlevels. 239130f712c9SDave Chinner */ 239230f712c9SDave Chinner void 239330f712c9SDave Chinner xfs_ialloc_compute_maxlevels( 239430f712c9SDave Chinner xfs_mount_t *mp) /* file system mount structure */ 239530f712c9SDave Chinner { 239630f712c9SDave Chinner int level; 239730f712c9SDave Chinner uint maxblocks; 239830f712c9SDave Chinner uint maxleafents; 239930f712c9SDave Chinner int minleafrecs; 240030f712c9SDave Chinner int minnoderecs; 240130f712c9SDave Chinner 240230f712c9SDave Chinner maxleafents = (1LL << XFS_INO_AGINO_BITS(mp)) >> 240330f712c9SDave Chinner XFS_INODES_PER_CHUNK_LOG; 240430f712c9SDave Chinner minleafrecs = mp->m_alloc_mnr[0]; 240530f712c9SDave Chinner minnoderecs = mp->m_alloc_mnr[1]; 240630f712c9SDave Chinner maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs; 240730f712c9SDave Chinner for (level = 1; maxblocks > 1; level++) 240830f712c9SDave Chinner maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs; 240930f712c9SDave Chinner mp->m_in_maxlevels = level; 241030f712c9SDave Chinner } 241130f712c9SDave Chinner 241230f712c9SDave Chinner /* 241330f712c9SDave Chinner * Log specified fields for the ag hdr (inode section). The growth of the agi 241430f712c9SDave Chinner * structure over time requires that we interpret the buffer as two logical 241530f712c9SDave Chinner * regions delineated by the end of the unlinked list. This is due to the size 241630f712c9SDave Chinner * of the hash table and its location in the middle of the agi. 241730f712c9SDave Chinner * 241830f712c9SDave Chinner * For example, a request to log a field before agi_unlinked and a field after 241930f712c9SDave Chinner * agi_unlinked could cause us to log the entire hash table and use an excessive 242030f712c9SDave Chinner * amount of log space. To avoid this behavior, log the region up through 242130f712c9SDave Chinner * agi_unlinked in one call and the region after agi_unlinked through the end of 242230f712c9SDave Chinner * the structure in another. 242330f712c9SDave Chinner */ 242430f712c9SDave Chinner void 242530f712c9SDave Chinner xfs_ialloc_log_agi( 242630f712c9SDave Chinner xfs_trans_t *tp, /* transaction pointer */ 242730f712c9SDave Chinner xfs_buf_t *bp, /* allocation group header buffer */ 242830f712c9SDave Chinner int fields) /* bitmask of fields to log */ 242930f712c9SDave Chinner { 243030f712c9SDave Chinner int first; /* first byte number */ 243130f712c9SDave Chinner int last; /* last byte number */ 243230f712c9SDave Chinner static const short offsets[] = { /* field starting offsets */ 243330f712c9SDave Chinner /* keep in sync with bit definitions */ 243430f712c9SDave Chinner offsetof(xfs_agi_t, agi_magicnum), 243530f712c9SDave Chinner offsetof(xfs_agi_t, agi_versionnum), 243630f712c9SDave Chinner offsetof(xfs_agi_t, agi_seqno), 243730f712c9SDave Chinner offsetof(xfs_agi_t, agi_length), 243830f712c9SDave Chinner offsetof(xfs_agi_t, agi_count), 243930f712c9SDave Chinner offsetof(xfs_agi_t, agi_root), 244030f712c9SDave Chinner offsetof(xfs_agi_t, agi_level), 244130f712c9SDave Chinner offsetof(xfs_agi_t, agi_freecount), 244230f712c9SDave Chinner offsetof(xfs_agi_t, agi_newino), 244330f712c9SDave Chinner offsetof(xfs_agi_t, agi_dirino), 244430f712c9SDave Chinner offsetof(xfs_agi_t, agi_unlinked), 244530f712c9SDave Chinner offsetof(xfs_agi_t, agi_free_root), 244630f712c9SDave Chinner offsetof(xfs_agi_t, agi_free_level), 244730f712c9SDave Chinner sizeof(xfs_agi_t) 244830f712c9SDave Chinner }; 244930f712c9SDave Chinner #ifdef DEBUG 245030f712c9SDave Chinner xfs_agi_t *agi; /* allocation group header */ 245130f712c9SDave Chinner 245230f712c9SDave Chinner agi = XFS_BUF_TO_AGI(bp); 245330f712c9SDave Chinner ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC)); 245430f712c9SDave Chinner #endif 245530f712c9SDave Chinner 245630f712c9SDave Chinner xfs_trans_buf_set_type(tp, bp, XFS_BLFT_AGI_BUF); 245730f712c9SDave Chinner 245830f712c9SDave Chinner /* 245930f712c9SDave Chinner * Compute byte offsets for the first and last fields in the first 246030f712c9SDave Chinner * region and log the agi buffer. This only logs up through 246130f712c9SDave Chinner * agi_unlinked. 246230f712c9SDave Chinner */ 246330f712c9SDave Chinner if (fields & XFS_AGI_ALL_BITS_R1) { 246430f712c9SDave Chinner xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R1, 246530f712c9SDave Chinner &first, &last); 246630f712c9SDave Chinner xfs_trans_log_buf(tp, bp, first, last); 246730f712c9SDave Chinner } 246830f712c9SDave Chinner 246930f712c9SDave Chinner /* 247030f712c9SDave Chinner * Mask off the bits in the first region and calculate the first and 247130f712c9SDave Chinner * last field offsets for any bits in the second region. 247230f712c9SDave Chinner */ 247330f712c9SDave Chinner fields &= ~XFS_AGI_ALL_BITS_R1; 247430f712c9SDave Chinner if (fields) { 247530f712c9SDave Chinner xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R2, 247630f712c9SDave Chinner &first, &last); 247730f712c9SDave Chinner xfs_trans_log_buf(tp, bp, first, last); 247830f712c9SDave Chinner } 247930f712c9SDave Chinner } 248030f712c9SDave Chinner 248130f712c9SDave Chinner #ifdef DEBUG 248230f712c9SDave Chinner STATIC void 248330f712c9SDave Chinner xfs_check_agi_unlinked( 248430f712c9SDave Chinner struct xfs_agi *agi) 248530f712c9SDave Chinner { 248630f712c9SDave Chinner int i; 248730f712c9SDave Chinner 248830f712c9SDave Chinner for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++) 248930f712c9SDave Chinner ASSERT(agi->agi_unlinked[i]); 249030f712c9SDave Chinner } 249130f712c9SDave Chinner #else 249230f712c9SDave Chinner #define xfs_check_agi_unlinked(agi) 249330f712c9SDave Chinner #endif 249430f712c9SDave Chinner 249530f712c9SDave Chinner static bool 249630f712c9SDave Chinner xfs_agi_verify( 249730f712c9SDave Chinner struct xfs_buf *bp) 249830f712c9SDave Chinner { 249930f712c9SDave Chinner struct xfs_mount *mp = bp->b_target->bt_mount; 250030f712c9SDave Chinner struct xfs_agi *agi = XFS_BUF_TO_AGI(bp); 250130f712c9SDave Chinner 250230f712c9SDave Chinner if (xfs_sb_version_hascrc(&mp->m_sb) && 250330f712c9SDave Chinner !uuid_equal(&agi->agi_uuid, &mp->m_sb.sb_uuid)) 250430f712c9SDave Chinner return false; 250530f712c9SDave Chinner /* 250630f712c9SDave Chinner * Validate the magic number of the agi block. 250730f712c9SDave Chinner */ 250830f712c9SDave Chinner if (agi->agi_magicnum != cpu_to_be32(XFS_AGI_MAGIC)) 250930f712c9SDave Chinner return false; 251030f712c9SDave Chinner if (!XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum))) 251130f712c9SDave Chinner return false; 251230f712c9SDave Chinner 2513e1b05723SEric Sandeen if (be32_to_cpu(agi->agi_level) > XFS_BTREE_MAXLEVELS) 2514e1b05723SEric Sandeen return false; 251530f712c9SDave Chinner /* 251630f712c9SDave Chinner * during growfs operations, the perag is not fully initialised, 251730f712c9SDave Chinner * so we can't use it for any useful checking. growfs ensures we can't 251830f712c9SDave Chinner * use it by using uncached buffers that don't have the perag attached 251930f712c9SDave Chinner * so we can detect and avoid this problem. 252030f712c9SDave Chinner */ 252130f712c9SDave Chinner if (bp->b_pag && be32_to_cpu(agi->agi_seqno) != bp->b_pag->pag_agno) 252230f712c9SDave Chinner return false; 252330f712c9SDave Chinner 252430f712c9SDave Chinner xfs_check_agi_unlinked(agi); 252530f712c9SDave Chinner return true; 252630f712c9SDave Chinner } 252730f712c9SDave Chinner 252830f712c9SDave Chinner static void 252930f712c9SDave Chinner xfs_agi_read_verify( 253030f712c9SDave Chinner struct xfs_buf *bp) 253130f712c9SDave Chinner { 253230f712c9SDave Chinner struct xfs_mount *mp = bp->b_target->bt_mount; 253330f712c9SDave Chinner 253430f712c9SDave Chinner if (xfs_sb_version_hascrc(&mp->m_sb) && 253530f712c9SDave Chinner !xfs_buf_verify_cksum(bp, XFS_AGI_CRC_OFF)) 25362451337dSDave Chinner xfs_buf_ioerror(bp, -EFSBADCRC); 253730f712c9SDave Chinner else if (XFS_TEST_ERROR(!xfs_agi_verify(bp), mp, 253830f712c9SDave Chinner XFS_ERRTAG_IALLOC_READ_AGI, 253930f712c9SDave Chinner XFS_RANDOM_IALLOC_READ_AGI)) 25402451337dSDave Chinner xfs_buf_ioerror(bp, -EFSCORRUPTED); 254130f712c9SDave Chinner 254230f712c9SDave Chinner if (bp->b_error) 254330f712c9SDave Chinner xfs_verifier_error(bp); 254430f712c9SDave Chinner } 254530f712c9SDave Chinner 254630f712c9SDave Chinner static void 254730f712c9SDave Chinner xfs_agi_write_verify( 254830f712c9SDave Chinner struct xfs_buf *bp) 254930f712c9SDave Chinner { 255030f712c9SDave Chinner struct xfs_mount *mp = bp->b_target->bt_mount; 255130f712c9SDave Chinner struct xfs_buf_log_item *bip = bp->b_fspriv; 255230f712c9SDave Chinner 255330f712c9SDave Chinner if (!xfs_agi_verify(bp)) { 25542451337dSDave Chinner xfs_buf_ioerror(bp, -EFSCORRUPTED); 255530f712c9SDave Chinner xfs_verifier_error(bp); 255630f712c9SDave Chinner return; 255730f712c9SDave Chinner } 255830f712c9SDave Chinner 255930f712c9SDave Chinner if (!xfs_sb_version_hascrc(&mp->m_sb)) 256030f712c9SDave Chinner return; 256130f712c9SDave Chinner 256230f712c9SDave Chinner if (bip) 256330f712c9SDave Chinner XFS_BUF_TO_AGI(bp)->agi_lsn = cpu_to_be64(bip->bli_item.li_lsn); 256430f712c9SDave Chinner xfs_buf_update_cksum(bp, XFS_AGI_CRC_OFF); 256530f712c9SDave Chinner } 256630f712c9SDave Chinner 256730f712c9SDave Chinner const struct xfs_buf_ops xfs_agi_buf_ops = { 256830f712c9SDave Chinner .verify_read = xfs_agi_read_verify, 256930f712c9SDave Chinner .verify_write = xfs_agi_write_verify, 257030f712c9SDave Chinner }; 257130f712c9SDave Chinner 257230f712c9SDave Chinner /* 257330f712c9SDave Chinner * Read in the allocation group header (inode allocation section) 257430f712c9SDave Chinner */ 257530f712c9SDave Chinner int 257630f712c9SDave Chinner xfs_read_agi( 257730f712c9SDave Chinner struct xfs_mount *mp, /* file system mount structure */ 257830f712c9SDave Chinner struct xfs_trans *tp, /* transaction pointer */ 257930f712c9SDave Chinner xfs_agnumber_t agno, /* allocation group number */ 258030f712c9SDave Chinner struct xfs_buf **bpp) /* allocation group hdr buf */ 258130f712c9SDave Chinner { 258230f712c9SDave Chinner int error; 258330f712c9SDave Chinner 258430f712c9SDave Chinner trace_xfs_read_agi(mp, agno); 258530f712c9SDave Chinner 258630f712c9SDave Chinner ASSERT(agno != NULLAGNUMBER); 258730f712c9SDave Chinner error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, 258830f712c9SDave Chinner XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)), 258930f712c9SDave Chinner XFS_FSS_TO_BB(mp, 1), 0, bpp, &xfs_agi_buf_ops); 259030f712c9SDave Chinner if (error) 259130f712c9SDave Chinner return error; 259230f712c9SDave Chinner 259330f712c9SDave Chinner xfs_buf_set_ref(*bpp, XFS_AGI_REF); 259430f712c9SDave Chinner return 0; 259530f712c9SDave Chinner } 259630f712c9SDave Chinner 259730f712c9SDave Chinner int 259830f712c9SDave Chinner xfs_ialloc_read_agi( 259930f712c9SDave Chinner struct xfs_mount *mp, /* file system mount structure */ 260030f712c9SDave Chinner struct xfs_trans *tp, /* transaction pointer */ 260130f712c9SDave Chinner xfs_agnumber_t agno, /* allocation group number */ 260230f712c9SDave Chinner struct xfs_buf **bpp) /* allocation group hdr buf */ 260330f712c9SDave Chinner { 260430f712c9SDave Chinner struct xfs_agi *agi; /* allocation group header */ 260530f712c9SDave Chinner struct xfs_perag *pag; /* per allocation group data */ 260630f712c9SDave Chinner int error; 260730f712c9SDave Chinner 260830f712c9SDave Chinner trace_xfs_ialloc_read_agi(mp, agno); 260930f712c9SDave Chinner 261030f712c9SDave Chinner error = xfs_read_agi(mp, tp, agno, bpp); 261130f712c9SDave Chinner if (error) 261230f712c9SDave Chinner return error; 261330f712c9SDave Chinner 261430f712c9SDave Chinner agi = XFS_BUF_TO_AGI(*bpp); 261530f712c9SDave Chinner pag = xfs_perag_get(mp, agno); 261630f712c9SDave Chinner if (!pag->pagi_init) { 261730f712c9SDave Chinner pag->pagi_freecount = be32_to_cpu(agi->agi_freecount); 261830f712c9SDave Chinner pag->pagi_count = be32_to_cpu(agi->agi_count); 261930f712c9SDave Chinner pag->pagi_init = 1; 262030f712c9SDave Chinner } 262130f712c9SDave Chinner 262230f712c9SDave Chinner /* 262330f712c9SDave Chinner * It's possible for these to be out of sync if 262430f712c9SDave Chinner * we are in the middle of a forced shutdown. 262530f712c9SDave Chinner */ 262630f712c9SDave Chinner ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) || 262730f712c9SDave Chinner XFS_FORCED_SHUTDOWN(mp)); 262830f712c9SDave Chinner xfs_perag_put(pag); 262930f712c9SDave Chinner return 0; 263030f712c9SDave Chinner } 263130f712c9SDave Chinner 263230f712c9SDave Chinner /* 263330f712c9SDave Chinner * Read in the agi to initialise the per-ag data in the mount structure 263430f712c9SDave Chinner */ 263530f712c9SDave Chinner int 263630f712c9SDave Chinner xfs_ialloc_pagi_init( 263730f712c9SDave Chinner xfs_mount_t *mp, /* file system mount structure */ 263830f712c9SDave Chinner xfs_trans_t *tp, /* transaction pointer */ 263930f712c9SDave Chinner xfs_agnumber_t agno) /* allocation group number */ 264030f712c9SDave Chinner { 264130f712c9SDave Chinner xfs_buf_t *bp = NULL; 264230f712c9SDave Chinner int error; 264330f712c9SDave Chinner 264430f712c9SDave Chinner error = xfs_ialloc_read_agi(mp, tp, agno, &bp); 264530f712c9SDave Chinner if (error) 264630f712c9SDave Chinner return error; 264730f712c9SDave Chinner if (bp) 264830f712c9SDave Chinner xfs_trans_brelse(tp, bp); 264930f712c9SDave Chinner return 0; 265030f712c9SDave Chinner } 2651