blob: 7679c1633053af1509f4b350059280103778dfa5 [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 Hellwigafabc242009-08-31 20:57:03 -0300113 * Update the record referred to by cur to the value given.
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100114 * This either works (return 0) or gets an EFSCORRUPTED error.
115 */
116STATIC int /* error */
117xfs_inobt_update(
118 struct xfs_btree_cur *cur, /* btree cursor */
Christoph Hellwigafabc242009-08-31 20:57:03 -0300119 xfs_inobt_rec_incore_t *irec) /* btree record */
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100120{
121 union xfs_btree_rec rec;
122
Christoph Hellwigafabc242009-08-31 20:57:03 -0300123 rec.inobt.ir_startino = cpu_to_be32(irec->ir_startino);
124 rec.inobt.ir_freecount = cpu_to_be32(irec->ir_freecount);
125 rec.inobt.ir_free = cpu_to_be64(irec->ir_free);
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100126 return xfs_btree_update(cur, &rec);
127}
128
129/*
Christoph Hellwig8cc938f2008-10-30 16:58:11 +1100130 * Get the data from the pointed-to record.
131 */
132int /* error */
133xfs_inobt_get_rec(
134 struct xfs_btree_cur *cur, /* btree cursor */
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300135 xfs_inobt_rec_incore_t *irec, /* btree record */
Christoph Hellwig8cc938f2008-10-30 16:58:11 +1100136 int *stat) /* output: success/failure */
137{
138 union xfs_btree_rec *rec;
139 int error;
140
141 error = xfs_btree_get_rec(cur, &rec, stat);
142 if (!error && *stat == 1) {
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300143 irec->ir_startino = be32_to_cpu(rec->inobt.ir_startino);
144 irec->ir_freecount = be32_to_cpu(rec->inobt.ir_freecount);
145 irec->ir_free = be64_to_cpu(rec->inobt.ir_free);
Christoph Hellwig8cc938f2008-10-30 16:58:11 +1100146 }
147 return error;
148}
149
150/*
Dave Chinner0b48db82009-08-31 20:57:09 -0300151 * Verify that the number of free inodes in the AGI is correct.
152 */
153#ifdef DEBUG
154STATIC int
155xfs_check_agi_freecount(
156 struct xfs_btree_cur *cur,
157 struct xfs_agi *agi)
158{
159 if (cur->bc_nlevels == 1) {
160 xfs_inobt_rec_incore_t rec;
161 int freecount = 0;
162 int error;
163 int i;
164
165 error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i);
166 if (error)
167 return error;
168
169 do {
170 error = xfs_inobt_get_rec(cur, &rec, &i);
171 if (error)
172 return error;
173
174 if (i) {
175 freecount += rec.ir_freecount;
176 error = xfs_btree_increment(cur, 0, &i);
177 if (error)
178 return error;
179 }
180 } while (i == 1);
181
182 if (!XFS_FORCED_SHUTDOWN(cur->bc_mp))
183 ASSERT(freecount == be32_to_cpu(agi->agi_freecount));
184 }
185 return 0;
186}
187#else
188#define xfs_check_agi_freecount(cur, agi) 0
189#endif
190
191/*
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300192 * Initialise a new set of inodes.
193 */
194STATIC void
195xfs_ialloc_inode_init(
196 struct xfs_mount *mp,
197 struct xfs_trans *tp,
198 xfs_agnumber_t agno,
199 xfs_agblock_t agbno,
200 xfs_agblock_t length,
201 unsigned int gen)
202{
203 struct xfs_buf *fbuf;
204 struct xfs_dinode *free;
205 int blks_per_cluster, nbufs, ninodes;
206 int version;
207 int i, j;
208 xfs_daddr_t d;
209
210 /*
211 * Loop over the new block(s), filling in the inodes.
212 * For small block sizes, manipulate the inodes in buffers
213 * which are multiples of the blocks size.
214 */
215 if (mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) {
216 blks_per_cluster = 1;
217 nbufs = length;
218 ninodes = mp->m_sb.sb_inopblock;
219 } else {
220 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) /
221 mp->m_sb.sb_blocksize;
222 nbufs = length / blks_per_cluster;
223 ninodes = blks_per_cluster * mp->m_sb.sb_inopblock;
224 }
225
226 /*
227 * Figure out what version number to use in the inodes we create.
228 * If the superblock version has caught up to the one that supports
229 * the new inode format, then use the new inode version. Otherwise
230 * use the old version so that old kernels will continue to be
231 * able to use the file system.
232 */
233 if (xfs_sb_version_hasnlink(&mp->m_sb))
234 version = 2;
235 else
236 version = 1;
237
238 for (j = 0; j < nbufs; j++) {
239 /*
240 * Get the block.
241 */
242 d = XFS_AGB_TO_DADDR(mp, agno, agbno + (j * blks_per_cluster));
243 fbuf = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
244 mp->m_bsize * blks_per_cluster,
245 XFS_BUF_LOCK);
246 ASSERT(fbuf);
247 ASSERT(!XFS_BUF_GETERROR(fbuf));
248
249 /*
250 * Initialize all inodes in this buffer and then log them.
251 *
252 * XXX: It would be much better if we had just one transaction
253 * to log a whole cluster of inodes instead of all the
254 * individual transactions causing a lot of log traffic.
255 */
256 xfs_biozero(fbuf, 0, ninodes << mp->m_sb.sb_inodelog);
257 for (i = 0; i < ninodes; i++) {
258 int ioffset = i << mp->m_sb.sb_inodelog;
259 uint isize = sizeof(struct xfs_dinode);
260
261 free = xfs_make_iptr(mp, fbuf, i);
262 free->di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
263 free->di_version = version;
264 free->di_gen = cpu_to_be32(gen);
265 free->di_next_unlinked = cpu_to_be32(NULLAGINO);
266 xfs_trans_log_buf(tp, fbuf, ioffset, ioffset + isize - 1);
267 }
268 xfs_trans_inode_alloc_buf(tp, fbuf);
269 }
270}
271
272/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 * Allocate new inodes in the allocation group specified by agbp.
274 * Return 0 for success, else error code.
275 */
276STATIC int /* error code or 0 */
277xfs_ialloc_ag_alloc(
278 xfs_trans_t *tp, /* transaction pointer */
279 xfs_buf_t *agbp, /* alloc group buffer */
280 int *alloc)
281{
282 xfs_agi_t *agi; /* allocation group header */
283 xfs_alloc_arg_t args; /* allocation argument structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 xfs_btree_cur_t *cur; /* inode btree cursor */
David Chinner92821e22007-05-24 15:26:31 +1000285 xfs_agnumber_t agno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 int error;
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300287 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 xfs_agino_t newino; /* new first inode's number */
289 xfs_agino_t newlen; /* new number of inodes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 xfs_agino_t thisino; /* current inode number, for loop */
Glen Overby3ccb8b52006-03-29 09:52:28 +1000291 int isaligned = 0; /* inode allocation at stripe unit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 /* boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 args.tp = tp;
295 args.mp = tp->t_mountp;
296
297 /*
298 * Locking will ensure that we don't have two callers in here
299 * at one time.
300 */
301 newlen = XFS_IALLOC_INODES(args.mp);
302 if (args.mp->m_maxicount &&
303 args.mp->m_sb.sb_icount + newlen > args.mp->m_maxicount)
304 return XFS_ERROR(ENOSPC);
305 args.minlen = args.maxlen = XFS_IALLOC_BLOCKS(args.mp);
306 /*
Glen Overby3ccb8b52006-03-29 09:52:28 +1000307 * First try to allocate inodes contiguous with the last-allocated
308 * chunk of inodes. If the filesystem is striped, this will fill
309 * an entire stripe unit with inodes.
310 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 agi = XFS_BUF_TO_AGI(agbp);
Glen Overby3ccb8b52006-03-29 09:52:28 +1000312 newino = be32_to_cpu(agi->agi_newino);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300313 agno = be32_to_cpu(agi->agi_seqno);
Nathan Scott019ff2d2006-04-11 15:45:05 +1000314 args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) +
315 XFS_IALLOC_BLOCKS(args.mp);
316 if (likely(newino != NULLAGINO &&
317 (args.agbno < be32_to_cpu(agi->agi_length)))) {
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300318 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
Glen Overby3ccb8b52006-03-29 09:52:28 +1000319 args.type = XFS_ALLOCTYPE_THIS_BNO;
320 args.mod = args.total = args.wasdel = args.isfl =
321 args.userdata = args.minalignslop = 0;
322 args.prod = 1;
David Chinner75de2a92008-03-27 18:00:38 +1100323
Glen Overby3ccb8b52006-03-29 09:52:28 +1000324 /*
David Chinner75de2a92008-03-27 18:00:38 +1100325 * We need to take into account alignment here to ensure that
326 * we don't modify the free list if we fail to have an exact
327 * block. If we don't have an exact match, and every oher
328 * attempt allocation attempt fails, we'll end up cancelling
329 * a dirty transaction and shutting down.
330 *
331 * For an exact allocation, alignment must be 1,
332 * however we need to take cluster alignment into account when
333 * fixing up the freelist. Use the minalignslop field to
334 * indicate that extra blocks might be required for alignment,
335 * but not to use them in the actual exact allocation.
Glen Overby3ccb8b52006-03-29 09:52:28 +1000336 */
David Chinner75de2a92008-03-27 18:00:38 +1100337 args.alignment = 1;
338 args.minalignslop = xfs_ialloc_cluster_alignment(&args) - 1;
339
340 /* Allow space for the inode btree to split. */
Christoph Hellwig0d87e652009-02-09 08:37:14 +0100341 args.minleft = args.mp->m_in_maxlevels - 1;
Glen Overby3ccb8b52006-03-29 09:52:28 +1000342 if ((error = xfs_alloc_vextent(&args)))
343 return error;
344 } else
345 args.fsbno = NULLFSBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Glen Overby3ccb8b52006-03-29 09:52:28 +1000347 if (unlikely(args.fsbno == NULLFSBLOCK)) {
348 /*
349 * Set the alignment for the allocation.
350 * If stripe alignment is turned on then align at stripe unit
351 * boundary.
Nathan Scott019ff2d2006-04-11 15:45:05 +1000352 * If the cluster size is smaller than a filesystem block
353 * then we're doing I/O for inodes in filesystem block size
Glen Overby3ccb8b52006-03-29 09:52:28 +1000354 * pieces, so don't need alignment anyway.
355 */
356 isaligned = 0;
357 if (args.mp->m_sinoalign) {
358 ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN));
359 args.alignment = args.mp->m_dalign;
360 isaligned = 1;
David Chinner75de2a92008-03-27 18:00:38 +1100361 } else
362 args.alignment = xfs_ialloc_cluster_alignment(&args);
Glen Overby3ccb8b52006-03-29 09:52:28 +1000363 /*
364 * Need to figure out where to allocate the inode blocks.
365 * Ideally they should be spaced out through the a.g.
366 * For now, just allocate blocks up front.
367 */
368 args.agbno = be32_to_cpu(agi->agi_root);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300369 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
Glen Overby3ccb8b52006-03-29 09:52:28 +1000370 /*
371 * Allocate a fixed-size extent of inodes.
372 */
373 args.type = XFS_ALLOCTYPE_NEAR_BNO;
374 args.mod = args.total = args.wasdel = args.isfl =
375 args.userdata = args.minalignslop = 0;
376 args.prod = 1;
377 /*
378 * Allow space for the inode btree to split.
379 */
Christoph Hellwig0d87e652009-02-09 08:37:14 +0100380 args.minleft = args.mp->m_in_maxlevels - 1;
Glen Overby3ccb8b52006-03-29 09:52:28 +1000381 if ((error = xfs_alloc_vextent(&args)))
382 return error;
383 }
Nathan Scott019ff2d2006-04-11 15:45:05 +1000384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 /*
386 * If stripe alignment is turned on, then try again with cluster
387 * alignment.
388 */
389 if (isaligned && args.fsbno == NULLFSBLOCK) {
390 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Christoph Hellwig16259e72005-11-02 15:11:25 +1100391 args.agbno = be32_to_cpu(agi->agi_root);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300392 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
David Chinner75de2a92008-03-27 18:00:38 +1100393 args.alignment = xfs_ialloc_cluster_alignment(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 if ((error = xfs_alloc_vextent(&args)))
395 return error;
396 }
397
398 if (args.fsbno == NULLFSBLOCK) {
399 *alloc = 0;
400 return 0;
401 }
402 ASSERT(args.len == args.minlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
David Chinner359346a2008-04-29 12:53:32 +1000404 /*
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300405 * Stamp and write the inode buffers.
406 *
David Chinner359346a2008-04-29 12:53:32 +1000407 * Seed the new inode cluster with a random generation number. This
408 * prevents short-term reuse of generation numbers if a chunk is
409 * freed and then immediately reallocated. We use random numbers
410 * rather than a linear progression to prevent the next generation
411 * number from being easily guessable.
412 */
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300413 xfs_ialloc_inode_init(args.mp, tp, agno, args.agbno, args.len,
414 random32());
Christoph Hellwigd42f08f2008-11-28 14:23:38 +1100415
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300416 /*
417 * Convert the results.
418 */
419 newino = XFS_OFFBNO_TO_AGINO(args.mp, args.agbno, 0);
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800420 be32_add_cpu(&agi->agi_count, newlen);
421 be32_add_cpu(&agi->agi_freecount, newlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 down_read(&args.mp->m_peraglock);
David Chinner92821e22007-05-24 15:26:31 +1000423 args.mp->m_perag[agno].pagi_freecount += newlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 up_read(&args.mp->m_peraglock);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100425 agi->agi_newino = cpu_to_be32(newino);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 /*
428 * Insert records describing the new inode chunk into the btree.
429 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100430 cur = xfs_inobt_init_cursor(args.mp, tp, agbp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 for (thisino = newino;
432 thisino < newino + newlen;
433 thisino += XFS_INODES_PER_CHUNK) {
434 if ((error = xfs_inobt_lookup_eq(cur, thisino,
435 XFS_INODES_PER_CHUNK, XFS_INOBT_ALL_FREE, &i))) {
436 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
437 return error;
438 }
439 ASSERT(i == 0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100440 if ((error = xfs_btree_insert(cur, &i))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
442 return error;
443 }
444 ASSERT(i == 1);
445 }
446 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
447 /*
448 * Log allocation group header fields
449 */
450 xfs_ialloc_log_agi(tp, agbp,
451 XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO);
452 /*
453 * Modify/log superblock values for inode count and inode free count.
454 */
455 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen);
456 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen);
457 *alloc = 1;
458 return 0;
459}
460
David Chinner7989cb82007-02-10 18:34:56 +1100461STATIC_INLINE xfs_agnumber_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462xfs_ialloc_next_ag(
463 xfs_mount_t *mp)
464{
465 xfs_agnumber_t agno;
466
467 spin_lock(&mp->m_agirotor_lock);
468 agno = mp->m_agirotor;
469 if (++mp->m_agirotor == mp->m_maxagi)
470 mp->m_agirotor = 0;
471 spin_unlock(&mp->m_agirotor_lock);
472
473 return agno;
474}
475
476/*
477 * Select an allocation group to look for a free inode in, based on the parent
478 * inode and then mode. Return the allocation group buffer.
479 */
480STATIC xfs_buf_t * /* allocation group buffer */
481xfs_ialloc_ag_select(
482 xfs_trans_t *tp, /* transaction pointer */
483 xfs_ino_t parent, /* parent directory inode number */
484 mode_t mode, /* bits set to indicate file type */
485 int okalloc) /* ok to allocate more space */
486{
487 xfs_buf_t *agbp; /* allocation group header buffer */
488 xfs_agnumber_t agcount; /* number of ag's in the filesystem */
489 xfs_agnumber_t agno; /* current ag number */
490 int flags; /* alloc buffer locking flags */
491 xfs_extlen_t ineed; /* blocks needed for inode allocation */
492 xfs_extlen_t longest = 0; /* longest extent available */
493 xfs_mount_t *mp; /* mount point structure */
494 int needspace; /* file mode implies space allocated */
495 xfs_perag_t *pag; /* per allocation group data */
496 xfs_agnumber_t pagno; /* parent (starting) ag number */
497
498 /*
499 * Files of these types need at least one block if length > 0
500 * (and they won't fit in the inode, but that's hard to figure out).
501 */
502 needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode);
503 mp = tp->t_mountp;
504 agcount = mp->m_maxagi;
505 if (S_ISDIR(mode))
506 pagno = xfs_ialloc_next_ag(mp);
507 else {
508 pagno = XFS_INO_TO_AGNO(mp, parent);
509 if (pagno >= agcount)
510 pagno = 0;
511 }
512 ASSERT(pagno < agcount);
513 /*
514 * Loop through allocation groups, looking for one with a little
515 * free space in it. Note we don't look for free inodes, exactly.
516 * Instead, we include whether there is a need to allocate inodes
517 * to mean that blocks must be allocated for them,
518 * if none are currently free.
519 */
520 agno = pagno;
521 flags = XFS_ALLOC_FLAG_TRYLOCK;
522 down_read(&mp->m_peraglock);
523 for (;;) {
524 pag = &mp->m_perag[agno];
525 if (!pag->pagi_init) {
526 if (xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
527 agbp = NULL;
528 goto nextag;
529 }
530 } else
531 agbp = NULL;
532
533 if (!pag->pagi_inodeok) {
534 xfs_ialloc_next_ag(mp);
535 goto unlock_nextag;
536 }
537
538 /*
539 * Is there enough free space for the file plus a block
540 * of inodes (if we need to allocate some)?
541 */
542 ineed = pag->pagi_freecount ? 0 : XFS_IALLOC_BLOCKS(mp);
543 if (ineed && !pag->pagf_init) {
544 if (agbp == NULL &&
545 xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
546 agbp = NULL;
547 goto nextag;
548 }
549 (void)xfs_alloc_pagf_init(mp, tp, agno, flags);
550 }
551 if (!ineed || pag->pagf_init) {
552 if (ineed && !(longest = pag->pagf_longest))
553 longest = pag->pagf_flcount > 0;
554 if (!ineed ||
555 (pag->pagf_freeblks >= needspace + ineed &&
556 longest >= ineed &&
557 okalloc)) {
558 if (agbp == NULL &&
559 xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
560 agbp = NULL;
561 goto nextag;
562 }
563 up_read(&mp->m_peraglock);
564 return agbp;
565 }
566 }
567unlock_nextag:
568 if (agbp)
569 xfs_trans_brelse(tp, agbp);
570nextag:
571 /*
572 * No point in iterating over the rest, if we're shutting
573 * down.
574 */
575 if (XFS_FORCED_SHUTDOWN(mp)) {
576 up_read(&mp->m_peraglock);
Nathan Scott1121b212006-09-28 10:58:40 +1000577 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
579 agno++;
580 if (agno >= agcount)
581 agno = 0;
582 if (agno == pagno) {
583 if (flags == 0) {
584 up_read(&mp->m_peraglock);
Nathan Scott1121b212006-09-28 10:58:40 +1000585 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
587 flags = 0;
588 }
589 }
590}
591
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300592/*
593 * Try to retrieve the next record to the left/right from the current one.
594 */
595STATIC int
596xfs_ialloc_next_rec(
597 struct xfs_btree_cur *cur,
598 xfs_inobt_rec_incore_t *rec,
599 int *done,
600 int left)
601{
602 int error;
603 int i;
604
605 if (left)
606 error = xfs_btree_decrement(cur, 0, &i);
607 else
608 error = xfs_btree_increment(cur, 0, &i);
609
610 if (error)
611 return error;
612 *done = !i;
613 if (i) {
614 error = xfs_inobt_get_rec(cur, rec, &i);
615 if (error)
616 return error;
617 XFS_WANT_CORRUPTED_RETURN(i == 1);
618 }
619
620 return 0;
621}
622
Dave Chinner0b48db82009-08-31 20:57:09 -0300623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624/*
625 * Visible inode allocation functions.
626 */
627
628/*
629 * Allocate an inode on disk.
630 * Mode is used to tell whether the new inode will need space, and whether
631 * it is a directory.
632 *
633 * The arguments IO_agbp and alloc_done are defined to work within
634 * the constraint of one allocation per transaction.
635 * xfs_dialloc() is designed to be called twice if it has to do an
636 * allocation to make more free inodes. On the first call,
637 * IO_agbp should be set to NULL. If an inode is available,
638 * i.e., xfs_dialloc() did not need to do an allocation, an inode
639 * number is returned. In this case, IO_agbp would be set to the
640 * current ag_buf and alloc_done set to false.
641 * If an allocation needed to be done, xfs_dialloc would return
642 * the current ag_buf in IO_agbp and set alloc_done to true.
643 * The caller should then commit the current transaction, allocate a new
644 * transaction, and call xfs_dialloc() again, passing in the previous
645 * value of IO_agbp. IO_agbp should be held across the transactions.
646 * Since the agbp is locked across the two calls, the second call is
647 * guaranteed to have a free inode available.
648 *
649 * Once we successfully pick an inode its number is returned and the
650 * on-disk data structures are updated. The inode itself is not read
651 * in, since doing so would break ordering constraints with xfs_reclaim.
652 */
653int
654xfs_dialloc(
655 xfs_trans_t *tp, /* transaction pointer */
656 xfs_ino_t parent, /* parent inode (directory) */
657 mode_t mode, /* mode bits for new inode */
658 int okalloc, /* ok to allocate more space */
659 xfs_buf_t **IO_agbp, /* in/out ag header's buffer */
660 boolean_t *alloc_done, /* true if we needed to replenish
661 inode freelist */
662 xfs_ino_t *inop) /* inode number allocated */
663{
664 xfs_agnumber_t agcount; /* number of allocation groups */
665 xfs_buf_t *agbp; /* allocation group header's buffer */
666 xfs_agnumber_t agno; /* allocation group number */
667 xfs_agi_t *agi; /* allocation group header structure */
668 xfs_btree_cur_t *cur; /* inode allocation btree cursor */
669 int error; /* error return value */
670 int i; /* result code */
671 int ialloced; /* inode allocation status */
672 int noroom = 0; /* no space for inode blk allocation */
673 xfs_ino_t ino; /* fs-relative inode to be returned */
674 /* REFERENCED */
675 int j; /* result code */
676 xfs_mount_t *mp; /* file system mount structure */
677 int offset; /* index of inode in chunk */
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300678 xfs_agino_t pagino; /* parent's AG relative inode # */
679 xfs_agnumber_t pagno; /* parent's AG number */
Christoph Hellwig61a25842006-09-28 10:57:04 +1000680 xfs_inobt_rec_incore_t rec; /* inode allocation record */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 xfs_agnumber_t tagno; /* testing allocation group number */
682 xfs_btree_cur_t *tcur; /* temp cursor */
Christoph Hellwig61a25842006-09-28 10:57:04 +1000683 xfs_inobt_rec_incore_t trec; /* temp inode allocation record */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685
686 if (*IO_agbp == NULL) {
687 /*
688 * We do not have an agbp, so select an initial allocation
689 * group for inode allocation.
690 */
691 agbp = xfs_ialloc_ag_select(tp, parent, mode, okalloc);
692 /*
693 * Couldn't find an allocation group satisfying the
694 * criteria, give up.
695 */
696 if (!agbp) {
697 *inop = NULLFSINO;
698 return 0;
699 }
700 agi = XFS_BUF_TO_AGI(agbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100701 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 } else {
703 /*
704 * Continue where we left off before. In this case, we
705 * know that the allocation group has free inodes.
706 */
707 agbp = *IO_agbp;
708 agi = XFS_BUF_TO_AGI(agbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100709 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
710 ASSERT(be32_to_cpu(agi->agi_freecount) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 }
712 mp = tp->t_mountp;
713 agcount = mp->m_sb.sb_agcount;
Christoph Hellwig16259e72005-11-02 15:11:25 +1100714 agno = be32_to_cpu(agi->agi_seqno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 tagno = agno;
716 pagno = XFS_INO_TO_AGNO(mp, parent);
717 pagino = XFS_INO_TO_AGINO(mp, parent);
718
719 /*
720 * If we have already hit the ceiling of inode blocks then clear
721 * okalloc so we scan all available agi structures for a free
722 * inode.
723 */
724
725 if (mp->m_maxicount &&
726 mp->m_sb.sb_icount + XFS_IALLOC_INODES(mp) > mp->m_maxicount) {
727 noroom = 1;
728 okalloc = 0;
729 }
730
731 /*
732 * Loop until we find an allocation group that either has free inodes
733 * or in which we can allocate some inodes. Iterate through the
734 * allocation groups upward, wrapping at the end.
735 */
736 *alloc_done = B_FALSE;
737 while (!agi->agi_freecount) {
738 /*
739 * Don't do anything if we're not supposed to allocate
740 * any blocks, just go on to the next ag.
741 */
742 if (okalloc) {
743 /*
744 * Try to allocate some new inodes in the allocation
745 * group.
746 */
747 if ((error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced))) {
748 xfs_trans_brelse(tp, agbp);
749 if (error == ENOSPC) {
750 *inop = NULLFSINO;
751 return 0;
752 } else
753 return error;
754 }
755 if (ialloced) {
756 /*
757 * We successfully allocated some inodes, return
758 * the current context to the caller so that it
759 * can commit the current transaction and call
760 * us again where we left off.
761 */
Christoph Hellwig16259e72005-11-02 15:11:25 +1100762 ASSERT(be32_to_cpu(agi->agi_freecount) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 *alloc_done = B_TRUE;
764 *IO_agbp = agbp;
765 *inop = NULLFSINO;
766 return 0;
767 }
768 }
769 /*
770 * If it failed, give up on this ag.
771 */
772 xfs_trans_brelse(tp, agbp);
773 /*
774 * Go on to the next ag: get its ag header.
775 */
776nextag:
777 if (++tagno == agcount)
778 tagno = 0;
779 if (tagno == agno) {
780 *inop = NULLFSINO;
781 return noroom ? ENOSPC : 0;
782 }
783 down_read(&mp->m_peraglock);
784 if (mp->m_perag[tagno].pagi_inodeok == 0) {
785 up_read(&mp->m_peraglock);
786 goto nextag;
787 }
788 error = xfs_ialloc_read_agi(mp, tp, tagno, &agbp);
789 up_read(&mp->m_peraglock);
790 if (error)
791 goto nextag;
792 agi = XFS_BUF_TO_AGI(agbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100793 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 }
795 /*
796 * Here with an allocation group that has a free inode.
797 * Reset agno since we may have chosen a new ag in the
798 * loop above.
799 */
800 agno = tagno;
801 *IO_agbp = NULL;
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100802 cur = xfs_inobt_init_cursor(mp, tp, agbp, be32_to_cpu(agi->agi_seqno));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 /*
804 * If pagino is 0 (this is the root inode allocation) use newino.
805 * This must work because we've just allocated some.
806 */
807 if (!pagino)
Christoph Hellwig16259e72005-11-02 15:11:25 +1100808 pagino = be32_to_cpu(agi->agi_newino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Dave Chinner0b48db82009-08-31 20:57:09 -0300810 error = xfs_check_agi_freecount(cur, agi);
811 if (error)
812 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 /*
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300815 * If in the same AG as the parent, try to get near the parent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 */
817 if (pagno == agno) {
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300818 int doneleft; /* done, to the left */
819 int doneright; /* done, to the right */
820
821 error = xfs_inobt_lookup_le(cur, pagino, 0, 0, &i);
822 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 goto error0;
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300824 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
825
826 error = xfs_inobt_get_rec(cur, &rec, &j);
827 if (error)
828 goto error0;
829 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
830
831 if (rec.ir_freecount > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 /*
833 * Found a free inode in the same chunk
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300834 * as the parent, done.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 */
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300836 goto alloc_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300838
839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 /*
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300841 * In the same AG as parent, but parent's chunk is full.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300844 /* duplicate the cursor, search left & right simultaneously */
845 error = xfs_btree_dup_cursor(cur, &tcur);
846 if (error)
847 goto error0;
848
849 /* search left with tcur, back up 1 record */
850 error = xfs_ialloc_next_rec(tcur, &trec, &doneleft, 1);
851 if (error)
852 goto error1;
853
854 /* search right with cur, go forward 1 record. */
855 error = xfs_ialloc_next_rec(cur, &rec, &doneright, 0);
856 if (error)
857 goto error1;
858
859 /*
860 * Loop until we find an inode chunk with a free inode.
861 */
862 while (!doneleft || !doneright) {
863 int useleft; /* using left inode chunk this time */
864
865 /* figure out the closer block if both are valid. */
866 if (!doneleft && !doneright) {
867 useleft = pagino -
868 (trec.ir_startino + XFS_INODES_PER_CHUNK - 1) <
869 rec.ir_startino - pagino;
870 } else {
871 useleft = !doneleft;
872 }
873
874 /* free inodes to the left? */
875 if (useleft && trec.ir_freecount) {
876 rec = trec;
877 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
878 cur = tcur;
879 goto alloc_inode;
880 }
881
882 /* free inodes to the right? */
883 if (!useleft && rec.ir_freecount) {
884 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
885 goto alloc_inode;
886 }
887
888 /* get next record to check */
889 if (useleft) {
890 error = xfs_ialloc_next_rec(tcur, &trec,
891 &doneleft, 1);
892 } else {
893 error = xfs_ialloc_next_rec(cur, &rec,
894 &doneright, 0);
895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 goto error1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 }
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300899 ASSERT(!doneleft || !doneright);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 }
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 /*
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300903 * In a different AG from the parent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 * 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) {
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300907 error = xfs_inobt_lookup_eq(cur, be32_to_cpu(agi->agi_newino),
908 0, 0, &i);
909 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 goto error0;
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300911
912 if (i == 1) {
913 error = xfs_inobt_get_rec(cur, &rec, &j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 if (error)
915 goto error0;
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300916
917 if (j == 1 && rec.ir_freecount > 0) {
918 /*
919 * The last chunk allocated in the group
920 * still has a free inode.
921 */
922 goto alloc_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 }
924 }
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300925
926 /*
927 * None left in the last group, search the whole AG
928 */
929 error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i);
930 if (error)
931 goto error0;
932 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
933
934 for (;;) {
935 error = xfs_inobt_get_rec(cur, &rec, &i);
936 if (error)
937 goto error0;
938 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
939 if (rec.ir_freecount > 0)
940 break;
941 error = xfs_btree_increment(cur, 0, &i);
942 if (error)
943 goto error0;
944 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 }
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300947
948alloc_inode:
Eric Sandeen9d87c312009-01-14 23:22:07 -0600949 offset = xfs_ialloc_find_free(&rec.ir_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 ASSERT(offset >= 0);
951 ASSERT(offset < XFS_INODES_PER_CHUNK);
952 ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
953 XFS_INODES_PER_CHUNK) == 0);
954 ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
Christoph Hellwig0d87e652009-02-09 08:37:14 +0100955 rec.ir_free &= ~XFS_INOBT_MASK(offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 rec.ir_freecount--;
Christoph Hellwigafabc242009-08-31 20:57:03 -0300957 error = xfs_inobt_update(cur, &rec);
958 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 goto error0;
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800960 be32_add_cpu(&agi->agi_freecount, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
962 down_read(&mp->m_peraglock);
963 mp->m_perag[tagno].pagi_freecount--;
964 up_read(&mp->m_peraglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Dave Chinner0b48db82009-08-31 20:57:09 -0300966 error = xfs_check_agi_freecount(cur, agi);
967 if (error)
968 goto error0;
969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
971 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
972 *inop = ino;
973 return 0;
974error1:
975 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
976error0:
977 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
978 return error;
979}
980
981/*
982 * Free disk inode. Carefully avoids touching the incore inode, all
983 * manipulations incore are the caller's responsibility.
984 * The on-disk inode is not changed by this operation, only the
985 * btree (free inode mask) is changed.
986 */
987int
988xfs_difree(
989 xfs_trans_t *tp, /* transaction pointer */
990 xfs_ino_t inode, /* inode to be freed */
991 xfs_bmap_free_t *flist, /* extents to free */
992 int *delete, /* set if inode cluster was deleted */
993 xfs_ino_t *first_ino) /* first inode in deleted cluster */
994{
995 /* REFERENCED */
996 xfs_agblock_t agbno; /* block number containing inode */
997 xfs_buf_t *agbp; /* buffer containing allocation group header */
998 xfs_agino_t agino; /* inode number relative to allocation group */
999 xfs_agnumber_t agno; /* allocation group number */
1000 xfs_agi_t *agi; /* allocation group header */
1001 xfs_btree_cur_t *cur; /* inode btree cursor */
1002 int error; /* error return value */
1003 int i; /* result code */
1004 int ilen; /* inodes in an inode cluster */
1005 xfs_mount_t *mp; /* mount structure for filesystem */
1006 int off; /* offset of inode in inode chunk */
Christoph Hellwig61a25842006-09-28 10:57:04 +10001007 xfs_inobt_rec_incore_t rec; /* btree record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
1009 mp = tp->t_mountp;
1010
1011 /*
1012 * Break up inode number into its components.
1013 */
1014 agno = XFS_INO_TO_AGNO(mp, inode);
1015 if (agno >= mp->m_sb.sb_agcount) {
1016 cmn_err(CE_WARN,
1017 "xfs_difree: agno >= mp->m_sb.sb_agcount (%d >= %d) on %s. Returning EINVAL.",
1018 agno, mp->m_sb.sb_agcount, mp->m_fsname);
1019 ASSERT(0);
1020 return XFS_ERROR(EINVAL);
1021 }
1022 agino = XFS_INO_TO_AGINO(mp, inode);
1023 if (inode != XFS_AGINO_TO_INO(mp, agno, agino)) {
1024 cmn_err(CE_WARN,
Christoph Hellwigda1650a2005-11-02 10:21:35 +11001025 "xfs_difree: inode != XFS_AGINO_TO_INO() "
1026 "(%llu != %llu) on %s. Returning EINVAL.",
1027 (unsigned long long)inode,
1028 (unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino),
1029 mp->m_fsname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 ASSERT(0);
1031 return XFS_ERROR(EINVAL);
1032 }
1033 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1034 if (agbno >= mp->m_sb.sb_agblocks) {
1035 cmn_err(CE_WARN,
1036 "xfs_difree: agbno >= mp->m_sb.sb_agblocks (%d >= %d) on %s. Returning EINVAL.",
1037 agbno, mp->m_sb.sb_agblocks, mp->m_fsname);
1038 ASSERT(0);
1039 return XFS_ERROR(EINVAL);
1040 }
1041 /*
1042 * Get the allocation group header.
1043 */
1044 down_read(&mp->m_peraglock);
1045 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1046 up_read(&mp->m_peraglock);
1047 if (error) {
1048 cmn_err(CE_WARN,
1049 "xfs_difree: xfs_ialloc_read_agi() returned an error %d on %s. Returning error.",
1050 error, mp->m_fsname);
1051 return error;
1052 }
1053 agi = XFS_BUF_TO_AGI(agbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001054 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
1055 ASSERT(agbno < be32_to_cpu(agi->agi_length));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 /*
1057 * Initialize the cursor.
1058 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001059 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Dave Chinner0b48db82009-08-31 20:57:09 -03001061 error = xfs_check_agi_freecount(cur, agi);
1062 if (error)
1063 goto error0;
1064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 /*
1066 * Look for the entry describing this inode.
1067 */
1068 if ((error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i))) {
1069 cmn_err(CE_WARN,
1070 "xfs_difree: xfs_inobt_lookup_le returned() an error %d on %s. Returning error.",
1071 error, mp->m_fsname);
1072 goto error0;
1073 }
1074 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Christoph Hellwig2e287a72009-08-31 20:56:58 -03001075 error = xfs_inobt_get_rec(cur, &rec, &i);
1076 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 cmn_err(CE_WARN,
1078 "xfs_difree: xfs_inobt_get_rec() returned an error %d on %s. Returning error.",
1079 error, mp->m_fsname);
1080 goto error0;
1081 }
1082 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1083 /*
1084 * Get the offset in the inode chunk.
1085 */
1086 off = agino - rec.ir_startino;
1087 ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK);
Christoph Hellwig0d87e652009-02-09 08:37:14 +01001088 ASSERT(!(rec.ir_free & XFS_INOBT_MASK(off)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 /*
1090 * Mark the inode free & increment the count.
1091 */
Christoph Hellwig0d87e652009-02-09 08:37:14 +01001092 rec.ir_free |= XFS_INOBT_MASK(off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 rec.ir_freecount++;
1094
1095 /*
Nathan Scottc41564b2006-03-29 08:55:14 +10001096 * When an inode cluster is free, it becomes eligible for removal
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 */
Josef Jeff Sipek1bd960e2008-02-29 13:58:40 +11001098 if (!(mp->m_flags & XFS_MOUNT_IKEEP) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 (rec.ir_freecount == XFS_IALLOC_INODES(mp))) {
1100
1101 *delete = 1;
1102 *first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino);
1103
1104 /*
1105 * Remove the inode cluster from the AGI B+Tree, adjust the
1106 * AGI and Superblock inode counts, and mark the disk space
1107 * to be freed when the transaction is committed.
1108 */
1109 ilen = XFS_IALLOC_INODES(mp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001110 be32_add_cpu(&agi->agi_count, -ilen);
1111 be32_add_cpu(&agi->agi_freecount, -(ilen - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT);
1113 down_read(&mp->m_peraglock);
1114 mp->m_perag[agno].pagi_freecount -= ilen - 1;
1115 up_read(&mp->m_peraglock);
1116 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen);
1117 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1));
1118
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001119 if ((error = xfs_btree_delete(cur, &i))) {
1120 cmn_err(CE_WARN, "xfs_difree: xfs_btree_delete returned an error %d on %s.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 error, mp->m_fsname);
1122 goto error0;
1123 }
1124
1125 xfs_bmap_add_free(XFS_AGB_TO_FSB(mp,
1126 agno, XFS_INO_TO_AGBNO(mp,rec.ir_startino)),
1127 XFS_IALLOC_BLOCKS(mp), flist, mp);
1128 } else {
1129 *delete = 0;
1130
Christoph Hellwigafabc242009-08-31 20:57:03 -03001131 error = xfs_inobt_update(cur, &rec);
1132 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 cmn_err(CE_WARN,
Christoph Hellwigafabc242009-08-31 20:57:03 -03001134 "xfs_difree: xfs_inobt_update returned an error %d on %s.",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 error, mp->m_fsname);
1136 goto error0;
1137 }
Christoph Hellwigafabc242009-08-31 20:57:03 -03001138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 /*
1140 * Change the inode free counts and log the ag/sb changes.
1141 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001142 be32_add_cpu(&agi->agi_freecount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
1144 down_read(&mp->m_peraglock);
1145 mp->m_perag[agno].pagi_freecount++;
1146 up_read(&mp->m_peraglock);
1147 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1);
1148 }
1149
Dave Chinner0b48db82009-08-31 20:57:09 -03001150 error = xfs_check_agi_freecount(cur, agi);
1151 if (error)
1152 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1155 return 0;
1156
1157error0:
1158 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1159 return error;
1160}
1161
1162/*
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001163 * Return the location of the inode in imap, for mapping it into a buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165int
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001166xfs_imap(
1167 xfs_mount_t *mp, /* file system mount structure */
1168 xfs_trans_t *tp, /* transaction pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 xfs_ino_t ino, /* inode to locate */
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001170 struct xfs_imap *imap, /* location map structure */
1171 uint flags) /* flags for inode btree lookup */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
1173 xfs_agblock_t agbno; /* block number of inode in the alloc group */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 xfs_agino_t agino; /* inode number within alloc group */
1175 xfs_agnumber_t agno; /* allocation group number */
1176 int blks_per_cluster; /* num blocks per inode cluster */
1177 xfs_agblock_t chunk_agbno; /* first block in inode chunk */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 xfs_agblock_t cluster_agbno; /* first block in inode cluster */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 int error; /* error code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 int offset; /* index of inode in its buffer */
1181 int offset_agbno; /* blks from chunk start to inode */
1182
1183 ASSERT(ino != NULLFSINO);
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 /*
1186 * Split up the inode number into its parts.
1187 */
1188 agno = XFS_INO_TO_AGNO(mp, ino);
1189 agino = XFS_INO_TO_AGINO(mp, ino);
1190 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1191 if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks ||
1192 ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1193#ifdef DEBUG
Nathan Scott4d1a2ed2006-06-09 17:12:28 +10001194 /* no diagnostics for bulkstat, ino comes from userspace */
Christoph Hellwigb48d8d62008-11-28 14:23:41 +11001195 if (flags & XFS_IGET_BULKSTAT)
Nathan Scott4d1a2ed2006-06-09 17:12:28 +10001196 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 if (agno >= mp->m_sb.sb_agcount) {
1198 xfs_fs_cmn_err(CE_ALERT, mp,
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001199 "xfs_imap: agno (%d) >= "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 "mp->m_sb.sb_agcount (%d)",
1201 agno, mp->m_sb.sb_agcount);
1202 }
1203 if (agbno >= mp->m_sb.sb_agblocks) {
1204 xfs_fs_cmn_err(CE_ALERT, mp,
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001205 "xfs_imap: agbno (0x%llx) >= "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 "mp->m_sb.sb_agblocks (0x%lx)",
1207 (unsigned long long) agbno,
1208 (unsigned long) mp->m_sb.sb_agblocks);
1209 }
1210 if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1211 xfs_fs_cmn_err(CE_ALERT, mp,
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001212 "xfs_imap: ino (0x%llx) != "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 "XFS_AGINO_TO_INO(mp, agno, agino) "
1214 "(0x%llx)",
1215 ino, XFS_AGINO_TO_INO(mp, agno, agino));
1216 }
Nathan Scott745b1f472006-09-28 11:02:23 +10001217 xfs_stack_trace();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218#endif /* DEBUG */
1219 return XFS_ERROR(EINVAL);
1220 }
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001221
1222 /*
1223 * If the inode cluster size is the same as the blocksize or
1224 * smaller we get to the buffer by simple arithmetics.
1225 */
1226 if (XFS_INODE_CLUSTER_SIZE(mp) <= mp->m_sb.sb_blocksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 offset = XFS_INO_TO_OFFSET(mp, ino);
1228 ASSERT(offset < mp->m_sb.sb_inopblock);
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001229
1230 imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, agbno);
1231 imap->im_len = XFS_FSB_TO_BB(mp, 1);
1232 imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 return 0;
1234 }
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001235
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_blocklog;
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001237
1238 /*
1239 * If we get a block number passed from bulkstat we can use it to
1240 * find the buffer easily.
1241 */
1242 if (imap->im_blkno) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 offset = XFS_INO_TO_OFFSET(mp, ino);
1244 ASSERT(offset < mp->m_sb.sb_inopblock);
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001245
Eric Sandeen9d87c312009-01-14 23:22:07 -06001246 cluster_agbno = xfs_daddr_to_agbno(mp, imap->im_blkno);
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001247 offset += (agbno - cluster_agbno) * mp->m_sb.sb_inopblock;
1248
1249 imap->im_len = XFS_FSB_TO_BB(mp, blks_per_cluster);
1250 imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 return 0;
1252 }
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001253
1254 /*
1255 * If the inode chunks are aligned then use simple maths to
1256 * find the location. Otherwise we have to do a btree
1257 * lookup to find the location.
1258 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 if (mp->m_inoalign_mask) {
1260 offset_agbno = agbno & mp->m_inoalign_mask;
1261 chunk_agbno = agbno - offset_agbno;
1262 } else {
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001263 xfs_btree_cur_t *cur; /* inode btree cursor */
Christoph Hellwig2e287a72009-08-31 20:56:58 -03001264 xfs_inobt_rec_incore_t chunk_rec;
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001265 xfs_buf_t *agbp; /* agi buffer */
1266 int i; /* temp state */
1267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 down_read(&mp->m_peraglock);
1269 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1270 up_read(&mp->m_peraglock);
1271 if (error) {
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001272 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 "xfs_ialloc_read_agi() returned "
1274 "error %d, agno %d",
1275 error, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 return error;
1277 }
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001278
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001279 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno);
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001280 error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i);
1281 if (error) {
1282 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 "xfs_inobt_lookup_le() failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 goto error0;
1285 }
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001286
Christoph Hellwig2e287a72009-08-31 20:56:58 -03001287 error = xfs_inobt_get_rec(cur, &chunk_rec, &i);
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001288 if (error) {
1289 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 "xfs_inobt_get_rec() failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 goto error0;
1292 }
1293 if (i == 0) {
1294#ifdef DEBUG
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001295 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 "xfs_inobt_get_rec() failed");
1297#endif /* DEBUG */
1298 error = XFS_ERROR(EINVAL);
1299 }
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001300 error0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 xfs_trans_brelse(tp, agbp);
1302 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1303 if (error)
1304 return error;
Christoph Hellwig2e287a72009-08-31 20:56:58 -03001305 chunk_agbno = XFS_AGINO_TO_AGBNO(mp, chunk_rec.ir_startino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 offset_agbno = agbno - chunk_agbno;
1307 }
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 ASSERT(agbno >= chunk_agbno);
1310 cluster_agbno = chunk_agbno +
1311 ((offset_agbno / blks_per_cluster) * blks_per_cluster);
1312 offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
1313 XFS_INO_TO_OFFSET(mp, ino);
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001314
1315 imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, cluster_agbno);
1316 imap->im_len = XFS_FSB_TO_BB(mp, blks_per_cluster);
1317 imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
1318
1319 /*
1320 * If the inode number maps to a block outside the bounds
1321 * of the file system then return NULL rather than calling
1322 * read_buf and panicing when we get an error from the
1323 * driver.
1324 */
1325 if ((imap->im_blkno + imap->im_len) >
1326 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) {
1327 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: "
1328 "(imap->im_blkno (0x%llx) + imap->im_len (0x%llx)) > "
1329 " XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks) (0x%llx)",
1330 (unsigned long long) imap->im_blkno,
1331 (unsigned long long) imap->im_len,
1332 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks));
1333 return XFS_ERROR(EINVAL);
1334 }
1335
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337}
1338
1339/*
1340 * Compute and fill in value of m_in_maxlevels.
1341 */
1342void
1343xfs_ialloc_compute_maxlevels(
1344 xfs_mount_t *mp) /* file system mount structure */
1345{
1346 int level;
1347 uint maxblocks;
1348 uint maxleafents;
1349 int minleafrecs;
1350 int minnoderecs;
1351
1352 maxleafents = (1LL << XFS_INO_AGINO_BITS(mp)) >>
1353 XFS_INODES_PER_CHUNK_LOG;
1354 minleafrecs = mp->m_alloc_mnr[0];
1355 minnoderecs = mp->m_alloc_mnr[1];
1356 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
1357 for (level = 1; maxblocks > 1; level++)
1358 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
1359 mp->m_in_maxlevels = level;
1360}
1361
1362/*
1363 * Log specified fields for the ag hdr (inode section)
1364 */
1365void
1366xfs_ialloc_log_agi(
1367 xfs_trans_t *tp, /* transaction pointer */
1368 xfs_buf_t *bp, /* allocation group header buffer */
1369 int fields) /* bitmask of fields to log */
1370{
1371 int first; /* first byte number */
1372 int last; /* last byte number */
1373 static const short offsets[] = { /* field starting offsets */
1374 /* keep in sync with bit definitions */
1375 offsetof(xfs_agi_t, agi_magicnum),
1376 offsetof(xfs_agi_t, agi_versionnum),
1377 offsetof(xfs_agi_t, agi_seqno),
1378 offsetof(xfs_agi_t, agi_length),
1379 offsetof(xfs_agi_t, agi_count),
1380 offsetof(xfs_agi_t, agi_root),
1381 offsetof(xfs_agi_t, agi_level),
1382 offsetof(xfs_agi_t, agi_freecount),
1383 offsetof(xfs_agi_t, agi_newino),
1384 offsetof(xfs_agi_t, agi_dirino),
1385 offsetof(xfs_agi_t, agi_unlinked),
1386 sizeof(xfs_agi_t)
1387 };
1388#ifdef DEBUG
1389 xfs_agi_t *agi; /* allocation group header */
1390
1391 agi = XFS_BUF_TO_AGI(bp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001392 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393#endif
1394 /*
1395 * Compute byte offsets for the first and last fields.
1396 */
1397 xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS, &first, &last);
1398 /*
1399 * Log the allocation group inode header buffer.
1400 */
1401 xfs_trans_log_buf(tp, bp, first, last);
1402}
1403
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001404#ifdef DEBUG
1405STATIC void
1406xfs_check_agi_unlinked(
1407 struct xfs_agi *agi)
1408{
1409 int i;
1410
1411 for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++)
1412 ASSERT(agi->agi_unlinked[i]);
1413}
1414#else
1415#define xfs_check_agi_unlinked(agi)
1416#endif
1417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418/*
1419 * Read in the allocation group header (inode allocation section)
1420 */
1421int
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001422xfs_read_agi(
1423 struct xfs_mount *mp, /* file system mount structure */
1424 struct xfs_trans *tp, /* transaction pointer */
1425 xfs_agnumber_t agno, /* allocation group number */
1426 struct xfs_buf **bpp) /* allocation group hdr buf */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427{
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001428 struct xfs_agi *agi; /* allocation group header */
1429 int agi_ok; /* agi is consistent */
1430 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
1432 ASSERT(agno != NULLAGNUMBER);
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001433
1434 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001436 XFS_FSS_TO_BB(mp, 1), 0, bpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 if (error)
1438 return error;
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001439
1440 ASSERT(*bpp && !XFS_BUF_GETERROR(*bpp));
1441 agi = XFS_BUF_TO_AGI(*bpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
1443 /*
1444 * Validate the magic number of the agi block.
1445 */
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001446 agi_ok = be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC &&
1447 XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum)) &&
1448 be32_to_cpu(agi->agi_seqno) == agno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 if (unlikely(XFS_TEST_ERROR(!agi_ok, mp, XFS_ERRTAG_IALLOC_READ_AGI,
1450 XFS_RANDOM_IALLOC_READ_AGI))) {
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001451 XFS_CORRUPTION_ERROR("xfs_read_agi", XFS_ERRLEVEL_LOW,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 mp, agi);
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001453 xfs_trans_brelse(tp, *bpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 return XFS_ERROR(EFSCORRUPTED);
1455 }
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001456
1457 XFS_BUF_SET_VTYPE_REF(*bpp, B_FS_AGI, XFS_AGI_REF);
1458
1459 xfs_check_agi_unlinked(agi);
1460 return 0;
1461}
1462
1463int
1464xfs_ialloc_read_agi(
1465 struct xfs_mount *mp, /* file system mount structure */
1466 struct xfs_trans *tp, /* transaction pointer */
1467 xfs_agnumber_t agno, /* allocation group number */
1468 struct xfs_buf **bpp) /* allocation group hdr buf */
1469{
1470 struct xfs_agi *agi; /* allocation group header */
1471 struct xfs_perag *pag; /* per allocation group data */
1472 int error;
1473
1474 error = xfs_read_agi(mp, tp, agno, bpp);
1475 if (error)
1476 return error;
1477
1478 agi = XFS_BUF_TO_AGI(*bpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 pag = &mp->m_perag[agno];
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 if (!pag->pagi_init) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11001482 pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
David Chinner92821e22007-05-24 15:26:31 +10001483 pag->pagi_count = be32_to_cpu(agi->agi_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 pag->pagi_init = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 }
1486
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001487 /*
1488 * It's possible for these to be out of sync if
1489 * we are in the middle of a forced shutdown.
1490 */
1491 ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) ||
1492 XFS_FORCED_SHUTDOWN(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 return 0;
1494}
David Chinner92821e22007-05-24 15:26:31 +10001495
1496/*
1497 * Read in the agi to initialise the per-ag data in the mount structure
1498 */
1499int
1500xfs_ialloc_pagi_init(
1501 xfs_mount_t *mp, /* file system mount structure */
1502 xfs_trans_t *tp, /* transaction pointer */
1503 xfs_agnumber_t agno) /* allocation group number */
1504{
1505 xfs_buf_t *bp = NULL;
1506 int error;
1507
1508 error = xfs_ialloc_read_agi(mp, tp, agno, &bp);
1509 if (error)
1510 return error;
1511 if (bp)
1512 xfs_trans_brelse(tp, bp);
1513 return 0;
1514}