quota: Increase size of variables for limits and inode usage
[pandora-kernel.git] / fs / quota_v1.c
1 #include <linux/errno.h>
2 #include <linux/fs.h>
3 #include <linux/quota.h>
4 #include <linux/quotaops.h>
5 #include <linux/dqblk_v1.h>
6 #include <linux/quotaio_v1.h>
7 #include <linux/kernel.h>
8 #include <linux/init.h>
9 #include <linux/module.h>
10
11 #include <asm/byteorder.h>
12
13 MODULE_AUTHOR("Jan Kara");
14 MODULE_DESCRIPTION("Old quota format support");
15 MODULE_LICENSE("GPL");
16
17 #define QUOTABLOCK_BITS 10
18 #define QUOTABLOCK_SIZE (1 << QUOTABLOCK_BITS)
19
20 static inline qsize_t v1_stoqb(qsize_t space)
21 {
22         return (space + QUOTABLOCK_SIZE - 1) >> QUOTABLOCK_BITS;
23 }
24
25 static inline qsize_t v1_qbtos(qsize_t blocks)
26 {
27         return blocks << QUOTABLOCK_BITS;
28 }
29
30 static void v1_disk2mem_dqblk(struct mem_dqblk *m, struct v1_disk_dqblk *d)
31 {
32         m->dqb_ihardlimit = d->dqb_ihardlimit;
33         m->dqb_isoftlimit = d->dqb_isoftlimit;
34         m->dqb_curinodes = d->dqb_curinodes;
35         m->dqb_bhardlimit = v1_qbtos(d->dqb_bhardlimit);
36         m->dqb_bsoftlimit = v1_qbtos(d->dqb_bsoftlimit);
37         m->dqb_curspace = v1_qbtos(d->dqb_curblocks);
38         m->dqb_itime = d->dqb_itime;
39         m->dqb_btime = d->dqb_btime;
40 }
41
42 static void v1_mem2disk_dqblk(struct v1_disk_dqblk *d, struct mem_dqblk *m)
43 {
44         d->dqb_ihardlimit = m->dqb_ihardlimit;
45         d->dqb_isoftlimit = m->dqb_isoftlimit;
46         d->dqb_curinodes = m->dqb_curinodes;
47         d->dqb_bhardlimit = v1_stoqb(m->dqb_bhardlimit);
48         d->dqb_bsoftlimit = v1_stoqb(m->dqb_bsoftlimit);
49         d->dqb_curblocks = v1_stoqb(m->dqb_curspace);
50         d->dqb_itime = m->dqb_itime;
51         d->dqb_btime = m->dqb_btime;
52 }
53
54 static int v1_read_dqblk(struct dquot *dquot)
55 {
56         int type = dquot->dq_type;
57         struct v1_disk_dqblk dqblk;
58
59         if (!sb_dqopt(dquot->dq_sb)->files[type])
60                 return -EINVAL;
61
62         /* Set structure to 0s in case read fails/is after end of file */
63         memset(&dqblk, 0, sizeof(struct v1_disk_dqblk));
64         dquot->dq_sb->s_op->quota_read(dquot->dq_sb, type, (char *)&dqblk, sizeof(struct v1_disk_dqblk), v1_dqoff(dquot->dq_id));
65
66         v1_disk2mem_dqblk(&dquot->dq_dqb, &dqblk);
67         if (dquot->dq_dqb.dqb_bhardlimit == 0 && dquot->dq_dqb.dqb_bsoftlimit == 0 &&
68             dquot->dq_dqb.dqb_ihardlimit == 0 && dquot->dq_dqb.dqb_isoftlimit == 0)
69                 set_bit(DQ_FAKE_B, &dquot->dq_flags);
70         dqstats.reads++;
71
72         return 0;
73 }
74
75 static int v1_commit_dqblk(struct dquot *dquot)
76 {
77         short type = dquot->dq_type;
78         ssize_t ret;
79         struct v1_disk_dqblk dqblk;
80
81         v1_mem2disk_dqblk(&dqblk, &dquot->dq_dqb);
82         if (dquot->dq_id == 0) {
83                 dqblk.dqb_btime = sb_dqopt(dquot->dq_sb)->info[type].dqi_bgrace;
84                 dqblk.dqb_itime = sb_dqopt(dquot->dq_sb)->info[type].dqi_igrace;
85         }
86         ret = 0;
87         if (sb_dqopt(dquot->dq_sb)->files[type])
88                 ret = dquot->dq_sb->s_op->quota_write(dquot->dq_sb, type, (char *)&dqblk,
89                                         sizeof(struct v1_disk_dqblk), v1_dqoff(dquot->dq_id));
90         if (ret != sizeof(struct v1_disk_dqblk)) {
91                 printk(KERN_WARNING "VFS: dquota write failed on dev %s\n",
92                         dquot->dq_sb->s_id);
93                 if (ret >= 0)
94                         ret = -EIO;
95                 goto out;
96         }
97         ret = 0;
98
99 out:
100         dqstats.writes++;
101
102         return ret;
103 }
104
105 /* Magics of new quota format */
106 #define V2_INITQMAGICS {\
107         0xd9c01f11,     /* USRQUOTA */\
108         0xd9c01927      /* GRPQUOTA */\
109 }
110
111 /* Header of new quota format */
112 struct v2_disk_dqheader {
113         __le32 dqh_magic;        /* Magic number identifying file */
114         __le32 dqh_version;      /* File version */
115 };
116
117 static int v1_check_quota_file(struct super_block *sb, int type)
118 {
119         struct inode *inode = sb_dqopt(sb)->files[type];
120         ulong blocks;
121         size_t off; 
122         struct v2_disk_dqheader dqhead;
123         ssize_t size;
124         loff_t isize;
125         static const uint quota_magics[] = V2_INITQMAGICS;
126
127         isize = i_size_read(inode);
128         if (!isize)
129                 return 0;
130         blocks = isize >> BLOCK_SIZE_BITS;
131         off = isize & (BLOCK_SIZE - 1);
132         if ((blocks % sizeof(struct v1_disk_dqblk) * BLOCK_SIZE + off) % sizeof(struct v1_disk_dqblk))
133                 return 0;
134         /* Doublecheck whether we didn't get file with new format - with old quotactl() this could happen */
135         size = sb->s_op->quota_read(sb, type, (char *)&dqhead, sizeof(struct v2_disk_dqheader), 0);
136         if (size != sizeof(struct v2_disk_dqheader))
137                 return 1;       /* Probably not new format */
138         if (le32_to_cpu(dqhead.dqh_magic) != quota_magics[type])
139                 return 1;       /* Definitely not new format */
140         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);
141         return 0;               /* Seems like a new format file -> refuse it */
142 }
143
144 static int v1_read_file_info(struct super_block *sb, int type)
145 {
146         struct quota_info *dqopt = sb_dqopt(sb);
147         struct v1_disk_dqblk dqblk;
148         int ret;
149
150         if ((ret = sb->s_op->quota_read(sb, type, (char *)&dqblk, sizeof(struct v1_disk_dqblk), v1_dqoff(0))) != sizeof(struct v1_disk_dqblk)) {
151                 if (ret >= 0)
152                         ret = -EIO;
153                 goto out;
154         }
155         ret = 0;
156         /* limits are stored as unsigned 32-bit data */
157         dqopt->info[type].dqi_maxblimit = 0xffffffff;
158         dqopt->info[type].dqi_maxilimit = 0xffffffff;
159         dqopt->info[type].dqi_igrace = dqblk.dqb_itime ? dqblk.dqb_itime : MAX_IQ_TIME;
160         dqopt->info[type].dqi_bgrace = dqblk.dqb_btime ? dqblk.dqb_btime : MAX_DQ_TIME;
161 out:
162         return ret;
163 }
164
165 static int v1_write_file_info(struct super_block *sb, int type)
166 {
167         struct quota_info *dqopt = sb_dqopt(sb);
168         struct v1_disk_dqblk dqblk;
169         int ret;
170
171         dqopt->info[type].dqi_flags &= ~DQF_INFO_DIRTY;
172         if ((ret = sb->s_op->quota_read(sb, type, (char *)&dqblk,
173             sizeof(struct v1_disk_dqblk), v1_dqoff(0))) != sizeof(struct v1_disk_dqblk)) {
174                 if (ret >= 0)
175                         ret = -EIO;
176                 goto out;
177         }
178         dqblk.dqb_itime = dqopt->info[type].dqi_igrace;
179         dqblk.dqb_btime = dqopt->info[type].dqi_bgrace;
180         ret = sb->s_op->quota_write(sb, type, (char *)&dqblk,
181               sizeof(struct v1_disk_dqblk), v1_dqoff(0));
182         if (ret == sizeof(struct v1_disk_dqblk))
183                 ret = 0;
184         else if (ret > 0)
185                 ret = -EIO;
186 out:
187         return ret;
188 }
189
190 static struct quota_format_ops v1_format_ops = {
191         .check_quota_file       = v1_check_quota_file,
192         .read_file_info         = v1_read_file_info,
193         .write_file_info        = v1_write_file_info,
194         .free_file_info         = NULL,
195         .read_dqblk             = v1_read_dqblk,
196         .commit_dqblk           = v1_commit_dqblk,
197 };
198
199 static struct quota_format_type v1_quota_format = {
200         .qf_fmt_id      = QFMT_VFS_OLD,
201         .qf_ops         = &v1_format_ops,
202         .qf_owner       = THIS_MODULE
203 };
204
205 static int __init init_v1_quota_format(void)
206 {
207         return register_quota_format(&v1_quota_format);
208 }
209
210 static void __exit exit_v1_quota_format(void)
211 {
212         unregister_quota_format(&v1_quota_format);
213 }
214
215 module_init(init_v1_quota_format);
216 module_exit(exit_v1_quota_format);
217