* buf_alloc: * * Allocate an empty buffer header. */
| 1645 | * Allocate an empty buffer header. |
| 1646 | */ |
| 1647 | static struct buf * |
| 1648 | buf_alloc(struct bufdomain *bd) |
| 1649 | { |
| 1650 | struct buf *bp; |
| 1651 | int freebufs, error; |
| 1652 | |
| 1653 | /* |
| 1654 | * We can only run out of bufs in the buf zone if the average buf |
| 1655 | * is less than BKVASIZE. In this case the actual wait/block will |
| 1656 | * come from buf_reycle() failing to flush one of these small bufs. |
| 1657 | */ |
| 1658 | bp = NULL; |
| 1659 | freebufs = atomic_fetchadd_int(&bd->bd_freebuffers, -1); |
| 1660 | if (freebufs > 0) |
| 1661 | bp = uma_zalloc(buf_zone, M_NOWAIT); |
| 1662 | if (bp == NULL) { |
| 1663 | atomic_add_int(&bd->bd_freebuffers, 1); |
| 1664 | bufspace_daemon_wakeup(bd); |
| 1665 | counter_u64_add(numbufallocfails, 1); |
| 1666 | return (NULL); |
| 1667 | } |
| 1668 | /* |
| 1669 | * Wake-up the bufspace daemon on transition below threshold. |
| 1670 | */ |
| 1671 | if (freebufs == bd->bd_lofreebuffers) |
| 1672 | bufspace_daemon_wakeup(bd); |
| 1673 | |
| 1674 | error = BUF_LOCK(bp, LK_EXCLUSIVE, NULL); |
| 1675 | KASSERT(error == 0, ("%s: BUF_LOCK on free buf %p: %d.", __func__, bp, |
| 1676 | error)); |
| 1677 | (void)error; |
| 1678 | |
| 1679 | KASSERT(bp->b_vp == NULL, |
| 1680 | ("bp: %p still has vnode %p.", bp, bp->b_vp)); |
| 1681 | KASSERT((bp->b_flags & (B_DELWRI | B_NOREUSE)) == 0, |
| 1682 | ("invalid buffer %p flags %#x", bp, bp->b_flags)); |
| 1683 | KASSERT((bp->b_xflags & (BX_VNCLEAN|BX_VNDIRTY)) == 0, |
| 1684 | ("bp: %p still on a buffer list. xflags %X", bp, bp->b_xflags)); |
| 1685 | KASSERT(bp->b_npages == 0, |
| 1686 | ("bp: %p still has %d vm pages\n", bp, bp->b_npages)); |
| 1687 | KASSERT(bp->b_kvasize == 0, ("bp: %p still has kva\n", bp)); |
| 1688 | KASSERT(bp->b_bufsize == 0, ("bp: %p still has bufspace\n", bp)); |
| 1689 | MPASS((bp->b_flags & B_MAXPHYS) == 0); |
| 1690 | |
| 1691 | bp->b_domain = BD_DOMAIN(bd); |
| 1692 | bp->b_flags = 0; |
| 1693 | bp->b_ioflags = 0; |
| 1694 | bp->b_xflags = 0; |
| 1695 | bp->b_vflags = 0; |
| 1696 | bp->b_vp = NULL; |
| 1697 | bp->b_blkno = bp->b_lblkno = 0; |
| 1698 | bp->b_offset = NOOFFSET; |
| 1699 | bp->b_iodone = 0; |
| 1700 | bp->b_error = 0; |
| 1701 | bp->b_resid = 0; |
| 1702 | bp->b_bcount = 0; |
| 1703 | bp->b_npages = 0; |
| 1704 | bp->b_dirtyoff = bp->b_dirtyend = 0; |
no test coverage detected