blob: 1409c2d61c1119880597a25b84b33c6e9a1b57f0 [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 */
110
111/*
112 * Allocate new inodes in the allocation group specified by agbp.
113 * Return 0 for success, else error code.
114 */
115STATIC int /* error code or 0 */
116xfs_ialloc_ag_alloc(
117 xfs_trans_t *tp, /* transaction pointer */
118 xfs_buf_t *agbp, /* alloc group buffer */
119 int *alloc)
120{
121 xfs_agi_t *agi; /* allocation group header */
122 xfs_alloc_arg_t args; /* allocation argument structure */
123 int blks_per_cluster; /* fs blocks per inode cluster */
124 xfs_btree_cur_t *cur; /* inode btree cursor */
125 xfs_daddr_t d; /* disk addr of buffer */
David Chinner92821e22007-05-24 15:26:31 +1000126 xfs_agnumber_t agno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 int error;
128 xfs_buf_t *fbuf; /* new free inodes' buffer */
129 xfs_dinode_t *free; /* new free inode structure */
130 int i; /* inode counter */
131 int j; /* block counter */
132 int nbufs; /* num bufs of new inodes */
133 xfs_agino_t newino; /* new first inode's number */
134 xfs_agino_t newlen; /* new number of inodes */
135 int ninodes; /* num inodes per buf */
136 xfs_agino_t thisino; /* current inode number, for loop */
137 int version; /* inode version number to use */
Glen Overby3ccb8b52006-03-29 09:52:28 +1000138 int isaligned = 0; /* inode allocation at stripe unit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 /* boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 args.tp = tp;
142 args.mp = tp->t_mountp;
143
144 /*
145 * Locking will ensure that we don't have two callers in here
146 * at one time.
147 */
148 newlen = XFS_IALLOC_INODES(args.mp);
149 if (args.mp->m_maxicount &&
150 args.mp->m_sb.sb_icount + newlen > args.mp->m_maxicount)
151 return XFS_ERROR(ENOSPC);
152 args.minlen = args.maxlen = XFS_IALLOC_BLOCKS(args.mp);
153 /*
Glen Overby3ccb8b52006-03-29 09:52:28 +1000154 * First try to allocate inodes contiguous with the last-allocated
155 * chunk of inodes. If the filesystem is striped, this will fill
156 * an entire stripe unit with inodes.
157 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 agi = XFS_BUF_TO_AGI(agbp);
Glen Overby3ccb8b52006-03-29 09:52:28 +1000159 newino = be32_to_cpu(agi->agi_newino);
Nathan Scott019ff2d2006-04-11 15:45:05 +1000160 args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) +
161 XFS_IALLOC_BLOCKS(args.mp);
162 if (likely(newino != NULLAGINO &&
163 (args.agbno < be32_to_cpu(agi->agi_length)))) {
Glen Overby3ccb8b52006-03-29 09:52:28 +1000164 args.fsbno = XFS_AGB_TO_FSB(args.mp,
165 be32_to_cpu(agi->agi_seqno), args.agbno);
166 args.type = XFS_ALLOCTYPE_THIS_BNO;
167 args.mod = args.total = args.wasdel = args.isfl =
168 args.userdata = args.minalignslop = 0;
169 args.prod = 1;
170 args.alignment = 1;
171 /*
172 * Allow space for the inode btree to split.
173 */
174 args.minleft = XFS_IN_MAXLEVELS(args.mp) - 1;
175 if ((error = xfs_alloc_vextent(&args)))
176 return error;
177 } else
178 args.fsbno = NULLFSBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Glen Overby3ccb8b52006-03-29 09:52:28 +1000180 if (unlikely(args.fsbno == NULLFSBLOCK)) {
181 /*
182 * Set the alignment for the allocation.
183 * If stripe alignment is turned on then align at stripe unit
184 * boundary.
Nathan Scott019ff2d2006-04-11 15:45:05 +1000185 * If the cluster size is smaller than a filesystem block
186 * then we're doing I/O for inodes in filesystem block size
Glen Overby3ccb8b52006-03-29 09:52:28 +1000187 * pieces, so don't need alignment anyway.
188 */
189 isaligned = 0;
190 if (args.mp->m_sinoalign) {
191 ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN));
192 args.alignment = args.mp->m_dalign;
193 isaligned = 1;
194 } else if (XFS_SB_VERSION_HASALIGN(&args.mp->m_sb) &&
Nathan Scott019ff2d2006-04-11 15:45:05 +1000195 args.mp->m_sb.sb_inoalignmt >=
Glen Overby3ccb8b52006-03-29 09:52:28 +1000196 XFS_B_TO_FSBT(args.mp,
197 XFS_INODE_CLUSTER_SIZE(args.mp)))
198 args.alignment = args.mp->m_sb.sb_inoalignmt;
199 else
200 args.alignment = 1;
201 /*
202 * Need to figure out where to allocate the inode blocks.
203 * Ideally they should be spaced out through the a.g.
204 * For now, just allocate blocks up front.
205 */
206 args.agbno = be32_to_cpu(agi->agi_root);
207 args.fsbno = XFS_AGB_TO_FSB(args.mp,
208 be32_to_cpu(agi->agi_seqno), args.agbno);
209 /*
210 * Allocate a fixed-size extent of inodes.
211 */
212 args.type = XFS_ALLOCTYPE_NEAR_BNO;
213 args.mod = args.total = args.wasdel = args.isfl =
214 args.userdata = args.minalignslop = 0;
215 args.prod = 1;
216 /*
217 * Allow space for the inode btree to split.
218 */
219 args.minleft = XFS_IN_MAXLEVELS(args.mp) - 1;
220 if ((error = xfs_alloc_vextent(&args)))
221 return error;
222 }
Nathan Scott019ff2d2006-04-11 15:45:05 +1000223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 /*
225 * If stripe alignment is turned on, then try again with cluster
226 * alignment.
227 */
228 if (isaligned && args.fsbno == NULLFSBLOCK) {
229 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Christoph Hellwig16259e72005-11-02 15:11:25 +1100230 args.agbno = be32_to_cpu(agi->agi_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 args.fsbno = XFS_AGB_TO_FSB(args.mp,
Christoph Hellwig16259e72005-11-02 15:11:25 +1100232 be32_to_cpu(agi->agi_seqno), args.agbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (XFS_SB_VERSION_HASALIGN(&args.mp->m_sb) &&
234 args.mp->m_sb.sb_inoalignmt >=
235 XFS_B_TO_FSBT(args.mp, XFS_INODE_CLUSTER_SIZE(args.mp)))
236 args.alignment = args.mp->m_sb.sb_inoalignmt;
237 else
238 args.alignment = 1;
239 if ((error = xfs_alloc_vextent(&args)))
240 return error;
241 }
242
243 if (args.fsbno == NULLFSBLOCK) {
244 *alloc = 0;
245 return 0;
246 }
247 ASSERT(args.len == args.minlen);
248 /*
249 * Convert the results.
250 */
251 newino = XFS_OFFBNO_TO_AGINO(args.mp, args.agbno, 0);
252 /*
253 * Loop over the new block(s), filling in the inodes.
254 * For small block sizes, manipulate the inodes in buffers
255 * which are multiples of the blocks size.
256 */
257 if (args.mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(args.mp)) {
258 blks_per_cluster = 1;
259 nbufs = (int)args.len;
260 ninodes = args.mp->m_sb.sb_inopblock;
261 } else {
262 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(args.mp) /
263 args.mp->m_sb.sb_blocksize;
264 nbufs = (int)args.len / blks_per_cluster;
265 ninodes = blks_per_cluster * args.mp->m_sb.sb_inopblock;
266 }
267 /*
268 * Figure out what version number to use in the inodes we create.
269 * If the superblock version has caught up to the one that supports
270 * the new inode format, then use the new inode version. Otherwise
271 * use the old version so that old kernels will continue to be
272 * able to use the file system.
273 */
274 if (XFS_SB_VERSION_HASNLINK(&args.mp->m_sb))
275 version = XFS_DINODE_VERSION_2;
276 else
277 version = XFS_DINODE_VERSION_1;
278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 for (j = 0; j < nbufs; j++) {
280 /*
281 * Get the block.
282 */
Christoph Hellwig16259e72005-11-02 15:11:25 +1100283 d = XFS_AGB_TO_DADDR(args.mp, be32_to_cpu(agi->agi_seqno),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 args.agbno + (j * blks_per_cluster));
285 fbuf = xfs_trans_get_buf(tp, args.mp->m_ddev_targp, d,
286 args.mp->m_bsize * blks_per_cluster,
287 XFS_BUF_LOCK);
288 ASSERT(fbuf);
289 ASSERT(!XFS_BUF_GETERROR(fbuf));
290 /*
Nathan Scottf30a1212006-03-14 14:07:36 +1100291 * Set initial values for the inodes in this buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 */
Nathan Scottf30a1212006-03-14 14:07:36 +1100293 xfs_biozero(fbuf, 0, ninodes << args.mp->m_sb.sb_inodelog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 for (i = 0; i < ninodes; i++) {
295 free = XFS_MAKE_IPTR(args.mp, fbuf, i);
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000296 free->di_core.di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
297 free->di_core.di_version = version;
298 free->di_next_unlinked = cpu_to_be32(NULLAGINO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 xfs_ialloc_log_di(tp, fbuf, i,
300 XFS_DI_CORE_BITS | XFS_DI_NEXT_UNLINKED);
301 }
302 xfs_trans_inode_alloc_buf(tp, fbuf);
303 }
Christoph Hellwig16259e72005-11-02 15:11:25 +1100304 be32_add(&agi->agi_count, newlen);
305 be32_add(&agi->agi_freecount, newlen);
David Chinner92821e22007-05-24 15:26:31 +1000306 agno = be32_to_cpu(agi->agi_seqno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 down_read(&args.mp->m_peraglock);
David Chinner92821e22007-05-24 15:26:31 +1000308 args.mp->m_perag[agno].pagi_freecount += newlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 up_read(&args.mp->m_peraglock);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100310 agi->agi_newino = cpu_to_be32(newino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 /*
312 * Insert records describing the new inode chunk into the btree.
313 */
David Chinner92821e22007-05-24 15:26:31 +1000314 cur = xfs_btree_init_cursor(args.mp, tp, agbp, agno,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 XFS_BTNUM_INO, (xfs_inode_t *)0, 0);
316 for (thisino = newino;
317 thisino < newino + newlen;
318 thisino += XFS_INODES_PER_CHUNK) {
319 if ((error = xfs_inobt_lookup_eq(cur, thisino,
320 XFS_INODES_PER_CHUNK, XFS_INOBT_ALL_FREE, &i))) {
321 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
322 return error;
323 }
324 ASSERT(i == 0);
325 if ((error = xfs_inobt_insert(cur, &i))) {
326 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
327 return error;
328 }
329 ASSERT(i == 1);
330 }
331 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
332 /*
333 * Log allocation group header fields
334 */
335 xfs_ialloc_log_agi(tp, agbp,
336 XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO);
337 /*
338 * Modify/log superblock values for inode count and inode free count.
339 */
340 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen);
341 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen);
342 *alloc = 1;
343 return 0;
344}
345
David Chinner7989cb82007-02-10 18:34:56 +1100346STATIC_INLINE xfs_agnumber_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347xfs_ialloc_next_ag(
348 xfs_mount_t *mp)
349{
350 xfs_agnumber_t agno;
351
352 spin_lock(&mp->m_agirotor_lock);
353 agno = mp->m_agirotor;
354 if (++mp->m_agirotor == mp->m_maxagi)
355 mp->m_agirotor = 0;
356 spin_unlock(&mp->m_agirotor_lock);
357
358 return agno;
359}
360
361/*
362 * Select an allocation group to look for a free inode in, based on the parent
363 * inode and then mode. Return the allocation group buffer.
364 */
365STATIC xfs_buf_t * /* allocation group buffer */
366xfs_ialloc_ag_select(
367 xfs_trans_t *tp, /* transaction pointer */
368 xfs_ino_t parent, /* parent directory inode number */
369 mode_t mode, /* bits set to indicate file type */
370 int okalloc) /* ok to allocate more space */
371{
372 xfs_buf_t *agbp; /* allocation group header buffer */
373 xfs_agnumber_t agcount; /* number of ag's in the filesystem */
374 xfs_agnumber_t agno; /* current ag number */
375 int flags; /* alloc buffer locking flags */
376 xfs_extlen_t ineed; /* blocks needed for inode allocation */
377 xfs_extlen_t longest = 0; /* longest extent available */
378 xfs_mount_t *mp; /* mount point structure */
379 int needspace; /* file mode implies space allocated */
380 xfs_perag_t *pag; /* per allocation group data */
381 xfs_agnumber_t pagno; /* parent (starting) ag number */
382
383 /*
384 * Files of these types need at least one block if length > 0
385 * (and they won't fit in the inode, but that's hard to figure out).
386 */
387 needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode);
388 mp = tp->t_mountp;
389 agcount = mp->m_maxagi;
390 if (S_ISDIR(mode))
391 pagno = xfs_ialloc_next_ag(mp);
392 else {
393 pagno = XFS_INO_TO_AGNO(mp, parent);
394 if (pagno >= agcount)
395 pagno = 0;
396 }
397 ASSERT(pagno < agcount);
398 /*
399 * Loop through allocation groups, looking for one with a little
400 * free space in it. Note we don't look for free inodes, exactly.
401 * Instead, we include whether there is a need to allocate inodes
402 * to mean that blocks must be allocated for them,
403 * if none are currently free.
404 */
405 agno = pagno;
406 flags = XFS_ALLOC_FLAG_TRYLOCK;
407 down_read(&mp->m_peraglock);
408 for (;;) {
409 pag = &mp->m_perag[agno];
410 if (!pag->pagi_init) {
411 if (xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
412 agbp = NULL;
413 goto nextag;
414 }
415 } else
416 agbp = NULL;
417
418 if (!pag->pagi_inodeok) {
419 xfs_ialloc_next_ag(mp);
420 goto unlock_nextag;
421 }
422
423 /*
424 * Is there enough free space for the file plus a block
425 * of inodes (if we need to allocate some)?
426 */
427 ineed = pag->pagi_freecount ? 0 : XFS_IALLOC_BLOCKS(mp);
428 if (ineed && !pag->pagf_init) {
429 if (agbp == NULL &&
430 xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
431 agbp = NULL;
432 goto nextag;
433 }
434 (void)xfs_alloc_pagf_init(mp, tp, agno, flags);
435 }
436 if (!ineed || pag->pagf_init) {
437 if (ineed && !(longest = pag->pagf_longest))
438 longest = pag->pagf_flcount > 0;
439 if (!ineed ||
440 (pag->pagf_freeblks >= needspace + ineed &&
441 longest >= ineed &&
442 okalloc)) {
443 if (agbp == NULL &&
444 xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
445 agbp = NULL;
446 goto nextag;
447 }
448 up_read(&mp->m_peraglock);
449 return agbp;
450 }
451 }
452unlock_nextag:
453 if (agbp)
454 xfs_trans_brelse(tp, agbp);
455nextag:
456 /*
457 * No point in iterating over the rest, if we're shutting
458 * down.
459 */
460 if (XFS_FORCED_SHUTDOWN(mp)) {
461 up_read(&mp->m_peraglock);
Nathan Scott1121b212006-09-28 10:58:40 +1000462 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 }
464 agno++;
465 if (agno >= agcount)
466 agno = 0;
467 if (agno == pagno) {
468 if (flags == 0) {
469 up_read(&mp->m_peraglock);
Nathan Scott1121b212006-09-28 10:58:40 +1000470 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
472 flags = 0;
473 }
474 }
475}
476
477/*
478 * Visible inode allocation functions.
479 */
480
481/*
482 * Allocate an inode on disk.
483 * Mode is used to tell whether the new inode will need space, and whether
484 * it is a directory.
485 *
486 * The arguments IO_agbp and alloc_done are defined to work within
487 * the constraint of one allocation per transaction.
488 * xfs_dialloc() is designed to be called twice if it has to do an
489 * allocation to make more free inodes. On the first call,
490 * IO_agbp should be set to NULL. If an inode is available,
491 * i.e., xfs_dialloc() did not need to do an allocation, an inode
492 * number is returned. In this case, IO_agbp would be set to the
493 * current ag_buf and alloc_done set to false.
494 * If an allocation needed to be done, xfs_dialloc would return
495 * the current ag_buf in IO_agbp and set alloc_done to true.
496 * The caller should then commit the current transaction, allocate a new
497 * transaction, and call xfs_dialloc() again, passing in the previous
498 * value of IO_agbp. IO_agbp should be held across the transactions.
499 * Since the agbp is locked across the two calls, the second call is
500 * guaranteed to have a free inode available.
501 *
502 * Once we successfully pick an inode its number is returned and the
503 * on-disk data structures are updated. The inode itself is not read
504 * in, since doing so would break ordering constraints with xfs_reclaim.
505 */
506int
507xfs_dialloc(
508 xfs_trans_t *tp, /* transaction pointer */
509 xfs_ino_t parent, /* parent inode (directory) */
510 mode_t mode, /* mode bits for new inode */
511 int okalloc, /* ok to allocate more space */
512 xfs_buf_t **IO_agbp, /* in/out ag header's buffer */
513 boolean_t *alloc_done, /* true if we needed to replenish
514 inode freelist */
515 xfs_ino_t *inop) /* inode number allocated */
516{
517 xfs_agnumber_t agcount; /* number of allocation groups */
518 xfs_buf_t *agbp; /* allocation group header's buffer */
519 xfs_agnumber_t agno; /* allocation group number */
520 xfs_agi_t *agi; /* allocation group header structure */
521 xfs_btree_cur_t *cur; /* inode allocation btree cursor */
522 int error; /* error return value */
523 int i; /* result code */
524 int ialloced; /* inode allocation status */
525 int noroom = 0; /* no space for inode blk allocation */
526 xfs_ino_t ino; /* fs-relative inode to be returned */
527 /* REFERENCED */
528 int j; /* result code */
529 xfs_mount_t *mp; /* file system mount structure */
530 int offset; /* index of inode in chunk */
531 xfs_agino_t pagino; /* parent's a.g. relative inode # */
532 xfs_agnumber_t pagno; /* parent's allocation group number */
Christoph Hellwig61a25842006-09-28 10:57:04 +1000533 xfs_inobt_rec_incore_t rec; /* inode allocation record */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 xfs_agnumber_t tagno; /* testing allocation group number */
535 xfs_btree_cur_t *tcur; /* temp cursor */
Christoph Hellwig61a25842006-09-28 10:57:04 +1000536 xfs_inobt_rec_incore_t trec; /* temp inode allocation record */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538
539 if (*IO_agbp == NULL) {
540 /*
541 * We do not have an agbp, so select an initial allocation
542 * group for inode allocation.
543 */
544 agbp = xfs_ialloc_ag_select(tp, parent, mode, okalloc);
545 /*
546 * Couldn't find an allocation group satisfying the
547 * criteria, give up.
548 */
549 if (!agbp) {
550 *inop = NULLFSINO;
551 return 0;
552 }
553 agi = XFS_BUF_TO_AGI(agbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100554 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 } else {
556 /*
557 * Continue where we left off before. In this case, we
558 * know that the allocation group has free inodes.
559 */
560 agbp = *IO_agbp;
561 agi = XFS_BUF_TO_AGI(agbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100562 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
563 ASSERT(be32_to_cpu(agi->agi_freecount) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
565 mp = tp->t_mountp;
566 agcount = mp->m_sb.sb_agcount;
Christoph Hellwig16259e72005-11-02 15:11:25 +1100567 agno = be32_to_cpu(agi->agi_seqno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 tagno = agno;
569 pagno = XFS_INO_TO_AGNO(mp, parent);
570 pagino = XFS_INO_TO_AGINO(mp, parent);
571
572 /*
573 * If we have already hit the ceiling of inode blocks then clear
574 * okalloc so we scan all available agi structures for a free
575 * inode.
576 */
577
578 if (mp->m_maxicount &&
579 mp->m_sb.sb_icount + XFS_IALLOC_INODES(mp) > mp->m_maxicount) {
580 noroom = 1;
581 okalloc = 0;
582 }
583
584 /*
585 * Loop until we find an allocation group that either has free inodes
586 * or in which we can allocate some inodes. Iterate through the
587 * allocation groups upward, wrapping at the end.
588 */
589 *alloc_done = B_FALSE;
590 while (!agi->agi_freecount) {
591 /*
592 * Don't do anything if we're not supposed to allocate
593 * any blocks, just go on to the next ag.
594 */
595 if (okalloc) {
596 /*
597 * Try to allocate some new inodes in the allocation
598 * group.
599 */
600 if ((error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced))) {
601 xfs_trans_brelse(tp, agbp);
602 if (error == ENOSPC) {
603 *inop = NULLFSINO;
604 return 0;
605 } else
606 return error;
607 }
608 if (ialloced) {
609 /*
610 * We successfully allocated some inodes, return
611 * the current context to the caller so that it
612 * can commit the current transaction and call
613 * us again where we left off.
614 */
Christoph Hellwig16259e72005-11-02 15:11:25 +1100615 ASSERT(be32_to_cpu(agi->agi_freecount) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 *alloc_done = B_TRUE;
617 *IO_agbp = agbp;
618 *inop = NULLFSINO;
619 return 0;
620 }
621 }
622 /*
623 * If it failed, give up on this ag.
624 */
625 xfs_trans_brelse(tp, agbp);
626 /*
627 * Go on to the next ag: get its ag header.
628 */
629nextag:
630 if (++tagno == agcount)
631 tagno = 0;
632 if (tagno == agno) {
633 *inop = NULLFSINO;
634 return noroom ? ENOSPC : 0;
635 }
636 down_read(&mp->m_peraglock);
637 if (mp->m_perag[tagno].pagi_inodeok == 0) {
638 up_read(&mp->m_peraglock);
639 goto nextag;
640 }
641 error = xfs_ialloc_read_agi(mp, tp, tagno, &agbp);
642 up_read(&mp->m_peraglock);
643 if (error)
644 goto nextag;
645 agi = XFS_BUF_TO_AGI(agbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100646 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
648 /*
649 * Here with an allocation group that has a free inode.
650 * Reset agno since we may have chosen a new ag in the
651 * loop above.
652 */
653 agno = tagno;
654 *IO_agbp = NULL;
Christoph Hellwig16259e72005-11-02 15:11:25 +1100655 cur = xfs_btree_init_cursor(mp, tp, agbp, be32_to_cpu(agi->agi_seqno),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 XFS_BTNUM_INO, (xfs_inode_t *)0, 0);
657 /*
658 * If pagino is 0 (this is the root inode allocation) use newino.
659 * This must work because we've just allocated some.
660 */
661 if (!pagino)
Christoph Hellwig16259e72005-11-02 15:11:25 +1100662 pagino = be32_to_cpu(agi->agi_newino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663#ifdef DEBUG
664 if (cur->bc_nlevels == 1) {
665 int freecount = 0;
666
667 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
668 goto error0;
669 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
670 do {
671 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
672 &rec.ir_freecount, &rec.ir_free, &i)))
673 goto error0;
674 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
675 freecount += rec.ir_freecount;
676 if ((error = xfs_inobt_increment(cur, 0, &i)))
677 goto error0;
678 } while (i == 1);
679
Christoph Hellwig16259e72005-11-02 15:11:25 +1100680 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 XFS_FORCED_SHUTDOWN(mp));
682 }
683#endif
684 /*
685 * If in the same a.g. as the parent, try to get near the parent.
686 */
687 if (pagno == agno) {
688 if ((error = xfs_inobt_lookup_le(cur, pagino, 0, 0, &i)))
689 goto error0;
690 if (i != 0 &&
691 (error = xfs_inobt_get_rec(cur, &rec.ir_startino,
692 &rec.ir_freecount, &rec.ir_free, &j)) == 0 &&
693 j == 1 &&
694 rec.ir_freecount > 0) {
695 /*
696 * Found a free inode in the same chunk
697 * as parent, done.
698 */
699 }
700 /*
701 * In the same a.g. as parent, but parent's chunk is full.
702 */
703 else {
704 int doneleft; /* done, to the left */
705 int doneright; /* done, to the right */
706
707 if (error)
708 goto error0;
709 ASSERT(i == 1);
710 ASSERT(j == 1);
711 /*
712 * Duplicate the cursor, search left & right
713 * simultaneously.
714 */
715 if ((error = xfs_btree_dup_cursor(cur, &tcur)))
716 goto error0;
717 /*
718 * Search left with tcur, back up 1 record.
719 */
720 if ((error = xfs_inobt_decrement(tcur, 0, &i)))
721 goto error1;
722 doneleft = !i;
723 if (!doneleft) {
724 if ((error = xfs_inobt_get_rec(tcur,
725 &trec.ir_startino,
726 &trec.ir_freecount,
727 &trec.ir_free, &i)))
728 goto error1;
729 XFS_WANT_CORRUPTED_GOTO(i == 1, error1);
730 }
731 /*
732 * Search right with cur, go forward 1 record.
733 */
734 if ((error = xfs_inobt_increment(cur, 0, &i)))
735 goto error1;
736 doneright = !i;
737 if (!doneright) {
738 if ((error = xfs_inobt_get_rec(cur,
739 &rec.ir_startino,
740 &rec.ir_freecount,
741 &rec.ir_free, &i)))
742 goto error1;
743 XFS_WANT_CORRUPTED_GOTO(i == 1, error1);
744 }
745 /*
746 * Loop until we find the closest inode chunk
747 * with a free one.
748 */
749 while (!doneleft || !doneright) {
750 int useleft; /* using left inode
751 chunk this time */
752
753 /*
754 * Figure out which block is closer,
755 * if both are valid.
756 */
757 if (!doneleft && !doneright)
758 useleft =
759 pagino -
760 (trec.ir_startino +
761 XFS_INODES_PER_CHUNK - 1) <
762 rec.ir_startino - pagino;
763 else
764 useleft = !doneleft;
765 /*
766 * If checking the left, does it have
767 * free inodes?
768 */
769 if (useleft && trec.ir_freecount) {
770 /*
771 * Yes, set it up as the chunk to use.
772 */
773 rec = trec;
774 xfs_btree_del_cursor(cur,
775 XFS_BTREE_NOERROR);
776 cur = tcur;
777 break;
778 }
779 /*
780 * If checking the right, does it have
781 * free inodes?
782 */
783 if (!useleft && rec.ir_freecount) {
784 /*
785 * Yes, it's already set up.
786 */
787 xfs_btree_del_cursor(tcur,
788 XFS_BTREE_NOERROR);
789 break;
790 }
791 /*
792 * If used the left, get another one
793 * further left.
794 */
795 if (useleft) {
796 if ((error = xfs_inobt_decrement(tcur, 0,
797 &i)))
798 goto error1;
799 doneleft = !i;
800 if (!doneleft) {
801 if ((error = xfs_inobt_get_rec(
802 tcur,
803 &trec.ir_startino,
804 &trec.ir_freecount,
805 &trec.ir_free, &i)))
806 goto error1;
807 XFS_WANT_CORRUPTED_GOTO(i == 1,
808 error1);
809 }
810 }
811 /*
812 * If used the right, get another one
813 * further right.
814 */
815 else {
816 if ((error = xfs_inobt_increment(cur, 0,
817 &i)))
818 goto error1;
819 doneright = !i;
820 if (!doneright) {
821 if ((error = xfs_inobt_get_rec(
822 cur,
823 &rec.ir_startino,
824 &rec.ir_freecount,
825 &rec.ir_free, &i)))
826 goto error1;
827 XFS_WANT_CORRUPTED_GOTO(i == 1,
828 error1);
829 }
830 }
831 }
832 ASSERT(!doneleft || !doneright);
833 }
834 }
835 /*
836 * In a different a.g. from the parent.
837 * See if the most recently allocated block has any free.
838 */
Christoph Hellwig16259e72005-11-02 15:11:25 +1100839 else if (be32_to_cpu(agi->agi_newino) != NULLAGINO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 if ((error = xfs_inobt_lookup_eq(cur,
Christoph Hellwig16259e72005-11-02 15:11:25 +1100841 be32_to_cpu(agi->agi_newino), 0, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 goto error0;
843 if (i == 1 &&
844 (error = xfs_inobt_get_rec(cur, &rec.ir_startino,
845 &rec.ir_freecount, &rec.ir_free, &j)) == 0 &&
846 j == 1 &&
847 rec.ir_freecount > 0) {
848 /*
849 * The last chunk allocated in the group still has
850 * a free inode.
851 */
852 }
853 /*
854 * None left in the last group, search the whole a.g.
855 */
856 else {
857 if (error)
858 goto error0;
859 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
860 goto error0;
861 ASSERT(i == 1);
862 for (;;) {
863 if ((error = xfs_inobt_get_rec(cur,
864 &rec.ir_startino,
865 &rec.ir_freecount, &rec.ir_free,
866 &i)))
867 goto error0;
868 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
869 if (rec.ir_freecount > 0)
870 break;
871 if ((error = xfs_inobt_increment(cur, 0, &i)))
872 goto error0;
873 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
874 }
875 }
876 }
877 offset = XFS_IALLOC_FIND_FREE(&rec.ir_free);
878 ASSERT(offset >= 0);
879 ASSERT(offset < XFS_INODES_PER_CHUNK);
880 ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
881 XFS_INODES_PER_CHUNK) == 0);
882 ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
883 XFS_INOBT_CLR_FREE(&rec, offset);
884 rec.ir_freecount--;
885 if ((error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount,
886 rec.ir_free)))
887 goto error0;
Christoph Hellwig16259e72005-11-02 15:11:25 +1100888 be32_add(&agi->agi_freecount, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
890 down_read(&mp->m_peraglock);
891 mp->m_perag[tagno].pagi_freecount--;
892 up_read(&mp->m_peraglock);
893#ifdef DEBUG
894 if (cur->bc_nlevels == 1) {
895 int freecount = 0;
896
897 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
898 goto error0;
899 do {
900 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
901 &rec.ir_freecount, &rec.ir_free, &i)))
902 goto error0;
903 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
904 freecount += rec.ir_freecount;
905 if ((error = xfs_inobt_increment(cur, 0, &i)))
906 goto error0;
907 } while (i == 1);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100908 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 XFS_FORCED_SHUTDOWN(mp));
910 }
911#endif
912 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
913 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
914 *inop = ino;
915 return 0;
916error1:
917 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
918error0:
919 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
920 return error;
921}
922
923/*
924 * Free disk inode. Carefully avoids touching the incore inode, all
925 * manipulations incore are the caller's responsibility.
926 * The on-disk inode is not changed by this operation, only the
927 * btree (free inode mask) is changed.
928 */
929int
930xfs_difree(
931 xfs_trans_t *tp, /* transaction pointer */
932 xfs_ino_t inode, /* inode to be freed */
933 xfs_bmap_free_t *flist, /* extents to free */
934 int *delete, /* set if inode cluster was deleted */
935 xfs_ino_t *first_ino) /* first inode in deleted cluster */
936{
937 /* REFERENCED */
938 xfs_agblock_t agbno; /* block number containing inode */
939 xfs_buf_t *agbp; /* buffer containing allocation group header */
940 xfs_agino_t agino; /* inode number relative to allocation group */
941 xfs_agnumber_t agno; /* allocation group number */
942 xfs_agi_t *agi; /* allocation group header */
943 xfs_btree_cur_t *cur; /* inode btree cursor */
944 int error; /* error return value */
945 int i; /* result code */
946 int ilen; /* inodes in an inode cluster */
947 xfs_mount_t *mp; /* mount structure for filesystem */
948 int off; /* offset of inode in inode chunk */
Christoph Hellwig61a25842006-09-28 10:57:04 +1000949 xfs_inobt_rec_incore_t rec; /* btree record */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951 mp = tp->t_mountp;
952
953 /*
954 * Break up inode number into its components.
955 */
956 agno = XFS_INO_TO_AGNO(mp, inode);
957 if (agno >= mp->m_sb.sb_agcount) {
958 cmn_err(CE_WARN,
959 "xfs_difree: agno >= mp->m_sb.sb_agcount (%d >= %d) on %s. Returning EINVAL.",
960 agno, mp->m_sb.sb_agcount, mp->m_fsname);
961 ASSERT(0);
962 return XFS_ERROR(EINVAL);
963 }
964 agino = XFS_INO_TO_AGINO(mp, inode);
965 if (inode != XFS_AGINO_TO_INO(mp, agno, agino)) {
966 cmn_err(CE_WARN,
Christoph Hellwigda1650a2005-11-02 10:21:35 +1100967 "xfs_difree: inode != XFS_AGINO_TO_INO() "
968 "(%llu != %llu) on %s. Returning EINVAL.",
969 (unsigned long long)inode,
970 (unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino),
971 mp->m_fsname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 ASSERT(0);
973 return XFS_ERROR(EINVAL);
974 }
975 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
976 if (agbno >= mp->m_sb.sb_agblocks) {
977 cmn_err(CE_WARN,
978 "xfs_difree: agbno >= mp->m_sb.sb_agblocks (%d >= %d) on %s. Returning EINVAL.",
979 agbno, mp->m_sb.sb_agblocks, mp->m_fsname);
980 ASSERT(0);
981 return XFS_ERROR(EINVAL);
982 }
983 /*
984 * Get the allocation group header.
985 */
986 down_read(&mp->m_peraglock);
987 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
988 up_read(&mp->m_peraglock);
989 if (error) {
990 cmn_err(CE_WARN,
991 "xfs_difree: xfs_ialloc_read_agi() returned an error %d on %s. Returning error.",
992 error, mp->m_fsname);
993 return error;
994 }
995 agi = XFS_BUF_TO_AGI(agbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100996 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
997 ASSERT(agbno < be32_to_cpu(agi->agi_length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 /*
999 * Initialize the cursor.
1000 */
1001 cur = xfs_btree_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO,
1002 (xfs_inode_t *)0, 0);
1003#ifdef DEBUG
1004 if (cur->bc_nlevels == 1) {
1005 int freecount = 0;
1006
1007 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
1008 goto error0;
1009 do {
1010 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
1011 &rec.ir_freecount, &rec.ir_free, &i)))
1012 goto error0;
1013 if (i) {
1014 freecount += rec.ir_freecount;
1015 if ((error = xfs_inobt_increment(cur, 0, &i)))
1016 goto error0;
1017 }
1018 } while (i == 1);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001019 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 XFS_FORCED_SHUTDOWN(mp));
1021 }
1022#endif
1023 /*
1024 * Look for the entry describing this inode.
1025 */
1026 if ((error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i))) {
1027 cmn_err(CE_WARN,
1028 "xfs_difree: xfs_inobt_lookup_le returned() an error %d on %s. Returning error.",
1029 error, mp->m_fsname);
1030 goto error0;
1031 }
1032 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1033 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino, &rec.ir_freecount,
1034 &rec.ir_free, &i))) {
1035 cmn_err(CE_WARN,
1036 "xfs_difree: xfs_inobt_get_rec() returned an error %d on %s. Returning error.",
1037 error, mp->m_fsname);
1038 goto error0;
1039 }
1040 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1041 /*
1042 * Get the offset in the inode chunk.
1043 */
1044 off = agino - rec.ir_startino;
1045 ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK);
1046 ASSERT(!XFS_INOBT_IS_FREE(&rec, off));
1047 /*
1048 * Mark the inode free & increment the count.
1049 */
1050 XFS_INOBT_SET_FREE(&rec, off);
1051 rec.ir_freecount++;
1052
1053 /*
Nathan Scottc41564b2006-03-29 08:55:14 +10001054 * When an inode cluster is free, it becomes eligible for removal
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 */
1056 if ((mp->m_flags & XFS_MOUNT_IDELETE) &&
1057 (rec.ir_freecount == XFS_IALLOC_INODES(mp))) {
1058
1059 *delete = 1;
1060 *first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino);
1061
1062 /*
1063 * Remove the inode cluster from the AGI B+Tree, adjust the
1064 * AGI and Superblock inode counts, and mark the disk space
1065 * to be freed when the transaction is committed.
1066 */
1067 ilen = XFS_IALLOC_INODES(mp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001068 be32_add(&agi->agi_count, -ilen);
1069 be32_add(&agi->agi_freecount, -(ilen - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT);
1071 down_read(&mp->m_peraglock);
1072 mp->m_perag[agno].pagi_freecount -= ilen - 1;
1073 up_read(&mp->m_peraglock);
1074 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen);
1075 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1));
1076
1077 if ((error = xfs_inobt_delete(cur, &i))) {
1078 cmn_err(CE_WARN, "xfs_difree: xfs_inobt_delete returned an error %d on %s.\n",
1079 error, mp->m_fsname);
1080 goto error0;
1081 }
1082
1083 xfs_bmap_add_free(XFS_AGB_TO_FSB(mp,
1084 agno, XFS_INO_TO_AGBNO(mp,rec.ir_startino)),
1085 XFS_IALLOC_BLOCKS(mp), flist, mp);
1086 } else {
1087 *delete = 0;
1088
1089 if ((error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount, rec.ir_free))) {
1090 cmn_err(CE_WARN,
1091 "xfs_difree: xfs_inobt_update() returned an error %d on %s. Returning error.",
1092 error, mp->m_fsname);
1093 goto error0;
1094 }
1095 /*
1096 * Change the inode free counts and log the ag/sb changes.
1097 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11001098 be32_add(&agi->agi_freecount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
1100 down_read(&mp->m_peraglock);
1101 mp->m_perag[agno].pagi_freecount++;
1102 up_read(&mp->m_peraglock);
1103 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1);
1104 }
1105
1106#ifdef DEBUG
1107 if (cur->bc_nlevels == 1) {
1108 int freecount = 0;
1109
1110 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
1111 goto error0;
1112 do {
1113 if ((error = xfs_inobt_get_rec(cur,
1114 &rec.ir_startino,
1115 &rec.ir_freecount,
1116 &rec.ir_free, &i)))
1117 goto error0;
1118 if (i) {
1119 freecount += rec.ir_freecount;
1120 if ((error = xfs_inobt_increment(cur, 0, &i)))
1121 goto error0;
1122 }
1123 } while (i == 1);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001124 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 XFS_FORCED_SHUTDOWN(mp));
1126 }
1127#endif
1128 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1129 return 0;
1130
1131error0:
1132 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1133 return error;
1134}
1135
1136/*
1137 * Return the location of the inode in bno/off, for mapping it into a buffer.
1138 */
1139/*ARGSUSED*/
1140int
1141xfs_dilocate(
1142 xfs_mount_t *mp, /* file system mount structure */
1143 xfs_trans_t *tp, /* transaction pointer */
1144 xfs_ino_t ino, /* inode to locate */
1145 xfs_fsblock_t *bno, /* output: block containing inode */
1146 int *len, /* output: num blocks in inode cluster */
1147 int *off, /* output: index in block of inode */
1148 uint flags) /* flags concerning inode lookup */
1149{
1150 xfs_agblock_t agbno; /* block number of inode in the alloc group */
1151 xfs_buf_t *agbp; /* agi buffer */
1152 xfs_agino_t agino; /* inode number within alloc group */
1153 xfs_agnumber_t agno; /* allocation group number */
1154 int blks_per_cluster; /* num blocks per inode cluster */
1155 xfs_agblock_t chunk_agbno; /* first block in inode chunk */
1156 xfs_agino_t chunk_agino; /* first agino in inode chunk */
1157 __int32_t chunk_cnt; /* count of free inodes in chunk */
1158 xfs_inofree_t chunk_free; /* mask of free inodes in chunk */
1159 xfs_agblock_t cluster_agbno; /* first block in inode cluster */
1160 xfs_btree_cur_t *cur; /* inode btree cursor */
1161 int error; /* error code */
1162 int i; /* temp state */
1163 int offset; /* index of inode in its buffer */
1164 int offset_agbno; /* blks from chunk start to inode */
1165
1166 ASSERT(ino != NULLFSINO);
1167 /*
1168 * Split up the inode number into its parts.
1169 */
1170 agno = XFS_INO_TO_AGNO(mp, ino);
1171 agino = XFS_INO_TO_AGINO(mp, ino);
1172 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1173 if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks ||
1174 ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1175#ifdef DEBUG
Nathan Scott4d1a2ed2006-06-09 17:12:28 +10001176 /* no diagnostics for bulkstat, ino comes from userspace */
1177 if (flags & XFS_IMAP_BULKSTAT)
1178 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 if (agno >= mp->m_sb.sb_agcount) {
1180 xfs_fs_cmn_err(CE_ALERT, mp,
1181 "xfs_dilocate: agno (%d) >= "
1182 "mp->m_sb.sb_agcount (%d)",
1183 agno, mp->m_sb.sb_agcount);
1184 }
1185 if (agbno >= mp->m_sb.sb_agblocks) {
1186 xfs_fs_cmn_err(CE_ALERT, mp,
1187 "xfs_dilocate: agbno (0x%llx) >= "
1188 "mp->m_sb.sb_agblocks (0x%lx)",
1189 (unsigned long long) agbno,
1190 (unsigned long) mp->m_sb.sb_agblocks);
1191 }
1192 if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1193 xfs_fs_cmn_err(CE_ALERT, mp,
1194 "xfs_dilocate: ino (0x%llx) != "
1195 "XFS_AGINO_TO_INO(mp, agno, agino) "
1196 "(0x%llx)",
1197 ino, XFS_AGINO_TO_INO(mp, agno, agino));
1198 }
Nathan Scott745b1f472006-09-28 11:02:23 +10001199 xfs_stack_trace();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200#endif /* DEBUG */
1201 return XFS_ERROR(EINVAL);
1202 }
1203 if ((mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) ||
1204 !(flags & XFS_IMAP_LOOKUP)) {
1205 offset = XFS_INO_TO_OFFSET(mp, ino);
1206 ASSERT(offset < mp->m_sb.sb_inopblock);
1207 *bno = XFS_AGB_TO_FSB(mp, agno, agbno);
1208 *off = offset;
1209 *len = 1;
1210 return 0;
1211 }
1212 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_blocklog;
1213 if (*bno != NULLFSBLOCK) {
1214 offset = XFS_INO_TO_OFFSET(mp, ino);
1215 ASSERT(offset < mp->m_sb.sb_inopblock);
1216 cluster_agbno = XFS_FSB_TO_AGBNO(mp, *bno);
1217 *off = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
1218 offset;
1219 *len = blks_per_cluster;
1220 return 0;
1221 }
1222 if (mp->m_inoalign_mask) {
1223 offset_agbno = agbno & mp->m_inoalign_mask;
1224 chunk_agbno = agbno - offset_agbno;
1225 } else {
1226 down_read(&mp->m_peraglock);
1227 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1228 up_read(&mp->m_peraglock);
1229 if (error) {
1230#ifdef DEBUG
1231 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1232 "xfs_ialloc_read_agi() returned "
1233 "error %d, agno %d",
1234 error, agno);
1235#endif /* DEBUG */
1236 return error;
1237 }
1238 cur = xfs_btree_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO,
1239 (xfs_inode_t *)0, 0);
1240 if ((error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i))) {
1241#ifdef DEBUG
1242 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1243 "xfs_inobt_lookup_le() failed");
1244#endif /* DEBUG */
1245 goto error0;
1246 }
1247 if ((error = xfs_inobt_get_rec(cur, &chunk_agino, &chunk_cnt,
1248 &chunk_free, &i))) {
1249#ifdef DEBUG
1250 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1251 "xfs_inobt_get_rec() failed");
1252#endif /* DEBUG */
1253 goto error0;
1254 }
1255 if (i == 0) {
1256#ifdef DEBUG
1257 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1258 "xfs_inobt_get_rec() failed");
1259#endif /* DEBUG */
1260 error = XFS_ERROR(EINVAL);
1261 }
1262 xfs_trans_brelse(tp, agbp);
1263 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1264 if (error)
1265 return error;
1266 chunk_agbno = XFS_AGINO_TO_AGBNO(mp, chunk_agino);
1267 offset_agbno = agbno - chunk_agbno;
1268 }
1269 ASSERT(agbno >= chunk_agbno);
1270 cluster_agbno = chunk_agbno +
1271 ((offset_agbno / blks_per_cluster) * blks_per_cluster);
1272 offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
1273 XFS_INO_TO_OFFSET(mp, ino);
1274 *bno = XFS_AGB_TO_FSB(mp, agno, cluster_agbno);
1275 *off = offset;
1276 *len = blks_per_cluster;
1277 return 0;
1278error0:
1279 xfs_trans_brelse(tp, agbp);
1280 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1281 return error;
1282}
1283
1284/*
1285 * Compute and fill in value of m_in_maxlevels.
1286 */
1287void
1288xfs_ialloc_compute_maxlevels(
1289 xfs_mount_t *mp) /* file system mount structure */
1290{
1291 int level;
1292 uint maxblocks;
1293 uint maxleafents;
1294 int minleafrecs;
1295 int minnoderecs;
1296
1297 maxleafents = (1LL << XFS_INO_AGINO_BITS(mp)) >>
1298 XFS_INODES_PER_CHUNK_LOG;
1299 minleafrecs = mp->m_alloc_mnr[0];
1300 minnoderecs = mp->m_alloc_mnr[1];
1301 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
1302 for (level = 1; maxblocks > 1; level++)
1303 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
1304 mp->m_in_maxlevels = level;
1305}
1306
1307/*
1308 * Log specified fields for the ag hdr (inode section)
1309 */
1310void
1311xfs_ialloc_log_agi(
1312 xfs_trans_t *tp, /* transaction pointer */
1313 xfs_buf_t *bp, /* allocation group header buffer */
1314 int fields) /* bitmask of fields to log */
1315{
1316 int first; /* first byte number */
1317 int last; /* last byte number */
1318 static const short offsets[] = { /* field starting offsets */
1319 /* keep in sync with bit definitions */
1320 offsetof(xfs_agi_t, agi_magicnum),
1321 offsetof(xfs_agi_t, agi_versionnum),
1322 offsetof(xfs_agi_t, agi_seqno),
1323 offsetof(xfs_agi_t, agi_length),
1324 offsetof(xfs_agi_t, agi_count),
1325 offsetof(xfs_agi_t, agi_root),
1326 offsetof(xfs_agi_t, agi_level),
1327 offsetof(xfs_agi_t, agi_freecount),
1328 offsetof(xfs_agi_t, agi_newino),
1329 offsetof(xfs_agi_t, agi_dirino),
1330 offsetof(xfs_agi_t, agi_unlinked),
1331 sizeof(xfs_agi_t)
1332 };
1333#ifdef DEBUG
1334 xfs_agi_t *agi; /* allocation group header */
1335
1336 agi = XFS_BUF_TO_AGI(bp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001337 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338#endif
1339 /*
1340 * Compute byte offsets for the first and last fields.
1341 */
1342 xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS, &first, &last);
1343 /*
1344 * Log the allocation group inode header buffer.
1345 */
1346 xfs_trans_log_buf(tp, bp, first, last);
1347}
1348
1349/*
1350 * Read in the allocation group header (inode allocation section)
1351 */
1352int
1353xfs_ialloc_read_agi(
1354 xfs_mount_t *mp, /* file system mount structure */
1355 xfs_trans_t *tp, /* transaction pointer */
1356 xfs_agnumber_t agno, /* allocation group number */
1357 xfs_buf_t **bpp) /* allocation group hdr buf */
1358{
1359 xfs_agi_t *agi; /* allocation group header */
1360 int agi_ok; /* agi is consistent */
1361 xfs_buf_t *bp; /* allocation group hdr buf */
1362 xfs_perag_t *pag; /* per allocation group data */
1363 int error;
1364
1365 ASSERT(agno != NULLAGNUMBER);
1366 error = xfs_trans_read_buf(
1367 mp, tp, mp->m_ddev_targp,
1368 XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
1369 XFS_FSS_TO_BB(mp, 1), 0, &bp);
1370 if (error)
1371 return error;
1372 ASSERT(bp && !XFS_BUF_GETERROR(bp));
1373
1374 /*
1375 * Validate the magic number of the agi block.
1376 */
1377 agi = XFS_BUF_TO_AGI(bp);
1378 agi_ok =
Christoph Hellwig16259e72005-11-02 15:11:25 +11001379 be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC &&
1380 XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 if (unlikely(XFS_TEST_ERROR(!agi_ok, mp, XFS_ERRTAG_IALLOC_READ_AGI,
1382 XFS_RANDOM_IALLOC_READ_AGI))) {
1383 XFS_CORRUPTION_ERROR("xfs_ialloc_read_agi", XFS_ERRLEVEL_LOW,
1384 mp, agi);
1385 xfs_trans_brelse(tp, bp);
1386 return XFS_ERROR(EFSCORRUPTED);
1387 }
1388 pag = &mp->m_perag[agno];
1389 if (!pag->pagi_init) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11001390 pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
David Chinner92821e22007-05-24 15:26:31 +10001391 pag->pagi_count = be32_to_cpu(agi->agi_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 pag->pagi_init = 1;
1393 } else {
1394 /*
1395 * It's possible for these to be out of sync if
1396 * we are in the middle of a forced shutdown.
1397 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11001398 ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) ||
1399 XFS_FORCED_SHUTDOWN(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 }
1401
1402#ifdef DEBUG
1403 {
1404 int i;
1405
1406 for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++)
1407 ASSERT(agi->agi_unlinked[i]);
1408 }
1409#endif
1410
1411 XFS_BUF_SET_VTYPE_REF(bp, B_FS_AGI, XFS_AGI_REF);
1412 *bpp = bp;
1413 return 0;
1414}
David Chinner92821e22007-05-24 15:26:31 +10001415
1416/*
1417 * Read in the agi to initialise the per-ag data in the mount structure
1418 */
1419int
1420xfs_ialloc_pagi_init(
1421 xfs_mount_t *mp, /* file system mount structure */
1422 xfs_trans_t *tp, /* transaction pointer */
1423 xfs_agnumber_t agno) /* allocation group number */
1424{
1425 xfs_buf_t *bp = NULL;
1426 int error;
1427
1428 error = xfs_ialloc_read_agi(mp, tp, agno, &bp);
1429 if (error)
1430 return error;
1431 if (bp)
1432 xfs_trans_brelse(tp, bp);
1433 return 0;
1434}