Whamcloud - gitweb
LU-2509 quota: various code cleanups
[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, 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 #ifndef EXPORT_SYMTAB
32 # define EXPORT_SYMTAB
33 #endif
34
35 #define DEBUG_SUBSYSTEM S_LQUOTA
36
37 #include <lustre_dlm.h>
38 #include <obd_class.h>
39
40 #include "qmt_internal.h"
41
42 /* intent policy function called from mdt_intent_opc() when the intent is of
43  * quota type */
44 int qmt_intent_policy(const struct lu_env *env, struct lu_device *ld,
45                       struct ptlrpc_request *req, struct ldlm_lock **lockp,
46                       int flags)
47 {
48         struct qmt_device       *qmt = lu2qmt_dev(ld);
49         struct ldlm_intent      *it;
50         struct quota_body       *reqbody;
51         struct quota_body       *repbody;
52         struct obd_uuid         *uuid;
53         struct lquota_lvb       *lvb;
54         struct ldlm_resource    *res = (*lockp)->l_resource;
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         uuid = &(*lockp)->l_export->exp_client_uuid;
83         switch (it->opc) {
84
85         case IT_QUOTA_DQACQ: {
86                 struct lquota_entry     *lqe;
87                 struct ldlm_lock        *lock;
88
89                 if (res->lr_name.name[LUSTRE_RES_ID_QUOTA_SEQ_OFF] == 0)
90                         /* acquire on global lock? something is wrong ... */
91                         GOTO(out, rc = -EPROTO);
92
93                 /* verify global lock isn't stale */
94                 if (!lustre_handle_is_used(&reqbody->qb_glb_lockh))
95                         GOTO(out, rc = -ENOLCK);
96
97                 lock = ldlm_handle2lock(&reqbody->qb_glb_lockh);
98                 if (lock == NULL)
99                         GOTO(out, rc = -ENOLCK);
100                 LDLM_LOCK_PUT(lock);
101
102                 lqe = res->lr_lvb_data;
103                 LASSERT(lqe != NULL);
104                 lqe_getref(lqe);
105
106                 /* acquire quota space */
107                 rc = qmt_dqacq0(env, lqe, qmt, uuid, reqbody->qb_flags,
108                                 reqbody->qb_count, reqbody->qb_usage,
109                                 repbody);
110                 lqe_putref(lqe);
111                 if (rc)
112                         GOTO(out, rc);
113                 break;
114         }
115
116         case IT_QUOTA_CONN:
117                 /* new connection from slave */
118
119                 if (res->lr_name.name[LUSTRE_RES_ID_QUOTA_SEQ_OFF] != 0)
120                         /* connection on per-ID lock? something is wrong ... */
121                         GOTO(out, rc = -EPROTO);
122
123                 rc = qmt_pool_new_conn(env, qmt, &reqbody->qb_fid,
124                                        &repbody->qb_slv_fid,
125                                        &repbody->qb_slv_ver, uuid);
126                 if (rc)
127                         GOTO(out, rc);
128                 break;
129
130         default:
131                 CERROR("%s: invalid intent opcode: "LPU64"\n", qmt->qmt_svname,
132                        it->opc);
133                 GOTO(out, rc = err_serious(-EINVAL));
134         }
135
136         /* on success, pack lvb in reply */
137         lvb = req_capsule_server_get(&req->rq_pill, &RMF_DLM_LVB);
138         lvb_len = ldlm_lvbo_size(*lockp);
139         lvb_len = ldlm_lvbo_fill(*lockp, lvb, lvb_len);
140         req_capsule_shrink(&req->rq_pill, &RMF_DLM_LVB, lvb_len, RCL_SERVER);
141         EXIT;
142 out:
143         return rc;
144 }
145
146 /*
147  * Initialize quota LVB associated with quota indexes.
148  * Called with res->lr_lvb_sem held
149  */
150 int qmt_lvbo_init(struct lu_device *ld, struct ldlm_resource *res)
151 {
152         struct lu_env           *env;
153         struct qmt_thread_info  *qti;
154         struct qmt_device       *qmt = lu2qmt_dev(ld);
155         int                      pool_id, pool_type, qtype;
156         int                      rc;
157         ENTRY;
158
159         LASSERT(res != NULL);
160
161         if (res->lr_type != LDLM_PLAIN)
162                 RETURN(-ENOTSUPP);
163
164         if (res->lr_lvb_data ||
165             res->lr_name.name[LUSTRE_RES_ID_SEQ_OFF] != FID_SEQ_QUOTA_GLB)
166                 RETURN(0);
167
168         OBD_ALLOC_PTR(env);
169         if (env == NULL)
170                 RETURN(-ENOMEM);
171
172         /* initialize environment */
173         rc = lu_env_init(env, LCT_MD_THREAD);
174         if (rc) {
175                 OBD_FREE_PTR(env);
176                 RETURN(rc);
177         }
178         qti = qmt_info(env);
179
180         /* extract global index FID and quota identifier */
181         fid_extract_quota_resid(&res->lr_name, &qti->qti_fid, &qti->qti_id);
182
183         /* sanity check the global index FID */
184         rc = lquota_extract_fid(&qti->qti_fid, &pool_id, &pool_type, &qtype);
185         if (rc) {
186                 CERROR("can't extract pool information from FID "DFID"\n",
187                        PFID(&qti->qti_fid));
188                 GOTO(out, rc);
189         }
190
191         if (res->lr_name.name[LUSTRE_RES_ID_QUOTA_SEQ_OFF] != 0) {
192                 /* no ID quota lock associated with UID/GID 0 or with a seq 0,
193                  * we are thus dealing with an ID lock. */
194                 struct lquota_entry     *lqe;
195
196                 /* Find the quota entry associated with the quota id */
197                 lqe = qmt_pool_lqe_lookup(env, qmt, pool_id, pool_type, qtype,
198                                           &qti->qti_id);
199                 if (IS_ERR(lqe))
200                         GOTO(out, rc = PTR_ERR(lqe));
201
202                 /* store reference to lqe in lr_lvb_data */
203                 res->lr_lvb_data = lqe;
204                 LQUOTA_DEBUG(lqe, "initialized res lvb");
205         } else {
206                 struct dt_object        *obj;
207
208                 /* lookup global index */
209                 obj = dt_locate(env, qmt->qmt_child, &qti->qti_fid);
210                 if (IS_ERR(obj))
211                         GOTO(out, rc = PTR_ERR(obj));
212                 if (!dt_object_exists(obj)) {
213                         lu_object_put(env, &obj->do_lu);
214                         GOTO(out, rc = -ENOENT);
215                 }
216
217                 /* store reference to global index object in lr_lvb_data */
218                 res->lr_lvb_data = obj;
219                 CDEBUG(D_QUOTA, DFID" initialized lvb\n", PFID(&qti->qti_fid));
220         }
221
222         res->lr_lvb_len  = sizeof(struct lquota_lvb);
223         EXIT;
224 out:
225         lu_env_fini(env);
226         OBD_FREE_PTR(env);
227         return rc;
228 }
229
230 /*
231  * Update LVB associated with the global quota index.
232  * This function is called from the DLM itself after a glimpse callback, in this
233  * case valid ptlrpc request is passed.
234  */
235 int qmt_lvbo_update(struct lu_device *ld, struct ldlm_resource *res,
236                     struct ptlrpc_request *req, int increase_only)
237 {
238         struct lu_env           *env;
239         struct qmt_thread_info  *qti;
240         struct qmt_device       *qmt = lu2qmt_dev(ld);
241         struct lquota_entry     *lqe;
242         struct lquota_lvb       *lvb;
243         struct ldlm_lock        *lock;
244         struct obd_export       *exp;
245         int                      rc = 0;
246         ENTRY;
247
248         LASSERT(res != NULL);
249
250         if (req == NULL)
251                 RETURN(0);
252
253         if (res->lr_name.name[LUSTRE_RES_ID_QUOTA_SEQ_OFF] == 0)
254                 /* no need to update lvb for global quota locks */
255                 RETURN(0);
256
257         lvb = req_capsule_server_swab_get(&req->rq_pill, &RMF_DLM_LVB,
258                                           lustre_swab_lquota_lvb);
259         if (lvb == NULL) {
260                 CERROR("%s: failed to extract lvb from request\n",
261                        qmt->qmt_svname);
262                 RETURN(-EFAULT);
263         }
264
265         lqe = res->lr_lvb_data;
266         LASSERT(lqe != NULL);
267         lqe_getref(lqe);
268
269         LQUOTA_DEBUG(lqe, "releasing:"LPU64" may release:"LPU64,
270                      lvb->lvb_id_rel, lvb->lvb_id_may_rel);
271
272         if (lvb->lvb_id_rel == 0) {
273                 /* nothing to release */
274                 if (lvb->lvb_id_may_rel != 0)
275                         /* but might still release later ... */
276                         lqe->lqe_may_rel += lvb->lvb_id_may_rel;
277                 GOTO(out_lqe, rc = 0);
278         }
279
280         /* allocate environement */
281         OBD_ALLOC_PTR(env);
282         if (env == NULL)
283                 GOTO(out_lqe, rc = -ENOMEM);
284
285         /* initialize environment */
286         rc = lu_env_init(env, LCT_MD_THREAD);
287         if (rc)
288                 GOTO(out_env, rc);
289         qti = qmt_info(env);
290
291         /* The request is a glimpse callback which was sent via the
292          * reverse import to the slave. What we care about here is the
293          * export associated with the slave and req->rq_export is
294          * definitely not what we are looking for (it is actually set to
295          * NULL here).
296          * Therefore we extract the lock from the request argument
297          * and use lock->l_export. */
298         lock = ldlm_request_lock(req);
299         if (IS_ERR(lock)) {
300                 CERROR("%s: failed to get lock from request!\n",
301                        qmt->qmt_svname);
302                 GOTO(out_env_init, rc = PTR_ERR(lock));
303         }
304
305         exp = class_export_get(lock->l_export);
306         if (exp == NULL) {
307                 CERROR("%s: failed to get export from lock!\n",
308                        qmt->qmt_svname);
309                 GOTO(out_env_init, rc = -EFAULT);
310         }
311
312         /* release quota space */
313         rc = qmt_dqacq0(env, lqe, qmt, &exp->exp_client_uuid,
314                         QUOTA_DQACQ_FL_REL, lvb->lvb_id_rel, 0, &qti->qti_body);
315         if (rc || qti->qti_body.qb_count != lvb->lvb_id_rel)
316                 LQUOTA_ERROR(lqe, "failed to release quota space on glimpse "
317                              LPU64"!="LPU64" rc:%d\n", qti->qti_body.qb_count,
318                              lvb->lvb_id_rel, rc);
319         class_export_put(exp);
320         if (rc)
321                 GOTO(out_env_init, rc);
322         EXIT;
323 out_env_init:
324         lu_env_fini(env);
325 out_env:
326         OBD_FREE_PTR(env);
327 out_lqe:
328         lqe_putref(lqe);
329         return rc;
330 }
331
332 /*
333  * Report size of lvb to ldlm layer in order to allocate lvb buffer
334  * As far as quota locks are concerned, the size is static and is the same
335  * for both global and per-ID locks which shares the same lvb format.
336  */
337 int qmt_lvbo_size(struct lu_device *ld, struct ldlm_lock *lock)
338 {
339         return sizeof(struct lquota_lvb);
340 }
341
342 /*
343  * Fill request buffer with quota lvb
344  */
345 int qmt_lvbo_fill(struct lu_device *ld, struct ldlm_lock *lock, void *lvb,
346                   int lvblen)
347 {
348         struct ldlm_resource    *res = lock->l_resource;
349         struct lquota_lvb       *qlvb = lvb;
350         ENTRY;
351
352         LASSERT(res != NULL);
353
354         if (res->lr_type != LDLM_PLAIN || res->lr_lvb_data == NULL ||
355             res->lr_name.name[LUSTRE_RES_ID_SEQ_OFF] != FID_SEQ_QUOTA_GLB)
356                 RETURN(-EINVAL);
357
358         if (res->lr_name.name[LUSTRE_RES_ID_QUOTA_SEQ_OFF] != 0) {
359                 /* no ID quota lock associated with UID/GID 0 or with a seq 0,
360                  * we are thus dealing with an ID lock. */
361                 struct lquota_entry     *lqe = res->lr_lvb_data;
362
363                 /* return current qunit value & edquot flags in lvb */
364                 lqe_getref(lqe);
365                 qlvb->lvb_id_qunit = lqe->lqe_qunit;
366                 qlvb->lvb_flags = 0;
367                 if (lqe->lqe_edquot)
368                         qlvb->lvb_flags = LQUOTA_FL_EDQUOT;
369                 lqe_putref(lqe);
370         } else {
371                 /* global quota lock */
372                 struct lu_env           *env;
373                 int                      rc;
374                 struct dt_object        *obj = res->lr_lvb_data;
375
376                 OBD_ALLOC_PTR(env);
377                 if (env == NULL)
378                         RETURN(-ENOMEM);
379
380                 /* initialize environment */
381                 rc = lu_env_init(env, LCT_LOCAL);
382                 if (rc) {
383                         OBD_FREE_PTR(env);
384                         RETURN(rc);
385                 }
386
387                 /* return current version of global index */
388                 qlvb->lvb_glb_ver = dt_version_get(env, obj);
389
390                 lu_env_fini(env);
391                 OBD_FREE_PTR(env);
392         }
393
394         RETURN(sizeof(struct lquota_lvb));
395 }
396
397 /*
398  * Free lvb associated with a given ldlm resource
399  * we don't really allocate a lvb, lr_lvb_data just points to
400  * the appropriate backend structures.
401  */
402 int qmt_lvbo_free(struct lu_device *ld, struct ldlm_resource *res)
403 {
404         ENTRY;
405
406         if (res->lr_lvb_data == NULL)
407                 RETURN(0);
408
409         if (res->lr_name.name[LUSTRE_RES_ID_QUOTA_SEQ_OFF] != 0) {
410                 struct lquota_entry     *lqe = res->lr_lvb_data;
411
412                 /* release lqe reference */
413                 lqe_putref(lqe);
414         } else {
415                 struct dt_object        *obj = res->lr_lvb_data;
416                 struct lu_env           *env;
417                 int                      rc;
418
419                 OBD_ALLOC_PTR(env);
420                 if (env == NULL)
421                         RETURN(-ENOMEM);
422
423                 /* initialize environment */
424                 rc = lu_env_init(env, LCT_LOCAL);
425                 if (rc) {
426                         OBD_FREE_PTR(env);
427                         RETURN(rc);
428                 }
429
430                 /* release object reference */
431                 lu_object_put(env, &obj->do_lu);
432                 lu_env_fini(env);
433                 OBD_FREE_PTR(env);
434         }
435
436         res->lr_lvb_data = NULL;
437         res->lr_lvb_len  = 0;
438
439         RETURN(0);
440 }
441
442 typedef int (*qmt_glimpse_cb_t)(const struct lu_env *, struct qmt_device *,
443                                 struct obd_uuid *, union ldlm_gl_desc *,
444                                 void *);
445 /*
446  * Send glimpse callback to slaves holding a lock on resource \res.
447  * This is used to notify slaves of new quota settings or to claim quota space
448  * back.
449  *
450  * \param env  - is the environment passed by the caller
451  * \param qmt  - is the quota master target
452  * \param res  - is the dlm resource associated with the quota object
453  * \param desc - is the glimpse descriptor to pack in glimpse callback
454  * \param cb   - is the callback function called on every lock and determine
455  *               whether a glimpse should be issued
456  * \param arg  - is an opaq parameter passed to the callback function
457  */
458 static int qmt_glimpse_lock(const struct lu_env *env, struct qmt_device *qmt,
459                             struct ldlm_resource *res, union ldlm_gl_desc *desc,
460                             qmt_glimpse_cb_t cb, void *arg)
461 {
462         cfs_list_t      *tmp, *pos;
463         CFS_LIST_HEAD(gl_list);
464         int              rc = 0;
465         ENTRY;
466
467         lock_res(res);
468         /* scan list of granted locks */
469         cfs_list_for_each(pos, &res->lr_granted) {
470                 struct ldlm_glimpse_work        *work;
471                 struct ldlm_lock                *lock;
472                 struct obd_uuid                 *uuid;
473
474                 lock = cfs_list_entry(pos, struct ldlm_lock, l_res_link);
475                 LASSERT(lock->l_export);
476                 uuid = &lock->l_export->exp_client_uuid;
477
478                 if (cb != NULL) {
479                         rc = cb(env, qmt, uuid, desc, arg);
480                         if (rc == 0)
481                                 /* slave should not be notified */
482                                 continue;
483                         if (rc < 0)
484                                 /* something wrong happened, we still notify */
485                                 CERROR("%s: callback function failed to "
486                                        "determine whether slave %s should be "
487                                        "notified (%d)\n", qmt->qmt_svname,
488                                        obd_uuid2str(uuid), rc);
489                 }
490
491                 OBD_ALLOC_PTR(work);
492                 if (work == NULL) {
493                         CERROR("%s: failed to notify %s\n", qmt->qmt_svname,
494                                obd_uuid2str(uuid));
495                         continue;
496                 }
497
498                 cfs_list_add_tail(&work->gl_list, &gl_list);
499                 work->gl_lock  = LDLM_LOCK_GET(lock);
500                 work->gl_flags = 0;
501                 work->gl_desc  = desc;
502
503         }
504         unlock_res(res);
505
506         if (cfs_list_empty(&gl_list)) {
507                 CDEBUG(D_QUOTA, "%s: nobody to notify\n", qmt->qmt_svname);
508                 RETURN(0);
509         }
510
511         /* issue glimpse callbacks to all connected slaves */
512         rc = ldlm_glimpse_locks(res, &gl_list);
513
514         cfs_list_for_each_safe(pos, tmp, &gl_list) {
515                 struct ldlm_glimpse_work *work;
516
517                 work = cfs_list_entry(pos, struct ldlm_glimpse_work, gl_list);
518
519                 cfs_list_del(&work->gl_list);
520                 CERROR("%s: failed to notify %s of new quota settings\n",
521                        qmt->qmt_svname,
522                        obd_uuid2str(&work->gl_lock->l_export->exp_client_uuid));
523                 LDLM_LOCK_RELEASE(work->gl_lock);
524                 OBD_FREE_PTR(work);
525         }
526
527         RETURN(rc);
528 }
529
530 /*
531  * Send glimpse request to all global quota locks to push new quota setting to
532  * slaves.
533  *
534  * \param env - is the environment passed by the caller
535  * \param lqe - is the lquota entry which has new settings
536  * \param ver - is the version associated with the setting change
537  */
538 void qmt_glb_lock_notify(const struct lu_env *env, struct lquota_entry *lqe,
539                          __u64 ver)
540 {
541         struct qmt_thread_info  *qti = qmt_info(env);
542         struct qmt_pool_info    *pool = lqe2qpi(lqe);
543         struct ldlm_resource    *res = NULL;
544         int                      rc;
545         ENTRY;
546
547         lquota_generate_fid(&qti->qti_fid, pool->qpi_key & 0x0000ffff,
548                             pool->qpi_key >> 16, lqe->lqe_site->lqs_qtype);
549
550         /* send glimpse callback to notify slaves of new quota settings */
551         qti->qti_gl_desc.lquota_desc.gl_id        = lqe->lqe_id;
552         qti->qti_gl_desc.lquota_desc.gl_flags     = 0;
553         qti->qti_gl_desc.lquota_desc.gl_hardlimit = lqe->lqe_hardlimit;
554         qti->qti_gl_desc.lquota_desc.gl_softlimit = lqe->lqe_softlimit;
555         qti->qti_gl_desc.lquota_desc.gl_time      = lqe->lqe_gracetime;
556         qti->qti_gl_desc.lquota_desc.gl_ver       = ver;
557
558         /* look up ldlm resource associated with global index */
559         fid_build_reg_res_name(&qti->qti_fid, &qti->qti_resid);
560         res = ldlm_resource_get(pool->qpi_qmt->qmt_ns, NULL, &qti->qti_resid,
561                                 LDLM_PLAIN, 0);
562         if (res == NULL) {
563                 /* this might happen if no slaves have enqueued global quota
564                  * locks yet */
565                 LQUOTA_DEBUG(lqe, "failed to lookup ldlm resource associated "
566                              "with "DFID, PFID(&qti->qti_fid));
567                 RETURN_EXIT;
568         }
569
570         rc = qmt_glimpse_lock(env, pool->qpi_qmt, res, &qti->qti_gl_desc,
571                               NULL, NULL);
572         ldlm_resource_putref(res);
573         EXIT;
574 }
575
576 /* Callback function used to select locks that should be glimpsed when
577  * broadcasting the new qunit value */
578 static int qmt_id_lock_cb(const struct lu_env *env, struct qmt_device *qmt,
579                           struct obd_uuid *uuid, union ldlm_gl_desc *desc,
580                           void *arg)
581 {
582         struct obd_uuid *slv_uuid = arg;
583
584         if (slv_uuid != NULL && obd_uuid_equals(uuid, slv_uuid))
585                 RETURN(0);
586         RETURN(+1);
587 }
588
589 /*
590  * Send glimpse request on per-ID lock to push new qunit value to slave.
591  *
592  * \param env  - is the environment passed by the caller
593  * \param qmt  - is the quota master target device
594  * \param lqe  - is the lquota entry with the new qunit value
595  * \param uuid - is the uuid of the slave acquiring space, if any
596  */
597 static void qmt_id_lock_glimpse(const struct lu_env *env,
598                                 struct qmt_device *qmt,
599                                 struct lquota_entry *lqe, struct obd_uuid *uuid)
600 {
601         struct qmt_thread_info  *qti = qmt_info(env);
602         struct qmt_pool_info    *pool = lqe2qpi(lqe);
603         struct ldlm_resource    *res = NULL;
604         int                      rc;
605         ENTRY;
606
607         if (!lqe->lqe_enforced)
608                 RETURN_EXIT;
609
610         lquota_generate_fid(&qti->qti_fid, pool->qpi_key & 0x0000ffff,
611                             pool->qpi_key >> 16, lqe->lqe_site->lqs_qtype);
612         fid_build_quota_resid(&qti->qti_fid, &lqe->lqe_id, &qti->qti_resid);
613         res = ldlm_resource_get(qmt->qmt_ns, NULL, &qti->qti_resid, LDLM_PLAIN,
614                                 0);
615         if (res == NULL) {
616                 /* this might legitimately happens if slaves haven't had the
617                  * opportunity to enqueue quota lock yet. */
618                 LQUOTA_DEBUG(lqe, "failed to lookup ldlm resource for per-ID "
619                              "lock "DFID, PFID(&qti->qti_fid));
620                 lqe_write_lock(lqe);
621                 if (lqe->lqe_revoke_time == 0 &&
622                     lqe->lqe_qunit == pool->qpi_least_qunit)
623                         lqe->lqe_revoke_time = cfs_time_current_64();
624                 lqe_write_unlock(lqe);
625                 RETURN_EXIT;
626         }
627
628         lqe_write_lock(lqe);
629         /* The purpose of glimpse callback on per-ID lock is twofold:
630          * - notify slaves of new qunit value and hope they will release some
631          *   spare quota space in return
632          * - notify slaves that master ran out of quota space and there is no
633          *   need to send acquire request any more until further notice */
634
635         /* fill glimpse descriptor with lqe settings */
636         if (lqe->lqe_edquot)
637                 qti->qti_gl_desc.lquota_desc.gl_flags = LQUOTA_FL_EDQUOT;
638         else
639                 qti->qti_gl_desc.lquota_desc.gl_flags = 0;
640         qti->qti_gl_desc.lquota_desc.gl_qunit = lqe->lqe_qunit;
641
642         if (lqe->lqe_revoke_time == 0 &&
643             qti->qti_gl_desc.lquota_desc.gl_qunit == pool->qpi_least_qunit)
644                 /* reset lqe_may_rel, it will be updated on glimpse callback
645                  * replies if needed */
646                 lqe->lqe_may_rel = 0;
647
648         /* The rebalance thread is the only thread which can issue glimpses */
649         LASSERT(!lqe->lqe_gl);
650         lqe->lqe_gl = true;
651         lqe_write_unlock(lqe);
652
653         /* issue glimpse callback to slaves */
654         rc = qmt_glimpse_lock(env, qmt, res, &qti->qti_gl_desc,
655                               uuid ? qmt_id_lock_cb : NULL, (void *)uuid);
656
657         lqe_write_lock(lqe);
658         if (lqe->lqe_revoke_time == 0 &&
659             qti->qti_gl_desc.lquota_desc.gl_qunit == pool->qpi_least_qunit &&
660             lqe->lqe_qunit == pool->qpi_least_qunit) {
661                 lqe->lqe_revoke_time = cfs_time_current_64();
662                 qmt_adjust_edquot(lqe, cfs_time_current_sec());
663         }
664         LASSERT(lqe->lqe_gl);
665         lqe->lqe_gl = false;
666         lqe_write_unlock(lqe);
667
668         ldlm_resource_putref(res);
669         EXIT;
670 }
671
672 /*
673  * Schedule a glimpse request on per-ID locks to push new qunit value or
674  * edquot flag to quota slaves.
675  *
676  * \param qmt  - is the quota master target device
677  * \param lqe  - is the lquota entry with the new qunit value
678  */
679 void qmt_id_lock_notify(struct qmt_device *qmt, struct lquota_entry *lqe)
680 {
681         bool    added = false;
682         ENTRY;
683
684         lqe_getref(lqe);
685         spin_lock(&qmt->qmt_reba_lock);
686         if (!qmt->qmt_stopping && cfs_list_empty(&lqe->lqe_link)) {
687                 cfs_list_add_tail(&lqe->lqe_link, &qmt->qmt_reba_list);
688                 added = true;
689         }
690         spin_unlock(&qmt->qmt_reba_lock);
691
692         if (added)
693                 cfs_waitq_signal(&qmt->qmt_reba_thread.t_ctl_waitq);
694         else
695                 lqe_putref(lqe);
696         EXIT;
697 }
698
699 /*
700  * The rebalance thread is in charge of sending glimpse callbacks on per-ID
701  * quota locks owned by slaves in order to notify them of:
702  * - a qunit shrink in which case slaves might release quota space back in
703  *   glimpse reply.
704  * - set/clear edquot flag used to cache the "quota exhausted" state of the
705  *   master. When the flag is set, slaves know that there is no need to
706  *   try to acquire quota from the master since this latter has already
707  *   distributed all the space.
708  */
709 static int qmt_reba_thread(void *arg)
710 {
711         struct qmt_device       *qmt = (struct qmt_device *)arg;
712         struct ptlrpc_thread    *thread = &qmt->qmt_reba_thread;
713         struct l_wait_info       lwi = { 0 };
714         struct lu_env           *env;
715         struct lquota_entry     *lqe, *tmp;
716         char                     pname[MTI_NAME_MAXLEN];
717         int                      rc;
718         ENTRY;
719
720         OBD_ALLOC_PTR(env);
721         if (env == NULL)
722                 RETURN(-ENOMEM);
723
724         rc = lu_env_init(env, LCT_MD_THREAD);
725         if (rc) {
726                 CERROR("%s: failed to init env.", qmt->qmt_svname);
727                 OBD_FREE_PTR(env);
728                 RETURN(rc);
729         }
730
731         snprintf(pname, MTI_NAME_MAXLEN, "qmt_reba_%s", qmt->qmt_svname);
732         cfs_daemonize(pname);
733
734         thread_set_flags(thread, SVC_RUNNING);
735         cfs_waitq_signal(&thread->t_ctl_waitq);
736
737         while (1) {
738                 l_wait_event(thread->t_ctl_waitq,
739                              !cfs_list_empty(&qmt->qmt_reba_list) ||
740                              !thread_is_running(thread), &lwi);
741
742                 spin_lock(&qmt->qmt_reba_lock);
743                 cfs_list_for_each_entry_safe(lqe, tmp, &qmt->qmt_reba_list,
744                                              lqe_link) {
745                         cfs_list_del_init(&lqe->lqe_link);
746                         spin_unlock(&qmt->qmt_reba_lock);
747
748                         if (thread_is_running(thread))
749                                 qmt_id_lock_glimpse(env, qmt, lqe, NULL);
750
751                         lqe_putref(lqe);
752                         spin_lock(&qmt->qmt_reba_lock);
753                 }
754                 spin_unlock(&qmt->qmt_reba_lock);
755
756                 if (!thread_is_running(thread))
757                         break;
758         }
759         lu_env_fini(env);
760         OBD_FREE_PTR(env);
761         thread_set_flags(thread, SVC_STOPPED);
762         cfs_waitq_signal(&thread->t_ctl_waitq);
763         RETURN(rc);
764 }
765
766 /*
767  * Start rebalance thread. Called when the QMT is being setup
768  */
769 int qmt_start_reba_thread(struct qmt_device *qmt)
770 {
771         struct ptlrpc_thread    *thread = &qmt->qmt_reba_thread;
772         struct l_wait_info       lwi    = { 0 };
773         int                      rc;
774         ENTRY;
775
776         rc = cfs_create_thread(qmt_reba_thread, (void *)qmt, 0);
777         if (rc < 0) {
778                 CERROR("%s: failed to start rebalance thread (%d)\n",
779                        qmt->qmt_svname, rc);
780                 thread_set_flags(thread, SVC_STOPPED);
781                 RETURN(rc);
782         }
783
784         l_wait_event(thread->t_ctl_waitq,
785                      thread_is_running(thread) || thread_is_stopped(thread),
786                      &lwi);
787
788         RETURN(0);
789 }
790
791 /*
792  * Stop rebalance thread. Called when the QMT is about to shutdown.
793  */
794 void qmt_stop_reba_thread(struct qmt_device *qmt)
795 {
796         struct ptlrpc_thread *thread = &qmt->qmt_reba_thread;
797
798         if (!thread_is_stopped(thread)) {
799                 struct l_wait_info lwi = { 0 };
800
801                 thread_set_flags(thread, SVC_STOPPING);
802                 cfs_waitq_signal(&thread->t_ctl_waitq);
803
804                 l_wait_event(thread->t_ctl_waitq, thread_is_stopped(thread),
805                              &lwi);
806         }
807         LASSERT(cfs_list_empty(&qmt->qmt_reba_list));
808 }