vfs: remove extraneous NULL d_inode check from do_filp_open
[linux-2.6-block.git] / fs / xfs / xfs_ialloc.c
CommitLineData
1da177e4 1/*
7b718769
NS
2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
1da177e4 4 *
7b718769
NS
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
1da177e4
LT
7 * published by the Free Software Foundation.
8 *
7b718769
NS
9 * 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.
1da177e4 13 *
7b718769
NS
14 * 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
1da177e4 17 */
1da177e4 18#include "xfs.h"
a844f451 19#include "xfs_fs.h"
1da177e4 20#include "xfs_types.h"
a844f451 21#include "xfs_bit.h"
1da177e4 22#include "xfs_log.h"
a844f451 23#include "xfs_inum.h"
1da177e4
LT
24#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
1da177e4
LT
27#include "xfs_dir2.h"
28#include "xfs_dmapi.h"
29#include "xfs_mount.h"
1da177e4 30#include "xfs_bmap_btree.h"
a844f451 31#include "xfs_alloc_btree.h"
1da177e4 32#include "xfs_ialloc_btree.h"
1da177e4 33#include "xfs_dir2_sf.h"
a844f451 34#include "xfs_attr_sf.h"
1da177e4
LT
35#include "xfs_dinode.h"
36#include "xfs_inode.h"
a844f451
NS
37#include "xfs_btree.h"
38#include "xfs_ialloc.h"
1da177e4 39#include "xfs_alloc.h"
1da177e4
LT
40#include "xfs_rtalloc.h"
41#include "xfs_error.h"
42#include "xfs_bmap.h"
43
1da177e4
LT
44
45/*
46 * Allocation group level functions.
47 */
75de2a91
DC
48static inline int
49xfs_ialloc_cluster_alignment(
50 xfs_alloc_arg_t *args)
51{
52 if (xfs_sb_version_hasalign(&args->mp->m_sb) &&
53 args->mp->m_sb.sb_inoalignmt >=
54 XFS_B_TO_FSBT(args->mp, XFS_INODE_CLUSTER_SIZE(args->mp)))
55 return args->mp->m_sb.sb_inoalignmt;
56 return 1;
57}
1da177e4 58
fe033cc8 59/*
21875505 60 * Lookup a record by ino in the btree given by cur.
fe033cc8 61 */
81e25176 62int /* error */
21875505 63xfs_inobt_lookup(
fe033cc8
CH
64 struct xfs_btree_cur *cur, /* btree cursor */
65 xfs_agino_t ino, /* starting inode of chunk */
21875505 66 xfs_lookup_t dir, /* <=, >=, == */
fe033cc8
CH
67 int *stat) /* success/failure */
68{
69 cur->bc_rec.i.ir_startino = ino;
21875505
CH
70 cur->bc_rec.i.ir_freecount = 0;
71 cur->bc_rec.i.ir_free = 0;
72 return xfs_btree_lookup(cur, dir, stat);
fe033cc8
CH
73}
74
278d0ca1 75/*
afabc24a 76 * Update the record referred to by cur to the value given.
278d0ca1
CH
77 * This either works (return 0) or gets an EFSCORRUPTED error.
78 */
79STATIC int /* error */
80xfs_inobt_update(
81 struct xfs_btree_cur *cur, /* btree cursor */
afabc24a 82 xfs_inobt_rec_incore_t *irec) /* btree record */
278d0ca1
CH
83{
84 union xfs_btree_rec rec;
85
afabc24a
CH
86 rec.inobt.ir_startino = cpu_to_be32(irec->ir_startino);
87 rec.inobt.ir_freecount = cpu_to_be32(irec->ir_freecount);
88 rec.inobt.ir_free = cpu_to_be64(irec->ir_free);
278d0ca1
CH
89 return xfs_btree_update(cur, &rec);
90}
91
8cc938fe
CH
92/*
93 * Get the data from the pointed-to record.
94 */
95int /* error */
96xfs_inobt_get_rec(
97 struct xfs_btree_cur *cur, /* btree cursor */
2e287a73 98 xfs_inobt_rec_incore_t *irec, /* btree record */
8cc938fe
CH
99 int *stat) /* output: success/failure */
100{
101 union xfs_btree_rec *rec;
102 int error;
103
104 error = xfs_btree_get_rec(cur, &rec, stat);
105 if (!error && *stat == 1) {
2e287a73
CH
106 irec->ir_startino = be32_to_cpu(rec->inobt.ir_startino);
107 irec->ir_freecount = be32_to_cpu(rec->inobt.ir_freecount);
108 irec->ir_free = be64_to_cpu(rec->inobt.ir_free);
8cc938fe
CH
109 }
110 return error;
111}
112
0b48db80
DC
113/*
114 * Verify that the number of free inodes in the AGI is correct.
115 */
116#ifdef DEBUG
117STATIC int
118xfs_check_agi_freecount(
119 struct xfs_btree_cur *cur,
120 struct xfs_agi *agi)
121{
122 if (cur->bc_nlevels == 1) {
123 xfs_inobt_rec_incore_t rec;
124 int freecount = 0;
125 int error;
126 int i;
127
21875505 128 error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
0b48db80
DC
129 if (error)
130 return error;
131
132 do {
133 error = xfs_inobt_get_rec(cur, &rec, &i);
134 if (error)
135 return error;
136
137 if (i) {
138 freecount += rec.ir_freecount;
139 error = xfs_btree_increment(cur, 0, &i);
140 if (error)
141 return error;
142 }
143 } while (i == 1);
144
145 if (!XFS_FORCED_SHUTDOWN(cur->bc_mp))
146 ASSERT(freecount == be32_to_cpu(agi->agi_freecount));
147 }
148 return 0;
149}
150#else
151#define xfs_check_agi_freecount(cur, agi) 0
152#endif
153
85c0b2ab
DC
154/*
155 * Initialise a new set of inodes.
156 */
157STATIC void
158xfs_ialloc_inode_init(
159 struct xfs_mount *mp,
160 struct xfs_trans *tp,
161 xfs_agnumber_t agno,
162 xfs_agblock_t agbno,
163 xfs_agblock_t length,
164 unsigned int gen)
165{
166 struct xfs_buf *fbuf;
167 struct xfs_dinode *free;
168 int blks_per_cluster, nbufs, ninodes;
169 int version;
170 int i, j;
171 xfs_daddr_t d;
172
173 /*
174 * Loop over the new block(s), filling in the inodes.
175 * For small block sizes, manipulate the inodes in buffers
176 * which are multiples of the blocks size.
177 */
178 if (mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) {
179 blks_per_cluster = 1;
180 nbufs = length;
181 ninodes = mp->m_sb.sb_inopblock;
182 } else {
183 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) /
184 mp->m_sb.sb_blocksize;
185 nbufs = length / blks_per_cluster;
186 ninodes = blks_per_cluster * mp->m_sb.sb_inopblock;
187 }
188
189 /*
190 * Figure out what version number to use in the inodes we create.
191 * If the superblock version has caught up to the one that supports
192 * the new inode format, then use the new inode version. Otherwise
193 * use the old version so that old kernels will continue to be
194 * able to use the file system.
195 */
196 if (xfs_sb_version_hasnlink(&mp->m_sb))
197 version = 2;
198 else
199 version = 1;
200
201 for (j = 0; j < nbufs; j++) {
202 /*
203 * Get the block.
204 */
205 d = XFS_AGB_TO_DADDR(mp, agno, agbno + (j * blks_per_cluster));
206 fbuf = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
207 mp->m_bsize * blks_per_cluster,
208 XFS_BUF_LOCK);
209 ASSERT(fbuf);
210 ASSERT(!XFS_BUF_GETERROR(fbuf));
211
212 /*
213 * Initialize all inodes in this buffer and then log them.
214 *
215 * XXX: It would be much better if we had just one transaction
216 * to log a whole cluster of inodes instead of all the
217 * individual transactions causing a lot of log traffic.
218 */
219 xfs_biozero(fbuf, 0, ninodes << mp->m_sb.sb_inodelog);
220 for (i = 0; i < ninodes; i++) {
221 int ioffset = i << mp->m_sb.sb_inodelog;
222 uint isize = sizeof(struct xfs_dinode);
223
224 free = xfs_make_iptr(mp, fbuf, i);
225 free->di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
226 free->di_version = version;
227 free->di_gen = cpu_to_be32(gen);
228 free->di_next_unlinked = cpu_to_be32(NULLAGINO);
229 xfs_trans_log_buf(tp, fbuf, ioffset, ioffset + isize - 1);
230 }
231 xfs_trans_inode_alloc_buf(tp, fbuf);
232 }
233}
234
1da177e4
LT
235/*
236 * Allocate new inodes in the allocation group specified by agbp.
237 * Return 0 for success, else error code.
238 */
239STATIC int /* error code or 0 */
240xfs_ialloc_ag_alloc(
241 xfs_trans_t *tp, /* transaction pointer */
242 xfs_buf_t *agbp, /* alloc group buffer */
243 int *alloc)
244{
245 xfs_agi_t *agi; /* allocation group header */
246 xfs_alloc_arg_t args; /* allocation argument structure */
1da177e4 247 xfs_btree_cur_t *cur; /* inode btree cursor */
92821e2b 248 xfs_agnumber_t agno;
1da177e4 249 int error;
85c0b2ab 250 int i;
1da177e4
LT
251 xfs_agino_t newino; /* new first inode's number */
252 xfs_agino_t newlen; /* new number of inodes */
1da177e4 253 xfs_agino_t thisino; /* current inode number, for loop */
3ccb8b5f 254 int isaligned = 0; /* inode allocation at stripe unit */
1da177e4 255 /* boundary */
1da177e4
LT
256
257 args.tp = tp;
258 args.mp = tp->t_mountp;
259
260 /*
261 * Locking will ensure that we don't have two callers in here
262 * at one time.
263 */
264 newlen = XFS_IALLOC_INODES(args.mp);
265 if (args.mp->m_maxicount &&
266 args.mp->m_sb.sb_icount + newlen > args.mp->m_maxicount)
267 return XFS_ERROR(ENOSPC);
268 args.minlen = args.maxlen = XFS_IALLOC_BLOCKS(args.mp);
269 /*
3ccb8b5f
GO
270 * First try to allocate inodes contiguous with the last-allocated
271 * chunk of inodes. If the filesystem is striped, this will fill
272 * an entire stripe unit with inodes.
273 */
1da177e4 274 agi = XFS_BUF_TO_AGI(agbp);
3ccb8b5f 275 newino = be32_to_cpu(agi->agi_newino);
85c0b2ab 276 agno = be32_to_cpu(agi->agi_seqno);
019ff2d5
NS
277 args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) +
278 XFS_IALLOC_BLOCKS(args.mp);
279 if (likely(newino != NULLAGINO &&
280 (args.agbno < be32_to_cpu(agi->agi_length)))) {
85c0b2ab 281 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
3ccb8b5f
GO
282 args.type = XFS_ALLOCTYPE_THIS_BNO;
283 args.mod = args.total = args.wasdel = args.isfl =
284 args.userdata = args.minalignslop = 0;
285 args.prod = 1;
75de2a91 286
3ccb8b5f 287 /*
75de2a91
DC
288 * We need to take into account alignment here to ensure that
289 * we don't modify the free list if we fail to have an exact
290 * block. If we don't have an exact match, and every oher
291 * attempt allocation attempt fails, we'll end up cancelling
292 * a dirty transaction and shutting down.
293 *
294 * For an exact allocation, alignment must be 1,
295 * however we need to take cluster alignment into account when
296 * fixing up the freelist. Use the minalignslop field to
297 * indicate that extra blocks might be required for alignment,
298 * but not to use them in the actual exact allocation.
3ccb8b5f 299 */
75de2a91
DC
300 args.alignment = 1;
301 args.minalignslop = xfs_ialloc_cluster_alignment(&args) - 1;
302
303 /* Allow space for the inode btree to split. */
0d87e656 304 args.minleft = args.mp->m_in_maxlevels - 1;
3ccb8b5f
GO
305 if ((error = xfs_alloc_vextent(&args)))
306 return error;
307 } else
308 args.fsbno = NULLFSBLOCK;
1da177e4 309
3ccb8b5f
GO
310 if (unlikely(args.fsbno == NULLFSBLOCK)) {
311 /*
312 * Set the alignment for the allocation.
313 * If stripe alignment is turned on then align at stripe unit
314 * boundary.
019ff2d5
NS
315 * If the cluster size is smaller than a filesystem block
316 * then we're doing I/O for inodes in filesystem block size
3ccb8b5f
GO
317 * pieces, so don't need alignment anyway.
318 */
319 isaligned = 0;
320 if (args.mp->m_sinoalign) {
321 ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN));
322 args.alignment = args.mp->m_dalign;
323 isaligned = 1;
75de2a91
DC
324 } else
325 args.alignment = xfs_ialloc_cluster_alignment(&args);
3ccb8b5f
GO
326 /*
327 * Need to figure out where to allocate the inode blocks.
328 * Ideally they should be spaced out through the a.g.
329 * For now, just allocate blocks up front.
330 */
331 args.agbno = be32_to_cpu(agi->agi_root);
85c0b2ab 332 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
3ccb8b5f
GO
333 /*
334 * Allocate a fixed-size extent of inodes.
335 */
336 args.type = XFS_ALLOCTYPE_NEAR_BNO;
337 args.mod = args.total = args.wasdel = args.isfl =
338 args.userdata = args.minalignslop = 0;
339 args.prod = 1;
340 /*
341 * Allow space for the inode btree to split.
342 */
0d87e656 343 args.minleft = args.mp->m_in_maxlevels - 1;
3ccb8b5f
GO
344 if ((error = xfs_alloc_vextent(&args)))
345 return error;
346 }
019ff2d5 347
1da177e4
LT
348 /*
349 * If stripe alignment is turned on, then try again with cluster
350 * alignment.
351 */
352 if (isaligned && args.fsbno == NULLFSBLOCK) {
353 args.type = XFS_ALLOCTYPE_NEAR_BNO;
16259e7d 354 args.agbno = be32_to_cpu(agi->agi_root);
85c0b2ab 355 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
75de2a91 356 args.alignment = xfs_ialloc_cluster_alignment(&args);
1da177e4
LT
357 if ((error = xfs_alloc_vextent(&args)))
358 return error;
359 }
360
361 if (args.fsbno == NULLFSBLOCK) {
362 *alloc = 0;
363 return 0;
364 }
365 ASSERT(args.len == args.minlen);
1da177e4 366
359346a9 367 /*
85c0b2ab
DC
368 * Stamp and write the inode buffers.
369 *
359346a9
DC
370 * Seed the new inode cluster with a random generation number. This
371 * prevents short-term reuse of generation numbers if a chunk is
372 * freed and then immediately reallocated. We use random numbers
373 * rather than a linear progression to prevent the next generation
374 * number from being easily guessable.
375 */
85c0b2ab
DC
376 xfs_ialloc_inode_init(args.mp, tp, agno, args.agbno, args.len,
377 random32());
d42f08f6 378
85c0b2ab
DC
379 /*
380 * Convert the results.
381 */
382 newino = XFS_OFFBNO_TO_AGINO(args.mp, args.agbno, 0);
413d57c9
MS
383 be32_add_cpu(&agi->agi_count, newlen);
384 be32_add_cpu(&agi->agi_freecount, newlen);
1da177e4 385 down_read(&args.mp->m_peraglock);
92821e2b 386 args.mp->m_perag[agno].pagi_freecount += newlen;
1da177e4 387 up_read(&args.mp->m_peraglock);
16259e7d 388 agi->agi_newino = cpu_to_be32(newino);
85c0b2ab 389
1da177e4
LT
390 /*
391 * Insert records describing the new inode chunk into the btree.
392 */
561f7d17 393 cur = xfs_inobt_init_cursor(args.mp, tp, agbp, agno);
1da177e4
LT
394 for (thisino = newino;
395 thisino < newino + newlen;
396 thisino += XFS_INODES_PER_CHUNK) {
21875505
CH
397 cur->bc_rec.i.ir_startino = thisino;
398 cur->bc_rec.i.ir_freecount = XFS_INODES_PER_CHUNK;
399 cur->bc_rec.i.ir_free = XFS_INOBT_ALL_FREE;
400 error = xfs_btree_lookup(cur, XFS_LOOKUP_EQ, &i);
401 if (error) {
1da177e4
LT
402 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
403 return error;
404 }
405 ASSERT(i == 0);
21875505
CH
406 error = xfs_btree_insert(cur, &i);
407 if (error) {
1da177e4
LT
408 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
409 return error;
410 }
411 ASSERT(i == 1);
412 }
413 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
414 /*
415 * Log allocation group header fields
416 */
417 xfs_ialloc_log_agi(tp, agbp,
418 XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO);
419 /*
420 * Modify/log superblock values for inode count and inode free count.
421 */
422 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen);
423 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen);
424 *alloc = 1;
425 return 0;
426}
427
b8f82a4a 428STATIC xfs_agnumber_t
1da177e4
LT
429xfs_ialloc_next_ag(
430 xfs_mount_t *mp)
431{
432 xfs_agnumber_t agno;
433
434 spin_lock(&mp->m_agirotor_lock);
435 agno = mp->m_agirotor;
436 if (++mp->m_agirotor == mp->m_maxagi)
437 mp->m_agirotor = 0;
438 spin_unlock(&mp->m_agirotor_lock);
439
440 return agno;
441}
442
443/*
444 * Select an allocation group to look for a free inode in, based on the parent
445 * inode and then mode. Return the allocation group buffer.
446 */
447STATIC xfs_buf_t * /* allocation group buffer */
448xfs_ialloc_ag_select(
449 xfs_trans_t *tp, /* transaction pointer */
450 xfs_ino_t parent, /* parent directory inode number */
451 mode_t mode, /* bits set to indicate file type */
452 int okalloc) /* ok to allocate more space */
453{
454 xfs_buf_t *agbp; /* allocation group header buffer */
455 xfs_agnumber_t agcount; /* number of ag's in the filesystem */
456 xfs_agnumber_t agno; /* current ag number */
457 int flags; /* alloc buffer locking flags */
458 xfs_extlen_t ineed; /* blocks needed for inode allocation */
459 xfs_extlen_t longest = 0; /* longest extent available */
460 xfs_mount_t *mp; /* mount point structure */
461 int needspace; /* file mode implies space allocated */
462 xfs_perag_t *pag; /* per allocation group data */
463 xfs_agnumber_t pagno; /* parent (starting) ag number */
464
465 /*
466 * Files of these types need at least one block if length > 0
467 * (and they won't fit in the inode, but that's hard to figure out).
468 */
469 needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode);
470 mp = tp->t_mountp;
471 agcount = mp->m_maxagi;
472 if (S_ISDIR(mode))
473 pagno = xfs_ialloc_next_ag(mp);
474 else {
475 pagno = XFS_INO_TO_AGNO(mp, parent);
476 if (pagno >= agcount)
477 pagno = 0;
478 }
479 ASSERT(pagno < agcount);
480 /*
481 * Loop through allocation groups, looking for one with a little
482 * free space in it. Note we don't look for free inodes, exactly.
483 * Instead, we include whether there is a need to allocate inodes
484 * to mean that blocks must be allocated for them,
485 * if none are currently free.
486 */
487 agno = pagno;
488 flags = XFS_ALLOC_FLAG_TRYLOCK;
489 down_read(&mp->m_peraglock);
490 for (;;) {
491 pag = &mp->m_perag[agno];
492 if (!pag->pagi_init) {
493 if (xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
494 agbp = NULL;
495 goto nextag;
496 }
497 } else
498 agbp = NULL;
499
500 if (!pag->pagi_inodeok) {
501 xfs_ialloc_next_ag(mp);
502 goto unlock_nextag;
503 }
504
505 /*
506 * Is there enough free space for the file plus a block
507 * of inodes (if we need to allocate some)?
508 */
509 ineed = pag->pagi_freecount ? 0 : XFS_IALLOC_BLOCKS(mp);
510 if (ineed && !pag->pagf_init) {
511 if (agbp == NULL &&
512 xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
513 agbp = NULL;
514 goto nextag;
515 }
516 (void)xfs_alloc_pagf_init(mp, tp, agno, flags);
517 }
518 if (!ineed || pag->pagf_init) {
519 if (ineed && !(longest = pag->pagf_longest))
520 longest = pag->pagf_flcount > 0;
521 if (!ineed ||
522 (pag->pagf_freeblks >= needspace + ineed &&
523 longest >= ineed &&
524 okalloc)) {
525 if (agbp == NULL &&
526 xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
527 agbp = NULL;
528 goto nextag;
529 }
530 up_read(&mp->m_peraglock);
531 return agbp;
532 }
533 }
534unlock_nextag:
535 if (agbp)
536 xfs_trans_brelse(tp, agbp);
537nextag:
538 /*
539 * No point in iterating over the rest, if we're shutting
540 * down.
541 */
542 if (XFS_FORCED_SHUTDOWN(mp)) {
543 up_read(&mp->m_peraglock);
1121b219 544 return NULL;
1da177e4
LT
545 }
546 agno++;
547 if (agno >= agcount)
548 agno = 0;
549 if (agno == pagno) {
550 if (flags == 0) {
551 up_read(&mp->m_peraglock);
1121b219 552 return NULL;
1da177e4
LT
553 }
554 flags = 0;
555 }
556 }
557}
558
4254b0bb
CH
559/*
560 * Try to retrieve the next record to the left/right from the current one.
561 */
562STATIC int
563xfs_ialloc_next_rec(
564 struct xfs_btree_cur *cur,
565 xfs_inobt_rec_incore_t *rec,
566 int *done,
567 int left)
568{
569 int error;
570 int i;
571
572 if (left)
573 error = xfs_btree_decrement(cur, 0, &i);
574 else
575 error = xfs_btree_increment(cur, 0, &i);
576
577 if (error)
578 return error;
579 *done = !i;
580 if (i) {
581 error = xfs_inobt_get_rec(cur, rec, &i);
582 if (error)
583 return error;
584 XFS_WANT_CORRUPTED_RETURN(i == 1);
585 }
586
587 return 0;
588}
589
bd169565
DC
590STATIC int
591xfs_ialloc_get_rec(
592 struct xfs_btree_cur *cur,
593 xfs_agino_t agino,
594 xfs_inobt_rec_incore_t *rec,
595 int *done,
596 int left)
597{
598 int error;
599 int i;
600
601 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_EQ, &i);
602 if (error)
603 return error;
604 *done = !i;
605 if (i) {
606 error = xfs_inobt_get_rec(cur, rec, &i);
607 if (error)
608 return error;
609 XFS_WANT_CORRUPTED_RETURN(i == 1);
610 }
611
612 return 0;
613}
0b48db80 614
1da177e4
LT
615/*
616 * Visible inode allocation functions.
617 */
618
619/*
620 * Allocate an inode on disk.
621 * Mode is used to tell whether the new inode will need space, and whether
622 * it is a directory.
623 *
624 * The arguments IO_agbp and alloc_done are defined to work within
625 * the constraint of one allocation per transaction.
626 * xfs_dialloc() is designed to be called twice if it has to do an
627 * allocation to make more free inodes. On the first call,
628 * IO_agbp should be set to NULL. If an inode is available,
629 * i.e., xfs_dialloc() did not need to do an allocation, an inode
630 * number is returned. In this case, IO_agbp would be set to the
631 * current ag_buf and alloc_done set to false.
632 * If an allocation needed to be done, xfs_dialloc would return
633 * the current ag_buf in IO_agbp and set alloc_done to true.
634 * The caller should then commit the current transaction, allocate a new
635 * transaction, and call xfs_dialloc() again, passing in the previous
636 * value of IO_agbp. IO_agbp should be held across the transactions.
637 * Since the agbp is locked across the two calls, the second call is
638 * guaranteed to have a free inode available.
639 *
640 * Once we successfully pick an inode its number is returned and the
641 * on-disk data structures are updated. The inode itself is not read
642 * in, since doing so would break ordering constraints with xfs_reclaim.
643 */
644int
645xfs_dialloc(
646 xfs_trans_t *tp, /* transaction pointer */
647 xfs_ino_t parent, /* parent inode (directory) */
648 mode_t mode, /* mode bits for new inode */
649 int okalloc, /* ok to allocate more space */
650 xfs_buf_t **IO_agbp, /* in/out ag header's buffer */
651 boolean_t *alloc_done, /* true if we needed to replenish
652 inode freelist */
653 xfs_ino_t *inop) /* inode number allocated */
654{
655 xfs_agnumber_t agcount; /* number of allocation groups */
656 xfs_buf_t *agbp; /* allocation group header's buffer */
657 xfs_agnumber_t agno; /* allocation group number */
658 xfs_agi_t *agi; /* allocation group header structure */
659 xfs_btree_cur_t *cur; /* inode allocation btree cursor */
660 int error; /* error return value */
661 int i; /* result code */
662 int ialloced; /* inode allocation status */
663 int noroom = 0; /* no space for inode blk allocation */
664 xfs_ino_t ino; /* fs-relative inode to be returned */
665 /* REFERENCED */
666 int j; /* result code */
667 xfs_mount_t *mp; /* file system mount structure */
668 int offset; /* index of inode in chunk */
4254b0bb
CH
669 xfs_agino_t pagino; /* parent's AG relative inode # */
670 xfs_agnumber_t pagno; /* parent's AG number */
61a25848 671 xfs_inobt_rec_incore_t rec; /* inode allocation record */
1da177e4
LT
672 xfs_agnumber_t tagno; /* testing allocation group number */
673 xfs_btree_cur_t *tcur; /* temp cursor */
61a25848 674 xfs_inobt_rec_incore_t trec; /* temp inode allocation record */
1da177e4
LT
675
676
677 if (*IO_agbp == NULL) {
678 /*
679 * We do not have an agbp, so select an initial allocation
680 * group for inode allocation.
681 */
682 agbp = xfs_ialloc_ag_select(tp, parent, mode, okalloc);
683 /*
684 * Couldn't find an allocation group satisfying the
685 * criteria, give up.
686 */
687 if (!agbp) {
688 *inop = NULLFSINO;
689 return 0;
690 }
691 agi = XFS_BUF_TO_AGI(agbp);
16259e7d 692 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
1da177e4
LT
693 } else {
694 /*
695 * Continue where we left off before. In this case, we
696 * know that the allocation group has free inodes.
697 */
698 agbp = *IO_agbp;
699 agi = XFS_BUF_TO_AGI(agbp);
16259e7d
CH
700 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
701 ASSERT(be32_to_cpu(agi->agi_freecount) > 0);
1da177e4
LT
702 }
703 mp = tp->t_mountp;
704 agcount = mp->m_sb.sb_agcount;
16259e7d 705 agno = be32_to_cpu(agi->agi_seqno);
1da177e4
LT
706 tagno = agno;
707 pagno = XFS_INO_TO_AGNO(mp, parent);
708 pagino = XFS_INO_TO_AGINO(mp, parent);
709
710 /*
711 * If we have already hit the ceiling of inode blocks then clear
712 * okalloc so we scan all available agi structures for a free
713 * inode.
714 */
715
716 if (mp->m_maxicount &&
717 mp->m_sb.sb_icount + XFS_IALLOC_INODES(mp) > mp->m_maxicount) {
718 noroom = 1;
719 okalloc = 0;
720 }
721
722 /*
723 * Loop until we find an allocation group that either has free inodes
724 * or in which we can allocate some inodes. Iterate through the
725 * allocation groups upward, wrapping at the end.
726 */
727 *alloc_done = B_FALSE;
728 while (!agi->agi_freecount) {
729 /*
730 * Don't do anything if we're not supposed to allocate
731 * any blocks, just go on to the next ag.
732 */
733 if (okalloc) {
734 /*
735 * Try to allocate some new inodes in the allocation
736 * group.
737 */
738 if ((error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced))) {
739 xfs_trans_brelse(tp, agbp);
740 if (error == ENOSPC) {
741 *inop = NULLFSINO;
742 return 0;
743 } else
744 return error;
745 }
746 if (ialloced) {
747 /*
748 * We successfully allocated some inodes, return
749 * the current context to the caller so that it
750 * can commit the current transaction and call
751 * us again where we left off.
752 */
16259e7d 753 ASSERT(be32_to_cpu(agi->agi_freecount) > 0);
1da177e4
LT
754 *alloc_done = B_TRUE;
755 *IO_agbp = agbp;
756 *inop = NULLFSINO;
757 return 0;
758 }
759 }
760 /*
761 * If it failed, give up on this ag.
762 */
763 xfs_trans_brelse(tp, agbp);
764 /*
765 * Go on to the next ag: get its ag header.
766 */
767nextag:
768 if (++tagno == agcount)
769 tagno = 0;
770 if (tagno == agno) {
771 *inop = NULLFSINO;
772 return noroom ? ENOSPC : 0;
773 }
774 down_read(&mp->m_peraglock);
775 if (mp->m_perag[tagno].pagi_inodeok == 0) {
776 up_read(&mp->m_peraglock);
777 goto nextag;
778 }
779 error = xfs_ialloc_read_agi(mp, tp, tagno, &agbp);
780 up_read(&mp->m_peraglock);
781 if (error)
782 goto nextag;
783 agi = XFS_BUF_TO_AGI(agbp);
16259e7d 784 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
1da177e4
LT
785 }
786 /*
787 * Here with an allocation group that has a free inode.
788 * Reset agno since we may have chosen a new ag in the
789 * loop above.
790 */
791 agno = tagno;
792 *IO_agbp = NULL;
bd169565
DC
793
794 restart_pagno:
561f7d17 795 cur = xfs_inobt_init_cursor(mp, tp, agbp, be32_to_cpu(agi->agi_seqno));
1da177e4
LT
796 /*
797 * If pagino is 0 (this is the root inode allocation) use newino.
798 * This must work because we've just allocated some.
799 */
800 if (!pagino)
16259e7d 801 pagino = be32_to_cpu(agi->agi_newino);
1da177e4 802
0b48db80
DC
803 error = xfs_check_agi_freecount(cur, agi);
804 if (error)
805 goto error0;
1da177e4 806
1da177e4 807 /*
4254b0bb 808 * If in the same AG as the parent, try to get near the parent.
1da177e4
LT
809 */
810 if (pagno == agno) {
bd169565 811 xfs_perag_t *pag = &mp->m_perag[agno];
4254b0bb
CH
812 int doneleft; /* done, to the left */
813 int doneright; /* done, to the right */
bd169565 814 int searchdistance = 10;
4254b0bb 815
21875505 816 error = xfs_inobt_lookup(cur, pagino, XFS_LOOKUP_LE, &i);
4254b0bb 817 if (error)
1da177e4 818 goto error0;
4254b0bb
CH
819 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
820
821 error = xfs_inobt_get_rec(cur, &rec, &j);
822 if (error)
823 goto error0;
824 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
825
826 if (rec.ir_freecount > 0) {
1da177e4
LT
827 /*
828 * Found a free inode in the same chunk
4254b0bb 829 * as the parent, done.
1da177e4 830 */
4254b0bb 831 goto alloc_inode;
1da177e4 832 }
4254b0bb
CH
833
834
1da177e4 835 /*
4254b0bb 836 * In the same AG as parent, but parent's chunk is full.
1da177e4 837 */
1da177e4 838
4254b0bb
CH
839 /* duplicate the cursor, search left & right simultaneously */
840 error = xfs_btree_dup_cursor(cur, &tcur);
841 if (error)
842 goto error0;
843
bd169565
DC
844 /*
845 * Skip to last blocks looked up if same parent inode.
846 */
847 if (pagino != NULLAGINO &&
848 pag->pagl_pagino == pagino &&
849 pag->pagl_leftrec != NULLAGINO &&
850 pag->pagl_rightrec != NULLAGINO) {
851 error = xfs_ialloc_get_rec(tcur, pag->pagl_leftrec,
852 &trec, &doneleft, 1);
853 if (error)
854 goto error1;
4254b0bb 855
bd169565
DC
856 error = xfs_ialloc_get_rec(cur, pag->pagl_rightrec,
857 &rec, &doneright, 0);
858 if (error)
859 goto error1;
860 } else {
861 /* search left with tcur, back up 1 record */
862 error = xfs_ialloc_next_rec(tcur, &trec, &doneleft, 1);
863 if (error)
864 goto error1;
865
866 /* search right with cur, go forward 1 record. */
867 error = xfs_ialloc_next_rec(cur, &rec, &doneright, 0);
868 if (error)
869 goto error1;
870 }
4254b0bb
CH
871
872 /*
873 * Loop until we find an inode chunk with a free inode.
874 */
875 while (!doneleft || !doneright) {
876 int useleft; /* using left inode chunk this time */
877
bd169565
DC
878 if (!--searchdistance) {
879 /*
880 * Not in range - save last search
881 * location and allocate a new inode
882 */
3b826386 883 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
bd169565
DC
884 pag->pagl_leftrec = trec.ir_startino;
885 pag->pagl_rightrec = rec.ir_startino;
886 pag->pagl_pagino = pagino;
887 goto newino;
888 }
889
4254b0bb
CH
890 /* figure out the closer block if both are valid. */
891 if (!doneleft && !doneright) {
892 useleft = pagino -
893 (trec.ir_startino + XFS_INODES_PER_CHUNK - 1) <
894 rec.ir_startino - pagino;
895 } else {
896 useleft = !doneleft;
1da177e4 897 }
4254b0bb
CH
898
899 /* free inodes to the left? */
900 if (useleft && trec.ir_freecount) {
901 rec = trec;
902 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
903 cur = tcur;
bd169565
DC
904
905 pag->pagl_leftrec = trec.ir_startino;
906 pag->pagl_rightrec = rec.ir_startino;
907 pag->pagl_pagino = pagino;
4254b0bb 908 goto alloc_inode;
1da177e4 909 }
1da177e4 910
4254b0bb
CH
911 /* free inodes to the right? */
912 if (!useleft && rec.ir_freecount) {
913 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
bd169565
DC
914
915 pag->pagl_leftrec = trec.ir_startino;
916 pag->pagl_rightrec = rec.ir_startino;
917 pag->pagl_pagino = pagino;
4254b0bb 918 goto alloc_inode;
1da177e4 919 }
4254b0bb
CH
920
921 /* get next record to check */
922 if (useleft) {
923 error = xfs_ialloc_next_rec(tcur, &trec,
924 &doneleft, 1);
925 } else {
926 error = xfs_ialloc_next_rec(cur, &rec,
927 &doneright, 0);
928 }
929 if (error)
930 goto error1;
1da177e4 931 }
bd169565
DC
932
933 /*
934 * We've reached the end of the btree. because
935 * we are only searching a small chunk of the
936 * btree each search, there is obviously free
937 * inodes closer to the parent inode than we
938 * are now. restart the search again.
939 */
940 pag->pagl_pagino = NULLAGINO;
941 pag->pagl_leftrec = NULLAGINO;
942 pag->pagl_rightrec = NULLAGINO;
943 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
944 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
945 goto restart_pagno;
1da177e4 946 }
4254b0bb 947
1da177e4 948 /*
4254b0bb 949 * In a different AG from the parent.
1da177e4
LT
950 * See if the most recently allocated block has any free.
951 */
bd169565
DC
952newino:
953 if (be32_to_cpu(agi->agi_newino) != NULLAGINO) {
21875505
CH
954 error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino),
955 XFS_LOOKUP_EQ, &i);
4254b0bb 956 if (error)
1da177e4 957 goto error0;
4254b0bb
CH
958
959 if (i == 1) {
960 error = xfs_inobt_get_rec(cur, &rec, &j);
961 if (error)
962 goto error0;
963
964 if (j == 1 && rec.ir_freecount > 0) {
965 /*
966 * The last chunk allocated in the group
967 * still has a free inode.
968 */
969 goto alloc_inode;
970 }
1da177e4 971 }
bd169565 972 }
4254b0bb 973
bd169565
DC
974 /*
975 * None left in the last group, search the whole AG
976 */
977 error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
978 if (error)
979 goto error0;
980 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
981
982 for (;;) {
983 error = xfs_inobt_get_rec(cur, &rec, &i);
984 if (error)
985 goto error0;
986 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
987 if (rec.ir_freecount > 0)
988 break;
989 error = xfs_btree_increment(cur, 0, &i);
4254b0bb
CH
990 if (error)
991 goto error0;
992 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1da177e4 993 }
4254b0bb
CH
994
995alloc_inode:
9d87c319 996 offset = xfs_ialloc_find_free(&rec.ir_free);
1da177e4
LT
997 ASSERT(offset >= 0);
998 ASSERT(offset < XFS_INODES_PER_CHUNK);
999 ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
1000 XFS_INODES_PER_CHUNK) == 0);
1001 ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
0d87e656 1002 rec.ir_free &= ~XFS_INOBT_MASK(offset);
1da177e4 1003 rec.ir_freecount--;
afabc24a
CH
1004 error = xfs_inobt_update(cur, &rec);
1005 if (error)
1da177e4 1006 goto error0;
413d57c9 1007 be32_add_cpu(&agi->agi_freecount, -1);
1da177e4
LT
1008 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
1009 down_read(&mp->m_peraglock);
1010 mp->m_perag[tagno].pagi_freecount--;
1011 up_read(&mp->m_peraglock);
1da177e4 1012
0b48db80
DC
1013 error = xfs_check_agi_freecount(cur, agi);
1014 if (error)
1015 goto error0;
1016
1da177e4
LT
1017 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1018 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
1019 *inop = ino;
1020 return 0;
1021error1:
1022 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
1023error0:
1024 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1025 return error;
1026}
1027
1028/*
1029 * Free disk inode. Carefully avoids touching the incore inode, all
1030 * manipulations incore are the caller's responsibility.
1031 * The on-disk inode is not changed by this operation, only the
1032 * btree (free inode mask) is changed.
1033 */
1034int
1035xfs_difree(
1036 xfs_trans_t *tp, /* transaction pointer */
1037 xfs_ino_t inode, /* inode to be freed */
1038 xfs_bmap_free_t *flist, /* extents to free */
1039 int *delete, /* set if inode cluster was deleted */
1040 xfs_ino_t *first_ino) /* first inode in deleted cluster */
1041{
1042 /* REFERENCED */
1043 xfs_agblock_t agbno; /* block number containing inode */
1044 xfs_buf_t *agbp; /* buffer containing allocation group header */
1045 xfs_agino_t agino; /* inode number relative to allocation group */
1046 xfs_agnumber_t agno; /* allocation group number */
1047 xfs_agi_t *agi; /* allocation group header */
1048 xfs_btree_cur_t *cur; /* inode btree cursor */
1049 int error; /* error return value */
1050 int i; /* result code */
1051 int ilen; /* inodes in an inode cluster */
1052 xfs_mount_t *mp; /* mount structure for filesystem */
1053 int off; /* offset of inode in inode chunk */
61a25848 1054 xfs_inobt_rec_incore_t rec; /* btree record */
1da177e4
LT
1055
1056 mp = tp->t_mountp;
1057
1058 /*
1059 * Break up inode number into its components.
1060 */
1061 agno = XFS_INO_TO_AGNO(mp, inode);
1062 if (agno >= mp->m_sb.sb_agcount) {
1063 cmn_err(CE_WARN,
1064 "xfs_difree: agno >= mp->m_sb.sb_agcount (%d >= %d) on %s. Returning EINVAL.",
1065 agno, mp->m_sb.sb_agcount, mp->m_fsname);
1066 ASSERT(0);
1067 return XFS_ERROR(EINVAL);
1068 }
1069 agino = XFS_INO_TO_AGINO(mp, inode);
1070 if (inode != XFS_AGINO_TO_INO(mp, agno, agino)) {
1071 cmn_err(CE_WARN,
da1650a5
CH
1072 "xfs_difree: inode != XFS_AGINO_TO_INO() "
1073 "(%llu != %llu) on %s. Returning EINVAL.",
1074 (unsigned long long)inode,
1075 (unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino),
1076 mp->m_fsname);
1da177e4
LT
1077 ASSERT(0);
1078 return XFS_ERROR(EINVAL);
1079 }
1080 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1081 if (agbno >= mp->m_sb.sb_agblocks) {
1082 cmn_err(CE_WARN,
1083 "xfs_difree: agbno >= mp->m_sb.sb_agblocks (%d >= %d) on %s. Returning EINVAL.",
1084 agbno, mp->m_sb.sb_agblocks, mp->m_fsname);
1085 ASSERT(0);
1086 return XFS_ERROR(EINVAL);
1087 }
1088 /*
1089 * Get the allocation group header.
1090 */
1091 down_read(&mp->m_peraglock);
1092 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1093 up_read(&mp->m_peraglock);
1094 if (error) {
1095 cmn_err(CE_WARN,
1096 "xfs_difree: xfs_ialloc_read_agi() returned an error %d on %s. Returning error.",
1097 error, mp->m_fsname);
1098 return error;
1099 }
1100 agi = XFS_BUF_TO_AGI(agbp);
16259e7d
CH
1101 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
1102 ASSERT(agbno < be32_to_cpu(agi->agi_length));
1da177e4
LT
1103 /*
1104 * Initialize the cursor.
1105 */
561f7d17 1106 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno);
1da177e4 1107
0b48db80
DC
1108 error = xfs_check_agi_freecount(cur, agi);
1109 if (error)
1110 goto error0;
1111
1da177e4
LT
1112 /*
1113 * Look for the entry describing this inode.
1114 */
21875505 1115 if ((error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i))) {
1da177e4 1116 cmn_err(CE_WARN,
21875505 1117 "xfs_difree: xfs_inobt_lookup returned() an error %d on %s. Returning error.",
1da177e4
LT
1118 error, mp->m_fsname);
1119 goto error0;
1120 }
1121 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
2e287a73
CH
1122 error = xfs_inobt_get_rec(cur, &rec, &i);
1123 if (error) {
1da177e4
LT
1124 cmn_err(CE_WARN,
1125 "xfs_difree: xfs_inobt_get_rec() returned an error %d on %s. Returning error.",
1126 error, mp->m_fsname);
1127 goto error0;
1128 }
1129 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1130 /*
1131 * Get the offset in the inode chunk.
1132 */
1133 off = agino - rec.ir_startino;
1134 ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK);
0d87e656 1135 ASSERT(!(rec.ir_free & XFS_INOBT_MASK(off)));
1da177e4
LT
1136 /*
1137 * Mark the inode free & increment the count.
1138 */
0d87e656 1139 rec.ir_free |= XFS_INOBT_MASK(off);
1da177e4
LT
1140 rec.ir_freecount++;
1141
1142 /*
c41564b5 1143 * When an inode cluster is free, it becomes eligible for removal
1da177e4 1144 */
1bd960ee 1145 if (!(mp->m_flags & XFS_MOUNT_IKEEP) &&
1da177e4
LT
1146 (rec.ir_freecount == XFS_IALLOC_INODES(mp))) {
1147
1148 *delete = 1;
1149 *first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino);
1150
1151 /*
1152 * Remove the inode cluster from the AGI B+Tree, adjust the
1153 * AGI and Superblock inode counts, and mark the disk space
1154 * to be freed when the transaction is committed.
1155 */
1156 ilen = XFS_IALLOC_INODES(mp);
413d57c9
MS
1157 be32_add_cpu(&agi->agi_count, -ilen);
1158 be32_add_cpu(&agi->agi_freecount, -(ilen - 1));
1da177e4
LT
1159 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT);
1160 down_read(&mp->m_peraglock);
1161 mp->m_perag[agno].pagi_freecount -= ilen - 1;
1162 up_read(&mp->m_peraglock);
1163 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen);
1164 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1));
1165
91cca5df
CH
1166 if ((error = xfs_btree_delete(cur, &i))) {
1167 cmn_err(CE_WARN, "xfs_difree: xfs_btree_delete returned an error %d on %s.\n",
1da177e4
LT
1168 error, mp->m_fsname);
1169 goto error0;
1170 }
1171
1172 xfs_bmap_add_free(XFS_AGB_TO_FSB(mp,
1173 agno, XFS_INO_TO_AGBNO(mp,rec.ir_startino)),
1174 XFS_IALLOC_BLOCKS(mp), flist, mp);
1175 } else {
1176 *delete = 0;
1177
afabc24a
CH
1178 error = xfs_inobt_update(cur, &rec);
1179 if (error) {
1da177e4 1180 cmn_err(CE_WARN,
afabc24a 1181 "xfs_difree: xfs_inobt_update returned an error %d on %s.",
1da177e4
LT
1182 error, mp->m_fsname);
1183 goto error0;
1184 }
afabc24a 1185
1da177e4
LT
1186 /*
1187 * Change the inode free counts and log the ag/sb changes.
1188 */
413d57c9 1189 be32_add_cpu(&agi->agi_freecount, 1);
1da177e4
LT
1190 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
1191 down_read(&mp->m_peraglock);
1192 mp->m_perag[agno].pagi_freecount++;
1193 up_read(&mp->m_peraglock);
1194 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1);
1195 }
1196
0b48db80
DC
1197 error = xfs_check_agi_freecount(cur, agi);
1198 if (error)
1199 goto error0;
1da177e4 1200
1da177e4
LT
1201 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1202 return 0;
1203
1204error0:
1205 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1206 return error;
1207}
1208
1209/*
94e1b69d 1210 * Return the location of the inode in imap, for mapping it into a buffer.
1da177e4 1211 */
1da177e4 1212int
94e1b69d
CH
1213xfs_imap(
1214 xfs_mount_t *mp, /* file system mount structure */
1215 xfs_trans_t *tp, /* transaction pointer */
1da177e4 1216 xfs_ino_t ino, /* inode to locate */
94e1b69d
CH
1217 struct xfs_imap *imap, /* location map structure */
1218 uint flags) /* flags for inode btree lookup */
1da177e4
LT
1219{
1220 xfs_agblock_t agbno; /* block number of inode in the alloc group */
1da177e4
LT
1221 xfs_agino_t agino; /* inode number within alloc group */
1222 xfs_agnumber_t agno; /* allocation group number */
1223 int blks_per_cluster; /* num blocks per inode cluster */
1224 xfs_agblock_t chunk_agbno; /* first block in inode chunk */
1da177e4 1225 xfs_agblock_t cluster_agbno; /* first block in inode cluster */
1da177e4 1226 int error; /* error code */
1da177e4
LT
1227 int offset; /* index of inode in its buffer */
1228 int offset_agbno; /* blks from chunk start to inode */
1229
1230 ASSERT(ino != NULLFSINO);
94e1b69d 1231
1da177e4
LT
1232 /*
1233 * Split up the inode number into its parts.
1234 */
1235 agno = XFS_INO_TO_AGNO(mp, ino);
1236 agino = XFS_INO_TO_AGINO(mp, ino);
1237 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1238 if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks ||
1239 ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1240#ifdef DEBUG
4d1a2ed3 1241 /* no diagnostics for bulkstat, ino comes from userspace */
b48d8d64 1242 if (flags & XFS_IGET_BULKSTAT)
4d1a2ed3 1243 return XFS_ERROR(EINVAL);
1da177e4
LT
1244 if (agno >= mp->m_sb.sb_agcount) {
1245 xfs_fs_cmn_err(CE_ALERT, mp,
94e1b69d 1246 "xfs_imap: agno (%d) >= "
1da177e4
LT
1247 "mp->m_sb.sb_agcount (%d)",
1248 agno, mp->m_sb.sb_agcount);
1249 }
1250 if (agbno >= mp->m_sb.sb_agblocks) {
1251 xfs_fs_cmn_err(CE_ALERT, mp,
94e1b69d 1252 "xfs_imap: agbno (0x%llx) >= "
1da177e4
LT
1253 "mp->m_sb.sb_agblocks (0x%lx)",
1254 (unsigned long long) agbno,
1255 (unsigned long) mp->m_sb.sb_agblocks);
1256 }
1257 if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1258 xfs_fs_cmn_err(CE_ALERT, mp,
94e1b69d 1259 "xfs_imap: ino (0x%llx) != "
1da177e4
LT
1260 "XFS_AGINO_TO_INO(mp, agno, agino) "
1261 "(0x%llx)",
1262 ino, XFS_AGINO_TO_INO(mp, agno, agino));
1263 }
745b1f47 1264 xfs_stack_trace();
1da177e4
LT
1265#endif /* DEBUG */
1266 return XFS_ERROR(EINVAL);
1267 }
94e1b69d
CH
1268
1269 /*
1270 * If the inode cluster size is the same as the blocksize or
1271 * smaller we get to the buffer by simple arithmetics.
1272 */
1273 if (XFS_INODE_CLUSTER_SIZE(mp) <= mp->m_sb.sb_blocksize) {
1da177e4
LT
1274 offset = XFS_INO_TO_OFFSET(mp, ino);
1275 ASSERT(offset < mp->m_sb.sb_inopblock);
94e1b69d
CH
1276
1277 imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, agbno);
1278 imap->im_len = XFS_FSB_TO_BB(mp, 1);
1279 imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
1da177e4
LT
1280 return 0;
1281 }
94e1b69d 1282
1da177e4 1283 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_blocklog;
94e1b69d
CH
1284
1285 /*
1286 * If we get a block number passed from bulkstat we can use it to
1287 * find the buffer easily.
1288 */
1289 if (imap->im_blkno) {
1da177e4
LT
1290 offset = XFS_INO_TO_OFFSET(mp, ino);
1291 ASSERT(offset < mp->m_sb.sb_inopblock);
94e1b69d 1292
9d87c319 1293 cluster_agbno = xfs_daddr_to_agbno(mp, imap->im_blkno);
94e1b69d
CH
1294 offset += (agbno - cluster_agbno) * mp->m_sb.sb_inopblock;
1295
1296 imap->im_len = XFS_FSB_TO_BB(mp, blks_per_cluster);
1297 imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
1da177e4
LT
1298 return 0;
1299 }
94e1b69d
CH
1300
1301 /*
1302 * If the inode chunks are aligned then use simple maths to
1303 * find the location. Otherwise we have to do a btree
1304 * lookup to find the location.
1305 */
1da177e4
LT
1306 if (mp->m_inoalign_mask) {
1307 offset_agbno = agbno & mp->m_inoalign_mask;
1308 chunk_agbno = agbno - offset_agbno;
1309 } else {
94e1b69d 1310 xfs_btree_cur_t *cur; /* inode btree cursor */
2e287a73 1311 xfs_inobt_rec_incore_t chunk_rec;
94e1b69d
CH
1312 xfs_buf_t *agbp; /* agi buffer */
1313 int i; /* temp state */
1314
1da177e4
LT
1315 down_read(&mp->m_peraglock);
1316 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1317 up_read(&mp->m_peraglock);
1318 if (error) {
94e1b69d 1319 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: "
1da177e4
LT
1320 "xfs_ialloc_read_agi() returned "
1321 "error %d, agno %d",
1322 error, agno);
1da177e4
LT
1323 return error;
1324 }
94e1b69d 1325
561f7d17 1326 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno);
21875505 1327 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i);
94e1b69d
CH
1328 if (error) {
1329 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: "
21875505 1330 "xfs_inobt_lookup() failed");
1da177e4
LT
1331 goto error0;
1332 }
94e1b69d 1333
2e287a73 1334 error = xfs_inobt_get_rec(cur, &chunk_rec, &i);
94e1b69d
CH
1335 if (error) {
1336 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: "
1da177e4 1337 "xfs_inobt_get_rec() failed");
1da177e4
LT
1338 goto error0;
1339 }
1340 if (i == 0) {
1341#ifdef DEBUG
94e1b69d 1342 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: "
1da177e4
LT
1343 "xfs_inobt_get_rec() failed");
1344#endif /* DEBUG */
1345 error = XFS_ERROR(EINVAL);
1346 }
94e1b69d 1347 error0:
1da177e4
LT
1348 xfs_trans_brelse(tp, agbp);
1349 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1350 if (error)
1351 return error;
2e287a73 1352 chunk_agbno = XFS_AGINO_TO_AGBNO(mp, chunk_rec.ir_startino);
1da177e4
LT
1353 offset_agbno = agbno - chunk_agbno;
1354 }
94e1b69d 1355
1da177e4
LT
1356 ASSERT(agbno >= chunk_agbno);
1357 cluster_agbno = chunk_agbno +
1358 ((offset_agbno / blks_per_cluster) * blks_per_cluster);
1359 offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
1360 XFS_INO_TO_OFFSET(mp, ino);
94e1b69d
CH
1361
1362 imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, cluster_agbno);
1363 imap->im_len = XFS_FSB_TO_BB(mp, blks_per_cluster);
1364 imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
1365
1366 /*
1367 * If the inode number maps to a block outside the bounds
1368 * of the file system then return NULL rather than calling
1369 * read_buf and panicing when we get an error from the
1370 * driver.
1371 */
1372 if ((imap->im_blkno + imap->im_len) >
1373 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) {
1374 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: "
1375 "(imap->im_blkno (0x%llx) + imap->im_len (0x%llx)) > "
1376 " XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks) (0x%llx)",
1377 (unsigned long long) imap->im_blkno,
1378 (unsigned long long) imap->im_len,
1379 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks));
1380 return XFS_ERROR(EINVAL);
1381 }
1382
1da177e4 1383 return 0;
1da177e4
LT
1384}
1385
1386/*
1387 * Compute and fill in value of m_in_maxlevels.
1388 */
1389void
1390xfs_ialloc_compute_maxlevels(
1391 xfs_mount_t *mp) /* file system mount structure */
1392{
1393 int level;
1394 uint maxblocks;
1395 uint maxleafents;
1396 int minleafrecs;
1397 int minnoderecs;
1398
1399 maxleafents = (1LL << XFS_INO_AGINO_BITS(mp)) >>
1400 XFS_INODES_PER_CHUNK_LOG;
1401 minleafrecs = mp->m_alloc_mnr[0];
1402 minnoderecs = mp->m_alloc_mnr[1];
1403 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
1404 for (level = 1; maxblocks > 1; level++)
1405 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
1406 mp->m_in_maxlevels = level;
1407}
1408
1409/*
1410 * Log specified fields for the ag hdr (inode section)
1411 */
1412void
1413xfs_ialloc_log_agi(
1414 xfs_trans_t *tp, /* transaction pointer */
1415 xfs_buf_t *bp, /* allocation group header buffer */
1416 int fields) /* bitmask of fields to log */
1417{
1418 int first; /* first byte number */
1419 int last; /* last byte number */
1420 static const short offsets[] = { /* field starting offsets */
1421 /* keep in sync with bit definitions */
1422 offsetof(xfs_agi_t, agi_magicnum),
1423 offsetof(xfs_agi_t, agi_versionnum),
1424 offsetof(xfs_agi_t, agi_seqno),
1425 offsetof(xfs_agi_t, agi_length),
1426 offsetof(xfs_agi_t, agi_count),
1427 offsetof(xfs_agi_t, agi_root),
1428 offsetof(xfs_agi_t, agi_level),
1429 offsetof(xfs_agi_t, agi_freecount),
1430 offsetof(xfs_agi_t, agi_newino),
1431 offsetof(xfs_agi_t, agi_dirino),
1432 offsetof(xfs_agi_t, agi_unlinked),
1433 sizeof(xfs_agi_t)
1434 };
1435#ifdef DEBUG
1436 xfs_agi_t *agi; /* allocation group header */
1437
1438 agi = XFS_BUF_TO_AGI(bp);
16259e7d 1439 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
1da177e4
LT
1440#endif
1441 /*
1442 * Compute byte offsets for the first and last fields.
1443 */
1444 xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS, &first, &last);
1445 /*
1446 * Log the allocation group inode header buffer.
1447 */
1448 xfs_trans_log_buf(tp, bp, first, last);
1449}
1450
5e1be0fb
CH
1451#ifdef DEBUG
1452STATIC void
1453xfs_check_agi_unlinked(
1454 struct xfs_agi *agi)
1455{
1456 int i;
1457
1458 for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++)
1459 ASSERT(agi->agi_unlinked[i]);
1460}
1461#else
1462#define xfs_check_agi_unlinked(agi)
1463#endif
1464
1da177e4
LT
1465/*
1466 * Read in the allocation group header (inode allocation section)
1467 */
1468int
5e1be0fb
CH
1469xfs_read_agi(
1470 struct xfs_mount *mp, /* file system mount structure */
1471 struct xfs_trans *tp, /* transaction pointer */
1472 xfs_agnumber_t agno, /* allocation group number */
1473 struct xfs_buf **bpp) /* allocation group hdr buf */
1da177e4 1474{
5e1be0fb
CH
1475 struct xfs_agi *agi; /* allocation group header */
1476 int agi_ok; /* agi is consistent */
1477 int error;
1da177e4
LT
1478
1479 ASSERT(agno != NULLAGNUMBER);
5e1be0fb
CH
1480
1481 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
1da177e4 1482 XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
5e1be0fb 1483 XFS_FSS_TO_BB(mp, 1), 0, bpp);
1da177e4
LT
1484 if (error)
1485 return error;
5e1be0fb
CH
1486
1487 ASSERT(*bpp && !XFS_BUF_GETERROR(*bpp));
1488 agi = XFS_BUF_TO_AGI(*bpp);
1da177e4
LT
1489
1490 /*
1491 * Validate the magic number of the agi block.
1492 */
5e1be0fb
CH
1493 agi_ok = be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC &&
1494 XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum)) &&
1495 be32_to_cpu(agi->agi_seqno) == agno;
1da177e4
LT
1496 if (unlikely(XFS_TEST_ERROR(!agi_ok, mp, XFS_ERRTAG_IALLOC_READ_AGI,
1497 XFS_RANDOM_IALLOC_READ_AGI))) {
5e1be0fb 1498 XFS_CORRUPTION_ERROR("xfs_read_agi", XFS_ERRLEVEL_LOW,
1da177e4 1499 mp, agi);
5e1be0fb 1500 xfs_trans_brelse(tp, *bpp);
1da177e4
LT
1501 return XFS_ERROR(EFSCORRUPTED);
1502 }
5e1be0fb
CH
1503
1504 XFS_BUF_SET_VTYPE_REF(*bpp, B_FS_AGI, XFS_AGI_REF);
1505
1506 xfs_check_agi_unlinked(agi);
1507 return 0;
1508}
1509
1510int
1511xfs_ialloc_read_agi(
1512 struct xfs_mount *mp, /* file system mount structure */
1513 struct xfs_trans *tp, /* transaction pointer */
1514 xfs_agnumber_t agno, /* allocation group number */
1515 struct xfs_buf **bpp) /* allocation group hdr buf */
1516{
1517 struct xfs_agi *agi; /* allocation group header */
1518 struct xfs_perag *pag; /* per allocation group data */
1519 int error;
1520
1521 error = xfs_read_agi(mp, tp, agno, bpp);
1522 if (error)
1523 return error;
1524
1525 agi = XFS_BUF_TO_AGI(*bpp);
1da177e4 1526 pag = &mp->m_perag[agno];
5e1be0fb 1527
1da177e4 1528 if (!pag->pagi_init) {
16259e7d 1529 pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
92821e2b 1530 pag->pagi_count = be32_to_cpu(agi->agi_count);
1da177e4 1531 pag->pagi_init = 1;
1da177e4 1532 }
1da177e4 1533
5e1be0fb
CH
1534 /*
1535 * It's possible for these to be out of sync if
1536 * we are in the middle of a forced shutdown.
1537 */
1538 ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) ||
1539 XFS_FORCED_SHUTDOWN(mp));
1da177e4
LT
1540 return 0;
1541}
92821e2b
DC
1542
1543/*
1544 * Read in the agi to initialise the per-ag data in the mount structure
1545 */
1546int
1547xfs_ialloc_pagi_init(
1548 xfs_mount_t *mp, /* file system mount structure */
1549 xfs_trans_t *tp, /* transaction pointer */
1550 xfs_agnumber_t agno) /* allocation group number */
1551{
1552 xfs_buf_t *bp = NULL;
1553 int error;
1554
1555 error = xfs_ialloc_read_agi(mp, tp, agno, &bp);
1556 if (error)
1557 return error;
1558 if (bp)
1559 xfs_trans_brelse(tp, bp);
1560 return 0;
1561}