summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/libc/nls/ru_RU.KOI8-R.msg8
-rw-r--r--lib/libutil/Makefile10
-rw-r--r--lib/libutil/libutil.h10
-rw-r--r--lib/libutil/quotafile.3170
-rw-r--r--lib/libutil/quotafile.c426
-rw-r--r--libexec/rpc.rquotad/Makefile4
-rw-r--r--libexec/rpc.rquotad/rquotad.c28
-rw-r--r--sys/ufs/ufs/quota.h42
-rw-r--r--sys/ufs/ufs/ufs_quota.c359
-rw-r--r--sys/ufs/ufs/ufs_vfsops.c12
-rw-r--r--sys/ufs/ufs/ufsmount.h1
-rw-r--r--usr.sbin/edquota/Makefile4
-rw-r--r--usr.sbin/edquota/edquota.820
-rw-r--r--usr.sbin/edquota/edquota.c615
14 files changed, 1365 insertions, 344 deletions
diff --git a/lib/libc/nls/ru_RU.KOI8-R.msg b/lib/libc/nls/ru_RU.KOI8-R.msg
index 431bc27..55233b9 100644
--- a/lib/libc/nls/ru_RU.KOI8-R.msg
+++ b/lib/libc/nls/ru_RU.KOI8-R.msg
@@ -114,7 +114,7 @@ $ ENETRESET
$ ECONNABORTED
53 Программа вызвала аварийное прекращение подключения
$ ECONNRESET
-54 Подключение сброшено противоположной стороной
+54 Подключение сброшено противоположной строной
$ ENOBUFS
55 Не осталось места под буфер
$ EISCONN
@@ -146,7 +146,7 @@ $ EUSERS
$ EDQUOT
69 Превзойдена дисковая квота
$ ESTALE
-70 Устаревший дескриптор файла NFS
+70 Устаревший декриптор файла NFS
$ EREMOTE
71 Слишком много дистанционных переходов в пути
$ EBADRPC
@@ -180,9 +180,9 @@ $ ECANCELED
$ EILSEQ
86 Недопустимая последовательность байтов
$ ENOATTR
-87 Атрибут не найден
+87 Аттрибут не найден
$ EDOOFUS
-88 Ошибка программирования
+88 Ошибка програмирования
$
$ strsignal() support catalog
$
diff --git a/lib/libutil/Makefile b/lib/libutil/Makefile
index 2933649..d7bd6dc 100644
--- a/lib/libutil/Makefile
+++ b/lib/libutil/Makefile
@@ -12,8 +12,8 @@ SRCS= _secure_path.c auth.c expand_number.c flopen.c fparseln.c gr_util.c \
hexdump.c humanize_number.c kinfo_getfile.c kinfo_getvmmap.c kld.c \
login.c login_auth.c login_cap.c \
login_class.c login_crypt.c login_ok.c login_times.c login_tty.c \
- logout.c logwtmp.c pidfile.c property.c pty.c pw_util.c realhostname.c \
- stub.c trimdomain.c uucplock.c
+ logout.c logwtmp.c pidfile.c property.c pty.c pw_util.c quotafile.c \
+ realhostname.c stub.c trimdomain.c uucplock.c
INCS= libutil.h login_cap.h
WARNS?= 6
@@ -31,7 +31,7 @@ MAN+= kld.3 login.3 login_auth.3 login_tty.3 logout.3 logwtmp.3 pty.3 \
_secure_path.3 uucplock.3 property.3 auth.3 realhostname.3 \
realhostname_sa.3 trimdomain.3 fparseln.3 humanize_number.3 \
pidfile.3 flopen.3 expand_number.3 hexdump.3 \
- kinfo_getfile.3 kinfo_getvmmap.3
+ kinfo_getfile.3 kinfo_getvmmap.3 quotafile.3
MAN+= login.conf.5 auth.conf.5
MLINKS+= kld.3 kld_isloaded.3 kld.3 kld_load.3
MLINKS+= property.3 properties_read.3 property.3 properties_free.3
@@ -59,5 +59,9 @@ MLINKS+=pidfile.3 pidfile_open.3 \
pidfile.3 pidfile_write.3 \
pidfile.3 pidfile_close.3 \
pidfile.3 pidfile_remove.3
+MLINKS+=quotafile.3 quota_open.3 \
+ quotafile.3 quota_read.3 \
+ quotafile.3 quota_write.3 \
+ quotafile.3 quota_close.3
.include <bsd.lib.mk>
diff --git a/lib/libutil/libutil.h b/lib/libutil/libutil.h
index 3187fb3..afd5db6 100644
--- a/lib/libutil/libutil.h
+++ b/lib/libutil/libutil.h
@@ -140,6 +140,16 @@ int pidfile_close(struct pidfh *pfh);
int pidfile_remove(struct pidfh *pfh);
#endif
+#ifdef _UFS_UFS_QUOTA_H_
+struct quotafile;
+struct fstab;
+struct quotafile *quota_open(struct fstab *, int, int);
+void quota_close(struct quotafile *);
+int quota_read(struct quotafile *, struct dqblk *, int);
+int quota_write_limits(struct quotafile *, struct dqblk *, int);
+int quota_write_usage(struct quotafile *, struct dqblk *, int);
+#endif
+
__END_DECLS
#define UU_LOCK_INUSE (1)
diff --git a/lib/libutil/quotafile.3 b/lib/libutil/quotafile.3
new file mode 100644
index 0000000..07a08a2
--- /dev/null
+++ b/lib/libutil/quotafile.3
@@ -0,0 +1,170 @@
+.\"-
+.\" Copyright (c) 2008 Dag-Erling Coц╞dan Smц╦rgrav
+.\" All rights reserved.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\" notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\" notice, this list of conditions and the following disclaimer in the
+.\" documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+.\" SUCH DAMAGE.
+.\"
+.\" $FreeBSD$
+.\"
+.Dd February 14, 2009
+.Dt QUOTAFILE 3
+.Os
+.Sh NAME
+.Nm quota_open
+.Nm quota_read
+.Nm quota_write_limits
+.Nm quota_write_usage
+.Nm quota_close
+.Nd "Manipulate quotas"
+.Sh LIBRARY
+.Lb libutil
+.Sh SYNOPSIS
+.In ufs/ufs/quota.h
+.In libutil.h
+.In fstab.h
+.Ft "struct quotafile *"
+.Fn quota_open "struct fstab *fs" "int quotatype" "int openflags"
+.Ft int
+.Fn quota_read "struct quotafile *qf" "struct dqblk *dqb" "int id"
+.Ft int
+.Fn quota_write_limits "struct quotafile *qf" "struct dqblk *dqb" "int id"
+.Ft int
+.Fn quota_write_usage "struct quotafile *qf" "struct dqblk *dqb" "int id"
+.Ft int
+.Fn quota_close "struct quotafile *qf"
+.Sh DESCRIPTION
+These functions are designed to simplify access to filesystem quotas.
+If quotas are active on a filesystem,
+these functions will access them directly from the kernel using the
+.Fn quotactl
+system call.
+If quotas are not active,
+these functions will access them by reading and writing
+the quota files directly.
+.Pp
+The
+.Fn quota_open
+function takes a pointer to an
+.Vt fstab
+entry corresponding to the filesystem on which quotas
+are to be accessed.
+The
+.Va quotatype
+field indicates the type of quotas being sought, either
+.Dv USRQUOTA
+or
+.Dv GRPQUOTA .
+The
+.Va openflags
+are those used by the
+.Fn open
+system call, usually either
+.Dv O_RDONLY
+if the quotas are just to be read, or
+.Dv O_RDWR
+if the quotas are to be updated.
+The
+.Dv O_CREAT
+flag should be specified if a new quota file of the requested type should
+be created if it does not already exist.
+.Pp
+The
+.Fn quota_read
+function reads the quota from the filesystem and quota type referenced by
+.Va qf
+for the user (or group) specified by
+.Va id
+into the
+.Vt dqblk
+quota structure pointed to by
+.Va dqb .
+.Pp
+The
+.Fn quota_write_limits
+function updates the limit fields (but not the usage fields)
+for the filesystem and quota type referenced by
+.Va qf
+for the user (or group) specified by
+.Va id
+from the
+.Vt dqblk
+quota structure pointed to by
+.Va dqb .
+.Pp
+The
+.Fn quota_write_usage
+function updates the usage fields (but not the limit fields)
+for the filesystem and quota type referenced by
+.Va qf
+for the user (or group) specified by
+.Va id
+from the
+.Vt dqblk
+quota structure pointed to by
+.Va dqb .
+.Pp
+The
+.Fn quota_close
+function closes any open file descriptors and frees any storage
+associated with the filesystem and quota type referenced by
+.Va qf .
+.Sh RETURN VALUES
+If the filesystem has quotas associated with it,
+.Fn quota_open
+returns a pointer to a
+.Vt quotafile
+structure used in subsequent quota access calls.
+If the filesystem has no quotas, or access permission is denied
+.Dv NULL
+is returned and
+.Va errno
+is set to indicate the cause of failure.
+.Pp
+The
+.Fn quota_read ,
+.Fn quota_write_limits ,
+.Fn quota_write_usage ,
+and
+.Fn quota_close
+functions return zero on success.
+On error they return
+.Dv -1
+and set
+.Va errno
+to indicate the cause of failure.
+.Sh SEE ALSO
+.Xr quotactl 2 ,
+.Xr quota.user 5 ,
+.Xr quota.group 5
+.Sh HISTORY
+The
+.Nm
+functions first appeared in
+.Fx 8.0 .
+.Sh AUTHORS
+.An -nosplit
+The
+.Nm
+functions and this manual page were written by
+.An Dag-Erling Sm\(/orgrav Aq des@FreeBSD.org
+and
+.An Marshall Kirk McKusick .
diff --git a/lib/libutil/quotafile.c b/lib/libutil/quotafile.c
new file mode 100644
index 0000000..58c02b1
--- /dev/null
+++ b/lib/libutil/quotafile.c
@@ -0,0 +1,426 @@
+/*-
+ * Copyright (c) 2008 Dag-Erling Coц╞dan Smц╦rgrav
+ * Copyright (c) 2008 Marshall Kirk McKusick
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer
+ * in this position and unchanged.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#include <sys/types.h>
+#include <sys/endian.h>
+#include <sys/mount.h>
+#include <sys/stat.h>
+
+#include <ufs/ufs/quota.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <fstab.h>
+#include <grp.h>
+#include <pwd.h>
+#include <libutil.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+struct quotafile {
+ int fd; /* -1 means using quotactl for access */
+ int wordsize; /* 32-bit or 64-bit limits */
+ int quotatype; /* USRQUOTA or GRPQUOTA */
+ char fsname[MAXPATHLEN + 1]; /* mount point of filesystem */
+ char qfname[MAXPATHLEN + 1]; /* quota file if not using quotactl */
+};
+
+static const char *qfextension[] = INITQFNAMES;
+
+/*
+ * Check to see if a particular quota is to be enabled.
+ */
+static int
+hasquota(struct fstab *fs, int type, char *qfnamep, int qfbufsize)
+{
+ char *opt;
+ char *cp;
+ struct statfs sfb;
+ char buf[BUFSIZ];
+ static char initname, usrname[100], grpname[100];
+
+ if (!initname) {
+ (void)snprintf(usrname, sizeof(usrname), "%s%s",
+ qfextension[USRQUOTA], QUOTAFILENAME);
+ (void)snprintf(grpname, sizeof(grpname), "%s%s",
+ qfextension[GRPQUOTA], QUOTAFILENAME);
+ initname = 1;
+ }
+ strcpy(buf, fs->fs_mntops);
+ for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
+ if ((cp = index(opt, '=')))
+ *cp++ = '\0';
+ if (type == USRQUOTA && strcmp(opt, usrname) == 0)
+ break;
+ if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
+ break;
+ }
+ if (!opt)
+ return (0);
+ /*
+ * Ensure that the filesystem is mounted.
+ */
+ if (statfs(fs->fs_file, &sfb) != 0 ||
+ strcmp(fs->fs_file, sfb.f_mntonname)) {
+ return (0);
+ }
+ if (cp) {
+ strncpy(qfnamep, cp, qfbufsize);
+ } else {
+ (void)snprintf(qfnamep, qfbufsize, "%s/%s.%s", fs->fs_file,
+ QUOTAFILENAME, qfextension[type]);
+ }
+ return (1);
+}
+
+struct quotafile *
+quota_open(struct fstab *fs, int quotatype, int openflags)
+{
+ struct quotafile *qf;
+ struct dqhdr64 dqh;
+ struct group *grp;
+ int qcmd, serrno;
+
+ if ((qf = calloc(1, sizeof(*qf))) == NULL)
+ return (NULL);
+ qf->quotatype = quotatype;
+ strncpy(qf->fsname, fs->fs_file, sizeof(qf->fsname));
+ qcmd = QCMD(Q_GETQUOTA, quotatype);
+ if (quotactl(fs->fs_file, qcmd, 0, &dqh) == 0) {
+ qf->wordsize = 64;
+ qf->fd = -1;
+ return (qf);
+ }
+ if (!hasquota(fs, quotatype, qf->qfname, sizeof(qf->qfname))) {
+ free(qf);
+ errno = EOPNOTSUPP;
+ return (NULL);
+ }
+ if ((qf->fd = open(qf->qfname, openflags & O_ACCMODE)) < 0 &&
+ (openflags & O_CREAT) == 0) {
+ serrno = errno;
+ free(qf);
+ errno = serrno;
+ return (NULL);
+ }
+ /* File open worked, so process it */
+ if (qf->fd != -1) {
+ qf->wordsize = 32;
+ switch (read(qf->fd, &dqh, sizeof(dqh))) {
+ case -1:
+ serrno = errno;
+ close(qf->fd);
+ free(qf);
+ errno = serrno;
+ return (NULL);
+ case sizeof(dqh):
+ if (strcmp(dqh.dqh_magic, Q_DQHDR64_MAGIC) != 0) {
+ /* no magic, assume 32 bits */
+ qf->wordsize = 32;
+ return (qf);
+ }
+ if (be32toh(dqh.dqh_version) != Q_DQHDR64_VERSION ||
+ be32toh(dqh.dqh_hdrlen) != sizeof(struct dqhdr64) ||
+ be32toh(dqh.dqh_reclen) != sizeof(struct dqblk64)) {
+ /* correct magic, wrong version / lengths */
+ close(qf->fd);
+ free(qf);
+ errno = EINVAL;
+ return (NULL);
+ }
+ qf->wordsize = 64;
+ return (qf);
+ default:
+ qf->wordsize = 32;
+ return (qf);
+ }
+ /* not reached */
+ }
+ /* Open failed above, but O_CREAT specified, so create a new file */
+ if ((qf->fd = open(qf->qfname, O_RDWR|O_CREAT|O_TRUNC, 0)) < 0) {
+ serrno = errno;
+ free(qf);
+ errno = serrno;
+ return (NULL);
+ }
+ qf->wordsize = 64;
+ memset(&dqh, 0, sizeof(dqh));
+ memcpy(dqh.dqh_magic, Q_DQHDR64_MAGIC, sizeof(dqh.dqh_magic));
+ dqh.dqh_version = htobe32(Q_DQHDR64_VERSION);
+ dqh.dqh_hdrlen = htobe32(sizeof(struct dqhdr64));
+ dqh.dqh_reclen = htobe32(sizeof(struct dqblk64));
+ if (write(qf->fd, &dqh, sizeof(dqh)) != sizeof(dqh)) {
+ serrno = errno;
+ unlink(qf->qfname);
+ close(qf->fd);
+ free(qf);
+ errno = serrno;
+ return (NULL);
+ }
+ grp = getgrnam(QUOTAGROUP);
+ fchown(qf->fd, 0, grp ? grp->gr_gid : 0);
+ fchmod(qf->fd, 0640);
+ return (qf);
+}
+
+void
+quota_close(struct quotafile *qf)
+{
+
+ if (qf->fd != -1)
+ close(qf->fd);
+ free(qf);
+}
+
+static int
+quota_read32(struct quotafile *qf, struct dqblk *dqb, int id)
+{
+ struct dqblk32 dqb32;
+ off_t off;
+
+ off = id * sizeof(struct dqblk32);
+ if (lseek(qf->fd, off, SEEK_SET) == -1)
+ return (-1);
+ switch (read(qf->fd, &dqb32, sizeof(dqb32))) {
+ case 0:
+ memset(&dqb, 0, sizeof(*dqb));
+ return (0);
+ case sizeof(dqb32):
+ dqb->dqb_bhardlimit = dqb32.dqb_bhardlimit;
+ dqb->dqb_bsoftlimit = dqb32.dqb_bsoftlimit;
+ dqb->dqb_curblocks = dqb32.dqb_curblocks;
+ dqb->dqb_ihardlimit = dqb32.dqb_ihardlimit;
+ dqb->dqb_isoftlimit = dqb32.dqb_isoftlimit;
+ dqb->dqb_curinodes = dqb32.dqb_curinodes;
+ dqb->dqb_btime = dqb32.dqb_btime;
+ dqb->dqb_itime = dqb32.dqb_itime;
+ return (0);
+ default:
+ return (-1);
+ }
+}
+
+static int
+quota_read64(struct quotafile *qf, struct dqblk *dqb, int id)
+{
+ struct dqblk64 dqb64;
+ off_t off;
+
+ off = sizeof(struct dqhdr64) + id * sizeof(struct dqblk64);
+ if (lseek(qf->fd, off, SEEK_SET) == -1)
+ return (-1);
+ switch (read(qf->fd, &dqb64, sizeof(dqb64))) {
+ case 0:
+ memset(&dqb, 0, sizeof(*dqb));
+ return (0);
+ case sizeof(dqb64):
+ dqb->dqb_bhardlimit = be64toh(dqb64.dqb_bhardlimit);
+ dqb->dqb_bsoftlimit = be64toh(dqb64.dqb_bsoftlimit);
+ dqb->dqb_curblocks = be64toh(dqb64.dqb_curblocks);
+ dqb->dqb_ihardlimit = be64toh(dqb64.dqb_ihardlimit);
+ dqb->dqb_isoftlimit = be64toh(dqb64.dqb_isoftlimit);
+ dqb->dqb_curinodes = be64toh(dqb64.dqb_curinodes);
+ dqb->dqb_btime = be64toh(dqb64.dqb_btime);
+ dqb->dqb_itime = be64toh(dqb64.dqb_itime);
+ return (0);
+ default:
+ return (-1);
+ }
+}
+
+int
+quota_read(struct quotafile *qf, struct dqblk *dqb, int id)
+{
+ int qcmd;
+
+ if (qf->fd == -1) {
+ qcmd = QCMD(Q_GETQUOTA, qf->quotatype);
+ return (quotactl(qf->fsname, qcmd, id, dqb));
+ }
+ switch (qf->wordsize) {
+ case 32:
+ return quota_read32(qf, dqb, id);
+ case 64:
+ return quota_read64(qf, dqb, id);
+ default:
+ errno = EINVAL;
+ return (-1);
+ }
+ /* not reached */
+}
+
+#define CLIP32(u64) ((u64) > UINT32_MAX ? UINT32_MAX : (uint32_t)(u64))
+
+static int
+quota_write32(struct quotafile *qf, const struct dqblk *dqb, int id)
+{
+ struct dqblk32 dqb32;
+ off_t off;
+
+ dqb32.dqb_bhardlimit = CLIP32(dqb->dqb_bhardlimit);
+ dqb32.dqb_bsoftlimit = CLIP32(dqb->dqb_bsoftlimit);
+ dqb32.dqb_curblocks = CLIP32(dqb->dqb_curblocks);
+ dqb32.dqb_ihardlimit = CLIP32(dqb->dqb_ihardlimit);
+ dqb32.dqb_isoftlimit = CLIP32(dqb->dqb_isoftlimit);
+ dqb32.dqb_curinodes = CLIP32(dqb->dqb_curinodes);
+ dqb32.dqb_btime = CLIP32(dqb->dqb_btime);
+ dqb32.dqb_itime = CLIP32(dqb->dqb_itime);
+
+ off = id * sizeof(struct dqblk32);
+ if (lseek(qf->fd, off, SEEK_SET) == -1)
+ return (-1);
+ if (write(qf->fd, &dqb32, sizeof(dqb32)) == sizeof(dqb32))
+ return (0);
+ return (-1);
+}
+
+static int
+quota_write64(struct quotafile *qf, const struct dqblk *dqb, int id)
+{
+ struct dqblk64 dqb64;
+ off_t off;
+
+ dqb64.dqb_bhardlimit = htobe64(dqb->dqb_bhardlimit);
+ dqb64.dqb_bsoftlimit = htobe64(dqb->dqb_bsoftlimit);
+ dqb64.dqb_curblocks = htobe64(dqb->dqb_curblocks);
+ dqb64.dqb_ihardlimit = htobe64(dqb->dqb_ihardlimit);
+ dqb64.dqb_isoftlimit = htobe64(dqb->dqb_isoftlimit);
+ dqb64.dqb_curinodes = htobe64(dqb->dqb_curinodes);
+ dqb64.dqb_btime = htobe64(dqb->dqb_btime);
+ dqb64.dqb_itime = htobe64(dqb->dqb_itime);
+
+ off = sizeof(struct dqhdr64) + id * sizeof(struct dqblk64);
+ if (lseek(qf->fd, off, SEEK_SET) == -1)
+ return (-1);
+ if (write(qf->fd, &dqb64, sizeof(dqb64)) == sizeof(dqb64))
+ return (0);
+ return (-1);
+}
+
+int
+quota_write_usage(struct quotafile *qf, struct dqblk *dqb, int id)
+{
+ struct dqblk dqbuf;
+ int qcmd;
+
+ if (qf->fd == -1) {
+ qcmd = QCMD(Q_SETUSE, qf->quotatype);
+ return (quotactl(qf->fsname, qcmd, id, dqb));
+ }
+ /*
+ * Have to do read-modify-write of quota in file.
+ */
+ if (quota_read(qf, &dqbuf, id) != 0)
+ return (-1);
+ /*
+ * Reset time limit if have a soft limit and were
+ * previously under it, but are now over it.
+ */
+ if (dqbuf.dqb_bsoftlimit && id != 0 &&
+ dqbuf.dqb_curblocks < dqbuf.dqb_bsoftlimit &&
+ dqb->dqb_curblocks >= dqbuf.dqb_bsoftlimit)
+ dqbuf.dqb_btime = 0;
+ if (dqbuf.dqb_isoftlimit && id != 0 &&
+ dqbuf.dqb_curinodes < dqbuf.dqb_isoftlimit &&
+ dqb->dqb_curinodes >= dqbuf.dqb_isoftlimit)
+ dqbuf.dqb_itime = 0;
+ dqbuf.dqb_curinodes = dqb->dqb_curinodes;
+ dqbuf.dqb_curblocks = dqb->dqb_curblocks;
+ /*
+ * Write it back.
+ */
+ switch (qf->wordsize) {
+ case 32:
+ return quota_write32(qf, &dqbuf, id);
+ case 64:
+ return quota_write64(qf, &dqbuf, id);
+ default:
+ errno = EINVAL;
+ return (-1);
+ }
+ /* not reached */
+}
+
+int
+quota_write_limits(struct quotafile *qf, struct dqblk *dqb, int id)
+{
+ struct dqblk dqbuf;
+ int qcmd;
+
+ if (qf->fd == -1) {
+ qcmd = QCMD(Q_SETQUOTA, qf->quotatype);
+ return (quotactl(qf->fsname, qcmd, id, dqb));
+ }
+ /*
+ * Have to do read-modify-write of quota in file.
+ */
+ if (quota_read(qf, &dqbuf, id) != 0)
+ return (-1);
+ /*
+ * Reset time limit if have a soft limit and were
+ * previously under it, but are now over it
+ * or if there previously was no soft limit, but
+ * now have one and are over it.
+ */
+ if (dqbuf.dqb_bsoftlimit && id != 0 &&
+ dqbuf.dqb_curblocks < dqbuf.dqb_bsoftlimit &&
+ dqbuf.dqb_curblocks >= dqb->dqb_bsoftlimit)
+ dqb->dqb_btime = 0;
+ if (dqbuf.dqb_bsoftlimit == 0 && id != 0 &&
+ dqb->dqb_bsoftlimit > 0 &&
+ dqbuf.dqb_curblocks >= dqb->dqb_bsoftlimit)
+ dqb->dqb_btime = 0;
+ if (dqbuf.dqb_isoftlimit && id != 0 &&
+ dqbuf.dqb_curinodes < dqbuf.dqb_isoftlimit &&
+ dqbuf.dqb_curinodes >= dqb->dqb_isoftlimit)
+ dqb->dqb_itime = 0;
+ if (dqbuf.dqb_isoftlimit == 0 && id !=0 &&
+ dqb->dqb_isoftlimit > 0 &&
+ dqbuf.dqb_curinodes >= dqb->dqb_isoftlimit)
+ dqb->dqb_itime = 0;
+ dqb->dqb_curinodes = dqbuf.dqb_curinodes;
+ dqb->dqb_curblocks = dqbuf.dqb_curblocks;
+ /*
+ * Write it back.
+ */
+ switch (qf->wordsize) {
+ case 32:
+ return quota_write32(qf, dqb, id);
+ case 64:
+ return quota_write64(qf, dqb, id);
+ default:
+ errno = EINVAL;
+ return (-1);
+ }
+ /* not reached */
+}
diff --git a/libexec/rpc.rquotad/Makefile b/libexec/rpc.rquotad/Makefile
index feacce5..fb3f38bc 100644
--- a/libexec/rpc.rquotad/Makefile
+++ b/libexec/rpc.rquotad/Makefile
@@ -6,7 +6,7 @@ MAN = rpc.rquotad.8
WARNS ?= 6
-DPADD= ${LIBRPCSVC}
-LDADD= -lrpcsvc
+DPADD= ${LIBRPCSVC} ${LIBUTIL}
+LDADD= -lrpcsvc -lutil
.include <bsd.prog.mk>
diff --git a/libexec/rpc.rquotad/rquotad.c b/libexec/rpc.rquotad/rquotad.c
index fe82742..2499c48 100644
--- a/libexec/rpc.rquotad/rquotad.c
+++ b/libexec/rpc.rquotad/rquotad.c
@@ -23,6 +23,7 @@ __FBSDID("$FreeBSD$");
#include <errno.h>
#include <fstab.h>
#include <grp.h>
+#include <libutil.h>
#include <pwd.h>
#include <signal.h>
#include <stdio.h>
@@ -245,9 +246,10 @@ initfs(void)
int
getfsquota(long id, char *path, struct dqblk *dqblk)
{
+ struct quotafile *qf;
struct stat st_path;
struct fs_stat *fs;
- int qcmd, fd, ret = 0;
+ int qcmd, ret = 0;
if (stat(path, &st_path) < 0)
return (0);
@@ -263,32 +265,16 @@ getfsquota(long id, char *path, struct dqblk *dqblk)
if (quotactl(fs->fs_file, qcmd, id, dqblk) == 0)
return (1);
- if ((fd = open(fs->qfpathname, O_RDONLY)) < 0) {
+ if ((qf = quota_open(fs->qfpathname)) == NULL) {
syslog(LOG_ERR, "open error: %s: %m", fs->qfpathname);
return (0);
}
- if (lseek(fd, (off_t)(id * sizeof(struct dqblk)), L_SET) == (off_t)-1) {
- close(fd);
- return (1);
- }
- switch (read(fd, dqblk, sizeof(struct dqblk))) {
- case 0:
- /*
- * Convert implicit 0 quota (EOF)
- * into an explicit one (zero'ed dqblk)
- */
- bzero(dqblk, sizeof(struct dqblk));
- ret = 1;
- break;
- case sizeof(struct dqblk): /* OK */
- ret = 1;
- break;
- default: /* ERROR */
+ if (quota_read(qf, dqblk, id) != 0) {
syslog(LOG_ERR, "read error: %s: %m", fs->qfpathname);
- close(fd);
+ quota_close(qf);
return (0);
}
- close(fd);
+ quota_close(qf);
}
return (ret);
}
diff --git a/sys/ufs/ufs/quota.h b/sys/ufs/ufs/quota.h
index 28f4e92..82b0170 100644
--- a/sys/ufs/ufs/quota.h
+++ b/sys/ufs/ufs/quota.h
@@ -81,10 +81,13 @@
#define Q_QUOTAON 0x0100 /* enable quotas */
#define Q_QUOTAOFF 0x0200 /* disable quotas */
-#define Q_GETQUOTA 0x0300 /* get limits and usage */
-#define Q_SETQUOTA 0x0400 /* set limits and usage */
-#define Q_SETUSE 0x0500 /* set usage */
+#define Q_GETQUOTA32 0x0300 /* get limits and usage (32-bit version) */
+#define Q_SETQUOTA32 0x0400 /* set limits and usage (32-bit version) */
+#define Q_SETUSE32 0x0500 /* set usage (32-bit version) */
#define Q_SYNC 0x0600 /* sync disk copy of a filesystems quotas */
+#define Q_GETQUOTA 0x0700 /* get limits and usage (64-bit version) */
+#define Q_SETQUOTA 0x0800 /* set limits and usage (64-bit version) */
+#define Q_SETUSE 0x0900 /* set usage (64-bit version) */
/*
* The following structure defines the format of the disk quota file
@@ -93,7 +96,7 @@
* the vnode for each quota file (a pointer is retained in the ufsmount
* structure).
*/
-struct dqblk {
+struct dqblk32 {
u_int32_t dqb_bhardlimit; /* absolute limit on disk blks alloc */
u_int32_t dqb_bsoftlimit; /* preferred limit on disk blks */
u_int32_t dqb_curblocks; /* current block count */
@@ -104,6 +107,30 @@ struct dqblk {
int32_t dqb_itime; /* time limit for excessive files */
};
+struct dqblk64 {
+ u_int64_t dqb_bhardlimit; /* absolute limit on disk blks alloc */
+ u_int64_t dqb_bsoftlimit; /* preferred limit on disk blks */
+ u_int64_t dqb_curblocks; /* current block count */
+ u_int64_t dqb_ihardlimit; /* maximum # allocated inodes + 1 */
+ u_int64_t dqb_isoftlimit; /* preferred inode limit */
+ u_int64_t dqb_curinodes; /* current # allocated inodes */
+ int64_t dqb_btime; /* time limit for excessive disk use */
+ int64_t dqb_itime; /* time limit for excessive files */
+};
+
+#define dqblk dqblk64
+
+#define Q_DQHDR64_MAGIC "QUOTA64"
+#define Q_DQHDR64_VERSION 0x20081104
+
+struct dqhdr64 {
+ char dqh_magic[8]; /* Q_DQHDR64_MAGIC */
+ uint32_t dqh_version; /* Q_DQHDR64_VERSION */
+ uint32_t dqh_hdrlen; /* header length */
+ uint32_t dqh_reclen; /* record length */
+ char dqh_unused[44]; /* reserved for future extension */
+};
+
#ifdef _KERNEL
#include <sys/queue.h>
@@ -125,7 +152,7 @@ struct dquot {
u_int32_t dq_id; /* identifier this applies to */
struct ufsmount *dq_ump; /* (h) filesystem that this is
taken from */
- struct dqblk dq_dqb; /* actual usage & quotas */
+ struct dqblk64 dq_dqb; /* actual usage & quotas */
};
/*
* Flag values.
@@ -199,10 +226,13 @@ void dqinit(void);
void dqrele(struct vnode *, struct dquot *);
void dquninit(void);
int getinoquota(struct inode *);
-int getquota(struct thread *, struct mount *, u_long, int, void *);
int qsync(struct mount *mp);
int quotaoff(struct thread *td, struct mount *, int);
int quotaon(struct thread *td, struct mount *, int, void *);
+int getquota32(struct thread *, struct mount *, u_long, int, void *);
+int setquota32(struct thread *, struct mount *, u_long, int, void *);
+int setuse32(struct thread *, struct mount *, u_long, int, void *);
+int getquota(struct thread *, struct mount *, u_long, int, void *);
int setquota(struct thread *, struct mount *, u_long, int, void *);
int setuse(struct thread *, struct mount *, u_long, int, void *);
vfs_quotactl_t ufs_quotactl;
diff --git a/sys/ufs/ufs/ufs_quota.c b/sys/ufs/ufs/ufs_quota.c
index 2db0444..5b1da50 100644
--- a/sys/ufs/ufs/ufs_quota.c
+++ b/sys/ufs/ufs/ufs_quota.c
@@ -39,6 +39,7 @@ __FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/systm.h>
+#include <sys/endian.h>
#include <sys/fcntl.h>
#include <sys/kernel.h>
#include <sys/lock.h>
@@ -59,6 +60,8 @@ __FBSDID("$FreeBSD$");
#include <ufs/ufs/ufsmount.h>
#include <ufs/ufs/ufs_extern.h>
+CTASSERT(sizeof(struct dqblk64) == sizeof(struct dqhdr64));
+
static int unprivileged_get_quota = 0;
SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_get_quota, CTLFLAG_RW,
&unprivileged_get_quota, 0,
@@ -73,6 +76,7 @@ static char *quotatypes[] = INITQFNAMES;
static int chkdqchg(struct inode *, ufs2_daddr_t, struct ucred *, int, int *);
static int chkiqchg(struct inode *, int, struct ucred *, int, int *);
+static int dqopen(struct vnode *, struct ufsmount *, int);
static int dqget(struct vnode *,
u_long, struct ufsmount *, int, struct dquot **);
static int dqsync(struct vnode *, struct dquot *);
@@ -80,6 +84,14 @@ static void dqflush(struct vnode *);
static int quotaoff1(struct thread *td, struct mount *mp, int type);
static int quotaoff_inchange(struct thread *td, struct mount *mp, int type);
+/* conversion functions - from_to() */
+static void dqb32_dq(const struct dqblk32 *, struct dquot *);
+static void dqb64_dq(const struct dqblk64 *, struct dquot *);
+static void dq_dqb32(const struct dquot *, struct dqblk32 *);
+static void dq_dqb64(const struct dquot *, struct dqblk64 *);
+static void dqb32_dqb64(const struct dqblk32 *, struct dqblk64 *);
+static void dqb64_dqb32(const struct dqblk64 *, struct dqblk32 *);
+
#ifdef DIAGNOSTIC
static void dqref(struct dquot *);
static void chkdquot(struct inode *);
@@ -90,7 +102,7 @@ static void chkdquot(struct inode *);
*
* This routine completely defines the semantics of quotas.
* If other criterion want to be used to establish quotas, the
- * MAXQUOTAS value in quotas.h should be increased, and the
+ * MAXQUOTAS value in quota.h should be increased, and the
* additional dquots set up here.
*/
int
@@ -522,6 +534,13 @@ quotaon(struct thread *td, struct mount *mp, int type, void *fname)
return (EALREADY);
}
ump->um_qflags[type] |= QTF_OPENING|QTF_CLOSING;
+ if ((error = dqopen(vp, ump, type)) != 0) {
+ ump->um_qflags[type] &= ~(QTF_OPENING|QTF_CLOSING);
+ UFS_UNLOCK(ump);
+ (void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
+ VFS_UNLOCK_GIANT(vfslocked);
+ return (error);
+ }
MNT_ILOCK(mp);
mp->mnt_flag |= MNT_QUOTA;
MNT_IUNLOCK(mp);
@@ -734,8 +753,9 @@ quotaoff(struct thread *td, struct mount *mp, int type)
/*
* Q_GETQUOTA - return current values in a dqblk structure.
*/
-int
-getquota(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
+static int
+_getquota(struct thread *td, struct mount *mp, u_long id, int type,
+ struct dqblk64 *dqb)
{
struct dquot *dq;
int error;
@@ -766,7 +786,7 @@ getquota(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
error = dqget(NULLVP, id, VFSTOUFS(mp), type, &dq);
if (error)
return (error);
- error = copyout(&dq->dq_dqb, addr, sizeof (struct dqblk));
+ *dqb = dq->dq_dqb;
dqrele(NULLVP, dq);
return (error);
}
@@ -774,23 +794,21 @@ getquota(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
/*
* Q_SETQUOTA - assign an entire dqblk structure.
*/
-int
-setquota(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
+static int
+_setquota(struct thread *td, struct mount *mp, u_long id, int type,
+ struct dqblk64 *dqb)
{
struct dquot *dq;
struct dquot *ndq;
struct ufsmount *ump;
- struct dqblk newlim;
+ struct dqblk64 newlim;
int error;
error = priv_check(td, PRIV_VFS_SETQUOTA);
if (error)
return (error);
- ump = VFSTOUFS(mp);
- error = copyin(addr, &newlim, sizeof (struct dqblk));
- if (error)
- return (error);
+ newlim = *dqb;
ndq = NODQUOT;
ump = VFSTOUFS(mp);
@@ -839,23 +857,21 @@ setquota(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
/*
* Q_SETUSE - set current inode and block usage.
*/
-int
-setuse(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
+static int
+_setuse(struct thread *td, struct mount *mp, u_long id, int type,
+ struct dqblk64 *dqb)
{
struct dquot *dq;
struct ufsmount *ump;
struct dquot *ndq;
- struct dqblk usage;
+ struct dqblk64 usage;
int error;
error = priv_check(td, PRIV_UFS_SETUSE);
if (error)
return (error);
- ump = VFSTOUFS(mp);
- error = copyin(addr, &usage, sizeof (struct dqblk));
- if (error)
- return (error);
+ usage = *dqb;
ump = VFSTOUFS(mp);
ndq = NODQUOT;
@@ -888,6 +904,90 @@ setuse(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
return (0);
}
+int
+getquota32(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
+{
+ struct dqblk32 dqb32;
+ struct dqblk64 dqb64;
+ int error;
+
+ error = _getquota(td, mp, id, type, &dqb64);
+ if (error)
+ return (error);
+ dqb64_dqb32(&dqb64, &dqb32);
+ error = copyout(&dqb32, addr, sizeof(dqb32));
+ return (error);
+}
+
+int
+setquota32(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
+{
+ struct dqblk32 dqb32;
+ struct dqblk64 dqb64;
+ int error;
+
+ error = copyin(addr, &dqb32, sizeof(dqb32));
+ if (error)
+ return (error);
+ dqb32_dqb64(&dqb32, &dqb64);
+ error = _setquota(td, mp, id, type, &dqb64);
+ return (error);
+}
+
+int
+setuse32(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
+{
+ struct dqblk32 dqb32;
+ struct dqblk64 dqb64;
+ int error;
+
+ error = copyin(addr, &dqb32, sizeof(dqb32));
+ if (error)
+ return (error);
+ dqb32_dqb64(&dqb32, &dqb64);
+ error = _setuse(td, mp, id, type, &dqb64);
+ return (error);
+}
+
+int
+getquota(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
+{
+ struct dqblk64 dqb64;
+ int error;
+
+ error = _getquota(td, mp, id, type, &dqb64);
+ if (error)
+ return (error);
+ error = copyout(&dqb64, addr, sizeof(dqb64));
+ return (error);
+}
+
+int
+setquota(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
+{
+ struct dqblk64 dqb64;
+ int error;
+
+ error = copyin(addr, &dqb64, sizeof(dqb64));
+ if (error)
+ return (error);
+ error = _setquota(td, mp, id, type, &dqb64);
+ return (error);
+}
+
+int
+setuse(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
+{
+ struct dqblk64 dqb64;
+ int error;
+
+ error = copyin(addr, &dqb64, sizeof(dqb64));
+ if (error)
+ return (error);
+ error = _setuse(td, mp, id, type, &dqb64);
+ return (error);
+}
+
/*
* Q_SYNC - sync quota files to disk.
*/
@@ -1025,6 +1125,55 @@ dqhashfind(struct dqhash *dqh, u_long id, struct vnode *dqvp)
}
/*
+ * Determine the quota file type.
+ *
+ * A 32-bit quota file is simply an array of struct dqblk32.
+ *
+ * A 64-bit quota file is a struct dqhdr64 followed by an array of struct
+ * dqblk64. The header contains various magic bits which allow us to be
+ * reasonably confident that it is indeeda 64-bit quota file and not just
+ * a 32-bit quota file that just happens to "look right".
+ *
+ */
+static int
+dqopen(struct vnode *vp, struct ufsmount *ump, int type)
+{
+ struct dqhdr64 dqh;
+ struct iovec aiov;
+ struct uio auio;
+ int error, vfslocked;
+
+ auio.uio_iov = &aiov;
+ auio.uio_iovcnt = 1;
+ aiov.iov_base = &dqh;
+ aiov.iov_len = sizeof(dqh);
+ auio.uio_resid = sizeof(dqh);
+ auio.uio_offset = 0;
+ auio.uio_segflg = UIO_SYSSPACE;
+ auio.uio_rw = UIO_READ;
+ auio.uio_td = (struct thread *)0;
+ vfslocked = VFS_LOCK_GIANT(vp->v_mount);
+ error = VOP_READ(vp, &auio, 0, ump->um_cred[type]);
+ VFS_UNLOCK_GIANT(vfslocked);
+
+ if (error != 0)
+ return (error);
+ if (auio.uio_resid > 0) {
+ /* assume 32 bits */
+ return (0);
+ }
+
+ if (strcmp(dqh.dqh_magic, Q_DQHDR64_MAGIC) == 0 &&
+ be32toh(dqh.dqh_version) == Q_DQHDR64_VERSION &&
+ be32toh(dqh.dqh_hdrlen) == (uint32_t)sizeof(struct dqhdr64) &&
+ be32toh(dqh.dqh_reclen) == (uint32_t)sizeof(struct dqblk64))
+ ump->um_qflags[type] |= QTF_64BIT;
+ /* XXX: what if the magic matches, but the sizes are wrong? */
+
+ return (0);
+}
+
+/*
* Obtain a dquot structure for the specified identifier and quota file
* reading the information from the file if necessary.
*/
@@ -1032,6 +1181,8 @@ static int
dqget(struct vnode *vp, u_long id, struct ufsmount *ump, int type,
struct dquot **dqp)
{
+ uint8_t buf[sizeof(struct dqblk64)];
+ off_t base, recsize;
struct dquot *dq, *dq1;
struct dqhash *dqh;
struct vnode *dqvp;
@@ -1121,8 +1272,7 @@ hfound: DQI_LOCK(dq);
if (numdquot < desireddquot) {
numdquot++;
DQH_UNLOCK();
- dq1 = (struct dquot *)malloc(sizeof *dq, M_DQUOT,
- M_WAITOK | M_ZERO);
+ dq1 = malloc(sizeof *dq1, M_DQUOT, M_WAITOK | M_ZERO);
mtx_init(&dq1->dq_lock, "dqlock", NULL, MTX_DEF);
DQH_LOCK();
/*
@@ -1169,20 +1319,37 @@ hfound: DQI_LOCK(dq);
DQREF(dq);
DQH_UNLOCK();
+ /*
+ * Read the requested quota record from the quota file, performing
+ * any necessary conversions.
+ */
+ if (ump->um_qflags[type] & QTF_64BIT) {
+ recsize = sizeof(struct dqblk64);
+ base = sizeof(struct dqhdr64);
+ } else {
+ recsize = sizeof(struct dqblk32);
+ base = 0;
+ }
auio.uio_iov = &aiov;
auio.uio_iovcnt = 1;
- aiov.iov_base = &dq->dq_dqb;
- aiov.iov_len = sizeof (struct dqblk);
- auio.uio_resid = sizeof (struct dqblk);
- auio.uio_offset = (off_t)id * sizeof (struct dqblk);
+ aiov.iov_base = buf;
+ aiov.iov_len = recsize;
+ auio.uio_resid = recsize;
+ auio.uio_offset = base + id * recsize;
auio.uio_segflg = UIO_SYSSPACE;
auio.uio_rw = UIO_READ;
auio.uio_td = (struct thread *)0;
vfslocked = VFS_LOCK_GIANT(dqvp->v_mount);
error = VOP_READ(dqvp, &auio, 0, ump->um_cred[type]);
- if (auio.uio_resid == sizeof(struct dqblk) && error == 0)
- bzero(&dq->dq_dqb, sizeof(struct dqblk));
+ if (auio.uio_resid == recsize && error == 0) {
+ bzero(&dq->dq_dqb, sizeof(dq->dq_dqb));
+ } else {
+ if (ump->um_qflags[type] & QTF_64BIT)
+ dqb64_dq((struct dqblk64 *)buf, dq);
+ else
+ dqb32_dq((struct dqblk32 *)buf, dq);
+ }
if (dqvplocked)
vput(dqvp);
else
@@ -1293,6 +1460,8 @@ sync:
static int
dqsync(struct vnode *vp, struct dquot *dq)
{
+ uint8_t buf[sizeof(struct dqblk64)];
+ off_t base, recsize;
struct vnode *dqvp;
struct iovec aiov;
struct uio auio;
@@ -1339,12 +1508,26 @@ dqsync(struct vnode *vp, struct dquot *dq)
dq->dq_flags |= DQ_LOCK;
DQI_UNLOCK(dq);
+ /*
+ * Write the quota record to the quota file, performing any
+ * necessary conversions. See dqget() for additional details.
+ */
+ if (ump->um_qflags[dq->dq_type] & QTF_64BIT) {
+ dq_dqb64(dq, (struct dqblk64 *)buf);
+ recsize = sizeof(struct dqblk64);
+ base = sizeof(struct dqhdr64);
+ } else {
+ dq_dqb32(dq, (struct dqblk32 *)buf);
+ recsize = sizeof(struct dqblk32);
+ base = 0;
+ }
+
auio.uio_iov = &aiov;
auio.uio_iovcnt = 1;
- aiov.iov_base = &dq->dq_dqb;
- aiov.iov_len = sizeof (struct dqblk);
- auio.uio_resid = sizeof (struct dqblk);
- auio.uio_offset = (off_t)dq->dq_id * sizeof (struct dqblk);
+ aiov.iov_base = buf;
+ aiov.iov_len = recsize;
+ auio.uio_resid = recsize;
+ auio.uio_offset = base + dq->dq_id * recsize;
auio.uio_segflg = UIO_SYSSPACE;
auio.uio_rw = UIO_WRITE;
auio.uio_td = (struct thread *)0;
@@ -1357,7 +1540,8 @@ dqsync(struct vnode *vp, struct dquot *dq)
DQI_LOCK(dq);
DQI_WAKEUP(dq);
dq->dq_flags &= ~DQ_MOD;
-out: DQI_UNLOCK(dq);
+out:
+ DQI_UNLOCK(dq);
vfslocked = VFS_LOCK_GIANT(dqvp->v_mount);
if (vp != dqvp)
vput(dqvp);
@@ -1396,3 +1580,116 @@ dqflush(struct vnode *vp)
}
DQH_UNLOCK();
}
+
+/*
+ * 32-bit / 64-bit conversion functions.
+ *
+ * 32-bit quota records are stored in native byte order. Attention must
+ * be paid to overflow issues.
+ *
+ * 64-bit quota records are stored in network byte order.
+ */
+
+#define CLIP32(u64) (u64 > UINT32_MAX ? UINT32_MAX : (uint32_t)u64)
+
+/*
+ * Convert 32-bit host-order structure to dquot.
+ */
+static void
+dqb32_dq(const struct dqblk32 *dqb32, struct dquot *dq)
+{
+
+ dq->dq_bhardlimit = dqb32->dqb_bhardlimit;
+ dq->dq_bsoftlimit = dqb32->dqb_bsoftlimit;
+ dq->dq_curblocks = dqb32->dqb_curblocks;
+ dq->dq_ihardlimit = dqb32->dqb_ihardlimit;
+ dq->dq_isoftlimit = dqb32->dqb_isoftlimit;
+ dq->dq_curinodes = dqb32->dqb_curinodes;
+ dq->dq_btime = dqb32->dqb_btime;
+ dq->dq_itime = dqb32->dqb_itime;
+}
+
+/*
+ * Convert 64-bit network-order structure to dquot.
+ */
+static void
+dqb64_dq(const struct dqblk64 *dqb64, struct dquot *dq)
+{
+
+ dq->dq_bhardlimit = be64toh(dqb64->dqb_bhardlimit);
+ dq->dq_bsoftlimit = be64toh(dqb64->dqb_bsoftlimit);
+ dq->dq_curblocks = be64toh(dqb64->dqb_curblocks);
+ dq->dq_ihardlimit = be64toh(dqb64->dqb_ihardlimit);
+ dq->dq_isoftlimit = be64toh(dqb64->dqb_isoftlimit);
+ dq->dq_curinodes = be64toh(dqb64->dqb_curinodes);
+ dq->dq_btime = be64toh(dqb64->dqb_btime);
+ dq->dq_itime = be64toh(dqb64->dqb_itime);
+}
+
+/*
+ * Convert dquot to 32-bit host-order structure.
+ */
+static void
+dq_dqb32(const struct dquot *dq, struct dqblk32 *dqb32)
+{
+
+ dqb32->dqb_bhardlimit = CLIP32(dq->dq_bhardlimit);
+ dqb32->dqb_bsoftlimit = CLIP32(dq->dq_bsoftlimit);
+ dqb32->dqb_curblocks = CLIP32(dq->dq_curblocks);
+ dqb32->dqb_ihardlimit = CLIP32(dq->dq_ihardlimit);
+ dqb32->dqb_isoftlimit = CLIP32(dq->dq_isoftlimit);
+ dqb32->dqb_curinodes = CLIP32(dq->dq_curinodes);
+ dqb32->dqb_btime = CLIP32(dq->dq_btime);
+ dqb32->dqb_itime = CLIP32(dq->dq_itime);
+}
+
+/*
+ * Convert dquot to 64-bit network-order structure.
+ */
+static void
+dq_dqb64(const struct dquot *dq, struct dqblk64 *dqb64)
+{
+
+ dqb64->dqb_bhardlimit = htobe64(dq->dq_bhardlimit);
+ dqb64->dqb_bsoftlimit = htobe64(dq->dq_bsoftlimit);
+ dqb64->dqb_curblocks = htobe64(dq->dq_curblocks);
+ dqb64->dqb_ihardlimit = htobe64(dq->dq_ihardlimit);
+ dqb64->dqb_isoftlimit = htobe64(dq->dq_isoftlimit);
+ dqb64->dqb_curinodes = htobe64(dq->dq_curinodes);
+ dqb64->dqb_btime = htobe64(dq->dq_btime);
+ dqb64->dqb_itime = htobe64(dq->dq_itime);
+}
+
+/*
+ * Convert 64-bit host-order structure to 32-bit host-order structure.
+ */
+static void
+dqb64_dqb32(const struct dqblk64 *dqb64, struct dqblk32 *dqb32)
+{
+
+ dqb32->dqb_bhardlimit = CLIP32(dqb64->dqb_bhardlimit);
+ dqb32->dqb_bsoftlimit = CLIP32(dqb64->dqb_bsoftlimit);
+ dqb32->dqb_curblocks = CLIP32(dqb64->dqb_curblocks);
+ dqb32->dqb_ihardlimit = CLIP32(dqb64->dqb_ihardlimit);
+ dqb32->dqb_isoftlimit = CLIP32(dqb64->dqb_isoftlimit);
+ dqb32->dqb_curinodes = CLIP32(dqb64->dqb_curinodes);
+ dqb32->dqb_btime = CLIP32(dqb64->dqb_btime);
+ dqb32->dqb_itime = CLIP32(dqb64->dqb_itime);
+}
+
+/*
+ * Convert 32-bit host-order structure to 64-bit host-order structure.
+ */
+static void
+dqb32_dqb64(const struct dqblk32 *dqb32, struct dqblk64 *dqb64)
+{
+
+ dqb64->dqb_bhardlimit = dqb32->dqb_bhardlimit;
+ dqb64->dqb_bsoftlimit = dqb32->dqb_bsoftlimit;
+ dqb64->dqb_curblocks = dqb32->dqb_curblocks;
+ dqb64->dqb_ihardlimit = dqb32->dqb_ihardlimit;
+ dqb64->dqb_isoftlimit = dqb32->dqb_isoftlimit;
+ dqb64->dqb_curinodes = dqb32->dqb_curinodes;
+ dqb64->dqb_btime = dqb32->dqb_btime;
+ dqb64->dqb_itime = dqb32->dqb_itime;
+}
diff --git a/sys/ufs/ufs/ufs_vfsops.c b/sys/ufs/ufs/ufs_vfsops.c
index f6b6b1e..3abd5eb 100644
--- a/sys/ufs/ufs/ufs_vfsops.c
+++ b/sys/ufs/ufs/ufs_vfsops.c
@@ -127,6 +127,18 @@ ufs_quotactl(mp, cmds, id, arg)
error = quotaoff(td, mp, type);
break;
+ case Q_SETQUOTA32:
+ error = setquota32(td, mp, id, type, arg);
+ break;
+
+ case Q_SETUSE32:
+ error = setuse32(td, mp, id, type, arg);
+ break;
+
+ case Q_GETQUOTA32:
+ error = getquota32(td, mp, id, type, arg);
+ break;
+
case Q_SETQUOTA:
error = setquota(td, mp, id, type, arg);
break;
diff --git a/sys/ufs/ufs/ufsmount.h b/sys/ufs/ufs/ufsmount.h
index 126867b..83f9af0 100644
--- a/sys/ufs/ufs/ufsmount.h
+++ b/sys/ufs/ufs/ufsmount.h
@@ -120,6 +120,7 @@ struct ufsmount {
*/
#define QTF_OPENING 0x01 /* Q_QUOTAON in progress */
#define QTF_CLOSING 0x02 /* Q_QUOTAOFF in progress */
+#define QTF_64BIT 0x04 /* 64-bit quota file */
/* Convert mount ptr to ufsmount ptr. */
#define VFSTOUFS(mp) ((struct ufsmount *)((mp)->mnt_data))
diff --git a/usr.sbin/edquota/Makefile b/usr.sbin/edquota/Makefile
index e5a3b12..1196e47 100644
--- a/usr.sbin/edquota/Makefile
+++ b/usr.sbin/edquota/Makefile
@@ -4,6 +4,10 @@
PROG= edquota
MAN= edquota.8
+CSTD= gnu99
WARNS?= 4
+DPADD= ${LIBUTIL}
+LDADD= -lutil
+
.include <bsd.prog.mk>
diff --git a/usr.sbin/edquota/edquota.8 b/usr.sbin/edquota/edquota.8
index 7cf72dd..326c837 100644
--- a/usr.sbin/edquota/edquota.8
+++ b/usr.sbin/edquota/edquota.8
@@ -39,7 +39,7 @@
.Nd edit user quotas
.Sh SYNOPSIS
.Nm
-.Op Fl u
+.Op Fl uh
.Op Fl f Ar fspath
.Op Fl p Ar proto-username
.Ar username ...
@@ -53,6 +53,7 @@
.Ar username ...
.Nm
.Fl g
+.Op Fl h
.Op Fl f Ar fspath
.Op Fl p Ar proto-groupname
.Ar groupname ...
@@ -97,6 +98,17 @@ unless the environment variable
specifies otherwise.
.Pp
The quotas may then be modified, new quotas added, etc.
+Block quotas can be specified in bytes (B), kilobytes (K),
+megabytes (M), terabytes (T), petabytes (P), or exabytes (E).
+If no units are specified, kilobytes are assumed.
+Inode quotas can be specified in kiloinodes (K),
+megainodes (M), terainodes (T), petainodes (P), or exainodes (E).
+If no units are specified, the number of inodes specified are used.
+If the
+.Fl h
+flag is specified, the editor will always display the
+block usage and limits in a more human readable format
+rather than displaying them in the historic kilobyte format.
Setting a quota to zero indicates that no quota should be imposed.
Setting a hard limit to one indicates that no allocations should
be permitted.
@@ -159,6 +171,12 @@ and
.Ar ihlim
values is omitted, it is assumed to be zero, therefore
indicating that no particular quota should be imposed.
+Block quotas can be specified in bytes (B), kilobytes (K),
+megabytes (M), terabytes (T), petabytes (P), or exabytes (E).
+If no units are specified, kilobytes are assumed.
+Inode quotas can be specified in kiloinodes (K),
+megainodes (M), terainodes (T), petainodes (P), or exainodes (E).
+If no units are specified, the number of inodes specified are used.
.Pp
If invoked with the
.Fl f
diff --git a/usr.sbin/edquota/edquota.c b/usr.sbin/edquota/edquota.c
index f38fa77..b2e57a1 100644
--- a/usr.sbin/edquota/edquota.c
+++ b/usr.sbin/edquota/edquota.c
@@ -55,42 +55,59 @@ __FBSDID("$FreeBSD$");
#include <sys/mount.h>
#include <sys/wait.h>
#include <ufs/ufs/quota.h>
+
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <fstab.h>
#include <grp.h>
+#include <inttypes.h>
+#include <libutil.h>
#include <pwd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
+
#include "pathnames.h"
-const char *qfname = QUOTAFILENAME;
+/* Let's be paranoid about block size */
+#if 10 > DEV_BSHIFT
+#define dbtokb(db) \
+ ((off_t)(db) >> (10-DEV_BSHIFT))
+#elif 10 < DEV_BSHIFT
+#define dbtokb(db) \
+ ((off_t)(db) << (DEV_BSHIFT-10))
+#else
+#define dbtokb(db) (db)
+#endif
+
const char *qfextension[] = INITQFNAMES;
-const char *quotagroup = QUOTAGROUP;
char tmpfil[] = _PATH_TMP;
+int hflag;
struct quotause {
struct quotause *next;
- long flags;
+ struct quotafile *qf;
struct dqblk dqblk;
+ int flags;
char fsname[MAXPATHLEN + 1];
- char qfname[1]; /* actually longer */
};
#define FOUND 0x01
int alldigits(const char *s);
-int cvtatos(time_t, char *, time_t *);
-char *cvtstoa(time_t);
+int cvtatos(u_int64_t, char *, u_int64_t *);
+char *cvtstoa(u_int64_t);
+u_int64_t cvtblkval(u_int64_t, char, const char *);
+u_int64_t cvtinoval(u_int64_t, char, const char *);
int editit(char *);
+char *fmthumanvalblks(int64_t);
+char *fmthumanvalinos(int64_t);
void freeprivs(struct quotause *);
int getentry(const char *, int);
struct quotause *getprivs(long, int, char *);
-int hasquota(struct fstab *, int, char **);
-void putprivs(long, int, struct quotause *);
+void putprivs(long, struct quotause *);
int readprivs(struct quotause *, char *);
int readtimes(struct quotause *, char *);
static void usage(void);
@@ -102,11 +119,10 @@ main(int argc, char *argv[])
{
struct quotause *qup, *protoprivs, *curprivs;
long id, protoid;
- long long lim;
int i, quotatype, range, tmpfd;
uid_t startuid, enduid;
- u_int32_t *limp;
- char *protoname, *cp, *oldoptarg;
+ u_int64_t lim;
+ char *protoname, *cp, *endpt, *oldoptarg;
int eflag = 0, tflag = 0, pflag = 0, ch;
char *fspath = NULL;
char buf[MAXLOGNAME];
@@ -119,18 +135,26 @@ main(int argc, char *argv[])
protoprivs = NULL;
curprivs = NULL;
protoname = NULL;
- while ((ch = getopt(argc, argv, "ugtf:p:e:")) != -1) {
+ while ((ch = getopt(argc, argv, "ughtf:p:e:")) != -1) {
switch(ch) {
case 'f':
fspath = optarg;
break;
case 'p':
+ if (eflag) {
+ warnx("cannot specify both -e and -p");
+ usage();
+ /* not reached */
+ }
protoname = optarg;
pflag++;
break;
case 'g':
quotatype = GRPQUOTA;
break;
+ case 'h':
+ hflag++;
+ break;
case 'u':
quotatype = USRQUOTA;
break;
@@ -138,51 +162,60 @@ main(int argc, char *argv[])
tflag++;
break;
case 'e':
- if ((qup = malloc(sizeof(*qup))) == NULL)
+ if (pflag) {
+ warnx("cannot specify both -e and -p");
+ usage();
+ /* not reached */
+ }
+ if ((qup = calloc(1, sizeof(*qup))) == NULL)
errx(2, "out of memory");
- bzero(qup, sizeof(*qup));
- i = 0;
oldoptarg = optarg;
- for (cp = optarg; (cp = strsep(&optarg, ":")) != NULL;
- i++) {
+ for (i = 0, cp = optarg;
+ (cp = strsep(&optarg, ":")) != NULL; i++) {
if (cp != oldoptarg)
*(cp - 1) = ':';
- limp = NULL;
+ if (i > 0 && !isdigit(*cp)) {
+ warnx("incorrect quota specification: "
+ "%s", oldoptarg);
+ usage();
+ /* Not Reached */
+ }
switch (i) {
case 0:
strlcpy(qup->fsname, cp,
sizeof(qup->fsname));
break;
case 1:
- limp = &qup->dqblk.dqb_bsoftlimit;
- break;
+ lim = strtoll(cp, &endpt, 10);
+ qup->dqblk.dqb_bsoftlimit =
+ cvtblkval(lim, *endpt,
+ "block soft limit");
+ continue;
case 2:
- limp = &qup->dqblk.dqb_bhardlimit;
- break;
+ lim = strtoll(cp, &endpt, 10);
+ qup->dqblk.dqb_bhardlimit =
+ cvtblkval(lim, *endpt,
+ "block hard limit");
+ continue;
case 3:
- limp = &qup->dqblk.dqb_isoftlimit;
- break;
+ lim = strtoll(cp, &endpt, 10);
+ qup->dqblk.dqb_isoftlimit =
+ cvtinoval(lim, *endpt,
+ "inode soft limit");
+ continue;
case 4:
- limp = &qup->dqblk.dqb_ihardlimit;
- break;
+ lim = strtoll(cp, &endpt, 10);
+ qup->dqblk.dqb_ihardlimit =
+ cvtinoval(lim, *endpt,
+ "inode hard limit");
+ continue;
default:
warnx("incorrect quota specification: "
"%s", oldoptarg);
usage();
- break; /* XXX: report an error */
- }
- if (limp != NULL) {
- lim = strtoll(cp, NULL, 10);
- if (lim < 0 || lim > UINT_MAX)
- errx(1, "invalid limit value: "
- "%lld", lim);
- *limp = (u_int32_t)lim;
+ /* Not Reached */
}
}
- qup->dqblk.dqb_bsoftlimit =
- btodb((off_t)qup->dqblk.dqb_bsoftlimit * 1024);
- qup->dqblk.dqb_bhardlimit =
- btodb((off_t)qup->dqblk.dqb_bhardlimit * 1024);
if (protoprivs == NULL) {
protoprivs = curprivs = qup;
} else {
@@ -190,19 +223,21 @@ main(int argc, char *argv[])
curprivs = qup;
}
eflag++;
- pflag++;
break;
default:
usage();
+ /* Not Reached */
}
}
argc -= optind;
argv += optind;
- if (pflag) {
- if (protoprivs == NULL) {
+ if (pflag || eflag) {
+ if (pflag) {
if ((protoid = getentry(protoname, quotatype)) == -1)
exit(1);
protoprivs = getprivs(protoid, quotatype, fspath);
+ if (protoprivs == NULL)
+ exit(0);
for (qup = protoprivs; qup; qup = qup->next) {
qup->dqblk.dqb_btime = 0;
qup->dqblk.dqb_itime = 0;
@@ -232,33 +267,34 @@ main(int argc, char *argv[])
*argv);
if ((id = getentry(buf, quotatype)) < 0)
continue;
- if (eflag) {
- for (qup = protoprivs; qup;
- qup = qup->next) {
- curprivs = getprivs(id,
- quotatype, qup->fsname);
- if (curprivs == NULL)
- continue;
- strcpy(qup->qfname,
- curprivs->qfname);
- strcpy(qup->fsname,
- curprivs->fsname);
- }
+ if (pflag) {
+ putprivs(id, protoprivs);
+ continue;
+ }
+ for (qup = protoprivs; qup; qup = qup->next) {
+ curprivs = getprivs(id, quotatype,
+ qup->fsname);
+ if (curprivs == NULL)
+ continue;
+ curprivs->dqblk = qup->dqblk;
+ putprivs(id, curprivs);
+ freeprivs(curprivs);
}
- putprivs(id, quotatype, protoprivs);
}
}
+ if (pflag)
+ freeprivs(protoprivs);
exit(0);
}
tmpfd = mkstemp(tmpfil);
fchown(tmpfd, getuid(), getgid());
if (tflag) {
- protoprivs = getprivs(0, quotatype, fspath);
- if (writetimes(protoprivs, tmpfd, quotatype) == 0)
- exit(1);
- if (editit(tmpfil) && readtimes(protoprivs, tmpfil))
- putprivs(0L, quotatype, protoprivs);
- freeprivs(protoprivs);
+ if ((protoprivs = getprivs(0, quotatype, fspath)) != NULL) {
+ if (writetimes(protoprivs, tmpfd, quotatype) != 0 &&
+ editit(tmpfil) && readtimes(protoprivs, tmpfil))
+ putprivs(0L, protoprivs);
+ freeprivs(protoprivs);
+ }
close(tmpfd);
unlink(tmpfil);
exit(0);
@@ -266,11 +302,12 @@ main(int argc, char *argv[])
for ( ; argc > 0; argc--, argv++) {
if ((id = getentry(*argv, quotatype)) == -1)
continue;
- curprivs = getprivs(id, quotatype, fspath);
+ if ((curprivs = getprivs(id, quotatype, fspath)) == NULL)
+ exit(1);
if (writeprivs(curprivs, tmpfd, *argv, quotatype) == 0)
continue;
if (editit(tmpfil) && readprivs(curprivs, tmpfil))
- putprivs(id, quotatype, curprivs);
+ putprivs(id, curprivs);
freeprivs(curprivs);
}
close(tmpfd);
@@ -282,10 +319,10 @@ static void
usage(void)
{
fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
- "usage: edquota [-u] [-f fspath] [-p username] username ...",
+ "usage: edquota [-uh] [-f fspath] [-p username] username ...",
" edquota [-u] -e fspath[:bslim[:bhlim[:islim[:ihlim]]]] [-e ...]",
" username ...",
- " edquota -g [-f fspath] [-p groupname] groupname ...",
+ " edquota -g [-h] [-f fspath] [-p groupname] groupname ...",
" edquota -g -e fspath[:bslim[:bhlim[:islim[:ihlim]]]] [-e ...]",
" groupname ...",
" edquota [-u] -t [-f fspath]",
@@ -311,14 +348,17 @@ getentry(const char *name, int quotatype)
if ((pw = getpwnam(name)))
return (pw->pw_uid);
warnx("%s: no such user", name);
+ sleep(3);
break;
case GRPQUOTA:
if ((gr = getgrnam(name)))
return (gr->gr_gid);
warnx("%s: no such group", name);
+ sleep(3);
break;
default:
warnx("%d: unknown quota type", quotatype);
+ sleep(3);
break;
}
sleep(1);
@@ -331,76 +371,33 @@ getentry(const char *name, int quotatype)
struct quotause *
getprivs(long id, int quotatype, char *fspath)
{
+ struct quotafile *qf;
struct fstab *fs;
struct quotause *qup, *quptail;
struct quotause *quphead;
- int qcmd, qupsize, fd;
- char *qfpathname;
- static int warned = 0;
setfsent();
quphead = quptail = NULL;
- qcmd = QCMD(Q_GETQUOTA, quotatype);
while ((fs = getfsent())) {
if (fspath && *fspath && strcmp(fspath, fs->fs_spec) &&
strcmp(fspath, fs->fs_file))
continue;
if (strcmp(fs->fs_vfstype, "ufs"))
continue;
- if (!hasquota(fs, quotatype, &qfpathname))
+ if ((qf = quota_open(fs, quotatype, O_CREAT|O_RDWR)) == NULL) {
+ if (errno != EOPNOTSUPP)
+ warn("cannot open quotas on %s", fs->fs_file);
continue;
- qupsize = sizeof(*qup) + strlen(qfpathname);
- if ((qup = (struct quotause *)malloc(qupsize)) == NULL)
+ }
+ if ((qup = (struct quotause *)calloc(1, sizeof(*qup))) == NULL)
errx(2, "out of memory");
- if (quotactl(fs->fs_file, qcmd, id, &qup->dqblk) != 0) {
- if (errno == EOPNOTSUPP && !warned) {
- warned++;
- warnx("warning: quotas are not compiled into this kernel");
- sleep(3);
- }
- if ((fd = open(qfpathname, O_RDONLY)) < 0) {
- fd = open(qfpathname, O_RDWR|O_CREAT, 0640);
- if (fd < 0 && errno != ENOENT) {
- warn("%s", qfpathname);
- free(qup);
- continue;
- }
- warnx("creating quota file %s", qfpathname);
- sleep(3);
- (void) fchown(fd, getuid(),
- getentry(quotagroup, GRPQUOTA));
- (void) fchmod(fd, 0640);
- }
- if (lseek(fd, (off_t)id * sizeof(struct dqblk),
- L_SET) < 0) {
- warn("seek error on %s", qfpathname);
- close(fd);
- free(qup);
- continue;
- }
- switch (read(fd, &qup->dqblk, sizeof(struct dqblk))) {
- case 0: /* EOF */
- /*
- * Convert implicit 0 quota (EOF)
- * into an explicit one (zero'ed dqblk)
- */
- bzero((caddr_t)&qup->dqblk,
- sizeof(struct dqblk));
- break;
-
- case sizeof(struct dqblk): /* OK */
- break;
-
- default: /* ERROR */
- warn("read error in %s", qfpathname);
- close(fd);
- free(qup);
- continue;
- }
- close(fd);
+ qup->qf = qf;
+ strncpy(qup->fsname, fs->fs_file, sizeof(qup->fsname));
+ if (quota_read(qf, &qup->dqblk, id) == -1) {
+ warn("cannot read quotas on %s", fs->fs_file);
+ freeprivs(qup);
+ continue;
}
- strcpy(qup->qfname, qfpathname);
- strcpy(qup->fsname, fs->fs_file);
if (quphead == NULL)
quphead = qup;
else
@@ -408,6 +405,9 @@ getprivs(long id, int quotatype, char *fspath)
quptail = qup;
qup->next = 0;
}
+ if (quphead == NULL) {
+ warnx("No quotas on %s", fspath ? fspath : "any filesystems");
+ }
endfsent();
return (quphead);
}
@@ -416,75 +416,13 @@ getprivs(long id, int quotatype, char *fspath)
* Store the requested quota information.
*/
void
-putprivs(long id, int quotatype, struct quotause *quplist)
+putprivs(long id, struct quotause *quplist)
{
struct quotause *qup;
- int qcmd, fd;
- struct dqblk dqbuf;
-
- qcmd = QCMD(Q_SETQUOTA, quotatype);
- for (qup = quplist; qup; qup = qup->next) {
- if (quotactl(qup->fsname, qcmd, id, &qup->dqblk) == 0)
- continue;
- if ((fd = open(qup->qfname, O_RDWR)) < 0) {
- warn("%s", qup->qfname);
- continue;
- }
- if (lseek(fd, (off_t)id * sizeof(struct dqblk), L_SET) < 0) {
- warn("seek error on %s", qup->qfname);
- close(fd);
- continue;
- }
- switch (read(fd, &dqbuf, sizeof(struct dqblk))) {
- case 0: /* EOF */
- /*
- * Convert implicit 0 quota (EOF)
- * into an explicit one (zero'ed dqblk)
- */
- bzero(&dqbuf, sizeof(struct dqblk));
- break;
-
- case sizeof(struct dqblk): /* OK */
- break;
- default: /* ERROR */
- warn("read error in %s", qup->qfname);
- close(fd);
- continue;
- }
- /*
- * Reset time limit if have a soft limit and were
- * previously under it, but are now over it
- * or if there previously was no soft limit, but
- * now have one and are over it.
- */
- if (dqbuf.dqb_bsoftlimit && id != 0 &&
- dqbuf.dqb_curblocks < dqbuf.dqb_bsoftlimit &&
- dqbuf.dqb_curblocks >= qup->dqblk.dqb_bsoftlimit)
- qup->dqblk.dqb_btime = 0;
- if (dqbuf.dqb_bsoftlimit == 0 && id != 0 &&
- dqbuf.dqb_curblocks >= qup->dqblk.dqb_bsoftlimit)
- qup->dqblk.dqb_btime = 0;
- if (dqbuf.dqb_isoftlimit && id != 0 &&
- dqbuf.dqb_curinodes < dqbuf.dqb_isoftlimit &&
- dqbuf.dqb_curinodes >= qup->dqblk.dqb_isoftlimit)
- qup->dqblk.dqb_itime = 0;
- if (dqbuf.dqb_isoftlimit == 0 && id !=0 &&
- dqbuf.dqb_curinodes >= qup->dqblk.dqb_isoftlimit)
- qup->dqblk.dqb_itime = 0;
- qup->dqblk.dqb_curinodes = dqbuf.dqb_curinodes;
- qup->dqblk.dqb_curblocks = dqbuf.dqb_curblocks;
- if (lseek(fd, (off_t)id * sizeof(struct dqblk), L_SET) < 0) {
- warn("seek error on %s", qup->qfname);
- close(fd);
- continue;
- }
- if (write(fd, &qup->dqblk, sizeof (struct dqblk)) !=
- sizeof (struct dqblk)) {
- warn("%s", qup->qfname);
- }
- close(fd);
- }
+ for (qup = quplist; qup; qup = qup->next)
+ if (quota_write_limits(qup->qf, &qup->dqblk, id) == -1)
+ warn("%s", qup->fsname);
}
/*
@@ -544,21 +482,51 @@ writeprivs(struct quotause *quplist, int outfd, char *name, int quotatype)
err(1, "%s", tmpfil);
fprintf(fd, "Quotas for %s %s:\n", qfextension[quotatype], name);
for (qup = quplist; qup; qup = qup->next) {
- fprintf(fd, "%s: %s %lu, limits (soft = %lu, hard = %lu)\n",
- qup->fsname, "kbytes in use:",
- (unsigned long)(dbtob(qup->dqblk.dqb_curblocks) / 1024),
- (unsigned long)(dbtob(qup->dqblk.dqb_bsoftlimit) / 1024),
- (unsigned long)(dbtob(qup->dqblk.dqb_bhardlimit) / 1024));
- fprintf(fd, "%s %lu, limits (soft = %lu, hard = %lu)\n",
- "\tinodes in use:",
- (unsigned long)qup->dqblk.dqb_curinodes,
- (unsigned long)qup->dqblk.dqb_isoftlimit,
- (unsigned long)qup->dqblk.dqb_ihardlimit);
+ fprintf(fd, "%s: in use: %s, ", qup->fsname,
+ fmthumanvalblks(qup->dqblk.dqb_curblocks));
+ fprintf(fd, "limits (soft = %s, ",
+ fmthumanvalblks(qup->dqblk.dqb_bsoftlimit));
+ fprintf(fd, "hard = %s)\n",
+ fmthumanvalblks(qup->dqblk.dqb_bhardlimit));
+ fprintf(fd, "\tinodes in use: %s, ",
+ fmthumanvalinos(qup->dqblk.dqb_curinodes));
+ fprintf(fd, "limits (soft = %s, ",
+ fmthumanvalinos(qup->dqblk.dqb_isoftlimit));
+ fprintf(fd, "hard = %s)\n",
+ fmthumanvalinos(qup->dqblk.dqb_ihardlimit));
}
fclose(fd);
return (1);
}
+char *
+fmthumanvalblks(int64_t blocks)
+{
+ static char numbuf[20];
+
+ if (hflag) {
+ humanize_number(numbuf, blocks < 0 ? 7 : 6,
+ dbtob(blocks), "", HN_AUTOSCALE, HN_NOSPACE);
+ return (numbuf);
+ }
+ snprintf(numbuf, sizeof(numbuf), "%lluk", dbtokb(blocks));
+ return(numbuf);
+}
+
+char *
+fmthumanvalinos(int64_t inos)
+{
+ static char numbuf[20];
+
+ if (hflag) {
+ humanize_number(numbuf, inos < 0 ? 7 : 6,
+ inos, "", HN_AUTOSCALE, HN_NOSPACE | HN_DIVISOR_1000);
+ return (numbuf);
+ }
+ snprintf(numbuf, sizeof(numbuf), "%llu", inos);
+ return(numbuf);
+}
+
/*
* Merge changes to an ASCII file into a quotause list.
*/
@@ -567,8 +535,8 @@ readprivs(struct quotause *quplist, char *inname)
{
struct quotause *qup;
FILE *fd;
- unsigned long bhardlimit, bsoftlimit, curblocks;
- unsigned long ihardlimit, isoftlimit, curinodes;
+ u_int64_t hardlimit, softlimit, curitems;
+ char hardunits, softunits, curitemunits;
int cnt;
char *cp;
struct dqblk dqblk;
@@ -594,29 +562,73 @@ readprivs(struct quotause *quplist, char *inname)
return (0);
}
cnt = sscanf(cp,
- " kbytes in use: %lu, limits (soft = %lu, hard = %lu)",
- &curblocks, &bsoftlimit, &bhardlimit);
- if (cnt != 3) {
+ " in use: %llu%c, limits (soft = %llu%c, hard = %llu%c)",
+ &curitems, &curitemunits, &softlimit, &softunits,
+ &hardlimit, &hardunits);
+ /*
+ * The next three check for old-style input formats.
+ */
+ if (cnt != 6)
+ cnt = sscanf(cp,
+ " in use: %llu%c, limits (soft = %llu%c hard = %llu%c",
+ &curitems, &curitemunits, &softlimit,
+ &softunits, &hardlimit, &hardunits);
+ if (cnt != 6)
+ cnt = sscanf(cp,
+ " in use: %llu%c, limits (soft = %llu%c hard = %llu%c)",
+ &curitems, &curitemunits, &softlimit,
+ &softunits, &hardlimit, &hardunits);
+ if (cnt != 6)
+ cnt = sscanf(cp,
+ " in use: %llu%c, limits (soft = %llu%c, hard = %llu%c",
+ &curitems, &curitemunits, &softlimit,
+ &softunits, &hardlimit, &hardunits);
+ if (cnt != 6) {
warnx("%s:%s: bad format", fsp, cp);
return (0);
}
- dqblk.dqb_curblocks = btodb((off_t)curblocks * 1024);
- dqblk.dqb_bsoftlimit = btodb((off_t)bsoftlimit * 1024);
- dqblk.dqb_bhardlimit = btodb((off_t)bhardlimit * 1024);
+ dqblk.dqb_curblocks = cvtblkval(curitems, curitemunits,
+ "current block count");
+ dqblk.dqb_bsoftlimit = cvtblkval(softlimit, softunits,
+ "block soft limit");
+ dqblk.dqb_bhardlimit = cvtblkval(hardlimit, hardunits,
+ "block hard limit");
if ((cp = strtok(line2, "\n")) == NULL) {
warnx("%s: %s: bad format", fsp, line2);
return (0);
}
- cnt = sscanf(cp,
- "\tinodes in use: %lu, limits (soft = %lu, hard = %lu)",
- &curinodes, &isoftlimit, &ihardlimit);
- if (cnt != 3) {
- warnx("%s: %s: bad format", fsp, line2);
+ cnt = sscanf(&cp[7],
+ " in use: %llu%c limits (soft = %llu%c, hard = %llu%c)",
+ &curitems, &curitemunits, &softlimit,
+ &softunits, &hardlimit, &hardunits);
+ /*
+ * The next three check for old-style input formats.
+ */
+ if (cnt != 6)
+ cnt = sscanf(&cp[7],
+ " in use: %llu%c limits (soft = %llu%c hard = %llu%c",
+ &curitems, &curitemunits, &softlimit,
+ &softunits, &hardlimit, &hardunits);
+ if (cnt != 6)
+ cnt = sscanf(&cp[7],
+ " in use: %llu%c limits (soft = %llu%c hard = %llu%c)",
+ &curitems, &curitemunits, &softlimit,
+ &softunits, &hardlimit, &hardunits);
+ if (cnt != 6)
+ cnt = sscanf(&cp[7],
+ " in use: %llu%c limits (soft = %llu%c, hard = %llu%c",
+ &curitems, &curitemunits, &softlimit,
+ &softunits, &hardlimit, &hardunits);
+ if (cnt != 6) {
+ warnx("%s: %s: bad format cnt %d", fsp, &cp[7], cnt);
return (0);
}
- dqblk.dqb_curinodes = curinodes;
- dqblk.dqb_isoftlimit = isoftlimit;
- dqblk.dqb_ihardlimit = ihardlimit;
+ dqblk.dqb_curinodes = cvtinoval(curitems, curitemunits,
+ "current inode count");
+ dqblk.dqb_isoftlimit = cvtinoval(softlimit, softunits,
+ "inode soft limit");
+ dqblk.dqb_ihardlimit = cvtinoval(hardlimit, hardunits,
+ "inode hard limit");
for (qup = quplist; qup; qup = qup->next) {
if (strcmp(fsp, qup->fsname))
continue;
@@ -643,8 +655,10 @@ readprivs(struct quotause *quplist, char *inname)
qup->dqblk.dqb_isoftlimit = dqblk.dqb_isoftlimit;
qup->dqblk.dqb_ihardlimit = dqblk.dqb_ihardlimit;
qup->flags |= FOUND;
- if (dqblk.dqb_curblocks == qup->dqblk.dqb_curblocks &&
- dqblk.dqb_curinodes == qup->dqblk.dqb_curinodes)
+ /* Humanized input returns only approximate counts */
+ if (hflag ||
+ (dqblk.dqb_curblocks == qup->dqblk.dqb_curblocks &&
+ dqblk.dqb_curinodes == qup->dqblk.dqb_curinodes))
break;
warnx("%s: cannot change current allocation", fsp);
break;
@@ -703,8 +717,7 @@ readtimes(struct quotause *quplist, char *inname)
FILE *fd;
int cnt;
char *cp;
- time_t itime, btime, iseconds, bseconds;
- long l_itime, l_btime;
+ u_int64_t itime, btime, iseconds, bseconds;
char *fsp, bunits[10], iunits[10], line1[BUFSIZ];
fd = fopen(inname, "r");
@@ -727,14 +740,12 @@ readtimes(struct quotause *quplist, char *inname)
return (0);
}
cnt = sscanf(cp,
- " block grace period: %ld %s file grace period: %ld %s",
- &l_btime, bunits, &l_itime, iunits);
+ " block grace period: %llu %s file grace period: %llu %s",
+ &btime, bunits, &itime, iunits);
if (cnt != 4) {
warnx("%s:%s: bad format", fsp, cp);
return (0);
}
- btime = l_btime;
- itime = l_itime;
if (cvtatos(btime, bunits, &bseconds) == 0)
return (0);
if (cvtatos(itime, iunits, &iseconds) == 0)
@@ -768,21 +779,21 @@ readtimes(struct quotause *quplist, char *inname)
* Convert seconds to ASCII times.
*/
char *
-cvtstoa(time_t secs)
+cvtstoa(u_int64_t secs)
{
static char buf[20];
if (secs % (24 * 60 * 60) == 0) {
secs /= 24 * 60 * 60;
- sprintf(buf, "%ld day%s", (long)secs, secs == 1 ? "" : "s");
+ sprintf(buf, "%llu day%s", secs, secs == 1 ? "" : "s");
} else if (secs % (60 * 60) == 0) {
secs /= 60 * 60;
- sprintf(buf, "%ld hour%s", (long)secs, secs == 1 ? "" : "s");
+ sprintf(buf, "%llu hour%s", secs, secs == 1 ? "" : "s");
} else if (secs % 60 == 0) {
secs /= 60;
- sprintf(buf, "%ld minute%s", (long)secs, secs == 1 ? "" : "s");
+ sprintf(buf, "%llu minute%s", secs, secs == 1 ? "" : "s");
} else
- sprintf(buf, "%ld second%s", (long)secs, secs == 1 ? "" : "s");
+ sprintf(buf, "%llu second%s", secs, secs == 1 ? "" : "s");
return (buf);
}
@@ -790,7 +801,7 @@ cvtstoa(time_t secs)
* Convert ASCII input times to seconds.
*/
int
-cvtatos(time_t period, char *units, time_t *seconds)
+cvtatos(u_int64_t period, char *units, u_int64_t *seconds)
{
if (bcmp(units, "second", 6) == 0)
@@ -802,7 +813,7 @@ cvtatos(time_t period, char *units, time_t *seconds)
else if (bcmp(units, "day", 3) == 0)
*seconds = period * 24 * 60 * 60;
else {
- printf("%s: bad units, specify %s\n", units,
+ warnx("%s: bad units, specify %s\n", units,
"days, hours, minutes, or seconds");
return (0);
}
@@ -810,6 +821,107 @@ cvtatos(time_t period, char *units, time_t *seconds)
}
/*
+ * Convert a limit to number of disk blocks.
+ */
+u_int64_t
+cvtblkval(u_int64_t limit, char units, const char *itemname)
+{
+
+ switch(units) {
+ case 'B':
+ case 'b':
+ limit = btodb(limit);
+ break;
+ case '\0': /* historic behavior */
+ case ',': /* historic behavior */
+ case ')': /* historic behavior */
+ case 'K':
+ case 'k':
+ limit *= btodb(1024);
+ break;
+ case 'M':
+ case 'm':
+ limit *= btodb(1048576);
+ break;
+ case 'G':
+ case 'g':
+ limit *= btodb(1073741824);
+ break;
+ case 'T':
+ case 't':
+ limit *= btodb(1099511627776);
+ break;
+ case 'P':
+ case 'p':
+ limit *= btodb(1125899906842624);
+ break;
+ case 'E':
+ case 'e':
+ limit *= btodb(1152921504606846976);
+ break;
+ case ' ':
+ errx(2, "No space permitted between value and units for %s\n",
+ itemname);
+ break;
+ default:
+ errx(2, "%llu%c: unknown units for %s, specify none, K, M, G, T, P, or E\n",
+ limit, units, itemname);
+ break;
+ }
+ return (limit);
+}
+
+/*
+ * Convert a limit to number of inodes.
+ */
+u_int64_t
+cvtinoval(u_int64_t limit, char units, const char *itemname)
+{
+
+ switch(units) {
+ case 'B':
+ case 'b':
+ case '\0': /* historic behavior */
+ case ',': /* historic behavior */
+ case ')': /* historic behavior */
+ break;
+ case 'K':
+ case 'k':
+ limit *= 1000;
+ break;
+ case 'M':
+ case 'm':
+ limit *= 1000000;
+ break;
+ case 'G':
+ case 'g':
+ limit *= 1000000000;
+ break;
+ case 'T':
+ case 't':
+ limit *= 1000000000000;
+ break;
+ case 'P':
+ case 'p':
+ limit *= 1000000000000000;
+ break;
+ case 'E':
+ case 'e':
+ limit *= 1000000000000000000;
+ break;
+ case ' ':
+ errx(2, "No space permitted between value and units for %s\n",
+ itemname);
+ break;
+ default:
+ errx(2, "%llu%c: unknown units for %s, specify none, K, M, G, T, P, or E\n",
+ limit, units, itemname);
+ break;
+ }
+ return (limit);
+}
+
+/*
* Free a list of quotause structures.
*/
void
@@ -818,6 +930,7 @@ freeprivs(struct quotause *quplist)
struct quotause *qup, *nextqup;
for (qup = quplist; qup; qup = nextqup) {
+ quota_close(qup->qf);
nextqup = qup->next;
free(qup);
}
@@ -838,53 +951,3 @@ alldigits(const char *s)
} while ((c = *s++));
return (1);
}
-
-/*
- * Check to see if a particular quota is to be enabled.
- */
-int
-hasquota(struct fstab *fs, int type, char **qfnamep)
-{
- char *opt;
- char *cp;
- struct statfs sfb;
- static char initname, usrname[100], grpname[100];
- static char buf[BUFSIZ];
-
- if (!initname) {
- (void)snprintf(usrname, sizeof(usrname), "%s%s",
- qfextension[USRQUOTA], qfname);
- (void)snprintf(grpname, sizeof(grpname), "%s%s",
- qfextension[GRPQUOTA], qfname);
- initname = 1;
- }
- strcpy(buf, fs->fs_mntops);
- for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
- if ((cp = index(opt, '=')))
- *cp++ = '\0';
- if (type == USRQUOTA && strcmp(opt, usrname) == 0)
- break;
- if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
- break;
- }
- if (!opt)
- return (0);
- if (cp)
- *qfnamep = cp;
- else {
- (void)snprintf(buf, sizeof(buf), "%s/%s.%s", fs->fs_file,
- qfname, qfextension[type]);
- *qfnamep = buf;
- }
- if (statfs(fs->fs_file, &sfb) != 0) {
- warn("cannot statfs mount point %s", fs->fs_file);
- return (0);
- }
- if (strcmp(fs->fs_file, sfb.f_mntonname)) {
- warnx("%s not mounted for %s quotas", fs->fs_file,
- type == USRQUOTA ? "user" : "group");
- sleep(3);
- return (0);
- }
- return (1);
-}
OpenPOWER on IntegriCloud