cdbf71664cdce08f66010d06f0204ee0a40ad50d
[sfrench/cifs-2.6.git] / fs / quota / quota_v2.c
1 /*
2  *      vfsv0 quota IO operations on file
3  */
4
5 #include <linux/errno.h>
6 #include <linux/fs.h>
7 #include <linux/mount.h>
8 #include <linux/dqblk_v2.h>
9 #include <linux/kernel.h>
10 #include <linux/init.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/quotaops.h>
14
15 #include <asm/byteorder.h>
16
17 #include "quota_tree.h"
18 #include "quotaio_v2.h"
19
20 MODULE_AUTHOR("Jan Kara");
21 MODULE_DESCRIPTION("Quota format v2 support");
22 MODULE_LICENSE("GPL");
23
24 #define __QUOTA_V2_PARANOIA
25
26 static void v2r0_mem2diskdqb(void *dp, struct dquot *dquot);
27 static void v2r0_disk2memdqb(struct dquot *dquot, void *dp);
28 static int v2r0_is_id(void *dp, struct dquot *dquot);
29 static void v2r1_mem2diskdqb(void *dp, struct dquot *dquot);
30 static void v2r1_disk2memdqb(struct dquot *dquot, void *dp);
31 static int v2r1_is_id(void *dp, struct dquot *dquot);
32
33 static const struct qtree_fmt_operations v2r0_qtree_ops = {
34         .mem2disk_dqblk = v2r0_mem2diskdqb,
35         .disk2mem_dqblk = v2r0_disk2memdqb,
36         .is_id = v2r0_is_id,
37 };
38
39 static const struct qtree_fmt_operations v2r1_qtree_ops = {
40         .mem2disk_dqblk = v2r1_mem2diskdqb,
41         .disk2mem_dqblk = v2r1_disk2memdqb,
42         .is_id = v2r1_is_id,
43 };
44
45 #define QUOTABLOCK_BITS 10
46 #define QUOTABLOCK_SIZE (1 << QUOTABLOCK_BITS)
47
48 static inline qsize_t v2_stoqb(qsize_t space)
49 {
50         return (space + QUOTABLOCK_SIZE - 1) >> QUOTABLOCK_BITS;
51 }
52
53 static inline qsize_t v2_qbtos(qsize_t blocks)
54 {
55         return blocks << QUOTABLOCK_BITS;
56 }
57
58 static int v2_read_header(struct super_block *sb, int type,
59                           struct v2_disk_dqheader *dqhead)
60 {
61         ssize_t size;
62
63         size = sb->s_op->quota_read(sb, type, (char *)dqhead,
64                                     sizeof(struct v2_disk_dqheader), 0);
65         if (size != sizeof(struct v2_disk_dqheader)) {
66                 quota_error(sb, "Failed header read: expected=%zd got=%zd",
67                             sizeof(struct v2_disk_dqheader), size);
68                 if (size < 0)
69                         return size;
70                 return -EIO;
71         }
72         return 0;
73 }
74
75 /* Check whether given file is really vfsv0 quotafile */
76 static int v2_check_quota_file(struct super_block *sb, int type)
77 {
78         struct v2_disk_dqheader dqhead;
79         static const uint quota_magics[] = V2_INITQMAGICS;
80         static const uint quota_versions[] = V2_INITQVERSIONS;
81  
82         if (v2_read_header(sb, type, &dqhead))
83                 return 0;
84         if (le32_to_cpu(dqhead.dqh_magic) != quota_magics[type] ||
85             le32_to_cpu(dqhead.dqh_version) > quota_versions[type])
86                 return 0;
87         return 1;
88 }
89
90 /* Read information header from quota file */
91 static int v2_read_file_info(struct super_block *sb, int type)
92 {
93         struct v2_disk_dqinfo dinfo;
94         struct v2_disk_dqheader dqhead;
95         struct quota_info *dqopt = sb_dqopt(sb);
96         struct mem_dqinfo *info = &dqopt->info[type];
97         struct qtree_mem_dqinfo *qinfo;
98         ssize_t size;
99         unsigned int version;
100         int ret;
101
102         down_read(&dqopt->dqio_sem);
103         ret = v2_read_header(sb, type, &dqhead);
104         if (ret < 0)
105                 goto out;
106         version = le32_to_cpu(dqhead.dqh_version);
107         if ((info->dqi_fmt_id == QFMT_VFS_V0 && version != 0) ||
108             (info->dqi_fmt_id == QFMT_VFS_V1 && version != 1)) {
109                 ret = -EINVAL;
110                 goto out;
111         }
112
113         size = sb->s_op->quota_read(sb, type, (char *)&dinfo,
114                sizeof(struct v2_disk_dqinfo), V2_DQINFOOFF);
115         if (size != sizeof(struct v2_disk_dqinfo)) {
116                 quota_error(sb, "Can't read info structure");
117                 if (size < 0)
118                         ret = size;
119                 else
120                         ret = -EIO;
121                 goto out;
122         }
123         info->dqi_priv = kmalloc(sizeof(struct qtree_mem_dqinfo), GFP_NOFS);
124         if (!info->dqi_priv) {
125                 printk(KERN_WARNING
126                        "Not enough memory for quota information structure.\n");
127                 ret = -ENOMEM;
128                 goto out;
129         }
130         qinfo = info->dqi_priv;
131         if (version == 0) {
132                 /* limits are stored as unsigned 32-bit data */
133                 info->dqi_max_spc_limit = 0xffffffffLL << QUOTABLOCK_BITS;
134                 info->dqi_max_ino_limit = 0xffffffff;
135         } else {
136                 /*
137                  * Used space is stored as unsigned 64-bit value in bytes but
138                  * quota core supports only signed 64-bit values so use that
139                  * as a limit
140                  */
141                 info->dqi_max_spc_limit = 0x7fffffffffffffffLL; /* 2^63-1 */
142                 info->dqi_max_ino_limit = 0x7fffffffffffffffLL;
143         }
144         info->dqi_bgrace = le32_to_cpu(dinfo.dqi_bgrace);
145         info->dqi_igrace = le32_to_cpu(dinfo.dqi_igrace);
146         /* No flags currently supported */
147         info->dqi_flags = 0;
148         qinfo->dqi_sb = sb;
149         qinfo->dqi_type = type;
150         qinfo->dqi_blocks = le32_to_cpu(dinfo.dqi_blocks);
151         qinfo->dqi_free_blk = le32_to_cpu(dinfo.dqi_free_blk);
152         qinfo->dqi_free_entry = le32_to_cpu(dinfo.dqi_free_entry);
153         qinfo->dqi_blocksize_bits = V2_DQBLKSIZE_BITS;
154         qinfo->dqi_usable_bs = 1 << V2_DQBLKSIZE_BITS;
155         qinfo->dqi_qtree_depth = qtree_depth(qinfo);
156         if (version == 0) {
157                 qinfo->dqi_entry_size = sizeof(struct v2r0_disk_dqblk);
158                 qinfo->dqi_ops = &v2r0_qtree_ops;
159         } else {
160                 qinfo->dqi_entry_size = sizeof(struct v2r1_disk_dqblk);
161                 qinfo->dqi_ops = &v2r1_qtree_ops;
162         }
163         ret = 0;
164 out:
165         up_read(&dqopt->dqio_sem);
166         return ret;
167 }
168
169 /* Write information header to quota file */
170 static int v2_write_file_info(struct super_block *sb, int type)
171 {
172         struct v2_disk_dqinfo dinfo;
173         struct quota_info *dqopt = sb_dqopt(sb);
174         struct mem_dqinfo *info = &dqopt->info[type];
175         struct qtree_mem_dqinfo *qinfo = info->dqi_priv;
176         ssize_t size;
177
178         down_write(&dqopt->dqio_sem);
179         spin_lock(&dq_data_lock);
180         info->dqi_flags &= ~DQF_INFO_DIRTY;
181         dinfo.dqi_bgrace = cpu_to_le32(info->dqi_bgrace);
182         dinfo.dqi_igrace = cpu_to_le32(info->dqi_igrace);
183         /* No flags currently supported */
184         dinfo.dqi_flags = cpu_to_le32(0);
185         spin_unlock(&dq_data_lock);
186         dinfo.dqi_blocks = cpu_to_le32(qinfo->dqi_blocks);
187         dinfo.dqi_free_blk = cpu_to_le32(qinfo->dqi_free_blk);
188         dinfo.dqi_free_entry = cpu_to_le32(qinfo->dqi_free_entry);
189         size = sb->s_op->quota_write(sb, type, (char *)&dinfo,
190                sizeof(struct v2_disk_dqinfo), V2_DQINFOOFF);
191         up_write(&dqopt->dqio_sem);
192         if (size != sizeof(struct v2_disk_dqinfo)) {
193                 quota_error(sb, "Can't write info structure");
194                 return -1;
195         }
196         return 0;
197 }
198
199 static void v2r0_disk2memdqb(struct dquot *dquot, void *dp)
200 {
201         struct v2r0_disk_dqblk *d = dp, empty;
202         struct mem_dqblk *m = &dquot->dq_dqb;
203
204         m->dqb_ihardlimit = le32_to_cpu(d->dqb_ihardlimit);
205         m->dqb_isoftlimit = le32_to_cpu(d->dqb_isoftlimit);
206         m->dqb_curinodes = le32_to_cpu(d->dqb_curinodes);
207         m->dqb_itime = le64_to_cpu(d->dqb_itime);
208         m->dqb_bhardlimit = v2_qbtos(le32_to_cpu(d->dqb_bhardlimit));
209         m->dqb_bsoftlimit = v2_qbtos(le32_to_cpu(d->dqb_bsoftlimit));
210         m->dqb_curspace = le64_to_cpu(d->dqb_curspace);
211         m->dqb_btime = le64_to_cpu(d->dqb_btime);
212         /* We need to escape back all-zero structure */
213         memset(&empty, 0, sizeof(struct v2r0_disk_dqblk));
214         empty.dqb_itime = cpu_to_le64(1);
215         if (!memcmp(&empty, dp, sizeof(struct v2r0_disk_dqblk)))
216                 m->dqb_itime = 0;
217 }
218
219 static void v2r0_mem2diskdqb(void *dp, struct dquot *dquot)
220 {
221         struct v2r0_disk_dqblk *d = dp;
222         struct mem_dqblk *m = &dquot->dq_dqb;
223         struct qtree_mem_dqinfo *info =
224                         sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
225
226         d->dqb_ihardlimit = cpu_to_le32(m->dqb_ihardlimit);
227         d->dqb_isoftlimit = cpu_to_le32(m->dqb_isoftlimit);
228         d->dqb_curinodes = cpu_to_le32(m->dqb_curinodes);
229         d->dqb_itime = cpu_to_le64(m->dqb_itime);
230         d->dqb_bhardlimit = cpu_to_le32(v2_stoqb(m->dqb_bhardlimit));
231         d->dqb_bsoftlimit = cpu_to_le32(v2_stoqb(m->dqb_bsoftlimit));
232         d->dqb_curspace = cpu_to_le64(m->dqb_curspace);
233         d->dqb_btime = cpu_to_le64(m->dqb_btime);
234         d->dqb_id = cpu_to_le32(from_kqid(&init_user_ns, dquot->dq_id));
235         if (qtree_entry_unused(info, dp))
236                 d->dqb_itime = cpu_to_le64(1);
237 }
238
239 static int v2r0_is_id(void *dp, struct dquot *dquot)
240 {
241         struct v2r0_disk_dqblk *d = dp;
242         struct qtree_mem_dqinfo *info =
243                         sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
244
245         if (qtree_entry_unused(info, dp))
246                 return 0;
247         return qid_eq(make_kqid(&init_user_ns, dquot->dq_id.type,
248                                 le32_to_cpu(d->dqb_id)),
249                       dquot->dq_id);
250 }
251
252 static void v2r1_disk2memdqb(struct dquot *dquot, void *dp)
253 {
254         struct v2r1_disk_dqblk *d = dp, empty;
255         struct mem_dqblk *m = &dquot->dq_dqb;
256
257         m->dqb_ihardlimit = le64_to_cpu(d->dqb_ihardlimit);
258         m->dqb_isoftlimit = le64_to_cpu(d->dqb_isoftlimit);
259         m->dqb_curinodes = le64_to_cpu(d->dqb_curinodes);
260         m->dqb_itime = le64_to_cpu(d->dqb_itime);
261         m->dqb_bhardlimit = v2_qbtos(le64_to_cpu(d->dqb_bhardlimit));
262         m->dqb_bsoftlimit = v2_qbtos(le64_to_cpu(d->dqb_bsoftlimit));
263         m->dqb_curspace = le64_to_cpu(d->dqb_curspace);
264         m->dqb_btime = le64_to_cpu(d->dqb_btime);
265         /* We need to escape back all-zero structure */
266         memset(&empty, 0, sizeof(struct v2r1_disk_dqblk));
267         empty.dqb_itime = cpu_to_le64(1);
268         if (!memcmp(&empty, dp, sizeof(struct v2r1_disk_dqblk)))
269                 m->dqb_itime = 0;
270 }
271
272 static void v2r1_mem2diskdqb(void *dp, struct dquot *dquot)
273 {
274         struct v2r1_disk_dqblk *d = dp;
275         struct mem_dqblk *m = &dquot->dq_dqb;
276         struct qtree_mem_dqinfo *info =
277                         sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
278
279         d->dqb_ihardlimit = cpu_to_le64(m->dqb_ihardlimit);
280         d->dqb_isoftlimit = cpu_to_le64(m->dqb_isoftlimit);
281         d->dqb_curinodes = cpu_to_le64(m->dqb_curinodes);
282         d->dqb_itime = cpu_to_le64(m->dqb_itime);
283         d->dqb_bhardlimit = cpu_to_le64(v2_stoqb(m->dqb_bhardlimit));
284         d->dqb_bsoftlimit = cpu_to_le64(v2_stoqb(m->dqb_bsoftlimit));
285         d->dqb_curspace = cpu_to_le64(m->dqb_curspace);
286         d->dqb_btime = cpu_to_le64(m->dqb_btime);
287         d->dqb_id = cpu_to_le32(from_kqid(&init_user_ns, dquot->dq_id));
288         if (qtree_entry_unused(info, dp))
289                 d->dqb_itime = cpu_to_le64(1);
290 }
291
292 static int v2r1_is_id(void *dp, struct dquot *dquot)
293 {
294         struct v2r1_disk_dqblk *d = dp;
295         struct qtree_mem_dqinfo *info =
296                         sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
297
298         if (qtree_entry_unused(info, dp))
299                 return 0;
300         return qid_eq(make_kqid(&init_user_ns, dquot->dq_id.type,
301                                 le32_to_cpu(d->dqb_id)),
302                       dquot->dq_id);
303 }
304
305 static int v2_read_dquot(struct dquot *dquot)
306 {
307         struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
308         int ret;
309
310         down_read(&dqopt->dqio_sem);
311         ret = qtree_read_dquot(
312                         sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv,
313                         dquot);
314         up_read(&dqopt->dqio_sem);
315         return ret;
316 }
317
318 static int v2_write_dquot(struct dquot *dquot)
319 {
320         struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
321         int ret;
322         bool alloc = false;
323
324         /*
325          * If space for dquot is already allocated, we don't need any
326          * protection as we'll only overwrite the place of dquot. We are
327          * still protected by concurrent writes of the same dquot by
328          * dquot->dq_lock.
329          */
330         if (!dquot->dq_off) {
331                 alloc = true;
332                 down_write(&dqopt->dqio_sem);
333         }
334         ret = qtree_write_dquot(
335                         sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv,
336                         dquot);
337         if (alloc)
338                 up_write(&dqopt->dqio_sem);
339         return ret;
340 }
341
342 static int v2_release_dquot(struct dquot *dquot)
343 {
344         struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
345         int ret;
346
347         down_write(&dqopt->dqio_sem);
348         ret = qtree_release_dquot(sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv, dquot);
349         up_write(&dqopt->dqio_sem);
350
351         return ret;
352 }
353
354 static int v2_free_file_info(struct super_block *sb, int type)
355 {
356         kfree(sb_dqinfo(sb, type)->dqi_priv);
357         return 0;
358 }
359
360 static int v2_get_next_id(struct super_block *sb, struct kqid *qid)
361 {
362         struct quota_info *dqopt = sb_dqopt(sb);
363         int ret;
364
365         down_read(&dqopt->dqio_sem);
366         ret = qtree_get_next_id(sb_dqinfo(sb, qid->type)->dqi_priv, qid);
367         up_read(&dqopt->dqio_sem);
368         return ret;
369 }
370
371 static const struct quota_format_ops v2_format_ops = {
372         .check_quota_file       = v2_check_quota_file,
373         .read_file_info         = v2_read_file_info,
374         .write_file_info        = v2_write_file_info,
375         .free_file_info         = v2_free_file_info,
376         .read_dqblk             = v2_read_dquot,
377         .commit_dqblk           = v2_write_dquot,
378         .release_dqblk          = v2_release_dquot,
379         .get_next_id            = v2_get_next_id,
380 };
381
382 static struct quota_format_type v2r0_quota_format = {
383         .qf_fmt_id      = QFMT_VFS_V0,
384         .qf_ops         = &v2_format_ops,
385         .qf_owner       = THIS_MODULE
386 };
387
388 static struct quota_format_type v2r1_quota_format = {
389         .qf_fmt_id      = QFMT_VFS_V1,
390         .qf_ops         = &v2_format_ops,
391         .qf_owner       = THIS_MODULE
392 };
393
394 static int __init init_v2_quota_format(void)
395 {
396         int ret;
397
398         ret = register_quota_format(&v2r0_quota_format);
399         if (ret)
400                 return ret;
401         return register_quota_format(&v2r1_quota_format);
402 }
403
404 static void __exit exit_v2_quota_format(void)
405 {
406         unregister_quota_format(&v2r0_quota_format);
407         unregister_quota_format(&v2r1_quota_format);
408 }
409
410 module_init(init_v2_quota_format);
411 module_exit(exit_v2_quota_format);