2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
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.
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.
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
20 #include "xfs_types.h"
24 #include "xfs_trans.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"
46 * Allocation group level functions.
49 xfs_ialloc_cluster_alignment(
50 xfs_alloc_arg_t *args)
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;
60 * Lookup the record equal to ino in the btree given by cur.
62 STATIC int /* error */
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 */
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);
77 * Lookup the first record greater than or equal to ino
78 * in the btree given by cur.
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 */
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);
95 * Lookup the first record less than or equal to ino
96 * in the btree given by cur.
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 */
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);
113 * Update the record referred to by cur to the value given.
114 * This either works (return 0) or gets an EFSCORRUPTED error.
116 STATIC int /* error */
118 struct xfs_btree_cur *cur, /* btree cursor */
119 xfs_inobt_rec_incore_t *irec) /* btree record */
121 union xfs_btree_rec rec;
123 rec.inobt.ir_startino = cpu_to_be32(irec->ir_startino);
124 rec.inobt.ir_freecount = cpu_to_be32(irec->ir_freecount);
125 rec.inobt.ir_free = cpu_to_be64(irec->ir_free);
126 return xfs_btree_update(cur, &rec);
130 * Get the data from the pointed-to record.
134 struct xfs_btree_cur *cur, /* btree cursor */
135 xfs_inobt_rec_incore_t *irec, /* btree record */
136 int *stat) /* output: success/failure */
138 union xfs_btree_rec *rec;
141 error = xfs_btree_get_rec(cur, &rec, stat);
142 if (!error && *stat == 1) {
143 irec->ir_startino = be32_to_cpu(rec->inobt.ir_startino);
144 irec->ir_freecount = be32_to_cpu(rec->inobt.ir_freecount);
145 irec->ir_free = be64_to_cpu(rec->inobt.ir_free);
151 * Initialise a new set of inodes.
154 xfs_ialloc_inode_init(
155 struct xfs_mount *mp,
156 struct xfs_trans *tp,
159 xfs_agblock_t length,
162 struct xfs_buf *fbuf;
163 struct xfs_dinode *free;
164 int blks_per_cluster, nbufs, ninodes;
170 * Loop over the new block(s), filling in the inodes.
171 * For small block sizes, manipulate the inodes in buffers
172 * which are multiples of the blocks size.
174 if (mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) {
175 blks_per_cluster = 1;
177 ninodes = mp->m_sb.sb_inopblock;
179 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) /
180 mp->m_sb.sb_blocksize;
181 nbufs = length / blks_per_cluster;
182 ninodes = blks_per_cluster * mp->m_sb.sb_inopblock;
186 * Figure out what version number to use in the inodes we create.
187 * If the superblock version has caught up to the one that supports
188 * the new inode format, then use the new inode version. Otherwise
189 * use the old version so that old kernels will continue to be
190 * able to use the file system.
192 if (xfs_sb_version_hasnlink(&mp->m_sb))
197 for (j = 0; j < nbufs; j++) {
201 d = XFS_AGB_TO_DADDR(mp, agno, agbno + (j * blks_per_cluster));
202 fbuf = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
203 mp->m_bsize * blks_per_cluster,
206 ASSERT(!XFS_BUF_GETERROR(fbuf));
209 * Initialize all inodes in this buffer and then log them.
211 * XXX: It would be much better if we had just one transaction
212 * to log a whole cluster of inodes instead of all the
213 * individual transactions causing a lot of log traffic.
215 xfs_biozero(fbuf, 0, ninodes << mp->m_sb.sb_inodelog);
216 for (i = 0; i < ninodes; i++) {
217 int ioffset = i << mp->m_sb.sb_inodelog;
218 uint isize = sizeof(struct xfs_dinode);
220 free = xfs_make_iptr(mp, fbuf, i);
221 free->di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
222 free->di_version = version;
223 free->di_gen = cpu_to_be32(gen);
224 free->di_next_unlinked = cpu_to_be32(NULLAGINO);
225 xfs_trans_log_buf(tp, fbuf, ioffset, ioffset + isize - 1);
227 xfs_trans_inode_alloc_buf(tp, fbuf);
232 * Allocate new inodes in the allocation group specified by agbp.
233 * Return 0 for success, else error code.
235 STATIC int /* error code or 0 */
237 xfs_trans_t *tp, /* transaction pointer */
238 xfs_buf_t *agbp, /* alloc group buffer */
241 xfs_agi_t *agi; /* allocation group header */
242 xfs_alloc_arg_t args; /* allocation argument structure */
243 xfs_btree_cur_t *cur; /* inode btree cursor */
247 xfs_agino_t newino; /* new first inode's number */
248 xfs_agino_t newlen; /* new number of inodes */
249 xfs_agino_t thisino; /* current inode number, for loop */
250 int isaligned = 0; /* inode allocation at stripe unit */
254 args.mp = tp->t_mountp;
257 * Locking will ensure that we don't have two callers in here
260 newlen = XFS_IALLOC_INODES(args.mp);
261 if (args.mp->m_maxicount &&
262 args.mp->m_sb.sb_icount + newlen > args.mp->m_maxicount)
263 return XFS_ERROR(ENOSPC);
264 args.minlen = args.maxlen = XFS_IALLOC_BLOCKS(args.mp);
266 * First try to allocate inodes contiguous with the last-allocated
267 * chunk of inodes. If the filesystem is striped, this will fill
268 * an entire stripe unit with inodes.
270 agi = XFS_BUF_TO_AGI(agbp);
271 newino = be32_to_cpu(agi->agi_newino);
272 agno = be32_to_cpu(agi->agi_seqno);
273 args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) +
274 XFS_IALLOC_BLOCKS(args.mp);
275 if (likely(newino != NULLAGINO &&
276 (args.agbno < be32_to_cpu(agi->agi_length)))) {
277 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
278 args.type = XFS_ALLOCTYPE_THIS_BNO;
279 args.mod = args.total = args.wasdel = args.isfl =
280 args.userdata = args.minalignslop = 0;
284 * We need to take into account alignment here to ensure that
285 * we don't modify the free list if we fail to have an exact
286 * block. If we don't have an exact match, and every oher
287 * attempt allocation attempt fails, we'll end up cancelling
288 * a dirty transaction and shutting down.
290 * For an exact allocation, alignment must be 1,
291 * however we need to take cluster alignment into account when
292 * fixing up the freelist. Use the minalignslop field to
293 * indicate that extra blocks might be required for alignment,
294 * but not to use them in the actual exact allocation.
297 args.minalignslop = xfs_ialloc_cluster_alignment(&args) - 1;
299 /* Allow space for the inode btree to split. */
300 args.minleft = args.mp->m_in_maxlevels - 1;
301 if ((error = xfs_alloc_vextent(&args)))
304 args.fsbno = NULLFSBLOCK;
306 if (unlikely(args.fsbno == NULLFSBLOCK)) {
308 * Set the alignment for the allocation.
309 * If stripe alignment is turned on then align at stripe unit
311 * If the cluster size is smaller than a filesystem block
312 * then we're doing I/O for inodes in filesystem block size
313 * pieces, so don't need alignment anyway.
316 if (args.mp->m_sinoalign) {
317 ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN));
318 args.alignment = args.mp->m_dalign;
321 args.alignment = xfs_ialloc_cluster_alignment(&args);
323 * Need to figure out where to allocate the inode blocks.
324 * Ideally they should be spaced out through the a.g.
325 * For now, just allocate blocks up front.
327 args.agbno = be32_to_cpu(agi->agi_root);
328 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
330 * Allocate a fixed-size extent of inodes.
332 args.type = XFS_ALLOCTYPE_NEAR_BNO;
333 args.mod = args.total = args.wasdel = args.isfl =
334 args.userdata = args.minalignslop = 0;
337 * Allow space for the inode btree to split.
339 args.minleft = args.mp->m_in_maxlevels - 1;
340 if ((error = xfs_alloc_vextent(&args)))
345 * If stripe alignment is turned on, then try again with cluster
348 if (isaligned && args.fsbno == NULLFSBLOCK) {
349 args.type = XFS_ALLOCTYPE_NEAR_BNO;
350 args.agbno = be32_to_cpu(agi->agi_root);
351 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
352 args.alignment = xfs_ialloc_cluster_alignment(&args);
353 if ((error = xfs_alloc_vextent(&args)))
357 if (args.fsbno == NULLFSBLOCK) {
361 ASSERT(args.len == args.minlen);
364 * Stamp and write the inode buffers.
366 * Seed the new inode cluster with a random generation number. This
367 * prevents short-term reuse of generation numbers if a chunk is
368 * freed and then immediately reallocated. We use random numbers
369 * rather than a linear progression to prevent the next generation
370 * number from being easily guessable.
372 xfs_ialloc_inode_init(args.mp, tp, agno, args.agbno, args.len,
376 * Convert the results.
378 newino = XFS_OFFBNO_TO_AGINO(args.mp, args.agbno, 0);
379 be32_add_cpu(&agi->agi_count, newlen);
380 be32_add_cpu(&agi->agi_freecount, newlen);
381 down_read(&args.mp->m_peraglock);
382 args.mp->m_perag[agno].pagi_freecount += newlen;
383 up_read(&args.mp->m_peraglock);
384 agi->agi_newino = cpu_to_be32(newino);
387 * Insert records describing the new inode chunk into the btree.
389 cur = xfs_inobt_init_cursor(args.mp, tp, agbp, agno);
390 for (thisino = newino;
391 thisino < newino + newlen;
392 thisino += XFS_INODES_PER_CHUNK) {
393 if ((error = xfs_inobt_lookup_eq(cur, thisino,
394 XFS_INODES_PER_CHUNK, XFS_INOBT_ALL_FREE, &i))) {
395 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
399 if ((error = xfs_btree_insert(cur, &i))) {
400 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
405 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
407 * Log allocation group header fields
409 xfs_ialloc_log_agi(tp, agbp,
410 XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO);
412 * Modify/log superblock values for inode count and inode free count.
414 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen);
415 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen);
420 STATIC_INLINE xfs_agnumber_t
426 spin_lock(&mp->m_agirotor_lock);
427 agno = mp->m_agirotor;
428 if (++mp->m_agirotor == mp->m_maxagi)
430 spin_unlock(&mp->m_agirotor_lock);
436 * Select an allocation group to look for a free inode in, based on the parent
437 * inode and then mode. Return the allocation group buffer.
439 STATIC xfs_buf_t * /* allocation group buffer */
440 xfs_ialloc_ag_select(
441 xfs_trans_t *tp, /* transaction pointer */
442 xfs_ino_t parent, /* parent directory inode number */
443 mode_t mode, /* bits set to indicate file type */
444 int okalloc) /* ok to allocate more space */
446 xfs_buf_t *agbp; /* allocation group header buffer */
447 xfs_agnumber_t agcount; /* number of ag's in the filesystem */
448 xfs_agnumber_t agno; /* current ag number */
449 int flags; /* alloc buffer locking flags */
450 xfs_extlen_t ineed; /* blocks needed for inode allocation */
451 xfs_extlen_t longest = 0; /* longest extent available */
452 xfs_mount_t *mp; /* mount point structure */
453 int needspace; /* file mode implies space allocated */
454 xfs_perag_t *pag; /* per allocation group data */
455 xfs_agnumber_t pagno; /* parent (starting) ag number */
458 * Files of these types need at least one block if length > 0
459 * (and they won't fit in the inode, but that's hard to figure out).
461 needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode);
463 agcount = mp->m_maxagi;
465 pagno = xfs_ialloc_next_ag(mp);
467 pagno = XFS_INO_TO_AGNO(mp, parent);
468 if (pagno >= agcount)
471 ASSERT(pagno < agcount);
473 * Loop through allocation groups, looking for one with a little
474 * free space in it. Note we don't look for free inodes, exactly.
475 * Instead, we include whether there is a need to allocate inodes
476 * to mean that blocks must be allocated for them,
477 * if none are currently free.
480 flags = XFS_ALLOC_FLAG_TRYLOCK;
481 down_read(&mp->m_peraglock);
483 pag = &mp->m_perag[agno];
484 if (!pag->pagi_init) {
485 if (xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
492 if (!pag->pagi_inodeok) {
493 xfs_ialloc_next_ag(mp);
498 * Is there enough free space for the file plus a block
499 * of inodes (if we need to allocate some)?
501 ineed = pag->pagi_freecount ? 0 : XFS_IALLOC_BLOCKS(mp);
502 if (ineed && !pag->pagf_init) {
504 xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
508 (void)xfs_alloc_pagf_init(mp, tp, agno, flags);
510 if (!ineed || pag->pagf_init) {
511 if (ineed && !(longest = pag->pagf_longest))
512 longest = pag->pagf_flcount > 0;
514 (pag->pagf_freeblks >= needspace + ineed &&
518 xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
522 up_read(&mp->m_peraglock);
528 xfs_trans_brelse(tp, agbp);
531 * No point in iterating over the rest, if we're shutting
534 if (XFS_FORCED_SHUTDOWN(mp)) {
535 up_read(&mp->m_peraglock);
543 up_read(&mp->m_peraglock);
552 * Visible inode allocation functions.
556 * Allocate an inode on disk.
557 * Mode is used to tell whether the new inode will need space, and whether
560 * The arguments IO_agbp and alloc_done are defined to work within
561 * the constraint of one allocation per transaction.
562 * xfs_dialloc() is designed to be called twice if it has to do an
563 * allocation to make more free inodes. On the first call,
564 * IO_agbp should be set to NULL. If an inode is available,
565 * i.e., xfs_dialloc() did not need to do an allocation, an inode
566 * number is returned. In this case, IO_agbp would be set to the
567 * current ag_buf and alloc_done set to false.
568 * If an allocation needed to be done, xfs_dialloc would return
569 * the current ag_buf in IO_agbp and set alloc_done to true.
570 * The caller should then commit the current transaction, allocate a new
571 * transaction, and call xfs_dialloc() again, passing in the previous
572 * value of IO_agbp. IO_agbp should be held across the transactions.
573 * Since the agbp is locked across the two calls, the second call is
574 * guaranteed to have a free inode available.
576 * Once we successfully pick an inode its number is returned and the
577 * on-disk data structures are updated. The inode itself is not read
578 * in, since doing so would break ordering constraints with xfs_reclaim.
582 xfs_trans_t *tp, /* transaction pointer */
583 xfs_ino_t parent, /* parent inode (directory) */
584 mode_t mode, /* mode bits for new inode */
585 int okalloc, /* ok to allocate more space */
586 xfs_buf_t **IO_agbp, /* in/out ag header's buffer */
587 boolean_t *alloc_done, /* true if we needed to replenish
589 xfs_ino_t *inop) /* inode number allocated */
591 xfs_agnumber_t agcount; /* number of allocation groups */
592 xfs_buf_t *agbp; /* allocation group header's buffer */
593 xfs_agnumber_t agno; /* allocation group number */
594 xfs_agi_t *agi; /* allocation group header structure */
595 xfs_btree_cur_t *cur; /* inode allocation btree cursor */
596 int error; /* error return value */
597 int i; /* result code */
598 int ialloced; /* inode allocation status */
599 int noroom = 0; /* no space for inode blk allocation */
600 xfs_ino_t ino; /* fs-relative inode to be returned */
602 int j; /* result code */
603 xfs_mount_t *mp; /* file system mount structure */
604 int offset; /* index of inode in chunk */
605 xfs_agino_t pagino; /* parent's a.g. relative inode # */
606 xfs_agnumber_t pagno; /* parent's allocation group number */
607 xfs_inobt_rec_incore_t rec; /* inode allocation record */
608 xfs_agnumber_t tagno; /* testing allocation group number */
609 xfs_btree_cur_t *tcur; /* temp cursor */
610 xfs_inobt_rec_incore_t trec; /* temp inode allocation record */
613 if (*IO_agbp == NULL) {
615 * We do not have an agbp, so select an initial allocation
616 * group for inode allocation.
618 agbp = xfs_ialloc_ag_select(tp, parent, mode, okalloc);
620 * Couldn't find an allocation group satisfying the
627 agi = XFS_BUF_TO_AGI(agbp);
628 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
631 * Continue where we left off before. In this case, we
632 * know that the allocation group has free inodes.
635 agi = XFS_BUF_TO_AGI(agbp);
636 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
637 ASSERT(be32_to_cpu(agi->agi_freecount) > 0);
640 agcount = mp->m_sb.sb_agcount;
641 agno = be32_to_cpu(agi->agi_seqno);
643 pagno = XFS_INO_TO_AGNO(mp, parent);
644 pagino = XFS_INO_TO_AGINO(mp, parent);
647 * If we have already hit the ceiling of inode blocks then clear
648 * okalloc so we scan all available agi structures for a free
652 if (mp->m_maxicount &&
653 mp->m_sb.sb_icount + XFS_IALLOC_INODES(mp) > mp->m_maxicount) {
659 * Loop until we find an allocation group that either has free inodes
660 * or in which we can allocate some inodes. Iterate through the
661 * allocation groups upward, wrapping at the end.
663 *alloc_done = B_FALSE;
664 while (!agi->agi_freecount) {
666 * Don't do anything if we're not supposed to allocate
667 * any blocks, just go on to the next ag.
671 * Try to allocate some new inodes in the allocation
674 if ((error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced))) {
675 xfs_trans_brelse(tp, agbp);
676 if (error == ENOSPC) {
684 * We successfully allocated some inodes, return
685 * the current context to the caller so that it
686 * can commit the current transaction and call
687 * us again where we left off.
689 ASSERT(be32_to_cpu(agi->agi_freecount) > 0);
690 *alloc_done = B_TRUE;
697 * If it failed, give up on this ag.
699 xfs_trans_brelse(tp, agbp);
701 * Go on to the next ag: get its ag header.
704 if (++tagno == agcount)
708 return noroom ? ENOSPC : 0;
710 down_read(&mp->m_peraglock);
711 if (mp->m_perag[tagno].pagi_inodeok == 0) {
712 up_read(&mp->m_peraglock);
715 error = xfs_ialloc_read_agi(mp, tp, tagno, &agbp);
716 up_read(&mp->m_peraglock);
719 agi = XFS_BUF_TO_AGI(agbp);
720 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
723 * Here with an allocation group that has a free inode.
724 * Reset agno since we may have chosen a new ag in the
729 cur = xfs_inobt_init_cursor(mp, tp, agbp, be32_to_cpu(agi->agi_seqno));
731 * If pagino is 0 (this is the root inode allocation) use newino.
732 * This must work because we've just allocated some.
735 pagino = be32_to_cpu(agi->agi_newino);
737 if (cur->bc_nlevels == 1) {
740 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
742 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
744 error = xfs_inobt_get_rec(cur, &rec, &i);
747 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
748 freecount += rec.ir_freecount;
749 if ((error = xfs_btree_increment(cur, 0, &i)))
753 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
754 XFS_FORCED_SHUTDOWN(mp));
758 * If in the same a.g. as the parent, try to get near the parent.
761 if ((error = xfs_inobt_lookup_le(cur, pagino, 0, 0, &i)))
764 (error = xfs_inobt_get_rec(cur, &rec, &j)) == 0 &&
766 rec.ir_freecount > 0) {
768 * Found a free inode in the same chunk
773 * In the same a.g. as parent, but parent's chunk is full.
776 int doneleft; /* done, to the left */
777 int doneright; /* done, to the right */
784 * Duplicate the cursor, search left & right
787 if ((error = xfs_btree_dup_cursor(cur, &tcur)))
790 * Search left with tcur, back up 1 record.
792 if ((error = xfs_btree_decrement(tcur, 0, &i)))
796 error = xfs_inobt_get_rec(tcur, &trec, &i);
799 XFS_WANT_CORRUPTED_GOTO(i == 1, error1);
802 * Search right with cur, go forward 1 record.
804 if ((error = xfs_btree_increment(cur, 0, &i)))
808 error = xfs_inobt_get_rec(cur, &rec, &i);
811 XFS_WANT_CORRUPTED_GOTO(i == 1, error1);
814 * Loop until we find the closest inode chunk
817 while (!doneleft || !doneright) {
818 int useleft; /* using left inode
822 * Figure out which block is closer,
825 if (!doneleft && !doneright)
829 XFS_INODES_PER_CHUNK - 1) <
830 rec.ir_startino - pagino;
834 * If checking the left, does it have
837 if (useleft && trec.ir_freecount) {
839 * Yes, set it up as the chunk to use.
842 xfs_btree_del_cursor(cur,
848 * If checking the right, does it have
851 if (!useleft && rec.ir_freecount) {
853 * Yes, it's already set up.
855 xfs_btree_del_cursor(tcur,
860 * If used the left, get another one
864 if ((error = xfs_btree_decrement(tcur, 0,
869 error = xfs_inobt_get_rec(
873 XFS_WANT_CORRUPTED_GOTO(i == 1,
878 * If used the right, get another one
882 if ((error = xfs_btree_increment(cur, 0,
887 error = xfs_inobt_get_rec(
891 XFS_WANT_CORRUPTED_GOTO(i == 1,
896 ASSERT(!doneleft || !doneright);
900 * In a different a.g. from the parent.
901 * See if the most recently allocated block has any free.
903 else if (be32_to_cpu(agi->agi_newino) != NULLAGINO) {
904 if ((error = xfs_inobt_lookup_eq(cur,
905 be32_to_cpu(agi->agi_newino), 0, 0, &i)))
908 (error = xfs_inobt_get_rec(cur, &rec, &j)) == 0 &&
910 rec.ir_freecount > 0) {
912 * The last chunk allocated in the group still has
917 * None left in the last group, search the whole a.g.
922 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
926 error = xfs_inobt_get_rec(cur, &rec, &i);
929 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
930 if (rec.ir_freecount > 0)
932 if ((error = xfs_btree_increment(cur, 0, &i)))
934 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
938 offset = xfs_ialloc_find_free(&rec.ir_free);
940 ASSERT(offset < XFS_INODES_PER_CHUNK);
941 ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
942 XFS_INODES_PER_CHUNK) == 0);
943 ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
944 rec.ir_free &= ~XFS_INOBT_MASK(offset);
946 error = xfs_inobt_update(cur, &rec);
949 be32_add_cpu(&agi->agi_freecount, -1);
950 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
951 down_read(&mp->m_peraglock);
952 mp->m_perag[tagno].pagi_freecount--;
953 up_read(&mp->m_peraglock);
955 if (cur->bc_nlevels == 1) {
958 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
961 error = xfs_inobt_get_rec(cur, &rec, &i);
964 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
965 freecount += rec.ir_freecount;
966 if ((error = xfs_btree_increment(cur, 0, &i)))
969 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
970 XFS_FORCED_SHUTDOWN(mp));
973 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
974 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
978 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
980 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
985 * Free disk inode. Carefully avoids touching the incore inode, all
986 * manipulations incore are the caller's responsibility.
987 * The on-disk inode is not changed by this operation, only the
988 * btree (free inode mask) is changed.
992 xfs_trans_t *tp, /* transaction pointer */
993 xfs_ino_t inode, /* inode to be freed */
994 xfs_bmap_free_t *flist, /* extents to free */
995 int *delete, /* set if inode cluster was deleted */
996 xfs_ino_t *first_ino) /* first inode in deleted cluster */
999 xfs_agblock_t agbno; /* block number containing inode */
1000 xfs_buf_t *agbp; /* buffer containing allocation group header */
1001 xfs_agino_t agino; /* inode number relative to allocation group */
1002 xfs_agnumber_t agno; /* allocation group number */
1003 xfs_agi_t *agi; /* allocation group header */
1004 xfs_btree_cur_t *cur; /* inode btree cursor */
1005 int error; /* error return value */
1006 int i; /* result code */
1007 int ilen; /* inodes in an inode cluster */
1008 xfs_mount_t *mp; /* mount structure for filesystem */
1009 int off; /* offset of inode in inode chunk */
1010 xfs_inobt_rec_incore_t rec; /* btree record */
1015 * Break up inode number into its components.
1017 agno = XFS_INO_TO_AGNO(mp, inode);
1018 if (agno >= mp->m_sb.sb_agcount) {
1020 "xfs_difree: agno >= mp->m_sb.sb_agcount (%d >= %d) on %s. Returning EINVAL.",
1021 agno, mp->m_sb.sb_agcount, mp->m_fsname);
1023 return XFS_ERROR(EINVAL);
1025 agino = XFS_INO_TO_AGINO(mp, inode);
1026 if (inode != XFS_AGINO_TO_INO(mp, agno, agino)) {
1028 "xfs_difree: inode != XFS_AGINO_TO_INO() "
1029 "(%llu != %llu) on %s. Returning EINVAL.",
1030 (unsigned long long)inode,
1031 (unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino),
1034 return XFS_ERROR(EINVAL);
1036 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1037 if (agbno >= mp->m_sb.sb_agblocks) {
1039 "xfs_difree: agbno >= mp->m_sb.sb_agblocks (%d >= %d) on %s. Returning EINVAL.",
1040 agbno, mp->m_sb.sb_agblocks, mp->m_fsname);
1042 return XFS_ERROR(EINVAL);
1045 * Get the allocation group header.
1047 down_read(&mp->m_peraglock);
1048 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1049 up_read(&mp->m_peraglock);
1052 "xfs_difree: xfs_ialloc_read_agi() returned an error %d on %s. Returning error.",
1053 error, mp->m_fsname);
1056 agi = XFS_BUF_TO_AGI(agbp);
1057 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
1058 ASSERT(agbno < be32_to_cpu(agi->agi_length));
1060 * Initialize the cursor.
1062 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno);
1064 if (cur->bc_nlevels == 1) {
1067 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
1070 error = xfs_inobt_get_rec(cur, &rec, &i);
1074 freecount += rec.ir_freecount;
1075 if ((error = xfs_btree_increment(cur, 0, &i)))
1079 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
1080 XFS_FORCED_SHUTDOWN(mp));
1084 * Look for the entry describing this inode.
1086 if ((error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i))) {
1088 "xfs_difree: xfs_inobt_lookup_le returned() an error %d on %s. Returning error.",
1089 error, mp->m_fsname);
1092 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1093 error = xfs_inobt_get_rec(cur, &rec, &i);
1096 "xfs_difree: xfs_inobt_get_rec() returned an error %d on %s. Returning error.",
1097 error, mp->m_fsname);
1100 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1102 * Get the offset in the inode chunk.
1104 off = agino - rec.ir_startino;
1105 ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK);
1106 ASSERT(!(rec.ir_free & XFS_INOBT_MASK(off)));
1108 * Mark the inode free & increment the count.
1110 rec.ir_free |= XFS_INOBT_MASK(off);
1114 * When an inode cluster is free, it becomes eligible for removal
1116 if (!(mp->m_flags & XFS_MOUNT_IKEEP) &&
1117 (rec.ir_freecount == XFS_IALLOC_INODES(mp))) {
1120 *first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino);
1123 * Remove the inode cluster from the AGI B+Tree, adjust the
1124 * AGI and Superblock inode counts, and mark the disk space
1125 * to be freed when the transaction is committed.
1127 ilen = XFS_IALLOC_INODES(mp);
1128 be32_add_cpu(&agi->agi_count, -ilen);
1129 be32_add_cpu(&agi->agi_freecount, -(ilen - 1));
1130 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT);
1131 down_read(&mp->m_peraglock);
1132 mp->m_perag[agno].pagi_freecount -= ilen - 1;
1133 up_read(&mp->m_peraglock);
1134 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen);
1135 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1));
1137 if ((error = xfs_btree_delete(cur, &i))) {
1138 cmn_err(CE_WARN, "xfs_difree: xfs_btree_delete returned an error %d on %s.\n",
1139 error, mp->m_fsname);
1143 xfs_bmap_add_free(XFS_AGB_TO_FSB(mp,
1144 agno, XFS_INO_TO_AGBNO(mp,rec.ir_startino)),
1145 XFS_IALLOC_BLOCKS(mp), flist, mp);
1149 error = xfs_inobt_update(cur, &rec);
1152 "xfs_difree: xfs_inobt_update returned an error %d on %s.",
1153 error, mp->m_fsname);
1158 * Change the inode free counts and log the ag/sb changes.
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);
1169 if (cur->bc_nlevels == 1) {
1172 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
1175 error = xfs_inobt_get_rec(cur, &rec, &i);
1179 freecount += rec.ir_freecount;
1180 if ((error = xfs_btree_increment(cur, 0, &i)))
1184 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
1185 XFS_FORCED_SHUTDOWN(mp));
1188 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1192 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1197 * Return the location of the inode in imap, for mapping it into a buffer.
1201 xfs_mount_t *mp, /* file system mount structure */
1202 xfs_trans_t *tp, /* transaction pointer */
1203 xfs_ino_t ino, /* inode to locate */
1204 struct xfs_imap *imap, /* location map structure */
1205 uint flags) /* flags for inode btree lookup */
1207 xfs_agblock_t agbno; /* block number of inode in the alloc group */
1208 xfs_agino_t agino; /* inode number within alloc group */
1209 xfs_agnumber_t agno; /* allocation group number */
1210 int blks_per_cluster; /* num blocks per inode cluster */
1211 xfs_agblock_t chunk_agbno; /* first block in inode chunk */
1212 xfs_agblock_t cluster_agbno; /* first block in inode cluster */
1213 int error; /* error code */
1214 int offset; /* index of inode in its buffer */
1215 int offset_agbno; /* blks from chunk start to inode */
1217 ASSERT(ino != NULLFSINO);
1220 * Split up the inode number into its parts.
1222 agno = XFS_INO_TO_AGNO(mp, ino);
1223 agino = XFS_INO_TO_AGINO(mp, ino);
1224 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1225 if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks ||
1226 ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1228 /* no diagnostics for bulkstat, ino comes from userspace */
1229 if (flags & XFS_IGET_BULKSTAT)
1230 return XFS_ERROR(EINVAL);
1231 if (agno >= mp->m_sb.sb_agcount) {
1232 xfs_fs_cmn_err(CE_ALERT, mp,
1233 "xfs_imap: agno (%d) >= "
1234 "mp->m_sb.sb_agcount (%d)",
1235 agno, mp->m_sb.sb_agcount);
1237 if (agbno >= mp->m_sb.sb_agblocks) {
1238 xfs_fs_cmn_err(CE_ALERT, mp,
1239 "xfs_imap: agbno (0x%llx) >= "
1240 "mp->m_sb.sb_agblocks (0x%lx)",
1241 (unsigned long long) agbno,
1242 (unsigned long) mp->m_sb.sb_agblocks);
1244 if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1245 xfs_fs_cmn_err(CE_ALERT, mp,
1246 "xfs_imap: ino (0x%llx) != "
1247 "XFS_AGINO_TO_INO(mp, agno, agino) "
1249 ino, XFS_AGINO_TO_INO(mp, agno, agino));
1253 return XFS_ERROR(EINVAL);
1257 * If the inode cluster size is the same as the blocksize or
1258 * smaller we get to the buffer by simple arithmetics.
1260 if (XFS_INODE_CLUSTER_SIZE(mp) <= mp->m_sb.sb_blocksize) {
1261 offset = XFS_INO_TO_OFFSET(mp, ino);
1262 ASSERT(offset < mp->m_sb.sb_inopblock);
1264 imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, agbno);
1265 imap->im_len = XFS_FSB_TO_BB(mp, 1);
1266 imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
1270 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_blocklog;
1273 * If we get a block number passed from bulkstat we can use it to
1274 * find the buffer easily.
1276 if (imap->im_blkno) {
1277 offset = XFS_INO_TO_OFFSET(mp, ino);
1278 ASSERT(offset < mp->m_sb.sb_inopblock);
1280 cluster_agbno = xfs_daddr_to_agbno(mp, imap->im_blkno);
1281 offset += (agbno - cluster_agbno) * mp->m_sb.sb_inopblock;
1283 imap->im_len = XFS_FSB_TO_BB(mp, blks_per_cluster);
1284 imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
1289 * If the inode chunks are aligned then use simple maths to
1290 * find the location. Otherwise we have to do a btree
1291 * lookup to find the location.
1293 if (mp->m_inoalign_mask) {
1294 offset_agbno = agbno & mp->m_inoalign_mask;
1295 chunk_agbno = agbno - offset_agbno;
1297 xfs_btree_cur_t *cur; /* inode btree cursor */
1298 xfs_inobt_rec_incore_t chunk_rec;
1299 xfs_buf_t *agbp; /* agi buffer */
1300 int i; /* temp state */
1302 down_read(&mp->m_peraglock);
1303 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1304 up_read(&mp->m_peraglock);
1306 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: "
1307 "xfs_ialloc_read_agi() returned "
1308 "error %d, agno %d",
1313 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno);
1314 error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i);
1316 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: "
1317 "xfs_inobt_lookup_le() failed");
1321 error = xfs_inobt_get_rec(cur, &chunk_rec, &i);
1323 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: "
1324 "xfs_inobt_get_rec() failed");
1329 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: "
1330 "xfs_inobt_get_rec() failed");
1332 error = XFS_ERROR(EINVAL);
1335 xfs_trans_brelse(tp, agbp);
1336 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1339 chunk_agbno = XFS_AGINO_TO_AGBNO(mp, chunk_rec.ir_startino);
1340 offset_agbno = agbno - chunk_agbno;
1343 ASSERT(agbno >= chunk_agbno);
1344 cluster_agbno = chunk_agbno +
1345 ((offset_agbno / blks_per_cluster) * blks_per_cluster);
1346 offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
1347 XFS_INO_TO_OFFSET(mp, ino);
1349 imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, cluster_agbno);
1350 imap->im_len = XFS_FSB_TO_BB(mp, blks_per_cluster);
1351 imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
1354 * If the inode number maps to a block outside the bounds
1355 * of the file system then return NULL rather than calling
1356 * read_buf and panicing when we get an error from the
1359 if ((imap->im_blkno + imap->im_len) >
1360 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) {
1361 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: "
1362 "(imap->im_blkno (0x%llx) + imap->im_len (0x%llx)) > "
1363 " XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks) (0x%llx)",
1364 (unsigned long long) imap->im_blkno,
1365 (unsigned long long) imap->im_len,
1366 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks));
1367 return XFS_ERROR(EINVAL);
1374 * Compute and fill in value of m_in_maxlevels.
1377 xfs_ialloc_compute_maxlevels(
1378 xfs_mount_t *mp) /* file system mount structure */
1386 maxleafents = (1LL << XFS_INO_AGINO_BITS(mp)) >>
1387 XFS_INODES_PER_CHUNK_LOG;
1388 minleafrecs = mp->m_alloc_mnr[0];
1389 minnoderecs = mp->m_alloc_mnr[1];
1390 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
1391 for (level = 1; maxblocks > 1; level++)
1392 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
1393 mp->m_in_maxlevels = level;
1397 * Log specified fields for the ag hdr (inode section)
1401 xfs_trans_t *tp, /* transaction pointer */
1402 xfs_buf_t *bp, /* allocation group header buffer */
1403 int fields) /* bitmask of fields to log */
1405 int first; /* first byte number */
1406 int last; /* last byte number */
1407 static const short offsets[] = { /* field starting offsets */
1408 /* keep in sync with bit definitions */
1409 offsetof(xfs_agi_t, agi_magicnum),
1410 offsetof(xfs_agi_t, agi_versionnum),
1411 offsetof(xfs_agi_t, agi_seqno),
1412 offsetof(xfs_agi_t, agi_length),
1413 offsetof(xfs_agi_t, agi_count),
1414 offsetof(xfs_agi_t, agi_root),
1415 offsetof(xfs_agi_t, agi_level),
1416 offsetof(xfs_agi_t, agi_freecount),
1417 offsetof(xfs_agi_t, agi_newino),
1418 offsetof(xfs_agi_t, agi_dirino),
1419 offsetof(xfs_agi_t, agi_unlinked),
1423 xfs_agi_t *agi; /* allocation group header */
1425 agi = XFS_BUF_TO_AGI(bp);
1426 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
1429 * Compute byte offsets for the first and last fields.
1431 xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS, &first, &last);
1433 * Log the allocation group inode header buffer.
1435 xfs_trans_log_buf(tp, bp, first, last);
1440 xfs_check_agi_unlinked(
1441 struct xfs_agi *agi)
1445 for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++)
1446 ASSERT(agi->agi_unlinked[i]);
1449 #define xfs_check_agi_unlinked(agi)
1453 * Read in the allocation group header (inode allocation section)
1457 struct xfs_mount *mp, /* file system mount structure */
1458 struct xfs_trans *tp, /* transaction pointer */
1459 xfs_agnumber_t agno, /* allocation group number */
1460 struct xfs_buf **bpp) /* allocation group hdr buf */
1462 struct xfs_agi *agi; /* allocation group header */
1463 int agi_ok; /* agi is consistent */
1466 ASSERT(agno != NULLAGNUMBER);
1468 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
1469 XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
1470 XFS_FSS_TO_BB(mp, 1), 0, bpp);
1474 ASSERT(*bpp && !XFS_BUF_GETERROR(*bpp));
1475 agi = XFS_BUF_TO_AGI(*bpp);
1478 * Validate the magic number of the agi block.
1480 agi_ok = be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC &&
1481 XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum)) &&
1482 be32_to_cpu(agi->agi_seqno) == agno;
1483 if (unlikely(XFS_TEST_ERROR(!agi_ok, mp, XFS_ERRTAG_IALLOC_READ_AGI,
1484 XFS_RANDOM_IALLOC_READ_AGI))) {
1485 XFS_CORRUPTION_ERROR("xfs_read_agi", XFS_ERRLEVEL_LOW,
1487 xfs_trans_brelse(tp, *bpp);
1488 return XFS_ERROR(EFSCORRUPTED);
1491 XFS_BUF_SET_VTYPE_REF(*bpp, B_FS_AGI, XFS_AGI_REF);
1493 xfs_check_agi_unlinked(agi);
1498 xfs_ialloc_read_agi(
1499 struct xfs_mount *mp, /* file system mount structure */
1500 struct xfs_trans *tp, /* transaction pointer */
1501 xfs_agnumber_t agno, /* allocation group number */
1502 struct xfs_buf **bpp) /* allocation group hdr buf */
1504 struct xfs_agi *agi; /* allocation group header */
1505 struct xfs_perag *pag; /* per allocation group data */
1508 error = xfs_read_agi(mp, tp, agno, bpp);
1512 agi = XFS_BUF_TO_AGI(*bpp);
1513 pag = &mp->m_perag[agno];
1515 if (!pag->pagi_init) {
1516 pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
1517 pag->pagi_count = be32_to_cpu(agi->agi_count);
1522 * It's possible for these to be out of sync if
1523 * we are in the middle of a forced shutdown.
1525 ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) ||
1526 XFS_FORCED_SHUTDOWN(mp));
1531 * Read in the agi to initialise the per-ag data in the mount structure
1534 xfs_ialloc_pagi_init(
1535 xfs_mount_t *mp, /* file system mount structure */
1536 xfs_trans_t *tp, /* transaction pointer */
1537 xfs_agnumber_t agno) /* allocation group number */
1539 xfs_buf_t *bp = NULL;
1542 error = xfs_ialloc_read_agi(mp, tp, agno, &bp);
1546 xfs_trans_brelse(tp, bp);