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 */
118 #define QUNIT_SET_STATE(qunit, state) \
120 spin_lock(&qunit->lq_lock); \
121 QDATA_DEBUG((&qunit->lq_data), "qunit(%p) lq_state(%s->%s), " \
123 qunit, qunit_state_names[qunit->lq_state], \
124 qunit_state_names[state], qunit->lq_rc); \
125 qunit->lq_state = state; \
126 spin_unlock(&qunit->lq_lock); \
129 #define QUNIT_SET_STATE_AND_RC(qunit, state, rc) \
131 spin_lock(&qunit->lq_lock); \
133 QDATA_DEBUG((&qunit->lq_data), "qunit(%p) lq_state(%s->%s), " \
135 qunit, qunit_state_names[qunit->lq_state], \
136 qunit_state_names[state], qunit->lq_rc); \
137 qunit->lq_state = state; \
138 spin_unlock(&qunit->lq_lock); \
142 int should_translate_quota (struct obd_import *imp)
147 if (imp->imp_connect_data.ocd_connect_flags & OBD_CONNECT_QUOTA64)
153 void qunit_cache_cleanup(void)
158 spin_lock(&qunit_hash_lock);
159 for (i = 0; i < NR_DQHASH; i++)
160 LASSERT(list_empty(qunit_hash + i));
161 spin_unlock(&qunit_hash_lock);
165 rc = cfs_mem_cache_destroy(qunit_cachep);
166 LASSERTF(rc == 0, "couldn't destroy qunit_cache slab\n");
172 int qunit_cache_init(void)
177 LASSERT(qunit_cachep == NULL);
178 qunit_cachep = cfs_mem_cache_create("ll_qunit_cache",
179 sizeof(struct lustre_qunit),
184 spin_lock(&qunit_hash_lock);
185 for (i = 0; i < NR_DQHASH; i++)
186 CFS_INIT_LIST_HEAD(qunit_hash + i);
187 spin_unlock(&qunit_hash_lock);
192 qunit_hashfn(struct lustre_quota_ctxt *qctxt, struct qunit_data *qdata)
193 __attribute__((__const__));
196 qunit_hashfn(struct lustre_quota_ctxt *qctxt, struct qunit_data *qdata)
198 unsigned int id = qdata->qd_id;
199 unsigned int type = QDATA_IS_GRP(qdata);
201 unsigned long tmp = ((unsigned long)qctxt >> L1_CACHE_SHIFT) ^ id;
202 tmp = (tmp * (MAXQUOTAS - type)) % NR_DQHASH;
206 /* caller must hold qunit_hash_lock */
207 static inline struct lustre_qunit *find_qunit(unsigned int hashent,
208 struct lustre_quota_ctxt *qctxt,
209 struct qunit_data *qdata)
211 struct lustre_qunit *qunit = NULL;
212 struct qunit_data *tmp;
214 LASSERT_SPIN_LOCKED(&qunit_hash_lock);
215 list_for_each_entry(qunit, qunit_hash + hashent, lq_hash) {
216 tmp = &qunit->lq_data;
217 if (qunit->lq_ctxt == qctxt &&
218 qdata->qd_id == tmp->qd_id &&
219 (qdata->qd_flags & LQUOTA_QUNIT_FLAGS) ==
220 (tmp->qd_flags & LQUOTA_QUNIT_FLAGS))
226 /* check_cur_qunit - check the current usage of qunit.
227 * @qctxt: quota context
228 * @qdata: the type of quota unit to be checked
230 * return: 1 - need acquire qunit;
231 * 2 - need release qunit;
232 * 0 - need do nothing.
236 check_cur_qunit(struct obd_device *obd,
237 struct lustre_quota_ctxt *qctxt, struct qunit_data *qdata)
239 struct super_block *sb = qctxt->lqc_sb;
240 unsigned long qunit_sz, tune_sz;
241 __u64 usage, limit, limit_org, pending_write = 0;
242 long long record = 0;
243 struct obd_quotactl *qctl;
244 struct lustre_qunit_size *lqs = NULL;
248 if (!sb_any_quota_enabled(sb))
251 spin_lock(&qctxt->lqc_lock);
252 if (!qctxt->lqc_valid){
253 spin_unlock(&qctxt->lqc_lock);
256 spin_unlock(&qctxt->lqc_lock);
262 /* get fs quota usage & limit */
263 qctl->qc_cmd = Q_GETQUOTA;
264 qctl->qc_id = qdata->qd_id;
265 qctl->qc_type = QDATA_IS_GRP(qdata);
266 ret = fsfilt_quotactl(obd, sb, qctl);
268 if (ret == -ESRCH) /* no limit */
271 CERROR("can't get fs quota usage! (rc:%d)\n", ret);
275 if (QDATA_IS_BLK(qdata)) {
276 usage = qctl->qc_dqblk.dqb_curspace;
277 limit = qctl->qc_dqblk.dqb_bhardlimit << QUOTABLOCK_BITS;
279 usage = qctl->qc_dqblk.dqb_curinodes;
280 limit = qctl->qc_dqblk.dqb_ihardlimit;
283 /* ignore the no quota limit case; and it can avoid creating
284 * unnecessary lqs for uid/gid */
289 quota_search_lqs(qdata, NULL, qctxt, &lqs);
291 CDEBUG(D_QUOTA, "Can't find the lustre qunit size!\n");
292 ret = quota_create_lqs(qdata, NULL, qctxt, &lqs);
293 if (ret == -EALREADY) {
300 spin_lock(&lqs->lqs_lock);
302 if (QDATA_IS_BLK(qdata)) {
303 qunit_sz = lqs->lqs_bunit_sz;
304 tune_sz = lqs->lqs_btune_sz;
305 pending_write = lqs->lqs_bwrite_pending;
306 record = lqs->lqs_blk_rec;
307 LASSERT(!(qunit_sz % QUOTABLOCK_SIZE));
309 /* we didn't need change inode qunit size now */
310 qunit_sz = lqs->lqs_iunit_sz;
311 tune_sz = lqs->lqs_itune_sz;
312 pending_write = lqs->lqs_iwrite_pending;
313 record = lqs->lqs_ino_rec;
316 /* we don't count the MIN_QLIMIT */
317 if ((limit == MIN_QLIMIT && !QDATA_IS_BLK(qdata)) ||
318 (toqb(limit) == MIN_QLIMIT && QDATA_IS_BLK(qdata)))
321 usage += pending_write;
323 /* when a releasing quota req is sent, before it returned
324 limit is assigned a small value. limit will overflow */
325 if (limit + record < 0)
330 LASSERT(qdata->qd_count == 0);
331 if (limit <= usage + tune_sz) {
332 while (qdata->qd_count + limit <=
334 qdata->qd_count += qunit_sz;
336 } else if (limit > usage + qunit_sz + tune_sz &&
337 limit_org > qdata->qd_count + qunit_sz) {
338 while (limit - qdata->qd_count > usage + qunit_sz + tune_sz &&
339 limit_org > qdata->qd_count + qunit_sz)
340 qdata->qd_count += qunit_sz;
342 /* if there are other pending writes for this uid/gid, releasing
343 * quota is put off until the last pending write b=16645 */
344 if (ret == 2 && pending_write) {
345 CDEBUG(D_QUOTA, "delay quota release\n");
349 CDEBUG(D_QUOTA, "type: %c, limit: "LPU64", usage: "LPU64
350 ", pending_write: "LPU64", record: "LPD64
351 ", qunit_sz: %lu, tune_sz: %lu, ret: %d.\n",
352 QDATA_IS_BLK(qdata) ? 'b' : 'i', limit, usage, pending_write,
353 record, qunit_sz, tune_sz, ret);
354 LASSERT(ret == 0 || qdata->qd_count);
356 spin_unlock(&lqs->lqs_lock);
365 * Compute the remaining quota for certain gid or uid b=11693
367 int compute_remquota(struct obd_device *obd, struct lustre_quota_ctxt *qctxt,
368 struct qunit_data *qdata, int isblk)
370 struct super_block *sb = qctxt->lqc_sb;
372 struct obd_quotactl *qctl;
373 int ret = QUOTA_RET_OK;
376 if (!sb_any_quota_enabled(sb))
377 RETURN(QUOTA_RET_NOQUOTA);
379 /* ignore root user */
380 if (qdata->qd_id == 0 && QDATA_IS_GRP(qdata) == USRQUOTA)
381 RETURN(QUOTA_RET_NOLIMIT);
387 /* get fs quota usage & limit */
388 qctl->qc_cmd = Q_GETQUOTA;
389 qctl->qc_id = qdata->qd_id;
390 qctl->qc_type = QDATA_IS_GRP(qdata);
391 ret = fsfilt_quotactl(obd, sb, qctl);
393 if (ret == -ESRCH) /* no limit */
394 ret = QUOTA_RET_NOLIMIT;
396 CDEBUG(D_QUOTA, "can't get fs quota usage! (rc:%d)",
401 usage = isblk ? qctl->qc_dqblk.dqb_curspace :
402 qctl->qc_dqblk.dqb_curinodes;
403 limit = isblk ? qctl->qc_dqblk.dqb_bhardlimit << QUOTABLOCK_BITS :
404 qctl->qc_dqblk.dqb_ihardlimit;
405 if (!limit){ /* no limit */
406 ret = QUOTA_RET_NOLIMIT;
411 qdata->qd_count = limit - usage;
420 static struct lustre_qunit *alloc_qunit(struct lustre_quota_ctxt *qctxt,
421 struct qunit_data *qdata, int opc)
423 struct lustre_qunit *qunit = NULL;
426 OBD_SLAB_ALLOC(qunit, qunit_cachep, CFS_ALLOC_IO, sizeof(*qunit));
430 CFS_INIT_LIST_HEAD(&qunit->lq_hash);
431 init_waitqueue_head(&qunit->lq_waitq);
432 atomic_set(&qunit->lq_refcnt, 1);
433 qunit->lq_ctxt = qctxt;
434 memcpy(&qunit->lq_data, qdata, sizeof(*qdata));
436 qunit->lq_lock = SPIN_LOCK_UNLOCKED;
437 QUNIT_SET_STATE_AND_RC(qunit, QUNIT_CREATED, 0);
441 static inline void free_qunit(struct lustre_qunit *qunit)
443 OBD_SLAB_FREE(qunit, qunit_cachep, sizeof(*qunit));
446 static inline void qunit_get(struct lustre_qunit *qunit)
448 atomic_inc(&qunit->lq_refcnt);
451 static void qunit_put(struct lustre_qunit *qunit)
453 LASSERT(atomic_read(&qunit->lq_refcnt));
454 if (atomic_dec_and_test(&qunit->lq_refcnt))
458 /* caller must hold qunit_hash_lock and release ref of qunit after using it */
459 static struct lustre_qunit *dqacq_in_flight(struct lustre_quota_ctxt *qctxt,
460 struct qunit_data *qdata)
462 unsigned int hashent = qunit_hashfn(qctxt, qdata);
463 struct lustre_qunit *qunit;
466 LASSERT_SPIN_LOCKED(&qunit_hash_lock);
467 qunit = find_qunit(hashent, qctxt, qdata);
474 insert_qunit_nolock(struct lustre_quota_ctxt *qctxt, struct lustre_qunit *qunit)
476 struct list_head *head;
478 LASSERT(list_empty(&qunit->lq_hash));
480 head = qunit_hash + qunit_hashfn(qctxt, &qunit->lq_data);
481 list_add(&qunit->lq_hash, head);
482 QUNIT_SET_STATE(qunit, QUNIT_IN_HASH);
485 static void compute_lqs_after_removing_qunit(struct lustre_qunit *qunit)
487 struct lustre_qunit_size *lqs = NULL;
489 quota_search_lqs(&qunit->lq_data, NULL, qunit->lq_ctxt, &lqs);
491 spin_lock(&lqs->lqs_lock);
492 if (qunit->lq_opc == QUOTA_DQACQ)
493 quota_compute_lqs(&qunit->lq_data, lqs, 0, 1);
494 if (qunit->lq_opc == QUOTA_DQREL)
495 quota_compute_lqs(&qunit->lq_data, lqs, 0, 0);
496 spin_unlock(&lqs->lqs_lock);
497 /* this is for quota_search_lqs */
499 /* this is for schedule_dqacq */
505 static void remove_qunit_nolock(struct lustre_qunit *qunit)
507 LASSERT(!list_empty(&qunit->lq_hash));
508 LASSERT_SPIN_LOCKED(&qunit_hash_lock);
510 list_del_init(&qunit->lq_hash);
511 QUNIT_SET_STATE(qunit, QUNIT_RM_FROM_HASH);
515 #define INC_QLIMIT(limit, count) (limit == MIN_QLIMIT) ? \
516 (limit = count) : (limit += count)
519 static inline int is_master(struct lustre_quota_ctxt *qctxt)
521 return qctxt->lqc_handler ? 1 : 0;
525 schedule_dqacq(struct obd_device *obd, struct lustre_quota_ctxt *qctxt,
526 struct qunit_data *qdata, int opc, int wait,
527 struct obd_trans_info *oti);
530 dqacq_completion(struct obd_device *obd, struct lustre_quota_ctxt *qctxt,
531 struct qunit_data *qdata, int rc, int opc)
533 struct lustre_qunit *qunit = NULL;
534 struct super_block *sb = qctxt->lqc_sb;
536 struct quota_adjust_qunit *oqaq = NULL;
541 QDATA_DEBUG(qdata, "obd(%s): complete %s quota req\n",
542 obd->obd_name, (opc == QUOTA_DQACQ) ? "acq" : "rel");
544 /* update local operational quota file */
546 __u64 count = QUSG(qdata->qd_count, QDATA_IS_BLK(qdata));
547 struct obd_quotactl *qctl;
552 GOTO(out, err = -ENOMEM);
554 /* acq/rel qunit for specified uid/gid is serialized,
555 * so there is no race between get fs quota limit and
556 * set fs quota limit */
557 qctl->qc_cmd = Q_GETQUOTA;
558 qctl->qc_id = qdata->qd_id;
559 qctl->qc_type = QDATA_IS_GRP(qdata);
560 err = fsfilt_quotactl(obd, sb, qctl);
562 CERROR("error get quota fs limit! (rc:%d)\n", err);
566 if (QDATA_IS_BLK(qdata)) {
567 qctl->qc_dqblk.dqb_valid = QIF_BLIMITS;
568 hardlimit = &qctl->qc_dqblk.dqb_bhardlimit;
570 qctl->qc_dqblk.dqb_valid = QIF_ILIMITS;
571 hardlimit = &qctl->qc_dqblk.dqb_ihardlimit;
574 CDEBUG(D_QUOTA, "hardlimt: "LPU64"\n", *hardlimit);
581 INC_QLIMIT(*hardlimit, count);
584 LASSERTF(count < *hardlimit,
585 "id(%u) flag(%u) type(%c) isblk(%c) "
586 "count("LPU64") qd_qunit("LPU64") "
587 "hardlimit("LPU64").\n",
588 qdata->qd_id, qdata->qd_flags,
589 QDATA_IS_GRP(qdata) ? 'g' : 'u',
590 QDATA_IS_BLK(qdata) ? 'b': 'i',
591 qdata->qd_count, qdata->qd_qunit, *hardlimit);
598 /* clear quota limit */
602 qctl->qc_cmd = Q_SETQUOTA;
603 err = fsfilt_quotactl(obd, sb, qctl);
605 CERROR("error set quota fs limit! (rc:%d)\n", err);
607 QDATA_DEBUG(qdata, "%s completion\n",
608 opc == QUOTA_DQACQ ? "DQACQ" : "DQREL");
611 } else if (rc == -EDQUOT) {
612 QDATA_DEBUG(qdata, "acquire qunit got EDQUOT.\n");
613 } else if (rc == -EBUSY) {
614 QDATA_DEBUG(qdata, "it's is recovering, got EBUSY.\n");
616 CERROR("acquire qunit got error! (rc:%d)\n", rc);
619 /* remove the qunit from hash */
620 spin_lock(&qunit_hash_lock);
622 qunit = dqacq_in_flight(qctxt, qdata);
623 /* this qunit has been removed by qctxt_cleanup() */
625 spin_unlock(&qunit_hash_lock);
626 QDATA_DEBUG(qdata, "%s is discarded because qunit isn't found\n",
627 opc == QUOTA_DQACQ ? "DQACQ" : "DQREL");
631 LASSERT(opc == qunit->lq_opc);
632 /* remove this qunit from lq_hash so that new processes cannot be added
633 * to qunit->lq_waiters */
634 remove_qunit_nolock(qunit);
635 spin_unlock(&qunit_hash_lock);
637 compute_lqs_after_removing_qunit(qunit);
640 rc = QUOTA_REQ_RETURNED;
641 QUNIT_SET_STATE_AND_RC(qunit, QUNIT_FINISHED, rc);
642 /* wake up all waiters */
643 wake_up_all(&qunit->lq_waitq);
645 /* this is for dqacq_in_flight() */
647 /* this is for alloc_qunit() */
649 if (rc < 0 && rc != -EDQUOT)
652 /* don't reschedule in such cases:
653 * - acq/rel failure and qunit isn't changed,
654 * but not for quota recovery.
655 * - local dqacq/dqrel.
656 * - local disk io failure.
661 qdata_to_oqaq(qdata, oqaq);
662 /* adjust the qunit size in slaves */
663 rc1 = quota_adjust_slave_lqs(oqaq, qctxt);
666 CERROR("adjust slave's qunit size failed!(rc:%d)\n", rc1);
669 if (err || (rc < 0 && rc != -EBUSY && rc1 == 0) || is_master(qctxt))
672 /* reschedule another dqacq/dqrel if needed */
674 qdata->qd_flags &= LQUOTA_QUNIT_FLAGS;
675 rc1 = check_cur_qunit(obd, qctxt, qdata);
678 opc = rc1 == 1 ? QUOTA_DQACQ : QUOTA_DQREL;
679 rc1 = schedule_dqacq(obd, qctxt, qdata, opc, 0, NULL);
680 QDATA_DEBUG(qdata, "reschedudle opc(%d) rc(%d)\n", opc, rc1);
685 struct dqacq_async_args {
686 struct lustre_quota_ctxt *aa_ctxt;
687 struct lustre_qunit *aa_qunit;
690 static int dqacq_interpret(const struct lu_env *env,
691 struct ptlrpc_request *req, void *data, int rc)
693 struct dqacq_async_args *aa = (struct dqacq_async_args *)data;
694 struct lustre_quota_ctxt *qctxt = aa->aa_ctxt;
695 struct obd_device_target *obt = qctxt->lqc_obt;
696 struct lustre_qunit *qunit = aa->aa_qunit;
697 struct obd_device *obd = req->rq_import->imp_obd;
698 struct qunit_data *qdata = NULL;
703 LASSERT(req->rq_import);
705 /* there are several forms of qunit(historic causes), so we need to
706 * adjust qunit from slaves to the same form here */
707 OBD_ALLOC(qdata, sizeof(struct qunit_data));
711 down_read(&obt->obt_rwsem);
712 /* if a quota req timeouts or is dropped, we should update quota
713 * statistics which will be handled in dqacq_completion. And in
714 * this situation we should get qdata from request instead of
716 rc1 = quota_get_qdata(req, qdata,
717 (rc != 0) ? QUOTA_REQUEST : QUOTA_REPLY,
720 DEBUG_REQ(D_ERROR, req,
721 "error unpacking qunit_data(rc: %d)\n", rc1);
722 GOTO(exit, rc = rc1);
725 QDATA_DEBUG(qdata, "qdata: interpret rc(%d).\n", rc);
726 QDATA_DEBUG((&qunit->lq_data), "lq_data: \n");
728 if (qdata->qd_id != qunit->lq_data.qd_id ||
729 OBD_FAIL_CHECK(OBD_FAIL_QUOTA_RET_QDATA)) {
730 CDEBUG(D_ERROR, "the returned qd_id isn't expected!"
731 "(qdata: %u, lq_data: %u)\n", qdata->qd_id,
732 qunit->lq_data.qd_id);
733 qdata->qd_id = qunit->lq_data.qd_id;
736 if (QDATA_IS_GRP(qdata) != QDATA_IS_GRP(&qunit->lq_data)) {
737 CDEBUG(D_ERROR, "the returned grp/usr isn't expected!"
738 "(qdata: %u, lq_data: %u)\n", qdata->qd_flags,
739 qunit->lq_data.qd_flags);
740 if (QDATA_IS_GRP(&qunit->lq_data))
741 QDATA_SET_GRP(qdata);
743 QDATA_CLR_GRP(qdata);
746 if (qdata->qd_count > qunit->lq_data.qd_count) {
747 CDEBUG(D_ERROR, "the returned qd_count isn't expected!"
748 "(qdata: "LPU64", lq_data: "LPU64")\n", qdata->qd_count,
749 qunit->lq_data.qd_count);
753 rc = dqacq_completion(obd, qctxt, qdata, rc,
754 lustre_msg_get_opc(req->rq_reqmsg));
757 up_read(&obt->obt_rwsem);
758 OBD_FREE(qdata, sizeof(struct qunit_data));
764 * check if quota master is online
766 int check_qm(struct lustre_quota_ctxt *qctxt)
771 spin_lock(&qctxt->lqc_lock);
772 /* quit waiting when mds is back or qctxt is cleaned up */
773 rc = qctxt->lqc_import || !qctxt->lqc_valid;
774 spin_unlock(&qctxt->lqc_lock);
779 /* wake up all waiting threads when lqc_import is NULL */
780 void dqacq_interrupt(struct lustre_quota_ctxt *qctxt)
782 struct lustre_qunit *qunit, *tmp;
786 spin_lock(&qunit_hash_lock);
787 for (i = 0; i < NR_DQHASH; i++) {
788 list_for_each_entry_safe(qunit, tmp, &qunit_hash[i], lq_hash) {
789 if (qunit->lq_ctxt != qctxt)
792 /* Wake up all waiters. Do not change lq_state.
793 * The waiters will check lq_rc which is kept as 0
794 * if no others change it, then the waiters will return
795 * -EAGAIN to caller who can perform related quota
796 * acq/rel if necessary. */
797 wake_up_all(&qunit->lq_waitq);
800 spin_unlock(&qunit_hash_lock);
804 static int got_qunit(struct lustre_qunit *qunit)
806 struct lustre_quota_ctxt *qctxt = qunit->lq_ctxt;
810 spin_lock(&qunit->lq_lock);
811 switch (qunit->lq_state) {
813 case QUNIT_RM_FROM_HASH:
819 CERROR("invalid qunit state %d\n", qunit->lq_state);
821 spin_unlock(&qunit->lq_lock);
824 spin_lock(&qctxt->lqc_lock);
825 rc = !qctxt->lqc_import || !qctxt->lqc_valid;
826 spin_unlock(&qctxt->lqc_lock);
833 schedule_dqacq(struct obd_device *obd, struct lustre_quota_ctxt *qctxt,
834 struct qunit_data *qdata, int opc, int wait,
835 struct obd_trans_info *oti)
837 struct lustre_qunit *qunit, *empty;
838 struct l_wait_info lwi = { 0 };
839 struct ptlrpc_request *req;
840 struct dqacq_async_args *aa;
841 struct obd_import *imp = NULL;
842 struct lustre_qunit_size *lqs = NULL;
843 struct timeval work_start;
844 struct timeval work_end;
849 LASSERT(opc == QUOTA_DQACQ || opc == QUOTA_DQREL);
850 do_gettimeofday(&work_start);
851 if ((empty = alloc_qunit(qctxt, qdata, opc)) == NULL)
854 spin_lock(&qunit_hash_lock);
855 qunit = dqacq_in_flight(qctxt, qdata);
857 spin_unlock(&qunit_hash_lock);
860 goto wait_completion;
864 insert_qunit_nolock(qctxt, qunit);
865 spin_unlock(&qunit_hash_lock);
867 quota_search_lqs(qdata, NULL, qctxt, &lqs);
869 spin_lock(&lqs->lqs_lock);
870 quota_compute_lqs(qdata, lqs, 1, (opc == QUOTA_DQACQ) ? 1 : 0);
871 /* when this qdata returned from mds, it will call lqs_putref */
873 spin_unlock(&lqs->lqs_lock);
874 /* this is for quota_search_lqs */
877 CDEBUG(D_ERROR, "Can't find the lustre qunit size!\n");
880 QDATA_DEBUG(qdata, "obd(%s): send %s quota req\n",
881 obd->obd_name, (opc == QUOTA_DQACQ) ? "acq" : "rel");
882 /* master is going to dqacq/dqrel from itself */
883 if (is_master(qctxt)) {
885 QDATA_DEBUG(qdata, "local %s.\n",
886 opc == QUOTA_DQACQ ? "DQACQ" : "DQREL");
887 QDATA_SET_CHANGE_QS(qdata);
888 rc = qctxt->lqc_handler(obd, qdata, opc);
889 rc2 = dqacq_completion(obd, qctxt, qdata, rc, opc);
890 /* this is for qunit_get() */
893 do_gettimeofday(&work_end);
894 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
895 if (opc == QUOTA_DQACQ)
896 lprocfs_counter_add(qctxt->lqc_stats,
897 wait ? LQUOTA_SYNC_ACQ : LQUOTA_ASYNC_ACQ,
900 lprocfs_counter_add(qctxt->lqc_stats,
901 wait ? LQUOTA_SYNC_REL : LQUOTA_ASYNC_REL,
903 RETURN(rc ? rc : rc2);
906 spin_lock(&qctxt->lqc_lock);
907 if (!qctxt->lqc_import) {
908 spin_unlock(&qctxt->lqc_lock);
909 QDATA_DEBUG(qdata, "lqc_import is invalid.\n");
911 spin_lock(&qunit_hash_lock);
912 remove_qunit_nolock(qunit);
913 spin_unlock(&qunit_hash_lock);
915 compute_lqs_after_removing_qunit(qunit);
917 QUNIT_SET_STATE_AND_RC(qunit, QUNIT_FINISHED, -EAGAIN);
918 wake_up_all(&qunit->lq_waitq);
920 /* this is for qunit_get() */
922 /* this for alloc_qunit() */
924 spin_lock(&qctxt->lqc_lock);
925 if (wait && !qctxt->lqc_import) {
926 spin_unlock(&qctxt->lqc_lock);
928 LASSERT(oti && oti->oti_thread &&
929 oti->oti_thread->t_watchdog);
931 lc_watchdog_disable(oti->oti_thread->t_watchdog);
932 CDEBUG(D_QUOTA, "sleep for quota master\n");
933 l_wait_event(qctxt->lqc_wait_for_qmaster,
934 check_qm(qctxt), &lwi);
935 CDEBUG(D_QUOTA, "wake up when quota master is back\n");
936 lc_watchdog_touch(oti->oti_thread->t_watchdog);
938 spin_unlock(&qctxt->lqc_lock);
943 imp = class_import_get(qctxt->lqc_import);
944 spin_unlock(&qctxt->lqc_lock);
946 /* build dqacq/dqrel request */
949 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_QUOTA_DQACQ,
950 LUSTRE_MDS_VERSION, opc);
951 class_import_put(imp);
953 CDEBUG(D_ERROR, "Can't alloc request\n");
954 dqacq_completion(obd, qctxt, qdata, -ENOMEM, opc);
955 /* this is for qunit_get() */
960 ptlrpc_request_set_replen(req);
961 req->rq_no_resend = req->rq_no_delay = 1;
962 rc = quota_copy_qdata(req, qdata, QUOTA_REQUEST, QUOTA_IMPORT);
964 CDEBUG(D_ERROR, "Can't pack qunit_data(rc: %d)\n", rc);
965 ptlrpc_req_finished(req);
966 dqacq_completion(obd, qctxt, qdata, -EPROTO, opc);
967 /* this is for qunit_get() */
972 CLASSERT(sizeof(*aa) <= sizeof(req->rq_async_args));
973 aa = ptlrpc_req_async_args(req);
975 aa->aa_qunit = qunit;
977 req->rq_interpret_reply = dqacq_interpret;
978 ptlrpcd_add_req(req, PSCOPE_OTHER);
980 QDATA_DEBUG(qdata, "%s scheduled.\n",
981 opc == QUOTA_DQACQ ? "DQACQ" : "DQREL");
984 struct qunit_data *p = &qunit->lq_data;
986 QDATA_DEBUG(p, "qunit(%p) is waiting for dqacq.\n", qunit);
987 l_wait_event(qunit->lq_waitq, got_qunit(qunit), &lwi);
988 /* rc = -EAGAIN, it means the quota master isn't ready yet
989 * rc = QUOTA_REQ_RETURNED, it means a quota req is finished;
990 * rc = -EDQUOT, it means out of quota
991 * rc = -EBUSY, it means recovery is happening
992 * other rc < 0, it means real errors, functions who call
993 * schedule_dqacq should take care of this */
994 spin_lock(&qunit->lq_lock);
996 spin_unlock(&qunit->lq_lock);
997 CDEBUG(D_QUOTA, "qunit(%p) finishes waiting. (rc:%d)\n",
1002 do_gettimeofday(&work_end);
1003 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
1004 if (opc == QUOTA_DQACQ)
1005 lprocfs_counter_add(qctxt->lqc_stats,
1006 wait ? LQUOTA_SYNC_ACQ : LQUOTA_ASYNC_ACQ,
1009 lprocfs_counter_add(qctxt->lqc_stats,
1010 wait ? LQUOTA_SYNC_REL : LQUOTA_ASYNC_REL,
1017 qctxt_adjust_qunit(struct obd_device *obd, struct lustre_quota_ctxt *qctxt,
1018 uid_t uid, gid_t gid, __u32 isblk, int wait,
1019 struct obd_trans_info *oti)
1021 int rc = 0, i = USRQUOTA;
1022 __u32 id[MAXQUOTAS] = { uid, gid };
1023 struct qunit_data qdata[MAXQUOTAS];
1026 CLASSERT(MAXQUOTAS < 4);
1027 if (!sb_any_quota_enabled(qctxt->lqc_sb))
1030 for (i = 0; i < MAXQUOTAS; i++) {
1031 qdata[i].qd_id = id[i];
1032 qdata[i].qd_flags = i;
1034 QDATA_SET_BLK(&qdata[i]);
1035 qdata[i].qd_count = 0;
1037 rc = check_cur_qunit(obd, qctxt, &qdata[i]);
1040 /* need acquire or release */
1041 opc = rc == 1 ? QUOTA_DQACQ : QUOTA_DQREL;
1042 rc = schedule_dqacq(obd, qctxt, &qdata[i], opc,
1046 } else if (wait == 1) {
1047 /* when wait equates 1, that means mds_quota_acquire
1048 * or filter_quota_acquire is calling it. */
1049 rc = qctxt_wait_pending_dqacq(qctxt, id[i], i, isblk);
1059 qctxt_wait_pending_dqacq(struct lustre_quota_ctxt *qctxt, unsigned int id,
1060 unsigned short type, int isblk)
1062 struct lustre_qunit *qunit = NULL;
1063 struct qunit_data qdata;
1064 struct timeval work_start;
1065 struct timeval work_end;
1067 struct l_wait_info lwi = { 0 };
1071 do_gettimeofday(&work_start);
1073 qdata.qd_flags = type;
1075 QDATA_SET_BLK(&qdata);
1078 spin_lock(&qunit_hash_lock);
1079 qunit = dqacq_in_flight(qctxt, &qdata);
1080 spin_unlock(&qunit_hash_lock);
1083 struct qunit_data *p = &qunit->lq_data;
1085 QDATA_DEBUG(p, "qunit(%p) is waiting for dqacq.\n", qunit);
1086 l_wait_event(qunit->lq_waitq, got_qunit(qunit), &lwi);
1087 CDEBUG(D_QUOTA, "qunit(%p) finishes waiting. (rc:%d)\n",
1088 qunit, qunit->lq_rc);
1089 /* keep same as schedule_dqacq() b=17030 */
1090 spin_lock(&qunit->lq_lock);
1092 spin_unlock(&qunit->lq_lock);
1093 /* this is for dqacq_in_flight() */
1095 do_gettimeofday(&work_end);
1096 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
1097 lprocfs_counter_add(qctxt->lqc_stats,
1098 isblk ? LQUOTA_WAIT_PENDING_BLK_QUOTA :
1099 LQUOTA_WAIT_PENDING_INO_QUOTA,
1102 do_gettimeofday(&work_end);
1103 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
1104 lprocfs_counter_add(qctxt->lqc_stats,
1105 isblk ? LQUOTA_NOWAIT_PENDING_BLK_QUOTA :
1106 LQUOTA_NOWAIT_PENDING_INO_QUOTA,
1114 qctxt_init(struct obd_device *obd, dqacq_handler_t handler)
1116 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
1117 struct obd_device_target *obt = &obd->u.obt;
1118 struct super_block *sb = obt->obt_sb;
1124 rc = ptlrpcd_addref();
1128 cfs_waitq_init(&qctxt->lqc_wait_for_qmaster);
1129 spin_lock_init(&qctxt->lqc_lock);
1130 spin_lock(&qctxt->lqc_lock);
1131 qctxt->lqc_handler = handler;
1133 qctxt->lqc_obt = obt;
1134 qctxt->lqc_import = NULL;
1135 qctxt->lqc_recovery = 0;
1136 qctxt->lqc_switch_qs = 1; /* Change qunit size in default setting */
1137 qctxt->lqc_valid = 1;
1138 qctxt->lqc_cqs_boundary_factor = 4;
1139 qctxt->lqc_cqs_least_bunit = PTLRPC_MAX_BRW_SIZE;
1140 qctxt->lqc_cqs_least_iunit = 2;
1141 qctxt->lqc_cqs_qs_factor = 2;
1142 qctxt->lqc_flags = 0;
1143 QUOTA_MASTER_UNREADY(qctxt);
1144 qctxt->lqc_bunit_sz = default_bunit_sz;
1145 qctxt->lqc_btune_sz = default_bunit_sz / 100 * default_btune_ratio;
1146 qctxt->lqc_iunit_sz = default_iunit_sz;
1147 qctxt->lqc_itune_sz = default_iunit_sz * default_itune_ratio / 100;
1148 qctxt->lqc_switch_seconds = 300; /* enlarging will wait 5 minutes
1149 * after the last shrinking */
1150 qctxt->lqc_sync_blk = 0;
1151 spin_unlock(&qctxt->lqc_lock);
1153 qctxt->lqc_lqs_hash = lustre_hash_init("LQS_HASH", 7, 7,
1155 if (!qctxt->lqc_lqs_hash) {
1156 CERROR("initialize hash lqs for %s error!\n", obd->obd_name);
1161 rc = lquota_proc_setup(obd, is_master(qctxt));
1163 CERROR("initialize proc for %s error!\n", obd->obd_name);
1169 void qctxt_cleanup(struct lustre_quota_ctxt *qctxt, int force)
1171 struct lustre_qunit *qunit, *tmp;
1172 struct list_head tmp_list;
1173 struct obd_device_target *obt = qctxt->lqc_obt;
1177 CFS_INIT_LIST_HEAD(&tmp_list);
1179 spin_lock(&qctxt->lqc_lock);
1180 qctxt->lqc_valid = 0;
1181 spin_unlock(&qctxt->lqc_lock);
1183 spin_lock(&qunit_hash_lock);
1184 for (i = 0; i < NR_DQHASH; i++) {
1185 list_for_each_entry_safe(qunit, tmp, &qunit_hash[i], lq_hash) {
1186 if (qunit->lq_ctxt != qctxt)
1188 remove_qunit_nolock(qunit);
1189 list_add(&qunit->lq_hash, &tmp_list);
1192 spin_unlock(&qunit_hash_lock);
1194 list_for_each_entry_safe(qunit, tmp, &tmp_list, lq_hash) {
1195 list_del_init(&qunit->lq_hash);
1196 compute_lqs_after_removing_qunit(qunit);
1198 /* wake up all waiters */
1199 QUNIT_SET_STATE_AND_RC(qunit, QUNIT_FINISHED, 0);
1200 wake_up_all(&qunit->lq_waitq);
1204 down_write(&obt->obt_rwsem);
1205 lustre_hash_exit(qctxt->lqc_lqs_hash);
1206 qctxt->lqc_lqs_hash = NULL;
1207 up_write(&obt->obt_rwsem);
1209 /* after qctxt_cleanup, qctxt might be freed, then check_qm() is
1210 * unpredicted. So we must wait until lqc_wait_for_qmaster is empty */
1211 while (cfs_waitq_active(&qctxt->lqc_wait_for_qmaster)) {
1212 cfs_waitq_signal(&qctxt->lqc_wait_for_qmaster);
1213 cfs_schedule_timeout(CFS_TASK_INTERRUPTIBLE,
1214 cfs_time_seconds(1));
1220 if (lquota_proc_cleanup(qctxt))
1221 CERROR("cleanup proc error!\n");
1227 struct qslave_recov_thread_data {
1228 struct obd_device *obd;
1229 struct lustre_quota_ctxt *qctxt;
1230 struct completion comp;
1233 /* FIXME only recovery block quota by now */
1234 static int qslave_recovery_main(void *arg)
1236 struct qslave_recov_thread_data *data = arg;
1237 struct obd_device *obd = data->obd;
1238 struct lustre_quota_ctxt *qctxt = data->qctxt;
1243 ptlrpc_daemonize("qslave_recovd");
1245 complete(&data->comp);
1247 if (qctxt->lqc_recovery)
1249 qctxt->lqc_recovery = 1;
1251 for (type = USRQUOTA; type < MAXQUOTAS; type++) {
1252 struct qunit_data qdata;
1253 struct quota_info *dqopt = sb_dqopt(qctxt->lqc_sb);
1254 struct list_head id_list;
1255 struct dquot_id *dqid, *tmp;
1258 LOCK_DQONOFF_MUTEX(dqopt);
1259 if (!sb_has_quota_enabled(qctxt->lqc_sb, type)) {
1260 UNLOCK_DQONOFF_MUTEX(dqopt);
1264 LASSERT(dqopt->files[type] != NULL);
1265 CFS_INIT_LIST_HEAD(&id_list);
1266 #ifndef KERNEL_SUPPORTS_QUOTA_READ
1267 rc = fsfilt_qids(obd, dqopt->files[type], NULL, type, &id_list);
1269 rc = fsfilt_qids(obd, NULL, dqopt->files[type], type, &id_list);
1271 UNLOCK_DQONOFF_MUTEX(dqopt);
1273 CERROR("Get ids from quota file failed. (rc:%d)\n", rc);
1275 list_for_each_entry_safe(dqid, tmp, &id_list, di_link) {
1276 list_del_init(&dqid->di_link);
1277 /* skip slave recovery on itself */
1278 if (is_master(qctxt))
1280 if (rc && rc != -EBUSY)
1283 qdata.qd_id = dqid->di_id;
1284 qdata.qd_flags = type;
1285 QDATA_SET_BLK(&qdata);
1288 ret = check_cur_qunit(obd, qctxt, &qdata);
1291 opc = ret == 1 ? QUOTA_DQACQ : QUOTA_DQREL;
1292 rc = schedule_dqacq(obd, qctxt, &qdata, opc,
1301 CDEBUG(rc == -EBUSY ? D_QUOTA : D_ERROR,
1302 "qslave recovery failed! (id:%d type:%d "
1303 " rc:%d)\n", dqid->di_id, type, rc);
1309 qctxt->lqc_recovery = 0;
1314 qslave_start_recovery(struct obd_device *obd, struct lustre_quota_ctxt *qctxt)
1316 struct qslave_recov_thread_data data;
1320 if (!sb_any_quota_enabled(qctxt->lqc_sb))
1325 init_completion(&data.comp);
1327 rc = kernel_thread(qslave_recovery_main, &data, CLONE_VM|CLONE_FILES);
1329 CERROR("Cannot start quota recovery thread: rc %d\n", rc);
1332 wait_for_completion(&data.comp);
1339 * lqs<->qctxt hash operations
1343 * string hashing using djb2 hash algorithm
1346 lqs_hash(lustre_hash_t *lh, void *key, unsigned mask)
1348 struct quota_adjust_qunit *lqs_key;
1353 lqs_key = (struct quota_adjust_qunit *)key;
1354 hash = (QAQ_IS_GRP(lqs_key) ? 5381 : 5387) * lqs_key->qaq_id;
1356 RETURN(hash & mask);
1360 lqs_compare(void *key, struct hlist_node *hnode)
1362 struct quota_adjust_qunit *lqs_key;
1363 struct lustre_qunit_size *q;
1368 lqs_key = (struct quota_adjust_qunit *)key;
1369 q = hlist_entry(hnode, struct lustre_qunit_size, lqs_hash);
1371 spin_lock(&q->lqs_lock);
1372 rc = ((lqs_key->qaq_id == q->lqs_id) &&
1373 (QAQ_IS_GRP(lqs_key) == LQS_IS_GRP(q)));
1374 spin_unlock(&q->lqs_lock);
1380 lqs_get(struct hlist_node *hnode)
1382 struct lustre_qunit_size *q =
1383 hlist_entry(hnode, struct lustre_qunit_size, lqs_hash);
1386 atomic_inc(&q->lqs_refcount);
1387 CDEBUG(D_QUOTA, "lqs=%p refcount %d\n",
1388 q, atomic_read(&q->lqs_refcount));
1394 lqs_put(struct hlist_node *hnode)
1396 struct lustre_qunit_size *q =
1397 hlist_entry(hnode, struct lustre_qunit_size, lqs_hash);
1400 LASSERT(atomic_read(&q->lqs_refcount) > 0);
1401 atomic_dec(&q->lqs_refcount);
1402 CDEBUG(D_QUOTA, "lqs=%p refcount %d\n",
1403 q, atomic_read(&q->lqs_refcount));
1409 lqs_exit(struct hlist_node *hnode)
1411 struct lustre_qunit_size *q;
1414 q = hlist_entry(hnode, struct lustre_qunit_size, lqs_hash);
1416 * Nothing should be left. User of lqs put it and
1417 * lqs also was deleted from table by this time
1418 * so we should have 0 refs.
1420 LASSERTF(atomic_read(&q->lqs_refcount) == 0,
1421 "Busy lqs %p with %d refs\n", q,
1422 atomic_read(&q->lqs_refcount));
1427 static lustre_hash_ops_t lqs_hash_ops = {
1428 .lh_hash = lqs_hash,
1429 .lh_compare = lqs_compare,
1434 #endif /* HAVE_QUOTA_SUPPORT */