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