[BACK]Return to quota2_subr.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / ufs / ufs

File: [cvs.NetBSD.org] / src / sys / ufs / ufs / quota2_subr.c (download)

Revision 1.5, Sun Feb 5 14:19:04 2012 UTC (2 years, 7 months ago) by dholland
Branch: MAIN
CVS Tags: yamt-pagecache-base9, yamt-pagecache-base8, yamt-pagecache-base7, yamt-pagecache-base6, yamt-pagecache-base5, yamt-pagecache-base4, tls-maxphys-base, tls-maxphys, tls-earlyentropy-base, tls-earlyentropy, rmind-smpnet-nbase, rmind-smpnet-base, rmind-smpnet, riastradh-xf86-video-intel-2-7-1-pre-2-21-15, riastradh-drm2-base3, riastradh-drm2-base2, riastradh-drm2-base1, riastradh-drm2-base, riastradh-drm2, netbsd-7-base, netbsd-7, netbsd-6-base, netbsd-6-1-RELEASE, netbsd-6-1-RC4, netbsd-6-1-RC3, netbsd-6-1-RC2, netbsd-6-1-RC1, netbsd-6-1-4-RELEASE, netbsd-6-1-3-RELEASE, netbsd-6-1-2-RELEASE, netbsd-6-1-1-RELEASE, netbsd-6-1, netbsd-6-0-RELEASE, netbsd-6-0-RC2, netbsd-6-0-RC1, netbsd-6-0-5-RELEASE, netbsd-6-0-4-RELEASE, netbsd-6-0-3-RELEASE, netbsd-6-0-2-RELEASE, netbsd-6-0-1-RELEASE, netbsd-6-0, netbsd-6, matt-nb6-plus-nbase, matt-nb6-plus-base, matt-nb6-plus, khorben-n900, jmcneill-usbmp-base9, jmcneill-usbmp-base8, jmcneill-usbmp-base7, jmcneill-usbmp-base6, jmcneill-usbmp-base5, jmcneill-usbmp-base4, jmcneill-usbmp-base3, jmcneill-usbmp-base2, jmcneill-usbmp-base10, agc-symver-base, agc-symver, HEAD
Changes since 1.4: +22 -3 lines

Migrate one last leftover bit (used only by the kernel now) to
sys/ufs/ufs and remove the old quota headers and no-longer-used shared
code. Ok by releng.

/* $NetBSD: quota2_subr.c,v 1.5 2012/02/05 14:19:04 dholland Exp $ */
/*-
  * Copyright (c) 2010, 2011 Manuel Bouyer
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
  * 1. Redistributions of source code must retain the above copyright
  *    notice, this list of conditions and the following disclaimer.
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
  *
  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  * POSSIBILITY OF SUCH DAMAGE.
  */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: quota2_subr.c,v 1.5 2012/02/05 14:19:04 dholland Exp $");

#include <sys/param.h>
#include <sys/time.h>

#include <ufs/ufs/dinode.h>
#include <ufs/ffs/fs.h>
#include <ufs/ffs/ffs_extern.h>
#include <ufs/ufs/ufs_bswap.h>
#include <ufs/ufs/quota2.h>

#ifndef _KERNEL
#include <string.h>
#endif

void
quota2_addfreeq2e(struct quota2_header *q2h, void *bp, uint64_t baseoff,
    uint64_t bsize, int ns)
{
	uint64_t blkoff = baseoff % bsize;
	int i, nq2e;
	struct quota2_entry *q2e;

	q2e = (void *)((char *)bp + blkoff);
	nq2e = (bsize - blkoff) / sizeof(*q2e);
	for (i = 0; i < nq2e; i++) {
		q2e[i].q2e_next = q2h->q2h_free;
		q2h->q2h_free = ufs_rw64(i * sizeof(*q2e) + baseoff, ns);
	}
}

void
quota2_create_blk0(uint64_t bsize, void *bp, int q2h_hash_shift, int type,
    int ns)
{
	struct quota2_header *q2h;
	const int quota2_hash_size = 1 << q2h_hash_shift;
	const int quota2_full_header_size = sizeof(struct quota2_header) +
	    sizeof(q2h->q2h_entries[0]) * quota2_hash_size;
	int i;

	memset(bp, 0, bsize);
	q2h = bp;
	q2h->q2h_magic_number = ufs_rw32(Q2_HEAD_MAGIC, ns);
	q2h->q2h_type = type;
	q2h->q2h_hash_shift = q2h_hash_shift;
	q2h->q2h_hash_size = ufs_rw16(quota2_hash_size, ns);
	/* setup defaut entry: unlimited, 7 days grace */
	for (i = 0; i < N_QL; i++) {
		q2h->q2h_defentry.q2e_val[i].q2v_hardlimit =
		    q2h->q2h_defentry.q2e_val[i].q2v_softlimit =
		    ufs_rw64(UQUAD_MAX, ns);
		q2h->q2h_defentry.q2e_val[i].q2v_grace =
		    ufs_rw64(7ULL * 24ULL * 3600ULL, ns);
	}

	/* first quota entry, after the hash table */
	quota2_addfreeq2e(q2h, bp, quota2_full_header_size, bsize, ns);
}

void
quota2_ufs_rwq2v(const struct quota2_val *s, struct quota2_val *d, int needswap)
{
	d->q2v_hardlimit = ufs_rw64(s->q2v_hardlimit, needswap);
	d->q2v_softlimit = ufs_rw64(s->q2v_softlimit, needswap);
	d->q2v_cur = ufs_rw64(s->q2v_cur, needswap);
	d->q2v_time = ufs_rw64(s->q2v_time, needswap);
	d->q2v_grace = ufs_rw64(s->q2v_grace, needswap);
}

void
quota2_ufs_rwq2e(const struct quota2_entry *s, struct quota2_entry *d,
int needswap)
{
	quota2_ufs_rwq2v(&s->q2e_val[QL_BLOCK], &d->q2e_val[QL_BLOCK],
	    needswap);
	quota2_ufs_rwq2v(&s->q2e_val[QL_FILE], &d->q2e_val[QL_FILE],
	    needswap);
	d->q2e_uid = ufs_rw32(s->q2e_uid, needswap);
}

int
quota_check_limit(uint64_t cur, uint64_t change, uint64_t soft, uint64_t hard,
    time_t expire, time_t now)
{ 
	if (cur + change > hard) {
		if (cur <= soft)
			return (QL_F_CROSS | QL_S_DENY_HARD);
		return QL_S_DENY_HARD;
	} else if (cur + change > soft) {
		if (cur <= soft)
			return (QL_F_CROSS | QL_S_ALLOW_SOFT);
		if (now > expire) {
			return QL_S_DENY_GRACE;
		}
		return QL_S_ALLOW_SOFT;
	}
	return QL_S_ALLOW_OK;
}