Home | History | Annotate | Line # | Download | only in kern
vfs_quotactl.c revision 1.23
      1  1.23  dholland /*	$NetBSD: vfs_quotactl.c,v 1.23 2012/01/29 07:02:06 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.23  dholland __KERNEL_RCSID(0, "$NetBSD: vfs_quotactl.c,v 1.23 2012/01/29 07:02:06 dholland Exp $");
     84   1.1  dholland 
     85  1.23  dholland #include <sys/malloc.h> /* XXX: temporary */
     86   1.2  dholland #include <sys/mount.h>
     87   1.8  dholland #include <sys/quota.h>
     88   1.4  dholland #include <sys/quotactl.h>
     89   1.3  dholland #include <quota/quotaprop.h>
     90   1.2  dholland 
     91   1.4  dholland static int
     92   1.5  dholland vfs_quotactl_getversion(struct mount *mp,
     93   1.5  dholland 			prop_dictionary_t cmddict, int q2type,
     94   1.5  dholland 			prop_array_t datas)
     95   1.5  dholland {
     96   1.6  dholland 	prop_array_t replies;
     97   1.6  dholland 	prop_dictionary_t data;
     98   1.6  dholland 	int q2version;
     99   1.5  dholland 	struct vfs_quotactl_args args;
    100   1.6  dholland 	int error;
    101   1.6  dholland 
    102   1.6  dholland 	KASSERT(prop_object_type(cmddict) == PROP_TYPE_DICTIONARY);
    103   1.6  dholland 	KASSERT(prop_object_type(datas) == PROP_TYPE_ARRAY);
    104   1.6  dholland 
    105   1.6  dholland 	args.qc_type = QCT_GETVERSION;
    106   1.6  dholland 	args.u.getversion.qc_version_ret = &q2version;
    107   1.6  dholland 	error = VFS_QUOTACTL(mp, QUOTACTL_GETVERSION, &args);
    108   1.6  dholland 	if (error) {
    109   1.6  dholland 		return error;
    110   1.6  dholland 	}
    111   1.6  dholland 
    112   1.6  dholland 	data = prop_dictionary_create();
    113   1.6  dholland 	if (data == NULL) {
    114   1.6  dholland 		return ENOMEM;
    115   1.6  dholland 	}
    116   1.6  dholland 
    117   1.6  dholland 	if (!prop_dictionary_set_int8(data, "version", q2version)) {
    118   1.6  dholland 		prop_object_release(data);
    119   1.6  dholland 		return ENOMEM;
    120   1.6  dholland 	}
    121   1.6  dholland 
    122   1.6  dholland 	replies = prop_array_create();
    123   1.6  dholland 	if (replies == NULL) {
    124   1.6  dholland 		prop_object_release(data);
    125   1.6  dholland 		return ENOMEM;
    126   1.6  dholland 	}
    127   1.5  dholland 
    128   1.6  dholland 	if (!prop_array_add_and_rel(replies, data)) {
    129   1.6  dholland 		prop_object_release(data);
    130   1.6  dholland 		prop_object_release(replies);
    131   1.6  dholland 		return ENOMEM;
    132   1.6  dholland 	}
    133   1.6  dholland 
    134   1.6  dholland 	if (!prop_dictionary_set_and_rel(cmddict, "data", replies)) {
    135   1.6  dholland 		prop_object_release(replies);
    136   1.6  dholland 		return ENOMEM;
    137   1.6  dholland 	}
    138   1.6  dholland 
    139   1.6  dholland 	return error;
    140   1.5  dholland }
    141   1.5  dholland 
    142   1.5  dholland static int
    143   1.5  dholland vfs_quotactl_quotaon(struct mount *mp,
    144   1.5  dholland 		     prop_dictionary_t cmddict, int q2type,
    145   1.5  dholland 		     prop_array_t datas)
    146   1.5  dholland {
    147   1.5  dholland 	struct vfs_quotactl_args args;
    148   1.5  dholland 
    149   1.5  dholland 	args.qc_type = QCT_PROPLIB;
    150   1.5  dholland 	args.u.proplib.qc_cmddict = cmddict;
    151   1.5  dholland 	args.u.proplib.qc_q2type = q2type;
    152   1.5  dholland 	args.u.proplib.qc_datas = datas;
    153   1.5  dholland 	return VFS_QUOTACTL(mp, QUOTACTL_QUOTAON, &args);
    154   1.5  dholland }
    155   1.5  dholland 
    156   1.5  dholland static int
    157   1.5  dholland vfs_quotactl_quotaoff(struct mount *mp,
    158   1.5  dholland 			prop_dictionary_t cmddict, int q2type,
    159   1.5  dholland 			prop_array_t datas)
    160   1.5  dholland {
    161   1.5  dholland 	struct vfs_quotactl_args args;
    162   1.5  dholland 
    163   1.5  dholland 	args.qc_type = QCT_PROPLIB;
    164   1.5  dholland 	args.u.proplib.qc_cmddict = cmddict;
    165   1.5  dholland 	args.u.proplib.qc_q2type = q2type;
    166   1.5  dholland 	args.u.proplib.qc_datas = datas;
    167   1.5  dholland 	return VFS_QUOTACTL(mp, QUOTACTL_QUOTAOFF, &args);
    168   1.5  dholland }
    169   1.5  dholland 
    170   1.5  dholland static int
    171  1.10  dholland vfs_quotactl_get_addreply(const struct quotakey *qk,
    172   1.8  dholland 			  const struct quotaval *blocks,
    173   1.8  dholland 			  const struct quotaval *files,
    174   1.8  dholland 			  prop_array_t replies)
    175   1.8  dholland {
    176   1.8  dholland 	prop_dictionary_t dict;
    177  1.10  dholland 	id_t id;
    178  1.10  dholland 	int defaultq;
    179  1.10  dholland 	uint64_t *valuesp[QUOTA_NLIMITS];
    180   1.8  dholland 
    181   1.8  dholland 	/* XXX illegal casts */
    182   1.8  dholland 	valuesp[QUOTA_LIMIT_BLOCK] = (void *)(intptr_t)&blocks->qv_hardlimit;
    183   1.8  dholland 	valuesp[QUOTA_LIMIT_FILE] =  (void *)(intptr_t)&files->qv_hardlimit;
    184   1.8  dholland 
    185  1.10  dholland 	if (qk->qk_id == QUOTA_DEFAULTID) {
    186  1.10  dholland 		id = 0;
    187  1.10  dholland 		defaultq = 1;
    188  1.10  dholland 	} else {
    189  1.10  dholland 		id = qk->qk_id;
    190  1.10  dholland 		defaultq = 0;
    191  1.10  dholland 	}
    192  1.10  dholland 
    193   1.8  dholland 	dict = quota64toprop(id, defaultq, valuesp,
    194   1.8  dholland 	    ufs_quota_entry_names, UFS_QUOTA_NENTRIES,
    195   1.8  dholland 	    ufs_quota_limit_names, QUOTA_NLIMITS);
    196   1.8  dholland 	if (dict == NULL)
    197   1.8  dholland 		return ENOMEM;
    198   1.8  dholland 	if (!prop_array_add_and_rel(replies, dict)) {
    199   1.8  dholland 		prop_object_release(dict);
    200   1.8  dholland 		return ENOMEM;
    201   1.8  dholland 	}
    202   1.8  dholland 
    203   1.8  dholland 	return 0;
    204   1.8  dholland }
    205   1.8  dholland 
    206   1.8  dholland static int
    207   1.5  dholland vfs_quotactl_get(struct mount *mp,
    208  1.10  dholland 			prop_dictionary_t cmddict, int idtype,
    209   1.5  dholland 			prop_array_t datas)
    210   1.5  dholland {
    211   1.7  dholland 	prop_object_iterator_t iter;
    212   1.7  dholland 	prop_dictionary_t data;
    213   1.8  dholland 	prop_array_t replies;
    214   1.7  dholland 	uint32_t id;
    215   1.7  dholland 	const char *idstr;
    216   1.5  dholland 	struct vfs_quotactl_args args;
    217  1.10  dholland 	struct quotakey qk;
    218   1.8  dholland 	struct quotaval blocks, files;
    219   1.7  dholland 	int error;
    220   1.7  dholland 
    221   1.7  dholland 	KASSERT(prop_object_type(cmddict) == PROP_TYPE_DICTIONARY);
    222   1.7  dholland 	KASSERT(prop_object_type(datas) == PROP_TYPE_ARRAY);
    223   1.7  dholland 
    224   1.7  dholland 	replies = prop_array_create();
    225   1.7  dholland 	if (replies == NULL) {
    226   1.7  dholland 		return ENOMEM;
    227   1.7  dholland 	}
    228   1.7  dholland 
    229   1.7  dholland 	iter = prop_array_iterator(datas);
    230   1.7  dholland 	if (iter == NULL) {
    231   1.7  dholland 		prop_object_release(replies);
    232   1.7  dholland 		return ENOMEM;
    233   1.7  dholland 	}
    234   1.7  dholland 
    235   1.7  dholland 	while ((data = prop_object_iterator_next(iter)) != NULL) {
    236  1.10  dholland 		qk.qk_idtype = idtype;
    237  1.10  dholland 
    238   1.7  dholland 		if (!prop_dictionary_get_uint32(data, "id", &id)) {
    239   1.7  dholland 			if (!prop_dictionary_get_cstring_nocopy(data, "id",
    240   1.7  dholland 			    &idstr))
    241   1.7  dholland 				continue;
    242   1.7  dholland 			if (strcmp(idstr, "default")) {
    243   1.7  dholland 				error = EINVAL;
    244   1.7  dholland 				goto fail;
    245   1.7  dholland 			}
    246  1.10  dholland 			qk.qk_id = QUOTA_DEFAULTID;
    247   1.7  dholland 		} else {
    248  1.10  dholland 			qk.qk_id = id;
    249   1.7  dholland 		}
    250   1.5  dholland 
    251  1.10  dholland 		qk.qk_objtype = QUOTA_OBJTYPE_BLOCKS;
    252  1.10  dholland 
    253   1.7  dholland 		args.qc_type = QCT_GET;
    254  1.10  dholland 		args.u.get.qc_key = &qk;
    255   1.9  dholland 		args.u.get.qc_ret = &blocks;
    256   1.9  dholland 		error = VFS_QUOTACTL(mp, QUOTACTL_GET, &args);
    257   1.9  dholland 		if (error == EPERM) {
    258   1.9  dholland 			/* XXX does this make sense? */
    259   1.9  dholland 			continue;
    260   1.9  dholland 		} else if (error == ENOENT) {
    261   1.9  dholland 			/* XXX does *this* make sense? */
    262   1.9  dholland 			continue;
    263   1.9  dholland 		} else if (error) {
    264   1.9  dholland 			goto fail;
    265   1.9  dholland 		}
    266   1.9  dholland 
    267  1.10  dholland 		qk.qk_objtype = QUOTA_OBJTYPE_FILES;
    268  1.10  dholland 
    269   1.9  dholland 		args.qc_type = QCT_GET;
    270  1.10  dholland 		args.u.get.qc_key = &qk;
    271   1.9  dholland 		args.u.get.qc_ret = &files;
    272   1.7  dholland 		error = VFS_QUOTACTL(mp, QUOTACTL_GET, &args);
    273   1.7  dholland 		if (error == EPERM) {
    274   1.7  dholland 			/* XXX does this make sense? */
    275   1.7  dholland 			continue;
    276   1.7  dholland 		} else if (error == ENOENT) {
    277   1.7  dholland 			/* XXX does *this* make sense? */
    278   1.7  dholland 			continue;
    279   1.7  dholland 		} else if (error) {
    280   1.7  dholland 			goto fail;
    281   1.7  dholland 		}
    282   1.8  dholland 
    283  1.10  dholland 		error = vfs_quotactl_get_addreply(&qk, &blocks, &files,
    284   1.8  dholland 						  replies);
    285   1.7  dholland 	}
    286   1.7  dholland 
    287   1.7  dholland 	prop_object_iterator_release(iter);
    288   1.7  dholland 	if (!prop_dictionary_set_and_rel(cmddict, "data", replies)) {
    289   1.7  dholland 		error = ENOMEM;
    290   1.7  dholland 	} else {
    291   1.7  dholland 		error = 0;
    292   1.7  dholland 	}
    293   1.7  dholland 
    294   1.7  dholland 	return error;
    295   1.7  dholland 
    296   1.7  dholland  fail:
    297   1.7  dholland 	prop_object_iterator_release(iter);
    298   1.7  dholland 	prop_object_release(replies);
    299   1.7  dholland 	return error;
    300   1.5  dholland }
    301   1.5  dholland 
    302   1.5  dholland static int
    303  1.15  dholland vfs_quotactl_put_extractinfo(prop_dictionary_t data,
    304  1.12  dholland 			struct quotaval *blocks, struct quotaval *files)
    305  1.12  dholland {
    306  1.12  dholland 	/*
    307  1.12  dholland 	 * So, the way proptoquota64 works is that you pass it an
    308  1.12  dholland 	 * array of pointers to uint64. Each of these pointers is
    309  1.12  dholland 	 * supposed to point to 5 (UFS_QUOTA_NENTRIES) uint64s. This
    310  1.12  dholland 	 * array of pointers is the second argument. The third and
    311  1.12  dholland 	 * forth argument are the names of the five values to extract,
    312  1.12  dholland 	 * and UFS_QUOTA_NENTRIES. The last two arguments are the
    313  1.12  dholland 	 * names assocated with the pointers (QUOTATYPE_LDICT_BLOCK,
    314  1.12  dholland 	 * QUOTADICT_LTYPE_FILE) and the number of pointers. Most of
    315  1.12  dholland 	 * the existing code was unsafely casting struct quotaval
    316  1.12  dholland 	 * (formerly struct ufs_quota_entry) to (uint64_t *) and using
    317  1.12  dholland 	 * that as the block of 5 uint64s. Or worse, pointing to
    318  1.12  dholland 	 * subregions of that and reducing the number of uint64s to
    319  1.12  dholland 	 * pull "adjacent" values. Demons fly out of your nose!
    320  1.12  dholland 	 */
    321  1.12  dholland 
    322  1.12  dholland 	uint64_t bvals[UFS_QUOTA_NENTRIES];
    323  1.12  dholland 	uint64_t fvals[UFS_QUOTA_NENTRIES];
    324  1.12  dholland 	uint64_t *valptrs[QUOTA_NLIMITS];
    325  1.12  dholland 	int error;
    326  1.12  dholland 
    327  1.12  dholland 	valptrs[QUOTA_LIMIT_BLOCK] = bvals;
    328  1.12  dholland 	valptrs[QUOTA_LIMIT_FILE] = fvals;
    329  1.12  dholland 	error = proptoquota64(data, valptrs,
    330  1.12  dholland 			      ufs_quota_entry_names, UFS_QUOTA_NENTRIES,
    331  1.12  dholland 			      ufs_quota_limit_names, QUOTA_NLIMITS);
    332  1.12  dholland 	if (error) {
    333  1.12  dholland 		return error;
    334  1.12  dholland 	}
    335  1.12  dholland 
    336  1.12  dholland 	/*
    337  1.12  dholland 	 * There are no symbolic constants for these indexes!
    338  1.12  dholland 	 */
    339  1.12  dholland 
    340  1.12  dholland 	blocks->qv_hardlimit = bvals[0];
    341  1.12  dholland 	blocks->qv_softlimit = bvals[1];
    342  1.12  dholland 	blocks->qv_usage = bvals[2];
    343  1.12  dholland 	blocks->qv_expiretime = bvals[3];
    344  1.12  dholland 	blocks->qv_grace = bvals[4];
    345  1.12  dholland 	files->qv_hardlimit = fvals[0];
    346  1.12  dholland 	files->qv_softlimit = fvals[1];
    347  1.12  dholland 	files->qv_usage = fvals[2];
    348  1.12  dholland 	files->qv_expiretime = fvals[3];
    349  1.12  dholland 	files->qv_grace = fvals[4];
    350  1.12  dholland 
    351  1.12  dholland 	return 0;
    352  1.12  dholland }
    353  1.12  dholland 
    354  1.12  dholland static int
    355  1.15  dholland vfs_quotactl_put(struct mount *mp,
    356   1.5  dholland 			prop_dictionary_t cmddict, int q2type,
    357   1.5  dholland 			prop_array_t datas)
    358   1.5  dholland {
    359  1.11  dholland 	prop_array_t replies;
    360  1.11  dholland 	prop_object_iterator_t iter;
    361  1.11  dholland 	prop_dictionary_t data;
    362  1.11  dholland 	int defaultq;
    363  1.11  dholland 	uint32_t id;
    364  1.11  dholland 	const char *idstr;
    365  1.14  dholland 	struct quotakey qk;
    366  1.12  dholland 	struct quotaval blocks, files;
    367   1.5  dholland 	struct vfs_quotactl_args args;
    368  1.11  dholland 	int error;
    369  1.11  dholland 
    370  1.11  dholland 	KASSERT(prop_object_type(cmddict) == PROP_TYPE_DICTIONARY);
    371  1.11  dholland 	KASSERT(prop_object_type(datas) == PROP_TYPE_ARRAY);
    372   1.5  dholland 
    373  1.11  dholland 	replies = prop_array_create();
    374  1.11  dholland 	if (replies == NULL)
    375  1.11  dholland 		return ENOMEM;
    376  1.11  dholland 
    377  1.11  dholland 	iter = prop_array_iterator(datas);
    378  1.11  dholland 	if (iter == NULL) {
    379  1.11  dholland 		prop_object_release(replies);
    380  1.11  dholland 		return ENOMEM;
    381  1.11  dholland 	}
    382  1.11  dholland 
    383  1.11  dholland 	while ((data = prop_object_iterator_next(iter)) != NULL) {
    384  1.13  dholland 
    385  1.13  dholland 		KASSERT(prop_object_type(data) == PROP_TYPE_DICTIONARY);
    386  1.13  dholland 
    387  1.11  dholland 		if (!prop_dictionary_get_uint32(data, "id", &id)) {
    388  1.11  dholland 			if (!prop_dictionary_get_cstring_nocopy(data, "id",
    389  1.11  dholland 			    &idstr))
    390  1.11  dholland 				continue;
    391  1.11  dholland 			if (strcmp(idstr, "default"))
    392  1.11  dholland 				continue;
    393  1.11  dholland 			id = 0;
    394  1.11  dholland 			defaultq = 1;
    395  1.11  dholland 		} else {
    396  1.11  dholland 			defaultq = 0;
    397  1.11  dholland 		}
    398  1.11  dholland 
    399  1.15  dholland 		error = vfs_quotactl_put_extractinfo(data, &blocks, &files);
    400  1.12  dholland 		if (error) {
    401  1.12  dholland 			goto err;
    402  1.12  dholland 		}
    403  1.12  dholland 
    404  1.14  dholland 		qk.qk_idtype = q2type;
    405  1.14  dholland 		qk.qk_id = defaultq ? QUOTA_DEFAULTID : id;
    406  1.14  dholland 		qk.qk_objtype = QUOTA_OBJTYPE_BLOCKS;
    407  1.14  dholland 
    408  1.15  dholland 		args.qc_type = QCT_PUT;
    409  1.15  dholland 		args.u.put.qc_key = &qk;
    410  1.15  dholland 		args.u.put.qc_val = &blocks;
    411  1.15  dholland 		error = VFS_QUOTACTL(mp, QUOTACTL_PUT, &args);
    412  1.13  dholland 		if (error) {
    413  1.13  dholland 			goto err;
    414  1.13  dholland 		}
    415  1.13  dholland 
    416  1.14  dholland 		qk.qk_idtype = q2type;
    417  1.14  dholland 		qk.qk_id = defaultq ? QUOTA_DEFAULTID : id;
    418  1.14  dholland 		qk.qk_objtype = QUOTA_OBJTYPE_FILES;
    419  1.14  dholland 
    420  1.15  dholland 		args.qc_type = QCT_PUT;
    421  1.15  dholland 		args.u.put.qc_key = &qk;
    422  1.15  dholland 		args.u.put.qc_val = &files;
    423  1.15  dholland 		error = VFS_QUOTACTL(mp, QUOTACTL_PUT, &args);
    424  1.11  dholland 		if (error) {
    425  1.11  dholland 			goto err;
    426  1.11  dholland 		}
    427  1.11  dholland 	}
    428  1.11  dholland 	prop_object_iterator_release(iter);
    429  1.11  dholland 	if (!prop_dictionary_set_and_rel(cmddict, "data", replies)) {
    430  1.11  dholland 		error = ENOMEM;
    431  1.11  dholland 	} else {
    432  1.11  dholland 		error = 0;
    433  1.11  dholland 	}
    434  1.11  dholland 	return error;
    435  1.11  dholland err:
    436  1.11  dholland 	prop_object_iterator_release(iter);
    437  1.11  dholland 	prop_object_release(replies);
    438  1.11  dholland 	return error;
    439   1.5  dholland }
    440   1.5  dholland 
    441  1.23  dholland static prop_dictionary_t
    442  1.23  dholland vfs_quotactl_getall_makereply(id_t id, int def,
    443  1.23  dholland 			      const struct quotaval *blocks,
    444  1.23  dholland 			      const struct quotaval *files)
    445  1.23  dholland {
    446  1.23  dholland #define INITQVNAMES_ALL { \
    447  1.23  dholland     QUOTADICT_LIMIT_HARD, \
    448  1.23  dholland     QUOTADICT_LIMIT_SOFT, \
    449  1.23  dholland     QUOTADICT_LIMIT_USAGE, \
    450  1.23  dholland     QUOTADICT_LIMIT_ETIME, \
    451  1.23  dholland     QUOTADICT_LIMIT_GTIME \
    452  1.23  dholland     }
    453  1.23  dholland #define N_QV 5
    454  1.23  dholland 
    455  1.23  dholland 	const char *val_names[] = INITQVNAMES_ALL;
    456  1.23  dholland 	uint64_t vals[N_QV];
    457  1.23  dholland 	prop_dictionary_t dict1 = prop_dictionary_create();
    458  1.23  dholland 	prop_dictionary_t dict2;
    459  1.23  dholland 
    460  1.23  dholland 	if (dict1 == NULL)
    461  1.23  dholland 		return NULL;
    462  1.23  dholland 
    463  1.23  dholland 	if (def) {
    464  1.23  dholland 		if (!prop_dictionary_set_cstring_nocopy(dict1, "id",
    465  1.23  dholland 		    "default")) {
    466  1.23  dholland 			goto err;
    467  1.23  dholland 		}
    468  1.23  dholland 	} else {
    469  1.23  dholland 		if (!prop_dictionary_set_uint32(dict1, "id", id)) {
    470  1.23  dholland 			goto err;
    471  1.23  dholland 		}
    472  1.23  dholland 	}
    473  1.23  dholland 
    474  1.23  dholland 	vals[0] = blocks->qv_hardlimit;
    475  1.23  dholland 	vals[1] = blocks->qv_softlimit;
    476  1.23  dholland 	vals[2] = blocks->qv_usage;
    477  1.23  dholland 	vals[3] = blocks->qv_expiretime;
    478  1.23  dholland 	vals[4] = blocks->qv_grace;
    479  1.23  dholland 	dict2 = limits64toprop(vals, val_names, N_QV);
    480  1.23  dholland 	if (dict2 == NULL)
    481  1.23  dholland 		goto err;
    482  1.23  dholland 	if (!prop_dictionary_set_and_rel(dict1, QUOTADICT_LTYPE_BLOCK, dict2))
    483  1.23  dholland 		goto err;
    484  1.23  dholland 
    485  1.23  dholland 
    486  1.23  dholland 	vals[0] = files->qv_hardlimit;
    487  1.23  dholland 	vals[1] = files->qv_softlimit;
    488  1.23  dholland 	vals[2] = files->qv_usage;
    489  1.23  dholland 	vals[3] = files->qv_expiretime;
    490  1.23  dholland 	vals[4] = files->qv_grace;
    491  1.23  dholland 	dict2 = limits64toprop(vals, val_names, N_QV);
    492  1.23  dholland 	if (dict2 == NULL)
    493  1.23  dholland 		goto err;
    494  1.23  dholland 	if (!prop_dictionary_set_and_rel(dict1, QUOTADICT_LTYPE_FILE, dict2))
    495  1.23  dholland 		goto err;
    496  1.23  dholland 
    497  1.23  dholland 	return dict1;
    498  1.23  dholland 
    499  1.23  dholland err:
    500  1.23  dholland 	prop_object_release(dict1);
    501  1.23  dholland 	return NULL;
    502  1.23  dholland }
    503  1.23  dholland 
    504   1.5  dholland static int
    505   1.5  dholland vfs_quotactl_getall(struct mount *mp,
    506   1.5  dholland 			prop_dictionary_t cmddict, int q2type,
    507   1.5  dholland 			prop_array_t datas)
    508   1.5  dholland {
    509  1.21  dholland 	struct quotakcursor cursor;
    510  1.23  dholland 	struct quota_getall_result result;
    511   1.5  dholland 	struct vfs_quotactl_args args;
    512  1.23  dholland 	prop_array_t replies;
    513  1.23  dholland 	prop_dictionary_t dict;
    514  1.23  dholland 	unsigned i;
    515  1.21  dholland 	int error, error2;
    516  1.23  dholland 	int skip = 0;
    517  1.23  dholland 
    518  1.23  dholland 	KASSERT(prop_object_type(cmddict) == PROP_TYPE_DICTIONARY);
    519  1.21  dholland 
    520  1.21  dholland 	args.qc_type = QCT_CURSOROPEN;
    521  1.21  dholland 	args.u.cursoropen.qc_cursor = &cursor;
    522  1.21  dholland 	error = VFS_QUOTACTL(mp, QUOTACTL_CURSOROPEN, &args);
    523  1.21  dholland 	if (error) {
    524  1.21  dholland 		return error;
    525  1.21  dholland 	}
    526   1.5  dholland 
    527  1.23  dholland 	result.qr_keys = NULL;
    528  1.23  dholland 	result.qr_vals = NULL;
    529  1.23  dholland 
    530  1.22  dholland 	args.qc_type = QCT_GETALL;
    531  1.22  dholland 	args.u.getall.qc_cursor = &cursor;
    532  1.22  dholland 	args.u.getall.qc_idtype = q2type;
    533  1.23  dholland 	args.u.getall.qc_result = &result;
    534  1.21  dholland 	error = VFS_QUOTACTL(mp, QUOTACTL_GETALL, &args);
    535  1.23  dholland 	/*
    536  1.23  dholland 	 * XXX this is bogus but up until now *all* errors
    537  1.23  dholland 	 * from inside quotactl_getall were suppressed by the
    538  1.23  dholland 	 * dispatching code in ufs_quota.c. Fixing that causes
    539  1.23  dholland 	 * repquota to break in an undesirable way; this is a
    540  1.23  dholland 	 * workaround.
    541  1.23  dholland 	 */
    542  1.23  dholland 	if (error == ENODEV || error == ENXIO) {
    543  1.23  dholland 		skip = 1;
    544  1.23  dholland 		error = 0;
    545  1.23  dholland 	}
    546  1.23  dholland 	if (error) {
    547  1.23  dholland 		goto err;
    548  1.23  dholland 	}
    549  1.23  dholland 
    550  1.23  dholland 	replies = prop_array_create();
    551  1.23  dholland 	if (replies == NULL) {
    552  1.23  dholland 		error = ENOMEM;
    553  1.23  dholland 		goto err;
    554  1.23  dholland 	}
    555  1.23  dholland 
    556  1.23  dholland 	if (skip) {
    557  1.23  dholland 		goto skip;
    558  1.23  dholland 	}
    559  1.23  dholland 
    560  1.23  dholland 	dict = vfs_quotactl_getall_makereply(0, 1, &result.qr_defblocks,
    561  1.23  dholland 					     &result.qr_deffiles);
    562  1.23  dholland 	if (!prop_array_add_and_rel(replies, dict)) {
    563  1.23  dholland 		error = ENOMEM;
    564  1.23  dholland 		goto err;
    565  1.23  dholland 	}
    566  1.23  dholland 
    567  1.23  dholland 	for (i = 0; i < result.qr_num; i += 2) {
    568  1.23  dholland 		dict = vfs_quotactl_getall_makereply(result.qr_keys[i].qk_id,0,
    569  1.23  dholland 						     &result.qr_vals[i],
    570  1.23  dholland 						     &result.qr_vals[i+1]);
    571  1.23  dholland 		if (dict == NULL) {
    572  1.23  dholland 			error = ENOMEM;
    573  1.23  dholland 			goto err;
    574  1.23  dholland 		}
    575  1.23  dholland 		if (!prop_array_add_and_rel(replies, dict)) {
    576  1.23  dholland 			error = ENOMEM;
    577  1.23  dholland 			goto err;
    578  1.23  dholland 		}
    579  1.23  dholland 	}
    580  1.23  dholland 
    581  1.23  dholland skip:
    582  1.23  dholland 
    583  1.23  dholland 	if (!prop_dictionary_set_and_rel(cmddict, "data", replies)) {
    584  1.23  dholland 		error = ENOMEM;
    585  1.23  dholland 		goto err;
    586  1.23  dholland 	}
    587  1.23  dholland 
    588  1.23  dholland 	error = 0;
    589  1.23  dholland  err:
    590  1.23  dholland 	if (result.qr_keys) {
    591  1.23  dholland 		free(result.qr_keys, M_TEMP);
    592  1.23  dholland 	}
    593  1.23  dholland 	if (result.qr_vals) {
    594  1.23  dholland 		free(result.qr_vals, M_TEMP);
    595  1.23  dholland 	}
    596  1.21  dholland 
    597  1.21  dholland 	args.qc_type = QCT_CURSORCLOSE;
    598  1.21  dholland 	args.u.cursorclose.qc_cursor = &cursor;
    599  1.21  dholland 	error2 = VFS_QUOTACTL(mp, QUOTACTL_CURSORCLOSE, &args);
    600  1.21  dholland 
    601  1.21  dholland 	if (error) {
    602  1.21  dholland 		return error;
    603  1.21  dholland 	}
    604  1.21  dholland 	error = error2;
    605  1.21  dholland 	return error;
    606   1.5  dholland }
    607   1.5  dholland 
    608   1.5  dholland static int
    609   1.5  dholland vfs_quotactl_clear(struct mount *mp,
    610   1.5  dholland 			prop_dictionary_t cmddict, int q2type,
    611   1.5  dholland 			prop_array_t datas)
    612   1.5  dholland {
    613  1.16  dholland 	prop_array_t replies;
    614  1.16  dholland 	prop_object_iterator_t iter;
    615  1.16  dholland 	prop_dictionary_t data;
    616  1.16  dholland 	uint32_t id;
    617  1.16  dholland 	int defaultq;
    618  1.16  dholland 	const char *idstr;
    619  1.20  dholland 	struct quotakey qk;
    620   1.5  dholland 	struct vfs_quotactl_args args;
    621  1.16  dholland 	int error;
    622  1.16  dholland 
    623  1.16  dholland 	KASSERT(prop_object_type(cmddict) == PROP_TYPE_DICTIONARY);
    624  1.16  dholland 	KASSERT(prop_object_type(datas) == PROP_TYPE_ARRAY);
    625  1.16  dholland 
    626  1.16  dholland 	replies = prop_array_create();
    627  1.16  dholland 	if (replies == NULL)
    628  1.16  dholland 		return ENOMEM;
    629   1.5  dholland 
    630  1.16  dholland 	iter = prop_array_iterator(datas);
    631  1.16  dholland 	if (iter == NULL) {
    632  1.16  dholland 		prop_object_release(replies);
    633  1.16  dholland 		return ENOMEM;
    634  1.16  dholland 	}
    635  1.16  dholland 
    636  1.16  dholland 	while ((data = prop_object_iterator_next(iter)) != NULL) {
    637  1.16  dholland 		if (!prop_dictionary_get_uint32(data, "id", &id)) {
    638  1.16  dholland 			if (!prop_dictionary_get_cstring_nocopy(data, "id",
    639  1.16  dholland 			    &idstr))
    640  1.16  dholland 				continue;
    641  1.16  dholland 			if (strcmp(idstr, "default"))
    642  1.16  dholland 				continue;
    643  1.16  dholland 			id = 0;
    644  1.16  dholland 			defaultq = 1;
    645  1.16  dholland 		} else {
    646  1.16  dholland 			defaultq = 0;
    647  1.16  dholland 		}
    648  1.16  dholland 
    649  1.20  dholland 		qk.qk_idtype = q2type;
    650  1.20  dholland 		qk.qk_id = defaultq ? QUOTA_DEFAULTID : id;
    651  1.20  dholland 		qk.qk_objtype = QUOTA_OBJTYPE_BLOCKS;
    652  1.20  dholland 
    653  1.19  dholland 		args.qc_type = QCT_DELETE;
    654  1.20  dholland 		args.u.delete.qc_key = &qk;
    655  1.19  dholland 		error = VFS_QUOTACTL(mp, QUOTACTL_DELETE, &args);
    656  1.18  dholland 		if (error) {
    657  1.18  dholland 			goto err;
    658  1.18  dholland 		}
    659  1.18  dholland 
    660  1.20  dholland 		qk.qk_idtype = q2type;
    661  1.20  dholland 		qk.qk_id = defaultq ? QUOTA_DEFAULTID : id;
    662  1.20  dholland 		qk.qk_objtype = QUOTA_OBJTYPE_FILES;
    663  1.20  dholland 
    664  1.19  dholland 		args.qc_type = QCT_DELETE;
    665  1.20  dholland 		args.u.delete.qc_key = &qk;
    666  1.19  dholland 		error = VFS_QUOTACTL(mp, QUOTACTL_DELETE, &args);
    667  1.16  dholland 		if (error) {
    668  1.16  dholland 			goto err;
    669  1.16  dholland 		}
    670  1.16  dholland 	}
    671  1.16  dholland 
    672  1.16  dholland 	prop_object_iterator_release(iter);
    673  1.16  dholland 	if (!prop_dictionary_set_and_rel(cmddict, "data", replies)) {
    674  1.16  dholland 		error = ENOMEM;
    675  1.16  dholland 	} else {
    676  1.16  dholland 		error = 0;
    677  1.16  dholland 	}
    678  1.16  dholland 	return error;
    679  1.16  dholland err:
    680  1.16  dholland 	prop_object_iterator_release(iter);
    681  1.16  dholland 	prop_object_release(replies);
    682  1.16  dholland 	return error;
    683   1.5  dholland }
    684   1.5  dholland 
    685   1.5  dholland static int
    686   1.4  dholland vfs_quotactl_cmd(struct mount *mp, prop_dictionary_t cmddict)
    687   1.4  dholland {
    688   1.4  dholland 	int error;
    689   1.4  dholland 	const char *cmd, *type;
    690   1.4  dholland 	prop_array_t datas;
    691   1.4  dholland 	int q2type;
    692   1.4  dholland 
    693   1.4  dholland 	if (!prop_dictionary_get_cstring_nocopy(cmddict, "command", &cmd))
    694   1.4  dholland 		return EINVAL;
    695   1.4  dholland 	if (!prop_dictionary_get_cstring_nocopy(cmddict, "type", &type))
    696   1.4  dholland 		return EINVAL;
    697   1.4  dholland 
    698   1.4  dholland 	if (!strcmp(type, QUOTADICT_CLASS_USER)) {
    699   1.4  dholland 		q2type = QUOTA_CLASS_USER;
    700   1.4  dholland 	} else if (!strcmp(type, QUOTADICT_CLASS_GROUP)) {
    701   1.4  dholland 		q2type = QUOTA_CLASS_GROUP;
    702   1.4  dholland 	} else {
    703   1.4  dholland 		/* XXX this is a bad errno for this case */
    704   1.4  dholland 		return EOPNOTSUPP;
    705   1.4  dholland 	}
    706   1.4  dholland 
    707   1.4  dholland 	datas = prop_dictionary_get(cmddict, "data");
    708   1.4  dholland 	if (datas == NULL || prop_object_type(datas) != PROP_TYPE_ARRAY)
    709   1.4  dholland 		return EINVAL;
    710   1.4  dholland 
    711   1.4  dholland 	prop_object_retain(datas);
    712   1.4  dholland 	prop_dictionary_remove(cmddict, "data"); /* prepare for return */
    713   1.4  dholland 
    714   1.4  dholland 	if (strcmp(cmd, "get version") == 0) {
    715   1.5  dholland 		error = vfs_quotactl_getversion(mp, cmddict, q2type, datas);
    716   1.4  dholland 	} else if (strcmp(cmd, "quotaon") == 0) {
    717   1.5  dholland 		error = vfs_quotactl_quotaon(mp, cmddict, q2type, datas);
    718   1.4  dholland 	} else if (strcmp(cmd, "quotaoff") == 0) {
    719   1.5  dholland 		error = vfs_quotactl_quotaoff(mp, cmddict, q2type, datas);
    720   1.4  dholland 	} else if (strcmp(cmd, "get") == 0) {
    721   1.5  dholland 		error = vfs_quotactl_get(mp, cmddict, q2type, datas);
    722   1.4  dholland 	} else if (strcmp(cmd, "set") == 0) {
    723  1.15  dholland 		error = vfs_quotactl_put(mp, cmddict, q2type, datas);
    724   1.4  dholland 	} else if (strcmp(cmd, "getall") == 0) {
    725   1.5  dholland 		error = vfs_quotactl_getall(mp, cmddict, q2type, datas);
    726   1.4  dholland 	} else if (strcmp(cmd, "clear") == 0) {
    727   1.5  dholland 		error = vfs_quotactl_clear(mp, cmddict, q2type, datas);
    728   1.4  dholland 	} else {
    729   1.4  dholland 		/* XXX this a bad errno for this case */
    730   1.4  dholland 		error = EOPNOTSUPP;
    731   1.4  dholland 	}
    732   1.4  dholland 
    733   1.4  dholland 	error = (prop_dictionary_set_int8(cmddict, "return",
    734   1.4  dholland 	    error) ? 0 : ENOMEM);
    735   1.4  dholland 	prop_object_release(datas);
    736   1.4  dholland 
    737   1.4  dholland 	return error;
    738   1.4  dholland }
    739   1.4  dholland 
    740   1.2  dholland int
    741   1.2  dholland vfs_quotactl(struct mount *mp, prop_dictionary_t dict)
    742   1.2  dholland {
    743   1.3  dholland 	prop_dictionary_t cmddict;
    744   1.3  dholland 	prop_array_t commands;
    745   1.3  dholland 	prop_object_iterator_t iter;
    746   1.3  dholland 	int error;
    747   1.3  dholland 
    748   1.3  dholland 	error = quota_get_cmds(dict, &commands);
    749   1.3  dholland 	if (error) {
    750   1.3  dholland 		return error;
    751   1.3  dholland 	}
    752   1.3  dholland 
    753   1.3  dholland 	iter = prop_array_iterator(commands);
    754   1.3  dholland 	if (iter == NULL) {
    755   1.3  dholland 		return ENOMEM;
    756   1.3  dholland 	}
    757   1.3  dholland 
    758   1.3  dholland 	while ((cmddict = prop_object_iterator_next(iter)) != NULL) {
    759   1.3  dholland 		if (prop_object_type(cmddict) != PROP_TYPE_DICTIONARY) {
    760   1.3  dholland 			/* XXX shouldn't this be an error? */
    761   1.3  dholland 			continue;
    762   1.3  dholland 		}
    763   1.4  dholland 		error = vfs_quotactl_cmd(mp, cmddict);
    764   1.3  dholland 		if (error) {
    765   1.3  dholland 			break;
    766   1.3  dholland 		}
    767   1.3  dholland 	}
    768   1.3  dholland 	prop_object_iterator_release(iter);
    769   1.3  dholland 	return error;
    770   1.2  dholland }
    771