Branch data Line data Source code
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 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 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 : 0 : return (space + QUOTABLOCK_SIZE - 1) >> QUOTABLOCK_BITS;
51 : : }
52 : :
53 : : static inline qsize_t v2_qbtos(qsize_t blocks)
54 : : {
55 : 0 : return blocks << QUOTABLOCK_BITS;
56 : : }
57 : :
58 : 0 : static int v2_read_header(struct super_block *sb, int type,
59 : : struct v2_disk_dqheader *dqhead)
60 : : {
61 : : ssize_t size;
62 : :
63 : 0 : size = sb->s_op->quota_read(sb, type, (char *)dqhead,
64 : : sizeof(struct v2_disk_dqheader), 0);
65 [ # # ]: 0 : if (size != sizeof(struct v2_disk_dqheader)) {
66 : 0 : quota_error(sb, "Failed header read: expected=%zd got=%zd",
67 : : sizeof(struct v2_disk_dqheader), size);
68 : 0 : return 0;
69 : : }
70 : : return 1;
71 : : }
72 : :
73 : : /* Check whether given file is really vfsv0 quotafile */
74 : 0 : 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 [ # # ]: 0 : if (!v2_read_header(sb, type, &dqhead))
81 : : return 0;
82 [ # # ][ # # ]: 0 : if (le32_to_cpu(dqhead.dqh_magic) != quota_magics[type] ||
83 : 0 : le32_to_cpu(dqhead.dqh_version) > quota_versions[type])
84 : : return 0;
85 : 0 : return 1;
86 : : }
87 : :
88 : : /* Read information header from quota file */
89 : 0 : 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 : 0 : struct mem_dqinfo *info = sb_dqinfo(sb, type);
94 : : struct qtree_mem_dqinfo *qinfo;
95 : : ssize_t size;
96 : : unsigned int version;
97 : :
98 [ # # ]: 0 : if (!v2_read_header(sb, type, &dqhead))
99 : : return -1;
100 : 0 : version = le32_to_cpu(dqhead.dqh_version);
101 [ # # ][ # # ]: 0 : if ((info->dqi_fmt_id == QFMT_VFS_V0 && version != 0) ||
[ # # ]
102 [ # # ]: 0 : (info->dqi_fmt_id == QFMT_VFS_V1 && version != 1))
103 : : return -1;
104 : :
105 : 0 : size = sb->s_op->quota_read(sb, type, (char *)&dinfo,
106 : : sizeof(struct v2_disk_dqinfo), V2_DQINFOOFF);
107 [ # # ]: 0 : if (size != sizeof(struct v2_disk_dqinfo)) {
108 : 0 : quota_error(sb, "Can't read info structure");
109 : 0 : return -1;
110 : : }
111 : 0 : info->dqi_priv = kmalloc(sizeof(struct qtree_mem_dqinfo), GFP_NOFS);
112 [ # # ]: 0 : if (!info->dqi_priv) {
113 : 0 : printk(KERN_WARNING
114 : : "Not enough memory for quota information structure.\n");
115 : 0 : return -ENOMEM;
116 : : }
117 : : qinfo = info->dqi_priv;
118 [ # # ]: 0 : if (version == 0) {
119 : : /* limits are stored as unsigned 32-bit data */
120 : 0 : info->dqi_maxblimit = 0xffffffff;
121 : 0 : info->dqi_maxilimit = 0xffffffff;
122 : : } else {
123 : : /* used space is stored as unsigned 64-bit value */
124 : 0 : info->dqi_maxblimit = 0xffffffffffffffffULL; /* 2^64-1 */
125 : 0 : info->dqi_maxilimit = 0xffffffffffffffffULL;
126 : : }
127 : 0 : info->dqi_bgrace = le32_to_cpu(dinfo.dqi_bgrace);
128 : 0 : info->dqi_igrace = le32_to_cpu(dinfo.dqi_igrace);
129 : 0 : info->dqi_flags = le32_to_cpu(dinfo.dqi_flags);
130 : 0 : qinfo->dqi_sb = sb;
131 : 0 : qinfo->dqi_type = type;
132 : 0 : qinfo->dqi_blocks = le32_to_cpu(dinfo.dqi_blocks);
133 : 0 : qinfo->dqi_free_blk = le32_to_cpu(dinfo.dqi_free_blk);
134 : 0 : qinfo->dqi_free_entry = le32_to_cpu(dinfo.dqi_free_entry);
135 : 0 : qinfo->dqi_blocksize_bits = V2_DQBLKSIZE_BITS;
136 : 0 : qinfo->dqi_usable_bs = 1 << V2_DQBLKSIZE_BITS;
137 : 0 : qinfo->dqi_qtree_depth = qtree_depth(qinfo);
138 [ # # ]: 0 : if (version == 0) {
139 : 0 : qinfo->dqi_entry_size = sizeof(struct v2r0_disk_dqblk);
140 : 0 : qinfo->dqi_ops = &v2r0_qtree_ops;
141 : : } else {
142 : 0 : qinfo->dqi_entry_size = sizeof(struct v2r1_disk_dqblk);
143 : 0 : qinfo->dqi_ops = &v2r1_qtree_ops;
144 : : }
145 : : return 0;
146 : : }
147 : :
148 : : /* Write information header to quota file */
149 : 0 : static int v2_write_file_info(struct super_block *sb, int type)
150 : : {
151 : : struct v2_disk_dqinfo dinfo;
152 : 0 : struct mem_dqinfo *info = sb_dqinfo(sb, type);
153 : 0 : struct qtree_mem_dqinfo *qinfo = info->dqi_priv;
154 : : ssize_t size;
155 : :
156 : : spin_lock(&dq_data_lock);
157 : 0 : info->dqi_flags &= ~DQF_INFO_DIRTY;
158 : 0 : dinfo.dqi_bgrace = cpu_to_le32(info->dqi_bgrace);
159 : 0 : dinfo.dqi_igrace = cpu_to_le32(info->dqi_igrace);
160 : 0 : dinfo.dqi_flags = cpu_to_le32(info->dqi_flags & DQF_MASK);
161 : : spin_unlock(&dq_data_lock);
162 : 0 : dinfo.dqi_blocks = cpu_to_le32(qinfo->dqi_blocks);
163 : 0 : dinfo.dqi_free_blk = cpu_to_le32(qinfo->dqi_free_blk);
164 : 0 : dinfo.dqi_free_entry = cpu_to_le32(qinfo->dqi_free_entry);
165 : 0 : size = sb->s_op->quota_write(sb, type, (char *)&dinfo,
166 : : sizeof(struct v2_disk_dqinfo), V2_DQINFOOFF);
167 [ # # ]: 0 : if (size != sizeof(struct v2_disk_dqinfo)) {
168 : 0 : quota_error(sb, "Can't write info structure");
169 : 0 : return -1;
170 : : }
171 : : return 0;
172 : : }
173 : :
174 : 0 : static void v2r0_disk2memdqb(struct dquot *dquot, void *dp)
175 : : {
176 : : struct v2r0_disk_dqblk *d = dp, empty;
177 : : struct mem_dqblk *m = &dquot->dq_dqb;
178 : :
179 : 0 : m->dqb_ihardlimit = le32_to_cpu(d->dqb_ihardlimit);
180 : 0 : m->dqb_isoftlimit = le32_to_cpu(d->dqb_isoftlimit);
181 : 0 : m->dqb_curinodes = le32_to_cpu(d->dqb_curinodes);
182 : 0 : m->dqb_itime = le64_to_cpu(d->dqb_itime);
183 : 0 : m->dqb_bhardlimit = v2_qbtos(le32_to_cpu(d->dqb_bhardlimit));
184 : 0 : m->dqb_bsoftlimit = v2_qbtos(le32_to_cpu(d->dqb_bsoftlimit));
185 : 0 : m->dqb_curspace = le64_to_cpu(d->dqb_curspace);
186 : 0 : m->dqb_btime = le64_to_cpu(d->dqb_btime);
187 : : /* We need to escape back all-zero structure */
188 : 0 : memset(&empty, 0, sizeof(struct v2r0_disk_dqblk));
189 : 0 : empty.dqb_itime = cpu_to_le64(1);
190 [ # # ]: 0 : if (!memcmp(&empty, dp, sizeof(struct v2r0_disk_dqblk)))
191 : 0 : m->dqb_itime = 0;
192 : 0 : }
193 : :
194 : 0 : static void v2r0_mem2diskdqb(void *dp, struct dquot *dquot)
195 : : {
196 : : struct v2r0_disk_dqblk *d = dp;
197 : : struct mem_dqblk *m = &dquot->dq_dqb;
198 : 0 : struct qtree_mem_dqinfo *info =
199 : 0 : sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
200 : :
201 : 0 : d->dqb_ihardlimit = cpu_to_le32(m->dqb_ihardlimit);
202 : 0 : d->dqb_isoftlimit = cpu_to_le32(m->dqb_isoftlimit);
203 : 0 : d->dqb_curinodes = cpu_to_le32(m->dqb_curinodes);
204 : 0 : d->dqb_itime = cpu_to_le64(m->dqb_itime);
205 : 0 : d->dqb_bhardlimit = cpu_to_le32(v2_stoqb(m->dqb_bhardlimit));
206 : 0 : d->dqb_bsoftlimit = cpu_to_le32(v2_stoqb(m->dqb_bsoftlimit));
207 : 0 : d->dqb_curspace = cpu_to_le64(m->dqb_curspace);
208 : 0 : d->dqb_btime = cpu_to_le64(m->dqb_btime);
209 : 0 : d->dqb_id = cpu_to_le32(from_kqid(&init_user_ns, dquot->dq_id));
210 [ # # ]: 0 : if (qtree_entry_unused(info, dp))
211 : 0 : d->dqb_itime = cpu_to_le64(1);
212 : 0 : }
213 : :
214 : 0 : static int v2r0_is_id(void *dp, struct dquot *dquot)
215 : : {
216 : : struct v2r0_disk_dqblk *d = dp;
217 : 0 : struct qtree_mem_dqinfo *info =
218 : 0 : sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
219 : :
220 [ # # ]: 0 : if (qtree_entry_unused(info, dp))
221 : : return 0;
222 : 0 : return qid_eq(make_kqid(&init_user_ns, dquot->dq_id.type,
223 : : le32_to_cpu(d->dqb_id)),
224 : : dquot->dq_id);
225 : : }
226 : :
227 : 0 : static 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 : 0 : m->dqb_ihardlimit = le64_to_cpu(d->dqb_ihardlimit);
233 : 0 : m->dqb_isoftlimit = le64_to_cpu(d->dqb_isoftlimit);
234 : 0 : m->dqb_curinodes = le64_to_cpu(d->dqb_curinodes);
235 : 0 : m->dqb_itime = le64_to_cpu(d->dqb_itime);
236 : 0 : m->dqb_bhardlimit = v2_qbtos(le64_to_cpu(d->dqb_bhardlimit));
237 : 0 : m->dqb_bsoftlimit = v2_qbtos(le64_to_cpu(d->dqb_bsoftlimit));
238 : 0 : m->dqb_curspace = le64_to_cpu(d->dqb_curspace);
239 : 0 : m->dqb_btime = le64_to_cpu(d->dqb_btime);
240 : : /* We need to escape back all-zero structure */
241 : 0 : memset(&empty, 0, sizeof(struct v2r1_disk_dqblk));
242 : 0 : empty.dqb_itime = cpu_to_le64(1);
243 [ # # ]: 0 : if (!memcmp(&empty, dp, sizeof(struct v2r1_disk_dqblk)))
244 : 0 : m->dqb_itime = 0;
245 : 0 : }
246 : :
247 : 0 : static 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 : 0 : struct qtree_mem_dqinfo *info =
252 : 0 : sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
253 : :
254 : 0 : d->dqb_ihardlimit = cpu_to_le64(m->dqb_ihardlimit);
255 : 0 : d->dqb_isoftlimit = cpu_to_le64(m->dqb_isoftlimit);
256 : 0 : d->dqb_curinodes = cpu_to_le64(m->dqb_curinodes);
257 : 0 : d->dqb_itime = cpu_to_le64(m->dqb_itime);
258 : 0 : d->dqb_bhardlimit = cpu_to_le64(v2_stoqb(m->dqb_bhardlimit));
259 : 0 : d->dqb_bsoftlimit = cpu_to_le64(v2_stoqb(m->dqb_bsoftlimit));
260 : 0 : d->dqb_curspace = cpu_to_le64(m->dqb_curspace);
261 : 0 : d->dqb_btime = cpu_to_le64(m->dqb_btime);
262 : 0 : d->dqb_id = cpu_to_le32(from_kqid(&init_user_ns, dquot->dq_id));
263 [ # # ]: 0 : if (qtree_entry_unused(info, dp))
264 : 0 : d->dqb_itime = cpu_to_le64(1);
265 : 0 : }
266 : :
267 : 0 : static int v2r1_is_id(void *dp, struct dquot *dquot)
268 : : {
269 : : struct v2r1_disk_dqblk *d = dp;
270 : 0 : struct qtree_mem_dqinfo *info =
271 : 0 : sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
272 : :
273 [ # # ]: 0 : if (qtree_entry_unused(info, dp))
274 : : return 0;
275 : 0 : return qid_eq(make_kqid(&init_user_ns, dquot->dq_id.type,
276 : : le32_to_cpu(d->dqb_id)),
277 : : dquot->dq_id);
278 : : }
279 : :
280 : 0 : static int v2_read_dquot(struct dquot *dquot)
281 : : {
282 : 0 : return qtree_read_dquot(sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv, dquot);
283 : : }
284 : :
285 : 0 : static int v2_write_dquot(struct dquot *dquot)
286 : : {
287 : 0 : return qtree_write_dquot(sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv, dquot);
288 : : }
289 : :
290 : 0 : static int v2_release_dquot(struct dquot *dquot)
291 : : {
292 : 0 : return qtree_release_dquot(sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv, dquot);
293 : : }
294 : :
295 : 0 : static int v2_free_file_info(struct super_block *sb, int type)
296 : : {
297 : 0 : kfree(sb_dqinfo(sb, type)->dqi_priv);
298 : 0 : return 0;
299 : : }
300 : :
301 : : static const struct quota_format_ops v2_format_ops = {
302 : : .check_quota_file = v2_check_quota_file,
303 : : .read_file_info = v2_read_file_info,
304 : : .write_file_info = v2_write_file_info,
305 : : .free_file_info = v2_free_file_info,
306 : : .read_dqblk = v2_read_dquot,
307 : : .commit_dqblk = v2_write_dquot,
308 : : .release_dqblk = v2_release_dquot,
309 : : };
310 : :
311 : : static struct quota_format_type v2r0_quota_format = {
312 : : .qf_fmt_id = QFMT_VFS_V0,
313 : : .qf_ops = &v2_format_ops,
314 : : .qf_owner = THIS_MODULE
315 : : };
316 : :
317 : : static 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 : :
323 : 0 : static int __init init_v2_quota_format(void)
324 : : {
325 : : int ret;
326 : :
327 : 0 : ret = register_quota_format(&v2r0_quota_format);
328 [ # # ]: 0 : if (ret)
329 : : return ret;
330 : 0 : return register_quota_format(&v2r1_quota_format);
331 : : }
332 : :
333 : 0 : static void __exit exit_v2_quota_format(void)
334 : : {
335 : 0 : unregister_quota_format(&v2r0_quota_format);
336 : 0 : unregister_quota_format(&v2r1_quota_format);
337 : 0 : }
338 : :
339 : : module_init(init_v2_quota_format);
340 : : module_exit(exit_v2_quota_format);
|