diff options
27 files changed, 2135 insertions, 1059 deletions
diff --git a/lib/libc/nls/ru_RU.KOI8-R.msg b/lib/libc/nls/ru_RU.KOI8-R.msg index 431bc27ca255..55233b946abf 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/libc/sys/quotactl.2 b/lib/libc/sys/quotactl.2 index 4b4dca5fbcbd..ff3cb4b1756b 100644 --- a/lib/libc/sys/quotactl.2 +++ b/lib/libc/sys/quotactl.2 @@ -84,7 +84,7 @@ and group identifiers (GRPQUOTA). The .Dq ufs specific commands are: -.Bl -tag -width Q_QUOTAOFFxx +.Bl -tag -width Q_GETQUOTASIZEx .It Dv Q_QUOTAON Enable disk quotas for the file system specified by .Fa path . @@ -110,6 +110,17 @@ and .Fa id arguments are unused. Only the super-user may turn quotas off. +.It Dv Q_GETQUOTASIZE +Get the wordsize used to represent the quotas for the user or group +(as determined by the command type). +Possible values are 32 for the old-style quota file +and 64 for the new-style quota file. +The +.Fa addr +argument is a pointer to an integer into which the size is stored. +The identifier +.Fa id +is not used. .It Dv Q_GETQUOTA Get disk quota limits and current usage for the user or group (as determined by the command type) with identifier @@ -177,9 +188,11 @@ The argument or the command type is invalid. In -.Dv Q_GETQUOTA -and +.Dv Q_GETQUOTASIZE , +.Dv Q_GETQUOTA , .Dv Q_SETQUOTA , +and +.Dv Q_SETUSE , quotas are not currently enabled for this file system. .Pp The @@ -208,7 +221,8 @@ Too many symbolic links were encountered in translating a pathname. .It Bq Er EROFS In .Dv Q_QUOTAON , -the quota file resides on a read-only file system. +either the file system on which quotas are to be enabled is mounted read-only +or the quota file resides on a read-only file system. .It Bq Er EIO An .Tn I/O diff --git a/lib/libutil/Makefile b/lib/libutil/Makefile index d82d93434a1f..9dc35c0f9efd 100644 --- a/lib/libutil/Makefile +++ b/lib/libutil/Makefile @@ -12,7 +12,7 @@ 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_auth.c login_cap.c \ login_class.c login_crypt.c login_ok.c login_times.c login_tty.c \ - pidfile.c property.c pty.c pw_util.c realhostname.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 @@ -29,7 +29,7 @@ MAN+= kld.3 login_auth.3 login_tty.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 @@ -57,5 +57,13 @@ 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_fsname.3 \ + quotafile.3 quota_qfname.3 \ + quotafile.3 quota_statfs.3 \ + quotafile.3 quota_read.3 \ + quotafile.3 quota_write_limits.3 \ + quotafile.3 quota_write_usage.3 \ + quotafile.3 quota_close.3 .include <bsd.lib.mk> diff --git a/lib/libutil/libutil.h b/lib/libutil/libutil.h index 4c2ee3bc0769..5b7ffad0a447 100644 --- a/lib/libutil/libutil.h +++ b/lib/libutil/libutil.h @@ -164,6 +164,23 @@ 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_on(struct quotafile *); +int quota_off(struct quotafile *); +const char *quota_fsname(const struct quotafile *); +const char *quota_qfname(const struct quotafile *); +int quota_maxid(struct quotafile *); +int quota_check_path(const struct quotafile *, const char *path); +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); +int quota_convert(struct quotafile *, 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 000000000000..5702cec90762 --- /dev/null +++ b/lib/libutil/quotafile.3 @@ -0,0 +1,290 @@ +.\"- +.\" Copyright (c) 2009 Dag-Erling Coц╞dan Smц╦rgrav and +.\" 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. +.\" 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 December 28, 2009 +.Dt QUOTAFILE 3 +.Os +.Sh NAME +.Nm quota_open +.Nm quota_close +.Nm quota_on +.Nm quota_off +.Nm quota_read +.Nm quota_write_limits +.Nm quota_write_usage +.Nm quota_fsname +.Nm quota_qfname +.Nm quota_maxid +.Nm quota_check_path +.Nm quota_convert +.Nd "Manipulate quotas" +.Sh LIBRARY +.Lb libutil +.Sh SYNOPSIS +.In sys/param.h +.In sys/mount.h +.In ufs/ufs/quota.h +.In fcntl.h +.In fstab.h +.In libutil.h +.Ft "struct quotafile *" +.Fn quota_open "struct fstab *fs" "int quotatype" "int openflags" +.Ft int +.Fn quota_close "struct quotafile *qf" +.Ft int +.Fn quota_on "const struct quotafile *qf" +.Ft int +.Fn quota_off "const struct quotafile *qf" +.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 "const char *" +.Fn quota_fsname "const struct quotafile *qf" +.Ft "const char *" +.Fn quota_qfname "const struct quotafile *qf" +.Ft int +.Fn quota_maxid "const struct quotafile *qf" +.Ft int +.Fn quota_check_path "const struct quotafile *qf" "const char *path" +.Ft int +.Fn quota_convert "struct quotafile *qf" "int wordsize" +.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_close +function closes any open file descriptors and frees any storage +associated with the filesystem and quota type referenced by +.Va qf . +.Pp +The +.Fn quota_on +function enables quotas for the filesystem associated with its +.Va qf +argument which may have been opened +.Dv O_RDONLY +or +.Dv O_RDWR . +The +.Fn quota_on +function returns 0 if successful; +otherwise the value\~-1 is returned and the global variable +.Va errno +is set to indicate the error, see +.Xr quotactl 2 +for the possible errors. +.Pp +The +.Fn quota_off +function disables quotas for the filesystem associated with its +.Va qf +argument which may have been opened +.Dv O_RDONLY +or +.Dv O_RDWR . +The +.Fn quota_off +function returns 0 if successful; +otherwise the value\~-1 is returned and the global variable +.Va errno +is set to indicate the error, see +.Xr quotactl 2 +for the possible errors. +.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_fsname +function returns a pointer to a buffer containing the path to the root +of the file system that corresponds to its +.Va qf +argument, as listed in +.Pa /etc/fstab . +Note that this may be a symbolic link to the actual directory. +.Pp +The +.Fn quota_qfname +function returns a pointer to a buffer containing the name of the +quota file that corresponds to its +.Va qf +argument. +Note that this may be a symbolic link to the actual file. +.Pp +The +.Fn quota_maxid +function returns the maximum user (or group) +.Va id +contained in the quota file associated with its +.Va qf +argument. +.Pp +The +.Fn quota_check_path +function checks if the specified path is within the filesystem that +corresponds to its +.Va qf +argument. +If the +.Va path +argument refers to a symbolic link, +.Fn quota_check_path +will follow it. +.Pp +The +.Fn quota_convert +function converts the quota file associated with its +.Va qf +argument to the data size specified by its +.Va wordsize +argument. +The supported wordsize arguments are 32 for the old 32-bit +quota file format and 64 for the new 64-bit quota file format. +The +.Fn quota_convert +function may only be called to operate on quota files that +are not currently active. +.Sh IMPLEMENTATION NOTES +If the underlying quota file is in or converted to the old 32-bit format, +limit and usage values written to the quota file will be clipped to 32 bits. +.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 error. +.Pp +The +.Fn quota_check_path +function returns\~1 for a positive result and\~0 for a negative +result. +If an error occurs, it returns\~-1 and sets +.Va errno +to indicate the error. +.Pp +The +.Fn quota_read , +.Fn quota_write_limits , +.Fn quota_write_usage , +.Fn quota_convert , +and +.Fn quota_close +functions return zero on success. +On error they return\~-1 +and set +.Va errno +to indicate the error. +.Sh SEE ALSO +.Xr quotactl 2 , +.Xr quota.user 5 , +.Xr quota.group 5 +.Sh HISTORY +The +.Nm quotafile +functions first appeared in +.Fx 8.1 . +.Sh AUTHORS +.An -nosplit +The +.Nm quotafile +functions and this manual page were written by +.An Dag-Erling Sm\(/orgrav Aq des@FreeBSD.org +and +.An Marshall Kirk McKusick Aq mckusick@mckusick.com . diff --git a/lib/libutil/quotafile.c b/lib/libutil/quotafile.c new file mode 100644 index 000000000000..0fda5f626870 --- /dev/null +++ b/lib/libutil/quotafile.c @@ -0,0 +1,593 @@ +/*- + * 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 accmode; /* access mode */ + int wordsize; /* 32-bit or 64-bit limits */ + int quotatype; /* USRQUOTA or GRPQUOTA */ + dev_t dev; /* device */ + 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]; + + /* + * 1) we only need one of these + * 2) fstab may specify a different filename + */ + 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; + struct stat st; + int qcmd, serrno; + + if (strcmp(fs->fs_vfstype, "ufs")) + return (NULL); + if ((qf = calloc(1, sizeof(*qf))) == NULL) + return (NULL); + qf->fd = -1; + qf->quotatype = quotatype; + strncpy(qf->fsname, fs->fs_file, sizeof(qf->fsname)); + if (stat(qf->fsname, &st) != 0) + goto error; + qf->dev = st.st_dev; + serrno = hasquota(fs, quotatype, qf->qfname, sizeof(qf->qfname)); + qcmd = QCMD(Q_GETQUOTASIZE, quotatype); + if (quotactl(qf->fsname, qcmd, 0, &qf->wordsize) == 0) + return (qf); + if (serrno == 0) { + errno = EOPNOTSUPP; + goto error; + } + qf->accmode = openflags & O_ACCMODE; + if ((qf->fd = open(qf->qfname, qf->accmode)) < 0 && + (openflags & O_CREAT) != O_CREAT) + goto error; + /* File open worked, so process it */ + if (qf->fd != -1) { + qf->wordsize = 32; + switch (read(qf->fd, &dqh, sizeof(dqh))) { + case -1: + goto error; + 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 */ + errno = EINVAL; + goto error; + } + qf->wordsize = 64; + return (qf); + default: + qf->wordsize = 32; + return (qf); + } + /* not reached */ + } + /* open failed, but O_CREAT was specified, so create a new file */ + if ((qf->fd = open(qf->qfname, O_RDWR|O_CREAT|O_TRUNC, 0)) < 0) + goto error; + 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)) { + /* it was one we created ourselves */ + unlink(qf->qfname); + goto error; + } + grp = getgrnam(QUOTAGROUP); + fchown(qf->fd, 0, grp ? grp->gr_gid : 0); + fchmod(qf->fd, 0640); + return (qf); +error: + serrno = errno; + /* did we have an open file? */ + if (qf->fd != -1) + close(qf->fd); + free(qf); + errno = serrno; + return (NULL); +} + +void +quota_close(struct quotafile *qf) +{ + + if (qf->fd != -1) + close(qf->fd); + free(qf); +} + +int +quota_on(struct quotafile *qf) +{ + int qcmd; + + qcmd = QCMD(Q_QUOTAON, qf->quotatype); + return (quotactl(qf->fsname, qcmd, 0, qf->qfname)); +} + +int +quota_off(struct quotafile *qf) +{ + + return (quotactl(qf->fsname, QCMD(Q_QUOTAOFF, qf->quotatype), 0, 0)); +} + +const char * +quota_fsname(const struct quotafile *qf) +{ + + return (qf->fsname); +} + +const char * +quota_qfname(const struct quotafile *qf) +{ + + return (qf->qfname); +} + +int +quota_check_path(const struct quotafile *qf, const char *path) +{ + struct stat st; + + if (stat(path, &st) == -1) + return (-1); + return (st.st_dev == qf->dev); +} + +int +quota_maxid(struct quotafile *qf) +{ + struct stat st; + int maxid; + + if (stat(qf->qfname, &st) < 0) + return (0); + switch (qf->wordsize) { + case 32: + maxid = st.st_size / sizeof(struct dqblk32) - 1; + break; + case 64: + maxid = st.st_size / sizeof(struct dqblk64) - 2; + break; + default: + maxid = 0; + break; + } + return (maxid > 0 ? maxid : 0); +} + +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 ((qf->accmode & O_RDWR) != O_RDWR) { + errno = EBADF; + return (-1); + } + 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 ((qf->accmode & O_RDWR) != O_RDWR) { + errno = EBADF; + return (-1); + } + 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 */ +} + +/* + * Convert a quota file from one format to another. + */ +int +quota_convert(struct quotafile *qf, int wordsize) +{ + struct quotafile *newqf; + struct dqhdr64 dqh; + struct dqblk dqblk; + struct group *grp; + int serrno, maxid, id, fd; + + /* + * Quotas must not be active and quotafile must be open + * for reading and writing. + */ + if ((qf->accmode & O_RDWR) != O_RDWR || qf->fd == -1) { + errno = EBADF; + return (-1); + } + if ((wordsize != 32 && wordsize != 64) || + wordsize == qf->wordsize) { + errno = EINVAL; + return (-1); + } + maxid = quota_maxid(qf); + if ((newqf = calloc(1, sizeof(*qf))) == NULL) { + errno = ENOMEM; + return (-1); + } + *newqf = *qf; + snprintf(newqf->qfname, MAXPATHLEN + 1, "%s_%d.orig", qf->qfname, + qf->wordsize); + if (rename(qf->qfname, newqf->qfname) < 0) { + free(newqf); + return (-1); + } + if ((newqf->fd = open(qf->qfname, O_RDWR|O_CREAT|O_TRUNC, 0)) < 0) { + serrno = errno; + goto error; + } + newqf->wordsize = wordsize; + if (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(newqf->fd, &dqh, sizeof(dqh)) != sizeof(dqh)) { + serrno = errno; + goto error; + } + } + grp = getgrnam(QUOTAGROUP); + fchown(newqf->fd, 0, grp ? grp->gr_gid : 0); + fchmod(newqf->fd, 0640); + for (id = 0; id <= maxid; id++) { + if ((quota_read(qf, &dqblk, id)) < 0) + break; + switch (newqf->wordsize) { + case 32: + if ((quota_write32(newqf, &dqblk, id)) < 0) + break; + continue; + case 64: + if ((quota_write64(newqf, &dqblk, id)) < 0) + break; + continue; + default: + errno = EINVAL; + break; + } + } + if (id < maxid) { + serrno = errno; + goto error; + } + /* + * Update the passed in quotafile to reference the new file + * of the converted format size. + */ + fd = qf->fd; + qf->fd = newqf->fd; + newqf->fd = fd; + qf->wordsize = newqf->wordsize; + quota_close(newqf); + return (0); +error: + /* put back the original file */ + (void) rename(newqf->qfname, qf->qfname); + quota_close(newqf); + errno = serrno; + return (-1); +} diff --git a/libexec/rpc.rquotad/Makefile b/libexec/rpc.rquotad/Makefile index 331f6ab6e2b6..95d4415e538b 100644 --- a/libexec/rpc.rquotad/Makefile +++ b/libexec/rpc.rquotad/Makefile @@ -4,7 +4,7 @@ PROG = rpc.rquotad SRCS = rquotad.c MAN = rpc.rquotad.8 -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 8b623da675a7..d9ce06aac82e 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> @@ -35,20 +36,9 @@ static void rquota_service(struct svc_req *request, SVCXPRT *transp); static void sendquota(struct svc_req *request, SVCXPRT *transp); static void initfs(void); static int getfsquota(long id, char *path, struct dqblk *dqblk); -static int hasquota(struct fstab *fs, char **qfnamep); - -/* - * structure containing informations about ufs filesystems - * initialised by initfs() - */ -struct fs_stat { - struct fs_stat *fs_next; /* next element */ - char *fs_file; /* mount point of the filesystem */ - char *qfpathname; /* pathname of the quota file */ - dev_t st_dev; /* device of the filesystem */ -} fs_stat; -static struct fs_stat *fs_begin = NULL; +static struct quotafile **qfa; /* array of qfs */ +static int nqf, szqf; /* number of qfs and size of array */ static int from_inetd = 1; static void @@ -74,9 +64,7 @@ main(void) if (!from_inetd) { daemon(0, 0); - (void)rpcb_unset(RQUOTAPROG, RQUOTAVERS, NULL); - (void)signal(SIGINT, cleanup); (void)signal(SIGTERM, cleanup); (void)signal(SIGHUP, cleanup); @@ -118,12 +106,10 @@ rquota_service(struct svc_req *request, SVCXPRT *transp) case NULLPROC: (void)svc_sendreply(transp, (xdrproc_t)xdr_void, (char *)NULL); break; - case RQUOTAPROC_GETQUOTA: case RQUOTAPROC_GETACTIVEQUOTA: sendquota(request, transp); break; - default: svcerr_noproc(transp); break; @@ -140,6 +126,7 @@ sendquota(struct svc_req *request, SVCXPRT *transp) struct getquota_rslt getq_rslt; struct dqblk dqblk; struct timeval timev; + int scale; bzero(&getq_args, sizeof(getq_args)); if (!svc_getargs(transp, (xdrproc_t)xdr_getquota_args, &getq_args)) { @@ -156,13 +143,15 @@ sendquota(struct svc_req *request, SVCXPRT *transp) gettimeofday(&timev, NULL); getq_rslt.status = Q_OK; getq_rslt.getquota_rslt_u.gqr_rquota.rq_active = TRUE; - getq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize = DEV_BSIZE; + scale = 1 << flsll(dqblk.dqb_bhardlimit >> 32); + getq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize = + DEV_BSIZE * scale; getq_rslt.getquota_rslt_u.gqr_rquota.rq_bhardlimit = - dqblk.dqb_bhardlimit; + dqblk.dqb_bhardlimit / scale; getq_rslt.getquota_rslt_u.gqr_rquota.rq_bsoftlimit = - dqblk.dqb_bsoftlimit; + dqblk.dqb_bsoftlimit / scale; getq_rslt.getquota_rslt_u.gqr_rquota.rq_curblocks = - dqblk.dqb_curblocks; + dqblk.dqb_curblocks / scale; getq_rslt.getquota_rslt_u.gqr_rquota.rq_fhardlimit = dqblk.dqb_ihardlimit; getq_rslt.getquota_rslt_u.gqr_rquota.rq_fsoftlimit = @@ -182,39 +171,39 @@ sendquota(struct svc_req *request, SVCXPRT *transp) } } -/* initialise the fs_tab list from entries in /etc/fstab */ static void initfs(void) { - struct fs_stat *fs_current = NULL; - struct fs_stat *fs_next = NULL; - char *qfpathname; struct fstab *fs; - struct stat st; setfsent(); + szqf = 8; + if ((qfa = malloc(szqf * sizeof *qfa)) == NULL) + goto enomem; while ((fs = getfsent())) { if (strcmp(fs->fs_vfstype, "ufs")) continue; - if (!hasquota(fs, &qfpathname)) + if (nqf >= szqf) { + szqf *= 2; + if ((qfa = reallocf(qfa, szqf * sizeof *qfa)) == NULL) + goto enomem; + } + if ((qfa[nqf] = quota_open(fs, USRQUOTA, O_RDONLY)) == NULL) { + if (errno != EOPNOTSUPP) + goto fserr; continue; - - fs_current = malloc(sizeof(struct fs_stat)); - fs_current->fs_next = fs_next; /* next element */ - - fs_current->fs_file = malloc(strlen(fs->fs_file) + 1); - strcpy(fs_current->fs_file, fs->fs_file); - - fs_current->qfpathname = malloc(strlen(qfpathname) + 1); - strcpy(fs_current->qfpathname, qfpathname); - - stat(fs_current->fs_file, &st); - fs_current->st_dev = st.st_dev; - - fs_next = fs_current; + } + ++nqf; + /* XXX */ } endfsent(); - fs_begin = fs_current; + return; +enomem: + syslog(LOG_ERR, "out of memory"); + exit(1); +fserr: + syslog(LOG_ERR, "%s: %s", fs->fs_file, strerror(errno)); + exit(1); } /* @@ -224,85 +213,10 @@ initfs(void) static int getfsquota(long id, char *path, struct dqblk *dqblk) { - struct stat st_path; - struct fs_stat *fs; - int qcmd, fd, ret = 0; - - if (stat(path, &st_path) < 0) - return (0); - - qcmd = QCMD(Q_GETQUOTA, USRQUOTA); + int i; - for (fs = fs_begin; fs != NULL; fs = fs->fs_next) { - /* where the device is the same as path */ - if (fs->st_dev != st_path.st_dev) - continue; - - /* find the specified filesystem. get and return quota */ - if (quotactl(fs->fs_file, qcmd, id, dqblk) == 0) - return (1); - - if ((fd = open(fs->qfpathname, O_RDONLY)) < 0) { - 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 */ - syslog(LOG_ERR, "read error: %s: %m", fs->qfpathname); - close(fd); - return (0); - } - close(fd); - } - return (ret); -} - -/* - * Check to see if a particular quota is to be enabled. - * Comes from quota.c, NetBSD 0.9 - */ -static int -hasquota(struct fstab *fs, char **qfnamep) -{ - static char initname, usrname[100]; - static char buf[BUFSIZ]; - char *opt, *cp; - const char *qfextension[] = INITQFNAMES; - - if (!initname) { - sprintf(usrname, "%s%s", qfextension[USRQUOTA], QUOTAFILENAME); - initname = 1; - } - strcpy(buf, fs->fs_mntops); - for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) { - if ((cp = index(opt, '='))) - *cp++ = '\0'; - if (strcmp(opt, usrname) == 0) - break; - } - if (!opt) - return (0); - if (cp) { - *qfnamep = cp; - return (1); - } - sprintf(buf, "%s/%s.%s", fs->fs_file, QUOTAFILENAME, - qfextension[USRQUOTA]); - *qfnamep = buf; - return (1); + for (i = 0; i < nqf; ++i) + if (quota_check_path(qfa[i], path) == 1) + return (quota_read(qfa[i], dqblk, id) == 0); + return (0); } diff --git a/sbin/quotacheck/Makefile b/sbin/quotacheck/Makefile index 5184bf261f1e..51a88b7681c7 100644 --- a/sbin/quotacheck/Makefile +++ b/sbin/quotacheck/Makefile @@ -5,6 +5,8 @@ PROG= quotacheck SRCS= quotacheck.c preen.c fsutil.c utilities.c WARNS?= 2 MAN= quotacheck.8 +DPADD= ${LIBUTIL} +LDADD= -lutil .PATH: ${.CURDIR}/../fsck ${.CURDIR}/../fsck_ffs diff --git a/sbin/quotacheck/preen.c b/sbin/quotacheck/preen.c index a6c4169ee55e..1c285ccae9ce 100644 --- a/sbin/quotacheck/preen.c +++ b/sbin/quotacheck/preen.c @@ -45,9 +45,13 @@ __RCSID("$NetBSD: preen.c,v 1.18 1998/07/26 20:02:36 mycroft Exp $"); #include <sys/wait.h> #include <sys/queue.h> +#include <ufs/ufs/quota.h> + #include <err.h> #include <ctype.h> +#include <fcntl.h> #include <fstab.h> +#include <libutil.h> #include <string.h> #include <stdio.h> #include <stdlib.h> @@ -58,9 +62,9 @@ __RCSID("$NetBSD: preen.c,v 1.18 1998/07/26 20:02:36 mycroft Exp $"); struct partentry { TAILQ_ENTRY(partentry) p_entries; char *p_devname; /* device name */ - char *p_mntpt; /* mount point */ - char *p_type; /* file system type */ - struct quotaname *p_quota; /* quota file info ptr */ + const char *p_mntpt; /* mount point */ + struct quotafile *p_qfu; /* user quota file info ptr */ + struct quotafile *p_qfg; /* group quota file info */ }; TAILQ_HEAD(part, partentry) badh; @@ -75,21 +79,19 @@ struct diskentry { TAILQ_HEAD(disk, diskentry) diskh; static struct diskentry *finddisk(const char *); -static void addpart(const char *, const char *, const char *, - struct quotaname *); +static void addpart(struct fstab *, struct quotafile *, struct quotafile *); static int startdisk(struct diskentry *); extern void *emalloc(size_t); extern char *estrdup(const char *); int -checkfstab(void) +checkfstab(int uflag, int gflag) { struct fstab *fs; struct diskentry *d, *nextdisk; struct partentry *p; int ret, pid, retcode, passno, sumstatus, status, nextpass; - char *name; - struct quotaname *qnp; + struct quotafile *qfu, *qfg; TAILQ_INIT(&badh); TAILQ_INIT(&diskh); @@ -104,30 +106,32 @@ checkfstab(void) return (8); } while ((fs = getfsent()) != 0) { - name = fs->fs_spec; if (fs->fs_passno > passno && fs->fs_passno < nextpass) nextpass = fs->fs_passno; if (passno != fs->fs_passno) continue; - if ((qnp = needchk(fs)) == NULL) + qfu = NULL; + if (uflag) + qfu = quota_open(fs, USRQUOTA, O_CREAT|O_RDWR); + qfg = NULL; + if (gflag) + qfg = quota_open(fs, GRPQUOTA, O_CREAT|O_RDWR); + if (qfu == NULL && qfg == NULL) continue; if (passno == 1) { - sumstatus = chkquota(name, fs->fs_file, qnp); - + sumstatus = chkquota(fs->fs_spec, qfu, qfg); + if (qfu) + quota_close(qfu); + if (qfg) + quota_close(qfg); if (sumstatus) return (sumstatus); continue; } - if (name == NULL) { - (void) fprintf(stderr, - "BAD DISK NAME %s\n", fs->fs_spec); - sumstatus |= 8; - continue; - } - addpart(fs->fs_vfstype, name, fs->fs_file, qnp); + addpart(fs, qfu, qfg); } if (passno == 1) @@ -157,8 +161,8 @@ checkfstab(void) if (WIFSIGNALED(status)) { (void) fprintf(stderr, - "%s: %s (%s): EXITED WITH SIGNAL %d\n", - p->p_type, p->p_devname, p->p_mntpt, + "%s: (%s): EXITED WITH SIGNAL %d\n", + p->p_devname, p->p_mntpt, WTERMSIG(status)); retcode = 8; } @@ -169,8 +173,11 @@ checkfstab(void) TAILQ_INSERT_TAIL(&badh, p, p_entries); sumstatus |= retcode; } else { - free(p->p_type); free(p->p_devname); + if (p->p_qfu) + quota_close(p->p_qfu); + if (p->p_qfg) + quota_close(p->p_qfg); free(p); } d->d_pid = 0; @@ -196,8 +203,8 @@ checkfstab(void) for (; p; p = TAILQ_NEXT(p, p_entries)) (void) fprintf(stderr, - "%s: %s (%s)%s", p->p_type, p->p_devname, - p->p_mntpt, TAILQ_NEXT(p, p_entries) ? ", " : "\n"); + "%s: (%s)%s", p->p_devname, p->p_mntpt, + TAILQ_NEXT(p, p_entries) ? ", " : "\n"); return sumstatus; } @@ -242,23 +249,25 @@ finddisk(const char *name) } static void -addpart(const char *type, const char *devname, const char *mntpt, - struct quotaname *qnp) +addpart(struct fstab *fs, struct quotafile *qfu, struct quotafile *qfg) { - struct diskentry *d = finddisk(devname); + struct diskentry *d = finddisk(fs->fs_spec); struct partentry *p; TAILQ_FOREACH(p, &d->d_part, p_entries) - if (strcmp(p->p_devname, devname) == 0) { - warnx("%s in fstab more than once!\n", devname); + if (strcmp(p->p_devname, fs->fs_spec) == 0) { + warnx("%s in fstab more than once!\n", fs->fs_spec); return; } p = emalloc(sizeof(*p)); - p->p_devname = estrdup(devname); - p->p_mntpt = estrdup(mntpt); - p->p_type = estrdup(type); - p->p_quota = qnp; + p->p_devname = estrdup(blockcheck(fs->fs_spec)); + if (qfu != NULL) + p->p_mntpt = quota_fsname(qfu); + else + p->p_mntpt = quota_fsname(qfg); + p->p_qfu = qfu; + p->p_qfg = qfg; TAILQ_INSERT_TAIL(&d->d_part, p, p_entries); } @@ -275,6 +284,6 @@ startdisk(struct diskentry *d) return (8); } if (d->d_pid == 0) - exit(chkquota(p->p_devname, p->p_mntpt, p->p_quota)); + exit(chkquota(p->p_devname, p->p_qfu, p->p_qfg)); return (0); } diff --git a/sbin/quotacheck/quotacheck.8 b/sbin/quotacheck/quotacheck.8 index d5f60f279c5e..1c34cd388d54 100644 --- a/sbin/quotacheck/quotacheck.8 +++ b/sbin/quotacheck/quotacheck.8 @@ -40,10 +40,12 @@ .Sh SYNOPSIS .Nm .Op Fl guv +.Op Fl c Ar 32 | 64 .Op Fl l Ar maxrun .Fl a .Nm .Op Fl guv +.Op Fl c Ar 32 | 64 .Ar filesystem ... .Sh DESCRIPTION The @@ -69,6 +71,22 @@ to be read-write with disk quotas. By default only the types of quotas listed in .Pa /etc/fstab are checked. +.It Fl c Ar 32 | 64 +Before performing its checks, +.Nm +will convert the quota file to the specified word size. +A conversion size of 64 is given to request conversion to +the new 64-bit quota file format. +A conversion size of 32 is given to request conversion back to +the old 32-bit quota file format. +The original quota file is left unchanged and moved aside with an +underscore and its format size plus a +.Pa .orig +extension added to its name. +Thus, the original 32-bit +.Pa quota.user +quota file converted to the 64-bit format quota file will be renamed to +.Pa quota.user_32.orig . .It Fl g Only group quotas listed in .Pa /etc/fstab diff --git a/sbin/quotacheck/quotacheck.c b/sbin/quotacheck/quotacheck.c index 863932a12d2e..ed9ce753c553 100644 --- a/sbin/quotacheck/quotacheck.c +++ b/sbin/quotacheck/quotacheck.c @@ -61,6 +61,7 @@ __FBSDID("$FreeBSD$"); #include <fcntl.h> #include <fstab.h> #include <grp.h> +#include <libutil.h> #include <pwd.h> #include <stdio.h> #include <stdlib.h> @@ -109,27 +110,26 @@ struct fileusage { struct fileusage *fuhead[MAXQUOTAS][FUHASH]; int aflag; /* all file systems */ +int cflag; /* convert format to 32 or 64 bit size */ int gflag; /* check group quotas */ int uflag; /* check user quotas */ int vflag; /* verbose */ int fi; /* open disk file descriptor */ struct fileusage * - addid(u_long, int, char *, char *); -char *blockcheck(char *); + addid(u_long, int, char *, const char *); void bread(ufs2_daddr_t, char *, long); void freeinodebuf(void); union dinode * getnextinode(ino_t); int getquotagid(void); -int hasquota(struct fstab *, int, char **); struct fileusage * lookup(u_long, int); -struct quotaname *needchk(struct fstab *); int oneof(char *, char*[], int); -void printchanges(char *, int, struct dqblk *, struct fileusage *, u_long); +void printchanges(const char *, int, struct dqblk *, struct fileusage *, + u_long); void setinodebuf(ino_t); -int update(char *, char *, int); +int update(const char *, struct quotafile *, int); void usage(void); int @@ -138,17 +138,22 @@ main(int argc, char *argv[]) struct fstab *fs; struct passwd *pw; struct group *gr; - struct quotaname *qnp; + struct quotafile *qfu, *qfg; int i, argnum, maxrun, errs, ch; long done = 0; char *name; errs = maxrun = 0; - while ((ch = getopt(argc, argv, "aguvl:")) != -1) { + while ((ch = getopt(argc, argv, "ac:guvl:")) != -1) { switch(ch) { case 'a': aflag++; break; + case 'c': + if (cflag) + usage(); + cflag = atoi(optarg); + break; case 'g': gflag++; break; @@ -169,6 +174,8 @@ main(int argc, char *argv[]) argv += optind; if ((argc == 0 && !aflag) || (argc > 0 && aflag)) usage(); + if (cflag && cflag != 32 && cflag != 64) + usage(); if (!gflag && !uflag) { gflag++; uflag++; @@ -193,16 +200,27 @@ main(int argc, char *argv[]) if (maxrun > 0) warnx("the -l option is now deprecated"); if (aflag) - exit(checkfstab()); + exit(checkfstab(uflag, gflag)); if (setfsent() == 0) errx(1, "%s: can't open", FSTAB); while ((fs = getfsent()) != NULL) { if (((argnum = oneof(fs->fs_file, argv, argc)) >= 0 || - (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) && - (qnp = needchk(fs)) && + (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) && (name = blockcheck(fs->fs_spec))) { done |= 1 << argnum; - errs += chkquota(name, fs->fs_file, qnp); + qfu = NULL; + if (uflag) + qfu = quota_open(fs, USRQUOTA, O_CREAT|O_RDWR); + qfg = NULL; + if (gflag) + qfg = quota_open(fs, GRPQUOTA, O_CREAT|O_RDWR); + if (qfu == NULL && qfg == NULL) + continue; + errs += chkquota(name, qfu, qfg); + if (qfu) + quota_close(qfu); + if (qfg) + quota_close(qfg); } } endfsent(); @@ -217,37 +235,11 @@ void usage(void) { (void)fprintf(stderr, "%s\n%s\n", - "usage: quotacheck [-guv] [-l maxrun] -a", - " quotacheck [-guv] filesystem ..."); + "usage: quotacheck [-guv] [-c 32 | 64] [-l maxrun] -a", + " quotacheck [-guv] [-c 32 | 64] filesystem ..."); exit(1); } -struct quotaname * -needchk(struct fstab *fs) -{ - struct quotaname *qnp; - char *qfnp; - - if (strcmp(fs->fs_vfstype, "ufs") || - strcmp(fs->fs_type, FSTAB_RW)) - return (NULL); - if ((qnp = malloc(sizeof(*qnp))) == NULL) - errx(1, "malloc failed"); - qnp->flags = 0; - if (gflag && hasquota(fs, GRPQUOTA, &qfnp)) { - strcpy(qnp->grpqfname, qfnp); - qnp->flags |= HASGRP; - } - if (uflag && hasquota(fs, USRQUOTA, &qfnp)) { - strcpy(qnp->usrqfname, qfnp); - qnp->flags |= HASUSR; - } - if (qnp->flags) - return (qnp); - free(qnp); - return (NULL); -} - /* * Possible superblock locations ordered from most to least likely. */ @@ -257,20 +249,49 @@ static int sblock_try[] = SBLOCKSEARCH; * Scan the specified file system to check quota(s) present on it. */ int -chkquota(char *fsname, char *mntpt, struct quotaname *qnp) +chkquota(char *specname, struct quotafile *qfu, struct quotafile *qfg) { struct fileusage *fup; union dinode *dp; int cg, i, mode, errs = 0; ino_t ino, inosused, userino = 0, groupino = 0; dev_t dev, userdev = 0, groupdev = 0; - char *cp; struct stat sb; + const char *mntpt; + char *cp; - if (qnp == NULL) - err(1, "null quota information passed to chkquota()\n"); - if ((fi = open(fsname, O_RDONLY, 0)) < 0) { - warn("%s", fsname); + if (qfu != NULL) + mntpt = quota_fsname(qfu); + else if (qfg != NULL) + mntpt = quota_fsname(qfg); + else + errx(1, "null quotafile information passed to chkquota()\n"); + if (cflag) { + if (vflag && qfu != NULL) + printf("%s: convert user quota to %d bits\n", + mntpt, cflag); + if (qfu != NULL && quota_convert(qfu, cflag) < 0) { + if (errno == EBADF) + errx(1, + "%s: cannot convert an active quota file", + mntpt); + err(1, "user quota conversion to size %d failed", + cflag); + } + if (vflag && qfg != NULL) + printf("%s: convert group quota to %d bits\n", + mntpt, cflag); + if (qfg != NULL && quota_convert(qfg, cflag) < 0) { + if (errno == EBADF) + errx(1, + "%s: cannot convert an active quota file", + mntpt); + err(1, "group quota conversion to size %d failed", + cflag); + } + } + if ((fi = open(specname, O_RDONLY, 0)) < 0) { + warn("%s", specname); return (1); } if ((stat(mntpt, &sb)) < 0) { @@ -280,21 +301,20 @@ chkquota(char *fsname, char *mntpt, struct quotaname *qnp) dev = sb.st_dev; if (vflag) { (void)printf("*** Checking "); - if (qnp->flags & HASUSR) - (void)printf("%s%s", qfextension[USRQUOTA], - (qnp->flags & HASGRP) ? " and " : ""); - if (qnp->flags & HASGRP) - (void)printf("%s", qfextension[GRPQUOTA]); - (void)printf(" quotas for %s (%s)\n", fsname, mntpt); + if (qfu) + (void)printf("user%s", qfg ? " and " : ""); + if (qfg) + (void)printf("group"); + (void)printf(" quotas for %s (%s)\n", specname, mntpt); } - if (qnp->flags & HASUSR) { - if (stat(qnp->usrqfname, &sb) == 0) { + if (qfu) { + if (stat(quota_qfname(qfu), &sb) == 0) { userino = sb.st_ino; userdev = sb.st_dev; } } - if (qnp->flags & HASGRP) { - if (stat(qnp->grpqfname, &sb) == 0) { + if (qfg) { + if (stat(quota_qfname(qfg), &sb) == 0) { groupino = sb.st_ino; groupdev = sb.st_dev; } @@ -382,7 +402,7 @@ chkquota(char *fsname, char *mntpt, struct quotaname *qnp) if ((ino == userino && dev == userdev) || (ino == groupino && dev == groupdev)) continue; - if (qnp->flags & HASGRP) { + if (qfg) { fup = addid((u_long)DIP(dp, di_gid), GRPQUOTA, (char *)0, mntpt); fup->fu_curinodes++; @@ -390,7 +410,7 @@ chkquota(char *fsname, char *mntpt, struct quotaname *qnp) mode == IFLNK) fup->fu_curblocks += DIP(dp, di_blocks); } - if (qnp->flags & HASUSR) { + if (qfu) { fup = addid((u_long)DIP(dp, di_uid), USRQUOTA, (char *)0, mntpt); fup->fu_curinodes++; @@ -401,10 +421,10 @@ chkquota(char *fsname, char *mntpt, struct quotaname *qnp) } } freeinodebuf(); - if (qnp->flags & HASUSR) - errs += update(mntpt, qnp->usrqfname, USRQUOTA); - if (qnp->flags & HASGRP) - errs += update(mntpt, qnp->grpqfname, GRPQUOTA); + if (qfu) + errs += update(mntpt, qfu, USRQUOTA); + if (qfg) + errs += update(mntpt, qfg, GRPQUOTA); close(fi); (void)fflush(stdout); return (errs); @@ -414,63 +434,21 @@ chkquota(char *fsname, char *mntpt, struct quotaname *qnp) * Update a specified quota file. */ int -update(char *fsname, char *quotafile, int type) +update(const char *fsname, struct quotafile *qf, int type) { struct fileusage *fup; - FILE *qfi, *qfo; u_long id, lastid, highid = 0; - off_t offset; - int i; struct dqblk dqbuf; struct stat sb; - static int warned = 0; static struct dqblk zerodqbuf; static struct fileusage zerofileusage; - if ((qfo = fopen(quotafile, "r+")) == NULL) { - if (errno == ENOENT) - qfo = fopen(quotafile, "w+"); - if (qfo) { - warnx("creating quota file %s", quotafile); -#define MODE (S_IRUSR|S_IWUSR|S_IRGRP) - (void) fchown(fileno(qfo), getuid(), getquotagid()); - (void) fchmod(fileno(qfo), MODE); - } else { - warn("%s", quotafile); - return (1); - } - } - if ((qfi = fopen(quotafile, "r")) == NULL) { - warn("%s", quotafile); - (void) fclose(qfo); - return (1); - } - if (quotactl(fsname, QCMD(Q_SYNC, type), (u_long)0, (caddr_t)0) < 0 && - errno == EOPNOTSUPP && !warned && vflag) { - warned++; - (void)printf("*** Warning: %s\n", - "Quotas are not compiled into this kernel"); - } - if (fstat(fileno(qfi), &sb) < 0) { - warn("Cannot fstat quota file %s\n", quotafile); - (void) fclose(qfo); - (void) fclose(qfi); - return (1); - } - if ((sb.st_size % sizeof(struct dqblk)) != 0) - warn("%s size is not a multiple of dqblk\n", quotafile); - /* * Scan the on-disk quota file and record any usage changes. */ - - if (sb.st_size != 0) - lastid = (sb.st_size / sizeof(struct dqblk)) - 1; - else - lastid = 0; - for (id = 0, offset = 0; id <= lastid; - id++, offset += sizeof(struct dqblk)) { - if (fread((char *)&dqbuf, sizeof(struct dqblk), 1, qfi) == 0) + lastid = quota_maxid(qf); + for (id = 0; id <= lastid; id++) { + if (quota_read(qf, &dqbuf, id) < 0) dqbuf = zerodqbuf; if ((fup = lookup(id, type)) == NULL) fup = &zerofileusage; @@ -485,27 +463,9 @@ update(char *fsname, char *quotafile, int type) continue; } printchanges(fsname, type, &dqbuf, fup, id); - /* - * 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 && - fup->fu_curblocks >= dqbuf.dqb_bsoftlimit) - dqbuf.dqb_btime = 0; - if (dqbuf.dqb_isoftlimit && id != 0 && - dqbuf.dqb_curinodes < dqbuf.dqb_isoftlimit && - fup->fu_curinodes >= dqbuf.dqb_isoftlimit) - dqbuf.dqb_itime = 0; dqbuf.dqb_curinodes = fup->fu_curinodes; dqbuf.dqb_curblocks = fup->fu_curblocks; - if (fseeko(qfo, offset, SEEK_SET) < 0) { - warn("%s: seek failed", quotafile); - return(1); - } - fwrite((char *)&dqbuf, sizeof(struct dqblk), 1, qfo); - (void) quotactl(fsname, QCMD(Q_SETUSE, type), id, - (caddr_t)&dqbuf); + (void) quota_write_usage(qf, &dqbuf, id); fup->fu_curinodes = 0; fup->fu_curblocks = 0; } @@ -515,9 +475,8 @@ update(char *fsname, char *quotafile, int type) * that are not currently recorded in the quota file. E.g. * ids that are past the end of the current file. */ - - for (i = 0; i < FUHASH; i++) { - for (fup = fuhead[type][i]; fup != NULL; fup = fup->fu_next) { + for (id = 0; id < FUHASH; id++) { + for (fup = fuhead[type][id]; fup != NULL; fup = fup->fu_next) { if (fup->fu_id <= lastid) continue; if (fup->fu_curinodes == 0 && fup->fu_curblocks == 0) @@ -525,26 +484,24 @@ update(char *fsname, char *quotafile, int type) bzero(&dqbuf, sizeof(struct dqblk)); if (fup->fu_id > highid) highid = fup->fu_id; - printchanges(fsname, type, &dqbuf, fup, id); + printchanges(fsname, type, &dqbuf, fup, fup->fu_id); dqbuf.dqb_curinodes = fup->fu_curinodes; dqbuf.dqb_curblocks = fup->fu_curblocks; - offset = (off_t)fup->fu_id * sizeof(struct dqblk); - if (fseeko(qfo, offset, SEEK_SET) < 0) { - warn("%s: seek failed", quotafile); - return(1); - } - fwrite((char *)&dqbuf, sizeof(struct dqblk), 1, qfo); - (void) quotactl(fsname, QCMD(Q_SETUSE, type), id, - (caddr_t)&dqbuf); + (void) quota_write_usage(qf, &dqbuf, fup->fu_id); fup->fu_curinodes = 0; fup->fu_curblocks = 0; } } - fclose(qfi); - fflush(qfo); - ftruncate(fileno(qfo), - (((off_t)highid + 1) * sizeof(struct dqblk))); - fclose(qfo); + /* + * If this is old format file, then size may be smaller, + * so ensure that we only truncate when it will make things + * smaller, and not if it will grow an old format file. + */ + if (highid < lastid && + stat(quota_qfname(qf), &sb) == 0 && + sb.st_size > (((off_t)highid + 2) * sizeof(struct dqblk))) + truncate(quota_qfname(qf), + (((off_t)highid + 2) * sizeof(struct dqblk))); return (0); } @@ -576,55 +533,6 @@ getquotagid(void) } /* - * 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, '=')) != NULL) - *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"); - return (0); - } - return (1); -} - -/* * Routines to manage the file usage table. * * Lookup an id of a specific type. @@ -644,7 +552,7 @@ lookup(u_long id, int type) * Add a new file usage id if it does not already exist. */ struct fileusage * -addid(u_long id, int type, char *name, char *fsname) +addid(u_long id, int type, char *name, const char *fsname) { struct fileusage *fup, **fhp; int len; @@ -779,7 +687,7 @@ bread(ufs2_daddr_t bno, char *buf, long cnt) * Display updated block and i-node counts. */ void -printchanges(char *fsname, int type, struct dqblk *dp, +printchanges(const char *fsname, int type, struct dqblk *dp, struct fileusage *fup, u_long id) { if (!vflag) diff --git a/sbin/quotacheck/quotacheck.h b/sbin/quotacheck/quotacheck.h index aad9d30a76a8..dcff75b68a8c 100644 --- a/sbin/quotacheck/quotacheck.h +++ b/sbin/quotacheck/quotacheck.h @@ -32,12 +32,6 @@ * $FreeBSD$ */ -struct quotaname { - long flags; - char grpqfname[PATH_MAX]; - char usrqfname[PATH_MAX]; -}; - -extern int checkfstab(); -extern int chkquota(char *, char *, struct quotaname *); -extern struct quotaname *needchk(struct fstab *); +extern char *blockcheck(char *); +extern int checkfstab(int, int); +extern int chkquota(char *, struct quotafile *, struct quotafile *); diff --git a/sys/ufs/ufs/quota.h b/sys/ufs/ufs/quota.h index 28f4e92d8fcb..ca0dcced7d2a 100644 --- a/sys/ufs/ufs/quota.h +++ b/sys/ufs/ufs/quota.h @@ -81,10 +81,14 @@ #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) */ +#define Q_GETQUOTASIZE 0x0A00 /* get bit-size of quota file fields */ /* * The following structure defines the format of the disk quota file @@ -93,7 +97,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 +108,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 +153,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,12 +227,16 @@ 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 *); +int getquotasize(struct thread *, struct mount *, u_long, int, void *); vfs_quotactl_t ufs_quotactl; #else /* !_KERNEL */ diff --git a/sys/ufs/ufs/ufs_quota.c b/sys/ufs/ufs/ufs_quota.c index 2db0444238d6..c5161899aa8c 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 @@ -496,6 +508,9 @@ quotaon(struct thread *td, struct mount *mp, int type, void *fname) if (error) return (error); + if (mp->mnt_flag & MNT_RDONLY) + return (EROFS); + ump = VFSTOUFS(mp); dq = NODQUOT; @@ -522,10 +537,18 @@ quotaon(struct thread *td, struct mount *mp, int type, void *fname) return (EALREADY); } ump->um_qflags[type] |= QTF_OPENING|QTF_CLOSING; + UFS_UNLOCK(ump); + if ((error = dqopen(vp, ump, type)) != 0) { + UFS_LOCK(ump); + 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); - UFS_UNLOCK(ump); vpp = &ump->um_quotas[type]; if (*vpp != vp) @@ -734,8 +757,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 +790,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 +798,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 +861,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 +908,114 @@ 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_GETQUOTASIZE - get bit-size of quota file fields + */ +int +getquotasize(struct thread *td, struct mount *mp, u_long id, int type, + void *sizep) +{ + struct ufsmount *ump = VFSTOUFS(mp); + int bitsize; + + UFS_LOCK(ump); + if (ump->um_quotas[type] == NULLVP || + (ump->um_qflags[type] & QTF_CLOSING)) { + UFS_UNLOCK(ump); + return (EINVAL); + } + if ((ump->um_qflags[type] & QTF_64BIT) != 0) + bitsize = 64; + else + bitsize = 32; + UFS_UNLOCK(ump); + return (copyout(&bitsize, sizep, sizeof(int))); +} + /* * Q_SYNC - sync quota files to disk. */ @@ -1025,6 +1153,60 @@ 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); + } + + UFS_LOCK(ump); + 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)) { + /* XXX: what if the magic matches, but the sizes are wrong? */ + ump->um_qflags[type] |= QTF_64BIT; + } else { + ump->um_qflags[type] &= ~QTF_64BIT; + } + UFS_UNLOCK(ump); + + return (0); +} + +/* * Obtain a dquot structure for the specified identifier and quota file * reading the information from the file if necessary. */ @@ -1032,6 +1214,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 +1305,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 +1352,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 +1493,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 +1541,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 +1573,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 +1613,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 f6b6b1e2ef8c..0eeb14fc54e8 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; @@ -139,6 +151,10 @@ ufs_quotactl(mp, cmds, id, arg) error = getquota(td, mp, id, type, arg); break; + case Q_GETQUOTASIZE: + error = getquotasize(td, mp, id, type, arg); + break; + case Q_SYNC: error = qsync(mp); break; diff --git a/sys/ufs/ufs/ufsmount.h b/sys/ufs/ufs/ufsmount.h index 3f356b1d521c..d5669179dac2 100644 --- a/sys/ufs/ufs/ufsmount.h +++ b/sys/ufs/ufs/ufsmount.h @@ -129,6 +129,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.bin/quota/Makefile b/usr.bin/quota/Makefile index a479402878fe..26585ae2b725 100644 --- a/usr.bin/quota/Makefile +++ b/usr.bin/quota/Makefile @@ -3,6 +3,7 @@ PROG= quota BINOWN= root +BINMODE=4555 DPADD= ${LIBRPCSVC} ${LIBUTIL} LDADD= -lrpcsvc -lutil diff --git a/usr.bin/quota/quota.c b/usr.bin/quota/quota.c index f504e7c31593..240a7564bae4 100644 --- a/usr.bin/quota/quota.c +++ b/usr.bin/quota/quota.c @@ -87,7 +87,7 @@ struct quotause { char fsname[MAXPATHLEN + 1]; }; -static char *timeprt(time_t seconds); +static char *timeprt(int64_t seconds); static struct quotause *getprivs(long id, int quotatype); static void usage(void); static int showuid(u_long uid); @@ -97,7 +97,6 @@ static int showgrpname(char *name); static int showquotas(int type, u_long id, const char *name); static void showrawquotas(int type, u_long id, struct quotause *qup); static void heading(int type, u_long id, const char *name, const char *tag); -static int ufshasquota(struct fstab *fs, int type, char **qfnamep); static int getufsquota(struct fstab *fs, struct quotause *qup, long id, int quotatype); static int getnfsquota(struct statfs *fst, struct quotause *qup, long id, @@ -117,8 +116,7 @@ int main(int argc, char *argv[]) { int ngroups; - long ngroups_max; - gid_t mygid, *gidset; + gid_t mygid, gidset[NGROUPS]; int i, ch, gflag = 0, uflag = 0, errflag = 0; while ((ch = getopt(argc, argv, "f:ghlrquv")) != -1) { @@ -160,18 +158,13 @@ main(int argc, char *argv[]) errflag += showuid(getuid()); if (gflag) { mygid = getgid(); - ngroups_max = sysconf(_SC_NGROUPS_MAX) + 1; - if ((gidset = malloc(sizeof(gid_t) * ngroups_max)) - == NULL) - err(1, "malloc"); - ngroups = getgroups(ngroups_max, gidset); + ngroups = getgroups(NGROUPS, gidset); if (ngroups < 0) err(1, "getgroups"); errflag += showgid(mygid); for (i = 0; i < ngroups; i++) if (gidset[i] != mygid) errflag += showgid(gidset[i]); - free(gidset); } return(errflag); } @@ -271,7 +264,7 @@ showgrpname(char *name) } static void -prthumanval(int len, int64_t bytes) +prthumanval(int len, u_int64_t bytes) { char buf[len + 1]; @@ -296,7 +289,7 @@ showquotas(int type, u_long id, const char *name) time(&now); quplist = getprivs(id, type); for (qup = quplist; qup; qup = qup->next) { - msgi = (char *)0; + msgi = NULL; if (qup->dqblk.dqb_ihardlimit && qup->dqblk.dqb_curinodes >= qup->dqblk.dqb_ihardlimit) { overquota++; @@ -310,7 +303,7 @@ showquotas(int type, u_long id, const char *name) else msgi = "Over file quota on"; } - msgb = (char *)0; + msgb = NULL; if (qup->dqblk.dqb_bhardlimit && qup->dqblk.dqb_curblocks >= qup->dqblk.dqb_bhardlimit) { overquota++; @@ -335,91 +328,95 @@ showquotas(int type, u_long id, const char *name) qup->dqblk.dqb_bhardlimit == 0) continue; if (qflag) { - if ((msgi != (char *)0 || msgb != (char *)0) && + if ((msgi != NULL || msgb != NULL) && lines++ == 0) heading(type, id, name, ""); - if (msgi != (char *)0) + if (msgi != NULL) printf("\t%s %s\n", msgi, qup->fsname); - if (msgb != (char *)0) + if (msgb != NULL) printf("\t%s %s\n", msgb, qup->fsname); continue; } - if (vflag || - qup->dqblk.dqb_curblocks || - qup->dqblk.dqb_curinodes) { - if (lines++ == 0) - heading(type, id, name, ""); - nam = qup->fsname; - if (strlen(qup->fsname) > 15) { - printf("%s\n", qup->fsname); - nam = ""; - } - printf("%15s", nam); - if (hflag) { - printf(" "); - prthumanval(4, dbtob(qup->dqblk.dqb_curblocks)); - printf("%c ", (msgb == (char *)0) ? ' ' : '*'); - prthumanval(4, dbtob(qup->dqblk.dqb_bsoftlimit)); - printf(" "); - prthumanval(4, dbtob(qup->dqblk.dqb_bhardlimit)); - } else { - printf(" %7ju%c %6ju %7ju", - (uintmax_t)(dbtob(qup->dqblk.dqb_curblocks) - / 1024), - (msgb == NULL) ? ' ' : '*', - (uintmax_t)(dbtob(qup->dqblk.dqb_bsoftlimit) - / 1024), - (uintmax_t)(dbtob(qup->dqblk.dqb_bhardlimit) - / 1024)); - } - if (msgb != NULL) - bgrace = timeprt(qup->dqblk.dqb_btime); - if (msgi != NULL) - igrace = timeprt(qup->dqblk.dqb_itime); - printf(" %7s %7ju%c %6ju %7ju %7s\n", - (msgb == NULL) ? "" : bgrace, - (uintmax_t)qup->dqblk.dqb_curinodes, - (msgi == NULL) ? ' ' : '*', - (uintmax_t)qup->dqblk.dqb_isoftlimit, - (uintmax_t)qup->dqblk.dqb_ihardlimit, - (msgi == NULL) ? "" : igrace - ); - if (msgb != NULL) - free(bgrace); - if (msgi != NULL) - free(igrace); + if (!vflag && + qup->dqblk.dqb_curblocks == 0 && + qup->dqblk.dqb_curinodes == 0) continue; + if (lines++ == 0) + heading(type, id, name, ""); + nam = qup->fsname; + if (strlen(qup->fsname) > 15) { + printf("%s\n", qup->fsname); + nam = ""; + } + printf("%-15s", nam); + if (hflag) { + prthumanval(7, dbtob(qup->dqblk.dqb_curblocks)); + printf("%c", (msgb == NULL) ? ' ' : '*'); + prthumanval(7, dbtob(qup->dqblk.dqb_bsoftlimit)); + prthumanval(7, dbtob(qup->dqblk.dqb_bhardlimit)); + } else { + printf(" %7ju%c %7ju %7ju", + dbtob(1024) * (uintmax_t)qup->dqblk.dqb_curblocks, + (msgb == NULL) ? ' ' : '*', + dbtob(1024) * (uintmax_t)qup->dqblk.dqb_bsoftlimit, + dbtob(1024) * (uintmax_t)qup->dqblk.dqb_bhardlimit); } + if (msgb != NULL) + bgrace = timeprt(qup->dqblk.dqb_btime); + if (msgi != NULL) + igrace = timeprt(qup->dqblk.dqb_itime); + printf("%8s %6ju%c %6ju %6ju%8s\n" + , (msgb == NULL) ? "" : bgrace + , (uintmax_t)qup->dqblk.dqb_curinodes + , (msgi == NULL) ? ' ' : '*' + , (uintmax_t)qup->dqblk.dqb_isoftlimit + , (uintmax_t)qup->dqblk.dqb_ihardlimit + , (msgi == NULL) ? "" : igrace + ); + if (msgb != NULL) + free(bgrace); + if (msgi != NULL) + free(igrace); } if (!qflag && !rflag && lines == 0) heading(type, id, name, "none"); - return(overquota); + return (overquota); } static void showrawquotas(int type, u_long id, struct quotause *qup) { - time_t tt; + time_t t; + printf("Raw %s quota information for id %lu on %s\n", type == USRQUOTA ? "user" : "group", id, qup->fsname); - printf("block hard limit: %ju\n", (uintmax_t)qup->dqblk.dqb_bhardlimit); - printf("block soft limit: %ju\n", (uintmax_t)qup->dqblk.dqb_bsoftlimit); - printf("current block count: %ju\n", (uintmax_t)qup->dqblk.dqb_curblocks); - printf("i-node hard limit: %ju\n", (uintmax_t)qup->dqblk.dqb_ihardlimit); - printf("i-node soft limit: %ju\n", (uintmax_t)qup->dqblk.dqb_isoftlimit); - printf("current i-node count: %ju\n", (uintmax_t)qup->dqblk.dqb_curinodes); - printf("block grace time: %jd", (intmax_t)qup->dqblk.dqb_btime); + printf("block hard limit: %ju\n", + (uintmax_t)qup->dqblk.dqb_bhardlimit); + printf("block soft limit: %ju\n", + (uintmax_t)qup->dqblk.dqb_bsoftlimit); + printf("current block count: %ju\n", + (uintmax_t)qup->dqblk.dqb_curblocks); + printf("i-node hard limit: %ju\n", + (uintmax_t)qup->dqblk.dqb_ihardlimit); + printf("i-node soft limit: %ju\n", + (uintmax_t)qup->dqblk.dqb_isoftlimit); + printf("current i-node count: %ju\n", + (uintmax_t)qup->dqblk.dqb_curinodes); + printf("block grace time: %jd", + (intmax_t)qup->dqblk.dqb_btime); if (qup->dqblk.dqb_btime != 0) { - tt = qup->dqblk.dqb_btime; - printf(" %s", ctime(&tt)); - } else + t = qup->dqblk.dqb_btime; + printf(" %s", ctime(&t)); + } else { printf("\n"); + } printf("i-node grace time: %jd", (intmax_t)qup->dqblk.dqb_itime); if (qup->dqblk.dqb_itime != 0) { - tt = qup->dqblk.dqb_itime; - printf(" %s", ctime(&tt)); - } else + t = qup->dqblk.dqb_itime; + printf(" %s", ctime(&t)); + } else { printf("\n"); + } } @@ -430,7 +427,7 @@ heading(int type, u_long id, const char *name, const char *tag) printf("Disk quotas for %s %s (%cid %lu): %s\n", qfextension[type], name, *qfextension[type], id, tag); if (!qflag && tag[0] == '\0') { - printf("%15s %7s %6s %7s %7s %7s %6s %7s %7s\n" + printf("%-15s %7s %8s %7s %7s %6s %7s %6s%8s\n" , "Filesystem" , "usage" , "quota" @@ -448,33 +445,35 @@ heading(int type, u_long id, const char *name, const char *tag) * Calculate the grace period and return a printable string for it. */ static char * -timeprt(time_t seconds) +timeprt(int64_t seconds) { time_t hours, minutes; - char *buf; + char *buf; static time_t now; if (now == 0) time(&now); if (now > seconds) { - return strdup("none"); + if ((buf = strdup("none")) == NULL) + errx(1, "strdup() failed in timeprt()"); + return (buf); } seconds -= now; minutes = (seconds + 30) / 60; hours = (minutes + 30) / 60; if (hours >= 36) { if (asprintf(&buf, "%lddays", ((long)hours + 12) / 24) < 0) - errx(1, "asprintf failed in timeprt(1)"); + errx(1, "asprintf() failed in timeprt(1)"); return (buf); } if (minutes >= 60) { if (asprintf(&buf, "%2ld:%ld", (long)minutes / 60, (long)minutes % 60) < 0) - errx(1, "asprintf failed in timeprt(2)"); + errx(1, "asprintf() failed in timeprt(2)"); return (buf); } if (asprintf(&buf, "%2ld", (long)minutes) < 0) - errx(1, "asprintf failed in timeprt(3)"); + errx(1, "asprintf() failed in timeprt(3)"); return (buf); } @@ -499,7 +498,7 @@ getprivs(long id, int quotatype) if (nfst == 0) errx(2, "no filesystems mounted!"); setfsent(); - for (i=0; i<nfst; i++) { + for (i = 0; i < nfst; i++) { if (qup == NULL) { if ((qup = (struct quotause *)malloc(sizeof *qup)) == NULL) @@ -548,87 +547,18 @@ getprivs(long id, int quotatype) } /* - * Check to see if a particular quota is to be enabled. + * Check to see if a particular quota is available. */ static int -ufshasquota(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"); - return (0); - } - return (1); -} - -static int getufsquota(struct fstab *fs, struct quotause *qup, long id, int quotatype) { - char *qfpathname; - int fd, qcmd; + struct quotafile *qf; - qcmd = QCMD(Q_GETQUOTA, quotatype); - if (!ufshasquota(fs, quotatype, &qfpathname)) + if ((qf = quota_open(fs, quotatype, O_RDONLY)) == NULL) return (0); - - if (quotactl(fs->fs_file, qcmd, id, (char *)&qup->dqblk) != 0) { - if ((fd = open(qfpathname, O_RDONLY)) < 0) { - warn("%s", qfpathname); - return (0); - } - (void) lseek(fd, (off_t)(id * sizeof(struct dqblk)), L_SET); - 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: %s", qfpathname); - close(fd); - return (0); - } - close(fd); - } + if (quota_read(qf, &qup->dqblk, id) != 0) + return (0); + quota_close(qf); return (1); } diff --git a/usr.sbin/edquota/Makefile b/usr.sbin/edquota/Makefile index b8c35ae5ce37..1196e4721417 100644 --- a/usr.sbin/edquota/Makefile +++ b/usr.sbin/edquota/Makefile @@ -4,4 +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 7cf72dd11186..326c8373c252 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 f38fa77b39eb..69ea497fa52a 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(uint64_t, char *, uint64_t *); +char *cvtstoa(uint64_t); +uint64_t cvtblkval(uint64_t, char, const char *); +uint64_t cvtinoval(uint64_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; + uint64_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), "%juk", (uintmax_t)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), "%ju", (uintmax_t)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; + uintmax_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: %ju%c, limits (soft = %ju%c, hard = %ju%c)", + &curitems, &curitemunits, &softlimit, &softunits, + &hardlimit, &hardunits); + /* + * The next three check for old-style input formats. + */ + if (cnt != 6) + cnt = sscanf(cp, + " in use: %ju%c, limits (soft = %ju%c hard = %ju%c", + &curitems, &curitemunits, &softlimit, + &softunits, &hardlimit, &hardunits); + if (cnt != 6) + cnt = sscanf(cp, + " in use: %ju%c, limits (soft = %ju%c hard = %ju%c)", + &curitems, &curitemunits, &softlimit, + &softunits, &hardlimit, &hardunits); + if (cnt != 6) + cnt = sscanf(cp, + " in use: %ju%c, limits (soft = %ju%c, hard = %ju%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: %ju%c limits (soft = %ju%c, hard = %ju%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: %ju%c limits (soft = %ju%c hard = %ju%c", + &curitems, &curitemunits, &softlimit, + &softunits, &hardlimit, &hardunits); + if (cnt != 6) + cnt = sscanf(&cp[7], + " in use: %ju%c limits (soft = %ju%c hard = %ju%c)", + &curitems, &curitemunits, &softlimit, + &softunits, &hardlimit, &hardunits); + if (cnt != 6) + cnt = sscanf(&cp[7], + " in use: %ju%c limits (soft = %ju%c, hard = %ju%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; + uintmax_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: %ju %s file grace period: %ju %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,25 @@ readtimes(struct quotause *quplist, char *inname) * Convert seconds to ASCII times. */ char * -cvtstoa(time_t secs) +cvtstoa(uint64_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, "%ju day%s", (uintmax_t)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, "%ju hour%s", (uintmax_t)secs, + secs == 1 ? "" : "s"); } else if (secs % 60 == 0) { secs /= 60; - sprintf(buf, "%ld minute%s", (long)secs, secs == 1 ? "" : "s"); + sprintf(buf, "%ju minute%s", (uintmax_t)secs, + secs == 1 ? "" : "s"); } else - sprintf(buf, "%ld second%s", (long)secs, secs == 1 ? "" : "s"); + sprintf(buf, "%ju second%s", (uintmax_t)secs, + secs == 1 ? "" : "s"); return (buf); } @@ -790,7 +805,7 @@ cvtstoa(time_t secs) * Convert ASCII input times to seconds. */ int -cvtatos(time_t period, char *units, time_t *seconds) +cvtatos(uint64_t period, char *units, uint64_t *seconds) { if (bcmp(units, "second", 6) == 0) @@ -802,7 +817,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 +825,109 @@ cvtatos(time_t period, char *units, time_t *seconds) } /* + * Convert a limit to number of disk blocks. + */ +uint64_t +cvtblkval(uint64_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, "%ju%c: unknown units for %s, specify " + "none, K, M, G, T, P, or E\n", + (uintmax_t)limit, units, itemname); + break; + } + return (limit); +} + +/* + * Convert a limit to number of inodes. + */ +uint64_t +cvtinoval(uint64_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, "%ju%c: unknown units for %s, specify " + "none, K, M, G, T, P, or E\n", + (uintmax_t)limit, units, itemname); + break; + } + return (limit); +} + +/* * Free a list of quotause structures. */ void @@ -818,6 +936,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 +957,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); -} diff --git a/usr.sbin/quotaon/Makefile b/usr.sbin/quotaon/Makefile index 984008cf8e80..23ba8d1dc4c8 100644 --- a/usr.sbin/quotaon/Makefile +++ b/usr.sbin/quotaon/Makefile @@ -6,4 +6,7 @@ LINKS= ${BINDIR}/quotaon ${BINDIR}/quotaoff MAN= quotaon.8 MLINKS= quotaon.8 quotaoff.8 +DPADD= ${LIBUTIL} +LDADD= -lutil + .include <bsd.prog.mk> diff --git a/usr.sbin/quotaon/quotaon.c b/usr.sbin/quotaon/quotaon.c index 84a523d7b1ca..d510e803c1f5 100644 --- a/usr.sbin/quotaon/quotaon.c +++ b/usr.sbin/quotaon/quotaon.c @@ -53,6 +53,7 @@ __FBSDID("$FreeBSD$"); #include <ufs/ufs/quota.h> #include <err.h> #include <fstab.h> +#include <libutil.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -66,17 +67,15 @@ int gflag; /* operate on group quotas */ int uflag; /* operate on user quotas */ int vflag; /* verbose */ -int hasquota(struct fstab *, int, char **); int oneof(char *, char *[], int); -int quotaonoff(struct fstab *fs, int, int, char *); -int readonly(struct fstab *); +int quotaonoff(struct fstab *fs, int, int); static void usage(void); int main(int argc, char **argv) { struct fstab *fs; - char *qfnp, *whoami; + char *whoami; long argnum, done = 0; int ch, i, offmode = 0, errs = 0; @@ -119,19 +118,19 @@ main(int argc, char **argv) strcmp(fs->fs_type, FSTAB_RW)) continue; if (aflag) { - if (gflag && hasquota(fs, GRPQUOTA, &qfnp)) - errs += quotaonoff(fs, offmode, GRPQUOTA, qfnp); - if (uflag && hasquota(fs, USRQUOTA, &qfnp)) - errs += quotaonoff(fs, offmode, USRQUOTA, qfnp); + if (gflag) + errs += quotaonoff(fs, offmode, GRPQUOTA); + if (uflag) + errs += quotaonoff(fs, offmode, USRQUOTA); continue; } if ((argnum = oneof(fs->fs_file, argv, argc)) >= 0 || (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) { done |= 1 << argnum; - if (gflag && hasquota(fs, GRPQUOTA, &qfnp)) - errs += quotaonoff(fs, offmode, GRPQUOTA, qfnp); - if (uflag && hasquota(fs, USRQUOTA, &qfnp)) - errs += quotaonoff(fs, offmode, USRQUOTA, qfnp); + if (gflag) + errs += quotaonoff(fs, offmode, GRPQUOTA); + if (uflag) + errs += quotaonoff(fs, offmode, USRQUOTA); } } endfsent(); @@ -154,29 +153,31 @@ usage(void) } int -quotaonoff(struct fstab *fs, int offmode, int type, char *qfpathname) +quotaonoff(struct fstab *fs, int offmode, int type) { + struct quotafile *qf; - if (strcmp(fs->fs_file, "/") && readonly(fs)) - return (1); + if ((qf = quota_open(fs, type, O_RDONLY)) == NULL) + return (0); if (offmode) { - if (quotactl(fs->fs_file, QCMD(Q_QUOTAOFF, type), 0, 0) < 0) { - warn("%s", fs->fs_file); + if (quota_off(qf) != 0) { + warn("%s", quota_fsname(qf)); return (1); } if (vflag) - printf("%s: quotas turned off\n", fs->fs_file); - return (0); + printf("%s: quotas turned off\n", quota_fsname(qf)); + quota_close(qf); + return(0); } - if (quotactl(fs->fs_file, QCMD(Q_QUOTAON, type), 0, qfpathname) < 0) { - warnx("using %s on", qfpathname); - warn("%s", fs->fs_file); + if (quota_on(qf) != 0) { + warn("using %s on %s", quota_qfname(qf), quota_fsname(qf)); return (1); } if (vflag) printf("%s: %s quotas turned on with data file %s\n", - fs->fs_file, qfextension[type], qfpathname); - return (0); + quota_fsname(qf), qfextension[type], quota_qfname(qf)); + quota_close(qf); + return(0); } /* @@ -192,73 +193,3 @@ oneof(char *target, char *list[], int cnt) return (i); 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"); - return (0); - } - return (1); -} - -/* - * Verify filesystem is mounted and not readonly. - */ -int -readonly(struct fstab *fs) -{ - struct statfs fsbuf; - - if (statfs(fs->fs_file, &fsbuf) < 0 || - strcmp(fsbuf.f_mntonname, fs->fs_file) || - strcmp(fsbuf.f_mntfromname, fs->fs_spec)) { - printf("%s: not mounted\n", fs->fs_file); - return (1); - } - if (fsbuf.f_flags & MNT_RDONLY) { - printf("%s: mounted read-only\n", fs->fs_file); - return (1); - } - return (0); -} diff --git a/usr.sbin/repquota/Makefile b/usr.sbin/repquota/Makefile index 208abd6553a7..ed8013285597 100644 --- a/usr.sbin/repquota/Makefile +++ b/usr.sbin/repquota/Makefile @@ -3,5 +3,7 @@ PROG= repquota MAN= repquota.8 +DPADD= ${LIBUTIL} +LDADD= -lutil .include <bsd.prog.mk> diff --git a/usr.sbin/repquota/repquota.8 b/usr.sbin/repquota/repquota.8 index b669fdf8266a..8b5ab689ac71 100644 --- a/usr.sbin/repquota/repquota.8 +++ b/usr.sbin/repquota/repquota.8 @@ -39,12 +39,14 @@ .Nd summarize quotas for a file system .Sh SYNOPSIS .Nm +.Op Fl h .Op Fl g .Op Fl n .Op Fl u .Op Fl v .Ar filesystem Ar ... .Nm +.Op Fl h .Op Fl g .Op Fl n .Op Fl u @@ -64,6 +66,9 @@ Print the quotas of all the file systems listed in .It Fl g Print only group quotas (the default is to print both group and user quotas if they exist). +.It Fl h +Display information in a more human readable format +rather than in historic kilobyte format. .It Fl n Display user and group IDs numerically rather than converting to a user or group name. @@ -75,7 +80,7 @@ Print a header line before printing each file system quotas. .El .Pp For each user or group, the current -number files and amount of space (in kilobytes) is +number files and amount of space is printed, along with any quotas created with .Xr edquota 8 . .Pp diff --git a/usr.sbin/repquota/repquota.c b/usr.sbin/repquota/repquota.c index b6d4be6b0092..8fdea201554f 100644 --- a/usr.sbin/repquota/repquota.c +++ b/usr.sbin/repquota/repquota.c @@ -49,12 +49,17 @@ __FBSDID("$FreeBSD$"); */ #include <sys/param.h> #include <sys/mount.h> + #include <ufs/ufs/quota.h> + #include <err.h> #include <errno.h> +#include <fcntl.h> #include <fstab.h> #include <grp.h> +#include <libutil.h> #include <pwd.h> +#include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -79,7 +84,6 @@ const char *qfextension[] = INITQFNAMES; struct fileusage { struct fileusage *fu_next; - struct dqblk fu_dqblk; u_long fu_id; char fu_name[1]; /* actually bigger */ @@ -93,11 +97,12 @@ u_long highid[MAXQUOTAS]; /* highest addid()'ed identifier per type */ int vflag; /* verbose */ int aflag; /* all filesystems */ int nflag; /* display user/group by id */ +int hflag; /* display in human readable format */ -int hasquota(struct fstab *, int, char **); int oneof(char *, char *[], int); -int repquota(struct fstab *, int, char *); +int repquota(struct fstab *, int); char *timeprt(time_t); +static void prthumanval(int64_t bytes); static void usage(void); int @@ -108,9 +113,8 @@ main(int argc, char *argv[]) struct group *gr; int ch, gflag = 0, uflag = 0, errs = 0; long i, argnum, done = 0; - char *qfnp; - while ((ch = getopt(argc, argv, "agnuv")) != -1) { + while ((ch = getopt(argc, argv, "aghnuv")) != -1) { switch(ch) { case 'a': aflag++; @@ -118,6 +122,9 @@ main(int argc, char *argv[]) case 'g': gflag++; break; + case 'h': + hflag++; + break; case 'n': nflag++; break; @@ -157,19 +164,19 @@ main(int argc, char *argv[]) if (strcmp(fs->fs_vfstype, "ufs")) continue; if (aflag) { - if (gflag && hasquota(fs, GRPQUOTA, &qfnp)) - errs += repquota(fs, GRPQUOTA, qfnp); - if (uflag && hasquota(fs, USRQUOTA, &qfnp)) - errs += repquota(fs, USRQUOTA, qfnp); + if (gflag) + errs += repquota(fs, GRPQUOTA); + if (uflag) + errs += repquota(fs, USRQUOTA); continue; } if ((argnum = oneof(fs->fs_file, argv, argc)) >= 0 || (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) { done |= 1 << argnum; - if (gflag && hasquota(fs, GRPQUOTA, &qfnp)) - errs += repquota(fs, GRPQUOTA, qfnp); - if (uflag && hasquota(fs, USRQUOTA, &qfnp)) - errs += repquota(fs, USRQUOTA, qfnp); + if (gflag) + errs += repquota(fs, GRPQUOTA); + if (uflag) + errs += repquota(fs, USRQUOTA); } } endfsent(); @@ -183,87 +190,94 @@ static void usage(void) { fprintf(stderr, "%s\n%s\n", - "usage: repquota [-v] [-g] [-n] [-u] -a", - " repquota [-v] [-g] [-n] [-u] filesystem ..."); + "usage: repquota [-h] [-v] [-g] [-n] [-u] -a", + " repquota [-h] [-v] [-g] [-n] [-u] filesystem ..."); exit(1); } int -repquota(struct fstab *fs, int type, char *qfpathname) +repquota(struct fstab *fs, int type) { struct fileusage *fup; - FILE *qf; - u_long id; + struct quotafile *qf; + u_long id, maxid; struct dqblk dqbuf; - static struct dqblk zerodqblk; - static int warned = 0; static int multiple = 0; - if (quotactl(fs->fs_file, QCMD(Q_SYNC, type), 0, 0) < 0 && - errno == EOPNOTSUPP && !warned && vflag) { - warned++; - fprintf(stdout, - "*** Warning: Quotas are not compiled into this kernel\n"); + if ((qf = quota_open(fs, type, O_RDONLY)) == NULL) { + if (vflag && !aflag) { + if (multiple++) + printf("\n"); + fprintf(stdout, "*** No %s quotas on %s (%s)\n", + qfextension[type], fs->fs_file, fs->fs_spec); + return(1); + } + return(0); } if (multiple++) printf("\n"); if (vflag) fprintf(stdout, "*** Report for %s quotas on %s (%s)\n", qfextension[type], fs->fs_file, fs->fs_spec); - if ((qf = fopen(qfpathname, "r")) == NULL) { - warn("%s", qfpathname); - return (1); - } - for (id = 0; ; id++) { - fread(&dqbuf, sizeof(struct dqblk), 1, qf); - if (feof(qf)) + printf("%*s Block limits File limits\n", + max(MAXLOGNAME - 1, 10), " "); + printf("User%*s used soft hard grace used soft hard grace\n", + max(MAXLOGNAME - 1, 10), " "); + maxid = quota_maxid(qf); + for (id = 0; id <= maxid; id++) { + if (quota_read(qf, &dqbuf, id) != 0) break; if (dqbuf.dqb_curinodes == 0 && dqbuf.dqb_curblocks == 0) continue; if ((fup = lookup(id, type)) == 0) fup = addid(id, type, (char *)0); - fup->fu_dqblk = dqbuf; - } - fclose(qf); - printf("%*s Block limits File limits\n", - max(MAXLOGNAME-1,10), " "); - printf("%s%*s used soft hard grace used soft hard grace\n", - type == USRQUOTA ? "User " : "Group", max(MAXLOGNAME-1,10), " "); - for (id = 0; id <= highid[type]; id++) { - fup = lookup(id, type); - if (fup == 0) - continue; - if (fup->fu_dqblk.dqb_curinodes == 0 && - fup->fu_dqblk.dqb_curblocks == 0) - continue; - printf("%-*s ", max(MAXLOGNAME-1,10), fup->fu_name); - printf("%c%c %8lu %8lu %8lu %6s", - fup->fu_dqblk.dqb_bsoftlimit && - fup->fu_dqblk.dqb_curblocks >= - fup->fu_dqblk.dqb_bsoftlimit ? '+' : '-', - fup->fu_dqblk.dqb_isoftlimit && - fup->fu_dqblk.dqb_curinodes >= - fup->fu_dqblk.dqb_isoftlimit ? '+' : '-', - (u_long)(dbtokb(fup->fu_dqblk.dqb_curblocks)), - (u_long)(dbtokb(fup->fu_dqblk.dqb_bsoftlimit)), - (u_long)(dbtokb(fup->fu_dqblk.dqb_bhardlimit)), - fup->fu_dqblk.dqb_bsoftlimit && - fup->fu_dqblk.dqb_curblocks >= - fup->fu_dqblk.dqb_bsoftlimit ? - timeprt(fup->fu_dqblk.dqb_btime) : "-"); - printf(" %7lu %7lu %7lu %6s\n", - (u_long)fup->fu_dqblk.dqb_curinodes, - (u_long)fup->fu_dqblk.dqb_isoftlimit, - (u_long)fup->fu_dqblk.dqb_ihardlimit, - fup->fu_dqblk.dqb_isoftlimit && - fup->fu_dqblk.dqb_curinodes >= - fup->fu_dqblk.dqb_isoftlimit ? - timeprt(fup->fu_dqblk.dqb_itime) : "-"); - fup->fu_dqblk = zerodqblk; + printf("%-*s ", max(MAXLOGNAME - 1, 10), fup->fu_name); + printf("%c%c", + dqbuf.dqb_bsoftlimit && + dqbuf.dqb_curblocks >= + dqbuf.dqb_bsoftlimit ? '+' : '-', + dqbuf.dqb_isoftlimit && + dqbuf.dqb_curinodes >= + dqbuf.dqb_isoftlimit ? '+' : '-'); + prthumanval(dqbuf.dqb_curblocks); + prthumanval(dqbuf.dqb_bsoftlimit); + prthumanval(dqbuf.dqb_bhardlimit); + printf(" %6s", + dqbuf.dqb_bsoftlimit && + dqbuf.dqb_curblocks >= + dqbuf.dqb_bsoftlimit ? + timeprt(dqbuf.dqb_btime) : "-"); + printf(" %7ju %7ju %7ju %6s\n", + (uintmax_t)dqbuf.dqb_curinodes, + (uintmax_t)dqbuf.dqb_isoftlimit, + (uintmax_t)dqbuf.dqb_ihardlimit, + dqbuf.dqb_isoftlimit && + dqbuf.dqb_curinodes >= + dqbuf.dqb_isoftlimit ? + timeprt(dqbuf.dqb_itime) : "-"); } + quota_close(qf); return (0); } +static void +prthumanval(int64_t blocks) +{ + char buf[7]; + int flags; + + if (!hflag) { + printf(" %6ju", (uintmax_t)dbtokb(blocks)); + return; + } + flags = HN_NOSPACE | HN_DECIMAL; + if (blocks != 0) + flags |= HN_B; + humanize_number(buf, sizeof(buf) - (blocks < 0 ? 0 : 1), + dbtob(blocks), "", HN_AUTOSCALE, flags); + (void)printf("%7s", buf); +} + /* * Check to see if target appears in list of size cnt. */ @@ -279,55 +293,6 @@ oneof(char *target, char *list[], int cnt) } /* - * 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"); - return (0); - } - return (1); -} - -/* * Routines to manage the file usage table. * * Lookup an id of a specific type. |