1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * lustre/quota/quota_context.c
7 * Copyright (c) 2001-2005 Cluster File Systems, Inc.
8 * Author: Niu YaWei <niu@clusterfs.com>
10 * This file is part of Lustre, http://www.lustre.org.
12 * No redistribution or use is permitted outside of Cluster File Systems, Inc.
16 # define EXPORT_SYMTAB
19 #define DEBUG_SUBSYSTEM S_MDS
21 #include <linux/version.h>
23 #include <asm/unistd.h>
24 #include <linux/slab.h>
25 #include <linux/quotaops.h>
26 #include <linux/module.h>
27 #include <linux/init.h>
29 #include <obd_class.h>
30 #include <lustre_quota.h>
31 #include <lustre_fsfilt.h>
32 #include "quota_internal.h"
34 unsigned long default_bunit_sz = 100 * 1024 * 1024; /* 100M bytes */
35 unsigned long default_btune_ratio = 50; /* 50 percentage */
36 unsigned long default_iunit_sz = 5000; /* 5000 inodes */
37 unsigned long default_itune_ratio = 50; /* 50 percentage */
39 cfs_mem_cache_t *qunit_cachep = NULL;
40 struct list_head qunit_hash[NR_DQHASH];
41 spinlock_t qunit_hash_lock = SPIN_LOCK_UNLOCKED;
44 struct list_head lq_hash; /* Hash list in memory */
45 atomic_t lq_refcnt; /* Use count */
46 struct lustre_quota_ctxt *lq_ctxt; /* Quota context this applies to */
47 struct qunit_data lq_data; /* See qunit_data */
48 unsigned int lq_opc; /* QUOTA_DQACQ, QUOTA_DQREL */
49 struct list_head lq_waiters; /* All write threads waiting for this qunit */
52 int should_translate_quota (struct obd_import *imp)
57 if ((imp->imp_connect_data.ocd_connect_flags & OBD_CONNECT_QUOTA64) &&
58 !OBD_FAIL_CHECK(OBD_FAIL_QUOTA_QD_COUNT_32BIT))
64 void qunit_cache_cleanup(void)
69 spin_lock(&qunit_hash_lock);
70 for (i = 0; i < NR_DQHASH; i++)
71 LASSERT(list_empty(qunit_hash + i));
72 spin_unlock(&qunit_hash_lock);
76 rc = cfs_mem_cache_destroy(qunit_cachep);
77 LASSERTF(rc == 0, "couldn't destory qunit_cache slab\n");
83 int qunit_cache_init(void)
88 LASSERT(qunit_cachep == NULL);
89 qunit_cachep = cfs_mem_cache_create("ll_qunit_cache",
90 sizeof(struct lustre_qunit),
95 spin_lock(&qunit_hash_lock);
96 for (i = 0; i < NR_DQHASH; i++)
97 INIT_LIST_HEAD(qunit_hash + i);
98 spin_unlock(&qunit_hash_lock);
103 qunit_hashfn(struct lustre_quota_ctxt *qctxt, struct qunit_data *qdata)
104 __attribute__((__const__));
107 qunit_hashfn(struct lustre_quota_ctxt *qctxt, struct qunit_data *qdata)
109 unsigned int id = qdata->qd_id;
110 unsigned int type = qdata->qd_flags & QUOTA_IS_GRP;
112 unsigned long tmp = ((unsigned long)qctxt >> L1_CACHE_SHIFT) ^ id;
113 tmp = (tmp * (MAXQUOTAS - type)) % NR_DQHASH;
117 /* compute the remaining quota for certain gid or uid b=11693 */
118 int compute_remquota(struct obd_device *obd,
119 struct lustre_quota_ctxt *qctxt, struct qunit_data *qdata)
121 struct super_block *sb = qctxt->lqc_sb;
123 struct obd_quotactl *qctl;
124 int ret = QUOTA_RET_OK;
125 __u32 qdata_type = qdata->qd_flags & QUOTA_IS_GRP;
128 if (!sb_any_quota_enabled(sb))
129 RETURN(QUOTA_RET_NOQUOTA);
131 /* ignore root user */
132 if (qdata->qd_id == 0 && qdata_type == USRQUOTA)
133 RETURN(QUOTA_RET_NOLIMIT);
139 /* get fs quota usage & limit */
140 qctl->qc_cmd = Q_GETQUOTA;
141 qctl->qc_id = qdata->qd_id;
142 qctl->qc_type = qdata_type;
143 ret = fsfilt_quotactl(obd, sb, qctl);
145 if (ret == -ESRCH) /* no limit */
146 ret = QUOTA_RET_NOLIMIT;
148 CDEBUG(D_QUOTA, "can't get fs quota usage! (rc:%d)",
153 usage = qctl->qc_dqblk.dqb_curspace;
154 limit = qctl->qc_dqblk.dqb_bhardlimit << QUOTABLOCK_BITS;
155 if (!limit){ /* no limit */
156 ret = QUOTA_RET_NOLIMIT;
161 qdata->qd_count = limit - usage;
170 /* caller must hold qunit_hash_lock */
171 static inline struct lustre_qunit *find_qunit(unsigned int hashent,
172 struct lustre_quota_ctxt *qctxt,
173 struct qunit_data *qdata)
175 struct lustre_qunit *qunit = NULL;
176 struct qunit_data *tmp;
178 LASSERT_SPIN_LOCKED(&qunit_hash_lock);
179 list_for_each_entry(qunit, qunit_hash + hashent, lq_hash) {
180 tmp = &qunit->lq_data;
181 if (qunit->lq_ctxt == qctxt &&
182 qdata->qd_id == tmp->qd_id && qdata->qd_flags == tmp->qd_flags)
188 /* check_cur_qunit - check the current usage of qunit.
189 * @qctxt: quota context
190 * @qdata: the type of quota unit to be checked
192 * return: 1 - need acquire qunit;
193 * 2 - need release qunit;
194 * 0 - need do nothing.
198 check_cur_qunit(struct obd_device *obd,
199 struct lustre_quota_ctxt *qctxt, struct qunit_data *qdata)
201 struct super_block *sb = qctxt->lqc_sb;
202 unsigned long qunit_sz, tune_sz;
204 struct obd_quotactl *qctl;
206 __u32 qdata_type = qdata->qd_flags & QUOTA_IS_GRP;
207 __u32 is_blk = (qdata->qd_flags & QUOTA_IS_BLOCK) >> 1;
210 if (!sb_any_quota_enabled(sb))
213 /* ignore root user */
214 if (qdata->qd_id == 0 && qdata_type == USRQUOTA)
221 /* get fs quota usage & limit */
222 qctl->qc_cmd = Q_GETQUOTA;
223 qctl->qc_id = qdata->qd_id;
224 qctl->qc_type = qdata_type;
225 ret = fsfilt_quotactl(obd, sb, qctl);
227 if (ret == -ESRCH) /* no limit */
230 CERROR("can't get fs quota usage! (rc:%d)\n", ret);
235 usage = qctl->qc_dqblk.dqb_curspace;
236 limit = qctl->qc_dqblk.dqb_bhardlimit << QUOTABLOCK_BITS;
237 qunit_sz = qctxt->lqc_bunit_sz;
238 tune_sz = qctxt->lqc_btune_sz;
240 LASSERT(!(qunit_sz % QUOTABLOCK_SIZE));
242 usage = qctl->qc_dqblk.dqb_curinodes;
243 limit = qctl->qc_dqblk.dqb_ihardlimit;
244 qunit_sz = qctxt->lqc_iunit_sz;
245 tune_sz = qctxt->lqc_itune_sz;
248 /* ignore the no quota limit case */
252 /* we don't count the MIN_QLIMIT */
253 if ((limit == MIN_QLIMIT && !is_blk) ||
254 (toqb(limit) == MIN_QLIMIT && is_blk))
257 LASSERT(qdata->qd_count == 0);
258 if (limit <= usage + tune_sz) {
259 while (qdata->qd_count + limit <= usage + tune_sz)
260 qdata->qd_count += qunit_sz;
262 } else if (limit > usage + qunit_sz + tune_sz) {
263 while (limit - qdata->qd_count > usage + qunit_sz + tune_sz)
264 qdata->qd_count += qunit_sz;
267 LASSERT(ret == 0 || qdata->qd_count);
274 /* caller must hold qunit_hash_lock */
275 static struct lustre_qunit *dqacq_in_flight(struct lustre_quota_ctxt *qctxt,
276 struct qunit_data *qdata)
278 unsigned int hashent = qunit_hashfn(qctxt, qdata);
279 struct lustre_qunit *qunit;
282 LASSERT_SPIN_LOCKED(&qunit_hash_lock);
283 qunit = find_qunit(hashent, qctxt, qdata);
287 static struct lustre_qunit *alloc_qunit(struct lustre_quota_ctxt *qctxt,
288 struct qunit_data *qdata, int opc)
290 struct lustre_qunit *qunit = NULL;
293 OBD_SLAB_ALLOC(qunit, qunit_cachep, CFS_ALLOC_IO, sizeof(*qunit));
297 INIT_LIST_HEAD(&qunit->lq_hash);
298 INIT_LIST_HEAD(&qunit->lq_waiters);
299 atomic_set(&qunit->lq_refcnt, 1);
300 qunit->lq_ctxt = qctxt;
301 memcpy(&qunit->lq_data, qdata, sizeof(*qdata));
307 static inline void free_qunit(struct lustre_qunit *qunit)
309 OBD_SLAB_FREE(qunit, qunit_cachep, sizeof(*qunit));
312 static inline void qunit_get(struct lustre_qunit *qunit)
314 atomic_inc(&qunit->lq_refcnt);
317 static void qunit_put(struct lustre_qunit *qunit)
319 LASSERT(atomic_read(&qunit->lq_refcnt));
320 if (atomic_dec_and_test(&qunit->lq_refcnt))
325 insert_qunit_nolock(struct lustre_quota_ctxt *qctxt, struct lustre_qunit *qunit)
327 struct list_head *head;
329 LASSERT(list_empty(&qunit->lq_hash));
330 head = qunit_hash + qunit_hashfn(qctxt, &qunit->lq_data);
331 list_add(&qunit->lq_hash, head);
334 static void remove_qunit_nolock(struct lustre_qunit *qunit)
336 LASSERT(!list_empty(&qunit->lq_hash));
337 list_del_init(&qunit->lq_hash);
340 struct qunit_waiter {
341 struct list_head qw_entry;
342 cfs_waitq_t qw_waitq;
346 #define INC_QLIMIT(limit, count) (limit == MIN_QLIMIT) ? \
347 (limit = count) : (limit += count)
350 /* FIXME check if this mds is the master of specified id */
352 is_master(struct obd_device *obd, struct lustre_quota_ctxt *qctxt,
353 unsigned int id, int type)
355 return qctxt->lqc_handler ? 1 : 0;
359 schedule_dqacq(struct obd_device *obd, struct lustre_quota_ctxt *qctxt,
360 struct qunit_data *qdata, int opc, int wait);
362 static int split_before_schedule_dqacq(struct obd_device *obd, struct lustre_quota_ctxt *qctxt,
363 struct qunit_data *qdata, int opc, int wait)
366 struct qunit_data tmp_qdata;
370 if (qctxt->lqc_import)
371 while (should_translate_quota(qctxt->lqc_import) &&
372 qdata->qd_count > MAX_QUOTA_COUNT32) {
375 tmp_qdata.qd_count = MAX_QUOTA_COUNT32;
376 qdata->qd_count -= tmp_qdata.qd_count;
377 ret = schedule_dqacq(obd, qctxt, &tmp_qdata, opc, wait);
382 if (qdata->qd_count){
383 ret = schedule_dqacq(obd, qctxt, qdata, opc, wait);
392 dqacq_completion(struct obd_device *obd,
393 struct lustre_quota_ctxt *qctxt,
394 struct qunit_data *qdata, int rc, int opc)
396 struct lustre_qunit *qunit = NULL;
397 struct super_block *sb = qctxt->lqc_sb;
398 unsigned long qunit_sz;
399 struct qunit_waiter *qw, *tmp;
401 __u32 qdata_type = qdata->qd_flags & QUOTA_IS_GRP;
402 __u32 is_blk = (qdata->qd_flags & QUOTA_IS_BLOCK) >> 1;
403 __u64 qd_tmp = qdata->qd_count;
408 qunit_sz = is_blk ? qctxt->lqc_bunit_sz : qctxt->lqc_iunit_sz;
409 div_r = do_div(qd_tmp, qunit_sz);
412 /* update local operational quota file */
414 __u32 count = QUSG(qdata->qd_count, is_blk);
415 struct obd_quotactl *qctl;
420 GOTO(out, err = -ENOMEM);
422 /* acq/rel qunit for specified uid/gid is serialized,
423 * so there is no race between get fs quota limit and
424 * set fs quota limit */
425 qctl->qc_cmd = Q_GETQUOTA;
426 qctl->qc_id = qdata->qd_id;
427 qctl->qc_type = qdata_type;
428 err = fsfilt_quotactl(obd, sb, qctl);
430 CERROR("error get quota fs limit! (rc:%d)\n", err);
435 qctl->qc_dqblk.dqb_valid = QIF_BLIMITS;
436 hardlimit = &qctl->qc_dqblk.dqb_bhardlimit;
438 qctl->qc_dqblk.dqb_valid = QIF_ILIMITS;
439 hardlimit = &qctl->qc_dqblk.dqb_ihardlimit;
444 INC_QLIMIT(*hardlimit, count);
447 LASSERT(count < *hardlimit);
454 /* clear quota limit */
458 qctl->qc_cmd = Q_SETQUOTA;
459 err = fsfilt_quotactl(obd, sb, qctl);
461 CERROR("error set quota fs limit! (rc:%d)\n", err);
463 QDATA_DEBUG(qdata, "%s completion\n",
464 opc == QUOTA_DQACQ ? "DQACQ" : "DQREL");
467 } else if (rc == -EDQUOT) {
468 QDATA_DEBUG(qdata, "acquire qunit got EDQUOT.\n");
469 } else if (rc == -EBUSY) {
470 QDATA_DEBUG(qdata, "it's is recovering, got EBUSY.\n");
472 CERROR("acquire qunit got error! (rc:%d)\n", rc);
475 /* remove the qunit from hash */
476 spin_lock(&qunit_hash_lock);
478 qunit = dqacq_in_flight(qctxt, qdata);
479 /* this qunit has been removed by qctxt_cleanup() */
481 spin_unlock(&qunit_hash_lock);
485 LASSERT(opc == qunit->lq_opc);
486 remove_qunit_nolock(qunit);
488 /* wake up all waiters */
489 list_for_each_entry_safe(qw, tmp, &qunit->lq_waiters, qw_entry) {
490 list_del_init(&qw->qw_entry);
492 wake_up(&qw->qw_waitq);
495 spin_unlock(&qunit_hash_lock);
499 /* don't reschedule in such cases:
500 * - acq/rel failure, but not for quota recovery.
501 * - local dqacq/dqrel.
502 * - local disk io failure.
504 if (err || (rc && rc != -EBUSY) ||
505 is_master(obd, qctxt, qdata->qd_id, qdata_type))
508 /* reschedule another dqacq/dqrel if needed */
510 rc = check_cur_qunit(obd, qctxt, qdata);
513 opc = rc == 1 ? QUOTA_DQACQ : QUOTA_DQREL;
514 rc = split_before_schedule_dqacq(obd, qctxt, qdata, opc, 0);
515 QDATA_DEBUG(qdata, "reschedudle opc(%d) rc(%d)\n", opc, rc);
520 struct dqacq_async_args {
521 struct lustre_quota_ctxt *aa_ctxt;
522 struct lustre_qunit *aa_qunit;
525 static int dqacq_interpret(struct ptlrpc_request *req, void *data, int rc)
527 struct dqacq_async_args *aa = (struct dqacq_async_args *)data;
528 struct lustre_quota_ctxt *qctxt = aa->aa_ctxt;
529 struct lustre_qunit *qunit = aa->aa_qunit;
530 struct obd_device *obd = req->rq_import->imp_obd;
531 struct qunit_data *qdata = NULL;
532 struct qunit_data_old *qdata_old = NULL;
536 LASSERT(req->rq_import);
537 if ((req->rq_import->imp_connect_data.ocd_connect_flags & OBD_CONNECT_QUOTA64) &&
538 !OBD_FAIL_CHECK(OBD_FAIL_QUOTA_QD_COUNT_32BIT)) {
539 CDEBUG(D_QUOTA, "qd_count is 64bit!\n");
540 qdata = lustre_swab_reqbuf(req, REPLY_REC_OFF, sizeof(*qdata), lustre_swab_qdata);
542 CDEBUG(D_QUOTA, "qd_count is 32bit!\n");
543 qdata_old = lustre_swab_reqbuf(req, REPLY_REC_OFF, sizeof(struct qunit_data_old),
544 lustre_swab_qdata_old);
545 qdata = lustre_quota_old_to_new(qdata_old);
548 DEBUG_REQ(D_ERROR, req, "error unpacking qunit_data");
552 LASSERT(qdata->qd_id == qunit->lq_data.qd_id &&
553 (qdata->qd_flags & QUOTA_IS_GRP) == (qunit->lq_data.qd_flags & QUOTA_IS_GRP) &&
554 (qdata->qd_count == qunit->lq_data.qd_count ||
555 qdata->qd_count == 0));
557 QDATA_DEBUG(qdata, "%s interpret rc(%d).\n",
558 lustre_msg_get_opc(req->rq_reqmsg) == QUOTA_DQACQ ?
559 "DQACQ" : "DQREL", rc);
561 rc = dqacq_completion(obd, qctxt, qdata, rc,
562 lustre_msg_get_opc(req->rq_reqmsg));
567 static int got_qunit(struct qunit_waiter *waiter)
571 spin_lock(&qunit_hash_lock);
572 rc = list_empty(&waiter->qw_entry);
573 spin_unlock(&qunit_hash_lock);
578 schedule_dqacq(struct obd_device *obd,
579 struct lustre_quota_ctxt *qctxt,
580 struct qunit_data *qdata, int opc, int wait)
582 struct lustre_qunit *qunit, *empty;
583 struct qunit_waiter qw;
584 struct l_wait_info lwi = { 0 };
585 struct ptlrpc_request *req;
586 struct qunit_data *reqdata;
587 struct dqacq_async_args *aa;
588 int size[2] = { sizeof(struct ptlrpc_body), sizeof(*reqdata) };
592 INIT_LIST_HEAD(&qw.qw_entry);
593 init_waitqueue_head(&qw.qw_waitq);
596 if ((empty = alloc_qunit(qctxt, qdata, opc)) == NULL)
599 spin_lock(&qunit_hash_lock);
601 qunit = dqacq_in_flight(qctxt, qdata);
604 list_add_tail(&qw.qw_entry, &qunit->lq_waiters);
605 spin_unlock(&qunit_hash_lock);
608 goto wait_completion;
611 insert_qunit_nolock(qctxt, qunit);
613 list_add_tail(&qw.qw_entry, &qunit->lq_waiters);
614 spin_unlock(&qunit_hash_lock);
618 /* master is going to dqacq/dqrel from itself */
619 if (is_master(obd, qctxt, qdata->qd_id, qdata->qd_flags & QUOTA_IS_GRP)) {
621 QDATA_DEBUG(qdata, "local %s.\n",
622 opc == QUOTA_DQACQ ? "DQACQ" : "DQREL");
623 rc = qctxt->lqc_handler(obd, qdata, opc);
624 rc2 = dqacq_completion(obd, qctxt, qdata, rc, opc);
625 RETURN((rc && rc != -EDQUOT) ? rc : rc2);
628 /* build dqacq/dqrel request */
629 LASSERT(qctxt->lqc_import);
630 req = ptlrpc_prep_req(qctxt->lqc_import, LUSTRE_MDS_VERSION, opc, 2,
633 dqacq_completion(obd, qctxt, qdata, -ENOMEM, opc);
637 LASSERT(!should_translate_quota(qctxt->lqc_import) ||
638 qdata->qd_count <= MAX_QUOTA_COUNT32);
639 if (should_translate_quota(qctxt->lqc_import))
641 struct qunit_data_old *reqdata_old, *tmp;
643 reqdata_old = lustre_msg_buf(req->rq_reqmsg, REPLY_REC_OFF,
644 sizeof(*reqdata_old));
645 tmp = lustre_quota_new_to_old(qdata);
647 size[1] = sizeof(*reqdata_old);
648 CDEBUG(D_QUOTA, "qd_count is 32bit!\n");
650 reqdata = lustre_msg_buf(req->rq_reqmsg, REPLY_REC_OFF,
653 size[1] = sizeof(*reqdata);
654 CDEBUG(D_QUOTA, "qd_count is 64bit!\n");
656 ptlrpc_req_set_repsize(req, 2, size);
658 CLASSERT(sizeof(*aa) <= sizeof(req->rq_async_args));
659 aa = (struct dqacq_async_args *)&req->rq_async_args;
661 aa->aa_qunit = qunit;
663 req->rq_interpret_reply = dqacq_interpret;
664 ptlrpcd_add_req(req);
666 QDATA_DEBUG(qdata, "%s scheduled.\n",
667 opc == QUOTA_DQACQ ? "DQACQ" : "DQREL");
670 struct qunit_data *p = &qunit->lq_data;
671 QDATA_DEBUG(p, "wait for dqacq.\n");
673 l_wait_event(qw.qw_waitq, got_qunit(&qw), &lwi);
677 CDEBUG(D_QUOTA, "wait dqacq done. (rc:%d)\n", qw.qw_rc);
683 qctxt_adjust_qunit(struct obd_device *obd, struct lustre_quota_ctxt *qctxt,
684 uid_t uid, gid_t gid, __u32 isblk, int wait)
686 int ret, rc = 0, i = USRQUOTA;
687 __u32 id[MAXQUOTAS] = { uid, gid };
688 struct qunit_data qdata[MAXQUOTAS];
691 CLASSERT(MAXQUOTAS < 4);
692 if (!sb_any_quota_enabled(qctxt->lqc_sb))
695 for (i = 0; i < MAXQUOTAS; i++) {
696 qdata[i].qd_id = id[i];
697 qdata[i].qd_flags = 0;
698 qdata[i].qd_flags |= i;
699 qdata[i].qd_flags |= isblk ? QUOTA_IS_BLOCK : 0;
700 qdata[i].qd_count = 0;
702 ret = check_cur_qunit(obd, qctxt, &qdata[i]);
705 /* need acquire or release */
706 opc = ret == 1 ? QUOTA_DQACQ : QUOTA_DQREL;
707 ret = split_before_schedule_dqacq(obd, qctxt, &qdata[i],
718 qctxt_wait_pending_dqacq(struct lustre_quota_ctxt *qctxt, unsigned int id,
719 unsigned short type, int isblk)
721 struct lustre_qunit *qunit = NULL;
722 struct qunit_waiter qw;
723 struct qunit_data qdata;
724 struct l_wait_info lwi = { 0 };
727 INIT_LIST_HEAD(&qw.qw_entry);
728 init_waitqueue_head(&qw.qw_waitq);
733 qdata.qd_flags |= type;
734 qdata.qd_flags |= isblk ? QUOTA_IS_BLOCK : 0;
737 spin_lock(&qunit_hash_lock);
739 qunit = dqacq_in_flight(qctxt, &qdata);
741 list_add_tail(&qw.qw_entry, &qunit->lq_waiters);
743 spin_unlock(&qunit_hash_lock);
746 struct qunit_data *p = &qdata;
747 QDATA_DEBUG(p, "wait for dqacq completion.\n");
748 l_wait_event(qw.qw_waitq, got_qunit(&qw), &lwi);
749 QDATA_DEBUG(p, "wait dqacq done. (rc:%d)\n", qw.qw_rc);
755 qctxt_init(struct lustre_quota_ctxt *qctxt, struct super_block *sb,
756 dqacq_handler_t handler)
761 rc = ptlrpcd_addref();
765 qctxt->lqc_handler = handler;
767 qctxt->lqc_import = NULL;
768 qctxt->lqc_recovery = 0;
769 qctxt->lqc_atype = 0;
770 qctxt->lqc_status= 0;
771 qctxt->lqc_bunit_sz = default_bunit_sz;
772 qctxt->lqc_btune_sz = default_bunit_sz / 100 * default_btune_ratio;
773 qctxt->lqc_iunit_sz = default_iunit_sz;
774 qctxt->lqc_itune_sz = default_iunit_sz * default_itune_ratio / 100;
779 void qctxt_cleanup(struct lustre_quota_ctxt *qctxt, int force)
781 struct lustre_qunit *qunit, *tmp;
782 struct qunit_waiter *qw, *tmp2;
786 spin_lock(&qunit_hash_lock);
788 for (i = 0; i < NR_DQHASH; i++) {
789 list_for_each_entry_safe(qunit, tmp, &qunit_hash[i], lq_hash) {
790 if (qunit->lq_ctxt != qctxt)
793 remove_qunit_nolock(qunit);
794 /* wake up all waiters */
795 list_for_each_entry_safe(qw, tmp2, &qunit->lq_waiters,
797 list_del_init(&qw->qw_entry);
799 wake_up(&qw->qw_waitq);
805 spin_unlock(&qunit_hash_lock);
812 struct qslave_recov_thread_data {
813 struct obd_device *obd;
814 struct lustre_quota_ctxt *qctxt;
815 struct completion comp;
818 /* FIXME only recovery block quota by now */
819 static int qslave_recovery_main(void *arg)
821 struct qslave_recov_thread_data *data = arg;
822 struct obd_device *obd = data->obd;
823 struct lustre_quota_ctxt *qctxt = data->qctxt;
828 ptlrpc_daemonize("qslave_recovd");
830 complete(&data->comp);
832 if (qctxt->lqc_recovery)
834 qctxt->lqc_recovery = 1;
836 for (type = USRQUOTA; type < MAXQUOTAS; type++) {
837 struct qunit_data qdata;
838 struct quota_info *dqopt = sb_dqopt(qctxt->lqc_sb);
839 struct list_head id_list;
840 struct dquot_id *dqid, *tmp;
843 LOCK_DQONOFF_MUTEX(dqopt);
844 if (!sb_has_quota_enabled(qctxt->lqc_sb, type)) {
845 UNLOCK_DQONOFF_MUTEX(dqopt);
849 LASSERT(dqopt->files[type] != NULL);
850 INIT_LIST_HEAD(&id_list);
851 #ifndef KERNEL_SUPPORTS_QUOTA_READ
852 rc = fsfilt_qids(obd, dqopt->files[type], NULL, type, &id_list);
854 rc = fsfilt_qids(obd, NULL, dqopt->files[type], type, &id_list);
856 UNLOCK_DQONOFF_MUTEX(dqopt);
858 CERROR("Get ids from quota file failed. (rc:%d)\n", rc);
860 list_for_each_entry_safe(dqid, tmp, &id_list, di_link) {
861 list_del_init(&dqid->di_link);
862 /* skip slave recovery on itself */
863 if (is_master(obd, qctxt, dqid->di_id, type))
865 if (rc && rc != -EBUSY)
868 qdata.qd_id = dqid->di_id;
870 qdata.qd_flags |= type;
871 qdata.qd_flags |= QUOTA_IS_BLOCK;
874 ret = check_cur_qunit(obd, qctxt, &qdata);
877 opc = ret == 1 ? QUOTA_DQACQ : QUOTA_DQREL;
878 rc = split_before_schedule_dqacq(obd, qctxt, &qdata, opc, 0);
883 CDEBUG(rc == -EBUSY ? D_QUOTA : D_ERROR,
884 "qslave recovery failed! (id:%d type:%d "
885 " rc:%d)\n", dqid->di_id, type, rc);
891 qctxt->lqc_recovery = 0;
896 qslave_start_recovery(struct obd_device *obd, struct lustre_quota_ctxt *qctxt)
898 struct qslave_recov_thread_data data;
902 if (!sb_any_quota_enabled(qctxt->lqc_sb))
907 init_completion(&data.comp);
909 rc = kernel_thread(qslave_recovery_main, &data, CLONE_VM|CLONE_FILES);
911 CERROR("Cannot start quota recovery thread: rc %d\n", rc);
914 wait_for_completion(&data.comp);