4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
24 * Copyright (c) 2012 Whamcloud, Inc.
25 * Use is subject to license terms.
28 #include "lquota_internal.h"
30 #ifndef _QSD_INTERNAL_H
31 #define _QSD_INTERNAL_H
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.
45 /* name of service which created this qsd instance */
46 char qsd_svname[MAX_OBD_NAME];
48 /* pool ID is always 0 for now */
51 /* dt_device associated with this qsd instance */
52 struct dt_device *qsd_dev;
54 /* procfs directory where information related to the underlying slaves
56 cfs_proc_dir_entry_t *qsd_proc;
58 /* export used for the connection to quota master */
59 struct obd_export *qsd_exp;
61 /* ldlm namespace used for quota locks */
62 struct ldlm_namespace *qsd_ns;
64 /* on-disk directory where to store index files for this qsd instance */
65 struct dt_object *qsd_root;
67 /* We create 2 quota slave instances:
68 * - one for user quota
69 * - one for group quota
71 * This will have to be revisited if new quota types are added in the
72 * future. For the time being, we can just use an array. */
73 struct qsd_qtype_info *qsd_type_array[MAXQUOTAS];
75 /* per-filesystem quota information */
76 struct qsd_fsinfo *qsd_fsinfo;
78 /* link into qfs_qsd_list of qfs_fsinfo */
81 /* list of lqe entry which might need quota space adjustment */
82 cfs_list_t qsd_adjust_list;
84 /* lock protecting adjust list */
85 cfs_spinlock_t qsd_adjust_lock;
87 /* dedicated thread for updating slave index files. */
88 struct ptlrpc_thread qsd_upd_thread;
90 /* list of update tasks */
91 cfs_list_t qsd_upd_list;
93 /* r/w spinlock protecting:
95 * - the qsd update list
97 * - flags of the qsd_qtype_info */
98 cfs_rwlock_t qsd_lock;
100 /* Default quota settings which apply to all identifiers */
101 /* when blk qunit reaches this value, later write reqs from client
102 * should be sync. b=16642 */
103 unsigned long qsd_sync_threshold;
105 /* how long a service thread can wait for quota space.
106 * value dynamically computed from obd_timeout and at_max if not
107 * enforced here (via procfs) */
110 unsigned long qsd_is_md:1, /* managing quota for mdt */
111 qsd_started:1, /* instance is now started */
112 qsd_prepared:1, /* qsd_prepare() successfully
114 qsd_exp_valid:1,/* qsd_exp is now valid */
115 qsd_stopping:1; /* qsd_instance is stopping */
119 * Per-type quota information.
120 * Quota slave instance for a specific quota type. The qsd instance has one such
121 * structure for each quota type (i.e. user & group).
123 struct qsd_qtype_info {
124 /* reference count incremented by each user of this structure */
125 cfs_atomic_t qqi_ref;
127 /* quota type, either USRQUOTA or GRPQUOTA
128 * immutable after creation. */
131 /* Global index FID to use for this quota type */
132 struct lu_fid qqi_fid;
134 /* Slave index FID allocated by the master */
135 struct lu_fid qqi_slv_fid;
137 /* back pointer to qsd device
138 * immutable after creation. */
139 struct qsd_instance *qqi_qsd;
141 /* handle of global quota lock */
142 struct lustre_handle qqi_lockh;
144 /* Local index files storing quota settings for this quota type */
145 struct dt_object *qqi_acct_obj; /* accounting object */
146 struct dt_object *qqi_slv_obj; /* slave index copy */
147 struct dt_object *qqi_glb_obj; /* global index copy */
149 /* Current object versions */
150 __u64 qqi_slv_ver; /* slave index version */
151 __u64 qqi_glb_ver; /* global index version */
153 /* per quota ID information. All lquota entry are kept in a hash table
154 * and read from disk on cache miss. */
155 struct lquota_site *qqi_site;
157 /* Reintegration thread */
158 struct ptlrpc_thread qqi_reint_thread;
160 /* statistics on operations performed by this slave */
161 struct lprocfs_stats *qqi_stats;
163 /* deferred update for the global index copy */
164 cfs_list_t qqi_deferred_glb;
165 /* deferred update for the slave index copy */
166 cfs_list_t qqi_deferred_slv;
168 /* Various flags representing the current state of the slave for this
170 unsigned long qqi_glb_uptodate:1, /* global index uptodate
172 qqi_slv_uptodate:1, /* slave index uptodate
174 qqi_reint:1; /* in reintegration or not */
176 /* A list of references to this instance, for debugging */
177 struct lu_ref qqi_reference;
181 * Per-filesystem quota information
182 * Structure tracking quota enforcement status on a per-filesystem basis
185 /* filesystem name */
186 char qfs_name[MTI_NAME_MAXLEN];
188 /* what type of quota is enabled for each resource type. */
189 unsigned int qfs_enabled[LQUOTA_NR_RES];
191 /* list of all qsd_instance for this fs */
192 cfs_list_t qfs_qsd_list;
193 cfs_semaphore_t qfs_sem;
195 /* link to the global quota fsinfo list. */
198 /* reference count */
203 * Helper functions & prototypes
206 /* helper routine to find qsd_instance associated a lquota_entry */
207 static inline struct qsd_qtype_info *lqe2qqi(struct lquota_entry *lqe)
209 LASSERT(!lqe_is_master(lqe));
210 return (struct qsd_qtype_info *)lqe->lqe_site->lqs_parent;
213 /* qqi_getref/putref is used to track users of a qqi structure */
214 static inline void qqi_getref(struct qsd_qtype_info *qqi)
216 cfs_atomic_inc(&qqi->qqi_ref);
219 static inline void qqi_putref(struct qsd_qtype_info *qqi)
221 LASSERT(cfs_atomic_read(&qqi->qqi_ref) > 0);
222 cfs_atomic_dec(&qqi->qqi_ref);
225 #define QSD_RES_TYPE(qsd) ((qsd)->qsd_is_md ? LQUOTA_RES_MD : LQUOTA_RES_DT)
227 /* udpate record for slave & global index copy */
229 cfs_list_t qur_link; /* link into qsd_upd_list */
230 union lquota_id qur_qid;
231 union lquota_rec qur_rec;
232 struct qsd_qtype_info *qur_qqi;
233 struct lquota_entry *qur_lqe;
238 /* Common data shared by qsd-level handlers. This is allocated per-thread to
239 * reduce stack consumption. */
240 struct qsd_thread_info {
241 union lquota_rec qti_rec;
242 union lquota_id qti_id;
243 struct lu_fid qti_fid;
244 struct ldlm_res_id qti_resid;
245 struct ldlm_enqueue_info qti_einfo;
246 struct lustre_handle qti_lockh;
248 union ldlm_wire_lvb qti_lvb;
250 struct quota_body qti_body;
251 struct idx_info qti_ii;
253 char qti_buf[MTI_NAME_MAXLEN];
256 extern struct lu_context_key qsd_thread_key;
259 struct qsd_thread_info *qsd_info(const struct lu_env *env)
261 struct qsd_thread_info *info;
263 info = lu_context_key_get(&env->le_ctx, &qsd_thread_key);
265 lu_env_refill((struct lu_env *)env);
266 info = lu_context_key_get(&env->le_ctx, &qsd_thread_key);
272 /* helper function to check whether a given quota type is enabled */
273 static inline int qsd_type_enabled(struct qsd_instance *qsd, int type)
277 LASSERT(qsd != NULL);
278 LASSERT(type < MAXQUOTAS);
280 if (qsd->qsd_fsinfo == NULL)
283 pool = qsd->qsd_is_md ? LQUOTA_RES_MD : LQUOTA_RES_DT;
284 enabled = qsd->qsd_fsinfo->qfs_enabled[pool - LQUOTA_FIRST_RES];
286 return enabled & (1 << type);
289 /* helper function to set new qunit and compute associated qtune value */
290 static inline void qsd_set_qunit(struct lquota_entry *lqe, __u64 qunit)
292 if (lqe->lqe_qunit == qunit)
295 lqe->lqe_qunit = qunit;
297 /* With very large qunit support, we can't afford to have a static
298 * qtune value, e.g. with a 1PB qunit and qtune set to 50%, we would
299 * start pre-allocation when 512TB of free quota space remains.
300 * Therefore, we adapt qtune depending on the actual qunit value */
301 if (qunit == 0) /* if qunit is NULL */
302 lqe->lqe_qtune = 0; /* qtune = 0 */
303 else if (qunit == 1024) /* if 1MB or 1K inodes */
304 lqe->lqe_qtune = qunit >> 1; /* => 50% */
305 else if (qunit <= 1024 * 1024) /* up to 1GB or 1M inodes */
306 lqe->lqe_qtune = qunit >> 2; /* => 25% */
307 else if (qunit <= 4 * 1024 * 1024) /* up to 16GB or 16M inodes */
308 lqe->lqe_qtune = qunit >> 3; /* => 12.5% */
309 else /* above 4GB/4M */
310 lqe->lqe_qtune = 1024 * 1024; /* value capped to 1GB/1M */
312 LQUOTA_DEBUG(lqe, "changing qunit & qtune");
314 /* turn on pre-acquire when qunit is modified */
315 lqe->lqe_nopreacq = false;
318 #define QSD_WB_INTERVAL 60 /* 60 seconds */
320 /* helper function calculating how long a service thread should be waiting for
322 static inline int qsd_wait_timeout(struct qsd_instance *qsd)
324 if (qsd->qsd_timeout != 0)
325 return qsd->qsd_timeout;
326 return min_t(int, at_max / 2, obd_timeout / 2);
330 extern struct lquota_entry_operations qsd_lqe_ops;
331 int qsd_refresh_usage(const struct lu_env *, struct lquota_entry *);
332 int qsd_update_index(const struct lu_env *, struct qsd_qtype_info *,
333 union lquota_id *, bool, __u64, void *);
334 int qsd_update_lqe(const struct lu_env *, struct lquota_entry *, bool,
336 int qsd_write_version(const struct lu_env *, struct qsd_qtype_info *,
340 extern struct ldlm_enqueue_info qsd_glb_einfo;
341 extern struct ldlm_enqueue_info qsd_id_einfo;
342 int qsd_id_lock_match(struct lustre_handle *, struct lustre_handle *);
343 int qsd_id_lock_cancel(const struct lu_env *, struct lquota_entry *);
346 int qsd_start_reint_thread(struct qsd_qtype_info *);
347 void qsd_stop_reint_thread(struct qsd_qtype_info *);
350 typedef void (*qsd_req_completion_t) (const struct lu_env *,
351 struct qsd_qtype_info *,
352 struct quota_body *, struct quota_body *,
353 struct lustre_handle *,
354 union ldlm_wire_lvb *, void *, int);
355 int qsd_send_dqacq(const struct lu_env *, struct obd_export *,
356 struct quota_body *, bool, qsd_req_completion_t,
357 struct qsd_qtype_info *, struct lustre_handle *,
358 struct lquota_entry *);
359 int qsd_intent_lock(const struct lu_env *, struct obd_export *,
360 struct quota_body *, bool, int, qsd_req_completion_t,
361 struct qsd_qtype_info *, union ldlm_wire_lvb *, void *);
362 int qsd_fetch_index(const struct lu_env *, struct obd_export *,
363 struct idx_info *, unsigned int, cfs_page_t **, bool *);
365 /* qsd_writeback.c */
366 void qsd_bump_version(struct qsd_qtype_info *, __u64, bool);
367 void qsd_upd_schedule(struct qsd_qtype_info *, struct lquota_entry *,
368 union lquota_id *, union lquota_rec *, __u64, bool);
370 struct qsd_fsinfo *qsd_get_fsinfo(char *, bool);
371 void qsd_put_fsinfo(struct qsd_fsinfo *);
372 int qsd_process_config(struct lustre_cfg *);
375 int qsd_adjust(const struct lu_env *, struct lquota_entry *);
377 /* qsd_writeback.c */
378 void qsd_upd_schedule(struct qsd_qtype_info *, struct lquota_entry *,
379 union lquota_id *, union lquota_rec *, __u64, bool);
380 void qsd_bump_version(struct qsd_qtype_info *, __u64, bool);
381 int qsd_start_upd_thread(struct qsd_instance *);
382 void qsd_stop_upd_thread(struct qsd_instance *);
383 void qsd_adjust_schedule(struct lquota_entry *, bool, bool);
384 #endif /* _QSD_INTERNAL_H */