4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Whamcloud, Inc.
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>
43 #define DEBUG_SUBSYSTEM S_LQUOTA
45 #include <linux/version.h>
47 #include <asm/unistd.h>
48 #include <linux/slab.h>
49 #include <linux/quotaops.h>
50 #include <linux/module.h>
51 #include <linux/init.h>
53 #include <obd_class.h>
54 #include <lustre_quota.h>
55 #include <lustre_fsfilt.h>
56 #include <lprocfs_status.h>
57 #include "quota_internal.h"
59 static int hash_lqs_cur_bits = HASH_LQS_CUR_BITS;
60 CFS_MODULE_PARM(hash_lqs_cur_bits, "i", int, 0444,
61 "the current bits of lqs hash");
63 static cfs_hash_ops_t lqs_hash_ops;
65 unsigned long default_bunit_sz = 128 * 1024 * 1024; /* 128M bytes */
66 unsigned long default_btune_ratio = 50; /* 50 percentage */
67 unsigned long default_iunit_sz = 5120; /* 5120 inodes */
68 unsigned long default_itune_ratio = 50; /* 50 percentage */
70 cfs_mem_cache_t *qunit_cachep = NULL;
71 cfs_list_t qunit_hash[NR_DQHASH];
72 cfs_spinlock_t qunit_hash_lock = CFS_SPIN_LOCK_UNLOCKED;
74 /* please sync qunit_state with qunit_state_names */
81 * a qunit is added into qunit hash, that means
82 * a quota req will be sent or is flying
86 * a qunit is removed from qunit hash, that
87 * means a quota req is handled and comes back
89 QUNIT_RM_FROM_HASH = 2,
91 * qunit can wake up all threads waiting for it
96 static const char *qunit_state_names[] = {
97 [QUNIT_CREATED] = "CREATED",
98 [QUNIT_IN_HASH] = "IN_HASH",
99 [QUNIT_RM_FROM_HASH] = "RM_FROM_HASH",
100 [QUNIT_FINISHED] = "FINISHED",
103 struct lustre_qunit {
104 cfs_list_t lq_hash; /** Hash list in memory */
105 cfs_atomic_t lq_refcnt; /** Use count */
106 struct lustre_quota_ctxt *lq_ctxt; /** Quota context this applies to */
107 struct qunit_data lq_data; /** See qunit_data */
108 unsigned int lq_opc; /** QUOTA_DQACQ, QUOTA_DQREL */
109 cfs_waitq_t lq_waitq; /** Threads waiting for this qunit */
110 cfs_spinlock_t lq_lock; /** Protect the whole structure */
111 enum qunit_state lq_state; /** Present the status of qunit */
112 int lq_rc; /** The rc of lq_data */
116 #define QUNIT_SET_STATE(qunit, state) \
118 cfs_spin_lock(&qunit->lq_lock); \
119 QDATA_DEBUG((&qunit->lq_data), "qunit(%p) lq_state(%s->%s), " \
120 "lq_rc(%d), lq_owner(%d)\n", \
121 qunit, qunit_state_names[qunit->lq_state], \
122 qunit_state_names[state], qunit->lq_rc, \
124 qunit->lq_state = state; \
125 cfs_spin_unlock(&qunit->lq_lock); \
128 #define QUNIT_SET_STATE_AND_RC(qunit, state, rc) \
130 cfs_spin_lock(&qunit->lq_lock); \
132 QDATA_DEBUG((&qunit->lq_data), "qunit(%p) lq_state(%s->%s), " \
133 "lq_rc(%d), lq_owner(%d)\n", \
134 qunit, qunit_state_names[qunit->lq_state], \
135 qunit_state_names[state], qunit->lq_rc, \
137 qunit->lq_state = state; \
138 cfs_spin_unlock(&qunit->lq_lock); \
141 int should_translate_quota (struct obd_import *imp)
146 if (imp->imp_connect_data.ocd_connect_flags & OBD_CONNECT_QUOTA64)
152 void qunit_cache_cleanup(void)
157 cfs_spin_lock(&qunit_hash_lock);
158 for (i = 0; i < NR_DQHASH; i++)
159 LASSERT(cfs_list_empty(qunit_hash + i));
160 cfs_spin_unlock(&qunit_hash_lock);
164 rc = cfs_mem_cache_destroy(qunit_cachep);
165 LASSERTF(rc == 0, "couldn't destroy qunit_cache slab\n");
171 int qunit_cache_init(void)
176 LASSERT(qunit_cachep == NULL);
177 qunit_cachep = cfs_mem_cache_create("ll_qunit_cache",
178 sizeof(struct lustre_qunit),
183 cfs_spin_lock(&qunit_hash_lock);
184 for (i = 0; i < NR_DQHASH; i++)
185 CFS_INIT_LIST_HEAD(qunit_hash + i);
186 cfs_spin_unlock(&qunit_hash_lock);
191 qunit_hashfn(struct lustre_quota_ctxt *qctxt, struct qunit_data *qdata)
192 __attribute__((__const__));
195 qunit_hashfn(struct lustre_quota_ctxt *qctxt, struct qunit_data *qdata)
197 unsigned int id = qdata->qd_id;
198 unsigned int type = QDATA_IS_GRP(qdata);
200 unsigned long tmp = ((unsigned long)qctxt >> L1_CACHE_SHIFT) ^ id;
201 tmp = (tmp * (MAXQUOTAS - type)) % NR_DQHASH;
205 /* caller must hold qunit_hash_lock */
206 static inline struct lustre_qunit *find_qunit(unsigned int hashent,
207 struct lustre_quota_ctxt *qctxt,
208 struct qunit_data *qdata)
210 struct lustre_qunit *qunit = NULL;
211 struct qunit_data *tmp;
213 LASSERT_SPIN_LOCKED(&qunit_hash_lock);
214 cfs_list_for_each_entry(qunit, qunit_hash + hashent, lq_hash) {
215 tmp = &qunit->lq_data;
216 if (qunit->lq_ctxt == qctxt &&
217 qdata->qd_id == tmp->qd_id &&
218 (qdata->qd_flags & LQUOTA_QUNIT_FLAGS) ==
219 (tmp->qd_flags & LQUOTA_QUNIT_FLAGS))
225 /* check_cur_qunit - check the current usage of qunit.
226 * @qctxt: quota context
227 * @qdata: the type of quota unit to be checked
229 * return: 1 - need acquire qunit;
230 * 2 - need release qunit;
231 * 0 - need do nothing.
235 check_cur_qunit(struct obd_device *obd,
236 struct lustre_quota_ctxt *qctxt, struct qunit_data *qdata)
238 struct super_block *sb = qctxt->lqc_sb;
239 unsigned long qunit_sz, tune_sz;
240 __u64 usage, limit, limit_org, pending_write = 0;
241 long long record = 0;
242 struct obd_quotactl *qctl;
243 struct lustre_qunit_size *lqs = NULL;
247 if (!ll_sb_any_quota_active(sb))
250 cfs_spin_lock(&qctxt->lqc_lock);
251 if (!qctxt->lqc_valid){
252 cfs_spin_unlock(&qctxt->lqc_lock);
255 cfs_spin_unlock(&qctxt->lqc_lock);
261 /* get fs quota usage & limit */
262 qctl->qc_cmd = Q_GETQUOTA;
263 qctl->qc_id = qdata->qd_id;
264 qctl->qc_type = QDATA_IS_GRP(qdata);
265 ret = fsfilt_quotactl(obd, sb, qctl);
267 if (ret == -ESRCH) /* no limit */
270 CERROR("can't get fs quota usage! (rc:%d)\n", ret);
274 if (QDATA_IS_BLK(qdata)) {
275 usage = qctl->qc_dqblk.dqb_curspace;
276 limit = qctl->qc_dqblk.dqb_bhardlimit << QUOTABLOCK_BITS;
278 usage = qctl->qc_dqblk.dqb_curinodes;
279 limit = qctl->qc_dqblk.dqb_ihardlimit;
282 /* ignore the no quota limit case; and it can avoid creating
283 * unnecessary lqs for uid/gid */
287 lqs = quota_search_lqs(LQS_KEY(QDATA_IS_GRP(qdata), qdata->qd_id),
289 if (IS_ERR(lqs) || lqs == NULL) {
290 CERROR("fail to find a lqs for %sid: %u)!\n",
291 QDATA_IS_GRP(qdata) ? "g" : "u", qdata->qd_id);
294 cfs_spin_lock(&lqs->lqs_lock);
296 if (QDATA_IS_BLK(qdata)) {
297 qunit_sz = lqs->lqs_bunit_sz;
298 tune_sz = lqs->lqs_btune_sz;
299 pending_write = lqs->lqs_bwrite_pending;
300 record = lqs->lqs_blk_rec;
301 LASSERT(!(qunit_sz % QUOTABLOCK_SIZE));
303 /* we didn't need change inode qunit size now */
304 qunit_sz = lqs->lqs_iunit_sz;
305 tune_sz = lqs->lqs_itune_sz;
306 pending_write = lqs->lqs_iwrite_pending;
307 record = lqs->lqs_ino_rec;
310 /* we don't count the MIN_QLIMIT */
311 if ((limit == MIN_QLIMIT && !QDATA_IS_BLK(qdata)) ||
312 (toqb(limit) == MIN_QLIMIT && QDATA_IS_BLK(qdata)))
315 usage += pending_write;
317 /* when a releasing quota req is sent, before it returned
318 limit is assigned a small value. limit will overflow */
324 LASSERT(qdata->qd_count == 0);
325 if (limit <= usage + tune_sz) {
326 while (qdata->qd_count + limit <=
328 qdata->qd_count += qunit_sz;
330 } else if (limit > usage + qunit_sz + tune_sz &&
331 limit_org > qdata->qd_count + qunit_sz) {
332 while (limit - qdata->qd_count > usage + qunit_sz + tune_sz &&
333 limit_org > qdata->qd_count + qunit_sz)
334 qdata->qd_count += qunit_sz;
336 /* if there are other pending writes for this uid/gid, releasing
337 * quota is put off until the last pending write b=16645 */
338 /* if there is an ongoing quota request, a releasing request is aborted.
339 * That ongoing quota request will call this function again when
340 * it returned b=18630 */
341 if (pending_write || record) {
342 CDEBUG(D_QUOTA, "delay quota release\n");
347 quota_compute_lqs(qdata, lqs, 1, (ret == 1) ? 1 : 0);
349 CDEBUG(D_QUOTA, "type: %c, limit: "LPU64", usage: "LPU64
350 ", pending_write: "LPU64", record: %lld"
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 cfs_spin_unlock(&lqs->lqs_lock);
366 * Compute the remaining quota for certain gid or uid b=11693
368 int compute_remquota(struct obd_device *obd, struct lustre_quota_ctxt *qctxt,
369 struct qunit_data *qdata, int isblk)
371 struct super_block *sb = qctxt->lqc_sb;
373 struct obd_quotactl *qctl;
374 int ret = QUOTA_RET_OK;
377 if (!ll_sb_any_quota_active(sb))
378 RETURN(QUOTA_RET_NOQUOTA);
380 /* ignore root user */
381 if (qdata->qd_id == 0 && QDATA_IS_GRP(qdata) == USRQUOTA)
382 RETURN(QUOTA_RET_NOLIMIT);
388 /* get fs quota usage & limit */
389 qctl->qc_cmd = Q_GETQUOTA;
390 qctl->qc_id = qdata->qd_id;
391 qctl->qc_type = QDATA_IS_GRP(qdata);
392 ret = fsfilt_quotactl(obd, sb, qctl);
394 if (ret == -ESRCH) /* no limit */
395 ret = QUOTA_RET_NOLIMIT;
397 CDEBUG(D_QUOTA, "can't get fs quota usage! (rc:%d)",
402 usage = isblk ? qctl->qc_dqblk.dqb_curspace :
403 qctl->qc_dqblk.dqb_curinodes;
404 limit = isblk ? qctl->qc_dqblk.dqb_bhardlimit << QUOTABLOCK_BITS :
405 qctl->qc_dqblk.dqb_ihardlimit;
406 if (!limit){ /* no limit */
407 ret = QUOTA_RET_NOLIMIT;
412 qdata->qd_count = limit - usage;
421 static struct lustre_qunit *alloc_qunit(struct lustre_quota_ctxt *qctxt,
422 struct qunit_data *qdata, int opc)
424 struct lustre_qunit *qunit = NULL;
427 OBD_SLAB_ALLOC_PTR_GFP(qunit, qunit_cachep, CFS_ALLOC_IO);
431 CFS_INIT_LIST_HEAD(&qunit->lq_hash);
432 cfs_waitq_init(&qunit->lq_waitq);
433 cfs_atomic_set(&qunit->lq_refcnt, 1);
434 qunit->lq_ctxt = qctxt;
435 memcpy(&qunit->lq_data, qdata, sizeof(*qdata));
437 qunit->lq_lock = CFS_SPIN_LOCK_UNLOCKED;
438 QUNIT_SET_STATE_AND_RC(qunit, QUNIT_CREATED, 0);
439 qunit->lq_owner = cfs_curproc_pid();
443 static inline void free_qunit(struct lustre_qunit *qunit)
445 OBD_SLAB_FREE(qunit, qunit_cachep, sizeof(*qunit));
448 static inline void qunit_get(struct lustre_qunit *qunit)
450 cfs_atomic_inc(&qunit->lq_refcnt);
453 static void qunit_put(struct lustre_qunit *qunit)
455 LASSERT(cfs_atomic_read(&qunit->lq_refcnt));
456 if (cfs_atomic_dec_and_test(&qunit->lq_refcnt))
460 /* caller must hold qunit_hash_lock and release ref of qunit after using it */
461 static struct lustre_qunit *dqacq_in_flight(struct lustre_quota_ctxt *qctxt,
462 struct qunit_data *qdata)
464 unsigned int hashent = qunit_hashfn(qctxt, qdata);
465 struct lustre_qunit *qunit;
468 LASSERT_SPIN_LOCKED(&qunit_hash_lock);
469 qunit = find_qunit(hashent, qctxt, qdata);
476 insert_qunit_nolock(struct lustre_quota_ctxt *qctxt, struct lustre_qunit *qunit)
480 LASSERT(cfs_list_empty(&qunit->lq_hash));
482 head = qunit_hash + qunit_hashfn(qctxt, &qunit->lq_data);
483 cfs_list_add(&qunit->lq_hash, head);
484 QUNIT_SET_STATE(qunit, QUNIT_IN_HASH);
487 static void compute_lqs_after_removing_qunit(struct lustre_qunit *qunit)
489 struct lustre_qunit_size *lqs;
491 lqs = quota_search_lqs(LQS_KEY(QDATA_IS_GRP(&qunit->lq_data),
492 qunit->lq_data.qd_id),
494 if (lqs && !IS_ERR(lqs)) {
495 cfs_spin_lock(&lqs->lqs_lock);
496 if (qunit->lq_opc == QUOTA_DQACQ)
497 quota_compute_lqs(&qunit->lq_data, lqs, 0, 1);
498 if (qunit->lq_opc == QUOTA_DQREL)
499 quota_compute_lqs(&qunit->lq_data, lqs, 0, 0);
500 cfs_spin_unlock(&lqs->lqs_lock);
501 /* this is for quota_search_lqs */
503 /* this is for schedule_dqacq */
508 static void remove_qunit_nolock(struct lustre_qunit *qunit)
510 LASSERT(!cfs_list_empty(&qunit->lq_hash));
511 LASSERT_SPIN_LOCKED(&qunit_hash_lock);
513 cfs_list_del_init(&qunit->lq_hash);
514 QUNIT_SET_STATE(qunit, QUNIT_RM_FROM_HASH);
518 void* quota_barrier(struct lustre_quota_ctxt *qctxt,
519 struct obd_quotactl *oqctl, int isblk)
521 struct lustre_qunit *qunit, *find_qunit;
524 OBD_SLAB_ALLOC_PTR(qunit, qunit_cachep);
526 CERROR("locating %sunit failed for %sid %u\n",
527 isblk ? "b" : "i", oqctl->qc_type ? "g" : "u",
529 qctxt_wait_pending_dqacq(qctxt, oqctl->qc_id,
530 oqctl->qc_type, isblk);
534 CFS_INIT_LIST_HEAD(&qunit->lq_hash);
535 qunit->lq_lock = CFS_SPIN_LOCK_UNLOCKED;
536 cfs_waitq_init(&qunit->lq_waitq);
537 cfs_atomic_set(&qunit->lq_refcnt, 1);
538 qunit->lq_ctxt = qctxt;
539 qunit->lq_data.qd_id = oqctl->qc_id;
540 qunit->lq_data.qd_flags = oqctl->qc_type;
542 QDATA_SET_BLK(&qunit->lq_data);
543 QUNIT_SET_STATE_AND_RC(qunit, QUNIT_CREATED, 0);
544 /* it means it is only an invalid qunit for barrier */
545 qunit->lq_opc = QUOTA_LAST_OPC;
548 cfs_spin_lock(&qunit_hash_lock);
549 find_qunit = dqacq_in_flight(qctxt, &qunit->lq_data);
551 cfs_spin_unlock(&qunit_hash_lock);
552 qunit_put(find_qunit);
553 qctxt_wait_pending_dqacq(qctxt, oqctl->qc_id,
554 oqctl->qc_type, isblk);
555 CDEBUG(D_QUOTA, "cycle=%d\n", cycle++);
560 insert_qunit_nolock(qctxt, qunit);
561 cfs_spin_unlock(&qunit_hash_lock);
565 void quota_unbarrier(void *handle)
567 struct lustre_qunit *qunit = (struct lustre_qunit *)handle;
570 CERROR("handle is NULL\n");
574 LASSERT(qunit->lq_opc == QUOTA_LAST_OPC);
575 cfs_spin_lock(&qunit_hash_lock);
576 remove_qunit_nolock(qunit);
577 cfs_spin_unlock(&qunit_hash_lock);
578 QUNIT_SET_STATE_AND_RC(qunit, QUNIT_FINISHED, QUOTA_REQ_RETURNED);
579 cfs_waitq_signal(&qunit->lq_waitq);
583 #define INC_QLIMIT(limit, count) (limit == MIN_QLIMIT) ? \
584 (limit = count) : (limit += count)
587 static inline int is_master(struct lustre_quota_ctxt *qctxt)
589 return qctxt->lqc_handler ? 1 : 0;
593 schedule_dqacq(struct obd_device *obd, struct lustre_quota_ctxt *qctxt,
594 struct qunit_data *qdata, int opc, int wait,
595 struct obd_trans_info *oti);
597 static inline void qdata_to_oqaq(struct qunit_data *qdata,
598 struct quota_adjust_qunit *oqaq)
603 oqaq->qaq_flags = qdata->qd_flags;
604 oqaq->qaq_id = qdata->qd_id;
605 if (QDATA_IS_ADJBLK(qdata))
606 oqaq->qaq_bunit_sz = qdata->qd_qunit;
607 if (QDATA_IS_ADJINO(qdata))
608 oqaq->qaq_iunit_sz = qdata->qd_qunit;
612 dqacq_completion(struct obd_device *obd, struct lustre_quota_ctxt *qctxt,
613 struct qunit_data *qdata, int rc, int opc)
615 struct lustre_qunit *qunit = NULL;
616 struct super_block *sb = qctxt->lqc_sb;
618 struct quota_adjust_qunit *oqaq = NULL;
623 QDATA_DEBUG(qdata, "obd(%s): complete %s quota req\n",
624 obd->obd_name, (opc == QUOTA_DQACQ) ? "acq" : "rel");
626 /* do it only when a releasing quota req more than 5MB b=18491 */
627 if (opc == QUOTA_DQREL && qdata->qd_count >= 5242880)
628 OBD_FAIL_TIMEOUT(OBD_FAIL_QUOTA_DELAY_REL, 5);
630 /* update local operational quota file */
632 __u64 count = QUSG(qdata->qd_count, QDATA_IS_BLK(qdata));
633 struct obd_quotactl *qctl;
638 GOTO(out, err = -ENOMEM);
640 /* acq/rel qunit for specified uid/gid is serialized,
641 * so there is no race between get fs quota limit and
642 * set fs quota limit */
643 qctl->qc_cmd = Q_GETQUOTA;
644 qctl->qc_id = qdata->qd_id;
645 qctl->qc_type = QDATA_IS_GRP(qdata);
646 err = fsfilt_quotactl(obd, sb, qctl);
648 CERROR("error get quota fs limit! (rc:%d)\n", err);
652 if (QDATA_IS_BLK(qdata)) {
653 qctl->qc_dqblk.dqb_valid = QIF_BLIMITS;
654 hardlimit = &qctl->qc_dqblk.dqb_bhardlimit;
656 qctl->qc_dqblk.dqb_valid = QIF_ILIMITS;
657 hardlimit = &qctl->qc_dqblk.dqb_ihardlimit;
660 CDEBUG(D_QUOTA, "hardlimt: "LPU64"\n", *hardlimit);
667 INC_QLIMIT(*hardlimit, count);
670 LASSERTF(count < *hardlimit,
671 "id(%u) flag(%u) type(%c) isblk(%c) "
672 "count("LPU64") qd_qunit("LPU64") "
673 "hardlimit("LPU64").\n",
674 qdata->qd_id, qdata->qd_flags,
675 QDATA_IS_GRP(qdata) ? 'g' : 'u',
676 QDATA_IS_BLK(qdata) ? 'b': 'i',
677 qdata->qd_count, qdata->qd_qunit, *hardlimit);
684 /* clear quota limit */
688 qctl->qc_cmd = Q_SETQUOTA;
689 err = fsfilt_quotactl(obd, sb, qctl);
691 CERROR("error set quota fs limit! (rc:%d)\n", err);
693 QDATA_DEBUG(qdata, "%s completion\n",
694 opc == QUOTA_DQACQ ? "DQACQ" : "DQREL");
697 } else if (rc == -EDQUOT) {
698 QDATA_DEBUG(qdata, "acquire qunit got EDQUOT.\n");
699 } else if (rc == -EBUSY) {
700 QDATA_DEBUG(qdata, "it's is recovering, got EBUSY.\n");
702 CERROR("acquire qunit got error! (rc:%d)\n", rc);
705 /* remove the qunit from hash */
706 cfs_spin_lock(&qunit_hash_lock);
708 qunit = dqacq_in_flight(qctxt, qdata);
709 /* this qunit has been removed by qctxt_cleanup() */
711 cfs_spin_unlock(&qunit_hash_lock);
712 QDATA_DEBUG(qdata, "%s is discarded because qunit isn't found\n",
713 opc == QUOTA_DQACQ ? "DQACQ" : "DQREL");
717 LASSERT(opc == qunit->lq_opc);
718 /* remove this qunit from lq_hash so that new processes cannot be added
719 * to qunit->lq_waiters */
720 remove_qunit_nolock(qunit);
721 cfs_spin_unlock(&qunit_hash_lock);
723 compute_lqs_after_removing_qunit(qunit);
726 rc = QUOTA_REQ_RETURNED;
727 QUNIT_SET_STATE_AND_RC(qunit, QUNIT_FINISHED, rc);
728 /* wake up all waiters */
729 cfs_waitq_broadcast(&qunit->lq_waitq);
731 /* this is for dqacq_in_flight() */
733 if (rc < 0 && rc != -EDQUOT)
736 /* don't reschedule in such cases:
737 * - acq/rel failure and qunit isn't changed,
738 * but not for quota recovery.
739 * - local dqacq/dqrel.
740 * - local disk io failure.
744 GOTO(out1, err = -ENOMEM);
745 qdata_to_oqaq(qdata, oqaq);
746 /* adjust the qunit size in slaves */
747 rc1 = quota_adjust_slave_lqs(oqaq, qctxt);
750 CERROR("adjust slave's qunit size failed!(rc:%d)\n", rc1);
751 GOTO(out1, err = rc1);
753 if (err || (rc < 0 && rc != -EBUSY && rc1 == 0) || is_master(qctxt))
756 if (opc == QUOTA_DQREL && qdata->qd_count >= 5242880 &&
757 OBD_FAIL_CHECK(OBD_FAIL_QUOTA_DELAY_REL))
760 /* reschedule another dqacq/dqrel if needed */
762 qdata->qd_flags &= LQUOTA_QUNIT_FLAGS;
763 rc1 = check_cur_qunit(obd, qctxt, qdata);
766 opc = rc1 == 1 ? QUOTA_DQACQ : QUOTA_DQREL;
767 rc1 = schedule_dqacq(obd, qctxt, qdata, opc, 0, NULL);
768 QDATA_DEBUG(qdata, "reschedudle opc(%d) rc(%d)\n", opc, rc1);
771 /* this is for alloc_qunit() */
776 struct dqacq_async_args {
777 struct lustre_quota_ctxt *aa_ctxt;
778 struct lustre_qunit *aa_qunit;
781 static int dqacq_interpret(const struct lu_env *env,
782 struct ptlrpc_request *req, void *data, int rc)
784 struct dqacq_async_args *aa = (struct dqacq_async_args *)data;
785 struct lustre_quota_ctxt *qctxt = aa->aa_ctxt;
786 struct obd_device_target *obt = qctxt->lqc_obt;
787 struct lustre_qunit *qunit = aa->aa_qunit;
788 struct obd_device *obd = req->rq_import->imp_obd;
789 struct qunit_data *qdata = NULL;
793 LASSERT(req->rq_import);
795 cfs_down_read(&obt->obt_rwsem);
796 /* if a quota req timeouts or is dropped, we should update quota
797 * statistics which will be handled in dqacq_completion. And in
798 * this situation we should get qdata from request instead of
800 qdata = quota_get_qdata(req, (rc != 0) ? QUOTA_REQUEST : QUOTA_REPLY,
804 DEBUG_REQ(D_ERROR, req,
805 "error unpacking qunit_data(rc: %ld)\n",
807 qdata = &qunit->lq_data;
810 QDATA_DEBUG(qdata, "qdata: interpret rc(%d).\n", rc);
811 QDATA_DEBUG((&qunit->lq_data), "lq_data: \n");
813 if (qdata->qd_id != qunit->lq_data.qd_id ||
814 OBD_FAIL_CHECK(OBD_FAIL_QUOTA_RET_QDATA)) {
815 CERROR("the returned qd_id isn't expected!"
816 "(qdata: %u, lq_data: %u)\n", qdata->qd_id,
817 qunit->lq_data.qd_id);
818 qdata->qd_id = qunit->lq_data.qd_id;
821 if (QDATA_IS_GRP(qdata) != QDATA_IS_GRP(&qunit->lq_data)) {
822 CERROR("the returned grp/usr isn't expected!"
823 "(qdata: %u, lq_data: %u)\n", qdata->qd_flags,
824 qunit->lq_data.qd_flags);
825 if (QDATA_IS_GRP(&qunit->lq_data))
826 QDATA_SET_GRP(qdata);
828 QDATA_CLR_GRP(qdata);
831 if (qdata->qd_count > qunit->lq_data.qd_count) {
832 CERROR("the returned qd_count isn't expected!"
833 "(qdata: "LPU64", lq_data: "LPU64")\n", qdata->qd_count,
834 qunit->lq_data.qd_count);
838 if (unlikely(rc == -ESRCH))
839 CERROR("quota for %s has been enabled by master, but disabled "
840 "by slave.\n", QDATA_IS_GRP(qdata) ? "group" : "user");
842 rc = dqacq_completion(obd, qctxt, qdata, rc,
843 lustre_msg_get_opc(req->rq_reqmsg));
845 cfs_up_read(&obt->obt_rwsem);
850 * check if quota master is online
852 int check_qm(struct lustre_quota_ctxt *qctxt)
857 cfs_spin_lock(&qctxt->lqc_lock);
858 /* quit waiting when mds is back or qctxt is cleaned up */
859 rc = qctxt->lqc_import || !qctxt->lqc_valid;
860 cfs_spin_unlock(&qctxt->lqc_lock);
865 /* wake up all waiting threads when lqc_import is NULL */
866 void dqacq_interrupt(struct lustre_quota_ctxt *qctxt)
868 struct lustre_qunit *qunit, *tmp;
872 cfs_spin_lock(&qunit_hash_lock);
873 for (i = 0; i < NR_DQHASH; i++) {
874 cfs_list_for_each_entry_safe(qunit, tmp, &qunit_hash[i],
876 if (qunit->lq_ctxt != qctxt)
879 /* Wake up all waiters. Do not change lq_state.
880 * The waiters will check lq_rc which is kept as 0
881 * if no others change it, then the waiters will return
882 * -EAGAIN to caller who can perform related quota
883 * acq/rel if necessary. */
884 cfs_waitq_broadcast(&qunit->lq_waitq);
887 cfs_spin_unlock(&qunit_hash_lock);
891 static int got_qunit(struct lustre_qunit *qunit, int is_master)
893 struct lustre_quota_ctxt *qctxt = qunit->lq_ctxt;
897 cfs_spin_lock(&qunit->lq_lock);
898 switch (qunit->lq_state) {
900 case QUNIT_RM_FROM_HASH:
906 CERROR("invalid qunit state %d\n", qunit->lq_state);
908 cfs_spin_unlock(&qunit->lq_lock);
911 cfs_spin_lock(&qctxt->lqc_lock);
912 rc = !qctxt->lqc_valid;
914 rc |= !qctxt->lqc_import;
915 cfs_spin_unlock(&qctxt->lqc_lock);
922 revoke_lqs_rec(struct lustre_qunit_size *lqs, struct qunit_data *qdata, int opc)
924 /* revoke lqs_xxx_rec which is computed in check_cur_qunit
926 cfs_spin_lock(&lqs->lqs_lock);
927 quota_compute_lqs(qdata, lqs, 0, (opc == QUOTA_DQACQ) ? 1 : 0);
928 cfs_spin_unlock(&lqs->lqs_lock);
932 schedule_dqacq(struct obd_device *obd, struct lustre_quota_ctxt *qctxt,
933 struct qunit_data *qdata, int opc, int wait,
934 struct obd_trans_info *oti)
936 struct lustre_qunit *qunit, *empty;
937 struct l_wait_info lwi = { 0 };
938 struct ptlrpc_request *req;
939 struct dqacq_async_args *aa;
940 struct obd_import *imp = NULL;
941 struct lustre_qunit_size *lqs = NULL;
942 struct timeval work_start;
943 struct timeval work_end;
948 LASSERT(opc == QUOTA_DQACQ || opc == QUOTA_DQREL);
949 cfs_gettimeofday(&work_start);
951 lqs = quota_search_lqs(LQS_KEY(QDATA_IS_GRP(qdata), qdata->qd_id),
953 if (lqs == NULL || IS_ERR(lqs)) {
954 CERROR("Can't find the lustre qunit size!\n");
958 if ((empty = alloc_qunit(qctxt, qdata, opc)) == NULL) {
959 revoke_lqs_rec(lqs, qdata, opc);
960 /* this is for quota_search_lqs */
965 OBD_FAIL_TIMEOUT(OBD_FAIL_QUOTA_DELAY_SD, 5);
967 cfs_spin_lock(&qunit_hash_lock);
968 qunit = dqacq_in_flight(qctxt, qdata);
970 cfs_spin_unlock(&qunit_hash_lock);
973 revoke_lqs_rec(lqs, qdata, opc);
974 /* this is for quota_search_lqs */
976 goto wait_completion;
980 insert_qunit_nolock(qctxt, qunit);
981 cfs_spin_unlock(&qunit_hash_lock);
983 /* From here, the quota request will be sent anyway.
984 * When this qdata request returned or is cancelled,
985 * lqs_putref will be called at that time */
987 /* this is for quota_search_lqs */
990 QDATA_DEBUG(qdata, "obd(%s): send %s quota req\n",
991 obd->obd_name, (opc == QUOTA_DQACQ) ? "acq" : "rel");
992 /* master is going to dqacq/dqrel from itself */
993 if (is_master(qctxt)) {
995 QDATA_DEBUG(qdata, "local %s.\n",
996 opc == QUOTA_DQACQ ? "DQACQ" : "DQREL");
997 QDATA_SET_CHANGE_QS(qdata);
998 rc = qctxt->lqc_handler(obd, qdata, opc);
999 rc2 = dqacq_completion(obd, qctxt, qdata, rc, opc);
1000 /* this is for qunit_get() */
1003 cfs_gettimeofday(&work_end);
1004 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
1005 if (opc == QUOTA_DQACQ)
1006 lprocfs_counter_add(qctxt->lqc_stats,
1007 wait ? LQUOTA_SYNC_ACQ : LQUOTA_ASYNC_ACQ,
1010 lprocfs_counter_add(qctxt->lqc_stats,
1011 wait ? LQUOTA_SYNC_REL : LQUOTA_ASYNC_REL,
1013 RETURN(rc ? rc : rc2);
1016 cfs_spin_lock(&qctxt->lqc_lock);
1017 if (!qctxt->lqc_import) {
1018 cfs_spin_unlock(&qctxt->lqc_lock);
1019 QDATA_DEBUG(qdata, "lqc_import is invalid.\n");
1021 cfs_spin_lock(&qunit_hash_lock);
1022 remove_qunit_nolock(qunit);
1023 cfs_spin_unlock(&qunit_hash_lock);
1025 compute_lqs_after_removing_qunit(qunit);
1027 QUNIT_SET_STATE_AND_RC(qunit, QUNIT_FINISHED, -EAGAIN);
1028 cfs_waitq_broadcast(&qunit->lq_waitq);
1030 /* this is for qunit_get() */
1032 /* this for alloc_qunit() */
1034 cfs_spin_lock(&qctxt->lqc_lock);
1035 if (wait && !qctxt->lqc_import) {
1036 cfs_spin_unlock(&qctxt->lqc_lock);
1037 LASSERT(oti && oti->oti_thread);
1038 /* The recovery thread doesn't have watchdog
1039 * attached. LU-369 */
1040 if (oti->oti_thread->t_watchdog)
1041 lc_watchdog_disable(oti->oti_thread->\
1043 CDEBUG(D_QUOTA, "sleep for quota master\n");
1044 l_wait_event(qctxt->lqc_wait_for_qmaster,
1045 check_qm(qctxt), &lwi);
1046 CDEBUG(D_QUOTA, "wake up when quota master is back\n");
1047 if (oti->oti_thread->t_watchdog)
1048 lc_watchdog_touch(oti->oti_thread->t_watchdog,
1049 CFS_GET_TIMEOUT(oti->oti_thread->t_svc));
1051 cfs_spin_unlock(&qctxt->lqc_lock);
1056 imp = class_import_get(qctxt->lqc_import);
1057 cfs_spin_unlock(&qctxt->lqc_lock);
1059 /* build dqacq/dqrel request */
1062 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_QUOTA_DQACQ,
1063 LUSTRE_MDS_VERSION, opc);
1064 class_import_put(imp);
1066 CERROR("Can't alloc request\n");
1067 dqacq_completion(obd, qctxt, qdata, -ENOMEM, opc);
1068 /* this is for qunit_get() */
1073 ptlrpc_request_set_replen(req);
1074 req->rq_no_resend = req->rq_no_delay = 1;
1075 rc = quota_copy_qdata(req, qdata, QUOTA_REQUEST, QUOTA_IMPORT);
1077 CERROR("Can't pack qunit_data(rc: %d)\n", rc);
1078 ptlrpc_req_finished(req);
1079 dqacq_completion(obd, qctxt, qdata, -EPROTO, opc);
1080 /* this is for qunit_get() */
1085 CLASSERT(sizeof(*aa) <= sizeof(req->rq_async_args));
1086 aa = ptlrpc_req_async_args(req);
1087 aa->aa_ctxt = qctxt;
1088 aa->aa_qunit = qunit;
1090 req->rq_interpret_reply = dqacq_interpret;
1091 ptlrpcd_add_req(req, PDL_POLICY_LOCAL, -1);
1093 QDATA_DEBUG(qdata, "%s scheduled.\n",
1094 opc == QUOTA_DQACQ ? "DQACQ" : "DQREL");
1096 if (wait && qunit) {
1097 struct qunit_data *p = &qunit->lq_data;
1099 QDATA_DEBUG(p, "qunit(%p) is waiting for dqacq.\n", qunit);
1100 l_wait_event(qunit->lq_waitq, got_qunit(qunit, is_master(qctxt)),
1102 /* rc = -EAGAIN, it means the quota master isn't ready yet
1103 * rc = QUOTA_REQ_RETURNED, it means a quota req is finished;
1104 * rc = -EDQUOT, it means out of quota
1105 * rc = -EBUSY, it means recovery is happening
1106 * other rc < 0, it means real errors, functions who call
1107 * schedule_dqacq should take care of this */
1108 cfs_spin_lock(&qunit->lq_lock);
1110 cfs_spin_unlock(&qunit->lq_lock);
1111 CDEBUG(D_QUOTA, "qunit(%p) finishes waiting: id(%u) flag(%u) "
1112 "rc(%d) owner(%d)\n", qunit, qunit->lq_data.qd_id,
1113 qunit->lq_data.qd_flags, rc, qunit->lq_owner);
1117 cfs_gettimeofday(&work_end);
1118 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
1119 if (opc == QUOTA_DQACQ)
1120 lprocfs_counter_add(qctxt->lqc_stats,
1121 wait ? LQUOTA_SYNC_ACQ : LQUOTA_ASYNC_ACQ,
1124 lprocfs_counter_add(qctxt->lqc_stats,
1125 wait ? LQUOTA_SYNC_REL : LQUOTA_ASYNC_REL,
1132 qctxt_adjust_qunit(struct obd_device *obd, struct lustre_quota_ctxt *qctxt,
1133 const unsigned int id[], __u32 isblk, int wait,
1134 struct obd_trans_info *oti)
1136 int rc = 0, i = USRQUOTA;
1137 struct qunit_data qdata[MAXQUOTAS];
1140 if (quota_is_set(obd, id, isblk ? QB_SET : QI_SET) == 0)
1143 for (i = 0; i < MAXQUOTAS; i++) {
1144 qdata[i].qd_id = id[i];
1145 qdata[i].qd_flags = i;
1147 QDATA_SET_BLK(&qdata[i]);
1148 qdata[i].qd_count = 0;
1150 rc = check_cur_qunit(obd, qctxt, &qdata[i]);
1153 /* need acquire or release */
1154 opc = rc == 1 ? QUOTA_DQACQ : QUOTA_DQREL;
1155 rc = schedule_dqacq(obd, qctxt, &qdata[i], opc,
1159 } else if (wait == 1) {
1160 /* when wait equates 1, that means mds_quota_acquire
1161 * or filter_quota_acquire is calling it. */
1162 rc = qctxt_wait_pending_dqacq(qctxt, id[i], i, isblk);
1172 qctxt_wait_pending_dqacq(struct lustre_quota_ctxt *qctxt, unsigned int id,
1173 unsigned short type, int isblk)
1175 struct lustre_qunit *qunit = NULL;
1176 struct qunit_data qdata;
1177 struct timeval work_start;
1178 struct timeval work_end;
1180 struct l_wait_info lwi = { 0 };
1184 cfs_gettimeofday(&work_start);
1186 qdata.qd_flags = type;
1188 QDATA_SET_BLK(&qdata);
1191 cfs_spin_lock(&qunit_hash_lock);
1192 qunit = dqacq_in_flight(qctxt, &qdata);
1193 cfs_spin_unlock(&qunit_hash_lock);
1196 struct qunit_data *p = &qunit->lq_data;
1198 QDATA_DEBUG(p, "qunit(%p) is waiting for dqacq.\n", qunit);
1199 l_wait_event(qunit->lq_waitq, got_qunit(qunit, is_master(qctxt)),
1201 CDEBUG(D_QUOTA, "qunit(%p) finishes waiting: rc(%d) "
1202 "owner(%d)\n", qunit, qunit->lq_rc, qunit->lq_owner);
1203 /* keep same as schedule_dqacq() b=17030 */
1204 cfs_spin_lock(&qunit->lq_lock);
1206 cfs_spin_unlock(&qunit->lq_lock);
1207 /* this is for dqacq_in_flight() */
1209 cfs_gettimeofday(&work_end);
1210 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
1211 lprocfs_counter_add(qctxt->lqc_stats,
1212 isblk ? LQUOTA_WAIT_PENDING_BLK_QUOTA :
1213 LQUOTA_WAIT_PENDING_INO_QUOTA,
1216 cfs_gettimeofday(&work_end);
1217 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
1218 lprocfs_counter_add(qctxt->lqc_stats,
1219 isblk ? LQUOTA_NOWAIT_PENDING_BLK_QUOTA :
1220 LQUOTA_NOWAIT_PENDING_INO_QUOTA,
1228 qctxt_init(struct obd_device *obd, dqacq_handler_t handler)
1230 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
1231 struct obd_device_target *obt = &obd->u.obt;
1232 struct super_block *sb = obt->obt_sb;
1238 rc = ptlrpcd_addref();
1242 cfs_waitq_init(&qctxt->lqc_wait_for_qmaster);
1243 cfs_waitq_init(&qctxt->lqc_lqs_waitq);
1244 cfs_atomic_set(&qctxt->lqc_lqs, 0);
1245 cfs_spin_lock_init(&qctxt->lqc_lock);
1246 cfs_spin_lock(&qctxt->lqc_lock);
1247 qctxt->lqc_handler = handler;
1249 qctxt->lqc_obt = obt;
1250 qctxt->lqc_import = NULL;
1251 qctxt->lqc_recovery = 0;
1252 qctxt->lqc_switch_qs = 1; /* Change qunit size in default setting */
1253 qctxt->lqc_valid = 1;
1254 qctxt->lqc_cqs_boundary_factor = 4;
1255 qctxt->lqc_cqs_least_bunit = PTLRPC_MAX_BRW_SIZE;
1256 qctxt->lqc_cqs_least_iunit = 2;
1257 qctxt->lqc_cqs_qs_factor = 2;
1258 qctxt->lqc_flags = 0;
1259 QUOTA_MASTER_UNREADY(qctxt);
1260 qctxt->lqc_bunit_sz = default_bunit_sz;
1261 qctxt->lqc_btune_sz = default_bunit_sz / 100 * default_btune_ratio;
1262 qctxt->lqc_iunit_sz = default_iunit_sz;
1263 qctxt->lqc_itune_sz = default_iunit_sz * default_itune_ratio / 100;
1264 qctxt->lqc_switch_seconds = 300; /* enlarging will wait 5 minutes
1265 * after the last shrinking */
1266 qctxt->lqc_sync_blk = 0;
1267 cfs_spin_unlock(&qctxt->lqc_lock);
1269 qctxt->lqc_lqs_hash = cfs_hash_create("LQS_HASH",
1272 min(hash_lqs_cur_bits,
1274 0, CFS_HASH_MIN_THETA,
1276 &lqs_hash_ops, CFS_HASH_DEFAULT);
1277 if (!qctxt->lqc_lqs_hash) {
1278 CERROR("initialize hash lqs for %s error!\n", obd->obd_name);
1283 rc = lquota_proc_setup(obd, is_master(qctxt));
1285 CERROR("initialize proc for %s error!\n", obd->obd_name);
1291 static int check_lqs(struct lustre_quota_ctxt *qctxt)
1296 rc = !cfs_atomic_read(&qctxt->lqc_lqs);
1301 int qctxt_del_lqs(cfs_hash_t *hs, cfs_hash_bd_t *bd,
1302 cfs_hlist_node_t *hnode, void *data)
1304 /* remove from hash and -1 refcount */
1305 cfs_hash_bd_del_locked(hs, bd, hnode);
1309 void qctxt_cleanup(struct lustre_quota_ctxt *qctxt, int force)
1311 struct lustre_qunit *qunit, *tmp;
1312 cfs_list_t tmp_list;
1313 struct l_wait_info lwi = { 0 };
1314 struct obd_device_target *obt = qctxt->lqc_obt;
1318 CFS_INIT_LIST_HEAD(&tmp_list);
1320 cfs_spin_lock(&qctxt->lqc_lock);
1321 qctxt->lqc_valid = 0;
1322 cfs_spin_unlock(&qctxt->lqc_lock);
1324 cfs_spin_lock(&qunit_hash_lock);
1325 for (i = 0; i < NR_DQHASH; i++) {
1326 cfs_list_for_each_entry_safe(qunit, tmp, &qunit_hash[i],
1328 if (qunit->lq_ctxt != qctxt)
1330 remove_qunit_nolock(qunit);
1331 cfs_list_add(&qunit->lq_hash, &tmp_list);
1334 cfs_spin_unlock(&qunit_hash_lock);
1336 cfs_list_for_each_entry_safe(qunit, tmp, &tmp_list, lq_hash) {
1337 cfs_list_del_init(&qunit->lq_hash);
1338 compute_lqs_after_removing_qunit(qunit);
1340 /* wake up all waiters */
1341 QUNIT_SET_STATE_AND_RC(qunit, QUNIT_FINISHED, 0);
1342 cfs_waitq_broadcast(&qunit->lq_waitq);
1346 /* after qctxt_cleanup, qctxt might be freed, then check_qm() is
1347 * unpredicted. So we must wait until lqc_wait_for_qmaster is empty */
1348 while (cfs_waitq_active(&qctxt->lqc_wait_for_qmaster)) {
1349 cfs_waitq_signal(&qctxt->lqc_wait_for_qmaster);
1350 cfs_schedule_timeout_and_set_state(CFS_TASK_INTERRUPTIBLE,
1351 cfs_time_seconds(1));
1354 /* release refcount on lustre_qunit_size holding by lqs_hash */
1355 cfs_hash_for_each_safe(qctxt->lqc_lqs_hash, qctxt_del_lqs, NULL);
1357 l_wait_event(qctxt->lqc_lqs_waitq, check_lqs(qctxt), &lwi);
1358 cfs_down_write(&obt->obt_rwsem);
1359 cfs_hash_putref(qctxt->lqc_lqs_hash);
1360 qctxt->lqc_lqs_hash = NULL;
1361 cfs_up_write(&obt->obt_rwsem);
1366 if (lquota_proc_cleanup(qctxt))
1367 CERROR("cleanup proc error!\n");
1373 struct qslave_recov_thread_data {
1374 struct obd_device *obd;
1375 struct lustre_quota_ctxt *qctxt;
1376 cfs_completion_t comp;
1379 /* FIXME only recovery block quota by now */
1380 static int qslave_recovery_main(void *arg)
1382 struct qslave_recov_thread_data *data = arg;
1383 struct obd_device *obd = data->obd;
1384 struct lustre_quota_ctxt *qctxt = data->qctxt;
1389 cfs_daemonize_ctxt("qslave_recovd");
1392 class_incref(obd, "qslave_recovd_filter", obd);
1394 cfs_complete(&data->comp);
1396 cfs_spin_lock(&qctxt->lqc_lock);
1397 if (qctxt->lqc_recovery) {
1398 cfs_spin_unlock(&qctxt->lqc_lock);
1399 class_decref(obd, "qslave_recovd_filter", obd);
1402 qctxt->lqc_recovery = 1;
1403 cfs_spin_unlock(&qctxt->lqc_lock);
1406 for (type = USRQUOTA; type < MAXQUOTAS; type++) {
1407 struct qunit_data qdata;
1408 struct quota_info *dqopt = sb_dqopt(qctxt->lqc_sb);
1410 struct dquot_id *dqid, *tmp;
1413 LOCK_DQONOFF_MUTEX(dqopt);
1414 if (!ll_sb_has_quota_active(qctxt->lqc_sb, type)) {
1415 UNLOCK_DQONOFF_MUTEX(dqopt);
1419 LASSERT(dqopt->files[type] != NULL);
1420 CFS_INIT_LIST_HEAD(&id_list);
1421 #ifndef KERNEL_SUPPORTS_QUOTA_READ
1422 rc = fsfilt_qids(obd, dqopt->files[type], NULL, type, &id_list);
1424 rc = fsfilt_qids(obd, NULL, dqopt->files[type], type, &id_list);
1426 UNLOCK_DQONOFF_MUTEX(dqopt);
1428 CERROR("Get ids from quota file failed. (rc:%d)\n", rc);
1430 cfs_list_for_each_entry_safe(dqid, tmp, &id_list, di_link) {
1431 cfs_list_del_init(&dqid->di_link);
1432 /* skip slave recovery on itself */
1433 if (is_master(qctxt))
1435 if (rc && rc != -EBUSY)
1438 qdata.qd_id = dqid->di_id;
1439 qdata.qd_flags = type;
1440 QDATA_SET_BLK(&qdata);
1443 ret = check_cur_qunit(obd, qctxt, &qdata);
1446 opc = ret == 1 ? QUOTA_DQACQ : QUOTA_DQREL;
1447 rc = schedule_dqacq(obd, qctxt, &qdata, opc,
1455 if (rc && rc != -EBUSY)
1456 CERROR("qslave recovery failed! (id:%d type:%d "
1457 " rc:%d)\n", dqid->di_id, type, rc);
1463 cfs_spin_lock(&qctxt->lqc_lock);
1464 qctxt->lqc_recovery = 0;
1465 cfs_spin_unlock(&qctxt->lqc_lock);
1466 class_decref(obd, "qslave_recovd_filter", obd);
1471 qslave_start_recovery(struct obd_device *obd, struct lustre_quota_ctxt *qctxt)
1473 struct qslave_recov_thread_data data;
1477 if (!ll_sb_any_quota_active(qctxt->lqc_sb))
1482 cfs_init_completion(&data.comp);
1484 rc = cfs_create_thread(qslave_recovery_main, &data,
1487 CERROR("Cannot start quota recovery thread: rc %d\n", rc);
1490 cfs_wait_for_completion(&data.comp);
1495 inline int quota_is_on(struct lustre_quota_ctxt *qctxt,
1496 struct obd_quotactl *oqctl)
1498 return ((qctxt->lqc_flags & UGQUOTA2LQC(oqctl->qc_type)) ==
1499 UGQUOTA2LQC(oqctl->qc_type));
1502 inline int quota_is_off(struct lustre_quota_ctxt *qctxt,
1503 struct obd_quotactl *oqctl)
1505 return !(qctxt->lqc_flags & UGQUOTA2LQC(oqctl->qc_type));
1509 * When quotaon, build a lqs for every uid/gid who has been set limitation
1510 * for quota. After quota_search_lqs, it will hold one ref for the lqs.
1511 * It will be released when qctxt_cleanup() is executed b=18574
1513 * Should be called with obt->obt_quotachecking held. b=20152
1515 void build_lqs(struct obd_device *obd)
1517 struct obd_device_target *obt = &obd->u.obt;
1518 struct lustre_quota_ctxt *qctxt = &obt->obt_qctxt;
1522 LASSERT_SEM_LOCKED(&obt->obt_quotachecking);
1523 CFS_INIT_LIST_HEAD(&id_list);
1524 for (i = 0; i < MAXQUOTAS; i++) {
1525 struct dquot_id *dqid, *tmp;
1527 if (sb_dqopt(qctxt->lqc_sb)->files[i] == NULL)
1530 #ifndef KERNEL_SUPPORTS_QUOTA_READ
1531 rc = fsfilt_qids(obd, sb_dqopt(qctxt->lqc_sb)->files[i], NULL,
1534 rc = fsfilt_qids(obd, NULL, sb_dqopt(qctxt->lqc_sb)->files[i],
1538 CERROR("%s: failed to get %s qids!\n", obd->obd_name,
1539 i ? "group" : "user");
1543 cfs_list_for_each_entry_safe(dqid, tmp, &id_list,
1545 struct lustre_qunit_size *lqs;
1547 cfs_list_del_init(&dqid->di_link);
1548 lqs = quota_search_lqs(LQS_KEY(i, dqid->di_id),
1550 if (lqs && !IS_ERR(lqs)) {
1551 lqs->lqs_flags |= dqid->di_flag;
1554 CERROR("%s: failed to create a lqs for %sid %u"
1555 "\n", obd->obd_name, i ? "g" : "u",
1565 * lqs<->qctxt hash operations
1569 * string hashing using djb2 hash algorithm
1572 lqs_hash(cfs_hash_t *hs, const void *key, unsigned mask)
1574 unsigned long long id;
1579 id = *((unsigned long long *)key);
1580 hash = (LQS_KEY_GRP(id) ? 5381 : 5387) * (unsigned)LQS_KEY_ID(id);
1582 RETURN(hash & mask);
1586 lqs_key(cfs_hlist_node_t *hnode)
1588 struct lustre_qunit_size *lqs;
1591 lqs = cfs_hlist_entry(hnode, struct lustre_qunit_size, lqs_hash);
1592 RETURN(&lqs->lqs_key);
1596 lqs_keycmp(const void *key, cfs_hlist_node_t *hnode)
1598 struct lustre_qunit_size *q =
1599 cfs_hlist_entry(hnode, struct lustre_qunit_size, lqs_hash);
1601 RETURN(q->lqs_key == *((unsigned long long *)key));
1605 lqs_object(cfs_hlist_node_t *hnode)
1607 return cfs_hlist_entry(hnode, struct lustre_qunit_size, lqs_hash);
1611 lqs_get(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
1613 struct lustre_qunit_size *q =
1614 cfs_hlist_entry(hnode, struct lustre_qunit_size, lqs_hash);
1620 lqs_put_locked(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
1622 struct lustre_qunit_size *q =
1623 cfs_hlist_entry(hnode, struct lustre_qunit_size, lqs_hash);
1629 lqs_exit(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
1631 CERROR("It should not have any item left to be handled by this!");
1634 static cfs_hash_ops_t lqs_hash_ops = {
1635 .hs_hash = lqs_hash,
1637 .hs_keycmp = lqs_keycmp,
1638 .hs_object = lqs_object,
1640 .hs_put_locked = lqs_put_locked,