Пример #1
0
/*
 * Flush all pending data to disk.  This operation will block.
 */
static int
alq_doio(struct alq *alq)
{
    struct thread *td;
    struct mount *mp;
    struct vnode *vp;
    struct uio auio;
    struct iovec aiov[2];
    int totlen;
    int iov;
    int vfslocked;
    int wrapearly;

    KASSERT((HAS_PENDING_DATA(alq)), ("%s: queue empty!", __func__));

    vp = alq->aq_vp;
    td = curthread;
    totlen = 0;
    iov = 1;
    wrapearly = alq->aq_wrapearly;

    bzero(&aiov, sizeof(aiov));
    bzero(&auio, sizeof(auio));

    /* Start the write from the location of our buffer tail pointer. */
    aiov[0].iov_base = alq->aq_entbuf + alq->aq_writetail;

    if (alq->aq_writetail < alq->aq_writehead) {
        /* Buffer not wrapped. */
        totlen = aiov[0].iov_len = alq->aq_writehead - alq->aq_writetail;
    } else if (alq->aq_writehead == 0) {
        /* Buffer not wrapped (special case to avoid an empty iov). */
        totlen = aiov[0].iov_len = alq->aq_buflen - alq->aq_writetail -
                                   wrapearly;
    } else {
        /*
         * Buffer wrapped, requires 2 aiov entries:
         * - first is from writetail to end of buffer
         * - second is from start of buffer to writehead
         */
        aiov[0].iov_len = alq->aq_buflen - alq->aq_writetail -
                          wrapearly;
        iov++;
        aiov[1].iov_base = alq->aq_entbuf;
        aiov[1].iov_len =  alq->aq_writehead;
        totlen = aiov[0].iov_len + aiov[1].iov_len;
    }

    alq->aq_flags |= AQ_FLUSHING;
    ALQ_UNLOCK(alq);

    auio.uio_iov = &aiov[0];
    auio.uio_offset = 0;
    auio.uio_segflg = UIO_SYSSPACE;
    auio.uio_rw = UIO_WRITE;
    auio.uio_iovcnt = iov;
    auio.uio_resid = totlen;
    auio.uio_td = td;

    /*
     * Do all of the junk required to write now.
     */
    vfslocked = VFS_LOCK_GIANT(vp->v_mount);
    vn_start_write(vp, &mp, V_WAIT);
    vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    /*
     * XXX: VOP_WRITE error checks are ignored.
     */
#ifdef MAC
    if (mac_vnode_check_write(alq->aq_cred, NOCRED, vp) == 0)
#endif
        VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, alq->aq_cred);
    VOP_UNLOCK(vp, 0);
    vn_finished_write(mp);
    VFS_UNLOCK_GIANT(vfslocked);

    ALQ_LOCK(alq);
    alq->aq_flags &= ~AQ_FLUSHING;

    /* Adjust writetail as required, taking into account wrapping. */
    alq->aq_writetail = (alq->aq_writetail + totlen + wrapearly) %
                        alq->aq_buflen;
    alq->aq_freebytes += totlen + wrapearly;

    /*
     * If we just flushed part of the buffer which wrapped, reset the
     * wrapearly indicator.
     */
    if (wrapearly)
        alq->aq_wrapearly = 0;

    /*
     * If we just flushed the buffer completely, reset indexes to 0 to
     * minimise buffer wraps.
     * This is also required to ensure alq_getn() can't wedge itself.
     */
    if (!HAS_PENDING_DATA(alq))
        alq->aq_writehead = alq->aq_writetail = 0;

    KASSERT((alq->aq_writetail >= 0 && alq->aq_writetail < alq->aq_buflen),
            ("%s: aq_writetail < 0 || aq_writetail >= aq_buflen", __func__));

    if (alq->aq_flags & AQ_WANTED) {
        alq->aq_flags &= ~AQ_WANTED;
        return (1);
    }

    return(0);
}
Пример #2
0
/*
 * Flush all pending data to disk.  This operation will block.
 */
static int
alq_doio(struct alq *alq)
{
	struct thread *td;
	struct mount *mp;
	struct vnode *vp;
	struct uio auio;
	struct iovec aiov[2];
	struct ale *ale;
	struct ale *alstart;
	int totlen;
	int iov;
	int vfslocked;

	vp = alq->aq_vp;
	td = curthread;
	totlen = 0;
	iov = 0;

	alstart = ale = alq->aq_entvalid;
	alq->aq_entvalid = NULL;

	bzero(&aiov, sizeof(aiov));
	bzero(&auio, sizeof(auio));

	do {
		if (aiov[iov].iov_base == NULL)
			aiov[iov].iov_base = ale->ae_data;
		aiov[iov].iov_len += alq->aq_entlen;
		totlen += alq->aq_entlen;
		/* Check to see if we're wrapping the buffer */
		if (ale->ae_data + alq->aq_entlen != ale->ae_next->ae_data)
			iov++;
		ale->ae_flags &= ~AE_VALID;
		ale = ale->ae_next;
	} while (ale->ae_flags & AE_VALID);

	alq->aq_flags |= AQ_FLUSHING;
	ALQ_UNLOCK(alq);

	if (iov == 2 || aiov[iov].iov_base == NULL)
		iov--;

	auio.uio_iov = &aiov[0];
	auio.uio_offset = 0;
	auio.uio_segflg = UIO_SYSSPACE;
	auio.uio_rw = UIO_WRITE;
	auio.uio_iovcnt = iov + 1;
	auio.uio_resid = totlen;
	auio.uio_td = td;

	/*
	 * Do all of the junk required to write now.
	 */
	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
	vn_start_write(vp, &mp, V_WAIT);
	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
	/*
	 * XXX: VOP_WRITE error checks are ignored.
	 */
#ifdef MAC
	if (mac_vnode_check_write(alq->aq_cred, NOCRED, vp) == 0)
#endif
		VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, alq->aq_cred);
	VOP_UNLOCK(vp, 0);
	vn_finished_write(mp);
	VFS_UNLOCK_GIANT(vfslocked);

	ALQ_LOCK(alq);
	alq->aq_flags &= ~AQ_FLUSHING;

	if (alq->aq_entfree == NULL)
		alq->aq_entfree = alstart;

	if (alq->aq_flags & AQ_WANTED) {
		alq->aq_flags &= ~AQ_WANTED;
		return (1);
	}

	return(0);
}