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