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, QUOTA_ALL_BIT);
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 */
53 #error "cannot have more than 32 quota types to fit in qtype_bits"
56 #define QUOTA_USR_BIT (1 << USRQUOTA)
57 #define QUOTA_GRP_BIT (1 << GRPQUOTA)
58 #define QUOTA_ALL_BIT (QUOTA_USR_BIT | QUOTA_GRP_BIT)
60 typedef struct quota_ctx *quota_ctx_t;
65 struct dict_t *quota_dict[MAXQUOTAS];
66 struct quota_handle *quota_file[MAXQUOTAS];
70 * Definitions of magics and versions of current quota files
72 #define INITQMAGICS {\
73 0xd9c01f11, /* USRQUOTA */\
74 0xd9c01927 /* GRPQUOTA */\
77 /* Size of blocks in which are counted size limits in generic utility parts */
78 #define QUOTABLOCK_BITS 10
79 #define QUOTABLOCK_SIZE (1 << QUOTABLOCK_BITS)
80 #define toqb(x) (((x) + QUOTABLOCK_SIZE - 1) >> QUOTABLOCK_BITS)
82 /* Quota format type IDs */
83 #define QFMT_VFS_OLD 1
88 * The following constants define the default amount of time given a user
89 * before the soft limits are treated as hard limits (usually resulting
90 * in an allocation failure). The timer is started when the user crosses
91 * their soft limit, it is reset when they go below their soft limit.
93 #define MAX_IQ_TIME 604800 /* (7*24*60*60) 1 week */
94 #define MAX_DQ_TIME 604800 /* (7*24*60*60) 1 week */
96 #define IOFL_INFODIRTY 0x01 /* Did info change? */
100 /* Generic information about quotafile */
102 time_t dqi_bgrace; /* Block grace time for given quotafile */
103 time_t dqi_igrace; /* Inode grace time for given quotafile */
105 struct v2_mem_dqinfo v2_mdqi;
106 } u; /* Format specific info about quotafile */
115 /* Structure for one opened quota file */
116 struct quota_handle {
117 enum quota_type qh_type; /* Type of quotafile */
118 int qh_fmt; /* Quotafile format */
120 int qh_io_flags; /* IO flags for file */
121 struct quota_file qh_qf;
122 unsigned int (*e2fs_read)(struct quota_file *qf, ext2_loff_t offset,
123 void *buf, unsigned int size);
124 unsigned int (*e2fs_write)(struct quota_file *qf, ext2_loff_t offset,
125 void *buf, unsigned int size);
126 struct quotafile_ops *qh_ops; /* Operations on quotafile */
127 struct util_dqinfo qh_info; /* Generic quotafile info */
130 /* Utility quota block */
132 qsize_t dqb_ihardlimit;
133 qsize_t dqb_isoftlimit;
134 qsize_t dqb_curinodes;
135 qsize_t dqb_bhardlimit;
136 qsize_t dqb_bsoftlimit;
137 qsize_t dqb_curspace;
141 struct v2_mem_dqblk v2_mdqb;
142 } u; /* Format specific dquot information */
145 /* Structure for one loaded quota */
147 struct dquot *dq_next; /* Pointer to next dquot in the list */
148 qid_t dq_id; /* ID dquot belongs to */
149 int dq_flags; /* Some flags for utils */
150 struct quota_handle *dq_h; /* Handle of quotafile for this dquot */
151 struct util_dqblk dq_dqb; /* Parsed data of dquot */
154 #define DQF_SEEN 0x0001
156 /* Structure of quotafile operations */
157 struct quotafile_ops {
158 /* Check whether quotafile is in our format */
159 int (*check_file) (struct quota_handle *h, int type, int fmt);
161 int (*init_io) (struct quota_handle *h);
162 /* Create new quotafile */
163 int (*new_io) (struct quota_handle *h);
164 /* Write all changes and close quotafile */
165 int (*end_io) (struct quota_handle *h);
166 /* Write info about quotafile */
167 int (*write_info) (struct quota_handle *h);
168 /* Read dquot into memory */
169 struct dquot *(*read_dquot) (struct quota_handle *h, qid_t id);
170 /* Write given dquot to disk */
171 int (*commit_dquot) (struct dquot *dquot);
172 /* Scan quotafile and call callback on every structure */
173 int (*scan_dquots) (struct quota_handle *h,
174 int (*process_dquot) (struct dquot *dquot,
177 /* Function to print format specific file information */
178 int (*report) (struct quota_handle *h, int verbose);
181 /* This might go into a special header file but that sounds a bit silly... */
182 extern struct quotafile_ops quotafile_ops_meta;
184 /* Open existing quotafile of given type (and verify its format) on given
186 errcode_t quota_file_open(quota_ctx_t qctx, struct quota_handle *h,
187 ext2_ino_t qf_ino, enum quota_type type,
191 /* Create new quotafile of specified format on given filesystem */
192 errcode_t quota_file_create(struct quota_handle *h, ext2_filsys fs,
193 enum quota_type qtype, int fmt);
195 /* Close quotafile */
196 errcode_t quota_file_close(quota_ctx_t qctx, struct quota_handle *h);
198 /* Get empty quota structure */
199 struct dquot *get_empty_dquot(void);
201 errcode_t quota_inode_truncate(ext2_filsys fs, ext2_ino_t ino);
203 const char *quota_type2name(enum quota_type qtype);
204 ext2_ino_t quota_type2inum(enum quota_type qtype, struct ext2_super_block *);
206 void update_grace_times(struct dquot *q);
208 /* size for the buffer returned by quota_get_qf_name(); must be greater
209 than maxlen of extensions[] and fmtnames[] (plus 2) found in quotaio.c */
210 #define QUOTA_NAME_LEN 16
212 const char *quota_get_qf_name(enum quota_type, int fmt, char *buf);
215 errcode_t quota_init_context(quota_ctx_t *qctx, ext2_filsys fs,
216 unsigned int qtype_bits);
217 void quota_data_inodes(quota_ctx_t qctx, struct ext2_inode *inode, ext2_ino_t ino,
219 void quota_data_add(quota_ctx_t qctx, struct ext2_inode *inode, ext2_ino_t ino,
221 void quota_data_sub(quota_ctx_t qctx, struct ext2_inode *inode, ext2_ino_t ino,
223 errcode_t quota_write_inode(quota_ctx_t qctx, enum quota_type qtype);
224 errcode_t quota_update_limits(quota_ctx_t qctx, ext2_ino_t qf_ino,
225 enum quota_type type);
226 errcode_t quota_compute_usage(quota_ctx_t qctx);
227 void quota_release_context(quota_ctx_t *qctx);
228 errcode_t quota_remove_inode(ext2_filsys fs, enum quota_type qtype);
229 int quota_file_exists(ext2_filsys fs, enum quota_type qtype);
230 void quota_set_sb_inum(ext2_filsys fs, ext2_ino_t ino, enum quota_type qtype);
231 errcode_t quota_compare_and_update(quota_ctx_t qctx, enum quota_type qtype,
232 int *usage_inconsistent);
233 int parse_quota_opts(const char *opts, int (*func)(), void *data);
236 * Return pointer to reserved inode field in superblock for given quota type.
238 * This allows the caller to get or set the quota inode by type without the
239 * need for the quota array to be contiguous in the superbock.
241 static inline ext2_ino_t *quota_sb_inump(struct ext2_super_block *sb,
242 enum quota_type qtype)
246 return &sb->s_usr_quota_inum;
248 return &sb->s_grp_quota_inum;
256 #endif /* GUARD_QUOTAIO_H */