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