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