2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2008 Dag-Erling Coïdan Smørgrav
5 * Copyright (c) 2008 Marshall Kirk McKusick
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer
13 * in this position and unchanged.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/types.h>
34 #include <sys/endian.h>
35 #include <sys/mount.h>
38 #include <ufs/ufs/quota.h>
53 int fd; /* -1 means using quotactl for access */
54 int accmode; /* access mode */
55 int wordsize; /* 32-bit or 64-bit limits */
56 int quotatype; /* USRQUOTA or GRPQUOTA */
57 dev_t dev; /* device */
58 char fsname[MAXPATHLEN + 1]; /* mount point of filesystem */
59 char qfname[MAXPATHLEN + 1]; /* quota file if not using quotactl */
62 static const char *qfextension[] = INITQFNAMES;
65 * Check to see if a particular quota is to be enabled.
68 hasquota(struct fstab *fs, int type, char *qfnamep, int qfbufsize)
74 static char initname, usrname[100], grpname[100];
77 * 1) we only need one of these
78 * 2) fstab may specify a different filename
81 (void)snprintf(usrname, sizeof(usrname), "%s%s",
82 qfextension[USRQUOTA], QUOTAFILENAME);
83 (void)snprintf(grpname, sizeof(grpname), "%s%s",
84 qfextension[GRPQUOTA], QUOTAFILENAME);
87 strcpy(buf, fs->fs_mntops);
88 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
89 if ((cp = strchr(opt, '=')))
91 if (type == USRQUOTA && strcmp(opt, usrname) == 0)
93 if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
99 * Ensure that the filesystem is mounted.
101 if (statfs(fs->fs_file, &sfb) != 0 ||
102 strcmp(fs->fs_file, sfb.f_mntonname)) {
106 strlcpy(qfnamep, cp, qfbufsize);
108 (void)snprintf(qfnamep, qfbufsize, "%s/%s.%s", fs->fs_file,
109 QUOTAFILENAME, qfextension[type]);
115 quota_open(struct fstab *fs, int quotatype, int openflags)
117 struct quotafile *qf;
121 int qcmd, serrno = 0;
124 if ((qf = calloc(1, sizeof(*qf))) == NULL)
127 qf->quotatype = quotatype;
128 strlcpy(qf->fsname, fs->fs_file, sizeof(qf->fsname));
129 if (stat(qf->fsname, &st) != 0)
132 qcmd = QCMD(Q_GETQUOTASIZE, quotatype);
133 ufs = strcmp(fs->fs_vfstype, "ufs") == 0;
135 * On UFS, hasquota() fills in qf->qfname. But we only care about
136 * this for UFS. So we need to call hasquota() for UFS, first.
139 serrno = hasquota(fs, quotatype, qf->qfname,
142 if (quotactl(qf->fsname, qcmd, 0, &qf->wordsize) == 0)
147 } else if (serrno == 0) {
151 qf->accmode = openflags & O_ACCMODE;
152 if ((qf->fd = open(qf->qfname, qf->accmode|O_CLOEXEC)) < 0 &&
153 (openflags & O_CREAT) != O_CREAT)
155 /* File open worked, so process it */
158 switch (read(qf->fd, &dqh, sizeof(dqh))) {
162 if (strcmp(dqh.dqh_magic, Q_DQHDR64_MAGIC) != 0) {
163 /* no magic, assume 32 bits */
167 if (be32toh(dqh.dqh_version) != Q_DQHDR64_VERSION ||
168 be32toh(dqh.dqh_hdrlen) != sizeof(struct dqhdr64) ||
169 be32toh(dqh.dqh_reclen) != sizeof(struct dqblk64)) {
170 /* correct magic, wrong version / lengths */
182 /* open failed, but O_CREAT was specified, so create a new file */
183 if ((qf->fd = open(qf->qfname, O_RDWR|O_CREAT|O_TRUNC|O_CLOEXEC, 0)) <
187 memset(&dqh, 0, sizeof(dqh));
188 memcpy(dqh.dqh_magic, Q_DQHDR64_MAGIC, sizeof(dqh.dqh_magic));
189 dqh.dqh_version = htobe32(Q_DQHDR64_VERSION);
190 dqh.dqh_hdrlen = htobe32(sizeof(struct dqhdr64));
191 dqh.dqh_reclen = htobe32(sizeof(struct dqblk64));
192 if (write(qf->fd, &dqh, sizeof(dqh)) != sizeof(dqh)) {
193 /* it was one we created ourselves */
197 grp = getgrnam(QUOTAGROUP);
198 fchown(qf->fd, 0, grp ? grp->gr_gid : 0);
199 fchmod(qf->fd, 0640);
203 /* did we have an open file? */
212 quota_close(struct quotafile *qf)
221 quota_on(struct quotafile *qf)
225 qcmd = QCMD(Q_QUOTAON, qf->quotatype);
226 return (quotactl(qf->fsname, qcmd, 0, qf->qfname));
230 quota_off(struct quotafile *qf)
233 return (quotactl(qf->fsname, QCMD(Q_QUOTAOFF, qf->quotatype), 0, 0));
237 quota_fsname(const struct quotafile *qf)
244 quota_qfname(const struct quotafile *qf)
251 quota_check_path(const struct quotafile *qf, const char *path)
255 if (stat(path, &st) == -1)
257 return (st.st_dev == qf->dev);
261 quota_maxid(struct quotafile *qf)
266 if (stat(qf->qfname, &st) < 0)
268 switch (qf->wordsize) {
270 maxid = st.st_size / sizeof(struct dqblk32) - 1;
273 maxid = st.st_size / sizeof(struct dqblk64) - 2;
279 return (maxid > 0 ? maxid : 0);
283 quota_read32(struct quotafile *qf, struct dqblk *dqb, int id)
285 struct dqblk32 dqb32;
288 off = id * sizeof(struct dqblk32);
289 if (lseek(qf->fd, off, SEEK_SET) == -1)
291 switch (read(qf->fd, &dqb32, sizeof(dqb32))) {
293 memset(dqb, 0, sizeof(*dqb));
296 dqb->dqb_bhardlimit = dqb32.dqb_bhardlimit;
297 dqb->dqb_bsoftlimit = dqb32.dqb_bsoftlimit;
298 dqb->dqb_curblocks = dqb32.dqb_curblocks;
299 dqb->dqb_ihardlimit = dqb32.dqb_ihardlimit;
300 dqb->dqb_isoftlimit = dqb32.dqb_isoftlimit;
301 dqb->dqb_curinodes = dqb32.dqb_curinodes;
302 dqb->dqb_btime = dqb32.dqb_btime;
303 dqb->dqb_itime = dqb32.dqb_itime;
311 quota_read64(struct quotafile *qf, struct dqblk *dqb, int id)
313 struct dqblk64 dqb64;
316 off = sizeof(struct dqhdr64) + id * sizeof(struct dqblk64);
317 if (lseek(qf->fd, off, SEEK_SET) == -1)
319 switch (read(qf->fd, &dqb64, sizeof(dqb64))) {
321 memset(dqb, 0, sizeof(*dqb));
324 dqb->dqb_bhardlimit = be64toh(dqb64.dqb_bhardlimit);
325 dqb->dqb_bsoftlimit = be64toh(dqb64.dqb_bsoftlimit);
326 dqb->dqb_curblocks = be64toh(dqb64.dqb_curblocks);
327 dqb->dqb_ihardlimit = be64toh(dqb64.dqb_ihardlimit);
328 dqb->dqb_isoftlimit = be64toh(dqb64.dqb_isoftlimit);
329 dqb->dqb_curinodes = be64toh(dqb64.dqb_curinodes);
330 dqb->dqb_btime = be64toh(dqb64.dqb_btime);
331 dqb->dqb_itime = be64toh(dqb64.dqb_itime);
339 quota_read(struct quotafile *qf, struct dqblk *dqb, int id)
344 qcmd = QCMD(Q_GETQUOTA, qf->quotatype);
345 return (quotactl(qf->fsname, qcmd, id, dqb));
347 switch (qf->wordsize) {
349 return (quota_read32(qf, dqb, id));
351 return (quota_read64(qf, dqb, id));
359 #define CLIP32(u64) ((u64) > UINT32_MAX ? UINT32_MAX : (uint32_t)(u64))
362 quota_write32(struct quotafile *qf, const struct dqblk *dqb, int id)
364 struct dqblk32 dqb32;
367 dqb32.dqb_bhardlimit = CLIP32(dqb->dqb_bhardlimit);
368 dqb32.dqb_bsoftlimit = CLIP32(dqb->dqb_bsoftlimit);
369 dqb32.dqb_curblocks = CLIP32(dqb->dqb_curblocks);
370 dqb32.dqb_ihardlimit = CLIP32(dqb->dqb_ihardlimit);
371 dqb32.dqb_isoftlimit = CLIP32(dqb->dqb_isoftlimit);
372 dqb32.dqb_curinodes = CLIP32(dqb->dqb_curinodes);
373 dqb32.dqb_btime = CLIP32(dqb->dqb_btime);
374 dqb32.dqb_itime = CLIP32(dqb->dqb_itime);
376 off = id * sizeof(struct dqblk32);
377 if (lseek(qf->fd, off, SEEK_SET) == -1)
379 if (write(qf->fd, &dqb32, sizeof(dqb32)) == sizeof(dqb32))
385 quota_write64(struct quotafile *qf, const struct dqblk *dqb, int id)
387 struct dqblk64 dqb64;
390 dqb64.dqb_bhardlimit = htobe64(dqb->dqb_bhardlimit);
391 dqb64.dqb_bsoftlimit = htobe64(dqb->dqb_bsoftlimit);
392 dqb64.dqb_curblocks = htobe64(dqb->dqb_curblocks);
393 dqb64.dqb_ihardlimit = htobe64(dqb->dqb_ihardlimit);
394 dqb64.dqb_isoftlimit = htobe64(dqb->dqb_isoftlimit);
395 dqb64.dqb_curinodes = htobe64(dqb->dqb_curinodes);
396 dqb64.dqb_btime = htobe64(dqb->dqb_btime);
397 dqb64.dqb_itime = htobe64(dqb->dqb_itime);
399 off = sizeof(struct dqhdr64) + id * sizeof(struct dqblk64);
400 if (lseek(qf->fd, off, SEEK_SET) == -1)
402 if (write(qf->fd, &dqb64, sizeof(dqb64)) == sizeof(dqb64))
408 quota_write_usage(struct quotafile *qf, struct dqblk *dqb, int id)
414 qcmd = QCMD(Q_SETUSE, qf->quotatype);
415 return (quotactl(qf->fsname, qcmd, id, dqb));
418 * Have to do read-modify-write of quota in file.
420 if ((qf->accmode & O_RDWR) != O_RDWR) {
424 if (quota_read(qf, &dqbuf, id) != 0)
427 * Reset time limit if have a soft limit and were
428 * previously under it, but are now over it.
430 if (dqbuf.dqb_bsoftlimit && id != 0 &&
431 dqbuf.dqb_curblocks < dqbuf.dqb_bsoftlimit &&
432 dqb->dqb_curblocks >= dqbuf.dqb_bsoftlimit)
434 if (dqbuf.dqb_isoftlimit && id != 0 &&
435 dqbuf.dqb_curinodes < dqbuf.dqb_isoftlimit &&
436 dqb->dqb_curinodes >= dqbuf.dqb_isoftlimit)
438 dqbuf.dqb_curinodes = dqb->dqb_curinodes;
439 dqbuf.dqb_curblocks = dqb->dqb_curblocks;
443 switch (qf->wordsize) {
445 return (quota_write32(qf, &dqbuf, id));
447 return (quota_write64(qf, &dqbuf, id));
456 quota_write_limits(struct quotafile *qf, struct dqblk *dqb, int id)
462 qcmd = QCMD(Q_SETQUOTA, qf->quotatype);
463 return (quotactl(qf->fsname, qcmd, id, dqb));
466 * Have to do read-modify-write of quota in file.
468 if ((qf->accmode & O_RDWR) != O_RDWR) {
472 if (quota_read(qf, &dqbuf, id) != 0)
475 * Reset time limit if have a soft limit and were
476 * previously under it, but are now over it
477 * or if there previously was no soft limit, but
478 * now have one and are over it.
480 if (dqbuf.dqb_bsoftlimit && id != 0 &&
481 dqbuf.dqb_curblocks < dqbuf.dqb_bsoftlimit &&
482 dqbuf.dqb_curblocks >= dqb->dqb_bsoftlimit)
484 if (dqbuf.dqb_bsoftlimit == 0 && id != 0 &&
485 dqb->dqb_bsoftlimit > 0 &&
486 dqbuf.dqb_curblocks >= dqb->dqb_bsoftlimit)
488 if (dqbuf.dqb_isoftlimit && id != 0 &&
489 dqbuf.dqb_curinodes < dqbuf.dqb_isoftlimit &&
490 dqbuf.dqb_curinodes >= dqb->dqb_isoftlimit)
492 if (dqbuf.dqb_isoftlimit == 0 && id !=0 &&
493 dqb->dqb_isoftlimit > 0 &&
494 dqbuf.dqb_curinodes >= dqb->dqb_isoftlimit)
496 dqb->dqb_curinodes = dqbuf.dqb_curinodes;
497 dqb->dqb_curblocks = dqbuf.dqb_curblocks;
501 switch (qf->wordsize) {
503 return (quota_write32(qf, dqb, id));
505 return (quota_write64(qf, dqb, id));
514 * Convert a quota file from one format to another.
517 quota_convert(struct quotafile *qf, int wordsize)
519 struct quotafile *newqf;
523 int serrno, maxid, id, fd;
526 * Quotas must not be active and quotafile must be open
527 * for reading and writing.
529 if ((qf->accmode & O_RDWR) != O_RDWR || qf->fd == -1) {
533 if ((wordsize != 32 && wordsize != 64) ||
534 wordsize == qf->wordsize) {
538 maxid = quota_maxid(qf);
539 if ((newqf = calloc(1, sizeof(*qf))) == NULL) {
544 snprintf(newqf->qfname, MAXPATHLEN + 1, "%s_%d.orig", qf->qfname,
546 if (rename(qf->qfname, newqf->qfname) < 0) {
550 if ((newqf->fd = open(qf->qfname, O_RDWR|O_CREAT|O_TRUNC|O_CLOEXEC,
555 newqf->wordsize = wordsize;
556 if (wordsize == 64) {
557 memset(&dqh, 0, sizeof(dqh));
558 memcpy(dqh.dqh_magic, Q_DQHDR64_MAGIC, sizeof(dqh.dqh_magic));
559 dqh.dqh_version = htobe32(Q_DQHDR64_VERSION);
560 dqh.dqh_hdrlen = htobe32(sizeof(struct dqhdr64));
561 dqh.dqh_reclen = htobe32(sizeof(struct dqblk64));
562 if (write(newqf->fd, &dqh, sizeof(dqh)) != sizeof(dqh)) {
567 grp = getgrnam(QUOTAGROUP);
568 fchown(newqf->fd, 0, grp ? grp->gr_gid : 0);
569 fchmod(newqf->fd, 0640);
570 for (id = 0; id <= maxid; id++) {
571 if ((quota_read(qf, &dqblk, id)) < 0)
573 switch (newqf->wordsize) {
575 if ((quota_write32(newqf, &dqblk, id)) < 0)
579 if ((quota_write64(newqf, &dqblk, id)) < 0)
592 * Update the passed in quotafile to reference the new file
593 * of the converted format size.
598 qf->wordsize = newqf->wordsize;
602 /* put back the original file */
603 (void) rename(newqf->qfname, qf->qfname);