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 (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2011, 2012, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
38 * lustre/quota/quota_context.c
40 * Lustre Quota Context
42 * Author: Niu YaWei <niu@clusterfs.com>
46 # define EXPORT_SYMTAB
49 #define DEBUG_SUBSYSTEM S_LQUOTA
51 #include <linux/version.h>
53 #include <asm/unistd.h>
54 #include <linux/slab.h>
55 #include <linux/quotaops.h>
56 #include <linux/module.h>
57 #include <linux/init.h>
59 #include <obd_class.h>
60 #include <lustre_quota.h>
61 #include <lustre_fsfilt.h>
62 #include <lprocfs_status.h>
63 #include "quota_internal.h"
65 static int hash_lqs_cur_bits = HASH_LQS_CUR_BITS;
66 CFS_MODULE_PARM(hash_lqs_cur_bits, "i", int, 0444,
67 "the current bits of lqs hash");
69 #ifdef HAVE_QUOTA_SUPPORT
71 static cfs_hash_ops_t lqs_hash_ops;
73 unsigned long default_bunit_sz = 128 * 1024 * 1024; /* 128M bytes */
74 unsigned long default_btune_ratio = 50; /* 50 percentage */
75 unsigned long default_iunit_sz = 5120; /* 5120 inodes */
76 unsigned long default_itune_ratio = 50; /* 50 percentage */
78 cfs_mem_cache_t *qunit_cachep = NULL;
79 cfs_list_t qunit_hash[NR_DQHASH];
80 cfs_spinlock_t qunit_hash_lock = CFS_SPIN_LOCK_UNLOCKED;
82 /* please sync qunit_state with qunit_state_names */
89 * a qunit is added into qunit hash, that means
90 * a quota req will be sent or is flying
94 * a qunit is removed from qunit hash, that
95 * means a quota req is handled and comes back
97 QUNIT_RM_FROM_HASH = 2,
99 * qunit can wake up all threads waiting for it
104 static const char *qunit_state_names[] = {
105 [QUNIT_CREATED] = "CREATED",
106 [QUNIT_IN_HASH] = "IN_HASH",
107 [QUNIT_RM_FROM_HASH] = "RM_FROM_HASH",
108 [QUNIT_FINISHED] = "FINISHED",
111 struct lustre_qunit {
112 cfs_list_t lq_hash; /** Hash list in memory */
113 cfs_atomic_t lq_refcnt; /** Use count */
114 struct lustre_quota_ctxt *lq_ctxt; /** Quota context this applies to */
115 struct qunit_data lq_data; /** See qunit_data */
116 unsigned int lq_opc; /** QUOTA_DQACQ, QUOTA_DQREL */
117 cfs_waitq_t lq_waitq; /** Threads waiting for this qunit */
118 cfs_spinlock_t lq_lock; /** Protect the whole structure */
119 enum qunit_state lq_state; /** Present the status of qunit */
120 int lq_rc; /** The rc of lq_data */
124 #define QUNIT_SET_STATE(qunit, state) \
126 cfs_spin_lock(&qunit->lq_lock); \
127 QDATA_DEBUG((&qunit->lq_data), "qunit(%p) lq_state(%s->%s), " \
128 "lq_rc(%d), lq_owner(%d)\n", \
129 qunit, qunit_state_names[qunit->lq_state], \
130 qunit_state_names[state], qunit->lq_rc, \
132 qunit->lq_state = state; \
133 cfs_spin_unlock(&qunit->lq_lock); \
136 #define QUNIT_SET_STATE_AND_RC(qunit, state, rc) \
138 cfs_spin_lock(&qunit->lq_lock); \
140 QDATA_DEBUG((&qunit->lq_data), "qunit(%p) lq_state(%s->%s), " \
141 "lq_rc(%d), lq_owner(%d)\n", \
142 qunit, qunit_state_names[qunit->lq_state], \
143 qunit_state_names[state], qunit->lq_rc, \
145 qunit->lq_state = state; \
146 cfs_spin_unlock(&qunit->lq_lock); \
149 int should_translate_quota (struct obd_import *imp)
154 if (imp->imp_connect_data.ocd_connect_flags & OBD_CONNECT_QUOTA64)
160 void qunit_cache_cleanup(void)
165 cfs_spin_lock(&qunit_hash_lock);
166 for (i = 0; i < NR_DQHASH; i++)
167 LASSERT(cfs_list_empty(qunit_hash + i));
168 cfs_spin_unlock(&qunit_hash_lock);
172 rc = cfs_mem_cache_destroy(qunit_cachep);
173 LASSERTF(rc == 0, "couldn't destroy qunit_cache slab\n");
179 int qunit_cache_init(void)
184 LASSERT(qunit_cachep == NULL);
185 qunit_cachep = cfs_mem_cache_create("ll_qunit_cache",
186 sizeof(struct lustre_qunit),
191 cfs_spin_lock(&qunit_hash_lock);
192 for (i = 0; i < NR_DQHASH; i++)
193 CFS_INIT_LIST_HEAD(qunit_hash + i);
194 cfs_spin_unlock(&qunit_hash_lock);
199 qunit_hashfn(struct lustre_quota_ctxt *qctxt, struct qunit_data *qdata)
200 __attribute__((__const__));
203 qunit_hashfn(struct lustre_quota_ctxt *qctxt, struct qunit_data *qdata)
205 unsigned int id = qdata->qd_id;
206 unsigned int type = QDATA_IS_GRP(qdata);
208 unsigned long tmp = ((unsigned long)qctxt >> L1_CACHE_SHIFT) ^ id;
209 tmp = (tmp * (MAXQUOTAS - type)) % NR_DQHASH;
213 /* caller must hold qunit_hash_lock */
214 static inline struct lustre_qunit *find_qunit(unsigned int hashent,
215 struct lustre_quota_ctxt *qctxt,
216 struct qunit_data *qdata)
218 struct lustre_qunit *qunit = NULL;
219 struct qunit_data *tmp;
221 LASSERT_SPIN_LOCKED(&qunit_hash_lock);
222 cfs_list_for_each_entry(qunit, qunit_hash + hashent, lq_hash) {
223 tmp = &qunit->lq_data;
224 if (qunit->lq_ctxt == qctxt &&
225 qdata->qd_id == tmp->qd_id &&
226 (qdata->qd_flags & LQUOTA_QUNIT_FLAGS) ==
227 (tmp->qd_flags & LQUOTA_QUNIT_FLAGS))
233 /* check_cur_qunit - check the current usage of qunit.
234 * @qctxt: quota context
235 * @qdata: the type of quota unit to be checked
237 * return: 1 - need acquire qunit;
238 * 2 - need release qunit;
239 * 0 - need do nothing.
243 check_cur_qunit(struct obd_device *obd,
244 struct lustre_quota_ctxt *qctxt, struct qunit_data *qdata)
246 struct super_block *sb = qctxt->lqc_sb;
247 unsigned long qunit_sz, tune_sz;
248 __u64 usage, limit, limit_org, pending_write = 0;
249 long long record = 0;
250 struct obd_quotactl *qctl;
251 struct lustre_qunit_size *lqs = NULL;
255 if (!ll_sb_any_quota_active(sb))
258 cfs_spin_lock(&qctxt->lqc_lock);
259 if (!qctxt->lqc_valid){
260 cfs_spin_unlock(&qctxt->lqc_lock);
263 cfs_spin_unlock(&qctxt->lqc_lock);
269 /* get fs quota usage & limit */
270 qctl->qc_cmd = Q_GETQUOTA;
271 qctl->qc_id = qdata->qd_id;
272 qctl->qc_type = QDATA_IS_GRP(qdata);
273 ret = fsfilt_quotactl(obd, sb, qctl);
275 if (ret == -ESRCH) /* no limit */
278 CERROR("can't get fs quota usage! (rc:%d)\n", ret);
282 if (QDATA_IS_BLK(qdata)) {
283 usage = qctl->qc_dqblk.dqb_curspace;
284 limit = qctl->qc_dqblk.dqb_bhardlimit << QUOTABLOCK_BITS;
286 usage = qctl->qc_dqblk.dqb_curinodes;
287 limit = qctl->qc_dqblk.dqb_ihardlimit;
290 /* ignore the no quota limit case; and it can avoid creating
291 * unnecessary lqs for uid/gid */
295 lqs = quota_search_lqs(LQS_KEY(QDATA_IS_GRP(qdata), qdata->qd_id),
297 if (IS_ERR(lqs) || lqs == NULL) {
298 CERROR("fail to find a lqs for %sid: %u)!\n",
299 QDATA_IS_GRP(qdata) ? "g" : "u", qdata->qd_id);
302 cfs_spin_lock(&lqs->lqs_lock);
304 if (QDATA_IS_BLK(qdata)) {
305 qunit_sz = lqs->lqs_bunit_sz;
306 tune_sz = lqs->lqs_btune_sz;
307 pending_write = lqs->lqs_bwrite_pending;
308 record = lqs->lqs_blk_rec;
309 LASSERT(!(qunit_sz % QUOTABLOCK_SIZE));
311 /* we didn't need change inode qunit size now */
312 qunit_sz = lqs->lqs_iunit_sz;
313 tune_sz = lqs->lqs_itune_sz;
314 pending_write = lqs->lqs_iwrite_pending;
315 record = lqs->lqs_ino_rec;
318 /* we don't count the MIN_QLIMIT */
319 if ((limit == MIN_QLIMIT && !QDATA_IS_BLK(qdata)) ||
320 (toqb(limit) == MIN_QLIMIT && QDATA_IS_BLK(qdata)))
323 usage += pending_write;
325 /* when a releasing quota req is sent, before it returned
326 limit is assigned a small value. limit will overflow */
332 LASSERT(qdata->qd_count == 0);
333 if (limit <= usage + tune_sz) {
334 while (qdata->qd_count + limit <=
336 qdata->qd_count += qunit_sz;
338 } else if (limit > usage + qunit_sz + tune_sz &&
339 limit_org > qdata->qd_count + qunit_sz) {
340 while (limit - qdata->qd_count > usage + qunit_sz + tune_sz &&
341 limit_org > qdata->qd_count + qunit_sz)
342 qdata->qd_count += qunit_sz;
344 /* if there are other pending writes for this uid/gid, releasing
345 * quota is put off until the last pending write b=16645 */
346 /* if there is an ongoing quota request, a releasing request is aborted.
347 * That ongoing quota request will call this function again when
348 * it returned b=18630 */
349 if (pending_write || record) {
350 CDEBUG(D_QUOTA, "delay quota release\n");
355 quota_compute_lqs(qdata, lqs, 1, (ret == 1) ? 1 : 0);
357 CDEBUG(D_QUOTA, "type: %c, limit: "LPU64", usage: "LPU64
358 ", pending_write: "LPU64", record: %lld"
359 ", qunit_sz: %lu, tune_sz: %lu, ret: %d.\n",
360 QDATA_IS_BLK(qdata) ? 'b' : 'i', limit, usage, pending_write,
361 record, qunit_sz, tune_sz, ret);
362 LASSERT(ret == 0 || qdata->qd_count);
364 cfs_spin_unlock(&lqs->lqs_lock);
374 * Compute the remaining quota for certain gid or uid b=11693
376 int compute_remquota(struct obd_device *obd, struct lustre_quota_ctxt *qctxt,
377 struct qunit_data *qdata, int isblk)
379 struct super_block *sb = qctxt->lqc_sb;
381 struct obd_quotactl *qctl;
382 int ret = QUOTA_RET_OK;
385 if (!ll_sb_any_quota_active(sb))
386 RETURN(QUOTA_RET_NOQUOTA);
388 /* ignore root user */
389 if (qdata->qd_id == 0 && QDATA_IS_GRP(qdata) == USRQUOTA)
390 RETURN(QUOTA_RET_NOLIMIT);
396 /* get fs quota usage & limit */
397 qctl->qc_cmd = Q_GETQUOTA;
398 qctl->qc_id = qdata->qd_id;
399 qctl->qc_type = QDATA_IS_GRP(qdata);
400 ret = fsfilt_quotactl(obd, sb, qctl);
402 if (ret == -ESRCH) /* no limit */
403 ret = QUOTA_RET_NOLIMIT;
405 CDEBUG(D_QUOTA, "can't get fs quota usage! (rc:%d)",
410 usage = isblk ? qctl->qc_dqblk.dqb_curspace :
411 qctl->qc_dqblk.dqb_curinodes;
412 limit = isblk ? qctl->qc_dqblk.dqb_bhardlimit << QUOTABLOCK_BITS :
413 qctl->qc_dqblk.dqb_ihardlimit;
414 if (!limit){ /* no limit */
415 ret = QUOTA_RET_NOLIMIT;
420 qdata->qd_count = limit - usage;
429 static struct lustre_qunit *alloc_qunit(struct lustre_quota_ctxt *qctxt,
430 struct qunit_data *qdata, int opc)
432 struct lustre_qunit *qunit = NULL;
435 OBD_SLAB_ALLOC_PTR_GFP(qunit, qunit_cachep, CFS_ALLOC_IO);
439 CFS_INIT_LIST_HEAD(&qunit->lq_hash);
440 cfs_waitq_init(&qunit->lq_waitq);
441 cfs_atomic_set(&qunit->lq_refcnt, 1);
442 qunit->lq_ctxt = qctxt;
443 memcpy(&qunit->lq_data, qdata, sizeof(*qdata));
445 qunit->lq_lock = CFS_SPIN_LOCK_UNLOCKED;
446 QUNIT_SET_STATE_AND_RC(qunit, QUNIT_CREATED, 0);
447 qunit->lq_owner = cfs_curproc_pid();
451 static inline void free_qunit(struct lustre_qunit *qunit)
453 OBD_SLAB_FREE(qunit, qunit_cachep, sizeof(*qunit));
456 static inline void qunit_get(struct lustre_qunit *qunit)
458 cfs_atomic_inc(&qunit->lq_refcnt);
461 static void qunit_put(struct lustre_qunit *qunit)
463 LASSERT(cfs_atomic_read(&qunit->lq_refcnt));
464 if (cfs_atomic_dec_and_test(&qunit->lq_refcnt))
468 /* caller must hold qunit_hash_lock and release ref of qunit after using it */
469 static struct lustre_qunit *dqacq_in_flight(struct lustre_quota_ctxt *qctxt,
470 struct qunit_data *qdata)
472 unsigned int hashent = qunit_hashfn(qctxt, qdata);
473 struct lustre_qunit *qunit;
476 LASSERT_SPIN_LOCKED(&qunit_hash_lock);
477 qunit = find_qunit(hashent, qctxt, qdata);
484 insert_qunit_nolock(struct lustre_quota_ctxt *qctxt, struct lustre_qunit *qunit)
488 LASSERT(cfs_list_empty(&qunit->lq_hash));
490 head = qunit_hash + qunit_hashfn(qctxt, &qunit->lq_data);
491 cfs_list_add(&qunit->lq_hash, head);
492 QUNIT_SET_STATE(qunit, QUNIT_IN_HASH);
495 static void compute_lqs_after_removing_qunit(struct lustre_qunit *qunit)
497 struct lustre_qunit_size *lqs;
499 lqs = quota_search_lqs(LQS_KEY(QDATA_IS_GRP(&qunit->lq_data),
500 qunit->lq_data.qd_id),
502 if (lqs && !IS_ERR(lqs)) {
503 cfs_spin_lock(&lqs->lqs_lock);
504 if (qunit->lq_opc == QUOTA_DQACQ)
505 quota_compute_lqs(&qunit->lq_data, lqs, 0, 1);
506 if (qunit->lq_opc == QUOTA_DQREL)
507 quota_compute_lqs(&qunit->lq_data, lqs, 0, 0);
508 cfs_spin_unlock(&lqs->lqs_lock);
509 /* this is for quota_search_lqs */
511 /* this is for schedule_dqacq */
516 static void remove_qunit_nolock(struct lustre_qunit *qunit)
518 LASSERT(!cfs_list_empty(&qunit->lq_hash));
519 LASSERT_SPIN_LOCKED(&qunit_hash_lock);
521 cfs_list_del_init(&qunit->lq_hash);
522 QUNIT_SET_STATE(qunit, QUNIT_RM_FROM_HASH);
526 void* quota_barrier(struct lustre_quota_ctxt *qctxt,
527 struct obd_quotactl *oqctl, int isblk)
529 struct lustre_qunit *qunit, *find_qunit;
532 OBD_SLAB_ALLOC(qunit, qunit_cachep, CFS_ALLOC_IO, sizeof(*qunit));
534 CERROR("locating %sunit failed for %sid %u\n",
535 isblk ? "b" : "i", oqctl->qc_type ? "g" : "u",
537 qctxt_wait_pending_dqacq(qctxt, oqctl->qc_id,
538 oqctl->qc_type, isblk);
542 CFS_INIT_LIST_HEAD(&qunit->lq_hash);
543 qunit->lq_lock = CFS_SPIN_LOCK_UNLOCKED;
544 cfs_waitq_init(&qunit->lq_waitq);
545 cfs_atomic_set(&qunit->lq_refcnt, 1);
546 qunit->lq_ctxt = qctxt;
547 qunit->lq_data.qd_id = oqctl->qc_id;
548 qunit->lq_data.qd_flags = oqctl->qc_type;
550 QDATA_SET_BLK(&qunit->lq_data);
551 QUNIT_SET_STATE_AND_RC(qunit, QUNIT_CREATED, 0);
552 /* it means it is only an invalid qunit for barrier */
553 qunit->lq_opc = QUOTA_LAST_OPC;
556 cfs_spin_lock(&qunit_hash_lock);
557 find_qunit = dqacq_in_flight(qctxt, &qunit->lq_data);
559 cfs_spin_unlock(&qunit_hash_lock);
560 qunit_put(find_qunit);
561 qctxt_wait_pending_dqacq(qctxt, oqctl->qc_id,
562 oqctl->qc_type, isblk);
563 CDEBUG(D_QUOTA, "cycle=%d\n", cycle++);
568 insert_qunit_nolock(qctxt, qunit);
569 cfs_spin_unlock(&qunit_hash_lock);
573 void quota_unbarrier(void *handle)
575 struct lustre_qunit *qunit = (struct lustre_qunit *)handle;
578 CERROR("handle is NULL\n");
582 LASSERT(qunit->lq_opc == QUOTA_LAST_OPC);
583 cfs_spin_lock(&qunit_hash_lock);
584 remove_qunit_nolock(qunit);
585 cfs_spin_unlock(&qunit_hash_lock);
586 QUNIT_SET_STATE_AND_RC(qunit, QUNIT_FINISHED, QUOTA_REQ_RETURNED);
587 cfs_waitq_signal(&qunit->lq_waitq);
591 #define INC_QLIMIT(limit, count) (limit == MIN_QLIMIT) ? \
592 (limit = count) : (limit += count)
595 static inline int is_master(struct lustre_quota_ctxt *qctxt)
597 return qctxt->lqc_handler ? 1 : 0;
601 schedule_dqacq(struct obd_device *obd, struct lustre_quota_ctxt *qctxt,
602 struct qunit_data *qdata, int opc, int wait,
603 struct obd_trans_info *oti);
605 static inline void qdata_to_oqaq(struct qunit_data *qdata,
606 struct quota_adjust_qunit *oqaq)
611 oqaq->qaq_flags = qdata->qd_flags;
612 oqaq->qaq_id = qdata->qd_id;
613 if (QDATA_IS_ADJBLK(qdata))
614 oqaq->qaq_bunit_sz = qdata->qd_qunit;
615 if (QDATA_IS_ADJINO(qdata))
616 oqaq->qaq_iunit_sz = qdata->qd_qunit;
620 dqacq_completion(struct obd_device *obd, struct lustre_quota_ctxt *qctxt,
621 struct qunit_data *qdata, int rc, int opc)
623 struct lustre_qunit *qunit = NULL;
624 struct super_block *sb = qctxt->lqc_sb;
626 struct quota_adjust_qunit *oqaq = NULL;
631 QDATA_DEBUG(qdata, "obd(%s): complete %s quota req\n",
632 obd->obd_name, (opc == QUOTA_DQACQ) ? "acq" : "rel");
634 /* do it only when a releasing quota req more than 5MB b=18491 */
635 if (opc == QUOTA_DQREL && qdata->qd_count >= 5242880)
636 OBD_FAIL_TIMEOUT(OBD_FAIL_QUOTA_DELAY_REL, 5);
638 /* update local operational quota file */
640 __u64 count = QUSG(qdata->qd_count, QDATA_IS_BLK(qdata));
641 struct obd_quotactl *qctl;
646 GOTO(out, err = -ENOMEM);
648 /* acq/rel qunit for specified uid/gid is serialized,
649 * so there is no race between get fs quota limit and
650 * set fs quota limit */
651 qctl->qc_cmd = Q_GETQUOTA;
652 qctl->qc_id = qdata->qd_id;
653 qctl->qc_type = QDATA_IS_GRP(qdata);
654 err = fsfilt_quotactl(obd, sb, qctl);
656 CERROR("error get quota fs limit! (rc:%d)\n", err);
660 if (QDATA_IS_BLK(qdata)) {
661 qctl->qc_dqblk.dqb_valid = QIF_BLIMITS;
662 hardlimit = &qctl->qc_dqblk.dqb_bhardlimit;
664 qctl->qc_dqblk.dqb_valid = QIF_ILIMITS;
665 hardlimit = &qctl->qc_dqblk.dqb_ihardlimit;
668 CDEBUG(D_QUOTA, "hardlimt: "LPU64"\n", *hardlimit);
675 INC_QLIMIT(*hardlimit, count);
678 LASSERTF(count < *hardlimit,
679 "id(%u) flag(%u) type(%c) isblk(%c) "
680 "count("LPU64") qd_qunit("LPU64") "
681 "hardlimit("LPU64").\n",
682 qdata->qd_id, qdata->qd_flags,
683 QDATA_IS_GRP(qdata) ? 'g' : 'u',
684 QDATA_IS_BLK(qdata) ? 'b': 'i',
685 qdata->qd_count, qdata->qd_qunit, *hardlimit);
692 /* clear quota limit */
696 qctl->qc_cmd = Q_SETQUOTA;
697 err = fsfilt_quotactl(obd, sb, qctl);
699 CERROR("error set quota fs limit! (rc:%d)\n", err);
701 QDATA_DEBUG(qdata, "%s completion\n",
702 opc == QUOTA_DQACQ ? "DQACQ" : "DQREL");
705 } else if (rc == -EDQUOT) {
706 QDATA_DEBUG(qdata, "acquire qunit got EDQUOT.\n");
707 } else if (rc == -EBUSY) {
708 QDATA_DEBUG(qdata, "it's is recovering, got EBUSY.\n");
710 CERROR("acquire qunit got error! (rc:%d)\n", rc);
713 /* remove the qunit from hash */
714 cfs_spin_lock(&qunit_hash_lock);
716 qunit = dqacq_in_flight(qctxt, qdata);
717 /* this qunit has been removed by qctxt_cleanup() */
719 cfs_spin_unlock(&qunit_hash_lock);
720 QDATA_DEBUG(qdata, "%s is discarded because qunit isn't found\n",
721 opc == QUOTA_DQACQ ? "DQACQ" : "DQREL");
725 LASSERT(opc == qunit->lq_opc);
726 /* remove this qunit from lq_hash so that new processes cannot be added
727 * to qunit->lq_waiters */
728 remove_qunit_nolock(qunit);
729 cfs_spin_unlock(&qunit_hash_lock);
731 compute_lqs_after_removing_qunit(qunit);
734 rc = QUOTA_REQ_RETURNED;
735 QUNIT_SET_STATE_AND_RC(qunit, QUNIT_FINISHED, rc);
736 /* wake up all waiters */
737 cfs_waitq_broadcast(&qunit->lq_waitq);
739 /* this is for dqacq_in_flight() */
741 if (rc < 0 && rc != -EDQUOT)
744 /* don't reschedule in such cases:
745 * - acq/rel failure and qunit isn't changed,
746 * but not for quota recovery.
747 * - local dqacq/dqrel.
748 * - local disk io failure.
752 GOTO(out1, err = -ENOMEM);
753 qdata_to_oqaq(qdata, oqaq);
754 /* adjust the qunit size in slaves */
755 rc1 = quota_adjust_slave_lqs(oqaq, qctxt);
758 CERROR("adjust slave's qunit size failed!(rc:%d)\n", rc1);
759 GOTO(out1, err = rc1);
761 if (err || (rc < 0 && rc != -EBUSY && rc1 == 0) || is_master(qctxt))
764 if (opc == QUOTA_DQREL && qdata->qd_count >= 5242880 &&
765 OBD_FAIL_CHECK(OBD_FAIL_QUOTA_DELAY_REL))
768 /* reschedule another dqacq/dqrel if needed */
770 qdata->qd_flags &= LQUOTA_QUNIT_FLAGS;
771 rc1 = check_cur_qunit(obd, qctxt, qdata);
774 opc = rc1 == 1 ? QUOTA_DQACQ : QUOTA_DQREL;
775 rc1 = schedule_dqacq(obd, qctxt, qdata, opc, 0, NULL);
776 QDATA_DEBUG(qdata, "reschedudle opc(%d) rc(%d)\n", opc, rc1);
779 /* this is for alloc_qunit() */
784 struct dqacq_async_args {
785 struct lustre_quota_ctxt *aa_ctxt;
786 struct lustre_qunit *aa_qunit;
789 static int dqacq_interpret(const struct lu_env *env,
790 struct ptlrpc_request *req, void *data, int rc)
792 struct dqacq_async_args *aa = (struct dqacq_async_args *)data;
793 struct lustre_quota_ctxt *qctxt = aa->aa_ctxt;
794 struct obd_device_target *obt = qctxt->lqc_obt;
795 struct lustre_qunit *qunit = aa->aa_qunit;
796 struct obd_device *obd = req->rq_import->imp_obd;
797 struct qunit_data *qdata = NULL;
801 LASSERT(req->rq_import);
803 cfs_down_read(&obt->obt_rwsem);
804 /* if a quota req timeouts or is dropped, we should update quota
805 * statistics which will be handled in dqacq_completion. And in
806 * this situation we should get qdata from request instead of
808 qdata = quota_get_qdata(req, (rc != 0) ? QUOTA_REQUEST : QUOTA_REPLY,
812 DEBUG_REQ(D_ERROR, req,
813 "error unpacking qunit_data(rc: %ld)\n",
815 qdata = &qunit->lq_data;
818 QDATA_DEBUG(qdata, "qdata: interpret rc(%d).\n", rc);
819 QDATA_DEBUG((&qunit->lq_data), "lq_data: \n");
821 if (qdata->qd_id != qunit->lq_data.qd_id ||
822 OBD_FAIL_CHECK(OBD_FAIL_QUOTA_RET_QDATA)) {
823 CERROR("the returned qd_id isn't expected!"
824 "(qdata: %u, lq_data: %u)\n", qdata->qd_id,
825 qunit->lq_data.qd_id);
826 qdata->qd_id = qunit->lq_data.qd_id;
829 if (QDATA_IS_GRP(qdata) != QDATA_IS_GRP(&qunit->lq_data)) {
830 CERROR("the returned grp/usr isn't expected!"
831 "(qdata: %u, lq_data: %u)\n", qdata->qd_flags,
832 qunit->lq_data.qd_flags);
833 if (QDATA_IS_GRP(&qunit->lq_data))
834 QDATA_SET_GRP(qdata);
836 QDATA_CLR_GRP(qdata);
839 if (qdata->qd_count > qunit->lq_data.qd_count) {
840 CERROR("the returned qd_count isn't expected!"
841 "(qdata: "LPU64", lq_data: "LPU64")\n", qdata->qd_count,
842 qunit->lq_data.qd_count);
846 if (unlikely(rc == -ESRCH))
847 CERROR("quota for %s has been enabled by master, but disabled "
848 "by slave.\n", QDATA_IS_GRP(qdata) ? "group" : "user");
850 rc = dqacq_completion(obd, qctxt, qdata, rc,
851 lustre_msg_get_opc(req->rq_reqmsg));
853 cfs_up_read(&obt->obt_rwsem);
858 * check if quota master is online
860 int check_qm(struct lustre_quota_ctxt *qctxt)
865 cfs_spin_lock(&qctxt->lqc_lock);
866 /* quit waiting when mds is back or qctxt is cleaned up */
867 rc = qctxt->lqc_import || !qctxt->lqc_valid;
868 cfs_spin_unlock(&qctxt->lqc_lock);
873 /* wake up all waiting threads when lqc_import is NULL */
874 void dqacq_interrupt(struct lustre_quota_ctxt *qctxt)
876 struct lustre_qunit *qunit, *tmp;
880 cfs_spin_lock(&qunit_hash_lock);
881 for (i = 0; i < NR_DQHASH; i++) {
882 cfs_list_for_each_entry_safe(qunit, tmp, &qunit_hash[i],
884 if (qunit->lq_ctxt != qctxt)
887 /* Wake up all waiters. Do not change lq_state.
888 * The waiters will check lq_rc which is kept as 0
889 * if no others change it, then the waiters will return
890 * -EAGAIN to caller who can perform related quota
891 * acq/rel if necessary. */
892 cfs_waitq_broadcast(&qunit->lq_waitq);
895 cfs_spin_unlock(&qunit_hash_lock);
899 static int got_qunit(struct lustre_qunit *qunit, int is_master)
901 struct lustre_quota_ctxt *qctxt = qunit->lq_ctxt;
905 cfs_spin_lock(&qunit->lq_lock);
906 switch (qunit->lq_state) {
908 case QUNIT_RM_FROM_HASH:
914 CERROR("invalid qunit state %d\n", qunit->lq_state);
916 cfs_spin_unlock(&qunit->lq_lock);
919 cfs_spin_lock(&qctxt->lqc_lock);
920 rc = !qctxt->lqc_valid;
922 rc |= !qctxt->lqc_import;
923 cfs_spin_unlock(&qctxt->lqc_lock);
930 revoke_lqs_rec(struct lustre_qunit_size *lqs, struct qunit_data *qdata, int opc)
932 /* revoke lqs_xxx_rec which is computed in check_cur_qunit
934 cfs_spin_lock(&lqs->lqs_lock);
935 quota_compute_lqs(qdata, lqs, 0, (opc == QUOTA_DQACQ) ? 1 : 0);
936 cfs_spin_unlock(&lqs->lqs_lock);
940 schedule_dqacq(struct obd_device *obd, struct lustre_quota_ctxt *qctxt,
941 struct qunit_data *qdata, int opc, int wait,
942 struct obd_trans_info *oti)
944 struct lustre_qunit *qunit, *empty;
945 struct l_wait_info lwi = { 0 };
946 struct ptlrpc_request *req;
947 struct dqacq_async_args *aa;
948 struct obd_import *imp = NULL;
949 struct lustre_qunit_size *lqs = NULL;
950 struct timeval work_start;
951 struct timeval work_end;
956 LASSERT(opc == QUOTA_DQACQ || opc == QUOTA_DQREL);
957 cfs_gettimeofday(&work_start);
959 lqs = quota_search_lqs(LQS_KEY(QDATA_IS_GRP(qdata), qdata->qd_id),
961 if (lqs == NULL || IS_ERR(lqs)) {
962 CERROR("Can't find the lustre qunit size!\n");
966 if ((empty = alloc_qunit(qctxt, qdata, opc)) == NULL) {
967 revoke_lqs_rec(lqs, qdata, opc);
968 /* this is for quota_search_lqs */
973 OBD_FAIL_TIMEOUT(OBD_FAIL_QUOTA_DELAY_SD, 5);
975 cfs_spin_lock(&qunit_hash_lock);
976 qunit = dqacq_in_flight(qctxt, qdata);
978 cfs_spin_unlock(&qunit_hash_lock);
981 revoke_lqs_rec(lqs, qdata, opc);
982 /* this is for quota_search_lqs */
984 goto wait_completion;
988 insert_qunit_nolock(qctxt, qunit);
989 cfs_spin_unlock(&qunit_hash_lock);
991 /* From here, the quota request will be sent anyway.
992 * When this qdata request returned or is cancelled,
993 * lqs_putref will be called at that time */
995 /* this is for quota_search_lqs */
998 QDATA_DEBUG(qdata, "obd(%s): send %s quota req\n",
999 obd->obd_name, (opc == QUOTA_DQACQ) ? "acq" : "rel");
1000 /* master is going to dqacq/dqrel from itself */
1001 if (is_master(qctxt)) {
1003 QDATA_DEBUG(qdata, "local %s.\n",
1004 opc == QUOTA_DQACQ ? "DQACQ" : "DQREL");
1005 QDATA_SET_CHANGE_QS(qdata);
1006 rc = qctxt->lqc_handler(obd, qdata, opc);
1007 rc2 = dqacq_completion(obd, qctxt, qdata, rc, opc);
1008 /* this is for qunit_get() */
1011 cfs_gettimeofday(&work_end);
1012 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
1013 if (opc == QUOTA_DQACQ)
1014 lprocfs_counter_add(qctxt->lqc_stats,
1015 wait ? LQUOTA_SYNC_ACQ : LQUOTA_ASYNC_ACQ,
1018 lprocfs_counter_add(qctxt->lqc_stats,
1019 wait ? LQUOTA_SYNC_REL : LQUOTA_ASYNC_REL,
1021 RETURN(rc ? rc : rc2);
1024 cfs_spin_lock(&qctxt->lqc_lock);
1025 if (!qctxt->lqc_import) {
1026 cfs_spin_unlock(&qctxt->lqc_lock);
1027 QDATA_DEBUG(qdata, "lqc_import is invalid.\n");
1029 cfs_spin_lock(&qunit_hash_lock);
1030 remove_qunit_nolock(qunit);
1031 cfs_spin_unlock(&qunit_hash_lock);
1033 compute_lqs_after_removing_qunit(qunit);
1035 QUNIT_SET_STATE_AND_RC(qunit, QUNIT_FINISHED, -EAGAIN);
1036 cfs_waitq_broadcast(&qunit->lq_waitq);
1038 /* this is for qunit_get() */
1040 /* this for alloc_qunit() */
1042 cfs_spin_lock(&qctxt->lqc_lock);
1043 if (wait && !qctxt->lqc_import) {
1044 cfs_spin_unlock(&qctxt->lqc_lock);
1045 LASSERT(oti && oti->oti_thread);
1046 /* The recovery thread doesn't have watchdog
1047 * attached. LU-369 */
1048 if (oti->oti_thread->t_watchdog)
1049 lc_watchdog_disable(oti->oti_thread->\
1051 CDEBUG(D_QUOTA, "sleep for quota master\n");
1052 l_wait_event(qctxt->lqc_wait_for_qmaster,
1053 check_qm(qctxt), &lwi);
1054 CDEBUG(D_QUOTA, "wake up when quota master is back\n");
1055 if (oti->oti_thread->t_watchdog)
1056 lc_watchdog_touch(oti->oti_thread->t_watchdog,
1057 CFS_GET_TIMEOUT(oti->oti_thread->t_svc));
1059 cfs_spin_unlock(&qctxt->lqc_lock);
1064 imp = class_import_get(qctxt->lqc_import);
1065 cfs_spin_unlock(&qctxt->lqc_lock);
1067 /* build dqacq/dqrel request */
1070 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_QUOTA_DQACQ,
1071 LUSTRE_MDS_VERSION, opc);
1072 class_import_put(imp);
1074 CERROR("Can't alloc request\n");
1075 dqacq_completion(obd, qctxt, qdata, -ENOMEM, opc);
1076 /* this is for qunit_get() */
1081 ptlrpc_request_set_replen(req);
1082 req->rq_no_resend = req->rq_no_delay = 1;
1083 rc = quota_copy_qdata(req, qdata, QUOTA_REQUEST, QUOTA_IMPORT);
1085 CERROR("Can't pack qunit_data(rc: %d)\n", rc);
1086 ptlrpc_req_finished(req);
1087 dqacq_completion(obd, qctxt, qdata, -EPROTO, opc);
1088 /* this is for qunit_get() */
1093 CLASSERT(sizeof(*aa) <= sizeof(req->rq_async_args));
1094 aa = ptlrpc_req_async_args(req);
1095 aa->aa_ctxt = qctxt;
1096 aa->aa_qunit = qunit;
1098 req->rq_interpret_reply = dqacq_interpret;
1099 ptlrpcd_add_req(req, PDL_POLICY_LOCAL, -1);
1101 QDATA_DEBUG(qdata, "%s scheduled.\n",
1102 opc == QUOTA_DQACQ ? "DQACQ" : "DQREL");
1104 if (wait && qunit) {
1105 struct qunit_data *p = &qunit->lq_data;
1107 QDATA_DEBUG(p, "qunit(%p) is waiting for dqacq.\n", qunit);
1108 l_wait_event(qunit->lq_waitq, got_qunit(qunit, is_master(qctxt)),
1110 /* rc = -EAGAIN, it means the quota master isn't ready yet
1111 * rc = QUOTA_REQ_RETURNED, it means a quota req is finished;
1112 * rc = -EDQUOT, it means out of quota
1113 * rc = -EBUSY, it means recovery is happening
1114 * other rc < 0, it means real errors, functions who call
1115 * schedule_dqacq should take care of this */
1116 cfs_spin_lock(&qunit->lq_lock);
1118 cfs_spin_unlock(&qunit->lq_lock);
1119 CDEBUG(D_QUOTA, "qunit(%p) finishes waiting: id(%u) flag(%u) "
1120 "rc(%d) owner(%d)\n", qunit, qunit->lq_data.qd_id,
1121 qunit->lq_data.qd_flags, rc, qunit->lq_owner);
1125 cfs_gettimeofday(&work_end);
1126 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
1127 if (opc == QUOTA_DQACQ)
1128 lprocfs_counter_add(qctxt->lqc_stats,
1129 wait ? LQUOTA_SYNC_ACQ : LQUOTA_ASYNC_ACQ,
1132 lprocfs_counter_add(qctxt->lqc_stats,
1133 wait ? LQUOTA_SYNC_REL : LQUOTA_ASYNC_REL,
1140 qctxt_adjust_qunit(struct obd_device *obd, struct lustre_quota_ctxt *qctxt,
1141 const unsigned int id[], __u32 isblk, int wait,
1142 struct obd_trans_info *oti)
1144 int rc = 0, i = USRQUOTA;
1145 struct qunit_data qdata[MAXQUOTAS];
1148 if (quota_is_set(obd, id, isblk ? QB_SET : QI_SET) == 0)
1151 for (i = 0; i < MAXQUOTAS; i++) {
1152 qdata[i].qd_id = id[i];
1153 qdata[i].qd_flags = i;
1155 QDATA_SET_BLK(&qdata[i]);
1156 qdata[i].qd_count = 0;
1158 rc = check_cur_qunit(obd, qctxt, &qdata[i]);
1161 /* need acquire or release */
1162 opc = rc == 1 ? QUOTA_DQACQ : QUOTA_DQREL;
1163 rc = schedule_dqacq(obd, qctxt, &qdata[i], opc,
1167 } else if (wait == 1) {
1168 /* when wait equates 1, that means mds_quota_acquire
1169 * or filter_quota_acquire is calling it. */
1170 rc = qctxt_wait_pending_dqacq(qctxt, id[i], i, isblk);
1180 qctxt_wait_pending_dqacq(struct lustre_quota_ctxt *qctxt, unsigned int id,
1181 unsigned short type, int isblk)
1183 struct lustre_qunit *qunit = NULL;
1184 struct qunit_data qdata;
1185 struct timeval work_start;
1186 struct timeval work_end;
1188 struct l_wait_info lwi = { 0 };
1192 cfs_gettimeofday(&work_start);
1194 qdata.qd_flags = type;
1196 QDATA_SET_BLK(&qdata);
1199 cfs_spin_lock(&qunit_hash_lock);
1200 qunit = dqacq_in_flight(qctxt, &qdata);
1201 cfs_spin_unlock(&qunit_hash_lock);
1204 struct qunit_data *p = &qunit->lq_data;
1206 QDATA_DEBUG(p, "qunit(%p) is waiting for dqacq.\n", qunit);
1207 l_wait_event(qunit->lq_waitq, got_qunit(qunit, is_master(qctxt)),
1209 CDEBUG(D_QUOTA, "qunit(%p) finishes waiting: rc(%d) "
1210 "owner(%d)\n", qunit, qunit->lq_rc, qunit->lq_owner);
1211 /* keep same as schedule_dqacq() b=17030 */
1212 cfs_spin_lock(&qunit->lq_lock);
1214 cfs_spin_unlock(&qunit->lq_lock);
1215 /* this is for dqacq_in_flight() */
1217 cfs_gettimeofday(&work_end);
1218 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
1219 lprocfs_counter_add(qctxt->lqc_stats,
1220 isblk ? LQUOTA_WAIT_PENDING_BLK_QUOTA :
1221 LQUOTA_WAIT_PENDING_INO_QUOTA,
1224 cfs_gettimeofday(&work_end);
1225 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
1226 lprocfs_counter_add(qctxt->lqc_stats,
1227 isblk ? LQUOTA_NOWAIT_PENDING_BLK_QUOTA :
1228 LQUOTA_NOWAIT_PENDING_INO_QUOTA,
1236 qctxt_init(struct obd_device *obd, dqacq_handler_t handler)
1238 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
1239 struct obd_device_target *obt = &obd->u.obt;
1240 struct super_block *sb = obt->obt_sb;
1246 rc = ptlrpcd_addref();
1250 cfs_waitq_init(&qctxt->lqc_wait_for_qmaster);
1251 cfs_waitq_init(&qctxt->lqc_lqs_waitq);
1252 cfs_atomic_set(&qctxt->lqc_lqs, 0);
1253 cfs_spin_lock_init(&qctxt->lqc_lock);
1254 cfs_spin_lock(&qctxt->lqc_lock);
1255 qctxt->lqc_handler = handler;
1257 qctxt->lqc_obt = obt;
1258 qctxt->lqc_import = NULL;
1259 qctxt->lqc_recovery = 0;
1260 qctxt->lqc_switch_qs = 1; /* Change qunit size in default setting */
1261 qctxt->lqc_valid = 1;
1262 qctxt->lqc_cqs_boundary_factor = 4;
1263 qctxt->lqc_cqs_least_bunit = PTLRPC_MAX_BRW_SIZE;
1264 qctxt->lqc_cqs_least_iunit = 2;
1265 qctxt->lqc_cqs_qs_factor = 2;
1266 qctxt->lqc_flags = 0;
1267 QUOTA_MASTER_UNREADY(qctxt);
1268 qctxt->lqc_bunit_sz = default_bunit_sz;
1269 qctxt->lqc_btune_sz = default_bunit_sz / 100 * default_btune_ratio;
1270 qctxt->lqc_iunit_sz = default_iunit_sz;
1271 qctxt->lqc_itune_sz = default_iunit_sz * default_itune_ratio / 100;
1272 qctxt->lqc_switch_seconds = 300; /* enlarging will wait 5 minutes
1273 * after the last shrinking */
1274 qctxt->lqc_sync_blk = 0;
1275 cfs_spin_unlock(&qctxt->lqc_lock);
1277 qctxt->lqc_lqs_hash = cfs_hash_create("LQS_HASH",
1280 min(hash_lqs_cur_bits,
1282 0, CFS_HASH_MIN_THETA,
1284 &lqs_hash_ops, CFS_HASH_DEFAULT);
1285 if (!qctxt->lqc_lqs_hash) {
1286 CERROR("initialize hash lqs for %s error!\n", obd->obd_name);
1291 rc = lquota_proc_setup(obd, is_master(qctxt));
1293 CERROR("initialize proc for %s error!\n", obd->obd_name);
1299 static int check_lqs(struct lustre_quota_ctxt *qctxt)
1304 rc = !cfs_atomic_read(&qctxt->lqc_lqs);
1309 int qctxt_del_lqs(cfs_hash_t *hs, cfs_hash_bd_t *bd,
1310 cfs_hlist_node_t *hnode, void *data)
1312 /* remove from hash and -1 refcount */
1313 cfs_hash_bd_del_locked(hs, bd, hnode);
1317 void qctxt_cleanup(struct lustre_quota_ctxt *qctxt, int force)
1319 struct lustre_qunit *qunit, *tmp;
1320 cfs_list_t tmp_list;
1321 struct l_wait_info lwi = { 0 };
1322 struct obd_device_target *obt = qctxt->lqc_obt;
1326 CFS_INIT_LIST_HEAD(&tmp_list);
1328 cfs_spin_lock(&qctxt->lqc_lock);
1329 qctxt->lqc_valid = 0;
1330 cfs_spin_unlock(&qctxt->lqc_lock);
1332 cfs_spin_lock(&qunit_hash_lock);
1333 for (i = 0; i < NR_DQHASH; i++) {
1334 cfs_list_for_each_entry_safe(qunit, tmp, &qunit_hash[i],
1336 if (qunit->lq_ctxt != qctxt)
1338 remove_qunit_nolock(qunit);
1339 cfs_list_add(&qunit->lq_hash, &tmp_list);
1342 cfs_spin_unlock(&qunit_hash_lock);
1344 cfs_list_for_each_entry_safe(qunit, tmp, &tmp_list, lq_hash) {
1345 cfs_list_del_init(&qunit->lq_hash);
1346 compute_lqs_after_removing_qunit(qunit);
1348 /* wake up all waiters */
1349 QUNIT_SET_STATE_AND_RC(qunit, QUNIT_FINISHED, 0);
1350 cfs_waitq_broadcast(&qunit->lq_waitq);
1354 /* after qctxt_cleanup, qctxt might be freed, then check_qm() is
1355 * unpredicted. So we must wait until lqc_wait_for_qmaster is empty */
1356 while (cfs_waitq_active(&qctxt->lqc_wait_for_qmaster)) {
1357 cfs_waitq_signal(&qctxt->lqc_wait_for_qmaster);
1358 cfs_schedule_timeout_and_set_state(CFS_TASK_INTERRUPTIBLE,
1359 cfs_time_seconds(1));
1362 /* release refcount on lustre_qunit_size holding by lqs_hash */
1363 cfs_hash_for_each_safe(qctxt->lqc_lqs_hash, qctxt_del_lqs, NULL);
1365 l_wait_event(qctxt->lqc_lqs_waitq, check_lqs(qctxt), &lwi);
1366 cfs_down_write(&obt->obt_rwsem);
1367 cfs_hash_putref(qctxt->lqc_lqs_hash);
1368 qctxt->lqc_lqs_hash = NULL;
1369 cfs_up_write(&obt->obt_rwsem);
1374 if (lquota_proc_cleanup(qctxt))
1375 CERROR("cleanup proc error!\n");
1381 struct qslave_recov_thread_data {
1382 struct obd_device *obd;
1383 struct lustre_quota_ctxt *qctxt;
1384 cfs_completion_t comp;
1387 /* FIXME only recovery block quota by now */
1388 static int qslave_recovery_main(void *arg)
1390 struct qslave_recov_thread_data *data = arg;
1391 struct obd_device *obd = data->obd;
1392 struct lustre_quota_ctxt *qctxt = data->qctxt;
1397 cfs_daemonize_ctxt("qslave_recovd");
1400 class_incref(obd, "qslave_recovd_filter", obd);
1402 cfs_complete(&data->comp);
1404 cfs_spin_lock(&qctxt->lqc_lock);
1405 if (qctxt->lqc_recovery) {
1406 cfs_spin_unlock(&qctxt->lqc_lock);
1407 class_decref(obd, "qslave_recovd_filter", obd);
1410 qctxt->lqc_recovery = 1;
1411 cfs_spin_unlock(&qctxt->lqc_lock);
1414 for (type = USRQUOTA; type < MAXQUOTAS; type++) {
1415 struct qunit_data qdata;
1416 struct quota_info *dqopt = sb_dqopt(qctxt->lqc_sb);
1418 struct dquot_id *dqid, *tmp;
1421 LOCK_DQONOFF_MUTEX(dqopt);
1422 if (!ll_sb_has_quota_active(qctxt->lqc_sb, type)) {
1423 UNLOCK_DQONOFF_MUTEX(dqopt);
1427 LASSERT(dqopt->files[type] != NULL);
1428 CFS_INIT_LIST_HEAD(&id_list);
1429 #ifndef KERNEL_SUPPORTS_QUOTA_READ
1430 rc = fsfilt_qids(obd, dqopt->files[type], NULL, type, &id_list);
1432 rc = fsfilt_qids(obd, NULL, dqopt->files[type], type, &id_list);
1434 UNLOCK_DQONOFF_MUTEX(dqopt);
1436 CERROR("Get ids from quota file failed. (rc:%d)\n", rc);
1438 cfs_list_for_each_entry_safe(dqid, tmp, &id_list, di_link) {
1439 cfs_list_del_init(&dqid->di_link);
1440 /* skip slave recovery on itself */
1441 if (is_master(qctxt))
1443 if (rc && rc != -EBUSY)
1446 qdata.qd_id = dqid->di_id;
1447 qdata.qd_flags = type;
1448 QDATA_SET_BLK(&qdata);
1451 ret = check_cur_qunit(obd, qctxt, &qdata);
1454 opc = ret == 1 ? QUOTA_DQACQ : QUOTA_DQREL;
1455 rc = schedule_dqacq(obd, qctxt, &qdata, opc,
1463 if (rc && rc != -EBUSY)
1464 CERROR("qslave recovery failed! (id:%d type:%d "
1465 " rc:%d)\n", dqid->di_id, type, rc);
1471 cfs_spin_lock(&qctxt->lqc_lock);
1472 qctxt->lqc_recovery = 0;
1473 cfs_spin_unlock(&qctxt->lqc_lock);
1474 class_decref(obd, "qslave_recovd_filter", obd);
1479 qslave_start_recovery(struct obd_device *obd, struct lustre_quota_ctxt *qctxt)
1481 struct qslave_recov_thread_data data;
1485 if (!ll_sb_any_quota_active(qctxt->lqc_sb))
1490 cfs_init_completion(&data.comp);
1492 rc = cfs_create_thread(qslave_recovery_main, &data,
1495 CERROR("Cannot start quota recovery thread: rc %d\n", rc);
1498 cfs_wait_for_completion(&data.comp);
1503 inline int quota_is_on(struct lustre_quota_ctxt *qctxt,
1504 struct obd_quotactl *oqctl)
1506 return ((qctxt->lqc_flags & UGQUOTA2LQC(oqctl->qc_type)) ==
1507 UGQUOTA2LQC(oqctl->qc_type));
1510 inline int quota_is_off(struct lustre_quota_ctxt *qctxt,
1511 struct obd_quotactl *oqctl)
1513 return !(qctxt->lqc_flags & UGQUOTA2LQC(oqctl->qc_type));
1517 * When quotaon, build a lqs for every uid/gid who has been set limitation
1518 * for quota. After quota_search_lqs, it will hold one ref for the lqs.
1519 * It will be released when qctxt_cleanup() is executed b=18574
1521 * Should be called with obt->obt_quotachecking held. b=20152
1523 void build_lqs(struct obd_device *obd)
1525 struct obd_device_target *obt = &obd->u.obt;
1526 struct lustre_quota_ctxt *qctxt = &obt->obt_qctxt;
1530 LASSERT_SEM_LOCKED(&obt->obt_quotachecking);
1531 CFS_INIT_LIST_HEAD(&id_list);
1532 for (i = 0; i < MAXQUOTAS; i++) {
1533 struct dquot_id *dqid, *tmp;
1535 if (sb_dqopt(qctxt->lqc_sb)->files[i] == NULL)
1538 #ifndef KERNEL_SUPPORTS_QUOTA_READ
1539 rc = fsfilt_qids(obd, sb_dqopt(qctxt->lqc_sb)->files[i], NULL,
1542 rc = fsfilt_qids(obd, NULL, sb_dqopt(qctxt->lqc_sb)->files[i],
1546 CERROR("%s: failed to get %s qids!\n", obd->obd_name,
1547 i ? "group" : "user");
1551 cfs_list_for_each_entry_safe(dqid, tmp, &id_list,
1553 struct lustre_qunit_size *lqs;
1555 cfs_list_del_init(&dqid->di_link);
1556 lqs = quota_search_lqs(LQS_KEY(i, dqid->di_id),
1558 if (lqs && !IS_ERR(lqs)) {
1559 lqs->lqs_flags |= dqid->di_flag;
1562 CERROR("%s: failed to create a lqs for %sid %u"
1563 "\n", obd->obd_name, i ? "g" : "u",
1573 * lqs<->qctxt hash operations
1577 * string hashing using djb2 hash algorithm
1580 lqs_hash(cfs_hash_t *hs, const void *key, unsigned mask)
1582 unsigned long long id;
1587 id = *((unsigned long long *)key);
1588 hash = (LQS_KEY_GRP(id) ? 5381 : 5387) * (unsigned)LQS_KEY_ID(id);
1590 RETURN(hash & mask);
1594 lqs_key(cfs_hlist_node_t *hnode)
1596 struct lustre_qunit_size *lqs;
1599 lqs = cfs_hlist_entry(hnode, struct lustre_qunit_size, lqs_hash);
1600 RETURN(&lqs->lqs_key);
1604 lqs_keycmp(const void *key, cfs_hlist_node_t *hnode)
1606 struct lustre_qunit_size *q =
1607 cfs_hlist_entry(hnode, struct lustre_qunit_size, lqs_hash);
1609 RETURN(q->lqs_key == *((unsigned long long *)key));
1613 lqs_object(cfs_hlist_node_t *hnode)
1615 return cfs_hlist_entry(hnode, struct lustre_qunit_size, lqs_hash);
1619 lqs_get(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
1621 struct lustre_qunit_size *q =
1622 cfs_hlist_entry(hnode, struct lustre_qunit_size, lqs_hash);
1628 lqs_put_locked(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
1630 struct lustre_qunit_size *q =
1631 cfs_hlist_entry(hnode, struct lustre_qunit_size, lqs_hash);
1637 lqs_exit(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
1639 CERROR("It should not have any item left to be handled by this!");
1642 static cfs_hash_ops_t lqs_hash_ops = {
1643 .hs_hash = lqs_hash,
1645 .hs_keycmp = lqs_keycmp,
1646 .hs_object = lqs_object,
1648 .hs_put_locked = lqs_put_locked,
1651 #endif /* HAVE_QUOTA_SUPPORT */