Home | History | Annotate | Line # | Download | only in lfs
ulfs_quota2_subr.c revision 1.2
      1 /*	$NetBSD: ulfs_quota2_subr.c,v 1.2 2013/06/06 00:44:40 dholland Exp $	*/
      2 /*  from NetBSD: quota2_subr.c,v 1.5 2012/02/05 14:19:04 dholland Exp  */
      3 
      4 /*-
      5   * Copyright (c) 2010, 2011 Manuel Bouyer
      6   * All rights reserved.
      7   *
      8   * Redistribution and use in source and binary forms, with or without
      9   * modification, are permitted provided that the following conditions
     10   * are met:
     11   * 1. Redistributions of source code must retain the above copyright
     12   *    notice, this list of conditions and the following disclaimer.
     13   * 2. Redistributions in binary form must reproduce the above copyright
     14   *    notice, this list of conditions and the following disclaimer in the
     15   *    documentation and/or other materials provided with the distribution.
     16   *
     17   * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     18   * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     19   * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20   * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     21   * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     25   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     26   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     27   * POSSIBILITY OF SUCH DAMAGE.
     28   */
     29 
     30 #include <sys/cdefs.h>
     31 __KERNEL_RCSID(0, "$NetBSD: ulfs_quota2_subr.c,v 1.2 2013/06/06 00:44:40 dholland Exp $");
     32 
     33 #include <sys/param.h>
     34 #include <sys/time.h>
     35 
     36 #include <ufs/lfs/ulfs_dinode.h>
     37 #include <ufs/ffs/fs.h>
     38 #include <ufs/ffs/ffs_extern.h>
     39 #include <ufs/lfs/ulfs_bswap.h>
     40 #include <ufs/lfs/ulfs_quota2.h>
     41 
     42 #ifndef _KERNEL
     43 #include <string.h>
     44 #endif
     45 
     46 void
     47 quota2_addfreeq2e(struct quota2_header *q2h, void *bp, uint64_t baseoff,
     48     uint64_t bsize, int ns)
     49 {
     50 	uint64_t blkoff = baseoff % bsize;
     51 	int i, nq2e;
     52 	struct quota2_entry *q2e;
     53 
     54 	q2e = (void *)((char *)bp + blkoff);
     55 	nq2e = (bsize - blkoff) / sizeof(*q2e);
     56 	for (i = 0; i < nq2e; i++) {
     57 		q2e[i].q2e_next = q2h->q2h_free;
     58 		q2h->q2h_free = ufs_rw64(i * sizeof(*q2e) + baseoff, ns);
     59 	}
     60 }
     61 
     62 void
     63 quota2_create_blk0(uint64_t bsize, void *bp, int q2h_hash_shift, int type,
     64     int ns)
     65 {
     66 	struct quota2_header *q2h;
     67 	const int quota2_hash_size = 1 << q2h_hash_shift;
     68 	const int quota2_full_header_size = sizeof(struct quota2_header) +
     69 	    sizeof(q2h->q2h_entries[0]) * quota2_hash_size;
     70 	int i;
     71 
     72 	memset(bp, 0, bsize);
     73 	q2h = bp;
     74 	q2h->q2h_magic_number = ufs_rw32(Q2_HEAD_MAGIC, ns);
     75 	q2h->q2h_type = type;
     76 	q2h->q2h_hash_shift = q2h_hash_shift;
     77 	q2h->q2h_hash_size = ufs_rw16(quota2_hash_size, ns);
     78 	/* setup defaut entry: unlimited, 7 days grace */
     79 	for (i = 0; i < N_QL; i++) {
     80 		q2h->q2h_defentry.q2e_val[i].q2v_hardlimit =
     81 		    q2h->q2h_defentry.q2e_val[i].q2v_softlimit =
     82 		    ufs_rw64(UQUAD_MAX, ns);
     83 		q2h->q2h_defentry.q2e_val[i].q2v_grace =
     84 		    ufs_rw64(7ULL * 24ULL * 3600ULL, ns);
     85 	}
     86 
     87 	/* first quota entry, after the hash table */
     88 	quota2_addfreeq2e(q2h, bp, quota2_full_header_size, bsize, ns);
     89 }
     90 
     91 void
     92 quota2_ufs_rwq2v(const struct quota2_val *s, struct quota2_val *d, int needswap)
     93 {
     94 	d->q2v_hardlimit = ufs_rw64(s->q2v_hardlimit, needswap);
     95 	d->q2v_softlimit = ufs_rw64(s->q2v_softlimit, needswap);
     96 	d->q2v_cur = ufs_rw64(s->q2v_cur, needswap);
     97 	d->q2v_time = ufs_rw64(s->q2v_time, needswap);
     98 	d->q2v_grace = ufs_rw64(s->q2v_grace, needswap);
     99 }
    100 
    101 void
    102 quota2_ufs_rwq2e(const struct quota2_entry *s, struct quota2_entry *d,
    103 int needswap)
    104 {
    105 	quota2_ufs_rwq2v(&s->q2e_val[QL_BLOCK], &d->q2e_val[QL_BLOCK],
    106 	    needswap);
    107 	quota2_ufs_rwq2v(&s->q2e_val[QL_FILE], &d->q2e_val[QL_FILE],
    108 	    needswap);
    109 	d->q2e_uid = ufs_rw32(s->q2e_uid, needswap);
    110 }
    111 
    112 int
    113 quota_check_limit(uint64_t cur, uint64_t change, uint64_t soft, uint64_t hard,
    114     time_t expire, time_t now)
    115 {
    116 	if (cur + change > hard) {
    117 		if (cur <= soft)
    118 			return (QL_F_CROSS | QL_S_DENY_HARD);
    119 		return QL_S_DENY_HARD;
    120 	} else if (cur + change > soft) {
    121 		if (cur <= soft)
    122 			return (QL_F_CROSS | QL_S_ALLOW_SOFT);
    123 		if (now > expire) {
    124 			return QL_S_DENY_GRACE;
    125 		}
    126 		return QL_S_ALLOW_SOFT;
    127 	}
    128 	return QL_S_ALLOW_OK;
    129 }
    130