patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / fs / quota_v1.c
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
13 MODULE_AUTHOR("Jan Kara");
14 MODULE_DESCRIPTION("Old quota format support");
15 MODULE_LICENSE("GPL");
16
17 static void v1_disk2mem_dqblk(struct mem_dqblk *m, struct v1_disk_dqblk *d)
18 {
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 = ((qsize_t)d->dqb_curblocks) << QUOTABLOCK_BITS;
25         m->dqb_itime = d->dqb_itime;
26         m->dqb_btime = d->dqb_btime;
27 }
28
29 static void v1_mem2disk_dqblk(struct v1_disk_dqblk *d, struct mem_dqblk *m)
30 {
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;
39 }
40
41 static int v1_read_dqblk(struct dquot *dquot)
42 {
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;
48
49         filp = sb_dqopt(dquot->dq_sb)->files[type];
50         if (filp == (struct file *)NULL)
51                 return -EINVAL;
52
53         /* Now we are sure filp is valid */
54         offset = v1_dqoff(dquot->dq_id);
55         /* Set structure to 0s in case read fails/is after end of file */
56         memset(&dqblk, 0, sizeof(struct v1_disk_dqblk));
57         fs = get_fs();
58         set_fs(KERNEL_DS);
59         filp->f_op->read(filp, (char *)&dqblk, sizeof(struct v1_disk_dqblk), &offset);
60         set_fs(fs);
61
62         v1_disk2mem_dqblk(&dquot->dq_dqb, &dqblk);
63         if (dquot->dq_dqb.dqb_bhardlimit == 0 && dquot->dq_dqb.dqb_bsoftlimit == 0 &&
64             dquot->dq_dqb.dqb_ihardlimit == 0 && dquot->dq_dqb.dqb_isoftlimit == 0)
65                 set_bit(DQ_FAKE_B, &dquot->dq_flags);
66         dqstats.reads++;
67
68         return 0;
69 }
70
71 static int v1_commit_dqblk(struct dquot *dquot)
72 {
73         short type = dquot->dq_type;
74         struct file *filp;
75         mm_segment_t fs;
76         loff_t offset;
77         ssize_t ret;
78         struct v1_disk_dqblk dqblk;
79
80         filp = sb_dqopt(dquot->dq_sb)->files[type];
81         offset = v1_dqoff(dquot->dq_id);
82         fs = get_fs();
83         set_fs(KERNEL_DS);
84
85         v1_mem2disk_dqblk(&dqblk, &dquot->dq_dqb);
86         if (dquot->dq_id == 0) {
87                 dqblk.dqb_btime = sb_dqopt(dquot->dq_sb)->info[type].dqi_bgrace;
88                 dqblk.dqb_itime = sb_dqopt(dquot->dq_sb)->info[type].dqi_igrace;
89         }
90         ret = 0;
91         if (filp)
92                 ret = filp->f_op->write(filp, (char *)&dqblk,
93                                         sizeof(struct v1_disk_dqblk), &offset);
94         if (ret != sizeof(struct v1_disk_dqblk)) {
95                 printk(KERN_WARNING "VFS: dquota write failed on dev %s\n",
96                         dquot->dq_sb->s_id);
97                 if (ret >= 0)
98                         ret = -EIO;
99                 goto out;
100         }
101         ret = 0;
102
103 out:
104         set_fs(fs);
105         dqstats.writes++;
106
107         return ret;
108 }
109
110 /* Magics of new quota format */
111 #define V2_INITQMAGICS {\
112         0xd9c01f11,     /* USRQUOTA */\
113         0xd9c01927      /* GRPQUOTA */\
114 }
115
116 /* Header of new quota format */
117 struct v2_disk_dqheader {
118         __u32 dqh_magic;        /* Magic number identifying file */
119         __u32 dqh_version;      /* File version */
120 };
121
122 static int v1_check_quota_file(struct super_block *sb, int type)
123 {
124         struct file *f = sb_dqopt(sb)->files[type];
125         struct inode *inode = f->f_dentry->d_inode;
126         ulong blocks;
127         size_t off; 
128         struct v2_disk_dqheader dqhead;
129         mm_segment_t fs;
130         ssize_t size;
131         loff_t offset = 0;
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);
140         if ((blocks % sizeof(struct v1_disk_dqblk) * BLOCK_SIZE + off) % sizeof(struct v1_disk_dqblk))
141                 return 0;
142         /* Doublecheck whether we didn't get file with new format - with old quotactl() this could happen */
143         fs = get_fs();
144         set_fs(KERNEL_DS);
145         size = f->f_op->read(f, (char *)&dqhead, sizeof(struct v2_disk_dqheader), &offset);
146         set_fs(fs);
147         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 */
151         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);
152         return 0;               /* Seems like a new format file -> refuse it */
153 }
154
155 static int v1_read_file_info(struct super_block *sb, int type)
156 {
157         struct quota_info *dqopt = sb_dqopt(sb);
158         mm_segment_t fs;
159         loff_t offset;
160         struct file *filp = dqopt->files[type];
161         struct v1_disk_dqblk dqblk;
162         int ret;
163
164         offset = v1_dqoff(0);
165         fs = get_fs();
166         set_fs(KERNEL_DS);
167         if ((ret = filp->f_op->read(filp, (char *)&dqblk, sizeof(struct v1_disk_dqblk), &offset)) != sizeof(struct v1_disk_dqblk)) {
168                 if (ret >= 0)
169                         ret = -EIO;
170                 goto out;
171         }
172         ret = 0;
173         dqopt->info[type].dqi_igrace = dqblk.dqb_itime ? dqblk.dqb_itime : MAX_IQ_TIME;
174         dqopt->info[type].dqi_bgrace = dqblk.dqb_btime ? dqblk.dqb_btime : MAX_DQ_TIME;
175 out:
176         set_fs(fs);
177         return ret;
178 }
179
180 static int v1_write_file_info(struct super_block *sb, int type)
181 {
182         struct quota_info *dqopt = sb_dqopt(sb);
183         mm_segment_t fs;
184         struct file *filp = dqopt->files[type];
185         struct v1_disk_dqblk dqblk;
186         loff_t offset;
187         int ret;
188
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;
206 out:
207         set_fs(fs);
208         return ret;
209 }
210
211 static struct quota_format_ops v1_format_ops = {
212         .check_quota_file       = v1_check_quota_file,
213         .read_file_info         = v1_read_file_info,
214         .write_file_info        = v1_write_file_info,
215         .free_file_info         = NULL,
216         .read_dqblk             = v1_read_dqblk,
217         .commit_dqblk           = v1_commit_dqblk,
218 };
219
220 static struct quota_format_type v1_quota_format = {
221         .qf_fmt_id      = QFMT_VFS_OLD,
222         .qf_ops         = &v1_format_ops,
223         .qf_owner       = THIS_MODULE
224 };
225
226 static int __init init_v1_quota_format(void)
227 {
228         return register_quota_format(&v1_quota_format);
229 }
230
231 static void __exit exit_v1_quota_format(void)
232 {
233         unregister_quota_format(&v1_quota_format);
234 }
235
236 module_init(init_v1_quota_format);
237 module_exit(exit_v1_quota_format);
238