Whamcloud - gitweb
LU-17871 ldlm: FLOCK ownlocks may be not set
[fs/lustre-release.git] / lustre / quota / qsd_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, 2014, Intel Corporation.
25  * Use is subject to license terms.
26  */
27
28 #include "lquota_internal.h"
29
30 #ifndef _QSD_INTERNAL_H
31 #define _QSD_INTERNAL_H
32
33 struct qsd_type_info;
34 struct qsd_fsinfo;
35
36 extern struct kmem_cache *upd_kmem;
37
38 /*
39  * A QSD instance implements quota enforcement support for a given OSD.
40  * The instance can be created via qsd_init() and then freed with qsd_fini().
41  * This structure gathers all quota parameters and pointers to on-disk indexes
42  * required on quota slave to:
43  * i. acquire/release quota space from the QMT;
44  * ii. allocate this quota space to local requests.
45  */
46 struct qsd_instance {
47         /* name of service which created this qsd instance */
48         char                     qsd_svname[MAX_OBD_NAME];
49
50         /* dt_device associated with this qsd instance */
51         struct dt_device        *qsd_dev;
52
53         /* procfs directory where information related to the underlying slaves
54          * are exported */
55         struct proc_dir_entry   *qsd_proc;
56
57         /* export used for the connection to quota master */
58         struct obd_export       *qsd_exp;
59
60         /* ldlm namespace used for quota locks */
61         struct ldlm_namespace   *qsd_ns;
62
63         /* on-disk directory where to store index files for this qsd instance */
64         struct dt_object        *qsd_root;
65
66         /* We create 2 quota slave instances:
67          * - one for user quota
68          * - one for group quota
69          *
70          * This will have to be revisited if new quota types are added in the
71          * future. For the time being, we can just use an array. */
72         struct qsd_qtype_info   *qsd_type_array[LL_MAXQUOTAS];
73
74         /* per-filesystem quota information */
75         struct qsd_fsinfo       *qsd_fsinfo;
76
77         /* link into qfs_qsd_list of qfs_fsinfo */
78         struct list_head         qsd_link;
79
80         /* list of lqe entry which might need quota space adjustment */
81         struct list_head         qsd_adjust_list;
82
83         /* lock protecting adjust list */
84         spinlock_t               qsd_adjust_lock;
85
86         /* dedicated thread for updating slave index files. */
87         struct task_struct      *qsd_upd_task;
88
89         /* list of update tasks */
90         struct list_head         qsd_upd_list;
91
92         /* r/w spinlock protecting:
93          * - the state flags
94          * - the qsd update list
95          * - the deferred list
96          * - flags of the qsd_qtype_info */
97         rwlock_t                 qsd_lock;
98
99         /* Default quota settings which apply to all identifiers */
100         /* when blk qunit reaches this value, later write reqs from client
101          * should be sync. b=16642 */
102         unsigned long            qsd_sync_threshold;
103
104         /* how long a service thread can wait for quota space.
105          * value dynamically computed from obd_timeout and at_max if not
106          * enforced here (via procfs) */
107         int                      qsd_timeout;
108
109         unsigned long           qsd_is_md:1,    /* managing quota for mdt */
110                                 qsd_started:1,  /* instance is now started */
111                                 qsd_prepared:1, /* qsd_prepare() successfully
112                                                   * called */
113                                 qsd_exp_valid:1,/* qsd_exp is now valid */
114                                 qsd_stopping:1, /* qsd_instance is stopping */
115                                 qsd_updating:1, /* qsd is updating record */
116                                 qsd_exclusive:1, /* upd exclusive with reint */
117                                 qsd_root_prj_enable:1;
118
119 };
120
121 /*
122  * Per-type quota information.
123  * Quota slave instance for a specific quota type. The qsd instance has one such
124  * structure for each quota type (i.e. user & group).
125  */
126 struct qsd_qtype_info {
127         /* reference count incremented by each user of this structure */
128         atomic_t                 qqi_ref;
129
130         /* quota type, either USRQUOTA or GRPQUOTA
131          * immutable after creation. */
132         int                      qqi_qtype;
133
134         /* Global index FID to use for this quota type */
135         struct lu_fid            qqi_fid;
136
137         /* Slave index FID allocated by the master */
138         struct lu_fid            qqi_slv_fid;
139
140         /* back pointer to qsd device
141          * immutable after creation. */
142         struct qsd_instance     *qqi_qsd;
143
144         /* handle of global quota lock */
145         struct lustre_handle     qqi_lockh;
146
147         /* Local index files storing quota settings for this quota type */
148         struct dt_object        *qqi_acct_obj; /* accounting object */
149         struct dt_object        *qqi_slv_obj;  /* slave index copy */
150         struct dt_object        *qqi_glb_obj;  /* global index copy */
151
152         /* Current object versions */
153         __u64                    qqi_slv_ver; /* slave index version */
154         __u64                    qqi_glb_ver; /* global index version */
155
156         /* per quota ID information. All lquota entry are kept in a hash table
157          * and read from disk on cache miss. */
158         struct lquota_site      *qqi_site;
159
160         /* Reintegration thread */
161         struct task_struct      *qqi_reint_task;
162
163         /* statistics on operations performed by this slave */
164         struct lprocfs_stats    *qqi_stats;
165
166         /* deferred update for the global index copy */
167         struct list_head         qqi_deferred_glb;
168         /* deferred update for the slave index copy */
169         struct list_head         qqi_deferred_slv;
170
171         /* Various flags representing the current state of the slave for this
172          * quota type. */
173         unsigned long           qqi_glb_uptodate:1, /* global index uptodate
174                                                         with master */
175                                 qqi_slv_uptodate:1, /* slave index uptodate
176                                                         with master */
177                                 qqi_reint:1,    /* in reintegration or not */
178                                 qqi_acct_failed:1; /* failed to setup acct */
179
180         /* A list of references to this instance, for debugging */
181         struct lu_ref           qqi_reference;
182
183         /* default quota setting*/
184         __u64                   qqi_default_hardlimit;
185         __u64                   qqi_default_softlimit;
186         __u64                   qqi_default_gracetime;
187 };
188
189 /*
190  * Per-filesystem quota information
191  * Structure tracking quota enforcement status on a per-filesystem basis
192  */
193 struct qsd_fsinfo {
194         /* filesystem name */
195         char                    qfs_name[MTI_NAME_MAXLEN];
196
197         /* what type of quota is enabled for each resource type. */
198         unsigned int            qfs_enabled[LQUOTA_NR_RES];
199
200         /* list of all qsd_instance for this fs */
201         struct list_head        qfs_qsd_list;
202         struct mutex            qfs_mutex;
203
204         /* link to the global quota fsinfo list.  */
205         struct list_head        qfs_link;
206
207         /* reference count */
208         int                     qfs_ref;
209 };
210
211 /*
212  * Helper functions & prototypes
213  */
214
215 /* helper routine to find qsd_instance associated a lquota_entry */
216 static inline struct qsd_qtype_info *lqe2qqi(struct lquota_entry *lqe)
217 {
218         LASSERT(!lqe_is_master(lqe));
219         return (struct qsd_qtype_info *)lqe->lqe_site->lqs_parent;
220 }
221
222 /* qqi_getref/putref is used to track users of a qqi structure  */
223 static inline void qqi_getref(struct qsd_qtype_info *qqi)
224 {
225         atomic_inc(&qqi->qqi_ref);
226 }
227
228 static inline void qqi_putref(struct qsd_qtype_info *qqi)
229 {
230         LASSERT(atomic_read(&qqi->qqi_ref) > 0);
231         atomic_dec(&qqi->qqi_ref);
232 }
233
234 #define QSD_RES_TYPE(qsd) ((qsd)->qsd_is_md ? LQUOTA_RES_MD : LQUOTA_RES_DT)
235
236 /* udpate record for slave & global index copy */
237 struct qsd_upd_rec {
238         struct list_head        qur_link; /* link into qsd_upd_list */
239         union lquota_id         qur_qid;
240         union lquota_rec        qur_rec;
241         struct qsd_qtype_info  *qur_qqi;
242         struct lquota_entry    *qur_lqe;
243         __u64                   qur_ver;
244         bool                    qur_global;
245 };
246
247 /* Common data shared by qsd-level handlers. This is allocated per-thread to
248  * reduce stack consumption.  */
249 struct qsd_thread_info {
250         union lquota_rec                qti_rec;
251         union lquota_id                 qti_id;
252         struct lu_fid                   qti_fid;
253         struct ldlm_res_id              qti_resid;
254         struct ldlm_enqueue_info        qti_einfo;
255         struct lustre_handle            qti_lockh;
256         __u64                           qti_slv_ver;
257         struct lquota_lvb               qti_lvb;
258         union {
259                 struct quota_body       qti_body;
260                 struct idx_info         qti_ii;
261         };
262         char                            qti_buf[MTI_NAME_MAXLEN];
263 };
264
265 extern struct lu_context_key qsd_thread_key;
266
267 static inline
268 struct qsd_thread_info *qsd_info(const struct lu_env *env)
269 {
270         return lu_env_info(env, &qsd_thread_key);
271 }
272
273 /* helper function to check whether a given quota type is enabled */
274 static inline int qsd_type_enabled(struct qsd_instance *qsd, int type)
275 {
276         int     enabled, pool;
277
278         LASSERT(qsd != NULL);
279         LASSERT(type < LL_MAXQUOTAS);
280
281         if (qsd->qsd_fsinfo == NULL)
282                 return 0;
283
284         pool = qsd->qsd_is_md ? LQUOTA_RES_MD : LQUOTA_RES_DT;
285         enabled = qsd->qsd_fsinfo->qfs_enabled[pool - LQUOTA_FIRST_RES];
286
287         return enabled & BIT(type);
288 }
289
290 /* helper function to set new qunit and compute associated qtune value */
291 static inline void qsd_set_qunit(struct lquota_entry *lqe, __u64 qunit)
292 {
293         if (lqe->lqe_qunit == qunit)
294                 return;
295
296         lqe->lqe_qunit = qunit;
297
298         /* With very large qunit support, we can't afford to have a static
299          * qtune value, e.g. with a 1PB qunit and qtune set to 50%, we would
300          * start pre-allocation when 512TB of free quota space remains.
301          * Therefore, we adapt qtune depending on the actual qunit value */
302         if (qunit == 0)                         /* if qunit is NULL           */
303                 lqe->lqe_qtune = 0;             /*  qtune = 0                 */
304         else if (qunit == 1024)                 /* if 1MB or 1K inodes        */
305                 lqe->lqe_qtune = qunit >> 1;    /*  => 50%                    */
306         else if (qunit <= 1024 * 1024)          /* up to 1GB or 1M inodes     */
307                 lqe->lqe_qtune = qunit >> 2;    /*  => 25%                    */
308         else if (qunit <= 4 * 1024 * 1024)      /* up to 16GB or 16M inodes   */
309                 lqe->lqe_qtune = qunit >> 3;    /*  => 12.5%                  */
310         else                                    /* above 4GB/4M               */
311                 lqe->lqe_qtune = 1024 * 1024;   /*  value capped to 1GB/1M    */
312
313         LQUOTA_DEBUG(lqe, "changing qunit & qtune");
314
315         /* turn on pre-acquire when qunit is modified */
316         lqe->lqe_nopreacq = false;
317 }
318
319 /* helper function to set/clear edquot flag */
320 static inline void qsd_set_edquot(struct lquota_entry *lqe, bool edquot)
321 {
322         lqe->lqe_edquot = edquot;
323         if (edquot)
324                 lqe->lqe_edquot_time = ktime_get_seconds();
325 }
326
327 #define QSD_WB_INTERVAL 60 /* 60 seconds */
328
329 /* helper function calculating how long a service thread should be waiting for
330  * quota space */
331 static inline int qsd_wait_timeout(struct qsd_instance *qsd)
332 {
333         struct obd_device *obd = qsd->qsd_dev->dd_lu_dev.ld_obd;
334
335         if (qsd->qsd_timeout != 0)
336                 return qsd->qsd_timeout;
337         return min_t(int, obd_get_at_max(obd) / 2, obd_timeout / 2);
338 }
339
340 /* qsd_entry.c */
341 extern const struct lquota_entry_operations qsd_lqe_ops;
342 int qsd_refresh_usage(const struct lu_env *, struct lquota_entry *);
343 int qsd_update_index(const struct lu_env *, struct qsd_qtype_info *,
344                      union lquota_id *, bool, __u64, void *);
345 int qsd_update_lqe(const struct lu_env *, struct lquota_entry *, bool,
346                    void *);
347 int qsd_write_version(const struct lu_env *, struct qsd_qtype_info *,
348                       __u64, bool);
349
350 /* qsd_lock.c */
351 extern struct ldlm_enqueue_info qsd_glb_einfo;
352 extern struct ldlm_enqueue_info qsd_id_einfo;
353 void qsd_update_default_quota(struct qsd_qtype_info *qqi, __u64 hardlimit,
354                               __u64 softlimit, __u64 gracetime);
355 int qsd_id_lock_match(struct lustre_handle *, struct lustre_handle *);
356 int qsd_id_lock_cancel(const struct lu_env *, struct lquota_entry *);
357
358 /* qsd_reint.c */
359 int qsd_start_reint_thread(struct qsd_qtype_info *);
360 void qsd_stop_reint_thread(struct qsd_qtype_info *);
361
362 /* qsd_request.c */
363 typedef void (*qsd_req_completion_t) (const struct lu_env *,
364                                       struct qsd_qtype_info *,
365                                       struct quota_body *, struct quota_body *,
366                                       struct lustre_handle *,
367                                       struct lquota_lvb *, void *, int);
368 int qsd_send_dqacq(const struct lu_env *, struct obd_export *,
369                    struct quota_body *, bool, qsd_req_completion_t,
370                    struct qsd_qtype_info *, struct lustre_handle *,
371                    struct lquota_entry *);
372 int qsd_intent_lock(const struct lu_env *, struct obd_export *,
373                     struct quota_body *, bool, int, qsd_req_completion_t,
374                     struct qsd_qtype_info *, struct lquota_lvb *, void *);
375 int qsd_fetch_index(const struct lu_env *, struct obd_export *,
376                     struct idx_info *, unsigned int, struct page **, bool *);
377
378 /* qsd_writeback.c */
379 void qsd_bump_version(struct qsd_qtype_info *, __u64, bool);
380 void qsd_upd_schedule(struct qsd_qtype_info *, struct lquota_entry *,
381                       union lquota_id *, union lquota_rec *, __u64, bool);
382 /* qsd_config.c */
383 struct qsd_fsinfo *qsd_get_fsinfo(char *, bool);
384 void qsd_put_fsinfo(struct qsd_fsinfo *);
385 int qsd_config(char *valstr, char *fsname, int pool);
386 int qsd_process_config(struct lustre_cfg *);
387
388 /* qsd_handler.c */
389 int qsd_adjust(const struct lu_env *, struct lquota_entry *);
390
391 /* qsd_writeback.c */
392 void qsd_upd_schedule(struct qsd_qtype_info *, struct lquota_entry *,
393                       union lquota_id *, union lquota_rec *, __u64, bool);
394 void qsd_bump_version(struct qsd_qtype_info *, __u64, bool);
395 int qsd_start_upd_thread(struct qsd_instance *);
396 void qsd_stop_upd_thread(struct qsd_instance *);
397 void qsd_adjust_schedule(struct lquota_entry *, bool, bool);
398 #endif /* _QSD_INTERNAL_H */