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" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include "xfs_mount.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include "xfs_bmap_btree.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 30 | #include "xfs_alloc_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include "xfs_ialloc_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include "xfs_dir2_sf.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 33 | #include "xfs_attr_sf.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include "xfs_dinode.h" |
| 35 | #include "xfs_inode.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 36 | #include "xfs_btree.h" |
| 37 | #include "xfs_ialloc.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include "xfs_alloc.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include "xfs_rtalloc.h" |
| 40 | #include "xfs_error.h" |
| 41 | #include "xfs_bmap.h" |
| 42 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
| 44 | /* |
| 45 | * Allocation group level functions. |
| 46 | */ |
David Chinner | 75de2a9 | 2008-03-27 18:00:38 +1100 | [diff] [blame] | 47 | static inline int |
| 48 | xfs_ialloc_cluster_alignment( |
| 49 | xfs_alloc_arg_t *args) |
| 50 | { |
| 51 | if (xfs_sb_version_hasalign(&args->mp->m_sb) && |
| 52 | args->mp->m_sb.sb_inoalignmt >= |
| 53 | XFS_B_TO_FSBT(args->mp, XFS_INODE_CLUSTER_SIZE(args->mp))) |
| 54 | return args->mp->m_sb.sb_inoalignmt; |
| 55 | return 1; |
| 56 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
| 58 | /* |
Christoph Hellwig | 2187550 | 2009-08-31 20:58:21 -0300 | [diff] [blame] | 59 | * Lookup a record by ino in the btree given by cur. |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 60 | */ |
Christoph Hellwig | 81e2517 | 2009-09-01 19:56:55 -0400 | [diff] [blame] | 61 | int /* error */ |
Christoph Hellwig | 2187550 | 2009-08-31 20:58:21 -0300 | [diff] [blame] | 62 | xfs_inobt_lookup( |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 63 | struct xfs_btree_cur *cur, /* btree cursor */ |
| 64 | xfs_agino_t ino, /* starting inode of chunk */ |
Christoph Hellwig | 2187550 | 2009-08-31 20:58:21 -0300 | [diff] [blame] | 65 | xfs_lookup_t dir, /* <=, >=, == */ |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 66 | int *stat) /* success/failure */ |
| 67 | { |
| 68 | cur->bc_rec.i.ir_startino = ino; |
Christoph Hellwig | 2187550 | 2009-08-31 20:58:21 -0300 | [diff] [blame] | 69 | cur->bc_rec.i.ir_freecount = 0; |
| 70 | cur->bc_rec.i.ir_free = 0; |
| 71 | return xfs_btree_lookup(cur, dir, stat); |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /* |
Christoph Hellwig | afabc24 | 2009-08-31 20:57:03 -0300 | [diff] [blame] | 75 | * Update the record referred to by cur to the value given. |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 76 | * This either works (return 0) or gets an EFSCORRUPTED error. |
| 77 | */ |
| 78 | STATIC int /* error */ |
| 79 | xfs_inobt_update( |
| 80 | struct xfs_btree_cur *cur, /* btree cursor */ |
Christoph Hellwig | afabc24 | 2009-08-31 20:57:03 -0300 | [diff] [blame] | 81 | xfs_inobt_rec_incore_t *irec) /* btree record */ |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 82 | { |
| 83 | union xfs_btree_rec rec; |
| 84 | |
Christoph Hellwig | afabc24 | 2009-08-31 20:57:03 -0300 | [diff] [blame] | 85 | rec.inobt.ir_startino = cpu_to_be32(irec->ir_startino); |
| 86 | rec.inobt.ir_freecount = cpu_to_be32(irec->ir_freecount); |
| 87 | rec.inobt.ir_free = cpu_to_be64(irec->ir_free); |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 88 | return xfs_btree_update(cur, &rec); |
| 89 | } |
| 90 | |
| 91 | /* |
Christoph Hellwig | 8cc938f | 2008-10-30 16:58:11 +1100 | [diff] [blame] | 92 | * Get the data from the pointed-to record. |
| 93 | */ |
| 94 | int /* error */ |
| 95 | xfs_inobt_get_rec( |
| 96 | struct xfs_btree_cur *cur, /* btree cursor */ |
Christoph Hellwig | 2e287a7 | 2009-08-31 20:56:58 -0300 | [diff] [blame] | 97 | xfs_inobt_rec_incore_t *irec, /* btree record */ |
Christoph Hellwig | 8cc938f | 2008-10-30 16:58:11 +1100 | [diff] [blame] | 98 | int *stat) /* output: success/failure */ |
| 99 | { |
| 100 | union xfs_btree_rec *rec; |
| 101 | int error; |
| 102 | |
| 103 | error = xfs_btree_get_rec(cur, &rec, stat); |
| 104 | if (!error && *stat == 1) { |
Christoph Hellwig | 2e287a7 | 2009-08-31 20:56:58 -0300 | [diff] [blame] | 105 | irec->ir_startino = be32_to_cpu(rec->inobt.ir_startino); |
| 106 | irec->ir_freecount = be32_to_cpu(rec->inobt.ir_freecount); |
| 107 | irec->ir_free = be64_to_cpu(rec->inobt.ir_free); |
Christoph Hellwig | 8cc938f | 2008-10-30 16:58:11 +1100 | [diff] [blame] | 108 | } |
| 109 | return error; |
| 110 | } |
| 111 | |
| 112 | /* |
Dave Chinner | 0b48db8 | 2009-08-31 20:57:09 -0300 | [diff] [blame] | 113 | * Verify that the number of free inodes in the AGI is correct. |
| 114 | */ |
| 115 | #ifdef DEBUG |
| 116 | STATIC int |
| 117 | xfs_check_agi_freecount( |
| 118 | struct xfs_btree_cur *cur, |
| 119 | struct xfs_agi *agi) |
| 120 | { |
| 121 | if (cur->bc_nlevels == 1) { |
| 122 | xfs_inobt_rec_incore_t rec; |
| 123 | int freecount = 0; |
| 124 | int error; |
| 125 | int i; |
| 126 | |
Christoph Hellwig | 2187550 | 2009-08-31 20:58:21 -0300 | [diff] [blame] | 127 | error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i); |
Dave Chinner | 0b48db8 | 2009-08-31 20:57:09 -0300 | [diff] [blame] | 128 | if (error) |
| 129 | return error; |
| 130 | |
| 131 | do { |
| 132 | error = xfs_inobt_get_rec(cur, &rec, &i); |
| 133 | if (error) |
| 134 | return error; |
| 135 | |
| 136 | if (i) { |
| 137 | freecount += rec.ir_freecount; |
| 138 | error = xfs_btree_increment(cur, 0, &i); |
| 139 | if (error) |
| 140 | return error; |
| 141 | } |
| 142 | } while (i == 1); |
| 143 | |
| 144 | if (!XFS_FORCED_SHUTDOWN(cur->bc_mp)) |
| 145 | ASSERT(freecount == be32_to_cpu(agi->agi_freecount)); |
| 146 | } |
| 147 | return 0; |
| 148 | } |
| 149 | #else |
| 150 | #define xfs_check_agi_freecount(cur, agi) 0 |
| 151 | #endif |
| 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, |
Christoph Hellwig | 0cadda1 | 2010-01-19 09:56:44 +0000 | [diff] [blame] | 207 | XBF_LOCK); |
Dave Chinner | 85c0b2a | 2009-08-31 20:56:51 -0300 | [diff] [blame] | 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 */ |
Dave Chinner | 44b56e0 | 2010-01-11 11:47:43 +0000 | [diff] [blame] | 255 | struct xfs_perag *pag; |
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); |
Dave Chinner | 44b56e0 | 2010-01-11 11:47:43 +0000 | [diff] [blame] | 385 | pag = xfs_perag_get(args.mp, agno); |
| 386 | pag->pagi_freecount += newlen; |
| 387 | xfs_perag_put(pag); |
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 | |
Christoph Hellwig | b8f82a4 | 2009-11-14 16:17:22 +0000 | [diff] [blame] | 428 | STATIC 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | for (;;) { |
Dave Chinner | 44b56e0 | 2010-01-11 11:47:43 +0000 | [diff] [blame] | 490 | pag = xfs_perag_get(mp, agno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | if (!pag->pagi_init) { |
| 492 | if (xfs_ialloc_read_agi(mp, tp, agno, &agbp)) { |
| 493 | agbp = NULL; |
| 494 | goto nextag; |
| 495 | } |
| 496 | } else |
| 497 | agbp = NULL; |
| 498 | |
| 499 | if (!pag->pagi_inodeok) { |
| 500 | xfs_ialloc_next_ag(mp); |
| 501 | goto unlock_nextag; |
| 502 | } |
| 503 | |
| 504 | /* |
| 505 | * Is there enough free space for the file plus a block |
| 506 | * of inodes (if we need to allocate some)? |
| 507 | */ |
| 508 | ineed = pag->pagi_freecount ? 0 : XFS_IALLOC_BLOCKS(mp); |
| 509 | if (ineed && !pag->pagf_init) { |
| 510 | if (agbp == NULL && |
| 511 | xfs_ialloc_read_agi(mp, tp, agno, &agbp)) { |
| 512 | agbp = NULL; |
| 513 | goto nextag; |
| 514 | } |
| 515 | (void)xfs_alloc_pagf_init(mp, tp, agno, flags); |
| 516 | } |
| 517 | if (!ineed || pag->pagf_init) { |
| 518 | if (ineed && !(longest = pag->pagf_longest)) |
| 519 | longest = pag->pagf_flcount > 0; |
| 520 | if (!ineed || |
| 521 | (pag->pagf_freeblks >= needspace + ineed && |
| 522 | longest >= ineed && |
| 523 | okalloc)) { |
| 524 | if (agbp == NULL && |
| 525 | xfs_ialloc_read_agi(mp, tp, agno, &agbp)) { |
| 526 | agbp = NULL; |
| 527 | goto nextag; |
| 528 | } |
Dave Chinner | 44b56e0 | 2010-01-11 11:47:43 +0000 | [diff] [blame] | 529 | xfs_perag_put(pag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | return agbp; |
| 531 | } |
| 532 | } |
| 533 | unlock_nextag: |
| 534 | if (agbp) |
| 535 | xfs_trans_brelse(tp, agbp); |
| 536 | nextag: |
Dave Chinner | 44b56e0 | 2010-01-11 11:47:43 +0000 | [diff] [blame] | 537 | xfs_perag_put(pag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | /* |
| 539 | * No point in iterating over the rest, if we're shutting |
| 540 | * down. |
| 541 | */ |
Dave Chinner | 1c1c6eb | 2010-01-11 11:47:44 +0000 | [diff] [blame] | 542 | if (XFS_FORCED_SHUTDOWN(mp)) |
Nathan Scott | 1121b21 | 2006-09-28 10:58:40 +1000 | [diff] [blame] | 543 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | agno++; |
| 545 | if (agno >= agcount) |
| 546 | agno = 0; |
| 547 | if (agno == pagno) { |
Dave Chinner | 1c1c6eb | 2010-01-11 11:47:44 +0000 | [diff] [blame] | 548 | if (flags == 0) |
Nathan Scott | 1121b21 | 2006-09-28 10:58:40 +1000 | [diff] [blame] | 549 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | flags = 0; |
| 551 | } |
| 552 | } |
| 553 | } |
| 554 | |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 555 | /* |
| 556 | * Try to retrieve the next record to the left/right from the current one. |
| 557 | */ |
| 558 | STATIC int |
| 559 | xfs_ialloc_next_rec( |
| 560 | struct xfs_btree_cur *cur, |
| 561 | xfs_inobt_rec_incore_t *rec, |
| 562 | int *done, |
| 563 | int left) |
| 564 | { |
| 565 | int error; |
| 566 | int i; |
| 567 | |
| 568 | if (left) |
| 569 | error = xfs_btree_decrement(cur, 0, &i); |
| 570 | else |
| 571 | error = xfs_btree_increment(cur, 0, &i); |
| 572 | |
| 573 | if (error) |
| 574 | return error; |
| 575 | *done = !i; |
| 576 | if (i) { |
| 577 | error = xfs_inobt_get_rec(cur, rec, &i); |
| 578 | if (error) |
| 579 | return error; |
| 580 | XFS_WANT_CORRUPTED_RETURN(i == 1); |
| 581 | } |
| 582 | |
| 583 | return 0; |
| 584 | } |
| 585 | |
Dave Chinner | bd16956 | 2009-08-31 20:58:28 -0300 | [diff] [blame] | 586 | STATIC int |
| 587 | xfs_ialloc_get_rec( |
| 588 | struct xfs_btree_cur *cur, |
| 589 | xfs_agino_t agino, |
| 590 | xfs_inobt_rec_incore_t *rec, |
| 591 | int *done, |
| 592 | int left) |
| 593 | { |
| 594 | int error; |
| 595 | int i; |
| 596 | |
| 597 | error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_EQ, &i); |
| 598 | if (error) |
| 599 | return error; |
| 600 | *done = !i; |
| 601 | if (i) { |
| 602 | error = xfs_inobt_get_rec(cur, rec, &i); |
| 603 | if (error) |
| 604 | return error; |
| 605 | XFS_WANT_CORRUPTED_RETURN(i == 1); |
| 606 | } |
| 607 | |
| 608 | return 0; |
| 609 | } |
Dave Chinner | 0b48db8 | 2009-08-31 20:57:09 -0300 | [diff] [blame] | 610 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | /* |
| 612 | * Visible inode allocation functions. |
| 613 | */ |
| 614 | |
| 615 | /* |
| 616 | * Allocate an inode on disk. |
| 617 | * Mode is used to tell whether the new inode will need space, and whether |
| 618 | * it is a directory. |
| 619 | * |
| 620 | * The arguments IO_agbp and alloc_done are defined to work within |
| 621 | * the constraint of one allocation per transaction. |
| 622 | * xfs_dialloc() is designed to be called twice if it has to do an |
| 623 | * allocation to make more free inodes. On the first call, |
| 624 | * IO_agbp should be set to NULL. If an inode is available, |
| 625 | * i.e., xfs_dialloc() did not need to do an allocation, an inode |
| 626 | * number is returned. In this case, IO_agbp would be set to the |
| 627 | * current ag_buf and alloc_done set to false. |
| 628 | * If an allocation needed to be done, xfs_dialloc would return |
| 629 | * the current ag_buf in IO_agbp and set alloc_done to true. |
| 630 | * The caller should then commit the current transaction, allocate a new |
| 631 | * transaction, and call xfs_dialloc() again, passing in the previous |
| 632 | * value of IO_agbp. IO_agbp should be held across the transactions. |
| 633 | * Since the agbp is locked across the two calls, the second call is |
| 634 | * guaranteed to have a free inode available. |
| 635 | * |
| 636 | * Once we successfully pick an inode its number is returned and the |
| 637 | * on-disk data structures are updated. The inode itself is not read |
| 638 | * in, since doing so would break ordering constraints with xfs_reclaim. |
| 639 | */ |
| 640 | int |
| 641 | xfs_dialloc( |
| 642 | xfs_trans_t *tp, /* transaction pointer */ |
| 643 | xfs_ino_t parent, /* parent inode (directory) */ |
| 644 | mode_t mode, /* mode bits for new inode */ |
| 645 | int okalloc, /* ok to allocate more space */ |
| 646 | xfs_buf_t **IO_agbp, /* in/out ag header's buffer */ |
| 647 | boolean_t *alloc_done, /* true if we needed to replenish |
| 648 | inode freelist */ |
| 649 | xfs_ino_t *inop) /* inode number allocated */ |
| 650 | { |
| 651 | xfs_agnumber_t agcount; /* number of allocation groups */ |
| 652 | xfs_buf_t *agbp; /* allocation group header's buffer */ |
| 653 | xfs_agnumber_t agno; /* allocation group number */ |
| 654 | xfs_agi_t *agi; /* allocation group header structure */ |
| 655 | xfs_btree_cur_t *cur; /* inode allocation btree cursor */ |
| 656 | int error; /* error return value */ |
| 657 | int i; /* result code */ |
| 658 | int ialloced; /* inode allocation status */ |
| 659 | int noroom = 0; /* no space for inode blk allocation */ |
| 660 | xfs_ino_t ino; /* fs-relative inode to be returned */ |
| 661 | /* REFERENCED */ |
| 662 | int j; /* result code */ |
| 663 | xfs_mount_t *mp; /* file system mount structure */ |
| 664 | int offset; /* index of inode in chunk */ |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 665 | xfs_agino_t pagino; /* parent's AG relative inode # */ |
| 666 | xfs_agnumber_t pagno; /* parent's AG number */ |
Christoph Hellwig | 61a2584 | 2006-09-28 10:57:04 +1000 | [diff] [blame] | 667 | xfs_inobt_rec_incore_t rec; /* inode allocation record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | xfs_agnumber_t tagno; /* testing allocation group number */ |
| 669 | xfs_btree_cur_t *tcur; /* temp cursor */ |
Christoph Hellwig | 61a2584 | 2006-09-28 10:57:04 +1000 | [diff] [blame] | 670 | xfs_inobt_rec_incore_t trec; /* temp inode allocation record */ |
Dave Chinner | 44b56e0 | 2010-01-11 11:47:43 +0000 | [diff] [blame] | 671 | struct xfs_perag *pag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | |
| 673 | |
| 674 | if (*IO_agbp == NULL) { |
| 675 | /* |
| 676 | * We do not have an agbp, so select an initial allocation |
| 677 | * group for inode allocation. |
| 678 | */ |
| 679 | agbp = xfs_ialloc_ag_select(tp, parent, mode, okalloc); |
| 680 | /* |
| 681 | * Couldn't find an allocation group satisfying the |
| 682 | * criteria, give up. |
| 683 | */ |
| 684 | if (!agbp) { |
| 685 | *inop = NULLFSINO; |
| 686 | return 0; |
| 687 | } |
| 688 | agi = XFS_BUF_TO_AGI(agbp); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 689 | ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | } else { |
| 691 | /* |
| 692 | * Continue where we left off before. In this case, we |
| 693 | * know that the allocation group has free inodes. |
| 694 | */ |
| 695 | agbp = *IO_agbp; |
| 696 | agi = XFS_BUF_TO_AGI(agbp); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 697 | ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC); |
| 698 | ASSERT(be32_to_cpu(agi->agi_freecount) > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | } |
| 700 | mp = tp->t_mountp; |
| 701 | agcount = mp->m_sb.sb_agcount; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 702 | agno = be32_to_cpu(agi->agi_seqno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | tagno = agno; |
| 704 | pagno = XFS_INO_TO_AGNO(mp, parent); |
| 705 | pagino = XFS_INO_TO_AGINO(mp, parent); |
| 706 | |
| 707 | /* |
| 708 | * If we have already hit the ceiling of inode blocks then clear |
| 709 | * okalloc so we scan all available agi structures for a free |
| 710 | * inode. |
| 711 | */ |
| 712 | |
| 713 | if (mp->m_maxicount && |
| 714 | mp->m_sb.sb_icount + XFS_IALLOC_INODES(mp) > mp->m_maxicount) { |
| 715 | noroom = 1; |
| 716 | okalloc = 0; |
| 717 | } |
| 718 | |
| 719 | /* |
| 720 | * Loop until we find an allocation group that either has free inodes |
| 721 | * or in which we can allocate some inodes. Iterate through the |
| 722 | * allocation groups upward, wrapping at the end. |
| 723 | */ |
| 724 | *alloc_done = B_FALSE; |
| 725 | while (!agi->agi_freecount) { |
| 726 | /* |
| 727 | * Don't do anything if we're not supposed to allocate |
| 728 | * any blocks, just go on to the next ag. |
| 729 | */ |
| 730 | if (okalloc) { |
| 731 | /* |
| 732 | * Try to allocate some new inodes in the allocation |
| 733 | * group. |
| 734 | */ |
| 735 | if ((error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced))) { |
| 736 | xfs_trans_brelse(tp, agbp); |
| 737 | if (error == ENOSPC) { |
| 738 | *inop = NULLFSINO; |
| 739 | return 0; |
| 740 | } else |
| 741 | return error; |
| 742 | } |
| 743 | if (ialloced) { |
| 744 | /* |
| 745 | * We successfully allocated some inodes, return |
| 746 | * the current context to the caller so that it |
| 747 | * can commit the current transaction and call |
| 748 | * us again where we left off. |
| 749 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 750 | ASSERT(be32_to_cpu(agi->agi_freecount) > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | *alloc_done = B_TRUE; |
| 752 | *IO_agbp = agbp; |
| 753 | *inop = NULLFSINO; |
| 754 | return 0; |
| 755 | } |
| 756 | } |
| 757 | /* |
| 758 | * If it failed, give up on this ag. |
| 759 | */ |
| 760 | xfs_trans_brelse(tp, agbp); |
| 761 | /* |
| 762 | * Go on to the next ag: get its ag header. |
| 763 | */ |
| 764 | nextag: |
| 765 | if (++tagno == agcount) |
| 766 | tagno = 0; |
| 767 | if (tagno == agno) { |
| 768 | *inop = NULLFSINO; |
| 769 | return noroom ? ENOSPC : 0; |
| 770 | } |
Dave Chinner | 44b56e0 | 2010-01-11 11:47:43 +0000 | [diff] [blame] | 771 | pag = xfs_perag_get(mp, tagno); |
| 772 | if (pag->pagi_inodeok == 0) { |
| 773 | xfs_perag_put(pag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | goto nextag; |
| 775 | } |
| 776 | error = xfs_ialloc_read_agi(mp, tp, tagno, &agbp); |
Dave Chinner | 44b56e0 | 2010-01-11 11:47:43 +0000 | [diff] [blame] | 777 | xfs_perag_put(pag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | if (error) |
| 779 | goto nextag; |
| 780 | agi = XFS_BUF_TO_AGI(agbp); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 781 | ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | } |
| 783 | /* |
| 784 | * Here with an allocation group that has a free inode. |
| 785 | * Reset agno since we may have chosen a new ag in the |
| 786 | * loop above. |
| 787 | */ |
| 788 | agno = tagno; |
| 789 | *IO_agbp = NULL; |
Dave Chinner | 44b56e0 | 2010-01-11 11:47:43 +0000 | [diff] [blame] | 790 | pag = xfs_perag_get(mp, agno); |
Dave Chinner | bd16956 | 2009-08-31 20:58:28 -0300 | [diff] [blame] | 791 | |
| 792 | restart_pagno: |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 793 | 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] | 794 | /* |
| 795 | * If pagino is 0 (this is the root inode allocation) use newino. |
| 796 | * This must work because we've just allocated some. |
| 797 | */ |
| 798 | if (!pagino) |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 799 | pagino = be32_to_cpu(agi->agi_newino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | |
Dave Chinner | 0b48db8 | 2009-08-31 20:57:09 -0300 | [diff] [blame] | 801 | error = xfs_check_agi_freecount(cur, agi); |
| 802 | if (error) |
| 803 | goto error0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | /* |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 806 | * 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] | 807 | */ |
| 808 | if (pagno == agno) { |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 809 | int doneleft; /* done, to the left */ |
| 810 | int doneright; /* done, to the right */ |
Dave Chinner | bd16956 | 2009-08-31 20:58:28 -0300 | [diff] [blame] | 811 | int searchdistance = 10; |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 812 | |
Christoph Hellwig | 2187550 | 2009-08-31 20:58:21 -0300 | [diff] [blame] | 813 | error = xfs_inobt_lookup(cur, pagino, XFS_LOOKUP_LE, &i); |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 814 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | goto error0; |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 816 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); |
| 817 | |
| 818 | error = xfs_inobt_get_rec(cur, &rec, &j); |
| 819 | if (error) |
| 820 | goto error0; |
| 821 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); |
| 822 | |
| 823 | if (rec.ir_freecount > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | /* |
| 825 | * Found a free inode in the same chunk |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 826 | * as the parent, done. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | */ |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 828 | goto alloc_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | } |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 830 | |
| 831 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | /* |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 833 | * In the same AG as parent, but parent's chunk is full. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 836 | /* duplicate the cursor, search left & right simultaneously */ |
| 837 | error = xfs_btree_dup_cursor(cur, &tcur); |
| 838 | if (error) |
| 839 | goto error0; |
| 840 | |
Dave Chinner | bd16956 | 2009-08-31 20:58:28 -0300 | [diff] [blame] | 841 | /* |
| 842 | * Skip to last blocks looked up if same parent inode. |
| 843 | */ |
| 844 | if (pagino != NULLAGINO && |
| 845 | pag->pagl_pagino == pagino && |
| 846 | pag->pagl_leftrec != NULLAGINO && |
| 847 | pag->pagl_rightrec != NULLAGINO) { |
| 848 | error = xfs_ialloc_get_rec(tcur, pag->pagl_leftrec, |
| 849 | &trec, &doneleft, 1); |
| 850 | if (error) |
| 851 | goto error1; |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 852 | |
Dave Chinner | bd16956 | 2009-08-31 20:58:28 -0300 | [diff] [blame] | 853 | error = xfs_ialloc_get_rec(cur, pag->pagl_rightrec, |
| 854 | &rec, &doneright, 0); |
| 855 | if (error) |
| 856 | goto error1; |
| 857 | } else { |
| 858 | /* search left with tcur, back up 1 record */ |
| 859 | error = xfs_ialloc_next_rec(tcur, &trec, &doneleft, 1); |
| 860 | if (error) |
| 861 | goto error1; |
| 862 | |
| 863 | /* search right with cur, go forward 1 record. */ |
| 864 | error = xfs_ialloc_next_rec(cur, &rec, &doneright, 0); |
| 865 | if (error) |
| 866 | goto error1; |
| 867 | } |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 868 | |
| 869 | /* |
| 870 | * Loop until we find an inode chunk with a free inode. |
| 871 | */ |
| 872 | while (!doneleft || !doneright) { |
| 873 | int useleft; /* using left inode chunk this time */ |
| 874 | |
Dave Chinner | bd16956 | 2009-08-31 20:58:28 -0300 | [diff] [blame] | 875 | if (!--searchdistance) { |
| 876 | /* |
| 877 | * Not in range - save last search |
| 878 | * location and allocate a new inode |
| 879 | */ |
Eric Sandeen | 3b82638 | 2009-10-30 09:27:07 +0100 | [diff] [blame] | 880 | xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); |
Dave Chinner | bd16956 | 2009-08-31 20:58:28 -0300 | [diff] [blame] | 881 | pag->pagl_leftrec = trec.ir_startino; |
| 882 | pag->pagl_rightrec = rec.ir_startino; |
| 883 | pag->pagl_pagino = pagino; |
| 884 | goto newino; |
| 885 | } |
| 886 | |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 887 | /* figure out the closer block if both are valid. */ |
| 888 | if (!doneleft && !doneright) { |
| 889 | useleft = pagino - |
| 890 | (trec.ir_startino + XFS_INODES_PER_CHUNK - 1) < |
| 891 | rec.ir_startino - pagino; |
| 892 | } else { |
| 893 | useleft = !doneleft; |
| 894 | } |
| 895 | |
| 896 | /* free inodes to the left? */ |
| 897 | if (useleft && trec.ir_freecount) { |
| 898 | rec = trec; |
| 899 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 900 | cur = tcur; |
Dave Chinner | bd16956 | 2009-08-31 20:58:28 -0300 | [diff] [blame] | 901 | |
| 902 | pag->pagl_leftrec = trec.ir_startino; |
| 903 | pag->pagl_rightrec = rec.ir_startino; |
| 904 | pag->pagl_pagino = pagino; |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 905 | goto alloc_inode; |
| 906 | } |
| 907 | |
| 908 | /* free inodes to the right? */ |
| 909 | if (!useleft && rec.ir_freecount) { |
| 910 | xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); |
Dave Chinner | bd16956 | 2009-08-31 20:58:28 -0300 | [diff] [blame] | 911 | |
| 912 | pag->pagl_leftrec = trec.ir_startino; |
| 913 | pag->pagl_rightrec = rec.ir_startino; |
| 914 | pag->pagl_pagino = pagino; |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 915 | goto alloc_inode; |
| 916 | } |
| 917 | |
| 918 | /* get next record to check */ |
| 919 | if (useleft) { |
| 920 | error = xfs_ialloc_next_rec(tcur, &trec, |
| 921 | &doneleft, 1); |
| 922 | } else { |
| 923 | error = xfs_ialloc_next_rec(cur, &rec, |
| 924 | &doneright, 0); |
| 925 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | goto error1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | } |
Dave Chinner | bd16956 | 2009-08-31 20:58:28 -0300 | [diff] [blame] | 929 | |
| 930 | /* |
| 931 | * We've reached the end of the btree. because |
| 932 | * we are only searching a small chunk of the |
| 933 | * btree each search, there is obviously free |
| 934 | * inodes closer to the parent inode than we |
| 935 | * are now. restart the search again. |
| 936 | */ |
| 937 | pag->pagl_pagino = NULLAGINO; |
| 938 | pag->pagl_leftrec = NULLAGINO; |
| 939 | pag->pagl_rightrec = NULLAGINO; |
| 940 | xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); |
| 941 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 942 | goto restart_pagno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | } |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 944 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | /* |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 946 | * In a different AG from the parent. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | * See if the most recently allocated block has any free. |
| 948 | */ |
Dave Chinner | bd16956 | 2009-08-31 20:58:28 -0300 | [diff] [blame] | 949 | newino: |
| 950 | if (be32_to_cpu(agi->agi_newino) != NULLAGINO) { |
Christoph Hellwig | 2187550 | 2009-08-31 20:58:21 -0300 | [diff] [blame] | 951 | error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino), |
| 952 | XFS_LOOKUP_EQ, &i); |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 953 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | goto error0; |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 955 | |
| 956 | if (i == 1) { |
| 957 | error = xfs_inobt_get_rec(cur, &rec, &j); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | if (error) |
| 959 | goto error0; |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 960 | |
| 961 | if (j == 1 && rec.ir_freecount > 0) { |
| 962 | /* |
| 963 | * The last chunk allocated in the group |
| 964 | * still has a free inode. |
| 965 | */ |
| 966 | goto alloc_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | } |
| 968 | } |
Dave Chinner | bd16956 | 2009-08-31 20:58:28 -0300 | [diff] [blame] | 969 | } |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 970 | |
Dave Chinner | bd16956 | 2009-08-31 20:58:28 -0300 | [diff] [blame] | 971 | /* |
| 972 | * None left in the last group, search the whole AG |
| 973 | */ |
| 974 | error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i); |
| 975 | if (error) |
| 976 | goto error0; |
| 977 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); |
| 978 | |
| 979 | for (;;) { |
| 980 | error = xfs_inobt_get_rec(cur, &rec, &i); |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 981 | if (error) |
| 982 | goto error0; |
| 983 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); |
Dave Chinner | bd16956 | 2009-08-31 20:58:28 -0300 | [diff] [blame] | 984 | if (rec.ir_freecount > 0) |
| 985 | break; |
| 986 | error = xfs_btree_increment(cur, 0, &i); |
| 987 | if (error) |
| 988 | goto error0; |
| 989 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | } |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 991 | |
| 992 | alloc_inode: |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 993 | offset = xfs_ialloc_find_free(&rec.ir_free); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | ASSERT(offset >= 0); |
| 995 | ASSERT(offset < XFS_INODES_PER_CHUNK); |
| 996 | ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) % |
| 997 | XFS_INODES_PER_CHUNK) == 0); |
| 998 | ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset); |
Christoph Hellwig | 0d87e65 | 2009-02-09 08:37:14 +0100 | [diff] [blame] | 999 | rec.ir_free &= ~XFS_INOBT_MASK(offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | rec.ir_freecount--; |
Christoph Hellwig | afabc24 | 2009-08-31 20:57:03 -0300 | [diff] [blame] | 1001 | error = xfs_inobt_update(cur, &rec); |
| 1002 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | goto error0; |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 1004 | be32_add_cpu(&agi->agi_freecount, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT); |
Dave Chinner | 44b56e0 | 2010-01-11 11:47:43 +0000 | [diff] [blame] | 1006 | pag->pagi_freecount--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | |
Dave Chinner | 0b48db8 | 2009-08-31 20:57:09 -0300 | [diff] [blame] | 1008 | error = xfs_check_agi_freecount(cur, agi); |
| 1009 | if (error) |
| 1010 | goto error0; |
| 1011 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 1013 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1); |
Dave Chinner | 44b56e0 | 2010-01-11 11:47:43 +0000 | [diff] [blame] | 1014 | xfs_perag_put(pag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | *inop = ino; |
| 1016 | return 0; |
| 1017 | error1: |
| 1018 | xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR); |
| 1019 | error0: |
| 1020 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
Dave Chinner | 44b56e0 | 2010-01-11 11:47:43 +0000 | [diff] [blame] | 1021 | xfs_perag_put(pag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | return error; |
| 1023 | } |
| 1024 | |
| 1025 | /* |
| 1026 | * Free disk inode. Carefully avoids touching the incore inode, all |
| 1027 | * manipulations incore are the caller's responsibility. |
| 1028 | * The on-disk inode is not changed by this operation, only the |
| 1029 | * btree (free inode mask) is changed. |
| 1030 | */ |
| 1031 | int |
| 1032 | xfs_difree( |
| 1033 | xfs_trans_t *tp, /* transaction pointer */ |
| 1034 | xfs_ino_t inode, /* inode to be freed */ |
| 1035 | xfs_bmap_free_t *flist, /* extents to free */ |
| 1036 | int *delete, /* set if inode cluster was deleted */ |
| 1037 | xfs_ino_t *first_ino) /* first inode in deleted cluster */ |
| 1038 | { |
| 1039 | /* REFERENCED */ |
| 1040 | xfs_agblock_t agbno; /* block number containing inode */ |
| 1041 | xfs_buf_t *agbp; /* buffer containing allocation group header */ |
| 1042 | xfs_agino_t agino; /* inode number relative to allocation group */ |
| 1043 | xfs_agnumber_t agno; /* allocation group number */ |
| 1044 | xfs_agi_t *agi; /* allocation group header */ |
| 1045 | xfs_btree_cur_t *cur; /* inode btree cursor */ |
| 1046 | int error; /* error return value */ |
| 1047 | int i; /* result code */ |
| 1048 | int ilen; /* inodes in an inode cluster */ |
| 1049 | xfs_mount_t *mp; /* mount structure for filesystem */ |
| 1050 | int off; /* offset of inode in inode chunk */ |
Christoph Hellwig | 61a2584 | 2006-09-28 10:57:04 +1000 | [diff] [blame] | 1051 | xfs_inobt_rec_incore_t rec; /* btree record */ |
Dave Chinner | 44b56e0 | 2010-01-11 11:47:43 +0000 | [diff] [blame] | 1052 | struct xfs_perag *pag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | |
| 1054 | mp = tp->t_mountp; |
| 1055 | |
| 1056 | /* |
| 1057 | * Break up inode number into its components. |
| 1058 | */ |
| 1059 | agno = XFS_INO_TO_AGNO(mp, inode); |
| 1060 | if (agno >= mp->m_sb.sb_agcount) { |
| 1061 | cmn_err(CE_WARN, |
| 1062 | "xfs_difree: agno >= mp->m_sb.sb_agcount (%d >= %d) on %s. Returning EINVAL.", |
| 1063 | agno, mp->m_sb.sb_agcount, mp->m_fsname); |
| 1064 | ASSERT(0); |
| 1065 | return XFS_ERROR(EINVAL); |
| 1066 | } |
| 1067 | agino = XFS_INO_TO_AGINO(mp, inode); |
| 1068 | if (inode != XFS_AGINO_TO_INO(mp, agno, agino)) { |
| 1069 | cmn_err(CE_WARN, |
Christoph Hellwig | da1650a | 2005-11-02 10:21:35 +1100 | [diff] [blame] | 1070 | "xfs_difree: inode != XFS_AGINO_TO_INO() " |
| 1071 | "(%llu != %llu) on %s. Returning EINVAL.", |
| 1072 | (unsigned long long)inode, |
| 1073 | (unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino), |
| 1074 | mp->m_fsname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | ASSERT(0); |
| 1076 | return XFS_ERROR(EINVAL); |
| 1077 | } |
| 1078 | agbno = XFS_AGINO_TO_AGBNO(mp, agino); |
| 1079 | if (agbno >= mp->m_sb.sb_agblocks) { |
| 1080 | cmn_err(CE_WARN, |
| 1081 | "xfs_difree: agbno >= mp->m_sb.sb_agblocks (%d >= %d) on %s. Returning EINVAL.", |
| 1082 | agbno, mp->m_sb.sb_agblocks, mp->m_fsname); |
| 1083 | ASSERT(0); |
| 1084 | return XFS_ERROR(EINVAL); |
| 1085 | } |
| 1086 | /* |
| 1087 | * Get the allocation group header. |
| 1088 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | error = xfs_ialloc_read_agi(mp, tp, agno, &agbp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | if (error) { |
| 1091 | cmn_err(CE_WARN, |
| 1092 | "xfs_difree: xfs_ialloc_read_agi() returned an error %d on %s. Returning error.", |
| 1093 | error, mp->m_fsname); |
| 1094 | return error; |
| 1095 | } |
| 1096 | agi = XFS_BUF_TO_AGI(agbp); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1097 | ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC); |
| 1098 | ASSERT(agbno < be32_to_cpu(agi->agi_length)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | /* |
| 1100 | * Initialize the cursor. |
| 1101 | */ |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 1102 | cur = xfs_inobt_init_cursor(mp, tp, agbp, agno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | |
Dave Chinner | 0b48db8 | 2009-08-31 20:57:09 -0300 | [diff] [blame] | 1104 | error = xfs_check_agi_freecount(cur, agi); |
| 1105 | if (error) |
| 1106 | goto error0; |
| 1107 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | /* |
| 1109 | * Look for the entry describing this inode. |
| 1110 | */ |
Christoph Hellwig | 2187550 | 2009-08-31 20:58:21 -0300 | [diff] [blame] | 1111 | if ((error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | cmn_err(CE_WARN, |
Christoph Hellwig | 2187550 | 2009-08-31 20:58:21 -0300 | [diff] [blame] | 1113 | "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] | 1114 | error, mp->m_fsname); |
| 1115 | goto error0; |
| 1116 | } |
| 1117 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); |
Christoph Hellwig | 2e287a7 | 2009-08-31 20:56:58 -0300 | [diff] [blame] | 1118 | error = xfs_inobt_get_rec(cur, &rec, &i); |
| 1119 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | cmn_err(CE_WARN, |
| 1121 | "xfs_difree: xfs_inobt_get_rec() returned an error %d on %s. Returning error.", |
| 1122 | error, mp->m_fsname); |
| 1123 | goto error0; |
| 1124 | } |
| 1125 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); |
| 1126 | /* |
| 1127 | * Get the offset in the inode chunk. |
| 1128 | */ |
| 1129 | off = agino - rec.ir_startino; |
| 1130 | ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK); |
Christoph Hellwig | 0d87e65 | 2009-02-09 08:37:14 +0100 | [diff] [blame] | 1131 | ASSERT(!(rec.ir_free & XFS_INOBT_MASK(off))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | /* |
| 1133 | * Mark the inode free & increment the count. |
| 1134 | */ |
Christoph Hellwig | 0d87e65 | 2009-02-09 08:37:14 +0100 | [diff] [blame] | 1135 | rec.ir_free |= XFS_INOBT_MASK(off); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | rec.ir_freecount++; |
| 1137 | |
| 1138 | /* |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 1139 | * When an inode cluster is free, it becomes eligible for removal |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | */ |
Josef Jeff Sipek | 1bd960e | 2008-02-29 13:58:40 +1100 | [diff] [blame] | 1141 | if (!(mp->m_flags & XFS_MOUNT_IKEEP) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | (rec.ir_freecount == XFS_IALLOC_INODES(mp))) { |
| 1143 | |
| 1144 | *delete = 1; |
| 1145 | *first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino); |
| 1146 | |
| 1147 | /* |
| 1148 | * Remove the inode cluster from the AGI B+Tree, adjust the |
| 1149 | * AGI and Superblock inode counts, and mark the disk space |
| 1150 | * to be freed when the transaction is committed. |
| 1151 | */ |
| 1152 | ilen = XFS_IALLOC_INODES(mp); |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 1153 | be32_add_cpu(&agi->agi_count, -ilen); |
| 1154 | be32_add_cpu(&agi->agi_freecount, -(ilen - 1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT); |
Dave Chinner | 44b56e0 | 2010-01-11 11:47:43 +0000 | [diff] [blame] | 1156 | pag = xfs_perag_get(mp, agno); |
| 1157 | pag->pagi_freecount -= ilen - 1; |
| 1158 | xfs_perag_put(pag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen); |
| 1160 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1)); |
| 1161 | |
Christoph Hellwig | 91cca5d | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 1162 | if ((error = xfs_btree_delete(cur, &i))) { |
| 1163 | 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] | 1164 | error, mp->m_fsname); |
| 1165 | goto error0; |
| 1166 | } |
| 1167 | |
| 1168 | xfs_bmap_add_free(XFS_AGB_TO_FSB(mp, |
| 1169 | agno, XFS_INO_TO_AGBNO(mp,rec.ir_startino)), |
| 1170 | XFS_IALLOC_BLOCKS(mp), flist, mp); |
| 1171 | } else { |
| 1172 | *delete = 0; |
| 1173 | |
Christoph Hellwig | afabc24 | 2009-08-31 20:57:03 -0300 | [diff] [blame] | 1174 | error = xfs_inobt_update(cur, &rec); |
| 1175 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | cmn_err(CE_WARN, |
Christoph Hellwig | afabc24 | 2009-08-31 20:57:03 -0300 | [diff] [blame] | 1177 | "xfs_difree: xfs_inobt_update returned an error %d on %s.", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | error, mp->m_fsname); |
| 1179 | goto error0; |
| 1180 | } |
Christoph Hellwig | afabc24 | 2009-08-31 20:57:03 -0300 | [diff] [blame] | 1181 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | /* |
| 1183 | * Change the inode free counts and log the ag/sb changes. |
| 1184 | */ |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 1185 | be32_add_cpu(&agi->agi_freecount, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT); |
Dave Chinner | 44b56e0 | 2010-01-11 11:47:43 +0000 | [diff] [blame] | 1187 | pag = xfs_perag_get(mp, agno); |
| 1188 | pag->pagi_freecount++; |
| 1189 | xfs_perag_put(pag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1); |
| 1191 | } |
| 1192 | |
Dave Chinner | 0b48db8 | 2009-08-31 20:57:09 -0300 | [diff] [blame] | 1193 | error = xfs_check_agi_freecount(cur, agi); |
| 1194 | if (error) |
| 1195 | goto error0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1197 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 1198 | return 0; |
| 1199 | |
| 1200 | error0: |
| 1201 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 1202 | return error; |
| 1203 | } |
| 1204 | |
Dave Chinner | 7124fe0 | 2010-06-24 11:15:33 +1000 | [diff] [blame] | 1205 | STATIC int |
| 1206 | xfs_imap_lookup( |
| 1207 | struct xfs_mount *mp, |
| 1208 | struct xfs_trans *tp, |
| 1209 | xfs_agnumber_t agno, |
| 1210 | xfs_agino_t agino, |
| 1211 | xfs_agblock_t agbno, |
| 1212 | xfs_agblock_t *chunk_agbno, |
| 1213 | xfs_agblock_t *offset_agbno, |
| 1214 | int flags) |
| 1215 | { |
| 1216 | struct xfs_inobt_rec_incore rec; |
| 1217 | struct xfs_btree_cur *cur; |
| 1218 | struct xfs_buf *agbp; |
| 1219 | xfs_agino_t startino; |
| 1220 | int error; |
| 1221 | int i; |
| 1222 | |
| 1223 | error = xfs_ialloc_read_agi(mp, tp, agno, &agbp); |
| 1224 | if (error) { |
| 1225 | xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: " |
| 1226 | "xfs_ialloc_read_agi() returned " |
| 1227 | "error %d, agno %d", |
| 1228 | error, agno); |
| 1229 | return error; |
| 1230 | } |
| 1231 | |
| 1232 | /* |
| 1233 | * derive and lookup the exact inode record for the given agino. If the |
| 1234 | * record cannot be found, then it's an invalid inode number and we |
| 1235 | * should abort. |
| 1236 | */ |
| 1237 | cur = xfs_inobt_init_cursor(mp, tp, agbp, agno); |
| 1238 | startino = agino & ~(XFS_IALLOC_INODES(mp) - 1); |
| 1239 | error = xfs_inobt_lookup(cur, startino, XFS_LOOKUP_EQ, &i); |
| 1240 | if (!error) { |
| 1241 | if (i) |
| 1242 | error = xfs_inobt_get_rec(cur, &rec, &i); |
| 1243 | if (!error && i == 0) |
| 1244 | error = EINVAL; |
| 1245 | } |
| 1246 | |
| 1247 | xfs_trans_brelse(tp, agbp); |
| 1248 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 1249 | if (error) |
| 1250 | return error; |
| 1251 | |
| 1252 | /* for untrusted inodes check it is allocated first */ |
Dave Chinner | 1920779 | 2010-06-24 11:15:47 +1000 | [diff] [blame] | 1253 | if ((flags & XFS_IGET_UNTRUSTED) && |
Dave Chinner | 7124fe0 | 2010-06-24 11:15:33 +1000 | [diff] [blame] | 1254 | (rec.ir_free & XFS_INOBT_MASK(agino - rec.ir_startino))) |
| 1255 | return EINVAL; |
| 1256 | |
| 1257 | *chunk_agbno = XFS_AGINO_TO_AGBNO(mp, rec.ir_startino); |
| 1258 | *offset_agbno = agbno - *chunk_agbno; |
| 1259 | return 0; |
| 1260 | } |
| 1261 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | /* |
Christoph Hellwig | 94e1b69 | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 1263 | * 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] | 1264 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | int |
Christoph Hellwig | 94e1b69 | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 1266 | xfs_imap( |
| 1267 | xfs_mount_t *mp, /* file system mount structure */ |
| 1268 | xfs_trans_t *tp, /* transaction pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | xfs_ino_t ino, /* inode to locate */ |
Christoph Hellwig | 94e1b69 | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 1270 | struct xfs_imap *imap, /* location map structure */ |
| 1271 | uint flags) /* flags for inode btree lookup */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | { |
| 1273 | xfs_agblock_t agbno; /* block number of inode in the alloc group */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | xfs_agino_t agino; /* inode number within alloc group */ |
| 1275 | xfs_agnumber_t agno; /* allocation group number */ |
| 1276 | int blks_per_cluster; /* num blocks per inode cluster */ |
| 1277 | xfs_agblock_t chunk_agbno; /* first block in inode chunk */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1278 | xfs_agblock_t cluster_agbno; /* first block in inode cluster */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1279 | int error; /* error code */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1280 | int offset; /* index of inode in its buffer */ |
| 1281 | int offset_agbno; /* blks from chunk start to inode */ |
| 1282 | |
| 1283 | ASSERT(ino != NULLFSINO); |
Christoph Hellwig | 94e1b69 | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 1284 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | /* |
| 1286 | * Split up the inode number into its parts. |
| 1287 | */ |
| 1288 | agno = XFS_INO_TO_AGNO(mp, ino); |
| 1289 | agino = XFS_INO_TO_AGINO(mp, ino); |
| 1290 | agbno = XFS_AGINO_TO_AGBNO(mp, agino); |
| 1291 | if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks || |
| 1292 | ino != XFS_AGINO_TO_INO(mp, agno, agino)) { |
| 1293 | #ifdef DEBUG |
Dave Chinner | 1920779 | 2010-06-24 11:15:47 +1000 | [diff] [blame] | 1294 | /* |
| 1295 | * Don't output diagnostic information for untrusted inodes |
| 1296 | * as they can be invalid without implying corruption. |
| 1297 | */ |
| 1298 | if (flags & XFS_IGET_UNTRUSTED) |
Nathan Scott | 4d1a2ed | 2006-06-09 17:12:28 +1000 | [diff] [blame] | 1299 | return XFS_ERROR(EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | if (agno >= mp->m_sb.sb_agcount) { |
| 1301 | xfs_fs_cmn_err(CE_ALERT, mp, |
Christoph Hellwig | 94e1b69 | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 1302 | "xfs_imap: agno (%d) >= " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | "mp->m_sb.sb_agcount (%d)", |
| 1304 | agno, mp->m_sb.sb_agcount); |
| 1305 | } |
| 1306 | if (agbno >= mp->m_sb.sb_agblocks) { |
| 1307 | xfs_fs_cmn_err(CE_ALERT, mp, |
Christoph Hellwig | 94e1b69 | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 1308 | "xfs_imap: agbno (0x%llx) >= " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | "mp->m_sb.sb_agblocks (0x%lx)", |
| 1310 | (unsigned long long) agbno, |
| 1311 | (unsigned long) mp->m_sb.sb_agblocks); |
| 1312 | } |
| 1313 | if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) { |
| 1314 | xfs_fs_cmn_err(CE_ALERT, mp, |
Christoph Hellwig | 94e1b69 | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 1315 | "xfs_imap: ino (0x%llx) != " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | "XFS_AGINO_TO_INO(mp, agno, agino) " |
| 1317 | "(0x%llx)", |
| 1318 | ino, XFS_AGINO_TO_INO(mp, agno, agino)); |
| 1319 | } |
Nathan Scott | 745b1f47 | 2006-09-28 11:02:23 +1000 | [diff] [blame] | 1320 | xfs_stack_trace(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | #endif /* DEBUG */ |
| 1322 | return XFS_ERROR(EINVAL); |
| 1323 | } |
Christoph Hellwig | 94e1b69 | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 1324 | |
Dave Chinner | 7124fe0 | 2010-06-24 11:15:33 +1000 | [diff] [blame] | 1325 | blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_blocklog; |
| 1326 | |
| 1327 | /* |
| 1328 | * For bulkstat and handle lookups, we have an untrusted inode number |
| 1329 | * that we have to verify is valid. We cannot do this just by reading |
| 1330 | * the inode buffer as it may have been unlinked and removed leaving |
| 1331 | * inodes in stale state on disk. Hence we have to do a btree lookup |
| 1332 | * in all cases where an untrusted inode number is passed. |
| 1333 | */ |
Dave Chinner | 1920779 | 2010-06-24 11:15:47 +1000 | [diff] [blame] | 1334 | if (flags & XFS_IGET_UNTRUSTED) { |
Dave Chinner | 7124fe0 | 2010-06-24 11:15:33 +1000 | [diff] [blame] | 1335 | error = xfs_imap_lookup(mp, tp, agno, agino, agbno, |
| 1336 | &chunk_agbno, &offset_agbno, flags); |
| 1337 | if (error) |
| 1338 | return error; |
| 1339 | goto out_map; |
| 1340 | } |
| 1341 | |
Christoph Hellwig | 94e1b69 | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 1342 | /* |
| 1343 | * If the inode cluster size is the same as the blocksize or |
| 1344 | * smaller we get to the buffer by simple arithmetics. |
| 1345 | */ |
| 1346 | if (XFS_INODE_CLUSTER_SIZE(mp) <= mp->m_sb.sb_blocksize) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | offset = XFS_INO_TO_OFFSET(mp, ino); |
| 1348 | ASSERT(offset < mp->m_sb.sb_inopblock); |
Christoph Hellwig | 94e1b69 | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 1349 | |
| 1350 | imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, agbno); |
| 1351 | imap->im_len = XFS_FSB_TO_BB(mp, 1); |
| 1352 | imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1353 | return 0; |
| 1354 | } |
Christoph Hellwig | 94e1b69 | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 1355 | |
Christoph Hellwig | 94e1b69 | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 1356 | /* |
Christoph Hellwig | 94e1b69 | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 1357 | * If the inode chunks are aligned then use simple maths to |
| 1358 | * find the location. Otherwise we have to do a btree |
| 1359 | * lookup to find the location. |
| 1360 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | if (mp->m_inoalign_mask) { |
| 1362 | offset_agbno = agbno & mp->m_inoalign_mask; |
| 1363 | chunk_agbno = agbno - offset_agbno; |
| 1364 | } else { |
Dave Chinner | 7124fe0 | 2010-06-24 11:15:33 +1000 | [diff] [blame] | 1365 | error = xfs_imap_lookup(mp, tp, agno, agino, agbno, |
| 1366 | &chunk_agbno, &offset_agbno, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | if (error) |
| 1368 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | } |
Christoph Hellwig | 94e1b69 | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 1370 | |
Dave Chinner | 7124fe0 | 2010-06-24 11:15:33 +1000 | [diff] [blame] | 1371 | out_map: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | ASSERT(agbno >= chunk_agbno); |
| 1373 | cluster_agbno = chunk_agbno + |
| 1374 | ((offset_agbno / blks_per_cluster) * blks_per_cluster); |
| 1375 | offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) + |
| 1376 | XFS_INO_TO_OFFSET(mp, ino); |
Christoph Hellwig | 94e1b69 | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 1377 | |
| 1378 | imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, cluster_agbno); |
| 1379 | imap->im_len = XFS_FSB_TO_BB(mp, blks_per_cluster); |
| 1380 | imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog); |
| 1381 | |
| 1382 | /* |
| 1383 | * If the inode number maps to a block outside the bounds |
| 1384 | * of the file system then return NULL rather than calling |
| 1385 | * read_buf and panicing when we get an error from the |
| 1386 | * driver. |
| 1387 | */ |
| 1388 | if ((imap->im_blkno + imap->im_len) > |
| 1389 | XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) { |
| 1390 | xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: " |
| 1391 | "(imap->im_blkno (0x%llx) + imap->im_len (0x%llx)) > " |
| 1392 | " XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks) (0x%llx)", |
| 1393 | (unsigned long long) imap->im_blkno, |
| 1394 | (unsigned long long) imap->im_len, |
| 1395 | XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)); |
| 1396 | return XFS_ERROR(EINVAL); |
| 1397 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1399 | } |
| 1400 | |
| 1401 | /* |
| 1402 | * Compute and fill in value of m_in_maxlevels. |
| 1403 | */ |
| 1404 | void |
| 1405 | xfs_ialloc_compute_maxlevels( |
| 1406 | xfs_mount_t *mp) /* file system mount structure */ |
| 1407 | { |
| 1408 | int level; |
| 1409 | uint maxblocks; |
| 1410 | uint maxleafents; |
| 1411 | int minleafrecs; |
| 1412 | int minnoderecs; |
| 1413 | |
| 1414 | maxleafents = (1LL << XFS_INO_AGINO_BITS(mp)) >> |
| 1415 | XFS_INODES_PER_CHUNK_LOG; |
| 1416 | minleafrecs = mp->m_alloc_mnr[0]; |
| 1417 | minnoderecs = mp->m_alloc_mnr[1]; |
| 1418 | maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs; |
| 1419 | for (level = 1; maxblocks > 1; level++) |
| 1420 | maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs; |
| 1421 | mp->m_in_maxlevels = level; |
| 1422 | } |
| 1423 | |
| 1424 | /* |
| 1425 | * Log specified fields for the ag hdr (inode section) |
| 1426 | */ |
| 1427 | void |
| 1428 | xfs_ialloc_log_agi( |
| 1429 | xfs_trans_t *tp, /* transaction pointer */ |
| 1430 | xfs_buf_t *bp, /* allocation group header buffer */ |
| 1431 | int fields) /* bitmask of fields to log */ |
| 1432 | { |
| 1433 | int first; /* first byte number */ |
| 1434 | int last; /* last byte number */ |
| 1435 | static const short offsets[] = { /* field starting offsets */ |
| 1436 | /* keep in sync with bit definitions */ |
| 1437 | offsetof(xfs_agi_t, agi_magicnum), |
| 1438 | offsetof(xfs_agi_t, agi_versionnum), |
| 1439 | offsetof(xfs_agi_t, agi_seqno), |
| 1440 | offsetof(xfs_agi_t, agi_length), |
| 1441 | offsetof(xfs_agi_t, agi_count), |
| 1442 | offsetof(xfs_agi_t, agi_root), |
| 1443 | offsetof(xfs_agi_t, agi_level), |
| 1444 | offsetof(xfs_agi_t, agi_freecount), |
| 1445 | offsetof(xfs_agi_t, agi_newino), |
| 1446 | offsetof(xfs_agi_t, agi_dirino), |
| 1447 | offsetof(xfs_agi_t, agi_unlinked), |
| 1448 | sizeof(xfs_agi_t) |
| 1449 | }; |
| 1450 | #ifdef DEBUG |
| 1451 | xfs_agi_t *agi; /* allocation group header */ |
| 1452 | |
| 1453 | agi = XFS_BUF_TO_AGI(bp); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1454 | ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1455 | #endif |
| 1456 | /* |
| 1457 | * Compute byte offsets for the first and last fields. |
| 1458 | */ |
| 1459 | xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS, &first, &last); |
| 1460 | /* |
| 1461 | * Log the allocation group inode header buffer. |
| 1462 | */ |
| 1463 | xfs_trans_log_buf(tp, bp, first, last); |
| 1464 | } |
| 1465 | |
Christoph Hellwig | 5e1be0f | 2008-11-28 14:23:37 +1100 | [diff] [blame] | 1466 | #ifdef DEBUG |
| 1467 | STATIC void |
| 1468 | xfs_check_agi_unlinked( |
| 1469 | struct xfs_agi *agi) |
| 1470 | { |
| 1471 | int i; |
| 1472 | |
| 1473 | for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++) |
| 1474 | ASSERT(agi->agi_unlinked[i]); |
| 1475 | } |
| 1476 | #else |
| 1477 | #define xfs_check_agi_unlinked(agi) |
| 1478 | #endif |
| 1479 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | /* |
| 1481 | * Read in the allocation group header (inode allocation section) |
| 1482 | */ |
| 1483 | int |
Christoph Hellwig | 5e1be0f | 2008-11-28 14:23:37 +1100 | [diff] [blame] | 1484 | xfs_read_agi( |
| 1485 | struct xfs_mount *mp, /* file system mount structure */ |
| 1486 | struct xfs_trans *tp, /* transaction pointer */ |
| 1487 | xfs_agnumber_t agno, /* allocation group number */ |
| 1488 | struct xfs_buf **bpp) /* allocation group hdr buf */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1489 | { |
Christoph Hellwig | 5e1be0f | 2008-11-28 14:23:37 +1100 | [diff] [blame] | 1490 | struct xfs_agi *agi; /* allocation group header */ |
| 1491 | int agi_ok; /* agi is consistent */ |
| 1492 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | |
| 1494 | ASSERT(agno != NULLAGNUMBER); |
Christoph Hellwig | 5e1be0f | 2008-11-28 14:23:37 +1100 | [diff] [blame] | 1495 | |
| 1496 | error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)), |
Christoph Hellwig | 5e1be0f | 2008-11-28 14:23:37 +1100 | [diff] [blame] | 1498 | XFS_FSS_TO_BB(mp, 1), 0, bpp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | if (error) |
| 1500 | return error; |
Christoph Hellwig | 5e1be0f | 2008-11-28 14:23:37 +1100 | [diff] [blame] | 1501 | |
| 1502 | ASSERT(*bpp && !XFS_BUF_GETERROR(*bpp)); |
| 1503 | agi = XFS_BUF_TO_AGI(*bpp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | |
| 1505 | /* |
| 1506 | * Validate the magic number of the agi block. |
| 1507 | */ |
Christoph Hellwig | 5e1be0f | 2008-11-28 14:23:37 +1100 | [diff] [blame] | 1508 | agi_ok = be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC && |
| 1509 | XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum)) && |
| 1510 | be32_to_cpu(agi->agi_seqno) == agno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | if (unlikely(XFS_TEST_ERROR(!agi_ok, mp, XFS_ERRTAG_IALLOC_READ_AGI, |
| 1512 | XFS_RANDOM_IALLOC_READ_AGI))) { |
Christoph Hellwig | 5e1be0f | 2008-11-28 14:23:37 +1100 | [diff] [blame] | 1513 | XFS_CORRUPTION_ERROR("xfs_read_agi", XFS_ERRLEVEL_LOW, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1514 | mp, agi); |
Christoph Hellwig | 5e1be0f | 2008-11-28 14:23:37 +1100 | [diff] [blame] | 1515 | xfs_trans_brelse(tp, *bpp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1516 | return XFS_ERROR(EFSCORRUPTED); |
| 1517 | } |
Christoph Hellwig | 5e1be0f | 2008-11-28 14:23:37 +1100 | [diff] [blame] | 1518 | |
| 1519 | XFS_BUF_SET_VTYPE_REF(*bpp, B_FS_AGI, XFS_AGI_REF); |
| 1520 | |
| 1521 | xfs_check_agi_unlinked(agi); |
| 1522 | return 0; |
| 1523 | } |
| 1524 | |
| 1525 | int |
| 1526 | xfs_ialloc_read_agi( |
| 1527 | struct xfs_mount *mp, /* file system mount structure */ |
| 1528 | struct xfs_trans *tp, /* transaction pointer */ |
| 1529 | xfs_agnumber_t agno, /* allocation group number */ |
| 1530 | struct xfs_buf **bpp) /* allocation group hdr buf */ |
| 1531 | { |
| 1532 | struct xfs_agi *agi; /* allocation group header */ |
| 1533 | struct xfs_perag *pag; /* per allocation group data */ |
| 1534 | int error; |
| 1535 | |
| 1536 | error = xfs_read_agi(mp, tp, agno, bpp); |
| 1537 | if (error) |
| 1538 | return error; |
| 1539 | |
| 1540 | agi = XFS_BUF_TO_AGI(*bpp); |
Dave Chinner | 44b56e0 | 2010-01-11 11:47:43 +0000 | [diff] [blame] | 1541 | pag = xfs_perag_get(mp, agno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1542 | if (!pag->pagi_init) { |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1543 | pag->pagi_freecount = be32_to_cpu(agi->agi_freecount); |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 1544 | pag->pagi_count = be32_to_cpu(agi->agi_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1545 | pag->pagi_init = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | } |
| 1547 | |
Christoph Hellwig | 5e1be0f | 2008-11-28 14:23:37 +1100 | [diff] [blame] | 1548 | /* |
| 1549 | * It's possible for these to be out of sync if |
| 1550 | * we are in the middle of a forced shutdown. |
| 1551 | */ |
| 1552 | ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) || |
| 1553 | XFS_FORCED_SHUTDOWN(mp)); |
Dave Chinner | 44b56e0 | 2010-01-11 11:47:43 +0000 | [diff] [blame] | 1554 | xfs_perag_put(pag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | return 0; |
| 1556 | } |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 1557 | |
| 1558 | /* |
| 1559 | * Read in the agi to initialise the per-ag data in the mount structure |
| 1560 | */ |
| 1561 | int |
| 1562 | xfs_ialloc_pagi_init( |
| 1563 | xfs_mount_t *mp, /* file system mount structure */ |
| 1564 | xfs_trans_t *tp, /* transaction pointer */ |
| 1565 | xfs_agnumber_t agno) /* allocation group number */ |
| 1566 | { |
| 1567 | xfs_buf_t *bp = NULL; |
| 1568 | int error; |
| 1569 | |
| 1570 | error = xfs_ialloc_read_agi(mp, tp, agno, &bp); |
| 1571 | if (error) |
| 1572 | return error; |
| 1573 | if (bp) |
| 1574 | xfs_trans_brelse(tp, bp); |
| 1575 | return 0; |
| 1576 | } |