vfs_quotactl.c revision 1.7 1 1.7 dholland /* $NetBSD: vfs_quotactl.c,v 1.7 2012/01/29 06:37:30 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.7 dholland __KERNEL_RCSID(0, "$NetBSD: vfs_quotactl.c,v 1.7 2012/01/29 06:37:30 dholland Exp $");
84 1.1 dholland
85 1.2 dholland #include <sys/mount.h>
86 1.4 dholland #include <sys/quotactl.h>
87 1.3 dholland #include <quota/quotaprop.h>
88 1.2 dholland
89 1.4 dholland static int
90 1.5 dholland vfs_quotactl_getversion(struct mount *mp,
91 1.5 dholland prop_dictionary_t cmddict, int q2type,
92 1.5 dholland prop_array_t datas)
93 1.5 dholland {
94 1.6 dholland prop_array_t replies;
95 1.6 dholland prop_dictionary_t data;
96 1.6 dholland int q2version;
97 1.5 dholland struct vfs_quotactl_args args;
98 1.6 dholland int error;
99 1.6 dholland
100 1.6 dholland KASSERT(prop_object_type(cmddict) == PROP_TYPE_DICTIONARY);
101 1.6 dholland KASSERT(prop_object_type(datas) == PROP_TYPE_ARRAY);
102 1.6 dholland
103 1.6 dholland args.qc_type = QCT_GETVERSION;
104 1.6 dholland args.u.getversion.qc_version_ret = &q2version;
105 1.6 dholland error = VFS_QUOTACTL(mp, QUOTACTL_GETVERSION, &args);
106 1.6 dholland if (error) {
107 1.6 dholland return error;
108 1.6 dholland }
109 1.6 dholland
110 1.6 dholland data = prop_dictionary_create();
111 1.6 dholland if (data == NULL) {
112 1.6 dholland return ENOMEM;
113 1.6 dholland }
114 1.6 dholland
115 1.6 dholland if (!prop_dictionary_set_int8(data, "version", q2version)) {
116 1.6 dholland prop_object_release(data);
117 1.6 dholland return ENOMEM;
118 1.6 dholland }
119 1.6 dholland
120 1.6 dholland replies = prop_array_create();
121 1.6 dholland if (replies == NULL) {
122 1.6 dholland prop_object_release(data);
123 1.6 dholland return ENOMEM;
124 1.6 dholland }
125 1.5 dholland
126 1.6 dholland if (!prop_array_add_and_rel(replies, data)) {
127 1.6 dholland prop_object_release(data);
128 1.6 dholland prop_object_release(replies);
129 1.6 dholland return ENOMEM;
130 1.6 dholland }
131 1.6 dholland
132 1.6 dholland if (!prop_dictionary_set_and_rel(cmddict, "data", replies)) {
133 1.6 dholland prop_object_release(replies);
134 1.6 dholland return ENOMEM;
135 1.6 dholland }
136 1.6 dholland
137 1.6 dholland return error;
138 1.5 dholland }
139 1.5 dholland
140 1.5 dholland static int
141 1.5 dholland vfs_quotactl_quotaon(struct mount *mp,
142 1.5 dholland prop_dictionary_t cmddict, int q2type,
143 1.5 dholland prop_array_t datas)
144 1.5 dholland {
145 1.5 dholland struct vfs_quotactl_args args;
146 1.5 dholland
147 1.5 dholland args.qc_type = QCT_PROPLIB;
148 1.5 dholland args.u.proplib.qc_cmddict = cmddict;
149 1.5 dholland args.u.proplib.qc_q2type = q2type;
150 1.5 dholland args.u.proplib.qc_datas = datas;
151 1.5 dholland return VFS_QUOTACTL(mp, QUOTACTL_QUOTAON, &args);
152 1.5 dholland }
153 1.5 dholland
154 1.5 dholland static int
155 1.5 dholland vfs_quotactl_quotaoff(struct mount *mp,
156 1.5 dholland prop_dictionary_t cmddict, int q2type,
157 1.5 dholland prop_array_t datas)
158 1.5 dholland {
159 1.5 dholland struct vfs_quotactl_args args;
160 1.5 dholland
161 1.5 dholland args.qc_type = QCT_PROPLIB;
162 1.5 dholland args.u.proplib.qc_cmddict = cmddict;
163 1.5 dholland args.u.proplib.qc_q2type = q2type;
164 1.5 dholland args.u.proplib.qc_datas = datas;
165 1.5 dholland return VFS_QUOTACTL(mp, QUOTACTL_QUOTAOFF, &args);
166 1.5 dholland }
167 1.5 dholland
168 1.5 dholland static int
169 1.5 dholland vfs_quotactl_get(struct mount *mp,
170 1.5 dholland prop_dictionary_t cmddict, int q2type,
171 1.5 dholland prop_array_t datas)
172 1.5 dholland {
173 1.7 dholland prop_object_iterator_t iter;
174 1.7 dholland prop_dictionary_t data;
175 1.7 dholland uint32_t id;
176 1.7 dholland int defaultq;
177 1.7 dholland const char *idstr;
178 1.7 dholland prop_array_t replies;
179 1.5 dholland struct vfs_quotactl_args args;
180 1.7 dholland int error;
181 1.7 dholland
182 1.7 dholland KASSERT(prop_object_type(cmddict) == PROP_TYPE_DICTIONARY);
183 1.7 dholland KASSERT(prop_object_type(datas) == PROP_TYPE_ARRAY);
184 1.7 dholland
185 1.7 dholland replies = prop_array_create();
186 1.7 dholland if (replies == NULL) {
187 1.7 dholland return ENOMEM;
188 1.7 dholland }
189 1.7 dholland
190 1.7 dholland iter = prop_array_iterator(datas);
191 1.7 dholland if (iter == NULL) {
192 1.7 dholland prop_object_release(replies);
193 1.7 dholland return ENOMEM;
194 1.7 dholland }
195 1.7 dholland
196 1.7 dholland while ((data = prop_object_iterator_next(iter)) != NULL) {
197 1.7 dholland if (!prop_dictionary_get_uint32(data, "id", &id)) {
198 1.7 dholland if (!prop_dictionary_get_cstring_nocopy(data, "id",
199 1.7 dholland &idstr))
200 1.7 dholland continue;
201 1.7 dholland if (strcmp(idstr, "default")) {
202 1.7 dholland error = EINVAL;
203 1.7 dholland goto fail;
204 1.7 dholland }
205 1.7 dholland id = 0;
206 1.7 dholland defaultq = 1;
207 1.7 dholland } else {
208 1.7 dholland defaultq = 0;
209 1.7 dholland }
210 1.5 dholland
211 1.7 dholland args.qc_type = QCT_GET;
212 1.7 dholland args.u.get.qc_q2type = q2type;
213 1.7 dholland args.u.get.qc_id = id;
214 1.7 dholland args.u.get.qc_defaultq = defaultq;
215 1.7 dholland args.u.get.qc_replies = replies;
216 1.7 dholland error = VFS_QUOTACTL(mp, QUOTACTL_GET, &args);
217 1.7 dholland if (error == EPERM) {
218 1.7 dholland /* XXX does this make sense? */
219 1.7 dholland continue;
220 1.7 dholland } else if (error == ENOENT) {
221 1.7 dholland /* XXX does *this* make sense? */
222 1.7 dholland continue;
223 1.7 dholland } else if (error) {
224 1.7 dholland goto fail;
225 1.7 dholland }
226 1.7 dholland }
227 1.7 dholland
228 1.7 dholland prop_object_iterator_release(iter);
229 1.7 dholland if (!prop_dictionary_set_and_rel(cmddict, "data", replies)) {
230 1.7 dholland error = ENOMEM;
231 1.7 dholland } else {
232 1.7 dholland error = 0;
233 1.7 dholland }
234 1.7 dholland
235 1.7 dholland return error;
236 1.7 dholland
237 1.7 dholland fail:
238 1.7 dholland prop_object_iterator_release(iter);
239 1.7 dholland prop_object_release(replies);
240 1.7 dholland return error;
241 1.5 dholland }
242 1.5 dholland
243 1.5 dholland static int
244 1.5 dholland vfs_quotactl_set(struct mount *mp,
245 1.5 dholland prop_dictionary_t cmddict, int q2type,
246 1.5 dholland prop_array_t datas)
247 1.5 dholland {
248 1.5 dholland struct vfs_quotactl_args args;
249 1.5 dholland
250 1.5 dholland args.qc_type = QCT_PROPLIB;
251 1.5 dholland args.u.proplib.qc_cmddict = cmddict;
252 1.5 dholland args.u.proplib.qc_q2type = q2type;
253 1.5 dholland args.u.proplib.qc_datas = datas;
254 1.5 dholland return VFS_QUOTACTL(mp, QUOTACTL_SET, &args);
255 1.5 dholland }
256 1.5 dholland
257 1.5 dholland static int
258 1.5 dholland vfs_quotactl_getall(struct mount *mp,
259 1.5 dholland prop_dictionary_t cmddict, int q2type,
260 1.5 dholland prop_array_t datas)
261 1.5 dholland {
262 1.5 dholland struct vfs_quotactl_args args;
263 1.5 dholland
264 1.5 dholland args.qc_type = QCT_PROPLIB;
265 1.5 dholland args.u.proplib.qc_cmddict = cmddict;
266 1.5 dholland args.u.proplib.qc_q2type = q2type;
267 1.5 dholland args.u.proplib.qc_datas = datas;
268 1.5 dholland return VFS_QUOTACTL(mp, QUOTACTL_GETALL, &args);
269 1.5 dholland }
270 1.5 dholland
271 1.5 dholland static int
272 1.5 dholland vfs_quotactl_clear(struct mount *mp,
273 1.5 dholland prop_dictionary_t cmddict, int q2type,
274 1.5 dholland prop_array_t datas)
275 1.5 dholland {
276 1.5 dholland struct vfs_quotactl_args args;
277 1.5 dholland
278 1.5 dholland args.qc_type = QCT_PROPLIB;
279 1.5 dholland args.u.proplib.qc_cmddict = cmddict;
280 1.5 dholland args.u.proplib.qc_q2type = q2type;
281 1.5 dholland args.u.proplib.qc_datas = datas;
282 1.5 dholland return VFS_QUOTACTL(mp, QUOTACTL_CLEAR, &args);
283 1.5 dholland }
284 1.5 dholland
285 1.5 dholland static int
286 1.4 dholland vfs_quotactl_cmd(struct mount *mp, prop_dictionary_t cmddict)
287 1.4 dholland {
288 1.4 dholland int error;
289 1.4 dholland const char *cmd, *type;
290 1.4 dholland prop_array_t datas;
291 1.4 dholland int q2type;
292 1.4 dholland
293 1.4 dholland if (!prop_dictionary_get_cstring_nocopy(cmddict, "command", &cmd))
294 1.4 dholland return EINVAL;
295 1.4 dholland if (!prop_dictionary_get_cstring_nocopy(cmddict, "type", &type))
296 1.4 dholland return EINVAL;
297 1.4 dholland
298 1.4 dholland if (!strcmp(type, QUOTADICT_CLASS_USER)) {
299 1.4 dholland q2type = QUOTA_CLASS_USER;
300 1.4 dholland } else if (!strcmp(type, QUOTADICT_CLASS_GROUP)) {
301 1.4 dholland q2type = QUOTA_CLASS_GROUP;
302 1.4 dholland } else {
303 1.4 dholland /* XXX this is a bad errno for this case */
304 1.4 dholland return EOPNOTSUPP;
305 1.4 dholland }
306 1.4 dholland
307 1.4 dholland datas = prop_dictionary_get(cmddict, "data");
308 1.4 dholland if (datas == NULL || prop_object_type(datas) != PROP_TYPE_ARRAY)
309 1.4 dholland return EINVAL;
310 1.4 dholland
311 1.4 dholland prop_object_retain(datas);
312 1.4 dholland prop_dictionary_remove(cmddict, "data"); /* prepare for return */
313 1.4 dholland
314 1.4 dholland if (strcmp(cmd, "get version") == 0) {
315 1.5 dholland error = vfs_quotactl_getversion(mp, cmddict, q2type, datas);
316 1.4 dholland } else if (strcmp(cmd, "quotaon") == 0) {
317 1.5 dholland error = vfs_quotactl_quotaon(mp, cmddict, q2type, datas);
318 1.4 dholland } else if (strcmp(cmd, "quotaoff") == 0) {
319 1.5 dholland error = vfs_quotactl_quotaoff(mp, cmddict, q2type, datas);
320 1.4 dholland } else if (strcmp(cmd, "get") == 0) {
321 1.5 dholland error = vfs_quotactl_get(mp, cmddict, q2type, datas);
322 1.4 dholland } else if (strcmp(cmd, "set") == 0) {
323 1.5 dholland error = vfs_quotactl_set(mp, cmddict, q2type, datas);
324 1.4 dholland } else if (strcmp(cmd, "getall") == 0) {
325 1.5 dholland error = vfs_quotactl_getall(mp, cmddict, q2type, datas);
326 1.4 dholland } else if (strcmp(cmd, "clear") == 0) {
327 1.5 dholland error = vfs_quotactl_clear(mp, cmddict, q2type, datas);
328 1.4 dholland } else {
329 1.4 dholland /* XXX this a bad errno for this case */
330 1.4 dholland error = EOPNOTSUPP;
331 1.4 dholland }
332 1.4 dholland
333 1.4 dholland error = (prop_dictionary_set_int8(cmddict, "return",
334 1.4 dholland error) ? 0 : ENOMEM);
335 1.4 dholland prop_object_release(datas);
336 1.4 dholland
337 1.4 dholland return error;
338 1.4 dholland }
339 1.4 dholland
340 1.2 dholland int
341 1.2 dholland vfs_quotactl(struct mount *mp, prop_dictionary_t dict)
342 1.2 dholland {
343 1.3 dholland prop_dictionary_t cmddict;
344 1.3 dholland prop_array_t commands;
345 1.3 dholland prop_object_iterator_t iter;
346 1.3 dholland int error;
347 1.3 dholland
348 1.3 dholland error = quota_get_cmds(dict, &commands);
349 1.3 dholland if (error) {
350 1.3 dholland return error;
351 1.3 dholland }
352 1.3 dholland
353 1.3 dholland iter = prop_array_iterator(commands);
354 1.3 dholland if (iter == NULL) {
355 1.3 dholland return ENOMEM;
356 1.3 dholland }
357 1.3 dholland
358 1.3 dholland while ((cmddict = prop_object_iterator_next(iter)) != NULL) {
359 1.3 dholland if (prop_object_type(cmddict) != PROP_TYPE_DICTIONARY) {
360 1.3 dholland /* XXX shouldn't this be an error? */
361 1.3 dholland continue;
362 1.3 dholland }
363 1.4 dholland error = vfs_quotactl_cmd(mp, cmddict);
364 1.3 dholland if (error) {
365 1.3 dholland break;
366 1.3 dholland }
367 1.3 dholland }
368 1.3 dholland prop_object_iterator_release(iter);
369 1.3 dholland return error;
370 1.2 dholland }
371