GNU Linux-libre 5.4.257-gnu1
[releases.git] / fs / quota / quota_v2.c
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 = -EUCLEAN;
163         /* Some sanity checks of the read headers... */
164         if ((loff_t)qinfo->dqi_blocks << qinfo->dqi_blocksize_bits >
165             i_size_read(sb_dqopt(sb)->files[type])) {
166                 quota_error(sb, "Number of blocks too big for quota file size (%llu > %llu).",
167                     (loff_t)qinfo->dqi_blocks << qinfo->dqi_blocksize_bits,
168                     i_size_read(sb_dqopt(sb)->files[type]));
169                 goto out_free;
170         }
171         if (qinfo->dqi_free_blk >= qinfo->dqi_blocks) {
172                 quota_error(sb, "Free block number too big (%u >= %u).",
173                             qinfo->dqi_free_blk, qinfo->dqi_blocks);
174                 goto out_free;
175         }
176         if (qinfo->dqi_free_entry >= qinfo->dqi_blocks) {
177                 quota_error(sb, "Block with free entry too big (%u >= %u).",
178                             qinfo->dqi_free_entry, qinfo->dqi_blocks);
179                 goto out_free;
180         }
181         ret = 0;
182 out_free:
183         if (ret) {
184                 kfree(info->dqi_priv);
185                 info->dqi_priv = NULL;
186         }
187 out:
188         up_read(&dqopt->dqio_sem);
189         return ret;
190 }
191
192 /* Write information header to quota file */
193 static int v2_write_file_info(struct super_block *sb, int type)
194 {
195         struct v2_disk_dqinfo dinfo;
196         struct quota_info *dqopt = sb_dqopt(sb);
197         struct mem_dqinfo *info = &dqopt->info[type];
198         struct qtree_mem_dqinfo *qinfo = info->dqi_priv;
199         ssize_t size;
200
201         down_write(&dqopt->dqio_sem);
202         spin_lock(&dq_data_lock);
203         info->dqi_flags &= ~DQF_INFO_DIRTY;
204         dinfo.dqi_bgrace = cpu_to_le32(info->dqi_bgrace);
205         dinfo.dqi_igrace = cpu_to_le32(info->dqi_igrace);
206         /* No flags currently supported */
207         dinfo.dqi_flags = cpu_to_le32(0);
208         spin_unlock(&dq_data_lock);
209         dinfo.dqi_blocks = cpu_to_le32(qinfo->dqi_blocks);
210         dinfo.dqi_free_blk = cpu_to_le32(qinfo->dqi_free_blk);
211         dinfo.dqi_free_entry = cpu_to_le32(qinfo->dqi_free_entry);
212         size = sb->s_op->quota_write(sb, type, (char *)&dinfo,
213                sizeof(struct v2_disk_dqinfo), V2_DQINFOOFF);
214         up_write(&dqopt->dqio_sem);
215         if (size != sizeof(struct v2_disk_dqinfo)) {
216                 quota_error(sb, "Can't write info structure");
217                 return -1;
218         }
219         return 0;
220 }
221
222 static void v2r0_disk2memdqb(struct dquot *dquot, void *dp)
223 {
224         struct v2r0_disk_dqblk *d = dp, empty;
225         struct mem_dqblk *m = &dquot->dq_dqb;
226
227         m->dqb_ihardlimit = le32_to_cpu(d->dqb_ihardlimit);
228         m->dqb_isoftlimit = le32_to_cpu(d->dqb_isoftlimit);
229         m->dqb_curinodes = le32_to_cpu(d->dqb_curinodes);
230         m->dqb_itime = le64_to_cpu(d->dqb_itime);
231         m->dqb_bhardlimit = v2_qbtos(le32_to_cpu(d->dqb_bhardlimit));
232         m->dqb_bsoftlimit = v2_qbtos(le32_to_cpu(d->dqb_bsoftlimit));
233         m->dqb_curspace = le64_to_cpu(d->dqb_curspace);
234         m->dqb_btime = le64_to_cpu(d->dqb_btime);
235         /* We need to escape back all-zero structure */
236         memset(&empty, 0, sizeof(struct v2r0_disk_dqblk));
237         empty.dqb_itime = cpu_to_le64(1);
238         if (!memcmp(&empty, dp, sizeof(struct v2r0_disk_dqblk)))
239                 m->dqb_itime = 0;
240 }
241
242 static void v2r0_mem2diskdqb(void *dp, struct dquot *dquot)
243 {
244         struct v2r0_disk_dqblk *d = dp;
245         struct mem_dqblk *m = &dquot->dq_dqb;
246         struct qtree_mem_dqinfo *info =
247                         sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
248
249         d->dqb_ihardlimit = cpu_to_le32(m->dqb_ihardlimit);
250         d->dqb_isoftlimit = cpu_to_le32(m->dqb_isoftlimit);
251         d->dqb_curinodes = cpu_to_le32(m->dqb_curinodes);
252         d->dqb_itime = cpu_to_le64(m->dqb_itime);
253         d->dqb_bhardlimit = cpu_to_le32(v2_stoqb(m->dqb_bhardlimit));
254         d->dqb_bsoftlimit = cpu_to_le32(v2_stoqb(m->dqb_bsoftlimit));
255         d->dqb_curspace = cpu_to_le64(m->dqb_curspace);
256         d->dqb_btime = cpu_to_le64(m->dqb_btime);
257         d->dqb_id = cpu_to_le32(from_kqid(&init_user_ns, dquot->dq_id));
258         if (qtree_entry_unused(info, dp))
259                 d->dqb_itime = cpu_to_le64(1);
260 }
261
262 static int v2r0_is_id(void *dp, struct dquot *dquot)
263 {
264         struct v2r0_disk_dqblk *d = dp;
265         struct qtree_mem_dqinfo *info =
266                         sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
267
268         if (qtree_entry_unused(info, dp))
269                 return 0;
270         return qid_eq(make_kqid(&init_user_ns, dquot->dq_id.type,
271                                 le32_to_cpu(d->dqb_id)),
272                       dquot->dq_id);
273 }
274
275 static void v2r1_disk2memdqb(struct dquot *dquot, void *dp)
276 {
277         struct v2r1_disk_dqblk *d = dp, empty;
278         struct mem_dqblk *m = &dquot->dq_dqb;
279
280         m->dqb_ihardlimit = le64_to_cpu(d->dqb_ihardlimit);
281         m->dqb_isoftlimit = le64_to_cpu(d->dqb_isoftlimit);
282         m->dqb_curinodes = le64_to_cpu(d->dqb_curinodes);
283         m->dqb_itime = le64_to_cpu(d->dqb_itime);
284         m->dqb_bhardlimit = v2_qbtos(le64_to_cpu(d->dqb_bhardlimit));
285         m->dqb_bsoftlimit = v2_qbtos(le64_to_cpu(d->dqb_bsoftlimit));
286         m->dqb_curspace = le64_to_cpu(d->dqb_curspace);
287         m->dqb_btime = le64_to_cpu(d->dqb_btime);
288         /* We need to escape back all-zero structure */
289         memset(&empty, 0, sizeof(struct v2r1_disk_dqblk));
290         empty.dqb_itime = cpu_to_le64(1);
291         if (!memcmp(&empty, dp, sizeof(struct v2r1_disk_dqblk)))
292                 m->dqb_itime = 0;
293 }
294
295 static void v2r1_mem2diskdqb(void *dp, struct dquot *dquot)
296 {
297         struct v2r1_disk_dqblk *d = dp;
298         struct mem_dqblk *m = &dquot->dq_dqb;
299         struct qtree_mem_dqinfo *info =
300                         sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
301
302         d->dqb_ihardlimit = cpu_to_le64(m->dqb_ihardlimit);
303         d->dqb_isoftlimit = cpu_to_le64(m->dqb_isoftlimit);
304         d->dqb_curinodes = cpu_to_le64(m->dqb_curinodes);
305         d->dqb_itime = cpu_to_le64(m->dqb_itime);
306         d->dqb_bhardlimit = cpu_to_le64(v2_stoqb(m->dqb_bhardlimit));
307         d->dqb_bsoftlimit = cpu_to_le64(v2_stoqb(m->dqb_bsoftlimit));
308         d->dqb_curspace = cpu_to_le64(m->dqb_curspace);
309         d->dqb_btime = cpu_to_le64(m->dqb_btime);
310         d->dqb_id = cpu_to_le32(from_kqid(&init_user_ns, dquot->dq_id));
311         d->dqb_pad = 0;
312         if (qtree_entry_unused(info, dp))
313                 d->dqb_itime = cpu_to_le64(1);
314 }
315
316 static int v2r1_is_id(void *dp, struct dquot *dquot)
317 {
318         struct v2r1_disk_dqblk *d = dp;
319         struct qtree_mem_dqinfo *info =
320                         sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
321
322         if (qtree_entry_unused(info, dp))
323                 return 0;
324         return qid_eq(make_kqid(&init_user_ns, dquot->dq_id.type,
325                                 le32_to_cpu(d->dqb_id)),
326                       dquot->dq_id);
327 }
328
329 static int v2_read_dquot(struct dquot *dquot)
330 {
331         struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
332         int ret;
333
334         down_read(&dqopt->dqio_sem);
335         ret = qtree_read_dquot(
336                         sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv,
337                         dquot);
338         up_read(&dqopt->dqio_sem);
339         return ret;
340 }
341
342 static int v2_write_dquot(struct dquot *dquot)
343 {
344         struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
345         int ret;
346         bool alloc = false;
347
348         /*
349          * If space for dquot is already allocated, we don't need any
350          * protection as we'll only overwrite the place of dquot. We are
351          * still protected by concurrent writes of the same dquot by
352          * dquot->dq_lock.
353          */
354         if (!dquot->dq_off) {
355                 alloc = true;
356                 down_write(&dqopt->dqio_sem);
357         } else {
358                 down_read(&dqopt->dqio_sem);
359         }
360         ret = qtree_write_dquot(
361                         sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv,
362                         dquot);
363         if (alloc)
364                 up_write(&dqopt->dqio_sem);
365         else
366                 up_read(&dqopt->dqio_sem);
367         return ret;
368 }
369
370 static int v2_release_dquot(struct dquot *dquot)
371 {
372         struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
373         int ret;
374
375         down_write(&dqopt->dqio_sem);
376         ret = qtree_release_dquot(sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv, dquot);
377         up_write(&dqopt->dqio_sem);
378
379         return ret;
380 }
381
382 static int v2_free_file_info(struct super_block *sb, int type)
383 {
384         kfree(sb_dqinfo(sb, type)->dqi_priv);
385         return 0;
386 }
387
388 static int v2_get_next_id(struct super_block *sb, struct kqid *qid)
389 {
390         struct quota_info *dqopt = sb_dqopt(sb);
391         int ret;
392
393         down_read(&dqopt->dqio_sem);
394         ret = qtree_get_next_id(sb_dqinfo(sb, qid->type)->dqi_priv, qid);
395         up_read(&dqopt->dqio_sem);
396         return ret;
397 }
398
399 static const struct quota_format_ops v2_format_ops = {
400         .check_quota_file       = v2_check_quota_file,
401         .read_file_info         = v2_read_file_info,
402         .write_file_info        = v2_write_file_info,
403         .free_file_info         = v2_free_file_info,
404         .read_dqblk             = v2_read_dquot,
405         .commit_dqblk           = v2_write_dquot,
406         .release_dqblk          = v2_release_dquot,
407         .get_next_id            = v2_get_next_id,
408 };
409
410 static struct quota_format_type v2r0_quota_format = {
411         .qf_fmt_id      = QFMT_VFS_V0,
412         .qf_ops         = &v2_format_ops,
413         .qf_owner       = THIS_MODULE
414 };
415
416 static struct quota_format_type v2r1_quota_format = {
417         .qf_fmt_id      = QFMT_VFS_V1,
418         .qf_ops         = &v2_format_ops,
419         .qf_owner       = THIS_MODULE
420 };
421
422 static int __init init_v2_quota_format(void)
423 {
424         int ret;
425
426         ret = register_quota_format(&v2r0_quota_format);
427         if (ret)
428                 return ret;
429         return register_quota_format(&v2r1_quota_format);
430 }
431
432 static void __exit exit_v2_quota_format(void)
433 {
434         unregister_quota_format(&v2r0_quota_format);
435         unregister_quota_format(&v2r1_quota_format);
436 }
437
438 module_init(init_v2_quota_format);
439 module_exit(exit_v2_quota_format);