]> git.karo-electronics.de Git - karo-tx-linux.git/blob - fs/xfs/xfs_ialloc.c
[XFS] Remove xfs_macros.c, xfs_macros.h, rework headers a whole lot.
[karo-tx-linux.git] / fs / xfs / xfs_ialloc.c
1 /*
2  * Copyright (c) 2000-2002 Silicon Graphics, Inc.  All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of version 2 of the GNU General Public License as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it would be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  *
12  * Further, this software is distributed without any warranty that it is
13  * free of the rightful claim of any third person regarding infringement
14  * or the like.  Any license provided herein, whether implied or
15  * otherwise, applies only to this software file.  Patent licenses, if
16  * any, provided herein do not apply to combinations of this program with
17  * other software, or any other product whatsoever.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write the Free Software Foundation, Inc., 59
21  * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22  *
23  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24  * Mountain View, CA  94043, or:
25  *
26  * http://www.sgi.com
27  *
28  * For further information regarding this notice, see:
29  *
30  * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31  */
32 #include "xfs.h"
33 #include "xfs_fs.h"
34 #include "xfs_types.h"
35 #include "xfs_bit.h"
36 #include "xfs_log.h"
37 #include "xfs_inum.h"
38 #include "xfs_trans.h"
39 #include "xfs_sb.h"
40 #include "xfs_ag.h"
41 #include "xfs_dir.h"
42 #include "xfs_dir2.h"
43 #include "xfs_dmapi.h"
44 #include "xfs_mount.h"
45 #include "xfs_bmap_btree.h"
46 #include "xfs_alloc_btree.h"
47 #include "xfs_ialloc_btree.h"
48 #include "xfs_dir_sf.h"
49 #include "xfs_dir2_sf.h"
50 #include "xfs_attr_sf.h"
51 #include "xfs_dinode.h"
52 #include "xfs_inode.h"
53 #include "xfs_btree.h"
54 #include "xfs_ialloc.h"
55 #include "xfs_alloc.h"
56 #include "xfs_rtalloc.h"
57 #include "xfs_error.h"
58 #include "xfs_bmap.h"
59
60 /*
61  * Log specified fields for the inode given by bp and off.
62  */
63 STATIC void
64 xfs_ialloc_log_di(
65         xfs_trans_t     *tp,            /* transaction pointer */
66         xfs_buf_t       *bp,            /* inode buffer */
67         int             off,            /* index of inode in buffer */
68         int             fields)         /* bitmask of fields to log */
69 {
70         int                     first;          /* first byte number */
71         int                     ioffset;        /* off in bytes */
72         int                     last;           /* last byte number */
73         xfs_mount_t             *mp;            /* mount point structure */
74         static const short      offsets[] = {   /* field offsets */
75                                                 /* keep in sync with bits */
76                 offsetof(xfs_dinode_core_t, di_magic),
77                 offsetof(xfs_dinode_core_t, di_mode),
78                 offsetof(xfs_dinode_core_t, di_version),
79                 offsetof(xfs_dinode_core_t, di_format),
80                 offsetof(xfs_dinode_core_t, di_onlink),
81                 offsetof(xfs_dinode_core_t, di_uid),
82                 offsetof(xfs_dinode_core_t, di_gid),
83                 offsetof(xfs_dinode_core_t, di_nlink),
84                 offsetof(xfs_dinode_core_t, di_projid),
85                 offsetof(xfs_dinode_core_t, di_pad),
86                 offsetof(xfs_dinode_core_t, di_atime),
87                 offsetof(xfs_dinode_core_t, di_mtime),
88                 offsetof(xfs_dinode_core_t, di_ctime),
89                 offsetof(xfs_dinode_core_t, di_size),
90                 offsetof(xfs_dinode_core_t, di_nblocks),
91                 offsetof(xfs_dinode_core_t, di_extsize),
92                 offsetof(xfs_dinode_core_t, di_nextents),
93                 offsetof(xfs_dinode_core_t, di_anextents),
94                 offsetof(xfs_dinode_core_t, di_forkoff),
95                 offsetof(xfs_dinode_core_t, di_aformat),
96                 offsetof(xfs_dinode_core_t, di_dmevmask),
97                 offsetof(xfs_dinode_core_t, di_dmstate),
98                 offsetof(xfs_dinode_core_t, di_flags),
99                 offsetof(xfs_dinode_core_t, di_gen),
100                 offsetof(xfs_dinode_t, di_next_unlinked),
101                 offsetof(xfs_dinode_t, di_u),
102                 offsetof(xfs_dinode_t, di_a),
103                 sizeof(xfs_dinode_t)
104         };
105
106
107         ASSERT(offsetof(xfs_dinode_t, di_core) == 0);
108         ASSERT((fields & (XFS_DI_U|XFS_DI_A)) == 0);
109         mp = tp->t_mountp;
110         /*
111          * Get the inode-relative first and last bytes for these fields
112          */
113         xfs_btree_offsets(fields, offsets, XFS_DI_NUM_BITS, &first, &last);
114         /*
115          * Convert to buffer offsets and log it.
116          */
117         ioffset = off << mp->m_sb.sb_inodelog;
118         first += ioffset;
119         last += ioffset;
120         xfs_trans_log_buf(tp, bp, first, last);
121 }
122
123 /*
124  * Allocation group level functions.
125  */
126
127 /*
128  * Allocate new inodes in the allocation group specified by agbp.
129  * Return 0 for success, else error code.
130  */
131 STATIC int                              /* error code or 0 */
132 xfs_ialloc_ag_alloc(
133         xfs_trans_t     *tp,            /* transaction pointer */
134         xfs_buf_t       *agbp,          /* alloc group buffer */
135         int             *alloc)
136 {
137         xfs_agi_t       *agi;           /* allocation group header */
138         xfs_alloc_arg_t args;           /* allocation argument structure */
139         int             blks_per_cluster;  /* fs blocks per inode cluster */
140         xfs_btree_cur_t *cur;           /* inode btree cursor */
141         xfs_daddr_t     d;              /* disk addr of buffer */
142         int             error;
143         xfs_buf_t       *fbuf;          /* new free inodes' buffer */
144         xfs_dinode_t    *free;          /* new free inode structure */
145         int             i;              /* inode counter */
146         int             j;              /* block counter */
147         int             nbufs;          /* num bufs of new inodes */
148         xfs_agino_t     newino;         /* new first inode's number */
149         xfs_agino_t     newlen;         /* new number of inodes */
150         int             ninodes;        /* num inodes per buf */
151         xfs_agino_t     thisino;        /* current inode number, for loop */
152         int             version;        /* inode version number to use */
153         int             isaligned;      /* inode allocation at stripe unit */
154                                         /* boundary */
155         xfs_dinode_core_t dic;          /* a dinode_core to copy to new */
156                                         /* inodes */
157
158         args.tp = tp;
159         args.mp = tp->t_mountp;
160
161         /*
162          * Locking will ensure that we don't have two callers in here
163          * at one time.
164          */
165         newlen = XFS_IALLOC_INODES(args.mp);
166         if (args.mp->m_maxicount &&
167             args.mp->m_sb.sb_icount + newlen > args.mp->m_maxicount)
168                 return XFS_ERROR(ENOSPC);
169         args.minlen = args.maxlen = XFS_IALLOC_BLOCKS(args.mp);
170         /*
171          * Set the alignment for the allocation.
172          * If stripe alignment is turned on then align at stripe unit
173          * boundary.
174          * If the cluster size is smaller than a filesystem block
175          * then we're doing I/O for inodes in filesystem block size pieces,
176          * so don't need alignment anyway.
177          */
178         isaligned = 0;
179         if (args.mp->m_sinoalign) {
180                 ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN));
181                 args.alignment = args.mp->m_dalign;
182                 isaligned = 1;
183         } else if (XFS_SB_VERSION_HASALIGN(&args.mp->m_sb) &&
184             args.mp->m_sb.sb_inoalignmt >=
185             XFS_B_TO_FSBT(args.mp, XFS_INODE_CLUSTER_SIZE(args.mp)))
186                 args.alignment = args.mp->m_sb.sb_inoalignmt;
187         else
188                 args.alignment = 1;
189         agi = XFS_BUF_TO_AGI(agbp);
190         /*
191          * Need to figure out where to allocate the inode blocks.
192          * Ideally they should be spaced out through the a.g.
193          * For now, just allocate blocks up front.
194          */
195         args.agbno = INT_GET(agi->agi_root, ARCH_CONVERT);
196         args.fsbno = XFS_AGB_TO_FSB(args.mp, INT_GET(agi->agi_seqno, ARCH_CONVERT),
197                                     args.agbno);
198         /*
199          * Allocate a fixed-size extent of inodes.
200          */
201         args.type = XFS_ALLOCTYPE_NEAR_BNO;
202         args.mod = args.total = args.wasdel = args.isfl = args.userdata =
203                 args.minalignslop = 0;
204         args.prod = 1;
205         /*
206          * Allow space for the inode btree to split.
207          */
208         args.minleft = XFS_IN_MAXLEVELS(args.mp) - 1;
209         if ((error = xfs_alloc_vextent(&args)))
210                 return error;
211
212         /*
213          * If stripe alignment is turned on, then try again with cluster
214          * alignment.
215          */
216         if (isaligned && args.fsbno == NULLFSBLOCK) {
217                 args.type = XFS_ALLOCTYPE_NEAR_BNO;
218                 args.agbno = INT_GET(agi->agi_root, ARCH_CONVERT);
219                 args.fsbno = XFS_AGB_TO_FSB(args.mp,
220                                 INT_GET(agi->agi_seqno, ARCH_CONVERT), args.agbno);
221                 if (XFS_SB_VERSION_HASALIGN(&args.mp->m_sb) &&
222                         args.mp->m_sb.sb_inoalignmt >=
223                         XFS_B_TO_FSBT(args.mp, XFS_INODE_CLUSTER_SIZE(args.mp)))
224                                 args.alignment = args.mp->m_sb.sb_inoalignmt;
225                 else
226                         args.alignment = 1;
227                 if ((error = xfs_alloc_vextent(&args)))
228                         return error;
229         }
230
231         if (args.fsbno == NULLFSBLOCK) {
232                 *alloc = 0;
233                 return 0;
234         }
235         ASSERT(args.len == args.minlen);
236         /*
237          * Convert the results.
238          */
239         newino = XFS_OFFBNO_TO_AGINO(args.mp, args.agbno, 0);
240         /*
241          * Loop over the new block(s), filling in the inodes.
242          * For small block sizes, manipulate the inodes in buffers
243          * which are multiples of the blocks size.
244          */
245         if (args.mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(args.mp)) {
246                 blks_per_cluster = 1;
247                 nbufs = (int)args.len;
248                 ninodes = args.mp->m_sb.sb_inopblock;
249         } else {
250                 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(args.mp) /
251                                    args.mp->m_sb.sb_blocksize;
252                 nbufs = (int)args.len / blks_per_cluster;
253                 ninodes = blks_per_cluster * args.mp->m_sb.sb_inopblock;
254         }
255         /*
256          * Figure out what version number to use in the inodes we create.
257          * If the superblock version has caught up to the one that supports
258          * the new inode format, then use the new inode version.  Otherwise
259          * use the old version so that old kernels will continue to be
260          * able to use the file system.
261          */
262         if (XFS_SB_VERSION_HASNLINK(&args.mp->m_sb))
263                 version = XFS_DINODE_VERSION_2;
264         else
265                 version = XFS_DINODE_VERSION_1;
266
267         memset(&dic, 0, sizeof(xfs_dinode_core_t));
268         INT_SET(dic.di_magic, ARCH_CONVERT, XFS_DINODE_MAGIC);
269         INT_SET(dic.di_version, ARCH_CONVERT, version);
270
271         for (j = 0; j < nbufs; j++) {
272                 /*
273                  * Get the block.
274                  */
275                 d = XFS_AGB_TO_DADDR(args.mp, INT_GET(agi->agi_seqno, ARCH_CONVERT),
276                                      args.agbno + (j * blks_per_cluster));
277                 fbuf = xfs_trans_get_buf(tp, args.mp->m_ddev_targp, d,
278                                          args.mp->m_bsize * blks_per_cluster,
279                                          XFS_BUF_LOCK);
280                 ASSERT(fbuf);
281                 ASSERT(!XFS_BUF_GETERROR(fbuf));
282                 /*
283                  * Loop over the inodes in this buffer.
284                  */
285
286                 for (i = 0; i < ninodes; i++) {
287                         free = XFS_MAKE_IPTR(args.mp, fbuf, i);
288                         memcpy(&(free->di_core), &dic, sizeof(xfs_dinode_core_t));
289                         INT_SET(free->di_next_unlinked, ARCH_CONVERT, NULLAGINO);
290                         xfs_ialloc_log_di(tp, fbuf, i,
291                                 XFS_DI_CORE_BITS | XFS_DI_NEXT_UNLINKED);
292                 }
293                 xfs_trans_inode_alloc_buf(tp, fbuf);
294         }
295         INT_MOD(agi->agi_count, ARCH_CONVERT, newlen);
296         INT_MOD(agi->agi_freecount, ARCH_CONVERT, newlen);
297         down_read(&args.mp->m_peraglock);
298         args.mp->m_perag[INT_GET(agi->agi_seqno, ARCH_CONVERT)].pagi_freecount += newlen;
299         up_read(&args.mp->m_peraglock);
300         INT_SET(agi->agi_newino, ARCH_CONVERT, newino);
301         /*
302          * Insert records describing the new inode chunk into the btree.
303          */
304         cur = xfs_btree_init_cursor(args.mp, tp, agbp,
305                         INT_GET(agi->agi_seqno, ARCH_CONVERT),
306                         XFS_BTNUM_INO, (xfs_inode_t *)0, 0);
307         for (thisino = newino;
308              thisino < newino + newlen;
309              thisino += XFS_INODES_PER_CHUNK) {
310                 if ((error = xfs_inobt_lookup_eq(cur, thisino,
311                                 XFS_INODES_PER_CHUNK, XFS_INOBT_ALL_FREE, &i))) {
312                         xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
313                         return error;
314                 }
315                 ASSERT(i == 0);
316                 if ((error = xfs_inobt_insert(cur, &i))) {
317                         xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
318                         return error;
319                 }
320                 ASSERT(i == 1);
321         }
322         xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
323         /*
324          * Log allocation group header fields
325          */
326         xfs_ialloc_log_agi(tp, agbp,
327                 XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO);
328         /*
329          * Modify/log superblock values for inode count and inode free count.
330          */
331         xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen);
332         xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen);
333         *alloc = 1;
334         return 0;
335 }
336
337 STATIC __inline xfs_agnumber_t
338 xfs_ialloc_next_ag(
339         xfs_mount_t     *mp)
340 {
341         xfs_agnumber_t  agno;
342
343         spin_lock(&mp->m_agirotor_lock);
344         agno = mp->m_agirotor;
345         if (++mp->m_agirotor == mp->m_maxagi)
346                 mp->m_agirotor = 0;
347         spin_unlock(&mp->m_agirotor_lock);
348
349         return agno;
350 }
351
352 /*
353  * Select an allocation group to look for a free inode in, based on the parent
354  * inode and then mode.  Return the allocation group buffer.
355  */
356 STATIC xfs_buf_t *                      /* allocation group buffer */
357 xfs_ialloc_ag_select(
358         xfs_trans_t     *tp,            /* transaction pointer */
359         xfs_ino_t       parent,         /* parent directory inode number */
360         mode_t          mode,           /* bits set to indicate file type */
361         int             okalloc)        /* ok to allocate more space */
362 {
363         xfs_buf_t       *agbp;          /* allocation group header buffer */
364         xfs_agnumber_t  agcount;        /* number of ag's in the filesystem */
365         xfs_agnumber_t  agno;           /* current ag number */
366         int             flags;          /* alloc buffer locking flags */
367         xfs_extlen_t    ineed;          /* blocks needed for inode allocation */
368         xfs_extlen_t    longest = 0;    /* longest extent available */
369         xfs_mount_t     *mp;            /* mount point structure */
370         int             needspace;      /* file mode implies space allocated */
371         xfs_perag_t     *pag;           /* per allocation group data */
372         xfs_agnumber_t  pagno;          /* parent (starting) ag number */
373
374         /*
375          * Files of these types need at least one block if length > 0
376          * (and they won't fit in the inode, but that's hard to figure out).
377          */
378         needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode);
379         mp = tp->t_mountp;
380         agcount = mp->m_maxagi;
381         if (S_ISDIR(mode))
382                 pagno = xfs_ialloc_next_ag(mp);
383         else {
384                 pagno = XFS_INO_TO_AGNO(mp, parent);
385                 if (pagno >= agcount)
386                         pagno = 0;
387         }
388         ASSERT(pagno < agcount);
389         /*
390          * Loop through allocation groups, looking for one with a little
391          * free space in it.  Note we don't look for free inodes, exactly.
392          * Instead, we include whether there is a need to allocate inodes
393          * to mean that blocks must be allocated for them,
394          * if none are currently free.
395          */
396         agno = pagno;
397         flags = XFS_ALLOC_FLAG_TRYLOCK;
398         down_read(&mp->m_peraglock);
399         for (;;) {
400                 pag = &mp->m_perag[agno];
401                 if (!pag->pagi_init) {
402                         if (xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
403                                 agbp = NULL;
404                                 goto nextag;
405                         }
406                 } else
407                         agbp = NULL;
408
409                 if (!pag->pagi_inodeok) {
410                         xfs_ialloc_next_ag(mp);
411                         goto unlock_nextag;
412                 }
413
414                 /*
415                  * Is there enough free space for the file plus a block
416                  * of inodes (if we need to allocate some)?
417                  */
418                 ineed = pag->pagi_freecount ? 0 : XFS_IALLOC_BLOCKS(mp);
419                 if (ineed && !pag->pagf_init) {
420                         if (agbp == NULL &&
421                             xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
422                                 agbp = NULL;
423                                 goto nextag;
424                         }
425                         (void)xfs_alloc_pagf_init(mp, tp, agno, flags);
426                 }
427                 if (!ineed || pag->pagf_init) {
428                         if (ineed && !(longest = pag->pagf_longest))
429                                 longest = pag->pagf_flcount > 0;
430                         if (!ineed ||
431                             (pag->pagf_freeblks >= needspace + ineed &&
432                              longest >= ineed &&
433                              okalloc)) {
434                                 if (agbp == NULL &&
435                                     xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
436                                         agbp = NULL;
437                                         goto nextag;
438                                 }
439                                 up_read(&mp->m_peraglock);
440                                 return agbp;
441                         }
442                 }
443 unlock_nextag:
444                 if (agbp)
445                         xfs_trans_brelse(tp, agbp);
446 nextag:
447                 /*
448                  * No point in iterating over the rest, if we're shutting
449                  * down.
450                  */
451                 if (XFS_FORCED_SHUTDOWN(mp)) {
452                         up_read(&mp->m_peraglock);
453                         return (xfs_buf_t *)0;
454                 }
455                 agno++;
456                 if (agno >= agcount)
457                         agno = 0;
458                 if (agno == pagno) {
459                         if (flags == 0) {
460                                 up_read(&mp->m_peraglock);
461                                 return (xfs_buf_t *)0;
462                         }
463                         flags = 0;
464                 }
465         }
466 }
467
468 /*
469  * Visible inode allocation functions.
470  */
471
472 /*
473  * Allocate an inode on disk.
474  * Mode is used to tell whether the new inode will need space, and whether
475  * it is a directory.
476  *
477  * The arguments IO_agbp and alloc_done are defined to work within
478  * the constraint of one allocation per transaction.
479  * xfs_dialloc() is designed to be called twice if it has to do an
480  * allocation to make more free inodes.  On the first call,
481  * IO_agbp should be set to NULL. If an inode is available,
482  * i.e., xfs_dialloc() did not need to do an allocation, an inode
483  * number is returned.  In this case, IO_agbp would be set to the
484  * current ag_buf and alloc_done set to false.
485  * If an allocation needed to be done, xfs_dialloc would return
486  * the current ag_buf in IO_agbp and set alloc_done to true.
487  * The caller should then commit the current transaction, allocate a new
488  * transaction, and call xfs_dialloc() again, passing in the previous
489  * value of IO_agbp.  IO_agbp should be held across the transactions.
490  * Since the agbp is locked across the two calls, the second call is
491  * guaranteed to have a free inode available.
492  *
493  * Once we successfully pick an inode its number is returned and the
494  * on-disk data structures are updated.  The inode itself is not read
495  * in, since doing so would break ordering constraints with xfs_reclaim.
496  */
497 int
498 xfs_dialloc(
499         xfs_trans_t     *tp,            /* transaction pointer */
500         xfs_ino_t       parent,         /* parent inode (directory) */
501         mode_t          mode,           /* mode bits for new inode */
502         int             okalloc,        /* ok to allocate more space */
503         xfs_buf_t       **IO_agbp,      /* in/out ag header's buffer */
504         boolean_t       *alloc_done,    /* true if we needed to replenish
505                                            inode freelist */
506         xfs_ino_t       *inop)          /* inode number allocated */
507 {
508         xfs_agnumber_t  agcount;        /* number of allocation groups */
509         xfs_buf_t       *agbp;          /* allocation group header's buffer */
510         xfs_agnumber_t  agno;           /* allocation group number */
511         xfs_agi_t       *agi;           /* allocation group header structure */
512         xfs_btree_cur_t *cur;           /* inode allocation btree cursor */
513         int             error;          /* error return value */
514         int             i;              /* result code */
515         int             ialloced;       /* inode allocation status */
516         int             noroom = 0;     /* no space for inode blk allocation */
517         xfs_ino_t       ino;            /* fs-relative inode to be returned */
518         /* REFERENCED */
519         int             j;              /* result code */
520         xfs_mount_t     *mp;            /* file system mount structure */
521         int             offset;         /* index of inode in chunk */
522         xfs_agino_t     pagino;         /* parent's a.g. relative inode # */
523         xfs_agnumber_t  pagno;          /* parent's allocation group number */
524         xfs_inobt_rec_t rec;            /* inode allocation record */
525         xfs_agnumber_t  tagno;          /* testing allocation group number */
526         xfs_btree_cur_t *tcur;          /* temp cursor */
527         xfs_inobt_rec_t trec;           /* temp inode allocation record */
528
529
530         if (*IO_agbp == NULL) {
531                 /*
532                  * We do not have an agbp, so select an initial allocation
533                  * group for inode allocation.
534                  */
535                 agbp = xfs_ialloc_ag_select(tp, parent, mode, okalloc);
536                 /*
537                  * Couldn't find an allocation group satisfying the
538                  * criteria, give up.
539                  */
540                 if (!agbp) {
541                         *inop = NULLFSINO;
542                         return 0;
543                 }
544                 agi = XFS_BUF_TO_AGI(agbp);
545                 ASSERT(INT_GET(agi->agi_magicnum, ARCH_CONVERT) == XFS_AGI_MAGIC);
546         } else {
547                 /*
548                  * Continue where we left off before.  In this case, we
549                  * know that the allocation group has free inodes.
550                  */
551                 agbp = *IO_agbp;
552                 agi = XFS_BUF_TO_AGI(agbp);
553                 ASSERT(INT_GET(agi->agi_magicnum, ARCH_CONVERT) == XFS_AGI_MAGIC);
554                 ASSERT(INT_GET(agi->agi_freecount, ARCH_CONVERT) > 0);
555         }
556         mp = tp->t_mountp;
557         agcount = mp->m_sb.sb_agcount;
558         agno = INT_GET(agi->agi_seqno, ARCH_CONVERT);
559         tagno = agno;
560         pagno = XFS_INO_TO_AGNO(mp, parent);
561         pagino = XFS_INO_TO_AGINO(mp, parent);
562
563         /*
564          * If we have already hit the ceiling of inode blocks then clear
565          * okalloc so we scan all available agi structures for a free
566          * inode.
567          */
568
569         if (mp->m_maxicount &&
570             mp->m_sb.sb_icount + XFS_IALLOC_INODES(mp) > mp->m_maxicount) {
571                 noroom = 1;
572                 okalloc = 0;
573         }
574
575         /*
576          * Loop until we find an allocation group that either has free inodes
577          * or in which we can allocate some inodes.  Iterate through the
578          * allocation groups upward, wrapping at the end.
579          */
580         *alloc_done = B_FALSE;
581         while (!agi->agi_freecount) {
582                 /*
583                  * Don't do anything if we're not supposed to allocate
584                  * any blocks, just go on to the next ag.
585                  */
586                 if (okalloc) {
587                         /*
588                          * Try to allocate some new inodes in the allocation
589                          * group.
590                          */
591                         if ((error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced))) {
592                                 xfs_trans_brelse(tp, agbp);
593                                 if (error == ENOSPC) {
594                                         *inop = NULLFSINO;
595                                         return 0;
596                                 } else
597                                         return error;
598                         }
599                         if (ialloced) {
600                                 /*
601                                  * We successfully allocated some inodes, return
602                                  * the current context to the caller so that it
603                                  * can commit the current transaction and call
604                                  * us again where we left off.
605                                  */
606                                 ASSERT(INT_GET(agi->agi_freecount, ARCH_CONVERT) > 0);
607                                 *alloc_done = B_TRUE;
608                                 *IO_agbp = agbp;
609                                 *inop = NULLFSINO;
610                                 return 0;
611                         }
612                 }
613                 /*
614                  * If it failed, give up on this ag.
615                  */
616                 xfs_trans_brelse(tp, agbp);
617                 /*
618                  * Go on to the next ag: get its ag header.
619                  */
620 nextag:
621                 if (++tagno == agcount)
622                         tagno = 0;
623                 if (tagno == agno) {
624                         *inop = NULLFSINO;
625                         return noroom ? ENOSPC : 0;
626                 }
627                 down_read(&mp->m_peraglock);
628                 if (mp->m_perag[tagno].pagi_inodeok == 0) {
629                         up_read(&mp->m_peraglock);
630                         goto nextag;
631                 }
632                 error = xfs_ialloc_read_agi(mp, tp, tagno, &agbp);
633                 up_read(&mp->m_peraglock);
634                 if (error)
635                         goto nextag;
636                 agi = XFS_BUF_TO_AGI(agbp);
637                 ASSERT(INT_GET(agi->agi_magicnum, ARCH_CONVERT) == XFS_AGI_MAGIC);
638         }
639         /*
640          * Here with an allocation group that has a free inode.
641          * Reset agno since we may have chosen a new ag in the
642          * loop above.
643          */
644         agno = tagno;
645         *IO_agbp = NULL;
646         cur = xfs_btree_init_cursor(mp, tp, agbp, INT_GET(agi->agi_seqno, ARCH_CONVERT),
647                                     XFS_BTNUM_INO, (xfs_inode_t *)0, 0);
648         /*
649          * If pagino is 0 (this is the root inode allocation) use newino.
650          * This must work because we've just allocated some.
651          */
652         if (!pagino)
653                 pagino = INT_GET(agi->agi_newino, ARCH_CONVERT);
654 #ifdef DEBUG
655         if (cur->bc_nlevels == 1) {
656                 int     freecount = 0;
657
658                 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
659                         goto error0;
660                 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
661                 do {
662                         if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
663                                         &rec.ir_freecount, &rec.ir_free, &i)))
664                                 goto error0;
665                         XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
666                         freecount += rec.ir_freecount;
667                         if ((error = xfs_inobt_increment(cur, 0, &i)))
668                                 goto error0;
669                 } while (i == 1);
670
671                 ASSERT(freecount == INT_GET(agi->agi_freecount, ARCH_CONVERT) ||
672                        XFS_FORCED_SHUTDOWN(mp));
673         }
674 #endif
675         /*
676          * If in the same a.g. as the parent, try to get near the parent.
677          */
678         if (pagno == agno) {
679                 if ((error = xfs_inobt_lookup_le(cur, pagino, 0, 0, &i)))
680                         goto error0;
681                 if (i != 0 &&
682                     (error = xfs_inobt_get_rec(cur, &rec.ir_startino,
683                             &rec.ir_freecount, &rec.ir_free, &j)) == 0 &&
684                     j == 1 &&
685                     rec.ir_freecount > 0) {
686                         /*
687                          * Found a free inode in the same chunk
688                          * as parent, done.
689                          */
690                 }
691                 /*
692                  * In the same a.g. as parent, but parent's chunk is full.
693                  */
694                 else {
695                         int     doneleft;       /* done, to the left */
696                         int     doneright;      /* done, to the right */
697
698                         if (error)
699                                 goto error0;
700                         ASSERT(i == 1);
701                         ASSERT(j == 1);
702                         /*
703                          * Duplicate the cursor, search left & right
704                          * simultaneously.
705                          */
706                         if ((error = xfs_btree_dup_cursor(cur, &tcur)))
707                                 goto error0;
708                         /*
709                          * Search left with tcur, back up 1 record.
710                          */
711                         if ((error = xfs_inobt_decrement(tcur, 0, &i)))
712                                 goto error1;
713                         doneleft = !i;
714                         if (!doneleft) {
715                                 if ((error = xfs_inobt_get_rec(tcur,
716                                                 &trec.ir_startino,
717                                                 &trec.ir_freecount,
718                                                 &trec.ir_free, &i)))
719                                         goto error1;
720                                 XFS_WANT_CORRUPTED_GOTO(i == 1, error1);
721                         }
722                         /*
723                          * Search right with cur, go forward 1 record.
724                          */
725                         if ((error = xfs_inobt_increment(cur, 0, &i)))
726                                 goto error1;
727                         doneright = !i;
728                         if (!doneright) {
729                                 if ((error = xfs_inobt_get_rec(cur,
730                                                 &rec.ir_startino,
731                                                 &rec.ir_freecount,
732                                                 &rec.ir_free, &i)))
733                                         goto error1;
734                                 XFS_WANT_CORRUPTED_GOTO(i == 1, error1);
735                         }
736                         /*
737                          * Loop until we find the closest inode chunk
738                          * with a free one.
739                          */
740                         while (!doneleft || !doneright) {
741                                 int     useleft;  /* using left inode
742                                                      chunk this time */
743
744                                 /*
745                                  * Figure out which block is closer,
746                                  * if both are valid.
747                                  */
748                                 if (!doneleft && !doneright)
749                                         useleft =
750                                                 pagino -
751                                                 (trec.ir_startino +
752                                                  XFS_INODES_PER_CHUNK - 1) <
753                                                  rec.ir_startino - pagino;
754                                 else
755                                         useleft = !doneleft;
756                                 /*
757                                  * If checking the left, does it have
758                                  * free inodes?
759                                  */
760                                 if (useleft && trec.ir_freecount) {
761                                         /*
762                                          * Yes, set it up as the chunk to use.
763                                          */
764                                         rec = trec;
765                                         xfs_btree_del_cursor(cur,
766                                                 XFS_BTREE_NOERROR);
767                                         cur = tcur;
768                                         break;
769                                 }
770                                 /*
771                                  * If checking the right, does it have
772                                  * free inodes?
773                                  */
774                                 if (!useleft && rec.ir_freecount) {
775                                         /*
776                                          * Yes, it's already set up.
777                                          */
778                                         xfs_btree_del_cursor(tcur,
779                                                 XFS_BTREE_NOERROR);
780                                         break;
781                                 }
782                                 /*
783                                  * If used the left, get another one
784                                  * further left.
785                                  */
786                                 if (useleft) {
787                                         if ((error = xfs_inobt_decrement(tcur, 0,
788                                                         &i)))
789                                                 goto error1;
790                                         doneleft = !i;
791                                         if (!doneleft) {
792                                                 if ((error = xfs_inobt_get_rec(
793                                                             tcur,
794                                                             &trec.ir_startino,
795                                                             &trec.ir_freecount,
796                                                             &trec.ir_free, &i)))
797                                                         goto error1;
798                                                 XFS_WANT_CORRUPTED_GOTO(i == 1,
799                                                         error1);
800                                         }
801                                 }
802                                 /*
803                                  * If used the right, get another one
804                                  * further right.
805                                  */
806                                 else {
807                                         if ((error = xfs_inobt_increment(cur, 0,
808                                                         &i)))
809                                                 goto error1;
810                                         doneright = !i;
811                                         if (!doneright) {
812                                                 if ((error = xfs_inobt_get_rec(
813                                                             cur,
814                                                             &rec.ir_startino,
815                                                             &rec.ir_freecount,
816                                                             &rec.ir_free, &i)))
817                                                         goto error1;
818                                                 XFS_WANT_CORRUPTED_GOTO(i == 1,
819                                                         error1);
820                                         }
821                                 }
822                         }
823                         ASSERT(!doneleft || !doneright);
824                 }
825         }
826         /*
827          * In a different a.g. from the parent.
828          * See if the most recently allocated block has any free.
829          */
830         else if (INT_GET(agi->agi_newino, ARCH_CONVERT) != NULLAGINO) {
831                 if ((error = xfs_inobt_lookup_eq(cur,
832                                 INT_GET(agi->agi_newino, ARCH_CONVERT), 0, 0, &i)))
833                         goto error0;
834                 if (i == 1 &&
835                     (error = xfs_inobt_get_rec(cur, &rec.ir_startino,
836                             &rec.ir_freecount, &rec.ir_free, &j)) == 0 &&
837                     j == 1 &&
838                     rec.ir_freecount > 0) {
839                         /*
840                          * The last chunk allocated in the group still has
841                          * a free inode.
842                          */
843                 }
844                 /*
845                  * None left in the last group, search the whole a.g.
846                  */
847                 else {
848                         if (error)
849                                 goto error0;
850                         if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
851                                 goto error0;
852                         ASSERT(i == 1);
853                         for (;;) {
854                                 if ((error = xfs_inobt_get_rec(cur,
855                                                 &rec.ir_startino,
856                                                 &rec.ir_freecount, &rec.ir_free,
857                                                 &i)))
858                                         goto error0;
859                                 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
860                                 if (rec.ir_freecount > 0)
861                                         break;
862                                 if ((error = xfs_inobt_increment(cur, 0, &i)))
863                                         goto error0;
864                                 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
865                         }
866                 }
867         }
868         offset = XFS_IALLOC_FIND_FREE(&rec.ir_free);
869         ASSERT(offset >= 0);
870         ASSERT(offset < XFS_INODES_PER_CHUNK);
871         ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
872                                    XFS_INODES_PER_CHUNK) == 0);
873         ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
874         XFS_INOBT_CLR_FREE(&rec, offset);
875         rec.ir_freecount--;
876         if ((error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount,
877                         rec.ir_free)))
878                 goto error0;
879         INT_MOD(agi->agi_freecount, ARCH_CONVERT, -1);
880         xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
881         down_read(&mp->m_peraglock);
882         mp->m_perag[tagno].pagi_freecount--;
883         up_read(&mp->m_peraglock);
884 #ifdef DEBUG
885         if (cur->bc_nlevels == 1) {
886                 int     freecount = 0;
887
888                 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
889                         goto error0;
890                 do {
891                         if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
892                                         &rec.ir_freecount, &rec.ir_free, &i)))
893                                 goto error0;
894                         XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
895                         freecount += rec.ir_freecount;
896                         if ((error = xfs_inobt_increment(cur, 0, &i)))
897                                 goto error0;
898                 } while (i == 1);
899                 ASSERT(freecount == INT_GET(agi->agi_freecount, ARCH_CONVERT) ||
900                        XFS_FORCED_SHUTDOWN(mp));
901         }
902 #endif
903         xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
904         xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
905         *inop = ino;
906         return 0;
907 error1:
908         xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
909 error0:
910         xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
911         return error;
912 }
913
914 /*
915  * Free disk inode.  Carefully avoids touching the incore inode, all
916  * manipulations incore are the caller's responsibility.
917  * The on-disk inode is not changed by this operation, only the
918  * btree (free inode mask) is changed.
919  */
920 int
921 xfs_difree(
922         xfs_trans_t     *tp,            /* transaction pointer */
923         xfs_ino_t       inode,          /* inode to be freed */
924         xfs_bmap_free_t *flist,         /* extents to free */
925         int             *delete,        /* set if inode cluster was deleted */
926         xfs_ino_t       *first_ino)     /* first inode in deleted cluster */
927 {
928         /* REFERENCED */
929         xfs_agblock_t   agbno;  /* block number containing inode */
930         xfs_buf_t       *agbp;  /* buffer containing allocation group header */
931         xfs_agino_t     agino;  /* inode number relative to allocation group */
932         xfs_agnumber_t  agno;   /* allocation group number */
933         xfs_agi_t       *agi;   /* allocation group header */
934         xfs_btree_cur_t *cur;   /* inode btree cursor */
935         int             error;  /* error return value */
936         int             i;      /* result code */
937         int             ilen;   /* inodes in an inode cluster */
938         xfs_mount_t     *mp;    /* mount structure for filesystem */
939         int             off;    /* offset of inode in inode chunk */
940         xfs_inobt_rec_t rec;    /* btree record */
941
942         mp = tp->t_mountp;
943
944         /*
945          * Break up inode number into its components.
946          */
947         agno = XFS_INO_TO_AGNO(mp, inode);
948         if (agno >= mp->m_sb.sb_agcount)  {
949                 cmn_err(CE_WARN,
950                         "xfs_difree: agno >= mp->m_sb.sb_agcount (%d >= %d) on %s.  Returning EINVAL.",
951                         agno, mp->m_sb.sb_agcount, mp->m_fsname);
952                 ASSERT(0);
953                 return XFS_ERROR(EINVAL);
954         }
955         agino = XFS_INO_TO_AGINO(mp, inode);
956         if (inode != XFS_AGINO_TO_INO(mp, agno, agino))  {
957                 cmn_err(CE_WARN,
958                         "xfs_difree: inode != XFS_AGINO_TO_INO() "
959                         "(%llu != %llu) on %s.  Returning EINVAL.",
960                         (unsigned long long)inode,
961                         (unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino),
962                         mp->m_fsname);
963                 ASSERT(0);
964                 return XFS_ERROR(EINVAL);
965         }
966         agbno = XFS_AGINO_TO_AGBNO(mp, agino);
967         if (agbno >= mp->m_sb.sb_agblocks)  {
968                 cmn_err(CE_WARN,
969                         "xfs_difree: agbno >= mp->m_sb.sb_agblocks (%d >= %d) on %s.  Returning EINVAL.",
970                         agbno, mp->m_sb.sb_agblocks, mp->m_fsname);
971                 ASSERT(0);
972                 return XFS_ERROR(EINVAL);
973         }
974         /*
975          * Get the allocation group header.
976          */
977         down_read(&mp->m_peraglock);
978         error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
979         up_read(&mp->m_peraglock);
980         if (error) {
981                 cmn_err(CE_WARN,
982                         "xfs_difree: xfs_ialloc_read_agi() returned an error %d on %s.  Returning error.",
983                         error, mp->m_fsname);
984                 return error;
985         }
986         agi = XFS_BUF_TO_AGI(agbp);
987         ASSERT(INT_GET(agi->agi_magicnum, ARCH_CONVERT) == XFS_AGI_MAGIC);
988         ASSERT(agbno < INT_GET(agi->agi_length, ARCH_CONVERT));
989         /*
990          * Initialize the cursor.
991          */
992         cur = xfs_btree_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO,
993                 (xfs_inode_t *)0, 0);
994 #ifdef DEBUG
995         if (cur->bc_nlevels == 1) {
996                 int freecount = 0;
997
998                 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
999                         goto error0;
1000                 do {
1001                         if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
1002                                         &rec.ir_freecount, &rec.ir_free, &i)))
1003                                 goto error0;
1004                         if (i) {
1005                                 freecount += rec.ir_freecount;
1006                                 if ((error = xfs_inobt_increment(cur, 0, &i)))
1007                                         goto error0;
1008                         }
1009                 } while (i == 1);
1010                 ASSERT(freecount == INT_GET(agi->agi_freecount, ARCH_CONVERT) ||
1011                        XFS_FORCED_SHUTDOWN(mp));
1012         }
1013 #endif
1014         /*
1015          * Look for the entry describing this inode.
1016          */
1017         if ((error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i))) {
1018                 cmn_err(CE_WARN,
1019                         "xfs_difree: xfs_inobt_lookup_le returned()  an error %d on %s.  Returning error.",
1020                         error, mp->m_fsname);
1021                 goto error0;
1022         }
1023         XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1024         if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino, &rec.ir_freecount,
1025                         &rec.ir_free, &i))) {
1026                 cmn_err(CE_WARN,
1027                         "xfs_difree: xfs_inobt_get_rec()  returned an error %d on %s.  Returning error.",
1028                         error, mp->m_fsname);
1029                 goto error0;
1030         }
1031         XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1032         /*
1033          * Get the offset in the inode chunk.
1034          */
1035         off = agino - rec.ir_startino;
1036         ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK);
1037         ASSERT(!XFS_INOBT_IS_FREE(&rec, off));
1038         /*
1039          * Mark the inode free & increment the count.
1040          */
1041         XFS_INOBT_SET_FREE(&rec, off);
1042         rec.ir_freecount++;
1043
1044         /*
1045          * When an inode cluster is free, it becomes elgible for removal
1046          */
1047         if ((mp->m_flags & XFS_MOUNT_IDELETE) &&
1048             (rec.ir_freecount == XFS_IALLOC_INODES(mp))) {
1049
1050                 *delete = 1;
1051                 *first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino);
1052
1053                 /*
1054                  * Remove the inode cluster from the AGI B+Tree, adjust the
1055                  * AGI and Superblock inode counts, and mark the disk space
1056                  * to be freed when the transaction is committed.
1057                  */
1058                 ilen = XFS_IALLOC_INODES(mp);
1059                 INT_MOD(agi->agi_count, ARCH_CONVERT, -ilen);
1060                 INT_MOD(agi->agi_freecount, ARCH_CONVERT, -(ilen - 1));
1061                 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT);
1062                 down_read(&mp->m_peraglock);
1063                 mp->m_perag[agno].pagi_freecount -= ilen - 1;
1064                 up_read(&mp->m_peraglock);
1065                 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen);
1066                 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1));
1067
1068                 if ((error = xfs_inobt_delete(cur, &i))) {
1069                         cmn_err(CE_WARN, "xfs_difree: xfs_inobt_delete returned an error %d on %s.\n",
1070                                 error, mp->m_fsname);
1071                         goto error0;
1072                 }
1073
1074                 xfs_bmap_add_free(XFS_AGB_TO_FSB(mp,
1075                                 agno, XFS_INO_TO_AGBNO(mp,rec.ir_startino)),
1076                                 XFS_IALLOC_BLOCKS(mp), flist, mp);
1077         } else {
1078                 *delete = 0;
1079
1080                 if ((error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount, rec.ir_free))) {
1081                         cmn_err(CE_WARN,
1082                                 "xfs_difree: xfs_inobt_update()  returned an error %d on %s.  Returning error.",
1083                                 error, mp->m_fsname);
1084                         goto error0;
1085                 }
1086                 /* 
1087                  * Change the inode free counts and log the ag/sb changes.
1088                  */
1089                 INT_MOD(agi->agi_freecount, ARCH_CONVERT, 1);
1090                 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
1091                 down_read(&mp->m_peraglock);
1092                 mp->m_perag[agno].pagi_freecount++;
1093                 up_read(&mp->m_peraglock);
1094                 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1);
1095         }
1096
1097 #ifdef DEBUG
1098         if (cur->bc_nlevels == 1) {
1099                 int freecount = 0;
1100
1101                 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
1102                         goto error0;
1103                 do {
1104                         if ((error = xfs_inobt_get_rec(cur,
1105                                         &rec.ir_startino,
1106                                         &rec.ir_freecount,
1107                                         &rec.ir_free, &i)))
1108                                 goto error0;
1109                         if (i) {
1110                                 freecount += rec.ir_freecount;
1111                                 if ((error = xfs_inobt_increment(cur, 0, &i)))
1112                                         goto error0;
1113                         }
1114                 } while (i == 1);
1115                 ASSERT(freecount == INT_GET(agi->agi_freecount, ARCH_CONVERT) ||
1116                        XFS_FORCED_SHUTDOWN(mp));
1117         }
1118 #endif
1119         xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1120         return 0;
1121
1122 error0:
1123         xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1124         return error;
1125 }
1126
1127 /*
1128  * Return the location of the inode in bno/off, for mapping it into a buffer.
1129  */
1130 /*ARGSUSED*/
1131 int
1132 xfs_dilocate(
1133         xfs_mount_t     *mp,    /* file system mount structure */
1134         xfs_trans_t     *tp,    /* transaction pointer */
1135         xfs_ino_t       ino,    /* inode to locate */
1136         xfs_fsblock_t   *bno,   /* output: block containing inode */
1137         int             *len,   /* output: num blocks in inode cluster */
1138         int             *off,   /* output: index in block of inode */
1139         uint            flags)  /* flags concerning inode lookup */
1140 {
1141         xfs_agblock_t   agbno;  /* block number of inode in the alloc group */
1142         xfs_buf_t       *agbp;  /* agi buffer */
1143         xfs_agino_t     agino;  /* inode number within alloc group */
1144         xfs_agnumber_t  agno;   /* allocation group number */
1145         int             blks_per_cluster; /* num blocks per inode cluster */
1146         xfs_agblock_t   chunk_agbno;    /* first block in inode chunk */
1147         xfs_agino_t     chunk_agino;    /* first agino in inode chunk */
1148         __int32_t       chunk_cnt;      /* count of free inodes in chunk */
1149         xfs_inofree_t   chunk_free;     /* mask of free inodes in chunk */
1150         xfs_agblock_t   cluster_agbno;  /* first block in inode cluster */
1151         xfs_btree_cur_t *cur;   /* inode btree cursor */
1152         int             error;  /* error code */
1153         int             i;      /* temp state */
1154         int             offset; /* index of inode in its buffer */
1155         int             offset_agbno;   /* blks from chunk start to inode */
1156
1157         ASSERT(ino != NULLFSINO);
1158         /*
1159          * Split up the inode number into its parts.
1160          */
1161         agno = XFS_INO_TO_AGNO(mp, ino);
1162         agino = XFS_INO_TO_AGINO(mp, ino);
1163         agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1164         if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks ||
1165             ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1166 #ifdef DEBUG
1167                 if (agno >= mp->m_sb.sb_agcount) {
1168                         xfs_fs_cmn_err(CE_ALERT, mp,
1169                                         "xfs_dilocate: agno (%d) >= "
1170                                         "mp->m_sb.sb_agcount (%d)",
1171                                         agno,  mp->m_sb.sb_agcount);
1172                 }
1173                 if (agbno >= mp->m_sb.sb_agblocks) {
1174                         xfs_fs_cmn_err(CE_ALERT, mp,
1175                                         "xfs_dilocate: agbno (0x%llx) >= "
1176                                         "mp->m_sb.sb_agblocks (0x%lx)",
1177                                         (unsigned long long) agbno,
1178                                         (unsigned long) mp->m_sb.sb_agblocks);
1179                 }
1180                 if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1181                         xfs_fs_cmn_err(CE_ALERT, mp,
1182                                         "xfs_dilocate: ino (0x%llx) != "
1183                                         "XFS_AGINO_TO_INO(mp, agno, agino) "
1184                                         "(0x%llx)",
1185                                         ino, XFS_AGINO_TO_INO(mp, agno, agino));
1186                 }
1187 #endif /* DEBUG */
1188                 return XFS_ERROR(EINVAL);
1189         }
1190         if ((mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) ||
1191             !(flags & XFS_IMAP_LOOKUP)) {
1192                 offset = XFS_INO_TO_OFFSET(mp, ino);
1193                 ASSERT(offset < mp->m_sb.sb_inopblock);
1194                 *bno = XFS_AGB_TO_FSB(mp, agno, agbno);
1195                 *off = offset;
1196                 *len = 1;
1197                 return 0;
1198         }
1199         blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_blocklog;
1200         if (*bno != NULLFSBLOCK) {
1201                 offset = XFS_INO_TO_OFFSET(mp, ino);
1202                 ASSERT(offset < mp->m_sb.sb_inopblock);
1203                 cluster_agbno = XFS_FSB_TO_AGBNO(mp, *bno);
1204                 *off = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
1205                         offset;
1206                 *len = blks_per_cluster;
1207                 return 0;
1208         }
1209         if (mp->m_inoalign_mask) {
1210                 offset_agbno = agbno & mp->m_inoalign_mask;
1211                 chunk_agbno = agbno - offset_agbno;
1212         } else {
1213                 down_read(&mp->m_peraglock);
1214                 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1215                 up_read(&mp->m_peraglock);
1216                 if (error) {
1217 #ifdef DEBUG
1218                         xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1219                                         "xfs_ialloc_read_agi() returned "
1220                                         "error %d, agno %d",
1221                                         error, agno);
1222 #endif /* DEBUG */
1223                         return error;
1224                 }
1225                 cur = xfs_btree_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO,
1226                         (xfs_inode_t *)0, 0);
1227                 if ((error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i))) {
1228 #ifdef DEBUG
1229                         xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1230                                         "xfs_inobt_lookup_le() failed");
1231 #endif /* DEBUG */
1232                         goto error0;
1233                 }
1234                 if ((error = xfs_inobt_get_rec(cur, &chunk_agino, &chunk_cnt,
1235                                 &chunk_free, &i))) {
1236 #ifdef DEBUG
1237                         xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1238                                         "xfs_inobt_get_rec() failed");
1239 #endif /* DEBUG */
1240                         goto error0;
1241                 }
1242                 if (i == 0) {
1243 #ifdef DEBUG
1244                         xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1245                                         "xfs_inobt_get_rec() failed");
1246 #endif /* DEBUG */
1247                         error = XFS_ERROR(EINVAL);
1248                 }
1249                 xfs_trans_brelse(tp, agbp);
1250                 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1251                 if (error)
1252                         return error;
1253                 chunk_agbno = XFS_AGINO_TO_AGBNO(mp, chunk_agino);
1254                 offset_agbno = agbno - chunk_agbno;
1255         }
1256         ASSERT(agbno >= chunk_agbno);
1257         cluster_agbno = chunk_agbno +
1258                 ((offset_agbno / blks_per_cluster) * blks_per_cluster);
1259         offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
1260                 XFS_INO_TO_OFFSET(mp, ino);
1261         *bno = XFS_AGB_TO_FSB(mp, agno, cluster_agbno);
1262         *off = offset;
1263         *len = blks_per_cluster;
1264         return 0;
1265 error0:
1266         xfs_trans_brelse(tp, agbp);
1267         xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1268         return error;
1269 }
1270
1271 /*
1272  * Compute and fill in value of m_in_maxlevels.
1273  */
1274 void
1275 xfs_ialloc_compute_maxlevels(
1276         xfs_mount_t     *mp)            /* file system mount structure */
1277 {
1278         int             level;
1279         uint            maxblocks;
1280         uint            maxleafents;
1281         int             minleafrecs;
1282         int             minnoderecs;
1283
1284         maxleafents = (1LL << XFS_INO_AGINO_BITS(mp)) >>
1285                 XFS_INODES_PER_CHUNK_LOG;
1286         minleafrecs = mp->m_alloc_mnr[0];
1287         minnoderecs = mp->m_alloc_mnr[1];
1288         maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
1289         for (level = 1; maxblocks > 1; level++)
1290                 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
1291         mp->m_in_maxlevels = level;
1292 }
1293
1294 /*
1295  * Log specified fields for the ag hdr (inode section)
1296  */
1297 void
1298 xfs_ialloc_log_agi(
1299         xfs_trans_t     *tp,            /* transaction pointer */
1300         xfs_buf_t       *bp,            /* allocation group header buffer */
1301         int             fields)         /* bitmask of fields to log */
1302 {
1303         int                     first;          /* first byte number */
1304         int                     last;           /* last byte number */
1305         static const short      offsets[] = {   /* field starting offsets */
1306                                         /* keep in sync with bit definitions */
1307                 offsetof(xfs_agi_t, agi_magicnum),
1308                 offsetof(xfs_agi_t, agi_versionnum),
1309                 offsetof(xfs_agi_t, agi_seqno),
1310                 offsetof(xfs_agi_t, agi_length),
1311                 offsetof(xfs_agi_t, agi_count),
1312                 offsetof(xfs_agi_t, agi_root),
1313                 offsetof(xfs_agi_t, agi_level),
1314                 offsetof(xfs_agi_t, agi_freecount),
1315                 offsetof(xfs_agi_t, agi_newino),
1316                 offsetof(xfs_agi_t, agi_dirino),
1317                 offsetof(xfs_agi_t, agi_unlinked),
1318                 sizeof(xfs_agi_t)
1319         };
1320 #ifdef DEBUG
1321         xfs_agi_t               *agi;   /* allocation group header */
1322
1323         agi = XFS_BUF_TO_AGI(bp);
1324         ASSERT(INT_GET(agi->agi_magicnum, ARCH_CONVERT) == XFS_AGI_MAGIC);
1325 #endif
1326         /*
1327          * Compute byte offsets for the first and last fields.
1328          */
1329         xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS, &first, &last);
1330         /*
1331          * Log the allocation group inode header buffer.
1332          */
1333         xfs_trans_log_buf(tp, bp, first, last);
1334 }
1335
1336 /*
1337  * Read in the allocation group header (inode allocation section)
1338  */
1339 int
1340 xfs_ialloc_read_agi(
1341         xfs_mount_t     *mp,            /* file system mount structure */
1342         xfs_trans_t     *tp,            /* transaction pointer */
1343         xfs_agnumber_t  agno,           /* allocation group number */
1344         xfs_buf_t       **bpp)          /* allocation group hdr buf */
1345 {
1346         xfs_agi_t       *agi;           /* allocation group header */
1347         int             agi_ok;         /* agi is consistent */
1348         xfs_buf_t       *bp;            /* allocation group hdr buf */
1349         xfs_perag_t     *pag;           /* per allocation group data */
1350         int             error;
1351
1352         ASSERT(agno != NULLAGNUMBER);
1353         error = xfs_trans_read_buf(
1354                         mp, tp, mp->m_ddev_targp,
1355                         XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
1356                         XFS_FSS_TO_BB(mp, 1), 0, &bp);
1357         if (error)
1358                 return error;
1359         ASSERT(bp && !XFS_BUF_GETERROR(bp));
1360
1361         /*
1362          * Validate the magic number of the agi block.
1363          */
1364         agi = XFS_BUF_TO_AGI(bp);
1365         agi_ok =
1366                 INT_GET(agi->agi_magicnum, ARCH_CONVERT) == XFS_AGI_MAGIC &&
1367                 XFS_AGI_GOOD_VERSION(
1368                         INT_GET(agi->agi_versionnum, ARCH_CONVERT));
1369         if (unlikely(XFS_TEST_ERROR(!agi_ok, mp, XFS_ERRTAG_IALLOC_READ_AGI,
1370                         XFS_RANDOM_IALLOC_READ_AGI))) {
1371                 XFS_CORRUPTION_ERROR("xfs_ialloc_read_agi", XFS_ERRLEVEL_LOW,
1372                                      mp, agi);
1373                 xfs_trans_brelse(tp, bp);
1374                 return XFS_ERROR(EFSCORRUPTED);
1375         }
1376         pag = &mp->m_perag[agno];
1377         if (!pag->pagi_init) {
1378                 pag->pagi_freecount = INT_GET(agi->agi_freecount, ARCH_CONVERT);
1379                 pag->pagi_init = 1;
1380         } else {
1381                 /*
1382                  * It's possible for these to be out of sync if
1383                  * we are in the middle of a forced shutdown.
1384                  */
1385                 ASSERT(pag->pagi_freecount ==
1386                                 INT_GET(agi->agi_freecount, ARCH_CONVERT)
1387                         || XFS_FORCED_SHUTDOWN(mp));
1388         }
1389
1390 #ifdef DEBUG
1391         {
1392                 int     i;
1393
1394                 for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++)
1395                         ASSERT(agi->agi_unlinked[i]);
1396         }
1397 #endif
1398
1399         XFS_BUF_SET_VTYPE_REF(bp, B_FS_AGI, XFS_AGI_REF);
1400         *bpp = bp;
1401         return 0;
1402 }