blob: b1467f3921c28fc644edce213c390382668b4856 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/kernel.h>
11#include <linux/init.h>
12#include <linux/module.h>
13#include <linux/slab.h>
Jan Kara74abb982008-07-25 01:46:51 -070014#include <linux/quotaops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16#include <asm/byteorder.h>
17
Jan Kara1ccd14b2008-09-22 05:54:49 +020018#include "quota_tree.h"
Jan Karacf770c12008-09-21 23:17:53 +020019#include "quotaio_v2.h"
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021MODULE_AUTHOR("Jan Kara");
22MODULE_DESCRIPTION("Quota format v2 support");
23MODULE_LICENSE("GPL");
24
Jan Kara498c6012009-11-16 18:09:47 +010025static void v2r0_mem2diskdqb(void *dp, struct dquot *dquot);
26static void v2r0_disk2memdqb(struct dquot *dquot, void *dp);
27static int v2r0_is_id(void *dp, struct dquot *dquot);
28static void v2r1_mem2diskdqb(void *dp, struct dquot *dquot);
29static void v2r1_disk2memdqb(struct dquot *dquot, void *dp);
30static int v2r1_is_id(void *dp, struct dquot *dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Julia Lawalld1b98c22016-01-01 08:53:37 +010032static const struct qtree_fmt_operations v2r0_qtree_ops = {
Jan Kara498c6012009-11-16 18:09:47 +010033 .mem2disk_dqblk = v2r0_mem2diskdqb,
34 .disk2mem_dqblk = v2r0_disk2memdqb,
35 .is_id = v2r0_is_id,
36};
37
Julia Lawalld1b98c22016-01-01 08:53:37 +010038static const struct qtree_fmt_operations v2r1_qtree_ops = {
Jan Kara498c6012009-11-16 18:09:47 +010039 .mem2disk_dqblk = v2r1_mem2diskdqb,
40 .disk2mem_dqblk = v2r1_disk2memdqb,
41 .is_id = v2r1_is_id,
Jan Kara1ccd14b2008-09-22 05:54:49 +020042};
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Jan Kara12095462008-08-20 14:45:12 +020044#define QUOTABLOCK_BITS 10
45#define QUOTABLOCK_SIZE (1 << QUOTABLOCK_BITS)
46
47static inline qsize_t v2_stoqb(qsize_t space)
48{
49 return (space + QUOTABLOCK_SIZE - 1) >> QUOTABLOCK_BITS;
50}
51
52static inline qsize_t v2_qbtos(qsize_t blocks)
53{
54 return blocks << QUOTABLOCK_BITS;
55}
56
Jan Kara498c6012009-11-16 18:09:47 +010057static 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)) {
Jiaying Zhangfb5ffb02010-07-20 16:54:43 +020065 quota_error(sb, "Failed header read: expected=%zd got=%zd",
66 sizeof(struct v2_disk_dqheader), size);
Jan Karaf98bbe32017-08-17 19:20:28 +020067 if (size < 0)
68 return size;
69 return -EIO;
Jan Kara498c6012009-11-16 18:09:47 +010070 }
Jan Karaf98bbe32017-08-17 19:20:28 +020071 return 0;
Jan Kara498c6012009-11-16 18:09:47 +010072}
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074/* Check whether given file is really vfsv0 quotafile */
75static int v2_check_quota_file(struct super_block *sb, int type)
76{
77 struct v2_disk_dqheader dqhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 static const uint quota_magics[] = V2_INITQMAGICS;
79 static const uint quota_versions[] = V2_INITQVERSIONS;
Sascha Hauer27942ef2019-03-26 08:54:31 +010080
Jan Karaf98bbe32017-08-17 19:20:28 +020081 if (v2_read_header(sb, type, &dqhead))
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 if (le32_to_cpu(dqhead.dqh_magic) != quota_magics[type] ||
Jan Kara498c6012009-11-16 18:09:47 +010084 le32_to_cpu(dqhead.dqh_version) > quota_versions[type])
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 return 0;
86 return 1;
87}
88
89/* Read information header from quota file */
90static int v2_read_file_info(struct super_block *sb, int type)
91{
92 struct v2_disk_dqinfo dinfo;
Jan Kara498c6012009-11-16 18:09:47 +010093 struct v2_disk_dqheader dqhead;
Jan Kara42fdb852017-06-09 08:59:46 +020094 struct quota_info *dqopt = sb_dqopt(sb);
95 struct mem_dqinfo *info = &dqopt->info[type];
Jan Karae3d4d562008-10-02 18:44:14 +020096 struct qtree_mem_dqinfo *qinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 ssize_t size;
Jan Kara498c6012009-11-16 18:09:47 +010098 unsigned int version;
Jan Kara42fdb852017-06-09 08:59:46 +020099 int ret;
Jan Kara498c6012009-11-16 18:09:47 +0100100
Jan Kara42fdb852017-06-09 08:59:46 +0200101 down_read(&dqopt->dqio_sem);
Jan Karaf98bbe32017-08-17 19:20:28 +0200102 ret = v2_read_header(sb, type, &dqhead);
103 if (ret < 0)
Jan Kara42fdb852017-06-09 08:59:46 +0200104 goto out;
Jan Kara498c6012009-11-16 18:09:47 +0100105 version = le32_to_cpu(dqhead.dqh_version);
Jan Kara869835d2009-12-21 21:57:04 +0100106 if ((info->dqi_fmt_id == QFMT_VFS_V0 && version != 0) ||
Jan Kara42fdb852017-06-09 08:59:46 +0200107 (info->dqi_fmt_id == QFMT_VFS_V1 && version != 1)) {
Jan Karacb8d01b2017-06-09 09:04:47 +0200108 ret = -EINVAL;
Jan Kara42fdb852017-06-09 08:59:46 +0200109 goto out;
110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
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)) {
Jiaying Zhangfb5ffb02010-07-20 16:54:43 +0200115 quota_error(sb, "Can't read info structure");
Jan Karaf98bbe32017-08-17 19:20:28 +0200116 if (size < 0)
117 ret = size;
118 else
119 ret = -EIO;
Jan Kara42fdb852017-06-09 08:59:46 +0200120 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
Jan Karae3d4d562008-10-02 18:44:14 +0200122 info->dqi_priv = kmalloc(sizeof(struct qtree_mem_dqinfo), GFP_NOFS);
123 if (!info->dqi_priv) {
Jan Kara42fdb852017-06-09 08:59:46 +0200124 ret = -ENOMEM;
125 goto out;
Jan Karae3d4d562008-10-02 18:44:14 +0200126 }
127 qinfo = info->dqi_priv;
Jan Kara498c6012009-11-16 18:09:47 +0100128 if (version == 0) {
129 /* limits are stored as unsigned 32-bit data */
Jan Kara7e08da52015-03-04 14:42:02 +0100130 info->dqi_max_spc_limit = 0xffffffffLL << QUOTABLOCK_BITS;
Jan Karab10a0812014-10-09 16:54:13 +0200131 info->dqi_max_ino_limit = 0xffffffff;
Jan Kara498c6012009-11-16 18:09:47 +0100132 } else {
Jan Kara7e08da52015-03-04 14:42:02 +0100133 /*
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;
Jan Kara498c6012009-11-16 18:09:47 +0100140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 info->dqi_bgrace = le32_to_cpu(dinfo.dqi_bgrace);
142 info->dqi_igrace = le32_to_cpu(dinfo.dqi_igrace);
Jan Karac119c5b2014-11-19 09:03:28 +0100143 /* No flags currently supported */
144 info->dqi_flags = 0;
Jan Karae3d4d562008-10-02 18:44:14 +0200145 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);
Jan Kara498c6012009-11-16 18:09:47 +0100153 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 }
Jan Karae1963112020-11-02 16:16:29 +0100160 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]));
Jan Kara8584d4f32020-12-22 12:09:53 +0100167 goto out_free;
Jan Karae1963112020-11-02 16:16:29 +0100168 }
169 if (qinfo->dqi_free_blk >= qinfo->dqi_blocks) {
170 quota_error(sb, "Free block number too big (%u >= %u).",
171 qinfo->dqi_free_blk, qinfo->dqi_blocks);
Jan Kara8584d4f32020-12-22 12:09:53 +0100172 goto out_free;
Jan Karae1963112020-11-02 16:16:29 +0100173 }
174 if (qinfo->dqi_free_entry >= qinfo->dqi_blocks) {
175 quota_error(sb, "Block with free entry too big (%u >= %u).",
176 qinfo->dqi_free_entry, qinfo->dqi_blocks);
Jan Kara8584d4f32020-12-22 12:09:53 +0100177 goto out_free;
Jan Karae1963112020-11-02 16:16:29 +0100178 }
Jan Kara42fdb852017-06-09 08:59:46 +0200179 ret = 0;
Jan Kara8584d4f32020-12-22 12:09:53 +0100180out_free:
181 if (ret) {
182 kfree(info->dqi_priv);
183 info->dqi_priv = NULL;
184 }
Jan Kara42fdb852017-06-09 08:59:46 +0200185out:
186 up_read(&dqopt->dqio_sem);
187 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
190/* Write information header to quota file */
191static int v2_write_file_info(struct super_block *sb, int type)
192{
193 struct v2_disk_dqinfo dinfo;
Jan Kara9a8ae302017-06-09 08:45:43 +0200194 struct quota_info *dqopt = sb_dqopt(sb);
195 struct mem_dqinfo *info = &dqopt->info[type];
Jan Karae3d4d562008-10-02 18:44:14 +0200196 struct qtree_mem_dqinfo *qinfo = info->dqi_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 ssize_t size;
198
Jan Kara9a8ae302017-06-09 08:45:43 +0200199 down_write(&dqopt->dqio_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 spin_lock(&dq_data_lock);
201 info->dqi_flags &= ~DQF_INFO_DIRTY;
202 dinfo.dqi_bgrace = cpu_to_le32(info->dqi_bgrace);
203 dinfo.dqi_igrace = cpu_to_le32(info->dqi_igrace);
Jan Karac119c5b2014-11-19 09:03:28 +0100204 /* No flags currently supported */
205 dinfo.dqi_flags = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 spin_unlock(&dq_data_lock);
Jan Karae3d4d562008-10-02 18:44:14 +0200207 dinfo.dqi_blocks = cpu_to_le32(qinfo->dqi_blocks);
208 dinfo.dqi_free_blk = cpu_to_le32(qinfo->dqi_free_blk);
209 dinfo.dqi_free_entry = cpu_to_le32(qinfo->dqi_free_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 size = sb->s_op->quota_write(sb, type, (char *)&dinfo,
211 sizeof(struct v2_disk_dqinfo), V2_DQINFOOFF);
Jan Kara9a8ae302017-06-09 08:45:43 +0200212 up_write(&dqopt->dqio_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 if (size != sizeof(struct v2_disk_dqinfo)) {
Jiaying Zhangfb5ffb02010-07-20 16:54:43 +0200214 quota_error(sb, "Can't write info structure");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return -1;
216 }
217 return 0;
218}
219
Jan Kara498c6012009-11-16 18:09:47 +0100220static void v2r0_disk2memdqb(struct dquot *dquot, void *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
Jan Kara498c6012009-11-16 18:09:47 +0100222 struct v2r0_disk_dqblk *d = dp, empty;
Jan Kara1ccd14b2008-09-22 05:54:49 +0200223 struct mem_dqblk *m = &dquot->dq_dqb;
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 m->dqb_ihardlimit = le32_to_cpu(d->dqb_ihardlimit);
226 m->dqb_isoftlimit = le32_to_cpu(d->dqb_isoftlimit);
227 m->dqb_curinodes = le32_to_cpu(d->dqb_curinodes);
228 m->dqb_itime = le64_to_cpu(d->dqb_itime);
Jan Kara12095462008-08-20 14:45:12 +0200229 m->dqb_bhardlimit = v2_qbtos(le32_to_cpu(d->dqb_bhardlimit));
230 m->dqb_bsoftlimit = v2_qbtos(le32_to_cpu(d->dqb_bsoftlimit));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 m->dqb_curspace = le64_to_cpu(d->dqb_curspace);
232 m->dqb_btime = le64_to_cpu(d->dqb_btime);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200233 /* We need to escape back all-zero structure */
Jan Kara498c6012009-11-16 18:09:47 +0100234 memset(&empty, 0, sizeof(struct v2r0_disk_dqblk));
Jan Kara1ccd14b2008-09-22 05:54:49 +0200235 empty.dqb_itime = cpu_to_le64(1);
Jan Kara498c6012009-11-16 18:09:47 +0100236 if (!memcmp(&empty, dp, sizeof(struct v2r0_disk_dqblk)))
Jan Kara1ccd14b2008-09-22 05:54:49 +0200237 m->dqb_itime = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
Jan Kara498c6012009-11-16 18:09:47 +0100240static void v2r0_mem2diskdqb(void *dp, struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Jan Kara498c6012009-11-16 18:09:47 +0100242 struct v2r0_disk_dqblk *d = dp;
Jan Kara1ccd14b2008-09-22 05:54:49 +0200243 struct mem_dqblk *m = &dquot->dq_dqb;
244 struct qtree_mem_dqinfo *info =
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700245 sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
Jan Kara1ccd14b2008-09-22 05:54:49 +0200246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 d->dqb_ihardlimit = cpu_to_le32(m->dqb_ihardlimit);
248 d->dqb_isoftlimit = cpu_to_le32(m->dqb_isoftlimit);
249 d->dqb_curinodes = cpu_to_le32(m->dqb_curinodes);
250 d->dqb_itime = cpu_to_le64(m->dqb_itime);
Jan Karacf770c12008-09-21 23:17:53 +0200251 d->dqb_bhardlimit = cpu_to_le32(v2_stoqb(m->dqb_bhardlimit));
252 d->dqb_bsoftlimit = cpu_to_le32(v2_stoqb(m->dqb_bsoftlimit));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 d->dqb_curspace = cpu_to_le64(m->dqb_curspace);
254 d->dqb_btime = cpu_to_le64(m->dqb_btime);
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700255 d->dqb_id = cpu_to_le32(from_kqid(&init_user_ns, dquot->dq_id));
Jan Kara1ccd14b2008-09-22 05:54:49 +0200256 if (qtree_entry_unused(info, dp))
257 d->dqb_itime = cpu_to_le64(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
Jan Kara498c6012009-11-16 18:09:47 +0100260static int v2r0_is_id(void *dp, struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Jan Kara498c6012009-11-16 18:09:47 +0100262 struct v2r0_disk_dqblk *d = dp;
263 struct qtree_mem_dqinfo *info =
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700264 sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
Jan Kara498c6012009-11-16 18:09:47 +0100265
266 if (qtree_entry_unused(info, dp))
267 return 0;
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700268 return qid_eq(make_kqid(&init_user_ns, dquot->dq_id.type,
269 le32_to_cpu(d->dqb_id)),
270 dquot->dq_id);
Jan Kara498c6012009-11-16 18:09:47 +0100271}
272
273static void v2r1_disk2memdqb(struct dquot *dquot, void *dp)
274{
275 struct v2r1_disk_dqblk *d = dp, empty;
276 struct mem_dqblk *m = &dquot->dq_dqb;
277
278 m->dqb_ihardlimit = le64_to_cpu(d->dqb_ihardlimit);
279 m->dqb_isoftlimit = le64_to_cpu(d->dqb_isoftlimit);
280 m->dqb_curinodes = le64_to_cpu(d->dqb_curinodes);
281 m->dqb_itime = le64_to_cpu(d->dqb_itime);
282 m->dqb_bhardlimit = v2_qbtos(le64_to_cpu(d->dqb_bhardlimit));
283 m->dqb_bsoftlimit = v2_qbtos(le64_to_cpu(d->dqb_bsoftlimit));
284 m->dqb_curspace = le64_to_cpu(d->dqb_curspace);
285 m->dqb_btime = le64_to_cpu(d->dqb_btime);
286 /* We need to escape back all-zero structure */
287 memset(&empty, 0, sizeof(struct v2r1_disk_dqblk));
288 empty.dqb_itime = cpu_to_le64(1);
289 if (!memcmp(&empty, dp, sizeof(struct v2r1_disk_dqblk)))
290 m->dqb_itime = 0;
291}
292
293static void v2r1_mem2diskdqb(void *dp, struct dquot *dquot)
294{
295 struct v2r1_disk_dqblk *d = dp;
296 struct mem_dqblk *m = &dquot->dq_dqb;
297 struct qtree_mem_dqinfo *info =
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700298 sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
Jan Kara498c6012009-11-16 18:09:47 +0100299
300 d->dqb_ihardlimit = cpu_to_le64(m->dqb_ihardlimit);
301 d->dqb_isoftlimit = cpu_to_le64(m->dqb_isoftlimit);
302 d->dqb_curinodes = cpu_to_le64(m->dqb_curinodes);
303 d->dqb_itime = cpu_to_le64(m->dqb_itime);
304 d->dqb_bhardlimit = cpu_to_le64(v2_stoqb(m->dqb_bhardlimit));
305 d->dqb_bsoftlimit = cpu_to_le64(v2_stoqb(m->dqb_bsoftlimit));
306 d->dqb_curspace = cpu_to_le64(m->dqb_curspace);
307 d->dqb_btime = cpu_to_le64(m->dqb_btime);
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700308 d->dqb_id = cpu_to_le32(from_kqid(&init_user_ns, dquot->dq_id));
Eric Dumazet3d3dc272020-09-24 11:36:19 -0700309 d->dqb_pad = 0;
Jan Kara498c6012009-11-16 18:09:47 +0100310 if (qtree_entry_unused(info, dp))
311 d->dqb_itime = cpu_to_le64(1);
312}
313
314static int v2r1_is_id(void *dp, struct dquot *dquot)
315{
316 struct v2r1_disk_dqblk *d = dp;
Jan Kara1ccd14b2008-09-22 05:54:49 +0200317 struct qtree_mem_dqinfo *info =
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700318 sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Jan Kara1ccd14b2008-09-22 05:54:49 +0200320 if (qtree_entry_unused(info, dp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 return 0;
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700322 return qid_eq(make_kqid(&init_user_ns, dquot->dq_id.type,
323 le32_to_cpu(d->dqb_id)),
324 dquot->dq_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
327static int v2_read_dquot(struct dquot *dquot)
328{
Jan Karae342e382017-06-08 15:30:45 +0200329 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
330 int ret;
331
332 down_read(&dqopt->dqio_sem);
333 ret = qtree_read_dquot(
334 sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv,
335 dquot);
336 up_read(&dqopt->dqio_sem);
337 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
Jan Kara1ccd14b2008-09-22 05:54:49 +0200340static int v2_write_dquot(struct dquot *dquot)
341{
Jan Kara8fc32c22017-06-08 15:48:16 +0200342 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
343 int ret;
Jan Karad2faa412017-06-08 16:09:46 +0200344 bool alloc = false;
Jan Kara8fc32c22017-06-08 15:48:16 +0200345
Jan Karad2faa412017-06-08 16:09:46 +0200346 /*
347 * If space for dquot is already allocated, we don't need any
348 * protection as we'll only overwrite the place of dquot. We are
349 * still protected by concurrent writes of the same dquot by
350 * dquot->dq_lock.
351 */
352 if (!dquot->dq_off) {
353 alloc = true;
354 down_write(&dqopt->dqio_sem);
Jan Kara4c6bb692017-09-26 10:36:05 +0200355 } else {
356 down_read(&dqopt->dqio_sem);
Jan Karad2faa412017-06-08 16:09:46 +0200357 }
Jan Kara8fc32c22017-06-08 15:48:16 +0200358 ret = qtree_write_dquot(
359 sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv,
360 dquot);
Jan Karad2faa412017-06-08 16:09:46 +0200361 if (alloc)
362 up_write(&dqopt->dqio_sem);
Jan Kara4c6bb692017-09-26 10:36:05 +0200363 else
364 up_read(&dqopt->dqio_sem);
Jan Kara8fc32c22017-06-08 15:48:16 +0200365 return ret;
Jan Kara1ccd14b2008-09-22 05:54:49 +0200366}
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368static int v2_release_dquot(struct dquot *dquot)
369{
Jan Karab9a1a7f2017-06-08 17:20:36 +0200370 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
371 int ret;
372
373 down_write(&dqopt->dqio_sem);
374 ret = qtree_release_dquot(sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv, dquot);
375 up_write(&dqopt->dqio_sem);
376
377 return ret;
Jan Karae3d4d562008-10-02 18:44:14 +0200378}
379
380static int v2_free_file_info(struct super_block *sb, int type)
381{
382 kfree(sb_dqinfo(sb, type)->dqi_priv);
383 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
Jan Kara00663732016-01-25 20:39:27 +0100386static int v2_get_next_id(struct super_block *sb, struct kqid *qid)
387{
Jan Karaf14618c2017-06-09 08:36:16 +0200388 struct quota_info *dqopt = sb_dqopt(sb);
389 int ret;
390
391 down_read(&dqopt->dqio_sem);
392 ret = qtree_get_next_id(sb_dqinfo(sb, qid->type)->dqi_priv, qid);
393 up_read(&dqopt->dqio_sem);
394 return ret;
Jan Kara00663732016-01-25 20:39:27 +0100395}
396
Alexey Dobriyan1472da52009-10-16 15:26:03 +0400397static const struct quota_format_ops v2_format_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 .check_quota_file = v2_check_quota_file,
399 .read_file_info = v2_read_file_info,
400 .write_file_info = v2_write_file_info,
Jan Karae3d4d562008-10-02 18:44:14 +0200401 .free_file_info = v2_free_file_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 .read_dqblk = v2_read_dquot,
403 .commit_dqblk = v2_write_dquot,
404 .release_dqblk = v2_release_dquot,
Jan Kara00663732016-01-25 20:39:27 +0100405 .get_next_id = v2_get_next_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406};
407
Jan Kara498c6012009-11-16 18:09:47 +0100408static struct quota_format_type v2r0_quota_format = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 .qf_fmt_id = QFMT_VFS_V0,
410 .qf_ops = &v2_format_ops,
411 .qf_owner = THIS_MODULE
412};
413
Jan Kara498c6012009-11-16 18:09:47 +0100414static struct quota_format_type v2r1_quota_format = {
415 .qf_fmt_id = QFMT_VFS_V1,
416 .qf_ops = &v2_format_ops,
417 .qf_owner = THIS_MODULE
418};
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420static int __init init_v2_quota_format(void)
421{
Jan Kara498c6012009-11-16 18:09:47 +0100422 int ret;
423
424 ret = register_quota_format(&v2r0_quota_format);
425 if (ret)
426 return ret;
427 return register_quota_format(&v2r1_quota_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
430static void __exit exit_v2_quota_format(void)
431{
Jan Kara498c6012009-11-16 18:09:47 +0100432 unregister_quota_format(&v2r0_quota_format);
433 unregister_quota_format(&v2r1_quota_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
436module_init(init_v2_quota_format);
437module_exit(exit_v2_quota_format);