vfs_quotactl.c revision 1.35 1 1.35 dholland /* $NetBSD: vfs_quotactl.c,v 1.35 2012/01/29 07:14:38 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.35 dholland __KERNEL_RCSID(0, "$NetBSD: vfs_quotactl.c,v 1.35 2012/01/29 07:14:38 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.33 dholland struct quotastat stat;
99 1.6 dholland int q2version;
100 1.5 dholland struct vfs_quotactl_args args;
101 1.6 dholland int error;
102 1.6 dholland
103 1.6 dholland KASSERT(prop_object_type(cmddict) == PROP_TYPE_DICTIONARY);
104 1.6 dholland KASSERT(prop_object_type(datas) == PROP_TYPE_ARRAY);
105 1.6 dholland
106 1.34 dholland args.qc_op = QUOTACTL_STAT;
107 1.33 dholland args.u.stat.qc_ret = &stat;
108 1.35 dholland error = VFS_QUOTACTL(mp, &args);
109 1.6 dholland if (error) {
110 1.6 dholland return error;
111 1.6 dholland }
112 1.6 dholland
113 1.33 dholland /*
114 1.33 dholland * Set q2version based on the stat results. Currently there
115 1.33 dholland * are two valid values for q2version, 1 and 2, which we pick
116 1.33 dholland * based on whether quotacheck is required.
117 1.33 dholland */
118 1.33 dholland if (stat.qs_restrictions & QUOTA_RESTRICT_NEEDSQUOTACHECK) {
119 1.33 dholland q2version = 1;
120 1.33 dholland } else {
121 1.33 dholland q2version = 2;
122 1.33 dholland }
123 1.33 dholland
124 1.6 dholland data = prop_dictionary_create();
125 1.6 dholland if (data == NULL) {
126 1.6 dholland return ENOMEM;
127 1.6 dholland }
128 1.6 dholland
129 1.6 dholland if (!prop_dictionary_set_int8(data, "version", q2version)) {
130 1.6 dholland prop_object_release(data);
131 1.6 dholland return ENOMEM;
132 1.6 dholland }
133 1.6 dholland
134 1.6 dholland replies = prop_array_create();
135 1.6 dholland if (replies == NULL) {
136 1.6 dholland prop_object_release(data);
137 1.6 dholland return ENOMEM;
138 1.6 dholland }
139 1.5 dholland
140 1.6 dholland if (!prop_array_add_and_rel(replies, data)) {
141 1.6 dholland prop_object_release(data);
142 1.6 dholland prop_object_release(replies);
143 1.6 dholland return ENOMEM;
144 1.6 dholland }
145 1.6 dholland
146 1.6 dholland if (!prop_dictionary_set_and_rel(cmddict, "data", replies)) {
147 1.6 dholland prop_object_release(replies);
148 1.6 dholland return ENOMEM;
149 1.6 dholland }
150 1.6 dholland
151 1.6 dholland return error;
152 1.5 dholland }
153 1.5 dholland
154 1.5 dholland static int
155 1.5 dholland vfs_quotactl_quotaon(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.31 dholland prop_dictionary_t data;
160 1.31 dholland const char *qfile;
161 1.5 dholland struct vfs_quotactl_args args;
162 1.5 dholland
163 1.31 dholland KASSERT(prop_object_type(cmddict) == PROP_TYPE_DICTIONARY);
164 1.31 dholland KASSERT(prop_object_type(datas) == PROP_TYPE_ARRAY);
165 1.31 dholland
166 1.31 dholland if (prop_array_count(datas) != 1)
167 1.31 dholland return EINVAL;
168 1.31 dholland
169 1.31 dholland data = prop_array_get(datas, 0);
170 1.31 dholland if (data == NULL)
171 1.31 dholland return ENOMEM;
172 1.31 dholland if (!prop_dictionary_get_cstring_nocopy(data, "quotafile",
173 1.31 dholland &qfile))
174 1.31 dholland return EINVAL;
175 1.31 dholland
176 1.34 dholland args.qc_op = QUOTACTL_QUOTAON;
177 1.31 dholland args.u.quotaon.qc_idtype = q2type;
178 1.31 dholland args.u.quotaon.qc_quotafile = qfile;
179 1.35 dholland return VFS_QUOTACTL(mp, &args);
180 1.5 dholland }
181 1.5 dholland
182 1.5 dholland static int
183 1.5 dholland vfs_quotactl_quotaoff(struct mount *mp,
184 1.5 dholland prop_dictionary_t cmddict, int q2type,
185 1.5 dholland prop_array_t datas)
186 1.5 dholland {
187 1.5 dholland struct vfs_quotactl_args args;
188 1.5 dholland
189 1.32 dholland KASSERT(prop_object_type(cmddict) == PROP_TYPE_DICTIONARY);
190 1.32 dholland KASSERT(prop_object_type(datas) == PROP_TYPE_ARRAY);
191 1.32 dholland
192 1.32 dholland if (prop_array_count(datas) != 0)
193 1.32 dholland return EINVAL;
194 1.32 dholland
195 1.34 dholland args.qc_op = QUOTACTL_QUOTAOFF;
196 1.32 dholland args.u.quotaoff.qc_idtype = q2type;
197 1.35 dholland return VFS_QUOTACTL(mp, &args);
198 1.5 dholland }
199 1.5 dholland
200 1.5 dholland static int
201 1.10 dholland vfs_quotactl_get_addreply(const struct quotakey *qk,
202 1.8 dholland const struct quotaval *blocks,
203 1.8 dholland const struct quotaval *files,
204 1.8 dholland prop_array_t replies)
205 1.8 dholland {
206 1.8 dholland prop_dictionary_t dict;
207 1.10 dholland id_t id;
208 1.10 dholland int defaultq;
209 1.10 dholland uint64_t *valuesp[QUOTA_NLIMITS];
210 1.8 dholland
211 1.8 dholland /* XXX illegal casts */
212 1.8 dholland valuesp[QUOTA_LIMIT_BLOCK] = (void *)(intptr_t)&blocks->qv_hardlimit;
213 1.8 dholland valuesp[QUOTA_LIMIT_FILE] = (void *)(intptr_t)&files->qv_hardlimit;
214 1.8 dholland
215 1.10 dholland if (qk->qk_id == QUOTA_DEFAULTID) {
216 1.10 dholland id = 0;
217 1.10 dholland defaultq = 1;
218 1.10 dholland } else {
219 1.10 dholland id = qk->qk_id;
220 1.10 dholland defaultq = 0;
221 1.10 dholland }
222 1.10 dholland
223 1.8 dholland dict = quota64toprop(id, defaultq, valuesp,
224 1.8 dholland ufs_quota_entry_names, UFS_QUOTA_NENTRIES,
225 1.8 dholland ufs_quota_limit_names, QUOTA_NLIMITS);
226 1.8 dholland if (dict == NULL)
227 1.8 dholland return ENOMEM;
228 1.8 dholland if (!prop_array_add_and_rel(replies, dict)) {
229 1.8 dholland prop_object_release(dict);
230 1.8 dholland return ENOMEM;
231 1.8 dholland }
232 1.8 dholland
233 1.8 dholland return 0;
234 1.8 dholland }
235 1.8 dholland
236 1.8 dholland static int
237 1.5 dholland vfs_quotactl_get(struct mount *mp,
238 1.10 dholland prop_dictionary_t cmddict, int idtype,
239 1.5 dholland prop_array_t datas)
240 1.5 dholland {
241 1.7 dholland prop_object_iterator_t iter;
242 1.7 dholland prop_dictionary_t data;
243 1.8 dholland prop_array_t replies;
244 1.7 dholland uint32_t id;
245 1.7 dholland const char *idstr;
246 1.5 dholland struct vfs_quotactl_args args;
247 1.10 dholland struct quotakey qk;
248 1.8 dholland struct quotaval blocks, files;
249 1.7 dholland int error;
250 1.7 dholland
251 1.7 dholland KASSERT(prop_object_type(cmddict) == PROP_TYPE_DICTIONARY);
252 1.7 dholland KASSERT(prop_object_type(datas) == PROP_TYPE_ARRAY);
253 1.7 dholland
254 1.7 dholland replies = prop_array_create();
255 1.7 dholland if (replies == NULL) {
256 1.7 dholland return ENOMEM;
257 1.7 dholland }
258 1.7 dholland
259 1.7 dholland iter = prop_array_iterator(datas);
260 1.7 dholland if (iter == NULL) {
261 1.7 dholland prop_object_release(replies);
262 1.7 dholland return ENOMEM;
263 1.7 dholland }
264 1.7 dholland
265 1.7 dholland while ((data = prop_object_iterator_next(iter)) != NULL) {
266 1.10 dholland qk.qk_idtype = idtype;
267 1.10 dholland
268 1.7 dholland if (!prop_dictionary_get_uint32(data, "id", &id)) {
269 1.7 dholland if (!prop_dictionary_get_cstring_nocopy(data, "id",
270 1.7 dholland &idstr))
271 1.7 dholland continue;
272 1.7 dholland if (strcmp(idstr, "default")) {
273 1.7 dholland error = EINVAL;
274 1.7 dholland goto fail;
275 1.7 dholland }
276 1.10 dholland qk.qk_id = QUOTA_DEFAULTID;
277 1.7 dholland } else {
278 1.10 dholland qk.qk_id = id;
279 1.7 dholland }
280 1.5 dholland
281 1.10 dholland qk.qk_objtype = QUOTA_OBJTYPE_BLOCKS;
282 1.10 dholland
283 1.34 dholland args.qc_op = QUOTACTL_GET;
284 1.10 dholland args.u.get.qc_key = &qk;
285 1.9 dholland args.u.get.qc_ret = &blocks;
286 1.35 dholland error = VFS_QUOTACTL(mp, &args);
287 1.9 dholland if (error == EPERM) {
288 1.9 dholland /* XXX does this make sense? */
289 1.9 dholland continue;
290 1.9 dholland } else if (error == ENOENT) {
291 1.9 dholland /* XXX does *this* make sense? */
292 1.9 dholland continue;
293 1.9 dholland } else if (error) {
294 1.9 dholland goto fail;
295 1.9 dholland }
296 1.9 dholland
297 1.10 dholland qk.qk_objtype = QUOTA_OBJTYPE_FILES;
298 1.10 dholland
299 1.34 dholland args.qc_op = QUOTACTL_GET;
300 1.10 dholland args.u.get.qc_key = &qk;
301 1.9 dholland args.u.get.qc_ret = &files;
302 1.35 dholland error = VFS_QUOTACTL(mp, &args);
303 1.7 dholland if (error == EPERM) {
304 1.7 dholland /* XXX does this make sense? */
305 1.7 dholland continue;
306 1.7 dholland } else if (error == ENOENT) {
307 1.7 dholland /* XXX does *this* make sense? */
308 1.7 dholland continue;
309 1.7 dholland } else if (error) {
310 1.7 dholland goto fail;
311 1.7 dholland }
312 1.8 dholland
313 1.10 dholland error = vfs_quotactl_get_addreply(&qk, &blocks, &files,
314 1.8 dholland replies);
315 1.7 dholland }
316 1.7 dholland
317 1.7 dholland prop_object_iterator_release(iter);
318 1.7 dholland if (!prop_dictionary_set_and_rel(cmddict, "data", replies)) {
319 1.7 dholland error = ENOMEM;
320 1.7 dholland } else {
321 1.7 dholland error = 0;
322 1.7 dholland }
323 1.7 dholland
324 1.7 dholland return error;
325 1.7 dholland
326 1.7 dholland fail:
327 1.7 dholland prop_object_iterator_release(iter);
328 1.7 dholland prop_object_release(replies);
329 1.7 dholland return error;
330 1.5 dholland }
331 1.5 dholland
332 1.5 dholland static int
333 1.15 dholland vfs_quotactl_put_extractinfo(prop_dictionary_t data,
334 1.12 dholland struct quotaval *blocks, struct quotaval *files)
335 1.12 dholland {
336 1.12 dholland /*
337 1.12 dholland * So, the way proptoquota64 works is that you pass it an
338 1.12 dholland * array of pointers to uint64. Each of these pointers is
339 1.12 dholland * supposed to point to 5 (UFS_QUOTA_NENTRIES) uint64s. This
340 1.12 dholland * array of pointers is the second argument. The third and
341 1.12 dholland * forth argument are the names of the five values to extract,
342 1.12 dholland * and UFS_QUOTA_NENTRIES. The last two arguments are the
343 1.12 dholland * names assocated with the pointers (QUOTATYPE_LDICT_BLOCK,
344 1.12 dholland * QUOTADICT_LTYPE_FILE) and the number of pointers. Most of
345 1.12 dholland * the existing code was unsafely casting struct quotaval
346 1.12 dholland * (formerly struct ufs_quota_entry) to (uint64_t *) and using
347 1.12 dholland * that as the block of 5 uint64s. Or worse, pointing to
348 1.12 dholland * subregions of that and reducing the number of uint64s to
349 1.12 dholland * pull "adjacent" values. Demons fly out of your nose!
350 1.12 dholland */
351 1.12 dholland
352 1.12 dholland uint64_t bvals[UFS_QUOTA_NENTRIES];
353 1.12 dholland uint64_t fvals[UFS_QUOTA_NENTRIES];
354 1.12 dholland uint64_t *valptrs[QUOTA_NLIMITS];
355 1.12 dholland int error;
356 1.12 dholland
357 1.12 dholland valptrs[QUOTA_LIMIT_BLOCK] = bvals;
358 1.12 dholland valptrs[QUOTA_LIMIT_FILE] = fvals;
359 1.12 dholland error = proptoquota64(data, valptrs,
360 1.12 dholland ufs_quota_entry_names, UFS_QUOTA_NENTRIES,
361 1.12 dholland ufs_quota_limit_names, QUOTA_NLIMITS);
362 1.12 dholland if (error) {
363 1.12 dholland return error;
364 1.12 dholland }
365 1.12 dholland
366 1.12 dholland /*
367 1.12 dholland * There are no symbolic constants for these indexes!
368 1.12 dholland */
369 1.12 dholland
370 1.12 dholland blocks->qv_hardlimit = bvals[0];
371 1.12 dholland blocks->qv_softlimit = bvals[1];
372 1.12 dholland blocks->qv_usage = bvals[2];
373 1.12 dholland blocks->qv_expiretime = bvals[3];
374 1.12 dholland blocks->qv_grace = bvals[4];
375 1.12 dholland files->qv_hardlimit = fvals[0];
376 1.12 dholland files->qv_softlimit = fvals[1];
377 1.12 dholland files->qv_usage = fvals[2];
378 1.12 dholland files->qv_expiretime = fvals[3];
379 1.12 dholland files->qv_grace = fvals[4];
380 1.12 dholland
381 1.12 dholland return 0;
382 1.12 dholland }
383 1.12 dholland
384 1.12 dholland static int
385 1.15 dholland vfs_quotactl_put(struct mount *mp,
386 1.5 dholland prop_dictionary_t cmddict, int q2type,
387 1.5 dholland prop_array_t datas)
388 1.5 dholland {
389 1.11 dholland prop_array_t replies;
390 1.11 dholland prop_object_iterator_t iter;
391 1.11 dholland prop_dictionary_t data;
392 1.11 dholland int defaultq;
393 1.11 dholland uint32_t id;
394 1.11 dholland const char *idstr;
395 1.14 dholland struct quotakey qk;
396 1.12 dholland struct quotaval blocks, files;
397 1.5 dholland struct vfs_quotactl_args args;
398 1.11 dholland int error;
399 1.11 dholland
400 1.11 dholland KASSERT(prop_object_type(cmddict) == PROP_TYPE_DICTIONARY);
401 1.11 dholland KASSERT(prop_object_type(datas) == PROP_TYPE_ARRAY);
402 1.5 dholland
403 1.11 dholland replies = prop_array_create();
404 1.11 dholland if (replies == NULL)
405 1.11 dholland return ENOMEM;
406 1.11 dholland
407 1.11 dholland iter = prop_array_iterator(datas);
408 1.11 dholland if (iter == NULL) {
409 1.11 dholland prop_object_release(replies);
410 1.11 dholland return ENOMEM;
411 1.11 dholland }
412 1.11 dholland
413 1.11 dholland while ((data = prop_object_iterator_next(iter)) != NULL) {
414 1.13 dholland
415 1.13 dholland KASSERT(prop_object_type(data) == PROP_TYPE_DICTIONARY);
416 1.13 dholland
417 1.11 dholland if (!prop_dictionary_get_uint32(data, "id", &id)) {
418 1.11 dholland if (!prop_dictionary_get_cstring_nocopy(data, "id",
419 1.11 dholland &idstr))
420 1.11 dholland continue;
421 1.11 dholland if (strcmp(idstr, "default"))
422 1.11 dholland continue;
423 1.11 dholland id = 0;
424 1.11 dholland defaultq = 1;
425 1.11 dholland } else {
426 1.11 dholland defaultq = 0;
427 1.11 dholland }
428 1.11 dholland
429 1.15 dholland error = vfs_quotactl_put_extractinfo(data, &blocks, &files);
430 1.12 dholland if (error) {
431 1.12 dholland goto err;
432 1.12 dholland }
433 1.12 dholland
434 1.14 dholland qk.qk_idtype = q2type;
435 1.14 dholland qk.qk_id = defaultq ? QUOTA_DEFAULTID : id;
436 1.14 dholland qk.qk_objtype = QUOTA_OBJTYPE_BLOCKS;
437 1.14 dholland
438 1.34 dholland args.qc_op = QUOTACTL_PUT;
439 1.15 dholland args.u.put.qc_key = &qk;
440 1.15 dholland args.u.put.qc_val = &blocks;
441 1.35 dholland error = VFS_QUOTACTL(mp, &args);
442 1.13 dholland if (error) {
443 1.13 dholland goto err;
444 1.13 dholland }
445 1.13 dholland
446 1.14 dholland qk.qk_idtype = q2type;
447 1.14 dholland qk.qk_id = defaultq ? QUOTA_DEFAULTID : id;
448 1.14 dholland qk.qk_objtype = QUOTA_OBJTYPE_FILES;
449 1.14 dholland
450 1.34 dholland args.qc_op = QUOTACTL_PUT;
451 1.15 dholland args.u.put.qc_key = &qk;
452 1.15 dholland args.u.put.qc_val = &files;
453 1.35 dholland error = VFS_QUOTACTL(mp, &args);
454 1.11 dholland if (error) {
455 1.11 dholland goto err;
456 1.11 dholland }
457 1.11 dholland }
458 1.11 dholland prop_object_iterator_release(iter);
459 1.11 dholland if (!prop_dictionary_set_and_rel(cmddict, "data", replies)) {
460 1.11 dholland error = ENOMEM;
461 1.11 dholland } else {
462 1.11 dholland error = 0;
463 1.11 dholland }
464 1.11 dholland return error;
465 1.11 dholland err:
466 1.11 dholland prop_object_iterator_release(iter);
467 1.11 dholland prop_object_release(replies);
468 1.11 dholland return error;
469 1.5 dholland }
470 1.5 dholland
471 1.23 dholland static prop_dictionary_t
472 1.26 dholland vfs_quotactl_getall_makereply(const struct quotakey *key)
473 1.26 dholland {
474 1.26 dholland prop_dictionary_t dict;
475 1.26 dholland id_t id;
476 1.26 dholland int defaultq;
477 1.26 dholland
478 1.26 dholland dict = prop_dictionary_create();
479 1.26 dholland if (dict == NULL)
480 1.26 dholland return NULL;
481 1.26 dholland
482 1.26 dholland id = key->qk_id;
483 1.26 dholland if (id == QUOTA_DEFAULTID) {
484 1.26 dholland id = 0;
485 1.26 dholland defaultq = 1;
486 1.26 dholland } else {
487 1.26 dholland defaultq = 0;
488 1.26 dholland }
489 1.26 dholland
490 1.26 dholland if (defaultq) {
491 1.26 dholland if (!prop_dictionary_set_cstring_nocopy(dict, "id",
492 1.26 dholland "default")) {
493 1.26 dholland goto err;
494 1.26 dholland }
495 1.26 dholland } else {
496 1.26 dholland if (!prop_dictionary_set_uint32(dict, "id", id)) {
497 1.26 dholland goto err;
498 1.26 dholland }
499 1.26 dholland }
500 1.26 dholland
501 1.26 dholland return dict;
502 1.26 dholland
503 1.26 dholland err:
504 1.26 dholland prop_object_release(dict);
505 1.26 dholland return NULL;
506 1.26 dholland }
507 1.26 dholland
508 1.26 dholland static int
509 1.26 dholland vfs_quotactl_getall_addreply(prop_dictionary_t thisreply,
510 1.26 dholland const struct quotakey *key, const struct quotaval *val)
511 1.23 dholland {
512 1.23 dholland #define INITQVNAMES_ALL { \
513 1.23 dholland QUOTADICT_LIMIT_HARD, \
514 1.23 dholland QUOTADICT_LIMIT_SOFT, \
515 1.23 dholland QUOTADICT_LIMIT_USAGE, \
516 1.23 dholland QUOTADICT_LIMIT_ETIME, \
517 1.23 dholland QUOTADICT_LIMIT_GTIME \
518 1.23 dholland }
519 1.23 dholland #define N_QV 5
520 1.23 dholland
521 1.23 dholland const char *val_names[] = INITQVNAMES_ALL;
522 1.23 dholland uint64_t vals[N_QV];
523 1.23 dholland prop_dictionary_t dict2;
524 1.26 dholland const char *objtypename;
525 1.23 dholland
526 1.26 dholland switch (key->qk_objtype) {
527 1.26 dholland case QUOTA_OBJTYPE_BLOCKS:
528 1.26 dholland objtypename = QUOTADICT_LTYPE_BLOCK;
529 1.26 dholland break;
530 1.26 dholland case QUOTA_OBJTYPE_FILES:
531 1.26 dholland objtypename = QUOTADICT_LTYPE_FILE;
532 1.26 dholland break;
533 1.26 dholland default:
534 1.26 dholland return EINVAL;
535 1.23 dholland }
536 1.23 dholland
537 1.26 dholland vals[0] = val->qv_hardlimit;
538 1.26 dholland vals[1] = val->qv_softlimit;
539 1.26 dholland vals[2] = val->qv_usage;
540 1.26 dholland vals[3] = val->qv_expiretime;
541 1.26 dholland vals[4] = val->qv_grace;
542 1.23 dholland dict2 = limits64toprop(vals, val_names, N_QV);
543 1.23 dholland if (dict2 == NULL)
544 1.26 dholland return ENOMEM;
545 1.23 dholland
546 1.26 dholland if (!prop_dictionary_set_and_rel(thisreply, objtypename, dict2))
547 1.26 dholland return ENOMEM;
548 1.23 dholland
549 1.26 dholland return 0;
550 1.23 dholland }
551 1.23 dholland
552 1.5 dholland static int
553 1.5 dholland vfs_quotactl_getall(struct mount *mp,
554 1.5 dholland prop_dictionary_t cmddict, int q2type,
555 1.5 dholland prop_array_t datas)
556 1.5 dholland {
557 1.21 dholland struct quotakcursor cursor;
558 1.27 dholland struct quotakey *keys;
559 1.27 dholland struct quotaval *vals;
560 1.27 dholland unsigned loopmax = 8;
561 1.27 dholland unsigned loopnum;
562 1.30 dholland int skipidtype;
563 1.5 dholland struct vfs_quotactl_args args;
564 1.23 dholland prop_array_t replies;
565 1.30 dholland int atend, atzero;
566 1.26 dholland struct quotakey *key;
567 1.26 dholland struct quotaval *val;
568 1.26 dholland id_t lastid;
569 1.27 dholland prop_dictionary_t thisreply;
570 1.23 dholland unsigned i;
571 1.21 dholland int error, error2;
572 1.23 dholland
573 1.23 dholland KASSERT(prop_object_type(cmddict) == PROP_TYPE_DICTIONARY);
574 1.21 dholland
575 1.34 dholland args.qc_op = QUOTACTL_CURSOROPEN;
576 1.21 dholland args.u.cursoropen.qc_cursor = &cursor;
577 1.35 dholland error = VFS_QUOTACTL(mp, &args);
578 1.21 dholland if (error) {
579 1.21 dholland return error;
580 1.21 dholland }
581 1.5 dholland
582 1.27 dholland keys = malloc(loopmax * sizeof(keys[0]), M_TEMP, M_WAITOK);
583 1.27 dholland vals = malloc(loopmax * sizeof(vals[0]), M_TEMP, M_WAITOK);
584 1.23 dholland
585 1.30 dholland skipidtype = (q2type == QUOTA_IDTYPE_USER ?
586 1.30 dholland QUOTA_IDTYPE_GROUP : QUOTA_IDTYPE_USER);
587 1.34 dholland args.qc_op = QUOTACTL_CURSORSKIPIDTYPE;
588 1.30 dholland args.u.cursorskipidtype.qc_cursor = &cursor;
589 1.30 dholland args.u.cursorskipidtype.qc_idtype = skipidtype;
590 1.35 dholland error = VFS_QUOTACTL(mp, &args);
591 1.30 dholland /* ignore if it fails */
592 1.30 dholland (void)error;
593 1.30 dholland
594 1.23 dholland replies = prop_array_create();
595 1.23 dholland if (replies == NULL) {
596 1.23 dholland error = ENOMEM;
597 1.23 dholland goto err;
598 1.23 dholland }
599 1.23 dholland
600 1.26 dholland thisreply = NULL;
601 1.26 dholland lastid = 0; /* value not actually referenced */
602 1.30 dholland atzero = 0;
603 1.27 dholland
604 1.27 dholland while (1) {
605 1.34 dholland args.qc_op = QUOTACTL_CURSORATEND;
606 1.30 dholland args.u.cursoratend.qc_cursor = &cursor;
607 1.30 dholland args.u.cursoratend.qc_ret = &atend;
608 1.35 dholland error = VFS_QUOTACTL(mp, &args);
609 1.30 dholland if (error) {
610 1.30 dholland goto err;
611 1.30 dholland }
612 1.30 dholland if (atend) {
613 1.30 dholland break;
614 1.30 dholland }
615 1.30 dholland
616 1.34 dholland args.qc_op = QUOTACTL_CURSORGET;
617 1.29 dholland args.u.cursorget.qc_cursor = &cursor;
618 1.29 dholland args.u.cursorget.qc_keys = keys;
619 1.29 dholland args.u.cursorget.qc_vals = vals;
620 1.29 dholland args.u.cursorget.qc_maxnum = loopmax;
621 1.29 dholland args.u.cursorget.qc_ret = &loopnum;
622 1.27 dholland
623 1.35 dholland error = VFS_QUOTACTL(mp, &args);
624 1.30 dholland if (error == EDEADLK) {
625 1.30 dholland /*
626 1.30 dholland * transaction abort, start over
627 1.30 dholland */
628 1.30 dholland
629 1.34 dholland args.qc_op = QUOTACTL_CURSORREWIND;
630 1.30 dholland args.u.cursorrewind.qc_cursor = &cursor;
631 1.35 dholland error = VFS_QUOTACTL(mp, &args);
632 1.30 dholland if (error) {
633 1.30 dholland goto err;
634 1.30 dholland }
635 1.30 dholland
636 1.34 dholland args.qc_op = QUOTACTL_CURSORSKIPIDTYPE;
637 1.30 dholland args.u.cursorskipidtype.qc_cursor = &cursor;
638 1.30 dholland args.u.cursorskipidtype.qc_idtype = skipidtype;
639 1.35 dholland error = VFS_QUOTACTL(mp, &args);
640 1.30 dholland /* ignore if it fails */
641 1.30 dholland (void)error;
642 1.30 dholland
643 1.30 dholland prop_object_release(replies);
644 1.30 dholland replies = prop_array_create();
645 1.30 dholland if (replies == NULL) {
646 1.30 dholland error = ENOMEM;
647 1.30 dholland goto err;
648 1.30 dholland }
649 1.30 dholland
650 1.30 dholland thisreply = NULL;
651 1.30 dholland lastid = 0;
652 1.30 dholland atzero = 0;
653 1.30 dholland
654 1.30 dholland continue;
655 1.30 dholland }
656 1.26 dholland if (error) {
657 1.23 dholland goto err;
658 1.23 dholland }
659 1.27 dholland
660 1.27 dholland if (loopnum == 0) {
661 1.30 dholland /*
662 1.30 dholland * This is not supposed to happen. However,
663 1.30 dholland * allow a return of zero items once as long
664 1.30 dholland * as something happens (including an atend
665 1.30 dholland * indication) on the next pass. If it happens
666 1.30 dholland * twice, warn and assume end of iteration.
667 1.30 dholland */
668 1.30 dholland if (atzero) {
669 1.30 dholland printf("vfs_quotactl: zero items returned\n");
670 1.30 dholland break;
671 1.30 dholland }
672 1.30 dholland atzero = 1;
673 1.30 dholland } else {
674 1.30 dholland atzero = 0;
675 1.27 dholland }
676 1.27 dholland
677 1.27 dholland for (i = 0; i < loopnum; i++) {
678 1.27 dholland key = &keys[i];
679 1.27 dholland val = &vals[i];
680 1.27 dholland
681 1.28 dholland if (key->qk_idtype != q2type) {
682 1.28 dholland /* don't want this result */
683 1.28 dholland continue;
684 1.28 dholland }
685 1.28 dholland
686 1.27 dholland if (thisreply == NULL || key->qk_id != lastid) {
687 1.27 dholland lastid = key->qk_id;
688 1.27 dholland thisreply = vfs_quotactl_getall_makereply(key);
689 1.27 dholland if (thisreply == NULL) {
690 1.27 dholland error = ENOMEM;
691 1.27 dholland goto err;
692 1.27 dholland }
693 1.27 dholland /*
694 1.27 dholland * Note: while we release our reference to
695 1.27 dholland * thisreply here, we can (and do) continue to
696 1.27 dholland * use the pointer in the loop because the
697 1.27 dholland * copy attached to the replies array is not
698 1.27 dholland * going away.
699 1.27 dholland */
700 1.27 dholland if (!prop_array_add_and_rel(replies,
701 1.27 dholland thisreply)) {
702 1.27 dholland error = ENOMEM;
703 1.27 dholland goto err;
704 1.27 dholland }
705 1.27 dholland }
706 1.27 dholland
707 1.27 dholland error = vfs_quotactl_getall_addreply(thisreply,
708 1.27 dholland key, val);
709 1.27 dholland if (error) {
710 1.27 dholland goto err;
711 1.27 dholland }
712 1.27 dholland }
713 1.23 dholland }
714 1.23 dholland
715 1.23 dholland if (!prop_dictionary_set_and_rel(cmddict, "data", replies)) {
716 1.30 dholland replies = NULL;
717 1.23 dholland error = ENOMEM;
718 1.23 dholland goto err;
719 1.23 dholland }
720 1.30 dholland replies = NULL;
721 1.30 dholland error = 0;
722 1.23 dholland
723 1.23 dholland err:
724 1.27 dholland free(keys, M_TEMP);
725 1.27 dholland free(vals, M_TEMP);
726 1.21 dholland
727 1.30 dholland if (replies != NULL) {
728 1.30 dholland prop_object_release(replies);
729 1.30 dholland }
730 1.30 dholland
731 1.34 dholland args.qc_op = QUOTACTL_CURSORCLOSE;
732 1.21 dholland args.u.cursorclose.qc_cursor = &cursor;
733 1.35 dholland error2 = VFS_QUOTACTL(mp, &args);
734 1.21 dholland
735 1.21 dholland if (error) {
736 1.21 dholland return error;
737 1.21 dholland }
738 1.21 dholland error = error2;
739 1.21 dholland return error;
740 1.5 dholland }
741 1.5 dholland
742 1.5 dholland static int
743 1.5 dholland vfs_quotactl_clear(struct mount *mp,
744 1.5 dholland prop_dictionary_t cmddict, int q2type,
745 1.5 dholland prop_array_t datas)
746 1.5 dholland {
747 1.16 dholland prop_array_t replies;
748 1.16 dholland prop_object_iterator_t iter;
749 1.16 dholland prop_dictionary_t data;
750 1.16 dholland uint32_t id;
751 1.16 dholland int defaultq;
752 1.16 dholland const char *idstr;
753 1.20 dholland struct quotakey qk;
754 1.5 dholland struct vfs_quotactl_args args;
755 1.16 dholland int error;
756 1.16 dholland
757 1.16 dholland KASSERT(prop_object_type(cmddict) == PROP_TYPE_DICTIONARY);
758 1.16 dholland KASSERT(prop_object_type(datas) == PROP_TYPE_ARRAY);
759 1.16 dholland
760 1.16 dholland replies = prop_array_create();
761 1.16 dholland if (replies == NULL)
762 1.16 dholland return ENOMEM;
763 1.5 dholland
764 1.16 dholland iter = prop_array_iterator(datas);
765 1.16 dholland if (iter == NULL) {
766 1.16 dholland prop_object_release(replies);
767 1.16 dholland return ENOMEM;
768 1.16 dholland }
769 1.16 dholland
770 1.16 dholland while ((data = prop_object_iterator_next(iter)) != NULL) {
771 1.16 dholland if (!prop_dictionary_get_uint32(data, "id", &id)) {
772 1.16 dholland if (!prop_dictionary_get_cstring_nocopy(data, "id",
773 1.16 dholland &idstr))
774 1.16 dholland continue;
775 1.16 dholland if (strcmp(idstr, "default"))
776 1.16 dholland continue;
777 1.16 dholland id = 0;
778 1.16 dholland defaultq = 1;
779 1.16 dholland } else {
780 1.16 dholland defaultq = 0;
781 1.16 dholland }
782 1.16 dholland
783 1.20 dholland qk.qk_idtype = q2type;
784 1.20 dholland qk.qk_id = defaultq ? QUOTA_DEFAULTID : id;
785 1.20 dholland qk.qk_objtype = QUOTA_OBJTYPE_BLOCKS;
786 1.20 dholland
787 1.34 dholland args.qc_op = QUOTACTL_DELETE;
788 1.20 dholland args.u.delete.qc_key = &qk;
789 1.35 dholland error = VFS_QUOTACTL(mp, &args);
790 1.18 dholland if (error) {
791 1.18 dholland goto err;
792 1.18 dholland }
793 1.18 dholland
794 1.20 dholland qk.qk_idtype = q2type;
795 1.20 dholland qk.qk_id = defaultq ? QUOTA_DEFAULTID : id;
796 1.20 dholland qk.qk_objtype = QUOTA_OBJTYPE_FILES;
797 1.20 dholland
798 1.34 dholland args.qc_op = QUOTACTL_DELETE;
799 1.20 dholland args.u.delete.qc_key = &qk;
800 1.35 dholland error = VFS_QUOTACTL(mp, &args);
801 1.16 dholland if (error) {
802 1.16 dholland goto err;
803 1.16 dholland }
804 1.16 dholland }
805 1.16 dholland
806 1.16 dholland prop_object_iterator_release(iter);
807 1.16 dholland if (!prop_dictionary_set_and_rel(cmddict, "data", replies)) {
808 1.16 dholland error = ENOMEM;
809 1.16 dholland } else {
810 1.16 dholland error = 0;
811 1.16 dholland }
812 1.16 dholland return error;
813 1.16 dholland err:
814 1.16 dholland prop_object_iterator_release(iter);
815 1.16 dholland prop_object_release(replies);
816 1.16 dholland return error;
817 1.5 dholland }
818 1.5 dholland
819 1.5 dholland static int
820 1.4 dholland vfs_quotactl_cmd(struct mount *mp, prop_dictionary_t cmddict)
821 1.4 dholland {
822 1.4 dholland int error;
823 1.4 dholland const char *cmd, *type;
824 1.4 dholland prop_array_t datas;
825 1.4 dholland int q2type;
826 1.4 dholland
827 1.4 dholland if (!prop_dictionary_get_cstring_nocopy(cmddict, "command", &cmd))
828 1.4 dholland return EINVAL;
829 1.4 dholland if (!prop_dictionary_get_cstring_nocopy(cmddict, "type", &type))
830 1.4 dholland return EINVAL;
831 1.4 dholland
832 1.4 dholland if (!strcmp(type, QUOTADICT_CLASS_USER)) {
833 1.4 dholland q2type = QUOTA_CLASS_USER;
834 1.4 dholland } else if (!strcmp(type, QUOTADICT_CLASS_GROUP)) {
835 1.4 dholland q2type = QUOTA_CLASS_GROUP;
836 1.4 dholland } else {
837 1.4 dholland /* XXX this is a bad errno for this case */
838 1.4 dholland return EOPNOTSUPP;
839 1.4 dholland }
840 1.4 dholland
841 1.4 dholland datas = prop_dictionary_get(cmddict, "data");
842 1.4 dholland if (datas == NULL || prop_object_type(datas) != PROP_TYPE_ARRAY)
843 1.4 dholland return EINVAL;
844 1.4 dholland
845 1.4 dholland prop_object_retain(datas);
846 1.4 dholland prop_dictionary_remove(cmddict, "data"); /* prepare for return */
847 1.4 dholland
848 1.4 dholland if (strcmp(cmd, "get version") == 0) {
849 1.5 dholland error = vfs_quotactl_getversion(mp, cmddict, q2type, datas);
850 1.4 dholland } else if (strcmp(cmd, "quotaon") == 0) {
851 1.5 dholland error = vfs_quotactl_quotaon(mp, cmddict, q2type, datas);
852 1.4 dholland } else if (strcmp(cmd, "quotaoff") == 0) {
853 1.5 dholland error = vfs_quotactl_quotaoff(mp, cmddict, q2type, datas);
854 1.4 dholland } else if (strcmp(cmd, "get") == 0) {
855 1.5 dholland error = vfs_quotactl_get(mp, cmddict, q2type, datas);
856 1.4 dholland } else if (strcmp(cmd, "set") == 0) {
857 1.15 dholland error = vfs_quotactl_put(mp, cmddict, q2type, datas);
858 1.4 dholland } else if (strcmp(cmd, "getall") == 0) {
859 1.5 dholland error = vfs_quotactl_getall(mp, cmddict, q2type, datas);
860 1.4 dholland } else if (strcmp(cmd, "clear") == 0) {
861 1.5 dholland error = vfs_quotactl_clear(mp, cmddict, q2type, datas);
862 1.4 dholland } else {
863 1.4 dholland /* XXX this a bad errno for this case */
864 1.4 dholland error = EOPNOTSUPP;
865 1.4 dholland }
866 1.4 dholland
867 1.4 dholland error = (prop_dictionary_set_int8(cmddict, "return",
868 1.4 dholland error) ? 0 : ENOMEM);
869 1.4 dholland prop_object_release(datas);
870 1.4 dholland
871 1.4 dholland return error;
872 1.4 dholland }
873 1.4 dholland
874 1.2 dholland int
875 1.2 dholland vfs_quotactl(struct mount *mp, prop_dictionary_t dict)
876 1.2 dholland {
877 1.3 dholland prop_dictionary_t cmddict;
878 1.3 dholland prop_array_t commands;
879 1.3 dholland prop_object_iterator_t iter;
880 1.3 dholland int error;
881 1.3 dholland
882 1.3 dholland error = quota_get_cmds(dict, &commands);
883 1.3 dholland if (error) {
884 1.3 dholland return error;
885 1.3 dholland }
886 1.3 dholland
887 1.3 dholland iter = prop_array_iterator(commands);
888 1.3 dholland if (iter == NULL) {
889 1.3 dholland return ENOMEM;
890 1.3 dholland }
891 1.3 dholland
892 1.3 dholland while ((cmddict = prop_object_iterator_next(iter)) != NULL) {
893 1.3 dholland if (prop_object_type(cmddict) != PROP_TYPE_DICTIONARY) {
894 1.3 dholland /* XXX shouldn't this be an error? */
895 1.3 dholland continue;
896 1.3 dholland }
897 1.4 dholland error = vfs_quotactl_cmd(mp, cmddict);
898 1.3 dholland if (error) {
899 1.3 dholland break;
900 1.3 dholland }
901 1.3 dholland }
902 1.3 dholland prop_object_iterator_release(iter);
903 1.3 dholland return error;
904 1.2 dholland }
905