0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032 #ifndef _LINUX_QUOTA_
0033 #define _LINUX_QUOTA_
0034
0035 #include <linux/list.h>
0036 #include <linux/mutex.h>
0037 #include <linux/rwsem.h>
0038 #include <linux/spinlock.h>
0039 #include <linux/wait.h>
0040 #include <linux/percpu_counter.h>
0041
0042 #include <linux/dqblk_xfs.h>
0043 #include <linux/dqblk_v1.h>
0044 #include <linux/dqblk_v2.h>
0045
0046 #include <linux/atomic.h>
0047 #include <linux/uidgid.h>
0048 #include <linux/projid.h>
0049 #include <uapi/linux/quota.h>
0050
0051 #undef USRQUOTA
0052 #undef GRPQUOTA
0053 #undef PRJQUOTA
0054 enum quota_type {
0055 USRQUOTA = 0,
0056 GRPQUOTA = 1,
0057 PRJQUOTA = 2,
0058 };
0059
0060
0061 #define QTYPE_MASK_USR (1 << USRQUOTA)
0062 #define QTYPE_MASK_GRP (1 << GRPQUOTA)
0063 #define QTYPE_MASK_PRJ (1 << PRJQUOTA)
0064
0065 typedef __kernel_uid32_t qid_t;
0066 typedef long long qsize_t;
0067
0068 struct kqid {
0069 union {
0070 kuid_t uid;
0071 kgid_t gid;
0072 kprojid_t projid;
0073 };
0074 enum quota_type type;
0075 };
0076
0077 extern bool qid_eq(struct kqid left, struct kqid right);
0078 extern bool qid_lt(struct kqid left, struct kqid right);
0079 extern qid_t from_kqid(struct user_namespace *to, struct kqid qid);
0080 extern qid_t from_kqid_munged(struct user_namespace *to, struct kqid qid);
0081 extern bool qid_valid(struct kqid qid);
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097 static inline struct kqid make_kqid(struct user_namespace *from,
0098 enum quota_type type, qid_t qid)
0099 {
0100 struct kqid kqid;
0101
0102 kqid.type = type;
0103 switch (type) {
0104 case USRQUOTA:
0105 kqid.uid = make_kuid(from, qid);
0106 break;
0107 case GRPQUOTA:
0108 kqid.gid = make_kgid(from, qid);
0109 break;
0110 case PRJQUOTA:
0111 kqid.projid = make_kprojid(from, qid);
0112 break;
0113 default:
0114 BUG();
0115 }
0116 return kqid;
0117 }
0118
0119
0120
0121
0122
0123
0124
0125 static inline struct kqid make_kqid_invalid(enum quota_type type)
0126 {
0127 struct kqid kqid;
0128
0129 kqid.type = type;
0130 switch (type) {
0131 case USRQUOTA:
0132 kqid.uid = INVALID_UID;
0133 break;
0134 case GRPQUOTA:
0135 kqid.gid = INVALID_GID;
0136 break;
0137 case PRJQUOTA:
0138 kqid.projid = INVALID_PROJID;
0139 break;
0140 default:
0141 BUG();
0142 }
0143 return kqid;
0144 }
0145
0146
0147
0148
0149
0150 static inline struct kqid make_kqid_uid(kuid_t uid)
0151 {
0152 struct kqid kqid;
0153 kqid.type = USRQUOTA;
0154 kqid.uid = uid;
0155 return kqid;
0156 }
0157
0158
0159
0160
0161
0162 static inline struct kqid make_kqid_gid(kgid_t gid)
0163 {
0164 struct kqid kqid;
0165 kqid.type = GRPQUOTA;
0166 kqid.gid = gid;
0167 return kqid;
0168 }
0169
0170
0171
0172
0173
0174 static inline struct kqid make_kqid_projid(kprojid_t projid)
0175 {
0176 struct kqid kqid;
0177 kqid.type = PRJQUOTA;
0178 kqid.projid = projid;
0179 return kqid;
0180 }
0181
0182
0183
0184
0185
0186
0187 static inline bool qid_has_mapping(struct user_namespace *ns, struct kqid qid)
0188 {
0189 return from_kqid(ns, qid) != (qid_t) -1;
0190 }
0191
0192
0193 extern spinlock_t dq_data_lock;
0194
0195
0196
0197 #define DQUOT_INIT_ALLOC max(V1_INIT_ALLOC, V2_INIT_ALLOC)
0198 #define DQUOT_INIT_REWRITE max(V1_INIT_REWRITE, V2_INIT_REWRITE)
0199 #define DQUOT_DEL_ALLOC max(V1_DEL_ALLOC, V2_DEL_ALLOC)
0200 #define DQUOT_DEL_REWRITE max(V1_DEL_REWRITE, V2_DEL_REWRITE)
0201
0202
0203
0204
0205 struct mem_dqblk {
0206 qsize_t dqb_bhardlimit;
0207 qsize_t dqb_bsoftlimit;
0208 qsize_t dqb_curspace;
0209 qsize_t dqb_rsvspace;
0210 qsize_t dqb_ihardlimit;
0211 qsize_t dqb_isoftlimit;
0212 qsize_t dqb_curinodes;
0213 time64_t dqb_btime;
0214 time64_t dqb_itime;
0215 };
0216
0217
0218
0219
0220 struct quota_format_type;
0221
0222 struct mem_dqinfo {
0223 struct quota_format_type *dqi_format;
0224 int dqi_fmt_id;
0225
0226 struct list_head dqi_dirty_list;
0227 unsigned long dqi_flags;
0228 unsigned int dqi_bgrace;
0229 unsigned int dqi_igrace;
0230 qsize_t dqi_max_spc_limit;
0231 qsize_t dqi_max_ino_limit;
0232 void *dqi_priv;
0233 };
0234
0235 struct super_block;
0236
0237
0238 #define DQF_GETINFO_MASK (DQF_ROOT_SQUASH | DQF_SYS_FILE)
0239
0240 #define DQF_SETINFO_MASK DQF_ROOT_SQUASH
0241
0242 enum {
0243 DQF_INFO_DIRTY_B = DQF_PRIVATE,
0244 };
0245 #define DQF_INFO_DIRTY (1 << DQF_INFO_DIRTY_B)
0246
0247 extern void mark_info_dirty(struct super_block *sb, int type);
0248 static inline int info_dirty(struct mem_dqinfo *info)
0249 {
0250 return test_bit(DQF_INFO_DIRTY_B, &info->dqi_flags);
0251 }
0252
0253 enum {
0254 DQST_LOOKUPS,
0255 DQST_DROPS,
0256 DQST_READS,
0257 DQST_WRITES,
0258 DQST_CACHE_HITS,
0259 DQST_ALLOC_DQUOTS,
0260 DQST_FREE_DQUOTS,
0261 DQST_SYNCS,
0262 _DQST_DQSTAT_LAST
0263 };
0264
0265 struct dqstats {
0266 unsigned long stat[_DQST_DQSTAT_LAST];
0267 struct percpu_counter counter[_DQST_DQSTAT_LAST];
0268 };
0269
0270 extern struct dqstats dqstats;
0271
0272 static inline void dqstats_inc(unsigned int type)
0273 {
0274 percpu_counter_inc(&dqstats.counter[type]);
0275 }
0276
0277 static inline void dqstats_dec(unsigned int type)
0278 {
0279 percpu_counter_dec(&dqstats.counter[type]);
0280 }
0281
0282 #define DQ_MOD_B 0
0283 #define DQ_BLKS_B 1
0284 #define DQ_INODES_B 2
0285 #define DQ_FAKE_B 3
0286 #define DQ_READ_B 4
0287 #define DQ_ACTIVE_B 5
0288 #define DQ_LASTSET_B 6
0289
0290
0291
0292
0293
0294 struct dquot {
0295 struct hlist_node dq_hash;
0296 struct list_head dq_inuse;
0297 struct list_head dq_free;
0298 struct list_head dq_dirty;
0299 struct mutex dq_lock;
0300 spinlock_t dq_dqb_lock;
0301 atomic_t dq_count;
0302 struct super_block *dq_sb;
0303 struct kqid dq_id;
0304 loff_t dq_off;
0305 unsigned long dq_flags;
0306 struct mem_dqblk dq_dqb;
0307 };
0308
0309
0310 struct quota_format_ops {
0311 int (*check_quota_file)(struct super_block *sb, int type);
0312 int (*read_file_info)(struct super_block *sb, int type);
0313 int (*write_file_info)(struct super_block *sb, int type);
0314 int (*free_file_info)(struct super_block *sb, int type);
0315 int (*read_dqblk)(struct dquot *dquot);
0316 int (*commit_dqblk)(struct dquot *dquot);
0317 int (*release_dqblk)(struct dquot *dquot);
0318 int (*get_next_id)(struct super_block *sb, struct kqid *qid);
0319 };
0320
0321
0322 struct dquot_operations {
0323 int (*write_dquot) (struct dquot *);
0324 struct dquot *(*alloc_dquot)(struct super_block *, int);
0325 void (*destroy_dquot)(struct dquot *);
0326 int (*acquire_dquot) (struct dquot *);
0327 int (*release_dquot) (struct dquot *);
0328 int (*mark_dirty) (struct dquot *);
0329 int (*write_info) (struct super_block *, int);
0330
0331
0332 qsize_t *(*get_reserved_space) (struct inode *);
0333 int (*get_projid) (struct inode *, kprojid_t *);
0334
0335 int (*get_inode_usage) (struct inode *, qsize_t *);
0336
0337 int (*get_next_id) (struct super_block *sb, struct kqid *qid);
0338 };
0339
0340 struct path;
0341
0342
0343 struct qc_dqblk {
0344 int d_fieldmask;
0345 u64 d_spc_hardlimit;
0346 u64 d_spc_softlimit;
0347 u64 d_ino_hardlimit;
0348 u64 d_ino_softlimit;
0349 u64 d_space;
0350 u64 d_ino_count;
0351 s64 d_ino_timer;
0352
0353 s64 d_spc_timer;
0354 int d_ino_warns;
0355 int d_spc_warns;
0356 u64 d_rt_spc_hardlimit;
0357 u64 d_rt_spc_softlimit;
0358 u64 d_rt_space;
0359 s64 d_rt_spc_timer;
0360 int d_rt_spc_warns;
0361 };
0362
0363
0364
0365
0366
0367 #define QC_INO_SOFT (1<<0)
0368 #define QC_INO_HARD (1<<1)
0369 #define QC_SPC_SOFT (1<<2)
0370 #define QC_SPC_HARD (1<<3)
0371 #define QC_RT_SPC_SOFT (1<<4)
0372 #define QC_RT_SPC_HARD (1<<5)
0373 #define QC_LIMIT_MASK (QC_INO_SOFT | QC_INO_HARD | QC_SPC_SOFT | QC_SPC_HARD | \
0374 QC_RT_SPC_SOFT | QC_RT_SPC_HARD)
0375 #define QC_SPC_TIMER (1<<6)
0376 #define QC_INO_TIMER (1<<7)
0377 #define QC_RT_SPC_TIMER (1<<8)
0378 #define QC_TIMER_MASK (QC_SPC_TIMER | QC_INO_TIMER | QC_RT_SPC_TIMER)
0379 #define QC_SPC_WARNS (1<<9)
0380 #define QC_INO_WARNS (1<<10)
0381 #define QC_RT_SPC_WARNS (1<<11)
0382 #define QC_WARNS_MASK (QC_SPC_WARNS | QC_INO_WARNS | QC_RT_SPC_WARNS)
0383 #define QC_SPACE (1<<12)
0384 #define QC_INO_COUNT (1<<13)
0385 #define QC_RT_SPACE (1<<14)
0386 #define QC_ACCT_MASK (QC_SPACE | QC_INO_COUNT | QC_RT_SPACE)
0387 #define QC_FLAGS (1<<15)
0388
0389 #define QCI_SYSFILE (1 << 0)
0390 #define QCI_ROOT_SQUASH (1 << 1)
0391 #define QCI_ACCT_ENABLED (1 << 2)
0392 #define QCI_LIMITS_ENFORCED (1 << 3)
0393
0394
0395 struct qc_type_state {
0396 unsigned int flags;
0397 unsigned int spc_timelimit;
0398
0399 unsigned int ino_timelimit;
0400 unsigned int rt_spc_timelimit;
0401 unsigned int spc_warnlimit;
0402 unsigned int ino_warnlimit;
0403 unsigned int rt_spc_warnlimit;
0404 unsigned long long ino;
0405 blkcnt_t blocks;
0406 blkcnt_t nextents;
0407 };
0408
0409 struct qc_state {
0410 unsigned int s_incoredqs;
0411 struct qc_type_state s_state[MAXQUOTAS];
0412 };
0413
0414
0415 struct qc_info {
0416 int i_fieldmask;
0417 unsigned int i_flags;
0418 unsigned int i_spc_timelimit;
0419
0420 unsigned int i_ino_timelimit;
0421 unsigned int i_rt_spc_timelimit;
0422 unsigned int i_spc_warnlimit;
0423 unsigned int i_ino_warnlimit;
0424 unsigned int i_rt_spc_warnlimit;
0425 };
0426
0427
0428 struct quotactl_ops {
0429 int (*quota_on)(struct super_block *, int, int, const struct path *);
0430 int (*quota_off)(struct super_block *, int);
0431 int (*quota_enable)(struct super_block *, unsigned int);
0432 int (*quota_disable)(struct super_block *, unsigned int);
0433 int (*quota_sync)(struct super_block *, int);
0434 int (*set_info)(struct super_block *, int, struct qc_info *);
0435 int (*get_dqblk)(struct super_block *, struct kqid, struct qc_dqblk *);
0436 int (*get_nextdqblk)(struct super_block *, struct kqid *,
0437 struct qc_dqblk *);
0438 int (*set_dqblk)(struct super_block *, struct kqid, struct qc_dqblk *);
0439 int (*get_state)(struct super_block *, struct qc_state *);
0440 int (*rm_xquota)(struct super_block *, unsigned int);
0441 };
0442
0443 struct quota_format_type {
0444 int qf_fmt_id;
0445 const struct quota_format_ops *qf_ops;
0446 struct module *qf_owner;
0447 struct quota_format_type *qf_next;
0448 };
0449
0450
0451
0452
0453
0454
0455
0456
0457
0458
0459
0460
0461
0462
0463
0464 enum {
0465 _DQUOT_USAGE_ENABLED = 0,
0466 _DQUOT_LIMITS_ENABLED,
0467 _DQUOT_SUSPENDED,
0468
0469
0470 _DQUOT_STATE_FLAGS
0471 };
0472 #define DQUOT_USAGE_ENABLED (1 << _DQUOT_USAGE_ENABLED * MAXQUOTAS)
0473 #define DQUOT_LIMITS_ENABLED (1 << _DQUOT_LIMITS_ENABLED * MAXQUOTAS)
0474 #define DQUOT_SUSPENDED (1 << _DQUOT_SUSPENDED * MAXQUOTAS)
0475 #define DQUOT_STATE_FLAGS (DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED | \
0476 DQUOT_SUSPENDED)
0477
0478 #define DQUOT_STATE_LAST (_DQUOT_STATE_FLAGS * MAXQUOTAS)
0479 #define DQUOT_QUOTA_SYS_FILE (1 << DQUOT_STATE_LAST)
0480
0481
0482
0483
0484
0485
0486 #define DQUOT_NEGATIVE_USAGE (1 << (DQUOT_STATE_LAST + 1))
0487
0488
0489 #define DQUOT_NOLIST_DIRTY (1 << (DQUOT_STATE_LAST + 2))
0490
0491 static inline unsigned int dquot_state_flag(unsigned int flags, int type)
0492 {
0493 return flags << type;
0494 }
0495
0496 static inline unsigned int dquot_generic_flag(unsigned int flags, int type)
0497 {
0498 return (flags >> type) & DQUOT_STATE_FLAGS;
0499 }
0500
0501
0502 static __always_inline unsigned dquot_state_types(unsigned flags, unsigned flag)
0503 {
0504 BUILD_BUG_ON_NOT_POWER_OF_2(flag);
0505 return (flags / flag) & ((1 << MAXQUOTAS) - 1);
0506 }
0507
0508 #ifdef CONFIG_QUOTA_NETLINK_INTERFACE
0509 extern void quota_send_warning(struct kqid qid, dev_t dev,
0510 const char warntype);
0511 #else
0512 static inline void quota_send_warning(struct kqid qid, dev_t dev,
0513 const char warntype)
0514 {
0515 return;
0516 }
0517 #endif
0518
0519 struct quota_info {
0520 unsigned int flags;
0521 struct rw_semaphore dqio_sem;
0522 struct inode *files[MAXQUOTAS];
0523 struct mem_dqinfo info[MAXQUOTAS];
0524 const struct quota_format_ops *ops[MAXQUOTAS];
0525 };
0526
0527 int register_quota_format(struct quota_format_type *fmt);
0528 void unregister_quota_format(struct quota_format_type *fmt);
0529
0530 struct quota_module_name {
0531 int qm_fmt_id;
0532 char *qm_mod_name;
0533 };
0534
0535 #define INIT_QUOTA_MODULE_NAMES {\
0536 {QFMT_VFS_OLD, "quota_v1"},\
0537 {QFMT_VFS_V0, "quota_v2"},\
0538 {QFMT_VFS_V1, "quota_v2"},\
0539 {0, NULL}}
0540
0541 #endif