Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 1982, 1986 Regents of the University of California. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * This code is derived from software contributed to Berkeley by |
| 6 | * Robert Elz at The University of Melbourne. |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in the |
| 15 | * documentation and/or other materials provided with the distribution. |
| 16 | * 3. Neither the name of the University nor the names of its contributors |
| 17 | * may be used to endorse or promote products derived from this software |
| 18 | * without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 30 | * SUCH DAMAGE. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #ifndef _LINUX_QUOTA_ |
| 33 | #define _LINUX_QUOTA_ |
| 34 | |
Matthew Wilcox | 8b91de2 | 2008-02-26 09:53:20 -0500 | [diff] [blame] | 35 | #include <linux/list.h> |
David Woodhouse | 0409d3a | 2006-04-25 14:52:13 +0100 | [diff] [blame] | 36 | #include <linux/mutex.h> |
Matthew Wilcox | 8b91de2 | 2008-02-26 09:53:20 -0500 | [diff] [blame] | 37 | #include <linux/rwsem.h> |
| 38 | #include <linux/spinlock.h> |
| 39 | #include <linux/wait.h> |
Dmitry Monakhov | f32764b | 2010-05-26 23:21:58 +0200 | [diff] [blame] | 40 | #include <linux/percpu_counter.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
| 42 | #include <linux/dqblk_xfs.h> |
| 43 | #include <linux/dqblk_v1.h> |
| 44 | #include <linux/dqblk_v2.h> |
| 45 | |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 46 | #include <linux/atomic.h> |
Eric W. Biederman | e8a3e47 | 2012-09-16 01:11:45 -0700 | [diff] [blame] | 47 | #include <linux/uidgid.h> |
| 48 | #include <linux/projid.h> |
David Howells | 607ca46 | 2012-10-13 10:46:48 +0100 | [diff] [blame] | 49 | #include <uapi/linux/quota.h> |
Eric W. Biederman | e8a3e47 | 2012-09-16 01:11:45 -0700 | [diff] [blame] | 50 | |
| 51 | #undef USRQUOTA |
| 52 | #undef GRPQUOTA |
Li Xi | 847aac6 | 2015-03-19 04:04:53 +0900 | [diff] [blame] | 53 | #undef PRJQUOTA |
Eric W. Biederman | e8a3e47 | 2012-09-16 01:11:45 -0700 | [diff] [blame] | 54 | enum quota_type { |
| 55 | USRQUOTA = 0, /* element used for user quotas */ |
| 56 | GRPQUOTA = 1, /* element used for group quotas */ |
| 57 | PRJQUOTA = 2, /* element used for project quotas */ |
| 58 | }; |
Matthew Wilcox | 8b91de2 | 2008-02-26 09:53:20 -0500 | [diff] [blame] | 59 | |
Jan Kara | 2c5f648 | 2014-09-30 10:43:09 +0200 | [diff] [blame] | 60 | /* Masks for quota types when used as a bitmask */ |
| 61 | #define QTYPE_MASK_USR (1 << USRQUOTA) |
| 62 | #define QTYPE_MASK_GRP (1 << GRPQUOTA) |
| 63 | #define QTYPE_MASK_PRJ (1 << PRJQUOTA) |
| 64 | |
Jan Kara | 74abb98 | 2008-07-25 01:46:51 -0700 | [diff] [blame] | 65 | typedef __kernel_uid32_t qid_t; /* Type in which we store ids in memory */ |
Jan Kara | db49d2d | 2008-10-01 18:21:39 +0200 | [diff] [blame] | 66 | typedef long long qsize_t; /* Type in which we store sizes */ |
Jan Kara | 74abb98 | 2008-07-25 01:46:51 -0700 | [diff] [blame] | 67 | |
Eric W. Biederman | e8a3e47 | 2012-09-16 01:11:45 -0700 | [diff] [blame] | 68 | struct kqid { /* Type in which we store the quota identifier */ |
| 69 | union { |
| 70 | kuid_t uid; |
| 71 | kgid_t gid; |
| 72 | kprojid_t projid; |
| 73 | }; |
| 74 | enum quota_type type; /* USRQUOTA (uid) or GRPQUOTA (gid) or PRJQUOTA (projid) */ |
| 75 | }; |
| 76 | |
| 77 | extern bool qid_eq(struct kqid left, struct kqid right); |
| 78 | extern bool qid_lt(struct kqid left, struct kqid right); |
| 79 | extern qid_t from_kqid(struct user_namespace *to, struct kqid qid); |
| 80 | extern qid_t from_kqid_munged(struct user_namespace *to, struct kqid qid); |
| 81 | extern bool qid_valid(struct kqid qid); |
| 82 | |
| 83 | /** |
| 84 | * make_kqid - Map a user-namespace, type, qid tuple into a kqid. |
| 85 | * @from: User namespace that the qid is in |
| 86 | * @type: The type of quota |
| 87 | * @qid: Quota identifier |
| 88 | * |
| 89 | * Maps a user-namespace, type qid tuple into a kernel internal |
| 90 | * kqid, and returns that kqid. |
| 91 | * |
| 92 | * When there is no mapping defined for the user-namespace, type, |
| 93 | * qid tuple an invalid kqid is returned. Callers are expected to |
| 94 | * test for and handle handle invalid kqids being returned. |
| 95 | * Invalid kqids may be tested for using qid_valid(). |
| 96 | */ |
| 97 | static 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 | * make_kqid_invalid - Explicitly make an invalid kqid |
| 121 | * @type: The type of quota identifier |
| 122 | * |
| 123 | * Returns an invalid kqid with the specified type. |
| 124 | */ |
| 125 | static 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 | * make_kqid_uid - Make a kqid from a kuid |
| 148 | * @uid: The kuid to make the quota identifier from |
| 149 | */ |
| 150 | static 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 | * make_kqid_gid - Make a kqid from a kgid |
| 160 | * @gid: The kgid to make the quota identifier from |
| 161 | */ |
| 162 | static 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 | * make_kqid_projid - Make a kqid from a projid |
| 172 | * @projid: The kprojid to make the quota identifier from |
| 173 | */ |
| 174 | static 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 | |
Eric W. Biederman | d49d376 | 2016-06-30 16:31:01 -0500 | [diff] [blame] | 182 | /** |
| 183 | * qid_has_mapping - Report if a qid maps into a user namespace. |
| 184 | * @ns: The user namespace to see if a value maps into. |
| 185 | * @qid: The kernel internal quota identifier to test. |
| 186 | */ |
| 187 | static inline bool qid_has_mapping(struct user_namespace *ns, struct kqid qid) |
| 188 | { |
| 189 | return from_kqid(ns, qid) != (qid_t) -1; |
| 190 | } |
| 191 | |
Eric W. Biederman | e8a3e47 | 2012-09-16 01:11:45 -0700 | [diff] [blame] | 192 | |
Mike Frysinger | acd64b7 | 2007-05-08 00:31:51 -0700 | [diff] [blame] | 193 | extern spinlock_t dq_data_lock; |
| 194 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | /* Maximal numbers of writes for quota operation (insert/delete/update) |
Jan Kara | 4e5117b | 2005-06-23 22:01:03 -0700 | [diff] [blame] | 196 | * (over VFS all formats) */ |
| 197 | #define DQUOT_INIT_ALLOC max(V1_INIT_ALLOC, V2_INIT_ALLOC) |
| 198 | #define DQUOT_INIT_REWRITE max(V1_INIT_REWRITE, V2_INIT_REWRITE) |
| 199 | #define DQUOT_DEL_ALLOC max(V1_DEL_ALLOC, V2_DEL_ALLOC) |
| 200 | #define DQUOT_DEL_REWRITE max(V1_DEL_REWRITE, V2_DEL_REWRITE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | |
| 202 | /* |
| 203 | * Data for one user/group kept in memory |
| 204 | */ |
| 205 | struct mem_dqblk { |
Jan Kara | 1209546 | 2008-08-20 14:45:12 +0200 | [diff] [blame] | 206 | qsize_t dqb_bhardlimit; /* absolute limit on disk blks alloc */ |
| 207 | qsize_t dqb_bsoftlimit; /* preferred limit on disk blks */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | qsize_t dqb_curspace; /* current used space */ |
Mingming Cao | f18df22 | 2009-01-13 16:43:09 +0100 | [diff] [blame] | 209 | qsize_t dqb_rsvspace; /* current reserved space for delalloc*/ |
Jan Kara | 1209546 | 2008-08-20 14:45:12 +0200 | [diff] [blame] | 210 | qsize_t dqb_ihardlimit; /* absolute limit on allocated inodes */ |
| 211 | qsize_t dqb_isoftlimit; /* preferred inode limit */ |
| 212 | qsize_t dqb_curinodes; /* current # allocated inodes */ |
Arnd Bergmann | e008bb6 | 2016-06-17 22:03:16 +0200 | [diff] [blame] | 213 | time64_t dqb_btime; /* time limit for excessive disk use */ |
| 214 | time64_t dqb_itime; /* time limit for excessive inode use */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | }; |
| 216 | |
| 217 | /* |
| 218 | * Data for one quotafile kept in memory |
| 219 | */ |
| 220 | struct quota_format_type; |
| 221 | |
| 222 | struct mem_dqinfo { |
| 223 | struct quota_format_type *dqi_format; |
Jan Kara | 0ff5af8 | 2008-04-28 02:14:33 -0700 | [diff] [blame] | 224 | int dqi_fmt_id; /* Id of the dqi_format - used when turning |
| 225 | * quotas on after remount RW */ |
Jan Kara | 6c83fd5 | 2017-08-21 14:06:46 +0200 | [diff] [blame] | 226 | struct list_head dqi_dirty_list; /* List of dirty dquots [dq_list_lock] */ |
| 227 | unsigned long dqi_flags; /* DFQ_ flags [dq_data_lock] */ |
| 228 | unsigned int dqi_bgrace; /* Space grace time [dq_data_lock] */ |
| 229 | unsigned int dqi_igrace; /* Inode grace time [dq_data_lock] */ |
| 230 | qsize_t dqi_max_spc_limit; /* Maximum space limit [static] */ |
| 231 | qsize_t dqi_max_ino_limit; /* Maximum inode limit [static] */ |
Jan Kara | e3d4d56 | 2008-10-02 18:44:14 +0200 | [diff] [blame] | 232 | void *dqi_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | }; |
| 234 | |
| 235 | struct super_block; |
| 236 | |
Jan Kara | 9c45101 | 2014-11-19 09:21:58 +0100 | [diff] [blame] | 237 | /* Mask for flags passed to userspace */ |
| 238 | #define DQF_GETINFO_MASK (DQF_ROOT_SQUASH | DQF_SYS_FILE) |
| 239 | /* Mask for flags modifiable from userspace */ |
| 240 | #define DQF_SETINFO_MASK DQF_ROOT_SQUASH |
| 241 | |
| 242 | enum { |
| 243 | DQF_INFO_DIRTY_B = DQF_PRIVATE, |
| 244 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | #define DQF_INFO_DIRTY (1 << DQF_INFO_DIRTY_B) /* Is info dirty? */ |
| 246 | |
| 247 | extern void mark_info_dirty(struct super_block *sb, int type); |
Jan Kara | 03b0634 | 2008-07-25 01:46:52 -0700 | [diff] [blame] | 248 | static inline int info_dirty(struct mem_dqinfo *info) |
| 249 | { |
| 250 | return test_bit(DQF_INFO_DIRTY_B, &info->dqi_flags); |
| 251 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | |
Dmitry Monakhov | dde9588 | 2010-04-26 20:03:33 +0400 | [diff] [blame] | 253 | enum { |
| 254 | DQST_LOOKUPS, |
| 255 | DQST_DROPS, |
| 256 | DQST_READS, |
| 257 | DQST_WRITES, |
| 258 | DQST_CACHE_HITS, |
| 259 | DQST_ALLOC_DQUOTS, |
| 260 | DQST_FREE_DQUOTS, |
| 261 | DQST_SYNCS, |
| 262 | _DQST_DQSTAT_LAST |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | }; |
| 264 | |
Dmitry Monakhov | dde9588 | 2010-04-26 20:03:33 +0400 | [diff] [blame] | 265 | struct dqstats { |
| 266 | int stat[_DQST_DQSTAT_LAST]; |
Dmitry Monakhov | f32764b | 2010-05-26 23:21:58 +0200 | [diff] [blame] | 267 | struct percpu_counter counter[_DQST_DQSTAT_LAST]; |
Dmitry Monakhov | dde9588 | 2010-04-26 20:03:33 +0400 | [diff] [blame] | 268 | }; |
| 269 | |
| 270 | extern struct dqstats *dqstats_pcpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | extern struct dqstats dqstats; |
| 272 | |
Dmitry Monakhov | dde9588 | 2010-04-26 20:03:33 +0400 | [diff] [blame] | 273 | static inline void dqstats_inc(unsigned int type) |
| 274 | { |
Dmitry Monakhov | f32764b | 2010-05-26 23:21:58 +0200 | [diff] [blame] | 275 | percpu_counter_inc(&dqstats.counter[type]); |
Dmitry Monakhov | dde9588 | 2010-04-26 20:03:33 +0400 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | static inline void dqstats_dec(unsigned int type) |
| 279 | { |
Dmitry Monakhov | f32764b | 2010-05-26 23:21:58 +0200 | [diff] [blame] | 280 | percpu_counter_dec(&dqstats.counter[type]); |
Dmitry Monakhov | dde9588 | 2010-04-26 20:03:33 +0400 | [diff] [blame] | 281 | } |
| 282 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | #define DQ_MOD_B 0 /* dquot modified since read */ |
| 284 | #define DQ_BLKS_B 1 /* uid/gid has been warned about blk limit */ |
| 285 | #define DQ_INODES_B 2 /* uid/gid has been warned about inode limit */ |
| 286 | #define DQ_FAKE_B 3 /* no limits only usage */ |
| 287 | #define DQ_READ_B 4 /* dquot was read into memory */ |
| 288 | #define DQ_ACTIVE_B 5 /* dquot is active (dquot_release not called) */ |
Jan Kara | 4d59bce | 2008-10-02 16:48:10 +0200 | [diff] [blame] | 289 | #define DQ_LASTSET_B 6 /* Following 6 bits (see QIF_) are reserved\ |
| 290 | * for the mask of entries set via SETQUOTA\ |
| 291 | * quotactl. They are set under dq_data_lock\ |
| 292 | * and the quota format handling dquot can\ |
| 293 | * clear them when it sees fit. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | |
| 295 | struct dquot { |
Jan Kara | 6c83fd5 | 2017-08-21 14:06:46 +0200 | [diff] [blame] | 296 | struct hlist_node dq_hash; /* Hash list in memory [dq_list_lock] */ |
| 297 | struct list_head dq_inuse; /* List of all quotas [dq_list_lock] */ |
| 298 | struct list_head dq_free; /* Free list element [dq_list_lock] */ |
| 299 | struct list_head dq_dirty; /* List of dirty dquots [dq_list_lock] */ |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 300 | struct mutex dq_lock; /* dquot IO lock */ |
Jan Kara | 7b9ca4c | 2017-08-07 13:19:50 +0200 | [diff] [blame] | 301 | spinlock_t dq_dqb_lock; /* Lock protecting dq_dqb changes */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | atomic_t dq_count; /* Use count */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | struct super_block *dq_sb; /* superblock this applies to */ |
Eric W. Biederman | 4c376dc | 2012-09-16 03:56:19 -0700 | [diff] [blame] | 304 | struct kqid dq_id; /* ID this applies to (uid, gid, projid) */ |
Jan Kara | 6c83fd5 | 2017-08-21 14:06:46 +0200 | [diff] [blame] | 305 | loff_t dq_off; /* Offset of dquot on disk [dq_lock, stable once set] */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | unsigned long dq_flags; /* See DQ_* */ |
Jan Kara | 7b9ca4c | 2017-08-07 13:19:50 +0200 | [diff] [blame] | 307 | struct mem_dqblk dq_dqb; /* Diskquota usage [dq_dqb_lock] */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | }; |
| 309 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | /* Operations which must be implemented by each quota format */ |
| 311 | struct quota_format_ops { |
| 312 | int (*check_quota_file)(struct super_block *sb, int type); /* Detect whether file is in our format */ |
| 313 | int (*read_file_info)(struct super_block *sb, int type); /* Read main info about file - called on quotaon() */ |
| 314 | int (*write_file_info)(struct super_block *sb, int type); /* Write main info about file */ |
| 315 | int (*free_file_info)(struct super_block *sb, int type); /* Called on quotaoff() */ |
| 316 | int (*read_dqblk)(struct dquot *dquot); /* Read structure for one user */ |
| 317 | int (*commit_dqblk)(struct dquot *dquot); /* Write structure for one user */ |
| 318 | int (*release_dqblk)(struct dquot *dquot); /* Called when last reference to dquot is being dropped */ |
Jan Kara | be6257b | 2016-01-25 19:24:50 +0100 | [diff] [blame] | 319 | int (*get_next_id)(struct super_block *sb, struct kqid *qid); /* Get next ID with existing structure in the quota file */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | }; |
| 321 | |
| 322 | /* Operations working with dquots */ |
| 323 | struct dquot_operations { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | int (*write_dquot) (struct dquot *); /* Ordinary dquot write */ |
Jan Kara | 74f783a | 2008-08-19 14:51:22 +0200 | [diff] [blame] | 325 | struct dquot *(*alloc_dquot)(struct super_block *, int); /* Allocate memory for new dquot */ |
| 326 | void (*destroy_dquot)(struct dquot *); /* Free memory for dquot */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | int (*acquire_dquot) (struct dquot *); /* Quota is going to be created on disk */ |
| 328 | int (*release_dquot) (struct dquot *); /* Quota is going to be deleted from disk */ |
| 329 | int (*mark_dirty) (struct dquot *); /* Dquot is marked dirty */ |
| 330 | int (*write_info) (struct super_block *, int); /* Write of quota "superblock" */ |
Dmitry Monakhov | fd8fbfc | 2009-12-14 15:21:13 +0300 | [diff] [blame] | 331 | /* get reserved quota for delayed alloc, value returned is managed by |
| 332 | * quota code only */ |
| 333 | qsize_t *(*get_reserved_space) (struct inode *); |
Li Xi | 847aac6 | 2015-03-19 04:04:53 +0900 | [diff] [blame] | 334 | int (*get_projid) (struct inode *, kprojid_t *);/* Get project ID */ |
Tahsin Erdogan | 7a9ca53 | 2017-06-22 11:46:48 -0400 | [diff] [blame] | 335 | /* Get number of inodes that were charged for a given inode */ |
| 336 | int (*get_inode_usage) (struct inode *, qsize_t *); |
Jan Kara | be6257b | 2016-01-25 19:24:50 +0100 | [diff] [blame] | 337 | /* Get next ID with active quota structure */ |
| 338 | int (*get_next_id) (struct super_block *sb, struct kqid *qid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | }; |
| 340 | |
Jan Kara | f00c9e4 | 2010-09-15 17:38:58 +0200 | [diff] [blame] | 341 | struct path; |
| 342 | |
Jan Kara | 14bf61f | 2014-10-09 16:03:13 +0200 | [diff] [blame] | 343 | /* Structure for communicating via ->get_dqblk() & ->set_dqblk() */ |
| 344 | struct qc_dqblk { |
| 345 | int d_fieldmask; /* mask of fields to change in ->set_dqblk() */ |
| 346 | u64 d_spc_hardlimit; /* absolute limit on used space */ |
| 347 | u64 d_spc_softlimit; /* preferred limit on used space */ |
| 348 | u64 d_ino_hardlimit; /* maximum # allocated inodes */ |
| 349 | u64 d_ino_softlimit; /* preferred inode limit */ |
| 350 | u64 d_space; /* Space owned by the user */ |
| 351 | u64 d_ino_count; /* # inodes owned by the user */ |
| 352 | s64 d_ino_timer; /* zero if within inode limits */ |
| 353 | /* if not, we refuse service */ |
| 354 | s64 d_spc_timer; /* similar to above; for space */ |
| 355 | int d_ino_warns; /* # warnings issued wrt num inodes */ |
| 356 | int d_spc_warns; /* # warnings issued wrt used space */ |
| 357 | u64 d_rt_spc_hardlimit; /* absolute limit on realtime space */ |
| 358 | u64 d_rt_spc_softlimit; /* preferred limit on RT space */ |
| 359 | u64 d_rt_space; /* realtime space owned */ |
| 360 | s64 d_rt_spc_timer; /* similar to above; for RT space */ |
| 361 | int d_rt_spc_warns; /* # warnings issued wrt RT space */ |
| 362 | }; |
| 363 | |
Jan Kara | 5eacb2a | 2014-12-16 12:03:51 +0100 | [diff] [blame] | 364 | /* |
| 365 | * Field specifiers for ->set_dqblk() in struct qc_dqblk and also for |
| 366 | * ->set_info() in struct qc_info |
| 367 | */ |
Jan Kara | 14bf61f | 2014-10-09 16:03:13 +0200 | [diff] [blame] | 368 | #define QC_INO_SOFT (1<<0) |
| 369 | #define QC_INO_HARD (1<<1) |
| 370 | #define QC_SPC_SOFT (1<<2) |
| 371 | #define QC_SPC_HARD (1<<3) |
| 372 | #define QC_RT_SPC_SOFT (1<<4) |
| 373 | #define QC_RT_SPC_HARD (1<<5) |
| 374 | #define QC_LIMIT_MASK (QC_INO_SOFT | QC_INO_HARD | QC_SPC_SOFT | QC_SPC_HARD | \ |
| 375 | QC_RT_SPC_SOFT | QC_RT_SPC_HARD) |
| 376 | #define QC_SPC_TIMER (1<<6) |
| 377 | #define QC_INO_TIMER (1<<7) |
| 378 | #define QC_RT_SPC_TIMER (1<<8) |
| 379 | #define QC_TIMER_MASK (QC_SPC_TIMER | QC_INO_TIMER | QC_RT_SPC_TIMER) |
| 380 | #define QC_SPC_WARNS (1<<9) |
| 381 | #define QC_INO_WARNS (1<<10) |
| 382 | #define QC_RT_SPC_WARNS (1<<11) |
| 383 | #define QC_WARNS_MASK (QC_SPC_WARNS | QC_INO_WARNS | QC_RT_SPC_WARNS) |
| 384 | #define QC_SPACE (1<<12) |
| 385 | #define QC_INO_COUNT (1<<13) |
| 386 | #define QC_RT_SPACE (1<<14) |
| 387 | #define QC_ACCT_MASK (QC_SPACE | QC_INO_COUNT | QC_RT_SPACE) |
Jan Kara | 5eacb2a | 2014-12-16 12:03:51 +0100 | [diff] [blame] | 388 | #define QC_FLAGS (1<<15) |
Jan Kara | 14bf61f | 2014-10-09 16:03:13 +0200 | [diff] [blame] | 389 | |
Jan Kara | 0a240339 | 2014-11-19 00:42:09 +0100 | [diff] [blame] | 390 | #define QCI_SYSFILE (1 << 0) /* Quota file is hidden from userspace */ |
| 391 | #define QCI_ROOT_SQUASH (1 << 1) /* Root squash turned on */ |
| 392 | #define QCI_ACCT_ENABLED (1 << 2) /* Quota accounting enabled */ |
| 393 | #define QCI_LIMITS_ENFORCED (1 << 3) /* Quota limits enforced */ |
| 394 | |
| 395 | /* Structures for communicating via ->get_state */ |
| 396 | struct qc_type_state { |
| 397 | unsigned int flags; /* Flags QCI_* */ |
| 398 | unsigned int spc_timelimit; /* Time after which space softlimit is |
| 399 | * enforced */ |
| 400 | unsigned int ino_timelimit; /* Ditto for inode softlimit */ |
| 401 | unsigned int rt_spc_timelimit; /* Ditto for real-time space */ |
| 402 | unsigned int spc_warnlimit; /* Limit for number of space warnings */ |
| 403 | unsigned int ino_warnlimit; /* Ditto for inodes */ |
| 404 | unsigned int rt_spc_warnlimit; /* Ditto for real-time space */ |
| 405 | unsigned long long ino; /* Inode number of quota file */ |
| 406 | blkcnt_t blocks; /* Number of 512-byte blocks in the file */ |
| 407 | blkcnt_t nextents; /* Number of extents in the file */ |
| 408 | }; |
| 409 | |
| 410 | struct qc_state { |
| 411 | unsigned int s_incoredqs; /* Number of dquots in core */ |
| 412 | /* |
| 413 | * Per quota type information. The array should really have |
| 414 | * max(MAXQUOTAS, XQM_MAXQUOTAS) entries. BUILD_BUG_ON in |
| 415 | * quota_getinfo() makes sure XQM_MAXQUOTAS is large enough. Once VFS |
| 416 | * supports project quotas, this can be changed to MAXQUOTAS |
| 417 | */ |
| 418 | struct qc_type_state s_state[XQM_MAXQUOTAS]; |
| 419 | }; |
| 420 | |
Jan Kara | 5eacb2a | 2014-12-16 12:03:51 +0100 | [diff] [blame] | 421 | /* Structure for communicating via ->set_info */ |
| 422 | struct qc_info { |
| 423 | int i_fieldmask; /* mask of fields to change in ->set_info() */ |
| 424 | unsigned int i_flags; /* Flags QCI_* */ |
| 425 | unsigned int i_spc_timelimit; /* Time after which space softlimit is |
| 426 | * enforced */ |
| 427 | unsigned int i_ino_timelimit; /* Ditto for inode softlimit */ |
| 428 | unsigned int i_rt_spc_timelimit;/* Ditto for real-time space */ |
| 429 | unsigned int i_spc_warnlimit; /* Limit for number of space warnings */ |
| 430 | unsigned int i_ino_warnlimit; /* Limit for number of inode warnings */ |
| 431 | unsigned int i_rt_spc_warnlimit; /* Ditto for real-time space */ |
| 432 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | |
| 434 | /* Operations handling requests from userspace */ |
| 435 | struct quotactl_ops { |
Al Viro | 8c54ca9 | 2016-11-20 19:49:34 -0500 | [diff] [blame] | 436 | int (*quota_on)(struct super_block *, int, int, const struct path *); |
Christoph Hellwig | 307ae18 | 2010-05-19 07:16:43 -0400 | [diff] [blame] | 437 | int (*quota_off)(struct super_block *, int); |
Jan Kara | 38e478c | 2014-10-08 15:56:21 +0200 | [diff] [blame] | 438 | int (*quota_enable)(struct super_block *, unsigned int); |
| 439 | int (*quota_disable)(struct super_block *, unsigned int); |
Jan Kara | ceed172 | 2012-07-03 16:45:28 +0200 | [diff] [blame] | 440 | int (*quota_sync)(struct super_block *, int); |
Jan Kara | 5eacb2a | 2014-12-16 12:03:51 +0100 | [diff] [blame] | 441 | int (*set_info)(struct super_block *, int, struct qc_info *); |
Jan Kara | 14bf61f | 2014-10-09 16:03:13 +0200 | [diff] [blame] | 442 | int (*get_dqblk)(struct super_block *, struct kqid, struct qc_dqblk *); |
Eric Sandeen | 8b37524 | 2016-02-08 11:21:50 +1100 | [diff] [blame] | 443 | int (*get_nextdqblk)(struct super_block *, struct kqid *, |
| 444 | struct qc_dqblk *); |
Jan Kara | 14bf61f | 2014-10-09 16:03:13 +0200 | [diff] [blame] | 445 | int (*set_dqblk)(struct super_block *, struct kqid, struct qc_dqblk *); |
Jan Kara | 0a240339 | 2014-11-19 00:42:09 +0100 | [diff] [blame] | 446 | int (*get_state)(struct super_block *, struct qc_state *); |
Eric Sandeen | 9da93f9 | 2014-05-05 17:25:50 +1000 | [diff] [blame] | 447 | int (*rm_xquota)(struct super_block *, unsigned int); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | }; |
| 449 | |
| 450 | struct quota_format_type { |
| 451 | int qf_fmt_id; /* Quota format id */ |
Alexey Dobriyan | 1472da5 | 2009-10-16 15:26:03 +0400 | [diff] [blame] | 452 | const struct quota_format_ops *qf_ops; /* Operations of format */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | struct module *qf_owner; /* Module implementing quota format */ |
| 454 | struct quota_format_type *qf_next; |
| 455 | }; |
| 456 | |
Konstantin Khlebnikov | 7819702 | 2015-02-12 19:08:16 +0300 | [diff] [blame] | 457 | /** |
| 458 | * Quota state flags - they actually come in two flavors - for users and groups. |
| 459 | * |
| 460 | * Actual typed flags layout: |
| 461 | * USRQUOTA GRPQUOTA |
| 462 | * DQUOT_USAGE_ENABLED 0x0001 0x0002 |
| 463 | * DQUOT_LIMITS_ENABLED 0x0004 0x0008 |
| 464 | * DQUOT_SUSPENDED 0x0010 0x0020 |
| 465 | * |
| 466 | * Following bits are used for non-typed flags: |
| 467 | * DQUOT_QUOTA_SYS_FILE 0x0040 |
| 468 | * DQUOT_NEGATIVE_USAGE 0x0080 |
| 469 | */ |
Jan Kara | f55abc0 | 2008-08-20 17:50:32 +0200 | [diff] [blame] | 470 | enum { |
| 471 | _DQUOT_USAGE_ENABLED = 0, /* Track disk usage for users */ |
| 472 | _DQUOT_LIMITS_ENABLED, /* Enforce quota limits for users */ |
| 473 | _DQUOT_SUSPENDED, /* User diskquotas are off, but |
Jan Kara | 0ff5af8 | 2008-04-28 02:14:33 -0700 | [diff] [blame] | 474 | * we have necessary info in |
| 475 | * memory to turn them on */ |
Jan Kara | f55abc0 | 2008-08-20 17:50:32 +0200 | [diff] [blame] | 476 | _DQUOT_STATE_FLAGS |
| 477 | }; |
Konstantin Khlebnikov | 7819702 | 2015-02-12 19:08:16 +0300 | [diff] [blame] | 478 | #define DQUOT_USAGE_ENABLED (1 << _DQUOT_USAGE_ENABLED * MAXQUOTAS) |
| 479 | #define DQUOT_LIMITS_ENABLED (1 << _DQUOT_LIMITS_ENABLED * MAXQUOTAS) |
| 480 | #define DQUOT_SUSPENDED (1 << _DQUOT_SUSPENDED * MAXQUOTAS) |
Jan Kara | f55abc0 | 2008-08-20 17:50:32 +0200 | [diff] [blame] | 481 | #define DQUOT_STATE_FLAGS (DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED | \ |
| 482 | DQUOT_SUSPENDED) |
Jan Kara | ca785ec | 2008-09-30 17:53:37 +0200 | [diff] [blame] | 483 | /* Other quota flags */ |
Dmitry Monakhov | ad1e6e8 | 2010-02-16 08:31:49 +0300 | [diff] [blame] | 484 | #define DQUOT_STATE_LAST (_DQUOT_STATE_FLAGS * MAXQUOTAS) |
| 485 | #define DQUOT_QUOTA_SYS_FILE (1 << DQUOT_STATE_LAST) |
| 486 | /* Quota file is a special |
Jan Kara | ca785ec | 2008-09-30 17:53:37 +0200 | [diff] [blame] | 487 | * system file and user cannot |
| 488 | * touch it. Filesystem is |
| 489 | * responsible for setting |
| 490 | * S_NOQUOTA, S_NOATIME flags |
| 491 | */ |
Dmitry Monakhov | ad1e6e8 | 2010-02-16 08:31:49 +0300 | [diff] [blame] | 492 | #define DQUOT_NEGATIVE_USAGE (1 << (DQUOT_STATE_LAST + 1)) |
| 493 | /* Allow negative quota usage */ |
Jan Kara | 834057b | 2017-08-03 11:18:23 +0200 | [diff] [blame] | 494 | /* Do not track dirty dquots in a list */ |
| 495 | #define DQUOT_NOLIST_DIRTY (1 << (DQUOT_STATE_LAST + 2)) |
| 496 | |
Jan Kara | f55abc0 | 2008-08-20 17:50:32 +0200 | [diff] [blame] | 497 | static inline unsigned int dquot_state_flag(unsigned int flags, int type) |
| 498 | { |
Konstantin Khlebnikov | 7819702 | 2015-02-12 19:08:16 +0300 | [diff] [blame] | 499 | return flags << type; |
Jan Kara | f55abc0 | 2008-08-20 17:50:32 +0200 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | static inline unsigned int dquot_generic_flag(unsigned int flags, int type) |
| 503 | { |
Konstantin Khlebnikov | 7819702 | 2015-02-12 19:08:16 +0300 | [diff] [blame] | 504 | return (flags >> type) & DQUOT_STATE_FLAGS; |
| 505 | } |
| 506 | |
| 507 | /* Bitmap of quota types where flag is set in flags */ |
| 508 | static __always_inline unsigned dquot_state_types(unsigned flags, unsigned flag) |
| 509 | { |
| 510 | BUILD_BUG_ON_NOT_POWER_OF_2(flag); |
| 511 | return (flags / flag) & ((1 << MAXQUOTAS) - 1); |
Jan Kara | f55abc0 | 2008-08-20 17:50:32 +0200 | [diff] [blame] | 512 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | |
Steven Whitehouse | 86e931a | 2009-09-28 12:35:17 +0100 | [diff] [blame] | 514 | #ifdef CONFIG_QUOTA_NETLINK_INTERFACE |
Eric W. Biederman | 431f197 | 2012-09-16 02:32:43 -0700 | [diff] [blame] | 515 | extern void quota_send_warning(struct kqid qid, dev_t dev, |
Steven Whitehouse | 86e931a | 2009-09-28 12:35:17 +0100 | [diff] [blame] | 516 | const char warntype); |
| 517 | #else |
Eric W. Biederman | 431f197 | 2012-09-16 02:32:43 -0700 | [diff] [blame] | 518 | static inline void quota_send_warning(struct kqid qid, dev_t dev, |
Steven Whitehouse | 86e931a | 2009-09-28 12:35:17 +0100 | [diff] [blame] | 519 | const char warntype) |
| 520 | { |
| 521 | return; |
| 522 | } |
| 523 | #endif /* CONFIG_QUOTA_NETLINK_INTERFACE */ |
| 524 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | struct quota_info { |
| 526 | unsigned int flags; /* Flags for diskquotas on this device */ |
Jan Kara | bc8230e | 2017-06-08 14:39:48 +0200 | [diff] [blame] | 527 | struct rw_semaphore dqio_sem; /* Lock quota file while I/O in progress */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | struct inode *files[MAXQUOTAS]; /* inodes of quotafiles */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | struct mem_dqinfo info[MAXQUOTAS]; /* Information for each quota type */ |
Alexey Dobriyan | 1472da5 | 2009-10-16 15:26:03 +0400 | [diff] [blame] | 530 | const struct quota_format_ops *ops[MAXQUOTAS]; /* Operations for each type */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | }; |
| 532 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | int register_quota_format(struct quota_format_type *fmt); |
| 534 | void unregister_quota_format(struct quota_format_type *fmt); |
| 535 | |
| 536 | struct quota_module_name { |
| 537 | int qm_fmt_id; |
| 538 | char *qm_mod_name; |
| 539 | }; |
| 540 | |
| 541 | #define INIT_QUOTA_MODULE_NAMES {\ |
| 542 | {QFMT_VFS_OLD, "quota_v1"},\ |
| 543 | {QFMT_VFS_V0, "quota_v2"},\ |
Theodore Ts'o | c3ad83d | 2013-01-24 23:24:56 -0500 | [diff] [blame] | 544 | {QFMT_VFS_V1, "quota_v2"},\ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | {0, NULL}} |
| 546 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | #endif /* _QUOTA_ */ |