Whamcloud - gitweb
LU-16339 quota: notify OSTs until lge_qunit_nu is set
[fs/lustre-release.git] / lustre / quota / qmt_internal.h
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
28 #ifndef _QMT_INTERNAL_H
29 #define _QMT_INTERNAL_H
30
31 #include "lquota_internal.h"
32
33 /*
34  * The Quota Master Target Device.
35  * The qmt is responsible for:
36  * - all interactions with MDT0 (provide request handlers, share ldlm namespace,
37  *   manage ldlm lvbo, ...)
38  * - all quota lock management (i.e. global quota locks as well as per-ID locks)
39  * - manage the quota pool configuration
40  *
41  * That's the structure MDT0 connects to in mdt_quota_init().
42  */
43 struct qmt_device {
44         /* Super-class. dt_device/lu_device for this master target */
45         struct dt_device        qmt_dt_dev;
46
47         /* service name of this qmt */
48         char                    qmt_svname[MAX_OBD_NAME];
49         /* root directory for this qmt */
50         struct dt_object        *qmt_root;
51
52         /* Reference to the next device in the side stack
53          * The child device is actually the OSD device where we store the quota
54          * index files */
55         struct obd_export       *qmt_child_exp;
56         struct dt_device        *qmt_child;
57
58         /* pointer to ldlm namespace to be used for quota locks */
59         struct ldlm_namespace   *qmt_ns;
60
61         /* List of pools managed by this master target */
62         struct list_head         qmt_pool_list;
63         /* rw semaphore to protect pool list */
64         struct rw_semaphore      qmt_pool_lock;
65
66         /* procfs root directory for this qmt */
67         struct proc_dir_entry   *qmt_proc;
68
69         /* dedicated thread in charge of space rebalancing */
70         struct task_struct      *qmt_reba_task;
71
72         /* list of lqe entry which need space rebalancing */
73         struct list_head         qmt_reba_list;
74
75         /* lock protecting rebalancing list */
76         spinlock_t               qmt_reba_lock;
77
78         unsigned long            qmt_stopping:1; /* qmt is stopping */
79
80 };
81
82 struct qmt_pool_info;
83 #define QPI_MAXNAME     (LOV_MAXPOOLNAME + 1)
84 #define qmt_pool_global(qpi) \
85         (!strncmp(qpi->qpi_name, GLB_POOL_NAME, \
86                   strlen(GLB_POOL_NAME) + 1) ? true : false)
87 /* Draft for mdt pools */
88 union qmt_sarray {
89         struct lu_tgt_pool      osts;
90 };
91
92 /* Since DOM support, data resources can exist
93  * on both MDT and OST targets. */
94 enum {
95         QMT_STYPE_MDT,
96         QMT_STYPE_OST,
97         QMT_STYPE_CNT
98 };
99
100 #define qmt_dom(rtype, stype) \
101         ((rtype == LQUOTA_RES_DT && \
102          stype == QMT_STYPE_MDT) ? true : false)
103
104 enum {
105         /* set while recalc_thread is working */
106         QPI_FLAG_RECALC_OFFSET,
107         QPI_FLAG_STATE_INITED,
108 };
109
110 /*
111  * Per-pool quota information.
112  * The qmt creates one such structure for each pool
113  * with quota enforced. All the structures are kept in a list.
114  * We currently only support the default data pool and default metadata pool.
115  */
116 struct qmt_pool_info {
117         /* chained list of all pools managed by the same qmt */
118         struct list_head         qpi_linkage;
119
120         /* Could be  LQUOTA_RES_MD or LQUOTA_RES_DT */
121         int                      qpi_rtype;
122         char                     qpi_name[QPI_MAXNAME];
123
124         union qmt_sarray         qpi_sarr;
125         /* recalculation thread pointer */
126         struct task_struct      *qpi_recalc_task;
127         /* rw semaphore to avoid acquire/release during
128          * pool recalculation. */
129         struct rw_semaphore      qpi_recalc_sem;
130         unsigned long            qpi_flags;
131
132         /* track users of this pool instance */
133         atomic_t                 qpi_ref;
134
135         /* back pointer to master target
136          * immutable after creation. */
137         struct qmt_device       *qpi_qmt;
138
139         /* pointer to dt object associated with global indexes for both user
140          * and group quota */
141         struct dt_object        *qpi_glb_obj[LL_MAXQUOTAS];
142
143         /* A pool supports two different quota types: user and group quota.
144          * Each quota type has its own global index and lquota_entry hash table.
145          */
146         struct lquota_site      *qpi_site[LL_MAXQUOTAS];
147
148         /* number of slaves registered for each quota types */
149         int                      qpi_slv_nr[QMT_STYPE_CNT][LL_MAXQUOTAS];
150
151         /* reference on lqe (ID 0) storing grace time. */
152         struct lquota_entry     *qpi_grace_lqe[LL_MAXQUOTAS];
153
154         /* procfs root directory for this pool */
155         struct proc_dir_entry   *qpi_proc;
156
157         /* pool directory where all indexes related to this pool instance are
158          * stored */
159         struct dt_object        *qpi_root;
160
161         /* Global quota parameters which apply to all quota type */
162         /* the least value of qunit */
163         unsigned long            qpi_least_qunit;
164
165         /* Least value of qunit when soft limit is exceeded.
166          *
167          * When soft limit is exceeded, qunit will be shrinked to least_qunit
168          * (1M for block limit), that results in significant write performance
169          * drop since the client will turn to sync write from now on.
170          *
171          * To retain the write performance in an acceptable level, we choose
172          * to sacrifice grace time accuracy a bit and use a larger least_qunit
173          * when soft limit is exceeded. It's (qpi_least_qunit * 4) by default,
174          * and user may enlarge it via procfs to get even better performance
175          * (with the cost of losing more grace time accuracy).
176          *
177          * See qmt_calc_softlimit().
178          */
179         unsigned long            qpi_soft_least_qunit;
180 };
181
182 static inline int qpi_slv_nr(struct qmt_pool_info *pool, int qtype)
183 {
184         int i, sum = 0;
185
186         for (i = 0; i < QMT_STYPE_CNT; i++)
187                 sum += pool->qpi_slv_nr[i][qtype];
188
189         return sum;
190 }
191
192 static inline int qpi_slv_nr_by_rtype(struct qmt_pool_info *pool, int qtype)
193 {
194         if (pool->qpi_rtype == LQUOTA_RES_DT)
195                 /* Here should be qpi_slv_nr() if MDTs will be added
196                  * to quota pools */
197                 return pool->qpi_slv_nr[QMT_STYPE_OST][qtype];
198         else
199                 return pool->qpi_slv_nr[QMT_STYPE_MDT][qtype];
200 }
201 /*
202  * Helper routines and prototypes
203  */
204
205 /* helper routine to find qmt_pool_info associated a lquota_entry */
206 static inline struct qmt_pool_info *lqe2qpi(struct lquota_entry *lqe)
207 {
208         LASSERT(lqe_is_master(lqe));
209         return (struct qmt_pool_info *)lqe->lqe_site->lqs_parent;
210 }
211
212 /* return true if someone holds either a read or write lock on the lqe */
213 static inline bool lqe_is_locked(struct lquota_entry *lqe)
214 {
215         LASSERT(lqe_is_master(lqe));
216         if (down_write_trylock(&lqe->lqe_sem) == 0)
217                 return true;
218         lqe_write_unlock(lqe);
219         return false;
220 }
221
222 /* value to be restored if someone wrong happens during lqe writeback */
223 struct qmt_lqe_restore {
224         __u64   qlr_hardlimit;
225         __u64   qlr_softlimit;
226         __u64   qlr_gracetime;
227         __u64   qlr_granted;
228         __u64   qlr_qunit;
229 };
230
231 #define QMT_MAX_POOL_NUM        16
232 /* Common data shared by qmt handlers */
233 struct qmt_thread_info {
234         union lquota_rec         qti_rec;
235         union lquota_id          qti_id;
236         char                     qti_buf[MTI_NAME_MAXLEN];
237         struct lu_fid            qti_fid;
238         struct ldlm_res_id       qti_resid;
239         union ldlm_gl_desc       qti_gl_desc;
240         struct quota_body        qti_body;
241         union {
242                 struct qmt_lqe_restore  qti_lqes_rstr_small[QMT_MAX_POOL_NUM];
243                 struct qmt_lqe_restore  *qti_lqes_rstr;
244         };
245         union {
246                 struct qmt_pool_info    *qti_pools_small[QMT_MAX_POOL_NUM];
247                 /* Pointer to an array of qpis in case when
248                  * qti_pools_cnt > QMT_MAX_POOL_NUM. */
249                 struct qmt_pool_info    **qti_pools;
250         };
251         /* The number of pools in qti_pools */
252         int                      qti_pools_cnt;
253         /* Maximum number of elements in qti_pools array.
254          * By default it is QMT_MAX_POOL_NUM. */
255         int                      qti_pools_num;
256         int                      qti_glbl_lqe_idx;
257         /* The same is for lqe ... */
258         union {
259                 struct lquota_entry     *qti_lqes_small[QMT_MAX_POOL_NUM];
260                 /* Pointer to an array of lqes in case when
261                  * qti_lqes_cnt > QMT_MAX_POOL_NUM. */
262                 struct lquota_entry     **qti_lqes;
263         };
264         /* The number of lqes in qti_lqes */
265         int                      qti_lqes_cnt;
266         /* Maximum number of elements in qti_lqes array.
267          * By default it is QMT_MAX_POOL_NUM. */
268         int                      qti_lqes_num;
269 };
270
271 extern struct lu_context_key qmt_thread_key;
272
273 /* helper function to extract qmt_thread_info from current environment */
274 static inline
275 struct qmt_thread_info *qmt_info(const struct lu_env *env)
276 {
277         return lu_env_info(env, &qmt_thread_key);
278 }
279
280 #define qti_lqes_num(env)       (qmt_info(env)->qti_lqes_num)
281 #define qti_lqes_inited(env)    (qmt_info(env)->qti_lqes_num)
282 #define qti_lqes_cnt(env)       (qmt_info(env)->qti_lqes_cnt)
283 #define qti_glbl_lqe_idx(env)   (qmt_info(env)->qti_glbl_lqe_idx)
284 #define qti_lqes(env)           (qti_lqes_num(env) > QMT_MAX_POOL_NUM ? \
285                                         qmt_info(env)->qti_lqes : \
286                                         qmt_info(env)->qti_lqes_small)
287 #define qti_lqes_rstr(env)      (qti_lqes_num(env) > QMT_MAX_POOL_NUM ? \
288                                         qmt_info(env)->qti_lqes_rstr : \
289                                         qmt_info(env)->qti_lqes_rstr_small)
290 #define qti_lqes_glbl(env)      (qti_lqes(env)[qti_glbl_lqe_idx(env)])
291 #define qti_lqe_hard(env, i)    (qti_lqes(env)[i]->lqe_hardlimit)
292 #define qti_lqe_soft(env, i)    (qti_lqes(env)[i]->lqe_softlimit)
293 #define qti_lqe_granted(env, i) (qti_lqes(env)[i]->lqe_granted)
294 #define qti_lqe_qunit(env, i)   (qti_lqes(env)[i]->lqe_qunit)
295
296 /* helper routine to convert a lu_device into a qmt_device */
297 static inline struct qmt_device *lu2qmt_dev(struct lu_device *ld)
298 {
299         return container_of_safe(lu2dt_dev(ld), struct qmt_device, qmt_dt_dev);
300 }
301
302 /* helper routine to convert a qmt_device into lu_device */
303 static inline struct lu_device *qmt2lu_dev(struct qmt_device *qmt)
304 {
305         return &qmt->qmt_dt_dev.dd_lu_dev;
306 }
307
308 #define LQE_ROOT(lqe)    (lqe2qpi(lqe)->qpi_root)
309 #define LQE_GLB_OBJ(lqe) (lqe2qpi(lqe)->qpi_glb_obj[lqe_qtype(lqe)])
310
311 /* helper function returning grace time to use for a given lquota entry */
312 static inline __u64 qmt_lqe_grace(struct lquota_entry *lqe)
313 {
314         struct qmt_pool_info    *pool = lqe2qpi(lqe);
315         struct lquota_entry     *grace_lqe;
316
317         grace_lqe = pool->qpi_grace_lqe[lqe_qtype(lqe)];
318         LASSERT(grace_lqe != NULL);
319
320         return grace_lqe->lqe_gracetime;
321 }
322
323 static inline void qmt_restore(struct lquota_entry *lqe,
324                                struct qmt_lqe_restore *restore)
325 {
326         lqe->lqe_hardlimit = restore->qlr_hardlimit;
327         lqe->lqe_softlimit = restore->qlr_softlimit;
328         lqe->lqe_gracetime = restore->qlr_gracetime;
329         lqe->lqe_granted   = restore->qlr_granted;
330         lqe->lqe_qunit     = restore->qlr_qunit;
331 }
332
333 static inline void qmt_restore_lqes(const struct lu_env *env)
334 {
335         int i;
336
337         for (i = 0; i < qti_lqes_cnt(env); i++)
338                 qmt_restore(qti_lqes(env)[i], &qti_lqes_rstr(env)[i]);
339 }
340
341 #define QMT_GRANT(lqe, slv, cnt)             \
342         do {                                 \
343                 (lqe)->lqe_granted += (cnt); \
344                 (slv) += (cnt);              \
345         } while (0)
346 #define QMT_REL(lqe, slv, cnt)               \
347         do {                                 \
348                 (lqe)->lqe_granted -= (cnt); \
349                 (slv) -= (cnt);              \
350         } while (0)
351
352 /* helper routine returning true when reached hardlimit */
353 static inline bool qmt_hard_exhausted(struct lquota_entry *lqe)
354 {
355         if (lqe->lqe_hardlimit != 0 && lqe->lqe_granted >= lqe->lqe_hardlimit)
356                 return true;
357         return false;
358 }
359
360 /* helper routine returning true when reached softlimit */
361 static inline bool qmt_soft_exhausted(struct lquota_entry *lqe, __u64 now)
362 {
363         if (lqe->lqe_softlimit != 0 && lqe->lqe_granted > lqe->lqe_softlimit &&
364             lqe->lqe_gracetime != 0 && now >= lqe->lqe_gracetime)
365                 return true;
366         return false;
367 }
368
369 /* helper routine returning true when the id has run out of quota space:
370  * - reached hardlimit
371  * OR
372  * - reached softlimit and grace time expired already */
373 static inline bool qmt_space_exhausted(struct lquota_entry *lqe, __u64 now)
374 {
375         return (qmt_hard_exhausted(lqe) || qmt_soft_exhausted(lqe, now));
376 }
377
378 static inline bool qmt_space_exhausted_lqes(const struct lu_env *env, __u64 now)
379 {
380         bool exhausted = false;
381         int i;
382
383         for (i = 0; i < qti_lqes_cnt(env) && !exhausted; i++)
384                 exhausted |= qmt_space_exhausted(qti_lqes(env)[i], now);
385
386         return exhausted;
387 }
388
389 /* helper routine clearing the default quota setting  */
390 static inline void qmt_lqe_clear_default(struct lquota_entry *lqe)
391 {
392         lqe->lqe_is_default = false;
393         lqe->lqe_gracetime &= ~((__u64)LQUOTA_FLAG_DEFAULT <<
394                                                         LQUOTA_GRACE_BITS);
395 }
396
397 /* number of seconds to wait for slaves to release quota space after
398  * rebalancing */
399 #define QMT_REBA_TIMEOUT 2
400
401 /* qmt_pool.c */
402
403 void qmt_pool_free(const struct lu_env *, struct qmt_pool_info *);
404 /*
405  * Reference counter management for qmt_pool_info structures
406  */
407 static inline void qpi_getref(struct qmt_pool_info *pool)
408 {
409         atomic_inc(&pool->qpi_ref);
410 }
411
412 static inline void qpi_putref(const struct lu_env *env,
413                               struct qmt_pool_info *pool)
414 {
415         LASSERT(atomic_read(&pool->qpi_ref) > 0);
416         if (atomic_dec_and_test(&pool->qpi_ref))
417                 qmt_pool_free(env, pool);
418 }
419
420
421 void qmt_pool_fini(const struct lu_env *, struct qmt_device *);
422 int qmt_pool_init(const struct lu_env *, struct qmt_device *);
423 int qmt_pool_prepare(const struct lu_env *, struct qmt_device *,
424                    struct dt_object *, char *);
425 int qmt_pool_new_conn(const struct lu_env *, struct qmt_device *,
426                       struct lu_fid *, struct lu_fid *, __u64 *,
427                       struct obd_uuid *);
428
429 #define GLB_POOL_NAME   "0x0"
430 #define qmt_pool_lookup_glb(env, qmt, type) \
431                 qmt_pool_lookup(env, qmt, type, NULL, -1, false)
432 #define qmt_pool_lookup_name(env, qmt, type, name) \
433                 qmt_pool_lookup(env, qmt, type, name, -1, false)
434 #define qmt_pool_lookup_arr(env, qmt, type, idx) \
435                 qmt_pool_lookup(env, qmt, type, NULL, idx, true)
436 struct qmt_pool_info *qmt_pool_lookup(const struct lu_env *env,
437                                              struct qmt_device *qmt,
438                                              int rtype,
439                                              char *pool_name,
440                                              int idx,
441                                              bool add);
442 struct lquota_entry *qmt_pool_lqe_lookup(const struct lu_env *,
443                                          struct qmt_device *, int, int,
444                                          union lquota_id *, char *);
445 int qmt_pool_lqes_lookup(const struct lu_env *, struct qmt_device *, int,
446                          int, int, union lquota_id *, char *, int);
447 int qmt_pool_lqes_lookup_spec(const struct lu_env *env, struct qmt_device *qmt,
448                               int rtype, int qtype, union lquota_id *qid);
449 void qmt_lqes_sort(const struct lu_env *env);
450 int qmt_pool_new(struct obd_device *obd, char *poolname);
451 int qmt_pool_add(struct obd_device *obd, char *poolname, char *ostname);
452 int qmt_pool_rem(struct obd_device *obd, char *poolname, char *ostname);
453 int qmt_pool_del(struct obd_device *obd, char *poolname);
454
455 struct rw_semaphore *qmt_sarr_rwsem(struct qmt_pool_info *qpi);
456 int qmt_sarr_get_idx(struct qmt_pool_info *qpi, int arr_idx);
457 unsigned int qmt_sarr_count(struct qmt_pool_info *qpi);
458
459 /* qmt_entry.c */
460 extern const struct lquota_entry_operations qmt_lqe_ops;
461 int qmt_lqe_set_default(const struct lu_env *env, struct qmt_pool_info *pool,
462                         struct lquota_entry *lqe, bool create_record);
463 struct thandle *qmt_trans_start_with_slv(const struct lu_env *,
464                                          struct lquota_entry *,
465                                          struct dt_object *,
466                                          bool);
467 struct thandle *qmt_trans_start(const struct lu_env *, struct lquota_entry *);
468 int qmt_glb_write_lqes(const struct lu_env *, struct thandle *, __u32, __u64 *);
469 int qmt_glb_write(const struct lu_env *, struct thandle *,
470                   struct lquota_entry *, __u32, __u64 *);
471 int qmt_slv_write(const struct lu_env *, struct thandle *,
472                   struct lquota_entry *, struct dt_object *, __u32, __u64 *,
473                   __u64);
474 int qmt_slv_read(const struct lu_env *,  union lquota_id *,
475                  struct dt_object *, __u64 *);
476 int qmt_validate_limits(struct lquota_entry *, __u64, __u64);
477 bool qmt_adjust_qunit(const struct lu_env *, struct lquota_entry *);
478 bool qmt_adjust_edquot(struct lquota_entry *, __u64);
479
480 #define qmt_adjust_edquot_notify(env, qmt, now, qb_flags) \
481           qmt_adjust_edquot_qunit_notify(env, qmt, now, true, \
482                                          false, qb_flags, -1)
483 #define qmt_adjust_notify_nu(env, qmt, now, qb_flags, idx) \
484           qmt_adjust_edquot_qunit_notify(env, qmt, now, true, \
485                                          true, qb_flags, idx)
486 bool qmt_adjust_edquot_qunit_notify(const struct lu_env *, struct qmt_device *,
487                                     __u64, bool, bool, __u32, int idx);
488 bool qmt_revalidate(const struct lu_env *, struct lquota_entry *);
489 void qmt_revalidate_lqes(const struct lu_env *, struct qmt_device *, __u32);
490 __u64 qmt_alloc_expand(struct lquota_entry *, __u64, __u64);
491
492 void qti_lqes_init(const struct lu_env *env);
493 int qti_lqes_add(const struct lu_env *env, struct lquota_entry *lqe);
494 void qti_lqes_del(const struct lu_env *env, int index);
495 void qti_lqes_fini(const struct lu_env *env);
496 __u64 qti_lqes_min_qunit(const struct lu_env *env);
497 int qti_lqes_edquot(const struct lu_env *env);
498 int qti_lqes_restore_init(const struct lu_env *env);
499 void qti_lqes_restore_fini(const struct lu_env *env);
500 void qti_lqes_write_lock(const struct lu_env *env);
501 void qti_lqes_write_unlock(const struct lu_env *env);
502
503 struct lqe_glbl_data *qmt_alloc_lqe_gd(struct qmt_pool_info *, int);
504 void qmt_free_lqe_gd(struct lqe_glbl_data *);
505 void qmt_setup_lqe_gd(const struct lu_env *,  struct qmt_device *,
506                     struct lquota_entry *, struct lqe_glbl_data *, int);
507 #define qmt_seed_glbe_edquot(env, lqeg) \
508                 qmt_seed_glbe_all(env, lqeg, false, true)
509 #define qmt_seed_glbe_qunit(env, lqeg) \
510                 qmt_seed_glbe_all(env, lqeg, true, false)
511 #define qmt_seed_glbe(env, lqeg) \
512                 qmt_seed_glbe_all(env, lqeg, true, true)
513 void qmt_seed_glbe_all(const struct lu_env *, struct lqe_glbl_data *,
514                        bool , bool);
515
516 /* qmt_handler.c */
517 int qmt_set_with_lqe(const struct lu_env *env, struct qmt_device *qmt,
518                      struct lquota_entry *lqe, __u64 hard, __u64 soft,
519                      __u64 time, __u32 valid, bool is_default, bool is_updated);
520 int qmt_dqacq0(const struct lu_env *, struct qmt_device *, struct obd_uuid *,
521                __u32, __u64, __u64, struct quota_body *, int);
522 int qmt_uuid2idx(struct obd_uuid *, int *);
523
524 /* qmt_lock.c */
525 int qmt_intent_policy(const struct lu_env *, struct lu_device *,
526                       struct ptlrpc_request *, struct ldlm_lock **, int);
527 int qmt_lvbo_init(struct lu_device *, struct ldlm_resource *);
528 int qmt_lvbo_update(struct lu_device *, struct ldlm_resource *,
529                     struct ptlrpc_request *, int);
530 int qmt_lvbo_size(struct lu_device *, struct ldlm_lock *);
531 int qmt_lvbo_fill(struct lu_device *, struct ldlm_lock *, void *, int);
532 int qmt_lvbo_free(struct lu_device *, struct ldlm_resource *);
533 int qmt_start_reba_thread(struct qmt_device *);
534 void qmt_stop_reba_thread(struct qmt_device *);
535 void qmt_glb_lock_notify(const struct lu_env *, struct lquota_entry *, __u64);
536 void qmt_id_lock_notify(struct qmt_device *, struct lquota_entry *);
537 #endif /* _QMT_INTERNAL_H */