Exemple #1
0
/*
 * Flush all entries from the cache for a particular vnode.
 */
static int
dqflush(struct vnode *vp)
{
    struct dquot *dq, *nextdq;
    struct dqhash *dqh;
    int error;

    /*
     * Move all dquot's that used to refer to this quota
     * file off their hash chains (they will eventually
     * fall off the head of the free list and be re-used).
     */
    error = 0;
    DQH_LOCK();
    for (dqh = &dqhashtbl[dqhash]; dqh >= dqhashtbl; dqh--) {
        for (dq = LIST_FIRST(dqh); dq; dq = nextdq) {
            nextdq = LIST_NEXT(dq, dq_hash);
            if (dq->dq_ump->um_quotas[dq->dq_type] != vp)
                continue;
            if (dq->dq_cnt)
                error = EBUSY;
            else {
                LIST_REMOVE(dq, dq_hash);
                dq->dq_ump = NULL;
            }
        }
    }
    DQH_UNLOCK();
    return (error);
}
Exemple #2
0
/*
 * Flush all entries from the cache for a particular vnode.
 */
static void
dqflush(struct vnode *vp)
{
	struct dquot *dq, *nextdq;
	struct dqhash *dqh;

	/*
	 * Move all dquot's that used to refer to this quota
	 * file off their hash chains (they will eventually
	 * fall off the head of the free list and be re-used).
	 */
	DQH_LOCK();
	for (dqh = &dqhashtbl[dqhash]; dqh >= dqhashtbl; dqh--) {
		for (dq = LIST_FIRST(dqh); dq; dq = nextdq) {
			nextdq = LIST_NEXT(dq, dq_hash);
			if (dq->dq_ump->um_quotas[dq->dq_type] != vp)
				continue;
			if (dq->dq_cnt)
				panic("dqflush: stray dquot");
			LIST_REMOVE(dq, dq_hash);
			dq->dq_ump = (struct ufsmount *)0;
		}
	}
	DQH_UNLOCK();
}
Exemple #3
0
/*
 * Release a reference to a dquot.
 */
void
dqrele(struct vnode *vp, struct dquot *dq)
{

    if (dq == NODQUOT)
        return;
    DQH_LOCK();
    KASSERT(dq->dq_cnt > 0, ("Lost dq %p reference 1", dq));
    if (dq->dq_cnt > 1) {
        dq->dq_cnt--;
        DQH_UNLOCK();
        return;
    }
    DQH_UNLOCK();
sync:
    (void) dqsync(vp, dq);

    DQH_LOCK();
    KASSERT(dq->dq_cnt > 0, ("Lost dq %p reference 2", dq));
    if (--dq->dq_cnt > 0)
    {
        DQH_UNLOCK();
        return;
    }

    /*
     * The dq may become dirty after it is synced but before it is
     * put to the free list. Checking the DQ_MOD there without
     * locking dq should be safe since no other references to the
     * dq exist.
     */
    if ((dq->dq_flags & DQ_MOD) != 0) {
        dq->dq_cnt++;
        DQH_UNLOCK();
        goto sync;
    }
    TAILQ_INSERT_TAIL(&dqfreelist, dq, dq_freelist);
    DQH_UNLOCK();
}
Exemple #4
0
/*
 * Obtain a dquot structure for the specified identifier and quota file
 * reading the information from the file if necessary.
 */
static int
dqget(struct vnode *vp, u_long id, struct ufsmount *ump, int type,
      struct dquot **dqp)
{
    uint8_t buf[sizeof(struct dqblk64)];
    off_t base, recsize;
    struct dquot *dq, *dq1;
    struct dqhash *dqh;
    struct vnode *dqvp;
    struct iovec aiov;
    struct uio auio;
    int dqvplocked, error;

#ifdef DEBUG_VFS_LOCKS
    if (vp != NULLVP)
        ASSERT_VOP_ELOCKED(vp, "dqget");
#endif

    if (vp != NULLVP && *dqp != NODQUOT) {
        return (0);
    }

    /* XXX: Disallow negative id values to prevent the
    * creation of 100GB+ quota data files.
    */
    if ((int)id < 0)
        return (EINVAL);

    UFS_LOCK(ump);
    dqvp = ump->um_quotas[type];
    if (dqvp == NULLVP || (ump->um_qflags[type] & QTF_CLOSING)) {
        *dqp = NODQUOT;
        UFS_UNLOCK(ump);
        return (EINVAL);
    }
    vref(dqvp);
    UFS_UNLOCK(ump);
    error = 0;
    dqvplocked = 0;

    /*
     * Check the cache first.
     */
    dqh = DQHASH(dqvp, id);
    DQH_LOCK();
    dq = dqhashfind(dqh, id, dqvp);
    if (dq != NULL) {
        DQH_UNLOCK();
hfound:
        DQI_LOCK(dq);
        DQI_WAIT(dq, PINOD+1, "dqget");
        DQI_UNLOCK(dq);
        if (dq->dq_ump == NULL) {
            dqrele(vp, dq);
            dq = NODQUOT;
            error = EIO;
        }
        *dqp = dq;
        if (dqvplocked)
            vput(dqvp);
        else
            vrele(dqvp);
        return (error);
    }

    /*
     * Quota vnode lock is before DQ_LOCK. Acquire dqvp lock there
     * since new dq will appear on the hash chain DQ_LOCKed.
     */
    if (vp != dqvp) {
        DQH_UNLOCK();
        vn_lock(dqvp, LK_SHARED | LK_RETRY);
        dqvplocked = 1;
        DQH_LOCK();
        /*
         * Recheck the cache after sleep for quota vnode lock.
         */
        dq = dqhashfind(dqh, id, dqvp);
        if (dq != NULL) {
            DQH_UNLOCK();
            goto hfound;
        }
    }

    /*
     * Not in cache, allocate a new one or take it from the
     * free list.
     */
    if (TAILQ_FIRST(&dqfreelist) == NODQUOT &&
            numdquot < MAXQUOTAS * desiredvnodes)
        desireddquot += DQUOTINC;
    if (numdquot < desireddquot) {
        numdquot++;
        DQH_UNLOCK();
        dq1 = malloc(sizeof *dq1, M_DQUOT, M_WAITOK | M_ZERO);
        mtx_init(&dq1->dq_lock, "dqlock", NULL, MTX_DEF);
        DQH_LOCK();
        /*
         * Recheck the cache after sleep for memory.
         */
        dq = dqhashfind(dqh, id, dqvp);
        if (dq != NULL) {
            numdquot--;
            DQH_UNLOCK();
            mtx_destroy(&dq1->dq_lock);
            free(dq1, M_DQUOT);
            goto hfound;
        }
        dq = dq1;
    } else {
        if ((dq = TAILQ_FIRST(&dqfreelist)) == NULL) {
            DQH_UNLOCK();
            tablefull("dquot");
            *dqp = NODQUOT;
            if (dqvplocked)
                vput(dqvp);
            else
                vrele(dqvp);
            return (EUSERS);
        }
        if (dq->dq_cnt || (dq->dq_flags & DQ_MOD))
            panic("dqget: free dquot isn't %p", dq);
        TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
        if (dq->dq_ump != NULL)
            LIST_REMOVE(dq, dq_hash);
    }

    /*
     * Dq is put into hash already locked to prevent parallel
     * usage while it is being read from file.
     */
    dq->dq_flags = DQ_LOCK;
    dq->dq_id = id;
    dq->dq_type = type;
    dq->dq_ump = ump;
    LIST_INSERT_HEAD(dqh, dq, dq_hash);
    DQREF(dq);
    DQH_UNLOCK();

    /*
     * Read the requested quota record from the quota file, performing
     * any necessary conversions.
     */
    if (ump->um_qflags[type] & QTF_64BIT) {
        recsize = sizeof(struct dqblk64);
        base = sizeof(struct dqhdr64);
    } else {
        recsize = sizeof(struct dqblk32);
        base = 0;
    }
    auio.uio_iov = &aiov;
    auio.uio_iovcnt = 1;
    aiov.iov_base = buf;
    aiov.iov_len = recsize;
    auio.uio_resid = recsize;
    auio.uio_offset = base + id * recsize;
    auio.uio_segflg = UIO_SYSSPACE;
    auio.uio_rw = UIO_READ;
    auio.uio_td = (struct thread *)0;

    error = VOP_READ(dqvp, &auio, 0, ump->um_cred[type]);
    if (auio.uio_resid == recsize && error == 0) {
        bzero(&dq->dq_dqb, sizeof(dq->dq_dqb));
    } else {
        if (ump->um_qflags[type] & QTF_64BIT)
            dqb64_dq((struct dqblk64 *)buf, dq);
        else
            dqb32_dq((struct dqblk32 *)buf, dq);
    }
    if (dqvplocked)
        vput(dqvp);
    else
        vrele(dqvp);
    /*
     * I/O error in reading quota file, release
     * quota structure and reflect problem to caller.
     */
    if (error) {
        DQH_LOCK();
        dq->dq_ump = NULL;
        LIST_REMOVE(dq, dq_hash);
        DQH_UNLOCK();
        DQI_LOCK(dq);
        if (dq->dq_flags & DQ_WANT)
            wakeup(dq);
        dq->dq_flags = 0;
        DQI_UNLOCK(dq);
        dqrele(vp, dq);
        *dqp = NODQUOT;
        return (error);
    }
    DQI_LOCK(dq);
    /*
     * Check for no limit to enforce.
     * Initialize time values if necessary.
     */
    if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
            dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
        dq->dq_flags |= DQ_FAKE;
    if (dq->dq_id != 0) {
        if (dq->dq_btime == 0) {
            dq->dq_btime = time_second + ump->um_btime[type];
            if (dq->dq_bsoftlimit &&
                    dq->dq_curblocks >= dq->dq_bsoftlimit)
                dq->dq_flags |= DQ_MOD;
        }
        if (dq->dq_itime == 0) {
            dq->dq_itime = time_second + ump->um_itime[type];
            if (dq->dq_isoftlimit &&
                    dq->dq_curinodes >= dq->dq_isoftlimit)
                dq->dq_flags |= DQ_MOD;
        }
    }
    DQI_WAKEUP(dq);
    DQI_UNLOCK(dq);
    *dqp = dq;
    return (0);
}