xref: /aosp_15_r20/external/f2fs-tools/fsck/quotaio_v2.c (revision 59bfda1f02d633cd6b8b69f31eee485d40f6eef6)
1 /*
2  * Implementation of new quotafile format
3  *
4  * Jan Kara <[email protected]> - sponsored by SuSE CR
5  * Hyojun Kim <[email protected]> - Ported to f2fs-tools
6  */
7 
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 
15 #include "common.h"
16 
17 #include "quotaio_v2.h"
18 #include "dqblk_v2.h"
19 #include "quotaio_tree.h"
20 
21 static int v2_check_file(struct quota_handle *h, int type);
22 static int v2_init_io(struct quota_handle *h, enum quota_type qtype);
23 static int v2_new_io(struct quota_handle *h);
24 static int v2_write_info(struct quota_handle *h);
25 static struct dquot *v2_read_dquot(struct quota_handle *h, qid_t id);
26 static int v2_commit_dquot(struct dquot *dquot);
27 static int v2_scan_dquots(struct quota_handle *h,
28 			  int (*process_dquot) (struct dquot *dquot,
29 						void *data),
30 			  void *data);
31 static int v2_report(struct quota_handle *h, int verbose);
32 
33 struct quotafile_ops quotafile_ops_2 = {
34 	.check_file	= v2_check_file,
35 	.init_io 	= v2_init_io,
36 	.new_io 	= v2_new_io,
37 	.write_info	= v2_write_info,
38 	.read_dquot	= v2_read_dquot,
39 	.commit_dquot	= v2_commit_dquot,
40 	.scan_dquots	= v2_scan_dquots,
41 	.report		= v2_report,
42 };
43 
44 /*
45  * Copy dquot from disk to memory
46  */
v2r1_disk2memdqblk(struct dquot * dquot,void * dp)47 static void v2r1_disk2memdqblk(struct dquot *dquot, void *dp)
48 {
49 	struct util_dqblk *m = &dquot->dq_dqb;
50 	struct v2r1_disk_dqblk *d = dp, empty;
51 
52 	dquot->dq_id = le32_to_cpu(d->dqb_id);
53 	m->dqb_ihardlimit = le64_to_cpu(d->dqb_ihardlimit);
54 	m->dqb_isoftlimit = le64_to_cpu(d->dqb_isoftlimit);
55 	m->dqb_bhardlimit = le64_to_cpu(d->dqb_bhardlimit);
56 	m->dqb_bsoftlimit = le64_to_cpu(d->dqb_bsoftlimit);
57 	m->dqb_curinodes = le64_to_cpu(d->dqb_curinodes);
58 	m->dqb_curspace = le64_to_cpu(d->dqb_curspace);
59 	m->dqb_itime = le64_to_cpu(d->dqb_itime);
60 	m->dqb_btime = le64_to_cpu(d->dqb_btime);
61 
62 	memset(&empty, 0, sizeof(struct v2r1_disk_dqblk));
63 	empty.dqb_itime = cpu_to_le64(1);
64 	if (!memcmp(&empty, dp, sizeof(struct v2r1_disk_dqblk)))
65 		m->dqb_itime = 0;
66 }
67 
68 /*
69  * Copy dquot from memory to disk
70  */
v2r1_mem2diskdqblk(void * dp,struct dquot * dquot)71 static void v2r1_mem2diskdqblk(void *dp, struct dquot *dquot)
72 {
73 	struct util_dqblk *m = &dquot->dq_dqb;
74 	struct v2r1_disk_dqblk *d = dp;
75 
76 	d->dqb_ihardlimit = cpu_to_le64(m->dqb_ihardlimit);
77 	d->dqb_isoftlimit = cpu_to_le64(m->dqb_isoftlimit);
78 	d->dqb_bhardlimit = cpu_to_le64(m->dqb_bhardlimit);
79 	d->dqb_bsoftlimit = cpu_to_le64(m->dqb_bsoftlimit);
80 	d->dqb_curinodes = cpu_to_le64(m->dqb_curinodes);
81 	d->dqb_curspace = cpu_to_le64(m->dqb_curspace);
82 	d->dqb_itime = cpu_to_le64(m->dqb_itime);
83 	d->dqb_btime = cpu_to_le64(m->dqb_btime);
84 	d->dqb_id = cpu_to_le32(dquot->dq_id);
85 	if (qtree_entry_unused(&dquot->dq_h->qh_info.u.v2_mdqi.dqi_qtree, dp))
86 		d->dqb_itime = cpu_to_le64(1);
87 }
88 
v2r1_is_id(void * dp,struct dquot * dquot)89 static int v2r1_is_id(void *dp, struct dquot *dquot)
90 {
91 	struct v2r1_disk_dqblk *d = dp;
92 	struct qtree_mem_dqinfo *info =
93 			&dquot->dq_h->qh_info.u.v2_mdqi.dqi_qtree;
94 
95 	if (qtree_entry_unused(info, dp))
96 		return 0;
97 	return le32_to_cpu(d->dqb_id) == dquot->dq_id;
98 }
99 
100 static struct qtree_fmt_operations v2r1_fmt_ops = {
101 	.mem2disk_dqblk = v2r1_mem2diskdqblk,
102 	.disk2mem_dqblk = v2r1_disk2memdqblk,
103 	.is_id = v2r1_is_id,
104 };
105 
106 /*
107  * Copy dqinfo from disk to memory
108  */
v2_disk2memdqinfo(struct util_dqinfo * m,struct v2_disk_dqinfo * d)109 static inline void v2_disk2memdqinfo(struct util_dqinfo *m,
110 				     struct v2_disk_dqinfo *d)
111 {
112 	m->dqi_bgrace = le32_to_cpu(d->dqi_bgrace);
113 	m->dqi_igrace = le32_to_cpu(d->dqi_igrace);
114 	m->u.v2_mdqi.dqi_flags = le32_to_cpu(d->dqi_flags) & V2_DQF_MASK;
115 	m->u.v2_mdqi.dqi_qtree.dqi_blocks = le32_to_cpu(d->dqi_blocks);
116 	m->u.v2_mdqi.dqi_qtree.dqi_free_blk =
117 		le32_to_cpu(d->dqi_free_blk);
118 	m->u.v2_mdqi.dqi_qtree.dqi_free_entry =
119 				le32_to_cpu(d->dqi_free_entry);
120 }
121 
122 /*
123  * Copy dqinfo from memory to disk
124  */
v2_mem2diskdqinfo(struct v2_disk_dqinfo * d,struct util_dqinfo * m)125 static inline void v2_mem2diskdqinfo(struct v2_disk_dqinfo *d,
126 				     struct util_dqinfo *m)
127 {
128 	d->dqi_bgrace = cpu_to_le32(m->dqi_bgrace);
129 	d->dqi_igrace = cpu_to_le32(m->dqi_igrace);
130 	d->dqi_flags = cpu_to_le32(m->u.v2_mdqi.dqi_flags & V2_DQF_MASK);
131 	d->dqi_blocks = cpu_to_le32(m->u.v2_mdqi.dqi_qtree.dqi_blocks);
132 	d->dqi_free_blk =
133 		cpu_to_le32(m->u.v2_mdqi.dqi_qtree.dqi_free_blk);
134 	d->dqi_free_entry =
135 		cpu_to_le32(m->u.v2_mdqi.dqi_qtree.dqi_free_entry);
136 }
137 
v2_read_header(struct quota_handle * h,struct v2_disk_dqheader * dqh)138 static int v2_read_header(struct quota_handle *h, struct v2_disk_dqheader *dqh)
139 {
140 	if (h->read(&h->qh_qf, 0, dqh, sizeof(struct v2_disk_dqheader)) !=
141 			sizeof(struct v2_disk_dqheader))
142 		return 0;
143 
144 	return 1;
145 }
146 
147 /*
148  * Check whether given quota file is in our format
149  */
v2_check_file(struct quota_handle * h,int type)150 static int v2_check_file(struct quota_handle *h, int type)
151 {
152 	struct v2_disk_dqheader dqh;
153 	int file_magics[] = INITQMAGICS;
154 	int be_magic;
155 
156 	if (!v2_read_header(h, &dqh))
157 		return 0;
158 
159 	be_magic = be32_to_cpu((__force __be32)dqh.dqh_magic);
160 	if (be_magic == file_magics[type]) {
161 		log_err("Your quota file is stored in wrong endianity");
162 		return 0;
163 	}
164 	if (V2_VERSION != le32_to_cpu(dqh.dqh_version))
165 		return 0;
166 	return 1;
167 }
168 
169 /*
170  * Open quotafile
171  */
v2_init_io(struct quota_handle * h,enum quota_type qtype)172 static int v2_init_io(struct quota_handle *h, enum quota_type qtype)
173 {
174 	struct v2_disk_dqinfo ddqinfo;
175 	struct v2_mem_dqinfo *info;
176 	u64 filesize;
177 	struct quota_file *qf = &h->qh_qf;
178 	u32 last_blkofs = qf_last_blkofs[qtype];
179 
180 	h->qh_info.u.v2_mdqi.dqi_qtree.dqi_entry_size =
181 		sizeof(struct v2r1_disk_dqblk);
182 	h->qh_info.u.v2_mdqi.dqi_qtree.dqi_ops = &v2r1_fmt_ops;
183 
184 	/* Read information about quotafile */
185 	if (h->read(qf, V2_DQINFOOFF, &ddqinfo,
186 			sizeof(ddqinfo)) != sizeof(ddqinfo))
187 		return -1;
188 	v2_disk2memdqinfo(&h->qh_info, &ddqinfo);
189 
190 	/* Check to make sure quota file info is sane */
191 	info = &h->qh_info.u.v2_mdqi;
192 	filesize = qf->filesize = f2fs_quota_size(qf);
193 	if (qf_szchk_type[qtype] == QF_SZCHK_REGFILE &&
194 			((filesize + F2FS_BLKSIZE - 1) >> F2FS_BLKSIZE_BITS <
195 			last_blkofs + 1 || filesize > qf_maxsize[qtype])) {
196 		/*
197 		 * reqular: qf_szchk is now the last block index,
198 		 * including the hole's index
199 		 */
200 		log_err("Quota inode %u corrupted: file size %" PRIu64
201 			" does not match page offset %" PRIu32,
202 			h->qh_qf.ino,
203 			filesize,
204 			last_blkofs);
205 		filesize = (last_blkofs + 1) << F2FS_BLKSIZE_BITS;
206 		f2fs_filesize_update(qf->sbi, qf->ino, filesize);
207 	}
208 
209 	if ((info->dqi_qtree.dqi_blocks >
210 			(filesize + QT_BLKSIZE - 1) >> QT_BLKSIZE_BITS)) {
211 		log_err("Quota inode %u corrupted: file size %" PRId64 "; "
212 				"dqi_blocks %u", h->qh_qf.ino,
213 				filesize, info->dqi_qtree.dqi_blocks);
214 		return -1;
215 	}
216 	if (info->dqi_qtree.dqi_free_blk >= info->dqi_qtree.dqi_blocks) {
217 		log_err("Quota inode %u corrupted: free_blk %u;"
218 				" dqi_blocks %u",
219 				h->qh_qf.ino, info->dqi_qtree.dqi_free_blk,
220 				info->dqi_qtree.dqi_blocks);
221 		return -1;
222 	}
223 	if (info->dqi_qtree.dqi_free_entry >= info->dqi_qtree.dqi_blocks) {
224 		log_err("Quota inode %u corrupted: free_entry %u; "
225 				"dqi_blocks %u", h->qh_qf.ino,
226 				info->dqi_qtree.dqi_free_entry,
227 				info->dqi_qtree.dqi_blocks);
228 		return -1;
229 	}
230 	return 0;
231 }
232 
233 /*
234  * Initialize new quotafile
235  */
v2_new_io(struct quota_handle * h)236 static int v2_new_io(struct quota_handle *h)
237 {
238 	int file_magics[] = INITQMAGICS;
239 	struct v2_disk_dqheader ddqheader;
240 	struct v2_disk_dqinfo ddqinfo;
241 
242 	if (h->qh_fmt != QFMT_VFS_V1)
243 		return -1;
244 
245 	/* Write basic quota header */
246 	ddqheader.dqh_magic = cpu_to_le32(file_magics[h->qh_type]);
247 	ddqheader.dqh_version = cpu_to_le32(V2_VERSION);
248 	if (h->write(&h->qh_qf, 0, &ddqheader, sizeof(ddqheader)) !=
249 			sizeof(ddqheader))
250 		return -1;
251 
252 	/* Write information about quotafile */
253 	h->qh_info.dqi_bgrace = MAX_DQ_TIME;
254 	h->qh_info.dqi_igrace = MAX_IQ_TIME;
255 	h->qh_info.u.v2_mdqi.dqi_flags = 0;
256 	h->qh_info.u.v2_mdqi.dqi_qtree.dqi_blocks = QT_TREEOFF + 1;
257 	h->qh_info.u.v2_mdqi.dqi_qtree.dqi_free_blk = 0;
258 	h->qh_info.u.v2_mdqi.dqi_qtree.dqi_free_entry = 0;
259 	h->qh_info.u.v2_mdqi.dqi_qtree.dqi_entry_size =
260 				sizeof(struct v2r1_disk_dqblk);
261 	h->qh_info.u.v2_mdqi.dqi_qtree.dqi_ops = &v2r1_fmt_ops;
262 	v2_mem2diskdqinfo(&ddqinfo, &h->qh_info);
263 	if (h->write(&h->qh_qf, V2_DQINFOOFF, &ddqinfo,
264 			  sizeof(ddqinfo)) !=
265 	    sizeof(ddqinfo))
266 		return -1;
267 
268 	return 0;
269 }
270 
271 /*
272  * Write information (grace times to file)
273  */
v2_write_info(struct quota_handle * h)274 static int v2_write_info(struct quota_handle *h)
275 {
276 	struct v2_disk_dqinfo ddqinfo;
277 
278 	v2_mem2diskdqinfo(&ddqinfo, &h->qh_info);
279 	if (h->write(&h->qh_qf, V2_DQINFOOFF, &ddqinfo, sizeof(ddqinfo)) !=
280 			sizeof(ddqinfo))
281 		return -1;
282 
283 	return 0;
284 }
285 
286 /*
287  * Read dquot from disk
288  */
v2_read_dquot(struct quota_handle * h,qid_t id)289 static struct dquot *v2_read_dquot(struct quota_handle *h, qid_t id)
290 {
291 	return qtree_read_dquot(h, id);
292 }
293 
294 /*
295  * Commit changes of dquot to disk - it might also mean deleting it when quota
296  * became fake one and user has no blocks.
297  * User can process use 'errno' to detect errstr.
298  */
v2_commit_dquot(struct dquot * dquot)299 static int v2_commit_dquot(struct dquot *dquot)
300 {
301 	struct util_dqblk *b = &dquot->dq_dqb;
302 
303 	if (!b->dqb_curspace && !b->dqb_curinodes && !b->dqb_bsoftlimit &&
304 	    !b->dqb_isoftlimit && !b->dqb_bhardlimit && !b->dqb_ihardlimit)
305 	{
306 		qtree_delete_dquot(dquot);
307 	} else {
308 		return qtree_write_dquot(dquot);
309 	}
310 	return 0;
311 }
312 
v2_scan_dquots(struct quota_handle * h,int (* process_dquot)(struct dquot *,void *),void * data)313 static int v2_scan_dquots(struct quota_handle *h,
314 			  int (*process_dquot) (struct dquot *, void *),
315 			  void *data)
316 {
317 	return qtree_scan_dquots(h, process_dquot, data);
318 }
319 
320 /* Report information about quotafile.
321  * TODO: Not used right now, but we should be able to use this when we add
322  * support to debugfs to read quota files.
323  */
v2_report(struct quota_handle * UNUSED (h),int UNUSED (verbose))324 static int v2_report(struct quota_handle *UNUSED(h), int UNUSED(verbose))
325 {
326 	log_err("Not Implemented.");
327 	return -1;
328 }
329