Whamcloud - gitweb
9e4cac281ab1b33e2756002fdb4df31efd732e90
[fs/lustre-release.git] / lustre / quota / quota_context.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Whamcloud, Inc.
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 #define DEBUG_SUBSYSTEM S_LQUOTA
44
45 #include <linux/version.h>
46 #include <linux/fs.h>
47 #include <asm/unistd.h>
48 #include <linux/slab.h>
49 #include <linux/quotaops.h>
50 #include <linux/module.h>
51 #include <linux/init.h>
52
53 #include <obd_class.h>
54 #include <lustre_quota.h>
55 #include <lustre_fsfilt.h>
56 #include <lprocfs_status.h>
57 #include "quota_internal.h"
58
59 static int hash_lqs_cur_bits = HASH_LQS_CUR_BITS;
60 CFS_MODULE_PARM(hash_lqs_cur_bits, "i", int, 0444,
61                 "the current bits of lqs hash");
62
63 static cfs_hash_ops_t lqs_hash_ops;
64
65 unsigned long default_bunit_sz = 128 * 1024 * 1024; /* 128M bytes */
66 unsigned long default_btune_ratio = 50;             /* 50 percentage */
67 unsigned long default_iunit_sz = 5120;              /* 5120 inodes */
68 unsigned long default_itune_ratio = 50;             /* 50 percentage */
69
70 cfs_mem_cache_t *qunit_cachep = NULL;
71 cfs_list_t qunit_hash[NR_DQHASH];
72 cfs_spinlock_t qunit_hash_lock = CFS_SPIN_LOCK_UNLOCKED;
73
74 /* please sync qunit_state with qunit_state_names */
75 enum qunit_state {
76         /**
77          * a qunit is created
78          */
79         QUNIT_CREATED      = 0,
80         /**
81          * a qunit is added into qunit hash, that means
82          * a quota req will be sent or is flying
83          */
84         QUNIT_IN_HASH      = 1,
85         /**
86          * a qunit is removed from qunit hash, that
87          * means a quota req is handled and comes back
88          */
89         QUNIT_RM_FROM_HASH = 2,
90         /**
91          * qunit can wake up all threads waiting for it
92          */
93         QUNIT_FINISHED     = 3,
94 };
95
96 static const char *qunit_state_names[] = {
97         [QUNIT_CREATED]      = "CREATED",
98         [QUNIT_IN_HASH]      = "IN_HASH",
99         [QUNIT_RM_FROM_HASH] = "RM_FROM_HASH",
100         [QUNIT_FINISHED]     = "FINISHED",
101 };
102
103 struct lustre_qunit {
104         cfs_list_t lq_hash;      /** Hash list in memory */
105         cfs_atomic_t lq_refcnt;            /** Use count */
106         struct lustre_quota_ctxt *lq_ctxt; /** Quota context this applies to */
107         struct qunit_data lq_data;         /** See qunit_data */
108         unsigned int lq_opc;               /** QUOTA_DQACQ, QUOTA_DQREL */
109         cfs_waitq_t lq_waitq;              /** Threads waiting for this qunit */
110         cfs_spinlock_t lq_lock;            /** Protect the whole structure */
111         enum qunit_state lq_state;         /** Present the status of qunit */
112         int lq_rc;                         /** The rc of lq_data */
113         pid_t lq_owner;
114 };
115
116 #define QUNIT_SET_STATE(qunit, state)                                   \
117 do {                                                                    \
118         cfs_spin_lock(&qunit->lq_lock);                                 \
119         QDATA_DEBUG((&qunit->lq_data), "qunit(%p) lq_state(%s->%s), "   \
120                     "lq_rc(%d), lq_owner(%d)\n",                        \
121                     qunit, qunit_state_names[qunit->lq_state],          \
122                     qunit_state_names[state], qunit->lq_rc,             \
123                     qunit->lq_owner);                                   \
124         qunit->lq_state = state;                                        \
125         cfs_spin_unlock(&qunit->lq_lock);                               \
126 } while(0)
127
128 #define QUNIT_SET_STATE_AND_RC(qunit, state, rc)                        \
129 do {                                                                    \
130         cfs_spin_lock(&qunit->lq_lock);                                 \
131         qunit->lq_rc = rc;                                              \
132         QDATA_DEBUG((&qunit->lq_data), "qunit(%p) lq_state(%s->%s), "   \
133                     "lq_rc(%d), lq_owner(%d)\n",                        \
134                     qunit, qunit_state_names[qunit->lq_state],          \
135                     qunit_state_names[state], qunit->lq_rc,             \
136                     qunit->lq_owner);                                   \
137         qunit->lq_state = state;                                        \
138         cfs_spin_unlock(&qunit->lq_lock);                               \
139 } while(0)
140
141 int should_translate_quota (struct obd_import *imp)
142 {
143         ENTRY;
144
145         LASSERT(imp);
146         if (imp->imp_connect_data.ocd_connect_flags & OBD_CONNECT_QUOTA64)
147                 RETURN(0);
148         else
149                 RETURN(1);
150 }
151
152 void qunit_cache_cleanup(void)
153 {
154         int i;
155         ENTRY;
156
157         cfs_spin_lock(&qunit_hash_lock);
158         for (i = 0; i < NR_DQHASH; i++)
159                 LASSERT(cfs_list_empty(qunit_hash + i));
160         cfs_spin_unlock(&qunit_hash_lock);
161
162         if (qunit_cachep) {
163                 int rc;
164                 rc = cfs_mem_cache_destroy(qunit_cachep);
165                 LASSERTF(rc == 0, "couldn't destroy qunit_cache slab\n");
166                 qunit_cachep = NULL;
167         }
168         EXIT;
169 }
170
171 int qunit_cache_init(void)
172 {
173         int i;
174         ENTRY;
175
176         LASSERT(qunit_cachep == NULL);
177         qunit_cachep = cfs_mem_cache_create("ll_qunit_cache",
178                                             sizeof(struct lustre_qunit),
179                                             0, 0);
180         if (!qunit_cachep)
181                 RETURN(-ENOMEM);
182
183         cfs_spin_lock(&qunit_hash_lock);
184         for (i = 0; i < NR_DQHASH; i++)
185                 CFS_INIT_LIST_HEAD(qunit_hash + i);
186         cfs_spin_unlock(&qunit_hash_lock);
187         RETURN(0);
188 }
189
190 static inline int
191 qunit_hashfn(struct lustre_quota_ctxt *qctxt, struct qunit_data *qdata)
192              __attribute__((__const__));
193
194 static inline int
195 qunit_hashfn(struct lustre_quota_ctxt *qctxt, struct qunit_data *qdata)
196 {
197         unsigned int id = qdata->qd_id;
198         unsigned int type = QDATA_IS_GRP(qdata);
199
200         unsigned long tmp = ((unsigned long)qctxt >> L1_CACHE_SHIFT) ^ id;
201         tmp = (tmp * (MAXQUOTAS - type)) % NR_DQHASH;
202         return tmp;
203 }
204
205 /* caller must hold qunit_hash_lock */
206 static inline struct lustre_qunit *find_qunit(unsigned int hashent,
207                                               struct lustre_quota_ctxt *qctxt,
208                                               struct qunit_data *qdata)
209 {
210         struct lustre_qunit *qunit = NULL;
211         struct qunit_data *tmp;
212
213         LASSERT_SPIN_LOCKED(&qunit_hash_lock);
214         cfs_list_for_each_entry(qunit, qunit_hash + hashent, lq_hash) {
215                 tmp = &qunit->lq_data;
216                 if (qunit->lq_ctxt == qctxt &&
217                     qdata->qd_id == tmp->qd_id &&
218                     (qdata->qd_flags & LQUOTA_QUNIT_FLAGS) ==
219                     (tmp->qd_flags & LQUOTA_QUNIT_FLAGS))
220                         return qunit;
221         }
222         return NULL;
223 }
224
225 /* check_cur_qunit - check the current usage of qunit.
226  * @qctxt: quota context
227  * @qdata: the type of quota unit to be checked
228  *
229  * return: 1 - need acquire qunit;
230  *         2 - need release qunit;
231  *         0 - need do nothing.
232  *       < 0 - error.
233  */
234 static int
235 check_cur_qunit(struct obd_device *obd,
236                 struct lustre_quota_ctxt *qctxt, struct qunit_data *qdata)
237 {
238         struct super_block *sb = qctxt->lqc_sb;
239         unsigned long qunit_sz, tune_sz;
240         __u64 usage, limit, limit_org, pending_write = 0;
241         long long record = 0;
242         struct obd_quotactl *qctl;
243         struct lustre_qunit_size *lqs = NULL;
244         int ret = 0;
245         ENTRY;
246
247         if (!ll_sb_any_quota_active(sb))
248                 RETURN(0);
249
250         cfs_spin_lock(&qctxt->lqc_lock);
251         if (!qctxt->lqc_valid){
252                 cfs_spin_unlock(&qctxt->lqc_lock);
253                 RETURN(0);
254         }
255         cfs_spin_unlock(&qctxt->lqc_lock);
256
257         OBD_ALLOC_PTR(qctl);
258         if (qctl == NULL)
259                 RETURN(-ENOMEM);
260
261         /* get fs quota usage & limit */
262         qctl->qc_cmd = Q_GETQUOTA;
263         qctl->qc_id = qdata->qd_id;
264         qctl->qc_type = QDATA_IS_GRP(qdata);
265         ret = fsfilt_quotactl(obd, sb, qctl);
266         if (ret) {
267                 if (ret == -ESRCH)      /* no limit */
268                         ret = 0;
269                 else
270                         CERROR("can't get fs quota usage! (rc:%d)\n", ret);
271                 GOTO(out, ret);
272         }
273
274         if (QDATA_IS_BLK(qdata)) {
275                 usage = qctl->qc_dqblk.dqb_curspace;
276                 limit = qctl->qc_dqblk.dqb_bhardlimit << QUOTABLOCK_BITS;
277         } else {
278                 usage = qctl->qc_dqblk.dqb_curinodes;
279                 limit = qctl->qc_dqblk.dqb_ihardlimit;
280         }
281
282         /* ignore the no quota limit case; and it can avoid creating
283          * unnecessary lqs for uid/gid */
284         if (!limit)
285                 GOTO(out, ret = 0);
286
287         lqs = quota_search_lqs(LQS_KEY(QDATA_IS_GRP(qdata), qdata->qd_id),
288                                qctxt, 0);
289         if (IS_ERR(lqs) || lqs == NULL) {
290                 CERROR("fail to find a lqs for %sid: %u)!\n",
291                        QDATA_IS_GRP(qdata) ? "g" : "u", qdata->qd_id);
292                 GOTO (out, ret = 0);
293         }
294         cfs_spin_lock(&lqs->lqs_lock);
295
296         if (QDATA_IS_BLK(qdata)) {
297                 qunit_sz = lqs->lqs_bunit_sz;
298                 tune_sz  = lqs->lqs_btune_sz;
299                 pending_write = lqs->lqs_bwrite_pending;
300                 record   = lqs->lqs_blk_rec;
301                 LASSERT(!(qunit_sz % QUOTABLOCK_SIZE));
302         } else {
303                 /* we didn't need change inode qunit size now */
304                 qunit_sz = lqs->lqs_iunit_sz;
305                 tune_sz  = lqs->lqs_itune_sz;
306                 pending_write = lqs->lqs_iwrite_pending;
307                 record   = lqs->lqs_ino_rec;
308         }
309
310         /* we don't count the MIN_QLIMIT */
311         if ((limit == MIN_QLIMIT && !QDATA_IS_BLK(qdata)) ||
312             (toqb(limit) == MIN_QLIMIT && QDATA_IS_BLK(qdata)))
313                 limit = 0;
314
315         usage += pending_write;
316         limit_org = limit;
317         /* when a releasing quota req is sent, before it returned
318            limit is assigned a small value. limit will overflow */
319         if (record < 0)
320                 usage -= record;
321         else
322                 limit += record;
323
324         LASSERT(qdata->qd_count == 0);
325         if (limit <= usage + tune_sz) {
326                 while (qdata->qd_count + limit <=
327                        usage + tune_sz)
328                         qdata->qd_count += qunit_sz;
329                 ret = 1;
330         } else if (limit > usage + qunit_sz + tune_sz &&
331                    limit_org > qdata->qd_count + qunit_sz) {
332                 while (limit - qdata->qd_count > usage + qunit_sz + tune_sz &&
333                        limit_org > qdata->qd_count + qunit_sz)
334                         qdata->qd_count += qunit_sz;
335                 ret = 2;
336                 /* if there are other pending writes for this uid/gid, releasing
337                  * quota is put off until the last pending write b=16645 */
338                 /* if there is an ongoing quota request, a releasing request is aborted.
339                  * That ongoing quota request will call this function again when
340                  * it returned b=18630 */
341                 if (pending_write || record) {
342                         CDEBUG(D_QUOTA, "delay quota release\n");
343                         ret = 0;
344                 }
345         }
346         if (ret > 0)
347                 quota_compute_lqs(qdata, lqs, 1, (ret == 1) ? 1 : 0);
348
349         CDEBUG(D_QUOTA, "type: %c, limit: "LPU64", usage: "LPU64
350                ", pending_write: "LPU64", record: %lld"
351                ", qunit_sz: %lu, tune_sz: %lu, ret: %d.\n",
352                QDATA_IS_BLK(qdata) ? 'b' : 'i', limit, usage, pending_write,
353                record, qunit_sz, tune_sz, ret);
354         LASSERT(ret == 0 || qdata->qd_count);
355
356         cfs_spin_unlock(&lqs->lqs_lock);
357         lqs_putref(lqs);
358
359         EXIT;
360  out:
361         OBD_FREE_PTR(qctl);
362         return ret;
363 }
364
365 /**
366  * Compute the remaining quota for certain gid or uid b=11693
367  */
368 int compute_remquota(struct obd_device *obd, struct lustre_quota_ctxt *qctxt,
369                      struct qunit_data *qdata, int isblk)
370 {
371         struct super_block *sb = qctxt->lqc_sb;
372         __u64 usage, limit;
373         struct obd_quotactl *qctl;
374         int ret = QUOTA_RET_OK;
375         ENTRY;
376
377         if (!ll_sb_any_quota_active(sb))
378                 RETURN(QUOTA_RET_NOQUOTA);
379
380         /* ignore root user */
381         if (qdata->qd_id == 0 && QDATA_IS_GRP(qdata) == USRQUOTA)
382                 RETURN(QUOTA_RET_NOLIMIT);
383
384         OBD_ALLOC_PTR(qctl);
385         if (qctl == NULL)
386                 RETURN(-ENOMEM);
387
388         /* get fs quota usage & limit */
389         qctl->qc_cmd = Q_GETQUOTA;
390         qctl->qc_id = qdata->qd_id;
391         qctl->qc_type = QDATA_IS_GRP(qdata);
392         ret = fsfilt_quotactl(obd, sb, qctl);
393         if (ret) {
394                 if (ret == -ESRCH)      /* no limit */
395                         ret = QUOTA_RET_NOLIMIT;
396                 else
397                         CDEBUG(D_QUOTA, "can't get fs quota usage! (rc:%d)",
398                                ret);
399                 GOTO(out, ret);
400         }
401
402         usage = isblk ? qctl->qc_dqblk.dqb_curspace :
403                 qctl->qc_dqblk.dqb_curinodes;
404         limit = isblk ? qctl->qc_dqblk.dqb_bhardlimit << QUOTABLOCK_BITS :
405                 qctl->qc_dqblk.dqb_ihardlimit;
406         if (!limit){            /* no limit */
407                 ret = QUOTA_RET_NOLIMIT;
408                 GOTO(out, ret);
409         }
410
411         if (limit >= usage)
412                 qdata->qd_count = limit - usage;
413         else
414                 qdata->qd_count = 0;
415         EXIT;
416 out:
417         OBD_FREE_PTR(qctl);
418         return ret;
419 }
420
421 static struct lustre_qunit *alloc_qunit(struct lustre_quota_ctxt *qctxt,
422                                         struct qunit_data *qdata, int opc)
423 {
424         struct lustre_qunit *qunit = NULL;
425         ENTRY;
426
427         OBD_SLAB_ALLOC_PTR_GFP(qunit, qunit_cachep, CFS_ALLOC_IO);
428         if (qunit == NULL)
429                 RETURN(NULL);
430
431         CFS_INIT_LIST_HEAD(&qunit->lq_hash);
432         cfs_waitq_init(&qunit->lq_waitq);
433         cfs_atomic_set(&qunit->lq_refcnt, 1);
434         qunit->lq_ctxt = qctxt;
435         memcpy(&qunit->lq_data, qdata, sizeof(*qdata));
436         qunit->lq_opc = opc;
437         qunit->lq_lock = CFS_SPIN_LOCK_UNLOCKED;
438         QUNIT_SET_STATE_AND_RC(qunit, QUNIT_CREATED, 0);
439         qunit->lq_owner = cfs_curproc_pid();
440         RETURN(qunit);
441 }
442
443 static inline void free_qunit(struct lustre_qunit *qunit)
444 {
445         OBD_SLAB_FREE(qunit, qunit_cachep, sizeof(*qunit));
446 }
447
448 static inline void qunit_get(struct lustre_qunit *qunit)
449 {
450         cfs_atomic_inc(&qunit->lq_refcnt);
451 }
452
453 static void qunit_put(struct lustre_qunit *qunit)
454 {
455         LASSERT(cfs_atomic_read(&qunit->lq_refcnt));
456         if (cfs_atomic_dec_and_test(&qunit->lq_refcnt))
457                 free_qunit(qunit);
458 }
459
460 /* caller must hold qunit_hash_lock and release ref of qunit after using it */
461 static struct lustre_qunit *dqacq_in_flight(struct lustre_quota_ctxt *qctxt,
462                                             struct qunit_data *qdata)
463 {
464         unsigned int hashent = qunit_hashfn(qctxt, qdata);
465         struct lustre_qunit *qunit;
466         ENTRY;
467
468         LASSERT_SPIN_LOCKED(&qunit_hash_lock);
469         qunit = find_qunit(hashent, qctxt, qdata);
470         if (qunit)
471                 qunit_get(qunit);
472         RETURN(qunit);
473 }
474
475 static void
476 insert_qunit_nolock(struct lustre_quota_ctxt *qctxt, struct lustre_qunit *qunit)
477 {
478         cfs_list_t *head;
479
480         LASSERT(cfs_list_empty(&qunit->lq_hash));
481         qunit_get(qunit);
482         head = qunit_hash + qunit_hashfn(qctxt, &qunit->lq_data);
483         cfs_list_add(&qunit->lq_hash, head);
484         QUNIT_SET_STATE(qunit, QUNIT_IN_HASH);
485 }
486
487 static void compute_lqs_after_removing_qunit(struct lustre_qunit *qunit)
488 {
489         struct lustre_qunit_size *lqs;
490
491         lqs = quota_search_lqs(LQS_KEY(QDATA_IS_GRP(&qunit->lq_data),
492                                        qunit->lq_data.qd_id),
493                                qunit->lq_ctxt, 0);
494         if (lqs && !IS_ERR(lqs)) {
495                 cfs_spin_lock(&lqs->lqs_lock);
496                 if (qunit->lq_opc == QUOTA_DQACQ)
497                         quota_compute_lqs(&qunit->lq_data, lqs, 0, 1);
498                 if (qunit->lq_opc == QUOTA_DQREL)
499                         quota_compute_lqs(&qunit->lq_data, lqs, 0, 0);
500                 cfs_spin_unlock(&lqs->lqs_lock);
501                 /* this is for quota_search_lqs */
502                 lqs_putref(lqs);
503                 /* this is for schedule_dqacq */
504                 lqs_putref(lqs);
505         }
506 }
507
508 static void remove_qunit_nolock(struct lustre_qunit *qunit)
509 {
510         LASSERT(!cfs_list_empty(&qunit->lq_hash));
511         LASSERT_SPIN_LOCKED(&qunit_hash_lock);
512
513         cfs_list_del_init(&qunit->lq_hash);
514         QUNIT_SET_STATE(qunit, QUNIT_RM_FROM_HASH);
515         qunit_put(qunit);
516 }
517
518 void* quota_barrier(struct lustre_quota_ctxt *qctxt,
519                     struct obd_quotactl *oqctl, int isblk)
520 {
521         struct lustre_qunit *qunit, *find_qunit;
522         int cycle = 1;
523
524         OBD_SLAB_ALLOC_PTR(qunit, qunit_cachep);
525         if (qunit == NULL) {
526                 CERROR("locating %sunit failed for %sid %u\n",
527                        isblk ? "b" : "i", oqctl->qc_type ? "g" : "u",
528                        oqctl->qc_id);
529                 qctxt_wait_pending_dqacq(qctxt, oqctl->qc_id,
530                                          oqctl->qc_type, isblk);
531                 return NULL;
532         }
533
534         CFS_INIT_LIST_HEAD(&qunit->lq_hash);
535         qunit->lq_lock = CFS_SPIN_LOCK_UNLOCKED;
536         cfs_waitq_init(&qunit->lq_waitq);
537         cfs_atomic_set(&qunit->lq_refcnt, 1);
538         qunit->lq_ctxt = qctxt;
539         qunit->lq_data.qd_id = oqctl->qc_id;
540         qunit->lq_data.qd_flags =  oqctl->qc_type;
541         if (isblk)
542                 QDATA_SET_BLK(&qunit->lq_data);
543         QUNIT_SET_STATE_AND_RC(qunit, QUNIT_CREATED, 0);
544         /* it means it is only an invalid qunit for barrier */
545         qunit->lq_opc = QUOTA_LAST_OPC;
546
547         while (1) {
548                 cfs_spin_lock(&qunit_hash_lock);
549                 find_qunit = dqacq_in_flight(qctxt, &qunit->lq_data);
550                 if (find_qunit) {
551                         cfs_spin_unlock(&qunit_hash_lock);
552                         qunit_put(find_qunit);
553                         qctxt_wait_pending_dqacq(qctxt, oqctl->qc_id,
554                                                  oqctl->qc_type, isblk);
555                         CDEBUG(D_QUOTA, "cycle=%d\n", cycle++);
556                         continue;
557                 }
558                 break;
559         }
560         insert_qunit_nolock(qctxt, qunit);
561         cfs_spin_unlock(&qunit_hash_lock);
562         return qunit;
563 }
564
565 void quota_unbarrier(void *handle)
566 {
567         struct lustre_qunit *qunit = (struct lustre_qunit *)handle;
568
569         if (qunit == NULL) {
570                 CERROR("handle is NULL\n");
571                 return;
572         }
573
574         LASSERT(qunit->lq_opc == QUOTA_LAST_OPC);
575         cfs_spin_lock(&qunit_hash_lock);
576         remove_qunit_nolock(qunit);
577         cfs_spin_unlock(&qunit_hash_lock);
578         QUNIT_SET_STATE_AND_RC(qunit, QUNIT_FINISHED, QUOTA_REQ_RETURNED);
579         cfs_waitq_signal(&qunit->lq_waitq);
580         qunit_put(qunit);
581 }
582
583 #define INC_QLIMIT(limit, count) (limit == MIN_QLIMIT) ? \
584                                  (limit = count) : (limit += count)
585
586
587 static inline int is_master(struct lustre_quota_ctxt *qctxt)
588 {
589         return qctxt->lqc_handler ? 1 : 0;
590 }
591
592 static int
593 schedule_dqacq(struct obd_device *obd, struct lustre_quota_ctxt *qctxt,
594                struct qunit_data *qdata, int opc, int wait,
595                struct obd_trans_info *oti);
596
597 static inline void qdata_to_oqaq(struct qunit_data *qdata,
598                                  struct quota_adjust_qunit *oqaq)
599 {
600         LASSERT(qdata);
601         LASSERT(oqaq);
602
603         oqaq->qaq_flags = qdata->qd_flags;
604         oqaq->qaq_id    = qdata->qd_id;
605         if (QDATA_IS_ADJBLK(qdata))
606                 oqaq->qaq_bunit_sz = qdata->qd_qunit;
607         if (QDATA_IS_ADJINO(qdata))
608                 oqaq->qaq_iunit_sz = qdata->qd_qunit;
609 }
610
611 static int
612 dqacq_completion(struct obd_device *obd, struct lustre_quota_ctxt *qctxt,
613                  struct qunit_data *qdata, int rc, int opc)
614 {
615         struct lustre_qunit *qunit = NULL;
616         struct super_block *sb = qctxt->lqc_sb;
617         int err = 0;
618         struct quota_adjust_qunit *oqaq = NULL;
619         int rc1 = 0;
620         ENTRY;
621
622         LASSERT(qdata);
623         QDATA_DEBUG(qdata, "obd(%s): complete %s quota req\n",
624                     obd->obd_name, (opc == QUOTA_DQACQ) ? "acq" : "rel");
625
626         /* do it only when a releasing quota req more than 5MB b=18491 */
627         if (opc == QUOTA_DQREL && qdata->qd_count >= 5242880)
628                 OBD_FAIL_TIMEOUT(OBD_FAIL_QUOTA_DELAY_REL, 5);
629
630         /* update local operational quota file */
631         if (rc == 0) {
632                 __u64 count = QUSG(qdata->qd_count, QDATA_IS_BLK(qdata));
633                 struct obd_quotactl *qctl;
634                 __u64 *hardlimit;
635
636                 OBD_ALLOC_PTR(qctl);
637                 if (qctl == NULL)
638                         GOTO(out, err = -ENOMEM);
639
640                 /* acq/rel qunit for specified uid/gid is serialized,
641                  * so there is no race between get fs quota limit and
642                  * set fs quota limit */
643                 qctl->qc_cmd = Q_GETQUOTA;
644                 qctl->qc_id = qdata->qd_id;
645                 qctl->qc_type = QDATA_IS_GRP(qdata);
646                 err = fsfilt_quotactl(obd, sb, qctl);
647                 if (err) {
648                         CERROR("error get quota fs limit! (rc:%d)\n", err);
649                         GOTO(out_mem, err);
650                 }
651
652                 if (QDATA_IS_BLK(qdata)) {
653                         qctl->qc_dqblk.dqb_valid = QIF_BLIMITS;
654                         hardlimit = &qctl->qc_dqblk.dqb_bhardlimit;
655                 } else {
656                         qctl->qc_dqblk.dqb_valid = QIF_ILIMITS;
657                         hardlimit = &qctl->qc_dqblk.dqb_ihardlimit;
658                 }
659
660                 CDEBUG(D_QUOTA, "hardlimt: "LPU64"\n", *hardlimit);
661
662                 if (*hardlimit == 0)
663                         goto out_mem;
664
665                 switch (opc) {
666                 case QUOTA_DQACQ:
667                         INC_QLIMIT(*hardlimit, count);
668                         break;
669                 case QUOTA_DQREL:
670                         LASSERTF(count < *hardlimit,
671                                  "id(%u) flag(%u) type(%c) isblk(%c) "
672                                  "count("LPU64") qd_qunit("LPU64") "
673                                  "hardlimit("LPU64").\n",
674                                  qdata->qd_id, qdata->qd_flags,
675                                  QDATA_IS_GRP(qdata) ? 'g' : 'u',
676                                  QDATA_IS_BLK(qdata) ? 'b': 'i',
677                                  qdata->qd_count, qdata->qd_qunit, *hardlimit);
678                         *hardlimit -= count;
679                         break;
680                 default:
681                         LBUG();
682                 }
683
684                 /* clear quota limit */
685                 if (count == 0)
686                         *hardlimit = 0;
687
688                 qctl->qc_cmd = Q_SETQUOTA;
689                 err = fsfilt_quotactl(obd, sb, qctl);
690                 if (err)
691                         CERROR("error set quota fs limit! (rc:%d)\n", err);
692
693                 QDATA_DEBUG(qdata, "%s completion\n",
694                             opc == QUOTA_DQACQ ? "DQACQ" : "DQREL");
695 out_mem:
696                 OBD_FREE_PTR(qctl);
697         } else if (rc == -EDQUOT) {
698                 QDATA_DEBUG(qdata, "acquire qunit got EDQUOT.\n");
699         } else if (rc == -EBUSY) {
700                 QDATA_DEBUG(qdata, "it's is recovering, got EBUSY.\n");
701         } else {
702                 CERROR("acquire qunit got error! (rc:%d)\n", rc);
703         }
704 out:
705         /* remove the qunit from hash */
706         cfs_spin_lock(&qunit_hash_lock);
707
708         qunit = dqacq_in_flight(qctxt, qdata);
709         /* this qunit has been removed by qctxt_cleanup() */
710         if (!qunit) {
711                 cfs_spin_unlock(&qunit_hash_lock);
712                 QDATA_DEBUG(qdata, "%s is discarded because qunit isn't found\n",
713                             opc == QUOTA_DQACQ ? "DQACQ" : "DQREL");
714                 RETURN(err);
715         }
716
717         LASSERT(opc == qunit->lq_opc);
718         /* remove this qunit from lq_hash so that new processes cannot be added
719          * to qunit->lq_waiters */
720         remove_qunit_nolock(qunit);
721         cfs_spin_unlock(&qunit_hash_lock);
722
723         compute_lqs_after_removing_qunit(qunit);
724
725         if (rc == 0)
726                 rc = QUOTA_REQ_RETURNED;
727         QUNIT_SET_STATE_AND_RC(qunit, QUNIT_FINISHED, rc);
728         /* wake up all waiters */
729         cfs_waitq_broadcast(&qunit->lq_waitq);
730
731         /* this is for dqacq_in_flight() */
732         qunit_put(qunit);
733         if (rc < 0 && rc != -EDQUOT)
734                 GOTO(out1, err);
735
736         /* don't reschedule in such cases:
737          *   - acq/rel failure and qunit isn't changed,
738          *     but not for quota recovery.
739          *   - local dqacq/dqrel.
740          *   - local disk io failure.
741          */
742          OBD_ALLOC_PTR(oqaq);
743          if (!oqaq)
744                  GOTO(out1, err = -ENOMEM);
745          qdata_to_oqaq(qdata, oqaq);
746          /* adjust the qunit size in slaves */
747          rc1 = quota_adjust_slave_lqs(oqaq, qctxt);
748          OBD_FREE_PTR(oqaq);
749          if (rc1 < 0) {
750                  CERROR("adjust slave's qunit size failed!(rc:%d)\n", rc1);
751                  GOTO(out1, err = rc1);
752          }
753          if (err || (rc < 0 && rc != -EBUSY && rc1 == 0) || is_master(qctxt))
754                  GOTO(out1, err);
755
756          if (opc == QUOTA_DQREL && qdata->qd_count >= 5242880 &&
757              OBD_FAIL_CHECK(OBD_FAIL_QUOTA_DELAY_REL))
758                  GOTO(out1, err);
759
760         /* reschedule another dqacq/dqrel if needed */
761         qdata->qd_count = 0;
762         qdata->qd_flags &= LQUOTA_QUNIT_FLAGS;
763         rc1 = check_cur_qunit(obd, qctxt, qdata);
764         if (rc1 > 0) {
765                 int opc;
766                 opc = rc1 == 1 ? QUOTA_DQACQ : QUOTA_DQREL;
767                 rc1 = schedule_dqacq(obd, qctxt, qdata, opc, 0, NULL);
768                 QDATA_DEBUG(qdata, "reschedudle opc(%d) rc(%d)\n", opc, rc1);
769         }
770  out1:
771         /* this is for alloc_qunit() */
772         qunit_put(qunit);
773         RETURN(err);
774 }
775
776 struct dqacq_async_args {
777         struct lustre_quota_ctxt *aa_ctxt;
778         struct lustre_qunit *aa_qunit;
779 };
780
781 static int dqacq_interpret(const struct lu_env *env,
782                            struct ptlrpc_request *req, void *data, int rc)
783 {
784         struct dqacq_async_args *aa = (struct dqacq_async_args *)data;
785         struct lustre_quota_ctxt *qctxt = aa->aa_ctxt;
786         struct obd_device_target *obt = qctxt->lqc_obt;
787         struct lustre_qunit *qunit = aa->aa_qunit;
788         struct obd_device *obd = req->rq_import->imp_obd;
789         struct qunit_data *qdata = NULL;
790         ENTRY;
791
792         LASSERT(req);
793         LASSERT(req->rq_import);
794
795         cfs_down_read(&obt->obt_rwsem);
796         /* if a quota req timeouts or is dropped, we should update quota
797          * statistics which will be handled in dqacq_completion. And in
798          * this situation we should get qdata from request instead of
799          * reply */
800         qdata = quota_get_qdata(req, (rc != 0) ? QUOTA_REQUEST : QUOTA_REPLY,
801                                 QUOTA_IMPORT);
802         if (IS_ERR(qdata)) {
803                 rc = PTR_ERR(qdata);
804                 DEBUG_REQ(D_ERROR, req,
805                           "error unpacking qunit_data(rc: %ld)\n",
806                           PTR_ERR(qdata));
807                 qdata = &qunit->lq_data;
808         }
809
810         QDATA_DEBUG(qdata, "qdata: interpret rc(%d).\n", rc);
811         QDATA_DEBUG((&qunit->lq_data), "lq_data: \n");
812
813         if (qdata->qd_id != qunit->lq_data.qd_id ||
814             OBD_FAIL_CHECK(OBD_FAIL_QUOTA_RET_QDATA)) {
815                 CERROR("the returned qd_id isn't expected!"
816                        "(qdata: %u, lq_data: %u)\n", qdata->qd_id,
817                        qunit->lq_data.qd_id);
818                 qdata->qd_id = qunit->lq_data.qd_id;
819                 rc = -EPROTO;
820         }
821         if (QDATA_IS_GRP(qdata) != QDATA_IS_GRP(&qunit->lq_data)) {
822                 CERROR("the returned grp/usr isn't expected!"
823                        "(qdata: %u, lq_data: %u)\n", qdata->qd_flags,
824                        qunit->lq_data.qd_flags);
825                 if (QDATA_IS_GRP(&qunit->lq_data))
826                         QDATA_SET_GRP(qdata);
827                 else
828                         QDATA_CLR_GRP(qdata);
829                 rc = -EPROTO;
830         }
831         if (qdata->qd_count > qunit->lq_data.qd_count) {
832                 CERROR("the returned qd_count isn't expected!"
833                        "(qdata: "LPU64", lq_data: "LPU64")\n", qdata->qd_count,
834                        qunit->lq_data.qd_count);
835                 rc = -EPROTO;
836         }
837
838         if (unlikely(rc == -ESRCH))
839                 CERROR("quota for %s has been enabled by master, but disabled "
840                        "by slave.\n", QDATA_IS_GRP(qdata) ? "group" : "user");
841
842         rc = dqacq_completion(obd, qctxt, qdata, rc,
843                               lustre_msg_get_opc(req->rq_reqmsg));
844
845         cfs_up_read(&obt->obt_rwsem);
846         RETURN(rc);
847 }
848
849 /**
850  * check if quota master is online
851  */
852 int check_qm(struct lustre_quota_ctxt *qctxt)
853 {
854         int rc;
855         ENTRY;
856
857         cfs_spin_lock(&qctxt->lqc_lock);
858         /* quit waiting when mds is back or qctxt is cleaned up */
859         rc = qctxt->lqc_import || !qctxt->lqc_valid;
860         cfs_spin_unlock(&qctxt->lqc_lock);
861
862         RETURN(rc);
863 }
864
865 /* wake up all waiting threads when lqc_import is NULL */
866 void dqacq_interrupt(struct lustre_quota_ctxt *qctxt)
867 {
868         struct lustre_qunit *qunit, *tmp;
869         int i;
870         ENTRY;
871
872         cfs_spin_lock(&qunit_hash_lock);
873         for (i = 0; i < NR_DQHASH; i++) {
874                 cfs_list_for_each_entry_safe(qunit, tmp, &qunit_hash[i],
875                                              lq_hash) {
876                         if (qunit->lq_ctxt != qctxt)
877                                 continue;
878
879                         /* Wake up all waiters. Do not change lq_state.
880                          * The waiters will check lq_rc which is kept as 0
881                          * if no others change it, then the waiters will return
882                          * -EAGAIN to caller who can perform related quota
883                          * acq/rel if necessary. */
884                         cfs_waitq_broadcast(&qunit->lq_waitq);
885                 }
886         }
887         cfs_spin_unlock(&qunit_hash_lock);
888         EXIT;
889 }
890
891 static int got_qunit(struct lustre_qunit *qunit, int is_master)
892 {
893         struct lustre_quota_ctxt *qctxt = qunit->lq_ctxt;
894         int rc = 0;
895         ENTRY;
896
897         cfs_spin_lock(&qunit->lq_lock);
898         switch (qunit->lq_state) {
899         case QUNIT_IN_HASH:
900         case QUNIT_RM_FROM_HASH:
901                 break;
902         case QUNIT_FINISHED:
903                 rc = 1;
904                 break;
905         default:
906                 CERROR("invalid qunit state %d\n", qunit->lq_state);
907         }
908         cfs_spin_unlock(&qunit->lq_lock);
909
910         if (!rc) {
911                 cfs_spin_lock(&qctxt->lqc_lock);
912                 rc = !qctxt->lqc_valid;
913                 if (!is_master)
914                         rc |= !qctxt->lqc_import;
915                 cfs_spin_unlock(&qctxt->lqc_lock);
916         }
917
918         RETURN(rc);
919 }
920
921 static inline void
922 revoke_lqs_rec(struct lustre_qunit_size *lqs, struct qunit_data *qdata, int opc)
923 {
924         /* revoke lqs_xxx_rec which is computed in check_cur_qunit
925          * b=18630 */
926         cfs_spin_lock(&lqs->lqs_lock);
927         quota_compute_lqs(qdata, lqs, 0, (opc == QUOTA_DQACQ) ? 1 : 0);
928         cfs_spin_unlock(&lqs->lqs_lock);
929 }
930
931 static int
932 schedule_dqacq(struct obd_device *obd, struct lustre_quota_ctxt *qctxt,
933                struct qunit_data *qdata, int opc, int wait,
934                struct obd_trans_info *oti)
935 {
936         struct lustre_qunit *qunit, *empty;
937         struct l_wait_info lwi = { 0 };
938         struct ptlrpc_request *req;
939         struct dqacq_async_args *aa;
940         struct obd_import *imp = NULL;
941         struct lustre_qunit_size *lqs = NULL;
942         struct timeval work_start;
943         struct timeval work_end;
944         long timediff;
945         int rc = 0;
946         ENTRY;
947
948         LASSERT(opc == QUOTA_DQACQ || opc == QUOTA_DQREL);
949         cfs_gettimeofday(&work_start);
950
951         lqs = quota_search_lqs(LQS_KEY(QDATA_IS_GRP(qdata), qdata->qd_id),
952                                qctxt, 0);
953         if (lqs == NULL || IS_ERR(lqs)) {
954                 CERROR("Can't find the lustre qunit size!\n");
955                 RETURN(-EPERM);
956         }
957
958         if ((empty = alloc_qunit(qctxt, qdata, opc)) == NULL) {
959                 revoke_lqs_rec(lqs, qdata, opc);
960                 /* this is for quota_search_lqs */
961                 lqs_putref(lqs);
962                 RETURN(-ENOMEM);
963         }
964
965         OBD_FAIL_TIMEOUT(OBD_FAIL_QUOTA_DELAY_SD, 5);
966
967         cfs_spin_lock(&qunit_hash_lock);
968         qunit = dqacq_in_flight(qctxt, qdata);
969         if (qunit) {
970                 cfs_spin_unlock(&qunit_hash_lock);
971                 qunit_put(empty);
972
973                 revoke_lqs_rec(lqs, qdata, opc);
974                 /* this is for quota_search_lqs */
975                 lqs_putref(lqs);
976                 goto wait_completion;
977         }
978         qunit = empty;
979         qunit_get(qunit);
980         insert_qunit_nolock(qctxt, qunit);
981         cfs_spin_unlock(&qunit_hash_lock);
982
983         /* From here, the quota request will be sent anyway.
984          * When this qdata request returned or is cancelled,
985          * lqs_putref will be called at that time */
986         lqs_getref(lqs);
987         /* this is for quota_search_lqs */
988         lqs_putref(lqs);
989
990         QDATA_DEBUG(qdata, "obd(%s): send %s quota req\n",
991                     obd->obd_name, (opc == QUOTA_DQACQ) ? "acq" : "rel");
992         /* master is going to dqacq/dqrel from itself */
993         if (is_master(qctxt)) {
994                 int rc2;
995                 QDATA_DEBUG(qdata, "local %s.\n",
996                             opc == QUOTA_DQACQ ? "DQACQ" : "DQREL");
997                 QDATA_SET_CHANGE_QS(qdata);
998                 rc = qctxt->lqc_handler(obd, qdata, opc);
999                 rc2 = dqacq_completion(obd, qctxt, qdata, rc, opc);
1000                 /* this is for qunit_get() */
1001                 qunit_put(qunit);
1002
1003                 cfs_gettimeofday(&work_end);
1004                 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
1005                 if (opc == QUOTA_DQACQ)
1006                         lprocfs_counter_add(qctxt->lqc_stats,
1007                                             wait ? LQUOTA_SYNC_ACQ : LQUOTA_ASYNC_ACQ,
1008                                             timediff);
1009                 else
1010                         lprocfs_counter_add(qctxt->lqc_stats,
1011                                             wait ? LQUOTA_SYNC_REL : LQUOTA_ASYNC_REL,
1012                                             timediff);
1013                 RETURN(rc ? rc : rc2);
1014         }
1015
1016         cfs_spin_lock(&qctxt->lqc_lock);
1017         if (!qctxt->lqc_import) {
1018                 cfs_spin_unlock(&qctxt->lqc_lock);
1019                 QDATA_DEBUG(qdata, "lqc_import is invalid.\n");
1020
1021                 cfs_spin_lock(&qunit_hash_lock);
1022                 remove_qunit_nolock(qunit);
1023                 cfs_spin_unlock(&qunit_hash_lock);
1024
1025                 compute_lqs_after_removing_qunit(qunit);
1026
1027                 QUNIT_SET_STATE_AND_RC(qunit, QUNIT_FINISHED, -EAGAIN);
1028                 cfs_waitq_broadcast(&qunit->lq_waitq);
1029
1030                 /* this is for qunit_get() */
1031                 qunit_put(qunit);
1032                 /* this for alloc_qunit() */
1033                 qunit_put(qunit);
1034                 cfs_spin_lock(&qctxt->lqc_lock);
1035                 if (wait && !qctxt->lqc_import) {
1036                         cfs_spin_unlock(&qctxt->lqc_lock);
1037                         LASSERT(oti && oti->oti_thread);
1038                         /* The recovery thread doesn't have watchdog
1039                          * attached. LU-369 */
1040                         if (oti->oti_thread->t_watchdog)
1041                                 lc_watchdog_disable(oti->oti_thread->\
1042                                                 t_watchdog);
1043                         CDEBUG(D_QUOTA, "sleep for quota master\n");
1044                         l_wait_event(qctxt->lqc_wait_for_qmaster,
1045                                      check_qm(qctxt), &lwi);
1046                         CDEBUG(D_QUOTA, "wake up when quota master is back\n");
1047                         if (oti->oti_thread->t_watchdog)
1048                                 lc_watchdog_touch(oti->oti_thread->t_watchdog,
1049                                       CFS_GET_TIMEOUT(oti->oti_thread->t_svc));
1050                 } else {
1051                         cfs_spin_unlock(&qctxt->lqc_lock);
1052                 }
1053
1054                 RETURN(-EAGAIN);
1055         }
1056         imp = class_import_get(qctxt->lqc_import);
1057         cfs_spin_unlock(&qctxt->lqc_lock);
1058
1059         /* build dqacq/dqrel request */
1060         LASSERT(imp);
1061
1062         req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_QUOTA_DQACQ,
1063                                         LUSTRE_MDS_VERSION, opc);
1064         class_import_put(imp);
1065         if (req == NULL) {
1066                 CERROR("Can't alloc request\n");
1067                 dqacq_completion(obd, qctxt, qdata, -ENOMEM, opc);
1068                 /* this is for qunit_get() */
1069                 qunit_put(qunit);
1070                 RETURN(-ENOMEM);
1071         }
1072
1073         ptlrpc_request_set_replen(req);
1074         req->rq_no_resend = req->rq_no_delay = 1;
1075         rc = quota_copy_qdata(req, qdata, QUOTA_REQUEST, QUOTA_IMPORT);
1076         if (rc < 0) {
1077                 CERROR("Can't pack qunit_data(rc: %d)\n", rc);
1078                 ptlrpc_req_finished(req);
1079                 dqacq_completion(obd, qctxt, qdata, -EPROTO, opc);
1080                 /* this is for qunit_get() */
1081                 qunit_put(qunit);
1082                 RETURN(rc);
1083         }
1084
1085         CLASSERT(sizeof(*aa) <= sizeof(req->rq_async_args));
1086         aa = ptlrpc_req_async_args(req);
1087         aa->aa_ctxt = qctxt;
1088         aa->aa_qunit = qunit;
1089
1090         req->rq_interpret_reply = dqacq_interpret;
1091         ptlrpcd_add_req(req, PDL_POLICY_LOCAL, -1);
1092
1093         QDATA_DEBUG(qdata, "%s scheduled.\n",
1094                     opc == QUOTA_DQACQ ? "DQACQ" : "DQREL");
1095 wait_completion:
1096         if (wait && qunit) {
1097                 struct qunit_data *p = &qunit->lq_data;
1098
1099                 QDATA_DEBUG(p, "qunit(%p) is waiting for dqacq.\n", qunit);
1100                 l_wait_event(qunit->lq_waitq, got_qunit(qunit, is_master(qctxt)),
1101                              &lwi);
1102                 /* rc = -EAGAIN, it means the quota master isn't ready yet
1103                  * rc = QUOTA_REQ_RETURNED, it means a quota req is finished;
1104                  * rc = -EDQUOT, it means out of quota
1105                  * rc = -EBUSY, it means recovery is happening
1106                  * other rc < 0, it means real errors, functions who call
1107                  * schedule_dqacq should take care of this */
1108                 cfs_spin_lock(&qunit->lq_lock);
1109                 rc = qunit->lq_rc;
1110                 cfs_spin_unlock(&qunit->lq_lock);
1111                 CDEBUG(D_QUOTA, "qunit(%p) finishes waiting: id(%u) flag(%u) "
1112                        "rc(%d) owner(%d)\n", qunit, qunit->lq_data.qd_id,
1113                        qunit->lq_data.qd_flags, rc, qunit->lq_owner);
1114         }
1115
1116         qunit_put(qunit);
1117         cfs_gettimeofday(&work_end);
1118         timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
1119         if (opc == QUOTA_DQACQ)
1120                 lprocfs_counter_add(qctxt->lqc_stats,
1121                                     wait ? LQUOTA_SYNC_ACQ : LQUOTA_ASYNC_ACQ,
1122                                     timediff);
1123         else
1124                 lprocfs_counter_add(qctxt->lqc_stats,
1125                                     wait ? LQUOTA_SYNC_REL : LQUOTA_ASYNC_REL,
1126                                     timediff);
1127
1128         RETURN(rc);
1129 }
1130
1131 int
1132 qctxt_adjust_qunit(struct obd_device *obd, struct lustre_quota_ctxt *qctxt,
1133                    const unsigned int id[], __u32 isblk, int wait,
1134                    struct obd_trans_info *oti)
1135 {
1136         int rc = 0, i = USRQUOTA;
1137         struct qunit_data qdata[MAXQUOTAS];
1138         ENTRY;
1139
1140         if (quota_is_set(obd, id, isblk ? QB_SET : QI_SET) == 0)
1141                 RETURN(0);
1142
1143         for (i = 0; i < MAXQUOTAS; i++) {
1144                 qdata[i].qd_id = id[i];
1145                 qdata[i].qd_flags = i;
1146                 if (isblk)
1147                         QDATA_SET_BLK(&qdata[i]);
1148                 qdata[i].qd_count = 0;
1149
1150                 rc = check_cur_qunit(obd, qctxt, &qdata[i]);
1151                 if (rc > 0) {
1152                         int opc;
1153                         /* need acquire or release */
1154                         opc = rc == 1 ? QUOTA_DQACQ : QUOTA_DQREL;
1155                         rc = schedule_dqacq(obd, qctxt, &qdata[i], opc,
1156                                             wait,oti);
1157                         if (rc < 0)
1158                                 RETURN(rc);
1159                 } else if (wait == 1) {
1160                         /* when wait equates 1, that means mds_quota_acquire
1161                          * or filter_quota_acquire is calling it. */
1162                         rc = qctxt_wait_pending_dqacq(qctxt, id[i], i, isblk);
1163                         if (rc < 0)
1164                                 RETURN(rc);
1165                 }
1166         }
1167
1168         RETURN(rc);
1169 }
1170
1171 int
1172 qctxt_wait_pending_dqacq(struct lustre_quota_ctxt *qctxt, unsigned int id,
1173                          unsigned short type, int isblk)
1174 {
1175         struct lustre_qunit *qunit = NULL;
1176         struct qunit_data qdata;
1177         struct timeval work_start;
1178         struct timeval work_end;
1179         long timediff;
1180         struct l_wait_info lwi = { 0 };
1181         int rc = 0;
1182         ENTRY;
1183
1184         cfs_gettimeofday(&work_start);
1185         qdata.qd_id = id;
1186         qdata.qd_flags = type;
1187         if (isblk)
1188                 QDATA_SET_BLK(&qdata);
1189         qdata.qd_count = 0;
1190
1191         cfs_spin_lock(&qunit_hash_lock);
1192         qunit = dqacq_in_flight(qctxt, &qdata);
1193         cfs_spin_unlock(&qunit_hash_lock);
1194
1195         if (qunit) {
1196                 struct qunit_data *p = &qunit->lq_data;
1197
1198                 QDATA_DEBUG(p, "qunit(%p) is waiting for dqacq.\n", qunit);
1199                 l_wait_event(qunit->lq_waitq, got_qunit(qunit, is_master(qctxt)),
1200                              &lwi);
1201                 CDEBUG(D_QUOTA, "qunit(%p) finishes waiting: rc(%d) "
1202                        "owner(%d)\n", qunit, qunit->lq_rc, qunit->lq_owner);
1203                 /* keep same as schedule_dqacq() b=17030 */
1204                 cfs_spin_lock(&qunit->lq_lock);
1205                 rc = qunit->lq_rc;
1206                 cfs_spin_unlock(&qunit->lq_lock);
1207                 /* this is for dqacq_in_flight() */
1208                 qunit_put(qunit);
1209                 cfs_gettimeofday(&work_end);
1210                 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
1211                 lprocfs_counter_add(qctxt->lqc_stats,
1212                                     isblk ? LQUOTA_WAIT_PENDING_BLK_QUOTA :
1213                                             LQUOTA_WAIT_PENDING_INO_QUOTA,
1214                                     timediff);
1215         } else {
1216                 cfs_gettimeofday(&work_end);
1217                 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
1218                 lprocfs_counter_add(qctxt->lqc_stats,
1219                                     isblk ? LQUOTA_NOWAIT_PENDING_BLK_QUOTA :
1220                                             LQUOTA_NOWAIT_PENDING_INO_QUOTA,
1221                                     timediff);
1222         }
1223
1224         RETURN(rc);
1225 }
1226
1227 int
1228 qctxt_init(struct obd_device *obd, dqacq_handler_t handler)
1229 {
1230         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
1231         struct obd_device_target *obt = &obd->u.obt;
1232         struct super_block *sb = obt->obt_sb;
1233         int rc = 0;
1234         ENTRY;
1235
1236         LASSERT(qctxt);
1237
1238         rc = ptlrpcd_addref();
1239         if (rc)
1240                 RETURN(rc);
1241
1242         cfs_waitq_init(&qctxt->lqc_wait_for_qmaster);
1243         cfs_waitq_init(&qctxt->lqc_lqs_waitq);
1244         cfs_atomic_set(&qctxt->lqc_lqs, 0);
1245         cfs_spin_lock_init(&qctxt->lqc_lock);
1246         cfs_spin_lock(&qctxt->lqc_lock);
1247         qctxt->lqc_handler = handler;
1248         qctxt->lqc_sb = sb;
1249         qctxt->lqc_obt = obt;
1250         qctxt->lqc_import = NULL;
1251         qctxt->lqc_recovery = 0;
1252         qctxt->lqc_switch_qs = 1; /* Change qunit size in default setting */
1253         qctxt->lqc_valid = 1;
1254         qctxt->lqc_cqs_boundary_factor = 4;
1255         qctxt->lqc_cqs_least_bunit = PTLRPC_MAX_BRW_SIZE;
1256         qctxt->lqc_cqs_least_iunit = 2;
1257         qctxt->lqc_cqs_qs_factor = 2;
1258         qctxt->lqc_flags = 0;
1259         QUOTA_MASTER_UNREADY(qctxt);
1260         qctxt->lqc_bunit_sz = default_bunit_sz;
1261         qctxt->lqc_btune_sz = default_bunit_sz / 100 * default_btune_ratio;
1262         qctxt->lqc_iunit_sz = default_iunit_sz;
1263         qctxt->lqc_itune_sz = default_iunit_sz * default_itune_ratio / 100;
1264         qctxt->lqc_switch_seconds = 300; /* enlarging will wait 5 minutes
1265                                           * after the last shrinking */
1266         qctxt->lqc_sync_blk = 0;
1267         cfs_spin_unlock(&qctxt->lqc_lock);
1268
1269         qctxt->lqc_lqs_hash = cfs_hash_create("LQS_HASH",
1270                                               hash_lqs_cur_bits,
1271                                               HASH_LQS_MAX_BITS,
1272                                               min(hash_lqs_cur_bits,
1273                                                   HASH_LQS_BKT_BITS),
1274                                               0, CFS_HASH_MIN_THETA,
1275                                               CFS_HASH_MAX_THETA,
1276                                               &lqs_hash_ops, CFS_HASH_DEFAULT);
1277         if (!qctxt->lqc_lqs_hash) {
1278                 CERROR("initialize hash lqs for %s error!\n", obd->obd_name);
1279                 RETURN(-ENOMEM);
1280         }
1281
1282 #ifdef LPROCFS
1283         rc = lquota_proc_setup(obd, is_master(qctxt));
1284         if (rc)
1285                 CERROR("initialize proc for %s error!\n", obd->obd_name);
1286 #endif
1287
1288         RETURN(rc);
1289 }
1290
1291 static int check_lqs(struct lustre_quota_ctxt *qctxt)
1292 {
1293         int rc;
1294         ENTRY;
1295
1296         rc = !cfs_atomic_read(&qctxt->lqc_lqs);
1297
1298         RETURN(rc);
1299 }
1300
1301 int qctxt_del_lqs(cfs_hash_t *hs, cfs_hash_bd_t *bd,
1302                  cfs_hlist_node_t *hnode, void *data)
1303 {
1304         /* remove from hash and -1 refcount */
1305         cfs_hash_bd_del_locked(hs, bd, hnode);
1306         return 0;
1307 }
1308
1309 void qctxt_cleanup(struct lustre_quota_ctxt *qctxt, int force)
1310 {
1311         struct lustre_qunit *qunit, *tmp;
1312         cfs_list_t tmp_list;
1313         struct l_wait_info lwi = { 0 };
1314         struct obd_device_target *obt = qctxt->lqc_obt;
1315         int i;
1316         ENTRY;
1317
1318         CFS_INIT_LIST_HEAD(&tmp_list);
1319
1320         cfs_spin_lock(&qctxt->lqc_lock);
1321         qctxt->lqc_valid = 0;
1322         cfs_spin_unlock(&qctxt->lqc_lock);
1323
1324         cfs_spin_lock(&qunit_hash_lock);
1325         for (i = 0; i < NR_DQHASH; i++) {
1326                 cfs_list_for_each_entry_safe(qunit, tmp, &qunit_hash[i],
1327                                              lq_hash) {
1328                         if (qunit->lq_ctxt != qctxt)
1329                                 continue;
1330                         remove_qunit_nolock(qunit);
1331                         cfs_list_add(&qunit->lq_hash, &tmp_list);
1332                 }
1333         }
1334         cfs_spin_unlock(&qunit_hash_lock);
1335
1336         cfs_list_for_each_entry_safe(qunit, tmp, &tmp_list, lq_hash) {
1337                 cfs_list_del_init(&qunit->lq_hash);
1338                 compute_lqs_after_removing_qunit(qunit);
1339
1340                 /* wake up all waiters */
1341                 QUNIT_SET_STATE_AND_RC(qunit, QUNIT_FINISHED, 0);
1342                 cfs_waitq_broadcast(&qunit->lq_waitq);
1343                 qunit_put(qunit);
1344         }
1345
1346         /* after qctxt_cleanup, qctxt might be freed, then check_qm() is
1347          * unpredicted. So we must wait until lqc_wait_for_qmaster is empty */
1348         while (cfs_waitq_active(&qctxt->lqc_wait_for_qmaster)) {
1349                 cfs_waitq_signal(&qctxt->lqc_wait_for_qmaster);
1350                 cfs_schedule_timeout_and_set_state(CFS_TASK_INTERRUPTIBLE,
1351                                                    cfs_time_seconds(1));
1352         }
1353
1354         /* release refcount on lustre_qunit_size holding by lqs_hash */
1355         cfs_hash_for_each_safe(qctxt->lqc_lqs_hash, qctxt_del_lqs, NULL);
1356
1357         l_wait_event(qctxt->lqc_lqs_waitq, check_lqs(qctxt), &lwi);
1358         cfs_down_write(&obt->obt_rwsem);
1359         cfs_hash_putref(qctxt->lqc_lqs_hash);
1360         qctxt->lqc_lqs_hash = NULL;
1361         cfs_up_write(&obt->obt_rwsem);
1362
1363         ptlrpcd_decref();
1364
1365 #ifdef LPROCFS
1366         if (lquota_proc_cleanup(qctxt))
1367                 CERROR("cleanup proc error!\n");
1368 #endif
1369
1370         EXIT;
1371 }
1372
1373 struct qslave_recov_thread_data {
1374         struct obd_device *obd;
1375         struct lustre_quota_ctxt *qctxt;
1376         cfs_completion_t comp;
1377 };
1378
1379 /* FIXME only recovery block quota by now */
1380 static int qslave_recovery_main(void *arg)
1381 {
1382         struct qslave_recov_thread_data *data = arg;
1383         struct obd_device *obd = data->obd;
1384         struct lustre_quota_ctxt *qctxt = data->qctxt;
1385         unsigned int type;
1386         int rc = 0;
1387         ENTRY;
1388
1389         cfs_daemonize_ctxt("qslave_recovd");
1390
1391         /* for obdfilter */
1392         class_incref(obd, "qslave_recovd_filter", obd);
1393
1394         cfs_complete(&data->comp);
1395
1396         cfs_spin_lock(&qctxt->lqc_lock);
1397         if (qctxt->lqc_recovery) {
1398                 cfs_spin_unlock(&qctxt->lqc_lock);
1399                 class_decref(obd, "qslave_recovd_filter", obd);
1400                 RETURN(0);
1401         } else {
1402                 qctxt->lqc_recovery = 1;
1403                 cfs_spin_unlock(&qctxt->lqc_lock);
1404         }
1405
1406         for (type = USRQUOTA; type < MAXQUOTAS; type++) {
1407                 struct qunit_data qdata;
1408                 struct quota_info *dqopt = sb_dqopt(qctxt->lqc_sb);
1409                 cfs_list_t id_list;
1410                 struct dquot_id *dqid, *tmp;
1411                 int ret;
1412
1413                 LOCK_DQONOFF_MUTEX(dqopt);
1414                 if (!ll_sb_has_quota_active(qctxt->lqc_sb, type)) {
1415                         UNLOCK_DQONOFF_MUTEX(dqopt);
1416                         break;
1417                 }
1418
1419                 LASSERT(dqopt->files[type] != NULL);
1420                 CFS_INIT_LIST_HEAD(&id_list);
1421 #ifndef KERNEL_SUPPORTS_QUOTA_READ
1422                 rc = fsfilt_qids(obd, dqopt->files[type], NULL, type, &id_list);
1423 #else
1424                 rc = fsfilt_qids(obd, NULL, dqopt->files[type], type, &id_list);
1425 #endif
1426                 UNLOCK_DQONOFF_MUTEX(dqopt);
1427                 if (rc)
1428                         CERROR("Get ids from quota file failed. (rc:%d)\n", rc);
1429
1430                 cfs_list_for_each_entry_safe(dqid, tmp, &id_list, di_link) {
1431                         cfs_list_del_init(&dqid->di_link);
1432                         /* skip slave recovery on itself */
1433                         if (is_master(qctxt))
1434                                 goto free;
1435                         if (rc && rc != -EBUSY)
1436                                 goto free;
1437
1438                         qdata.qd_id = dqid->di_id;
1439                         qdata.qd_flags = type;
1440                         QDATA_SET_BLK(&qdata);
1441                         qdata.qd_count = 0;
1442
1443                         ret = check_cur_qunit(obd, qctxt, &qdata);
1444                         if (ret > 0) {
1445                                 int opc;
1446                                 opc = ret == 1 ? QUOTA_DQACQ : QUOTA_DQREL;
1447                                 rc = schedule_dqacq(obd, qctxt, &qdata, opc,
1448                                                     0, NULL);
1449                                 if (rc == -EDQUOT)
1450                                         rc = 0;
1451                         } else {
1452                                 rc = 0;
1453                         }
1454
1455                         if (rc && rc != -EBUSY)
1456                                 CERROR("qslave recovery failed! (id:%d type:%d "
1457                                        " rc:%d)\n", dqid->di_id, type, rc);
1458 free:
1459                         OBD_FREE_PTR(dqid);
1460                 }
1461         }
1462
1463         cfs_spin_lock(&qctxt->lqc_lock);
1464         qctxt->lqc_recovery = 0;
1465         cfs_spin_unlock(&qctxt->lqc_lock);
1466         class_decref(obd, "qslave_recovd_filter", obd);
1467         RETURN(rc);
1468 }
1469
1470 void
1471 qslave_start_recovery(struct obd_device *obd, struct lustre_quota_ctxt *qctxt)
1472 {
1473         struct qslave_recov_thread_data data;
1474         int rc;
1475         ENTRY;
1476
1477         if (!ll_sb_any_quota_active(qctxt->lqc_sb))
1478                 goto exit;
1479
1480         data.obd = obd;
1481         data.qctxt = qctxt;
1482         cfs_init_completion(&data.comp);
1483
1484         rc = cfs_create_thread(qslave_recovery_main, &data,
1485                                CFS_DAEMON_FLAGS);
1486         if (rc < 0) {
1487                 CERROR("Cannot start quota recovery thread: rc %d\n", rc);
1488                 goto exit;
1489         }
1490         cfs_wait_for_completion(&data.comp);
1491 exit:
1492         EXIT;
1493 }
1494
1495 inline int quota_is_on(struct lustre_quota_ctxt *qctxt,
1496                        struct obd_quotactl *oqctl)
1497 {
1498         return ((qctxt->lqc_flags & UGQUOTA2LQC(oqctl->qc_type)) ==
1499                 UGQUOTA2LQC(oqctl->qc_type));
1500 }
1501
1502 inline int quota_is_off(struct lustre_quota_ctxt *qctxt,
1503                         struct obd_quotactl *oqctl)
1504 {
1505         return !(qctxt->lqc_flags & UGQUOTA2LQC(oqctl->qc_type));
1506 }
1507
1508 /**
1509  * When quotaon, build a lqs for every uid/gid who has been set limitation
1510  * for quota. After quota_search_lqs, it will hold one ref for the lqs.
1511  * It will be released when qctxt_cleanup() is executed b=18574
1512  *
1513  * Should be called with obt->obt_quotachecking held. b=20152 
1514  */
1515 void build_lqs(struct obd_device *obd)
1516 {
1517         struct obd_device_target *obt = &obd->u.obt;
1518         struct lustre_quota_ctxt *qctxt = &obt->obt_qctxt;
1519         cfs_list_t id_list;
1520         int i, rc;
1521
1522         LASSERT_SEM_LOCKED(&obt->obt_quotachecking);
1523         CFS_INIT_LIST_HEAD(&id_list);
1524         for (i = 0; i < MAXQUOTAS; i++) {
1525                 struct dquot_id *dqid, *tmp;
1526
1527                 if (sb_dqopt(qctxt->lqc_sb)->files[i] == NULL)
1528                         continue;
1529
1530 #ifndef KERNEL_SUPPORTS_QUOTA_READ
1531                 rc = fsfilt_qids(obd, sb_dqopt(qctxt->lqc_sb)->files[i], NULL,
1532                                  i, &id_list);
1533 #else
1534                 rc = fsfilt_qids(obd, NULL, sb_dqopt(qctxt->lqc_sb)->files[i],
1535                                  i, &id_list);
1536 #endif
1537                 if (rc) {
1538                         CERROR("%s: failed to get %s qids!\n", obd->obd_name,
1539                                i ? "group" : "user");
1540                         continue;
1541                 }
1542
1543                 cfs_list_for_each_entry_safe(dqid, tmp, &id_list,
1544                                              di_link) {
1545                         struct lustre_qunit_size *lqs;
1546
1547                         cfs_list_del_init(&dqid->di_link);
1548                         lqs = quota_search_lqs(LQS_KEY(i, dqid->di_id),
1549                                                qctxt, 1);
1550                         if (lqs && !IS_ERR(lqs)) {
1551                                 lqs->lqs_flags |= dqid->di_flag;
1552                                 lqs_putref(lqs);
1553                         } else {
1554                                 CERROR("%s: failed to create a lqs for %sid %u"
1555                                        "\n", obd->obd_name, i ? "g" : "u",
1556                                        dqid->di_id);
1557                         }
1558
1559                         OBD_FREE_PTR(dqid);
1560                 }
1561         }
1562 }
1563
1564 /**
1565  * lqs<->qctxt hash operations
1566  */
1567
1568 /**
1569  * string hashing using djb2 hash algorithm
1570  */
1571 static unsigned
1572 lqs_hash(cfs_hash_t *hs, const void *key, unsigned mask)
1573 {
1574         unsigned long long id;
1575         unsigned hash;
1576         ENTRY;
1577
1578         LASSERT(key);
1579         id = *((unsigned long long *)key);
1580         hash = (LQS_KEY_GRP(id) ? 5381 : 5387) * (unsigned)LQS_KEY_ID(id);
1581
1582         RETURN(hash & mask);
1583 }
1584
1585 static void *
1586 lqs_key(cfs_hlist_node_t *hnode)
1587 {
1588         struct lustre_qunit_size *lqs;
1589         ENTRY;
1590
1591         lqs = cfs_hlist_entry(hnode, struct lustre_qunit_size, lqs_hash);
1592         RETURN(&lqs->lqs_key);
1593 }
1594
1595 static int
1596 lqs_keycmp(const void *key, cfs_hlist_node_t *hnode)
1597 {
1598         struct lustre_qunit_size *q =
1599                 cfs_hlist_entry(hnode, struct lustre_qunit_size, lqs_hash);
1600
1601         RETURN(q->lqs_key == *((unsigned long long *)key));
1602 }
1603
1604 static void *
1605 lqs_object(cfs_hlist_node_t *hnode)
1606 {
1607         return cfs_hlist_entry(hnode, struct lustre_qunit_size, lqs_hash);
1608 }
1609
1610 static void
1611 lqs_get(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
1612 {
1613         struct lustre_qunit_size *q =
1614                 cfs_hlist_entry(hnode, struct lustre_qunit_size, lqs_hash);
1615
1616         lqs_getref(q);
1617 }
1618
1619 static void
1620 lqs_put_locked(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
1621 {
1622         struct lustre_qunit_size *q =
1623                 cfs_hlist_entry(hnode, struct lustre_qunit_size, lqs_hash);
1624
1625         lqs_putref(q);
1626 }
1627
1628 static void
1629 lqs_exit(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
1630 {
1631         CERROR("It should not have any item left to be handled by this!");
1632 }
1633
1634 static cfs_hash_ops_t lqs_hash_ops = {
1635         .hs_hash        = lqs_hash,
1636         .hs_key         = lqs_key,
1637         .hs_keycmp      = lqs_keycmp,
1638         .hs_object      = lqs_object,
1639         .hs_get         = lqs_get,
1640         .hs_put_locked  = lqs_put_locked,
1641         .hs_exit        = lqs_exit
1642 };