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