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"
43 #include "support/dict.h"
45 typedef int64_t qsize_t; /* Type in which we store size limitations */
55 #error "cannot have more than 32 quota types to fit in qtype_bits"
58 #define QUOTA_USR_BIT (1 << USRQUOTA)
59 #define QUOTA_GRP_BIT (1 << GRPQUOTA)
60 #define QUOTA_PRJ_BIT (1 << PRJQUOTA)
61 #define QUOTA_ALL_BIT (QUOTA_USR_BIT | QUOTA_GRP_BIT | QUOTA_PRJ_BIT)
63 typedef struct quota_ctx *quota_ctx_t;
68 struct dict_t *quota_dict[MAXQUOTAS];
69 struct quota_handle *quota_file[MAXQUOTAS];
73 * Definitions of magics and versions of current quota files
75 #define INITQMAGICS {\
76 0xd9c01f11, /* USRQUOTA */\
77 0xd9c01927, /* GRPQUOTA */\
78 0xd9c03f14 /* PRJQUOTA */\
81 /* Size of blocks in which are counted size limits in generic utility parts */
82 #define QUOTABLOCK_BITS 10
83 #define QUOTABLOCK_SIZE (1 << QUOTABLOCK_BITS)
84 #define toqb(x) (((x) + QUOTABLOCK_SIZE - 1) >> QUOTABLOCK_BITS)
86 /* Quota format type IDs */
87 #define QFMT_VFS_OLD 1
92 * The following constants define the default amount of time given a user
93 * before the soft limits are treated as hard limits (usually resulting
94 * in an allocation failure). The timer is started when the user crosses
95 * their soft limit, it is reset when they go below their soft limit.
97 #define MAX_IQ_TIME 604800 /* (7*24*60*60) 1 week */
98 #define MAX_DQ_TIME 604800 /* (7*24*60*60) 1 week */
100 #define IOFL_INFODIRTY 0x01 /* Did info change? */
102 struct quotafile_ops;
104 /* Generic information about quotafile */
106 time_t dqi_bgrace; /* Block grace time for given quotafile */
107 time_t dqi_igrace; /* Inode grace time for given quotafile */
109 struct v2_mem_dqinfo v2_mdqi;
110 } u; /* Format specific info about quotafile */
119 /* Structure for one opened quota file */
120 struct quota_handle {
121 enum quota_type qh_type; /* Type of quotafile */
122 int qh_fmt; /* Quotafile format */
124 int qh_io_flags; /* IO flags for file */
125 struct quota_file qh_qf;
126 unsigned int (*e2fs_read)(struct quota_file *qf, ext2_loff_t offset,
127 void *buf, unsigned int size);
128 unsigned int (*e2fs_write)(struct quota_file *qf, ext2_loff_t offset,
129 void *buf, unsigned int size);
130 struct quotafile_ops *qh_ops; /* Operations on quotafile */
131 struct util_dqinfo qh_info; /* Generic quotafile info */
134 /* Utility quota block */
136 qsize_t dqb_ihardlimit;
137 qsize_t dqb_isoftlimit;
138 qsize_t dqb_curinodes;
139 qsize_t dqb_bhardlimit;
140 qsize_t dqb_bsoftlimit;
141 qsize_t dqb_curspace;
145 struct v2_mem_dqblk v2_mdqb;
146 } u; /* Format specific dquot information */
149 /* Structure for one loaded quota */
151 struct dquot *dq_next; /* Pointer to next dquot in the list */
152 qid_t dq_id; /* ID dquot belongs to */
153 int dq_flags; /* Some flags for utils */
154 struct quota_handle *dq_h; /* Handle of quotafile for this dquot */
155 struct util_dqblk dq_dqb; /* Parsed data of dquot */
158 #define DQF_SEEN 0x0001
160 /* Structure of quotafile operations */
161 struct quotafile_ops {
162 /* Check whether quotafile is in our format */
163 int (*check_file) (struct quota_handle *h, int type, int fmt);
165 int (*init_io) (struct quota_handle *h);
166 /* Create new quotafile */
167 int (*new_io) (struct quota_handle *h);
168 /* Write all changes and close quotafile */
169 int (*end_io) (struct quota_handle *h);
170 /* Write info about quotafile */
171 int (*write_info) (struct quota_handle *h);
172 /* Read dquot into memory */
173 struct dquot *(*read_dquot) (struct quota_handle *h, qid_t id);
174 /* Write given dquot to disk */
175 int (*commit_dquot) (struct dquot *dquot);
176 /* Scan quotafile and call callback on every structure */
177 int (*scan_dquots) (struct quota_handle *h,
178 int (*process_dquot) (struct dquot *dquot,
181 /* Function to print format specific file information */
182 int (*report) (struct quota_handle *h, int verbose);
185 /* This might go into a special header file but that sounds a bit silly... */
186 extern struct quotafile_ops quotafile_ops_meta;
188 /* Open existing quotafile of given type (and verify its format) on given
190 errcode_t quota_file_open(quota_ctx_t qctx, struct quota_handle *h,
191 ext2_ino_t qf_ino, enum quota_type type,
195 /* Create new quotafile of specified format on given filesystem */
196 errcode_t quota_file_create(struct quota_handle *h, ext2_filsys fs,
197 enum quota_type qtype, int fmt);
199 /* Close quotafile */
200 errcode_t quota_file_close(quota_ctx_t qctx, struct quota_handle *h);
202 /* Get empty quota structure */
203 struct dquot *get_empty_dquot(void);
205 errcode_t quota_inode_truncate(ext2_filsys fs, ext2_ino_t ino);
207 const char *quota_type2name(enum quota_type qtype);
208 ext2_ino_t quota_type2inum(enum quota_type qtype, struct ext2_super_block *);
210 void update_grace_times(struct dquot *q);
212 /* size for the buffer returned by quota_get_qf_name(); must be greater
213 than maxlen of extensions[] and fmtnames[] (plus 2) found in quotaio.c */
214 #define QUOTA_NAME_LEN 16
216 const char *quota_get_qf_name(enum quota_type, int fmt, char *buf);
219 errcode_t quota_init_context(quota_ctx_t *qctx, ext2_filsys fs,
220 unsigned int qtype_bits);
221 void quota_data_inodes(quota_ctx_t qctx, struct ext2_inode_large *inode,
222 ext2_ino_t ino, int adjust);
223 void quota_data_add(quota_ctx_t qctx, struct ext2_inode_large *inode,
224 ext2_ino_t ino, qsize_t space);
225 void quota_data_sub(quota_ctx_t qctx, struct ext2_inode_large *inode,
226 ext2_ino_t ino, qsize_t space);
227 errcode_t quota_write_inode(quota_ctx_t qctx, enum quota_type qtype);
228 /* Flags for quota_read_all_dquots() */
229 #define QREAD_USAGE 0x01
230 #define QREAD_LIMITS 0x02
231 errcode_t quota_read_all_dquots(quota_ctx_t qctx, ext2_ino_t qf_ino,
232 enum quota_type type, unsigned int flags);
233 errcode_t quota_compute_usage(quota_ctx_t qctx);
234 void quota_release_context(quota_ctx_t *qctx);
235 errcode_t quota_remove_inode(ext2_filsys fs, enum quota_type qtype);
236 int quota_file_exists(ext2_filsys fs, enum quota_type qtype);
237 void quota_set_sb_inum(ext2_filsys fs, ext2_ino_t ino, enum quota_type qtype);
238 errcode_t quota_compare_and_update(quota_ctx_t qctx, enum quota_type qtype,
239 int *usage_inconsistent);
240 errcode_t quota_merge_and_update_usage(quota_ctx_t dest_qctx,
241 quota_ctx_t src_qctx);
242 int parse_quota_opts(const char *opts, int (*func)(char *));
245 int parse_quota_types(const char *in_str, unsigned int *qtype_bits,
249 * Return pointer to reserved inode field in superblock for given quota type.
251 * This allows the caller to get or set the quota inode by type without the
252 * need for the quota array to be contiguous in the superblock.
254 static inline ext2_ino_t *quota_sb_inump(struct ext2_super_block *sb,
255 enum quota_type qtype)
259 return &sb->s_usr_quota_inum;
261 return &sb->s_grp_quota_inum;
263 return &sb->s_prj_quota_inum;
271 #endif /* GUARD_QUOTAIO_H */