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