blob: ce9edf7f4cb4b810d2eb679184b8f7c157b5d4d9 [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 */
138 xfs_agino_t *ino, /* output: starting inode of chunk */
139 __int32_t *fcnt, /* output: number of free inodes */
140 xfs_inofree_t *free, /* output: free inode mask */
141 int *stat) /* output: success/failure */
142{
143 union xfs_btree_rec *rec;
144 int error;
145
146 error = xfs_btree_get_rec(cur, &rec, stat);
147 if (!error && *stat == 1) {
148 *ino = be32_to_cpu(rec->inobt.ir_startino);
149 *fcnt = be32_to_cpu(rec->inobt.ir_freecount);
150 *free = be64_to_cpu(rec->inobt.ir_free);
151 }
152 return error;
153}
154
155/*
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300156 * Initialise a new set of inodes.
157 */
158STATIC void
159xfs_ialloc_inode_init(
160 struct xfs_mount *mp,
161 struct xfs_trans *tp,
162 xfs_agnumber_t agno,
163 xfs_agblock_t agbno,
164 xfs_agblock_t length,
165 unsigned int gen)
166{
167 struct xfs_buf *fbuf;
168 struct xfs_dinode *free;
169 int blks_per_cluster, nbufs, ninodes;
170 int version;
171 int i, j;
172 xfs_daddr_t d;
173
174 /*
175 * Loop over the new block(s), filling in the inodes.
176 * For small block sizes, manipulate the inodes in buffers
177 * which are multiples of the blocks size.
178 */
179 if (mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) {
180 blks_per_cluster = 1;
181 nbufs = length;
182 ninodes = mp->m_sb.sb_inopblock;
183 } else {
184 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) /
185 mp->m_sb.sb_blocksize;
186 nbufs = length / blks_per_cluster;
187 ninodes = blks_per_cluster * mp->m_sb.sb_inopblock;
188 }
189
190 /*
191 * Figure out what version number to use in the inodes we create.
192 * If the superblock version has caught up to the one that supports
193 * the new inode format, then use the new inode version. Otherwise
194 * use the old version so that old kernels will continue to be
195 * able to use the file system.
196 */
197 if (xfs_sb_version_hasnlink(&mp->m_sb))
198 version = 2;
199 else
200 version = 1;
201
202 for (j = 0; j < nbufs; j++) {
203 /*
204 * Get the block.
205 */
206 d = XFS_AGB_TO_DADDR(mp, agno, agbno + (j * blks_per_cluster));
207 fbuf = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
208 mp->m_bsize * blks_per_cluster,
209 XFS_BUF_LOCK);
210 ASSERT(fbuf);
211 ASSERT(!XFS_BUF_GETERROR(fbuf));
212
213 /*
214 * Initialize all inodes in this buffer and then log them.
215 *
216 * XXX: It would be much better if we had just one transaction
217 * to log a whole cluster of inodes instead of all the
218 * individual transactions causing a lot of log traffic.
219 */
220 xfs_biozero(fbuf, 0, ninodes << mp->m_sb.sb_inodelog);
221 for (i = 0; i < ninodes; i++) {
222 int ioffset = i << mp->m_sb.sb_inodelog;
223 uint isize = sizeof(struct xfs_dinode);
224
225 free = xfs_make_iptr(mp, fbuf, i);
226 free->di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
227 free->di_version = version;
228 free->di_gen = cpu_to_be32(gen);
229 free->di_next_unlinked = cpu_to_be32(NULLAGINO);
230 xfs_trans_log_buf(tp, fbuf, ioffset, ioffset + isize - 1);
231 }
232 xfs_trans_inode_alloc_buf(tp, fbuf);
233 }
234}
235
236/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 * Allocate new inodes in the allocation group specified by agbp.
238 * Return 0 for success, else error code.
239 */
240STATIC int /* error code or 0 */
241xfs_ialloc_ag_alloc(
242 xfs_trans_t *tp, /* transaction pointer */
243 xfs_buf_t *agbp, /* alloc group buffer */
244 int *alloc)
245{
246 xfs_agi_t *agi; /* allocation group header */
247 xfs_alloc_arg_t args; /* allocation argument structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 xfs_btree_cur_t *cur; /* inode btree cursor */
David Chinner92821e22007-05-24 15:26:31 +1000249 xfs_agnumber_t agno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 int error;
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300251 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 xfs_agino_t newino; /* new first inode's number */
253 xfs_agino_t newlen; /* new number of inodes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 xfs_agino_t thisino; /* current inode number, for loop */
Glen Overby3ccb8b52006-03-29 09:52:28 +1000255 int isaligned = 0; /* inode allocation at stripe unit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 /* boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 args.tp = tp;
259 args.mp = tp->t_mountp;
260
261 /*
262 * Locking will ensure that we don't have two callers in here
263 * at one time.
264 */
265 newlen = XFS_IALLOC_INODES(args.mp);
266 if (args.mp->m_maxicount &&
267 args.mp->m_sb.sb_icount + newlen > args.mp->m_maxicount)
268 return XFS_ERROR(ENOSPC);
269 args.minlen = args.maxlen = XFS_IALLOC_BLOCKS(args.mp);
270 /*
Glen Overby3ccb8b52006-03-29 09:52:28 +1000271 * First try to allocate inodes contiguous with the last-allocated
272 * chunk of inodes. If the filesystem is striped, this will fill
273 * an entire stripe unit with inodes.
274 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 agi = XFS_BUF_TO_AGI(agbp);
Glen Overby3ccb8b52006-03-29 09:52:28 +1000276 newino = be32_to_cpu(agi->agi_newino);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300277 agno = be32_to_cpu(agi->agi_seqno);
Nathan Scott019ff2d2006-04-11 15:45:05 +1000278 args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) +
279 XFS_IALLOC_BLOCKS(args.mp);
280 if (likely(newino != NULLAGINO &&
281 (args.agbno < be32_to_cpu(agi->agi_length)))) {
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300282 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
Glen Overby3ccb8b52006-03-29 09:52:28 +1000283 args.type = XFS_ALLOCTYPE_THIS_BNO;
284 args.mod = args.total = args.wasdel = args.isfl =
285 args.userdata = args.minalignslop = 0;
286 args.prod = 1;
David Chinner75de2a92008-03-27 18:00:38 +1100287
Glen Overby3ccb8b52006-03-29 09:52:28 +1000288 /*
David Chinner75de2a92008-03-27 18:00:38 +1100289 * We need to take into account alignment here to ensure that
290 * we don't modify the free list if we fail to have an exact
291 * block. If we don't have an exact match, and every oher
292 * attempt allocation attempt fails, we'll end up cancelling
293 * a dirty transaction and shutting down.
294 *
295 * For an exact allocation, alignment must be 1,
296 * however we need to take cluster alignment into account when
297 * fixing up the freelist. Use the minalignslop field to
298 * indicate that extra blocks might be required for alignment,
299 * but not to use them in the actual exact allocation.
Glen Overby3ccb8b52006-03-29 09:52:28 +1000300 */
David Chinner75de2a92008-03-27 18:00:38 +1100301 args.alignment = 1;
302 args.minalignslop = xfs_ialloc_cluster_alignment(&args) - 1;
303
304 /* Allow space for the inode btree to split. */
Christoph Hellwig0d87e652009-02-09 08:37:14 +0100305 args.minleft = args.mp->m_in_maxlevels - 1;
Glen Overby3ccb8b52006-03-29 09:52:28 +1000306 if ((error = xfs_alloc_vextent(&args)))
307 return error;
308 } else
309 args.fsbno = NULLFSBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Glen Overby3ccb8b52006-03-29 09:52:28 +1000311 if (unlikely(args.fsbno == NULLFSBLOCK)) {
312 /*
313 * Set the alignment for the allocation.
314 * If stripe alignment is turned on then align at stripe unit
315 * boundary.
Nathan Scott019ff2d2006-04-11 15:45:05 +1000316 * If the cluster size is smaller than a filesystem block
317 * then we're doing I/O for inodes in filesystem block size
Glen Overby3ccb8b52006-03-29 09:52:28 +1000318 * pieces, so don't need alignment anyway.
319 */
320 isaligned = 0;
321 if (args.mp->m_sinoalign) {
322 ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN));
323 args.alignment = args.mp->m_dalign;
324 isaligned = 1;
David Chinner75de2a92008-03-27 18:00:38 +1100325 } else
326 args.alignment = xfs_ialloc_cluster_alignment(&args);
Glen Overby3ccb8b52006-03-29 09:52:28 +1000327 /*
328 * Need to figure out where to allocate the inode blocks.
329 * Ideally they should be spaced out through the a.g.
330 * For now, just allocate blocks up front.
331 */
332 args.agbno = be32_to_cpu(agi->agi_root);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300333 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
Glen Overby3ccb8b52006-03-29 09:52:28 +1000334 /*
335 * Allocate a fixed-size extent of inodes.
336 */
337 args.type = XFS_ALLOCTYPE_NEAR_BNO;
338 args.mod = args.total = args.wasdel = args.isfl =
339 args.userdata = args.minalignslop = 0;
340 args.prod = 1;
341 /*
342 * Allow space for the inode btree to split.
343 */
Christoph Hellwig0d87e652009-02-09 08:37:14 +0100344 args.minleft = args.mp->m_in_maxlevels - 1;
Glen Overby3ccb8b52006-03-29 09:52:28 +1000345 if ((error = xfs_alloc_vextent(&args)))
346 return error;
347 }
Nathan Scott019ff2d2006-04-11 15:45:05 +1000348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 /*
350 * If stripe alignment is turned on, then try again with cluster
351 * alignment.
352 */
353 if (isaligned && args.fsbno == NULLFSBLOCK) {
354 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Christoph Hellwig16259e72005-11-02 15:11:25 +1100355 args.agbno = be32_to_cpu(agi->agi_root);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300356 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
David Chinner75de2a92008-03-27 18:00:38 +1100357 args.alignment = xfs_ialloc_cluster_alignment(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if ((error = xfs_alloc_vextent(&args)))
359 return error;
360 }
361
362 if (args.fsbno == NULLFSBLOCK) {
363 *alloc = 0;
364 return 0;
365 }
366 ASSERT(args.len == args.minlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
David Chinner359346a2008-04-29 12:53:32 +1000368 /*
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300369 * Stamp and write the inode buffers.
370 *
David Chinner359346a2008-04-29 12:53:32 +1000371 * Seed the new inode cluster with a random generation number. This
372 * prevents short-term reuse of generation numbers if a chunk is
373 * freed and then immediately reallocated. We use random numbers
374 * rather than a linear progression to prevent the next generation
375 * number from being easily guessable.
376 */
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300377 xfs_ialloc_inode_init(args.mp, tp, agno, args.agbno, args.len,
378 random32());
Christoph Hellwigd42f08f2008-11-28 14:23:38 +1100379
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300380 /*
381 * Convert the results.
382 */
383 newino = XFS_OFFBNO_TO_AGINO(args.mp, args.agbno, 0);
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800384 be32_add_cpu(&agi->agi_count, newlen);
385 be32_add_cpu(&agi->agi_freecount, newlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 down_read(&args.mp->m_peraglock);
David Chinner92821e22007-05-24 15:26:31 +1000387 args.mp->m_perag[agno].pagi_freecount += newlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 up_read(&args.mp->m_peraglock);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100389 agi->agi_newino = cpu_to_be32(newino);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 /*
392 * Insert records describing the new inode chunk into the btree.
393 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100394 cur = xfs_inobt_init_cursor(args.mp, tp, agbp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 for (thisino = newino;
396 thisino < newino + newlen;
397 thisino += XFS_INODES_PER_CHUNK) {
398 if ((error = xfs_inobt_lookup_eq(cur, thisino,
399 XFS_INODES_PER_CHUNK, XFS_INOBT_ALL_FREE, &i))) {
400 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
401 return error;
402 }
403 ASSERT(i == 0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100404 if ((error = xfs_btree_insert(cur, &i))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
406 return error;
407 }
408 ASSERT(i == 1);
409 }
410 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
411 /*
412 * Log allocation group header fields
413 */
414 xfs_ialloc_log_agi(tp, agbp,
415 XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO);
416 /*
417 * Modify/log superblock values for inode count and inode free count.
418 */
419 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen);
420 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen);
421 *alloc = 1;
422 return 0;
423}
424
David Chinner7989cb82007-02-10 18:34:56 +1100425STATIC_INLINE xfs_agnumber_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426xfs_ialloc_next_ag(
427 xfs_mount_t *mp)
428{
429 xfs_agnumber_t agno;
430
431 spin_lock(&mp->m_agirotor_lock);
432 agno = mp->m_agirotor;
433 if (++mp->m_agirotor == mp->m_maxagi)
434 mp->m_agirotor = 0;
435 spin_unlock(&mp->m_agirotor_lock);
436
437 return agno;
438}
439
440/*
441 * Select an allocation group to look for a free inode in, based on the parent
442 * inode and then mode. Return the allocation group buffer.
443 */
444STATIC xfs_buf_t * /* allocation group buffer */
445xfs_ialloc_ag_select(
446 xfs_trans_t *tp, /* transaction pointer */
447 xfs_ino_t parent, /* parent directory inode number */
448 mode_t mode, /* bits set to indicate file type */
449 int okalloc) /* ok to allocate more space */
450{
451 xfs_buf_t *agbp; /* allocation group header buffer */
452 xfs_agnumber_t agcount; /* number of ag's in the filesystem */
453 xfs_agnumber_t agno; /* current ag number */
454 int flags; /* alloc buffer locking flags */
455 xfs_extlen_t ineed; /* blocks needed for inode allocation */
456 xfs_extlen_t longest = 0; /* longest extent available */
457 xfs_mount_t *mp; /* mount point structure */
458 int needspace; /* file mode implies space allocated */
459 xfs_perag_t *pag; /* per allocation group data */
460 xfs_agnumber_t pagno; /* parent (starting) ag number */
461
462 /*
463 * Files of these types need at least one block if length > 0
464 * (and they won't fit in the inode, but that's hard to figure out).
465 */
466 needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode);
467 mp = tp->t_mountp;
468 agcount = mp->m_maxagi;
469 if (S_ISDIR(mode))
470 pagno = xfs_ialloc_next_ag(mp);
471 else {
472 pagno = XFS_INO_TO_AGNO(mp, parent);
473 if (pagno >= agcount)
474 pagno = 0;
475 }
476 ASSERT(pagno < agcount);
477 /*
478 * Loop through allocation groups, looking for one with a little
479 * free space in it. Note we don't look for free inodes, exactly.
480 * Instead, we include whether there is a need to allocate inodes
481 * to mean that blocks must be allocated for them,
482 * if none are currently free.
483 */
484 agno = pagno;
485 flags = XFS_ALLOC_FLAG_TRYLOCK;
486 down_read(&mp->m_peraglock);
487 for (;;) {
488 pag = &mp->m_perag[agno];
489 if (!pag->pagi_init) {
490 if (xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
491 agbp = NULL;
492 goto nextag;
493 }
494 } else
495 agbp = NULL;
496
497 if (!pag->pagi_inodeok) {
498 xfs_ialloc_next_ag(mp);
499 goto unlock_nextag;
500 }
501
502 /*
503 * Is there enough free space for the file plus a block
504 * of inodes (if we need to allocate some)?
505 */
506 ineed = pag->pagi_freecount ? 0 : XFS_IALLOC_BLOCKS(mp);
507 if (ineed && !pag->pagf_init) {
508 if (agbp == NULL &&
509 xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
510 agbp = NULL;
511 goto nextag;
512 }
513 (void)xfs_alloc_pagf_init(mp, tp, agno, flags);
514 }
515 if (!ineed || pag->pagf_init) {
516 if (ineed && !(longest = pag->pagf_longest))
517 longest = pag->pagf_flcount > 0;
518 if (!ineed ||
519 (pag->pagf_freeblks >= needspace + ineed &&
520 longest >= ineed &&
521 okalloc)) {
522 if (agbp == NULL &&
523 xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
524 agbp = NULL;
525 goto nextag;
526 }
527 up_read(&mp->m_peraglock);
528 return agbp;
529 }
530 }
531unlock_nextag:
532 if (agbp)
533 xfs_trans_brelse(tp, agbp);
534nextag:
535 /*
536 * No point in iterating over the rest, if we're shutting
537 * down.
538 */
539 if (XFS_FORCED_SHUTDOWN(mp)) {
540 up_read(&mp->m_peraglock);
Nathan Scott1121b212006-09-28 10:58:40 +1000541 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
543 agno++;
544 if (agno >= agcount)
545 agno = 0;
546 if (agno == pagno) {
547 if (flags == 0) {
548 up_read(&mp->m_peraglock);
Nathan Scott1121b212006-09-28 10:58:40 +1000549 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
551 flags = 0;
552 }
553 }
554}
555
556/*
557 * Visible inode allocation functions.
558 */
559
560/*
561 * Allocate an inode on disk.
562 * Mode is used to tell whether the new inode will need space, and whether
563 * it is a directory.
564 *
565 * The arguments IO_agbp and alloc_done are defined to work within
566 * the constraint of one allocation per transaction.
567 * xfs_dialloc() is designed to be called twice if it has to do an
568 * allocation to make more free inodes. On the first call,
569 * IO_agbp should be set to NULL. If an inode is available,
570 * i.e., xfs_dialloc() did not need to do an allocation, an inode
571 * number is returned. In this case, IO_agbp would be set to the
572 * current ag_buf and alloc_done set to false.
573 * If an allocation needed to be done, xfs_dialloc would return
574 * the current ag_buf in IO_agbp and set alloc_done to true.
575 * The caller should then commit the current transaction, allocate a new
576 * transaction, and call xfs_dialloc() again, passing in the previous
577 * value of IO_agbp. IO_agbp should be held across the transactions.
578 * Since the agbp is locked across the two calls, the second call is
579 * guaranteed to have a free inode available.
580 *
581 * Once we successfully pick an inode its number is returned and the
582 * on-disk data structures are updated. The inode itself is not read
583 * in, since doing so would break ordering constraints with xfs_reclaim.
584 */
585int
586xfs_dialloc(
587 xfs_trans_t *tp, /* transaction pointer */
588 xfs_ino_t parent, /* parent inode (directory) */
589 mode_t mode, /* mode bits for new inode */
590 int okalloc, /* ok to allocate more space */
591 xfs_buf_t **IO_agbp, /* in/out ag header's buffer */
592 boolean_t *alloc_done, /* true if we needed to replenish
593 inode freelist */
594 xfs_ino_t *inop) /* inode number allocated */
595{
596 xfs_agnumber_t agcount; /* number of allocation groups */
597 xfs_buf_t *agbp; /* allocation group header's buffer */
598 xfs_agnumber_t agno; /* allocation group number */
599 xfs_agi_t *agi; /* allocation group header structure */
600 xfs_btree_cur_t *cur; /* inode allocation btree cursor */
601 int error; /* error return value */
602 int i; /* result code */
603 int ialloced; /* inode allocation status */
604 int noroom = 0; /* no space for inode blk allocation */
605 xfs_ino_t ino; /* fs-relative inode to be returned */
606 /* REFERENCED */
607 int j; /* result code */
608 xfs_mount_t *mp; /* file system mount structure */
609 int offset; /* index of inode in chunk */
610 xfs_agino_t pagino; /* parent's a.g. relative inode # */
611 xfs_agnumber_t pagno; /* parent's allocation group number */
Christoph Hellwig61a25842006-09-28 10:57:04 +1000612 xfs_inobt_rec_incore_t rec; /* inode allocation record */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 xfs_agnumber_t tagno; /* testing allocation group number */
614 xfs_btree_cur_t *tcur; /* temp cursor */
Christoph Hellwig61a25842006-09-28 10:57:04 +1000615 xfs_inobt_rec_incore_t trec; /* temp inode allocation record */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617
618 if (*IO_agbp == NULL) {
619 /*
620 * We do not have an agbp, so select an initial allocation
621 * group for inode allocation.
622 */
623 agbp = xfs_ialloc_ag_select(tp, parent, mode, okalloc);
624 /*
625 * Couldn't find an allocation group satisfying the
626 * criteria, give up.
627 */
628 if (!agbp) {
629 *inop = NULLFSINO;
630 return 0;
631 }
632 agi = XFS_BUF_TO_AGI(agbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100633 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 } else {
635 /*
636 * Continue where we left off before. In this case, we
637 * know that the allocation group has free inodes.
638 */
639 agbp = *IO_agbp;
640 agi = XFS_BUF_TO_AGI(agbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100641 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
642 ASSERT(be32_to_cpu(agi->agi_freecount) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
644 mp = tp->t_mountp;
645 agcount = mp->m_sb.sb_agcount;
Christoph Hellwig16259e72005-11-02 15:11:25 +1100646 agno = be32_to_cpu(agi->agi_seqno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 tagno = agno;
648 pagno = XFS_INO_TO_AGNO(mp, parent);
649 pagino = XFS_INO_TO_AGINO(mp, parent);
650
651 /*
652 * If we have already hit the ceiling of inode blocks then clear
653 * okalloc so we scan all available agi structures for a free
654 * inode.
655 */
656
657 if (mp->m_maxicount &&
658 mp->m_sb.sb_icount + XFS_IALLOC_INODES(mp) > mp->m_maxicount) {
659 noroom = 1;
660 okalloc = 0;
661 }
662
663 /*
664 * Loop until we find an allocation group that either has free inodes
665 * or in which we can allocate some inodes. Iterate through the
666 * allocation groups upward, wrapping at the end.
667 */
668 *alloc_done = B_FALSE;
669 while (!agi->agi_freecount) {
670 /*
671 * Don't do anything if we're not supposed to allocate
672 * any blocks, just go on to the next ag.
673 */
674 if (okalloc) {
675 /*
676 * Try to allocate some new inodes in the allocation
677 * group.
678 */
679 if ((error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced))) {
680 xfs_trans_brelse(tp, agbp);
681 if (error == ENOSPC) {
682 *inop = NULLFSINO;
683 return 0;
684 } else
685 return error;
686 }
687 if (ialloced) {
688 /*
689 * We successfully allocated some inodes, return
690 * the current context to the caller so that it
691 * can commit the current transaction and call
692 * us again where we left off.
693 */
Christoph Hellwig16259e72005-11-02 15:11:25 +1100694 ASSERT(be32_to_cpu(agi->agi_freecount) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 *alloc_done = B_TRUE;
696 *IO_agbp = agbp;
697 *inop = NULLFSINO;
698 return 0;
699 }
700 }
701 /*
702 * If it failed, give up on this ag.
703 */
704 xfs_trans_brelse(tp, agbp);
705 /*
706 * Go on to the next ag: get its ag header.
707 */
708nextag:
709 if (++tagno == agcount)
710 tagno = 0;
711 if (tagno == agno) {
712 *inop = NULLFSINO;
713 return noroom ? ENOSPC : 0;
714 }
715 down_read(&mp->m_peraglock);
716 if (mp->m_perag[tagno].pagi_inodeok == 0) {
717 up_read(&mp->m_peraglock);
718 goto nextag;
719 }
720 error = xfs_ialloc_read_agi(mp, tp, tagno, &agbp);
721 up_read(&mp->m_peraglock);
722 if (error)
723 goto nextag;
724 agi = XFS_BUF_TO_AGI(agbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100725 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 }
727 /*
728 * Here with an allocation group that has a free inode.
729 * Reset agno since we may have chosen a new ag in the
730 * loop above.
731 */
732 agno = tagno;
733 *IO_agbp = NULL;
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100734 cur = xfs_inobt_init_cursor(mp, tp, agbp, be32_to_cpu(agi->agi_seqno));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 /*
736 * If pagino is 0 (this is the root inode allocation) use newino.
737 * This must work because we've just allocated some.
738 */
739 if (!pagino)
Christoph Hellwig16259e72005-11-02 15:11:25 +1100740 pagino = be32_to_cpu(agi->agi_newino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741#ifdef DEBUG
742 if (cur->bc_nlevels == 1) {
743 int freecount = 0;
744
745 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
746 goto error0;
747 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
748 do {
749 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
750 &rec.ir_freecount, &rec.ir_free, &i)))
751 goto error0;
752 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
753 freecount += rec.ir_freecount;
Christoph Hellwig637aa502008-10-30 16:55:45 +1100754 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 goto error0;
756 } while (i == 1);
757
Christoph Hellwig16259e72005-11-02 15:11:25 +1100758 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 XFS_FORCED_SHUTDOWN(mp));
760 }
761#endif
762 /*
763 * If in the same a.g. as the parent, try to get near the parent.
764 */
765 if (pagno == agno) {
766 if ((error = xfs_inobt_lookup_le(cur, pagino, 0, 0, &i)))
767 goto error0;
768 if (i != 0 &&
769 (error = xfs_inobt_get_rec(cur, &rec.ir_startino,
770 &rec.ir_freecount, &rec.ir_free, &j)) == 0 &&
771 j == 1 &&
772 rec.ir_freecount > 0) {
773 /*
774 * Found a free inode in the same chunk
775 * as parent, done.
776 */
777 }
778 /*
779 * In the same a.g. as parent, but parent's chunk is full.
780 */
781 else {
782 int doneleft; /* done, to the left */
783 int doneright; /* done, to the right */
784
785 if (error)
786 goto error0;
787 ASSERT(i == 1);
788 ASSERT(j == 1);
789 /*
790 * Duplicate the cursor, search left & right
791 * simultaneously.
792 */
793 if ((error = xfs_btree_dup_cursor(cur, &tcur)))
794 goto error0;
795 /*
796 * Search left with tcur, back up 1 record.
797 */
Christoph Hellwig8df4da42008-10-30 16:55:58 +1100798 if ((error = xfs_btree_decrement(tcur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 goto error1;
800 doneleft = !i;
801 if (!doneleft) {
802 if ((error = xfs_inobt_get_rec(tcur,
803 &trec.ir_startino,
804 &trec.ir_freecount,
805 &trec.ir_free, &i)))
806 goto error1;
807 XFS_WANT_CORRUPTED_GOTO(i == 1, error1);
808 }
809 /*
810 * Search right with cur, go forward 1 record.
811 */
Christoph Hellwig637aa502008-10-30 16:55:45 +1100812 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 goto error1;
814 doneright = !i;
815 if (!doneright) {
816 if ((error = xfs_inobt_get_rec(cur,
817 &rec.ir_startino,
818 &rec.ir_freecount,
819 &rec.ir_free, &i)))
820 goto error1;
821 XFS_WANT_CORRUPTED_GOTO(i == 1, error1);
822 }
823 /*
824 * Loop until we find the closest inode chunk
825 * with a free one.
826 */
827 while (!doneleft || !doneright) {
828 int useleft; /* using left inode
829 chunk this time */
830
831 /*
832 * Figure out which block is closer,
833 * if both are valid.
834 */
835 if (!doneleft && !doneright)
836 useleft =
837 pagino -
838 (trec.ir_startino +
839 XFS_INODES_PER_CHUNK - 1) <
840 rec.ir_startino - pagino;
841 else
842 useleft = !doneleft;
843 /*
844 * If checking the left, does it have
845 * free inodes?
846 */
847 if (useleft && trec.ir_freecount) {
848 /*
849 * Yes, set it up as the chunk to use.
850 */
851 rec = trec;
852 xfs_btree_del_cursor(cur,
853 XFS_BTREE_NOERROR);
854 cur = tcur;
855 break;
856 }
857 /*
858 * If checking the right, does it have
859 * free inodes?
860 */
861 if (!useleft && rec.ir_freecount) {
862 /*
863 * Yes, it's already set up.
864 */
865 xfs_btree_del_cursor(tcur,
866 XFS_BTREE_NOERROR);
867 break;
868 }
869 /*
870 * If used the left, get another one
871 * further left.
872 */
873 if (useleft) {
Christoph Hellwig8df4da42008-10-30 16:55:58 +1100874 if ((error = xfs_btree_decrement(tcur, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 &i)))
876 goto error1;
877 doneleft = !i;
878 if (!doneleft) {
879 if ((error = xfs_inobt_get_rec(
880 tcur,
881 &trec.ir_startino,
882 &trec.ir_freecount,
883 &trec.ir_free, &i)))
884 goto error1;
885 XFS_WANT_CORRUPTED_GOTO(i == 1,
886 error1);
887 }
888 }
889 /*
890 * If used the right, get another one
891 * further right.
892 */
893 else {
Christoph Hellwig637aa502008-10-30 16:55:45 +1100894 if ((error = xfs_btree_increment(cur, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 &i)))
896 goto error1;
897 doneright = !i;
898 if (!doneright) {
899 if ((error = xfs_inobt_get_rec(
900 cur,
901 &rec.ir_startino,
902 &rec.ir_freecount,
903 &rec.ir_free, &i)))
904 goto error1;
905 XFS_WANT_CORRUPTED_GOTO(i == 1,
906 error1);
907 }
908 }
909 }
910 ASSERT(!doneleft || !doneright);
911 }
912 }
913 /*
914 * In a different a.g. from the parent.
915 * See if the most recently allocated block has any free.
916 */
Christoph Hellwig16259e72005-11-02 15:11:25 +1100917 else if (be32_to_cpu(agi->agi_newino) != NULLAGINO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 if ((error = xfs_inobt_lookup_eq(cur,
Christoph Hellwig16259e72005-11-02 15:11:25 +1100919 be32_to_cpu(agi->agi_newino), 0, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 goto error0;
921 if (i == 1 &&
922 (error = xfs_inobt_get_rec(cur, &rec.ir_startino,
923 &rec.ir_freecount, &rec.ir_free, &j)) == 0 &&
924 j == 1 &&
925 rec.ir_freecount > 0) {
926 /*
927 * The last chunk allocated in the group still has
928 * a free inode.
929 */
930 }
931 /*
932 * None left in the last group, search the whole a.g.
933 */
934 else {
935 if (error)
936 goto error0;
937 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
938 goto error0;
939 ASSERT(i == 1);
940 for (;;) {
941 if ((error = xfs_inobt_get_rec(cur,
942 &rec.ir_startino,
943 &rec.ir_freecount, &rec.ir_free,
944 &i)))
945 goto error0;
946 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
947 if (rec.ir_freecount > 0)
948 break;
Christoph Hellwig637aa502008-10-30 16:55:45 +1100949 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 goto error0;
951 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
952 }
953 }
954 }
Eric Sandeen9d87c312009-01-14 23:22:07 -0600955 offset = xfs_ialloc_find_free(&rec.ir_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 ASSERT(offset >= 0);
957 ASSERT(offset < XFS_INODES_PER_CHUNK);
958 ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
959 XFS_INODES_PER_CHUNK) == 0);
960 ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
Christoph Hellwig0d87e652009-02-09 08:37:14 +0100961 rec.ir_free &= ~XFS_INOBT_MASK(offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 rec.ir_freecount--;
963 if ((error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount,
964 rec.ir_free)))
965 goto error0;
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800966 be32_add_cpu(&agi->agi_freecount, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
968 down_read(&mp->m_peraglock);
969 mp->m_perag[tagno].pagi_freecount--;
970 up_read(&mp->m_peraglock);
971#ifdef DEBUG
972 if (cur->bc_nlevels == 1) {
973 int freecount = 0;
974
975 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
976 goto error0;
977 do {
978 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
979 &rec.ir_freecount, &rec.ir_free, &i)))
980 goto error0;
981 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
982 freecount += rec.ir_freecount;
Christoph Hellwig637aa502008-10-30 16:55:45 +1100983 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 goto error0;
985 } while (i == 1);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100986 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 XFS_FORCED_SHUTDOWN(mp));
988 }
989#endif
990 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
991 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
992 *inop = ino;
993 return 0;
994error1:
995 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
996error0:
997 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
998 return error;
999}
1000
1001/*
1002 * Free disk inode. Carefully avoids touching the incore inode, all
1003 * manipulations incore are the caller's responsibility.
1004 * The on-disk inode is not changed by this operation, only the
1005 * btree (free inode mask) is changed.
1006 */
1007int
1008xfs_difree(
1009 xfs_trans_t *tp, /* transaction pointer */
1010 xfs_ino_t inode, /* inode to be freed */
1011 xfs_bmap_free_t *flist, /* extents to free */
1012 int *delete, /* set if inode cluster was deleted */
1013 xfs_ino_t *first_ino) /* first inode in deleted cluster */
1014{
1015 /* REFERENCED */
1016 xfs_agblock_t agbno; /* block number containing inode */
1017 xfs_buf_t *agbp; /* buffer containing allocation group header */
1018 xfs_agino_t agino; /* inode number relative to allocation group */
1019 xfs_agnumber_t agno; /* allocation group number */
1020 xfs_agi_t *agi; /* allocation group header */
1021 xfs_btree_cur_t *cur; /* inode btree cursor */
1022 int error; /* error return value */
1023 int i; /* result code */
1024 int ilen; /* inodes in an inode cluster */
1025 xfs_mount_t *mp; /* mount structure for filesystem */
1026 int off; /* offset of inode in inode chunk */
Christoph Hellwig61a25842006-09-28 10:57:04 +10001027 xfs_inobt_rec_incore_t rec; /* btree record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
1029 mp = tp->t_mountp;
1030
1031 /*
1032 * Break up inode number into its components.
1033 */
1034 agno = XFS_INO_TO_AGNO(mp, inode);
1035 if (agno >= mp->m_sb.sb_agcount) {
1036 cmn_err(CE_WARN,
1037 "xfs_difree: agno >= mp->m_sb.sb_agcount (%d >= %d) on %s. Returning EINVAL.",
1038 agno, mp->m_sb.sb_agcount, mp->m_fsname);
1039 ASSERT(0);
1040 return XFS_ERROR(EINVAL);
1041 }
1042 agino = XFS_INO_TO_AGINO(mp, inode);
1043 if (inode != XFS_AGINO_TO_INO(mp, agno, agino)) {
1044 cmn_err(CE_WARN,
Christoph Hellwigda1650a2005-11-02 10:21:35 +11001045 "xfs_difree: inode != XFS_AGINO_TO_INO() "
1046 "(%llu != %llu) on %s. Returning EINVAL.",
1047 (unsigned long long)inode,
1048 (unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino),
1049 mp->m_fsname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 ASSERT(0);
1051 return XFS_ERROR(EINVAL);
1052 }
1053 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1054 if (agbno >= mp->m_sb.sb_agblocks) {
1055 cmn_err(CE_WARN,
1056 "xfs_difree: agbno >= mp->m_sb.sb_agblocks (%d >= %d) on %s. Returning EINVAL.",
1057 agbno, mp->m_sb.sb_agblocks, mp->m_fsname);
1058 ASSERT(0);
1059 return XFS_ERROR(EINVAL);
1060 }
1061 /*
1062 * Get the allocation group header.
1063 */
1064 down_read(&mp->m_peraglock);
1065 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1066 up_read(&mp->m_peraglock);
1067 if (error) {
1068 cmn_err(CE_WARN,
1069 "xfs_difree: xfs_ialloc_read_agi() returned an error %d on %s. Returning error.",
1070 error, mp->m_fsname);
1071 return error;
1072 }
1073 agi = XFS_BUF_TO_AGI(agbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001074 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
1075 ASSERT(agbno < be32_to_cpu(agi->agi_length));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 /*
1077 * Initialize the cursor.
1078 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001079 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080#ifdef DEBUG
1081 if (cur->bc_nlevels == 1) {
1082 int freecount = 0;
1083
1084 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
1085 goto error0;
1086 do {
1087 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
1088 &rec.ir_freecount, &rec.ir_free, &i)))
1089 goto error0;
1090 if (i) {
1091 freecount += rec.ir_freecount;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001092 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 goto error0;
1094 }
1095 } while (i == 1);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001096 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 XFS_FORCED_SHUTDOWN(mp));
1098 }
1099#endif
1100 /*
1101 * Look for the entry describing this inode.
1102 */
1103 if ((error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i))) {
1104 cmn_err(CE_WARN,
1105 "xfs_difree: xfs_inobt_lookup_le returned() an error %d on %s. Returning error.",
1106 error, mp->m_fsname);
1107 goto error0;
1108 }
1109 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1110 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino, &rec.ir_freecount,
1111 &rec.ir_free, &i))) {
1112 cmn_err(CE_WARN,
1113 "xfs_difree: xfs_inobt_get_rec() returned an error %d on %s. Returning error.",
1114 error, mp->m_fsname);
1115 goto error0;
1116 }
1117 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1118 /*
1119 * Get the offset in the inode chunk.
1120 */
1121 off = agino - rec.ir_startino;
1122 ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK);
Christoph Hellwig0d87e652009-02-09 08:37:14 +01001123 ASSERT(!(rec.ir_free & XFS_INOBT_MASK(off)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 /*
1125 * Mark the inode free & increment the count.
1126 */
Christoph Hellwig0d87e652009-02-09 08:37:14 +01001127 rec.ir_free |= XFS_INOBT_MASK(off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 rec.ir_freecount++;
1129
1130 /*
Nathan Scottc41564b2006-03-29 08:55:14 +10001131 * When an inode cluster is free, it becomes eligible for removal
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 */
Josef Jeff Sipek1bd960e2008-02-29 13:58:40 +11001133 if (!(mp->m_flags & XFS_MOUNT_IKEEP) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 (rec.ir_freecount == XFS_IALLOC_INODES(mp))) {
1135
1136 *delete = 1;
1137 *first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino);
1138
1139 /*
1140 * Remove the inode cluster from the AGI B+Tree, adjust the
1141 * AGI and Superblock inode counts, and mark the disk space
1142 * to be freed when the transaction is committed.
1143 */
1144 ilen = XFS_IALLOC_INODES(mp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001145 be32_add_cpu(&agi->agi_count, -ilen);
1146 be32_add_cpu(&agi->agi_freecount, -(ilen - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT);
1148 down_read(&mp->m_peraglock);
1149 mp->m_perag[agno].pagi_freecount -= ilen - 1;
1150 up_read(&mp->m_peraglock);
1151 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen);
1152 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1));
1153
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001154 if ((error = xfs_btree_delete(cur, &i))) {
1155 cmn_err(CE_WARN, "xfs_difree: xfs_btree_delete returned an error %d on %s.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 error, mp->m_fsname);
1157 goto error0;
1158 }
1159
1160 xfs_bmap_add_free(XFS_AGB_TO_FSB(mp,
1161 agno, XFS_INO_TO_AGBNO(mp,rec.ir_startino)),
1162 XFS_IALLOC_BLOCKS(mp), flist, mp);
1163 } else {
1164 *delete = 0;
1165
1166 if ((error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount, rec.ir_free))) {
1167 cmn_err(CE_WARN,
1168 "xfs_difree: xfs_inobt_update() returned an error %d on %s. Returning error.",
1169 error, mp->m_fsname);
1170 goto error0;
1171 }
1172 /*
1173 * Change the inode free counts and log the ag/sb changes.
1174 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001175 be32_add_cpu(&agi->agi_freecount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
1177 down_read(&mp->m_peraglock);
1178 mp->m_perag[agno].pagi_freecount++;
1179 up_read(&mp->m_peraglock);
1180 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1);
1181 }
1182
1183#ifdef DEBUG
1184 if (cur->bc_nlevels == 1) {
1185 int freecount = 0;
1186
1187 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
1188 goto error0;
1189 do {
1190 if ((error = xfs_inobt_get_rec(cur,
1191 &rec.ir_startino,
1192 &rec.ir_freecount,
1193 &rec.ir_free, &i)))
1194 goto error0;
1195 if (i) {
1196 freecount += rec.ir_freecount;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001197 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 goto error0;
1199 }
1200 } while (i == 1);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001201 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 XFS_FORCED_SHUTDOWN(mp));
1203 }
1204#endif
1205 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1206 return 0;
1207
1208error0:
1209 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1210 return error;
1211}
1212
1213/*
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001214 * Return the location of the inode in imap, for mapping it into a buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216int
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001217xfs_imap(
1218 xfs_mount_t *mp, /* file system mount structure */
1219 xfs_trans_t *tp, /* transaction pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 xfs_ino_t ino, /* inode to locate */
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001221 struct xfs_imap *imap, /* location map structure */
1222 uint flags) /* flags for inode btree lookup */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
1224 xfs_agblock_t agbno; /* block number of inode in the alloc group */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 xfs_agino_t agino; /* inode number within alloc group */
1226 xfs_agnumber_t agno; /* allocation group number */
1227 int blks_per_cluster; /* num blocks per inode cluster */
1228 xfs_agblock_t chunk_agbno; /* first block in inode chunk */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 xfs_agblock_t cluster_agbno; /* first block in inode cluster */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 int error; /* error code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 int offset; /* index of inode in its buffer */
1232 int offset_agbno; /* blks from chunk start to inode */
1233
1234 ASSERT(ino != NULLFSINO);
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001235
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 /*
1237 * Split up the inode number into its parts.
1238 */
1239 agno = XFS_INO_TO_AGNO(mp, ino);
1240 agino = XFS_INO_TO_AGINO(mp, ino);
1241 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1242 if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks ||
1243 ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1244#ifdef DEBUG
Nathan Scott4d1a2ed2006-06-09 17:12:28 +10001245 /* no diagnostics for bulkstat, ino comes from userspace */
Christoph Hellwigb48d8d62008-11-28 14:23:41 +11001246 if (flags & XFS_IGET_BULKSTAT)
Nathan Scott4d1a2ed2006-06-09 17:12:28 +10001247 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 if (agno >= mp->m_sb.sb_agcount) {
1249 xfs_fs_cmn_err(CE_ALERT, mp,
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001250 "xfs_imap: agno (%d) >= "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 "mp->m_sb.sb_agcount (%d)",
1252 agno, mp->m_sb.sb_agcount);
1253 }
1254 if (agbno >= mp->m_sb.sb_agblocks) {
1255 xfs_fs_cmn_err(CE_ALERT, mp,
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001256 "xfs_imap: agbno (0x%llx) >= "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 "mp->m_sb.sb_agblocks (0x%lx)",
1258 (unsigned long long) agbno,
1259 (unsigned long) mp->m_sb.sb_agblocks);
1260 }
1261 if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1262 xfs_fs_cmn_err(CE_ALERT, mp,
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001263 "xfs_imap: ino (0x%llx) != "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 "XFS_AGINO_TO_INO(mp, agno, agino) "
1265 "(0x%llx)",
1266 ino, XFS_AGINO_TO_INO(mp, agno, agino));
1267 }
Nathan Scott745b1f472006-09-28 11:02:23 +10001268 xfs_stack_trace();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269#endif /* DEBUG */
1270 return XFS_ERROR(EINVAL);
1271 }
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001272
1273 /*
1274 * If the inode cluster size is the same as the blocksize or
1275 * smaller we get to the buffer by simple arithmetics.
1276 */
1277 if (XFS_INODE_CLUSTER_SIZE(mp) <= mp->m_sb.sb_blocksize) {
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
1281 imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, agbno);
1282 imap->im_len = XFS_FSB_TO_BB(mp, 1);
1283 imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return 0;
1285 }
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001286
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_blocklog;
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001288
1289 /*
1290 * If we get a block number passed from bulkstat we can use it to
1291 * find the buffer easily.
1292 */
1293 if (imap->im_blkno) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 offset = XFS_INO_TO_OFFSET(mp, ino);
1295 ASSERT(offset < mp->m_sb.sb_inopblock);
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001296
Eric Sandeen9d87c312009-01-14 23:22:07 -06001297 cluster_agbno = xfs_daddr_to_agbno(mp, imap->im_blkno);
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001298 offset += (agbno - cluster_agbno) * mp->m_sb.sb_inopblock;
1299
1300 imap->im_len = XFS_FSB_TO_BB(mp, blks_per_cluster);
1301 imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 return 0;
1303 }
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001304
1305 /*
1306 * If the inode chunks are aligned then use simple maths to
1307 * find the location. Otherwise we have to do a btree
1308 * lookup to find the location.
1309 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 if (mp->m_inoalign_mask) {
1311 offset_agbno = agbno & mp->m_inoalign_mask;
1312 chunk_agbno = agbno - offset_agbno;
1313 } else {
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001314 xfs_btree_cur_t *cur; /* inode btree cursor */
1315 xfs_agino_t chunk_agino; /* first agino in inode chunk */
1316 __int32_t chunk_cnt; /* count of free inodes in chunk */
1317 xfs_inofree_t chunk_free; /* mask of free inodes in chunk */
1318 xfs_buf_t *agbp; /* agi buffer */
1319 int i; /* temp state */
1320
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 down_read(&mp->m_peraglock);
1322 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1323 up_read(&mp->m_peraglock);
1324 if (error) {
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001325 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 "xfs_ialloc_read_agi() returned "
1327 "error %d, agno %d",
1328 error, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 return error;
1330 }
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001331
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001332 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno);
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001333 error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i);
1334 if (error) {
1335 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 "xfs_inobt_lookup_le() failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 goto error0;
1338 }
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001339
1340 error = xfs_inobt_get_rec(cur, &chunk_agino, &chunk_cnt,
1341 &chunk_free, &i);
1342 if (error) {
1343 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 "xfs_inobt_get_rec() failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 goto error0;
1346 }
1347 if (i == 0) {
1348#ifdef DEBUG
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001349 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 "xfs_inobt_get_rec() failed");
1351#endif /* DEBUG */
1352 error = XFS_ERROR(EINVAL);
1353 }
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001354 error0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 xfs_trans_brelse(tp, agbp);
1356 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1357 if (error)
1358 return error;
1359 chunk_agbno = XFS_AGINO_TO_AGBNO(mp, chunk_agino);
1360 offset_agbno = agbno - chunk_agbno;
1361 }
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 ASSERT(agbno >= chunk_agbno);
1364 cluster_agbno = chunk_agbno +
1365 ((offset_agbno / blks_per_cluster) * blks_per_cluster);
1366 offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
1367 XFS_INO_TO_OFFSET(mp, ino);
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001368
1369 imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, cluster_agbno);
1370 imap->im_len = XFS_FSB_TO_BB(mp, blks_per_cluster);
1371 imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
1372
1373 /*
1374 * If the inode number maps to a block outside the bounds
1375 * of the file system then return NULL rather than calling
1376 * read_buf and panicing when we get an error from the
1377 * driver.
1378 */
1379 if ((imap->im_blkno + imap->im_len) >
1380 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) {
1381 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: "
1382 "(imap->im_blkno (0x%llx) + imap->im_len (0x%llx)) > "
1383 " XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks) (0x%llx)",
1384 (unsigned long long) imap->im_blkno,
1385 (unsigned long long) imap->im_len,
1386 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks));
1387 return XFS_ERROR(EINVAL);
1388 }
1389
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391}
1392
1393/*
1394 * Compute and fill in value of m_in_maxlevels.
1395 */
1396void
1397xfs_ialloc_compute_maxlevels(
1398 xfs_mount_t *mp) /* file system mount structure */
1399{
1400 int level;
1401 uint maxblocks;
1402 uint maxleafents;
1403 int minleafrecs;
1404 int minnoderecs;
1405
1406 maxleafents = (1LL << XFS_INO_AGINO_BITS(mp)) >>
1407 XFS_INODES_PER_CHUNK_LOG;
1408 minleafrecs = mp->m_alloc_mnr[0];
1409 minnoderecs = mp->m_alloc_mnr[1];
1410 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
1411 for (level = 1; maxblocks > 1; level++)
1412 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
1413 mp->m_in_maxlevels = level;
1414}
1415
1416/*
1417 * Log specified fields for the ag hdr (inode section)
1418 */
1419void
1420xfs_ialloc_log_agi(
1421 xfs_trans_t *tp, /* transaction pointer */
1422 xfs_buf_t *bp, /* allocation group header buffer */
1423 int fields) /* bitmask of fields to log */
1424{
1425 int first; /* first byte number */
1426 int last; /* last byte number */
1427 static const short offsets[] = { /* field starting offsets */
1428 /* keep in sync with bit definitions */
1429 offsetof(xfs_agi_t, agi_magicnum),
1430 offsetof(xfs_agi_t, agi_versionnum),
1431 offsetof(xfs_agi_t, agi_seqno),
1432 offsetof(xfs_agi_t, agi_length),
1433 offsetof(xfs_agi_t, agi_count),
1434 offsetof(xfs_agi_t, agi_root),
1435 offsetof(xfs_agi_t, agi_level),
1436 offsetof(xfs_agi_t, agi_freecount),
1437 offsetof(xfs_agi_t, agi_newino),
1438 offsetof(xfs_agi_t, agi_dirino),
1439 offsetof(xfs_agi_t, agi_unlinked),
1440 sizeof(xfs_agi_t)
1441 };
1442#ifdef DEBUG
1443 xfs_agi_t *agi; /* allocation group header */
1444
1445 agi = XFS_BUF_TO_AGI(bp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001446 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447#endif
1448 /*
1449 * Compute byte offsets for the first and last fields.
1450 */
1451 xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS, &first, &last);
1452 /*
1453 * Log the allocation group inode header buffer.
1454 */
1455 xfs_trans_log_buf(tp, bp, first, last);
1456}
1457
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001458#ifdef DEBUG
1459STATIC void
1460xfs_check_agi_unlinked(
1461 struct xfs_agi *agi)
1462{
1463 int i;
1464
1465 for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++)
1466 ASSERT(agi->agi_unlinked[i]);
1467}
1468#else
1469#define xfs_check_agi_unlinked(agi)
1470#endif
1471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472/*
1473 * Read in the allocation group header (inode allocation section)
1474 */
1475int
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001476xfs_read_agi(
1477 struct xfs_mount *mp, /* file system mount structure */
1478 struct xfs_trans *tp, /* transaction pointer */
1479 xfs_agnumber_t agno, /* allocation group number */
1480 struct xfs_buf **bpp) /* allocation group hdr buf */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001482 struct xfs_agi *agi; /* allocation group header */
1483 int agi_ok; /* agi is consistent */
1484 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
1486 ASSERT(agno != NULLAGNUMBER);
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001487
1488 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001490 XFS_FSS_TO_BB(mp, 1), 0, bpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 if (error)
1492 return error;
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001493
1494 ASSERT(*bpp && !XFS_BUF_GETERROR(*bpp));
1495 agi = XFS_BUF_TO_AGI(*bpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
1497 /*
1498 * Validate the magic number of the agi block.
1499 */
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001500 agi_ok = be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC &&
1501 XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum)) &&
1502 be32_to_cpu(agi->agi_seqno) == agno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 if (unlikely(XFS_TEST_ERROR(!agi_ok, mp, XFS_ERRTAG_IALLOC_READ_AGI,
1504 XFS_RANDOM_IALLOC_READ_AGI))) {
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001505 XFS_CORRUPTION_ERROR("xfs_read_agi", XFS_ERRLEVEL_LOW,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 mp, agi);
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001507 xfs_trans_brelse(tp, *bpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 return XFS_ERROR(EFSCORRUPTED);
1509 }
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001510
1511 XFS_BUF_SET_VTYPE_REF(*bpp, B_FS_AGI, XFS_AGI_REF);
1512
1513 xfs_check_agi_unlinked(agi);
1514 return 0;
1515}
1516
1517int
1518xfs_ialloc_read_agi(
1519 struct xfs_mount *mp, /* file system mount structure */
1520 struct xfs_trans *tp, /* transaction pointer */
1521 xfs_agnumber_t agno, /* allocation group number */
1522 struct xfs_buf **bpp) /* allocation group hdr buf */
1523{
1524 struct xfs_agi *agi; /* allocation group header */
1525 struct xfs_perag *pag; /* per allocation group data */
1526 int error;
1527
1528 error = xfs_read_agi(mp, tp, agno, bpp);
1529 if (error)
1530 return error;
1531
1532 agi = XFS_BUF_TO_AGI(*bpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 pag = &mp->m_perag[agno];
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 if (!pag->pagi_init) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11001536 pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
David Chinner92821e22007-05-24 15:26:31 +10001537 pag->pagi_count = be32_to_cpu(agi->agi_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 pag->pagi_init = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 }
1540
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001541 /*
1542 * It's possible for these to be out of sync if
1543 * we are in the middle of a forced shutdown.
1544 */
1545 ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) ||
1546 XFS_FORCED_SHUTDOWN(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 return 0;
1548}
David Chinner92821e22007-05-24 15:26:31 +10001549
1550/*
1551 * Read in the agi to initialise the per-ag data in the mount structure
1552 */
1553int
1554xfs_ialloc_pagi_init(
1555 xfs_mount_t *mp, /* file system mount structure */
1556 xfs_trans_t *tp, /* transaction pointer */
1557 xfs_agnumber_t agno) /* allocation group number */
1558{
1559 xfs_buf_t *bp = NULL;
1560 int error;
1561
1562 error = xfs_ialloc_read_agi(mp, tp, agno, &bp);
1563 if (error)
1564 return error;
1565 if (bp)
1566 xfs_trans_brelse(tp, bp);
1567 return 0;
1568}