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