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