Whamcloud - gitweb
455652c372a15c48b371b66ff2e4ff74e23e0a3a
[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 pool_id - is the 16-bit pool identifier
42  * \param restype - is the pool type, either block (i.e. LQUOTA_RES_DT) or inode
43  *                  (i.e. LQUOTA_RES_MD)
44  * \param qtype   - is the quota type
45  * \param id      - is the quota indentifier for which we want to acces quota
46  *                  settings.
47  * \param hard    - is the output variable where to copy the hard limit
48  * \param soft    - is the output variable where to copy the soft limit
49  * \param time    - is the output variable where to copy the grace time
50  */
51 static int qmt_get(const struct lu_env *env, struct qmt_device *qmt,
52                    __u16 pool_id, __u8 restype, __u8 qtype, union lquota_id *id,
53                    __u64 *hard, __u64 *soft, __u64 *time, bool is_default)
54 {
55         struct lquota_entry     *lqe;
56         ENTRY;
57
58         LASSERT(!is_default || id->qid_uid == 0);
59
60         /* look-up lqe structure containing quota settings */
61         lqe = qmt_pool_lqe_lookup(env, qmt, pool_id, restype, qtype, id);
62         if (IS_ERR(lqe))
63                 RETURN(PTR_ERR(lqe));
64
65         /* copy quota settings */
66         lqe_read_lock(lqe);
67         LQUOTA_DEBUG(lqe, "fetch settings");
68         if (hard != NULL)
69                 *hard = lqe->lqe_hardlimit;
70         if (soft != NULL)
71                 *soft = lqe->lqe_softlimit;
72         if (time != NULL) {
73                 *time = lqe->lqe_gracetime;
74                 if (lqe->lqe_is_default)
75                         *time |= (__u64)LQUOTA_FLAG_DEFAULT <<
76                                                         LQUOTA_GRACE_BITS;
77         }
78         lqe_read_unlock(lqe);
79
80         lqe_putref(lqe);
81         RETURN(0);
82 }
83
84 struct qmt_entry_iter_data {
85         const struct lu_env *qeid_env;
86         struct qmt_device   *qeid_qmt;
87 };
88
89 static int qmt_entry_iter_cb(struct cfs_hash *hs, struct cfs_hash_bd *bd,
90                              struct hlist_node *hnode, void *d)
91 {
92         struct qmt_entry_iter_data *iter = (struct qmt_entry_iter_data *)d;
93         struct lquota_entry     *lqe;
94
95         lqe = hlist_entry(hnode, struct lquota_entry, lqe_hash);
96         LASSERT(atomic_read(&lqe->lqe_ref) > 0);
97
98         if (lqe->lqe_id.qid_uid == 0 || !lqe->lqe_is_default)
99                 return 0;
100
101         return qmt_set_with_lqe(iter->qeid_env, iter->qeid_qmt, lqe, 0, 0, 0, 0,
102                                 true, true);
103 }
104
105 /*
106  * Update quota settings for a given lqe.
107  *
108  * \param env        - is the environment passed by the caller
109  * \param qmt        - is the quota master target
110  * \param lqe        - is the lquota_entry for which we want to modify quota
111  *                     settings.
112  * \param hard       - is the new hard limit
113  * \param soft       - is the new soft limit
114  * \param time       - is the new grace time
115  * \param valid      - is the list of settings to change
116  * \param is_default - true for default quota setting
117  * \param is_updated - true if the lqe is updated and no need to write back
118  */
119
120 int qmt_set_with_lqe(const struct lu_env *env, struct qmt_device *qmt,
121                      struct lquota_entry *lqe, __u64 hard, __u64 soft,
122                      __u64 time, __u32 valid, bool is_default, bool is_updated)
123 {
124         struct qmt_thread_info  *qti = qmt_info(env);
125         struct thandle          *th = NULL;
126         time64_t now;
127         __u64                    ver;
128         bool                     dirtied = false;
129         int                      rc = 0;
130         ENTRY;
131
132         /* need to write back to global quota file? */
133         if (!is_updated) {
134                 /* allocate & start transaction with enough credits to update
135                  * quota  settings in the global index file */
136                 th = qmt_trans_start(env, lqe, &qti->qti_restore);
137                 if (IS_ERR(th))
138                         GOTO(out_nolock, rc = PTR_ERR(th));
139         }
140
141         now = ktime_get_real_seconds();
142
143         lqe_write_lock(lqe);
144         LQUOTA_DEBUG(lqe, "changing quota settings valid:%x hard:%llu soft:"
145                      "%llu time:%llu", 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 (dirtied) {
196                 if (!is_default && lqe->lqe_is_default) {
197                         LQUOTA_DEBUG(lqe, "the qid %llu has been set quota"
198                                      " explicitly, clear the default flag",
199                                      lqe->lqe_id.qid_uid);
200
201                         qmt_lqe_clear_default(lqe);
202                 }
203
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 pool_id    - is the 16-bit pool identifier
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                    __u16 pool_id, __u8 restype, __u8 qtype,
267                    union lquota_id *id, __u64 hard, __u64 soft, __u64 time,
268                    __u32 valid, 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, pool_id, 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, 0, 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, 0, 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, 0, 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, 0, 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
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, 0, 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, 0, 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
384         case Q_SETQUOTA: /* change quota limits */
385                 /* extract quota ID from quotactl request */
386                 id->qid_uid = oqctl->qc_id;
387
388                 if ((dqb->dqb_valid & QIF_IFLAGS) != 0) {
389                         /* update inode quota settings */
390                         rc = qmt_set(env, qmt, 0, LQUOTA_RES_MD, oqctl->qc_type,
391                                      id, dqb->dqb_ihardlimit,
392                                      dqb->dqb_isoftlimit, dqb->dqb_itime,
393                                      dqb->dqb_valid & QIF_IFLAGS, is_default,
394                                      false);
395                         if (rc)
396                                 break;
397                 }
398
399                 if ((dqb->dqb_valid & QIF_BFLAGS) != 0)
400                         /* update block quota settings */
401                         rc = qmt_set(env, qmt, 0, LQUOTA_RES_DT, oqctl->qc_type,
402                                      id, dqb->dqb_bhardlimit,
403                                      dqb->dqb_bsoftlimit, dqb->dqb_btime,
404                                      dqb->dqb_valid & QIF_BFLAGS, is_default,
405                                      false);
406                 break;
407
408         default:
409                 CERROR("%s: unsupported quotactl command: %d\n",
410                        qmt->qmt_svname, oqctl->qc_cmd);
411                 RETURN(-ENOTSUPP);
412         }
413
414         RETURN(rc);
415 }
416
417 /*
418  * Helper function to handle quota request from slave.
419  *
420  * \param env     - is the environment passed by the caller
421  * \param lqe     - is the lquota_entry subject to the quota request
422  * \param qmt     - is the master device
423  * \param uuid    - is the uuid associated with the slave
424  * \param qb_flags - are the quota request flags as packed in the quota_body
425  * \param qb_count - is the amount of quota space the slave wants to
426  *                   acquire/release
427  * \param qb_usage - is the current space usage on the slave
428  * \param repbody - is the quota_body of reply
429  *
430  * \retval 0            : success
431  * \retval -EDQUOT      : out of quota
432  *         -EINPROGRESS : inform client to retry write/create
433  *         -ve          : other appropriate errors
434  */
435 int qmt_dqacq0(const struct lu_env *env, struct lquota_entry *lqe,
436                struct qmt_device *qmt, struct obd_uuid *uuid, __u32 qb_flags,
437                __u64 qb_count, __u64 qb_usage, struct quota_body *repbody)
438 {
439         struct qmt_thread_info  *qti = qmt_info(env);
440         __u64                    now, count;
441         struct dt_object        *slv_obj = NULL;
442         __u64                    slv_granted, slv_granted_bck;
443         struct thandle          *th = NULL;
444         int                      rc, ret;
445         ENTRY;
446
447         LASSERT(uuid != NULL);
448
449         /* initialize reply */
450         memset(repbody, 0, sizeof(*repbody));
451         memcpy(&repbody->qb_id, &lqe->lqe_id, sizeof(repbody->qb_id));
452
453         if (OBD_FAIL_CHECK(OBD_FAIL_QUOTA_RECOVERABLE_ERR))
454                 RETURN(-cfs_fail_val);
455
456         /* look-up index file associated with acquiring slave */
457         slv_obj = lquota_disk_slv_find(env, qmt->qmt_child, LQE_ROOT(lqe),
458                                        lu_object_fid(&LQE_GLB_OBJ(lqe)->do_lu),
459                                        uuid);
460         if (IS_ERR(slv_obj))
461                 GOTO(out, rc = PTR_ERR(slv_obj));
462
463         /* pack slave fid in reply just for sanity check */
464         memcpy(&repbody->qb_slv_fid, lu_object_fid(&slv_obj->do_lu),
465                sizeof(struct lu_fid));
466
467         /* allocate & start transaction with enough credits to update
468          * global & slave indexes */
469         th = qmt_trans_start_with_slv(env, lqe, slv_obj, &qti->qti_restore);
470         if (IS_ERR(th))
471                 GOTO(out, rc = PTR_ERR(th));
472
473         lqe_write_lock(lqe);
474         LQUOTA_DEBUG(lqe, "dqacq starts uuid:%s flags:0x%x wanted:%llu"
475                      " usage:%llu", obd_uuid2str(uuid), qb_flags, qb_count,
476                      qb_usage);
477
478         /* Legal race, limits have been removed on master, but slave didn't
479          * receive the change yet. Just return EINPROGRESS until the slave gets
480          * notified. */
481         if (!lqe->lqe_enforced && !req_is_rel(qb_flags))
482                 GOTO(out_locked, rc = -ESRCH);
483
484         /* recompute qunit in case it was never initialized */
485         qmt_revalidate(env, lqe);
486
487         /* slave just wants to acquire per-ID lock */
488         if (req_is_acq(qb_flags) && qb_count == 0)
489                 GOTO(out_locked, rc = 0);
490
491         /* fetch how much quota space is already granted to this slave */
492         rc = qmt_slv_read(env, lqe, slv_obj, &slv_granted);
493         if (rc) {
494                 LQUOTA_ERROR(lqe, "Failed to get granted for slave %s, rc=%d",
495                              obd_uuid2str(uuid), rc);
496                 GOTO(out_locked, rc);
497         }
498         /* recall how much space this slave currently owns in order to restore
499          * it in case of failure */
500         slv_granted_bck = slv_granted;
501
502         /* record current time for soft limit & grace time management */
503         now = ktime_get_real_seconds();
504
505         if (req_is_rel(qb_flags)) {
506                 /* Slave would like to release quota space */
507                 if (slv_granted < qb_count ||
508                     lqe->lqe_granted < qb_count) {
509                         /* can't release more than granted */
510                         LQUOTA_ERROR(lqe, "Release too much! uuid:%s release:"
511                                      "%llu granted:%llu, total:%llu",
512                                      obd_uuid2str(uuid), qb_count,
513                                      slv_granted, lqe->lqe_granted);
514                         GOTO(out_locked, rc = -EINVAL);
515                 }
516
517                 repbody->qb_count = qb_count;
518                 /* put released space back to global pool */
519                 QMT_REL(lqe, slv_granted, qb_count);
520                 GOTO(out_write, rc = 0);
521         }
522
523         if (req_has_rep(qb_flags) && slv_granted < qb_usage) {
524                 /* Slave is reporting space usage in quota request and it turns
525                  * out to be using more quota space than owned, so we adjust
526                  * granted space regardless of the current state of affairs */
527                 repbody->qb_count = qb_usage - slv_granted;
528                 QMT_GRANT(lqe, slv_granted, repbody->qb_count);
529         }
530
531         if (!req_is_acq(qb_flags) && !req_is_preacq(qb_flags))
532                 GOTO(out_write, rc = 0);
533
534         qmt_adjust_edquot(lqe, now);
535         if (lqe->lqe_edquot)
536                 /* no hope to claim further space back */
537                 GOTO(out_write, rc = -EDQUOT);
538
539         if (qmt_space_exhausted(lqe, now)) {
540                 /* might have some free space once rebalancing is completed */
541                 rc = req_is_acq(qb_flags) ? -EINPROGRESS : -EDQUOT;
542                 GOTO(out_write, rc);
543         }
544
545         if (req_is_preacq(qb_flags)) {
546                 /* slave would like to pre-acquire quota space. To do so, it
547                  * reports in qb_count how much spare quota space it owns and we
548                  * can grant back quota space which is consistent with qunit
549                  * value. */
550
551                 if (qb_count >= lqe->lqe_qunit)
552                         /* slave already own the maximum it should */
553                         GOTO(out_write, rc = 0);
554
555                 count = qmt_alloc_expand(lqe, slv_granted, qb_count);
556                 if (count == 0)
557                         GOTO(out_write, rc = -EDQUOT);
558
559                 repbody->qb_count += count;
560                 QMT_GRANT(lqe, slv_granted, count);
561                 GOTO(out_write, rc = 0);
562         }
563
564         /* processing acquire request with clients waiting */
565         if (lqe->lqe_hardlimit != 0 &&
566             lqe->lqe_granted + qb_count > lqe->lqe_hardlimit) {
567                 /* cannot grant as much as asked, but can still afford to grant
568                  * some quota space back */
569                 count = lqe->lqe_hardlimit - lqe->lqe_granted;
570                 repbody->qb_count += count;
571                 QMT_GRANT(lqe, slv_granted, count);
572                 GOTO(out_write, rc = 0);
573         }
574
575         /* Whouhou! we can satisfy the slave request! */
576         repbody->qb_count += qb_count;
577         QMT_GRANT(lqe, slv_granted, qb_count);
578
579         /* Try to expand the acquired count for DQACQ */
580         count = qmt_alloc_expand(lqe, slv_granted, 0);
581         if (count != 0) {
582                 /* can even grant more than asked, it is like xmas ... */
583                 repbody->qb_count += count;
584                 QMT_GRANT(lqe, slv_granted, count);
585                 GOTO(out_write, rc = 0);
586         }
587
588         GOTO(out_write, rc = 0);
589 out_write:
590         if (repbody->qb_count == 0)
591                 GOTO(out_locked, rc);
592
593         /* start/stop grace timer if required */
594         if (lqe->lqe_softlimit != 0) {
595                 if (lqe->lqe_granted > lqe->lqe_softlimit &&
596                     lqe->lqe_gracetime == 0)
597                         /* first time over soft limit, let's start grace
598                          * timer */
599                         lqe->lqe_gracetime = now + qmt_lqe_grace(lqe);
600                 else if (lqe->lqe_granted <= lqe->lqe_softlimit &&
601                          lqe->lqe_gracetime != 0)
602                         /* Clear grace timer */
603                         lqe->lqe_gracetime = 0;
604         }
605
606         /* Update slave index first since it is easier to roll back */
607         ret = qmt_slv_write(env, th, lqe, slv_obj, LQUOTA_BUMP_VER,
608                             &repbody->qb_slv_ver, slv_granted);
609         if (ret) {
610                 /* restore initial quota settings */
611                 qmt_restore(lqe, &qti->qti_restore);
612                 /* reset qb_count */
613                 repbody->qb_count = 0;
614                 GOTO(out_locked, rc = ret);
615         }
616
617         /* Update global index, no version bump needed */
618         ret = qmt_glb_write(env, th, lqe, 0, NULL);
619         if (ret) {
620                 rc = ret;
621                 /* restore initial quota settings */
622                 qmt_restore(lqe, &qti->qti_restore);
623                 /* reset qb_count */
624                 repbody->qb_count = 0;
625
626                 /* restore previous granted value */
627                 ret = qmt_slv_write(env, th, lqe, slv_obj, 0, NULL,
628                                     slv_granted_bck);
629                 if (ret) {
630                         LQUOTA_ERROR(lqe, "failed to restore initial slave "
631                                      "value rc:%d ret%d", rc, ret);
632                         LBUG();
633                 }
634                 qmt_adjust_edquot(lqe, now);
635                 GOTO(out_locked, rc);
636         }
637
638         /* Total granted has been changed, let's try to adjust the qunit
639          * size according to the total granted & limits. */
640         qmt_adjust_qunit(env, lqe);
641
642         /* clear/set edquot flag and notify slaves via glimpse if needed */
643         qmt_adjust_edquot(lqe, now);
644 out_locked:
645         LQUOTA_DEBUG(lqe, "dqacq ends count:%llu ver:%llu rc:%d",
646                      repbody->qb_count, repbody->qb_slv_ver, rc);
647         lqe_write_unlock(lqe);
648 out:
649         if (th != NULL && !IS_ERR(th))
650                 dt_trans_stop(env, qmt->qmt_child, th);
651
652         if (slv_obj != NULL && !IS_ERR(slv_obj))
653                 dt_object_put(env, slv_obj);
654
655         if ((req_is_acq(qb_flags) || req_is_preacq(qb_flags)) &&
656             OBD_FAIL_CHECK(OBD_FAIL_QUOTA_EDQUOT)) {
657                 /* introduce inconsistency between granted value in slave index
658                  * and slave index copy of slave */
659                 repbody->qb_count = 0;
660                 rc = -EDQUOT;
661         }
662
663         RETURN(rc);
664 }
665
666 /*
667  * Handle quota request from slave.
668  *
669  * \param env  - is the environment passed by the caller
670  * \param ld   - is the lu device associated with the qmt
671  * \param req  - is the quota acquire request
672  */
673 static int qmt_dqacq(const struct lu_env *env, struct lu_device *ld,
674                      struct ptlrpc_request *req)
675 {
676         struct qmt_device       *qmt = lu2qmt_dev(ld);
677         struct quota_body       *qbody, *repbody;
678         struct obd_uuid         *uuid;
679         struct ldlm_lock        *lock;
680         struct lquota_entry     *lqe;
681         int                      pool_id, pool_type, qtype;
682         int                      rc;
683         ENTRY;
684
685         qbody = req_capsule_client_get(&req->rq_pill, &RMF_QUOTA_BODY);
686         if (qbody == NULL)
687                 RETURN(err_serious(-EPROTO));
688
689         repbody = req_capsule_server_get(&req->rq_pill, &RMF_QUOTA_BODY);
690         if (repbody == NULL)
691                 RETURN(err_serious(-EFAULT));
692
693         /* verify if global lock is stale */
694         if (!lustre_handle_is_used(&qbody->qb_glb_lockh))
695                 RETURN(-ENOLCK);
696
697         lock = ldlm_handle2lock(&qbody->qb_glb_lockh);
698         if (lock == NULL)
699                 RETURN(-ENOLCK);
700         LDLM_LOCK_PUT(lock);
701
702         uuid = &req->rq_export->exp_client_uuid;
703
704         if (req_is_rel(qbody->qb_flags) + req_is_acq(qbody->qb_flags) +
705             req_is_preacq(qbody->qb_flags) > 1) {
706                 CERROR("%s: malformed quota request with conflicting flags set "
707                        "(%x) from slave %s\n", qmt->qmt_svname,
708                        qbody->qb_flags, obd_uuid2str(uuid));
709                 RETURN(-EPROTO);
710         }
711
712         if (req_is_acq(qbody->qb_flags) || req_is_preacq(qbody->qb_flags)) {
713                 /* acquire and pre-acquire should use a valid ID lock */
714
715                 if (!lustre_handle_is_used(&qbody->qb_lockh))
716                         RETURN(-ENOLCK);
717
718                 lock = ldlm_handle2lock(&qbody->qb_lockh);
719                 if (lock == NULL)
720                         /* no lock associated with this handle */
721                         RETURN(-ENOLCK);
722
723                 LDLM_DEBUG(lock, "%sacquire request",
724                            req_is_preacq(qbody->qb_flags) ? "pre" : "");
725
726                 if (!obd_uuid_equals(&lock->l_export->exp_client_uuid, uuid)) {
727                         /* sorry, no way to cheat ... */
728                         LDLM_LOCK_PUT(lock);
729                         RETURN(-ENOLCK);
730                 }
731
732                 if (ldlm_is_ast_sent(lock)) {
733                         struct ptlrpc_service_part *svc;
734                         time64_t timeout;
735
736                         svc = req->rq_rqbd->rqbd_svcpt;
737                         timeout = at_est2timeout(at_get(&svc->scp_at_estimate));
738                         timeout += (ldlm_bl_timeout(lock) >> 1);
739
740                         /* lock is being cancelled, prolong timeout */
741                         ldlm_refresh_waiting_lock(lock, timeout);
742                 }
743                 LDLM_LOCK_PUT(lock);
744         }
745
746         /* extract pool & quota information from global index FID packed in the
747          * request */
748         rc = lquota_extract_fid(&qbody->qb_fid, &pool_id, &pool_type, &qtype);
749         if (rc)
750                 RETURN(-EINVAL);
751
752         /* Find the quota entry associated with the quota id */
753         lqe = qmt_pool_lqe_lookup(env, qmt, pool_id, pool_type, qtype,
754                                   &qbody->qb_id);
755         if (IS_ERR(lqe))
756                 RETURN(PTR_ERR(lqe));
757
758         /* process quota request */
759         rc = qmt_dqacq0(env, lqe, qmt, uuid, qbody->qb_flags, qbody->qb_count,
760                         qbody->qb_usage, repbody);
761
762         if (lustre_handle_is_used(&qbody->qb_lockh))
763                 /* return current qunit value only to slaves owning an per-ID
764                  * quota lock. For enqueue, the qunit value will be returned in
765                  * the LVB */
766                  repbody->qb_qunit = lqe->lqe_qunit;
767         lqe_putref(lqe);
768         RETURN(rc);
769 }
770
771 /* Vector of quota request handlers. This vector is used by the MDT to forward
772  * requests to the quota master. */
773 struct qmt_handlers qmt_hdls = {
774         /* quota request handlers */
775         .qmth_quotactl          = qmt_quotactl,
776         .qmth_dqacq             = qmt_dqacq,
777
778         /* ldlm handlers */
779         .qmth_intent_policy     = qmt_intent_policy,
780         .qmth_lvbo_init         = qmt_lvbo_init,
781         .qmth_lvbo_update       = qmt_lvbo_update,
782         .qmth_lvbo_size         = qmt_lvbo_size,
783         .qmth_lvbo_fill         = qmt_lvbo_fill,
784         .qmth_lvbo_free         = qmt_lvbo_free,
785 };
786 EXPORT_SYMBOL(qmt_hdls);