blob: 3a8f0670e07075b3751583c25cfd8116f56f8ccf [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
44/*
45 * Log specified fields for the inode given by bp and off.
46 */
47STATIC void
48xfs_ialloc_log_di(
49 xfs_trans_t *tp, /* transaction pointer */
50 xfs_buf_t *bp, /* inode buffer */
51 int off, /* index of inode in buffer */
52 int fields) /* bitmask of fields to log */
53{
54 int first; /* first byte number */
55 int ioffset; /* off in bytes */
56 int last; /* last byte number */
57 xfs_mount_t *mp; /* mount point structure */
58 static const short offsets[] = { /* field offsets */
59 /* keep in sync with bits */
60 offsetof(xfs_dinode_core_t, di_magic),
61 offsetof(xfs_dinode_core_t, di_mode),
62 offsetof(xfs_dinode_core_t, di_version),
63 offsetof(xfs_dinode_core_t, di_format),
64 offsetof(xfs_dinode_core_t, di_onlink),
65 offsetof(xfs_dinode_core_t, di_uid),
66 offsetof(xfs_dinode_core_t, di_gid),
67 offsetof(xfs_dinode_core_t, di_nlink),
68 offsetof(xfs_dinode_core_t, di_projid),
69 offsetof(xfs_dinode_core_t, di_pad),
70 offsetof(xfs_dinode_core_t, di_atime),
71 offsetof(xfs_dinode_core_t, di_mtime),
72 offsetof(xfs_dinode_core_t, di_ctime),
73 offsetof(xfs_dinode_core_t, di_size),
74 offsetof(xfs_dinode_core_t, di_nblocks),
75 offsetof(xfs_dinode_core_t, di_extsize),
76 offsetof(xfs_dinode_core_t, di_nextents),
77 offsetof(xfs_dinode_core_t, di_anextents),
78 offsetof(xfs_dinode_core_t, di_forkoff),
79 offsetof(xfs_dinode_core_t, di_aformat),
80 offsetof(xfs_dinode_core_t, di_dmevmask),
81 offsetof(xfs_dinode_core_t, di_dmstate),
82 offsetof(xfs_dinode_core_t, di_flags),
83 offsetof(xfs_dinode_core_t, di_gen),
84 offsetof(xfs_dinode_t, di_next_unlinked),
85 offsetof(xfs_dinode_t, di_u),
86 offsetof(xfs_dinode_t, di_a),
87 sizeof(xfs_dinode_t)
88 };
89
90
91 ASSERT(offsetof(xfs_dinode_t, di_core) == 0);
92 ASSERT((fields & (XFS_DI_U|XFS_DI_A)) == 0);
93 mp = tp->t_mountp;
94 /*
95 * Get the inode-relative first and last bytes for these fields
96 */
97 xfs_btree_offsets(fields, offsets, XFS_DI_NUM_BITS, &first, &last);
98 /*
99 * Convert to buffer offsets and log it.
100 */
101 ioffset = off << mp->m_sb.sb_inodelog;
102 first += ioffset;
103 last += ioffset;
104 xfs_trans_log_buf(tp, bp, first, last);
105}
106
107/*
108 * Allocation group level functions.
109 */
David Chinner75de2a92008-03-27 18:00:38 +1100110static inline int
111xfs_ialloc_cluster_alignment(
112 xfs_alloc_arg_t *args)
113{
114 if (xfs_sb_version_hasalign(&args->mp->m_sb) &&
115 args->mp->m_sb.sb_inoalignmt >=
116 XFS_B_TO_FSBT(args->mp, XFS_INODE_CLUSTER_SIZE(args->mp)))
117 return args->mp->m_sb.sb_inoalignmt;
118 return 1;
119}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121/*
122 * Allocate new inodes in the allocation group specified by agbp.
123 * Return 0 for success, else error code.
124 */
125STATIC int /* error code or 0 */
126xfs_ialloc_ag_alloc(
127 xfs_trans_t *tp, /* transaction pointer */
128 xfs_buf_t *agbp, /* alloc group buffer */
129 int *alloc)
130{
131 xfs_agi_t *agi; /* allocation group header */
132 xfs_alloc_arg_t args; /* allocation argument structure */
133 int blks_per_cluster; /* fs blocks per inode cluster */
134 xfs_btree_cur_t *cur; /* inode btree cursor */
135 xfs_daddr_t d; /* disk addr of buffer */
David Chinner92821e22007-05-24 15:26:31 +1000136 xfs_agnumber_t agno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 int error;
138 xfs_buf_t *fbuf; /* new free inodes' buffer */
139 xfs_dinode_t *free; /* new free inode structure */
140 int i; /* inode counter */
141 int j; /* block counter */
142 int nbufs; /* num bufs of new inodes */
143 xfs_agino_t newino; /* new first inode's number */
144 xfs_agino_t newlen; /* new number of inodes */
145 int ninodes; /* num inodes per buf */
146 xfs_agino_t thisino; /* current inode number, for loop */
147 int version; /* inode version number to use */
Glen Overby3ccb8b52006-03-29 09:52:28 +1000148 int isaligned = 0; /* inode allocation at stripe unit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 /* boundary */
David Chinner359346a2008-04-29 12:53:32 +1000150 unsigned int gen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152 args.tp = tp;
153 args.mp = tp->t_mountp;
154
155 /*
156 * Locking will ensure that we don't have two callers in here
157 * at one time.
158 */
159 newlen = XFS_IALLOC_INODES(args.mp);
160 if (args.mp->m_maxicount &&
161 args.mp->m_sb.sb_icount + newlen > args.mp->m_maxicount)
162 return XFS_ERROR(ENOSPC);
163 args.minlen = args.maxlen = XFS_IALLOC_BLOCKS(args.mp);
164 /*
Glen Overby3ccb8b52006-03-29 09:52:28 +1000165 * First try to allocate inodes contiguous with the last-allocated
166 * chunk of inodes. If the filesystem is striped, this will fill
167 * an entire stripe unit with inodes.
168 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 agi = XFS_BUF_TO_AGI(agbp);
Glen Overby3ccb8b52006-03-29 09:52:28 +1000170 newino = be32_to_cpu(agi->agi_newino);
Nathan Scott019ff2d2006-04-11 15:45:05 +1000171 args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) +
172 XFS_IALLOC_BLOCKS(args.mp);
173 if (likely(newino != NULLAGINO &&
174 (args.agbno < be32_to_cpu(agi->agi_length)))) {
Glen Overby3ccb8b52006-03-29 09:52:28 +1000175 args.fsbno = XFS_AGB_TO_FSB(args.mp,
176 be32_to_cpu(agi->agi_seqno), args.agbno);
177 args.type = XFS_ALLOCTYPE_THIS_BNO;
178 args.mod = args.total = args.wasdel = args.isfl =
179 args.userdata = args.minalignslop = 0;
180 args.prod = 1;
David Chinner75de2a92008-03-27 18:00:38 +1100181
Glen Overby3ccb8b52006-03-29 09:52:28 +1000182 /*
David Chinner75de2a92008-03-27 18:00:38 +1100183 * We need to take into account alignment here to ensure that
184 * we don't modify the free list if we fail to have an exact
185 * block. If we don't have an exact match, and every oher
186 * attempt allocation attempt fails, we'll end up cancelling
187 * a dirty transaction and shutting down.
188 *
189 * For an exact allocation, alignment must be 1,
190 * however we need to take cluster alignment into account when
191 * fixing up the freelist. Use the minalignslop field to
192 * indicate that extra blocks might be required for alignment,
193 * but not to use them in the actual exact allocation.
Glen Overby3ccb8b52006-03-29 09:52:28 +1000194 */
David Chinner75de2a92008-03-27 18:00:38 +1100195 args.alignment = 1;
196 args.minalignslop = xfs_ialloc_cluster_alignment(&args) - 1;
197
198 /* Allow space for the inode btree to split. */
Glen Overby3ccb8b52006-03-29 09:52:28 +1000199 args.minleft = XFS_IN_MAXLEVELS(args.mp) - 1;
200 if ((error = xfs_alloc_vextent(&args)))
201 return error;
202 } else
203 args.fsbno = NULLFSBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Glen Overby3ccb8b52006-03-29 09:52:28 +1000205 if (unlikely(args.fsbno == NULLFSBLOCK)) {
206 /*
207 * Set the alignment for the allocation.
208 * If stripe alignment is turned on then align at stripe unit
209 * boundary.
Nathan Scott019ff2d2006-04-11 15:45:05 +1000210 * If the cluster size is smaller than a filesystem block
211 * then we're doing I/O for inodes in filesystem block size
Glen Overby3ccb8b52006-03-29 09:52:28 +1000212 * pieces, so don't need alignment anyway.
213 */
214 isaligned = 0;
215 if (args.mp->m_sinoalign) {
216 ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN));
217 args.alignment = args.mp->m_dalign;
218 isaligned = 1;
David Chinner75de2a92008-03-27 18:00:38 +1100219 } else
220 args.alignment = xfs_ialloc_cluster_alignment(&args);
Glen Overby3ccb8b52006-03-29 09:52:28 +1000221 /*
222 * Need to figure out where to allocate the inode blocks.
223 * Ideally they should be spaced out through the a.g.
224 * For now, just allocate blocks up front.
225 */
226 args.agbno = be32_to_cpu(agi->agi_root);
227 args.fsbno = XFS_AGB_TO_FSB(args.mp,
228 be32_to_cpu(agi->agi_seqno), args.agbno);
229 /*
230 * Allocate a fixed-size extent of inodes.
231 */
232 args.type = XFS_ALLOCTYPE_NEAR_BNO;
233 args.mod = args.total = args.wasdel = args.isfl =
234 args.userdata = args.minalignslop = 0;
235 args.prod = 1;
236 /*
237 * Allow space for the inode btree to split.
238 */
239 args.minleft = XFS_IN_MAXLEVELS(args.mp) - 1;
240 if ((error = xfs_alloc_vextent(&args)))
241 return error;
242 }
Nathan Scott019ff2d2006-04-11 15:45:05 +1000243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 /*
245 * If stripe alignment is turned on, then try again with cluster
246 * alignment.
247 */
248 if (isaligned && args.fsbno == NULLFSBLOCK) {
249 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Christoph Hellwig16259e72005-11-02 15:11:25 +1100250 args.agbno = be32_to_cpu(agi->agi_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 args.fsbno = XFS_AGB_TO_FSB(args.mp,
Christoph Hellwig16259e72005-11-02 15:11:25 +1100252 be32_to_cpu(agi->agi_seqno), args.agbno);
David Chinner75de2a92008-03-27 18:00:38 +1100253 args.alignment = xfs_ialloc_cluster_alignment(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if ((error = xfs_alloc_vextent(&args)))
255 return error;
256 }
257
258 if (args.fsbno == NULLFSBLOCK) {
259 *alloc = 0;
260 return 0;
261 }
262 ASSERT(args.len == args.minlen);
263 /*
264 * Convert the results.
265 */
266 newino = XFS_OFFBNO_TO_AGINO(args.mp, args.agbno, 0);
267 /*
268 * Loop over the new block(s), filling in the inodes.
269 * For small block sizes, manipulate the inodes in buffers
270 * which are multiples of the blocks size.
271 */
272 if (args.mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(args.mp)) {
273 blks_per_cluster = 1;
274 nbufs = (int)args.len;
275 ninodes = args.mp->m_sb.sb_inopblock;
276 } else {
277 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(args.mp) /
278 args.mp->m_sb.sb_blocksize;
279 nbufs = (int)args.len / blks_per_cluster;
280 ninodes = blks_per_cluster * args.mp->m_sb.sb_inopblock;
281 }
282 /*
283 * Figure out what version number to use in the inodes we create.
284 * If the superblock version has caught up to the one that supports
285 * the new inode format, then use the new inode version. Otherwise
286 * use the old version so that old kernels will continue to be
287 * able to use the file system.
288 */
Eric Sandeen62118702008-03-06 13:44:28 +1100289 if (xfs_sb_version_hasnlink(&args.mp->m_sb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 version = XFS_DINODE_VERSION_2;
291 else
292 version = XFS_DINODE_VERSION_1;
293
David Chinner359346a2008-04-29 12:53:32 +1000294 /*
295 * Seed the new inode cluster with a random generation number. This
296 * prevents short-term reuse of generation numbers if a chunk is
297 * freed and then immediately reallocated. We use random numbers
298 * rather than a linear progression to prevent the next generation
299 * number from being easily guessable.
300 */
301 gen = random32();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 for (j = 0; j < nbufs; j++) {
303 /*
304 * Get the block.
305 */
Christoph Hellwig16259e72005-11-02 15:11:25 +1100306 d = XFS_AGB_TO_DADDR(args.mp, be32_to_cpu(agi->agi_seqno),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 args.agbno + (j * blks_per_cluster));
308 fbuf = xfs_trans_get_buf(tp, args.mp->m_ddev_targp, d,
309 args.mp->m_bsize * blks_per_cluster,
310 XFS_BUF_LOCK);
311 ASSERT(fbuf);
312 ASSERT(!XFS_BUF_GETERROR(fbuf));
313 /*
Nathan Scottf30a1212006-03-14 14:07:36 +1100314 * Set initial values for the inodes in this buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 */
Nathan Scottf30a1212006-03-14 14:07:36 +1100316 xfs_biozero(fbuf, 0, ninodes << args.mp->m_sb.sb_inodelog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 for (i = 0; i < ninodes; i++) {
318 free = XFS_MAKE_IPTR(args.mp, fbuf, i);
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000319 free->di_core.di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
320 free->di_core.di_version = version;
David Chinner359346a2008-04-29 12:53:32 +1000321 free->di_core.di_gen = cpu_to_be32(gen);
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000322 free->di_next_unlinked = cpu_to_be32(NULLAGINO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 xfs_ialloc_log_di(tp, fbuf, i,
324 XFS_DI_CORE_BITS | XFS_DI_NEXT_UNLINKED);
325 }
326 xfs_trans_inode_alloc_buf(tp, fbuf);
327 }
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800328 be32_add_cpu(&agi->agi_count, newlen);
329 be32_add_cpu(&agi->agi_freecount, newlen);
David Chinner92821e22007-05-24 15:26:31 +1000330 agno = be32_to_cpu(agi->agi_seqno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 down_read(&args.mp->m_peraglock);
David Chinner92821e22007-05-24 15:26:31 +1000332 args.mp->m_perag[agno].pagi_freecount += newlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 up_read(&args.mp->m_peraglock);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100334 agi->agi_newino = cpu_to_be32(newino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 /*
336 * Insert records describing the new inode chunk into the btree.
337 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100338 cur = xfs_inobt_init_cursor(args.mp, tp, agbp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 for (thisino = newino;
340 thisino < newino + newlen;
341 thisino += XFS_INODES_PER_CHUNK) {
342 if ((error = xfs_inobt_lookup_eq(cur, thisino,
343 XFS_INODES_PER_CHUNK, XFS_INOBT_ALL_FREE, &i))) {
344 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
345 return error;
346 }
347 ASSERT(i == 0);
348 if ((error = xfs_inobt_insert(cur, &i))) {
349 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
350 return error;
351 }
352 ASSERT(i == 1);
353 }
354 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
355 /*
356 * Log allocation group header fields
357 */
358 xfs_ialloc_log_agi(tp, agbp,
359 XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO);
360 /*
361 * Modify/log superblock values for inode count and inode free count.
362 */
363 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen);
364 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen);
365 *alloc = 1;
366 return 0;
367}
368
David Chinner7989cb82007-02-10 18:34:56 +1100369STATIC_INLINE xfs_agnumber_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370xfs_ialloc_next_ag(
371 xfs_mount_t *mp)
372{
373 xfs_agnumber_t agno;
374
375 spin_lock(&mp->m_agirotor_lock);
376 agno = mp->m_agirotor;
377 if (++mp->m_agirotor == mp->m_maxagi)
378 mp->m_agirotor = 0;
379 spin_unlock(&mp->m_agirotor_lock);
380
381 return agno;
382}
383
384/*
385 * Select an allocation group to look for a free inode in, based on the parent
386 * inode and then mode. Return the allocation group buffer.
387 */
388STATIC xfs_buf_t * /* allocation group buffer */
389xfs_ialloc_ag_select(
390 xfs_trans_t *tp, /* transaction pointer */
391 xfs_ino_t parent, /* parent directory inode number */
392 mode_t mode, /* bits set to indicate file type */
393 int okalloc) /* ok to allocate more space */
394{
395 xfs_buf_t *agbp; /* allocation group header buffer */
396 xfs_agnumber_t agcount; /* number of ag's in the filesystem */
397 xfs_agnumber_t agno; /* current ag number */
398 int flags; /* alloc buffer locking flags */
399 xfs_extlen_t ineed; /* blocks needed for inode allocation */
400 xfs_extlen_t longest = 0; /* longest extent available */
401 xfs_mount_t *mp; /* mount point structure */
402 int needspace; /* file mode implies space allocated */
403 xfs_perag_t *pag; /* per allocation group data */
404 xfs_agnumber_t pagno; /* parent (starting) ag number */
405
406 /*
407 * Files of these types need at least one block if length > 0
408 * (and they won't fit in the inode, but that's hard to figure out).
409 */
410 needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode);
411 mp = tp->t_mountp;
412 agcount = mp->m_maxagi;
413 if (S_ISDIR(mode))
414 pagno = xfs_ialloc_next_ag(mp);
415 else {
416 pagno = XFS_INO_TO_AGNO(mp, parent);
417 if (pagno >= agcount)
418 pagno = 0;
419 }
420 ASSERT(pagno < agcount);
421 /*
422 * Loop through allocation groups, looking for one with a little
423 * free space in it. Note we don't look for free inodes, exactly.
424 * Instead, we include whether there is a need to allocate inodes
425 * to mean that blocks must be allocated for them,
426 * if none are currently free.
427 */
428 agno = pagno;
429 flags = XFS_ALLOC_FLAG_TRYLOCK;
430 down_read(&mp->m_peraglock);
431 for (;;) {
432 pag = &mp->m_perag[agno];
433 if (!pag->pagi_init) {
434 if (xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
435 agbp = NULL;
436 goto nextag;
437 }
438 } else
439 agbp = NULL;
440
441 if (!pag->pagi_inodeok) {
442 xfs_ialloc_next_ag(mp);
443 goto unlock_nextag;
444 }
445
446 /*
447 * Is there enough free space for the file plus a block
448 * of inodes (if we need to allocate some)?
449 */
450 ineed = pag->pagi_freecount ? 0 : XFS_IALLOC_BLOCKS(mp);
451 if (ineed && !pag->pagf_init) {
452 if (agbp == NULL &&
453 xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
454 agbp = NULL;
455 goto nextag;
456 }
457 (void)xfs_alloc_pagf_init(mp, tp, agno, flags);
458 }
459 if (!ineed || pag->pagf_init) {
460 if (ineed && !(longest = pag->pagf_longest))
461 longest = pag->pagf_flcount > 0;
462 if (!ineed ||
463 (pag->pagf_freeblks >= needspace + ineed &&
464 longest >= ineed &&
465 okalloc)) {
466 if (agbp == NULL &&
467 xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
468 agbp = NULL;
469 goto nextag;
470 }
471 up_read(&mp->m_peraglock);
472 return agbp;
473 }
474 }
475unlock_nextag:
476 if (agbp)
477 xfs_trans_brelse(tp, agbp);
478nextag:
479 /*
480 * No point in iterating over the rest, if we're shutting
481 * down.
482 */
483 if (XFS_FORCED_SHUTDOWN(mp)) {
484 up_read(&mp->m_peraglock);
Nathan Scott1121b212006-09-28 10:58:40 +1000485 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
487 agno++;
488 if (agno >= agcount)
489 agno = 0;
490 if (agno == pagno) {
491 if (flags == 0) {
492 up_read(&mp->m_peraglock);
Nathan Scott1121b212006-09-28 10:58:40 +1000493 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 }
495 flags = 0;
496 }
497 }
498}
499
500/*
501 * Visible inode allocation functions.
502 */
503
504/*
505 * Allocate an inode on disk.
506 * Mode is used to tell whether the new inode will need space, and whether
507 * it is a directory.
508 *
509 * The arguments IO_agbp and alloc_done are defined to work within
510 * the constraint of one allocation per transaction.
511 * xfs_dialloc() is designed to be called twice if it has to do an
512 * allocation to make more free inodes. On the first call,
513 * IO_agbp should be set to NULL. If an inode is available,
514 * i.e., xfs_dialloc() did not need to do an allocation, an inode
515 * number is returned. In this case, IO_agbp would be set to the
516 * current ag_buf and alloc_done set to false.
517 * If an allocation needed to be done, xfs_dialloc would return
518 * the current ag_buf in IO_agbp and set alloc_done to true.
519 * The caller should then commit the current transaction, allocate a new
520 * transaction, and call xfs_dialloc() again, passing in the previous
521 * value of IO_agbp. IO_agbp should be held across the transactions.
522 * Since the agbp is locked across the two calls, the second call is
523 * guaranteed to have a free inode available.
524 *
525 * Once we successfully pick an inode its number is returned and the
526 * on-disk data structures are updated. The inode itself is not read
527 * in, since doing so would break ordering constraints with xfs_reclaim.
528 */
529int
530xfs_dialloc(
531 xfs_trans_t *tp, /* transaction pointer */
532 xfs_ino_t parent, /* parent inode (directory) */
533 mode_t mode, /* mode bits for new inode */
534 int okalloc, /* ok to allocate more space */
535 xfs_buf_t **IO_agbp, /* in/out ag header's buffer */
536 boolean_t *alloc_done, /* true if we needed to replenish
537 inode freelist */
538 xfs_ino_t *inop) /* inode number allocated */
539{
540 xfs_agnumber_t agcount; /* number of allocation groups */
541 xfs_buf_t *agbp; /* allocation group header's buffer */
542 xfs_agnumber_t agno; /* allocation group number */
543 xfs_agi_t *agi; /* allocation group header structure */
544 xfs_btree_cur_t *cur; /* inode allocation btree cursor */
545 int error; /* error return value */
546 int i; /* result code */
547 int ialloced; /* inode allocation status */
548 int noroom = 0; /* no space for inode blk allocation */
549 xfs_ino_t ino; /* fs-relative inode to be returned */
550 /* REFERENCED */
551 int j; /* result code */
552 xfs_mount_t *mp; /* file system mount structure */
553 int offset; /* index of inode in chunk */
554 xfs_agino_t pagino; /* parent's a.g. relative inode # */
555 xfs_agnumber_t pagno; /* parent's allocation group number */
Christoph Hellwig61a25842006-09-28 10:57:04 +1000556 xfs_inobt_rec_incore_t rec; /* inode allocation record */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 xfs_agnumber_t tagno; /* testing allocation group number */
558 xfs_btree_cur_t *tcur; /* temp cursor */
Christoph Hellwig61a25842006-09-28 10:57:04 +1000559 xfs_inobt_rec_incore_t trec; /* temp inode allocation record */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561
562 if (*IO_agbp == NULL) {
563 /*
564 * We do not have an agbp, so select an initial allocation
565 * group for inode allocation.
566 */
567 agbp = xfs_ialloc_ag_select(tp, parent, mode, okalloc);
568 /*
569 * Couldn't find an allocation group satisfying the
570 * criteria, give up.
571 */
572 if (!agbp) {
573 *inop = NULLFSINO;
574 return 0;
575 }
576 agi = XFS_BUF_TO_AGI(agbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100577 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 } else {
579 /*
580 * Continue where we left off before. In this case, we
581 * know that the allocation group has free inodes.
582 */
583 agbp = *IO_agbp;
584 agi = XFS_BUF_TO_AGI(agbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100585 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
586 ASSERT(be32_to_cpu(agi->agi_freecount) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 }
588 mp = tp->t_mountp;
589 agcount = mp->m_sb.sb_agcount;
Christoph Hellwig16259e72005-11-02 15:11:25 +1100590 agno = be32_to_cpu(agi->agi_seqno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 tagno = agno;
592 pagno = XFS_INO_TO_AGNO(mp, parent);
593 pagino = XFS_INO_TO_AGINO(mp, parent);
594
595 /*
596 * If we have already hit the ceiling of inode blocks then clear
597 * okalloc so we scan all available agi structures for a free
598 * inode.
599 */
600
601 if (mp->m_maxicount &&
602 mp->m_sb.sb_icount + XFS_IALLOC_INODES(mp) > mp->m_maxicount) {
603 noroom = 1;
604 okalloc = 0;
605 }
606
607 /*
608 * Loop until we find an allocation group that either has free inodes
609 * or in which we can allocate some inodes. Iterate through the
610 * allocation groups upward, wrapping at the end.
611 */
612 *alloc_done = B_FALSE;
613 while (!agi->agi_freecount) {
614 /*
615 * Don't do anything if we're not supposed to allocate
616 * any blocks, just go on to the next ag.
617 */
618 if (okalloc) {
619 /*
620 * Try to allocate some new inodes in the allocation
621 * group.
622 */
623 if ((error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced))) {
624 xfs_trans_brelse(tp, agbp);
625 if (error == ENOSPC) {
626 *inop = NULLFSINO;
627 return 0;
628 } else
629 return error;
630 }
631 if (ialloced) {
632 /*
633 * We successfully allocated some inodes, return
634 * the current context to the caller so that it
635 * can commit the current transaction and call
636 * us again where we left off.
637 */
Christoph Hellwig16259e72005-11-02 15:11:25 +1100638 ASSERT(be32_to_cpu(agi->agi_freecount) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 *alloc_done = B_TRUE;
640 *IO_agbp = agbp;
641 *inop = NULLFSINO;
642 return 0;
643 }
644 }
645 /*
646 * If it failed, give up on this ag.
647 */
648 xfs_trans_brelse(tp, agbp);
649 /*
650 * Go on to the next ag: get its ag header.
651 */
652nextag:
653 if (++tagno == agcount)
654 tagno = 0;
655 if (tagno == agno) {
656 *inop = NULLFSINO;
657 return noroom ? ENOSPC : 0;
658 }
659 down_read(&mp->m_peraglock);
660 if (mp->m_perag[tagno].pagi_inodeok == 0) {
661 up_read(&mp->m_peraglock);
662 goto nextag;
663 }
664 error = xfs_ialloc_read_agi(mp, tp, tagno, &agbp);
665 up_read(&mp->m_peraglock);
666 if (error)
667 goto nextag;
668 agi = XFS_BUF_TO_AGI(agbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100669 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
671 /*
672 * Here with an allocation group that has a free inode.
673 * Reset agno since we may have chosen a new ag in the
674 * loop above.
675 */
676 agno = tagno;
677 *IO_agbp = NULL;
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100678 cur = xfs_inobt_init_cursor(mp, tp, agbp, be32_to_cpu(agi->agi_seqno));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 /*
680 * If pagino is 0 (this is the root inode allocation) use newino.
681 * This must work because we've just allocated some.
682 */
683 if (!pagino)
Christoph Hellwig16259e72005-11-02 15:11:25 +1100684 pagino = be32_to_cpu(agi->agi_newino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685#ifdef DEBUG
686 if (cur->bc_nlevels == 1) {
687 int freecount = 0;
688
689 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
690 goto error0;
691 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
692 do {
693 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
694 &rec.ir_freecount, &rec.ir_free, &i)))
695 goto error0;
696 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
697 freecount += rec.ir_freecount;
Christoph Hellwig637aa502008-10-30 16:55:45 +1100698 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 goto error0;
700 } while (i == 1);
701
Christoph Hellwig16259e72005-11-02 15:11:25 +1100702 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 XFS_FORCED_SHUTDOWN(mp));
704 }
705#endif
706 /*
707 * If in the same a.g. as the parent, try to get near the parent.
708 */
709 if (pagno == agno) {
710 if ((error = xfs_inobt_lookup_le(cur, pagino, 0, 0, &i)))
711 goto error0;
712 if (i != 0 &&
713 (error = xfs_inobt_get_rec(cur, &rec.ir_startino,
714 &rec.ir_freecount, &rec.ir_free, &j)) == 0 &&
715 j == 1 &&
716 rec.ir_freecount > 0) {
717 /*
718 * Found a free inode in the same chunk
719 * as parent, done.
720 */
721 }
722 /*
723 * In the same a.g. as parent, but parent's chunk is full.
724 */
725 else {
726 int doneleft; /* done, to the left */
727 int doneright; /* done, to the right */
728
729 if (error)
730 goto error0;
731 ASSERT(i == 1);
732 ASSERT(j == 1);
733 /*
734 * Duplicate the cursor, search left & right
735 * simultaneously.
736 */
737 if ((error = xfs_btree_dup_cursor(cur, &tcur)))
738 goto error0;
739 /*
740 * Search left with tcur, back up 1 record.
741 */
742 if ((error = xfs_inobt_decrement(tcur, 0, &i)))
743 goto error1;
744 doneleft = !i;
745 if (!doneleft) {
746 if ((error = xfs_inobt_get_rec(tcur,
747 &trec.ir_startino,
748 &trec.ir_freecount,
749 &trec.ir_free, &i)))
750 goto error1;
751 XFS_WANT_CORRUPTED_GOTO(i == 1, error1);
752 }
753 /*
754 * Search right with cur, go forward 1 record.
755 */
Christoph Hellwig637aa502008-10-30 16:55:45 +1100756 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 goto error1;
758 doneright = !i;
759 if (!doneright) {
760 if ((error = xfs_inobt_get_rec(cur,
761 &rec.ir_startino,
762 &rec.ir_freecount,
763 &rec.ir_free, &i)))
764 goto error1;
765 XFS_WANT_CORRUPTED_GOTO(i == 1, error1);
766 }
767 /*
768 * Loop until we find the closest inode chunk
769 * with a free one.
770 */
771 while (!doneleft || !doneright) {
772 int useleft; /* using left inode
773 chunk this time */
774
775 /*
776 * Figure out which block is closer,
777 * if both are valid.
778 */
779 if (!doneleft && !doneright)
780 useleft =
781 pagino -
782 (trec.ir_startino +
783 XFS_INODES_PER_CHUNK - 1) <
784 rec.ir_startino - pagino;
785 else
786 useleft = !doneleft;
787 /*
788 * If checking the left, does it have
789 * free inodes?
790 */
791 if (useleft && trec.ir_freecount) {
792 /*
793 * Yes, set it up as the chunk to use.
794 */
795 rec = trec;
796 xfs_btree_del_cursor(cur,
797 XFS_BTREE_NOERROR);
798 cur = tcur;
799 break;
800 }
801 /*
802 * If checking the right, does it have
803 * free inodes?
804 */
805 if (!useleft && rec.ir_freecount) {
806 /*
807 * Yes, it's already set up.
808 */
809 xfs_btree_del_cursor(tcur,
810 XFS_BTREE_NOERROR);
811 break;
812 }
813 /*
814 * If used the left, get another one
815 * further left.
816 */
817 if (useleft) {
818 if ((error = xfs_inobt_decrement(tcur, 0,
819 &i)))
820 goto error1;
821 doneleft = !i;
822 if (!doneleft) {
823 if ((error = xfs_inobt_get_rec(
824 tcur,
825 &trec.ir_startino,
826 &trec.ir_freecount,
827 &trec.ir_free, &i)))
828 goto error1;
829 XFS_WANT_CORRUPTED_GOTO(i == 1,
830 error1);
831 }
832 }
833 /*
834 * If used the right, get another one
835 * further right.
836 */
837 else {
Christoph Hellwig637aa502008-10-30 16:55:45 +1100838 if ((error = xfs_btree_increment(cur, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 &i)))
840 goto error1;
841 doneright = !i;
842 if (!doneright) {
843 if ((error = xfs_inobt_get_rec(
844 cur,
845 &rec.ir_startino,
846 &rec.ir_freecount,
847 &rec.ir_free, &i)))
848 goto error1;
849 XFS_WANT_CORRUPTED_GOTO(i == 1,
850 error1);
851 }
852 }
853 }
854 ASSERT(!doneleft || !doneright);
855 }
856 }
857 /*
858 * In a different a.g. from the parent.
859 * See if the most recently allocated block has any free.
860 */
Christoph Hellwig16259e72005-11-02 15:11:25 +1100861 else if (be32_to_cpu(agi->agi_newino) != NULLAGINO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 if ((error = xfs_inobt_lookup_eq(cur,
Christoph Hellwig16259e72005-11-02 15:11:25 +1100863 be32_to_cpu(agi->agi_newino), 0, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 goto error0;
865 if (i == 1 &&
866 (error = xfs_inobt_get_rec(cur, &rec.ir_startino,
867 &rec.ir_freecount, &rec.ir_free, &j)) == 0 &&
868 j == 1 &&
869 rec.ir_freecount > 0) {
870 /*
871 * The last chunk allocated in the group still has
872 * a free inode.
873 */
874 }
875 /*
876 * None left in the last group, search the whole a.g.
877 */
878 else {
879 if (error)
880 goto error0;
881 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
882 goto error0;
883 ASSERT(i == 1);
884 for (;;) {
885 if ((error = xfs_inobt_get_rec(cur,
886 &rec.ir_startino,
887 &rec.ir_freecount, &rec.ir_free,
888 &i)))
889 goto error0;
890 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
891 if (rec.ir_freecount > 0)
892 break;
Christoph Hellwig637aa502008-10-30 16:55:45 +1100893 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 goto error0;
895 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
896 }
897 }
898 }
899 offset = XFS_IALLOC_FIND_FREE(&rec.ir_free);
900 ASSERT(offset >= 0);
901 ASSERT(offset < XFS_INODES_PER_CHUNK);
902 ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
903 XFS_INODES_PER_CHUNK) == 0);
904 ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
905 XFS_INOBT_CLR_FREE(&rec, offset);
906 rec.ir_freecount--;
907 if ((error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount,
908 rec.ir_free)))
909 goto error0;
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800910 be32_add_cpu(&agi->agi_freecount, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
912 down_read(&mp->m_peraglock);
913 mp->m_perag[tagno].pagi_freecount--;
914 up_read(&mp->m_peraglock);
915#ifdef DEBUG
916 if (cur->bc_nlevels == 1) {
917 int freecount = 0;
918
919 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
920 goto error0;
921 do {
922 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
923 &rec.ir_freecount, &rec.ir_free, &i)))
924 goto error0;
925 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
926 freecount += rec.ir_freecount;
Christoph Hellwig637aa502008-10-30 16:55:45 +1100927 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 goto error0;
929 } while (i == 1);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100930 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 XFS_FORCED_SHUTDOWN(mp));
932 }
933#endif
934 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
935 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
936 *inop = ino;
937 return 0;
938error1:
939 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
940error0:
941 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
942 return error;
943}
944
945/*
946 * Free disk inode. Carefully avoids touching the incore inode, all
947 * manipulations incore are the caller's responsibility.
948 * The on-disk inode is not changed by this operation, only the
949 * btree (free inode mask) is changed.
950 */
951int
952xfs_difree(
953 xfs_trans_t *tp, /* transaction pointer */
954 xfs_ino_t inode, /* inode to be freed */
955 xfs_bmap_free_t *flist, /* extents to free */
956 int *delete, /* set if inode cluster was deleted */
957 xfs_ino_t *first_ino) /* first inode in deleted cluster */
958{
959 /* REFERENCED */
960 xfs_agblock_t agbno; /* block number containing inode */
961 xfs_buf_t *agbp; /* buffer containing allocation group header */
962 xfs_agino_t agino; /* inode number relative to allocation group */
963 xfs_agnumber_t agno; /* allocation group number */
964 xfs_agi_t *agi; /* allocation group header */
965 xfs_btree_cur_t *cur; /* inode btree cursor */
966 int error; /* error return value */
967 int i; /* result code */
968 int ilen; /* inodes in an inode cluster */
969 xfs_mount_t *mp; /* mount structure for filesystem */
970 int off; /* offset of inode in inode chunk */
Christoph Hellwig61a25842006-09-28 10:57:04 +1000971 xfs_inobt_rec_incore_t rec; /* btree record */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
973 mp = tp->t_mountp;
974
975 /*
976 * Break up inode number into its components.
977 */
978 agno = XFS_INO_TO_AGNO(mp, inode);
979 if (agno >= mp->m_sb.sb_agcount) {
980 cmn_err(CE_WARN,
981 "xfs_difree: agno >= mp->m_sb.sb_agcount (%d >= %d) on %s. Returning EINVAL.",
982 agno, mp->m_sb.sb_agcount, mp->m_fsname);
983 ASSERT(0);
984 return XFS_ERROR(EINVAL);
985 }
986 agino = XFS_INO_TO_AGINO(mp, inode);
987 if (inode != XFS_AGINO_TO_INO(mp, agno, agino)) {
988 cmn_err(CE_WARN,
Christoph Hellwigda1650a2005-11-02 10:21:35 +1100989 "xfs_difree: inode != XFS_AGINO_TO_INO() "
990 "(%llu != %llu) on %s. Returning EINVAL.",
991 (unsigned long long)inode,
992 (unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino),
993 mp->m_fsname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 ASSERT(0);
995 return XFS_ERROR(EINVAL);
996 }
997 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
998 if (agbno >= mp->m_sb.sb_agblocks) {
999 cmn_err(CE_WARN,
1000 "xfs_difree: agbno >= mp->m_sb.sb_agblocks (%d >= %d) on %s. Returning EINVAL.",
1001 agbno, mp->m_sb.sb_agblocks, mp->m_fsname);
1002 ASSERT(0);
1003 return XFS_ERROR(EINVAL);
1004 }
1005 /*
1006 * Get the allocation group header.
1007 */
1008 down_read(&mp->m_peraglock);
1009 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1010 up_read(&mp->m_peraglock);
1011 if (error) {
1012 cmn_err(CE_WARN,
1013 "xfs_difree: xfs_ialloc_read_agi() returned an error %d on %s. Returning error.",
1014 error, mp->m_fsname);
1015 return error;
1016 }
1017 agi = XFS_BUF_TO_AGI(agbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001018 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
1019 ASSERT(agbno < be32_to_cpu(agi->agi_length));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 /*
1021 * Initialize the cursor.
1022 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001023 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024#ifdef DEBUG
1025 if (cur->bc_nlevels == 1) {
1026 int freecount = 0;
1027
1028 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
1029 goto error0;
1030 do {
1031 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
1032 &rec.ir_freecount, &rec.ir_free, &i)))
1033 goto error0;
1034 if (i) {
1035 freecount += rec.ir_freecount;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001036 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 goto error0;
1038 }
1039 } while (i == 1);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001040 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 XFS_FORCED_SHUTDOWN(mp));
1042 }
1043#endif
1044 /*
1045 * Look for the entry describing this inode.
1046 */
1047 if ((error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i))) {
1048 cmn_err(CE_WARN,
1049 "xfs_difree: xfs_inobt_lookup_le returned() an error %d on %s. Returning error.",
1050 error, mp->m_fsname);
1051 goto error0;
1052 }
1053 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1054 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino, &rec.ir_freecount,
1055 &rec.ir_free, &i))) {
1056 cmn_err(CE_WARN,
1057 "xfs_difree: xfs_inobt_get_rec() returned an error %d on %s. Returning error.",
1058 error, mp->m_fsname);
1059 goto error0;
1060 }
1061 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1062 /*
1063 * Get the offset in the inode chunk.
1064 */
1065 off = agino - rec.ir_startino;
1066 ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK);
1067 ASSERT(!XFS_INOBT_IS_FREE(&rec, off));
1068 /*
1069 * Mark the inode free & increment the count.
1070 */
1071 XFS_INOBT_SET_FREE(&rec, off);
1072 rec.ir_freecount++;
1073
1074 /*
Nathan Scottc41564b2006-03-29 08:55:14 +10001075 * When an inode cluster is free, it becomes eligible for removal
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 */
Josef Jeff Sipek1bd960e2008-02-29 13:58:40 +11001077 if (!(mp->m_flags & XFS_MOUNT_IKEEP) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 (rec.ir_freecount == XFS_IALLOC_INODES(mp))) {
1079
1080 *delete = 1;
1081 *first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino);
1082
1083 /*
1084 * Remove the inode cluster from the AGI B+Tree, adjust the
1085 * AGI and Superblock inode counts, and mark the disk space
1086 * to be freed when the transaction is committed.
1087 */
1088 ilen = XFS_IALLOC_INODES(mp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001089 be32_add_cpu(&agi->agi_count, -ilen);
1090 be32_add_cpu(&agi->agi_freecount, -(ilen - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT);
1092 down_read(&mp->m_peraglock);
1093 mp->m_perag[agno].pagi_freecount -= ilen - 1;
1094 up_read(&mp->m_peraglock);
1095 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen);
1096 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1));
1097
1098 if ((error = xfs_inobt_delete(cur, &i))) {
1099 cmn_err(CE_WARN, "xfs_difree: xfs_inobt_delete returned an error %d on %s.\n",
1100 error, mp->m_fsname);
1101 goto error0;
1102 }
1103
1104 xfs_bmap_add_free(XFS_AGB_TO_FSB(mp,
1105 agno, XFS_INO_TO_AGBNO(mp,rec.ir_startino)),
1106 XFS_IALLOC_BLOCKS(mp), flist, mp);
1107 } else {
1108 *delete = 0;
1109
1110 if ((error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount, rec.ir_free))) {
1111 cmn_err(CE_WARN,
1112 "xfs_difree: xfs_inobt_update() returned an error %d on %s. Returning error.",
1113 error, mp->m_fsname);
1114 goto error0;
1115 }
1116 /*
1117 * Change the inode free counts and log the ag/sb changes.
1118 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001119 be32_add_cpu(&agi->agi_freecount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
1121 down_read(&mp->m_peraglock);
1122 mp->m_perag[agno].pagi_freecount++;
1123 up_read(&mp->m_peraglock);
1124 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1);
1125 }
1126
1127#ifdef DEBUG
1128 if (cur->bc_nlevels == 1) {
1129 int freecount = 0;
1130
1131 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
1132 goto error0;
1133 do {
1134 if ((error = xfs_inobt_get_rec(cur,
1135 &rec.ir_startino,
1136 &rec.ir_freecount,
1137 &rec.ir_free, &i)))
1138 goto error0;
1139 if (i) {
1140 freecount += rec.ir_freecount;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001141 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 goto error0;
1143 }
1144 } while (i == 1);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001145 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 XFS_FORCED_SHUTDOWN(mp));
1147 }
1148#endif
1149 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1150 return 0;
1151
1152error0:
1153 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1154 return error;
1155}
1156
1157/*
1158 * Return the location of the inode in bno/off, for mapping it into a buffer.
1159 */
1160/*ARGSUSED*/
1161int
1162xfs_dilocate(
1163 xfs_mount_t *mp, /* file system mount structure */
1164 xfs_trans_t *tp, /* transaction pointer */
1165 xfs_ino_t ino, /* inode to locate */
1166 xfs_fsblock_t *bno, /* output: block containing inode */
1167 int *len, /* output: num blocks in inode cluster */
1168 int *off, /* output: index in block of inode */
1169 uint flags) /* flags concerning inode lookup */
1170{
1171 xfs_agblock_t agbno; /* block number of inode in the alloc group */
1172 xfs_buf_t *agbp; /* agi buffer */
1173 xfs_agino_t agino; /* inode number within alloc group */
1174 xfs_agnumber_t agno; /* allocation group number */
1175 int blks_per_cluster; /* num blocks per inode cluster */
1176 xfs_agblock_t chunk_agbno; /* first block in inode chunk */
1177 xfs_agino_t chunk_agino; /* first agino in inode chunk */
1178 __int32_t chunk_cnt; /* count of free inodes in chunk */
1179 xfs_inofree_t chunk_free; /* mask of free inodes in chunk */
1180 xfs_agblock_t cluster_agbno; /* first block in inode cluster */
1181 xfs_btree_cur_t *cur; /* inode btree cursor */
1182 int error; /* error code */
1183 int i; /* temp state */
1184 int offset; /* index of inode in its buffer */
1185 int offset_agbno; /* blks from chunk start to inode */
1186
1187 ASSERT(ino != NULLFSINO);
1188 /*
1189 * Split up the inode number into its parts.
1190 */
1191 agno = XFS_INO_TO_AGNO(mp, ino);
1192 agino = XFS_INO_TO_AGINO(mp, ino);
1193 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1194 if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks ||
1195 ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1196#ifdef DEBUG
Nathan Scott4d1a2ed2006-06-09 17:12:28 +10001197 /* no diagnostics for bulkstat, ino comes from userspace */
1198 if (flags & XFS_IMAP_BULKSTAT)
1199 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 if (agno >= mp->m_sb.sb_agcount) {
1201 xfs_fs_cmn_err(CE_ALERT, mp,
1202 "xfs_dilocate: agno (%d) >= "
1203 "mp->m_sb.sb_agcount (%d)",
1204 agno, mp->m_sb.sb_agcount);
1205 }
1206 if (agbno >= mp->m_sb.sb_agblocks) {
1207 xfs_fs_cmn_err(CE_ALERT, mp,
1208 "xfs_dilocate: agbno (0x%llx) >= "
1209 "mp->m_sb.sb_agblocks (0x%lx)",
1210 (unsigned long long) agbno,
1211 (unsigned long) mp->m_sb.sb_agblocks);
1212 }
1213 if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1214 xfs_fs_cmn_err(CE_ALERT, mp,
1215 "xfs_dilocate: ino (0x%llx) != "
1216 "XFS_AGINO_TO_INO(mp, agno, agino) "
1217 "(0x%llx)",
1218 ino, XFS_AGINO_TO_INO(mp, agno, agino));
1219 }
Nathan Scott745b1f472006-09-28 11:02:23 +10001220 xfs_stack_trace();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221#endif /* DEBUG */
1222 return XFS_ERROR(EINVAL);
1223 }
1224 if ((mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) ||
1225 !(flags & XFS_IMAP_LOOKUP)) {
1226 offset = XFS_INO_TO_OFFSET(mp, ino);
1227 ASSERT(offset < mp->m_sb.sb_inopblock);
1228 *bno = XFS_AGB_TO_FSB(mp, agno, agbno);
1229 *off = offset;
1230 *len = 1;
1231 return 0;
1232 }
1233 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_blocklog;
1234 if (*bno != NULLFSBLOCK) {
1235 offset = XFS_INO_TO_OFFSET(mp, ino);
1236 ASSERT(offset < mp->m_sb.sb_inopblock);
1237 cluster_agbno = XFS_FSB_TO_AGBNO(mp, *bno);
1238 *off = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
1239 offset;
1240 *len = blks_per_cluster;
1241 return 0;
1242 }
1243 if (mp->m_inoalign_mask) {
1244 offset_agbno = agbno & mp->m_inoalign_mask;
1245 chunk_agbno = agbno - offset_agbno;
1246 } else {
1247 down_read(&mp->m_peraglock);
1248 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1249 up_read(&mp->m_peraglock);
1250 if (error) {
1251#ifdef DEBUG
1252 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1253 "xfs_ialloc_read_agi() returned "
1254 "error %d, agno %d",
1255 error, agno);
1256#endif /* DEBUG */
1257 return error;
1258 }
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001259 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 if ((error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i))) {
1261#ifdef DEBUG
1262 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1263 "xfs_inobt_lookup_le() failed");
1264#endif /* DEBUG */
1265 goto error0;
1266 }
1267 if ((error = xfs_inobt_get_rec(cur, &chunk_agino, &chunk_cnt,
1268 &chunk_free, &i))) {
1269#ifdef DEBUG
1270 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1271 "xfs_inobt_get_rec() failed");
1272#endif /* DEBUG */
1273 goto error0;
1274 }
1275 if (i == 0) {
1276#ifdef DEBUG
1277 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1278 "xfs_inobt_get_rec() failed");
1279#endif /* DEBUG */
1280 error = XFS_ERROR(EINVAL);
1281 }
1282 xfs_trans_brelse(tp, agbp);
1283 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1284 if (error)
1285 return error;
1286 chunk_agbno = XFS_AGINO_TO_AGBNO(mp, chunk_agino);
1287 offset_agbno = agbno - chunk_agbno;
1288 }
1289 ASSERT(agbno >= chunk_agbno);
1290 cluster_agbno = chunk_agbno +
1291 ((offset_agbno / blks_per_cluster) * blks_per_cluster);
1292 offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
1293 XFS_INO_TO_OFFSET(mp, ino);
1294 *bno = XFS_AGB_TO_FSB(mp, agno, cluster_agbno);
1295 *off = offset;
1296 *len = blks_per_cluster;
1297 return 0;
1298error0:
1299 xfs_trans_brelse(tp, agbp);
1300 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1301 return error;
1302}
1303
1304/*
1305 * Compute and fill in value of m_in_maxlevels.
1306 */
1307void
1308xfs_ialloc_compute_maxlevels(
1309 xfs_mount_t *mp) /* file system mount structure */
1310{
1311 int level;
1312 uint maxblocks;
1313 uint maxleafents;
1314 int minleafrecs;
1315 int minnoderecs;
1316
1317 maxleafents = (1LL << XFS_INO_AGINO_BITS(mp)) >>
1318 XFS_INODES_PER_CHUNK_LOG;
1319 minleafrecs = mp->m_alloc_mnr[0];
1320 minnoderecs = mp->m_alloc_mnr[1];
1321 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
1322 for (level = 1; maxblocks > 1; level++)
1323 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
1324 mp->m_in_maxlevels = level;
1325}
1326
1327/*
1328 * Log specified fields for the ag hdr (inode section)
1329 */
1330void
1331xfs_ialloc_log_agi(
1332 xfs_trans_t *tp, /* transaction pointer */
1333 xfs_buf_t *bp, /* allocation group header buffer */
1334 int fields) /* bitmask of fields to log */
1335{
1336 int first; /* first byte number */
1337 int last; /* last byte number */
1338 static const short offsets[] = { /* field starting offsets */
1339 /* keep in sync with bit definitions */
1340 offsetof(xfs_agi_t, agi_magicnum),
1341 offsetof(xfs_agi_t, agi_versionnum),
1342 offsetof(xfs_agi_t, agi_seqno),
1343 offsetof(xfs_agi_t, agi_length),
1344 offsetof(xfs_agi_t, agi_count),
1345 offsetof(xfs_agi_t, agi_root),
1346 offsetof(xfs_agi_t, agi_level),
1347 offsetof(xfs_agi_t, agi_freecount),
1348 offsetof(xfs_agi_t, agi_newino),
1349 offsetof(xfs_agi_t, agi_dirino),
1350 offsetof(xfs_agi_t, agi_unlinked),
1351 sizeof(xfs_agi_t)
1352 };
1353#ifdef DEBUG
1354 xfs_agi_t *agi; /* allocation group header */
1355
1356 agi = XFS_BUF_TO_AGI(bp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001357 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358#endif
1359 /*
1360 * Compute byte offsets for the first and last fields.
1361 */
1362 xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS, &first, &last);
1363 /*
1364 * Log the allocation group inode header buffer.
1365 */
1366 xfs_trans_log_buf(tp, bp, first, last);
1367}
1368
1369/*
1370 * Read in the allocation group header (inode allocation section)
1371 */
1372int
1373xfs_ialloc_read_agi(
1374 xfs_mount_t *mp, /* file system mount structure */
1375 xfs_trans_t *tp, /* transaction pointer */
1376 xfs_agnumber_t agno, /* allocation group number */
1377 xfs_buf_t **bpp) /* allocation group hdr buf */
1378{
1379 xfs_agi_t *agi; /* allocation group header */
1380 int agi_ok; /* agi is consistent */
1381 xfs_buf_t *bp; /* allocation group hdr buf */
1382 xfs_perag_t *pag; /* per allocation group data */
1383 int error;
1384
1385 ASSERT(agno != NULLAGNUMBER);
1386 error = xfs_trans_read_buf(
1387 mp, tp, mp->m_ddev_targp,
1388 XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
1389 XFS_FSS_TO_BB(mp, 1), 0, &bp);
1390 if (error)
1391 return error;
1392 ASSERT(bp && !XFS_BUF_GETERROR(bp));
1393
1394 /*
1395 * Validate the magic number of the agi block.
1396 */
1397 agi = XFS_BUF_TO_AGI(bp);
1398 agi_ok =
Christoph Hellwig16259e72005-11-02 15:11:25 +11001399 be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC &&
1400 XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 if (unlikely(XFS_TEST_ERROR(!agi_ok, mp, XFS_ERRTAG_IALLOC_READ_AGI,
1402 XFS_RANDOM_IALLOC_READ_AGI))) {
1403 XFS_CORRUPTION_ERROR("xfs_ialloc_read_agi", XFS_ERRLEVEL_LOW,
1404 mp, agi);
1405 xfs_trans_brelse(tp, bp);
1406 return XFS_ERROR(EFSCORRUPTED);
1407 }
1408 pag = &mp->m_perag[agno];
1409 if (!pag->pagi_init) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11001410 pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
David Chinner92821e22007-05-24 15:26:31 +10001411 pag->pagi_count = be32_to_cpu(agi->agi_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 pag->pagi_init = 1;
1413 } else {
1414 /*
1415 * It's possible for these to be out of sync if
1416 * we are in the middle of a forced shutdown.
1417 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11001418 ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) ||
1419 XFS_FORCED_SHUTDOWN(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 }
1421
1422#ifdef DEBUG
1423 {
1424 int i;
1425
1426 for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++)
1427 ASSERT(agi->agi_unlinked[i]);
1428 }
1429#endif
1430
1431 XFS_BUF_SET_VTYPE_REF(bp, B_FS_AGI, XFS_AGI_REF);
1432 *bpp = bp;
1433 return 0;
1434}
David Chinner92821e22007-05-24 15:26:31 +10001435
1436/*
1437 * Read in the agi to initialise the per-ag data in the mount structure
1438 */
1439int
1440xfs_ialloc_pagi_init(
1441 xfs_mount_t *mp, /* file system mount structure */
1442 xfs_trans_t *tp, /* transaction pointer */
1443 xfs_agnumber_t agno) /* allocation group number */
1444{
1445 xfs_buf_t *bp = NULL;
1446 int error;
1447
1448 error = xfs_ialloc_read_agi(mp, tp, agno, &bp);
1449 if (error)
1450 return error;
1451 if (bp)
1452 xfs_trans_brelse(tp, bp);
1453 return 0;
1454}