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); 6138c26bfdSDave Chinner if (xfs_has_sparseinodes(cur->bc_mp)) { 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, 77159eb69dSDarrick J. Wong const 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); 8138c26bfdSDave Chinner if (xfs_has_sparseinodes(mp)) { 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 2179ba0889eSDave Chinner static int 21830f712c9SDave Chinner xfs_check_agi_freecount( 2199ba0889eSDave 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 24475c8c50fSDave Chinner if (!xfs_is_shutdown(cur->bc_mp)) 2459ba0889eSDave Chinner ASSERT(freecount == cur->bc_ag.pag->pagi_freecount); 24630f712c9SDave Chinner } 24730f712c9SDave Chinner return 0; 24830f712c9SDave Chinner } 24930f712c9SDave Chinner #else 2509ba0889eSDave 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 */ 305ebd9027dSDave Chinner if (xfs_has_v3inodes(mp)) { 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; 34030f712c9SDave Chinner 34130f712c9SDave Chinner free = xfs_make_iptr(mp, fbuf, i); 34230f712c9SDave Chinner free->di_magic = cpu_to_be16(XFS_DINODE_MAGIC); 34330f712c9SDave Chinner free->di_version = version; 34430f712c9SDave Chinner free->di_gen = cpu_to_be32(gen); 34530f712c9SDave Chinner free->di_next_unlinked = cpu_to_be32(NULLAGINO); 34630f712c9SDave Chinner 34730f712c9SDave Chinner if (version == 3) { 34830f712c9SDave Chinner free->di_ino = cpu_to_be64(ino); 34930f712c9SDave Chinner ino++; 350ce748eaaSEric Sandeen uuid_copy(&free->di_uuid, 351ce748eaaSEric Sandeen &mp->m_sb.sb_meta_uuid); 35230f712c9SDave Chinner xfs_dinode_calc_crc(mp, free); 35330f712c9SDave Chinner } else if (tp) { 35430f712c9SDave Chinner /* just log the inode core */ 35530f712c9SDave Chinner xfs_trans_log_buf(tp, fbuf, ioffset, 356cf28e17cSDave Chinner ioffset + XFS_DINODE_SIZE(mp) - 1); 35730f712c9SDave Chinner } 35830f712c9SDave Chinner } 35930f712c9SDave Chinner 36030f712c9SDave Chinner if (tp) { 36130f712c9SDave Chinner /* 36230f712c9SDave Chinner * Mark the buffer as an inode allocation buffer so it 36330f712c9SDave Chinner * sticks in AIL at the point of this allocation 36430f712c9SDave Chinner * transaction. This ensures the they are on disk before 36530f712c9SDave Chinner * the tail of the log can be moved past this 36630f712c9SDave Chinner * transaction (i.e. by preventing relogging from moving 36730f712c9SDave Chinner * it forward in the log). 36830f712c9SDave Chinner */ 36930f712c9SDave Chinner xfs_trans_inode_alloc_buf(tp, fbuf); 37030f712c9SDave Chinner if (version == 3) { 37130f712c9SDave Chinner /* 37230f712c9SDave Chinner * Mark the buffer as ordered so that they are 37330f712c9SDave Chinner * not physically logged in the transaction but 37430f712c9SDave Chinner * still tracked in the AIL as part of the 37530f712c9SDave Chinner * transaction and pin the log appropriately. 37630f712c9SDave Chinner */ 37730f712c9SDave Chinner xfs_trans_ordered_buf(tp, fbuf); 37830f712c9SDave Chinner } 37930f712c9SDave Chinner } else { 38030f712c9SDave Chinner fbuf->b_flags |= XBF_DONE; 38130f712c9SDave Chinner xfs_buf_delwri_queue(fbuf, buffer_list); 38230f712c9SDave Chinner xfs_buf_relse(fbuf); 38330f712c9SDave Chinner } 38430f712c9SDave Chinner } 38530f712c9SDave Chinner return 0; 38630f712c9SDave Chinner } 38730f712c9SDave Chinner 38830f712c9SDave Chinner /* 38956d1115cSBrian Foster * Align startino and allocmask for a recently allocated sparse chunk such that 39056d1115cSBrian Foster * they are fit for insertion (or merge) into the on-disk inode btrees. 39156d1115cSBrian Foster * 39256d1115cSBrian Foster * Background: 39356d1115cSBrian Foster * 39456d1115cSBrian Foster * When enabled, sparse inode support increases the inode alignment from cluster 39556d1115cSBrian Foster * size to inode chunk size. This means that the minimum range between two 39656d1115cSBrian Foster * non-adjacent inode records in the inobt is large enough for a full inode 39756d1115cSBrian Foster * record. This allows for cluster sized, cluster aligned block allocation 39856d1115cSBrian Foster * without need to worry about whether the resulting inode record overlaps with 39956d1115cSBrian Foster * another record in the tree. Without this basic rule, we would have to deal 40056d1115cSBrian Foster * with the consequences of overlap by potentially undoing recent allocations in 40156d1115cSBrian Foster * the inode allocation codepath. 40256d1115cSBrian Foster * 40356d1115cSBrian Foster * Because of this alignment rule (which is enforced on mount), there are two 40456d1115cSBrian Foster * inobt possibilities for newly allocated sparse chunks. One is that the 40556d1115cSBrian Foster * aligned inode record for the chunk covers a range of inodes not already 40656d1115cSBrian Foster * covered in the inobt (i.e., it is safe to insert a new sparse record). The 40756d1115cSBrian Foster * other is that a record already exists at the aligned startino that considers 40856d1115cSBrian Foster * the newly allocated range as sparse. In the latter case, record content is 40956d1115cSBrian Foster * merged in hope that sparse inode chunks fill to full chunks over time. 41056d1115cSBrian Foster */ 41156d1115cSBrian Foster STATIC void 41256d1115cSBrian Foster xfs_align_sparse_ino( 41356d1115cSBrian Foster struct xfs_mount *mp, 41456d1115cSBrian Foster xfs_agino_t *startino, 41556d1115cSBrian Foster uint16_t *allocmask) 41656d1115cSBrian Foster { 41756d1115cSBrian Foster xfs_agblock_t agbno; 41856d1115cSBrian Foster xfs_agblock_t mod; 41956d1115cSBrian Foster int offset; 42056d1115cSBrian Foster 42156d1115cSBrian Foster agbno = XFS_AGINO_TO_AGBNO(mp, *startino); 42256d1115cSBrian Foster mod = agbno % mp->m_sb.sb_inoalignmt; 42356d1115cSBrian Foster if (!mod) 42456d1115cSBrian Foster return; 42556d1115cSBrian Foster 42656d1115cSBrian Foster /* calculate the inode offset and align startino */ 42743004b2aSDarrick J. Wong offset = XFS_AGB_TO_AGINO(mp, mod); 42856d1115cSBrian Foster *startino -= offset; 42956d1115cSBrian Foster 43056d1115cSBrian Foster /* 43156d1115cSBrian Foster * Since startino has been aligned down, left shift allocmask such that 43256d1115cSBrian Foster * it continues to represent the same physical inodes relative to the 43356d1115cSBrian Foster * new startino. 43456d1115cSBrian Foster */ 43556d1115cSBrian Foster *allocmask <<= offset / XFS_INODES_PER_HOLEMASK_BIT; 43656d1115cSBrian Foster } 43756d1115cSBrian Foster 43856d1115cSBrian Foster /* 43956d1115cSBrian Foster * Determine whether the source inode record can merge into the target. Both 44056d1115cSBrian Foster * records must be sparse, the inode ranges must match and there must be no 44156d1115cSBrian Foster * allocation overlap between the records. 44256d1115cSBrian Foster */ 44356d1115cSBrian Foster STATIC bool 44456d1115cSBrian Foster __xfs_inobt_can_merge( 44556d1115cSBrian Foster struct xfs_inobt_rec_incore *trec, /* tgt record */ 44656d1115cSBrian Foster struct xfs_inobt_rec_incore *srec) /* src record */ 44756d1115cSBrian Foster { 44856d1115cSBrian Foster uint64_t talloc; 44956d1115cSBrian Foster uint64_t salloc; 45056d1115cSBrian Foster 45156d1115cSBrian Foster /* records must cover the same inode range */ 45256d1115cSBrian Foster if (trec->ir_startino != srec->ir_startino) 45356d1115cSBrian Foster return false; 45456d1115cSBrian Foster 45556d1115cSBrian Foster /* both records must be sparse */ 45656d1115cSBrian Foster if (!xfs_inobt_issparse(trec->ir_holemask) || 45756d1115cSBrian Foster !xfs_inobt_issparse(srec->ir_holemask)) 45856d1115cSBrian Foster return false; 45956d1115cSBrian Foster 46056d1115cSBrian Foster /* both records must track some inodes */ 46156d1115cSBrian Foster if (!trec->ir_count || !srec->ir_count) 46256d1115cSBrian Foster return false; 46356d1115cSBrian Foster 46456d1115cSBrian Foster /* can't exceed capacity of a full record */ 46556d1115cSBrian Foster if (trec->ir_count + srec->ir_count > XFS_INODES_PER_CHUNK) 46656d1115cSBrian Foster return false; 46756d1115cSBrian Foster 46856d1115cSBrian Foster /* verify there is no allocation overlap */ 46956d1115cSBrian Foster talloc = xfs_inobt_irec_to_allocmask(trec); 47056d1115cSBrian Foster salloc = xfs_inobt_irec_to_allocmask(srec); 47156d1115cSBrian Foster if (talloc & salloc) 47256d1115cSBrian Foster return false; 47356d1115cSBrian Foster 47456d1115cSBrian Foster return true; 47556d1115cSBrian Foster } 47656d1115cSBrian Foster 47756d1115cSBrian Foster /* 47856d1115cSBrian Foster * Merge the source inode record into the target. The caller must call 47956d1115cSBrian Foster * __xfs_inobt_can_merge() to ensure the merge is valid. 48056d1115cSBrian Foster */ 48156d1115cSBrian Foster STATIC void 48256d1115cSBrian Foster __xfs_inobt_rec_merge( 48356d1115cSBrian Foster struct xfs_inobt_rec_incore *trec, /* target */ 48456d1115cSBrian Foster struct xfs_inobt_rec_incore *srec) /* src */ 48556d1115cSBrian Foster { 48656d1115cSBrian Foster ASSERT(trec->ir_startino == srec->ir_startino); 48756d1115cSBrian Foster 48856d1115cSBrian Foster /* combine the counts */ 48956d1115cSBrian Foster trec->ir_count += srec->ir_count; 49056d1115cSBrian Foster trec->ir_freecount += srec->ir_freecount; 49156d1115cSBrian Foster 49256d1115cSBrian Foster /* 49356d1115cSBrian Foster * Merge the holemask and free mask. For both fields, 0 bits refer to 49456d1115cSBrian Foster * allocated inodes. We combine the allocated ranges with bitwise AND. 49556d1115cSBrian Foster */ 49656d1115cSBrian Foster trec->ir_holemask &= srec->ir_holemask; 49756d1115cSBrian Foster trec->ir_free &= srec->ir_free; 49856d1115cSBrian Foster } 49956d1115cSBrian Foster 50056d1115cSBrian Foster /* 50156d1115cSBrian Foster * Insert a new sparse inode chunk into the associated inode btree. The inode 50256d1115cSBrian Foster * record for the sparse chunk is pre-aligned to a startino that should match 50356d1115cSBrian Foster * any pre-existing sparse inode record in the tree. This allows sparse chunks 50456d1115cSBrian Foster * to fill over time. 50556d1115cSBrian Foster * 50656d1115cSBrian Foster * This function supports two modes of handling preexisting records depending on 50756d1115cSBrian Foster * the merge flag. If merge is true, the provided record is merged with the 50856d1115cSBrian Foster * existing record and updated in place. The merged record is returned in nrec. 50956d1115cSBrian Foster * If merge is false, an existing record is replaced with the provided record. 51056d1115cSBrian Foster * If no preexisting record exists, the provided record is always inserted. 51156d1115cSBrian Foster * 51256d1115cSBrian Foster * It is considered corruption if a merge is requested and not possible. Given 51356d1115cSBrian Foster * the sparse inode alignment constraints, this should never happen. 51456d1115cSBrian Foster */ 51556d1115cSBrian Foster STATIC int 51656d1115cSBrian Foster xfs_inobt_insert_sprec( 51756d1115cSBrian Foster struct xfs_mount *mp, 51856d1115cSBrian Foster struct xfs_trans *tp, 51956d1115cSBrian Foster struct xfs_buf *agbp, 5207b13c515SDave Chinner struct xfs_perag *pag, 52156d1115cSBrian Foster int btnum, 52256d1115cSBrian Foster struct xfs_inobt_rec_incore *nrec, /* in/out: new/merged rec. */ 52356d1115cSBrian Foster bool merge) /* merge or replace */ 52456d1115cSBrian Foster { 52556d1115cSBrian Foster struct xfs_btree_cur *cur; 52656d1115cSBrian Foster int error; 52756d1115cSBrian Foster int i; 52856d1115cSBrian Foster struct xfs_inobt_rec_incore rec; 52956d1115cSBrian Foster 5307b13c515SDave Chinner cur = xfs_inobt_init_cursor(mp, tp, agbp, pag, btnum); 53156d1115cSBrian Foster 53256d1115cSBrian Foster /* the new record is pre-aligned so we know where to look */ 53356d1115cSBrian Foster error = xfs_inobt_lookup(cur, nrec->ir_startino, XFS_LOOKUP_EQ, &i); 53456d1115cSBrian Foster if (error) 53556d1115cSBrian Foster goto error; 53656d1115cSBrian Foster /* if nothing there, insert a new record and return */ 53756d1115cSBrian Foster if (i == 0) { 53856d1115cSBrian Foster error = xfs_inobt_insert_rec(cur, nrec->ir_holemask, 53956d1115cSBrian Foster nrec->ir_count, nrec->ir_freecount, 54056d1115cSBrian Foster nrec->ir_free, &i); 54156d1115cSBrian Foster if (error) 54256d1115cSBrian Foster goto error; 543f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(mp, i != 1)) { 544f9e03706SDarrick J. Wong error = -EFSCORRUPTED; 545f9e03706SDarrick J. Wong goto error; 546f9e03706SDarrick J. Wong } 54756d1115cSBrian Foster 54856d1115cSBrian Foster goto out; 54956d1115cSBrian Foster } 55056d1115cSBrian Foster 55156d1115cSBrian Foster /* 55256d1115cSBrian Foster * A record exists at this startino. Merge or replace the record 55356d1115cSBrian Foster * depending on what we've been asked to do. 55456d1115cSBrian Foster */ 55556d1115cSBrian Foster if (merge) { 55656d1115cSBrian Foster error = xfs_inobt_get_rec(cur, &rec, &i); 55756d1115cSBrian Foster if (error) 55856d1115cSBrian Foster goto error; 559f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(mp, i != 1)) { 560f9e03706SDarrick J. Wong error = -EFSCORRUPTED; 561f9e03706SDarrick J. Wong goto error; 562f9e03706SDarrick J. Wong } 563f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(mp, rec.ir_startino != nrec->ir_startino)) { 564f9e03706SDarrick J. Wong error = -EFSCORRUPTED; 565f9e03706SDarrick J. Wong goto error; 566f9e03706SDarrick J. Wong } 56756d1115cSBrian Foster 56856d1115cSBrian Foster /* 56956d1115cSBrian Foster * This should never fail. If we have coexisting records that 57056d1115cSBrian Foster * cannot merge, something is seriously wrong. 57156d1115cSBrian Foster */ 572f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(mp, !__xfs_inobt_can_merge(nrec, &rec))) { 573f9e03706SDarrick J. Wong error = -EFSCORRUPTED; 574f9e03706SDarrick J. Wong goto error; 575f9e03706SDarrick J. Wong } 57656d1115cSBrian Foster 5777b13c515SDave Chinner trace_xfs_irec_merge_pre(mp, pag->pag_agno, rec.ir_startino, 57856d1115cSBrian Foster rec.ir_holemask, nrec->ir_startino, 57956d1115cSBrian Foster nrec->ir_holemask); 58056d1115cSBrian Foster 58156d1115cSBrian Foster /* merge to nrec to output the updated record */ 58256d1115cSBrian Foster __xfs_inobt_rec_merge(nrec, &rec); 58356d1115cSBrian Foster 5847b13c515SDave Chinner trace_xfs_irec_merge_post(mp, pag->pag_agno, nrec->ir_startino, 58556d1115cSBrian Foster nrec->ir_holemask); 58656d1115cSBrian Foster 58756d1115cSBrian Foster error = xfs_inobt_rec_check_count(mp, nrec); 58856d1115cSBrian Foster if (error) 58956d1115cSBrian Foster goto error; 59056d1115cSBrian Foster } 59156d1115cSBrian Foster 59256d1115cSBrian Foster error = xfs_inobt_update(cur, nrec); 59356d1115cSBrian Foster if (error) 59456d1115cSBrian Foster goto error; 59556d1115cSBrian Foster 59656d1115cSBrian Foster out: 59756d1115cSBrian Foster xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); 59856d1115cSBrian Foster return 0; 59956d1115cSBrian Foster error: 60056d1115cSBrian Foster xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); 60156d1115cSBrian Foster return error; 60256d1115cSBrian Foster } 60356d1115cSBrian Foster 60456d1115cSBrian Foster /* 6058237fbf5SDave Chinner * Allocate new inodes in the allocation group specified by agbp. Returns 0 if 6068237fbf5SDave Chinner * inodes were allocated in this AG; -EAGAIN if there was no space in this AG so 6078237fbf5SDave Chinner * the caller knows it can try another AG, a hard -ENOSPC when over the maximum 6088237fbf5SDave Chinner * inode count threshold, or the usual negative error code for other errors. 60930f712c9SDave Chinner */ 610ef325959SDarrick J. Wong STATIC int 61130f712c9SDave Chinner xfs_ialloc_ag_alloc( 612ef325959SDarrick J. Wong struct xfs_trans *tp, 6137b13c515SDave Chinner struct xfs_buf *agbp, 6147b13c515SDave Chinner struct xfs_perag *pag) 61530f712c9SDave Chinner { 616ef325959SDarrick J. Wong struct xfs_agi *agi; 617ef325959SDarrick J. Wong struct xfs_alloc_arg args; 61830f712c9SDave Chinner int error; 61930f712c9SDave Chinner xfs_agino_t newino; /* new first inode's number */ 62030f712c9SDave Chinner xfs_agino_t newlen; /* new number of inodes */ 621ef325959SDarrick J. Wong int isaligned = 0; /* inode allocation at stripe */ 622ef325959SDarrick J. Wong /* unit boundary */ 623ef325959SDarrick J. Wong /* init. to full chunk */ 62456d1115cSBrian Foster struct xfs_inobt_rec_incore rec; 625ef325959SDarrick J. Wong struct xfs_ino_geometry *igeo = M_IGEO(tp->t_mountp); 6267b13c515SDave Chinner uint16_t allocmask = (uint16_t) -1; 6271cdadee1SBrian Foster int do_sparse = 0; 6281cdadee1SBrian Foster 62930f712c9SDave Chinner memset(&args, 0, sizeof(args)); 63030f712c9SDave Chinner args.tp = tp; 63130f712c9SDave Chinner args.mp = tp->t_mountp; 6321cdadee1SBrian Foster args.fsbno = NULLFSBLOCK; 6337280fedaSDarrick J. Wong args.oinfo = XFS_RMAP_OINFO_INODES; 63430f712c9SDave Chinner 63546fc58daSBrian Foster #ifdef DEBUG 63646fc58daSBrian Foster /* randomly do sparse inode allocations */ 637ebd9027dSDave Chinner if (xfs_has_sparseinodes(tp->t_mountp) && 638ef325959SDarrick J. Wong igeo->ialloc_min_blks < igeo->ialloc_blks) 63946fc58daSBrian Foster do_sparse = prandom_u32() & 1; 64046fc58daSBrian Foster #endif 64146fc58daSBrian Foster 64230f712c9SDave Chinner /* 64330f712c9SDave Chinner * Locking will ensure that we don't have two callers in here 64430f712c9SDave Chinner * at one time. 64530f712c9SDave Chinner */ 646ef325959SDarrick J. Wong newlen = igeo->ialloc_inos; 647ef325959SDarrick J. Wong if (igeo->maxicount && 64874f9ce1cSGeorge Wang percpu_counter_read_positive(&args.mp->m_icount) + newlen > 649ef325959SDarrick J. Wong igeo->maxicount) 6502451337dSDave Chinner return -ENOSPC; 651ef325959SDarrick J. Wong args.minlen = args.maxlen = igeo->ialloc_blks; 65230f712c9SDave Chinner /* 65330f712c9SDave Chinner * First try to allocate inodes contiguous with the last-allocated 65430f712c9SDave Chinner * chunk of inodes. If the filesystem is striped, this will fill 65530f712c9SDave Chinner * an entire stripe unit with inodes. 65630f712c9SDave Chinner */ 657370c782bSChristoph Hellwig agi = agbp->b_addr; 65830f712c9SDave Chinner newino = be32_to_cpu(agi->agi_newino); 65930f712c9SDave Chinner args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) + 660ef325959SDarrick J. Wong igeo->ialloc_blks; 6611cdadee1SBrian Foster if (do_sparse) 6621cdadee1SBrian Foster goto sparse_alloc; 66330f712c9SDave Chinner if (likely(newino != NULLAGINO && 66430f712c9SDave Chinner (args.agbno < be32_to_cpu(agi->agi_length)))) { 6657b13c515SDave Chinner args.fsbno = XFS_AGB_TO_FSB(args.mp, pag->pag_agno, args.agbno); 66630f712c9SDave Chinner args.type = XFS_ALLOCTYPE_THIS_BNO; 66730f712c9SDave Chinner args.prod = 1; 66830f712c9SDave Chinner 66930f712c9SDave Chinner /* 67030f712c9SDave Chinner * We need to take into account alignment here to ensure that 67130f712c9SDave Chinner * we don't modify the free list if we fail to have an exact 67230f712c9SDave Chinner * block. If we don't have an exact match, and every oher 67330f712c9SDave Chinner * attempt allocation attempt fails, we'll end up cancelling 67430f712c9SDave Chinner * a dirty transaction and shutting down. 67530f712c9SDave Chinner * 67630f712c9SDave Chinner * For an exact allocation, alignment must be 1, 67730f712c9SDave Chinner * however we need to take cluster alignment into account when 67830f712c9SDave Chinner * fixing up the freelist. Use the minalignslop field to 67930f712c9SDave Chinner * indicate that extra blocks might be required for alignment, 68030f712c9SDave Chinner * but not to use them in the actual exact allocation. 68130f712c9SDave Chinner */ 68230f712c9SDave Chinner args.alignment = 1; 683ef325959SDarrick J. Wong args.minalignslop = igeo->cluster_align - 1; 68430f712c9SDave Chinner 68530f712c9SDave Chinner /* Allow space for the inode btree to split. */ 686657f1019SBrian Foster args.minleft = igeo->inobt_maxlevels; 68730f712c9SDave Chinner if ((error = xfs_alloc_vextent(&args))) 68830f712c9SDave Chinner return error; 68930f712c9SDave Chinner 69030f712c9SDave Chinner /* 69130f712c9SDave Chinner * This request might have dirtied the transaction if the AG can 69230f712c9SDave Chinner * satisfy the request, but the exact block was not available. 69330f712c9SDave Chinner * If the allocation did fail, subsequent requests will relax 69430f712c9SDave Chinner * the exact agbno requirement and increase the alignment 69530f712c9SDave Chinner * instead. It is critical that the total size of the request 69630f712c9SDave Chinner * (len + alignment + slop) does not increase from this point 69730f712c9SDave Chinner * on, so reset minalignslop to ensure it is not included in 69830f712c9SDave Chinner * subsequent requests. 69930f712c9SDave Chinner */ 70030f712c9SDave Chinner args.minalignslop = 0; 7011cdadee1SBrian Foster } 70230f712c9SDave Chinner 70330f712c9SDave Chinner if (unlikely(args.fsbno == NULLFSBLOCK)) { 70430f712c9SDave Chinner /* 70530f712c9SDave Chinner * Set the alignment for the allocation. 70630f712c9SDave Chinner * If stripe alignment is turned on then align at stripe unit 70730f712c9SDave Chinner * boundary. 70830f712c9SDave Chinner * If the cluster size is smaller than a filesystem block 70930f712c9SDave Chinner * then we're doing I/O for inodes in filesystem block size 71030f712c9SDave Chinner * pieces, so don't need alignment anyway. 71130f712c9SDave Chinner */ 71230f712c9SDave Chinner isaligned = 0; 713ef325959SDarrick J. Wong if (igeo->ialloc_align) { 7140560f31aSDave Chinner ASSERT(!xfs_has_noalign(args.mp)); 71530f712c9SDave Chinner args.alignment = args.mp->m_dalign; 71630f712c9SDave Chinner isaligned = 1; 71730f712c9SDave Chinner } else 718ef325959SDarrick J. Wong args.alignment = igeo->cluster_align; 71930f712c9SDave Chinner /* 72030f712c9SDave Chinner * Need to figure out where to allocate the inode blocks. 72130f712c9SDave Chinner * Ideally they should be spaced out through the a.g. 72230f712c9SDave Chinner * For now, just allocate blocks up front. 72330f712c9SDave Chinner */ 72430f712c9SDave Chinner args.agbno = be32_to_cpu(agi->agi_root); 7257b13c515SDave Chinner args.fsbno = XFS_AGB_TO_FSB(args.mp, pag->pag_agno, args.agbno); 72630f712c9SDave Chinner /* 72730f712c9SDave Chinner * Allocate a fixed-size extent of inodes. 72830f712c9SDave Chinner */ 72930f712c9SDave Chinner args.type = XFS_ALLOCTYPE_NEAR_BNO; 73030f712c9SDave Chinner args.prod = 1; 73130f712c9SDave Chinner /* 73230f712c9SDave Chinner * Allow space for the inode btree to split. 73330f712c9SDave Chinner */ 734657f1019SBrian Foster args.minleft = igeo->inobt_maxlevels; 73530f712c9SDave Chinner if ((error = xfs_alloc_vextent(&args))) 73630f712c9SDave Chinner return error; 73730f712c9SDave Chinner } 73830f712c9SDave Chinner 73930f712c9SDave Chinner /* 74030f712c9SDave Chinner * If stripe alignment is turned on, then try again with cluster 74130f712c9SDave Chinner * alignment. 74230f712c9SDave Chinner */ 74330f712c9SDave Chinner if (isaligned && args.fsbno == NULLFSBLOCK) { 74430f712c9SDave Chinner args.type = XFS_ALLOCTYPE_NEAR_BNO; 74530f712c9SDave Chinner args.agbno = be32_to_cpu(agi->agi_root); 7467b13c515SDave Chinner args.fsbno = XFS_AGB_TO_FSB(args.mp, pag->pag_agno, args.agbno); 747ef325959SDarrick J. Wong args.alignment = igeo->cluster_align; 74830f712c9SDave Chinner if ((error = xfs_alloc_vextent(&args))) 74930f712c9SDave Chinner return error; 75030f712c9SDave Chinner } 75130f712c9SDave Chinner 75256d1115cSBrian Foster /* 75356d1115cSBrian Foster * Finally, try a sparse allocation if the filesystem supports it and 75456d1115cSBrian Foster * the sparse allocation length is smaller than a full chunk. 75556d1115cSBrian Foster */ 756ebd9027dSDave Chinner if (xfs_has_sparseinodes(args.mp) && 757ef325959SDarrick J. Wong igeo->ialloc_min_blks < igeo->ialloc_blks && 75856d1115cSBrian Foster args.fsbno == NULLFSBLOCK) { 7591cdadee1SBrian Foster sparse_alloc: 76056d1115cSBrian Foster args.type = XFS_ALLOCTYPE_NEAR_BNO; 76156d1115cSBrian Foster args.agbno = be32_to_cpu(agi->agi_root); 7627b13c515SDave Chinner args.fsbno = XFS_AGB_TO_FSB(args.mp, pag->pag_agno, args.agbno); 76356d1115cSBrian Foster args.alignment = args.mp->m_sb.sb_spino_align; 76456d1115cSBrian Foster args.prod = 1; 76556d1115cSBrian Foster 766ef325959SDarrick J. Wong args.minlen = igeo->ialloc_min_blks; 76756d1115cSBrian Foster args.maxlen = args.minlen; 76856d1115cSBrian Foster 76956d1115cSBrian Foster /* 77056d1115cSBrian Foster * The inode record will be aligned to full chunk size. We must 77156d1115cSBrian Foster * prevent sparse allocation from AG boundaries that result in 77256d1115cSBrian Foster * invalid inode records, such as records that start at agbno 0 77356d1115cSBrian Foster * or extend beyond the AG. 77456d1115cSBrian Foster * 77556d1115cSBrian Foster * Set min agbno to the first aligned, non-zero agbno and max to 77656d1115cSBrian Foster * the last aligned agbno that is at least one full chunk from 77756d1115cSBrian Foster * the end of the AG. 77856d1115cSBrian Foster */ 77956d1115cSBrian Foster args.min_agbno = args.mp->m_sb.sb_inoalignmt; 78056d1115cSBrian Foster args.max_agbno = round_down(args.mp->m_sb.sb_agblocks, 78156d1115cSBrian Foster args.mp->m_sb.sb_inoalignmt) - 782ef325959SDarrick J. Wong igeo->ialloc_blks; 78356d1115cSBrian Foster 78456d1115cSBrian Foster error = xfs_alloc_vextent(&args); 78556d1115cSBrian Foster if (error) 78656d1115cSBrian Foster return error; 78756d1115cSBrian Foster 78843004b2aSDarrick J. Wong newlen = XFS_AGB_TO_AGINO(args.mp, args.len); 78946fc58daSBrian Foster ASSERT(newlen <= XFS_INODES_PER_CHUNK); 79056d1115cSBrian Foster allocmask = (1 << (newlen / XFS_INODES_PER_HOLEMASK_BIT)) - 1; 79156d1115cSBrian Foster } 79256d1115cSBrian Foster 7933937493cSGao Xiang if (args.fsbno == NULLFSBLOCK) 7948237fbf5SDave Chinner return -EAGAIN; 7953937493cSGao Xiang 79630f712c9SDave Chinner ASSERT(args.len == args.minlen); 79730f712c9SDave Chinner 79830f712c9SDave Chinner /* 79930f712c9SDave Chinner * Stamp and write the inode buffers. 80030f712c9SDave Chinner * 80130f712c9SDave Chinner * Seed the new inode cluster with a random generation number. This 80230f712c9SDave Chinner * prevents short-term reuse of generation numbers if a chunk is 80330f712c9SDave Chinner * freed and then immediately reallocated. We use random numbers 80430f712c9SDave Chinner * rather than a linear progression to prevent the next generation 80530f712c9SDave Chinner * number from being easily guessable. 80630f712c9SDave Chinner */ 8077b13c515SDave Chinner error = xfs_ialloc_inode_init(args.mp, tp, NULL, newlen, pag->pag_agno, 808463958afSBrian Foster args.agbno, args.len, prandom_u32()); 80930f712c9SDave Chinner 81030f712c9SDave Chinner if (error) 81130f712c9SDave Chinner return error; 81230f712c9SDave Chinner /* 81330f712c9SDave Chinner * Convert the results. 81430f712c9SDave Chinner */ 81543004b2aSDarrick J. Wong newino = XFS_AGB_TO_AGINO(args.mp, args.agbno); 81656d1115cSBrian Foster 81756d1115cSBrian Foster if (xfs_inobt_issparse(~allocmask)) { 81856d1115cSBrian Foster /* 81956d1115cSBrian Foster * We've allocated a sparse chunk. Align the startino and mask. 82056d1115cSBrian Foster */ 82156d1115cSBrian Foster xfs_align_sparse_ino(args.mp, &newino, &allocmask); 82256d1115cSBrian Foster 82356d1115cSBrian Foster rec.ir_startino = newino; 82456d1115cSBrian Foster rec.ir_holemask = ~allocmask; 82556d1115cSBrian Foster rec.ir_count = newlen; 82656d1115cSBrian Foster rec.ir_freecount = newlen; 82756d1115cSBrian Foster rec.ir_free = XFS_INOBT_ALL_FREE; 82856d1115cSBrian Foster 82956d1115cSBrian Foster /* 83056d1115cSBrian Foster * Insert the sparse record into the inobt and allow for a merge 83156d1115cSBrian Foster * if necessary. If a merge does occur, rec is updated to the 83256d1115cSBrian Foster * merged record. 83356d1115cSBrian Foster */ 8347b13c515SDave Chinner error = xfs_inobt_insert_sprec(args.mp, tp, agbp, pag, 8357b13c515SDave Chinner XFS_BTNUM_INO, &rec, true); 83656d1115cSBrian Foster if (error == -EFSCORRUPTED) { 83756d1115cSBrian Foster xfs_alert(args.mp, 83856d1115cSBrian Foster "invalid sparse inode record: ino 0x%llx holemask 0x%x count %u", 8397b13c515SDave Chinner XFS_AGINO_TO_INO(args.mp, pag->pag_agno, 84056d1115cSBrian Foster rec.ir_startino), 84156d1115cSBrian Foster rec.ir_holemask, rec.ir_count); 84256d1115cSBrian Foster xfs_force_shutdown(args.mp, SHUTDOWN_CORRUPT_INCORE); 84356d1115cSBrian Foster } 84456d1115cSBrian Foster if (error) 84556d1115cSBrian Foster return error; 84656d1115cSBrian Foster 84756d1115cSBrian Foster /* 84856d1115cSBrian Foster * We can't merge the part we've just allocated as for the inobt 84956d1115cSBrian Foster * due to finobt semantics. The original record may or may not 85056d1115cSBrian Foster * exist independent of whether physical inodes exist in this 85156d1115cSBrian Foster * sparse chunk. 85256d1115cSBrian Foster * 85356d1115cSBrian Foster * We must update the finobt record based on the inobt record. 85456d1115cSBrian Foster * rec contains the fully merged and up to date inobt record 85556d1115cSBrian Foster * from the previous call. Set merge false to replace any 85656d1115cSBrian Foster * existing record with this one. 85756d1115cSBrian Foster */ 858ebd9027dSDave Chinner if (xfs_has_finobt(args.mp)) { 8597b13c515SDave Chinner error = xfs_inobt_insert_sprec(args.mp, tp, agbp, pag, 8607b13c515SDave Chinner XFS_BTNUM_FINO, &rec, false); 86156d1115cSBrian Foster if (error) 86256d1115cSBrian Foster return error; 86356d1115cSBrian Foster } 86456d1115cSBrian Foster } else { 86556d1115cSBrian Foster /* full chunk - insert new records to both btrees */ 8667b13c515SDave Chinner error = xfs_inobt_insert(args.mp, tp, agbp, pag, newino, newlen, 86756d1115cSBrian Foster XFS_BTNUM_INO); 86856d1115cSBrian Foster if (error) 86956d1115cSBrian Foster return error; 87056d1115cSBrian Foster 871ebd9027dSDave Chinner if (xfs_has_finobt(args.mp)) { 8727b13c515SDave Chinner error = xfs_inobt_insert(args.mp, tp, agbp, pag, newino, 87356d1115cSBrian Foster newlen, XFS_BTNUM_FINO); 87456d1115cSBrian Foster if (error) 87556d1115cSBrian Foster return error; 87656d1115cSBrian Foster } 87756d1115cSBrian Foster } 87856d1115cSBrian Foster 87956d1115cSBrian Foster /* 88056d1115cSBrian Foster * Update AGI counts and newino. 88156d1115cSBrian Foster */ 88230f712c9SDave Chinner be32_add_cpu(&agi->agi_count, newlen); 88330f712c9SDave Chinner be32_add_cpu(&agi->agi_freecount, newlen); 88430f712c9SDave Chinner pag->pagi_freecount += newlen; 88589e9b5c0SDarrick J. Wong pag->pagi_count += newlen; 88630f712c9SDave Chinner agi->agi_newino = cpu_to_be32(newino); 88730f712c9SDave Chinner 88830f712c9SDave Chinner /* 88930f712c9SDave Chinner * Log allocation group header fields 89030f712c9SDave Chinner */ 89130f712c9SDave Chinner xfs_ialloc_log_agi(tp, agbp, 89230f712c9SDave Chinner XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO); 89330f712c9SDave Chinner /* 89430f712c9SDave Chinner * Modify/log superblock values for inode count and inode free count. 89530f712c9SDave Chinner */ 89630f712c9SDave Chinner xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen); 89730f712c9SDave Chinner xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen); 89830f712c9SDave Chinner return 0; 89930f712c9SDave Chinner } 90030f712c9SDave Chinner 90130f712c9SDave Chinner /* 90230f712c9SDave Chinner * Try to retrieve the next record to the left/right from the current one. 90330f712c9SDave Chinner */ 90430f712c9SDave Chinner STATIC int 90530f712c9SDave Chinner xfs_ialloc_next_rec( 90630f712c9SDave Chinner struct xfs_btree_cur *cur, 90730f712c9SDave Chinner xfs_inobt_rec_incore_t *rec, 90830f712c9SDave Chinner int *done, 90930f712c9SDave Chinner int left) 91030f712c9SDave Chinner { 91130f712c9SDave Chinner int error; 91230f712c9SDave Chinner int i; 91330f712c9SDave Chinner 91430f712c9SDave Chinner if (left) 91530f712c9SDave Chinner error = xfs_btree_decrement(cur, 0, &i); 91630f712c9SDave Chinner else 91730f712c9SDave Chinner error = xfs_btree_increment(cur, 0, &i); 91830f712c9SDave Chinner 91930f712c9SDave Chinner if (error) 92030f712c9SDave Chinner return error; 92130f712c9SDave Chinner *done = !i; 92230f712c9SDave Chinner if (i) { 92330f712c9SDave Chinner error = xfs_inobt_get_rec(cur, rec, &i); 92430f712c9SDave Chinner if (error) 92530f712c9SDave Chinner return error; 926f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) 927f9e03706SDarrick J. Wong return -EFSCORRUPTED; 92830f712c9SDave Chinner } 92930f712c9SDave Chinner 93030f712c9SDave Chinner return 0; 93130f712c9SDave Chinner } 93230f712c9SDave Chinner 93330f712c9SDave Chinner STATIC int 93430f712c9SDave Chinner xfs_ialloc_get_rec( 93530f712c9SDave Chinner struct xfs_btree_cur *cur, 93630f712c9SDave Chinner xfs_agino_t agino, 93730f712c9SDave Chinner xfs_inobt_rec_incore_t *rec, 93830f712c9SDave Chinner int *done) 93930f712c9SDave Chinner { 94030f712c9SDave Chinner int error; 94130f712c9SDave Chinner int i; 94230f712c9SDave Chinner 94330f712c9SDave Chinner error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_EQ, &i); 94430f712c9SDave Chinner if (error) 94530f712c9SDave Chinner return error; 94630f712c9SDave Chinner *done = !i; 94730f712c9SDave Chinner if (i) { 94830f712c9SDave Chinner error = xfs_inobt_get_rec(cur, rec, &i); 94930f712c9SDave Chinner if (error) 95030f712c9SDave Chinner return error; 951f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) 952f9e03706SDarrick J. Wong return -EFSCORRUPTED; 95330f712c9SDave Chinner } 95430f712c9SDave Chinner 95530f712c9SDave Chinner return 0; 95630f712c9SDave Chinner } 95730f712c9SDave Chinner 95830f712c9SDave Chinner /* 95926dd5217SBrian Foster * Return the offset of the first free inode in the record. If the inode chunk 96026dd5217SBrian Foster * is sparsely allocated, we convert the record holemask to inode granularity 96126dd5217SBrian Foster * and mask off the unallocated regions from the inode free mask. 962d4cc540bSBrian Foster */ 963d4cc540bSBrian Foster STATIC int 964d4cc540bSBrian Foster xfs_inobt_first_free_inode( 965d4cc540bSBrian Foster struct xfs_inobt_rec_incore *rec) 966d4cc540bSBrian Foster { 96726dd5217SBrian Foster xfs_inofree_t realfree; 96826dd5217SBrian Foster 96926dd5217SBrian Foster /* if there are no holes, return the first available offset */ 97026dd5217SBrian Foster if (!xfs_inobt_issparse(rec->ir_holemask)) 971d4cc540bSBrian Foster return xfs_lowbit64(rec->ir_free); 97226dd5217SBrian Foster 97326dd5217SBrian Foster realfree = xfs_inobt_irec_to_allocmask(rec); 97426dd5217SBrian Foster realfree &= rec->ir_free; 97526dd5217SBrian Foster 97626dd5217SBrian Foster return xfs_lowbit64(realfree); 977d4cc540bSBrian Foster } 978d4cc540bSBrian Foster 979d4cc540bSBrian Foster /* 98030f712c9SDave Chinner * Allocate an inode using the inobt-only algorithm. 98130f712c9SDave Chinner */ 98230f712c9SDave Chinner STATIC int 98330f712c9SDave Chinner xfs_dialloc_ag_inobt( 98430f712c9SDave Chinner struct xfs_trans *tp, 98530f712c9SDave Chinner struct xfs_buf *agbp, 9867b13c515SDave Chinner struct xfs_perag *pag, 98730f712c9SDave Chinner xfs_ino_t parent, 98830f712c9SDave Chinner xfs_ino_t *inop) 98930f712c9SDave Chinner { 99030f712c9SDave Chinner struct xfs_mount *mp = tp->t_mountp; 991370c782bSChristoph Hellwig struct xfs_agi *agi = agbp->b_addr; 99230f712c9SDave Chinner xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent); 99330f712c9SDave Chinner xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent); 99430f712c9SDave Chinner struct xfs_btree_cur *cur, *tcur; 99530f712c9SDave Chinner struct xfs_inobt_rec_incore rec, trec; 99630f712c9SDave Chinner xfs_ino_t ino; 99730f712c9SDave Chinner int error; 99830f712c9SDave Chinner int offset; 99930f712c9SDave Chinner int i, j; 10002d32311cSCarlos Maiolino int searchdistance = 10; 100130f712c9SDave Chinner 100230f712c9SDave Chinner ASSERT(pag->pagi_init); 100330f712c9SDave Chinner ASSERT(pag->pagi_inodeok); 100430f712c9SDave Chinner ASSERT(pag->pagi_freecount > 0); 100530f712c9SDave Chinner 100630f712c9SDave Chinner restart_pagno: 10077b13c515SDave Chinner cur = xfs_inobt_init_cursor(mp, tp, agbp, pag, XFS_BTNUM_INO); 100830f712c9SDave Chinner /* 100930f712c9SDave Chinner * If pagino is 0 (this is the root inode allocation) use newino. 101030f712c9SDave Chinner * This must work because we've just allocated some. 101130f712c9SDave Chinner */ 101230f712c9SDave Chinner if (!pagino) 101330f712c9SDave Chinner pagino = be32_to_cpu(agi->agi_newino); 101430f712c9SDave Chinner 10159ba0889eSDave Chinner error = xfs_check_agi_freecount(cur); 101630f712c9SDave Chinner if (error) 101730f712c9SDave Chinner goto error0; 101830f712c9SDave Chinner 101930f712c9SDave Chinner /* 102030f712c9SDave Chinner * If in the same AG as the parent, try to get near the parent. 102130f712c9SDave Chinner */ 10227b13c515SDave Chinner if (pagno == pag->pag_agno) { 102330f712c9SDave Chinner int doneleft; /* done, to the left */ 102430f712c9SDave Chinner int doneright; /* done, to the right */ 102530f712c9SDave Chinner 102630f712c9SDave Chinner error = xfs_inobt_lookup(cur, pagino, XFS_LOOKUP_LE, &i); 102730f712c9SDave Chinner if (error) 102830f712c9SDave Chinner goto error0; 1029f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(mp, i != 1)) { 1030f9e03706SDarrick J. Wong error = -EFSCORRUPTED; 1031f9e03706SDarrick J. Wong goto error0; 1032f9e03706SDarrick J. Wong } 103330f712c9SDave Chinner 103430f712c9SDave Chinner error = xfs_inobt_get_rec(cur, &rec, &j); 103530f712c9SDave Chinner if (error) 103630f712c9SDave Chinner goto error0; 1037f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(mp, j != 1)) { 1038f9e03706SDarrick J. Wong error = -EFSCORRUPTED; 1039f9e03706SDarrick J. Wong goto error0; 1040f9e03706SDarrick J. Wong } 104130f712c9SDave Chinner 104230f712c9SDave Chinner if (rec.ir_freecount > 0) { 104330f712c9SDave Chinner /* 104430f712c9SDave Chinner * Found a free inode in the same chunk 104530f712c9SDave Chinner * as the parent, done. 104630f712c9SDave Chinner */ 104730f712c9SDave Chinner goto alloc_inode; 104830f712c9SDave Chinner } 104930f712c9SDave Chinner 105030f712c9SDave Chinner 105130f712c9SDave Chinner /* 105230f712c9SDave Chinner * In the same AG as parent, but parent's chunk is full. 105330f712c9SDave Chinner */ 105430f712c9SDave Chinner 105530f712c9SDave Chinner /* duplicate the cursor, search left & right simultaneously */ 105630f712c9SDave Chinner error = xfs_btree_dup_cursor(cur, &tcur); 105730f712c9SDave Chinner if (error) 105830f712c9SDave Chinner goto error0; 105930f712c9SDave Chinner 106030f712c9SDave Chinner /* 106130f712c9SDave Chinner * Skip to last blocks looked up if same parent inode. 106230f712c9SDave Chinner */ 106330f712c9SDave Chinner if (pagino != NULLAGINO && 106430f712c9SDave Chinner pag->pagl_pagino == pagino && 106530f712c9SDave Chinner pag->pagl_leftrec != NULLAGINO && 106630f712c9SDave Chinner pag->pagl_rightrec != NULLAGINO) { 106730f712c9SDave Chinner error = xfs_ialloc_get_rec(tcur, pag->pagl_leftrec, 106830f712c9SDave Chinner &trec, &doneleft); 106930f712c9SDave Chinner if (error) 107030f712c9SDave Chinner goto error1; 107130f712c9SDave Chinner 107230f712c9SDave Chinner error = xfs_ialloc_get_rec(cur, pag->pagl_rightrec, 107330f712c9SDave Chinner &rec, &doneright); 107430f712c9SDave Chinner if (error) 107530f712c9SDave Chinner goto error1; 107630f712c9SDave Chinner } else { 107730f712c9SDave Chinner /* search left with tcur, back up 1 record */ 107830f712c9SDave Chinner error = xfs_ialloc_next_rec(tcur, &trec, &doneleft, 1); 107930f712c9SDave Chinner if (error) 108030f712c9SDave Chinner goto error1; 108130f712c9SDave Chinner 108230f712c9SDave Chinner /* search right with cur, go forward 1 record. */ 108330f712c9SDave Chinner error = xfs_ialloc_next_rec(cur, &rec, &doneright, 0); 108430f712c9SDave Chinner if (error) 108530f712c9SDave Chinner goto error1; 108630f712c9SDave Chinner } 108730f712c9SDave Chinner 108830f712c9SDave Chinner /* 108930f712c9SDave Chinner * Loop until we find an inode chunk with a free inode. 109030f712c9SDave Chinner */ 10912d32311cSCarlos Maiolino while (--searchdistance > 0 && (!doneleft || !doneright)) { 109230f712c9SDave Chinner int useleft; /* using left inode chunk this time */ 109330f712c9SDave Chinner 109430f712c9SDave Chinner /* figure out the closer block if both are valid. */ 109530f712c9SDave Chinner if (!doneleft && !doneright) { 109630f712c9SDave Chinner useleft = pagino - 109730f712c9SDave Chinner (trec.ir_startino + XFS_INODES_PER_CHUNK - 1) < 109830f712c9SDave Chinner rec.ir_startino - pagino; 109930f712c9SDave Chinner } else { 110030f712c9SDave Chinner useleft = !doneleft; 110130f712c9SDave Chinner } 110230f712c9SDave Chinner 110330f712c9SDave Chinner /* free inodes to the left? */ 110430f712c9SDave Chinner if (useleft && trec.ir_freecount) { 110530f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); 110630f712c9SDave Chinner cur = tcur; 110730f712c9SDave Chinner 110830f712c9SDave Chinner pag->pagl_leftrec = trec.ir_startino; 110930f712c9SDave Chinner pag->pagl_rightrec = rec.ir_startino; 111030f712c9SDave Chinner pag->pagl_pagino = pagino; 1111c44245b3SOmar Sandoval rec = trec; 111230f712c9SDave Chinner goto alloc_inode; 111330f712c9SDave Chinner } 111430f712c9SDave Chinner 111530f712c9SDave Chinner /* free inodes to the right? */ 111630f712c9SDave Chinner if (!useleft && rec.ir_freecount) { 111730f712c9SDave Chinner xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); 111830f712c9SDave Chinner 111930f712c9SDave Chinner pag->pagl_leftrec = trec.ir_startino; 112030f712c9SDave Chinner pag->pagl_rightrec = rec.ir_startino; 112130f712c9SDave Chinner pag->pagl_pagino = pagino; 112230f712c9SDave Chinner goto alloc_inode; 112330f712c9SDave Chinner } 112430f712c9SDave Chinner 112530f712c9SDave Chinner /* get next record to check */ 112630f712c9SDave Chinner if (useleft) { 112730f712c9SDave Chinner error = xfs_ialloc_next_rec(tcur, &trec, 112830f712c9SDave Chinner &doneleft, 1); 112930f712c9SDave Chinner } else { 113030f712c9SDave Chinner error = xfs_ialloc_next_rec(cur, &rec, 113130f712c9SDave Chinner &doneright, 0); 113230f712c9SDave Chinner } 113330f712c9SDave Chinner if (error) 113430f712c9SDave Chinner goto error1; 113530f712c9SDave Chinner } 113630f712c9SDave Chinner 11372d32311cSCarlos Maiolino if (searchdistance <= 0) { 11382d32311cSCarlos Maiolino /* 11392d32311cSCarlos Maiolino * Not in range - save last search 11402d32311cSCarlos Maiolino * location and allocate a new inode 11412d32311cSCarlos Maiolino */ 11422d32311cSCarlos Maiolino xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); 11432d32311cSCarlos Maiolino pag->pagl_leftrec = trec.ir_startino; 11442d32311cSCarlos Maiolino pag->pagl_rightrec = rec.ir_startino; 11452d32311cSCarlos Maiolino pag->pagl_pagino = pagino; 11462d32311cSCarlos Maiolino 11472d32311cSCarlos Maiolino } else { 114830f712c9SDave Chinner /* 114930f712c9SDave Chinner * We've reached the end of the btree. because 115030f712c9SDave Chinner * we are only searching a small chunk of the 115130f712c9SDave Chinner * btree each search, there is obviously free 115230f712c9SDave Chinner * inodes closer to the parent inode than we 115330f712c9SDave Chinner * are now. restart the search again. 115430f712c9SDave Chinner */ 115530f712c9SDave Chinner pag->pagl_pagino = NULLAGINO; 115630f712c9SDave Chinner pag->pagl_leftrec = NULLAGINO; 115730f712c9SDave Chinner pag->pagl_rightrec = NULLAGINO; 115830f712c9SDave Chinner xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); 115930f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); 116030f712c9SDave Chinner goto restart_pagno; 116130f712c9SDave Chinner } 11622d32311cSCarlos Maiolino } 116330f712c9SDave Chinner 116430f712c9SDave Chinner /* 116530f712c9SDave Chinner * In a different AG from the parent. 116630f712c9SDave Chinner * See if the most recently allocated block has any free. 116730f712c9SDave Chinner */ 116830f712c9SDave Chinner if (agi->agi_newino != cpu_to_be32(NULLAGINO)) { 116930f712c9SDave Chinner error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino), 117030f712c9SDave Chinner XFS_LOOKUP_EQ, &i); 117130f712c9SDave Chinner if (error) 117230f712c9SDave Chinner goto error0; 117330f712c9SDave Chinner 117430f712c9SDave Chinner if (i == 1) { 117530f712c9SDave Chinner error = xfs_inobt_get_rec(cur, &rec, &j); 117630f712c9SDave Chinner if (error) 117730f712c9SDave Chinner goto error0; 117830f712c9SDave Chinner 117930f712c9SDave Chinner if (j == 1 && rec.ir_freecount > 0) { 118030f712c9SDave Chinner /* 118130f712c9SDave Chinner * The last chunk allocated in the group 118230f712c9SDave Chinner * still has a free inode. 118330f712c9SDave Chinner */ 118430f712c9SDave Chinner goto alloc_inode; 118530f712c9SDave Chinner } 118630f712c9SDave Chinner } 118730f712c9SDave Chinner } 118830f712c9SDave Chinner 118930f712c9SDave Chinner /* 119030f712c9SDave Chinner * None left in the last group, search the whole AG 119130f712c9SDave Chinner */ 119230f712c9SDave Chinner error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i); 119330f712c9SDave Chinner if (error) 119430f712c9SDave Chinner goto error0; 1195f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(mp, i != 1)) { 1196f9e03706SDarrick J. Wong error = -EFSCORRUPTED; 1197f9e03706SDarrick J. Wong goto error0; 1198f9e03706SDarrick J. Wong } 119930f712c9SDave Chinner 120030f712c9SDave Chinner for (;;) { 120130f712c9SDave Chinner error = xfs_inobt_get_rec(cur, &rec, &i); 120230f712c9SDave Chinner if (error) 120330f712c9SDave Chinner goto error0; 1204f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(mp, i != 1)) { 1205f9e03706SDarrick J. Wong error = -EFSCORRUPTED; 1206f9e03706SDarrick J. Wong goto error0; 1207f9e03706SDarrick J. Wong } 120830f712c9SDave Chinner if (rec.ir_freecount > 0) 120930f712c9SDave Chinner break; 121030f712c9SDave Chinner error = xfs_btree_increment(cur, 0, &i); 121130f712c9SDave Chinner if (error) 121230f712c9SDave Chinner goto error0; 1213f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(mp, i != 1)) { 1214f9e03706SDarrick J. Wong error = -EFSCORRUPTED; 1215f9e03706SDarrick J. Wong goto error0; 1216f9e03706SDarrick J. Wong } 121730f712c9SDave Chinner } 121830f712c9SDave Chinner 121930f712c9SDave Chinner alloc_inode: 1220d4cc540bSBrian Foster offset = xfs_inobt_first_free_inode(&rec); 122130f712c9SDave Chinner ASSERT(offset >= 0); 122230f712c9SDave Chinner ASSERT(offset < XFS_INODES_PER_CHUNK); 122330f712c9SDave Chinner ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) % 122430f712c9SDave Chinner XFS_INODES_PER_CHUNK) == 0); 12257b13c515SDave Chinner ino = XFS_AGINO_TO_INO(mp, pag->pag_agno, rec.ir_startino + offset); 122630f712c9SDave Chinner rec.ir_free &= ~XFS_INOBT_MASK(offset); 122730f712c9SDave Chinner rec.ir_freecount--; 122830f712c9SDave Chinner error = xfs_inobt_update(cur, &rec); 122930f712c9SDave Chinner if (error) 123030f712c9SDave Chinner goto error0; 123130f712c9SDave Chinner be32_add_cpu(&agi->agi_freecount, -1); 123230f712c9SDave Chinner xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT); 123330f712c9SDave Chinner pag->pagi_freecount--; 123430f712c9SDave Chinner 12359ba0889eSDave Chinner error = xfs_check_agi_freecount(cur); 123630f712c9SDave Chinner if (error) 123730f712c9SDave Chinner goto error0; 123830f712c9SDave Chinner 123930f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); 124030f712c9SDave Chinner xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1); 124130f712c9SDave Chinner *inop = ino; 124230f712c9SDave Chinner return 0; 124330f712c9SDave Chinner error1: 124430f712c9SDave Chinner xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR); 124530f712c9SDave Chinner error0: 124630f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); 124730f712c9SDave Chinner return error; 124830f712c9SDave Chinner } 124930f712c9SDave Chinner 125030f712c9SDave Chinner /* 125130f712c9SDave Chinner * Use the free inode btree to allocate an inode based on distance from the 125230f712c9SDave Chinner * parent. Note that the provided cursor may be deleted and replaced. 125330f712c9SDave Chinner */ 125430f712c9SDave Chinner STATIC int 125530f712c9SDave Chinner xfs_dialloc_ag_finobt_near( 125630f712c9SDave Chinner xfs_agino_t pagino, 125730f712c9SDave Chinner struct xfs_btree_cur **ocur, 125830f712c9SDave Chinner struct xfs_inobt_rec_incore *rec) 125930f712c9SDave Chinner { 126030f712c9SDave Chinner struct xfs_btree_cur *lcur = *ocur; /* left search cursor */ 126130f712c9SDave Chinner struct xfs_btree_cur *rcur; /* right search cursor */ 126230f712c9SDave Chinner struct xfs_inobt_rec_incore rrec; 126330f712c9SDave Chinner int error; 126430f712c9SDave Chinner int i, j; 126530f712c9SDave Chinner 126630f712c9SDave Chinner error = xfs_inobt_lookup(lcur, pagino, XFS_LOOKUP_LE, &i); 126730f712c9SDave Chinner if (error) 126830f712c9SDave Chinner return error; 126930f712c9SDave Chinner 127030f712c9SDave Chinner if (i == 1) { 127130f712c9SDave Chinner error = xfs_inobt_get_rec(lcur, rec, &i); 127230f712c9SDave Chinner if (error) 127330f712c9SDave Chinner return error; 1274f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(lcur->bc_mp, i != 1)) 1275f9e03706SDarrick J. Wong return -EFSCORRUPTED; 127630f712c9SDave Chinner 127730f712c9SDave Chinner /* 127830f712c9SDave Chinner * See if we've landed in the parent inode record. The finobt 127930f712c9SDave Chinner * only tracks chunks with at least one free inode, so record 128030f712c9SDave Chinner * existence is enough. 128130f712c9SDave Chinner */ 128230f712c9SDave Chinner if (pagino >= rec->ir_startino && 128330f712c9SDave Chinner pagino < (rec->ir_startino + XFS_INODES_PER_CHUNK)) 128430f712c9SDave Chinner return 0; 128530f712c9SDave Chinner } 128630f712c9SDave Chinner 128730f712c9SDave Chinner error = xfs_btree_dup_cursor(lcur, &rcur); 128830f712c9SDave Chinner if (error) 128930f712c9SDave Chinner return error; 129030f712c9SDave Chinner 129130f712c9SDave Chinner error = xfs_inobt_lookup(rcur, pagino, XFS_LOOKUP_GE, &j); 129230f712c9SDave Chinner if (error) 129330f712c9SDave Chinner goto error_rcur; 129430f712c9SDave Chinner if (j == 1) { 129530f712c9SDave Chinner error = xfs_inobt_get_rec(rcur, &rrec, &j); 129630f712c9SDave Chinner if (error) 129730f712c9SDave Chinner goto error_rcur; 1298f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(lcur->bc_mp, j != 1)) { 1299f9e03706SDarrick J. Wong error = -EFSCORRUPTED; 1300f9e03706SDarrick J. Wong goto error_rcur; 1301f9e03706SDarrick J. Wong } 130230f712c9SDave Chinner } 130330f712c9SDave Chinner 1304f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(lcur->bc_mp, i != 1 && j != 1)) { 1305f9e03706SDarrick J. Wong error = -EFSCORRUPTED; 1306f9e03706SDarrick J. Wong goto error_rcur; 1307f9e03706SDarrick J. Wong } 130830f712c9SDave Chinner if (i == 1 && j == 1) { 130930f712c9SDave Chinner /* 131030f712c9SDave Chinner * Both the left and right records are valid. Choose the closer 131130f712c9SDave Chinner * inode chunk to the target. 131230f712c9SDave Chinner */ 131330f712c9SDave Chinner if ((pagino - rec->ir_startino + XFS_INODES_PER_CHUNK - 1) > 131430f712c9SDave Chinner (rrec.ir_startino - pagino)) { 131530f712c9SDave Chinner *rec = rrec; 131630f712c9SDave Chinner xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR); 131730f712c9SDave Chinner *ocur = rcur; 131830f712c9SDave Chinner } else { 131930f712c9SDave Chinner xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR); 132030f712c9SDave Chinner } 132130f712c9SDave Chinner } else if (j == 1) { 132230f712c9SDave Chinner /* only the right record is valid */ 132330f712c9SDave Chinner *rec = rrec; 132430f712c9SDave Chinner xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR); 132530f712c9SDave Chinner *ocur = rcur; 132630f712c9SDave Chinner } else if (i == 1) { 132730f712c9SDave Chinner /* only the left record is valid */ 132830f712c9SDave Chinner xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR); 132930f712c9SDave Chinner } 133030f712c9SDave Chinner 133130f712c9SDave Chinner return 0; 133230f712c9SDave Chinner 133330f712c9SDave Chinner error_rcur: 133430f712c9SDave Chinner xfs_btree_del_cursor(rcur, XFS_BTREE_ERROR); 133530f712c9SDave Chinner return error; 133630f712c9SDave Chinner } 133730f712c9SDave Chinner 133830f712c9SDave Chinner /* 133930f712c9SDave Chinner * Use the free inode btree to find a free inode based on a newino hint. If 134030f712c9SDave Chinner * the hint is NULL, find the first free inode in the AG. 134130f712c9SDave Chinner */ 134230f712c9SDave Chinner STATIC int 134330f712c9SDave Chinner xfs_dialloc_ag_finobt_newino( 134430f712c9SDave Chinner struct xfs_agi *agi, 134530f712c9SDave Chinner struct xfs_btree_cur *cur, 134630f712c9SDave Chinner struct xfs_inobt_rec_incore *rec) 134730f712c9SDave Chinner { 134830f712c9SDave Chinner int error; 134930f712c9SDave Chinner int i; 135030f712c9SDave Chinner 135130f712c9SDave Chinner if (agi->agi_newino != cpu_to_be32(NULLAGINO)) { 1352e68ed775SDave Chinner error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino), 1353e68ed775SDave Chinner XFS_LOOKUP_EQ, &i); 135430f712c9SDave Chinner if (error) 135530f712c9SDave Chinner return error; 135630f712c9SDave Chinner if (i == 1) { 135730f712c9SDave Chinner error = xfs_inobt_get_rec(cur, rec, &i); 135830f712c9SDave Chinner if (error) 135930f712c9SDave Chinner return error; 1360f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) 1361f9e03706SDarrick J. Wong return -EFSCORRUPTED; 136230f712c9SDave Chinner return 0; 136330f712c9SDave Chinner } 136430f712c9SDave Chinner } 136530f712c9SDave Chinner 136630f712c9SDave Chinner /* 136730f712c9SDave Chinner * Find the first inode available in the AG. 136830f712c9SDave Chinner */ 136930f712c9SDave Chinner error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i); 137030f712c9SDave Chinner if (error) 137130f712c9SDave Chinner return error; 1372f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) 1373f9e03706SDarrick J. Wong return -EFSCORRUPTED; 137430f712c9SDave Chinner 137530f712c9SDave Chinner error = xfs_inobt_get_rec(cur, rec, &i); 137630f712c9SDave Chinner if (error) 137730f712c9SDave Chinner return error; 1378f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) 1379f9e03706SDarrick J. Wong return -EFSCORRUPTED; 138030f712c9SDave Chinner 138130f712c9SDave Chinner return 0; 138230f712c9SDave Chinner } 138330f712c9SDave Chinner 138430f712c9SDave Chinner /* 138530f712c9SDave Chinner * Update the inobt based on a modification made to the finobt. Also ensure that 138630f712c9SDave Chinner * the records from both trees are equivalent post-modification. 138730f712c9SDave Chinner */ 138830f712c9SDave Chinner STATIC int 138930f712c9SDave Chinner xfs_dialloc_ag_update_inobt( 139030f712c9SDave Chinner struct xfs_btree_cur *cur, /* inobt cursor */ 139130f712c9SDave Chinner struct xfs_inobt_rec_incore *frec, /* finobt record */ 139230f712c9SDave Chinner int offset) /* inode offset */ 139330f712c9SDave Chinner { 139430f712c9SDave Chinner struct xfs_inobt_rec_incore rec; 139530f712c9SDave Chinner int error; 139630f712c9SDave Chinner int i; 139730f712c9SDave Chinner 139830f712c9SDave Chinner error = xfs_inobt_lookup(cur, frec->ir_startino, XFS_LOOKUP_EQ, &i); 139930f712c9SDave Chinner if (error) 140030f712c9SDave Chinner return error; 1401f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) 1402f9e03706SDarrick J. Wong return -EFSCORRUPTED; 140330f712c9SDave Chinner 140430f712c9SDave Chinner error = xfs_inobt_get_rec(cur, &rec, &i); 140530f712c9SDave Chinner if (error) 140630f712c9SDave Chinner return error; 1407f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) 1408f9e03706SDarrick J. Wong return -EFSCORRUPTED; 140930f712c9SDave Chinner ASSERT((XFS_AGINO_TO_OFFSET(cur->bc_mp, rec.ir_startino) % 141030f712c9SDave Chinner XFS_INODES_PER_CHUNK) == 0); 141130f712c9SDave Chinner 141230f712c9SDave Chinner rec.ir_free &= ~XFS_INOBT_MASK(offset); 141330f712c9SDave Chinner rec.ir_freecount--; 141430f712c9SDave Chinner 1415f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(cur->bc_mp, 1416f9e03706SDarrick J. Wong rec.ir_free != frec->ir_free || 1417f9e03706SDarrick J. Wong rec.ir_freecount != frec->ir_freecount)) 1418f9e03706SDarrick J. Wong return -EFSCORRUPTED; 141930f712c9SDave Chinner 1420b72091f2Skbuild test robot return xfs_inobt_update(cur, &rec); 142130f712c9SDave Chinner } 142230f712c9SDave Chinner 142330f712c9SDave Chinner /* 142430f712c9SDave Chinner * Allocate an inode using the free inode btree, if available. Otherwise, fall 142530f712c9SDave Chinner * back to the inobt search algorithm. 142630f712c9SDave Chinner * 142730f712c9SDave Chinner * The caller selected an AG for us, and made sure that free inodes are 142830f712c9SDave Chinner * available. 142930f712c9SDave Chinner */ 1430b652afd9SDave Chinner static int 143130f712c9SDave Chinner xfs_dialloc_ag( 143230f712c9SDave Chinner struct xfs_trans *tp, 143330f712c9SDave Chinner struct xfs_buf *agbp, 1434309161f6SDave Chinner struct xfs_perag *pag, 143530f712c9SDave Chinner xfs_ino_t parent, 143630f712c9SDave Chinner xfs_ino_t *inop) 143730f712c9SDave Chinner { 143830f712c9SDave Chinner struct xfs_mount *mp = tp->t_mountp; 1439370c782bSChristoph Hellwig struct xfs_agi *agi = agbp->b_addr; 144030f712c9SDave Chinner xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent); 144130f712c9SDave Chinner xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent); 144230f712c9SDave Chinner struct xfs_btree_cur *cur; /* finobt cursor */ 144330f712c9SDave Chinner struct xfs_btree_cur *icur; /* inobt cursor */ 144430f712c9SDave Chinner struct xfs_inobt_rec_incore rec; 144530f712c9SDave Chinner xfs_ino_t ino; 144630f712c9SDave Chinner int error; 144730f712c9SDave Chinner int offset; 144830f712c9SDave Chinner int i; 144930f712c9SDave Chinner 1450ebd9027dSDave Chinner if (!xfs_has_finobt(mp)) 14517b13c515SDave Chinner return xfs_dialloc_ag_inobt(tp, agbp, pag, parent, inop); 145230f712c9SDave Chinner 145330f712c9SDave Chinner /* 145430f712c9SDave Chinner * If pagino is 0 (this is the root inode allocation) use newino. 145530f712c9SDave Chinner * This must work because we've just allocated some. 145630f712c9SDave Chinner */ 145730f712c9SDave Chinner if (!pagino) 145830f712c9SDave Chinner pagino = be32_to_cpu(agi->agi_newino); 145930f712c9SDave Chinner 14607b13c515SDave Chinner cur = xfs_inobt_init_cursor(mp, tp, agbp, pag, XFS_BTNUM_FINO); 146130f712c9SDave Chinner 14629ba0889eSDave Chinner error = xfs_check_agi_freecount(cur); 146330f712c9SDave Chinner if (error) 146430f712c9SDave Chinner goto error_cur; 146530f712c9SDave Chinner 146630f712c9SDave Chinner /* 146730f712c9SDave Chinner * The search algorithm depends on whether we're in the same AG as the 146830f712c9SDave Chinner * parent. If so, find the closest available inode to the parent. If 146930f712c9SDave Chinner * not, consider the agi hint or find the first free inode in the AG. 147030f712c9SDave Chinner */ 14717b13c515SDave Chinner if (pag->pag_agno == pagno) 147230f712c9SDave Chinner error = xfs_dialloc_ag_finobt_near(pagino, &cur, &rec); 147330f712c9SDave Chinner else 147430f712c9SDave Chinner error = xfs_dialloc_ag_finobt_newino(agi, cur, &rec); 147530f712c9SDave Chinner if (error) 147630f712c9SDave Chinner goto error_cur; 147730f712c9SDave Chinner 1478d4cc540bSBrian Foster offset = xfs_inobt_first_free_inode(&rec); 147930f712c9SDave Chinner ASSERT(offset >= 0); 148030f712c9SDave Chinner ASSERT(offset < XFS_INODES_PER_CHUNK); 148130f712c9SDave Chinner ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) % 148230f712c9SDave Chinner XFS_INODES_PER_CHUNK) == 0); 14837b13c515SDave Chinner ino = XFS_AGINO_TO_INO(mp, pag->pag_agno, rec.ir_startino + offset); 148430f712c9SDave Chinner 148530f712c9SDave Chinner /* 148630f712c9SDave Chinner * Modify or remove the finobt record. 148730f712c9SDave Chinner */ 148830f712c9SDave Chinner rec.ir_free &= ~XFS_INOBT_MASK(offset); 148930f712c9SDave Chinner rec.ir_freecount--; 149030f712c9SDave Chinner if (rec.ir_freecount) 149130f712c9SDave Chinner error = xfs_inobt_update(cur, &rec); 149230f712c9SDave Chinner else 149330f712c9SDave Chinner error = xfs_btree_delete(cur, &i); 149430f712c9SDave Chinner if (error) 149530f712c9SDave Chinner goto error_cur; 149630f712c9SDave Chinner 149730f712c9SDave Chinner /* 149830f712c9SDave Chinner * The finobt has now been updated appropriately. We haven't updated the 149930f712c9SDave Chinner * agi and superblock yet, so we can create an inobt cursor and validate 150030f712c9SDave Chinner * the original freecount. If all is well, make the equivalent update to 150130f712c9SDave Chinner * the inobt using the finobt record and offset information. 150230f712c9SDave Chinner */ 15037b13c515SDave Chinner icur = xfs_inobt_init_cursor(mp, tp, agbp, pag, XFS_BTNUM_INO); 150430f712c9SDave Chinner 15059ba0889eSDave Chinner error = xfs_check_agi_freecount(icur); 150630f712c9SDave Chinner if (error) 150730f712c9SDave Chinner goto error_icur; 150830f712c9SDave Chinner 150930f712c9SDave Chinner error = xfs_dialloc_ag_update_inobt(icur, &rec, offset); 151030f712c9SDave Chinner if (error) 151130f712c9SDave Chinner goto error_icur; 151230f712c9SDave Chinner 151330f712c9SDave Chinner /* 151430f712c9SDave Chinner * Both trees have now been updated. We must update the perag and 151530f712c9SDave Chinner * superblock before we can check the freecount for each btree. 151630f712c9SDave Chinner */ 151730f712c9SDave Chinner be32_add_cpu(&agi->agi_freecount, -1); 151830f712c9SDave Chinner xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT); 15197b13c515SDave Chinner pag->pagi_freecount--; 152030f712c9SDave Chinner 152130f712c9SDave Chinner xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1); 152230f712c9SDave Chinner 15239ba0889eSDave Chinner error = xfs_check_agi_freecount(icur); 152430f712c9SDave Chinner if (error) 152530f712c9SDave Chinner goto error_icur; 15269ba0889eSDave Chinner error = xfs_check_agi_freecount(cur); 152730f712c9SDave Chinner if (error) 152830f712c9SDave Chinner goto error_icur; 152930f712c9SDave Chinner 153030f712c9SDave Chinner xfs_btree_del_cursor(icur, XFS_BTREE_NOERROR); 153130f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); 153230f712c9SDave Chinner *inop = ino; 153330f712c9SDave Chinner return 0; 153430f712c9SDave Chinner 153530f712c9SDave Chinner error_icur: 153630f712c9SDave Chinner xfs_btree_del_cursor(icur, XFS_BTREE_ERROR); 153730f712c9SDave Chinner error_cur: 153830f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); 153930f712c9SDave Chinner return error; 154030f712c9SDave Chinner } 154130f712c9SDave Chinner 1542f3bf6e0fSDave Chinner static int 1543aececc9fSDave Chinner xfs_dialloc_roll( 1544aececc9fSDave Chinner struct xfs_trans **tpp, 1545aececc9fSDave Chinner struct xfs_buf *agibp) 1546aececc9fSDave Chinner { 1547aececc9fSDave Chinner struct xfs_trans *tp = *tpp; 1548aececc9fSDave Chinner struct xfs_dquot_acct *dqinfo; 1549aececc9fSDave Chinner int error; 1550aececc9fSDave Chinner 1551aececc9fSDave Chinner /* 1552aececc9fSDave Chinner * Hold to on to the agibp across the commit so no other allocation can 1553aececc9fSDave Chinner * come in and take the free inodes we just allocated for our caller. 1554aececc9fSDave Chinner */ 1555aececc9fSDave Chinner xfs_trans_bhold(tp, agibp); 1556aececc9fSDave Chinner 1557aececc9fSDave Chinner /* 1558aececc9fSDave Chinner * We want the quota changes to be associated with the next transaction, 1559aececc9fSDave Chinner * NOT this one. So, detach the dqinfo from this and attach it to the 1560aececc9fSDave Chinner * next transaction. 1561aececc9fSDave Chinner */ 1562aececc9fSDave Chinner dqinfo = tp->t_dqinfo; 1563aececc9fSDave Chinner tp->t_dqinfo = NULL; 1564aececc9fSDave Chinner 1565aececc9fSDave Chinner error = xfs_trans_roll(&tp); 1566aececc9fSDave Chinner 1567aececc9fSDave Chinner /* Re-attach the quota info that we detached from prev trx. */ 1568aececc9fSDave Chinner tp->t_dqinfo = dqinfo; 1569aececc9fSDave Chinner 15708237fbf5SDave Chinner /* 15718237fbf5SDave Chinner * Join the buffer even on commit error so that the buffer is released 15728237fbf5SDave Chinner * when the caller cancels the transaction and doesn't have to handle 15738237fbf5SDave Chinner * this error case specially. 15748237fbf5SDave Chinner */ 1575aececc9fSDave Chinner xfs_trans_bjoin(tp, agibp); 15768237fbf5SDave Chinner *tpp = tp; 15778237fbf5SDave Chinner return error; 1578aececc9fSDave Chinner } 1579aececc9fSDave Chinner 15808237fbf5SDave Chinner static xfs_agnumber_t 158189b1f55aSDave Chinner xfs_ialloc_next_ag( 158289b1f55aSDave Chinner xfs_mount_t *mp) 158389b1f55aSDave Chinner { 158489b1f55aSDave Chinner xfs_agnumber_t agno; 158589b1f55aSDave Chinner 158689b1f55aSDave Chinner spin_lock(&mp->m_agirotor_lock); 158789b1f55aSDave Chinner agno = mp->m_agirotor; 158889b1f55aSDave Chinner if (++mp->m_agirotor >= mp->m_maxagi) 158989b1f55aSDave Chinner mp->m_agirotor = 0; 159089b1f55aSDave Chinner spin_unlock(&mp->m_agirotor_lock); 159189b1f55aSDave Chinner 159289b1f55aSDave Chinner return agno; 159389b1f55aSDave Chinner } 159489b1f55aSDave Chinner 15958237fbf5SDave Chinner static bool 15968237fbf5SDave Chinner xfs_dialloc_good_ag( 15978237fbf5SDave Chinner struct xfs_trans *tp, 15988237fbf5SDave Chinner struct xfs_perag *pag, 15998237fbf5SDave Chinner umode_t mode, 16008237fbf5SDave Chinner int flags, 16018237fbf5SDave Chinner bool ok_alloc) 16028237fbf5SDave Chinner { 16038237fbf5SDave Chinner struct xfs_mount *mp = tp->t_mountp; 16048237fbf5SDave Chinner xfs_extlen_t ineed; 16058237fbf5SDave Chinner xfs_extlen_t longest = 0; 16068237fbf5SDave Chinner int needspace; 16078237fbf5SDave Chinner int error; 16088237fbf5SDave Chinner 16098237fbf5SDave Chinner if (!pag->pagi_inodeok) 16108237fbf5SDave Chinner return false; 16118237fbf5SDave Chinner 16128237fbf5SDave Chinner if (!pag->pagi_init) { 16138237fbf5SDave Chinner error = xfs_ialloc_pagi_init(mp, tp, pag->pag_agno); 16148237fbf5SDave Chinner if (error) 16158237fbf5SDave Chinner return false; 16168237fbf5SDave Chinner } 16178237fbf5SDave Chinner 16188237fbf5SDave Chinner if (pag->pagi_freecount) 16198237fbf5SDave Chinner return true; 16208237fbf5SDave Chinner if (!ok_alloc) 16218237fbf5SDave Chinner return false; 16228237fbf5SDave Chinner 16238237fbf5SDave Chinner if (!pag->pagf_init) { 16248237fbf5SDave Chinner error = xfs_alloc_pagf_init(mp, tp, pag->pag_agno, flags); 16258237fbf5SDave Chinner if (error) 16268237fbf5SDave Chinner return false; 16278237fbf5SDave Chinner } 16288237fbf5SDave Chinner 162930f712c9SDave Chinner /* 16308237fbf5SDave Chinner * Check that there is enough free space for the file plus a chunk of 16318237fbf5SDave Chinner * inodes if we need to allocate some. If this is the first pass across 16328237fbf5SDave Chinner * the AGs, take into account the potential space needed for alignment 16338237fbf5SDave Chinner * of inode chunks when checking the longest contiguous free space in 16348237fbf5SDave Chinner * the AG - this prevents us from getting ENOSPC because we have free 16358237fbf5SDave Chinner * space larger than ialloc_blks but alignment constraints prevent us 16368237fbf5SDave Chinner * from using it. 16378237fbf5SDave Chinner * 16388237fbf5SDave Chinner * If we can't find an AG with space for full alignment slack to be 16398237fbf5SDave Chinner * taken into account, we must be near ENOSPC in all AGs. Hence we 16408237fbf5SDave Chinner * don't include alignment for the second pass and so if we fail 16418237fbf5SDave Chinner * allocation due to alignment issues then it is most likely a real 16428237fbf5SDave Chinner * ENOSPC condition. 16438237fbf5SDave Chinner * 16448237fbf5SDave Chinner * XXX(dgc): this calculation is now bogus thanks to the per-ag 16458237fbf5SDave Chinner * reservations that xfs_alloc_fix_freelist() now does via 16468237fbf5SDave Chinner * xfs_alloc_space_available(). When the AG fills up, pagf_freeblks will 16478237fbf5SDave Chinner * be more than large enough for the check below to succeed, but 16488237fbf5SDave Chinner * xfs_alloc_space_available() will fail because of the non-zero 16498237fbf5SDave Chinner * metadata reservation and hence we won't actually be able to allocate 16508237fbf5SDave Chinner * more inodes in this AG. We do soooo much unnecessary work near ENOSPC 16518237fbf5SDave Chinner * because of this. 16528237fbf5SDave Chinner */ 16538237fbf5SDave Chinner ineed = M_IGEO(mp)->ialloc_min_blks; 16548237fbf5SDave Chinner if (flags && ineed > 1) 16558237fbf5SDave Chinner ineed += M_IGEO(mp)->cluster_align; 16568237fbf5SDave Chinner longest = pag->pagf_longest; 16578237fbf5SDave Chinner if (!longest) 16588237fbf5SDave Chinner longest = pag->pagf_flcount > 0; 16598237fbf5SDave Chinner needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode); 16608237fbf5SDave Chinner 16618237fbf5SDave Chinner if (pag->pagf_freeblks < needspace + ineed || longest < ineed) 16628237fbf5SDave Chinner return false; 16638237fbf5SDave Chinner return true; 16648237fbf5SDave Chinner } 16658237fbf5SDave Chinner 16668237fbf5SDave Chinner static int 16678237fbf5SDave Chinner xfs_dialloc_try_ag( 16688237fbf5SDave Chinner struct xfs_trans **tpp, 16698237fbf5SDave Chinner struct xfs_perag *pag, 16708237fbf5SDave Chinner xfs_ino_t parent, 16718237fbf5SDave Chinner xfs_ino_t *new_ino, 16728237fbf5SDave Chinner bool ok_alloc) 16738237fbf5SDave Chinner { 16748237fbf5SDave Chinner struct xfs_buf *agbp; 16758237fbf5SDave Chinner xfs_ino_t ino; 16768237fbf5SDave Chinner int error; 16778237fbf5SDave Chinner 16788237fbf5SDave Chinner /* 16798237fbf5SDave Chinner * Then read in the AGI buffer and recheck with the AGI buffer 16808237fbf5SDave Chinner * lock held. 16818237fbf5SDave Chinner */ 16828237fbf5SDave Chinner error = xfs_ialloc_read_agi(pag->pag_mount, *tpp, pag->pag_agno, &agbp); 16838237fbf5SDave Chinner if (error) 16848237fbf5SDave Chinner return error; 16858237fbf5SDave Chinner 16868237fbf5SDave Chinner if (!pag->pagi_freecount) { 16878237fbf5SDave Chinner if (!ok_alloc) { 16888237fbf5SDave Chinner error = -EAGAIN; 16898237fbf5SDave Chinner goto out_release; 16908237fbf5SDave Chinner } 16918237fbf5SDave Chinner 16928237fbf5SDave Chinner error = xfs_ialloc_ag_alloc(*tpp, agbp, pag); 16938237fbf5SDave Chinner if (error < 0) 16948237fbf5SDave Chinner goto out_release; 16958237fbf5SDave Chinner 16968237fbf5SDave Chinner /* 16978237fbf5SDave Chinner * We successfully allocated space for an inode cluster in this 16988237fbf5SDave Chinner * AG. Roll the transaction so that we can allocate one of the 16998237fbf5SDave Chinner * new inodes. 17008237fbf5SDave Chinner */ 17018237fbf5SDave Chinner ASSERT(pag->pagi_freecount > 0); 17028237fbf5SDave Chinner error = xfs_dialloc_roll(tpp, agbp); 17038237fbf5SDave Chinner if (error) 17048237fbf5SDave Chinner goto out_release; 17058237fbf5SDave Chinner } 17068237fbf5SDave Chinner 17078237fbf5SDave Chinner /* Allocate an inode in the found AG */ 17088237fbf5SDave Chinner error = xfs_dialloc_ag(*tpp, agbp, pag, parent, &ino); 17098237fbf5SDave Chinner if (!error) 17108237fbf5SDave Chinner *new_ino = ino; 17118237fbf5SDave Chinner return error; 17128237fbf5SDave Chinner 17138237fbf5SDave Chinner out_release: 17148237fbf5SDave Chinner xfs_trans_brelse(*tpp, agbp); 17158237fbf5SDave Chinner return error; 17168237fbf5SDave Chinner } 17178237fbf5SDave Chinner 17188237fbf5SDave Chinner /* 17198237fbf5SDave Chinner * Allocate an on-disk inode. 172030f712c9SDave Chinner * 17218d822dc3SDave Chinner * Mode is used to tell whether the new inode is a directory and hence where to 17228237fbf5SDave Chinner * locate it. The on-disk inode that is allocated will be returned in @new_ino 17238237fbf5SDave Chinner * on success, otherwise an error will be set to indicate the failure (e.g. 17248237fbf5SDave Chinner * -ENOSPC). 172530f712c9SDave Chinner */ 172630f712c9SDave Chinner int 1727b652afd9SDave Chinner xfs_dialloc( 1728f3bf6e0fSDave Chinner struct xfs_trans **tpp, 172930f712c9SDave Chinner xfs_ino_t parent, 173030f712c9SDave Chinner umode_t mode, 1731b652afd9SDave Chinner xfs_ino_t *new_ino) 173230f712c9SDave Chinner { 1733f3bf6e0fSDave Chinner struct xfs_mount *mp = (*tpp)->t_mountp; 173430f712c9SDave Chinner xfs_agnumber_t agno; 1735b652afd9SDave Chinner int error = 0; 173630f712c9SDave Chinner xfs_agnumber_t start_agno; 173730f712c9SDave Chinner struct xfs_perag *pag; 1738ef325959SDarrick J. Wong struct xfs_ino_geometry *igeo = M_IGEO(mp); 17398237fbf5SDave Chinner bool ok_alloc = true; 174089b1f55aSDave Chinner int flags; 1741b652afd9SDave Chinner xfs_ino_t ino; 17428d822dc3SDave Chinner 174330f712c9SDave Chinner /* 174489b1f55aSDave Chinner * Directories, symlinks, and regular files frequently allocate at least 174589b1f55aSDave Chinner * one block, so factor that potential expansion when we examine whether 174689b1f55aSDave Chinner * an AG has enough space for file creation. 174730f712c9SDave Chinner */ 174889b1f55aSDave Chinner if (S_ISDIR(mode)) 174989b1f55aSDave Chinner start_agno = xfs_ialloc_next_ag(mp); 175089b1f55aSDave Chinner else { 175189b1f55aSDave Chinner start_agno = XFS_INO_TO_AGNO(mp, parent); 175289b1f55aSDave Chinner if (start_agno >= mp->m_maxagi) 175389b1f55aSDave Chinner start_agno = 0; 175489b1f55aSDave Chinner } 175530f712c9SDave Chinner 175630f712c9SDave Chinner /* 175730f712c9SDave Chinner * If we have already hit the ceiling of inode blocks then clear 17588237fbf5SDave Chinner * ok_alloc so we scan all available agi structures for a free 175930f712c9SDave Chinner * inode. 176074f9ce1cSGeorge Wang * 176174f9ce1cSGeorge Wang * Read rough value of mp->m_icount by percpu_counter_read_positive, 176274f9ce1cSGeorge Wang * which will sacrifice the preciseness but improve the performance. 176330f712c9SDave Chinner */ 1764ef325959SDarrick J. Wong if (igeo->maxicount && 1765ef325959SDarrick J. Wong percpu_counter_read_positive(&mp->m_icount) + igeo->ialloc_inos 1766ef325959SDarrick J. Wong > igeo->maxicount) { 17678237fbf5SDave Chinner ok_alloc = false; 176830f712c9SDave Chinner } 176930f712c9SDave Chinner 177030f712c9SDave Chinner /* 177130f712c9SDave Chinner * Loop until we find an allocation group that either has free inodes 177230f712c9SDave Chinner * or in which we can allocate some inodes. Iterate through the 177330f712c9SDave Chinner * allocation groups upward, wrapping at the end. 177430f712c9SDave Chinner */ 177530f712c9SDave Chinner agno = start_agno; 177689b1f55aSDave Chinner flags = XFS_ALLOC_FLAG_TRYLOCK; 177730f712c9SDave Chinner for (;;) { 177830f712c9SDave Chinner pag = xfs_perag_get(mp, agno); 17798237fbf5SDave Chinner if (xfs_dialloc_good_ag(*tpp, pag, mode, flags, ok_alloc)) { 17808237fbf5SDave Chinner error = xfs_dialloc_try_ag(tpp, pag, parent, 17818237fbf5SDave Chinner &ino, ok_alloc); 17828237fbf5SDave Chinner if (error != -EAGAIN) 17838d822dc3SDave Chinner break; 178430f712c9SDave Chinner } 178530f712c9SDave Chinner 178675c8c50fSDave Chinner if (xfs_is_shutdown(mp)) { 178789b1f55aSDave Chinner error = -EFSCORRUPTED; 178842685473SDave Chinner break; 178989b1f55aSDave Chinner } 179089b1f55aSDave Chinner if (++agno == mp->m_maxagi) 179189b1f55aSDave Chinner agno = 0; 179289b1f55aSDave Chinner if (agno == start_agno) { 17938237fbf5SDave Chinner if (!flags) { 17948237fbf5SDave Chinner error = -ENOSPC; 179589b1f55aSDave Chinner break; 17968237fbf5SDave Chinner } 179789b1f55aSDave Chinner flags = 0; 179889b1f55aSDave Chinner } 179942685473SDave Chinner xfs_perag_put(pag); 180030f712c9SDave Chinner } 180130f712c9SDave Chinner 18028237fbf5SDave Chinner if (!error) 1803b652afd9SDave Chinner *new_ino = ino; 18048237fbf5SDave Chinner xfs_perag_put(pag); 18058237fbf5SDave Chinner return error; 180630f712c9SDave Chinner } 180730f712c9SDave Chinner 180810ae3dc7SBrian Foster /* 180910ae3dc7SBrian Foster * Free the blocks of an inode chunk. We must consider that the inode chunk 181010ae3dc7SBrian Foster * might be sparse and only free the regions that are allocated as part of the 181110ae3dc7SBrian Foster * chunk. 181210ae3dc7SBrian Foster */ 181310ae3dc7SBrian Foster STATIC void 181410ae3dc7SBrian Foster xfs_difree_inode_chunk( 18150f37d178SBrian Foster struct xfs_trans *tp, 181610ae3dc7SBrian Foster xfs_agnumber_t agno, 18170f37d178SBrian Foster struct xfs_inobt_rec_incore *rec) 181810ae3dc7SBrian Foster { 18190f37d178SBrian Foster struct xfs_mount *mp = tp->t_mountp; 18200f37d178SBrian Foster xfs_agblock_t sagbno = XFS_AGINO_TO_AGBNO(mp, 18210f37d178SBrian Foster rec->ir_startino); 182210ae3dc7SBrian Foster int startidx, endidx; 182310ae3dc7SBrian Foster int nextbit; 182410ae3dc7SBrian Foster xfs_agblock_t agbno; 182510ae3dc7SBrian Foster int contigblk; 182610ae3dc7SBrian Foster DECLARE_BITMAP(holemask, XFS_INOBT_HOLEMASK_BITS); 182710ae3dc7SBrian Foster 182810ae3dc7SBrian Foster if (!xfs_inobt_issparse(rec->ir_holemask)) { 182910ae3dc7SBrian Foster /* not sparse, calculate extent info directly */ 1830c201d9caSDarrick J. Wong xfs_free_extent_later(tp, XFS_AGB_TO_FSB(mp, agno, sagbno), 1831ef325959SDarrick J. Wong M_IGEO(mp)->ialloc_blks, 1832ef325959SDarrick J. Wong &XFS_RMAP_OINFO_INODES); 183310ae3dc7SBrian Foster return; 183410ae3dc7SBrian Foster } 183510ae3dc7SBrian Foster 183610ae3dc7SBrian Foster /* holemask is only 16-bits (fits in an unsigned long) */ 183710ae3dc7SBrian Foster ASSERT(sizeof(rec->ir_holemask) <= sizeof(holemask[0])); 183810ae3dc7SBrian Foster holemask[0] = rec->ir_holemask; 183910ae3dc7SBrian Foster 184010ae3dc7SBrian Foster /* 184110ae3dc7SBrian Foster * Find contiguous ranges of zeroes (i.e., allocated regions) in the 184210ae3dc7SBrian Foster * holemask and convert the start/end index of each range to an extent. 184310ae3dc7SBrian Foster * We start with the start and end index both pointing at the first 0 in 184410ae3dc7SBrian Foster * the mask. 184510ae3dc7SBrian Foster */ 184610ae3dc7SBrian Foster startidx = endidx = find_first_zero_bit(holemask, 184710ae3dc7SBrian Foster XFS_INOBT_HOLEMASK_BITS); 184810ae3dc7SBrian Foster nextbit = startidx + 1; 184910ae3dc7SBrian Foster while (startidx < XFS_INOBT_HOLEMASK_BITS) { 185010ae3dc7SBrian Foster nextbit = find_next_zero_bit(holemask, XFS_INOBT_HOLEMASK_BITS, 185110ae3dc7SBrian Foster nextbit); 185210ae3dc7SBrian Foster /* 185310ae3dc7SBrian Foster * If the next zero bit is contiguous, update the end index of 185410ae3dc7SBrian Foster * the current range and continue. 185510ae3dc7SBrian Foster */ 185610ae3dc7SBrian Foster if (nextbit != XFS_INOBT_HOLEMASK_BITS && 185710ae3dc7SBrian Foster nextbit == endidx + 1) { 185810ae3dc7SBrian Foster endidx = nextbit; 185910ae3dc7SBrian Foster goto next; 186010ae3dc7SBrian Foster } 186110ae3dc7SBrian Foster 186210ae3dc7SBrian Foster /* 186310ae3dc7SBrian Foster * nextbit is not contiguous with the current end index. Convert 186410ae3dc7SBrian Foster * the current start/end to an extent and add it to the free 186510ae3dc7SBrian Foster * list. 186610ae3dc7SBrian Foster */ 186710ae3dc7SBrian Foster agbno = sagbno + (startidx * XFS_INODES_PER_HOLEMASK_BIT) / 186810ae3dc7SBrian Foster mp->m_sb.sb_inopblock; 186910ae3dc7SBrian Foster contigblk = ((endidx - startidx + 1) * 187010ae3dc7SBrian Foster XFS_INODES_PER_HOLEMASK_BIT) / 187110ae3dc7SBrian Foster mp->m_sb.sb_inopblock; 187210ae3dc7SBrian Foster 187310ae3dc7SBrian Foster ASSERT(agbno % mp->m_sb.sb_spino_align == 0); 187410ae3dc7SBrian Foster ASSERT(contigblk % mp->m_sb.sb_spino_align == 0); 1875c201d9caSDarrick J. Wong xfs_free_extent_later(tp, XFS_AGB_TO_FSB(mp, agno, agbno), 18767280fedaSDarrick J. Wong contigblk, &XFS_RMAP_OINFO_INODES); 187710ae3dc7SBrian Foster 187810ae3dc7SBrian Foster /* reset range to current bit and carry on... */ 187910ae3dc7SBrian Foster startidx = endidx = nextbit; 188010ae3dc7SBrian Foster 188110ae3dc7SBrian Foster next: 188210ae3dc7SBrian Foster nextbit++; 188310ae3dc7SBrian Foster } 188410ae3dc7SBrian Foster } 188510ae3dc7SBrian Foster 188630f712c9SDave Chinner STATIC int 188730f712c9SDave Chinner xfs_difree_inobt( 188830f712c9SDave Chinner struct xfs_mount *mp, 188930f712c9SDave Chinner struct xfs_trans *tp, 189030f712c9SDave Chinner struct xfs_buf *agbp, 18917b13c515SDave Chinner struct xfs_perag *pag, 189230f712c9SDave Chinner xfs_agino_t agino, 189309b56604SBrian Foster struct xfs_icluster *xic, 189430f712c9SDave Chinner struct xfs_inobt_rec_incore *orec) 189530f712c9SDave Chinner { 1896370c782bSChristoph Hellwig struct xfs_agi *agi = agbp->b_addr; 189730f712c9SDave Chinner struct xfs_btree_cur *cur; 189830f712c9SDave Chinner struct xfs_inobt_rec_incore rec; 189930f712c9SDave Chinner int ilen; 190030f712c9SDave Chinner int error; 190130f712c9SDave Chinner int i; 190230f712c9SDave Chinner int off; 190330f712c9SDave Chinner 190430f712c9SDave Chinner ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC)); 190530f712c9SDave Chinner ASSERT(XFS_AGINO_TO_AGBNO(mp, agino) < be32_to_cpu(agi->agi_length)); 190630f712c9SDave Chinner 190730f712c9SDave Chinner /* 190830f712c9SDave Chinner * Initialize the cursor. 190930f712c9SDave Chinner */ 19107b13c515SDave Chinner cur = xfs_inobt_init_cursor(mp, tp, agbp, pag, XFS_BTNUM_INO); 191130f712c9SDave Chinner 19129ba0889eSDave Chinner error = xfs_check_agi_freecount(cur); 191330f712c9SDave Chinner if (error) 191430f712c9SDave Chinner goto error0; 191530f712c9SDave Chinner 191630f712c9SDave Chinner /* 191730f712c9SDave Chinner * Look for the entry describing this inode. 191830f712c9SDave Chinner */ 191930f712c9SDave Chinner if ((error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i))) { 192030f712c9SDave Chinner xfs_warn(mp, "%s: xfs_inobt_lookup() returned error %d.", 192130f712c9SDave Chinner __func__, error); 192230f712c9SDave Chinner goto error0; 192330f712c9SDave Chinner } 1924f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(mp, i != 1)) { 1925f9e03706SDarrick J. Wong error = -EFSCORRUPTED; 1926f9e03706SDarrick J. Wong goto error0; 1927f9e03706SDarrick J. Wong } 192830f712c9SDave Chinner error = xfs_inobt_get_rec(cur, &rec, &i); 192930f712c9SDave Chinner if (error) { 193030f712c9SDave Chinner xfs_warn(mp, "%s: xfs_inobt_get_rec() returned error %d.", 193130f712c9SDave Chinner __func__, error); 193230f712c9SDave Chinner goto error0; 193330f712c9SDave Chinner } 1934f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(mp, i != 1)) { 1935f9e03706SDarrick J. Wong error = -EFSCORRUPTED; 1936f9e03706SDarrick J. Wong goto error0; 1937f9e03706SDarrick J. Wong } 193830f712c9SDave Chinner /* 193930f712c9SDave Chinner * Get the offset in the inode chunk. 194030f712c9SDave Chinner */ 194130f712c9SDave Chinner off = agino - rec.ir_startino; 194230f712c9SDave Chinner ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK); 194330f712c9SDave Chinner ASSERT(!(rec.ir_free & XFS_INOBT_MASK(off))); 194430f712c9SDave Chinner /* 194530f712c9SDave Chinner * Mark the inode free & increment the count. 194630f712c9SDave Chinner */ 194730f712c9SDave Chinner rec.ir_free |= XFS_INOBT_MASK(off); 194830f712c9SDave Chinner rec.ir_freecount++; 194930f712c9SDave Chinner 195030f712c9SDave Chinner /* 1951999633d3SBrian Foster * When an inode chunk is free, it becomes eligible for removal. Don't 1952999633d3SBrian Foster * remove the chunk if the block size is large enough for multiple inode 1953999633d3SBrian Foster * chunks (that might not be free). 195430f712c9SDave Chinner */ 19550560f31aSDave Chinner if (!xfs_has_ikeep(mp) && rec.ir_free == XFS_INOBT_ALL_FREE && 1956999633d3SBrian Foster mp->m_sb.sb_inopblock <= XFS_INODES_PER_CHUNK) { 195792a00544SGao Xiang struct xfs_perag *pag = agbp->b_pag; 195892a00544SGao Xiang 1959749f24f3SThomas Meyer xic->deleted = true; 19607b13c515SDave Chinner xic->first_ino = XFS_AGINO_TO_INO(mp, pag->pag_agno, 19617b13c515SDave Chinner rec.ir_startino); 196209b56604SBrian Foster xic->alloc = xfs_inobt_irec_to_allocmask(&rec); 196330f712c9SDave Chinner 196430f712c9SDave Chinner /* 196530f712c9SDave Chinner * Remove the inode cluster from the AGI B+Tree, adjust the 196630f712c9SDave Chinner * AGI and Superblock inode counts, and mark the disk space 196730f712c9SDave Chinner * to be freed when the transaction is committed. 196830f712c9SDave Chinner */ 1969999633d3SBrian Foster ilen = rec.ir_freecount; 197030f712c9SDave Chinner be32_add_cpu(&agi->agi_count, -ilen); 197130f712c9SDave Chinner be32_add_cpu(&agi->agi_freecount, -(ilen - 1)); 197230f712c9SDave Chinner xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT); 197330f712c9SDave Chinner pag->pagi_freecount -= ilen - 1; 197489e9b5c0SDarrick J. Wong pag->pagi_count -= ilen; 197530f712c9SDave Chinner xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen); 197630f712c9SDave Chinner xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1)); 197730f712c9SDave Chinner 197830f712c9SDave Chinner if ((error = xfs_btree_delete(cur, &i))) { 197930f712c9SDave Chinner xfs_warn(mp, "%s: xfs_btree_delete returned error %d.", 198030f712c9SDave Chinner __func__, error); 198130f712c9SDave Chinner goto error0; 198230f712c9SDave Chinner } 198330f712c9SDave Chinner 19847b13c515SDave Chinner xfs_difree_inode_chunk(tp, pag->pag_agno, &rec); 198530f712c9SDave Chinner } else { 1986749f24f3SThomas Meyer xic->deleted = false; 198730f712c9SDave Chinner 198830f712c9SDave Chinner error = xfs_inobt_update(cur, &rec); 198930f712c9SDave Chinner if (error) { 199030f712c9SDave Chinner xfs_warn(mp, "%s: xfs_inobt_update returned error %d.", 199130f712c9SDave Chinner __func__, error); 199230f712c9SDave Chinner goto error0; 199330f712c9SDave Chinner } 199430f712c9SDave Chinner 199530f712c9SDave Chinner /* 199630f712c9SDave Chinner * Change the inode free counts and log the ag/sb changes. 199730f712c9SDave Chinner */ 199830f712c9SDave Chinner be32_add_cpu(&agi->agi_freecount, 1); 199930f712c9SDave Chinner xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT); 20007b13c515SDave Chinner pag->pagi_freecount++; 200130f712c9SDave Chinner xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1); 200230f712c9SDave Chinner } 200330f712c9SDave Chinner 20049ba0889eSDave Chinner error = xfs_check_agi_freecount(cur); 200530f712c9SDave Chinner if (error) 200630f712c9SDave Chinner goto error0; 200730f712c9SDave Chinner 200830f712c9SDave Chinner *orec = rec; 200930f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); 201030f712c9SDave Chinner return 0; 201130f712c9SDave Chinner 201230f712c9SDave Chinner error0: 201330f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); 201430f712c9SDave Chinner return error; 201530f712c9SDave Chinner } 201630f712c9SDave Chinner 201730f712c9SDave Chinner /* 201830f712c9SDave Chinner * Free an inode in the free inode btree. 201930f712c9SDave Chinner */ 202030f712c9SDave Chinner STATIC int 202130f712c9SDave Chinner xfs_difree_finobt( 202230f712c9SDave Chinner struct xfs_mount *mp, 202330f712c9SDave Chinner struct xfs_trans *tp, 202430f712c9SDave Chinner struct xfs_buf *agbp, 20257b13c515SDave Chinner struct xfs_perag *pag, 202630f712c9SDave Chinner xfs_agino_t agino, 202730f712c9SDave Chinner struct xfs_inobt_rec_incore *ibtrec) /* inobt record */ 202830f712c9SDave Chinner { 202930f712c9SDave Chinner struct xfs_btree_cur *cur; 203030f712c9SDave Chinner struct xfs_inobt_rec_incore rec; 203130f712c9SDave Chinner int offset = agino - ibtrec->ir_startino; 203230f712c9SDave Chinner int error; 203330f712c9SDave Chinner int i; 203430f712c9SDave Chinner 20357b13c515SDave Chinner cur = xfs_inobt_init_cursor(mp, tp, agbp, pag, XFS_BTNUM_FINO); 203630f712c9SDave Chinner 203730f712c9SDave Chinner error = xfs_inobt_lookup(cur, ibtrec->ir_startino, XFS_LOOKUP_EQ, &i); 203830f712c9SDave Chinner if (error) 203930f712c9SDave Chinner goto error; 204030f712c9SDave Chinner if (i == 0) { 204130f712c9SDave Chinner /* 204230f712c9SDave Chinner * If the record does not exist in the finobt, we must have just 204330f712c9SDave Chinner * freed an inode in a previously fully allocated chunk. If not, 204430f712c9SDave Chinner * something is out of sync. 204530f712c9SDave Chinner */ 2046f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(mp, ibtrec->ir_freecount != 1)) { 2047f9e03706SDarrick J. Wong error = -EFSCORRUPTED; 2048f9e03706SDarrick J. Wong goto error; 2049f9e03706SDarrick J. Wong } 205030f712c9SDave Chinner 20515419040fSBrian Foster error = xfs_inobt_insert_rec(cur, ibtrec->ir_holemask, 20525419040fSBrian Foster ibtrec->ir_count, 20535419040fSBrian Foster ibtrec->ir_freecount, 205430f712c9SDave Chinner ibtrec->ir_free, &i); 205530f712c9SDave Chinner if (error) 205630f712c9SDave Chinner goto error; 205730f712c9SDave Chinner ASSERT(i == 1); 205830f712c9SDave Chinner 205930f712c9SDave Chinner goto out; 206030f712c9SDave Chinner } 206130f712c9SDave Chinner 206230f712c9SDave Chinner /* 206330f712c9SDave Chinner * Read and update the existing record. We could just copy the ibtrec 206430f712c9SDave Chinner * across here, but that would defeat the purpose of having redundant 206530f712c9SDave Chinner * metadata. By making the modifications independently, we can catch 206630f712c9SDave Chinner * corruptions that we wouldn't see if we just copied from one record 206730f712c9SDave Chinner * to another. 206830f712c9SDave Chinner */ 206930f712c9SDave Chinner error = xfs_inobt_get_rec(cur, &rec, &i); 207030f712c9SDave Chinner if (error) 207130f712c9SDave Chinner goto error; 2072f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(mp, i != 1)) { 2073f9e03706SDarrick J. Wong error = -EFSCORRUPTED; 2074f9e03706SDarrick J. Wong goto error; 2075f9e03706SDarrick J. Wong } 207630f712c9SDave Chinner 207730f712c9SDave Chinner rec.ir_free |= XFS_INOBT_MASK(offset); 207830f712c9SDave Chinner rec.ir_freecount++; 207930f712c9SDave Chinner 2080f9e03706SDarrick J. Wong if (XFS_IS_CORRUPT(mp, 2081f9e03706SDarrick J. Wong rec.ir_free != ibtrec->ir_free || 2082f9e03706SDarrick J. Wong rec.ir_freecount != ibtrec->ir_freecount)) { 2083f9e03706SDarrick J. Wong error = -EFSCORRUPTED; 2084f9e03706SDarrick J. Wong goto error; 2085f9e03706SDarrick J. Wong } 208630f712c9SDave Chinner 208730f712c9SDave Chinner /* 208830f712c9SDave Chinner * The content of inobt records should always match between the inobt 208930f712c9SDave Chinner * and finobt. The lifecycle of records in the finobt is different from 209030f712c9SDave Chinner * the inobt in that the finobt only tracks records with at least one 209130f712c9SDave Chinner * free inode. Hence, if all of the inodes are free and we aren't 209230f712c9SDave Chinner * keeping inode chunks permanently on disk, remove the record. 209330f712c9SDave Chinner * Otherwise, update the record with the new information. 2094999633d3SBrian Foster * 2095999633d3SBrian Foster * Note that we currently can't free chunks when the block size is large 2096999633d3SBrian Foster * enough for multiple chunks. Leave the finobt record to remain in sync 2097999633d3SBrian Foster * with the inobt. 209830f712c9SDave Chinner */ 20990560f31aSDave Chinner if (!xfs_has_ikeep(mp) && rec.ir_free == XFS_INOBT_ALL_FREE && 21000560f31aSDave Chinner mp->m_sb.sb_inopblock <= XFS_INODES_PER_CHUNK) { 210130f712c9SDave Chinner error = xfs_btree_delete(cur, &i); 210230f712c9SDave Chinner if (error) 210330f712c9SDave Chinner goto error; 210430f712c9SDave Chinner ASSERT(i == 1); 210530f712c9SDave Chinner } else { 210630f712c9SDave Chinner error = xfs_inobt_update(cur, &rec); 210730f712c9SDave Chinner if (error) 210830f712c9SDave Chinner goto error; 210930f712c9SDave Chinner } 211030f712c9SDave Chinner 211130f712c9SDave Chinner out: 21129ba0889eSDave Chinner error = xfs_check_agi_freecount(cur); 211330f712c9SDave Chinner if (error) 211430f712c9SDave Chinner goto error; 211530f712c9SDave Chinner 211630f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); 211730f712c9SDave Chinner return 0; 211830f712c9SDave Chinner 211930f712c9SDave Chinner error: 212030f712c9SDave Chinner xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); 212130f712c9SDave Chinner return error; 212230f712c9SDave Chinner } 212330f712c9SDave Chinner 212430f712c9SDave Chinner /* 212530f712c9SDave Chinner * Free disk inode. Carefully avoids touching the incore inode, all 212630f712c9SDave Chinner * manipulations incore are the caller's responsibility. 212730f712c9SDave Chinner * The on-disk inode is not changed by this operation, only the 212830f712c9SDave Chinner * btree (free inode mask) is changed. 212930f712c9SDave Chinner */ 213030f712c9SDave Chinner int 213130f712c9SDave Chinner xfs_difree( 2132f40aadb2SDave Chinner struct xfs_trans *tp, 2133f40aadb2SDave Chinner struct xfs_perag *pag, 2134f40aadb2SDave Chinner xfs_ino_t inode, 2135f40aadb2SDave Chinner struct xfs_icluster *xic) 213630f712c9SDave Chinner { 213730f712c9SDave Chinner /* REFERENCED */ 213830f712c9SDave Chinner xfs_agblock_t agbno; /* block number containing inode */ 213930f712c9SDave Chinner struct xfs_buf *agbp; /* buffer for allocation group header */ 214030f712c9SDave Chinner xfs_agino_t agino; /* allocation group inode number */ 214130f712c9SDave Chinner int error; /* error return value */ 21427b13c515SDave Chinner struct xfs_mount *mp = tp->t_mountp; 214330f712c9SDave Chinner struct xfs_inobt_rec_incore rec;/* btree record */ 214430f712c9SDave Chinner 214530f712c9SDave Chinner /* 214630f712c9SDave Chinner * Break up inode number into its components. 214730f712c9SDave Chinner */ 2148f40aadb2SDave Chinner if (pag->pag_agno != XFS_INO_TO_AGNO(mp, inode)) { 2149f40aadb2SDave Chinner xfs_warn(mp, "%s: agno != pag->pag_agno (%d != %d).", 2150f40aadb2SDave Chinner __func__, XFS_INO_TO_AGNO(mp, inode), pag->pag_agno); 215130f712c9SDave Chinner ASSERT(0); 21522451337dSDave Chinner return -EINVAL; 215330f712c9SDave Chinner } 215430f712c9SDave Chinner agino = XFS_INO_TO_AGINO(mp, inode); 2155f40aadb2SDave Chinner if (inode != XFS_AGINO_TO_INO(mp, pag->pag_agno, agino)) { 215630f712c9SDave Chinner xfs_warn(mp, "%s: inode != XFS_AGINO_TO_INO() (%llu != %llu).", 215730f712c9SDave Chinner __func__, (unsigned long long)inode, 2158f40aadb2SDave Chinner (unsigned long long)XFS_AGINO_TO_INO(mp, pag->pag_agno, agino)); 215930f712c9SDave Chinner ASSERT(0); 21602451337dSDave Chinner return -EINVAL; 216130f712c9SDave Chinner } 216230f712c9SDave Chinner agbno = XFS_AGINO_TO_AGBNO(mp, agino); 216330f712c9SDave Chinner if (agbno >= mp->m_sb.sb_agblocks) { 216430f712c9SDave Chinner xfs_warn(mp, "%s: agbno >= mp->m_sb.sb_agblocks (%d >= %d).", 216530f712c9SDave Chinner __func__, agbno, mp->m_sb.sb_agblocks); 216630f712c9SDave Chinner ASSERT(0); 21672451337dSDave Chinner return -EINVAL; 216830f712c9SDave Chinner } 216930f712c9SDave Chinner /* 217030f712c9SDave Chinner * Get the allocation group header. 217130f712c9SDave Chinner */ 2172f40aadb2SDave Chinner error = xfs_ialloc_read_agi(mp, tp, pag->pag_agno, &agbp); 217330f712c9SDave Chinner if (error) { 217430f712c9SDave Chinner xfs_warn(mp, "%s: xfs_ialloc_read_agi() returned error %d.", 217530f712c9SDave Chinner __func__, error); 217630f712c9SDave Chinner return error; 217730f712c9SDave Chinner } 217830f712c9SDave Chinner 217930f712c9SDave Chinner /* 218030f712c9SDave Chinner * Fix up the inode allocation btree. 218130f712c9SDave Chinner */ 21827b13c515SDave Chinner error = xfs_difree_inobt(mp, tp, agbp, pag, agino, xic, &rec); 218330f712c9SDave Chinner if (error) 218430f712c9SDave Chinner goto error0; 218530f712c9SDave Chinner 218630f712c9SDave Chinner /* 218730f712c9SDave Chinner * Fix up the free inode btree. 218830f712c9SDave Chinner */ 2189ebd9027dSDave Chinner if (xfs_has_finobt(mp)) { 21907b13c515SDave Chinner error = xfs_difree_finobt(mp, tp, agbp, pag, agino, &rec); 219130f712c9SDave Chinner if (error) 219230f712c9SDave Chinner goto error0; 219330f712c9SDave Chinner } 219430f712c9SDave Chinner 219530f712c9SDave Chinner return 0; 219630f712c9SDave Chinner 219730f712c9SDave Chinner error0: 219830f712c9SDave Chinner return error; 219930f712c9SDave Chinner } 220030f712c9SDave Chinner 220130f712c9SDave Chinner STATIC int 220230f712c9SDave Chinner xfs_imap_lookup( 220330f712c9SDave Chinner struct xfs_mount *mp, 220430f712c9SDave Chinner struct xfs_trans *tp, 22057b13c515SDave Chinner struct xfs_perag *pag, 220630f712c9SDave Chinner xfs_agino_t agino, 220730f712c9SDave Chinner xfs_agblock_t agbno, 220830f712c9SDave Chinner xfs_agblock_t *chunk_agbno, 220930f712c9SDave Chinner xfs_agblock_t *offset_agbno, 221030f712c9SDave Chinner int flags) 221130f712c9SDave Chinner { 221230f712c9SDave Chinner struct xfs_inobt_rec_incore rec; 221330f712c9SDave Chinner struct xfs_btree_cur *cur; 221430f712c9SDave Chinner struct xfs_buf *agbp; 221530f712c9SDave Chinner int error; 221630f712c9SDave Chinner int i; 221730f712c9SDave Chinner 22187b13c515SDave Chinner error = xfs_ialloc_read_agi(mp, tp, pag->pag_agno, &agbp); 221930f712c9SDave Chinner if (error) { 222030f712c9SDave Chinner xfs_alert(mp, 222130f712c9SDave Chinner "%s: xfs_ialloc_read_agi() returned error %d, agno %d", 22227b13c515SDave Chinner __func__, error, pag->pag_agno); 222330f712c9SDave Chinner return error; 222430f712c9SDave Chinner } 222530f712c9SDave Chinner 222630f712c9SDave Chinner /* 222730f712c9SDave Chinner * Lookup the inode record for the given agino. If the record cannot be 222830f712c9SDave Chinner * found, then it's an invalid inode number and we should abort. Once 222930f712c9SDave Chinner * we have a record, we need to ensure it contains the inode number 223030f712c9SDave Chinner * we are looking up. 223130f712c9SDave Chinner */ 22327b13c515SDave Chinner cur = xfs_inobt_init_cursor(mp, tp, agbp, pag, XFS_BTNUM_INO); 223330f712c9SDave Chinner error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i); 223430f712c9SDave Chinner if (!error) { 223530f712c9SDave Chinner if (i) 223630f712c9SDave Chinner error = xfs_inobt_get_rec(cur, &rec, &i); 223730f712c9SDave Chinner if (!error && i == 0) 22382451337dSDave Chinner error = -EINVAL; 223930f712c9SDave Chinner } 224030f712c9SDave Chinner 224130f712c9SDave Chinner xfs_trans_brelse(tp, agbp); 22420b04b6b8SDarrick J. Wong xfs_btree_del_cursor(cur, error); 224330f712c9SDave Chinner if (error) 224430f712c9SDave Chinner return error; 224530f712c9SDave Chinner 224630f712c9SDave Chinner /* check that the returned record contains the required inode */ 224730f712c9SDave Chinner if (rec.ir_startino > agino || 2248ef325959SDarrick J. Wong rec.ir_startino + M_IGEO(mp)->ialloc_inos <= agino) 22492451337dSDave Chinner return -EINVAL; 225030f712c9SDave Chinner 225130f712c9SDave Chinner /* for untrusted inodes check it is allocated first */ 225230f712c9SDave Chinner if ((flags & XFS_IGET_UNTRUSTED) && 225330f712c9SDave Chinner (rec.ir_free & XFS_INOBT_MASK(agino - rec.ir_startino))) 22542451337dSDave Chinner return -EINVAL; 225530f712c9SDave Chinner 225630f712c9SDave Chinner *chunk_agbno = XFS_AGINO_TO_AGBNO(mp, rec.ir_startino); 225730f712c9SDave Chinner *offset_agbno = agbno - *chunk_agbno; 225830f712c9SDave Chinner return 0; 225930f712c9SDave Chinner } 226030f712c9SDave Chinner 226130f712c9SDave Chinner /* 226230f712c9SDave Chinner * Return the location of the inode in imap, for mapping it into a buffer. 226330f712c9SDave Chinner */ 226430f712c9SDave Chinner int 226530f712c9SDave Chinner xfs_imap( 22667b13c515SDave Chinner struct xfs_mount *mp, /* file system mount structure */ 22677b13c515SDave Chinner struct xfs_trans *tp, /* transaction pointer */ 226830f712c9SDave Chinner xfs_ino_t ino, /* inode to locate */ 226930f712c9SDave Chinner struct xfs_imap *imap, /* location map structure */ 227030f712c9SDave Chinner uint flags) /* flags for inode btree lookup */ 227130f712c9SDave Chinner { 227230f712c9SDave Chinner xfs_agblock_t agbno; /* block number of inode in the alloc group */ 227330f712c9SDave Chinner xfs_agino_t agino; /* inode number within alloc group */ 227430f712c9SDave Chinner xfs_agblock_t chunk_agbno; /* first block in inode chunk */ 227530f712c9SDave Chinner xfs_agblock_t cluster_agbno; /* first block in inode cluster */ 227630f712c9SDave Chinner int error; /* error code */ 227730f712c9SDave Chinner int offset; /* index of inode in its buffer */ 227830f712c9SDave Chinner xfs_agblock_t offset_agbno; /* blks from chunk start to inode */ 22797b13c515SDave Chinner struct xfs_perag *pag; 228030f712c9SDave Chinner 228130f712c9SDave Chinner ASSERT(ino != NULLFSINO); 228230f712c9SDave Chinner 228330f712c9SDave Chinner /* 228430f712c9SDave Chinner * Split up the inode number into its parts. 228530f712c9SDave Chinner */ 22867b13c515SDave Chinner pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ino)); 228730f712c9SDave Chinner agino = XFS_INO_TO_AGINO(mp, ino); 228830f712c9SDave Chinner agbno = XFS_AGINO_TO_AGBNO(mp, agino); 22897b13c515SDave Chinner if (!pag || agbno >= mp->m_sb.sb_agblocks || 22907b13c515SDave Chinner ino != XFS_AGINO_TO_INO(mp, pag->pag_agno, agino)) { 22917b13c515SDave Chinner error = -EINVAL; 229230f712c9SDave Chinner #ifdef DEBUG 229330f712c9SDave Chinner /* 229430f712c9SDave Chinner * Don't output diagnostic information for untrusted inodes 229530f712c9SDave Chinner * as they can be invalid without implying corruption. 229630f712c9SDave Chinner */ 229730f712c9SDave Chinner if (flags & XFS_IGET_UNTRUSTED) 22987b13c515SDave Chinner goto out_drop; 22997b13c515SDave Chinner if (!pag) { 230030f712c9SDave Chinner xfs_alert(mp, 230130f712c9SDave Chinner "%s: agno (%d) >= mp->m_sb.sb_agcount (%d)", 23027b13c515SDave Chinner __func__, XFS_INO_TO_AGNO(mp, ino), 23037b13c515SDave Chinner mp->m_sb.sb_agcount); 230430f712c9SDave Chinner } 230530f712c9SDave Chinner if (agbno >= mp->m_sb.sb_agblocks) { 230630f712c9SDave Chinner xfs_alert(mp, 230730f712c9SDave Chinner "%s: agbno (0x%llx) >= mp->m_sb.sb_agblocks (0x%lx)", 230830f712c9SDave Chinner __func__, (unsigned long long)agbno, 230930f712c9SDave Chinner (unsigned long)mp->m_sb.sb_agblocks); 231030f712c9SDave Chinner } 23117b13c515SDave Chinner if (pag && ino != XFS_AGINO_TO_INO(mp, pag->pag_agno, agino)) { 231230f712c9SDave Chinner xfs_alert(mp, 231330f712c9SDave Chinner "%s: ino (0x%llx) != XFS_AGINO_TO_INO() (0x%llx)", 231430f712c9SDave Chinner __func__, ino, 23157b13c515SDave Chinner XFS_AGINO_TO_INO(mp, pag->pag_agno, agino)); 231630f712c9SDave Chinner } 231730f712c9SDave Chinner xfs_stack_trace(); 231830f712c9SDave Chinner #endif /* DEBUG */ 23197b13c515SDave Chinner goto out_drop; 232030f712c9SDave Chinner } 232130f712c9SDave Chinner 232230f712c9SDave Chinner /* 232330f712c9SDave Chinner * For bulkstat and handle lookups, we have an untrusted inode number 232430f712c9SDave Chinner * that we have to verify is valid. We cannot do this just by reading 232530f712c9SDave Chinner * the inode buffer as it may have been unlinked and removed leaving 232630f712c9SDave Chinner * inodes in stale state on disk. Hence we have to do a btree lookup 232730f712c9SDave Chinner * in all cases where an untrusted inode number is passed. 232830f712c9SDave Chinner */ 232930f712c9SDave Chinner if (flags & XFS_IGET_UNTRUSTED) { 23307b13c515SDave Chinner error = xfs_imap_lookup(mp, tp, pag, agino, agbno, 233130f712c9SDave Chinner &chunk_agbno, &offset_agbno, flags); 233230f712c9SDave Chinner if (error) 23337b13c515SDave Chinner goto out_drop; 233430f712c9SDave Chinner goto out_map; 233530f712c9SDave Chinner } 233630f712c9SDave Chinner 233730f712c9SDave Chinner /* 233830f712c9SDave Chinner * If the inode cluster size is the same as the blocksize or 233930f712c9SDave Chinner * smaller we get to the buffer by simple arithmetics. 234030f712c9SDave Chinner */ 2341ef325959SDarrick J. Wong if (M_IGEO(mp)->blocks_per_cluster == 1) { 234230f712c9SDave Chinner offset = XFS_INO_TO_OFFSET(mp, ino); 234330f712c9SDave Chinner ASSERT(offset < mp->m_sb.sb_inopblock); 234430f712c9SDave Chinner 23457b13c515SDave Chinner imap->im_blkno = XFS_AGB_TO_DADDR(mp, pag->pag_agno, agbno); 234630f712c9SDave Chinner imap->im_len = XFS_FSB_TO_BB(mp, 1); 2347755c7bf5SDarrick J. Wong imap->im_boffset = (unsigned short)(offset << 2348755c7bf5SDarrick J. Wong mp->m_sb.sb_inodelog); 23497b13c515SDave Chinner error = 0; 23507b13c515SDave Chinner goto out_drop; 235130f712c9SDave Chinner } 235230f712c9SDave Chinner 235330f712c9SDave Chinner /* 235430f712c9SDave Chinner * If the inode chunks are aligned then use simple maths to 235530f712c9SDave Chinner * find the location. Otherwise we have to do a btree 235630f712c9SDave Chinner * lookup to find the location. 235730f712c9SDave Chinner */ 2358ef325959SDarrick J. Wong if (M_IGEO(mp)->inoalign_mask) { 2359ef325959SDarrick J. Wong offset_agbno = agbno & M_IGEO(mp)->inoalign_mask; 236030f712c9SDave Chinner chunk_agbno = agbno - offset_agbno; 236130f712c9SDave Chinner } else { 23627b13c515SDave Chinner error = xfs_imap_lookup(mp, tp, pag, agino, agbno, 236330f712c9SDave Chinner &chunk_agbno, &offset_agbno, flags); 236430f712c9SDave Chinner if (error) 23657b13c515SDave Chinner goto out_drop; 236630f712c9SDave Chinner } 236730f712c9SDave Chinner 236830f712c9SDave Chinner out_map: 236930f712c9SDave Chinner ASSERT(agbno >= chunk_agbno); 237030f712c9SDave Chinner cluster_agbno = chunk_agbno + 2371ef325959SDarrick J. Wong ((offset_agbno / M_IGEO(mp)->blocks_per_cluster) * 2372ef325959SDarrick J. Wong M_IGEO(mp)->blocks_per_cluster); 237330f712c9SDave Chinner offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) + 237430f712c9SDave Chinner XFS_INO_TO_OFFSET(mp, ino); 237530f712c9SDave Chinner 23767b13c515SDave Chinner imap->im_blkno = XFS_AGB_TO_DADDR(mp, pag->pag_agno, cluster_agbno); 2377ef325959SDarrick J. Wong imap->im_len = XFS_FSB_TO_BB(mp, M_IGEO(mp)->blocks_per_cluster); 2378755c7bf5SDarrick J. Wong imap->im_boffset = (unsigned short)(offset << mp->m_sb.sb_inodelog); 237930f712c9SDave Chinner 238030f712c9SDave Chinner /* 238130f712c9SDave Chinner * If the inode number maps to a block outside the bounds 238230f712c9SDave Chinner * of the file system then return NULL rather than calling 238330f712c9SDave Chinner * read_buf and panicing when we get an error from the 238430f712c9SDave Chinner * driver. 238530f712c9SDave Chinner */ 238630f712c9SDave Chinner if ((imap->im_blkno + imap->im_len) > 238730f712c9SDave Chinner XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) { 238830f712c9SDave Chinner xfs_alert(mp, 238930f712c9SDave Chinner "%s: (im_blkno (0x%llx) + im_len (0x%llx)) > sb_dblocks (0x%llx)", 239030f712c9SDave Chinner __func__, (unsigned long long) imap->im_blkno, 239130f712c9SDave Chinner (unsigned long long) imap->im_len, 239230f712c9SDave Chinner XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)); 23937b13c515SDave Chinner error = -EINVAL; 23947b13c515SDave Chinner goto out_drop; 239530f712c9SDave Chinner } 23967b13c515SDave Chinner error = 0; 23977b13c515SDave Chinner out_drop: 239890e2c1c2SDave Chinner if (pag) 23997b13c515SDave Chinner xfs_perag_put(pag); 24007b13c515SDave Chinner return error; 240130f712c9SDave Chinner } 240230f712c9SDave Chinner 240330f712c9SDave Chinner /* 240430f712c9SDave Chinner * Log specified fields for the ag hdr (inode section). The growth of the agi 240530f712c9SDave Chinner * structure over time requires that we interpret the buffer as two logical 240630f712c9SDave Chinner * regions delineated by the end of the unlinked list. This is due to the size 240730f712c9SDave Chinner * of the hash table and its location in the middle of the agi. 240830f712c9SDave Chinner * 240930f712c9SDave Chinner * For example, a request to log a field before agi_unlinked and a field after 241030f712c9SDave Chinner * agi_unlinked could cause us to log the entire hash table and use an excessive 241130f712c9SDave Chinner * amount of log space. To avoid this behavior, log the region up through 241230f712c9SDave Chinner * agi_unlinked in one call and the region after agi_unlinked through the end of 241330f712c9SDave Chinner * the structure in another. 241430f712c9SDave Chinner */ 241530f712c9SDave Chinner void 241630f712c9SDave Chinner xfs_ialloc_log_agi( 2417*0d1b9769SDave Chinner struct xfs_trans *tp, 2418*0d1b9769SDave Chinner struct xfs_buf *bp, 2419*0d1b9769SDave Chinner uint32_t fields) 242030f712c9SDave Chinner { 242130f712c9SDave Chinner int first; /* first byte number */ 242230f712c9SDave Chinner int last; /* last byte number */ 242330f712c9SDave Chinner static const short offsets[] = { /* field starting offsets */ 242430f712c9SDave Chinner /* keep in sync with bit definitions */ 242530f712c9SDave Chinner offsetof(xfs_agi_t, agi_magicnum), 242630f712c9SDave Chinner offsetof(xfs_agi_t, agi_versionnum), 242730f712c9SDave Chinner offsetof(xfs_agi_t, agi_seqno), 242830f712c9SDave Chinner offsetof(xfs_agi_t, agi_length), 242930f712c9SDave Chinner offsetof(xfs_agi_t, agi_count), 243030f712c9SDave Chinner offsetof(xfs_agi_t, agi_root), 243130f712c9SDave Chinner offsetof(xfs_agi_t, agi_level), 243230f712c9SDave Chinner offsetof(xfs_agi_t, agi_freecount), 243330f712c9SDave Chinner offsetof(xfs_agi_t, agi_newino), 243430f712c9SDave Chinner offsetof(xfs_agi_t, agi_dirino), 243530f712c9SDave Chinner offsetof(xfs_agi_t, agi_unlinked), 243630f712c9SDave Chinner offsetof(xfs_agi_t, agi_free_root), 243730f712c9SDave Chinner offsetof(xfs_agi_t, agi_free_level), 24382a39946cSDarrick J. Wong offsetof(xfs_agi_t, agi_iblocks), 243930f712c9SDave Chinner sizeof(xfs_agi_t) 244030f712c9SDave Chinner }; 244130f712c9SDave Chinner #ifdef DEBUG 2442370c782bSChristoph Hellwig struct xfs_agi *agi = bp->b_addr; 244330f712c9SDave Chinner 244430f712c9SDave Chinner ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC)); 244530f712c9SDave Chinner #endif 244630f712c9SDave Chinner 244730f712c9SDave Chinner /* 244830f712c9SDave Chinner * Compute byte offsets for the first and last fields in the first 244930f712c9SDave Chinner * region and log the agi buffer. This only logs up through 245030f712c9SDave Chinner * agi_unlinked. 245130f712c9SDave Chinner */ 245230f712c9SDave Chinner if (fields & XFS_AGI_ALL_BITS_R1) { 245330f712c9SDave Chinner xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R1, 245430f712c9SDave Chinner &first, &last); 245530f712c9SDave Chinner xfs_trans_log_buf(tp, bp, first, last); 245630f712c9SDave Chinner } 245730f712c9SDave Chinner 245830f712c9SDave Chinner /* 245930f712c9SDave Chinner * Mask off the bits in the first region and calculate the first and 246030f712c9SDave Chinner * last field offsets for any bits in the second region. 246130f712c9SDave Chinner */ 246230f712c9SDave Chinner fields &= ~XFS_AGI_ALL_BITS_R1; 246330f712c9SDave Chinner if (fields) { 246430f712c9SDave Chinner xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R2, 246530f712c9SDave Chinner &first, &last); 246630f712c9SDave Chinner xfs_trans_log_buf(tp, bp, first, last); 246730f712c9SDave Chinner } 246830f712c9SDave Chinner } 246930f712c9SDave Chinner 2470a6a781a5SDarrick J. Wong static xfs_failaddr_t 247130f712c9SDave Chinner xfs_agi_verify( 247230f712c9SDave Chinner struct xfs_buf *bp) 247330f712c9SDave Chinner { 2474dbd329f1SChristoph Hellwig struct xfs_mount *mp = bp->b_mount; 2475370c782bSChristoph Hellwig struct xfs_agi *agi = bp->b_addr; 24769f96cc95SDave Chinner int i; 247730f712c9SDave Chinner 247838c26bfdSDave Chinner if (xfs_has_crc(mp)) { 2479a45086e2SBrian Foster if (!uuid_equal(&agi->agi_uuid, &mp->m_sb.sb_meta_uuid)) 2480a6a781a5SDarrick J. Wong return __this_address; 2481370c782bSChristoph Hellwig if (!xfs_log_check_lsn(mp, be64_to_cpu(agi->agi_lsn))) 2482a6a781a5SDarrick J. Wong return __this_address; 2483a45086e2SBrian Foster } 2484a45086e2SBrian Foster 248530f712c9SDave Chinner /* 248630f712c9SDave Chinner * Validate the magic number of the agi block. 248730f712c9SDave Chinner */ 248839708c20SBrian Foster if (!xfs_verify_magic(bp, agi->agi_magicnum)) 2489a6a781a5SDarrick J. Wong return __this_address; 249030f712c9SDave Chinner if (!XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum))) 2491a6a781a5SDarrick J. Wong return __this_address; 249230f712c9SDave Chinner 2493d2a047f3SDarrick J. Wong if (be32_to_cpu(agi->agi_level) < 1 || 2494973975b7SDarrick J. Wong be32_to_cpu(agi->agi_level) > M_IGEO(mp)->inobt_maxlevels) 2495a6a781a5SDarrick J. Wong return __this_address; 2496d2a047f3SDarrick J. Wong 249738c26bfdSDave Chinner if (xfs_has_finobt(mp) && 2498d2a047f3SDarrick J. Wong (be32_to_cpu(agi->agi_free_level) < 1 || 2499973975b7SDarrick J. Wong be32_to_cpu(agi->agi_free_level) > M_IGEO(mp)->inobt_maxlevels)) 2500a6a781a5SDarrick J. Wong return __this_address; 2501d2a047f3SDarrick J. Wong 250230f712c9SDave Chinner /* 250330f712c9SDave Chinner * during growfs operations, the perag is not fully initialised, 250430f712c9SDave Chinner * so we can't use it for any useful checking. growfs ensures we can't 250530f712c9SDave Chinner * use it by using uncached buffers that don't have the perag attached 250630f712c9SDave Chinner * so we can detect and avoid this problem. 250730f712c9SDave Chinner */ 250830f712c9SDave Chinner if (bp->b_pag && be32_to_cpu(agi->agi_seqno) != bp->b_pag->pag_agno) 2509a6a781a5SDarrick J. Wong return __this_address; 251030f712c9SDave Chinner 25119f96cc95SDave Chinner for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++) { 25125089eaffSCarlos Maiolino if (agi->agi_unlinked[i] == cpu_to_be32(NULLAGINO)) 25139f96cc95SDave Chinner continue; 25149f96cc95SDave Chinner if (!xfs_verify_ino(mp, be32_to_cpu(agi->agi_unlinked[i]))) 25159f96cc95SDave Chinner return __this_address; 25169f96cc95SDave Chinner } 25179f96cc95SDave Chinner 2518a6a781a5SDarrick J. Wong return NULL; 251930f712c9SDave Chinner } 252030f712c9SDave Chinner 252130f712c9SDave Chinner static void 252230f712c9SDave Chinner xfs_agi_read_verify( 252330f712c9SDave Chinner struct xfs_buf *bp) 252430f712c9SDave Chinner { 2525dbd329f1SChristoph Hellwig struct xfs_mount *mp = bp->b_mount; 2526bc1a09b8SDarrick J. Wong xfs_failaddr_t fa; 252730f712c9SDave Chinner 252838c26bfdSDave Chinner if (xfs_has_crc(mp) && 252930f712c9SDave Chinner !xfs_buf_verify_cksum(bp, XFS_AGI_CRC_OFF)) 2530bc1a09b8SDarrick J. Wong xfs_verifier_error(bp, -EFSBADCRC, __this_address); 2531bc1a09b8SDarrick J. Wong else { 2532bc1a09b8SDarrick J. Wong fa = xfs_agi_verify(bp); 2533bc1a09b8SDarrick J. Wong if (XFS_TEST_ERROR(fa, mp, XFS_ERRTAG_IALLOC_READ_AGI)) 2534bc1a09b8SDarrick J. Wong xfs_verifier_error(bp, -EFSCORRUPTED, fa); 2535bc1a09b8SDarrick J. Wong } 253630f712c9SDave Chinner } 253730f712c9SDave Chinner 253830f712c9SDave Chinner static void 253930f712c9SDave Chinner xfs_agi_write_verify( 254030f712c9SDave Chinner struct xfs_buf *bp) 254130f712c9SDave Chinner { 2542dbd329f1SChristoph Hellwig struct xfs_mount *mp = bp->b_mount; 2543fb1755a6SCarlos Maiolino struct xfs_buf_log_item *bip = bp->b_log_item; 2544370c782bSChristoph Hellwig struct xfs_agi *agi = bp->b_addr; 2545bc1a09b8SDarrick J. Wong xfs_failaddr_t fa; 254630f712c9SDave Chinner 2547bc1a09b8SDarrick J. Wong fa = xfs_agi_verify(bp); 2548bc1a09b8SDarrick J. Wong if (fa) { 2549bc1a09b8SDarrick J. Wong xfs_verifier_error(bp, -EFSCORRUPTED, fa); 255030f712c9SDave Chinner return; 255130f712c9SDave Chinner } 255230f712c9SDave Chinner 255338c26bfdSDave Chinner if (!xfs_has_crc(mp)) 255430f712c9SDave Chinner return; 255530f712c9SDave Chinner 255630f712c9SDave Chinner if (bip) 2557370c782bSChristoph Hellwig agi->agi_lsn = cpu_to_be64(bip->bli_item.li_lsn); 255830f712c9SDave Chinner xfs_buf_update_cksum(bp, XFS_AGI_CRC_OFF); 255930f712c9SDave Chinner } 256030f712c9SDave Chinner 256130f712c9SDave Chinner const struct xfs_buf_ops xfs_agi_buf_ops = { 2562233135b7SEric Sandeen .name = "xfs_agi", 256339708c20SBrian Foster .magic = { cpu_to_be32(XFS_AGI_MAGIC), cpu_to_be32(XFS_AGI_MAGIC) }, 256430f712c9SDave Chinner .verify_read = xfs_agi_read_verify, 256530f712c9SDave Chinner .verify_write = xfs_agi_write_verify, 2566b5572597SDarrick J. Wong .verify_struct = xfs_agi_verify, 256730f712c9SDave Chinner }; 256830f712c9SDave Chinner 256930f712c9SDave Chinner /* 257030f712c9SDave Chinner * Read in the allocation group header (inode allocation section) 257130f712c9SDave Chinner */ 257230f712c9SDave Chinner int 257330f712c9SDave Chinner xfs_read_agi( 257430f712c9SDave Chinner struct xfs_mount *mp, /* file system mount structure */ 257530f712c9SDave Chinner struct xfs_trans *tp, /* transaction pointer */ 257630f712c9SDave Chinner xfs_agnumber_t agno, /* allocation group number */ 257730f712c9SDave Chinner struct xfs_buf **bpp) /* allocation group hdr buf */ 257830f712c9SDave Chinner { 257930f712c9SDave Chinner int error; 258030f712c9SDave Chinner 258130f712c9SDave Chinner trace_xfs_read_agi(mp, agno); 258230f712c9SDave Chinner 258330f712c9SDave Chinner ASSERT(agno != NULLAGNUMBER); 258430f712c9SDave Chinner error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, 258530f712c9SDave Chinner XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)), 258630f712c9SDave Chinner XFS_FSS_TO_BB(mp, 1), 0, bpp, &xfs_agi_buf_ops); 258730f712c9SDave Chinner if (error) 258830f712c9SDave Chinner return error; 2589200237d6SEric Sandeen if (tp) 2590200237d6SEric Sandeen xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_AGI_BUF); 259130f712c9SDave Chinner 259230f712c9SDave Chinner xfs_buf_set_ref(*bpp, XFS_AGI_REF); 259330f712c9SDave Chinner return 0; 259430f712c9SDave Chinner } 259530f712c9SDave Chinner 259630f712c9SDave Chinner int 259730f712c9SDave Chinner xfs_ialloc_read_agi( 259830f712c9SDave Chinner struct xfs_mount *mp, /* file system mount structure */ 259930f712c9SDave Chinner struct xfs_trans *tp, /* transaction pointer */ 260030f712c9SDave Chinner xfs_agnumber_t agno, /* allocation group number */ 260130f712c9SDave Chinner struct xfs_buf **bpp) /* allocation group hdr buf */ 260230f712c9SDave Chinner { 260330f712c9SDave Chinner struct xfs_agi *agi; /* allocation group header */ 260430f712c9SDave Chinner struct xfs_perag *pag; /* per allocation group data */ 260530f712c9SDave Chinner int error; 260630f712c9SDave Chinner 260730f712c9SDave Chinner trace_xfs_ialloc_read_agi(mp, agno); 260830f712c9SDave Chinner 260930f712c9SDave Chinner error = xfs_read_agi(mp, tp, agno, bpp); 261030f712c9SDave Chinner if (error) 261130f712c9SDave Chinner return error; 261230f712c9SDave Chinner 2613370c782bSChristoph Hellwig agi = (*bpp)->b_addr; 261492a00544SGao Xiang pag = (*bpp)->b_pag; 261530f712c9SDave Chinner if (!pag->pagi_init) { 261630f712c9SDave Chinner pag->pagi_freecount = be32_to_cpu(agi->agi_freecount); 261730f712c9SDave Chinner pag->pagi_count = be32_to_cpu(agi->agi_count); 261830f712c9SDave Chinner pag->pagi_init = 1; 261930f712c9SDave Chinner } 262030f712c9SDave Chinner 262130f712c9SDave Chinner /* 262230f712c9SDave Chinner * It's possible for these to be out of sync if 262330f712c9SDave Chinner * we are in the middle of a forced shutdown. 262430f712c9SDave Chinner */ 262530f712c9SDave Chinner ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) || 262675c8c50fSDave Chinner xfs_is_shutdown(mp)); 262730f712c9SDave Chinner return 0; 262830f712c9SDave Chinner } 262930f712c9SDave Chinner 263030f712c9SDave Chinner /* 263130f712c9SDave Chinner * Read in the agi to initialise the per-ag data in the mount structure 263230f712c9SDave Chinner */ 263330f712c9SDave Chinner int 263430f712c9SDave Chinner xfs_ialloc_pagi_init( 263530f712c9SDave Chinner xfs_mount_t *mp, /* file system mount structure */ 263630f712c9SDave Chinner xfs_trans_t *tp, /* transaction pointer */ 263730f712c9SDave Chinner xfs_agnumber_t agno) /* allocation group number */ 263830f712c9SDave Chinner { 2639e8222613SDave Chinner struct xfs_buf *bp = NULL; 264030f712c9SDave Chinner int error; 264130f712c9SDave Chinner 264230f712c9SDave Chinner error = xfs_ialloc_read_agi(mp, tp, agno, &bp); 264330f712c9SDave Chinner if (error) 264430f712c9SDave Chinner return error; 264530f712c9SDave Chinner if (bp) 264630f712c9SDave Chinner xfs_trans_brelse(tp, bp); 264730f712c9SDave Chinner return 0; 264830f712c9SDave Chinner } 264991fb9afcSDarrick J. Wong 26502e001266SDarrick J. Wong /* Is there an inode record covering a given range of inode numbers? */ 26512e001266SDarrick J. Wong int 26522e001266SDarrick J. Wong xfs_ialloc_has_inode_record( 26532e001266SDarrick J. Wong struct xfs_btree_cur *cur, 26542e001266SDarrick J. Wong xfs_agino_t low, 26552e001266SDarrick J. Wong xfs_agino_t high, 26562e001266SDarrick J. Wong bool *exists) 26572e001266SDarrick J. Wong { 26582e001266SDarrick J. Wong struct xfs_inobt_rec_incore irec; 26592e001266SDarrick J. Wong xfs_agino_t agino; 26602e001266SDarrick J. Wong uint16_t holemask; 26612e001266SDarrick J. Wong int has_record; 26622e001266SDarrick J. Wong int i; 26632e001266SDarrick J. Wong int error; 26642e001266SDarrick J. Wong 26652e001266SDarrick J. Wong *exists = false; 26662e001266SDarrick J. Wong error = xfs_inobt_lookup(cur, low, XFS_LOOKUP_LE, &has_record); 26672e001266SDarrick J. Wong while (error == 0 && has_record) { 26682e001266SDarrick J. Wong error = xfs_inobt_get_rec(cur, &irec, &has_record); 26692e001266SDarrick J. Wong if (error || irec.ir_startino > high) 26702e001266SDarrick J. Wong break; 26712e001266SDarrick J. Wong 26722e001266SDarrick J. Wong agino = irec.ir_startino; 26732e001266SDarrick J. Wong holemask = irec.ir_holemask; 26742e001266SDarrick J. Wong for (i = 0; i < XFS_INOBT_HOLEMASK_BITS; holemask >>= 1, 26752e001266SDarrick J. Wong i++, agino += XFS_INODES_PER_HOLEMASK_BIT) { 26762e001266SDarrick J. Wong if (holemask & 1) 26772e001266SDarrick J. Wong continue; 26782e001266SDarrick J. Wong if (agino + XFS_INODES_PER_HOLEMASK_BIT > low && 26792e001266SDarrick J. Wong agino <= high) { 26802e001266SDarrick J. Wong *exists = true; 26812e001266SDarrick J. Wong return 0; 26822e001266SDarrick J. Wong } 26832e001266SDarrick J. Wong } 26842e001266SDarrick J. Wong 26852e001266SDarrick J. Wong error = xfs_btree_increment(cur, 0, &has_record); 26862e001266SDarrick J. Wong } 26872e001266SDarrick J. Wong return error; 26882e001266SDarrick J. Wong } 26892e001266SDarrick J. Wong 26902e001266SDarrick J. Wong /* Is there an inode record covering a given extent? */ 26912e001266SDarrick J. Wong int 26922e001266SDarrick J. Wong xfs_ialloc_has_inodes_at_extent( 26932e001266SDarrick J. Wong struct xfs_btree_cur *cur, 26942e001266SDarrick J. Wong xfs_agblock_t bno, 26952e001266SDarrick J. Wong xfs_extlen_t len, 26962e001266SDarrick J. Wong bool *exists) 26972e001266SDarrick J. Wong { 26982e001266SDarrick J. Wong xfs_agino_t low; 26992e001266SDarrick J. Wong xfs_agino_t high; 27002e001266SDarrick J. Wong 270143004b2aSDarrick J. Wong low = XFS_AGB_TO_AGINO(cur->bc_mp, bno); 270243004b2aSDarrick J. Wong high = XFS_AGB_TO_AGINO(cur->bc_mp, bno + len) - 1; 27032e001266SDarrick J. Wong 27042e001266SDarrick J. Wong return xfs_ialloc_has_inode_record(cur, low, high, exists); 27052e001266SDarrick J. Wong } 27062e001266SDarrick J. Wong 27072e001266SDarrick J. Wong struct xfs_ialloc_count_inodes { 27082e001266SDarrick J. Wong xfs_agino_t count; 27092e001266SDarrick J. Wong xfs_agino_t freecount; 27102e001266SDarrick J. Wong }; 27112e001266SDarrick J. Wong 27122e001266SDarrick J. Wong /* Record inode counts across all inobt records. */ 27132e001266SDarrick J. Wong STATIC int 27142e001266SDarrick J. Wong xfs_ialloc_count_inodes_rec( 27152e001266SDarrick J. Wong struct xfs_btree_cur *cur, 2716159eb69dSDarrick J. Wong const union xfs_btree_rec *rec, 27172e001266SDarrick J. Wong void *priv) 27182e001266SDarrick J. Wong { 27192e001266SDarrick J. Wong struct xfs_inobt_rec_incore irec; 27202e001266SDarrick J. Wong struct xfs_ialloc_count_inodes *ci = priv; 27212e001266SDarrick J. Wong 27222e001266SDarrick J. Wong xfs_inobt_btrec_to_irec(cur->bc_mp, rec, &irec); 27232e001266SDarrick J. Wong ci->count += irec.ir_count; 27242e001266SDarrick J. Wong ci->freecount += irec.ir_freecount; 27252e001266SDarrick J. Wong 27262e001266SDarrick J. Wong return 0; 27272e001266SDarrick J. Wong } 27282e001266SDarrick J. Wong 27292e001266SDarrick J. Wong /* Count allocated and free inodes under an inobt. */ 27302e001266SDarrick J. Wong int 27312e001266SDarrick J. Wong xfs_ialloc_count_inodes( 27322e001266SDarrick J. Wong struct xfs_btree_cur *cur, 27332e001266SDarrick J. Wong xfs_agino_t *count, 27342e001266SDarrick J. Wong xfs_agino_t *freecount) 27352e001266SDarrick J. Wong { 27362e001266SDarrick J. Wong struct xfs_ialloc_count_inodes ci = {0}; 27372e001266SDarrick J. Wong int error; 27382e001266SDarrick J. Wong 27392e001266SDarrick J. Wong ASSERT(cur->bc_btnum == XFS_BTNUM_INO); 27402e001266SDarrick J. Wong error = xfs_btree_query_all(cur, xfs_ialloc_count_inodes_rec, &ci); 27412e001266SDarrick J. Wong if (error) 27422e001266SDarrick J. Wong return error; 27432e001266SDarrick J. Wong 27442e001266SDarrick J. Wong *count = ci.count; 27452e001266SDarrick J. Wong *freecount = ci.freecount; 27462e001266SDarrick J. Wong return 0; 27472e001266SDarrick J. Wong } 2748494dba7bSDarrick J. Wong 2749494dba7bSDarrick J. Wong /* 2750494dba7bSDarrick J. Wong * Initialize inode-related geometry information. 2751494dba7bSDarrick J. Wong * 2752494dba7bSDarrick J. Wong * Compute the inode btree min and max levels and set maxicount. 2753494dba7bSDarrick J. Wong * 2754494dba7bSDarrick J. Wong * Set the inode cluster size. This may still be overridden by the file 2755494dba7bSDarrick J. Wong * system block size if it is larger than the chosen cluster size. 2756494dba7bSDarrick J. Wong * 2757494dba7bSDarrick J. Wong * For v5 filesystems, scale the cluster size with the inode size to keep a 2758494dba7bSDarrick J. Wong * constant ratio of inode per cluster buffer, but only if mkfs has set the 2759494dba7bSDarrick J. Wong * inode alignment value appropriately for larger cluster sizes. 2760494dba7bSDarrick J. Wong * 2761494dba7bSDarrick J. Wong * Then compute the inode cluster alignment information. 2762494dba7bSDarrick J. Wong */ 2763494dba7bSDarrick J. Wong void 2764494dba7bSDarrick J. Wong xfs_ialloc_setup_geometry( 2765494dba7bSDarrick J. Wong struct xfs_mount *mp) 2766494dba7bSDarrick J. Wong { 2767494dba7bSDarrick J. Wong struct xfs_sb *sbp = &mp->m_sb; 2768494dba7bSDarrick J. Wong struct xfs_ino_geometry *igeo = M_IGEO(mp); 2769494dba7bSDarrick J. Wong uint64_t icount; 2770494dba7bSDarrick J. Wong uint inodes; 2771494dba7bSDarrick J. Wong 2772f93e5436SDarrick J. Wong igeo->new_diflags2 = 0; 2773ebd9027dSDave Chinner if (xfs_has_bigtime(mp)) 2774f93e5436SDarrick J. Wong igeo->new_diflags2 |= XFS_DIFLAG2_BIGTIME; 2775f93e5436SDarrick J. Wong 2776494dba7bSDarrick J. Wong /* Compute inode btree geometry. */ 2777494dba7bSDarrick J. Wong igeo->agino_log = sbp->sb_inopblog + sbp->sb_agblklog; 2778494dba7bSDarrick J. Wong igeo->inobt_mxr[0] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 1); 2779494dba7bSDarrick J. Wong igeo->inobt_mxr[1] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 0); 2780494dba7bSDarrick J. Wong igeo->inobt_mnr[0] = igeo->inobt_mxr[0] / 2; 2781494dba7bSDarrick J. Wong igeo->inobt_mnr[1] = igeo->inobt_mxr[1] / 2; 2782494dba7bSDarrick J. Wong 2783494dba7bSDarrick J. Wong igeo->ialloc_inos = max_t(uint16_t, XFS_INODES_PER_CHUNK, 2784494dba7bSDarrick J. Wong sbp->sb_inopblock); 2785494dba7bSDarrick J. Wong igeo->ialloc_blks = igeo->ialloc_inos >> sbp->sb_inopblog; 2786494dba7bSDarrick J. Wong 2787494dba7bSDarrick J. Wong if (sbp->sb_spino_align) 2788494dba7bSDarrick J. Wong igeo->ialloc_min_blks = sbp->sb_spino_align; 2789494dba7bSDarrick J. Wong else 2790494dba7bSDarrick J. Wong igeo->ialloc_min_blks = igeo->ialloc_blks; 2791494dba7bSDarrick J. Wong 2792494dba7bSDarrick J. Wong /* Compute and fill in value of m_ino_geo.inobt_maxlevels. */ 2793494dba7bSDarrick J. Wong inodes = (1LL << XFS_INO_AGINO_BITS(mp)) >> XFS_INODES_PER_CHUNK_LOG; 2794494dba7bSDarrick J. Wong igeo->inobt_maxlevels = xfs_btree_compute_maxlevels(igeo->inobt_mnr, 2795494dba7bSDarrick J. Wong inodes); 27960ed5f735SDarrick J. Wong ASSERT(igeo->inobt_maxlevels <= xfs_iallocbt_maxlevels_ondisk()); 2797494dba7bSDarrick J. Wong 2798c94613feSDarrick J. Wong /* 2799c94613feSDarrick J. Wong * Set the maximum inode count for this filesystem, being careful not 2800c94613feSDarrick J. Wong * to use obviously garbage sb_inopblog/sb_inopblock values. Regular 2801c94613feSDarrick J. Wong * users should never get here due to failing sb verification, but 2802c94613feSDarrick J. Wong * certain users (xfs_db) need to be usable even with corrupt metadata. 2803c94613feSDarrick J. Wong */ 2804c94613feSDarrick J. Wong if (sbp->sb_imax_pct && igeo->ialloc_blks) { 2805494dba7bSDarrick J. Wong /* 2806494dba7bSDarrick J. Wong * Make sure the maximum inode count is a multiple 2807494dba7bSDarrick J. Wong * of the units we allocate inodes in. 2808494dba7bSDarrick J. Wong */ 2809494dba7bSDarrick J. Wong icount = sbp->sb_dblocks * sbp->sb_imax_pct; 2810494dba7bSDarrick J. Wong do_div(icount, 100); 2811494dba7bSDarrick J. Wong do_div(icount, igeo->ialloc_blks); 2812494dba7bSDarrick J. Wong igeo->maxicount = XFS_FSB_TO_INO(mp, 2813494dba7bSDarrick J. Wong icount * igeo->ialloc_blks); 2814494dba7bSDarrick J. Wong } else { 2815494dba7bSDarrick J. Wong igeo->maxicount = 0; 2816494dba7bSDarrick J. Wong } 2817494dba7bSDarrick J. Wong 2818490d451fSDarrick J. Wong /* 2819490d451fSDarrick J. Wong * Compute the desired size of an inode cluster buffer size, which 2820490d451fSDarrick J. Wong * starts at 8K and (on v5 filesystems) scales up with larger inode 2821490d451fSDarrick J. Wong * sizes. 2822490d451fSDarrick J. Wong * 2823490d451fSDarrick J. Wong * Preserve the desired inode cluster size because the sparse inodes 2824490d451fSDarrick J. Wong * feature uses that desired size (not the actual size) to compute the 2825490d451fSDarrick J. Wong * sparse inode alignment. The mount code validates this value, so we 2826490d451fSDarrick J. Wong * cannot change the behavior. 2827490d451fSDarrick J. Wong */ 2828490d451fSDarrick J. Wong igeo->inode_cluster_size_raw = XFS_INODE_BIG_CLUSTER_SIZE; 2829ebd9027dSDave Chinner if (xfs_has_v3inodes(mp)) { 2830490d451fSDarrick J. Wong int new_size = igeo->inode_cluster_size_raw; 2831494dba7bSDarrick J. Wong 2832494dba7bSDarrick J. Wong new_size *= mp->m_sb.sb_inodesize / XFS_DINODE_MIN_SIZE; 2833494dba7bSDarrick J. Wong if (mp->m_sb.sb_inoalignmt >= XFS_B_TO_FSBT(mp, new_size)) 2834490d451fSDarrick J. Wong igeo->inode_cluster_size_raw = new_size; 2835494dba7bSDarrick J. Wong } 2836494dba7bSDarrick J. Wong 2837494dba7bSDarrick J. Wong /* Calculate inode cluster ratios. */ 2838490d451fSDarrick J. Wong if (igeo->inode_cluster_size_raw > mp->m_sb.sb_blocksize) 2839494dba7bSDarrick J. Wong igeo->blocks_per_cluster = XFS_B_TO_FSBT(mp, 2840490d451fSDarrick J. Wong igeo->inode_cluster_size_raw); 2841494dba7bSDarrick J. Wong else 2842494dba7bSDarrick J. Wong igeo->blocks_per_cluster = 1; 2843490d451fSDarrick J. Wong igeo->inode_cluster_size = XFS_FSB_TO_B(mp, igeo->blocks_per_cluster); 2844494dba7bSDarrick J. Wong igeo->inodes_per_cluster = XFS_FSB_TO_INO(mp, igeo->blocks_per_cluster); 2845494dba7bSDarrick J. Wong 2846494dba7bSDarrick J. Wong /* Calculate inode cluster alignment. */ 2847ebd9027dSDave Chinner if (xfs_has_align(mp) && 2848494dba7bSDarrick J. Wong mp->m_sb.sb_inoalignmt >= igeo->blocks_per_cluster) 2849494dba7bSDarrick J. Wong igeo->cluster_align = mp->m_sb.sb_inoalignmt; 2850494dba7bSDarrick J. Wong else 2851494dba7bSDarrick J. Wong igeo->cluster_align = 1; 2852494dba7bSDarrick J. Wong igeo->inoalign_mask = igeo->cluster_align - 1; 2853494dba7bSDarrick J. Wong igeo->cluster_align_inodes = XFS_FSB_TO_INO(mp, igeo->cluster_align); 2854494dba7bSDarrick J. Wong 2855494dba7bSDarrick J. Wong /* 2856494dba7bSDarrick J. Wong * If we are using stripe alignment, check whether 2857494dba7bSDarrick J. Wong * the stripe unit is a multiple of the inode alignment 2858494dba7bSDarrick J. Wong */ 2859494dba7bSDarrick J. Wong if (mp->m_dalign && igeo->inoalign_mask && 2860494dba7bSDarrick J. Wong !(mp->m_dalign & igeo->inoalign_mask)) 2861494dba7bSDarrick J. Wong igeo->ialloc_align = mp->m_dalign; 2862494dba7bSDarrick J. Wong else 2863494dba7bSDarrick J. Wong igeo->ialloc_align = 0; 2864494dba7bSDarrick J. Wong } 286513eaec4bSDarrick J. Wong 286613eaec4bSDarrick J. Wong /* Compute the location of the root directory inode that is laid out by mkfs. */ 286713eaec4bSDarrick J. Wong xfs_ino_t 286813eaec4bSDarrick J. Wong xfs_ialloc_calc_rootino( 286913eaec4bSDarrick J. Wong struct xfs_mount *mp, 287013eaec4bSDarrick J. Wong int sunit) 287113eaec4bSDarrick J. Wong { 287213eaec4bSDarrick J. Wong struct xfs_ino_geometry *igeo = M_IGEO(mp); 287313eaec4bSDarrick J. Wong xfs_agblock_t first_bno; 287413eaec4bSDarrick J. Wong 287513eaec4bSDarrick J. Wong /* 287613eaec4bSDarrick J. Wong * Pre-calculate the geometry of AG 0. We know what it looks like 287713eaec4bSDarrick J. Wong * because libxfs knows how to create allocation groups now. 287813eaec4bSDarrick J. Wong * 287913eaec4bSDarrick J. Wong * first_bno is the first block in which mkfs could possibly have 288013eaec4bSDarrick J. Wong * allocated the root directory inode, once we factor in the metadata 288113eaec4bSDarrick J. Wong * that mkfs formats before it. Namely, the four AG headers... 288213eaec4bSDarrick J. Wong */ 288313eaec4bSDarrick J. Wong first_bno = howmany(4 * mp->m_sb.sb_sectsize, mp->m_sb.sb_blocksize); 288413eaec4bSDarrick J. Wong 288513eaec4bSDarrick J. Wong /* ...the two free space btree roots... */ 288613eaec4bSDarrick J. Wong first_bno += 2; 288713eaec4bSDarrick J. Wong 288813eaec4bSDarrick J. Wong /* ...the inode btree root... */ 288913eaec4bSDarrick J. Wong first_bno += 1; 289013eaec4bSDarrick J. Wong 289113eaec4bSDarrick J. Wong /* ...the initial AGFL... */ 289213eaec4bSDarrick J. Wong first_bno += xfs_alloc_min_freelist(mp, NULL); 289313eaec4bSDarrick J. Wong 289413eaec4bSDarrick J. Wong /* ...the free inode btree root... */ 2895ebd9027dSDave Chinner if (xfs_has_finobt(mp)) 289613eaec4bSDarrick J. Wong first_bno++; 289713eaec4bSDarrick J. Wong 289813eaec4bSDarrick J. Wong /* ...the reverse mapping btree root... */ 2899ebd9027dSDave Chinner if (xfs_has_rmapbt(mp)) 290013eaec4bSDarrick J. Wong first_bno++; 290113eaec4bSDarrick J. Wong 290213eaec4bSDarrick J. Wong /* ...the reference count btree... */ 2903ebd9027dSDave Chinner if (xfs_has_reflink(mp)) 290413eaec4bSDarrick J. Wong first_bno++; 290513eaec4bSDarrick J. Wong 290613eaec4bSDarrick J. Wong /* 290713eaec4bSDarrick J. Wong * ...and the log, if it is allocated in the first allocation group. 290813eaec4bSDarrick J. Wong * 290913eaec4bSDarrick J. Wong * This can happen with filesystems that only have a single 291013eaec4bSDarrick J. Wong * allocation group, or very odd geometries created by old mkfs 291113eaec4bSDarrick J. Wong * versions on very small filesystems. 291213eaec4bSDarrick J. Wong */ 291313eaec4bSDarrick J. Wong if (mp->m_sb.sb_logstart && 291413eaec4bSDarrick J. Wong XFS_FSB_TO_AGNO(mp, mp->m_sb.sb_logstart) == 0) 291513eaec4bSDarrick J. Wong first_bno += mp->m_sb.sb_logblocks; 291613eaec4bSDarrick J. Wong 291713eaec4bSDarrick J. Wong /* 291813eaec4bSDarrick J. Wong * Now round first_bno up to whatever allocation alignment is given 291913eaec4bSDarrick J. Wong * by the filesystem or was passed in. 292013eaec4bSDarrick J. Wong */ 2921ebd9027dSDave Chinner if (xfs_has_dalign(mp) && igeo->ialloc_align > 0) 292213eaec4bSDarrick J. Wong first_bno = roundup(first_bno, sunit); 2923ebd9027dSDave Chinner else if (xfs_has_align(mp) && 292413eaec4bSDarrick J. Wong mp->m_sb.sb_inoalignmt > 1) 292513eaec4bSDarrick J. Wong first_bno = roundup(first_bno, mp->m_sb.sb_inoalignmt); 292613eaec4bSDarrick J. Wong 292713eaec4bSDarrick J. Wong return XFS_AGINO_TO_INO(mp, 0, XFS_AGB_TO_AGINO(mp, first_bno)); 292813eaec4bSDarrick J. Wong } 2929da062d16SDarrick J. Wong 2930da062d16SDarrick J. Wong /* 2931da062d16SDarrick J. Wong * Ensure there are not sparse inode clusters that cross the new EOAG. 2932da062d16SDarrick J. Wong * 2933da062d16SDarrick J. Wong * This is a no-op for non-spinode filesystems since clusters are always fully 2934da062d16SDarrick J. Wong * allocated and checking the bnobt suffices. However, a spinode filesystem 2935da062d16SDarrick J. Wong * could have a record where the upper inodes are free blocks. If those blocks 2936da062d16SDarrick J. Wong * were removed from the filesystem, the inode record would extend beyond EOAG, 2937da062d16SDarrick J. Wong * which will be flagged as corruption. 2938da062d16SDarrick J. Wong */ 2939da062d16SDarrick J. Wong int 2940da062d16SDarrick J. Wong xfs_ialloc_check_shrink( 2941da062d16SDarrick J. Wong struct xfs_trans *tp, 2942da062d16SDarrick J. Wong xfs_agnumber_t agno, 2943da062d16SDarrick J. Wong struct xfs_buf *agibp, 2944da062d16SDarrick J. Wong xfs_agblock_t new_length) 2945da062d16SDarrick J. Wong { 2946da062d16SDarrick J. Wong struct xfs_inobt_rec_incore rec; 2947da062d16SDarrick J. Wong struct xfs_btree_cur *cur; 2948da062d16SDarrick J. Wong struct xfs_mount *mp = tp->t_mountp; 2949da062d16SDarrick J. Wong struct xfs_perag *pag; 2950da062d16SDarrick J. Wong xfs_agino_t agino = XFS_AGB_TO_AGINO(mp, new_length); 2951da062d16SDarrick J. Wong int has; 2952da062d16SDarrick J. Wong int error; 2953da062d16SDarrick J. Wong 295438c26bfdSDave Chinner if (!xfs_has_sparseinodes(mp)) 2955da062d16SDarrick J. Wong return 0; 2956da062d16SDarrick J. Wong 2957da062d16SDarrick J. Wong pag = xfs_perag_get(mp, agno); 2958da062d16SDarrick J. Wong cur = xfs_inobt_init_cursor(mp, tp, agibp, pag, XFS_BTNUM_INO); 2959da062d16SDarrick J. Wong 2960da062d16SDarrick J. Wong /* Look up the inobt record that would correspond to the new EOFS. */ 2961da062d16SDarrick J. Wong error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &has); 2962da062d16SDarrick J. Wong if (error || !has) 2963da062d16SDarrick J. Wong goto out; 2964da062d16SDarrick J. Wong 2965da062d16SDarrick J. Wong error = xfs_inobt_get_rec(cur, &rec, &has); 2966da062d16SDarrick J. Wong if (error) 2967da062d16SDarrick J. Wong goto out; 2968da062d16SDarrick J. Wong 2969da062d16SDarrick J. Wong if (!has) { 2970da062d16SDarrick J. Wong error = -EFSCORRUPTED; 2971da062d16SDarrick J. Wong goto out; 2972da062d16SDarrick J. Wong } 2973da062d16SDarrick J. Wong 2974da062d16SDarrick J. Wong /* If the record covers inodes that would be beyond EOFS, bail out. */ 2975da062d16SDarrick J. Wong if (rec.ir_startino + XFS_INODES_PER_CHUNK > agino) { 2976da062d16SDarrick J. Wong error = -ENOSPC; 2977da062d16SDarrick J. Wong goto out; 2978da062d16SDarrick J. Wong } 2979da062d16SDarrick J. Wong out: 2980da062d16SDarrick J. Wong xfs_btree_del_cursor(cur, error); 2981da062d16SDarrick J. Wong xfs_perag_put(pag); 2982da062d16SDarrick J. Wong return error; 2983da062d16SDarrick J. Wong } 2984