Whamcloud - gitweb
b=20591 fix race on obd_device::md_stats
[fs/lustre-release.git] / lustre / quota / quota_context.c
index fd85997..588a579 100644 (file)
 #include <obd_class.h>
 #include <lustre_quota.h>
 #include <lustre_fsfilt.h>
-#include <class_hash.h>
 #include <lprocfs_status.h>
 #include "quota_internal.h"
 
 #ifdef HAVE_QUOTA_SUPPORT
 
-static lustre_hash_ops_t lqs_hash_ops;
+static cfs_hash_ops_t lqs_hash_ops;
 
 unsigned long default_bunit_sz = 128 * 1024 * 1024; /* 128M bytes */
 unsigned long default_btune_ratio = 50;             /* 50 percentage */
@@ -247,7 +246,7 @@ check_cur_qunit(struct obd_device *obd,
         int ret = 0;
         ENTRY;
 
-        if (!sb_any_quota_enabled(sb))
+        if (!ll_sb_any_quota_active(sb))
                 RETURN(0);
 
         spin_lock(&qctxt->lqc_lock);
@@ -371,7 +370,7 @@ int compute_remquota(struct obd_device *obd, struct lustre_quota_ctxt *qctxt,
         int ret = QUOTA_RET_OK;
         ENTRY;
 
-        if (!sb_any_quota_enabled(sb))
+        if (!ll_sb_any_quota_active(sb))
                 RETURN(QUOTA_RET_NOQUOTA);
 
         /* ignore root user */
@@ -726,10 +725,8 @@ out:
 
         /* this is for dqacq_in_flight() */
         qunit_put(qunit);
-        /* this is for alloc_qunit() */
-        qunit_put(qunit);
         if (rc < 0 && rc != -EDQUOT)
-                 RETURN(err);
+                GOTO(out1, err);
 
         /* don't reschedule in such cases:
          *   - acq/rel failure and qunit isn't changed,
@@ -739,21 +736,21 @@ out:
          */
          OBD_ALLOC_PTR(oqaq);
          if (!oqaq)
-                 RETURN(-ENOMEM);
+                 GOTO(out1, err = -ENOMEM);
          qdata_to_oqaq(qdata, oqaq);
          /* adjust the qunit size in slaves */
          rc1 = quota_adjust_slave_lqs(oqaq, qctxt);
          OBD_FREE_PTR(oqaq);
          if (rc1 < 0) {
                  CERROR("adjust slave's qunit size failed!(rc:%d)\n", rc1);
-                 RETURN(rc1);
+                 GOTO(out1, err = rc1);
          }
          if (err || (rc < 0 && rc != -EBUSY && rc1 == 0) || is_master(qctxt))
-                 RETURN(err);
+                 GOTO(out1, err);
 
          if (opc == QUOTA_DQREL && qdata->qd_count >= 5242880 &&
              OBD_FAIL_CHECK(OBD_FAIL_QUOTA_DELAY_REL))
-                 RETURN(err);
+                 GOTO(out1, err);
 
         /* reschedule another dqacq/dqrel if needed */
         qdata->qd_count = 0;
@@ -765,6 +762,9 @@ out:
                 rc1 = schedule_dqacq(obd, qctxt, qdata, opc, 0, NULL);
                 QDATA_DEBUG(qdata, "reschedudle opc(%d) rc(%d)\n", opc, rc1);
         }
+ out1:
+        /* this is for alloc_qunit() */
+        qunit_put(qunit);
         RETURN(err);
 }
 
@@ -799,7 +799,7 @@ static int dqacq_interpret(const struct lu_env *env,
                 DEBUG_REQ(D_ERROR, req,
                           "error unpacking qunit_data(rc: %ld)\n",
                           PTR_ERR(qdata));
-                RETURN(PTR_ERR(qdata));
+                qdata = &qunit->lq_data;
         }
 
         QDATA_DEBUG(qdata, "qdata: interpret rc(%d).\n", rc);
@@ -830,6 +830,10 @@ static int dqacq_interpret(const struct lu_env *env,
                 rc = -EPROTO;
         }
 
+        if (unlikely(rc == -ESRCH))
+                CERROR("quota for %s has been enabled by master, but disabled "
+                       "by slave.\n", QDATA_IS_GRP(qdata) ? "group" : "user");
+
         rc = dqacq_completion(obd, qctxt, qdata, rc,
                               lustre_msg_get_opc(req->rq_reqmsg));
 
@@ -1234,10 +1238,10 @@ qctxt_init(struct obd_device *obd, dqacq_handler_t handler)
         qctxt->lqc_sync_blk = 0;
         spin_unlock(&qctxt->lqc_lock);
 
-        qctxt->lqc_lqs_hash = lustre_hash_init("LQS_HASH",
-                                               HASH_LQS_CUR_BITS,
-                                               HASH_LQS_MAX_BITS,
-                                               &lqs_hash_ops, 0);
+        qctxt->lqc_lqs_hash = cfs_hash_create("LQS_HASH",
+                                              HASH_LQS_CUR_BITS,
+                                              HASH_LQS_MAX_BITS,
+                                              &lqs_hash_ops, CFS_HASH_REHASH);
         if (!qctxt->lqc_lqs_hash) {
                 CERROR("initialize hash lqs for %s error!\n", obd->obd_name);
                 RETURN(-ENOMEM);
@@ -1312,10 +1316,10 @@ void qctxt_cleanup(struct lustre_quota_ctxt *qctxt, int force)
                                      cfs_time_seconds(1));
         }
 
-        lustre_hash_for_each_safe(qctxt->lqc_lqs_hash, hash_put_lqs, NULL);
+        cfs_hash_for_each_safe(qctxt->lqc_lqs_hash, hash_put_lqs, NULL);
         l_wait_event(qctxt->lqc_lqs_waitq, check_lqs(qctxt), &lwi);
         down_write(&obt->obt_rwsem);
-        lustre_hash_exit(qctxt->lqc_lqs_hash);
+        cfs_hash_destroy(qctxt->lqc_lqs_hash);
         qctxt->lqc_lqs_hash = NULL;
         up_write(&obt->obt_rwsem);
 
@@ -1345,7 +1349,7 @@ static int qslave_recovery_main(void *arg)
         int rc = 0;
         ENTRY;
 
-        ptlrpc_daemonize("qslave_recovd");
+        cfs_daemonize_ctxt("qslave_recovd");
 
         /* for obdfilter */
         class_incref(obd, "qslave_recovd_filter", obd);
@@ -1370,7 +1374,7 @@ static int qslave_recovery_main(void *arg)
                 int ret;
 
                 LOCK_DQONOFF_MUTEX(dqopt);
-                if (!sb_has_quota_enabled(qctxt->lqc_sb, type)) {
+                if (!ll_sb_has_quota_active(qctxt->lqc_sb, type)) {
                         UNLOCK_DQONOFF_MUTEX(dqopt);
                         break;
                 }
@@ -1434,7 +1438,7 @@ qslave_start_recovery(struct obd_device *obd, struct lustre_quota_ctxt *qctxt)
         int rc;
         ENTRY;
 
-        if (!sb_any_quota_enabled(qctxt->lqc_sb))
+        if (!ll_sb_any_quota_active(qctxt->lqc_sb))
                 goto exit;
 
         data.obd = obd;
@@ -1451,6 +1455,87 @@ exit:
         EXIT;
 }
 
+int quota_is_on(struct lustre_quota_ctxt *qctxt, struct obd_quotactl *oqctl)
+{
+        unsigned int type;
+
+        for (type = USRQUOTA; type < MAXQUOTAS; type++) {
+                if (!Q_TYPESET(oqctl, type))
+                        continue;
+                if (!(qctxt->lqc_flags & UGQUOTA2LQC(oqctl->qc_type)))
+                        return 0;
+        }
+        return 1;
+}
+
+int quota_is_off(struct lustre_quota_ctxt *qctxt, struct obd_quotactl *oqctl)
+{
+        unsigned int type;
+
+        for (type = USRQUOTA; type < MAXQUOTAS; type++) {
+                if (!Q_TYPESET(oqctl, type))
+                        continue;
+                if (qctxt->lqc_flags & UGQUOTA2LQC(oqctl->qc_type))
+                        return 0;
+        }
+        return 1;
+}
+
+/**
+ * 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
+ *
+ * Should be called with obt->obt_quotachecking held. b=20152 
+ */
+void build_lqs(struct obd_device *obd)
+{
+        struct obd_device_target *obt = &obd->u.obt;
+        struct lustre_quota_ctxt *qctxt = &obt->obt_qctxt;
+        struct list_head id_list;
+        int i, rc;
+
+        LASSERT_SEM_LOCKED(&obt->obt_quotachecking);
+        INIT_LIST_HEAD(&id_list);
+        for (i = 0; i < MAXQUOTAS; i++) {
+                struct dquot_id *dqid, *tmp;
+
+                if (sb_dqopt(qctxt->lqc_sb)->files[i] == NULL)
+                        continue;
+
+#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);
+                }
+        }
+}
 
 /**
  * lqs<->qctxt hash operations
@@ -1460,7 +1545,7 @@ exit:
  * string hashing using djb2 hash algorithm
  */
 static unsigned
-lqs_hash(lustre_hash_t *lh, void *key, unsigned mask)
+lqs_hash(cfs_hash_t *hs, void *key, unsigned mask)
 {
         struct quota_adjust_qunit *lqs_key;
         unsigned hash;
@@ -1509,7 +1594,7 @@ lqs_put(struct hlist_node *hnode)
                 hlist_entry(hnode, struct lustre_qunit_size, lqs_hash);
         ENTRY;
 
-        __lqs_putref(q, 0);
+        __lqs_putref(q);
 
         RETURN(q);
 }
@@ -1533,11 +1618,11 @@ lqs_exit(struct hlist_node *hnode)
         EXIT;
 }
 
-static lustre_hash_ops_t lqs_hash_ops = {
-        .lh_hash    = lqs_hash,
-        .lh_compare = lqs_compare,
-        .lh_get     = lqs_get,
-        .lh_put     = lqs_put,
-        .lh_exit    = lqs_exit
+static cfs_hash_ops_t lqs_hash_ops = {
+        .hs_hash    = lqs_hash,
+        .hs_compare = lqs_compare,
+        .hs_get     = lqs_get,
+        .hs_put     = lqs_put,
+        .hs_exit    = lqs_exit
 };
 #endif /* HAVE_QUOTA_SUPPORT */