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