quota_kernel.c revision 1.4.4.2 1 1.4.4.2 yamt /* $NetBSD: quota_kernel.c,v 1.4.4.2 2012/04/17 00:05:32 yamt Exp $ */
2 1.4.4.2 yamt /*-
3 1.4.4.2 yamt * Copyright (c) 2012 The NetBSD Foundation, Inc.
4 1.4.4.2 yamt * All rights reserved.
5 1.4.4.2 yamt *
6 1.4.4.2 yamt * This code is derived from software contributed to The NetBSD Foundation
7 1.4.4.2 yamt * by David A. Holland.
8 1.4.4.2 yamt *
9 1.4.4.2 yamt * Redistribution and use in source and binary forms, with or without
10 1.4.4.2 yamt * modification, are permitted provided that the following conditions
11 1.4.4.2 yamt * are met:
12 1.4.4.2 yamt * 1. Redistributions of source code must retain the above copyright
13 1.4.4.2 yamt * notice, this list of conditions and the following disclaimer.
14 1.4.4.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
15 1.4.4.2 yamt * notice, this list of conditions and the following disclaimer in the
16 1.4.4.2 yamt * documentation and/or other materials provided with the distribution.
17 1.4.4.2 yamt *
18 1.4.4.2 yamt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 1.4.4.2 yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 1.4.4.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 1.4.4.2 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 1.4.4.2 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 1.4.4.2 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 1.4.4.2 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 1.4.4.2 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 1.4.4.2 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 1.4.4.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 1.4.4.2 yamt * POSSIBILITY OF SUCH DAMAGE.
29 1.4.4.2 yamt */
30 1.4.4.2 yamt
31 1.4.4.2 yamt #include <sys/cdefs.h>
32 1.4.4.2 yamt __RCSID("$NetBSD: quota_kernel.c,v 1.4.4.2 2012/04/17 00:05:32 yamt Exp $");
33 1.4.4.2 yamt
34 1.4.4.2 yamt #include <stdlib.h>
35 1.4.4.2 yamt #include <err.h>
36 1.4.4.2 yamt #include <errno.h>
37 1.4.4.2 yamt #include <limits.h>
38 1.4.4.2 yamt
39 1.4.4.2 yamt #include <quota.h>
40 1.4.4.2 yamt #include <sys/quotactl.h>
41 1.4.4.2 yamt
42 1.4.4.2 yamt #include "quotapvt.h"
43 1.4.4.2 yamt
44 1.4.4.2 yamt struct kernel_quotacursor {
45 1.4.4.2 yamt /* just wrap the kernel interface type */
46 1.4.4.2 yamt struct quotakcursor kcursor;
47 1.4.4.2 yamt };
48 1.4.4.2 yamt
49 1.4.4.2 yamt static int
50 1.4.4.2 yamt __quota_kernel_stat(struct quotahandle *qh, struct quotastat *stat)
51 1.4.4.2 yamt {
52 1.4.4.2 yamt struct quotactl_args args;
53 1.4.4.2 yamt
54 1.4.4.2 yamt args.qc_op = QUOTACTL_STAT;
55 1.4.4.2 yamt args.u.stat.qc_info = stat;
56 1.4.4.2 yamt return __quotactl(qh->qh_mountpoint, &args);
57 1.4.4.2 yamt }
58 1.4.4.2 yamt
59 1.4.4.2 yamt const char *
60 1.4.4.2 yamt __quota_kernel_getimplname(struct quotahandle *qh)
61 1.4.4.2 yamt {
62 1.4.4.2 yamt static struct quotastat stat;
63 1.4.4.2 yamt
64 1.4.4.2 yamt if (__quota_kernel_stat(qh, &stat)) {
65 1.4.4.2 yamt return NULL;
66 1.4.4.2 yamt }
67 1.4.4.2 yamt return stat.qs_implname;
68 1.4.4.2 yamt }
69 1.4.4.2 yamt
70 1.4.4.2 yamt unsigned
71 1.4.4.2 yamt __quota_kernel_getrestrictions(struct quotahandle *qh)
72 1.4.4.2 yamt {
73 1.4.4.2 yamt struct quotastat stat;
74 1.4.4.2 yamt
75 1.4.4.2 yamt if (__quota_kernel_stat(qh, &stat)) {
76 1.4.4.2 yamt /* XXX no particularly satisfactory thing to do here */
77 1.4.4.2 yamt return 0;
78 1.4.4.2 yamt }
79 1.4.4.2 yamt return stat.qs_restrictions;
80 1.4.4.2 yamt }
81 1.4.4.2 yamt
82 1.4.4.2 yamt int
83 1.4.4.2 yamt __quota_kernel_getnumidtypes(struct quotahandle *qh)
84 1.4.4.2 yamt {
85 1.4.4.2 yamt struct quotastat stat;
86 1.4.4.2 yamt
87 1.4.4.2 yamt if (__quota_kernel_stat(qh, &stat)) {
88 1.4.4.2 yamt return 0;
89 1.4.4.2 yamt }
90 1.4.4.2 yamt return stat.qs_numidtypes;
91 1.4.4.2 yamt }
92 1.4.4.2 yamt
93 1.4.4.2 yamt const char *
94 1.4.4.2 yamt __quota_kernel_idtype_getname(struct quotahandle *qh, int idtype)
95 1.4.4.2 yamt {
96 1.4.4.2 yamt static struct quotaidtypestat stat;
97 1.4.4.2 yamt struct quotactl_args args;
98 1.4.4.2 yamt
99 1.4.4.2 yamt args.qc_op = QUOTACTL_IDTYPESTAT;
100 1.4.4.2 yamt args.u.idtypestat.qc_idtype = idtype;
101 1.4.4.2 yamt args.u.idtypestat.qc_info = &stat;
102 1.4.4.2 yamt if (__quotactl(qh->qh_mountpoint, &args)) {
103 1.4.4.2 yamt return NULL;
104 1.4.4.2 yamt }
105 1.4.4.2 yamt return stat.qis_name;
106 1.4.4.2 yamt }
107 1.4.4.2 yamt
108 1.4.4.2 yamt int
109 1.4.4.2 yamt __quota_kernel_getnumobjtypes(struct quotahandle *qh)
110 1.4.4.2 yamt {
111 1.4.4.2 yamt struct quotastat stat;
112 1.4.4.2 yamt
113 1.4.4.2 yamt if (__quota_kernel_stat(qh, &stat)) {
114 1.4.4.2 yamt return 0;
115 1.4.4.2 yamt }
116 1.4.4.2 yamt return stat.qs_numobjtypes;
117 1.4.4.2 yamt }
118 1.4.4.2 yamt
119 1.4.4.2 yamt const char *
120 1.4.4.2 yamt __quota_kernel_objtype_getname(struct quotahandle *qh, int objtype)
121 1.4.4.2 yamt {
122 1.4.4.2 yamt static struct quotaobjtypestat stat;
123 1.4.4.2 yamt struct quotactl_args args;
124 1.4.4.2 yamt
125 1.4.4.2 yamt args.qc_op = QUOTACTL_OBJTYPESTAT;
126 1.4.4.2 yamt args.u.objtypestat.qc_objtype = objtype;
127 1.4.4.2 yamt args.u.objtypestat.qc_info = &stat;
128 1.4.4.2 yamt if (__quotactl(qh->qh_mountpoint, &args)) {
129 1.4.4.2 yamt return NULL;
130 1.4.4.2 yamt }
131 1.4.4.2 yamt return stat.qos_name;
132 1.4.4.2 yamt }
133 1.4.4.2 yamt
134 1.4.4.2 yamt int
135 1.4.4.2 yamt __quota_kernel_objtype_isbytes(struct quotahandle *qh, int objtype)
136 1.4.4.2 yamt {
137 1.4.4.2 yamt struct quotaobjtypestat stat;
138 1.4.4.2 yamt struct quotactl_args args;
139 1.4.4.2 yamt
140 1.4.4.2 yamt args.qc_op = QUOTACTL_OBJTYPESTAT;
141 1.4.4.2 yamt args.u.objtypestat.qc_objtype = objtype;
142 1.4.4.2 yamt args.u.objtypestat.qc_info = &stat;
143 1.4.4.2 yamt if (__quotactl(qh->qh_mountpoint, &args)) {
144 1.4.4.2 yamt return 0;
145 1.4.4.2 yamt }
146 1.4.4.2 yamt return stat.qos_isbytes;
147 1.4.4.2 yamt }
148 1.4.4.2 yamt
149 1.4.4.2 yamt int
150 1.4.4.2 yamt __quota_kernel_quotaon(struct quotahandle *qh, int idtype)
151 1.4.4.2 yamt {
152 1.4.4.2 yamt struct quotactl_args args;
153 1.4.4.2 yamt const char *file;
154 1.4.4.2 yamt char path[PATH_MAX];
155 1.4.4.2 yamt
156 1.4.4.2 yamt /*
157 1.4.4.2 yamt * Note that while it is an error to call quotaon on something
158 1.4.4.2 yamt * that isn't a volume with old-style quotas that expects
159 1.4.4.2 yamt * quotaon to be called, it's not our responsibility to check
160 1.4.4.2 yamt * for that; the filesystem will. Also note that it is not an
161 1.4.4.2 yamt * error to call quotaon repeatedly -- apparently this is to
162 1.4.4.2 yamt * permit changing the quota file in use on the fly or
163 1.4.4.2 yamt * something. So all we need to do here is ask the oldfiles
164 1.4.4.2 yamt * code if the mount option was set in fstab and fetch back
165 1.4.4.2 yamt * the filename.
166 1.4.4.2 yamt */
167 1.4.4.2 yamt
168 1.4.4.2 yamt file = __quota_oldfiles_getquotafile(qh, idtype, path, sizeof(path));
169 1.4.4.2 yamt if (file == NULL) {
170 1.4.4.2 yamt /*
171 1.4.4.2 yamt * This idtype (or maybe any idtype) was not enabled
172 1.4.4.2 yamt * in fstab.
173 1.4.4.2 yamt */
174 1.4.4.2 yamt errno = ENXIO;
175 1.4.4.2 yamt return -1;
176 1.4.4.2 yamt }
177 1.4.4.2 yamt
178 1.4.4.2 yamt args.qc_op = QUOTACTL_QUOTAON;
179 1.4.4.2 yamt args.u.quotaon.qc_idtype = idtype;
180 1.4.4.2 yamt args.u.quotaon.qc_quotafile = file;
181 1.4.4.2 yamt return __quotactl(qh->qh_mountpoint, &args);
182 1.4.4.2 yamt }
183 1.4.4.2 yamt
184 1.4.4.2 yamt int
185 1.4.4.2 yamt __quota_kernel_quotaoff(struct quotahandle *qh, int idtype)
186 1.4.4.2 yamt {
187 1.4.4.2 yamt struct quotactl_args args;
188 1.4.4.2 yamt
189 1.4.4.2 yamt args.qc_op = QUOTACTL_QUOTAOFF;
190 1.4.4.2 yamt args.u.quotaoff.qc_idtype = idtype;
191 1.4.4.2 yamt return __quotactl(qh->qh_mountpoint, &args);
192 1.4.4.2 yamt }
193 1.4.4.2 yamt
194 1.4.4.2 yamt int
195 1.4.4.2 yamt __quota_kernel_get(struct quotahandle *qh, const struct quotakey *qk,
196 1.4.4.2 yamt struct quotaval *qv)
197 1.4.4.2 yamt {
198 1.4.4.2 yamt struct quotactl_args args;
199 1.4.4.2 yamt
200 1.4.4.2 yamt args.qc_op = QUOTACTL_GET;
201 1.4.4.2 yamt args.u.get.qc_key = qk;
202 1.4.4.2 yamt args.u.get.qc_val = qv;
203 1.4.4.2 yamt return __quotactl(qh->qh_mountpoint, &args);
204 1.4.4.2 yamt }
205 1.4.4.2 yamt
206 1.4.4.2 yamt int
207 1.4.4.2 yamt __quota_kernel_put(struct quotahandle *qh, const struct quotakey *qk,
208 1.4.4.2 yamt const struct quotaval *qv)
209 1.4.4.2 yamt {
210 1.4.4.2 yamt struct quotactl_args args;
211 1.4.4.2 yamt
212 1.4.4.2 yamt args.qc_op = QUOTACTL_PUT;
213 1.4.4.2 yamt args.u.put.qc_key = qk;
214 1.4.4.2 yamt args.u.put.qc_val = qv;
215 1.4.4.2 yamt return __quotactl(qh->qh_mountpoint, &args);
216 1.4.4.2 yamt }
217 1.4.4.2 yamt
218 1.4.4.2 yamt int
219 1.4.4.2 yamt __quota_kernel_delete(struct quotahandle *qh, const struct quotakey *qk)
220 1.4.4.2 yamt {
221 1.4.4.2 yamt struct quotactl_args args;
222 1.4.4.2 yamt
223 1.4.4.2 yamt args.qc_op = QUOTACTL_DELETE;
224 1.4.4.2 yamt args.u.delete.qc_key = qk;
225 1.4.4.2 yamt return __quotactl(qh->qh_mountpoint, &args);
226 1.4.4.2 yamt }
227 1.4.4.2 yamt
228 1.4.4.2 yamt struct kernel_quotacursor *
229 1.4.4.2 yamt __quota_kernel_cursor_create(struct quotahandle *qh)
230 1.4.4.2 yamt {
231 1.4.4.2 yamt struct quotactl_args args;
232 1.4.4.2 yamt struct kernel_quotacursor *cursor;
233 1.4.4.2 yamt int sverrno;
234 1.4.4.2 yamt
235 1.4.4.2 yamt cursor = malloc(sizeof(*cursor));
236 1.4.4.2 yamt if (cursor == NULL) {
237 1.4.4.2 yamt return NULL;
238 1.4.4.2 yamt }
239 1.4.4.2 yamt
240 1.4.4.2 yamt args.qc_op = QUOTACTL_CURSOROPEN;
241 1.4.4.2 yamt args.u.cursoropen.qc_cursor = &cursor->kcursor;
242 1.4.4.2 yamt if (__quotactl(qh->qh_mountpoint, &args)) {
243 1.4.4.2 yamt sverrno = errno;
244 1.4.4.2 yamt free(cursor);
245 1.4.4.2 yamt errno = sverrno;
246 1.4.4.2 yamt return NULL;
247 1.4.4.2 yamt }
248 1.4.4.2 yamt return cursor;
249 1.4.4.2 yamt }
250 1.4.4.2 yamt
251 1.4.4.2 yamt void
252 1.4.4.2 yamt __quota_kernel_cursor_destroy(struct quotahandle *qh,
253 1.4.4.2 yamt struct kernel_quotacursor *cursor)
254 1.4.4.2 yamt {
255 1.4.4.2 yamt struct quotactl_args args;
256 1.4.4.2 yamt
257 1.4.4.2 yamt args.qc_op = QUOTACTL_CURSORCLOSE;
258 1.4.4.2 yamt args.u.cursorclose.qc_cursor = &cursor->kcursor;
259 1.4.4.2 yamt if (__quotactl(qh->qh_mountpoint, &args)) {
260 1.4.4.2 yamt /* XXX should we really print from inside the library? */
261 1.4.4.2 yamt warn("__quotactl cursorclose");
262 1.4.4.2 yamt }
263 1.4.4.2 yamt free(cursor);
264 1.4.4.2 yamt }
265 1.4.4.2 yamt
266 1.4.4.2 yamt int
267 1.4.4.2 yamt __quota_kernel_cursor_skipidtype(struct quotahandle *qh,
268 1.4.4.2 yamt struct kernel_quotacursor *cursor,
269 1.4.4.2 yamt int idtype)
270 1.4.4.2 yamt {
271 1.4.4.2 yamt struct quotactl_args args;
272 1.4.4.2 yamt
273 1.4.4.2 yamt args.qc_op = QUOTACTL_CURSORSKIPIDTYPE;
274 1.4.4.2 yamt args.u.cursorskipidtype.qc_cursor = &cursor->kcursor;
275 1.4.4.2 yamt args.u.cursorskipidtype.qc_idtype = idtype;
276 1.4.4.2 yamt return __quotactl(qh->qh_mountpoint, &args);
277 1.4.4.2 yamt }
278 1.4.4.2 yamt
279 1.4.4.2 yamt int
280 1.4.4.2 yamt __quota_kernel_cursor_get(struct quotahandle *qh,
281 1.4.4.2 yamt struct kernel_quotacursor *cursor,
282 1.4.4.2 yamt struct quotakey *key, struct quotaval *val)
283 1.4.4.2 yamt {
284 1.4.4.2 yamt int ret;
285 1.4.4.2 yamt
286 1.4.4.2 yamt ret = __quota_kernel_cursor_getn(qh, cursor, key, val, 1);
287 1.4.4.2 yamt if (ret < 0) {
288 1.4.4.2 yamt return -1;
289 1.4.4.2 yamt }
290 1.4.4.2 yamt return 0;
291 1.4.4.2 yamt }
292 1.4.4.2 yamt
293 1.4.4.2 yamt int
294 1.4.4.2 yamt __quota_kernel_cursor_getn(struct quotahandle *qh,
295 1.4.4.2 yamt struct kernel_quotacursor *cursor,
296 1.4.4.2 yamt struct quotakey *keys, struct quotaval *vals,
297 1.4.4.2 yamt unsigned maxnum)
298 1.4.4.2 yamt {
299 1.4.4.2 yamt struct quotactl_args args;
300 1.4.4.2 yamt unsigned ret;
301 1.4.4.2 yamt
302 1.4.4.2 yamt if (maxnum > INT_MAX) {
303 1.4.4.2 yamt /* joker, eh? */
304 1.4.4.2 yamt errno = EINVAL;
305 1.4.4.2 yamt return -1;
306 1.4.4.2 yamt }
307 1.4.4.2 yamt
308 1.4.4.2 yamt args.qc_op = QUOTACTL_CURSORGET;
309 1.4.4.2 yamt args.u.cursorget.qc_cursor = &cursor->kcursor;
310 1.4.4.2 yamt args.u.cursorget.qc_keys = keys;
311 1.4.4.2 yamt args.u.cursorget.qc_vals = vals;
312 1.4.4.2 yamt args.u.cursorget.qc_maxnum = maxnum;
313 1.4.4.2 yamt args.u.cursorget.qc_ret = &ret;
314 1.4.4.2 yamt if (__quotactl(qh->qh_mountpoint, &args) < 0) {
315 1.4.4.2 yamt return -1;
316 1.4.4.2 yamt }
317 1.4.4.2 yamt return ret;
318 1.4.4.2 yamt }
319 1.4.4.2 yamt
320 1.4.4.2 yamt int
321 1.4.4.2 yamt __quota_kernel_cursor_atend(struct quotahandle *qh,
322 1.4.4.2 yamt struct kernel_quotacursor *cursor)
323 1.4.4.2 yamt {
324 1.4.4.2 yamt struct quotactl_args args;
325 1.4.4.2 yamt int ret;
326 1.4.4.2 yamt
327 1.4.4.2 yamt args.qc_op = QUOTACTL_CURSORATEND;
328 1.4.4.2 yamt args.u.cursoratend.qc_cursor = &cursor->kcursor;
329 1.4.4.2 yamt args.u.cursoratend.qc_ret = &ret;
330 1.4.4.2 yamt if (__quotactl(qh->qh_mountpoint, &args)) {
331 1.4.4.2 yamt /*
332 1.4.4.2 yamt * Return -1 so naive callers, who test for the return
333 1.4.4.2 yamt * value being nonzero, stop iterating, and
334 1.4.4.2 yamt * sophisticated callers can tell an error from
335 1.4.4.2 yamt * end-of-data.
336 1.4.4.2 yamt */
337 1.4.4.2 yamt return -1;
338 1.4.4.2 yamt }
339 1.4.4.2 yamt return ret;
340 1.4.4.2 yamt }
341 1.4.4.2 yamt
342 1.4.4.2 yamt int
343 1.4.4.2 yamt __quota_kernel_cursor_rewind(struct quotahandle *qh,
344 1.4.4.2 yamt struct kernel_quotacursor *cursor)
345 1.4.4.2 yamt {
346 1.4.4.2 yamt struct quotactl_args args;
347 1.4.4.2 yamt
348 1.4.4.2 yamt args.qc_op = QUOTACTL_CURSORREWIND;
349 1.4.4.2 yamt args.u.cursorrewind.qc_cursor = &cursor->kcursor;
350 1.4.4.2 yamt return __quotactl(qh->qh_mountpoint, &args);
351 1.4.4.2 yamt }
352