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