Whamcloud - gitweb
LU-16340 quota: notify only global lqe
[fs/lustre-release.git] / lustre / quota / qmt_lock.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 <linux/kthread.h>
34
35 #include <lustre_dlm.h>
36 #include <lustre_swab.h>
37 #include <obd_class.h>
38
39 #include "qmt_internal.h"
40
41 /* intent policy function called from mdt_intent_opc() when the intent is of
42  * quota type */
43 int qmt_intent_policy(const struct lu_env *env, struct lu_device *ld,
44                       struct ptlrpc_request *req, struct ldlm_lock **lockp,
45                       int flags)
46 {
47         struct qmt_device       *qmt = lu2qmt_dev(ld);
48         struct ldlm_intent      *it;
49         struct quota_body       *reqbody;
50         struct quota_body       *repbody;
51         struct obd_uuid         *uuid;
52         struct lquota_lvb       *lvb;
53         struct ldlm_resource    *res = (*lockp)->l_resource;
54         struct ldlm_reply       *ldlm_rep;
55         int                      rc, lvb_len;
56         ENTRY;
57
58         req_capsule_extend(&req->rq_pill, &RQF_LDLM_INTENT_QUOTA);
59         req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
60                              ldlm_lvbo_size(*lockp));
61
62         /* extract quota body and intent opc */
63         it = req_capsule_client_get(&req->rq_pill, &RMF_LDLM_INTENT);
64         if (it == NULL)
65                 RETURN(err_serious(-EFAULT));
66
67         reqbody = req_capsule_client_get(&req->rq_pill, &RMF_QUOTA_BODY);
68         if (reqbody == NULL)
69                 RETURN(err_serious(-EFAULT));
70
71         /* prepare reply */
72         rc = req_capsule_server_pack(&req->rq_pill);
73         if (rc != 0) {
74                 CERROR("Can't pack response, rc %d\n", rc);
75                 RETURN(err_serious(rc));
76         }
77
78         repbody = req_capsule_server_get(&req->rq_pill, &RMF_QUOTA_BODY);
79         if (repbody == NULL)
80                 RETURN(err_serious(-EFAULT));
81
82         ldlm_rep = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
83         if (ldlm_rep == NULL)
84                 RETURN(err_serious(-EFAULT));
85
86         uuid = &(*lockp)->l_export->exp_client_uuid;
87         switch (it->opc) {
88
89         case IT_QUOTA_DQACQ: {
90                 struct lquota_entry     *lqe;
91                 struct ldlm_lock        *lock;
92                 int idx, stype;
93
94                 if (res->lr_name.name[LUSTRE_RES_ID_QUOTA_SEQ_OFF] == 0)
95                         /* acquire on global lock? something is wrong ... */
96                         GOTO(out, rc = -EPROTO);
97
98                 /* verify global lock isn't stale */
99                 if (!lustre_handle_is_used(&reqbody->qb_glb_lockh))
100                         GOTO(out, rc = -ENOLCK);
101
102                 lock = ldlm_handle2lock(&reqbody->qb_glb_lockh);
103                 if (lock == NULL)
104                         GOTO(out, rc = -ENOLCK);
105                 LDLM_LOCK_PUT(lock);
106
107                 stype = qmt_uuid2idx(uuid, &idx);
108                 if (stype < 0)
109                         GOTO(out, rc = -EINVAL);
110
111                 /* TODO: it seems we don't need to get lqe from
112                  * lq_lvb_data anymore ... And do extra get
113                  * and put on it */
114                 lqe = res->lr_lvb_data;
115                 LASSERT(lqe != NULL);
116                 lqe_getref(lqe);
117
118                 rc = qmt_pool_lqes_lookup(env, qmt, lqe_rtype(lqe), stype,
119                                           lqe_qtype(lqe), &reqbody->qb_id,
120                                           NULL, idx);
121                 if (rc) {
122                         lqe_putref(lqe);
123                         GOTO(out, rc);
124                 }
125
126                 /* acquire quota space */
127                 rc = qmt_dqacq0(env, qmt, uuid,
128                                 reqbody->qb_flags, reqbody->qb_count,
129                                 reqbody->qb_usage, repbody,
130                                 qmt_dom(lqe_rtype(lqe), stype) ? -1 : idx);
131                 lqe_putref(lqe);
132                 qti_lqes_fini(env);
133                 if (rc)
134                         GOTO(out, rc);
135                 break;
136         }
137
138         case IT_QUOTA_CONN:
139                 /* new connection from slave */
140
141                 if (res->lr_name.name[LUSTRE_RES_ID_QUOTA_SEQ_OFF] != 0)
142                         /* connection on per-ID lock? something is wrong ... */
143                         GOTO(out, rc = -EPROTO);
144
145                 rc = qmt_pool_new_conn(env, qmt, &reqbody->qb_fid,
146                                        &repbody->qb_slv_fid,
147                                        &repbody->qb_slv_ver, uuid);
148                 if (rc)
149                         GOTO(out, rc);
150                 break;
151
152         default:
153                 CERROR("%s: invalid intent opcode: %llu\n", qmt->qmt_svname,
154                        it->opc);
155                 GOTO(out, rc = -EINVAL);
156         }
157
158         /* on success, pack lvb in reply */
159         lvb = req_capsule_server_get(&req->rq_pill, &RMF_DLM_LVB);
160         lvb_len = ldlm_lvbo_size(*lockp);
161         lvb_len = ldlm_lvbo_fill(*lockp, lvb, &lvb_len);
162         if (lvb_len < 0)
163                 GOTO(out, rc = lvb_len);
164
165         req_capsule_shrink(&req->rq_pill, &RMF_DLM_LVB, lvb_len, RCL_SERVER);
166 out:
167         ldlm_rep->lock_policy_res2 = clear_serious(rc);
168         EXIT;
169         return ELDLM_OK;
170 }
171
172 /*
173  * Initialize quota LVB associated with quota indexes.
174  * Called with res->lr_lvb_sem held
175  */
176 int qmt_lvbo_init(struct lu_device *ld, struct ldlm_resource *res)
177 {
178         struct lu_env           *env;
179         struct qmt_thread_info  *qti;
180         struct qmt_device       *qmt = lu2qmt_dev(ld);
181         int                      pool_type, qtype;
182         int                      rc;
183         ENTRY;
184
185         LASSERT(res != NULL);
186
187         if (res->lr_type != LDLM_PLAIN)
188                 RETURN(-ENOTSUPP);
189
190         if (res->lr_lvb_data ||
191             res->lr_name.name[LUSTRE_RES_ID_SEQ_OFF] != FID_SEQ_QUOTA_GLB)
192                 RETURN(0);
193
194         env = lu_env_find();
195         LASSERT(env);
196         qti = qmt_info(env);
197
198         /* extract global index FID and quota identifier */
199         fid_extract_from_quota_res(&qti->qti_fid, &qti->qti_id, &res->lr_name);
200
201         /* sanity check the global index FID */
202         rc = lquota_extract_fid(&qti->qti_fid, &pool_type, &qtype);
203         if (rc) {
204                 CERROR("can't extract glb index information from FID "DFID"\n",
205                        PFID(&qti->qti_fid));
206                 GOTO(out, rc);
207         }
208
209         if (res->lr_name.name[LUSTRE_RES_ID_QUOTA_SEQ_OFF] != 0) {
210                 /* no ID quota lock associated with UID/GID 0 or with a seq 0,
211                  * we are thus dealing with an ID lock. */
212                 struct qmt_pool_info    *pool;
213                 struct lquota_entry     *lqe;
214                 struct lqe_glbl_data    *lgd;
215
216                 pool = qmt_pool_lookup_glb(env, qmt, pool_type);
217                 if (IS_ERR(pool))
218                         GOTO(out, rc = -ENOMEM);
219
220                 /* Find the quota entry associated with the quota id */
221                 lqe = qmt_pool_lqe_lookup(env, qmt, pool_type, qtype,
222                                           &qti->qti_id, NULL);
223                 if (IS_ERR(lqe)) {
224                         qpi_putref(env, pool);
225                         GOTO(out, rc = PTR_ERR(lqe));
226                 }
227
228                 /* TODO: need something like qmt_extend_lqe_gd that has
229                  * to be calledeach time when qpi_slv_nr is incremented */
230                 lgd = qmt_alloc_lqe_gd(pool, qtype);
231                 if (!lgd) {
232                         lqe_putref(lqe);
233                         qpi_putref(env, pool);
234                         GOTO(out, rc = -ENOMEM);
235                 }
236
237                 qmt_setup_lqe_gd(env, qmt, lqe, lgd, pool_type);
238
239                 /* store reference to lqe in lr_lvb_data */
240                 res->lr_lvb_data = lqe;
241                 qpi_putref(env, pool);
242                 LQUOTA_DEBUG(lqe, "initialized res lvb");
243         } else {
244                 struct dt_object        *obj;
245
246                 /* lookup global index */
247                 obj = dt_locate(env, qmt->qmt_child, &qti->qti_fid);
248                 if (IS_ERR(obj))
249                         GOTO(out, rc = PTR_ERR(obj));
250                 if (!dt_object_exists(obj)) {
251                         dt_object_put(env, obj);
252                         GOTO(out, rc = -ENOENT);
253                 }
254
255                 /* store reference to global index object in lr_lvb_data */
256                 res->lr_lvb_data = obj;
257                 CDEBUG(D_QUOTA, DFID" initialized lvb\n", PFID(&qti->qti_fid));
258         }
259
260         res->lr_lvb_len = sizeof(struct lquota_lvb);
261         EXIT;
262 out:
263         return rc;
264 }
265
266 /* clear lge_qunit/edquot_nu flags -
267  * slave recieved new qunit and edquot.
268  *
269  * \retval      true if revoke is needed - qunit
270  *              for this slave reaches least_qunit
271  */
272 static bool qmt_clear_lgeg_arr_nu(struct lquota_entry *lqe, int stype, int idx)
273 {
274         unsigned long least_qunit = lqe2qpi(lqe)->qpi_least_qunit;
275         struct lqe_glbl_data *lgd = lqe->lqe_glbl_data;
276
277         /* There is no array to store lge for the case of DOM.
278          * Ignore it until MDT pools will be ready. */
279         if (!qmt_dom(lqe_rtype(lqe), stype)) {
280                 lqe->lqe_glbl_data->lqeg_arr[idx].lge_qunit_nu = 0;
281                 lqe->lqe_glbl_data->lqeg_arr[idx].lge_edquot_nu = 0;
282
283                 /* We shouldn't call revoke for DOM case, it will be updated
284                  * at qmt_id_lock_glimpse. */
285                 return (lgd->lqeg_arr[idx].lge_qunit == least_qunit);
286         }
287
288         return false;
289 }
290
291 static bool qmt_set_revoke(struct lu_env *env, struct lquota_entry *lqe,
292                           int stype, int idx)
293 {
294         unsigned long least_qunit = lqe2qpi(lqe)->qpi_least_qunit;
295         struct lqe_glbl_data *lgd = lqe->lqe_glbl_data;
296         bool notify = false;
297
298         if (lgd->lqeg_arr[idx].lge_qunit == least_qunit) {
299                 int i;
300
301                 qti_lqes_write_lock(env);
302                 for (i = 0; i < qti_lqes_cnt(env); i++) {
303                         LQUOTA_DEBUG(qti_lqes(env)[i],
304                                      "idx %d lge_qunit %llu least_qunit %lu\n",
305                                      idx, lgd->lqeg_arr[idx].lge_qunit,
306                                      least_qunit);
307                         if (qti_lqes(env)[i]->lqe_qunit == least_qunit) {
308                                 qti_lqes(env)[i]->lqe_revoke_time =
309                                                         ktime_get_seconds();
310                                 notify |= qmt_adjust_edquot(qti_lqes(env)[i],
311                                                   ktime_get_real_seconds());
312                         }
313                 }
314                 qti_lqes_write_unlock(env);
315         }
316         return notify;
317 }
318
319 /*
320  * Update LVB associated with the global quota index.
321  * This function is called from the DLM itself after a glimpse callback, in this
322  * case valid ptlrpc request is passed.
323  */
324 int qmt_lvbo_update(struct lu_device *ld, struct ldlm_resource *res,
325                     struct ptlrpc_request *req, int increase_only)
326 {
327         struct lu_env           *env;
328         struct qmt_thread_info  *qti;
329         struct qmt_device       *qmt = lu2qmt_dev(ld);
330         struct lquota_entry     *lqe;
331         struct lquota_lvb       *lvb;
332         struct ldlm_lock        *lock;
333         struct obd_export       *exp;
334         bool                     need_revoke;
335         int                      rc = 0, idx, stype;
336         ENTRY;
337
338         LASSERT(res != NULL);
339
340         if (req == NULL)
341                 RETURN(0);
342
343         if (res->lr_name.name[LUSTRE_RES_ID_QUOTA_SEQ_OFF] == 0)
344                 /* no need to update lvb for global quota locks */
345                 RETURN(0);
346
347         lvb = req_capsule_server_swab_get(&req->rq_pill, &RMF_DLM_LVB,
348                                           lustre_swab_lquota_lvb);
349         if (lvb == NULL) {
350                 CERROR("%s: failed to extract lvb from request\n",
351                        qmt->qmt_svname);
352                 RETURN(-EFAULT);
353         }
354
355         lqe = res->lr_lvb_data;
356         LASSERT(lqe != NULL);
357         lqe_getref(lqe);
358
359         /* allocate environement */
360         env = lu_env_find();
361         LASSERT(env);
362         qti = qmt_info(env);
363
364         /* The request is a glimpse callback which was sent via the
365          * reverse import to the slave. What we care about here is the
366          * export associated with the slave and req->rq_export is
367          * definitely not what we are looking for (it is actually set to
368          * NULL here).
369          * Therefore we extract the lock from the request argument
370          * and use lock->l_export. */
371         lock = ldlm_request_lock(req);
372         if (IS_ERR(lock)) {
373                 CERROR("%s: failed to get lock from request!\n",
374                        qmt->qmt_svname);
375                 GOTO(out, rc = PTR_ERR(lock));
376         }
377
378         exp = class_export_get(lock->l_export);
379         if (exp == NULL) {
380                 CERROR("%s: failed to get export from lock!\n",
381                        qmt->qmt_svname);
382                 GOTO(out, rc = -EFAULT);
383         }
384
385         stype = qmt_uuid2idx(&exp->exp_client_uuid, &idx);
386         if (stype < 0)
387                 GOTO(out_exp, rc = stype);
388
389         need_revoke = qmt_clear_lgeg_arr_nu(lqe, stype, idx);
390         if (lvb->lvb_id_rel == 0) {
391                 /* nothing to release */
392                 if (lvb->lvb_id_may_rel != 0)
393                         /* but might still release later ... */
394                         lqe->lqe_may_rel += lvb->lvb_id_may_rel;
395         }
396
397         if (!need_revoke && lvb->lvb_id_rel == 0)
398                 GOTO(out_exp, rc = 0);
399
400         rc = qmt_pool_lqes_lookup(env, qmt, lqe_rtype(lqe), stype,
401                                   lqe_qtype(lqe), &lqe->lqe_id, NULL, idx);
402         if (rc)
403                 GOTO(out_exp, rc);
404
405         if (need_revoke && qmt_set_revoke(env, lqe, stype, idx) &&
406             lqe->lqe_glbl_data) {
407                 qmt_seed_glbe_edquot(env, lqe->lqe_glbl_data);
408                 qmt_id_lock_notify(qmt, lqe);
409         }
410
411         if (lvb->lvb_id_rel) {
412                 LQUOTA_DEBUG(lqe, "releasing:%llu may release:%llu",
413                              lvb->lvb_id_rel, lvb->lvb_id_may_rel);
414
415                 /* release quota space */
416                 rc = qmt_dqacq0(env, qmt, &exp->exp_client_uuid,
417                                 QUOTA_DQACQ_FL_REL, lvb->lvb_id_rel,
418                                 0, &qti->qti_body,
419                                 qmt_dom(lqe_rtype(lqe), stype) ? -1 : idx);
420                 if (rc || qti->qti_body.qb_count != lvb->lvb_id_rel)
421                         LQUOTA_ERROR(lqe,
422                                      "failed to release quota space on glimpse %llu!=%llu : rc = %d\n",
423                                      qti->qti_body.qb_count,
424                                      lvb->lvb_id_rel, rc);
425         }
426         qti_lqes_fini(env);
427         if (rc)
428                 GOTO(out_exp, rc);
429         EXIT;
430 out_exp:
431         class_export_put(exp);
432 out:
433         lqe_putref(lqe);
434         return rc;
435 }
436
437 /*
438  * Report size of lvb to ldlm layer in order to allocate lvb buffer
439  * As far as quota locks are concerned, the size is static and is the same
440  * for both global and per-ID locks which shares the same lvb format.
441  */
442 int qmt_lvbo_size(struct lu_device *ld, struct ldlm_lock *lock)
443 {
444         return sizeof(struct lquota_lvb);
445 }
446
447 /*
448  * Fill request buffer with quota lvb
449  */
450 int qmt_lvbo_fill(struct lu_device *ld, struct ldlm_lock *lock, void *lvb,
451                   int lvblen)
452 {
453         struct ldlm_resource *res = lock->l_resource;
454         struct lquota_lvb *qlvb = lvb;
455         struct lu_env *env;
456         int rc;
457         ENTRY;
458
459         LASSERT(res != NULL);
460         rc = 0;
461
462         if (res->lr_type != LDLM_PLAIN || res->lr_lvb_data == NULL ||
463             res->lr_name.name[LUSTRE_RES_ID_SEQ_OFF] != FID_SEQ_QUOTA_GLB)
464                 RETURN(-EINVAL);
465
466         env = lu_env_find();
467         LASSERT(env);
468
469         if (res->lr_name.name[LUSTRE_RES_ID_QUOTA_SEQ_OFF] != 0) {
470                 /* no ID quota lock associated with UID/GID 0 or with a seq 0,
471                  * we are thus dealing with an ID lock. */
472                 struct lquota_entry *lqe = res->lr_lvb_data;
473                 struct qmt_device *qmt;
474                 struct obd_uuid *uuid;
475                 int idx;
476
477                 uuid = &(lock)->l_export->exp_client_uuid;
478                 rc = qmt_uuid2idx(uuid, &idx);
479                 if (rc < 0)
480                         RETURN(rc);
481                 qmt = lu2qmt_dev(ld);
482                 /* return current qunit value & edquot flags in lvb */
483                 lqe_getref(lqe);
484                 rc = qmt_pool_lqes_lookup(env, qmt, lqe_rtype(lqe), rc,
485                                           lqe_qtype(lqe), &lqe->lqe_id,
486                                           NULL, idx);
487                 if (!rc) {
488                         qlvb->lvb_id_qunit = qti_lqes_min_qunit(env);
489                         qlvb->lvb_flags = 0;
490                         if (qti_lqes_edquot(env))
491                                 qlvb->lvb_flags = LQUOTA_FL_EDQUOT;
492                         qti_lqes_fini(env);
493                 }
494                 CDEBUG(D_QUOTA, "uuid %s lqe_id %lu, edquot %llu qunit %llu\n",
495                        (char *)uuid, (unsigned long)lqe->lqe_id.qid_uid,
496                        qlvb->lvb_flags, qlvb->lvb_id_qunit);
497                 lqe_putref(lqe);
498         } else {
499                 /* global quota lock */
500                 struct dt_object *obj = res->lr_lvb_data;
501
502                 /* return current version of global index */
503                 qlvb->lvb_glb_ver = dt_version_get(env, obj);
504         }
505
506         RETURN(rc = rc ?: sizeof(struct lquota_lvb));
507 }
508
509 /*
510  * Free lvb associated with a given ldlm resource
511  * we don't really allocate a lvb, lr_lvb_data just points to
512  * the appropriate backend structures.
513  */
514 int qmt_lvbo_free(struct lu_device *ld, struct ldlm_resource *res)
515 {
516         ENTRY;
517
518         if (res->lr_lvb_data == NULL)
519                 RETURN(0);
520
521         if (res->lr_name.name[LUSTRE_RES_ID_QUOTA_SEQ_OFF] != 0) {
522                 struct lquota_entry *lqe = res->lr_lvb_data;
523                 struct lqe_glbl_data *lgd = lqe->lqe_glbl_data;
524
525                 mutex_lock(&lqe->lqe_glbl_data_lock);
526                 lqe->lqe_glbl_data = NULL;
527                 qmt_free_lqe_gd(lgd);
528                 mutex_unlock(&lqe->lqe_glbl_data_lock);
529
530                 /* release lqe reference */
531                 lqe_putref(lqe);
532         } else {
533                 struct dt_object *obj = res->lr_lvb_data;
534                 /* release object reference */
535                 dt_object_put(lu_env_find(), obj);
536         }
537
538         res->lr_lvb_data = NULL;
539         res->lr_lvb_len  = 0;
540
541         RETURN(0);
542 }
543
544 typedef int (*qmt_glimpse_cb_t)(struct ldlm_lock *, struct lquota_entry *);
545
546 struct qmt_gl_lock_array {
547         unsigned long             q_max;
548         unsigned long             q_cnt;
549         struct ldlm_lock        **q_locks;
550 };
551
552 static void qmt_free_lock_array(struct qmt_gl_lock_array *array)
553 {
554         int i;
555
556         if (array->q_max == 0) {
557                 LASSERT(array->q_locks == NULL);
558                 return;
559         }
560
561         for (i = 0; i < array->q_cnt; i++) {
562                 LASSERT(array->q_locks[i]);
563                 LDLM_LOCK_RELEASE(array->q_locks[i]);
564                 array->q_locks[i] = NULL;
565         }
566         array->q_cnt = 0;
567         OBD_FREE_PTR_ARRAY(array->q_locks, array->q_max);
568         array->q_locks = NULL;
569         array->q_max = 0;
570 }
571
572 static int qmt_alloc_lock_array(struct ldlm_resource *res,
573                                 struct qmt_gl_lock_array *array,
574                                 qmt_glimpse_cb_t cb, void *arg)
575 {
576         struct list_head *pos;
577         unsigned long count = 0;
578         int fail_cnt = 0;
579         ENTRY;
580
581         LASSERT(!array->q_max && !array->q_cnt && !array->q_locks);
582 again:
583         lock_res(res);
584         /* scan list of granted locks */
585         list_for_each(pos, &res->lr_granted) {
586                 struct ldlm_lock *lock;
587                 int rc;
588
589                 lock = list_entry(pos, struct ldlm_lock, l_res_link);
590                 LASSERT(lock->l_export);
591
592                 if (cb != NULL) {
593                         rc = cb(lock, arg);
594                         /* slave should not be notified */
595                         if (rc == 0)
596                                 continue;
597                 }
598
599                 count++;
600                 if (array->q_max != 0 && array->q_cnt < array->q_max) {
601                         array->q_locks[array->q_cnt] = LDLM_LOCK_GET(lock);
602                         array->q_cnt++;
603                 }
604         }
605         unlock_res(res);
606
607         if (count > array->q_max) {
608                 qmt_free_lock_array(array);
609                 if (++fail_cnt > 5)
610                         RETURN(-EAGAIN);
611                 /*
612                  * allocate more slots in case of more qualified locks are
613                  * found during next loop
614                  */
615                 array->q_max = count + count / 2 + 10;
616                 count = 0;
617                 LASSERT(array->q_locks == NULL && array->q_cnt == 0);
618                 OBD_ALLOC_PTR_ARRAY(array->q_locks, array->q_max);
619                 if (array->q_locks == NULL) {
620                         array->q_max = 0;
621                         RETURN(-ENOMEM);
622                 }
623
624                 goto again;
625         }
626         RETURN(0);
627 }
628
629 void qmt_setup_id_desc(struct ldlm_lock *lock, union ldlm_gl_desc *desc,
630                        struct lquota_entry *lqe)
631 {
632         struct obd_uuid *uuid = &(lock)->l_export->exp_client_uuid;
633         struct lqe_glbl_data *lgd = lqe->lqe_glbl_data;
634         int idx, stype;
635         __u64 qunit;
636         bool edquot;
637
638         stype = qmt_uuid2idx(uuid, &idx);
639         LASSERT(stype >= 0);
640
641         /* DOM case - set global lqe settings */
642         if (qmt_dom(lqe_rtype(lqe), stype)) {
643                 edquot = lqe->lqe_edquot;
644                 qunit = lqe->lqe_qunit;
645         } else {
646                 edquot = lgd->lqeg_arr[idx].lge_edquot;
647                 qunit = lgd->lqeg_arr[idx].lge_qunit;
648         }
649
650         /* fill glimpse descriptor with lqe settings */
651         desc->lquota_desc.gl_flags = edquot ? LQUOTA_FL_EDQUOT : 0;
652         desc->lquota_desc.gl_qunit = qunit;
653         CDEBUG(D_QUOTA, "setup desc: stype %d idx %d, edquot %llu qunit %llu\n",
654                          stype, idx, desc->lquota_desc.gl_flags,
655                          desc->lquota_desc.gl_qunit);
656 }
657
658 /*
659  * Send glimpse callback to slaves holding a lock on resource \res.
660  * This is used to notify slaves of new quota settings or to claim quota space
661  * back.
662  *
663  * \param env  - is the environment passed by the caller
664  * \param qmt  - is the quota master target
665  * \param res  - is the dlm resource associated with the quota object
666  * \param desc - is the glimpse descriptor to pack in glimpse callback
667  * \param cb   - is the callback function called on every lock and determine
668  *               whether a glimpse should be issued
669  * \param arg  - is an opaq parameter passed to the callback function
670  */
671 static int qmt_glimpse_lock(const struct lu_env *env, struct qmt_device *qmt,
672                             struct ldlm_resource *res, union ldlm_gl_desc *desc,
673                             qmt_glimpse_cb_t cb, struct lquota_entry *lqe)
674 {
675         union ldlm_gl_desc *descs = NULL;
676         struct lqe_glbl_data *gld;
677         struct list_head *tmp, *pos;
678         LIST_HEAD(gl_list);
679         struct qmt_gl_lock_array locks;
680         unsigned long i, locks_count;
681         int rc = 0;
682         ENTRY;
683
684         gld = lqe ? lqe->lqe_glbl_data : NULL;
685         memset(&locks, 0, sizeof(locks));
686         rc = qmt_alloc_lock_array(res, &locks, cb, lqe);
687         if (rc) {
688                 CERROR("%s: failed to allocate glimpse lock array (%d)\n",
689                        qmt->qmt_svname, rc);
690                 RETURN(rc);
691         }
692         if (!locks.q_cnt) {
693                 CDEBUG(D_QUOTA, "%s: no granted locks to send glimpse\n",
694                        qmt->qmt_svname);
695                 RETURN(0);
696         }
697         CDEBUG(D_QUOTA, "found granted locks %lu\n", locks.q_cnt);
698         locks_count = locks.q_cnt;
699
700         /* Use one desc for all works, when called from qmt_glb_lock_notify */
701         if (gld && locks.q_cnt > 1) {
702                 /* TODO: think about to store this preallocated descs
703                  * in lqe_global in lqeg_arr as a part of lqe_glbl_entry.
704                  * The benefit is that we don't need to allocate/free
705                  * and setup this descs each time. But the drawback is
706                  * memory use (sizeof ldlm_gl_desc * OST_COUNT * user_number).
707                  * for examfple it could be 88 * 256 * 10 000 about 225 MB. */
708                 OBD_ALLOC(descs,
709                           sizeof(struct ldlm_gl_lquota_desc) * locks.q_cnt);
710                 if (!descs) {
711                         CERROR("%s: alloc glimpse lock array failed: rc = %d\n",
712                                qmt->qmt_svname, rc);
713                         qmt_free_lock_array(&locks);
714                         RETURN(-ENOMEM);
715                 }
716         }
717
718         for (i = locks.q_cnt; i > 0; i--) {
719                 struct ldlm_glimpse_work *work;
720
721                 OBD_ALLOC_PTR(work);
722                 if (work == NULL) {
723                         CERROR("%s: failed to notify a lock.\n",
724                                qmt->qmt_svname);
725                         continue;
726                 }
727
728                 if (gld) {
729                         if (descs)
730                                 desc = &descs[i - 1];
731                         qmt_setup_id_desc(locks.q_locks[i - 1], desc, lqe);
732                         work->gl_interpret_data = lqe;
733                 }
734
735                 list_add_tail(&work->gl_list, &gl_list);
736                 work->gl_lock  = locks.q_locks[i - 1];
737                 work->gl_flags = 0;
738                 work->gl_desc  = desc;
739
740                 locks.q_locks[i - 1] = NULL;
741                 locks.q_cnt--;
742         }
743
744         qmt_free_lock_array(&locks);
745
746         if (list_empty(&gl_list)) {
747                 CDEBUG(D_QUOTA, "%s: nobody to notify\n", qmt->qmt_svname);
748                 GOTO(out, rc = 0);
749         }
750
751         /* issue glimpse callbacks to all connected slaves */
752         rc = ldlm_glimpse_locks(res, &gl_list);
753
754         list_for_each_safe(pos, tmp, &gl_list) {
755                 struct ldlm_glimpse_work *work;
756
757                 work = list_entry(pos, struct ldlm_glimpse_work, gl_list);
758
759                 list_del(&work->gl_list);
760                 CERROR("%s: failed to notify %s of new quota settings\n",
761                        qmt->qmt_svname,
762                        obd_uuid2str(&work->gl_lock->l_export->exp_client_uuid));
763                 LDLM_LOCK_RELEASE(work->gl_lock);
764                 OBD_FREE_PTR(work);
765         }
766 out:
767         if (descs)
768                 OBD_FREE(descs,
769                          sizeof(struct ldlm_gl_lquota_desc) * locks_count);
770
771         RETURN(rc);
772 }
773
774 /*
775  * Send glimpse request to all global quota locks to push new quota setting to
776  * slaves.
777  *
778  * \param env - is the environment passed by the caller
779  * \param lqe - is the lquota entry which has new settings
780  * \param ver - is the version associated with the setting change
781  */
782 void qmt_glb_lock_notify(const struct lu_env *env, struct lquota_entry *lqe,
783                          __u64 ver)
784 {
785         struct qmt_thread_info  *qti = qmt_info(env);
786         struct qmt_pool_info    *pool = lqe2qpi(lqe);
787         struct ldlm_resource    *res = NULL;
788         ENTRY;
789
790         lquota_generate_fid(&qti->qti_fid, pool->qpi_rtype, lqe_qtype(lqe));
791
792         /* send glimpse callback to notify slaves of new quota settings */
793         qti->qti_gl_desc.lquota_desc.gl_id        = lqe->lqe_id;
794         qti->qti_gl_desc.lquota_desc.gl_flags     = 0;
795         if (lqe->lqe_is_default) {
796                 qti->qti_gl_desc.lquota_desc.gl_hardlimit = 0;
797                 qti->qti_gl_desc.lquota_desc.gl_softlimit = 0;
798                 qti->qti_gl_desc.lquota_desc.gl_time = LQUOTA_GRACE_FLAG(0,
799                                                         LQUOTA_FLAG_DEFAULT);
800
801         } else if (lqe->lqe_is_deleted) {
802                 qti->qti_gl_desc.lquota_desc.gl_hardlimit = 0;
803                 qti->qti_gl_desc.lquota_desc.gl_softlimit = 0;
804                 qti->qti_gl_desc.lquota_desc.gl_time = LQUOTA_GRACE_FLAG(0,
805                                                         LQUOTA_FLAG_DELETED);
806         } else if (lqe->lqe_is_reset) {
807                 qti->qti_gl_desc.lquota_desc.gl_hardlimit = lqe->lqe_hardlimit;
808                 qti->qti_gl_desc.lquota_desc.gl_softlimit = lqe->lqe_softlimit;
809                 qti->qti_gl_desc.lquota_desc.gl_time = LQUOTA_GRACE_FLAG(0,
810                                                         LQUOTA_FLAG_RESET);
811         } else {
812                 qti->qti_gl_desc.lquota_desc.gl_hardlimit = lqe->lqe_hardlimit;
813                 qti->qti_gl_desc.lquota_desc.gl_softlimit = lqe->lqe_softlimit;
814                 qti->qti_gl_desc.lquota_desc.gl_time = lqe->lqe_gracetime;
815         }
816         qti->qti_gl_desc.lquota_desc.gl_ver       = ver;
817
818         /* look up ldlm resource associated with global index */
819         fid_build_reg_res_name(&qti->qti_fid, &qti->qti_resid);
820         res = ldlm_resource_get(pool->qpi_qmt->qmt_ns, &qti->qti_resid,
821                                 LDLM_PLAIN, 0);
822         if (IS_ERR(res)) {
823                 /* this might happen if no slaves have enqueued global quota
824                  * locks yet */
825                 LQUOTA_DEBUG(lqe, "failed to lookup ldlm resource associated "
826                              "with "DFID, PFID(&qti->qti_fid));
827                 RETURN_EXIT;
828         }
829
830         qmt_glimpse_lock(env, pool->qpi_qmt, res, &qti->qti_gl_desc,
831                          NULL, NULL);
832         ldlm_resource_putref(res);
833         EXIT;
834 }
835
836 /* Callback function used to select locks that should be glimpsed when
837  * broadcasting the new qunit value */
838 static int qmt_id_lock_cb(struct ldlm_lock *lock, struct lquota_entry *lqe)
839 {
840         struct obd_uuid *uuid = &(lock)->l_export->exp_client_uuid;
841         struct lqe_glbl_data *lgd = lqe->lqe_glbl_data;
842         int idx;
843         int stype = qmt_uuid2idx(uuid, &idx);
844
845         LASSERT(stype == QMT_STYPE_OST || stype == QMT_STYPE_MDT);
846
847         /* Quota pools support only OSTs, despite MDTs also could be registered
848          * as LQUOTA_RES_DT devices(DOM). */
849         if (qmt_dom(lqe_rtype(lqe), stype))
850                 return 1;
851         else
852                 return lgd->lqeg_arr[idx].lge_edquot_nu ||
853                        lgd->lqeg_arr[idx].lge_qunit_nu;
854 }
855
856
857 /*
858  * Send glimpse request on per-ID lock to push new qunit value to slave.
859  *
860  * \param env  - is the environment passed by the caller
861  * \param qmt  - is the quota master target device
862  * \param lqe  - is the lquota entry with the new qunit value
863  * \param uuid - is the uuid of the slave acquiring space, if any
864  */
865 static void qmt_id_lock_glimpse(const struct lu_env *env,
866                                 struct qmt_device *qmt,
867                                 struct lquota_entry *lqe, struct obd_uuid *uuid)
868 {
869         struct qmt_thread_info  *qti = qmt_info(env);
870         struct qmt_pool_info    *pool = lqe2qpi(lqe);
871         struct ldlm_resource    *res = NULL;
872         ENTRY;
873
874         if (!lqe->lqe_enforced)
875                 RETURN_EXIT;
876
877         lquota_generate_fid(&qti->qti_fid, pool->qpi_rtype, lqe_qtype(lqe));
878         fid_build_quota_res_name(&qti->qti_fid, &lqe->lqe_id, &qti->qti_resid);
879         res = ldlm_resource_get(qmt->qmt_ns, &qti->qti_resid, LDLM_PLAIN, 0);
880         if (IS_ERR(res)) {
881                 /* this might legitimately happens if slaves haven't had the
882                  * opportunity to enqueue quota lock yet. */
883                 LQUOTA_DEBUG(lqe, "failed to lookup ldlm resource for per-ID "
884                              "lock "DFID, PFID(&qti->qti_fid));
885                 lqe_write_lock(lqe);
886                 if (lqe->lqe_revoke_time == 0 &&
887                     lqe->lqe_qunit == pool->qpi_least_qunit)
888                         lqe->lqe_revoke_time = ktime_get_seconds();
889                 lqe_write_unlock(lqe);
890                 RETURN_EXIT;
891         }
892
893         lqe_write_lock(lqe);
894         /* The purpose of glimpse callback on per-ID lock is twofold:
895          * - notify slaves of new qunit value and hope they will release some
896          *   spare quota space in return
897          * - notify slaves that master ran out of quota space and there is no
898          *   need to send acquire request any more until further notice */
899
900         /* TODO: it is not clear how to implement below case for all lqes
901          * from where slaves will be notified in qmt_glimpse_lock. Because
902          * here we have just global lqe with an array of OSTs that should
903          * be notified. Theoretically we can find all lqes that includes
904          * these OSTs, but it is not trivial. So I would propose to move
905          * this case to another place ... */
906         if (lqe->lqe_revoke_time == 0 &&
907             lqe->lqe_qunit == pool->qpi_least_qunit)
908                 /* reset lqe_may_rel, it will be updated on glimpse callback
909                  * replies if needed */
910                 lqe->lqe_may_rel = 0;
911
912         /* The rebalance thread is the only thread which can issue glimpses */
913         LASSERT(!lqe->lqe_gl);
914         lqe->lqe_gl = true;
915         lqe_write_unlock(lqe);
916
917         /* issue glimpse callback to slaves */
918         if (lqe->lqe_glbl_data)
919                 qmt_glimpse_lock(env, qmt, res, &qti->qti_gl_desc,
920                                  qmt_id_lock_cb, lqe);
921
922         lqe_write_lock(lqe);
923         if (lqe->lqe_revoke_time == 0 &&
924             lqe->lqe_qunit == pool->qpi_least_qunit) {
925                 lqe->lqe_revoke_time = ktime_get_seconds();
926                 qmt_adjust_edquot(lqe, ktime_get_real_seconds());
927         }
928         LASSERT(lqe->lqe_gl);
929         lqe->lqe_gl = false;
930         lqe_write_unlock(lqe);
931
932         ldlm_resource_putref(res);
933         EXIT;
934 }
935
936 /*
937  * Schedule a glimpse request on per-ID locks to push new qunit value or
938  * edquot flag to quota slaves.
939  *
940  * \param qmt  - is the quota master target device
941  * \param lqe  - is the lquota entry with the new qunit value
942  */
943 void qmt_id_lock_notify(struct qmt_device *qmt, struct lquota_entry *lqe)
944 {
945         bool    added = false;
946         ENTRY;
947
948         LASSERT(lqe->lqe_is_global);
949         lqe_getref(lqe);
950         spin_lock(&qmt->qmt_reba_lock);
951         if (!qmt->qmt_stopping && list_empty(&lqe->lqe_link)) {
952                 list_add_tail(&lqe->lqe_link, &qmt->qmt_reba_list);
953                 added = true;
954                 if (qmt->qmt_reba_task)
955                         wake_up_process(qmt->qmt_reba_task);
956         }
957         spin_unlock(&qmt->qmt_reba_lock);
958
959         if (!added)
960                 lqe_putref(lqe);
961         EXIT;
962 }
963
964 struct qmt_reba_args {
965         struct qmt_device       *qra_dev;
966         struct lu_env            qra_env;
967         struct completion       *qra_started;
968 };
969
970 #ifndef TASK_IDLE
971 #define TASK_IDLE TASK_INTERRUPTIBLE
972 #endif
973
974 /*
975  * The rebalance thread is in charge of sending glimpse callbacks on per-ID
976  * quota locks owned by slaves in order to notify them of:
977  * - a qunit shrink in which case slaves might release quota space back in
978  *   glimpse reply.
979  * - set/clear edquot flag used to cache the "quota exhausted" state of the
980  *   master. When the flag is set, slaves know that there is no need to
981  *   try to acquire quota from the master since this latter has already
982  *   distributed all the space.
983  */
984 static int qmt_reba_thread(void *_args)
985 {
986         struct qmt_reba_args    *args = _args;
987         struct qmt_device       *qmt = args->qra_dev;
988         struct lu_env           *env = &args->qra_env;
989         struct lquota_entry     *lqe, *tmp;
990         ENTRY;
991
992         complete(args->qra_started);
993         while (({set_current_state(TASK_IDLE);
994                  !kthread_should_stop(); })) {
995
996                 spin_lock(&qmt->qmt_reba_lock);
997                 list_for_each_entry_safe(lqe, tmp, &qmt->qmt_reba_list,
998                                          lqe_link) {
999                         __set_current_state(TASK_RUNNING);
1000                         list_del_init(&lqe->lqe_link);
1001                         spin_unlock(&qmt->qmt_reba_lock);
1002
1003                         /* lqe_ref == 1 means we hold the last ref,
1004                          * so no need to send glimpse callbacks.
1005                          */
1006                         if (!kthread_should_stop() &&
1007                             atomic_read(&lqe->lqe_ref) > 1)
1008                                 qmt_id_lock_glimpse(env, qmt, lqe, NULL);
1009
1010                         lqe_putref(lqe);
1011                         spin_lock(&qmt->qmt_reba_lock);
1012                 }
1013                 spin_unlock(&qmt->qmt_reba_lock);
1014                 schedule();
1015         }
1016         __set_current_state(TASK_RUNNING);
1017
1018         lu_env_remove(env);
1019         lu_env_fini(env);
1020         OBD_FREE_PTR(args);
1021         RETURN(0);
1022 }
1023
1024 /*
1025  * Start rebalance thread. Called when the QMT is being setup
1026  */
1027 int qmt_start_reba_thread(struct qmt_device *qmt)
1028 {
1029         struct task_struct *task;
1030         struct qmt_reba_args *args;
1031         DECLARE_COMPLETION_ONSTACK(started);
1032         int rc;
1033         ENTRY;
1034
1035         OBD_ALLOC_PTR(args);
1036         if (args == NULL)
1037                 RETURN(-ENOMEM);
1038         args->qra_dev = qmt;
1039         args->qra_started = &started;
1040
1041         rc = lu_env_init(&args->qra_env, LCT_MD_THREAD);
1042         if (rc) {
1043                 CERROR("%s: failed to init env.\n", qmt->qmt_svname);
1044                 GOTO(out_env, rc);
1045         }
1046
1047         task = kthread_create(qmt_reba_thread, args,
1048                               "qmt_reba_%s", qmt->qmt_svname);
1049         if (IS_ERR(task)) {
1050                 CERROR("%s: failed to start rebalance thread (%ld)\n",
1051                        qmt->qmt_svname, PTR_ERR(task));
1052                 GOTO(out_env_fini, rc = PTR_ERR(task));
1053         }
1054
1055         rc = lu_env_add_task(&args->qra_env, task);
1056         if (rc) {
1057                 kthread_stop(task);
1058                 GOTO(out_env_fini, rc);
1059         }
1060         qmt->qmt_reba_task = task;
1061         wake_up_process(task);
1062         wait_for_completion(&started);
1063
1064         RETURN(0);
1065 out_env_fini:
1066         lu_env_fini(&args->qra_env);
1067 out_env:
1068         OBD_FREE_PTR(args);
1069         RETURN(rc);
1070 }
1071
1072 /*
1073  * Stop rebalance thread. Called when the QMT is about to shutdown.
1074  */
1075 void qmt_stop_reba_thread(struct qmt_device *qmt)
1076 {
1077         struct task_struct *task;
1078
1079         spin_lock(&qmt->qmt_reba_lock);
1080         task = qmt->qmt_reba_task;
1081         qmt->qmt_reba_task = NULL;
1082         spin_unlock(&qmt->qmt_reba_lock);
1083
1084         if (task)
1085                 kthread_stop(task);
1086
1087         LASSERT(list_empty(&qmt->qmt_reba_list));
1088 }