Merge tag 'devicetree-fixes-for-4.20-1' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6-block.git] / fs / quota / quota_v1.c
CommitLineData
1da177e4
LT
1#include <linux/errno.h>
2#include <linux/fs.h>
3#include <linux/quota.h>
74abb989 4#include <linux/quotaops.h>
1da177e4 5#include <linux/dqblk_v1.h>
1da177e4
LT
6#include <linux/kernel.h>
7#include <linux/init.h>
8#include <linux/module.h>
9
10#include <asm/byteorder.h>
11
cf770c13
JK
12#include "quotaio_v1.h"
13
1da177e4
LT
14MODULE_AUTHOR("Jan Kara");
15MODULE_DESCRIPTION("Old quota format support");
16MODULE_LICENSE("GPL");
17
12095460
JK
18#define QUOTABLOCK_BITS 10
19#define QUOTABLOCK_SIZE (1 << QUOTABLOCK_BITS)
20
21static inline qsize_t v1_stoqb(qsize_t space)
22{
23 return (space + QUOTABLOCK_SIZE - 1) >> QUOTABLOCK_BITS;
24}
25
26static inline qsize_t v1_qbtos(qsize_t blocks)
27{
28 return blocks << QUOTABLOCK_BITS;
29}
30
1da177e4
LT
31static void v1_disk2mem_dqblk(struct mem_dqblk *m, struct v1_disk_dqblk *d)
32{
33 m->dqb_ihardlimit = d->dqb_ihardlimit;
34 m->dqb_isoftlimit = d->dqb_isoftlimit;
35 m->dqb_curinodes = d->dqb_curinodes;
12095460
JK
36 m->dqb_bhardlimit = v1_qbtos(d->dqb_bhardlimit);
37 m->dqb_bsoftlimit = v1_qbtos(d->dqb_bsoftlimit);
38 m->dqb_curspace = v1_qbtos(d->dqb_curblocks);
1da177e4
LT
39 m->dqb_itime = d->dqb_itime;
40 m->dqb_btime = d->dqb_btime;
41}
42
43static void v1_mem2disk_dqblk(struct v1_disk_dqblk *d, struct mem_dqblk *m)
44{
45 d->dqb_ihardlimit = m->dqb_ihardlimit;
46 d->dqb_isoftlimit = m->dqb_isoftlimit;
47 d->dqb_curinodes = m->dqb_curinodes;
12095460
JK
48 d->dqb_bhardlimit = v1_stoqb(m->dqb_bhardlimit);
49 d->dqb_bsoftlimit = v1_stoqb(m->dqb_bsoftlimit);
50 d->dqb_curblocks = v1_stoqb(m->dqb_curspace);
1da177e4
LT
51 d->dqb_itime = m->dqb_itime;
52 d->dqb_btime = m->dqb_btime;
53}
54
55static int v1_read_dqblk(struct dquot *dquot)
56{
4c376dca 57 int type = dquot->dq_id.type;
1da177e4 58 struct v1_disk_dqblk dqblk;
e342e38d 59 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
1da177e4 60
e342e38d 61 if (!dqopt->files[type])
1da177e4
LT
62 return -EINVAL;
63
64 /* Set structure to 0s in case read fails/is after end of file */
65 memset(&dqblk, 0, sizeof(struct v1_disk_dqblk));
268157ba 66 dquot->dq_sb->s_op->quota_read(dquot->dq_sb, type, (char *)&dqblk,
4c376dca
EB
67 sizeof(struct v1_disk_dqblk),
68 v1_dqoff(from_kqid(&init_user_ns, dquot->dq_id)));
1da177e4
LT
69
70 v1_disk2mem_dqblk(&dquot->dq_dqb, &dqblk);
268157ba
JK
71 if (dquot->dq_dqb.dqb_bhardlimit == 0 &&
72 dquot->dq_dqb.dqb_bsoftlimit == 0 &&
73 dquot->dq_dqb.dqb_ihardlimit == 0 &&
74 dquot->dq_dqb.dqb_isoftlimit == 0)
1da177e4 75 set_bit(DQ_FAKE_B, &dquot->dq_flags);
dde95888 76 dqstats_inc(DQST_READS);
1da177e4
LT
77
78 return 0;
79}
80
81static int v1_commit_dqblk(struct dquot *dquot)
82{
4c376dca 83 short type = dquot->dq_id.type;
1da177e4
LT
84 ssize_t ret;
85 struct v1_disk_dqblk dqblk;
86
87 v1_mem2disk_dqblk(&dqblk, &dquot->dq_dqb);
4c376dca
EB
88 if (((type == USRQUOTA) && uid_eq(dquot->dq_id.uid, GLOBAL_ROOT_UID)) ||
89 ((type == GRPQUOTA) && gid_eq(dquot->dq_id.gid, GLOBAL_ROOT_GID))) {
268157ba
JK
90 dqblk.dqb_btime =
91 sb_dqopt(dquot->dq_sb)->info[type].dqi_bgrace;
92 dqblk.dqb_itime =
93 sb_dqopt(dquot->dq_sb)->info[type].dqi_igrace;
1da177e4
LT
94 }
95 ret = 0;
96 if (sb_dqopt(dquot->dq_sb)->files[type])
268157ba
JK
97 ret = dquot->dq_sb->s_op->quota_write(dquot->dq_sb, type,
98 (char *)&dqblk, sizeof(struct v1_disk_dqblk),
4c376dca 99 v1_dqoff(from_kqid(&init_user_ns, dquot->dq_id)));
1da177e4 100 if (ret != sizeof(struct v1_disk_dqblk)) {
fb5ffb0e 101 quota_error(dquot->dq_sb, "dquota write failed");
1da177e4
LT
102 if (ret >= 0)
103 ret = -EIO;
104 goto out;
105 }
106 ret = 0;
107
108out:
dde95888 109 dqstats_inc(DQST_WRITES);
1da177e4
LT
110
111 return ret;
112}
113
114/* Magics of new quota format */
115#define V2_INITQMAGICS {\
116 0xd9c01f11, /* USRQUOTA */\
117 0xd9c01927 /* GRPQUOTA */\
118}
119
120/* Header of new quota format */
121struct v2_disk_dqheader {
122 __le32 dqh_magic; /* Magic number identifying file */
123 __le32 dqh_version; /* File version */
124};
125
126static int v1_check_quota_file(struct super_block *sb, int type)
127{
128 struct inode *inode = sb_dqopt(sb)->files[type];
129 ulong blocks;
130 size_t off;
131 struct v2_disk_dqheader dqhead;
132 ssize_t size;
133 loff_t isize;
134 static const uint quota_magics[] = V2_INITQMAGICS;
135
136 isize = i_size_read(inode);
137 if (!isize)
138 return 0;
139 blocks = isize >> BLOCK_SIZE_BITS;
140 off = isize & (BLOCK_SIZE - 1);
268157ba
JK
141 if ((blocks % sizeof(struct v1_disk_dqblk) * BLOCK_SIZE + off) %
142 sizeof(struct v1_disk_dqblk))
1da177e4 143 return 0;
268157ba
JK
144 /* Doublecheck whether we didn't get file with new format - with old
145 * quotactl() this could happen */
146 size = sb->s_op->quota_read(sb, type, (char *)&dqhead,
147 sizeof(struct v2_disk_dqheader), 0);
1da177e4
LT
148 if (size != sizeof(struct v2_disk_dqheader))
149 return 1; /* Probably not new format */
150 if (le32_to_cpu(dqhead.dqh_magic) != quota_magics[type])
151 return 1; /* Definitely not new format */
268157ba
JK
152 printk(KERN_INFO
153 "VFS: %s: Refusing to turn on old quota format on given file."
154 " It probably contains newer quota format.\n", sb->s_id);
1da177e4
LT
155 return 0; /* Seems like a new format file -> refuse it */
156}
157
158static int v1_read_file_info(struct super_block *sb, int type)
159{
160 struct quota_info *dqopt = sb_dqopt(sb);
161 struct v1_disk_dqblk dqblk;
162 int ret;
163
42fdb858 164 down_read(&dqopt->dqio_sem);
268157ba
JK
165 ret = sb->s_op->quota_read(sb, type, (char *)&dqblk,
166 sizeof(struct v1_disk_dqblk), v1_dqoff(0));
167 if (ret != sizeof(struct v1_disk_dqblk)) {
1da177e4
LT
168 if (ret >= 0)
169 ret = -EIO;
170 goto out;
171 }
172 ret = 0;
338bf9af 173 /* limits are stored as unsigned 32-bit data */
b10a0819
JK
174 dqopt->info[type].dqi_max_spc_limit = 0xffffffffULL << QUOTABLOCK_BITS;
175 dqopt->info[type].dqi_max_ino_limit = 0xffffffff;
268157ba
JK
176 dqopt->info[type].dqi_igrace =
177 dqblk.dqb_itime ? dqblk.dqb_itime : MAX_IQ_TIME;
178 dqopt->info[type].dqi_bgrace =
179 dqblk.dqb_btime ? dqblk.dqb_btime : MAX_DQ_TIME;
1da177e4 180out:
42fdb858 181 up_read(&dqopt->dqio_sem);
1da177e4
LT
182 return ret;
183}
184
185static int v1_write_file_info(struct super_block *sb, int type)
186{
187 struct quota_info *dqopt = sb_dqopt(sb);
188 struct v1_disk_dqblk dqblk;
189 int ret;
190
9a8ae30e 191 down_write(&dqopt->dqio_sem);
268157ba
JK
192 ret = sb->s_op->quota_read(sb, type, (char *)&dqblk,
193 sizeof(struct v1_disk_dqblk), v1_dqoff(0));
194 if (ret != sizeof(struct v1_disk_dqblk)) {
1da177e4
LT
195 if (ret >= 0)
196 ret = -EIO;
197 goto out;
198 }
15512377
JK
199 spin_lock(&dq_data_lock);
200 dqopt->info[type].dqi_flags &= ~DQF_INFO_DIRTY;
1da177e4
LT
201 dqblk.dqb_itime = dqopt->info[type].dqi_igrace;
202 dqblk.dqb_btime = dqopt->info[type].dqi_bgrace;
15512377 203 spin_unlock(&dq_data_lock);
1da177e4
LT
204 ret = sb->s_op->quota_write(sb, type, (char *)&dqblk,
205 sizeof(struct v1_disk_dqblk), v1_dqoff(0));
206 if (ret == sizeof(struct v1_disk_dqblk))
207 ret = 0;
208 else if (ret > 0)
209 ret = -EIO;
210out:
9a8ae30e 211 up_write(&dqopt->dqio_sem);
1da177e4
LT
212 return ret;
213}
214
1472da5f 215static const struct quota_format_ops v1_format_ops = {
1da177e4
LT
216 .check_quota_file = v1_check_quota_file,
217 .read_file_info = v1_read_file_info,
218 .write_file_info = v1_write_file_info,
219 .free_file_info = NULL,
220 .read_dqblk = v1_read_dqblk,
221 .commit_dqblk = v1_commit_dqblk,
222};
223
224static struct quota_format_type v1_quota_format = {
225 .qf_fmt_id = QFMT_VFS_OLD,
226 .qf_ops = &v1_format_ops,
227 .qf_owner = THIS_MODULE
228};
229
230static int __init init_v1_quota_format(void)
231{
232 return register_quota_format(&v1_quota_format);
233}
234
235static void __exit exit_v1_quota_format(void)
236{
237 unregister_quota_format(&v1_quota_format);
238}
239
240module_init(init_v1_quota_format);
241module_exit(exit_v1_quota_format);
242