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