Whamcloud - gitweb
LU-17744 ldiskfs: mballoc stats fixes
[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 <dt_object.h>
30 #include <lustre_quota.h>
31
32 #ifndef _LQUOTA_INTERNAL_H
33 #define _LQUOTA_INTERNAL_H
34
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         LQUOTA_PRJ_OID          = 3UL, /* slave index copy for project quota */
50         /* all OIDs after this are allocated dynamically by the QMT */
51         LQUOTA_GENERATED_OID    = 4096UL,
52 };
53
54 static inline __u32 qtype2slv_oid(int qtype)
55 {
56         switch (qtype) {
57         case USRQUOTA:
58                 return LQUOTA_USR_OID;
59         case GRPQUOTA:
60                 return LQUOTA_GRP_OID;
61         case PRJQUOTA:
62                 return LQUOTA_PRJ_OID;
63         }
64
65         /* should not come here, just make compile happy */
66         return LQUOTA_USR_OID;
67 }
68
69 /*
70  * lquota_entry support
71  */
72
73 /* Common operations supported by a lquota_entry */
74 struct lquota_entry_operations {
75         /* Initialize specific fields of a lquota entry */
76         void (*lqe_init)(struct lquota_entry *, void *arg);
77
78         /* Read quota settings from disk and update lquota entry */
79         int (*lqe_read)(const struct lu_env *, struct lquota_entry *,
80                         void *arg, bool find);
81
82         /* Print debug information about a given lquota entry */
83         void (*lqe_debug)(struct lquota_entry *, void *,
84                           struct libcfs_debug_msg_data *,
85                           struct va_format *vaf);
86 };
87
88 /* Per-ID information specific to the quota master target */
89 struct lquota_mst_entry {
90         /* global hard limit, in inodes or kbytes */
91         __u64                   lme_hardlimit;
92
93         /* global quota soft limit, in inodes or kbytes */
94         __u64                   lme_softlimit;
95
96         /* grace time, in seconds */
97         __u64                   lme_gracetime;
98
99         /* last time we glimpsed */
100         time64_t                lme_revoke_time;
101
102         /* r/w semaphore used to protect concurrent access to the quota
103          * parameters which are stored on disk */
104         struct rw_semaphore     lme_sem;
105
106         /* quota space that may be released after glimpse */
107         __u64                   lme_may_rel;
108 };
109
110 /* Per-ID information specific to the quota slave */
111 struct lquota_slv_entry {
112         /* [ib]tune size, inodes or kbytes */
113         __u64                   lse_qtune;
114
115         /* per-ID lock handle */
116         struct lustre_handle    lse_lockh;
117
118         /* pending write which were granted quota space but haven't completed
119          * yet, in inodes or kbytes. */
120         __u64                   lse_pending_write;
121
122         /* writes waiting for quota space, in inodes or kbytes. */
123         __u64                   lse_waiting_write;
124
125         /* pending release, in inodes or kbytes */
126         __u64                   lse_pending_rel;
127
128         /* pending dqacq/dqrel requests. */
129         unsigned int            lse_pending_req;
130
131         /* rw spinlock protecting in-memory counters (i.e. lse_pending*) */
132         rwlock_t                lse_lock;
133
134         /* waiter for pending request done */
135         wait_queue_head_t       lse_waiters;
136
137         /* hint on current on-disk usage, in inodes or kbytes */
138         __u64                   lse_usage;
139
140         /* time to trigger quota adjust */
141         time64_t                lse_adjust_time;
142
143         /* return code of latest acquire RPC */
144         int                     lse_acq_rc;
145
146         /* when latest acquire RPC completed */
147         time64_t                lse_acq_time;
148
149         /* when latest edquot set */
150         time64_t                lse_edquot_time;
151 };
152
153 /* In-memory entry for each enforced quota id
154  * A lquota_entry structure belong to a single lquota_site */
155 struct lquota_entry {
156         /* link to site hash table */
157         struct hlist_node        lqe_hash;
158
159         /* quota identifier associated with this entry */
160         union lquota_id          lqe_id;
161
162         /* site this quota entry belongs to */
163         struct lquota_site      *lqe_site;
164
165         /* reference counter */
166         atomic_t                 lqe_ref;
167
168         /* linked to list of lqes which:
169          * - need quota space adjustment on slave
170          * - need glimpse to be sent on master */
171         struct list_head         lqe_link;
172
173         /* current quota settings/usage of this ID */
174         __u64           lqe_granted; /* granted limit, inodes or kbytes */
175         /* used in quota pool recalc process (only on QMT) */
176         __u64           lqe_recalc_granted;
177         __u64           lqe_qunit; /* [ib]unit size, inodes or kbytes */
178         union {
179                 struct  lquota_mst_entry me; /* params specific to QMT */
180                 struct  lquota_slv_entry se; /* params specific to QSD */
181         } u;
182
183         /* flags describing the state of the lquota_entry */
184         unsigned long   lqe_enforced:1,   /* quota enforced or not */
185                         lqe_uptodate:1,   /* successfully read from disk */
186                         lqe_edquot:1,     /* id out of quota space on QMT */
187                         lqe_gl:1,         /* glimpse is in progress */
188                         lqe_nopreacq:1,   /* pre-acquire disabled */
189                         lqe_is_default:1, /* the default quota is used */
190                         lqe_is_global:1,  /* lqe belongs to global pool "0x0"*/
191                         lqe_is_deleted:1, /* lqe will be deleted soon */
192                         lqe_is_reset:1,   /* lqe has been reset */
193                         lqe_revoke:1;     /* all extra grant will be revoked */
194
195         /* the lock to protect lqe_glbl_data */
196         struct mutex             lqe_glbl_data_lock;
197         struct lqe_glbl_data    *lqe_glbl_data;
198         struct work_struct       lqe_work; /* workitem to free lvbo */
199 };
200
201 #define lqe_qtype(lqe)          (lqe->lqe_site->lqs_qtype)
202 #define lqe_rtype(lqe)          (lqe2qpi(lqe)->qpi_rtype)
203
204 struct lqe_glbl_entry {
205         __u64                    lge_qunit;
206         unsigned long            lge_idx:16,
207                                  /* index of target */
208                                  lge_edquot:1,
209                                  /* true when minimum qunit is set */
210                                  lge_qunit_set:1,
211                                  /* qunit or edquot is changed - need
212                                  * to send glimpse to appropriate slave */
213                                  lge_qunit_nu:1,
214                                  lge_edquot_nu:1;
215 };
216
217 struct lqe_glbl_data {
218         struct lqe_glbl_entry   *lqeg_arr;
219         /* number of initialised entries */
220         int                      lqeg_num_used;
221         /* number of allocated entries */
222         int                      lqeg_num_alloc;
223 };
224
225 /* Compartment within which lquota_entry are unique.
226  * lquota_entry structures are kept in a hash table and read from disk if not
227  * present.  */
228 struct lquota_site {
229         /* Hash table storing lquota_entry structures */
230         struct cfs_hash *lqs_hash;
231
232         /* Quota type, either user or group. */
233         int              lqs_qtype;
234
235         /* Record whether this site is for a QMT or a slave */
236         int              lqs_is_mst;
237
238         /* Vector of operations which can be done on lquota entry belonging to
239          * this quota site */
240         const struct lquota_entry_operations    *lqs_ops;
241
242         /* Backpointer to parent structure, either QMT pool info for master or
243          * QSD for slave */
244         void            *lqs_parent;
245 };
246
247 #define lqe_hardlimit           u.me.lme_hardlimit
248 #define lqe_softlimit           u.me.lme_softlimit
249 #define lqe_gracetime           u.me.lme_gracetime
250 #define lqe_revoke_time         u.me.lme_revoke_time
251 #define lqe_sem                 u.me.lme_sem
252 #define lqe_may_rel             u.me.lme_may_rel
253
254 #define lqe_qtune               u.se.lse_qtune
255 #define lqe_pending_write       u.se.lse_pending_write
256 #define lqe_waiting_write       u.se.lse_waiting_write
257 #define lqe_pending_rel         u.se.lse_pending_rel
258 #define lqe_pending_req         u.se.lse_pending_req
259 #define lqe_waiters             u.se.lse_waiters
260 #define lqe_lock                u.se.lse_lock
261 #define lqe_usage               u.se.lse_usage
262 #define lqe_adjust_time         u.se.lse_adjust_time
263 #define lqe_lockh               u.se.lse_lockh
264 #define lqe_acq_rc              u.se.lse_acq_rc
265 #define lqe_acq_time            u.se.lse_acq_time
266 #define lqe_edquot_time         u.se.lse_edquot_time
267
268 #define LQUOTA_BUMP_VER 0x1
269 #define LQUOTA_SET_VER  0x2
270
271 extern struct kmem_cache *lqe_kmem;
272
273 /* helper routine to get/put reference on lquota_entry */
274 static inline void lqe_getref(struct lquota_entry *lqe)
275 {
276         LASSERT(lqe != NULL);
277         atomic_inc(&lqe->lqe_ref);
278 }
279
280 static inline void lqe_putref(struct lquota_entry *lqe)
281 {
282         LASSERT(lqe != NULL);
283         LASSERT(atomic_read(&lqe->lqe_ref) > 0);
284         if (atomic_dec_and_test(&lqe->lqe_ref))
285                 OBD_SLAB_FREE_PTR(lqe, lqe_kmem);
286 }
287
288 static inline int lqe_is_master(struct lquota_entry *lqe)
289 {
290         return lqe->lqe_site->lqs_is_mst;
291 }
292
293 /* lqe locking helpers */
294 static inline void lqe_write_lock(struct lquota_entry *lqe)
295 {
296         if (lqe_is_master(lqe))
297                 down_write(&lqe->lqe_sem);
298         else
299                 write_lock(&lqe->lqe_lock);
300 }
301
302 static inline void lqe_write_unlock(struct lquota_entry *lqe)
303 {
304         if (lqe_is_master(lqe))
305                 up_write(&lqe->lqe_sem);
306         else
307                 write_unlock(&lqe->lqe_lock);
308 }
309
310 static inline void lqe_read_lock(struct lquota_entry *lqe)
311 {
312         if (lqe_is_master(lqe))
313                 down_read(&lqe->lqe_sem);
314         else
315                 read_lock(&lqe->lqe_lock);
316 }
317
318 static inline void lqe_read_unlock(struct lquota_entry *lqe)
319 {
320         if (lqe_is_master(lqe))
321                 up_read(&lqe->lqe_sem);
322         else
323                 read_unlock(&lqe->lqe_lock);
324 }
325
326 /*
327  * Helper functions & prototypes
328  */
329
330 /* minimum qunit size, 1K inode for metadata pool and 1MB for data pool */
331 #define LQUOTA_LEAST_QUNIT(type) \
332         (type == LQUOTA_RES_MD ? (1 << 10) : toqb(OFD_MAX_BRW_SIZE))
333
334 static inline enum osd_quota_local_flags lquota_over_fl(int qtype)
335 {
336         switch (qtype) {
337         case USRQUOTA:
338                 return QUOTA_FL_OVER_USRQUOTA;
339         case GRPQUOTA:
340                 return QUOTA_FL_OVER_GRPQUOTA;
341         case PRJQUOTA:
342                 return QUOTA_FL_OVER_PRJQUOTA;
343         }
344
345         /* should not come here, just make compile happy */
346         return QUOTA_FL_OVER_USRQUOTA;
347 }
348
349 /* Common data shared by quota-level handlers. This is allocated per-thread to
350  * reduce stack consumption */
351 struct lquota_thread_info {
352         union  lquota_rec       qti_rec;
353         struct lu_buf           qti_lb;
354         struct lu_attr          qti_attr;
355         struct dt_object_format qti_dof;
356         struct lu_fid           qti_fid;
357         char                    qti_buf[LQUOTA_NAME_MAX];
358 };
359
360 #define qti_glb_rec     qti_rec.lqr_glb_rec
361 #define qti_acct_rec    qti_rec.lqr_acct_rec
362 #define qti_slv_rec     qti_rec.lqr_slv_rec
363
364 #define LQUOTA_BUMP_VER 0x1
365 #define LQUOTA_SET_VER  0x2
366
367 extern struct lu_context_key lquota_thread_key;
368
369 /* extract lquota_threa_info context from environment */
370 static inline
371 struct lquota_thread_info *lquota_info(const struct lu_env *env)
372 {
373         return lu_env_info(env, &lquota_thread_key);
374 }
375
376 #define req_is_acq(flags)    ((flags & QUOTA_DQACQ_FL_ACQ) != 0)
377 #define req_is_preacq(flags) ((flags & QUOTA_DQACQ_FL_PREACQ) != 0)
378 #define req_is_rel(flags)    ((flags & QUOTA_DQACQ_FL_REL) != 0)
379 #define req_has_rep(flags)   ((flags & QUOTA_DQACQ_FL_REPORT) != 0)
380
381 /* debugging macros */
382 #ifdef LIBCFS_DEBUG
383 #define lquota_lqe_debug(msgdata, mask, cdls, lqe, fmt, a...) do {      \
384         if (((mask) & D_CANTMASK) != 0 ||                               \
385             ((libcfs_debug & (mask)) != 0 &&                            \
386              (libcfs_subsystem_debug & DEBUG_SUBSYSTEM) != 0))          \
387                 lquota_lqe_debug0(lqe, msgdata, fmt, ##a);              \
388 } while(0)
389
390 void lquota_lqe_debug0(struct lquota_entry *lqe,
391                        struct libcfs_debug_msg_data *data, const char *fmt, ...)
392         __attribute__ ((format (printf, 3, 4)));
393
394 #define LQUOTA_DEBUG_LIMIT(mask, lqe, fmt, a...) do {                          \
395         static struct cfs_debug_limit_state _lquota_cdls;                      \
396         LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, mask, &_lquota_cdls);              \
397         lquota_lqe_debug(&msgdata, mask, &_lquota_cdls, lqe, "$$$ "fmt" ",     \
398                          ##a);                                                 \
399 } while (0)
400
401 #define LQUOTA_ERROR(lqe, fmt, a...) LQUOTA_DEBUG_LIMIT(D_ERROR, lqe, fmt, ## a)
402 #define LQUOTA_WARN(lqe, fmt, a...) \
403         LQUOTA_DEBUG_LIMIT(D_WARNING, lqe, fmt, ## a)
404 #define LQUOTA_CONSOLE(lqe, fmt, a...) \
405         LQUOTA_DEBUG_LIMIT(D_CONSOLE, lqe, fmt, ## a)
406
407 #define LQUOTA_ELEVEL_LQES(level, env, fmt, a...) do {          \
408         int i;                                                  \
409         for (i = 0; i < qti_lqes_cnt(env); i++) {               \
410                 LQUOTA_##level(qti_lqes(env)[i], fmt, ##a);     \
411         }                                                       \
412 } while (0)
413 #define LQUOTA_WARN_LQES(lqe, fmt, a...) \
414                 LQUOTA_ELEVEL_LQES(WARN, env, fmt, ##a)
415 #define LQUOTA_CONSOLE_LQES(lqe, fmt, a...) \
416                 LQUOTA_ELEVEL_LQES(CONSOLE, env, fmt, ##a)
417 #define LQUOTA_ERROR_LQES(lqe, fmt, a...) \
418                 LQUOTA_ELEVEL_LQES(ERROR, env, fmt, ##a)
419
420 #define LQUOTA_DEBUG(lqe, fmt, a...) do {                                 \
421         LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_QUOTA, NULL);                \
422         lquota_lqe_debug(&msgdata, D_QUOTA, NULL, lqe, "$$$ "fmt" ", ##a); \
423 } while (0)
424
425 #define LQUOTA_DEBUG_LQES(env, fmt, a...) do {                  \
426         int i;                                                  \
427         for (i = 0; i < qti_lqes_cnt(env); i++) {               \
428                 LQUOTA_DEBUG(qti_lqes(env)[i], fmt, ##a);       \
429         }                                                       \
430 } while (0)
431
432
433 #else /* !LIBCFS_DEBUG */
434 # define LQUOTA_DEBUG_LQES(lqe, fmt, a...) ((void)0)
435 # define LQUOTA_ERROR_LQES(lqe, fmt, a...) ((void)0)
436 # define LQUOTA_WARN_LQES(lqe, fmt, a...) ((void)0)
437 # define LQUOTA_CONSOLE_LQES(lqe, fmt, a...) ((void)0)
438 # define LQUOTA_DEBUG(lqe, fmt, a...) ((void)0)
439 # define LQUOTA_ERROR(lqe, fmt, a...) ((void)0)
440 # define LQUOTA_WARN(lqe, fmt, a...) ((void)0)
441 # define LQUOTA_CONSOLE(lqe, fmt, a...) ((void)0)
442 # define lquota_lqe_debug(cdls, level, lqe, file, func, line, fmt, a...) \
443                 ((void)0)
444 #endif
445
446 /* lquota_lib.c */
447 struct dt_object *acct_obj_lookup(const struct lu_env *, struct dt_device *,
448                                   int);
449 void lquota_generate_fid(struct lu_fid *, int, int);
450 int lquota_extract_fid(const struct lu_fid *, int *, int *);
451 const struct dt_index_features *glb_idx_feature(struct lu_fid *);
452 int lquota_obj_iter(const struct lu_env *env, struct dt_device *dev,
453                     struct dt_object *obj, struct obd_quotactl *oqctl,
454                     char *buffer, int size, bool is_glb, bool is_md);
455
456 /* lquota_entry.c */
457 /* site create/destroy */
458 struct lquota_site *lquota_site_alloc(const struct lu_env *env, void *parent,
459                                       bool master, short qtype,
460                                       const struct lquota_entry_operations *op);
461 void lquota_site_free(const struct lu_env *, struct lquota_site *);
462 /* quota entry operations */
463 #define lqe_locate(env, site, id) lqe_locate_find(env, site, id, false)
464 #define lqe_find(env, site, id) lqe_locate_find(env, site, id, true)
465 struct lquota_entry *lqe_locate_find(const struct lu_env *,
466                                      struct lquota_site *,
467                                      union lquota_id *, bool);
468
469 static inline void lqe_set_deleted(struct lquota_entry *lqe)
470 {
471         lqe->lqe_enforced = 0;
472         lqe->lqe_edquot = 0;
473         lqe->lqe_is_default = 0;
474         lqe->lqe_hardlimit = 0;
475         lqe->lqe_softlimit = 0;
476         lqe->lqe_gracetime = 0;
477
478         lqe->lqe_is_deleted = 1;
479 }
480
481 /* lquota_disk.c */
482 struct dt_object *lquota_disk_dir_find_create(const struct lu_env *,
483                                               struct dt_device *,
484                                               struct dt_object *, const char *);
485 struct dt_object *lquota_disk_glb_find_create(const struct lu_env *,
486                                               struct dt_device *,
487                                               struct dt_object *,
488                                               struct lu_fid *, bool);
489 struct dt_object *lquota_disk_slv_find_create(const struct lu_env *,
490                                               struct dt_device *,
491                                               struct dt_object *,
492                                               struct lu_fid *,
493                                               struct obd_uuid *, bool);
494 typedef int (*lquota_disk_slv_cb_t) (const struct lu_env *, struct lu_fid *,
495                                      char *, struct lu_fid *, void *);
496 int lquota_disk_for_each_slv(const struct lu_env *, struct dt_object *,
497                              struct lu_fid *, lquota_disk_slv_cb_t, void *);
498 struct dt_object *lquota_disk_slv_find(const struct lu_env *,
499                                        struct dt_device *, struct dt_object *,
500                                        const struct lu_fid *,
501                                        struct obd_uuid *);
502 int lquota_disk_read(const struct lu_env *, struct dt_object *,
503                      union lquota_id *, struct dt_rec *);
504 int lquota_disk_declare_write(const struct lu_env *, struct thandle *,
505                               struct dt_object *, union lquota_id *);
506 int lquota_disk_write(const struct lu_env *, struct thandle *,
507                       struct dt_object *, union lquota_id *, struct dt_rec *,
508                       __u32, __u64 *);
509 int lquota_disk_delete(const struct lu_env *env, struct thandle *th,
510                        struct dt_object *obj, __u64 qid, __u64 *ver);
511 int lquota_disk_update_ver(const struct lu_env *, struct dt_device *,
512                            struct dt_object *, __u64);
513 int lquota_disk_write_glb(const struct lu_env *, struct dt_object *, __u64,
514                           struct lquota_glb_rec *);
515
516 /* qmt_dev.c */
517 int qmt_glb_init(void);
518 void qmt_glb_fini(void);
519
520 /* lproc_quota.c */
521 extern const struct proc_ops lprocfs_quota_seq_fops;
522
523 /* qsd_lib.c */
524 int qsd_glb_init(void);
525 void qsd_glb_fini(void);
526 #endif /* _LQUOTA_INTERNAL_H */