quota_oldfiles.c revision 1.4 1 1.4 dholland /* $NetBSD: quota_oldfiles.c,v 1.4 2012/01/30 06:15:22 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.1 dholland static uint64_t
241 1.2 dholland dqblk_getlimit(uint32_t val)
242 1.1 dholland {
243 1.1 dholland if (val == 0) {
244 1.1 dholland return QUOTA_NOLIMIT;
245 1.1 dholland } else {
246 1.1 dholland return val - 1;
247 1.1 dholland }
248 1.1 dholland }
249 1.1 dholland
250 1.2 dholland static uint32_t
251 1.2 dholland dqblk_setlimit(uint64_t val)
252 1.2 dholland {
253 1.2 dholland if (val == QUOTA_NOLIMIT && val >= 0xffffffffUL) {
254 1.2 dholland return 0;
255 1.2 dholland } else {
256 1.2 dholland return (uint32_t)val + 1;
257 1.2 dholland }
258 1.2 dholland }
259 1.2 dholland
260 1.1 dholland static void
261 1.1 dholland dqblk_getblocks(const struct dqblk *dq, struct quotaval *qv)
262 1.1 dholland {
263 1.2 dholland qv->qv_hardlimit = dqblk_getlimit(dq->dqb_bhardlimit);
264 1.2 dholland qv->qv_softlimit = dqblk_getlimit(dq->dqb_bsoftlimit);
265 1.1 dholland qv->qv_usage = dq->dqb_curblocks;
266 1.1 dholland qv->qv_expiretime = dq->dqb_btime;
267 1.1 dholland qv->qv_grace = QUOTA_NOTIME;
268 1.1 dholland }
269 1.1 dholland
270 1.1 dholland static void
271 1.1 dholland dqblk_getfiles(const struct dqblk *dq, struct quotaval *qv)
272 1.1 dholland {
273 1.2 dholland qv->qv_hardlimit = dqblk_getlimit(dq->dqb_ihardlimit);
274 1.2 dholland qv->qv_softlimit = dqblk_getlimit(dq->dqb_isoftlimit);
275 1.1 dholland qv->qv_usage = dq->dqb_curinodes;
276 1.1 dholland qv->qv_expiretime = dq->dqb_itime;
277 1.1 dholland qv->qv_grace = QUOTA_NOTIME;
278 1.1 dholland }
279 1.1 dholland
280 1.2 dholland static void
281 1.2 dholland dqblk_putblocks(const struct quotaval *qv, struct dqblk *dq)
282 1.2 dholland {
283 1.2 dholland dq->dqb_bhardlimit = dqblk_setlimit(qv->qv_hardlimit);
284 1.2 dholland dq->dqb_bsoftlimit = dqblk_setlimit(qv->qv_softlimit);
285 1.2 dholland dq->dqb_curblocks = qv->qv_usage;
286 1.2 dholland dq->dqb_btime = qv->qv_expiretime;
287 1.2 dholland /* ignore qv->qv_grace */
288 1.2 dholland }
289 1.2 dholland
290 1.2 dholland static void
291 1.2 dholland dqblk_putfiles(const struct quotaval *qv, struct dqblk *dq)
292 1.2 dholland {
293 1.2 dholland dq->dqb_ihardlimit = dqblk_setlimit(qv->qv_hardlimit);
294 1.2 dholland dq->dqb_isoftlimit = dqblk_setlimit(qv->qv_softlimit);
295 1.2 dholland dq->dqb_curinodes = qv->qv_usage;
296 1.2 dholland dq->dqb_itime = qv->qv_expiretime;
297 1.2 dholland /* ignore qv->qv_grace */
298 1.2 dholland }
299 1.2 dholland
300 1.1 dholland static int
301 1.1 dholland __quota_oldfiles_open(struct quotahandle *qh, const char *path, int *fd_ret)
302 1.1 dholland {
303 1.1 dholland int fd;
304 1.1 dholland
305 1.1 dholland fd = open(path, O_RDWR);
306 1.1 dholland if (fd < 0 && (errno == EACCES || errno == EROFS)) {
307 1.1 dholland fd = open(path, O_RDONLY);
308 1.1 dholland if (fd < 0) {
309 1.1 dholland return -1;
310 1.1 dholland }
311 1.1 dholland }
312 1.1 dholland *fd_ret = fd;
313 1.1 dholland return 0;
314 1.1 dholland }
315 1.1 dholland
316 1.1 dholland int
317 1.1 dholland __quota_oldfiles_initialize(struct quotahandle *qh)
318 1.1 dholland {
319 1.1 dholland static const char *const names[] = INITQFNAMES;
320 1.1 dholland
321 1.3 dholland const struct oldfiles_fstabentry *ofe;
322 1.1 dholland char path[PATH_MAX];
323 1.1 dholland const char *userquotafile, *groupquotafile;
324 1.1 dholland
325 1.3 dholland if (qh->qh_oldfilesopen) {
326 1.1 dholland /* already initialized */
327 1.1 dholland return 0;
328 1.1 dholland }
329 1.1 dholland
330 1.1 dholland /*
331 1.1 dholland * Find the fstab entry.
332 1.1 dholland */
333 1.3 dholland ofe = __quota_oldfiles_find_fstabentry(qh->qh_mountpoint);
334 1.3 dholland if (ofe == NULL) {
335 1.1 dholland warnx("%s not found in fstab", qh->qh_mountpoint);
336 1.1 dholland errno = ENXIO;
337 1.1 dholland return -1;
338 1.1 dholland }
339 1.1 dholland
340 1.3 dholland if (!ofe->ofe_hasuserquota && !ofe->ofe_hasgroupquota) {
341 1.1 dholland errno = ENXIO;
342 1.1 dholland return -1;
343 1.1 dholland }
344 1.1 dholland
345 1.3 dholland if (ofe->ofe_hasuserquota) {
346 1.3 dholland userquotafile = ofe->ofe_userquotafile;
347 1.1 dholland if (userquotafile == NULL) {
348 1.1 dholland (void)snprintf(path, sizeof(path), "%s/%s.%s",
349 1.3 dholland qh->qh_mountpoint,
350 1.1 dholland QUOTAFILENAME, names[USRQUOTA]);
351 1.1 dholland userquotafile = path;
352 1.1 dholland }
353 1.1 dholland if (__quota_oldfiles_open(qh, userquotafile,
354 1.1 dholland &qh->qh_userfile)) {
355 1.1 dholland return -1;
356 1.1 dholland }
357 1.1 dholland }
358 1.3 dholland if (ofe->ofe_hasgroupquota) {
359 1.3 dholland groupquotafile = ofe->ofe_groupquotafile;
360 1.1 dholland if (groupquotafile == NULL) {
361 1.1 dholland (void)snprintf(path, sizeof(path), "%s/%s.%s",
362 1.3 dholland qh->qh_mountpoint,
363 1.1 dholland QUOTAFILENAME, names[GRPQUOTA]);
364 1.1 dholland groupquotafile = path;
365 1.1 dholland }
366 1.1 dholland if (__quota_oldfiles_open(qh, groupquotafile,
367 1.1 dholland &qh->qh_groupfile)) {
368 1.1 dholland return -1;
369 1.1 dholland }
370 1.1 dholland }
371 1.1 dholland
372 1.3 dholland qh->qh_oldfilesopen = 1;
373 1.1 dholland
374 1.1 dholland return 0;
375 1.1 dholland }
376 1.1 dholland
377 1.1 dholland const char *
378 1.1 dholland __quota_oldfiles_getimplname(struct quotahandle *qh)
379 1.1 dholland {
380 1.3 dholland return "ufs/ffs quota v1 file access";
381 1.1 dholland }
382 1.1 dholland
383 1.1 dholland static int
384 1.1 dholland __quota_oldfiles_doget(struct quotahandle *qh, const struct quotakey *qk,
385 1.1 dholland struct quotaval *qv, int *isallzero)
386 1.1 dholland {
387 1.1 dholland int file;
388 1.1 dholland off_t pos;
389 1.1 dholland struct dqblk dq;
390 1.1 dholland ssize_t result;
391 1.1 dholland
392 1.3 dholland if (!qh->qh_oldfilesopen) {
393 1.3 dholland if (__quota_oldfiles_initialize(qh)) {
394 1.3 dholland return -1;
395 1.3 dholland }
396 1.3 dholland }
397 1.3 dholland
398 1.1 dholland switch (qk->qk_idtype) {
399 1.1 dholland case QUOTA_IDTYPE_USER:
400 1.1 dholland file = qh->qh_userfile;
401 1.1 dholland break;
402 1.1 dholland case QUOTA_IDTYPE_GROUP:
403 1.1 dholland file = qh->qh_groupfile;
404 1.1 dholland break;
405 1.1 dholland default:
406 1.1 dholland errno = EINVAL;
407 1.1 dholland return -1;
408 1.1 dholland }
409 1.1 dholland
410 1.1 dholland if (qk->qk_id == QUOTA_DEFAULTID) {
411 1.1 dholland pos = 0;
412 1.1 dholland } else {
413 1.1 dholland pos = qk->qk_id * sizeof(struct dqblk);
414 1.1 dholland }
415 1.1 dholland
416 1.1 dholland result = pread(file, &dq, sizeof(dq), pos);
417 1.1 dholland if (result < 0) {
418 1.1 dholland return -1;
419 1.2 dholland } else if (result == 0) {
420 1.2 dholland /* Past EOF; no quota info on file for this ID */
421 1.2 dholland errno = ENOENT;
422 1.2 dholland return -1;
423 1.2 dholland } else if ((size_t)result != sizeof(dq)) {
424 1.1 dholland errno = EFTYPE;
425 1.1 dholland return -1;
426 1.1 dholland }
427 1.1 dholland
428 1.1 dholland switch (qk->qk_objtype) {
429 1.1 dholland case QUOTA_OBJTYPE_BLOCKS:
430 1.1 dholland dqblk_getblocks(&dq, qv);
431 1.1 dholland break;
432 1.1 dholland case QUOTA_OBJTYPE_FILES:
433 1.1 dholland dqblk_getfiles(&dq, qv);
434 1.1 dholland break;
435 1.1 dholland default:
436 1.1 dholland errno = EINVAL;
437 1.1 dholland return -1;
438 1.1 dholland }
439 1.1 dholland
440 1.1 dholland if (qk->qk_id == QUOTA_DEFAULTID) {
441 1.1 dholland qv->qv_usage = 0;
442 1.1 dholland qv->qv_grace = qv->qv_expiretime;
443 1.1 dholland qv->qv_expiretime = QUOTA_NOTIME;
444 1.1 dholland } else if (qk->qk_id == 0) {
445 1.1 dholland qv->qv_hardlimit = 0;
446 1.1 dholland qv->qv_softlimit = 0;
447 1.1 dholland qv->qv_expiretime = QUOTA_NOTIME;
448 1.1 dholland qv->qv_grace = QUOTA_NOTIME;
449 1.1 dholland }
450 1.1 dholland
451 1.1 dholland if (isallzero != NULL) {
452 1.1 dholland if (dq.dqb_bhardlimit == 0 &&
453 1.1 dholland dq.dqb_bsoftlimit == 0 &&
454 1.1 dholland dq.dqb_curblocks == 0 &&
455 1.1 dholland dq.dqb_ihardlimit == 0 &&
456 1.1 dholland dq.dqb_isoftlimit == 0 &&
457 1.1 dholland dq.dqb_curinodes == 0 &&
458 1.1 dholland dq.dqb_btime == 0 &&
459 1.1 dholland dq.dqb_itime == 0) {
460 1.1 dholland *isallzero = 1;
461 1.1 dholland } else {
462 1.1 dholland *isallzero = 0;
463 1.1 dholland }
464 1.1 dholland }
465 1.1 dholland
466 1.1 dholland return 0;
467 1.1 dholland }
468 1.1 dholland
469 1.2 dholland static int
470 1.2 dholland __quota_oldfiles_doput(struct quotahandle *qh, const struct quotakey *qk,
471 1.2 dholland const struct quotaval *qv)
472 1.2 dholland {
473 1.2 dholland int file;
474 1.2 dholland off_t pos;
475 1.2 dholland struct quotaval qv2;
476 1.2 dholland struct dqblk dq;
477 1.2 dholland ssize_t result;
478 1.2 dholland
479 1.3 dholland if (!qh->qh_oldfilesopen) {
480 1.3 dholland if (__quota_oldfiles_initialize(qh)) {
481 1.3 dholland return -1;
482 1.3 dholland }
483 1.3 dholland }
484 1.3 dholland
485 1.2 dholland switch (qk->qk_idtype) {
486 1.2 dholland case QUOTA_IDTYPE_USER:
487 1.2 dholland file = qh->qh_userfile;
488 1.2 dholland break;
489 1.2 dholland case QUOTA_IDTYPE_GROUP:
490 1.2 dholland file = qh->qh_groupfile;
491 1.2 dholland break;
492 1.2 dholland default:
493 1.2 dholland errno = EINVAL;
494 1.2 dholland return -1;
495 1.2 dholland }
496 1.2 dholland
497 1.2 dholland if (qk->qk_id == QUOTA_DEFAULTID) {
498 1.2 dholland pos = 0;
499 1.2 dholland } else {
500 1.2 dholland pos = qk->qk_id * sizeof(struct dqblk);
501 1.2 dholland }
502 1.2 dholland
503 1.2 dholland result = pread(file, &dq, sizeof(dq), pos);
504 1.2 dholland if (result < 0) {
505 1.2 dholland return -1;
506 1.2 dholland } else if (result == 0) {
507 1.2 dholland /* Past EOF; fill in a blank dq to start from */
508 1.2 dholland dq.dqb_bhardlimit = 0;
509 1.2 dholland dq.dqb_bsoftlimit = 0;
510 1.2 dholland dq.dqb_curblocks = 0;
511 1.2 dholland dq.dqb_ihardlimit = 0;
512 1.2 dholland dq.dqb_isoftlimit = 0;
513 1.2 dholland dq.dqb_curinodes = 0;
514 1.2 dholland dq.dqb_btime = 0;
515 1.2 dholland dq.dqb_itime = 0;
516 1.2 dholland } else if ((size_t)result != sizeof(dq)) {
517 1.2 dholland errno = EFTYPE;
518 1.2 dholland return -1;
519 1.2 dholland }
520 1.2 dholland
521 1.2 dholland switch (qk->qk_objtype) {
522 1.2 dholland case QUOTA_OBJTYPE_BLOCKS:
523 1.2 dholland dqblk_getblocks(&dq, &qv2);
524 1.2 dholland break;
525 1.2 dholland case QUOTA_OBJTYPE_FILES:
526 1.2 dholland dqblk_getfiles(&dq, &qv2);
527 1.2 dholland break;
528 1.2 dholland default:
529 1.2 dholland errno = EINVAL;
530 1.2 dholland return -1;
531 1.2 dholland }
532 1.2 dholland
533 1.2 dholland if (qk->qk_id == QUOTA_DEFAULTID) {
534 1.2 dholland qv2.qv_hardlimit = qv->qv_hardlimit;
535 1.2 dholland qv2.qv_softlimit = qv->qv_softlimit;
536 1.2 dholland /* leave qv2.qv_usage unchanged */
537 1.2 dholland qv2.qv_expiretime = qv->qv_grace;
538 1.2 dholland /* skip qv2.qv_grace */
539 1.2 dholland
540 1.2 dholland /* ignore qv->qv_usage */
541 1.2 dholland /* ignore qv->qv_expiretime */
542 1.2 dholland } else if (qk->qk_id == 0) {
543 1.2 dholland /* leave qv2.qv_hardlimit unchanged */
544 1.2 dholland /* leave qv2.qv_softlimit unchanged */
545 1.2 dholland qv2.qv_usage = qv->qv_usage;
546 1.2 dholland /* leave qv2.qv_expiretime unchanged */
547 1.2 dholland /* skip qv2.qv_grace */
548 1.2 dholland
549 1.2 dholland /* ignore qv->qv_hardlimit */
550 1.2 dholland /* ignore qv->qv_softlimit */
551 1.2 dholland /* ignore qv->qv_expiretime */
552 1.2 dholland /* ignore qv->qv_grace */
553 1.2 dholland } else {
554 1.2 dholland qv2 = *qv;
555 1.2 dholland }
556 1.2 dholland
557 1.2 dholland switch (qk->qk_objtype) {
558 1.2 dholland case QUOTA_OBJTYPE_BLOCKS:
559 1.2 dholland dqblk_putblocks(&qv2, &dq);
560 1.2 dholland break;
561 1.2 dholland case QUOTA_OBJTYPE_FILES:
562 1.2 dholland dqblk_putfiles(&qv2, &dq);
563 1.2 dholland break;
564 1.2 dholland default:
565 1.2 dholland errno = EINVAL;
566 1.2 dholland return -1;
567 1.2 dholland }
568 1.2 dholland
569 1.2 dholland result = pwrite(file, &dq, sizeof(dq), pos);
570 1.2 dholland if (result < 0) {
571 1.2 dholland return -1;
572 1.2 dholland } else if ((size_t)result != sizeof(dq)) {
573 1.2 dholland /* ? */
574 1.2 dholland errno = EFTYPE;
575 1.2 dholland return -1;
576 1.2 dholland }
577 1.2 dholland
578 1.2 dholland return 0;
579 1.2 dholland }
580 1.2 dholland
581 1.1 dholland int
582 1.1 dholland __quota_oldfiles_get(struct quotahandle *qh, const struct quotakey *qk,
583 1.1 dholland struct quotaval *qv)
584 1.1 dholland {
585 1.1 dholland return __quota_oldfiles_doget(qh, qk, qv, NULL);
586 1.1 dholland }
587 1.1 dholland
588 1.2 dholland int
589 1.2 dholland __quota_oldfiles_put(struct quotahandle *qh, const struct quotakey *qk,
590 1.2 dholland const struct quotaval *qv)
591 1.2 dholland {
592 1.2 dholland return __quota_oldfiles_doput(qh, qk, qv);
593 1.2 dholland }
594 1.2 dholland
595 1.2 dholland int
596 1.2 dholland __quota_oldfiles_delete(struct quotahandle *qh, const struct quotakey *qk)
597 1.2 dholland {
598 1.2 dholland struct quotaval qv;
599 1.2 dholland
600 1.2 dholland quotaval_clear(&qv);
601 1.2 dholland return __quota_oldfiles_doput(qh, qk, &qv);
602 1.2 dholland }
603 1.2 dholland
604 1.1 dholland struct oldfiles_quotacursor *
605 1.1 dholland __quota_oldfiles_cursor_create(struct quotahandle *qh)
606 1.1 dholland {
607 1.1 dholland struct oldfiles_quotacursor *oqc;
608 1.1 dholland struct stat st;
609 1.1 dholland int serrno;
610 1.1 dholland
611 1.3 dholland /* quota_opencursor calls initialize for us, no need to do it here */
612 1.3 dholland
613 1.1 dholland oqc = malloc(sizeof(*oqc));
614 1.1 dholland if (oqc == NULL) {
615 1.1 dholland return NULL;
616 1.1 dholland }
617 1.1 dholland
618 1.1 dholland oqc->oqc_didusers = 0;
619 1.1 dholland oqc->oqc_didgroups = 0;
620 1.1 dholland oqc->oqc_diddefault = 0;
621 1.1 dholland oqc->oqc_pos = 0;
622 1.1 dholland oqc->oqc_didblocks = 0;
623 1.1 dholland
624 1.1 dholland if (qh->qh_userfile >= 0) {
625 1.1 dholland oqc->oqc_doingusers = 1;
626 1.1 dholland } else {
627 1.1 dholland oqc->oqc_doingusers = 0;
628 1.1 dholland oqc->oqc_didusers = 1;
629 1.1 dholland }
630 1.1 dholland
631 1.1 dholland if (qh->qh_groupfile >= 0) {
632 1.1 dholland oqc->oqc_doinggroups = 1;
633 1.1 dholland } else {
634 1.1 dholland oqc->oqc_doinggroups = 0;
635 1.1 dholland oqc->oqc_didgroups = 1;
636 1.1 dholland }
637 1.1 dholland
638 1.1 dholland if (fstat(qh->qh_userfile, &st) < 0) {
639 1.1 dholland serrno = errno;
640 1.1 dholland free(oqc);
641 1.1 dholland errno = serrno;
642 1.1 dholland return NULL;
643 1.1 dholland }
644 1.1 dholland oqc->oqc_numusers = st.st_size / sizeof(struct dqblk);
645 1.1 dholland
646 1.1 dholland if (fstat(qh->qh_groupfile, &st) < 0) {
647 1.1 dholland serrno = errno;
648 1.1 dholland free(oqc);
649 1.1 dholland errno = serrno;
650 1.1 dholland return NULL;
651 1.1 dholland }
652 1.1 dholland oqc->oqc_numgroups = st.st_size / sizeof(struct dqblk);
653 1.1 dholland
654 1.1 dholland return oqc;
655 1.1 dholland }
656 1.1 dholland
657 1.1 dholland void
658 1.1 dholland __quota_oldfiles_cursor_destroy(struct oldfiles_quotacursor *oqc)
659 1.1 dholland {
660 1.1 dholland free(oqc);
661 1.1 dholland }
662 1.1 dholland
663 1.1 dholland int
664 1.1 dholland __quota_oldfiles_cursor_skipidtype(struct oldfiles_quotacursor *oqc,
665 1.1 dholland unsigned idtype)
666 1.1 dholland {
667 1.1 dholland switch (idtype) {
668 1.1 dholland case QUOTA_IDTYPE_USER:
669 1.1 dholland oqc->oqc_doingusers = 0;
670 1.1 dholland oqc->oqc_didusers = 1;
671 1.1 dholland break;
672 1.1 dholland case QUOTA_IDTYPE_GROUP:
673 1.1 dholland oqc->oqc_doinggroups = 0;
674 1.1 dholland oqc->oqc_didgroups = 1;
675 1.1 dholland break;
676 1.1 dholland default:
677 1.1 dholland errno = EINVAL;
678 1.1 dholland return -1;
679 1.1 dholland }
680 1.1 dholland return 0;
681 1.1 dholland }
682 1.1 dholland
683 1.1 dholland int
684 1.1 dholland __quota_oldfiles_cursor_get(struct quotahandle *qh,
685 1.1 dholland struct oldfiles_quotacursor *oqc,
686 1.1 dholland struct quotakey *key, struct quotaval *val)
687 1.1 dholland {
688 1.1 dholland unsigned maxpos;
689 1.1 dholland int isallzero;
690 1.1 dholland
691 1.1 dholland /* in case one of the sizes is zero */
692 1.1 dholland if (!oqc->oqc_didusers && oqc->oqc_pos >= oqc->oqc_numusers) {
693 1.1 dholland oqc->oqc_didusers = 1;
694 1.1 dholland }
695 1.1 dholland if (!oqc->oqc_didgroups && oqc->oqc_pos >= oqc->oqc_numgroups) {
696 1.1 dholland oqc->oqc_didgroups = 1;
697 1.1 dholland }
698 1.1 dholland
699 1.1 dholland again:
700 1.1 dholland /*
701 1.1 dholland * Figure out what to get
702 1.1 dholland */
703 1.1 dholland
704 1.1 dholland if (!oqc->oqc_didusers) {
705 1.1 dholland key->qk_idtype = QUOTA_IDTYPE_USER;
706 1.1 dholland maxpos = oqc->oqc_numusers;
707 1.1 dholland } else if (!oqc->oqc_didgroups) {
708 1.1 dholland key->qk_idtype = QUOTA_IDTYPE_GROUP;
709 1.1 dholland maxpos = oqc->oqc_numgroups;
710 1.1 dholland } else {
711 1.1 dholland errno = ENOENT;
712 1.1 dholland return -1;
713 1.1 dholland }
714 1.1 dholland
715 1.1 dholland if (!oqc->oqc_diddefault) {
716 1.1 dholland key->qk_id = QUOTA_DEFAULTID;
717 1.1 dholland } else {
718 1.1 dholland key->qk_id = oqc->oqc_pos;
719 1.1 dholland }
720 1.1 dholland
721 1.1 dholland if (!oqc->oqc_didblocks) {
722 1.1 dholland key->qk_objtype = QUOTA_OBJTYPE_BLOCKS;
723 1.1 dholland } else {
724 1.1 dholland key->qk_objtype = QUOTA_OBJTYPE_FILES;
725 1.1 dholland }
726 1.1 dholland
727 1.1 dholland /*
728 1.1 dholland * Get it
729 1.1 dholland */
730 1.1 dholland
731 1.1 dholland if (__quota_oldfiles_doget(qh, key, val, &isallzero)) {
732 1.1 dholland return -1;
733 1.1 dholland }
734 1.1 dholland
735 1.1 dholland /*
736 1.1 dholland * Advance the cursor
737 1.1 dholland */
738 1.1 dholland if (!oqc->oqc_didblocks) {
739 1.1 dholland oqc->oqc_didblocks = 1;
740 1.1 dholland } else {
741 1.1 dholland oqc->oqc_didblocks = 0;
742 1.1 dholland if (!oqc->oqc_diddefault) {
743 1.1 dholland oqc->oqc_diddefault = 1;
744 1.1 dholland } else {
745 1.1 dholland oqc->oqc_pos++;
746 1.1 dholland if (oqc->oqc_pos >= maxpos) {
747 1.1 dholland oqc->oqc_pos = 0;
748 1.1 dholland oqc->oqc_diddefault = 0;
749 1.1 dholland if (!oqc->oqc_didusers) {
750 1.1 dholland oqc->oqc_didusers = 1;
751 1.1 dholland } else {
752 1.1 dholland oqc->oqc_didgroups = 1;
753 1.1 dholland }
754 1.1 dholland }
755 1.1 dholland }
756 1.1 dholland }
757 1.1 dholland
758 1.1 dholland /*
759 1.1 dholland * If we got an all-zero dqblk (e.g. from the middle of a hole
760 1.1 dholland * in the quota file) don't bother returning it to the caller.
761 1.1 dholland *
762 1.1 dholland * ...unless we're at the end of the data, to avoid going past
763 1.1 dholland * the end and generating a spurious failure. There's no
764 1.1 dholland * reasonable way to make _atend detect empty entries at the
765 1.1 dholland * end of the quota files.
766 1.1 dholland */
767 1.1 dholland if (isallzero && (!oqc->oqc_didusers || !oqc->oqc_didgroups)) {
768 1.1 dholland goto again;
769 1.1 dholland }
770 1.1 dholland return 0;
771 1.1 dholland }
772 1.1 dholland
773 1.1 dholland int
774 1.1 dholland __quota_oldfiles_cursor_getn(struct quotahandle *qh,
775 1.1 dholland struct oldfiles_quotacursor *oqc,
776 1.1 dholland struct quotakey *keys, struct quotaval *vals,
777 1.1 dholland unsigned maxnum)
778 1.1 dholland {
779 1.1 dholland unsigned i;
780 1.1 dholland
781 1.1 dholland if (maxnum > INT_MAX) {
782 1.1 dholland /* joker, eh? */
783 1.1 dholland errno = EINVAL;
784 1.1 dholland return -1;
785 1.1 dholland }
786 1.1 dholland
787 1.1 dholland for (i=0; i<maxnum; i++) {
788 1.1 dholland if (__quota_oldfiles_cursor_atend(oqc)) {
789 1.1 dholland break;
790 1.1 dholland }
791 1.1 dholland if (__quota_oldfiles_cursor_get(qh, oqc, &keys[i], &vals[i])) {
792 1.1 dholland if (i > 0) {
793 1.1 dholland /*
794 1.1 dholland * Succeed witih what we have so far;
795 1.1 dholland * the next attempt will hit the same
796 1.1 dholland * error again.
797 1.1 dholland */
798 1.1 dholland break;
799 1.1 dholland }
800 1.1 dholland return -1;
801 1.1 dholland }
802 1.1 dholland }
803 1.1 dholland return i;
804 1.1 dholland
805 1.1 dholland }
806 1.1 dholland
807 1.1 dholland int
808 1.1 dholland __quota_oldfiles_cursor_atend(struct oldfiles_quotacursor *oqc)
809 1.1 dholland {
810 1.1 dholland /* in case one of the sizes is zero */
811 1.1 dholland if (!oqc->oqc_didusers && oqc->oqc_pos >= oqc->oqc_numusers) {
812 1.1 dholland oqc->oqc_didusers = 1;
813 1.1 dholland }
814 1.1 dholland if (!oqc->oqc_didgroups && oqc->oqc_pos >= oqc->oqc_numgroups) {
815 1.1 dholland oqc->oqc_didgroups = 1;
816 1.1 dholland }
817 1.1 dholland
818 1.1 dholland return oqc->oqc_didusers && oqc->oqc_didgroups;
819 1.1 dholland }
820 1.1 dholland
821 1.1 dholland int
822 1.1 dholland __quota_oldfiles_cursor_rewind(struct oldfiles_quotacursor *oqc)
823 1.1 dholland {
824 1.1 dholland oqc->oqc_didusers = 0;
825 1.1 dholland oqc->oqc_didgroups = 0;
826 1.1 dholland oqc->oqc_diddefault = 0;
827 1.1 dholland oqc->oqc_pos = 0;
828 1.1 dholland oqc->oqc_didblocks = 0;
829 1.1 dholland return 0;
830 1.1 dholland }
831