Home | History | Annotate | Line # | Download | only in kern
vfs_quotactl.c revision 1.22
      1  1.22  dholland /*	$NetBSD: vfs_quotactl.c,v 1.22 2012/01/29 07:00:39 dholland Exp $	*/
      2   1.3  dholland 
      3   1.3  dholland /*
      4   1.3  dholland  * Copyright (c) 1991, 1993, 1994
      5   1.3  dholland  *	The Regents of the University of California.  All rights reserved.
      6   1.3  dholland  * (c) UNIX System Laboratories, Inc.
      7   1.3  dholland  * All or some portions of this file are derived from material licensed
      8   1.3  dholland  * to the University of California by American Telephone and Telegraph
      9   1.3  dholland  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10   1.3  dholland  * the permission of UNIX System Laboratories, Inc.
     11   1.3  dholland  *
     12   1.3  dholland  * Redistribution and use in source and binary forms, with or without
     13   1.3  dholland  * modification, are permitted provided that the following conditions
     14   1.3  dholland  * are met:
     15   1.3  dholland  * 1. Redistributions of source code must retain the above copyright
     16   1.3  dholland  *    notice, this list of conditions and the following disclaimer.
     17   1.3  dholland  * 2. Redistributions in binary form must reproduce the above copyright
     18   1.3  dholland  *    notice, this list of conditions and the following disclaimer in the
     19   1.3  dholland  *    documentation and/or other materials provided with the distribution.
     20   1.3  dholland  * 3. Neither the name of the University nor the names of its contributors
     21   1.3  dholland  *    may be used to endorse or promote products derived from this software
     22   1.3  dholland  *    without specific prior written permission.
     23   1.3  dholland  *
     24   1.3  dholland  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25   1.3  dholland  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26   1.3  dholland  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27   1.3  dholland  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28   1.3  dholland  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29   1.3  dholland  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30   1.3  dholland  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31   1.3  dholland  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32   1.3  dholland  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33   1.3  dholland  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34   1.3  dholland  * SUCH DAMAGE.
     35   1.3  dholland  *
     36   1.3  dholland  *	@(#)ufs_vfsops.c	8.8 (Berkeley) 5/20/95
     37   1.3  dholland  *	From NetBSD: ufs_vfsops.c,v 1.42 2011/03/24 17:05:46 bouyer Exp
     38   1.3  dholland  */
     39   1.1  dholland 
     40   1.1  dholland /*
     41   1.1  dholland  * Copyright (c) 1982, 1986, 1990, 1993, 1995
     42   1.1  dholland  *	The Regents of the University of California.  All rights reserved.
     43   1.1  dholland  *
     44   1.1  dholland  * This code is derived from software contributed to Berkeley by
     45   1.1  dholland  * Robert Elz at The University of Melbourne.
     46   1.1  dholland  *
     47   1.1  dholland  * Redistribution and use in source and binary forms, with or without
     48   1.1  dholland  * modification, are permitted provided that the following conditions
     49   1.1  dholland  * are met:
     50   1.1  dholland  * 1. Redistributions of source code must retain the above copyright
     51   1.1  dholland  *    notice, this list of conditions and the following disclaimer.
     52   1.1  dholland  * 2. Redistributions in binary form must reproduce the above copyright
     53   1.1  dholland  *    notice, this list of conditions and the following disclaimer in the
     54   1.1  dholland  *    documentation and/or other materials provided with the distribution.
     55   1.1  dholland  * 3. Neither the name of the University nor the names of its contributors
     56   1.1  dholland  *    may be used to endorse or promote products derived from this software
     57   1.1  dholland  *    without specific prior written permission.
     58   1.1  dholland  *
     59   1.1  dholland  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     60   1.1  dholland  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     61   1.1  dholland  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     62   1.1  dholland  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     63   1.1  dholland  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     64   1.1  dholland  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     65   1.1  dholland  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66   1.1  dholland  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67   1.1  dholland  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68   1.1  dholland  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69   1.1  dholland  * SUCH DAMAGE.
     70   1.1  dholland  *
     71   1.1  dholland  *	@(#)ufs_quota.c	8.5 (Berkeley) 5/20/95
     72   1.3  dholland  *	From NetBSD: ufs_quota.c,v 1.70 2011/03/24 17:05:46 bouyer Exp
     73   1.3  dholland  */
     74   1.3  dholland 
     75   1.3  dholland /*
     76   1.3  dholland  * Note that both of the copyrights above are moderately spurious;
     77   1.3  dholland  * this code should almost certainly have the Copyright 2010 Manuel
     78   1.3  dholland  * Bouyer notice and license found in e.g. sys/ufs/ufs/quota2_subr.c.
     79   1.3  dholland  * However, they're what was on the files this code was sliced out of.
     80   1.1  dholland  */
     81   1.1  dholland 
     82   1.1  dholland #include <sys/cdefs.h>
     83  1.22  dholland __KERNEL_RCSID(0, "$NetBSD: vfs_quotactl.c,v 1.22 2012/01/29 07:00:39 dholland Exp $");
     84   1.1  dholland 
     85   1.2  dholland #include <sys/mount.h>
     86   1.8  dholland #include <sys/quota.h>
     87   1.4  dholland #include <sys/quotactl.h>
     88   1.3  dholland #include <quota/quotaprop.h>
     89   1.2  dholland 
     90   1.4  dholland static int
     91   1.5  dholland vfs_quotactl_getversion(struct mount *mp,
     92   1.5  dholland 			prop_dictionary_t cmddict, int q2type,
     93   1.5  dholland 			prop_array_t datas)
     94   1.5  dholland {
     95   1.6  dholland 	prop_array_t replies;
     96   1.6  dholland 	prop_dictionary_t data;
     97   1.6  dholland 	int q2version;
     98   1.5  dholland 	struct vfs_quotactl_args args;
     99   1.6  dholland 	int error;
    100   1.6  dholland 
    101   1.6  dholland 	KASSERT(prop_object_type(cmddict) == PROP_TYPE_DICTIONARY);
    102   1.6  dholland 	KASSERT(prop_object_type(datas) == PROP_TYPE_ARRAY);
    103   1.6  dholland 
    104   1.6  dholland 	args.qc_type = QCT_GETVERSION;
    105   1.6  dholland 	args.u.getversion.qc_version_ret = &q2version;
    106   1.6  dholland 	error = VFS_QUOTACTL(mp, QUOTACTL_GETVERSION, &args);
    107   1.6  dholland 	if (error) {
    108   1.6  dholland 		return error;
    109   1.6  dholland 	}
    110   1.6  dholland 
    111   1.6  dholland 	data = prop_dictionary_create();
    112   1.6  dholland 	if (data == NULL) {
    113   1.6  dholland 		return ENOMEM;
    114   1.6  dholland 	}
    115   1.6  dholland 
    116   1.6  dholland 	if (!prop_dictionary_set_int8(data, "version", q2version)) {
    117   1.6  dholland 		prop_object_release(data);
    118   1.6  dholland 		return ENOMEM;
    119   1.6  dholland 	}
    120   1.6  dholland 
    121   1.6  dholland 	replies = prop_array_create();
    122   1.6  dholland 	if (replies == NULL) {
    123   1.6  dholland 		prop_object_release(data);
    124   1.6  dholland 		return ENOMEM;
    125   1.6  dholland 	}
    126   1.5  dholland 
    127   1.6  dholland 	if (!prop_array_add_and_rel(replies, data)) {
    128   1.6  dholland 		prop_object_release(data);
    129   1.6  dholland 		prop_object_release(replies);
    130   1.6  dholland 		return ENOMEM;
    131   1.6  dholland 	}
    132   1.6  dholland 
    133   1.6  dholland 	if (!prop_dictionary_set_and_rel(cmddict, "data", replies)) {
    134   1.6  dholland 		prop_object_release(replies);
    135   1.6  dholland 		return ENOMEM;
    136   1.6  dholland 	}
    137   1.6  dholland 
    138   1.6  dholland 	return error;
    139   1.5  dholland }
    140   1.5  dholland 
    141   1.5  dholland static int
    142   1.5  dholland vfs_quotactl_quotaon(struct mount *mp,
    143   1.5  dholland 		     prop_dictionary_t cmddict, int q2type,
    144   1.5  dholland 		     prop_array_t datas)
    145   1.5  dholland {
    146   1.5  dholland 	struct vfs_quotactl_args args;
    147   1.5  dholland 
    148   1.5  dholland 	args.qc_type = QCT_PROPLIB;
    149   1.5  dholland 	args.u.proplib.qc_cmddict = cmddict;
    150   1.5  dholland 	args.u.proplib.qc_q2type = q2type;
    151   1.5  dholland 	args.u.proplib.qc_datas = datas;
    152   1.5  dholland 	return VFS_QUOTACTL(mp, QUOTACTL_QUOTAON, &args);
    153   1.5  dholland }
    154   1.5  dholland 
    155   1.5  dholland static int
    156   1.5  dholland vfs_quotactl_quotaoff(struct mount *mp,
    157   1.5  dholland 			prop_dictionary_t cmddict, int q2type,
    158   1.5  dholland 			prop_array_t datas)
    159   1.5  dholland {
    160   1.5  dholland 	struct vfs_quotactl_args args;
    161   1.5  dholland 
    162   1.5  dholland 	args.qc_type = QCT_PROPLIB;
    163   1.5  dholland 	args.u.proplib.qc_cmddict = cmddict;
    164   1.5  dholland 	args.u.proplib.qc_q2type = q2type;
    165   1.5  dholland 	args.u.proplib.qc_datas = datas;
    166   1.5  dholland 	return VFS_QUOTACTL(mp, QUOTACTL_QUOTAOFF, &args);
    167   1.5  dholland }
    168   1.5  dholland 
    169   1.5  dholland static int
    170  1.10  dholland vfs_quotactl_get_addreply(const struct quotakey *qk,
    171   1.8  dholland 			  const struct quotaval *blocks,
    172   1.8  dholland 			  const struct quotaval *files,
    173   1.8  dholland 			  prop_array_t replies)
    174   1.8  dholland {
    175   1.8  dholland 	prop_dictionary_t dict;
    176  1.10  dholland 	id_t id;
    177  1.10  dholland 	int defaultq;
    178  1.10  dholland 	uint64_t *valuesp[QUOTA_NLIMITS];
    179   1.8  dholland 
    180   1.8  dholland 	/* XXX illegal casts */
    181   1.8  dholland 	valuesp[QUOTA_LIMIT_BLOCK] = (void *)(intptr_t)&blocks->qv_hardlimit;
    182   1.8  dholland 	valuesp[QUOTA_LIMIT_FILE] =  (void *)(intptr_t)&files->qv_hardlimit;
    183   1.8  dholland 
    184  1.10  dholland 	if (qk->qk_id == QUOTA_DEFAULTID) {
    185  1.10  dholland 		id = 0;
    186  1.10  dholland 		defaultq = 1;
    187  1.10  dholland 	} else {
    188  1.10  dholland 		id = qk->qk_id;
    189  1.10  dholland 		defaultq = 0;
    190  1.10  dholland 	}
    191  1.10  dholland 
    192   1.8  dholland 	dict = quota64toprop(id, defaultq, valuesp,
    193   1.8  dholland 	    ufs_quota_entry_names, UFS_QUOTA_NENTRIES,
    194   1.8  dholland 	    ufs_quota_limit_names, QUOTA_NLIMITS);
    195   1.8  dholland 	if (dict == NULL)
    196   1.8  dholland 		return ENOMEM;
    197   1.8  dholland 	if (!prop_array_add_and_rel(replies, dict)) {
    198   1.8  dholland 		prop_object_release(dict);
    199   1.8  dholland 		return ENOMEM;
    200   1.8  dholland 	}
    201   1.8  dholland 
    202   1.8  dholland 	return 0;
    203   1.8  dholland }
    204   1.8  dholland 
    205   1.8  dholland static int
    206   1.5  dholland vfs_quotactl_get(struct mount *mp,
    207  1.10  dholland 			prop_dictionary_t cmddict, int idtype,
    208   1.5  dholland 			prop_array_t datas)
    209   1.5  dholland {
    210   1.7  dholland 	prop_object_iterator_t iter;
    211   1.7  dholland 	prop_dictionary_t data;
    212   1.8  dholland 	prop_array_t replies;
    213   1.7  dholland 	uint32_t id;
    214   1.7  dholland 	const char *idstr;
    215   1.5  dholland 	struct vfs_quotactl_args args;
    216  1.10  dholland 	struct quotakey qk;
    217   1.8  dholland 	struct quotaval blocks, files;
    218   1.7  dholland 	int error;
    219   1.7  dholland 
    220   1.7  dholland 	KASSERT(prop_object_type(cmddict) == PROP_TYPE_DICTIONARY);
    221   1.7  dholland 	KASSERT(prop_object_type(datas) == PROP_TYPE_ARRAY);
    222   1.7  dholland 
    223   1.7  dholland 	replies = prop_array_create();
    224   1.7  dholland 	if (replies == NULL) {
    225   1.7  dholland 		return ENOMEM;
    226   1.7  dholland 	}
    227   1.7  dholland 
    228   1.7  dholland 	iter = prop_array_iterator(datas);
    229   1.7  dholland 	if (iter == NULL) {
    230   1.7  dholland 		prop_object_release(replies);
    231   1.7  dholland 		return ENOMEM;
    232   1.7  dholland 	}
    233   1.7  dholland 
    234   1.7  dholland 	while ((data = prop_object_iterator_next(iter)) != NULL) {
    235  1.10  dholland 		qk.qk_idtype = idtype;
    236  1.10  dholland 
    237   1.7  dholland 		if (!prop_dictionary_get_uint32(data, "id", &id)) {
    238   1.7  dholland 			if (!prop_dictionary_get_cstring_nocopy(data, "id",
    239   1.7  dholland 			    &idstr))
    240   1.7  dholland 				continue;
    241   1.7  dholland 			if (strcmp(idstr, "default")) {
    242   1.7  dholland 				error = EINVAL;
    243   1.7  dholland 				goto fail;
    244   1.7  dholland 			}
    245  1.10  dholland 			qk.qk_id = QUOTA_DEFAULTID;
    246   1.7  dholland 		} else {
    247  1.10  dholland 			qk.qk_id = id;
    248   1.7  dholland 		}
    249   1.5  dholland 
    250  1.10  dholland 		qk.qk_objtype = QUOTA_OBJTYPE_BLOCKS;
    251  1.10  dholland 
    252   1.7  dholland 		args.qc_type = QCT_GET;
    253  1.10  dholland 		args.u.get.qc_key = &qk;
    254   1.9  dholland 		args.u.get.qc_ret = &blocks;
    255   1.9  dholland 		error = VFS_QUOTACTL(mp, QUOTACTL_GET, &args);
    256   1.9  dholland 		if (error == EPERM) {
    257   1.9  dholland 			/* XXX does this make sense? */
    258   1.9  dholland 			continue;
    259   1.9  dholland 		} else if (error == ENOENT) {
    260   1.9  dholland 			/* XXX does *this* make sense? */
    261   1.9  dholland 			continue;
    262   1.9  dholland 		} else if (error) {
    263   1.9  dholland 			goto fail;
    264   1.9  dholland 		}
    265   1.9  dholland 
    266  1.10  dholland 		qk.qk_objtype = QUOTA_OBJTYPE_FILES;
    267  1.10  dholland 
    268   1.9  dholland 		args.qc_type = QCT_GET;
    269  1.10  dholland 		args.u.get.qc_key = &qk;
    270   1.9  dholland 		args.u.get.qc_ret = &files;
    271   1.7  dholland 		error = VFS_QUOTACTL(mp, QUOTACTL_GET, &args);
    272   1.7  dholland 		if (error == EPERM) {
    273   1.7  dholland 			/* XXX does this make sense? */
    274   1.7  dholland 			continue;
    275   1.7  dholland 		} else if (error == ENOENT) {
    276   1.7  dholland 			/* XXX does *this* make sense? */
    277   1.7  dholland 			continue;
    278   1.7  dholland 		} else if (error) {
    279   1.7  dholland 			goto fail;
    280   1.7  dholland 		}
    281   1.8  dholland 
    282  1.10  dholland 		error = vfs_quotactl_get_addreply(&qk, &blocks, &files,
    283   1.8  dholland 						  replies);
    284   1.7  dholland 	}
    285   1.7  dholland 
    286   1.7  dholland 	prop_object_iterator_release(iter);
    287   1.7  dholland 	if (!prop_dictionary_set_and_rel(cmddict, "data", replies)) {
    288   1.7  dholland 		error = ENOMEM;
    289   1.7  dholland 	} else {
    290   1.7  dholland 		error = 0;
    291   1.7  dholland 	}
    292   1.7  dholland 
    293   1.7  dholland 	return error;
    294   1.7  dholland 
    295   1.7  dholland  fail:
    296   1.7  dholland 	prop_object_iterator_release(iter);
    297   1.7  dholland 	prop_object_release(replies);
    298   1.7  dholland 	return error;
    299   1.5  dholland }
    300   1.5  dholland 
    301   1.5  dholland static int
    302  1.15  dholland vfs_quotactl_put_extractinfo(prop_dictionary_t data,
    303  1.12  dholland 			struct quotaval *blocks, struct quotaval *files)
    304  1.12  dholland {
    305  1.12  dholland 	/*
    306  1.12  dholland 	 * So, the way proptoquota64 works is that you pass it an
    307  1.12  dholland 	 * array of pointers to uint64. Each of these pointers is
    308  1.12  dholland 	 * supposed to point to 5 (UFS_QUOTA_NENTRIES) uint64s. This
    309  1.12  dholland 	 * array of pointers is the second argument. The third and
    310  1.12  dholland 	 * forth argument are the names of the five values to extract,
    311  1.12  dholland 	 * and UFS_QUOTA_NENTRIES. The last two arguments are the
    312  1.12  dholland 	 * names assocated with the pointers (QUOTATYPE_LDICT_BLOCK,
    313  1.12  dholland 	 * QUOTADICT_LTYPE_FILE) and the number of pointers. Most of
    314  1.12  dholland 	 * the existing code was unsafely casting struct quotaval
    315  1.12  dholland 	 * (formerly struct ufs_quota_entry) to (uint64_t *) and using
    316  1.12  dholland 	 * that as the block of 5 uint64s. Or worse, pointing to
    317  1.12  dholland 	 * subregions of that and reducing the number of uint64s to
    318  1.12  dholland 	 * pull "adjacent" values. Demons fly out of your nose!
    319  1.12  dholland 	 */
    320  1.12  dholland 
    321  1.12  dholland 	uint64_t bvals[UFS_QUOTA_NENTRIES];
    322  1.12  dholland 	uint64_t fvals[UFS_QUOTA_NENTRIES];
    323  1.12  dholland 	uint64_t *valptrs[QUOTA_NLIMITS];
    324  1.12  dholland 	int error;
    325  1.12  dholland 
    326  1.12  dholland 	valptrs[QUOTA_LIMIT_BLOCK] = bvals;
    327  1.12  dholland 	valptrs[QUOTA_LIMIT_FILE] = fvals;
    328  1.12  dholland 	error = proptoquota64(data, valptrs,
    329  1.12  dholland 			      ufs_quota_entry_names, UFS_QUOTA_NENTRIES,
    330  1.12  dholland 			      ufs_quota_limit_names, QUOTA_NLIMITS);
    331  1.12  dholland 	if (error) {
    332  1.12  dholland 		return error;
    333  1.12  dholland 	}
    334  1.12  dholland 
    335  1.12  dholland 	/*
    336  1.12  dholland 	 * There are no symbolic constants for these indexes!
    337  1.12  dholland 	 */
    338  1.12  dholland 
    339  1.12  dholland 	blocks->qv_hardlimit = bvals[0];
    340  1.12  dholland 	blocks->qv_softlimit = bvals[1];
    341  1.12  dholland 	blocks->qv_usage = bvals[2];
    342  1.12  dholland 	blocks->qv_expiretime = bvals[3];
    343  1.12  dholland 	blocks->qv_grace = bvals[4];
    344  1.12  dholland 	files->qv_hardlimit = fvals[0];
    345  1.12  dholland 	files->qv_softlimit = fvals[1];
    346  1.12  dholland 	files->qv_usage = fvals[2];
    347  1.12  dholland 	files->qv_expiretime = fvals[3];
    348  1.12  dholland 	files->qv_grace = fvals[4];
    349  1.12  dholland 
    350  1.12  dholland 	return 0;
    351  1.12  dholland }
    352  1.12  dholland 
    353  1.12  dholland static int
    354  1.15  dholland vfs_quotactl_put(struct mount *mp,
    355   1.5  dholland 			prop_dictionary_t cmddict, int q2type,
    356   1.5  dholland 			prop_array_t datas)
    357   1.5  dholland {
    358  1.11  dholland 	prop_array_t replies;
    359  1.11  dholland 	prop_object_iterator_t iter;
    360  1.11  dholland 	prop_dictionary_t data;
    361  1.11  dholland 	int defaultq;
    362  1.11  dholland 	uint32_t id;
    363  1.11  dholland 	const char *idstr;
    364  1.14  dholland 	struct quotakey qk;
    365  1.12  dholland 	struct quotaval blocks, files;
    366   1.5  dholland 	struct vfs_quotactl_args args;
    367  1.11  dholland 	int error;
    368  1.11  dholland 
    369  1.11  dholland 	KASSERT(prop_object_type(cmddict) == PROP_TYPE_DICTIONARY);
    370  1.11  dholland 	KASSERT(prop_object_type(datas) == PROP_TYPE_ARRAY);
    371   1.5  dholland 
    372  1.11  dholland 	replies = prop_array_create();
    373  1.11  dholland 	if (replies == NULL)
    374  1.11  dholland 		return ENOMEM;
    375  1.11  dholland 
    376  1.11  dholland 	iter = prop_array_iterator(datas);
    377  1.11  dholland 	if (iter == NULL) {
    378  1.11  dholland 		prop_object_release(replies);
    379  1.11  dholland 		return ENOMEM;
    380  1.11  dholland 	}
    381  1.11  dholland 
    382  1.11  dholland 	while ((data = prop_object_iterator_next(iter)) != NULL) {
    383  1.13  dholland 
    384  1.13  dholland 		KASSERT(prop_object_type(data) == PROP_TYPE_DICTIONARY);
    385  1.13  dholland 
    386  1.11  dholland 		if (!prop_dictionary_get_uint32(data, "id", &id)) {
    387  1.11  dholland 			if (!prop_dictionary_get_cstring_nocopy(data, "id",
    388  1.11  dholland 			    &idstr))
    389  1.11  dholland 				continue;
    390  1.11  dholland 			if (strcmp(idstr, "default"))
    391  1.11  dholland 				continue;
    392  1.11  dholland 			id = 0;
    393  1.11  dholland 			defaultq = 1;
    394  1.11  dholland 		} else {
    395  1.11  dholland 			defaultq = 0;
    396  1.11  dholland 		}
    397  1.11  dholland 
    398  1.15  dholland 		error = vfs_quotactl_put_extractinfo(data, &blocks, &files);
    399  1.12  dholland 		if (error) {
    400  1.12  dholland 			goto err;
    401  1.12  dholland 		}
    402  1.12  dholland 
    403  1.14  dholland 		qk.qk_idtype = q2type;
    404  1.14  dholland 		qk.qk_id = defaultq ? QUOTA_DEFAULTID : id;
    405  1.14  dholland 		qk.qk_objtype = QUOTA_OBJTYPE_BLOCKS;
    406  1.14  dholland 
    407  1.15  dholland 		args.qc_type = QCT_PUT;
    408  1.15  dholland 		args.u.put.qc_key = &qk;
    409  1.15  dholland 		args.u.put.qc_val = &blocks;
    410  1.15  dholland 		error = VFS_QUOTACTL(mp, QUOTACTL_PUT, &args);
    411  1.13  dholland 		if (error) {
    412  1.13  dholland 			goto err;
    413  1.13  dholland 		}
    414  1.13  dholland 
    415  1.14  dholland 		qk.qk_idtype = q2type;
    416  1.14  dholland 		qk.qk_id = defaultq ? QUOTA_DEFAULTID : id;
    417  1.14  dholland 		qk.qk_objtype = QUOTA_OBJTYPE_FILES;
    418  1.14  dholland 
    419  1.15  dholland 		args.qc_type = QCT_PUT;
    420  1.15  dholland 		args.u.put.qc_key = &qk;
    421  1.15  dholland 		args.u.put.qc_val = &files;
    422  1.15  dholland 		error = VFS_QUOTACTL(mp, QUOTACTL_PUT, &args);
    423  1.11  dholland 		if (error) {
    424  1.11  dholland 			goto err;
    425  1.11  dholland 		}
    426  1.11  dholland 	}
    427  1.11  dholland 	prop_object_iterator_release(iter);
    428  1.11  dholland 	if (!prop_dictionary_set_and_rel(cmddict, "data", replies)) {
    429  1.11  dholland 		error = ENOMEM;
    430  1.11  dholland 	} else {
    431  1.11  dholland 		error = 0;
    432  1.11  dholland 	}
    433  1.11  dholland 	return error;
    434  1.11  dholland err:
    435  1.11  dholland 	prop_object_iterator_release(iter);
    436  1.11  dholland 	prop_object_release(replies);
    437  1.11  dholland 	return error;
    438   1.5  dholland }
    439   1.5  dholland 
    440   1.5  dholland static int
    441   1.5  dholland vfs_quotactl_getall(struct mount *mp,
    442   1.5  dholland 			prop_dictionary_t cmddict, int q2type,
    443   1.5  dholland 			prop_array_t datas)
    444   1.5  dholland {
    445  1.21  dholland 	struct quotakcursor cursor;
    446   1.5  dholland 	struct vfs_quotactl_args args;
    447  1.21  dholland 	int error, error2;
    448  1.21  dholland 
    449  1.21  dholland 	args.qc_type = QCT_CURSOROPEN;
    450  1.21  dholland 	args.u.cursoropen.qc_cursor = &cursor;
    451  1.21  dholland 	error = VFS_QUOTACTL(mp, QUOTACTL_CURSOROPEN, &args);
    452  1.21  dholland 	if (error) {
    453  1.21  dholland 		return error;
    454  1.21  dholland 	}
    455   1.5  dholland 
    456  1.22  dholland 	args.qc_type = QCT_GETALL;
    457  1.22  dholland 	args.u.getall.qc_cursor = &cursor;
    458  1.22  dholland 	args.u.getall.qc_idtype = q2type;
    459  1.22  dholland 	args.u.getall.qc_cmddict = cmddict;
    460  1.21  dholland 	error = VFS_QUOTACTL(mp, QUOTACTL_GETALL, &args);
    461  1.21  dholland 
    462  1.21  dholland 	args.qc_type = QCT_CURSORCLOSE;
    463  1.21  dholland 	args.u.cursorclose.qc_cursor = &cursor;
    464  1.21  dholland 	error2 = VFS_QUOTACTL(mp, QUOTACTL_CURSORCLOSE, &args);
    465  1.21  dholland 
    466  1.21  dholland 	if (error) {
    467  1.21  dholland 		return error;
    468  1.21  dholland 	}
    469  1.21  dholland 	error = error2;
    470  1.21  dholland 	return error;
    471   1.5  dholland }
    472   1.5  dholland 
    473   1.5  dholland static int
    474   1.5  dholland vfs_quotactl_clear(struct mount *mp,
    475   1.5  dholland 			prop_dictionary_t cmddict, int q2type,
    476   1.5  dholland 			prop_array_t datas)
    477   1.5  dholland {
    478  1.16  dholland 	prop_array_t replies;
    479  1.16  dholland 	prop_object_iterator_t iter;
    480  1.16  dholland 	prop_dictionary_t data;
    481  1.16  dholland 	uint32_t id;
    482  1.16  dholland 	int defaultq;
    483  1.16  dholland 	const char *idstr;
    484  1.20  dholland 	struct quotakey qk;
    485   1.5  dholland 	struct vfs_quotactl_args args;
    486  1.16  dholland 	int error;
    487  1.16  dholland 
    488  1.16  dholland 	KASSERT(prop_object_type(cmddict) == PROP_TYPE_DICTIONARY);
    489  1.16  dholland 	KASSERT(prop_object_type(datas) == PROP_TYPE_ARRAY);
    490  1.16  dholland 
    491  1.16  dholland 	replies = prop_array_create();
    492  1.16  dholland 	if (replies == NULL)
    493  1.16  dholland 		return ENOMEM;
    494   1.5  dholland 
    495  1.16  dholland 	iter = prop_array_iterator(datas);
    496  1.16  dholland 	if (iter == NULL) {
    497  1.16  dholland 		prop_object_release(replies);
    498  1.16  dholland 		return ENOMEM;
    499  1.16  dholland 	}
    500  1.16  dholland 
    501  1.16  dholland 	while ((data = prop_object_iterator_next(iter)) != NULL) {
    502  1.16  dholland 		if (!prop_dictionary_get_uint32(data, "id", &id)) {
    503  1.16  dholland 			if (!prop_dictionary_get_cstring_nocopy(data, "id",
    504  1.16  dholland 			    &idstr))
    505  1.16  dholland 				continue;
    506  1.16  dholland 			if (strcmp(idstr, "default"))
    507  1.16  dholland 				continue;
    508  1.16  dholland 			id = 0;
    509  1.16  dholland 			defaultq = 1;
    510  1.16  dholland 		} else {
    511  1.16  dholland 			defaultq = 0;
    512  1.16  dholland 		}
    513  1.16  dholland 
    514  1.20  dholland 		qk.qk_idtype = q2type;
    515  1.20  dholland 		qk.qk_id = defaultq ? QUOTA_DEFAULTID : id;
    516  1.20  dholland 		qk.qk_objtype = QUOTA_OBJTYPE_BLOCKS;
    517  1.20  dholland 
    518  1.19  dholland 		args.qc_type = QCT_DELETE;
    519  1.20  dholland 		args.u.delete.qc_key = &qk;
    520  1.19  dholland 		error = VFS_QUOTACTL(mp, QUOTACTL_DELETE, &args);
    521  1.18  dholland 		if (error) {
    522  1.18  dholland 			goto err;
    523  1.18  dholland 		}
    524  1.18  dholland 
    525  1.20  dholland 		qk.qk_idtype = q2type;
    526  1.20  dholland 		qk.qk_id = defaultq ? QUOTA_DEFAULTID : id;
    527  1.20  dholland 		qk.qk_objtype = QUOTA_OBJTYPE_FILES;
    528  1.20  dholland 
    529  1.19  dholland 		args.qc_type = QCT_DELETE;
    530  1.20  dholland 		args.u.delete.qc_key = &qk;
    531  1.19  dholland 		error = VFS_QUOTACTL(mp, QUOTACTL_DELETE, &args);
    532  1.16  dholland 		if (error) {
    533  1.16  dholland 			goto err;
    534  1.16  dholland 		}
    535  1.16  dholland 	}
    536  1.16  dholland 
    537  1.16  dholland 	prop_object_iterator_release(iter);
    538  1.16  dholland 	if (!prop_dictionary_set_and_rel(cmddict, "data", replies)) {
    539  1.16  dholland 		error = ENOMEM;
    540  1.16  dholland 	} else {
    541  1.16  dholland 		error = 0;
    542  1.16  dholland 	}
    543  1.16  dholland 	return error;
    544  1.16  dholland err:
    545  1.16  dholland 	prop_object_iterator_release(iter);
    546  1.16  dholland 	prop_object_release(replies);
    547  1.16  dholland 	return error;
    548   1.5  dholland }
    549   1.5  dholland 
    550   1.5  dholland static int
    551   1.4  dholland vfs_quotactl_cmd(struct mount *mp, prop_dictionary_t cmddict)
    552   1.4  dholland {
    553   1.4  dholland 	int error;
    554   1.4  dholland 	const char *cmd, *type;
    555   1.4  dholland 	prop_array_t datas;
    556   1.4  dholland 	int q2type;
    557   1.4  dholland 
    558   1.4  dholland 	if (!prop_dictionary_get_cstring_nocopy(cmddict, "command", &cmd))
    559   1.4  dholland 		return EINVAL;
    560   1.4  dholland 	if (!prop_dictionary_get_cstring_nocopy(cmddict, "type", &type))
    561   1.4  dholland 		return EINVAL;
    562   1.4  dholland 
    563   1.4  dholland 	if (!strcmp(type, QUOTADICT_CLASS_USER)) {
    564   1.4  dholland 		q2type = QUOTA_CLASS_USER;
    565   1.4  dholland 	} else if (!strcmp(type, QUOTADICT_CLASS_GROUP)) {
    566   1.4  dholland 		q2type = QUOTA_CLASS_GROUP;
    567   1.4  dholland 	} else {
    568   1.4  dholland 		/* XXX this is a bad errno for this case */
    569   1.4  dholland 		return EOPNOTSUPP;
    570   1.4  dholland 	}
    571   1.4  dholland 
    572   1.4  dholland 	datas = prop_dictionary_get(cmddict, "data");
    573   1.4  dholland 	if (datas == NULL || prop_object_type(datas) != PROP_TYPE_ARRAY)
    574   1.4  dholland 		return EINVAL;
    575   1.4  dholland 
    576   1.4  dholland 	prop_object_retain(datas);
    577   1.4  dholland 	prop_dictionary_remove(cmddict, "data"); /* prepare for return */
    578   1.4  dholland 
    579   1.4  dholland 	if (strcmp(cmd, "get version") == 0) {
    580   1.5  dholland 		error = vfs_quotactl_getversion(mp, cmddict, q2type, datas);
    581   1.4  dholland 	} else if (strcmp(cmd, "quotaon") == 0) {
    582   1.5  dholland 		error = vfs_quotactl_quotaon(mp, cmddict, q2type, datas);
    583   1.4  dholland 	} else if (strcmp(cmd, "quotaoff") == 0) {
    584   1.5  dholland 		error = vfs_quotactl_quotaoff(mp, cmddict, q2type, datas);
    585   1.4  dholland 	} else if (strcmp(cmd, "get") == 0) {
    586   1.5  dholland 		error = vfs_quotactl_get(mp, cmddict, q2type, datas);
    587   1.4  dholland 	} else if (strcmp(cmd, "set") == 0) {
    588  1.15  dholland 		error = vfs_quotactl_put(mp, cmddict, q2type, datas);
    589   1.4  dholland 	} else if (strcmp(cmd, "getall") == 0) {
    590   1.5  dholland 		error = vfs_quotactl_getall(mp, cmddict, q2type, datas);
    591   1.4  dholland 	} else if (strcmp(cmd, "clear") == 0) {
    592   1.5  dholland 		error = vfs_quotactl_clear(mp, cmddict, q2type, datas);
    593   1.4  dholland 	} else {
    594   1.4  dholland 		/* XXX this a bad errno for this case */
    595   1.4  dholland 		error = EOPNOTSUPP;
    596   1.4  dholland 	}
    597   1.4  dholland 
    598   1.4  dholland 	error = (prop_dictionary_set_int8(cmddict, "return",
    599   1.4  dholland 	    error) ? 0 : ENOMEM);
    600   1.4  dholland 	prop_object_release(datas);
    601   1.4  dholland 
    602   1.4  dholland 	return error;
    603   1.4  dholland }
    604   1.4  dholland 
    605   1.2  dholland int
    606   1.2  dholland vfs_quotactl(struct mount *mp, prop_dictionary_t dict)
    607   1.2  dholland {
    608   1.3  dholland 	prop_dictionary_t cmddict;
    609   1.3  dholland 	prop_array_t commands;
    610   1.3  dholland 	prop_object_iterator_t iter;
    611   1.3  dholland 	int error;
    612   1.3  dholland 
    613   1.3  dholland 	error = quota_get_cmds(dict, &commands);
    614   1.3  dholland 	if (error) {
    615   1.3  dholland 		return error;
    616   1.3  dholland 	}
    617   1.3  dholland 
    618   1.3  dholland 	iter = prop_array_iterator(commands);
    619   1.3  dholland 	if (iter == NULL) {
    620   1.3  dholland 		return ENOMEM;
    621   1.3  dholland 	}
    622   1.3  dholland 
    623   1.3  dholland 	while ((cmddict = prop_object_iterator_next(iter)) != NULL) {
    624   1.3  dholland 		if (prop_object_type(cmddict) != PROP_TYPE_DICTIONARY) {
    625   1.3  dholland 			/* XXX shouldn't this be an error? */
    626   1.3  dholland 			continue;
    627   1.3  dholland 		}
    628   1.4  dholland 		error = vfs_quotactl_cmd(mp, cmddict);
    629   1.3  dholland 		if (error) {
    630   1.3  dholland 			break;
    631   1.3  dholland 		}
    632   1.3  dholland 	}
    633   1.3  dholland 	prop_object_iterator_release(iter);
    634   1.3  dholland 	return error;
    635   1.2  dholland }
    636