Index: sys/ufs/ufs/quota.h =================================================================== --- sys/ufs/ufs/quota.h (revision 185617) +++ sys/ufs/ufs/quota.h (working copy) @@ -81,10 +81,13 @@ #define Q_QUOTAON 0x0100 /* enable quotas */ #define Q_QUOTAOFF 0x0200 /* disable quotas */ -#define Q_GETQUOTA 0x0300 /* get limits and usage */ -#define Q_SETQUOTA 0x0400 /* set limits and usage */ -#define Q_SETUSE 0x0500 /* set usage */ +#define Q_GETQUOTA32 0x0300 /* get limits and usage (32-bit version) */ +#define Q_SETQUOTA32 0x0400 /* set limits and usage (32-bit version) */ +#define Q_SETUSE32 0x0500 /* set usage (32-bit version) */ #define Q_SYNC 0x0600 /* sync disk copy of a filesystems quotas */ +#define Q_GETQUOTA 0x0700 /* get limits and usage (64-bit version) */ +#define Q_SETQUOTA 0x0800 /* set limits and usage (64-bit version) */ +#define Q_SETUSE 0x0900 /* set usage (64-bit version) */ /* * The following structure defines the format of the disk quota file @@ -93,7 +96,7 @@ * the vnode for each quota file (a pointer is retained in the ufsmount * structure). */ -struct dqblk { +struct dqblk32 { u_int32_t dqb_bhardlimit; /* absolute limit on disk blks alloc */ u_int32_t dqb_bsoftlimit; /* preferred limit on disk blks */ u_int32_t dqb_curblocks; /* current block count */ @@ -104,6 +107,30 @@ 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 @@ -125,7 +152,7 @@ u_int32_t dq_id; /* identifier this applies to */ struct ufsmount *dq_ump; /* (h) filesystem that this is taken from */ - struct dqblk dq_dqb; /* actual usage & quotas */ + struct dqblk64 dq_dqb; /* actual usage & quotas */ }; /* * Flag values. @@ -199,10 +226,13 @@ void dqrele(struct vnode *, struct dquot *); void dquninit(void); int getinoquota(struct inode *); -int getquota(struct thread *, struct mount *, u_long, int, void *); int qsync(struct mount *mp); int quotaoff(struct thread *td, struct mount *, int); int quotaon(struct thread *td, struct mount *, int, void *); +int getquota32(struct thread *, struct mount *, u_long, int, void *); +int setquota32(struct thread *, struct mount *, u_long, int, void *); +int setuse32(struct thread *, struct mount *, u_long, int, void *); +int getquota(struct thread *, struct mount *, u_long, int, void *); int setquota(struct thread *, struct mount *, u_long, int, void *); int setuse(struct thread *, struct mount *, u_long, int, void *); vfs_quotactl_t ufs_quotactl; Index: sys/ufs/ufs/ufs_quota.c =================================================================== --- sys/ufs/ufs/ufs_quota.c (revision 185617) +++ sys/ufs/ufs/ufs_quota.c (working copy) @@ -39,6 +39,7 @@ #include #include +#include #include #include #include @@ -73,6 +74,7 @@ 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 +82,14 @@ 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 +100,7 @@ * * This routine completely defines the semantics of quotas. * If other criterion want to be used to establish quotas, the - * MAXQUOTAS value in quotas.h should be increased, and the + * MAXQUOTAS value in quota.h should be increased, and the * additional dquots set up here. */ int @@ -522,6 +532,13 @@ return (EALREADY); } ump->um_qflags[type] |= QTF_OPENING|QTF_CLOSING; + if ((error = dqopen(vp, ump, type)) != 0) { + ump->um_qflags[type] &= ~(QTF_OPENING|QTF_CLOSING); + UFS_UNLOCK(ump); + (void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td); + VFS_UNLOCK_GIANT(vfslocked); + return (error); + } MNT_ILOCK(mp); mp->mnt_flag |= MNT_QUOTA; MNT_IUNLOCK(mp); @@ -734,8 +751,9 @@ /* * 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 +784,7 @@ 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 +792,21 @@ /* * 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 +855,21 @@ /* * 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 +902,90 @@ return (0); } +int +getquota32(struct thread *td, struct mount *mp, u_long id, int type, void *addr) +{ + struct dqblk32 dqb32; + struct dqblk64 dqb64; + int error; + + error = _getquota(td, mp, id, type, &dqb64); + if (error) + return (error); + dqb64_dqb32(&dqb64, &dqb32); + error = copyout(&dqb32, addr, sizeof(dqb32)); + return (error); +} + +int +setquota32(struct thread *td, struct mount *mp, u_long id, int type, void *addr) +{ + struct dqblk32 dqb32; + struct dqblk64 dqb64; + int error; + + error = copyin(addr, &dqb32, sizeof(dqb32)); + if (error) + return (error); + dqb32_dqb64(&dqb32, &dqb64); + error = _setquota(td, mp, id, type, &dqb64); + return (error); +} + +int +setuse32(struct thread *td, struct mount *mp, u_long id, int type, void *addr) +{ + struct dqblk32 dqb32; + struct dqblk64 dqb64; + int error; + + error = copyin(addr, &dqb32, sizeof(dqb32)); + if (error) + return (error); + dqb32_dqb64(&dqb32, &dqb64); + error = _setuse(td, mp, id, type, &dqb64); + return (error); +} + +int +getquota(struct thread *td, struct mount *mp, u_long id, int type, void *addr) +{ + struct dqblk64 dqb64; + int error; + + error = _getquota(td, mp, id, type, &dqb64); + if (error) + return (error); + error = copyout(&dqb64, addr, sizeof(dqb64)); + return (error); +} + +int +setquota(struct thread *td, struct mount *mp, u_long id, int type, void *addr) +{ + struct dqblk64 dqb64; + int error; + + error = copyin(addr, &dqb64, sizeof(dqb64)); + if (error) + return (error); + error = _setquota(td, mp, id, type, &dqb64); + return (error); +} + +int +setuse(struct thread *td, struct mount *mp, u_long id, int type, void *addr) +{ + struct dqblk64 dqb64; + int error; + + error = copyin(addr, &dqb64, sizeof(dqb64)); + if (error) + return (error); + error = _setuse(td, mp, id, type, &dqb64); + return (error); +} + /* * Q_SYNC - sync quota files to disk. */ @@ -1025,6 +1123,47 @@ } /* + * Determine the quota file type. + */ +static int +dqopen(struct vnode *vp, struct ufsmount *ump, int type) +{ + struct dqhdr64 dqh; + struct iovec aiov; + struct uio auio; + int error, vfslocked; + + auio.uio_iov = &aiov; + auio.uio_iovcnt = 1; + aiov.iov_base = &dqh; + aiov.iov_len = sizeof(dqh); + auio.uio_resid = sizeof(dqh); + auio.uio_offset = 0; + auio.uio_segflg = UIO_SYSSPACE; + auio.uio_rw = UIO_READ; + auio.uio_td = (struct thread *)0; + vfslocked = VFS_LOCK_GIANT(vp->v_mount); + error = VOP_READ(vp, &auio, 0, ump->um_cred[type]); + VFS_UNLOCK_GIANT(vfslocked); + + if (error != 0) + return (error); + if (auio.uio_resid > 0) { + /* assume 32 bits */ + return (0); + } + + if (strcmp(dqh.dqh_magic, Q_DQHDR64_MAGIC) == 0 && + be32toh(dqh.dqh_version) == Q_DQHDR64_VERSION && + be32toh(dqh.dqh_hdrlen) == (uint32_t)sizeof(struct dqhdr64) && + be32toh(dqh.dqh_reclen) == (uint32_t)sizeof(struct dqblk64)) + ump->um_qflags[type] |= QTF_64BIT; + /* XXX: what if the magic matches, but the sizes are wrong? */ + + return (0); +} + +/* * Obtain a dquot structure for the specified identifier and quota file * reading the information from the file if necessary. */ @@ -1032,6 +1171,8 @@ 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 +1262,7 @@ 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 +1309,52 @@ DQREF(dq); DQH_UNLOCK(); + /* + * Read the requested quota record from the quota file, performing + * any necessary conversions. + * + * The record's offset within the file depends on the size of the + * record, which means we need to know whether it's a 32-bit file + * or a 64-bit file. + * + * Luckily, root's record is always at offset 0, and most of it is + * unused, so we can use it to store a magic number indicating the + * file format. Due to an acute lack of imagination, this magic + * number, stored in the first byte of root's record and hence the + * first byte of the file, is 64. + * + * Another lucky break is that quotaon() always loads root's + * record, to get the default values for dq_btime and dq_itime, so + * we will always have a chance to check the file format before + * being asked for a "real" record. + */ + if (id == 0 || (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 @@ -1281,6 +1453,8 @@ 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; @@ -1327,12 +1501,26 @@ 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; @@ -1345,7 +1533,8 @@ 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); @@ -1384,3 +1573,98 @@ } 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) + +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; +} + +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); +} + +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); +} + +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); +} + +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); +} + +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; +} Index: sys/ufs/ufs/ufsmount.h =================================================================== --- sys/ufs/ufs/ufsmount.h (revision 185617) +++ sys/ufs/ufs/ufsmount.h (working copy) @@ -120,6 +120,7 @@ */ #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)) Index: sys/ufs/ufs/ufs_vfsops.c =================================================================== --- sys/ufs/ufs/ufs_vfsops.c (revision 185617) +++ sys/ufs/ufs/ufs_vfsops.c (working copy) @@ -130,6 +130,18 @@ 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; Index: usr.sbin/edquota/edquota.c =================================================================== --- usr.sbin/edquota/edquota.c (revision 185617) +++ usr.sbin/edquota/edquota.c (working copy) @@ -55,42 +55,59 @@ #include #include #include + #include #include #include #include #include +#include +#include #include #include #include #include #include #include + #include "pathnames.h" -const char *qfname = QUOTAFILENAME; +/* Let's be paranoid about block size */ +#if 10 > DEV_BSHIFT +#define dbtokb(db) \ + ((off_t)(db) >> (10-DEV_BSHIFT)) +#elif 10 < DEV_BSHIFT +#define dbtokb(db) \ + ((off_t)(db) << (DEV_BSHIFT-10)) +#else +#define dbtokb(db) (db) +#endif + const char *qfextension[] = INITQFNAMES; -const char *quotagroup = QUOTAGROUP; char tmpfil[] = _PATH_TMP; +int hflag; struct quotause { struct quotause *next; - long flags; + struct quotafile *qf; struct dqblk dqblk; + int flags; char fsname[MAXPATHLEN + 1]; - char qfname[1]; /* actually longer */ }; #define FOUND 0x01 int alldigits(const char *s); -int cvtatos(time_t, char *, time_t *); -char *cvtstoa(time_t); +int cvtatos(u_int64_t, char *, u_int64_t *); +char *cvtstoa(u_int64_t); +u_int64_t cvtblkval(u_int64_t, char, const char *); +u_int64_t cvtinoval(u_int64_t, char, const char *); int editit(char *); +char *fmthumanvalblks(int64_t); +char *fmthumanvalinos(int64_t); void freeprivs(struct quotause *); int getentry(const char *, int); struct quotause *getprivs(long, int, char *); -int hasquota(struct fstab *, int, char **); -void putprivs(long, int, struct quotause *); +void putprivs(long, struct quotause *); int readprivs(struct quotause *, char *); int readtimes(struct quotause *, char *); static void usage(void); @@ -102,11 +119,10 @@ { struct quotause *qup, *protoprivs, *curprivs; long id, protoid; - long long lim; int i, quotatype, range, tmpfd; uid_t startuid, enduid; - u_int32_t *limp; - char *protoname, *cp, *oldoptarg; + u_int64_t lim; + char *protoname, *cp, *endpt, *oldoptarg; int eflag = 0, tflag = 0, pflag = 0, ch; char *fspath = NULL; char buf[MAXLOGNAME]; @@ -119,18 +135,26 @@ 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 @@ 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 */ + /* Not Reached */ } - 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; - } } - 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 @@ 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 @@ *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; } - putprivs(id, quotatype, protoprivs); + 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); + } } } + 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ quptail = qup; qup->next = 0; } + if (quphead == NULL) { + warnx("No quotas on %s", fspath ? fspath : "any filesystems"); + } endfsent(); return (quphead); } @@ -416,75 +416,13 @@ * 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 @@ err(1, "%s", tmpfil); fprintf(fd, "Quotas for %s %s:\n", qfextension[quotatype], name); for (qup = quplist; qup; qup = qup->next) { - fprintf(fd, "%s: %s %lu, limits (soft = %lu, hard = %lu)\n", - qup->fsname, "kbytes in use:", - (unsigned long)(dbtob(qup->dqblk.dqb_curblocks) / 1024), - (unsigned long)(dbtob(qup->dqblk.dqb_bsoftlimit) / 1024), - (unsigned long)(dbtob(qup->dqblk.dqb_bhardlimit) / 1024)); - fprintf(fd, "%s %lu, limits (soft = %lu, hard = %lu)\n", - "\tinodes in use:", - (unsigned long)qup->dqblk.dqb_curinodes, - (unsigned long)qup->dqblk.dqb_isoftlimit, - (unsigned long)qup->dqblk.dqb_ihardlimit); + fprintf(fd, "%s: in use: %s, ", qup->fsname, + fmthumanvalblks(qup->dqblk.dqb_curblocks)); + fprintf(fd, "limits (soft = %s, ", + fmthumanvalblks(qup->dqblk.dqb_bsoftlimit)); + fprintf(fd, "hard = %s)\n", + fmthumanvalblks(qup->dqblk.dqb_bhardlimit)); + fprintf(fd, "\tinodes in use: %s, ", + fmthumanvalinos(qup->dqblk.dqb_curinodes)); + fprintf(fd, "limits (soft = %s, ", + fmthumanvalinos(qup->dqblk.dqb_isoftlimit)); + fprintf(fd, "hard = %s)\n", + fmthumanvalinos(qup->dqblk.dqb_ihardlimit)); } fclose(fd); return (1); } +char * +fmthumanvalblks(int64_t blocks) +{ + static char numbuf[20]; + + if (hflag) { + humanize_number(numbuf, blocks < 0 ? 7 : 6, + dbtob(blocks), "", HN_AUTOSCALE, HN_NOSPACE); + return (numbuf); + } + snprintf(numbuf, sizeof(numbuf), "%lluk", dbtokb(blocks)); + return(numbuf); +} + +char * +fmthumanvalinos(int64_t inos) +{ + static char numbuf[20]; + + if (hflag) { + humanize_number(numbuf, inos < 0 ? 7 : 6, + inos, "", HN_AUTOSCALE, HN_NOSPACE | HN_DIVISOR_1000); + return (numbuf); + } + snprintf(numbuf, sizeof(numbuf), "%llu", inos); + return(numbuf); +} + /* * Merge changes to an ASCII file into a quotause list. */ @@ -567,8 +535,8 @@ { struct quotause *qup; FILE *fd; - unsigned long bhardlimit, bsoftlimit, curblocks; - unsigned long ihardlimit, isoftlimit, curinodes; + u_int64_t hardlimit, softlimit, curitems; + char hardunits, softunits, curitemunits; int cnt; char *cp; struct dqblk dqblk; @@ -594,29 +562,73 @@ return (0); } cnt = sscanf(cp, - " kbytes in use: %lu, limits (soft = %lu, hard = %lu)", - &curblocks, &bsoftlimit, &bhardlimit); - if (cnt != 3) { + " in use: %llu%c, limits (soft = %llu%c, hard = %llu%c)", + &curitems, &curitemunits, &softlimit, &softunits, + &hardlimit, &hardunits); + /* + * The next three check for old-style input formats. + */ + if (cnt != 6) + cnt = sscanf(cp, + " in use: %llu%c, limits (soft = %llu%c hard = %llu%c", + &curitems, &curitemunits, &softlimit, + &softunits, &hardlimit, &hardunits); + if (cnt != 6) + cnt = sscanf(cp, + " in use: %llu%c, limits (soft = %llu%c hard = %llu%c)", + &curitems, &curitemunits, &softlimit, + &softunits, &hardlimit, &hardunits); + if (cnt != 6) + cnt = sscanf(cp, + " in use: %llu%c, limits (soft = %llu%c, hard = %llu%c", + &curitems, &curitemunits, &softlimit, + &softunits, &hardlimit, &hardunits); + if (cnt != 6) { warnx("%s:%s: bad format", fsp, cp); return (0); } - dqblk.dqb_curblocks = btodb((off_t)curblocks * 1024); - dqblk.dqb_bsoftlimit = btodb((off_t)bsoftlimit * 1024); - dqblk.dqb_bhardlimit = btodb((off_t)bhardlimit * 1024); + dqblk.dqb_curblocks = cvtblkval(curitems, curitemunits, + "current block count"); + dqblk.dqb_bsoftlimit = cvtblkval(softlimit, softunits, + "block soft limit"); + dqblk.dqb_bhardlimit = cvtblkval(hardlimit, hardunits, + "block hard limit"); if ((cp = strtok(line2, "\n")) == NULL) { warnx("%s: %s: bad format", fsp, line2); return (0); } - cnt = sscanf(cp, - "\tinodes in use: %lu, limits (soft = %lu, hard = %lu)", - &curinodes, &isoftlimit, &ihardlimit); - if (cnt != 3) { - warnx("%s: %s: bad format", fsp, line2); + cnt = sscanf(&cp[7], + " in use: %llu%c limits (soft = %llu%c, hard = %llu%c)", + &curitems, &curitemunits, &softlimit, + &softunits, &hardlimit, &hardunits); + /* + * The next three check for old-style input formats. + */ + if (cnt != 6) + cnt = sscanf(&cp[7], + " in use: %llu%c limits (soft = %llu%c hard = %llu%c", + &curitems, &curitemunits, &softlimit, + &softunits, &hardlimit, &hardunits); + if (cnt != 6) + cnt = sscanf(&cp[7], + " in use: %llu%c limits (soft = %llu%c hard = %llu%c)", + &curitems, &curitemunits, &softlimit, + &softunits, &hardlimit, &hardunits); + if (cnt != 6) + cnt = sscanf(&cp[7], + " in use: %llu%c limits (soft = %llu%c, hard = %llu%c", + &curitems, &curitemunits, &softlimit, + &softunits, &hardlimit, &hardunits); + if (cnt != 6) { + warnx("%s: %s: bad format cnt %d", fsp, &cp[7], cnt); return (0); } - dqblk.dqb_curinodes = curinodes; - dqblk.dqb_isoftlimit = isoftlimit; - dqblk.dqb_ihardlimit = ihardlimit; + dqblk.dqb_curinodes = cvtinoval(curitems, curitemunits, + "current inode count"); + dqblk.dqb_isoftlimit = cvtinoval(softlimit, softunits, + "inode soft limit"); + dqblk.dqb_ihardlimit = cvtinoval(hardlimit, hardunits, + "inode hard limit"); for (qup = quplist; qup; qup = qup->next) { if (strcmp(fsp, qup->fsname)) continue; @@ -643,8 +655,10 @@ 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 @@ FILE *fd; int cnt; char *cp; - time_t itime, btime, iseconds, bseconds; - long l_itime, l_btime; + u_int64_t itime, btime, iseconds, bseconds; char *fsp, bunits[10], iunits[10], line1[BUFSIZ]; fd = fopen(inname, "r"); @@ -727,14 +740,12 @@ return (0); } cnt = sscanf(cp, - " block grace period: %ld %s file grace period: %ld %s", - &l_btime, bunits, &l_itime, iunits); + " block grace period: %llu %s file grace period: %llu %s", + &btime, bunits, &itime, iunits); if (cnt != 4) { warnx("%s:%s: bad format", fsp, cp); return (0); } - btime = l_btime; - itime = l_itime; if (cvtatos(btime, bunits, &bseconds) == 0) return (0); if (cvtatos(itime, iunits, &iseconds) == 0) @@ -768,21 +779,21 @@ * Convert seconds to ASCII times. */ char * -cvtstoa(time_t secs) +cvtstoa(u_int64_t secs) { static char buf[20]; if (secs % (24 * 60 * 60) == 0) { secs /= 24 * 60 * 60; - sprintf(buf, "%ld day%s", (long)secs, secs == 1 ? "" : "s"); + sprintf(buf, "%llu day%s", secs, secs == 1 ? "" : "s"); } else if (secs % (60 * 60) == 0) { secs /= 60 * 60; - sprintf(buf, "%ld hour%s", (long)secs, secs == 1 ? "" : "s"); + sprintf(buf, "%llu hour%s", secs, secs == 1 ? "" : "s"); } else if (secs % 60 == 0) { secs /= 60; - sprintf(buf, "%ld minute%s", (long)secs, secs == 1 ? "" : "s"); + sprintf(buf, "%llu minute%s", secs, secs == 1 ? "" : "s"); } else - sprintf(buf, "%ld second%s", (long)secs, secs == 1 ? "" : "s"); + sprintf(buf, "%llu second%s", secs, secs == 1 ? "" : "s"); return (buf); } @@ -790,7 +801,7 @@ * Convert ASCII input times to seconds. */ int -cvtatos(time_t period, char *units, time_t *seconds) +cvtatos(u_int64_t period, char *units, u_int64_t *seconds) { if (bcmp(units, "second", 6) == 0) @@ -802,7 +813,7 @@ else if (bcmp(units, "day", 3) == 0) *seconds = period * 24 * 60 * 60; else { - printf("%s: bad units, specify %s\n", units, + warnx("%s: bad units, specify %s\n", units, "days, hours, minutes, or seconds"); return (0); } @@ -810,6 +821,107 @@ } /* + * Convert a limit to number of disk blocks. + */ +u_int64_t +cvtblkval(u_int64_t limit, char units, const char *itemname) +{ + + switch(units) { + case 'B': + case 'b': + limit = btodb(limit); + break; + case '\0': /* historic behavior */ + case ',': /* historic behavior */ + case ')': /* historic behavior */ + case 'K': + case 'k': + limit *= btodb(1024); + break; + case 'M': + case 'm': + limit *= btodb(1048576); + break; + case 'G': + case 'g': + limit *= btodb(1073741824); + break; + case 'T': + case 't': + limit *= btodb(1099511627776); + break; + case 'P': + case 'p': + limit *= btodb(1125899906842624); + break; + case 'E': + case 'e': + limit *= btodb(1152921504606846976); + break; + case ' ': + errx(2, "No space permitted between value and units for %s\n", + itemname); + break; + default: + errx(2, "%llu%c: unknown units for %s, specify none, K, M, G, T, P, or E\n", + limit, units, itemname); + break; + } + return (limit); +} + +/* + * Convert a limit to number of inodes. + */ +u_int64_t +cvtinoval(u_int64_t limit, char units, const char *itemname) +{ + + switch(units) { + case 'B': + case 'b': + case '\0': /* historic behavior */ + case ',': /* historic behavior */ + case ')': /* historic behavior */ + break; + case 'K': + case 'k': + limit *= 1000; + break; + case 'M': + case 'm': + limit *= 1000000; + break; + case 'G': + case 'g': + limit *= 1000000000; + break; + case 'T': + case 't': + limit *= 1000000000000; + break; + case 'P': + case 'p': + limit *= 1000000000000000; + break; + case 'E': + case 'e': + limit *= 1000000000000000000; + break; + case ' ': + errx(2, "No space permitted between value and units for %s\n", + itemname); + break; + default: + errx(2, "%llu%c: unknown units for %s, specify none, K, M, G, T, P, or E\n", + limit, units, itemname); + break; + } + return (limit); +} + +/* * Free a list of quotause structures. */ void @@ -818,6 +930,7 @@ struct quotause *qup, *nextqup; for (qup = quplist; qup; qup = nextqup) { + quota_close(qup->qf); nextqup = qup->next; free(qup); } @@ -838,53 +951,3 @@ } 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); -} Index: usr.sbin/edquota/edquota.8 =================================================================== --- usr.sbin/edquota/edquota.8 (revision 185617) +++ usr.sbin/edquota/edquota.8 (working copy) @@ -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 @@ 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 @@ .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 Index: usr.sbin/edquota/Makefile =================================================================== --- usr.sbin/edquota/Makefile (revision 185617) +++ usr.sbin/edquota/Makefile (working copy) @@ -4,6 +4,10 @@ PROG= edquota MAN= edquota.8 +CSTD= gnu99 WARNS?= 4 +DPADD= ${LIBUTIL} +LDADD= -lutil + .include Index: lib/libutil/quotafile.3 =================================================================== --- lib/libutil/quotafile.3 (revision 0) +++ lib/libutil/quotafile.3 (revision 197266) @@ -0,0 +1,170 @@ +.\"- +.\" Copyright (c) 2008 Dag-Erling Coïdan Smørgrav +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" $FreeBSD$ +.\" +.Dd February 14, 2009 +.Dt QUOTAFILE 3 +.Os +.Sh NAME +.Nm quota_open +.Nm quota_read +.Nm quota_write_limits +.Nm quota_write_usage +.Nm quota_close +.Nd "Manipulate quotas" +.Sh LIBRARY +.Lb libutil +.Sh SYNOPSIS +.In ufs/ufs/quota.h +.In libutil.h +.In fstab.h +.Ft "struct quotafile *" +.Fn quota_open "struct fstab *fs" "int quotatype" "int openflags" +.Ft int +.Fn quota_read "struct quotafile *qf" "struct dqblk *dqb" "int id" +.Ft int +.Fn quota_write_limits "struct quotafile *qf" "struct dqblk *dqb" "int id" +.Ft int +.Fn quota_write_usage "struct quotafile *qf" "struct dqblk *dqb" "int id" +.Ft int +.Fn quota_close "struct quotafile *qf" +.Sh DESCRIPTION +These functions are designed to simplify access to filesystem quotas. +If quotas are active on a filesystem, +these functions will access them directly from the kernel using the +.Fn quotactl +system call. +If quotas are not active, +these functions will access them by reading and writing +the quota files directly. +.Pp +The +.Fn quota_open +function takes a pointer to an +.Vt fstab +entry corresponding to the filesystem on which quotas +are to be accessed. +The +.Va quotatype +field indicates the type of quotas being sought, either +.Dv USRQUOTA +or +.Dv GRPQUOTA . +The +.Va openflags +are those used by the +.Fn open +system call, usually either +.Dv O_RDONLY +if the quotas are just to be read, or +.Dv O_RDWR +if the quotas are to be updated. +The +.Dv O_CREAT +flag should be specified if a new quota file of the requested type should +be created if it does not already exist. +.Pp +The +.Fn quota_read +function reads the quota from the filesystem and quota type referenced by +.Va qf +for the user (or group) specified by +.Va id +into the +.Vt dqblk +quota structure pointed to by +.Va dqb . +.Pp +The +.Fn quota_write_limits +function updates the limit fields (but not the usage fields) +for the filesystem and quota type referenced by +.Va qf +for the user (or group) specified by +.Va id +from the +.Vt dqblk +quota structure pointed to by +.Va dqb . +.Pp +The +.Fn quota_write_usage +function updates the usage fields (but not the limit fields) +for the filesystem and quota type referenced by +.Va qf +for the user (or group) specified by +.Va id +from the +.Vt dqblk +quota structure pointed to by +.Va dqb . +.Pp +The +.Fn quota_close +function closes any open file descriptors and frees any storage +associated with the filesystem and quota type referenced by +.Va qf . +.Sh RETURN VALUES +If the filesystem has quotas associated with it, +.Fn quota_open +returns a pointer to a +.Vt quotafile +structure used in subsequent quota access calls. +If the filesystem has no quotas, or access permission is denied +.Dv NULL +is returned and +.Va errno +is set to indicate the cause of failure. +.Pp +The +.Fn quota_read , +.Fn quota_write_limits , +.Fn quota_write_usage , +and +.Fn quota_close +functions return zero on success. +On error they return +.Dv -1 +and set +.Va errno +to indicate the cause of failure. +.Sh SEE ALSO +.Xr quotactl 2 , +.Xr quota.user 5 , +.Xr quota.group 5 +.Sh HISTORY +The +.Nm +functions first appeared in +.Fx 8.0 . +.Sh AUTHORS +.An -nosplit +The +.Nm +functions and this manual page were written by +.An Dag-Erling Sm\(/orgrav Aq des@FreeBSD.org +and +.An Marshall Kirk McKusick . Property changes on: lib/libutil/quotafile.3 ___________________________________________________________________ Added: svn:keywords + FreeBSD=%H Added: svn:eol-style + native Index: lib/libutil/quotafile.c =================================================================== --- lib/libutil/quotafile.c (revision 0) +++ lib/libutil/quotafile.c (revision 197266) @@ -0,0 +1,426 @@ +/*- + * Copyright (c) 2008 Dag-Erling Coïdan Smørgrav + * Copyright (c) 2008 Marshall Kirk McKusick + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer + * in this position and unchanged. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +struct quotafile { + int fd; /* -1 means using quotactl for access */ + int wordsize; /* 32-bit or 64-bit limits */ + int quotatype; /* USRQUOTA or GRPQUOTA */ + char fsname[MAXPATHLEN + 1]; /* mount point of filesystem */ + char qfname[MAXPATHLEN + 1]; /* quota file if not using quotactl */ +}; + +static const char *qfextension[] = INITQFNAMES; + +/* + * Check to see if a particular quota is to be enabled. + */ +static int +hasquota(struct fstab *fs, int type, char *qfnamep, int qfbufsize) +{ + char *opt; + char *cp; + struct statfs sfb; + char buf[BUFSIZ]; + static char initname, usrname[100], grpname[100]; + + if (!initname) { + (void)snprintf(usrname, sizeof(usrname), "%s%s", + qfextension[USRQUOTA], QUOTAFILENAME); + (void)snprintf(grpname, sizeof(grpname), "%s%s", + qfextension[GRPQUOTA], QUOTAFILENAME); + initname = 1; + } + strcpy(buf, fs->fs_mntops); + for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) { + if ((cp = index(opt, '='))) + *cp++ = '\0'; + if (type == USRQUOTA && strcmp(opt, usrname) == 0) + break; + if (type == GRPQUOTA && strcmp(opt, grpname) == 0) + break; + } + if (!opt) + return (0); + /* + * Ensure that the filesystem is mounted. + */ + if (statfs(fs->fs_file, &sfb) != 0 || + strcmp(fs->fs_file, sfb.f_mntonname)) { + return (0); + } + if (cp) { + strncpy(qfnamep, cp, qfbufsize); + } else { + (void)snprintf(qfnamep, qfbufsize, "%s/%s.%s", fs->fs_file, + QUOTAFILENAME, qfextension[type]); + } + return (1); +} + +struct quotafile * +quota_open(struct fstab *fs, int quotatype, int openflags) +{ + struct quotafile *qf; + struct dqhdr64 dqh; + struct group *grp; + int qcmd, serrno; + + if ((qf = calloc(1, sizeof(*qf))) == NULL) + return (NULL); + qf->quotatype = quotatype; + strncpy(qf->fsname, fs->fs_file, sizeof(qf->fsname)); + qcmd = QCMD(Q_GETQUOTA, quotatype); + if (quotactl(fs->fs_file, qcmd, 0, &dqh) == 0) { + qf->wordsize = 64; + qf->fd = -1; + return (qf); + } + if (!hasquota(fs, quotatype, qf->qfname, sizeof(qf->qfname))) { + free(qf); + errno = EOPNOTSUPP; + return (NULL); + } + if ((qf->fd = open(qf->qfname, openflags & O_ACCMODE)) < 0 && + (openflags & O_CREAT) == 0) { + serrno = errno; + free(qf); + errno = serrno; + return (NULL); + } + /* File open worked, so process it */ + if (qf->fd != -1) { + qf->wordsize = 32; + switch (read(qf->fd, &dqh, sizeof(dqh))) { + case -1: + serrno = errno; + close(qf->fd); + free(qf); + errno = serrno; + return (NULL); + case sizeof(dqh): + if (strcmp(dqh.dqh_magic, Q_DQHDR64_MAGIC) != 0) { + /* no magic, assume 32 bits */ + qf->wordsize = 32; + return (qf); + } + if (be32toh(dqh.dqh_version) != Q_DQHDR64_VERSION || + be32toh(dqh.dqh_hdrlen) != sizeof(struct dqhdr64) || + be32toh(dqh.dqh_reclen) != sizeof(struct dqblk64)) { + /* correct magic, wrong version / lengths */ + close(qf->fd); + free(qf); + errno = EINVAL; + return (NULL); + } + qf->wordsize = 64; + return (qf); + default: + qf->wordsize = 32; + return (qf); + } + /* not reached */ + } + /* Open failed above, but O_CREAT specified, so create a new file */ + if ((qf->fd = open(qf->qfname, O_RDWR|O_CREAT|O_TRUNC, 0)) < 0) { + serrno = errno; + free(qf); + errno = serrno; + return (NULL); + } + qf->wordsize = 64; + memset(&dqh, 0, sizeof(dqh)); + memcpy(dqh.dqh_magic, Q_DQHDR64_MAGIC, sizeof(dqh.dqh_magic)); + dqh.dqh_version = htobe32(Q_DQHDR64_VERSION); + dqh.dqh_hdrlen = htobe32(sizeof(struct dqhdr64)); + dqh.dqh_reclen = htobe32(sizeof(struct dqblk64)); + if (write(qf->fd, &dqh, sizeof(dqh)) != sizeof(dqh)) { + serrno = errno; + unlink(qf->qfname); + close(qf->fd); + free(qf); + errno = serrno; + return (NULL); + } + grp = getgrnam(QUOTAGROUP); + fchown(qf->fd, 0, grp ? grp->gr_gid : 0); + fchmod(qf->fd, 0640); + return (qf); +} + +void +quota_close(struct quotafile *qf) +{ + + if (qf->fd != -1) + close(qf->fd); + free(qf); +} + +static int +quota_read32(struct quotafile *qf, struct dqblk *dqb, int id) +{ + struct dqblk32 dqb32; + off_t off; + + off = id * sizeof(struct dqblk32); + if (lseek(qf->fd, off, SEEK_SET) == -1) + return (-1); + switch (read(qf->fd, &dqb32, sizeof(dqb32))) { + case 0: + memset(&dqb, 0, sizeof(*dqb)); + return (0); + case sizeof(dqb32): + dqb->dqb_bhardlimit = dqb32.dqb_bhardlimit; + dqb->dqb_bsoftlimit = dqb32.dqb_bsoftlimit; + dqb->dqb_curblocks = dqb32.dqb_curblocks; + dqb->dqb_ihardlimit = dqb32.dqb_ihardlimit; + dqb->dqb_isoftlimit = dqb32.dqb_isoftlimit; + dqb->dqb_curinodes = dqb32.dqb_curinodes; + dqb->dqb_btime = dqb32.dqb_btime; + dqb->dqb_itime = dqb32.dqb_itime; + return (0); + default: + return (-1); + } +} + +static int +quota_read64(struct quotafile *qf, struct dqblk *dqb, int id) +{ + struct dqblk64 dqb64; + off_t off; + + off = sizeof(struct dqhdr64) + id * sizeof(struct dqblk64); + if (lseek(qf->fd, off, SEEK_SET) == -1) + return (-1); + switch (read(qf->fd, &dqb64, sizeof(dqb64))) { + case 0: + memset(&dqb, 0, sizeof(*dqb)); + return (0); + case sizeof(dqb64): + dqb->dqb_bhardlimit = be64toh(dqb64.dqb_bhardlimit); + dqb->dqb_bsoftlimit = be64toh(dqb64.dqb_bsoftlimit); + dqb->dqb_curblocks = be64toh(dqb64.dqb_curblocks); + dqb->dqb_ihardlimit = be64toh(dqb64.dqb_ihardlimit); + dqb->dqb_isoftlimit = be64toh(dqb64.dqb_isoftlimit); + dqb->dqb_curinodes = be64toh(dqb64.dqb_curinodes); + dqb->dqb_btime = be64toh(dqb64.dqb_btime); + dqb->dqb_itime = be64toh(dqb64.dqb_itime); + return (0); + default: + return (-1); + } +} + +int +quota_read(struct quotafile *qf, struct dqblk *dqb, int id) +{ + int qcmd; + + if (qf->fd == -1) { + qcmd = QCMD(Q_GETQUOTA, qf->quotatype); + return (quotactl(qf->fsname, qcmd, id, dqb)); + } + switch (qf->wordsize) { + case 32: + return quota_read32(qf, dqb, id); + case 64: + return quota_read64(qf, dqb, id); + default: + errno = EINVAL; + return (-1); + } + /* not reached */ +} + +#define CLIP32(u64) ((u64) > UINT32_MAX ? UINT32_MAX : (uint32_t)(u64)) + +static int +quota_write32(struct quotafile *qf, const struct dqblk *dqb, int id) +{ + struct dqblk32 dqb32; + off_t off; + + dqb32.dqb_bhardlimit = CLIP32(dqb->dqb_bhardlimit); + dqb32.dqb_bsoftlimit = CLIP32(dqb->dqb_bsoftlimit); + dqb32.dqb_curblocks = CLIP32(dqb->dqb_curblocks); + dqb32.dqb_ihardlimit = CLIP32(dqb->dqb_ihardlimit); + dqb32.dqb_isoftlimit = CLIP32(dqb->dqb_isoftlimit); + dqb32.dqb_curinodes = CLIP32(dqb->dqb_curinodes); + dqb32.dqb_btime = CLIP32(dqb->dqb_btime); + dqb32.dqb_itime = CLIP32(dqb->dqb_itime); + + off = id * sizeof(struct dqblk32); + if (lseek(qf->fd, off, SEEK_SET) == -1) + return (-1); + if (write(qf->fd, &dqb32, sizeof(dqb32)) == sizeof(dqb32)) + return (0); + return (-1); +} + +static int +quota_write64(struct quotafile *qf, const struct dqblk *dqb, int id) +{ + struct dqblk64 dqb64; + off_t off; + + dqb64.dqb_bhardlimit = htobe64(dqb->dqb_bhardlimit); + dqb64.dqb_bsoftlimit = htobe64(dqb->dqb_bsoftlimit); + dqb64.dqb_curblocks = htobe64(dqb->dqb_curblocks); + dqb64.dqb_ihardlimit = htobe64(dqb->dqb_ihardlimit); + dqb64.dqb_isoftlimit = htobe64(dqb->dqb_isoftlimit); + dqb64.dqb_curinodes = htobe64(dqb->dqb_curinodes); + dqb64.dqb_btime = htobe64(dqb->dqb_btime); + dqb64.dqb_itime = htobe64(dqb->dqb_itime); + + off = sizeof(struct dqhdr64) + id * sizeof(struct dqblk64); + if (lseek(qf->fd, off, SEEK_SET) == -1) + return (-1); + if (write(qf->fd, &dqb64, sizeof(dqb64)) == sizeof(dqb64)) + return (0); + return (-1); +} + +int +quota_write_usage(struct quotafile *qf, struct dqblk *dqb, int id) +{ + struct dqblk dqbuf; + int qcmd; + + if (qf->fd == -1) { + qcmd = QCMD(Q_SETUSE, qf->quotatype); + return (quotactl(qf->fsname, qcmd, id, dqb)); + } + /* + * Have to do read-modify-write of quota in file. + */ + if (quota_read(qf, &dqbuf, id) != 0) + return (-1); + /* + * Reset time limit if have a soft limit and were + * previously under it, but are now over it. + */ + if (dqbuf.dqb_bsoftlimit && id != 0 && + dqbuf.dqb_curblocks < dqbuf.dqb_bsoftlimit && + dqb->dqb_curblocks >= dqbuf.dqb_bsoftlimit) + dqbuf.dqb_btime = 0; + if (dqbuf.dqb_isoftlimit && id != 0 && + dqbuf.dqb_curinodes < dqbuf.dqb_isoftlimit && + dqb->dqb_curinodes >= dqbuf.dqb_isoftlimit) + dqbuf.dqb_itime = 0; + dqbuf.dqb_curinodes = dqb->dqb_curinodes; + dqbuf.dqb_curblocks = dqb->dqb_curblocks; + /* + * Write it back. + */ + switch (qf->wordsize) { + case 32: + return quota_write32(qf, &dqbuf, id); + case 64: + return quota_write64(qf, &dqbuf, id); + default: + errno = EINVAL; + return (-1); + } + /* not reached */ +} + +int +quota_write_limits(struct quotafile *qf, struct dqblk *dqb, int id) +{ + struct dqblk dqbuf; + int qcmd; + + if (qf->fd == -1) { + qcmd = QCMD(Q_SETQUOTA, qf->quotatype); + return (quotactl(qf->fsname, qcmd, id, dqb)); + } + /* + * Have to do read-modify-write of quota in file. + */ + if (quota_read(qf, &dqbuf, id) != 0) + return (-1); + /* + * Reset time limit if have a soft limit and were + * previously under it, but are now over it + * or if there previously was no soft limit, but + * now have one and are over it. + */ + if (dqbuf.dqb_bsoftlimit && id != 0 && + dqbuf.dqb_curblocks < dqbuf.dqb_bsoftlimit && + dqbuf.dqb_curblocks >= dqb->dqb_bsoftlimit) + dqb->dqb_btime = 0; + if (dqbuf.dqb_bsoftlimit == 0 && id != 0 && + dqb->dqb_bsoftlimit > 0 && + dqbuf.dqb_curblocks >= dqb->dqb_bsoftlimit) + dqb->dqb_btime = 0; + if (dqbuf.dqb_isoftlimit && id != 0 && + dqbuf.dqb_curinodes < dqbuf.dqb_isoftlimit && + dqbuf.dqb_curinodes >= dqb->dqb_isoftlimit) + dqb->dqb_itime = 0; + if (dqbuf.dqb_isoftlimit == 0 && id !=0 && + dqb->dqb_isoftlimit > 0 && + dqbuf.dqb_curinodes >= dqb->dqb_isoftlimit) + dqb->dqb_itime = 0; + dqb->dqb_curinodes = dqbuf.dqb_curinodes; + dqb->dqb_curblocks = dqbuf.dqb_curblocks; + /* + * Write it back. + */ + switch (qf->wordsize) { + case 32: + return quota_write32(qf, dqb, id); + case 64: + return quota_write64(qf, dqb, id); + default: + errno = EINVAL; + return (-1); + } + /* not reached */ +} Property changes on: lib/libutil/quotafile.c ___________________________________________________________________ Added: svn:keywords + FreeBSD=%H Added: svn:eol-style + native Index: lib/libutil/Makefile =================================================================== --- lib/libutil/Makefile (revision 185617) +++ lib/libutil/Makefile (working copy) @@ -12,8 +12,8 @@ hexdump.c humanize_number.c kinfo_getfile.c kinfo_getvmmap.c kld.c \ login.c login_auth.c login_cap.c \ login_class.c login_crypt.c login_ok.c login_times.c login_tty.c \ - logout.c logwtmp.c pidfile.c property.c pty.c pw_util.c realhostname.c \ - stub.c trimdomain.c uucplock.c + logout.c logwtmp.c pidfile.c property.c pty.c pw_util.c quotafile.c \ + realhostname.c stub.c trimdomain.c uucplock.c INCS= libutil.h login_cap.h WARNS?= 6 @@ -30,7 +30,7 @@ login_cap.3 login_class.3 login_times.3 login_ok.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 + pidfile.3 flopen.3 expand_number.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 @@ -58,5 +58,9 @@ pidfile.3 pidfile_write.3 \ pidfile.3 pidfile_close.3 \ pidfile.3 pidfile_remove.3 +MLINKS+=quotafile.3 quota_open.3 \ + quotafile.3 quota_read.3 \ + quotafile.3 quota_write.3 \ + quotafile.3 quota_close.3 .include Index: lib/libutil/libutil.h =================================================================== --- lib/libutil/libutil.h (revision 185617) +++ lib/libutil/libutil.h (working copy) @@ -140,6 +140,16 @@ int pidfile_remove(struct pidfh *pfh); #endif +#ifdef _UFS_UFS_QUOTA_H_ +struct quotafile; +struct fstab; +struct quotafile *quota_open(struct fstab *, int, int); +void quota_close(struct quotafile *); +int quota_read(struct quotafile *, struct dqblk *, int); +int quota_write_limits(struct quotafile *, struct dqblk *, int); +int quota_write_usage(struct quotafile *, struct dqblk *, int); +#endif + __END_DECLS #define UU_LOCK_INUSE (1) Index: libexec/rpc.rquotad/rquotad.c =================================================================== --- libexec/rpc.rquotad/rquotad.c (revision 185617) +++ libexec/rpc.rquotad/rquotad.c (working copy) @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -247,9 +248,10 @@ int getfsquota(long id, char *path, struct dqblk *dqblk) { + struct quotafile *qf; struct stat st_path; struct fs_stat *fs; - int qcmd, fd, ret = 0; + int qcmd, ret = 0; if (stat(path, &st_path) < 0) return (0); @@ -265,32 +267,16 @@ if (quotactl(fs->fs_file, qcmd, id, dqblk) == 0) return (1); - if ((fd = open(fs->qfpathname, O_RDONLY)) < 0) { + if ((qf = quota_open(fs->qfpathname)) == NULL) { syslog(LOG_ERR, "open error: %s: %m", fs->qfpathname); return (0); } - if (lseek(fd, (off_t)(id * sizeof(struct dqblk)), L_SET) == (off_t)-1) { - close(fd); - return (1); - } - switch (read(fd, dqblk, sizeof(struct dqblk))) { - case 0: - /* - * Convert implicit 0 quota (EOF) - * into an explicit one (zero'ed dqblk) - */ - bzero(dqblk, sizeof(struct dqblk)); - ret = 1; - break; - case sizeof(struct dqblk): /* OK */ - ret = 1; - break; - default: /* ERROR */ + if (quota_read(qf, dqblk, id) != 0) { syslog(LOG_ERR, "read error: %s: %m", fs->qfpathname); - close(fd); + quota_close(qf); return (0); } - close(fd); + quota_close(qf); } return (ret); } Index: libexec/rpc.rquotad/Makefile =================================================================== --- libexec/rpc.rquotad/Makefile (revision 185617) +++ libexec/rpc.rquotad/Makefile (working copy) @@ -6,7 +6,7 @@ WARNS ?= 6 -DPADD= ${LIBRPCSVC} -LDADD= -lrpcsvc +DPADD= ${LIBRPCSVC} ${LIBUTIL} +LDADD= -lrpcsvc -lutil .include