Whamcloud - gitweb
LU-1842 quota: add per-filesystem information
[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 Whamcloud, Inc.
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         /* pool ID is always 0 for now */
49         int                      qsd_pool_id;
50
51         /* dt_device associated with this qsd instance */
52         struct dt_device        *qsd_dev;
53
54         /* procfs directory where information related to the underlying slaves
55          * are exported */
56         cfs_proc_dir_entry_t    *qsd_proc;
57
58         /* on-disk directory where to store index files for this qsd instance */
59         struct dt_object        *qsd_root;
60
61         /* We create 2 quota slave instances:
62          * - one for user quota
63          * - one for group quota
64          *
65          * This will have to be revisited if new quota types are added in the
66          * future. For the time being, we can just use an array. */
67         struct qsd_qtype_info   *qsd_type_array[MAXQUOTAS];
68
69         /* r/w spinlock protecting:
70          * - the state flags
71          * - the qsd update list
72          * - the deferred list
73          * - flags of the qsd_qtype_info
74          *
75          * probably way too much :(
76          */
77         cfs_rwlock_t             qsd_lock;
78
79         /* per-filesystem quota information */
80         struct qsd_fsinfo       *qsd_fsinfo;
81
82         /* link into qfs_qsd_list of qfs_fsinfo */
83         cfs_list_t               qsd_link;
84
85         unsigned long            qsd_is_md:1,    /* managing quota for mdt */
86                                  qsd_stopping:1; /* qsd_instance is stopping */
87 };
88
89 /*
90  * Per-type quota information.
91  * Quota slave instance for a specific quota type. The qsd instance has one such
92  * structure for each quota type (i.e. user & group).
93  */
94 struct qsd_qtype_info {
95         /* reference count incremented by each user of this structure */
96         cfs_atomic_t             qqi_ref;
97
98         /* quota type, either USRQUOTA or GRPQUOTA
99          * immutable after creation. */
100         int                      qqi_qtype;
101
102         /* Global index FID to use for this quota type */
103         struct lu_fid            qqi_fid;
104
105         /* back pointer to qsd device
106          * immutable after creation. */
107         struct qsd_instance     *qqi_qsd;
108
109         /* handle of global quota lock */
110         struct lustre_handle     qqi_lockh;
111
112         /* Local index files storing quota settings for this quota type */
113         struct dt_object        *qqi_acct_obj; /* accounting object */
114         struct dt_object        *qqi_slv_obj;  /* slave index copy */
115         struct dt_object        *qqi_glb_obj;  /* global index copy */
116
117         /* Current object versions */
118         __u64                    qqi_slv_ver; /* slave index version */
119         __u64                    qqi_glb_ver; /* global index version */
120
121         /* Various flags representing the current state of the slave for this
122          * quota type. */
123         unsigned long            qqi_glb_uptodate:1, /* global index uptodate
124                                                         with master */
125                                  qqi_slv_uptodate:1, /* slave index uptodate
126                                                         with master */
127                                  qqi_reint:1;    /* in reintegration or not */
128
129         /* A list of references to this instance, for debugging */
130         struct lu_ref            qqi_reference;
131 };
132
133 /*
134  * Per-filesystem quota information
135  * Structure tracking quota enforcement status on a per-filesystem basis
136  */
137 struct qsd_fsinfo {
138         /* filesystem name */
139         char                    qfs_name[MTI_NAME_MAXLEN];
140
141         /* what type of quota is enabled for each resource type. */
142         unsigned int            qfs_enabled[LQUOTA_NR_RES];
143
144         /* list of all qsd_instance for this fs */
145         cfs_list_t              qfs_qsd_list;
146         cfs_semaphore_t         qfs_sem;
147
148         /* link to the global quota fsinfo list.  */
149         cfs_list_t              qfs_link;
150
151         /* reference count */
152         int                     qfs_ref;
153 };
154
155 /*
156  * Helper functions & prototypes
157  */
158
159 /* helper routine to find qsd_instance associated a lquota_entry */
160 static inline struct qsd_qtype_info *lqe2qqi(struct lquota_entry *lqe)
161 {
162         LASSERT(!lqe_is_master(lqe));
163         return (struct qsd_qtype_info *)lqe->lqe_site->lqs_parent;
164 }
165
166 /* qqi_getref/putref is used to track users of a qqi structure  */
167 static inline void qqi_getref(struct qsd_qtype_info *qqi)
168 {
169         cfs_atomic_inc(&qqi->qqi_ref);
170 }
171
172 static inline void qqi_putref(struct qsd_qtype_info *qqi)
173 {
174         LASSERT(cfs_atomic_read(&qqi->qqi_ref) > 0);
175         cfs_atomic_dec(&qqi->qqi_ref);
176 }
177
178 /* all kind of operations supported by qsd_dqacq() */
179 enum qsd_ops {
180         QSD_ADJ, /* adjust quota space based on current qunit */
181         QSD_ACQ, /* acquire space for requests */
182         QSD_REL, /* release all space quota space uncondionnally */
183         QSD_REP, /* report space usage during reintegration */
184 };
185
186 #define QSD_RES_TYPE(qsd) ((qsd)->qsd_is_md ? LQUOTA_RES_MD : LQUOTA_RES_DT)
187
188 /* Common data shared by qsd-level handlers. This is allocated per-thread to
189  * reduce stack consumption.  */
190 struct qsd_thread_info {
191         union lquota_rec                qti_rec;
192         union lquota_id                 qti_id;
193         struct lu_fid                   qti_fid;
194         struct ldlm_res_id              qti_resid;
195         struct ldlm_enqueue_info        qti_einfo;
196         struct lustre_handle            qti_lockh;
197         __u64                           qti_slv_ver;
198         union ldlm_wire_lvb             qti_lvb;
199         union {
200                 struct quota_body       qti_body;
201                 struct idx_info         qti_ii;
202         };
203         char                            qti_buf[MTI_NAME_MAXLEN];
204 };
205
206 extern struct lu_context_key qsd_thread_key;
207
208 static inline
209 struct qsd_thread_info *qsd_info(const struct lu_env *env)
210 {
211         struct qsd_thread_info *info;
212
213         info = lu_context_key_get(&env->le_ctx, &qsd_thread_key);
214         if (info == NULL) {
215                 lu_env_refill((struct lu_env *)env);
216                 info = lu_context_key_get(&env->le_ctx, &qsd_thread_key);
217         }
218         LASSERT(info);
219         return info;
220 }
221
222 /* helper function to check whether a given quota type is enabled */
223 static inline int qsd_type_enabled(struct qsd_instance *qsd, int type)
224 {
225         int     enabled, pool;
226
227         LASSERT(qsd != NULL);
228         LASSERT(type < MAXQUOTAS);
229
230         if (qsd->qsd_fsinfo == NULL)
231                 return 0;
232
233         pool = qsd->qsd_is_md ? LQUOTA_RES_MD : LQUOTA_RES_DT;
234         enabled = qsd->qsd_fsinfo->qfs_enabled[pool - LQUOTA_FIRST_RES];
235
236         return enabled & (1 << type);
237 }
238
239 /* helper function to set new qunit and compute associated qtune value */
240 static inline void qsd_set_qunit(struct lquota_entry *lqe, __u64 qunit)
241 {
242         if (lqe->lqe_qunit == qunit)
243                 return;
244
245         lqe->lqe_qunit = qunit;
246
247         /* With very large qunit support, we can't afford to have a static
248          * qtune value, e.g. with a 1PB qunit and qtune set to 50%, we would
249          * start pre-allocation when 512TB of free quota space remains.
250          * Therefore, we adapt qtune depending on the actual qunit value */
251         if (qunit == 0)                         /* if qunit is NULL           */
252                 lqe->lqe_qtune = 0;             /*  qtune = 0                 */
253         else if (qunit == 1024)                 /* if 1MB or 1K inodes        */
254                 lqe->lqe_qtune = qunit >> 1;    /*  => 50%                    */
255         else if (qunit <= 1024 * 1024)          /* up to 1GB or 1M inodes     */
256                 lqe->lqe_qtune = qunit >> 2;    /*  => 25%                    */
257         else if (qunit <= 4 * 1024 * 1024)      /* up to 16GB or 16M inodes   */
258                 lqe->lqe_qtune = qunit >> 3;    /*  => 12.5%                  */
259         else                                    /* above 4GB/4M               */
260                 lqe->lqe_qtune = 1024 * 1024;   /*  value capped to 1GB/1M    */
261
262         LQUOTA_DEBUG(lqe, "changing qunit & qtune");
263
264         /* turn on pre-acquire when qunit is modified */
265         lqe->lqe_nopreacq = false;
266 }
267
268 /* qsd_entry.c */
269 extern struct lquota_entry_operations qsd_lqe_ops;
270 int qsd_refresh_usage(const struct lu_env *, struct lquota_entry *);
271 int qsd_update_index(const struct lu_env *, struct qsd_qtype_info *,
272                      union lquota_id *, bool, __u64, void *);
273 int qsd_update_lqe(const struct lu_env *, struct lquota_entry *, bool,
274                    void *);
275 int qsd_write_version(const struct lu_env *, struct qsd_qtype_info *,
276                       __u64, bool);
277
278 /* qsd_lock.c */
279 extern struct ldlm_enqueue_info qsd_glb_einfo;
280 extern struct ldlm_enqueue_info qsd_id_einfo;
281 int qsd_id_lock_match(struct lustre_handle *, struct lustre_handle *);
282 int qsd_id_lock_cancel(const struct lu_env *, struct lquota_entry *);
283
284 /* qsd_reint.c */
285 int qsd_start_reint_thread(struct qsd_qtype_info *);
286 void qsd_stop_reint_thread(struct qsd_qtype_info *);
287
288 /* qsd_request.c */
289 typedef void (*qsd_req_completion_t) (const struct lu_env *,
290                                       struct qsd_qtype_info *,
291                                       struct quota_body *, struct quota_body *,
292                                       struct lustre_handle *,
293                                       union ldlm_wire_lvb *, void *, int);
294 int qsd_send_dqacq(const struct lu_env *, struct obd_export *,
295                    struct quota_body *, bool, qsd_req_completion_t,
296                    struct qsd_qtype_info *, struct lustre_handle *,
297                    struct lquota_entry *);
298 int qsd_intent_lock(const struct lu_env *, struct obd_export *,
299                     struct quota_body *, bool, int, qsd_req_completion_t,
300                     struct qsd_qtype_info *, union ldlm_wire_lvb *, void *);
301 int qsd_fetch_index(const struct lu_env *, struct obd_export *,
302                     struct idx_info *, unsigned int, cfs_page_t **, bool *);
303
304 /* qsd_writeback.c */
305 void qsd_bump_version(struct qsd_qtype_info *, __u64, bool);
306 void qsd_upd_schedule(struct qsd_qtype_info *, struct lquota_entry *,
307                       union lquota_id *, union lquota_rec *, __u64, bool);
308 /* qsd_config.c */
309 struct qsd_fsinfo *qsd_get_fsinfo(char *, bool);
310 void qsd_put_fsinfo(struct qsd_fsinfo *);
311 int qsd_process_config(struct lustre_cfg *);
312
313 /* qsd_handler.c */
314 /* XXX to be replaced with real function once qsd_handler landed */
315 static inline int qsd_dqacq(const struct lu_env *env, struct lquota_entry *lqe,
316                             enum qsd_ops op)
317 {
318         return 0;
319 }
320
321 #endif /* _QSD_INTERNAL_H */