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