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