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