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