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