blob: 02751ec695c596eb68fc558ab0514c39ab0943c8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/kernel.h>
10#include <linux/init.h>
11#include <linux/module.h>
12#include <linux/slab.h>
Jan Kara74abb982008-07-25 01:46:51 -070013#include <linux/quotaops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15#include <asm/byteorder.h>
16
Jan Kara1ccd14b2008-09-22 05:54:49 +020017#include "quota_tree.h"
Jan Karacf770c12008-09-21 23:17:53 +020018#include "quotaio_v2.h"
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020MODULE_AUTHOR("Jan Kara");
21MODULE_DESCRIPTION("Quota format v2 support");
22MODULE_LICENSE("GPL");
23
24#define __QUOTA_V2_PARANOIA
25
Jan Kara498c6012009-11-16 18:09:47 +010026static void v2r0_mem2diskdqb(void *dp, struct dquot *dquot);
27static void v2r0_disk2memdqb(struct dquot *dquot, void *dp);
28static int v2r0_is_id(void *dp, struct dquot *dquot);
29static void v2r1_mem2diskdqb(void *dp, struct dquot *dquot);
30static void v2r1_disk2memdqb(struct dquot *dquot, void *dp);
31static int v2r1_is_id(void *dp, struct dquot *dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Jan Kara498c6012009-11-16 18:09:47 +010033static 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
39static struct qtree_fmt_operations v2r1_qtree_ops = {
40 .mem2disk_dqblk = v2r1_mem2diskdqb,
41 .disk2mem_dqblk = v2r1_disk2memdqb,
42 .is_id = v2r1_is_id,
Jan Kara1ccd14b2008-09-22 05:54:49 +020043};
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Jan Kara12095462008-08-20 14:45:12 +020045#define QUOTABLOCK_BITS 10
46#define QUOTABLOCK_SIZE (1 << QUOTABLOCK_BITS)
47
48static inline qsize_t v2_stoqb(qsize_t space)
49{
50 return (space + QUOTABLOCK_SIZE - 1) >> QUOTABLOCK_BITS;
51}
52
53static inline qsize_t v2_qbtos(qsize_t blocks)
54{
55 return blocks << QUOTABLOCK_BITS;
56}
57
Jan Kara498c6012009-11-16 18:09:47 +010058static 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)) {
Jiaying Zhangfb5ffb02010-07-20 16:54:43 +020066 quota_error(sb, "Failed header read: expected=%zd got=%zd",
67 sizeof(struct v2_disk_dqheader), size);
Jan Kara498c6012009-11-16 18:09:47 +010068 return 0;
69 }
70 return 1;
71}
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/* Check whether given file is really vfsv0 quotafile */
74static int v2_check_quota_file(struct super_block *sb, int type)
75{
76 struct v2_disk_dqheader dqhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 static const uint quota_magics[] = V2_INITQMAGICS;
78 static const uint quota_versions[] = V2_INITQVERSIONS;
79
Jan Kara498c6012009-11-16 18:09:47 +010080 if (!v2_read_header(sb, type, &dqhead))
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 if (le32_to_cpu(dqhead.dqh_magic) != quota_magics[type] ||
Jan Kara498c6012009-11-16 18:09:47 +010083 le32_to_cpu(dqhead.dqh_version) > quota_versions[type])
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 return 0;
85 return 1;
86}
87
88/* Read information header from quota file */
89static int v2_read_file_info(struct super_block *sb, int type)
90{
91 struct v2_disk_dqinfo dinfo;
Jan Kara498c6012009-11-16 18:09:47 +010092 struct v2_disk_dqheader dqhead;
Jan Kara1ccd14b2008-09-22 05:54:49 +020093 struct mem_dqinfo *info = sb_dqinfo(sb, type);
Jan Karae3d4d562008-10-02 18:44:14 +020094 struct qtree_mem_dqinfo *qinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 ssize_t size;
Jan Kara498c6012009-11-16 18:09:47 +010096 unsigned int version;
97
98 if (!v2_read_header(sb, type, &dqhead))
Jan Kara869835d2009-12-21 21:57:04 +010099 return -1;
Jan Kara498c6012009-11-16 18:09:47 +0100100 version = le32_to_cpu(dqhead.dqh_version);
Jan Kara869835d2009-12-21 21:57:04 +0100101 if ((info->dqi_fmt_id == QFMT_VFS_V0 && version != 0) ||
102 (info->dqi_fmt_id == QFMT_VFS_V1 && version != 1))
103 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
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)) {
Jiaying Zhangfb5ffb02010-07-20 16:54:43 +0200108 quota_error(sb, "Can't read info structure");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 return -1;
110 }
Jan Karae3d4d562008-10-02 18:44:14 +0200111 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");
Davidlohr Bueso7a39de12011-01-20 15:32:05 -0300115 return -ENOMEM;
Jan Karae3d4d562008-10-02 18:44:14 +0200116 }
117 qinfo = info->dqi_priv;
Jan Kara498c6012009-11-16 18:09:47 +0100118 if (version == 0) {
119 /* limits are stored as unsigned 32-bit data */
120 info->dqi_maxblimit = 0xffffffff;
121 info->dqi_maxilimit = 0xffffffff;
122 } else {
123 /* used space is stored as unsigned 64-bit value */
Jan Kara82fdfa92009-12-15 22:24:36 +0100124 info->dqi_maxblimit = 0xffffffffffffffffULL; /* 2^64-1 */
125 info->dqi_maxilimit = 0xffffffffffffffffULL;
Jan Kara498c6012009-11-16 18:09:47 +0100126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 info->dqi_bgrace = le32_to_cpu(dinfo.dqi_bgrace);
128 info->dqi_igrace = le32_to_cpu(dinfo.dqi_igrace);
129 info->dqi_flags = le32_to_cpu(dinfo.dqi_flags);
Jan Karae3d4d562008-10-02 18:44:14 +0200130 qinfo->dqi_sb = sb;
131 qinfo->dqi_type = type;
132 qinfo->dqi_blocks = le32_to_cpu(dinfo.dqi_blocks);
133 qinfo->dqi_free_blk = le32_to_cpu(dinfo.dqi_free_blk);
134 qinfo->dqi_free_entry = le32_to_cpu(dinfo.dqi_free_entry);
135 qinfo->dqi_blocksize_bits = V2_DQBLKSIZE_BITS;
136 qinfo->dqi_usable_bs = 1 << V2_DQBLKSIZE_BITS;
137 qinfo->dqi_qtree_depth = qtree_depth(qinfo);
Jan Kara498c6012009-11-16 18:09:47 +0100138 if (version == 0) {
139 qinfo->dqi_entry_size = sizeof(struct v2r0_disk_dqblk);
140 qinfo->dqi_ops = &v2r0_qtree_ops;
141 } else {
142 qinfo->dqi_entry_size = sizeof(struct v2r1_disk_dqblk);
143 qinfo->dqi_ops = &v2r1_qtree_ops;
144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 return 0;
146}
147
148/* Write information header to quota file */
149static int v2_write_file_info(struct super_block *sb, int type)
150{
151 struct v2_disk_dqinfo dinfo;
Jan Kara1ccd14b2008-09-22 05:54:49 +0200152 struct mem_dqinfo *info = sb_dqinfo(sb, type);
Jan Karae3d4d562008-10-02 18:44:14 +0200153 struct qtree_mem_dqinfo *qinfo = info->dqi_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 ssize_t size;
155
156 spin_lock(&dq_data_lock);
157 info->dqi_flags &= ~DQF_INFO_DIRTY;
158 dinfo.dqi_bgrace = cpu_to_le32(info->dqi_bgrace);
159 dinfo.dqi_igrace = cpu_to_le32(info->dqi_igrace);
160 dinfo.dqi_flags = cpu_to_le32(info->dqi_flags & DQF_MASK);
161 spin_unlock(&dq_data_lock);
Jan Karae3d4d562008-10-02 18:44:14 +0200162 dinfo.dqi_blocks = cpu_to_le32(qinfo->dqi_blocks);
163 dinfo.dqi_free_blk = cpu_to_le32(qinfo->dqi_free_blk);
164 dinfo.dqi_free_entry = cpu_to_le32(qinfo->dqi_free_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 size = sb->s_op->quota_write(sb, type, (char *)&dinfo,
166 sizeof(struct v2_disk_dqinfo), V2_DQINFOOFF);
167 if (size != sizeof(struct v2_disk_dqinfo)) {
Jiaying Zhangfb5ffb02010-07-20 16:54:43 +0200168 quota_error(sb, "Can't write info structure");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 return -1;
170 }
171 return 0;
172}
173
Jan Kara498c6012009-11-16 18:09:47 +0100174static void v2r0_disk2memdqb(struct dquot *dquot, void *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
Jan Kara498c6012009-11-16 18:09:47 +0100176 struct v2r0_disk_dqblk *d = dp, empty;
Jan Kara1ccd14b2008-09-22 05:54:49 +0200177 struct mem_dqblk *m = &dquot->dq_dqb;
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 m->dqb_ihardlimit = le32_to_cpu(d->dqb_ihardlimit);
180 m->dqb_isoftlimit = le32_to_cpu(d->dqb_isoftlimit);
181 m->dqb_curinodes = le32_to_cpu(d->dqb_curinodes);
182 m->dqb_itime = le64_to_cpu(d->dqb_itime);
Jan Kara12095462008-08-20 14:45:12 +0200183 m->dqb_bhardlimit = v2_qbtos(le32_to_cpu(d->dqb_bhardlimit));
184 m->dqb_bsoftlimit = v2_qbtos(le32_to_cpu(d->dqb_bsoftlimit));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 m->dqb_curspace = le64_to_cpu(d->dqb_curspace);
186 m->dqb_btime = le64_to_cpu(d->dqb_btime);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200187 /* We need to escape back all-zero structure */
Jan Kara498c6012009-11-16 18:09:47 +0100188 memset(&empty, 0, sizeof(struct v2r0_disk_dqblk));
Jan Kara1ccd14b2008-09-22 05:54:49 +0200189 empty.dqb_itime = cpu_to_le64(1);
Jan Kara498c6012009-11-16 18:09:47 +0100190 if (!memcmp(&empty, dp, sizeof(struct v2r0_disk_dqblk)))
Jan Kara1ccd14b2008-09-22 05:54:49 +0200191 m->dqb_itime = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
Jan Kara498c6012009-11-16 18:09:47 +0100194static void v2r0_mem2diskdqb(void *dp, struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
Jan Kara498c6012009-11-16 18:09:47 +0100196 struct v2r0_disk_dqblk *d = dp;
Jan Kara1ccd14b2008-09-22 05:54:49 +0200197 struct mem_dqblk *m = &dquot->dq_dqb;
198 struct qtree_mem_dqinfo *info =
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700199 sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
Jan Kara1ccd14b2008-09-22 05:54:49 +0200200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 d->dqb_ihardlimit = cpu_to_le32(m->dqb_ihardlimit);
202 d->dqb_isoftlimit = cpu_to_le32(m->dqb_isoftlimit);
203 d->dqb_curinodes = cpu_to_le32(m->dqb_curinodes);
204 d->dqb_itime = cpu_to_le64(m->dqb_itime);
Jan Karacf770c12008-09-21 23:17:53 +0200205 d->dqb_bhardlimit = cpu_to_le32(v2_stoqb(m->dqb_bhardlimit));
206 d->dqb_bsoftlimit = cpu_to_le32(v2_stoqb(m->dqb_bsoftlimit));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 d->dqb_curspace = cpu_to_le64(m->dqb_curspace);
208 d->dqb_btime = cpu_to_le64(m->dqb_btime);
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700209 d->dqb_id = cpu_to_le32(from_kqid(&init_user_ns, dquot->dq_id));
Jan Kara1ccd14b2008-09-22 05:54:49 +0200210 if (qtree_entry_unused(info, dp))
211 d->dqb_itime = cpu_to_le64(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
Jan Kara498c6012009-11-16 18:09:47 +0100214static int v2r0_is_id(void *dp, struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
Jan Kara498c6012009-11-16 18:09:47 +0100216 struct v2r0_disk_dqblk *d = dp;
217 struct qtree_mem_dqinfo *info =
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700218 sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
Jan Kara498c6012009-11-16 18:09:47 +0100219
220 if (qtree_entry_unused(info, dp))
221 return 0;
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700222 return qid_eq(make_kqid(&init_user_ns, dquot->dq_id.type,
223 le32_to_cpu(d->dqb_id)),
224 dquot->dq_id);
Jan Kara498c6012009-11-16 18:09:47 +0100225}
226
227static void v2r1_disk2memdqb(struct dquot *dquot, void *dp)
228{
229 struct v2r1_disk_dqblk *d = dp, empty;
230 struct mem_dqblk *m = &dquot->dq_dqb;
231
232 m->dqb_ihardlimit = le64_to_cpu(d->dqb_ihardlimit);
233 m->dqb_isoftlimit = le64_to_cpu(d->dqb_isoftlimit);
234 m->dqb_curinodes = le64_to_cpu(d->dqb_curinodes);
235 m->dqb_itime = le64_to_cpu(d->dqb_itime);
236 m->dqb_bhardlimit = v2_qbtos(le64_to_cpu(d->dqb_bhardlimit));
237 m->dqb_bsoftlimit = v2_qbtos(le64_to_cpu(d->dqb_bsoftlimit));
238 m->dqb_curspace = le64_to_cpu(d->dqb_curspace);
239 m->dqb_btime = le64_to_cpu(d->dqb_btime);
240 /* We need to escape back all-zero structure */
241 memset(&empty, 0, sizeof(struct v2r1_disk_dqblk));
242 empty.dqb_itime = cpu_to_le64(1);
243 if (!memcmp(&empty, dp, sizeof(struct v2r1_disk_dqblk)))
244 m->dqb_itime = 0;
245}
246
247static void v2r1_mem2diskdqb(void *dp, struct dquot *dquot)
248{
249 struct v2r1_disk_dqblk *d = dp;
250 struct mem_dqblk *m = &dquot->dq_dqb;
251 struct qtree_mem_dqinfo *info =
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700252 sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
Jan Kara498c6012009-11-16 18:09:47 +0100253
254 d->dqb_ihardlimit = cpu_to_le64(m->dqb_ihardlimit);
255 d->dqb_isoftlimit = cpu_to_le64(m->dqb_isoftlimit);
256 d->dqb_curinodes = cpu_to_le64(m->dqb_curinodes);
257 d->dqb_itime = cpu_to_le64(m->dqb_itime);
258 d->dqb_bhardlimit = cpu_to_le64(v2_stoqb(m->dqb_bhardlimit));
259 d->dqb_bsoftlimit = cpu_to_le64(v2_stoqb(m->dqb_bsoftlimit));
260 d->dqb_curspace = cpu_to_le64(m->dqb_curspace);
261 d->dqb_btime = cpu_to_le64(m->dqb_btime);
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700262 d->dqb_id = cpu_to_le32(from_kqid(&init_user_ns, dquot->dq_id));
Jan Kara498c6012009-11-16 18:09:47 +0100263 if (qtree_entry_unused(info, dp))
264 d->dqb_itime = cpu_to_le64(1);
265}
266
267static int v2r1_is_id(void *dp, struct dquot *dquot)
268{
269 struct v2r1_disk_dqblk *d = dp;
Jan Kara1ccd14b2008-09-22 05:54:49 +0200270 struct qtree_mem_dqinfo *info =
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700271 sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Jan Kara1ccd14b2008-09-22 05:54:49 +0200273 if (qtree_entry_unused(info, dp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return 0;
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700275 return qid_eq(make_kqid(&init_user_ns, dquot->dq_id.type,
276 le32_to_cpu(d->dqb_id)),
277 dquot->dq_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
280static int v2_read_dquot(struct dquot *dquot)
281{
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700282 return qtree_read_dquot(sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv, dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
Jan Kara1ccd14b2008-09-22 05:54:49 +0200285static int v2_write_dquot(struct dquot *dquot)
286{
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700287 return qtree_write_dquot(sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv, dquot);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200288}
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290static int v2_release_dquot(struct dquot *dquot)
291{
Eric W. Biederman4c376dc2012-09-16 03:56:19 -0700292 return qtree_release_dquot(sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv, dquot);
Jan Karae3d4d562008-10-02 18:44:14 +0200293}
294
295static int v2_free_file_info(struct super_block *sb, int type)
296{
297 kfree(sb_dqinfo(sb, type)->dqi_priv);
298 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
Alexey Dobriyan1472da52009-10-16 15:26:03 +0400301static const struct quota_format_ops v2_format_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 .check_quota_file = v2_check_quota_file,
303 .read_file_info = v2_read_file_info,
304 .write_file_info = v2_write_file_info,
Jan Karae3d4d562008-10-02 18:44:14 +0200305 .free_file_info = v2_free_file_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 .read_dqblk = v2_read_dquot,
307 .commit_dqblk = v2_write_dquot,
308 .release_dqblk = v2_release_dquot,
309};
310
Jan Kara498c6012009-11-16 18:09:47 +0100311static struct quota_format_type v2r0_quota_format = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 .qf_fmt_id = QFMT_VFS_V0,
313 .qf_ops = &v2_format_ops,
314 .qf_owner = THIS_MODULE
315};
316
Jan Kara498c6012009-11-16 18:09:47 +0100317static struct quota_format_type v2r1_quota_format = {
318 .qf_fmt_id = QFMT_VFS_V1,
319 .qf_ops = &v2_format_ops,
320 .qf_owner = THIS_MODULE
321};
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323static int __init init_v2_quota_format(void)
324{
Jan Kara498c6012009-11-16 18:09:47 +0100325 int ret;
326
327 ret = register_quota_format(&v2r0_quota_format);
328 if (ret)
329 return ret;
330 return register_quota_format(&v2r1_quota_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
333static void __exit exit_v2_quota_format(void)
334{
Jan Kara498c6012009-11-16 18:09:47 +0100335 unregister_quota_format(&v2r0_quota_format);
336 unregister_quota_format(&v2r1_quota_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
339module_init(init_v2_quota_format);
340module_exit(exit_v2_quota_format);