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