Whamcloud - gitweb
LU-3963 libcfs: convert ptlrpc,quota plus others to linux atomics
[fs/lustre-release.git] / lustre / quota / lquota_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, Intel Corporation.
25  * Use is subject to license terms.
26  */
27
28 #include <obd.h>
29 #include <lustre_quota.h>
30
31 #ifndef _LQUOTA_INTERNAL_H
32 #define _LQUOTA_INTERNAL_H
33
34 #define QTYPE_NAME(qtype) ((qtype) == USRQUOTA ? "usr" : "grp")
35 #define RES_NAME(res) ((res) == LQUOTA_RES_MD ? "md" : "dt")
36
37 #define QIF_IFLAGS (QIF_INODES | QIF_ITIME | QIF_ILIMITS)
38 #define QIF_BFLAGS (QIF_SPACE | QIF_BTIME | QIF_BLIMITS)
39
40 /* The biggest filename are the one used for slave index which are in the form
41  * of 0x%x-%s,glb_fid.f_oid,slv_uuid, that's to say:
42  * 2(0x) + 8(f_oid) + 1(-) + 40(UUID_MAX) which means 51 chars + '\0' */
43 #define LQUOTA_NAME_MAX 52
44
45 /* reserved OID in FID_SEQ_QUOTA for local objects */
46 enum lquota_local_oid {
47         LQUOTA_USR_OID          = 1UL, /* slave index copy for user quota */
48         LQUOTA_GRP_OID          = 2UL, /* slave index copy for group quota */
49         /* all OIDs after this are allocated dynamically by the QMT */
50         LQUOTA_GENERATED_OID    = 4096UL,
51 };
52
53 /*
54  * lquota_entry support
55  */
56
57 /* Common operations supported by a lquota_entry */
58 struct lquota_entry_operations {
59         /* Initialize specific fields of a lquota entry */
60         void (*lqe_init)(struct lquota_entry *, void *arg);
61
62         /* Read quota settings from disk and update lquota entry */
63         int (*lqe_read)(const struct lu_env *, struct lquota_entry *,
64                         void *arg);
65
66         /* Print debug information about a given lquota entry */
67         void (*lqe_debug)(struct lquota_entry *, void *,
68                           struct libcfs_debug_msg_data *, const char *,
69                           va_list);
70 };
71
72 /* Per-ID information specific to the quota master target */
73 struct lquota_mst_entry {
74         /* global hard limit, in inodes or kbytes */
75         __u64                   lme_hardlimit;
76
77         /* global quota soft limit, in inodes or kbytes */
78         __u64                   lme_softlimit;
79
80         /* grace time, in seconds */
81         __u64                   lme_gracetime;
82
83         /* last time we glimpsed */
84         __u64                   lme_revoke_time;
85
86         /* r/w semaphore used to protect concurrent access to the quota
87          * parameters which are stored on disk */
88         struct rw_semaphore     lme_sem;
89
90         /* quota space that may be released after glimpse */
91         __u64                   lme_may_rel;
92 };
93
94 /* Per-ID information specific to the quota slave */
95 struct lquota_slv_entry {
96         /* [ib]tune size, inodes or kbytes */
97         __u64                   lse_qtune;
98
99         /* per-ID lock handle */
100         struct lustre_handle    lse_lockh;
101
102         /* pending write which were granted quota space but haven't completed
103          * yet, in inodes or kbytes. */
104         __u64                   lse_pending_write;
105
106         /* writes waiting for quota space, in inodes or kbytes. */
107         __u64                   lse_waiting_write;
108
109         /* pending release, in inodes or kbytes */
110         __u64                   lse_pending_rel;
111
112         /* pending dqacq/dqrel requests. */
113         unsigned int            lse_pending_req;
114
115         /* rw spinlock protecting in-memory counters (i.e. lse_pending*) */
116         rwlock_t                lse_lock;
117
118         /* waiter for pending request done */
119         wait_queue_head_t       lse_waiters;
120
121         /* hint on current on-disk usage, in inodes or kbytes */
122         __u64                   lse_usage;
123
124         /* time to trigger quota adjust */
125         __u64                   lse_adjust_time;
126
127         /* return code of latest acquire RPC */
128         int                     lse_acq_rc;
129
130         /* when latest acquire RPC completed */
131         __u64                   lse_acq_time;
132
133         /* when latest edquot set */
134         __u64                   lse_edquot_time;
135 };
136
137 /* In-memory entry for each enforced quota id
138  * A lquota_entry structure belong to a single lquota_site */
139 struct lquota_entry {
140         /* link to site hash table */
141         cfs_hlist_node_t         lqe_hash;
142
143         /* quota identifier associated with this entry */
144         union lquota_id          lqe_id;
145
146         /* site this quota entry belongs to */
147         struct lquota_site      *lqe_site;
148
149         /* reference counter */
150         atomic_t                 lqe_ref;
151
152         /* linked to list of lqes which:
153          * - need quota space adjustment on slave
154          * - need glimpse to be sent on master */
155         cfs_list_t               lqe_link;
156
157         /* current quota settings/usage of this ID */
158         __u64           lqe_granted; /* granted limit, inodes or kbytes */
159         __u64           lqe_qunit; /* [ib]unit size, inodes or kbytes */
160         union {
161                 struct  lquota_mst_entry me; /* params specific to QMT */
162                 struct  lquota_slv_entry se; /* params specific to QSD */
163         } u;
164
165         /* flags describing the state of the lquota_entry */
166         unsigned long   lqe_enforced:1,/* quota enforced or not */
167                         lqe_uptodate:1,/* successfully read from disk */
168                         lqe_edquot:1,  /* id out of quota space on QMT */
169                         lqe_gl:1,      /* glimpse is in progress */
170                         lqe_nopreacq:1;/* pre-acquire disabled */
171 };
172
173 /* Compartment within which lquota_entry are unique.
174  * lquota_entry structures are kept in a hash table and read from disk if not
175  * present.  */
176 struct lquota_site {
177         /* Hash table storing lquota_entry structures */
178         cfs_hash_t      *lqs_hash;
179
180         /* Quota type, either user or group. */
181         int              lqs_qtype;
182
183         /* Record whether this site is for a QMT or a slave */
184         int              lqs_is_mst;
185
186         /* Vector of operations which can be done on lquota entry belonging to
187          * this quota site */
188         struct lquota_entry_operations  *lqs_ops;
189
190         /* Backpointer to parent structure, either QMT pool info for master or
191          * QSD for slave */
192         void            *lqs_parent;
193 };
194
195 #define lqe_hardlimit           u.me.lme_hardlimit
196 #define lqe_softlimit           u.me.lme_softlimit
197 #define lqe_gracetime           u.me.lme_gracetime
198 #define lqe_revoke_time         u.me.lme_revoke_time
199 #define lqe_sem                 u.me.lme_sem
200 #define lqe_may_rel             u.me.lme_may_rel
201
202 #define lqe_qtune               u.se.lse_qtune
203 #define lqe_pending_write       u.se.lse_pending_write
204 #define lqe_waiting_write       u.se.lse_waiting_write
205 #define lqe_pending_rel         u.se.lse_pending_rel
206 #define lqe_pending_req         u.se.lse_pending_req
207 #define lqe_waiters             u.se.lse_waiters
208 #define lqe_lock                u.se.lse_lock
209 #define lqe_usage               u.se.lse_usage
210 #define lqe_adjust_time         u.se.lse_adjust_time
211 #define lqe_lockh               u.se.lse_lockh
212 #define lqe_acq_rc              u.se.lse_acq_rc
213 #define lqe_acq_time            u.se.lse_acq_time
214 #define lqe_edquot_time         u.se.lse_edquot_time
215
216 #define LQUOTA_BUMP_VER 0x1
217 #define LQUOTA_SET_VER  0x2
218
219 /* helper routine to get/put reference on lquota_entry */
220 static inline void lqe_getref(struct lquota_entry *lqe)
221 {
222         LASSERT(lqe != NULL);
223         atomic_inc(&lqe->lqe_ref);
224 }
225
226 static inline void lqe_putref(struct lquota_entry *lqe)
227 {
228         LASSERT(lqe != NULL);
229         LASSERT(atomic_read(&lqe->lqe_ref) > 0);
230         if (atomic_dec_and_test(&lqe->lqe_ref))
231                 OBD_FREE_PTR(lqe);
232 }
233
234 static inline int lqe_is_master(struct lquota_entry *lqe)
235 {
236         return lqe->lqe_site->lqs_is_mst;
237 }
238
239 /* lqe locking helpers */
240 static inline void lqe_write_lock(struct lquota_entry *lqe)
241 {
242         if (lqe_is_master(lqe))
243                 down_write(&lqe->lqe_sem);
244         else
245                 write_lock(&lqe->lqe_lock);
246 }
247
248 static inline void lqe_write_unlock(struct lquota_entry *lqe)
249 {
250         if (lqe_is_master(lqe))
251                 up_write(&lqe->lqe_sem);
252         else
253                 write_unlock(&lqe->lqe_lock);
254 }
255
256 static inline void lqe_read_lock(struct lquota_entry *lqe)
257 {
258         if (lqe_is_master(lqe))
259                 down_read(&lqe->lqe_sem);
260         else
261                 read_lock(&lqe->lqe_lock);
262 }
263
264 static inline void lqe_read_unlock(struct lquota_entry *lqe)
265 {
266         if (lqe_is_master(lqe))
267                 up_read(&lqe->lqe_sem);
268         else
269                 read_unlock(&lqe->lqe_lock);
270 }
271
272 /*
273  * Helper functions & prototypes
274  */
275
276 /* minimum qunit size, 1K inode for metadata pool and 1MB for data pool */
277 #define LQUOTA_LEAST_QUNIT(type) \
278         (type == LQUOTA_RES_MD ? (1 << 10) : toqb(OFD_MAX_BRW_SIZE))
279
280 #define LQUOTA_OVER_FL(type) \
281         (type == USRQUOTA ? QUOTA_FL_OVER_USRQUOTA : QUOTA_FL_OVER_GRPQUOTA)
282
283 /* Common data shared by quota-level handlers. This is allocated per-thread to
284  * reduce stack consumption */
285 struct lquota_thread_info {
286         union  lquota_rec       qti_rec;
287         struct lu_buf           qti_lb;
288         struct lu_attr          qti_attr;
289         struct dt_object_format qti_dof;
290         struct lustre_mdt_attrs qti_lma;
291         struct lu_fid           qti_fid;
292         char                    qti_buf[LQUOTA_NAME_MAX];
293 };
294
295 #define qti_glb_rec     qti_rec.lqr_glb_rec
296 #define qti_acct_rec    qti_rec.lqr_acct_rec
297 #define qti_slv_rec     qti_rec.lqr_slv_rec
298
299 #define LQUOTA_BUMP_VER 0x1
300 #define LQUOTA_SET_VER  0x2
301
302 extern struct lu_context_key lquota_thread_key;
303
304 /* extract lquota_threa_info context from environment */
305 static inline
306 struct lquota_thread_info *lquota_info(const struct lu_env *env)
307 {
308         struct lquota_thread_info       *info;
309
310         info = lu_context_key_get(&env->le_ctx, &lquota_thread_key);
311         if (info == NULL) {
312                 lu_env_refill((struct lu_env *)env);
313                 info = lu_context_key_get(&env->le_ctx, &lquota_thread_key);
314         }
315         LASSERT(info);
316         return info;
317 }
318
319 #define req_is_acq(flags)    ((flags & QUOTA_DQACQ_FL_ACQ) != 0)
320 #define req_is_preacq(flags) ((flags & QUOTA_DQACQ_FL_PREACQ) != 0)
321 #define req_is_rel(flags)    ((flags & QUOTA_DQACQ_FL_REL) != 0)
322 #define req_has_rep(flags)   ((flags & QUOTA_DQACQ_FL_REPORT) != 0)
323
324 /* debugging macros */
325 #ifdef LIBCFS_DEBUG
326 #define lquota_lqe_debug(msgdata, mask, cdls, lqe, fmt, a...) do {      \
327         CFS_CHECK_STACK(msgdata, mask, cdls);                           \
328                                                                         \
329         if (((mask) & D_CANTMASK) != 0 ||                               \
330             ((libcfs_debug & (mask)) != 0 &&                            \
331              (libcfs_subsystem_debug & DEBUG_SUBSYSTEM) != 0))          \
332                 lquota_lqe_debug0(lqe, msgdata, fmt, ##a);              \
333 } while(0)
334
335 void lquota_lqe_debug0(struct lquota_entry *lqe,
336                        struct libcfs_debug_msg_data *data, const char *fmt, ...)
337         __attribute__ ((format (printf, 3, 4)));
338
339 #define LQUOTA_DEBUG_LIMIT(mask, lqe, fmt, a...) do {                          \
340         static cfs_debug_limit_state_t _lquota_cdls;                           \
341         LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, mask, &_lquota_cdls);              \
342         lquota_lqe_debug(&msgdata, mask, &_lquota_cdls, lqe, "$$$ "fmt" ",     \
343                          ##a);                                                 \
344 } while (0)
345
346 #define LQUOTA_ERROR(lqe, fmt, a...) LQUOTA_DEBUG_LIMIT(D_ERROR, lqe, fmt, ## a)
347 #define LQUOTA_WARN(lqe, fmt, a...) \
348         LQUOTA_DEBUG_LIMIT(D_WARNING, lqe, fmt, ## a)
349 #define LQUOTA_CONSOLE(lqe, fmt, a...) \
350         LQUOTA_DEBUG_LIMIT(D_CONSOLE, lqe, fmt, ## a)
351
352 #define LQUOTA_DEBUG(lock, fmt, a...) do {                                 \
353         LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_QUOTA, NULL);                \
354         lquota_lqe_debug(&msgdata, D_QUOTA, NULL, lqe, "$$$ "fmt" ", ##a); \
355 } while (0)
356 #else /* !LIBCFS_DEBUG */
357 # define LQUOTA_DEBUG(lqe, fmt, a...) ((void)0)
358 # define LQUOTA_ERROR(lqe, fmt, a...) ((void)0)
359 # define LQUOTA_WARN(lqe, fmt, a...) ((void)0)
360 # define LQUOTA_CONSOLE(lqe, fmt, a...) ((void)0)
361 # define lquota_lqe_debug(cdls, level, lqe, file, func, line, fmt, a...) \
362                 ((void)0)
363 #endif
364
365 /* lquota_lib.c */
366 struct dt_object *acct_obj_lookup(const struct lu_env *, struct dt_device *,
367                                   int);
368 void lquota_generate_fid(struct lu_fid *, int, int, int);
369 int lquota_extract_fid(const struct lu_fid *, int *, int *, int *);
370 const struct dt_index_features *glb_idx_feature(struct lu_fid *);
371 extern struct kmem_cache *lqe_kmem;
372
373 /* lquota_entry.c */
374 /* site create/destroy */
375 struct lquota_site *lquota_site_alloc(const struct lu_env *, void *, bool,
376                                       short, struct lquota_entry_operations *);
377 void lquota_site_free(const struct lu_env *, struct lquota_site *);
378 /* quota entry operations */
379 struct lquota_entry *lqe_locate(const struct lu_env *, struct lquota_site *,
380                                 union lquota_id *);
381
382 /* lquota_disk.c */
383 struct dt_object *lquota_disk_dir_find_create(const struct lu_env *,
384                                               struct dt_device *,
385                                               struct dt_object *, const char *);
386 struct dt_object *lquota_disk_glb_find_create(const struct lu_env *,
387                                               struct dt_device *,
388                                               struct dt_object *,
389                                               struct lu_fid *, bool);
390 struct dt_object *lquota_disk_slv_find_create(const struct lu_env *,
391                                               struct dt_device *,
392                                               struct dt_object *,
393                                               struct lu_fid *,
394                                               struct obd_uuid *, bool);
395 typedef int (*lquota_disk_slv_cb_t) (const struct lu_env *, struct lu_fid *,
396                                      char *, struct lu_fid *, void *);
397 int lquota_disk_for_each_slv(const struct lu_env *, struct dt_object *,
398                              struct lu_fid *, lquota_disk_slv_cb_t, void *);
399 struct dt_object *lquota_disk_slv_find(const struct lu_env *,
400                                        struct dt_device *, struct dt_object *,
401                                        const struct lu_fid *,
402                                        struct obd_uuid *);
403 int lquota_disk_read(const struct lu_env *, struct dt_object *,
404                      union lquota_id *, struct dt_rec *);
405 int lquota_disk_declare_write(const struct lu_env *, struct thandle *,
406                               struct dt_object *, union lquota_id *);
407 int lquota_disk_write(const struct lu_env *, struct thandle *,
408                       struct dt_object *, union lquota_id *, struct dt_rec *,
409                       __u32, __u64 *);
410 int lquota_disk_update_ver(const struct lu_env *, struct dt_device *,
411                            struct dt_object *, __u64);
412
413 /* qmt_dev.c */
414 int qmt_glb_init(void);
415 void qmt_glb_fini(void);
416
417 /* lproc_quota.c */
418 extern struct file_operations lprocfs_quota_seq_fops;
419
420 /* qsd_lib.c */
421 int qsd_glb_init(void);
422 void qsd_glb_fini(void);
423 #endif /* _LQUOTA_INTERNAL_H */