xref: /openbmc/linux/fs/quota/quota_v2.c (revision ae213c44)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *	vfsv0 quota IO operations on file
4  */
5 
6 #include <linux/errno.h>
7 #include <linux/fs.h>
8 #include <linux/mount.h>
9 #include <linux/dqblk_v2.h>
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/quotaops.h>
15 
16 #include <asm/byteorder.h>
17 
18 #include "quota_tree.h"
19 #include "quotaio_v2.h"
20 
21 MODULE_AUTHOR("Jan Kara");
22 MODULE_DESCRIPTION("Quota format v2 support");
23 MODULE_LICENSE("GPL");
24 
25 #define __QUOTA_V2_PARANOIA
26 
27 static void v2r0_mem2diskdqb(void *dp, struct dquot *dquot);
28 static void v2r0_disk2memdqb(struct dquot *dquot, void *dp);
29 static int v2r0_is_id(void *dp, struct dquot *dquot);
30 static void v2r1_mem2diskdqb(void *dp, struct dquot *dquot);
31 static void v2r1_disk2memdqb(struct dquot *dquot, void *dp);
32 static int v2r1_is_id(void *dp, struct dquot *dquot);
33 
34 static const struct qtree_fmt_operations v2r0_qtree_ops = {
35 	.mem2disk_dqblk = v2r0_mem2diskdqb,
36 	.disk2mem_dqblk = v2r0_disk2memdqb,
37 	.is_id = v2r0_is_id,
38 };
39 
40 static const struct qtree_fmt_operations v2r1_qtree_ops = {
41 	.mem2disk_dqblk = v2r1_mem2diskdqb,
42 	.disk2mem_dqblk = v2r1_disk2memdqb,
43 	.is_id = v2r1_is_id,
44 };
45 
46 #define QUOTABLOCK_BITS 10
47 #define QUOTABLOCK_SIZE (1 << QUOTABLOCK_BITS)
48 
49 static inline qsize_t v2_stoqb(qsize_t space)
50 {
51 	return (space + QUOTABLOCK_SIZE - 1) >> QUOTABLOCK_BITS;
52 }
53 
54 static inline qsize_t v2_qbtos(qsize_t blocks)
55 {
56 	return blocks << QUOTABLOCK_BITS;
57 }
58 
59 static int v2_read_header(struct super_block *sb, int type,
60 			  struct v2_disk_dqheader *dqhead)
61 {
62 	ssize_t size;
63 
64 	size = sb->s_op->quota_read(sb, type, (char *)dqhead,
65 				    sizeof(struct v2_disk_dqheader), 0);
66 	if (size != sizeof(struct v2_disk_dqheader)) {
67 		quota_error(sb, "Failed header read: expected=%zd got=%zd",
68 			    sizeof(struct v2_disk_dqheader), size);
69 		if (size < 0)
70 			return size;
71 		return -EIO;
72 	}
73 	return 0;
74 }
75 
76 /* Check whether given file is really vfsv0 quotafile */
77 static int v2_check_quota_file(struct super_block *sb, int type)
78 {
79 	struct v2_disk_dqheader dqhead;
80 	static const uint quota_magics[] = V2_INITQMAGICS;
81 	static const uint quota_versions[] = V2_INITQVERSIONS;
82 
83 	if (v2_read_header(sb, type, &dqhead))
84 		return 0;
85 	if (le32_to_cpu(dqhead.dqh_magic) != quota_magics[type] ||
86 	    le32_to_cpu(dqhead.dqh_version) > quota_versions[type])
87 		return 0;
88 	return 1;
89 }
90 
91 /* Read information header from quota file */
92 static int v2_read_file_info(struct super_block *sb, int type)
93 {
94 	struct v2_disk_dqinfo dinfo;
95 	struct v2_disk_dqheader dqhead;
96 	struct quota_info *dqopt = sb_dqopt(sb);
97 	struct mem_dqinfo *info = &dqopt->info[type];
98 	struct qtree_mem_dqinfo *qinfo;
99 	ssize_t size;
100 	unsigned int version;
101 	int ret;
102 
103 	down_read(&dqopt->dqio_sem);
104 	ret = v2_read_header(sb, type, &dqhead);
105 	if (ret < 0)
106 		goto out;
107 	version = le32_to_cpu(dqhead.dqh_version);
108 	if ((info->dqi_fmt_id == QFMT_VFS_V0 && version != 0) ||
109 	    (info->dqi_fmt_id == QFMT_VFS_V1 && version != 1)) {
110 		ret = -EINVAL;
111 		goto out;
112 	}
113 
114 	size = sb->s_op->quota_read(sb, type, (char *)&dinfo,
115 	       sizeof(struct v2_disk_dqinfo), V2_DQINFOOFF);
116 	if (size != sizeof(struct v2_disk_dqinfo)) {
117 		quota_error(sb, "Can't read info structure");
118 		if (size < 0)
119 			ret = size;
120 		else
121 			ret = -EIO;
122 		goto out;
123 	}
124 	info->dqi_priv = kmalloc(sizeof(struct qtree_mem_dqinfo), GFP_NOFS);
125 	if (!info->dqi_priv) {
126 		ret = -ENOMEM;
127 		goto out;
128 	}
129 	qinfo = info->dqi_priv;
130 	if (version == 0) {
131 		/* limits are stored as unsigned 32-bit data */
132 		info->dqi_max_spc_limit = 0xffffffffLL << QUOTABLOCK_BITS;
133 		info->dqi_max_ino_limit = 0xffffffff;
134 	} else {
135 		/*
136 		 * Used space is stored as unsigned 64-bit value in bytes but
137 		 * quota core supports only signed 64-bit values so use that
138 		 * as a limit
139 		 */
140 		info->dqi_max_spc_limit = 0x7fffffffffffffffLL; /* 2^63-1 */
141 		info->dqi_max_ino_limit = 0x7fffffffffffffffLL;
142 	}
143 	info->dqi_bgrace = le32_to_cpu(dinfo.dqi_bgrace);
144 	info->dqi_igrace = le32_to_cpu(dinfo.dqi_igrace);
145 	/* No flags currently supported */
146 	info->dqi_flags = 0;
147 	qinfo->dqi_sb = sb;
148 	qinfo->dqi_type = type;
149 	qinfo->dqi_blocks = le32_to_cpu(dinfo.dqi_blocks);
150 	qinfo->dqi_free_blk = le32_to_cpu(dinfo.dqi_free_blk);
151 	qinfo->dqi_free_entry = le32_to_cpu(dinfo.dqi_free_entry);
152 	qinfo->dqi_blocksize_bits = V2_DQBLKSIZE_BITS;
153 	qinfo->dqi_usable_bs = 1 << V2_DQBLKSIZE_BITS;
154 	qinfo->dqi_qtree_depth = qtree_depth(qinfo);
155 	if (version == 0) {
156 		qinfo->dqi_entry_size = sizeof(struct v2r0_disk_dqblk);
157 		qinfo->dqi_ops = &v2r0_qtree_ops;
158 	} else {
159 		qinfo->dqi_entry_size = sizeof(struct v2r1_disk_dqblk);
160 		qinfo->dqi_ops = &v2r1_qtree_ops;
161 	}
162 	ret = 0;
163 out:
164 	up_read(&dqopt->dqio_sem);
165 	return ret;
166 }
167 
168 /* Write information header to quota file */
169 static int v2_write_file_info(struct super_block *sb, int type)
170 {
171 	struct v2_disk_dqinfo dinfo;
172 	struct quota_info *dqopt = sb_dqopt(sb);
173 	struct mem_dqinfo *info = &dqopt->info[type];
174 	struct qtree_mem_dqinfo *qinfo = info->dqi_priv;
175 	ssize_t size;
176 
177 	down_write(&dqopt->dqio_sem);
178 	spin_lock(&dq_data_lock);
179 	info->dqi_flags &= ~DQF_INFO_DIRTY;
180 	dinfo.dqi_bgrace = cpu_to_le32(info->dqi_bgrace);
181 	dinfo.dqi_igrace = cpu_to_le32(info->dqi_igrace);
182 	/* No flags currently supported */
183 	dinfo.dqi_flags = cpu_to_le32(0);
184 	spin_unlock(&dq_data_lock);
185 	dinfo.dqi_blocks = cpu_to_le32(qinfo->dqi_blocks);
186 	dinfo.dqi_free_blk = cpu_to_le32(qinfo->dqi_free_blk);
187 	dinfo.dqi_free_entry = cpu_to_le32(qinfo->dqi_free_entry);
188 	size = sb->s_op->quota_write(sb, type, (char *)&dinfo,
189 	       sizeof(struct v2_disk_dqinfo), V2_DQINFOOFF);
190 	up_write(&dqopt->dqio_sem);
191 	if (size != sizeof(struct v2_disk_dqinfo)) {
192 		quota_error(sb, "Can't write info structure");
193 		return -1;
194 	}
195 	return 0;
196 }
197 
198 static void v2r0_disk2memdqb(struct dquot *dquot, void *dp)
199 {
200 	struct v2r0_disk_dqblk *d = dp, empty;
201 	struct mem_dqblk *m = &dquot->dq_dqb;
202 
203 	m->dqb_ihardlimit = le32_to_cpu(d->dqb_ihardlimit);
204 	m->dqb_isoftlimit = le32_to_cpu(d->dqb_isoftlimit);
205 	m->dqb_curinodes = le32_to_cpu(d->dqb_curinodes);
206 	m->dqb_itime = le64_to_cpu(d->dqb_itime);
207 	m->dqb_bhardlimit = v2_qbtos(le32_to_cpu(d->dqb_bhardlimit));
208 	m->dqb_bsoftlimit = v2_qbtos(le32_to_cpu(d->dqb_bsoftlimit));
209 	m->dqb_curspace = le64_to_cpu(d->dqb_curspace);
210 	m->dqb_btime = le64_to_cpu(d->dqb_btime);
211 	/* We need to escape back all-zero structure */
212 	memset(&empty, 0, sizeof(struct v2r0_disk_dqblk));
213 	empty.dqb_itime = cpu_to_le64(1);
214 	if (!memcmp(&empty, dp, sizeof(struct v2r0_disk_dqblk)))
215 		m->dqb_itime = 0;
216 }
217 
218 static void v2r0_mem2diskdqb(void *dp, struct dquot *dquot)
219 {
220 	struct v2r0_disk_dqblk *d = dp;
221 	struct mem_dqblk *m = &dquot->dq_dqb;
222 	struct qtree_mem_dqinfo *info =
223 			sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
224 
225 	d->dqb_ihardlimit = cpu_to_le32(m->dqb_ihardlimit);
226 	d->dqb_isoftlimit = cpu_to_le32(m->dqb_isoftlimit);
227 	d->dqb_curinodes = cpu_to_le32(m->dqb_curinodes);
228 	d->dqb_itime = cpu_to_le64(m->dqb_itime);
229 	d->dqb_bhardlimit = cpu_to_le32(v2_stoqb(m->dqb_bhardlimit));
230 	d->dqb_bsoftlimit = cpu_to_le32(v2_stoqb(m->dqb_bsoftlimit));
231 	d->dqb_curspace = cpu_to_le64(m->dqb_curspace);
232 	d->dqb_btime = cpu_to_le64(m->dqb_btime);
233 	d->dqb_id = cpu_to_le32(from_kqid(&init_user_ns, dquot->dq_id));
234 	if (qtree_entry_unused(info, dp))
235 		d->dqb_itime = cpu_to_le64(1);
236 }
237 
238 static int v2r0_is_id(void *dp, struct dquot *dquot)
239 {
240 	struct v2r0_disk_dqblk *d = dp;
241 	struct qtree_mem_dqinfo *info =
242 			sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
243 
244 	if (qtree_entry_unused(info, dp))
245 		return 0;
246 	return qid_eq(make_kqid(&init_user_ns, dquot->dq_id.type,
247 				le32_to_cpu(d->dqb_id)),
248 		      dquot->dq_id);
249 }
250 
251 static void v2r1_disk2memdqb(struct dquot *dquot, void *dp)
252 {
253 	struct v2r1_disk_dqblk *d = dp, empty;
254 	struct mem_dqblk *m = &dquot->dq_dqb;
255 
256 	m->dqb_ihardlimit = le64_to_cpu(d->dqb_ihardlimit);
257 	m->dqb_isoftlimit = le64_to_cpu(d->dqb_isoftlimit);
258 	m->dqb_curinodes = le64_to_cpu(d->dqb_curinodes);
259 	m->dqb_itime = le64_to_cpu(d->dqb_itime);
260 	m->dqb_bhardlimit = v2_qbtos(le64_to_cpu(d->dqb_bhardlimit));
261 	m->dqb_bsoftlimit = v2_qbtos(le64_to_cpu(d->dqb_bsoftlimit));
262 	m->dqb_curspace = le64_to_cpu(d->dqb_curspace);
263 	m->dqb_btime = le64_to_cpu(d->dqb_btime);
264 	/* We need to escape back all-zero structure */
265 	memset(&empty, 0, sizeof(struct v2r1_disk_dqblk));
266 	empty.dqb_itime = cpu_to_le64(1);
267 	if (!memcmp(&empty, dp, sizeof(struct v2r1_disk_dqblk)))
268 		m->dqb_itime = 0;
269 }
270 
271 static void v2r1_mem2diskdqb(void *dp, struct dquot *dquot)
272 {
273 	struct v2r1_disk_dqblk *d = dp;
274 	struct mem_dqblk *m = &dquot->dq_dqb;
275 	struct qtree_mem_dqinfo *info =
276 			sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
277 
278 	d->dqb_ihardlimit = cpu_to_le64(m->dqb_ihardlimit);
279 	d->dqb_isoftlimit = cpu_to_le64(m->dqb_isoftlimit);
280 	d->dqb_curinodes = cpu_to_le64(m->dqb_curinodes);
281 	d->dqb_itime = cpu_to_le64(m->dqb_itime);
282 	d->dqb_bhardlimit = cpu_to_le64(v2_stoqb(m->dqb_bhardlimit));
283 	d->dqb_bsoftlimit = cpu_to_le64(v2_stoqb(m->dqb_bsoftlimit));
284 	d->dqb_curspace = cpu_to_le64(m->dqb_curspace);
285 	d->dqb_btime = cpu_to_le64(m->dqb_btime);
286 	d->dqb_id = cpu_to_le32(from_kqid(&init_user_ns, dquot->dq_id));
287 	if (qtree_entry_unused(info, dp))
288 		d->dqb_itime = cpu_to_le64(1);
289 }
290 
291 static int v2r1_is_id(void *dp, struct dquot *dquot)
292 {
293 	struct v2r1_disk_dqblk *d = dp;
294 	struct qtree_mem_dqinfo *info =
295 			sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
296 
297 	if (qtree_entry_unused(info, dp))
298 		return 0;
299 	return qid_eq(make_kqid(&init_user_ns, dquot->dq_id.type,
300 				le32_to_cpu(d->dqb_id)),
301 		      dquot->dq_id);
302 }
303 
304 static int v2_read_dquot(struct dquot *dquot)
305 {
306 	struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
307 	int ret;
308 
309 	down_read(&dqopt->dqio_sem);
310 	ret = qtree_read_dquot(
311 			sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv,
312 			dquot);
313 	up_read(&dqopt->dqio_sem);
314 	return ret;
315 }
316 
317 static int v2_write_dquot(struct dquot *dquot)
318 {
319 	struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
320 	int ret;
321 	bool alloc = false;
322 
323 	/*
324 	 * If space for dquot is already allocated, we don't need any
325 	 * protection as we'll only overwrite the place of dquot. We are
326 	 * still protected by concurrent writes of the same dquot by
327 	 * dquot->dq_lock.
328 	 */
329 	if (!dquot->dq_off) {
330 		alloc = true;
331 		down_write(&dqopt->dqio_sem);
332 	} else {
333 		down_read(&dqopt->dqio_sem);
334 	}
335 	ret = qtree_write_dquot(
336 			sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv,
337 			dquot);
338 	if (alloc)
339 		up_write(&dqopt->dqio_sem);
340 	else
341 		up_read(&dqopt->dqio_sem);
342 	return ret;
343 }
344 
345 static int v2_release_dquot(struct dquot *dquot)
346 {
347 	struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
348 	int ret;
349 
350 	down_write(&dqopt->dqio_sem);
351 	ret = qtree_release_dquot(sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv, dquot);
352 	up_write(&dqopt->dqio_sem);
353 
354 	return ret;
355 }
356 
357 static int v2_free_file_info(struct super_block *sb, int type)
358 {
359 	kfree(sb_dqinfo(sb, type)->dqi_priv);
360 	return 0;
361 }
362 
363 static int v2_get_next_id(struct super_block *sb, struct kqid *qid)
364 {
365 	struct quota_info *dqopt = sb_dqopt(sb);
366 	int ret;
367 
368 	down_read(&dqopt->dqio_sem);
369 	ret = qtree_get_next_id(sb_dqinfo(sb, qid->type)->dqi_priv, qid);
370 	up_read(&dqopt->dqio_sem);
371 	return ret;
372 }
373 
374 static const struct quota_format_ops v2_format_ops = {
375 	.check_quota_file	= v2_check_quota_file,
376 	.read_file_info		= v2_read_file_info,
377 	.write_file_info	= v2_write_file_info,
378 	.free_file_info		= v2_free_file_info,
379 	.read_dqblk		= v2_read_dquot,
380 	.commit_dqblk		= v2_write_dquot,
381 	.release_dqblk		= v2_release_dquot,
382 	.get_next_id		= v2_get_next_id,
383 };
384 
385 static struct quota_format_type v2r0_quota_format = {
386 	.qf_fmt_id	= QFMT_VFS_V0,
387 	.qf_ops		= &v2_format_ops,
388 	.qf_owner	= THIS_MODULE
389 };
390 
391 static struct quota_format_type v2r1_quota_format = {
392 	.qf_fmt_id	= QFMT_VFS_V1,
393 	.qf_ops		= &v2_format_ops,
394 	.qf_owner	= THIS_MODULE
395 };
396 
397 static int __init init_v2_quota_format(void)
398 {
399 	int ret;
400 
401 	ret = register_quota_format(&v2r0_quota_format);
402 	if (ret)
403 		return ret;
404 	return register_quota_format(&v2r1_quota_format);
405 }
406 
407 static void __exit exit_v2_quota_format(void)
408 {
409 	unregister_quota_format(&v2r0_quota_format);
410 	unregister_quota_format(&v2r1_quota_format);
411 }
412 
413 module_init(init_v2_quota_format);
414 module_exit(exit_v2_quota_format);
415