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