blob: 8fe79beced5cc05c77ad0d536fe017beeb4a2f9e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <linux/errno.h>
2#include <linux/fs.h>
3#include <linux/quota.h>
Jan Kara74abb982008-07-25 01:46:51 -07004#include <linux/quotaops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/dqblk_v1.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <linux/kernel.h>
7#include <linux/init.h>
8#include <linux/module.h>
9
10#include <asm/byteorder.h>
11
Jan Karacf770c12008-09-21 23:17:53 +020012#include "quotaio_v1.h"
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014MODULE_AUTHOR("Jan Kara");
15MODULE_DESCRIPTION("Old quota format support");
16MODULE_LICENSE("GPL");
17
Jan Kara12095462008-08-20 14:45:12 +020018#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
Linus Torvalds1da177e2005-04-16 15:20:36 -070031static 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;
Jan Kara12095462008-08-20 14:45:12 +020036 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 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;
Jan Kara12095462008-08-20 14:45:12 +020048 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 d->dqb_itime = m->dqb_itime;
52 d->dqb_btime = m->dqb_btime;
53}
54
55static int v1_read_dqblk(struct dquot *dquot)
56{
Eric W. Biederman4c376dc2012-09-16 03:56:19 -070057 int type = dquot->dq_id.type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 struct v1_disk_dqblk dqblk;
59
60 if (!sb_dqopt(dquot->dq_sb)->files[type])
61 return -EINVAL;
62
63 /* Set structure to 0s in case read fails/is after end of file */
64 memset(&dqblk, 0, sizeof(struct v1_disk_dqblk));
Jan Kara268157b2009-01-27 15:47:22 +010065 dquot->dq_sb->s_op->quota_read(dquot->dq_sb, type, (char *)&dqblk,
Eric W. Biederman4c376dc2012-09-16 03:56:19 -070066 sizeof(struct v1_disk_dqblk),
67 v1_dqoff(from_kqid(&init_user_ns, dquot->dq_id)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69 v1_disk2mem_dqblk(&dquot->dq_dqb, &dqblk);
Jan Kara268157b2009-01-27 15:47:22 +010070 if (dquot->dq_dqb.dqb_bhardlimit == 0 &&
71 dquot->dq_dqb.dqb_bsoftlimit == 0 &&
72 dquot->dq_dqb.dqb_ihardlimit == 0 &&
73 dquot->dq_dqb.dqb_isoftlimit == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 set_bit(DQ_FAKE_B, &dquot->dq_flags);
Dmitry Monakhovdde95882010-04-26 20:03:33 +040075 dqstats_inc(DQST_READS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77 return 0;
78}
79
80static int v1_commit_dqblk(struct dquot *dquot)
81{
Eric W. Biederman4c376dc2012-09-16 03:56:19 -070082 short type = dquot->dq_id.type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 ssize_t ret;
84 struct v1_disk_dqblk dqblk;
85
86 v1_mem2disk_dqblk(&dqblk, &dquot->dq_dqb);
Eric W. Biederman4c376dc2012-09-16 03:56:19 -070087 if (((type == USRQUOTA) && uid_eq(dquot->dq_id.uid, GLOBAL_ROOT_UID)) ||
88 ((type == GRPQUOTA) && gid_eq(dquot->dq_id.gid, GLOBAL_ROOT_GID))) {
Jan Kara268157b2009-01-27 15:47:22 +010089 dqblk.dqb_btime =
90 sb_dqopt(dquot->dq_sb)->info[type].dqi_bgrace;
91 dqblk.dqb_itime =
92 sb_dqopt(dquot->dq_sb)->info[type].dqi_igrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 }
94 ret = 0;
95 if (sb_dqopt(dquot->dq_sb)->files[type])
Jan Kara268157b2009-01-27 15:47:22 +010096 ret = dquot->dq_sb->s_op->quota_write(dquot->dq_sb, type,
97 (char *)&dqblk, sizeof(struct v1_disk_dqblk),
Eric W. Biederman4c376dc2012-09-16 03:56:19 -070098 v1_dqoff(from_kqid(&init_user_ns, dquot->dq_id)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 if (ret != sizeof(struct v1_disk_dqblk)) {
Jiaying Zhangfb5ffb02010-07-20 16:54:43 +0200100 quota_error(dquot->dq_sb, "dquota write failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 if (ret >= 0)
102 ret = -EIO;
103 goto out;
104 }
105 ret = 0;
106
107out:
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400108 dqstats_inc(DQST_WRITES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 return ret;
111}
112
113/* Magics of new quota format */
114#define V2_INITQMAGICS {\
115 0xd9c01f11, /* USRQUOTA */\
116 0xd9c01927 /* GRPQUOTA */\
117}
118
119/* Header of new quota format */
120struct v2_disk_dqheader {
121 __le32 dqh_magic; /* Magic number identifying file */
122 __le32 dqh_version; /* File version */
123};
124
125static int v1_check_quota_file(struct super_block *sb, int type)
126{
127 struct inode *inode = sb_dqopt(sb)->files[type];
128 ulong blocks;
129 size_t off;
130 struct v2_disk_dqheader dqhead;
131 ssize_t size;
132 loff_t isize;
133 static const uint quota_magics[] = V2_INITQMAGICS;
134
135 isize = i_size_read(inode);
136 if (!isize)
137 return 0;
138 blocks = isize >> BLOCK_SIZE_BITS;
139 off = isize & (BLOCK_SIZE - 1);
Jan Kara268157b2009-01-27 15:47:22 +0100140 if ((blocks % sizeof(struct v1_disk_dqblk) * BLOCK_SIZE + off) %
141 sizeof(struct v1_disk_dqblk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return 0;
Jan Kara268157b2009-01-27 15:47:22 +0100143 /* Doublecheck whether we didn't get file with new format - with old
144 * quotactl() this could happen */
145 size = sb->s_op->quota_read(sb, type, (char *)&dqhead,
146 sizeof(struct v2_disk_dqheader), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 if (size != sizeof(struct v2_disk_dqheader))
148 return 1; /* Probably not new format */
149 if (le32_to_cpu(dqhead.dqh_magic) != quota_magics[type])
150 return 1; /* Definitely not new format */
Jan Kara268157b2009-01-27 15:47:22 +0100151 printk(KERN_INFO
152 "VFS: %s: Refusing to turn on old quota format on given file."
153 " It probably contains newer quota format.\n", sb->s_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 return 0; /* Seems like a new format file -> refuse it */
155}
156
157static int v1_read_file_info(struct super_block *sb, int type)
158{
159 struct quota_info *dqopt = sb_dqopt(sb);
160 struct v1_disk_dqblk dqblk;
161 int ret;
162
Jan Kara268157b2009-01-27 15:47:22 +0100163 ret = sb->s_op->quota_read(sb, type, (char *)&dqblk,
164 sizeof(struct v1_disk_dqblk), v1_dqoff(0));
165 if (ret != sizeof(struct v1_disk_dqblk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 if (ret >= 0)
167 ret = -EIO;
168 goto out;
169 }
170 ret = 0;
Andrew Perepechko338bf9a2008-04-28 02:14:31 -0700171 /* limits are stored as unsigned 32-bit data */
Jan Karab10a0812014-10-09 16:54:13 +0200172 dqopt->info[type].dqi_max_spc_limit = 0xffffffffULL << QUOTABLOCK_BITS;
173 dqopt->info[type].dqi_max_ino_limit = 0xffffffff;
Jan Kara268157b2009-01-27 15:47:22 +0100174 dqopt->info[type].dqi_igrace =
175 dqblk.dqb_itime ? dqblk.dqb_itime : MAX_IQ_TIME;
176 dqopt->info[type].dqi_bgrace =
177 dqblk.dqb_btime ? dqblk.dqb_btime : MAX_DQ_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178out:
179 return ret;
180}
181
182static int v1_write_file_info(struct super_block *sb, int type)
183{
184 struct quota_info *dqopt = sb_dqopt(sb);
185 struct v1_disk_dqblk dqblk;
186 int ret;
187
188 dqopt->info[type].dqi_flags &= ~DQF_INFO_DIRTY;
Jan Kara268157b2009-01-27 15:47:22 +0100189 ret = sb->s_op->quota_read(sb, type, (char *)&dqblk,
190 sizeof(struct v1_disk_dqblk), v1_dqoff(0));
191 if (ret != sizeof(struct v1_disk_dqblk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 if (ret >= 0)
193 ret = -EIO;
194 goto out;
195 }
196 dqblk.dqb_itime = dqopt->info[type].dqi_igrace;
197 dqblk.dqb_btime = dqopt->info[type].dqi_bgrace;
198 ret = sb->s_op->quota_write(sb, type, (char *)&dqblk,
199 sizeof(struct v1_disk_dqblk), v1_dqoff(0));
200 if (ret == sizeof(struct v1_disk_dqblk))
201 ret = 0;
202 else if (ret > 0)
203 ret = -EIO;
204out:
205 return ret;
206}
207
Alexey Dobriyan1472da52009-10-16 15:26:03 +0400208static const struct quota_format_ops v1_format_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 .check_quota_file = v1_check_quota_file,
210 .read_file_info = v1_read_file_info,
211 .write_file_info = v1_write_file_info,
212 .free_file_info = NULL,
213 .read_dqblk = v1_read_dqblk,
214 .commit_dqblk = v1_commit_dqblk,
215};
216
217static struct quota_format_type v1_quota_format = {
218 .qf_fmt_id = QFMT_VFS_OLD,
219 .qf_ops = &v1_format_ops,
220 .qf_owner = THIS_MODULE
221};
222
223static int __init init_v1_quota_format(void)
224{
225 return register_quota_format(&v1_quota_format);
226}
227
228static void __exit exit_v1_quota_format(void)
229{
230 unregister_quota_format(&v1_quota_format);
231}
232
233module_init(init_v1_quota_format);
234module_exit(exit_v1_quota_format);
235