Partial rewrite.
[linux-2.6/linux-mips.git] / fs / quota_v1.c
blob6b778f34e7e74b6356f66885096ad2a592edb68b
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>
10 #include <asm/uaccess.h>
11 #include <asm/byteorder.h>
13 MODULE_AUTHOR("Jan Kara");
14 MODULE_DESCRIPTION("Old quota format support");
15 MODULE_LICENSE("GPL");
17 static void v1_disk2mem_dqblk(struct mem_dqblk *m, struct v1_disk_dqblk *d)
19 m->dqb_ihardlimit = d->dqb_ihardlimit;
20 m->dqb_isoftlimit = d->dqb_isoftlimit;
21 m->dqb_curinodes = d->dqb_curinodes;
22 m->dqb_bhardlimit = d->dqb_bhardlimit;
23 m->dqb_bsoftlimit = d->dqb_bsoftlimit;
24 m->dqb_curspace = d->dqb_curblocks << QUOTABLOCK_BITS;
25 m->dqb_itime = d->dqb_itime;
26 m->dqb_btime = d->dqb_btime;
29 static void v1_mem2disk_dqblk(struct v1_disk_dqblk *d, struct mem_dqblk *m)
31 d->dqb_ihardlimit = m->dqb_ihardlimit;
32 d->dqb_isoftlimit = m->dqb_isoftlimit;
33 d->dqb_curinodes = m->dqb_curinodes;
34 d->dqb_bhardlimit = m->dqb_bhardlimit;
35 d->dqb_bsoftlimit = m->dqb_bsoftlimit;
36 d->dqb_curblocks = toqb(m->dqb_curspace);
37 d->dqb_itime = m->dqb_itime;
38 d->dqb_btime = m->dqb_btime;
41 static int v1_read_dqblk(struct dquot *dquot)
43 int type = dquot->dq_type;
44 struct file *filp;
45 mm_segment_t fs;
46 loff_t offset;
47 struct v1_disk_dqblk dqblk;
49 filp = sb_dqopt(dquot->dq_sb)->files[type];
50 if (filp == (struct file *)NULL)
51 return -EINVAL;
53 /* Now we are sure filp is valid */
54 offset = v1_dqoff(dquot->dq_id);
55 fs = get_fs();
56 set_fs(KERNEL_DS);
57 filp->f_op->read(filp, (char *)&dqblk, sizeof(struct v1_disk_dqblk), &offset);
58 set_fs(fs);
60 v1_disk2mem_dqblk(&dquot->dq_dqb, &dqblk);
61 if (dquot->dq_dqb.dqb_bhardlimit == 0 && dquot->dq_dqb.dqb_bsoftlimit == 0 &&
62 dquot->dq_dqb.dqb_ihardlimit == 0 && dquot->dq_dqb.dqb_isoftlimit == 0)
63 dquot->dq_flags |= DQ_FAKE;
64 dqstats.reads++;
66 return 0;
69 static int v1_commit_dqblk(struct dquot *dquot)
71 short type = dquot->dq_type;
72 struct file *filp;
73 mm_segment_t fs;
74 loff_t offset;
75 ssize_t ret;
76 struct v1_disk_dqblk dqblk;
78 filp = sb_dqopt(dquot->dq_sb)->files[type];
79 offset = v1_dqoff(dquot->dq_id);
80 fs = get_fs();
81 set_fs(KERNEL_DS);
84 * Note: clear the DQ_MOD flag unconditionally,
85 * so we don't loop forever on failure.
87 v1_mem2disk_dqblk(&dqblk, &dquot->dq_dqb);
88 dquot->dq_flags &= ~DQ_MOD;
89 if (dquot->dq_id == 0) {
90 dqblk.dqb_btime = sb_dqopt(dquot->dq_sb)->info[type].dqi_bgrace;
91 dqblk.dqb_itime = sb_dqopt(dquot->dq_sb)->info[type].dqi_igrace;
93 ret = 0;
94 if (filp)
95 ret = filp->f_op->write(filp, (char *)&dqblk,
96 sizeof(struct v1_disk_dqblk), &offset);
97 if (ret != sizeof(struct v1_disk_dqblk)) {
98 printk(KERN_WARNING "VFS: dquota write failed on dev %s\n",
99 dquot->dq_sb->s_id);
100 if (ret >= 0)
101 ret = -EIO;
102 goto out;
104 ret = 0;
106 out:
107 set_fs(fs);
108 dqstats.writes++;
110 return ret;
113 /* Magics of new quota format */
114 #define V2_INITQMAGICS {\
115 0xd9c01f11, /* USRQUOTA */\
116 0xd9c01927 /* GRPQUOTA */\
119 /* Header of new quota format */
120 struct v2_disk_dqheader {
121 __u32 dqh_magic; /* Magic number identifying file */
122 __u32 dqh_version; /* File version */
125 static int v1_check_quota_file(struct super_block *sb, int type)
127 struct file *f = sb_dqopt(sb)->files[type];
128 struct inode *inode = f->f_dentry->d_inode;
129 ulong blocks;
130 size_t off;
131 struct v2_disk_dqheader dqhead;
132 mm_segment_t fs;
133 ssize_t size;
134 loff_t offset = 0;
135 static const uint quota_magics[] = V2_INITQMAGICS;
137 if (!inode->i_size)
138 return 0;
139 blocks = inode->i_size >> BLOCK_SIZE_BITS;
140 off = inode->i_size & (BLOCK_SIZE - 1);
141 if ((blocks % sizeof(struct v1_disk_dqblk) * BLOCK_SIZE + off) % sizeof(struct v1_disk_dqblk))
142 return 0;
143 /* Doublecheck whether we didn't get file with new format - with old quotactl() this could happen */
144 fs = get_fs();
145 set_fs(KERNEL_DS);
146 size = f->f_op->read(f, (char *)&dqhead, sizeof(struct v2_disk_dqheader), &offset);
147 set_fs(fs);
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 */
152 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);
153 return 0; /* Seems like a new format file -> refuse it */
156 static int v1_read_file_info(struct super_block *sb, int type)
158 struct quota_info *dqopt = sb_dqopt(sb);
159 mm_segment_t fs;
160 loff_t offset;
161 struct file *filp = dqopt->files[type];
162 struct v1_disk_dqblk dqblk;
163 int ret;
165 down(&dqopt->dqio_sem);
166 offset = v1_dqoff(0);
167 fs = get_fs();
168 set_fs(KERNEL_DS);
169 if ((ret = filp->f_op->read(filp, (char *)&dqblk, sizeof(struct v1_disk_dqblk), &offset)) != sizeof(struct v1_disk_dqblk)) {
170 if (ret >= 0)
171 ret = -EIO;
172 goto out;
174 ret = 0;
175 dqopt->info[type].dqi_igrace = dqblk.dqb_itime ? dqblk.dqb_itime : MAX_IQ_TIME;
176 dqopt->info[type].dqi_bgrace = dqblk.dqb_btime ? dqblk.dqb_btime : MAX_DQ_TIME;
177 out:
178 up(&dqopt->dqio_sem);
179 set_fs(fs);
180 return ret;
183 static int v1_write_file_info(struct super_block *sb, int type)
185 struct quota_info *dqopt = sb_dqopt(sb);
186 mm_segment_t fs;
187 struct file *filp = dqopt->files[type];
188 struct v1_disk_dqblk dqblk;
189 loff_t offset;
190 int ret;
192 down(&dqopt->dqio_sem);
193 dqopt->info[type].dqi_flags &= ~DQF_INFO_DIRTY;
194 offset = v1_dqoff(0);
195 fs = get_fs();
196 set_fs(KERNEL_DS);
197 if ((ret = filp->f_op->read(filp, (char *)&dqblk, sizeof(struct v1_disk_dqblk), &offset)) != sizeof(struct v1_disk_dqblk)) {
198 if (ret >= 0)
199 ret = -EIO;
200 goto out;
202 dqblk.dqb_itime = dqopt->info[type].dqi_igrace;
203 dqblk.dqb_btime = dqopt->info[type].dqi_bgrace;
204 offset = v1_dqoff(0);
205 ret = filp->f_op->write(filp, (char *)&dqblk, sizeof(struct v1_disk_dqblk), &offset);
206 if (ret == sizeof(struct v1_disk_dqblk))
207 ret = 0;
208 else if (ret > 0)
209 ret = -EIO;
210 out:
211 up(&dqopt->dqio_sem);
212 set_fs(fs);
213 return ret;
216 static struct quota_format_ops v1_format_ops = {
217 .check_quota_file = v1_check_quota_file,
218 .read_file_info = v1_read_file_info,
219 .write_file_info = v1_write_file_info,
220 .free_file_info = NULL,
221 .read_dqblk = v1_read_dqblk,
222 .commit_dqblk = v1_commit_dqblk,
225 static struct quota_format_type v1_quota_format = {
226 .qf_fmt_id = QFMT_VFS_OLD,
227 .qf_ops = &v1_format_ops,
228 .qf_owner = THIS_MODULE
231 static int __init init_v1_quota_format(void)
233 return register_quota_format(&v1_quota_format);
236 static void __exit exit_v1_quota_format(void)
238 unregister_quota_format(&v1_quota_format);
241 module_init(init_v1_quota_format);
242 module_exit(exit_v1_quota_format);