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