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