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