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