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