quotaio_v2.c revision d1154eb460efe588eaed3d439c1caaca149fa362
1/*
2 * Implementation of new quotafile format
3 *
4 * Jan Kara <jack@suse.cz> - sponsored by SuSE CR
5 */
6
7#include "config.h"
8#include <sys/types.h>
9#include <errno.h>
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13#include <unistd.h>
14#include <asm/byteorder.h>
15
16#include "common.h"
17#include "quotaio_v2.h"
18#include "dqblk_v2.h"
19#include "quotaio.h"
20#include "quotaio_tree.h"
21
22typedef char *dqbuf_t;
23
24static int v2_check_file(struct quota_handle *h, int type, int fmt);
25static int v2_init_io(struct quota_handle *h);
26static int v2_new_io(struct quota_handle *h);
27static int v2_write_info(struct quota_handle *h);
28static struct dquot *v2_read_dquot(struct quota_handle *h, qid_t id);
29static int v2_commit_dquot(struct dquot *dquot, int flags);
30static int v2_scan_dquots(struct quota_handle *h,
31			  int (*process_dquot) (struct dquot *dquot,
32						char *dqname));
33static int v2_report(struct quota_handle *h, int verbose);
34
35struct quotafile_ops quotafile_ops_2 = {
36check_file:	v2_check_file,
37init_io:	v2_init_io,
38new_io:		v2_new_io,
39write_info:	v2_write_info,
40read_dquot:	v2_read_dquot,
41commit_dquot:	v2_commit_dquot,
42scan_dquots:	v2_scan_dquots,
43report:	v2_report
44};
45
46#define getdqbuf() smalloc(V2_DQBLKSIZE)
47#define freedqbuf(buf) free(buf)
48
49/*
50 * Copy dquot from disk to memory
51 */
52static void v2r1_disk2memdqblk(struct dquot *dquot, void *dp)
53{
54	struct util_dqblk *m = &dquot->dq_dqb;
55	struct v2r1_disk_dqblk *d = dp, empty;
56
57	dquot->dq_id = __le32_to_cpu(d->dqb_id);
58	m->dqb_ihardlimit = __le64_to_cpu(d->dqb_ihardlimit);
59	m->dqb_isoftlimit = __le64_to_cpu(d->dqb_isoftlimit);
60	m->dqb_bhardlimit = __le64_to_cpu(d->dqb_bhardlimit);
61	m->dqb_bsoftlimit = __le64_to_cpu(d->dqb_bsoftlimit);
62	m->dqb_curinodes = __le64_to_cpu(d->dqb_curinodes);
63	m->dqb_curspace = __le64_to_cpu(d->dqb_curspace);
64	m->dqb_itime = __le64_to_cpu(d->dqb_itime);
65	m->dqb_btime = __le64_to_cpu(d->dqb_btime);
66
67	memset(&empty, 0, sizeof(struct v2r1_disk_dqblk));
68	empty.dqb_itime = __cpu_to_le64(1);
69	if (!memcmp(&empty, dp, sizeof(struct v2r1_disk_dqblk)))
70		m->dqb_itime = 0;
71}
72
73/*
74 * Copy dquot from memory to disk
75 */
76static void v2r1_mem2diskdqblk(void *dp, struct dquot *dquot)
77{
78	struct util_dqblk *m = &dquot->dq_dqb;
79	struct v2r1_disk_dqblk *d = dp;
80
81	d->dqb_ihardlimit = __cpu_to_le64(m->dqb_ihardlimit);
82	d->dqb_isoftlimit = __cpu_to_le64(m->dqb_isoftlimit);
83	d->dqb_bhardlimit = __cpu_to_le64(m->dqb_bhardlimit);
84	d->dqb_bsoftlimit = __cpu_to_le64(m->dqb_bsoftlimit);
85	d->dqb_curinodes = __cpu_to_le64(m->dqb_curinodes);
86	d->dqb_curspace = __cpu_to_le64(m->dqb_curspace);
87	d->dqb_itime = __cpu_to_le64(m->dqb_itime);
88	d->dqb_btime = __cpu_to_le64(m->dqb_btime);
89	d->dqb_id = __cpu_to_le32(dquot->dq_id);
90	if (qtree_entry_unused(&dquot->dq_h->qh_info.u.v2_mdqi.dqi_qtree, dp))
91		d->dqb_itime = __cpu_to_le64(1);
92}
93
94static int v2r1_is_id(void *dp, struct dquot *dquot)
95{
96	struct v2r1_disk_dqblk *d = dp;
97	struct qtree_mem_dqinfo *info =
98			&dquot->dq_h->qh_info.u.v2_mdqi.dqi_qtree;
99
100	if (qtree_entry_unused(info, dp))
101		return 0;
102	return __le32_to_cpu(d->dqb_id) == dquot->dq_id;
103}
104
105static struct qtree_fmt_operations v2r1_fmt_ops = {
106	.mem2disk_dqblk = v2r1_mem2diskdqblk,
107	.disk2mem_dqblk = v2r1_disk2memdqblk,
108	.is_id = v2r1_is_id,
109};
110
111/*
112 * Copy dqinfo from disk to memory
113 */
114static inline void v2_disk2memdqinfo(struct util_dqinfo *m,
115				     struct v2_disk_dqinfo *d)
116{
117	m->dqi_bgrace = __le32_to_cpu(d->dqi_bgrace);
118	m->dqi_igrace = __le32_to_cpu(d->dqi_igrace);
119	m->u.v2_mdqi.dqi_flags = __le32_to_cpu(d->dqi_flags) & V2_DQF_MASK;
120	m->u.v2_mdqi.dqi_qtree.dqi_blocks = __le32_to_cpu(d->dqi_blocks);
121	m->u.v2_mdqi.dqi_qtree.dqi_free_blk = __le32_to_cpu(d->dqi_free_blk);
122	m->u.v2_mdqi.dqi_qtree.dqi_free_entry =
123				__le32_to_cpu(d->dqi_free_entry);
124}
125
126/*
127 * Copy dqinfo from memory to disk
128 */
129static inline void v2_mem2diskdqinfo(struct v2_disk_dqinfo *d,
130				     struct util_dqinfo *m)
131{
132	d->dqi_bgrace = __cpu_to_le32(m->dqi_bgrace);
133	d->dqi_igrace = __cpu_to_le32(m->dqi_igrace);
134	d->dqi_flags = __cpu_to_le32(m->u.v2_mdqi.dqi_flags & V2_DQF_MASK);
135	d->dqi_blocks = __cpu_to_le32(m->u.v2_mdqi.dqi_qtree.dqi_blocks);
136	d->dqi_free_blk = __cpu_to_le32(m->u.v2_mdqi.dqi_qtree.dqi_free_blk);
137	d->dqi_free_entry =
138			__cpu_to_le32(m->u.v2_mdqi.dqi_qtree.dqi_free_entry);
139}
140
141/* Convert kernel quotablock format to utility one */
142static inline void v2_kern2utildqblk(struct util_dqblk *u,
143				     struct v2_kern_dqblk *k)
144{
145	u->dqb_ihardlimit = k->dqb_ihardlimit;
146	u->dqb_isoftlimit = k->dqb_isoftlimit;
147	u->dqb_bhardlimit = k->dqb_bhardlimit;
148	u->dqb_bsoftlimit = k->dqb_bsoftlimit;
149	u->dqb_curinodes = k->dqb_curinodes;
150	u->dqb_curspace = k->dqb_curspace;
151	u->dqb_itime = k->dqb_itime;
152	u->dqb_btime = k->dqb_btime;
153}
154
155/* Convert utility quotablock format to kernel one */
156static inline void v2_util2kerndqblk(struct v2_kern_dqblk *k,
157				     struct util_dqblk *u)
158{
159	k->dqb_ihardlimit = u->dqb_ihardlimit;
160	k->dqb_isoftlimit = u->dqb_isoftlimit;
161	k->dqb_bhardlimit = u->dqb_bhardlimit;
162	k->dqb_bsoftlimit = u->dqb_bsoftlimit;
163	k->dqb_curinodes = u->dqb_curinodes;
164	k->dqb_curspace = u->dqb_curspace;
165	k->dqb_itime = u->dqb_itime;
166	k->dqb_btime = u->dqb_btime;
167}
168
169static int v2_read_header(struct quota_handle *h, struct v2_disk_dqheader *dqh)
170{
171	if (h->e2fs_read(&h->qh_qf, 0, dqh, sizeof(struct v2_disk_dqheader)) !=
172			sizeof(struct v2_disk_dqheader))
173		return 0;
174
175	return 1;
176}
177
178/*
179 * Check whether given quota file is in our format
180 */
181static int v2_check_file(struct quota_handle *h, int type, int fmt)
182{
183	struct v2_disk_dqheader dqh;
184	int file_magics[] = INITQMAGICS;
185	int known_versions[] = INIT_V2_VERSIONS;
186	int version;
187
188	if (!v2_read_header(h, &dqh))
189		return 0;
190	if (fmt == QFMT_VFS_V0)
191		version = 0;
192	else if (fmt == QFMT_VFS_V1)
193		version = 1;
194	else
195		return 0;
196
197	if (__le32_to_cpu(dqh.dqh_magic) != file_magics[type]) {
198		if (__be32_to_cpu(dqh.dqh_magic) == file_magics[type])
199			log_fatal(3, "Your quota file is stored in wrong "
200				  "endianity.", "");
201		return 0;
202	}
203	if (__le32_to_cpu(dqh.dqh_version) > known_versions[type])
204		return 0;
205	if (version != __le32_to_cpu(dqh.dqh_version))
206		return 0;
207	return 1;
208}
209
210/*
211 * Open quotafile
212 */
213static int v2_init_io(struct quota_handle *h)
214{
215	log_err("Not Implemented.", "");
216	BUG_ON(1);
217	return 0;
218}
219
220/*
221 * Initialize new quotafile
222 */
223static int v2_new_io(struct quota_handle *h)
224{
225	int file_magics[] = INITQMAGICS;
226	struct v2_disk_dqheader ddqheader;
227	struct v2_disk_dqinfo ddqinfo;
228	int version = 1;
229
230	BUG_ON(h->qh_fmt != QFMT_VFS_V1);
231
232	/* Write basic quota header */
233	ddqheader.dqh_magic = __cpu_to_le32(file_magics[h->qh_type]);
234	ddqheader.dqh_version = __cpu_to_le32(version);
235	if (h->e2fs_write(&h->qh_qf, 0, &ddqheader, sizeof(ddqheader)) !=
236			sizeof(ddqheader))
237		return -1;
238
239	/* Write information about quotafile */
240	h->qh_info.dqi_bgrace = MAX_DQ_TIME;
241	h->qh_info.dqi_igrace = MAX_IQ_TIME;
242	h->qh_info.u.v2_mdqi.dqi_flags = 0;
243	h->qh_info.u.v2_mdqi.dqi_qtree.dqi_blocks = QT_TREEOFF + 1;
244	h->qh_info.u.v2_mdqi.dqi_qtree.dqi_free_blk = 0;
245	h->qh_info.u.v2_mdqi.dqi_qtree.dqi_free_entry = 0;
246	h->qh_info.u.v2_mdqi.dqi_qtree.dqi_entry_size =
247				sizeof(struct v2r1_disk_dqblk);
248	h->qh_info.u.v2_mdqi.dqi_qtree.dqi_ops = &v2r1_fmt_ops;
249	v2_mem2diskdqinfo(&ddqinfo, &h->qh_info);
250	if (h->e2fs_write(&h->qh_qf, V2_DQINFOOFF, &ddqinfo,
251			  sizeof(ddqinfo)) !=
252	    sizeof(ddqinfo))
253		return -1;
254
255	return 0;
256}
257
258/*
259 * Write information (grace times to file)
260 */
261static int v2_write_info(struct quota_handle *h)
262{
263	struct v2_disk_dqinfo ddqinfo;
264
265	v2_mem2diskdqinfo(&ddqinfo, &h->qh_info);
266	if (h->e2fs_write(&h->qh_qf, V2_DQINFOOFF, &ddqinfo, sizeof(ddqinfo)) !=
267			sizeof(ddqinfo))
268		return -1;
269
270	return 0;
271}
272
273/*
274 * Read dquot (either from disk or from kernel)
275 * User can use errno to detect errstr when NULL is returned
276 */
277static struct dquot *v2_read_dquot(struct quota_handle *h, qid_t id)
278{
279	return qtree_read_dquot(h, id);
280}
281
282/*
283 * Commit changes of dquot to disk - it might also mean deleting it when quota
284 * became fake one and user has no blocks.
285 * User can process use 'errno' to detect errstr.
286 */
287static int v2_commit_dquot(struct dquot *dquot, int flags)
288{
289	struct util_dqblk *b = &dquot->dq_dqb;
290
291	if (!b->dqb_curspace && !b->dqb_curinodes && !b->dqb_bsoftlimit &&
292	    !b->dqb_isoftlimit && !b->dqb_bhardlimit && !b->dqb_ihardlimit)
293		qtree_delete_dquot(dquot);
294	else
295		qtree_write_dquot(dquot);
296	return 0;
297}
298
299static int v2_scan_dquots(struct quota_handle *h,
300			  int (*process_dquot) (struct dquot *, char *))
301{
302	return qtree_scan_dquots(h, process_dquot);
303}
304
305/* Report information about quotafile.
306 * TODO: Not used right now, but we should be able to use this when we add
307 * support to debugfs to read quota files.
308 */
309static int v2_report(struct quota_handle *h, int verbose)
310{
311	log_err("Not Implemented.", "");
312	BUG_ON(1);
313	return 0;
314}
315