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