1#include <linux/errno.h>
2#include <linux/fs.h>
3#include <linux/quota.h>
4#include <linux/dqblk_v1.h>
5#include <linux/quotaio_v1.h>
6#include <linux/kernel.h>
7#include <linux/init.h>
8#include <linux/module.h>
9
10#include <asm/uaccess.h>
11#include <asm/byteorder.h>
12
13static void v1_disk2mem_dqblk(struct mem_dqblk *m, struct v1_disk_dqblk *d)
14{
15 m->dqb_ihardlimit = d->dqb_ihardlimit;
16 m->dqb_isoftlimit = d->dqb_isoftlimit;
17 m->dqb_curinodes = d->dqb_curinodes;
18 m->dqb_bhardlimit = d->dqb_bhardlimit;
19 m->dqb_bsoftlimit = d->dqb_bsoftlimit;
20 m->dqb_curspace = d->dqb_curblocks << QUOTABLOCK_BITS;
21 m->dqb_itime = d->dqb_itime;
22 m->dqb_btime = d->dqb_btime;
23}
24
25static void v1_mem2disk_dqblk(struct v1_disk_dqblk *d, struct mem_dqblk *m)
26{
27 d->dqb_ihardlimit = m->dqb_ihardlimit;
28 d->dqb_isoftlimit = m->dqb_isoftlimit;
29 d->dqb_curinodes = m->dqb_curinodes;
30 d->dqb_bhardlimit = m->dqb_bhardlimit;
31 d->dqb_bsoftlimit = m->dqb_bsoftlimit;
32 d->dqb_curblocks = toqb(m->dqb_curspace);
33 d->dqb_itime = m->dqb_itime;
34 d->dqb_btime = m->dqb_btime;
35}
36
37static int v1_read_dqblk(struct dquot *dquot)
38{
39 int type = dquot->dq_type;
40 struct file *filp;
41 mm_segment_t fs;
42 loff_t offset;
43 struct v1_disk_dqblk dqblk;
44
45 filp = sb_dqopt(dquot->dq_sb)->files[type];
46 if (filp == (struct file *)NULL)
47 return -EINVAL;
48
49
50 offset = v1_dqoff(dquot->dq_id);
51 fs = get_fs();
52 set_fs(KERNEL_DS);
53 filp->f_op->read(filp, (char *)&dqblk, sizeof(struct v1_disk_dqblk), &offset);
54 set_fs(fs);
55
56 v1_disk2mem_dqblk(&dquot->dq_dqb, &dqblk);
57 if (dquot->dq_dqb.dqb_bhardlimit == 0 && dquot->dq_dqb.dqb_bsoftlimit == 0 &&
58 dquot->dq_dqb.dqb_ihardlimit == 0 && dquot->dq_dqb.dqb_isoftlimit == 0)
59 dquot->dq_flags |= DQ_FAKE;
60 dqstats.reads++;
61
62 return 0;
63}
64
65static int v1_commit_dqblk(struct dquot *dquot)
66{
67 short type = dquot->dq_type;
68 struct file *filp;
69 mm_segment_t fs;
70 loff_t offset;
71 ssize_t ret;
72 struct v1_disk_dqblk dqblk;
73
74 filp = sb_dqopt(dquot->dq_sb)->files[type];
75 offset = v1_dqoff(dquot->dq_id);
76 fs = get_fs();
77 set_fs(KERNEL_DS);
78
79
80
81
82
83 v1_mem2disk_dqblk(&dqblk, &dquot->dq_dqb);
84 dquot->dq_flags &= ~DQ_MOD;
85 if (dquot->dq_id == 0) {
86 dqblk.dqb_btime = sb_dqopt(dquot->dq_sb)->info[type].dqi_bgrace;
87 dqblk.dqb_itime = sb_dqopt(dquot->dq_sb)->info[type].dqi_igrace;
88 }
89 ret = 0;
90 if (filp)
91 ret = filp->f_op->write(filp, (char *)&dqblk,
92 sizeof(struct v1_disk_dqblk), &offset);
93 if (ret != sizeof(struct v1_disk_dqblk)) {
94 printk(KERN_WARNING "VFS: dquota write failed on dev %s\n",
95 dquot->dq_sb->s_id);
96 if (ret >= 0)
97 ret = -EIO;
98 goto out;
99 }
100 ret = 0;
101
102out:
103 set_fs(fs);
104 dqstats.writes++;
105
106 return ret;
107}
108
109
110#define V2_INITQMAGICS {\
111 0xd9c01f11, \
112 0xd9c01927 \
113}
114
115
116struct v2_disk_dqheader {
117 __u32 dqh_magic;
118 __u32 dqh_version;
119};
120
121static int v1_check_quota_file(struct super_block *sb, int type)
122{
123 struct file *f = sb_dqopt(sb)->files[type];
124 struct inode *inode = f->f_dentry->d_inode;
125 ulong blocks;
126 size_t off;
127 struct v2_disk_dqheader dqhead;
128 mm_segment_t fs;
129 ssize_t size;
130 loff_t offset = 0;
131 static const uint quota_magics[] = V2_INITQMAGICS;
132
133 if (!inode->i_size)
134 return 0;
135 blocks = inode->i_size >> BLOCK_SIZE_BITS;
136 off = inode->i_size & (BLOCK_SIZE - 1);
137 if ((blocks % sizeof(struct v1_disk_dqblk) * BLOCK_SIZE + off) % sizeof(struct v1_disk_dqblk))
138 return 0;
139
140 fs = get_fs();
141 set_fs(KERNEL_DS);
142 size = f->f_op->read(f, (char *)&dqhead, sizeof(struct v2_disk_dqheader), &offset);
143 set_fs(fs);
144 if (size != sizeof(struct v2_disk_dqheader))
145 return 1;
146 if (le32_to_cpu(dqhead.dqh_magic) != quota_magics[type])
147 return 1;
148 printk(KERN_INFO "VFS: %s: Refusing to turn on old quota format on given file. It probably contains newer quota format.\n", sb->s_id);
149 return 0;
150}
151
152static int v1_read_file_info(struct super_block *sb, int type)
153{
154 struct quota_info *dqopt = sb_dqopt(sb);
155 mm_segment_t fs;
156 loff_t offset;
157 struct file *filp = dqopt->files[type];
158 struct v1_disk_dqblk dqblk;
159 int ret;
160
161 down(&dqopt->dqio_sem);
162 offset = v1_dqoff(0);
163 fs = get_fs();
164 set_fs(KERNEL_DS);
165 if ((ret = filp->f_op->read(filp, (char *)&dqblk, sizeof(struct v1_disk_dqblk), &offset)) != sizeof(struct v1_disk_dqblk)) {
166 if (ret >= 0)
167 ret = -EIO;
168 goto out;
169 }
170 ret = 0;
171 dqopt->info[type].dqi_igrace = dqblk.dqb_itime ? dqblk.dqb_itime : MAX_IQ_TIME;
172 dqopt->info[type].dqi_bgrace = dqblk.dqb_btime ? dqblk.dqb_btime : MAX_DQ_TIME;
173out:
174 up(&dqopt->dqio_sem);
175 set_fs(fs);
176 return ret;
177}
178
179static int v1_write_file_info(struct super_block *sb, int type)
180{
181 struct quota_info *dqopt = sb_dqopt(sb);
182 mm_segment_t fs;
183 struct file *filp = dqopt->files[type];
184 struct v1_disk_dqblk dqblk;
185 loff_t offset;
186 int ret;
187
188 down(&dqopt->dqio_sem);
189 dqopt->info[type].dqi_flags &= ~DQF_INFO_DIRTY;
190 offset = v1_dqoff(0);
191 fs = get_fs();
192 set_fs(KERNEL_DS);
193 if ((ret = filp->f_op->read(filp, (char *)&dqblk, sizeof(struct v1_disk_dqblk), &offset)) != sizeof(struct v1_disk_dqblk)) {
194 if (ret >= 0)
195 ret = -EIO;
196 goto out;
197 }
198 dqblk.dqb_itime = dqopt->info[type].dqi_igrace;
199 dqblk.dqb_btime = dqopt->info[type].dqi_bgrace;
200 offset = v1_dqoff(0);
201 ret = filp->f_op->write(filp, (char *)&dqblk, sizeof(struct v1_disk_dqblk), &offset);
202 if (ret == sizeof(struct v1_disk_dqblk))
203 ret = 0;
204 else if (ret > 0)
205 ret = -EIO;
206out:
207 up(&dqopt->dqio_sem);
208 set_fs(fs);
209 return ret;
210}
211
212static struct quota_format_ops v1_format_ops = {
213 .check_quota_file = v1_check_quota_file,
214 .read_file_info = v1_read_file_info,
215 .write_file_info = v1_write_file_info,
216 .free_file_info = NULL,
217 .read_dqblk = v1_read_dqblk,
218 .commit_dqblk = v1_commit_dqblk,
219};
220
221static struct quota_format_type v1_quota_format = {
222 .qf_fmt_id = QFMT_VFS_OLD,
223 .qf_ops = &v1_format_ops,
224 .qf_owner = THIS_MODULE
225};
226
227static int __init init_v1_quota_format(void)
228{
229 return register_quota_format(&v1_quota_format);
230}
231
232static void __exit exit_v1_quota_format(void)
233{
234 unregister_quota_format(&v1_quota_format);
235}
236
237EXPORT_NO_SYMBOLS;
238
239module_init(init_v1_quota_format);
240module_exit(exit_v1_quota_format);
241
242