Whamcloud - gitweb
LU-9679 general: add missing spaces near punctuation
[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 {
54         struct lquota_entry     *lqe;
55         ENTRY;
56
57         LASSERT(!is_default || id->qid_uid == 0);
58
59         /* look-up lqe structure containing quota settings */
60         lqe = qmt_pool_lqe_lookup(env, qmt, restype, qtype, id);
61         if (IS_ERR(lqe))
62                 RETURN(PTR_ERR(lqe));
63
64         /* copy quota settings */
65         lqe_read_lock(lqe);
66         LQUOTA_DEBUG(lqe, "fetch settings");
67         if (hard != NULL)
68                 *hard = lqe->lqe_hardlimit;
69         if (soft != NULL)
70                 *soft = lqe->lqe_softlimit;
71         if (time != NULL) {
72                 *time = lqe->lqe_gracetime;
73                 if (lqe->lqe_is_default)
74                         *time |= (__u64)LQUOTA_FLAG_DEFAULT <<
75                                                         LQUOTA_GRACE_BITS;
76         }
77         lqe_read_unlock(lqe);
78
79         lqe_putref(lqe);
80         RETURN(0);
81 }
82
83 struct qmt_entry_iter_data {
84         const struct lu_env *qeid_env;
85         struct qmt_device   *qeid_qmt;
86 };
87
88 static int qmt_entry_iter_cb(struct cfs_hash *hs, struct cfs_hash_bd *bd,
89                              struct hlist_node *hnode, void *d)
90 {
91         struct qmt_entry_iter_data *iter = (struct qmt_entry_iter_data *)d;
92         struct lquota_entry     *lqe;
93
94         lqe = hlist_entry(hnode, struct lquota_entry, lqe_hash);
95         LASSERT(atomic_read(&lqe->lqe_ref) > 0);
96
97         if (lqe->lqe_id.qid_uid == 0 || !lqe->lqe_is_default)
98                 return 0;
99
100         return qmt_set_with_lqe(iter->qeid_env, iter->qeid_qmt, lqe, 0, 0, 0, 0,
101                                 true, true);
102 }
103
104 /*
105  * Update quota settings for a given lqe.
106  *
107  * \param env        - is the environment passed by the caller
108  * \param qmt        - is the quota master target
109  * \param lqe        - is the lquota_entry for which we want to modify quota
110  *                     settings.
111  * \param hard       - is the new hard limit
112  * \param soft       - is the new soft limit
113  * \param time       - is the new grace time
114  * \param valid      - is the list of settings to change
115  * \param is_default - true for default quota setting
116  * \param is_updated - true if the lqe is updated and no need to write back
117  */
118
119 int qmt_set_with_lqe(const struct lu_env *env, struct qmt_device *qmt,
120                      struct lquota_entry *lqe, __u64 hard, __u64 soft,
121                      __u64 time, __u32 valid, bool is_default, bool is_updated)
122 {
123         struct qmt_thread_info  *qti = qmt_info(env);
124         struct thandle          *th = NULL;
125         time64_t now;
126         __u64                    ver;
127         bool                     dirtied = false;
128         int                      rc = 0;
129         ENTRY;
130
131         /* need to write back to global quota file? */
132         if (!is_updated) {
133                 /* allocate & start transaction with enough credits to update
134                  * quota  settings in the global index file */
135                 th = qmt_trans_start(env, lqe, &qti->qti_restore);
136                 if (IS_ERR(th))
137                         GOTO(out_nolock, rc = PTR_ERR(th));
138         }
139
140         now = ktime_get_real_seconds();
141
142         lqe_write_lock(lqe);
143         LQUOTA_DEBUG(lqe,
144                      "changing quota settings valid:%x hard:%llu soft:%llu time:%llu",
145                      valid, hard, soft, time);
146
147         if (is_default && lqe->lqe_id.qid_uid != 0) {
148                 LQUOTA_DEBUG(lqe, "set qid %llu to use default quota setting",
149                              lqe->lqe_id.qid_uid);
150
151                 qmt_lqe_set_default(env, lqe->lqe_site->lqs_parent, lqe, false);
152                 GOTO(quota_set, 0);
153         }
154
155         if ((valid & QIF_TIMES) != 0 && lqe->lqe_gracetime != time) {
156                 /* change time settings */
157                 lqe->lqe_gracetime = time;
158                 dirtied            = true;
159         }
160
161         if ((valid & QIF_LIMITS) != 0 &&
162             (lqe->lqe_hardlimit != hard || lqe->lqe_softlimit != soft)) {
163                 rc = qmt_validate_limits(lqe, hard, soft);
164                 if (rc)
165                         GOTO(out, rc);
166
167                 /* change quota limits */
168                 lqe->lqe_hardlimit = hard;
169                 lqe->lqe_softlimit = soft;
170
171 quota_set:
172                 /* recompute qunit in case it was never initialized */
173                 qmt_revalidate(env, lqe);
174
175                 /* clear grace time */
176                 if (lqe->lqe_softlimit == 0 ||
177                     lqe->lqe_granted <= lqe->lqe_softlimit)
178                         /* no soft limit or below soft limit, let's clear grace
179                          * time */
180                         lqe->lqe_gracetime = 0;
181                 else if ((valid & QIF_TIMES) == 0)
182                         /* set grace only if user hasn't provided his own */
183                          lqe->lqe_gracetime = now + qmt_lqe_grace(lqe);
184
185                 /* change enforced status based on new parameters */
186                 if (lqe->lqe_id.qid_uid == 0 || (lqe->lqe_hardlimit == 0 &&
187                     lqe->lqe_softlimit == 0))
188                         lqe->lqe_enforced = false;
189                 else
190                         lqe->lqe_enforced = true;
191
192                 dirtied = true;
193         }
194
195         if (!is_default && lqe->lqe_is_default) {
196                 LQUOTA_DEBUG(lqe, "the qid %llu has been set quota"
197                              " explicitly, clear the default flag",
198                              lqe->lqe_id.qid_uid);
199
200                 qmt_lqe_clear_default(lqe);
201                 dirtied = true;
202         }
203
204         if (dirtied) {
205                 if (!is_updated) {
206                         /* write new quota settings to disk */
207                         rc = qmt_glb_write(env, th, lqe, LQUOTA_BUMP_VER, &ver);
208                         if (rc) {
209                                 /* restore initial quota settings */
210                                 qmt_restore(lqe, &qti->qti_restore);
211                                 GOTO(out, rc);
212                         }
213                 } else {
214                         ver = dt_version_get(env, LQE_GLB_OBJ(lqe));
215                 }
216
217                 /* compute new qunit value now that we have modified the quota
218                  * settings */
219                 qmt_adjust_qunit(env, lqe);
220
221                 /* clear/set edquot flag as needed */
222                 qmt_adjust_edquot(lqe, now);
223         }
224         EXIT;
225 out:
226         lqe_write_unlock(lqe);
227
228 out_nolock:
229         if (th != NULL && !IS_ERR(th))
230                 dt_trans_stop(env, qmt->qmt_child, th);
231
232         if (rc == 0 && dirtied) {
233                 qmt_glb_lock_notify(env, lqe, ver);
234                 if (lqe->lqe_id.qid_uid == 0) {
235                         struct qmt_entry_iter_data iter_data;
236
237                         LQUOTA_DEBUG(lqe, "notify all lqe with default quota");
238                         iter_data.qeid_env = env;
239                         iter_data.qeid_qmt = qmt;
240                         cfs_hash_for_each_safe(lqe->lqe_site->lqs_hash,
241                                                qmt_entry_iter_cb, &iter_data);
242                 }
243         }
244
245         return rc;
246 }
247
248 /*
249  * Update quota settings for a given identifier.
250  *
251  * \param env        - is the environment passed by the caller
252  * \param qmt        - is the quota master target
253  * \param restype    - is the pool type, either block (i.e. LQUOTA_RES_DT) or
254  *                     inode (i.e. LQUOTA_RES_MD)
255  * \param qtype      - is the quota type
256  * \param id         - is the quota indentifier for which we want to modify
257  *                     quota settings.
258  * \param hard       - is the new hard limit
259  * \param soft       - is the new soft limit
260  * \param time       - is the new grace time
261  * \param valid      - is the list of settings to change
262  * \param is_default - true for default quota setting
263  * \param is_updated - true if the lqe is updated and no need to write back
264  */
265 static int qmt_set(const struct lu_env *env, struct qmt_device *qmt,
266                    __u8 restype, __u8 qtype, union lquota_id *id,
267                    __u64 hard, __u64 soft, __u64 time, __u32 valid,
268                    bool is_default, bool is_updated)
269 {
270         struct lquota_entry *lqe;
271         int rc;
272         ENTRY;
273
274         /* look-up quota entry associated with this ID */
275         lqe = qmt_pool_lqe_lookup(env, qmt, restype, qtype, id);
276         if (IS_ERR(lqe))
277                         RETURN(PTR_ERR(lqe));
278
279         rc = qmt_set_with_lqe(env, qmt, lqe, hard, soft, time, valid,
280                               is_default, is_updated);
281
282         lqe_putref(lqe);
283         RETURN(rc);
284 }
285
286 /*
287  * Handle quotactl request.
288  *
289  * \param env   - is the environment passed by the caller
290  * \param ld    - is the lu device associated with the qmt
291  * \param oqctl - is the quotactl request
292  */
293 static int qmt_quotactl(const struct lu_env *env, struct lu_device *ld,
294                         struct obd_quotactl *oqctl)
295 {
296         struct qmt_thread_info  *qti = qmt_info(env);
297         union lquota_id         *id  = &qti->qti_id;
298         struct qmt_device       *qmt = lu2qmt_dev(ld);
299         struct obd_dqblk        *dqb = &oqctl->qc_dqblk;
300         int                      rc = 0;
301         bool                     is_default = false;
302         ENTRY;
303
304         LASSERT(qmt != NULL);
305
306         if (oqctl->qc_type >= LL_MAXQUOTAS)
307                 /* invalid quota type */
308                 RETURN(-EINVAL);
309
310         switch (oqctl->qc_cmd) {
311
312         case Q_GETINFO:  /* read grace times */
313                 /* Global grace time is stored in quota settings of ID 0. */
314                 id->qid_uid = 0;
315
316                 /* read inode grace time */
317                 rc = qmt_get(env, qmt, LQUOTA_RES_MD, oqctl->qc_type, id,
318                              NULL, NULL, &oqctl->qc_dqinfo.dqi_igrace, false);
319                 if (rc)
320                         break;
321
322                 /* read block grace time */
323                 rc = qmt_get(env, qmt, LQUOTA_RES_DT, oqctl->qc_type, id,
324                              NULL, NULL, &oqctl->qc_dqinfo.dqi_bgrace, false);
325                 break;
326
327         case Q_SETINFO:  /* modify grace times */
328                 /* setinfo should be using dqi->dqi_valid, but lfs incorrectly
329                  * sets the valid flags in dqb->dqb_valid instead, try to live
330                  * with that ... */
331
332                 /* Global grace time is stored in quota settings of ID 0. */
333                 id->qid_uid = 0;
334
335                 if ((dqb->dqb_valid & QIF_ITIME) != 0) {
336                         /* set inode grace time */
337                         rc = qmt_set(env, qmt, LQUOTA_RES_MD, oqctl->qc_type,
338                                      id, 0, 0, oqctl->qc_dqinfo.dqi_igrace,
339                                      QIF_TIMES, false, false);
340                         if (rc)
341                                 break;
342                 }
343
344                 if ((dqb->dqb_valid & QIF_BTIME) != 0)
345                         /* set block grace time */
346                         rc = qmt_set(env, qmt, LQUOTA_RES_DT, oqctl->qc_type,
347                                      id, 0, 0, oqctl->qc_dqinfo.dqi_bgrace,
348                                      QIF_TIMES, false, false);
349                 break;
350
351         case LUSTRE_Q_GETDEFAULT:
352                 is_default = true;
353                 /* fallthrough */
354
355         case Q_GETQUOTA: /* consult quota limit */
356                 /* extract quota ID from quotactl request */
357                 id->qid_uid = oqctl->qc_id;
358
359                 /* look-up inode quota settings */
360                 rc = qmt_get(env, qmt, LQUOTA_RES_MD, oqctl->qc_type, id,
361                              &dqb->dqb_ihardlimit, &dqb->dqb_isoftlimit,
362                              &dqb->dqb_itime, is_default);
363                 if (rc)
364                         break;
365
366                 dqb->dqb_valid |= QIF_ILIMITS | QIF_ITIME;
367                 /* master isn't aware of actual inode usage */
368                 dqb->dqb_curinodes = 0;
369
370                 /* look-up block quota settings */
371                 rc = qmt_get(env, qmt, LQUOTA_RES_DT, oqctl->qc_type, id,
372                              &dqb->dqb_bhardlimit, &dqb->dqb_bsoftlimit,
373                              &dqb->dqb_btime, is_default);
374                 if (rc)
375                         break;
376
377                 dqb->dqb_valid |= QIF_BLIMITS | QIF_BTIME;
378                 /* master doesn't know the actual block usage */
379                 dqb->dqb_curspace = 0;
380                 break;
381
382         case LUSTRE_Q_SETDEFAULT:
383                 is_default = true;
384                 /* fallthrough */
385
386         case Q_SETQUOTA: /* change quota limits */
387                 /* extract quota ID from quotactl request */
388                 id->qid_uid = oqctl->qc_id;
389
390                 if ((dqb->dqb_valid & QIF_IFLAGS) != 0) {
391                         /* update inode quota settings */
392                         rc = qmt_set(env, qmt, LQUOTA_RES_MD, oqctl->qc_type,
393                                      id, dqb->dqb_ihardlimit,
394                                      dqb->dqb_isoftlimit, dqb->dqb_itime,
395                                      dqb->dqb_valid & QIF_IFLAGS, is_default,
396                                      false);
397                         if (rc)
398                                 break;
399                 }
400
401                 if ((dqb->dqb_valid & QIF_BFLAGS) != 0)
402                         /* update block quota settings */
403                         rc = qmt_set(env, qmt, LQUOTA_RES_DT, oqctl->qc_type,
404                                      id, dqb->dqb_bhardlimit,
405                                      dqb->dqb_bsoftlimit, dqb->dqb_btime,
406                                      dqb->dqb_valid & QIF_BFLAGS, is_default,
407                                      false);
408                 break;
409
410         default:
411                 CERROR("%s: unsupported quotactl command: %d\n",
412                        qmt->qmt_svname, oqctl->qc_cmd);
413                 RETURN(-ENOTSUPP);
414         }
415
416         RETURN(rc);
417 }
418
419 /*
420  * Helper function to handle quota request from slave.
421  *
422  * \param env     - is the environment passed by the caller
423  * \param lqe     - is the lquota_entry subject to the quota request
424  * \param qmt     - is the master device
425  * \param uuid    - is the uuid associated with the slave
426  * \param qb_flags - are the quota request flags as packed in the quota_body
427  * \param qb_count - is the amount of quota space the slave wants to
428  *                   acquire/release
429  * \param qb_usage - is the current space usage on the slave
430  * \param repbody - is the quota_body of reply
431  *
432  * \retval 0            : success
433  * \retval -EDQUOT      : out of quota
434  *         -EINPROGRESS : inform client to retry write/create
435  *         -ve          : other appropriate errors
436  */
437 int qmt_dqacq0(const struct lu_env *env, struct lquota_entry *lqe,
438                struct qmt_device *qmt, struct obd_uuid *uuid, __u32 qb_flags,
439                __u64 qb_count, __u64 qb_usage, struct quota_body *repbody)
440 {
441         struct qmt_thread_info  *qti = qmt_info(env);
442         __u64                    now, count;
443         struct dt_object        *slv_obj = NULL;
444         __u64                    slv_granted, slv_granted_bck;
445         struct thandle          *th = NULL;
446         int                      rc, ret;
447         ENTRY;
448
449         LASSERT(uuid != NULL);
450
451         /* initialize reply */
452         memset(repbody, 0, sizeof(*repbody));
453         memcpy(&repbody->qb_id, &lqe->lqe_id, sizeof(repbody->qb_id));
454
455         if (OBD_FAIL_CHECK(OBD_FAIL_QUOTA_RECOVERABLE_ERR))
456                 RETURN(-cfs_fail_val);
457
458         /* look-up index file associated with acquiring slave */
459         slv_obj = lquota_disk_slv_find(env, qmt->qmt_child, LQE_ROOT(lqe),
460                                        lu_object_fid(&LQE_GLB_OBJ(lqe)->do_lu),
461                                        uuid);
462         if (IS_ERR(slv_obj))
463                 GOTO(out, rc = PTR_ERR(slv_obj));
464
465         /* pack slave fid in reply just for sanity check */
466         memcpy(&repbody->qb_slv_fid, lu_object_fid(&slv_obj->do_lu),
467                sizeof(struct lu_fid));
468
469         /* allocate & start transaction with enough credits to update
470          * global & slave indexes */
471         th = qmt_trans_start_with_slv(env, lqe, slv_obj, &qti->qti_restore);
472         if (IS_ERR(th))
473                 GOTO(out, rc = PTR_ERR(th));
474
475         lqe_write_lock(lqe);
476         LQUOTA_DEBUG(lqe, "dqacq starts uuid:%s flags:0x%x wanted:%llu"
477                      " usage:%llu", obd_uuid2str(uuid), qb_flags, qb_count,
478                      qb_usage);
479
480         /* Legal race, limits have been removed on master, but slave didn't
481          * receive the change yet. Just return EINPROGRESS until the slave gets
482          * notified. */
483         if (!lqe->lqe_enforced && !req_is_rel(qb_flags))
484                 GOTO(out_locked, rc = -ESRCH);
485
486         /* recompute qunit in case it was never initialized */
487         qmt_revalidate(env, lqe);
488
489         /* slave just wants to acquire per-ID lock */
490         if (req_is_acq(qb_flags) && qb_count == 0)
491                 GOTO(out_locked, rc = 0);
492
493         /* fetch how much quota space is already granted to this slave */
494         rc = qmt_slv_read(env, lqe, slv_obj, &slv_granted);
495         if (rc) {
496                 LQUOTA_ERROR(lqe, "Failed to get granted for slave %s, rc=%d",
497                              obd_uuid2str(uuid), rc);
498                 GOTO(out_locked, rc);
499         }
500         /* recall how much space this slave currently owns in order to restore
501          * it in case of failure */
502         slv_granted_bck = slv_granted;
503
504         /* record current time for soft limit & grace time management */
505         now = ktime_get_real_seconds();
506
507         if (req_is_rel(qb_flags)) {
508                 /* Slave would like to release quota space */
509                 if (slv_granted < qb_count ||
510                     lqe->lqe_granted < qb_count) {
511                         /* can't release more than granted */
512                         LQUOTA_ERROR(lqe,
513                                      "Release too much! uuid:%s release:%llu granted:%llu, total:%llu",
514                                      obd_uuid2str(uuid), qb_count,
515                                      slv_granted, lqe->lqe_granted);
516                         GOTO(out_locked, rc = -EINVAL);
517                 }
518
519                 repbody->qb_count = qb_count;
520                 /* put released space back to global pool */
521                 QMT_REL(lqe, slv_granted, qb_count);
522                 GOTO(out_write, rc = 0);
523         }
524
525         if (req_has_rep(qb_flags) && slv_granted < qb_usage) {
526                 /* Slave is reporting space usage in quota request and it turns
527                  * out to be using more quota space than owned, so we adjust
528                  * granted space regardless of the current state of affairs */
529                 repbody->qb_count = qb_usage - slv_granted;
530                 QMT_GRANT(lqe, slv_granted, repbody->qb_count);
531         }
532
533         if (!req_is_acq(qb_flags) && !req_is_preacq(qb_flags))
534                 GOTO(out_write, rc = 0);
535
536         qmt_adjust_edquot(lqe, now);
537         if (lqe->lqe_edquot)
538                 /* no hope to claim further space back */
539                 GOTO(out_write, rc = -EDQUOT);
540
541         if (qmt_space_exhausted(lqe, now)) {
542                 /* might have some free space once rebalancing is completed */
543                 rc = req_is_acq(qb_flags) ? -EINPROGRESS : -EDQUOT;
544                 GOTO(out_write, rc);
545         }
546
547         if (req_is_preacq(qb_flags)) {
548                 /* slave would like to pre-acquire quota space. To do so, it
549                  * reports in qb_count how much spare quota space it owns and we
550                  * can grant back quota space which is consistent with qunit
551                  * value. */
552
553                 if (qb_count >= lqe->lqe_qunit)
554                         /* slave already own the maximum it should */
555                         GOTO(out_write, rc = 0);
556
557                 count = qmt_alloc_expand(lqe, slv_granted, qb_count);
558                 if (count == 0)
559                         GOTO(out_write, rc = -EDQUOT);
560
561                 repbody->qb_count += count;
562                 QMT_GRANT(lqe, slv_granted, count);
563                 GOTO(out_write, rc = 0);
564         }
565
566         /* processing acquire request with clients waiting */
567         if (lqe->lqe_hardlimit != 0 &&
568             lqe->lqe_granted + qb_count > lqe->lqe_hardlimit) {
569                 /* cannot grant as much as asked, but can still afford to grant
570                  * some quota space back */
571                 count = lqe->lqe_hardlimit - lqe->lqe_granted;
572                 repbody->qb_count += count;
573                 QMT_GRANT(lqe, slv_granted, count);
574                 GOTO(out_write, rc = 0);
575         }
576
577         /* Whouhou! we can satisfy the slave request! */
578         repbody->qb_count += qb_count;
579         QMT_GRANT(lqe, slv_granted, qb_count);
580
581         /* Try to expand the acquired count for DQACQ */
582         count = qmt_alloc_expand(lqe, slv_granted, 0);
583         if (count != 0) {
584                 /* can even grant more than asked, it is like xmas ... */
585                 repbody->qb_count += count;
586                 QMT_GRANT(lqe, slv_granted, count);
587                 GOTO(out_write, rc = 0);
588         }
589
590         GOTO(out_write, rc = 0);
591 out_write:
592         if (repbody->qb_count == 0)
593                 GOTO(out_locked, rc);
594
595         /* start/stop grace timer if required */
596         if (lqe->lqe_softlimit != 0) {
597                 if (lqe->lqe_granted > lqe->lqe_softlimit &&
598                     lqe->lqe_gracetime == 0)
599                         /* first time over soft limit, let's start grace
600                          * timer */
601                         lqe->lqe_gracetime = now + qmt_lqe_grace(lqe);
602                 else if (lqe->lqe_granted <= lqe->lqe_softlimit &&
603                          lqe->lqe_gracetime != 0)
604                         /* Clear grace timer */
605                         lqe->lqe_gracetime = 0;
606         }
607
608         /* Update slave index first since it is easier to roll back */
609         ret = qmt_slv_write(env, th, lqe, slv_obj, LQUOTA_BUMP_VER,
610                             &repbody->qb_slv_ver, slv_granted);
611         if (ret) {
612                 /* restore initial quota settings */
613                 qmt_restore(lqe, &qti->qti_restore);
614                 /* reset qb_count */
615                 repbody->qb_count = 0;
616                 GOTO(out_locked, rc = ret);
617         }
618
619         /* Update global index, no version bump needed */
620         ret = qmt_glb_write(env, th, lqe, 0, NULL);
621         if (ret) {
622                 rc = ret;
623                 /* restore initial quota settings */
624                 qmt_restore(lqe, &qti->qti_restore);
625                 /* reset qb_count */
626                 repbody->qb_count = 0;
627
628                 /* restore previous granted value */
629                 ret = qmt_slv_write(env, th, lqe, slv_obj, 0, NULL,
630                                     slv_granted_bck);
631                 if (ret) {
632                         LQUOTA_ERROR(lqe, "failed to restore initial slave "
633                                      "value rc:%d ret%d", rc, ret);
634                         LBUG();
635                 }
636                 qmt_adjust_edquot(lqe, now);
637                 GOTO(out_locked, rc);
638         }
639
640         /* Total granted has been changed, let's try to adjust the qunit
641          * size according to the total granted & limits. */
642         qmt_adjust_qunit(env, lqe);
643
644         /* clear/set edquot flag and notify slaves via glimpse if needed */
645         qmt_adjust_edquot(lqe, now);
646 out_locked:
647         LQUOTA_DEBUG(lqe, "dqacq ends count:%llu ver:%llu rc:%d",
648                      repbody->qb_count, repbody->qb_slv_ver, rc);
649         lqe_write_unlock(lqe);
650 out:
651         if (th != NULL && !IS_ERR(th))
652                 dt_trans_stop(env, qmt->qmt_child, th);
653
654         if (slv_obj != NULL && !IS_ERR(slv_obj))
655                 dt_object_put(env, slv_obj);
656
657         if ((req_is_acq(qb_flags) || req_is_preacq(qb_flags)) &&
658             OBD_FAIL_CHECK(OBD_FAIL_QUOTA_EDQUOT)) {
659                 /* introduce inconsistency between granted value in slave index
660                  * and slave index copy of slave */
661                 repbody->qb_count = 0;
662                 rc = -EDQUOT;
663         }
664
665         RETURN(rc);
666 }
667
668 /*
669  * Handle quota request from slave.
670  *
671  * \param env  - is the environment passed by the caller
672  * \param ld   - is the lu device associated with the qmt
673  * \param req  - is the quota acquire request
674  */
675 static int qmt_dqacq(const struct lu_env *env, struct lu_device *ld,
676                      struct ptlrpc_request *req)
677 {
678         struct qmt_device       *qmt = lu2qmt_dev(ld);
679         struct quota_body       *qbody, *repbody;
680         struct obd_uuid         *uuid;
681         struct ldlm_lock        *lock;
682         struct lquota_entry     *lqe;
683         int                      pool_type, qtype;
684         int                      rc;
685         ENTRY;
686
687         qbody = req_capsule_client_get(&req->rq_pill, &RMF_QUOTA_BODY);
688         if (qbody == NULL)
689                 RETURN(err_serious(-EPROTO));
690
691         repbody = req_capsule_server_get(&req->rq_pill, &RMF_QUOTA_BODY);
692         if (repbody == NULL)
693                 RETURN(err_serious(-EFAULT));
694
695         /* verify if global lock is stale */
696         if (!lustre_handle_is_used(&qbody->qb_glb_lockh))
697                 RETURN(-ENOLCK);
698
699         lock = ldlm_handle2lock(&qbody->qb_glb_lockh);
700         if (lock == NULL)
701                 RETURN(-ENOLCK);
702         LDLM_LOCK_PUT(lock);
703
704         uuid = &req->rq_export->exp_client_uuid;
705
706         if (req_is_rel(qbody->qb_flags) + req_is_acq(qbody->qb_flags) +
707             req_is_preacq(qbody->qb_flags) > 1) {
708                 CERROR("%s: malformed quota request with conflicting flags set "
709                        "(%x) from slave %s\n", qmt->qmt_svname,
710                        qbody->qb_flags, obd_uuid2str(uuid));
711                 RETURN(-EPROTO);
712         }
713
714         if (req_is_acq(qbody->qb_flags) || req_is_preacq(qbody->qb_flags)) {
715                 /* acquire and pre-acquire should use a valid ID lock */
716
717                 if (!lustre_handle_is_used(&qbody->qb_lockh))
718                         RETURN(-ENOLCK);
719
720                 lock = ldlm_handle2lock(&qbody->qb_lockh);
721                 if (lock == NULL)
722                         /* no lock associated with this handle */
723                         RETURN(-ENOLCK);
724
725                 LDLM_DEBUG(lock, "%sacquire request",
726                            req_is_preacq(qbody->qb_flags) ? "pre" : "");
727
728                 if (!obd_uuid_equals(&lock->l_export->exp_client_uuid, uuid)) {
729                         /* sorry, no way to cheat ... */
730                         LDLM_LOCK_PUT(lock);
731                         RETURN(-ENOLCK);
732                 }
733
734                 if (ldlm_is_ast_sent(lock)) {
735                         struct ptlrpc_service_part *svc;
736                         time64_t timeout;
737
738                         svc = req->rq_rqbd->rqbd_svcpt;
739                         timeout = at_est2timeout(at_get(&svc->scp_at_estimate));
740                         timeout += (ldlm_bl_timeout(lock) >> 1);
741
742                         /* lock is being cancelled, prolong timeout */
743                         ldlm_refresh_waiting_lock(lock, timeout);
744                 }
745                 LDLM_LOCK_PUT(lock);
746         }
747
748         /* extract quota information from global index FID packed in the
749          * request */
750         rc = lquota_extract_fid(&qbody->qb_fid, &pool_type, &qtype);
751         if (rc)
752                 RETURN(-EINVAL);
753
754         /* Find the quota entry associated with the quota id */
755         lqe = qmt_pool_lqe_lookup(env, qmt, pool_type, qtype,
756                                   &qbody->qb_id);
757         if (IS_ERR(lqe))
758                 RETURN(PTR_ERR(lqe));
759
760         /* process quota request */
761         rc = qmt_dqacq0(env, lqe, qmt, uuid, qbody->qb_flags, qbody->qb_count,
762                         qbody->qb_usage, repbody);
763
764         if (lustre_handle_is_used(&qbody->qb_lockh))
765                 /* return current qunit value only to slaves owning an per-ID
766                  * quota lock. For enqueue, the qunit value will be returned in
767                  * the LVB */
768                  repbody->qb_qunit = lqe->lqe_qunit;
769         lqe_putref(lqe);
770         RETURN(rc);
771 }
772
773 /* Vector of quota request handlers. This vector is used by the MDT to forward
774  * requests to the quota master. */
775 struct qmt_handlers qmt_hdls = {
776         /* quota request handlers */
777         .qmth_quotactl          = qmt_quotactl,
778         .qmth_dqacq             = qmt_dqacq,
779
780         /* ldlm handlers */
781         .qmth_intent_policy     = qmt_intent_policy,
782         .qmth_lvbo_init         = qmt_lvbo_init,
783         .qmth_lvbo_update       = qmt_lvbo_update,
784         .qmth_lvbo_size         = qmt_lvbo_size,
785         .qmth_lvbo_fill         = qmt_lvbo_fill,
786         .qmth_lvbo_free         = qmt_lvbo_free,
787 };
788 EXPORT_SYMBOL(qmt_hdls);