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