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