94cceb76b9a3cf5b9ca66203450eb9564e493bdd
[sfrench/cifs-2.6.git] / fs / quota / 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/kernel.h>
7 #include <linux/init.h>
8 #include <linux/module.h>
9
10 #include <asm/byteorder.h>
11
12 #include "quotaio_v1.h"
13
14 MODULE_AUTHOR("Jan Kara");
15 MODULE_DESCRIPTION("Old quota format support");
16 MODULE_LICENSE("GPL");
17
18 #define QUOTABLOCK_BITS 10
19 #define QUOTABLOCK_SIZE (1 << QUOTABLOCK_BITS)
20
21 static inline qsize_t v1_stoqb(qsize_t space)
22 {
23         return (space + QUOTABLOCK_SIZE - 1) >> QUOTABLOCK_BITS;
24 }
25
26 static inline qsize_t v1_qbtos(qsize_t blocks)
27 {
28         return blocks << QUOTABLOCK_BITS;
29 }
30
31 static 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;
36         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);
39         m->dqb_itime = d->dqb_itime;
40         m->dqb_btime = d->dqb_btime;
41 }
42
43 static 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;
48         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);
51         d->dqb_itime = m->dqb_itime;
52         d->dqb_btime = m->dqb_btime;
53 }
54
55 static int v1_read_dqblk(struct dquot *dquot)
56 {
57         int type = dquot->dq_id.type;
58         struct v1_disk_dqblk dqblk;
59         struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
60
61         if (!dqopt->files[type])
62                 return -EINVAL;
63
64         /* Set structure to 0s in case read fails/is after end of file */
65         memset(&dqblk, 0, sizeof(struct v1_disk_dqblk));
66         dquot->dq_sb->s_op->quota_read(dquot->dq_sb, type, (char *)&dqblk,
67                         sizeof(struct v1_disk_dqblk),
68                         v1_dqoff(from_kqid(&init_user_ns, dquot->dq_id)));
69
70         v1_disk2mem_dqblk(&dquot->dq_dqb, &dqblk);
71         if (dquot->dq_dqb.dqb_bhardlimit == 0 &&
72             dquot->dq_dqb.dqb_bsoftlimit == 0 &&
73             dquot->dq_dqb.dqb_ihardlimit == 0 &&
74             dquot->dq_dqb.dqb_isoftlimit == 0)
75                 set_bit(DQ_FAKE_B, &dquot->dq_flags);
76         dqstats_inc(DQST_READS);
77
78         return 0;
79 }
80
81 static int v1_commit_dqblk(struct dquot *dquot)
82 {
83         short type = dquot->dq_id.type;
84         ssize_t ret;
85         struct v1_disk_dqblk dqblk;
86         struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
87
88         down_write(&dqopt->dqio_sem);
89         v1_mem2disk_dqblk(&dqblk, &dquot->dq_dqb);
90         if (((type == USRQUOTA) && uid_eq(dquot->dq_id.uid, GLOBAL_ROOT_UID)) ||
91             ((type == GRPQUOTA) && gid_eq(dquot->dq_id.gid, GLOBAL_ROOT_GID))) {
92                 dqblk.dqb_btime =
93                         sb_dqopt(dquot->dq_sb)->info[type].dqi_bgrace;
94                 dqblk.dqb_itime =
95                         sb_dqopt(dquot->dq_sb)->info[type].dqi_igrace;
96         }
97         ret = 0;
98         if (sb_dqopt(dquot->dq_sb)->files[type])
99                 ret = dquot->dq_sb->s_op->quota_write(dquot->dq_sb, type,
100                         (char *)&dqblk, sizeof(struct v1_disk_dqblk),
101                         v1_dqoff(from_kqid(&init_user_ns, dquot->dq_id)));
102         up_write(&dqopt->dqio_sem);
103         if (ret != sizeof(struct v1_disk_dqblk)) {
104                 quota_error(dquot->dq_sb, "dquota write failed");
105                 if (ret >= 0)
106                         ret = -EIO;
107                 goto out;
108         }
109         ret = 0;
110
111 out:
112         dqstats_inc(DQST_WRITES);
113
114         return ret;
115 }
116
117 /* Magics of new quota format */
118 #define V2_INITQMAGICS {\
119         0xd9c01f11,     /* USRQUOTA */\
120         0xd9c01927      /* GRPQUOTA */\
121 }
122
123 /* Header of new quota format */
124 struct v2_disk_dqheader {
125         __le32 dqh_magic;        /* Magic number identifying file */
126         __le32 dqh_version;      /* File version */
127 };
128
129 static int v1_check_quota_file(struct super_block *sb, int type)
130 {
131         struct inode *inode = sb_dqopt(sb)->files[type];
132         ulong blocks;
133         size_t off; 
134         struct v2_disk_dqheader dqhead;
135         ssize_t size;
136         loff_t isize;
137         static const uint quota_magics[] = V2_INITQMAGICS;
138
139         isize = i_size_read(inode);
140         if (!isize)
141                 return 0;
142         blocks = isize >> BLOCK_SIZE_BITS;
143         off = isize & (BLOCK_SIZE - 1);
144         if ((blocks % sizeof(struct v1_disk_dqblk) * BLOCK_SIZE + off) %
145             sizeof(struct v1_disk_dqblk))
146                 return 0;
147         /* Doublecheck whether we didn't get file with new format - with old
148          * quotactl() this could happen */
149         size = sb->s_op->quota_read(sb, type, (char *)&dqhead,
150                                     sizeof(struct v2_disk_dqheader), 0);
151         if (size != sizeof(struct v2_disk_dqheader))
152                 return 1;       /* Probably not new format */
153         if (le32_to_cpu(dqhead.dqh_magic) != quota_magics[type])
154                 return 1;       /* Definitely not new format */
155         printk(KERN_INFO
156                "VFS: %s: Refusing to turn on old quota format on given file."
157                " It probably contains newer quota format.\n", sb->s_id);
158         return 0;               /* Seems like a new format file -> refuse it */
159 }
160
161 static int v1_read_file_info(struct super_block *sb, int type)
162 {
163         struct quota_info *dqopt = sb_dqopt(sb);
164         struct v1_disk_dqblk dqblk;
165         int ret;
166
167         ret = sb->s_op->quota_read(sb, type, (char *)&dqblk,
168                                 sizeof(struct v1_disk_dqblk), v1_dqoff(0));
169         if (ret != sizeof(struct v1_disk_dqblk)) {
170                 if (ret >= 0)
171                         ret = -EIO;
172                 goto out;
173         }
174         ret = 0;
175         /* limits are stored as unsigned 32-bit data */
176         dqopt->info[type].dqi_max_spc_limit = 0xffffffffULL << QUOTABLOCK_BITS;
177         dqopt->info[type].dqi_max_ino_limit = 0xffffffff;
178         dqopt->info[type].dqi_igrace =
179                         dqblk.dqb_itime ? dqblk.dqb_itime : MAX_IQ_TIME;
180         dqopt->info[type].dqi_bgrace =
181                         dqblk.dqb_btime ? dqblk.dqb_btime : MAX_DQ_TIME;
182 out:
183         return ret;
184 }
185
186 static int v1_write_file_info(struct super_block *sb, int type)
187 {
188         struct quota_info *dqopt = sb_dqopt(sb);
189         struct v1_disk_dqblk dqblk;
190         int ret;
191
192         dqopt->info[type].dqi_flags &= ~DQF_INFO_DIRTY;
193         ret = sb->s_op->quota_read(sb, type, (char *)&dqblk,
194                                 sizeof(struct v1_disk_dqblk), v1_dqoff(0));
195         if (ret != sizeof(struct v1_disk_dqblk)) {
196                 if (ret >= 0)
197                         ret = -EIO;
198                 goto out;
199         }
200         dqblk.dqb_itime = dqopt->info[type].dqi_igrace;
201         dqblk.dqb_btime = dqopt->info[type].dqi_bgrace;
202         ret = sb->s_op->quota_write(sb, type, (char *)&dqblk,
203               sizeof(struct v1_disk_dqblk), v1_dqoff(0));
204         if (ret == sizeof(struct v1_disk_dqblk))
205                 ret = 0;
206         else if (ret > 0)
207                 ret = -EIO;
208 out:
209         return ret;
210 }
211
212 static const 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
221 static 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
227 static int __init init_v1_quota_format(void)
228 {
229         return register_quota_format(&v1_quota_format);
230 }
231
232 static void __exit exit_v1_quota_format(void)
233 {
234         unregister_quota_format(&v1_quota_format);
235 }
236
237 module_init(init_v1_quota_format);
238 module_exit(exit_v1_quota_format);
239