gfs2_quota_chk_ret_allow and gfs2_quota_lck_chk_ret_allow are
variants of gfs2_quota_check and gfs2_quota_lock_check respectively.

If an operation will not succeed due to a quota violation, these
functions will return the number of blocks that quota will actually
allow without failing in an extra parameter 'allow'

If acceptable to the caller logic, any of the quota_check functions
may be called again with the 'allow'ed blocks to try and avoid a
quota violation.

Signed-off-by: Abhi Das <a...@redhat.com>
---
 fs/gfs2/quota.c | 24 ++++++++++++++++++++----
 fs/gfs2/quota.h | 20 +++++++++++++++++---
 2 files changed, 37 insertions(+), 7 deletions(-)

diff --git a/fs/gfs2/quota.c b/fs/gfs2/quota.c
index e2f86ec..98cdf97 100644
--- a/fs/gfs2/quota.c
+++ b/fs/gfs2/quota.c
@@ -1092,8 +1092,20 @@ static int print_message(struct gfs2_quota_data *qd, 
char *type)
 
        return 0;
 }
-
-int gfs2_quota_check(struct gfs2_inode *ip, kuid_t uid, kgid_t gid, s64 
exp_change)
+/**
+ * gfs2_quota_chk_ret_allow - Checks if adding the specified number of 
+ * blocks will exceed usr/grp quotas
+ * @ip: The inode for which this check is being performed
+ * @uid: The uid to check against
+ * @gid: The gid to check against
+ * @exp_change: The expected change in blocks
+ * @allow: If non-NULL, we should return the number of blocks
+ *         quota will allow if exp_change exceeds limits
+ *
+ * Returns: 0 on success, error code otherwise.
+ */
+int gfs2_quota_chk_ret_allow(struct gfs2_inode *ip, kuid_t uid, kgid_t gid,
+                            s64 exp_change, u32 *allow)
 {
        struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
        struct gfs2_quota_data *qd;
@@ -1119,12 +1131,16 @@ int gfs2_quota_check(struct gfs2_inode *ip, kuid_t uid, 
kgid_t gid, s64 exp_chan
                value += qd->qd_change + exp_change;
                spin_unlock(&qd_lock);
 
-               if (be64_to_cpu(qd->qd_qb.qb_limit) && 
(s64)be64_to_cpu(qd->qd_qb.qb_limit) < value) {
+               if (be64_to_cpu(qd->qd_qb.qb_limit) &&
+                   (s64)be64_to_cpu(qd->qd_qb.qb_limit) < value) {
                        print_message(qd, "exceeded");
                        quota_send_warning(qd->qd_id,
                                           sdp->sd_vfs->s_dev, 
QUOTA_NL_BHARDWARN);
-
                        error = -EDQUOT;
+                       if (allow) {
+                               *allow = (s64)be64_to_cpu(qd->qd_qb.qb_limit) -
+                                       (value - exp_change);
+                       }
                        break;
                } else if (be64_to_cpu(qd->qd_qb.qb_warn) &&
                           (s64)be64_to_cpu(qd->qd_qb.qb_warn) < value &&
diff --git a/fs/gfs2/quota.h b/fs/gfs2/quota.h
index 1457c66..49d1fd9 100644
--- a/fs/gfs2/quota.h
+++ b/fs/gfs2/quota.h
@@ -24,7 +24,14 @@ extern void gfs2_quota_unhold(struct gfs2_inode *ip);
 extern int gfs2_quota_lock(struct gfs2_inode *ip, kuid_t uid, kgid_t gid);
 extern void gfs2_quota_unlock(struct gfs2_inode *ip);
 
-extern int gfs2_quota_check(struct gfs2_inode *ip, kuid_t uid, kgid_t gid, s64 
exp_change);
+extern int gfs2_quota_chk_ret_allow(struct gfs2_inode *ip, kuid_t uid,
+                                   kgid_t gid, s64 exp_change, u32 *allow);
+static inline int gfs2_quota_check(struct gfs2_inode *ip, kuid_t uid, kgid_t 
gid,
+                                  s64 exp_change)
+{
+       return gfs2_quota_chk_ret_allow(ip, uid, gid, exp_change, NULL);
+}
+
 extern void gfs2_quota_change(struct gfs2_inode *ip, s64 change,
                              kuid_t uid, kgid_t gid);
 
@@ -37,7 +44,8 @@ extern int gfs2_quotad(void *data);
 
 extern void gfs2_wake_up_statfs(struct gfs2_sbd *sdp);
 
-static inline int gfs2_quota_lock_check(struct gfs2_inode *ip, s64 exp_change)
+static inline int gfs2_quota_lck_chk_ret_allow(struct gfs2_inode *ip,
+                                              s64 exp_change, u32 *allow)
 {
        struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
        int ret;
@@ -48,12 +56,18 @@ static inline int gfs2_quota_lock_check(struct gfs2_inode 
*ip, s64 exp_change)
                return ret;
        if (sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
                return 0;
-       ret = gfs2_quota_check(ip, ip->i_inode.i_uid, ip->i_inode.i_gid, 
exp_change);
+       ret = gfs2_quota_chk_ret_allow(ip, ip->i_inode.i_uid, ip->i_inode.i_gid,
+                                      exp_change, allow);
        if (ret)
                gfs2_quota_unlock(ip);
        return ret;
 }
 
+static inline int gfs2_quota_lock_check(struct gfs2_inode *ip, s64 exp_change)
+{
+       return gfs2_quota_lck_chk_ret_allow(ip, exp_change, NULL);
+}
+
 extern const struct quotactl_ops gfs2_quotactl_ops;
 extern struct shrinker gfs2_qd_shrinker;
 extern struct list_lru gfs2_qd_lru;
-- 
1.8.1.4

Reply via email to