3 * Interface to the quota library.
5 * The quota library provides interface for creating and updating the quota
6 * files and the ext4 superblock fields. It supports the new VFS_V1 quota
7 * format. The quota library also provides support for keeping track of quotas
9 * The typical way to use the quota library is as follows:
13 * quota_init_context(&qctx, fs, QUOTA_ALL_BIT);
15 * quota_compute_usage(qctx);
17 * quota_data_add/quota_data_sub/quota_data_inodes();
19 * quota_write_inode(qctx, USRQUOTA);
20 * quota_write_inode(qctx, GRPQUOTA);
21 * quota_release_context(&qctx);
24 * This initial version does not support reading the quota files. This support
25 * will be added in near future.
27 * Aditya Kali <adityakali@google.com>
28 * Header of IO operations for quota utilities
30 * Jan Kara <jack@suse.cz>
33 #ifndef GUARD_QUOTAIO_H
34 #define GUARD_QUOTAIO_H
37 #include <sys/types.h>
40 #include "ext2fs/ext2_fs.h"
41 #include "ext2fs/ext2fs.h"
44 typedef int64_t qsize_t; /* Type in which we store size limitations */
54 #error "cannot have more than 32 quota types to fit in qtype_bits"
57 #define QUOTA_USR_BIT (1 << USRQUOTA)
58 #define QUOTA_GRP_BIT (1 << GRPQUOTA)
59 #define QUOTA_PRJ_BIT (1 << PRJQUOTA)
60 #define QUOTA_ALL_BIT (QUOTA_USR_BIT | QUOTA_GRP_BIT | QUOTA_PRJ_BIT)
62 typedef struct quota_ctx *quota_ctx_t;
67 struct dict_t *quota_dict[MAXQUOTAS];
68 struct quota_handle *quota_file[MAXQUOTAS];
72 * Definitions of magics and versions of current quota files
74 #define INITQMAGICS {\
75 0xd9c01f11, /* USRQUOTA */\
76 0xd9c01927, /* GRPQUOTA */\
77 0xd9c03f14 /* PRJQUOTA */\
80 /* Size of blocks in which are counted size limits in generic utility parts */
81 #define QUOTABLOCK_BITS 10
82 #define QUOTABLOCK_SIZE (1 << QUOTABLOCK_BITS)
83 #define toqb(x) (((x) + QUOTABLOCK_SIZE - 1) >> QUOTABLOCK_BITS)
85 /* Quota format type IDs */
86 #define QFMT_VFS_OLD 1
91 * The following constants define the default amount of time given a user
92 * before the soft limits are treated as hard limits (usually resulting
93 * in an allocation failure). The timer is started when the user crosses
94 * their soft limit, it is reset when they go below their soft limit.
96 #define MAX_IQ_TIME 604800 /* (7*24*60*60) 1 week */
97 #define MAX_DQ_TIME 604800 /* (7*24*60*60) 1 week */
99 #define IOFL_INFODIRTY 0x01 /* Did info change? */
101 struct quotafile_ops;
103 /* Generic information about quotafile */
105 time_t dqi_bgrace; /* Block grace time for given quotafile */
106 time_t dqi_igrace; /* Inode grace time for given quotafile */
108 struct v2_mem_dqinfo v2_mdqi;
109 } u; /* Format specific info about quotafile */
118 /* Structure for one opened quota file */
119 struct quota_handle {
120 enum quota_type qh_type; /* Type of quotafile */
121 int qh_fmt; /* Quotafile format */
123 int qh_io_flags; /* IO flags for file */
124 struct quota_file qh_qf;
125 unsigned int (*e2fs_read)(struct quota_file *qf, ext2_loff_t offset,
126 void *buf, unsigned int size);
127 unsigned int (*e2fs_write)(struct quota_file *qf, ext2_loff_t offset,
128 void *buf, unsigned int size);
129 struct quotafile_ops *qh_ops; /* Operations on quotafile */
130 struct util_dqinfo qh_info; /* Generic quotafile info */
133 /* Utility quota block */
135 qsize_t dqb_ihardlimit;
136 qsize_t dqb_isoftlimit;
137 qsize_t dqb_curinodes;
138 qsize_t dqb_bhardlimit;
139 qsize_t dqb_bsoftlimit;
140 qsize_t dqb_curspace;
144 struct v2_mem_dqblk v2_mdqb;
145 } u; /* Format specific dquot information */
148 /* Structure for one loaded quota */
150 struct dquot *dq_next; /* Pointer to next dquot in the list */
151 qid_t dq_id; /* ID dquot belongs to */
152 int dq_flags; /* Some flags for utils */
153 struct quota_handle *dq_h; /* Handle of quotafile for this dquot */
154 struct util_dqblk dq_dqb; /* Parsed data of dquot */
157 #define DQF_SEEN 0x0001
159 /* Structure of quotafile operations */
160 struct quotafile_ops {
161 /* Check whether quotafile is in our format */
162 int (*check_file) (struct quota_handle *h, int type, int fmt);
164 int (*init_io) (struct quota_handle *h);
165 /* Create new quotafile */
166 int (*new_io) (struct quota_handle *h);
167 /* Write all changes and close quotafile */
168 int (*end_io) (struct quota_handle *h);
169 /* Write info about quotafile */
170 int (*write_info) (struct quota_handle *h);
171 /* Read dquot into memory */
172 struct dquot *(*read_dquot) (struct quota_handle *h, qid_t id);
173 /* Write given dquot to disk */
174 int (*commit_dquot) (struct dquot *dquot);
175 /* Scan quotafile and call callback on every structure */
176 int (*scan_dquots) (struct quota_handle *h,
177 int (*process_dquot) (struct dquot *dquot,
180 /* Function to print format specific file information */
181 int (*report) (struct quota_handle *h, int verbose);
184 /* This might go into a special header file but that sounds a bit silly... */
185 extern struct quotafile_ops quotafile_ops_meta;
187 /* Open existing quotafile of given type (and verify its format) on given
189 errcode_t quota_file_open(quota_ctx_t qctx, struct quota_handle *h,
190 ext2_ino_t qf_ino, enum quota_type type,
194 /* Create new quotafile of specified format on given filesystem */
195 errcode_t quota_file_create(struct quota_handle *h, ext2_filsys fs,
196 enum quota_type qtype, int fmt);
198 /* Close quotafile */
199 errcode_t quota_file_close(quota_ctx_t qctx, struct quota_handle *h);
201 /* Get empty quota structure */
202 struct dquot *get_empty_dquot(void);
204 errcode_t quota_inode_truncate(ext2_filsys fs, ext2_ino_t ino);
206 const char *quota_type2name(enum quota_type qtype);
207 ext2_ino_t quota_type2inum(enum quota_type qtype, struct ext2_super_block *);
209 void update_grace_times(struct dquot *q);
211 /* size for the buffer returned by quota_get_qf_name(); must be greater
212 than maxlen of extensions[] and fmtnames[] (plus 2) found in quotaio.c */
213 #define QUOTA_NAME_LEN 16
215 const char *quota_get_qf_name(enum quota_type, int fmt, char *buf);
218 errcode_t quota_init_context(quota_ctx_t *qctx, ext2_filsys fs,
219 unsigned int qtype_bits);
220 void quota_data_inodes(quota_ctx_t qctx, struct ext2_inode_large *inode,
221 ext2_ino_t ino, int adjust);
222 void quota_data_add(quota_ctx_t qctx, struct ext2_inode_large *inode,
223 ext2_ino_t ino, qsize_t space);
224 void quota_data_sub(quota_ctx_t qctx, struct ext2_inode_large *inode,
225 ext2_ino_t ino, qsize_t space);
226 errcode_t quota_write_inode(quota_ctx_t qctx, enum quota_type qtype);
227 /* Flags for quota_read_all_dquots() */
228 #define QREAD_USAGE 0x01
229 #define QREAD_LIMITS 0x02
230 errcode_t quota_read_all_dquots(quota_ctx_t qctx, ext2_ino_t qf_ino,
231 enum quota_type type, unsigned int flags);
232 errcode_t quota_compute_usage(quota_ctx_t qctx);
233 void quota_release_context(quota_ctx_t *qctx);
234 errcode_t quota_remove_inode(ext2_filsys fs, enum quota_type qtype);
235 int quota_file_exists(ext2_filsys fs, enum quota_type qtype);
236 void quota_set_sb_inum(ext2_filsys fs, ext2_ino_t ino, enum quota_type qtype);
237 errcode_t quota_compare_and_update(quota_ctx_t qctx, enum quota_type qtype,
238 int *usage_inconsistent);
239 int parse_quota_opts(const char *opts, int (*func)(char *));
242 int parse_quota_types(const char *in_str, unsigned int *qtype_bits,
246 * Return pointer to reserved inode field in superblock for given quota type.
248 * This allows the caller to get or set the quota inode by type without the
249 * need for the quota array to be contiguous in the superblock.
251 static inline ext2_ino_t *quota_sb_inump(struct ext2_super_block *sb,
252 enum quota_type qtype)
256 return &sb->s_usr_quota_inum;
258 return &sb->s_grp_quota_inum;
260 return &sb->s_prj_quota_inum;
268 #endif /* GUARD_QUOTAIO_H */