Home | History | Annotate | Line # | Download | only in libquota
quota_oldfiles.c revision 1.1
      1  1.1  dholland /*	$NetBSD: quota_oldfiles.c,v 1.1 2012/01/09 15:41:58 dholland Exp $	*/
      2  1.1  dholland 
      3  1.1  dholland /*
      4  1.1  dholland  * Copyright (c) 1980, 1990, 1993
      5  1.1  dholland  *	The Regents of the University of California.  All rights reserved.
      6  1.1  dholland  *
      7  1.1  dholland  * This code is derived from software contributed to Berkeley by
      8  1.1  dholland  * Robert Elz at The University of Melbourne.
      9  1.1  dholland  *
     10  1.1  dholland  * Redistribution and use in source and binary forms, with or without
     11  1.1  dholland  * modification, are permitted provided that the following conditions
     12  1.1  dholland  * are met:
     13  1.1  dholland  * 1. Redistributions of source code must retain the above copyright
     14  1.1  dholland  *    notice, this list of conditions and the following disclaimer.
     15  1.1  dholland  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  dholland  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  dholland  *    documentation and/or other materials provided with the distribution.
     18  1.1  dholland  * 3. Neither the name of the University nor the names of its contributors
     19  1.1  dholland  *    may be used to endorse or promote products derived from this software
     20  1.1  dholland  *    without specific prior written permission.
     21  1.1  dholland  *
     22  1.1  dholland  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  1.1  dholland  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  1.1  dholland  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  1.1  dholland  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  1.1  dholland  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  1.1  dholland  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  1.1  dholland  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  1.1  dholland  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  1.1  dholland  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  1.1  dholland  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  1.1  dholland  * SUCH DAMAGE.
     33  1.1  dholland  */
     34  1.1  dholland 
     35  1.1  dholland #include <sys/types.h>
     36  1.1  dholland #include <sys/stat.h>
     37  1.1  dholland #include <stdio.h>
     38  1.1  dholland #include <stdlib.h>
     39  1.1  dholland #include <string.h>
     40  1.1  dholland #include <unistd.h>
     41  1.1  dholland #include <fcntl.h>
     42  1.1  dholland #include <limits.h>
     43  1.1  dholland #include <fstab.h>
     44  1.1  dholland #include <errno.h>
     45  1.1  dholland #include <err.h>
     46  1.1  dholland 
     47  1.1  dholland #include <ufs/ufs/quota1.h>
     48  1.1  dholland 
     49  1.1  dholland #include <quota.h>
     50  1.1  dholland #include "quotapvt.h"
     51  1.1  dholland 
     52  1.1  dholland struct oldfiles_quotacursor {
     53  1.1  dholland 	unsigned oqc_doingusers;
     54  1.1  dholland 	unsigned oqc_doinggroups;
     55  1.1  dholland 
     56  1.1  dholland 	unsigned oqc_numusers;
     57  1.1  dholland 	unsigned oqc_numgroups;
     58  1.1  dholland 
     59  1.1  dholland 	unsigned oqc_didusers;
     60  1.1  dholland 	unsigned oqc_didgroups;
     61  1.1  dholland 	unsigned oqc_diddefault;
     62  1.1  dholland 	unsigned oqc_pos;
     63  1.1  dholland 	unsigned oqc_didblocks;
     64  1.1  dholland };
     65  1.1  dholland 
     66  1.1  dholland static uint64_t
     67  1.1  dholland dqblk_limit(uint32_t val)
     68  1.1  dholland {
     69  1.1  dholland 	if (val == 0) {
     70  1.1  dholland 		return QUOTA_NOLIMIT;
     71  1.1  dholland 	} else {
     72  1.1  dholland 		return val - 1;
     73  1.1  dholland 	}
     74  1.1  dholland }
     75  1.1  dholland 
     76  1.1  dholland static void
     77  1.1  dholland dqblk_getblocks(const struct dqblk *dq, struct quotaval *qv)
     78  1.1  dholland {
     79  1.1  dholland 	qv->qv_hardlimit = dqblk_limit(dq->dqb_bhardlimit);
     80  1.1  dholland 	qv->qv_softlimit = dqblk_limit(dq->dqb_bsoftlimit);
     81  1.1  dholland 	qv->qv_usage = dq->dqb_curblocks;
     82  1.1  dholland 	qv->qv_expiretime = dq->dqb_btime;
     83  1.1  dholland 	qv->qv_grace = QUOTA_NOTIME;
     84  1.1  dholland }
     85  1.1  dholland 
     86  1.1  dholland static void
     87  1.1  dholland dqblk_getfiles(const struct dqblk *dq, struct quotaval *qv)
     88  1.1  dholland {
     89  1.1  dholland 	qv->qv_hardlimit = dqblk_limit(dq->dqb_ihardlimit);
     90  1.1  dholland 	qv->qv_softlimit = dqblk_limit(dq->dqb_isoftlimit);
     91  1.1  dholland 	qv->qv_usage = dq->dqb_curinodes;
     92  1.1  dholland 	qv->qv_expiretime = dq->dqb_itime;
     93  1.1  dholland 	qv->qv_grace = QUOTA_NOTIME;
     94  1.1  dholland }
     95  1.1  dholland 
     96  1.1  dholland static int
     97  1.1  dholland __quota_oldfiles_open(struct quotahandle *qh, const char *path, int *fd_ret)
     98  1.1  dholland {
     99  1.1  dholland 	int fd;
    100  1.1  dholland 
    101  1.1  dholland 	fd = open(path, O_RDWR);
    102  1.1  dholland 	if (fd < 0 && (errno == EACCES || errno == EROFS)) {
    103  1.1  dholland 		fd = open(path, O_RDONLY);
    104  1.1  dholland 		if (fd < 0) {
    105  1.1  dholland 			return -1;
    106  1.1  dholland 		}
    107  1.1  dholland 	}
    108  1.1  dholland 	*fd_ret = fd;
    109  1.1  dholland 	return 0;
    110  1.1  dholland }
    111  1.1  dholland 
    112  1.1  dholland int
    113  1.1  dholland __quota_oldfiles_initialize(struct quotahandle *qh)
    114  1.1  dholland {
    115  1.1  dholland 	static const char *const names[] = INITQFNAMES;
    116  1.1  dholland 
    117  1.1  dholland 	struct fstab *fs;
    118  1.1  dholland 	char buf[sizeof(fs->fs_mntops)];
    119  1.1  dholland 	char *opt, *state, *s;
    120  1.1  dholland 	char path[PATH_MAX];
    121  1.1  dholland 	const char *userquotafile, *groupquotafile;
    122  1.1  dholland 	int hasuserquota, hasgroupquota;
    123  1.1  dholland 
    124  1.1  dholland 	if (qh->qh_hasoldfiles) {
    125  1.1  dholland 		/* already initialized */
    126  1.1  dholland 		return 0;
    127  1.1  dholland 	}
    128  1.1  dholland 
    129  1.1  dholland 	/*
    130  1.1  dholland 	 * Find the fstab entry.
    131  1.1  dholland 	 *
    132  1.1  dholland 	 * XXX: should be able to handle not just ffs quota1 files but
    133  1.1  dholland 	 * also lfs and even ext2fs.
    134  1.1  dholland 	 */
    135  1.1  dholland 	setfsent();
    136  1.1  dholland 	while ((fs = getfsent()) != NULL) {
    137  1.1  dholland 		if (!strcmp(fs->fs_vfstype, "ffs") &&
    138  1.1  dholland 		    !strcmp(fs->fs_file, qh->qh_mountpoint)) {
    139  1.1  dholland 			break;
    140  1.1  dholland 		}
    141  1.1  dholland 	}
    142  1.1  dholland 	endfsent();
    143  1.1  dholland 
    144  1.1  dholland 	if (fs == NULL) {
    145  1.1  dholland 		warnx("%s not found in fstab", qh->qh_mountpoint);
    146  1.1  dholland 		errno = ENXIO;
    147  1.1  dholland 		return -1;
    148  1.1  dholland 	}
    149  1.1  dholland 
    150  1.1  dholland 	/*
    151  1.1  dholland 	 * Inspect the mount options to find the quota files.
    152  1.1  dholland 	 * XXX this info should be gotten from the kernel.
    153  1.1  dholland 	 *
    154  1.1  dholland 	 * The options are:
    155  1.1  dholland 	 *    userquota[=path]          enable user quotas
    156  1.1  dholland 	 *    groupquota[=path]         enable group quotas
    157  1.1  dholland 	 */
    158  1.1  dholland 	hasuserquota = hasgroupquota = 0;
    159  1.1  dholland 	userquotafile = groupquotafile = NULL;
    160  1.1  dholland 	strlcpy(buf, fs->fs_mntops, sizeof(buf));
    161  1.1  dholland 	for (opt = strtok_r(buf, ",", &state);
    162  1.1  dholland 	     opt != NULL;
    163  1.1  dholland 	     opt = strtok_r(NULL, ",", &state)) {
    164  1.1  dholland 		s = strchr(opt, '=');
    165  1.1  dholland 		if (s != NULL) {
    166  1.1  dholland 			*(s++) = '\0';
    167  1.1  dholland 		}
    168  1.1  dholland 		if (!strcmp(opt, "userquota")) {
    169  1.1  dholland 			hasuserquota = 1;
    170  1.1  dholland 			if (s != NULL) {
    171  1.1  dholland 				userquotafile = s;
    172  1.1  dholland 			}
    173  1.1  dholland 		} else if (!strcmp(opt, "groupquota")) {
    174  1.1  dholland 			hasgroupquota = 1;
    175  1.1  dholland 			if (s != NULL) {
    176  1.1  dholland 				groupquotafile = s;
    177  1.1  dholland 			}
    178  1.1  dholland 		}
    179  1.1  dholland 	}
    180  1.1  dholland 
    181  1.1  dholland 	if (!hasuserquota && !hasgroupquota) {
    182  1.1  dholland 		errno = ENXIO;
    183  1.1  dholland 		return -1;
    184  1.1  dholland 	}
    185  1.1  dholland 
    186  1.1  dholland 	if (hasuserquota) {
    187  1.1  dholland 		if (userquotafile == NULL) {
    188  1.1  dholland 			(void)snprintf(path, sizeof(path), "%s/%s.%s",
    189  1.1  dholland 				       fs->fs_file,
    190  1.1  dholland 				       QUOTAFILENAME, names[USRQUOTA]);
    191  1.1  dholland 			userquotafile = path;
    192  1.1  dholland 		}
    193  1.1  dholland 		if (__quota_oldfiles_open(qh, userquotafile,
    194  1.1  dholland 					  &qh->qh_userfile)) {
    195  1.1  dholland 			return -1;
    196  1.1  dholland 		}
    197  1.1  dholland 	}
    198  1.1  dholland 	if (hasgroupquota) {
    199  1.1  dholland 		if (groupquotafile == NULL) {
    200  1.1  dholland 			(void)snprintf(path, sizeof(path), "%s/%s.%s",
    201  1.1  dholland 				       fs->fs_file,
    202  1.1  dholland 				       QUOTAFILENAME, names[GRPQUOTA]);
    203  1.1  dholland 			groupquotafile = path;
    204  1.1  dholland 		}
    205  1.1  dholland 		if (__quota_oldfiles_open(qh, groupquotafile,
    206  1.1  dholland 					  &qh->qh_groupfile)) {
    207  1.1  dholland 			return -1;
    208  1.1  dholland 		}
    209  1.1  dholland 	}
    210  1.1  dholland 
    211  1.1  dholland 	qh->qh_hasoldfiles = 1;
    212  1.1  dholland 
    213  1.1  dholland 	return 0;
    214  1.1  dholland }
    215  1.1  dholland 
    216  1.1  dholland const char *
    217  1.1  dholland __quota_oldfiles_getimplname(struct quotahandle *qh)
    218  1.1  dholland {
    219  1.1  dholland 	return "ffs quota1 direct file access";
    220  1.1  dholland }
    221  1.1  dholland 
    222  1.1  dholland static int
    223  1.1  dholland __quota_oldfiles_doget(struct quotahandle *qh, const struct quotakey *qk,
    224  1.1  dholland 		       struct quotaval *qv, int *isallzero)
    225  1.1  dholland {
    226  1.1  dholland 	int file;
    227  1.1  dholland 	off_t pos;
    228  1.1  dholland 	struct dqblk dq;
    229  1.1  dholland 	ssize_t result;
    230  1.1  dholland 
    231  1.1  dholland 	switch (qk->qk_idtype) {
    232  1.1  dholland 	    case QUOTA_IDTYPE_USER:
    233  1.1  dholland 		file = qh->qh_userfile;
    234  1.1  dholland 		break;
    235  1.1  dholland 	    case QUOTA_IDTYPE_GROUP:
    236  1.1  dholland 		file = qh->qh_groupfile;
    237  1.1  dholland 		break;
    238  1.1  dholland 	    default:
    239  1.1  dholland 		errno = EINVAL;
    240  1.1  dholland 		return -1;
    241  1.1  dholland 	}
    242  1.1  dholland 
    243  1.1  dholland 	if (qk->qk_id == QUOTA_DEFAULTID) {
    244  1.1  dholland 		pos = 0;
    245  1.1  dholland 	} else {
    246  1.1  dholland 		pos = qk->qk_id * sizeof(struct dqblk);
    247  1.1  dholland 	}
    248  1.1  dholland 
    249  1.1  dholland 	result = pread(file, &dq, sizeof(dq), pos);
    250  1.1  dholland 	if (result < 0) {
    251  1.1  dholland 		return -1;
    252  1.1  dholland 	}
    253  1.1  dholland 	if ((size_t)result != sizeof(dq)) {
    254  1.1  dholland 		errno = EFTYPE;
    255  1.1  dholland 		return -1;
    256  1.1  dholland 	}
    257  1.1  dholland 
    258  1.1  dholland 	switch (qk->qk_objtype) {
    259  1.1  dholland 	    case QUOTA_OBJTYPE_BLOCKS:
    260  1.1  dholland 		dqblk_getblocks(&dq, qv);
    261  1.1  dholland 		break;
    262  1.1  dholland 	    case QUOTA_OBJTYPE_FILES:
    263  1.1  dholland 		dqblk_getfiles(&dq, qv);
    264  1.1  dholland 		break;
    265  1.1  dholland 	    default:
    266  1.1  dholland 		errno = EINVAL;
    267  1.1  dholland 		return -1;
    268  1.1  dholland 	}
    269  1.1  dholland 
    270  1.1  dholland 	if (qk->qk_id == QUOTA_DEFAULTID) {
    271  1.1  dholland 		qv->qv_usage = 0;
    272  1.1  dholland 		qv->qv_grace = qv->qv_expiretime;
    273  1.1  dholland 		qv->qv_expiretime = QUOTA_NOTIME;
    274  1.1  dholland 	} else if (qk->qk_id == 0) {
    275  1.1  dholland 		qv->qv_hardlimit = 0;
    276  1.1  dholland 		qv->qv_softlimit = 0;
    277  1.1  dholland 		qv->qv_expiretime = QUOTA_NOTIME;
    278  1.1  dholland 		qv->qv_grace = QUOTA_NOTIME;
    279  1.1  dholland 	}
    280  1.1  dholland 
    281  1.1  dholland 	if (isallzero != NULL) {
    282  1.1  dholland 		if (dq.dqb_bhardlimit == 0 &&
    283  1.1  dholland 		    dq.dqb_bsoftlimit == 0 &&
    284  1.1  dholland 		    dq.dqb_curblocks == 0 &&
    285  1.1  dholland 		    dq.dqb_ihardlimit == 0 &&
    286  1.1  dholland 		    dq.dqb_isoftlimit == 0 &&
    287  1.1  dholland 		    dq.dqb_curinodes == 0 &&
    288  1.1  dholland 		    dq.dqb_btime == 0 &&
    289  1.1  dholland 		    dq.dqb_itime == 0) {
    290  1.1  dholland 			*isallzero = 1;
    291  1.1  dholland 		} else {
    292  1.1  dholland 			*isallzero = 0;
    293  1.1  dholland 		}
    294  1.1  dholland 	}
    295  1.1  dholland 
    296  1.1  dholland 	return 0;
    297  1.1  dholland }
    298  1.1  dholland 
    299  1.1  dholland int
    300  1.1  dholland __quota_oldfiles_get(struct quotahandle *qh, const struct quotakey *qk,
    301  1.1  dholland 		     struct quotaval *qv)
    302  1.1  dholland {
    303  1.1  dholland 	return __quota_oldfiles_doget(qh, qk, qv, NULL);
    304  1.1  dholland }
    305  1.1  dholland 
    306  1.1  dholland struct oldfiles_quotacursor *
    307  1.1  dholland __quota_oldfiles_cursor_create(struct quotahandle *qh)
    308  1.1  dholland {
    309  1.1  dholland 	struct oldfiles_quotacursor *oqc;
    310  1.1  dholland 	struct stat st;
    311  1.1  dholland 	int serrno;
    312  1.1  dholland 
    313  1.1  dholland 	oqc = malloc(sizeof(*oqc));
    314  1.1  dholland 	if (oqc == NULL) {
    315  1.1  dholland 		return NULL;
    316  1.1  dholland 	}
    317  1.1  dholland 
    318  1.1  dholland 	oqc->oqc_didusers = 0;
    319  1.1  dholland 	oqc->oqc_didgroups = 0;
    320  1.1  dholland 	oqc->oqc_diddefault = 0;
    321  1.1  dholland 	oqc->oqc_pos = 0;
    322  1.1  dholland 	oqc->oqc_didblocks = 0;
    323  1.1  dholland 
    324  1.1  dholland 	if (qh->qh_userfile >= 0) {
    325  1.1  dholland 		oqc->oqc_doingusers = 1;
    326  1.1  dholland 	} else {
    327  1.1  dholland 		oqc->oqc_doingusers = 0;
    328  1.1  dholland 		oqc->oqc_didusers = 1;
    329  1.1  dholland 	}
    330  1.1  dholland 
    331  1.1  dholland 	if (qh->qh_groupfile >= 0) {
    332  1.1  dholland 		oqc->oqc_doinggroups = 1;
    333  1.1  dholland 	} else {
    334  1.1  dholland 		oqc->oqc_doinggroups = 0;
    335  1.1  dholland 		oqc->oqc_didgroups = 1;
    336  1.1  dholland 	}
    337  1.1  dholland 
    338  1.1  dholland 	if (fstat(qh->qh_userfile, &st) < 0) {
    339  1.1  dholland 		serrno = errno;
    340  1.1  dholland 		free(oqc);
    341  1.1  dholland 		errno = serrno;
    342  1.1  dholland 		return NULL;
    343  1.1  dholland 	}
    344  1.1  dholland 	oqc->oqc_numusers = st.st_size / sizeof(struct dqblk);
    345  1.1  dholland 
    346  1.1  dholland 	if (fstat(qh->qh_groupfile, &st) < 0) {
    347  1.1  dholland 		serrno = errno;
    348  1.1  dholland 		free(oqc);
    349  1.1  dholland 		errno = serrno;
    350  1.1  dholland 		return NULL;
    351  1.1  dholland 	}
    352  1.1  dholland 	oqc->oqc_numgroups = st.st_size / sizeof(struct dqblk);
    353  1.1  dholland 
    354  1.1  dholland 	return oqc;
    355  1.1  dholland }
    356  1.1  dholland 
    357  1.1  dholland void
    358  1.1  dholland __quota_oldfiles_cursor_destroy(struct oldfiles_quotacursor *oqc)
    359  1.1  dholland {
    360  1.1  dholland 	free(oqc);
    361  1.1  dholland }
    362  1.1  dholland 
    363  1.1  dholland int
    364  1.1  dholland __quota_oldfiles_cursor_skipidtype(struct oldfiles_quotacursor *oqc,
    365  1.1  dholland 				   unsigned idtype)
    366  1.1  dholland {
    367  1.1  dholland 	switch (idtype) {
    368  1.1  dholland 	    case QUOTA_IDTYPE_USER:
    369  1.1  dholland 		oqc->oqc_doingusers = 0;
    370  1.1  dholland 		oqc->oqc_didusers = 1;
    371  1.1  dholland 		break;
    372  1.1  dholland 	    case QUOTA_IDTYPE_GROUP:
    373  1.1  dholland 		oqc->oqc_doinggroups = 0;
    374  1.1  dholland 		oqc->oqc_didgroups = 1;
    375  1.1  dholland 		break;
    376  1.1  dholland 	    default:
    377  1.1  dholland 		errno = EINVAL;
    378  1.1  dholland 		return -1;
    379  1.1  dholland 	}
    380  1.1  dholland 	return 0;
    381  1.1  dholland }
    382  1.1  dholland 
    383  1.1  dholland int
    384  1.1  dholland __quota_oldfiles_cursor_get(struct quotahandle *qh,
    385  1.1  dholland 			    struct oldfiles_quotacursor *oqc,
    386  1.1  dholland 			    struct quotakey *key, struct quotaval *val)
    387  1.1  dholland {
    388  1.1  dholland 	unsigned maxpos;
    389  1.1  dholland 	int isallzero;
    390  1.1  dholland 
    391  1.1  dholland 	/* in case one of the sizes is zero */
    392  1.1  dholland 	if (!oqc->oqc_didusers && oqc->oqc_pos >= oqc->oqc_numusers) {
    393  1.1  dholland 		oqc->oqc_didusers = 1;
    394  1.1  dholland 	}
    395  1.1  dholland 	if (!oqc->oqc_didgroups && oqc->oqc_pos >= oqc->oqc_numgroups) {
    396  1.1  dholland 		oqc->oqc_didgroups = 1;
    397  1.1  dholland 	}
    398  1.1  dholland 
    399  1.1  dholland  again:
    400  1.1  dholland 	/*
    401  1.1  dholland 	 * Figure out what to get
    402  1.1  dholland 	 */
    403  1.1  dholland 
    404  1.1  dholland 	if (!oqc->oqc_didusers) {
    405  1.1  dholland 		key->qk_idtype = QUOTA_IDTYPE_USER;
    406  1.1  dholland 		maxpos = oqc->oqc_numusers;
    407  1.1  dholland 	} else if (!oqc->oqc_didgroups) {
    408  1.1  dholland 		key->qk_idtype = QUOTA_IDTYPE_GROUP;
    409  1.1  dholland 		maxpos = oqc->oqc_numgroups;
    410  1.1  dholland 	} else {
    411  1.1  dholland 		errno = ENOENT;
    412  1.1  dholland 		return -1;
    413  1.1  dholland 	}
    414  1.1  dholland 
    415  1.1  dholland 	if (!oqc->oqc_diddefault) {
    416  1.1  dholland 		key->qk_id = QUOTA_DEFAULTID;
    417  1.1  dholland 	} else {
    418  1.1  dholland 		key->qk_id = oqc->oqc_pos;
    419  1.1  dholland 	}
    420  1.1  dholland 
    421  1.1  dholland 	if (!oqc->oqc_didblocks) {
    422  1.1  dholland 		key->qk_objtype = QUOTA_OBJTYPE_BLOCKS;
    423  1.1  dholland 	} else {
    424  1.1  dholland 		key->qk_objtype = QUOTA_OBJTYPE_FILES;
    425  1.1  dholland 	}
    426  1.1  dholland 
    427  1.1  dholland 	/*
    428  1.1  dholland 	 * Get it
    429  1.1  dholland 	 */
    430  1.1  dholland 
    431  1.1  dholland 	if (__quota_oldfiles_doget(qh, key, val, &isallzero)) {
    432  1.1  dholland 		return -1;
    433  1.1  dholland 	}
    434  1.1  dholland 
    435  1.1  dholland 	/*
    436  1.1  dholland 	 * Advance the cursor
    437  1.1  dholland 	 */
    438  1.1  dholland 	if (!oqc->oqc_didblocks) {
    439  1.1  dholland 		oqc->oqc_didblocks = 1;
    440  1.1  dholland 	} else {
    441  1.1  dholland 		oqc->oqc_didblocks = 0;
    442  1.1  dholland 		if (!oqc->oqc_diddefault) {
    443  1.1  dholland 			oqc->oqc_diddefault = 1;
    444  1.1  dholland 		} else {
    445  1.1  dholland 			oqc->oqc_pos++;
    446  1.1  dholland 			if (oqc->oqc_pos >= maxpos) {
    447  1.1  dholland 				oqc->oqc_pos = 0;
    448  1.1  dholland 				oqc->oqc_diddefault = 0;
    449  1.1  dholland 				if (!oqc->oqc_didusers) {
    450  1.1  dholland 					oqc->oqc_didusers = 1;
    451  1.1  dholland 				} else {
    452  1.1  dholland 					oqc->oqc_didgroups = 1;
    453  1.1  dholland 				}
    454  1.1  dholland 			}
    455  1.1  dholland 		}
    456  1.1  dholland 	}
    457  1.1  dholland 
    458  1.1  dholland 	/*
    459  1.1  dholland 	 * If we got an all-zero dqblk (e.g. from the middle of a hole
    460  1.1  dholland 	 * in the quota file) don't bother returning it to the caller.
    461  1.1  dholland 	 *
    462  1.1  dholland 	 * ...unless we're at the end of the data, to avoid going past
    463  1.1  dholland 	 * the end and generating a spurious failure. There's no
    464  1.1  dholland 	 * reasonable way to make _atend detect empty entries at the
    465  1.1  dholland 	 * end of the quota files.
    466  1.1  dholland 	 */
    467  1.1  dholland 	if (isallzero && (!oqc->oqc_didusers || !oqc->oqc_didgroups)) {
    468  1.1  dholland 		goto again;
    469  1.1  dholland 	}
    470  1.1  dholland 	return 0;
    471  1.1  dholland }
    472  1.1  dholland 
    473  1.1  dholland int
    474  1.1  dholland __quota_oldfiles_cursor_getn(struct quotahandle *qh,
    475  1.1  dholland 			     struct oldfiles_quotacursor *oqc,
    476  1.1  dholland 			     struct quotakey *keys, struct quotaval *vals,
    477  1.1  dholland 			     unsigned maxnum)
    478  1.1  dholland {
    479  1.1  dholland 	unsigned i;
    480  1.1  dholland 
    481  1.1  dholland 	if (maxnum > INT_MAX) {
    482  1.1  dholland 		/* joker, eh? */
    483  1.1  dholland 		errno = EINVAL;
    484  1.1  dholland 		return -1;
    485  1.1  dholland 	}
    486  1.1  dholland 
    487  1.1  dholland 	for (i=0; i<maxnum; i++) {
    488  1.1  dholland 		if (__quota_oldfiles_cursor_atend(oqc)) {
    489  1.1  dholland 			break;
    490  1.1  dholland 		}
    491  1.1  dholland 		if (__quota_oldfiles_cursor_get(qh, oqc, &keys[i], &vals[i])) {
    492  1.1  dholland 			if (i > 0) {
    493  1.1  dholland 				/*
    494  1.1  dholland 				 * Succeed witih what we have so far;
    495  1.1  dholland 				 * the next attempt will hit the same
    496  1.1  dholland 				 * error again.
    497  1.1  dholland 				 */
    498  1.1  dholland 				break;
    499  1.1  dholland 			}
    500  1.1  dholland 			return -1;
    501  1.1  dholland 		}
    502  1.1  dholland 	}
    503  1.1  dholland 	return i;
    504  1.1  dholland 
    505  1.1  dholland }
    506  1.1  dholland 
    507  1.1  dholland int
    508  1.1  dholland __quota_oldfiles_cursor_atend(struct oldfiles_quotacursor *oqc)
    509  1.1  dholland {
    510  1.1  dholland 	/* in case one of the sizes is zero */
    511  1.1  dholland 	if (!oqc->oqc_didusers && oqc->oqc_pos >= oqc->oqc_numusers) {
    512  1.1  dholland 		oqc->oqc_didusers = 1;
    513  1.1  dholland 	}
    514  1.1  dholland 	if (!oqc->oqc_didgroups && oqc->oqc_pos >= oqc->oqc_numgroups) {
    515  1.1  dholland 		oqc->oqc_didgroups = 1;
    516  1.1  dholland 	}
    517  1.1  dholland 
    518  1.1  dholland 	return oqc->oqc_didusers && oqc->oqc_didgroups;
    519  1.1  dholland }
    520  1.1  dholland 
    521  1.1  dholland int
    522  1.1  dholland __quota_oldfiles_cursor_rewind(struct oldfiles_quotacursor *oqc)
    523  1.1  dholland {
    524  1.1  dholland 	oqc->oqc_didusers = 0;
    525  1.1  dholland 	oqc->oqc_didgroups = 0;
    526  1.1  dholland 	oqc->oqc_diddefault = 0;
    527  1.1  dholland 	oqc->oqc_pos = 0;
    528  1.1  dholland 	oqc->oqc_didblocks = 0;
    529  1.1  dholland 	return 0;
    530  1.1  dholland }
    531