Whamcloud - gitweb
LU-8851 nodemap: add uid/gid only flags to control mapping
[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, 2016, 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
50         /* Reference to the next device in the side stack
51          * The child device is actually the OSD device where we store the quota
52          * index files */
53         struct obd_export       *qmt_child_exp;
54         struct dt_device        *qmt_child;
55
56         /* pointer to ldlm namespace to be used for quota locks */
57         struct ldlm_namespace   *qmt_ns;
58
59         /* Hash table containing a qmt_pool_info structure for each pool
60          * this quota master is in charge of. We only have 2 pools in this
61          * hash for the time being:
62          * - one for quota management on the default metadata pool
63          * - one for quota managment on the default data pool
64          *
65          * Once we support quota on non-default pools, then more pools will
66          * be added to this hash table and pool master setup would have to be
67          * handled via configuration logs */
68         struct cfs_hash         *qmt_pool_hash;
69
70         /* List of pools managed by this master target */
71         struct list_head         qmt_pool_list;
72
73         /* procfs root directory for this qmt */
74         struct proc_dir_entry   *qmt_proc;
75
76         /* dedicated thread in charge of space rebalancing */
77         struct ptlrpc_thread     qmt_reba_thread;
78
79         /* list of lqe entry which need space rebalancing */
80         struct list_head         qmt_reba_list;
81
82         /* lock protecting rebalancing list */
83         spinlock_t               qmt_reba_lock;
84
85         unsigned long            qmt_stopping:1; /* qmt is stopping */
86
87 };
88
89 /*
90  * Per-pool quota information.
91  * The qmt creates one such structure for each pool
92  * with quota enforced. All the structures are kept in a hash which is used to
93  * determine whether or not quota is enforced for a given pool.
94  * We currently only support the default data pool and default metadata pool
95  * with the pool_id 0.
96  */
97 struct qmt_pool_info {
98         /* link to qmt's pool hash */
99         struct hlist_node        qpi_hash;
100
101         /* chained list of all pools managed by the same qmt */
102         struct list_head         qpi_linkage;
103
104         /* Pool key composed of pool_id | (pool_type << 16)
105          * Only pool ID 0 is supported for now and the pool type is either
106          * QUOTA_RES_MD or QUOTA_RES_DT.
107          * immutable after creation. */
108         __u32                    qpi_key;
109
110         /* track users of this pool instance */
111         atomic_t                 qpi_ref;
112
113         /* back pointer to master target
114          * immutable after creation. */
115         struct qmt_device       *qpi_qmt;
116
117         /* pointer to dt object associated with global indexes for both user
118          * and group quota */
119         struct dt_object        *qpi_glb_obj[LL_MAXQUOTAS];
120
121         /* A pool supports two different quota types: user and group quota.
122          * Each quota type has its own global index and lquota_entry hash table.
123          */
124         struct lquota_site      *qpi_site[LL_MAXQUOTAS];
125
126         /* number of slaves registered for each quota types */
127         int                      qpi_slv_nr[LL_MAXQUOTAS];
128
129         /* reference on lqe (ID 0) storing grace time. */
130         struct lquota_entry     *qpi_grace_lqe[LL_MAXQUOTAS];
131
132         /* procfs root directory for this pool */
133         struct proc_dir_entry   *qpi_proc;
134
135         /* pool directory where all indexes related to this pool instance are
136          * stored */
137         struct dt_object        *qpi_root;
138
139         /* Global quota parameters which apply to all quota type */
140         /* the least value of qunit */
141         unsigned long            qpi_least_qunit;
142
143         /* Least value of qunit when soft limit is exceeded.
144          *
145          * When soft limit is exceeded, qunit will be shrinked to least_qunit
146          * (1M for block limit), that results in significant write performance
147          * drop since the client will turn to sync write from now on.
148          *
149          * To retain the write performance in an acceptable level, we choose
150          * to sacrifice grace time accuracy a bit and use a larger least_qunit
151          * when soft limit is exceeded. It's (qpi_least_qunit * 4) by default,
152          * and user may enlarge it via procfs to get even better performance
153          * (with the cost of losing more grace time accuracy).
154          *
155          * See qmt_calc_softlimit().
156          */
157         unsigned long            qpi_soft_least_qunit;
158 };
159
160 /*
161  * Helper routines and prototypes
162  */
163
164 /* helper routine to find qmt_pool_info associated a lquota_entry */
165 static inline struct qmt_pool_info *lqe2qpi(struct lquota_entry *lqe)
166 {
167         LASSERT(lqe_is_master(lqe));
168         return (struct qmt_pool_info *)lqe->lqe_site->lqs_parent;
169 }
170
171 /* return true if someone holds either a read or write lock on the lqe */
172 static inline bool lqe_is_locked(struct lquota_entry *lqe)
173 {
174         LASSERT(lqe_is_master(lqe));
175         if (down_write_trylock(&lqe->lqe_sem) == 0)
176                 return true;
177         lqe_write_unlock(lqe);
178         return false;
179 }
180
181 /* value to be restored if someone wrong happens during lqe writeback */
182 struct qmt_lqe_restore {
183         __u64   qlr_hardlimit;
184         __u64   qlr_softlimit;
185         __u64   qlr_gracetime;
186         __u64   qlr_granted;
187         __u64   qlr_qunit;
188 };
189
190 /* Common data shared by qmt handlers */
191 struct qmt_thread_info {
192         union lquota_rec        qti_rec;
193         union lquota_id         qti_id;
194         char                    qti_buf[MTI_NAME_MAXLEN];
195         struct lu_fid           qti_fid;
196         struct ldlm_res_id      qti_resid;
197         union ldlm_gl_desc      qti_gl_desc;
198         struct quota_body       qti_body;
199         struct qmt_lqe_restore  qti_restore;
200 };
201
202 extern struct lu_context_key qmt_thread_key;
203
204 /* helper function to extract qmt_thread_info from current environment */
205 static inline
206 struct qmt_thread_info *qmt_info(const struct lu_env *env)
207 {
208         struct qmt_thread_info  *info;
209
210         info = lu_context_key_get(&env->le_ctx, &qmt_thread_key);
211         if (info == NULL) {
212                 lu_env_refill((struct lu_env *)env);
213                 info = lu_context_key_get(&env->le_ctx, &qmt_thread_key);
214         }
215         LASSERT(info);
216         return info;
217 }
218
219 /* helper routine to convert a lu_device into a qmt_device */
220 static inline struct qmt_device *lu2qmt_dev(struct lu_device *ld)
221 {
222         return container_of0(lu2dt_dev(ld), struct qmt_device, qmt_dt_dev);
223 }
224
225 /* helper routine to convert a qmt_device into lu_device */
226 static inline struct lu_device *qmt2lu_dev(struct qmt_device *qmt)
227 {
228         return &qmt->qmt_dt_dev.dd_lu_dev;
229 }
230
231 #define LQE_ROOT(lqe)    (lqe2qpi(lqe)->qpi_root)
232 #define LQE_GLB_OBJ(lqe) (lqe2qpi(lqe)->qpi_glb_obj[lqe->lqe_site->lqs_qtype])
233
234 /* helper function returning grace time to use for a given lquota entry */
235 static inline __u64 qmt_lqe_grace(struct lquota_entry *lqe)
236 {
237         struct qmt_pool_info    *pool = lqe2qpi(lqe);
238         struct lquota_entry     *grace_lqe;
239
240         grace_lqe = pool->qpi_grace_lqe[lqe->lqe_site->lqs_qtype];
241         LASSERT(grace_lqe != NULL);
242
243         return grace_lqe->lqe_gracetime;
244 }
245
246 static inline void qmt_restore(struct lquota_entry *lqe,
247                                struct qmt_lqe_restore *restore)
248 {
249         lqe->lqe_hardlimit = restore->qlr_hardlimit;
250         lqe->lqe_softlimit = restore->qlr_softlimit;
251         lqe->lqe_gracetime = restore->qlr_gracetime;
252         lqe->lqe_granted   = restore->qlr_granted;
253         lqe->lqe_qunit     = restore->qlr_qunit;
254 }
255
256 #define QMT_GRANT(lqe, slv, cnt)             \
257         do {                                 \
258                 (lqe)->lqe_granted += (cnt); \
259                 (slv) += (cnt);              \
260         } while (0)
261 #define QMT_REL(lqe, slv, cnt)               \
262         do {                                 \
263                 (lqe)->lqe_granted -= (cnt); \
264                 (slv) -= (cnt);              \
265         } while (0)
266
267 /* helper routine returning true when reached hardlimit */
268 static inline bool qmt_hard_exhausted(struct lquota_entry *lqe)
269 {
270         if (lqe->lqe_hardlimit != 0 && lqe->lqe_granted >= lqe->lqe_hardlimit)
271                 return true;
272         return false;
273 }
274
275 /* helper routine returning true when reached softlimit */
276 static inline bool qmt_soft_exhausted(struct lquota_entry *lqe, __u64 now)
277 {
278         if (lqe->lqe_softlimit != 0 && lqe->lqe_granted > lqe->lqe_softlimit &&
279             lqe->lqe_gracetime != 0 && now >= lqe->lqe_gracetime)
280                 return true;
281         return false;
282 }
283
284 /* helper routine returning true when the id has run out of quota space:
285  * - reached hardlimit
286  * OR
287  * - reached softlimit and grace time expired already */
288 static inline bool qmt_space_exhausted(struct lquota_entry *lqe, __u64 now)
289 {
290         return (qmt_hard_exhausted(lqe) || qmt_soft_exhausted(lqe, now));
291 }
292
293 /* number of seconds to wait for slaves to release quota space after
294  * rebalancing */
295 #define QMT_REBA_TIMEOUT 2
296
297 /* qmt_pool.c */
298 void qmt_pool_fini(const struct lu_env *, struct qmt_device *);
299 int qmt_pool_init(const struct lu_env *, struct qmt_device *);
300 int qmt_pool_prepare(const struct lu_env *, struct qmt_device *,
301                    struct dt_object *);
302 int qmt_pool_new_conn(const struct lu_env *, struct qmt_device *,
303                       struct lu_fid *, struct lu_fid *, __u64 *,
304                       struct obd_uuid *);
305 struct lquota_entry *qmt_pool_lqe_lookup(const struct lu_env *,
306                                          struct qmt_device *, int, int, int,
307                                          union lquota_id *);
308 /* qmt_entry.c */
309 extern struct lquota_entry_operations qmt_lqe_ops;
310 struct thandle *qmt_trans_start_with_slv(const struct lu_env *,
311                                          struct lquota_entry *,
312                                          struct dt_object *,
313                                          struct qmt_lqe_restore *);
314 struct thandle *qmt_trans_start(const struct lu_env *, struct lquota_entry *,
315                                 struct qmt_lqe_restore *);
316 int qmt_glb_write(const struct lu_env *, struct thandle *,
317                   struct lquota_entry *, __u32, __u64 *);
318 int qmt_slv_write(const struct lu_env *, struct thandle *,
319                   struct lquota_entry *, struct dt_object *, __u32, __u64 *,
320                   __u64);
321 int qmt_slv_read(const struct lu_env *, struct lquota_entry *,
322                  struct dt_object *, __u64 *);
323 int qmt_validate_limits(struct lquota_entry *, __u64, __u64);
324 void qmt_adjust_qunit(const struct lu_env *, struct lquota_entry *);
325 void qmt_adjust_edquot(struct lquota_entry *, __u64);
326 void qmt_revalidate(const struct lu_env *, struct lquota_entry *);
327 __u64 qmt_alloc_expand(struct lquota_entry *, __u64, __u64);
328
329 /* qmt_handler.c */
330 int qmt_dqacq0(const struct lu_env *, struct lquota_entry *,
331                struct qmt_device *, struct obd_uuid *, __u32, __u64, __u64,
332                struct quota_body *);
333
334 /* qmt_lock.c */
335 int qmt_intent_policy(const struct lu_env *, struct lu_device *,
336                       struct ptlrpc_request *, struct ldlm_lock **, int);
337 int qmt_lvbo_init(struct lu_device *, struct ldlm_resource *);
338 int qmt_lvbo_update(struct lu_device *, struct ldlm_resource *,
339                     struct ptlrpc_request *, int);
340 int qmt_lvbo_size(struct lu_device *, struct ldlm_lock *);
341 int qmt_lvbo_fill(struct lu_device *, struct ldlm_lock *, void *, int);
342 int qmt_lvbo_free(struct lu_device *, struct ldlm_resource *);
343 int qmt_start_reba_thread(struct qmt_device *);
344 void qmt_stop_reba_thread(struct qmt_device *);
345 void qmt_glb_lock_notify(const struct lu_env *, struct lquota_entry *, __u64);
346 void qmt_id_lock_notify(struct qmt_device *, struct lquota_entry *);
347 #endif /* _QMT_INTERNAL_H */