blob: 72fa3bfc56ebe7e2d3707f68dd27dd6e4045b94a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_dir2.h"
28#include "xfs_dmapi.h"
29#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110031#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110034#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_dinode.h"
36#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110037#include "xfs_btree.h"
38#include "xfs_ialloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "xfs_alloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "xfs_rtalloc.h"
41#include "xfs_error.h"
42#include "xfs_bmap.h"
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45/*
46 * Allocation group level functions.
47 */
David Chinner75de2a92008-03-27 18:00:38 +110048static inline int
49xfs_ialloc_cluster_alignment(
50 xfs_alloc_arg_t *args)
51{
52 if (xfs_sb_version_hasalign(&args->mp->m_sb) &&
53 args->mp->m_sb.sb_inoalignmt >=
54 XFS_B_TO_FSBT(args->mp, XFS_INODE_CLUSTER_SIZE(args->mp)))
55 return args->mp->m_sb.sb_inoalignmt;
56 return 1;
57}
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59/*
Christoph Hellwigfe033cc2008-10-30 16:56:09 +110060 * Lookup the record equal to ino in the btree given by cur.
61 */
62STATIC int /* error */
63xfs_inobt_lookup_eq(
64 struct xfs_btree_cur *cur, /* btree cursor */
65 xfs_agino_t ino, /* starting inode of chunk */
66 __int32_t fcnt, /* free inode count */
67 xfs_inofree_t free, /* free inode mask */
68 int *stat) /* success/failure */
69{
70 cur->bc_rec.i.ir_startino = ino;
71 cur->bc_rec.i.ir_freecount = fcnt;
72 cur->bc_rec.i.ir_free = free;
73 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
74}
75
76/*
77 * Lookup the first record greater than or equal to ino
78 * in the btree given by cur.
79 */
80int /* error */
81xfs_inobt_lookup_ge(
82 struct xfs_btree_cur *cur, /* btree cursor */
83 xfs_agino_t ino, /* starting inode of chunk */
84 __int32_t fcnt, /* free inode count */
85 xfs_inofree_t free, /* free inode mask */
86 int *stat) /* success/failure */
87{
88 cur->bc_rec.i.ir_startino = ino;
89 cur->bc_rec.i.ir_freecount = fcnt;
90 cur->bc_rec.i.ir_free = free;
91 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
92}
93
94/*
95 * Lookup the first record less than or equal to ino
96 * in the btree given by cur.
97 */
98int /* error */
99xfs_inobt_lookup_le(
100 struct xfs_btree_cur *cur, /* btree cursor */
101 xfs_agino_t ino, /* starting inode of chunk */
102 __int32_t fcnt, /* free inode count */
103 xfs_inofree_t free, /* free inode mask */
104 int *stat) /* success/failure */
105{
106 cur->bc_rec.i.ir_startino = ino;
107 cur->bc_rec.i.ir_freecount = fcnt;
108 cur->bc_rec.i.ir_free = free;
109 return xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat);
110}
111
112/*
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100113 * Update the record referred to by cur to the value given
114 * by [ino, fcnt, free].
115 * This either works (return 0) or gets an EFSCORRUPTED error.
116 */
117STATIC int /* error */
118xfs_inobt_update(
119 struct xfs_btree_cur *cur, /* btree cursor */
120 xfs_agino_t ino, /* starting inode of chunk */
121 __int32_t fcnt, /* free inode count */
122 xfs_inofree_t free) /* free inode mask */
123{
124 union xfs_btree_rec rec;
125
126 rec.inobt.ir_startino = cpu_to_be32(ino);
127 rec.inobt.ir_freecount = cpu_to_be32(fcnt);
128 rec.inobt.ir_free = cpu_to_be64(free);
129 return xfs_btree_update(cur, &rec);
130}
131
132/*
Christoph Hellwig8cc938f2008-10-30 16:58:11 +1100133 * Get the data from the pointed-to record.
134 */
135int /* error */
136xfs_inobt_get_rec(
137 struct xfs_btree_cur *cur, /* btree cursor */
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300138 xfs_inobt_rec_incore_t *irec, /* btree record */
Christoph Hellwig8cc938f2008-10-30 16:58:11 +1100139 int *stat) /* output: success/failure */
140{
141 union xfs_btree_rec *rec;
142 int error;
143
144 error = xfs_btree_get_rec(cur, &rec, stat);
145 if (!error && *stat == 1) {
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300146 irec->ir_startino = be32_to_cpu(rec->inobt.ir_startino);
147 irec->ir_freecount = be32_to_cpu(rec->inobt.ir_freecount);
148 irec->ir_free = be64_to_cpu(rec->inobt.ir_free);
Christoph Hellwig8cc938f2008-10-30 16:58:11 +1100149 }
150 return error;
151}
152
153/*
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300154 * Initialise a new set of inodes.
155 */
156STATIC void
157xfs_ialloc_inode_init(
158 struct xfs_mount *mp,
159 struct xfs_trans *tp,
160 xfs_agnumber_t agno,
161 xfs_agblock_t agbno,
162 xfs_agblock_t length,
163 unsigned int gen)
164{
165 struct xfs_buf *fbuf;
166 struct xfs_dinode *free;
167 int blks_per_cluster, nbufs, ninodes;
168 int version;
169 int i, j;
170 xfs_daddr_t d;
171
172 /*
173 * Loop over the new block(s), filling in the inodes.
174 * For small block sizes, manipulate the inodes in buffers
175 * which are multiples of the blocks size.
176 */
177 if (mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) {
178 blks_per_cluster = 1;
179 nbufs = length;
180 ninodes = mp->m_sb.sb_inopblock;
181 } else {
182 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) /
183 mp->m_sb.sb_blocksize;
184 nbufs = length / blks_per_cluster;
185 ninodes = blks_per_cluster * mp->m_sb.sb_inopblock;
186 }
187
188 /*
189 * Figure out what version number to use in the inodes we create.
190 * If the superblock version has caught up to the one that supports
191 * the new inode format, then use the new inode version. Otherwise
192 * use the old version so that old kernels will continue to be
193 * able to use the file system.
194 */
195 if (xfs_sb_version_hasnlink(&mp->m_sb))
196 version = 2;
197 else
198 version = 1;
199
200 for (j = 0; j < nbufs; j++) {
201 /*
202 * Get the block.
203 */
204 d = XFS_AGB_TO_DADDR(mp, agno, agbno + (j * blks_per_cluster));
205 fbuf = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
206 mp->m_bsize * blks_per_cluster,
207 XFS_BUF_LOCK);
208 ASSERT(fbuf);
209 ASSERT(!XFS_BUF_GETERROR(fbuf));
210
211 /*
212 * Initialize all inodes in this buffer and then log them.
213 *
214 * XXX: It would be much better if we had just one transaction
215 * to log a whole cluster of inodes instead of all the
216 * individual transactions causing a lot of log traffic.
217 */
218 xfs_biozero(fbuf, 0, ninodes << mp->m_sb.sb_inodelog);
219 for (i = 0; i < ninodes; i++) {
220 int ioffset = i << mp->m_sb.sb_inodelog;
221 uint isize = sizeof(struct xfs_dinode);
222
223 free = xfs_make_iptr(mp, fbuf, i);
224 free->di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
225 free->di_version = version;
226 free->di_gen = cpu_to_be32(gen);
227 free->di_next_unlinked = cpu_to_be32(NULLAGINO);
228 xfs_trans_log_buf(tp, fbuf, ioffset, ioffset + isize - 1);
229 }
230 xfs_trans_inode_alloc_buf(tp, fbuf);
231 }
232}
233
234/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 * Allocate new inodes in the allocation group specified by agbp.
236 * Return 0 for success, else error code.
237 */
238STATIC int /* error code or 0 */
239xfs_ialloc_ag_alloc(
240 xfs_trans_t *tp, /* transaction pointer */
241 xfs_buf_t *agbp, /* alloc group buffer */
242 int *alloc)
243{
244 xfs_agi_t *agi; /* allocation group header */
245 xfs_alloc_arg_t args; /* allocation argument structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 xfs_btree_cur_t *cur; /* inode btree cursor */
David Chinner92821e22007-05-24 15:26:31 +1000247 xfs_agnumber_t agno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 int error;
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300249 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 xfs_agino_t newino; /* new first inode's number */
251 xfs_agino_t newlen; /* new number of inodes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 xfs_agino_t thisino; /* current inode number, for loop */
Glen Overby3ccb8b52006-03-29 09:52:28 +1000253 int isaligned = 0; /* inode allocation at stripe unit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 /* boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 args.tp = tp;
257 args.mp = tp->t_mountp;
258
259 /*
260 * Locking will ensure that we don't have two callers in here
261 * at one time.
262 */
263 newlen = XFS_IALLOC_INODES(args.mp);
264 if (args.mp->m_maxicount &&
265 args.mp->m_sb.sb_icount + newlen > args.mp->m_maxicount)
266 return XFS_ERROR(ENOSPC);
267 args.minlen = args.maxlen = XFS_IALLOC_BLOCKS(args.mp);
268 /*
Glen Overby3ccb8b52006-03-29 09:52:28 +1000269 * First try to allocate inodes contiguous with the last-allocated
270 * chunk of inodes. If the filesystem is striped, this will fill
271 * an entire stripe unit with inodes.
272 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 agi = XFS_BUF_TO_AGI(agbp);
Glen Overby3ccb8b52006-03-29 09:52:28 +1000274 newino = be32_to_cpu(agi->agi_newino);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300275 agno = be32_to_cpu(agi->agi_seqno);
Nathan Scott019ff2d2006-04-11 15:45:05 +1000276 args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) +
277 XFS_IALLOC_BLOCKS(args.mp);
278 if (likely(newino != NULLAGINO &&
279 (args.agbno < be32_to_cpu(agi->agi_length)))) {
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300280 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
Glen Overby3ccb8b52006-03-29 09:52:28 +1000281 args.type = XFS_ALLOCTYPE_THIS_BNO;
282 args.mod = args.total = args.wasdel = args.isfl =
283 args.userdata = args.minalignslop = 0;
284 args.prod = 1;
David Chinner75de2a92008-03-27 18:00:38 +1100285
Glen Overby3ccb8b52006-03-29 09:52:28 +1000286 /*
David Chinner75de2a92008-03-27 18:00:38 +1100287 * We need to take into account alignment here to ensure that
288 * we don't modify the free list if we fail to have an exact
289 * block. If we don't have an exact match, and every oher
290 * attempt allocation attempt fails, we'll end up cancelling
291 * a dirty transaction and shutting down.
292 *
293 * For an exact allocation, alignment must be 1,
294 * however we need to take cluster alignment into account when
295 * fixing up the freelist. Use the minalignslop field to
296 * indicate that extra blocks might be required for alignment,
297 * but not to use them in the actual exact allocation.
Glen Overby3ccb8b52006-03-29 09:52:28 +1000298 */
David Chinner75de2a92008-03-27 18:00:38 +1100299 args.alignment = 1;
300 args.minalignslop = xfs_ialloc_cluster_alignment(&args) - 1;
301
302 /* Allow space for the inode btree to split. */
Christoph Hellwig0d87e652009-02-09 08:37:14 +0100303 args.minleft = args.mp->m_in_maxlevels - 1;
Glen Overby3ccb8b52006-03-29 09:52:28 +1000304 if ((error = xfs_alloc_vextent(&args)))
305 return error;
306 } else
307 args.fsbno = NULLFSBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Glen Overby3ccb8b52006-03-29 09:52:28 +1000309 if (unlikely(args.fsbno == NULLFSBLOCK)) {
310 /*
311 * Set the alignment for the allocation.
312 * If stripe alignment is turned on then align at stripe unit
313 * boundary.
Nathan Scott019ff2d2006-04-11 15:45:05 +1000314 * If the cluster size is smaller than a filesystem block
315 * then we're doing I/O for inodes in filesystem block size
Glen Overby3ccb8b52006-03-29 09:52:28 +1000316 * pieces, so don't need alignment anyway.
317 */
318 isaligned = 0;
319 if (args.mp->m_sinoalign) {
320 ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN));
321 args.alignment = args.mp->m_dalign;
322 isaligned = 1;
David Chinner75de2a92008-03-27 18:00:38 +1100323 } else
324 args.alignment = xfs_ialloc_cluster_alignment(&args);
Glen Overby3ccb8b52006-03-29 09:52:28 +1000325 /*
326 * Need to figure out where to allocate the inode blocks.
327 * Ideally they should be spaced out through the a.g.
328 * For now, just allocate blocks up front.
329 */
330 args.agbno = be32_to_cpu(agi->agi_root);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300331 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
Glen Overby3ccb8b52006-03-29 09:52:28 +1000332 /*
333 * Allocate a fixed-size extent of inodes.
334 */
335 args.type = XFS_ALLOCTYPE_NEAR_BNO;
336 args.mod = args.total = args.wasdel = args.isfl =
337 args.userdata = args.minalignslop = 0;
338 args.prod = 1;
339 /*
340 * Allow space for the inode btree to split.
341 */
Christoph Hellwig0d87e652009-02-09 08:37:14 +0100342 args.minleft = args.mp->m_in_maxlevels - 1;
Glen Overby3ccb8b52006-03-29 09:52:28 +1000343 if ((error = xfs_alloc_vextent(&args)))
344 return error;
345 }
Nathan Scott019ff2d2006-04-11 15:45:05 +1000346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 /*
348 * If stripe alignment is turned on, then try again with cluster
349 * alignment.
350 */
351 if (isaligned && args.fsbno == NULLFSBLOCK) {
352 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Christoph Hellwig16259e72005-11-02 15:11:25 +1100353 args.agbno = be32_to_cpu(agi->agi_root);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300354 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
David Chinner75de2a92008-03-27 18:00:38 +1100355 args.alignment = xfs_ialloc_cluster_alignment(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if ((error = xfs_alloc_vextent(&args)))
357 return error;
358 }
359
360 if (args.fsbno == NULLFSBLOCK) {
361 *alloc = 0;
362 return 0;
363 }
364 ASSERT(args.len == args.minlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
David Chinner359346a2008-04-29 12:53:32 +1000366 /*
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300367 * Stamp and write the inode buffers.
368 *
David Chinner359346a2008-04-29 12:53:32 +1000369 * Seed the new inode cluster with a random generation number. This
370 * prevents short-term reuse of generation numbers if a chunk is
371 * freed and then immediately reallocated. We use random numbers
372 * rather than a linear progression to prevent the next generation
373 * number from being easily guessable.
374 */
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300375 xfs_ialloc_inode_init(args.mp, tp, agno, args.agbno, args.len,
376 random32());
Christoph Hellwigd42f08f2008-11-28 14:23:38 +1100377
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300378 /*
379 * Convert the results.
380 */
381 newino = XFS_OFFBNO_TO_AGINO(args.mp, args.agbno, 0);
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800382 be32_add_cpu(&agi->agi_count, newlen);
383 be32_add_cpu(&agi->agi_freecount, newlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 down_read(&args.mp->m_peraglock);
David Chinner92821e22007-05-24 15:26:31 +1000385 args.mp->m_perag[agno].pagi_freecount += newlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 up_read(&args.mp->m_peraglock);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100387 agi->agi_newino = cpu_to_be32(newino);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 /*
390 * Insert records describing the new inode chunk into the btree.
391 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100392 cur = xfs_inobt_init_cursor(args.mp, tp, agbp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 for (thisino = newino;
394 thisino < newino + newlen;
395 thisino += XFS_INODES_PER_CHUNK) {
396 if ((error = xfs_inobt_lookup_eq(cur, thisino,
397 XFS_INODES_PER_CHUNK, XFS_INOBT_ALL_FREE, &i))) {
398 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
399 return error;
400 }
401 ASSERT(i == 0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100402 if ((error = xfs_btree_insert(cur, &i))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
404 return error;
405 }
406 ASSERT(i == 1);
407 }
408 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
409 /*
410 * Log allocation group header fields
411 */
412 xfs_ialloc_log_agi(tp, agbp,
413 XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO);
414 /*
415 * Modify/log superblock values for inode count and inode free count.
416 */
417 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen);
418 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen);
419 *alloc = 1;
420 return 0;
421}
422
David Chinner7989cb82007-02-10 18:34:56 +1100423STATIC_INLINE xfs_agnumber_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424xfs_ialloc_next_ag(
425 xfs_mount_t *mp)
426{
427 xfs_agnumber_t agno;
428
429 spin_lock(&mp->m_agirotor_lock);
430 agno = mp->m_agirotor;
431 if (++mp->m_agirotor == mp->m_maxagi)
432 mp->m_agirotor = 0;
433 spin_unlock(&mp->m_agirotor_lock);
434
435 return agno;
436}
437
438/*
439 * Select an allocation group to look for a free inode in, based on the parent
440 * inode and then mode. Return the allocation group buffer.
441 */
442STATIC xfs_buf_t * /* allocation group buffer */
443xfs_ialloc_ag_select(
444 xfs_trans_t *tp, /* transaction pointer */
445 xfs_ino_t parent, /* parent directory inode number */
446 mode_t mode, /* bits set to indicate file type */
447 int okalloc) /* ok to allocate more space */
448{
449 xfs_buf_t *agbp; /* allocation group header buffer */
450 xfs_agnumber_t agcount; /* number of ag's in the filesystem */
451 xfs_agnumber_t agno; /* current ag number */
452 int flags; /* alloc buffer locking flags */
453 xfs_extlen_t ineed; /* blocks needed for inode allocation */
454 xfs_extlen_t longest = 0; /* longest extent available */
455 xfs_mount_t *mp; /* mount point structure */
456 int needspace; /* file mode implies space allocated */
457 xfs_perag_t *pag; /* per allocation group data */
458 xfs_agnumber_t pagno; /* parent (starting) ag number */
459
460 /*
461 * Files of these types need at least one block if length > 0
462 * (and they won't fit in the inode, but that's hard to figure out).
463 */
464 needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode);
465 mp = tp->t_mountp;
466 agcount = mp->m_maxagi;
467 if (S_ISDIR(mode))
468 pagno = xfs_ialloc_next_ag(mp);
469 else {
470 pagno = XFS_INO_TO_AGNO(mp, parent);
471 if (pagno >= agcount)
472 pagno = 0;
473 }
474 ASSERT(pagno < agcount);
475 /*
476 * Loop through allocation groups, looking for one with a little
477 * free space in it. Note we don't look for free inodes, exactly.
478 * Instead, we include whether there is a need to allocate inodes
479 * to mean that blocks must be allocated for them,
480 * if none are currently free.
481 */
482 agno = pagno;
483 flags = XFS_ALLOC_FLAG_TRYLOCK;
484 down_read(&mp->m_peraglock);
485 for (;;) {
486 pag = &mp->m_perag[agno];
487 if (!pag->pagi_init) {
488 if (xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
489 agbp = NULL;
490 goto nextag;
491 }
492 } else
493 agbp = NULL;
494
495 if (!pag->pagi_inodeok) {
496 xfs_ialloc_next_ag(mp);
497 goto unlock_nextag;
498 }
499
500 /*
501 * Is there enough free space for the file plus a block
502 * of inodes (if we need to allocate some)?
503 */
504 ineed = pag->pagi_freecount ? 0 : XFS_IALLOC_BLOCKS(mp);
505 if (ineed && !pag->pagf_init) {
506 if (agbp == NULL &&
507 xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
508 agbp = NULL;
509 goto nextag;
510 }
511 (void)xfs_alloc_pagf_init(mp, tp, agno, flags);
512 }
513 if (!ineed || pag->pagf_init) {
514 if (ineed && !(longest = pag->pagf_longest))
515 longest = pag->pagf_flcount > 0;
516 if (!ineed ||
517 (pag->pagf_freeblks >= needspace + ineed &&
518 longest >= ineed &&
519 okalloc)) {
520 if (agbp == NULL &&
521 xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
522 agbp = NULL;
523 goto nextag;
524 }
525 up_read(&mp->m_peraglock);
526 return agbp;
527 }
528 }
529unlock_nextag:
530 if (agbp)
531 xfs_trans_brelse(tp, agbp);
532nextag:
533 /*
534 * No point in iterating over the rest, if we're shutting
535 * down.
536 */
537 if (XFS_FORCED_SHUTDOWN(mp)) {
538 up_read(&mp->m_peraglock);
Nathan Scott1121b212006-09-28 10:58:40 +1000539 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 }
541 agno++;
542 if (agno >= agcount)
543 agno = 0;
544 if (agno == pagno) {
545 if (flags == 0) {
546 up_read(&mp->m_peraglock);
Nathan Scott1121b212006-09-28 10:58:40 +1000547 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
549 flags = 0;
550 }
551 }
552}
553
554/*
555 * Visible inode allocation functions.
556 */
557
558/*
559 * Allocate an inode on disk.
560 * Mode is used to tell whether the new inode will need space, and whether
561 * it is a directory.
562 *
563 * The arguments IO_agbp and alloc_done are defined to work within
564 * the constraint of one allocation per transaction.
565 * xfs_dialloc() is designed to be called twice if it has to do an
566 * allocation to make more free inodes. On the first call,
567 * IO_agbp should be set to NULL. If an inode is available,
568 * i.e., xfs_dialloc() did not need to do an allocation, an inode
569 * number is returned. In this case, IO_agbp would be set to the
570 * current ag_buf and alloc_done set to false.
571 * If an allocation needed to be done, xfs_dialloc would return
572 * the current ag_buf in IO_agbp and set alloc_done to true.
573 * The caller should then commit the current transaction, allocate a new
574 * transaction, and call xfs_dialloc() again, passing in the previous
575 * value of IO_agbp. IO_agbp should be held across the transactions.
576 * Since the agbp is locked across the two calls, the second call is
577 * guaranteed to have a free inode available.
578 *
579 * Once we successfully pick an inode its number is returned and the
580 * on-disk data structures are updated. The inode itself is not read
581 * in, since doing so would break ordering constraints with xfs_reclaim.
582 */
583int
584xfs_dialloc(
585 xfs_trans_t *tp, /* transaction pointer */
586 xfs_ino_t parent, /* parent inode (directory) */
587 mode_t mode, /* mode bits for new inode */
588 int okalloc, /* ok to allocate more space */
589 xfs_buf_t **IO_agbp, /* in/out ag header's buffer */
590 boolean_t *alloc_done, /* true if we needed to replenish
591 inode freelist */
592 xfs_ino_t *inop) /* inode number allocated */
593{
594 xfs_agnumber_t agcount; /* number of allocation groups */
595 xfs_buf_t *agbp; /* allocation group header's buffer */
596 xfs_agnumber_t agno; /* allocation group number */
597 xfs_agi_t *agi; /* allocation group header structure */
598 xfs_btree_cur_t *cur; /* inode allocation btree cursor */
599 int error; /* error return value */
600 int i; /* result code */
601 int ialloced; /* inode allocation status */
602 int noroom = 0; /* no space for inode blk allocation */
603 xfs_ino_t ino; /* fs-relative inode to be returned */
604 /* REFERENCED */
605 int j; /* result code */
606 xfs_mount_t *mp; /* file system mount structure */
607 int offset; /* index of inode in chunk */
608 xfs_agino_t pagino; /* parent's a.g. relative inode # */
609 xfs_agnumber_t pagno; /* parent's allocation group number */
Christoph Hellwig61a25842006-09-28 10:57:04 +1000610 xfs_inobt_rec_incore_t rec; /* inode allocation record */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 xfs_agnumber_t tagno; /* testing allocation group number */
612 xfs_btree_cur_t *tcur; /* temp cursor */
Christoph Hellwig61a25842006-09-28 10:57:04 +1000613 xfs_inobt_rec_incore_t trec; /* temp inode allocation record */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615
616 if (*IO_agbp == NULL) {
617 /*
618 * We do not have an agbp, so select an initial allocation
619 * group for inode allocation.
620 */
621 agbp = xfs_ialloc_ag_select(tp, parent, mode, okalloc);
622 /*
623 * Couldn't find an allocation group satisfying the
624 * criteria, give up.
625 */
626 if (!agbp) {
627 *inop = NULLFSINO;
628 return 0;
629 }
630 agi = XFS_BUF_TO_AGI(agbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100631 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 } else {
633 /*
634 * Continue where we left off before. In this case, we
635 * know that the allocation group has free inodes.
636 */
637 agbp = *IO_agbp;
638 agi = XFS_BUF_TO_AGI(agbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100639 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
640 ASSERT(be32_to_cpu(agi->agi_freecount) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 }
642 mp = tp->t_mountp;
643 agcount = mp->m_sb.sb_agcount;
Christoph Hellwig16259e72005-11-02 15:11:25 +1100644 agno = be32_to_cpu(agi->agi_seqno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 tagno = agno;
646 pagno = XFS_INO_TO_AGNO(mp, parent);
647 pagino = XFS_INO_TO_AGINO(mp, parent);
648
649 /*
650 * If we have already hit the ceiling of inode blocks then clear
651 * okalloc so we scan all available agi structures for a free
652 * inode.
653 */
654
655 if (mp->m_maxicount &&
656 mp->m_sb.sb_icount + XFS_IALLOC_INODES(mp) > mp->m_maxicount) {
657 noroom = 1;
658 okalloc = 0;
659 }
660
661 /*
662 * Loop until we find an allocation group that either has free inodes
663 * or in which we can allocate some inodes. Iterate through the
664 * allocation groups upward, wrapping at the end.
665 */
666 *alloc_done = B_FALSE;
667 while (!agi->agi_freecount) {
668 /*
669 * Don't do anything if we're not supposed to allocate
670 * any blocks, just go on to the next ag.
671 */
672 if (okalloc) {
673 /*
674 * Try to allocate some new inodes in the allocation
675 * group.
676 */
677 if ((error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced))) {
678 xfs_trans_brelse(tp, agbp);
679 if (error == ENOSPC) {
680 *inop = NULLFSINO;
681 return 0;
682 } else
683 return error;
684 }
685 if (ialloced) {
686 /*
687 * We successfully allocated some inodes, return
688 * the current context to the caller so that it
689 * can commit the current transaction and call
690 * us again where we left off.
691 */
Christoph Hellwig16259e72005-11-02 15:11:25 +1100692 ASSERT(be32_to_cpu(agi->agi_freecount) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 *alloc_done = B_TRUE;
694 *IO_agbp = agbp;
695 *inop = NULLFSINO;
696 return 0;
697 }
698 }
699 /*
700 * If it failed, give up on this ag.
701 */
702 xfs_trans_brelse(tp, agbp);
703 /*
704 * Go on to the next ag: get its ag header.
705 */
706nextag:
707 if (++tagno == agcount)
708 tagno = 0;
709 if (tagno == agno) {
710 *inop = NULLFSINO;
711 return noroom ? ENOSPC : 0;
712 }
713 down_read(&mp->m_peraglock);
714 if (mp->m_perag[tagno].pagi_inodeok == 0) {
715 up_read(&mp->m_peraglock);
716 goto nextag;
717 }
718 error = xfs_ialloc_read_agi(mp, tp, tagno, &agbp);
719 up_read(&mp->m_peraglock);
720 if (error)
721 goto nextag;
722 agi = XFS_BUF_TO_AGI(agbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100723 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 }
725 /*
726 * Here with an allocation group that has a free inode.
727 * Reset agno since we may have chosen a new ag in the
728 * loop above.
729 */
730 agno = tagno;
731 *IO_agbp = NULL;
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100732 cur = xfs_inobt_init_cursor(mp, tp, agbp, be32_to_cpu(agi->agi_seqno));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 /*
734 * If pagino is 0 (this is the root inode allocation) use newino.
735 * This must work because we've just allocated some.
736 */
737 if (!pagino)
Christoph Hellwig16259e72005-11-02 15:11:25 +1100738 pagino = be32_to_cpu(agi->agi_newino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739#ifdef DEBUG
740 if (cur->bc_nlevels == 1) {
741 int freecount = 0;
742
743 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
744 goto error0;
745 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
746 do {
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300747 error = xfs_inobt_get_rec(cur, &rec, &i);
748 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 goto error0;
750 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
751 freecount += rec.ir_freecount;
Christoph Hellwig637aa502008-10-30 16:55:45 +1100752 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 goto error0;
754 } while (i == 1);
755
Christoph Hellwig16259e72005-11-02 15:11:25 +1100756 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 XFS_FORCED_SHUTDOWN(mp));
758 }
759#endif
760 /*
761 * If in the same a.g. as the parent, try to get near the parent.
762 */
763 if (pagno == agno) {
764 if ((error = xfs_inobt_lookup_le(cur, pagino, 0, 0, &i)))
765 goto error0;
766 if (i != 0 &&
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300767 (error = xfs_inobt_get_rec(cur, &rec, &j)) == 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 j == 1 &&
769 rec.ir_freecount > 0) {
770 /*
771 * Found a free inode in the same chunk
772 * as parent, done.
773 */
774 }
775 /*
776 * In the same a.g. as parent, but parent's chunk is full.
777 */
778 else {
779 int doneleft; /* done, to the left */
780 int doneright; /* done, to the right */
781
782 if (error)
783 goto error0;
784 ASSERT(i == 1);
785 ASSERT(j == 1);
786 /*
787 * Duplicate the cursor, search left & right
788 * simultaneously.
789 */
790 if ((error = xfs_btree_dup_cursor(cur, &tcur)))
791 goto error0;
792 /*
793 * Search left with tcur, back up 1 record.
794 */
Christoph Hellwig8df4da42008-10-30 16:55:58 +1100795 if ((error = xfs_btree_decrement(tcur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 goto error1;
797 doneleft = !i;
798 if (!doneleft) {
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300799 error = xfs_inobt_get_rec(tcur, &trec, &i);
800 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 goto error1;
802 XFS_WANT_CORRUPTED_GOTO(i == 1, error1);
803 }
804 /*
805 * Search right with cur, go forward 1 record.
806 */
Christoph Hellwig637aa502008-10-30 16:55:45 +1100807 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 goto error1;
809 doneright = !i;
810 if (!doneright) {
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300811 error = xfs_inobt_get_rec(cur, &rec, &i);
812 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 goto error1;
814 XFS_WANT_CORRUPTED_GOTO(i == 1, error1);
815 }
816 /*
817 * Loop until we find the closest inode chunk
818 * with a free one.
819 */
820 while (!doneleft || !doneright) {
821 int useleft; /* using left inode
822 chunk this time */
823
824 /*
825 * Figure out which block is closer,
826 * if both are valid.
827 */
828 if (!doneleft && !doneright)
829 useleft =
830 pagino -
831 (trec.ir_startino +
832 XFS_INODES_PER_CHUNK - 1) <
833 rec.ir_startino - pagino;
834 else
835 useleft = !doneleft;
836 /*
837 * If checking the left, does it have
838 * free inodes?
839 */
840 if (useleft && trec.ir_freecount) {
841 /*
842 * Yes, set it up as the chunk to use.
843 */
844 rec = trec;
845 xfs_btree_del_cursor(cur,
846 XFS_BTREE_NOERROR);
847 cur = tcur;
848 break;
849 }
850 /*
851 * If checking the right, does it have
852 * free inodes?
853 */
854 if (!useleft && rec.ir_freecount) {
855 /*
856 * Yes, it's already set up.
857 */
858 xfs_btree_del_cursor(tcur,
859 XFS_BTREE_NOERROR);
860 break;
861 }
862 /*
863 * If used the left, get another one
864 * further left.
865 */
866 if (useleft) {
Christoph Hellwig8df4da42008-10-30 16:55:58 +1100867 if ((error = xfs_btree_decrement(tcur, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 &i)))
869 goto error1;
870 doneleft = !i;
871 if (!doneleft) {
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300872 error = xfs_inobt_get_rec(
873 tcur, &trec, &i);
874 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 goto error1;
876 XFS_WANT_CORRUPTED_GOTO(i == 1,
877 error1);
878 }
879 }
880 /*
881 * If used the right, get another one
882 * further right.
883 */
884 else {
Christoph Hellwig637aa502008-10-30 16:55:45 +1100885 if ((error = xfs_btree_increment(cur, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 &i)))
887 goto error1;
888 doneright = !i;
889 if (!doneright) {
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300890 error = xfs_inobt_get_rec(
891 cur, &rec, &i);
892 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 goto error1;
894 XFS_WANT_CORRUPTED_GOTO(i == 1,
895 error1);
896 }
897 }
898 }
899 ASSERT(!doneleft || !doneright);
900 }
901 }
902 /*
903 * In a different a.g. from the parent.
904 * See if the most recently allocated block has any free.
905 */
Christoph Hellwig16259e72005-11-02 15:11:25 +1100906 else if (be32_to_cpu(agi->agi_newino) != NULLAGINO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 if ((error = xfs_inobt_lookup_eq(cur,
Christoph Hellwig16259e72005-11-02 15:11:25 +1100908 be32_to_cpu(agi->agi_newino), 0, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 goto error0;
910 if (i == 1 &&
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300911 (error = xfs_inobt_get_rec(cur, &rec, &j)) == 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 j == 1 &&
913 rec.ir_freecount > 0) {
914 /*
915 * The last chunk allocated in the group still has
916 * a free inode.
917 */
918 }
919 /*
920 * None left in the last group, search the whole a.g.
921 */
922 else {
923 if (error)
924 goto error0;
925 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
926 goto error0;
927 ASSERT(i == 1);
928 for (;;) {
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300929 error = xfs_inobt_get_rec(cur, &rec, &i);
930 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 goto error0;
932 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
933 if (rec.ir_freecount > 0)
934 break;
Christoph Hellwig637aa502008-10-30 16:55:45 +1100935 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 goto error0;
937 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
938 }
939 }
940 }
Eric Sandeen9d87c312009-01-14 23:22:07 -0600941 offset = xfs_ialloc_find_free(&rec.ir_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 ASSERT(offset >= 0);
943 ASSERT(offset < XFS_INODES_PER_CHUNK);
944 ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
945 XFS_INODES_PER_CHUNK) == 0);
946 ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
Christoph Hellwig0d87e652009-02-09 08:37:14 +0100947 rec.ir_free &= ~XFS_INOBT_MASK(offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 rec.ir_freecount--;
949 if ((error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount,
950 rec.ir_free)))
951 goto error0;
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800952 be32_add_cpu(&agi->agi_freecount, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
954 down_read(&mp->m_peraglock);
955 mp->m_perag[tagno].pagi_freecount--;
956 up_read(&mp->m_peraglock);
957#ifdef DEBUG
958 if (cur->bc_nlevels == 1) {
959 int freecount = 0;
960
961 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
962 goto error0;
963 do {
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300964 error = xfs_inobt_get_rec(cur, &rec, &i);
965 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 goto error0;
967 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
968 freecount += rec.ir_freecount;
Christoph Hellwig637aa502008-10-30 16:55:45 +1100969 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 goto error0;
971 } while (i == 1);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100972 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 XFS_FORCED_SHUTDOWN(mp));
974 }
975#endif
976 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
977 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
978 *inop = ino;
979 return 0;
980error1:
981 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
982error0:
983 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
984 return error;
985}
986
987/*
988 * Free disk inode. Carefully avoids touching the incore inode, all
989 * manipulations incore are the caller's responsibility.
990 * The on-disk inode is not changed by this operation, only the
991 * btree (free inode mask) is changed.
992 */
993int
994xfs_difree(
995 xfs_trans_t *tp, /* transaction pointer */
996 xfs_ino_t inode, /* inode to be freed */
997 xfs_bmap_free_t *flist, /* extents to free */
998 int *delete, /* set if inode cluster was deleted */
999 xfs_ino_t *first_ino) /* first inode in deleted cluster */
1000{
1001 /* REFERENCED */
1002 xfs_agblock_t agbno; /* block number containing inode */
1003 xfs_buf_t *agbp; /* buffer containing allocation group header */
1004 xfs_agino_t agino; /* inode number relative to allocation group */
1005 xfs_agnumber_t agno; /* allocation group number */
1006 xfs_agi_t *agi; /* allocation group header */
1007 xfs_btree_cur_t *cur; /* inode btree cursor */
1008 int error; /* error return value */
1009 int i; /* result code */
1010 int ilen; /* inodes in an inode cluster */
1011 xfs_mount_t *mp; /* mount structure for filesystem */
1012 int off; /* offset of inode in inode chunk */
Christoph Hellwig61a25842006-09-28 10:57:04 +10001013 xfs_inobt_rec_incore_t rec; /* btree record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 mp = tp->t_mountp;
1016
1017 /*
1018 * Break up inode number into its components.
1019 */
1020 agno = XFS_INO_TO_AGNO(mp, inode);
1021 if (agno >= mp->m_sb.sb_agcount) {
1022 cmn_err(CE_WARN,
1023 "xfs_difree: agno >= mp->m_sb.sb_agcount (%d >= %d) on %s. Returning EINVAL.",
1024 agno, mp->m_sb.sb_agcount, mp->m_fsname);
1025 ASSERT(0);
1026 return XFS_ERROR(EINVAL);
1027 }
1028 agino = XFS_INO_TO_AGINO(mp, inode);
1029 if (inode != XFS_AGINO_TO_INO(mp, agno, agino)) {
1030 cmn_err(CE_WARN,
Christoph Hellwigda1650a2005-11-02 10:21:35 +11001031 "xfs_difree: inode != XFS_AGINO_TO_INO() "
1032 "(%llu != %llu) on %s. Returning EINVAL.",
1033 (unsigned long long)inode,
1034 (unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino),
1035 mp->m_fsname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 ASSERT(0);
1037 return XFS_ERROR(EINVAL);
1038 }
1039 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1040 if (agbno >= mp->m_sb.sb_agblocks) {
1041 cmn_err(CE_WARN,
1042 "xfs_difree: agbno >= mp->m_sb.sb_agblocks (%d >= %d) on %s. Returning EINVAL.",
1043 agbno, mp->m_sb.sb_agblocks, mp->m_fsname);
1044 ASSERT(0);
1045 return XFS_ERROR(EINVAL);
1046 }
1047 /*
1048 * Get the allocation group header.
1049 */
1050 down_read(&mp->m_peraglock);
1051 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1052 up_read(&mp->m_peraglock);
1053 if (error) {
1054 cmn_err(CE_WARN,
1055 "xfs_difree: xfs_ialloc_read_agi() returned an error %d on %s. Returning error.",
1056 error, mp->m_fsname);
1057 return error;
1058 }
1059 agi = XFS_BUF_TO_AGI(agbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001060 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
1061 ASSERT(agbno < be32_to_cpu(agi->agi_length));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 /*
1063 * Initialize the cursor.
1064 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001065 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066#ifdef DEBUG
1067 if (cur->bc_nlevels == 1) {
1068 int freecount = 0;
1069
1070 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
1071 goto error0;
1072 do {
Christoph Hellwig2e287a72009-08-31 20:56:58 -03001073 error = xfs_inobt_get_rec(cur, &rec, &i);
1074 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 goto error0;
1076 if (i) {
1077 freecount += rec.ir_freecount;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001078 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 goto error0;
1080 }
1081 } while (i == 1);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001082 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 XFS_FORCED_SHUTDOWN(mp));
1084 }
1085#endif
1086 /*
1087 * Look for the entry describing this inode.
1088 */
1089 if ((error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i))) {
1090 cmn_err(CE_WARN,
1091 "xfs_difree: xfs_inobt_lookup_le returned() an error %d on %s. Returning error.",
1092 error, mp->m_fsname);
1093 goto error0;
1094 }
1095 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Christoph Hellwig2e287a72009-08-31 20:56:58 -03001096 error = xfs_inobt_get_rec(cur, &rec, &i);
1097 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 cmn_err(CE_WARN,
1099 "xfs_difree: xfs_inobt_get_rec() returned an error %d on %s. Returning error.",
1100 error, mp->m_fsname);
1101 goto error0;
1102 }
1103 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1104 /*
1105 * Get the offset in the inode chunk.
1106 */
1107 off = agino - rec.ir_startino;
1108 ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK);
Christoph Hellwig0d87e652009-02-09 08:37:14 +01001109 ASSERT(!(rec.ir_free & XFS_INOBT_MASK(off)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 /*
1111 * Mark the inode free & increment the count.
1112 */
Christoph Hellwig0d87e652009-02-09 08:37:14 +01001113 rec.ir_free |= XFS_INOBT_MASK(off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 rec.ir_freecount++;
1115
1116 /*
Nathan Scottc41564b2006-03-29 08:55:14 +10001117 * When an inode cluster is free, it becomes eligible for removal
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 */
Josef Jeff Sipek1bd960e2008-02-29 13:58:40 +11001119 if (!(mp->m_flags & XFS_MOUNT_IKEEP) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 (rec.ir_freecount == XFS_IALLOC_INODES(mp))) {
1121
1122 *delete = 1;
1123 *first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino);
1124
1125 /*
1126 * Remove the inode cluster from the AGI B+Tree, adjust the
1127 * AGI and Superblock inode counts, and mark the disk space
1128 * to be freed when the transaction is committed.
1129 */
1130 ilen = XFS_IALLOC_INODES(mp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001131 be32_add_cpu(&agi->agi_count, -ilen);
1132 be32_add_cpu(&agi->agi_freecount, -(ilen - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT);
1134 down_read(&mp->m_peraglock);
1135 mp->m_perag[agno].pagi_freecount -= ilen - 1;
1136 up_read(&mp->m_peraglock);
1137 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen);
1138 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1));
1139
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001140 if ((error = xfs_btree_delete(cur, &i))) {
1141 cmn_err(CE_WARN, "xfs_difree: xfs_btree_delete returned an error %d on %s.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 error, mp->m_fsname);
1143 goto error0;
1144 }
1145
1146 xfs_bmap_add_free(XFS_AGB_TO_FSB(mp,
1147 agno, XFS_INO_TO_AGBNO(mp,rec.ir_startino)),
1148 XFS_IALLOC_BLOCKS(mp), flist, mp);
1149 } else {
1150 *delete = 0;
1151
1152 if ((error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount, rec.ir_free))) {
1153 cmn_err(CE_WARN,
1154 "xfs_difree: xfs_inobt_update() returned an error %d on %s. Returning error.",
1155 error, mp->m_fsname);
1156 goto error0;
1157 }
1158 /*
1159 * Change the inode free counts and log the ag/sb changes.
1160 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001161 be32_add_cpu(&agi->agi_freecount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
1163 down_read(&mp->m_peraglock);
1164 mp->m_perag[agno].pagi_freecount++;
1165 up_read(&mp->m_peraglock);
1166 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1);
1167 }
1168
1169#ifdef DEBUG
1170 if (cur->bc_nlevels == 1) {
1171 int freecount = 0;
1172
1173 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
1174 goto error0;
1175 do {
Christoph Hellwig2e287a72009-08-31 20:56:58 -03001176 error = xfs_inobt_get_rec(cur, &rec, &i);
1177 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 goto error0;
1179 if (i) {
1180 freecount += rec.ir_freecount;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001181 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 goto error0;
1183 }
1184 } while (i == 1);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001185 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 XFS_FORCED_SHUTDOWN(mp));
1187 }
1188#endif
1189 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1190 return 0;
1191
1192error0:
1193 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1194 return error;
1195}
1196
1197/*
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001198 * Return the location of the inode in imap, for mapping it into a buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200int
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001201xfs_imap(
1202 xfs_mount_t *mp, /* file system mount structure */
1203 xfs_trans_t *tp, /* transaction pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 xfs_ino_t ino, /* inode to locate */
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001205 struct xfs_imap *imap, /* location map structure */
1206 uint flags) /* flags for inode btree lookup */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207{
1208 xfs_agblock_t agbno; /* block number of inode in the alloc group */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 xfs_agino_t agino; /* inode number within alloc group */
1210 xfs_agnumber_t agno; /* allocation group number */
1211 int blks_per_cluster; /* num blocks per inode cluster */
1212 xfs_agblock_t chunk_agbno; /* first block in inode chunk */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 xfs_agblock_t cluster_agbno; /* first block in inode cluster */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 int error; /* error code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 int offset; /* index of inode in its buffer */
1216 int offset_agbno; /* blks from chunk start to inode */
1217
1218 ASSERT(ino != NULLFSINO);
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 /*
1221 * Split up the inode number into its parts.
1222 */
1223 agno = XFS_INO_TO_AGNO(mp, ino);
1224 agino = XFS_INO_TO_AGINO(mp, ino);
1225 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1226 if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks ||
1227 ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1228#ifdef DEBUG
Nathan Scott4d1a2ed2006-06-09 17:12:28 +10001229 /* no diagnostics for bulkstat, ino comes from userspace */
Christoph Hellwigb48d8d62008-11-28 14:23:41 +11001230 if (flags & XFS_IGET_BULKSTAT)
Nathan Scott4d1a2ed2006-06-09 17:12:28 +10001231 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 if (agno >= mp->m_sb.sb_agcount) {
1233 xfs_fs_cmn_err(CE_ALERT, mp,
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001234 "xfs_imap: agno (%d) >= "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 "mp->m_sb.sb_agcount (%d)",
1236 agno, mp->m_sb.sb_agcount);
1237 }
1238 if (agbno >= mp->m_sb.sb_agblocks) {
1239 xfs_fs_cmn_err(CE_ALERT, mp,
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001240 "xfs_imap: agbno (0x%llx) >= "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 "mp->m_sb.sb_agblocks (0x%lx)",
1242 (unsigned long long) agbno,
1243 (unsigned long) mp->m_sb.sb_agblocks);
1244 }
1245 if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1246 xfs_fs_cmn_err(CE_ALERT, mp,
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001247 "xfs_imap: ino (0x%llx) != "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 "XFS_AGINO_TO_INO(mp, agno, agino) "
1249 "(0x%llx)",
1250 ino, XFS_AGINO_TO_INO(mp, agno, agino));
1251 }
Nathan Scott745b1f472006-09-28 11:02:23 +10001252 xfs_stack_trace();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253#endif /* DEBUG */
1254 return XFS_ERROR(EINVAL);
1255 }
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001256
1257 /*
1258 * If the inode cluster size is the same as the blocksize or
1259 * smaller we get to the buffer by simple arithmetics.
1260 */
1261 if (XFS_INODE_CLUSTER_SIZE(mp) <= mp->m_sb.sb_blocksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 offset = XFS_INO_TO_OFFSET(mp, ino);
1263 ASSERT(offset < mp->m_sb.sb_inopblock);
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001264
1265 imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, agbno);
1266 imap->im_len = XFS_FSB_TO_BB(mp, 1);
1267 imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 return 0;
1269 }
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_blocklog;
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001272
1273 /*
1274 * If we get a block number passed from bulkstat we can use it to
1275 * find the buffer easily.
1276 */
1277 if (imap->im_blkno) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 offset = XFS_INO_TO_OFFSET(mp, ino);
1279 ASSERT(offset < mp->m_sb.sb_inopblock);
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001280
Eric Sandeen9d87c312009-01-14 23:22:07 -06001281 cluster_agbno = xfs_daddr_to_agbno(mp, imap->im_blkno);
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001282 offset += (agbno - cluster_agbno) * mp->m_sb.sb_inopblock;
1283
1284 imap->im_len = XFS_FSB_TO_BB(mp, blks_per_cluster);
1285 imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 return 0;
1287 }
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001288
1289 /*
1290 * If the inode chunks are aligned then use simple maths to
1291 * find the location. Otherwise we have to do a btree
1292 * lookup to find the location.
1293 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 if (mp->m_inoalign_mask) {
1295 offset_agbno = agbno & mp->m_inoalign_mask;
1296 chunk_agbno = agbno - offset_agbno;
1297 } else {
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001298 xfs_btree_cur_t *cur; /* inode btree cursor */
Christoph Hellwig2e287a72009-08-31 20:56:58 -03001299 xfs_inobt_rec_incore_t chunk_rec;
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001300 xfs_buf_t *agbp; /* agi buffer */
1301 int i; /* temp state */
1302
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 down_read(&mp->m_peraglock);
1304 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1305 up_read(&mp->m_peraglock);
1306 if (error) {
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001307 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 "xfs_ialloc_read_agi() returned "
1309 "error %d, agno %d",
1310 error, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 return error;
1312 }
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001313
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001314 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno);
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001315 error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i);
1316 if (error) {
1317 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 "xfs_inobt_lookup_le() failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 goto error0;
1320 }
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001321
Christoph Hellwig2e287a72009-08-31 20:56:58 -03001322 error = xfs_inobt_get_rec(cur, &chunk_rec, &i);
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001323 if (error) {
1324 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 "xfs_inobt_get_rec() failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 goto error0;
1327 }
1328 if (i == 0) {
1329#ifdef DEBUG
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001330 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 "xfs_inobt_get_rec() failed");
1332#endif /* DEBUG */
1333 error = XFS_ERROR(EINVAL);
1334 }
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001335 error0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 xfs_trans_brelse(tp, agbp);
1337 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1338 if (error)
1339 return error;
Christoph Hellwig2e287a72009-08-31 20:56:58 -03001340 chunk_agbno = XFS_AGINO_TO_AGBNO(mp, chunk_rec.ir_startino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 offset_agbno = agbno - chunk_agbno;
1342 }
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001343
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 ASSERT(agbno >= chunk_agbno);
1345 cluster_agbno = chunk_agbno +
1346 ((offset_agbno / blks_per_cluster) * blks_per_cluster);
1347 offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
1348 XFS_INO_TO_OFFSET(mp, ino);
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001349
1350 imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, cluster_agbno);
1351 imap->im_len = XFS_FSB_TO_BB(mp, blks_per_cluster);
1352 imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
1353
1354 /*
1355 * If the inode number maps to a block outside the bounds
1356 * of the file system then return NULL rather than calling
1357 * read_buf and panicing when we get an error from the
1358 * driver.
1359 */
1360 if ((imap->im_blkno + imap->im_len) >
1361 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) {
1362 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: "
1363 "(imap->im_blkno (0x%llx) + imap->im_len (0x%llx)) > "
1364 " XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks) (0x%llx)",
1365 (unsigned long long) imap->im_blkno,
1366 (unsigned long long) imap->im_len,
1367 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks));
1368 return XFS_ERROR(EINVAL);
1369 }
1370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372}
1373
1374/*
1375 * Compute and fill in value of m_in_maxlevels.
1376 */
1377void
1378xfs_ialloc_compute_maxlevels(
1379 xfs_mount_t *mp) /* file system mount structure */
1380{
1381 int level;
1382 uint maxblocks;
1383 uint maxleafents;
1384 int minleafrecs;
1385 int minnoderecs;
1386
1387 maxleafents = (1LL << XFS_INO_AGINO_BITS(mp)) >>
1388 XFS_INODES_PER_CHUNK_LOG;
1389 minleafrecs = mp->m_alloc_mnr[0];
1390 minnoderecs = mp->m_alloc_mnr[1];
1391 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
1392 for (level = 1; maxblocks > 1; level++)
1393 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
1394 mp->m_in_maxlevels = level;
1395}
1396
1397/*
1398 * Log specified fields for the ag hdr (inode section)
1399 */
1400void
1401xfs_ialloc_log_agi(
1402 xfs_trans_t *tp, /* transaction pointer */
1403 xfs_buf_t *bp, /* allocation group header buffer */
1404 int fields) /* bitmask of fields to log */
1405{
1406 int first; /* first byte number */
1407 int last; /* last byte number */
1408 static const short offsets[] = { /* field starting offsets */
1409 /* keep in sync with bit definitions */
1410 offsetof(xfs_agi_t, agi_magicnum),
1411 offsetof(xfs_agi_t, agi_versionnum),
1412 offsetof(xfs_agi_t, agi_seqno),
1413 offsetof(xfs_agi_t, agi_length),
1414 offsetof(xfs_agi_t, agi_count),
1415 offsetof(xfs_agi_t, agi_root),
1416 offsetof(xfs_agi_t, agi_level),
1417 offsetof(xfs_agi_t, agi_freecount),
1418 offsetof(xfs_agi_t, agi_newino),
1419 offsetof(xfs_agi_t, agi_dirino),
1420 offsetof(xfs_agi_t, agi_unlinked),
1421 sizeof(xfs_agi_t)
1422 };
1423#ifdef DEBUG
1424 xfs_agi_t *agi; /* allocation group header */
1425
1426 agi = XFS_BUF_TO_AGI(bp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001427 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428#endif
1429 /*
1430 * Compute byte offsets for the first and last fields.
1431 */
1432 xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS, &first, &last);
1433 /*
1434 * Log the allocation group inode header buffer.
1435 */
1436 xfs_trans_log_buf(tp, bp, first, last);
1437}
1438
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001439#ifdef DEBUG
1440STATIC void
1441xfs_check_agi_unlinked(
1442 struct xfs_agi *agi)
1443{
1444 int i;
1445
1446 for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++)
1447 ASSERT(agi->agi_unlinked[i]);
1448}
1449#else
1450#define xfs_check_agi_unlinked(agi)
1451#endif
1452
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453/*
1454 * Read in the allocation group header (inode allocation section)
1455 */
1456int
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001457xfs_read_agi(
1458 struct xfs_mount *mp, /* file system mount structure */
1459 struct xfs_trans *tp, /* transaction pointer */
1460 xfs_agnumber_t agno, /* allocation group number */
1461 struct xfs_buf **bpp) /* allocation group hdr buf */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462{
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001463 struct xfs_agi *agi; /* allocation group header */
1464 int agi_ok; /* agi is consistent */
1465 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
1467 ASSERT(agno != NULLAGNUMBER);
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001468
1469 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001471 XFS_FSS_TO_BB(mp, 1), 0, bpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 if (error)
1473 return error;
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001474
1475 ASSERT(*bpp && !XFS_BUF_GETERROR(*bpp));
1476 agi = XFS_BUF_TO_AGI(*bpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
1478 /*
1479 * Validate the magic number of the agi block.
1480 */
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001481 agi_ok = be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC &&
1482 XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum)) &&
1483 be32_to_cpu(agi->agi_seqno) == agno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 if (unlikely(XFS_TEST_ERROR(!agi_ok, mp, XFS_ERRTAG_IALLOC_READ_AGI,
1485 XFS_RANDOM_IALLOC_READ_AGI))) {
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001486 XFS_CORRUPTION_ERROR("xfs_read_agi", XFS_ERRLEVEL_LOW,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 mp, agi);
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001488 xfs_trans_brelse(tp, *bpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 return XFS_ERROR(EFSCORRUPTED);
1490 }
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001491
1492 XFS_BUF_SET_VTYPE_REF(*bpp, B_FS_AGI, XFS_AGI_REF);
1493
1494 xfs_check_agi_unlinked(agi);
1495 return 0;
1496}
1497
1498int
1499xfs_ialloc_read_agi(
1500 struct xfs_mount *mp, /* file system mount structure */
1501 struct xfs_trans *tp, /* transaction pointer */
1502 xfs_agnumber_t agno, /* allocation group number */
1503 struct xfs_buf **bpp) /* allocation group hdr buf */
1504{
1505 struct xfs_agi *agi; /* allocation group header */
1506 struct xfs_perag *pag; /* per allocation group data */
1507 int error;
1508
1509 error = xfs_read_agi(mp, tp, agno, bpp);
1510 if (error)
1511 return error;
1512
1513 agi = XFS_BUF_TO_AGI(*bpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 pag = &mp->m_perag[agno];
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 if (!pag->pagi_init) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11001517 pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
David Chinner92821e22007-05-24 15:26:31 +10001518 pag->pagi_count = be32_to_cpu(agi->agi_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 pag->pagi_init = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 }
1521
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001522 /*
1523 * It's possible for these to be out of sync if
1524 * we are in the middle of a forced shutdown.
1525 */
1526 ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) ||
1527 XFS_FORCED_SHUTDOWN(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 return 0;
1529}
David Chinner92821e22007-05-24 15:26:31 +10001530
1531/*
1532 * Read in the agi to initialise the per-ag data in the mount structure
1533 */
1534int
1535xfs_ialloc_pagi_init(
1536 xfs_mount_t *mp, /* file system mount structure */
1537 xfs_trans_t *tp, /* transaction pointer */
1538 xfs_agnumber_t agno) /* allocation group number */
1539{
1540 xfs_buf_t *bp = NULL;
1541 int error;
1542
1543 error = xfs_ialloc_read_agi(mp, tp, agno, &bp);
1544 if (error)
1545 return error;
1546 if (bp)
1547 xfs_trans_brelse(tp, bp);
1548 return 0;
1549}