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