Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 2 | * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc. |
| 3 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License as |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * published by the Free Software Foundation. |
| 8 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 9 | * This program is distributed in the hope that it would be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write the Free Software Foundation, |
| 16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "xfs.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 19 | #include "xfs_fs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include "xfs_types.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 21 | #include "xfs_bit.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include "xfs_log.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 23 | #include "xfs_inum.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include "xfs_trans.h" |
| 25 | #include "xfs_sb.h" |
| 26 | #include "xfs_ag.h" |
| 27 | #include "xfs_dir.h" |
| 28 | #include "xfs_dir2.h" |
| 29 | #include "xfs_dmapi.h" |
| 30 | #include "xfs_mount.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include "xfs_bmap_btree.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 32 | #include "xfs_alloc_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include "xfs_ialloc_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include "xfs_dir_sf.h" |
| 35 | #include "xfs_dir2_sf.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 36 | #include "xfs_attr_sf.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include "xfs_dinode.h" |
| 38 | #include "xfs_inode.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 39 | #include "xfs_btree.h" |
| 40 | #include "xfs_ialloc.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include "xfs_alloc.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include "xfs_rtalloc.h" |
| 43 | #include "xfs_error.h" |
| 44 | #include "xfs_bmap.h" |
| 45 | |
| 46 | /* |
| 47 | * Log specified fields for the inode given by bp and off. |
| 48 | */ |
| 49 | STATIC void |
| 50 | xfs_ialloc_log_di( |
| 51 | xfs_trans_t *tp, /* transaction pointer */ |
| 52 | xfs_buf_t *bp, /* inode buffer */ |
| 53 | int off, /* index of inode in buffer */ |
| 54 | int fields) /* bitmask of fields to log */ |
| 55 | { |
| 56 | int first; /* first byte number */ |
| 57 | int ioffset; /* off in bytes */ |
| 58 | int last; /* last byte number */ |
| 59 | xfs_mount_t *mp; /* mount point structure */ |
| 60 | static const short offsets[] = { /* field offsets */ |
| 61 | /* keep in sync with bits */ |
| 62 | offsetof(xfs_dinode_core_t, di_magic), |
| 63 | offsetof(xfs_dinode_core_t, di_mode), |
| 64 | offsetof(xfs_dinode_core_t, di_version), |
| 65 | offsetof(xfs_dinode_core_t, di_format), |
| 66 | offsetof(xfs_dinode_core_t, di_onlink), |
| 67 | offsetof(xfs_dinode_core_t, di_uid), |
| 68 | offsetof(xfs_dinode_core_t, di_gid), |
| 69 | offsetof(xfs_dinode_core_t, di_nlink), |
| 70 | offsetof(xfs_dinode_core_t, di_projid), |
| 71 | offsetof(xfs_dinode_core_t, di_pad), |
| 72 | offsetof(xfs_dinode_core_t, di_atime), |
| 73 | offsetof(xfs_dinode_core_t, di_mtime), |
| 74 | offsetof(xfs_dinode_core_t, di_ctime), |
| 75 | offsetof(xfs_dinode_core_t, di_size), |
| 76 | offsetof(xfs_dinode_core_t, di_nblocks), |
| 77 | offsetof(xfs_dinode_core_t, di_extsize), |
| 78 | offsetof(xfs_dinode_core_t, di_nextents), |
| 79 | offsetof(xfs_dinode_core_t, di_anextents), |
| 80 | offsetof(xfs_dinode_core_t, di_forkoff), |
| 81 | offsetof(xfs_dinode_core_t, di_aformat), |
| 82 | offsetof(xfs_dinode_core_t, di_dmevmask), |
| 83 | offsetof(xfs_dinode_core_t, di_dmstate), |
| 84 | offsetof(xfs_dinode_core_t, di_flags), |
| 85 | offsetof(xfs_dinode_core_t, di_gen), |
| 86 | offsetof(xfs_dinode_t, di_next_unlinked), |
| 87 | offsetof(xfs_dinode_t, di_u), |
| 88 | offsetof(xfs_dinode_t, di_a), |
| 89 | sizeof(xfs_dinode_t) |
| 90 | }; |
| 91 | |
| 92 | |
| 93 | ASSERT(offsetof(xfs_dinode_t, di_core) == 0); |
| 94 | ASSERT((fields & (XFS_DI_U|XFS_DI_A)) == 0); |
| 95 | mp = tp->t_mountp; |
| 96 | /* |
| 97 | * Get the inode-relative first and last bytes for these fields |
| 98 | */ |
| 99 | xfs_btree_offsets(fields, offsets, XFS_DI_NUM_BITS, &first, &last); |
| 100 | /* |
| 101 | * Convert to buffer offsets and log it. |
| 102 | */ |
| 103 | ioffset = off << mp->m_sb.sb_inodelog; |
| 104 | first += ioffset; |
| 105 | last += ioffset; |
| 106 | xfs_trans_log_buf(tp, bp, first, last); |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | * Allocation group level functions. |
| 111 | */ |
| 112 | |
| 113 | /* |
| 114 | * Allocate new inodes in the allocation group specified by agbp. |
| 115 | * Return 0 for success, else error code. |
| 116 | */ |
| 117 | STATIC int /* error code or 0 */ |
| 118 | xfs_ialloc_ag_alloc( |
| 119 | xfs_trans_t *tp, /* transaction pointer */ |
| 120 | xfs_buf_t *agbp, /* alloc group buffer */ |
| 121 | int *alloc) |
| 122 | { |
| 123 | xfs_agi_t *agi; /* allocation group header */ |
| 124 | xfs_alloc_arg_t args; /* allocation argument structure */ |
| 125 | int blks_per_cluster; /* fs blocks per inode cluster */ |
| 126 | xfs_btree_cur_t *cur; /* inode btree cursor */ |
| 127 | xfs_daddr_t d; /* disk addr of buffer */ |
| 128 | int error; |
| 129 | xfs_buf_t *fbuf; /* new free inodes' buffer */ |
| 130 | xfs_dinode_t *free; /* new free inode structure */ |
| 131 | int i; /* inode counter */ |
| 132 | int j; /* block counter */ |
| 133 | int nbufs; /* num bufs of new inodes */ |
| 134 | xfs_agino_t newino; /* new first inode's number */ |
| 135 | xfs_agino_t newlen; /* new number of inodes */ |
| 136 | int ninodes; /* num inodes per buf */ |
| 137 | xfs_agino_t thisino; /* current inode number, for loop */ |
| 138 | int version; /* inode version number to use */ |
| 139 | int isaligned; /* inode allocation at stripe unit */ |
| 140 | /* boundary */ |
| 141 | xfs_dinode_core_t dic; /* a dinode_core to copy to new */ |
| 142 | /* inodes */ |
| 143 | |
| 144 | args.tp = tp; |
| 145 | args.mp = tp->t_mountp; |
| 146 | |
| 147 | /* |
| 148 | * Locking will ensure that we don't have two callers in here |
| 149 | * at one time. |
| 150 | */ |
| 151 | newlen = XFS_IALLOC_INODES(args.mp); |
| 152 | if (args.mp->m_maxicount && |
| 153 | args.mp->m_sb.sb_icount + newlen > args.mp->m_maxicount) |
| 154 | return XFS_ERROR(ENOSPC); |
| 155 | args.minlen = args.maxlen = XFS_IALLOC_BLOCKS(args.mp); |
| 156 | /* |
| 157 | * Set the alignment for the allocation. |
| 158 | * If stripe alignment is turned on then align at stripe unit |
| 159 | * boundary. |
| 160 | * If the cluster size is smaller than a filesystem block |
| 161 | * then we're doing I/O for inodes in filesystem block size pieces, |
| 162 | * so don't need alignment anyway. |
| 163 | */ |
| 164 | isaligned = 0; |
| 165 | if (args.mp->m_sinoalign) { |
| 166 | ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN)); |
| 167 | args.alignment = args.mp->m_dalign; |
| 168 | isaligned = 1; |
| 169 | } else if (XFS_SB_VERSION_HASALIGN(&args.mp->m_sb) && |
| 170 | args.mp->m_sb.sb_inoalignmt >= |
| 171 | XFS_B_TO_FSBT(args.mp, XFS_INODE_CLUSTER_SIZE(args.mp))) |
| 172 | args.alignment = args.mp->m_sb.sb_inoalignmt; |
| 173 | else |
| 174 | args.alignment = 1; |
| 175 | agi = XFS_BUF_TO_AGI(agbp); |
| 176 | /* |
| 177 | * Need to figure out where to allocate the inode blocks. |
| 178 | * Ideally they should be spaced out through the a.g. |
| 179 | * For now, just allocate blocks up front. |
| 180 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 181 | args.agbno = be32_to_cpu(agi->agi_root); |
| 182 | args.fsbno = XFS_AGB_TO_FSB(args.mp, be32_to_cpu(agi->agi_seqno), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | args.agbno); |
| 184 | /* |
| 185 | * Allocate a fixed-size extent of inodes. |
| 186 | */ |
| 187 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 188 | args.mod = args.total = args.wasdel = args.isfl = args.userdata = |
| 189 | args.minalignslop = 0; |
| 190 | args.prod = 1; |
| 191 | /* |
| 192 | * Allow space for the inode btree to split. |
| 193 | */ |
| 194 | args.minleft = XFS_IN_MAXLEVELS(args.mp) - 1; |
| 195 | if ((error = xfs_alloc_vextent(&args))) |
| 196 | return error; |
| 197 | |
| 198 | /* |
| 199 | * If stripe alignment is turned on, then try again with cluster |
| 200 | * alignment. |
| 201 | */ |
| 202 | if (isaligned && args.fsbno == NULLFSBLOCK) { |
| 203 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 204 | args.agbno = be32_to_cpu(agi->agi_root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | args.fsbno = XFS_AGB_TO_FSB(args.mp, |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 206 | be32_to_cpu(agi->agi_seqno), args.agbno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | if (XFS_SB_VERSION_HASALIGN(&args.mp->m_sb) && |
| 208 | args.mp->m_sb.sb_inoalignmt >= |
| 209 | XFS_B_TO_FSBT(args.mp, XFS_INODE_CLUSTER_SIZE(args.mp))) |
| 210 | args.alignment = args.mp->m_sb.sb_inoalignmt; |
| 211 | else |
| 212 | args.alignment = 1; |
| 213 | if ((error = xfs_alloc_vextent(&args))) |
| 214 | return error; |
| 215 | } |
| 216 | |
| 217 | if (args.fsbno == NULLFSBLOCK) { |
| 218 | *alloc = 0; |
| 219 | return 0; |
| 220 | } |
| 221 | ASSERT(args.len == args.minlen); |
| 222 | /* |
| 223 | * Convert the results. |
| 224 | */ |
| 225 | newino = XFS_OFFBNO_TO_AGINO(args.mp, args.agbno, 0); |
| 226 | /* |
| 227 | * Loop over the new block(s), filling in the inodes. |
| 228 | * For small block sizes, manipulate the inodes in buffers |
| 229 | * which are multiples of the blocks size. |
| 230 | */ |
| 231 | if (args.mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(args.mp)) { |
| 232 | blks_per_cluster = 1; |
| 233 | nbufs = (int)args.len; |
| 234 | ninodes = args.mp->m_sb.sb_inopblock; |
| 235 | } else { |
| 236 | blks_per_cluster = XFS_INODE_CLUSTER_SIZE(args.mp) / |
| 237 | args.mp->m_sb.sb_blocksize; |
| 238 | nbufs = (int)args.len / blks_per_cluster; |
| 239 | ninodes = blks_per_cluster * args.mp->m_sb.sb_inopblock; |
| 240 | } |
| 241 | /* |
| 242 | * Figure out what version number to use in the inodes we create. |
| 243 | * If the superblock version has caught up to the one that supports |
| 244 | * the new inode format, then use the new inode version. Otherwise |
| 245 | * use the old version so that old kernels will continue to be |
| 246 | * able to use the file system. |
| 247 | */ |
| 248 | if (XFS_SB_VERSION_HASNLINK(&args.mp->m_sb)) |
| 249 | version = XFS_DINODE_VERSION_2; |
| 250 | else |
| 251 | version = XFS_DINODE_VERSION_1; |
| 252 | |
| 253 | memset(&dic, 0, sizeof(xfs_dinode_core_t)); |
| 254 | INT_SET(dic.di_magic, ARCH_CONVERT, XFS_DINODE_MAGIC); |
| 255 | INT_SET(dic.di_version, ARCH_CONVERT, version); |
| 256 | |
| 257 | for (j = 0; j < nbufs; j++) { |
| 258 | /* |
| 259 | * Get the block. |
| 260 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 261 | d = XFS_AGB_TO_DADDR(args.mp, be32_to_cpu(agi->agi_seqno), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | args.agbno + (j * blks_per_cluster)); |
| 263 | fbuf = xfs_trans_get_buf(tp, args.mp->m_ddev_targp, d, |
| 264 | args.mp->m_bsize * blks_per_cluster, |
| 265 | XFS_BUF_LOCK); |
| 266 | ASSERT(fbuf); |
| 267 | ASSERT(!XFS_BUF_GETERROR(fbuf)); |
| 268 | /* |
| 269 | * Loop over the inodes in this buffer. |
| 270 | */ |
| 271 | |
| 272 | for (i = 0; i < ninodes; i++) { |
| 273 | free = XFS_MAKE_IPTR(args.mp, fbuf, i); |
| 274 | memcpy(&(free->di_core), &dic, sizeof(xfs_dinode_core_t)); |
| 275 | INT_SET(free->di_next_unlinked, ARCH_CONVERT, NULLAGINO); |
| 276 | xfs_ialloc_log_di(tp, fbuf, i, |
| 277 | XFS_DI_CORE_BITS | XFS_DI_NEXT_UNLINKED); |
| 278 | } |
| 279 | xfs_trans_inode_alloc_buf(tp, fbuf); |
| 280 | } |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 281 | be32_add(&agi->agi_count, newlen); |
| 282 | be32_add(&agi->agi_freecount, newlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | down_read(&args.mp->m_peraglock); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 284 | args.mp->m_perag[be32_to_cpu(agi->agi_seqno)].pagi_freecount += newlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | up_read(&args.mp->m_peraglock); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 286 | agi->agi_newino = cpu_to_be32(newino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | /* |
| 288 | * Insert records describing the new inode chunk into the btree. |
| 289 | */ |
| 290 | cur = xfs_btree_init_cursor(args.mp, tp, agbp, |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 291 | be32_to_cpu(agi->agi_seqno), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | XFS_BTNUM_INO, (xfs_inode_t *)0, 0); |
| 293 | for (thisino = newino; |
| 294 | thisino < newino + newlen; |
| 295 | thisino += XFS_INODES_PER_CHUNK) { |
| 296 | if ((error = xfs_inobt_lookup_eq(cur, thisino, |
| 297 | XFS_INODES_PER_CHUNK, XFS_INOBT_ALL_FREE, &i))) { |
| 298 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 299 | return error; |
| 300 | } |
| 301 | ASSERT(i == 0); |
| 302 | if ((error = xfs_inobt_insert(cur, &i))) { |
| 303 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 304 | return error; |
| 305 | } |
| 306 | ASSERT(i == 1); |
| 307 | } |
| 308 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 309 | /* |
| 310 | * Log allocation group header fields |
| 311 | */ |
| 312 | xfs_ialloc_log_agi(tp, agbp, |
| 313 | XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO); |
| 314 | /* |
| 315 | * Modify/log superblock values for inode count and inode free count. |
| 316 | */ |
| 317 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen); |
| 318 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen); |
| 319 | *alloc = 1; |
| 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | STATIC __inline xfs_agnumber_t |
| 324 | xfs_ialloc_next_ag( |
| 325 | xfs_mount_t *mp) |
| 326 | { |
| 327 | xfs_agnumber_t agno; |
| 328 | |
| 329 | spin_lock(&mp->m_agirotor_lock); |
| 330 | agno = mp->m_agirotor; |
| 331 | if (++mp->m_agirotor == mp->m_maxagi) |
| 332 | mp->m_agirotor = 0; |
| 333 | spin_unlock(&mp->m_agirotor_lock); |
| 334 | |
| 335 | return agno; |
| 336 | } |
| 337 | |
| 338 | /* |
| 339 | * Select an allocation group to look for a free inode in, based on the parent |
| 340 | * inode and then mode. Return the allocation group buffer. |
| 341 | */ |
| 342 | STATIC xfs_buf_t * /* allocation group buffer */ |
| 343 | xfs_ialloc_ag_select( |
| 344 | xfs_trans_t *tp, /* transaction pointer */ |
| 345 | xfs_ino_t parent, /* parent directory inode number */ |
| 346 | mode_t mode, /* bits set to indicate file type */ |
| 347 | int okalloc) /* ok to allocate more space */ |
| 348 | { |
| 349 | xfs_buf_t *agbp; /* allocation group header buffer */ |
| 350 | xfs_agnumber_t agcount; /* number of ag's in the filesystem */ |
| 351 | xfs_agnumber_t agno; /* current ag number */ |
| 352 | int flags; /* alloc buffer locking flags */ |
| 353 | xfs_extlen_t ineed; /* blocks needed for inode allocation */ |
| 354 | xfs_extlen_t longest = 0; /* longest extent available */ |
| 355 | xfs_mount_t *mp; /* mount point structure */ |
| 356 | int needspace; /* file mode implies space allocated */ |
| 357 | xfs_perag_t *pag; /* per allocation group data */ |
| 358 | xfs_agnumber_t pagno; /* parent (starting) ag number */ |
| 359 | |
| 360 | /* |
| 361 | * Files of these types need at least one block if length > 0 |
| 362 | * (and they won't fit in the inode, but that's hard to figure out). |
| 363 | */ |
| 364 | needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode); |
| 365 | mp = tp->t_mountp; |
| 366 | agcount = mp->m_maxagi; |
| 367 | if (S_ISDIR(mode)) |
| 368 | pagno = xfs_ialloc_next_ag(mp); |
| 369 | else { |
| 370 | pagno = XFS_INO_TO_AGNO(mp, parent); |
| 371 | if (pagno >= agcount) |
| 372 | pagno = 0; |
| 373 | } |
| 374 | ASSERT(pagno < agcount); |
| 375 | /* |
| 376 | * Loop through allocation groups, looking for one with a little |
| 377 | * free space in it. Note we don't look for free inodes, exactly. |
| 378 | * Instead, we include whether there is a need to allocate inodes |
| 379 | * to mean that blocks must be allocated for them, |
| 380 | * if none are currently free. |
| 381 | */ |
| 382 | agno = pagno; |
| 383 | flags = XFS_ALLOC_FLAG_TRYLOCK; |
| 384 | down_read(&mp->m_peraglock); |
| 385 | for (;;) { |
| 386 | pag = &mp->m_perag[agno]; |
| 387 | if (!pag->pagi_init) { |
| 388 | if (xfs_ialloc_read_agi(mp, tp, agno, &agbp)) { |
| 389 | agbp = NULL; |
| 390 | goto nextag; |
| 391 | } |
| 392 | } else |
| 393 | agbp = NULL; |
| 394 | |
| 395 | if (!pag->pagi_inodeok) { |
| 396 | xfs_ialloc_next_ag(mp); |
| 397 | goto unlock_nextag; |
| 398 | } |
| 399 | |
| 400 | /* |
| 401 | * Is there enough free space for the file plus a block |
| 402 | * of inodes (if we need to allocate some)? |
| 403 | */ |
| 404 | ineed = pag->pagi_freecount ? 0 : XFS_IALLOC_BLOCKS(mp); |
| 405 | if (ineed && !pag->pagf_init) { |
| 406 | if (agbp == NULL && |
| 407 | xfs_ialloc_read_agi(mp, tp, agno, &agbp)) { |
| 408 | agbp = NULL; |
| 409 | goto nextag; |
| 410 | } |
| 411 | (void)xfs_alloc_pagf_init(mp, tp, agno, flags); |
| 412 | } |
| 413 | if (!ineed || pag->pagf_init) { |
| 414 | if (ineed && !(longest = pag->pagf_longest)) |
| 415 | longest = pag->pagf_flcount > 0; |
| 416 | if (!ineed || |
| 417 | (pag->pagf_freeblks >= needspace + ineed && |
| 418 | longest >= ineed && |
| 419 | okalloc)) { |
| 420 | if (agbp == NULL && |
| 421 | xfs_ialloc_read_agi(mp, tp, agno, &agbp)) { |
| 422 | agbp = NULL; |
| 423 | goto nextag; |
| 424 | } |
| 425 | up_read(&mp->m_peraglock); |
| 426 | return agbp; |
| 427 | } |
| 428 | } |
| 429 | unlock_nextag: |
| 430 | if (agbp) |
| 431 | xfs_trans_brelse(tp, agbp); |
| 432 | nextag: |
| 433 | /* |
| 434 | * No point in iterating over the rest, if we're shutting |
| 435 | * down. |
| 436 | */ |
| 437 | if (XFS_FORCED_SHUTDOWN(mp)) { |
| 438 | up_read(&mp->m_peraglock); |
| 439 | return (xfs_buf_t *)0; |
| 440 | } |
| 441 | agno++; |
| 442 | if (agno >= agcount) |
| 443 | agno = 0; |
| 444 | if (agno == pagno) { |
| 445 | if (flags == 0) { |
| 446 | up_read(&mp->m_peraglock); |
| 447 | return (xfs_buf_t *)0; |
| 448 | } |
| 449 | flags = 0; |
| 450 | } |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | /* |
| 455 | * Visible inode allocation functions. |
| 456 | */ |
| 457 | |
| 458 | /* |
| 459 | * Allocate an inode on disk. |
| 460 | * Mode is used to tell whether the new inode will need space, and whether |
| 461 | * it is a directory. |
| 462 | * |
| 463 | * The arguments IO_agbp and alloc_done are defined to work within |
| 464 | * the constraint of one allocation per transaction. |
| 465 | * xfs_dialloc() is designed to be called twice if it has to do an |
| 466 | * allocation to make more free inodes. On the first call, |
| 467 | * IO_agbp should be set to NULL. If an inode is available, |
| 468 | * i.e., xfs_dialloc() did not need to do an allocation, an inode |
| 469 | * number is returned. In this case, IO_agbp would be set to the |
| 470 | * current ag_buf and alloc_done set to false. |
| 471 | * If an allocation needed to be done, xfs_dialloc would return |
| 472 | * the current ag_buf in IO_agbp and set alloc_done to true. |
| 473 | * The caller should then commit the current transaction, allocate a new |
| 474 | * transaction, and call xfs_dialloc() again, passing in the previous |
| 475 | * value of IO_agbp. IO_agbp should be held across the transactions. |
| 476 | * Since the agbp is locked across the two calls, the second call is |
| 477 | * guaranteed to have a free inode available. |
| 478 | * |
| 479 | * Once we successfully pick an inode its number is returned and the |
| 480 | * on-disk data structures are updated. The inode itself is not read |
| 481 | * in, since doing so would break ordering constraints with xfs_reclaim. |
| 482 | */ |
| 483 | int |
| 484 | xfs_dialloc( |
| 485 | xfs_trans_t *tp, /* transaction pointer */ |
| 486 | xfs_ino_t parent, /* parent inode (directory) */ |
| 487 | mode_t mode, /* mode bits for new inode */ |
| 488 | int okalloc, /* ok to allocate more space */ |
| 489 | xfs_buf_t **IO_agbp, /* in/out ag header's buffer */ |
| 490 | boolean_t *alloc_done, /* true if we needed to replenish |
| 491 | inode freelist */ |
| 492 | xfs_ino_t *inop) /* inode number allocated */ |
| 493 | { |
| 494 | xfs_agnumber_t agcount; /* number of allocation groups */ |
| 495 | xfs_buf_t *agbp; /* allocation group header's buffer */ |
| 496 | xfs_agnumber_t agno; /* allocation group number */ |
| 497 | xfs_agi_t *agi; /* allocation group header structure */ |
| 498 | xfs_btree_cur_t *cur; /* inode allocation btree cursor */ |
| 499 | int error; /* error return value */ |
| 500 | int i; /* result code */ |
| 501 | int ialloced; /* inode allocation status */ |
| 502 | int noroom = 0; /* no space for inode blk allocation */ |
| 503 | xfs_ino_t ino; /* fs-relative inode to be returned */ |
| 504 | /* REFERENCED */ |
| 505 | int j; /* result code */ |
| 506 | xfs_mount_t *mp; /* file system mount structure */ |
| 507 | int offset; /* index of inode in chunk */ |
| 508 | xfs_agino_t pagino; /* parent's a.g. relative inode # */ |
| 509 | xfs_agnumber_t pagno; /* parent's allocation group number */ |
| 510 | xfs_inobt_rec_t rec; /* inode allocation record */ |
| 511 | xfs_agnumber_t tagno; /* testing allocation group number */ |
| 512 | xfs_btree_cur_t *tcur; /* temp cursor */ |
| 513 | xfs_inobt_rec_t trec; /* temp inode allocation record */ |
| 514 | |
| 515 | |
| 516 | if (*IO_agbp == NULL) { |
| 517 | /* |
| 518 | * We do not have an agbp, so select an initial allocation |
| 519 | * group for inode allocation. |
| 520 | */ |
| 521 | agbp = xfs_ialloc_ag_select(tp, parent, mode, okalloc); |
| 522 | /* |
| 523 | * Couldn't find an allocation group satisfying the |
| 524 | * criteria, give up. |
| 525 | */ |
| 526 | if (!agbp) { |
| 527 | *inop = NULLFSINO; |
| 528 | return 0; |
| 529 | } |
| 530 | agi = XFS_BUF_TO_AGI(agbp); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 531 | ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | } else { |
| 533 | /* |
| 534 | * Continue where we left off before. In this case, we |
| 535 | * know that the allocation group has free inodes. |
| 536 | */ |
| 537 | agbp = *IO_agbp; |
| 538 | agi = XFS_BUF_TO_AGI(agbp); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 539 | ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC); |
| 540 | ASSERT(be32_to_cpu(agi->agi_freecount) > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | } |
| 542 | mp = tp->t_mountp; |
| 543 | agcount = mp->m_sb.sb_agcount; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 544 | agno = be32_to_cpu(agi->agi_seqno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | tagno = agno; |
| 546 | pagno = XFS_INO_TO_AGNO(mp, parent); |
| 547 | pagino = XFS_INO_TO_AGINO(mp, parent); |
| 548 | |
| 549 | /* |
| 550 | * If we have already hit the ceiling of inode blocks then clear |
| 551 | * okalloc so we scan all available agi structures for a free |
| 552 | * inode. |
| 553 | */ |
| 554 | |
| 555 | if (mp->m_maxicount && |
| 556 | mp->m_sb.sb_icount + XFS_IALLOC_INODES(mp) > mp->m_maxicount) { |
| 557 | noroom = 1; |
| 558 | okalloc = 0; |
| 559 | } |
| 560 | |
| 561 | /* |
| 562 | * Loop until we find an allocation group that either has free inodes |
| 563 | * or in which we can allocate some inodes. Iterate through the |
| 564 | * allocation groups upward, wrapping at the end. |
| 565 | */ |
| 566 | *alloc_done = B_FALSE; |
| 567 | while (!agi->agi_freecount) { |
| 568 | /* |
| 569 | * Don't do anything if we're not supposed to allocate |
| 570 | * any blocks, just go on to the next ag. |
| 571 | */ |
| 572 | if (okalloc) { |
| 573 | /* |
| 574 | * Try to allocate some new inodes in the allocation |
| 575 | * group. |
| 576 | */ |
| 577 | if ((error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced))) { |
| 578 | xfs_trans_brelse(tp, agbp); |
| 579 | if (error == ENOSPC) { |
| 580 | *inop = NULLFSINO; |
| 581 | return 0; |
| 582 | } else |
| 583 | return error; |
| 584 | } |
| 585 | if (ialloced) { |
| 586 | /* |
| 587 | * We successfully allocated some inodes, return |
| 588 | * the current context to the caller so that it |
| 589 | * can commit the current transaction and call |
| 590 | * us again where we left off. |
| 591 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 592 | ASSERT(be32_to_cpu(agi->agi_freecount) > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | *alloc_done = B_TRUE; |
| 594 | *IO_agbp = agbp; |
| 595 | *inop = NULLFSINO; |
| 596 | return 0; |
| 597 | } |
| 598 | } |
| 599 | /* |
| 600 | * If it failed, give up on this ag. |
| 601 | */ |
| 602 | xfs_trans_brelse(tp, agbp); |
| 603 | /* |
| 604 | * Go on to the next ag: get its ag header. |
| 605 | */ |
| 606 | nextag: |
| 607 | if (++tagno == agcount) |
| 608 | tagno = 0; |
| 609 | if (tagno == agno) { |
| 610 | *inop = NULLFSINO; |
| 611 | return noroom ? ENOSPC : 0; |
| 612 | } |
| 613 | down_read(&mp->m_peraglock); |
| 614 | if (mp->m_perag[tagno].pagi_inodeok == 0) { |
| 615 | up_read(&mp->m_peraglock); |
| 616 | goto nextag; |
| 617 | } |
| 618 | error = xfs_ialloc_read_agi(mp, tp, tagno, &agbp); |
| 619 | up_read(&mp->m_peraglock); |
| 620 | if (error) |
| 621 | goto nextag; |
| 622 | agi = XFS_BUF_TO_AGI(agbp); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 623 | ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | } |
| 625 | /* |
| 626 | * Here with an allocation group that has a free inode. |
| 627 | * Reset agno since we may have chosen a new ag in the |
| 628 | * loop above. |
| 629 | */ |
| 630 | agno = tagno; |
| 631 | *IO_agbp = NULL; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 632 | cur = xfs_btree_init_cursor(mp, tp, agbp, be32_to_cpu(agi->agi_seqno), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | XFS_BTNUM_INO, (xfs_inode_t *)0, 0); |
| 634 | /* |
| 635 | * If pagino is 0 (this is the root inode allocation) use newino. |
| 636 | * This must work because we've just allocated some. |
| 637 | */ |
| 638 | if (!pagino) |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 639 | pagino = be32_to_cpu(agi->agi_newino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | #ifdef DEBUG |
| 641 | if (cur->bc_nlevels == 1) { |
| 642 | int freecount = 0; |
| 643 | |
| 644 | if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i))) |
| 645 | goto error0; |
| 646 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); |
| 647 | do { |
| 648 | if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino, |
| 649 | &rec.ir_freecount, &rec.ir_free, &i))) |
| 650 | goto error0; |
| 651 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); |
| 652 | freecount += rec.ir_freecount; |
| 653 | if ((error = xfs_inobt_increment(cur, 0, &i))) |
| 654 | goto error0; |
| 655 | } while (i == 1); |
| 656 | |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 657 | ASSERT(freecount == be32_to_cpu(agi->agi_freecount) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | XFS_FORCED_SHUTDOWN(mp)); |
| 659 | } |
| 660 | #endif |
| 661 | /* |
| 662 | * If in the same a.g. as the parent, try to get near the parent. |
| 663 | */ |
| 664 | if (pagno == agno) { |
| 665 | if ((error = xfs_inobt_lookup_le(cur, pagino, 0, 0, &i))) |
| 666 | goto error0; |
| 667 | if (i != 0 && |
| 668 | (error = xfs_inobt_get_rec(cur, &rec.ir_startino, |
| 669 | &rec.ir_freecount, &rec.ir_free, &j)) == 0 && |
| 670 | j == 1 && |
| 671 | rec.ir_freecount > 0) { |
| 672 | /* |
| 673 | * Found a free inode in the same chunk |
| 674 | * as parent, done. |
| 675 | */ |
| 676 | } |
| 677 | /* |
| 678 | * In the same a.g. as parent, but parent's chunk is full. |
| 679 | */ |
| 680 | else { |
| 681 | int doneleft; /* done, to the left */ |
| 682 | int doneright; /* done, to the right */ |
| 683 | |
| 684 | if (error) |
| 685 | goto error0; |
| 686 | ASSERT(i == 1); |
| 687 | ASSERT(j == 1); |
| 688 | /* |
| 689 | * Duplicate the cursor, search left & right |
| 690 | * simultaneously. |
| 691 | */ |
| 692 | if ((error = xfs_btree_dup_cursor(cur, &tcur))) |
| 693 | goto error0; |
| 694 | /* |
| 695 | * Search left with tcur, back up 1 record. |
| 696 | */ |
| 697 | if ((error = xfs_inobt_decrement(tcur, 0, &i))) |
| 698 | goto error1; |
| 699 | doneleft = !i; |
| 700 | if (!doneleft) { |
| 701 | if ((error = xfs_inobt_get_rec(tcur, |
| 702 | &trec.ir_startino, |
| 703 | &trec.ir_freecount, |
| 704 | &trec.ir_free, &i))) |
| 705 | goto error1; |
| 706 | XFS_WANT_CORRUPTED_GOTO(i == 1, error1); |
| 707 | } |
| 708 | /* |
| 709 | * Search right with cur, go forward 1 record. |
| 710 | */ |
| 711 | if ((error = xfs_inobt_increment(cur, 0, &i))) |
| 712 | goto error1; |
| 713 | doneright = !i; |
| 714 | if (!doneright) { |
| 715 | if ((error = xfs_inobt_get_rec(cur, |
| 716 | &rec.ir_startino, |
| 717 | &rec.ir_freecount, |
| 718 | &rec.ir_free, &i))) |
| 719 | goto error1; |
| 720 | XFS_WANT_CORRUPTED_GOTO(i == 1, error1); |
| 721 | } |
| 722 | /* |
| 723 | * Loop until we find the closest inode chunk |
| 724 | * with a free one. |
| 725 | */ |
| 726 | while (!doneleft || !doneright) { |
| 727 | int useleft; /* using left inode |
| 728 | chunk this time */ |
| 729 | |
| 730 | /* |
| 731 | * Figure out which block is closer, |
| 732 | * if both are valid. |
| 733 | */ |
| 734 | if (!doneleft && !doneright) |
| 735 | useleft = |
| 736 | pagino - |
| 737 | (trec.ir_startino + |
| 738 | XFS_INODES_PER_CHUNK - 1) < |
| 739 | rec.ir_startino - pagino; |
| 740 | else |
| 741 | useleft = !doneleft; |
| 742 | /* |
| 743 | * If checking the left, does it have |
| 744 | * free inodes? |
| 745 | */ |
| 746 | if (useleft && trec.ir_freecount) { |
| 747 | /* |
| 748 | * Yes, set it up as the chunk to use. |
| 749 | */ |
| 750 | rec = trec; |
| 751 | xfs_btree_del_cursor(cur, |
| 752 | XFS_BTREE_NOERROR); |
| 753 | cur = tcur; |
| 754 | break; |
| 755 | } |
| 756 | /* |
| 757 | * If checking the right, does it have |
| 758 | * free inodes? |
| 759 | */ |
| 760 | if (!useleft && rec.ir_freecount) { |
| 761 | /* |
| 762 | * Yes, it's already set up. |
| 763 | */ |
| 764 | xfs_btree_del_cursor(tcur, |
| 765 | XFS_BTREE_NOERROR); |
| 766 | break; |
| 767 | } |
| 768 | /* |
| 769 | * If used the left, get another one |
| 770 | * further left. |
| 771 | */ |
| 772 | if (useleft) { |
| 773 | if ((error = xfs_inobt_decrement(tcur, 0, |
| 774 | &i))) |
| 775 | goto error1; |
| 776 | doneleft = !i; |
| 777 | if (!doneleft) { |
| 778 | if ((error = xfs_inobt_get_rec( |
| 779 | tcur, |
| 780 | &trec.ir_startino, |
| 781 | &trec.ir_freecount, |
| 782 | &trec.ir_free, &i))) |
| 783 | goto error1; |
| 784 | XFS_WANT_CORRUPTED_GOTO(i == 1, |
| 785 | error1); |
| 786 | } |
| 787 | } |
| 788 | /* |
| 789 | * If used the right, get another one |
| 790 | * further right. |
| 791 | */ |
| 792 | else { |
| 793 | if ((error = xfs_inobt_increment(cur, 0, |
| 794 | &i))) |
| 795 | goto error1; |
| 796 | doneright = !i; |
| 797 | if (!doneright) { |
| 798 | if ((error = xfs_inobt_get_rec( |
| 799 | cur, |
| 800 | &rec.ir_startino, |
| 801 | &rec.ir_freecount, |
| 802 | &rec.ir_free, &i))) |
| 803 | goto error1; |
| 804 | XFS_WANT_CORRUPTED_GOTO(i == 1, |
| 805 | error1); |
| 806 | } |
| 807 | } |
| 808 | } |
| 809 | ASSERT(!doneleft || !doneright); |
| 810 | } |
| 811 | } |
| 812 | /* |
| 813 | * In a different a.g. from the parent. |
| 814 | * See if the most recently allocated block has any free. |
| 815 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 816 | else if (be32_to_cpu(agi->agi_newino) != NULLAGINO) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | if ((error = xfs_inobt_lookup_eq(cur, |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 818 | be32_to_cpu(agi->agi_newino), 0, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | goto error0; |
| 820 | if (i == 1 && |
| 821 | (error = xfs_inobt_get_rec(cur, &rec.ir_startino, |
| 822 | &rec.ir_freecount, &rec.ir_free, &j)) == 0 && |
| 823 | j == 1 && |
| 824 | rec.ir_freecount > 0) { |
| 825 | /* |
| 826 | * The last chunk allocated in the group still has |
| 827 | * a free inode. |
| 828 | */ |
| 829 | } |
| 830 | /* |
| 831 | * None left in the last group, search the whole a.g. |
| 832 | */ |
| 833 | else { |
| 834 | if (error) |
| 835 | goto error0; |
| 836 | if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i))) |
| 837 | goto error0; |
| 838 | ASSERT(i == 1); |
| 839 | for (;;) { |
| 840 | if ((error = xfs_inobt_get_rec(cur, |
| 841 | &rec.ir_startino, |
| 842 | &rec.ir_freecount, &rec.ir_free, |
| 843 | &i))) |
| 844 | goto error0; |
| 845 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); |
| 846 | if (rec.ir_freecount > 0) |
| 847 | break; |
| 848 | if ((error = xfs_inobt_increment(cur, 0, &i))) |
| 849 | goto error0; |
| 850 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); |
| 851 | } |
| 852 | } |
| 853 | } |
| 854 | offset = XFS_IALLOC_FIND_FREE(&rec.ir_free); |
| 855 | ASSERT(offset >= 0); |
| 856 | ASSERT(offset < XFS_INODES_PER_CHUNK); |
| 857 | ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) % |
| 858 | XFS_INODES_PER_CHUNK) == 0); |
| 859 | ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset); |
| 860 | XFS_INOBT_CLR_FREE(&rec, offset); |
| 861 | rec.ir_freecount--; |
| 862 | if ((error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount, |
| 863 | rec.ir_free))) |
| 864 | goto error0; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 865 | be32_add(&agi->agi_freecount, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT); |
| 867 | down_read(&mp->m_peraglock); |
| 868 | mp->m_perag[tagno].pagi_freecount--; |
| 869 | up_read(&mp->m_peraglock); |
| 870 | #ifdef DEBUG |
| 871 | if (cur->bc_nlevels == 1) { |
| 872 | int freecount = 0; |
| 873 | |
| 874 | if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i))) |
| 875 | goto error0; |
| 876 | do { |
| 877 | if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino, |
| 878 | &rec.ir_freecount, &rec.ir_free, &i))) |
| 879 | goto error0; |
| 880 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); |
| 881 | freecount += rec.ir_freecount; |
| 882 | if ((error = xfs_inobt_increment(cur, 0, &i))) |
| 883 | goto error0; |
| 884 | } while (i == 1); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 885 | ASSERT(freecount == be32_to_cpu(agi->agi_freecount) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | XFS_FORCED_SHUTDOWN(mp)); |
| 887 | } |
| 888 | #endif |
| 889 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 890 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1); |
| 891 | *inop = ino; |
| 892 | return 0; |
| 893 | error1: |
| 894 | xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR); |
| 895 | error0: |
| 896 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 897 | return error; |
| 898 | } |
| 899 | |
| 900 | /* |
| 901 | * Free disk inode. Carefully avoids touching the incore inode, all |
| 902 | * manipulations incore are the caller's responsibility. |
| 903 | * The on-disk inode is not changed by this operation, only the |
| 904 | * btree (free inode mask) is changed. |
| 905 | */ |
| 906 | int |
| 907 | xfs_difree( |
| 908 | xfs_trans_t *tp, /* transaction pointer */ |
| 909 | xfs_ino_t inode, /* inode to be freed */ |
| 910 | xfs_bmap_free_t *flist, /* extents to free */ |
| 911 | int *delete, /* set if inode cluster was deleted */ |
| 912 | xfs_ino_t *first_ino) /* first inode in deleted cluster */ |
| 913 | { |
| 914 | /* REFERENCED */ |
| 915 | xfs_agblock_t agbno; /* block number containing inode */ |
| 916 | xfs_buf_t *agbp; /* buffer containing allocation group header */ |
| 917 | xfs_agino_t agino; /* inode number relative to allocation group */ |
| 918 | xfs_agnumber_t agno; /* allocation group number */ |
| 919 | xfs_agi_t *agi; /* allocation group header */ |
| 920 | xfs_btree_cur_t *cur; /* inode btree cursor */ |
| 921 | int error; /* error return value */ |
| 922 | int i; /* result code */ |
| 923 | int ilen; /* inodes in an inode cluster */ |
| 924 | xfs_mount_t *mp; /* mount structure for filesystem */ |
| 925 | int off; /* offset of inode in inode chunk */ |
| 926 | xfs_inobt_rec_t rec; /* btree record */ |
| 927 | |
| 928 | mp = tp->t_mountp; |
| 929 | |
| 930 | /* |
| 931 | * Break up inode number into its components. |
| 932 | */ |
| 933 | agno = XFS_INO_TO_AGNO(mp, inode); |
| 934 | if (agno >= mp->m_sb.sb_agcount) { |
| 935 | cmn_err(CE_WARN, |
| 936 | "xfs_difree: agno >= mp->m_sb.sb_agcount (%d >= %d) on %s. Returning EINVAL.", |
| 937 | agno, mp->m_sb.sb_agcount, mp->m_fsname); |
| 938 | ASSERT(0); |
| 939 | return XFS_ERROR(EINVAL); |
| 940 | } |
| 941 | agino = XFS_INO_TO_AGINO(mp, inode); |
| 942 | if (inode != XFS_AGINO_TO_INO(mp, agno, agino)) { |
| 943 | cmn_err(CE_WARN, |
Christoph Hellwig | da1650a | 2005-11-02 10:21:35 +1100 | [diff] [blame] | 944 | "xfs_difree: inode != XFS_AGINO_TO_INO() " |
| 945 | "(%llu != %llu) on %s. Returning EINVAL.", |
| 946 | (unsigned long long)inode, |
| 947 | (unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino), |
| 948 | mp->m_fsname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | ASSERT(0); |
| 950 | return XFS_ERROR(EINVAL); |
| 951 | } |
| 952 | agbno = XFS_AGINO_TO_AGBNO(mp, agino); |
| 953 | if (agbno >= mp->m_sb.sb_agblocks) { |
| 954 | cmn_err(CE_WARN, |
| 955 | "xfs_difree: agbno >= mp->m_sb.sb_agblocks (%d >= %d) on %s. Returning EINVAL.", |
| 956 | agbno, mp->m_sb.sb_agblocks, mp->m_fsname); |
| 957 | ASSERT(0); |
| 958 | return XFS_ERROR(EINVAL); |
| 959 | } |
| 960 | /* |
| 961 | * Get the allocation group header. |
| 962 | */ |
| 963 | down_read(&mp->m_peraglock); |
| 964 | error = xfs_ialloc_read_agi(mp, tp, agno, &agbp); |
| 965 | up_read(&mp->m_peraglock); |
| 966 | if (error) { |
| 967 | cmn_err(CE_WARN, |
| 968 | "xfs_difree: xfs_ialloc_read_agi() returned an error %d on %s. Returning error.", |
| 969 | error, mp->m_fsname); |
| 970 | return error; |
| 971 | } |
| 972 | agi = XFS_BUF_TO_AGI(agbp); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 973 | ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC); |
| 974 | ASSERT(agbno < be32_to_cpu(agi->agi_length)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | /* |
| 976 | * Initialize the cursor. |
| 977 | */ |
| 978 | cur = xfs_btree_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO, |
| 979 | (xfs_inode_t *)0, 0); |
| 980 | #ifdef DEBUG |
| 981 | if (cur->bc_nlevels == 1) { |
| 982 | int freecount = 0; |
| 983 | |
| 984 | if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i))) |
| 985 | goto error0; |
| 986 | do { |
| 987 | if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino, |
| 988 | &rec.ir_freecount, &rec.ir_free, &i))) |
| 989 | goto error0; |
| 990 | if (i) { |
| 991 | freecount += rec.ir_freecount; |
| 992 | if ((error = xfs_inobt_increment(cur, 0, &i))) |
| 993 | goto error0; |
| 994 | } |
| 995 | } while (i == 1); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 996 | ASSERT(freecount == be32_to_cpu(agi->agi_freecount) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | XFS_FORCED_SHUTDOWN(mp)); |
| 998 | } |
| 999 | #endif |
| 1000 | /* |
| 1001 | * Look for the entry describing this inode. |
| 1002 | */ |
| 1003 | if ((error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i))) { |
| 1004 | cmn_err(CE_WARN, |
| 1005 | "xfs_difree: xfs_inobt_lookup_le returned() an error %d on %s. Returning error.", |
| 1006 | error, mp->m_fsname); |
| 1007 | goto error0; |
| 1008 | } |
| 1009 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); |
| 1010 | if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino, &rec.ir_freecount, |
| 1011 | &rec.ir_free, &i))) { |
| 1012 | cmn_err(CE_WARN, |
| 1013 | "xfs_difree: xfs_inobt_get_rec() returned an error %d on %s. Returning error.", |
| 1014 | error, mp->m_fsname); |
| 1015 | goto error0; |
| 1016 | } |
| 1017 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); |
| 1018 | /* |
| 1019 | * Get the offset in the inode chunk. |
| 1020 | */ |
| 1021 | off = agino - rec.ir_startino; |
| 1022 | ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK); |
| 1023 | ASSERT(!XFS_INOBT_IS_FREE(&rec, off)); |
| 1024 | /* |
| 1025 | * Mark the inode free & increment the count. |
| 1026 | */ |
| 1027 | XFS_INOBT_SET_FREE(&rec, off); |
| 1028 | rec.ir_freecount++; |
| 1029 | |
| 1030 | /* |
| 1031 | * When an inode cluster is free, it becomes elgible for removal |
| 1032 | */ |
| 1033 | if ((mp->m_flags & XFS_MOUNT_IDELETE) && |
| 1034 | (rec.ir_freecount == XFS_IALLOC_INODES(mp))) { |
| 1035 | |
| 1036 | *delete = 1; |
| 1037 | *first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino); |
| 1038 | |
| 1039 | /* |
| 1040 | * Remove the inode cluster from the AGI B+Tree, adjust the |
| 1041 | * AGI and Superblock inode counts, and mark the disk space |
| 1042 | * to be freed when the transaction is committed. |
| 1043 | */ |
| 1044 | ilen = XFS_IALLOC_INODES(mp); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1045 | be32_add(&agi->agi_count, -ilen); |
| 1046 | be32_add(&agi->agi_freecount, -(ilen - 1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT); |
| 1048 | down_read(&mp->m_peraglock); |
| 1049 | mp->m_perag[agno].pagi_freecount -= ilen - 1; |
| 1050 | up_read(&mp->m_peraglock); |
| 1051 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen); |
| 1052 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1)); |
| 1053 | |
| 1054 | if ((error = xfs_inobt_delete(cur, &i))) { |
| 1055 | cmn_err(CE_WARN, "xfs_difree: xfs_inobt_delete returned an error %d on %s.\n", |
| 1056 | error, mp->m_fsname); |
| 1057 | goto error0; |
| 1058 | } |
| 1059 | |
| 1060 | xfs_bmap_add_free(XFS_AGB_TO_FSB(mp, |
| 1061 | agno, XFS_INO_TO_AGBNO(mp,rec.ir_startino)), |
| 1062 | XFS_IALLOC_BLOCKS(mp), flist, mp); |
| 1063 | } else { |
| 1064 | *delete = 0; |
| 1065 | |
| 1066 | if ((error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount, rec.ir_free))) { |
| 1067 | cmn_err(CE_WARN, |
| 1068 | "xfs_difree: xfs_inobt_update() returned an error %d on %s. Returning error.", |
| 1069 | error, mp->m_fsname); |
| 1070 | goto error0; |
| 1071 | } |
| 1072 | /* |
| 1073 | * Change the inode free counts and log the ag/sb changes. |
| 1074 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1075 | be32_add(&agi->agi_freecount, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT); |
| 1077 | down_read(&mp->m_peraglock); |
| 1078 | mp->m_perag[agno].pagi_freecount++; |
| 1079 | up_read(&mp->m_peraglock); |
| 1080 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1); |
| 1081 | } |
| 1082 | |
| 1083 | #ifdef DEBUG |
| 1084 | if (cur->bc_nlevels == 1) { |
| 1085 | int freecount = 0; |
| 1086 | |
| 1087 | if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i))) |
| 1088 | goto error0; |
| 1089 | do { |
| 1090 | if ((error = xfs_inobt_get_rec(cur, |
| 1091 | &rec.ir_startino, |
| 1092 | &rec.ir_freecount, |
| 1093 | &rec.ir_free, &i))) |
| 1094 | goto error0; |
| 1095 | if (i) { |
| 1096 | freecount += rec.ir_freecount; |
| 1097 | if ((error = xfs_inobt_increment(cur, 0, &i))) |
| 1098 | goto error0; |
| 1099 | } |
| 1100 | } while (i == 1); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1101 | ASSERT(freecount == be32_to_cpu(agi->agi_freecount) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | XFS_FORCED_SHUTDOWN(mp)); |
| 1103 | } |
| 1104 | #endif |
| 1105 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 1106 | return 0; |
| 1107 | |
| 1108 | error0: |
| 1109 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 1110 | return error; |
| 1111 | } |
| 1112 | |
| 1113 | /* |
| 1114 | * Return the location of the inode in bno/off, for mapping it into a buffer. |
| 1115 | */ |
| 1116 | /*ARGSUSED*/ |
| 1117 | int |
| 1118 | xfs_dilocate( |
| 1119 | xfs_mount_t *mp, /* file system mount structure */ |
| 1120 | xfs_trans_t *tp, /* transaction pointer */ |
| 1121 | xfs_ino_t ino, /* inode to locate */ |
| 1122 | xfs_fsblock_t *bno, /* output: block containing inode */ |
| 1123 | int *len, /* output: num blocks in inode cluster */ |
| 1124 | int *off, /* output: index in block of inode */ |
| 1125 | uint flags) /* flags concerning inode lookup */ |
| 1126 | { |
| 1127 | xfs_agblock_t agbno; /* block number of inode in the alloc group */ |
| 1128 | xfs_buf_t *agbp; /* agi buffer */ |
| 1129 | xfs_agino_t agino; /* inode number within alloc group */ |
| 1130 | xfs_agnumber_t agno; /* allocation group number */ |
| 1131 | int blks_per_cluster; /* num blocks per inode cluster */ |
| 1132 | xfs_agblock_t chunk_agbno; /* first block in inode chunk */ |
| 1133 | xfs_agino_t chunk_agino; /* first agino in inode chunk */ |
| 1134 | __int32_t chunk_cnt; /* count of free inodes in chunk */ |
| 1135 | xfs_inofree_t chunk_free; /* mask of free inodes in chunk */ |
| 1136 | xfs_agblock_t cluster_agbno; /* first block in inode cluster */ |
| 1137 | xfs_btree_cur_t *cur; /* inode btree cursor */ |
| 1138 | int error; /* error code */ |
| 1139 | int i; /* temp state */ |
| 1140 | int offset; /* index of inode in its buffer */ |
| 1141 | int offset_agbno; /* blks from chunk start to inode */ |
| 1142 | |
| 1143 | ASSERT(ino != NULLFSINO); |
| 1144 | /* |
| 1145 | * Split up the inode number into its parts. |
| 1146 | */ |
| 1147 | agno = XFS_INO_TO_AGNO(mp, ino); |
| 1148 | agino = XFS_INO_TO_AGINO(mp, ino); |
| 1149 | agbno = XFS_AGINO_TO_AGBNO(mp, agino); |
| 1150 | if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks || |
| 1151 | ino != XFS_AGINO_TO_INO(mp, agno, agino)) { |
| 1152 | #ifdef DEBUG |
| 1153 | if (agno >= mp->m_sb.sb_agcount) { |
| 1154 | xfs_fs_cmn_err(CE_ALERT, mp, |
| 1155 | "xfs_dilocate: agno (%d) >= " |
| 1156 | "mp->m_sb.sb_agcount (%d)", |
| 1157 | agno, mp->m_sb.sb_agcount); |
| 1158 | } |
| 1159 | if (agbno >= mp->m_sb.sb_agblocks) { |
| 1160 | xfs_fs_cmn_err(CE_ALERT, mp, |
| 1161 | "xfs_dilocate: agbno (0x%llx) >= " |
| 1162 | "mp->m_sb.sb_agblocks (0x%lx)", |
| 1163 | (unsigned long long) agbno, |
| 1164 | (unsigned long) mp->m_sb.sb_agblocks); |
| 1165 | } |
| 1166 | if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) { |
| 1167 | xfs_fs_cmn_err(CE_ALERT, mp, |
| 1168 | "xfs_dilocate: ino (0x%llx) != " |
| 1169 | "XFS_AGINO_TO_INO(mp, agno, agino) " |
| 1170 | "(0x%llx)", |
| 1171 | ino, XFS_AGINO_TO_INO(mp, agno, agino)); |
| 1172 | } |
| 1173 | #endif /* DEBUG */ |
| 1174 | return XFS_ERROR(EINVAL); |
| 1175 | } |
| 1176 | if ((mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) || |
| 1177 | !(flags & XFS_IMAP_LOOKUP)) { |
| 1178 | offset = XFS_INO_TO_OFFSET(mp, ino); |
| 1179 | ASSERT(offset < mp->m_sb.sb_inopblock); |
| 1180 | *bno = XFS_AGB_TO_FSB(mp, agno, agbno); |
| 1181 | *off = offset; |
| 1182 | *len = 1; |
| 1183 | return 0; |
| 1184 | } |
| 1185 | blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_blocklog; |
| 1186 | if (*bno != NULLFSBLOCK) { |
| 1187 | offset = XFS_INO_TO_OFFSET(mp, ino); |
| 1188 | ASSERT(offset < mp->m_sb.sb_inopblock); |
| 1189 | cluster_agbno = XFS_FSB_TO_AGBNO(mp, *bno); |
| 1190 | *off = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) + |
| 1191 | offset; |
| 1192 | *len = blks_per_cluster; |
| 1193 | return 0; |
| 1194 | } |
| 1195 | if (mp->m_inoalign_mask) { |
| 1196 | offset_agbno = agbno & mp->m_inoalign_mask; |
| 1197 | chunk_agbno = agbno - offset_agbno; |
| 1198 | } else { |
| 1199 | down_read(&mp->m_peraglock); |
| 1200 | error = xfs_ialloc_read_agi(mp, tp, agno, &agbp); |
| 1201 | up_read(&mp->m_peraglock); |
| 1202 | if (error) { |
| 1203 | #ifdef DEBUG |
| 1204 | xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: " |
| 1205 | "xfs_ialloc_read_agi() returned " |
| 1206 | "error %d, agno %d", |
| 1207 | error, agno); |
| 1208 | #endif /* DEBUG */ |
| 1209 | return error; |
| 1210 | } |
| 1211 | cur = xfs_btree_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO, |
| 1212 | (xfs_inode_t *)0, 0); |
| 1213 | if ((error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i))) { |
| 1214 | #ifdef DEBUG |
| 1215 | xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: " |
| 1216 | "xfs_inobt_lookup_le() failed"); |
| 1217 | #endif /* DEBUG */ |
| 1218 | goto error0; |
| 1219 | } |
| 1220 | if ((error = xfs_inobt_get_rec(cur, &chunk_agino, &chunk_cnt, |
| 1221 | &chunk_free, &i))) { |
| 1222 | #ifdef DEBUG |
| 1223 | xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: " |
| 1224 | "xfs_inobt_get_rec() failed"); |
| 1225 | #endif /* DEBUG */ |
| 1226 | goto error0; |
| 1227 | } |
| 1228 | if (i == 0) { |
| 1229 | #ifdef DEBUG |
| 1230 | xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: " |
| 1231 | "xfs_inobt_get_rec() failed"); |
| 1232 | #endif /* DEBUG */ |
| 1233 | error = XFS_ERROR(EINVAL); |
| 1234 | } |
| 1235 | xfs_trans_brelse(tp, agbp); |
| 1236 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 1237 | if (error) |
| 1238 | return error; |
| 1239 | chunk_agbno = XFS_AGINO_TO_AGBNO(mp, chunk_agino); |
| 1240 | offset_agbno = agbno - chunk_agbno; |
| 1241 | } |
| 1242 | ASSERT(agbno >= chunk_agbno); |
| 1243 | cluster_agbno = chunk_agbno + |
| 1244 | ((offset_agbno / blks_per_cluster) * blks_per_cluster); |
| 1245 | offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) + |
| 1246 | XFS_INO_TO_OFFSET(mp, ino); |
| 1247 | *bno = XFS_AGB_TO_FSB(mp, agno, cluster_agbno); |
| 1248 | *off = offset; |
| 1249 | *len = blks_per_cluster; |
| 1250 | return 0; |
| 1251 | error0: |
| 1252 | xfs_trans_brelse(tp, agbp); |
| 1253 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 1254 | return error; |
| 1255 | } |
| 1256 | |
| 1257 | /* |
| 1258 | * Compute and fill in value of m_in_maxlevels. |
| 1259 | */ |
| 1260 | void |
| 1261 | xfs_ialloc_compute_maxlevels( |
| 1262 | xfs_mount_t *mp) /* file system mount structure */ |
| 1263 | { |
| 1264 | int level; |
| 1265 | uint maxblocks; |
| 1266 | uint maxleafents; |
| 1267 | int minleafrecs; |
| 1268 | int minnoderecs; |
| 1269 | |
| 1270 | maxleafents = (1LL << XFS_INO_AGINO_BITS(mp)) >> |
| 1271 | XFS_INODES_PER_CHUNK_LOG; |
| 1272 | minleafrecs = mp->m_alloc_mnr[0]; |
| 1273 | minnoderecs = mp->m_alloc_mnr[1]; |
| 1274 | maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs; |
| 1275 | for (level = 1; maxblocks > 1; level++) |
| 1276 | maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs; |
| 1277 | mp->m_in_maxlevels = level; |
| 1278 | } |
| 1279 | |
| 1280 | /* |
| 1281 | * Log specified fields for the ag hdr (inode section) |
| 1282 | */ |
| 1283 | void |
| 1284 | xfs_ialloc_log_agi( |
| 1285 | xfs_trans_t *tp, /* transaction pointer */ |
| 1286 | xfs_buf_t *bp, /* allocation group header buffer */ |
| 1287 | int fields) /* bitmask of fields to log */ |
| 1288 | { |
| 1289 | int first; /* first byte number */ |
| 1290 | int last; /* last byte number */ |
| 1291 | static const short offsets[] = { /* field starting offsets */ |
| 1292 | /* keep in sync with bit definitions */ |
| 1293 | offsetof(xfs_agi_t, agi_magicnum), |
| 1294 | offsetof(xfs_agi_t, agi_versionnum), |
| 1295 | offsetof(xfs_agi_t, agi_seqno), |
| 1296 | offsetof(xfs_agi_t, agi_length), |
| 1297 | offsetof(xfs_agi_t, agi_count), |
| 1298 | offsetof(xfs_agi_t, agi_root), |
| 1299 | offsetof(xfs_agi_t, agi_level), |
| 1300 | offsetof(xfs_agi_t, agi_freecount), |
| 1301 | offsetof(xfs_agi_t, agi_newino), |
| 1302 | offsetof(xfs_agi_t, agi_dirino), |
| 1303 | offsetof(xfs_agi_t, agi_unlinked), |
| 1304 | sizeof(xfs_agi_t) |
| 1305 | }; |
| 1306 | #ifdef DEBUG |
| 1307 | xfs_agi_t *agi; /* allocation group header */ |
| 1308 | |
| 1309 | agi = XFS_BUF_TO_AGI(bp); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1310 | ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 | #endif |
| 1312 | /* |
| 1313 | * Compute byte offsets for the first and last fields. |
| 1314 | */ |
| 1315 | xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS, &first, &last); |
| 1316 | /* |
| 1317 | * Log the allocation group inode header buffer. |
| 1318 | */ |
| 1319 | xfs_trans_log_buf(tp, bp, first, last); |
| 1320 | } |
| 1321 | |
| 1322 | /* |
| 1323 | * Read in the allocation group header (inode allocation section) |
| 1324 | */ |
| 1325 | int |
| 1326 | xfs_ialloc_read_agi( |
| 1327 | xfs_mount_t *mp, /* file system mount structure */ |
| 1328 | xfs_trans_t *tp, /* transaction pointer */ |
| 1329 | xfs_agnumber_t agno, /* allocation group number */ |
| 1330 | xfs_buf_t **bpp) /* allocation group hdr buf */ |
| 1331 | { |
| 1332 | xfs_agi_t *agi; /* allocation group header */ |
| 1333 | int agi_ok; /* agi is consistent */ |
| 1334 | xfs_buf_t *bp; /* allocation group hdr buf */ |
| 1335 | xfs_perag_t *pag; /* per allocation group data */ |
| 1336 | int error; |
| 1337 | |
| 1338 | ASSERT(agno != NULLAGNUMBER); |
| 1339 | error = xfs_trans_read_buf( |
| 1340 | mp, tp, mp->m_ddev_targp, |
| 1341 | XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)), |
| 1342 | XFS_FSS_TO_BB(mp, 1), 0, &bp); |
| 1343 | if (error) |
| 1344 | return error; |
| 1345 | ASSERT(bp && !XFS_BUF_GETERROR(bp)); |
| 1346 | |
| 1347 | /* |
| 1348 | * Validate the magic number of the agi block. |
| 1349 | */ |
| 1350 | agi = XFS_BUF_TO_AGI(bp); |
| 1351 | agi_ok = |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1352 | be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC && |
| 1353 | XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | if (unlikely(XFS_TEST_ERROR(!agi_ok, mp, XFS_ERRTAG_IALLOC_READ_AGI, |
| 1355 | XFS_RANDOM_IALLOC_READ_AGI))) { |
| 1356 | XFS_CORRUPTION_ERROR("xfs_ialloc_read_agi", XFS_ERRLEVEL_LOW, |
| 1357 | mp, agi); |
| 1358 | xfs_trans_brelse(tp, bp); |
| 1359 | return XFS_ERROR(EFSCORRUPTED); |
| 1360 | } |
| 1361 | pag = &mp->m_perag[agno]; |
| 1362 | if (!pag->pagi_init) { |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1363 | pag->pagi_freecount = be32_to_cpu(agi->agi_freecount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | pag->pagi_init = 1; |
| 1365 | } else { |
| 1366 | /* |
| 1367 | * It's possible for these to be out of sync if |
| 1368 | * we are in the middle of a forced shutdown. |
| 1369 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1370 | ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) || |
| 1371 | XFS_FORCED_SHUTDOWN(mp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | } |
| 1373 | |
| 1374 | #ifdef DEBUG |
| 1375 | { |
| 1376 | int i; |
| 1377 | |
| 1378 | for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++) |
| 1379 | ASSERT(agi->agi_unlinked[i]); |
| 1380 | } |
| 1381 | #endif |
| 1382 | |
| 1383 | XFS_BUF_SET_VTYPE_REF(bp, B_FS_AGI, XFS_AGI_REF); |
| 1384 | *bpp = bp; |
| 1385 | return 0; |
| 1386 | } |