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