1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32#ifndef _LINUX_QUOTA_
33#define _LINUX_QUOTA_
34
35#include <linux/list.h>
36#include <linux/mutex.h>
37#include <linux/rwsem.h>
38#include <linux/spinlock.h>
39#include <linux/wait.h>
40#include <linux/percpu_counter.h>
41
42#include <linux/dqblk_xfs.h>
43#include <linux/dqblk_v1.h>
44#include <linux/dqblk_v2.h>
45
46#include <linux/atomic.h>
47#include <linux/uidgid.h>
48#include <linux/projid.h>
49#include <uapi/linux/quota.h>
50
51#include <linux/rh_kabi.h>
52
53#undef USRQUOTA
54#undef GRPQUOTA
55#undef PRJQUOTA
56enum quota_type {
57 USRQUOTA = 0,
58 GRPQUOTA = 1,
59 PRJQUOTA = 2,
60};
61
62
63#define QTYPE_MASK_USR (1 << USRQUOTA)
64#define QTYPE_MASK_GRP (1 << GRPQUOTA)
65#define QTYPE_MASK_PRJ (1 << PRJQUOTA)
66
67typedef __kernel_uid32_t qid_t;
68typedef long long qsize_t;
69
70struct kqid {
71 union {
72 kuid_t uid;
73 kgid_t gid;
74 kprojid_t projid;
75 };
76 enum quota_type type;
77};
78
79extern bool qid_eq(struct kqid left, struct kqid right);
80extern bool qid_lt(struct kqid left, struct kqid right);
81extern qid_t from_kqid(struct user_namespace *to, struct kqid qid);
82extern qid_t from_kqid_munged(struct user_namespace *to, struct kqid qid);
83extern bool qid_valid(struct kqid qid);
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99static inline struct kqid make_kqid(struct user_namespace *from,
100 enum quota_type type, qid_t qid)
101{
102 struct kqid kqid;
103
104 kqid.type = type;
105 switch (type) {
106 case USRQUOTA:
107 kqid.uid = make_kuid(from, qid);
108 break;
109 case GRPQUOTA:
110 kqid.gid = make_kgid(from, qid);
111 break;
112 case PRJQUOTA:
113 kqid.projid = make_kprojid(from, qid);
114 break;
115 default:
116 BUG();
117 }
118 return kqid;
119}
120
121
122
123
124
125
126
127static inline struct kqid make_kqid_invalid(enum quota_type type)
128{
129 struct kqid kqid;
130
131 kqid.type = type;
132 switch (type) {
133 case USRQUOTA:
134 kqid.uid = INVALID_UID;
135 break;
136 case GRPQUOTA:
137 kqid.gid = INVALID_GID;
138 break;
139 case PRJQUOTA:
140 kqid.projid = INVALID_PROJID;
141 break;
142 default:
143 BUG();
144 }
145 return kqid;
146}
147
148
149
150
151
152static inline struct kqid make_kqid_uid(kuid_t uid)
153{
154 struct kqid kqid;
155 kqid.type = USRQUOTA;
156 kqid.uid = uid;
157 return kqid;
158}
159
160
161
162
163
164static inline struct kqid make_kqid_gid(kgid_t gid)
165{
166 struct kqid kqid;
167 kqid.type = GRPQUOTA;
168 kqid.gid = gid;
169 return kqid;
170}
171
172
173
174
175
176static inline struct kqid make_kqid_projid(kprojid_t projid)
177{
178 struct kqid kqid;
179 kqid.type = PRJQUOTA;
180 kqid.projid = projid;
181 return kqid;
182}
183
184
185
186
187
188
189static inline bool qid_has_mapping(struct user_namespace *ns, struct kqid qid)
190{
191 return from_kqid(ns, qid) != (qid_t) -1;
192}
193
194
195extern spinlock_t dq_data_lock;
196
197
198
199#define DQUOT_INIT_ALLOC max(V1_INIT_ALLOC, V2_INIT_ALLOC)
200#define DQUOT_INIT_REWRITE max(V1_INIT_REWRITE, V2_INIT_REWRITE)
201#define DQUOT_DEL_ALLOC max(V1_DEL_ALLOC, V2_DEL_ALLOC)
202#define DQUOT_DEL_REWRITE max(V1_DEL_REWRITE, V2_DEL_REWRITE)
203
204
205
206
207struct mem_dqblk {
208 qsize_t dqb_bhardlimit;
209 qsize_t dqb_bsoftlimit;
210 qsize_t dqb_curspace;
211 qsize_t dqb_rsvspace;
212 qsize_t dqb_ihardlimit;
213 qsize_t dqb_isoftlimit;
214 qsize_t dqb_curinodes;
215 time64_t dqb_btime;
216 time64_t dqb_itime;
217};
218
219
220
221
222struct quota_format_type;
223
224struct mem_dqinfo {
225 struct quota_format_type *dqi_format;
226 int dqi_fmt_id;
227
228 struct list_head dqi_dirty_list;
229 unsigned long dqi_flags;
230 unsigned int dqi_bgrace;
231 unsigned int dqi_igrace;
232 qsize_t dqi_max_spc_limit;
233 qsize_t dqi_max_ino_limit;
234 void *dqi_priv;
235};
236
237struct super_block;
238
239
240#define DQF_GETINFO_MASK (DQF_ROOT_SQUASH | DQF_SYS_FILE)
241
242#define DQF_SETINFO_MASK DQF_ROOT_SQUASH
243
244enum {
245 DQF_INFO_DIRTY_B = DQF_PRIVATE,
246};
247#define DQF_INFO_DIRTY (1 << DQF_INFO_DIRTY_B)
248
249extern void mark_info_dirty(struct super_block *sb, int type);
250static inline int info_dirty(struct mem_dqinfo *info)
251{
252 return test_bit(DQF_INFO_DIRTY_B, &info->dqi_flags);
253}
254
255enum {
256 DQST_LOOKUPS,
257 DQST_DROPS,
258 DQST_READS,
259 DQST_WRITES,
260 DQST_CACHE_HITS,
261 DQST_ALLOC_DQUOTS,
262 DQST_FREE_DQUOTS,
263 DQST_SYNCS,
264 _DQST_DQSTAT_LAST
265};
266
267struct dqstats {
268 int stat[_DQST_DQSTAT_LAST];
269 struct percpu_counter counter[_DQST_DQSTAT_LAST];
270};
271
272extern struct dqstats dqstats;
273
274static inline void dqstats_inc(unsigned int type)
275{
276 percpu_counter_inc(&dqstats.counter[type]);
277}
278
279static inline void dqstats_dec(unsigned int type)
280{
281 percpu_counter_dec(&dqstats.counter[type]);
282}
283
284#define DQ_MOD_B 0
285#define DQ_BLKS_B 1
286#define DQ_INODES_B 2
287#define DQ_FAKE_B 3
288#define DQ_READ_B 4
289#define DQ_ACTIVE_B 5
290#define DQ_LASTSET_B 6
291
292
293
294
295
296struct dquot {
297 struct hlist_node dq_hash;
298 struct list_head dq_inuse;
299 struct list_head dq_free;
300 struct list_head dq_dirty;
301 struct mutex dq_lock;
302 spinlock_t dq_dqb_lock;
303 atomic_t dq_count;
304 struct super_block *dq_sb;
305 struct kqid dq_id;
306 loff_t dq_off;
307 unsigned long dq_flags;
308 struct mem_dqblk dq_dqb;
309};
310
311
312struct quota_format_ops {
313 int (*check_quota_file)(struct super_block *sb, int type);
314 int (*read_file_info)(struct super_block *sb, int type);
315 int (*write_file_info)(struct super_block *sb, int type);
316 int (*free_file_info)(struct super_block *sb, int type);
317 int (*read_dqblk)(struct dquot *dquot);
318 int (*commit_dqblk)(struct dquot *dquot);
319 int (*release_dqblk)(struct dquot *dquot);
320 int (*get_next_id)(struct super_block *sb, struct kqid *qid);
321 RH_KABI_RESERVE(1)
322 RH_KABI_RESERVE(2)
323};
324
325
326struct dquot_operations {
327 int (*write_dquot) (struct dquot *);
328 struct dquot *(*alloc_dquot)(struct super_block *, int);
329 void (*destroy_dquot)(struct dquot *);
330 int (*acquire_dquot) (struct dquot *);
331 int (*release_dquot) (struct dquot *);
332 int (*mark_dirty) (struct dquot *);
333 int (*write_info) (struct super_block *, int);
334
335
336 qsize_t *(*get_reserved_space) (struct inode *);
337 int (*get_projid) (struct inode *, kprojid_t *);
338
339 int (*get_inode_usage) (struct inode *, qsize_t *);
340
341 int (*get_next_id) (struct super_block *sb, struct kqid *qid);
342 RH_KABI_RESERVE(1)
343 RH_KABI_RESERVE(2)
344};
345
346struct path;
347
348
349struct qc_dqblk {
350 int d_fieldmask;
351 u64 d_spc_hardlimit;
352 u64 d_spc_softlimit;
353 u64 d_ino_hardlimit;
354 u64 d_ino_softlimit;
355 u64 d_space;
356 u64 d_ino_count;
357 s64 d_ino_timer;
358
359 s64 d_spc_timer;
360 int d_ino_warns;
361 int d_spc_warns;
362 u64 d_rt_spc_hardlimit;
363 u64 d_rt_spc_softlimit;
364 u64 d_rt_space;
365 s64 d_rt_spc_timer;
366 int d_rt_spc_warns;
367};
368
369
370
371
372
373#define QC_INO_SOFT (1<<0)
374#define QC_INO_HARD (1<<1)
375#define QC_SPC_SOFT (1<<2)
376#define QC_SPC_HARD (1<<3)
377#define QC_RT_SPC_SOFT (1<<4)
378#define QC_RT_SPC_HARD (1<<5)
379#define QC_LIMIT_MASK (QC_INO_SOFT | QC_INO_HARD | QC_SPC_SOFT | QC_SPC_HARD | \
380 QC_RT_SPC_SOFT | QC_RT_SPC_HARD)
381#define QC_SPC_TIMER (1<<6)
382#define QC_INO_TIMER (1<<7)
383#define QC_RT_SPC_TIMER (1<<8)
384#define QC_TIMER_MASK (QC_SPC_TIMER | QC_INO_TIMER | QC_RT_SPC_TIMER)
385#define QC_SPC_WARNS (1<<9)
386#define QC_INO_WARNS (1<<10)
387#define QC_RT_SPC_WARNS (1<<11)
388#define QC_WARNS_MASK (QC_SPC_WARNS | QC_INO_WARNS | QC_RT_SPC_WARNS)
389#define QC_SPACE (1<<12)
390#define QC_INO_COUNT (1<<13)
391#define QC_RT_SPACE (1<<14)
392#define QC_ACCT_MASK (QC_SPACE | QC_INO_COUNT | QC_RT_SPACE)
393#define QC_FLAGS (1<<15)
394
395#define QCI_SYSFILE (1 << 0)
396#define QCI_ROOT_SQUASH (1 << 1)
397#define QCI_ACCT_ENABLED (1 << 2)
398#define QCI_LIMITS_ENFORCED (1 << 3)
399
400
401struct qc_type_state {
402 unsigned int flags;
403 unsigned int spc_timelimit;
404
405 unsigned int ino_timelimit;
406 unsigned int rt_spc_timelimit;
407 unsigned int spc_warnlimit;
408 unsigned int ino_warnlimit;
409 unsigned int rt_spc_warnlimit;
410 unsigned long long ino;
411 blkcnt_t blocks;
412 blkcnt_t nextents;
413};
414
415struct qc_state {
416 unsigned int s_incoredqs;
417 struct qc_type_state s_state[MAXQUOTAS];
418};
419
420
421struct qc_info {
422 int i_fieldmask;
423 unsigned int i_flags;
424 unsigned int i_spc_timelimit;
425
426 unsigned int i_ino_timelimit;
427 unsigned int i_rt_spc_timelimit;
428 unsigned int i_spc_warnlimit;
429 unsigned int i_ino_warnlimit;
430 unsigned int i_rt_spc_warnlimit;
431};
432
433
434struct quotactl_ops {
435 int (*quota_on)(struct super_block *, int, int, const struct path *);
436 int (*quota_off)(struct super_block *, int);
437 int (*quota_enable)(struct super_block *, unsigned int);
438 int (*quota_disable)(struct super_block *, unsigned int);
439 int (*quota_sync)(struct super_block *, int);
440 int (*set_info)(struct super_block *, int, struct qc_info *);
441 int (*get_dqblk)(struct super_block *, struct kqid, struct qc_dqblk *);
442 int (*get_nextdqblk)(struct super_block *, struct kqid *,
443 struct qc_dqblk *);
444 int (*set_dqblk)(struct super_block *, struct kqid, struct qc_dqblk *);
445 int (*get_state)(struct super_block *, struct qc_state *);
446 int (*rm_xquota)(struct super_block *, unsigned int);
447 RH_KABI_RESERVE(1)
448 RH_KABI_RESERVE(2)
449 RH_KABI_RESERVE(3)
450 RH_KABI_RESERVE(4)
451};
452
453struct quota_format_type {
454 int qf_fmt_id;
455 const struct quota_format_ops *qf_ops;
456 struct module *qf_owner;
457 struct quota_format_type *qf_next;
458};
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473enum {
474 _DQUOT_USAGE_ENABLED = 0,
475 _DQUOT_LIMITS_ENABLED,
476 _DQUOT_SUSPENDED,
477
478
479 _DQUOT_STATE_FLAGS
480};
481#define DQUOT_USAGE_ENABLED (1 << _DQUOT_USAGE_ENABLED * MAXQUOTAS)
482#define DQUOT_LIMITS_ENABLED (1 << _DQUOT_LIMITS_ENABLED * MAXQUOTAS)
483#define DQUOT_SUSPENDED (1 << _DQUOT_SUSPENDED * MAXQUOTAS)
484#define DQUOT_STATE_FLAGS (DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED | \
485 DQUOT_SUSPENDED)
486
487#define DQUOT_STATE_LAST (_DQUOT_STATE_FLAGS * MAXQUOTAS)
488#define DQUOT_QUOTA_SYS_FILE (1 << DQUOT_STATE_LAST)
489
490
491
492
493
494
495#define DQUOT_NEGATIVE_USAGE (1 << (DQUOT_STATE_LAST + 1))
496
497
498#define DQUOT_NOLIST_DIRTY (1 << (DQUOT_STATE_LAST + 2))
499
500static inline unsigned int dquot_state_flag(unsigned int flags, int type)
501{
502 return flags << type;
503}
504
505static inline unsigned int dquot_generic_flag(unsigned int flags, int type)
506{
507 return (flags >> type) & DQUOT_STATE_FLAGS;
508}
509
510
511static __always_inline unsigned dquot_state_types(unsigned flags, unsigned flag)
512{
513 BUILD_BUG_ON_NOT_POWER_OF_2(flag);
514 return (flags / flag) & ((1 << MAXQUOTAS) - 1);
515}
516
517#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
518extern void quota_send_warning(struct kqid qid, dev_t dev,
519 const char warntype);
520#else
521static inline void quota_send_warning(struct kqid qid, dev_t dev,
522 const char warntype)
523{
524 return;
525}
526#endif
527
528struct quota_info {
529 unsigned int flags;
530 struct rw_semaphore dqio_sem;
531 struct inode *files[MAXQUOTAS];
532 struct mem_dqinfo info[MAXQUOTAS];
533 const struct quota_format_ops *ops[MAXQUOTAS];
534};
535
536int register_quota_format(struct quota_format_type *fmt);
537void unregister_quota_format(struct quota_format_type *fmt);
538
539struct quota_module_name {
540 int qm_fmt_id;
541 char *qm_mod_name;
542};
543
544#define INIT_QUOTA_MODULE_NAMES {\
545 {QFMT_VFS_OLD, "quota_v1"},\
546 {QFMT_VFS_V0, "quota_v2"},\
547 {QFMT_VFS_V1, "quota_v2"},\
548 {0, NULL}}
549
550#endif
551