10b61f8a4SDave Chinner // SPDX-License-Identifier: GPL-2.0 230f712c9SDave Chinner /* 330f712c9SDave Chinner * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc. 430f712c9SDave Chinner * All Rights Reserved. 530f712c9SDave Chinner */ 630f712c9SDave Chinner #include "xfs.h" 730f712c9SDave Chinner #include "xfs_fs.h" 830f712c9SDave Chinner #include "xfs_shared.h" 930f712c9SDave Chinner #include "xfs_format.h" 1030f712c9SDave Chinner #include "xfs_log_format.h" 1130f712c9SDave Chinner #include "xfs_trans_resv.h" 1230f712c9SDave Chinner #include "xfs_bit.h" 1330f712c9SDave Chinner #include "xfs_mount.h" 1430f712c9SDave Chinner #include "xfs_inode.h" 1530f712c9SDave Chinner #include "xfs_btree.h" 1630f712c9SDave Chinner #include "xfs_ialloc.h" 1730f712c9SDave Chinner #include "xfs_ialloc_btree.h" 1830f712c9SDave Chinner #include "xfs_alloc.h" 19e9e899a2SDarrick J. Wong #include "xfs_errortag.h" 2030f712c9SDave Chinner #include "xfs_error.h" 2130f712c9SDave Chinner #include "xfs_bmap.h" 2230f712c9SDave Chinner #include "xfs_trans.h" 2330f712c9SDave Chinner #include "xfs_buf_item.h" 2430f712c9SDave Chinner #include "xfs_icreate_item.h" 2530f712c9SDave Chinner #include "xfs_icache.h" 2630f712c9SDave Chinner #include "xfs_trace.h" 27a45086e2SBrian Foster #include "xfs_log.h" 28340785ccSDarrick J. Wong #include "xfs_rmap.h" 299bbafc71SDave Chinner #include "xfs_ag.h" 3030f712c9SDave Chinner 3130f712c9SDave Chinner /* 3230f712c9SDave Chinner * Lookup a record by ino in the btree given by cur. 3330f712c9SDave Chinner */ 3430f712c9SDave Chinner int /* error */ 3530f712c9SDave Chinner xfs_inobt_lookup( 3630f712c9SDave Chinner struct xfs_btree_cur *cur, /* btree cursor */ 3730f712c9SDave Chinner xfs_agino_t ino, /* starting inode of chunk */ 3830f712c9SDave Chinner xfs_lookup_t dir, /* <=, >=, == */ 3930f712c9SDave Chinner int *stat) /* success/failure */ 4030f712c9SDave Chinner { 4130f712c9SDave Chinner cur->bc_rec.i.ir_startino = ino; 425419040fSBrian Foster cur->bc_rec.i.ir_holemask = 0; 435419040fSBrian Foster cur->bc_rec.i.ir_count = 0; 4430f712c9SDave Chinner cur->bc_rec.i.ir_freecount = 0; 4530f712c9SDave Chinner cur->bc_rec.i.ir_free = 0; 4630f712c9SDave Chinner return xfs_btree_lookup(cur, dir, stat); 4730f712c9SDave Chinner } 4830f712c9SDave Chinner 4930f712c9SDave Chinner /* 5030f712c9SDave Chinner * Update the record referred to by cur to the value given. 5130f712c9SDave Chinner * This either works (return 0) or gets an EFSCORRUPTED error. 5230f712c9SDave Chinner */ 5330f712c9SDave Chinner STATIC int /* error */ 5430f712c9SDave Chinner xfs_inobt_update( 5530f712c9SDave Chinner struct xfs_btree_cur *cur, /* btree cursor */ 5630f712c9SDave Chinner xfs_inobt_rec_incore_t *irec) /* btree record */ 5730f712c9SDave Chinner { 5830f712c9SDave Chinner union xfs_btree_rec rec; 5930f712c9SDave Chinner 6030f712c9SDave Chinner rec.inobt.ir_startino = cpu_to_be32(irec->ir_startino); 615419040fSBrian Foster if (xfs_sb_version_hassparseinodes(&cur->bc_mp->m_sb)) { 625419040fSBrian Foster rec.inobt.ir_u.sp.ir_holemask = cpu_to_be16(irec->ir_holemask); 635419040fSBrian Foster rec.inobt.ir_u.sp.ir_count = irec->ir_count; 645419040fSBrian Foster rec.inobt.ir_u.sp.ir_freecount = irec->ir_freecount; 655419040fSBrian Foster } else { 665419040fSBrian Foster /* ir_holemask/ir_count not supported on-disk */ 675419040fSBrian Foster rec.inobt.ir_u.f.ir_freecount = cpu_to_be32(irec->ir_freecount); 685419040fSBrian Foster } 6930f712c9SDave Chinner rec.inobt.ir_free = cpu_to_be64(irec->ir_free); 7030f712c9SDave Chinner return xfs_btree_update(cur, &rec); 7130f712c9SDave Chinner } 7230f712c9SDave Chinner 73e936945eSDarrick J. Wong /* Convert on-disk btree record to incore inobt record. */ 74e936945eSDarrick J. Wong void 75e936945eSDarrick J. Wong xfs_inobt_btrec_to_irec( 76e936945eSDarrick J. Wong struct xfs_mount *mp, 77e936945eSDarrick J. Wong union xfs_btree_rec *rec, 78e936945eSDarrick J. Wong struct xfs_inobt_rec_incore *irec) 7930f712c9SDave Chinner { 805419040fSBrian Foster irec->ir_startino = be32_to_cpu(rec->inobt.ir_startino); 81e936945eSDarrick J. Wong if (xfs_sb_version_hassparseinodes(&mp->m_sb)) { 825419040fSBrian Foster irec->ir_holemask = be16_to_cpu(rec->inobt.ir_u.sp.ir_holemask); 835419040fSBrian Foster irec->ir_count = rec->inobt.ir_u.sp.ir_count; 845419040fSBrian Foster irec->ir_freecount = rec->inobt.ir_u.sp.ir_freecount; 855419040fSBrian Foster } else { 865419040fSBrian Foster /* 875419040fSBrian Foster * ir_holemask/ir_count not supported on-disk. Fill in hardcoded 885419040fSBrian Foster * values for full inode chunks. 895419040fSBrian Foster */ 905419040fSBrian Foster irec->ir_holemask = XFS_INOBT_HOLEMASK_FULL; 915419040fSBrian Foster irec->ir_count = XFS_INODES_PER_CHUNK; 925419040fSBrian Foster irec->ir_freecount = 935419040fSBrian Foster be32_to_cpu(rec->inobt.ir_u.f.ir_freecount); 945419040fSBrian Foster } 955419040fSBrian Foster irec->ir_free = be64_to_cpu(rec->inobt.ir_free); 96e936945eSDarrick J. Wong } 97e936945eSDarrick J. Wong 98e936945eSDarrick J. Wong /* 99e936945eSDarrick J. Wong * Get the data from the pointed-to record. 100e936945eSDarrick J. Wong */ 101e936945eSDarrick J. Wong int 102e936945eSDarrick J. Wong xfs_inobt_get_rec( 103e936945eSDarrick J. Wong struct xfs_btree_cur *cur, 104e936945eSDarrick J. Wong struct xfs_inobt_rec_incore *irec, 105e936945eSDarrick J. Wong int *stat) 106e936945eSDarrick J. Wong { 1079e6c08d4SDave Chinner struct xfs_mount *mp = cur->bc_mp; 10850f02fe3SDave Chinner xfs_agnumber_t agno = cur->bc_ag.pag->pag_agno; 109e936945eSDarrick J. Wong union xfs_btree_rec *rec; 110e936945eSDarrick J. Wong int error; 1119e6c08d4SDave Chinner uint64_t realfree; 112e936945eSDarrick J. Wong 113e936945eSDarrick J. Wong error = xfs_btree_get_rec(cur, &rec, stat); 114e936945eSDarrick J. Wong if (error || *stat == 0) 115e936945eSDarrick J. Wong return error; 116e936945eSDarrick J. Wong 1179e6c08d4SDave Chinner xfs_inobt_btrec_to_irec(mp, rec, irec); 1189e6c08d4SDave Chinner 1199e6c08d4SDave Chinner if (!xfs_verify_agino(mp, agno, irec->ir_startino)) 1209e6c08d4SDave Chinner goto out_bad_rec; 1219e6c08d4SDave Chinner if (irec->ir_count < XFS_INODES_PER_HOLEMASK_BIT || 1229e6c08d4SDave Chinner irec->ir_count > XFS_INODES_PER_CHUNK) 1239e6c08d4SDave Chinner goto out_bad_rec; 1249e6c08d4SDave Chinner if (irec->ir_freecount > XFS_INODES_PER_CHUNK) 1259e6c08d4SDave Chinner goto out_bad_rec; 1269e6c08d4SDave Chinner 1279e6c08d4SDave Chinner /* if there are no holes, return the first available offset */ 1289e6c08d4SDave Chinner if (!xfs_inobt_issparse(irec->ir_holemask)) 1299e6c08d4SDave Chinner realfree = irec->ir_free; 1309e6c08d4SDave Chinner else 1319e6c08d4SDave Chinner realfree = irec->ir_free & xfs_inobt_irec_to_allocmask(irec); 1329e6c08d4SDave Chinner if (hweight64(realfree) != irec->ir_freecount) 1339e6c08d4SDave Chinner goto out_bad_rec; 1345419040fSBrian Foster 1355419040fSBrian Foster return 0; 1369e6c08d4SDave Chinner 1379e6c08d4SDave Chinner out_bad_rec: 1389e6c08d4SDave Chinner xfs_warn(mp, 1399e6c08d4SDave Chinner "%s Inode BTree record corruption in AG %d detected!", 1409e6c08d4SDave Chinner cur->bc_btnum == XFS_BTNUM_INO ? "Used" : "Free", agno); 1419e6c08d4SDave Chinner xfs_warn(mp, 1429e6c08d4SDave Chinner "start inode 0x%x, count 0x%x, free 0x%x freemask 0x%llx, holemask 0x%x", 1439e6c08d4SDave Chinner irec->ir_startino, irec->ir_count, irec->ir_freecount, 1449e6c08d4SDave Chinner irec->ir_free, irec->ir_holemask); 1459e6c08d4SDave Chinner return -EFSCORRUPTED; 14630f712c9SDave Chinner } 14730f712c9SDave Chinner 14830f712c9SDave Chinner /* 14930f712c9SDave Chinner * Insert a single inobt record. Cursor must already point to desired location. 15030f712c9SDave Chinner */ 1517f8f1313SDarrick J. Wong int 15230f712c9SDave Chinner xfs_inobt_insert_rec( 15330f712c9SDave Chinner struct xfs_btree_cur *cur, 154c8ce540dSDarrick J. Wong uint16_t holemask, 155c8ce540dSDarrick J. Wong uint8_t count, 156c8ce540dSDarrick J. Wong int32_t freecount, 15730f712c9SDave Chinner xfs_inofree_t free, 15830f712c9SDave Chinner int *stat) 15930f712c9SDave Chinner { 1605419040fSBrian Foster cur->bc_rec.i.ir_holemask = holemask; 1615419040fSBrian Foster cur->bc_rec.i.ir_count = count; 16230f712c9SDave Chinner cur->bc_rec.i.ir_freecount = freecount; 16330f712c9SDave Chinner cur->bc_rec.i.ir_free = free; 16430f712c9SDave Chinner return xfs_btree_insert(cur, stat); 16530f712c9SDave Chinner } 16630f712c9SDave Chinner 16730f712c9SDave Chinner /* 16830f712c9SDave Chinner * Insert records describing a newly allocated inode chunk into the inobt. 16930f712c9SDave Chinner */ 17030f712c9SDave Chinner STATIC int 17130f712c9SDave Chinner xfs_inobt_insert( 17230f712c9SDave Chinner struct xfs_mount *mp, 17330f712c9SDave Chinner struct xfs_trans *tp, 17430f712c9SDave Chinner struct xfs_buf *agbp, 1757b13c515SDave Chinner struct xfs_perag *pag, 17630f712c9SDave Chinner xfs_agino_t newino, 17730f712c9SDave Chinner xfs_agino_t newlen, 17830f712c9SDave Chinner xfs_btnum_t btnum) 17930f712c9SDave Chinner { 18030f712c9SDave Chinner struct xfs_btree_cur *cur; 18130f712c9SDave Chinner xfs_agino_t thisino; 18230f712c9SDave Chinner int i; 18330f712c9SDave Chinner int error; 18430f712c9SDave Chinner 1857b13c515SDave Chinner cur = xfs_inobt_init_cursor(mp, tp, agbp, pag, btnum); 18630f712c9SDave Chinner 18730f712c9SDave Chinner for (thisino = newino; 18830f712c9SDave Chinner thisino < newino + newlen; 18930f712c9SDave Chinner thisino += XFS_INODES_PER_CHUNK) { 19030f712c9SDave Chinner error = xfs_inobt_lookup(cur, thisino, XFS_LOOKUP_EQ, &i); 19130f712c9SDave Chinner if (error) { 19230f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); 19330f712c9SDave Chinner return error; 19430f712c9SDave Chinner } 19530f712c9SDave Chinner ASSERT(i == 0); 19630f712c9SDave Chinner 1975419040fSBrian Foster error = xfs_inobt_insert_rec(cur, XFS_INOBT_HOLEMASK_FULL, 1985419040fSBrian Foster XFS_INODES_PER_CHUNK, 1995419040fSBrian Foster XFS_INODES_PER_CHUNK, 20030f712c9SDave Chinner XFS_INOBT_ALL_FREE, &i); 20130f712c9SDave Chinner if (error) { 20230f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); 20330f712c9SDave Chinner return error; 20430f712c9SDave Chinner } 20530f712c9SDave Chinner ASSERT(i == 1); 20630f712c9SDave Chinner } 20730f712c9SDave Chinner 20830f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); 20930f712c9SDave Chinner 21030f712c9SDave Chinner return 0; 21130f712c9SDave Chinner } 21230f712c9SDave Chinner 21330f712c9SDave Chinner /* 21430f712c9SDave Chinner * Verify that the number of free inodes in the AGI is correct. 21530f712c9SDave Chinner */ 21630f712c9SDave Chinner #ifdef DEBUG 217*9ba0889eSDave Chinner static int 21830f712c9SDave Chinner xfs_check_agi_freecount( 219*9ba0889eSDave Chinner struct xfs_btree_cur *cur) 22030f712c9SDave Chinner { 22130f712c9SDave Chinner if (cur->bc_nlevels == 1) { 22230f712c9SDave Chinner xfs_inobt_rec_incore_t rec; 22330f712c9SDave Chinner int freecount = 0; 22430f712c9SDave Chinner int error; 22530f712c9SDave Chinner int i; 22630f712c9SDave Chinner 22730f712c9SDave Chinner error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i); 22830f712c9SDave Chinner if (error) 22930f712c9SDave Chinner return error; 23030f712c9SDave Chinner 23130f712c9SDave Chinner do { 23230f712c9SDave Chinner error = xfs_inobt_get_rec(cur, &rec, &i); 23330f712c9SDave Chinner if (error) 23430f712c9SDave Chinner return error; 23530f712c9SDave Chinner 23630f712c9SDave Chinner if (i) { 23730f712c9SDave Chinner freecount += rec.ir_freecount; 23830f712c9SDave Chinner error = xfs_btree_increment(cur, 0, &i); 23930f712c9SDave Chinner if (error) 24030f712c9SDave Chinner return error; 24130f712c9SDave Chinner } 24230f712c9SDave Chinner } while (i == 1); 24330f712c9SDave Chinner 24430f712c9SDave Chinner if (!XFS_FORCED_SHUTDOWN(cur->bc_mp)) 245*9ba0889eSDave Chinner ASSERT(freecount == cur->bc_ag.pag->pagi_freecount); 24630f712c9SDave Chinner } 24730f712c9SDave Chinner return 0; 24830f712c9SDave Chinner } 24930f712c9SDave Chinner #else 250*9ba0889eSDave Chinner #define xfs_check_agi_freecount(cur) 0 25130f712c9SDave Chinner #endif 25230f712c9SDave Chinner 25330f712c9SDave Chinner /* 25430f712c9SDave Chinner * Initialise a new set of inodes. When called without a transaction context 25530f712c9SDave Chinner * (e.g. from recovery) we initiate a delayed write of the inode buffers rather 25630f712c9SDave Chinner * than logging them (which in a transaction context puts them into the AIL 25730f712c9SDave Chinner * for writeback rather than the xfsbufd queue). 25830f712c9SDave Chinner */ 25930f712c9SDave Chinner int 26030f712c9SDave Chinner xfs_ialloc_inode_init( 26130f712c9SDave Chinner struct xfs_mount *mp, 26230f712c9SDave Chinner struct xfs_trans *tp, 26330f712c9SDave Chinner struct list_head *buffer_list, 264463958afSBrian Foster int icount, 26530f712c9SDave Chinner xfs_agnumber_t agno, 26630f712c9SDave Chinner xfs_agblock_t agbno, 26730f712c9SDave Chinner xfs_agblock_t length, 26830f712c9SDave Chinner unsigned int gen) 26930f712c9SDave Chinner { 27030f712c9SDave Chinner struct xfs_buf *fbuf; 27130f712c9SDave Chinner struct xfs_dinode *free; 27283dcdb44SDarrick J. Wong int nbufs; 27330f712c9SDave Chinner int version; 27430f712c9SDave Chinner int i, j; 27530f712c9SDave Chinner xfs_daddr_t d; 27630f712c9SDave Chinner xfs_ino_t ino = 0; 277ce92464cSDarrick J. Wong int error; 27830f712c9SDave Chinner 27930f712c9SDave Chinner /* 28030f712c9SDave Chinner * Loop over the new block(s), filling in the inodes. For small block 28130f712c9SDave Chinner * sizes, manipulate the inodes in buffers which are multiples of the 28230f712c9SDave Chinner * blocks size. 28330f712c9SDave Chinner */ 284ef325959SDarrick J. Wong nbufs = length / M_IGEO(mp)->blocks_per_cluster; 28530f712c9SDave Chinner 28630f712c9SDave Chinner /* 28730f712c9SDave Chinner * Figure out what version number to use in the inodes we create. If 28830f712c9SDave Chinner * the superblock version has caught up to the one that supports the new 28930f712c9SDave Chinner * inode format, then use the new inode version. Otherwise use the old 29030f712c9SDave Chinner * version so that old kernels will continue to be able to use the file 29130f712c9SDave Chinner * system. 29230f712c9SDave Chinner * 29330f712c9SDave Chinner * For v3 inodes, we also need to write the inode number into the inode, 29430f712c9SDave Chinner * so calculate the first inode number of the chunk here as 29543004b2aSDarrick J. Wong * XFS_AGB_TO_AGINO() only works within a filesystem block, not 29630f712c9SDave Chinner * across multiple filesystem blocks (such as a cluster) and so cannot 29730f712c9SDave Chinner * be used in the cluster buffer loop below. 29830f712c9SDave Chinner * 29930f712c9SDave Chinner * Further, because we are writing the inode directly into the buffer 30030f712c9SDave Chinner * and calculating a CRC on the entire inode, we have ot log the entire 30130f712c9SDave Chinner * inode so that the entire range the CRC covers is present in the log. 30230f712c9SDave Chinner * That means for v3 inode we log the entire buffer rather than just the 30330f712c9SDave Chinner * inode cores. 30430f712c9SDave Chinner */ 305b81b79f4SChristoph Hellwig if (xfs_sb_version_has_v3inode(&mp->m_sb)) { 30630f712c9SDave Chinner version = 3; 30743004b2aSDarrick J. Wong ino = XFS_AGINO_TO_INO(mp, agno, XFS_AGB_TO_AGINO(mp, agbno)); 30830f712c9SDave Chinner 30930f712c9SDave Chinner /* 31030f712c9SDave Chinner * log the initialisation that is about to take place as an 31130f712c9SDave Chinner * logical operation. This means the transaction does not 31230f712c9SDave Chinner * need to log the physical changes to the inode buffers as log 31330f712c9SDave Chinner * recovery will know what initialisation is actually needed. 31430f712c9SDave Chinner * Hence we only need to log the buffers as "ordered" buffers so 31530f712c9SDave Chinner * they track in the AIL as if they were physically logged. 31630f712c9SDave Chinner */ 31730f712c9SDave Chinner if (tp) 318463958afSBrian Foster xfs_icreate_log(tp, agno, agbno, icount, 31930f712c9SDave Chinner mp->m_sb.sb_inodesize, length, gen); 32030f712c9SDave Chinner } else 32130f712c9SDave Chinner version = 2; 32230f712c9SDave Chinner 32330f712c9SDave Chinner for (j = 0; j < nbufs; j++) { 32430f712c9SDave Chinner /* 32530f712c9SDave Chinner * Get the block. 32630f712c9SDave Chinner */ 32783dcdb44SDarrick J. Wong d = XFS_AGB_TO_DADDR(mp, agno, agbno + 328ef325959SDarrick J. Wong (j * M_IGEO(mp)->blocks_per_cluster)); 329ce92464cSDarrick J. Wong error = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, 330ce92464cSDarrick J. Wong mp->m_bsize * M_IGEO(mp)->blocks_per_cluster, 331ce92464cSDarrick J. Wong XBF_UNMAPPED, &fbuf); 332ce92464cSDarrick J. Wong if (error) 333ce92464cSDarrick J. Wong return error; 33430f712c9SDave Chinner 33530f712c9SDave Chinner /* Initialize the inode buffers and log them appropriately. */ 33630f712c9SDave Chinner fbuf->b_ops = &xfs_inode_buf_ops; 33730f712c9SDave Chinner xfs_buf_zero(fbuf, 0, BBTOB(fbuf->b_length)); 338ef325959SDarrick J. Wong for (i = 0; i < M_IGEO(mp)->inodes_per_cluster; i++) { 33930f712c9SDave Chinner int ioffset = i << mp->m_sb.sb_inodelog; 340e9e2eae8SChristoph Hellwig uint isize = XFS_DINODE_SIZE(&mp->m_sb); 34130f712c9SDave Chinner 34230f712c9SDave Chinner free = xfs_make_iptr(mp, fbuf, i); 34330f712c9SDave Chinner free->di_magic = cpu_to_be16(XFS_DINODE_MAGIC); 34430f712c9SDave Chinner free->di_version = version; 34530f712c9SDave Chinner free->di_gen = cpu_to_be32(gen); 34630f712c9SDave Chinner free->di_next_unlinked = cpu_to_be32(NULLAGINO); 34730f712c9SDave Chinner 34830f712c9SDave Chinner if (version == 3) { 34930f712c9SDave Chinner free->di_ino = cpu_to_be64(ino); 35030f712c9SDave Chinner ino++; 351ce748eaaSEric Sandeen uuid_copy(&free->di_uuid, 352ce748eaaSEric Sandeen &mp->m_sb.sb_meta_uuid); 35330f712c9SDave Chinner xfs_dinode_calc_crc(mp, free); 35430f712c9SDave Chinner } else if (tp) { 35530f712c9SDave Chinner /* just log the inode core */ 35630f712c9SDave Chinner xfs_trans_log_buf(tp, fbuf, ioffset, 35730f712c9SDave Chinner ioffset + isize - 1); 35830f712c9SDave Chinner } 35930f712c9SDave Chinner } 36030f712c9SDave Chinner 36130f712c9SDave Chinner if (tp) { 36230f712c9SDave Chinner /* 36330f712c9SDave Chinner * Mark the buffer as an inode allocation buffer so it 36430f712c9SDave Chinner * sticks in AIL at the point of this allocation 36530f712c9SDave Chinner * transaction. This ensures the they are on disk before 36630f712c9SDave Chinner * the tail of the log can be moved past this 36730f712c9SDave Chinner * transaction (i.e. by preventing relogging from moving 36830f712c9SDave Chinner * it forward in the log). 36930f712c9SDave Chinner */ 37030f712c9SDave Chinner xfs_trans_inode_alloc_buf(tp, fbuf); 37130f712c9SDave Chinner if (version == 3) { 37230f712c9SDave Chinner /* 37330f712c9SDave Chinner * Mark the buffer as ordered so that they are 37430f712c9SDave Chinner * not physically logged in the transaction but 37530f712c9SDave Chinner * still tracked in the AIL as part of the 37630f712c9SDave Chinner * transaction and pin the log appropriately. 37730f712c9SDave Chinner */ 37830f712c9SDave Chinner xfs_trans_ordered_buf(tp, fbuf); 37930f712c9SDave Chinner } 38030f712c9SDave Chinner } else { 38130f712c9SDave Chinner fbuf->b_flags |= XBF_DONE; 38230f712c9SDave Chinner xfs_buf_delwri_queue(fbuf, buffer_list); 38330f712c9SDave Chinner xfs_buf_relse(fbuf); 38430f712c9SDave Chinner } 38530f712c9SDave Chinner } 38630f712c9SDave Chinner return 0; 38730f712c9SDave Chinner } 38830f712c9SDave Chinner 38930f712c9SDave Chinner /* 39056d1115cSBrian Foster * Align startino and allocmask for a recently allocated sparse chunk such that 39156d1115cSBrian Foster * they are fit for insertion (or merge) into the on-disk inode btrees. 39256d1115cSBrian Foster * 39356d1115cSBrian Foster * Background: 39456d1115cSBrian Foster * 39556d1115cSBrian Foster * When enabled, sparse inode support increases the inode alignment from cluster 39656d1115cSBrian Foster * size to inode chunk size. This means that the minimum range between two 39756d1115cSBrian Foster * non-adjacent inode records in the inobt is large enough for a full inode 39856d1115cSBrian Foster * record. This allows for cluster sized, cluster aligned block allocation 39956d1115cSBrian Foster * without need to worry about whether the resulting inode record overlaps with 40056d1115cSBrian Foster * another record in the tree. Without this basic rule, we would have to deal 40156d1115cSBrian Foster * with the consequences of overlap by potentially undoing recent allocations in 40256d1115cSBrian Foster * the inode allocation codepath. 40356d1115cSBrian Foster * 40456d1115cSBrian Foster * Because of this alignment rule (which is enforced on mount), there are two 40556d1115cSBrian Foster * inobt possibilities for newly allocated sparse chunks. One is that the 40656d1115cSBrian Foster * aligned inode record for the chunk covers a range of inodes not already 40756d1115cSBrian Foster * covered in the inobt (i.e., it is safe to insert a new sparse record). The 40856d1115cSBrian Foster * other is that a record already exists at the aligned startino that considers 40956d1115cSBrian Foster * the newly allocated range as sparse. In the latter case, record content is 41056d1115cSBrian Foster * merged in hope that sparse inode chunks fill to full chunks over time. 41156d1115cSBrian Foster */ 41256d1115cSBrian Foster STATIC void 41356d1115cSBrian Foster xfs_align_sparse_ino( 41456d1115cSBrian Foster struct xfs_mount *mp, 41556d1115cSBrian Foster xfs_agino_t *startino, 41656d1115cSBrian Foster uint16_t *allocmask) 41756d1115cSBrian Foster { 41856d1115cSBrian Foster xfs_agblock_t agbno; 41956d1115cSBrian Foster xfs_agblock_t mod; 42056d1115cSBrian Foster int offset; 42156d1115cSBrian Foster 42256d1115cSBrian Foster agbno = XFS_AGINO_TO_AGBNO(mp, *startino); 42356d1115cSBrian Foster mod = agbno % mp->m_sb.sb_inoalignmt; 42456d1115cSBrian Foster if (!mod) 42556d1115cSBrian Foster return; 42656d1115cSBrian Foster 42756d1115cSBrian Foster /* calculate the inode offset and align startino */ 42843004b2aSDarrick J. Wong offset = XFS_AGB_TO_AGINO(mp, mod); 42956d1115cSBrian Foster *startino -= offset; 43056d1115cSBrian Foster 43156d1115cSBrian Foster /* 43256d1115cSBrian Foster * Since startino has been aligned down, left shift allocmask such that 43356d1115cSBrian Foster * it continues to represent the same physical inodes relative to the 43456d1115cSBrian Foster * new startino. 43556d1115cSBrian Foster */ 43656d1115cSBrian Foster *allocmask <<= offset / XFS_INODES_PER_HOLEMASK_BIT; 43756d1115cSBrian Foster } 43856d1115cSBrian Foster 43956d1115cSBrian Foster /* 44056d1115cSBrian Foster * Determine whether the source inode record can merge into the target. Both 44156d1115cSBrian Foster * records must be sparse, the inode ranges must match and there must be no 44256d1115cSBrian Foster * allocation overlap between the records. 44356d1115cSBrian Foster */ 44456d1115cSBrian Foster STATIC bool 44556d1115cSBrian Foster __xfs_inobt_can_merge( 44656d1115cSBrian Foster struct xfs_inobt_rec_incore *trec, /* tgt record */ 44756d1115cSBrian Foster struct xfs_inobt_rec_incore *srec) /* src record */ 44856d1115cSBrian Foster { 44956d1115cSBrian Foster uint64_t talloc; 45056d1115cSBrian Foster uint64_t salloc; 45156d1115cSBrian Foster 45256d1115cSBrian Foster /* records must cover the same inode range */ 45356d1115cSBrian Foster if (trec->ir_startino != srec->ir_startino) 45456d1115cSBrian Foster return false; 45556d1115cSBrian Foster 45656d1115cSBrian Foster /* both records must be sparse */ 45756d1115cSBrian Foster if (!xfs_inobt_issparse(trec->ir_holemask) || 45856d1115cSBrian Foster !xfs_inobt_issparse(srec->ir_holemask)) 45956d1115cSBrian Foster return false; 46056d1115cSBrian Foster 46156d1115cSBrian Foster /* both records must track some inodes */ 46256d1115cSBrian Foster if (!trec->ir_count || !srec->ir_count) 46356d1115cSBrian Foster return false; 46456d1115cSBrian Foster 46556d1115cSBrian Foster /* can't exceed capacity of a full record */ 46656d1115cSBrian Foster if (trec->ir_count + srec->ir_count > XFS_INODES_PER_CHUNK) 46756d1115cSBrian Foster return false; 46856d1115cSBrian Foster 46956d1115cSBrian Foster /* verify there is no allocation overlap */ 47056d1115cSBrian Foster talloc = xfs_inobt_irec_to_allocmask(trec); 47156d1115cSBrian Foster salloc = xfs_inobt_irec_to_allocmask(srec); 47256d1115cSBrian Foster if (talloc & salloc) 47356d1115cSBrian Foster return false; 47456d1115cSBrian Foster 47556d1115cSBrian Foster return true; 47656d1115cSBrian Foster } 47756d1115cSBrian Foster 47856d1115cSBrian Foster /* 47956d1115cSBrian Foster * Merge the source inode record into the target. The caller must call 48056d1115cSBrian Foster * __xfs_inobt_can_merge() to ensure the merge is valid. 48156d1115cSBrian Foster */ 48256d1115cSBrian Foster STATIC void 48356d1115cSBrian Foster __xfs_inobt_rec_merge( 48456d1115cSBrian Foster struct xfs_inobt_rec_incore *trec, /* target */ 48556d1115cSBrian Foster struct xfs_inobt_rec_incore *srec) /* src */ 48656d1115cSBrian Foster { 48756d1115cSBrian Foster ASSERT(trec->ir_startino == srec->ir_startino); 48856d1115cSBrian Foster 48956d1115cSBrian Foster /* combine the counts */ 49056d1115cSBrian Foster trec->ir_count += srec->ir_count; 49156d1115cSBrian Foster trec->ir_freecount += srec->ir_freecount; 49256d1115cSBrian Foster 49356d1115cSBrian Foster /* 49456d1115cSBrian Foster * Merge the holemask and free mask. For both fields, 0 bits refer to 49556d1115cSBrian Foster * allocated inodes. We combine the allocated ranges with bitwise AND. 49656d1115cSBrian Foster */ 49756d1115cSBrian Foster trec->ir_holemask &= srec->ir_holemask; 49856d1115cSBrian Foster trec->ir_free &= srec->ir_free; 49956d1115cSBrian Foster } 50056d1115cSBrian Foster 50156d1115cSBrian Foster /* 50256d1115cSBrian Foster * Insert a new sparse inode chunk into the associated inode btree. The inode 50356d1115cSBrian Foster * record for the sparse chunk is pre-aligned to a startino that should match 50456d1115cSBrian Foster * any pre-existing sparse inode record in the tree. This allows sparse chunks 50556d1115cSBrian Foster * to fill over time. 50656d1115cSBrian Foster * 50756d1115cSBrian Foster * This function supports two modes of handling preexisting records depending on 50856d1115cSBrian Foster * the merge flag. If merge is true, the provided record is merged with the 50956d1115cSBrian Foster * existing record and updated in place. The merged record is returned in nrec. 51056d1115cSBrian Foster * If merge is false, an existing record is replaced with the provided record. 51156d1115cSBrian Foster * If no preexisting record exists, the provided record is always inserted. 51256d1115cSBrian Foster * 51356d1115cSBrian Foster * It is considered corruption if a merge is requested and not possible. Given 51456d1115cSBrian Foster * the sparse inode alignment constraints, this should never happen. 51556d1115cSBrian Foster */ 51656d1115cSBrian Foster STATIC int 51756d1115cSBrian Foster xfs_inobt_insert_sprec( 51856d1115cSBrian Foster struct xfs_mount *mp, 51956d1115cSBrian Foster struct xfs_trans *tp, 52056d1115cSBrian Foster struct xfs_buf *agbp, 5217b13c515SDave Chinner struct xfs_perag *pag, 52256d1115cSBrian Foster int btnum, 52356d1115cSBrian Foster struct xfs_inobt_rec_incore *nrec, /* in/out: new/merged rec. */ 52456d1115cSBrian Foster bool merge) /* merge or replace */ 52556d1115cSBrian Foster { 52656d1115cSBrian Foster struct xfs_btree_cur *cur; 52756d1115cSBrian Foster int error; 52856d1115cSBrian Foster int i; 52956d1115cSBrian Foster struct xfs_inobt_rec_incore rec; 53056d1115cSBrian Foster 5317b13c515SDave Chinner cur = xfs_inobt_init_cursor(mp, tp, agbp, pag, btnum); 53256d1115cSBrian Foster 53356d1115cSBrian Foster /* the new record is pre-aligned so we know where to look */ 53456d1115cSBrian Foster error = xfs_inobt_lookup(cur, nrec->ir_startino, XFS_LOOKUP_EQ, &i); 53556d1115cSBrian Foster if (error) 53656d1115cSBrian Foster goto error; 53756d1115cSBrian Foster /* if nothing there, insert a new record and return */ 53856d1115cSBrian Foster if (i == 0) { 53956d1115cSBrian Foster error = xfs_inobt_insert_rec(cur, nrec->ir_holemask, 54056d1115cSBrian Foster nrec->ir_count, nrec->ir_freecount, 54156d1115cSBrian Foster nrec->ir_free, &i); 54256d1115cSBrian Foster if (error) 54356d1115cSBrian Foster goto error; 544f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(mp, i != 1)) { 545f9e03706SDarrick J. Wong error = -EFSCORRUPTED; 546f9e03706SDarrick J. Wong goto error; 547f9e03706SDarrick J. Wong } 54856d1115cSBrian Foster 54956d1115cSBrian Foster goto out; 55056d1115cSBrian Foster } 55156d1115cSBrian Foster 55256d1115cSBrian Foster /* 55356d1115cSBrian Foster * A record exists at this startino. Merge or replace the record 55456d1115cSBrian Foster * depending on what we've been asked to do. 55556d1115cSBrian Foster */ 55656d1115cSBrian Foster if (merge) { 55756d1115cSBrian Foster error = xfs_inobt_get_rec(cur, &rec, &i); 55856d1115cSBrian Foster if (error) 55956d1115cSBrian Foster goto error; 560f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(mp, i != 1)) { 561f9e03706SDarrick J. Wong error = -EFSCORRUPTED; 562f9e03706SDarrick J. Wong goto error; 563f9e03706SDarrick J. Wong } 564f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(mp, rec.ir_startino != nrec->ir_startino)) { 565f9e03706SDarrick J. Wong error = -EFSCORRUPTED; 566f9e03706SDarrick J. Wong goto error; 567f9e03706SDarrick J. Wong } 56856d1115cSBrian Foster 56956d1115cSBrian Foster /* 57056d1115cSBrian Foster * This should never fail. If we have coexisting records that 57156d1115cSBrian Foster * cannot merge, something is seriously wrong. 57256d1115cSBrian Foster */ 573f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(mp, !__xfs_inobt_can_merge(nrec, &rec))) { 574f9e03706SDarrick J. Wong error = -EFSCORRUPTED; 575f9e03706SDarrick J. Wong goto error; 576f9e03706SDarrick J. Wong } 57756d1115cSBrian Foster 5787b13c515SDave Chinner trace_xfs_irec_merge_pre(mp, pag->pag_agno, rec.ir_startino, 57956d1115cSBrian Foster rec.ir_holemask, nrec->ir_startino, 58056d1115cSBrian Foster nrec->ir_holemask); 58156d1115cSBrian Foster 58256d1115cSBrian Foster /* merge to nrec to output the updated record */ 58356d1115cSBrian Foster __xfs_inobt_rec_merge(nrec, &rec); 58456d1115cSBrian Foster 5857b13c515SDave Chinner trace_xfs_irec_merge_post(mp, pag->pag_agno, nrec->ir_startino, 58656d1115cSBrian Foster nrec->ir_holemask); 58756d1115cSBrian Foster 58856d1115cSBrian Foster error = xfs_inobt_rec_check_count(mp, nrec); 58956d1115cSBrian Foster if (error) 59056d1115cSBrian Foster goto error; 59156d1115cSBrian Foster } 59256d1115cSBrian Foster 59356d1115cSBrian Foster error = xfs_inobt_update(cur, nrec); 59456d1115cSBrian Foster if (error) 59556d1115cSBrian Foster goto error; 59656d1115cSBrian Foster 59756d1115cSBrian Foster out: 59856d1115cSBrian Foster xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); 59956d1115cSBrian Foster return 0; 60056d1115cSBrian Foster error: 60156d1115cSBrian Foster xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); 60256d1115cSBrian Foster return error; 60356d1115cSBrian Foster } 60456d1115cSBrian Foster 60556d1115cSBrian Foster /* 6068237fbf5SDave Chinner * Allocate new inodes in the allocation group specified by agbp. Returns 0 if 6078237fbf5SDave Chinner * inodes were allocated in this AG; -EAGAIN if there was no space in this AG so 6088237fbf5SDave Chinner * the caller knows it can try another AG, a hard -ENOSPC when over the maximum 6098237fbf5SDave Chinner * inode count threshold, or the usual negative error code for other errors. 61030f712c9SDave Chinner */ 611ef325959SDarrick J. Wong STATIC int 61230f712c9SDave Chinner xfs_ialloc_ag_alloc( 613ef325959SDarrick J. Wong struct xfs_trans *tp, 6147b13c515SDave Chinner struct xfs_buf *agbp, 6157b13c515SDave Chinner struct xfs_perag *pag) 61630f712c9SDave Chinner { 617ef325959SDarrick J. Wong struct xfs_agi *agi; 618ef325959SDarrick J. Wong struct xfs_alloc_arg args; 61930f712c9SDave Chinner int error; 62030f712c9SDave Chinner xfs_agino_t newino; /* new first inode's number */ 62130f712c9SDave Chinner xfs_agino_t newlen; /* new number of inodes */ 622ef325959SDarrick J. Wong int isaligned = 0; /* inode allocation at stripe */ 623ef325959SDarrick J. Wong /* unit boundary */ 624ef325959SDarrick J. Wong /* init. to full chunk */ 62556d1115cSBrian Foster struct xfs_inobt_rec_incore rec; 626ef325959SDarrick J. Wong struct xfs_ino_geometry *igeo = M_IGEO(tp->t_mountp); 6277b13c515SDave Chinner uint16_t allocmask = (uint16_t) -1; 6281cdadee1SBrian Foster int do_sparse = 0; 6291cdadee1SBrian Foster 63030f712c9SDave Chinner memset(&args, 0, sizeof(args)); 63130f712c9SDave Chinner args.tp = tp; 63230f712c9SDave Chinner args.mp = tp->t_mountp; 6331cdadee1SBrian Foster args.fsbno = NULLFSBLOCK; 6347280fedaSDarrick J. Wong args.oinfo = XFS_RMAP_OINFO_INODES; 63530f712c9SDave Chinner 63646fc58daSBrian Foster #ifdef DEBUG 63746fc58daSBrian Foster /* randomly do sparse inode allocations */ 63846fc58daSBrian Foster if (xfs_sb_version_hassparseinodes(&tp->t_mountp->m_sb) && 639ef325959SDarrick J. Wong igeo->ialloc_min_blks < igeo->ialloc_blks) 64046fc58daSBrian Foster do_sparse = prandom_u32() & 1; 64146fc58daSBrian Foster #endif 64246fc58daSBrian Foster 64330f712c9SDave Chinner /* 64430f712c9SDave Chinner * Locking will ensure that we don't have two callers in here 64530f712c9SDave Chinner * at one time. 64630f712c9SDave Chinner */ 647ef325959SDarrick J. Wong newlen = igeo->ialloc_inos; 648ef325959SDarrick J. Wong if (igeo->maxicount && 64974f9ce1cSGeorge Wang percpu_counter_read_positive(&args.mp->m_icount) + newlen > 650ef325959SDarrick J. Wong igeo->maxicount) 6512451337dSDave Chinner return -ENOSPC; 652ef325959SDarrick J. Wong args.minlen = args.maxlen = igeo->ialloc_blks; 65330f712c9SDave Chinner /* 65430f712c9SDave Chinner * First try to allocate inodes contiguous with the last-allocated 65530f712c9SDave Chinner * chunk of inodes. If the filesystem is striped, this will fill 65630f712c9SDave Chinner * an entire stripe unit with inodes. 65730f712c9SDave Chinner */ 658370c782bSChristoph Hellwig agi = agbp->b_addr; 65930f712c9SDave Chinner newino = be32_to_cpu(agi->agi_newino); 66030f712c9SDave Chinner args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) + 661ef325959SDarrick J. Wong igeo->ialloc_blks; 6621cdadee1SBrian Foster if (do_sparse) 6631cdadee1SBrian Foster goto sparse_alloc; 66430f712c9SDave Chinner if (likely(newino != NULLAGINO && 66530f712c9SDave Chinner (args.agbno < be32_to_cpu(agi->agi_length)))) { 6667b13c515SDave Chinner args.fsbno = XFS_AGB_TO_FSB(args.mp, pag->pag_agno, args.agbno); 66730f712c9SDave Chinner args.type = XFS_ALLOCTYPE_THIS_BNO; 66830f712c9SDave Chinner args.prod = 1; 66930f712c9SDave Chinner 67030f712c9SDave Chinner /* 67130f712c9SDave Chinner * We need to take into account alignment here to ensure that 67230f712c9SDave Chinner * we don't modify the free list if we fail to have an exact 67330f712c9SDave Chinner * block. If we don't have an exact match, and every oher 67430f712c9SDave Chinner * attempt allocation attempt fails, we'll end up cancelling 67530f712c9SDave Chinner * a dirty transaction and shutting down. 67630f712c9SDave Chinner * 67730f712c9SDave Chinner * For an exact allocation, alignment must be 1, 67830f712c9SDave Chinner * however we need to take cluster alignment into account when 67930f712c9SDave Chinner * fixing up the freelist. Use the minalignslop field to 68030f712c9SDave Chinner * indicate that extra blocks might be required for alignment, 68130f712c9SDave Chinner * but not to use them in the actual exact allocation. 68230f712c9SDave Chinner */ 68330f712c9SDave Chinner args.alignment = 1; 684ef325959SDarrick J. Wong args.minalignslop = igeo->cluster_align - 1; 68530f712c9SDave Chinner 68630f712c9SDave Chinner /* Allow space for the inode btree to split. */ 687657f1019SBrian Foster args.minleft = igeo->inobt_maxlevels; 68830f712c9SDave Chinner if ((error = xfs_alloc_vextent(&args))) 68930f712c9SDave Chinner return error; 69030f712c9SDave Chinner 69130f712c9SDave Chinner /* 69230f712c9SDave Chinner * This request might have dirtied the transaction if the AG can 69330f712c9SDave Chinner * satisfy the request, but the exact block was not available. 69430f712c9SDave Chinner * If the allocation did fail, subsequent requests will relax 69530f712c9SDave Chinner * the exact agbno requirement and increase the alignment 69630f712c9SDave Chinner * instead. It is critical that the total size of the request 69730f712c9SDave Chinner * (len + alignment + slop) does not increase from this point 69830f712c9SDave Chinner * on, so reset minalignslop to ensure it is not included in 69930f712c9SDave Chinner * subsequent requests. 70030f712c9SDave Chinner */ 70130f712c9SDave Chinner args.minalignslop = 0; 7021cdadee1SBrian Foster } 70330f712c9SDave Chinner 70430f712c9SDave Chinner if (unlikely(args.fsbno == NULLFSBLOCK)) { 70530f712c9SDave Chinner /* 70630f712c9SDave Chinner * Set the alignment for the allocation. 70730f712c9SDave Chinner * If stripe alignment is turned on then align at stripe unit 70830f712c9SDave Chinner * boundary. 70930f712c9SDave Chinner * If the cluster size is smaller than a filesystem block 71030f712c9SDave Chinner * then we're doing I/O for inodes in filesystem block size 71130f712c9SDave Chinner * pieces, so don't need alignment anyway. 71230f712c9SDave Chinner */ 71330f712c9SDave Chinner isaligned = 0; 714ef325959SDarrick J. Wong if (igeo->ialloc_align) { 71530f712c9SDave Chinner ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN)); 71630f712c9SDave Chinner args.alignment = args.mp->m_dalign; 71730f712c9SDave Chinner isaligned = 1; 71830f712c9SDave Chinner } else 719ef325959SDarrick J. Wong args.alignment = igeo->cluster_align; 72030f712c9SDave Chinner /* 72130f712c9SDave Chinner * Need to figure out where to allocate the inode blocks. 72230f712c9SDave Chinner * Ideally they should be spaced out through the a.g. 72330f712c9SDave Chinner * For now, just allocate blocks up front. 72430f712c9SDave Chinner */ 72530f712c9SDave Chinner args.agbno = be32_to_cpu(agi->agi_root); 7267b13c515SDave Chinner args.fsbno = XFS_AGB_TO_FSB(args.mp, pag->pag_agno, args.agbno); 72730f712c9SDave Chinner /* 72830f712c9SDave Chinner * Allocate a fixed-size extent of inodes. 72930f712c9SDave Chinner */ 73030f712c9SDave Chinner args.type = XFS_ALLOCTYPE_NEAR_BNO; 73130f712c9SDave Chinner args.prod = 1; 73230f712c9SDave Chinner /* 73330f712c9SDave Chinner * Allow space for the inode btree to split. 73430f712c9SDave Chinner */ 735657f1019SBrian Foster args.minleft = igeo->inobt_maxlevels; 73630f712c9SDave Chinner if ((error = xfs_alloc_vextent(&args))) 73730f712c9SDave Chinner return error; 73830f712c9SDave Chinner } 73930f712c9SDave Chinner 74030f712c9SDave Chinner /* 74130f712c9SDave Chinner * If stripe alignment is turned on, then try again with cluster 74230f712c9SDave Chinner * alignment. 74330f712c9SDave Chinner */ 74430f712c9SDave Chinner if (isaligned && args.fsbno == NULLFSBLOCK) { 74530f712c9SDave Chinner args.type = XFS_ALLOCTYPE_NEAR_BNO; 74630f712c9SDave Chinner args.agbno = be32_to_cpu(agi->agi_root); 7477b13c515SDave Chinner args.fsbno = XFS_AGB_TO_FSB(args.mp, pag->pag_agno, args.agbno); 748ef325959SDarrick J. Wong args.alignment = igeo->cluster_align; 74930f712c9SDave Chinner if ((error = xfs_alloc_vextent(&args))) 75030f712c9SDave Chinner return error; 75130f712c9SDave Chinner } 75230f712c9SDave Chinner 75356d1115cSBrian Foster /* 75456d1115cSBrian Foster * Finally, try a sparse allocation if the filesystem supports it and 75556d1115cSBrian Foster * the sparse allocation length is smaller than a full chunk. 75656d1115cSBrian Foster */ 75756d1115cSBrian Foster if (xfs_sb_version_hassparseinodes(&args.mp->m_sb) && 758ef325959SDarrick J. Wong igeo->ialloc_min_blks < igeo->ialloc_blks && 75956d1115cSBrian Foster args.fsbno == NULLFSBLOCK) { 7601cdadee1SBrian Foster sparse_alloc: 76156d1115cSBrian Foster args.type = XFS_ALLOCTYPE_NEAR_BNO; 76256d1115cSBrian Foster args.agbno = be32_to_cpu(agi->agi_root); 7637b13c515SDave Chinner args.fsbno = XFS_AGB_TO_FSB(args.mp, pag->pag_agno, args.agbno); 76456d1115cSBrian Foster args.alignment = args.mp->m_sb.sb_spino_align; 76556d1115cSBrian Foster args.prod = 1; 76656d1115cSBrian Foster 767ef325959SDarrick J. Wong args.minlen = igeo->ialloc_min_blks; 76856d1115cSBrian Foster args.maxlen = args.minlen; 76956d1115cSBrian Foster 77056d1115cSBrian Foster /* 77156d1115cSBrian Foster * The inode record will be aligned to full chunk size. We must 77256d1115cSBrian Foster * prevent sparse allocation from AG boundaries that result in 77356d1115cSBrian Foster * invalid inode records, such as records that start at agbno 0 77456d1115cSBrian Foster * or extend beyond the AG. 77556d1115cSBrian Foster * 77656d1115cSBrian Foster * Set min agbno to the first aligned, non-zero agbno and max to 77756d1115cSBrian Foster * the last aligned agbno that is at least one full chunk from 77856d1115cSBrian Foster * the end of the AG. 77956d1115cSBrian Foster */ 78056d1115cSBrian Foster args.min_agbno = args.mp->m_sb.sb_inoalignmt; 78156d1115cSBrian Foster args.max_agbno = round_down(args.mp->m_sb.sb_agblocks, 78256d1115cSBrian Foster args.mp->m_sb.sb_inoalignmt) - 783ef325959SDarrick J. Wong igeo->ialloc_blks; 78456d1115cSBrian Foster 78556d1115cSBrian Foster error = xfs_alloc_vextent(&args); 78656d1115cSBrian Foster if (error) 78756d1115cSBrian Foster return error; 78856d1115cSBrian Foster 78943004b2aSDarrick J. Wong newlen = XFS_AGB_TO_AGINO(args.mp, args.len); 79046fc58daSBrian Foster ASSERT(newlen <= XFS_INODES_PER_CHUNK); 79156d1115cSBrian Foster allocmask = (1 << (newlen / XFS_INODES_PER_HOLEMASK_BIT)) - 1; 79256d1115cSBrian Foster } 79356d1115cSBrian Foster 7943937493cSGao Xiang if (args.fsbno == NULLFSBLOCK) 7958237fbf5SDave Chinner return -EAGAIN; 7963937493cSGao Xiang 79730f712c9SDave Chinner ASSERT(args.len == args.minlen); 79830f712c9SDave Chinner 79930f712c9SDave Chinner /* 80030f712c9SDave Chinner * Stamp and write the inode buffers. 80130f712c9SDave Chinner * 80230f712c9SDave Chinner * Seed the new inode cluster with a random generation number. This 80330f712c9SDave Chinner * prevents short-term reuse of generation numbers if a chunk is 80430f712c9SDave Chinner * freed and then immediately reallocated. We use random numbers 80530f712c9SDave Chinner * rather than a linear progression to prevent the next generation 80630f712c9SDave Chinner * number from being easily guessable. 80730f712c9SDave Chinner */ 8087b13c515SDave Chinner error = xfs_ialloc_inode_init(args.mp, tp, NULL, newlen, pag->pag_agno, 809463958afSBrian Foster args.agbno, args.len, prandom_u32()); 81030f712c9SDave Chinner 81130f712c9SDave Chinner if (error) 81230f712c9SDave Chinner return error; 81330f712c9SDave Chinner /* 81430f712c9SDave Chinner * Convert the results. 81530f712c9SDave Chinner */ 81643004b2aSDarrick J. Wong newino = XFS_AGB_TO_AGINO(args.mp, args.agbno); 81756d1115cSBrian Foster 81856d1115cSBrian Foster if (xfs_inobt_issparse(~allocmask)) { 81956d1115cSBrian Foster /* 82056d1115cSBrian Foster * We've allocated a sparse chunk. Align the startino and mask. 82156d1115cSBrian Foster */ 82256d1115cSBrian Foster xfs_align_sparse_ino(args.mp, &newino, &allocmask); 82356d1115cSBrian Foster 82456d1115cSBrian Foster rec.ir_startino = newino; 82556d1115cSBrian Foster rec.ir_holemask = ~allocmask; 82656d1115cSBrian Foster rec.ir_count = newlen; 82756d1115cSBrian Foster rec.ir_freecount = newlen; 82856d1115cSBrian Foster rec.ir_free = XFS_INOBT_ALL_FREE; 82956d1115cSBrian Foster 83056d1115cSBrian Foster /* 83156d1115cSBrian Foster * Insert the sparse record into the inobt and allow for a merge 83256d1115cSBrian Foster * if necessary. If a merge does occur, rec is updated to the 83356d1115cSBrian Foster * merged record. 83456d1115cSBrian Foster */ 8357b13c515SDave Chinner error = xfs_inobt_insert_sprec(args.mp, tp, agbp, pag, 8367b13c515SDave Chinner XFS_BTNUM_INO, &rec, true); 83756d1115cSBrian Foster if (error == -EFSCORRUPTED) { 83856d1115cSBrian Foster xfs_alert(args.mp, 83956d1115cSBrian Foster "invalid sparse inode record: ino 0x%llx holemask 0x%x count %u", 8407b13c515SDave Chinner XFS_AGINO_TO_INO(args.mp, pag->pag_agno, 84156d1115cSBrian Foster rec.ir_startino), 84256d1115cSBrian Foster rec.ir_holemask, rec.ir_count); 84356d1115cSBrian Foster xfs_force_shutdown(args.mp, SHUTDOWN_CORRUPT_INCORE); 84456d1115cSBrian Foster } 84556d1115cSBrian Foster if (error) 84656d1115cSBrian Foster return error; 84756d1115cSBrian Foster 84856d1115cSBrian Foster /* 84956d1115cSBrian Foster * We can't merge the part we've just allocated as for the inobt 85056d1115cSBrian Foster * due to finobt semantics. The original record may or may not 85156d1115cSBrian Foster * exist independent of whether physical inodes exist in this 85256d1115cSBrian Foster * sparse chunk. 85356d1115cSBrian Foster * 85456d1115cSBrian Foster * We must update the finobt record based on the inobt record. 85556d1115cSBrian Foster * rec contains the fully merged and up to date inobt record 85656d1115cSBrian Foster * from the previous call. Set merge false to replace any 85756d1115cSBrian Foster * existing record with this one. 85856d1115cSBrian Foster */ 85956d1115cSBrian Foster if (xfs_sb_version_hasfinobt(&args.mp->m_sb)) { 8607b13c515SDave Chinner error = xfs_inobt_insert_sprec(args.mp, tp, agbp, pag, 8617b13c515SDave Chinner XFS_BTNUM_FINO, &rec, false); 86256d1115cSBrian Foster if (error) 86356d1115cSBrian Foster return error; 86456d1115cSBrian Foster } 86556d1115cSBrian Foster } else { 86656d1115cSBrian Foster /* full chunk - insert new records to both btrees */ 8677b13c515SDave Chinner error = xfs_inobt_insert(args.mp, tp, agbp, pag, newino, newlen, 86856d1115cSBrian Foster XFS_BTNUM_INO); 86956d1115cSBrian Foster if (error) 87056d1115cSBrian Foster return error; 87156d1115cSBrian Foster 87256d1115cSBrian Foster if (xfs_sb_version_hasfinobt(&args.mp->m_sb)) { 8737b13c515SDave Chinner error = xfs_inobt_insert(args.mp, tp, agbp, pag, newino, 87456d1115cSBrian Foster newlen, XFS_BTNUM_FINO); 87556d1115cSBrian Foster if (error) 87656d1115cSBrian Foster return error; 87756d1115cSBrian Foster } 87856d1115cSBrian Foster } 87956d1115cSBrian Foster 88056d1115cSBrian Foster /* 88156d1115cSBrian Foster * Update AGI counts and newino. 88256d1115cSBrian Foster */ 88330f712c9SDave Chinner be32_add_cpu(&agi->agi_count, newlen); 88430f712c9SDave Chinner be32_add_cpu(&agi->agi_freecount, newlen); 88530f712c9SDave Chinner pag->pagi_freecount += newlen; 88689e9b5c0SDarrick J. Wong pag->pagi_count += newlen; 88730f712c9SDave Chinner agi->agi_newino = cpu_to_be32(newino); 88830f712c9SDave Chinner 88930f712c9SDave Chinner /* 89030f712c9SDave Chinner * Log allocation group header fields 89130f712c9SDave Chinner */ 89230f712c9SDave Chinner xfs_ialloc_log_agi(tp, agbp, 89330f712c9SDave Chinner XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO); 89430f712c9SDave Chinner /* 89530f712c9SDave Chinner * Modify/log superblock values for inode count and inode free count. 89630f712c9SDave Chinner */ 89730f712c9SDave Chinner xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen); 89830f712c9SDave Chinner xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen); 89930f712c9SDave Chinner return 0; 90030f712c9SDave Chinner } 90130f712c9SDave Chinner 90230f712c9SDave Chinner /* 90330f712c9SDave Chinner * Try to retrieve the next record to the left/right from the current one. 90430f712c9SDave Chinner */ 90530f712c9SDave Chinner STATIC int 90630f712c9SDave Chinner xfs_ialloc_next_rec( 90730f712c9SDave Chinner struct xfs_btree_cur *cur, 90830f712c9SDave Chinner xfs_inobt_rec_incore_t *rec, 90930f712c9SDave Chinner int *done, 91030f712c9SDave Chinner int left) 91130f712c9SDave Chinner { 91230f712c9SDave Chinner int error; 91330f712c9SDave Chinner int i; 91430f712c9SDave Chinner 91530f712c9SDave Chinner if (left) 91630f712c9SDave Chinner error = xfs_btree_decrement(cur, 0, &i); 91730f712c9SDave Chinner else 91830f712c9SDave Chinner error = xfs_btree_increment(cur, 0, &i); 91930f712c9SDave Chinner 92030f712c9SDave Chinner if (error) 92130f712c9SDave Chinner return error; 92230f712c9SDave Chinner *done = !i; 92330f712c9SDave Chinner if (i) { 92430f712c9SDave Chinner error = xfs_inobt_get_rec(cur, rec, &i); 92530f712c9SDave Chinner if (error) 92630f712c9SDave Chinner return error; 927f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) 928f9e03706SDarrick J. Wong return -EFSCORRUPTED; 92930f712c9SDave Chinner } 93030f712c9SDave Chinner 93130f712c9SDave Chinner return 0; 93230f712c9SDave Chinner } 93330f712c9SDave Chinner 93430f712c9SDave Chinner STATIC int 93530f712c9SDave Chinner xfs_ialloc_get_rec( 93630f712c9SDave Chinner struct xfs_btree_cur *cur, 93730f712c9SDave Chinner xfs_agino_t agino, 93830f712c9SDave Chinner xfs_inobt_rec_incore_t *rec, 93930f712c9SDave Chinner int *done) 94030f712c9SDave Chinner { 94130f712c9SDave Chinner int error; 94230f712c9SDave Chinner int i; 94330f712c9SDave Chinner 94430f712c9SDave Chinner error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_EQ, &i); 94530f712c9SDave Chinner if (error) 94630f712c9SDave Chinner return error; 94730f712c9SDave Chinner *done = !i; 94830f712c9SDave Chinner if (i) { 94930f712c9SDave Chinner error = xfs_inobt_get_rec(cur, rec, &i); 95030f712c9SDave Chinner if (error) 95130f712c9SDave Chinner return error; 952f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) 953f9e03706SDarrick J. Wong return -EFSCORRUPTED; 95430f712c9SDave Chinner } 95530f712c9SDave Chinner 95630f712c9SDave Chinner return 0; 95730f712c9SDave Chinner } 95830f712c9SDave Chinner 95930f712c9SDave Chinner /* 96026dd5217SBrian Foster * Return the offset of the first free inode in the record. If the inode chunk 96126dd5217SBrian Foster * is sparsely allocated, we convert the record holemask to inode granularity 96226dd5217SBrian Foster * and mask off the unallocated regions from the inode free mask. 963d4cc540bSBrian Foster */ 964d4cc540bSBrian Foster STATIC int 965d4cc540bSBrian Foster xfs_inobt_first_free_inode( 966d4cc540bSBrian Foster struct xfs_inobt_rec_incore *rec) 967d4cc540bSBrian Foster { 96826dd5217SBrian Foster xfs_inofree_t realfree; 96926dd5217SBrian Foster 97026dd5217SBrian Foster /* if there are no holes, return the first available offset */ 97126dd5217SBrian Foster if (!xfs_inobt_issparse(rec->ir_holemask)) 972d4cc540bSBrian Foster return xfs_lowbit64(rec->ir_free); 97326dd5217SBrian Foster 97426dd5217SBrian Foster realfree = xfs_inobt_irec_to_allocmask(rec); 97526dd5217SBrian Foster realfree &= rec->ir_free; 97626dd5217SBrian Foster 97726dd5217SBrian Foster return xfs_lowbit64(realfree); 978d4cc540bSBrian Foster } 979d4cc540bSBrian Foster 980d4cc540bSBrian Foster /* 98130f712c9SDave Chinner * Allocate an inode using the inobt-only algorithm. 98230f712c9SDave Chinner */ 98330f712c9SDave Chinner STATIC int 98430f712c9SDave Chinner xfs_dialloc_ag_inobt( 98530f712c9SDave Chinner struct xfs_trans *tp, 98630f712c9SDave Chinner struct xfs_buf *agbp, 9877b13c515SDave Chinner struct xfs_perag *pag, 98830f712c9SDave Chinner xfs_ino_t parent, 98930f712c9SDave Chinner xfs_ino_t *inop) 99030f712c9SDave Chinner { 99130f712c9SDave Chinner struct xfs_mount *mp = tp->t_mountp; 992370c782bSChristoph Hellwig struct xfs_agi *agi = agbp->b_addr; 99330f712c9SDave Chinner xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent); 99430f712c9SDave Chinner xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent); 99530f712c9SDave Chinner struct xfs_btree_cur *cur, *tcur; 99630f712c9SDave Chinner struct xfs_inobt_rec_incore rec, trec; 99730f712c9SDave Chinner xfs_ino_t ino; 99830f712c9SDave Chinner int error; 99930f712c9SDave Chinner int offset; 100030f712c9SDave Chinner int i, j; 10012d32311cSCarlos Maiolino int searchdistance = 10; 100230f712c9SDave Chinner 100330f712c9SDave Chinner ASSERT(pag->pagi_init); 100430f712c9SDave Chinner ASSERT(pag->pagi_inodeok); 100530f712c9SDave Chinner ASSERT(pag->pagi_freecount > 0); 100630f712c9SDave Chinner 100730f712c9SDave Chinner restart_pagno: 10087b13c515SDave Chinner cur = xfs_inobt_init_cursor(mp, tp, agbp, pag, XFS_BTNUM_INO); 100930f712c9SDave Chinner /* 101030f712c9SDave Chinner * If pagino is 0 (this is the root inode allocation) use newino. 101130f712c9SDave Chinner * This must work because we've just allocated some. 101230f712c9SDave Chinner */ 101330f712c9SDave Chinner if (!pagino) 101430f712c9SDave Chinner pagino = be32_to_cpu(agi->agi_newino); 101530f712c9SDave Chinner 1016*9ba0889eSDave Chinner error = xfs_check_agi_freecount(cur); 101730f712c9SDave Chinner if (error) 101830f712c9SDave Chinner goto error0; 101930f712c9SDave Chinner 102030f712c9SDave Chinner /* 102130f712c9SDave Chinner * If in the same AG as the parent, try to get near the parent. 102230f712c9SDave Chinner */ 10237b13c515SDave Chinner if (pagno == pag->pag_agno) { 102430f712c9SDave Chinner int doneleft; /* done, to the left */ 102530f712c9SDave Chinner int doneright; /* done, to the right */ 102630f712c9SDave Chinner 102730f712c9SDave Chinner error = xfs_inobt_lookup(cur, pagino, XFS_LOOKUP_LE, &i); 102830f712c9SDave Chinner if (error) 102930f712c9SDave Chinner goto error0; 1030f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(mp, i != 1)) { 1031f9e03706SDarrick J. Wong error = -EFSCORRUPTED; 1032f9e03706SDarrick J. Wong goto error0; 1033f9e03706SDarrick J. Wong } 103430f712c9SDave Chinner 103530f712c9SDave Chinner error = xfs_inobt_get_rec(cur, &rec, &j); 103630f712c9SDave Chinner if (error) 103730f712c9SDave Chinner goto error0; 1038f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(mp, j != 1)) { 1039f9e03706SDarrick J. Wong error = -EFSCORRUPTED; 1040f9e03706SDarrick J. Wong goto error0; 1041f9e03706SDarrick J. Wong } 104230f712c9SDave Chinner 104330f712c9SDave Chinner if (rec.ir_freecount > 0) { 104430f712c9SDave Chinner /* 104530f712c9SDave Chinner * Found a free inode in the same chunk 104630f712c9SDave Chinner * as the parent, done. 104730f712c9SDave Chinner */ 104830f712c9SDave Chinner goto alloc_inode; 104930f712c9SDave Chinner } 105030f712c9SDave Chinner 105130f712c9SDave Chinner 105230f712c9SDave Chinner /* 105330f712c9SDave Chinner * In the same AG as parent, but parent's chunk is full. 105430f712c9SDave Chinner */ 105530f712c9SDave Chinner 105630f712c9SDave Chinner /* duplicate the cursor, search left & right simultaneously */ 105730f712c9SDave Chinner error = xfs_btree_dup_cursor(cur, &tcur); 105830f712c9SDave Chinner if (error) 105930f712c9SDave Chinner goto error0; 106030f712c9SDave Chinner 106130f712c9SDave Chinner /* 106230f712c9SDave Chinner * Skip to last blocks looked up if same parent inode. 106330f712c9SDave Chinner */ 106430f712c9SDave Chinner if (pagino != NULLAGINO && 106530f712c9SDave Chinner pag->pagl_pagino == pagino && 106630f712c9SDave Chinner pag->pagl_leftrec != NULLAGINO && 106730f712c9SDave Chinner pag->pagl_rightrec != NULLAGINO) { 106830f712c9SDave Chinner error = xfs_ialloc_get_rec(tcur, pag->pagl_leftrec, 106930f712c9SDave Chinner &trec, &doneleft); 107030f712c9SDave Chinner if (error) 107130f712c9SDave Chinner goto error1; 107230f712c9SDave Chinner 107330f712c9SDave Chinner error = xfs_ialloc_get_rec(cur, pag->pagl_rightrec, 107430f712c9SDave Chinner &rec, &doneright); 107530f712c9SDave Chinner if (error) 107630f712c9SDave Chinner goto error1; 107730f712c9SDave Chinner } else { 107830f712c9SDave Chinner /* search left with tcur, back up 1 record */ 107930f712c9SDave Chinner error = xfs_ialloc_next_rec(tcur, &trec, &doneleft, 1); 108030f712c9SDave Chinner if (error) 108130f712c9SDave Chinner goto error1; 108230f712c9SDave Chinner 108330f712c9SDave Chinner /* search right with cur, go forward 1 record. */ 108430f712c9SDave Chinner error = xfs_ialloc_next_rec(cur, &rec, &doneright, 0); 108530f712c9SDave Chinner if (error) 108630f712c9SDave Chinner goto error1; 108730f712c9SDave Chinner } 108830f712c9SDave Chinner 108930f712c9SDave Chinner /* 109030f712c9SDave Chinner * Loop until we find an inode chunk with a free inode. 109130f712c9SDave Chinner */ 10922d32311cSCarlos Maiolino while (--searchdistance > 0 && (!doneleft || !doneright)) { 109330f712c9SDave Chinner int useleft; /* using left inode chunk this time */ 109430f712c9SDave Chinner 109530f712c9SDave Chinner /* figure out the closer block if both are valid. */ 109630f712c9SDave Chinner if (!doneleft && !doneright) { 109730f712c9SDave Chinner useleft = pagino - 109830f712c9SDave Chinner (trec.ir_startino + XFS_INODES_PER_CHUNK - 1) < 109930f712c9SDave Chinner rec.ir_startino - pagino; 110030f712c9SDave Chinner } else { 110130f712c9SDave Chinner useleft = !doneleft; 110230f712c9SDave Chinner } 110330f712c9SDave Chinner 110430f712c9SDave Chinner /* free inodes to the left? */ 110530f712c9SDave Chinner if (useleft && trec.ir_freecount) { 110630f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); 110730f712c9SDave Chinner cur = tcur; 110830f712c9SDave Chinner 110930f712c9SDave Chinner pag->pagl_leftrec = trec.ir_startino; 111030f712c9SDave Chinner pag->pagl_rightrec = rec.ir_startino; 111130f712c9SDave Chinner pag->pagl_pagino = pagino; 1112c44245b3SOmar Sandoval rec = trec; 111330f712c9SDave Chinner goto alloc_inode; 111430f712c9SDave Chinner } 111530f712c9SDave Chinner 111630f712c9SDave Chinner /* free inodes to the right? */ 111730f712c9SDave Chinner if (!useleft && rec.ir_freecount) { 111830f712c9SDave Chinner xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); 111930f712c9SDave Chinner 112030f712c9SDave Chinner pag->pagl_leftrec = trec.ir_startino; 112130f712c9SDave Chinner pag->pagl_rightrec = rec.ir_startino; 112230f712c9SDave Chinner pag->pagl_pagino = pagino; 112330f712c9SDave Chinner goto alloc_inode; 112430f712c9SDave Chinner } 112530f712c9SDave Chinner 112630f712c9SDave Chinner /* get next record to check */ 112730f712c9SDave Chinner if (useleft) { 112830f712c9SDave Chinner error = xfs_ialloc_next_rec(tcur, &trec, 112930f712c9SDave Chinner &doneleft, 1); 113030f712c9SDave Chinner } else { 113130f712c9SDave Chinner error = xfs_ialloc_next_rec(cur, &rec, 113230f712c9SDave Chinner &doneright, 0); 113330f712c9SDave Chinner } 113430f712c9SDave Chinner if (error) 113530f712c9SDave Chinner goto error1; 113630f712c9SDave Chinner } 113730f712c9SDave Chinner 11382d32311cSCarlos Maiolino if (searchdistance <= 0) { 11392d32311cSCarlos Maiolino /* 11402d32311cSCarlos Maiolino * Not in range - save last search 11412d32311cSCarlos Maiolino * location and allocate a new inode 11422d32311cSCarlos Maiolino */ 11432d32311cSCarlos Maiolino xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); 11442d32311cSCarlos Maiolino pag->pagl_leftrec = trec.ir_startino; 11452d32311cSCarlos Maiolino pag->pagl_rightrec = rec.ir_startino; 11462d32311cSCarlos Maiolino pag->pagl_pagino = pagino; 11472d32311cSCarlos Maiolino 11482d32311cSCarlos Maiolino } else { 114930f712c9SDave Chinner /* 115030f712c9SDave Chinner * We've reached the end of the btree. because 115130f712c9SDave Chinner * we are only searching a small chunk of the 115230f712c9SDave Chinner * btree each search, there is obviously free 115330f712c9SDave Chinner * inodes closer to the parent inode than we 115430f712c9SDave Chinner * are now. restart the search again. 115530f712c9SDave Chinner */ 115630f712c9SDave Chinner pag->pagl_pagino = NULLAGINO; 115730f712c9SDave Chinner pag->pagl_leftrec = NULLAGINO; 115830f712c9SDave Chinner pag->pagl_rightrec = NULLAGINO; 115930f712c9SDave Chinner xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); 116030f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); 116130f712c9SDave Chinner goto restart_pagno; 116230f712c9SDave Chinner } 11632d32311cSCarlos Maiolino } 116430f712c9SDave Chinner 116530f712c9SDave Chinner /* 116630f712c9SDave Chinner * In a different AG from the parent. 116730f712c9SDave Chinner * See if the most recently allocated block has any free. 116830f712c9SDave Chinner */ 116930f712c9SDave Chinner if (agi->agi_newino != cpu_to_be32(NULLAGINO)) { 117030f712c9SDave Chinner error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino), 117130f712c9SDave Chinner XFS_LOOKUP_EQ, &i); 117230f712c9SDave Chinner if (error) 117330f712c9SDave Chinner goto error0; 117430f712c9SDave Chinner 117530f712c9SDave Chinner if (i == 1) { 117630f712c9SDave Chinner error = xfs_inobt_get_rec(cur, &rec, &j); 117730f712c9SDave Chinner if (error) 117830f712c9SDave Chinner goto error0; 117930f712c9SDave Chinner 118030f712c9SDave Chinner if (j == 1 && rec.ir_freecount > 0) { 118130f712c9SDave Chinner /* 118230f712c9SDave Chinner * The last chunk allocated in the group 118330f712c9SDave Chinner * still has a free inode. 118430f712c9SDave Chinner */ 118530f712c9SDave Chinner goto alloc_inode; 118630f712c9SDave Chinner } 118730f712c9SDave Chinner } 118830f712c9SDave Chinner } 118930f712c9SDave Chinner 119030f712c9SDave Chinner /* 119130f712c9SDave Chinner * None left in the last group, search the whole AG 119230f712c9SDave Chinner */ 119330f712c9SDave Chinner error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i); 119430f712c9SDave Chinner if (error) 119530f712c9SDave Chinner goto error0; 1196f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(mp, i != 1)) { 1197f9e03706SDarrick J. Wong error = -EFSCORRUPTED; 1198f9e03706SDarrick J. Wong goto error0; 1199f9e03706SDarrick J. Wong } 120030f712c9SDave Chinner 120130f712c9SDave Chinner for (;;) { 120230f712c9SDave Chinner error = xfs_inobt_get_rec(cur, &rec, &i); 120330f712c9SDave Chinner if (error) 120430f712c9SDave Chinner goto error0; 1205f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(mp, i != 1)) { 1206f9e03706SDarrick J. Wong error = -EFSCORRUPTED; 1207f9e03706SDarrick J. Wong goto error0; 1208f9e03706SDarrick J. Wong } 120930f712c9SDave Chinner if (rec.ir_freecount > 0) 121030f712c9SDave Chinner break; 121130f712c9SDave Chinner error = xfs_btree_increment(cur, 0, &i); 121230f712c9SDave Chinner if (error) 121330f712c9SDave Chinner goto error0; 1214f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(mp, i != 1)) { 1215f9e03706SDarrick J. Wong error = -EFSCORRUPTED; 1216f9e03706SDarrick J. Wong goto error0; 1217f9e03706SDarrick J. Wong } 121830f712c9SDave Chinner } 121930f712c9SDave Chinner 122030f712c9SDave Chinner alloc_inode: 1221d4cc540bSBrian Foster offset = xfs_inobt_first_free_inode(&rec); 122230f712c9SDave Chinner ASSERT(offset >= 0); 122330f712c9SDave Chinner ASSERT(offset < XFS_INODES_PER_CHUNK); 122430f712c9SDave Chinner ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) % 122530f712c9SDave Chinner XFS_INODES_PER_CHUNK) == 0); 12267b13c515SDave Chinner ino = XFS_AGINO_TO_INO(mp, pag->pag_agno, rec.ir_startino + offset); 122730f712c9SDave Chinner rec.ir_free &= ~XFS_INOBT_MASK(offset); 122830f712c9SDave Chinner rec.ir_freecount--; 122930f712c9SDave Chinner error = xfs_inobt_update(cur, &rec); 123030f712c9SDave Chinner if (error) 123130f712c9SDave Chinner goto error0; 123230f712c9SDave Chinner be32_add_cpu(&agi->agi_freecount, -1); 123330f712c9SDave Chinner xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT); 123430f712c9SDave Chinner pag->pagi_freecount--; 123530f712c9SDave Chinner 1236*9ba0889eSDave Chinner error = xfs_check_agi_freecount(cur); 123730f712c9SDave Chinner if (error) 123830f712c9SDave Chinner goto error0; 123930f712c9SDave Chinner 124030f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); 124130f712c9SDave Chinner xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1); 124230f712c9SDave Chinner *inop = ino; 124330f712c9SDave Chinner return 0; 124430f712c9SDave Chinner error1: 124530f712c9SDave Chinner xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR); 124630f712c9SDave Chinner error0: 124730f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); 124830f712c9SDave Chinner return error; 124930f712c9SDave Chinner } 125030f712c9SDave Chinner 125130f712c9SDave Chinner /* 125230f712c9SDave Chinner * Use the free inode btree to allocate an inode based on distance from the 125330f712c9SDave Chinner * parent. Note that the provided cursor may be deleted and replaced. 125430f712c9SDave Chinner */ 125530f712c9SDave Chinner STATIC int 125630f712c9SDave Chinner xfs_dialloc_ag_finobt_near( 125730f712c9SDave Chinner xfs_agino_t pagino, 125830f712c9SDave Chinner struct xfs_btree_cur **ocur, 125930f712c9SDave Chinner struct xfs_inobt_rec_incore *rec) 126030f712c9SDave Chinner { 126130f712c9SDave Chinner struct xfs_btree_cur *lcur = *ocur; /* left search cursor */ 126230f712c9SDave Chinner struct xfs_btree_cur *rcur; /* right search cursor */ 126330f712c9SDave Chinner struct xfs_inobt_rec_incore rrec; 126430f712c9SDave Chinner int error; 126530f712c9SDave Chinner int i, j; 126630f712c9SDave Chinner 126730f712c9SDave Chinner error = xfs_inobt_lookup(lcur, pagino, XFS_LOOKUP_LE, &i); 126830f712c9SDave Chinner if (error) 126930f712c9SDave Chinner return error; 127030f712c9SDave Chinner 127130f712c9SDave Chinner if (i == 1) { 127230f712c9SDave Chinner error = xfs_inobt_get_rec(lcur, rec, &i); 127330f712c9SDave Chinner if (error) 127430f712c9SDave Chinner return error; 1275f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(lcur->bc_mp, i != 1)) 1276f9e03706SDarrick J. Wong return -EFSCORRUPTED; 127730f712c9SDave Chinner 127830f712c9SDave Chinner /* 127930f712c9SDave Chinner * See if we've landed in the parent inode record. The finobt 128030f712c9SDave Chinner * only tracks chunks with at least one free inode, so record 128130f712c9SDave Chinner * existence is enough. 128230f712c9SDave Chinner */ 128330f712c9SDave Chinner if (pagino >= rec->ir_startino && 128430f712c9SDave Chinner pagino < (rec->ir_startino + XFS_INODES_PER_CHUNK)) 128530f712c9SDave Chinner return 0; 128630f712c9SDave Chinner } 128730f712c9SDave Chinner 128830f712c9SDave Chinner error = xfs_btree_dup_cursor(lcur, &rcur); 128930f712c9SDave Chinner if (error) 129030f712c9SDave Chinner return error; 129130f712c9SDave Chinner 129230f712c9SDave Chinner error = xfs_inobt_lookup(rcur, pagino, XFS_LOOKUP_GE, &j); 129330f712c9SDave Chinner if (error) 129430f712c9SDave Chinner goto error_rcur; 129530f712c9SDave Chinner if (j == 1) { 129630f712c9SDave Chinner error = xfs_inobt_get_rec(rcur, &rrec, &j); 129730f712c9SDave Chinner if (error) 129830f712c9SDave Chinner goto error_rcur; 1299f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(lcur->bc_mp, j != 1)) { 1300f9e03706SDarrick J. Wong error = -EFSCORRUPTED; 1301f9e03706SDarrick J. Wong goto error_rcur; 1302f9e03706SDarrick J. Wong } 130330f712c9SDave Chinner } 130430f712c9SDave Chinner 1305f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(lcur->bc_mp, i != 1 && j != 1)) { 1306f9e03706SDarrick J. Wong error = -EFSCORRUPTED; 1307f9e03706SDarrick J. Wong goto error_rcur; 1308f9e03706SDarrick J. Wong } 130930f712c9SDave Chinner if (i == 1 && j == 1) { 131030f712c9SDave Chinner /* 131130f712c9SDave Chinner * Both the left and right records are valid. Choose the closer 131230f712c9SDave Chinner * inode chunk to the target. 131330f712c9SDave Chinner */ 131430f712c9SDave Chinner if ((pagino - rec->ir_startino + XFS_INODES_PER_CHUNK - 1) > 131530f712c9SDave Chinner (rrec.ir_startino - pagino)) { 131630f712c9SDave Chinner *rec = rrec; 131730f712c9SDave Chinner xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR); 131830f712c9SDave Chinner *ocur = rcur; 131930f712c9SDave Chinner } else { 132030f712c9SDave Chinner xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR); 132130f712c9SDave Chinner } 132230f712c9SDave Chinner } else if (j == 1) { 132330f712c9SDave Chinner /* only the right record is valid */ 132430f712c9SDave Chinner *rec = rrec; 132530f712c9SDave Chinner xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR); 132630f712c9SDave Chinner *ocur = rcur; 132730f712c9SDave Chinner } else if (i == 1) { 132830f712c9SDave Chinner /* only the left record is valid */ 132930f712c9SDave Chinner xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR); 133030f712c9SDave Chinner } 133130f712c9SDave Chinner 133230f712c9SDave Chinner return 0; 133330f712c9SDave Chinner 133430f712c9SDave Chinner error_rcur: 133530f712c9SDave Chinner xfs_btree_del_cursor(rcur, XFS_BTREE_ERROR); 133630f712c9SDave Chinner return error; 133730f712c9SDave Chinner } 133830f712c9SDave Chinner 133930f712c9SDave Chinner /* 134030f712c9SDave Chinner * Use the free inode btree to find a free inode based on a newino hint. If 134130f712c9SDave Chinner * the hint is NULL, find the first free inode in the AG. 134230f712c9SDave Chinner */ 134330f712c9SDave Chinner STATIC int 134430f712c9SDave Chinner xfs_dialloc_ag_finobt_newino( 134530f712c9SDave Chinner struct xfs_agi *agi, 134630f712c9SDave Chinner struct xfs_btree_cur *cur, 134730f712c9SDave Chinner struct xfs_inobt_rec_incore *rec) 134830f712c9SDave Chinner { 134930f712c9SDave Chinner int error; 135030f712c9SDave Chinner int i; 135130f712c9SDave Chinner 135230f712c9SDave Chinner if (agi->agi_newino != cpu_to_be32(NULLAGINO)) { 1353e68ed775SDave Chinner error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino), 1354e68ed775SDave Chinner XFS_LOOKUP_EQ, &i); 135530f712c9SDave Chinner if (error) 135630f712c9SDave Chinner return error; 135730f712c9SDave Chinner if (i == 1) { 135830f712c9SDave Chinner error = xfs_inobt_get_rec(cur, rec, &i); 135930f712c9SDave Chinner if (error) 136030f712c9SDave Chinner return error; 1361f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) 1362f9e03706SDarrick J. Wong return -EFSCORRUPTED; 136330f712c9SDave Chinner return 0; 136430f712c9SDave Chinner } 136530f712c9SDave Chinner } 136630f712c9SDave Chinner 136730f712c9SDave Chinner /* 136830f712c9SDave Chinner * Find the first inode available in the AG. 136930f712c9SDave Chinner */ 137030f712c9SDave Chinner error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i); 137130f712c9SDave Chinner if (error) 137230f712c9SDave Chinner return error; 1373f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) 1374f9e03706SDarrick J. Wong return -EFSCORRUPTED; 137530f712c9SDave Chinner 137630f712c9SDave Chinner error = xfs_inobt_get_rec(cur, rec, &i); 137730f712c9SDave Chinner if (error) 137830f712c9SDave Chinner return error; 1379f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) 1380f9e03706SDarrick J. Wong return -EFSCORRUPTED; 138130f712c9SDave Chinner 138230f712c9SDave Chinner return 0; 138330f712c9SDave Chinner } 138430f712c9SDave Chinner 138530f712c9SDave Chinner /* 138630f712c9SDave Chinner * Update the inobt based on a modification made to the finobt. Also ensure that 138730f712c9SDave Chinner * the records from both trees are equivalent post-modification. 138830f712c9SDave Chinner */ 138930f712c9SDave Chinner STATIC int 139030f712c9SDave Chinner xfs_dialloc_ag_update_inobt( 139130f712c9SDave Chinner struct xfs_btree_cur *cur, /* inobt cursor */ 139230f712c9SDave Chinner struct xfs_inobt_rec_incore *frec, /* finobt record */ 139330f712c9SDave Chinner int offset) /* inode offset */ 139430f712c9SDave Chinner { 139530f712c9SDave Chinner struct xfs_inobt_rec_incore rec; 139630f712c9SDave Chinner int error; 139730f712c9SDave Chinner int i; 139830f712c9SDave Chinner 139930f712c9SDave Chinner error = xfs_inobt_lookup(cur, frec->ir_startino, XFS_LOOKUP_EQ, &i); 140030f712c9SDave Chinner if (error) 140130f712c9SDave Chinner return error; 1402f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) 1403f9e03706SDarrick J. Wong return -EFSCORRUPTED; 140430f712c9SDave Chinner 140530f712c9SDave Chinner error = xfs_inobt_get_rec(cur, &rec, &i); 140630f712c9SDave Chinner if (error) 140730f712c9SDave Chinner return error; 1408f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) 1409f9e03706SDarrick J. Wong return -EFSCORRUPTED; 141030f712c9SDave Chinner ASSERT((XFS_AGINO_TO_OFFSET(cur->bc_mp, rec.ir_startino) % 141130f712c9SDave Chinner XFS_INODES_PER_CHUNK) == 0); 141230f712c9SDave Chinner 141330f712c9SDave Chinner rec.ir_free &= ~XFS_INOBT_MASK(offset); 141430f712c9SDave Chinner rec.ir_freecount--; 141530f712c9SDave Chinner 1416f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(cur->bc_mp, 1417f9e03706SDarrick J. Wong rec.ir_free != frec->ir_free || 1418f9e03706SDarrick J. Wong rec.ir_freecount != frec->ir_freecount)) 1419f9e03706SDarrick J. Wong return -EFSCORRUPTED; 142030f712c9SDave Chinner 1421b72091f2Skbuild test robot return xfs_inobt_update(cur, &rec); 142230f712c9SDave Chinner } 142330f712c9SDave Chinner 142430f712c9SDave Chinner /* 142530f712c9SDave Chinner * Allocate an inode using the free inode btree, if available. Otherwise, fall 142630f712c9SDave Chinner * back to the inobt search algorithm. 142730f712c9SDave Chinner * 142830f712c9SDave Chinner * The caller selected an AG for us, and made sure that free inodes are 142930f712c9SDave Chinner * available. 143030f712c9SDave Chinner */ 1431b652afd9SDave Chinner static int 143230f712c9SDave Chinner xfs_dialloc_ag( 143330f712c9SDave Chinner struct xfs_trans *tp, 143430f712c9SDave Chinner struct xfs_buf *agbp, 1435309161f6SDave Chinner struct xfs_perag *pag, 143630f712c9SDave Chinner xfs_ino_t parent, 143730f712c9SDave Chinner xfs_ino_t *inop) 143830f712c9SDave Chinner { 143930f712c9SDave Chinner struct xfs_mount *mp = tp->t_mountp; 1440370c782bSChristoph Hellwig struct xfs_agi *agi = agbp->b_addr; 144130f712c9SDave Chinner xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent); 144230f712c9SDave Chinner xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent); 144330f712c9SDave Chinner struct xfs_btree_cur *cur; /* finobt cursor */ 144430f712c9SDave Chinner struct xfs_btree_cur *icur; /* inobt cursor */ 144530f712c9SDave Chinner struct xfs_inobt_rec_incore rec; 144630f712c9SDave Chinner xfs_ino_t ino; 144730f712c9SDave Chinner int error; 144830f712c9SDave Chinner int offset; 144930f712c9SDave Chinner int i; 145030f712c9SDave Chinner 145130f712c9SDave Chinner if (!xfs_sb_version_hasfinobt(&mp->m_sb)) 14527b13c515SDave Chinner return xfs_dialloc_ag_inobt(tp, agbp, pag, parent, inop); 145330f712c9SDave Chinner 145430f712c9SDave Chinner /* 145530f712c9SDave Chinner * If pagino is 0 (this is the root inode allocation) use newino. 145630f712c9SDave Chinner * This must work because we've just allocated some. 145730f712c9SDave Chinner */ 145830f712c9SDave Chinner if (!pagino) 145930f712c9SDave Chinner pagino = be32_to_cpu(agi->agi_newino); 146030f712c9SDave Chinner 14617b13c515SDave Chinner cur = xfs_inobt_init_cursor(mp, tp, agbp, pag, XFS_BTNUM_FINO); 146230f712c9SDave Chinner 1463*9ba0889eSDave Chinner error = xfs_check_agi_freecount(cur); 146430f712c9SDave Chinner if (error) 146530f712c9SDave Chinner goto error_cur; 146630f712c9SDave Chinner 146730f712c9SDave Chinner /* 146830f712c9SDave Chinner * The search algorithm depends on whether we're in the same AG as the 146930f712c9SDave Chinner * parent. If so, find the closest available inode to the parent. If 147030f712c9SDave Chinner * not, consider the agi hint or find the first free inode in the AG. 147130f712c9SDave Chinner */ 14727b13c515SDave Chinner if (pag->pag_agno == pagno) 147330f712c9SDave Chinner error = xfs_dialloc_ag_finobt_near(pagino, &cur, &rec); 147430f712c9SDave Chinner else 147530f712c9SDave Chinner error = xfs_dialloc_ag_finobt_newino(agi, cur, &rec); 147630f712c9SDave Chinner if (error) 147730f712c9SDave Chinner goto error_cur; 147830f712c9SDave Chinner 1479d4cc540bSBrian Foster offset = xfs_inobt_first_free_inode(&rec); 148030f712c9SDave Chinner ASSERT(offset >= 0); 148130f712c9SDave Chinner ASSERT(offset < XFS_INODES_PER_CHUNK); 148230f712c9SDave Chinner ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) % 148330f712c9SDave Chinner XFS_INODES_PER_CHUNK) == 0); 14847b13c515SDave Chinner ino = XFS_AGINO_TO_INO(mp, pag->pag_agno, rec.ir_startino + offset); 148530f712c9SDave Chinner 148630f712c9SDave Chinner /* 148730f712c9SDave Chinner * Modify or remove the finobt record. 148830f712c9SDave Chinner */ 148930f712c9SDave Chinner rec.ir_free &= ~XFS_INOBT_MASK(offset); 149030f712c9SDave Chinner rec.ir_freecount--; 149130f712c9SDave Chinner if (rec.ir_freecount) 149230f712c9SDave Chinner error = xfs_inobt_update(cur, &rec); 149330f712c9SDave Chinner else 149430f712c9SDave Chinner error = xfs_btree_delete(cur, &i); 149530f712c9SDave Chinner if (error) 149630f712c9SDave Chinner goto error_cur; 149730f712c9SDave Chinner 149830f712c9SDave Chinner /* 149930f712c9SDave Chinner * The finobt has now been updated appropriately. We haven't updated the 150030f712c9SDave Chinner * agi and superblock yet, so we can create an inobt cursor and validate 150130f712c9SDave Chinner * the original freecount. If all is well, make the equivalent update to 150230f712c9SDave Chinner * the inobt using the finobt record and offset information. 150330f712c9SDave Chinner */ 15047b13c515SDave Chinner icur = xfs_inobt_init_cursor(mp, tp, agbp, pag, XFS_BTNUM_INO); 150530f712c9SDave Chinner 1506*9ba0889eSDave Chinner error = xfs_check_agi_freecount(icur); 150730f712c9SDave Chinner if (error) 150830f712c9SDave Chinner goto error_icur; 150930f712c9SDave Chinner 151030f712c9SDave Chinner error = xfs_dialloc_ag_update_inobt(icur, &rec, offset); 151130f712c9SDave Chinner if (error) 151230f712c9SDave Chinner goto error_icur; 151330f712c9SDave Chinner 151430f712c9SDave Chinner /* 151530f712c9SDave Chinner * Both trees have now been updated. We must update the perag and 151630f712c9SDave Chinner * superblock before we can check the freecount for each btree. 151730f712c9SDave Chinner */ 151830f712c9SDave Chinner be32_add_cpu(&agi->agi_freecount, -1); 151930f712c9SDave Chinner xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT); 15207b13c515SDave Chinner pag->pagi_freecount--; 152130f712c9SDave Chinner 152230f712c9SDave Chinner xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1); 152330f712c9SDave Chinner 1524*9ba0889eSDave Chinner error = xfs_check_agi_freecount(icur); 152530f712c9SDave Chinner if (error) 152630f712c9SDave Chinner goto error_icur; 1527*9ba0889eSDave Chinner error = xfs_check_agi_freecount(cur); 152830f712c9SDave Chinner if (error) 152930f712c9SDave Chinner goto error_icur; 153030f712c9SDave Chinner 153130f712c9SDave Chinner xfs_btree_del_cursor(icur, XFS_BTREE_NOERROR); 153230f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); 153330f712c9SDave Chinner *inop = ino; 153430f712c9SDave Chinner return 0; 153530f712c9SDave Chinner 153630f712c9SDave Chinner error_icur: 153730f712c9SDave Chinner xfs_btree_del_cursor(icur, XFS_BTREE_ERROR); 153830f712c9SDave Chinner error_cur: 153930f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); 154030f712c9SDave Chinner return error; 154130f712c9SDave Chinner } 154230f712c9SDave Chinner 1543f3bf6e0fSDave Chinner static int 1544aececc9fSDave Chinner xfs_dialloc_roll( 1545aececc9fSDave Chinner struct xfs_trans **tpp, 1546aececc9fSDave Chinner struct xfs_buf *agibp) 1547aececc9fSDave Chinner { 1548aececc9fSDave Chinner struct xfs_trans *tp = *tpp; 1549aececc9fSDave Chinner struct xfs_dquot_acct *dqinfo; 1550aececc9fSDave Chinner int error; 1551aececc9fSDave Chinner 1552aececc9fSDave Chinner /* 1553aececc9fSDave Chinner * Hold to on to the agibp across the commit so no other allocation can 1554aececc9fSDave Chinner * come in and take the free inodes we just allocated for our caller. 1555aececc9fSDave Chinner */ 1556aececc9fSDave Chinner xfs_trans_bhold(tp, agibp); 1557aececc9fSDave Chinner 1558aececc9fSDave Chinner /* 1559aececc9fSDave Chinner * We want the quota changes to be associated with the next transaction, 1560aececc9fSDave Chinner * NOT this one. So, detach the dqinfo from this and attach it to the 1561aececc9fSDave Chinner * next transaction. 1562aececc9fSDave Chinner */ 1563aececc9fSDave Chinner dqinfo = tp->t_dqinfo; 1564aececc9fSDave Chinner tp->t_dqinfo = NULL; 1565aececc9fSDave Chinner 1566aececc9fSDave Chinner error = xfs_trans_roll(&tp); 1567aececc9fSDave Chinner 1568aececc9fSDave Chinner /* Re-attach the quota info that we detached from prev trx. */ 1569aececc9fSDave Chinner tp->t_dqinfo = dqinfo; 1570aececc9fSDave Chinner 15718237fbf5SDave Chinner /* 15728237fbf5SDave Chinner * Join the buffer even on commit error so that the buffer is released 15738237fbf5SDave Chinner * when the caller cancels the transaction and doesn't have to handle 15748237fbf5SDave Chinner * this error case specially. 15758237fbf5SDave Chinner */ 1576aececc9fSDave Chinner xfs_trans_bjoin(tp, agibp); 15778237fbf5SDave Chinner *tpp = tp; 15788237fbf5SDave Chinner return error; 1579aececc9fSDave Chinner } 1580aececc9fSDave Chinner 15818237fbf5SDave Chinner static xfs_agnumber_t 158289b1f55aSDave Chinner xfs_ialloc_next_ag( 158389b1f55aSDave Chinner xfs_mount_t *mp) 158489b1f55aSDave Chinner { 158589b1f55aSDave Chinner xfs_agnumber_t agno; 158689b1f55aSDave Chinner 158789b1f55aSDave Chinner spin_lock(&mp->m_agirotor_lock); 158889b1f55aSDave Chinner agno = mp->m_agirotor; 158989b1f55aSDave Chinner if (++mp->m_agirotor >= mp->m_maxagi) 159089b1f55aSDave Chinner mp->m_agirotor = 0; 159189b1f55aSDave Chinner spin_unlock(&mp->m_agirotor_lock); 159289b1f55aSDave Chinner 159389b1f55aSDave Chinner return agno; 159489b1f55aSDave Chinner } 159589b1f55aSDave Chinner 15968237fbf5SDave Chinner static bool 15978237fbf5SDave Chinner xfs_dialloc_good_ag( 15988237fbf5SDave Chinner struct xfs_trans *tp, 15998237fbf5SDave Chinner struct xfs_perag *pag, 16008237fbf5SDave Chinner umode_t mode, 16018237fbf5SDave Chinner int flags, 16028237fbf5SDave Chinner bool ok_alloc) 16038237fbf5SDave Chinner { 16048237fbf5SDave Chinner struct xfs_mount *mp = tp->t_mountp; 16058237fbf5SDave Chinner xfs_extlen_t ineed; 16068237fbf5SDave Chinner xfs_extlen_t longest = 0; 16078237fbf5SDave Chinner int needspace; 16088237fbf5SDave Chinner int error; 16098237fbf5SDave Chinner 16108237fbf5SDave Chinner if (!pag->pagi_inodeok) 16118237fbf5SDave Chinner return false; 16128237fbf5SDave Chinner 16138237fbf5SDave Chinner if (!pag->pagi_init) { 16148237fbf5SDave Chinner error = xfs_ialloc_pagi_init(mp, tp, pag->pag_agno); 16158237fbf5SDave Chinner if (error) 16168237fbf5SDave Chinner return false; 16178237fbf5SDave Chinner } 16188237fbf5SDave Chinner 16198237fbf5SDave Chinner if (pag->pagi_freecount) 16208237fbf5SDave Chinner return true; 16218237fbf5SDave Chinner if (!ok_alloc) 16228237fbf5SDave Chinner return false; 16238237fbf5SDave Chinner 16248237fbf5SDave Chinner if (!pag->pagf_init) { 16258237fbf5SDave Chinner error = xfs_alloc_pagf_init(mp, tp, pag->pag_agno, flags); 16268237fbf5SDave Chinner if (error) 16278237fbf5SDave Chinner return false; 16288237fbf5SDave Chinner } 16298237fbf5SDave Chinner 163030f712c9SDave Chinner /* 16318237fbf5SDave Chinner * Check that there is enough free space for the file plus a chunk of 16328237fbf5SDave Chinner * inodes if we need to allocate some. If this is the first pass across 16338237fbf5SDave Chinner * the AGs, take into account the potential space needed for alignment 16348237fbf5SDave Chinner * of inode chunks when checking the longest contiguous free space in 16358237fbf5SDave Chinner * the AG - this prevents us from getting ENOSPC because we have free 16368237fbf5SDave Chinner * space larger than ialloc_blks but alignment constraints prevent us 16378237fbf5SDave Chinner * from using it. 16388237fbf5SDave Chinner * 16398237fbf5SDave Chinner * If we can't find an AG with space for full alignment slack to be 16408237fbf5SDave Chinner * taken into account, we must be near ENOSPC in all AGs. Hence we 16418237fbf5SDave Chinner * don't include alignment for the second pass and so if we fail 16428237fbf5SDave Chinner * allocation due to alignment issues then it is most likely a real 16438237fbf5SDave Chinner * ENOSPC condition. 16448237fbf5SDave Chinner * 16458237fbf5SDave Chinner * XXX(dgc): this calculation is now bogus thanks to the per-ag 16468237fbf5SDave Chinner * reservations that xfs_alloc_fix_freelist() now does via 16478237fbf5SDave Chinner * xfs_alloc_space_available(). When the AG fills up, pagf_freeblks will 16488237fbf5SDave Chinner * be more than large enough for the check below to succeed, but 16498237fbf5SDave Chinner * xfs_alloc_space_available() will fail because of the non-zero 16508237fbf5SDave Chinner * metadata reservation and hence we won't actually be able to allocate 16518237fbf5SDave Chinner * more inodes in this AG. We do soooo much unnecessary work near ENOSPC 16528237fbf5SDave Chinner * because of this. 16538237fbf5SDave Chinner */ 16548237fbf5SDave Chinner ineed = M_IGEO(mp)->ialloc_min_blks; 16558237fbf5SDave Chinner if (flags && ineed > 1) 16568237fbf5SDave Chinner ineed += M_IGEO(mp)->cluster_align; 16578237fbf5SDave Chinner longest = pag->pagf_longest; 16588237fbf5SDave Chinner if (!longest) 16598237fbf5SDave Chinner longest = pag->pagf_flcount > 0; 16608237fbf5SDave Chinner needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode); 16618237fbf5SDave Chinner 16628237fbf5SDave Chinner if (pag->pagf_freeblks < needspace + ineed || longest < ineed) 16638237fbf5SDave Chinner return false; 16648237fbf5SDave Chinner return true; 16658237fbf5SDave Chinner } 16668237fbf5SDave Chinner 16678237fbf5SDave Chinner static int 16688237fbf5SDave Chinner xfs_dialloc_try_ag( 16698237fbf5SDave Chinner struct xfs_trans **tpp, 16708237fbf5SDave Chinner struct xfs_perag *pag, 16718237fbf5SDave Chinner xfs_ino_t parent, 16728237fbf5SDave Chinner xfs_ino_t *new_ino, 16738237fbf5SDave Chinner bool ok_alloc) 16748237fbf5SDave Chinner { 16758237fbf5SDave Chinner struct xfs_buf *agbp; 16768237fbf5SDave Chinner xfs_ino_t ino; 16778237fbf5SDave Chinner int error; 16788237fbf5SDave Chinner 16798237fbf5SDave Chinner /* 16808237fbf5SDave Chinner * Then read in the AGI buffer and recheck with the AGI buffer 16818237fbf5SDave Chinner * lock held. 16828237fbf5SDave Chinner */ 16838237fbf5SDave Chinner error = xfs_ialloc_read_agi(pag->pag_mount, *tpp, pag->pag_agno, &agbp); 16848237fbf5SDave Chinner if (error) 16858237fbf5SDave Chinner return error; 16868237fbf5SDave Chinner 16878237fbf5SDave Chinner if (!pag->pagi_freecount) { 16888237fbf5SDave Chinner if (!ok_alloc) { 16898237fbf5SDave Chinner error = -EAGAIN; 16908237fbf5SDave Chinner goto out_release; 16918237fbf5SDave Chinner } 16928237fbf5SDave Chinner 16938237fbf5SDave Chinner error = xfs_ialloc_ag_alloc(*tpp, agbp, pag); 16948237fbf5SDave Chinner if (error < 0) 16958237fbf5SDave Chinner goto out_release; 16968237fbf5SDave Chinner 16978237fbf5SDave Chinner /* 16988237fbf5SDave Chinner * We successfully allocated space for an inode cluster in this 16998237fbf5SDave Chinner * AG. Roll the transaction so that we can allocate one of the 17008237fbf5SDave Chinner * new inodes. 17018237fbf5SDave Chinner */ 17028237fbf5SDave Chinner ASSERT(pag->pagi_freecount > 0); 17038237fbf5SDave Chinner error = xfs_dialloc_roll(tpp, agbp); 17048237fbf5SDave Chinner if (error) 17058237fbf5SDave Chinner goto out_release; 17068237fbf5SDave Chinner } 17078237fbf5SDave Chinner 17088237fbf5SDave Chinner /* Allocate an inode in the found AG */ 17098237fbf5SDave Chinner error = xfs_dialloc_ag(*tpp, agbp, pag, parent, &ino); 17108237fbf5SDave Chinner if (!error) 17118237fbf5SDave Chinner *new_ino = ino; 17128237fbf5SDave Chinner return error; 17138237fbf5SDave Chinner 17148237fbf5SDave Chinner out_release: 17158237fbf5SDave Chinner xfs_trans_brelse(*tpp, agbp); 17168237fbf5SDave Chinner return error; 17178237fbf5SDave Chinner } 17188237fbf5SDave Chinner 17198237fbf5SDave Chinner /* 17208237fbf5SDave Chinner * Allocate an on-disk inode. 172130f712c9SDave Chinner * 17228d822dc3SDave Chinner * Mode is used to tell whether the new inode is a directory and hence where to 17238237fbf5SDave Chinner * locate it. The on-disk inode that is allocated will be returned in @new_ino 17248237fbf5SDave Chinner * on success, otherwise an error will be set to indicate the failure (e.g. 17258237fbf5SDave Chinner * -ENOSPC). 172630f712c9SDave Chinner */ 172730f712c9SDave Chinner int 1728b652afd9SDave Chinner xfs_dialloc( 1729f3bf6e0fSDave Chinner struct xfs_trans **tpp, 173030f712c9SDave Chinner xfs_ino_t parent, 173130f712c9SDave Chinner umode_t mode, 1732b652afd9SDave Chinner xfs_ino_t *new_ino) 173330f712c9SDave Chinner { 1734f3bf6e0fSDave Chinner struct xfs_mount *mp = (*tpp)->t_mountp; 173530f712c9SDave Chinner xfs_agnumber_t agno; 1736b652afd9SDave Chinner int error = 0; 173730f712c9SDave Chinner xfs_agnumber_t start_agno; 173830f712c9SDave Chinner struct xfs_perag *pag; 1739ef325959SDarrick J. Wong struct xfs_ino_geometry *igeo = M_IGEO(mp); 17408237fbf5SDave Chinner bool ok_alloc = true; 174189b1f55aSDave Chinner int flags; 1742b652afd9SDave Chinner xfs_ino_t ino; 17438d822dc3SDave Chinner 174430f712c9SDave Chinner /* 174589b1f55aSDave Chinner * Directories, symlinks, and regular files frequently allocate at least 174689b1f55aSDave Chinner * one block, so factor that potential expansion when we examine whether 174789b1f55aSDave Chinner * an AG has enough space for file creation. 174830f712c9SDave Chinner */ 174989b1f55aSDave Chinner if (S_ISDIR(mode)) 175089b1f55aSDave Chinner start_agno = xfs_ialloc_next_ag(mp); 175189b1f55aSDave Chinner else { 175289b1f55aSDave Chinner start_agno = XFS_INO_TO_AGNO(mp, parent); 175389b1f55aSDave Chinner if (start_agno >= mp->m_maxagi) 175489b1f55aSDave Chinner start_agno = 0; 175589b1f55aSDave Chinner } 175630f712c9SDave Chinner 175730f712c9SDave Chinner /* 175830f712c9SDave Chinner * If we have already hit the ceiling of inode blocks then clear 17598237fbf5SDave Chinner * ok_alloc so we scan all available agi structures for a free 176030f712c9SDave Chinner * inode. 176174f9ce1cSGeorge Wang * 176274f9ce1cSGeorge Wang * Read rough value of mp->m_icount by percpu_counter_read_positive, 176374f9ce1cSGeorge Wang * which will sacrifice the preciseness but improve the performance. 176430f712c9SDave Chinner */ 1765ef325959SDarrick J. Wong if (igeo->maxicount && 1766ef325959SDarrick J. Wong percpu_counter_read_positive(&mp->m_icount) + igeo->ialloc_inos 1767ef325959SDarrick J. Wong > igeo->maxicount) { 17688237fbf5SDave Chinner ok_alloc = false; 176930f712c9SDave Chinner } 177030f712c9SDave Chinner 177130f712c9SDave Chinner /* 177230f712c9SDave Chinner * Loop until we find an allocation group that either has free inodes 177330f712c9SDave Chinner * or in which we can allocate some inodes. Iterate through the 177430f712c9SDave Chinner * allocation groups upward, wrapping at the end. 177530f712c9SDave Chinner */ 177630f712c9SDave Chinner agno = start_agno; 177789b1f55aSDave Chinner flags = XFS_ALLOC_FLAG_TRYLOCK; 177830f712c9SDave Chinner for (;;) { 177930f712c9SDave Chinner pag = xfs_perag_get(mp, agno); 17808237fbf5SDave Chinner if (xfs_dialloc_good_ag(*tpp, pag, mode, flags, ok_alloc)) { 17818237fbf5SDave Chinner error = xfs_dialloc_try_ag(tpp, pag, parent, 17828237fbf5SDave Chinner &ino, ok_alloc); 17838237fbf5SDave Chinner if (error != -EAGAIN) 17848d822dc3SDave Chinner break; 178530f712c9SDave Chinner } 178630f712c9SDave Chinner 178789b1f55aSDave Chinner if (XFS_FORCED_SHUTDOWN(mp)) { 178889b1f55aSDave Chinner error = -EFSCORRUPTED; 178942685473SDave Chinner break; 179089b1f55aSDave Chinner } 179189b1f55aSDave Chinner if (++agno == mp->m_maxagi) 179289b1f55aSDave Chinner agno = 0; 179389b1f55aSDave Chinner if (agno == start_agno) { 17948237fbf5SDave Chinner if (!flags) { 17958237fbf5SDave Chinner error = -ENOSPC; 179689b1f55aSDave Chinner break; 17978237fbf5SDave Chinner } 179889b1f55aSDave Chinner flags = 0; 179989b1f55aSDave Chinner } 180042685473SDave Chinner xfs_perag_put(pag); 180130f712c9SDave Chinner } 180230f712c9SDave Chinner 18038237fbf5SDave Chinner if (!error) 1804b652afd9SDave Chinner *new_ino = ino; 18058237fbf5SDave Chinner xfs_perag_put(pag); 18068237fbf5SDave Chinner return error; 180730f712c9SDave Chinner } 180830f712c9SDave Chinner 180910ae3dc7SBrian Foster /* 181010ae3dc7SBrian Foster * Free the blocks of an inode chunk. We must consider that the inode chunk 181110ae3dc7SBrian Foster * might be sparse and only free the regions that are allocated as part of the 181210ae3dc7SBrian Foster * chunk. 181310ae3dc7SBrian Foster */ 181410ae3dc7SBrian Foster STATIC void 181510ae3dc7SBrian Foster xfs_difree_inode_chunk( 18160f37d178SBrian Foster struct xfs_trans *tp, 181710ae3dc7SBrian Foster xfs_agnumber_t agno, 18180f37d178SBrian Foster struct xfs_inobt_rec_incore *rec) 181910ae3dc7SBrian Foster { 18200f37d178SBrian Foster struct xfs_mount *mp = tp->t_mountp; 18210f37d178SBrian Foster xfs_agblock_t sagbno = XFS_AGINO_TO_AGBNO(mp, 18220f37d178SBrian Foster rec->ir_startino); 182310ae3dc7SBrian Foster int startidx, endidx; 182410ae3dc7SBrian Foster int nextbit; 182510ae3dc7SBrian Foster xfs_agblock_t agbno; 182610ae3dc7SBrian Foster int contigblk; 182710ae3dc7SBrian Foster DECLARE_BITMAP(holemask, XFS_INOBT_HOLEMASK_BITS); 182810ae3dc7SBrian Foster 182910ae3dc7SBrian Foster if (!xfs_inobt_issparse(rec->ir_holemask)) { 183010ae3dc7SBrian Foster /* not sparse, calculate extent info directly */ 18310f37d178SBrian Foster xfs_bmap_add_free(tp, XFS_AGB_TO_FSB(mp, agno, sagbno), 1832ef325959SDarrick J. Wong M_IGEO(mp)->ialloc_blks, 1833ef325959SDarrick J. Wong &XFS_RMAP_OINFO_INODES); 183410ae3dc7SBrian Foster return; 183510ae3dc7SBrian Foster } 183610ae3dc7SBrian Foster 183710ae3dc7SBrian Foster /* holemask is only 16-bits (fits in an unsigned long) */ 183810ae3dc7SBrian Foster ASSERT(sizeof(rec->ir_holemask) <= sizeof(holemask[0])); 183910ae3dc7SBrian Foster holemask[0] = rec->ir_holemask; 184010ae3dc7SBrian Foster 184110ae3dc7SBrian Foster /* 184210ae3dc7SBrian Foster * Find contiguous ranges of zeroes (i.e., allocated regions) in the 184310ae3dc7SBrian Foster * holemask and convert the start/end index of each range to an extent. 184410ae3dc7SBrian Foster * We start with the start and end index both pointing at the first 0 in 184510ae3dc7SBrian Foster * the mask. 184610ae3dc7SBrian Foster */ 184710ae3dc7SBrian Foster startidx = endidx = find_first_zero_bit(holemask, 184810ae3dc7SBrian Foster XFS_INOBT_HOLEMASK_BITS); 184910ae3dc7SBrian Foster nextbit = startidx + 1; 185010ae3dc7SBrian Foster while (startidx < XFS_INOBT_HOLEMASK_BITS) { 185110ae3dc7SBrian Foster nextbit = find_next_zero_bit(holemask, XFS_INOBT_HOLEMASK_BITS, 185210ae3dc7SBrian Foster nextbit); 185310ae3dc7SBrian Foster /* 185410ae3dc7SBrian Foster * If the next zero bit is contiguous, update the end index of 185510ae3dc7SBrian Foster * the current range and continue. 185610ae3dc7SBrian Foster */ 185710ae3dc7SBrian Foster if (nextbit != XFS_INOBT_HOLEMASK_BITS && 185810ae3dc7SBrian Foster nextbit == endidx + 1) { 185910ae3dc7SBrian Foster endidx = nextbit; 186010ae3dc7SBrian Foster goto next; 186110ae3dc7SBrian Foster } 186210ae3dc7SBrian Foster 186310ae3dc7SBrian Foster /* 186410ae3dc7SBrian Foster * nextbit is not contiguous with the current end index. Convert 186510ae3dc7SBrian Foster * the current start/end to an extent and add it to the free 186610ae3dc7SBrian Foster * list. 186710ae3dc7SBrian Foster */ 186810ae3dc7SBrian Foster agbno = sagbno + (startidx * XFS_INODES_PER_HOLEMASK_BIT) / 186910ae3dc7SBrian Foster mp->m_sb.sb_inopblock; 187010ae3dc7SBrian Foster contigblk = ((endidx - startidx + 1) * 187110ae3dc7SBrian Foster XFS_INODES_PER_HOLEMASK_BIT) / 187210ae3dc7SBrian Foster mp->m_sb.sb_inopblock; 187310ae3dc7SBrian Foster 187410ae3dc7SBrian Foster ASSERT(agbno % mp->m_sb.sb_spino_align == 0); 187510ae3dc7SBrian Foster ASSERT(contigblk % mp->m_sb.sb_spino_align == 0); 18760f37d178SBrian Foster xfs_bmap_add_free(tp, XFS_AGB_TO_FSB(mp, agno, agbno), 18777280fedaSDarrick J. Wong contigblk, &XFS_RMAP_OINFO_INODES); 187810ae3dc7SBrian Foster 187910ae3dc7SBrian Foster /* reset range to current bit and carry on... */ 188010ae3dc7SBrian Foster startidx = endidx = nextbit; 188110ae3dc7SBrian Foster 188210ae3dc7SBrian Foster next: 188310ae3dc7SBrian Foster nextbit++; 188410ae3dc7SBrian Foster } 188510ae3dc7SBrian Foster } 188610ae3dc7SBrian Foster 188730f712c9SDave Chinner STATIC int 188830f712c9SDave Chinner xfs_difree_inobt( 188930f712c9SDave Chinner struct xfs_mount *mp, 189030f712c9SDave Chinner struct xfs_trans *tp, 189130f712c9SDave Chinner struct xfs_buf *agbp, 18927b13c515SDave Chinner struct xfs_perag *pag, 189330f712c9SDave Chinner xfs_agino_t agino, 189409b56604SBrian Foster struct xfs_icluster *xic, 189530f712c9SDave Chinner struct xfs_inobt_rec_incore *orec) 189630f712c9SDave Chinner { 1897370c782bSChristoph Hellwig struct xfs_agi *agi = agbp->b_addr; 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 */ 19117b13c515SDave Chinner cur = xfs_inobt_init_cursor(mp, tp, agbp, pag, XFS_BTNUM_INO); 191230f712c9SDave Chinner 1913*9ba0889eSDave Chinner error = xfs_check_agi_freecount(cur); 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 } 1925f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(mp, i != 1)) { 1926f9e03706SDarrick J. Wong error = -EFSCORRUPTED; 1927f9e03706SDarrick J. Wong goto error0; 1928f9e03706SDarrick J. Wong } 192930f712c9SDave Chinner error = xfs_inobt_get_rec(cur, &rec, &i); 193030f712c9SDave Chinner if (error) { 193130f712c9SDave Chinner xfs_warn(mp, "%s: xfs_inobt_get_rec() returned error %d.", 193230f712c9SDave Chinner __func__, error); 193330f712c9SDave Chinner goto error0; 193430f712c9SDave Chinner } 1935f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(mp, i != 1)) { 1936f9e03706SDarrick J. Wong error = -EFSCORRUPTED; 1937f9e03706SDarrick J. Wong goto error0; 1938f9e03706SDarrick J. Wong } 193930f712c9SDave Chinner /* 194030f712c9SDave Chinner * Get the offset in the inode chunk. 194130f712c9SDave Chinner */ 194230f712c9SDave Chinner off = agino - rec.ir_startino; 194330f712c9SDave Chinner ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK); 194430f712c9SDave Chinner ASSERT(!(rec.ir_free & XFS_INOBT_MASK(off))); 194530f712c9SDave Chinner /* 194630f712c9SDave Chinner * Mark the inode free & increment the count. 194730f712c9SDave Chinner */ 194830f712c9SDave Chinner rec.ir_free |= XFS_INOBT_MASK(off); 194930f712c9SDave Chinner rec.ir_freecount++; 195030f712c9SDave Chinner 195130f712c9SDave Chinner /* 1952999633d3SBrian Foster * When an inode chunk is free, it becomes eligible for removal. Don't 1953999633d3SBrian Foster * remove the chunk if the block size is large enough for multiple inode 1954999633d3SBrian Foster * chunks (that might not be free). 195530f712c9SDave Chinner */ 195630f712c9SDave Chinner if (!(mp->m_flags & XFS_MOUNT_IKEEP) && 1957999633d3SBrian Foster rec.ir_free == XFS_INOBT_ALL_FREE && 1958999633d3SBrian Foster mp->m_sb.sb_inopblock <= XFS_INODES_PER_CHUNK) { 195992a00544SGao Xiang struct xfs_perag *pag = agbp->b_pag; 196092a00544SGao Xiang 1961749f24f3SThomas Meyer xic->deleted = true; 19627b13c515SDave Chinner xic->first_ino = XFS_AGINO_TO_INO(mp, pag->pag_agno, 19637b13c515SDave Chinner rec.ir_startino); 196409b56604SBrian Foster xic->alloc = xfs_inobt_irec_to_allocmask(&rec); 196530f712c9SDave Chinner 196630f712c9SDave Chinner /* 196730f712c9SDave Chinner * Remove the inode cluster from the AGI B+Tree, adjust the 196830f712c9SDave Chinner * AGI and Superblock inode counts, and mark the disk space 196930f712c9SDave Chinner * to be freed when the transaction is committed. 197030f712c9SDave Chinner */ 1971999633d3SBrian Foster ilen = rec.ir_freecount; 197230f712c9SDave Chinner be32_add_cpu(&agi->agi_count, -ilen); 197330f712c9SDave Chinner be32_add_cpu(&agi->agi_freecount, -(ilen - 1)); 197430f712c9SDave Chinner xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT); 197530f712c9SDave Chinner pag->pagi_freecount -= ilen - 1; 197689e9b5c0SDarrick J. Wong pag->pagi_count -= ilen; 197730f712c9SDave Chinner xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen); 197830f712c9SDave Chinner xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1)); 197930f712c9SDave Chinner 198030f712c9SDave Chinner if ((error = xfs_btree_delete(cur, &i))) { 198130f712c9SDave Chinner xfs_warn(mp, "%s: xfs_btree_delete returned error %d.", 198230f712c9SDave Chinner __func__, error); 198330f712c9SDave Chinner goto error0; 198430f712c9SDave Chinner } 198530f712c9SDave Chinner 19867b13c515SDave Chinner xfs_difree_inode_chunk(tp, pag->pag_agno, &rec); 198730f712c9SDave Chinner } else { 1988749f24f3SThomas Meyer xic->deleted = false; 198930f712c9SDave Chinner 199030f712c9SDave Chinner error = xfs_inobt_update(cur, &rec); 199130f712c9SDave Chinner if (error) { 199230f712c9SDave Chinner xfs_warn(mp, "%s: xfs_inobt_update returned error %d.", 199330f712c9SDave Chinner __func__, error); 199430f712c9SDave Chinner goto error0; 199530f712c9SDave Chinner } 199630f712c9SDave Chinner 199730f712c9SDave Chinner /* 199830f712c9SDave Chinner * Change the inode free counts and log the ag/sb changes. 199930f712c9SDave Chinner */ 200030f712c9SDave Chinner be32_add_cpu(&agi->agi_freecount, 1); 200130f712c9SDave Chinner xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT); 20027b13c515SDave Chinner pag->pagi_freecount++; 200330f712c9SDave Chinner xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1); 200430f712c9SDave Chinner } 200530f712c9SDave Chinner 2006*9ba0889eSDave Chinner error = xfs_check_agi_freecount(cur); 200730f712c9SDave Chinner if (error) 200830f712c9SDave Chinner goto error0; 200930f712c9SDave Chinner 201030f712c9SDave Chinner *orec = rec; 201130f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); 201230f712c9SDave Chinner return 0; 201330f712c9SDave Chinner 201430f712c9SDave Chinner error0: 201530f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); 201630f712c9SDave Chinner return error; 201730f712c9SDave Chinner } 201830f712c9SDave Chinner 201930f712c9SDave Chinner /* 202030f712c9SDave Chinner * Free an inode in the free inode btree. 202130f712c9SDave Chinner */ 202230f712c9SDave Chinner STATIC int 202330f712c9SDave Chinner xfs_difree_finobt( 202430f712c9SDave Chinner struct xfs_mount *mp, 202530f712c9SDave Chinner struct xfs_trans *tp, 202630f712c9SDave Chinner struct xfs_buf *agbp, 20277b13c515SDave Chinner struct xfs_perag *pag, 202830f712c9SDave Chinner xfs_agino_t agino, 202930f712c9SDave Chinner struct xfs_inobt_rec_incore *ibtrec) /* inobt record */ 203030f712c9SDave Chinner { 203130f712c9SDave Chinner struct xfs_btree_cur *cur; 203230f712c9SDave Chinner struct xfs_inobt_rec_incore rec; 203330f712c9SDave Chinner int offset = agino - ibtrec->ir_startino; 203430f712c9SDave Chinner int error; 203530f712c9SDave Chinner int i; 203630f712c9SDave Chinner 20377b13c515SDave Chinner cur = xfs_inobt_init_cursor(mp, tp, agbp, pag, XFS_BTNUM_FINO); 203830f712c9SDave Chinner 203930f712c9SDave Chinner error = xfs_inobt_lookup(cur, ibtrec->ir_startino, XFS_LOOKUP_EQ, &i); 204030f712c9SDave Chinner if (error) 204130f712c9SDave Chinner goto error; 204230f712c9SDave Chinner if (i == 0) { 204330f712c9SDave Chinner /* 204430f712c9SDave Chinner * If the record does not exist in the finobt, we must have just 204530f712c9SDave Chinner * freed an inode in a previously fully allocated chunk. If not, 204630f712c9SDave Chinner * something is out of sync. 204730f712c9SDave Chinner */ 2048f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(mp, ibtrec->ir_freecount != 1)) { 2049f9e03706SDarrick J. Wong error = -EFSCORRUPTED; 2050f9e03706SDarrick J. Wong goto error; 2051f9e03706SDarrick J. Wong } 205230f712c9SDave Chinner 20535419040fSBrian Foster error = xfs_inobt_insert_rec(cur, ibtrec->ir_holemask, 20545419040fSBrian Foster ibtrec->ir_count, 20555419040fSBrian Foster ibtrec->ir_freecount, 205630f712c9SDave Chinner ibtrec->ir_free, &i); 205730f712c9SDave Chinner if (error) 205830f712c9SDave Chinner goto error; 205930f712c9SDave Chinner ASSERT(i == 1); 206030f712c9SDave Chinner 206130f712c9SDave Chinner goto out; 206230f712c9SDave Chinner } 206330f712c9SDave Chinner 206430f712c9SDave Chinner /* 206530f712c9SDave Chinner * Read and update the existing record. We could just copy the ibtrec 206630f712c9SDave Chinner * across here, but that would defeat the purpose of having redundant 206730f712c9SDave Chinner * metadata. By making the modifications independently, we can catch 206830f712c9SDave Chinner * corruptions that we wouldn't see if we just copied from one record 206930f712c9SDave Chinner * to another. 207030f712c9SDave Chinner */ 207130f712c9SDave Chinner error = xfs_inobt_get_rec(cur, &rec, &i); 207230f712c9SDave Chinner if (error) 207330f712c9SDave Chinner goto error; 2074f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(mp, i != 1)) { 2075f9e03706SDarrick J. Wong error = -EFSCORRUPTED; 2076f9e03706SDarrick J. Wong goto error; 2077f9e03706SDarrick J. Wong } 207830f712c9SDave Chinner 207930f712c9SDave Chinner rec.ir_free |= XFS_INOBT_MASK(offset); 208030f712c9SDave Chinner rec.ir_freecount++; 208130f712c9SDave Chinner 2082f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(mp, 2083f9e03706SDarrick J. Wong rec.ir_free != ibtrec->ir_free || 2084f9e03706SDarrick J. Wong rec.ir_freecount != ibtrec->ir_freecount)) { 2085f9e03706SDarrick J. Wong error = -EFSCORRUPTED; 2086f9e03706SDarrick J. Wong goto error; 2087f9e03706SDarrick J. Wong } 208830f712c9SDave Chinner 208930f712c9SDave Chinner /* 209030f712c9SDave Chinner * The content of inobt records should always match between the inobt 209130f712c9SDave Chinner * and finobt. The lifecycle of records in the finobt is different from 209230f712c9SDave Chinner * the inobt in that the finobt only tracks records with at least one 209330f712c9SDave Chinner * free inode. Hence, if all of the inodes are free and we aren't 209430f712c9SDave Chinner * keeping inode chunks permanently on disk, remove the record. 209530f712c9SDave Chinner * Otherwise, update the record with the new information. 2096999633d3SBrian Foster * 2097999633d3SBrian Foster * Note that we currently can't free chunks when the block size is large 2098999633d3SBrian Foster * enough for multiple chunks. Leave the finobt record to remain in sync 2099999633d3SBrian Foster * with the inobt. 210030f712c9SDave Chinner */ 2101999633d3SBrian Foster if (rec.ir_free == XFS_INOBT_ALL_FREE && 2102999633d3SBrian Foster mp->m_sb.sb_inopblock <= XFS_INODES_PER_CHUNK && 210330f712c9SDave Chinner !(mp->m_flags & XFS_MOUNT_IKEEP)) { 210430f712c9SDave Chinner error = xfs_btree_delete(cur, &i); 210530f712c9SDave Chinner if (error) 210630f712c9SDave Chinner goto error; 210730f712c9SDave Chinner ASSERT(i == 1); 210830f712c9SDave Chinner } else { 210930f712c9SDave Chinner error = xfs_inobt_update(cur, &rec); 211030f712c9SDave Chinner if (error) 211130f712c9SDave Chinner goto error; 211230f712c9SDave Chinner } 211330f712c9SDave Chinner 211430f712c9SDave Chinner out: 2115*9ba0889eSDave Chinner error = xfs_check_agi_freecount(cur); 211630f712c9SDave Chinner if (error) 211730f712c9SDave Chinner goto error; 211830f712c9SDave Chinner 211930f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); 212030f712c9SDave Chinner return 0; 212130f712c9SDave Chinner 212230f712c9SDave Chinner error: 212330f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); 212430f712c9SDave Chinner return error; 212530f712c9SDave Chinner } 212630f712c9SDave Chinner 212730f712c9SDave Chinner /* 212830f712c9SDave Chinner * Free disk inode. Carefully avoids touching the incore inode, all 212930f712c9SDave Chinner * manipulations incore are the caller's responsibility. 213030f712c9SDave Chinner * The on-disk inode is not changed by this operation, only the 213130f712c9SDave Chinner * btree (free inode mask) is changed. 213230f712c9SDave Chinner */ 213330f712c9SDave Chinner int 213430f712c9SDave Chinner xfs_difree( 2135f40aadb2SDave Chinner struct xfs_trans *tp, 2136f40aadb2SDave Chinner struct xfs_perag *pag, 2137f40aadb2SDave Chinner xfs_ino_t inode, 2138f40aadb2SDave Chinner struct xfs_icluster *xic) 213930f712c9SDave Chinner { 214030f712c9SDave Chinner /* REFERENCED */ 214130f712c9SDave Chinner xfs_agblock_t agbno; /* block number containing inode */ 214230f712c9SDave Chinner struct xfs_buf *agbp; /* buffer for allocation group header */ 214330f712c9SDave Chinner xfs_agino_t agino; /* allocation group inode number */ 214430f712c9SDave Chinner int error; /* error return value */ 21457b13c515SDave Chinner struct xfs_mount *mp = tp->t_mountp; 214630f712c9SDave Chinner struct xfs_inobt_rec_incore rec;/* btree record */ 214730f712c9SDave Chinner 214830f712c9SDave Chinner /* 214930f712c9SDave Chinner * Break up inode number into its components. 215030f712c9SDave Chinner */ 2151f40aadb2SDave Chinner if (pag->pag_agno != XFS_INO_TO_AGNO(mp, inode)) { 2152f40aadb2SDave Chinner xfs_warn(mp, "%s: agno != pag->pag_agno (%d != %d).", 2153f40aadb2SDave Chinner __func__, XFS_INO_TO_AGNO(mp, inode), pag->pag_agno); 215430f712c9SDave Chinner ASSERT(0); 21552451337dSDave Chinner return -EINVAL; 215630f712c9SDave Chinner } 215730f712c9SDave Chinner agino = XFS_INO_TO_AGINO(mp, inode); 2158f40aadb2SDave Chinner if (inode != XFS_AGINO_TO_INO(mp, pag->pag_agno, agino)) { 215930f712c9SDave Chinner xfs_warn(mp, "%s: inode != XFS_AGINO_TO_INO() (%llu != %llu).", 216030f712c9SDave Chinner __func__, (unsigned long long)inode, 2161f40aadb2SDave Chinner (unsigned long long)XFS_AGINO_TO_INO(mp, pag->pag_agno, agino)); 216230f712c9SDave Chinner ASSERT(0); 21632451337dSDave Chinner return -EINVAL; 216430f712c9SDave Chinner } 216530f712c9SDave Chinner agbno = XFS_AGINO_TO_AGBNO(mp, agino); 216630f712c9SDave Chinner if (agbno >= mp->m_sb.sb_agblocks) { 216730f712c9SDave Chinner xfs_warn(mp, "%s: agbno >= mp->m_sb.sb_agblocks (%d >= %d).", 216830f712c9SDave Chinner __func__, agbno, mp->m_sb.sb_agblocks); 216930f712c9SDave Chinner ASSERT(0); 21702451337dSDave Chinner return -EINVAL; 217130f712c9SDave Chinner } 217230f712c9SDave Chinner /* 217330f712c9SDave Chinner * Get the allocation group header. 217430f712c9SDave Chinner */ 2175f40aadb2SDave Chinner error = xfs_ialloc_read_agi(mp, tp, pag->pag_agno, &agbp); 217630f712c9SDave Chinner if (error) { 217730f712c9SDave Chinner xfs_warn(mp, "%s: xfs_ialloc_read_agi() returned error %d.", 217830f712c9SDave Chinner __func__, error); 217930f712c9SDave Chinner return error; 218030f712c9SDave Chinner } 218130f712c9SDave Chinner 218230f712c9SDave Chinner /* 218330f712c9SDave Chinner * Fix up the inode allocation btree. 218430f712c9SDave Chinner */ 21857b13c515SDave Chinner error = xfs_difree_inobt(mp, tp, agbp, pag, agino, xic, &rec); 218630f712c9SDave Chinner if (error) 218730f712c9SDave Chinner goto error0; 218830f712c9SDave Chinner 218930f712c9SDave Chinner /* 219030f712c9SDave Chinner * Fix up the free inode btree. 219130f712c9SDave Chinner */ 219230f712c9SDave Chinner if (xfs_sb_version_hasfinobt(&mp->m_sb)) { 21937b13c515SDave Chinner error = xfs_difree_finobt(mp, tp, agbp, pag, agino, &rec); 219430f712c9SDave Chinner if (error) 219530f712c9SDave Chinner goto error0; 219630f712c9SDave Chinner } 219730f712c9SDave Chinner 219830f712c9SDave Chinner return 0; 219930f712c9SDave Chinner 220030f712c9SDave Chinner error0: 220130f712c9SDave Chinner return error; 220230f712c9SDave Chinner } 220330f712c9SDave Chinner 220430f712c9SDave Chinner STATIC int 220530f712c9SDave Chinner xfs_imap_lookup( 220630f712c9SDave Chinner struct xfs_mount *mp, 220730f712c9SDave Chinner struct xfs_trans *tp, 22087b13c515SDave Chinner struct xfs_perag *pag, 220930f712c9SDave Chinner xfs_agino_t agino, 221030f712c9SDave Chinner xfs_agblock_t agbno, 221130f712c9SDave Chinner xfs_agblock_t *chunk_agbno, 221230f712c9SDave Chinner xfs_agblock_t *offset_agbno, 221330f712c9SDave Chinner int flags) 221430f712c9SDave Chinner { 221530f712c9SDave Chinner struct xfs_inobt_rec_incore rec; 221630f712c9SDave Chinner struct xfs_btree_cur *cur; 221730f712c9SDave Chinner struct xfs_buf *agbp; 221830f712c9SDave Chinner int error; 221930f712c9SDave Chinner int i; 222030f712c9SDave Chinner 22217b13c515SDave Chinner error = xfs_ialloc_read_agi(mp, tp, pag->pag_agno, &agbp); 222230f712c9SDave Chinner if (error) { 222330f712c9SDave Chinner xfs_alert(mp, 222430f712c9SDave Chinner "%s: xfs_ialloc_read_agi() returned error %d, agno %d", 22257b13c515SDave Chinner __func__, error, pag->pag_agno); 222630f712c9SDave Chinner return error; 222730f712c9SDave Chinner } 222830f712c9SDave Chinner 222930f712c9SDave Chinner /* 223030f712c9SDave Chinner * Lookup the inode record for the given agino. If the record cannot be 223130f712c9SDave Chinner * found, then it's an invalid inode number and we should abort. Once 223230f712c9SDave Chinner * we have a record, we need to ensure it contains the inode number 223330f712c9SDave Chinner * we are looking up. 223430f712c9SDave Chinner */ 22357b13c515SDave Chinner cur = xfs_inobt_init_cursor(mp, tp, agbp, pag, XFS_BTNUM_INO); 223630f712c9SDave Chinner error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i); 223730f712c9SDave Chinner if (!error) { 223830f712c9SDave Chinner if (i) 223930f712c9SDave Chinner error = xfs_inobt_get_rec(cur, &rec, &i); 224030f712c9SDave Chinner if (!error && i == 0) 22412451337dSDave Chinner error = -EINVAL; 224230f712c9SDave Chinner } 224330f712c9SDave Chinner 224430f712c9SDave Chinner xfs_trans_brelse(tp, agbp); 22450b04b6b8SDarrick J. Wong xfs_btree_del_cursor(cur, error); 224630f712c9SDave Chinner if (error) 224730f712c9SDave Chinner return error; 224830f712c9SDave Chinner 224930f712c9SDave Chinner /* check that the returned record contains the required inode */ 225030f712c9SDave Chinner if (rec.ir_startino > agino || 2251ef325959SDarrick J. Wong rec.ir_startino + M_IGEO(mp)->ialloc_inos <= agino) 22522451337dSDave Chinner return -EINVAL; 225330f712c9SDave Chinner 225430f712c9SDave Chinner /* for untrusted inodes check it is allocated first */ 225530f712c9SDave Chinner if ((flags & XFS_IGET_UNTRUSTED) && 225630f712c9SDave Chinner (rec.ir_free & XFS_INOBT_MASK(agino - rec.ir_startino))) 22572451337dSDave Chinner return -EINVAL; 225830f712c9SDave Chinner 225930f712c9SDave Chinner *chunk_agbno = XFS_AGINO_TO_AGBNO(mp, rec.ir_startino); 226030f712c9SDave Chinner *offset_agbno = agbno - *chunk_agbno; 226130f712c9SDave Chinner return 0; 226230f712c9SDave Chinner } 226330f712c9SDave Chinner 226430f712c9SDave Chinner /* 226530f712c9SDave Chinner * Return the location of the inode in imap, for mapping it into a buffer. 226630f712c9SDave Chinner */ 226730f712c9SDave Chinner int 226830f712c9SDave Chinner xfs_imap( 22697b13c515SDave Chinner struct xfs_mount *mp, /* file system mount structure */ 22707b13c515SDave Chinner struct xfs_trans *tp, /* transaction pointer */ 227130f712c9SDave Chinner xfs_ino_t ino, /* inode to locate */ 227230f712c9SDave Chinner struct xfs_imap *imap, /* location map structure */ 227330f712c9SDave Chinner uint flags) /* flags for inode btree lookup */ 227430f712c9SDave Chinner { 227530f712c9SDave Chinner xfs_agblock_t agbno; /* block number of inode in the alloc group */ 227630f712c9SDave Chinner xfs_agino_t agino; /* inode number within alloc group */ 227730f712c9SDave Chinner xfs_agblock_t chunk_agbno; /* first block in inode chunk */ 227830f712c9SDave Chinner xfs_agblock_t cluster_agbno; /* first block in inode cluster */ 227930f712c9SDave Chinner int error; /* error code */ 228030f712c9SDave Chinner int offset; /* index of inode in its buffer */ 228130f712c9SDave Chinner xfs_agblock_t offset_agbno; /* blks from chunk start to inode */ 22827b13c515SDave Chinner struct xfs_perag *pag; 228330f712c9SDave Chinner 228430f712c9SDave Chinner ASSERT(ino != NULLFSINO); 228530f712c9SDave Chinner 228630f712c9SDave Chinner /* 228730f712c9SDave Chinner * Split up the inode number into its parts. 228830f712c9SDave Chinner */ 22897b13c515SDave Chinner pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ino)); 229030f712c9SDave Chinner agino = XFS_INO_TO_AGINO(mp, ino); 229130f712c9SDave Chinner agbno = XFS_AGINO_TO_AGBNO(mp, agino); 22927b13c515SDave Chinner if (!pag || agbno >= mp->m_sb.sb_agblocks || 22937b13c515SDave Chinner ino != XFS_AGINO_TO_INO(mp, pag->pag_agno, agino)) { 22947b13c515SDave Chinner error = -EINVAL; 229530f712c9SDave Chinner #ifdef DEBUG 229630f712c9SDave Chinner /* 229730f712c9SDave Chinner * Don't output diagnostic information for untrusted inodes 229830f712c9SDave Chinner * as they can be invalid without implying corruption. 229930f712c9SDave Chinner */ 230030f712c9SDave Chinner if (flags & XFS_IGET_UNTRUSTED) 23017b13c515SDave Chinner goto out_drop; 23027b13c515SDave Chinner if (!pag) { 230330f712c9SDave Chinner xfs_alert(mp, 230430f712c9SDave Chinner "%s: agno (%d) >= mp->m_sb.sb_agcount (%d)", 23057b13c515SDave Chinner __func__, XFS_INO_TO_AGNO(mp, ino), 23067b13c515SDave Chinner mp->m_sb.sb_agcount); 230730f712c9SDave Chinner } 230830f712c9SDave Chinner if (agbno >= mp->m_sb.sb_agblocks) { 230930f712c9SDave Chinner xfs_alert(mp, 231030f712c9SDave Chinner "%s: agbno (0x%llx) >= mp->m_sb.sb_agblocks (0x%lx)", 231130f712c9SDave Chinner __func__, (unsigned long long)agbno, 231230f712c9SDave Chinner (unsigned long)mp->m_sb.sb_agblocks); 231330f712c9SDave Chinner } 23147b13c515SDave Chinner if (pag && ino != XFS_AGINO_TO_INO(mp, pag->pag_agno, agino)) { 231530f712c9SDave Chinner xfs_alert(mp, 231630f712c9SDave Chinner "%s: ino (0x%llx) != XFS_AGINO_TO_INO() (0x%llx)", 231730f712c9SDave Chinner __func__, ino, 23187b13c515SDave Chinner XFS_AGINO_TO_INO(mp, pag->pag_agno, agino)); 231930f712c9SDave Chinner } 232030f712c9SDave Chinner xfs_stack_trace(); 232130f712c9SDave Chinner #endif /* DEBUG */ 23227b13c515SDave Chinner goto out_drop; 232330f712c9SDave Chinner } 232430f712c9SDave Chinner 232530f712c9SDave Chinner /* 232630f712c9SDave Chinner * For bulkstat and handle lookups, we have an untrusted inode number 232730f712c9SDave Chinner * that we have to verify is valid. We cannot do this just by reading 232830f712c9SDave Chinner * the inode buffer as it may have been unlinked and removed leaving 232930f712c9SDave Chinner * inodes in stale state on disk. Hence we have to do a btree lookup 233030f712c9SDave Chinner * in all cases where an untrusted inode number is passed. 233130f712c9SDave Chinner */ 233230f712c9SDave Chinner if (flags & XFS_IGET_UNTRUSTED) { 23337b13c515SDave Chinner error = xfs_imap_lookup(mp, tp, pag, agino, agbno, 233430f712c9SDave Chinner &chunk_agbno, &offset_agbno, flags); 233530f712c9SDave Chinner if (error) 23367b13c515SDave Chinner goto out_drop; 233730f712c9SDave Chinner goto out_map; 233830f712c9SDave Chinner } 233930f712c9SDave Chinner 234030f712c9SDave Chinner /* 234130f712c9SDave Chinner * If the inode cluster size is the same as the blocksize or 234230f712c9SDave Chinner * smaller we get to the buffer by simple arithmetics. 234330f712c9SDave Chinner */ 2344ef325959SDarrick J. Wong if (M_IGEO(mp)->blocks_per_cluster == 1) { 234530f712c9SDave Chinner offset = XFS_INO_TO_OFFSET(mp, ino); 234630f712c9SDave Chinner ASSERT(offset < mp->m_sb.sb_inopblock); 234730f712c9SDave Chinner 23487b13c515SDave Chinner imap->im_blkno = XFS_AGB_TO_DADDR(mp, pag->pag_agno, agbno); 234930f712c9SDave Chinner imap->im_len = XFS_FSB_TO_BB(mp, 1); 2350755c7bf5SDarrick J. Wong imap->im_boffset = (unsigned short)(offset << 2351755c7bf5SDarrick J. Wong mp->m_sb.sb_inodelog); 23527b13c515SDave Chinner error = 0; 23537b13c515SDave Chinner goto out_drop; 235430f712c9SDave Chinner } 235530f712c9SDave Chinner 235630f712c9SDave Chinner /* 235730f712c9SDave Chinner * If the inode chunks are aligned then use simple maths to 235830f712c9SDave Chinner * find the location. Otherwise we have to do a btree 235930f712c9SDave Chinner * lookup to find the location. 236030f712c9SDave Chinner */ 2361ef325959SDarrick J. Wong if (M_IGEO(mp)->inoalign_mask) { 2362ef325959SDarrick J. Wong offset_agbno = agbno & M_IGEO(mp)->inoalign_mask; 236330f712c9SDave Chinner chunk_agbno = agbno - offset_agbno; 236430f712c9SDave Chinner } else { 23657b13c515SDave Chinner error = xfs_imap_lookup(mp, tp, pag, agino, agbno, 236630f712c9SDave Chinner &chunk_agbno, &offset_agbno, flags); 236730f712c9SDave Chinner if (error) 23687b13c515SDave Chinner goto out_drop; 236930f712c9SDave Chinner } 237030f712c9SDave Chinner 237130f712c9SDave Chinner out_map: 237230f712c9SDave Chinner ASSERT(agbno >= chunk_agbno); 237330f712c9SDave Chinner cluster_agbno = chunk_agbno + 2374ef325959SDarrick J. Wong ((offset_agbno / M_IGEO(mp)->blocks_per_cluster) * 2375ef325959SDarrick J. Wong M_IGEO(mp)->blocks_per_cluster); 237630f712c9SDave Chinner offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) + 237730f712c9SDave Chinner XFS_INO_TO_OFFSET(mp, ino); 237830f712c9SDave Chinner 23797b13c515SDave Chinner imap->im_blkno = XFS_AGB_TO_DADDR(mp, pag->pag_agno, cluster_agbno); 2380ef325959SDarrick J. Wong imap->im_len = XFS_FSB_TO_BB(mp, M_IGEO(mp)->blocks_per_cluster); 2381755c7bf5SDarrick J. Wong imap->im_boffset = (unsigned short)(offset << mp->m_sb.sb_inodelog); 238230f712c9SDave Chinner 238330f712c9SDave Chinner /* 238430f712c9SDave Chinner * If the inode number maps to a block outside the bounds 238530f712c9SDave Chinner * of the file system then return NULL rather than calling 238630f712c9SDave Chinner * read_buf and panicing when we get an error from the 238730f712c9SDave Chinner * driver. 238830f712c9SDave Chinner */ 238930f712c9SDave Chinner if ((imap->im_blkno + imap->im_len) > 239030f712c9SDave Chinner XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) { 239130f712c9SDave Chinner xfs_alert(mp, 239230f712c9SDave Chinner "%s: (im_blkno (0x%llx) + im_len (0x%llx)) > sb_dblocks (0x%llx)", 239330f712c9SDave Chinner __func__, (unsigned long long) imap->im_blkno, 239430f712c9SDave Chinner (unsigned long long) imap->im_len, 239530f712c9SDave Chinner XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)); 23967b13c515SDave Chinner error = -EINVAL; 23977b13c515SDave Chinner goto out_drop; 239830f712c9SDave Chinner } 23997b13c515SDave Chinner error = 0; 24007b13c515SDave Chinner out_drop: 24017b13c515SDave Chinner xfs_perag_put(pag); 24027b13c515SDave Chinner return error; 240330f712c9SDave Chinner } 240430f712c9SDave Chinner 240530f712c9SDave Chinner /* 240630f712c9SDave Chinner * Log specified fields for the ag hdr (inode section). The growth of the agi 240730f712c9SDave Chinner * structure over time requires that we interpret the buffer as two logical 240830f712c9SDave Chinner * regions delineated by the end of the unlinked list. This is due to the size 240930f712c9SDave Chinner * of the hash table and its location in the middle of the agi. 241030f712c9SDave Chinner * 241130f712c9SDave Chinner * For example, a request to log a field before agi_unlinked and a field after 241230f712c9SDave Chinner * agi_unlinked could cause us to log the entire hash table and use an excessive 241330f712c9SDave Chinner * amount of log space. To avoid this behavior, log the region up through 241430f712c9SDave Chinner * agi_unlinked in one call and the region after agi_unlinked through the end of 241530f712c9SDave Chinner * the structure in another. 241630f712c9SDave Chinner */ 241730f712c9SDave Chinner void 241830f712c9SDave Chinner xfs_ialloc_log_agi( 241930f712c9SDave Chinner xfs_trans_t *tp, /* transaction pointer */ 2420e8222613SDave Chinner struct xfs_buf *bp, /* allocation group header buffer */ 242130f712c9SDave Chinner int fields) /* bitmask of fields to log */ 242230f712c9SDave Chinner { 242330f712c9SDave Chinner int first; /* first byte number */ 242430f712c9SDave Chinner int last; /* last byte number */ 242530f712c9SDave Chinner static const short offsets[] = { /* field starting offsets */ 242630f712c9SDave Chinner /* keep in sync with bit definitions */ 242730f712c9SDave Chinner offsetof(xfs_agi_t, agi_magicnum), 242830f712c9SDave Chinner offsetof(xfs_agi_t, agi_versionnum), 242930f712c9SDave Chinner offsetof(xfs_agi_t, agi_seqno), 243030f712c9SDave Chinner offsetof(xfs_agi_t, agi_length), 243130f712c9SDave Chinner offsetof(xfs_agi_t, agi_count), 243230f712c9SDave Chinner offsetof(xfs_agi_t, agi_root), 243330f712c9SDave Chinner offsetof(xfs_agi_t, agi_level), 243430f712c9SDave Chinner offsetof(xfs_agi_t, agi_freecount), 243530f712c9SDave Chinner offsetof(xfs_agi_t, agi_newino), 243630f712c9SDave Chinner offsetof(xfs_agi_t, agi_dirino), 243730f712c9SDave Chinner offsetof(xfs_agi_t, agi_unlinked), 243830f712c9SDave Chinner offsetof(xfs_agi_t, agi_free_root), 243930f712c9SDave Chinner offsetof(xfs_agi_t, agi_free_level), 24402a39946cSDarrick J. Wong offsetof(xfs_agi_t, agi_iblocks), 244130f712c9SDave Chinner sizeof(xfs_agi_t) 244230f712c9SDave Chinner }; 244330f712c9SDave Chinner #ifdef DEBUG 2444370c782bSChristoph Hellwig struct xfs_agi *agi = bp->b_addr; 244530f712c9SDave Chinner 244630f712c9SDave Chinner ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC)); 244730f712c9SDave Chinner #endif 244830f712c9SDave Chinner 244930f712c9SDave Chinner /* 245030f712c9SDave Chinner * Compute byte offsets for the first and last fields in the first 245130f712c9SDave Chinner * region and log the agi buffer. This only logs up through 245230f712c9SDave Chinner * agi_unlinked. 245330f712c9SDave Chinner */ 245430f712c9SDave Chinner if (fields & XFS_AGI_ALL_BITS_R1) { 245530f712c9SDave Chinner xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R1, 245630f712c9SDave Chinner &first, &last); 245730f712c9SDave Chinner xfs_trans_log_buf(tp, bp, first, last); 245830f712c9SDave Chinner } 245930f712c9SDave Chinner 246030f712c9SDave Chinner /* 246130f712c9SDave Chinner * Mask off the bits in the first region and calculate the first and 246230f712c9SDave Chinner * last field offsets for any bits in the second region. 246330f712c9SDave Chinner */ 246430f712c9SDave Chinner fields &= ~XFS_AGI_ALL_BITS_R1; 246530f712c9SDave Chinner if (fields) { 246630f712c9SDave Chinner xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R2, 246730f712c9SDave Chinner &first, &last); 246830f712c9SDave Chinner xfs_trans_log_buf(tp, bp, first, last); 246930f712c9SDave Chinner } 247030f712c9SDave Chinner } 247130f712c9SDave Chinner 2472a6a781a5SDarrick J. Wong static xfs_failaddr_t 247330f712c9SDave Chinner xfs_agi_verify( 247430f712c9SDave Chinner struct xfs_buf *bp) 247530f712c9SDave Chinner { 2476dbd329f1SChristoph Hellwig struct xfs_mount *mp = bp->b_mount; 2477370c782bSChristoph Hellwig struct xfs_agi *agi = bp->b_addr; 24789f96cc95SDave Chinner int i; 247930f712c9SDave Chinner 2480a45086e2SBrian Foster if (xfs_sb_version_hascrc(&mp->m_sb)) { 2481a45086e2SBrian Foster if (!uuid_equal(&agi->agi_uuid, &mp->m_sb.sb_meta_uuid)) 2482a6a781a5SDarrick J. Wong return __this_address; 2483370c782bSChristoph Hellwig if (!xfs_log_check_lsn(mp, be64_to_cpu(agi->agi_lsn))) 2484a6a781a5SDarrick J. Wong return __this_address; 2485a45086e2SBrian Foster } 2486a45086e2SBrian Foster 248730f712c9SDave Chinner /* 248830f712c9SDave Chinner * Validate the magic number of the agi block. 248930f712c9SDave Chinner */ 249039708c20SBrian Foster if (!xfs_verify_magic(bp, agi->agi_magicnum)) 2491a6a781a5SDarrick J. Wong return __this_address; 249230f712c9SDave Chinner if (!XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum))) 2493a6a781a5SDarrick J. Wong return __this_address; 249430f712c9SDave Chinner 2495d2a047f3SDarrick J. Wong if (be32_to_cpu(agi->agi_level) < 1 || 2496973975b7SDarrick J. Wong be32_to_cpu(agi->agi_level) > M_IGEO(mp)->inobt_maxlevels) 2497a6a781a5SDarrick J. Wong return __this_address; 2498d2a047f3SDarrick J. Wong 2499d2a047f3SDarrick J. Wong if (xfs_sb_version_hasfinobt(&mp->m_sb) && 2500d2a047f3SDarrick J. Wong (be32_to_cpu(agi->agi_free_level) < 1 || 2501973975b7SDarrick J. Wong be32_to_cpu(agi->agi_free_level) > M_IGEO(mp)->inobt_maxlevels)) 2502a6a781a5SDarrick J. Wong return __this_address; 2503d2a047f3SDarrick J. Wong 250430f712c9SDave Chinner /* 250530f712c9SDave Chinner * during growfs operations, the perag is not fully initialised, 250630f712c9SDave Chinner * so we can't use it for any useful checking. growfs ensures we can't 250730f712c9SDave Chinner * use it by using uncached buffers that don't have the perag attached 250830f712c9SDave Chinner * so we can detect and avoid this problem. 250930f712c9SDave Chinner */ 251030f712c9SDave Chinner if (bp->b_pag && be32_to_cpu(agi->agi_seqno) != bp->b_pag->pag_agno) 2511a6a781a5SDarrick J. Wong return __this_address; 251230f712c9SDave Chinner 25139f96cc95SDave Chinner for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++) { 25145089eaffSCarlos Maiolino if (agi->agi_unlinked[i] == cpu_to_be32(NULLAGINO)) 25159f96cc95SDave Chinner continue; 25169f96cc95SDave Chinner if (!xfs_verify_ino(mp, be32_to_cpu(agi->agi_unlinked[i]))) 25179f96cc95SDave Chinner return __this_address; 25189f96cc95SDave Chinner } 25199f96cc95SDave Chinner 2520a6a781a5SDarrick J. Wong return NULL; 252130f712c9SDave Chinner } 252230f712c9SDave Chinner 252330f712c9SDave Chinner static void 252430f712c9SDave Chinner xfs_agi_read_verify( 252530f712c9SDave Chinner struct xfs_buf *bp) 252630f712c9SDave Chinner { 2527dbd329f1SChristoph Hellwig struct xfs_mount *mp = bp->b_mount; 2528bc1a09b8SDarrick J. Wong xfs_failaddr_t fa; 252930f712c9SDave Chinner 253030f712c9SDave Chinner if (xfs_sb_version_hascrc(&mp->m_sb) && 253130f712c9SDave Chinner !xfs_buf_verify_cksum(bp, XFS_AGI_CRC_OFF)) 2532bc1a09b8SDarrick J. Wong xfs_verifier_error(bp, -EFSBADCRC, __this_address); 2533bc1a09b8SDarrick J. Wong else { 2534bc1a09b8SDarrick J. Wong fa = xfs_agi_verify(bp); 2535bc1a09b8SDarrick J. Wong if (XFS_TEST_ERROR(fa, mp, XFS_ERRTAG_IALLOC_READ_AGI)) 2536bc1a09b8SDarrick J. Wong xfs_verifier_error(bp, -EFSCORRUPTED, fa); 2537bc1a09b8SDarrick J. Wong } 253830f712c9SDave Chinner } 253930f712c9SDave Chinner 254030f712c9SDave Chinner static void 254130f712c9SDave Chinner xfs_agi_write_verify( 254230f712c9SDave Chinner struct xfs_buf *bp) 254330f712c9SDave Chinner { 2544dbd329f1SChristoph Hellwig struct xfs_mount *mp = bp->b_mount; 2545fb1755a6SCarlos Maiolino struct xfs_buf_log_item *bip = bp->b_log_item; 2546370c782bSChristoph Hellwig struct xfs_agi *agi = bp->b_addr; 2547bc1a09b8SDarrick J. Wong xfs_failaddr_t fa; 254830f712c9SDave Chinner 2549bc1a09b8SDarrick J. Wong fa = xfs_agi_verify(bp); 2550bc1a09b8SDarrick J. Wong if (fa) { 2551bc1a09b8SDarrick J. Wong xfs_verifier_error(bp, -EFSCORRUPTED, fa); 255230f712c9SDave Chinner return; 255330f712c9SDave Chinner } 255430f712c9SDave Chinner 255530f712c9SDave Chinner if (!xfs_sb_version_hascrc(&mp->m_sb)) 255630f712c9SDave Chinner return; 255730f712c9SDave Chinner 255830f712c9SDave Chinner if (bip) 2559370c782bSChristoph Hellwig agi->agi_lsn = cpu_to_be64(bip->bli_item.li_lsn); 256030f712c9SDave Chinner xfs_buf_update_cksum(bp, XFS_AGI_CRC_OFF); 256130f712c9SDave Chinner } 256230f712c9SDave Chinner 256330f712c9SDave Chinner const struct xfs_buf_ops xfs_agi_buf_ops = { 2564233135b7SEric Sandeen .name = "xfs_agi", 256539708c20SBrian Foster .magic = { cpu_to_be32(XFS_AGI_MAGIC), cpu_to_be32(XFS_AGI_MAGIC) }, 256630f712c9SDave Chinner .verify_read = xfs_agi_read_verify, 256730f712c9SDave Chinner .verify_write = xfs_agi_write_verify, 2568b5572597SDarrick J. Wong .verify_struct = xfs_agi_verify, 256930f712c9SDave Chinner }; 257030f712c9SDave Chinner 257130f712c9SDave Chinner /* 257230f712c9SDave Chinner * Read in the allocation group header (inode allocation section) 257330f712c9SDave Chinner */ 257430f712c9SDave Chinner int 257530f712c9SDave Chinner xfs_read_agi( 257630f712c9SDave Chinner struct xfs_mount *mp, /* file system mount structure */ 257730f712c9SDave Chinner struct xfs_trans *tp, /* transaction pointer */ 257830f712c9SDave Chinner xfs_agnumber_t agno, /* allocation group number */ 257930f712c9SDave Chinner struct xfs_buf **bpp) /* allocation group hdr buf */ 258030f712c9SDave Chinner { 258130f712c9SDave Chinner int error; 258230f712c9SDave Chinner 258330f712c9SDave Chinner trace_xfs_read_agi(mp, agno); 258430f712c9SDave Chinner 258530f712c9SDave Chinner ASSERT(agno != NULLAGNUMBER); 258630f712c9SDave Chinner error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, 258730f712c9SDave Chinner XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)), 258830f712c9SDave Chinner XFS_FSS_TO_BB(mp, 1), 0, bpp, &xfs_agi_buf_ops); 258930f712c9SDave Chinner if (error) 259030f712c9SDave Chinner return error; 2591200237d6SEric Sandeen if (tp) 2592200237d6SEric Sandeen xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_AGI_BUF); 259330f712c9SDave Chinner 259430f712c9SDave Chinner xfs_buf_set_ref(*bpp, XFS_AGI_REF); 259530f712c9SDave Chinner return 0; 259630f712c9SDave Chinner } 259730f712c9SDave Chinner 259830f712c9SDave Chinner int 259930f712c9SDave Chinner xfs_ialloc_read_agi( 260030f712c9SDave Chinner struct xfs_mount *mp, /* file system mount structure */ 260130f712c9SDave Chinner struct xfs_trans *tp, /* transaction pointer */ 260230f712c9SDave Chinner xfs_agnumber_t agno, /* allocation group number */ 260330f712c9SDave Chinner struct xfs_buf **bpp) /* allocation group hdr buf */ 260430f712c9SDave Chinner { 260530f712c9SDave Chinner struct xfs_agi *agi; /* allocation group header */ 260630f712c9SDave Chinner struct xfs_perag *pag; /* per allocation group data */ 260730f712c9SDave Chinner int error; 260830f712c9SDave Chinner 260930f712c9SDave Chinner trace_xfs_ialloc_read_agi(mp, agno); 261030f712c9SDave Chinner 261130f712c9SDave Chinner error = xfs_read_agi(mp, tp, agno, bpp); 261230f712c9SDave Chinner if (error) 261330f712c9SDave Chinner return error; 261430f712c9SDave Chinner 2615370c782bSChristoph Hellwig agi = (*bpp)->b_addr; 261692a00544SGao Xiang pag = (*bpp)->b_pag; 261730f712c9SDave Chinner if (!pag->pagi_init) { 261830f712c9SDave Chinner pag->pagi_freecount = be32_to_cpu(agi->agi_freecount); 261930f712c9SDave Chinner pag->pagi_count = be32_to_cpu(agi->agi_count); 262030f712c9SDave Chinner pag->pagi_init = 1; 262130f712c9SDave Chinner } 262230f712c9SDave Chinner 262330f712c9SDave Chinner /* 262430f712c9SDave Chinner * It's possible for these to be out of sync if 262530f712c9SDave Chinner * we are in the middle of a forced shutdown. 262630f712c9SDave Chinner */ 262730f712c9SDave Chinner ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) || 262830f712c9SDave Chinner XFS_FORCED_SHUTDOWN(mp)); 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 { 2641e8222613SDave Chinner struct xfs_buf *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 } 265191fb9afcSDarrick J. Wong 26522e001266SDarrick J. Wong /* Is there an inode record covering a given range of inode numbers? */ 26532e001266SDarrick J. Wong int 26542e001266SDarrick J. Wong xfs_ialloc_has_inode_record( 26552e001266SDarrick J. Wong struct xfs_btree_cur *cur, 26562e001266SDarrick J. Wong xfs_agino_t low, 26572e001266SDarrick J. Wong xfs_agino_t high, 26582e001266SDarrick J. Wong bool *exists) 26592e001266SDarrick J. Wong { 26602e001266SDarrick J. Wong struct xfs_inobt_rec_incore irec; 26612e001266SDarrick J. Wong xfs_agino_t agino; 26622e001266SDarrick J. Wong uint16_t holemask; 26632e001266SDarrick J. Wong int has_record; 26642e001266SDarrick J. Wong int i; 26652e001266SDarrick J. Wong int error; 26662e001266SDarrick J. Wong 26672e001266SDarrick J. Wong *exists = false; 26682e001266SDarrick J. Wong error = xfs_inobt_lookup(cur, low, XFS_LOOKUP_LE, &has_record); 26692e001266SDarrick J. Wong while (error == 0 && has_record) { 26702e001266SDarrick J. Wong error = xfs_inobt_get_rec(cur, &irec, &has_record); 26712e001266SDarrick J. Wong if (error || irec.ir_startino > high) 26722e001266SDarrick J. Wong break; 26732e001266SDarrick J. Wong 26742e001266SDarrick J. Wong agino = irec.ir_startino; 26752e001266SDarrick J. Wong holemask = irec.ir_holemask; 26762e001266SDarrick J. Wong for (i = 0; i < XFS_INOBT_HOLEMASK_BITS; holemask >>= 1, 26772e001266SDarrick J. Wong i++, agino += XFS_INODES_PER_HOLEMASK_BIT) { 26782e001266SDarrick J. Wong if (holemask & 1) 26792e001266SDarrick J. Wong continue; 26802e001266SDarrick J. Wong if (agino + XFS_INODES_PER_HOLEMASK_BIT > low && 26812e001266SDarrick J. Wong agino <= high) { 26822e001266SDarrick J. Wong *exists = true; 26832e001266SDarrick J. Wong return 0; 26842e001266SDarrick J. Wong } 26852e001266SDarrick J. Wong } 26862e001266SDarrick J. Wong 26872e001266SDarrick J. Wong error = xfs_btree_increment(cur, 0, &has_record); 26882e001266SDarrick J. Wong } 26892e001266SDarrick J. Wong return error; 26902e001266SDarrick J. Wong } 26912e001266SDarrick J. Wong 26922e001266SDarrick J. Wong /* Is there an inode record covering a given extent? */ 26932e001266SDarrick J. Wong int 26942e001266SDarrick J. Wong xfs_ialloc_has_inodes_at_extent( 26952e001266SDarrick J. Wong struct xfs_btree_cur *cur, 26962e001266SDarrick J. Wong xfs_agblock_t bno, 26972e001266SDarrick J. Wong xfs_extlen_t len, 26982e001266SDarrick J. Wong bool *exists) 26992e001266SDarrick J. Wong { 27002e001266SDarrick J. Wong xfs_agino_t low; 27012e001266SDarrick J. Wong xfs_agino_t high; 27022e001266SDarrick J. Wong 270343004b2aSDarrick J. Wong low = XFS_AGB_TO_AGINO(cur->bc_mp, bno); 270443004b2aSDarrick J. Wong high = XFS_AGB_TO_AGINO(cur->bc_mp, bno + len) - 1; 27052e001266SDarrick J. Wong 27062e001266SDarrick J. Wong return xfs_ialloc_has_inode_record(cur, low, high, exists); 27072e001266SDarrick J. Wong } 27082e001266SDarrick J. Wong 27092e001266SDarrick J. Wong struct xfs_ialloc_count_inodes { 27102e001266SDarrick J. Wong xfs_agino_t count; 27112e001266SDarrick J. Wong xfs_agino_t freecount; 27122e001266SDarrick J. Wong }; 27132e001266SDarrick J. Wong 27142e001266SDarrick J. Wong /* Record inode counts across all inobt records. */ 27152e001266SDarrick J. Wong STATIC int 27162e001266SDarrick J. Wong xfs_ialloc_count_inodes_rec( 27172e001266SDarrick J. Wong struct xfs_btree_cur *cur, 27182e001266SDarrick J. Wong union xfs_btree_rec *rec, 27192e001266SDarrick J. Wong void *priv) 27202e001266SDarrick J. Wong { 27212e001266SDarrick J. Wong struct xfs_inobt_rec_incore irec; 27222e001266SDarrick J. Wong struct xfs_ialloc_count_inodes *ci = priv; 27232e001266SDarrick J. Wong 27242e001266SDarrick J. Wong xfs_inobt_btrec_to_irec(cur->bc_mp, rec, &irec); 27252e001266SDarrick J. Wong ci->count += irec.ir_count; 27262e001266SDarrick J. Wong ci->freecount += irec.ir_freecount; 27272e001266SDarrick J. Wong 27282e001266SDarrick J. Wong return 0; 27292e001266SDarrick J. Wong } 27302e001266SDarrick J. Wong 27312e001266SDarrick J. Wong /* Count allocated and free inodes under an inobt. */ 27322e001266SDarrick J. Wong int 27332e001266SDarrick J. Wong xfs_ialloc_count_inodes( 27342e001266SDarrick J. Wong struct xfs_btree_cur *cur, 27352e001266SDarrick J. Wong xfs_agino_t *count, 27362e001266SDarrick J. Wong xfs_agino_t *freecount) 27372e001266SDarrick J. Wong { 27382e001266SDarrick J. Wong struct xfs_ialloc_count_inodes ci = {0}; 27392e001266SDarrick J. Wong int error; 27402e001266SDarrick J. Wong 27412e001266SDarrick J. Wong ASSERT(cur->bc_btnum == XFS_BTNUM_INO); 27422e001266SDarrick J. Wong error = xfs_btree_query_all(cur, xfs_ialloc_count_inodes_rec, &ci); 27432e001266SDarrick J. Wong if (error) 27442e001266SDarrick J. Wong return error; 27452e001266SDarrick J. Wong 27462e001266SDarrick J. Wong *count = ci.count; 27472e001266SDarrick J. Wong *freecount = ci.freecount; 27482e001266SDarrick J. Wong return 0; 27492e001266SDarrick J. Wong } 2750494dba7bSDarrick J. Wong 2751494dba7bSDarrick J. Wong /* 2752494dba7bSDarrick J. Wong * Initialize inode-related geometry information. 2753494dba7bSDarrick J. Wong * 2754494dba7bSDarrick J. Wong * Compute the inode btree min and max levels and set maxicount. 2755494dba7bSDarrick J. Wong * 2756494dba7bSDarrick J. Wong * Set the inode cluster size. This may still be overridden by the file 2757494dba7bSDarrick J. Wong * system block size if it is larger than the chosen cluster size. 2758494dba7bSDarrick J. Wong * 2759494dba7bSDarrick J. Wong * For v5 filesystems, scale the cluster size with the inode size to keep a 2760494dba7bSDarrick J. Wong * constant ratio of inode per cluster buffer, but only if mkfs has set the 2761494dba7bSDarrick J. Wong * inode alignment value appropriately for larger cluster sizes. 2762494dba7bSDarrick J. Wong * 2763494dba7bSDarrick J. Wong * Then compute the inode cluster alignment information. 2764494dba7bSDarrick J. Wong */ 2765494dba7bSDarrick J. Wong void 2766494dba7bSDarrick J. Wong xfs_ialloc_setup_geometry( 2767494dba7bSDarrick J. Wong struct xfs_mount *mp) 2768494dba7bSDarrick J. Wong { 2769494dba7bSDarrick J. Wong struct xfs_sb *sbp = &mp->m_sb; 2770494dba7bSDarrick J. Wong struct xfs_ino_geometry *igeo = M_IGEO(mp); 2771494dba7bSDarrick J. Wong uint64_t icount; 2772494dba7bSDarrick J. Wong uint inodes; 2773494dba7bSDarrick J. Wong 2774f93e5436SDarrick J. Wong igeo->new_diflags2 = 0; 2775f93e5436SDarrick J. Wong if (xfs_sb_version_hasbigtime(&mp->m_sb)) 2776f93e5436SDarrick J. Wong igeo->new_diflags2 |= XFS_DIFLAG2_BIGTIME; 2777f93e5436SDarrick J. Wong 2778494dba7bSDarrick J. Wong /* Compute inode btree geometry. */ 2779494dba7bSDarrick J. Wong igeo->agino_log = sbp->sb_inopblog + sbp->sb_agblklog; 2780494dba7bSDarrick J. Wong igeo->inobt_mxr[0] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 1); 2781494dba7bSDarrick J. Wong igeo->inobt_mxr[1] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 0); 2782494dba7bSDarrick J. Wong igeo->inobt_mnr[0] = igeo->inobt_mxr[0] / 2; 2783494dba7bSDarrick J. Wong igeo->inobt_mnr[1] = igeo->inobt_mxr[1] / 2; 2784494dba7bSDarrick J. Wong 2785494dba7bSDarrick J. Wong igeo->ialloc_inos = max_t(uint16_t, XFS_INODES_PER_CHUNK, 2786494dba7bSDarrick J. Wong sbp->sb_inopblock); 2787494dba7bSDarrick J. Wong igeo->ialloc_blks = igeo->ialloc_inos >> sbp->sb_inopblog; 2788494dba7bSDarrick J. Wong 2789494dba7bSDarrick J. Wong if (sbp->sb_spino_align) 2790494dba7bSDarrick J. Wong igeo->ialloc_min_blks = sbp->sb_spino_align; 2791494dba7bSDarrick J. Wong else 2792494dba7bSDarrick J. Wong igeo->ialloc_min_blks = igeo->ialloc_blks; 2793494dba7bSDarrick J. Wong 2794494dba7bSDarrick J. Wong /* Compute and fill in value of m_ino_geo.inobt_maxlevels. */ 2795494dba7bSDarrick J. Wong inodes = (1LL << XFS_INO_AGINO_BITS(mp)) >> XFS_INODES_PER_CHUNK_LOG; 2796494dba7bSDarrick J. Wong igeo->inobt_maxlevels = xfs_btree_compute_maxlevels(igeo->inobt_mnr, 2797494dba7bSDarrick J. Wong inodes); 2798494dba7bSDarrick J. Wong 2799c94613feSDarrick J. Wong /* 2800c94613feSDarrick J. Wong * Set the maximum inode count for this filesystem, being careful not 2801c94613feSDarrick J. Wong * to use obviously garbage sb_inopblog/sb_inopblock values. Regular 2802c94613feSDarrick J. Wong * users should never get here due to failing sb verification, but 2803c94613feSDarrick J. Wong * certain users (xfs_db) need to be usable even with corrupt metadata. 2804c94613feSDarrick J. Wong */ 2805c94613feSDarrick J. Wong if (sbp->sb_imax_pct && igeo->ialloc_blks) { 2806494dba7bSDarrick J. Wong /* 2807494dba7bSDarrick J. Wong * Make sure the maximum inode count is a multiple 2808494dba7bSDarrick J. Wong * of the units we allocate inodes in. 2809494dba7bSDarrick J. Wong */ 2810494dba7bSDarrick J. Wong icount = sbp->sb_dblocks * sbp->sb_imax_pct; 2811494dba7bSDarrick J. Wong do_div(icount, 100); 2812494dba7bSDarrick J. Wong do_div(icount, igeo->ialloc_blks); 2813494dba7bSDarrick J. Wong igeo->maxicount = XFS_FSB_TO_INO(mp, 2814494dba7bSDarrick J. Wong icount * igeo->ialloc_blks); 2815494dba7bSDarrick J. Wong } else { 2816494dba7bSDarrick J. Wong igeo->maxicount = 0; 2817494dba7bSDarrick J. Wong } 2818494dba7bSDarrick J. Wong 2819490d451fSDarrick J. Wong /* 2820490d451fSDarrick J. Wong * Compute the desired size of an inode cluster buffer size, which 2821490d451fSDarrick J. Wong * starts at 8K and (on v5 filesystems) scales up with larger inode 2822490d451fSDarrick J. Wong * sizes. 2823490d451fSDarrick J. Wong * 2824490d451fSDarrick J. Wong * Preserve the desired inode cluster size because the sparse inodes 2825490d451fSDarrick J. Wong * feature uses that desired size (not the actual size) to compute the 2826490d451fSDarrick J. Wong * sparse inode alignment. The mount code validates this value, so we 2827490d451fSDarrick J. Wong * cannot change the behavior. 2828490d451fSDarrick J. Wong */ 2829490d451fSDarrick J. Wong igeo->inode_cluster_size_raw = XFS_INODE_BIG_CLUSTER_SIZE; 2830b81b79f4SChristoph Hellwig if (xfs_sb_version_has_v3inode(&mp->m_sb)) { 2831490d451fSDarrick J. Wong int new_size = igeo->inode_cluster_size_raw; 2832494dba7bSDarrick J. Wong 2833494dba7bSDarrick J. Wong new_size *= mp->m_sb.sb_inodesize / XFS_DINODE_MIN_SIZE; 2834494dba7bSDarrick J. Wong if (mp->m_sb.sb_inoalignmt >= XFS_B_TO_FSBT(mp, new_size)) 2835490d451fSDarrick J. Wong igeo->inode_cluster_size_raw = new_size; 2836494dba7bSDarrick J. Wong } 2837494dba7bSDarrick J. Wong 2838494dba7bSDarrick J. Wong /* Calculate inode cluster ratios. */ 2839490d451fSDarrick J. Wong if (igeo->inode_cluster_size_raw > mp->m_sb.sb_blocksize) 2840494dba7bSDarrick J. Wong igeo->blocks_per_cluster = XFS_B_TO_FSBT(mp, 2841490d451fSDarrick J. Wong igeo->inode_cluster_size_raw); 2842494dba7bSDarrick J. Wong else 2843494dba7bSDarrick J. Wong igeo->blocks_per_cluster = 1; 2844490d451fSDarrick J. Wong igeo->inode_cluster_size = XFS_FSB_TO_B(mp, igeo->blocks_per_cluster); 2845494dba7bSDarrick J. Wong igeo->inodes_per_cluster = XFS_FSB_TO_INO(mp, igeo->blocks_per_cluster); 2846494dba7bSDarrick J. Wong 2847494dba7bSDarrick J. Wong /* Calculate inode cluster alignment. */ 2848494dba7bSDarrick J. Wong if (xfs_sb_version_hasalign(&mp->m_sb) && 2849494dba7bSDarrick J. Wong mp->m_sb.sb_inoalignmt >= igeo->blocks_per_cluster) 2850494dba7bSDarrick J. Wong igeo->cluster_align = mp->m_sb.sb_inoalignmt; 2851494dba7bSDarrick J. Wong else 2852494dba7bSDarrick J. Wong igeo->cluster_align = 1; 2853494dba7bSDarrick J. Wong igeo->inoalign_mask = igeo->cluster_align - 1; 2854494dba7bSDarrick J. Wong igeo->cluster_align_inodes = XFS_FSB_TO_INO(mp, igeo->cluster_align); 2855494dba7bSDarrick J. Wong 2856494dba7bSDarrick J. Wong /* 2857494dba7bSDarrick J. Wong * If we are using stripe alignment, check whether 2858494dba7bSDarrick J. Wong * the stripe unit is a multiple of the inode alignment 2859494dba7bSDarrick J. Wong */ 2860494dba7bSDarrick J. Wong if (mp->m_dalign && igeo->inoalign_mask && 2861494dba7bSDarrick J. Wong !(mp->m_dalign & igeo->inoalign_mask)) 2862494dba7bSDarrick J. Wong igeo->ialloc_align = mp->m_dalign; 2863494dba7bSDarrick J. Wong else 2864494dba7bSDarrick J. Wong igeo->ialloc_align = 0; 2865494dba7bSDarrick J. Wong } 286613eaec4bSDarrick J. Wong 286713eaec4bSDarrick J. Wong /* Compute the location of the root directory inode that is laid out by mkfs. */ 286813eaec4bSDarrick J. Wong xfs_ino_t 286913eaec4bSDarrick J. Wong xfs_ialloc_calc_rootino( 287013eaec4bSDarrick J. Wong struct xfs_mount *mp, 287113eaec4bSDarrick J. Wong int sunit) 287213eaec4bSDarrick J. Wong { 287313eaec4bSDarrick J. Wong struct xfs_ino_geometry *igeo = M_IGEO(mp); 287413eaec4bSDarrick J. Wong xfs_agblock_t first_bno; 287513eaec4bSDarrick J. Wong 287613eaec4bSDarrick J. Wong /* 287713eaec4bSDarrick J. Wong * Pre-calculate the geometry of AG 0. We know what it looks like 287813eaec4bSDarrick J. Wong * because libxfs knows how to create allocation groups now. 287913eaec4bSDarrick J. Wong * 288013eaec4bSDarrick J. Wong * first_bno is the first block in which mkfs could possibly have 288113eaec4bSDarrick J. Wong * allocated the root directory inode, once we factor in the metadata 288213eaec4bSDarrick J. Wong * that mkfs formats before it. Namely, the four AG headers... 288313eaec4bSDarrick J. Wong */ 288413eaec4bSDarrick J. Wong first_bno = howmany(4 * mp->m_sb.sb_sectsize, mp->m_sb.sb_blocksize); 288513eaec4bSDarrick J. Wong 288613eaec4bSDarrick J. Wong /* ...the two free space btree roots... */ 288713eaec4bSDarrick J. Wong first_bno += 2; 288813eaec4bSDarrick J. Wong 288913eaec4bSDarrick J. Wong /* ...the inode btree root... */ 289013eaec4bSDarrick J. Wong first_bno += 1; 289113eaec4bSDarrick J. Wong 289213eaec4bSDarrick J. Wong /* ...the initial AGFL... */ 289313eaec4bSDarrick J. Wong first_bno += xfs_alloc_min_freelist(mp, NULL); 289413eaec4bSDarrick J. Wong 289513eaec4bSDarrick J. Wong /* ...the free inode btree root... */ 289613eaec4bSDarrick J. Wong if (xfs_sb_version_hasfinobt(&mp->m_sb)) 289713eaec4bSDarrick J. Wong first_bno++; 289813eaec4bSDarrick J. Wong 289913eaec4bSDarrick J. Wong /* ...the reverse mapping btree root... */ 290013eaec4bSDarrick J. Wong if (xfs_sb_version_hasrmapbt(&mp->m_sb)) 290113eaec4bSDarrick J. Wong first_bno++; 290213eaec4bSDarrick J. Wong 290313eaec4bSDarrick J. Wong /* ...the reference count btree... */ 290413eaec4bSDarrick J. Wong if (xfs_sb_version_hasreflink(&mp->m_sb)) 290513eaec4bSDarrick J. Wong first_bno++; 290613eaec4bSDarrick J. Wong 290713eaec4bSDarrick J. Wong /* 290813eaec4bSDarrick J. Wong * ...and the log, if it is allocated in the first allocation group. 290913eaec4bSDarrick J. Wong * 291013eaec4bSDarrick J. Wong * This can happen with filesystems that only have a single 291113eaec4bSDarrick J. Wong * allocation group, or very odd geometries created by old mkfs 291213eaec4bSDarrick J. Wong * versions on very small filesystems. 291313eaec4bSDarrick J. Wong */ 291413eaec4bSDarrick J. Wong if (mp->m_sb.sb_logstart && 291513eaec4bSDarrick J. Wong XFS_FSB_TO_AGNO(mp, mp->m_sb.sb_logstart) == 0) 291613eaec4bSDarrick J. Wong first_bno += mp->m_sb.sb_logblocks; 291713eaec4bSDarrick J. Wong 291813eaec4bSDarrick J. Wong /* 291913eaec4bSDarrick J. Wong * Now round first_bno up to whatever allocation alignment is given 292013eaec4bSDarrick J. Wong * by the filesystem or was passed in. 292113eaec4bSDarrick J. Wong */ 292213eaec4bSDarrick J. Wong if (xfs_sb_version_hasdalign(&mp->m_sb) && igeo->ialloc_align > 0) 292313eaec4bSDarrick J. Wong first_bno = roundup(first_bno, sunit); 292413eaec4bSDarrick J. Wong else if (xfs_sb_version_hasalign(&mp->m_sb) && 292513eaec4bSDarrick J. Wong mp->m_sb.sb_inoalignmt > 1) 292613eaec4bSDarrick J. Wong first_bno = roundup(first_bno, mp->m_sb.sb_inoalignmt); 292713eaec4bSDarrick J. Wong 292813eaec4bSDarrick J. Wong return XFS_AGINO_TO_INO(mp, 0, XFS_AGB_TO_AGINO(mp, first_bno)); 292913eaec4bSDarrick J. Wong } 2930