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