Whamcloud - gitweb
Land b_head_quota onto HEAD (20081116_0105)
[fs/lustre-release.git] / lustre / quota / quota_context.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
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.
11  *
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).
17  *
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
21  *
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
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/quota/quota_context.c
37  *
38  * Lustre Quota Context
39  *
40  * Author: Niu YaWei <niu@clusterfs.com>
41  */
42
43 #ifndef EXPORT_SYMTAB
44 # define EXPORT_SYMTAB
45 #endif
46
47 #define DEBUG_SUBSYSTEM S_LQUOTA
48
49 #include <linux/version.h>
50 #include <linux/fs.h>
51 #include <asm/unistd.h>
52 #include <linux/slab.h>
53 #include <linux/quotaops.h>
54 #include <linux/module.h>
55 #include <linux/init.h>
56
57 #include <obd_class.h>
58 #include <lustre_quota.h>
59 #include <lustre_fsfilt.h>
60 #include <class_hash.h>
61 #include <lprocfs_status.h>
62 #include "quota_internal.h"
63
64 #ifdef HAVE_QUOTA_SUPPORT
65
66 static lustre_hash_ops_t lqs_hash_ops;
67
68 unsigned long default_bunit_sz = 128 * 1024 * 1024; /* 128M bytes */
69 unsigned long default_btune_ratio = 50;             /* 50 percentage */
70 unsigned long default_iunit_sz = 5120;              /* 5120 inodes */
71 unsigned long default_itune_ratio = 50;             /* 50 percentage */
72
73 cfs_mem_cache_t *qunit_cachep = NULL;
74 struct list_head qunit_hash[NR_DQHASH];
75 spinlock_t qunit_hash_lock = SPIN_LOCK_UNLOCKED;
76
77 /* please sync qunit_state with qunit_state_names */
78 enum qunit_state {
79         /**
80          * a qunit is created
81          */
82         QUNIT_CREATED      = 0,
83         /**
84          * a qunit is added into qunit hash, that means
85          * a quota req will be sent or is flying
86          */
87         QUNIT_IN_HASH      = 1,
88         /**
89          * a qunit is removed from qunit hash, that
90          * means a quota req is handled and comes back
91          */
92         QUNIT_RM_FROM_HASH = 2,
93         /**
94          * qunit can wake up all threads waiting for it
95          */
96         QUNIT_FINISHED     = 3,
97 };
98
99 static const char *qunit_state_names[] = {
100         [QUNIT_CREATED]      = "CREATED",
101         [QUNIT_IN_HASH]      = "IN_HASH",
102         [QUNIT_RM_FROM_HASH] = "RM_FROM_HASH",
103         [QUNIT_FINISHED]     = "FINISHED",
104 };
105
106 struct lustre_qunit {
107         struct list_head lq_hash;          /** Hash list in memory */
108         atomic_t lq_refcnt;                /** Use count */
109         struct lustre_quota_ctxt *lq_ctxt; /** Quota context this applies to */
110         struct qunit_data lq_data;         /** See qunit_data */
111         unsigned int lq_opc;               /** QUOTA_DQACQ, QUOTA_DQREL */
112         cfs_waitq_t lq_waitq;              /** Threads waiting for this qunit */
113         spinlock_t lq_lock;                /** Protect the whole structure */
114         enum qunit_state lq_state;         /** Present the status of qunit */
115         int lq_rc;                         /** The rc of lq_data */
116 };
117
118 #define QUNIT_SET_STATE(qunit, state)                                   \
119 do {                                                                    \
120         spin_lock(&qunit->lq_lock);                                     \
121         QDATA_DEBUG((&qunit->lq_data), "qunit(%p) lq_state(%s->%s), "   \
122                     "lq_rc(%d)\n",                                      \
123                     qunit, qunit_state_names[qunit->lq_state],          \
124                     qunit_state_names[state], qunit->lq_rc);            \
125         qunit->lq_state = state;                                        \
126         spin_unlock(&qunit->lq_lock);                                   \
127 } while(0)
128
129 #define QUNIT_SET_STATE_AND_RC(qunit, state, rc)                        \
130 do {                                                                    \
131         spin_lock(&qunit->lq_lock);                                     \
132         qunit->lq_rc = rc;                                              \
133         QDATA_DEBUG((&qunit->lq_data), "qunit(%p) lq_state(%s->%s), "   \
134                     "lq_rc(%d)\n",                                      \
135                     qunit, qunit_state_names[qunit->lq_state],          \
136                     qunit_state_names[state], qunit->lq_rc);            \
137         qunit->lq_state = state;                                        \
138         spin_unlock(&qunit->lq_lock);                                   \
139 } while(0)
140
141
142 int should_translate_quota (struct obd_import *imp)
143 {
144         ENTRY;
145
146         LASSERT(imp);
147         if (imp->imp_connect_data.ocd_connect_flags & OBD_CONNECT_QUOTA64)
148                 RETURN(0);
149         else
150                 RETURN(1);
151 }
152
153 void qunit_cache_cleanup(void)
154 {
155         int i;
156         ENTRY;
157
158         spin_lock(&qunit_hash_lock);
159         for (i = 0; i < NR_DQHASH; i++)
160                 LASSERT(list_empty(qunit_hash + i));
161         spin_unlock(&qunit_hash_lock);
162
163         if (qunit_cachep) {
164                 int rc;
165                 rc = cfs_mem_cache_destroy(qunit_cachep);
166                 LASSERTF(rc == 0, "couldn't destroy qunit_cache slab\n");
167                 qunit_cachep = NULL;
168         }
169         EXIT;
170 }
171
172 int qunit_cache_init(void)
173 {
174         int i;
175         ENTRY;
176
177         LASSERT(qunit_cachep == NULL);
178         qunit_cachep = cfs_mem_cache_create("ll_qunit_cache",
179                                             sizeof(struct lustre_qunit),
180                                             0, 0);
181         if (!qunit_cachep)
182                 RETURN(-ENOMEM);
183
184         spin_lock(&qunit_hash_lock);
185         for (i = 0; i < NR_DQHASH; i++)
186                 CFS_INIT_LIST_HEAD(qunit_hash + i);
187         spin_unlock(&qunit_hash_lock);
188         RETURN(0);
189 }
190
191 static inline int
192 qunit_hashfn(struct lustre_quota_ctxt *qctxt, struct qunit_data *qdata)
193              __attribute__((__const__));
194
195 static inline int
196 qunit_hashfn(struct lustre_quota_ctxt *qctxt, struct qunit_data *qdata)
197 {
198         unsigned int id = qdata->qd_id;
199         unsigned int type = QDATA_IS_GRP(qdata);
200
201         unsigned long tmp = ((unsigned long)qctxt >> L1_CACHE_SHIFT) ^ id;
202         tmp = (tmp * (MAXQUOTAS - type)) % NR_DQHASH;
203         return tmp;
204 }
205
206 /* caller must hold qunit_hash_lock */
207 static inline struct lustre_qunit *find_qunit(unsigned int hashent,
208                                               struct lustre_quota_ctxt *qctxt,
209                                               struct qunit_data *qdata)
210 {
211         struct lustre_qunit *qunit = NULL;
212         struct qunit_data *tmp;
213
214         LASSERT_SPIN_LOCKED(&qunit_hash_lock);
215         list_for_each_entry(qunit, qunit_hash + hashent, lq_hash) {
216                 tmp = &qunit->lq_data;
217                 if (qunit->lq_ctxt == qctxt &&
218                     qdata->qd_id == tmp->qd_id &&
219                     (qdata->qd_flags & LQUOTA_QUNIT_FLAGS) ==
220                     (tmp->qd_flags & LQUOTA_QUNIT_FLAGS))
221                         return qunit;
222         }
223         return NULL;
224 }
225
226 /* check_cur_qunit - check the current usage of qunit.
227  * @qctxt: quota context
228  * @qdata: the type of quota unit to be checked
229  *
230  * return: 1 - need acquire qunit;
231  *         2 - need release qunit;
232  *         0 - need do nothing.
233  *       < 0 - error.
234  */
235 static int
236 check_cur_qunit(struct obd_device *obd,
237                 struct lustre_quota_ctxt *qctxt, struct qunit_data *qdata)
238 {
239         struct super_block *sb = qctxt->lqc_sb;
240         unsigned long qunit_sz, tune_sz;
241         __u64 usage, limit, limit_org, pending_write = 0;
242         long long record = 0;
243         struct obd_quotactl *qctl;
244         struct lustre_qunit_size *lqs = NULL;
245         int ret = 0;
246         ENTRY;
247
248         if (!sb_any_quota_enabled(sb))
249                 RETURN(0);
250
251         spin_lock(&qctxt->lqc_lock);
252         if (!qctxt->lqc_valid){
253                 spin_unlock(&qctxt->lqc_lock);
254                 RETURN(0);
255         }
256         spin_unlock(&qctxt->lqc_lock);
257
258         OBD_ALLOC_PTR(qctl);
259         if (qctl == NULL)
260                 RETURN(-ENOMEM);
261
262         /* get fs quota usage & limit */
263         qctl->qc_cmd = Q_GETQUOTA;
264         qctl->qc_id = qdata->qd_id;
265         qctl->qc_type = QDATA_IS_GRP(qdata);
266         ret = fsfilt_quotactl(obd, sb, qctl);
267         if (ret) {
268                 if (ret == -ESRCH)      /* no limit */
269                         ret = 0;
270                 else
271                         CERROR("can't get fs quota usage! (rc:%d)\n", ret);
272                 GOTO(out, ret);
273         }
274
275         if (QDATA_IS_BLK(qdata)) {
276                 usage = qctl->qc_dqblk.dqb_curspace;
277                 limit = qctl->qc_dqblk.dqb_bhardlimit << QUOTABLOCK_BITS;
278         } else {
279                 usage = qctl->qc_dqblk.dqb_curinodes;
280                 limit = qctl->qc_dqblk.dqb_ihardlimit;
281         }
282
283         /* ignore the no quota limit case; and it can avoid creating
284          * unnecessary lqs for uid/gid */
285         if (!limit)
286                 GOTO(out, ret = 0);
287
288  search_lqs:
289         quota_search_lqs(qdata, NULL, qctxt, &lqs);
290         if (!lqs) {
291                 CDEBUG(D_QUOTA, "Can't find the lustre qunit size!\n");
292                 ret = quota_create_lqs(qdata, NULL, qctxt, &lqs);
293                 if (ret == -EALREADY) {
294                         ret = 0;
295                         goto search_lqs;
296                 }
297                 if (ret < 0)
298                         GOTO (out, ret);
299         }
300         spin_lock(&lqs->lqs_lock);
301
302         if (QDATA_IS_BLK(qdata)) {
303                 qunit_sz = lqs->lqs_bunit_sz;
304                 tune_sz  = lqs->lqs_btune_sz;
305                 pending_write = lqs->lqs_bwrite_pending * CFS_PAGE_SIZE;
306                 record   = lqs->lqs_blk_rec;
307                 LASSERT(!(qunit_sz % QUOTABLOCK_SIZE));
308         } else {
309                 /* we didn't need change inode qunit size now */
310                 qunit_sz = lqs->lqs_iunit_sz;
311                 tune_sz  = lqs->lqs_itune_sz;
312                 pending_write = lqs->lqs_iwrite_pending;
313                 record   = lqs->lqs_ino_rec;
314         }
315
316         /* we don't count the MIN_QLIMIT */
317         if ((limit == MIN_QLIMIT && !QDATA_IS_BLK(qdata)) ||
318             (toqb(limit) == MIN_QLIMIT && QDATA_IS_BLK(qdata)))
319                 limit = 0;
320
321         usage += pending_write;
322         limit_org = limit;
323         /* when a releasing quota req is sent, before it returned
324            limit is assigned a small value. limit will overflow */
325         if (limit + record < 0)
326                 usage -= record;
327         else
328                 limit += record;
329
330         LASSERT(qdata->qd_count == 0);
331         if (limit <= usage + tune_sz) {
332                 while (qdata->qd_count + limit <=
333                        usage + tune_sz)
334                         qdata->qd_count += qunit_sz;
335                 ret = 1;
336         } else if (limit > usage + qunit_sz + tune_sz &&
337                    limit_org > qdata->qd_count + qunit_sz) {
338                 while (limit - qdata->qd_count > usage + qunit_sz + tune_sz &&
339                        limit_org > qdata->qd_count + qunit_sz)
340                         qdata->qd_count += qunit_sz;
341                 ret = 2;
342                 /* if there are other pending writes for this uid/gid, releasing
343                  * quota is put off until the last pending write b=16645 */
344                 if (ret == 2 && pending_write) {
345                         CDEBUG(D_QUOTA, "delay quota release\n");
346                         ret = 0;
347                 }
348         }
349         CDEBUG(D_QUOTA, "type: %c, limit: "LPU64", usage: "LPU64
350                ", pending_write: "LPU64", record: "LPD64
351                ", qunit_sz: %lu, tune_sz: %lu, ret: %d.\n",
352                QDATA_IS_BLK(qdata) ? 'b' : 'i', limit, usage, pending_write,
353                record, qunit_sz, tune_sz, ret);
354         LASSERT(ret == 0 || qdata->qd_count);
355
356         spin_unlock(&lqs->lqs_lock);
357         lqs_putref(lqs);
358         EXIT;
359  out:
360         OBD_FREE_PTR(qctl);
361         return ret;
362 }
363
364 /**
365  * Compute the remaining quota for certain gid or uid b=11693
366  */
367 int compute_remquota(struct obd_device *obd, struct lustre_quota_ctxt *qctxt,
368                      struct qunit_data *qdata, int isblk)
369 {
370         struct super_block *sb = qctxt->lqc_sb;
371         __u64 usage, limit;
372         struct obd_quotactl *qctl;
373         int ret = QUOTA_RET_OK;
374         ENTRY;
375
376         if (!sb_any_quota_enabled(sb))
377                 RETURN(QUOTA_RET_NOQUOTA);
378
379         /* ignore root user */
380         if (qdata->qd_id == 0 && QDATA_IS_GRP(qdata) == USRQUOTA)
381                 RETURN(QUOTA_RET_NOLIMIT);
382
383         OBD_ALLOC_PTR(qctl);
384         if (qctl == NULL)
385                 RETURN(-ENOMEM);
386
387         /* get fs quota usage & limit */
388         qctl->qc_cmd = Q_GETQUOTA;
389         qctl->qc_id = qdata->qd_id;
390         qctl->qc_type = QDATA_IS_GRP(qdata);
391         ret = fsfilt_quotactl(obd, sb, qctl);
392         if (ret) {
393                 if (ret == -ESRCH)      /* no limit */
394                         ret = QUOTA_RET_NOLIMIT;
395                 else
396                         CDEBUG(D_QUOTA, "can't get fs quota usage! (rc:%d)",
397                                ret);
398                 GOTO(out, ret);
399         }
400
401         usage = isblk ? qctl->qc_dqblk.dqb_curspace :
402                 qctl->qc_dqblk.dqb_curinodes;
403         limit = isblk ? qctl->qc_dqblk.dqb_bhardlimit << QUOTABLOCK_BITS :
404                 qctl->qc_dqblk.dqb_ihardlimit;
405         if (!limit){            /* no limit */
406                 ret = QUOTA_RET_NOLIMIT;
407                 GOTO(out, ret);
408         }
409
410         if (limit >= usage)
411                 qdata->qd_count = limit - usage;
412         else
413                 qdata->qd_count = 0;
414         EXIT;
415 out:
416         OBD_FREE_PTR(qctl);
417         return ret;
418 }
419
420 /* caller must hold qunit_hash_lock */
421 static struct lustre_qunit *dqacq_in_flight(struct lustre_quota_ctxt *qctxt,
422                                             struct qunit_data *qdata)
423 {
424         unsigned int hashent = qunit_hashfn(qctxt, qdata);
425         struct lustre_qunit *qunit;
426         ENTRY;
427
428         LASSERT_SPIN_LOCKED(&qunit_hash_lock);
429         qunit = find_qunit(hashent, qctxt, qdata);
430         RETURN(qunit);
431 }
432
433 static struct lustre_qunit *alloc_qunit(struct lustre_quota_ctxt *qctxt,
434                                         struct qunit_data *qdata, int opc)
435 {
436         struct lustre_qunit *qunit = NULL;
437         ENTRY;
438
439         OBD_SLAB_ALLOC(qunit, qunit_cachep, CFS_ALLOC_IO, sizeof(*qunit));
440         if (qunit == NULL)
441                 RETURN(NULL);
442
443         CFS_INIT_LIST_HEAD(&qunit->lq_hash);
444         init_waitqueue_head(&qunit->lq_waitq);
445         atomic_set(&qunit->lq_refcnt, 1);
446         qunit->lq_ctxt = qctxt;
447         memcpy(&qunit->lq_data, qdata, sizeof(*qdata));
448         qunit->lq_opc = opc;
449         qunit->lq_lock = SPIN_LOCK_UNLOCKED;
450         QUNIT_SET_STATE_AND_RC(qunit, QUNIT_CREATED, 0);
451         RETURN(qunit);
452 }
453
454 static inline void free_qunit(struct lustre_qunit *qunit)
455 {
456         OBD_SLAB_FREE(qunit, qunit_cachep, sizeof(*qunit));
457 }
458
459 static inline void qunit_get(struct lustre_qunit *qunit)
460 {
461         atomic_inc(&qunit->lq_refcnt);
462 }
463
464 static void qunit_put(struct lustre_qunit *qunit)
465 {
466         LASSERT(atomic_read(&qunit->lq_refcnt));
467         if (atomic_dec_and_test(&qunit->lq_refcnt))
468                 free_qunit(qunit);
469 }
470
471 static void
472 insert_qunit_nolock(struct lustre_quota_ctxt *qctxt, struct lustre_qunit *qunit)
473 {
474         struct list_head *head;
475
476         LASSERT(list_empty(&qunit->lq_hash));
477         qunit_get(qunit);
478         head = qunit_hash + qunit_hashfn(qctxt, &qunit->lq_data);
479         list_add(&qunit->lq_hash, head);
480         QUNIT_SET_STATE(qunit, QUNIT_IN_HASH);
481 }
482
483 static void compute_lqs_after_removing_qunit(struct lustre_qunit *qunit)
484 {
485         struct lustre_qunit_size *lqs = NULL;
486
487         quota_search_lqs(&qunit->lq_data, NULL, qunit->lq_ctxt, &lqs);
488         if (lqs) {
489                 spin_lock(&lqs->lqs_lock);
490                 if (qunit->lq_opc == QUOTA_DQACQ)
491                         quota_compute_lqs(&qunit->lq_data, lqs, 0, 1);
492                 if (qunit->lq_opc == QUOTA_DQREL)
493                         quota_compute_lqs(&qunit->lq_data, lqs, 0, 0);
494                 spin_unlock(&lqs->lqs_lock);
495                 /* this is for quota_search_lqs */
496                 lqs_putref(lqs);
497                 /* this is for schedule_dqacq */
498                 lqs_putref(lqs);
499         }
500
501 }
502
503 static void remove_qunit_nolock(struct lustre_qunit *qunit)
504 {
505         LASSERT(!list_empty(&qunit->lq_hash));
506         LASSERT_SPIN_LOCKED(&qunit_hash_lock);
507
508         list_del_init(&qunit->lq_hash);
509         QUNIT_SET_STATE(qunit, QUNIT_RM_FROM_HASH);
510         qunit_put(qunit);
511 }
512
513 #define INC_QLIMIT(limit, count) (limit == MIN_QLIMIT) ? \
514                                  (limit = count) : (limit += count)
515
516
517 static inline int is_master(struct lustre_quota_ctxt *qctxt)
518 {
519         return qctxt->lqc_handler ? 1 : 0;
520 }
521
522 static int
523 schedule_dqacq(struct obd_device *obd, struct lustre_quota_ctxt *qctxt,
524                struct qunit_data *qdata, int opc, int wait,
525                struct obd_trans_info *oti);
526
527 static int
528 dqacq_completion(struct obd_device *obd, struct lustre_quota_ctxt *qctxt,
529                  struct qunit_data *qdata, int rc, int opc)
530 {
531         struct lustre_qunit *qunit = NULL;
532         struct super_block *sb = qctxt->lqc_sb;
533         int err = 0;
534         struct quota_adjust_qunit *oqaq = NULL;
535         int rc1 = 0;
536         ENTRY;
537
538         LASSERT(qdata);
539         QDATA_DEBUG(qdata, "obd(%s): complete %s quota req\n",
540                     obd->obd_name, (opc == QUOTA_DQACQ) ? "acq" : "rel");
541
542         /* update local operational quota file */
543         if (rc == 0) {
544                 __u64 count = QUSG(qdata->qd_count, QDATA_IS_BLK(qdata));
545                 struct obd_quotactl *qctl;
546                 __u64 *hardlimit;
547
548                 OBD_ALLOC_PTR(qctl);
549                 if (qctl == NULL)
550                         GOTO(out, err = -ENOMEM);
551
552                 /* acq/rel qunit for specified uid/gid is serialized,
553                  * so there is no race between get fs quota limit and
554                  * set fs quota limit */
555                 qctl->qc_cmd = Q_GETQUOTA;
556                 qctl->qc_id = qdata->qd_id;
557                 qctl->qc_type = QDATA_IS_GRP(qdata);
558                 err = fsfilt_quotactl(obd, sb, qctl);
559                 if (err) {
560                         CERROR("error get quota fs limit! (rc:%d)\n", err);
561                         GOTO(out_mem, err);
562                 }
563
564                 if (QDATA_IS_BLK(qdata)) {
565                         qctl->qc_dqblk.dqb_valid = QIF_BLIMITS;
566                         hardlimit = &qctl->qc_dqblk.dqb_bhardlimit;
567                 } else {
568                         qctl->qc_dqblk.dqb_valid = QIF_ILIMITS;
569                         hardlimit = &qctl->qc_dqblk.dqb_ihardlimit;
570                 }
571
572                 CDEBUG(D_QUOTA, "hardlimt: "LPU64"\n", *hardlimit);
573
574                 if (*hardlimit == 0)
575                         goto out_mem;
576
577                 switch (opc) {
578                 case QUOTA_DQACQ:
579                         INC_QLIMIT(*hardlimit, count);
580                         break;
581                 case QUOTA_DQREL:
582                         LASSERTF(count < *hardlimit,
583                                  "id(%u) flag(%u) type(%c) isblk(%c) "
584                                  "count("LPU64") qd_qunit("LPU64") "
585                                  "hardlimit("LPU64").\n",
586                                  qdata->qd_id, qdata->qd_flags,
587                                  QDATA_IS_GRP(qdata) ? 'g' : 'u',
588                                  QDATA_IS_BLK(qdata) ? 'b': 'i',
589                                  qdata->qd_count, qdata->qd_qunit, *hardlimit);
590                         *hardlimit -= count;
591                         break;
592                 default:
593                         LBUG();
594                 }
595
596                 /* clear quota limit */
597                 if (count == 0)
598                         *hardlimit = 0;
599
600                 qctl->qc_cmd = Q_SETQUOTA;
601                 err = fsfilt_quotactl(obd, sb, qctl);
602                 if (err)
603                         CERROR("error set quota fs limit! (rc:%d)\n", err);
604
605                 QDATA_DEBUG(qdata, "%s completion\n",
606                             opc == QUOTA_DQACQ ? "DQACQ" : "DQREL");
607 out_mem:
608                 OBD_FREE_PTR(qctl);
609         } else if (rc == -EDQUOT) {
610                 QDATA_DEBUG(qdata, "acquire qunit got EDQUOT.\n");
611         } else if (rc == -EBUSY) {
612                 QDATA_DEBUG(qdata, "it's is recovering, got EBUSY.\n");
613         } else {
614                 CERROR("acquire qunit got error! (rc:%d)\n", rc);
615         }
616 out:
617         /* remove the qunit from hash */
618         spin_lock(&qunit_hash_lock);
619
620         qunit = dqacq_in_flight(qctxt, qdata);
621         /* this qunit has been removed by qctxt_cleanup() */
622         if (!qunit) {
623                 spin_unlock(&qunit_hash_lock);
624                 QDATA_DEBUG(qdata, "%s is discarded because qunit isn't found\n",
625                             opc == QUOTA_DQACQ ? "DQACQ" : "DQREL");
626                 RETURN(err);
627         }
628
629         LASSERT(opc == qunit->lq_opc);
630         /* remove this qunit from lq_hash so that new processes cannot be added
631          * to qunit->lq_waiters */
632         remove_qunit_nolock(qunit);
633         spin_unlock(&qunit_hash_lock);
634
635         compute_lqs_after_removing_qunit(qunit);
636
637         /* wake up all waiters */
638         QUNIT_SET_STATE_AND_RC(qunit, QUNIT_FINISHED, rc);
639         wake_up_all(&qunit->lq_waitq);
640
641         qunit_put(qunit);
642         if (rc < 0 && rc != -EDQUOT)
643                  RETURN(err);
644
645         /* don't reschedule in such cases:
646          *   - acq/rel failure and qunit isn't changed,
647          *     but not for quota recovery.
648          *   - local dqacq/dqrel.
649          *   - local disk io failure.
650          */
651          OBD_ALLOC_PTR(oqaq);
652          if (!oqaq)
653                  RETURN(-ENOMEM);
654          qdata_to_oqaq(qdata, oqaq);
655          /* adjust the qunit size in slaves */
656          rc1 = quota_adjust_slave_lqs(oqaq, qctxt);
657          OBD_FREE_PTR(oqaq);
658          if (rc1 < 0) {
659                  CERROR("adjust slave's qunit size failed!(rc:%d)\n", rc1);
660                  RETURN(rc1);
661          }
662          if (err || (rc && rc != -EBUSY && rc1 == 0) || is_master(qctxt))
663                 RETURN(err);
664
665         /* reschedule another dqacq/dqrel if needed */
666         qdata->qd_count = 0;
667         qdata->qd_flags &= LQUOTA_QUNIT_FLAGS;
668         rc1 = check_cur_qunit(obd, qctxt, qdata);
669         if (rc1 > 0) {
670                 int opc;
671                 opc = rc1 == 1 ? QUOTA_DQACQ : QUOTA_DQREL;
672                 rc1 = schedule_dqacq(obd, qctxt, qdata, opc, 0, NULL);
673                 QDATA_DEBUG(qdata, "reschedudle opc(%d) rc(%d)\n", opc, rc1);
674         }
675         RETURN(err);
676 }
677
678 struct dqacq_async_args {
679         struct lustre_quota_ctxt *aa_ctxt;
680         struct lustre_qunit *aa_qunit;
681 };
682
683 static int dqacq_interpret(const struct lu_env *env,
684                            struct ptlrpc_request *req, void *data, int rc)
685 {
686         struct dqacq_async_args *aa = (struct dqacq_async_args *)data;
687         struct lustre_quota_ctxt *qctxt = aa->aa_ctxt;
688         struct obd_device_target *obt = qctxt->lqc_obt;
689         struct lustre_qunit *qunit = aa->aa_qunit;
690         struct obd_device *obd = req->rq_import->imp_obd;
691         struct qunit_data *qdata = NULL;
692         int rc1 = 0;
693         ENTRY;
694
695         LASSERT(req);
696         LASSERT(req->rq_import);
697
698         /* there are several forms of qunit(historic causes), so we need to
699          * adjust qunit from slaves to the same form here */
700         OBD_ALLOC(qdata, sizeof(struct qunit_data));
701         if (!qdata)
702                 RETURN(-ENOMEM);
703
704         down_read(&obt->obt_rwsem);
705         /* if a quota req timeouts or is dropped, we should update quota
706          * statistics which will be handled in dqacq_completion. And in
707          * this situation we should get qdata from request instead of
708          * reply */
709         rc1 = quota_get_qdata(req, qdata,
710                               (rc != 0) ? QUOTA_REQUEST : QUOTA_REPLY,
711                               QUOTA_IMPORT);
712         if (rc1 < 0) {
713                 DEBUG_REQ(D_ERROR, req,
714                           "error unpacking qunit_data(rc: %d)\n", rc1);
715                 GOTO(exit, rc = rc1);
716         }
717
718         QDATA_DEBUG(qdata, "qdata: interpret rc(%d).\n", rc);
719         QDATA_DEBUG((&qunit->lq_data), "lq_data: \n");
720
721         if (qdata->qd_id != qunit->lq_data.qd_id ||
722             OBD_FAIL_CHECK(OBD_FAIL_QUOTA_RET_QDATA)) {
723                 CDEBUG(D_ERROR, "the returned qd_id isn't expected!"
724                        "(qdata: %u, lq_data: %u)\n", qdata->qd_id,
725                        qunit->lq_data.qd_id);
726                 qdata->qd_id = qunit->lq_data.qd_id;
727                 rc = -EPROTO;
728         }
729         if (QDATA_IS_GRP(qdata) != QDATA_IS_GRP(&qunit->lq_data)) {
730                 CDEBUG(D_ERROR, "the returned grp/usr isn't expected!"
731                        "(qdata: %u, lq_data: %u)\n", qdata->qd_flags,
732                        qunit->lq_data.qd_flags);
733                 if (QDATA_IS_GRP(&qunit->lq_data))
734                         QDATA_SET_GRP(qdata);
735                 else
736                         QDATA_CLR_GRP(qdata);
737                 rc = -EPROTO;
738         }
739         if (qdata->qd_count > qunit->lq_data.qd_count) {
740                 CDEBUG(D_ERROR, "the returned qd_count isn't expected!"
741                        "(qdata: "LPU64", lq_data: "LPU64")\n", qdata->qd_count,
742                        qunit->lq_data.qd_count);
743                 rc = -EPROTO;
744         }
745
746         rc = dqacq_completion(obd, qctxt, qdata, rc,
747                               lustre_msg_get_opc(req->rq_reqmsg));
748
749 exit:
750         up_read(&obt->obt_rwsem);
751         OBD_FREE(qdata, sizeof(struct qunit_data));
752
753         RETURN(rc);
754 }
755
756 /**
757  * check if quota master is online
758  */
759 int check_qm(struct lustre_quota_ctxt *qctxt)
760 {
761         int rc;
762         ENTRY;
763
764         spin_lock(&qctxt->lqc_lock);
765         /* quit waiting when mds is back or qctxt is cleaned up */
766         rc = qctxt->lqc_import || !qctxt->lqc_valid;
767         spin_unlock(&qctxt->lqc_lock);
768
769         RETURN(rc);
770 }
771
772 static int got_qunit(struct lustre_qunit *qunit)
773 {
774         int rc;
775         ENTRY;
776
777         spin_lock(&qunit->lq_lock);
778         switch (qunit->lq_state) {
779         case QUNIT_IN_HASH:
780         case QUNIT_RM_FROM_HASH:
781                 rc = 0;
782                 break;
783         case QUNIT_FINISHED:
784                 rc = 1;
785                 break;
786         default:
787                 rc = 0;
788                 CERROR("invalid qunit state %d\n", qunit->lq_state);
789         }
790         spin_unlock(&qunit->lq_lock);
791         RETURN(rc);
792 }
793
794 static int
795 schedule_dqacq(struct obd_device *obd, struct lustre_quota_ctxt *qctxt,
796                struct qunit_data *qdata, int opc, int wait,
797                struct obd_trans_info *oti)
798 {
799         struct lustre_qunit *qunit, *empty;
800         struct l_wait_info lwi = { 0 };
801         struct ptlrpc_request *req;
802         struct dqacq_async_args *aa;
803         struct obd_import *imp = NULL;
804         struct lustre_qunit_size *lqs = NULL;
805         struct timeval work_start;
806         struct timeval work_end;
807         long timediff;
808         int rc = 0;
809         ENTRY;
810
811         LASSERT(opc == QUOTA_DQACQ || opc == QUOTA_DQREL);
812         do_gettimeofday(&work_start);
813         if ((empty = alloc_qunit(qctxt, qdata, opc)) == NULL)
814                 RETURN(-ENOMEM);
815
816         spin_lock(&qunit_hash_lock);
817         qunit = dqacq_in_flight(qctxt, qdata);
818         if (qunit) {
819                 if (wait)
820                         qunit_get(qunit);
821                 spin_unlock(&qunit_hash_lock);
822                 qunit_put(empty);
823
824                 goto wait_completion;
825         }
826         qunit = empty;
827         insert_qunit_nolock(qctxt, qunit);
828         spin_unlock(&qunit_hash_lock);
829
830         LASSERT(qunit);
831
832         quota_search_lqs(qdata, NULL, qctxt, &lqs);
833         if (lqs) {
834                 spin_lock(&lqs->lqs_lock);
835                 quota_compute_lqs(qdata, lqs, 1, (opc == QUOTA_DQACQ) ? 1 : 0);
836                 /* when this qdata returned from mds, it will call lqs_putref */
837                 lqs_getref(lqs);
838                 spin_unlock(&lqs->lqs_lock);
839                 /* this is for quota_search_lqs */
840                 lqs_putref(lqs);
841         } else {
842                 CDEBUG(D_ERROR, "Can't find the lustre qunit size!\n");
843         }
844
845         QDATA_DEBUG(qdata, "obd(%s): send %s quota req\n",
846                     obd->obd_name, (opc == QUOTA_DQACQ) ? "acq" : "rel");
847         /* master is going to dqacq/dqrel from itself */
848         if (is_master(qctxt)) {
849                 int rc2;
850                 QDATA_DEBUG(qdata, "local %s.\n",
851                             opc == QUOTA_DQACQ ? "DQACQ" : "DQREL");
852                 QDATA_SET_CHANGE_QS(qdata);
853                 rc = qctxt->lqc_handler(obd, qdata, opc);
854                 rc2 = dqacq_completion(obd, qctxt, qdata, rc, opc);
855                 do_gettimeofday(&work_end);
856                 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
857                 if (opc == QUOTA_DQACQ)
858                         lprocfs_counter_add(qctxt->lqc_stats,
859                                             wait ? LQUOTA_SYNC_ACQ : LQUOTA_ASYNC_ACQ,
860                                             timediff);
861                 else
862                         lprocfs_counter_add(qctxt->lqc_stats,
863                                             wait ? LQUOTA_SYNC_REL : LQUOTA_ASYNC_REL,
864                                             timediff);
865                 RETURN(rc ? rc : rc2);
866         }
867
868         spin_lock(&qctxt->lqc_lock);
869         if (!qctxt->lqc_import) {
870                 spin_unlock(&qctxt->lqc_lock);
871                 QDATA_DEBUG(qdata, "lqc_import is invalid.\n");
872
873                 spin_lock(&qunit_hash_lock);
874                 remove_qunit_nolock(qunit);
875                 spin_unlock(&qunit_hash_lock);
876
877                 compute_lqs_after_removing_qunit(qunit);
878
879                 QUNIT_SET_STATE_AND_RC(qunit, QUNIT_FINISHED, -EAGAIN);
880                 wake_up_all(&qunit->lq_waitq);
881
882                 qunit_put(qunit);
883                 spin_lock(&qctxt->lqc_lock);
884                 if (wait && !qctxt->lqc_import) {
885                         spin_unlock(&qctxt->lqc_lock);
886
887                         LASSERT(oti && oti->oti_thread &&
888                                 oti->oti_thread->t_watchdog);
889
890                         lc_watchdog_disable(oti->oti_thread->t_watchdog);
891                         CDEBUG(D_QUOTA, "sleep for quota master\n");
892                         l_wait_event(qctxt->lqc_wait_for_qmaster,
893                                      check_qm(qctxt), &lwi);
894                         CDEBUG(D_QUOTA, "wake up when quota master is back\n");
895                         lc_watchdog_touch(oti->oti_thread->t_watchdog);
896                 } else {
897                         spin_unlock(&qctxt->lqc_lock);
898                 }
899
900                 RETURN(-EAGAIN);
901         }
902         imp = class_import_get(qctxt->lqc_import);
903         spin_unlock(&qctxt->lqc_lock);
904
905         /* build dqacq/dqrel request */
906         LASSERT(imp);
907
908         req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_QUOTA_DQACQ,
909                                         LUSTRE_MDS_VERSION, opc);
910         class_import_put(imp);
911         if (req == NULL) {
912                 CDEBUG(D_ERROR, "Can't alloc request\n");
913                 dqacq_completion(obd, qctxt, qdata, -ENOMEM, opc);
914                 RETURN(-ENOMEM);
915         }
916
917         ptlrpc_request_set_replen(req);
918         req->rq_no_resend = req->rq_no_delay = 1;
919         rc = quota_copy_qdata(req, qdata, QUOTA_REQUEST, QUOTA_IMPORT);
920         if (rc < 0) {
921                 CDEBUG(D_ERROR, "Can't pack qunit_data(rc: %d)\n", rc);
922                 ptlrpc_req_finished(req);
923                 dqacq_completion(obd, qctxt, qdata, -EPROTO, opc);
924                 RETURN(rc);
925         }
926
927         if (wait && qunit)
928                 qunit_get(qunit);
929
930         CLASSERT(sizeof(*aa) <= sizeof(req->rq_async_args));
931         aa = ptlrpc_req_async_args(req);
932         aa->aa_ctxt = qctxt;
933         aa->aa_qunit = qunit;
934
935         req->rq_interpret_reply = dqacq_interpret;
936         ptlrpcd_add_req(req, PSCOPE_OTHER);
937
938         QDATA_DEBUG(qdata, "%s scheduled.\n",
939                     opc == QUOTA_DQACQ ? "DQACQ" : "DQREL");
940 wait_completion:
941         if (wait && qunit) {
942                 struct qunit_data *p = &qunit->lq_data;
943
944                 QDATA_DEBUG(p, "qunit(%p) is waiting for dqacq.\n", qunit);
945                 l_wait_event(qunit->lq_waitq, got_qunit(qunit), &lwi);
946                 /* rc = -EAGAIN, it means a quota req is finished;
947                  * rc = -EDQUOT, it means out of quota
948                  * rc = -EBUSY, it means recovery is happening
949                  * other rc < 0, it means real errors, functions who call
950                  * schedule_dqacq should take care of this */
951                 spin_lock(&qunit->lq_lock);
952                 if (qunit->lq_rc == 0)
953                         rc = -EAGAIN;
954                 else
955                         rc = qunit->lq_rc;
956                 spin_unlock(&qunit->lq_lock);
957                 CDEBUG(D_QUOTA, "qunit(%p) finishes waiting. (rc:%d)\n",
958                        qunit, rc);
959                 qunit_put(qunit);
960         }
961
962         do_gettimeofday(&work_end);
963         timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
964         if (opc == QUOTA_DQACQ)
965                 lprocfs_counter_add(qctxt->lqc_stats,
966                                     wait ? LQUOTA_SYNC_ACQ : LQUOTA_ASYNC_ACQ,
967                                     timediff);
968         else
969                 lprocfs_counter_add(qctxt->lqc_stats,
970                                     wait ? LQUOTA_SYNC_REL : LQUOTA_ASYNC_REL,
971                                     timediff);
972
973         RETURN(rc);
974 }
975
976 int
977 qctxt_adjust_qunit(struct obd_device *obd, struct lustre_quota_ctxt *qctxt,
978                    uid_t uid, gid_t gid, __u32 isblk, int wait,
979                    struct obd_trans_info *oti)
980 {
981         int rc = 0, i = USRQUOTA;
982         __u32 id[MAXQUOTAS] = { uid, gid };
983         struct qunit_data qdata[MAXQUOTAS];
984         ENTRY;
985
986         CLASSERT(MAXQUOTAS < 4);
987         if (!sb_any_quota_enabled(qctxt->lqc_sb))
988                 RETURN(0);
989
990         for (i = 0; i < MAXQUOTAS; i++) {
991                 qdata[i].qd_id = id[i];
992                 qdata[i].qd_flags = i;
993                 if (isblk)
994                         QDATA_SET_BLK(&qdata[i]);
995                 qdata[i].qd_count = 0;
996
997                 rc = check_cur_qunit(obd, qctxt, &qdata[i]);
998                 if (rc > 0) {
999                         int opc;
1000                         /* need acquire or release */
1001                         opc = rc == 1 ? QUOTA_DQACQ : QUOTA_DQREL;
1002                         rc = schedule_dqacq(obd, qctxt, &qdata[i], opc,
1003                                             wait,oti);
1004                         if (rc < 0)
1005                                 RETURN(rc);
1006                 } else if (wait == 1) {
1007                         /* when wait equates 1, that means mds_quota_acquire
1008                          * or filter_quota_acquire is calling it. */
1009                         rc = qctxt_wait_pending_dqacq(qctxt, id[i], i, isblk);
1010                         if (rc < 0)
1011                                 RETURN(rc);
1012                 }
1013         }
1014
1015         RETURN(rc);
1016 }
1017
1018 int
1019 qctxt_wait_pending_dqacq(struct lustre_quota_ctxt *qctxt, unsigned int id,
1020                          unsigned short type, int isblk)
1021 {
1022         struct lustre_qunit *qunit = NULL;
1023         struct qunit_data qdata;
1024         struct timeval work_start;
1025         struct timeval work_end;
1026         long timediff;
1027         struct l_wait_info lwi = { 0 };
1028         int rc = 0;
1029         ENTRY;
1030
1031         do_gettimeofday(&work_start);
1032         qdata.qd_id = id;
1033         qdata.qd_flags = type;
1034         if (isblk)
1035                 QDATA_SET_BLK(&qdata);
1036         qdata.qd_count = 0;
1037
1038         spin_lock(&qunit_hash_lock);
1039         qunit = dqacq_in_flight(qctxt, &qdata);
1040         if (qunit)
1041                 /* grab reference on this qunit to handle races with
1042                  * dqacq_completion(). Otherwise, this qunit could be freed just
1043                  * after we release the qunit_hash_lock */
1044                 qunit_get(qunit);
1045         spin_unlock(&qunit_hash_lock);
1046
1047         if (qunit) {
1048                 struct qunit_data *p = &qunit->lq_data;
1049
1050                 QDATA_DEBUG(p, "qunit(%p) is waiting for dqacq.\n", qunit);
1051                 l_wait_event(qunit->lq_waitq, got_qunit(qunit), &lwi);
1052                 CDEBUG(D_QUOTA, "qunit(%p) finishes waiting. (rc:%d)\n",
1053                        qunit, qunit->lq_rc);
1054                 qunit_put(qunit);
1055                 do_gettimeofday(&work_end);
1056                 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
1057                 lprocfs_counter_add(qctxt->lqc_stats,
1058                                     isblk ? LQUOTA_WAIT_PENDING_BLK_QUOTA :
1059                                             LQUOTA_WAIT_PENDING_INO_QUOTA,
1060                                     timediff);
1061                 /* keep same as schedule_dqacq() b=17030 */
1062                 spin_lock(&qunit->lq_lock);
1063                 if (qunit->lq_rc == 0)
1064                         rc = -EAGAIN;
1065                 else
1066                         rc = qunit->lq_rc;
1067                 spin_unlock(&qunit->lq_lock);
1068         } else {
1069                 do_gettimeofday(&work_end);
1070                 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
1071                 lprocfs_counter_add(qctxt->lqc_stats,
1072                                     isblk ? LQUOTA_NOWAIT_PENDING_BLK_QUOTA :
1073                                             LQUOTA_NOWAIT_PENDING_INO_QUOTA,
1074                                     timediff);
1075         }
1076
1077         RETURN(rc);
1078 }
1079
1080 int
1081 qctxt_init(struct obd_device *obd, dqacq_handler_t handler)
1082 {
1083         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
1084         struct obd_device_target *obt = &obd->u.obt;
1085         struct super_block *sb = obt->obt_sb;
1086         int rc = 0;
1087         ENTRY;
1088
1089         LASSERT(qctxt);
1090
1091         rc = ptlrpcd_addref();
1092         if (rc)
1093                 RETURN(rc);
1094
1095         cfs_waitq_init(&qctxt->lqc_wait_for_qmaster);
1096         spin_lock_init(&qctxt->lqc_lock);
1097         spin_lock(&qctxt->lqc_lock);
1098         qctxt->lqc_handler = handler;
1099         qctxt->lqc_sb = sb;
1100         qctxt->lqc_obt = obt;
1101         qctxt->lqc_import = NULL;
1102         qctxt->lqc_recovery = 0;
1103         qctxt->lqc_switch_qs = 1; /* Change qunit size in default setting */
1104         qctxt->lqc_valid = 1;
1105         qctxt->lqc_cqs_boundary_factor = 4;
1106         qctxt->lqc_cqs_least_bunit = PTLRPC_MAX_BRW_SIZE;
1107         qctxt->lqc_cqs_least_iunit = 2;
1108         qctxt->lqc_cqs_qs_factor = 2;
1109         qctxt->lqc_flags = 0;
1110         QUOTA_MASTER_UNREADY(qctxt);
1111         qctxt->lqc_bunit_sz = default_bunit_sz;
1112         qctxt->lqc_btune_sz = default_bunit_sz / 100 * default_btune_ratio;
1113         qctxt->lqc_iunit_sz = default_iunit_sz;
1114         qctxt->lqc_itune_sz = default_iunit_sz * default_itune_ratio / 100;
1115         qctxt->lqc_switch_seconds = 300; /* enlarging will wait 5 minutes
1116                                           * after the last shrinking */
1117         qctxt->lqc_sync_blk = 0;
1118         spin_unlock(&qctxt->lqc_lock);
1119
1120         qctxt->lqc_lqs_hash = lustre_hash_init("LQS_HASH", 7, 7,
1121                                                &lqs_hash_ops, 0);
1122         if (!qctxt->lqc_lqs_hash) {
1123                 CERROR("initialize hash lqs for %s error!\n", obd->obd_name);
1124                 RETURN(-ENOMEM);
1125         }
1126
1127 #ifdef LPROCFS
1128         rc = lquota_proc_setup(obd, is_master(qctxt));
1129         if (rc)
1130                 CERROR("initialize proc for %s error!\n", obd->obd_name);
1131 #endif
1132
1133         RETURN(rc);
1134 }
1135
1136 void qctxt_cleanup(struct lustre_quota_ctxt *qctxt, int force)
1137 {
1138         struct lustre_qunit *qunit, *tmp;
1139         struct list_head tmp_list;
1140         struct obd_device_target *obt = qctxt->lqc_obt;
1141         int i;
1142         ENTRY;
1143
1144         CFS_INIT_LIST_HEAD(&tmp_list);
1145
1146         spin_lock(&qctxt->lqc_lock);
1147         qctxt->lqc_valid = 0;
1148         spin_unlock(&qctxt->lqc_lock);
1149
1150         spin_lock(&qunit_hash_lock);
1151         for (i = 0; i < NR_DQHASH; i++) {
1152                 list_for_each_entry_safe(qunit, tmp, &qunit_hash[i], lq_hash) {
1153                         if (qunit->lq_ctxt != qctxt)
1154                                 continue;
1155                         remove_qunit_nolock(qunit);
1156                         list_add(&qunit->lq_hash, &tmp_list);
1157                 }
1158         }
1159         spin_unlock(&qunit_hash_lock);
1160
1161         list_for_each_entry_safe(qunit, tmp, &tmp_list, lq_hash) {
1162                 list_del_init(&qunit->lq_hash);
1163                 compute_lqs_after_removing_qunit(qunit);
1164
1165                 /* wake up all waiters */
1166                 QUNIT_SET_STATE_AND_RC(qunit, QUNIT_FINISHED, 0);
1167                 wake_up_all(&qunit->lq_waitq);
1168                 qunit_put(qunit);
1169         }
1170
1171         down_write(&obt->obt_rwsem);
1172         lustre_hash_exit(qctxt->lqc_lqs_hash);
1173         qctxt->lqc_lqs_hash = NULL;
1174         up_write(&obt->obt_rwsem);
1175
1176         /* after qctxt_cleanup, qctxt might be freed, then check_qm() is
1177          * unpredicted. So we must wait until lqc_wait_for_qmaster is empty */
1178         while (cfs_waitq_active(&qctxt->lqc_wait_for_qmaster)) {
1179                 cfs_waitq_signal(&qctxt->lqc_wait_for_qmaster);
1180                 cfs_schedule_timeout(CFS_TASK_INTERRUPTIBLE,
1181                                      cfs_time_seconds(1));
1182         }
1183
1184         ptlrpcd_decref();
1185
1186 #ifdef LPROCFS
1187         if (lquota_proc_cleanup(qctxt))
1188                 CERROR("cleanup proc error!\n");
1189 #endif
1190
1191         EXIT;
1192 }
1193
1194 struct qslave_recov_thread_data {
1195         struct obd_device *obd;
1196         struct lustre_quota_ctxt *qctxt;
1197         struct completion comp;
1198 };
1199
1200 /* FIXME only recovery block quota by now */
1201 static int qslave_recovery_main(void *arg)
1202 {
1203         struct qslave_recov_thread_data *data = arg;
1204         struct obd_device *obd = data->obd;
1205         struct lustre_quota_ctxt *qctxt = data->qctxt;
1206         unsigned int type;
1207         int rc = 0;
1208         ENTRY;
1209
1210         ptlrpc_daemonize("qslave_recovd");
1211
1212         complete(&data->comp);
1213
1214         if (qctxt->lqc_recovery)
1215                 RETURN(0);
1216         qctxt->lqc_recovery = 1;
1217
1218         for (type = USRQUOTA; type < MAXQUOTAS; type++) {
1219                 struct qunit_data qdata;
1220                 struct quota_info *dqopt = sb_dqopt(qctxt->lqc_sb);
1221                 struct list_head id_list;
1222                 struct dquot_id *dqid, *tmp;
1223                 int ret;
1224
1225                 LOCK_DQONOFF_MUTEX(dqopt);
1226                 if (!sb_has_quota_enabled(qctxt->lqc_sb, type)) {
1227                         UNLOCK_DQONOFF_MUTEX(dqopt);
1228                         break;
1229                 }
1230
1231                 LASSERT(dqopt->files[type] != NULL);
1232                 CFS_INIT_LIST_HEAD(&id_list);
1233 #ifndef KERNEL_SUPPORTS_QUOTA_READ
1234                 rc = fsfilt_qids(obd, dqopt->files[type], NULL, type, &id_list);
1235 #else
1236                 rc = fsfilt_qids(obd, NULL, dqopt->files[type], type, &id_list);
1237 #endif
1238                 UNLOCK_DQONOFF_MUTEX(dqopt);
1239                 if (rc)
1240                         CERROR("Get ids from quota file failed. (rc:%d)\n", rc);
1241
1242                 list_for_each_entry_safe(dqid, tmp, &id_list, di_link) {
1243                         list_del_init(&dqid->di_link);
1244                         /* skip slave recovery on itself */
1245                         if (is_master(qctxt))
1246                                 goto free;
1247                         if (rc && rc != -EBUSY)
1248                                 goto free;
1249
1250                         qdata.qd_id = dqid->di_id;
1251                         qdata.qd_flags = type;
1252                         QDATA_SET_BLK(&qdata);
1253                         qdata.qd_count = 0;
1254
1255                         ret = check_cur_qunit(obd, qctxt, &qdata);
1256                         if (ret > 0) {
1257                                 int opc;
1258                                 opc = ret == 1 ? QUOTA_DQACQ : QUOTA_DQREL;
1259                                 rc = schedule_dqacq(obd, qctxt, &qdata, opc,
1260                                                     0, NULL);
1261                                 if (rc == -EDQUOT)
1262                                         rc = 0;
1263                         } else {
1264                                 rc = 0;
1265                         }
1266
1267                         if (rc)
1268                                 CDEBUG(rc == -EBUSY ? D_QUOTA : D_ERROR,
1269                                        "qslave recovery failed! (id:%d type:%d "
1270                                        " rc:%d)\n", dqid->di_id, type, rc);
1271 free:
1272                         kfree(dqid);
1273                 }
1274         }
1275
1276         qctxt->lqc_recovery = 0;
1277         RETURN(rc);
1278 }
1279
1280 void
1281 qslave_start_recovery(struct obd_device *obd, struct lustre_quota_ctxt *qctxt)
1282 {
1283         struct qslave_recov_thread_data data;
1284         int rc;
1285         ENTRY;
1286
1287         if (!sb_any_quota_enabled(qctxt->lqc_sb))
1288                 goto exit;
1289
1290         data.obd = obd;
1291         data.qctxt = qctxt;
1292         init_completion(&data.comp);
1293
1294         rc = kernel_thread(qslave_recovery_main, &data, CLONE_VM|CLONE_FILES);
1295         if (rc < 0) {
1296                 CERROR("Cannot start quota recovery thread: rc %d\n", rc);
1297                 goto exit;
1298         }
1299         wait_for_completion(&data.comp);
1300 exit:
1301         EXIT;
1302 }
1303
1304
1305 /**
1306  * lqs<->qctxt hash operations
1307  */
1308
1309 /**
1310  * string hashing using djb2 hash algorithm
1311  */
1312 static unsigned
1313 lqs_hash(lustre_hash_t *lh, void *key, unsigned mask)
1314 {
1315         struct quota_adjust_qunit *lqs_key;
1316         unsigned hash;
1317         ENTRY;
1318
1319         LASSERT(key);
1320         lqs_key = (struct quota_adjust_qunit *)key;
1321         hash = (QAQ_IS_GRP(lqs_key) ? 5381 : 5387) * lqs_key->qaq_id;
1322
1323         RETURN(hash & mask);
1324 }
1325
1326 static int
1327 lqs_compare(void *key, struct hlist_node *hnode)
1328 {
1329         struct quota_adjust_qunit *lqs_key;
1330         struct lustre_qunit_size *q;
1331         int rc;
1332         ENTRY;
1333
1334         LASSERT(key);
1335         lqs_key = (struct quota_adjust_qunit *)key;
1336         q = hlist_entry(hnode, struct lustre_qunit_size, lqs_hash);
1337
1338         spin_lock(&q->lqs_lock);
1339         rc = ((lqs_key->qaq_id == q->lqs_id) &&
1340               (QAQ_IS_GRP(lqs_key) == LQS_IS_GRP(q)));
1341         spin_unlock(&q->lqs_lock);
1342
1343         RETURN(rc);
1344 }
1345
1346 static void *
1347 lqs_get(struct hlist_node *hnode)
1348 {
1349         struct lustre_qunit_size *q = 
1350             hlist_entry(hnode, struct lustre_qunit_size, lqs_hash);
1351         ENTRY;
1352
1353         atomic_inc(&q->lqs_refcount);
1354         CDEBUG(D_QUOTA, "lqs=%p refcount %d\n",
1355                q, atomic_read(&q->lqs_refcount));
1356
1357         RETURN(q);
1358 }
1359
1360 static void *
1361 lqs_put(struct hlist_node *hnode)
1362 {
1363         struct lustre_qunit_size *q = 
1364             hlist_entry(hnode, struct lustre_qunit_size, lqs_hash);
1365         ENTRY;
1366
1367         LASSERT(atomic_read(&q->lqs_refcount) > 0);
1368         atomic_dec(&q->lqs_refcount);
1369         CDEBUG(D_QUOTA, "lqs=%p refcount %d\n",
1370                q, atomic_read(&q->lqs_refcount));
1371
1372         RETURN(q);
1373 }
1374
1375 static void
1376 lqs_exit(struct hlist_node *hnode)
1377 {
1378         struct lustre_qunit_size *q;
1379         ENTRY;
1380
1381         q = hlist_entry(hnode, struct lustre_qunit_size, lqs_hash);
1382         /* 
1383          * Nothing should be left. User of lqs put it and
1384          * lqs also was deleted from table by this time
1385          * so we should have 0 refs.
1386          */
1387         LASSERTF(atomic_read(&q->lqs_refcount) == 0, 
1388                  "Busy lqs %p with %d refs\n", q,
1389                  atomic_read(&q->lqs_refcount));
1390         OBD_FREE_PTR(q);
1391         EXIT;
1392 }
1393
1394 static lustre_hash_ops_t lqs_hash_ops = {
1395         .lh_hash    = lqs_hash,
1396         .lh_compare = lqs_compare,
1397         .lh_get     = lqs_get,
1398         .lh_put     = lqs_put,
1399         .lh_exit    = lqs_exit
1400 };
1401 #endif /* HAVE_QUOTA_SUPPORT */