1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/quota/quota_context.c
38 * Lustre Quota Context
40 * Author: Niu YaWei <niu@clusterfs.com>
44 # define EXPORT_SYMTAB
47 #define DEBUG_SUBSYSTEM S_LQUOTA
49 #include <linux/version.h>
51 #include <asm/unistd.h>
52 #include <linux/slab.h>
53 #include <linux/quotaops.h>
54 #include <linux/module.h>
55 #include <linux/init.h>
57 #include <obd_class.h>
58 #include <lustre_quota.h>
59 #include <lustre_fsfilt.h>
60 #include <class_hash.h>
61 #include <lprocfs_status.h>
62 #include "quota_internal.h"
64 #ifdef HAVE_QUOTA_SUPPORT
66 static lustre_hash_ops_t lqs_hash_ops;
68 unsigned long default_bunit_sz = 128 * 1024 * 1024; /* 128M bytes */
69 unsigned long default_btune_ratio = 50; /* 50 percentage */
70 unsigned long default_iunit_sz = 5120; /* 5120 inodes */
71 unsigned long default_itune_ratio = 50; /* 50 percentage */
73 cfs_mem_cache_t *qunit_cachep = NULL;
74 struct list_head qunit_hash[NR_DQHASH];
75 spinlock_t qunit_hash_lock = SPIN_LOCK_UNLOCKED;
77 /* please sync qunit_state with qunit_state_names */
84 * a qunit is added into qunit hash, that means
85 * a quota req will be sent or is flying
89 * a qunit is removed from qunit hash, that
90 * means a quota req is handled and comes back
92 QUNIT_RM_FROM_HASH = 2,
94 * qunit can wake up all threads waiting for it
99 static const char *qunit_state_names[] = {
100 [QUNIT_CREATED] = "CREATED",
101 [QUNIT_IN_HASH] = "IN_HASH",
102 [QUNIT_RM_FROM_HASH] = "RM_FROM_HASH",
103 [QUNIT_FINISHED] = "FINISHED",
106 struct lustre_qunit {
107 struct list_head lq_hash; /** Hash list in memory */
108 atomic_t lq_refcnt; /** Use count */
109 struct lustre_quota_ctxt *lq_ctxt; /** Quota context this applies to */
110 struct qunit_data lq_data; /** See qunit_data */
111 unsigned int lq_opc; /** QUOTA_DQACQ, QUOTA_DQREL */
112 cfs_waitq_t lq_waitq; /** Threads waiting for this qunit */
113 spinlock_t lq_lock; /** Protect the whole structure */
114 enum qunit_state lq_state; /** Present the status of qunit */
115 int lq_rc; /** The rc of lq_data */
119 #define QUNIT_SET_STATE(qunit, state) \
121 spin_lock(&qunit->lq_lock); \
122 QDATA_DEBUG((&qunit->lq_data), "qunit(%p) lq_state(%s->%s), " \
123 "lq_rc(%d), lq_owner(%d)\n", \
124 qunit, qunit_state_names[qunit->lq_state], \
125 qunit_state_names[state], qunit->lq_rc, \
127 qunit->lq_state = state; \
128 spin_unlock(&qunit->lq_lock); \
131 #define QUNIT_SET_STATE_AND_RC(qunit, state, rc) \
133 spin_lock(&qunit->lq_lock); \
135 QDATA_DEBUG((&qunit->lq_data), "qunit(%p) lq_state(%s->%s), " \
136 "lq_rc(%d), lq_owner(%d)\n", \
137 qunit, qunit_state_names[qunit->lq_state], \
138 qunit_state_names[state], qunit->lq_rc, \
140 qunit->lq_state = state; \
141 spin_unlock(&qunit->lq_lock); \
145 int should_translate_quota (struct obd_import *imp)
150 if (imp->imp_connect_data.ocd_connect_flags & OBD_CONNECT_QUOTA64)
156 void qunit_cache_cleanup(void)
161 spin_lock(&qunit_hash_lock);
162 for (i = 0; i < NR_DQHASH; i++)
163 LASSERT(list_empty(qunit_hash + i));
164 spin_unlock(&qunit_hash_lock);
168 rc = cfs_mem_cache_destroy(qunit_cachep);
169 LASSERTF(rc == 0, "couldn't destroy qunit_cache slab\n");
175 int qunit_cache_init(void)
180 LASSERT(qunit_cachep == NULL);
181 qunit_cachep = cfs_mem_cache_create("ll_qunit_cache",
182 sizeof(struct lustre_qunit),
187 spin_lock(&qunit_hash_lock);
188 for (i = 0; i < NR_DQHASH; i++)
189 CFS_INIT_LIST_HEAD(qunit_hash + i);
190 spin_unlock(&qunit_hash_lock);
195 qunit_hashfn(struct lustre_quota_ctxt *qctxt, struct qunit_data *qdata)
196 __attribute__((__const__));
199 qunit_hashfn(struct lustre_quota_ctxt *qctxt, struct qunit_data *qdata)
201 unsigned int id = qdata->qd_id;
202 unsigned int type = QDATA_IS_GRP(qdata);
204 unsigned long tmp = ((unsigned long)qctxt >> L1_CACHE_SHIFT) ^ id;
205 tmp = (tmp * (MAXQUOTAS - type)) % NR_DQHASH;
209 /* caller must hold qunit_hash_lock */
210 static inline struct lustre_qunit *find_qunit(unsigned int hashent,
211 struct lustre_quota_ctxt *qctxt,
212 struct qunit_data *qdata)
214 struct lustre_qunit *qunit = NULL;
215 struct qunit_data *tmp;
217 LASSERT_SPIN_LOCKED(&qunit_hash_lock);
218 list_for_each_entry(qunit, qunit_hash + hashent, lq_hash) {
219 tmp = &qunit->lq_data;
220 if (qunit->lq_ctxt == qctxt &&
221 qdata->qd_id == tmp->qd_id &&
222 (qdata->qd_flags & LQUOTA_QUNIT_FLAGS) ==
223 (tmp->qd_flags & LQUOTA_QUNIT_FLAGS))
229 /* check_cur_qunit - check the current usage of qunit.
230 * @qctxt: quota context
231 * @qdata: the type of quota unit to be checked
233 * return: 1 - need acquire qunit;
234 * 2 - need release qunit;
235 * 0 - need do nothing.
239 check_cur_qunit(struct obd_device *obd,
240 struct lustre_quota_ctxt *qctxt, struct qunit_data *qdata)
242 struct super_block *sb = qctxt->lqc_sb;
243 unsigned long qunit_sz, tune_sz;
244 __u64 usage, limit, limit_org, pending_write = 0;
245 long long record = 0;
246 struct obd_quotactl *qctl;
247 struct lustre_qunit_size *lqs = NULL;
251 if (!sb_any_quota_enabled(sb))
254 spin_lock(&qctxt->lqc_lock);
255 if (!qctxt->lqc_valid){
256 spin_unlock(&qctxt->lqc_lock);
259 spin_unlock(&qctxt->lqc_lock);
265 /* get fs quota usage & limit */
266 qctl->qc_cmd = Q_GETQUOTA;
267 qctl->qc_id = qdata->qd_id;
268 qctl->qc_type = QDATA_IS_GRP(qdata);
269 ret = fsfilt_quotactl(obd, sb, qctl);
271 if (ret == -ESRCH) /* no limit */
274 CERROR("can't get fs quota usage! (rc:%d)\n", ret);
278 if (QDATA_IS_BLK(qdata)) {
279 usage = qctl->qc_dqblk.dqb_curspace;
280 limit = qctl->qc_dqblk.dqb_bhardlimit << QUOTABLOCK_BITS;
282 usage = qctl->qc_dqblk.dqb_curinodes;
283 limit = qctl->qc_dqblk.dqb_ihardlimit;
286 /* ignore the no quota limit case; and it can avoid creating
287 * unnecessary lqs for uid/gid */
291 lqs = quota_search_lqs(LQS_KEY(QDATA_IS_GRP(qdata), qdata->qd_id),
294 GOTO (out, ret = PTR_ERR(lqs));
295 spin_lock(&lqs->lqs_lock);
297 if (QDATA_IS_BLK(qdata)) {
298 qunit_sz = lqs->lqs_bunit_sz;
299 tune_sz = lqs->lqs_btune_sz;
300 pending_write = lqs->lqs_bwrite_pending;
301 record = lqs->lqs_blk_rec;
302 LASSERT(!(qunit_sz % QUOTABLOCK_SIZE));
304 /* we didn't need change inode qunit size now */
305 qunit_sz = lqs->lqs_iunit_sz;
306 tune_sz = lqs->lqs_itune_sz;
307 pending_write = lqs->lqs_iwrite_pending;
308 record = lqs->lqs_ino_rec;
311 /* we don't count the MIN_QLIMIT */
312 if ((limit == MIN_QLIMIT && !QDATA_IS_BLK(qdata)) ||
313 (toqb(limit) == MIN_QLIMIT && QDATA_IS_BLK(qdata)))
316 usage += pending_write;
318 /* when a releasing quota req is sent, before it returned
319 limit is assigned a small value. limit will overflow */
320 if (limit + record < 0)
325 LASSERT(qdata->qd_count == 0);
326 if (limit <= usage + tune_sz) {
327 while (qdata->qd_count + limit <=
329 qdata->qd_count += qunit_sz;
331 } else if (limit > usage + qunit_sz + tune_sz &&
332 limit_org > qdata->qd_count + qunit_sz) {
333 while (limit - qdata->qd_count > usage + qunit_sz + tune_sz &&
334 limit_org > qdata->qd_count + qunit_sz)
335 qdata->qd_count += qunit_sz;
337 /* if there are other pending writes for this uid/gid, releasing
338 * quota is put off until the last pending write b=16645 */
339 if (ret == 2 && pending_write) {
340 CDEBUG(D_QUOTA, "delay quota release\n");
344 CDEBUG(D_QUOTA, "type: %c, limit: "LPU64", usage: "LPU64
345 ", pending_write: "LPU64", record: "LPD64
346 ", qunit_sz: %lu, tune_sz: %lu, ret: %d.\n",
347 QDATA_IS_BLK(qdata) ? 'b' : 'i', limit, usage, pending_write,
348 record, qunit_sz, tune_sz, ret);
349 LASSERT(ret == 0 || qdata->qd_count);
351 spin_unlock(&lqs->lqs_lock);
360 * Compute the remaining quota for certain gid or uid b=11693
362 int compute_remquota(struct obd_device *obd, struct lustre_quota_ctxt *qctxt,
363 struct qunit_data *qdata, int isblk)
365 struct super_block *sb = qctxt->lqc_sb;
367 struct obd_quotactl *qctl;
368 int ret = QUOTA_RET_OK;
371 if (!sb_any_quota_enabled(sb))
372 RETURN(QUOTA_RET_NOQUOTA);
374 /* ignore root user */
375 if (qdata->qd_id == 0 && QDATA_IS_GRP(qdata) == USRQUOTA)
376 RETURN(QUOTA_RET_NOLIMIT);
382 /* get fs quota usage & limit */
383 qctl->qc_cmd = Q_GETQUOTA;
384 qctl->qc_id = qdata->qd_id;
385 qctl->qc_type = QDATA_IS_GRP(qdata);
386 ret = fsfilt_quotactl(obd, sb, qctl);
388 if (ret == -ESRCH) /* no limit */
389 ret = QUOTA_RET_NOLIMIT;
391 CDEBUG(D_QUOTA, "can't get fs quota usage! (rc:%d)",
396 usage = isblk ? qctl->qc_dqblk.dqb_curspace :
397 qctl->qc_dqblk.dqb_curinodes;
398 limit = isblk ? qctl->qc_dqblk.dqb_bhardlimit << QUOTABLOCK_BITS :
399 qctl->qc_dqblk.dqb_ihardlimit;
400 if (!limit){ /* no limit */
401 ret = QUOTA_RET_NOLIMIT;
406 qdata->qd_count = limit - usage;
415 static struct lustre_qunit *alloc_qunit(struct lustre_quota_ctxt *qctxt,
416 struct qunit_data *qdata, int opc)
418 struct lustre_qunit *qunit = NULL;
421 OBD_SLAB_ALLOC_PTR_GFP(qunit, qunit_cachep, CFS_ALLOC_IO);
425 CFS_INIT_LIST_HEAD(&qunit->lq_hash);
426 init_waitqueue_head(&qunit->lq_waitq);
427 atomic_set(&qunit->lq_refcnt, 1);
428 qunit->lq_ctxt = qctxt;
429 memcpy(&qunit->lq_data, qdata, sizeof(*qdata));
431 qunit->lq_lock = SPIN_LOCK_UNLOCKED;
432 QUNIT_SET_STATE_AND_RC(qunit, QUNIT_CREATED, 0);
433 qunit->lq_owner = cfs_curproc_pid();
437 static inline void free_qunit(struct lustre_qunit *qunit)
439 OBD_SLAB_FREE(qunit, qunit_cachep, sizeof(*qunit));
442 static inline void qunit_get(struct lustre_qunit *qunit)
444 atomic_inc(&qunit->lq_refcnt);
447 static void qunit_put(struct lustre_qunit *qunit)
449 LASSERT(atomic_read(&qunit->lq_refcnt));
450 if (atomic_dec_and_test(&qunit->lq_refcnt))
454 /* caller must hold qunit_hash_lock and release ref of qunit after using it */
455 static struct lustre_qunit *dqacq_in_flight(struct lustre_quota_ctxt *qctxt,
456 struct qunit_data *qdata)
458 unsigned int hashent = qunit_hashfn(qctxt, qdata);
459 struct lustre_qunit *qunit;
462 LASSERT_SPIN_LOCKED(&qunit_hash_lock);
463 qunit = find_qunit(hashent, qctxt, qdata);
470 insert_qunit_nolock(struct lustre_quota_ctxt *qctxt, struct lustre_qunit *qunit)
472 struct list_head *head;
474 LASSERT(list_empty(&qunit->lq_hash));
476 head = qunit_hash + qunit_hashfn(qctxt, &qunit->lq_data);
477 list_add(&qunit->lq_hash, head);
478 QUNIT_SET_STATE(qunit, QUNIT_IN_HASH);
481 static void compute_lqs_after_removing_qunit(struct lustre_qunit *qunit)
483 struct lustre_qunit_size *lqs;
485 lqs = quota_search_lqs(LQS_KEY(QDATA_IS_GRP(&qunit->lq_data),
486 qunit->lq_data.qd_id),
488 if (lqs && !IS_ERR(lqs)) {
489 spin_lock(&lqs->lqs_lock);
490 if (qunit->lq_opc == QUOTA_DQACQ)
491 quota_compute_lqs(&qunit->lq_data, lqs, 0, 1);
492 if (qunit->lq_opc == QUOTA_DQREL)
493 quota_compute_lqs(&qunit->lq_data, lqs, 0, 0);
494 spin_unlock(&lqs->lqs_lock);
495 /* this is for quota_search_lqs */
497 /* this is for schedule_dqacq */
502 static void remove_qunit_nolock(struct lustre_qunit *qunit)
504 LASSERT(!list_empty(&qunit->lq_hash));
505 LASSERT_SPIN_LOCKED(&qunit_hash_lock);
507 list_del_init(&qunit->lq_hash);
508 QUNIT_SET_STATE(qunit, QUNIT_RM_FROM_HASH);
512 #define INC_QLIMIT(limit, count) (limit == MIN_QLIMIT) ? \
513 (limit = count) : (limit += count)
516 static inline int is_master(struct lustre_quota_ctxt *qctxt)
518 return qctxt->lqc_handler ? 1 : 0;
522 schedule_dqacq(struct obd_device *obd, struct lustre_quota_ctxt *qctxt,
523 struct qunit_data *qdata, int opc, int wait,
524 struct obd_trans_info *oti);
526 static inline void qdata_to_oqaq(struct qunit_data *qdata,
527 struct quota_adjust_qunit *oqaq)
532 oqaq->qaq_flags = qdata->qd_flags;
533 oqaq->qaq_id = qdata->qd_id;
534 if (QDATA_IS_ADJBLK(qdata))
535 oqaq->qaq_bunit_sz = qdata->qd_qunit;
536 if (QDATA_IS_ADJINO(qdata))
537 oqaq->qaq_iunit_sz = qdata->qd_qunit;
541 dqacq_completion(struct obd_device *obd, struct lustre_quota_ctxt *qctxt,
542 struct qunit_data *qdata, int rc, int opc)
544 struct lustre_qunit *qunit = NULL;
545 struct super_block *sb = qctxt->lqc_sb;
547 struct quota_adjust_qunit *oqaq = NULL;
552 QDATA_DEBUG(qdata, "obd(%s): complete %s quota req\n",
553 obd->obd_name, (opc == QUOTA_DQACQ) ? "acq" : "rel");
555 /* do it only when a releasing quota req more than 5MB b=18491 */
556 if (opc == QUOTA_DQREL && qdata->qd_count >= 5242880)
557 OBD_FAIL_TIMEOUT(OBD_FAIL_QUOTA_DELAY_REL, 5);
559 /* update local operational quota file */
561 __u64 count = QUSG(qdata->qd_count, QDATA_IS_BLK(qdata));
562 struct obd_quotactl *qctl;
567 GOTO(out, err = -ENOMEM);
569 /* acq/rel qunit for specified uid/gid is serialized,
570 * so there is no race between get fs quota limit and
571 * set fs quota limit */
572 qctl->qc_cmd = Q_GETQUOTA;
573 qctl->qc_id = qdata->qd_id;
574 qctl->qc_type = QDATA_IS_GRP(qdata);
575 err = fsfilt_quotactl(obd, sb, qctl);
577 CERROR("error get quota fs limit! (rc:%d)\n", err);
581 if (QDATA_IS_BLK(qdata)) {
582 qctl->qc_dqblk.dqb_valid = QIF_BLIMITS;
583 hardlimit = &qctl->qc_dqblk.dqb_bhardlimit;
585 qctl->qc_dqblk.dqb_valid = QIF_ILIMITS;
586 hardlimit = &qctl->qc_dqblk.dqb_ihardlimit;
589 CDEBUG(D_QUOTA, "hardlimt: "LPU64"\n", *hardlimit);
596 INC_QLIMIT(*hardlimit, count);
599 LASSERTF(count < *hardlimit,
600 "id(%u) flag(%u) type(%c) isblk(%c) "
601 "count("LPU64") qd_qunit("LPU64") "
602 "hardlimit("LPU64").\n",
603 qdata->qd_id, qdata->qd_flags,
604 QDATA_IS_GRP(qdata) ? 'g' : 'u',
605 QDATA_IS_BLK(qdata) ? 'b': 'i',
606 qdata->qd_count, qdata->qd_qunit, *hardlimit);
613 /* clear quota limit */
617 qctl->qc_cmd = Q_SETQUOTA;
618 err = fsfilt_quotactl(obd, sb, qctl);
620 CERROR("error set quota fs limit! (rc:%d)\n", err);
622 QDATA_DEBUG(qdata, "%s completion\n",
623 opc == QUOTA_DQACQ ? "DQACQ" : "DQREL");
626 } else if (rc == -EDQUOT) {
627 QDATA_DEBUG(qdata, "acquire qunit got EDQUOT.\n");
628 } else if (rc == -EBUSY) {
629 QDATA_DEBUG(qdata, "it's is recovering, got EBUSY.\n");
631 CERROR("acquire qunit got error! (rc:%d)\n", rc);
634 /* remove the qunit from hash */
635 spin_lock(&qunit_hash_lock);
637 qunit = dqacq_in_flight(qctxt, qdata);
638 /* this qunit has been removed by qctxt_cleanup() */
640 spin_unlock(&qunit_hash_lock);
641 QDATA_DEBUG(qdata, "%s is discarded because qunit isn't found\n",
642 opc == QUOTA_DQACQ ? "DQACQ" : "DQREL");
646 LASSERT(opc == qunit->lq_opc);
647 /* remove this qunit from lq_hash so that new processes cannot be added
648 * to qunit->lq_waiters */
649 remove_qunit_nolock(qunit);
650 spin_unlock(&qunit_hash_lock);
652 compute_lqs_after_removing_qunit(qunit);
655 rc = QUOTA_REQ_RETURNED;
656 QUNIT_SET_STATE_AND_RC(qunit, QUNIT_FINISHED, rc);
657 /* wake up all waiters */
658 wake_up_all(&qunit->lq_waitq);
660 /* this is for dqacq_in_flight() */
662 /* this is for alloc_qunit() */
664 if (rc < 0 && rc != -EDQUOT)
667 /* don't reschedule in such cases:
668 * - acq/rel failure and qunit isn't changed,
669 * but not for quota recovery.
670 * - local dqacq/dqrel.
671 * - local disk io failure.
676 qdata_to_oqaq(qdata, oqaq);
677 /* adjust the qunit size in slaves */
678 rc1 = quota_adjust_slave_lqs(oqaq, qctxt);
681 CERROR("adjust slave's qunit size failed!(rc:%d)\n", rc1);
684 if (err || (rc < 0 && rc != -EBUSY && rc1 == 0) || is_master(qctxt))
687 if (opc == QUOTA_DQREL && qdata->qd_count >= 5242880 &&
688 OBD_FAIL_CHECK(OBD_FAIL_QUOTA_DELAY_REL))
691 /* reschedule another dqacq/dqrel if needed */
693 qdata->qd_flags &= LQUOTA_QUNIT_FLAGS;
694 rc1 = check_cur_qunit(obd, qctxt, qdata);
697 opc = rc1 == 1 ? QUOTA_DQACQ : QUOTA_DQREL;
698 rc1 = schedule_dqacq(obd, qctxt, qdata, opc, 0, NULL);
699 QDATA_DEBUG(qdata, "reschedudle opc(%d) rc(%d)\n", opc, rc1);
704 struct dqacq_async_args {
705 struct lustre_quota_ctxt *aa_ctxt;
706 struct lustre_qunit *aa_qunit;
709 static int dqacq_interpret(const struct lu_env *env,
710 struct ptlrpc_request *req, void *data, int rc)
712 struct dqacq_async_args *aa = (struct dqacq_async_args *)data;
713 struct lustre_quota_ctxt *qctxt = aa->aa_ctxt;
714 struct obd_device_target *obt = qctxt->lqc_obt;
715 struct lustre_qunit *qunit = aa->aa_qunit;
716 struct obd_device *obd = req->rq_import->imp_obd;
717 struct qunit_data *qdata = NULL;
721 LASSERT(req->rq_import);
723 down_read(&obt->obt_rwsem);
724 /* if a quota req timeouts or is dropped, we should update quota
725 * statistics which will be handled in dqacq_completion. And in
726 * this situation we should get qdata from request instead of
728 qdata = quota_get_qdata(req, (rc != 0) ? QUOTA_REQUEST : QUOTA_REPLY,
732 DEBUG_REQ(D_ERROR, req,
733 "error unpacking qunit_data(rc: %ld)\n",
735 RETURN(PTR_ERR(qdata));
738 QDATA_DEBUG(qdata, "qdata: interpret rc(%d).\n", rc);
739 QDATA_DEBUG((&qunit->lq_data), "lq_data: \n");
741 if (qdata->qd_id != qunit->lq_data.qd_id ||
742 OBD_FAIL_CHECK(OBD_FAIL_QUOTA_RET_QDATA)) {
743 CDEBUG(D_ERROR, "the returned qd_id isn't expected!"
744 "(qdata: %u, lq_data: %u)\n", qdata->qd_id,
745 qunit->lq_data.qd_id);
746 qdata->qd_id = qunit->lq_data.qd_id;
749 if (QDATA_IS_GRP(qdata) != QDATA_IS_GRP(&qunit->lq_data)) {
750 CDEBUG(D_ERROR, "the returned grp/usr isn't expected!"
751 "(qdata: %u, lq_data: %u)\n", qdata->qd_flags,
752 qunit->lq_data.qd_flags);
753 if (QDATA_IS_GRP(&qunit->lq_data))
754 QDATA_SET_GRP(qdata);
756 QDATA_CLR_GRP(qdata);
759 if (qdata->qd_count > qunit->lq_data.qd_count) {
760 CDEBUG(D_ERROR, "the returned qd_count isn't expected!"
761 "(qdata: "LPU64", lq_data: "LPU64")\n", qdata->qd_count,
762 qunit->lq_data.qd_count);
766 rc = dqacq_completion(obd, qctxt, qdata, rc,
767 lustre_msg_get_opc(req->rq_reqmsg));
769 up_read(&obt->obt_rwsem);
774 * check if quota master is online
776 int check_qm(struct lustre_quota_ctxt *qctxt)
781 spin_lock(&qctxt->lqc_lock);
782 /* quit waiting when mds is back or qctxt is cleaned up */
783 rc = qctxt->lqc_import || !qctxt->lqc_valid;
784 spin_unlock(&qctxt->lqc_lock);
789 /* wake up all waiting threads when lqc_import is NULL */
790 void dqacq_interrupt(struct lustre_quota_ctxt *qctxt)
792 struct lustre_qunit *qunit, *tmp;
796 spin_lock(&qunit_hash_lock);
797 for (i = 0; i < NR_DQHASH; i++) {
798 list_for_each_entry_safe(qunit, tmp, &qunit_hash[i], lq_hash) {
799 if (qunit->lq_ctxt != qctxt)
802 /* Wake up all waiters. Do not change lq_state.
803 * The waiters will check lq_rc which is kept as 0
804 * if no others change it, then the waiters will return
805 * -EAGAIN to caller who can perform related quota
806 * acq/rel if necessary. */
807 wake_up_all(&qunit->lq_waitq);
810 spin_unlock(&qunit_hash_lock);
814 static int got_qunit(struct lustre_qunit *qunit, int is_master)
816 struct lustre_quota_ctxt *qctxt = qunit->lq_ctxt;
820 spin_lock(&qunit->lq_lock);
821 switch (qunit->lq_state) {
823 case QUNIT_RM_FROM_HASH:
829 CERROR("invalid qunit state %d\n", qunit->lq_state);
831 spin_unlock(&qunit->lq_lock);
834 spin_lock(&qctxt->lqc_lock);
835 rc = !qctxt->lqc_valid;
837 rc |= !qctxt->lqc_import;
838 spin_unlock(&qctxt->lqc_lock);
845 schedule_dqacq(struct obd_device *obd, struct lustre_quota_ctxt *qctxt,
846 struct qunit_data *qdata, int opc, int wait,
847 struct obd_trans_info *oti)
849 struct lustre_qunit *qunit, *empty;
850 struct l_wait_info lwi = { 0 };
851 struct ptlrpc_request *req;
852 struct dqacq_async_args *aa;
853 struct obd_import *imp = NULL;
854 struct lustre_qunit_size *lqs = NULL;
855 struct timeval work_start;
856 struct timeval work_end;
861 LASSERT(opc == QUOTA_DQACQ || opc == QUOTA_DQREL);
862 do_gettimeofday(&work_start);
863 if ((empty = alloc_qunit(qctxt, qdata, opc)) == NULL)
866 spin_lock(&qunit_hash_lock);
867 qunit = dqacq_in_flight(qctxt, qdata);
869 spin_unlock(&qunit_hash_lock);
872 goto wait_completion;
876 insert_qunit_nolock(qctxt, qunit);
877 spin_unlock(&qunit_hash_lock);
879 lqs = quota_search_lqs(LQS_KEY(QDATA_IS_GRP(qdata), qdata->qd_id),
881 if (lqs && !IS_ERR(lqs)) {
882 spin_lock(&lqs->lqs_lock);
883 quota_compute_lqs(qdata, lqs, 1, (opc == QUOTA_DQACQ) ? 1 : 0);
884 /* when this qdata returned from mds, it will call lqs_putref */
886 spin_unlock(&lqs->lqs_lock);
887 /* this is for quota_search_lqs */
890 CDEBUG(D_ERROR, "Can't find the lustre qunit size!\n");
893 QDATA_DEBUG(qdata, "obd(%s): send %s quota req\n",
894 obd->obd_name, (opc == QUOTA_DQACQ) ? "acq" : "rel");
895 /* master is going to dqacq/dqrel from itself */
896 if (is_master(qctxt)) {
898 QDATA_DEBUG(qdata, "local %s.\n",
899 opc == QUOTA_DQACQ ? "DQACQ" : "DQREL");
900 QDATA_SET_CHANGE_QS(qdata);
901 rc = qctxt->lqc_handler(obd, qdata, opc);
902 rc2 = dqacq_completion(obd, qctxt, qdata, rc, opc);
903 /* this is for qunit_get() */
906 do_gettimeofday(&work_end);
907 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
908 if (opc == QUOTA_DQACQ)
909 lprocfs_counter_add(qctxt->lqc_stats,
910 wait ? LQUOTA_SYNC_ACQ : LQUOTA_ASYNC_ACQ,
913 lprocfs_counter_add(qctxt->lqc_stats,
914 wait ? LQUOTA_SYNC_REL : LQUOTA_ASYNC_REL,
916 RETURN(rc ? rc : rc2);
919 spin_lock(&qctxt->lqc_lock);
920 if (!qctxt->lqc_import) {
921 spin_unlock(&qctxt->lqc_lock);
922 QDATA_DEBUG(qdata, "lqc_import is invalid.\n");
924 spin_lock(&qunit_hash_lock);
925 remove_qunit_nolock(qunit);
926 spin_unlock(&qunit_hash_lock);
928 compute_lqs_after_removing_qunit(qunit);
930 QUNIT_SET_STATE_AND_RC(qunit, QUNIT_FINISHED, -EAGAIN);
931 wake_up_all(&qunit->lq_waitq);
933 /* this is for qunit_get() */
935 /* this for alloc_qunit() */
937 spin_lock(&qctxt->lqc_lock);
938 if (wait && !qctxt->lqc_import) {
939 spin_unlock(&qctxt->lqc_lock);
941 LASSERT(oti && oti->oti_thread &&
942 oti->oti_thread->t_watchdog);
944 lc_watchdog_disable(oti->oti_thread->t_watchdog);
945 CDEBUG(D_QUOTA, "sleep for quota master\n");
946 l_wait_event(qctxt->lqc_wait_for_qmaster,
947 check_qm(qctxt), &lwi);
948 CDEBUG(D_QUOTA, "wake up when quota master is back\n");
949 lc_watchdog_touch(oti->oti_thread->t_watchdog,
950 GET_TIMEOUT(oti->oti_thread->t_svc));
952 spin_unlock(&qctxt->lqc_lock);
957 imp = class_import_get(qctxt->lqc_import);
958 spin_unlock(&qctxt->lqc_lock);
960 /* build dqacq/dqrel request */
963 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_QUOTA_DQACQ,
964 LUSTRE_MDS_VERSION, opc);
965 class_import_put(imp);
967 CDEBUG(D_ERROR, "Can't alloc request\n");
968 dqacq_completion(obd, qctxt, qdata, -ENOMEM, opc);
969 /* this is for qunit_get() */
974 ptlrpc_request_set_replen(req);
975 req->rq_no_resend = req->rq_no_delay = 1;
976 rc = quota_copy_qdata(req, qdata, QUOTA_REQUEST, QUOTA_IMPORT);
978 CDEBUG(D_ERROR, "Can't pack qunit_data(rc: %d)\n", rc);
979 ptlrpc_req_finished(req);
980 dqacq_completion(obd, qctxt, qdata, -EPROTO, opc);
981 /* this is for qunit_get() */
986 CLASSERT(sizeof(*aa) <= sizeof(req->rq_async_args));
987 aa = ptlrpc_req_async_args(req);
989 aa->aa_qunit = qunit;
991 req->rq_interpret_reply = dqacq_interpret;
992 ptlrpcd_add_req(req, PSCOPE_OTHER);
994 QDATA_DEBUG(qdata, "%s scheduled.\n",
995 opc == QUOTA_DQACQ ? "DQACQ" : "DQREL");
998 struct qunit_data *p = &qunit->lq_data;
1000 QDATA_DEBUG(p, "qunit(%p) is waiting for dqacq.\n", qunit);
1001 l_wait_event(qunit->lq_waitq, got_qunit(qunit, is_master(qctxt)),
1003 /* rc = -EAGAIN, it means the quota master isn't ready yet
1004 * rc = QUOTA_REQ_RETURNED, it means a quota req is finished;
1005 * rc = -EDQUOT, it means out of quota
1006 * rc = -EBUSY, it means recovery is happening
1007 * other rc < 0, it means real errors, functions who call
1008 * schedule_dqacq should take care of this */
1009 spin_lock(&qunit->lq_lock);
1011 spin_unlock(&qunit->lq_lock);
1012 CDEBUG(D_QUOTA, "qunit(%p) finishes waiting: id(%u) flag(%u) "
1013 "rc(%d) owner(%d)\n", qunit, qunit->lq_data.qd_id,
1014 qunit->lq_data.qd_flags, rc, qunit->lq_owner);
1018 do_gettimeofday(&work_end);
1019 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
1020 if (opc == QUOTA_DQACQ)
1021 lprocfs_counter_add(qctxt->lqc_stats,
1022 wait ? LQUOTA_SYNC_ACQ : LQUOTA_ASYNC_ACQ,
1025 lprocfs_counter_add(qctxt->lqc_stats,
1026 wait ? LQUOTA_SYNC_REL : LQUOTA_ASYNC_REL,
1033 qctxt_adjust_qunit(struct obd_device *obd, struct lustre_quota_ctxt *qctxt,
1034 const unsigned int id[], __u32 isblk, int wait,
1035 struct obd_trans_info *oti)
1037 int rc = 0, i = USRQUOTA;
1038 struct qunit_data qdata[MAXQUOTAS];
1041 CLASSERT(MAXQUOTAS < 4);
1042 if (!sb_any_quota_enabled(qctxt->lqc_sb))
1045 for (i = 0; i < MAXQUOTAS; i++) {
1046 qdata[i].qd_id = id[i];
1047 qdata[i].qd_flags = i;
1049 QDATA_SET_BLK(&qdata[i]);
1050 qdata[i].qd_count = 0;
1052 rc = check_cur_qunit(obd, qctxt, &qdata[i]);
1055 /* need acquire or release */
1056 opc = rc == 1 ? QUOTA_DQACQ : QUOTA_DQREL;
1057 rc = schedule_dqacq(obd, qctxt, &qdata[i], opc,
1061 } else if (wait == 1) {
1062 /* when wait equates 1, that means mds_quota_acquire
1063 * or filter_quota_acquire is calling it. */
1064 rc = qctxt_wait_pending_dqacq(qctxt, id[i], i, isblk);
1074 qctxt_wait_pending_dqacq(struct lustre_quota_ctxt *qctxt, unsigned int id,
1075 unsigned short type, int isblk)
1077 struct lustre_qunit *qunit = NULL;
1078 struct qunit_data qdata;
1079 struct timeval work_start;
1080 struct timeval work_end;
1082 struct l_wait_info lwi = { 0 };
1086 do_gettimeofday(&work_start);
1088 qdata.qd_flags = type;
1090 QDATA_SET_BLK(&qdata);
1093 spin_lock(&qunit_hash_lock);
1094 qunit = dqacq_in_flight(qctxt, &qdata);
1095 spin_unlock(&qunit_hash_lock);
1098 struct qunit_data *p = &qunit->lq_data;
1100 QDATA_DEBUG(p, "qunit(%p) is waiting for dqacq.\n", qunit);
1101 l_wait_event(qunit->lq_waitq, got_qunit(qunit, is_master(qctxt)),
1103 CDEBUG(D_QUOTA, "qunit(%p) finishes waiting: rc(%d) "
1104 "owner(%d)\n", qunit, qunit->lq_rc, qunit->lq_owner);
1105 /* keep same as schedule_dqacq() b=17030 */
1106 spin_lock(&qunit->lq_lock);
1108 spin_unlock(&qunit->lq_lock);
1109 /* this is for dqacq_in_flight() */
1111 do_gettimeofday(&work_end);
1112 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
1113 lprocfs_counter_add(qctxt->lqc_stats,
1114 isblk ? LQUOTA_WAIT_PENDING_BLK_QUOTA :
1115 LQUOTA_WAIT_PENDING_INO_QUOTA,
1118 do_gettimeofday(&work_end);
1119 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
1120 lprocfs_counter_add(qctxt->lqc_stats,
1121 isblk ? LQUOTA_NOWAIT_PENDING_BLK_QUOTA :
1122 LQUOTA_NOWAIT_PENDING_INO_QUOTA,
1130 qctxt_init(struct obd_device *obd, dqacq_handler_t handler)
1132 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
1133 struct obd_device_target *obt = &obd->u.obt;
1134 struct super_block *sb = obt->obt_sb;
1140 rc = ptlrpcd_addref();
1144 cfs_waitq_init(&qctxt->lqc_wait_for_qmaster);
1145 spin_lock_init(&qctxt->lqc_lock);
1146 spin_lock(&qctxt->lqc_lock);
1147 qctxt->lqc_handler = handler;
1149 qctxt->lqc_obt = obt;
1150 qctxt->lqc_import = NULL;
1151 qctxt->lqc_recovery = 0;
1152 qctxt->lqc_switch_qs = 1; /* Change qunit size in default setting */
1153 qctxt->lqc_valid = 1;
1154 qctxt->lqc_cqs_boundary_factor = 4;
1155 qctxt->lqc_cqs_least_bunit = PTLRPC_MAX_BRW_SIZE;
1156 qctxt->lqc_cqs_least_iunit = 2;
1157 qctxt->lqc_cqs_qs_factor = 2;
1158 qctxt->lqc_flags = 0;
1159 QUOTA_MASTER_UNREADY(qctxt);
1160 qctxt->lqc_bunit_sz = default_bunit_sz;
1161 qctxt->lqc_btune_sz = default_bunit_sz / 100 * default_btune_ratio;
1162 qctxt->lqc_iunit_sz = default_iunit_sz;
1163 qctxt->lqc_itune_sz = default_iunit_sz * default_itune_ratio / 100;
1164 qctxt->lqc_switch_seconds = 300; /* enlarging will wait 5 minutes
1165 * after the last shrinking */
1166 qctxt->lqc_sync_blk = 0;
1167 spin_unlock(&qctxt->lqc_lock);
1169 qctxt->lqc_lqs_hash = lustre_hash_init("LQS_HASH",
1173 if (!qctxt->lqc_lqs_hash) {
1174 CERROR("initialize hash lqs for %s error!\n", obd->obd_name);
1179 rc = lquota_proc_setup(obd, is_master(qctxt));
1181 CERROR("initialize proc for %s error!\n", obd->obd_name);
1187 void qctxt_cleanup(struct lustre_quota_ctxt *qctxt, int force)
1189 struct lustre_qunit *qunit, *tmp;
1190 struct list_head tmp_list;
1191 struct obd_device_target *obt = qctxt->lqc_obt;
1195 CFS_INIT_LIST_HEAD(&tmp_list);
1197 spin_lock(&qctxt->lqc_lock);
1198 qctxt->lqc_valid = 0;
1199 spin_unlock(&qctxt->lqc_lock);
1201 spin_lock(&qunit_hash_lock);
1202 for (i = 0; i < NR_DQHASH; i++) {
1203 list_for_each_entry_safe(qunit, tmp, &qunit_hash[i], lq_hash) {
1204 if (qunit->lq_ctxt != qctxt)
1206 remove_qunit_nolock(qunit);
1207 list_add(&qunit->lq_hash, &tmp_list);
1210 spin_unlock(&qunit_hash_lock);
1212 list_for_each_entry_safe(qunit, tmp, &tmp_list, lq_hash) {
1213 list_del_init(&qunit->lq_hash);
1214 compute_lqs_after_removing_qunit(qunit);
1216 /* wake up all waiters */
1217 QUNIT_SET_STATE_AND_RC(qunit, QUNIT_FINISHED, 0);
1218 wake_up_all(&qunit->lq_waitq);
1222 down_write(&obt->obt_rwsem);
1223 lustre_hash_exit(qctxt->lqc_lqs_hash);
1224 qctxt->lqc_lqs_hash = NULL;
1225 up_write(&obt->obt_rwsem);
1227 /* after qctxt_cleanup, qctxt might be freed, then check_qm() is
1228 * unpredicted. So we must wait until lqc_wait_for_qmaster is empty */
1229 while (cfs_waitq_active(&qctxt->lqc_wait_for_qmaster)) {
1230 cfs_waitq_signal(&qctxt->lqc_wait_for_qmaster);
1231 cfs_schedule_timeout(CFS_TASK_INTERRUPTIBLE,
1232 cfs_time_seconds(1));
1238 if (lquota_proc_cleanup(qctxt))
1239 CERROR("cleanup proc error!\n");
1245 struct qslave_recov_thread_data {
1246 struct obd_device *obd;
1247 struct lustre_quota_ctxt *qctxt;
1248 struct completion comp;
1251 /* FIXME only recovery block quota by now */
1252 static int qslave_recovery_main(void *arg)
1254 struct qslave_recov_thread_data *data = arg;
1255 struct obd_device *obd = data->obd;
1256 struct lustre_quota_ctxt *qctxt = data->qctxt;
1261 ptlrpc_daemonize("qslave_recovd");
1264 class_incref(obd, "qslave_recovd_filter", obd);
1266 complete(&data->comp);
1268 spin_lock(&qctxt->lqc_lock);
1269 if (qctxt->lqc_recovery) {
1270 spin_unlock(&qctxt->lqc_lock);
1271 class_decref(obd, "qslave_recovd_filter", obd);
1274 qctxt->lqc_recovery = 1;
1275 spin_unlock(&qctxt->lqc_lock);
1278 for (type = USRQUOTA; type < MAXQUOTAS; type++) {
1279 struct qunit_data qdata;
1280 struct quota_info *dqopt = sb_dqopt(qctxt->lqc_sb);
1281 struct list_head id_list;
1282 struct dquot_id *dqid, *tmp;
1285 LOCK_DQONOFF_MUTEX(dqopt);
1286 if (!sb_has_quota_enabled(qctxt->lqc_sb, type)) {
1287 UNLOCK_DQONOFF_MUTEX(dqopt);
1291 LASSERT(dqopt->files[type] != NULL);
1292 CFS_INIT_LIST_HEAD(&id_list);
1293 #ifndef KERNEL_SUPPORTS_QUOTA_READ
1294 rc = fsfilt_qids(obd, dqopt->files[type], NULL, type, &id_list);
1296 rc = fsfilt_qids(obd, NULL, dqopt->files[type], type, &id_list);
1298 UNLOCK_DQONOFF_MUTEX(dqopt);
1300 CERROR("Get ids from quota file failed. (rc:%d)\n", rc);
1302 list_for_each_entry_safe(dqid, tmp, &id_list, di_link) {
1303 list_del_init(&dqid->di_link);
1304 /* skip slave recovery on itself */
1305 if (is_master(qctxt))
1307 if (rc && rc != -EBUSY)
1310 qdata.qd_id = dqid->di_id;
1311 qdata.qd_flags = type;
1312 QDATA_SET_BLK(&qdata);
1315 ret = check_cur_qunit(obd, qctxt, &qdata);
1318 opc = ret == 1 ? QUOTA_DQACQ : QUOTA_DQREL;
1319 rc = schedule_dqacq(obd, qctxt, &qdata, opc,
1328 CDEBUG(rc == -EBUSY ? D_QUOTA : D_ERROR,
1329 "qslave recovery failed! (id:%d type:%d "
1330 " rc:%d)\n", dqid->di_id, type, rc);
1336 spin_lock(&qctxt->lqc_lock);
1337 qctxt->lqc_recovery = 0;
1338 spin_unlock(&qctxt->lqc_lock);
1339 class_decref(obd, "qslave_recovd_filter", obd);
1344 qslave_start_recovery(struct obd_device *obd, struct lustre_quota_ctxt *qctxt)
1346 struct qslave_recov_thread_data data;
1350 if (!sb_any_quota_enabled(qctxt->lqc_sb))
1355 init_completion(&data.comp);
1357 rc = kernel_thread(qslave_recovery_main, &data, CLONE_VM|CLONE_FILES);
1359 CERROR("Cannot start quota recovery thread: rc %d\n", rc);
1362 wait_for_completion(&data.comp);
1369 * lqs<->qctxt hash operations
1373 * string hashing using djb2 hash algorithm
1376 lqs_hash(lustre_hash_t *lh, void *key, unsigned mask)
1378 struct quota_adjust_qunit *lqs_key;
1383 lqs_key = (struct quota_adjust_qunit *)key;
1384 hash = (QAQ_IS_GRP(lqs_key) ? 5381 : 5387) * lqs_key->qaq_id;
1386 RETURN(hash & mask);
1390 lqs_compare(void *key, struct hlist_node *hnode)
1392 struct lustre_qunit_size *q;
1397 q = hlist_entry(hnode, struct lustre_qunit_size, lqs_hash);
1399 spin_lock(&q->lqs_lock);
1400 rc = (q->lqs_key == *((unsigned long long *)key));
1401 spin_unlock(&q->lqs_lock);
1407 lqs_get(struct hlist_node *hnode)
1409 struct lustre_qunit_size *q =
1410 hlist_entry(hnode, struct lustre_qunit_size, lqs_hash);
1413 atomic_inc(&q->lqs_refcount);
1414 CDEBUG(D_QUOTA, "lqs=%p refcount %d\n",
1415 q, atomic_read(&q->lqs_refcount));
1421 lqs_put(struct hlist_node *hnode)
1423 struct lustre_qunit_size *q =
1424 hlist_entry(hnode, struct lustre_qunit_size, lqs_hash);
1427 LASSERT(atomic_read(&q->lqs_refcount) > 0);
1428 atomic_dec(&q->lqs_refcount);
1429 CDEBUG(D_QUOTA, "lqs=%p refcount %d\n",
1430 q, atomic_read(&q->lqs_refcount));
1436 lqs_exit(struct hlist_node *hnode)
1438 struct lustre_qunit_size *q;
1441 q = hlist_entry(hnode, struct lustre_qunit_size, lqs_hash);
1443 * Nothing should be left. User of lqs put it and
1444 * lqs also was deleted from table by this time
1445 * so we should have 0 refs.
1447 LASSERTF(atomic_read(&q->lqs_refcount) == 0,
1448 "Busy lqs %p with %d refs\n", q,
1449 atomic_read(&q->lqs_refcount));
1454 static lustre_hash_ops_t lqs_hash_ops = {
1455 .lh_hash = lqs_hash,
1456 .lh_compare = lqs_compare,
1461 #endif /* HAVE_QUOTA_SUPPORT */