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