• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /** quotaio.h
2  *
3  * Interface to the quota library.
4  *
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
8  * in memory.
9  * The typical way to use the quota library is as follows:
10  * {
11  *	quota_ctx_t qctx;
12  *
13  *	quota_init_context(&qctx, fs, QUOTA_ALL_BIT);
14  *	{
15  *		quota_compute_usage(qctx);
16  *		AND/OR
17  *		quota_data_add/quota_data_sub/quota_data_inodes();
18  *	}
19  *	quota_write_inode(qctx, USRQUOTA);
20  *	quota_write_inode(qctx, GRPQUOTA);
21  *	quota_release_context(&qctx);
22  * }
23  *
24  * This initial version does not support reading the quota files. This support
25  * will be added in near future.
26  *
27  * Aditya Kali <adityakali@google.com>
28  * Header of IO operations for quota utilities
29  *
30  * Jan Kara <jack@suse.cz>
31  */
32 
33 #ifndef GUARD_QUOTAIO_H
34 #define GUARD_QUOTAIO_H
35 
36 #include <limits.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 
40 #include "ext2fs/ext2_fs.h"
41 #include "ext2fs/ext2fs.h"
42 #include "dqblk_v2.h"
43 
44 typedef int64_t qsize_t;	/* Type in which we store size limitations */
45 
46 enum quota_type {
47 	USRQUOTA = 0,
48 	GRPQUOTA = 1,
49 	PRJQUOTA = 2,
50 	MAXQUOTAS = 3,
51 };
52 
53 #if MAXQUOTAS > 32
54 #error "cannot have more than 32 quota types to fit in qtype_bits"
55 #endif
56 
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)
61 
62 typedef struct quota_ctx *quota_ctx_t;
63 struct dict_t;
64 
65 struct quota_ctx {
66 	ext2_filsys	fs;
67 	struct dict_t	*quota_dict[MAXQUOTAS];
68 	struct quota_handle *quota_file[MAXQUOTAS];
69 };
70 
71 /*
72  * Definitions of magics and versions of current quota files
73  */
74 #define INITQMAGICS {\
75 	0xd9c01f11,	/* USRQUOTA */\
76 	0xd9c01927,	/* GRPQUOTA */\
77 	0xd9c03f14	/* PRJQUOTA */\
78 }
79 
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)
84 
85 /* Quota format type IDs */
86 #define	QFMT_VFS_OLD 1
87 #define	QFMT_VFS_V0 2
88 #define	QFMT_VFS_V1 4
89 
90 /*
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.
95  */
96 #define MAX_IQ_TIME  604800	/* (7*24*60*60) 1 week */
97 #define MAX_DQ_TIME  604800	/* (7*24*60*60) 1 week */
98 
99 #define IOFL_INFODIRTY	0x01	/* Did info change? */
100 
101 struct quotafile_ops;
102 
103 /* Generic information about quotafile */
104 struct util_dqinfo {
105 	time_t dqi_bgrace;	/* Block grace time for given quotafile */
106 	time_t dqi_igrace;	/* Inode grace time for given quotafile */
107 	union {
108 		struct v2_mem_dqinfo v2_mdqi;
109 	} u;			/* Format specific info about quotafile */
110 };
111 
112 struct quota_file {
113 	ext2_filsys fs;
114 	ext2_ino_t ino;
115 	ext2_file_t e2_file;
116 };
117 
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 */
122 	int qh_file_flags;
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 */
131 };
132 
133 /* Utility quota block */
134 struct util_dqblk {
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;
141 	time_t dqb_btime;
142 	time_t dqb_itime;
143 	union {
144 		struct v2_mem_dqblk v2_mdqb;
145 	} u;			/* Format specific dquot information */
146 };
147 
148 /* Structure for one loaded quota */
149 struct dquot {
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 */
155 };
156 
157 #define DQF_SEEN	0x0001
158 
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);
163 	/* Open quotafile */
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,
178 						  void *data),
179 			    void *data);
180 	/* Function to print format specific file information */
181 	int (*report) (struct quota_handle *h, int verbose);
182 };
183 
184 /* This might go into a special header file but that sounds a bit silly... */
185 extern struct quotafile_ops quotafile_ops_meta;
186 
187 /* Open existing quotafile of given type (and verify its format) on given
188  * filesystem. */
189 errcode_t quota_file_open(quota_ctx_t qctx, struct quota_handle *h,
190 			  ext2_ino_t qf_ino, enum quota_type type,
191 			  int fmt, int flags);
192 
193 
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);
197 
198 /* Close quotafile */
199 errcode_t quota_file_close(quota_ctx_t qctx, struct quota_handle *h);
200 
201 /* Get empty quota structure */
202 struct dquot *get_empty_dquot(void);
203 
204 errcode_t quota_inode_truncate(ext2_filsys fs, ext2_ino_t ino);
205 
206 const char *quota_type2name(enum quota_type qtype);
207 ext2_ino_t quota_type2inum(enum quota_type qtype, struct ext2_super_block *);
208 
209 void update_grace_times(struct dquot *q);
210 
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
214 
215 const char *quota_get_qf_name(enum quota_type, int fmt, char *buf);
216 
217 /* In mkquota.c */
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 errcode_t quota_update_limits(quota_ctx_t qctx, ext2_ino_t qf_ino,
228 			      enum quota_type type);
229 errcode_t quota_compute_usage(quota_ctx_t qctx);
230 void quota_release_context(quota_ctx_t *qctx);
231 errcode_t quota_remove_inode(ext2_filsys fs, enum quota_type qtype);
232 int quota_file_exists(ext2_filsys fs, enum quota_type qtype);
233 void quota_set_sb_inum(ext2_filsys fs, ext2_ino_t ino, enum quota_type qtype);
234 errcode_t quota_compare_and_update(quota_ctx_t qctx, enum quota_type qtype,
235 				   int *usage_inconsistent);
236 int parse_quota_opts(const char *opts, int (*func)(char *));
237 
238 /* parse_qtype.c */
239 int parse_quota_types(const char *in_str, unsigned int *qtype_bits,
240 		      char **err_token);
241 
242 /*
243  * Return pointer to reserved inode field in superblock for given quota type.
244  *
245  * This allows the caller to get or set the quota inode by type without the
246  * need for the quota array to be contiguous in the superblock.
247  */
quota_sb_inump(struct ext2_super_block * sb,enum quota_type qtype)248 static inline ext2_ino_t *quota_sb_inump(struct ext2_super_block *sb,
249 					 enum quota_type qtype)
250 {
251 	switch (qtype) {
252 	case USRQUOTA:
253 		return &sb->s_usr_quota_inum;
254 	case GRPQUOTA:
255 		return &sb->s_grp_quota_inum;
256 	case PRJQUOTA:
257 		return &sb->s_prj_quota_inum;
258 	default:
259 		return NULL;
260 	}
261 
262 	return NULL;
263 }
264 
265 #endif /* GUARD_QUOTAIO_H */
266