xref: /aosp_15_r20/external/e2fsprogs/lib/support/quotaio_v2.c (revision 6a54128f25917bfc36a8a6e9d722c04a0b4641b6)
1*6a54128fSAndroid Build Coastguard Worker /*
2*6a54128fSAndroid Build Coastguard Worker  * Implementation of new quotafile format
3*6a54128fSAndroid Build Coastguard Worker  *
4*6a54128fSAndroid Build Coastguard Worker  * Jan Kara <[email protected]> - sponsored by SuSE CR
5*6a54128fSAndroid Build Coastguard Worker  */
6*6a54128fSAndroid Build Coastguard Worker 
7*6a54128fSAndroid Build Coastguard Worker #include "config.h"
8*6a54128fSAndroid Build Coastguard Worker #include <sys/types.h>
9*6a54128fSAndroid Build Coastguard Worker #include <errno.h>
10*6a54128fSAndroid Build Coastguard Worker #include <stdio.h>
11*6a54128fSAndroid Build Coastguard Worker #include <stdlib.h>
12*6a54128fSAndroid Build Coastguard Worker #include <string.h>
13*6a54128fSAndroid Build Coastguard Worker #include <unistd.h>
14*6a54128fSAndroid Build Coastguard Worker 
15*6a54128fSAndroid Build Coastguard Worker #include "common.h"
16*6a54128fSAndroid Build Coastguard Worker #include "quotaio_v2.h"
17*6a54128fSAndroid Build Coastguard Worker #include "dqblk_v2.h"
18*6a54128fSAndroid Build Coastguard Worker #include "quotaio.h"
19*6a54128fSAndroid Build Coastguard Worker #include "quotaio_tree.h"
20*6a54128fSAndroid Build Coastguard Worker 
21*6a54128fSAndroid Build Coastguard Worker static int v2_check_file(struct quota_handle *h, int type, int fmt);
22*6a54128fSAndroid Build Coastguard Worker static int v2_init_io(struct quota_handle *h);
23*6a54128fSAndroid Build Coastguard Worker static int v2_new_io(struct quota_handle *h);
24*6a54128fSAndroid Build Coastguard Worker static int v2_write_info(struct quota_handle *h);
25*6a54128fSAndroid Build Coastguard Worker static struct dquot *v2_read_dquot(struct quota_handle *h, qid_t id);
26*6a54128fSAndroid Build Coastguard Worker static int v2_commit_dquot(struct dquot *dquot);
27*6a54128fSAndroid Build Coastguard Worker static int v2_scan_dquots(struct quota_handle *h,
28*6a54128fSAndroid Build Coastguard Worker 			  int (*process_dquot) (struct dquot *dquot,
29*6a54128fSAndroid Build Coastguard Worker 						void *data),
30*6a54128fSAndroid Build Coastguard Worker 			  void *data);
31*6a54128fSAndroid Build Coastguard Worker static int v2_report(struct quota_handle *h, int verbose);
32*6a54128fSAndroid Build Coastguard Worker 
33*6a54128fSAndroid Build Coastguard Worker struct quotafile_ops quotafile_ops_2 = {
34*6a54128fSAndroid Build Coastguard Worker 	.check_file	= v2_check_file,
35*6a54128fSAndroid Build Coastguard Worker 	.init_io 	= v2_init_io,
36*6a54128fSAndroid Build Coastguard Worker 	.new_io 	= v2_new_io,
37*6a54128fSAndroid Build Coastguard Worker 	.write_info	= v2_write_info,
38*6a54128fSAndroid Build Coastguard Worker 	.read_dquot	= v2_read_dquot,
39*6a54128fSAndroid Build Coastguard Worker 	.commit_dquot	= v2_commit_dquot,
40*6a54128fSAndroid Build Coastguard Worker 	.scan_dquots	= v2_scan_dquots,
41*6a54128fSAndroid Build Coastguard Worker 	.report		= v2_report,
42*6a54128fSAndroid Build Coastguard Worker };
43*6a54128fSAndroid Build Coastguard Worker 
44*6a54128fSAndroid Build Coastguard Worker /*
45*6a54128fSAndroid Build Coastguard Worker  * Copy dquot from disk to memory
46*6a54128fSAndroid Build Coastguard Worker  */
v2r0_disk2memdqblk(struct dquot * dquot,void * dp)47*6a54128fSAndroid Build Coastguard Worker static void v2r0_disk2memdqblk(struct dquot *dquot, void *dp)
48*6a54128fSAndroid Build Coastguard Worker {
49*6a54128fSAndroid Build Coastguard Worker 	struct util_dqblk *m = &dquot->dq_dqb;
50*6a54128fSAndroid Build Coastguard Worker 	struct v2r0_disk_dqblk *d = dp, empty;
51*6a54128fSAndroid Build Coastguard Worker 
52*6a54128fSAndroid Build Coastguard Worker 	dquot->dq_id = ext2fs_le32_to_cpu(d->dqb_id);
53*6a54128fSAndroid Build Coastguard Worker 	m->dqb_ihardlimit = ext2fs_le32_to_cpu(d->dqb_ihardlimit);
54*6a54128fSAndroid Build Coastguard Worker 	m->dqb_isoftlimit = ext2fs_le32_to_cpu(d->dqb_isoftlimit);
55*6a54128fSAndroid Build Coastguard Worker 	m->dqb_bhardlimit = ext2fs_le32_to_cpu(d->dqb_bhardlimit);
56*6a54128fSAndroid Build Coastguard Worker 	m->dqb_bsoftlimit = ext2fs_le32_to_cpu(d->dqb_bsoftlimit);
57*6a54128fSAndroid Build Coastguard Worker 	m->dqb_curinodes = ext2fs_le32_to_cpu(d->dqb_curinodes);
58*6a54128fSAndroid Build Coastguard Worker 	m->dqb_curspace = ext2fs_le64_to_cpu(d->dqb_curspace);
59*6a54128fSAndroid Build Coastguard Worker 	m->dqb_itime = ext2fs_le64_to_cpu(d->dqb_itime);
60*6a54128fSAndroid Build Coastguard Worker 	m->dqb_btime = ext2fs_le64_to_cpu(d->dqb_btime);
61*6a54128fSAndroid Build Coastguard Worker 
62*6a54128fSAndroid Build Coastguard Worker 	memset(&empty, 0, sizeof(struct v2r0_disk_dqblk));
63*6a54128fSAndroid Build Coastguard Worker 	empty.dqb_itime = ext2fs_cpu_to_le64(1);
64*6a54128fSAndroid Build Coastguard Worker 	if (!memcmp(&empty, dp, sizeof(struct v2r0_disk_dqblk)))
65*6a54128fSAndroid Build Coastguard Worker 		m->dqb_itime = 0;
66*6a54128fSAndroid Build Coastguard Worker }
67*6a54128fSAndroid Build Coastguard Worker 
68*6a54128fSAndroid Build Coastguard Worker /*
69*6a54128fSAndroid Build Coastguard Worker  * Copy dquot from memory to disk
70*6a54128fSAndroid Build Coastguard Worker  */
v2r0_mem2diskdqblk(void * dp,struct dquot * dquot)71*6a54128fSAndroid Build Coastguard Worker static void v2r0_mem2diskdqblk(void *dp, struct dquot *dquot)
72*6a54128fSAndroid Build Coastguard Worker {
73*6a54128fSAndroid Build Coastguard Worker 	struct util_dqblk *m = &dquot->dq_dqb;
74*6a54128fSAndroid Build Coastguard Worker 	struct v2r0_disk_dqblk *d = dp;
75*6a54128fSAndroid Build Coastguard Worker 
76*6a54128fSAndroid Build Coastguard Worker 	d->dqb_ihardlimit = ext2fs_cpu_to_le32(m->dqb_ihardlimit);
77*6a54128fSAndroid Build Coastguard Worker 	d->dqb_isoftlimit = ext2fs_cpu_to_le32(m->dqb_isoftlimit);
78*6a54128fSAndroid Build Coastguard Worker 	d->dqb_bhardlimit = ext2fs_cpu_to_le32(m->dqb_bhardlimit);
79*6a54128fSAndroid Build Coastguard Worker 	d->dqb_bsoftlimit = ext2fs_cpu_to_le32(m->dqb_bsoftlimit);
80*6a54128fSAndroid Build Coastguard Worker 	d->dqb_curinodes = ext2fs_cpu_to_le32(m->dqb_curinodes);
81*6a54128fSAndroid Build Coastguard Worker 	d->dqb_curspace = ext2fs_cpu_to_le64(m->dqb_curspace);
82*6a54128fSAndroid Build Coastguard Worker 	d->dqb_itime = ext2fs_cpu_to_le64(m->dqb_itime);
83*6a54128fSAndroid Build Coastguard Worker 	d->dqb_btime = ext2fs_cpu_to_le64(m->dqb_btime);
84*6a54128fSAndroid Build Coastguard Worker 	d->dqb_id = ext2fs_cpu_to_le32(dquot->dq_id);
85*6a54128fSAndroid Build Coastguard Worker 	if (qtree_entry_unused(&dquot->dq_h->qh_info.u.v2_mdqi.dqi_qtree, dp))
86*6a54128fSAndroid Build Coastguard Worker 		d->dqb_itime = ext2fs_cpu_to_le64(1);
87*6a54128fSAndroid Build Coastguard Worker }
88*6a54128fSAndroid Build Coastguard Worker 
v2r0_is_id(void * dp,struct dquot * dquot)89*6a54128fSAndroid Build Coastguard Worker static int v2r0_is_id(void *dp, struct dquot *dquot)
90*6a54128fSAndroid Build Coastguard Worker {
91*6a54128fSAndroid Build Coastguard Worker 	struct v2r0_disk_dqblk *d = dp;
92*6a54128fSAndroid Build Coastguard Worker 	struct qtree_mem_dqinfo *info =
93*6a54128fSAndroid Build Coastguard Worker 			&dquot->dq_h->qh_info.u.v2_mdqi.dqi_qtree;
94*6a54128fSAndroid Build Coastguard Worker 
95*6a54128fSAndroid Build Coastguard Worker 	if (qtree_entry_unused(info, dp))
96*6a54128fSAndroid Build Coastguard Worker 		return 0;
97*6a54128fSAndroid Build Coastguard Worker 	return ext2fs_le32_to_cpu(d->dqb_id) == dquot->dq_id;
98*6a54128fSAndroid Build Coastguard Worker }
99*6a54128fSAndroid Build Coastguard Worker 
100*6a54128fSAndroid Build Coastguard Worker static struct qtree_fmt_operations v2r0_fmt_ops = {
101*6a54128fSAndroid Build Coastguard Worker 	.mem2disk_dqblk = v2r0_mem2diskdqblk,
102*6a54128fSAndroid Build Coastguard Worker 	.disk2mem_dqblk = v2r0_disk2memdqblk,
103*6a54128fSAndroid Build Coastguard Worker 	.is_id = v2r0_is_id,
104*6a54128fSAndroid Build Coastguard Worker };
105*6a54128fSAndroid Build Coastguard Worker 
106*6a54128fSAndroid Build Coastguard Worker /*
107*6a54128fSAndroid Build Coastguard Worker  * Copy dquot from disk to memory
108*6a54128fSAndroid Build Coastguard Worker  */
v2r1_disk2memdqblk(struct dquot * dquot,void * dp)109*6a54128fSAndroid Build Coastguard Worker static void v2r1_disk2memdqblk(struct dquot *dquot, void *dp)
110*6a54128fSAndroid Build Coastguard Worker {
111*6a54128fSAndroid Build Coastguard Worker 	struct util_dqblk *m = &dquot->dq_dqb;
112*6a54128fSAndroid Build Coastguard Worker 	struct v2r1_disk_dqblk *d = dp, empty;
113*6a54128fSAndroid Build Coastguard Worker 
114*6a54128fSAndroid Build Coastguard Worker 	dquot->dq_id = ext2fs_le32_to_cpu(d->dqb_id);
115*6a54128fSAndroid Build Coastguard Worker 	m->dqb_ihardlimit = ext2fs_le64_to_cpu(d->dqb_ihardlimit);
116*6a54128fSAndroid Build Coastguard Worker 	m->dqb_isoftlimit = ext2fs_le64_to_cpu(d->dqb_isoftlimit);
117*6a54128fSAndroid Build Coastguard Worker 	m->dqb_bhardlimit = ext2fs_le64_to_cpu(d->dqb_bhardlimit);
118*6a54128fSAndroid Build Coastguard Worker 	m->dqb_bsoftlimit = ext2fs_le64_to_cpu(d->dqb_bsoftlimit);
119*6a54128fSAndroid Build Coastguard Worker 	m->dqb_curinodes = ext2fs_le64_to_cpu(d->dqb_curinodes);
120*6a54128fSAndroid Build Coastguard Worker 	m->dqb_curspace = ext2fs_le64_to_cpu(d->dqb_curspace);
121*6a54128fSAndroid Build Coastguard Worker 	m->dqb_itime = ext2fs_le64_to_cpu(d->dqb_itime);
122*6a54128fSAndroid Build Coastguard Worker 	m->dqb_btime = ext2fs_le64_to_cpu(d->dqb_btime);
123*6a54128fSAndroid Build Coastguard Worker 
124*6a54128fSAndroid Build Coastguard Worker 	memset(&empty, 0, sizeof(struct v2r1_disk_dqblk));
125*6a54128fSAndroid Build Coastguard Worker 	empty.dqb_itime = ext2fs_cpu_to_le64(1);
126*6a54128fSAndroid Build Coastguard Worker 	if (!memcmp(&empty, dp, sizeof(struct v2r1_disk_dqblk)))
127*6a54128fSAndroid Build Coastguard Worker 		m->dqb_itime = 0;
128*6a54128fSAndroid Build Coastguard Worker }
129*6a54128fSAndroid Build Coastguard Worker 
130*6a54128fSAndroid Build Coastguard Worker /*
131*6a54128fSAndroid Build Coastguard Worker  * Copy dquot from memory to disk
132*6a54128fSAndroid Build Coastguard Worker  */
v2r1_mem2diskdqblk(void * dp,struct dquot * dquot)133*6a54128fSAndroid Build Coastguard Worker static void v2r1_mem2diskdqblk(void *dp, struct dquot *dquot)
134*6a54128fSAndroid Build Coastguard Worker {
135*6a54128fSAndroid Build Coastguard Worker 	struct util_dqblk *m = &dquot->dq_dqb;
136*6a54128fSAndroid Build Coastguard Worker 	struct v2r1_disk_dqblk *d = dp;
137*6a54128fSAndroid Build Coastguard Worker 
138*6a54128fSAndroid Build Coastguard Worker 	d->dqb_ihardlimit = ext2fs_cpu_to_le64(m->dqb_ihardlimit);
139*6a54128fSAndroid Build Coastguard Worker 	d->dqb_isoftlimit = ext2fs_cpu_to_le64(m->dqb_isoftlimit);
140*6a54128fSAndroid Build Coastguard Worker 	d->dqb_bhardlimit = ext2fs_cpu_to_le64(m->dqb_bhardlimit);
141*6a54128fSAndroid Build Coastguard Worker 	d->dqb_bsoftlimit = ext2fs_cpu_to_le64(m->dqb_bsoftlimit);
142*6a54128fSAndroid Build Coastguard Worker 	d->dqb_curinodes = ext2fs_cpu_to_le64(m->dqb_curinodes);
143*6a54128fSAndroid Build Coastguard Worker 	d->dqb_curspace = ext2fs_cpu_to_le64(m->dqb_curspace);
144*6a54128fSAndroid Build Coastguard Worker 	d->dqb_itime = ext2fs_cpu_to_le64(m->dqb_itime);
145*6a54128fSAndroid Build Coastguard Worker 	d->dqb_btime = ext2fs_cpu_to_le64(m->dqb_btime);
146*6a54128fSAndroid Build Coastguard Worker 	d->dqb_id = ext2fs_cpu_to_le32(dquot->dq_id);
147*6a54128fSAndroid Build Coastguard Worker 	if (qtree_entry_unused(&dquot->dq_h->qh_info.u.v2_mdqi.dqi_qtree, dp))
148*6a54128fSAndroid Build Coastguard Worker 		d->dqb_itime = ext2fs_cpu_to_le64(1);
149*6a54128fSAndroid Build Coastguard Worker }
150*6a54128fSAndroid Build Coastguard Worker 
v2r1_is_id(void * dp,struct dquot * dquot)151*6a54128fSAndroid Build Coastguard Worker static int v2r1_is_id(void *dp, struct dquot *dquot)
152*6a54128fSAndroid Build Coastguard Worker {
153*6a54128fSAndroid Build Coastguard Worker 	struct v2r1_disk_dqblk *d = dp;
154*6a54128fSAndroid Build Coastguard Worker 	struct qtree_mem_dqinfo *info =
155*6a54128fSAndroid Build Coastguard Worker 			&dquot->dq_h->qh_info.u.v2_mdqi.dqi_qtree;
156*6a54128fSAndroid Build Coastguard Worker 
157*6a54128fSAndroid Build Coastguard Worker 	if (qtree_entry_unused(info, dp))
158*6a54128fSAndroid Build Coastguard Worker 		return 0;
159*6a54128fSAndroid Build Coastguard Worker 	return ext2fs_le32_to_cpu(d->dqb_id) == dquot->dq_id;
160*6a54128fSAndroid Build Coastguard Worker }
161*6a54128fSAndroid Build Coastguard Worker 
162*6a54128fSAndroid Build Coastguard Worker static struct qtree_fmt_operations v2r1_fmt_ops = {
163*6a54128fSAndroid Build Coastguard Worker 	.mem2disk_dqblk = v2r1_mem2diskdqblk,
164*6a54128fSAndroid Build Coastguard Worker 	.disk2mem_dqblk = v2r1_disk2memdqblk,
165*6a54128fSAndroid Build Coastguard Worker 	.is_id = v2r1_is_id,
166*6a54128fSAndroid Build Coastguard Worker };
167*6a54128fSAndroid Build Coastguard Worker 
168*6a54128fSAndroid Build Coastguard Worker /*
169*6a54128fSAndroid Build Coastguard Worker  * Copy dqinfo from disk to memory
170*6a54128fSAndroid Build Coastguard Worker  */
v2_disk2memdqinfo(struct util_dqinfo * m,struct v2_disk_dqinfo * d)171*6a54128fSAndroid Build Coastguard Worker static inline void v2_disk2memdqinfo(struct util_dqinfo *m,
172*6a54128fSAndroid Build Coastguard Worker 				     struct v2_disk_dqinfo *d)
173*6a54128fSAndroid Build Coastguard Worker {
174*6a54128fSAndroid Build Coastguard Worker 	m->dqi_bgrace = ext2fs_le32_to_cpu(d->dqi_bgrace);
175*6a54128fSAndroid Build Coastguard Worker 	m->dqi_igrace = ext2fs_le32_to_cpu(d->dqi_igrace);
176*6a54128fSAndroid Build Coastguard Worker 	m->u.v2_mdqi.dqi_flags = ext2fs_le32_to_cpu(d->dqi_flags) & V2_DQF_MASK;
177*6a54128fSAndroid Build Coastguard Worker 	m->u.v2_mdqi.dqi_qtree.dqi_blocks = ext2fs_le32_to_cpu(d->dqi_blocks);
178*6a54128fSAndroid Build Coastguard Worker 	m->u.v2_mdqi.dqi_qtree.dqi_free_blk =
179*6a54128fSAndroid Build Coastguard Worker 		ext2fs_le32_to_cpu(d->dqi_free_blk);
180*6a54128fSAndroid Build Coastguard Worker 	m->u.v2_mdqi.dqi_qtree.dqi_free_entry =
181*6a54128fSAndroid Build Coastguard Worker 				ext2fs_le32_to_cpu(d->dqi_free_entry);
182*6a54128fSAndroid Build Coastguard Worker }
183*6a54128fSAndroid Build Coastguard Worker 
184*6a54128fSAndroid Build Coastguard Worker /*
185*6a54128fSAndroid Build Coastguard Worker  * Copy dqinfo from memory to disk
186*6a54128fSAndroid Build Coastguard Worker  */
v2_mem2diskdqinfo(struct v2_disk_dqinfo * d,struct util_dqinfo * m)187*6a54128fSAndroid Build Coastguard Worker static inline void v2_mem2diskdqinfo(struct v2_disk_dqinfo *d,
188*6a54128fSAndroid Build Coastguard Worker 				     struct util_dqinfo *m)
189*6a54128fSAndroid Build Coastguard Worker {
190*6a54128fSAndroid Build Coastguard Worker 	d->dqi_bgrace = ext2fs_cpu_to_le32(m->dqi_bgrace);
191*6a54128fSAndroid Build Coastguard Worker 	d->dqi_igrace = ext2fs_cpu_to_le32(m->dqi_igrace);
192*6a54128fSAndroid Build Coastguard Worker 	d->dqi_flags = ext2fs_cpu_to_le32(m->u.v2_mdqi.dqi_flags & V2_DQF_MASK);
193*6a54128fSAndroid Build Coastguard Worker 	d->dqi_blocks = ext2fs_cpu_to_le32(m->u.v2_mdqi.dqi_qtree.dqi_blocks);
194*6a54128fSAndroid Build Coastguard Worker 	d->dqi_free_blk =
195*6a54128fSAndroid Build Coastguard Worker 		ext2fs_cpu_to_le32(m->u.v2_mdqi.dqi_qtree.dqi_free_blk);
196*6a54128fSAndroid Build Coastguard Worker 	d->dqi_free_entry =
197*6a54128fSAndroid Build Coastguard Worker 		ext2fs_cpu_to_le32(m->u.v2_mdqi.dqi_qtree.dqi_free_entry);
198*6a54128fSAndroid Build Coastguard Worker }
199*6a54128fSAndroid Build Coastguard Worker 
v2_read_header(struct quota_handle * h,struct v2_disk_dqheader * dqh)200*6a54128fSAndroid Build Coastguard Worker static int v2_read_header(struct quota_handle *h, struct v2_disk_dqheader *dqh)
201*6a54128fSAndroid Build Coastguard Worker {
202*6a54128fSAndroid Build Coastguard Worker 	if (h->e2fs_read(&h->qh_qf, 0, dqh, sizeof(struct v2_disk_dqheader)) !=
203*6a54128fSAndroid Build Coastguard Worker 			sizeof(struct v2_disk_dqheader))
204*6a54128fSAndroid Build Coastguard Worker 		return 0;
205*6a54128fSAndroid Build Coastguard Worker 
206*6a54128fSAndroid Build Coastguard Worker 	return 1;
207*6a54128fSAndroid Build Coastguard Worker }
208*6a54128fSAndroid Build Coastguard Worker 
209*6a54128fSAndroid Build Coastguard Worker /*
210*6a54128fSAndroid Build Coastguard Worker  * Check whether given quota file is in our format
211*6a54128fSAndroid Build Coastguard Worker  */
v2_check_file(struct quota_handle * h,int type,int fmt)212*6a54128fSAndroid Build Coastguard Worker static int v2_check_file(struct quota_handle *h, int type, int fmt)
213*6a54128fSAndroid Build Coastguard Worker {
214*6a54128fSAndroid Build Coastguard Worker 	struct v2_disk_dqheader dqh;
215*6a54128fSAndroid Build Coastguard Worker 	int file_magics[] = INITQMAGICS;
216*6a54128fSAndroid Build Coastguard Worker 	int be_magic;
217*6a54128fSAndroid Build Coastguard Worker 
218*6a54128fSAndroid Build Coastguard Worker 	if (fmt != QFMT_VFS_V1)
219*6a54128fSAndroid Build Coastguard Worker 		return 0;
220*6a54128fSAndroid Build Coastguard Worker 
221*6a54128fSAndroid Build Coastguard Worker 	if (!v2_read_header(h, &dqh))
222*6a54128fSAndroid Build Coastguard Worker 		return 0;
223*6a54128fSAndroid Build Coastguard Worker 
224*6a54128fSAndroid Build Coastguard Worker 	be_magic = ext2fs_be32_to_cpu((__force __be32)dqh.dqh_magic);
225*6a54128fSAndroid Build Coastguard Worker 	if (be_magic == file_magics[type]) {
226*6a54128fSAndroid Build Coastguard Worker 		log_err("Your quota file is stored in wrong endianness");
227*6a54128fSAndroid Build Coastguard Worker 		return 0;
228*6a54128fSAndroid Build Coastguard Worker 	}
229*6a54128fSAndroid Build Coastguard Worker 	if (V2_VERSION_R0 != ext2fs_le32_to_cpu(dqh.dqh_version) &&
230*6a54128fSAndroid Build Coastguard Worker 	    V2_VERSION_R1 != ext2fs_le32_to_cpu(dqh.dqh_version))
231*6a54128fSAndroid Build Coastguard Worker 		return 0;
232*6a54128fSAndroid Build Coastguard Worker 	return 1;
233*6a54128fSAndroid Build Coastguard Worker }
234*6a54128fSAndroid Build Coastguard Worker 
235*6a54128fSAndroid Build Coastguard Worker /*
236*6a54128fSAndroid Build Coastguard Worker  * Open quotafile
237*6a54128fSAndroid Build Coastguard Worker  */
v2_init_io(struct quota_handle * h)238*6a54128fSAndroid Build Coastguard Worker static int v2_init_io(struct quota_handle *h)
239*6a54128fSAndroid Build Coastguard Worker {
240*6a54128fSAndroid Build Coastguard Worker 	struct v2_disk_dqheader dqh;
241*6a54128fSAndroid Build Coastguard Worker 	struct v2_disk_dqinfo ddqinfo;
242*6a54128fSAndroid Build Coastguard Worker 	struct v2_mem_dqinfo *info;
243*6a54128fSAndroid Build Coastguard Worker 	__u64 filesize;
244*6a54128fSAndroid Build Coastguard Worker 	int version;
245*6a54128fSAndroid Build Coastguard Worker 
246*6a54128fSAndroid Build Coastguard Worker 	if (!v2_read_header(h, &dqh))
247*6a54128fSAndroid Build Coastguard Worker 		return -1;
248*6a54128fSAndroid Build Coastguard Worker 	version = ext2fs_le32_to_cpu(dqh.dqh_version);
249*6a54128fSAndroid Build Coastguard Worker 
250*6a54128fSAndroid Build Coastguard Worker 	if (version == V2_VERSION_R0) {
251*6a54128fSAndroid Build Coastguard Worker 		h->qh_info.u.v2_mdqi.dqi_qtree.dqi_entry_size =
252*6a54128fSAndroid Build Coastguard Worker 			sizeof(struct v2r0_disk_dqblk);
253*6a54128fSAndroid Build Coastguard Worker 		h->qh_info.u.v2_mdqi.dqi_qtree.dqi_ops = &v2r0_fmt_ops;
254*6a54128fSAndroid Build Coastguard Worker 	} else {
255*6a54128fSAndroid Build Coastguard Worker 		h->qh_info.u.v2_mdqi.dqi_qtree.dqi_entry_size =
256*6a54128fSAndroid Build Coastguard Worker 			sizeof(struct v2r1_disk_dqblk);
257*6a54128fSAndroid Build Coastguard Worker 		h->qh_info.u.v2_mdqi.dqi_qtree.dqi_ops = &v2r1_fmt_ops;
258*6a54128fSAndroid Build Coastguard Worker 	}
259*6a54128fSAndroid Build Coastguard Worker 
260*6a54128fSAndroid Build Coastguard Worker 	/* Read information about quotafile */
261*6a54128fSAndroid Build Coastguard Worker 	if (h->e2fs_read(&h->qh_qf, V2_DQINFOOFF, &ddqinfo,
262*6a54128fSAndroid Build Coastguard Worker 			 sizeof(ddqinfo)) != sizeof(ddqinfo))
263*6a54128fSAndroid Build Coastguard Worker 		return -1;
264*6a54128fSAndroid Build Coastguard Worker 	v2_disk2memdqinfo(&h->qh_info, &ddqinfo);
265*6a54128fSAndroid Build Coastguard Worker 
266*6a54128fSAndroid Build Coastguard Worker 	/* Check to make sure quota file info is sane */
267*6a54128fSAndroid Build Coastguard Worker 	info = &h->qh_info.u.v2_mdqi;
268*6a54128fSAndroid Build Coastguard Worker 	if (ext2fs_file_get_lsize(h->qh_qf.e2_file, &filesize))
269*6a54128fSAndroid Build Coastguard Worker 		return -1;
270*6a54128fSAndroid Build Coastguard Worker 	if ((filesize > (1U << 31)) ||
271*6a54128fSAndroid Build Coastguard Worker 	    (info->dqi_qtree.dqi_blocks >
272*6a54128fSAndroid Build Coastguard Worker 	     (filesize + QT_BLKSIZE - 1) >> QT_BLKSIZE_BITS)) {
273*6a54128fSAndroid Build Coastguard Worker 		log_err("Quota inode %u corrupted: file size %llu; "
274*6a54128fSAndroid Build Coastguard Worker 			"dqi_blocks %u", h->qh_qf.ino,
275*6a54128fSAndroid Build Coastguard Worker 			(unsigned long long) filesize,
276*6a54128fSAndroid Build Coastguard Worker 			info->dqi_qtree.dqi_blocks);
277*6a54128fSAndroid Build Coastguard Worker 		return -1;
278*6a54128fSAndroid Build Coastguard Worker 	}
279*6a54128fSAndroid Build Coastguard Worker 	if (info->dqi_qtree.dqi_free_blk >= info->dqi_qtree.dqi_blocks) {
280*6a54128fSAndroid Build Coastguard Worker 		log_err("Quota inode %u corrupted: free_blk %u; dqi_blocks %u",
281*6a54128fSAndroid Build Coastguard Worker 			h->qh_qf.ino, info->dqi_qtree.dqi_free_blk,
282*6a54128fSAndroid Build Coastguard Worker 			info->dqi_qtree.dqi_blocks);
283*6a54128fSAndroid Build Coastguard Worker 		return -1;
284*6a54128fSAndroid Build Coastguard Worker 	}
285*6a54128fSAndroid Build Coastguard Worker 	if (info->dqi_qtree.dqi_free_entry >= info->dqi_qtree.dqi_blocks) {
286*6a54128fSAndroid Build Coastguard Worker 		log_err("Quota inode %u corrupted: free_entry %u; "
287*6a54128fSAndroid Build Coastguard Worker 			"dqi_blocks %u", h->qh_qf.ino,
288*6a54128fSAndroid Build Coastguard Worker 			info->dqi_qtree.dqi_free_entry,
289*6a54128fSAndroid Build Coastguard Worker 			info->dqi_qtree.dqi_blocks);
290*6a54128fSAndroid Build Coastguard Worker 		return -1;
291*6a54128fSAndroid Build Coastguard Worker 	}
292*6a54128fSAndroid Build Coastguard Worker 	return 0;
293*6a54128fSAndroid Build Coastguard Worker }
294*6a54128fSAndroid Build Coastguard Worker 
295*6a54128fSAndroid Build Coastguard Worker /*
296*6a54128fSAndroid Build Coastguard Worker  * Initialize new quotafile
297*6a54128fSAndroid Build Coastguard Worker  */
v2_new_io(struct quota_handle * h)298*6a54128fSAndroid Build Coastguard Worker static int v2_new_io(struct quota_handle *h)
299*6a54128fSAndroid Build Coastguard Worker {
300*6a54128fSAndroid Build Coastguard Worker 	int file_magics[] = INITQMAGICS;
301*6a54128fSAndroid Build Coastguard Worker 	struct v2_disk_dqheader ddqheader;
302*6a54128fSAndroid Build Coastguard Worker 	struct v2_disk_dqinfo ddqinfo;
303*6a54128fSAndroid Build Coastguard Worker 
304*6a54128fSAndroid Build Coastguard Worker 	if (h->qh_fmt != QFMT_VFS_V1)
305*6a54128fSAndroid Build Coastguard Worker 		return -1;
306*6a54128fSAndroid Build Coastguard Worker 
307*6a54128fSAndroid Build Coastguard Worker 	/* Write basic quota header */
308*6a54128fSAndroid Build Coastguard Worker 	ddqheader.dqh_magic = ext2fs_cpu_to_le32(file_magics[h->qh_type]);
309*6a54128fSAndroid Build Coastguard Worker 	ddqheader.dqh_version = ext2fs_cpu_to_le32(V2_VERSION_R1);
310*6a54128fSAndroid Build Coastguard Worker 	if (h->e2fs_write(&h->qh_qf, 0, &ddqheader, sizeof(ddqheader)) !=
311*6a54128fSAndroid Build Coastguard Worker 			sizeof(ddqheader))
312*6a54128fSAndroid Build Coastguard Worker 		return -1;
313*6a54128fSAndroid Build Coastguard Worker 
314*6a54128fSAndroid Build Coastguard Worker 	/* Write information about quotafile */
315*6a54128fSAndroid Build Coastguard Worker 	h->qh_info.dqi_bgrace = MAX_DQ_TIME;
316*6a54128fSAndroid Build Coastguard Worker 	h->qh_info.dqi_igrace = MAX_IQ_TIME;
317*6a54128fSAndroid Build Coastguard Worker 	h->qh_info.u.v2_mdqi.dqi_flags = 0;
318*6a54128fSAndroid Build Coastguard Worker 	h->qh_info.u.v2_mdqi.dqi_qtree.dqi_blocks = QT_TREEOFF + 1;
319*6a54128fSAndroid Build Coastguard Worker 	h->qh_info.u.v2_mdqi.dqi_qtree.dqi_free_blk = 0;
320*6a54128fSAndroid Build Coastguard Worker 	h->qh_info.u.v2_mdqi.dqi_qtree.dqi_free_entry = 0;
321*6a54128fSAndroid Build Coastguard Worker 	h->qh_info.u.v2_mdqi.dqi_qtree.dqi_entry_size =
322*6a54128fSAndroid Build Coastguard Worker 				sizeof(struct v2r1_disk_dqblk);
323*6a54128fSAndroid Build Coastguard Worker 	h->qh_info.u.v2_mdqi.dqi_qtree.dqi_ops = &v2r1_fmt_ops;
324*6a54128fSAndroid Build Coastguard Worker 	v2_mem2diskdqinfo(&ddqinfo, &h->qh_info);
325*6a54128fSAndroid Build Coastguard Worker 	if (h->e2fs_write(&h->qh_qf, V2_DQINFOOFF, &ddqinfo,
326*6a54128fSAndroid Build Coastguard Worker 			  sizeof(ddqinfo)) !=
327*6a54128fSAndroid Build Coastguard Worker 	    sizeof(ddqinfo))
328*6a54128fSAndroid Build Coastguard Worker 		return -1;
329*6a54128fSAndroid Build Coastguard Worker 
330*6a54128fSAndroid Build Coastguard Worker 	return 0;
331*6a54128fSAndroid Build Coastguard Worker }
332*6a54128fSAndroid Build Coastguard Worker 
333*6a54128fSAndroid Build Coastguard Worker /*
334*6a54128fSAndroid Build Coastguard Worker  * Write information (grace times to file)
335*6a54128fSAndroid Build Coastguard Worker  */
v2_write_info(struct quota_handle * h)336*6a54128fSAndroid Build Coastguard Worker static int v2_write_info(struct quota_handle *h)
337*6a54128fSAndroid Build Coastguard Worker {
338*6a54128fSAndroid Build Coastguard Worker 	struct v2_disk_dqinfo ddqinfo;
339*6a54128fSAndroid Build Coastguard Worker 
340*6a54128fSAndroid Build Coastguard Worker 	v2_mem2diskdqinfo(&ddqinfo, &h->qh_info);
341*6a54128fSAndroid Build Coastguard Worker 	if (h->e2fs_write(&h->qh_qf, V2_DQINFOOFF, &ddqinfo, sizeof(ddqinfo)) !=
342*6a54128fSAndroid Build Coastguard Worker 			sizeof(ddqinfo))
343*6a54128fSAndroid Build Coastguard Worker 		return -1;
344*6a54128fSAndroid Build Coastguard Worker 
345*6a54128fSAndroid Build Coastguard Worker 	return 0;
346*6a54128fSAndroid Build Coastguard Worker }
347*6a54128fSAndroid Build Coastguard Worker 
348*6a54128fSAndroid Build Coastguard Worker /*
349*6a54128fSAndroid Build Coastguard Worker  * Read dquot from disk
350*6a54128fSAndroid Build Coastguard Worker  */
v2_read_dquot(struct quota_handle * h,qid_t id)351*6a54128fSAndroid Build Coastguard Worker static struct dquot *v2_read_dquot(struct quota_handle *h, qid_t id)
352*6a54128fSAndroid Build Coastguard Worker {
353*6a54128fSAndroid Build Coastguard Worker 	return qtree_read_dquot(h, id);
354*6a54128fSAndroid Build Coastguard Worker }
355*6a54128fSAndroid Build Coastguard Worker 
356*6a54128fSAndroid Build Coastguard Worker /*
357*6a54128fSAndroid Build Coastguard Worker  * Commit changes of dquot to disk - it might also mean deleting it when quota
358*6a54128fSAndroid Build Coastguard Worker  * became fake one and user has no blocks.
359*6a54128fSAndroid Build Coastguard Worker  * User can process use 'errno' to detect errstr.
360*6a54128fSAndroid Build Coastguard Worker  */
v2_commit_dquot(struct dquot * dquot)361*6a54128fSAndroid Build Coastguard Worker static int v2_commit_dquot(struct dquot *dquot)
362*6a54128fSAndroid Build Coastguard Worker {
363*6a54128fSAndroid Build Coastguard Worker 	struct util_dqblk *b = &dquot->dq_dqb;
364*6a54128fSAndroid Build Coastguard Worker 
365*6a54128fSAndroid Build Coastguard Worker 	if (!b->dqb_curspace && !b->dqb_curinodes && !b->dqb_bsoftlimit &&
366*6a54128fSAndroid Build Coastguard Worker 	    !b->dqb_isoftlimit && !b->dqb_bhardlimit && !b->dqb_ihardlimit)
367*6a54128fSAndroid Build Coastguard Worker 		qtree_delete_dquot(dquot);
368*6a54128fSAndroid Build Coastguard Worker 	else
369*6a54128fSAndroid Build Coastguard Worker 		qtree_write_dquot(dquot);
370*6a54128fSAndroid Build Coastguard Worker 	return 0;
371*6a54128fSAndroid Build Coastguard Worker }
372*6a54128fSAndroid Build Coastguard Worker 
v2_scan_dquots(struct quota_handle * h,int (* process_dquot)(struct dquot *,void *),void * data)373*6a54128fSAndroid Build Coastguard Worker static int v2_scan_dquots(struct quota_handle *h,
374*6a54128fSAndroid Build Coastguard Worker 			  int (*process_dquot) (struct dquot *, void *),
375*6a54128fSAndroid Build Coastguard Worker 			  void *data)
376*6a54128fSAndroid Build Coastguard Worker {
377*6a54128fSAndroid Build Coastguard Worker 	return qtree_scan_dquots(h, process_dquot, data);
378*6a54128fSAndroid Build Coastguard Worker }
379*6a54128fSAndroid Build Coastguard Worker 
380*6a54128fSAndroid Build Coastguard Worker /* Report information about quotafile.
381*6a54128fSAndroid Build Coastguard Worker  * TODO: Not used right now, but we should be able to use this when we add
382*6a54128fSAndroid Build Coastguard Worker  * support to debugfs to read quota files.
383*6a54128fSAndroid Build Coastguard Worker  */
v2_report(struct quota_handle * h EXT2FS_ATTR ((unused)),int verbose EXT2FS_ATTR ((unused)))384*6a54128fSAndroid Build Coastguard Worker static int v2_report(struct quota_handle *h EXT2FS_ATTR((unused)),
385*6a54128fSAndroid Build Coastguard Worker 		     int verbose EXT2FS_ATTR((unused)))
386*6a54128fSAndroid Build Coastguard Worker {
387*6a54128fSAndroid Build Coastguard Worker 	log_err("Not Implemented.");
388*6a54128fSAndroid Build Coastguard Worker 	return -1;
389*6a54128fSAndroid Build Coastguard Worker }
390