Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 2 | * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc. |
| 3 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License as |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * published by the Free Software Foundation. |
| 8 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 9 | * This program is distributed in the hope that it would be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write the Free Software Foundation, |
| 16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "xfs.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 19 | #include "xfs_fs.h" |
Dave Chinner | 70a9883 | 2013-10-23 10:36:05 +1100 | [diff] [blame] | 20 | #include "xfs_shared.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 21 | #include "xfs_format.h" |
| 22 | #include "xfs_log_format.h" |
| 23 | #include "xfs_trans_resv.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 24 | #include "xfs_bit.h" |
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" |
Darrick J. Wong | 3ab78df | 2016-08-03 11:15:38 +1000 | [diff] [blame] | 27 | #include "xfs_defer.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include "xfs_inode.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 29 | #include "xfs_btree.h" |
| 30 | #include "xfs_ialloc.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 31 | #include "xfs_ialloc_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include "xfs_alloc.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include "xfs_rtalloc.h" |
Darrick J. Wong | e9e899a | 2017-10-31 12:04:49 -0700 | [diff] [blame] | 34 | #include "xfs_errortag.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include "xfs_error.h" |
| 36 | #include "xfs_bmap.h" |
Dave Chinner | 983d09f | 2013-04-03 16:11:15 +1100 | [diff] [blame] | 37 | #include "xfs_cksum.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 38 | #include "xfs_trans.h" |
Dave Chinner | 983d09f | 2013-04-03 16:11:15 +1100 | [diff] [blame] | 39 | #include "xfs_buf_item.h" |
Dave Chinner | ddf6ad0 | 2013-06-27 16:04:56 +1000 | [diff] [blame] | 40 | #include "xfs_icreate_item.h" |
Dave Chinner | 7bb85ef | 2013-08-12 20:49:34 +1000 | [diff] [blame] | 41 | #include "xfs_icache.h" |
Dave Chinner | d123031 | 2013-11-01 15:27:19 +1100 | [diff] [blame] | 42 | #include "xfs_trace.h" |
Brian Foster | a45086e | 2015-10-12 15:59:25 +1100 | [diff] [blame] | 43 | #include "xfs_log.h" |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 44 | #include "xfs_rmap.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
| 47 | /* |
| 48 | * Allocation group level functions. |
| 49 | */ |
Darrick J. Wong | e936945 | 2017-06-16 11:00:08 -0700 | [diff] [blame] | 50 | int |
David Chinner | 75de2a9 | 2008-03-27 18:00:38 +1100 | [diff] [blame] | 51 | xfs_ialloc_cluster_alignment( |
Dave Chinner | 7a1df15 | 2014-12-04 09:42:21 +1100 | [diff] [blame] | 52 | struct xfs_mount *mp) |
David Chinner | 75de2a9 | 2008-03-27 18:00:38 +1100 | [diff] [blame] | 53 | { |
Dave Chinner | 7a1df15 | 2014-12-04 09:42:21 +1100 | [diff] [blame] | 54 | if (xfs_sb_version_hasalign(&mp->m_sb) && |
Chandan Rajendra | 8ee9fdb | 2017-02-16 17:12:16 -0800 | [diff] [blame] | 55 | mp->m_sb.sb_inoalignmt >= xfs_icluster_size_fsb(mp)) |
Dave Chinner | 7a1df15 | 2014-12-04 09:42:21 +1100 | [diff] [blame] | 56 | return mp->m_sb.sb_inoalignmt; |
David Chinner | 75de2a9 | 2008-03-27 18:00:38 +1100 | [diff] [blame] | 57 | return 1; |
| 58 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
| 60 | /* |
Christoph Hellwig | 2187550 | 2009-08-31 20:58:21 -0300 | [diff] [blame] | 61 | * Lookup a record by ino in the btree given by cur. |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 62 | */ |
Christoph Hellwig | 81e2517 | 2009-09-01 19:56:55 -0400 | [diff] [blame] | 63 | int /* error */ |
Christoph Hellwig | 2187550 | 2009-08-31 20:58:21 -0300 | [diff] [blame] | 64 | xfs_inobt_lookup( |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 65 | struct xfs_btree_cur *cur, /* btree cursor */ |
| 66 | xfs_agino_t ino, /* starting inode of chunk */ |
Christoph Hellwig | 2187550 | 2009-08-31 20:58:21 -0300 | [diff] [blame] | 67 | xfs_lookup_t dir, /* <=, >=, == */ |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 68 | int *stat) /* success/failure */ |
| 69 | { |
| 70 | cur->bc_rec.i.ir_startino = ino; |
Brian Foster | 5419040 | 2015-05-29 09:03:04 +1000 | [diff] [blame] | 71 | cur->bc_rec.i.ir_holemask = 0; |
| 72 | cur->bc_rec.i.ir_count = 0; |
Christoph Hellwig | 2187550 | 2009-08-31 20:58:21 -0300 | [diff] [blame] | 73 | cur->bc_rec.i.ir_freecount = 0; |
| 74 | cur->bc_rec.i.ir_free = 0; |
| 75 | return xfs_btree_lookup(cur, dir, stat); |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /* |
Christoph Hellwig | afabc24 | 2009-08-31 20:57:03 -0300 | [diff] [blame] | 79 | * Update the record referred to by cur to the value given. |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 80 | * This either works (return 0) or gets an EFSCORRUPTED error. |
| 81 | */ |
| 82 | STATIC int /* error */ |
| 83 | xfs_inobt_update( |
| 84 | struct xfs_btree_cur *cur, /* btree cursor */ |
Christoph Hellwig | afabc24 | 2009-08-31 20:57:03 -0300 | [diff] [blame] | 85 | xfs_inobt_rec_incore_t *irec) /* btree record */ |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 86 | { |
| 87 | union xfs_btree_rec rec; |
| 88 | |
Christoph Hellwig | afabc24 | 2009-08-31 20:57:03 -0300 | [diff] [blame] | 89 | rec.inobt.ir_startino = cpu_to_be32(irec->ir_startino); |
Brian Foster | 5419040 | 2015-05-29 09:03:04 +1000 | [diff] [blame] | 90 | if (xfs_sb_version_hassparseinodes(&cur->bc_mp->m_sb)) { |
| 91 | rec.inobt.ir_u.sp.ir_holemask = cpu_to_be16(irec->ir_holemask); |
| 92 | rec.inobt.ir_u.sp.ir_count = irec->ir_count; |
| 93 | rec.inobt.ir_u.sp.ir_freecount = irec->ir_freecount; |
| 94 | } else { |
| 95 | /* ir_holemask/ir_count not supported on-disk */ |
| 96 | rec.inobt.ir_u.f.ir_freecount = cpu_to_be32(irec->ir_freecount); |
| 97 | } |
Christoph Hellwig | afabc24 | 2009-08-31 20:57:03 -0300 | [diff] [blame] | 98 | rec.inobt.ir_free = cpu_to_be64(irec->ir_free); |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 99 | return xfs_btree_update(cur, &rec); |
| 100 | } |
| 101 | |
Darrick J. Wong | e936945 | 2017-06-16 11:00:08 -0700 | [diff] [blame] | 102 | /* Convert on-disk btree record to incore inobt record. */ |
| 103 | void |
| 104 | xfs_inobt_btrec_to_irec( |
| 105 | struct xfs_mount *mp, |
| 106 | union xfs_btree_rec *rec, |
| 107 | struct xfs_inobt_rec_incore *irec) |
Christoph Hellwig | 8cc938f | 2008-10-30 16:58:11 +1100 | [diff] [blame] | 108 | { |
Brian Foster | 5419040 | 2015-05-29 09:03:04 +1000 | [diff] [blame] | 109 | irec->ir_startino = be32_to_cpu(rec->inobt.ir_startino); |
Darrick J. Wong | e936945 | 2017-06-16 11:00:08 -0700 | [diff] [blame] | 110 | if (xfs_sb_version_hassparseinodes(&mp->m_sb)) { |
Brian Foster | 5419040 | 2015-05-29 09:03:04 +1000 | [diff] [blame] | 111 | irec->ir_holemask = be16_to_cpu(rec->inobt.ir_u.sp.ir_holemask); |
| 112 | irec->ir_count = rec->inobt.ir_u.sp.ir_count; |
| 113 | irec->ir_freecount = rec->inobt.ir_u.sp.ir_freecount; |
| 114 | } else { |
| 115 | /* |
| 116 | * ir_holemask/ir_count not supported on-disk. Fill in hardcoded |
| 117 | * values for full inode chunks. |
| 118 | */ |
| 119 | irec->ir_holemask = XFS_INOBT_HOLEMASK_FULL; |
| 120 | irec->ir_count = XFS_INODES_PER_CHUNK; |
| 121 | irec->ir_freecount = |
| 122 | be32_to_cpu(rec->inobt.ir_u.f.ir_freecount); |
Christoph Hellwig | 8cc938f | 2008-10-30 16:58:11 +1100 | [diff] [blame] | 123 | } |
Brian Foster | 5419040 | 2015-05-29 09:03:04 +1000 | [diff] [blame] | 124 | irec->ir_free = be64_to_cpu(rec->inobt.ir_free); |
Darrick J. Wong | e936945 | 2017-06-16 11:00:08 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /* |
| 128 | * Get the data from the pointed-to record. |
| 129 | */ |
| 130 | int |
| 131 | xfs_inobt_get_rec( |
| 132 | struct xfs_btree_cur *cur, |
| 133 | struct xfs_inobt_rec_incore *irec, |
| 134 | int *stat) |
| 135 | { |
Dave Chinner | 9e6c08d | 2018-06-05 19:42:13 -0700 | [diff] [blame^] | 136 | struct xfs_mount *mp = cur->bc_mp; |
| 137 | xfs_agnumber_t agno = cur->bc_private.a.agno; |
Darrick J. Wong | e936945 | 2017-06-16 11:00:08 -0700 | [diff] [blame] | 138 | union xfs_btree_rec *rec; |
| 139 | int error; |
Dave Chinner | 9e6c08d | 2018-06-05 19:42:13 -0700 | [diff] [blame^] | 140 | uint64_t realfree; |
Darrick J. Wong | e936945 | 2017-06-16 11:00:08 -0700 | [diff] [blame] | 141 | |
| 142 | error = xfs_btree_get_rec(cur, &rec, stat); |
| 143 | if (error || *stat == 0) |
| 144 | return error; |
| 145 | |
Dave Chinner | 9e6c08d | 2018-06-05 19:42:13 -0700 | [diff] [blame^] | 146 | xfs_inobt_btrec_to_irec(mp, rec, irec); |
| 147 | |
| 148 | if (!xfs_verify_agino(mp, agno, irec->ir_startino)) |
| 149 | goto out_bad_rec; |
| 150 | if (irec->ir_count < XFS_INODES_PER_HOLEMASK_BIT || |
| 151 | irec->ir_count > XFS_INODES_PER_CHUNK) |
| 152 | goto out_bad_rec; |
| 153 | if (irec->ir_freecount > XFS_INODES_PER_CHUNK) |
| 154 | goto out_bad_rec; |
| 155 | |
| 156 | /* if there are no holes, return the first available offset */ |
| 157 | if (!xfs_inobt_issparse(irec->ir_holemask)) |
| 158 | realfree = irec->ir_free; |
| 159 | else |
| 160 | realfree = irec->ir_free & xfs_inobt_irec_to_allocmask(irec); |
| 161 | if (hweight64(realfree) != irec->ir_freecount) |
| 162 | goto out_bad_rec; |
Brian Foster | 5419040 | 2015-05-29 09:03:04 +1000 | [diff] [blame] | 163 | |
| 164 | return 0; |
Dave Chinner | 9e6c08d | 2018-06-05 19:42:13 -0700 | [diff] [blame^] | 165 | |
| 166 | out_bad_rec: |
| 167 | xfs_warn(mp, |
| 168 | "%s Inode BTree record corruption in AG %d detected!", |
| 169 | cur->bc_btnum == XFS_BTNUM_INO ? "Used" : "Free", agno); |
| 170 | xfs_warn(mp, |
| 171 | "start inode 0x%x, count 0x%x, free 0x%x freemask 0x%llx, holemask 0x%x", |
| 172 | irec->ir_startino, irec->ir_count, irec->ir_freecount, |
| 173 | irec->ir_free, irec->ir_holemask); |
| 174 | return -EFSCORRUPTED; |
Christoph Hellwig | 8cc938f | 2008-10-30 16:58:11 +1100 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /* |
Brian Foster | 0aa0a75 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 178 | * Insert a single inobt record. Cursor must already point to desired location. |
| 179 | */ |
Darrick J. Wong | 7f8f131 | 2018-05-09 10:02:02 -0700 | [diff] [blame] | 180 | int |
Brian Foster | 0aa0a75 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 181 | xfs_inobt_insert_rec( |
| 182 | struct xfs_btree_cur *cur, |
Darrick J. Wong | c8ce540 | 2017-06-16 11:00:05 -0700 | [diff] [blame] | 183 | uint16_t holemask, |
| 184 | uint8_t count, |
| 185 | int32_t freecount, |
Brian Foster | 0aa0a75 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 186 | xfs_inofree_t free, |
| 187 | int *stat) |
| 188 | { |
Brian Foster | 5419040 | 2015-05-29 09:03:04 +1000 | [diff] [blame] | 189 | cur->bc_rec.i.ir_holemask = holemask; |
| 190 | cur->bc_rec.i.ir_count = count; |
Brian Foster | 0aa0a75 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 191 | cur->bc_rec.i.ir_freecount = freecount; |
| 192 | cur->bc_rec.i.ir_free = free; |
| 193 | return xfs_btree_insert(cur, stat); |
| 194 | } |
| 195 | |
| 196 | /* |
| 197 | * Insert records describing a newly allocated inode chunk into the inobt. |
| 198 | */ |
| 199 | STATIC int |
| 200 | xfs_inobt_insert( |
| 201 | struct xfs_mount *mp, |
| 202 | struct xfs_trans *tp, |
| 203 | struct xfs_buf *agbp, |
| 204 | xfs_agino_t newino, |
| 205 | xfs_agino_t newlen, |
| 206 | xfs_btnum_t btnum) |
| 207 | { |
| 208 | struct xfs_btree_cur *cur; |
| 209 | struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp); |
| 210 | xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno); |
| 211 | xfs_agino_t thisino; |
| 212 | int i; |
| 213 | int error; |
| 214 | |
| 215 | cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, btnum); |
| 216 | |
| 217 | for (thisino = newino; |
| 218 | thisino < newino + newlen; |
| 219 | thisino += XFS_INODES_PER_CHUNK) { |
| 220 | error = xfs_inobt_lookup(cur, thisino, XFS_LOOKUP_EQ, &i); |
| 221 | if (error) { |
| 222 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 223 | return error; |
| 224 | } |
| 225 | ASSERT(i == 0); |
| 226 | |
Brian Foster | 5419040 | 2015-05-29 09:03:04 +1000 | [diff] [blame] | 227 | error = xfs_inobt_insert_rec(cur, XFS_INOBT_HOLEMASK_FULL, |
| 228 | XFS_INODES_PER_CHUNK, |
| 229 | XFS_INODES_PER_CHUNK, |
Brian Foster | 0aa0a75 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 230 | XFS_INOBT_ALL_FREE, &i); |
| 231 | if (error) { |
| 232 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 233 | return error; |
| 234 | } |
| 235 | ASSERT(i == 1); |
| 236 | } |
| 237 | |
| 238 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 239 | |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | /* |
Dave Chinner | 0b48db8 | 2009-08-31 20:57:09 -0300 | [diff] [blame] | 244 | * Verify that the number of free inodes in the AGI is correct. |
| 245 | */ |
| 246 | #ifdef DEBUG |
| 247 | STATIC int |
| 248 | xfs_check_agi_freecount( |
| 249 | struct xfs_btree_cur *cur, |
| 250 | struct xfs_agi *agi) |
| 251 | { |
| 252 | if (cur->bc_nlevels == 1) { |
| 253 | xfs_inobt_rec_incore_t rec; |
| 254 | int freecount = 0; |
| 255 | int error; |
| 256 | int i; |
| 257 | |
Christoph Hellwig | 2187550 | 2009-08-31 20:58:21 -0300 | [diff] [blame] | 258 | error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i); |
Dave Chinner | 0b48db8 | 2009-08-31 20:57:09 -0300 | [diff] [blame] | 259 | if (error) |
| 260 | return error; |
| 261 | |
| 262 | do { |
| 263 | error = xfs_inobt_get_rec(cur, &rec, &i); |
| 264 | if (error) |
| 265 | return error; |
| 266 | |
| 267 | if (i) { |
| 268 | freecount += rec.ir_freecount; |
| 269 | error = xfs_btree_increment(cur, 0, &i); |
| 270 | if (error) |
| 271 | return error; |
| 272 | } |
| 273 | } while (i == 1); |
| 274 | |
| 275 | if (!XFS_FORCED_SHUTDOWN(cur->bc_mp)) |
| 276 | ASSERT(freecount == be32_to_cpu(agi->agi_freecount)); |
| 277 | } |
| 278 | return 0; |
| 279 | } |
| 280 | #else |
| 281 | #define xfs_check_agi_freecount(cur, agi) 0 |
| 282 | #endif |
| 283 | |
| 284 | /* |
Dave Chinner | 28c8e41 | 2013-06-27 16:04:55 +1000 | [diff] [blame] | 285 | * Initialise a new set of inodes. When called without a transaction context |
| 286 | * (e.g. from recovery) we initiate a delayed write of the inode buffers rather |
| 287 | * than logging them (which in a transaction context puts them into the AIL |
| 288 | * for writeback rather than the xfsbufd queue). |
Dave Chinner | 85c0b2a | 2009-08-31 20:56:51 -0300 | [diff] [blame] | 289 | */ |
Dave Chinner | ddf6ad0 | 2013-06-27 16:04:56 +1000 | [diff] [blame] | 290 | int |
Dave Chinner | 85c0b2a | 2009-08-31 20:56:51 -0300 | [diff] [blame] | 291 | xfs_ialloc_inode_init( |
| 292 | struct xfs_mount *mp, |
| 293 | struct xfs_trans *tp, |
Dave Chinner | 28c8e41 | 2013-06-27 16:04:55 +1000 | [diff] [blame] | 294 | struct list_head *buffer_list, |
Brian Foster | 463958a | 2015-05-29 09:05:49 +1000 | [diff] [blame] | 295 | int icount, |
Dave Chinner | 85c0b2a | 2009-08-31 20:56:51 -0300 | [diff] [blame] | 296 | xfs_agnumber_t agno, |
| 297 | xfs_agblock_t agbno, |
| 298 | xfs_agblock_t length, |
| 299 | unsigned int gen) |
| 300 | { |
| 301 | struct xfs_buf *fbuf; |
| 302 | struct xfs_dinode *free; |
Jie Liu | 6e0c7b8 | 2013-12-13 15:51:49 +1100 | [diff] [blame] | 303 | int nbufs, blks_per_cluster, inodes_per_cluster; |
Dave Chinner | 85c0b2a | 2009-08-31 20:56:51 -0300 | [diff] [blame] | 304 | int version; |
| 305 | int i, j; |
| 306 | xfs_daddr_t d; |
Christoph Hellwig | 93848a9 | 2013-04-03 16:11:17 +1100 | [diff] [blame] | 307 | xfs_ino_t ino = 0; |
Dave Chinner | 85c0b2a | 2009-08-31 20:56:51 -0300 | [diff] [blame] | 308 | |
| 309 | /* |
Jie Liu | 6e0c7b8 | 2013-12-13 15:51:49 +1100 | [diff] [blame] | 310 | * Loop over the new block(s), filling in the inodes. For small block |
| 311 | * sizes, manipulate the inodes in buffers which are multiples of the |
| 312 | * blocks size. |
Dave Chinner | 85c0b2a | 2009-08-31 20:56:51 -0300 | [diff] [blame] | 313 | */ |
Jie Liu | 6e0c7b8 | 2013-12-13 15:51:49 +1100 | [diff] [blame] | 314 | blks_per_cluster = xfs_icluster_size_fsb(mp); |
| 315 | inodes_per_cluster = blks_per_cluster << mp->m_sb.sb_inopblog; |
| 316 | nbufs = length / blks_per_cluster; |
Dave Chinner | 85c0b2a | 2009-08-31 20:56:51 -0300 | [diff] [blame] | 317 | |
| 318 | /* |
Christoph Hellwig | 93848a9 | 2013-04-03 16:11:17 +1100 | [diff] [blame] | 319 | * Figure out what version number to use in the inodes we create. If |
| 320 | * the superblock version has caught up to the one that supports the new |
| 321 | * inode format, then use the new inode version. Otherwise use the old |
| 322 | * version so that old kernels will continue to be able to use the file |
| 323 | * system. |
| 324 | * |
| 325 | * For v3 inodes, we also need to write the inode number into the inode, |
| 326 | * so calculate the first inode number of the chunk here as |
| 327 | * XFS_OFFBNO_TO_AGINO() only works within a filesystem block, not |
| 328 | * across multiple filesystem blocks (such as a cluster) and so cannot |
| 329 | * be used in the cluster buffer loop below. |
| 330 | * |
| 331 | * Further, because we are writing the inode directly into the buffer |
| 332 | * and calculating a CRC on the entire inode, we have ot log the entire |
| 333 | * inode so that the entire range the CRC covers is present in the log. |
| 334 | * That means for v3 inode we log the entire buffer rather than just the |
| 335 | * inode cores. |
Dave Chinner | 85c0b2a | 2009-08-31 20:56:51 -0300 | [diff] [blame] | 336 | */ |
Christoph Hellwig | 93848a9 | 2013-04-03 16:11:17 +1100 | [diff] [blame] | 337 | if (xfs_sb_version_hascrc(&mp->m_sb)) { |
| 338 | version = 3; |
| 339 | ino = XFS_AGINO_TO_INO(mp, agno, |
| 340 | XFS_OFFBNO_TO_AGINO(mp, agbno, 0)); |
Dave Chinner | ddf6ad0 | 2013-06-27 16:04:56 +1000 | [diff] [blame] | 341 | |
| 342 | /* |
| 343 | * log the initialisation that is about to take place as an |
| 344 | * logical operation. This means the transaction does not |
| 345 | * need to log the physical changes to the inode buffers as log |
| 346 | * recovery will know what initialisation is actually needed. |
| 347 | * Hence we only need to log the buffers as "ordered" buffers so |
| 348 | * they track in the AIL as if they were physically logged. |
| 349 | */ |
| 350 | if (tp) |
Brian Foster | 463958a | 2015-05-29 09:05:49 +1000 | [diff] [blame] | 351 | xfs_icreate_log(tp, agno, agbno, icount, |
Dave Chinner | ddf6ad0 | 2013-06-27 16:04:56 +1000 | [diff] [blame] | 352 | mp->m_sb.sb_inodesize, length, gen); |
Dave Chinner | 263997a | 2014-05-20 07:46:40 +1000 | [diff] [blame] | 353 | } else |
Dave Chinner | 85c0b2a | 2009-08-31 20:56:51 -0300 | [diff] [blame] | 354 | version = 2; |
Dave Chinner | 85c0b2a | 2009-08-31 20:56:51 -0300 | [diff] [blame] | 355 | |
| 356 | for (j = 0; j < nbufs; j++) { |
| 357 | /* |
| 358 | * Get the block. |
| 359 | */ |
| 360 | d = XFS_AGB_TO_DADDR(mp, agno, agbno + (j * blks_per_cluster)); |
| 361 | fbuf = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, |
Dave Chinner | 7c4cebe | 2012-11-23 14:24:23 +1100 | [diff] [blame] | 362 | mp->m_bsize * blks_per_cluster, |
| 363 | XBF_UNMAPPED); |
Chandra Seetharaman | 2a30f36d | 2011-09-20 13:56:55 +0000 | [diff] [blame] | 364 | if (!fbuf) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 365 | return -ENOMEM; |
Dave Chinner | ddf6ad0 | 2013-06-27 16:04:56 +1000 | [diff] [blame] | 366 | |
| 367 | /* Initialize the inode buffers and log them appropriately. */ |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 368 | fbuf->b_ops = &xfs_inode_buf_ops; |
Christoph Hellwig | 93848a9 | 2013-04-03 16:11:17 +1100 | [diff] [blame] | 369 | xfs_buf_zero(fbuf, 0, BBTOB(fbuf->b_length)); |
Jie Liu | 6e0c7b8 | 2013-12-13 15:51:49 +1100 | [diff] [blame] | 370 | for (i = 0; i < inodes_per_cluster; i++) { |
Dave Chinner | 85c0b2a | 2009-08-31 20:56:51 -0300 | [diff] [blame] | 371 | int ioffset = i << mp->m_sb.sb_inodelog; |
Christoph Hellwig | 93848a9 | 2013-04-03 16:11:17 +1100 | [diff] [blame] | 372 | uint isize = xfs_dinode_size(version); |
Dave Chinner | 85c0b2a | 2009-08-31 20:56:51 -0300 | [diff] [blame] | 373 | |
| 374 | free = xfs_make_iptr(mp, fbuf, i); |
| 375 | free->di_magic = cpu_to_be16(XFS_DINODE_MAGIC); |
| 376 | free->di_version = version; |
| 377 | free->di_gen = cpu_to_be32(gen); |
| 378 | free->di_next_unlinked = cpu_to_be32(NULLAGINO); |
Christoph Hellwig | 93848a9 | 2013-04-03 16:11:17 +1100 | [diff] [blame] | 379 | |
| 380 | if (version == 3) { |
| 381 | free->di_ino = cpu_to_be64(ino); |
| 382 | ino++; |
Eric Sandeen | ce748ea | 2015-07-29 11:53:31 +1000 | [diff] [blame] | 383 | uuid_copy(&free->di_uuid, |
| 384 | &mp->m_sb.sb_meta_uuid); |
Christoph Hellwig | 93848a9 | 2013-04-03 16:11:17 +1100 | [diff] [blame] | 385 | xfs_dinode_calc_crc(mp, free); |
Dave Chinner | 28c8e41 | 2013-06-27 16:04:55 +1000 | [diff] [blame] | 386 | } else if (tp) { |
Christoph Hellwig | 93848a9 | 2013-04-03 16:11:17 +1100 | [diff] [blame] | 387 | /* just log the inode core */ |
| 388 | xfs_trans_log_buf(tp, fbuf, ioffset, |
| 389 | ioffset + isize - 1); |
| 390 | } |
| 391 | } |
Dave Chinner | 28c8e41 | 2013-06-27 16:04:55 +1000 | [diff] [blame] | 392 | |
| 393 | if (tp) { |
| 394 | /* |
| 395 | * Mark the buffer as an inode allocation buffer so it |
| 396 | * sticks in AIL at the point of this allocation |
| 397 | * transaction. This ensures the they are on disk before |
| 398 | * the tail of the log can be moved past this |
| 399 | * transaction (i.e. by preventing relogging from moving |
| 400 | * it forward in the log). |
| 401 | */ |
| 402 | xfs_trans_inode_alloc_buf(tp, fbuf); |
| 403 | if (version == 3) { |
Dave Chinner | ddf6ad0 | 2013-06-27 16:04:56 +1000 | [diff] [blame] | 404 | /* |
| 405 | * Mark the buffer as ordered so that they are |
| 406 | * not physically logged in the transaction but |
| 407 | * still tracked in the AIL as part of the |
| 408 | * transaction and pin the log appropriately. |
| 409 | */ |
| 410 | xfs_trans_ordered_buf(tp, fbuf); |
Dave Chinner | 28c8e41 | 2013-06-27 16:04:55 +1000 | [diff] [blame] | 411 | } |
| 412 | } else { |
| 413 | fbuf->b_flags |= XBF_DONE; |
| 414 | xfs_buf_delwri_queue(fbuf, buffer_list); |
| 415 | xfs_buf_relse(fbuf); |
Dave Chinner | 85c0b2a | 2009-08-31 20:56:51 -0300 | [diff] [blame] | 416 | } |
Dave Chinner | 85c0b2a | 2009-08-31 20:56:51 -0300 | [diff] [blame] | 417 | } |
Chandra Seetharaman | 2a30f36d | 2011-09-20 13:56:55 +0000 | [diff] [blame] | 418 | return 0; |
Dave Chinner | 85c0b2a | 2009-08-31 20:56:51 -0300 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | /* |
Brian Foster | 56d1115 | 2015-05-29 09:18:32 +1000 | [diff] [blame] | 422 | * Align startino and allocmask for a recently allocated sparse chunk such that |
| 423 | * they are fit for insertion (or merge) into the on-disk inode btrees. |
| 424 | * |
| 425 | * Background: |
| 426 | * |
| 427 | * When enabled, sparse inode support increases the inode alignment from cluster |
| 428 | * size to inode chunk size. This means that the minimum range between two |
| 429 | * non-adjacent inode records in the inobt is large enough for a full inode |
| 430 | * record. This allows for cluster sized, cluster aligned block allocation |
| 431 | * without need to worry about whether the resulting inode record overlaps with |
| 432 | * another record in the tree. Without this basic rule, we would have to deal |
| 433 | * with the consequences of overlap by potentially undoing recent allocations in |
| 434 | * the inode allocation codepath. |
| 435 | * |
| 436 | * Because of this alignment rule (which is enforced on mount), there are two |
| 437 | * inobt possibilities for newly allocated sparse chunks. One is that the |
| 438 | * aligned inode record for the chunk covers a range of inodes not already |
| 439 | * covered in the inobt (i.e., it is safe to insert a new sparse record). The |
| 440 | * other is that a record already exists at the aligned startino that considers |
| 441 | * the newly allocated range as sparse. In the latter case, record content is |
| 442 | * merged in hope that sparse inode chunks fill to full chunks over time. |
| 443 | */ |
| 444 | STATIC void |
| 445 | xfs_align_sparse_ino( |
| 446 | struct xfs_mount *mp, |
| 447 | xfs_agino_t *startino, |
| 448 | uint16_t *allocmask) |
| 449 | { |
| 450 | xfs_agblock_t agbno; |
| 451 | xfs_agblock_t mod; |
| 452 | int offset; |
| 453 | |
| 454 | agbno = XFS_AGINO_TO_AGBNO(mp, *startino); |
| 455 | mod = agbno % mp->m_sb.sb_inoalignmt; |
| 456 | if (!mod) |
| 457 | return; |
| 458 | |
| 459 | /* calculate the inode offset and align startino */ |
| 460 | offset = mod << mp->m_sb.sb_inopblog; |
| 461 | *startino -= offset; |
| 462 | |
| 463 | /* |
| 464 | * Since startino has been aligned down, left shift allocmask such that |
| 465 | * it continues to represent the same physical inodes relative to the |
| 466 | * new startino. |
| 467 | */ |
| 468 | *allocmask <<= offset / XFS_INODES_PER_HOLEMASK_BIT; |
| 469 | } |
| 470 | |
| 471 | /* |
| 472 | * Determine whether the source inode record can merge into the target. Both |
| 473 | * records must be sparse, the inode ranges must match and there must be no |
| 474 | * allocation overlap between the records. |
| 475 | */ |
| 476 | STATIC bool |
| 477 | __xfs_inobt_can_merge( |
| 478 | struct xfs_inobt_rec_incore *trec, /* tgt record */ |
| 479 | struct xfs_inobt_rec_incore *srec) /* src record */ |
| 480 | { |
| 481 | uint64_t talloc; |
| 482 | uint64_t salloc; |
| 483 | |
| 484 | /* records must cover the same inode range */ |
| 485 | if (trec->ir_startino != srec->ir_startino) |
| 486 | return false; |
| 487 | |
| 488 | /* both records must be sparse */ |
| 489 | if (!xfs_inobt_issparse(trec->ir_holemask) || |
| 490 | !xfs_inobt_issparse(srec->ir_holemask)) |
| 491 | return false; |
| 492 | |
| 493 | /* both records must track some inodes */ |
| 494 | if (!trec->ir_count || !srec->ir_count) |
| 495 | return false; |
| 496 | |
| 497 | /* can't exceed capacity of a full record */ |
| 498 | if (trec->ir_count + srec->ir_count > XFS_INODES_PER_CHUNK) |
| 499 | return false; |
| 500 | |
| 501 | /* verify there is no allocation overlap */ |
| 502 | talloc = xfs_inobt_irec_to_allocmask(trec); |
| 503 | salloc = xfs_inobt_irec_to_allocmask(srec); |
| 504 | if (talloc & salloc) |
| 505 | return false; |
| 506 | |
| 507 | return true; |
| 508 | } |
| 509 | |
| 510 | /* |
| 511 | * Merge the source inode record into the target. The caller must call |
| 512 | * __xfs_inobt_can_merge() to ensure the merge is valid. |
| 513 | */ |
| 514 | STATIC void |
| 515 | __xfs_inobt_rec_merge( |
| 516 | struct xfs_inobt_rec_incore *trec, /* target */ |
| 517 | struct xfs_inobt_rec_incore *srec) /* src */ |
| 518 | { |
| 519 | ASSERT(trec->ir_startino == srec->ir_startino); |
| 520 | |
| 521 | /* combine the counts */ |
| 522 | trec->ir_count += srec->ir_count; |
| 523 | trec->ir_freecount += srec->ir_freecount; |
| 524 | |
| 525 | /* |
| 526 | * Merge the holemask and free mask. For both fields, 0 bits refer to |
| 527 | * allocated inodes. We combine the allocated ranges with bitwise AND. |
| 528 | */ |
| 529 | trec->ir_holemask &= srec->ir_holemask; |
| 530 | trec->ir_free &= srec->ir_free; |
| 531 | } |
| 532 | |
| 533 | /* |
| 534 | * Insert a new sparse inode chunk into the associated inode btree. The inode |
| 535 | * record for the sparse chunk is pre-aligned to a startino that should match |
| 536 | * any pre-existing sparse inode record in the tree. This allows sparse chunks |
| 537 | * to fill over time. |
| 538 | * |
| 539 | * This function supports two modes of handling preexisting records depending on |
| 540 | * the merge flag. If merge is true, the provided record is merged with the |
| 541 | * existing record and updated in place. The merged record is returned in nrec. |
| 542 | * If merge is false, an existing record is replaced with the provided record. |
| 543 | * If no preexisting record exists, the provided record is always inserted. |
| 544 | * |
| 545 | * It is considered corruption if a merge is requested and not possible. Given |
| 546 | * the sparse inode alignment constraints, this should never happen. |
| 547 | */ |
| 548 | STATIC int |
| 549 | xfs_inobt_insert_sprec( |
| 550 | struct xfs_mount *mp, |
| 551 | struct xfs_trans *tp, |
| 552 | struct xfs_buf *agbp, |
| 553 | int btnum, |
| 554 | struct xfs_inobt_rec_incore *nrec, /* in/out: new/merged rec. */ |
| 555 | bool merge) /* merge or replace */ |
| 556 | { |
| 557 | struct xfs_btree_cur *cur; |
| 558 | struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp); |
| 559 | xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno); |
| 560 | int error; |
| 561 | int i; |
| 562 | struct xfs_inobt_rec_incore rec; |
| 563 | |
| 564 | cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, btnum); |
| 565 | |
| 566 | /* the new record is pre-aligned so we know where to look */ |
| 567 | error = xfs_inobt_lookup(cur, nrec->ir_startino, XFS_LOOKUP_EQ, &i); |
| 568 | if (error) |
| 569 | goto error; |
| 570 | /* if nothing there, insert a new record and return */ |
| 571 | if (i == 0) { |
| 572 | error = xfs_inobt_insert_rec(cur, nrec->ir_holemask, |
| 573 | nrec->ir_count, nrec->ir_freecount, |
| 574 | nrec->ir_free, &i); |
| 575 | if (error) |
| 576 | goto error; |
| 577 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error); |
| 578 | |
| 579 | goto out; |
| 580 | } |
| 581 | |
| 582 | /* |
| 583 | * A record exists at this startino. Merge or replace the record |
| 584 | * depending on what we've been asked to do. |
| 585 | */ |
| 586 | if (merge) { |
| 587 | error = xfs_inobt_get_rec(cur, &rec, &i); |
| 588 | if (error) |
| 589 | goto error; |
| 590 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error); |
| 591 | XFS_WANT_CORRUPTED_GOTO(mp, |
| 592 | rec.ir_startino == nrec->ir_startino, |
| 593 | error); |
| 594 | |
| 595 | /* |
| 596 | * This should never fail. If we have coexisting records that |
| 597 | * cannot merge, something is seriously wrong. |
| 598 | */ |
| 599 | XFS_WANT_CORRUPTED_GOTO(mp, __xfs_inobt_can_merge(nrec, &rec), |
| 600 | error); |
| 601 | |
| 602 | trace_xfs_irec_merge_pre(mp, agno, rec.ir_startino, |
| 603 | rec.ir_holemask, nrec->ir_startino, |
| 604 | nrec->ir_holemask); |
| 605 | |
| 606 | /* merge to nrec to output the updated record */ |
| 607 | __xfs_inobt_rec_merge(nrec, &rec); |
| 608 | |
| 609 | trace_xfs_irec_merge_post(mp, agno, nrec->ir_startino, |
| 610 | nrec->ir_holemask); |
| 611 | |
| 612 | error = xfs_inobt_rec_check_count(mp, nrec); |
| 613 | if (error) |
| 614 | goto error; |
| 615 | } |
| 616 | |
| 617 | error = xfs_inobt_update(cur, nrec); |
| 618 | if (error) |
| 619 | goto error; |
| 620 | |
| 621 | out: |
| 622 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 623 | return 0; |
| 624 | error: |
| 625 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 626 | return error; |
| 627 | } |
| 628 | |
| 629 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | * Allocate new inodes in the allocation group specified by agbp. |
| 631 | * Return 0 for success, else error code. |
| 632 | */ |
| 633 | STATIC int /* error code or 0 */ |
| 634 | xfs_ialloc_ag_alloc( |
| 635 | xfs_trans_t *tp, /* transaction pointer */ |
| 636 | xfs_buf_t *agbp, /* alloc group buffer */ |
| 637 | int *alloc) |
| 638 | { |
| 639 | xfs_agi_t *agi; /* allocation group header */ |
| 640 | xfs_alloc_arg_t args; /* allocation argument structure */ |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 641 | xfs_agnumber_t agno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | xfs_agino_t newino; /* new first inode's number */ |
| 644 | xfs_agino_t newlen; /* new number of inodes */ |
Glen Overby | 3ccb8b5 | 2006-03-29 09:52:28 +1000 | [diff] [blame] | 645 | int isaligned = 0; /* inode allocation at stripe unit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | /* boundary */ |
Brian Foster | 56d1115 | 2015-05-29 09:18:32 +1000 | [diff] [blame] | 647 | uint16_t allocmask = (uint16_t) -1; /* init. to full chunk */ |
| 648 | struct xfs_inobt_rec_incore rec; |
Dave Chinner | 44b56e0 | 2010-01-11 11:47:43 +0000 | [diff] [blame] | 649 | struct xfs_perag *pag; |
Brian Foster | 1cdadee | 2015-05-29 09:19:29 +1000 | [diff] [blame] | 650 | int do_sparse = 0; |
| 651 | |
Mark Tinguely | a004168 | 2012-09-20 13:16:45 -0500 | [diff] [blame] | 652 | memset(&args, 0, sizeof(args)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | args.tp = tp; |
| 654 | args.mp = tp->t_mountp; |
Brian Foster | 1cdadee | 2015-05-29 09:19:29 +1000 | [diff] [blame] | 655 | args.fsbno = NULLFSBLOCK; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 656 | xfs_rmap_ag_owner(&args.oinfo, XFS_RMAP_OWN_INODES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | |
Brian Foster | 46fc58d | 2015-06-04 13:03:34 +1000 | [diff] [blame] | 658 | #ifdef DEBUG |
| 659 | /* randomly do sparse inode allocations */ |
| 660 | if (xfs_sb_version_hassparseinodes(&tp->t_mountp->m_sb) && |
| 661 | args.mp->m_ialloc_min_blks < args.mp->m_ialloc_blks) |
| 662 | do_sparse = prandom_u32() & 1; |
| 663 | #endif |
| 664 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | /* |
| 666 | * Locking will ensure that we don't have two callers in here |
| 667 | * at one time. |
| 668 | */ |
Jie Liu | 7178343 | 2013-12-13 15:51:46 +1100 | [diff] [blame] | 669 | newlen = args.mp->m_ialloc_inos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | if (args.mp->m_maxicount && |
George Wang | 74f9ce1 | 2015-05-29 07:39:34 +1000 | [diff] [blame] | 671 | percpu_counter_read_positive(&args.mp->m_icount) + newlen > |
Dave Chinner | 501ab32 | 2015-02-23 21:19:28 +1100 | [diff] [blame] | 672 | args.mp->m_maxicount) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 673 | return -ENOSPC; |
Jie Liu | 126cd10 | 2013-12-13 15:51:48 +1100 | [diff] [blame] | 674 | args.minlen = args.maxlen = args.mp->m_ialloc_blks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | /* |
Glen Overby | 3ccb8b5 | 2006-03-29 09:52:28 +1000 | [diff] [blame] | 676 | * First try to allocate inodes contiguous with the last-allocated |
| 677 | * chunk of inodes. If the filesystem is striped, this will fill |
| 678 | * an entire stripe unit with inodes. |
Dave Chinner | 28c8e41 | 2013-06-27 16:04:55 +1000 | [diff] [blame] | 679 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | agi = XFS_BUF_TO_AGI(agbp); |
Glen Overby | 3ccb8b5 | 2006-03-29 09:52:28 +1000 | [diff] [blame] | 681 | newino = be32_to_cpu(agi->agi_newino); |
Dave Chinner | 85c0b2a | 2009-08-31 20:56:51 -0300 | [diff] [blame] | 682 | agno = be32_to_cpu(agi->agi_seqno); |
Nathan Scott | 019ff2d | 2006-04-11 15:45:05 +1000 | [diff] [blame] | 683 | args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) + |
Jie Liu | 126cd10 | 2013-12-13 15:51:48 +1100 | [diff] [blame] | 684 | args.mp->m_ialloc_blks; |
Brian Foster | 1cdadee | 2015-05-29 09:19:29 +1000 | [diff] [blame] | 685 | if (do_sparse) |
| 686 | goto sparse_alloc; |
Nathan Scott | 019ff2d | 2006-04-11 15:45:05 +1000 | [diff] [blame] | 687 | if (likely(newino != NULLAGINO && |
| 688 | (args.agbno < be32_to_cpu(agi->agi_length)))) { |
Dave Chinner | 85c0b2a | 2009-08-31 20:56:51 -0300 | [diff] [blame] | 689 | args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno); |
Glen Overby | 3ccb8b5 | 2006-03-29 09:52:28 +1000 | [diff] [blame] | 690 | args.type = XFS_ALLOCTYPE_THIS_BNO; |
Glen Overby | 3ccb8b5 | 2006-03-29 09:52:28 +1000 | [diff] [blame] | 691 | args.prod = 1; |
David Chinner | 75de2a9 | 2008-03-27 18:00:38 +1100 | [diff] [blame] | 692 | |
Glen Overby | 3ccb8b5 | 2006-03-29 09:52:28 +1000 | [diff] [blame] | 693 | /* |
David Chinner | 75de2a9 | 2008-03-27 18:00:38 +1100 | [diff] [blame] | 694 | * We need to take into account alignment here to ensure that |
| 695 | * we don't modify the free list if we fail to have an exact |
| 696 | * block. If we don't have an exact match, and every oher |
| 697 | * attempt allocation attempt fails, we'll end up cancelling |
| 698 | * a dirty transaction and shutting down. |
| 699 | * |
| 700 | * For an exact allocation, alignment must be 1, |
| 701 | * however we need to take cluster alignment into account when |
| 702 | * fixing up the freelist. Use the minalignslop field to |
| 703 | * indicate that extra blocks might be required for alignment, |
| 704 | * but not to use them in the actual exact allocation. |
Glen Overby | 3ccb8b5 | 2006-03-29 09:52:28 +1000 | [diff] [blame] | 705 | */ |
David Chinner | 75de2a9 | 2008-03-27 18:00:38 +1100 | [diff] [blame] | 706 | args.alignment = 1; |
Dave Chinner | 7a1df15 | 2014-12-04 09:42:21 +1100 | [diff] [blame] | 707 | args.minalignslop = xfs_ialloc_cluster_alignment(args.mp) - 1; |
David Chinner | 75de2a9 | 2008-03-27 18:00:38 +1100 | [diff] [blame] | 708 | |
| 709 | /* Allow space for the inode btree to split. */ |
Christoph Hellwig | 0d87e65 | 2009-02-09 08:37:14 +0100 | [diff] [blame] | 710 | args.minleft = args.mp->m_in_maxlevels - 1; |
Glen Overby | 3ccb8b5 | 2006-03-29 09:52:28 +1000 | [diff] [blame] | 711 | if ((error = xfs_alloc_vextent(&args))) |
| 712 | return error; |
Brian Foster | e480a72 | 2014-03-07 16:19:14 +1100 | [diff] [blame] | 713 | |
| 714 | /* |
| 715 | * This request might have dirtied the transaction if the AG can |
| 716 | * satisfy the request, but the exact block was not available. |
| 717 | * If the allocation did fail, subsequent requests will relax |
| 718 | * the exact agbno requirement and increase the alignment |
| 719 | * instead. It is critical that the total size of the request |
| 720 | * (len + alignment + slop) does not increase from this point |
| 721 | * on, so reset minalignslop to ensure it is not included in |
| 722 | * subsequent requests. |
| 723 | */ |
| 724 | args.minalignslop = 0; |
Brian Foster | 1cdadee | 2015-05-29 09:19:29 +1000 | [diff] [blame] | 725 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | |
Glen Overby | 3ccb8b5 | 2006-03-29 09:52:28 +1000 | [diff] [blame] | 727 | if (unlikely(args.fsbno == NULLFSBLOCK)) { |
| 728 | /* |
| 729 | * Set the alignment for the allocation. |
| 730 | * If stripe alignment is turned on then align at stripe unit |
| 731 | * boundary. |
Nathan Scott | 019ff2d | 2006-04-11 15:45:05 +1000 | [diff] [blame] | 732 | * If the cluster size is smaller than a filesystem block |
| 733 | * then we're doing I/O for inodes in filesystem block size |
Glen Overby | 3ccb8b5 | 2006-03-29 09:52:28 +1000 | [diff] [blame] | 734 | * pieces, so don't need alignment anyway. |
| 735 | */ |
| 736 | isaligned = 0; |
| 737 | if (args.mp->m_sinoalign) { |
| 738 | ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN)); |
| 739 | args.alignment = args.mp->m_dalign; |
| 740 | isaligned = 1; |
David Chinner | 75de2a9 | 2008-03-27 18:00:38 +1100 | [diff] [blame] | 741 | } else |
Dave Chinner | 7a1df15 | 2014-12-04 09:42:21 +1100 | [diff] [blame] | 742 | args.alignment = xfs_ialloc_cluster_alignment(args.mp); |
Glen Overby | 3ccb8b5 | 2006-03-29 09:52:28 +1000 | [diff] [blame] | 743 | /* |
| 744 | * Need to figure out where to allocate the inode blocks. |
| 745 | * Ideally they should be spaced out through the a.g. |
| 746 | * For now, just allocate blocks up front. |
| 747 | */ |
| 748 | args.agbno = be32_to_cpu(agi->agi_root); |
Dave Chinner | 85c0b2a | 2009-08-31 20:56:51 -0300 | [diff] [blame] | 749 | args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno); |
Glen Overby | 3ccb8b5 | 2006-03-29 09:52:28 +1000 | [diff] [blame] | 750 | /* |
| 751 | * Allocate a fixed-size extent of inodes. |
| 752 | */ |
| 753 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
Glen Overby | 3ccb8b5 | 2006-03-29 09:52:28 +1000 | [diff] [blame] | 754 | args.prod = 1; |
| 755 | /* |
| 756 | * Allow space for the inode btree to split. |
| 757 | */ |
Christoph Hellwig | 0d87e65 | 2009-02-09 08:37:14 +0100 | [diff] [blame] | 758 | args.minleft = args.mp->m_in_maxlevels - 1; |
Glen Overby | 3ccb8b5 | 2006-03-29 09:52:28 +1000 | [diff] [blame] | 759 | if ((error = xfs_alloc_vextent(&args))) |
| 760 | return error; |
| 761 | } |
Nathan Scott | 019ff2d | 2006-04-11 15:45:05 +1000 | [diff] [blame] | 762 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | /* |
| 764 | * If stripe alignment is turned on, then try again with cluster |
| 765 | * alignment. |
| 766 | */ |
| 767 | if (isaligned && args.fsbno == NULLFSBLOCK) { |
| 768 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 769 | args.agbno = be32_to_cpu(agi->agi_root); |
Dave Chinner | 85c0b2a | 2009-08-31 20:56:51 -0300 | [diff] [blame] | 770 | args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno); |
Dave Chinner | 7a1df15 | 2014-12-04 09:42:21 +1100 | [diff] [blame] | 771 | args.alignment = xfs_ialloc_cluster_alignment(args.mp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | if ((error = xfs_alloc_vextent(&args))) |
| 773 | return error; |
| 774 | } |
| 775 | |
Brian Foster | 56d1115 | 2015-05-29 09:18:32 +1000 | [diff] [blame] | 776 | /* |
| 777 | * Finally, try a sparse allocation if the filesystem supports it and |
| 778 | * the sparse allocation length is smaller than a full chunk. |
| 779 | */ |
| 780 | if (xfs_sb_version_hassparseinodes(&args.mp->m_sb) && |
| 781 | args.mp->m_ialloc_min_blks < args.mp->m_ialloc_blks && |
| 782 | args.fsbno == NULLFSBLOCK) { |
Brian Foster | 1cdadee | 2015-05-29 09:19:29 +1000 | [diff] [blame] | 783 | sparse_alloc: |
Brian Foster | 56d1115 | 2015-05-29 09:18:32 +1000 | [diff] [blame] | 784 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 785 | args.agbno = be32_to_cpu(agi->agi_root); |
| 786 | args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno); |
| 787 | args.alignment = args.mp->m_sb.sb_spino_align; |
| 788 | args.prod = 1; |
| 789 | |
| 790 | args.minlen = args.mp->m_ialloc_min_blks; |
| 791 | args.maxlen = args.minlen; |
| 792 | |
| 793 | /* |
| 794 | * The inode record will be aligned to full chunk size. We must |
| 795 | * prevent sparse allocation from AG boundaries that result in |
| 796 | * invalid inode records, such as records that start at agbno 0 |
| 797 | * or extend beyond the AG. |
| 798 | * |
| 799 | * Set min agbno to the first aligned, non-zero agbno and max to |
| 800 | * the last aligned agbno that is at least one full chunk from |
| 801 | * the end of the AG. |
| 802 | */ |
| 803 | args.min_agbno = args.mp->m_sb.sb_inoalignmt; |
| 804 | args.max_agbno = round_down(args.mp->m_sb.sb_agblocks, |
| 805 | args.mp->m_sb.sb_inoalignmt) - |
| 806 | args.mp->m_ialloc_blks; |
| 807 | |
| 808 | error = xfs_alloc_vextent(&args); |
| 809 | if (error) |
| 810 | return error; |
| 811 | |
| 812 | newlen = args.len << args.mp->m_sb.sb_inopblog; |
Brian Foster | 46fc58d | 2015-06-04 13:03:34 +1000 | [diff] [blame] | 813 | ASSERT(newlen <= XFS_INODES_PER_CHUNK); |
Brian Foster | 56d1115 | 2015-05-29 09:18:32 +1000 | [diff] [blame] | 814 | allocmask = (1 << (newlen / XFS_INODES_PER_HOLEMASK_BIT)) - 1; |
| 815 | } |
| 816 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | if (args.fsbno == NULLFSBLOCK) { |
| 818 | *alloc = 0; |
| 819 | return 0; |
| 820 | } |
| 821 | ASSERT(args.len == args.minlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | |
David Chinner | 359346a | 2008-04-29 12:53:32 +1000 | [diff] [blame] | 823 | /* |
Dave Chinner | 85c0b2a | 2009-08-31 20:56:51 -0300 | [diff] [blame] | 824 | * Stamp and write the inode buffers. |
| 825 | * |
David Chinner | 359346a | 2008-04-29 12:53:32 +1000 | [diff] [blame] | 826 | * Seed the new inode cluster with a random generation number. This |
| 827 | * prevents short-term reuse of generation numbers if a chunk is |
| 828 | * freed and then immediately reallocated. We use random numbers |
| 829 | * rather than a linear progression to prevent the next generation |
| 830 | * number from being easily guessable. |
| 831 | */ |
Brian Foster | 463958a | 2015-05-29 09:05:49 +1000 | [diff] [blame] | 832 | error = xfs_ialloc_inode_init(args.mp, tp, NULL, newlen, agno, |
| 833 | args.agbno, args.len, prandom_u32()); |
Christoph Hellwig | d42f08f | 2008-11-28 14:23:38 +1100 | [diff] [blame] | 834 | |
Chandra Seetharaman | 2a30f36d | 2011-09-20 13:56:55 +0000 | [diff] [blame] | 835 | if (error) |
| 836 | return error; |
Dave Chinner | 85c0b2a | 2009-08-31 20:56:51 -0300 | [diff] [blame] | 837 | /* |
| 838 | * Convert the results. |
| 839 | */ |
| 840 | newino = XFS_OFFBNO_TO_AGINO(args.mp, args.agbno, 0); |
Brian Foster | 56d1115 | 2015-05-29 09:18:32 +1000 | [diff] [blame] | 841 | |
| 842 | if (xfs_inobt_issparse(~allocmask)) { |
| 843 | /* |
| 844 | * We've allocated a sparse chunk. Align the startino and mask. |
| 845 | */ |
| 846 | xfs_align_sparse_ino(args.mp, &newino, &allocmask); |
| 847 | |
| 848 | rec.ir_startino = newino; |
| 849 | rec.ir_holemask = ~allocmask; |
| 850 | rec.ir_count = newlen; |
| 851 | rec.ir_freecount = newlen; |
| 852 | rec.ir_free = XFS_INOBT_ALL_FREE; |
| 853 | |
| 854 | /* |
| 855 | * Insert the sparse record into the inobt and allow for a merge |
| 856 | * if necessary. If a merge does occur, rec is updated to the |
| 857 | * merged record. |
| 858 | */ |
| 859 | error = xfs_inobt_insert_sprec(args.mp, tp, agbp, XFS_BTNUM_INO, |
| 860 | &rec, true); |
| 861 | if (error == -EFSCORRUPTED) { |
| 862 | xfs_alert(args.mp, |
| 863 | "invalid sparse inode record: ino 0x%llx holemask 0x%x count %u", |
| 864 | XFS_AGINO_TO_INO(args.mp, agno, |
| 865 | rec.ir_startino), |
| 866 | rec.ir_holemask, rec.ir_count); |
| 867 | xfs_force_shutdown(args.mp, SHUTDOWN_CORRUPT_INCORE); |
| 868 | } |
| 869 | if (error) |
| 870 | return error; |
| 871 | |
| 872 | /* |
| 873 | * We can't merge the part we've just allocated as for the inobt |
| 874 | * due to finobt semantics. The original record may or may not |
| 875 | * exist independent of whether physical inodes exist in this |
| 876 | * sparse chunk. |
| 877 | * |
| 878 | * We must update the finobt record based on the inobt record. |
| 879 | * rec contains the fully merged and up to date inobt record |
| 880 | * from the previous call. Set merge false to replace any |
| 881 | * existing record with this one. |
| 882 | */ |
| 883 | if (xfs_sb_version_hasfinobt(&args.mp->m_sb)) { |
| 884 | error = xfs_inobt_insert_sprec(args.mp, tp, agbp, |
| 885 | XFS_BTNUM_FINO, &rec, |
| 886 | false); |
| 887 | if (error) |
| 888 | return error; |
| 889 | } |
| 890 | } else { |
| 891 | /* full chunk - insert new records to both btrees */ |
| 892 | error = xfs_inobt_insert(args.mp, tp, agbp, newino, newlen, |
| 893 | XFS_BTNUM_INO); |
| 894 | if (error) |
| 895 | return error; |
| 896 | |
| 897 | if (xfs_sb_version_hasfinobt(&args.mp->m_sb)) { |
| 898 | error = xfs_inobt_insert(args.mp, tp, agbp, newino, |
| 899 | newlen, XFS_BTNUM_FINO); |
| 900 | if (error) |
| 901 | return error; |
| 902 | } |
| 903 | } |
| 904 | |
| 905 | /* |
| 906 | * Update AGI counts and newino. |
| 907 | */ |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 908 | be32_add_cpu(&agi->agi_count, newlen); |
| 909 | be32_add_cpu(&agi->agi_freecount, newlen); |
Dave Chinner | 44b56e0 | 2010-01-11 11:47:43 +0000 | [diff] [blame] | 910 | pag = xfs_perag_get(args.mp, agno); |
| 911 | pag->pagi_freecount += newlen; |
| 912 | xfs_perag_put(pag); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 913 | agi->agi_newino = cpu_to_be32(newino); |
Dave Chinner | 85c0b2a | 2009-08-31 20:56:51 -0300 | [diff] [blame] | 914 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | * Log allocation group header fields |
| 917 | */ |
| 918 | xfs_ialloc_log_agi(tp, agbp, |
| 919 | XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO); |
| 920 | /* |
| 921 | * Modify/log superblock values for inode count and inode free count. |
| 922 | */ |
| 923 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen); |
| 924 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen); |
| 925 | *alloc = 1; |
| 926 | return 0; |
| 927 | } |
| 928 | |
Christoph Hellwig | b8f82a4 | 2009-11-14 16:17:22 +0000 | [diff] [blame] | 929 | STATIC xfs_agnumber_t |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | xfs_ialloc_next_ag( |
| 931 | xfs_mount_t *mp) |
| 932 | { |
| 933 | xfs_agnumber_t agno; |
| 934 | |
| 935 | spin_lock(&mp->m_agirotor_lock); |
| 936 | agno = mp->m_agirotor; |
Carlos Maiolino | 8aea3ff | 2012-09-20 10:32:36 -0300 | [diff] [blame] | 937 | if (++mp->m_agirotor >= mp->m_maxagi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | mp->m_agirotor = 0; |
| 939 | spin_unlock(&mp->m_agirotor_lock); |
| 940 | |
| 941 | return agno; |
| 942 | } |
| 943 | |
| 944 | /* |
| 945 | * 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] | 946 | * inode and the mode. Return the allocation group buffer. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | */ |
Christoph Hellwig | 55d6af6 | 2012-07-04 10:54:49 -0400 | [diff] [blame] | 948 | STATIC xfs_agnumber_t |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | xfs_ialloc_ag_select( |
| 950 | xfs_trans_t *tp, /* transaction pointer */ |
| 951 | xfs_ino_t parent, /* parent directory inode number */ |
Christoph Hellwig | f59cf5c | 2017-12-04 17:32:55 -0800 | [diff] [blame] | 952 | umode_t mode) /* bits set to indicate file type */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | xfs_agnumber_t agcount; /* number of ag's in the filesystem */ |
| 955 | xfs_agnumber_t agno; /* current ag number */ |
| 956 | int flags; /* alloc buffer locking flags */ |
| 957 | xfs_extlen_t ineed; /* blocks needed for inode allocation */ |
| 958 | xfs_extlen_t longest = 0; /* longest extent available */ |
| 959 | xfs_mount_t *mp; /* mount point structure */ |
| 960 | int needspace; /* file mode implies space allocated */ |
| 961 | xfs_perag_t *pag; /* per allocation group data */ |
| 962 | xfs_agnumber_t pagno; /* parent (starting) ag number */ |
Christoph Hellwig | 55d6af6 | 2012-07-04 10:54:49 -0400 | [diff] [blame] | 963 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | |
| 965 | /* |
| 966 | * Files of these types need at least one block if length > 0 |
| 967 | * (and they won't fit in the inode, but that's hard to figure out). |
| 968 | */ |
| 969 | needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode); |
| 970 | mp = tp->t_mountp; |
| 971 | agcount = mp->m_maxagi; |
| 972 | if (S_ISDIR(mode)) |
| 973 | pagno = xfs_ialloc_next_ag(mp); |
| 974 | else { |
| 975 | pagno = XFS_INO_TO_AGNO(mp, parent); |
| 976 | if (pagno >= agcount) |
| 977 | pagno = 0; |
| 978 | } |
Christoph Hellwig | 55d6af6 | 2012-07-04 10:54:49 -0400 | [diff] [blame] | 979 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | ASSERT(pagno < agcount); |
Christoph Hellwig | 55d6af6 | 2012-07-04 10:54:49 -0400 | [diff] [blame] | 981 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | /* |
| 983 | * Loop through allocation groups, looking for one with a little |
| 984 | * free space in it. Note we don't look for free inodes, exactly. |
| 985 | * Instead, we include whether there is a need to allocate inodes |
| 986 | * to mean that blocks must be allocated for them, |
| 987 | * if none are currently free. |
| 988 | */ |
| 989 | agno = pagno; |
| 990 | flags = XFS_ALLOC_FLAG_TRYLOCK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | for (;;) { |
Dave Chinner | 44b56e0 | 2010-01-11 11:47:43 +0000 | [diff] [blame] | 992 | pag = xfs_perag_get(mp, agno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | if (!pag->pagi_inodeok) { |
| 994 | xfs_ialloc_next_ag(mp); |
Christoph Hellwig | 55d6af6 | 2012-07-04 10:54:49 -0400 | [diff] [blame] | 995 | goto nextag; |
| 996 | } |
| 997 | |
| 998 | if (!pag->pagi_init) { |
| 999 | error = xfs_ialloc_pagi_init(mp, tp, agno); |
| 1000 | if (error) |
| 1001 | goto nextag; |
| 1002 | } |
| 1003 | |
| 1004 | if (pag->pagi_freecount) { |
| 1005 | xfs_perag_put(pag); |
| 1006 | return agno; |
| 1007 | } |
| 1008 | |
Christoph Hellwig | 55d6af6 | 2012-07-04 10:54:49 -0400 | [diff] [blame] | 1009 | if (!pag->pagf_init) { |
| 1010 | error = xfs_alloc_pagf_init(mp, tp, agno, flags); |
| 1011 | if (error) |
| 1012 | goto nextag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | /* |
Dave Chinner | 7a1df15 | 2014-12-04 09:42:21 +1100 | [diff] [blame] | 1016 | * Check that there is enough free space for the file plus a |
| 1017 | * chunk of inodes if we need to allocate some. If this is the |
| 1018 | * first pass across the AGs, take into account the potential |
| 1019 | * space needed for alignment of inode chunks when checking the |
| 1020 | * longest contiguous free space in the AG - this prevents us |
| 1021 | * from getting ENOSPC because we have free space larger than |
| 1022 | * m_ialloc_blks but alignment constraints prevent us from using |
| 1023 | * it. |
| 1024 | * |
| 1025 | * If we can't find an AG with space for full alignment slack to |
| 1026 | * be taken into account, we must be near ENOSPC in all AGs. |
| 1027 | * Hence we don't include alignment for the second pass and so |
| 1028 | * if we fail allocation due to alignment issues then it is most |
| 1029 | * likely a real ENOSPC condition. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | */ |
Brian Foster | 066a188 | 2015-05-29 08:55:20 +1000 | [diff] [blame] | 1031 | ineed = mp->m_ialloc_min_blks; |
Dave Chinner | 7a1df15 | 2014-12-04 09:42:21 +1100 | [diff] [blame] | 1032 | if (flags && ineed > 1) |
| 1033 | ineed += xfs_ialloc_cluster_alignment(mp); |
Christoph Hellwig | 55d6af6 | 2012-07-04 10:54:49 -0400 | [diff] [blame] | 1034 | longest = pag->pagf_longest; |
| 1035 | if (!longest) |
| 1036 | longest = pag->pagf_flcount > 0; |
| 1037 | |
| 1038 | if (pag->pagf_freeblks >= needspace + ineed && |
| 1039 | longest >= ineed) { |
| 1040 | xfs_perag_put(pag); |
| 1041 | return agno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | nextag: |
Dave Chinner | 44b56e0 | 2010-01-11 11:47:43 +0000 | [diff] [blame] | 1044 | xfs_perag_put(pag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | /* |
| 1046 | * No point in iterating over the rest, if we're shutting |
| 1047 | * down. |
| 1048 | */ |
Dave Chinner | 1c1c6eb | 2010-01-11 11:47:44 +0000 | [diff] [blame] | 1049 | if (XFS_FORCED_SHUTDOWN(mp)) |
Christoph Hellwig | 55d6af6 | 2012-07-04 10:54:49 -0400 | [diff] [blame] | 1050 | return NULLAGNUMBER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | agno++; |
| 1052 | if (agno >= agcount) |
| 1053 | agno = 0; |
| 1054 | if (agno == pagno) { |
Dave Chinner | 1c1c6eb | 2010-01-11 11:47:44 +0000 | [diff] [blame] | 1055 | if (flags == 0) |
Christoph Hellwig | 55d6af6 | 2012-07-04 10:54:49 -0400 | [diff] [blame] | 1056 | return NULLAGNUMBER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | flags = 0; |
| 1058 | } |
| 1059 | } |
| 1060 | } |
| 1061 | |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 1062 | /* |
| 1063 | * Try to retrieve the next record to the left/right from the current one. |
| 1064 | */ |
| 1065 | STATIC int |
| 1066 | xfs_ialloc_next_rec( |
| 1067 | struct xfs_btree_cur *cur, |
| 1068 | xfs_inobt_rec_incore_t *rec, |
| 1069 | int *done, |
| 1070 | int left) |
| 1071 | { |
| 1072 | int error; |
| 1073 | int i; |
| 1074 | |
| 1075 | if (left) |
| 1076 | error = xfs_btree_decrement(cur, 0, &i); |
| 1077 | else |
| 1078 | error = xfs_btree_increment(cur, 0, &i); |
| 1079 | |
| 1080 | if (error) |
| 1081 | return error; |
| 1082 | *done = !i; |
| 1083 | if (i) { |
| 1084 | error = xfs_inobt_get_rec(cur, rec, &i); |
| 1085 | if (error) |
| 1086 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 1087 | XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1); |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 1088 | } |
| 1089 | |
| 1090 | return 0; |
| 1091 | } |
| 1092 | |
Dave Chinner | bd16956 | 2009-08-31 20:58:28 -0300 | [diff] [blame] | 1093 | STATIC int |
| 1094 | xfs_ialloc_get_rec( |
| 1095 | struct xfs_btree_cur *cur, |
| 1096 | xfs_agino_t agino, |
| 1097 | xfs_inobt_rec_incore_t *rec, |
Jie Liu | 43df2ee | 2013-06-24 13:37:23 +0800 | [diff] [blame] | 1098 | int *done) |
Dave Chinner | bd16956 | 2009-08-31 20:58:28 -0300 | [diff] [blame] | 1099 | { |
| 1100 | int error; |
| 1101 | int i; |
| 1102 | |
| 1103 | error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_EQ, &i); |
| 1104 | if (error) |
| 1105 | return error; |
| 1106 | *done = !i; |
| 1107 | if (i) { |
| 1108 | error = xfs_inobt_get_rec(cur, rec, &i); |
| 1109 | if (error) |
| 1110 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 1111 | XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1); |
Dave Chinner | bd16956 | 2009-08-31 20:58:28 -0300 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | return 0; |
| 1115 | } |
Dave Chinner | 0b48db8 | 2009-08-31 20:57:09 -0300 | [diff] [blame] | 1116 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1117 | /* |
Brian Foster | 26dd521 | 2015-05-29 09:20:10 +1000 | [diff] [blame] | 1118 | * Return the offset of the first free inode in the record. If the inode chunk |
| 1119 | * is sparsely allocated, we convert the record holemask to inode granularity |
| 1120 | * and mask off the unallocated regions from the inode free mask. |
Brian Foster | d4cc540 | 2015-05-29 08:50:21 +1000 | [diff] [blame] | 1121 | */ |
| 1122 | STATIC int |
| 1123 | xfs_inobt_first_free_inode( |
| 1124 | struct xfs_inobt_rec_incore *rec) |
| 1125 | { |
Brian Foster | 26dd521 | 2015-05-29 09:20:10 +1000 | [diff] [blame] | 1126 | xfs_inofree_t realfree; |
| 1127 | |
| 1128 | /* if there are no holes, return the first available offset */ |
| 1129 | if (!xfs_inobt_issparse(rec->ir_holemask)) |
| 1130 | return xfs_lowbit64(rec->ir_free); |
| 1131 | |
| 1132 | realfree = xfs_inobt_irec_to_allocmask(rec); |
| 1133 | realfree &= rec->ir_free; |
| 1134 | |
| 1135 | return xfs_lowbit64(realfree); |
Brian Foster | d4cc540 | 2015-05-29 08:50:21 +1000 | [diff] [blame] | 1136 | } |
| 1137 | |
| 1138 | /* |
Brian Foster | 6dd8638 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 1139 | * Allocate an inode using the inobt-only algorithm. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | */ |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 1141 | STATIC int |
Brian Foster | 6dd8638 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 1142 | xfs_dialloc_ag_inobt( |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 1143 | struct xfs_trans *tp, |
| 1144 | struct xfs_buf *agbp, |
| 1145 | xfs_ino_t parent, |
| 1146 | xfs_ino_t *inop) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | { |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 1148 | struct xfs_mount *mp = tp->t_mountp; |
| 1149 | struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp); |
| 1150 | xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno); |
| 1151 | xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent); |
| 1152 | xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent); |
| 1153 | struct xfs_perag *pag; |
| 1154 | struct xfs_btree_cur *cur, *tcur; |
| 1155 | struct xfs_inobt_rec_incore rec, trec; |
| 1156 | xfs_ino_t ino; |
| 1157 | int error; |
| 1158 | int offset; |
| 1159 | int i, j; |
Carlos Maiolino | 2d32311 | 2017-08-18 18:07:04 -0700 | [diff] [blame] | 1160 | int searchdistance = 10; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | |
Dave Chinner | 44b56e0 | 2010-01-11 11:47:43 +0000 | [diff] [blame] | 1162 | pag = xfs_perag_get(mp, agno); |
Dave Chinner | bd16956 | 2009-08-31 20:58:28 -0300 | [diff] [blame] | 1163 | |
Christoph Hellwig | 4bb6106 | 2012-07-04 10:54:48 -0400 | [diff] [blame] | 1164 | ASSERT(pag->pagi_init); |
| 1165 | ASSERT(pag->pagi_inodeok); |
| 1166 | ASSERT(pag->pagi_freecount > 0); |
| 1167 | |
Dave Chinner | bd16956 | 2009-08-31 20:58:28 -0300 | [diff] [blame] | 1168 | restart_pagno: |
Brian Foster | 57bd3db | 2014-04-24 16:00:50 +1000 | [diff] [blame] | 1169 | cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | /* |
| 1171 | * If pagino is 0 (this is the root inode allocation) use newino. |
| 1172 | * This must work because we've just allocated some. |
| 1173 | */ |
| 1174 | if (!pagino) |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1175 | pagino = be32_to_cpu(agi->agi_newino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | |
Dave Chinner | 0b48db8 | 2009-08-31 20:57:09 -0300 | [diff] [blame] | 1177 | error = xfs_check_agi_freecount(cur, agi); |
| 1178 | if (error) |
| 1179 | goto error0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | /* |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 1182 | * 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] | 1183 | */ |
| 1184 | if (pagno == agno) { |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 1185 | int doneleft; /* done, to the left */ |
| 1186 | int doneright; /* done, to the right */ |
| 1187 | |
Christoph Hellwig | 2187550 | 2009-08-31 20:58:21 -0300 | [diff] [blame] | 1188 | error = xfs_inobt_lookup(cur, pagino, XFS_LOOKUP_LE, &i); |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 1189 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | goto error0; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1191 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0); |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 1192 | |
| 1193 | error = xfs_inobt_get_rec(cur, &rec, &j); |
| 1194 | if (error) |
| 1195 | goto error0; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1196 | XFS_WANT_CORRUPTED_GOTO(mp, j == 1, error0); |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 1197 | |
| 1198 | if (rec.ir_freecount > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | /* |
| 1200 | * Found a free inode in the same chunk |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 1201 | * as the parent, done. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | */ |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 1203 | goto alloc_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | } |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 1205 | |
| 1206 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | /* |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 1208 | * In the same AG as parent, but parent's chunk is full. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 1211 | /* duplicate the cursor, search left & right simultaneously */ |
| 1212 | error = xfs_btree_dup_cursor(cur, &tcur); |
| 1213 | if (error) |
| 1214 | goto error0; |
| 1215 | |
Dave Chinner | bd16956 | 2009-08-31 20:58:28 -0300 | [diff] [blame] | 1216 | /* |
| 1217 | * Skip to last blocks looked up if same parent inode. |
| 1218 | */ |
| 1219 | if (pagino != NULLAGINO && |
| 1220 | pag->pagl_pagino == pagino && |
| 1221 | pag->pagl_leftrec != NULLAGINO && |
| 1222 | pag->pagl_rightrec != NULLAGINO) { |
| 1223 | error = xfs_ialloc_get_rec(tcur, pag->pagl_leftrec, |
Jie Liu | 43df2ee | 2013-06-24 13:37:23 +0800 | [diff] [blame] | 1224 | &trec, &doneleft); |
Dave Chinner | bd16956 | 2009-08-31 20:58:28 -0300 | [diff] [blame] | 1225 | if (error) |
| 1226 | goto error1; |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 1227 | |
Dave Chinner | bd16956 | 2009-08-31 20:58:28 -0300 | [diff] [blame] | 1228 | error = xfs_ialloc_get_rec(cur, pag->pagl_rightrec, |
Jie Liu | 43df2ee | 2013-06-24 13:37:23 +0800 | [diff] [blame] | 1229 | &rec, &doneright); |
Dave Chinner | bd16956 | 2009-08-31 20:58:28 -0300 | [diff] [blame] | 1230 | if (error) |
| 1231 | goto error1; |
| 1232 | } else { |
| 1233 | /* search left with tcur, back up 1 record */ |
| 1234 | error = xfs_ialloc_next_rec(tcur, &trec, &doneleft, 1); |
| 1235 | if (error) |
| 1236 | goto error1; |
| 1237 | |
| 1238 | /* search right with cur, go forward 1 record. */ |
| 1239 | error = xfs_ialloc_next_rec(cur, &rec, &doneright, 0); |
| 1240 | if (error) |
| 1241 | goto error1; |
| 1242 | } |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 1243 | |
| 1244 | /* |
| 1245 | * Loop until we find an inode chunk with a free inode. |
| 1246 | */ |
Carlos Maiolino | 2d32311 | 2017-08-18 18:07:04 -0700 | [diff] [blame] | 1247 | while (--searchdistance > 0 && (!doneleft || !doneright)) { |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 1248 | int useleft; /* using left inode chunk this time */ |
| 1249 | |
| 1250 | /* figure out the closer block if both are valid. */ |
| 1251 | if (!doneleft && !doneright) { |
| 1252 | useleft = pagino - |
| 1253 | (trec.ir_startino + XFS_INODES_PER_CHUNK - 1) < |
| 1254 | rec.ir_startino - pagino; |
| 1255 | } else { |
| 1256 | useleft = !doneleft; |
| 1257 | } |
| 1258 | |
| 1259 | /* free inodes to the left? */ |
| 1260 | if (useleft && trec.ir_freecount) { |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 1261 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 1262 | cur = tcur; |
Dave Chinner | bd16956 | 2009-08-31 20:58:28 -0300 | [diff] [blame] | 1263 | |
| 1264 | pag->pagl_leftrec = trec.ir_startino; |
| 1265 | pag->pagl_rightrec = rec.ir_startino; |
| 1266 | pag->pagl_pagino = pagino; |
Omar Sandoval | c44245b | 2017-08-11 09:00:06 -0700 | [diff] [blame] | 1267 | rec = trec; |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 1268 | goto alloc_inode; |
| 1269 | } |
| 1270 | |
| 1271 | /* free inodes to the right? */ |
| 1272 | if (!useleft && rec.ir_freecount) { |
| 1273 | xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); |
Dave Chinner | bd16956 | 2009-08-31 20:58:28 -0300 | [diff] [blame] | 1274 | |
| 1275 | pag->pagl_leftrec = trec.ir_startino; |
| 1276 | pag->pagl_rightrec = rec.ir_startino; |
| 1277 | pag->pagl_pagino = pagino; |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 1278 | goto alloc_inode; |
| 1279 | } |
| 1280 | |
| 1281 | /* get next record to check */ |
| 1282 | if (useleft) { |
| 1283 | error = xfs_ialloc_next_rec(tcur, &trec, |
| 1284 | &doneleft, 1); |
| 1285 | } else { |
| 1286 | error = xfs_ialloc_next_rec(cur, &rec, |
| 1287 | &doneright, 0); |
| 1288 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | goto error1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | } |
Dave Chinner | bd16956 | 2009-08-31 20:58:28 -0300 | [diff] [blame] | 1292 | |
Carlos Maiolino | 2d32311 | 2017-08-18 18:07:04 -0700 | [diff] [blame] | 1293 | if (searchdistance <= 0) { |
| 1294 | /* |
| 1295 | * Not in range - save last search |
| 1296 | * location and allocate a new inode |
| 1297 | */ |
| 1298 | xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); |
| 1299 | pag->pagl_leftrec = trec.ir_startino; |
| 1300 | pag->pagl_rightrec = rec.ir_startino; |
| 1301 | pag->pagl_pagino = pagino; |
| 1302 | |
| 1303 | } else { |
| 1304 | /* |
| 1305 | * We've reached the end of the btree. because |
| 1306 | * we are only searching a small chunk of the |
| 1307 | * btree each search, there is obviously free |
| 1308 | * inodes closer to the parent inode than we |
| 1309 | * are now. restart the search again. |
| 1310 | */ |
| 1311 | pag->pagl_pagino = NULLAGINO; |
| 1312 | pag->pagl_leftrec = NULLAGINO; |
| 1313 | pag->pagl_rightrec = NULLAGINO; |
| 1314 | xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); |
| 1315 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 1316 | goto restart_pagno; |
| 1317 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | } |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 1319 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1320 | /* |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 1321 | * In a different AG from the parent. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | * See if the most recently allocated block has any free. |
| 1323 | */ |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 1324 | if (agi->agi_newino != cpu_to_be32(NULLAGINO)) { |
Christoph Hellwig | 2187550 | 2009-08-31 20:58:21 -0300 | [diff] [blame] | 1325 | error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino), |
| 1326 | XFS_LOOKUP_EQ, &i); |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 1327 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1328 | goto error0; |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 1329 | |
| 1330 | if (i == 1) { |
| 1331 | error = xfs_inobt_get_rec(cur, &rec, &j); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | if (error) |
| 1333 | goto error0; |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 1334 | |
| 1335 | if (j == 1 && rec.ir_freecount > 0) { |
| 1336 | /* |
| 1337 | * The last chunk allocated in the group |
| 1338 | * still has a free inode. |
| 1339 | */ |
| 1340 | goto alloc_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 | } |
| 1342 | } |
Dave Chinner | bd16956 | 2009-08-31 20:58:28 -0300 | [diff] [blame] | 1343 | } |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 1344 | |
Dave Chinner | bd16956 | 2009-08-31 20:58:28 -0300 | [diff] [blame] | 1345 | /* |
| 1346 | * None left in the last group, search the whole AG |
| 1347 | */ |
| 1348 | error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i); |
| 1349 | if (error) |
| 1350 | goto error0; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1351 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0); |
Dave Chinner | bd16956 | 2009-08-31 20:58:28 -0300 | [diff] [blame] | 1352 | |
| 1353 | for (;;) { |
| 1354 | error = xfs_inobt_get_rec(cur, &rec, &i); |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 1355 | if (error) |
| 1356 | goto error0; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1357 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0); |
Dave Chinner | bd16956 | 2009-08-31 20:58:28 -0300 | [diff] [blame] | 1358 | if (rec.ir_freecount > 0) |
| 1359 | break; |
| 1360 | error = xfs_btree_increment(cur, 0, &i); |
| 1361 | if (error) |
| 1362 | goto error0; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1363 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | } |
Christoph Hellwig | 4254b0b | 2009-08-31 20:57:14 -0300 | [diff] [blame] | 1365 | |
| 1366 | alloc_inode: |
Brian Foster | d4cc540 | 2015-05-29 08:50:21 +1000 | [diff] [blame] | 1367 | offset = xfs_inobt_first_free_inode(&rec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | ASSERT(offset >= 0); |
| 1369 | ASSERT(offset < XFS_INODES_PER_CHUNK); |
| 1370 | ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) % |
| 1371 | XFS_INODES_PER_CHUNK) == 0); |
| 1372 | ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset); |
Christoph Hellwig | 0d87e65 | 2009-02-09 08:37:14 +0100 | [diff] [blame] | 1373 | rec.ir_free &= ~XFS_INOBT_MASK(offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | rec.ir_freecount--; |
Christoph Hellwig | afabc24 | 2009-08-31 20:57:03 -0300 | [diff] [blame] | 1375 | error = xfs_inobt_update(cur, &rec); |
| 1376 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1377 | goto error0; |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 1378 | be32_add_cpu(&agi->agi_freecount, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT); |
Dave Chinner | 44b56e0 | 2010-01-11 11:47:43 +0000 | [diff] [blame] | 1380 | pag->pagi_freecount--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | |
Dave Chinner | 0b48db8 | 2009-08-31 20:57:09 -0300 | [diff] [blame] | 1382 | error = xfs_check_agi_freecount(cur, agi); |
| 1383 | if (error) |
| 1384 | goto error0; |
| 1385 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 1387 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1); |
Dave Chinner | 44b56e0 | 2010-01-11 11:47:43 +0000 | [diff] [blame] | 1388 | xfs_perag_put(pag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | *inop = ino; |
| 1390 | return 0; |
| 1391 | error1: |
| 1392 | xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR); |
| 1393 | error0: |
| 1394 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
Dave Chinner | 44b56e0 | 2010-01-11 11:47:43 +0000 | [diff] [blame] | 1395 | xfs_perag_put(pag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | return error; |
| 1397 | } |
| 1398 | |
| 1399 | /* |
Brian Foster | 6dd8638 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 1400 | * Use the free inode btree to allocate an inode based on distance from the |
| 1401 | * parent. Note that the provided cursor may be deleted and replaced. |
| 1402 | */ |
| 1403 | STATIC int |
| 1404 | xfs_dialloc_ag_finobt_near( |
| 1405 | xfs_agino_t pagino, |
| 1406 | struct xfs_btree_cur **ocur, |
| 1407 | struct xfs_inobt_rec_incore *rec) |
| 1408 | { |
| 1409 | struct xfs_btree_cur *lcur = *ocur; /* left search cursor */ |
| 1410 | struct xfs_btree_cur *rcur; /* right search cursor */ |
| 1411 | struct xfs_inobt_rec_incore rrec; |
| 1412 | int error; |
| 1413 | int i, j; |
| 1414 | |
| 1415 | error = xfs_inobt_lookup(lcur, pagino, XFS_LOOKUP_LE, &i); |
| 1416 | if (error) |
| 1417 | return error; |
| 1418 | |
| 1419 | if (i == 1) { |
| 1420 | error = xfs_inobt_get_rec(lcur, rec, &i); |
| 1421 | if (error) |
| 1422 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 1423 | XFS_WANT_CORRUPTED_RETURN(lcur->bc_mp, i == 1); |
Brian Foster | 6dd8638 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 1424 | |
| 1425 | /* |
| 1426 | * See if we've landed in the parent inode record. The finobt |
| 1427 | * only tracks chunks with at least one free inode, so record |
| 1428 | * existence is enough. |
| 1429 | */ |
| 1430 | if (pagino >= rec->ir_startino && |
| 1431 | pagino < (rec->ir_startino + XFS_INODES_PER_CHUNK)) |
| 1432 | return 0; |
| 1433 | } |
| 1434 | |
| 1435 | error = xfs_btree_dup_cursor(lcur, &rcur); |
| 1436 | if (error) |
| 1437 | return error; |
| 1438 | |
| 1439 | error = xfs_inobt_lookup(rcur, pagino, XFS_LOOKUP_GE, &j); |
| 1440 | if (error) |
| 1441 | goto error_rcur; |
| 1442 | if (j == 1) { |
| 1443 | error = xfs_inobt_get_rec(rcur, &rrec, &j); |
| 1444 | if (error) |
| 1445 | goto error_rcur; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1446 | XFS_WANT_CORRUPTED_GOTO(lcur->bc_mp, j == 1, error_rcur); |
Brian Foster | 6dd8638 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 1447 | } |
| 1448 | |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1449 | 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] | 1450 | if (i == 1 && j == 1) { |
| 1451 | /* |
| 1452 | * Both the left and right records are valid. Choose the closer |
| 1453 | * inode chunk to the target. |
| 1454 | */ |
| 1455 | if ((pagino - rec->ir_startino + XFS_INODES_PER_CHUNK - 1) > |
| 1456 | (rrec.ir_startino - pagino)) { |
| 1457 | *rec = rrec; |
| 1458 | xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR); |
| 1459 | *ocur = rcur; |
| 1460 | } else { |
| 1461 | xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR); |
| 1462 | } |
| 1463 | } else if (j == 1) { |
| 1464 | /* only the right record is valid */ |
| 1465 | *rec = rrec; |
| 1466 | xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR); |
| 1467 | *ocur = rcur; |
| 1468 | } else if (i == 1) { |
| 1469 | /* only the left record is valid */ |
| 1470 | xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR); |
| 1471 | } |
| 1472 | |
| 1473 | return 0; |
| 1474 | |
| 1475 | error_rcur: |
| 1476 | xfs_btree_del_cursor(rcur, XFS_BTREE_ERROR); |
| 1477 | return error; |
| 1478 | } |
| 1479 | |
| 1480 | /* |
| 1481 | * Use the free inode btree to find a free inode based on a newino hint. If |
| 1482 | * the hint is NULL, find the first free inode in the AG. |
| 1483 | */ |
| 1484 | STATIC int |
| 1485 | xfs_dialloc_ag_finobt_newino( |
| 1486 | struct xfs_agi *agi, |
| 1487 | struct xfs_btree_cur *cur, |
| 1488 | struct xfs_inobt_rec_incore *rec) |
| 1489 | { |
| 1490 | int error; |
| 1491 | int i; |
| 1492 | |
| 1493 | if (agi->agi_newino != cpu_to_be32(NULLAGINO)) { |
Dave Chinner | e68ed77 | 2014-09-29 10:43:15 +1000 | [diff] [blame] | 1494 | error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino), |
| 1495 | XFS_LOOKUP_EQ, &i); |
Brian Foster | 6dd8638 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 1496 | if (error) |
| 1497 | return error; |
| 1498 | if (i == 1) { |
| 1499 | error = xfs_inobt_get_rec(cur, rec, &i); |
| 1500 | if (error) |
| 1501 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 1502 | XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1); |
Brian Foster | 6dd8638 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 1503 | return 0; |
| 1504 | } |
| 1505 | } |
| 1506 | |
| 1507 | /* |
| 1508 | * Find the first inode available in the AG. |
| 1509 | */ |
| 1510 | error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i); |
| 1511 | if (error) |
| 1512 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 1513 | XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1); |
Brian Foster | 6dd8638 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 1514 | |
| 1515 | error = xfs_inobt_get_rec(cur, rec, &i); |
| 1516 | if (error) |
| 1517 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 1518 | XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1); |
Brian Foster | 6dd8638 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 1519 | |
| 1520 | return 0; |
| 1521 | } |
| 1522 | |
| 1523 | /* |
| 1524 | * Update the inobt based on a modification made to the finobt. Also ensure that |
| 1525 | * the records from both trees are equivalent post-modification. |
| 1526 | */ |
| 1527 | STATIC int |
| 1528 | xfs_dialloc_ag_update_inobt( |
| 1529 | struct xfs_btree_cur *cur, /* inobt cursor */ |
| 1530 | struct xfs_inobt_rec_incore *frec, /* finobt record */ |
| 1531 | int offset) /* inode offset */ |
| 1532 | { |
| 1533 | struct xfs_inobt_rec_incore rec; |
| 1534 | int error; |
| 1535 | int i; |
| 1536 | |
| 1537 | error = xfs_inobt_lookup(cur, frec->ir_startino, XFS_LOOKUP_EQ, &i); |
| 1538 | if (error) |
| 1539 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 1540 | XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1); |
Brian Foster | 6dd8638 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 1541 | |
| 1542 | error = xfs_inobt_get_rec(cur, &rec, &i); |
| 1543 | if (error) |
| 1544 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 1545 | XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1); |
Brian Foster | 6dd8638 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 1546 | ASSERT((XFS_AGINO_TO_OFFSET(cur->bc_mp, rec.ir_startino) % |
| 1547 | XFS_INODES_PER_CHUNK) == 0); |
| 1548 | |
| 1549 | rec.ir_free &= ~XFS_INOBT_MASK(offset); |
| 1550 | rec.ir_freecount--; |
| 1551 | |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 1552 | 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] | 1553 | (rec.ir_freecount == frec->ir_freecount)); |
| 1554 | |
kbuild test robot | b72091f | 2014-12-01 08:24:58 +1100 | [diff] [blame] | 1555 | return xfs_inobt_update(cur, &rec); |
Brian Foster | 6dd8638 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 1556 | } |
| 1557 | |
| 1558 | /* |
| 1559 | * Allocate an inode using the free inode btree, if available. Otherwise, fall |
| 1560 | * back to the inobt search algorithm. |
| 1561 | * |
| 1562 | * The caller selected an AG for us, and made sure that free inodes are |
| 1563 | * available. |
| 1564 | */ |
| 1565 | STATIC int |
| 1566 | xfs_dialloc_ag( |
| 1567 | struct xfs_trans *tp, |
| 1568 | struct xfs_buf *agbp, |
| 1569 | xfs_ino_t parent, |
| 1570 | xfs_ino_t *inop) |
| 1571 | { |
| 1572 | struct xfs_mount *mp = tp->t_mountp; |
| 1573 | struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp); |
| 1574 | xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno); |
| 1575 | xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent); |
| 1576 | xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent); |
| 1577 | struct xfs_perag *pag; |
| 1578 | struct xfs_btree_cur *cur; /* finobt cursor */ |
| 1579 | struct xfs_btree_cur *icur; /* inobt cursor */ |
| 1580 | struct xfs_inobt_rec_incore rec; |
| 1581 | xfs_ino_t ino; |
| 1582 | int error; |
| 1583 | int offset; |
| 1584 | int i; |
| 1585 | |
| 1586 | if (!xfs_sb_version_hasfinobt(&mp->m_sb)) |
| 1587 | return xfs_dialloc_ag_inobt(tp, agbp, parent, inop); |
| 1588 | |
| 1589 | pag = xfs_perag_get(mp, agno); |
| 1590 | |
| 1591 | /* |
| 1592 | * If pagino is 0 (this is the root inode allocation) use newino. |
| 1593 | * This must work because we've just allocated some. |
| 1594 | */ |
| 1595 | if (!pagino) |
| 1596 | pagino = be32_to_cpu(agi->agi_newino); |
| 1597 | |
| 1598 | cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_FINO); |
| 1599 | |
| 1600 | error = xfs_check_agi_freecount(cur, agi); |
| 1601 | if (error) |
| 1602 | goto error_cur; |
| 1603 | |
| 1604 | /* |
| 1605 | * The search algorithm depends on whether we're in the same AG as the |
| 1606 | * parent. If so, find the closest available inode to the parent. If |
| 1607 | * not, consider the agi hint or find the first free inode in the AG. |
| 1608 | */ |
| 1609 | if (agno == pagno) |
| 1610 | error = xfs_dialloc_ag_finobt_near(pagino, &cur, &rec); |
| 1611 | else |
| 1612 | error = xfs_dialloc_ag_finobt_newino(agi, cur, &rec); |
| 1613 | if (error) |
| 1614 | goto error_cur; |
| 1615 | |
Brian Foster | d4cc540 | 2015-05-29 08:50:21 +1000 | [diff] [blame] | 1616 | offset = xfs_inobt_first_free_inode(&rec); |
Brian Foster | 6dd8638 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 1617 | ASSERT(offset >= 0); |
| 1618 | ASSERT(offset < XFS_INODES_PER_CHUNK); |
| 1619 | ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) % |
| 1620 | XFS_INODES_PER_CHUNK) == 0); |
| 1621 | ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset); |
| 1622 | |
| 1623 | /* |
| 1624 | * Modify or remove the finobt record. |
| 1625 | */ |
| 1626 | rec.ir_free &= ~XFS_INOBT_MASK(offset); |
| 1627 | rec.ir_freecount--; |
| 1628 | if (rec.ir_freecount) |
| 1629 | error = xfs_inobt_update(cur, &rec); |
| 1630 | else |
| 1631 | error = xfs_btree_delete(cur, &i); |
| 1632 | if (error) |
| 1633 | goto error_cur; |
| 1634 | |
| 1635 | /* |
| 1636 | * The finobt has now been updated appropriately. We haven't updated the |
| 1637 | * agi and superblock yet, so we can create an inobt cursor and validate |
| 1638 | * the original freecount. If all is well, make the equivalent update to |
| 1639 | * the inobt using the finobt record and offset information. |
| 1640 | */ |
| 1641 | icur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO); |
| 1642 | |
| 1643 | error = xfs_check_agi_freecount(icur, agi); |
| 1644 | if (error) |
| 1645 | goto error_icur; |
| 1646 | |
| 1647 | error = xfs_dialloc_ag_update_inobt(icur, &rec, offset); |
| 1648 | if (error) |
| 1649 | goto error_icur; |
| 1650 | |
| 1651 | /* |
| 1652 | * Both trees have now been updated. We must update the perag and |
| 1653 | * superblock before we can check the freecount for each btree. |
| 1654 | */ |
| 1655 | be32_add_cpu(&agi->agi_freecount, -1); |
| 1656 | xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT); |
| 1657 | pag->pagi_freecount--; |
| 1658 | |
| 1659 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1); |
| 1660 | |
| 1661 | error = xfs_check_agi_freecount(icur, agi); |
| 1662 | if (error) |
| 1663 | goto error_icur; |
| 1664 | error = xfs_check_agi_freecount(cur, agi); |
| 1665 | if (error) |
| 1666 | goto error_icur; |
| 1667 | |
| 1668 | xfs_btree_del_cursor(icur, XFS_BTREE_NOERROR); |
| 1669 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 1670 | xfs_perag_put(pag); |
| 1671 | *inop = ino; |
| 1672 | return 0; |
| 1673 | |
| 1674 | error_icur: |
| 1675 | xfs_btree_del_cursor(icur, XFS_BTREE_ERROR); |
| 1676 | error_cur: |
| 1677 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 1678 | xfs_perag_put(pag); |
| 1679 | return error; |
| 1680 | } |
| 1681 | |
| 1682 | /* |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 1683 | * Allocate an inode on disk. |
| 1684 | * |
| 1685 | * Mode is used to tell whether the new inode will need space, and whether it |
| 1686 | * is a directory. |
| 1687 | * |
| 1688 | * This function is designed to be called twice if it has to do an allocation |
| 1689 | * to make more free inodes. On the first call, *IO_agbp should be set to NULL. |
| 1690 | * 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] | 1691 | * number is returned. In this case, *IO_agbp is set to NULL. If an allocation |
| 1692 | * needs to be done, xfs_dialloc returns the current AGI buffer in *IO_agbp. |
| 1693 | * The caller should then commit the current transaction, allocate a |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 1694 | * new transaction, and call xfs_dialloc() again, passing in the previous value |
| 1695 | * of *IO_agbp. IO_agbp should be held across the transactions. Since the AGI |
| 1696 | * buffer is locked across the two calls, the second call is guaranteed to have |
| 1697 | * a free inode available. |
| 1698 | * |
| 1699 | * Once we successfully pick an inode its number is returned and the on-disk |
| 1700 | * data structures are updated. The inode itself is not read in, since doing so |
| 1701 | * would break ordering constraints with xfs_reclaim. |
| 1702 | */ |
| 1703 | int |
| 1704 | xfs_dialloc( |
| 1705 | struct xfs_trans *tp, |
| 1706 | xfs_ino_t parent, |
| 1707 | umode_t mode, |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 1708 | struct xfs_buf **IO_agbp, |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 1709 | xfs_ino_t *inop) |
| 1710 | { |
Christoph Hellwig | 55d6af6 | 2012-07-04 10:54:49 -0400 | [diff] [blame] | 1711 | struct xfs_mount *mp = tp->t_mountp; |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 1712 | struct xfs_buf *agbp; |
| 1713 | xfs_agnumber_t agno; |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 1714 | int error; |
| 1715 | int ialloced; |
| 1716 | int noroom = 0; |
Christoph Hellwig | be60fe5 | 2012-07-04 10:54:50 -0400 | [diff] [blame] | 1717 | xfs_agnumber_t start_agno; |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 1718 | struct xfs_perag *pag; |
Christoph Hellwig | f59cf5c | 2017-12-04 17:32:55 -0800 | [diff] [blame] | 1719 | int okalloc = 1; |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 1720 | |
Christoph Hellwig | 4bb6106 | 2012-07-04 10:54:48 -0400 | [diff] [blame] | 1721 | if (*IO_agbp) { |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 1722 | /* |
Christoph Hellwig | 4bb6106 | 2012-07-04 10:54:48 -0400 | [diff] [blame] | 1723 | * If the caller passes in a pointer to the AGI buffer, |
| 1724 | * continue where we left off before. In this case, we |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 1725 | * know that the allocation group has free inodes. |
| 1726 | */ |
| 1727 | agbp = *IO_agbp; |
Christoph Hellwig | 4bb6106 | 2012-07-04 10:54:48 -0400 | [diff] [blame] | 1728 | goto out_alloc; |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 1729 | } |
Christoph Hellwig | 4bb6106 | 2012-07-04 10:54:48 -0400 | [diff] [blame] | 1730 | |
| 1731 | /* |
| 1732 | * We do not have an agbp, so select an initial allocation |
| 1733 | * group for inode allocation. |
| 1734 | */ |
Christoph Hellwig | f59cf5c | 2017-12-04 17:32:55 -0800 | [diff] [blame] | 1735 | start_agno = xfs_ialloc_ag_select(tp, parent, mode); |
Christoph Hellwig | be60fe5 | 2012-07-04 10:54:50 -0400 | [diff] [blame] | 1736 | if (start_agno == NULLAGNUMBER) { |
Christoph Hellwig | 4bb6106 | 2012-07-04 10:54:48 -0400 | [diff] [blame] | 1737 | *inop = NULLFSINO; |
| 1738 | return 0; |
| 1739 | } |
Christoph Hellwig | 55d6af6 | 2012-07-04 10:54:49 -0400 | [diff] [blame] | 1740 | |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 1741 | /* |
| 1742 | * If we have already hit the ceiling of inode blocks then clear |
| 1743 | * okalloc so we scan all available agi structures for a free |
| 1744 | * inode. |
George Wang | 74f9ce1 | 2015-05-29 07:39:34 +1000 | [diff] [blame] | 1745 | * |
| 1746 | * Read rough value of mp->m_icount by percpu_counter_read_positive, |
| 1747 | * which will sacrifice the preciseness but improve the performance. |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 1748 | */ |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 1749 | if (mp->m_maxicount && |
George Wang | 74f9ce1 | 2015-05-29 07:39:34 +1000 | [diff] [blame] | 1750 | percpu_counter_read_positive(&mp->m_icount) + mp->m_ialloc_inos |
| 1751 | > mp->m_maxicount) { |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 1752 | noroom = 1; |
| 1753 | okalloc = 0; |
| 1754 | } |
| 1755 | |
| 1756 | /* |
| 1757 | * Loop until we find an allocation group that either has free inodes |
| 1758 | * or in which we can allocate some inodes. Iterate through the |
| 1759 | * allocation groups upward, wrapping at the end. |
| 1760 | */ |
Christoph Hellwig | be60fe5 | 2012-07-04 10:54:50 -0400 | [diff] [blame] | 1761 | agno = start_agno; |
| 1762 | for (;;) { |
| 1763 | pag = xfs_perag_get(mp, agno); |
| 1764 | if (!pag->pagi_inodeok) { |
| 1765 | xfs_ialloc_next_ag(mp); |
| 1766 | goto nextag; |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 1767 | } |
Christoph Hellwig | be60fe5 | 2012-07-04 10:54:50 -0400 | [diff] [blame] | 1768 | |
| 1769 | if (!pag->pagi_init) { |
| 1770 | error = xfs_ialloc_pagi_init(mp, tp, agno); |
| 1771 | if (error) |
| 1772 | goto out_error; |
| 1773 | } |
| 1774 | |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 1775 | /* |
Christoph Hellwig | be60fe5 | 2012-07-04 10:54:50 -0400 | [diff] [blame] | 1776 | * Do a first racy fast path check if this AG is usable. |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 1777 | */ |
Christoph Hellwig | be60fe5 | 2012-07-04 10:54:50 -0400 | [diff] [blame] | 1778 | if (!pag->pagi_freecount && !okalloc) |
| 1779 | goto nextag; |
| 1780 | |
Christoph Hellwig | c498211 | 2012-08-07 02:02:02 -0400 | [diff] [blame] | 1781 | /* |
| 1782 | * Then read in the AGI buffer and recheck with the AGI buffer |
| 1783 | * lock held. |
| 1784 | */ |
Christoph Hellwig | be60fe5 | 2012-07-04 10:54:50 -0400 | [diff] [blame] | 1785 | error = xfs_ialloc_read_agi(mp, tp, agno, &agbp); |
| 1786 | if (error) |
| 1787 | goto out_error; |
| 1788 | |
Christoph Hellwig | be60fe5 | 2012-07-04 10:54:50 -0400 | [diff] [blame] | 1789 | if (pag->pagi_freecount) { |
| 1790 | xfs_perag_put(pag); |
| 1791 | goto out_alloc; |
| 1792 | } |
| 1793 | |
Christoph Hellwig | c498211 | 2012-08-07 02:02:02 -0400 | [diff] [blame] | 1794 | if (!okalloc) |
| 1795 | goto nextag_relse_buffer; |
| 1796 | |
Christoph Hellwig | be60fe5 | 2012-07-04 10:54:50 -0400 | [diff] [blame] | 1797 | |
| 1798 | error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced); |
| 1799 | if (error) { |
| 1800 | xfs_trans_brelse(tp, agbp); |
| 1801 | |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1802 | if (error != -ENOSPC) |
Christoph Hellwig | be60fe5 | 2012-07-04 10:54:50 -0400 | [diff] [blame] | 1803 | goto out_error; |
| 1804 | |
| 1805 | xfs_perag_put(pag); |
| 1806 | *inop = NULLFSINO; |
| 1807 | return 0; |
| 1808 | } |
| 1809 | |
| 1810 | if (ialloced) { |
| 1811 | /* |
| 1812 | * We successfully allocated some inodes, return |
| 1813 | * the current context to the caller so that it |
| 1814 | * can commit the current transaction and call |
| 1815 | * us again where we left off. |
| 1816 | */ |
| 1817 | ASSERT(pag->pagi_freecount > 0); |
| 1818 | xfs_perag_put(pag); |
| 1819 | |
| 1820 | *IO_agbp = agbp; |
| 1821 | *inop = NULLFSINO; |
| 1822 | return 0; |
| 1823 | } |
| 1824 | |
Christoph Hellwig | c498211 | 2012-08-07 02:02:02 -0400 | [diff] [blame] | 1825 | nextag_relse_buffer: |
| 1826 | xfs_trans_brelse(tp, agbp); |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 1827 | nextag: |
Christoph Hellwig | be60fe5 | 2012-07-04 10:54:50 -0400 | [diff] [blame] | 1828 | xfs_perag_put(pag); |
| 1829 | if (++agno == mp->m_sb.sb_agcount) |
| 1830 | agno = 0; |
| 1831 | if (agno == start_agno) { |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 1832 | *inop = NULLFSINO; |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1833 | return noroom ? -ENOSPC : 0; |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 1834 | } |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 1835 | } |
| 1836 | |
Christoph Hellwig | 4bb6106 | 2012-07-04 10:54:48 -0400 | [diff] [blame] | 1837 | out_alloc: |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 1838 | *IO_agbp = NULL; |
| 1839 | return xfs_dialloc_ag(tp, agbp, parent, inop); |
Christoph Hellwig | be60fe5 | 2012-07-04 10:54:50 -0400 | [diff] [blame] | 1840 | out_error: |
| 1841 | xfs_perag_put(pag); |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 1842 | return error; |
Christoph Hellwig | f2ecc5e | 2012-07-04 10:54:46 -0400 | [diff] [blame] | 1843 | } |
| 1844 | |
Brian Foster | 10ae3dc7 | 2015-05-29 09:22:52 +1000 | [diff] [blame] | 1845 | /* |
| 1846 | * Free the blocks of an inode chunk. We must consider that the inode chunk |
| 1847 | * might be sparse and only free the regions that are allocated as part of the |
| 1848 | * chunk. |
| 1849 | */ |
| 1850 | STATIC void |
| 1851 | xfs_difree_inode_chunk( |
| 1852 | struct xfs_mount *mp, |
| 1853 | xfs_agnumber_t agno, |
| 1854 | struct xfs_inobt_rec_incore *rec, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1855 | struct xfs_defer_ops *dfops) |
Brian Foster | 10ae3dc7 | 2015-05-29 09:22:52 +1000 | [diff] [blame] | 1856 | { |
| 1857 | xfs_agblock_t sagbno = XFS_AGINO_TO_AGBNO(mp, rec->ir_startino); |
| 1858 | int startidx, endidx; |
| 1859 | int nextbit; |
| 1860 | xfs_agblock_t agbno; |
| 1861 | int contigblk; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 1862 | struct xfs_owner_info oinfo; |
Brian Foster | 10ae3dc7 | 2015-05-29 09:22:52 +1000 | [diff] [blame] | 1863 | DECLARE_BITMAP(holemask, XFS_INOBT_HOLEMASK_BITS); |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 1864 | xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_INODES); |
Brian Foster | 10ae3dc7 | 2015-05-29 09:22:52 +1000 | [diff] [blame] | 1865 | |
| 1866 | if (!xfs_inobt_issparse(rec->ir_holemask)) { |
| 1867 | /* not sparse, calculate extent info directly */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1868 | xfs_bmap_add_free(mp, dfops, XFS_AGB_TO_FSB(mp, agno, sagbno), |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 1869 | mp->m_ialloc_blks, &oinfo); |
Brian Foster | 10ae3dc7 | 2015-05-29 09:22:52 +1000 | [diff] [blame] | 1870 | return; |
| 1871 | } |
| 1872 | |
| 1873 | /* holemask is only 16-bits (fits in an unsigned long) */ |
| 1874 | ASSERT(sizeof(rec->ir_holemask) <= sizeof(holemask[0])); |
| 1875 | holemask[0] = rec->ir_holemask; |
| 1876 | |
| 1877 | /* |
| 1878 | * Find contiguous ranges of zeroes (i.e., allocated regions) in the |
| 1879 | * holemask and convert the start/end index of each range to an extent. |
| 1880 | * We start with the start and end index both pointing at the first 0 in |
| 1881 | * the mask. |
| 1882 | */ |
| 1883 | startidx = endidx = find_first_zero_bit(holemask, |
| 1884 | XFS_INOBT_HOLEMASK_BITS); |
| 1885 | nextbit = startidx + 1; |
| 1886 | while (startidx < XFS_INOBT_HOLEMASK_BITS) { |
| 1887 | nextbit = find_next_zero_bit(holemask, XFS_INOBT_HOLEMASK_BITS, |
| 1888 | nextbit); |
| 1889 | /* |
| 1890 | * If the next zero bit is contiguous, update the end index of |
| 1891 | * the current range and continue. |
| 1892 | */ |
| 1893 | if (nextbit != XFS_INOBT_HOLEMASK_BITS && |
| 1894 | nextbit == endidx + 1) { |
| 1895 | endidx = nextbit; |
| 1896 | goto next; |
| 1897 | } |
| 1898 | |
| 1899 | /* |
| 1900 | * nextbit is not contiguous with the current end index. Convert |
| 1901 | * the current start/end to an extent and add it to the free |
| 1902 | * list. |
| 1903 | */ |
| 1904 | agbno = sagbno + (startidx * XFS_INODES_PER_HOLEMASK_BIT) / |
| 1905 | mp->m_sb.sb_inopblock; |
| 1906 | contigblk = ((endidx - startidx + 1) * |
| 1907 | XFS_INODES_PER_HOLEMASK_BIT) / |
| 1908 | mp->m_sb.sb_inopblock; |
| 1909 | |
| 1910 | ASSERT(agbno % mp->m_sb.sb_spino_align == 0); |
| 1911 | ASSERT(contigblk % mp->m_sb.sb_spino_align == 0); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1912 | xfs_bmap_add_free(mp, dfops, XFS_AGB_TO_FSB(mp, agno, agbno), |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 1913 | contigblk, &oinfo); |
Brian Foster | 10ae3dc7 | 2015-05-29 09:22:52 +1000 | [diff] [blame] | 1914 | |
| 1915 | /* reset range to current bit and carry on... */ |
| 1916 | startidx = endidx = nextbit; |
| 1917 | |
| 1918 | next: |
| 1919 | nextbit++; |
| 1920 | } |
| 1921 | } |
| 1922 | |
Brian Foster | 2b64ee5 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 1923 | STATIC int |
| 1924 | xfs_difree_inobt( |
| 1925 | struct xfs_mount *mp, |
| 1926 | struct xfs_trans *tp, |
| 1927 | struct xfs_buf *agbp, |
| 1928 | xfs_agino_t agino, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1929 | struct xfs_defer_ops *dfops, |
Brian Foster | 09b5660 | 2015-05-29 09:26:03 +1000 | [diff] [blame] | 1930 | struct xfs_icluster *xic, |
Brian Foster | 2b64ee5 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 1931 | struct xfs_inobt_rec_incore *orec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1932 | { |
Brian Foster | 2b64ee5 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 1933 | struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp); |
| 1934 | xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno); |
| 1935 | struct xfs_perag *pag; |
| 1936 | struct xfs_btree_cur *cur; |
| 1937 | struct xfs_inobt_rec_incore rec; |
| 1938 | int ilen; |
| 1939 | int error; |
| 1940 | int i; |
| 1941 | int off; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1942 | |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 1943 | ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC)); |
Brian Foster | 2b64ee5 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 1944 | ASSERT(XFS_AGINO_TO_AGBNO(mp, agino) < be32_to_cpu(agi->agi_length)); |
| 1945 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1946 | /* |
| 1947 | * Initialize the cursor. |
| 1948 | */ |
Brian Foster | 57bd3db | 2014-04-24 16:00:50 +1000 | [diff] [blame] | 1949 | cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1950 | |
Dave Chinner | 0b48db8 | 2009-08-31 20:57:09 -0300 | [diff] [blame] | 1951 | error = xfs_check_agi_freecount(cur, agi); |
| 1952 | if (error) |
| 1953 | goto error0; |
| 1954 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1955 | /* |
| 1956 | * Look for the entry describing this inode. |
| 1957 | */ |
Christoph Hellwig | 2187550 | 2009-08-31 20:58:21 -0300 | [diff] [blame] | 1958 | if ((error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i))) { |
Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 1959 | xfs_warn(mp, "%s: xfs_inobt_lookup() returned error %d.", |
| 1960 | __func__, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1961 | goto error0; |
| 1962 | } |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1963 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0); |
Christoph Hellwig | 2e287a7 | 2009-08-31 20:56:58 -0300 | [diff] [blame] | 1964 | error = xfs_inobt_get_rec(cur, &rec, &i); |
| 1965 | if (error) { |
Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 1966 | xfs_warn(mp, "%s: xfs_inobt_get_rec() returned error %d.", |
| 1967 | __func__, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1968 | goto error0; |
| 1969 | } |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1970 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1971 | /* |
| 1972 | * Get the offset in the inode chunk. |
| 1973 | */ |
| 1974 | off = agino - rec.ir_startino; |
| 1975 | ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK); |
Christoph Hellwig | 0d87e65 | 2009-02-09 08:37:14 +0100 | [diff] [blame] | 1976 | ASSERT(!(rec.ir_free & XFS_INOBT_MASK(off))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1977 | /* |
| 1978 | * Mark the inode free & increment the count. |
| 1979 | */ |
Christoph Hellwig | 0d87e65 | 2009-02-09 08:37:14 +0100 | [diff] [blame] | 1980 | rec.ir_free |= XFS_INOBT_MASK(off); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1981 | rec.ir_freecount++; |
| 1982 | |
| 1983 | /* |
Brian Foster | 999633d | 2015-05-29 08:51:37 +1000 | [diff] [blame] | 1984 | * When an inode chunk is free, it becomes eligible for removal. Don't |
| 1985 | * remove the chunk if the block size is large enough for multiple inode |
| 1986 | * chunks (that might not be free). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1987 | */ |
Josef Jeff Sipek | 1bd960e | 2008-02-29 13:58:40 +1100 | [diff] [blame] | 1988 | if (!(mp->m_flags & XFS_MOUNT_IKEEP) && |
Brian Foster | 999633d | 2015-05-29 08:51:37 +1000 | [diff] [blame] | 1989 | rec.ir_free == XFS_INOBT_ALL_FREE && |
| 1990 | mp->m_sb.sb_inopblock <= XFS_INODES_PER_CHUNK) { |
Thomas Meyer | 749f24f | 2017-10-09 11:38:54 -0700 | [diff] [blame] | 1991 | xic->deleted = true; |
Brian Foster | 09b5660 | 2015-05-29 09:26:03 +1000 | [diff] [blame] | 1992 | xic->first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino); |
| 1993 | xic->alloc = xfs_inobt_irec_to_allocmask(&rec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1994 | |
| 1995 | /* |
| 1996 | * Remove the inode cluster from the AGI B+Tree, adjust the |
| 1997 | * AGI and Superblock inode counts, and mark the disk space |
| 1998 | * to be freed when the transaction is committed. |
| 1999 | */ |
Brian Foster | 999633d | 2015-05-29 08:51:37 +1000 | [diff] [blame] | 2000 | ilen = rec.ir_freecount; |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 2001 | be32_add_cpu(&agi->agi_count, -ilen); |
| 2002 | be32_add_cpu(&agi->agi_freecount, -(ilen - 1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2003 | xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT); |
Dave Chinner | 44b56e0 | 2010-01-11 11:47:43 +0000 | [diff] [blame] | 2004 | pag = xfs_perag_get(mp, agno); |
| 2005 | pag->pagi_freecount -= ilen - 1; |
| 2006 | xfs_perag_put(pag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2007 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen); |
| 2008 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1)); |
| 2009 | |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2010 | if ((error = xfs_btree_delete(cur, &i))) { |
Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 2011 | xfs_warn(mp, "%s: xfs_btree_delete returned error %d.", |
| 2012 | __func__, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2013 | goto error0; |
| 2014 | } |
| 2015 | |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 2016 | xfs_difree_inode_chunk(mp, agno, &rec, dfops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2017 | } else { |
Thomas Meyer | 749f24f | 2017-10-09 11:38:54 -0700 | [diff] [blame] | 2018 | xic->deleted = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2019 | |
Christoph Hellwig | afabc24 | 2009-08-31 20:57:03 -0300 | [diff] [blame] | 2020 | error = xfs_inobt_update(cur, &rec); |
| 2021 | if (error) { |
Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 2022 | xfs_warn(mp, "%s: xfs_inobt_update returned error %d.", |
| 2023 | __func__, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2024 | goto error0; |
| 2025 | } |
Christoph Hellwig | afabc24 | 2009-08-31 20:57:03 -0300 | [diff] [blame] | 2026 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2027 | /* |
| 2028 | * Change the inode free counts and log the ag/sb changes. |
| 2029 | */ |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 2030 | be32_add_cpu(&agi->agi_freecount, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2031 | xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT); |
Dave Chinner | 44b56e0 | 2010-01-11 11:47:43 +0000 | [diff] [blame] | 2032 | pag = xfs_perag_get(mp, agno); |
| 2033 | pag->pagi_freecount++; |
| 2034 | xfs_perag_put(pag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2035 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1); |
| 2036 | } |
| 2037 | |
Dave Chinner | 0b48db8 | 2009-08-31 20:57:09 -0300 | [diff] [blame] | 2038 | error = xfs_check_agi_freecount(cur, agi); |
| 2039 | if (error) |
| 2040 | goto error0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2041 | |
Brian Foster | 2b64ee5 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 2042 | *orec = rec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2043 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 2044 | return 0; |
| 2045 | |
| 2046 | error0: |
| 2047 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 2048 | return error; |
| 2049 | } |
| 2050 | |
Brian Foster | 2b64ee5 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 2051 | /* |
Brian Foster | 3efa4ff | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 2052 | * Free an inode in the free inode btree. |
| 2053 | */ |
| 2054 | STATIC int |
| 2055 | xfs_difree_finobt( |
| 2056 | struct xfs_mount *mp, |
| 2057 | struct xfs_trans *tp, |
| 2058 | struct xfs_buf *agbp, |
| 2059 | xfs_agino_t agino, |
| 2060 | struct xfs_inobt_rec_incore *ibtrec) /* inobt record */ |
| 2061 | { |
| 2062 | struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp); |
| 2063 | xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno); |
| 2064 | struct xfs_btree_cur *cur; |
| 2065 | struct xfs_inobt_rec_incore rec; |
| 2066 | int offset = agino - ibtrec->ir_startino; |
| 2067 | int error; |
| 2068 | int i; |
| 2069 | |
| 2070 | cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_FINO); |
| 2071 | |
| 2072 | error = xfs_inobt_lookup(cur, ibtrec->ir_startino, XFS_LOOKUP_EQ, &i); |
| 2073 | if (error) |
| 2074 | goto error; |
| 2075 | if (i == 0) { |
| 2076 | /* |
| 2077 | * If the record does not exist in the finobt, we must have just |
| 2078 | * freed an inode in a previously fully allocated chunk. If not, |
| 2079 | * something is out of sync. |
| 2080 | */ |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2081 | XFS_WANT_CORRUPTED_GOTO(mp, ibtrec->ir_freecount == 1, error); |
Brian Foster | 3efa4ff | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 2082 | |
Brian Foster | 5419040 | 2015-05-29 09:03:04 +1000 | [diff] [blame] | 2083 | error = xfs_inobt_insert_rec(cur, ibtrec->ir_holemask, |
| 2084 | ibtrec->ir_count, |
| 2085 | ibtrec->ir_freecount, |
Brian Foster | 3efa4ff | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 2086 | ibtrec->ir_free, &i); |
| 2087 | if (error) |
| 2088 | goto error; |
| 2089 | ASSERT(i == 1); |
| 2090 | |
| 2091 | goto out; |
| 2092 | } |
| 2093 | |
| 2094 | /* |
| 2095 | * Read and update the existing record. We could just copy the ibtrec |
| 2096 | * across here, but that would defeat the purpose of having redundant |
| 2097 | * metadata. By making the modifications independently, we can catch |
| 2098 | * corruptions that we wouldn't see if we just copied from one record |
| 2099 | * to another. |
| 2100 | */ |
| 2101 | error = xfs_inobt_get_rec(cur, &rec, &i); |
| 2102 | if (error) |
| 2103 | goto error; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2104 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error); |
Brian Foster | 3efa4ff | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 2105 | |
| 2106 | rec.ir_free |= XFS_INOBT_MASK(offset); |
| 2107 | rec.ir_freecount++; |
| 2108 | |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2109 | XFS_WANT_CORRUPTED_GOTO(mp, (rec.ir_free == ibtrec->ir_free) && |
Brian Foster | 3efa4ff | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 2110 | (rec.ir_freecount == ibtrec->ir_freecount), |
| 2111 | error); |
| 2112 | |
| 2113 | /* |
| 2114 | * The content of inobt records should always match between the inobt |
| 2115 | * and finobt. The lifecycle of records in the finobt is different from |
| 2116 | * the inobt in that the finobt only tracks records with at least one |
| 2117 | * free inode. Hence, if all of the inodes are free and we aren't |
| 2118 | * keeping inode chunks permanently on disk, remove the record. |
| 2119 | * Otherwise, update the record with the new information. |
Brian Foster | 999633d | 2015-05-29 08:51:37 +1000 | [diff] [blame] | 2120 | * |
| 2121 | * Note that we currently can't free chunks when the block size is large |
| 2122 | * enough for multiple chunks. Leave the finobt record to remain in sync |
| 2123 | * with the inobt. |
Brian Foster | 3efa4ff | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 2124 | */ |
Brian Foster | 999633d | 2015-05-29 08:51:37 +1000 | [diff] [blame] | 2125 | if (rec.ir_free == XFS_INOBT_ALL_FREE && |
| 2126 | mp->m_sb.sb_inopblock <= XFS_INODES_PER_CHUNK && |
Brian Foster | 3efa4ff | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 2127 | !(mp->m_flags & XFS_MOUNT_IKEEP)) { |
| 2128 | error = xfs_btree_delete(cur, &i); |
| 2129 | if (error) |
| 2130 | goto error; |
| 2131 | ASSERT(i == 1); |
| 2132 | } else { |
| 2133 | error = xfs_inobt_update(cur, &rec); |
| 2134 | if (error) |
| 2135 | goto error; |
| 2136 | } |
| 2137 | |
| 2138 | out: |
| 2139 | error = xfs_check_agi_freecount(cur, agi); |
| 2140 | if (error) |
| 2141 | goto error; |
| 2142 | |
| 2143 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 2144 | return 0; |
| 2145 | |
| 2146 | error: |
| 2147 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 2148 | return error; |
| 2149 | } |
| 2150 | |
| 2151 | /* |
Brian Foster | 2b64ee5 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 2152 | * Free disk inode. Carefully avoids touching the incore inode, all |
| 2153 | * manipulations incore are the caller's responsibility. |
| 2154 | * The on-disk inode is not changed by this operation, only the |
| 2155 | * btree (free inode mask) is changed. |
| 2156 | */ |
| 2157 | int |
| 2158 | xfs_difree( |
| 2159 | struct xfs_trans *tp, /* transaction pointer */ |
| 2160 | xfs_ino_t inode, /* inode to be freed */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 2161 | struct xfs_defer_ops *dfops, /* extents to free */ |
Brian Foster | 09b5660 | 2015-05-29 09:26:03 +1000 | [diff] [blame] | 2162 | struct xfs_icluster *xic) /* cluster info if deleted */ |
Brian Foster | 2b64ee5 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 2163 | { |
| 2164 | /* REFERENCED */ |
| 2165 | xfs_agblock_t agbno; /* block number containing inode */ |
| 2166 | struct xfs_buf *agbp; /* buffer for allocation group header */ |
| 2167 | xfs_agino_t agino; /* allocation group inode number */ |
| 2168 | xfs_agnumber_t agno; /* allocation group number */ |
| 2169 | int error; /* error return value */ |
| 2170 | struct xfs_mount *mp; /* mount structure for filesystem */ |
| 2171 | struct xfs_inobt_rec_incore rec;/* btree record */ |
| 2172 | |
| 2173 | mp = tp->t_mountp; |
| 2174 | |
| 2175 | /* |
| 2176 | * Break up inode number into its components. |
| 2177 | */ |
| 2178 | agno = XFS_INO_TO_AGNO(mp, inode); |
| 2179 | if (agno >= mp->m_sb.sb_agcount) { |
| 2180 | xfs_warn(mp, "%s: agno >= mp->m_sb.sb_agcount (%d >= %d).", |
| 2181 | __func__, agno, mp->m_sb.sb_agcount); |
| 2182 | ASSERT(0); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 2183 | return -EINVAL; |
Brian Foster | 2b64ee5 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 2184 | } |
| 2185 | agino = XFS_INO_TO_AGINO(mp, inode); |
| 2186 | if (inode != XFS_AGINO_TO_INO(mp, agno, agino)) { |
| 2187 | xfs_warn(mp, "%s: inode != XFS_AGINO_TO_INO() (%llu != %llu).", |
| 2188 | __func__, (unsigned long long)inode, |
| 2189 | (unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino)); |
| 2190 | ASSERT(0); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 2191 | return -EINVAL; |
Brian Foster | 2b64ee5 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 2192 | } |
| 2193 | agbno = XFS_AGINO_TO_AGBNO(mp, agino); |
| 2194 | if (agbno >= mp->m_sb.sb_agblocks) { |
| 2195 | xfs_warn(mp, "%s: agbno >= mp->m_sb.sb_agblocks (%d >= %d).", |
| 2196 | __func__, agbno, mp->m_sb.sb_agblocks); |
| 2197 | ASSERT(0); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 2198 | return -EINVAL; |
Brian Foster | 2b64ee5 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 2199 | } |
| 2200 | /* |
| 2201 | * Get the allocation group header. |
| 2202 | */ |
| 2203 | error = xfs_ialloc_read_agi(mp, tp, agno, &agbp); |
| 2204 | if (error) { |
| 2205 | xfs_warn(mp, "%s: xfs_ialloc_read_agi() returned error %d.", |
| 2206 | __func__, error); |
| 2207 | return error; |
| 2208 | } |
| 2209 | |
| 2210 | /* |
| 2211 | * Fix up the inode allocation btree. |
| 2212 | */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 2213 | error = xfs_difree_inobt(mp, tp, agbp, agino, dfops, xic, &rec); |
Brian Foster | 2b64ee5 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 2214 | if (error) |
| 2215 | goto error0; |
| 2216 | |
Brian Foster | 3efa4ff | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 2217 | /* |
| 2218 | * Fix up the free inode btree. |
| 2219 | */ |
| 2220 | if (xfs_sb_version_hasfinobt(&mp->m_sb)) { |
| 2221 | error = xfs_difree_finobt(mp, tp, agbp, agino, &rec); |
| 2222 | if (error) |
| 2223 | goto error0; |
| 2224 | } |
| 2225 | |
Brian Foster | 2b64ee5 | 2014-04-24 16:00:53 +1000 | [diff] [blame] | 2226 | return 0; |
| 2227 | |
| 2228 | error0: |
| 2229 | return error; |
| 2230 | } |
| 2231 | |
Dave Chinner | 7124fe0 | 2010-06-24 11:15:33 +1000 | [diff] [blame] | 2232 | STATIC int |
| 2233 | xfs_imap_lookup( |
| 2234 | struct xfs_mount *mp, |
| 2235 | struct xfs_trans *tp, |
| 2236 | xfs_agnumber_t agno, |
| 2237 | xfs_agino_t agino, |
| 2238 | xfs_agblock_t agbno, |
| 2239 | xfs_agblock_t *chunk_agbno, |
| 2240 | xfs_agblock_t *offset_agbno, |
| 2241 | int flags) |
| 2242 | { |
| 2243 | struct xfs_inobt_rec_incore rec; |
| 2244 | struct xfs_btree_cur *cur; |
| 2245 | struct xfs_buf *agbp; |
Dave Chinner | 7124fe0 | 2010-06-24 11:15:33 +1000 | [diff] [blame] | 2246 | int error; |
| 2247 | int i; |
| 2248 | |
| 2249 | error = xfs_ialloc_read_agi(mp, tp, agno, &agbp); |
| 2250 | if (error) { |
Dave Chinner | 5348778 | 2011-03-07 10:05:35 +1100 | [diff] [blame] | 2251 | xfs_alert(mp, |
| 2252 | "%s: xfs_ialloc_read_agi() returned error %d, agno %d", |
| 2253 | __func__, error, agno); |
Dave Chinner | 7124fe0 | 2010-06-24 11:15:33 +1000 | [diff] [blame] | 2254 | return error; |
| 2255 | } |
| 2256 | |
| 2257 | /* |
Dave Chinner | 4536f2a | 2010-08-24 11:42:30 +1000 | [diff] [blame] | 2258 | * Lookup the inode record for the given agino. If the record cannot be |
| 2259 | * found, then it's an invalid inode number and we should abort. Once |
| 2260 | * we have a record, we need to ensure it contains the inode number |
| 2261 | * we are looking up. |
Dave Chinner | 7124fe0 | 2010-06-24 11:15:33 +1000 | [diff] [blame] | 2262 | */ |
Brian Foster | 57bd3db | 2014-04-24 16:00:50 +1000 | [diff] [blame] | 2263 | cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO); |
Dave Chinner | 4536f2a | 2010-08-24 11:42:30 +1000 | [diff] [blame] | 2264 | error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i); |
Dave Chinner | 7124fe0 | 2010-06-24 11:15:33 +1000 | [diff] [blame] | 2265 | if (!error) { |
| 2266 | if (i) |
| 2267 | error = xfs_inobt_get_rec(cur, &rec, &i); |
| 2268 | if (!error && i == 0) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 2269 | error = -EINVAL; |
Dave Chinner | 7124fe0 | 2010-06-24 11:15:33 +1000 | [diff] [blame] | 2270 | } |
| 2271 | |
| 2272 | xfs_trans_brelse(tp, agbp); |
Brian Foster | f307080 | 2015-08-19 10:00:53 +1000 | [diff] [blame] | 2273 | xfs_btree_del_cursor(cur, error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
Dave Chinner | 7124fe0 | 2010-06-24 11:15:33 +1000 | [diff] [blame] | 2274 | if (error) |
| 2275 | return error; |
| 2276 | |
Dave Chinner | 4536f2a | 2010-08-24 11:42:30 +1000 | [diff] [blame] | 2277 | /* check that the returned record contains the required inode */ |
| 2278 | if (rec.ir_startino > agino || |
Jie Liu | 7178343 | 2013-12-13 15:51:46 +1100 | [diff] [blame] | 2279 | rec.ir_startino + mp->m_ialloc_inos <= agino) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 2280 | return -EINVAL; |
Dave Chinner | 4536f2a | 2010-08-24 11:42:30 +1000 | [diff] [blame] | 2281 | |
Dave Chinner | 7124fe0 | 2010-06-24 11:15:33 +1000 | [diff] [blame] | 2282 | /* for untrusted inodes check it is allocated first */ |
Dave Chinner | 1920779 | 2010-06-24 11:15:47 +1000 | [diff] [blame] | 2283 | if ((flags & XFS_IGET_UNTRUSTED) && |
Dave Chinner | 7124fe0 | 2010-06-24 11:15:33 +1000 | [diff] [blame] | 2284 | (rec.ir_free & XFS_INOBT_MASK(agino - rec.ir_startino))) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 2285 | return -EINVAL; |
Dave Chinner | 7124fe0 | 2010-06-24 11:15:33 +1000 | [diff] [blame] | 2286 | |
| 2287 | *chunk_agbno = XFS_AGINO_TO_AGBNO(mp, rec.ir_startino); |
| 2288 | *offset_agbno = agbno - *chunk_agbno; |
| 2289 | return 0; |
| 2290 | } |
| 2291 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2292 | /* |
Christoph Hellwig | 94e1b69 | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 2293 | * 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] | 2294 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2295 | int |
Christoph Hellwig | 94e1b69 | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 2296 | xfs_imap( |
| 2297 | xfs_mount_t *mp, /* file system mount structure */ |
| 2298 | xfs_trans_t *tp, /* transaction pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2299 | xfs_ino_t ino, /* inode to locate */ |
Christoph Hellwig | 94e1b69 | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 2300 | struct xfs_imap *imap, /* location map structure */ |
| 2301 | uint flags) /* flags for inode btree lookup */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2302 | { |
| 2303 | xfs_agblock_t agbno; /* block number of inode in the alloc group */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2304 | xfs_agino_t agino; /* inode number within alloc group */ |
| 2305 | xfs_agnumber_t agno; /* allocation group number */ |
| 2306 | int blks_per_cluster; /* num blocks per inode cluster */ |
| 2307 | xfs_agblock_t chunk_agbno; /* first block in inode chunk */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2308 | xfs_agblock_t cluster_agbno; /* first block in inode cluster */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2309 | int error; /* error code */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2310 | int offset; /* index of inode in its buffer */ |
Dave Chinner | 836a94a | 2013-08-12 20:49:44 +1000 | [diff] [blame] | 2311 | xfs_agblock_t offset_agbno; /* blks from chunk start to inode */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2312 | |
| 2313 | ASSERT(ino != NULLFSINO); |
Christoph Hellwig | 94e1b69 | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 2314 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2315 | /* |
| 2316 | * Split up the inode number into its parts. |
| 2317 | */ |
| 2318 | agno = XFS_INO_TO_AGNO(mp, ino); |
| 2319 | agino = XFS_INO_TO_AGINO(mp, ino); |
| 2320 | agbno = XFS_AGINO_TO_AGBNO(mp, agino); |
| 2321 | if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks || |
| 2322 | ino != XFS_AGINO_TO_INO(mp, agno, agino)) { |
| 2323 | #ifdef DEBUG |
Dave Chinner | 1920779 | 2010-06-24 11:15:47 +1000 | [diff] [blame] | 2324 | /* |
| 2325 | * Don't output diagnostic information for untrusted inodes |
| 2326 | * as they can be invalid without implying corruption. |
| 2327 | */ |
| 2328 | if (flags & XFS_IGET_UNTRUSTED) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 2329 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2330 | if (agno >= mp->m_sb.sb_agcount) { |
Dave Chinner | 5348778 | 2011-03-07 10:05:35 +1100 | [diff] [blame] | 2331 | xfs_alert(mp, |
| 2332 | "%s: agno (%d) >= mp->m_sb.sb_agcount (%d)", |
| 2333 | __func__, agno, mp->m_sb.sb_agcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2334 | } |
| 2335 | if (agbno >= mp->m_sb.sb_agblocks) { |
Dave Chinner | 5348778 | 2011-03-07 10:05:35 +1100 | [diff] [blame] | 2336 | xfs_alert(mp, |
| 2337 | "%s: agbno (0x%llx) >= mp->m_sb.sb_agblocks (0x%lx)", |
| 2338 | __func__, (unsigned long long)agbno, |
| 2339 | (unsigned long)mp->m_sb.sb_agblocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2340 | } |
| 2341 | if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) { |
Dave Chinner | 5348778 | 2011-03-07 10:05:35 +1100 | [diff] [blame] | 2342 | xfs_alert(mp, |
| 2343 | "%s: ino (0x%llx) != XFS_AGINO_TO_INO() (0x%llx)", |
| 2344 | __func__, ino, |
| 2345 | XFS_AGINO_TO_INO(mp, agno, agino)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2346 | } |
Nathan Scott | 745b1f47 | 2006-09-28 11:02:23 +1000 | [diff] [blame] | 2347 | xfs_stack_trace(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2348 | #endif /* DEBUG */ |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 2349 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2350 | } |
Christoph Hellwig | 94e1b69 | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 2351 | |
Jie Liu | f9e5abc | 2013-12-13 15:51:49 +1100 | [diff] [blame] | 2352 | blks_per_cluster = xfs_icluster_size_fsb(mp); |
Dave Chinner | 7124fe0 | 2010-06-24 11:15:33 +1000 | [diff] [blame] | 2353 | |
| 2354 | /* |
| 2355 | * For bulkstat and handle lookups, we have an untrusted inode number |
| 2356 | * that we have to verify is valid. We cannot do this just by reading |
| 2357 | * the inode buffer as it may have been unlinked and removed leaving |
| 2358 | * inodes in stale state on disk. Hence we have to do a btree lookup |
| 2359 | * in all cases where an untrusted inode number is passed. |
| 2360 | */ |
Dave Chinner | 1920779 | 2010-06-24 11:15:47 +1000 | [diff] [blame] | 2361 | if (flags & XFS_IGET_UNTRUSTED) { |
Dave Chinner | 7124fe0 | 2010-06-24 11:15:33 +1000 | [diff] [blame] | 2362 | error = xfs_imap_lookup(mp, tp, agno, agino, agbno, |
| 2363 | &chunk_agbno, &offset_agbno, flags); |
| 2364 | if (error) |
| 2365 | return error; |
| 2366 | goto out_map; |
| 2367 | } |
| 2368 | |
Christoph Hellwig | 94e1b69 | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 2369 | /* |
| 2370 | * If the inode cluster size is the same as the blocksize or |
| 2371 | * smaller we get to the buffer by simple arithmetics. |
| 2372 | */ |
Jie Liu | f9e5abc | 2013-12-13 15:51:49 +1100 | [diff] [blame] | 2373 | if (blks_per_cluster == 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2374 | offset = XFS_INO_TO_OFFSET(mp, ino); |
| 2375 | ASSERT(offset < mp->m_sb.sb_inopblock); |
Christoph Hellwig | 94e1b69 | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 2376 | |
| 2377 | imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, agbno); |
| 2378 | imap->im_len = XFS_FSB_TO_BB(mp, 1); |
Darrick J. Wong | 755c7bf | 2016-11-08 11:55:48 +1100 | [diff] [blame] | 2379 | imap->im_boffset = (unsigned short)(offset << |
| 2380 | mp->m_sb.sb_inodelog); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2381 | return 0; |
| 2382 | } |
Christoph Hellwig | 94e1b69 | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 2383 | |
Christoph Hellwig | 94e1b69 | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 2384 | /* |
Christoph Hellwig | 94e1b69 | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 2385 | * If the inode chunks are aligned then use simple maths to |
| 2386 | * find the location. Otherwise we have to do a btree |
| 2387 | * lookup to find the location. |
| 2388 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2389 | if (mp->m_inoalign_mask) { |
| 2390 | offset_agbno = agbno & mp->m_inoalign_mask; |
| 2391 | chunk_agbno = agbno - offset_agbno; |
| 2392 | } else { |
Dave Chinner | 7124fe0 | 2010-06-24 11:15:33 +1000 | [diff] [blame] | 2393 | error = xfs_imap_lookup(mp, tp, agno, agino, agbno, |
| 2394 | &chunk_agbno, &offset_agbno, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2395 | if (error) |
| 2396 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2397 | } |
Christoph Hellwig | 94e1b69 | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 2398 | |
Dave Chinner | 7124fe0 | 2010-06-24 11:15:33 +1000 | [diff] [blame] | 2399 | out_map: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2400 | ASSERT(agbno >= chunk_agbno); |
| 2401 | cluster_agbno = chunk_agbno + |
| 2402 | ((offset_agbno / blks_per_cluster) * blks_per_cluster); |
| 2403 | offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) + |
| 2404 | XFS_INO_TO_OFFSET(mp, ino); |
Christoph Hellwig | 94e1b69 | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 2405 | |
| 2406 | imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, cluster_agbno); |
| 2407 | imap->im_len = XFS_FSB_TO_BB(mp, blks_per_cluster); |
Darrick J. Wong | 755c7bf | 2016-11-08 11:55:48 +1100 | [diff] [blame] | 2408 | imap->im_boffset = (unsigned short)(offset << mp->m_sb.sb_inodelog); |
Christoph Hellwig | 94e1b69 | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 2409 | |
| 2410 | /* |
| 2411 | * If the inode number maps to a block outside the bounds |
| 2412 | * of the file system then return NULL rather than calling |
| 2413 | * read_buf and panicing when we get an error from the |
| 2414 | * driver. |
| 2415 | */ |
| 2416 | if ((imap->im_blkno + imap->im_len) > |
| 2417 | XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) { |
Dave Chinner | 5348778 | 2011-03-07 10:05:35 +1100 | [diff] [blame] | 2418 | xfs_alert(mp, |
| 2419 | "%s: (im_blkno (0x%llx) + im_len (0x%llx)) > sb_dblocks (0x%llx)", |
| 2420 | __func__, (unsigned long long) imap->im_blkno, |
Christoph Hellwig | 94e1b69 | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 2421 | (unsigned long long) imap->im_len, |
| 2422 | XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 2423 | return -EINVAL; |
Christoph Hellwig | 94e1b69 | 2008-11-28 14:23:41 +1100 | [diff] [blame] | 2424 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2425 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2426 | } |
| 2427 | |
| 2428 | /* |
| 2429 | * Compute and fill in value of m_in_maxlevels. |
| 2430 | */ |
| 2431 | void |
| 2432 | xfs_ialloc_compute_maxlevels( |
| 2433 | xfs_mount_t *mp) /* file system mount structure */ |
| 2434 | { |
Darrick J. Wong | 19b54ee | 2016-06-21 11:53:28 +1000 | [diff] [blame] | 2435 | uint inodes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2436 | |
Darrick J. Wong | 19b54ee | 2016-06-21 11:53:28 +1000 | [diff] [blame] | 2437 | inodes = (1LL << XFS_INO_AGINO_BITS(mp)) >> XFS_INODES_PER_CHUNK_LOG; |
Eric Sandeen | a1f6941 | 2018-04-06 10:09:42 -0700 | [diff] [blame] | 2438 | mp->m_in_maxlevels = xfs_btree_compute_maxlevels(mp->m_inobt_mnr, |
Darrick J. Wong | 19b54ee | 2016-06-21 11:53:28 +1000 | [diff] [blame] | 2439 | inodes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2440 | } |
| 2441 | |
| 2442 | /* |
Brian Foster | aafc3c2 | 2014-04-24 16:00:52 +1000 | [diff] [blame] | 2443 | * Log specified fields for the ag hdr (inode section). The growth of the agi |
| 2444 | * structure over time requires that we interpret the buffer as two logical |
| 2445 | * regions delineated by the end of the unlinked list. This is due to the size |
| 2446 | * of the hash table and its location in the middle of the agi. |
| 2447 | * |
| 2448 | * For example, a request to log a field before agi_unlinked and a field after |
| 2449 | * agi_unlinked could cause us to log the entire hash table and use an excessive |
| 2450 | * amount of log space. To avoid this behavior, log the region up through |
| 2451 | * agi_unlinked in one call and the region after agi_unlinked through the end of |
| 2452 | * the structure in another. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2453 | */ |
| 2454 | void |
| 2455 | xfs_ialloc_log_agi( |
| 2456 | xfs_trans_t *tp, /* transaction pointer */ |
| 2457 | xfs_buf_t *bp, /* allocation group header buffer */ |
| 2458 | int fields) /* bitmask of fields to log */ |
| 2459 | { |
| 2460 | int first; /* first byte number */ |
| 2461 | int last; /* last byte number */ |
| 2462 | static const short offsets[] = { /* field starting offsets */ |
| 2463 | /* keep in sync with bit definitions */ |
| 2464 | offsetof(xfs_agi_t, agi_magicnum), |
| 2465 | offsetof(xfs_agi_t, agi_versionnum), |
| 2466 | offsetof(xfs_agi_t, agi_seqno), |
| 2467 | offsetof(xfs_agi_t, agi_length), |
| 2468 | offsetof(xfs_agi_t, agi_count), |
| 2469 | offsetof(xfs_agi_t, agi_root), |
| 2470 | offsetof(xfs_agi_t, agi_level), |
| 2471 | offsetof(xfs_agi_t, agi_freecount), |
| 2472 | offsetof(xfs_agi_t, agi_newino), |
| 2473 | offsetof(xfs_agi_t, agi_dirino), |
| 2474 | offsetof(xfs_agi_t, agi_unlinked), |
Brian Foster | aafc3c2 | 2014-04-24 16:00:52 +1000 | [diff] [blame] | 2475 | offsetof(xfs_agi_t, agi_free_root), |
| 2476 | offsetof(xfs_agi_t, agi_free_level), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2477 | sizeof(xfs_agi_t) |
| 2478 | }; |
| 2479 | #ifdef DEBUG |
| 2480 | xfs_agi_t *agi; /* allocation group header */ |
| 2481 | |
| 2482 | agi = XFS_BUF_TO_AGI(bp); |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 2483 | ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2484 | #endif |
Brian Foster | aafc3c2 | 2014-04-24 16:00:52 +1000 | [diff] [blame] | 2485 | |
Brian Foster | aafc3c2 | 2014-04-24 16:00:52 +1000 | [diff] [blame] | 2486 | /* |
| 2487 | * Compute byte offsets for the first and last fields in the first |
| 2488 | * region and log the agi buffer. This only logs up through |
| 2489 | * agi_unlinked. |
| 2490 | */ |
| 2491 | if (fields & XFS_AGI_ALL_BITS_R1) { |
| 2492 | xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R1, |
| 2493 | &first, &last); |
| 2494 | xfs_trans_log_buf(tp, bp, first, last); |
| 2495 | } |
| 2496 | |
| 2497 | /* |
| 2498 | * Mask off the bits in the first region and calculate the first and |
| 2499 | * last field offsets for any bits in the second region. |
| 2500 | */ |
| 2501 | fields &= ~XFS_AGI_ALL_BITS_R1; |
| 2502 | if (fields) { |
| 2503 | xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R2, |
| 2504 | &first, &last); |
| 2505 | xfs_trans_log_buf(tp, bp, first, last); |
| 2506 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2507 | } |
| 2508 | |
Darrick J. Wong | a6a781a | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 2509 | static xfs_failaddr_t |
Dave Chinner | 612cfbf | 2012-11-14 17:52:32 +1100 | [diff] [blame] | 2510 | xfs_agi_verify( |
Dave Chinner | 3702ce6 | 2012-11-12 22:54:05 +1100 | [diff] [blame] | 2511 | struct xfs_buf *bp) |
| 2512 | { |
| 2513 | struct xfs_mount *mp = bp->b_target->bt_mount; |
| 2514 | struct xfs_agi *agi = XFS_BUF_TO_AGI(bp); |
Dave Chinner | 9f96cc9 | 2018-06-03 16:12:16 -0700 | [diff] [blame] | 2515 | int i; |
Dave Chinner | 3702ce6 | 2012-11-12 22:54:05 +1100 | [diff] [blame] | 2516 | |
Brian Foster | a45086e | 2015-10-12 15:59:25 +1100 | [diff] [blame] | 2517 | if (xfs_sb_version_hascrc(&mp->m_sb)) { |
| 2518 | if (!uuid_equal(&agi->agi_uuid, &mp->m_sb.sb_meta_uuid)) |
Darrick J. Wong | a6a781a | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 2519 | return __this_address; |
Brian Foster | a45086e | 2015-10-12 15:59:25 +1100 | [diff] [blame] | 2520 | if (!xfs_log_check_lsn(mp, |
| 2521 | be64_to_cpu(XFS_BUF_TO_AGI(bp)->agi_lsn))) |
Darrick J. Wong | a6a781a | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 2522 | return __this_address; |
Brian Foster | a45086e | 2015-10-12 15:59:25 +1100 | [diff] [blame] | 2523 | } |
| 2524 | |
Dave Chinner | 3702ce6 | 2012-11-12 22:54:05 +1100 | [diff] [blame] | 2525 | /* |
| 2526 | * Validate the magic number of the agi block. |
| 2527 | */ |
Dave Chinner | 983d09f | 2013-04-03 16:11:15 +1100 | [diff] [blame] | 2528 | if (agi->agi_magicnum != cpu_to_be32(XFS_AGI_MAGIC)) |
Darrick J. Wong | a6a781a | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 2529 | return __this_address; |
Dave Chinner | 983d09f | 2013-04-03 16:11:15 +1100 | [diff] [blame] | 2530 | if (!XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum))) |
Darrick J. Wong | a6a781a | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 2531 | return __this_address; |
Dave Chinner | 3702ce6 | 2012-11-12 22:54:05 +1100 | [diff] [blame] | 2532 | |
Darrick J. Wong | d2a047f | 2016-12-05 12:32:50 +1100 | [diff] [blame] | 2533 | if (be32_to_cpu(agi->agi_level) < 1 || |
| 2534 | be32_to_cpu(agi->agi_level) > XFS_BTREE_MAXLEVELS) |
Darrick J. Wong | a6a781a | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 2535 | return __this_address; |
Darrick J. Wong | d2a047f | 2016-12-05 12:32:50 +1100 | [diff] [blame] | 2536 | |
| 2537 | if (xfs_sb_version_hasfinobt(&mp->m_sb) && |
| 2538 | (be32_to_cpu(agi->agi_free_level) < 1 || |
| 2539 | be32_to_cpu(agi->agi_free_level) > XFS_BTREE_MAXLEVELS)) |
Darrick J. Wong | a6a781a | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 2540 | return __this_address; |
Darrick J. Wong | d2a047f | 2016-12-05 12:32:50 +1100 | [diff] [blame] | 2541 | |
Dave Chinner | 3702ce6 | 2012-11-12 22:54:05 +1100 | [diff] [blame] | 2542 | /* |
| 2543 | * during growfs operations, the perag is not fully initialised, |
| 2544 | * so we can't use it for any useful checking. growfs ensures we can't |
| 2545 | * use it by using uncached buffers that don't have the perag attached |
| 2546 | * so we can detect and avoid this problem. |
| 2547 | */ |
Dave Chinner | 983d09f | 2013-04-03 16:11:15 +1100 | [diff] [blame] | 2548 | if (bp->b_pag && be32_to_cpu(agi->agi_seqno) != bp->b_pag->pag_agno) |
Darrick J. Wong | a6a781a | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 2549 | return __this_address; |
Dave Chinner | 3702ce6 | 2012-11-12 22:54:05 +1100 | [diff] [blame] | 2550 | |
Dave Chinner | 9f96cc9 | 2018-06-03 16:12:16 -0700 | [diff] [blame] | 2551 | for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++) { |
| 2552 | if (agi->agi_unlinked[i] == NULLAGINO) |
| 2553 | continue; |
| 2554 | if (!xfs_verify_ino(mp, be32_to_cpu(agi->agi_unlinked[i]))) |
| 2555 | return __this_address; |
| 2556 | } |
| 2557 | |
Darrick J. Wong | a6a781a | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 2558 | return NULL; |
Dave Chinner | 612cfbf | 2012-11-14 17:52:32 +1100 | [diff] [blame] | 2559 | } |
| 2560 | |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 2561 | static void |
| 2562 | xfs_agi_read_verify( |
Dave Chinner | 612cfbf | 2012-11-14 17:52:32 +1100 | [diff] [blame] | 2563 | struct xfs_buf *bp) |
| 2564 | { |
Dave Chinner | 983d09f | 2013-04-03 16:11:15 +1100 | [diff] [blame] | 2565 | struct xfs_mount *mp = bp->b_target->bt_mount; |
Darrick J. Wong | bc1a09b | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 2566 | xfs_failaddr_t fa; |
Dave Chinner | 983d09f | 2013-04-03 16:11:15 +1100 | [diff] [blame] | 2567 | |
Eric Sandeen | ce5028c | 2014-02-27 15:23:10 +1100 | [diff] [blame] | 2568 | if (xfs_sb_version_hascrc(&mp->m_sb) && |
| 2569 | !xfs_buf_verify_cksum(bp, XFS_AGI_CRC_OFF)) |
Darrick J. Wong | bc1a09b | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 2570 | xfs_verifier_error(bp, -EFSBADCRC, __this_address); |
| 2571 | else { |
| 2572 | fa = xfs_agi_verify(bp); |
| 2573 | if (XFS_TEST_ERROR(fa, mp, XFS_ERRTAG_IALLOC_READ_AGI)) |
| 2574 | xfs_verifier_error(bp, -EFSCORRUPTED, fa); |
| 2575 | } |
Dave Chinner | 612cfbf | 2012-11-14 17:52:32 +1100 | [diff] [blame] | 2576 | } |
| 2577 | |
Dave Chinner | b0f539d | 2012-11-14 17:53:49 +1100 | [diff] [blame] | 2578 | static void |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 2579 | xfs_agi_write_verify( |
Dave Chinner | 612cfbf | 2012-11-14 17:52:32 +1100 | [diff] [blame] | 2580 | struct xfs_buf *bp) |
| 2581 | { |
Carlos Maiolino | fb1755a | 2018-01-24 13:38:48 -0800 | [diff] [blame] | 2582 | struct xfs_mount *mp = bp->b_target->bt_mount; |
| 2583 | struct xfs_buf_log_item *bip = bp->b_log_item; |
Darrick J. Wong | bc1a09b | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 2584 | xfs_failaddr_t fa; |
Dave Chinner | 983d09f | 2013-04-03 16:11:15 +1100 | [diff] [blame] | 2585 | |
Darrick J. Wong | bc1a09b | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 2586 | fa = xfs_agi_verify(bp); |
| 2587 | if (fa) { |
| 2588 | xfs_verifier_error(bp, -EFSCORRUPTED, fa); |
Dave Chinner | 983d09f | 2013-04-03 16:11:15 +1100 | [diff] [blame] | 2589 | return; |
| 2590 | } |
| 2591 | |
| 2592 | if (!xfs_sb_version_hascrc(&mp->m_sb)) |
| 2593 | return; |
| 2594 | |
| 2595 | if (bip) |
| 2596 | 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] | 2597 | xfs_buf_update_cksum(bp, XFS_AGI_CRC_OFF); |
Dave Chinner | 3702ce6 | 2012-11-12 22:54:05 +1100 | [diff] [blame] | 2598 | } |
| 2599 | |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 2600 | const struct xfs_buf_ops xfs_agi_buf_ops = { |
Eric Sandeen | 233135b | 2016-01-04 16:10:19 +1100 | [diff] [blame] | 2601 | .name = "xfs_agi", |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 2602 | .verify_read = xfs_agi_read_verify, |
| 2603 | .verify_write = xfs_agi_write_verify, |
Darrick J. Wong | b557259 | 2018-01-08 10:51:08 -0800 | [diff] [blame] | 2604 | .verify_struct = xfs_agi_verify, |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 2605 | }; |
| 2606 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2607 | /* |
| 2608 | * Read in the allocation group header (inode allocation section) |
| 2609 | */ |
| 2610 | int |
Christoph Hellwig | 5e1be0f | 2008-11-28 14:23:37 +1100 | [diff] [blame] | 2611 | xfs_read_agi( |
| 2612 | struct xfs_mount *mp, /* file system mount structure */ |
| 2613 | struct xfs_trans *tp, /* transaction pointer */ |
| 2614 | xfs_agnumber_t agno, /* allocation group number */ |
| 2615 | struct xfs_buf **bpp) /* allocation group hdr buf */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2616 | { |
Christoph Hellwig | 5e1be0f | 2008-11-28 14:23:37 +1100 | [diff] [blame] | 2617 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2618 | |
Dave Chinner | d123031 | 2013-11-01 15:27:19 +1100 | [diff] [blame] | 2619 | trace_xfs_read_agi(mp, agno); |
Christoph Hellwig | 5e1be0f | 2008-11-28 14:23:37 +1100 | [diff] [blame] | 2620 | |
Dave Chinner | d123031 | 2013-11-01 15:27:19 +1100 | [diff] [blame] | 2621 | ASSERT(agno != NULLAGNUMBER); |
Christoph Hellwig | 5e1be0f | 2008-11-28 14:23:37 +1100 | [diff] [blame] | 2622 | error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2623 | XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)), |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 2624 | XFS_FSS_TO_BB(mp, 1), 0, bpp, &xfs_agi_buf_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2625 | if (error) |
| 2626 | return error; |
Eric Sandeen | 200237d | 2016-12-05 12:31:31 +1100 | [diff] [blame] | 2627 | if (tp) |
| 2628 | xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_AGI_BUF); |
Christoph Hellwig | 5e1be0f | 2008-11-28 14:23:37 +1100 | [diff] [blame] | 2629 | |
Christoph Hellwig | 38f2323 | 2011-10-10 16:52:45 +0000 | [diff] [blame] | 2630 | xfs_buf_set_ref(*bpp, XFS_AGI_REF); |
Christoph Hellwig | 5e1be0f | 2008-11-28 14:23:37 +1100 | [diff] [blame] | 2631 | return 0; |
| 2632 | } |
| 2633 | |
| 2634 | int |
| 2635 | xfs_ialloc_read_agi( |
| 2636 | struct xfs_mount *mp, /* file system mount structure */ |
| 2637 | struct xfs_trans *tp, /* transaction pointer */ |
| 2638 | xfs_agnumber_t agno, /* allocation group number */ |
| 2639 | struct xfs_buf **bpp) /* allocation group hdr buf */ |
| 2640 | { |
| 2641 | struct xfs_agi *agi; /* allocation group header */ |
| 2642 | struct xfs_perag *pag; /* per allocation group data */ |
| 2643 | int error; |
| 2644 | |
Dave Chinner | d123031 | 2013-11-01 15:27:19 +1100 | [diff] [blame] | 2645 | trace_xfs_ialloc_read_agi(mp, agno); |
| 2646 | |
Christoph Hellwig | 5e1be0f | 2008-11-28 14:23:37 +1100 | [diff] [blame] | 2647 | error = xfs_read_agi(mp, tp, agno, bpp); |
| 2648 | if (error) |
| 2649 | return error; |
| 2650 | |
| 2651 | agi = XFS_BUF_TO_AGI(*bpp); |
Dave Chinner | 44b56e0 | 2010-01-11 11:47:43 +0000 | [diff] [blame] | 2652 | pag = xfs_perag_get(mp, agno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2653 | if (!pag->pagi_init) { |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 2654 | pag->pagi_freecount = be32_to_cpu(agi->agi_freecount); |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 2655 | pag->pagi_count = be32_to_cpu(agi->agi_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2656 | pag->pagi_init = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2657 | } |
| 2658 | |
Christoph Hellwig | 5e1be0f | 2008-11-28 14:23:37 +1100 | [diff] [blame] | 2659 | /* |
| 2660 | * It's possible for these to be out of sync if |
| 2661 | * we are in the middle of a forced shutdown. |
| 2662 | */ |
| 2663 | ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) || |
| 2664 | XFS_FORCED_SHUTDOWN(mp)); |
Dave Chinner | 44b56e0 | 2010-01-11 11:47:43 +0000 | [diff] [blame] | 2665 | xfs_perag_put(pag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2666 | return 0; |
| 2667 | } |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 2668 | |
| 2669 | /* |
| 2670 | * Read in the agi to initialise the per-ag data in the mount structure |
| 2671 | */ |
| 2672 | int |
| 2673 | xfs_ialloc_pagi_init( |
| 2674 | xfs_mount_t *mp, /* file system mount structure */ |
| 2675 | xfs_trans_t *tp, /* transaction pointer */ |
| 2676 | xfs_agnumber_t agno) /* allocation group number */ |
| 2677 | { |
| 2678 | xfs_buf_t *bp = NULL; |
| 2679 | int error; |
| 2680 | |
| 2681 | error = xfs_ialloc_read_agi(mp, tp, agno, &bp); |
| 2682 | if (error) |
| 2683 | return error; |
| 2684 | if (bp) |
| 2685 | xfs_trans_brelse(tp, bp); |
| 2686 | return 0; |
| 2687 | } |
Darrick J. Wong | 91fb9af | 2017-10-17 21:37:34 -0700 | [diff] [blame] | 2688 | |
| 2689 | /* Calculate the first and last possible inode number in an AG. */ |
| 2690 | void |
| 2691 | xfs_ialloc_agino_range( |
| 2692 | struct xfs_mount *mp, |
| 2693 | xfs_agnumber_t agno, |
| 2694 | xfs_agino_t *first, |
| 2695 | xfs_agino_t *last) |
| 2696 | { |
| 2697 | xfs_agblock_t bno; |
| 2698 | xfs_agblock_t eoag; |
| 2699 | |
| 2700 | eoag = xfs_ag_block_count(mp, agno); |
| 2701 | |
| 2702 | /* |
| 2703 | * Calculate the first inode, which will be in the first |
| 2704 | * cluster-aligned block after the AGFL. |
| 2705 | */ |
| 2706 | bno = round_up(XFS_AGFL_BLOCK(mp) + 1, |
| 2707 | xfs_ialloc_cluster_alignment(mp)); |
| 2708 | *first = XFS_OFFBNO_TO_AGINO(mp, bno, 0); |
| 2709 | |
| 2710 | /* |
| 2711 | * Calculate the last inode, which will be at the end of the |
| 2712 | * last (aligned) cluster that can be allocated in the AG. |
| 2713 | */ |
| 2714 | bno = round_down(eoag, xfs_ialloc_cluster_alignment(mp)); |
| 2715 | *last = XFS_OFFBNO_TO_AGINO(mp, bno, 0) - 1; |
| 2716 | } |
| 2717 | |
| 2718 | /* |
| 2719 | * Verify that an AG inode number pointer neither points outside the AG |
| 2720 | * nor points at static metadata. |
| 2721 | */ |
| 2722 | bool |
| 2723 | xfs_verify_agino( |
| 2724 | struct xfs_mount *mp, |
| 2725 | xfs_agnumber_t agno, |
| 2726 | xfs_agino_t agino) |
| 2727 | { |
| 2728 | xfs_agino_t first; |
| 2729 | xfs_agino_t last; |
| 2730 | |
| 2731 | xfs_ialloc_agino_range(mp, agno, &first, &last); |
| 2732 | return agino >= first && agino <= last; |
| 2733 | } |
| 2734 | |
| 2735 | /* |
| 2736 | * Verify that an FS inode number pointer neither points outside the |
| 2737 | * filesystem nor points at static AG metadata. |
| 2738 | */ |
| 2739 | bool |
| 2740 | xfs_verify_ino( |
| 2741 | struct xfs_mount *mp, |
| 2742 | xfs_ino_t ino) |
| 2743 | { |
| 2744 | xfs_agnumber_t agno = XFS_INO_TO_AGNO(mp, ino); |
| 2745 | xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ino); |
| 2746 | |
| 2747 | if (agno >= mp->m_sb.sb_agcount) |
| 2748 | return false; |
| 2749 | if (XFS_AGINO_TO_INO(mp, agno, agino) != ino) |
| 2750 | return false; |
| 2751 | return xfs_verify_agino(mp, agno, agino); |
| 2752 | } |
| 2753 | |
| 2754 | /* Is this an internal inode number? */ |
| 2755 | bool |
| 2756 | xfs_internal_inum( |
| 2757 | struct xfs_mount *mp, |
| 2758 | xfs_ino_t ino) |
| 2759 | { |
| 2760 | return ino == mp->m_sb.sb_rbmino || ino == mp->m_sb.sb_rsumino || |
| 2761 | (xfs_sb_version_hasquota(&mp->m_sb) && |
| 2762 | xfs_is_quota_inode(&mp->m_sb, ino)); |
| 2763 | } |
| 2764 | |
| 2765 | /* |
| 2766 | * Verify that a directory entry's inode number doesn't point at an internal |
| 2767 | * inode, empty space, or static AG metadata. |
| 2768 | */ |
| 2769 | bool |
| 2770 | xfs_verify_dir_ino( |
| 2771 | struct xfs_mount *mp, |
| 2772 | xfs_ino_t ino) |
| 2773 | { |
| 2774 | if (xfs_internal_inum(mp, ino)) |
| 2775 | return false; |
| 2776 | return xfs_verify_ino(mp, ino); |
| 2777 | } |
Darrick J. Wong | 2e00126 | 2018-01-16 18:52:12 -0800 | [diff] [blame] | 2778 | |
| 2779 | /* Is there an inode record covering a given range of inode numbers? */ |
| 2780 | int |
| 2781 | xfs_ialloc_has_inode_record( |
| 2782 | struct xfs_btree_cur *cur, |
| 2783 | xfs_agino_t low, |
| 2784 | xfs_agino_t high, |
| 2785 | bool *exists) |
| 2786 | { |
| 2787 | struct xfs_inobt_rec_incore irec; |
| 2788 | xfs_agino_t agino; |
| 2789 | uint16_t holemask; |
| 2790 | int has_record; |
| 2791 | int i; |
| 2792 | int error; |
| 2793 | |
| 2794 | *exists = false; |
| 2795 | error = xfs_inobt_lookup(cur, low, XFS_LOOKUP_LE, &has_record); |
| 2796 | while (error == 0 && has_record) { |
| 2797 | error = xfs_inobt_get_rec(cur, &irec, &has_record); |
| 2798 | if (error || irec.ir_startino > high) |
| 2799 | break; |
| 2800 | |
| 2801 | agino = irec.ir_startino; |
| 2802 | holemask = irec.ir_holemask; |
| 2803 | for (i = 0; i < XFS_INOBT_HOLEMASK_BITS; holemask >>= 1, |
| 2804 | i++, agino += XFS_INODES_PER_HOLEMASK_BIT) { |
| 2805 | if (holemask & 1) |
| 2806 | continue; |
| 2807 | if (agino + XFS_INODES_PER_HOLEMASK_BIT > low && |
| 2808 | agino <= high) { |
| 2809 | *exists = true; |
| 2810 | return 0; |
| 2811 | } |
| 2812 | } |
| 2813 | |
| 2814 | error = xfs_btree_increment(cur, 0, &has_record); |
| 2815 | } |
| 2816 | return error; |
| 2817 | } |
| 2818 | |
| 2819 | /* Is there an inode record covering a given extent? */ |
| 2820 | int |
| 2821 | xfs_ialloc_has_inodes_at_extent( |
| 2822 | struct xfs_btree_cur *cur, |
| 2823 | xfs_agblock_t bno, |
| 2824 | xfs_extlen_t len, |
| 2825 | bool *exists) |
| 2826 | { |
| 2827 | xfs_agino_t low; |
| 2828 | xfs_agino_t high; |
| 2829 | |
| 2830 | low = XFS_OFFBNO_TO_AGINO(cur->bc_mp, bno, 0); |
| 2831 | high = XFS_OFFBNO_TO_AGINO(cur->bc_mp, bno + len, 0) - 1; |
| 2832 | |
| 2833 | return xfs_ialloc_has_inode_record(cur, low, high, exists); |
| 2834 | } |
| 2835 | |
| 2836 | struct xfs_ialloc_count_inodes { |
| 2837 | xfs_agino_t count; |
| 2838 | xfs_agino_t freecount; |
| 2839 | }; |
| 2840 | |
| 2841 | /* Record inode counts across all inobt records. */ |
| 2842 | STATIC int |
| 2843 | xfs_ialloc_count_inodes_rec( |
| 2844 | struct xfs_btree_cur *cur, |
| 2845 | union xfs_btree_rec *rec, |
| 2846 | void *priv) |
| 2847 | { |
| 2848 | struct xfs_inobt_rec_incore irec; |
| 2849 | struct xfs_ialloc_count_inodes *ci = priv; |
| 2850 | |
| 2851 | xfs_inobt_btrec_to_irec(cur->bc_mp, rec, &irec); |
| 2852 | ci->count += irec.ir_count; |
| 2853 | ci->freecount += irec.ir_freecount; |
| 2854 | |
| 2855 | return 0; |
| 2856 | } |
| 2857 | |
| 2858 | /* Count allocated and free inodes under an inobt. */ |
| 2859 | int |
| 2860 | xfs_ialloc_count_inodes( |
| 2861 | struct xfs_btree_cur *cur, |
| 2862 | xfs_agino_t *count, |
| 2863 | xfs_agino_t *freecount) |
| 2864 | { |
| 2865 | struct xfs_ialloc_count_inodes ci = {0}; |
| 2866 | int error; |
| 2867 | |
| 2868 | ASSERT(cur->bc_btnum == XFS_BTNUM_INO); |
| 2869 | error = xfs_btree_query_all(cur, xfs_ialloc_count_inodes_rec, &ci); |
| 2870 | if (error) |
| 2871 | return error; |
| 2872 | |
| 2873 | *count = ci.count; |
| 2874 | *freecount = ci.freecount; |
| 2875 | return 0; |
| 2876 | } |