Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/bouyer-quota2]: src factor out code to chech a quota against its limits.



details:   https://anonhg.NetBSD.org/src/rev/d35a7f24b1b7
branches:  bouyer-quota2
changeset: 761107:d35a7f24b1b7
user:      bouyer <bouyer%NetBSD.org@localhost>
date:      Thu Feb 03 15:56:15 2011 +0000

description:
factor out code to chech a quota against its limits.

diffstat:

 sys/ufs/ufs/quota2.h          |   16 +++++-
 sys/ufs/ufs/quota2_subr.c     |   22 +++++++-
 sys/ufs/ufs/ufs_quota2.c      |  109 +++++++++++++++++++++++------------------
 tests/fs/ffs/clients/Makefile |    9 +-
 usr.bin/quota/Makefile        |    4 +-
 usr.bin/quota/quota.c         |   53 +++++++++++---------
 usr.sbin/repquota/Makefile    |    4 +-
 usr.sbin/repquota/repquota.c  |   59 ++++++++++------------
 8 files changed, 159 insertions(+), 117 deletions(-)

diffs (truncated from 487 to 300 lines):

diff -r 861aeb5f5852 -r d35a7f24b1b7 sys/ufs/ufs/quota2.h
--- a/sys/ufs/ufs/quota2.h      Thu Feb 03 15:53:25 2011 +0000
+++ b/sys/ufs/ufs/quota2.h      Thu Feb 03 15:56:15 2011 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: quota2.h,v 1.1.2.5 2011/01/31 15:19:21 bouyer Exp $ */
+/* $NetBSD: quota2.h,v 1.1.2.6 2011/02/03 15:56:16 bouyer Exp $ */
 /*-
   * Copyright (c) 2010 Manuel Bouyer
   * All rights reserved.
@@ -57,7 +57,7 @@
 };
 
 /*
- * Description of a user or group quota
+ * On-disk description of a user or group quota
  * These entries are keept as linked list, either in one of the hash HEAD,
  * or in the free list.
  */
@@ -100,4 +100,16 @@
 void quota2_create_blk0(uint64_t, void *bp, int, int, int);
 void quota2_ufs_rwq2v(const struct quota2_val *, struct quota2_val *, int);
 void quota2_ufs_rwq2e(const struct quota2_entry *, struct quota2_entry *, int);
+
+int quota2_check_limit(struct quota2_val *, uint64_t, time_t);
+#define QL_S_ALLOW_OK  0x00 /* below soft limit */
+#define QL_S_ALLOW_SOFT        0x01 /* over soft limit */
+#define QL_S_DENY_GRACE        0x02 /* over soft limit, grace time expired */
+#define QL_S_DENY_HARD 0x03 /* over hard limit */
+
+#define QL_F_CROSS     0x80 /* crossing soft limit */
+
+#define QL_STATUS(x)   ((x) & 0x0f)
+#define QL_FLAGS(x)    ((x) & 0xf0)
+
 #endif /*  _UFS_UFS_QUOTA2_H_ */
diff -r 861aeb5f5852 -r d35a7f24b1b7 sys/ufs/ufs/quota2_subr.c
--- a/sys/ufs/ufs/quota2_subr.c Thu Feb 03 15:53:25 2011 +0000
+++ b/sys/ufs/ufs/quota2_subr.c Thu Feb 03 15:56:15 2011 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: quota2_subr.c,v 1.1.2.4 2011/01/31 15:24:10 bouyer Exp $ */
+/* $NetBSD: quota2_subr.c,v 1.1.2.5 2011/02/03 15:56:16 bouyer Exp $ */
 /*-
   * Copyright (c) 2010 Manuel Bouyer
   * All rights reserved.
@@ -28,7 +28,7 @@
   */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: quota2_subr.c,v 1.1.2.4 2011/01/31 15:24:10 bouyer Exp $");
+__KERNEL_RCSID(0, "$NetBSD: quota2_subr.c,v 1.1.2.5 2011/02/03 15:56:16 bouyer Exp $");
 
 #include <sys/param.h>
 #include <sys/time.h>
@@ -108,3 +108,21 @@
            needswap);
        d->q2e_uid = ufs_rw32(s->q2e_uid, needswap);
 }
+
+int
+quota2_check_limit(struct quota2_val *q2v, uint64_t change, time_t now)
+{
+       if (q2v->q2v_cur + change >= q2v->q2v_hardlimit) {
+               if (q2v->q2v_cur < q2v->q2v_softlimit) 
+                       return (QL_F_CROSS | QL_S_DENY_HARD);
+               return QL_S_DENY_HARD;
+       } else if (q2v->q2v_cur + change >= q2v->q2v_softlimit) {
+               if (q2v->q2v_cur < q2v->q2v_softlimit)
+                       return (QL_F_CROSS | QL_S_ALLOW_SOFT);
+               if (now > q2v->q2v_time) {
+                       return QL_S_DENY_GRACE;
+               }
+               return QL_S_ALLOW_SOFT;
+       }
+       return QL_S_ALLOW_OK;
+}
diff -r 861aeb5f5852 -r d35a7f24b1b7 sys/ufs/ufs/ufs_quota2.c
--- a/sys/ufs/ufs/ufs_quota2.c  Thu Feb 03 15:53:25 2011 +0000
+++ b/sys/ufs/ufs/ufs_quota2.c  Thu Feb 03 15:56:15 2011 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: ufs_quota2.c,v 1.1.2.6 2011/01/31 15:24:11 bouyer Exp $ */
+/* $NetBSD: ufs_quota2.c,v 1.1.2.7 2011/02/03 15:56:16 bouyer Exp $ */
 /*-
   * Copyright (c) 2010 Manuel Bouyer
   * All rights reserved.
@@ -28,7 +28,7 @@
   */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: ufs_quota2.c,v 1.1.2.6 2011/01/31 15:24:11 bouyer Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ufs_quota2.c,v 1.1.2.7 2011/02/03 15:56:16 bouyer Exp $");
 
 #include <sys/buf.h>
 #include <sys/param.h>
@@ -357,10 +357,11 @@
        int error;
        struct buf *bp[MAXQUOTAS];
        struct quota2_entry *q2e[MAXQUOTAS];
-       struct quota2_val *q2v;
+       struct quota2_val *q2vp;
        struct dquot *dq;
-       uint64_t ncurblks, soft, hard;
+       uint64_t ncurblks;
        struct ufsmount *ump = ip->i_ump;
+       struct mount *mp = ump->um_mountp;
        const int needswap = UFS_MPNEEDSWAP(ump);
        int i;
 
@@ -386,13 +387,13 @@
                                mutex_exit(&dq->dq_interlock);
                                continue;
                        }
-                       q2v = &q2e[i]->q2e_val[vtype];
-                       ncurblks = ufs_rw64(q2v->q2v_cur, needswap);
+                       q2vp = &q2e[i]->q2e_val[vtype];
+                       ncurblks = ufs_rw64(q2vp->q2v_cur, needswap);
                        if (ncurblks < -change)
                                ncurblks = 0;
                        else
                                ncurblks += change;
-                       q2v->q2v_cur = ufs_rw64(ncurblks, needswap);
+                       q2vp->q2v_cur = ufs_rw64(ncurblks, needswap);
                        VOP_BWRITE(bp[i]);
                        mutex_exit(&dq->dq_interlock);
                }
@@ -400,40 +401,61 @@
        }
        /* see if the allocation is allowed */
        for (i = 0; i < MAXQUOTAS; i++) {
-               if ((flags & FORCE) != 0 ||
-                   kauth_authorize_system(cred, KAUTH_SYSTEM_FS_QUOTA,
-                   KAUTH_REQ_SYSTEM_FS_QUOTA_NOLIMIT,
-                   KAUTH_ARG(i), KAUTH_ARG(vtype), NULL) == 0) {
-                       /* don't check this limit */
-                       continue;
-               }
+               struct quota2_val q2v;
+               int ql_stat;
                dq = ip->i_dquot[i];
                if (dq == NODQUOT)
                        continue;
                KASSERT(q2e[i] != NULL);
-               q2v = &q2e[i]->q2e_val[vtype];
-               ncurblks = ufs_rw64(q2v->q2v_cur, needswap);
-               soft = ufs_rw64(q2v->q2v_softlimit, needswap);
-               hard = ufs_rw64(q2v->q2v_hardlimit, needswap);
-               if (ncurblks + change >= hard) {
-                       if ((dq->dq_flags & DQ_WARN(vtype)) == 0) {
-                               uprintf("\n%s: write failed, %s %s limit "
-                                   "reached\n",
-                                   ITOV(ip)->v_mount->mnt_stat.f_mntonname,
-                                   quotatypes[i], valtypes[vtype]);
-                               dq->dq_flags |= DQ_WARN(vtype);
+               quota2_ufs_rwq2v(&q2e[i]->q2e_val[vtype], &q2v, needswap);
+               ql_stat = quota2_check_limit(&q2v, change, time_second);
+
+               if ((flags & FORCE) == 0 &&
+                   kauth_authorize_system(cred, KAUTH_SYSTEM_FS_QUOTA,
+                   KAUTH_REQ_SYSTEM_FS_QUOTA_NOLIMIT,
+                   KAUTH_ARG(i), KAUTH_ARG(vtype), NULL) != 0) {
+                       /* enforce this limit */
+                       switch(QL_STATUS(ql_stat)) {
+                       case QL_S_DENY_HARD:
+                               if ((dq->dq_flags & DQ_WARN(vtype)) == 0) {
+                                       uprintf("\n%s: write failed, %s %s "
+                                           "limit reached\n",
+                                           mp->mnt_stat.f_mntonname,
+                                           quotatypes[i], valtypes[vtype]);
+                                       dq->dq_flags |= DQ_WARN(vtype);
+                               }
+                               error = EDQUOT;
+                               break;
+                       case QL_S_DENY_GRACE:
+                               if ((dq->dq_flags & DQ_WARN(vtype)) == 0) {
+                                       uprintf("\n%s: write failed, %s %s "
+                                           "limit reached\n",
+                                           mp->mnt_stat.f_mntonname,
+                                           quotatypes[i], valtypes[vtype]);
+                                       dq->dq_flags |= DQ_WARN(vtype);
+                               }
+                               error = EDQUOT;
+                               break;
+                       case QL_S_ALLOW_SOFT:
+                               if ((dq->dq_flags & DQ_WARN(vtype)) == 0) {
+                                       uprintf("\n%s: warning, %s %s "
+                                           "quota exceeded\n",
+                                           mp->mnt_stat.f_mntonname,
+                                           quotatypes[i], valtypes[vtype]);
+                                       dq->dq_flags |= DQ_WARN(vtype);
+                               }
+                               break;
                        }
-                       error = EDQUOT;
-               } else if (ncurblks >= soft &&
-                   time_second > ufs_rw64(q2v->q2v_time, needswap)) {
-                       if ((dq->dq_flags & DQ_WARN(vtype)) == 0) {
-                               uprintf("\n%s: write failed, %s %s "
-                                   "limit reached\n",
-                                   ump->um_mountp->mnt_stat.f_mntonname,
-                                   quotatypes[i], valtypes[vtype]);
-                               dq->dq_flags |= DQ_WARN(vtype);
-                       }
-                       error = EDQUOT;
+               }
+               /*
+                * always do this; we don't know if the allocation will
+                * succed or not in the end. if we don't do the allocation
+                * q2v_time will be ignored anyway
+                */
+               if (ql_stat & QL_F_CROSS) {
+                       q2v.q2v_time = time_second + q2v.q2v_grace;
+                       quota2_ufs_rwq2v(&q2v, &q2e[i]->q2e_val[vtype],
+                           needswap);
                }
        }
 
@@ -444,18 +466,9 @@
                        continue;
                KASSERT(q2e[i] != NULL);
                if (error == 0) {
-                       q2v = &q2e[i]->q2e_val[vtype];
-                       ncurblks = ufs_rw64(q2v->q2v_cur, needswap);
-                       soft = ufs_rw64(q2v->q2v_softlimit, needswap);
-                       if (ncurblks < soft && (ncurblks + change) >= soft) {
-                               q2v->q2v_time = ufs_rw64(time_second +
-                                   ufs_rw64(q2v->q2v_grace, needswap),
-                                   needswap);
-                               uprintf("\n%s: warning, %s %s quota exceeded\n",
-                                   ump->um_mountp->mnt_stat.f_mntonname,
-                                   quotatypes[i], valtypes[vtype]);
-                       }
-                       q2v->q2v_cur = ufs_rw64(ncurblks + change, needswap);
+                       q2vp = &q2e[i]->q2e_val[vtype];
+                       ncurblks = ufs_rw64(q2vp->q2v_cur, needswap);
+                       q2vp->q2v_cur = ufs_rw64(ncurblks + change, needswap);
                        VOP_BWRITE(bp[i]);
                } else
                        brelse(bp[i], 0);
diff -r 861aeb5f5852 -r d35a7f24b1b7 tests/fs/ffs/clients/Makefile
--- a/tests/fs/ffs/clients/Makefile     Thu Feb 03 15:53:25 2011 +0000
+++ b/tests/fs/ffs/clients/Makefile     Thu Feb 03 15:56:15 2011 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: Makefile,v 1.1.2.4 2011/02/02 19:17:09 bouyer Exp $
+#      $NetBSD: Makefile,v 1.1.2.5 2011/02/03 15:56:16 bouyer Exp $
 #
 
 .include <bsd.own.mk>
@@ -9,7 +9,7 @@
 PROGS=                 rump_quota rump_repquota rump_edquota rump_quotactl
 
 .PATH: ${NETBSDSRCDIR}/usr.bin/quota
-SRCS.rump_quota=       quota.c printquota.c getvfsquota.c quota_rumpops.c
+SRCS.rump_quota=       quota.c printquota.c getvfsquota.c quota_rumpops.c 
 SRCS.rump_repquota=    printquota.c quota_rumpops.c
 SRCS.rump_edquota=     printquota.c getvfsquota.c quota_rumpops.c
 CPPFLAGS.quota.c+=     -I${NETBSDSRCDIR}/usr.bin/quota
@@ -29,12 +29,13 @@
 CPPFLAGS.quotactl.c+=  -I${NETBSDSRCDIR}/sys
 
 .PATH:  ${NETBSDSRCDIR}/sys/ufs/ufs 
-SRCS.rump_quota+=      quota2_prop.c
-SRCS.rump_repquota+=   quota2_prop.c quota1_subr.c
+SRCS.rump_quota+=      quota2_prop.c quota2_subr.c
+SRCS.rump_repquota+=   quota2_prop.c quota2_subr.c quota1_subr.c
 SRCS.rump_edquota+=    quota2_prop.c quota1_subr.c
 SRCS.rump_quotactl+=   quota2_prop.c
 CPPFLAGS.quota2_prop.c+=-I${NETBSDSRCDIR}/sys
 CPPFLAGS.quota1_subr.c+=-I${NETBSDSRCDIR}/sys
+CPPFLAGS.quota2_subr.c+=-I${NETBSDSRCDIR}/sys
 
 DPADD.rump_quota=      ${LIBRPCSVC} ${LIBPROP} ${LIBRUMPCLIENT}
 LDADD.rump_quota=      -lrpcsvc -lprop -lrumpclient
diff -r 861aeb5f5852 -r d35a7f24b1b7 usr.bin/quota/Makefile
--- a/usr.bin/quota/Makefile    Thu Feb 03 15:53:25 2011 +0000
+++ b/usr.bin/quota/Makefile    Thu Feb 03 15:56:15 2011 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: Makefile,v 1.6.64.3 2011/01/28 22:15:36 bouyer Exp $   
+#      $NetBSD: Makefile,v 1.6.64.4 2011/02/03 15:56:15 bouyer Exp $   
 #      from: @(#)Makefile      8.1 (Berkeley) 6/6/93
 
 .include <bsd.own.mk>
@@ -10,5 +10,5 @@
 LDADD= -lrpcsvc -lprop
 
 .PATH: ${NETBSDSRCDIR}/sys/ufs/ufs 
-SRCS+= quota2_prop.c
+SRCS+= quota2_prop.c quota2_subr.c
 .include <bsd.prog.mk>
diff -r 861aeb5f5852 -r d35a7f24b1b7 usr.bin/quota/quota.c
--- a/usr.bin/quota/quota.c     Thu Feb 03 15:53:25 2011 +0000
+++ b/usr.bin/quota/quota.c     Thu Feb 03 15:56:15 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: quota.c,v 1.33.2.6 2011/01/31 15:26:31 bouyer Exp $    */
+/*     $NetBSD: quota.c,v 1.33.2.7 2011/02/03 15:56:15 bouyer Exp $    */
 
 /*
  * Copyright (c) 1980, 1990, 1993
@@ -42,7 +42,7 @@
 #if 0
 static char sccsid[] = "@(#)quota.c    8.4 (Berkeley) 4/28/95";
 #else



Home | Main Index | Thread Index | Old Index