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