Commit 85c0b2ab5e69ca6133380ead1c50e0840d136b39

Authored by Dave Chinner
Committed by Felix Blyakher
1 parent 1da1daed81

xfs: factor out inode initialisation

Factor out code to initialize new inode clusters into a function of it's own.
This keeps xfs_ialloc_ag_alloc smaller and better structured and enables a
future inode cluster initialization transaction.  Also initialize the agno
variable earlier in xfs_ialloc_ag_alloc to avoid repeated byte swaps.

[hch:  The original patch is from Dave from his unpublished inode create
 transaction patch series, with some modifcations by me to apply stand-alone]

Signed-off-by: Dave Chinner <david@fromorbit.com>
Signed-off-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Alex Elder <aelder@sgi.com>
Signed-off-by: Felix Blyakher <felixb@sgi.com>

Showing 1 changed file with 95 additions and 80 deletions Inline Diff

1 /* 1 /*
2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc. 2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
3 * All Rights Reserved. 3 * All Rights Reserved.
4 * 4 *
5 * This program is free software; you can redistribute it and/or 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 6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * This program is distributed in the hope that it would be useful, 9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 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, 15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */ 17 */
18 #include "xfs.h" 18 #include "xfs.h"
19 #include "xfs_fs.h" 19 #include "xfs_fs.h"
20 #include "xfs_types.h" 20 #include "xfs_types.h"
21 #include "xfs_bit.h" 21 #include "xfs_bit.h"
22 #include "xfs_log.h" 22 #include "xfs_log.h"
23 #include "xfs_inum.h" 23 #include "xfs_inum.h"
24 #include "xfs_trans.h" 24 #include "xfs_trans.h"
25 #include "xfs_sb.h" 25 #include "xfs_sb.h"
26 #include "xfs_ag.h" 26 #include "xfs_ag.h"
27 #include "xfs_dir2.h" 27 #include "xfs_dir2.h"
28 #include "xfs_dmapi.h" 28 #include "xfs_dmapi.h"
29 #include "xfs_mount.h" 29 #include "xfs_mount.h"
30 #include "xfs_bmap_btree.h" 30 #include "xfs_bmap_btree.h"
31 #include "xfs_alloc_btree.h" 31 #include "xfs_alloc_btree.h"
32 #include "xfs_ialloc_btree.h" 32 #include "xfs_ialloc_btree.h"
33 #include "xfs_dir2_sf.h" 33 #include "xfs_dir2_sf.h"
34 #include "xfs_attr_sf.h" 34 #include "xfs_attr_sf.h"
35 #include "xfs_dinode.h" 35 #include "xfs_dinode.h"
36 #include "xfs_inode.h" 36 #include "xfs_inode.h"
37 #include "xfs_btree.h" 37 #include "xfs_btree.h"
38 #include "xfs_ialloc.h" 38 #include "xfs_ialloc.h"
39 #include "xfs_alloc.h" 39 #include "xfs_alloc.h"
40 #include "xfs_rtalloc.h" 40 #include "xfs_rtalloc.h"
41 #include "xfs_error.h" 41 #include "xfs_error.h"
42 #include "xfs_bmap.h" 42 #include "xfs_bmap.h"
43 43
44 44
45 /* 45 /*
46 * Allocation group level functions. 46 * Allocation group level functions.
47 */ 47 */
48 static inline int 48 static inline int
49 xfs_ialloc_cluster_alignment( 49 xfs_ialloc_cluster_alignment(
50 xfs_alloc_arg_t *args) 50 xfs_alloc_arg_t *args)
51 { 51 {
52 if (xfs_sb_version_hasalign(&args->mp->m_sb) && 52 if (xfs_sb_version_hasalign(&args->mp->m_sb) &&
53 args->mp->m_sb.sb_inoalignmt >= 53 args->mp->m_sb.sb_inoalignmt >=
54 XFS_B_TO_FSBT(args->mp, XFS_INODE_CLUSTER_SIZE(args->mp))) 54 XFS_B_TO_FSBT(args->mp, XFS_INODE_CLUSTER_SIZE(args->mp)))
55 return args->mp->m_sb.sb_inoalignmt; 55 return args->mp->m_sb.sb_inoalignmt;
56 return 1; 56 return 1;
57 } 57 }
58 58
59 /* 59 /*
60 * Lookup the record equal to ino in the btree given by cur. 60 * Lookup the record equal to ino in the btree given by cur.
61 */ 61 */
62 STATIC int /* error */ 62 STATIC int /* error */
63 xfs_inobt_lookup_eq( 63 xfs_inobt_lookup_eq(
64 struct xfs_btree_cur *cur, /* btree cursor */ 64 struct xfs_btree_cur *cur, /* btree cursor */
65 xfs_agino_t ino, /* starting inode of chunk */ 65 xfs_agino_t ino, /* starting inode of chunk */
66 __int32_t fcnt, /* free inode count */ 66 __int32_t fcnt, /* free inode count */
67 xfs_inofree_t free, /* free inode mask */ 67 xfs_inofree_t free, /* free inode mask */
68 int *stat) /* success/failure */ 68 int *stat) /* success/failure */
69 { 69 {
70 cur->bc_rec.i.ir_startino = ino; 70 cur->bc_rec.i.ir_startino = ino;
71 cur->bc_rec.i.ir_freecount = fcnt; 71 cur->bc_rec.i.ir_freecount = fcnt;
72 cur->bc_rec.i.ir_free = free; 72 cur->bc_rec.i.ir_free = free;
73 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat); 73 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
74 } 74 }
75 75
76 /* 76 /*
77 * Lookup the first record greater than or equal to ino 77 * Lookup the first record greater than or equal to ino
78 * in the btree given by cur. 78 * in the btree given by cur.
79 */ 79 */
80 int /* error */ 80 int /* error */
81 xfs_inobt_lookup_ge( 81 xfs_inobt_lookup_ge(
82 struct xfs_btree_cur *cur, /* btree cursor */ 82 struct xfs_btree_cur *cur, /* btree cursor */
83 xfs_agino_t ino, /* starting inode of chunk */ 83 xfs_agino_t ino, /* starting inode of chunk */
84 __int32_t fcnt, /* free inode count */ 84 __int32_t fcnt, /* free inode count */
85 xfs_inofree_t free, /* free inode mask */ 85 xfs_inofree_t free, /* free inode mask */
86 int *stat) /* success/failure */ 86 int *stat) /* success/failure */
87 { 87 {
88 cur->bc_rec.i.ir_startino = ino; 88 cur->bc_rec.i.ir_startino = ino;
89 cur->bc_rec.i.ir_freecount = fcnt; 89 cur->bc_rec.i.ir_freecount = fcnt;
90 cur->bc_rec.i.ir_free = free; 90 cur->bc_rec.i.ir_free = free;
91 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat); 91 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
92 } 92 }
93 93
94 /* 94 /*
95 * Lookup the first record less than or equal to ino 95 * Lookup the first record less than or equal to ino
96 * in the btree given by cur. 96 * in the btree given by cur.
97 */ 97 */
98 int /* error */ 98 int /* error */
99 xfs_inobt_lookup_le( 99 xfs_inobt_lookup_le(
100 struct xfs_btree_cur *cur, /* btree cursor */ 100 struct xfs_btree_cur *cur, /* btree cursor */
101 xfs_agino_t ino, /* starting inode of chunk */ 101 xfs_agino_t ino, /* starting inode of chunk */
102 __int32_t fcnt, /* free inode count */ 102 __int32_t fcnt, /* free inode count */
103 xfs_inofree_t free, /* free inode mask */ 103 xfs_inofree_t free, /* free inode mask */
104 int *stat) /* success/failure */ 104 int *stat) /* success/failure */
105 { 105 {
106 cur->bc_rec.i.ir_startino = ino; 106 cur->bc_rec.i.ir_startino = ino;
107 cur->bc_rec.i.ir_freecount = fcnt; 107 cur->bc_rec.i.ir_freecount = fcnt;
108 cur->bc_rec.i.ir_free = free; 108 cur->bc_rec.i.ir_free = free;
109 return xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat); 109 return xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat);
110 } 110 }
111 111
112 /* 112 /*
113 * Update the record referred to by cur to the value given 113 * Update the record referred to by cur to the value given
114 * by [ino, fcnt, free]. 114 * by [ino, fcnt, free].
115 * This either works (return 0) or gets an EFSCORRUPTED error. 115 * This either works (return 0) or gets an EFSCORRUPTED error.
116 */ 116 */
117 STATIC int /* error */ 117 STATIC int /* error */
118 xfs_inobt_update( 118 xfs_inobt_update(
119 struct xfs_btree_cur *cur, /* btree cursor */ 119 struct xfs_btree_cur *cur, /* btree cursor */
120 xfs_agino_t ino, /* starting inode of chunk */ 120 xfs_agino_t ino, /* starting inode of chunk */
121 __int32_t fcnt, /* free inode count */ 121 __int32_t fcnt, /* free inode count */
122 xfs_inofree_t free) /* free inode mask */ 122 xfs_inofree_t free) /* free inode mask */
123 { 123 {
124 union xfs_btree_rec rec; 124 union xfs_btree_rec rec;
125 125
126 rec.inobt.ir_startino = cpu_to_be32(ino); 126 rec.inobt.ir_startino = cpu_to_be32(ino);
127 rec.inobt.ir_freecount = cpu_to_be32(fcnt); 127 rec.inobt.ir_freecount = cpu_to_be32(fcnt);
128 rec.inobt.ir_free = cpu_to_be64(free); 128 rec.inobt.ir_free = cpu_to_be64(free);
129 return xfs_btree_update(cur, &rec); 129 return xfs_btree_update(cur, &rec);
130 } 130 }
131 131
132 /* 132 /*
133 * Get the data from the pointed-to record. 133 * Get the data from the pointed-to record.
134 */ 134 */
135 int /* error */ 135 int /* error */
136 xfs_inobt_get_rec( 136 xfs_inobt_get_rec(
137 struct xfs_btree_cur *cur, /* btree cursor */ 137 struct xfs_btree_cur *cur, /* btree cursor */
138 xfs_agino_t *ino, /* output: starting inode of chunk */ 138 xfs_agino_t *ino, /* output: starting inode of chunk */
139 __int32_t *fcnt, /* output: number of free inodes */ 139 __int32_t *fcnt, /* output: number of free inodes */
140 xfs_inofree_t *free, /* output: free inode mask */ 140 xfs_inofree_t *free, /* output: free inode mask */
141 int *stat) /* output: success/failure */ 141 int *stat) /* output: success/failure */
142 { 142 {
143 union xfs_btree_rec *rec; 143 union xfs_btree_rec *rec;
144 int error; 144 int error;
145 145
146 error = xfs_btree_get_rec(cur, &rec, stat); 146 error = xfs_btree_get_rec(cur, &rec, stat);
147 if (!error && *stat == 1) { 147 if (!error && *stat == 1) {
148 *ino = be32_to_cpu(rec->inobt.ir_startino); 148 *ino = be32_to_cpu(rec->inobt.ir_startino);
149 *fcnt = be32_to_cpu(rec->inobt.ir_freecount); 149 *fcnt = be32_to_cpu(rec->inobt.ir_freecount);
150 *free = be64_to_cpu(rec->inobt.ir_free); 150 *free = be64_to_cpu(rec->inobt.ir_free);
151 } 151 }
152 return error; 152 return error;
153 } 153 }
154 154
155 /* 155 /*
156 * Initialise a new set of inodes.
157 */
158 STATIC void
159 xfs_ialloc_inode_init(
160 struct xfs_mount *mp,
161 struct xfs_trans *tp,
162 xfs_agnumber_t agno,
163 xfs_agblock_t agbno,
164 xfs_agblock_t length,
165 unsigned int gen)
166 {
167 struct xfs_buf *fbuf;
168 struct xfs_dinode *free;
169 int blks_per_cluster, nbufs, ninodes;
170 int version;
171 int i, j;
172 xfs_daddr_t d;
173
174 /*
175 * Loop over the new block(s), filling in the inodes.
176 * For small block sizes, manipulate the inodes in buffers
177 * which are multiples of the blocks size.
178 */
179 if (mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) {
180 blks_per_cluster = 1;
181 nbufs = length;
182 ninodes = mp->m_sb.sb_inopblock;
183 } else {
184 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) /
185 mp->m_sb.sb_blocksize;
186 nbufs = length / blks_per_cluster;
187 ninodes = blks_per_cluster * mp->m_sb.sb_inopblock;
188 }
189
190 /*
191 * Figure out what version number to use in the inodes we create.
192 * If the superblock version has caught up to the one that supports
193 * the new inode format, then use the new inode version. Otherwise
194 * use the old version so that old kernels will continue to be
195 * able to use the file system.
196 */
197 if (xfs_sb_version_hasnlink(&mp->m_sb))
198 version = 2;
199 else
200 version = 1;
201
202 for (j = 0; j < nbufs; j++) {
203 /*
204 * Get the block.
205 */
206 d = XFS_AGB_TO_DADDR(mp, agno, agbno + (j * blks_per_cluster));
207 fbuf = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
208 mp->m_bsize * blks_per_cluster,
209 XFS_BUF_LOCK);
210 ASSERT(fbuf);
211 ASSERT(!XFS_BUF_GETERROR(fbuf));
212
213 /*
214 * Initialize all inodes in this buffer and then log them.
215 *
216 * XXX: It would be much better if we had just one transaction
217 * to log a whole cluster of inodes instead of all the
218 * individual transactions causing a lot of log traffic.
219 */
220 xfs_biozero(fbuf, 0, ninodes << mp->m_sb.sb_inodelog);
221 for (i = 0; i < ninodes; i++) {
222 int ioffset = i << mp->m_sb.sb_inodelog;
223 uint isize = sizeof(struct xfs_dinode);
224
225 free = xfs_make_iptr(mp, fbuf, i);
226 free->di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
227 free->di_version = version;
228 free->di_gen = cpu_to_be32(gen);
229 free->di_next_unlinked = cpu_to_be32(NULLAGINO);
230 xfs_trans_log_buf(tp, fbuf, ioffset, ioffset + isize - 1);
231 }
232 xfs_trans_inode_alloc_buf(tp, fbuf);
233 }
234 }
235
236 /*
156 * Allocate new inodes in the allocation group specified by agbp. 237 * Allocate new inodes in the allocation group specified by agbp.
157 * Return 0 for success, else error code. 238 * Return 0 for success, else error code.
158 */ 239 */
159 STATIC int /* error code or 0 */ 240 STATIC int /* error code or 0 */
160 xfs_ialloc_ag_alloc( 241 xfs_ialloc_ag_alloc(
161 xfs_trans_t *tp, /* transaction pointer */ 242 xfs_trans_t *tp, /* transaction pointer */
162 xfs_buf_t *agbp, /* alloc group buffer */ 243 xfs_buf_t *agbp, /* alloc group buffer */
163 int *alloc) 244 int *alloc)
164 { 245 {
165 xfs_agi_t *agi; /* allocation group header */ 246 xfs_agi_t *agi; /* allocation group header */
166 xfs_alloc_arg_t args; /* allocation argument structure */ 247 xfs_alloc_arg_t args; /* allocation argument structure */
167 int blks_per_cluster; /* fs blocks per inode cluster */
168 xfs_btree_cur_t *cur; /* inode btree cursor */ 248 xfs_btree_cur_t *cur; /* inode btree cursor */
169 xfs_daddr_t d; /* disk addr of buffer */
170 xfs_agnumber_t agno; 249 xfs_agnumber_t agno;
171 int error; 250 int error;
172 xfs_buf_t *fbuf; /* new free inodes' buffer */ 251 int i;
173 xfs_dinode_t *free; /* new free inode structure */
174 int i; /* inode counter */
175 int j; /* block counter */
176 int nbufs; /* num bufs of new inodes */
177 xfs_agino_t newino; /* new first inode's number */ 252 xfs_agino_t newino; /* new first inode's number */
178 xfs_agino_t newlen; /* new number of inodes */ 253 xfs_agino_t newlen; /* new number of inodes */
179 int ninodes; /* num inodes per buf */
180 xfs_agino_t thisino; /* current inode number, for loop */ 254 xfs_agino_t thisino; /* current inode number, for loop */
181 int version; /* inode version number to use */
182 int isaligned = 0; /* inode allocation at stripe unit */ 255 int isaligned = 0; /* inode allocation at stripe unit */
183 /* boundary */ 256 /* boundary */
184 unsigned int gen;
185 257
186 args.tp = tp; 258 args.tp = tp;
187 args.mp = tp->t_mountp; 259 args.mp = tp->t_mountp;
188 260
189 /* 261 /*
190 * Locking will ensure that we don't have two callers in here 262 * Locking will ensure that we don't have two callers in here
191 * at one time. 263 * at one time.
192 */ 264 */
193 newlen = XFS_IALLOC_INODES(args.mp); 265 newlen = XFS_IALLOC_INODES(args.mp);
194 if (args.mp->m_maxicount && 266 if (args.mp->m_maxicount &&
195 args.mp->m_sb.sb_icount + newlen > args.mp->m_maxicount) 267 args.mp->m_sb.sb_icount + newlen > args.mp->m_maxicount)
196 return XFS_ERROR(ENOSPC); 268 return XFS_ERROR(ENOSPC);
197 args.minlen = args.maxlen = XFS_IALLOC_BLOCKS(args.mp); 269 args.minlen = args.maxlen = XFS_IALLOC_BLOCKS(args.mp);
198 /* 270 /*
199 * First try to allocate inodes contiguous with the last-allocated 271 * First try to allocate inodes contiguous with the last-allocated
200 * chunk of inodes. If the filesystem is striped, this will fill 272 * chunk of inodes. If the filesystem is striped, this will fill
201 * an entire stripe unit with inodes. 273 * an entire stripe unit with inodes.
202 */ 274 */
203 agi = XFS_BUF_TO_AGI(agbp); 275 agi = XFS_BUF_TO_AGI(agbp);
204 newino = be32_to_cpu(agi->agi_newino); 276 newino = be32_to_cpu(agi->agi_newino);
277 agno = be32_to_cpu(agi->agi_seqno);
205 args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) + 278 args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) +
206 XFS_IALLOC_BLOCKS(args.mp); 279 XFS_IALLOC_BLOCKS(args.mp);
207 if (likely(newino != NULLAGINO && 280 if (likely(newino != NULLAGINO &&
208 (args.agbno < be32_to_cpu(agi->agi_length)))) { 281 (args.agbno < be32_to_cpu(agi->agi_length)))) {
209 args.fsbno = XFS_AGB_TO_FSB(args.mp, 282 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
210 be32_to_cpu(agi->agi_seqno), args.agbno);
211 args.type = XFS_ALLOCTYPE_THIS_BNO; 283 args.type = XFS_ALLOCTYPE_THIS_BNO;
212 args.mod = args.total = args.wasdel = args.isfl = 284 args.mod = args.total = args.wasdel = args.isfl =
213 args.userdata = args.minalignslop = 0; 285 args.userdata = args.minalignslop = 0;
214 args.prod = 1; 286 args.prod = 1;
215 287
216 /* 288 /*
217 * We need to take into account alignment here to ensure that 289 * We need to take into account alignment here to ensure that
218 * we don't modify the free list if we fail to have an exact 290 * we don't modify the free list if we fail to have an exact
219 * block. If we don't have an exact match, and every oher 291 * block. If we don't have an exact match, and every oher
220 * attempt allocation attempt fails, we'll end up cancelling 292 * attempt allocation attempt fails, we'll end up cancelling
221 * a dirty transaction and shutting down. 293 * a dirty transaction and shutting down.
222 * 294 *
223 * For an exact allocation, alignment must be 1, 295 * For an exact allocation, alignment must be 1,
224 * however we need to take cluster alignment into account when 296 * however we need to take cluster alignment into account when
225 * fixing up the freelist. Use the minalignslop field to 297 * fixing up the freelist. Use the minalignslop field to
226 * indicate that extra blocks might be required for alignment, 298 * indicate that extra blocks might be required for alignment,
227 * but not to use them in the actual exact allocation. 299 * but not to use them in the actual exact allocation.
228 */ 300 */
229 args.alignment = 1; 301 args.alignment = 1;
230 args.minalignslop = xfs_ialloc_cluster_alignment(&args) - 1; 302 args.minalignslop = xfs_ialloc_cluster_alignment(&args) - 1;
231 303
232 /* Allow space for the inode btree to split. */ 304 /* Allow space for the inode btree to split. */
233 args.minleft = args.mp->m_in_maxlevels - 1; 305 args.minleft = args.mp->m_in_maxlevels - 1;
234 if ((error = xfs_alloc_vextent(&args))) 306 if ((error = xfs_alloc_vextent(&args)))
235 return error; 307 return error;
236 } else 308 } else
237 args.fsbno = NULLFSBLOCK; 309 args.fsbno = NULLFSBLOCK;
238 310
239 if (unlikely(args.fsbno == NULLFSBLOCK)) { 311 if (unlikely(args.fsbno == NULLFSBLOCK)) {
240 /* 312 /*
241 * Set the alignment for the allocation. 313 * Set the alignment for the allocation.
242 * If stripe alignment is turned on then align at stripe unit 314 * If stripe alignment is turned on then align at stripe unit
243 * boundary. 315 * boundary.
244 * If the cluster size is smaller than a filesystem block 316 * If the cluster size is smaller than a filesystem block
245 * then we're doing I/O for inodes in filesystem block size 317 * then we're doing I/O for inodes in filesystem block size
246 * pieces, so don't need alignment anyway. 318 * pieces, so don't need alignment anyway.
247 */ 319 */
248 isaligned = 0; 320 isaligned = 0;
249 if (args.mp->m_sinoalign) { 321 if (args.mp->m_sinoalign) {
250 ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN)); 322 ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN));
251 args.alignment = args.mp->m_dalign; 323 args.alignment = args.mp->m_dalign;
252 isaligned = 1; 324 isaligned = 1;
253 } else 325 } else
254 args.alignment = xfs_ialloc_cluster_alignment(&args); 326 args.alignment = xfs_ialloc_cluster_alignment(&args);
255 /* 327 /*
256 * Need to figure out where to allocate the inode blocks. 328 * Need to figure out where to allocate the inode blocks.
257 * Ideally they should be spaced out through the a.g. 329 * Ideally they should be spaced out through the a.g.
258 * For now, just allocate blocks up front. 330 * For now, just allocate blocks up front.
259 */ 331 */
260 args.agbno = be32_to_cpu(agi->agi_root); 332 args.agbno = be32_to_cpu(agi->agi_root);
261 args.fsbno = XFS_AGB_TO_FSB(args.mp, 333 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
262 be32_to_cpu(agi->agi_seqno), args.agbno);
263 /* 334 /*
264 * Allocate a fixed-size extent of inodes. 335 * Allocate a fixed-size extent of inodes.
265 */ 336 */
266 args.type = XFS_ALLOCTYPE_NEAR_BNO; 337 args.type = XFS_ALLOCTYPE_NEAR_BNO;
267 args.mod = args.total = args.wasdel = args.isfl = 338 args.mod = args.total = args.wasdel = args.isfl =
268 args.userdata = args.minalignslop = 0; 339 args.userdata = args.minalignslop = 0;
269 args.prod = 1; 340 args.prod = 1;
270 /* 341 /*
271 * Allow space for the inode btree to split. 342 * Allow space for the inode btree to split.
272 */ 343 */
273 args.minleft = args.mp->m_in_maxlevels - 1; 344 args.minleft = args.mp->m_in_maxlevels - 1;
274 if ((error = xfs_alloc_vextent(&args))) 345 if ((error = xfs_alloc_vextent(&args)))
275 return error; 346 return error;
276 } 347 }
277 348
278 /* 349 /*
279 * If stripe alignment is turned on, then try again with cluster 350 * If stripe alignment is turned on, then try again with cluster
280 * alignment. 351 * alignment.
281 */ 352 */
282 if (isaligned && args.fsbno == NULLFSBLOCK) { 353 if (isaligned && args.fsbno == NULLFSBLOCK) {
283 args.type = XFS_ALLOCTYPE_NEAR_BNO; 354 args.type = XFS_ALLOCTYPE_NEAR_BNO;
284 args.agbno = be32_to_cpu(agi->agi_root); 355 args.agbno = be32_to_cpu(agi->agi_root);
285 args.fsbno = XFS_AGB_TO_FSB(args.mp, 356 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
286 be32_to_cpu(agi->agi_seqno), args.agbno);
287 args.alignment = xfs_ialloc_cluster_alignment(&args); 357 args.alignment = xfs_ialloc_cluster_alignment(&args);
288 if ((error = xfs_alloc_vextent(&args))) 358 if ((error = xfs_alloc_vextent(&args)))
289 return error; 359 return error;
290 } 360 }
291 361
292 if (args.fsbno == NULLFSBLOCK) { 362 if (args.fsbno == NULLFSBLOCK) {
293 *alloc = 0; 363 *alloc = 0;
294 return 0; 364 return 0;
295 } 365 }
296 ASSERT(args.len == args.minlen); 366 ASSERT(args.len == args.minlen);
297 /*
298 * Convert the results.
299 */
300 newino = XFS_OFFBNO_TO_AGINO(args.mp, args.agbno, 0);
301 /*
302 * Loop over the new block(s), filling in the inodes.
303 * For small block sizes, manipulate the inodes in buffers
304 * which are multiples of the blocks size.
305 */
306 if (args.mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(args.mp)) {
307 blks_per_cluster = 1;
308 nbufs = (int)args.len;
309 ninodes = args.mp->m_sb.sb_inopblock;
310 } else {
311 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(args.mp) /
312 args.mp->m_sb.sb_blocksize;
313 nbufs = (int)args.len / blks_per_cluster;
314 ninodes = blks_per_cluster * args.mp->m_sb.sb_inopblock;
315 }
316 /*
317 * Figure out what version number to use in the inodes we create.
318 * If the superblock version has caught up to the one that supports
319 * the new inode format, then use the new inode version. Otherwise
320 * use the old version so that old kernels will continue to be
321 * able to use the file system.
322 */
323 if (xfs_sb_version_hasnlink(&args.mp->m_sb))
324 version = 2;
325 else
326 version = 1;
327 367
328 /* 368 /*
369 * Stamp and write the inode buffers.
370 *
329 * Seed the new inode cluster with a random generation number. This 371 * Seed the new inode cluster with a random generation number. This
330 * prevents short-term reuse of generation numbers if a chunk is 372 * prevents short-term reuse of generation numbers if a chunk is
331 * freed and then immediately reallocated. We use random numbers 373 * freed and then immediately reallocated. We use random numbers
332 * rather than a linear progression to prevent the next generation 374 * rather than a linear progression to prevent the next generation
333 * number from being easily guessable. 375 * number from being easily guessable.
334 */ 376 */
335 gen = random32(); 377 xfs_ialloc_inode_init(args.mp, tp, agno, args.agbno, args.len,
336 for (j = 0; j < nbufs; j++) { 378 random32());
337 /*
338 * Get the block.
339 */
340 d = XFS_AGB_TO_DADDR(args.mp, be32_to_cpu(agi->agi_seqno),
341 args.agbno + (j * blks_per_cluster));
342 fbuf = xfs_trans_get_buf(tp, args.mp->m_ddev_targp, d,
343 args.mp->m_bsize * blks_per_cluster,
344 XFS_BUF_LOCK);
345 ASSERT(fbuf);
346 ASSERT(!XFS_BUF_GETERROR(fbuf));
347 379
348 /* 380 /*
349 * Initialize all inodes in this buffer and then log them. 381 * Convert the results.
350 * 382 */
351 * XXX: It would be much better if we had just one transaction to 383 newino = XFS_OFFBNO_TO_AGINO(args.mp, args.agbno, 0);
352 * log a whole cluster of inodes instead of all the individual
353 * transactions causing a lot of log traffic.
354 */
355 xfs_biozero(fbuf, 0, ninodes << args.mp->m_sb.sb_inodelog);
356 for (i = 0; i < ninodes; i++) {
357 int ioffset = i << args.mp->m_sb.sb_inodelog;
358 uint isize = sizeof(struct xfs_dinode);
359
360 free = xfs_make_iptr(args.mp, fbuf, i);
361 free->di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
362 free->di_version = version;
363 free->di_gen = cpu_to_be32(gen);
364 free->di_next_unlinked = cpu_to_be32(NULLAGINO);
365 xfs_trans_log_buf(tp, fbuf, ioffset, ioffset + isize - 1);
366 }
367 xfs_trans_inode_alloc_buf(tp, fbuf);
368 }
369 be32_add_cpu(&agi->agi_count, newlen); 384 be32_add_cpu(&agi->agi_count, newlen);
370 be32_add_cpu(&agi->agi_freecount, newlen); 385 be32_add_cpu(&agi->agi_freecount, newlen);
371 agno = be32_to_cpu(agi->agi_seqno);
372 down_read(&args.mp->m_peraglock); 386 down_read(&args.mp->m_peraglock);
373 args.mp->m_perag[agno].pagi_freecount += newlen; 387 args.mp->m_perag[agno].pagi_freecount += newlen;
374 up_read(&args.mp->m_peraglock); 388 up_read(&args.mp->m_peraglock);
375 agi->agi_newino = cpu_to_be32(newino); 389 agi->agi_newino = cpu_to_be32(newino);
390
376 /* 391 /*
377 * Insert records describing the new inode chunk into the btree. 392 * Insert records describing the new inode chunk into the btree.
378 */ 393 */
379 cur = xfs_inobt_init_cursor(args.mp, tp, agbp, agno); 394 cur = xfs_inobt_init_cursor(args.mp, tp, agbp, agno);
380 for (thisino = newino; 395 for (thisino = newino;
381 thisino < newino + newlen; 396 thisino < newino + newlen;
382 thisino += XFS_INODES_PER_CHUNK) { 397 thisino += XFS_INODES_PER_CHUNK) {
383 if ((error = xfs_inobt_lookup_eq(cur, thisino, 398 if ((error = xfs_inobt_lookup_eq(cur, thisino,
384 XFS_INODES_PER_CHUNK, XFS_INOBT_ALL_FREE, &i))) { 399 XFS_INODES_PER_CHUNK, XFS_INOBT_ALL_FREE, &i))) {
385 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); 400 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
386 return error; 401 return error;
387 } 402 }
388 ASSERT(i == 0); 403 ASSERT(i == 0);
389 if ((error = xfs_btree_insert(cur, &i))) { 404 if ((error = xfs_btree_insert(cur, &i))) {
390 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); 405 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
391 return error; 406 return error;
392 } 407 }
393 ASSERT(i == 1); 408 ASSERT(i == 1);
394 } 409 }
395 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); 410 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
396 /* 411 /*
397 * Log allocation group header fields 412 * Log allocation group header fields
398 */ 413 */
399 xfs_ialloc_log_agi(tp, agbp, 414 xfs_ialloc_log_agi(tp, agbp,
400 XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO); 415 XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO);
401 /* 416 /*
402 * Modify/log superblock values for inode count and inode free count. 417 * Modify/log superblock values for inode count and inode free count.
403 */ 418 */
404 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen); 419 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen);
405 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen); 420 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen);
406 *alloc = 1; 421 *alloc = 1;
407 return 0; 422 return 0;
408 } 423 }
409 424
410 STATIC_INLINE xfs_agnumber_t 425 STATIC_INLINE xfs_agnumber_t
411 xfs_ialloc_next_ag( 426 xfs_ialloc_next_ag(
412 xfs_mount_t *mp) 427 xfs_mount_t *mp)
413 { 428 {
414 xfs_agnumber_t agno; 429 xfs_agnumber_t agno;
415 430
416 spin_lock(&mp->m_agirotor_lock); 431 spin_lock(&mp->m_agirotor_lock);
417 agno = mp->m_agirotor; 432 agno = mp->m_agirotor;
418 if (++mp->m_agirotor == mp->m_maxagi) 433 if (++mp->m_agirotor == mp->m_maxagi)
419 mp->m_agirotor = 0; 434 mp->m_agirotor = 0;
420 spin_unlock(&mp->m_agirotor_lock); 435 spin_unlock(&mp->m_agirotor_lock);
421 436
422 return agno; 437 return agno;
423 } 438 }
424 439
425 /* 440 /*
426 * Select an allocation group to look for a free inode in, based on the parent 441 * Select an allocation group to look for a free inode in, based on the parent
427 * inode and then mode. Return the allocation group buffer. 442 * inode and then mode. Return the allocation group buffer.
428 */ 443 */
429 STATIC xfs_buf_t * /* allocation group buffer */ 444 STATIC xfs_buf_t * /* allocation group buffer */
430 xfs_ialloc_ag_select( 445 xfs_ialloc_ag_select(
431 xfs_trans_t *tp, /* transaction pointer */ 446 xfs_trans_t *tp, /* transaction pointer */
432 xfs_ino_t parent, /* parent directory inode number */ 447 xfs_ino_t parent, /* parent directory inode number */
433 mode_t mode, /* bits set to indicate file type */ 448 mode_t mode, /* bits set to indicate file type */
434 int okalloc) /* ok to allocate more space */ 449 int okalloc) /* ok to allocate more space */
435 { 450 {
436 xfs_buf_t *agbp; /* allocation group header buffer */ 451 xfs_buf_t *agbp; /* allocation group header buffer */
437 xfs_agnumber_t agcount; /* number of ag's in the filesystem */ 452 xfs_agnumber_t agcount; /* number of ag's in the filesystem */
438 xfs_agnumber_t agno; /* current ag number */ 453 xfs_agnumber_t agno; /* current ag number */
439 int flags; /* alloc buffer locking flags */ 454 int flags; /* alloc buffer locking flags */
440 xfs_extlen_t ineed; /* blocks needed for inode allocation */ 455 xfs_extlen_t ineed; /* blocks needed for inode allocation */
441 xfs_extlen_t longest = 0; /* longest extent available */ 456 xfs_extlen_t longest = 0; /* longest extent available */
442 xfs_mount_t *mp; /* mount point structure */ 457 xfs_mount_t *mp; /* mount point structure */
443 int needspace; /* file mode implies space allocated */ 458 int needspace; /* file mode implies space allocated */
444 xfs_perag_t *pag; /* per allocation group data */ 459 xfs_perag_t *pag; /* per allocation group data */
445 xfs_agnumber_t pagno; /* parent (starting) ag number */ 460 xfs_agnumber_t pagno; /* parent (starting) ag number */
446 461
447 /* 462 /*
448 * Files of these types need at least one block if length > 0 463 * Files of these types need at least one block if length > 0
449 * (and they won't fit in the inode, but that's hard to figure out). 464 * (and they won't fit in the inode, but that's hard to figure out).
450 */ 465 */
451 needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode); 466 needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode);
452 mp = tp->t_mountp; 467 mp = tp->t_mountp;
453 agcount = mp->m_maxagi; 468 agcount = mp->m_maxagi;
454 if (S_ISDIR(mode)) 469 if (S_ISDIR(mode))
455 pagno = xfs_ialloc_next_ag(mp); 470 pagno = xfs_ialloc_next_ag(mp);
456 else { 471 else {
457 pagno = XFS_INO_TO_AGNO(mp, parent); 472 pagno = XFS_INO_TO_AGNO(mp, parent);
458 if (pagno >= agcount) 473 if (pagno >= agcount)
459 pagno = 0; 474 pagno = 0;
460 } 475 }
461 ASSERT(pagno < agcount); 476 ASSERT(pagno < agcount);
462 /* 477 /*
463 * Loop through allocation groups, looking for one with a little 478 * Loop through allocation groups, looking for one with a little
464 * free space in it. Note we don't look for free inodes, exactly. 479 * free space in it. Note we don't look for free inodes, exactly.
465 * Instead, we include whether there is a need to allocate inodes 480 * Instead, we include whether there is a need to allocate inodes
466 * to mean that blocks must be allocated for them, 481 * to mean that blocks must be allocated for them,
467 * if none are currently free. 482 * if none are currently free.
468 */ 483 */
469 agno = pagno; 484 agno = pagno;
470 flags = XFS_ALLOC_FLAG_TRYLOCK; 485 flags = XFS_ALLOC_FLAG_TRYLOCK;
471 down_read(&mp->m_peraglock); 486 down_read(&mp->m_peraglock);
472 for (;;) { 487 for (;;) {
473 pag = &mp->m_perag[agno]; 488 pag = &mp->m_perag[agno];
474 if (!pag->pagi_init) { 489 if (!pag->pagi_init) {
475 if (xfs_ialloc_read_agi(mp, tp, agno, &agbp)) { 490 if (xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
476 agbp = NULL; 491 agbp = NULL;
477 goto nextag; 492 goto nextag;
478 } 493 }
479 } else 494 } else
480 agbp = NULL; 495 agbp = NULL;
481 496
482 if (!pag->pagi_inodeok) { 497 if (!pag->pagi_inodeok) {
483 xfs_ialloc_next_ag(mp); 498 xfs_ialloc_next_ag(mp);
484 goto unlock_nextag; 499 goto unlock_nextag;
485 } 500 }
486 501
487 /* 502 /*
488 * Is there enough free space for the file plus a block 503 * Is there enough free space for the file plus a block
489 * of inodes (if we need to allocate some)? 504 * of inodes (if we need to allocate some)?
490 */ 505 */
491 ineed = pag->pagi_freecount ? 0 : XFS_IALLOC_BLOCKS(mp); 506 ineed = pag->pagi_freecount ? 0 : XFS_IALLOC_BLOCKS(mp);
492 if (ineed && !pag->pagf_init) { 507 if (ineed && !pag->pagf_init) {
493 if (agbp == NULL && 508 if (agbp == NULL &&
494 xfs_ialloc_read_agi(mp, tp, agno, &agbp)) { 509 xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
495 agbp = NULL; 510 agbp = NULL;
496 goto nextag; 511 goto nextag;
497 } 512 }
498 (void)xfs_alloc_pagf_init(mp, tp, agno, flags); 513 (void)xfs_alloc_pagf_init(mp, tp, agno, flags);
499 } 514 }
500 if (!ineed || pag->pagf_init) { 515 if (!ineed || pag->pagf_init) {
501 if (ineed && !(longest = pag->pagf_longest)) 516 if (ineed && !(longest = pag->pagf_longest))
502 longest = pag->pagf_flcount > 0; 517 longest = pag->pagf_flcount > 0;
503 if (!ineed || 518 if (!ineed ||
504 (pag->pagf_freeblks >= needspace + ineed && 519 (pag->pagf_freeblks >= needspace + ineed &&
505 longest >= ineed && 520 longest >= ineed &&
506 okalloc)) { 521 okalloc)) {
507 if (agbp == NULL && 522 if (agbp == NULL &&
508 xfs_ialloc_read_agi(mp, tp, agno, &agbp)) { 523 xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
509 agbp = NULL; 524 agbp = NULL;
510 goto nextag; 525 goto nextag;
511 } 526 }
512 up_read(&mp->m_peraglock); 527 up_read(&mp->m_peraglock);
513 return agbp; 528 return agbp;
514 } 529 }
515 } 530 }
516 unlock_nextag: 531 unlock_nextag:
517 if (agbp) 532 if (agbp)
518 xfs_trans_brelse(tp, agbp); 533 xfs_trans_brelse(tp, agbp);
519 nextag: 534 nextag:
520 /* 535 /*
521 * No point in iterating over the rest, if we're shutting 536 * No point in iterating over the rest, if we're shutting
522 * down. 537 * down.
523 */ 538 */
524 if (XFS_FORCED_SHUTDOWN(mp)) { 539 if (XFS_FORCED_SHUTDOWN(mp)) {
525 up_read(&mp->m_peraglock); 540 up_read(&mp->m_peraglock);
526 return NULL; 541 return NULL;
527 } 542 }
528 agno++; 543 agno++;
529 if (agno >= agcount) 544 if (agno >= agcount)
530 agno = 0; 545 agno = 0;
531 if (agno == pagno) { 546 if (agno == pagno) {
532 if (flags == 0) { 547 if (flags == 0) {
533 up_read(&mp->m_peraglock); 548 up_read(&mp->m_peraglock);
534 return NULL; 549 return NULL;
535 } 550 }
536 flags = 0; 551 flags = 0;
537 } 552 }
538 } 553 }
539 } 554 }
540 555
541 /* 556 /*
542 * Visible inode allocation functions. 557 * Visible inode allocation functions.
543 */ 558 */
544 559
545 /* 560 /*
546 * Allocate an inode on disk. 561 * Allocate an inode on disk.
547 * Mode is used to tell whether the new inode will need space, and whether 562 * Mode is used to tell whether the new inode will need space, and whether
548 * it is a directory. 563 * it is a directory.
549 * 564 *
550 * The arguments IO_agbp and alloc_done are defined to work within 565 * The arguments IO_agbp and alloc_done are defined to work within
551 * the constraint of one allocation per transaction. 566 * the constraint of one allocation per transaction.
552 * xfs_dialloc() is designed to be called twice if it has to do an 567 * xfs_dialloc() is designed to be called twice if it has to do an
553 * allocation to make more free inodes. On the first call, 568 * allocation to make more free inodes. On the first call,
554 * IO_agbp should be set to NULL. If an inode is available, 569 * IO_agbp should be set to NULL. If an inode is available,
555 * i.e., xfs_dialloc() did not need to do an allocation, an inode 570 * i.e., xfs_dialloc() did not need to do an allocation, an inode
556 * number is returned. In this case, IO_agbp would be set to the 571 * number is returned. In this case, IO_agbp would be set to the
557 * current ag_buf and alloc_done set to false. 572 * current ag_buf and alloc_done set to false.
558 * If an allocation needed to be done, xfs_dialloc would return 573 * If an allocation needed to be done, xfs_dialloc would return
559 * the current ag_buf in IO_agbp and set alloc_done to true. 574 * the current ag_buf in IO_agbp and set alloc_done to true.
560 * The caller should then commit the current transaction, allocate a new 575 * The caller should then commit the current transaction, allocate a new
561 * transaction, and call xfs_dialloc() again, passing in the previous 576 * transaction, and call xfs_dialloc() again, passing in the previous
562 * value of IO_agbp. IO_agbp should be held across the transactions. 577 * value of IO_agbp. IO_agbp should be held across the transactions.
563 * Since the agbp is locked across the two calls, the second call is 578 * Since the agbp is locked across the two calls, the second call is
564 * guaranteed to have a free inode available. 579 * guaranteed to have a free inode available.
565 * 580 *
566 * Once we successfully pick an inode its number is returned and the 581 * Once we successfully pick an inode its number is returned and the
567 * on-disk data structures are updated. The inode itself is not read 582 * on-disk data structures are updated. The inode itself is not read
568 * in, since doing so would break ordering constraints with xfs_reclaim. 583 * in, since doing so would break ordering constraints with xfs_reclaim.
569 */ 584 */
570 int 585 int
571 xfs_dialloc( 586 xfs_dialloc(
572 xfs_trans_t *tp, /* transaction pointer */ 587 xfs_trans_t *tp, /* transaction pointer */
573 xfs_ino_t parent, /* parent inode (directory) */ 588 xfs_ino_t parent, /* parent inode (directory) */
574 mode_t mode, /* mode bits for new inode */ 589 mode_t mode, /* mode bits for new inode */
575 int okalloc, /* ok to allocate more space */ 590 int okalloc, /* ok to allocate more space */
576 xfs_buf_t **IO_agbp, /* in/out ag header's buffer */ 591 xfs_buf_t **IO_agbp, /* in/out ag header's buffer */
577 boolean_t *alloc_done, /* true if we needed to replenish 592 boolean_t *alloc_done, /* true if we needed to replenish
578 inode freelist */ 593 inode freelist */
579 xfs_ino_t *inop) /* inode number allocated */ 594 xfs_ino_t *inop) /* inode number allocated */
580 { 595 {
581 xfs_agnumber_t agcount; /* number of allocation groups */ 596 xfs_agnumber_t agcount; /* number of allocation groups */
582 xfs_buf_t *agbp; /* allocation group header's buffer */ 597 xfs_buf_t *agbp; /* allocation group header's buffer */
583 xfs_agnumber_t agno; /* allocation group number */ 598 xfs_agnumber_t agno; /* allocation group number */
584 xfs_agi_t *agi; /* allocation group header structure */ 599 xfs_agi_t *agi; /* allocation group header structure */
585 xfs_btree_cur_t *cur; /* inode allocation btree cursor */ 600 xfs_btree_cur_t *cur; /* inode allocation btree cursor */
586 int error; /* error return value */ 601 int error; /* error return value */
587 int i; /* result code */ 602 int i; /* result code */
588 int ialloced; /* inode allocation status */ 603 int ialloced; /* inode allocation status */
589 int noroom = 0; /* no space for inode blk allocation */ 604 int noroom = 0; /* no space for inode blk allocation */
590 xfs_ino_t ino; /* fs-relative inode to be returned */ 605 xfs_ino_t ino; /* fs-relative inode to be returned */
591 /* REFERENCED */ 606 /* REFERENCED */
592 int j; /* result code */ 607 int j; /* result code */
593 xfs_mount_t *mp; /* file system mount structure */ 608 xfs_mount_t *mp; /* file system mount structure */
594 int offset; /* index of inode in chunk */ 609 int offset; /* index of inode in chunk */
595 xfs_agino_t pagino; /* parent's a.g. relative inode # */ 610 xfs_agino_t pagino; /* parent's a.g. relative inode # */
596 xfs_agnumber_t pagno; /* parent's allocation group number */ 611 xfs_agnumber_t pagno; /* parent's allocation group number */
597 xfs_inobt_rec_incore_t rec; /* inode allocation record */ 612 xfs_inobt_rec_incore_t rec; /* inode allocation record */
598 xfs_agnumber_t tagno; /* testing allocation group number */ 613 xfs_agnumber_t tagno; /* testing allocation group number */
599 xfs_btree_cur_t *tcur; /* temp cursor */ 614 xfs_btree_cur_t *tcur; /* temp cursor */
600 xfs_inobt_rec_incore_t trec; /* temp inode allocation record */ 615 xfs_inobt_rec_incore_t trec; /* temp inode allocation record */
601 616
602 617
603 if (*IO_agbp == NULL) { 618 if (*IO_agbp == NULL) {
604 /* 619 /*
605 * We do not have an agbp, so select an initial allocation 620 * We do not have an agbp, so select an initial allocation
606 * group for inode allocation. 621 * group for inode allocation.
607 */ 622 */
608 agbp = xfs_ialloc_ag_select(tp, parent, mode, okalloc); 623 agbp = xfs_ialloc_ag_select(tp, parent, mode, okalloc);
609 /* 624 /*
610 * Couldn't find an allocation group satisfying the 625 * Couldn't find an allocation group satisfying the
611 * criteria, give up. 626 * criteria, give up.
612 */ 627 */
613 if (!agbp) { 628 if (!agbp) {
614 *inop = NULLFSINO; 629 *inop = NULLFSINO;
615 return 0; 630 return 0;
616 } 631 }
617 agi = XFS_BUF_TO_AGI(agbp); 632 agi = XFS_BUF_TO_AGI(agbp);
618 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC); 633 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
619 } else { 634 } else {
620 /* 635 /*
621 * Continue where we left off before. In this case, we 636 * Continue where we left off before. In this case, we
622 * know that the allocation group has free inodes. 637 * know that the allocation group has free inodes.
623 */ 638 */
624 agbp = *IO_agbp; 639 agbp = *IO_agbp;
625 agi = XFS_BUF_TO_AGI(agbp); 640 agi = XFS_BUF_TO_AGI(agbp);
626 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC); 641 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
627 ASSERT(be32_to_cpu(agi->agi_freecount) > 0); 642 ASSERT(be32_to_cpu(agi->agi_freecount) > 0);
628 } 643 }
629 mp = tp->t_mountp; 644 mp = tp->t_mountp;
630 agcount = mp->m_sb.sb_agcount; 645 agcount = mp->m_sb.sb_agcount;
631 agno = be32_to_cpu(agi->agi_seqno); 646 agno = be32_to_cpu(agi->agi_seqno);
632 tagno = agno; 647 tagno = agno;
633 pagno = XFS_INO_TO_AGNO(mp, parent); 648 pagno = XFS_INO_TO_AGNO(mp, parent);
634 pagino = XFS_INO_TO_AGINO(mp, parent); 649 pagino = XFS_INO_TO_AGINO(mp, parent);
635 650
636 /* 651 /*
637 * If we have already hit the ceiling of inode blocks then clear 652 * If we have already hit the ceiling of inode blocks then clear
638 * okalloc so we scan all available agi structures for a free 653 * okalloc so we scan all available agi structures for a free
639 * inode. 654 * inode.
640 */ 655 */
641 656
642 if (mp->m_maxicount && 657 if (mp->m_maxicount &&
643 mp->m_sb.sb_icount + XFS_IALLOC_INODES(mp) > mp->m_maxicount) { 658 mp->m_sb.sb_icount + XFS_IALLOC_INODES(mp) > mp->m_maxicount) {
644 noroom = 1; 659 noroom = 1;
645 okalloc = 0; 660 okalloc = 0;
646 } 661 }
647 662
648 /* 663 /*
649 * Loop until we find an allocation group that either has free inodes 664 * Loop until we find an allocation group that either has free inodes
650 * or in which we can allocate some inodes. Iterate through the 665 * or in which we can allocate some inodes. Iterate through the
651 * allocation groups upward, wrapping at the end. 666 * allocation groups upward, wrapping at the end.
652 */ 667 */
653 *alloc_done = B_FALSE; 668 *alloc_done = B_FALSE;
654 while (!agi->agi_freecount) { 669 while (!agi->agi_freecount) {
655 /* 670 /*
656 * Don't do anything if we're not supposed to allocate 671 * Don't do anything if we're not supposed to allocate
657 * any blocks, just go on to the next ag. 672 * any blocks, just go on to the next ag.
658 */ 673 */
659 if (okalloc) { 674 if (okalloc) {
660 /* 675 /*
661 * Try to allocate some new inodes in the allocation 676 * Try to allocate some new inodes in the allocation
662 * group. 677 * group.
663 */ 678 */
664 if ((error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced))) { 679 if ((error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced))) {
665 xfs_trans_brelse(tp, agbp); 680 xfs_trans_brelse(tp, agbp);
666 if (error == ENOSPC) { 681 if (error == ENOSPC) {
667 *inop = NULLFSINO; 682 *inop = NULLFSINO;
668 return 0; 683 return 0;
669 } else 684 } else
670 return error; 685 return error;
671 } 686 }
672 if (ialloced) { 687 if (ialloced) {
673 /* 688 /*
674 * We successfully allocated some inodes, return 689 * We successfully allocated some inodes, return
675 * the current context to the caller so that it 690 * the current context to the caller so that it
676 * can commit the current transaction and call 691 * can commit the current transaction and call
677 * us again where we left off. 692 * us again where we left off.
678 */ 693 */
679 ASSERT(be32_to_cpu(agi->agi_freecount) > 0); 694 ASSERT(be32_to_cpu(agi->agi_freecount) > 0);
680 *alloc_done = B_TRUE; 695 *alloc_done = B_TRUE;
681 *IO_agbp = agbp; 696 *IO_agbp = agbp;
682 *inop = NULLFSINO; 697 *inop = NULLFSINO;
683 return 0; 698 return 0;
684 } 699 }
685 } 700 }
686 /* 701 /*
687 * If it failed, give up on this ag. 702 * If it failed, give up on this ag.
688 */ 703 */
689 xfs_trans_brelse(tp, agbp); 704 xfs_trans_brelse(tp, agbp);
690 /* 705 /*
691 * Go on to the next ag: get its ag header. 706 * Go on to the next ag: get its ag header.
692 */ 707 */
693 nextag: 708 nextag:
694 if (++tagno == agcount) 709 if (++tagno == agcount)
695 tagno = 0; 710 tagno = 0;
696 if (tagno == agno) { 711 if (tagno == agno) {
697 *inop = NULLFSINO; 712 *inop = NULLFSINO;
698 return noroom ? ENOSPC : 0; 713 return noroom ? ENOSPC : 0;
699 } 714 }
700 down_read(&mp->m_peraglock); 715 down_read(&mp->m_peraglock);
701 if (mp->m_perag[tagno].pagi_inodeok == 0) { 716 if (mp->m_perag[tagno].pagi_inodeok == 0) {
702 up_read(&mp->m_peraglock); 717 up_read(&mp->m_peraglock);
703 goto nextag; 718 goto nextag;
704 } 719 }
705 error = xfs_ialloc_read_agi(mp, tp, tagno, &agbp); 720 error = xfs_ialloc_read_agi(mp, tp, tagno, &agbp);
706 up_read(&mp->m_peraglock); 721 up_read(&mp->m_peraglock);
707 if (error) 722 if (error)
708 goto nextag; 723 goto nextag;
709 agi = XFS_BUF_TO_AGI(agbp); 724 agi = XFS_BUF_TO_AGI(agbp);
710 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC); 725 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
711 } 726 }
712 /* 727 /*
713 * Here with an allocation group that has a free inode. 728 * Here with an allocation group that has a free inode.
714 * Reset agno since we may have chosen a new ag in the 729 * Reset agno since we may have chosen a new ag in the
715 * loop above. 730 * loop above.
716 */ 731 */
717 agno = tagno; 732 agno = tagno;
718 *IO_agbp = NULL; 733 *IO_agbp = NULL;
719 cur = xfs_inobt_init_cursor(mp, tp, agbp, be32_to_cpu(agi->agi_seqno)); 734 cur = xfs_inobt_init_cursor(mp, tp, agbp, be32_to_cpu(agi->agi_seqno));
720 /* 735 /*
721 * If pagino is 0 (this is the root inode allocation) use newino. 736 * If pagino is 0 (this is the root inode allocation) use newino.
722 * This must work because we've just allocated some. 737 * This must work because we've just allocated some.
723 */ 738 */
724 if (!pagino) 739 if (!pagino)
725 pagino = be32_to_cpu(agi->agi_newino); 740 pagino = be32_to_cpu(agi->agi_newino);
726 #ifdef DEBUG 741 #ifdef DEBUG
727 if (cur->bc_nlevels == 1) { 742 if (cur->bc_nlevels == 1) {
728 int freecount = 0; 743 int freecount = 0;
729 744
730 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i))) 745 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
731 goto error0; 746 goto error0;
732 XFS_WANT_CORRUPTED_GOTO(i == 1, error0); 747 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
733 do { 748 do {
734 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino, 749 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
735 &rec.ir_freecount, &rec.ir_free, &i))) 750 &rec.ir_freecount, &rec.ir_free, &i)))
736 goto error0; 751 goto error0;
737 XFS_WANT_CORRUPTED_GOTO(i == 1, error0); 752 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
738 freecount += rec.ir_freecount; 753 freecount += rec.ir_freecount;
739 if ((error = xfs_btree_increment(cur, 0, &i))) 754 if ((error = xfs_btree_increment(cur, 0, &i)))
740 goto error0; 755 goto error0;
741 } while (i == 1); 756 } while (i == 1);
742 757
743 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) || 758 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
744 XFS_FORCED_SHUTDOWN(mp)); 759 XFS_FORCED_SHUTDOWN(mp));
745 } 760 }
746 #endif 761 #endif
747 /* 762 /*
748 * If in the same a.g. as the parent, try to get near the parent. 763 * If in the same a.g. as the parent, try to get near the parent.
749 */ 764 */
750 if (pagno == agno) { 765 if (pagno == agno) {
751 if ((error = xfs_inobt_lookup_le(cur, pagino, 0, 0, &i))) 766 if ((error = xfs_inobt_lookup_le(cur, pagino, 0, 0, &i)))
752 goto error0; 767 goto error0;
753 if (i != 0 && 768 if (i != 0 &&
754 (error = xfs_inobt_get_rec(cur, &rec.ir_startino, 769 (error = xfs_inobt_get_rec(cur, &rec.ir_startino,
755 &rec.ir_freecount, &rec.ir_free, &j)) == 0 && 770 &rec.ir_freecount, &rec.ir_free, &j)) == 0 &&
756 j == 1 && 771 j == 1 &&
757 rec.ir_freecount > 0) { 772 rec.ir_freecount > 0) {
758 /* 773 /*
759 * Found a free inode in the same chunk 774 * Found a free inode in the same chunk
760 * as parent, done. 775 * as parent, done.
761 */ 776 */
762 } 777 }
763 /* 778 /*
764 * In the same a.g. as parent, but parent's chunk is full. 779 * In the same a.g. as parent, but parent's chunk is full.
765 */ 780 */
766 else { 781 else {
767 int doneleft; /* done, to the left */ 782 int doneleft; /* done, to the left */
768 int doneright; /* done, to the right */ 783 int doneright; /* done, to the right */
769 784
770 if (error) 785 if (error)
771 goto error0; 786 goto error0;
772 ASSERT(i == 1); 787 ASSERT(i == 1);
773 ASSERT(j == 1); 788 ASSERT(j == 1);
774 /* 789 /*
775 * Duplicate the cursor, search left & right 790 * Duplicate the cursor, search left & right
776 * simultaneously. 791 * simultaneously.
777 */ 792 */
778 if ((error = xfs_btree_dup_cursor(cur, &tcur))) 793 if ((error = xfs_btree_dup_cursor(cur, &tcur)))
779 goto error0; 794 goto error0;
780 /* 795 /*
781 * Search left with tcur, back up 1 record. 796 * Search left with tcur, back up 1 record.
782 */ 797 */
783 if ((error = xfs_btree_decrement(tcur, 0, &i))) 798 if ((error = xfs_btree_decrement(tcur, 0, &i)))
784 goto error1; 799 goto error1;
785 doneleft = !i; 800 doneleft = !i;
786 if (!doneleft) { 801 if (!doneleft) {
787 if ((error = xfs_inobt_get_rec(tcur, 802 if ((error = xfs_inobt_get_rec(tcur,
788 &trec.ir_startino, 803 &trec.ir_startino,
789 &trec.ir_freecount, 804 &trec.ir_freecount,
790 &trec.ir_free, &i))) 805 &trec.ir_free, &i)))
791 goto error1; 806 goto error1;
792 XFS_WANT_CORRUPTED_GOTO(i == 1, error1); 807 XFS_WANT_CORRUPTED_GOTO(i == 1, error1);
793 } 808 }
794 /* 809 /*
795 * Search right with cur, go forward 1 record. 810 * Search right with cur, go forward 1 record.
796 */ 811 */
797 if ((error = xfs_btree_increment(cur, 0, &i))) 812 if ((error = xfs_btree_increment(cur, 0, &i)))
798 goto error1; 813 goto error1;
799 doneright = !i; 814 doneright = !i;
800 if (!doneright) { 815 if (!doneright) {
801 if ((error = xfs_inobt_get_rec(cur, 816 if ((error = xfs_inobt_get_rec(cur,
802 &rec.ir_startino, 817 &rec.ir_startino,
803 &rec.ir_freecount, 818 &rec.ir_freecount,
804 &rec.ir_free, &i))) 819 &rec.ir_free, &i)))
805 goto error1; 820 goto error1;
806 XFS_WANT_CORRUPTED_GOTO(i == 1, error1); 821 XFS_WANT_CORRUPTED_GOTO(i == 1, error1);
807 } 822 }
808 /* 823 /*
809 * Loop until we find the closest inode chunk 824 * Loop until we find the closest inode chunk
810 * with a free one. 825 * with a free one.
811 */ 826 */
812 while (!doneleft || !doneright) { 827 while (!doneleft || !doneright) {
813 int useleft; /* using left inode 828 int useleft; /* using left inode
814 chunk this time */ 829 chunk this time */
815 830
816 /* 831 /*
817 * Figure out which block is closer, 832 * Figure out which block is closer,
818 * if both are valid. 833 * if both are valid.
819 */ 834 */
820 if (!doneleft && !doneright) 835 if (!doneleft && !doneright)
821 useleft = 836 useleft =
822 pagino - 837 pagino -
823 (trec.ir_startino + 838 (trec.ir_startino +
824 XFS_INODES_PER_CHUNK - 1) < 839 XFS_INODES_PER_CHUNK - 1) <
825 rec.ir_startino - pagino; 840 rec.ir_startino - pagino;
826 else 841 else
827 useleft = !doneleft; 842 useleft = !doneleft;
828 /* 843 /*
829 * If checking the left, does it have 844 * If checking the left, does it have
830 * free inodes? 845 * free inodes?
831 */ 846 */
832 if (useleft && trec.ir_freecount) { 847 if (useleft && trec.ir_freecount) {
833 /* 848 /*
834 * Yes, set it up as the chunk to use. 849 * Yes, set it up as the chunk to use.
835 */ 850 */
836 rec = trec; 851 rec = trec;
837 xfs_btree_del_cursor(cur, 852 xfs_btree_del_cursor(cur,
838 XFS_BTREE_NOERROR); 853 XFS_BTREE_NOERROR);
839 cur = tcur; 854 cur = tcur;
840 break; 855 break;
841 } 856 }
842 /* 857 /*
843 * If checking the right, does it have 858 * If checking the right, does it have
844 * free inodes? 859 * free inodes?
845 */ 860 */
846 if (!useleft && rec.ir_freecount) { 861 if (!useleft && rec.ir_freecount) {
847 /* 862 /*
848 * Yes, it's already set up. 863 * Yes, it's already set up.
849 */ 864 */
850 xfs_btree_del_cursor(tcur, 865 xfs_btree_del_cursor(tcur,
851 XFS_BTREE_NOERROR); 866 XFS_BTREE_NOERROR);
852 break; 867 break;
853 } 868 }
854 /* 869 /*
855 * If used the left, get another one 870 * If used the left, get another one
856 * further left. 871 * further left.
857 */ 872 */
858 if (useleft) { 873 if (useleft) {
859 if ((error = xfs_btree_decrement(tcur, 0, 874 if ((error = xfs_btree_decrement(tcur, 0,
860 &i))) 875 &i)))
861 goto error1; 876 goto error1;
862 doneleft = !i; 877 doneleft = !i;
863 if (!doneleft) { 878 if (!doneleft) {
864 if ((error = xfs_inobt_get_rec( 879 if ((error = xfs_inobt_get_rec(
865 tcur, 880 tcur,
866 &trec.ir_startino, 881 &trec.ir_startino,
867 &trec.ir_freecount, 882 &trec.ir_freecount,
868 &trec.ir_free, &i))) 883 &trec.ir_free, &i)))
869 goto error1; 884 goto error1;
870 XFS_WANT_CORRUPTED_GOTO(i == 1, 885 XFS_WANT_CORRUPTED_GOTO(i == 1,
871 error1); 886 error1);
872 } 887 }
873 } 888 }
874 /* 889 /*
875 * If used the right, get another one 890 * If used the right, get another one
876 * further right. 891 * further right.
877 */ 892 */
878 else { 893 else {
879 if ((error = xfs_btree_increment(cur, 0, 894 if ((error = xfs_btree_increment(cur, 0,
880 &i))) 895 &i)))
881 goto error1; 896 goto error1;
882 doneright = !i; 897 doneright = !i;
883 if (!doneright) { 898 if (!doneright) {
884 if ((error = xfs_inobt_get_rec( 899 if ((error = xfs_inobt_get_rec(
885 cur, 900 cur,
886 &rec.ir_startino, 901 &rec.ir_startino,
887 &rec.ir_freecount, 902 &rec.ir_freecount,
888 &rec.ir_free, &i))) 903 &rec.ir_free, &i)))
889 goto error1; 904 goto error1;
890 XFS_WANT_CORRUPTED_GOTO(i == 1, 905 XFS_WANT_CORRUPTED_GOTO(i == 1,
891 error1); 906 error1);
892 } 907 }
893 } 908 }
894 } 909 }
895 ASSERT(!doneleft || !doneright); 910 ASSERT(!doneleft || !doneright);
896 } 911 }
897 } 912 }
898 /* 913 /*
899 * In a different a.g. from the parent. 914 * In a different a.g. from the parent.
900 * See if the most recently allocated block has any free. 915 * See if the most recently allocated block has any free.
901 */ 916 */
902 else if (be32_to_cpu(agi->agi_newino) != NULLAGINO) { 917 else if (be32_to_cpu(agi->agi_newino) != NULLAGINO) {
903 if ((error = xfs_inobt_lookup_eq(cur, 918 if ((error = xfs_inobt_lookup_eq(cur,
904 be32_to_cpu(agi->agi_newino), 0, 0, &i))) 919 be32_to_cpu(agi->agi_newino), 0, 0, &i)))
905 goto error0; 920 goto error0;
906 if (i == 1 && 921 if (i == 1 &&
907 (error = xfs_inobt_get_rec(cur, &rec.ir_startino, 922 (error = xfs_inobt_get_rec(cur, &rec.ir_startino,
908 &rec.ir_freecount, &rec.ir_free, &j)) == 0 && 923 &rec.ir_freecount, &rec.ir_free, &j)) == 0 &&
909 j == 1 && 924 j == 1 &&
910 rec.ir_freecount > 0) { 925 rec.ir_freecount > 0) {
911 /* 926 /*
912 * The last chunk allocated in the group still has 927 * The last chunk allocated in the group still has
913 * a free inode. 928 * a free inode.
914 */ 929 */
915 } 930 }
916 /* 931 /*
917 * None left in the last group, search the whole a.g. 932 * None left in the last group, search the whole a.g.
918 */ 933 */
919 else { 934 else {
920 if (error) 935 if (error)
921 goto error0; 936 goto error0;
922 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i))) 937 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
923 goto error0; 938 goto error0;
924 ASSERT(i == 1); 939 ASSERT(i == 1);
925 for (;;) { 940 for (;;) {
926 if ((error = xfs_inobt_get_rec(cur, 941 if ((error = xfs_inobt_get_rec(cur,
927 &rec.ir_startino, 942 &rec.ir_startino,
928 &rec.ir_freecount, &rec.ir_free, 943 &rec.ir_freecount, &rec.ir_free,
929 &i))) 944 &i)))
930 goto error0; 945 goto error0;
931 XFS_WANT_CORRUPTED_GOTO(i == 1, error0); 946 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
932 if (rec.ir_freecount > 0) 947 if (rec.ir_freecount > 0)
933 break; 948 break;
934 if ((error = xfs_btree_increment(cur, 0, &i))) 949 if ((error = xfs_btree_increment(cur, 0, &i)))
935 goto error0; 950 goto error0;
936 XFS_WANT_CORRUPTED_GOTO(i == 1, error0); 951 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
937 } 952 }
938 } 953 }
939 } 954 }
940 offset = xfs_ialloc_find_free(&rec.ir_free); 955 offset = xfs_ialloc_find_free(&rec.ir_free);
941 ASSERT(offset >= 0); 956 ASSERT(offset >= 0);
942 ASSERT(offset < XFS_INODES_PER_CHUNK); 957 ASSERT(offset < XFS_INODES_PER_CHUNK);
943 ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) % 958 ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
944 XFS_INODES_PER_CHUNK) == 0); 959 XFS_INODES_PER_CHUNK) == 0);
945 ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset); 960 ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
946 rec.ir_free &= ~XFS_INOBT_MASK(offset); 961 rec.ir_free &= ~XFS_INOBT_MASK(offset);
947 rec.ir_freecount--; 962 rec.ir_freecount--;
948 if ((error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount, 963 if ((error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount,
949 rec.ir_free))) 964 rec.ir_free)))
950 goto error0; 965 goto error0;
951 be32_add_cpu(&agi->agi_freecount, -1); 966 be32_add_cpu(&agi->agi_freecount, -1);
952 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT); 967 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
953 down_read(&mp->m_peraglock); 968 down_read(&mp->m_peraglock);
954 mp->m_perag[tagno].pagi_freecount--; 969 mp->m_perag[tagno].pagi_freecount--;
955 up_read(&mp->m_peraglock); 970 up_read(&mp->m_peraglock);
956 #ifdef DEBUG 971 #ifdef DEBUG
957 if (cur->bc_nlevels == 1) { 972 if (cur->bc_nlevels == 1) {
958 int freecount = 0; 973 int freecount = 0;
959 974
960 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i))) 975 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
961 goto error0; 976 goto error0;
962 do { 977 do {
963 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino, 978 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
964 &rec.ir_freecount, &rec.ir_free, &i))) 979 &rec.ir_freecount, &rec.ir_free, &i)))
965 goto error0; 980 goto error0;
966 XFS_WANT_CORRUPTED_GOTO(i == 1, error0); 981 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
967 freecount += rec.ir_freecount; 982 freecount += rec.ir_freecount;
968 if ((error = xfs_btree_increment(cur, 0, &i))) 983 if ((error = xfs_btree_increment(cur, 0, &i)))
969 goto error0; 984 goto error0;
970 } while (i == 1); 985 } while (i == 1);
971 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) || 986 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
972 XFS_FORCED_SHUTDOWN(mp)); 987 XFS_FORCED_SHUTDOWN(mp));
973 } 988 }
974 #endif 989 #endif
975 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); 990 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
976 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1); 991 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
977 *inop = ino; 992 *inop = ino;
978 return 0; 993 return 0;
979 error1: 994 error1:
980 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR); 995 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
981 error0: 996 error0:
982 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); 997 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
983 return error; 998 return error;
984 } 999 }
985 1000
986 /* 1001 /*
987 * Free disk inode. Carefully avoids touching the incore inode, all 1002 * Free disk inode. Carefully avoids touching the incore inode, all
988 * manipulations incore are the caller's responsibility. 1003 * manipulations incore are the caller's responsibility.
989 * The on-disk inode is not changed by this operation, only the 1004 * The on-disk inode is not changed by this operation, only the
990 * btree (free inode mask) is changed. 1005 * btree (free inode mask) is changed.
991 */ 1006 */
992 int 1007 int
993 xfs_difree( 1008 xfs_difree(
994 xfs_trans_t *tp, /* transaction pointer */ 1009 xfs_trans_t *tp, /* transaction pointer */
995 xfs_ino_t inode, /* inode to be freed */ 1010 xfs_ino_t inode, /* inode to be freed */
996 xfs_bmap_free_t *flist, /* extents to free */ 1011 xfs_bmap_free_t *flist, /* extents to free */
997 int *delete, /* set if inode cluster was deleted */ 1012 int *delete, /* set if inode cluster was deleted */
998 xfs_ino_t *first_ino) /* first inode in deleted cluster */ 1013 xfs_ino_t *first_ino) /* first inode in deleted cluster */
999 { 1014 {
1000 /* REFERENCED */ 1015 /* REFERENCED */
1001 xfs_agblock_t agbno; /* block number containing inode */ 1016 xfs_agblock_t agbno; /* block number containing inode */
1002 xfs_buf_t *agbp; /* buffer containing allocation group header */ 1017 xfs_buf_t *agbp; /* buffer containing allocation group header */
1003 xfs_agino_t agino; /* inode number relative to allocation group */ 1018 xfs_agino_t agino; /* inode number relative to allocation group */
1004 xfs_agnumber_t agno; /* allocation group number */ 1019 xfs_agnumber_t agno; /* allocation group number */
1005 xfs_agi_t *agi; /* allocation group header */ 1020 xfs_agi_t *agi; /* allocation group header */
1006 xfs_btree_cur_t *cur; /* inode btree cursor */ 1021 xfs_btree_cur_t *cur; /* inode btree cursor */
1007 int error; /* error return value */ 1022 int error; /* error return value */
1008 int i; /* result code */ 1023 int i; /* result code */
1009 int ilen; /* inodes in an inode cluster */ 1024 int ilen; /* inodes in an inode cluster */
1010 xfs_mount_t *mp; /* mount structure for filesystem */ 1025 xfs_mount_t *mp; /* mount structure for filesystem */
1011 int off; /* offset of inode in inode chunk */ 1026 int off; /* offset of inode in inode chunk */
1012 xfs_inobt_rec_incore_t rec; /* btree record */ 1027 xfs_inobt_rec_incore_t rec; /* btree record */
1013 1028
1014 mp = tp->t_mountp; 1029 mp = tp->t_mountp;
1015 1030
1016 /* 1031 /*
1017 * Break up inode number into its components. 1032 * Break up inode number into its components.
1018 */ 1033 */
1019 agno = XFS_INO_TO_AGNO(mp, inode); 1034 agno = XFS_INO_TO_AGNO(mp, inode);
1020 if (agno >= mp->m_sb.sb_agcount) { 1035 if (agno >= mp->m_sb.sb_agcount) {
1021 cmn_err(CE_WARN, 1036 cmn_err(CE_WARN,
1022 "xfs_difree: agno >= mp->m_sb.sb_agcount (%d >= %d) on %s. Returning EINVAL.", 1037 "xfs_difree: agno >= mp->m_sb.sb_agcount (%d >= %d) on %s. Returning EINVAL.",
1023 agno, mp->m_sb.sb_agcount, mp->m_fsname); 1038 agno, mp->m_sb.sb_agcount, mp->m_fsname);
1024 ASSERT(0); 1039 ASSERT(0);
1025 return XFS_ERROR(EINVAL); 1040 return XFS_ERROR(EINVAL);
1026 } 1041 }
1027 agino = XFS_INO_TO_AGINO(mp, inode); 1042 agino = XFS_INO_TO_AGINO(mp, inode);
1028 if (inode != XFS_AGINO_TO_INO(mp, agno, agino)) { 1043 if (inode != XFS_AGINO_TO_INO(mp, agno, agino)) {
1029 cmn_err(CE_WARN, 1044 cmn_err(CE_WARN,
1030 "xfs_difree: inode != XFS_AGINO_TO_INO() " 1045 "xfs_difree: inode != XFS_AGINO_TO_INO() "
1031 "(%llu != %llu) on %s. Returning EINVAL.", 1046 "(%llu != %llu) on %s. Returning EINVAL.",
1032 (unsigned long long)inode, 1047 (unsigned long long)inode,
1033 (unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino), 1048 (unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino),
1034 mp->m_fsname); 1049 mp->m_fsname);
1035 ASSERT(0); 1050 ASSERT(0);
1036 return XFS_ERROR(EINVAL); 1051 return XFS_ERROR(EINVAL);
1037 } 1052 }
1038 agbno = XFS_AGINO_TO_AGBNO(mp, agino); 1053 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1039 if (agbno >= mp->m_sb.sb_agblocks) { 1054 if (agbno >= mp->m_sb.sb_agblocks) {
1040 cmn_err(CE_WARN, 1055 cmn_err(CE_WARN,
1041 "xfs_difree: agbno >= mp->m_sb.sb_agblocks (%d >= %d) on %s. Returning EINVAL.", 1056 "xfs_difree: agbno >= mp->m_sb.sb_agblocks (%d >= %d) on %s. Returning EINVAL.",
1042 agbno, mp->m_sb.sb_agblocks, mp->m_fsname); 1057 agbno, mp->m_sb.sb_agblocks, mp->m_fsname);
1043 ASSERT(0); 1058 ASSERT(0);
1044 return XFS_ERROR(EINVAL); 1059 return XFS_ERROR(EINVAL);
1045 } 1060 }
1046 /* 1061 /*
1047 * Get the allocation group header. 1062 * Get the allocation group header.
1048 */ 1063 */
1049 down_read(&mp->m_peraglock); 1064 down_read(&mp->m_peraglock);
1050 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp); 1065 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1051 up_read(&mp->m_peraglock); 1066 up_read(&mp->m_peraglock);
1052 if (error) { 1067 if (error) {
1053 cmn_err(CE_WARN, 1068 cmn_err(CE_WARN,
1054 "xfs_difree: xfs_ialloc_read_agi() returned an error %d on %s. Returning error.", 1069 "xfs_difree: xfs_ialloc_read_agi() returned an error %d on %s. Returning error.",
1055 error, mp->m_fsname); 1070 error, mp->m_fsname);
1056 return error; 1071 return error;
1057 } 1072 }
1058 agi = XFS_BUF_TO_AGI(agbp); 1073 agi = XFS_BUF_TO_AGI(agbp);
1059 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC); 1074 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
1060 ASSERT(agbno < be32_to_cpu(agi->agi_length)); 1075 ASSERT(agbno < be32_to_cpu(agi->agi_length));
1061 /* 1076 /*
1062 * Initialize the cursor. 1077 * Initialize the cursor.
1063 */ 1078 */
1064 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno); 1079 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno);
1065 #ifdef DEBUG 1080 #ifdef DEBUG
1066 if (cur->bc_nlevels == 1) { 1081 if (cur->bc_nlevels == 1) {
1067 int freecount = 0; 1082 int freecount = 0;
1068 1083
1069 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i))) 1084 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
1070 goto error0; 1085 goto error0;
1071 do { 1086 do {
1072 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino, 1087 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
1073 &rec.ir_freecount, &rec.ir_free, &i))) 1088 &rec.ir_freecount, &rec.ir_free, &i)))
1074 goto error0; 1089 goto error0;
1075 if (i) { 1090 if (i) {
1076 freecount += rec.ir_freecount; 1091 freecount += rec.ir_freecount;
1077 if ((error = xfs_btree_increment(cur, 0, &i))) 1092 if ((error = xfs_btree_increment(cur, 0, &i)))
1078 goto error0; 1093 goto error0;
1079 } 1094 }
1080 } while (i == 1); 1095 } while (i == 1);
1081 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) || 1096 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
1082 XFS_FORCED_SHUTDOWN(mp)); 1097 XFS_FORCED_SHUTDOWN(mp));
1083 } 1098 }
1084 #endif 1099 #endif
1085 /* 1100 /*
1086 * Look for the entry describing this inode. 1101 * Look for the entry describing this inode.
1087 */ 1102 */
1088 if ((error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i))) { 1103 if ((error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i))) {
1089 cmn_err(CE_WARN, 1104 cmn_err(CE_WARN,
1090 "xfs_difree: xfs_inobt_lookup_le returned() an error %d on %s. Returning error.", 1105 "xfs_difree: xfs_inobt_lookup_le returned() an error %d on %s. Returning error.",
1091 error, mp->m_fsname); 1106 error, mp->m_fsname);
1092 goto error0; 1107 goto error0;
1093 } 1108 }
1094 XFS_WANT_CORRUPTED_GOTO(i == 1, error0); 1109 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1095 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino, &rec.ir_freecount, 1110 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino, &rec.ir_freecount,
1096 &rec.ir_free, &i))) { 1111 &rec.ir_free, &i))) {
1097 cmn_err(CE_WARN, 1112 cmn_err(CE_WARN,
1098 "xfs_difree: xfs_inobt_get_rec() returned an error %d on %s. Returning error.", 1113 "xfs_difree: xfs_inobt_get_rec() returned an error %d on %s. Returning error.",
1099 error, mp->m_fsname); 1114 error, mp->m_fsname);
1100 goto error0; 1115 goto error0;
1101 } 1116 }
1102 XFS_WANT_CORRUPTED_GOTO(i == 1, error0); 1117 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1103 /* 1118 /*
1104 * Get the offset in the inode chunk. 1119 * Get the offset in the inode chunk.
1105 */ 1120 */
1106 off = agino - rec.ir_startino; 1121 off = agino - rec.ir_startino;
1107 ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK); 1122 ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK);
1108 ASSERT(!(rec.ir_free & XFS_INOBT_MASK(off))); 1123 ASSERT(!(rec.ir_free & XFS_INOBT_MASK(off)));
1109 /* 1124 /*
1110 * Mark the inode free & increment the count. 1125 * Mark the inode free & increment the count.
1111 */ 1126 */
1112 rec.ir_free |= XFS_INOBT_MASK(off); 1127 rec.ir_free |= XFS_INOBT_MASK(off);
1113 rec.ir_freecount++; 1128 rec.ir_freecount++;
1114 1129
1115 /* 1130 /*
1116 * When an inode cluster is free, it becomes eligible for removal 1131 * When an inode cluster is free, it becomes eligible for removal
1117 */ 1132 */
1118 if (!(mp->m_flags & XFS_MOUNT_IKEEP) && 1133 if (!(mp->m_flags & XFS_MOUNT_IKEEP) &&
1119 (rec.ir_freecount == XFS_IALLOC_INODES(mp))) { 1134 (rec.ir_freecount == XFS_IALLOC_INODES(mp))) {
1120 1135
1121 *delete = 1; 1136 *delete = 1;
1122 *first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino); 1137 *first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino);
1123 1138
1124 /* 1139 /*
1125 * Remove the inode cluster from the AGI B+Tree, adjust the 1140 * Remove the inode cluster from the AGI B+Tree, adjust the
1126 * AGI and Superblock inode counts, and mark the disk space 1141 * AGI and Superblock inode counts, and mark the disk space
1127 * to be freed when the transaction is committed. 1142 * to be freed when the transaction is committed.
1128 */ 1143 */
1129 ilen = XFS_IALLOC_INODES(mp); 1144 ilen = XFS_IALLOC_INODES(mp);
1130 be32_add_cpu(&agi->agi_count, -ilen); 1145 be32_add_cpu(&agi->agi_count, -ilen);
1131 be32_add_cpu(&agi->agi_freecount, -(ilen - 1)); 1146 be32_add_cpu(&agi->agi_freecount, -(ilen - 1));
1132 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT); 1147 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT);
1133 down_read(&mp->m_peraglock); 1148 down_read(&mp->m_peraglock);
1134 mp->m_perag[agno].pagi_freecount -= ilen - 1; 1149 mp->m_perag[agno].pagi_freecount -= ilen - 1;
1135 up_read(&mp->m_peraglock); 1150 up_read(&mp->m_peraglock);
1136 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen); 1151 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen);
1137 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1)); 1152 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1));
1138 1153
1139 if ((error = xfs_btree_delete(cur, &i))) { 1154 if ((error = xfs_btree_delete(cur, &i))) {
1140 cmn_err(CE_WARN, "xfs_difree: xfs_btree_delete returned an error %d on %s.\n", 1155 cmn_err(CE_WARN, "xfs_difree: xfs_btree_delete returned an error %d on %s.\n",
1141 error, mp->m_fsname); 1156 error, mp->m_fsname);
1142 goto error0; 1157 goto error0;
1143 } 1158 }
1144 1159
1145 xfs_bmap_add_free(XFS_AGB_TO_FSB(mp, 1160 xfs_bmap_add_free(XFS_AGB_TO_FSB(mp,
1146 agno, XFS_INO_TO_AGBNO(mp,rec.ir_startino)), 1161 agno, XFS_INO_TO_AGBNO(mp,rec.ir_startino)),
1147 XFS_IALLOC_BLOCKS(mp), flist, mp); 1162 XFS_IALLOC_BLOCKS(mp), flist, mp);
1148 } else { 1163 } else {
1149 *delete = 0; 1164 *delete = 0;
1150 1165
1151 if ((error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount, rec.ir_free))) { 1166 if ((error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount, rec.ir_free))) {
1152 cmn_err(CE_WARN, 1167 cmn_err(CE_WARN,
1153 "xfs_difree: xfs_inobt_update() returned an error %d on %s. Returning error.", 1168 "xfs_difree: xfs_inobt_update() returned an error %d on %s. Returning error.",
1154 error, mp->m_fsname); 1169 error, mp->m_fsname);
1155 goto error0; 1170 goto error0;
1156 } 1171 }
1157 /* 1172 /*
1158 * Change the inode free counts and log the ag/sb changes. 1173 * Change the inode free counts and log the ag/sb changes.
1159 */ 1174 */
1160 be32_add_cpu(&agi->agi_freecount, 1); 1175 be32_add_cpu(&agi->agi_freecount, 1);
1161 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT); 1176 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
1162 down_read(&mp->m_peraglock); 1177 down_read(&mp->m_peraglock);
1163 mp->m_perag[agno].pagi_freecount++; 1178 mp->m_perag[agno].pagi_freecount++;
1164 up_read(&mp->m_peraglock); 1179 up_read(&mp->m_peraglock);
1165 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1); 1180 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1);
1166 } 1181 }
1167 1182
1168 #ifdef DEBUG 1183 #ifdef DEBUG
1169 if (cur->bc_nlevels == 1) { 1184 if (cur->bc_nlevels == 1) {
1170 int freecount = 0; 1185 int freecount = 0;
1171 1186
1172 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i))) 1187 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
1173 goto error0; 1188 goto error0;
1174 do { 1189 do {
1175 if ((error = xfs_inobt_get_rec(cur, 1190 if ((error = xfs_inobt_get_rec(cur,
1176 &rec.ir_startino, 1191 &rec.ir_startino,
1177 &rec.ir_freecount, 1192 &rec.ir_freecount,
1178 &rec.ir_free, &i))) 1193 &rec.ir_free, &i)))
1179 goto error0; 1194 goto error0;
1180 if (i) { 1195 if (i) {
1181 freecount += rec.ir_freecount; 1196 freecount += rec.ir_freecount;
1182 if ((error = xfs_btree_increment(cur, 0, &i))) 1197 if ((error = xfs_btree_increment(cur, 0, &i)))
1183 goto error0; 1198 goto error0;
1184 } 1199 }
1185 } while (i == 1); 1200 } while (i == 1);
1186 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) || 1201 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
1187 XFS_FORCED_SHUTDOWN(mp)); 1202 XFS_FORCED_SHUTDOWN(mp));
1188 } 1203 }
1189 #endif 1204 #endif
1190 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); 1205 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1191 return 0; 1206 return 0;
1192 1207
1193 error0: 1208 error0:
1194 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); 1209 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1195 return error; 1210 return error;
1196 } 1211 }
1197 1212
1198 /* 1213 /*
1199 * Return the location of the inode in imap, for mapping it into a buffer. 1214 * Return the location of the inode in imap, for mapping it into a buffer.
1200 */ 1215 */
1201 int 1216 int
1202 xfs_imap( 1217 xfs_imap(
1203 xfs_mount_t *mp, /* file system mount structure */ 1218 xfs_mount_t *mp, /* file system mount structure */
1204 xfs_trans_t *tp, /* transaction pointer */ 1219 xfs_trans_t *tp, /* transaction pointer */
1205 xfs_ino_t ino, /* inode to locate */ 1220 xfs_ino_t ino, /* inode to locate */
1206 struct xfs_imap *imap, /* location map structure */ 1221 struct xfs_imap *imap, /* location map structure */
1207 uint flags) /* flags for inode btree lookup */ 1222 uint flags) /* flags for inode btree lookup */
1208 { 1223 {
1209 xfs_agblock_t agbno; /* block number of inode in the alloc group */ 1224 xfs_agblock_t agbno; /* block number of inode in the alloc group */
1210 xfs_agino_t agino; /* inode number within alloc group */ 1225 xfs_agino_t agino; /* inode number within alloc group */
1211 xfs_agnumber_t agno; /* allocation group number */ 1226 xfs_agnumber_t agno; /* allocation group number */
1212 int blks_per_cluster; /* num blocks per inode cluster */ 1227 int blks_per_cluster; /* num blocks per inode cluster */
1213 xfs_agblock_t chunk_agbno; /* first block in inode chunk */ 1228 xfs_agblock_t chunk_agbno; /* first block in inode chunk */
1214 xfs_agblock_t cluster_agbno; /* first block in inode cluster */ 1229 xfs_agblock_t cluster_agbno; /* first block in inode cluster */
1215 int error; /* error code */ 1230 int error; /* error code */
1216 int offset; /* index of inode in its buffer */ 1231 int offset; /* index of inode in its buffer */
1217 int offset_agbno; /* blks from chunk start to inode */ 1232 int offset_agbno; /* blks from chunk start to inode */
1218 1233
1219 ASSERT(ino != NULLFSINO); 1234 ASSERT(ino != NULLFSINO);
1220 1235
1221 /* 1236 /*
1222 * Split up the inode number into its parts. 1237 * Split up the inode number into its parts.
1223 */ 1238 */
1224 agno = XFS_INO_TO_AGNO(mp, ino); 1239 agno = XFS_INO_TO_AGNO(mp, ino);
1225 agino = XFS_INO_TO_AGINO(mp, ino); 1240 agino = XFS_INO_TO_AGINO(mp, ino);
1226 agbno = XFS_AGINO_TO_AGBNO(mp, agino); 1241 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1227 if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks || 1242 if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks ||
1228 ino != XFS_AGINO_TO_INO(mp, agno, agino)) { 1243 ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1229 #ifdef DEBUG 1244 #ifdef DEBUG
1230 /* no diagnostics for bulkstat, ino comes from userspace */ 1245 /* no diagnostics for bulkstat, ino comes from userspace */
1231 if (flags & XFS_IGET_BULKSTAT) 1246 if (flags & XFS_IGET_BULKSTAT)
1232 return XFS_ERROR(EINVAL); 1247 return XFS_ERROR(EINVAL);
1233 if (agno >= mp->m_sb.sb_agcount) { 1248 if (agno >= mp->m_sb.sb_agcount) {
1234 xfs_fs_cmn_err(CE_ALERT, mp, 1249 xfs_fs_cmn_err(CE_ALERT, mp,
1235 "xfs_imap: agno (%d) >= " 1250 "xfs_imap: agno (%d) >= "
1236 "mp->m_sb.sb_agcount (%d)", 1251 "mp->m_sb.sb_agcount (%d)",
1237 agno, mp->m_sb.sb_agcount); 1252 agno, mp->m_sb.sb_agcount);
1238 } 1253 }
1239 if (agbno >= mp->m_sb.sb_agblocks) { 1254 if (agbno >= mp->m_sb.sb_agblocks) {
1240 xfs_fs_cmn_err(CE_ALERT, mp, 1255 xfs_fs_cmn_err(CE_ALERT, mp,
1241 "xfs_imap: agbno (0x%llx) >= " 1256 "xfs_imap: agbno (0x%llx) >= "
1242 "mp->m_sb.sb_agblocks (0x%lx)", 1257 "mp->m_sb.sb_agblocks (0x%lx)",
1243 (unsigned long long) agbno, 1258 (unsigned long long) agbno,
1244 (unsigned long) mp->m_sb.sb_agblocks); 1259 (unsigned long) mp->m_sb.sb_agblocks);
1245 } 1260 }
1246 if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) { 1261 if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1247 xfs_fs_cmn_err(CE_ALERT, mp, 1262 xfs_fs_cmn_err(CE_ALERT, mp,
1248 "xfs_imap: ino (0x%llx) != " 1263 "xfs_imap: ino (0x%llx) != "
1249 "XFS_AGINO_TO_INO(mp, agno, agino) " 1264 "XFS_AGINO_TO_INO(mp, agno, agino) "
1250 "(0x%llx)", 1265 "(0x%llx)",
1251 ino, XFS_AGINO_TO_INO(mp, agno, agino)); 1266 ino, XFS_AGINO_TO_INO(mp, agno, agino));
1252 } 1267 }
1253 xfs_stack_trace(); 1268 xfs_stack_trace();
1254 #endif /* DEBUG */ 1269 #endif /* DEBUG */
1255 return XFS_ERROR(EINVAL); 1270 return XFS_ERROR(EINVAL);
1256 } 1271 }
1257 1272
1258 /* 1273 /*
1259 * If the inode cluster size is the same as the blocksize or 1274 * If the inode cluster size is the same as the blocksize or
1260 * smaller we get to the buffer by simple arithmetics. 1275 * smaller we get to the buffer by simple arithmetics.
1261 */ 1276 */
1262 if (XFS_INODE_CLUSTER_SIZE(mp) <= mp->m_sb.sb_blocksize) { 1277 if (XFS_INODE_CLUSTER_SIZE(mp) <= mp->m_sb.sb_blocksize) {
1263 offset = XFS_INO_TO_OFFSET(mp, ino); 1278 offset = XFS_INO_TO_OFFSET(mp, ino);
1264 ASSERT(offset < mp->m_sb.sb_inopblock); 1279 ASSERT(offset < mp->m_sb.sb_inopblock);
1265 1280
1266 imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, agbno); 1281 imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, agbno);
1267 imap->im_len = XFS_FSB_TO_BB(mp, 1); 1282 imap->im_len = XFS_FSB_TO_BB(mp, 1);
1268 imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog); 1283 imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
1269 return 0; 1284 return 0;
1270 } 1285 }
1271 1286
1272 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_blocklog; 1287 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_blocklog;
1273 1288
1274 /* 1289 /*
1275 * If we get a block number passed from bulkstat we can use it to 1290 * If we get a block number passed from bulkstat we can use it to
1276 * find the buffer easily. 1291 * find the buffer easily.
1277 */ 1292 */
1278 if (imap->im_blkno) { 1293 if (imap->im_blkno) {
1279 offset = XFS_INO_TO_OFFSET(mp, ino); 1294 offset = XFS_INO_TO_OFFSET(mp, ino);
1280 ASSERT(offset < mp->m_sb.sb_inopblock); 1295 ASSERT(offset < mp->m_sb.sb_inopblock);
1281 1296
1282 cluster_agbno = xfs_daddr_to_agbno(mp, imap->im_blkno); 1297 cluster_agbno = xfs_daddr_to_agbno(mp, imap->im_blkno);
1283 offset += (agbno - cluster_agbno) * mp->m_sb.sb_inopblock; 1298 offset += (agbno - cluster_agbno) * mp->m_sb.sb_inopblock;
1284 1299
1285 imap->im_len = XFS_FSB_TO_BB(mp, blks_per_cluster); 1300 imap->im_len = XFS_FSB_TO_BB(mp, blks_per_cluster);
1286 imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog); 1301 imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
1287 return 0; 1302 return 0;
1288 } 1303 }
1289 1304
1290 /* 1305 /*
1291 * If the inode chunks are aligned then use simple maths to 1306 * If the inode chunks are aligned then use simple maths to
1292 * find the location. Otherwise we have to do a btree 1307 * find the location. Otherwise we have to do a btree
1293 * lookup to find the location. 1308 * lookup to find the location.
1294 */ 1309 */
1295 if (mp->m_inoalign_mask) { 1310 if (mp->m_inoalign_mask) {
1296 offset_agbno = agbno & mp->m_inoalign_mask; 1311 offset_agbno = agbno & mp->m_inoalign_mask;
1297 chunk_agbno = agbno - offset_agbno; 1312 chunk_agbno = agbno - offset_agbno;
1298 } else { 1313 } else {
1299 xfs_btree_cur_t *cur; /* inode btree cursor */ 1314 xfs_btree_cur_t *cur; /* inode btree cursor */
1300 xfs_agino_t chunk_agino; /* first agino in inode chunk */ 1315 xfs_agino_t chunk_agino; /* first agino in inode chunk */
1301 __int32_t chunk_cnt; /* count of free inodes in chunk */ 1316 __int32_t chunk_cnt; /* count of free inodes in chunk */
1302 xfs_inofree_t chunk_free; /* mask of free inodes in chunk */ 1317 xfs_inofree_t chunk_free; /* mask of free inodes in chunk */
1303 xfs_buf_t *agbp; /* agi buffer */ 1318 xfs_buf_t *agbp; /* agi buffer */
1304 int i; /* temp state */ 1319 int i; /* temp state */
1305 1320
1306 down_read(&mp->m_peraglock); 1321 down_read(&mp->m_peraglock);
1307 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp); 1322 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1308 up_read(&mp->m_peraglock); 1323 up_read(&mp->m_peraglock);
1309 if (error) { 1324 if (error) {
1310 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: " 1325 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: "
1311 "xfs_ialloc_read_agi() returned " 1326 "xfs_ialloc_read_agi() returned "
1312 "error %d, agno %d", 1327 "error %d, agno %d",
1313 error, agno); 1328 error, agno);
1314 return error; 1329 return error;
1315 } 1330 }
1316 1331
1317 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno); 1332 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno);
1318 error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i); 1333 error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i);
1319 if (error) { 1334 if (error) {
1320 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: " 1335 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: "
1321 "xfs_inobt_lookup_le() failed"); 1336 "xfs_inobt_lookup_le() failed");
1322 goto error0; 1337 goto error0;
1323 } 1338 }
1324 1339
1325 error = xfs_inobt_get_rec(cur, &chunk_agino, &chunk_cnt, 1340 error = xfs_inobt_get_rec(cur, &chunk_agino, &chunk_cnt,
1326 &chunk_free, &i); 1341 &chunk_free, &i);
1327 if (error) { 1342 if (error) {
1328 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: " 1343 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: "
1329 "xfs_inobt_get_rec() failed"); 1344 "xfs_inobt_get_rec() failed");
1330 goto error0; 1345 goto error0;
1331 } 1346 }
1332 if (i == 0) { 1347 if (i == 0) {
1333 #ifdef DEBUG 1348 #ifdef DEBUG
1334 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: " 1349 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: "
1335 "xfs_inobt_get_rec() failed"); 1350 "xfs_inobt_get_rec() failed");
1336 #endif /* DEBUG */ 1351 #endif /* DEBUG */
1337 error = XFS_ERROR(EINVAL); 1352 error = XFS_ERROR(EINVAL);
1338 } 1353 }
1339 error0: 1354 error0:
1340 xfs_trans_brelse(tp, agbp); 1355 xfs_trans_brelse(tp, agbp);
1341 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); 1356 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1342 if (error) 1357 if (error)
1343 return error; 1358 return error;
1344 chunk_agbno = XFS_AGINO_TO_AGBNO(mp, chunk_agino); 1359 chunk_agbno = XFS_AGINO_TO_AGBNO(mp, chunk_agino);
1345 offset_agbno = agbno - chunk_agbno; 1360 offset_agbno = agbno - chunk_agbno;
1346 } 1361 }
1347 1362
1348 ASSERT(agbno >= chunk_agbno); 1363 ASSERT(agbno >= chunk_agbno);
1349 cluster_agbno = chunk_agbno + 1364 cluster_agbno = chunk_agbno +
1350 ((offset_agbno / blks_per_cluster) * blks_per_cluster); 1365 ((offset_agbno / blks_per_cluster) * blks_per_cluster);
1351 offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) + 1366 offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
1352 XFS_INO_TO_OFFSET(mp, ino); 1367 XFS_INO_TO_OFFSET(mp, ino);
1353 1368
1354 imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, cluster_agbno); 1369 imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, cluster_agbno);
1355 imap->im_len = XFS_FSB_TO_BB(mp, blks_per_cluster); 1370 imap->im_len = XFS_FSB_TO_BB(mp, blks_per_cluster);
1356 imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog); 1371 imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
1357 1372
1358 /* 1373 /*
1359 * If the inode number maps to a block outside the bounds 1374 * If the inode number maps to a block outside the bounds
1360 * of the file system then return NULL rather than calling 1375 * of the file system then return NULL rather than calling
1361 * read_buf and panicing when we get an error from the 1376 * read_buf and panicing when we get an error from the
1362 * driver. 1377 * driver.
1363 */ 1378 */
1364 if ((imap->im_blkno + imap->im_len) > 1379 if ((imap->im_blkno + imap->im_len) >
1365 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) { 1380 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) {
1366 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: " 1381 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: "
1367 "(imap->im_blkno (0x%llx) + imap->im_len (0x%llx)) > " 1382 "(imap->im_blkno (0x%llx) + imap->im_len (0x%llx)) > "
1368 " XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks) (0x%llx)", 1383 " XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks) (0x%llx)",
1369 (unsigned long long) imap->im_blkno, 1384 (unsigned long long) imap->im_blkno,
1370 (unsigned long long) imap->im_len, 1385 (unsigned long long) imap->im_len,
1371 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)); 1386 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks));
1372 return XFS_ERROR(EINVAL); 1387 return XFS_ERROR(EINVAL);
1373 } 1388 }
1374 1389
1375 return 0; 1390 return 0;
1376 } 1391 }
1377 1392
1378 /* 1393 /*
1379 * Compute and fill in value of m_in_maxlevels. 1394 * Compute and fill in value of m_in_maxlevels.
1380 */ 1395 */
1381 void 1396 void
1382 xfs_ialloc_compute_maxlevels( 1397 xfs_ialloc_compute_maxlevels(
1383 xfs_mount_t *mp) /* file system mount structure */ 1398 xfs_mount_t *mp) /* file system mount structure */
1384 { 1399 {
1385 int level; 1400 int level;
1386 uint maxblocks; 1401 uint maxblocks;
1387 uint maxleafents; 1402 uint maxleafents;
1388 int minleafrecs; 1403 int minleafrecs;
1389 int minnoderecs; 1404 int minnoderecs;
1390 1405
1391 maxleafents = (1LL << XFS_INO_AGINO_BITS(mp)) >> 1406 maxleafents = (1LL << XFS_INO_AGINO_BITS(mp)) >>
1392 XFS_INODES_PER_CHUNK_LOG; 1407 XFS_INODES_PER_CHUNK_LOG;
1393 minleafrecs = mp->m_alloc_mnr[0]; 1408 minleafrecs = mp->m_alloc_mnr[0];
1394 minnoderecs = mp->m_alloc_mnr[1]; 1409 minnoderecs = mp->m_alloc_mnr[1];
1395 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs; 1410 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
1396 for (level = 1; maxblocks > 1; level++) 1411 for (level = 1; maxblocks > 1; level++)
1397 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs; 1412 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
1398 mp->m_in_maxlevels = level; 1413 mp->m_in_maxlevels = level;
1399 } 1414 }
1400 1415
1401 /* 1416 /*
1402 * Log specified fields for the ag hdr (inode section) 1417 * Log specified fields for the ag hdr (inode section)
1403 */ 1418 */
1404 void 1419 void
1405 xfs_ialloc_log_agi( 1420 xfs_ialloc_log_agi(
1406 xfs_trans_t *tp, /* transaction pointer */ 1421 xfs_trans_t *tp, /* transaction pointer */
1407 xfs_buf_t *bp, /* allocation group header buffer */ 1422 xfs_buf_t *bp, /* allocation group header buffer */
1408 int fields) /* bitmask of fields to log */ 1423 int fields) /* bitmask of fields to log */
1409 { 1424 {
1410 int first; /* first byte number */ 1425 int first; /* first byte number */
1411 int last; /* last byte number */ 1426 int last; /* last byte number */
1412 static const short offsets[] = { /* field starting offsets */ 1427 static const short offsets[] = { /* field starting offsets */
1413 /* keep in sync with bit definitions */ 1428 /* keep in sync with bit definitions */
1414 offsetof(xfs_agi_t, agi_magicnum), 1429 offsetof(xfs_agi_t, agi_magicnum),
1415 offsetof(xfs_agi_t, agi_versionnum), 1430 offsetof(xfs_agi_t, agi_versionnum),
1416 offsetof(xfs_agi_t, agi_seqno), 1431 offsetof(xfs_agi_t, agi_seqno),
1417 offsetof(xfs_agi_t, agi_length), 1432 offsetof(xfs_agi_t, agi_length),
1418 offsetof(xfs_agi_t, agi_count), 1433 offsetof(xfs_agi_t, agi_count),
1419 offsetof(xfs_agi_t, agi_root), 1434 offsetof(xfs_agi_t, agi_root),
1420 offsetof(xfs_agi_t, agi_level), 1435 offsetof(xfs_agi_t, agi_level),
1421 offsetof(xfs_agi_t, agi_freecount), 1436 offsetof(xfs_agi_t, agi_freecount),
1422 offsetof(xfs_agi_t, agi_newino), 1437 offsetof(xfs_agi_t, agi_newino),
1423 offsetof(xfs_agi_t, agi_dirino), 1438 offsetof(xfs_agi_t, agi_dirino),
1424 offsetof(xfs_agi_t, agi_unlinked), 1439 offsetof(xfs_agi_t, agi_unlinked),
1425 sizeof(xfs_agi_t) 1440 sizeof(xfs_agi_t)
1426 }; 1441 };
1427 #ifdef DEBUG 1442 #ifdef DEBUG
1428 xfs_agi_t *agi; /* allocation group header */ 1443 xfs_agi_t *agi; /* allocation group header */
1429 1444
1430 agi = XFS_BUF_TO_AGI(bp); 1445 agi = XFS_BUF_TO_AGI(bp);
1431 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC); 1446 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
1432 #endif 1447 #endif
1433 /* 1448 /*
1434 * Compute byte offsets for the first and last fields. 1449 * Compute byte offsets for the first and last fields.
1435 */ 1450 */
1436 xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS, &first, &last); 1451 xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS, &first, &last);
1437 /* 1452 /*
1438 * Log the allocation group inode header buffer. 1453 * Log the allocation group inode header buffer.
1439 */ 1454 */
1440 xfs_trans_log_buf(tp, bp, first, last); 1455 xfs_trans_log_buf(tp, bp, first, last);
1441 } 1456 }
1442 1457
1443 #ifdef DEBUG 1458 #ifdef DEBUG
1444 STATIC void 1459 STATIC void
1445 xfs_check_agi_unlinked( 1460 xfs_check_agi_unlinked(
1446 struct xfs_agi *agi) 1461 struct xfs_agi *agi)
1447 { 1462 {
1448 int i; 1463 int i;
1449 1464
1450 for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++) 1465 for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++)
1451 ASSERT(agi->agi_unlinked[i]); 1466 ASSERT(agi->agi_unlinked[i]);
1452 } 1467 }
1453 #else 1468 #else
1454 #define xfs_check_agi_unlinked(agi) 1469 #define xfs_check_agi_unlinked(agi)
1455 #endif 1470 #endif
1456 1471
1457 /* 1472 /*
1458 * Read in the allocation group header (inode allocation section) 1473 * Read in the allocation group header (inode allocation section)
1459 */ 1474 */
1460 int 1475 int
1461 xfs_read_agi( 1476 xfs_read_agi(
1462 struct xfs_mount *mp, /* file system mount structure */ 1477 struct xfs_mount *mp, /* file system mount structure */
1463 struct xfs_trans *tp, /* transaction pointer */ 1478 struct xfs_trans *tp, /* transaction pointer */
1464 xfs_agnumber_t agno, /* allocation group number */ 1479 xfs_agnumber_t agno, /* allocation group number */
1465 struct xfs_buf **bpp) /* allocation group hdr buf */ 1480 struct xfs_buf **bpp) /* allocation group hdr buf */
1466 { 1481 {
1467 struct xfs_agi *agi; /* allocation group header */ 1482 struct xfs_agi *agi; /* allocation group header */
1468 int agi_ok; /* agi is consistent */ 1483 int agi_ok; /* agi is consistent */
1469 int error; 1484 int error;
1470 1485
1471 ASSERT(agno != NULLAGNUMBER); 1486 ASSERT(agno != NULLAGNUMBER);
1472 1487
1473 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, 1488 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
1474 XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)), 1489 XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
1475 XFS_FSS_TO_BB(mp, 1), 0, bpp); 1490 XFS_FSS_TO_BB(mp, 1), 0, bpp);
1476 if (error) 1491 if (error)
1477 return error; 1492 return error;
1478 1493
1479 ASSERT(*bpp && !XFS_BUF_GETERROR(*bpp)); 1494 ASSERT(*bpp && !XFS_BUF_GETERROR(*bpp));
1480 agi = XFS_BUF_TO_AGI(*bpp); 1495 agi = XFS_BUF_TO_AGI(*bpp);
1481 1496
1482 /* 1497 /*
1483 * Validate the magic number of the agi block. 1498 * Validate the magic number of the agi block.
1484 */ 1499 */