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