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