blob: 34b37a67bb16c6c936bd725e5fa79e76ffb2ddc0 [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{
57 int type = dquot->dq_type;
58 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,
66 sizeof(struct v1_disk_dqblk), v1_dqoff(dquot->dq_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68 v1_disk2mem_dqblk(&dquot->dq_dqb, &dqblk);
Jan Kara268157b2009-01-27 15:47:22 +010069 if (dquot->dq_dqb.dqb_bhardlimit == 0 &&
70 dquot->dq_dqb.dqb_bsoftlimit == 0 &&
71 dquot->dq_dqb.dqb_ihardlimit == 0 &&
72 dquot->dq_dqb.dqb_isoftlimit == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 set_bit(DQ_FAKE_B, &dquot->dq_flags);
Dmitry Monakhovdde95882010-04-26 20:03:33 +040074 dqstats_inc(DQST_READS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 return 0;
77}
78
79static int v1_commit_dqblk(struct dquot *dquot)
80{
81 short type = dquot->dq_type;
82 ssize_t ret;
83 struct v1_disk_dqblk dqblk;
84
85 v1_mem2disk_dqblk(&dqblk, &dquot->dq_dqb);
86 if (dquot->dq_id == 0) {
Jan Kara268157b2009-01-27 15:47:22 +010087 dqblk.dqb_btime =
88 sb_dqopt(dquot->dq_sb)->info[type].dqi_bgrace;
89 dqblk.dqb_itime =
90 sb_dqopt(dquot->dq_sb)->info[type].dqi_igrace;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 }
92 ret = 0;
93 if (sb_dqopt(dquot->dq_sb)->files[type])
Jan Kara268157b2009-01-27 15:47:22 +010094 ret = dquot->dq_sb->s_op->quota_write(dquot->dq_sb, type,
95 (char *)&dqblk, sizeof(struct v1_disk_dqblk),
96 v1_dqoff(dquot->dq_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 if (ret != sizeof(struct v1_disk_dqblk)) {
Jiaying Zhangfb5ffb02010-07-20 16:54:43 +020098 quota_error(dquot->dq_sb, "dquota write failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 if (ret >= 0)
100 ret = -EIO;
101 goto out;
102 }
103 ret = 0;
104
105out:
Dmitry Monakhovdde95882010-04-26 20:03:33 +0400106 dqstats_inc(DQST_WRITES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 return ret;
109}
110
111/* Magics of new quota format */
112#define V2_INITQMAGICS {\
113 0xd9c01f11, /* USRQUOTA */\
114 0xd9c01927 /* GRPQUOTA */\
115}
116
117/* Header of new quota format */
118struct v2_disk_dqheader {
119 __le32 dqh_magic; /* Magic number identifying file */
120 __le32 dqh_version; /* File version */
121};
122
123static int v1_check_quota_file(struct super_block *sb, int type)
124{
125 struct inode *inode = sb_dqopt(sb)->files[type];
126 ulong blocks;
127 size_t off;
128 struct v2_disk_dqheader dqhead;
129 ssize_t size;
130 loff_t isize;
131 static const uint quota_magics[] = V2_INITQMAGICS;
132
133 isize = i_size_read(inode);
134 if (!isize)
135 return 0;
136 blocks = isize >> BLOCK_SIZE_BITS;
137 off = isize & (BLOCK_SIZE - 1);
Jan Kara268157b2009-01-27 15:47:22 +0100138 if ((blocks % sizeof(struct v1_disk_dqblk) * BLOCK_SIZE + off) %
139 sizeof(struct v1_disk_dqblk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 return 0;
Jan Kara268157b2009-01-27 15:47:22 +0100141 /* Doublecheck whether we didn't get file with new format - with old
142 * quotactl() this could happen */
143 size = sb->s_op->quota_read(sb, type, (char *)&dqhead,
144 sizeof(struct v2_disk_dqheader), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 if (size != sizeof(struct v2_disk_dqheader))
146 return 1; /* Probably not new format */
147 if (le32_to_cpu(dqhead.dqh_magic) != quota_magics[type])
148 return 1; /* Definitely not new format */
Jan Kara268157b2009-01-27 15:47:22 +0100149 printk(KERN_INFO
150 "VFS: %s: Refusing to turn on old quota format on given file."
151 " It probably contains newer quota format.\n", sb->s_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 return 0; /* Seems like a new format file -> refuse it */
153}
154
155static int v1_read_file_info(struct super_block *sb, int type)
156{
157 struct quota_info *dqopt = sb_dqopt(sb);
158 struct v1_disk_dqblk dqblk;
159 int ret;
160
Jan Kara268157b2009-01-27 15:47:22 +0100161 ret = sb->s_op->quota_read(sb, type, (char *)&dqblk,
162 sizeof(struct v1_disk_dqblk), v1_dqoff(0));
163 if (ret != sizeof(struct v1_disk_dqblk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (ret >= 0)
165 ret = -EIO;
166 goto out;
167 }
168 ret = 0;
Andrew Perepechko338bf9a2008-04-28 02:14:31 -0700169 /* limits are stored as unsigned 32-bit data */
170 dqopt->info[type].dqi_maxblimit = 0xffffffff;
171 dqopt->info[type].dqi_maxilimit = 0xffffffff;
Jan Kara268157b2009-01-27 15:47:22 +0100172 dqopt->info[type].dqi_igrace =
173 dqblk.dqb_itime ? dqblk.dqb_itime : MAX_IQ_TIME;
174 dqopt->info[type].dqi_bgrace =
175 dqblk.dqb_btime ? dqblk.dqb_btime : MAX_DQ_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176out:
177 return ret;
178}
179
180static int v1_write_file_info(struct super_block *sb, int type)
181{
182 struct quota_info *dqopt = sb_dqopt(sb);
183 struct v1_disk_dqblk dqblk;
184 int ret;
185
186 dqopt->info[type].dqi_flags &= ~DQF_INFO_DIRTY;
Jan Kara268157b2009-01-27 15:47:22 +0100187 ret = sb->s_op->quota_read(sb, type, (char *)&dqblk,
188 sizeof(struct v1_disk_dqblk), v1_dqoff(0));
189 if (ret != sizeof(struct v1_disk_dqblk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 if (ret >= 0)
191 ret = -EIO;
192 goto out;
193 }
194 dqblk.dqb_itime = dqopt->info[type].dqi_igrace;
195 dqblk.dqb_btime = dqopt->info[type].dqi_bgrace;
196 ret = sb->s_op->quota_write(sb, type, (char *)&dqblk,
197 sizeof(struct v1_disk_dqblk), v1_dqoff(0));
198 if (ret == sizeof(struct v1_disk_dqblk))
199 ret = 0;
200 else if (ret > 0)
201 ret = -EIO;
202out:
203 return ret;
204}
205
Alexey Dobriyan1472da52009-10-16 15:26:03 +0400206static const struct quota_format_ops v1_format_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 .check_quota_file = v1_check_quota_file,
208 .read_file_info = v1_read_file_info,
209 .write_file_info = v1_write_file_info,
210 .free_file_info = NULL,
211 .read_dqblk = v1_read_dqblk,
212 .commit_dqblk = v1_commit_dqblk,
213};
214
215static struct quota_format_type v1_quota_format = {
216 .qf_fmt_id = QFMT_VFS_OLD,
217 .qf_ops = &v1_format_ops,
218 .qf_owner = THIS_MODULE
219};
220
221static int __init init_v1_quota_format(void)
222{
223 return register_quota_format(&v1_quota_format);
224}
225
226static void __exit exit_v1_quota_format(void)
227{
228 unregister_quota_format(&v1_quota_format);
229}
230
231module_init(init_v1_quota_format);
232module_exit(exit_v1_quota_format);
233