Home | History | Annotate | Line # | Download | only in kern
vfs_quotactl.c revision 1.9
      1 /*	$NetBSD: vfs_quotactl.c,v 1.9 2012/01/29 06:40:57 dholland Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1991, 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  * (c) UNIX System Laboratories, Inc.
      7  * All or some portions of this file are derived from material licensed
      8  * to the University of California by American Telephone and Telegraph
      9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10  * the permission of UNIX System Laboratories, Inc.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  *	@(#)ufs_vfsops.c	8.8 (Berkeley) 5/20/95
     37  *	From NetBSD: ufs_vfsops.c,v 1.42 2011/03/24 17:05:46 bouyer Exp
     38  */
     39 
     40 /*
     41  * Copyright (c) 1982, 1986, 1990, 1993, 1995
     42  *	The Regents of the University of California.  All rights reserved.
     43  *
     44  * This code is derived from software contributed to Berkeley by
     45  * Robert Elz at The University of Melbourne.
     46  *
     47  * Redistribution and use in source and binary forms, with or without
     48  * modification, are permitted provided that the following conditions
     49  * are met:
     50  * 1. Redistributions of source code must retain the above copyright
     51  *    notice, this list of conditions and the following disclaimer.
     52  * 2. Redistributions in binary form must reproduce the above copyright
     53  *    notice, this list of conditions and the following disclaimer in the
     54  *    documentation and/or other materials provided with the distribution.
     55  * 3. Neither the name of the University nor the names of its contributors
     56  *    may be used to endorse or promote products derived from this software
     57  *    without specific prior written permission.
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69  * SUCH DAMAGE.
     70  *
     71  *	@(#)ufs_quota.c	8.5 (Berkeley) 5/20/95
     72  *	From NetBSD: ufs_quota.c,v 1.70 2011/03/24 17:05:46 bouyer Exp
     73  */
     74 
     75 /*
     76  * Note that both of the copyrights above are moderately spurious;
     77  * this code should almost certainly have the Copyright 2010 Manuel
     78  * Bouyer notice and license found in e.g. sys/ufs/ufs/quota2_subr.c.
     79  * However, they're what was on the files this code was sliced out of.
     80  */
     81 
     82 #include <sys/cdefs.h>
     83 __KERNEL_RCSID(0, "$NetBSD: vfs_quotactl.c,v 1.9 2012/01/29 06:40:57 dholland Exp $");
     84 
     85 #include <sys/mount.h>
     86 #include <sys/quota.h>
     87 #include <sys/quotactl.h>
     88 #include <quota/quotaprop.h>
     89 
     90 static int
     91 vfs_quotactl_getversion(struct mount *mp,
     92 			prop_dictionary_t cmddict, int q2type,
     93 			prop_array_t datas)
     94 {
     95 	prop_array_t replies;
     96 	prop_dictionary_t data;
     97 	int q2version;
     98 	struct vfs_quotactl_args args;
     99 	int error;
    100 
    101 	KASSERT(prop_object_type(cmddict) == PROP_TYPE_DICTIONARY);
    102 	KASSERT(prop_object_type(datas) == PROP_TYPE_ARRAY);
    103 
    104 	args.qc_type = QCT_GETVERSION;
    105 	args.u.getversion.qc_version_ret = &q2version;
    106 	error = VFS_QUOTACTL(mp, QUOTACTL_GETVERSION, &args);
    107 	if (error) {
    108 		return error;
    109 	}
    110 
    111 	data = prop_dictionary_create();
    112 	if (data == NULL) {
    113 		return ENOMEM;
    114 	}
    115 
    116 	if (!prop_dictionary_set_int8(data, "version", q2version)) {
    117 		prop_object_release(data);
    118 		return ENOMEM;
    119 	}
    120 
    121 	replies = prop_array_create();
    122 	if (replies == NULL) {
    123 		prop_object_release(data);
    124 		return ENOMEM;
    125 	}
    126 
    127 	if (!prop_array_add_and_rel(replies, data)) {
    128 		prop_object_release(data);
    129 		prop_object_release(replies);
    130 		return ENOMEM;
    131 	}
    132 
    133 	if (!prop_dictionary_set_and_rel(cmddict, "data", replies)) {
    134 		prop_object_release(replies);
    135 		return ENOMEM;
    136 	}
    137 
    138 	return error;
    139 }
    140 
    141 static int
    142 vfs_quotactl_quotaon(struct mount *mp,
    143 		     prop_dictionary_t cmddict, int q2type,
    144 		     prop_array_t datas)
    145 {
    146 	struct vfs_quotactl_args args;
    147 
    148 	args.qc_type = QCT_PROPLIB;
    149 	args.u.proplib.qc_cmddict = cmddict;
    150 	args.u.proplib.qc_q2type = q2type;
    151 	args.u.proplib.qc_datas = datas;
    152 	return VFS_QUOTACTL(mp, QUOTACTL_QUOTAON, &args);
    153 }
    154 
    155 static int
    156 vfs_quotactl_quotaoff(struct mount *mp,
    157 			prop_dictionary_t cmddict, int q2type,
    158 			prop_array_t datas)
    159 {
    160 	struct vfs_quotactl_args args;
    161 
    162 	args.qc_type = QCT_PROPLIB;
    163 	args.u.proplib.qc_cmddict = cmddict;
    164 	args.u.proplib.qc_q2type = q2type;
    165 	args.u.proplib.qc_datas = datas;
    166 	return VFS_QUOTACTL(mp, QUOTACTL_QUOTAOFF, &args);
    167 }
    168 
    169 static int
    170 vfs_quotactl_get_addreply(id_t id,
    171 			  int defaultq,
    172 			  const struct quotaval *blocks,
    173 			  const struct quotaval *files,
    174 			  prop_array_t replies)
    175 {
    176 	prop_dictionary_t dict;
    177 
    178 	/* XXX illegal casts */
    179 	uint64_t *valuesp[QUOTA_NLIMITS];
    180 	valuesp[QUOTA_LIMIT_BLOCK] = (void *)(intptr_t)&blocks->qv_hardlimit;
    181 	valuesp[QUOTA_LIMIT_FILE] =  (void *)(intptr_t)&files->qv_hardlimit;
    182 
    183 	dict = quota64toprop(id, defaultq, valuesp,
    184 	    ufs_quota_entry_names, UFS_QUOTA_NENTRIES,
    185 	    ufs_quota_limit_names, QUOTA_NLIMITS);
    186 	if (dict == NULL)
    187 		return ENOMEM;
    188 	if (!prop_array_add_and_rel(replies, dict)) {
    189 		prop_object_release(dict);
    190 		return ENOMEM;
    191 	}
    192 
    193 	return 0;
    194 }
    195 
    196 static int
    197 vfs_quotactl_get(struct mount *mp,
    198 			prop_dictionary_t cmddict, int q2type,
    199 			prop_array_t datas)
    200 {
    201 	prop_object_iterator_t iter;
    202 	prop_dictionary_t data;
    203 	prop_array_t replies;
    204 	uint32_t id;
    205 	int defaultq;
    206 	const char *idstr;
    207 	struct vfs_quotactl_args args;
    208 	struct quotaval blocks, files;
    209 	int error;
    210 
    211 	KASSERT(prop_object_type(cmddict) == PROP_TYPE_DICTIONARY);
    212 	KASSERT(prop_object_type(datas) == PROP_TYPE_ARRAY);
    213 
    214 	replies = prop_array_create();
    215 	if (replies == NULL) {
    216 		return ENOMEM;
    217 	}
    218 
    219 	iter = prop_array_iterator(datas);
    220 	if (iter == NULL) {
    221 		prop_object_release(replies);
    222 		return ENOMEM;
    223 	}
    224 
    225 	while ((data = prop_object_iterator_next(iter)) != NULL) {
    226 		if (!prop_dictionary_get_uint32(data, "id", &id)) {
    227 			if (!prop_dictionary_get_cstring_nocopy(data, "id",
    228 			    &idstr))
    229 				continue;
    230 			if (strcmp(idstr, "default")) {
    231 				error = EINVAL;
    232 				goto fail;
    233 			}
    234 			id = 0;
    235 			defaultq = 1;
    236 		} else {
    237 			defaultq = 0;
    238 		}
    239 
    240 		args.qc_type = QCT_GET;
    241 		args.u.get.qc_q2type = q2type;
    242 		args.u.get.qc_id = id;
    243 		args.u.get.qc_defaultq = defaultq;
    244 		args.u.get.qc_objtype = QUOTA_OBJTYPE_BLOCKS;
    245 		args.u.get.qc_ret = &blocks;
    246 		error = VFS_QUOTACTL(mp, QUOTACTL_GET, &args);
    247 		if (error == EPERM) {
    248 			/* XXX does this make sense? */
    249 			continue;
    250 		} else if (error == ENOENT) {
    251 			/* XXX does *this* make sense? */
    252 			continue;
    253 		} else if (error) {
    254 			goto fail;
    255 		}
    256 
    257 		args.qc_type = QCT_GET;
    258 		args.u.get.qc_q2type = q2type;
    259 		args.u.get.qc_id = id;
    260 		args.u.get.qc_defaultq = defaultq;
    261 		args.u.get.qc_objtype = QUOTA_OBJTYPE_FILES;
    262 		args.u.get.qc_ret = &files;
    263 		error = VFS_QUOTACTL(mp, QUOTACTL_GET, &args);
    264 		if (error == EPERM) {
    265 			/* XXX does this make sense? */
    266 			continue;
    267 		} else if (error == ENOENT) {
    268 			/* XXX does *this* make sense? */
    269 			continue;
    270 		} else if (error) {
    271 			goto fail;
    272 		}
    273 
    274 		error = vfs_quotactl_get_addreply(id, defaultq,
    275 						  &blocks, &files,
    276 						  replies);
    277 	}
    278 
    279 	prop_object_iterator_release(iter);
    280 	if (!prop_dictionary_set_and_rel(cmddict, "data", replies)) {
    281 		error = ENOMEM;
    282 	} else {
    283 		error = 0;
    284 	}
    285 
    286 	return error;
    287 
    288  fail:
    289 	prop_object_iterator_release(iter);
    290 	prop_object_release(replies);
    291 	return error;
    292 }
    293 
    294 static int
    295 vfs_quotactl_set(struct mount *mp,
    296 			prop_dictionary_t cmddict, int q2type,
    297 			prop_array_t datas)
    298 {
    299 	struct vfs_quotactl_args args;
    300 
    301 	args.qc_type = QCT_PROPLIB;
    302 	args.u.proplib.qc_cmddict = cmddict;
    303 	args.u.proplib.qc_q2type = q2type;
    304 	args.u.proplib.qc_datas = datas;
    305 	return VFS_QUOTACTL(mp, QUOTACTL_SET, &args);
    306 }
    307 
    308 static int
    309 vfs_quotactl_getall(struct mount *mp,
    310 			prop_dictionary_t cmddict, int q2type,
    311 			prop_array_t datas)
    312 {
    313 	struct vfs_quotactl_args args;
    314 
    315 	args.qc_type = QCT_PROPLIB;
    316 	args.u.proplib.qc_cmddict = cmddict;
    317 	args.u.proplib.qc_q2type = q2type;
    318 	args.u.proplib.qc_datas = datas;
    319 	return VFS_QUOTACTL(mp, QUOTACTL_GETALL, &args);
    320 }
    321 
    322 static int
    323 vfs_quotactl_clear(struct mount *mp,
    324 			prop_dictionary_t cmddict, int q2type,
    325 			prop_array_t datas)
    326 {
    327 	struct vfs_quotactl_args args;
    328 
    329 	args.qc_type = QCT_PROPLIB;
    330 	args.u.proplib.qc_cmddict = cmddict;
    331 	args.u.proplib.qc_q2type = q2type;
    332 	args.u.proplib.qc_datas = datas;
    333 	return VFS_QUOTACTL(mp, QUOTACTL_CLEAR, &args);
    334 }
    335 
    336 static int
    337 vfs_quotactl_cmd(struct mount *mp, prop_dictionary_t cmddict)
    338 {
    339 	int error;
    340 	const char *cmd, *type;
    341 	prop_array_t datas;
    342 	int q2type;
    343 
    344 	if (!prop_dictionary_get_cstring_nocopy(cmddict, "command", &cmd))
    345 		return EINVAL;
    346 	if (!prop_dictionary_get_cstring_nocopy(cmddict, "type", &type))
    347 		return EINVAL;
    348 
    349 	if (!strcmp(type, QUOTADICT_CLASS_USER)) {
    350 		q2type = QUOTA_CLASS_USER;
    351 	} else if (!strcmp(type, QUOTADICT_CLASS_GROUP)) {
    352 		q2type = QUOTA_CLASS_GROUP;
    353 	} else {
    354 		/* XXX this is a bad errno for this case */
    355 		return EOPNOTSUPP;
    356 	}
    357 
    358 	datas = prop_dictionary_get(cmddict, "data");
    359 	if (datas == NULL || prop_object_type(datas) != PROP_TYPE_ARRAY)
    360 		return EINVAL;
    361 
    362 	prop_object_retain(datas);
    363 	prop_dictionary_remove(cmddict, "data"); /* prepare for return */
    364 
    365 	if (strcmp(cmd, "get version") == 0) {
    366 		error = vfs_quotactl_getversion(mp, cmddict, q2type, datas);
    367 	} else if (strcmp(cmd, "quotaon") == 0) {
    368 		error = vfs_quotactl_quotaon(mp, cmddict, q2type, datas);
    369 	} else if (strcmp(cmd, "quotaoff") == 0) {
    370 		error = vfs_quotactl_quotaoff(mp, cmddict, q2type, datas);
    371 	} else if (strcmp(cmd, "get") == 0) {
    372 		error = vfs_quotactl_get(mp, cmddict, q2type, datas);
    373 	} else if (strcmp(cmd, "set") == 0) {
    374 		error = vfs_quotactl_set(mp, cmddict, q2type, datas);
    375 	} else if (strcmp(cmd, "getall") == 0) {
    376 		error = vfs_quotactl_getall(mp, cmddict, q2type, datas);
    377 	} else if (strcmp(cmd, "clear") == 0) {
    378 		error = vfs_quotactl_clear(mp, cmddict, q2type, datas);
    379 	} else {
    380 		/* XXX this a bad errno for this case */
    381 		error = EOPNOTSUPP;
    382 	}
    383 
    384 	error = (prop_dictionary_set_int8(cmddict, "return",
    385 	    error) ? 0 : ENOMEM);
    386 	prop_object_release(datas);
    387 
    388 	return error;
    389 }
    390 
    391 int
    392 vfs_quotactl(struct mount *mp, prop_dictionary_t dict)
    393 {
    394 	prop_dictionary_t cmddict;
    395 	prop_array_t commands;
    396 	prop_object_iterator_t iter;
    397 	int error;
    398 
    399 	error = quota_get_cmds(dict, &commands);
    400 	if (error) {
    401 		return error;
    402 	}
    403 
    404 	iter = prop_array_iterator(commands);
    405 	if (iter == NULL) {
    406 		return ENOMEM;
    407 	}
    408 
    409 	while ((cmddict = prop_object_iterator_next(iter)) != NULL) {
    410 		if (prop_object_type(cmddict) != PROP_TYPE_DICTIONARY) {
    411 			/* XXX shouldn't this be an error? */
    412 			continue;
    413 		}
    414 		error = vfs_quotactl_cmd(mp, cmddict);
    415 		if (error) {
    416 			break;
    417 		}
    418 	}
    419 	prop_object_iterator_release(iter);
    420 	return error;
    421 }
    422