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