Home | History | Annotate | Line # | Download | only in lfs
ulfs_quota2.c revision 1.9
      1 /*	$NetBSD: ulfs_quota2.c,v 1.9 2013/07/28 01:10:49 dholland Exp $	*/
      2 /*  from NetBSD: ufs_quota2.c,v 1.35 2012/09/27 07:47:56 bouyer Exp  */
      3 
      4 /*-
      5   * Copyright (c) 2010 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.c,v 1.9 2013/07/28 01:10:49 dholland Exp $");
     32 
     33 #include <sys/buf.h>
     34 #include <sys/param.h>
     35 #include <sys/kernel.h>
     36 #include <sys/systm.h>
     37 #include <sys/namei.h>
     38 #include <sys/file.h>
     39 #include <sys/proc.h>
     40 #include <sys/vnode.h>
     41 #include <sys/mount.h>
     42 #include <sys/fstrans.h>
     43 #include <sys/kauth.h>
     44 #include <sys/wapbl.h>
     45 #include <sys/quota.h>
     46 #include <sys/quotactl.h>
     47 
     48 #include <ufs/lfs/lfs_extern.h>
     49 
     50 #include <ufs/lfs/ulfs_quota2.h>
     51 #include <ufs/lfs/ulfs_inode.h>
     52 #include <ufs/lfs/ulfsmount.h>
     53 #include <ufs/lfs/ulfs_bswap.h>
     54 #include <ufs/lfs/ulfs_extern.h>
     55 #include <ufs/lfs/ulfs_quota.h>
     56 
     57 /*
     58  * LOCKING:
     59  * Data in the entries are protected by the associated struct dquot's
     60  * dq_interlock (this means we can't read or change a quota entry without
     61  * grabing a dquot for it).
     62  * The header and lists (including pointers in the data entries, and q2e_uid)
     63  * are protected by the global dqlock.
     64  * the locking order is dq_interlock -> dqlock
     65  */
     66 
     67 static int quota2_bwrite(struct mount *, struct buf *);
     68 static int getinoquota2(struct inode *, bool, bool, struct buf **,
     69     struct quota2_entry **);
     70 static int getq2h(struct ulfsmount *, int, struct buf **,
     71     struct quota2_header **, int);
     72 static int getq2e(struct ulfsmount *, int, daddr_t, int, struct buf **,
     73     struct quota2_entry **, int);
     74 static int quota2_walk_list(struct ulfsmount *, struct buf *, int,
     75     uint64_t *, int, void *,
     76     int (*func)(struct ulfsmount *, uint64_t *, struct quota2_entry *,
     77       uint64_t, void *));
     78 
     79 static const char *limnames[] = INITQLNAMES;
     80 
     81 static void
     82 quota2_dict_update_q2e_limits(int objtype, const struct quotaval *val,
     83     struct quota2_entry *q2e)
     84 {
     85 	/* make sure we can index q2e_val[] by the fs-independent objtype */
     86 	CTASSERT(QUOTA_OBJTYPE_BLOCKS == QL_BLOCK);
     87 	CTASSERT(QUOTA_OBJTYPE_FILES == QL_FILE);
     88 
     89 	q2e->q2e_val[objtype].q2v_hardlimit = val->qv_hardlimit;
     90 	q2e->q2e_val[objtype].q2v_softlimit = val->qv_softlimit;
     91 	q2e->q2e_val[objtype].q2v_grace = val->qv_grace;
     92 }
     93 
     94 /*
     95  * Convert internal representation to FS-independent representation.
     96  * (Note that while the two types are currently identical, the
     97  * internal representation is an on-disk struct and the FS-independent
     98  * representation is not, and they might diverge in the future.)
     99  */
    100 static void
    101 q2val_to_quotaval(struct quota2_val *q2v, struct quotaval *qv)
    102 {
    103 	qv->qv_softlimit = q2v->q2v_softlimit;
    104 	qv->qv_hardlimit = q2v->q2v_hardlimit;
    105 	qv->qv_usage = q2v->q2v_cur;
    106 	qv->qv_expiretime = q2v->q2v_time;
    107 	qv->qv_grace = q2v->q2v_grace;
    108 }
    109 
    110 /*
    111  * Convert a quota2entry and default-flag to the FS-independent
    112  * representation.
    113  */
    114 static void
    115 q2e_to_quotaval(struct quota2_entry *q2e, int def,
    116 	       id_t *id, int objtype, struct quotaval *ret)
    117 {
    118 	if (def) {
    119 		*id = QUOTA_DEFAULTID;
    120 	} else {
    121 		*id = q2e->q2e_uid;
    122 	}
    123 
    124 	KASSERT(objtype >= 0 && objtype < N_QL);
    125 	q2val_to_quotaval(&q2e->q2e_val[objtype], ret);
    126 }
    127 
    128 
    129 static int
    130 quota2_bwrite(struct mount *mp, struct buf *bp)
    131 {
    132 	if (mp->mnt_flag & MNT_SYNCHRONOUS)
    133 		return bwrite(bp);
    134 	else {
    135 		bdwrite(bp);
    136 		return 0;
    137 	}
    138 }
    139 
    140 static int
    141 getq2h(struct ulfsmount *ump, int type,
    142     struct buf **bpp, struct quota2_header **q2hp, int flags)
    143 {
    144 #ifdef LFS_EI
    145 	struct lfs *fs = ump->um_lfs;
    146 	const int needswap = ULFS_MPNEEDSWAP(fs);
    147 #endif
    148 	int error;
    149 	struct buf *bp;
    150 	struct quota2_header *q2h;
    151 
    152 	KASSERT(mutex_owned(&lfs_dqlock));
    153 	error = bread(ump->um_quotas[type], 0, ump->umq2_bsize,
    154 	    ump->um_cred[type], flags, &bp);
    155 	if (error)
    156 		return error;
    157 	if (bp->b_resid != 0)
    158 		panic("dq2get: %s quota file truncated", lfs_quotatypes[type]);
    159 
    160 	q2h = (void *)bp->b_data;
    161 	if (ulfs_rw32(q2h->q2h_magic_number, needswap) != Q2_HEAD_MAGIC ||
    162 	    q2h->q2h_type != type)
    163 		panic("dq2get: corrupted %s quota header", lfs_quotatypes[type]);
    164 	*bpp = bp;
    165 	*q2hp = q2h;
    166 	return 0;
    167 }
    168 
    169 static int
    170 getq2e(struct ulfsmount *ump, int type, daddr_t lblkno, int blkoffset,
    171     struct buf **bpp, struct quota2_entry **q2ep, int flags)
    172 {
    173 	int error;
    174 	struct buf *bp;
    175 
    176 	if (blkoffset & (sizeof(uint64_t) - 1)) {
    177 		panic("dq2get: %s quota file corrupted",
    178 		    lfs_quotatypes[type]);
    179 	}
    180 	error = bread(ump->um_quotas[type], lblkno, ump->umq2_bsize,
    181 	    ump->um_cred[type], flags, &bp);
    182 	if (error)
    183 		return error;
    184 	if (bp->b_resid != 0) {
    185 		panic("dq2get: %s quota file corrupted",
    186 		    lfs_quotatypes[type]);
    187 	}
    188 	*q2ep = (void *)((char *)bp->b_data + blkoffset);
    189 	*bpp = bp;
    190 	return 0;
    191 }
    192 
    193 /* walk a quota entry list, calling the callback for each entry */
    194 #define Q2WL_ABORT 0x10000000
    195 
    196 static int
    197 quota2_walk_list(struct ulfsmount *ump, struct buf *hbp, int type,
    198     uint64_t *offp, int flags, void *a,
    199     int (*func)(struct ulfsmount *, uint64_t *, struct quota2_entry *, uint64_t, void *))
    200 {
    201 #ifdef LFS_EI
    202 	struct lfs *fs = ump->um_lfs;
    203 	const int needswap = ULFS_MPNEEDSWAP(fs);
    204 #endif
    205 	daddr_t off = ulfs_rw64(*offp, needswap);
    206 	struct buf *bp, *obp = hbp;
    207 	int ret = 0, ret2 = 0;
    208 	struct quota2_entry *q2e;
    209 	daddr_t lblkno, blkoff, olblkno = 0;
    210 
    211 	KASSERT(mutex_owner(&lfs_dqlock));
    212 
    213 	while (off != 0) {
    214 		lblkno = (off >> ump->um_mountp->mnt_fs_bshift);
    215 		blkoff = (off & ump->umq2_bmask);
    216 		if (lblkno == 0) {
    217 			/* in the header block */
    218 			bp = hbp;
    219 		} else if (lblkno == olblkno) {
    220 			/* still in the same buf */
    221 			bp = obp;
    222 		} else {
    223 			ret = bread(ump->um_quotas[type], lblkno,
    224 			    ump->umq2_bsize,
    225 			    ump->um_cred[type], flags, &bp);
    226 			if (ret)
    227 				return ret;
    228 			if (bp->b_resid != 0) {
    229 				panic("quota2_walk_list: %s quota file corrupted",
    230 				    lfs_quotatypes[type]);
    231 			}
    232 		}
    233 		q2e = (void *)((char *)(bp->b_data) + blkoff);
    234 		ret = (*func)(ump, offp, q2e, off, a);
    235 		if (off != ulfs_rw64(*offp, needswap)) {
    236 			/* callback changed parent's pointer, redo */
    237 			off = ulfs_rw64(*offp, needswap);
    238 			if (bp != hbp && bp != obp)
    239 				ret2 = bwrite(bp);
    240 		} else {
    241 			/* parent if now current */
    242 			if (obp != bp && obp != hbp) {
    243 				if (flags & B_MODIFY)
    244 					ret2 = bwrite(obp);
    245 				else
    246 					brelse(obp, 0);
    247 			}
    248 			obp = bp;
    249 			olblkno = lblkno;
    250 			offp = &(q2e->q2e_next);
    251 			off = ulfs_rw64(*offp, needswap);
    252 		}
    253 		if (ret)
    254 			break;
    255 		if (ret2) {
    256 			ret = ret2;
    257 			break;
    258 		}
    259 	}
    260 	if (obp != hbp) {
    261 		if (flags & B_MODIFY)
    262 			ret2 = bwrite(obp);
    263 		else
    264 			brelse(obp, 0);
    265 	}
    266 	if (ret & Q2WL_ABORT)
    267 		return 0;
    268 	if (ret == 0)
    269 		return ret2;
    270 	return ret;
    271 }
    272 
    273 int
    274 lfsquota2_umount(struct mount *mp, int flags)
    275 {
    276 	int i, error;
    277 	struct ulfsmount *ump = VFSTOULFS(mp);
    278 	struct lfs *fs = ump->um_lfs;
    279 
    280 	if ((fs->um_flags & ULFS_QUOTA2) == 0)
    281 		return 0;
    282 
    283 	for (i = 0; i < ULFS_MAXQUOTAS; i++) {
    284 		if (ump->um_quotas[i] != NULLVP) {
    285 			error = vn_close(ump->um_quotas[i], FREAD|FWRITE,
    286 			    ump->um_cred[i]);
    287 			if (error) {
    288 				printf("quota2_umount failed: close(%p) %d\n",
    289 				    ump->um_quotas[i], error);
    290 				return error;
    291 			}
    292 		}
    293 		ump->um_quotas[i] = NULLVP;
    294 	}
    295 	return 0;
    296 }
    297 
    298 static int
    299 quota2_q2ealloc(struct ulfsmount *ump, int type, uid_t uid, struct dquot *dq)
    300 {
    301 	int error, error2;
    302 	struct buf *hbp, *bp;
    303 	struct quota2_header *q2h;
    304 	struct quota2_entry *q2e;
    305 	daddr_t offset;
    306 	u_long hash_mask;
    307 	struct lfs *fs = ump->um_lfs;
    308 	const int needswap = ULFS_MPNEEDSWAP(fs);
    309 
    310 	KASSERT(mutex_owned(&dq->dq_interlock));
    311 	KASSERT(mutex_owned(&lfs_dqlock));
    312 	error = getq2h(ump, type, &hbp, &q2h, B_MODIFY);
    313 	if (error)
    314 		return error;
    315 	offset = ulfs_rw64(q2h->q2h_free, needswap);
    316 	if (offset == 0) {
    317 		struct vnode *vp = ump->um_quotas[type];
    318 		struct inode *ip = VTOI(vp);
    319 		uint64_t size = ip->i_size;
    320 		/* need to alocate a new disk block */
    321 		error = lfs_balloc(vp, size, ump->umq2_bsize,
    322 		    ump->um_cred[type], B_CLRBUF | B_SYNC, &bp);
    323 		if (error) {
    324 			brelse(hbp, 0);
    325 			return error;
    326 		}
    327 		KASSERT((ip->i_size % ump->umq2_bsize) == 0);
    328 		ip->i_size += ump->umq2_bsize;
    329 		DIP_ASSIGN(ip, size, ip->i_size);
    330 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    331 		uvm_vnp_setsize(vp, ip->i_size);
    332 		lfsquota2_addfreeq2e(q2h, bp->b_data, size, ump->umq2_bsize,
    333 		    needswap);
    334 		error = bwrite(bp);
    335 		error2 = lfs_update(vp, NULL, NULL, UPDATE_WAIT);
    336 		if (error || error2) {
    337 			brelse(hbp, 0);
    338 			if (error)
    339 				return error;
    340 			return error2;
    341 		}
    342 		offset = ulfs_rw64(q2h->q2h_free, needswap);
    343 		KASSERT(offset != 0);
    344 	}
    345 	dq->dq2_lblkno = (offset >> ump->um_mountp->mnt_fs_bshift);
    346 	dq->dq2_blkoff = (offset & ump->umq2_bmask);
    347 	if (dq->dq2_lblkno == 0) {
    348 		bp = hbp;
    349 		q2e = (void *)((char *)bp->b_data + dq->dq2_blkoff);
    350 	} else {
    351 		error = getq2e(ump, type, dq->dq2_lblkno,
    352 		    dq->dq2_blkoff, &bp, &q2e, B_MODIFY);
    353 		if (error) {
    354 			brelse(hbp, 0);
    355 			return error;
    356 		}
    357 	}
    358 	hash_mask = ((1 << q2h->q2h_hash_shift) - 1);
    359 	/* remove from free list */
    360 	q2h->q2h_free = q2e->q2e_next;
    361 
    362 	memcpy(q2e, &q2h->q2h_defentry, sizeof(*q2e));
    363 	q2e->q2e_uid = ulfs_rw32(uid, needswap);
    364 	/* insert in hash list */
    365 	q2e->q2e_next = q2h->q2h_entries[uid & hash_mask];
    366 	q2h->q2h_entries[uid & hash_mask] = ulfs_rw64(offset, needswap);
    367 	if (hbp != bp) {
    368 		bwrite(hbp);
    369 	}
    370 	bwrite(bp);
    371 	return 0;
    372 }
    373 
    374 static int
    375 getinoquota2(struct inode *ip, bool alloc, bool modify, struct buf **bpp,
    376     struct quota2_entry **q2ep)
    377 {
    378 	int error;
    379 	int i;
    380 	struct dquot *dq;
    381 	struct ulfsmount *ump = ip->i_ump;
    382 	u_int32_t ino_ids[ULFS_MAXQUOTAS];
    383 
    384 	error = lfs_getinoquota(ip);
    385 	if (error)
    386 		return error;
    387 
    388         ino_ids[ULFS_USRQUOTA] = ip->i_uid;
    389         ino_ids[ULFS_GRPQUOTA] = ip->i_gid;
    390 	/* first get the interlock for all dquot */
    391 	for (i = 0; i < ULFS_MAXQUOTAS; i++) {
    392 		dq = ip->i_dquot[i];
    393 		if (dq == NODQUOT)
    394 			continue;
    395 		mutex_enter(&dq->dq_interlock);
    396 	}
    397 	/* now get the corresponding quota entry */
    398 	for (i = 0; i < ULFS_MAXQUOTAS; i++) {
    399 		bpp[i] = NULL;
    400 		q2ep[i] = NULL;
    401 		dq = ip->i_dquot[i];
    402 		if (dq == NODQUOT)
    403 			continue;
    404 		if (__predict_false(ump->um_quotas[i] == NULL)) {
    405 			/*
    406 			 * quotas have been turned off. This can happen
    407 			 * at umount time.
    408 			 */
    409 			mutex_exit(&dq->dq_interlock);
    410 			lfs_dqrele(NULLVP, dq);
    411 			ip->i_dquot[i] = NULL;
    412 			continue;
    413 		}
    414 
    415 		if ((dq->dq2_lblkno | dq->dq2_blkoff) == 0) {
    416 			if (!alloc) {
    417 				continue;
    418 			}
    419 			/* need to alloc a new on-disk quot */
    420 			mutex_enter(&lfs_dqlock);
    421 			error = quota2_q2ealloc(ump, i, ino_ids[i], dq);
    422 			mutex_exit(&lfs_dqlock);
    423 			if (error)
    424 				return error;
    425 		}
    426 		KASSERT(dq->dq2_lblkno != 0 || dq->dq2_blkoff != 0);
    427 		error = getq2e(ump, i, dq->dq2_lblkno,
    428 		    dq->dq2_blkoff, &bpp[i], &q2ep[i],
    429 		    modify ? B_MODIFY : 0);
    430 		if (error)
    431 			return error;
    432 	}
    433 	return 0;
    434 }
    435 
    436 __inline static int __unused
    437 lfsquota2_check_limit(struct quota2_val *q2v, uint64_t change, time_t now)
    438 {
    439 	return lfsquota_check_limit(q2v->q2v_cur, change, q2v->q2v_softlimit,
    440 	    q2v->q2v_hardlimit, q2v->q2v_time, now);
    441 }
    442 
    443 static int
    444 quota2_check(struct inode *ip, int vtype, int64_t change, kauth_cred_t cred,
    445     int flags)
    446 {
    447 	int error;
    448 	struct buf *bp[ULFS_MAXQUOTAS];
    449 	struct quota2_entry *q2e[ULFS_MAXQUOTAS];
    450 	struct quota2_val *q2vp;
    451 	struct dquot *dq;
    452 	uint64_t ncurblks;
    453 	struct ulfsmount *ump = ip->i_ump;
    454 	struct lfs *fs = ip->i_lfs;
    455 	struct mount *mp = ump->um_mountp;
    456 	const int needswap = ULFS_MPNEEDSWAP(fs);
    457 	int i;
    458 
    459 	if ((error = getinoquota2(ip, change > 0, change != 0, bp, q2e)) != 0)
    460 		return error;
    461 	if (change == 0) {
    462 		for (i = 0; i < ULFS_MAXQUOTAS; i++) {
    463 			dq = ip->i_dquot[i];
    464 			if (dq == NODQUOT)
    465 				continue;
    466 			if (bp[i])
    467 				brelse(bp[i], 0);
    468 			mutex_exit(&dq->dq_interlock);
    469 		}
    470 		return 0;
    471 	}
    472 	if (change < 0) {
    473 		for (i = 0; i < ULFS_MAXQUOTAS; i++) {
    474 			dq = ip->i_dquot[i];
    475 			if (dq == NODQUOT)
    476 				continue;
    477 			if (q2e[i] == NULL) {
    478 				mutex_exit(&dq->dq_interlock);
    479 				continue;
    480 			}
    481 			q2vp = &q2e[i]->q2e_val[vtype];
    482 			ncurblks = ulfs_rw64(q2vp->q2v_cur, needswap);
    483 			if (ncurblks < -change)
    484 				ncurblks = 0;
    485 			else
    486 				ncurblks += change;
    487 			q2vp->q2v_cur = ulfs_rw64(ncurblks, needswap);
    488 			quota2_bwrite(mp, bp[i]);
    489 			mutex_exit(&dq->dq_interlock);
    490 		}
    491 		return 0;
    492 	}
    493 	/* see if the allocation is allowed */
    494 	for (i = 0; i < ULFS_MAXQUOTAS; i++) {
    495 		struct quota2_val q2v;
    496 		int ql_stat;
    497 		dq = ip->i_dquot[i];
    498 		if (dq == NODQUOT)
    499 			continue;
    500 		KASSERT(q2e[i] != NULL);
    501 		lfsquota2_ulfs_rwq2v(&q2e[i]->q2e_val[vtype], &q2v, needswap);
    502 		ql_stat = lfsquota2_check_limit(&q2v, change, time_second);
    503 
    504 		if ((flags & FORCE) == 0 &&
    505 		    kauth_authorize_system(cred, KAUTH_SYSTEM_FS_QUOTA,
    506 		    KAUTH_REQ_SYSTEM_FS_QUOTA_NOLIMIT,
    507 		    KAUTH_ARG(i), KAUTH_ARG(vtype), NULL) != 0) {
    508 			/* enforce this limit */
    509 			switch(QL_STATUS(ql_stat)) {
    510 			case QL_S_DENY_HARD:
    511 				if ((dq->dq_flags & DQ_WARN(vtype)) == 0) {
    512 					uprintf("\n%s: write failed, %s %s "
    513 					    "limit reached\n",
    514 					    mp->mnt_stat.f_mntonname,
    515 					    lfs_quotatypes[i], limnames[vtype]);
    516 					dq->dq_flags |= DQ_WARN(vtype);
    517 				}
    518 				error = EDQUOT;
    519 				break;
    520 			case QL_S_DENY_GRACE:
    521 				if ((dq->dq_flags & DQ_WARN(vtype)) == 0) {
    522 					uprintf("\n%s: write failed, %s %s "
    523 					    "limit reached\n",
    524 					    mp->mnt_stat.f_mntonname,
    525 					    lfs_quotatypes[i], limnames[vtype]);
    526 					dq->dq_flags |= DQ_WARN(vtype);
    527 				}
    528 				error = EDQUOT;
    529 				break;
    530 			case QL_S_ALLOW_SOFT:
    531 				if ((dq->dq_flags & DQ_WARN(vtype)) == 0) {
    532 					uprintf("\n%s: warning, %s %s "
    533 					    "quota exceeded\n",
    534 					    mp->mnt_stat.f_mntonname,
    535 					    lfs_quotatypes[i], limnames[vtype]);
    536 					dq->dq_flags |= DQ_WARN(vtype);
    537 				}
    538 				break;
    539 			}
    540 		}
    541 		/*
    542 		 * always do this; we don't know if the allocation will
    543 		 * succed or not in the end. if we don't do the allocation
    544 		 * q2v_time will be ignored anyway
    545 		 */
    546 		if (ql_stat & QL_F_CROSS) {
    547 			q2v.q2v_time = time_second + q2v.q2v_grace;
    548 			lfsquota2_ulfs_rwq2v(&q2v, &q2e[i]->q2e_val[vtype],
    549 			    needswap);
    550 		}
    551 	}
    552 
    553 	/* now do the allocation if allowed */
    554 	for (i = 0; i < ULFS_MAXQUOTAS; i++) {
    555 		dq = ip->i_dquot[i];
    556 		if (dq == NODQUOT)
    557 			continue;
    558 		KASSERT(q2e[i] != NULL);
    559 		if (error == 0) {
    560 			q2vp = &q2e[i]->q2e_val[vtype];
    561 			ncurblks = ulfs_rw64(q2vp->q2v_cur, needswap);
    562 			q2vp->q2v_cur = ulfs_rw64(ncurblks + change, needswap);
    563 			quota2_bwrite(mp, bp[i]);
    564 		} else
    565 			brelse(bp[i], 0);
    566 		mutex_exit(&dq->dq_interlock);
    567 	}
    568 	return error;
    569 }
    570 
    571 int
    572 lfs_chkdq2(struct inode *ip, int64_t change, kauth_cred_t cred, int flags)
    573 {
    574 	return quota2_check(ip, QL_BLOCK, change, cred, flags);
    575 }
    576 
    577 int
    578 lfs_chkiq2(struct inode *ip, int32_t change, kauth_cred_t cred, int flags)
    579 {
    580 	return quota2_check(ip, QL_FILE, change, cred, flags);
    581 }
    582 
    583 int
    584 lfsquota2_handle_cmd_put(struct ulfsmount *ump, const struct quotakey *key,
    585     const struct quotaval *val)
    586 {
    587 	int error;
    588 	struct dquot *dq;
    589 	struct quota2_header *q2h;
    590 	struct quota2_entry q2e, *q2ep;
    591 	struct buf *bp;
    592 	struct lfs *fs = ump->um_lfs;
    593 	const int needswap = ULFS_MPNEEDSWAP(fs);
    594 
    595 	/* make sure we can index by the fs-independent idtype */
    596 	CTASSERT(QUOTA_IDTYPE_USER == ULFS_USRQUOTA);
    597 	CTASSERT(QUOTA_IDTYPE_GROUP == ULFS_GRPQUOTA);
    598 
    599 	if (ump->um_quotas[key->qk_idtype] == NULLVP)
    600 		return ENODEV;
    601 
    602 	if (key->qk_id == QUOTA_DEFAULTID) {
    603 		mutex_enter(&lfs_dqlock);
    604 		error = getq2h(ump, key->qk_idtype, &bp, &q2h, B_MODIFY);
    605 		if (error) {
    606 			mutex_exit(&lfs_dqlock);
    607 			goto out_wapbl;
    608 		}
    609 		lfsquota2_ulfs_rwq2e(&q2h->q2h_defentry, &q2e, needswap);
    610 		quota2_dict_update_q2e_limits(key->qk_objtype, val, &q2e);
    611 		lfsquota2_ulfs_rwq2e(&q2e, &q2h->q2h_defentry, needswap);
    612 		mutex_exit(&lfs_dqlock);
    613 		quota2_bwrite(ump->um_mountp, bp);
    614 		goto out_wapbl;
    615 	}
    616 
    617 	error = lfs_dqget(NULLVP, key->qk_id, ump, key->qk_idtype, &dq);
    618 	if (error)
    619 		goto out_wapbl;
    620 
    621 	mutex_enter(&dq->dq_interlock);
    622 	if (dq->dq2_lblkno == 0 && dq->dq2_blkoff == 0) {
    623 		/* need to alloc a new on-disk quot */
    624 		mutex_enter(&lfs_dqlock);
    625 		error = quota2_q2ealloc(ump, key->qk_idtype, key->qk_id, dq);
    626 		mutex_exit(&lfs_dqlock);
    627 		if (error)
    628 			goto out_il;
    629 	}
    630 	KASSERT(dq->dq2_lblkno != 0 || dq->dq2_blkoff != 0);
    631 	error = getq2e(ump, key->qk_idtype, dq->dq2_lblkno,
    632 	    dq->dq2_blkoff, &bp, &q2ep, B_MODIFY);
    633 	if (error)
    634 		goto out_il;
    635 
    636 	lfsquota2_ulfs_rwq2e(q2ep, &q2e, needswap);
    637 	quota2_dict_update_q2e_limits(key->qk_objtype, val, &q2e);
    638 	lfsquota2_ulfs_rwq2e(&q2e, q2ep, needswap);
    639 	quota2_bwrite(ump->um_mountp, bp);
    640 
    641 out_il:
    642 	mutex_exit(&dq->dq_interlock);
    643 	lfs_dqrele(NULLVP, dq);
    644 out_wapbl:
    645 	return error;
    646 }
    647 
    648 struct dq2clear_callback {
    649 	uid_t id;
    650 	struct dquot *dq;
    651 	struct quota2_header *q2h;
    652 };
    653 
    654 static int
    655 dq2clear_callback(struct ulfsmount *ump, uint64_t *offp, struct quota2_entry *q2e,
    656     uint64_t off, void *v)
    657 {
    658 	struct dq2clear_callback *c = v;
    659 #ifdef LFS_EI
    660 	struct lfs *fs = ump->um_lfs;
    661 	const int needswap = ULFS_MPNEEDSWAP(fs);
    662 #endif
    663 	uint64_t myoff;
    664 
    665 	if (ulfs_rw32(q2e->q2e_uid, needswap) == c->id) {
    666 		KASSERT(mutex_owned(&c->dq->dq_interlock));
    667 		c->dq->dq2_lblkno = 0;
    668 		c->dq->dq2_blkoff = 0;
    669 		myoff = *offp;
    670 		/* remove from hash list */
    671 		*offp = q2e->q2e_next;
    672 		/* add to free list */
    673 		q2e->q2e_next = c->q2h->q2h_free;
    674 		c->q2h->q2h_free = myoff;
    675 		return Q2WL_ABORT;
    676 	}
    677 	return 0;
    678 }
    679 int
    680 lfsquota2_handle_cmd_delete(struct ulfsmount *ump, const struct quotakey *qk)
    681 {
    682 	int idtype;
    683 	id_t id;
    684 	int objtype;
    685 	int error, i, canfree;
    686 	struct dquot *dq;
    687 	struct quota2_header *q2h;
    688 	struct quota2_entry q2e, *q2ep;
    689 	struct buf *hbp, *bp;
    690 	u_long hash_mask;
    691 	struct dq2clear_callback c;
    692 
    693 	idtype = qk->qk_idtype;
    694 	id = qk->qk_id;
    695 	objtype = qk->qk_objtype;
    696 
    697 	if (ump->um_quotas[idtype] == NULLVP)
    698 		return ENODEV;
    699 	if (id == QUOTA_DEFAULTID)
    700 		return EOPNOTSUPP;
    701 
    702 	/* get the default entry before locking the entry's buffer */
    703 	mutex_enter(&lfs_dqlock);
    704 	error = getq2h(ump, idtype, &hbp, &q2h, 0);
    705 	if (error) {
    706 		mutex_exit(&lfs_dqlock);
    707 		return error;
    708 	}
    709 	/* we'll copy to another disk entry, so no need to swap */
    710 	memcpy(&q2e, &q2h->q2h_defentry, sizeof(q2e));
    711 	mutex_exit(&lfs_dqlock);
    712 	brelse(hbp, 0);
    713 
    714 	error = lfs_dqget(NULLVP, id, ump, idtype, &dq);
    715 	if (error)
    716 		return error;
    717 
    718 	mutex_enter(&dq->dq_interlock);
    719 	if (dq->dq2_lblkno == 0 && dq->dq2_blkoff == 0) {
    720 		/* already clear, nothing to do */
    721 		error = ENOENT;
    722 		goto out_il;
    723 	}
    724 
    725 	error = getq2e(ump, idtype, dq->dq2_lblkno, dq->dq2_blkoff,
    726 	    &bp, &q2ep, B_MODIFY);
    727 	if (error)
    728 		goto out_wapbl;
    729 
    730 	/* make sure we can index by the objtype passed in */
    731 	CTASSERT(QUOTA_OBJTYPE_BLOCKS == QL_BLOCK);
    732 	CTASSERT(QUOTA_OBJTYPE_FILES == QL_FILE);
    733 
    734 	/* clear the requested objtype by copying from the default entry */
    735 	q2ep->q2e_val[objtype].q2v_softlimit =
    736 		q2e.q2e_val[objtype].q2v_softlimit;
    737 	q2ep->q2e_val[objtype].q2v_hardlimit =
    738 		q2e.q2e_val[objtype].q2v_hardlimit;
    739 	q2ep->q2e_val[objtype].q2v_grace =
    740 		q2e.q2e_val[objtype].q2v_grace;
    741 	q2ep->q2e_val[objtype].q2v_time = 0;
    742 
    743 	/* if this entry now contains no information, we can free it */
    744 	canfree = 1;
    745 	for (i = 0; i < N_QL; i++) {
    746 		if (q2ep->q2e_val[i].q2v_cur != 0 ||
    747 		    (q2ep->q2e_val[i].q2v_softlimit !=
    748 		     q2e.q2e_val[i].q2v_softlimit) ||
    749 		    (q2ep->q2e_val[i].q2v_hardlimit !=
    750 		     q2e.q2e_val[i].q2v_hardlimit) ||
    751 		    (q2ep->q2e_val[i].q2v_grace !=
    752 		     q2e.q2e_val[i].q2v_grace)) {
    753 			canfree = 0;
    754 			break;
    755 		}
    756 		/* note: do not need to check q2v_time */
    757 	}
    758 
    759 	if (canfree == 0) {
    760 		quota2_bwrite(ump->um_mountp, bp);
    761 		goto out_wapbl;
    762 	}
    763 	/* we can free it. release bp so we can walk the list */
    764 	brelse(bp, 0);
    765 	mutex_enter(&lfs_dqlock);
    766 	error = getq2h(ump, idtype, &hbp, &q2h, 0);
    767 	if (error)
    768 		goto out_dqlock;
    769 
    770 	hash_mask = ((1 << q2h->q2h_hash_shift) - 1);
    771 	c.dq = dq;
    772 	c.id = id;
    773 	c.q2h = q2h;
    774 	error = quota2_walk_list(ump, hbp, idtype,
    775 	    &q2h->q2h_entries[id & hash_mask], B_MODIFY, &c,
    776 	    dq2clear_callback);
    777 
    778 	bwrite(hbp);
    779 
    780 out_dqlock:
    781 	mutex_exit(&lfs_dqlock);
    782 out_wapbl:
    783 out_il:
    784 	mutex_exit(&dq->dq_interlock);
    785 	lfs_dqrele(NULLVP, dq);
    786 	return error;
    787 }
    788 
    789 static int
    790 quota2_fetch_q2e(struct ulfsmount *ump, const struct quotakey *qk,
    791     struct quota2_entry *ret)
    792 {
    793 	struct dquot *dq;
    794 	int error;
    795 	struct quota2_entry *q2ep;
    796 	struct buf *bp;
    797 	struct lfs *fs = ump->um_lfs;
    798 	const int needswap = ULFS_MPNEEDSWAP(fs);
    799 
    800 	error = lfs_dqget(NULLVP, qk->qk_id, ump, qk->qk_idtype, &dq);
    801 	if (error)
    802 		return error;
    803 
    804 	mutex_enter(&dq->dq_interlock);
    805 	if (dq->dq2_lblkno == 0 && dq->dq2_blkoff == 0) {
    806 		mutex_exit(&dq->dq_interlock);
    807 		lfs_dqrele(NULLVP, dq);
    808 		return ENOENT;
    809 	}
    810 	error = getq2e(ump, qk->qk_idtype, dq->dq2_lblkno, dq->dq2_blkoff,
    811 	    &bp, &q2ep, 0);
    812 	if (error) {
    813 		mutex_exit(&dq->dq_interlock);
    814 		lfs_dqrele(NULLVP, dq);
    815 		return error;
    816 	}
    817 	lfsquota2_ulfs_rwq2e(q2ep, ret, needswap);
    818 	brelse(bp, 0);
    819 	mutex_exit(&dq->dq_interlock);
    820 	lfs_dqrele(NULLVP, dq);
    821 
    822 	return 0;
    823 }
    824 
    825 static int
    826 quota2_fetch_quotaval(struct ulfsmount *ump, const struct quotakey *qk,
    827     struct quotaval *ret)
    828 {
    829 	struct dquot *dq;
    830 	int error;
    831 	struct quota2_entry *q2ep, q2e;
    832 	struct buf  *bp;
    833 	struct lfs *fs = ump->um_lfs;
    834 	const int needswap = ULFS_MPNEEDSWAP(fs);
    835 	id_t id2;
    836 
    837 	error = lfs_dqget(NULLVP, qk->qk_id, ump, qk->qk_idtype, &dq);
    838 	if (error)
    839 		return error;
    840 
    841 	mutex_enter(&dq->dq_interlock);
    842 	if (dq->dq2_lblkno == 0 && dq->dq2_blkoff == 0) {
    843 		mutex_exit(&dq->dq_interlock);
    844 		lfs_dqrele(NULLVP, dq);
    845 		return ENOENT;
    846 	}
    847 	error = getq2e(ump, qk->qk_idtype, dq->dq2_lblkno, dq->dq2_blkoff,
    848 	    &bp, &q2ep, 0);
    849 	if (error) {
    850 		mutex_exit(&dq->dq_interlock);
    851 		lfs_dqrele(NULLVP, dq);
    852 		return error;
    853 	}
    854 	lfsquota2_ulfs_rwq2e(q2ep, &q2e, needswap);
    855 	brelse(bp, 0);
    856 	mutex_exit(&dq->dq_interlock);
    857 	lfs_dqrele(NULLVP, dq);
    858 
    859 	q2e_to_quotaval(&q2e, 0, &id2, qk->qk_objtype, ret);
    860 	KASSERT(id2 == qk->qk_id);
    861 	return 0;
    862 }
    863 
    864 int
    865 lfsquota2_handle_cmd_get(struct ulfsmount *ump, const struct quotakey *qk,
    866     struct quotaval *qv)
    867 {
    868 	int error;
    869 	struct quota2_header *q2h;
    870 	struct quota2_entry q2e;
    871 	struct buf *bp;
    872 	struct lfs *fs = ump->um_lfs;
    873 	const int needswap = ULFS_MPNEEDSWAP(fs);
    874 	id_t id2;
    875 
    876 	/*
    877 	 * Make sure the FS-independent codes match the internal ones,
    878 	 * so we can use the passed-in objtype without having to
    879 	 * convert it explicitly to QL_BLOCK/QL_FILE.
    880 	 */
    881 	CTASSERT(QL_BLOCK == QUOTA_OBJTYPE_BLOCKS);
    882 	CTASSERT(QL_FILE == QUOTA_OBJTYPE_FILES);
    883 	CTASSERT(N_QL == 2);
    884 
    885 	if (qk->qk_objtype < 0 || qk->qk_objtype >= N_QL) {
    886 		return EINVAL;
    887 	}
    888 
    889 	if (ump->um_quotas[qk->qk_idtype] == NULLVP)
    890 		return ENODEV;
    891 	if (qk->qk_id == QUOTA_DEFAULTID) {
    892 		mutex_enter(&lfs_dqlock);
    893 		error = getq2h(ump, qk->qk_idtype, &bp, &q2h, 0);
    894 		if (error) {
    895 			mutex_exit(&lfs_dqlock);
    896 			return error;
    897 		}
    898 		lfsquota2_ulfs_rwq2e(&q2h->q2h_defentry, &q2e, needswap);
    899 		mutex_exit(&lfs_dqlock);
    900 		brelse(bp, 0);
    901 		q2e_to_quotaval(&q2e, qk->qk_id == QUOTA_DEFAULTID, &id2,
    902 				qk->qk_objtype, qv);
    903 		(void)id2;
    904 	} else
    905 		error = quota2_fetch_quotaval(ump, qk, qv);
    906 
    907 	return error;
    908 }
    909 
    910 /*
    911  * Cursor structure we used.
    912  *
    913  * This will get stored in userland between calls so we must not assume
    914  * it isn't arbitrarily corrupted.
    915  */
    916 struct ulfsq2_cursor {
    917 	uint32_t q2c_magic;	/* magic number */
    918 	int q2c_hashsize;	/* size of hash table at last go */
    919 
    920 	int q2c_users_done;	/* true if we've returned all user data */
    921 	int q2c_groups_done;	/* true if we've returned all group data */
    922 	int q2c_defaults_done;	/* true if we've returned the default values */
    923 	int q2c_hashpos;	/* slot to start at in hash table */
    924 	int q2c_uidpos;		/* number of ids we've handled */
    925 	int q2c_blocks_done;	/* true if we've returned the blocks value */
    926 };
    927 
    928 /*
    929  * State of a single cursorget call, or at least the part of it that
    930  * needs to be passed around.
    931  */
    932 struct q2cursor_state {
    933 	/* data return pointers */
    934 	struct quotakey *keys;
    935 	struct quotaval *vals;
    936 
    937 	/* key/value counters */
    938 	unsigned maxkeyvals;
    939 	unsigned numkeys;	/* number of keys assigned */
    940 
    941 	/* ID to key/value conversion state */
    942 	int skipfirst;		/* if true skip first key/value */
    943 	int skiplast;		/* if true skip last key/value */
    944 
    945 	/* ID counters */
    946 	unsigned maxids;	/* maximum number of IDs to handle */
    947 	unsigned numids;	/* number of IDs handled */
    948 };
    949 
    950 /*
    951  * Additional structure for getids callback.
    952  */
    953 struct q2cursor_getids {
    954 	struct q2cursor_state *state;
    955 	int idtype;
    956 	unsigned skip;		/* number of ids to skip over */
    957 	unsigned new_skip;	/* number of ids to skip over next time */
    958 	unsigned skipped;	/* number skipped so far */
    959 	int stopped;		/* true if we stopped quota_walk_list early */
    960 };
    961 
    962 /*
    963  * Cursor-related functions
    964  */
    965 
    966 /* magic number */
    967 #define Q2C_MAGIC (0xbeebe111)
    968 
    969 /* extract cursor from caller form */
    970 #define Q2CURSOR(qkc) ((struct ulfsq2_cursor *)&qkc->u.qkc_space[0])
    971 
    972 /*
    973  * Check that a cursor we're handed is something like valid. If
    974  * someone munges it and it still passes these checks, they'll get
    975  * partial or odd results back but won't break anything.
    976  */
    977 static int
    978 q2cursor_check(struct ulfsq2_cursor *cursor)
    979 {
    980 	if (cursor->q2c_magic != Q2C_MAGIC) {
    981 		return EINVAL;
    982 	}
    983 	if (cursor->q2c_hashsize < 0) {
    984 		return EINVAL;
    985 	}
    986 
    987 	if (cursor->q2c_users_done != 0 && cursor->q2c_users_done != 1) {
    988 		return EINVAL;
    989 	}
    990 	if (cursor->q2c_groups_done != 0 && cursor->q2c_groups_done != 1) {
    991 		return EINVAL;
    992 	}
    993 	if (cursor->q2c_defaults_done != 0 && cursor->q2c_defaults_done != 1) {
    994 		return EINVAL;
    995 	}
    996 	if (cursor->q2c_hashpos < 0 || cursor->q2c_uidpos < 0) {
    997 		return EINVAL;
    998 	}
    999 	if (cursor->q2c_blocks_done != 0 && cursor->q2c_blocks_done != 1) {
   1000 		return EINVAL;
   1001 	}
   1002 	return 0;
   1003 }
   1004 
   1005 /*
   1006  * Set up the q2cursor state.
   1007  */
   1008 static void
   1009 q2cursor_initstate(struct q2cursor_state *state, struct quotakey *keys,
   1010     struct quotaval *vals, unsigned maxkeyvals, int blocks_done)
   1011 {
   1012 	state->keys = keys;
   1013 	state->vals = vals;
   1014 
   1015 	state->maxkeyvals = maxkeyvals;
   1016 	state->numkeys = 0;
   1017 
   1018 	/*
   1019 	 * For each ID there are two quotavals to return. If the
   1020 	 * maximum number of entries to return is odd, we might want
   1021 	 * to skip the first quotaval of the first ID, or the last
   1022 	 * quotaval of the last ID, but not both. So the number of IDs
   1023 	 * we want is (up to) half the number of return slots we have,
   1024 	 * rounded up.
   1025 	 */
   1026 
   1027 	state->maxids = (state->maxkeyvals + 1) / 2;
   1028 	state->numids = 0;
   1029 	if (state->maxkeyvals % 2) {
   1030 		if (blocks_done) {
   1031 			state->skipfirst = 1;
   1032 			state->skiplast = 0;
   1033 		} else {
   1034 			state->skipfirst = 0;
   1035 			state->skiplast = 1;
   1036 		}
   1037 	} else {
   1038 		state->skipfirst = 0;
   1039 		state->skiplast = 0;
   1040 	}
   1041 }
   1042 
   1043 /*
   1044  * Choose which idtype we're going to work on. If doing a full
   1045  * iteration, we do users first, then groups, but either might be
   1046  * disabled or marked to skip via cursorsetidtype(), so don't make
   1047  * silly assumptions.
   1048  */
   1049 static int
   1050 q2cursor_pickidtype(struct ulfsq2_cursor *cursor, int *idtype_ret)
   1051 {
   1052 	if (cursor->q2c_users_done == 0) {
   1053 		*idtype_ret = QUOTA_IDTYPE_USER;
   1054 	} else if (cursor->q2c_groups_done == 0) {
   1055 		*idtype_ret = QUOTA_IDTYPE_GROUP;
   1056 	} else {
   1057 		return EAGAIN;
   1058 	}
   1059 	return 0;
   1060 }
   1061 
   1062 /*
   1063  * Add an ID to the current state. Sets up either one or two keys to
   1064  * refer to it, depending on whether it's first/last and the setting
   1065  * of skipfirst. (skiplast does not need to be explicitly tested)
   1066  */
   1067 static void
   1068 q2cursor_addid(struct q2cursor_state *state, int idtype, id_t id)
   1069 {
   1070 	KASSERT(state->numids < state->maxids);
   1071 	KASSERT(state->numkeys < state->maxkeyvals);
   1072 
   1073 	if (!state->skipfirst || state->numkeys > 0) {
   1074 		state->keys[state->numkeys].qk_idtype = idtype;
   1075 		state->keys[state->numkeys].qk_id = id;
   1076 		state->keys[state->numkeys].qk_objtype = QUOTA_OBJTYPE_BLOCKS;
   1077 		state->numkeys++;
   1078 	}
   1079 	if (state->numkeys < state->maxkeyvals) {
   1080 		state->keys[state->numkeys].qk_idtype = idtype;
   1081 		state->keys[state->numkeys].qk_id = id;
   1082 		state->keys[state->numkeys].qk_objtype = QUOTA_OBJTYPE_FILES;
   1083 		state->numkeys++;
   1084 	} else {
   1085 		KASSERT(state->skiplast);
   1086 	}
   1087 	state->numids++;
   1088 }
   1089 
   1090 /*
   1091  * Callback function for getting IDs. Update counting and call addid.
   1092  */
   1093 static int
   1094 q2cursor_getids_callback(struct ulfsmount *ump, uint64_t *offp,
   1095     struct quota2_entry *q2ep, uint64_t off, void *v)
   1096 {
   1097 	struct q2cursor_getids *gi = v;
   1098 	id_t id;
   1099 #ifdef LFS_EI
   1100 	struct lfs *fs = ump->um_lfs;
   1101 	const int needswap = ULFS_MPNEEDSWAP(fs);
   1102 #endif
   1103 
   1104 	if (gi->skipped < gi->skip) {
   1105 		gi->skipped++;
   1106 		return 0;
   1107 	}
   1108 	id = ulfs_rw32(q2ep->q2e_uid, needswap);
   1109 	q2cursor_addid(gi->state, gi->idtype, id);
   1110 	gi->new_skip++;
   1111 	if (gi->state->numids >= gi->state->maxids) {
   1112 		/* got enough ids, stop now */
   1113 		gi->stopped = 1;
   1114 		return Q2WL_ABORT;
   1115 	}
   1116 	return 0;
   1117 }
   1118 
   1119 /*
   1120  * Fill in a batch of quotakeys by scanning one or more hash chains.
   1121  */
   1122 static int
   1123 q2cursor_getkeys(struct ulfsmount *ump, int idtype, struct ulfsq2_cursor *cursor,
   1124     struct q2cursor_state *state,
   1125     int *hashsize_ret, struct quota2_entry *default_q2e_ret)
   1126 {
   1127 	struct lfs *fs = ump->um_lfs;
   1128 	const int needswap = ULFS_MPNEEDSWAP(fs);
   1129 	struct buf *hbp;
   1130 	struct quota2_header *q2h;
   1131 	int quota2_hash_size;
   1132 	struct q2cursor_getids gi;
   1133 	uint64_t offset;
   1134 	int error;
   1135 
   1136 	/*
   1137 	 * Read the header block.
   1138 	 */
   1139 
   1140 	mutex_enter(&lfs_dqlock);
   1141 	error = getq2h(ump, idtype, &hbp, &q2h, 0);
   1142 	if (error) {
   1143 		mutex_exit(&lfs_dqlock);
   1144 		return error;
   1145 	}
   1146 
   1147 	/* if the table size has changed, make the caller start over */
   1148 	quota2_hash_size = ulfs_rw16(q2h->q2h_hash_size, needswap);
   1149 	if (cursor->q2c_hashsize == 0) {
   1150 		cursor->q2c_hashsize = quota2_hash_size;
   1151 	} else if (cursor->q2c_hashsize != quota2_hash_size) {
   1152 		error = EDEADLK;
   1153 		goto scanfail;
   1154 	}
   1155 
   1156 	/* grab the entry with the default values out of the header */
   1157 	lfsquota2_ulfs_rwq2e(&q2h->q2h_defentry, default_q2e_ret, needswap);
   1158 
   1159 	/* If we haven't done the defaults yet, that goes first. */
   1160 	if (cursor->q2c_defaults_done == 0) {
   1161 		q2cursor_addid(state, idtype, QUOTA_DEFAULTID);
   1162 		/* if we read both halves, mark it done */
   1163 		if (state->numids < state->maxids || !state->skiplast) {
   1164 			cursor->q2c_defaults_done = 1;
   1165 		}
   1166 	}
   1167 
   1168 	gi.state = state;
   1169 	gi.idtype = idtype;
   1170 
   1171 	while (state->numids < state->maxids) {
   1172 		if (cursor->q2c_hashpos >= quota2_hash_size) {
   1173 			/* nothing more left */
   1174 			break;
   1175 		}
   1176 
   1177 		/* scan this hash chain */
   1178 		gi.skip = cursor->q2c_uidpos;
   1179 		gi.new_skip = gi.skip;
   1180 		gi.skipped = 0;
   1181 		gi.stopped = 0;
   1182 		offset = q2h->q2h_entries[cursor->q2c_hashpos];
   1183 
   1184 		error = quota2_walk_list(ump, hbp, idtype, &offset, 0, &gi,
   1185 		    q2cursor_getids_callback);
   1186 		KASSERT(error != Q2WL_ABORT);
   1187 		if (error) {
   1188 			break;
   1189 		}
   1190 		if (gi.stopped) {
   1191 			/* callback stopped before reading whole chain */
   1192 			cursor->q2c_uidpos = gi.new_skip;
   1193 			/* if we didn't get both halves, back up */
   1194 			if (state->numids == state->maxids && state->skiplast){
   1195 				KASSERT(cursor->q2c_uidpos > 0);
   1196 				cursor->q2c_uidpos--;
   1197 			}
   1198 		} else {
   1199 			/* read whole chain */
   1200 			/* if we got both halves of the last id, advance */
   1201 			if (state->numids < state->maxids || !state->skiplast){
   1202 				cursor->q2c_uidpos = 0;
   1203 				cursor->q2c_hashpos++;
   1204 			}
   1205 		}
   1206 	}
   1207 
   1208 scanfail:
   1209 	mutex_exit(&lfs_dqlock);
   1210 	brelse(hbp, 0);
   1211 	if (error)
   1212 		return error;
   1213 
   1214 	*hashsize_ret = quota2_hash_size;
   1215 	return 0;
   1216 }
   1217 
   1218 /*
   1219  * Fetch the quotavals for the quotakeys.
   1220  */
   1221 static int
   1222 q2cursor_getvals(struct ulfsmount *ump, struct q2cursor_state *state,
   1223     const struct quota2_entry *default_q2e)
   1224 {
   1225 	int hasid;
   1226 	id_t loadedid, id;
   1227 	unsigned pos;
   1228 	struct quota2_entry q2e;
   1229 	int objtype;
   1230 	int error;
   1231 
   1232 	hasid = 0;
   1233 	loadedid = 0;
   1234 	for (pos = 0; pos < state->numkeys; pos++) {
   1235 		id = state->keys[pos].qk_id;
   1236 		if (!hasid || id != loadedid) {
   1237 			hasid = 1;
   1238 			loadedid = id;
   1239 			if (id == QUOTA_DEFAULTID) {
   1240 				q2e = *default_q2e;
   1241 			} else {
   1242 				error = quota2_fetch_q2e(ump,
   1243 							 &state->keys[pos],
   1244 							 &q2e);
   1245 				if (error == ENOENT) {
   1246 					/* something changed - start over */
   1247 					error = EDEADLK;
   1248 				}
   1249 				if (error) {
   1250 					return error;
   1251 				}
   1252  			}
   1253 		}
   1254 
   1255 
   1256 		objtype = state->keys[pos].qk_objtype;
   1257 		KASSERT(objtype >= 0 && objtype < N_QL);
   1258 		q2val_to_quotaval(&q2e.q2e_val[objtype], &state->vals[pos]);
   1259 	}
   1260 
   1261 	return 0;
   1262 }
   1263 
   1264 /*
   1265  * Handle cursorget.
   1266  *
   1267  * We can't just read keys and values directly, because we can't walk
   1268  * the list with qdlock and grab dq_interlock to read the entries at
   1269  * the same time. So we're going to do two passes: one to figure out
   1270  * which IDs we want and fill in the keys, and then a second to use
   1271  * the keys to fetch the values.
   1272  */
   1273 int
   1274 lfsquota2_handle_cmd_cursorget(struct ulfsmount *ump, struct quotakcursor *qkc,
   1275     struct quotakey *keys, struct quotaval *vals, unsigned maxreturn,
   1276     unsigned *ret)
   1277 {
   1278 	int error;
   1279 	struct ulfsq2_cursor *cursor;
   1280 	struct ulfsq2_cursor newcursor;
   1281 	struct q2cursor_state state;
   1282 	struct quota2_entry default_q2e;
   1283 	int idtype;
   1284 	int quota2_hash_size;
   1285 
   1286 	/*
   1287 	 * Convert and validate the cursor.
   1288 	 */
   1289 	cursor = Q2CURSOR(qkc);
   1290 	error = q2cursor_check(cursor);
   1291 	if (error) {
   1292 		return error;
   1293 	}
   1294 
   1295 	/*
   1296 	 * Make sure our on-disk codes match the values of the
   1297 	 * FS-independent ones. This avoids the need for explicit
   1298 	 * conversion (which would be a NOP anyway and thus easily
   1299 	 * left out or called in the wrong places...)
   1300 	 */
   1301 	CTASSERT(QUOTA_IDTYPE_USER == ULFS_USRQUOTA);
   1302 	CTASSERT(QUOTA_IDTYPE_GROUP == ULFS_GRPQUOTA);
   1303 	CTASSERT(QUOTA_OBJTYPE_BLOCKS == QL_BLOCK);
   1304 	CTASSERT(QUOTA_OBJTYPE_FILES == QL_FILE);
   1305 
   1306 	/*
   1307 	 * If some of the idtypes aren't configured/enabled, arrange
   1308 	 * to skip over them.
   1309 	 */
   1310 	if (cursor->q2c_users_done == 0 &&
   1311 	    ump->um_quotas[ULFS_USRQUOTA] == NULLVP) {
   1312 		cursor->q2c_users_done = 1;
   1313 	}
   1314 	if (cursor->q2c_groups_done == 0 &&
   1315 	    ump->um_quotas[ULFS_GRPQUOTA] == NULLVP) {
   1316 		cursor->q2c_groups_done = 1;
   1317 	}
   1318 
   1319 	/* Loop over, potentially, both idtypes */
   1320 	while (1) {
   1321 
   1322 		/* Choose id type */
   1323 		error = q2cursor_pickidtype(cursor, &idtype);
   1324 		if (error == EAGAIN) {
   1325 			/* nothing more to do, return 0 */
   1326 			*ret = 0;
   1327 			return 0;
   1328 		}
   1329 		KASSERT(ump->um_quotas[idtype] != NULLVP);
   1330 
   1331 		/*
   1332 		 * Initialize the per-call iteration state. Copy the
   1333 		 * cursor state so we can update it in place but back
   1334 		 * out on error.
   1335 		 */
   1336 		q2cursor_initstate(&state, keys, vals, maxreturn,
   1337 				   cursor->q2c_blocks_done);
   1338 		newcursor = *cursor;
   1339 
   1340 		/* Assign keys */
   1341 		error = q2cursor_getkeys(ump, idtype, &newcursor, &state,
   1342 					 &quota2_hash_size, &default_q2e);
   1343 		if (error) {
   1344 			return error;
   1345 		}
   1346 
   1347 		/* Now fill in the values. */
   1348 		error = q2cursor_getvals(ump, &state, &default_q2e);
   1349 		if (error) {
   1350 			return error;
   1351 		}
   1352 
   1353 		/*
   1354 		 * Now that we aren't going to fail and lose what we
   1355 		 * did so far, we can update the cursor state.
   1356 		 */
   1357 
   1358 		if (newcursor.q2c_hashpos >= quota2_hash_size) {
   1359 			if (idtype == QUOTA_IDTYPE_USER)
   1360 				cursor->q2c_users_done = 1;
   1361 			else
   1362 				cursor->q2c_groups_done = 1;
   1363 
   1364 			/* start over on another id type */
   1365 			cursor->q2c_hashsize = 0;
   1366 			cursor->q2c_defaults_done = 0;
   1367 			cursor->q2c_hashpos = 0;
   1368 			cursor->q2c_uidpos = 0;
   1369 			cursor->q2c_blocks_done = 0;
   1370 		} else {
   1371 			*cursor = newcursor;
   1372 			cursor->q2c_blocks_done = state.skiplast;
   1373 		}
   1374 
   1375 		/*
   1376 		 * If we have something to return, return it.
   1377 		 * Otherwise, continue to the other idtype, if any,
   1378 		 * and only return zero at end of iteration.
   1379 		 */
   1380 		if (state.numkeys > 0) {
   1381 			break;
   1382 		}
   1383 	}
   1384 
   1385 	*ret = state.numkeys;
   1386 	return 0;
   1387 }
   1388 
   1389 int
   1390 lfsquota2_handle_cmd_cursoropen(struct ulfsmount *ump, struct quotakcursor *qkc)
   1391 {
   1392 	struct ulfsq2_cursor *cursor;
   1393 
   1394 	CTASSERT(sizeof(*cursor) <= sizeof(qkc->u.qkc_space));
   1395 	cursor = Q2CURSOR(qkc);
   1396 
   1397 	cursor->q2c_magic = Q2C_MAGIC;
   1398 	cursor->q2c_hashsize = 0;
   1399 
   1400 	cursor->q2c_users_done = 0;
   1401 	cursor->q2c_groups_done = 0;
   1402 	cursor->q2c_defaults_done = 0;
   1403 	cursor->q2c_hashpos = 0;
   1404 	cursor->q2c_uidpos = 0;
   1405 	cursor->q2c_blocks_done = 0;
   1406 	return 0;
   1407 }
   1408 
   1409 int
   1410 lfsquota2_handle_cmd_cursorclose(struct ulfsmount *ump, struct quotakcursor *qkc)
   1411 {
   1412 	struct ulfsq2_cursor *cursor;
   1413 	int error;
   1414 
   1415 	cursor = Q2CURSOR(qkc);
   1416 	error = q2cursor_check(cursor);
   1417 	if (error) {
   1418 		return error;
   1419 	}
   1420 
   1421 	/* nothing to do */
   1422 
   1423 	return 0;
   1424 }
   1425 
   1426 int
   1427 lfsquota2_handle_cmd_cursorskipidtype(struct ulfsmount *ump,
   1428     struct quotakcursor *qkc, int idtype)
   1429 {
   1430 	struct ulfsq2_cursor *cursor;
   1431 	int error;
   1432 
   1433 	cursor = Q2CURSOR(qkc);
   1434 	error = q2cursor_check(cursor);
   1435 	if (error) {
   1436 		return error;
   1437 	}
   1438 
   1439 	switch (idtype) {
   1440 	    case QUOTA_IDTYPE_USER:
   1441 		cursor->q2c_users_done = 1;
   1442 		break;
   1443 	    case QUOTA_IDTYPE_GROUP:
   1444 		cursor->q2c_groups_done = 1;
   1445 		break;
   1446 	    default:
   1447 		return EINVAL;
   1448 	}
   1449 
   1450 	return 0;
   1451 }
   1452 
   1453 int
   1454 lfsquota2_handle_cmd_cursoratend(struct ulfsmount *ump, struct quotakcursor *qkc,
   1455     int *ret)
   1456 {
   1457 	struct ulfsq2_cursor *cursor;
   1458 	int error;
   1459 
   1460 	cursor = Q2CURSOR(qkc);
   1461 	error = q2cursor_check(cursor);
   1462 	if (error) {
   1463 		return error;
   1464 	}
   1465 
   1466 	*ret = (cursor->q2c_users_done && cursor->q2c_groups_done);
   1467 	return 0;
   1468 }
   1469 
   1470 int
   1471 lfsquota2_handle_cmd_cursorrewind(struct ulfsmount *ump, struct quotakcursor *qkc)
   1472 {
   1473 	struct ulfsq2_cursor *cursor;
   1474 	int error;
   1475 
   1476 	cursor = Q2CURSOR(qkc);
   1477 	error = q2cursor_check(cursor);
   1478 	if (error) {
   1479 		return error;
   1480 	}
   1481 
   1482 	cursor->q2c_hashsize = 0;
   1483 
   1484 	cursor->q2c_users_done = 0;
   1485 	cursor->q2c_groups_done = 0;
   1486 	cursor->q2c_defaults_done = 0;
   1487 	cursor->q2c_hashpos = 0;
   1488 	cursor->q2c_uidpos = 0;
   1489 	cursor->q2c_blocks_done = 0;
   1490 
   1491 	return 0;
   1492 }
   1493 
   1494 int
   1495 lfs_q2sync(struct mount *mp)
   1496 {
   1497 	return 0;
   1498 }
   1499 
   1500 struct dq2get_callback {
   1501 	uid_t id;
   1502 	struct dquot *dq;
   1503 };
   1504 
   1505 static int
   1506 dq2get_callback(struct ulfsmount *ump, uint64_t *offp, struct quota2_entry *q2e,
   1507     uint64_t off, void *v)
   1508 {
   1509 	struct dq2get_callback *c = v;
   1510 	daddr_t lblkno;
   1511 	int blkoff;
   1512 #ifdef LFS_EI
   1513 	struct lfs *fs = ump->um_lfs;
   1514 	const int needswap = ULFS_MPNEEDSWAP(fs);
   1515 #endif
   1516 
   1517 	if (ulfs_rw32(q2e->q2e_uid, needswap) == c->id) {
   1518 		KASSERT(mutex_owned(&c->dq->dq_interlock));
   1519 		lblkno = (off >> ump->um_mountp->mnt_fs_bshift);
   1520 		blkoff = (off & ump->umq2_bmask);
   1521 		c->dq->dq2_lblkno = lblkno;
   1522 		c->dq->dq2_blkoff = blkoff;
   1523 		return Q2WL_ABORT;
   1524 	}
   1525 	return 0;
   1526 }
   1527 
   1528 int
   1529 lfs_dq2get(struct vnode *dqvp, u_long id, struct ulfsmount *ump, int type,
   1530     struct dquot *dq)
   1531 {
   1532 	struct buf *bp;
   1533 	struct quota2_header *q2h;
   1534 	int error;
   1535 	daddr_t offset;
   1536 	u_long hash_mask;
   1537 	struct dq2get_callback c = {
   1538 		.id = id,
   1539 		.dq = dq
   1540 	};
   1541 
   1542 	KASSERT(mutex_owned(&dq->dq_interlock));
   1543 	mutex_enter(&lfs_dqlock);
   1544 	error = getq2h(ump, type, &bp, &q2h, 0);
   1545 	if (error)
   1546 		goto out_mutex;
   1547 	/* look for our entry */
   1548 	hash_mask = ((1 << q2h->q2h_hash_shift) - 1);
   1549 	offset = q2h->q2h_entries[id & hash_mask];
   1550 	error = quota2_walk_list(ump, bp, type, &offset, 0, (void *)&c,
   1551 	    dq2get_callback);
   1552 	brelse(bp, 0);
   1553 out_mutex:
   1554 	mutex_exit(&lfs_dqlock);
   1555 	return error;
   1556 }
   1557 
   1558 int
   1559 lfs_dq2sync(struct vnode *vp, struct dquot *dq)
   1560 {
   1561 	return 0;
   1562 }
   1563