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