Whamcloud - gitweb
LU-847 quota: client retrieve quota usage directly
[fs/lustre-release.git] / lustre / quota / quota_ctl.c
index 8a982c1..bce3546 100644 (file)
@@ -26,7 +26,7 @@
  * GPL HEADER END
  */
 /*
- * Copyright  2008 Sun Microsystems, Inc. All rights reserved
+ * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  */
 /*
 #ifdef HAVE_QUOTA_SUPPORT
 #ifdef __KERNEL__
 
-/* When quotaon, build a lqs for every uid/gid who has been set limitation
- * for quota. After quota_search_lqs, it will hold one ref for the lqs.
- * It will be released when qctxt_cleanup() is executed b=18574 */
-void build_lqs(struct obd_device *obd)
-{
-        struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
-        struct list_head id_list;
-        int i, rc;
-
-        INIT_LIST_HEAD(&id_list);
-        for (i = 0; i < MAXQUOTAS; i++) {
-                struct dquot_id *dqid, *tmp;
-
-#ifndef KERNEL_SUPPORTS_QUOTA_READ
-                rc = fsfilt_qids(obd, sb_dqopt(qctxt->lqc_sb)->files[i], NULL,
-                                 i, &id_list);
-#else
-                rc = fsfilt_qids(obd, NULL, sb_dqopt(qctxt->lqc_sb)->files[i],
-                                 i, &id_list);
-#endif
-                if (rc) {
-                        CDEBUG(D_ERROR, "fail to get %s qids!\n",
-                               i ? "group" : "user");
-                        continue;
-                }
-
-                list_for_each_entry_safe(dqid, tmp, &id_list,
-                                         di_link) {
-                        struct lustre_qunit_size *lqs;
-
-                        list_del_init(&dqid->di_link);
-                        lqs = quota_search_lqs(LQS_KEY(i, dqid->di_id),
-                                               qctxt, 1);
-                        if (lqs && !IS_ERR(lqs)) {
-                                lqs->lqs_flags |= dqid->di_flag;
-                                lqs_putref(lqs);
-                        } else {
-                                CDEBUG(D_ERROR, "fail to create a lqs"
-                                       "(%s id: %u)!\n", i ? "group" : "user",
-                                       dqid->di_id);
-                        }
-
-                        OBD_FREE_PTR(dqid);
-                }
-        }
-}
-
 int mds_quota_ctl(struct obd_device *obd, struct obd_export *unused,
                   struct obd_quotactl *oqctl)
 {
@@ -123,13 +76,11 @@ int mds_quota_ctl(struct obd_device *obd, struct obd_export *unused,
         int rc = 0;
         ENTRY;
 
-        do_gettimeofday(&work_start);
+        cfs_gettimeofday(&work_start);
         switch (oqctl->qc_cmd) {
         case Q_QUOTAON:
                 oqctl->qc_id = obt->obt_qfmt; /* override qfmt version */
                 rc = mds_quota_on(obd, oqctl);
-                /* when quotaon, create lqs for every quota uid/gid b=18574 */
-                build_lqs(obd);
                 break;
         case Q_QUOTAOFF:
                 oqctl->qc_id = obt->obt_qfmt; /* override qfmt version */
@@ -168,7 +119,7 @@ int mds_quota_ctl(struct obd_device *obd, struct obd_export *unused,
                 CDEBUG(D_INFO, "mds_quotactl admin quota command %d, id %u, "
                                "type %d, failed: rc = %d\n",
                        oqctl->qc_cmd, oqctl->qc_id, oqctl->qc_type, rc);
-        do_gettimeofday(&work_end);
+        cfs_gettimeofday(&work_end);
         timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
         lprocfs_counter_add(qctxt->lqc_stats, LQUOTA_QUOTA_CTL, timediff);
 
@@ -181,7 +132,7 @@ int filter_quota_ctl(struct obd_device *unused, struct obd_export *exp,
         struct obd_device *obd = exp->exp_obd;
         struct obd_device_target *obt = &obd->u.obt;
         struct lvfs_run_ctxt saved;
-        struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
+        struct lustre_quota_ctxt *qctxt = &obt->obt_qctxt;
         struct lustre_qunit_size *lqs;
         void *handle = NULL;
         struct timeval work_start;
@@ -190,20 +141,19 @@ int filter_quota_ctl(struct obd_device *unused, struct obd_export *exp,
         int rc = 0;
         ENTRY;
 
-        do_gettimeofday(&work_start);
+        cfs_gettimeofday(&work_start);
         switch (oqctl->qc_cmd) {
-        case Q_FINVALIDATE:
         case Q_QUOTAON:
+                oqctl->qc_id = obt->obt_qfmt;
+                rc = generic_quota_on(obd, oqctl, 0);
+                break;
+        case Q_FINVALIDATE:
         case Q_QUOTAOFF:
-                if (!atomic_dec_and_test(&obt->obt_quotachecking)) {
-                        CDEBUG(D_INFO, "other people are doing quotacheck\n");
-                        atomic_inc(&obt->obt_quotachecking);
-                        rc = -EBUSY;
-                        break;
-                }
+                cfs_down(&obt->obt_quotachecking);
                 if (oqctl->qc_cmd == Q_FINVALIDATE &&
                     (obt->obt_qctxt.lqc_flags & UGQUOTA2LQC(oqctl->qc_type))) {
-                        atomic_inc(&obt->obt_quotachecking);
+                        CWARN("quota[%u] is on yet\n", oqctl->qc_type);
+                        cfs_up(&obt->obt_quotachecking);
                         rc = -EBUSY;
                         break;
                 }
@@ -225,18 +175,21 @@ int filter_quota_ctl(struct obd_device *unused, struct obd_export *exp,
                 if (oqctl->qc_stat == QUOTA_RECOVERING)
                         quota_unbarrier(handle);
 
-                if (oqctl->qc_cmd == Q_QUOTAON || oqctl->qc_cmd == Q_QUOTAOFF ||
+                if (oqctl->qc_cmd == Q_QUOTAOFF ||
                     oqctl->qc_cmd == Q_FINVALIDATE) {
-                        if (!rc && oqctl->qc_cmd == Q_QUOTAON)
-                                obt->obt_qctxt.lqc_flags |= UGQUOTA2LQC(oqctl->qc_type);
-                        if (!rc && oqctl->qc_cmd == Q_QUOTAOFF)
-                                obt->obt_qctxt.lqc_flags &= ~UGQUOTA2LQC(oqctl->qc_type);
-                        atomic_inc(&obt->obt_quotachecking);
+                        if (oqctl->qc_cmd == Q_QUOTAOFF) {
+                                if (!rc)
+                                        obt->obt_qctxt.lqc_flags &=
+                                                ~UGQUOTA2LQC(oqctl->qc_type);
+                                else if (quota_is_off(qctxt, oqctl))
+                                                rc = -EALREADY;
+                                CDEBUG(D_QUOTA, "%s: quotaoff type:flags:rc "
+                                       "%u:%lu:%d\n", obd->obd_name,
+                                       oqctl->qc_type, qctxt->lqc_flags, rc);
+                        }
+                        cfs_up(&obt->obt_quotachecking);
                 }
 
-                /* when quotaon, create lqs for every quota uid/gid b=18574 */
-                if (oqctl->qc_cmd == Q_QUOTAON)
-                        build_lqs(obd);
                 break;
         case Q_SETQUOTA:
                 /* currently, it is only used for nullifying the quota */
@@ -256,7 +209,9 @@ int filter_quota_ctl(struct obd_device *unused, struct obd_export *exp,
                 lqs = quota_search_lqs(LQS_KEY(oqctl->qc_type, oqctl->qc_id),
                                        qctxt, 0);
                 if (lqs == NULL || IS_ERR(lqs)){
-                        CDEBUG(D_ERROR, "fail to create lqs when setquota\n");
+                        CERROR("fail to create lqs during setquota operation "
+                               "for %sid %u\n", oqctl->qc_type ? "g" : "u",
+                               oqctl->qc_id);
                 } else {
                         lqs->lqs_flags &= ~QB_SET;
                         lqs_putref(lqs);
@@ -298,7 +253,9 @@ adjust:
                 lqs = quota_search_lqs(LQS_KEY(oqctl->qc_type, oqctl->qc_id),
                                        qctxt, 1);
                 if (lqs == NULL || IS_ERR(lqs)){
-                        CDEBUG(D_ERROR, "fail to create lqs when setquota\n");
+                        CERROR("fail to create lqs during setquota operation "
+                               "for %sid %u\n", oqctl->qc_type ? "g" : "u",
+                               oqctl->qc_id);
                         break;
                 } else {
                         lqs->lqs_flags |= QB_SET;
@@ -325,7 +282,7 @@ adjust:
                        obd->obd_name, oqctl->qc_cmd);
                 RETURN(-EFAULT);
         }
-        do_gettimeofday(&work_end);
+        cfs_gettimeofday(&work_end);
         timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
         lprocfs_counter_add(qctxt->lqc_stats, LQUOTA_QUOTA_CTL, timediff);
 
@@ -363,26 +320,25 @@ int client_quota_ctl(struct obd_device *unused, struct obd_export *exp,
         *oqc = *oqctl;
 
         ptlrpc_request_set_replen(req);
+        ptlrpc_at_set_req_timeout(req);
+        req->rq_no_resend = 1;
 
         rc = ptlrpc_queue_wait(req);
-        if (rc) {
+        if (rc)
                 CERROR("ptlrpc_queue_wait failed, rc: %d\n", rc);
-                GOTO(out, rc);
-        }
-
-        oqc = NULL;
-        if (req->rq_repmsg)
-                oqc = req_capsule_server_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
 
-        if (oqc == NULL) {
+        if (req->rq_repmsg &&
+            (oqc = req_capsule_server_get(&req->rq_pill, &RMF_OBD_QUOTACTL))) {
+                *oqctl = *oqc;
+        } else if (!rc) {
                 CERROR ("Can't unpack obd_quotactl\n");
-                GOTO(out, rc = -EPROTO);
+                rc = -EPROTO;
         }
 
-        *oqctl = *oqc;
         EXIT;
-out:
+
         ptlrpc_req_finished(req);
+
         return rc;
 }
 
@@ -396,7 +352,9 @@ int lmv_quota_ctl(struct obd_device *unused, struct obd_export *exp,
         struct obd_device *obd = class_exp2obd(exp);
         struct lmv_obd *lmv = &obd->u.lmv;
         struct lmv_tgt_desc *tgt = &lmv->tgts[0];
-        int rc;
+        int rc = 0, i;
+        __u64 curspace;
+        __u64 curinodes;
         ENTRY;
 
         if (!lmv->desc.ld_tgt_count || !tgt->ltd_active) {
@@ -404,7 +362,36 @@ int lmv_quota_ctl(struct obd_device *unused, struct obd_export *exp,
                 RETURN(-EIO);
         }
 
-        rc = obd_quotactl(tgt->ltd_exp, oqctl);
+        if (oqctl->qc_cmd != Q_GETOQUOTA) {
+                rc = obd_quotactl(tgt->ltd_exp, oqctl);
+                RETURN(rc);
+        }
+
+        curspace = curinodes = 0;
+        for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
+                int err;
+                tgt = &lmv->tgts[i];
+
+                if (tgt->ltd_exp == NULL)
+                        continue;
+                if (!tgt->ltd_active) {
+                        CDEBUG(D_HA, "mdt %d is inactive.\n", i);
+                        continue;
+                }
+
+                err = obd_quotactl(tgt->ltd_exp, oqctl);
+                if (err) {
+                        CERROR("getquota on mdt %d failed. %d\n", i, err);
+                        if (!rc)
+                                rc = err;
+                } else {
+                        curspace += oqctl->qc_dqblk.dqb_curspace;
+                        curinodes += oqctl->qc_dqblk.dqb_curinodes;
+                }
+        }
+        oqctl->qc_dqblk.dqb_curspace = curspace;
+        oqctl->qc_dqblk.dqb_curinodes = curinodes;
+
         RETURN(rc);
 }
 
@@ -435,8 +422,13 @@ int lov_quota_ctl(struct obd_device *unused, struct obd_export *exp,
                 int err;
 
                 tgt = lov->lov_tgts[i];
-                if (!tgt || !tgt->ltd_active || tgt->ltd_reap) {
-                        if (oqctl->qc_cmd == Q_GETOQUOTA) {
+
+                if (!tgt)
+                        continue;
+
+                if (!tgt->ltd_active || tgt->ltd_reap) {
+                        if (oqctl->qc_cmd == Q_GETOQUOTA &&
+                            lov->lov_tgts[i]->ltd_activate) {
                                 rc = -EREMOTEIO;
                                 CERROR("ost %d is inactive\n", i);
                         } else {