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