Whamcloud - gitweb
f10ed72bb963574371d03bbad895bb5098a092ca
[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, bool find);
80
81         /* Print debug information about a given lquota entry */
82         void (*lqe_debug)(struct lquota_entry *, void *,
83                           struct libcfs_debug_msg_data *,
84                           struct va_format *vaf);
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         /* used in quota pool recalc process (only on QMT) */
175         __u64           lqe_recalc_granted;
176         __u64           lqe_qunit; /* [ib]unit size, inodes or kbytes */
177         union {
178                 struct  lquota_mst_entry me; /* params specific to QMT */
179                 struct  lquota_slv_entry se; /* params specific to QSD */
180         } u;
181
182         /* flags describing the state of the lquota_entry */
183         unsigned long   lqe_enforced:1,   /* quota enforced or not */
184                         lqe_uptodate:1,   /* successfully read from disk */
185                         lqe_edquot:1,     /* id out of quota space on QMT */
186                         lqe_gl:1,         /* glimpse is in progress */
187                         lqe_nopreacq:1,   /* pre-acquire disabled */
188                         lqe_is_default:1, /* the default quota is used */
189                         lqe_is_global:1,  /* lqe belongs to global pool "0x0"*/
190                         lqe_is_deleted:1; /* lqe will be deleted soon */
191
192         struct lqe_glbl_data    *lqe_glbl_data;
193 };
194
195 #define lqe_qtype(lqe)          (lqe->lqe_site->lqs_qtype)
196 #define lqe_rtype(lqe)          (lqe2qpi(lqe)->qpi_rtype)
197
198 struct lqe_glbl_entry {
199         __u64                    lge_qunit;
200         unsigned long            lge_edquot:1,
201                                  /* true when minimum qunit is set */
202                                  lge_qunit_set:1,
203                                  /* qunit or edquot is changed - need
204                                  * to send glimpse to appropriate slave */
205                                  lge_qunit_nu:1,
206                                  lge_edquot_nu:1;
207 };
208
209 struct lqe_glbl_data {
210         struct lqe_glbl_entry   *lqeg_arr;
211         /* number of initialised entries */
212         int                      lqeg_num_used;
213         /* number of allocated entries */
214         int                      lqeg_num_alloc;
215 };
216
217 /* Compartment within which lquota_entry are unique.
218  * lquota_entry structures are kept in a hash table and read from disk if not
219  * present.  */
220 struct lquota_site {
221         /* Hash table storing lquota_entry structures */
222         struct cfs_hash *lqs_hash;
223
224         /* Quota type, either user or group. */
225         int              lqs_qtype;
226
227         /* Record whether this site is for a QMT or a slave */
228         int              lqs_is_mst;
229
230         /* Vector of operations which can be done on lquota entry belonging to
231          * this quota site */
232         const struct lquota_entry_operations    *lqs_ops;
233
234         /* Backpointer to parent structure, either QMT pool info for master or
235          * QSD for slave */
236         void            *lqs_parent;
237 };
238
239 #define lqe_hardlimit           u.me.lme_hardlimit
240 #define lqe_softlimit           u.me.lme_softlimit
241 #define lqe_gracetime           u.me.lme_gracetime
242 #define lqe_revoke_time         u.me.lme_revoke_time
243 #define lqe_sem                 u.me.lme_sem
244 #define lqe_may_rel             u.me.lme_may_rel
245
246 #define lqe_qtune               u.se.lse_qtune
247 #define lqe_pending_write       u.se.lse_pending_write
248 #define lqe_waiting_write       u.se.lse_waiting_write
249 #define lqe_pending_rel         u.se.lse_pending_rel
250 #define lqe_pending_req         u.se.lse_pending_req
251 #define lqe_waiters             u.se.lse_waiters
252 #define lqe_lock                u.se.lse_lock
253 #define lqe_usage               u.se.lse_usage
254 #define lqe_adjust_time         u.se.lse_adjust_time
255 #define lqe_lockh               u.se.lse_lockh
256 #define lqe_acq_rc              u.se.lse_acq_rc
257 #define lqe_acq_time            u.se.lse_acq_time
258 #define lqe_edquot_time         u.se.lse_edquot_time
259
260 #define LQUOTA_BUMP_VER 0x1
261 #define LQUOTA_SET_VER  0x2
262
263 extern struct kmem_cache *lqe_kmem;
264
265 /* helper routine to get/put reference on lquota_entry */
266 static inline void lqe_getref(struct lquota_entry *lqe)
267 {
268         LASSERT(lqe != NULL);
269         atomic_inc(&lqe->lqe_ref);
270 }
271
272 static inline void lqe_putref(struct lquota_entry *lqe)
273 {
274         LASSERT(lqe != NULL);
275         LASSERT(atomic_read(&lqe->lqe_ref) > 0);
276         if (atomic_dec_and_test(&lqe->lqe_ref))
277                 OBD_SLAB_FREE_PTR(lqe, lqe_kmem);
278 }
279
280 static inline int lqe_is_master(struct lquota_entry *lqe)
281 {
282         return lqe->lqe_site->lqs_is_mst;
283 }
284
285 /* lqe locking helpers */
286 static inline void lqe_write_lock(struct lquota_entry *lqe)
287 {
288         if (lqe_is_master(lqe))
289                 down_write(&lqe->lqe_sem);
290         else
291                 write_lock(&lqe->lqe_lock);
292 }
293
294 static inline void lqe_write_unlock(struct lquota_entry *lqe)
295 {
296         if (lqe_is_master(lqe))
297                 up_write(&lqe->lqe_sem);
298         else
299                 write_unlock(&lqe->lqe_lock);
300 }
301
302 static inline void lqe_read_lock(struct lquota_entry *lqe)
303 {
304         if (lqe_is_master(lqe))
305                 down_read(&lqe->lqe_sem);
306         else
307                 read_lock(&lqe->lqe_lock);
308 }
309
310 static inline void lqe_read_unlock(struct lquota_entry *lqe)
311 {
312         if (lqe_is_master(lqe))
313                 up_read(&lqe->lqe_sem);
314         else
315                 read_unlock(&lqe->lqe_lock);
316 }
317
318 /*
319  * Helper functions & prototypes
320  */
321
322 /* minimum qunit size, 1K inode for metadata pool and 1MB for data pool */
323 #define LQUOTA_LEAST_QUNIT(type) \
324         (type == LQUOTA_RES_MD ? (1 << 10) : toqb(OFD_MAX_BRW_SIZE))
325
326 static inline enum osd_quota_local_flags lquota_over_fl(int qtype)
327 {
328         switch (qtype) {
329         case USRQUOTA:
330                 return QUOTA_FL_OVER_USRQUOTA;
331         case GRPQUOTA:
332                 return QUOTA_FL_OVER_GRPQUOTA;
333         case PRJQUOTA:
334                 return QUOTA_FL_OVER_PRJQUOTA;
335         }
336
337         /* should not come here, just make compile happy */
338         return QUOTA_FL_OVER_USRQUOTA;
339 }
340
341 /* Common data shared by quota-level handlers. This is allocated per-thread to
342  * reduce stack consumption */
343 struct lquota_thread_info {
344         union  lquota_rec       qti_rec;
345         struct lu_buf           qti_lb;
346         struct lu_attr          qti_attr;
347         struct dt_object_format qti_dof;
348         struct lu_fid           qti_fid;
349         char                    qti_buf[LQUOTA_NAME_MAX];
350 };
351
352 #define qti_glb_rec     qti_rec.lqr_glb_rec
353 #define qti_acct_rec    qti_rec.lqr_acct_rec
354 #define qti_slv_rec     qti_rec.lqr_slv_rec
355
356 #define LQUOTA_BUMP_VER 0x1
357 #define LQUOTA_SET_VER  0x2
358
359 extern struct lu_context_key lquota_thread_key;
360
361 /* extract lquota_threa_info context from environment */
362 static inline
363 struct lquota_thread_info *lquota_info(const struct lu_env *env)
364 {
365         return lu_env_info(env, &lquota_thread_key);
366 }
367
368 #define req_is_acq(flags)    ((flags & QUOTA_DQACQ_FL_ACQ) != 0)
369 #define req_is_preacq(flags) ((flags & QUOTA_DQACQ_FL_PREACQ) != 0)
370 #define req_is_rel(flags)    ((flags & QUOTA_DQACQ_FL_REL) != 0)
371 #define req_has_rep(flags)   ((flags & QUOTA_DQACQ_FL_REPORT) != 0)
372
373 /* debugging macros */
374 #ifdef LIBCFS_DEBUG
375 #define lquota_lqe_debug(msgdata, mask, cdls, lqe, fmt, a...) do {      \
376         CFS_CHECK_STACK(msgdata, mask, cdls);                           \
377                                                                         \
378         if (((mask) & D_CANTMASK) != 0 ||                               \
379             ((libcfs_debug & (mask)) != 0 &&                            \
380              (libcfs_subsystem_debug & DEBUG_SUBSYSTEM) != 0))          \
381                 lquota_lqe_debug0(lqe, msgdata, fmt, ##a);              \
382 } while(0)
383
384 void lquota_lqe_debug0(struct lquota_entry *lqe,
385                        struct libcfs_debug_msg_data *data, const char *fmt, ...)
386         __attribute__ ((format (printf, 3, 4)));
387
388 #define LQUOTA_DEBUG_LIMIT(mask, lqe, fmt, a...) do {                          \
389         static struct cfs_debug_limit_state _lquota_cdls;                      \
390         LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, mask, &_lquota_cdls);              \
391         lquota_lqe_debug(&msgdata, mask, &_lquota_cdls, lqe, "$$$ "fmt" ",     \
392                          ##a);                                                 \
393 } while (0)
394
395 #define LQUOTA_ERROR(lqe, fmt, a...) LQUOTA_DEBUG_LIMIT(D_ERROR, lqe, fmt, ## a)
396 #define LQUOTA_WARN(lqe, fmt, a...) \
397         LQUOTA_DEBUG_LIMIT(D_WARNING, lqe, fmt, ## a)
398 #define LQUOTA_CONSOLE(lqe, fmt, a...) \
399         LQUOTA_DEBUG_LIMIT(D_CONSOLE, lqe, fmt, ## a)
400
401 #define LQUOTA_ELEVEL_LQES(level, env, fmt, a...) do {          \
402         int i;                                                  \
403         for (i = 0; i < qti_lqes_cnt(env); i++) {               \
404                 LQUOTA_##level(qti_lqes(env)[i], fmt, ##a);     \
405         }                                                       \
406 } while (0)
407 #define LQUOTA_WARN_LQES(lqe, fmt, a...) \
408                 LQUOTA_ELEVEL_LQES(WARN, env, fmt, ##a)
409 #define LQUOTA_CONSOLE_LQES(lqe, fmt, a...) \
410                 LQUOTA_ELEVEL_LQES(CONSOLE, env, fmt, ##a)
411 #define LQUOTA_ERROR_LQES(lqe, fmt, a...) \
412                 LQUOTA_ELEVEL_LQES(ERROR, env, fmt, ##a)
413
414 #define LQUOTA_DEBUG(lqe, fmt, a...) do {                                 \
415         LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_QUOTA, NULL);                \
416         lquota_lqe_debug(&msgdata, D_QUOTA, NULL, lqe, "$$$ "fmt" ", ##a); \
417 } while (0)
418
419 #define LQUOTA_DEBUG_LQES(env, fmt, a...) do {                  \
420         int i;                                                  \
421         for (i = 0; i < qti_lqes_cnt(env); i++) {               \
422                 LQUOTA_DEBUG(qti_lqes(env)[i], fmt, ##a);       \
423         }                                                       \
424 } while (0)
425
426
427 #else /* !LIBCFS_DEBUG */
428 # define LQUOTA_DEBUG_LQES(lqe, fmt, a...) ((void)0)
429 # define LQUOTA_ERROR_LQES(lqe, fmt, a...) ((void)0)
430 # define LQUOTA_WARN_LQES(lqe, fmt, a...) ((void)0)
431 # define LQUOTA_CONSOLE_LQES(lqe, fmt, a...) ((void)0)
432 # define LQUOTA_DEBUG(lqe, fmt, a...) ((void)0)
433 # define LQUOTA_ERROR(lqe, fmt, a...) ((void)0)
434 # define LQUOTA_WARN(lqe, fmt, a...) ((void)0)
435 # define LQUOTA_CONSOLE(lqe, fmt, a...) ((void)0)
436 # define lquota_lqe_debug(cdls, level, lqe, file, func, line, fmt, a...) \
437                 ((void)0)
438 #endif
439
440 /* lquota_lib.c */
441 struct dt_object *acct_obj_lookup(const struct lu_env *, struct dt_device *,
442                                   int);
443 void lquota_generate_fid(struct lu_fid *, int, int);
444 int lquota_extract_fid(const struct lu_fid *, int *, int *);
445 const struct dt_index_features *glb_idx_feature(struct lu_fid *);
446
447 /* lquota_entry.c */
448 /* site create/destroy */
449 struct lquota_site *lquota_site_alloc(const struct lu_env *env, void *parent,
450                                       bool master, short qtype,
451                                       const struct lquota_entry_operations *op);
452 void lquota_site_free(const struct lu_env *, struct lquota_site *);
453 /* quota entry operations */
454 #define lqe_locate(env, site, id) lqe_locate_find(env, site, id, false)
455 #define lqe_find(env, site, id) lqe_locate_find(env, site, id, true)
456 struct lquota_entry *lqe_locate_find(const struct lu_env *,
457                                      struct lquota_site *,
458                                      union lquota_id *, bool);
459
460 static inline void lqe_set_deleted(struct lquota_entry *lqe)
461 {
462         lqe->lqe_enforced = 0;
463         lqe->lqe_edquot = 0;
464         lqe->lqe_is_default = 0;
465         lqe->lqe_hardlimit = 0;
466         lqe->lqe_softlimit = 0;
467         lqe->lqe_gracetime = 0;
468
469         lqe->lqe_is_deleted = 1;
470 }
471
472 /* lquota_disk.c */
473 struct dt_object *lquota_disk_dir_find_create(const struct lu_env *,
474                                               struct dt_device *,
475                                               struct dt_object *, const char *);
476 struct dt_object *lquota_disk_glb_find_create(const struct lu_env *,
477                                               struct dt_device *,
478                                               struct dt_object *,
479                                               struct lu_fid *, bool);
480 struct dt_object *lquota_disk_slv_find_create(const struct lu_env *,
481                                               struct dt_device *,
482                                               struct dt_object *,
483                                               struct lu_fid *,
484                                               struct obd_uuid *, bool);
485 typedef int (*lquota_disk_slv_cb_t) (const struct lu_env *, struct lu_fid *,
486                                      char *, struct lu_fid *, void *);
487 int lquota_disk_for_each_slv(const struct lu_env *, struct dt_object *,
488                              struct lu_fid *, lquota_disk_slv_cb_t, void *);
489 struct dt_object *lquota_disk_slv_find(const struct lu_env *,
490                                        struct dt_device *, struct dt_object *,
491                                        const struct lu_fid *,
492                                        struct obd_uuid *);
493 int lquota_disk_read(const struct lu_env *, struct dt_object *,
494                      union lquota_id *, struct dt_rec *);
495 int lquota_disk_declare_write(const struct lu_env *, struct thandle *,
496                               struct dt_object *, union lquota_id *);
497 int lquota_disk_write(const struct lu_env *, struct thandle *,
498                       struct dt_object *, union lquota_id *, struct dt_rec *,
499                       __u32, __u64 *);
500 int lquota_disk_delete(const struct lu_env *env, struct thandle *th,
501                        struct dt_object *obj, __u64 qid, __u64 *ver);
502 int lquota_disk_update_ver(const struct lu_env *, struct dt_device *,
503                            struct dt_object *, __u64);
504 int lquota_disk_write_glb(const struct lu_env *, struct dt_object *, __u64,
505                           struct lquota_glb_rec *);
506
507 /* qmt_dev.c */
508 int qmt_glb_init(void);
509 void qmt_glb_fini(void);
510
511 /* lproc_quota.c */
512 extern const struct proc_ops lprocfs_quota_seq_fops;
513
514 /* qsd_lib.c */
515 int qsd_glb_init(void);
516 void qsd_glb_fini(void);
517 #endif /* _LQUOTA_INTERNAL_H */