quota_oldfiles.c revision 1.5 1 1.5 dholland /* $NetBSD: quota_oldfiles.c,v 1.5 2012/01/30 16:45:13 dholland Exp $ */
2 1.1 dholland
3 1.1 dholland /*
4 1.1 dholland * Copyright (c) 1980, 1990, 1993
5 1.1 dholland * The Regents of the University of California. All rights reserved.
6 1.1 dholland *
7 1.1 dholland * This code is derived from software contributed to Berkeley by
8 1.1 dholland * Robert Elz at The University of Melbourne.
9 1.1 dholland *
10 1.1 dholland * Redistribution and use in source and binary forms, with or without
11 1.1 dholland * modification, are permitted provided that the following conditions
12 1.1 dholland * are met:
13 1.1 dholland * 1. Redistributions of source code must retain the above copyright
14 1.1 dholland * notice, this list of conditions and the following disclaimer.
15 1.1 dholland * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 dholland * notice, this list of conditions and the following disclaimer in the
17 1.1 dholland * documentation and/or other materials provided with the distribution.
18 1.1 dholland * 3. Neither the name of the University nor the names of its contributors
19 1.1 dholland * may be used to endorse or promote products derived from this software
20 1.1 dholland * without specific prior written permission.
21 1.1 dholland *
22 1.1 dholland * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 dholland * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 dholland * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 dholland * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 dholland * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 dholland * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 dholland * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 dholland * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 dholland * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 dholland * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 dholland * SUCH DAMAGE.
33 1.1 dholland */
34 1.1 dholland
35 1.1 dholland #include <sys/types.h>
36 1.1 dholland #include <sys/stat.h>
37 1.1 dholland #include <stdio.h>
38 1.1 dholland #include <stdlib.h>
39 1.1 dholland #include <string.h>
40 1.1 dholland #include <unistd.h>
41 1.1 dholland #include <fcntl.h>
42 1.1 dholland #include <limits.h>
43 1.1 dholland #include <fstab.h>
44 1.1 dholland #include <errno.h>
45 1.1 dholland #include <err.h>
46 1.1 dholland
47 1.1 dholland #include <ufs/ufs/quota1.h>
48 1.1 dholland
49 1.1 dholland #include <quota.h>
50 1.1 dholland #include "quotapvt.h"
51 1.1 dholland
52 1.3 dholland struct oldfiles_fstabentry {
53 1.3 dholland char *ofe_mountpoint;
54 1.3 dholland int ofe_hasuserquota;
55 1.3 dholland int ofe_hasgroupquota;
56 1.3 dholland char *ofe_userquotafile;
57 1.3 dholland char *ofe_groupquotafile;
58 1.3 dholland };
59 1.3 dholland
60 1.1 dholland struct oldfiles_quotacursor {
61 1.1 dholland unsigned oqc_doingusers;
62 1.1 dholland unsigned oqc_doinggroups;
63 1.1 dholland
64 1.1 dholland unsigned oqc_numusers;
65 1.1 dholland unsigned oqc_numgroups;
66 1.1 dholland
67 1.1 dholland unsigned oqc_didusers;
68 1.1 dholland unsigned oqc_didgroups;
69 1.1 dholland unsigned oqc_diddefault;
70 1.1 dholland unsigned oqc_pos;
71 1.1 dholland unsigned oqc_didblocks;
72 1.1 dholland };
73 1.1 dholland
74 1.3 dholland static struct oldfiles_fstabentry *__quota_oldfiles_fstab;
75 1.3 dholland static unsigned __quota_oldfiles_numfstab;
76 1.3 dholland static unsigned __quota_oldfiles_maxfstab;
77 1.3 dholland static int __quota_oldfiles_fstab_loaded;
78 1.3 dholland
79 1.3 dholland static const struct oldfiles_fstabentry *
80 1.3 dholland __quota_oldfiles_find_fstabentry(const char *mountpoint)
81 1.3 dholland {
82 1.3 dholland unsigned i;
83 1.3 dholland
84 1.3 dholland for (i = 0; i < __quota_oldfiles_numfstab; i++) {
85 1.3 dholland if (!strcmp(mountpoint,
86 1.3 dholland __quota_oldfiles_fstab[i].ofe_mountpoint)) {
87 1.3 dholland return &__quota_oldfiles_fstab[i];
88 1.3 dholland }
89 1.3 dholland }
90 1.3 dholland return NULL;
91 1.3 dholland }
92 1.3 dholland
93 1.3 dholland static int
94 1.3 dholland __quota_oldfiles_add_fstabentry(struct oldfiles_fstabentry *ofe)
95 1.3 dholland {
96 1.3 dholland unsigned newmax;
97 1.3 dholland struct oldfiles_fstabentry *newptr;
98 1.3 dholland
99 1.3 dholland if (__quota_oldfiles_numfstab + 1 >= __quota_oldfiles_maxfstab) {
100 1.3 dholland if (__quota_oldfiles_maxfstab == 0) {
101 1.3 dholland newmax = 4;
102 1.3 dholland } else {
103 1.3 dholland newmax = __quota_oldfiles_maxfstab * 2;
104 1.3 dholland }
105 1.3 dholland newptr = realloc(__quota_oldfiles_fstab,
106 1.3 dholland newmax * sizeof(__quota_oldfiles_fstab[0]));
107 1.3 dholland if (newptr == NULL) {
108 1.3 dholland return -1;
109 1.3 dholland }
110 1.3 dholland __quota_oldfiles_maxfstab = newmax;
111 1.3 dholland __quota_oldfiles_fstab = newptr;
112 1.3 dholland }
113 1.3 dholland
114 1.3 dholland __quota_oldfiles_fstab[__quota_oldfiles_numfstab++] = *ofe;
115 1.3 dholland return 0;
116 1.3 dholland }
117 1.3 dholland
118 1.3 dholland static int
119 1.3 dholland __quota_oldfiles_fill_fstabentry(const struct fstab *fs,
120 1.3 dholland struct oldfiles_fstabentry *ofe)
121 1.3 dholland {
122 1.4 dholland char buf[256];
123 1.3 dholland char *opt, *state, *s;
124 1.3 dholland int serrno;
125 1.3 dholland int ret = 0;
126 1.3 dholland
127 1.3 dholland /*
128 1.3 dholland * Inspect the mount options to find the quota files.
129 1.3 dholland * XXX this info should be gotten from the kernel.
130 1.3 dholland *
131 1.3 dholland * The options are:
132 1.3 dholland * userquota[=path] enable user quotas
133 1.3 dholland * groupquota[=path] enable group quotas
134 1.3 dholland */
135 1.3 dholland
136 1.3 dholland ofe->ofe_mountpoint = NULL;
137 1.3 dholland ofe->ofe_hasuserquota = ofe->ofe_hasgroupquota = 0;
138 1.3 dholland ofe->ofe_userquotafile = ofe->ofe_groupquotafile = NULL;
139 1.3 dholland
140 1.3 dholland strlcpy(buf, fs->fs_mntops, sizeof(buf));
141 1.3 dholland for (opt = strtok_r(buf, ",", &state);
142 1.3 dholland opt != NULL;
143 1.3 dholland opt = strtok_r(NULL, ",", &state)) {
144 1.3 dholland s = strchr(opt, '=');
145 1.3 dholland if (s != NULL) {
146 1.3 dholland *(s++) = '\0';
147 1.3 dholland }
148 1.3 dholland if (!strcmp(opt, "userquota")) {
149 1.3 dholland ret = 1;
150 1.3 dholland ofe->ofe_hasuserquota = 1;
151 1.3 dholland if (s != NULL) {
152 1.3 dholland ofe->ofe_userquotafile = strdup(s);
153 1.3 dholland if (ofe->ofe_userquotafile == NULL) {
154 1.3 dholland goto fail;
155 1.3 dholland }
156 1.3 dholland }
157 1.3 dholland } else if (!strcmp(opt, "groupquota")) {
158 1.3 dholland ret = 1;
159 1.3 dholland ofe->ofe_hasgroupquota = 1;
160 1.3 dholland if (s != NULL) {
161 1.3 dholland ofe->ofe_groupquotafile = strdup(s);
162 1.3 dholland if (ofe->ofe_groupquotafile == NULL) {
163 1.3 dholland goto fail;
164 1.3 dholland }
165 1.3 dholland }
166 1.3 dholland }
167 1.3 dholland }
168 1.3 dholland
169 1.3 dholland if (ret == 1) {
170 1.3 dholland ofe->ofe_mountpoint = strdup(fs->fs_file);
171 1.3 dholland if (ofe->ofe_mountpoint == NULL) {
172 1.3 dholland goto fail;
173 1.3 dholland }
174 1.3 dholland }
175 1.3 dholland
176 1.3 dholland return ret;
177 1.3 dholland
178 1.3 dholland fail:
179 1.3 dholland serrno = errno;
180 1.3 dholland if (ofe->ofe_mountpoint != NULL) {
181 1.3 dholland free(ofe->ofe_mountpoint);
182 1.3 dholland }
183 1.3 dholland if (ofe->ofe_groupquotafile != NULL) {
184 1.3 dholland free(ofe->ofe_groupquotafile);
185 1.3 dholland }
186 1.3 dholland if (ofe->ofe_userquotafile != NULL) {
187 1.3 dholland free(ofe->ofe_userquotafile);
188 1.3 dholland }
189 1.3 dholland errno = serrno;
190 1.3 dholland return -1;
191 1.3 dholland }
192 1.3 dholland
193 1.3 dholland void
194 1.3 dholland __quota_oldfiles_load_fstab(void)
195 1.3 dholland {
196 1.3 dholland struct oldfiles_fstabentry ofe;
197 1.3 dholland struct fstab *fs;
198 1.3 dholland int result;
199 1.3 dholland
200 1.3 dholland if (__quota_oldfiles_fstab_loaded) {
201 1.3 dholland return;
202 1.3 dholland }
203 1.3 dholland
204 1.3 dholland /*
205 1.3 dholland * XXX: should be able to handle ext2fs quota1 files too
206 1.3 dholland *
207 1.3 dholland * XXX: should use getfsent_r(), but there isn't one.
208 1.3 dholland */
209 1.3 dholland setfsent();
210 1.3 dholland while ((fs = getfsent()) != NULL) {
211 1.3 dholland if (!strcmp(fs->fs_vfstype, "ffs") ||
212 1.3 dholland !strcmp(fs->fs_vfstype, "lfs")) {
213 1.3 dholland result = __quota_oldfiles_fill_fstabentry(fs, &ofe);
214 1.3 dholland if (result == -1) {
215 1.3 dholland goto failed;
216 1.3 dholland }
217 1.3 dholland if (result == 0) {
218 1.3 dholland continue;
219 1.3 dholland }
220 1.3 dholland if (__quota_oldfiles_add_fstabentry(&ofe)) {
221 1.3 dholland goto failed;
222 1.3 dholland }
223 1.3 dholland }
224 1.3 dholland }
225 1.3 dholland endfsent();
226 1.3 dholland __quota_oldfiles_fstab_loaded = 1;
227 1.3 dholland
228 1.3 dholland return;
229 1.3 dholland failed:
230 1.3 dholland warn("Failed reading fstab");
231 1.3 dholland return;
232 1.3 dholland }
233 1.3 dholland
234 1.3 dholland int
235 1.3 dholland __quota_oldfiles_infstab(const char *mountpoint)
236 1.3 dholland {
237 1.3 dholland return __quota_oldfiles_find_fstabentry(mountpoint) != NULL;
238 1.3 dholland }
239 1.3 dholland
240 1.5 dholland static void
241 1.5 dholland __quota_oldfiles_defquotafile(struct quotahandle *qh, int idtype,
242 1.5 dholland char *buf, size_t maxlen)
243 1.5 dholland {
244 1.5 dholland static const char *const names[] = INITQFNAMES;
245 1.5 dholland
246 1.5 dholland (void)snprintf(buf, maxlen, "%s/%s.%s",
247 1.5 dholland qh->qh_mountpoint,
248 1.5 dholland QUOTAFILENAME, names[USRQUOTA]);
249 1.5 dholland }
250 1.5 dholland
251 1.5 dholland const char *
252 1.5 dholland __quota_oldfiles_getquotafile(struct quotahandle *qh, int idtype,
253 1.5 dholland char *buf, size_t maxlen)
254 1.5 dholland {
255 1.5 dholland const struct oldfiles_fstabentry *ofe;
256 1.5 dholland const char *file;
257 1.5 dholland
258 1.5 dholland ofe = __quota_oldfiles_find_fstabentry(qh->qh_mountpoint);
259 1.5 dholland if (ofe == NULL) {
260 1.5 dholland errno = ENXIO;
261 1.5 dholland return NULL;
262 1.5 dholland }
263 1.5 dholland
264 1.5 dholland switch (idtype) {
265 1.5 dholland case USRQUOTA:
266 1.5 dholland if (!ofe->ofe_hasuserquota) {
267 1.5 dholland errno = ENXIO;
268 1.5 dholland return NULL;
269 1.5 dholland }
270 1.5 dholland file = ofe->ofe_userquotafile;
271 1.5 dholland break;
272 1.5 dholland case GRPQUOTA:
273 1.5 dholland if (!ofe->ofe_hasgroupquota) {
274 1.5 dholland errno = ENXIO;
275 1.5 dholland return NULL;
276 1.5 dholland }
277 1.5 dholland file = ofe->ofe_groupquotafile;
278 1.5 dholland break;
279 1.5 dholland default:
280 1.5 dholland errno = EINVAL;
281 1.5 dholland return NULL;
282 1.5 dholland }
283 1.5 dholland
284 1.5 dholland if (file == NULL) {
285 1.5 dholland __quota_oldfiles_defquotafile(qh, idtype, buf, maxlen);
286 1.5 dholland file = buf;
287 1.5 dholland }
288 1.5 dholland return file;
289 1.5 dholland }
290 1.5 dholland
291 1.1 dholland static uint64_t
292 1.2 dholland dqblk_getlimit(uint32_t val)
293 1.1 dholland {
294 1.1 dholland if (val == 0) {
295 1.1 dholland return QUOTA_NOLIMIT;
296 1.1 dholland } else {
297 1.1 dholland return val - 1;
298 1.1 dholland }
299 1.1 dholland }
300 1.1 dholland
301 1.2 dholland static uint32_t
302 1.2 dholland dqblk_setlimit(uint64_t val)
303 1.2 dholland {
304 1.2 dholland if (val == QUOTA_NOLIMIT && val >= 0xffffffffUL) {
305 1.2 dholland return 0;
306 1.2 dholland } else {
307 1.2 dholland return (uint32_t)val + 1;
308 1.2 dholland }
309 1.2 dholland }
310 1.2 dholland
311 1.1 dholland static void
312 1.1 dholland dqblk_getblocks(const struct dqblk *dq, struct quotaval *qv)
313 1.1 dholland {
314 1.2 dholland qv->qv_hardlimit = dqblk_getlimit(dq->dqb_bhardlimit);
315 1.2 dholland qv->qv_softlimit = dqblk_getlimit(dq->dqb_bsoftlimit);
316 1.1 dholland qv->qv_usage = dq->dqb_curblocks;
317 1.1 dholland qv->qv_expiretime = dq->dqb_btime;
318 1.1 dholland qv->qv_grace = QUOTA_NOTIME;
319 1.1 dholland }
320 1.1 dholland
321 1.1 dholland static void
322 1.1 dholland dqblk_getfiles(const struct dqblk *dq, struct quotaval *qv)
323 1.1 dholland {
324 1.2 dholland qv->qv_hardlimit = dqblk_getlimit(dq->dqb_ihardlimit);
325 1.2 dholland qv->qv_softlimit = dqblk_getlimit(dq->dqb_isoftlimit);
326 1.1 dholland qv->qv_usage = dq->dqb_curinodes;
327 1.1 dholland qv->qv_expiretime = dq->dqb_itime;
328 1.1 dholland qv->qv_grace = QUOTA_NOTIME;
329 1.1 dholland }
330 1.1 dholland
331 1.2 dholland static void
332 1.2 dholland dqblk_putblocks(const struct quotaval *qv, struct dqblk *dq)
333 1.2 dholland {
334 1.2 dholland dq->dqb_bhardlimit = dqblk_setlimit(qv->qv_hardlimit);
335 1.2 dholland dq->dqb_bsoftlimit = dqblk_setlimit(qv->qv_softlimit);
336 1.2 dholland dq->dqb_curblocks = qv->qv_usage;
337 1.2 dholland dq->dqb_btime = qv->qv_expiretime;
338 1.2 dholland /* ignore qv->qv_grace */
339 1.2 dholland }
340 1.2 dholland
341 1.2 dholland static void
342 1.2 dholland dqblk_putfiles(const struct quotaval *qv, struct dqblk *dq)
343 1.2 dholland {
344 1.2 dholland dq->dqb_ihardlimit = dqblk_setlimit(qv->qv_hardlimit);
345 1.2 dholland dq->dqb_isoftlimit = dqblk_setlimit(qv->qv_softlimit);
346 1.2 dholland dq->dqb_curinodes = qv->qv_usage;
347 1.2 dholland dq->dqb_itime = qv->qv_expiretime;
348 1.2 dholland /* ignore qv->qv_grace */
349 1.2 dholland }
350 1.2 dholland
351 1.1 dholland static int
352 1.1 dholland __quota_oldfiles_open(struct quotahandle *qh, const char *path, int *fd_ret)
353 1.1 dholland {
354 1.1 dholland int fd;
355 1.1 dholland
356 1.1 dholland fd = open(path, O_RDWR);
357 1.1 dholland if (fd < 0 && (errno == EACCES || errno == EROFS)) {
358 1.1 dholland fd = open(path, O_RDONLY);
359 1.1 dholland if (fd < 0) {
360 1.1 dholland return -1;
361 1.1 dholland }
362 1.1 dholland }
363 1.1 dholland *fd_ret = fd;
364 1.1 dholland return 0;
365 1.1 dholland }
366 1.1 dholland
367 1.1 dholland int
368 1.1 dholland __quota_oldfiles_initialize(struct quotahandle *qh)
369 1.1 dholland {
370 1.3 dholland const struct oldfiles_fstabentry *ofe;
371 1.1 dholland char path[PATH_MAX];
372 1.1 dholland const char *userquotafile, *groupquotafile;
373 1.1 dholland
374 1.3 dholland if (qh->qh_oldfilesopen) {
375 1.1 dholland /* already initialized */
376 1.1 dholland return 0;
377 1.1 dholland }
378 1.1 dholland
379 1.1 dholland /*
380 1.1 dholland * Find the fstab entry.
381 1.1 dholland */
382 1.3 dholland ofe = __quota_oldfiles_find_fstabentry(qh->qh_mountpoint);
383 1.3 dholland if (ofe == NULL) {
384 1.1 dholland warnx("%s not found in fstab", qh->qh_mountpoint);
385 1.1 dholland errno = ENXIO;
386 1.1 dholland return -1;
387 1.1 dholland }
388 1.1 dholland
389 1.3 dholland if (!ofe->ofe_hasuserquota && !ofe->ofe_hasgroupquota) {
390 1.1 dholland errno = ENXIO;
391 1.1 dholland return -1;
392 1.1 dholland }
393 1.1 dholland
394 1.3 dholland if (ofe->ofe_hasuserquota) {
395 1.3 dholland userquotafile = ofe->ofe_userquotafile;
396 1.1 dholland if (userquotafile == NULL) {
397 1.5 dholland __quota_oldfiles_defquotafile(qh, USRQUOTA,
398 1.5 dholland path, sizeof(path));
399 1.1 dholland userquotafile = path;
400 1.1 dholland }
401 1.1 dholland if (__quota_oldfiles_open(qh, userquotafile,
402 1.1 dholland &qh->qh_userfile)) {
403 1.1 dholland return -1;
404 1.1 dholland }
405 1.1 dholland }
406 1.3 dholland if (ofe->ofe_hasgroupquota) {
407 1.3 dholland groupquotafile = ofe->ofe_groupquotafile;
408 1.1 dholland if (groupquotafile == NULL) {
409 1.5 dholland __quota_oldfiles_defquotafile(qh, GRPQUOTA,
410 1.5 dholland path, sizeof(path));
411 1.1 dholland groupquotafile = path;
412 1.1 dholland }
413 1.1 dholland if (__quota_oldfiles_open(qh, groupquotafile,
414 1.1 dholland &qh->qh_groupfile)) {
415 1.1 dholland return -1;
416 1.1 dholland }
417 1.1 dholland }
418 1.1 dholland
419 1.3 dholland qh->qh_oldfilesopen = 1;
420 1.1 dholland
421 1.1 dholland return 0;
422 1.1 dholland }
423 1.1 dholland
424 1.1 dholland const char *
425 1.1 dholland __quota_oldfiles_getimplname(struct quotahandle *qh)
426 1.1 dholland {
427 1.3 dholland return "ufs/ffs quota v1 file access";
428 1.1 dholland }
429 1.1 dholland
430 1.5 dholland int
431 1.5 dholland __quota_oldfiles_quotaon(struct quotahandle *qh, int idtype)
432 1.5 dholland {
433 1.5 dholland int result;
434 1.5 dholland
435 1.5 dholland /*
436 1.5 dholland * If we have the quota files open, close them.
437 1.5 dholland */
438 1.5 dholland
439 1.5 dholland if (qh->qh_oldfilesopen) {
440 1.5 dholland if (qh->qh_userfile >= 0) {
441 1.5 dholland close(qh->qh_userfile);
442 1.5 dholland qh->qh_userfile = -1;
443 1.5 dholland }
444 1.5 dholland if (qh->qh_groupfile >= 0) {
445 1.5 dholland close(qh->qh_groupfile);
446 1.5 dholland qh->qh_groupfile = -1;
447 1.5 dholland }
448 1.5 dholland qh->qh_oldfilesopen = 0;
449 1.5 dholland }
450 1.5 dholland
451 1.5 dholland /*
452 1.5 dholland * Go over to the syscall interface.
453 1.5 dholland */
454 1.5 dholland
455 1.5 dholland result = __quota_proplib_quotaon(qh, idtype);
456 1.5 dholland if (result < 0) {
457 1.5 dholland return -1;
458 1.5 dholland }
459 1.5 dholland
460 1.5 dholland /*
461 1.5 dholland * We succeeded, so all further access should be via the
462 1.5 dholland * kernel.
463 1.5 dholland */
464 1.5 dholland
465 1.5 dholland qh->qh_mode = QUOTA_MODE_PROPLIB;
466 1.5 dholland return 0;
467 1.5 dholland }
468 1.5 dholland
469 1.1 dholland static int
470 1.1 dholland __quota_oldfiles_doget(struct quotahandle *qh, const struct quotakey *qk,
471 1.1 dholland struct quotaval *qv, int *isallzero)
472 1.1 dholland {
473 1.1 dholland int file;
474 1.1 dholland off_t pos;
475 1.1 dholland struct dqblk dq;
476 1.1 dholland ssize_t result;
477 1.1 dholland
478 1.3 dholland if (!qh->qh_oldfilesopen) {
479 1.3 dholland if (__quota_oldfiles_initialize(qh)) {
480 1.3 dholland return -1;
481 1.3 dholland }
482 1.3 dholland }
483 1.3 dholland
484 1.1 dholland switch (qk->qk_idtype) {
485 1.1 dholland case QUOTA_IDTYPE_USER:
486 1.1 dholland file = qh->qh_userfile;
487 1.1 dholland break;
488 1.1 dholland case QUOTA_IDTYPE_GROUP:
489 1.1 dholland file = qh->qh_groupfile;
490 1.1 dholland break;
491 1.1 dholland default:
492 1.1 dholland errno = EINVAL;
493 1.1 dholland return -1;
494 1.1 dholland }
495 1.1 dholland
496 1.1 dholland if (qk->qk_id == QUOTA_DEFAULTID) {
497 1.1 dholland pos = 0;
498 1.1 dholland } else {
499 1.1 dholland pos = qk->qk_id * sizeof(struct dqblk);
500 1.1 dholland }
501 1.1 dholland
502 1.1 dholland result = pread(file, &dq, sizeof(dq), pos);
503 1.1 dholland if (result < 0) {
504 1.1 dholland return -1;
505 1.2 dholland } else if (result == 0) {
506 1.2 dholland /* Past EOF; no quota info on file for this ID */
507 1.2 dholland errno = ENOENT;
508 1.2 dholland return -1;
509 1.2 dholland } else if ((size_t)result != sizeof(dq)) {
510 1.1 dholland errno = EFTYPE;
511 1.1 dholland return -1;
512 1.1 dholland }
513 1.1 dholland
514 1.1 dholland switch (qk->qk_objtype) {
515 1.1 dholland case QUOTA_OBJTYPE_BLOCKS:
516 1.1 dholland dqblk_getblocks(&dq, qv);
517 1.1 dholland break;
518 1.1 dholland case QUOTA_OBJTYPE_FILES:
519 1.1 dholland dqblk_getfiles(&dq, qv);
520 1.1 dholland break;
521 1.1 dholland default:
522 1.1 dholland errno = EINVAL;
523 1.1 dholland return -1;
524 1.1 dholland }
525 1.1 dholland
526 1.1 dholland if (qk->qk_id == QUOTA_DEFAULTID) {
527 1.1 dholland qv->qv_usage = 0;
528 1.1 dholland qv->qv_grace = qv->qv_expiretime;
529 1.1 dholland qv->qv_expiretime = QUOTA_NOTIME;
530 1.1 dholland } else if (qk->qk_id == 0) {
531 1.1 dholland qv->qv_hardlimit = 0;
532 1.1 dholland qv->qv_softlimit = 0;
533 1.1 dholland qv->qv_expiretime = QUOTA_NOTIME;
534 1.1 dholland qv->qv_grace = QUOTA_NOTIME;
535 1.1 dholland }
536 1.1 dholland
537 1.1 dholland if (isallzero != NULL) {
538 1.1 dholland if (dq.dqb_bhardlimit == 0 &&
539 1.1 dholland dq.dqb_bsoftlimit == 0 &&
540 1.1 dholland dq.dqb_curblocks == 0 &&
541 1.1 dholland dq.dqb_ihardlimit == 0 &&
542 1.1 dholland dq.dqb_isoftlimit == 0 &&
543 1.1 dholland dq.dqb_curinodes == 0 &&
544 1.1 dholland dq.dqb_btime == 0 &&
545 1.1 dholland dq.dqb_itime == 0) {
546 1.1 dholland *isallzero = 1;
547 1.1 dholland } else {
548 1.1 dholland *isallzero = 0;
549 1.1 dholland }
550 1.1 dholland }
551 1.1 dholland
552 1.1 dholland return 0;
553 1.1 dholland }
554 1.1 dholland
555 1.2 dholland static int
556 1.2 dholland __quota_oldfiles_doput(struct quotahandle *qh, const struct quotakey *qk,
557 1.2 dholland const struct quotaval *qv)
558 1.2 dholland {
559 1.2 dholland int file;
560 1.2 dholland off_t pos;
561 1.2 dholland struct quotaval qv2;
562 1.2 dholland struct dqblk dq;
563 1.2 dholland ssize_t result;
564 1.2 dholland
565 1.3 dholland if (!qh->qh_oldfilesopen) {
566 1.3 dholland if (__quota_oldfiles_initialize(qh)) {
567 1.3 dholland return -1;
568 1.3 dholland }
569 1.3 dholland }
570 1.3 dholland
571 1.2 dholland switch (qk->qk_idtype) {
572 1.2 dholland case QUOTA_IDTYPE_USER:
573 1.2 dholland file = qh->qh_userfile;
574 1.2 dholland break;
575 1.2 dholland case QUOTA_IDTYPE_GROUP:
576 1.2 dholland file = qh->qh_groupfile;
577 1.2 dholland break;
578 1.2 dholland default:
579 1.2 dholland errno = EINVAL;
580 1.2 dholland return -1;
581 1.2 dholland }
582 1.2 dholland
583 1.2 dholland if (qk->qk_id == QUOTA_DEFAULTID) {
584 1.2 dholland pos = 0;
585 1.2 dholland } else {
586 1.2 dholland pos = qk->qk_id * sizeof(struct dqblk);
587 1.2 dholland }
588 1.2 dholland
589 1.2 dholland result = pread(file, &dq, sizeof(dq), pos);
590 1.2 dholland if (result < 0) {
591 1.2 dholland return -1;
592 1.2 dholland } else if (result == 0) {
593 1.2 dholland /* Past EOF; fill in a blank dq to start from */
594 1.2 dholland dq.dqb_bhardlimit = 0;
595 1.2 dholland dq.dqb_bsoftlimit = 0;
596 1.2 dholland dq.dqb_curblocks = 0;
597 1.2 dholland dq.dqb_ihardlimit = 0;
598 1.2 dholland dq.dqb_isoftlimit = 0;
599 1.2 dholland dq.dqb_curinodes = 0;
600 1.2 dholland dq.dqb_btime = 0;
601 1.2 dholland dq.dqb_itime = 0;
602 1.2 dholland } else if ((size_t)result != sizeof(dq)) {
603 1.2 dholland errno = EFTYPE;
604 1.2 dholland return -1;
605 1.2 dholland }
606 1.2 dholland
607 1.2 dholland switch (qk->qk_objtype) {
608 1.2 dholland case QUOTA_OBJTYPE_BLOCKS:
609 1.2 dholland dqblk_getblocks(&dq, &qv2);
610 1.2 dholland break;
611 1.2 dholland case QUOTA_OBJTYPE_FILES:
612 1.2 dholland dqblk_getfiles(&dq, &qv2);
613 1.2 dholland break;
614 1.2 dholland default:
615 1.2 dholland errno = EINVAL;
616 1.2 dholland return -1;
617 1.2 dholland }
618 1.2 dholland
619 1.2 dholland if (qk->qk_id == QUOTA_DEFAULTID) {
620 1.2 dholland qv2.qv_hardlimit = qv->qv_hardlimit;
621 1.2 dholland qv2.qv_softlimit = qv->qv_softlimit;
622 1.2 dholland /* leave qv2.qv_usage unchanged */
623 1.2 dholland qv2.qv_expiretime = qv->qv_grace;
624 1.2 dholland /* skip qv2.qv_grace */
625 1.2 dholland
626 1.2 dholland /* ignore qv->qv_usage */
627 1.2 dholland /* ignore qv->qv_expiretime */
628 1.2 dholland } else if (qk->qk_id == 0) {
629 1.2 dholland /* leave qv2.qv_hardlimit unchanged */
630 1.2 dholland /* leave qv2.qv_softlimit unchanged */
631 1.2 dholland qv2.qv_usage = qv->qv_usage;
632 1.2 dholland /* leave qv2.qv_expiretime unchanged */
633 1.2 dholland /* skip qv2.qv_grace */
634 1.2 dholland
635 1.2 dholland /* ignore qv->qv_hardlimit */
636 1.2 dholland /* ignore qv->qv_softlimit */
637 1.2 dholland /* ignore qv->qv_expiretime */
638 1.2 dholland /* ignore qv->qv_grace */
639 1.2 dholland } else {
640 1.2 dholland qv2 = *qv;
641 1.2 dholland }
642 1.2 dholland
643 1.2 dholland switch (qk->qk_objtype) {
644 1.2 dholland case QUOTA_OBJTYPE_BLOCKS:
645 1.2 dholland dqblk_putblocks(&qv2, &dq);
646 1.2 dholland break;
647 1.2 dholland case QUOTA_OBJTYPE_FILES:
648 1.2 dholland dqblk_putfiles(&qv2, &dq);
649 1.2 dholland break;
650 1.2 dholland default:
651 1.2 dholland errno = EINVAL;
652 1.2 dholland return -1;
653 1.2 dholland }
654 1.2 dholland
655 1.2 dholland result = pwrite(file, &dq, sizeof(dq), pos);
656 1.2 dholland if (result < 0) {
657 1.2 dholland return -1;
658 1.2 dholland } else if ((size_t)result != sizeof(dq)) {
659 1.2 dholland /* ? */
660 1.2 dholland errno = EFTYPE;
661 1.2 dholland return -1;
662 1.2 dholland }
663 1.2 dholland
664 1.2 dholland return 0;
665 1.2 dholland }
666 1.2 dholland
667 1.1 dholland int
668 1.1 dholland __quota_oldfiles_get(struct quotahandle *qh, const struct quotakey *qk,
669 1.1 dholland struct quotaval *qv)
670 1.1 dholland {
671 1.1 dholland return __quota_oldfiles_doget(qh, qk, qv, NULL);
672 1.1 dholland }
673 1.1 dholland
674 1.2 dholland int
675 1.2 dholland __quota_oldfiles_put(struct quotahandle *qh, const struct quotakey *qk,
676 1.2 dholland const struct quotaval *qv)
677 1.2 dholland {
678 1.2 dholland return __quota_oldfiles_doput(qh, qk, qv);
679 1.2 dholland }
680 1.2 dholland
681 1.2 dholland int
682 1.2 dholland __quota_oldfiles_delete(struct quotahandle *qh, const struct quotakey *qk)
683 1.2 dholland {
684 1.2 dholland struct quotaval qv;
685 1.2 dholland
686 1.2 dholland quotaval_clear(&qv);
687 1.2 dholland return __quota_oldfiles_doput(qh, qk, &qv);
688 1.2 dholland }
689 1.2 dholland
690 1.1 dholland struct oldfiles_quotacursor *
691 1.1 dholland __quota_oldfiles_cursor_create(struct quotahandle *qh)
692 1.1 dholland {
693 1.1 dholland struct oldfiles_quotacursor *oqc;
694 1.1 dholland struct stat st;
695 1.1 dholland int serrno;
696 1.1 dholland
697 1.3 dholland /* quota_opencursor calls initialize for us, no need to do it here */
698 1.3 dholland
699 1.1 dholland oqc = malloc(sizeof(*oqc));
700 1.1 dholland if (oqc == NULL) {
701 1.1 dholland return NULL;
702 1.1 dholland }
703 1.1 dholland
704 1.1 dholland oqc->oqc_didusers = 0;
705 1.1 dholland oqc->oqc_didgroups = 0;
706 1.1 dholland oqc->oqc_diddefault = 0;
707 1.1 dholland oqc->oqc_pos = 0;
708 1.1 dholland oqc->oqc_didblocks = 0;
709 1.1 dholland
710 1.1 dholland if (qh->qh_userfile >= 0) {
711 1.1 dholland oqc->oqc_doingusers = 1;
712 1.1 dholland } else {
713 1.1 dholland oqc->oqc_doingusers = 0;
714 1.1 dholland oqc->oqc_didusers = 1;
715 1.1 dholland }
716 1.1 dholland
717 1.1 dholland if (qh->qh_groupfile >= 0) {
718 1.1 dholland oqc->oqc_doinggroups = 1;
719 1.1 dholland } else {
720 1.1 dholland oqc->oqc_doinggroups = 0;
721 1.1 dholland oqc->oqc_didgroups = 1;
722 1.1 dholland }
723 1.1 dholland
724 1.1 dholland if (fstat(qh->qh_userfile, &st) < 0) {
725 1.1 dholland serrno = errno;
726 1.1 dholland free(oqc);
727 1.1 dholland errno = serrno;
728 1.1 dholland return NULL;
729 1.1 dholland }
730 1.1 dholland oqc->oqc_numusers = st.st_size / sizeof(struct dqblk);
731 1.1 dholland
732 1.1 dholland if (fstat(qh->qh_groupfile, &st) < 0) {
733 1.1 dholland serrno = errno;
734 1.1 dholland free(oqc);
735 1.1 dholland errno = serrno;
736 1.1 dholland return NULL;
737 1.1 dholland }
738 1.1 dholland oqc->oqc_numgroups = st.st_size / sizeof(struct dqblk);
739 1.1 dholland
740 1.1 dholland return oqc;
741 1.1 dholland }
742 1.1 dholland
743 1.1 dholland void
744 1.1 dholland __quota_oldfiles_cursor_destroy(struct oldfiles_quotacursor *oqc)
745 1.1 dholland {
746 1.1 dholland free(oqc);
747 1.1 dholland }
748 1.1 dholland
749 1.1 dholland int
750 1.1 dholland __quota_oldfiles_cursor_skipidtype(struct oldfiles_quotacursor *oqc,
751 1.1 dholland unsigned idtype)
752 1.1 dholland {
753 1.1 dholland switch (idtype) {
754 1.1 dholland case QUOTA_IDTYPE_USER:
755 1.1 dholland oqc->oqc_doingusers = 0;
756 1.1 dholland oqc->oqc_didusers = 1;
757 1.1 dholland break;
758 1.1 dholland case QUOTA_IDTYPE_GROUP:
759 1.1 dholland oqc->oqc_doinggroups = 0;
760 1.1 dholland oqc->oqc_didgroups = 1;
761 1.1 dholland break;
762 1.1 dholland default:
763 1.1 dholland errno = EINVAL;
764 1.1 dholland return -1;
765 1.1 dholland }
766 1.1 dholland return 0;
767 1.1 dholland }
768 1.1 dholland
769 1.1 dholland int
770 1.1 dholland __quota_oldfiles_cursor_get(struct quotahandle *qh,
771 1.1 dholland struct oldfiles_quotacursor *oqc,
772 1.1 dholland struct quotakey *key, struct quotaval *val)
773 1.1 dholland {
774 1.1 dholland unsigned maxpos;
775 1.1 dholland int isallzero;
776 1.1 dholland
777 1.1 dholland /* in case one of the sizes is zero */
778 1.1 dholland if (!oqc->oqc_didusers && oqc->oqc_pos >= oqc->oqc_numusers) {
779 1.1 dholland oqc->oqc_didusers = 1;
780 1.1 dholland }
781 1.1 dholland if (!oqc->oqc_didgroups && oqc->oqc_pos >= oqc->oqc_numgroups) {
782 1.1 dholland oqc->oqc_didgroups = 1;
783 1.1 dholland }
784 1.1 dholland
785 1.1 dholland again:
786 1.1 dholland /*
787 1.1 dholland * Figure out what to get
788 1.1 dholland */
789 1.1 dholland
790 1.1 dholland if (!oqc->oqc_didusers) {
791 1.1 dholland key->qk_idtype = QUOTA_IDTYPE_USER;
792 1.1 dholland maxpos = oqc->oqc_numusers;
793 1.1 dholland } else if (!oqc->oqc_didgroups) {
794 1.1 dholland key->qk_idtype = QUOTA_IDTYPE_GROUP;
795 1.1 dholland maxpos = oqc->oqc_numgroups;
796 1.1 dholland } else {
797 1.1 dholland errno = ENOENT;
798 1.1 dholland return -1;
799 1.1 dholland }
800 1.1 dholland
801 1.1 dholland if (!oqc->oqc_diddefault) {
802 1.1 dholland key->qk_id = QUOTA_DEFAULTID;
803 1.1 dholland } else {
804 1.1 dholland key->qk_id = oqc->oqc_pos;
805 1.1 dholland }
806 1.1 dholland
807 1.1 dholland if (!oqc->oqc_didblocks) {
808 1.1 dholland key->qk_objtype = QUOTA_OBJTYPE_BLOCKS;
809 1.1 dholland } else {
810 1.1 dholland key->qk_objtype = QUOTA_OBJTYPE_FILES;
811 1.1 dholland }
812 1.1 dholland
813 1.1 dholland /*
814 1.1 dholland * Get it
815 1.1 dholland */
816 1.1 dholland
817 1.1 dholland if (__quota_oldfiles_doget(qh, key, val, &isallzero)) {
818 1.1 dholland return -1;
819 1.1 dholland }
820 1.1 dholland
821 1.1 dholland /*
822 1.1 dholland * Advance the cursor
823 1.1 dholland */
824 1.1 dholland if (!oqc->oqc_didblocks) {
825 1.1 dholland oqc->oqc_didblocks = 1;
826 1.1 dholland } else {
827 1.1 dholland oqc->oqc_didblocks = 0;
828 1.1 dholland if (!oqc->oqc_diddefault) {
829 1.1 dholland oqc->oqc_diddefault = 1;
830 1.1 dholland } else {
831 1.1 dholland oqc->oqc_pos++;
832 1.1 dholland if (oqc->oqc_pos >= maxpos) {
833 1.1 dholland oqc->oqc_pos = 0;
834 1.1 dholland oqc->oqc_diddefault = 0;
835 1.1 dholland if (!oqc->oqc_didusers) {
836 1.1 dholland oqc->oqc_didusers = 1;
837 1.1 dholland } else {
838 1.1 dholland oqc->oqc_didgroups = 1;
839 1.1 dholland }
840 1.1 dholland }
841 1.1 dholland }
842 1.1 dholland }
843 1.1 dholland
844 1.1 dholland /*
845 1.1 dholland * If we got an all-zero dqblk (e.g. from the middle of a hole
846 1.1 dholland * in the quota file) don't bother returning it to the caller.
847 1.1 dholland *
848 1.1 dholland * ...unless we're at the end of the data, to avoid going past
849 1.1 dholland * the end and generating a spurious failure. There's no
850 1.1 dholland * reasonable way to make _atend detect empty entries at the
851 1.1 dholland * end of the quota files.
852 1.1 dholland */
853 1.1 dholland if (isallzero && (!oqc->oqc_didusers || !oqc->oqc_didgroups)) {
854 1.1 dholland goto again;
855 1.1 dholland }
856 1.1 dholland return 0;
857 1.1 dholland }
858 1.1 dholland
859 1.1 dholland int
860 1.1 dholland __quota_oldfiles_cursor_getn(struct quotahandle *qh,
861 1.1 dholland struct oldfiles_quotacursor *oqc,
862 1.1 dholland struct quotakey *keys, struct quotaval *vals,
863 1.1 dholland unsigned maxnum)
864 1.1 dholland {
865 1.1 dholland unsigned i;
866 1.1 dholland
867 1.1 dholland if (maxnum > INT_MAX) {
868 1.1 dholland /* joker, eh? */
869 1.1 dholland errno = EINVAL;
870 1.1 dholland return -1;
871 1.1 dholland }
872 1.1 dholland
873 1.1 dholland for (i=0; i<maxnum; i++) {
874 1.1 dholland if (__quota_oldfiles_cursor_atend(oqc)) {
875 1.1 dholland break;
876 1.1 dholland }
877 1.1 dholland if (__quota_oldfiles_cursor_get(qh, oqc, &keys[i], &vals[i])) {
878 1.1 dholland if (i > 0) {
879 1.1 dholland /*
880 1.1 dholland * Succeed witih what we have so far;
881 1.1 dholland * the next attempt will hit the same
882 1.1 dholland * error again.
883 1.1 dholland */
884 1.1 dholland break;
885 1.1 dholland }
886 1.1 dholland return -1;
887 1.1 dholland }
888 1.1 dholland }
889 1.1 dholland return i;
890 1.1 dholland
891 1.1 dholland }
892 1.1 dholland
893 1.1 dholland int
894 1.1 dholland __quota_oldfiles_cursor_atend(struct oldfiles_quotacursor *oqc)
895 1.1 dholland {
896 1.1 dholland /* in case one of the sizes is zero */
897 1.1 dholland if (!oqc->oqc_didusers && oqc->oqc_pos >= oqc->oqc_numusers) {
898 1.1 dholland oqc->oqc_didusers = 1;
899 1.1 dholland }
900 1.1 dholland if (!oqc->oqc_didgroups && oqc->oqc_pos >= oqc->oqc_numgroups) {
901 1.1 dholland oqc->oqc_didgroups = 1;
902 1.1 dholland }
903 1.1 dholland
904 1.1 dholland return oqc->oqc_didusers && oqc->oqc_didgroups;
905 1.1 dholland }
906 1.1 dholland
907 1.1 dholland int
908 1.1 dholland __quota_oldfiles_cursor_rewind(struct oldfiles_quotacursor *oqc)
909 1.1 dholland {
910 1.1 dholland oqc->oqc_didusers = 0;
911 1.1 dholland oqc->oqc_didgroups = 0;
912 1.1 dholland oqc->oqc_diddefault = 0;
913 1.1 dholland oqc->oqc_pos = 0;
914 1.1 dholland oqc->oqc_didblocks = 0;
915 1.1 dholland return 0;
916 1.1 dholland }
917