Whamcloud - gitweb
LU-17705 ptlrpc: replace synchronize_rcu() with rcu_barrier()
[fs/lustre-release.git] / lustre / quota / qmt_handler.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 021110-1307, USA
20  *
21  * GPL HEADER END
22  */
23 /*
24  * Copyright (c) 2012, 2017, Intel Corporation.
25  * Use is subject to license terms.
26  *
27  * Author: Johann Lombardi <johann.lombardi@intel.com>
28  * Author: Niu    Yawei    <yawei.niu@intel.com>
29  */
30
31 #define DEBUG_SUBSYSTEM S_LQUOTA
32
33 #include <obd_class.h>
34 #include "qmt_internal.h"
35
36 /*
37  * Retrieve quota settings for a given identifier.
38  *
39  * \param env     - is the environment passed by the caller
40  * \param qmt     - is the quota master target
41  * \param restype - is the pool type, either block (i.e. LQUOTA_RES_DT) or inode
42  *                  (i.e. LQUOTA_RES_MD)
43  * \param qtype   - is the quota type
44  * \param id      - is the quota indentifier for which we want to acces quota
45  *                  settings.
46  * \param hard    - is the output variable where to copy the hard limit
47  * \param soft    - is the output variable where to copy the soft limit
48  * \param time    - is the output variable where to copy the grace time
49  */
50 static int qmt_get(const struct lu_env *env, struct qmt_device *qmt,
51                    __u8 restype, __u8 qtype, union lquota_id *id,
52                    __u64 *hard, __u64 *soft, __u64 *time, bool is_default,
53                    char *pool_name)
54 {
55         struct lquota_entry     *lqe;
56         ENTRY;
57
58         LASSERT(!is_default || id->qid_uid == 0);
59         if (pool_name && !strnlen(pool_name, LOV_MAXPOOLNAME))
60                 pool_name = NULL;
61
62         /* look-up lqe structure containing quota settings */
63         lqe = qmt_pool_lqe_lookup(env, qmt, restype, qtype, id, pool_name);
64         if (IS_ERR(lqe))
65                 RETURN(PTR_ERR(lqe));
66
67         /* copy quota settings */
68         lqe_read_lock(lqe);
69         LQUOTA_DEBUG(lqe, "fetch settings");
70         if (hard != NULL)
71                 *hard = lqe->lqe_hardlimit;
72         if (soft != NULL)
73                 *soft = lqe->lqe_softlimit;
74         if (time != NULL) {
75                 *time = lqe->lqe_gracetime;
76                 if (lqe->lqe_is_default)
77                         *time |= (__u64)LQUOTA_FLAG_DEFAULT <<
78                                                         LQUOTA_GRACE_BITS;
79         }
80         lqe_read_unlock(lqe);
81
82         lqe_putref(lqe);
83         RETURN(0);
84 }
85
86 struct qmt_entry_iter_data {
87         const struct lu_env *qeid_env;
88         struct qmt_device   *qeid_qmt;
89 };
90
91 static int qmt_entry_iter_cb(struct cfs_hash *hs, struct cfs_hash_bd *bd,
92                              struct hlist_node *hnode, void *d)
93 {
94         struct qmt_entry_iter_data *iter = (struct qmt_entry_iter_data *)d;
95         struct lquota_entry     *lqe;
96
97         lqe = hlist_entry(hnode, struct lquota_entry, lqe_hash);
98         LASSERT(atomic_read(&lqe->lqe_ref) > 0);
99
100         if (lqe->lqe_id.qid_uid == 0 || !lqe->lqe_is_default)
101                 return 0;
102
103         return qmt_set_with_lqe(iter->qeid_env, iter->qeid_qmt, lqe, 0, 0, 0, 0,
104                                 true, true);
105 }
106
107 static void qmt_set_id_notify(const struct lu_env *env, struct qmt_device *qmt,
108                               struct lquota_entry *lqe)
109 {
110         struct lquota_entry *lqe_gl;
111         int rc;
112
113         lqe_gl = lqe->lqe_is_global ? lqe : NULL;
114         rc = qmt_pool_lqes_lookup_spec(env, qmt, lqe_rtype(lqe),
115                                        lqe_qtype(lqe), &lqe->lqe_id);
116         if (!qti_lqes_cnt(env))
117                 GOTO(lqes_fini, rc);
118
119         if (!lqe_gl && qti_lqes_glbl(env)->lqe_is_global)
120                 lqe_gl = qti_lqes_glbl(env);
121
122         if (!lqe_gl)
123                 GOTO(lqes_fini, rc);
124
125         if (lqe_gl->lqe_glbl_data)
126                 qmt_seed_glbe(env, lqe_gl->lqe_glbl_data);
127         /* Even if slaves haven't enqueued quota lock yet,
128          * it is needed to set lqe_revoke_time in qmt_id_lock_glimpse
129          * in case of reaching qpi_least_qunit */
130         qmt_id_lock_notify(qmt, lqe_gl);
131 lqes_fini:
132         qti_lqes_fini(env);
133 }
134
135 /*
136  * Update quota settings for a given lqe.
137  *
138  * \param env        - is the environment passed by the caller
139  * \param qmt        - is the quota master target
140  * \param lqe        - is the lquota_entry for which we want to modify quota
141  *                     settings.
142  * \param hard       - is the new hard limit
143  * \param soft       - is the new soft limit
144  * \param time       - is the new grace time
145  * \param valid      - is the list of settings to change
146  * \param is_default - true for default quota setting
147  * \param is_updated - true if the lqe is updated and no need to write back
148  */
149
150 int qmt_set_with_lqe(const struct lu_env *env, struct qmt_device *qmt,
151                      struct lquota_entry *lqe, __u64 hard, __u64 soft,
152                      __u64 time, __u32 valid, bool is_default, bool is_updated)
153 {
154         struct thandle *th = NULL;
155         time64_t now = 0;
156         __u64 ver;
157         bool dirtied = false;
158         int rc = 0;
159         bool need_id_notify = false;
160         ENTRY;
161
162         /* need to write back to global quota file? */
163         if (!is_updated) {
164                 /* By default we should have here only 1 lqe,
165                  * so no allocations should be done. */
166                 if (qti_lqes_restore_init(env))
167                         GOTO(out_nolock, rc = -ENOMEM);
168                 /* allocate & start transaction with enough credits to update
169                  * quota  settings in the global index file */
170                 th = qmt_trans_start(env, lqe);
171                 if (IS_ERR(th))
172                         GOTO(out_nolock, rc = PTR_ERR(th));
173         }
174
175         now = ktime_get_real_seconds();
176
177         lqe_write_lock(lqe);
178         LQUOTA_DEBUG(lqe,
179                      "changing quota settings valid:%x hard:%llu soft:%llu time:%llu",
180                      valid, hard, soft, time);
181
182         if (is_default && lqe->lqe_id.qid_uid != 0) {
183                 LQUOTA_DEBUG(lqe, "set qid %llu to use default quota setting",
184                              lqe->lqe_id.qid_uid);
185
186                 qmt_lqe_set_default(env, lqe->lqe_site->lqs_parent, lqe, false);
187                 GOTO(quota_set, 0);
188         }
189
190         if ((valid & QIF_TIMES) != 0 && lqe->lqe_gracetime != time) {
191                 /* change time settings */
192                 lqe->lqe_gracetime = time;
193                 dirtied            = true;
194         }
195
196         if ((valid & QIF_LIMITS) != 0 &&
197             (lqe->lqe_hardlimit != hard || lqe->lqe_softlimit != soft)) {
198                 rc = qmt_validate_limits(lqe, hard, soft);
199                 if (rc)
200                         GOTO(out, rc);
201
202                 /* change quota limits */
203                 lqe->lqe_hardlimit = hard;
204                 lqe->lqe_softlimit = soft;
205
206 quota_set:
207                 /* recompute qunit in case it was never initialized */
208                 if (qmt_revalidate(env, lqe))
209                         need_id_notify = true;
210
211                 /* clear grace time */
212                 if (lqe->lqe_softlimit == 0 ||
213                     lqe->lqe_granted <= lqe->lqe_softlimit)
214                         /* no soft limit or below soft limit, let's clear grace
215                          * time */
216                         lqe->lqe_gracetime = 0;
217                 else if ((valid & QIF_TIMES) == 0)
218                         /* set grace only if user hasn't provided his own */
219                          lqe->lqe_gracetime = now + qmt_lqe_grace(lqe);
220
221                 /* change enforced status based on new parameters */
222                 if (lqe->lqe_id.qid_uid == 0 || (lqe->lqe_hardlimit == 0 &&
223                     lqe->lqe_softlimit == 0))
224                         lqe->lqe_enforced = false;
225                 else
226                         lqe->lqe_enforced = true;
227
228                 dirtied = true;
229         }
230
231         if (!is_default && lqe->lqe_is_default) {
232                 LQUOTA_DEBUG(lqe, "the qid %llu has been set quota"
233                              " explicitly, clear the default flag",
234                              lqe->lqe_id.qid_uid);
235
236                 qmt_lqe_clear_default(lqe);
237                 dirtied = true;
238         }
239
240         if (dirtied) {
241                 if (!is_updated) {
242                         /* write new quota settings to disk */
243                         rc = qmt_glb_write(env, th, lqe, LQUOTA_BUMP_VER, &ver);
244                         if (rc) {
245                                 /* restore initial quota settings */
246                                 qmt_restore(lqe, &qti_lqes_rstr(env)[0]);
247                                 GOTO(out, rc);
248                         }
249                 } else {
250                         ver = dt_version_get(env, LQE_GLB_OBJ(lqe));
251                 }
252
253                 /* compute new qunit value now that we have modified the quota
254                  * settings or clear/set edquot flag if needed */
255                 need_id_notify |= qmt_adjust_qunit(env, lqe);
256                 need_id_notify |= qmt_adjust_edquot(lqe, now);
257         }
258         EXIT;
259 out:
260         lqe_write_unlock(lqe);
261
262 out_nolock:
263         qti_lqes_restore_fini(env);
264         if (th != NULL && !IS_ERR(th))
265                 dt_trans_stop(env, qmt->qmt_child, th);
266
267         if (rc == 0 && dirtied) {
268                 qmt_glb_lock_notify(env, lqe, ver);
269                 if (lqe->lqe_id.qid_uid == 0) {
270                         struct qmt_entry_iter_data iter_data;
271
272                         LQUOTA_DEBUG(lqe, "notify all lqe with default quota");
273                         iter_data.qeid_env = env;
274                         iter_data.qeid_qmt = qmt;
275                         cfs_hash_for_each(lqe->lqe_site->lqs_hash,
276                                                qmt_entry_iter_cb, &iter_data);
277                         /* Always notify slaves with default values. Don't
278                          * care about overhead as will be sent only not changed
279                          * values(see qmt_id_lock_cb for details).*/
280                         need_id_notify = true;
281                 }
282                 /* qti_lqes_inited > 0 means we came here from another
283                  * qmt_pool_lqes_lookup(qmt_dqacq, intent_policy ...). Thus
284                  * we can't init and add new lqes to don't overwrite already
285                  * added.
286                  */
287                 if (!qti_lqes_inited(env) && need_id_notify)
288                         qmt_set_id_notify(env, qmt, lqe);
289         }
290
291         return rc;
292 }
293
294 /*
295  * Update quota settings for a given identifier.
296  *
297  * \param env        - is the environment passed by the caller
298  * \param qmt        - is the quota master target
299  * \param restype    - is the pool type, either block (i.e. LQUOTA_RES_DT) or
300  *                     inode (i.e. LQUOTA_RES_MD)
301  * \param qtype      - is the quota type
302  * \param id         - is the quota indentifier for which we want to modify
303  *                     quota settings.
304  * \param hard       - is the new hard limit
305  * \param soft       - is the new soft limit
306  * \param time       - is the new grace time
307  * \param valid      - is the list of settings to change
308  * \param is_default - true for default quota setting
309  * \param is_updated - true if the lqe is updated and no need to write back
310  */
311 static int qmt_set(const struct lu_env *env, struct qmt_device *qmt,
312                    __u8 restype, __u8 qtype, union lquota_id *id,
313                    __u64 hard, __u64 soft, __u64 time, __u32 valid,
314                    bool is_default, bool is_updated, char *pool_name)
315 {
316         struct lquota_entry *lqe;
317         int rc;
318         ENTRY;
319
320         if (pool_name && !strnlen(pool_name, LOV_MAXPOOLNAME))
321                 pool_name = NULL;
322
323         /* look-up quota entry associated with this ID */
324         lqe = qmt_pool_lqe_lookup(env, qmt, restype, qtype, id, pool_name);
325         if (IS_ERR(lqe))
326                         RETURN(PTR_ERR(lqe));
327
328         rc = qmt_set_with_lqe(env, qmt, lqe, hard, soft, time, valid,
329                               is_default, is_updated);
330         lqe_putref(lqe);
331         RETURN(rc);
332 }
333
334 /*
335  * Handle quotactl request.
336  *
337  * \param env   - is the environment passed by the caller
338  * \param ld    - is the lu device associated with the qmt
339  * \param oqctl - is the quotactl request
340  */
341 static int qmt_quotactl(const struct lu_env *env, struct lu_device *ld,
342                         struct obd_quotactl *oqctl)
343 {
344         struct qmt_thread_info *qti = qmt_info(env);
345         union lquota_id *id  = &qti->qti_id;
346         struct qmt_device *qmt = lu2qmt_dev(ld);
347         struct obd_dqblk *dqb = &oqctl->qc_dqblk;
348         char *poolname;
349         int rc = 0;
350         bool is_default = false;
351         ENTRY;
352
353         LASSERT(qmt != NULL);
354
355         if (oqctl->qc_type >= LL_MAXQUOTAS)
356                 /* invalid quota type */
357                 RETURN(-EINVAL);
358
359         poolname = LUSTRE_Q_CMD_IS_POOL(oqctl->qc_cmd) ?
360                         oqctl->qc_poolname : NULL;
361
362         switch (oqctl->qc_cmd) {
363
364         case Q_GETINFO:  /* read grace times */
365         case LUSTRE_Q_GETINFOPOOL:
366                 /* Global grace time is stored in quota settings of ID 0. */
367                 id->qid_uid = 0;
368
369                 /* read inode grace time */
370                 rc = qmt_get(env, qmt, LQUOTA_RES_MD, oqctl->qc_type, id, NULL,
371                              NULL, &oqctl->qc_dqinfo.dqi_igrace,
372                              false, poolname);
373                 /* There could be no MD pool, so try to find DT pool */
374                 if (rc && rc != -ENOENT)
375                         break;
376
377                 /* read block grace time */
378                 rc = qmt_get(env, qmt, LQUOTA_RES_DT, oqctl->qc_type, id, NULL,
379                              NULL, &oqctl->qc_dqinfo.dqi_bgrace,
380                              false, poolname);
381                 break;
382
383         case Q_SETINFO:  /* modify grace times */
384         case LUSTRE_Q_SETINFOPOOL:
385                 /* setinfo should be using dqi->dqi_valid, but lfs incorrectly
386                  * sets the valid flags in dqb->dqb_valid instead, try to live
387                  * with that ... */
388
389                 /* Global grace time is stored in quota settings of ID 0. */
390                 id->qid_uid = 0;
391
392                 if ((dqb->dqb_valid & QIF_ITIME) != 0) {
393                         /* set inode grace time */
394                         rc = qmt_set(env, qmt, LQUOTA_RES_MD, oqctl->qc_type,
395                                      id, 0, 0, oqctl->qc_dqinfo.dqi_igrace,
396                                      QIF_TIMES, false, false,
397                                      poolname);
398                         if (rc)
399                                 break;
400                 }
401
402                 if ((dqb->dqb_valid & QIF_BTIME) != 0)
403                         /* set block grace time */
404                         rc = qmt_set(env, qmt, LQUOTA_RES_DT, oqctl->qc_type,
405                                      id, 0, 0, oqctl->qc_dqinfo.dqi_bgrace,
406                                      QIF_TIMES, false, false,
407                                      poolname);
408                 break;
409
410         case LUSTRE_Q_GETDEFAULT:
411         case LUSTRE_Q_GETDEFAULT_POOL:
412                 is_default = true;
413                 /* fallthrough */
414
415         case Q_GETQUOTA: /* consult quota limit */
416         case LUSTRE_Q_GETQUOTAPOOL:
417                 /* extract quota ID from quotactl request */
418                 id->qid_uid = oqctl->qc_id;
419
420                 /* look-up inode quota settings */
421                 rc = qmt_get(env, qmt, LQUOTA_RES_MD, oqctl->qc_type, id,
422                              &dqb->dqb_ihardlimit, &dqb->dqb_isoftlimit,
423                              &dqb->dqb_itime, is_default, poolname);
424                 /* There could be no MD pool, so try to find DT pool */
425                 if (rc && rc != -ENOENT)
426                         break;
427                 else
428                         dqb->dqb_valid |= QIF_ILIMITS | QIF_ITIME;
429
430                 /* master isn't aware of actual inode usage */
431                 dqb->dqb_curinodes = 0;
432
433                 /* look-up block quota settings */
434                 rc = qmt_get(env, qmt, LQUOTA_RES_DT, oqctl->qc_type, id,
435                              &dqb->dqb_bhardlimit, &dqb->dqb_bsoftlimit,
436                              &dqb->dqb_btime, is_default, poolname);
437                 if (rc)
438                         break;
439
440                 dqb->dqb_valid |= QIF_BLIMITS | QIF_BTIME;
441                 /* master doesn't know the actual block usage */
442                 dqb->dqb_curspace = 0;
443                 break;
444
445         case LUSTRE_Q_SETDEFAULT:
446         case LUSTRE_Q_SETDEFAULT_POOL:
447                 is_default = true;
448                 /* fallthrough */
449
450         case Q_SETQUOTA: /* change quota limits */
451         case LUSTRE_Q_SETQUOTAPOOL:
452                 /* extract quota ID from quotactl request */
453                 id->qid_uid = oqctl->qc_id;
454
455                 if ((dqb->dqb_valid & QIF_IFLAGS) != 0) {
456                         /* update inode quota settings */
457                         rc = qmt_set(env, qmt, LQUOTA_RES_MD, oqctl->qc_type,
458                                      id, dqb->dqb_ihardlimit,
459                                      dqb->dqb_isoftlimit, dqb->dqb_itime,
460                                      dqb->dqb_valid & QIF_IFLAGS, is_default,
461                                      false, poolname);
462                         if (rc)
463                                 break;
464                 }
465
466                 if ((dqb->dqb_valid & QIF_BFLAGS) != 0)
467                         /* update block quota settings */
468                         rc = qmt_set(env, qmt, LQUOTA_RES_DT, oqctl->qc_type,
469                                      id, dqb->dqb_bhardlimit,
470                                      dqb->dqb_bsoftlimit, dqb->dqb_btime,
471                                      dqb->dqb_valid & QIF_BFLAGS, is_default,
472                                      false, poolname);
473                 break;
474
475         default:
476                 CERROR("%s: unsupported quotactl command: %d\n",
477                        qmt->qmt_svname, oqctl->qc_cmd);
478                 RETURN(-ENOTSUPP);
479         }
480
481         RETURN(rc);
482 }
483
484 static inline
485 void qmt_grant_lqes(const struct lu_env *env, __u64 *slv, __u64 cnt)
486 {
487         int i;
488
489         for (i = 0; i < qti_lqes_cnt(env); i++)
490                 qti_lqe_granted(env, i) += cnt;
491
492         *slv += cnt;
493 }
494
495 static inline bool qmt_lqes_can_rel(const struct lu_env *env, __u64 cnt)
496 {
497         bool can_release = true;
498         int i;
499
500         for (i = 0; i < qti_lqes_cnt(env); i++) {
501                 if (cnt > qti_lqe_granted(env, i)) {
502                         LQUOTA_ERROR(qti_lqes(env)[i],
503                                      "Can't release %llu that is larger than lqe_granted.\n",
504                                      cnt);
505                         can_release = false;
506                 }
507         }
508         return can_release;
509 }
510
511 static inline void qmt_rel_lqes(const struct lu_env *env, __u64 *slv, __u64 cnt)
512 {
513         int i;
514
515         for (i = 0; i < qti_lqes_cnt(env); i++)
516                 qti_lqe_granted(env, i) -= cnt;
517
518         *slv -= cnt;
519 }
520
521 static inline bool qmt_lqes_cannot_grant(const struct lu_env *env, __u64 cnt)
522 {
523         bool cannot_grant = false;
524         int i;
525
526         for (i = 0; i < qti_lqes_cnt(env); i++) {
527                 if (qti_lqe_hard(env, i) != 0 &&
528                     qti_lqe_granted(env, i) + cnt > qti_lqe_hard(env, i)) {
529                         cannot_grant = true;
530                         break;
531                 }
532         }
533         return cannot_grant;
534 }
535
536 static inline __u64 qmt_lqes_grant_some_quota(const struct lu_env *env)
537 {
538         __u64 min_count, tmp;
539         bool flag = false;
540         int i;
541
542         for (i = 0, min_count = 0; i < qti_lqes_cnt(env); i++) {
543                 if (!qti_lqes(env)[i]->lqe_enforced &&
544                     !qti_lqes(env)[i]->lqe_is_global)
545                         continue;
546
547                 tmp = qti_lqe_hard(env, i) - qti_lqe_granted(env, i);
548                 if (flag) {
549                         min_count = tmp < min_count ? tmp : min_count;
550                 } else {
551                         flag = true;
552                         min_count = tmp;
553                 }
554         }
555         return min_count;
556 }
557
558 static inline __u64 qmt_lqes_alloc_expand(const struct lu_env *env,
559                                           __u64 slv_granted, __u64 spare)
560 {
561         __u64 min_count, tmp;
562         bool flag = false;
563         int i;
564
565         for (i = 0, min_count = 0; i < qti_lqes_cnt(env); i++) {
566                 /* Don't take into account not enforced lqes that belong
567                  * to non global pool. These lqes present in array to
568                  * support actual lqe_granted even for lqes without limits. */
569                 if (!qti_lqes(env)[i]->lqe_enforced &&
570                     !qti_lqes(env)[i]->lqe_is_global)
571                         continue;
572
573                 tmp = qmt_alloc_expand(qti_lqes(env)[i], slv_granted, spare);
574                 if (flag) {
575                         min_count = tmp < min_count ? tmp : min_count;
576                 } else {
577                         flag = true;
578                         min_count = tmp;
579                 }
580         }
581         return min_count;
582 }
583
584 static inline void qmt_lqes_tune_grace(const struct lu_env *env, __u64 now)
585 {
586         int i;
587
588         for (i = 0; i < qti_lqes_cnt(env); i++) {
589                 struct lquota_entry *lqe;
590
591                 lqe = qti_lqes(env)[i];
592                 if (lqe->lqe_softlimit != 0) {
593                         if (lqe->lqe_granted > lqe->lqe_softlimit &&
594                             lqe->lqe_gracetime == 0) {
595                                 /* First time over soft limit, let's start grace
596                                  * timer */
597                                 lqe->lqe_gracetime = now + qmt_lqe_grace(lqe);
598                         } else if (lqe->lqe_granted <= lqe->lqe_softlimit &&
599                                    lqe->lqe_gracetime != 0) {
600                                 /* Clear grace timer */
601                                 lqe->lqe_gracetime = 0;
602                         }
603                 }
604         }
605 }
606
607 /*
608  * Helper function to handle quota request from slave.
609  *
610  * \param env     - is the environment passed by the caller
611  * \param qmt     - is the master device
612  * \param uuid    - is the uuid associated with the slave
613  * \param qb_flags - are the quota request flags as packed in the quota_body
614  * \param qb_count - is the amount of quota space the slave wants to
615  *                   acquire/release
616  * \param qb_usage - is the current space usage on the slave
617  * \param repbody - is the quota_body of reply
618  *
619  * \retval 0            : success
620  * \retval -EDQUOT      : out of quota
621  *         -EINPROGRESS : inform client to retry write/create
622  *         -ve          : other appropriate errors
623  */
624 int qmt_dqacq0(const struct lu_env *env, struct qmt_device *qmt,
625                struct obd_uuid *uuid, __u32 qb_flags, __u64 qb_count,
626                __u64 qb_usage, struct quota_body *repbody)
627 {
628         __u64                    now, count;
629         struct dt_object        *slv_obj = NULL;
630         __u64                    slv_granted, slv_granted_bck;
631         struct thandle          *th = NULL;
632         int                      rc, ret;
633         struct lquota_entry *lqe = qti_lqes_glbl(env);
634         ENTRY;
635
636         LASSERT(uuid != NULL);
637
638         /* initialize reply */
639         memset(repbody, 0, sizeof(*repbody));
640         memcpy(&repbody->qb_id, &lqe->lqe_id, sizeof(repbody->qb_id));
641
642         if (OBD_FAIL_CHECK(OBD_FAIL_QUOTA_RECOVERABLE_ERR))
643                 RETURN(-cfs_fail_val);
644
645         if (qti_lqes_restore_init(env))
646                 RETURN(-ENOMEM);
647
648         /* look-up index file associated with acquiring slave */
649         slv_obj = lquota_disk_slv_find(env, qmt->qmt_child, LQE_ROOT(lqe),
650                                        lu_object_fid(&LQE_GLB_OBJ(lqe)->do_lu),
651                                        uuid);
652         if (IS_ERR(slv_obj))
653                 GOTO(out, rc = PTR_ERR(slv_obj));
654
655         /* pack slave fid in reply just for sanity check */
656         memcpy(&repbody->qb_slv_fid, lu_object_fid(&slv_obj->do_lu),
657                sizeof(struct lu_fid));
658
659         /* allocate & start transaction with enough credits to update
660          * global & slave indexes */
661         th = qmt_trans_start_with_slv(env, NULL, slv_obj, false);
662         if (IS_ERR(th))
663                 GOTO(out, rc = PTR_ERR(th));
664
665         qti_lqes_write_lock(env);
666
667         LQUOTA_DEBUG_LQES(env, "dqacq starts uuid:%s flags:0x%x wanted:%llu"
668                      " usage:%llu", obd_uuid2str(uuid), qb_flags, qb_count,
669                      qb_usage);
670
671         /* Legal race, limits have been removed on master, but slave didn't
672          * receive the change yet. Just return EINPROGRESS until the slave gets
673          * notified. */
674         if (!lqe->lqe_enforced && !req_is_rel(qb_flags))
675                 GOTO(out_locked, rc = -ESRCH);
676
677         /* recompute qunit in case it was never initialized */
678         qmt_revalidate_lqes(env, qmt, qb_flags);
679
680         /* slave just wants to acquire per-ID lock */
681         if (req_is_acq(qb_flags) && qb_count == 0)
682                 GOTO(out_locked, rc = 0);
683
684         /* fetch how much quota space is already granted to this slave */
685         rc = qmt_slv_read(env, &lqe->lqe_id, slv_obj, &slv_granted);
686         if (rc) {
687                 LQUOTA_ERROR(lqe, "Failed to get granted for slave %s, rc=%d",
688                              obd_uuid2str(uuid), rc);
689                 GOTO(out_locked, rc);
690         }
691         /* recall how much space this slave currently owns in order to restore
692          * it in case of failure */
693         slv_granted_bck = slv_granted;
694
695         /* record current time for soft limit & grace time management */
696         now = ktime_get_real_seconds();
697
698         if (req_is_rel(qb_flags)) {
699                 /* Slave would like to release quota space */
700                 if (slv_granted < qb_count ||
701                     !qmt_lqes_can_rel(env, qb_count)) {
702                         /* can't release more than granted */
703                         LQUOTA_ERROR_LQES(env,
704                                           "Release too much! uuid:%s release: %llu granted:%llu, total:%llu",
705                                           obd_uuid2str(uuid), qb_count,
706                                           slv_granted, lqe->lqe_granted);
707                         GOTO(out_locked, rc = -EINVAL);
708                 }
709
710                 repbody->qb_count = qb_count;
711                 /* put released space back to global pool */
712                 qmt_rel_lqes(env, &slv_granted, qb_count);
713                 GOTO(out_write, rc = 0);
714         }
715
716         if (req_has_rep(qb_flags) && slv_granted < qb_usage) {
717                 /* Slave is reporting space usage in quota request and it turns
718                  * out to be using more quota space than owned, so we adjust
719                  * granted space regardless of the current state of affairs */
720                 repbody->qb_count = qb_usage - slv_granted;
721                 qmt_grant_lqes(env, &slv_granted, repbody->qb_count);
722         }
723
724         if (!req_is_acq(qb_flags) && !req_is_preacq(qb_flags))
725                 GOTO(out_write, rc = 0);
726
727         qmt_adjust_edquot_notify(env, qmt, now, qb_flags);
728         if (qti_lqes_edquot(env))
729                 /* no hope to claim further space back */
730                 GOTO(out_write, rc = -EDQUOT);
731
732         if (qmt_space_exhausted_lqes(env, now)) {
733                 /* might have some free space once rebalancing is completed */
734                 rc = req_is_acq(qb_flags) ? -EINPROGRESS : -EDQUOT;
735                 GOTO(out_write, rc);
736         }
737
738         if (req_is_preacq(qb_flags)) {
739                 /* slave would like to pre-acquire quota space. To do so, it
740                  * reports in qb_count how much spare quota space it owns and we
741                  * can grant back quota space which is consistent with qunit
742                  * value. */
743                 if (qb_count >= qti_lqes_min_qunit(env))
744                         /* slave already own the maximum it should */
745                         GOTO(out_write, rc = 0);
746
747                 count = qmt_lqes_alloc_expand(env, slv_granted, qb_count);
748                 if (count == 0)
749                         GOTO(out_write, rc = -EDQUOT);
750
751                 repbody->qb_count += count;
752                 qmt_grant_lqes(env, &slv_granted, count);
753                 GOTO(out_write, rc = 0);
754         }
755
756         /* processing acquire request with clients waiting */
757         if (qmt_lqes_cannot_grant(env, qb_count)) {
758                 /* cannot grant as much as asked, but can still afford to grant
759                  * some quota space back */
760                 count = qmt_lqes_grant_some_quota(env);
761                 repbody->qb_count += count;
762                 qmt_grant_lqes(env, &slv_granted, count);
763                 GOTO(out_write, rc = 0);
764         }
765
766         /* Whouhou! we can satisfy the slave request! */
767         repbody->qb_count += qb_count;
768         qmt_grant_lqes(env, &slv_granted, qb_count);
769
770         /* Try to expand the acquired count for DQACQ */
771         count = qmt_lqes_alloc_expand(env, slv_granted, 0);
772         if (count != 0) {
773                 /* can even grant more than asked, it is like xmas ... */
774                 repbody->qb_count += count;
775                 qmt_grant_lqes(env, &slv_granted, count);
776                 GOTO(out_write, rc = 0);
777         }
778
779         GOTO(out_write, rc = 0);
780 out_write:
781         if (repbody->qb_count == 0)
782                 GOTO(out_locked, rc);
783
784         /* start/stop grace timer if required */
785         qmt_lqes_tune_grace(env, now);
786
787         /* Update slave index first since it is easier to roll back */
788         ret = qmt_slv_write(env, th, lqe, slv_obj, LQUOTA_BUMP_VER,
789                             &repbody->qb_slv_ver, slv_granted);
790         if (ret) {
791                 /* restore initial quota settings */
792                 qmt_restore_lqes(env);
793                 /* reset qb_count */
794                 repbody->qb_count = 0;
795                 GOTO(out_locked, rc = ret);
796         }
797
798         /* Update global index, no version bump needed */
799         ret = qmt_glb_write_lqes(env, th, 0, NULL);
800         if (ret) {
801                 rc = ret;
802                 /* restore initial quota settings */
803                 qmt_restore_lqes(env);
804                 /* reset qb_count */
805                 repbody->qb_count = 0;
806
807                 /* restore previous granted value */
808                 ret = qmt_slv_write(env, th, lqe, slv_obj, 0, NULL,
809                                     slv_granted_bck);
810                 if (ret) {
811                         LQUOTA_ERROR(lqe, "failed to restore initial slave "
812                                      "value rc:%d ret%d", rc, ret);
813                         LBUG();
814                 }
815                 qmt_adjust_edquot_notify(env, qmt, now, qb_flags);
816                 GOTO(out_locked, rc);
817         }
818
819         /* Total granted has been changed, let's try to adjust the qunit
820          * size according to the total granted & limits. */
821
822         /* clear/set edquot flag and notify slaves via glimpse if needed */
823         qmt_adjust_and_notify(env, qmt, now, qb_flags);
824 out_locked:
825         LQUOTA_DEBUG_LQES(env, "dqacq ends count:%llu ver:%llu rc:%d",
826                      repbody->qb_count, repbody->qb_slv_ver, rc);
827         qti_lqes_write_unlock(env);
828 out:
829         qti_lqes_restore_fini(env);
830
831         if (th != NULL && !IS_ERR(th))
832                 dt_trans_stop(env, qmt->qmt_child, th);
833
834         if (slv_obj != NULL && !IS_ERR(slv_obj))
835                 dt_object_put(env, slv_obj);
836
837         if ((req_is_acq(qb_flags) || req_is_preacq(qb_flags)) &&
838             OBD_FAIL_CHECK(OBD_FAIL_QUOTA_EDQUOT)) {
839                 /* introduce inconsistency between granted value in slave index
840                  * and slave index copy of slave */
841                 repbody->qb_count = 0;
842                 rc = -EDQUOT;
843         }
844
845         RETURN(rc);
846 }
847
848 /*
849  * Extract index from uuid or quota index file name.
850  *
851  * \param[in] uuid      uuid or quota index name(0x1020000-OST0001_UUID)
852  * \param[out] idx      pointer to save index
853  *
854  * \retval              slave type(QMT_STYPE_MDT or QMT_STYPE_OST)
855  * \retval -EINVAL      wrong uuid
856  */
857 int qmt_uuid2idx(struct obd_uuid *uuid, int *idx)
858 {
859         char *uuid_str, *name, *dash;
860         int rc = -EINVAL;
861
862         uuid_str = (char *)uuid->uuid;
863
864         if (strnlen(uuid_str, UUID_MAX) >= UUID_MAX) {
865                 CERROR("quota: UUID '%.*s' missing trailing NUL: rc = %d\n",
866                        UUID_MAX, uuid_str, rc);
867                 return rc;
868         }
869
870         dash = strrchr(uuid_str, '-');
871         name = dash + 1;
872         /* Going to get index from MDTXXXX/OSTXXXX. Thus uuid should
873          * have at least 8 bytes after '-': 3 for MDT/OST, 4 for index
874          * and 1 byte for null character. */
875         if (*dash != '-' || ((uuid_str + UUID_MAX - name) < 8)) {
876                 CERROR("quota: wrong UUID format '%s': rc = %d\n",
877                        uuid_str, rc);
878                 return rc;
879         }
880
881         rc = target_name2index(name, idx, NULL);
882         switch (rc) {
883         case LDD_F_SV_TYPE_MDT:
884                 rc = QMT_STYPE_MDT;
885                 break;
886         case LDD_F_SV_TYPE_OST:
887                 rc = QMT_STYPE_OST;
888                 break;
889         default:
890                 CERROR("quota: wrong UUID type '%s': rc = %d\n", uuid_str, rc);
891                 rc = -EINVAL;
892         }
893
894         RETURN(rc);
895 }
896
897 /*
898  * Handle quota request from slave.
899  *
900  * \param env  - is the environment passed by the caller
901  * \param ld   - is the lu device associated with the qmt
902  * \param req  - is the quota acquire request
903  */
904 static int qmt_dqacq(const struct lu_env *env, struct lu_device *ld,
905                      struct ptlrpc_request *req)
906 {
907         struct qmt_device *qmt = lu2qmt_dev(ld);
908         struct quota_body *qbody, *repbody;
909         struct obd_uuid *uuid;
910         struct ldlm_lock *lock;
911         int rtype, qtype;
912         int rc, idx, stype;
913         ENTRY;
914
915         qbody = req_capsule_client_get(&req->rq_pill, &RMF_QUOTA_BODY);
916         if (qbody == NULL)
917                 RETURN(err_serious(-EPROTO));
918
919         repbody = req_capsule_server_get(&req->rq_pill, &RMF_QUOTA_BODY);
920         if (repbody == NULL)
921                 RETURN(err_serious(-EFAULT));
922
923         /* verify if global lock is stale */
924         if (!lustre_handle_is_used(&qbody->qb_glb_lockh))
925                 RETURN(-ENOLCK);
926
927         lock = ldlm_handle2lock(&qbody->qb_glb_lockh);
928         if (lock == NULL)
929                 RETURN(-ENOLCK);
930         LDLM_LOCK_PUT(lock);
931
932         uuid = &req->rq_export->exp_client_uuid;
933         stype = qmt_uuid2idx(uuid, &idx);
934         if (stype < 0)
935                 RETURN(stype);
936
937         if (req_is_rel(qbody->qb_flags) + req_is_acq(qbody->qb_flags) +
938             req_is_preacq(qbody->qb_flags) > 1) {
939                 CERROR("%s: malformed quota request with conflicting flags set "
940                        "(%x) from slave %s\n", qmt->qmt_svname,
941                        qbody->qb_flags, obd_uuid2str(uuid));
942                 RETURN(-EPROTO);
943         }
944
945         if (req_is_acq(qbody->qb_flags) || req_is_preacq(qbody->qb_flags)) {
946                 /* acquire and pre-acquire should use a valid ID lock */
947
948                 if (!lustre_handle_is_used(&qbody->qb_lockh))
949                         RETURN(-ENOLCK);
950
951                 lock = ldlm_handle2lock(&qbody->qb_lockh);
952                 if (lock == NULL)
953                         /* no lock associated with this handle */
954                         RETURN(-ENOLCK);
955
956                 LDLM_DEBUG(lock, "%sacquire request",
957                            req_is_preacq(qbody->qb_flags) ? "pre" : "");
958
959                 if (!obd_uuid_equals(&lock->l_export->exp_client_uuid, uuid)) {
960                         /* sorry, no way to cheat ... */
961                         LDLM_LOCK_PUT(lock);
962                         RETURN(-ENOLCK);
963                 }
964
965                 if (ldlm_is_ast_sent(lock)) {
966                         struct ptlrpc_service_part *svc;
967                         timeout_t timeout;
968
969                         svc = req->rq_rqbd->rqbd_svcpt;
970                         timeout = at_est2timeout(at_get(&svc->scp_at_estimate));
971                         timeout += (ldlm_bl_timeout(lock) >> 1);
972
973                         /* lock is being cancelled, prolong timeout */
974                         ldlm_refresh_waiting_lock(lock, timeout);
975                 }
976                 LDLM_LOCK_PUT(lock);
977         }
978
979         /* extract quota information from global index FID packed in the
980          * request */
981         rc = lquota_extract_fid(&qbody->qb_fid, &rtype, &qtype);
982         if (rc)
983                 RETURN(-EINVAL);
984
985         /* Find the quota entry associated with the quota id */
986         rc = qmt_pool_lqes_lookup(env, qmt, rtype, stype, qtype,
987                                   &qbody->qb_id, NULL, idx);
988         if (rc)
989                 RETURN(rc);
990
991         rc = qmt_dqacq0(env, qmt, uuid, qbody->qb_flags,
992                         qbody->qb_count, qbody->qb_usage, repbody);
993
994         if (lustre_handle_is_used(&qbody->qb_lockh))
995                 /* return current qunit value only to slaves owning an per-ID
996                  * quota lock. For enqueue, the qunit value will be returned in
997                  * the LVB */
998                 repbody->qb_qunit = qti_lqes_min_qunit(env);
999         CDEBUG(D_QUOTA, "qmt_dqacq return qb_qunit %llu qb_count %llu\n",
1000                repbody->qb_qunit, repbody->qb_count);
1001         qti_lqes_fini(env);
1002         RETURN(rc);
1003 }
1004
1005 /* Vector of quota request handlers. This vector is used by the MDT to forward
1006  * requests to the quota master. */
1007 struct qmt_handlers qmt_hdls = {
1008         /* quota request handlers */
1009         .qmth_quotactl          = qmt_quotactl,
1010         .qmth_dqacq             = qmt_dqacq,
1011
1012         /* ldlm handlers */
1013         .qmth_intent_policy     = qmt_intent_policy,
1014         .qmth_lvbo_init         = qmt_lvbo_init,
1015         .qmth_lvbo_update       = qmt_lvbo_update,
1016         .qmth_lvbo_size         = qmt_lvbo_size,
1017         .qmth_lvbo_fill         = qmt_lvbo_fill,
1018         .qmth_lvbo_free         = qmt_lvbo_free,
1019 };
1020 EXPORT_SYMBOL(qmt_hdls);