Whamcloud - gitweb
2ed9051e45df87d03c59e514be666ee643d52014
[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                         if (lqe->lqe_enforced) {
225                                 lqe->lqe_enforced = false;
226                                 /* Clear qunit and edquot as lqe_adjust_edquot
227                                  * does not handle not enforced lqes */
228                                 lqe->lqe_edquot = 0;
229                                 lqe->lqe_qunit = 0;
230                                 need_id_notify = true;
231                         }
232                 } else {
233                         lqe->lqe_enforced = true;
234                 }
235
236                 dirtied = true;
237         }
238
239         if (!is_default && lqe->lqe_is_default) {
240                 LQUOTA_DEBUG(lqe, "the qid %llu has been set quota"
241                              " explicitly, clear the default flag",
242                              lqe->lqe_id.qid_uid);
243
244                 qmt_lqe_clear_default(lqe);
245                 dirtied = true;
246         }
247
248         if (dirtied) {
249                 if (!is_updated) {
250                         /* write new quota settings to disk */
251                         rc = qmt_glb_write(env, th, lqe, LQUOTA_BUMP_VER, &ver);
252                         if (rc) {
253                                 /* restore initial quota settings */
254                                 qmt_restore(lqe, &qti_lqes_rstr(env)[0]);
255                                 GOTO(out, rc);
256                         }
257                 } else {
258                         ver = dt_version_get(env, LQE_GLB_OBJ(lqe));
259                 }
260
261                 /* compute new qunit value now that we have modified the quota
262                  * settings or clear/set edquot flag if needed */
263                 need_id_notify |= qmt_adjust_qunit(env, lqe);
264                 need_id_notify |= qmt_adjust_edquot(lqe, now);
265         }
266         EXIT;
267 out:
268         lqe_write_unlock(lqe);
269
270 out_nolock:
271         if (!is_updated) {
272                 if (th != NULL && !IS_ERR(th))
273                         dt_trans_stop(env, qmt->qmt_child, th);
274                 qti_lqes_restore_fini(env);
275         }
276
277         if (rc == 0 && dirtied) {
278                 qmt_glb_lock_notify(env, lqe, ver);
279                 if (lqe->lqe_id.qid_uid == 0) {
280                         struct qmt_entry_iter_data iter_data;
281
282                         LQUOTA_DEBUG(lqe, "notify all lqe with default quota");
283                         iter_data.qeid_env = env;
284                         iter_data.qeid_qmt = qmt;
285                         cfs_hash_for_each(lqe->lqe_site->lqs_hash,
286                                                qmt_entry_iter_cb, &iter_data);
287                         /* Always notify slaves with default values. Don't
288                          * care about overhead as will be sent only not changed
289                          * values(see qmt_id_lock_cb for details).*/
290                         need_id_notify = true;
291                 }
292                 /* qti_lqes_inited > 0 means we came here from another
293                  * qmt_pool_lqes_lookup(qmt_dqacq, intent_policy ...). Thus
294                  * we can't init and add new lqes to don't overwrite already
295                  * added.
296                  */
297                 if (!qti_lqes_inited(env) && need_id_notify)
298                         qmt_set_id_notify(env, qmt, lqe);
299         }
300
301         return rc;
302 }
303
304 /*
305  * Update quota settings for a given identifier.
306  *
307  * \param env        - is the environment passed by the caller
308  * \param qmt        - is the quota master target
309  * \param restype    - is the pool type, either block (i.e. LQUOTA_RES_DT) or
310  *                     inode (i.e. LQUOTA_RES_MD)
311  * \param qtype      - is the quota type
312  * \param id         - is the quota indentifier for which we want to modify
313  *                     quota settings.
314  * \param hard       - is the new hard limit
315  * \param soft       - is the new soft limit
316  * \param time       - is the new grace time
317  * \param valid      - is the list of settings to change
318  * \param is_default - true for default quota setting
319  * \param is_updated - true if the lqe is updated and no need to write back
320  */
321 static int qmt_set(const struct lu_env *env, struct qmt_device *qmt,
322                    __u8 restype, __u8 qtype, union lquota_id *id,
323                    __u64 hard, __u64 soft, __u64 time, __u32 valid,
324                    bool is_default, bool is_updated, char *pool_name)
325 {
326         struct lquota_entry *lqe;
327         int rc;
328         ENTRY;
329
330         if (pool_name && !strnlen(pool_name, LOV_MAXPOOLNAME))
331                 pool_name = NULL;
332
333         /* look-up quota entry associated with this ID */
334         lqe = qmt_pool_lqe_lookup(env, qmt, restype, qtype, id, pool_name);
335         if (IS_ERR(lqe))
336                         RETURN(PTR_ERR(lqe));
337
338         rc = qmt_set_with_lqe(env, qmt, lqe, hard, soft, time, valid,
339                               is_default, is_updated);
340         lqe_putref(lqe);
341         RETURN(rc);
342 }
343
344 /*
345  * Handle quotactl request.
346  *
347  * \param env   - is the environment passed by the caller
348  * \param ld    - is the lu device associated with the qmt
349  * \param oqctl - is the quotactl request
350  */
351 static int qmt_quotactl(const struct lu_env *env, struct lu_device *ld,
352                         struct obd_quotactl *oqctl)
353 {
354         struct qmt_thread_info *qti = qmt_info(env);
355         union lquota_id *id  = &qti->qti_id;
356         struct qmt_device *qmt = lu2qmt_dev(ld);
357         struct obd_dqblk *dqb = &oqctl->qc_dqblk;
358         char *poolname;
359         int rc = 0;
360         bool is_default = false;
361         ENTRY;
362
363         LASSERT(qmt != NULL);
364
365         if (oqctl->qc_type >= LL_MAXQUOTAS)
366                 /* invalid quota type */
367                 RETURN(-EINVAL);
368
369         poolname = LUSTRE_Q_CMD_IS_POOL(oqctl->qc_cmd) ?
370                         oqctl->qc_poolname : NULL;
371
372         switch (oqctl->qc_cmd) {
373
374         case Q_GETINFO:  /* read grace times */
375         case LUSTRE_Q_GETINFOPOOL:
376                 /* Global grace time is stored in quota settings of ID 0. */
377                 id->qid_uid = 0;
378
379                 /* read inode grace time */
380                 rc = qmt_get(env, qmt, LQUOTA_RES_MD, oqctl->qc_type, id, NULL,
381                              NULL, &oqctl->qc_dqinfo.dqi_igrace,
382                              false, poolname);
383                 /* There could be no MD pool, so try to find DT pool */
384                 if (rc && rc != -ENOENT)
385                         break;
386
387                 /* read block grace time */
388                 rc = qmt_get(env, qmt, LQUOTA_RES_DT, oqctl->qc_type, id, NULL,
389                              NULL, &oqctl->qc_dqinfo.dqi_bgrace,
390                              false, poolname);
391                 break;
392
393         case Q_SETINFO:  /* modify grace times */
394         case LUSTRE_Q_SETINFOPOOL:
395                 /* setinfo should be using dqi->dqi_valid, but lfs incorrectly
396                  * sets the valid flags in dqb->dqb_valid instead, try to live
397                  * with that ... */
398
399                 /* Global grace time is stored in quota settings of ID 0. */
400                 id->qid_uid = 0;
401
402                 if ((dqb->dqb_valid & QIF_ITIME) != 0) {
403                         /* set inode grace time */
404                         rc = qmt_set(env, qmt, LQUOTA_RES_MD, oqctl->qc_type,
405                                      id, 0, 0, oqctl->qc_dqinfo.dqi_igrace,
406                                      QIF_TIMES, false, false,
407                                      poolname);
408                         if (rc)
409                                 break;
410                 }
411
412                 if ((dqb->dqb_valid & QIF_BTIME) != 0)
413                         /* set block grace time */
414                         rc = qmt_set(env, qmt, LQUOTA_RES_DT, oqctl->qc_type,
415                                      id, 0, 0, oqctl->qc_dqinfo.dqi_bgrace,
416                                      QIF_TIMES, false, false,
417                                      poolname);
418                 break;
419
420         case LUSTRE_Q_GETDEFAULT:
421         case LUSTRE_Q_GETDEFAULT_POOL:
422                 is_default = true;
423                 /* fallthrough */
424
425         case Q_GETQUOTA: /* consult quota limit */
426         case LUSTRE_Q_GETQUOTAPOOL:
427                 /* extract quota ID from quotactl request */
428                 id->qid_uid = oqctl->qc_id;
429
430                 /* look-up inode quota settings */
431                 rc = qmt_get(env, qmt, LQUOTA_RES_MD, oqctl->qc_type, id,
432                              &dqb->dqb_ihardlimit, &dqb->dqb_isoftlimit,
433                              &dqb->dqb_itime, is_default, poolname);
434                 /* There could be no MD pool, so try to find DT pool */
435                 if (rc && rc != -ENOENT)
436                         break;
437                 else
438                         dqb->dqb_valid |= QIF_ILIMITS | QIF_ITIME;
439
440                 /* master isn't aware of actual inode usage */
441                 dqb->dqb_curinodes = 0;
442
443                 /* look-up block quota settings */
444                 rc = qmt_get(env, qmt, LQUOTA_RES_DT, oqctl->qc_type, id,
445                              &dqb->dqb_bhardlimit, &dqb->dqb_bsoftlimit,
446                              &dqb->dqb_btime, is_default, poolname);
447                 if (rc)
448                         break;
449
450                 dqb->dqb_valid |= QIF_BLIMITS | QIF_BTIME;
451                 /* master doesn't know the actual block usage */
452                 dqb->dqb_curspace = 0;
453                 break;
454
455         case LUSTRE_Q_SETDEFAULT:
456         case LUSTRE_Q_SETDEFAULT_POOL:
457                 is_default = true;
458                 /* fallthrough */
459
460         case Q_SETQUOTA: /* change quota limits */
461         case LUSTRE_Q_SETQUOTAPOOL:
462                 /* extract quota ID from quotactl request */
463                 id->qid_uid = oqctl->qc_id;
464
465                 if ((dqb->dqb_valid & QIF_IFLAGS) != 0) {
466                         /* update inode quota settings */
467                         rc = qmt_set(env, qmt, LQUOTA_RES_MD, oqctl->qc_type,
468                                      id, dqb->dqb_ihardlimit,
469                                      dqb->dqb_isoftlimit, dqb->dqb_itime,
470                                      dqb->dqb_valid & QIF_IFLAGS, is_default,
471                                      false, poolname);
472                         if (rc)
473                                 break;
474                 }
475
476                 if ((dqb->dqb_valid & QIF_BFLAGS) != 0)
477                         /* update block quota settings */
478                         rc = qmt_set(env, qmt, LQUOTA_RES_DT, oqctl->qc_type,
479                                      id, dqb->dqb_bhardlimit,
480                                      dqb->dqb_bsoftlimit, dqb->dqb_btime,
481                                      dqb->dqb_valid & QIF_BFLAGS, is_default,
482                                      false, poolname);
483                 break;
484
485         default:
486                 CERROR("%s: unsupported quotactl command: %d\n",
487                        qmt->qmt_svname, oqctl->qc_cmd);
488                 RETURN(-ENOTSUPP);
489         }
490
491         RETURN(rc);
492 }
493
494 static inline
495 void qmt_grant_lqes(const struct lu_env *env, __u64 *slv, __u64 cnt)
496 {
497         int i;
498
499         for (i = 0; i < qti_lqes_cnt(env); i++)
500                 qti_lqe_granted(env, i) += cnt;
501
502         *slv += cnt;
503 }
504
505 static inline bool qmt_lqes_can_rel(const struct lu_env *env, __u64 cnt)
506 {
507         bool can_release = true;
508         int i;
509
510         for (i = 0; i < qti_lqes_cnt(env); i++) {
511                 if (cnt > qti_lqe_granted(env, i)) {
512                         LQUOTA_ERROR(qti_lqes(env)[i],
513                                      "Can't release %llu that is larger than lqe_granted.\n",
514                                      cnt);
515                         can_release = false;
516                 }
517         }
518         return can_release;
519 }
520
521 static inline void qmt_rel_lqes(const struct lu_env *env, __u64 *slv, __u64 cnt)
522 {
523         int i;
524
525         for (i = 0; i < qti_lqes_cnt(env); i++)
526                 qti_lqe_granted(env, i) -= cnt;
527
528         *slv -= cnt;
529 }
530
531 static inline bool qmt_lqes_cannot_grant(const struct lu_env *env, __u64 cnt)
532 {
533         bool cannot_grant = false;
534         int i;
535
536         for (i = 0; i < qti_lqes_cnt(env); i++) {
537                 if (qti_lqe_hard(env, i) != 0 &&
538                     qti_lqe_granted(env, i) + cnt > qti_lqe_hard(env, i)) {
539                         cannot_grant = true;
540                         break;
541                 }
542         }
543         return cannot_grant;
544 }
545
546 static inline __u64 qmt_lqes_grant_some_quota(const struct lu_env *env)
547 {
548         __u64 min_count, tmp;
549         bool flag = false;
550         int i;
551
552         for (i = 0, min_count = 0; i < qti_lqes_cnt(env); i++) {
553                 if (!qti_lqes(env)[i]->lqe_enforced &&
554                     !qti_lqes(env)[i]->lqe_is_global)
555                         continue;
556
557                 tmp = qti_lqe_hard(env, i) - qti_lqe_granted(env, i);
558                 if (flag) {
559                         min_count = tmp < min_count ? tmp : min_count;
560                 } else {
561                         flag = true;
562                         min_count = tmp;
563                 }
564         }
565         return min_count;
566 }
567
568 static inline __u64 qmt_lqes_alloc_expand(const struct lu_env *env,
569                                           __u64 slv_granted, __u64 spare)
570 {
571         __u64 min_count, tmp;
572         bool flag = false;
573         int i;
574
575         for (i = 0, min_count = 0; i < qti_lqes_cnt(env); i++) {
576                 /* Don't take into account not enforced lqes that belong
577                  * to non global pool. These lqes present in array to
578                  * support actual lqe_granted even for lqes without limits. */
579                 if (!qti_lqes(env)[i]->lqe_enforced &&
580                     !qti_lqes(env)[i]->lqe_is_global)
581                         continue;
582
583                 tmp = qmt_alloc_expand(qti_lqes(env)[i], slv_granted, spare);
584                 if (flag) {
585                         min_count = tmp < min_count ? tmp : min_count;
586                 } else {
587                         flag = true;
588                         min_count = tmp;
589                 }
590         }
591         return min_count;
592 }
593
594 static inline void qmt_lqes_tune_grace(const struct lu_env *env, __u64 now)
595 {
596         int i;
597
598         for (i = 0; i < qti_lqes_cnt(env); i++) {
599                 struct lquota_entry *lqe;
600
601                 lqe = qti_lqes(env)[i];
602                 if (lqe->lqe_softlimit != 0) {
603                         if (lqe->lqe_granted > lqe->lqe_softlimit &&
604                             lqe->lqe_gracetime == 0) {
605                                 /* First time over soft limit, let's start grace
606                                  * timer */
607                                 lqe->lqe_gracetime = now + qmt_lqe_grace(lqe);
608                         } else if (lqe->lqe_granted <= lqe->lqe_softlimit &&
609                                    lqe->lqe_gracetime != 0) {
610                                 /* Clear grace timer */
611                                 lqe->lqe_gracetime = 0;
612                         }
613                 }
614         }
615 }
616
617 /*
618  * Helper function to handle quota request from slave.
619  *
620  * \param env     - is the environment passed by the caller
621  * \param qmt     - is the master device
622  * \param uuid    - is the uuid associated with the slave
623  * \param qb_flags - are the quota request flags as packed in the quota_body
624  * \param qb_count - is the amount of quota space the slave wants to
625  *                   acquire/release
626  * \param qb_usage - is the current space usage on the slave
627  * \param repbody - is the quota_body of reply
628  *
629  * \retval 0            : success
630  * \retval -EDQUOT      : out of quota
631  *         -EINPROGRESS : inform client to retry write/create
632  *         -ve          : other appropriate errors
633  */
634 int qmt_dqacq0(const struct lu_env *env, struct qmt_device *qmt,
635                struct obd_uuid *uuid, __u32 qb_flags, __u64 qb_count,
636                __u64 qb_usage, struct quota_body *repbody)
637 {
638         __u64                    now, count;
639         struct dt_object        *slv_obj = NULL;
640         __u64                    slv_granted, slv_granted_bck;
641         struct thandle          *th = NULL;
642         int                      rc, ret;
643         struct lquota_entry *lqe = qti_lqes_glbl(env);
644         ENTRY;
645
646         LASSERT(uuid != NULL);
647
648         /* initialize reply */
649         memset(repbody, 0, sizeof(*repbody));
650         memcpy(&repbody->qb_id, &lqe->lqe_id, sizeof(repbody->qb_id));
651
652         if (OBD_FAIL_CHECK(OBD_FAIL_QUOTA_RECOVERABLE_ERR))
653                 RETURN(-cfs_fail_val);
654
655         if (qti_lqes_restore_init(env))
656                 RETURN(-ENOMEM);
657
658         /* look-up index file associated with acquiring slave */
659         slv_obj = lquota_disk_slv_find(env, qmt->qmt_child, LQE_ROOT(lqe),
660                                        lu_object_fid(&LQE_GLB_OBJ(lqe)->do_lu),
661                                        uuid);
662         if (IS_ERR(slv_obj))
663                 GOTO(out, rc = PTR_ERR(slv_obj));
664
665         /* pack slave fid in reply just for sanity check */
666         memcpy(&repbody->qb_slv_fid, lu_object_fid(&slv_obj->do_lu),
667                sizeof(struct lu_fid));
668
669         /* allocate & start transaction with enough credits to update
670          * global & slave indexes */
671         th = qmt_trans_start_with_slv(env, NULL, slv_obj, false);
672         if (IS_ERR(th))
673                 GOTO(out, rc = PTR_ERR(th));
674
675         qti_lqes_write_lock(env);
676
677         LQUOTA_DEBUG_LQES(env, "dqacq starts uuid:%s flags:0x%x wanted:%llu"
678                      " usage:%llu", obd_uuid2str(uuid), qb_flags, qb_count,
679                      qb_usage);
680
681         /* Legal race, limits have been removed on master, but slave didn't
682          * receive the change yet. Just return EINPROGRESS until the slave gets
683          * notified. */
684         if (!lqe->lqe_enforced && !req_is_rel(qb_flags))
685                 GOTO(out_locked, rc = -ESRCH);
686
687         /* recompute qunit in case it was never initialized */
688         qmt_revalidate_lqes(env, qmt, qb_flags);
689
690         /* slave just wants to acquire per-ID lock */
691         if (req_is_acq(qb_flags) && qb_count == 0)
692                 GOTO(out_locked, rc = 0);
693
694         /* fetch how much quota space is already granted to this slave */
695         rc = qmt_slv_read(env, &lqe->lqe_id, slv_obj, &slv_granted);
696         if (rc) {
697                 LQUOTA_ERROR(lqe, "Failed to get granted for slave %s, rc=%d",
698                              obd_uuid2str(uuid), rc);
699                 GOTO(out_locked, rc);
700         }
701         /* recall how much space this slave currently owns in order to restore
702          * it in case of failure */
703         slv_granted_bck = slv_granted;
704
705         /* record current time for soft limit & grace time management */
706         now = ktime_get_real_seconds();
707
708         if (req_is_rel(qb_flags)) {
709                 /* Slave would like to release quota space */
710                 if (slv_granted < qb_count ||
711                     !qmt_lqes_can_rel(env, qb_count)) {
712                         /* can't release more than granted */
713                         LQUOTA_ERROR_LQES(env,
714                                           "Release too much! uuid:%s release: %llu granted:%llu, total:%llu",
715                                           obd_uuid2str(uuid), qb_count,
716                                           slv_granted, lqe->lqe_granted);
717                         GOTO(out_locked, rc = -EINVAL);
718                 }
719
720                 repbody->qb_count = qb_count;
721                 /* put released space back to global pool */
722                 qmt_rel_lqes(env, &slv_granted, qb_count);
723                 GOTO(out_write, rc = 0);
724         }
725
726         if (req_has_rep(qb_flags) && slv_granted < qb_usage) {
727                 /* Slave is reporting space usage in quota request and it turns
728                  * out to be using more quota space than owned, so we adjust
729                  * granted space regardless of the current state of affairs */
730                 repbody->qb_count = qb_usage - slv_granted;
731                 qmt_grant_lqes(env, &slv_granted, repbody->qb_count);
732         }
733
734         if (!req_is_acq(qb_flags) && !req_is_preacq(qb_flags))
735                 GOTO(out_write, rc = 0);
736
737         qmt_adjust_edquot_notify(env, qmt, now, qb_flags);
738         if (qti_lqes_edquot(env))
739                 /* no hope to claim further space back */
740                 GOTO(out_write, rc = -EDQUOT);
741
742         if (qmt_space_exhausted_lqes(env, now)) {
743                 /* might have some free space once rebalancing is completed */
744                 rc = req_is_acq(qb_flags) ? -EINPROGRESS : -EDQUOT;
745                 GOTO(out_write, rc);
746         }
747
748         if (req_is_preacq(qb_flags)) {
749                 /* slave would like to pre-acquire quota space. To do so, it
750                  * reports in qb_count how much spare quota space it owns and we
751                  * can grant back quota space which is consistent with qunit
752                  * value. */
753                 if (qb_count >= qti_lqes_min_qunit(env))
754                         /* slave already own the maximum it should */
755                         GOTO(out_write, rc = 0);
756
757                 count = qmt_lqes_alloc_expand(env, slv_granted, qb_count);
758                 if (count == 0)
759                         GOTO(out_write, rc = -EDQUOT);
760
761                 repbody->qb_count += count;
762                 qmt_grant_lqes(env, &slv_granted, count);
763                 GOTO(out_write, rc = 0);
764         }
765
766         /* processing acquire request with clients waiting */
767         if (qmt_lqes_cannot_grant(env, qb_count)) {
768                 /* cannot grant as much as asked, but can still afford to grant
769                  * some quota space back */
770                 count = qmt_lqes_grant_some_quota(env);
771                 repbody->qb_count += count;
772                 qmt_grant_lqes(env, &slv_granted, count);
773                 GOTO(out_write, rc = 0);
774         }
775
776         /* Whouhou! we can satisfy the slave request! */
777         repbody->qb_count += qb_count;
778         qmt_grant_lqes(env, &slv_granted, qb_count);
779
780         /* Try to expand the acquired count for DQACQ */
781         count = qmt_lqes_alloc_expand(env, slv_granted, 0);
782         if (count != 0) {
783                 /* can even grant more than asked, it is like xmas ... */
784                 repbody->qb_count += count;
785                 qmt_grant_lqes(env, &slv_granted, count);
786                 GOTO(out_write, rc = 0);
787         }
788
789         GOTO(out_write, rc = 0);
790 out_write:
791         if (repbody->qb_count == 0)
792                 GOTO(out_locked, rc);
793
794         /* start/stop grace timer if required */
795         qmt_lqes_tune_grace(env, now);
796
797         /* Update slave index first since it is easier to roll back */
798         ret = qmt_slv_write(env, th, lqe, slv_obj, LQUOTA_BUMP_VER,
799                             &repbody->qb_slv_ver, slv_granted);
800         if (ret) {
801                 /* restore initial quota settings */
802                 qmt_restore_lqes(env);
803                 /* reset qb_count */
804                 repbody->qb_count = 0;
805                 GOTO(out_locked, rc = ret);
806         }
807
808         /* Update global index, no version bump needed */
809         ret = qmt_glb_write_lqes(env, th, 0, NULL);
810         if (ret) {
811                 rc = ret;
812                 /* restore initial quota settings */
813                 qmt_restore_lqes(env);
814                 /* reset qb_count */
815                 repbody->qb_count = 0;
816
817                 /* restore previous granted value */
818                 ret = qmt_slv_write(env, th, lqe, slv_obj, 0, NULL,
819                                     slv_granted_bck);
820                 if (ret) {
821                         LQUOTA_ERROR(lqe, "failed to restore initial slave "
822                                      "value rc:%d ret%d", rc, ret);
823                         LBUG();
824                 }
825                 qmt_adjust_edquot_notify(env, qmt, now, qb_flags);
826                 GOTO(out_locked, rc);
827         }
828
829         /* Total granted has been changed, let's try to adjust the qunit
830          * size according to the total granted & limits. */
831
832         /* clear/set edquot flag and notify slaves via glimpse if needed */
833         qmt_adjust_and_notify(env, qmt, now, qb_flags);
834 out_locked:
835         LQUOTA_DEBUG_LQES(env, "dqacq ends count:%llu ver:%llu rc:%d",
836                      repbody->qb_count, repbody->qb_slv_ver, rc);
837         qti_lqes_write_unlock(env);
838 out:
839         qti_lqes_restore_fini(env);
840
841         if (th != NULL && !IS_ERR(th))
842                 dt_trans_stop(env, qmt->qmt_child, th);
843
844         if (slv_obj != NULL && !IS_ERR(slv_obj))
845                 dt_object_put(env, slv_obj);
846
847         if ((req_is_acq(qb_flags) || req_is_preacq(qb_flags)) &&
848             OBD_FAIL_CHECK(OBD_FAIL_QUOTA_EDQUOT)) {
849                 /* introduce inconsistency between granted value in slave index
850                  * and slave index copy of slave */
851                 repbody->qb_count = 0;
852                 rc = -EDQUOT;
853         }
854
855         RETURN(rc);
856 }
857
858 /*
859  * Extract index from uuid or quota index file name.
860  *
861  * \param[in] uuid      uuid or quota index name(0x1020000-OST0001_UUID)
862  * \param[out] idx      pointer to save index
863  *
864  * \retval              slave type(QMT_STYPE_MDT or QMT_STYPE_OST)
865  * \retval -EINVAL      wrong uuid
866  */
867 int qmt_uuid2idx(struct obd_uuid *uuid, int *idx)
868 {
869         char *uuid_str, *name, *dash;
870         int rc = -EINVAL;
871
872         uuid_str = (char *)uuid->uuid;
873
874         if (strnlen(uuid_str, UUID_MAX) >= UUID_MAX) {
875                 CERROR("quota: UUID '%.*s' missing trailing NUL: rc = %d\n",
876                        UUID_MAX, uuid_str, rc);
877                 return rc;
878         }
879
880         dash = strrchr(uuid_str, '-');
881         name = dash + 1;
882         /* Going to get index from MDTXXXX/OSTXXXX. Thus uuid should
883          * have at least 8 bytes after '-': 3 for MDT/OST, 4 for index
884          * and 1 byte for null character. */
885         if (*dash != '-' || ((uuid_str + UUID_MAX - name) < 8)) {
886                 CERROR("quota: wrong UUID format '%s': rc = %d\n",
887                        uuid_str, rc);
888                 return rc;
889         }
890
891         rc = target_name2index(name, idx, NULL);
892         switch (rc) {
893         case LDD_F_SV_TYPE_MDT:
894                 rc = QMT_STYPE_MDT;
895                 break;
896         case LDD_F_SV_TYPE_OST:
897                 rc = QMT_STYPE_OST;
898                 break;
899         default:
900                 CERROR("quota: wrong UUID type '%s': rc = %d\n", uuid_str, rc);
901                 rc = -EINVAL;
902         }
903
904         RETURN(rc);
905 }
906
907 /*
908  * Handle quota request from slave.
909  *
910  * \param env  - is the environment passed by the caller
911  * \param ld   - is the lu device associated with the qmt
912  * \param req  - is the quota acquire request
913  */
914 static int qmt_dqacq(const struct lu_env *env, struct lu_device *ld,
915                      struct ptlrpc_request *req)
916 {
917         struct qmt_device *qmt = lu2qmt_dev(ld);
918         struct quota_body *qbody, *repbody;
919         struct obd_uuid *uuid;
920         struct ldlm_lock *lock;
921         int rtype, qtype;
922         int rc, idx, stype;
923         ENTRY;
924
925         qbody = req_capsule_client_get(&req->rq_pill, &RMF_QUOTA_BODY);
926         if (qbody == NULL)
927                 RETURN(err_serious(-EPROTO));
928
929         repbody = req_capsule_server_get(&req->rq_pill, &RMF_QUOTA_BODY);
930         if (repbody == NULL)
931                 RETURN(err_serious(-EFAULT));
932
933         /* verify if global lock is stale */
934         if (!lustre_handle_is_used(&qbody->qb_glb_lockh))
935                 RETURN(-ENOLCK);
936
937         lock = ldlm_handle2lock(&qbody->qb_glb_lockh);
938         if (lock == NULL)
939                 RETURN(-ENOLCK);
940         LDLM_LOCK_PUT(lock);
941
942         uuid = &req->rq_export->exp_client_uuid;
943         stype = qmt_uuid2idx(uuid, &idx);
944         if (stype < 0)
945                 RETURN(stype);
946
947         if (req_is_rel(qbody->qb_flags) + req_is_acq(qbody->qb_flags) +
948             req_is_preacq(qbody->qb_flags) > 1) {
949                 CERROR("%s: malformed quota request with conflicting flags set "
950                        "(%x) from slave %s\n", qmt->qmt_svname,
951                        qbody->qb_flags, obd_uuid2str(uuid));
952                 RETURN(-EPROTO);
953         }
954
955         if (req_is_acq(qbody->qb_flags) || req_is_preacq(qbody->qb_flags)) {
956                 /* acquire and pre-acquire should use a valid ID lock */
957
958                 if (!lustre_handle_is_used(&qbody->qb_lockh))
959                         RETURN(-ENOLCK);
960
961                 lock = ldlm_handle2lock(&qbody->qb_lockh);
962                 if (lock == NULL)
963                         /* no lock associated with this handle */
964                         RETURN(-ENOLCK);
965
966                 LDLM_DEBUG(lock, "%sacquire request",
967                            req_is_preacq(qbody->qb_flags) ? "pre" : "");
968
969                 if (!obd_uuid_equals(&lock->l_export->exp_client_uuid, uuid)) {
970                         /* sorry, no way to cheat ... */
971                         LDLM_LOCK_PUT(lock);
972                         RETURN(-ENOLCK);
973                 }
974
975                 if (ldlm_is_ast_sent(lock)) {
976                         struct ptlrpc_service_part *svc;
977                         timeout_t timeout;
978
979                         svc = req->rq_rqbd->rqbd_svcpt;
980                         timeout = at_est2timeout(at_get(&svc->scp_at_estimate));
981                         timeout += (ldlm_bl_timeout(lock) >> 1);
982
983                         /* lock is being cancelled, prolong timeout */
984                         ldlm_refresh_waiting_lock(lock, timeout);
985                 }
986                 LDLM_LOCK_PUT(lock);
987         }
988
989         /* extract quota information from global index FID packed in the
990          * request */
991         rc = lquota_extract_fid(&qbody->qb_fid, &rtype, &qtype);
992         if (rc)
993                 RETURN(-EINVAL);
994
995         /* Find the quota entry associated with the quota id */
996         rc = qmt_pool_lqes_lookup(env, qmt, rtype, stype, qtype,
997                                   &qbody->qb_id, NULL, idx);
998         if (rc)
999                 RETURN(rc);
1000
1001         rc = qmt_dqacq0(env, qmt, uuid, qbody->qb_flags,
1002                         qbody->qb_count, qbody->qb_usage, repbody);
1003
1004         if (lustre_handle_is_used(&qbody->qb_lockh))
1005                 /* return current qunit value only to slaves owning an per-ID
1006                  * quota lock. For enqueue, the qunit value will be returned in
1007                  * the LVB */
1008                 repbody->qb_qunit = qti_lqes_min_qunit(env);
1009         CDEBUG(D_QUOTA, "qmt_dqacq return qb_qunit %llu qb_count %llu\n",
1010                repbody->qb_qunit, repbody->qb_count);
1011         qti_lqes_fini(env);
1012         RETURN(rc);
1013 }
1014
1015 /* Vector of quota request handlers. This vector is used by the MDT to forward
1016  * requests to the quota master. */
1017 struct qmt_handlers qmt_hdls = {
1018         /* quota request handlers */
1019         .qmth_quotactl          = qmt_quotactl,
1020         .qmth_dqacq             = qmt_dqacq,
1021
1022         /* ldlm handlers */
1023         .qmth_intent_policy     = qmt_intent_policy,
1024         .qmth_lvbo_init         = qmt_lvbo_init,
1025         .qmth_lvbo_update       = qmt_lvbo_update,
1026         .qmth_lvbo_size         = qmt_lvbo_size,
1027         .qmth_lvbo_fill         = qmt_lvbo_fill,
1028         .qmth_lvbo_free         = qmt_lvbo_free,
1029 };
1030 EXPORT_SYMBOL(qmt_hdls);