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