Whamcloud - gitweb
LU-1438 quota: quota active checking is missed on slave
[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_has_quota_active(sb, QDATA_IS_GRP(qdata)))
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         /* ignore root user */
378         if (qdata->qd_id == 0 && QDATA_IS_GRP(qdata) == USRQUOTA)
379                 RETURN(QUOTA_RET_NOLIMIT);
380
381         OBD_ALLOC_PTR(qctl);
382         if (qctl == NULL)
383                 RETURN(-ENOMEM);
384
385         /* get fs quota usage & limit */
386         qctl->qc_cmd = Q_GETQUOTA;
387         qctl->qc_id = qdata->qd_id;
388         qctl->qc_type = QDATA_IS_GRP(qdata);
389         ret = fsfilt_quotactl(obd, sb, qctl);
390         if (ret) {
391                 if (ret == -ESRCH)      /* no limit */
392                         ret = QUOTA_RET_NOLIMIT;
393                 else
394                         CDEBUG(D_QUOTA, "can't get fs quota usage! (rc:%d)",
395                                ret);
396                 GOTO(out, ret);
397         }
398
399         usage = isblk ? qctl->qc_dqblk.dqb_curspace :
400                 qctl->qc_dqblk.dqb_curinodes;
401         limit = isblk ? qctl->qc_dqblk.dqb_bhardlimit << QUOTABLOCK_BITS :
402                 qctl->qc_dqblk.dqb_ihardlimit;
403         if (!limit){            /* no limit */
404                 ret = QUOTA_RET_NOLIMIT;
405                 GOTO(out, ret);
406         }
407
408         if (limit >= usage)
409                 qdata->qd_count = limit - usage;
410         else
411                 qdata->qd_count = 0;
412         EXIT;
413 out:
414         OBD_FREE_PTR(qctl);
415         return ret;
416 }
417
418 static struct lustre_qunit *alloc_qunit(struct lustre_quota_ctxt *qctxt,
419                                         struct qunit_data *qdata, int opc)
420 {
421         struct lustre_qunit *qunit = NULL;
422         ENTRY;
423
424         OBD_SLAB_ALLOC_PTR_GFP(qunit, qunit_cachep, CFS_ALLOC_IO);
425         if (qunit == NULL)
426                 RETURN(NULL);
427
428         CFS_INIT_LIST_HEAD(&qunit->lq_hash);
429         cfs_waitq_init(&qunit->lq_waitq);
430         cfs_atomic_set(&qunit->lq_refcnt, 1);
431         qunit->lq_ctxt = qctxt;
432         memcpy(&qunit->lq_data, qdata, sizeof(*qdata));
433         qunit->lq_opc = opc;
434         qunit->lq_lock = CFS_SPIN_LOCK_UNLOCKED;
435         QUNIT_SET_STATE_AND_RC(qunit, QUNIT_CREATED, 0);
436         qunit->lq_owner = cfs_curproc_pid();
437         RETURN(qunit);
438 }
439
440 static inline void free_qunit(struct lustre_qunit *qunit)
441 {
442         OBD_SLAB_FREE(qunit, qunit_cachep, sizeof(*qunit));
443 }
444
445 static inline void qunit_get(struct lustre_qunit *qunit)
446 {
447         cfs_atomic_inc(&qunit->lq_refcnt);
448 }
449
450 static void qunit_put(struct lustre_qunit *qunit)
451 {
452         LASSERT(cfs_atomic_read(&qunit->lq_refcnt));
453         if (cfs_atomic_dec_and_test(&qunit->lq_refcnt))
454                 free_qunit(qunit);
455 }
456
457 /* caller must hold qunit_hash_lock and release ref of qunit after using it */
458 static struct lustre_qunit *dqacq_in_flight(struct lustre_quota_ctxt *qctxt,
459                                             struct qunit_data *qdata)
460 {
461         unsigned int hashent = qunit_hashfn(qctxt, qdata);
462         struct lustre_qunit *qunit;
463         ENTRY;
464
465         LASSERT_SPIN_LOCKED(&qunit_hash_lock);
466         qunit = find_qunit(hashent, qctxt, qdata);
467         if (qunit)
468                 qunit_get(qunit);
469         RETURN(qunit);
470 }
471
472 static void
473 insert_qunit_nolock(struct lustre_quota_ctxt *qctxt, struct lustre_qunit *qunit)
474 {
475         cfs_list_t *head;
476
477         LASSERT(cfs_list_empty(&qunit->lq_hash));
478         qunit_get(qunit);
479         head = qunit_hash + qunit_hashfn(qctxt, &qunit->lq_data);
480         cfs_list_add(&qunit->lq_hash, head);
481         QUNIT_SET_STATE(qunit, QUNIT_IN_HASH);
482 }
483
484 static void compute_lqs_after_removing_qunit(struct lustre_qunit *qunit)
485 {
486         struct lustre_qunit_size *lqs;
487
488         lqs = quota_search_lqs(LQS_KEY(QDATA_IS_GRP(&qunit->lq_data),
489                                        qunit->lq_data.qd_id),
490                                qunit->lq_ctxt, 0);
491         if (lqs && !IS_ERR(lqs)) {
492                 cfs_spin_lock(&lqs->lqs_lock);
493                 if (qunit->lq_opc == QUOTA_DQACQ)
494                         quota_compute_lqs(&qunit->lq_data, lqs, 0, 1);
495                 if (qunit->lq_opc == QUOTA_DQREL)
496                         quota_compute_lqs(&qunit->lq_data, lqs, 0, 0);
497                 cfs_spin_unlock(&lqs->lqs_lock);
498                 /* this is for quota_search_lqs */
499                 lqs_putref(lqs);
500                 /* this is for schedule_dqacq */
501                 lqs_putref(lqs);
502         }
503 }
504
505 static void remove_qunit_nolock(struct lustre_qunit *qunit)
506 {
507         LASSERT(!cfs_list_empty(&qunit->lq_hash));
508         LASSERT_SPIN_LOCKED(&qunit_hash_lock);
509
510         cfs_list_del_init(&qunit->lq_hash);
511         QUNIT_SET_STATE(qunit, QUNIT_RM_FROM_HASH);
512         qunit_put(qunit);
513 }
514
515 void* quota_barrier(struct lustre_quota_ctxt *qctxt,
516                     struct obd_quotactl *oqctl, int isblk)
517 {
518         struct lustre_qunit *qunit, *find_qunit;
519         int cycle = 1;
520
521         OBD_SLAB_ALLOC_PTR(qunit, qunit_cachep);
522         if (qunit == NULL) {
523                 CERROR("locating %sunit failed for %sid %u\n",
524                        isblk ? "b" : "i", oqctl->qc_type ? "g" : "u",
525                        oqctl->qc_id);
526                 qctxt_wait_pending_dqacq(qctxt, oqctl->qc_id,
527                                          oqctl->qc_type, isblk);
528                 return NULL;
529         }
530
531         CFS_INIT_LIST_HEAD(&qunit->lq_hash);
532         qunit->lq_lock = CFS_SPIN_LOCK_UNLOCKED;
533         cfs_waitq_init(&qunit->lq_waitq);
534         cfs_atomic_set(&qunit->lq_refcnt, 1);
535         qunit->lq_ctxt = qctxt;
536         qunit->lq_data.qd_id = oqctl->qc_id;
537         qunit->lq_data.qd_flags =  oqctl->qc_type;
538         if (isblk)
539                 QDATA_SET_BLK(&qunit->lq_data);
540         QUNIT_SET_STATE_AND_RC(qunit, QUNIT_CREATED, 0);
541         /* it means it is only an invalid qunit for barrier */
542         qunit->lq_opc = QUOTA_LAST_OPC;
543
544         while (1) {
545                 cfs_spin_lock(&qunit_hash_lock);
546                 find_qunit = dqacq_in_flight(qctxt, &qunit->lq_data);
547                 if (find_qunit) {
548                         cfs_spin_unlock(&qunit_hash_lock);
549                         qunit_put(find_qunit);
550                         qctxt_wait_pending_dqacq(qctxt, oqctl->qc_id,
551                                                  oqctl->qc_type, isblk);
552                         CDEBUG(D_QUOTA, "cycle=%d\n", cycle++);
553                         continue;
554                 }
555                 break;
556         }
557         insert_qunit_nolock(qctxt, qunit);
558         cfs_spin_unlock(&qunit_hash_lock);
559         return qunit;
560 }
561
562 void quota_unbarrier(void *handle)
563 {
564         struct lustre_qunit *qunit = (struct lustre_qunit *)handle;
565
566         if (qunit == NULL) {
567                 CERROR("handle is NULL\n");
568                 return;
569         }
570
571         LASSERT(qunit->lq_opc == QUOTA_LAST_OPC);
572         cfs_spin_lock(&qunit_hash_lock);
573         remove_qunit_nolock(qunit);
574         cfs_spin_unlock(&qunit_hash_lock);
575         QUNIT_SET_STATE_AND_RC(qunit, QUNIT_FINISHED, QUOTA_REQ_RETURNED);
576         cfs_waitq_signal(&qunit->lq_waitq);
577         qunit_put(qunit);
578 }
579
580 #define INC_QLIMIT(limit, count) (limit == MIN_QLIMIT) ? \
581                                  (limit = count) : (limit += count)
582
583
584 static inline int is_master(struct lustre_quota_ctxt *qctxt)
585 {
586         return qctxt->lqc_handler ? 1 : 0;
587 }
588
589 static int
590 schedule_dqacq(struct obd_device *obd, struct lustre_quota_ctxt *qctxt,
591                struct qunit_data *qdata, int opc, int wait,
592                struct obd_trans_info *oti);
593
594 static inline void qdata_to_oqaq(struct qunit_data *qdata,
595                                  struct quota_adjust_qunit *oqaq)
596 {
597         LASSERT(qdata);
598         LASSERT(oqaq);
599
600         oqaq->qaq_flags = qdata->qd_flags;
601         oqaq->qaq_id    = qdata->qd_id;
602         if (QDATA_IS_ADJBLK(qdata))
603                 oqaq->qaq_bunit_sz = qdata->qd_qunit;
604         if (QDATA_IS_ADJINO(qdata))
605                 oqaq->qaq_iunit_sz = qdata->qd_qunit;
606 }
607
608 static int
609 dqacq_completion(struct obd_device *obd, struct lustre_quota_ctxt *qctxt,
610                  struct qunit_data *qdata, int rc, int opc)
611 {
612         struct lustre_qunit *qunit = NULL;
613         struct super_block *sb = qctxt->lqc_sb;
614         int err = 0;
615         struct quota_adjust_qunit *oqaq = NULL;
616         int rc1 = 0;
617         ENTRY;
618
619         LASSERT(qdata);
620         QDATA_DEBUG(qdata, "obd(%s): complete %s quota req\n",
621                     obd->obd_name, (opc == QUOTA_DQACQ) ? "acq" : "rel");
622
623         /* do it only when a releasing quota req more than 5MB b=18491 */
624         if (opc == QUOTA_DQREL && qdata->qd_count >= 5242880)
625                 OBD_FAIL_TIMEOUT(OBD_FAIL_QUOTA_DELAY_REL, 5);
626
627         /* update local operational quota file */
628         if (rc == 0) {
629                 __u64 count = QUSG(qdata->qd_count, QDATA_IS_BLK(qdata));
630                 struct obd_quotactl *qctl;
631                 __u64 *hardlimit;
632
633                 OBD_ALLOC_PTR(qctl);
634                 if (qctl == NULL)
635                         GOTO(out, err = -ENOMEM);
636
637                 /* acq/rel qunit for specified uid/gid is serialized,
638                  * so there is no race between get fs quota limit and
639                  * set fs quota limit */
640                 qctl->qc_cmd = Q_GETQUOTA;
641                 qctl->qc_id = qdata->qd_id;
642                 qctl->qc_type = QDATA_IS_GRP(qdata);
643                 err = fsfilt_quotactl(obd, sb, qctl);
644                 if (err) {
645                         CERROR("error get quota fs limit! (rc:%d)\n", err);
646                         GOTO(out_mem, err);
647                 }
648
649                 if (QDATA_IS_BLK(qdata)) {
650                         qctl->qc_dqblk.dqb_valid = QIF_BLIMITS;
651                         hardlimit = &qctl->qc_dqblk.dqb_bhardlimit;
652                 } else {
653                         qctl->qc_dqblk.dqb_valid = QIF_ILIMITS;
654                         hardlimit = &qctl->qc_dqblk.dqb_ihardlimit;
655                 }
656
657                 CDEBUG(D_QUOTA, "hardlimt: "LPU64"\n", *hardlimit);
658
659                 if (*hardlimit == 0)
660                         goto out_mem;
661
662                 switch (opc) {
663                 case QUOTA_DQACQ:
664                         INC_QLIMIT(*hardlimit, count);
665                         break;
666                 case QUOTA_DQREL:
667                         LASSERTF(count < *hardlimit,
668                                  "id(%u) flag(%u) type(%c) isblk(%c) "
669                                  "count("LPU64") qd_qunit("LPU64") "
670                                  "hardlimit("LPU64").\n",
671                                  qdata->qd_id, qdata->qd_flags,
672                                  QDATA_IS_GRP(qdata) ? 'g' : 'u',
673                                  QDATA_IS_BLK(qdata) ? 'b': 'i',
674                                  qdata->qd_count, qdata->qd_qunit, *hardlimit);
675                         *hardlimit -= count;
676                         break;
677                 default:
678                         LBUG();
679                 }
680
681                 /* clear quota limit */
682                 if (count == 0)
683                         *hardlimit = 0;
684
685                 qctl->qc_cmd = Q_SETQUOTA;
686                 err = fsfilt_quotactl(obd, sb, qctl);
687                 if (err)
688                         CERROR("error set quota fs limit! (rc:%d)\n", err);
689
690                 QDATA_DEBUG(qdata, "%s completion\n",
691                             opc == QUOTA_DQACQ ? "DQACQ" : "DQREL");
692 out_mem:
693                 OBD_FREE_PTR(qctl);
694         } else if (rc == -EDQUOT) {
695                 QDATA_DEBUG(qdata, "acquire qunit got EDQUOT.\n");
696         } else if (rc == -EBUSY) {
697                 QDATA_DEBUG(qdata, "it's is recovering, got EBUSY.\n");
698         } else {
699                 CERROR("acquire qunit got error! (rc:%d)\n", rc);
700         }
701 out:
702         /* remove the qunit from hash */
703         cfs_spin_lock(&qunit_hash_lock);
704
705         qunit = dqacq_in_flight(qctxt, qdata);
706         /* this qunit has been removed by qctxt_cleanup() */
707         if (!qunit) {
708                 cfs_spin_unlock(&qunit_hash_lock);
709                 QDATA_DEBUG(qdata, "%s is discarded because qunit isn't found\n",
710                             opc == QUOTA_DQACQ ? "DQACQ" : "DQREL");
711                 RETURN(err);
712         }
713
714         LASSERT(opc == qunit->lq_opc);
715         /* remove this qunit from lq_hash so that new processes cannot be added
716          * to qunit->lq_waiters */
717         remove_qunit_nolock(qunit);
718         cfs_spin_unlock(&qunit_hash_lock);
719
720         compute_lqs_after_removing_qunit(qunit);
721
722         if (rc == 0)
723                 rc = QUOTA_REQ_RETURNED;
724         QUNIT_SET_STATE_AND_RC(qunit, QUNIT_FINISHED, rc);
725         /* wake up all waiters */
726         cfs_waitq_broadcast(&qunit->lq_waitq);
727
728         /* this is for dqacq_in_flight() */
729         qunit_put(qunit);
730         if (rc < 0 && rc != -EDQUOT)
731                 GOTO(out1, 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                  GOTO(out1, err = -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                  GOTO(out1, err = rc1);
749          }
750          if (err || (rc < 0 && rc != -EBUSY && rc1 == 0) || is_master(qctxt))
751                  GOTO(out1, err);
752
753          if (opc == QUOTA_DQREL && qdata->qd_count >= 5242880 &&
754              OBD_FAIL_CHECK(OBD_FAIL_QUOTA_DELAY_REL))
755                  GOTO(out1, 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  out1:
768         /* this is for alloc_qunit() */
769         qunit_put(qunit);
770         RETURN(err);
771 }
772
773 struct dqacq_async_args {
774         struct lustre_quota_ctxt *aa_ctxt;
775         struct lustre_qunit *aa_qunit;
776 };
777
778 static int dqacq_interpret(const struct lu_env *env,
779                            struct ptlrpc_request *req, void *data, int rc)
780 {
781         struct dqacq_async_args *aa = (struct dqacq_async_args *)data;
782         struct lustre_quota_ctxt *qctxt = aa->aa_ctxt;
783         struct obd_device_target *obt = qctxt->lqc_obt;
784         struct lustre_qunit *qunit = aa->aa_qunit;
785         struct obd_device *obd = req->rq_import->imp_obd;
786         struct qunit_data *qdata = NULL;
787         ENTRY;
788
789         LASSERT(req);
790         LASSERT(req->rq_import);
791
792         cfs_down_read(&obt->obt_rwsem);
793         /* if a quota req timeouts or is dropped, we should update quota
794          * statistics which will be handled in dqacq_completion. And in
795          * this situation we should get qdata from request instead of
796          * reply */
797         qdata = quota_get_qdata(req, (rc != 0) ? QUOTA_REQUEST : QUOTA_REPLY,
798                                 QUOTA_IMPORT);
799         if (IS_ERR(qdata)) {
800                 rc = PTR_ERR(qdata);
801                 DEBUG_REQ(D_ERROR, req,
802                           "error unpacking qunit_data(rc: %ld)\n",
803                           PTR_ERR(qdata));
804                 qdata = &qunit->lq_data;
805         }
806
807         QDATA_DEBUG(qdata, "qdata: interpret rc(%d).\n", rc);
808         QDATA_DEBUG((&qunit->lq_data), "lq_data: \n");
809
810         if (qdata->qd_id != qunit->lq_data.qd_id ||
811             OBD_FAIL_CHECK(OBD_FAIL_QUOTA_RET_QDATA)) {
812                 CERROR("the returned qd_id isn't expected!"
813                        "(qdata: %u, lq_data: %u)\n", qdata->qd_id,
814                        qunit->lq_data.qd_id);
815                 qdata->qd_id = qunit->lq_data.qd_id;
816                 rc = -EPROTO;
817         }
818         if (QDATA_IS_GRP(qdata) != QDATA_IS_GRP(&qunit->lq_data)) {
819                 CERROR("the returned grp/usr isn't expected!"
820                        "(qdata: %u, lq_data: %u)\n", qdata->qd_flags,
821                        qunit->lq_data.qd_flags);
822                 if (QDATA_IS_GRP(&qunit->lq_data))
823                         QDATA_SET_GRP(qdata);
824                 else
825                         QDATA_CLR_GRP(qdata);
826                 rc = -EPROTO;
827         }
828         if (qdata->qd_count > qunit->lq_data.qd_count) {
829                 CERROR("the returned qd_count isn't expected!"
830                        "(qdata: "LPU64", lq_data: "LPU64")\n", qdata->qd_count,
831                        qunit->lq_data.qd_count);
832                 rc = -EPROTO;
833         }
834
835         if (unlikely(rc == -ESRCH))
836                 CERROR("quota for %s has been enabled by master, but disabled "
837                        "by slave.\n", QDATA_IS_GRP(qdata) ? "group" : "user");
838
839         rc = dqacq_completion(obd, qctxt, qdata, rc,
840                               lustre_msg_get_opc(req->rq_reqmsg));
841
842         cfs_up_read(&obt->obt_rwsem);
843         RETURN(rc);
844 }
845
846 /**
847  * check if quota master is online
848  */
849 int check_qm(struct lustre_quota_ctxt *qctxt)
850 {
851         int rc;
852         ENTRY;
853
854         cfs_spin_lock(&qctxt->lqc_lock);
855         /* quit waiting when mds is back or qctxt is cleaned up */
856         rc = qctxt->lqc_import || !qctxt->lqc_valid;
857         cfs_spin_unlock(&qctxt->lqc_lock);
858
859         RETURN(rc);
860 }
861
862 /* wake up all waiting threads when lqc_import is NULL */
863 void dqacq_interrupt(struct lustre_quota_ctxt *qctxt)
864 {
865         struct lustre_qunit *qunit, *tmp;
866         int i;
867         ENTRY;
868
869         cfs_spin_lock(&qunit_hash_lock);
870         for (i = 0; i < NR_DQHASH; i++) {
871                 cfs_list_for_each_entry_safe(qunit, tmp, &qunit_hash[i],
872                                              lq_hash) {
873                         if (qunit->lq_ctxt != qctxt)
874                                 continue;
875
876                         /* Wake up all waiters. Do not change lq_state.
877                          * The waiters will check lq_rc which is kept as 0
878                          * if no others change it, then the waiters will return
879                          * -EAGAIN to caller who can perform related quota
880                          * acq/rel if necessary. */
881                         cfs_waitq_broadcast(&qunit->lq_waitq);
882                 }
883         }
884         cfs_spin_unlock(&qunit_hash_lock);
885         EXIT;
886 }
887
888 static int got_qunit(struct lustre_qunit *qunit, int is_master)
889 {
890         struct lustre_quota_ctxt *qctxt = qunit->lq_ctxt;
891         int rc = 0;
892         ENTRY;
893
894         cfs_spin_lock(&qunit->lq_lock);
895         switch (qunit->lq_state) {
896         case QUNIT_IN_HASH:
897         case QUNIT_RM_FROM_HASH:
898                 break;
899         case QUNIT_FINISHED:
900                 rc = 1;
901                 break;
902         default:
903                 CERROR("invalid qunit state %d\n", qunit->lq_state);
904         }
905         cfs_spin_unlock(&qunit->lq_lock);
906
907         if (!rc) {
908                 cfs_spin_lock(&qctxt->lqc_lock);
909                 rc = !qctxt->lqc_valid;
910                 if (!is_master)
911                         rc |= !qctxt->lqc_import;
912                 cfs_spin_unlock(&qctxt->lqc_lock);
913         }
914
915         RETURN(rc);
916 }
917
918 static inline void
919 revoke_lqs_rec(struct lustre_qunit_size *lqs, struct qunit_data *qdata, int opc)
920 {
921         /* revoke lqs_xxx_rec which is computed in check_cur_qunit
922          * b=18630 */
923         cfs_spin_lock(&lqs->lqs_lock);
924         quota_compute_lqs(qdata, lqs, 0, (opc == QUOTA_DQACQ) ? 1 : 0);
925         cfs_spin_unlock(&lqs->lqs_lock);
926 }
927
928 static int
929 schedule_dqacq(struct obd_device *obd, struct lustre_quota_ctxt *qctxt,
930                struct qunit_data *qdata, int opc, int wait,
931                struct obd_trans_info *oti)
932 {
933         struct lustre_qunit *qunit, *empty;
934         struct l_wait_info lwi = { 0 };
935         struct ptlrpc_request *req;
936         struct dqacq_async_args *aa;
937         struct obd_import *imp = NULL;
938         struct lustre_qunit_size *lqs = NULL;
939         struct timeval work_start;
940         struct timeval work_end;
941         long timediff;
942         int rc = 0;
943         ENTRY;
944
945         LASSERT(opc == QUOTA_DQACQ || opc == QUOTA_DQREL);
946         cfs_gettimeofday(&work_start);
947
948         lqs = quota_search_lqs(LQS_KEY(QDATA_IS_GRP(qdata), qdata->qd_id),
949                                qctxt, 0);
950         if (lqs == NULL || IS_ERR(lqs)) {
951                 CERROR("Can't find the lustre qunit size!\n");
952                 RETURN(-EPERM);
953         }
954
955         if ((empty = alloc_qunit(qctxt, qdata, opc)) == NULL) {
956                 revoke_lqs_rec(lqs, qdata, opc);
957                 /* this is for quota_search_lqs */
958                 lqs_putref(lqs);
959                 RETURN(-ENOMEM);
960         }
961
962         OBD_FAIL_TIMEOUT(OBD_FAIL_QUOTA_DELAY_SD, 5);
963
964         cfs_spin_lock(&qunit_hash_lock);
965         qunit = dqacq_in_flight(qctxt, qdata);
966         if (qunit) {
967                 cfs_spin_unlock(&qunit_hash_lock);
968                 qunit_put(empty);
969
970                 revoke_lqs_rec(lqs, qdata, opc);
971                 /* this is for quota_search_lqs */
972                 lqs_putref(lqs);
973                 goto wait_completion;
974         }
975         qunit = empty;
976         qunit_get(qunit);
977         insert_qunit_nolock(qctxt, qunit);
978         cfs_spin_unlock(&qunit_hash_lock);
979
980         /* From here, the quota request will be sent anyway.
981          * When this qdata request returned or is cancelled,
982          * lqs_putref will be called at that time */
983         lqs_getref(lqs);
984         /* this is for quota_search_lqs */
985         lqs_putref(lqs);
986
987         QDATA_DEBUG(qdata, "obd(%s): send %s quota req\n",
988                     obd->obd_name, (opc == QUOTA_DQACQ) ? "acq" : "rel");
989         /* master is going to dqacq/dqrel from itself */
990         if (is_master(qctxt)) {
991                 int rc2;
992                 QDATA_DEBUG(qdata, "local %s.\n",
993                             opc == QUOTA_DQACQ ? "DQACQ" : "DQREL");
994                 QDATA_SET_CHANGE_QS(qdata);
995                 rc = qctxt->lqc_handler(obd, qdata, opc);
996                 rc2 = dqacq_completion(obd, qctxt, qdata, rc, opc);
997                 /* this is for qunit_get() */
998                 qunit_put(qunit);
999
1000                 cfs_gettimeofday(&work_end);
1001                 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
1002                 if (opc == QUOTA_DQACQ)
1003                         lprocfs_counter_add(qctxt->lqc_stats,
1004                                             wait ? LQUOTA_SYNC_ACQ : LQUOTA_ASYNC_ACQ,
1005                                             timediff);
1006                 else
1007                         lprocfs_counter_add(qctxt->lqc_stats,
1008                                             wait ? LQUOTA_SYNC_REL : LQUOTA_ASYNC_REL,
1009                                             timediff);
1010                 RETURN(rc ? rc : rc2);
1011         }
1012
1013         cfs_spin_lock(&qctxt->lqc_lock);
1014         if (!qctxt->lqc_import) {
1015                 cfs_spin_unlock(&qctxt->lqc_lock);
1016                 QDATA_DEBUG(qdata, "lqc_import is invalid.\n");
1017
1018                 cfs_spin_lock(&qunit_hash_lock);
1019                 remove_qunit_nolock(qunit);
1020                 cfs_spin_unlock(&qunit_hash_lock);
1021
1022                 compute_lqs_after_removing_qunit(qunit);
1023
1024                 QUNIT_SET_STATE_AND_RC(qunit, QUNIT_FINISHED, -EAGAIN);
1025                 cfs_waitq_broadcast(&qunit->lq_waitq);
1026
1027                 /* this is for qunit_get() */
1028                 qunit_put(qunit);
1029                 /* this for alloc_qunit() */
1030                 qunit_put(qunit);
1031                 cfs_spin_lock(&qctxt->lqc_lock);
1032                 if (wait && !qctxt->lqc_import) {
1033                         cfs_spin_unlock(&qctxt->lqc_lock);
1034                         LASSERT(oti && oti->oti_thread);
1035                         /* The recovery thread doesn't have watchdog
1036                          * attached. LU-369 */
1037                         if (oti->oti_thread->t_watchdog)
1038                                 lc_watchdog_disable(oti->oti_thread->\
1039                                                 t_watchdog);
1040                         CDEBUG(D_QUOTA, "sleep for quota master\n");
1041                         l_wait_event(qctxt->lqc_wait_for_qmaster,
1042                                      check_qm(qctxt), &lwi);
1043                         CDEBUG(D_QUOTA, "wake up when quota master is back\n");
1044                         if (oti->oti_thread->t_watchdog)
1045                                 lc_watchdog_touch(oti->oti_thread->t_watchdog,
1046                                       CFS_GET_TIMEOUT(oti->oti_thread->t_svc));
1047                 } else {
1048                         cfs_spin_unlock(&qctxt->lqc_lock);
1049                 }
1050
1051                 RETURN(-EAGAIN);
1052         }
1053         imp = class_import_get(qctxt->lqc_import);
1054         cfs_spin_unlock(&qctxt->lqc_lock);
1055
1056         /* build dqacq/dqrel request */
1057         LASSERT(imp);
1058
1059         req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_QUOTA_DQACQ,
1060                                         LUSTRE_MDS_VERSION, opc);
1061         class_import_put(imp);
1062         if (req == NULL) {
1063                 CERROR("Can't alloc request\n");
1064                 dqacq_completion(obd, qctxt, qdata, -ENOMEM, opc);
1065                 /* this is for qunit_get() */
1066                 qunit_put(qunit);
1067                 RETURN(-ENOMEM);
1068         }
1069
1070         ptlrpc_request_set_replen(req);
1071         req->rq_no_resend = req->rq_no_delay = 1;
1072         rc = quota_copy_qdata(req, qdata, QUOTA_REQUEST, QUOTA_IMPORT);
1073         if (rc < 0) {
1074                 CERROR("Can't pack qunit_data(rc: %d)\n", rc);
1075                 ptlrpc_req_finished(req);
1076                 dqacq_completion(obd, qctxt, qdata, -EPROTO, opc);
1077                 /* this is for qunit_get() */
1078                 qunit_put(qunit);
1079                 RETURN(rc);
1080         }
1081
1082         CLASSERT(sizeof(*aa) <= sizeof(req->rq_async_args));
1083         aa = ptlrpc_req_async_args(req);
1084         aa->aa_ctxt = qctxt;
1085         aa->aa_qunit = qunit;
1086
1087         req->rq_interpret_reply = dqacq_interpret;
1088         ptlrpcd_add_req(req, PDL_POLICY_LOCAL, -1);
1089
1090         QDATA_DEBUG(qdata, "%s scheduled.\n",
1091                     opc == QUOTA_DQACQ ? "DQACQ" : "DQREL");
1092 wait_completion:
1093         if (wait && qunit) {
1094                 struct qunit_data *p = &qunit->lq_data;
1095
1096                 QDATA_DEBUG(p, "qunit(%p) is waiting for dqacq.\n", qunit);
1097                 l_wait_event(qunit->lq_waitq, got_qunit(qunit, is_master(qctxt)),
1098                              &lwi);
1099                 /* rc = -EAGAIN, it means the quota master isn't ready yet
1100                  * rc = QUOTA_REQ_RETURNED, it means a quota req is finished;
1101                  * rc = -EDQUOT, it means out of quota
1102                  * rc = -EBUSY, it means recovery is happening
1103                  * other rc < 0, it means real errors, functions who call
1104                  * schedule_dqacq should take care of this */
1105                 cfs_spin_lock(&qunit->lq_lock);
1106                 rc = qunit->lq_rc;
1107                 cfs_spin_unlock(&qunit->lq_lock);
1108                 CDEBUG(D_QUOTA, "qunit(%p) finishes waiting: id(%u) flag(%u) "
1109                        "rc(%d) owner(%d)\n", qunit, qunit->lq_data.qd_id,
1110                        qunit->lq_data.qd_flags, rc, qunit->lq_owner);
1111         }
1112
1113         qunit_put(qunit);
1114         cfs_gettimeofday(&work_end);
1115         timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
1116         if (opc == QUOTA_DQACQ)
1117                 lprocfs_counter_add(qctxt->lqc_stats,
1118                                     wait ? LQUOTA_SYNC_ACQ : LQUOTA_ASYNC_ACQ,
1119                                     timediff);
1120         else
1121                 lprocfs_counter_add(qctxt->lqc_stats,
1122                                     wait ? LQUOTA_SYNC_REL : LQUOTA_ASYNC_REL,
1123                                     timediff);
1124
1125         RETURN(rc);
1126 }
1127
1128 int
1129 qctxt_adjust_qunit(struct obd_device *obd, struct lustre_quota_ctxt *qctxt,
1130                    const unsigned int id[], __u32 isblk, int wait,
1131                    struct obd_trans_info *oti)
1132 {
1133         int rc = 0, i = USRQUOTA;
1134         struct qunit_data qdata[MAXQUOTAS];
1135         ENTRY;
1136
1137         /* XXX In quota_chk_acq_common(), we do something like:
1138          *
1139          *     while (quota_check_common() & QUOTA_RET_ACQUOTA) {
1140          *            rc = qctxt_adjust_qunit();
1141          *     }
1142          *
1143          *     to make sure the slave acquired enough quota from master.
1144          *
1145          *     Unfortunately, qctxt_adjust_qunit() checks QB/QI_SET to
1146          *     decide if do real DQACQ or not, but quota_check_common()
1147          *     doesn't check QB/QI_SET flags. This inconsistence could
1148          *     lead into an infinite loop.
1149          *
1150          *     We can't fix it by simply adding QB/QI_SET checking in the
1151          *     quota_check_common(), since we must guarantee that the
1152          *     paried quota_pending_commit() has same QB/QI_SET, but the
1153          *     flags can be actually cleared at any time...
1154          *
1155          *     A quick non-intrusive solution is to just skip the
1156          *     QB/QI_SET checking here when the @wait is non-zero.
1157          *     (If the @wait is non-zero, the caller must have already
1158          *     checked the QB/QI_SET).
1159          */
1160         if (!wait && quota_is_set(obd, id, isblk ? QB_SET : QI_SET) == 0)
1161                 RETURN(0);
1162
1163         for (i = 0; i < MAXQUOTAS; i++) {
1164                 qdata[i].qd_id = id[i];
1165                 qdata[i].qd_flags = i;
1166                 if (isblk)
1167                         QDATA_SET_BLK(&qdata[i]);
1168                 qdata[i].qd_count = 0;
1169
1170                 rc = check_cur_qunit(obd, qctxt, &qdata[i]);
1171                 if (rc > 0) {
1172                         int opc;
1173                         /* need acquire or release */
1174                         opc = rc == 1 ? QUOTA_DQACQ : QUOTA_DQREL;
1175                         rc = schedule_dqacq(obd, qctxt, &qdata[i], opc,
1176                                             wait,oti);
1177                         if (rc < 0)
1178                                 RETURN(rc);
1179                 } else if (wait == 1) {
1180                         /* when wait equates 1, that means mds_quota_acquire
1181                          * or filter_quota_acquire is calling it. */
1182                         rc = qctxt_wait_pending_dqacq(qctxt, id[i], i, isblk);
1183                         if (rc < 0)
1184                                 RETURN(rc);
1185                 }
1186         }
1187
1188         RETURN(rc);
1189 }
1190
1191 int
1192 qctxt_wait_pending_dqacq(struct lustre_quota_ctxt *qctxt, unsigned int id,
1193                          unsigned short type, int isblk)
1194 {
1195         struct lustre_qunit *qunit = NULL;
1196         struct qunit_data qdata;
1197         struct timeval work_start;
1198         struct timeval work_end;
1199         long timediff;
1200         struct l_wait_info lwi = { 0 };
1201         int rc = 0;
1202         ENTRY;
1203
1204         cfs_gettimeofday(&work_start);
1205         qdata.qd_id = id;
1206         qdata.qd_flags = type;
1207         if (isblk)
1208                 QDATA_SET_BLK(&qdata);
1209         qdata.qd_count = 0;
1210
1211         cfs_spin_lock(&qunit_hash_lock);
1212         qunit = dqacq_in_flight(qctxt, &qdata);
1213         cfs_spin_unlock(&qunit_hash_lock);
1214
1215         if (qunit) {
1216                 struct qunit_data *p = &qunit->lq_data;
1217
1218                 QDATA_DEBUG(p, "qunit(%p) is waiting for dqacq.\n", qunit);
1219                 l_wait_event(qunit->lq_waitq, got_qunit(qunit, is_master(qctxt)),
1220                              &lwi);
1221                 CDEBUG(D_QUOTA, "qunit(%p) finishes waiting: rc(%d) "
1222                        "owner(%d)\n", qunit, qunit->lq_rc, qunit->lq_owner);
1223                 /* keep same as schedule_dqacq() b=17030 */
1224                 cfs_spin_lock(&qunit->lq_lock);
1225                 rc = qunit->lq_rc;
1226                 cfs_spin_unlock(&qunit->lq_lock);
1227                 /* this is for dqacq_in_flight() */
1228                 qunit_put(qunit);
1229                 cfs_gettimeofday(&work_end);
1230                 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
1231                 lprocfs_counter_add(qctxt->lqc_stats,
1232                                     isblk ? LQUOTA_WAIT_PENDING_BLK_QUOTA :
1233                                             LQUOTA_WAIT_PENDING_INO_QUOTA,
1234                                     timediff);
1235         } else {
1236                 cfs_gettimeofday(&work_end);
1237                 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
1238                 lprocfs_counter_add(qctxt->lqc_stats,
1239                                     isblk ? LQUOTA_NOWAIT_PENDING_BLK_QUOTA :
1240                                             LQUOTA_NOWAIT_PENDING_INO_QUOTA,
1241                                     timediff);
1242         }
1243
1244         RETURN(rc);
1245 }
1246
1247 int
1248 qctxt_init(struct obd_device *obd, dqacq_handler_t handler)
1249 {
1250         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
1251         struct obd_device_target *obt = &obd->u.obt;
1252         struct super_block *sb = obt->obt_sb;
1253         int rc = 0;
1254         ENTRY;
1255
1256         LASSERT(qctxt);
1257
1258         rc = ptlrpcd_addref();
1259         if (rc)
1260                 RETURN(rc);
1261
1262         cfs_waitq_init(&qctxt->lqc_wait_for_qmaster);
1263         cfs_waitq_init(&qctxt->lqc_lqs_waitq);
1264         cfs_atomic_set(&qctxt->lqc_lqs, 0);
1265         cfs_spin_lock_init(&qctxt->lqc_lock);
1266         cfs_spin_lock(&qctxt->lqc_lock);
1267         qctxt->lqc_handler = handler;
1268         qctxt->lqc_sb = sb;
1269         qctxt->lqc_obt = obt;
1270         qctxt->lqc_import = NULL;
1271         qctxt->lqc_recovery = 0;
1272         qctxt->lqc_switch_qs = 1; /* Change qunit size in default setting */
1273         qctxt->lqc_valid = 1;
1274         qctxt->lqc_cqs_boundary_factor = 4;
1275         qctxt->lqc_cqs_least_bunit = PTLRPC_MAX_BRW_SIZE;
1276         qctxt->lqc_cqs_least_iunit = 2;
1277         qctxt->lqc_cqs_qs_factor = 2;
1278         qctxt->lqc_flags = 0;
1279         QUOTA_MASTER_UNREADY(qctxt);
1280         qctxt->lqc_bunit_sz = default_bunit_sz;
1281         qctxt->lqc_btune_sz = default_bunit_sz / 100 * default_btune_ratio;
1282         qctxt->lqc_iunit_sz = default_iunit_sz;
1283         qctxt->lqc_itune_sz = default_iunit_sz * default_itune_ratio / 100;
1284         qctxt->lqc_switch_seconds = 300; /* enlarging will wait 5 minutes
1285                                           * after the last shrinking */
1286         qctxt->lqc_sync_blk = 0;
1287         cfs_spin_unlock(&qctxt->lqc_lock);
1288
1289         qctxt->lqc_lqs_hash = cfs_hash_create("LQS_HASH",
1290                                               hash_lqs_cur_bits,
1291                                               HASH_LQS_MAX_BITS,
1292                                               min(hash_lqs_cur_bits,
1293                                                   HASH_LQS_BKT_BITS),
1294                                               0, CFS_HASH_MIN_THETA,
1295                                               CFS_HASH_MAX_THETA,
1296                                               &lqs_hash_ops, CFS_HASH_DEFAULT);
1297         if (!qctxt->lqc_lqs_hash) {
1298                 CERROR("initialize hash lqs for %s error!\n", obd->obd_name);
1299                 RETURN(-ENOMEM);
1300         }
1301
1302 #ifdef LPROCFS
1303         rc = lquota_proc_setup(obd, is_master(qctxt));
1304         if (rc)
1305                 CERROR("initialize proc for %s error!\n", obd->obd_name);
1306 #endif
1307
1308         RETURN(rc);
1309 }
1310
1311 static int check_lqs(struct lustre_quota_ctxt *qctxt)
1312 {
1313         int rc;
1314         ENTRY;
1315
1316         rc = !cfs_atomic_read(&qctxt->lqc_lqs);
1317
1318         RETURN(rc);
1319 }
1320
1321 int qctxt_del_lqs(cfs_hash_t *hs, cfs_hash_bd_t *bd,
1322                  cfs_hlist_node_t *hnode, void *data)
1323 {
1324         /* remove from hash and -1 refcount */
1325         cfs_hash_bd_del_locked(hs, bd, hnode);
1326         return 0;
1327 }
1328
1329 void qctxt_cleanup(struct lustre_quota_ctxt *qctxt, int force)
1330 {
1331         struct lustre_qunit *qunit, *tmp;
1332         cfs_list_t tmp_list;
1333         struct l_wait_info lwi = { 0 };
1334         struct obd_device_target *obt = qctxt->lqc_obt;
1335         int i;
1336         ENTRY;
1337
1338         CFS_INIT_LIST_HEAD(&tmp_list);
1339
1340         cfs_spin_lock(&qctxt->lqc_lock);
1341         qctxt->lqc_valid = 0;
1342         cfs_spin_unlock(&qctxt->lqc_lock);
1343
1344         cfs_spin_lock(&qunit_hash_lock);
1345         for (i = 0; i < NR_DQHASH; i++) {
1346                 cfs_list_for_each_entry_safe(qunit, tmp, &qunit_hash[i],
1347                                              lq_hash) {
1348                         if (qunit->lq_ctxt != qctxt)
1349                                 continue;
1350                         remove_qunit_nolock(qunit);
1351                         cfs_list_add(&qunit->lq_hash, &tmp_list);
1352                 }
1353         }
1354         cfs_spin_unlock(&qunit_hash_lock);
1355
1356         cfs_list_for_each_entry_safe(qunit, tmp, &tmp_list, lq_hash) {
1357                 cfs_list_del_init(&qunit->lq_hash);
1358                 compute_lqs_after_removing_qunit(qunit);
1359
1360                 /* wake up all waiters */
1361                 QUNIT_SET_STATE_AND_RC(qunit, QUNIT_FINISHED, 0);
1362                 cfs_waitq_broadcast(&qunit->lq_waitq);
1363                 qunit_put(qunit);
1364         }
1365
1366         /* after qctxt_cleanup, qctxt might be freed, then check_qm() is
1367          * unpredicted. So we must wait until lqc_wait_for_qmaster is empty */
1368         while (cfs_waitq_active(&qctxt->lqc_wait_for_qmaster)) {
1369                 cfs_waitq_signal(&qctxt->lqc_wait_for_qmaster);
1370                 cfs_schedule_timeout_and_set_state(CFS_TASK_INTERRUPTIBLE,
1371                                                    cfs_time_seconds(1));
1372         }
1373
1374         /* release refcount on lustre_qunit_size holding by lqs_hash */
1375         cfs_hash_for_each_safe(qctxt->lqc_lqs_hash, qctxt_del_lqs, NULL);
1376
1377         l_wait_event(qctxt->lqc_lqs_waitq, check_lqs(qctxt), &lwi);
1378         cfs_down_write(&obt->obt_rwsem);
1379         cfs_hash_putref(qctxt->lqc_lqs_hash);
1380         qctxt->lqc_lqs_hash = NULL;
1381         cfs_up_write(&obt->obt_rwsem);
1382
1383         ptlrpcd_decref();
1384
1385 #ifdef LPROCFS
1386         if (lquota_proc_cleanup(qctxt))
1387                 CERROR("cleanup proc error!\n");
1388 #endif
1389
1390         EXIT;
1391 }
1392
1393 struct qslave_recov_thread_data {
1394         struct obd_device *obd;
1395         struct lustre_quota_ctxt *qctxt;
1396         cfs_completion_t comp;
1397 };
1398
1399 /* FIXME only recovery block quota by now */
1400 static int qslave_recovery_main(void *arg)
1401 {
1402         struct qslave_recov_thread_data *data = arg;
1403         struct obd_device *obd = data->obd;
1404         struct lustre_quota_ctxt *qctxt = data->qctxt;
1405         unsigned int type;
1406         int rc = 0;
1407         ENTRY;
1408
1409         cfs_daemonize_ctxt("qslave_recovd");
1410
1411         /* for obdfilter */
1412         class_incref(obd, "qslave_recovd_filter", obd);
1413
1414         cfs_complete(&data->comp);
1415
1416         cfs_spin_lock(&qctxt->lqc_lock);
1417         if (qctxt->lqc_recovery) {
1418                 cfs_spin_unlock(&qctxt->lqc_lock);
1419                 class_decref(obd, "qslave_recovd_filter", obd);
1420                 RETURN(0);
1421         } else {
1422                 qctxt->lqc_recovery = 1;
1423                 cfs_spin_unlock(&qctxt->lqc_lock);
1424         }
1425
1426         for (type = USRQUOTA; type < MAXQUOTAS; type++) {
1427                 struct qunit_data qdata;
1428                 struct quota_info *dqopt = sb_dqopt(qctxt->lqc_sb);
1429                 cfs_list_t id_list;
1430                 struct dquot_id *dqid, *tmp;
1431                 int ret;
1432
1433                 LOCK_DQONOFF_MUTEX(dqopt);
1434                 if (!ll_sb_has_quota_active(qctxt->lqc_sb, type)) {
1435                         UNLOCK_DQONOFF_MUTEX(dqopt);
1436                         break;
1437                 }
1438
1439                 LASSERT(dqopt->files[type] != NULL);
1440                 CFS_INIT_LIST_HEAD(&id_list);
1441 #ifndef KERNEL_SUPPORTS_QUOTA_READ
1442                 rc = fsfilt_qids(obd, dqopt->files[type], NULL, type, &id_list);
1443 #else
1444                 rc = fsfilt_qids(obd, NULL, dqopt->files[type], type, &id_list);
1445 #endif
1446                 UNLOCK_DQONOFF_MUTEX(dqopt);
1447                 if (rc)
1448                         CERROR("Get ids from quota file failed. (rc:%d)\n", rc);
1449
1450                 cfs_list_for_each_entry_safe(dqid, tmp, &id_list, di_link) {
1451                         cfs_list_del_init(&dqid->di_link);
1452                         /* skip slave recovery on itself */
1453                         if (is_master(qctxt))
1454                                 goto free;
1455                         if (rc && rc != -EBUSY)
1456                                 goto free;
1457
1458                         qdata.qd_id = dqid->di_id;
1459                         qdata.qd_flags = type;
1460                         QDATA_SET_BLK(&qdata);
1461                         qdata.qd_count = 0;
1462
1463                         ret = check_cur_qunit(obd, qctxt, &qdata);
1464                         if (ret > 0) {
1465                                 int opc;
1466                                 opc = ret == 1 ? QUOTA_DQACQ : QUOTA_DQREL;
1467                                 rc = schedule_dqacq(obd, qctxt, &qdata, opc,
1468                                                     0, NULL);
1469                                 if (rc == -EDQUOT)
1470                                         rc = 0;
1471                         } else {
1472                                 rc = 0;
1473                         }
1474
1475                         if (rc && rc != -EBUSY)
1476                                 CERROR("qslave recovery failed! (id:%d type:%d "
1477                                        " rc:%d)\n", dqid->di_id, type, rc);
1478 free:
1479                         OBD_FREE_PTR(dqid);
1480                 }
1481         }
1482
1483         cfs_spin_lock(&qctxt->lqc_lock);
1484         qctxt->lqc_recovery = 0;
1485         cfs_spin_unlock(&qctxt->lqc_lock);
1486         class_decref(obd, "qslave_recovd_filter", obd);
1487         RETURN(rc);
1488 }
1489
1490 void
1491 qslave_start_recovery(struct obd_device *obd, struct lustre_quota_ctxt *qctxt)
1492 {
1493         struct qslave_recov_thread_data data;
1494         int rc;
1495         ENTRY;
1496
1497         if (!ll_sb_any_quota_active(qctxt->lqc_sb))
1498                 goto exit;
1499
1500         data.obd = obd;
1501         data.qctxt = qctxt;
1502         cfs_init_completion(&data.comp);
1503
1504         rc = cfs_create_thread(qslave_recovery_main, &data,
1505                                CFS_DAEMON_FLAGS);
1506         if (rc < 0) {
1507                 CERROR("Cannot start quota recovery thread: rc %d\n", rc);
1508                 goto exit;
1509         }
1510         cfs_wait_for_completion(&data.comp);
1511 exit:
1512         EXIT;
1513 }
1514
1515 inline int quota_is_on(struct lustre_quota_ctxt *qctxt,
1516                        struct obd_quotactl *oqctl)
1517 {
1518         return ((qctxt->lqc_flags & UGQUOTA2LQC(oqctl->qc_type)) ==
1519                 UGQUOTA2LQC(oqctl->qc_type));
1520 }
1521
1522 inline int quota_is_off(struct lustre_quota_ctxt *qctxt,
1523                         struct obd_quotactl *oqctl)
1524 {
1525         return !(qctxt->lqc_flags & UGQUOTA2LQC(oqctl->qc_type));
1526 }
1527
1528 /**
1529  * When quotaon, build a lqs for every uid/gid who has been set limitation
1530  * for quota. After quota_search_lqs, it will hold one ref for the lqs.
1531  * It will be released when qctxt_cleanup() is executed b=18574
1532  *
1533  * Should be called with obt->obt_quotachecking held. b=20152 
1534  */
1535 void build_lqs(struct obd_device *obd)
1536 {
1537         struct obd_device_target *obt = &obd->u.obt;
1538         struct lustre_quota_ctxt *qctxt = &obt->obt_qctxt;
1539         cfs_list_t id_list;
1540         int i, rc;
1541
1542         LASSERT_SEM_LOCKED(&obt->obt_quotachecking);
1543         CFS_INIT_LIST_HEAD(&id_list);
1544         for (i = 0; i < MAXQUOTAS; i++) {
1545                 struct dquot_id *dqid, *tmp;
1546
1547                 if (sb_dqopt(qctxt->lqc_sb)->files[i] == NULL)
1548                         continue;
1549
1550 #ifndef KERNEL_SUPPORTS_QUOTA_READ
1551                 rc = fsfilt_qids(obd, sb_dqopt(qctxt->lqc_sb)->files[i], NULL,
1552                                  i, &id_list);
1553 #else
1554                 rc = fsfilt_qids(obd, NULL, sb_dqopt(qctxt->lqc_sb)->files[i],
1555                                  i, &id_list);
1556 #endif
1557                 if (rc) {
1558                         CERROR("%s: failed to get %s qids!\n", obd->obd_name,
1559                                i ? "group" : "user");
1560                         continue;
1561                 }
1562
1563                 cfs_list_for_each_entry_safe(dqid, tmp, &id_list,
1564                                              di_link) {
1565                         struct lustre_qunit_size *lqs;
1566
1567                         cfs_list_del_init(&dqid->di_link);
1568                         lqs = quota_search_lqs(LQS_KEY(i, dqid->di_id),
1569                                                qctxt, 1);
1570                         if (lqs && !IS_ERR(lqs)) {
1571                                 lqs->lqs_flags |= dqid->di_flag;
1572                                 lqs_putref(lqs);
1573                         } else {
1574                                 CERROR("%s: failed to create a lqs for %sid %u"
1575                                        "\n", obd->obd_name, i ? "g" : "u",
1576                                        dqid->di_id);
1577                         }
1578
1579                         OBD_FREE_PTR(dqid);
1580                 }
1581         }
1582 }
1583
1584 /**
1585  * lqs<->qctxt hash operations
1586  */
1587
1588 /**
1589  * string hashing using djb2 hash algorithm
1590  */
1591 static unsigned
1592 lqs_hash(cfs_hash_t *hs, const void *key, unsigned mask)
1593 {
1594         unsigned long long id;
1595         unsigned hash;
1596         ENTRY;
1597
1598         LASSERT(key);
1599         id = *((unsigned long long *)key);
1600         hash = (LQS_KEY_GRP(id) ? 5381 : 5387) * (unsigned)LQS_KEY_ID(id);
1601
1602         RETURN(hash & mask);
1603 }
1604
1605 static void *
1606 lqs_key(cfs_hlist_node_t *hnode)
1607 {
1608         struct lustre_qunit_size *lqs;
1609         ENTRY;
1610
1611         lqs = cfs_hlist_entry(hnode, struct lustre_qunit_size, lqs_hash);
1612         RETURN(&lqs->lqs_key);
1613 }
1614
1615 static int
1616 lqs_keycmp(const void *key, cfs_hlist_node_t *hnode)
1617 {
1618         struct lustre_qunit_size *q =
1619                 cfs_hlist_entry(hnode, struct lustre_qunit_size, lqs_hash);
1620
1621         RETURN(q->lqs_key == *((unsigned long long *)key));
1622 }
1623
1624 static void *
1625 lqs_object(cfs_hlist_node_t *hnode)
1626 {
1627         return cfs_hlist_entry(hnode, struct lustre_qunit_size, lqs_hash);
1628 }
1629
1630 static void
1631 lqs_get(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
1632 {
1633         struct lustre_qunit_size *q =
1634                 cfs_hlist_entry(hnode, struct lustre_qunit_size, lqs_hash);
1635
1636         lqs_getref(q);
1637 }
1638
1639 static void
1640 lqs_put_locked(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
1641 {
1642         struct lustre_qunit_size *q =
1643                 cfs_hlist_entry(hnode, struct lustre_qunit_size, lqs_hash);
1644
1645         lqs_putref(q);
1646 }
1647
1648 static void
1649 lqs_exit(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
1650 {
1651         CERROR("It should not have any item left to be handled by this!");
1652 }
1653
1654 static cfs_hash_ops_t lqs_hash_ops = {
1655         .hs_hash        = lqs_hash,
1656         .hs_key         = lqs_key,
1657         .hs_keycmp      = lqs_keycmp,
1658         .hs_object      = lqs_object,
1659         .hs_get         = lqs_get,
1660         .hs_put_locked  = lqs_put_locked,
1661         .hs_exit        = lqs_exit
1662 };