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