Whamcloud - gitweb
3446229ad9ae08e96b12e2ddb53da03ff12dedba
[fs/lustre-release.git] / lustre / quota / qmt_handler.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, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 021110-1307, USA
20  *
21  * GPL HEADER END
22  */
23 /*
24  * Copyright (c) 2012, 2017, Intel Corporation.
25  * Use is subject to license terms.
26  *
27  * Author: Johann Lombardi <johann.lombardi@intel.com>
28  * Author: Niu    Yawei    <yawei.niu@intel.com>
29  */
30
31 #define DEBUG_SUBSYSTEM S_LQUOTA
32
33 #include <obd_class.h>
34 #include "qmt_internal.h"
35
36 /*
37  * Retrieve quota settings for a given identifier.
38  *
39  * \param env     - is the environment passed by the caller
40  * \param qmt     - is the quota master target
41  * \param restype - is the pool type, either block (i.e. LQUOTA_RES_DT) or inode
42  *                  (i.e. LQUOTA_RES_MD)
43  * \param qtype   - is the quota type
44  * \param id      - is the quota indentifier for which we want to acces quota
45  *                  settings.
46  * \param hard    - is the output variable where to copy the hard limit
47  * \param soft    - is the output variable where to copy the soft limit
48  * \param time    - is the output variable where to copy the grace time
49  */
50 static int qmt_get(const struct lu_env *env, struct qmt_device *qmt,
51                    __u8 restype, __u8 qtype, union lquota_id *id,
52                    __u64 *hard, __u64 *soft, __u64 *time, bool is_default,
53                    char *pool_name)
54 {
55         struct lquota_entry     *lqe;
56         ENTRY;
57
58         LASSERT(!is_default || id->qid_uid == 0);
59         if (pool_name && !strnlen(pool_name, LOV_MAXPOOLNAME))
60                 pool_name = NULL;
61
62         /* look-up lqe structure containing quota settings */
63         lqe = qmt_pool_lqe_lookup(env, qmt, restype, qtype, id, pool_name);
64         if (IS_ERR(lqe))
65                 RETURN(PTR_ERR(lqe));
66
67         /* copy quota settings */
68         lqe_read_lock(lqe);
69         LQUOTA_DEBUG(lqe, "fetch settings");
70         if (hard != NULL)
71                 *hard = lqe->lqe_hardlimit;
72         if (soft != NULL)
73                 *soft = lqe->lqe_softlimit;
74         if (time != NULL) {
75                 *time = lqe->lqe_gracetime;
76                 if (lqe->lqe_is_default)
77                         *time |= (__u64)LQUOTA_FLAG_DEFAULT <<
78                                                         LQUOTA_GRACE_BITS;
79         }
80         lqe_read_unlock(lqe);
81
82         lqe_putref(lqe);
83         RETURN(0);
84 }
85
86 struct qmt_entry_iter_data {
87         const struct lu_env *qeid_env;
88         struct qmt_device   *qeid_qmt;
89 };
90
91 static int qmt_entry_iter_cb(struct cfs_hash *hs, struct cfs_hash_bd *bd,
92                              struct hlist_node *hnode, void *d)
93 {
94         struct qmt_entry_iter_data *iter = (struct qmt_entry_iter_data *)d;
95         struct lquota_entry     *lqe;
96
97         lqe = hlist_entry(hnode, struct lquota_entry, lqe_hash);
98         LASSERT(atomic_read(&lqe->lqe_ref) > 0);
99
100         if (lqe->lqe_id.qid_uid == 0 || !lqe->lqe_is_default)
101                 return 0;
102
103         return qmt_set_with_lqe(iter->qeid_env, iter->qeid_qmt, lqe, 0, 0, 0, 0,
104                                 true, true);
105 }
106
107 static void qmt_set_id_notify(const struct lu_env *env, struct qmt_device *qmt,
108                               struct lquota_entry *lqe)
109 {
110         struct lquota_entry *lqe_gl;
111         int rc;
112
113         lqe_gl = lqe->lqe_is_global ? lqe : NULL;
114         rc = qmt_pool_lqes_lookup_spec(env, qmt, lqe_rtype(lqe),
115                                        lqe_qtype(lqe), &lqe->lqe_id);
116         if (rc)
117                 GOTO(lqes_fini, rc);
118
119         if (!lqe_gl && qti_lqes_glbl(env)->lqe_is_global)
120                 lqe_gl = qti_lqes_glbl(env);
121
122         if (!lqe_gl)
123                 GOTO(lqes_fini, rc);
124
125         mutex_lock(&lqe_gl->lqe_glbl_data_lock);
126         if (lqe_gl->lqe_glbl_data)
127                 qmt_seed_glbe(env, lqe_gl->lqe_glbl_data);
128         mutex_unlock(&lqe_gl->lqe_glbl_data_lock);
129
130         /* Even if slaves haven't enqueued quota lock yet,
131          * it is needed to set lqe_revoke_time in qmt_id_lock_glimpse
132          * in case of reaching qpi_least_qunit */
133         qmt_id_lock_notify(qmt, lqe_gl);
134 lqes_fini:
135         qti_lqes_fini(env);
136 }
137
138 /*
139  * Update quota settings for a given lqe.
140  *
141  * \param env        - is the environment passed by the caller
142  * \param qmt        - is the quota master target
143  * \param lqe        - is the lquota_entry for which we want to modify quota
144  *                     settings.
145  * \param hard       - is the new hard limit
146  * \param soft       - is the new soft limit
147  * \param time       - is the new grace time
148  * \param valid      - is the list of settings to change
149  * \param is_default - true for default quota setting
150  * \param is_updated - true if the lqe is updated and no need to write back
151  */
152
153 int qmt_set_with_lqe(const struct lu_env *env, struct qmt_device *qmt,
154                      struct lquota_entry *lqe, __u64 hard, __u64 soft,
155                      __u64 time, __u32 valid, bool is_default, bool is_updated)
156 {
157         struct thandle *th = NULL;
158         time64_t now = 0;
159         __u64 ver;
160         bool dirtied = false;
161         int rc = 0;
162         bool need_id_notify = false;
163         ENTRY;
164
165         /* need to write back to global quota file? */
166         if (!is_updated) {
167                 /* By default we should have here only 1 lqe,
168                  * so no allocations should be done. */
169                 if (qti_lqes_restore_init(env))
170                         GOTO(out_nolock, rc = -ENOMEM);
171                 /* allocate & start transaction with enough credits to update
172                  * quota  settings in the global index file */
173                 th = qmt_trans_start(env, lqe);
174                 if (IS_ERR(th))
175                         GOTO(out_nolock, rc = PTR_ERR(th));
176         }
177
178         now = ktime_get_real_seconds();
179
180         lqe_write_lock(lqe);
181         LQUOTA_DEBUG(lqe,
182                      "changing quota settings valid:%x hard:%llu soft:%llu time:%llu",
183                      valid, hard, soft, time);
184
185         if (is_default && lqe->lqe_id.qid_uid != 0) {
186                 LQUOTA_DEBUG(lqe, "set qid %llu to use default quota setting",
187                              lqe->lqe_id.qid_uid);
188
189                 qmt_lqe_set_default(env, lqe->lqe_site->lqs_parent, lqe, false);
190                 GOTO(quota_set, 0);
191         }
192
193         if ((valid & QIF_TIMES) != 0 && lqe->lqe_gracetime != time) {
194                 /* change time settings */
195                 lqe->lqe_gracetime = time;
196                 dirtied            = true;
197         }
198
199         if ((valid & QIF_LIMITS) != 0 &&
200             (lqe->lqe_hardlimit != hard || lqe->lqe_softlimit != soft)) {
201                 rc = qmt_validate_limits(lqe, hard, soft);
202                 if (rc)
203                         GOTO(out, rc);
204
205                 /* change quota limits */
206                 lqe->lqe_hardlimit = hard;
207                 lqe->lqe_softlimit = soft;
208                 if (is_default) {
209                         dirtied = true;
210                         GOTO(quota_write, 0);
211                 }
212
213 quota_set:
214                 /* recompute qunit in case it was never initialized */
215                 if (qmt_revalidate(env, lqe))
216                         need_id_notify = true;
217
218                 /* clear grace time */
219                 if (lqe->lqe_softlimit == 0 ||
220                     lqe->lqe_granted <= lqe->lqe_softlimit)
221                         /* no soft limit or below soft limit, let's clear grace
222                          * time */
223                         lqe->lqe_gracetime = 0;
224                 else if ((valid & QIF_TIMES) == 0)
225                         /* set grace only if user hasn't provided his own */
226                          lqe->lqe_gracetime = now + qmt_lqe_grace(lqe);
227
228                 /* change enforced status based on new parameters */
229                 if (lqe->lqe_id.qid_uid == 0 || (lqe->lqe_hardlimit == 0 &&
230                     lqe->lqe_softlimit == 0)) {
231                         if (lqe->lqe_enforced) {
232                                 lqe->lqe_enforced = false;
233                                 /* Clear qunit and edquot as lqe_adjust_edquot
234                                  * does not handle not enforced lqes */
235                                 lqe->lqe_edquot = 0;
236                                 lqe->lqe_qunit = 0;
237                                 need_id_notify = true;
238                         }
239                 } else {
240                         lqe->lqe_enforced = true;
241                 }
242
243                 dirtied = true;
244         }
245
246         if (!is_default && lqe->lqe_is_default) {
247                 LQUOTA_DEBUG(lqe, "the qid %llu has been set quota"
248                              " explicitly, clear the default flag",
249                              lqe->lqe_id.qid_uid);
250
251                 qmt_lqe_clear_default(lqe);
252                 dirtied = true;
253         }
254
255 quota_write:
256         if (dirtied) {
257                 if (!is_updated) {
258                         /* write new quota settings to disk */
259                         rc = qmt_glb_write(env, th, lqe, LQUOTA_BUMP_VER, &ver);
260                         if (rc) {
261                                 /* restore initial quota settings */
262                                 qmt_restore(lqe, &qti_lqes_rstr(env)[0]);
263                                 GOTO(out, rc);
264                         }
265                 } else {
266                         ver = dt_version_get(env, LQE_GLB_OBJ(lqe));
267                 }
268
269                 /* compute new qunit value now that we have modified the quota
270                  * settings or clear/set edquot flag if needed */
271                 need_id_notify |= qmt_adjust_qunit(env, lqe);
272                 need_id_notify |= qmt_adjust_edquot(lqe, now);
273         }
274         EXIT;
275 out:
276         lqe_write_unlock(lqe);
277
278 out_nolock:
279         if (!is_updated) {
280                 if (th != NULL && !IS_ERR(th))
281                         dt_trans_stop(env, qmt->qmt_child, th);
282                 qti_lqes_restore_fini(env);
283         }
284
285         if (rc == 0 && dirtied) {
286                 qmt_glb_lock_notify(env, lqe, ver);
287                 if (lqe->lqe_id.qid_uid == 0) {
288                         struct qmt_entry_iter_data iter_data;
289
290                         LQUOTA_DEBUG(lqe, "notify all lqe with default quota");
291                         iter_data.qeid_env = env;
292                         iter_data.qeid_qmt = qmt;
293                         cfs_hash_for_each(lqe->lqe_site->lqs_hash,
294                                                qmt_entry_iter_cb, &iter_data);
295                         /* Always notify slaves with default values. Don't
296                          * care about overhead as will be sent only not changed
297                          * values(see qmt_id_lock_cb for details).*/
298                         need_id_notify = true;
299                 }
300                 /* qti_lqes_inited > 0 means we came here from another
301                  * qmt_pool_lqes_lookup(qmt_dqacq, intent_policy ...). Thus
302                  * we can't init and add new lqes to don't overwrite already
303                  * added.
304                  */
305                 if (!qti_lqes_inited(env) && need_id_notify)
306                         qmt_set_id_notify(env, qmt, lqe);
307         }
308
309         return rc;
310 }
311
312 /*
313  * Update quota settings for a given identifier.
314  *
315  * \param env        - is the environment passed by the caller
316  * \param qmt        - is the quota master target
317  * \param restype    - is the pool type, either block (i.e. LQUOTA_RES_DT) or
318  *                     inode (i.e. LQUOTA_RES_MD)
319  * \param qtype      - is the quota type
320  * \param id         - is the quota indentifier for which we want to modify
321  *                     quota settings.
322  * \param hard       - is the new hard limit
323  * \param soft       - is the new soft limit
324  * \param time       - is the new grace time
325  * \param valid      - is the list of settings to change
326  * \param is_default - true for default quota setting
327  * \param is_updated - true if the lqe is updated and no need to write back
328  */
329 static int qmt_set(const struct lu_env *env, struct qmt_device *qmt,
330                    __u8 restype, __u8 qtype, union lquota_id *id,
331                    __u64 hard, __u64 soft, __u64 time, __u32 valid,
332                    bool is_default, bool is_updated, char *pool_name)
333 {
334         struct lquota_entry *lqe;
335         int rc;
336         ENTRY;
337
338         if (pool_name && !strnlen(pool_name, LOV_MAXPOOLNAME))
339                 pool_name = NULL;
340
341         /* look-up quota entry associated with this ID */
342         lqe = qmt_pool_lqe_lookup(env, qmt, restype, qtype, id, pool_name);
343         if (IS_ERR(lqe))
344                         RETURN(PTR_ERR(lqe));
345
346         lqe->lqe_is_deleted = 0;
347         lqe->lqe_is_reset = 0;
348         rc = qmt_set_with_lqe(env, qmt, lqe, hard, soft, time, valid,
349                               is_default, is_updated);
350         if (rc == 0)
351                 lqe->lqe_is_deleted = 0;
352
353         lqe_putref(lqe);
354         RETURN(rc);
355 }
356
357 /*
358  * Delete the quota setting of the specified quota ID
359  *
360  * \param env        - is the environment passed by the caller
361  * \param qmt        - is the quota master target
362  * \param restype    - is the pool type, either block (i.e. LQUOTA_RES_DT) or
363  *                     inode (i.e. LQUOTA_RES_MD)
364  * \param qtype      - is the quota type
365  * \param qid        - is the quota indentifier for which we want to delete its
366  *                     quota settings.
367  */
368 static int qmt_delete_qid(const struct lu_env *env, struct qmt_device *qmt,
369                           __u8 restype, __u8 qtype, __u64 qid)
370 {
371         struct qmt_thread_info *qti = qmt_info(env);
372         union lquota_id *quota_id = &qti->qti_id;
373         struct thandle *th = NULL;
374         struct qmt_pool_info *qpi = NULL;
375         struct lquota_entry *lqe = NULL;
376         __u64 ver = 0;
377         int rc;
378
379         ENTRY;
380
381         quota_id->qid_uid = qid;
382         lqe = qmt_pool_lqe_lookup(env, qmt, restype, qtype, quota_id, NULL);
383         if (IS_ERR(lqe))
384                 RETURN(PTR_ERR(lqe));
385
386         lqe_write_lock(lqe);
387
388         qpi = qmt_pool_lookup_glb(env, qmt, restype);
389         if (IS_ERR(qpi))
390                 GOTO(out, rc = -ENOMEM);
391
392         th = qmt_trans_start(env, lqe);
393         if (IS_ERR(th))
394                 GOTO(out, rc = PTR_ERR(th));
395
396         rc = lquota_disk_delete(env, th,
397                                 qpi->qpi_glb_obj[qtype], qid, &ver);
398
399         dt_trans_stop(env, qmt->qmt_child, th);
400
401         if (rc == 0) {
402                 lqe_set_deleted(lqe);
403                 qmt_glb_lock_notify(env, lqe, ver);
404         } else if (rc == -ENOENT) {
405                 rc = 0;
406         }
407
408 out:
409         if (!IS_ERR_OR_NULL(qpi))
410                 qpi_putref(env, qpi);
411
412         lqe_write_unlock(lqe);
413         lqe_putref(lqe);
414
415         RETURN(rc);
416 }
417
418 static int qmt_reset_slv_cb(const struct lu_env *env, struct lu_fid *glb_fid,
419                             char *slv_name, struct lu_fid *slv_fid, void *arg)
420 {
421         struct qmt_device *qmt = (struct qmt_device *)arg;
422         struct qmt_thread_info *qti = qmt_info(env);
423         struct dt_object *slv_obj = NULL;
424         struct lquota_slv_rec rec;
425         struct thandle *th = NULL;
426         int rc;
427
428         slv_obj = dt_locate(env, qmt->qmt_child, slv_fid);
429         if (IS_ERR(slv_obj))
430                 GOTO(out, rc = PTR_ERR(slv_obj));
431
432         if (slv_obj->do_index_ops == NULL) {
433                 rc = slv_obj->do_ops->do_index_try(env, slv_obj,
434                                                    &dt_quota_slv_features);
435                 if (rc) {
436                         CERROR("%s: fail to setup slave idx for %s: rc = %d\n",
437                                qmt->qmt_child->dd_lu_dev.ld_obd->obd_name,
438                                slv_name, rc);
439                         GOTO(out, rc);
440                 }
441         }
442
443         th = qmt_trans_start(env, qti_lqes(env)[0]);
444         if (IS_ERR(th))
445                 GOTO(out, rc = PTR_ERR(th));
446
447         rec.qsr_granted = 0;
448         rc = lquota_disk_write(env, th, slv_obj, &qti->qti_id,
449                                (struct dt_rec *)&rec, 0, NULL);
450         if (rc)
451                 CERROR("%s: failed to reset slave grant for %s: rc = %d\n",
452                        qmt->qmt_child->dd_lu_dev.ld_obd->obd_name, slv_name,
453                        rc);
454 out:
455         if (!IS_ERR_OR_NULL(th))
456                 dt_trans_stop(env, qmt->qmt_child, th);
457
458         if (slv_obj != NULL)
459                 dt_object_put(env, slv_obj);
460         return 0;
461 }
462
463 /*
464  * Reset the quota of the quota ID, it will reset the soft/hard limit and grant
465  *
466  * \param env        - is the environment passed by the caller
467  * \param qmt        - is the quota master target
468  * \param restype    - is the pool type, either block (i.e. LQUOTA_RES_DT) or
469  *                     inode (i.e. LQUOTA_RES_MD)
470  * \param qtype      - is the quota type
471  * \param qid        - is the quota indentifier for which we want to delete its
472  *                     quota settings.
473  */
474 static int qmt_reset_qid(const struct lu_env *env, struct qmt_device *qmt,
475                          __u8 restype, __u8 qtype, __u64 qid)
476 {
477         struct qmt_thread_info *qti = qmt_info(env);
478         union lquota_id *quota_id = &qti->qti_id;
479         struct qmt_pool_info *qpi = NULL;
480         struct lquota_entry *lqe = NULL;
481         struct thandle *th = NULL;
482         __u64 softlimit = 0, hardlimit = 0;
483         __u64 ver = 0;
484         int rc;
485
486         ENTRY;
487
488         quota_id->qid_uid = qid;
489         lqe = qmt_pool_lqe_lookup(env, qmt, restype, qtype, quota_id, NULL);
490         if (IS_ERR(lqe))
491                 RETURN(PTR_ERR(lqe));
492
493         lqe_write_lock(lqe);
494
495         qpi = qmt_pool_lookup_glb(env, qmt, restype);
496         if (IS_ERR(qpi))
497                 GOTO(out, rc = -ENOMEM);
498
499         th = qmt_trans_start(env, lqe);
500         if (IS_ERR(th))
501                 GOTO(out, rc = PTR_ERR(th));
502
503         softlimit = lqe->lqe_softlimit;
504         hardlimit = lqe->lqe_hardlimit;
505
506         lqe->lqe_softlimit = 0;
507         lqe->lqe_hardlimit = 0;
508         lqe->lqe_granted = 0;
509         lqe->lqe_edquot = 0;
510         lqe->lqe_qunit = 0;
511         lqe->lqe_is_default = 0;
512         lqe->lqe_is_deleted = 0;
513         lqe->lqe_is_reset = 1;
514         rc = qmt_glb_write(env, th, lqe, LQUOTA_BUMP_VER, &ver);
515         if (rc)
516                 LQUOTA_ERROR(lqe, "failed to write quota global rec\n");
517         dt_trans_stop(env, qmt->qmt_child, th);
518         if (rc)
519                 GOTO(out, rc);
520
521         lquota_generate_fid(&qti->qti_fid, restype, qtype);
522         qti_lqes(env)[0] = lqe;
523         lquota_disk_for_each_slv(env, qpi->qpi_root, &qti->qti_fid,
524                                  qmt_reset_slv_cb, qmt);
525
526         qmt_glb_lock_notify(env, lqe, ver);
527
528 out:
529         if (rc) {
530                 if (softlimit != 0)
531                         lqe->lqe_softlimit = softlimit;
532                 if (hardlimit != 0)
533                         lqe->lqe_hardlimit = hardlimit;
534                 lqe->lqe_is_reset = 0;
535         }
536
537         if (!IS_ERR_OR_NULL(qpi))
538                 qpi_putref(env, qpi);
539
540         lqe_write_unlock(lqe);
541         lqe_putref(lqe);
542
543         RETURN(rc);
544 }
545 /*
546  * Handle quotactl request.
547  *
548  * \param env   - is the environment passed by the caller
549  * \param ld    - is the lu device associated with the qmt
550  * \param oqctl - is the quotactl request
551  */
552 static int qmt_quotactl(const struct lu_env *env, struct lu_device *ld,
553                         struct obd_quotactl *oqctl)
554 {
555         struct qmt_thread_info *qti = qmt_info(env);
556         union lquota_id *id  = &qti->qti_id;
557         struct qmt_device *qmt = lu2qmt_dev(ld);
558         struct obd_dqblk *dqb = &oqctl->qc_dqblk;
559         char *poolname;
560         int rc = 0;
561         bool is_default = false;
562         ENTRY;
563
564         LASSERT(qmt != NULL);
565
566         if (oqctl->qc_type >= LL_MAXQUOTAS)
567                 /* invalid quota type */
568                 RETURN(-EINVAL);
569
570         poolname = LUSTRE_Q_CMD_IS_POOL(oqctl->qc_cmd) ?
571                         oqctl->qc_poolname : NULL;
572
573         switch (oqctl->qc_cmd) {
574
575         case Q_GETINFO:  /* read grace times */
576         case LUSTRE_Q_GETINFOPOOL:
577                 /* Global grace time is stored in quota settings of ID 0. */
578                 id->qid_uid = 0;
579
580                 /* read inode grace time */
581                 rc = qmt_get(env, qmt, LQUOTA_RES_MD, oqctl->qc_type, id, NULL,
582                              NULL, &oqctl->qc_dqinfo.dqi_igrace,
583                              false, poolname);
584                 /* There could be no MD pool, so try to find DT pool */
585                 if (rc && rc != -ENOENT)
586                         break;
587
588                 /* read block grace time */
589                 rc = qmt_get(env, qmt, LQUOTA_RES_DT, oqctl->qc_type, id, NULL,
590                              NULL, &oqctl->qc_dqinfo.dqi_bgrace,
591                              false, poolname);
592                 break;
593
594         case Q_SETINFO:  /* modify grace times */
595         case LUSTRE_Q_SETINFOPOOL:
596                 /* setinfo should be using dqi->dqi_valid, but lfs incorrectly
597                  * sets the valid flags in dqb->dqb_valid instead, try to live
598                  * with that ... */
599
600                 /* Global grace time is stored in quota settings of ID 0. */
601                 id->qid_uid = 0;
602
603                 if ((dqb->dqb_valid & QIF_ITIME) != 0) {
604                         /* set inode grace time */
605                         rc = qmt_set(env, qmt, LQUOTA_RES_MD, oqctl->qc_type,
606                                      id, 0, 0, oqctl->qc_dqinfo.dqi_igrace,
607                                      QIF_TIMES, false, false,
608                                      poolname);
609                         if (rc)
610                                 break;
611                 }
612
613                 if ((dqb->dqb_valid & QIF_BTIME) != 0)
614                         /* set block grace time */
615                         rc = qmt_set(env, qmt, LQUOTA_RES_DT, oqctl->qc_type,
616                                      id, 0, 0, oqctl->qc_dqinfo.dqi_bgrace,
617                                      QIF_TIMES, false, false,
618                                      poolname);
619                 break;
620
621         case LUSTRE_Q_GETDEFAULT:
622         case LUSTRE_Q_GETDEFAULT_POOL:
623                 is_default = true;
624                 fallthrough;
625
626         case Q_GETQUOTA: /* consult quota limit */
627         case LUSTRE_Q_GETQUOTAPOOL:
628                 /* extract quota ID from quotactl request */
629                 id->qid_uid = oqctl->qc_id;
630
631                 /* look-up inode quota settings */
632                 rc = qmt_get(env, qmt, LQUOTA_RES_MD, oqctl->qc_type, id,
633                              &dqb->dqb_ihardlimit, &dqb->dqb_isoftlimit,
634                              &dqb->dqb_itime, is_default, poolname);
635                 /* There could be no MD pool, so try to find DT pool */
636                 if (rc && rc != -ENOENT)
637                         break;
638                 else
639                         dqb->dqb_valid |= QIF_ILIMITS | QIF_ITIME;
640
641                 /* master isn't aware of actual inode usage */
642                 dqb->dqb_curinodes = 0;
643
644                 /* look-up block quota settings */
645                 rc = qmt_get(env, qmt, LQUOTA_RES_DT, oqctl->qc_type, id,
646                              &dqb->dqb_bhardlimit, &dqb->dqb_bsoftlimit,
647                              &dqb->dqb_btime, is_default, poolname);
648                 if (rc)
649                         break;
650
651                 dqb->dqb_valid |= QIF_BLIMITS | QIF_BTIME;
652                 /* master doesn't know the actual block usage */
653                 dqb->dqb_curspace = 0;
654                 break;
655
656         case LUSTRE_Q_SETDEFAULT:
657         case LUSTRE_Q_SETDEFAULT_POOL:
658                 is_default = true;
659                 fallthrough;
660
661         case Q_SETQUOTA: /* change quota limits */
662         case LUSTRE_Q_SETQUOTAPOOL:
663                 /* extract quota ID from quotactl request */
664                 id->qid_uid = oqctl->qc_id;
665
666                 if ((dqb->dqb_valid & QIF_IFLAGS) != 0) {
667                         /* update inode quota settings */
668                         rc = qmt_set(env, qmt, LQUOTA_RES_MD, oqctl->qc_type,
669                                      id, dqb->dqb_ihardlimit,
670                                      dqb->dqb_isoftlimit, dqb->dqb_itime,
671                                      dqb->dqb_valid & QIF_IFLAGS, is_default,
672                                      false, poolname);
673                         if (rc)
674                                 break;
675                 }
676
677                 if ((dqb->dqb_valid & QIF_BFLAGS) != 0)
678                         /* update block quota settings */
679                         rc = qmt_set(env, qmt, LQUOTA_RES_DT, oqctl->qc_type,
680                                      id, dqb->dqb_bhardlimit,
681                                      dqb->dqb_bsoftlimit, dqb->dqb_btime,
682                                      dqb->dqb_valid & QIF_BFLAGS, is_default,
683                                      false, poolname);
684                 break;
685
686         case LUSTRE_Q_DELETEQID:
687                 rc = qmt_delete_qid(env, qmt, LQUOTA_RES_MD, oqctl->qc_type,
688                                     oqctl->qc_id);
689                 if (rc)
690                         break;
691
692                 rc = qmt_delete_qid(env, qmt, LQUOTA_RES_DT, oqctl->qc_type,
693                                     oqctl->qc_id);
694                 break;
695
696         case LUSTRE_Q_RESETQID:
697                 if (oqctl->qc_id == 0)
698                         RETURN(-EINVAL);
699
700                 id->qid_uid = oqctl->qc_id;
701                 /* save the quota setting before resetting */
702                 rc = qmt_get(env, qmt, LQUOTA_RES_MD, oqctl->qc_type, id,
703                              &dqb->dqb_ihardlimit, &dqb->dqb_isoftlimit,
704                              &dqb->dqb_itime, false, NULL);
705                 if (rc)
706                         break;
707                 else
708                         dqb->dqb_valid |= QIF_ILIMITS | QIF_ITIME;
709
710                 rc = qmt_get(env, qmt, LQUOTA_RES_DT, oqctl->qc_type, id,
711                              &dqb->dqb_bhardlimit, &dqb->dqb_bsoftlimit,
712                              &dqb->dqb_btime, false, NULL);
713                 if (rc)
714                         break;
715
716                 dqb->dqb_valid |= QIF_BLIMITS | QIF_BTIME;
717                 dqb->dqb_curinodes = 0;
718                 dqb->dqb_curspace = 0;
719
720                 /* reset the corresponding quota ID */
721                 rc = qmt_reset_qid(env, qmt, LQUOTA_RES_MD, oqctl->qc_type,
722                                    oqctl->qc_id);
723                 if (rc)
724                         break;
725
726                 rc = qmt_reset_qid(env, qmt, LQUOTA_RES_DT, oqctl->qc_type,
727                                    oqctl->qc_id);
728                 break;
729
730
731         default:
732                 CERROR("%s: unsupported quotactl command: %d\n",
733                        qmt->qmt_svname, oqctl->qc_cmd);
734                 RETURN(-ENOTSUPP);
735         }
736
737         RETURN(rc);
738 }
739
740 static inline
741 void qmt_grant_lqes(const struct lu_env *env, __u64 *slv, __u64 cnt)
742 {
743         int i;
744
745         for (i = 0; i < qti_lqes_cnt(env); i++)
746                 qti_lqe_granted(env, i) += cnt;
747
748         *slv += cnt;
749 }
750
751 static inline bool qmt_lqes_can_rel(const struct lu_env *env, __u64 cnt)
752 {
753         bool can_release = true;
754         int i;
755
756         for (i = 0; i < qti_lqes_cnt(env); i++) {
757                 if (cnt > qti_lqe_granted(env, i)) {
758                         LQUOTA_ERROR(qti_lqes(env)[i],
759                                      "Can't release %llu that is larger than lqe_granted.\n",
760                                      cnt);
761                         can_release = false;
762                 }
763         }
764         return can_release;
765 }
766
767 static inline void qmt_rel_lqes(const struct lu_env *env, __u64 *slv, __u64 cnt,
768                                 bool reset)
769 {
770         int i;
771
772         for (i = 0; i < qti_lqes_cnt(env); i++) {
773                 if (reset)
774                         qti_lqe_granted(env, i) = 0;
775                 else
776                         qti_lqe_granted(env, i) -= cnt;
777         }
778
779         if (reset)
780                 *slv = 0;
781         else
782                 *slv -= cnt;
783 }
784
785 static inline bool qmt_lqes_cannot_grant(const struct lu_env *env, __u64 cnt)
786 {
787         bool cannot_grant = false;
788         int i;
789
790         for (i = 0; i < qti_lqes_cnt(env); i++) {
791                 if (qti_lqe_hard(env, i) != 0 &&
792                     qti_lqe_granted(env, i) + cnt > qti_lqe_hard(env, i)) {
793                         cannot_grant = true;
794                         break;
795                 }
796         }
797         return cannot_grant;
798 }
799
800 static inline __u64 qmt_lqes_grant_some_quota(const struct lu_env *env)
801 {
802         __u64 min_count, tmp;
803         bool flag = false;
804         int i;
805
806         for (i = 0, min_count = 0; i < qti_lqes_cnt(env); i++) {
807                 if (!qti_lqes(env)[i]->lqe_enforced &&
808                     !qti_lqes(env)[i]->lqe_is_global)
809                         continue;
810
811                 tmp = qti_lqe_hard(env, i) - qti_lqe_granted(env, i);
812                 if (flag) {
813                         min_count = tmp < min_count ? tmp : min_count;
814                 } else {
815                         flag = true;
816                         min_count = tmp;
817                 }
818         }
819         return min_count;
820 }
821
822 static inline __u64 qmt_lqes_alloc_expand(const struct lu_env *env,
823                                           __u64 slv_granted, __u64 spare)
824 {
825         __u64 min_count, tmp;
826         bool flag = false;
827         int i;
828
829         for (i = 0, min_count = 0; i < qti_lqes_cnt(env); i++) {
830                 /* Don't take into account not enforced lqes that belong
831                  * to non global pool. These lqes present in array to
832                  * support actual lqe_granted even for lqes without limits. */
833                 if (!qti_lqes(env)[i]->lqe_enforced &&
834                     !qti_lqes(env)[i]->lqe_is_global)
835                         continue;
836
837                 tmp = qmt_alloc_expand(qti_lqes(env)[i], slv_granted, spare);
838                 if (flag) {
839                         min_count = tmp < min_count ? tmp : min_count;
840                 } else {
841                         flag = true;
842                         min_count = tmp;
843                 }
844         }
845         return min_count;
846 }
847
848 static inline void qmt_lqes_tune_grace(const struct lu_env *env, __u64 now)
849 {
850         int i;
851
852         for (i = 0; i < qti_lqes_cnt(env); i++) {
853                 struct lquota_entry *lqe;
854
855                 lqe = qti_lqes(env)[i];
856                 if (lqe->lqe_softlimit != 0) {
857                         if (lqe->lqe_granted > lqe->lqe_softlimit &&
858                             lqe->lqe_gracetime == 0) {
859                                 /* First time over soft limit, let's start grace
860                                  * timer */
861                                 lqe->lqe_gracetime = now + qmt_lqe_grace(lqe);
862                         } else if (lqe->lqe_granted <= lqe->lqe_softlimit &&
863                                    lqe->lqe_gracetime != 0) {
864                                 /* Clear grace timer */
865                                 lqe->lqe_gracetime = 0;
866                         }
867                 }
868         }
869 }
870
871 /*
872  * Helper function to handle quota request from slave.
873  *
874  * \param env     - is the environment passed by the caller
875  * \param qmt     - is the master device
876  * \param uuid    - is the uuid associated with the slave
877  * \param qb_flags - are the quota request flags as packed in the quota_body
878  * \param qb_count - is the amount of quota space the slave wants to
879  *                   acquire/release
880  * \param qb_usage - is the current space usage on the slave
881  * \param repbody - is the quota_body of reply
882  *
883  * \retval 0            : success
884  * \retval -EDQUOT      : out of quota
885  *         -EINPROGRESS : inform client to retry write/create
886  *         -ve          : other appropriate errors
887  */
888 int qmt_dqacq0(const struct lu_env *env, struct qmt_device *qmt,
889                struct obd_uuid *uuid, __u32 qb_flags, __u64 qb_count,
890                __u64 qb_usage, struct quota_body *repbody)
891 {
892         __u64                    now, count = 0;
893         struct dt_object        *slv_obj = NULL;
894         __u64                    slv_granted, slv_granted_bck;
895         struct thandle          *th = NULL;
896         int                      rc, ret;
897         struct lquota_entry *lqe = qti_lqes_glbl(env);
898         ENTRY;
899
900         LASSERT(uuid != NULL);
901
902         /* initialize reply */
903         memset(repbody, 0, sizeof(*repbody));
904         memcpy(&repbody->qb_id, &lqe->lqe_id, sizeof(repbody->qb_id));
905
906         if (OBD_FAIL_CHECK(OBD_FAIL_QUOTA_RECOVERABLE_ERR))
907                 RETURN(-cfs_fail_val);
908
909         if (OBD_FAIL_CHECK(OBD_FAIL_QUOTA_PREACQ) &&
910            (req_is_preacq(qb_flags) || req_is_rel(qb_flags)))
911                 RETURN(-EAGAIN);
912
913         if (qti_lqes_restore_init(env))
914                 RETURN(-ENOMEM);
915
916         /* look-up index file associated with acquiring slave */
917         slv_obj = lquota_disk_slv_find(env, qmt->qmt_child, LQE_ROOT(lqe),
918                                        lu_object_fid(&LQE_GLB_OBJ(lqe)->do_lu),
919                                        uuid);
920         if (IS_ERR(slv_obj))
921                 GOTO(out, rc = PTR_ERR(slv_obj));
922
923         /* pack slave fid in reply just for sanity check */
924         memcpy(&repbody->qb_slv_fid, lu_object_fid(&slv_obj->do_lu),
925                sizeof(struct lu_fid));
926
927         /* allocate & start transaction with enough credits to update
928          * global & slave indexes */
929         th = qmt_trans_start_with_slv(env, NULL, slv_obj, false);
930         if (IS_ERR(th))
931                 GOTO(out, rc = PTR_ERR(th));
932
933         qti_lqes_write_lock(env);
934
935         LQUOTA_DEBUG_LQES(env, "dqacq starts uuid:%s flags:0x%x wanted:%llu"
936                      " usage:%llu", obd_uuid2str(uuid), qb_flags, qb_count,
937                      qb_usage);
938
939         /* Legal race, limits have been removed on master, but slave didn't
940          * receive the change yet. Just return EINPROGRESS until the slave gets
941          * notified. */
942         if (!lqe->lqe_enforced && !req_is_rel(qb_flags))
943                 GOTO(out_locked, rc = -ESRCH);
944
945         /* recompute qunit in case it was never initialized */
946         qmt_revalidate_lqes(env, qmt, qb_flags);
947
948         /* slave just wants to acquire per-ID lock */
949         if (req_is_acq(qb_flags) && qb_count == 0)
950                 GOTO(out_locked, rc = 0);
951
952         if (lqe->lqe_is_reset) {
953                 lqe->lqe_granted = 0;
954                 repbody->qb_count = qb_count;
955                 qmt_rel_lqes(env, &slv_granted, qb_count, lqe->lqe_is_reset);
956                 GOTO(out_locked, rc = 0);
957         }
958
959         /* fetch how much quota space is already granted to this slave */
960         rc = qmt_slv_read(env, &lqe->lqe_id, slv_obj, &slv_granted);
961         if (rc) {
962                 LQUOTA_ERROR(lqe, "Failed to get granted for slave %s, rc=%d",
963                              obd_uuid2str(uuid), rc);
964                 GOTO(out_locked, rc);
965         }
966         /* recall how much space this slave currently owns in order to restore
967          * it in case of failure */
968         slv_granted_bck = slv_granted;
969
970         /* record current time for soft limit & grace time management */
971         now = ktime_get_real_seconds();
972
973         if (req_is_rel(qb_flags)) {
974                 /* Slave would like to release quota space */
975                 if (slv_granted < qb_count ||
976                     !qmt_lqes_can_rel(env, qb_count)) {
977                         /* can't release more than granted */
978                         LQUOTA_ERROR_LQES(env,
979                                           "Release too much! uuid:%s release: %llu granted:%llu, total:%llu",
980                                           obd_uuid2str(uuid), qb_count,
981                                           slv_granted, lqe->lqe_granted);
982                         GOTO(out_locked, rc = -EINVAL);
983                 }
984
985                 repbody->qb_count = qb_count;
986                 /* put released space back to global pool */
987                 qmt_rel_lqes(env, &slv_granted, qb_count, lqe->lqe_is_reset);
988                 GOTO(out_write, rc = 0);
989         }
990
991         if (req_has_rep(qb_flags) && slv_granted < qb_usage) {
992                 /* Slave is reporting space usage in quota request and it turns
993                  * out to be using more quota space than owned, so we adjust
994                  * granted space regardless of the current state of affairs */
995                 repbody->qb_count = qb_usage - slv_granted;
996                 qmt_grant_lqes(env, &slv_granted, repbody->qb_count);
997         }
998
999         if (!req_is_acq(qb_flags) && !req_is_preacq(qb_flags))
1000                 GOTO(out_write, rc = 0);
1001
1002         qmt_adjust_edquot_notify(env, qmt, now, qb_flags);
1003         if (qti_lqes_edquot(env))
1004                 /* no hope to claim further space back */
1005                 GOTO(out_write, rc = -EDQUOT);
1006
1007         if (qmt_space_exhausted_lqes(env, now)) {
1008                 /* might have some free space once rebalancing is completed */
1009                 rc = req_is_acq(qb_flags) ? -EINPROGRESS : -EDQUOT;
1010                 GOTO(out_write, rc);
1011         }
1012
1013         if (req_is_preacq(qb_flags)) {
1014                 /* slave would like to pre-acquire quota space. To do so, it
1015                  * reports in qb_count how much spare quota space it owns and we
1016                  * can grant back quota space which is consistent with qunit
1017                  * value. */
1018                 if (qb_count >= qti_lqes_min_qunit(env))
1019                         /* slave already own the maximum it should */
1020                         GOTO(out_write, rc = 0);
1021
1022                 count = qmt_lqes_alloc_expand(env, slv_granted, qb_count);
1023                 if (count == 0)
1024                         GOTO(out_write, rc = -EDQUOT);
1025
1026                 repbody->qb_count += count;
1027                 qmt_grant_lqes(env, &slv_granted, count);
1028                 GOTO(out_write, rc = 0);
1029         }
1030
1031         /* processing acquire request with clients waiting */
1032         if (qmt_lqes_cannot_grant(env, qb_count)) {
1033                 /* cannot grant as much as asked, but can still afford to grant
1034                  * some quota space back */
1035                 count = qmt_lqes_grant_some_quota(env);
1036                 repbody->qb_count += count;
1037                 qmt_grant_lqes(env, &slv_granted, count);
1038                 GOTO(out_write, rc = 0);
1039         }
1040
1041         /* Whouhou! we can satisfy the slave request! */
1042         repbody->qb_count += qb_count;
1043         qmt_grant_lqes(env, &slv_granted, qb_count);
1044
1045         /* Try to expand the acquired count for DQACQ */
1046         count = qmt_lqes_alloc_expand(env, slv_granted, 0);
1047         if (count != 0) {
1048                 /* can even grant more than asked, it is like xmas ... */
1049                 repbody->qb_count += count;
1050                 qmt_grant_lqes(env, &slv_granted, count);
1051                 GOTO(out_write, rc = 0);
1052         }
1053
1054         GOTO(out_write, rc = 0);
1055 out_write:
1056         if (repbody->qb_count == 0)
1057                 GOTO(out_locked, rc);
1058
1059         /* start/stop grace timer if required */
1060         qmt_lqes_tune_grace(env, now);
1061
1062         if (OBD_FAIL_CHECK(OBD_FAIL_QUOTA_GRANT))
1063                 slv_granted = 0xFFFFFFFFFFF00000;
1064
1065         /* Update slave index first since it is easier to roll back */
1066         ret = qmt_slv_write(env, th, lqe, slv_obj, LQUOTA_BUMP_VER,
1067                             &repbody->qb_slv_ver, slv_granted);
1068         if (ret) {
1069                 /* restore initial quota settings */
1070                 qmt_restore_lqes(env);
1071                 /* reset qb_count */
1072                 repbody->qb_count = 0;
1073                 GOTO(out_locked, rc = ret);
1074         }
1075
1076         /* Update global index, no version bump needed */
1077         ret = qmt_glb_write_lqes(env, th, 0, NULL);
1078         if (ret) {
1079                 rc = ret;
1080                 /* restore initial quota settings */
1081                 qmt_restore_lqes(env);
1082                 /* reset qb_count */
1083                 repbody->qb_count = 0;
1084
1085                 /* restore previous granted value */
1086                 ret = qmt_slv_write(env, th, lqe, slv_obj, 0, NULL,
1087                                     slv_granted_bck);
1088                 if (ret) {
1089                         LQUOTA_ERROR(lqe, "failed to restore initial slave "
1090                                      "value rc:%d ret%d", rc, ret);
1091                         LBUG();
1092                 }
1093                 qmt_adjust_edquot_notify(env, qmt, now, qb_flags);
1094                 GOTO(out_locked, rc);
1095         }
1096
1097         /* Total granted has been changed, let's try to adjust the qunit
1098          * size according to the total granted & limits. */
1099
1100         /* clear/set edquot flag and notify slaves via glimpse if needed */
1101         qmt_adjust_and_notify(env, qmt, now, qb_flags);
1102 out_locked:
1103         LQUOTA_DEBUG_LQES(env, "dqacq ends count:%llu ver:%llu rc:%d",
1104                      repbody->qb_count, repbody->qb_slv_ver, rc);
1105         qti_lqes_write_unlock(env);
1106 out:
1107         qti_lqes_restore_fini(env);
1108
1109         if (th != NULL && !IS_ERR(th))
1110                 dt_trans_stop(env, qmt->qmt_child, th);
1111
1112         if (slv_obj != NULL && !IS_ERR(slv_obj))
1113                 dt_object_put(env, slv_obj);
1114
1115         if ((req_is_acq(qb_flags) || req_is_preacq(qb_flags)) &&
1116             OBD_FAIL_CHECK(OBD_FAIL_QUOTA_EDQUOT)) {
1117                 /* introduce inconsistency between granted value in slave index
1118                  * and slave index copy of slave */
1119                 repbody->qb_count = 0;
1120                 rc = -EDQUOT;
1121         }
1122
1123         RETURN(rc);
1124 }
1125
1126 /*
1127  * Extract index from uuid or quota index file name.
1128  *
1129  * \param[in] uuid      uuid or quota index name(0x1020000-OST0001_UUID)
1130  * \param[out] idx      pointer to save index
1131  *
1132  * \retval              slave type(QMT_STYPE_MDT or QMT_STYPE_OST)
1133  * \retval -EINVAL      wrong uuid
1134  */
1135 int qmt_uuid2idx(struct obd_uuid *uuid, int *idx)
1136 {
1137         char *uuid_str, *name, *dash;
1138         int rc = -EINVAL;
1139
1140         uuid_str = (char *)uuid->uuid;
1141
1142         if (strnlen(uuid_str, UUID_MAX) >= UUID_MAX) {
1143                 CERROR("quota: UUID '%.*s' missing trailing NUL: rc = %d\n",
1144                        UUID_MAX, uuid_str, rc);
1145                 return rc;
1146         }
1147
1148         dash = strrchr(uuid_str, '-');
1149         name = dash + 1;
1150         /* Going to get index from MDTXXXX/OSTXXXX. Thus uuid should
1151          * have at least 8 bytes after '-': 3 for MDT/OST, 4 for index
1152          * and 1 byte for null character. */
1153         if (*dash != '-' || ((uuid_str + UUID_MAX - name) < 8)) {
1154                 CERROR("quota: wrong UUID format '%s': rc = %d\n",
1155                        uuid_str, rc);
1156                 return rc;
1157         }
1158
1159         rc = target_name2index(name, idx, NULL);
1160         switch (rc) {
1161         case LDD_F_SV_TYPE_MDT:
1162                 rc = QMT_STYPE_MDT;
1163                 break;
1164         case LDD_F_SV_TYPE_OST:
1165                 rc = QMT_STYPE_OST;
1166                 break;
1167         default:
1168                 CERROR("quota: wrong UUID type '%s': rc = %d\n", uuid_str, rc);
1169                 rc = -EINVAL;
1170         }
1171
1172         RETURN(rc);
1173 }
1174
1175 /*
1176  * Handle quota request from slave.
1177  *
1178  * \param env  - is the environment passed by the caller
1179  * \param ld   - is the lu device associated with the qmt
1180  * \param req  - is the quota acquire request
1181  */
1182 static int qmt_dqacq(const struct lu_env *env, struct lu_device *ld,
1183                      struct ptlrpc_request *req)
1184 {
1185         struct qmt_device *qmt = lu2qmt_dev(ld);
1186         struct quota_body *qbody, *repbody;
1187         struct obd_uuid *uuid;
1188         struct ldlm_lock *lock;
1189         int rtype, qtype;
1190         int rc, idx, stype;
1191         ENTRY;
1192
1193         qbody = req_capsule_client_get(&req->rq_pill, &RMF_QUOTA_BODY);
1194         if (qbody == NULL)
1195                 RETURN(err_serious(-EPROTO));
1196
1197         repbody = req_capsule_server_get(&req->rq_pill, &RMF_QUOTA_BODY);
1198         if (repbody == NULL)
1199                 RETURN(err_serious(-EFAULT));
1200
1201         /* verify if global lock is stale */
1202         if (!lustre_handle_is_used(&qbody->qb_glb_lockh))
1203                 RETURN(-ENOLCK);
1204
1205         lock = ldlm_handle2lock(&qbody->qb_glb_lockh);
1206         if (lock == NULL)
1207                 RETURN(-ENOLCK);
1208         LDLM_LOCK_PUT(lock);
1209
1210         uuid = &req->rq_export->exp_client_uuid;
1211         stype = qmt_uuid2idx(uuid, &idx);
1212         if (stype < 0)
1213                 RETURN(stype);
1214
1215         if (req_is_rel(qbody->qb_flags) + req_is_acq(qbody->qb_flags) +
1216             req_is_preacq(qbody->qb_flags) > 1) {
1217                 CERROR("%s: malformed quota request with conflicting flags set "
1218                        "(%x) from slave %s\n", qmt->qmt_svname,
1219                        qbody->qb_flags, obd_uuid2str(uuid));
1220                 RETURN(-EPROTO);
1221         }
1222
1223         if (req_is_acq(qbody->qb_flags) || req_is_preacq(qbody->qb_flags)) {
1224                 /* acquire and pre-acquire should use a valid ID lock */
1225
1226                 if (!lustre_handle_is_used(&qbody->qb_lockh))
1227                         RETURN(-ENOLCK);
1228
1229                 lock = ldlm_handle2lock(&qbody->qb_lockh);
1230                 if (lock == NULL)
1231                         /* no lock associated with this handle */
1232                         RETURN(-ENOLCK);
1233
1234                 LDLM_DEBUG(lock, "%sacquire request",
1235                            req_is_preacq(qbody->qb_flags) ? "pre" : "");
1236
1237                 if (!obd_uuid_equals(&lock->l_export->exp_client_uuid, uuid)) {
1238                         /* sorry, no way to cheat ... */
1239                         LDLM_LOCK_PUT(lock);
1240                         RETURN(-ENOLCK);
1241                 }
1242
1243                 if (ldlm_is_ast_sent(lock)) {
1244                         struct ptlrpc_service_part *svc;
1245                         timeout_t timeout;
1246
1247                         svc = req->rq_rqbd->rqbd_svcpt;
1248                         timeout = at_est2timeout(at_get(&svc->scp_at_estimate));
1249                         timeout += (ldlm_bl_timeout(lock) >> 1);
1250
1251                         /* lock is being cancelled, prolong timeout */
1252                         ldlm_refresh_waiting_lock(lock, timeout);
1253                 }
1254                 LDLM_LOCK_PUT(lock);
1255         }
1256
1257         /* extract quota information from global index FID packed in the
1258          * request */
1259         rc = lquota_extract_fid(&qbody->qb_fid, &rtype, &qtype);
1260         if (rc)
1261                 RETURN(-EINVAL);
1262
1263         /* Find the quota entry associated with the quota id */
1264         rc = qmt_pool_lqes_lookup(env, qmt, rtype, stype, qtype,
1265                                   &qbody->qb_id, NULL, idx);
1266         if (rc)
1267                 RETURN(rc);
1268
1269         rc = qmt_dqacq0(env, qmt, uuid, qbody->qb_flags,
1270                         qbody->qb_count, qbody->qb_usage, repbody);
1271
1272         if (lustre_handle_is_used(&qbody->qb_lockh))
1273                 /* return current qunit value only to slaves owning an per-ID
1274                  * quota lock. For enqueue, the qunit value will be returned in
1275                  * the LVB */
1276                 repbody->qb_qunit = qti_lqes_min_qunit(env);
1277         CDEBUG(D_QUOTA, "qmt_dqacq return qb_qunit %llu qb_count %llu\n",
1278                repbody->qb_qunit, repbody->qb_count);
1279         qti_lqes_fini(env);
1280         RETURN(rc);
1281 }
1282
1283 /* Vector of quota request handlers. This vector is used by the MDT to forward
1284  * requests to the quota master. */
1285 struct qmt_handlers qmt_hdls = {
1286         /* quota request handlers */
1287         .qmth_quotactl          = qmt_quotactl,
1288         .qmth_dqacq             = qmt_dqacq,
1289
1290         /* ldlm handlers */
1291         .qmth_intent_policy     = qmt_intent_policy,
1292         .qmth_lvbo_init         = qmt_lvbo_init,
1293         .qmth_lvbo_update       = qmt_lvbo_update,
1294         .qmth_lvbo_size         = qmt_lvbo_size,
1295         .qmth_lvbo_fill         = qmt_lvbo_fill,
1296         .qmth_lvbo_free         = qmt_lvbo_free,
1297 };
1298 EXPORT_SYMBOL(qmt_hdls);