vfs_quotactl.c revision 1.20 1 /* $NetBSD: vfs_quotactl.c,v 1.20 2012/01/29 06:55:44 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.20 2012/01/29 06:55:44 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(const struct quotakey *qk,
171 const struct quotaval *blocks,
172 const struct quotaval *files,
173 prop_array_t replies)
174 {
175 prop_dictionary_t dict;
176 id_t id;
177 int defaultq;
178 uint64_t *valuesp[QUOTA_NLIMITS];
179
180 /* XXX illegal casts */
181 valuesp[QUOTA_LIMIT_BLOCK] = (void *)(intptr_t)&blocks->qv_hardlimit;
182 valuesp[QUOTA_LIMIT_FILE] = (void *)(intptr_t)&files->qv_hardlimit;
183
184 if (qk->qk_id == QUOTA_DEFAULTID) {
185 id = 0;
186 defaultq = 1;
187 } else {
188 id = qk->qk_id;
189 defaultq = 0;
190 }
191
192 dict = quota64toprop(id, defaultq, valuesp,
193 ufs_quota_entry_names, UFS_QUOTA_NENTRIES,
194 ufs_quota_limit_names, QUOTA_NLIMITS);
195 if (dict == NULL)
196 return ENOMEM;
197 if (!prop_array_add_and_rel(replies, dict)) {
198 prop_object_release(dict);
199 return ENOMEM;
200 }
201
202 return 0;
203 }
204
205 static int
206 vfs_quotactl_get(struct mount *mp,
207 prop_dictionary_t cmddict, int idtype,
208 prop_array_t datas)
209 {
210 prop_object_iterator_t iter;
211 prop_dictionary_t data;
212 prop_array_t replies;
213 uint32_t id;
214 const char *idstr;
215 struct vfs_quotactl_args args;
216 struct quotakey qk;
217 struct quotaval blocks, files;
218 int error;
219
220 KASSERT(prop_object_type(cmddict) == PROP_TYPE_DICTIONARY);
221 KASSERT(prop_object_type(datas) == PROP_TYPE_ARRAY);
222
223 replies = prop_array_create();
224 if (replies == NULL) {
225 return ENOMEM;
226 }
227
228 iter = prop_array_iterator(datas);
229 if (iter == NULL) {
230 prop_object_release(replies);
231 return ENOMEM;
232 }
233
234 while ((data = prop_object_iterator_next(iter)) != NULL) {
235 qk.qk_idtype = idtype;
236
237 if (!prop_dictionary_get_uint32(data, "id", &id)) {
238 if (!prop_dictionary_get_cstring_nocopy(data, "id",
239 &idstr))
240 continue;
241 if (strcmp(idstr, "default")) {
242 error = EINVAL;
243 goto fail;
244 }
245 qk.qk_id = QUOTA_DEFAULTID;
246 } else {
247 qk.qk_id = id;
248 }
249
250 qk.qk_objtype = QUOTA_OBJTYPE_BLOCKS;
251
252 args.qc_type = QCT_GET;
253 args.u.get.qc_key = &qk;
254 args.u.get.qc_ret = &blocks;
255 error = VFS_QUOTACTL(mp, QUOTACTL_GET, &args);
256 if (error == EPERM) {
257 /* XXX does this make sense? */
258 continue;
259 } else if (error == ENOENT) {
260 /* XXX does *this* make sense? */
261 continue;
262 } else if (error) {
263 goto fail;
264 }
265
266 qk.qk_objtype = QUOTA_OBJTYPE_FILES;
267
268 args.qc_type = QCT_GET;
269 args.u.get.qc_key = &qk;
270 args.u.get.qc_ret = &files;
271 error = VFS_QUOTACTL(mp, QUOTACTL_GET, &args);
272 if (error == EPERM) {
273 /* XXX does this make sense? */
274 continue;
275 } else if (error == ENOENT) {
276 /* XXX does *this* make sense? */
277 continue;
278 } else if (error) {
279 goto fail;
280 }
281
282 error = vfs_quotactl_get_addreply(&qk, &blocks, &files,
283 replies);
284 }
285
286 prop_object_iterator_release(iter);
287 if (!prop_dictionary_set_and_rel(cmddict, "data", replies)) {
288 error = ENOMEM;
289 } else {
290 error = 0;
291 }
292
293 return error;
294
295 fail:
296 prop_object_iterator_release(iter);
297 prop_object_release(replies);
298 return error;
299 }
300
301 static int
302 vfs_quotactl_put_extractinfo(prop_dictionary_t data,
303 struct quotaval *blocks, struct quotaval *files)
304 {
305 /*
306 * So, the way proptoquota64 works is that you pass it an
307 * array of pointers to uint64. Each of these pointers is
308 * supposed to point to 5 (UFS_QUOTA_NENTRIES) uint64s. This
309 * array of pointers is the second argument. The third and
310 * forth argument are the names of the five values to extract,
311 * and UFS_QUOTA_NENTRIES. The last two arguments are the
312 * names assocated with the pointers (QUOTATYPE_LDICT_BLOCK,
313 * QUOTADICT_LTYPE_FILE) and the number of pointers. Most of
314 * the existing code was unsafely casting struct quotaval
315 * (formerly struct ufs_quota_entry) to (uint64_t *) and using
316 * that as the block of 5 uint64s. Or worse, pointing to
317 * subregions of that and reducing the number of uint64s to
318 * pull "adjacent" values. Demons fly out of your nose!
319 */
320
321 uint64_t bvals[UFS_QUOTA_NENTRIES];
322 uint64_t fvals[UFS_QUOTA_NENTRIES];
323 uint64_t *valptrs[QUOTA_NLIMITS];
324 int error;
325
326 valptrs[QUOTA_LIMIT_BLOCK] = bvals;
327 valptrs[QUOTA_LIMIT_FILE] = fvals;
328 error = proptoquota64(data, valptrs,
329 ufs_quota_entry_names, UFS_QUOTA_NENTRIES,
330 ufs_quota_limit_names, QUOTA_NLIMITS);
331 if (error) {
332 return error;
333 }
334
335 /*
336 * There are no symbolic constants for these indexes!
337 */
338
339 blocks->qv_hardlimit = bvals[0];
340 blocks->qv_softlimit = bvals[1];
341 blocks->qv_usage = bvals[2];
342 blocks->qv_expiretime = bvals[3];
343 blocks->qv_grace = bvals[4];
344 files->qv_hardlimit = fvals[0];
345 files->qv_softlimit = fvals[1];
346 files->qv_usage = fvals[2];
347 files->qv_expiretime = fvals[3];
348 files->qv_grace = fvals[4];
349
350 return 0;
351 }
352
353 static int
354 vfs_quotactl_put(struct mount *mp,
355 prop_dictionary_t cmddict, int q2type,
356 prop_array_t datas)
357 {
358 prop_array_t replies;
359 prop_object_iterator_t iter;
360 prop_dictionary_t data;
361 int defaultq;
362 uint32_t id;
363 const char *idstr;
364 struct quotakey qk;
365 struct quotaval blocks, files;
366 struct vfs_quotactl_args args;
367 int error;
368
369 KASSERT(prop_object_type(cmddict) == PROP_TYPE_DICTIONARY);
370 KASSERT(prop_object_type(datas) == PROP_TYPE_ARRAY);
371
372 replies = prop_array_create();
373 if (replies == NULL)
374 return ENOMEM;
375
376 iter = prop_array_iterator(datas);
377 if (iter == NULL) {
378 prop_object_release(replies);
379 return ENOMEM;
380 }
381
382 while ((data = prop_object_iterator_next(iter)) != NULL) {
383
384 KASSERT(prop_object_type(data) == PROP_TYPE_DICTIONARY);
385
386 if (!prop_dictionary_get_uint32(data, "id", &id)) {
387 if (!prop_dictionary_get_cstring_nocopy(data, "id",
388 &idstr))
389 continue;
390 if (strcmp(idstr, "default"))
391 continue;
392 id = 0;
393 defaultq = 1;
394 } else {
395 defaultq = 0;
396 }
397
398 error = vfs_quotactl_put_extractinfo(data, &blocks, &files);
399 if (error) {
400 goto err;
401 }
402
403 qk.qk_idtype = q2type;
404 qk.qk_id = defaultq ? QUOTA_DEFAULTID : id;
405 qk.qk_objtype = QUOTA_OBJTYPE_BLOCKS;
406
407 args.qc_type = QCT_PUT;
408 args.u.put.qc_key = &qk;
409 args.u.put.qc_val = &blocks;
410 error = VFS_QUOTACTL(mp, QUOTACTL_PUT, &args);
411 if (error) {
412 goto err;
413 }
414
415 qk.qk_idtype = q2type;
416 qk.qk_id = defaultq ? QUOTA_DEFAULTID : id;
417 qk.qk_objtype = QUOTA_OBJTYPE_FILES;
418
419 args.qc_type = QCT_PUT;
420 args.u.put.qc_key = &qk;
421 args.u.put.qc_val = &files;
422 error = VFS_QUOTACTL(mp, QUOTACTL_PUT, &args);
423 if (error) {
424 goto err;
425 }
426 }
427 prop_object_iterator_release(iter);
428 if (!prop_dictionary_set_and_rel(cmddict, "data", replies)) {
429 error = ENOMEM;
430 } else {
431 error = 0;
432 }
433 return error;
434 err:
435 prop_object_iterator_release(iter);
436 prop_object_release(replies);
437 return error;
438 }
439
440 static int
441 vfs_quotactl_getall(struct mount *mp,
442 prop_dictionary_t cmddict, int q2type,
443 prop_array_t datas)
444 {
445 struct vfs_quotactl_args args;
446
447 args.qc_type = QCT_PROPLIB;
448 args.u.proplib.qc_cmddict = cmddict;
449 args.u.proplib.qc_q2type = q2type;
450 args.u.proplib.qc_datas = datas;
451 return VFS_QUOTACTL(mp, QUOTACTL_GETALL, &args);
452 }
453
454 static int
455 vfs_quotactl_clear(struct mount *mp,
456 prop_dictionary_t cmddict, int q2type,
457 prop_array_t datas)
458 {
459 prop_array_t replies;
460 prop_object_iterator_t iter;
461 prop_dictionary_t data;
462 uint32_t id;
463 int defaultq;
464 const char *idstr;
465 struct quotakey qk;
466 struct vfs_quotactl_args args;
467 int error;
468
469 KASSERT(prop_object_type(cmddict) == PROP_TYPE_DICTIONARY);
470 KASSERT(prop_object_type(datas) == PROP_TYPE_ARRAY);
471
472 replies = prop_array_create();
473 if (replies == NULL)
474 return ENOMEM;
475
476 iter = prop_array_iterator(datas);
477 if (iter == NULL) {
478 prop_object_release(replies);
479 return ENOMEM;
480 }
481
482 while ((data = prop_object_iterator_next(iter)) != NULL) {
483 if (!prop_dictionary_get_uint32(data, "id", &id)) {
484 if (!prop_dictionary_get_cstring_nocopy(data, "id",
485 &idstr))
486 continue;
487 if (strcmp(idstr, "default"))
488 continue;
489 id = 0;
490 defaultq = 1;
491 } else {
492 defaultq = 0;
493 }
494
495 qk.qk_idtype = q2type;
496 qk.qk_id = defaultq ? QUOTA_DEFAULTID : id;
497 qk.qk_objtype = QUOTA_OBJTYPE_BLOCKS;
498
499 args.qc_type = QCT_DELETE;
500 args.u.delete.qc_key = &qk;
501 error = VFS_QUOTACTL(mp, QUOTACTL_DELETE, &args);
502 if (error) {
503 goto err;
504 }
505
506 qk.qk_idtype = q2type;
507 qk.qk_id = defaultq ? QUOTA_DEFAULTID : id;
508 qk.qk_objtype = QUOTA_OBJTYPE_FILES;
509
510 args.qc_type = QCT_DELETE;
511 args.u.delete.qc_key = &qk;
512 error = VFS_QUOTACTL(mp, QUOTACTL_DELETE, &args);
513 if (error) {
514 goto err;
515 }
516 }
517
518 prop_object_iterator_release(iter);
519 if (!prop_dictionary_set_and_rel(cmddict, "data", replies)) {
520 error = ENOMEM;
521 } else {
522 error = 0;
523 }
524 return error;
525 err:
526 prop_object_iterator_release(iter);
527 prop_object_release(replies);
528 return error;
529 }
530
531 static int
532 vfs_quotactl_cmd(struct mount *mp, prop_dictionary_t cmddict)
533 {
534 int error;
535 const char *cmd, *type;
536 prop_array_t datas;
537 int q2type;
538
539 if (!prop_dictionary_get_cstring_nocopy(cmddict, "command", &cmd))
540 return EINVAL;
541 if (!prop_dictionary_get_cstring_nocopy(cmddict, "type", &type))
542 return EINVAL;
543
544 if (!strcmp(type, QUOTADICT_CLASS_USER)) {
545 q2type = QUOTA_CLASS_USER;
546 } else if (!strcmp(type, QUOTADICT_CLASS_GROUP)) {
547 q2type = QUOTA_CLASS_GROUP;
548 } else {
549 /* XXX this is a bad errno for this case */
550 return EOPNOTSUPP;
551 }
552
553 datas = prop_dictionary_get(cmddict, "data");
554 if (datas == NULL || prop_object_type(datas) != PROP_TYPE_ARRAY)
555 return EINVAL;
556
557 prop_object_retain(datas);
558 prop_dictionary_remove(cmddict, "data"); /* prepare for return */
559
560 if (strcmp(cmd, "get version") == 0) {
561 error = vfs_quotactl_getversion(mp, cmddict, q2type, datas);
562 } else if (strcmp(cmd, "quotaon") == 0) {
563 error = vfs_quotactl_quotaon(mp, cmddict, q2type, datas);
564 } else if (strcmp(cmd, "quotaoff") == 0) {
565 error = vfs_quotactl_quotaoff(mp, cmddict, q2type, datas);
566 } else if (strcmp(cmd, "get") == 0) {
567 error = vfs_quotactl_get(mp, cmddict, q2type, datas);
568 } else if (strcmp(cmd, "set") == 0) {
569 error = vfs_quotactl_put(mp, cmddict, q2type, datas);
570 } else if (strcmp(cmd, "getall") == 0) {
571 error = vfs_quotactl_getall(mp, cmddict, q2type, datas);
572 } else if (strcmp(cmd, "clear") == 0) {
573 error = vfs_quotactl_clear(mp, cmddict, q2type, datas);
574 } else {
575 /* XXX this a bad errno for this case */
576 error = EOPNOTSUPP;
577 }
578
579 error = (prop_dictionary_set_int8(cmddict, "return",
580 error) ? 0 : ENOMEM);
581 prop_object_release(datas);
582
583 return error;
584 }
585
586 int
587 vfs_quotactl(struct mount *mp, prop_dictionary_t dict)
588 {
589 prop_dictionary_t cmddict;
590 prop_array_t commands;
591 prop_object_iterator_t iter;
592 int error;
593
594 error = quota_get_cmds(dict, &commands);
595 if (error) {
596 return error;
597 }
598
599 iter = prop_array_iterator(commands);
600 if (iter == NULL) {
601 return ENOMEM;
602 }
603
604 while ((cmddict = prop_object_iterator_next(iter)) != NULL) {
605 if (prop_object_type(cmddict) != PROP_TYPE_DICTIONARY) {
606 /* XXX shouldn't this be an error? */
607 continue;
608 }
609 error = vfs_quotactl_cmd(mp, cmddict);
610 if (error) {
611 break;
612 }
613 }
614 prop_object_iterator_release(iter);
615 return error;
616 }
617