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( |
Dave Chinner | 7a1df15 | 2014-12-04 09:42:21 +1100 | [diff] [blame^] | 51 | struct xfs_mount *mp) |
David Chinner | 75de2a9 | 2008-03-27 18:00:38 +1100 | [diff] [blame] | 52 | { |
Dave Chinner | 7a1df15 | 2014-12-04 09:42:21 +1100 | [diff] [blame^] | 53 | if (xfs_sb_version_hasalign(&mp->m_sb) && |
| 54 | mp->m_sb.sb_inoalignmt >= |
| 55 | XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size)) |
| 56 | return mp->m_sb.sb_inoalignmt; |
David Chinner | 75de2a9 | 2008-03-27 18:00:38 +1100 | [diff] [blame] | 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); |
Dave Chinner | 263997a | 2014-05-20 07:46:40 +1000 | [diff] [blame] | 283 | } else |
Dave Chinner | 85c0b2a | 2009-08-31 20:56:51 -0300 | [diff] [blame] | 284 | version = 2; |
Dave Chinner | 85c0b2a | 2009-08-31 20:56:51 -0300 | [diff] [blame] | 285 | |
| 286 | for (j = 0; j < nbufs; j++) { |
| 287 | /* |
| 288 | * Get the block. |
| 289 | */ |
| 290 | d = XFS_AGB_TO_DADDR(mp, agno, agbno + (j * blks_per_cluster)); |
| 291 | fbuf = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, |
Dave Chinner | 7c4cebe | 2012-11-23 14:24:23 +1100 | [diff] [blame] | 292 | mp->m_bsize * blks_per_cluster, |
| 293 | XBF_UNMAPPED); |
Chandra Seetharaman | 2a30f36d | 2011-09-20 13:56:55 +0000 | [diff] [blame] | 294 | if (!fbuf) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 295 | return -ENOMEM; |
Dave Chinner | ddf6ad0 | 2013-06-27 16:04:56 +1000 | [diff] [blame] | 296 | |
| 297 | /* Initialize the inode buffers and log them appropriately. */ |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 298 | fbuf->b_ops = &xfs_inode_buf_ops; |
Christoph Hellwig | 93848a9 | 2013-04-03 16:11:17 +1100 | [diff] [blame] | 299 | xfs_buf_zero(fbuf, 0, BBTOB(fbuf->b_length)); |
Jie Liu | 6e0c7b8 | 2013-12-13 15:51:49 +1100 | [diff] [blame] | 300 | for (i = 0; i < inodes_per_cluster; i++) { |
Dave Chinner | 85c0b2a | 2009-08-31 20:56:51 -0300 | [diff] [blame] | 301 | int ioffset = i << mp->m_sb.sb_inodelog; |
Christoph Hellwig | 93848a9 | 2013-04-03 16:11:17 +1100 | [diff] [blame] | 302 | uint isize = xfs_dinode_size(version); |
Dave Chinner | 85c0b2a | 2009-08-31 20:56:51 -0300 | [diff] [blame] | 303 | |
| 304 | free = xfs_make_iptr(mp, fbuf, i); |
| 305 | free->di_magic = cpu_to_be16(XFS_DINODE_MAGIC); |
| 306 | free->di_version = version; |
| 307 | free->di_gen = cpu_to_be32(gen); |
| 308 | free->di_next_unlinked = cpu_to_be32(NULLAGINO); |
Christoph Hellwig | 93848a9 | 2013-04-03 16:11:17 +1100 | [diff] [blame] | 309 | |
| 310 | if (version == 3) { |
| 311 | free->di_ino = cpu_to_be64(ino); |
| 312 | ino++; |
| 313 | uuid_copy(&free->di_uuid, &mp->m_sb.sb_uuid); |
| 314 | xfs_dinode_calc_crc(mp, free); |
Dave Chinner | 28c8e41 | 2013-06-27 16:04:55 +1000 | [diff] [blame] | 315 | } else if (tp) { |
Christoph Hellwig | 93848a9 | 2013-04-03 16:11:17 +1100 | [diff] [blame] | 316 | /* just log the inode core */ |
| 317 | xfs_trans_log_buf(tp, fbuf, ioffset, |
| 318 | ioffset + isize - 1); |
| 319 | } |
| 320 | } |
Dave Chinner | 28c8e41 | 2013-06-27 16:04:55 +1000 | [diff] [blame] | 321 | |
| 322 | if (tp) { |
| 323 | /* |
| 324 | * Mark the buffer as an inode allocation buffer so it |
| 325 | * sticks in AIL at the point of this allocation |
| 326 | * transaction. This ensures the they are on disk before |
| 327 | * the tail of the log can be moved past this |
| 328 | * transaction (i.e. by preventing relogging from moving |
| 329 | * it forward in the log). |
| 330 | */ |
| 331 | xfs_trans_inode_alloc_buf(tp, fbuf); |
| 332 | if (version == 3) { |
Dave Chinner | ddf6ad0 | 2013-06-27 16:04:56 +1000 | [diff] [blame] | 333 | /* |
| 334 | * Mark the buffer as ordered so that they are |
| 335 | * not physically logged in the transaction but |
| 336 | * still tracked in the AIL as part of the |
| 337 | * transaction and pin the log appropriately. |
| 338 | */ |
| 339 | xfs_trans_ordered_buf(tp, fbuf); |
Dave Chinner | 28c8e41 | 2013-06-27 16:04:55 +1000 | [diff] [blame] | 340 | xfs_trans_log_buf(tp, fbuf, 0, |
| 341 | BBTOB(fbuf->b_length) - 1); |
| 342 | } |
| 343 | } else { |
| 344 | fbuf->b_flags |= XBF_DONE; |
| 345 | xfs_buf_delwri_queue(fbuf, buffer_list); |
| 346 | xfs_buf_relse(fbuf); |
Dave Chinner | 85c0b2a | 2009-08-31 20:56:51 -0300 | [diff] [blame] | 347 | } |
Dave Chinner | 85c0b2a | 2009-08-31 20:56:51 -0300 | [diff] [blame] | 348 | } |
Chandra Seetharaman | 2a30f36d | 2011-09-20 13:56:55 +0000 | [diff] [blame] | 349 | return 0; |
Dave Chinner | 85c0b2a | 2009-08-31 20:56:51 -0300 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | * Allocate new inodes in the allocation group specified by agbp. |
| 354 | * Return 0 for success, else error code. |
| 355 | */ |
| 356 | STATIC int /* error code or 0 */ |
| 357 | xfs_ialloc_ag_alloc( |
| 358 | xfs_trans_t *tp, /* transaction pointer */ |
| 359 | xfs_buf_t *agbp, /* alloc group buffer */ |
| 360 | int *alloc) |
| 361 | { |
| 362 | xfs_agi_t *agi; /* allocation group header */ |
| 363 | xfs_alloc_arg_t args; /* allocation argument structure */ |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 364 | xfs_agnumber_t agno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | xfs_agino_t newino; /* new first inode's number */ |
| 367 | xfs_agino_t newlen; /* new number of inodes */ |
Glen Overby | 3ccb8b5 | 2006-03-29 09:52:28 +1000 | [diff] [blame] | 368 | int isaligned = 0; /* inode allocation at stripe unit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | /* boundary */ |
Dave Chinner | 44b56e0 | 2010-01-11 11:47:43 +0000 | [diff] [blame] | 370 | struct xfs_perag *pag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | |
Mark Tinguely | a004168 | 2012-09-20 13:16:45 -0500 | [diff] [blame] | 372 | memset(&args, 0, sizeof(args)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | args.tp = tp; |
| 374 | args.mp = tp->t_mountp; |
| 375 | |
| 376 | /* |
| 377 | * Locking will ensure that we don't have two callers in here |
| 378 | * at one time. |
| 379 | */ |
Jie Liu | 7178343 | 2013-12-13 15:51:46 +1100 | [diff] [blame] | 380 | newlen = args.mp->m_ialloc_inos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | if (args.mp->m_maxicount && |
| 382 | args.mp->m_sb.sb_icount + newlen > args.mp->m_maxicount) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 383 | return -ENOSPC; |
Jie Liu | 126cd10 | 2013-12-13 15:51:48 +1100 | [diff] [blame] | 384 | args.minlen = args.maxlen = args.mp->m_ialloc_blks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | /* |
Glen Overby | 3ccb8b5 | 2006-03-29 09:52:28 +1000 | [diff] [blame] | 386 | * First try to allocate inodes contiguous with the last-allocated |
| 387 | * chunk of inodes. If the filesystem is striped, this will fill |
| 388 | * an entire stripe unit with inodes. |
Dave Chinner | 28c8e41 | 2013-06-27 16:04:55 +1000 | [diff] [blame] | 389 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | agi = XFS_BUF_TO_AGI(agbp); |
Glen Overby | 3ccb8b5 | 2006-03-29 09:52:28 +1000 | [diff] [blame] | 391 | newino = be32_to_cpu(agi->agi_newino); |
Dave Chinner | 85c0b2a | 2009-08-31 20:56:51 -0300 | [diff] [blame] | 392 | agno = be32_to_cpu(agi->agi_seqno); |
Nathan Scott | 019ff2d | 2006-04-11 15:45:05 +1000 | [diff] [blame] | 393 | args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) + |
Jie Liu | 126cd10 | 2013-12-13 15:51:48 +1100 | [diff] [blame] | 394 | args.mp->m_ialloc_blks; |
Nathan Scott | 019ff2d | 2006-04-11 15:45:05 +1000 | [diff] [blame] | 395 | if (likely(newino != NULLAGINO && |
| 396 | (args.agbno < be32_to_cpu(agi->agi_length)))) { |
Dave Chinner | 85c0b2a | 2009-08-31 20:56:51 -0300 | [diff] [blame] | 397 | args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno); |
Glen Overby | 3ccb8b5 | 2006-03-29 09:52:28 +1000 | [diff] [blame] | 398 | args.type = XFS_ALLOCTYPE_THIS_BNO; |
Glen Overby | 3ccb8b5 | 2006-03-29 09:52:28 +1000 | [diff] [blame] | 399 | args.prod = 1; |
David Chinner | 75de2a9 | 2008-03-27 18:00:38 +1100 | [diff] [blame] | 400 | |
Glen Overby | 3ccb8b5 | 2006-03-29 09:52:28 +1000 | [diff] [blame] | 401 | /* |
David Chinner | 75de2a9 | 2008-03-27 18:00:38 +1100 | [diff] [blame] | 402 | * We need to take into account alignment here to ensure that |
| 403 | * we don't modify the free list if we fail to have an exact |
| 404 | * block. If we don't have an exact match, and every oher |
| 405 | * attempt allocation attempt fails, we'll end up cancelling |
| 406 | * a dirty transaction and shutting down. |
| 407 | * |
| 408 | * For an exact allocation, alignment must be 1, |
| 409 | * however we need to take cluster alignment into account when |
| 410 | * fixing up the freelist. Use the minalignslop field to |
| 411 | * indicate that extra blocks might be required for alignment, |
| 412 | * but not to use them in the actual exact allocation. |
Glen Overby | 3ccb8b5 | 2006-03-29 09:52:28 +1000 | [diff] [blame] | 413 | */ |
David Chinner | 75de2a9 | 2008-03-27 18:00:38 +1100 | [diff] [blame] | 414 | args.alignment = 1; |
Dave Chinner | 7a1df15 | 2014-12-04 09:42:21 +1100 | [diff] [blame^] | 415 | args.minalignslop = xfs_ialloc_cluster_alignment(args.mp) - 1; |
David Chinner | 75de2a9 | 2008-03-27 18:00:38 +1100 | [diff] [blame] | 416 | |
| 417 | /* Allow space for the inode btree to split. */ |
Christoph Hellwig | 0d87e65 | 2009-02-09 08:37:14 +0100 | [diff] [blame] | 418 | args.minleft = args.mp->m_in_maxlevels - 1; |
Glen Overby | 3ccb8b5 | 2006-03-29 09:52:28 +1000 | [diff] [blame] | 419 | if ((error = xfs_alloc_vextent(&args))) |
| 420 | return error; |
Brian Foster | e480a72 | 2014-03-07 16:19:14 +1100 | [diff] [blame] | 421 | |
| 422 | /* |
| 423 | * This request might have dirtied the transaction if the AG can |
| 424 | * satisfy the request, but the exact block was not available. |
| 425 | * If the allocation did fail, subsequent requests will relax |
| 426 | * the exact agbno requirement and increase the alignment |
| 427 | * instead. It is critical that the total size of the request |
| 428 | * (len + alignment + slop) does not increase from this point |
| 429 | * on, so reset minalignslop to ensure it is not included in |
| 430 | * subsequent requests. |
| 431 | */ |
| 432 | args.minalignslop = 0; |
Glen Overby | 3ccb8b5 | 2006-03-29 09:52:28 +1000 | [diff] [blame] | 433 | } else |
| 434 | args.fsbno = NULLFSBLOCK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | |
Glen Overby | 3ccb8b5 | 2006-03-29 09:52:28 +1000 | [diff] [blame] | 436 | if (unlikely(args.fsbno == NULLFSBLOCK)) { |
| 437 | /* |
| 438 | * Set the alignment for the allocation. |
| 439 | * If stripe alignment is turned on then align at stripe unit |
| 440 | * boundary. |
Nathan Scott | 019ff2d | 2006-04-11 15:45:05 +1000 | [diff] [blame] | 441 | * If the cluster size is smaller than a filesystem block |
| 442 | * then we're doing I/O for inodes in filesystem block size |
Glen Overby | 3ccb8b5 | 2006-03-29 09:52:28 +1000 | [diff] [blame] | 443 | * pieces, so don't need alignment anyway. |
| 444 | */ |
| 445 | isaligned = 0; |
| 446 | if (args.mp->m_sinoalign) { |
| 447 | ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN)); |
| 448 | args.alignment = args.mp->m_dalign; |
| 449 | isaligned = 1; |
David Chinner | 75de2a9 | 2008-03-27 18:00:38 +1100 | [diff] [blame] | 450 | } else |
Dave Chinner | 7a1df15 | 2014-12-04 09:42:21 +1100 | [diff] [blame^] | 451 | args.alignment = xfs_ialloc_cluster_alignment(args.mp); |
Glen Overby | 3ccb8b5 | 2006-03-29 09:52:28 +1000 | [diff] [blame] | 452 | /* |
| 453 | * Need to figure out where to allocate the inode blocks. |
| 454 | * Ideally they should be spaced out through the a.g. |
| 455 | * For now, just allocate blocks up front. |
| 456 | */ |
| 457 | args.agbno = be32_to_cpu(agi->agi_root); |
Dave Chinner | 85c0b2a | 2009-08-31 20:56:51 -0300 | [diff] [blame] | 458 | args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno); |
Glen Overby | 3ccb8b5 | 2006-03-29 09:52:28 +1000 | [diff] [blame] | 459 | /* |
| 460 | * Allocate a fixed-size extent of inodes. |
| 461 | */ |
| 462 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
Glen Overby | 3ccb8b5 | 2006-03-29 09:52:28 +1000 | [diff] [blame] | 463 | args.prod = 1; |
| 464 | /* |
| 465 | * Allow space for the inode btree to split. |
| 466 | */ |
Christoph Hellwig | 0d87e65 | 2009-02-09 08:37:14 +0100 | [diff] [blame] | 467 | args.minleft = args.mp->m_in_maxlevels - 1; |
Glen Overby | 3ccb8b5 | 2006-03-29 09:52:28 +1000 | [diff] [blame] | 468 | if ((error = xfs_alloc_vextent(&args))) |
| 469 | return error; |
| 470 | } |
Nathan Scott | 019ff2d | 2006-04-11 15:45:05 +1000 | [diff] [blame] | 471 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | /* |
| 473 | * If stripe alignment is turned on, then try again with cluster |
| 474 | * alignment. |
| 475 | */ |
| 476 | if (isaligned && args.fsbno == NULLFSBLOCK) { |
| 477 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 478 | args.agbno = be32_to_cpu(agi->agi_root); |
Dave Chinner | 85c0b2a | 2009-08-31 20:56:51 -0300 | [diff] [blame] | 479 | args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno); |
Dave Chinner | 7a1df15 | 2014-12-04 09:42:21 +1100 | [diff] [blame^] | 480 | args.alignment = xfs_ialloc_cluster_alignment(args.mp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | if ((error = xfs_alloc_vextent(&args))) |
| 482 | return error; |
| 483 | } |
| 484 | |
| 485 | if (args.fsbno == NULLFSBLOCK) { |
| 486 | *alloc = 0; |
| 487 | return 0; |
| 488 | } |
| 489 | ASSERT(args.len == args.minlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | |
David Chinner | 359346a | 2008-04-29 12:53:32 +1000 | [diff] [blame] | 491 | /* |
Dave Chinner | 85c0b2a | 2009-08-31 20:56:51 -0300 | [diff] [blame] | 492 | * Stamp and write the inode buffers. |
| 493 | * |
David Chinner | 359346a | 2008-04-29 12:53:32 +1000 | [diff] [blame] | 494 | * Seed the new inode cluster with a random generation number. This |
| 495 | * prevents short-term reuse of generation numbers if a chunk is |
| 496 | * freed and then immediately reallocated. We use random numbers |
| 497 | * rather than a linear progression to prevent the next generation |
| 498 | * number from being easily guessable. |
| 499 | */ |
Dave Chinner | 28c8e41 | 2013-06-27 16:04:55 +1000 | [diff] [blame] | 500 | error = xfs_ialloc_inode_init(args.mp, tp, NULL, agno, args.agbno, |
Akinobu Mita | ecb3403 | 2013-03-04 21:58:20 +0900 | [diff] [blame] | 501 | args.len, prandom_u32()); |
Christoph Hellwig | d42f08f | 2008-11-28 14:23:38 +1100 | [diff] [blame] | 502 | |
Chandra Seetharaman | 2a30f36d | 2011-09-20 13:56:55 +0000 | [diff] [blame] | 503 | if (error) |
| 504 | return error; |
Dave Chinner | 85c0b2a | 2009-08-31 20:56:51 -0300 | [diff] [blame] | 505 | /* |
| 506 | * Convert the results. |
| 507 | */ |
| 508 | newino = XFS_OFFBNO_TO_AGINO(args.mp, args.agbno, 0); |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 509 | be32_add_cpu(&agi->agi_count, newlen); |
| 510 | be32_add_cpu(&agi->agi_freecount, newlen); |
Dave Chinner | 44b56e0 | 2010-01-11 11:47:43 +0000 | [diff] [blame] | 511 | pag = xfs_perag_get(args.mp, agno); |
| 512 | pag->pagi_freecount += newlen; |
| 513 | xfs_perag_put(pag); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 514 | agi->agi_newino = cpu_to_be32(newino); |
Dave Chinner | 85c0b2a | 2009-08-31 20:56:51 -0300 | [diff] [blame] | 515 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | /* |
Brian Foster | 0aa0a75 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 517 | * Insert records describing the new inode chunk into the btrees. |
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 | error = xfs_inobt_insert(args.mp, tp, agbp, newino, newlen, |
| 520 | XFS_BTNUM_INO); |
| 521 | if (error) |
| 522 | return error; |
| 523 | |
| 524 | if (xfs_sb_version_hasfinobt(&args.mp->m_sb)) { |
| 525 | error = xfs_inobt_insert(args.mp, tp, agbp, newino, newlen, |
| 526 | XFS_BTNUM_FINO); |
| 527 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | /* |
| 531 | * Log allocation group header fields |
| 532 | */ |
| 533 | xfs_ialloc_log_agi(tp, agbp, |
| 534 | XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO); |
| 535 | /* |
| 536 | * Modify/log superblock values for inode count and inode free count. |
| 537 | */ |
| 538 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen); |
| 539 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen); |
| 540 | *alloc = 1; |
| 541 | return 0; |
| 542 | } |
| 543 | |
Christoph Hellwig | b8f82a4 | 2009-11-14 16:17:22 +0000 | [diff] [blame] | 544 | STATIC xfs_agnumber_t |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | xfs_ialloc_next_ag( |
| 546 | xfs_mount_t *mp) |
| 547 | { |
| 548 | xfs_agnumber_t agno; |
| 549 | |
| 550 | spin_lock(&mp->m_agirotor_lock); |
| 551 | agno = mp->m_agirotor; |
Carlos Maiolino | 8aea3ff | 2012-09-20 10:32:36 -0300 | [diff] [blame] | 552 | if (++mp->m_agirotor >= mp->m_maxagi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | mp->m_agirotor = 0; |
| 554 | spin_unlock(&mp->m_agirotor_lock); |
| 555 | |
| 556 | return agno; |
| 557 | } |
| 558 | |
| 559 | /* |
| 560 | * 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] | 561 | * inode and the mode. Return the allocation group buffer. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | */ |
Christoph Hellwig | 55d6af6 | 2012-07-04 10:54:49 -0400 | [diff] [blame] | 563 | STATIC xfs_agnumber_t |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | xfs_ialloc_ag_select( |
| 565 | xfs_trans_t *tp, /* transaction pointer */ |
| 566 | xfs_ino_t parent, /* parent directory inode number */ |
Al Viro | 576b1d6 | 2011-07-26 02:50:15 -0400 | [diff] [blame] | 567 | umode_t mode, /* bits set to indicate file type */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | int okalloc) /* ok to allocate more space */ |
| 569 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | xfs_agnumber_t agcount; /* number of ag's in the filesystem */ |
| 571 | xfs_agnumber_t agno; /* current ag number */ |
| 572 | int flags; /* alloc buffer locking flags */ |
| 573 | xfs_extlen_t ineed; /* blocks needed for inode allocation */ |
| 574 | xfs_extlen_t longest = 0; /* longest extent available */ |
| 575 | xfs_mount_t *mp; /* mount point structure */ |
| 576 | int needspace; /* file mode implies space allocated */ |
| 577 | xfs_perag_t *pag; /* per allocation group data */ |
| 578 | xfs_agnumber_t pagno; /* parent (starting) ag number */ |
Christoph Hellwig | 55d6af6 | 2012-07-04 10:54:49 -0400 | [diff] [blame] | 579 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | |
| 581 | /* |
| 582 | * Files of these types need at least one block if length > 0 |
| 583 | * (and they won't fit in the inode, but that's hard to figure out). |
| 584 | */ |
| 585 | needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode); |
| 586 | mp = tp->t_mountp; |
| 587 | agcount = mp->m_maxagi; |
| 588 | if (S_ISDIR(mode)) |
| 589 | pagno = xfs_ialloc_next_ag(mp); |
| 590 | else { |
| 591 | pagno = XFS_INO_TO_AGNO(mp, parent); |
| 592 | if (pagno >= agcount) |
| 593 | pagno = 0; |
| 594 | } |
Christoph Hellwig | 55d6af6 | 2012-07-04 10:54:49 -0400 | [diff] [blame] | 595 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | ASSERT(pagno < agcount); |
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 | /* |
| 599 | * Loop through allocation groups, looking for one with a little |
| 600 | * free space in it. Note we don't look for free inodes, exactly. |
| 601 | * Instead, we include whether there is a need to allocate inodes |
| 602 | * to mean that blocks must be allocated for them, |
| 603 | * if none are currently free. |
| 604 | */ |
| 605 | agno = pagno; |
| 606 | flags = XFS_ALLOC_FLAG_TRYLOCK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | for (;;) { |
Dave Chinner | 44b56e0 | 2010-01-11 11:47:43 +0000 | [diff] [blame] | 608 | pag = xfs_perag_get(mp, agno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | if (!pag->pagi_inodeok) { |
| 610 | xfs_ialloc_next_ag(mp); |
Christoph Hellwig | 55d6af6 | 2012-07-04 10:54:49 -0400 | [diff] [blame] | 611 | goto nextag; |
| 612 | } |
| 613 | |
| 614 | if (!pag->pagi_init) { |
| 615 | error = xfs_ialloc_pagi_init(mp, tp, agno); |
| 616 | if (error) |
| 617 | goto nextag; |
| 618 | } |
| 619 | |
| 620 | if (pag->pagi_freecount) { |
| 621 | xfs_perag_put(pag); |
| 622 | return agno; |
| 623 | } |
| 624 | |
| 625 | if (!okalloc) |
| 626 | goto nextag; |
| 627 | |
| 628 | if (!pag->pagf_init) { |
| 629 | error = xfs_alloc_pagf_init(mp, tp, agno, flags); |
| 630 | if (error) |
| 631 | goto nextag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | /* |
Dave Chinner | 7a1df15 | 2014-12-04 09:42:21 +1100 | [diff] [blame^] | 635 | * Check that there is enough free space for the file plus a |
| 636 | * chunk of inodes if we need to allocate some. If this is the |
| 637 | * first pass across the AGs, take into account the potential |
| 638 | * space needed for alignment of inode chunks when checking the |
| 639 | * longest contiguous free space in the AG - this prevents us |
| 640 | * from getting ENOSPC because we have free space larger than |
| 641 | * m_ialloc_blks but alignment constraints prevent us from using |
| 642 | * it. |
| 643 | * |
| 644 | * If we can't find an AG with space for full alignment slack to |
| 645 | * be taken into account, we must be near ENOSPC in all AGs. |
| 646 | * Hence we don't include alignment for the second pass and so |
| 647 | * if we fail allocation due to alignment issues then it is most |
| 648 | * likely a real ENOSPC condition. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | */ |
Jie Liu | 126cd10 | 2013-12-13 15:51:48 +1100 | [diff] [blame] | 650 | ineed = mp->m_ialloc_blks; |
Dave Chinner | 7a1df15 | 2014-12-04 09:42:21 +1100 | [diff] [blame^] | 651 | if (flags && ineed > 1) |
| 652 | ineed += xfs_ialloc_cluster_alignment(mp); |
Christoph Hellwig | 55d6af6 | 2012-07-04 10:54:49 -0400 | [diff] [blame] | 653 | longest = pag->pagf_longest; |
| 654 | if (!longest) |
| 655 | longest = pag->pagf_flcount > 0; |
| 656 | |
| 657 | if (pag->pagf_freeblks >= needspace + ineed && |
| 658 | longest >= ineed) { |
| 659 | xfs_perag_put(pag); |
| 660 | return agno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | nextag: |
Dave Chinner | 44b56e0 | 2010-01-11 11:47:43 +0000 | [diff] [blame] | 663 | xfs_perag_put(pag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | /* |
| 665 | * No point in iterating over the rest, if we're shutting |
| 666 | * down. |
| 667 | */ |
Dave Chinner | 1c1c6eb | 2010-01-11 11:47:44 +0000 | [diff] [blame] | 668 | if (XFS_FORCED_SHUTDOWN(mp)) |
Christoph Hellwig | 55d6af6 | 2012-07-04 10:54:49 -0400 | [diff] [blame] | 669 | return NULLAGNUMBER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | agno++; |
| 671 | if (agno >= agcount) |
| 672 | agno = 0; |
| 673 | if (agno == pagno) { |
Dave Chinner | 1c1c6eb | 2010-01-11 11:47:44 +0000 | [diff] [blame] | 674 | if (flags == 0) |
Christoph Hellwig | 55d6af6 | 2012-07-04 10:54:49 -0400 | [diff] [blame] | 675 | return NULLAGNUMBER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | flags = 0; |
| 677 | } |
| 678 | } |
| 679 | } |
| 680 | |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 681 | /* |
| 682 | * Try to retrieve the next record to the left/right from the current one. |
| 683 | */ |
| 684 | STATIC int |
| 685 | xfs_ialloc_next_rec( |
| 686 | struct xfs_btree_cur *cur, |
| 687 | xfs_inobt_rec_incore_t *rec, |
| 688 | int *done, |
| 689 | int left) |
| 690 | { |
| 691 | int error; |
| 692 | int i; |
| 693 | |
| 694 | if (left) |
| 695 | error = xfs_btree_decrement(cur, 0, &i); |
| 696 | else |
| 697 | error = xfs_btree_increment(cur, 0, &i); |
| 698 | |
| 699 | if (error) |
| 700 | return error; |
| 701 | *done = !i; |
| 702 | if (i) { |
| 703 | error = xfs_inobt_get_rec(cur, rec, &i); |
| 704 | if (error) |
| 705 | return error; |
| 706 | XFS_WANT_CORRUPTED_RETURN(i == 1); |
| 707 | } |
| 708 | |
| 709 | return 0; |
| 710 | } |
| 711 | |
Dave Chinner | bd16956 | 2009-08-31 20:58:28 -0300 | [diff] [blame] | 712 | STATIC int |
| 713 | xfs_ialloc_get_rec( |
| 714 | struct xfs_btree_cur *cur, |
| 715 | xfs_agino_t agino, |
| 716 | xfs_inobt_rec_incore_t *rec, |
Jie Liu | 43df2ee | 2013-06-24 13:37:23 +0800 | [diff] [blame] | 717 | int *done) |
Dave Chinner | bd16956 | 2009-08-31 20:58:28 -0300 | [diff] [blame] | 718 | { |
| 719 | int error; |
| 720 | int i; |
| 721 | |
| 722 | error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_EQ, &i); |
| 723 | if (error) |
| 724 | return error; |
| 725 | *done = !i; |
| 726 | if (i) { |
| 727 | error = xfs_inobt_get_rec(cur, rec, &i); |
| 728 | if (error) |
| 729 | return error; |
| 730 | XFS_WANT_CORRUPTED_RETURN(i == 1); |
| 731 | } |
| 732 | |
| 733 | return 0; |
| 734 | } |
Dave Chinner | 0b48db8 | 2009-08-31 20:57:09 -0300 | [diff] [blame] | 735 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | /* |
Brian Foster | 6dd8638 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 737 | * Allocate an inode using the inobt-only algorithm. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | */ |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 739 | STATIC int |
Brian Foster | 6dd8638 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 740 | xfs_dialloc_ag_inobt( |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 741 | struct xfs_trans *tp, |
| 742 | struct xfs_buf *agbp, |
| 743 | xfs_ino_t parent, |
| 744 | xfs_ino_t *inop) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | { |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 746 | struct xfs_mount *mp = tp->t_mountp; |
| 747 | struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp); |
| 748 | xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno); |
| 749 | xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent); |
| 750 | xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent); |
| 751 | struct xfs_perag *pag; |
| 752 | struct xfs_btree_cur *cur, *tcur; |
| 753 | struct xfs_inobt_rec_incore rec, trec; |
| 754 | xfs_ino_t ino; |
| 755 | int error; |
| 756 | int offset; |
| 757 | int i, j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | |
Dave Chinner | 44b56e0 | 2010-01-11 11:47:43 +0000 | [diff] [blame] | 759 | pag = xfs_perag_get(mp, agno); |
Dave Chinner | bd16956 | 2009-08-31 20:58:28 -0300 | [diff] [blame] | 760 | |
Christoph Hellwig | 4bb6106 | 2012-07-04 10:54:48 -0400 | [diff] [blame] | 761 | ASSERT(pag->pagi_init); |
| 762 | ASSERT(pag->pagi_inodeok); |
| 763 | ASSERT(pag->pagi_freecount > 0); |
| 764 | |
Dave Chinner | bd16956 | 2009-08-31 20:58:28 -0300 | [diff] [blame] | 765 | restart_pagno: |
Brian Foster | 57bd3db | 2014-04-24 16:00:50 +1000 | [diff] [blame] | 766 | cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | /* |
| 768 | * If pagino is 0 (this is the root inode allocation) use newino. |
| 769 | * This must work because we've just allocated some. |
| 770 | */ |
| 771 | if (!pagino) |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 772 | pagino = be32_to_cpu(agi->agi_newino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | |
Dave Chinner | 0b48db8 | 2009-08-31 20:57:09 -0300 | [diff] [blame] | 774 | error = xfs_check_agi_freecount(cur, agi); |
| 775 | if (error) |
| 776 | goto error0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | /* |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 779 | * 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] | 780 | */ |
| 781 | if (pagno == agno) { |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 782 | int doneleft; /* done, to the left */ |
| 783 | int doneright; /* done, to the right */ |
Dave Chinner | bd16956 | 2009-08-31 20:58:28 -0300 | [diff] [blame] | 784 | int searchdistance = 10; |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 785 | |
Christoph Hellwig | 2187550 | 2009-08-31 20:58:21 -0300 | [diff] [blame] | 786 | error = xfs_inobt_lookup(cur, pagino, XFS_LOOKUP_LE, &i); |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 787 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | goto error0; |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 789 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); |
| 790 | |
| 791 | error = xfs_inobt_get_rec(cur, &rec, &j); |
| 792 | if (error) |
| 793 | goto error0; |
Brian Foster | b121099 | 2013-08-27 17:15:45 -0400 | [diff] [blame] | 794 | XFS_WANT_CORRUPTED_GOTO(j == 1, error0); |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 795 | |
| 796 | if (rec.ir_freecount > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | /* |
| 798 | * Found a free inode in the same chunk |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 799 | * as the parent, done. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | */ |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 801 | goto alloc_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | } |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 803 | |
| 804 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | /* |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 806 | * In the same AG as parent, but parent's chunk is full. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 809 | /* duplicate the cursor, search left & right simultaneously */ |
| 810 | error = xfs_btree_dup_cursor(cur, &tcur); |
| 811 | if (error) |
| 812 | goto error0; |
| 813 | |
Dave Chinner | bd16956 | 2009-08-31 20:58:28 -0300 | [diff] [blame] | 814 | /* |
| 815 | * Skip to last blocks looked up if same parent inode. |
| 816 | */ |
| 817 | if (pagino != NULLAGINO && |
| 818 | pag->pagl_pagino == pagino && |
| 819 | pag->pagl_leftrec != NULLAGINO && |
| 820 | pag->pagl_rightrec != NULLAGINO) { |
| 821 | error = xfs_ialloc_get_rec(tcur, pag->pagl_leftrec, |
Jie Liu | 43df2ee | 2013-06-24 13:37:23 +0800 | [diff] [blame] | 822 | &trec, &doneleft); |
Dave Chinner | bd16956 | 2009-08-31 20:58:28 -0300 | [diff] [blame] | 823 | if (error) |
| 824 | goto error1; |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 825 | |
Dave Chinner | bd16956 | 2009-08-31 20:58:28 -0300 | [diff] [blame] | 826 | error = xfs_ialloc_get_rec(cur, pag->pagl_rightrec, |
Jie Liu | 43df2ee | 2013-06-24 13:37:23 +0800 | [diff] [blame] | 827 | &rec, &doneright); |
Dave Chinner | bd16956 | 2009-08-31 20:58:28 -0300 | [diff] [blame] | 828 | if (error) |
| 829 | goto error1; |
| 830 | } else { |
| 831 | /* search left with tcur, back up 1 record */ |
| 832 | error = xfs_ialloc_next_rec(tcur, &trec, &doneleft, 1); |
| 833 | if (error) |
| 834 | goto error1; |
| 835 | |
| 836 | /* search right with cur, go forward 1 record. */ |
| 837 | error = xfs_ialloc_next_rec(cur, &rec, &doneright, 0); |
| 838 | if (error) |
| 839 | goto error1; |
| 840 | } |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 841 | |
| 842 | /* |
| 843 | * Loop until we find an inode chunk with a free inode. |
| 844 | */ |
| 845 | while (!doneleft || !doneright) { |
| 846 | int useleft; /* using left inode chunk this time */ |
| 847 | |
Dave Chinner | bd16956 | 2009-08-31 20:58:28 -0300 | [diff] [blame] | 848 | if (!--searchdistance) { |
| 849 | /* |
| 850 | * Not in range - save last search |
| 851 | * location and allocate a new inode |
| 852 | */ |
Eric Sandeen | 3b82638 | 2009-10-30 09:27:07 +0100 | [diff] [blame] | 853 | xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); |
Dave Chinner | bd16956 | 2009-08-31 20:58:28 -0300 | [diff] [blame] | 854 | pag->pagl_leftrec = trec.ir_startino; |
| 855 | pag->pagl_rightrec = rec.ir_startino; |
| 856 | pag->pagl_pagino = pagino; |
| 857 | goto newino; |
| 858 | } |
| 859 | |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 860 | /* figure out the closer block if both are valid. */ |
| 861 | if (!doneleft && !doneright) { |
| 862 | useleft = pagino - |
| 863 | (trec.ir_startino + XFS_INODES_PER_CHUNK - 1) < |
| 864 | rec.ir_startino - pagino; |
| 865 | } else { |
| 866 | useleft = !doneleft; |
| 867 | } |
| 868 | |
| 869 | /* free inodes to the left? */ |
| 870 | if (useleft && trec.ir_freecount) { |
| 871 | rec = trec; |
| 872 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 873 | cur = tcur; |
Dave Chinner | bd16956 | 2009-08-31 20:58:28 -0300 | [diff] [blame] | 874 | |
| 875 | pag->pagl_leftrec = trec.ir_startino; |
| 876 | pag->pagl_rightrec = rec.ir_startino; |
| 877 | pag->pagl_pagino = pagino; |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 878 | goto alloc_inode; |
| 879 | } |
| 880 | |
| 881 | /* free inodes to the right? */ |
| 882 | if (!useleft && rec.ir_freecount) { |
| 883 | xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); |
Dave Chinner | bd16956 | 2009-08-31 20:58:28 -0300 | [diff] [blame] | 884 | |
| 885 | pag->pagl_leftrec = trec.ir_startino; |
| 886 | pag->pagl_rightrec = rec.ir_startino; |
| 887 | pag->pagl_pagino = pagino; |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 888 | goto alloc_inode; |
| 889 | } |
| 890 | |
| 891 | /* get next record to check */ |
| 892 | if (useleft) { |
| 893 | error = xfs_ialloc_next_rec(tcur, &trec, |
| 894 | &doneleft, 1); |
| 895 | } else { |
| 896 | error = xfs_ialloc_next_rec(cur, &rec, |
| 897 | &doneright, 0); |
| 898 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | goto error1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | } |
Dave Chinner | bd16956 | 2009-08-31 20:58:28 -0300 | [diff] [blame] | 902 | |
| 903 | /* |
| 904 | * We've reached the end of the btree. because |
| 905 | * we are only searching a small chunk of the |
| 906 | * btree each search, there is obviously free |
| 907 | * inodes closer to the parent inode than we |
| 908 | * are now. restart the search again. |
| 909 | */ |
| 910 | pag->pagl_pagino = NULLAGINO; |
| 911 | pag->pagl_leftrec = NULLAGINO; |
| 912 | pag->pagl_rightrec = NULLAGINO; |
| 913 | xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); |
| 914 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 915 | goto restart_pagno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | } |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 917 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | /* |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 919 | * In a different AG from the parent. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | * See if the most recently allocated block has any free. |
| 921 | */ |
Dave Chinner | bd16956 | 2009-08-31 20:58:28 -0300 | [diff] [blame] | 922 | newino: |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 923 | if (agi->agi_newino != cpu_to_be32(NULLAGINO)) { |
Christoph Hellwig | 2187550 | 2009-08-31 20:58:21 -0300 | [diff] [blame] | 924 | error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino), |
| 925 | XFS_LOOKUP_EQ, &i); |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 926 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | goto error0; |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 928 | |
| 929 | if (i == 1) { |
| 930 | error = xfs_inobt_get_rec(cur, &rec, &j); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | if (error) |
| 932 | goto error0; |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 933 | |
| 934 | if (j == 1 && rec.ir_freecount > 0) { |
| 935 | /* |
| 936 | * The last chunk allocated in the group |
| 937 | * still has a free inode. |
| 938 | */ |
| 939 | goto alloc_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | } |
| 941 | } |
Dave Chinner | bd16956 | 2009-08-31 20:58:28 -0300 | [diff] [blame] | 942 | } |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 943 | |
Dave Chinner | bd16956 | 2009-08-31 20:58:28 -0300 | [diff] [blame] | 944 | /* |
| 945 | * None left in the last group, search the whole AG |
| 946 | */ |
| 947 | error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i); |
| 948 | if (error) |
| 949 | goto error0; |
| 950 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); |
| 951 | |
| 952 | for (;;) { |
| 953 | error = xfs_inobt_get_rec(cur, &rec, &i); |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 954 | if (error) |
| 955 | goto error0; |
| 956 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); |
Dave Chinner | bd16956 | 2009-08-31 20:58:28 -0300 | [diff] [blame] | 957 | if (rec.ir_freecount > 0) |
| 958 | break; |
| 959 | error = xfs_btree_increment(cur, 0, &i); |
| 960 | if (error) |
| 961 | goto error0; |
| 962 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | } |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 964 | |
| 965 | alloc_inode: |
Christoph Hellwig | 824c313 | 2012-07-04 10:54:45 -0400 | [diff] [blame] | 966 | offset = xfs_lowbit64(rec.ir_free); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | ASSERT(offset >= 0); |
| 968 | ASSERT(offset < XFS_INODES_PER_CHUNK); |
| 969 | ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) % |
| 970 | XFS_INODES_PER_CHUNK) == 0); |
| 971 | ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset); |
Christoph Hellwig | 0d87e65 | 2009-02-09 08:37:14 +0100 | [diff] [blame] | 972 | rec.ir_free &= ~XFS_INOBT_MASK(offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | rec.ir_freecount--; |
Christoph Hellwig | afabc24 | 2009-08-31 20:57:03 -0300 | [diff] [blame] | 974 | error = xfs_inobt_update(cur, &rec); |
| 975 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | goto error0; |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 977 | be32_add_cpu(&agi->agi_freecount, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT); |
Dave Chinner | 44b56e0 | 2010-01-11 11:47:43 +0000 | [diff] [blame] | 979 | pag->pagi_freecount--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | |
Dave Chinner | 0b48db8 | 2009-08-31 20:57:09 -0300 | [diff] [blame] | 981 | error = xfs_check_agi_freecount(cur, agi); |
| 982 | if (error) |
| 983 | goto error0; |
| 984 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 986 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1); |
Dave Chinner | 44b56e0 | 2010-01-11 11:47:43 +0000 | [diff] [blame] | 987 | xfs_perag_put(pag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | *inop = ino; |
| 989 | return 0; |
| 990 | error1: |
| 991 | xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR); |
| 992 | error0: |
| 993 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
Dave Chinner | 44b56e0 | 2010-01-11 11:47:43 +0000 | [diff] [blame] | 994 | xfs_perag_put(pag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | return error; |
| 996 | } |
| 997 | |
| 998 | /* |
Brian Foster | 6dd8638 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 999 | * Use the free inode btree to allocate an inode based on distance from the |
| 1000 | * parent. Note that the provided cursor may be deleted and replaced. |
| 1001 | */ |
| 1002 | STATIC int |
| 1003 | xfs_dialloc_ag_finobt_near( |
| 1004 | xfs_agino_t pagino, |
| 1005 | struct xfs_btree_cur **ocur, |
| 1006 | struct xfs_inobt_rec_incore *rec) |
| 1007 | { |
| 1008 | struct xfs_btree_cur *lcur = *ocur; /* left search cursor */ |
| 1009 | struct xfs_btree_cur *rcur; /* right search cursor */ |
| 1010 | struct xfs_inobt_rec_incore rrec; |
| 1011 | int error; |
| 1012 | int i, j; |
| 1013 | |
| 1014 | error = xfs_inobt_lookup(lcur, pagino, XFS_LOOKUP_LE, &i); |
| 1015 | if (error) |
| 1016 | return error; |
| 1017 | |
| 1018 | if (i == 1) { |
| 1019 | error = xfs_inobt_get_rec(lcur, rec, &i); |
| 1020 | if (error) |
| 1021 | return error; |
| 1022 | XFS_WANT_CORRUPTED_RETURN(i == 1); |
| 1023 | |
| 1024 | /* |
| 1025 | * See if we've landed in the parent inode record. The finobt |
| 1026 | * only tracks chunks with at least one free inode, so record |
| 1027 | * existence is enough. |
| 1028 | */ |
| 1029 | if (pagino >= rec->ir_startino && |
| 1030 | pagino < (rec->ir_startino + XFS_INODES_PER_CHUNK)) |
| 1031 | return 0; |
| 1032 | } |
| 1033 | |
| 1034 | error = xfs_btree_dup_cursor(lcur, &rcur); |
| 1035 | if (error) |
| 1036 | return error; |
| 1037 | |
| 1038 | error = xfs_inobt_lookup(rcur, pagino, XFS_LOOKUP_GE, &j); |
| 1039 | if (error) |
| 1040 | goto error_rcur; |
| 1041 | if (j == 1) { |
| 1042 | error = xfs_inobt_get_rec(rcur, &rrec, &j); |
| 1043 | if (error) |
| 1044 | goto error_rcur; |
| 1045 | XFS_WANT_CORRUPTED_GOTO(j == 1, error_rcur); |
| 1046 | } |
| 1047 | |
| 1048 | XFS_WANT_CORRUPTED_GOTO(i == 1 || j == 1, error_rcur); |
| 1049 | if (i == 1 && j == 1) { |
| 1050 | /* |
| 1051 | * Both the left and right records are valid. Choose the closer |
| 1052 | * inode chunk to the target. |
| 1053 | */ |
| 1054 | if ((pagino - rec->ir_startino + XFS_INODES_PER_CHUNK - 1) > |
| 1055 | (rrec.ir_startino - pagino)) { |
| 1056 | *rec = rrec; |
| 1057 | xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR); |
| 1058 | *ocur = rcur; |
| 1059 | } else { |
| 1060 | xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR); |
| 1061 | } |
| 1062 | } else if (j == 1) { |
| 1063 | /* only the right record is valid */ |
| 1064 | *rec = rrec; |
| 1065 | xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR); |
| 1066 | *ocur = rcur; |
| 1067 | } else if (i == 1) { |
| 1068 | /* only the left record is valid */ |
| 1069 | xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR); |
| 1070 | } |
| 1071 | |
| 1072 | return 0; |
| 1073 | |
| 1074 | error_rcur: |
| 1075 | xfs_btree_del_cursor(rcur, XFS_BTREE_ERROR); |
| 1076 | return error; |
| 1077 | } |
| 1078 | |
| 1079 | /* |
| 1080 | * Use the free inode btree to find a free inode based on a newino hint. If |
| 1081 | * the hint is NULL, find the first free inode in the AG. |
| 1082 | */ |
| 1083 | STATIC int |
| 1084 | xfs_dialloc_ag_finobt_newino( |
| 1085 | struct xfs_agi *agi, |
| 1086 | struct xfs_btree_cur *cur, |
| 1087 | struct xfs_inobt_rec_incore *rec) |
| 1088 | { |
| 1089 | int error; |
| 1090 | int i; |
| 1091 | |
| 1092 | if (agi->agi_newino != cpu_to_be32(NULLAGINO)) { |
Dave Chinner | e68ed77 | 2014-09-29 10:43:15 +1000 | [diff] [blame] | 1093 | error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino), |
| 1094 | XFS_LOOKUP_EQ, &i); |
Brian Foster | 6dd8638 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 1095 | if (error) |
| 1096 | return error; |
| 1097 | if (i == 1) { |
| 1098 | error = xfs_inobt_get_rec(cur, rec, &i); |
| 1099 | if (error) |
| 1100 | return error; |
| 1101 | XFS_WANT_CORRUPTED_RETURN(i == 1); |
Brian Foster | 6dd8638 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 1102 | return 0; |
| 1103 | } |
| 1104 | } |
| 1105 | |
| 1106 | /* |
| 1107 | * Find the first inode available in the AG. |
| 1108 | */ |
| 1109 | error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i); |
| 1110 | if (error) |
| 1111 | return error; |
| 1112 | XFS_WANT_CORRUPTED_RETURN(i == 1); |
| 1113 | |
| 1114 | error = xfs_inobt_get_rec(cur, rec, &i); |
| 1115 | if (error) |
| 1116 | return error; |
| 1117 | XFS_WANT_CORRUPTED_RETURN(i == 1); |
| 1118 | |
| 1119 | return 0; |
| 1120 | } |
| 1121 | |
| 1122 | /* |
| 1123 | * Update the inobt based on a modification made to the finobt. Also ensure that |
| 1124 | * the records from both trees are equivalent post-modification. |
| 1125 | */ |
| 1126 | STATIC int |
| 1127 | xfs_dialloc_ag_update_inobt( |
| 1128 | struct xfs_btree_cur *cur, /* inobt cursor */ |
| 1129 | struct xfs_inobt_rec_incore *frec, /* finobt record */ |
| 1130 | int offset) /* inode offset */ |
| 1131 | { |
| 1132 | struct xfs_inobt_rec_incore rec; |
| 1133 | int error; |
| 1134 | int i; |
| 1135 | |
| 1136 | error = xfs_inobt_lookup(cur, frec->ir_startino, XFS_LOOKUP_EQ, &i); |
| 1137 | if (error) |
| 1138 | return error; |
| 1139 | XFS_WANT_CORRUPTED_RETURN(i == 1); |
| 1140 | |
| 1141 | error = xfs_inobt_get_rec(cur, &rec, &i); |
| 1142 | if (error) |
| 1143 | return error; |
| 1144 | XFS_WANT_CORRUPTED_RETURN(i == 1); |
| 1145 | ASSERT((XFS_AGINO_TO_OFFSET(cur->bc_mp, rec.ir_startino) % |
| 1146 | XFS_INODES_PER_CHUNK) == 0); |
| 1147 | |
| 1148 | rec.ir_free &= ~XFS_INOBT_MASK(offset); |
| 1149 | rec.ir_freecount--; |
| 1150 | |
| 1151 | XFS_WANT_CORRUPTED_RETURN((rec.ir_free == frec->ir_free) && |
| 1152 | (rec.ir_freecount == frec->ir_freecount)); |
| 1153 | |
kbuild test robot | b72091f | 2014-12-01 08:24:58 +1100 | [diff] [blame] | 1154 | return xfs_inobt_update(cur, &rec); |
Brian Foster | 6dd8638 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 1155 | } |
| 1156 | |
| 1157 | /* |
| 1158 | * Allocate an inode using the free inode btree, if available. Otherwise, fall |
| 1159 | * back to the inobt search algorithm. |
| 1160 | * |
| 1161 | * The caller selected an AG for us, and made sure that free inodes are |
| 1162 | * available. |
| 1163 | */ |
| 1164 | STATIC int |
| 1165 | xfs_dialloc_ag( |
| 1166 | struct xfs_trans *tp, |
| 1167 | struct xfs_buf *agbp, |
| 1168 | xfs_ino_t parent, |
| 1169 | xfs_ino_t *inop) |
| 1170 | { |
| 1171 | struct xfs_mount *mp = tp->t_mountp; |
| 1172 | struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp); |
| 1173 | xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno); |
| 1174 | xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent); |
| 1175 | xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent); |
| 1176 | struct xfs_perag *pag; |
| 1177 | struct xfs_btree_cur *cur; /* finobt cursor */ |
| 1178 | struct xfs_btree_cur *icur; /* inobt cursor */ |
| 1179 | struct xfs_inobt_rec_incore rec; |
| 1180 | xfs_ino_t ino; |
| 1181 | int error; |
| 1182 | int offset; |
| 1183 | int i; |
| 1184 | |
| 1185 | if (!xfs_sb_version_hasfinobt(&mp->m_sb)) |
| 1186 | return xfs_dialloc_ag_inobt(tp, agbp, parent, inop); |
| 1187 | |
| 1188 | pag = xfs_perag_get(mp, agno); |
| 1189 | |
| 1190 | /* |
| 1191 | * If pagino is 0 (this is the root inode allocation) use newino. |
| 1192 | * This must work because we've just allocated some. |
| 1193 | */ |
| 1194 | if (!pagino) |
| 1195 | pagino = be32_to_cpu(agi->agi_newino); |
| 1196 | |
| 1197 | cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_FINO); |
| 1198 | |
| 1199 | error = xfs_check_agi_freecount(cur, agi); |
| 1200 | if (error) |
| 1201 | goto error_cur; |
| 1202 | |
| 1203 | /* |
| 1204 | * The search algorithm depends on whether we're in the same AG as the |
| 1205 | * parent. If so, find the closest available inode to the parent. If |
| 1206 | * not, consider the agi hint or find the first free inode in the AG. |
| 1207 | */ |
| 1208 | if (agno == pagno) |
| 1209 | error = xfs_dialloc_ag_finobt_near(pagino, &cur, &rec); |
| 1210 | else |
| 1211 | error = xfs_dialloc_ag_finobt_newino(agi, cur, &rec); |
| 1212 | if (error) |
| 1213 | goto error_cur; |
| 1214 | |
| 1215 | offset = xfs_lowbit64(rec.ir_free); |
| 1216 | ASSERT(offset >= 0); |
| 1217 | ASSERT(offset < XFS_INODES_PER_CHUNK); |
| 1218 | ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) % |
| 1219 | XFS_INODES_PER_CHUNK) == 0); |
| 1220 | ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset); |
| 1221 | |
| 1222 | /* |
| 1223 | * Modify or remove the finobt record. |
| 1224 | */ |
| 1225 | rec.ir_free &= ~XFS_INOBT_MASK(offset); |
| 1226 | rec.ir_freecount--; |
| 1227 | if (rec.ir_freecount) |
| 1228 | error = xfs_inobt_update(cur, &rec); |
| 1229 | else |
| 1230 | error = xfs_btree_delete(cur, &i); |
| 1231 | if (error) |
| 1232 | goto error_cur; |
| 1233 | |
| 1234 | /* |
| 1235 | * The finobt has now been updated appropriately. We haven't updated the |
| 1236 | * agi and superblock yet, so we can create an inobt cursor and validate |
| 1237 | * the original freecount. If all is well, make the equivalent update to |
| 1238 | * the inobt using the finobt record and offset information. |
| 1239 | */ |
| 1240 | icur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO); |
| 1241 | |
| 1242 | error = xfs_check_agi_freecount(icur, agi); |
| 1243 | if (error) |
| 1244 | goto error_icur; |
| 1245 | |
| 1246 | error = xfs_dialloc_ag_update_inobt(icur, &rec, offset); |
| 1247 | if (error) |
| 1248 | goto error_icur; |
| 1249 | |
| 1250 | /* |
| 1251 | * Both trees have now been updated. We must update the perag and |
| 1252 | * superblock before we can check the freecount for each btree. |
| 1253 | */ |
| 1254 | be32_add_cpu(&agi->agi_freecount, -1); |
| 1255 | xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT); |
| 1256 | pag->pagi_freecount--; |
| 1257 | |
| 1258 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1); |
| 1259 | |
| 1260 | error = xfs_check_agi_freecount(icur, agi); |
| 1261 | if (error) |
| 1262 | goto error_icur; |
| 1263 | error = xfs_check_agi_freecount(cur, agi); |
| 1264 | if (error) |
| 1265 | goto error_icur; |
| 1266 | |
| 1267 | xfs_btree_del_cursor(icur, XFS_BTREE_NOERROR); |
| 1268 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 1269 | xfs_perag_put(pag); |
| 1270 | *inop = ino; |
| 1271 | return 0; |
| 1272 | |
| 1273 | error_icur: |
| 1274 | xfs_btree_del_cursor(icur, XFS_BTREE_ERROR); |
| 1275 | error_cur: |
| 1276 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 1277 | xfs_perag_put(pag); |
| 1278 | return error; |
| 1279 | } |
| 1280 | |
| 1281 | /* |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 1282 | * Allocate an inode on disk. |
| 1283 | * |
| 1284 | * Mode is used to tell whether the new inode will need space, and whether it |
| 1285 | * is a directory. |
| 1286 | * |
| 1287 | * This function is designed to be called twice if it has to do an allocation |
| 1288 | * to make more free inodes. On the first call, *IO_agbp should be set to NULL. |
| 1289 | * 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] | 1290 | * number is returned. In this case, *IO_agbp is set to NULL. If an allocation |
| 1291 | * needs to be done, xfs_dialloc returns the current AGI buffer in *IO_agbp. |
| 1292 | * The caller should then commit the current transaction, allocate a |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 1293 | * new transaction, and call xfs_dialloc() again, passing in the previous value |
| 1294 | * of *IO_agbp. IO_agbp should be held across the transactions. Since the AGI |
| 1295 | * buffer is locked across the two calls, the second call is guaranteed to have |
| 1296 | * a free inode available. |
| 1297 | * |
| 1298 | * Once we successfully pick an inode its number is returned and the on-disk |
| 1299 | * data structures are updated. The inode itself is not read in, since doing so |
| 1300 | * would break ordering constraints with xfs_reclaim. |
| 1301 | */ |
| 1302 | int |
| 1303 | xfs_dialloc( |
| 1304 | struct xfs_trans *tp, |
| 1305 | xfs_ino_t parent, |
| 1306 | umode_t mode, |
| 1307 | int okalloc, |
| 1308 | struct xfs_buf **IO_agbp, |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 1309 | xfs_ino_t *inop) |
| 1310 | { |
Christoph Hellwig | 55d6af6 | 2012-07-04 10:54:49 -0400 | [diff] [blame] | 1311 | struct xfs_mount *mp = tp->t_mountp; |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 1312 | struct xfs_buf *agbp; |
| 1313 | xfs_agnumber_t agno; |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 1314 | int error; |
| 1315 | int ialloced; |
| 1316 | int noroom = 0; |
Christoph Hellwig | be60fe5 | 2012-07-04 10:54:50 -0400 | [diff] [blame] | 1317 | xfs_agnumber_t start_agno; |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 1318 | struct xfs_perag *pag; |
| 1319 | |
Christoph Hellwig | 4bb6106 | 2012-07-04 10:54:48 -0400 | [diff] [blame] | 1320 | if (*IO_agbp) { |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 1321 | /* |
Christoph Hellwig | 4bb6106 | 2012-07-04 10:54:48 -0400 | [diff] [blame] | 1322 | * If the caller passes in a pointer to the AGI buffer, |
| 1323 | * continue where we left off before. In this case, we |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 1324 | * know that the allocation group has free inodes. |
| 1325 | */ |
| 1326 | agbp = *IO_agbp; |
Christoph Hellwig | 4bb6106 | 2012-07-04 10:54:48 -0400 | [diff] [blame] | 1327 | goto out_alloc; |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 1328 | } |
Christoph Hellwig | 4bb6106 | 2012-07-04 10:54:48 -0400 | [diff] [blame] | 1329 | |
| 1330 | /* |
| 1331 | * We do not have an agbp, so select an initial allocation |
| 1332 | * group for inode allocation. |
| 1333 | */ |
Christoph Hellwig | be60fe5 | 2012-07-04 10:54:50 -0400 | [diff] [blame] | 1334 | start_agno = xfs_ialloc_ag_select(tp, parent, mode, okalloc); |
| 1335 | if (start_agno == NULLAGNUMBER) { |
Christoph Hellwig | 4bb6106 | 2012-07-04 10:54:48 -0400 | [diff] [blame] | 1336 | *inop = NULLFSINO; |
| 1337 | return 0; |
| 1338 | } |
Christoph Hellwig | 55d6af6 | 2012-07-04 10:54:49 -0400 | [diff] [blame] | 1339 | |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 1340 | /* |
| 1341 | * If we have already hit the ceiling of inode blocks then clear |
| 1342 | * okalloc so we scan all available agi structures for a free |
| 1343 | * inode. |
| 1344 | */ |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 1345 | if (mp->m_maxicount && |
Jie Liu | 7178343 | 2013-12-13 15:51:46 +1100 | [diff] [blame] | 1346 | mp->m_sb.sb_icount + mp->m_ialloc_inos > mp->m_maxicount) { |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 1347 | noroom = 1; |
| 1348 | okalloc = 0; |
| 1349 | } |
| 1350 | |
| 1351 | /* |
| 1352 | * Loop until we find an allocation group that either has free inodes |
| 1353 | * or in which we can allocate some inodes. Iterate through the |
| 1354 | * allocation groups upward, wrapping at the end. |
| 1355 | */ |
Christoph Hellwig | be60fe5 | 2012-07-04 10:54:50 -0400 | [diff] [blame] | 1356 | agno = start_agno; |
| 1357 | for (;;) { |
| 1358 | pag = xfs_perag_get(mp, agno); |
| 1359 | if (!pag->pagi_inodeok) { |
| 1360 | xfs_ialloc_next_ag(mp); |
| 1361 | goto nextag; |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 1362 | } |
Christoph Hellwig | be60fe5 | 2012-07-04 10:54:50 -0400 | [diff] [blame] | 1363 | |
| 1364 | if (!pag->pagi_init) { |
| 1365 | error = xfs_ialloc_pagi_init(mp, tp, agno); |
| 1366 | if (error) |
| 1367 | goto out_error; |
| 1368 | } |
| 1369 | |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 1370 | /* |
Christoph Hellwig | be60fe5 | 2012-07-04 10:54:50 -0400 | [diff] [blame] | 1371 | * Do a first racy fast path check if this AG is usable. |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 1372 | */ |
Christoph Hellwig | be60fe5 | 2012-07-04 10:54:50 -0400 | [diff] [blame] | 1373 | if (!pag->pagi_freecount && !okalloc) |
| 1374 | goto nextag; |
| 1375 | |
Christoph Hellwig | c498211 | 2012-08-07 02:02:02 -0400 | [diff] [blame] | 1376 | /* |
| 1377 | * Then read in the AGI buffer and recheck with the AGI buffer |
| 1378 | * lock held. |
| 1379 | */ |
Christoph Hellwig | be60fe5 | 2012-07-04 10:54:50 -0400 | [diff] [blame] | 1380 | error = xfs_ialloc_read_agi(mp, tp, agno, &agbp); |
| 1381 | if (error) |
| 1382 | goto out_error; |
| 1383 | |
Christoph Hellwig | be60fe5 | 2012-07-04 10:54:50 -0400 | [diff] [blame] | 1384 | if (pag->pagi_freecount) { |
| 1385 | xfs_perag_put(pag); |
| 1386 | goto out_alloc; |
| 1387 | } |
| 1388 | |
Christoph Hellwig | c498211 | 2012-08-07 02:02:02 -0400 | [diff] [blame] | 1389 | if (!okalloc) |
| 1390 | goto nextag_relse_buffer; |
| 1391 | |
Christoph Hellwig | be60fe5 | 2012-07-04 10:54:50 -0400 | [diff] [blame] | 1392 | |
| 1393 | error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced); |
| 1394 | if (error) { |
| 1395 | xfs_trans_brelse(tp, agbp); |
| 1396 | |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1397 | if (error != -ENOSPC) |
Christoph Hellwig | be60fe5 | 2012-07-04 10:54:50 -0400 | [diff] [blame] | 1398 | goto out_error; |
| 1399 | |
| 1400 | xfs_perag_put(pag); |
| 1401 | *inop = NULLFSINO; |
| 1402 | return 0; |
| 1403 | } |
| 1404 | |
| 1405 | if (ialloced) { |
| 1406 | /* |
| 1407 | * We successfully allocated some inodes, return |
| 1408 | * the current context to the caller so that it |
| 1409 | * can commit the current transaction and call |
| 1410 | * us again where we left off. |
| 1411 | */ |
| 1412 | ASSERT(pag->pagi_freecount > 0); |
| 1413 | xfs_perag_put(pag); |
| 1414 | |
| 1415 | *IO_agbp = agbp; |
| 1416 | *inop = NULLFSINO; |
| 1417 | return 0; |
| 1418 | } |
| 1419 | |
Christoph Hellwig | c498211 | 2012-08-07 02:02:02 -0400 | [diff] [blame] | 1420 | nextag_relse_buffer: |
| 1421 | xfs_trans_brelse(tp, agbp); |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 1422 | nextag: |
Christoph Hellwig | be60fe5 | 2012-07-04 10:54:50 -0400 | [diff] [blame] | 1423 | xfs_perag_put(pag); |
| 1424 | if (++agno == mp->m_sb.sb_agcount) |
| 1425 | agno = 0; |
| 1426 | if (agno == start_agno) { |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 1427 | *inop = NULLFSINO; |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1428 | return noroom ? -ENOSPC : 0; |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 1429 | } |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 1430 | } |
| 1431 | |
Christoph Hellwig | 4bb6106 | 2012-07-04 10:54:48 -0400 | [diff] [blame] | 1432 | out_alloc: |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 1433 | *IO_agbp = NULL; |
| 1434 | return xfs_dialloc_ag(tp, agbp, parent, inop); |
Christoph Hellwig | be60fe5 | 2012-07-04 10:54:50 -0400 | [diff] [blame] | 1435 | out_error: |
| 1436 | xfs_perag_put(pag); |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 1437 | return error; |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 1438 | } |
| 1439 | |
Brian Foster | 2b64ee5 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 1440 | STATIC int |
| 1441 | xfs_difree_inobt( |
| 1442 | struct xfs_mount *mp, |
| 1443 | struct xfs_trans *tp, |
| 1444 | struct xfs_buf *agbp, |
| 1445 | xfs_agino_t agino, |
| 1446 | struct xfs_bmap_free *flist, |
Dave Chinner | 0d907a3 | 2014-05-20 08:56:00 +1000 | [diff] [blame] | 1447 | int *deleted, |
Brian Foster | 2b64ee5 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 1448 | xfs_ino_t *first_ino, |
| 1449 | struct xfs_inobt_rec_incore *orec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1450 | { |
Brian Foster | 2b64ee5 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 1451 | struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp); |
| 1452 | xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno); |
| 1453 | struct xfs_perag *pag; |
| 1454 | struct xfs_btree_cur *cur; |
| 1455 | struct xfs_inobt_rec_incore rec; |
| 1456 | int ilen; |
| 1457 | int error; |
| 1458 | int i; |
| 1459 | int off; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1460 | |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 1461 | ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC)); |
Brian Foster | 2b64ee5 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 1462 | ASSERT(XFS_AGINO_TO_AGBNO(mp, agino) < be32_to_cpu(agi->agi_length)); |
| 1463 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | /* |
| 1465 | * Initialize the cursor. |
| 1466 | */ |
Brian Foster | 57bd3db | 2014-04-24 16:00:50 +1000 | [diff] [blame] | 1467 | cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1468 | |
Dave Chinner | 0b48db8 | 2009-08-31 20:57:09 -0300 | [diff] [blame] | 1469 | error = xfs_check_agi_freecount(cur, agi); |
| 1470 | if (error) |
| 1471 | goto error0; |
| 1472 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1473 | /* |
| 1474 | * Look for the entry describing this inode. |
| 1475 | */ |
Christoph Hellwig | 2187550 | 2009-08-31 20:58:21 -0300 | [diff] [blame] | 1476 | if ((error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i))) { |
Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 1477 | xfs_warn(mp, "%s: xfs_inobt_lookup() returned error %d.", |
| 1478 | __func__, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | goto error0; |
| 1480 | } |
| 1481 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); |
Christoph Hellwig | 2e287a7 | 2009-08-31 20:56:58 -0300 | [diff] [blame] | 1482 | error = xfs_inobt_get_rec(cur, &rec, &i); |
| 1483 | if (error) { |
Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 1484 | xfs_warn(mp, "%s: xfs_inobt_get_rec() returned error %d.", |
| 1485 | __func__, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1486 | goto error0; |
| 1487 | } |
| 1488 | XFS_WANT_CORRUPTED_GOTO(i == 1, error0); |
| 1489 | /* |
| 1490 | * Get the offset in the inode chunk. |
| 1491 | */ |
| 1492 | off = agino - rec.ir_startino; |
| 1493 | ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK); |
Christoph Hellwig | 0d87e65 | 2009-02-09 08:37:14 +0100 | [diff] [blame] | 1494 | ASSERT(!(rec.ir_free & XFS_INOBT_MASK(off))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1495 | /* |
| 1496 | * Mark the inode free & increment the count. |
| 1497 | */ |
Christoph Hellwig | 0d87e65 | 2009-02-09 08:37:14 +0100 | [diff] [blame] | 1498 | rec.ir_free |= XFS_INOBT_MASK(off); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | rec.ir_freecount++; |
| 1500 | |
| 1501 | /* |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 1502 | * When an inode cluster is free, it becomes eligible for removal |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1503 | */ |
Josef Jeff Sipek | 1bd960e | 2008-02-29 13:58:40 +1100 | [diff] [blame] | 1504 | if (!(mp->m_flags & XFS_MOUNT_IKEEP) && |
Jie Liu | 7178343 | 2013-12-13 15:51:46 +1100 | [diff] [blame] | 1505 | (rec.ir_freecount == mp->m_ialloc_inos)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | |
Roger Willcocks | 376c2f3 | 2014-05-20 08:52:21 +1000 | [diff] [blame] | 1507 | *deleted = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | *first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino); |
| 1509 | |
| 1510 | /* |
| 1511 | * Remove the inode cluster from the AGI B+Tree, adjust the |
| 1512 | * AGI and Superblock inode counts, and mark the disk space |
| 1513 | * to be freed when the transaction is committed. |
| 1514 | */ |
Jie Liu | 7178343 | 2013-12-13 15:51:46 +1100 | [diff] [blame] | 1515 | ilen = mp->m_ialloc_inos; |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 1516 | be32_add_cpu(&agi->agi_count, -ilen); |
| 1517 | be32_add_cpu(&agi->agi_freecount, -(ilen - 1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1518 | xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT); |
Dave Chinner | 44b56e0 | 2010-01-11 11:47:43 +0000 | [diff] [blame] | 1519 | pag = xfs_perag_get(mp, agno); |
| 1520 | pag->pagi_freecount -= ilen - 1; |
| 1521 | xfs_perag_put(pag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen); |
| 1523 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1)); |
| 1524 | |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 1525 | if ((error = xfs_btree_delete(cur, &i))) { |
Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 1526 | xfs_warn(mp, "%s: xfs_btree_delete returned error %d.", |
| 1527 | __func__, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1528 | goto error0; |
| 1529 | } |
| 1530 | |
Jie Liu | 126cd10 | 2013-12-13 15:51:48 +1100 | [diff] [blame] | 1531 | xfs_bmap_add_free(XFS_AGB_TO_FSB(mp, agno, |
| 1532 | XFS_AGINO_TO_AGBNO(mp, rec.ir_startino)), |
| 1533 | mp->m_ialloc_blks, flist, mp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1534 | } else { |
Roger Willcocks | 376c2f3 | 2014-05-20 08:52:21 +1000 | [diff] [blame] | 1535 | *deleted = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1536 | |
Christoph Hellwig | afabc24 | 2009-08-31 20:57:03 -0300 | [diff] [blame] | 1537 | error = xfs_inobt_update(cur, &rec); |
| 1538 | if (error) { |
Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 1539 | xfs_warn(mp, "%s: xfs_inobt_update returned error %d.", |
| 1540 | __func__, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1541 | goto error0; |
| 1542 | } |
Christoph Hellwig | afabc24 | 2009-08-31 20:57:03 -0300 | [diff] [blame] | 1543 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | /* |
| 1545 | * Change the inode free counts and log the ag/sb changes. |
| 1546 | */ |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 1547 | be32_add_cpu(&agi->agi_freecount, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1548 | xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT); |
Dave Chinner | 44b56e0 | 2010-01-11 11:47:43 +0000 | [diff] [blame] | 1549 | pag = xfs_perag_get(mp, agno); |
| 1550 | pag->pagi_freecount++; |
| 1551 | xfs_perag_put(pag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1552 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1); |
| 1553 | } |
| 1554 | |
Dave Chinner | 0b48db8 | 2009-08-31 20:57:09 -0300 | [diff] [blame] | 1555 | error = xfs_check_agi_freecount(cur, agi); |
| 1556 | if (error) |
| 1557 | goto error0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | |
Brian Foster | 2b64ee5 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 1559 | *orec = rec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1560 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 1561 | return 0; |
| 1562 | |
| 1563 | error0: |
| 1564 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 1565 | return error; |
| 1566 | } |
| 1567 | |
Brian Foster | 2b64ee5 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 1568 | /* |
Brian Foster | 3efa4ff | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 1569 | * Free an inode in the free inode btree. |
| 1570 | */ |
| 1571 | STATIC int |
| 1572 | xfs_difree_finobt( |
| 1573 | struct xfs_mount *mp, |
| 1574 | struct xfs_trans *tp, |
| 1575 | struct xfs_buf *agbp, |
| 1576 | xfs_agino_t agino, |
| 1577 | struct xfs_inobt_rec_incore *ibtrec) /* inobt record */ |
| 1578 | { |
| 1579 | struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp); |
| 1580 | xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno); |
| 1581 | struct xfs_btree_cur *cur; |
| 1582 | struct xfs_inobt_rec_incore rec; |
| 1583 | int offset = agino - ibtrec->ir_startino; |
| 1584 | int error; |
| 1585 | int i; |
| 1586 | |
| 1587 | cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_FINO); |
| 1588 | |
| 1589 | error = xfs_inobt_lookup(cur, ibtrec->ir_startino, XFS_LOOKUP_EQ, &i); |
| 1590 | if (error) |
| 1591 | goto error; |
| 1592 | if (i == 0) { |
| 1593 | /* |
| 1594 | * If the record does not exist in the finobt, we must have just |
| 1595 | * freed an inode in a previously fully allocated chunk. If not, |
| 1596 | * something is out of sync. |
| 1597 | */ |
| 1598 | XFS_WANT_CORRUPTED_GOTO(ibtrec->ir_freecount == 1, error); |
| 1599 | |
| 1600 | error = xfs_inobt_insert_rec(cur, ibtrec->ir_freecount, |
| 1601 | ibtrec->ir_free, &i); |
| 1602 | if (error) |
| 1603 | goto error; |
| 1604 | ASSERT(i == 1); |
| 1605 | |
| 1606 | goto out; |
| 1607 | } |
| 1608 | |
| 1609 | /* |
| 1610 | * Read and update the existing record. We could just copy the ibtrec |
| 1611 | * across here, but that would defeat the purpose of having redundant |
| 1612 | * metadata. By making the modifications independently, we can catch |
| 1613 | * corruptions that we wouldn't see if we just copied from one record |
| 1614 | * to another. |
| 1615 | */ |
| 1616 | error = xfs_inobt_get_rec(cur, &rec, &i); |
| 1617 | if (error) |
| 1618 | goto error; |
| 1619 | XFS_WANT_CORRUPTED_GOTO(i == 1, error); |
| 1620 | |
| 1621 | rec.ir_free |= XFS_INOBT_MASK(offset); |
| 1622 | rec.ir_freecount++; |
| 1623 | |
| 1624 | XFS_WANT_CORRUPTED_GOTO((rec.ir_free == ibtrec->ir_free) && |
| 1625 | (rec.ir_freecount == ibtrec->ir_freecount), |
| 1626 | error); |
| 1627 | |
| 1628 | /* |
| 1629 | * The content of inobt records should always match between the inobt |
| 1630 | * and finobt. The lifecycle of records in the finobt is different from |
| 1631 | * the inobt in that the finobt only tracks records with at least one |
| 1632 | * free inode. Hence, if all of the inodes are free and we aren't |
| 1633 | * keeping inode chunks permanently on disk, remove the record. |
| 1634 | * Otherwise, update the record with the new information. |
| 1635 | */ |
| 1636 | if (rec.ir_freecount == mp->m_ialloc_inos && |
| 1637 | !(mp->m_flags & XFS_MOUNT_IKEEP)) { |
| 1638 | error = xfs_btree_delete(cur, &i); |
| 1639 | if (error) |
| 1640 | goto error; |
| 1641 | ASSERT(i == 1); |
| 1642 | } else { |
| 1643 | error = xfs_inobt_update(cur, &rec); |
| 1644 | if (error) |
| 1645 | goto error; |
| 1646 | } |
| 1647 | |
| 1648 | out: |
| 1649 | error = xfs_check_agi_freecount(cur, agi); |
| 1650 | if (error) |
| 1651 | goto error; |
| 1652 | |
| 1653 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 1654 | return 0; |
| 1655 | |
| 1656 | error: |
| 1657 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 1658 | return error; |
| 1659 | } |
| 1660 | |
| 1661 | /* |
Brian Foster | 2b64ee5 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 1662 | * Free disk inode. Carefully avoids touching the incore inode, all |
| 1663 | * manipulations incore are the caller's responsibility. |
| 1664 | * The on-disk inode is not changed by this operation, only the |
| 1665 | * btree (free inode mask) is changed. |
| 1666 | */ |
| 1667 | int |
| 1668 | xfs_difree( |
| 1669 | struct xfs_trans *tp, /* transaction pointer */ |
| 1670 | xfs_ino_t inode, /* inode to be freed */ |
| 1671 | struct xfs_bmap_free *flist, /* extents to free */ |
Dave Chinner | 0d907a3 | 2014-05-20 08:56:00 +1000 | [diff] [blame] | 1672 | int *deleted,/* set if inode cluster was deleted */ |
Brian Foster | 2b64ee5 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 1673 | xfs_ino_t *first_ino)/* first inode in deleted cluster */ |
| 1674 | { |
| 1675 | /* REFERENCED */ |
| 1676 | xfs_agblock_t agbno; /* block number containing inode */ |
| 1677 | struct xfs_buf *agbp; /* buffer for allocation group header */ |
| 1678 | xfs_agino_t agino; /* allocation group inode number */ |
| 1679 | xfs_agnumber_t agno; /* allocation group number */ |
| 1680 | int error; /* error return value */ |
| 1681 | struct xfs_mount *mp; /* mount structure for filesystem */ |
| 1682 | struct xfs_inobt_rec_incore rec;/* btree record */ |
| 1683 | |
| 1684 | mp = tp->t_mountp; |
| 1685 | |
| 1686 | /* |
| 1687 | * Break up inode number into its components. |
| 1688 | */ |
| 1689 | agno = XFS_INO_TO_AGNO(mp, inode); |
| 1690 | if (agno >= mp->m_sb.sb_agcount) { |
| 1691 | xfs_warn(mp, "%s: agno >= mp->m_sb.sb_agcount (%d >= %d).", |
| 1692 | __func__, agno, mp->m_sb.sb_agcount); |
| 1693 | ASSERT(0); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1694 | return -EINVAL; |
Brian Foster | 2b64ee5 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 1695 | } |
| 1696 | agino = XFS_INO_TO_AGINO(mp, inode); |
| 1697 | if (inode != XFS_AGINO_TO_INO(mp, agno, agino)) { |
| 1698 | xfs_warn(mp, "%s: inode != XFS_AGINO_TO_INO() (%llu != %llu).", |
| 1699 | __func__, (unsigned long long)inode, |
| 1700 | (unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino)); |
| 1701 | ASSERT(0); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1702 | return -EINVAL; |
Brian Foster | 2b64ee5 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 1703 | } |
| 1704 | agbno = XFS_AGINO_TO_AGBNO(mp, agino); |
| 1705 | if (agbno >= mp->m_sb.sb_agblocks) { |
| 1706 | xfs_warn(mp, "%s: agbno >= mp->m_sb.sb_agblocks (%d >= %d).", |
| 1707 | __func__, agbno, mp->m_sb.sb_agblocks); |
| 1708 | ASSERT(0); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1709 | return -EINVAL; |
Brian Foster | 2b64ee5 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 1710 | } |
| 1711 | /* |
| 1712 | * Get the allocation group header. |
| 1713 | */ |
| 1714 | error = xfs_ialloc_read_agi(mp, tp, agno, &agbp); |
| 1715 | if (error) { |
| 1716 | xfs_warn(mp, "%s: xfs_ialloc_read_agi() returned error %d.", |
| 1717 | __func__, error); |
| 1718 | return error; |
| 1719 | } |
| 1720 | |
| 1721 | /* |
| 1722 | * Fix up the inode allocation btree. |
| 1723 | */ |
Dave Chinner | 0d907a3 | 2014-05-20 08:56:00 +1000 | [diff] [blame] | 1724 | error = xfs_difree_inobt(mp, tp, agbp, agino, flist, deleted, first_ino, |
Brian Foster | 2b64ee5 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 1725 | &rec); |
| 1726 | if (error) |
| 1727 | goto error0; |
| 1728 | |
Brian Foster | 3efa4ff | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 1729 | /* |
| 1730 | * Fix up the free inode btree. |
| 1731 | */ |
| 1732 | if (xfs_sb_version_hasfinobt(&mp->m_sb)) { |
| 1733 | error = xfs_difree_finobt(mp, tp, agbp, agino, &rec); |
| 1734 | if (error) |
| 1735 | goto error0; |
| 1736 | } |
| 1737 | |
Brian Foster | 2b64ee5 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 1738 | return 0; |
| 1739 | |
| 1740 | error0: |
| 1741 | return error; |
| 1742 | } |
| 1743 | |
Dave Chinner | 7124fe0 | 2010-06-24 11:15:33 +1000 | [diff] [blame] | 1744 | STATIC int |
| 1745 | xfs_imap_lookup( |
| 1746 | struct xfs_mount *mp, |
| 1747 | struct xfs_trans *tp, |
| 1748 | xfs_agnumber_t agno, |
| 1749 | xfs_agino_t agino, |
| 1750 | xfs_agblock_t agbno, |
| 1751 | xfs_agblock_t *chunk_agbno, |
| 1752 | xfs_agblock_t *offset_agbno, |
| 1753 | int flags) |
| 1754 | { |
| 1755 | struct xfs_inobt_rec_incore rec; |
| 1756 | struct xfs_btree_cur *cur; |
| 1757 | struct xfs_buf *agbp; |
Dave Chinner | 7124fe0 | 2010-06-24 11:15:33 +1000 | [diff] [blame] | 1758 | int error; |
| 1759 | int i; |
| 1760 | |
| 1761 | error = xfs_ialloc_read_agi(mp, tp, agno, &agbp); |
| 1762 | if (error) { |
Dave Chinner | 5348778 | 2011-03-07 10:05:35 +1100 | [diff] [blame] | 1763 | xfs_alert(mp, |
| 1764 | "%s: xfs_ialloc_read_agi() returned error %d, agno %d", |
| 1765 | __func__, error, agno); |
Dave Chinner | 7124fe0 | 2010-06-24 11:15:33 +1000 | [diff] [blame] | 1766 | return error; |
| 1767 | } |
| 1768 | |
| 1769 | /* |
Dave Chinner | 4536f2a | 2010-08-24 11:42:30 +1000 | [diff] [blame] | 1770 | * Lookup the inode record for the given agino. If the record cannot be |
| 1771 | * found, then it's an invalid inode number and we should abort. Once |
| 1772 | * we have a record, we need to ensure it contains the inode number |
| 1773 | * we are looking up. |
Dave Chinner | 7124fe0 | 2010-06-24 11:15:33 +1000 | [diff] [blame] | 1774 | */ |
Brian Foster | 57bd3db | 2014-04-24 16:00:50 +1000 | [diff] [blame] | 1775 | cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO); |
Dave Chinner | 4536f2a | 2010-08-24 11:42:30 +1000 | [diff] [blame] | 1776 | error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i); |
Dave Chinner | 7124fe0 | 2010-06-24 11:15:33 +1000 | [diff] [blame] | 1777 | if (!error) { |
| 1778 | if (i) |
| 1779 | error = xfs_inobt_get_rec(cur, &rec, &i); |
| 1780 | if (!error && i == 0) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1781 | error = -EINVAL; |
Dave Chinner | 7124fe0 | 2010-06-24 11:15:33 +1000 | [diff] [blame] | 1782 | } |
| 1783 | |
| 1784 | xfs_trans_brelse(tp, agbp); |
| 1785 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 1786 | if (error) |
| 1787 | return error; |
| 1788 | |
Dave Chinner | 4536f2a | 2010-08-24 11:42:30 +1000 | [diff] [blame] | 1789 | /* check that the returned record contains the required inode */ |
| 1790 | if (rec.ir_startino > agino || |
Jie Liu | 7178343 | 2013-12-13 15:51:46 +1100 | [diff] [blame] | 1791 | rec.ir_startino + mp->m_ialloc_inos <= agino) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1792 | return -EINVAL; |
Dave Chinner | 4536f2a | 2010-08-24 11:42:30 +1000 | [diff] [blame] | 1793 | |
Dave Chinner | 7124fe0 | 2010-06-24 11:15:33 +1000 | [diff] [blame] | 1794 | /* for untrusted inodes check it is allocated first */ |
Dave Chinner | 1920779 | 2010-06-24 11:15:47 +1000 | [diff] [blame] | 1795 | if ((flags & XFS_IGET_UNTRUSTED) && |
Dave Chinner | 7124fe0 | 2010-06-24 11:15:33 +1000 | [diff] [blame] | 1796 | (rec.ir_free & XFS_INOBT_MASK(agino - rec.ir_startino))) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1797 | return -EINVAL; |
Dave Chinner | 7124fe0 | 2010-06-24 11:15:33 +1000 | [diff] [blame] | 1798 | |
| 1799 | *chunk_agbno = XFS_AGINO_TO_AGBNO(mp, rec.ir_startino); |
| 1800 | *offset_agbno = agbno - *chunk_agbno; |
| 1801 | return 0; |
| 1802 | } |
| 1803 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1804 | /* |
Christoph Hellwig | 94e1b69 | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 1805 | * 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] | 1806 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1807 | int |
Christoph Hellwig | 94e1b69 | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 1808 | xfs_imap( |
| 1809 | xfs_mount_t *mp, /* file system mount structure */ |
| 1810 | xfs_trans_t *tp, /* transaction pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1811 | xfs_ino_t ino, /* inode to locate */ |
Christoph Hellwig | 94e1b69 | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 1812 | struct xfs_imap *imap, /* location map structure */ |
| 1813 | uint flags) /* flags for inode btree lookup */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1814 | { |
| 1815 | xfs_agblock_t agbno; /* block number of inode in the alloc group */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1816 | xfs_agino_t agino; /* inode number within alloc group */ |
| 1817 | xfs_agnumber_t agno; /* allocation group number */ |
| 1818 | int blks_per_cluster; /* num blocks per inode cluster */ |
| 1819 | xfs_agblock_t chunk_agbno; /* first block in inode chunk */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1820 | xfs_agblock_t cluster_agbno; /* first block in inode cluster */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1821 | int error; /* error code */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1822 | int offset; /* index of inode in its buffer */ |
Dave Chinner | 836a94a | 2013-08-12 20:49:44 +1000 | [diff] [blame] | 1823 | xfs_agblock_t offset_agbno; /* blks from chunk start to inode */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1824 | |
| 1825 | ASSERT(ino != NULLFSINO); |
Christoph Hellwig | 94e1b69 | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 1826 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1827 | /* |
| 1828 | * Split up the inode number into its parts. |
| 1829 | */ |
| 1830 | agno = XFS_INO_TO_AGNO(mp, ino); |
| 1831 | agino = XFS_INO_TO_AGINO(mp, ino); |
| 1832 | agbno = XFS_AGINO_TO_AGBNO(mp, agino); |
| 1833 | if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks || |
| 1834 | ino != XFS_AGINO_TO_INO(mp, agno, agino)) { |
| 1835 | #ifdef DEBUG |
Dave Chinner | 1920779 | 2010-06-24 11:15:47 +1000 | [diff] [blame] | 1836 | /* |
| 1837 | * Don't output diagnostic information for untrusted inodes |
| 1838 | * as they can be invalid without implying corruption. |
| 1839 | */ |
| 1840 | if (flags & XFS_IGET_UNTRUSTED) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1841 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1842 | if (agno >= mp->m_sb.sb_agcount) { |
Dave Chinner | 5348778 | 2011-03-07 10:05:35 +1100 | [diff] [blame] | 1843 | xfs_alert(mp, |
| 1844 | "%s: agno (%d) >= mp->m_sb.sb_agcount (%d)", |
| 1845 | __func__, agno, mp->m_sb.sb_agcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1846 | } |
| 1847 | if (agbno >= mp->m_sb.sb_agblocks) { |
Dave Chinner | 5348778 | 2011-03-07 10:05:35 +1100 | [diff] [blame] | 1848 | xfs_alert(mp, |
| 1849 | "%s: agbno (0x%llx) >= mp->m_sb.sb_agblocks (0x%lx)", |
| 1850 | __func__, (unsigned long long)agbno, |
| 1851 | (unsigned long)mp->m_sb.sb_agblocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1852 | } |
| 1853 | if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) { |
Dave Chinner | 5348778 | 2011-03-07 10:05:35 +1100 | [diff] [blame] | 1854 | xfs_alert(mp, |
| 1855 | "%s: ino (0x%llx) != XFS_AGINO_TO_INO() (0x%llx)", |
| 1856 | __func__, ino, |
| 1857 | XFS_AGINO_TO_INO(mp, agno, agino)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1858 | } |
Nathan Scott | 745b1f47 | 2006-09-28 11:02:23 +1000 | [diff] [blame] | 1859 | xfs_stack_trace(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1860 | #endif /* DEBUG */ |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1861 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1862 | } |
Christoph Hellwig | 94e1b69 | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 1863 | |
Jie Liu | f9e5abc | 2013-12-13 15:51:49 +1100 | [diff] [blame] | 1864 | blks_per_cluster = xfs_icluster_size_fsb(mp); |
Dave Chinner | 7124fe0 | 2010-06-24 11:15:33 +1000 | [diff] [blame] | 1865 | |
| 1866 | /* |
| 1867 | * For bulkstat and handle lookups, we have an untrusted inode number |
| 1868 | * that we have to verify is valid. We cannot do this just by reading |
| 1869 | * the inode buffer as it may have been unlinked and removed leaving |
| 1870 | * inodes in stale state on disk. Hence we have to do a btree lookup |
| 1871 | * in all cases where an untrusted inode number is passed. |
| 1872 | */ |
Dave Chinner | 1920779 | 2010-06-24 11:15:47 +1000 | [diff] [blame] | 1873 | if (flags & XFS_IGET_UNTRUSTED) { |
Dave Chinner | 7124fe0 | 2010-06-24 11:15:33 +1000 | [diff] [blame] | 1874 | error = xfs_imap_lookup(mp, tp, agno, agino, agbno, |
| 1875 | &chunk_agbno, &offset_agbno, flags); |
| 1876 | if (error) |
| 1877 | return error; |
| 1878 | goto out_map; |
| 1879 | } |
| 1880 | |
Christoph Hellwig | 94e1b69 | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 1881 | /* |
| 1882 | * If the inode cluster size is the same as the blocksize or |
| 1883 | * smaller we get to the buffer by simple arithmetics. |
| 1884 | */ |
Jie Liu | f9e5abc | 2013-12-13 15:51:49 +1100 | [diff] [blame] | 1885 | if (blks_per_cluster == 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1886 | offset = XFS_INO_TO_OFFSET(mp, ino); |
| 1887 | ASSERT(offset < mp->m_sb.sb_inopblock); |
Christoph Hellwig | 94e1b69 | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 1888 | |
| 1889 | imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, agbno); |
| 1890 | imap->im_len = XFS_FSB_TO_BB(mp, 1); |
| 1891 | imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1892 | return 0; |
| 1893 | } |
Christoph Hellwig | 94e1b69 | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 1894 | |
Christoph Hellwig | 94e1b69 | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 1895 | /* |
Christoph Hellwig | 94e1b69 | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 1896 | * If the inode chunks are aligned then use simple maths to |
| 1897 | * find the location. Otherwise we have to do a btree |
| 1898 | * lookup to find the location. |
| 1899 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1900 | if (mp->m_inoalign_mask) { |
| 1901 | offset_agbno = agbno & mp->m_inoalign_mask; |
| 1902 | chunk_agbno = agbno - offset_agbno; |
| 1903 | } else { |
Dave Chinner | 7124fe0 | 2010-06-24 11:15:33 +1000 | [diff] [blame] | 1904 | error = xfs_imap_lookup(mp, tp, agno, agino, agbno, |
| 1905 | &chunk_agbno, &offset_agbno, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1906 | if (error) |
| 1907 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1908 | } |
Christoph Hellwig | 94e1b69 | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 1909 | |
Dave Chinner | 7124fe0 | 2010-06-24 11:15:33 +1000 | [diff] [blame] | 1910 | out_map: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1911 | ASSERT(agbno >= chunk_agbno); |
| 1912 | cluster_agbno = chunk_agbno + |
| 1913 | ((offset_agbno / blks_per_cluster) * blks_per_cluster); |
| 1914 | offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) + |
| 1915 | XFS_INO_TO_OFFSET(mp, ino); |
Christoph Hellwig | 94e1b69 | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 1916 | |
| 1917 | imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, cluster_agbno); |
| 1918 | imap->im_len = XFS_FSB_TO_BB(mp, blks_per_cluster); |
| 1919 | imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog); |
| 1920 | |
| 1921 | /* |
| 1922 | * If the inode number maps to a block outside the bounds |
| 1923 | * of the file system then return NULL rather than calling |
| 1924 | * read_buf and panicing when we get an error from the |
| 1925 | * driver. |
| 1926 | */ |
| 1927 | if ((imap->im_blkno + imap->im_len) > |
| 1928 | XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) { |
Dave Chinner | 5348778 | 2011-03-07 10:05:35 +1100 | [diff] [blame] | 1929 | xfs_alert(mp, |
| 1930 | "%s: (im_blkno (0x%llx) + im_len (0x%llx)) > sb_dblocks (0x%llx)", |
| 1931 | __func__, (unsigned long long) imap->im_blkno, |
Christoph Hellwig | 94e1b69 | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 1932 | (unsigned long long) imap->im_len, |
| 1933 | XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1934 | return -EINVAL; |
Christoph Hellwig | 94e1b69 | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 1935 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1936 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1937 | } |
| 1938 | |
| 1939 | /* |
| 1940 | * Compute and fill in value of m_in_maxlevels. |
| 1941 | */ |
| 1942 | void |
| 1943 | xfs_ialloc_compute_maxlevels( |
| 1944 | xfs_mount_t *mp) /* file system mount structure */ |
| 1945 | { |
| 1946 | int level; |
| 1947 | uint maxblocks; |
| 1948 | uint maxleafents; |
| 1949 | int minleafrecs; |
| 1950 | int minnoderecs; |
| 1951 | |
| 1952 | maxleafents = (1LL << XFS_INO_AGINO_BITS(mp)) >> |
| 1953 | XFS_INODES_PER_CHUNK_LOG; |
| 1954 | minleafrecs = mp->m_alloc_mnr[0]; |
| 1955 | minnoderecs = mp->m_alloc_mnr[1]; |
| 1956 | maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs; |
| 1957 | for (level = 1; maxblocks > 1; level++) |
| 1958 | maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs; |
| 1959 | mp->m_in_maxlevels = level; |
| 1960 | } |
| 1961 | |
| 1962 | /* |
Brian Foster | aafc3c2 | 2014-04-24 16:00:52 +1000 | [diff] [blame] | 1963 | * Log specified fields for the ag hdr (inode section). The growth of the agi |
| 1964 | * structure over time requires that we interpret the buffer as two logical |
| 1965 | * regions delineated by the end of the unlinked list. This is due to the size |
| 1966 | * of the hash table and its location in the middle of the agi. |
| 1967 | * |
| 1968 | * For example, a request to log a field before agi_unlinked and a field after |
| 1969 | * agi_unlinked could cause us to log the entire hash table and use an excessive |
| 1970 | * amount of log space. To avoid this behavior, log the region up through |
| 1971 | * agi_unlinked in one call and the region after agi_unlinked through the end of |
| 1972 | * the structure in another. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1973 | */ |
| 1974 | void |
| 1975 | xfs_ialloc_log_agi( |
| 1976 | xfs_trans_t *tp, /* transaction pointer */ |
| 1977 | xfs_buf_t *bp, /* allocation group header buffer */ |
| 1978 | int fields) /* bitmask of fields to log */ |
| 1979 | { |
| 1980 | int first; /* first byte number */ |
| 1981 | int last; /* last byte number */ |
| 1982 | static const short offsets[] = { /* field starting offsets */ |
| 1983 | /* keep in sync with bit definitions */ |
| 1984 | offsetof(xfs_agi_t, agi_magicnum), |
| 1985 | offsetof(xfs_agi_t, agi_versionnum), |
| 1986 | offsetof(xfs_agi_t, agi_seqno), |
| 1987 | offsetof(xfs_agi_t, agi_length), |
| 1988 | offsetof(xfs_agi_t, agi_count), |
| 1989 | offsetof(xfs_agi_t, agi_root), |
| 1990 | offsetof(xfs_agi_t, agi_level), |
| 1991 | offsetof(xfs_agi_t, agi_freecount), |
| 1992 | offsetof(xfs_agi_t, agi_newino), |
| 1993 | offsetof(xfs_agi_t, agi_dirino), |
| 1994 | offsetof(xfs_agi_t, agi_unlinked), |
Brian Foster | aafc3c2 | 2014-04-24 16:00:52 +1000 | [diff] [blame] | 1995 | offsetof(xfs_agi_t, agi_free_root), |
| 1996 | offsetof(xfs_agi_t, agi_free_level), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1997 | sizeof(xfs_agi_t) |
| 1998 | }; |
| 1999 | #ifdef DEBUG |
| 2000 | xfs_agi_t *agi; /* allocation group header */ |
| 2001 | |
| 2002 | agi = XFS_BUF_TO_AGI(bp); |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 2003 | ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2004 | #endif |
Brian Foster | aafc3c2 | 2014-04-24 16:00:52 +1000 | [diff] [blame] | 2005 | |
Dave Chinner | 61fe135 | 2013-04-03 16:11:30 +1100 | [diff] [blame] | 2006 | xfs_trans_buf_set_type(tp, bp, XFS_BLFT_AGI_BUF); |
Brian Foster | aafc3c2 | 2014-04-24 16:00:52 +1000 | [diff] [blame] | 2007 | |
| 2008 | /* |
| 2009 | * Compute byte offsets for the first and last fields in the first |
| 2010 | * region and log the agi buffer. This only logs up through |
| 2011 | * agi_unlinked. |
| 2012 | */ |
| 2013 | if (fields & XFS_AGI_ALL_BITS_R1) { |
| 2014 | xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R1, |
| 2015 | &first, &last); |
| 2016 | xfs_trans_log_buf(tp, bp, first, last); |
| 2017 | } |
| 2018 | |
| 2019 | /* |
| 2020 | * Mask off the bits in the first region and calculate the first and |
| 2021 | * last field offsets for any bits in the second region. |
| 2022 | */ |
| 2023 | fields &= ~XFS_AGI_ALL_BITS_R1; |
| 2024 | if (fields) { |
| 2025 | xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R2, |
| 2026 | &first, &last); |
| 2027 | xfs_trans_log_buf(tp, bp, first, last); |
| 2028 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2029 | } |
| 2030 | |
Christoph Hellwig | 5e1be0f | 2008-11-28 14:23:37 +1100 | [diff] [blame] | 2031 | #ifdef DEBUG |
| 2032 | STATIC void |
| 2033 | xfs_check_agi_unlinked( |
| 2034 | struct xfs_agi *agi) |
| 2035 | { |
| 2036 | int i; |
| 2037 | |
| 2038 | for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++) |
| 2039 | ASSERT(agi->agi_unlinked[i]); |
| 2040 | } |
| 2041 | #else |
| 2042 | #define xfs_check_agi_unlinked(agi) |
| 2043 | #endif |
| 2044 | |
Dave Chinner | 983d09f | 2013-04-03 16:11:15 +1100 | [diff] [blame] | 2045 | static bool |
Dave Chinner | 612cfbf | 2012-11-14 17:52:32 +1100 | [diff] [blame] | 2046 | xfs_agi_verify( |
Dave Chinner | 3702ce6 | 2012-11-12 22:54:05 +1100 | [diff] [blame] | 2047 | struct xfs_buf *bp) |
| 2048 | { |
| 2049 | struct xfs_mount *mp = bp->b_target->bt_mount; |
| 2050 | struct xfs_agi *agi = XFS_BUF_TO_AGI(bp); |
Dave Chinner | 3702ce6 | 2012-11-12 22:54:05 +1100 | [diff] [blame] | 2051 | |
Dave Chinner | 983d09f | 2013-04-03 16:11:15 +1100 | [diff] [blame] | 2052 | if (xfs_sb_version_hascrc(&mp->m_sb) && |
| 2053 | !uuid_equal(&agi->agi_uuid, &mp->m_sb.sb_uuid)) |
| 2054 | return false; |
Dave Chinner | 3702ce6 | 2012-11-12 22:54:05 +1100 | [diff] [blame] | 2055 | /* |
| 2056 | * Validate the magic number of the agi block. |
| 2057 | */ |
Dave Chinner | 983d09f | 2013-04-03 16:11:15 +1100 | [diff] [blame] | 2058 | if (agi->agi_magicnum != cpu_to_be32(XFS_AGI_MAGIC)) |
| 2059 | return false; |
| 2060 | if (!XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum))) |
| 2061 | return false; |
Dave Chinner | 3702ce6 | 2012-11-12 22:54:05 +1100 | [diff] [blame] | 2062 | |
Eric Sandeen | e1b0572 | 2014-09-09 11:47:24 +1000 | [diff] [blame] | 2063 | if (be32_to_cpu(agi->agi_level) > XFS_BTREE_MAXLEVELS) |
| 2064 | return false; |
Dave Chinner | 3702ce6 | 2012-11-12 22:54:05 +1100 | [diff] [blame] | 2065 | /* |
| 2066 | * during growfs operations, the perag is not fully initialised, |
| 2067 | * so we can't use it for any useful checking. growfs ensures we can't |
| 2068 | * use it by using uncached buffers that don't have the perag attached |
| 2069 | * so we can detect and avoid this problem. |
| 2070 | */ |
Dave Chinner | 983d09f | 2013-04-03 16:11:15 +1100 | [diff] [blame] | 2071 | if (bp->b_pag && be32_to_cpu(agi->agi_seqno) != bp->b_pag->pag_agno) |
| 2072 | return false; |
Dave Chinner | 3702ce6 | 2012-11-12 22:54:05 +1100 | [diff] [blame] | 2073 | |
Dave Chinner | 3702ce6 | 2012-11-12 22:54:05 +1100 | [diff] [blame] | 2074 | xfs_check_agi_unlinked(agi); |
Dave Chinner | 983d09f | 2013-04-03 16:11:15 +1100 | [diff] [blame] | 2075 | return true; |
Dave Chinner | 612cfbf | 2012-11-14 17:52:32 +1100 | [diff] [blame] | 2076 | } |
| 2077 | |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 2078 | static void |
| 2079 | xfs_agi_read_verify( |
Dave Chinner | 612cfbf | 2012-11-14 17:52:32 +1100 | [diff] [blame] | 2080 | struct xfs_buf *bp) |
| 2081 | { |
Dave Chinner | 983d09f | 2013-04-03 16:11:15 +1100 | [diff] [blame] | 2082 | struct xfs_mount *mp = bp->b_target->bt_mount; |
Dave Chinner | 983d09f | 2013-04-03 16:11:15 +1100 | [diff] [blame] | 2083 | |
Eric Sandeen | ce5028c | 2014-02-27 15:23:10 +1100 | [diff] [blame] | 2084 | if (xfs_sb_version_hascrc(&mp->m_sb) && |
| 2085 | !xfs_buf_verify_cksum(bp, XFS_AGI_CRC_OFF)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 2086 | xfs_buf_ioerror(bp, -EFSBADCRC); |
Eric Sandeen | ce5028c | 2014-02-27 15:23:10 +1100 | [diff] [blame] | 2087 | else if (XFS_TEST_ERROR(!xfs_agi_verify(bp), mp, |
| 2088 | XFS_ERRTAG_IALLOC_READ_AGI, |
| 2089 | XFS_RANDOM_IALLOC_READ_AGI)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 2090 | xfs_buf_ioerror(bp, -EFSCORRUPTED); |
Eric Sandeen | ce5028c | 2014-02-27 15:23:10 +1100 | [diff] [blame] | 2091 | |
| 2092 | if (bp->b_error) |
| 2093 | xfs_verifier_error(bp); |
Dave Chinner | 612cfbf | 2012-11-14 17:52:32 +1100 | [diff] [blame] | 2094 | } |
| 2095 | |
Dave Chinner | b0f539d | 2012-11-14 17:53:49 +1100 | [diff] [blame] | 2096 | static void |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 2097 | xfs_agi_write_verify( |
Dave Chinner | 612cfbf | 2012-11-14 17:52:32 +1100 | [diff] [blame] | 2098 | struct xfs_buf *bp) |
| 2099 | { |
Dave Chinner | 983d09f | 2013-04-03 16:11:15 +1100 | [diff] [blame] | 2100 | struct xfs_mount *mp = bp->b_target->bt_mount; |
| 2101 | struct xfs_buf_log_item *bip = bp->b_fspriv; |
| 2102 | |
| 2103 | if (!xfs_agi_verify(bp)) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 2104 | xfs_buf_ioerror(bp, -EFSCORRUPTED); |
Eric Sandeen | ce5028c | 2014-02-27 15:23:10 +1100 | [diff] [blame] | 2105 | xfs_verifier_error(bp); |
Dave Chinner | 983d09f | 2013-04-03 16:11:15 +1100 | [diff] [blame] | 2106 | return; |
| 2107 | } |
| 2108 | |
| 2109 | if (!xfs_sb_version_hascrc(&mp->m_sb)) |
| 2110 | return; |
| 2111 | |
| 2112 | if (bip) |
| 2113 | 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] | 2114 | xfs_buf_update_cksum(bp, XFS_AGI_CRC_OFF); |
Dave Chinner | 3702ce6 | 2012-11-12 22:54:05 +1100 | [diff] [blame] | 2115 | } |
| 2116 | |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 2117 | const struct xfs_buf_ops xfs_agi_buf_ops = { |
| 2118 | .verify_read = xfs_agi_read_verify, |
| 2119 | .verify_write = xfs_agi_write_verify, |
| 2120 | }; |
| 2121 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2122 | /* |
| 2123 | * Read in the allocation group header (inode allocation section) |
| 2124 | */ |
| 2125 | int |
Christoph Hellwig | 5e1be0f | 2008-11-28 14:23:37 +1100 | [diff] [blame] | 2126 | xfs_read_agi( |
| 2127 | struct xfs_mount *mp, /* file system mount structure */ |
| 2128 | struct xfs_trans *tp, /* transaction pointer */ |
| 2129 | xfs_agnumber_t agno, /* allocation group number */ |
| 2130 | struct xfs_buf **bpp) /* allocation group hdr buf */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2131 | { |
Christoph Hellwig | 5e1be0f | 2008-11-28 14:23:37 +1100 | [diff] [blame] | 2132 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2133 | |
Dave Chinner | d123031 | 2013-11-01 15:27:19 +1100 | [diff] [blame] | 2134 | trace_xfs_read_agi(mp, agno); |
Christoph Hellwig | 5e1be0f | 2008-11-28 14:23:37 +1100 | [diff] [blame] | 2135 | |
Dave Chinner | d123031 | 2013-11-01 15:27:19 +1100 | [diff] [blame] | 2136 | ASSERT(agno != NULLAGNUMBER); |
Christoph Hellwig | 5e1be0f | 2008-11-28 14:23:37 +1100 | [diff] [blame] | 2137 | error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2138 | XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)), |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 2139 | XFS_FSS_TO_BB(mp, 1), 0, bpp, &xfs_agi_buf_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2140 | if (error) |
| 2141 | return error; |
Christoph Hellwig | 5e1be0f | 2008-11-28 14:23:37 +1100 | [diff] [blame] | 2142 | |
Christoph Hellwig | 38f2323 | 2011-10-10 16:52:45 +0000 | [diff] [blame] | 2143 | xfs_buf_set_ref(*bpp, XFS_AGI_REF); |
Christoph Hellwig | 5e1be0f | 2008-11-28 14:23:37 +1100 | [diff] [blame] | 2144 | return 0; |
| 2145 | } |
| 2146 | |
| 2147 | int |
| 2148 | xfs_ialloc_read_agi( |
| 2149 | struct xfs_mount *mp, /* file system mount structure */ |
| 2150 | struct xfs_trans *tp, /* transaction pointer */ |
| 2151 | xfs_agnumber_t agno, /* allocation group number */ |
| 2152 | struct xfs_buf **bpp) /* allocation group hdr buf */ |
| 2153 | { |
| 2154 | struct xfs_agi *agi; /* allocation group header */ |
| 2155 | struct xfs_perag *pag; /* per allocation group data */ |
| 2156 | int error; |
| 2157 | |
Dave Chinner | d123031 | 2013-11-01 15:27:19 +1100 | [diff] [blame] | 2158 | trace_xfs_ialloc_read_agi(mp, agno); |
| 2159 | |
Christoph Hellwig | 5e1be0f | 2008-11-28 14:23:37 +1100 | [diff] [blame] | 2160 | error = xfs_read_agi(mp, tp, agno, bpp); |
| 2161 | if (error) |
| 2162 | return error; |
| 2163 | |
| 2164 | agi = XFS_BUF_TO_AGI(*bpp); |
Dave Chinner | 44b56e0 | 2010-01-11 11:47:43 +0000 | [diff] [blame] | 2165 | pag = xfs_perag_get(mp, agno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2166 | if (!pag->pagi_init) { |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 2167 | pag->pagi_freecount = be32_to_cpu(agi->agi_freecount); |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 2168 | pag->pagi_count = be32_to_cpu(agi->agi_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2169 | pag->pagi_init = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2170 | } |
| 2171 | |
Christoph Hellwig | 5e1be0f | 2008-11-28 14:23:37 +1100 | [diff] [blame] | 2172 | /* |
| 2173 | * It's possible for these to be out of sync if |
| 2174 | * we are in the middle of a forced shutdown. |
| 2175 | */ |
| 2176 | ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) || |
| 2177 | XFS_FORCED_SHUTDOWN(mp)); |
Dave Chinner | 44b56e0 | 2010-01-11 11:47:43 +0000 | [diff] [blame] | 2178 | xfs_perag_put(pag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2179 | return 0; |
| 2180 | } |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 2181 | |
| 2182 | /* |
| 2183 | * Read in the agi to initialise the per-ag data in the mount structure |
| 2184 | */ |
| 2185 | int |
| 2186 | xfs_ialloc_pagi_init( |
| 2187 | xfs_mount_t *mp, /* file system mount structure */ |
| 2188 | xfs_trans_t *tp, /* transaction pointer */ |
| 2189 | xfs_agnumber_t agno) /* allocation group number */ |
| 2190 | { |
| 2191 | xfs_buf_t *bp = NULL; |
| 2192 | int error; |
| 2193 | |
| 2194 | error = xfs_ialloc_read_agi(mp, tp, agno, &bp); |
| 2195 | if (error) |
| 2196 | return error; |
| 2197 | if (bp) |
| 2198 | xfs_trans_brelse(tp, bp); |
| 2199 | return 0; |
| 2200 | } |