1 /*
2 * Implementation of new quotafile format
3 *
4 * Jan Kara <jack@suse.cz> - sponsored by SuSE CR
5 */
6
7 #include "config.h"
8 #include <sys/types.h>
9 #include <errno.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <unistd.h>
14
15 #include "common.h"
16 #include "quotaio_v2.h"
17 #include "dqblk_v2.h"
18 #include "quotaio.h"
19 #include "quotaio_tree.h"
20
21 static int v2_check_file(struct quota_handle *h, int type, int fmt);
22 static int v2_init_io(struct quota_handle *h);
23 static int v2_new_io(struct quota_handle *h);
24 static int v2_write_info(struct quota_handle *h);
25 static struct dquot *v2_read_dquot(struct quota_handle *h, qid_t id);
26 static int v2_commit_dquot(struct dquot *dquot);
27 static int v2_scan_dquots(struct quota_handle *h,
28 int (*process_dquot) (struct dquot *dquot,
29 void *data),
30 void *data);
31 static int v2_report(struct quota_handle *h, int verbose);
32
33 struct quotafile_ops quotafile_ops_2 = {
34 .check_file = v2_check_file,
35 .init_io = v2_init_io,
36 .new_io = v2_new_io,
37 .write_info = v2_write_info,
38 .read_dquot = v2_read_dquot,
39 .commit_dquot = v2_commit_dquot,
40 .scan_dquots = v2_scan_dquots,
41 .report = v2_report,
42 };
43
44 /*
45 * Copy dquot from disk to memory
46 */
v2r0_disk2memdqblk(struct dquot * dquot,void * dp)47 static void v2r0_disk2memdqblk(struct dquot *dquot, void *dp)
48 {
49 struct util_dqblk *m = &dquot->dq_dqb;
50 struct v2r0_disk_dqblk *d = dp, empty;
51
52 dquot->dq_id = ext2fs_le32_to_cpu(d->dqb_id);
53 m->dqb_ihardlimit = ext2fs_le32_to_cpu(d->dqb_ihardlimit);
54 m->dqb_isoftlimit = ext2fs_le32_to_cpu(d->dqb_isoftlimit);
55 m->dqb_bhardlimit = ext2fs_le32_to_cpu(d->dqb_bhardlimit);
56 m->dqb_bsoftlimit = ext2fs_le32_to_cpu(d->dqb_bsoftlimit);
57 m->dqb_curinodes = ext2fs_le32_to_cpu(d->dqb_curinodes);
58 m->dqb_curspace = ext2fs_le64_to_cpu(d->dqb_curspace);
59 m->dqb_itime = ext2fs_le64_to_cpu(d->dqb_itime);
60 m->dqb_btime = ext2fs_le64_to_cpu(d->dqb_btime);
61
62 memset(&empty, 0, sizeof(struct v2r0_disk_dqblk));
63 empty.dqb_itime = ext2fs_cpu_to_le64(1);
64 if (!memcmp(&empty, dp, sizeof(struct v2r0_disk_dqblk)))
65 m->dqb_itime = 0;
66 }
67
68 /*
69 * Copy dquot from memory to disk
70 */
v2r0_mem2diskdqblk(void * dp,struct dquot * dquot)71 static void v2r0_mem2diskdqblk(void *dp, struct dquot *dquot)
72 {
73 struct util_dqblk *m = &dquot->dq_dqb;
74 struct v2r0_disk_dqblk *d = dp;
75
76 d->dqb_ihardlimit = ext2fs_cpu_to_le32(m->dqb_ihardlimit);
77 d->dqb_isoftlimit = ext2fs_cpu_to_le32(m->dqb_isoftlimit);
78 d->dqb_bhardlimit = ext2fs_cpu_to_le32(m->dqb_bhardlimit);
79 d->dqb_bsoftlimit = ext2fs_cpu_to_le32(m->dqb_bsoftlimit);
80 d->dqb_curinodes = ext2fs_cpu_to_le32(m->dqb_curinodes);
81 d->dqb_curspace = ext2fs_cpu_to_le64(m->dqb_curspace);
82 d->dqb_itime = ext2fs_cpu_to_le64(m->dqb_itime);
83 d->dqb_btime = ext2fs_cpu_to_le64(m->dqb_btime);
84 d->dqb_id = ext2fs_cpu_to_le32(dquot->dq_id);
85 if (qtree_entry_unused(&dquot->dq_h->qh_info.u.v2_mdqi.dqi_qtree, dp))
86 d->dqb_itime = ext2fs_cpu_to_le64(1);
87 }
88
v2r0_is_id(void * dp,struct dquot * dquot)89 static int v2r0_is_id(void *dp, struct dquot *dquot)
90 {
91 struct v2r0_disk_dqblk *d = dp;
92 struct qtree_mem_dqinfo *info =
93 &dquot->dq_h->qh_info.u.v2_mdqi.dqi_qtree;
94
95 if (qtree_entry_unused(info, dp))
96 return 0;
97 return ext2fs_le32_to_cpu(d->dqb_id) == dquot->dq_id;
98 }
99
100 static struct qtree_fmt_operations v2r0_fmt_ops = {
101 .mem2disk_dqblk = v2r0_mem2diskdqblk,
102 .disk2mem_dqblk = v2r0_disk2memdqblk,
103 .is_id = v2r0_is_id,
104 };
105
106 /*
107 * Copy dquot from disk to memory
108 */
v2r1_disk2memdqblk(struct dquot * dquot,void * dp)109 static void v2r1_disk2memdqblk(struct dquot *dquot, void *dp)
110 {
111 struct util_dqblk *m = &dquot->dq_dqb;
112 struct v2r1_disk_dqblk *d = dp, empty;
113
114 dquot->dq_id = ext2fs_le32_to_cpu(d->dqb_id);
115 m->dqb_ihardlimit = ext2fs_le64_to_cpu(d->dqb_ihardlimit);
116 m->dqb_isoftlimit = ext2fs_le64_to_cpu(d->dqb_isoftlimit);
117 m->dqb_bhardlimit = ext2fs_le64_to_cpu(d->dqb_bhardlimit);
118 m->dqb_bsoftlimit = ext2fs_le64_to_cpu(d->dqb_bsoftlimit);
119 m->dqb_curinodes = ext2fs_le64_to_cpu(d->dqb_curinodes);
120 m->dqb_curspace = ext2fs_le64_to_cpu(d->dqb_curspace);
121 m->dqb_itime = ext2fs_le64_to_cpu(d->dqb_itime);
122 m->dqb_btime = ext2fs_le64_to_cpu(d->dqb_btime);
123
124 memset(&empty, 0, sizeof(struct v2r1_disk_dqblk));
125 empty.dqb_itime = ext2fs_cpu_to_le64(1);
126 if (!memcmp(&empty, dp, sizeof(struct v2r1_disk_dqblk)))
127 m->dqb_itime = 0;
128 }
129
130 /*
131 * Copy dquot from memory to disk
132 */
v2r1_mem2diskdqblk(void * dp,struct dquot * dquot)133 static void v2r1_mem2diskdqblk(void *dp, struct dquot *dquot)
134 {
135 struct util_dqblk *m = &dquot->dq_dqb;
136 struct v2r1_disk_dqblk *d = dp;
137
138 d->dqb_ihardlimit = ext2fs_cpu_to_le64(m->dqb_ihardlimit);
139 d->dqb_isoftlimit = ext2fs_cpu_to_le64(m->dqb_isoftlimit);
140 d->dqb_bhardlimit = ext2fs_cpu_to_le64(m->dqb_bhardlimit);
141 d->dqb_bsoftlimit = ext2fs_cpu_to_le64(m->dqb_bsoftlimit);
142 d->dqb_curinodes = ext2fs_cpu_to_le64(m->dqb_curinodes);
143 d->dqb_curspace = ext2fs_cpu_to_le64(m->dqb_curspace);
144 d->dqb_itime = ext2fs_cpu_to_le64(m->dqb_itime);
145 d->dqb_btime = ext2fs_cpu_to_le64(m->dqb_btime);
146 d->dqb_id = ext2fs_cpu_to_le32(dquot->dq_id);
147 if (qtree_entry_unused(&dquot->dq_h->qh_info.u.v2_mdqi.dqi_qtree, dp))
148 d->dqb_itime = ext2fs_cpu_to_le64(1);
149 }
150
v2r1_is_id(void * dp,struct dquot * dquot)151 static int v2r1_is_id(void *dp, struct dquot *dquot)
152 {
153 struct v2r1_disk_dqblk *d = dp;
154 struct qtree_mem_dqinfo *info =
155 &dquot->dq_h->qh_info.u.v2_mdqi.dqi_qtree;
156
157 if (qtree_entry_unused(info, dp))
158 return 0;
159 return ext2fs_le32_to_cpu(d->dqb_id) == dquot->dq_id;
160 }
161
162 static struct qtree_fmt_operations v2r1_fmt_ops = {
163 .mem2disk_dqblk = v2r1_mem2diskdqblk,
164 .disk2mem_dqblk = v2r1_disk2memdqblk,
165 .is_id = v2r1_is_id,
166 };
167
168 /*
169 * Copy dqinfo from disk to memory
170 */
v2_disk2memdqinfo(struct util_dqinfo * m,struct v2_disk_dqinfo * d)171 static inline void v2_disk2memdqinfo(struct util_dqinfo *m,
172 struct v2_disk_dqinfo *d)
173 {
174 m->dqi_bgrace = ext2fs_le32_to_cpu(d->dqi_bgrace);
175 m->dqi_igrace = ext2fs_le32_to_cpu(d->dqi_igrace);
176 m->u.v2_mdqi.dqi_flags = ext2fs_le32_to_cpu(d->dqi_flags) & V2_DQF_MASK;
177 m->u.v2_mdqi.dqi_qtree.dqi_blocks = ext2fs_le32_to_cpu(d->dqi_blocks);
178 m->u.v2_mdqi.dqi_qtree.dqi_free_blk =
179 ext2fs_le32_to_cpu(d->dqi_free_blk);
180 m->u.v2_mdqi.dqi_qtree.dqi_free_entry =
181 ext2fs_le32_to_cpu(d->dqi_free_entry);
182 }
183
184 /*
185 * Copy dqinfo from memory to disk
186 */
v2_mem2diskdqinfo(struct v2_disk_dqinfo * d,struct util_dqinfo * m)187 static inline void v2_mem2diskdqinfo(struct v2_disk_dqinfo *d,
188 struct util_dqinfo *m)
189 {
190 d->dqi_bgrace = ext2fs_cpu_to_le32(m->dqi_bgrace);
191 d->dqi_igrace = ext2fs_cpu_to_le32(m->dqi_igrace);
192 d->dqi_flags = ext2fs_cpu_to_le32(m->u.v2_mdqi.dqi_flags & V2_DQF_MASK);
193 d->dqi_blocks = ext2fs_cpu_to_le32(m->u.v2_mdqi.dqi_qtree.dqi_blocks);
194 d->dqi_free_blk =
195 ext2fs_cpu_to_le32(m->u.v2_mdqi.dqi_qtree.dqi_free_blk);
196 d->dqi_free_entry =
197 ext2fs_cpu_to_le32(m->u.v2_mdqi.dqi_qtree.dqi_free_entry);
198 }
199
v2_read_header(struct quota_handle * h,struct v2_disk_dqheader * dqh)200 static int v2_read_header(struct quota_handle *h, struct v2_disk_dqheader *dqh)
201 {
202 if (h->e2fs_read(&h->qh_qf, 0, dqh, sizeof(struct v2_disk_dqheader)) !=
203 sizeof(struct v2_disk_dqheader))
204 return 0;
205
206 return 1;
207 }
208
209 /*
210 * Check whether given quota file is in our format
211 */
v2_check_file(struct quota_handle * h,int type,int fmt)212 static int v2_check_file(struct quota_handle *h, int type, int fmt)
213 {
214 struct v2_disk_dqheader dqh;
215 int file_magics[] = INITQMAGICS;
216 int be_magic;
217
218 if (fmt != QFMT_VFS_V1)
219 return 0;
220
221 if (!v2_read_header(h, &dqh))
222 return 0;
223
224 be_magic = ext2fs_be32_to_cpu((__force __be32)dqh.dqh_magic);
225 if (be_magic == file_magics[type]) {
226 log_err("Your quota file is stored in wrong endianness");
227 return 0;
228 }
229 if (V2_VERSION_R0 != ext2fs_le32_to_cpu(dqh.dqh_version) &&
230 V2_VERSION_R1 != ext2fs_le32_to_cpu(dqh.dqh_version))
231 return 0;
232 return 1;
233 }
234
235 /*
236 * Open quotafile
237 */
v2_init_io(struct quota_handle * h)238 static int v2_init_io(struct quota_handle *h)
239 {
240 struct v2_disk_dqheader dqh;
241 struct v2_disk_dqinfo ddqinfo;
242 struct v2_mem_dqinfo *info;
243 __u64 filesize;
244 int version;
245
246 if (!v2_read_header(h, &dqh))
247 return -1;
248 version = ext2fs_le32_to_cpu(dqh.dqh_version);
249
250 if (version == V2_VERSION_R0) {
251 h->qh_info.u.v2_mdqi.dqi_qtree.dqi_entry_size =
252 sizeof(struct v2r0_disk_dqblk);
253 h->qh_info.u.v2_mdqi.dqi_qtree.dqi_ops = &v2r0_fmt_ops;
254 } else {
255 h->qh_info.u.v2_mdqi.dqi_qtree.dqi_entry_size =
256 sizeof(struct v2r1_disk_dqblk);
257 h->qh_info.u.v2_mdqi.dqi_qtree.dqi_ops = &v2r1_fmt_ops;
258 }
259
260 /* Read information about quotafile */
261 if (h->e2fs_read(&h->qh_qf, V2_DQINFOOFF, &ddqinfo,
262 sizeof(ddqinfo)) != sizeof(ddqinfo))
263 return -1;
264 v2_disk2memdqinfo(&h->qh_info, &ddqinfo);
265
266 /* Check to make sure quota file info is sane */
267 info = &h->qh_info.u.v2_mdqi;
268 if (ext2fs_file_get_lsize(h->qh_qf.e2_file, &filesize))
269 return -1;
270 if ((filesize > (1U << 31)) ||
271 (info->dqi_qtree.dqi_blocks >
272 (filesize + QT_BLKSIZE - 1) >> QT_BLKSIZE_BITS)) {
273 log_err("Quota inode %u corrupted: file size %llu; "
274 "dqi_blocks %u", h->qh_qf.ino,
275 (unsigned long long) filesize,
276 info->dqi_qtree.dqi_blocks);
277 return -1;
278 }
279 if (info->dqi_qtree.dqi_free_blk >= info->dqi_qtree.dqi_blocks) {
280 log_err("Quota inode %u corrupted: free_blk %u; dqi_blocks %u",
281 h->qh_qf.ino, info->dqi_qtree.dqi_free_blk,
282 info->dqi_qtree.dqi_blocks);
283 return -1;
284 }
285 if (info->dqi_qtree.dqi_free_entry >= info->dqi_qtree.dqi_blocks) {
286 log_err("Quota inode %u corrupted: free_entry %u; "
287 "dqi_blocks %u", h->qh_qf.ino,
288 info->dqi_qtree.dqi_free_entry,
289 info->dqi_qtree.dqi_blocks);
290 return -1;
291 }
292 return 0;
293 }
294
295 /*
296 * Initialize new quotafile
297 */
v2_new_io(struct quota_handle * h)298 static int v2_new_io(struct quota_handle *h)
299 {
300 int file_magics[] = INITQMAGICS;
301 struct v2_disk_dqheader ddqheader;
302 struct v2_disk_dqinfo ddqinfo;
303
304 if (h->qh_fmt != QFMT_VFS_V1)
305 return -1;
306
307 /* Write basic quota header */
308 ddqheader.dqh_magic = ext2fs_cpu_to_le32(file_magics[h->qh_type]);
309 ddqheader.dqh_version = ext2fs_cpu_to_le32(V2_VERSION_R1);
310 if (h->e2fs_write(&h->qh_qf, 0, &ddqheader, sizeof(ddqheader)) !=
311 sizeof(ddqheader))
312 return -1;
313
314 /* Write information about quotafile */
315 h->qh_info.dqi_bgrace = MAX_DQ_TIME;
316 h->qh_info.dqi_igrace = MAX_IQ_TIME;
317 h->qh_info.u.v2_mdqi.dqi_flags = 0;
318 h->qh_info.u.v2_mdqi.dqi_qtree.dqi_blocks = QT_TREEOFF + 1;
319 h->qh_info.u.v2_mdqi.dqi_qtree.dqi_free_blk = 0;
320 h->qh_info.u.v2_mdqi.dqi_qtree.dqi_free_entry = 0;
321 h->qh_info.u.v2_mdqi.dqi_qtree.dqi_entry_size =
322 sizeof(struct v2r1_disk_dqblk);
323 h->qh_info.u.v2_mdqi.dqi_qtree.dqi_ops = &v2r1_fmt_ops;
324 v2_mem2diskdqinfo(&ddqinfo, &h->qh_info);
325 if (h->e2fs_write(&h->qh_qf, V2_DQINFOOFF, &ddqinfo,
326 sizeof(ddqinfo)) !=
327 sizeof(ddqinfo))
328 return -1;
329
330 return 0;
331 }
332
333 /*
334 * Write information (grace times to file)
335 */
v2_write_info(struct quota_handle * h)336 static int v2_write_info(struct quota_handle *h)
337 {
338 struct v2_disk_dqinfo ddqinfo;
339
340 v2_mem2diskdqinfo(&ddqinfo, &h->qh_info);
341 if (h->e2fs_write(&h->qh_qf, V2_DQINFOOFF, &ddqinfo, sizeof(ddqinfo)) !=
342 sizeof(ddqinfo))
343 return -1;
344
345 return 0;
346 }
347
348 /*
349 * Read dquot from disk
350 */
v2_read_dquot(struct quota_handle * h,qid_t id)351 static struct dquot *v2_read_dquot(struct quota_handle *h, qid_t id)
352 {
353 return qtree_read_dquot(h, id);
354 }
355
356 /*
357 * Commit changes of dquot to disk - it might also mean deleting it when quota
358 * became fake one and user has no blocks.
359 * User can process use 'errno' to detect errstr.
360 */
v2_commit_dquot(struct dquot * dquot)361 static int v2_commit_dquot(struct dquot *dquot)
362 {
363 struct util_dqblk *b = &dquot->dq_dqb;
364
365 if (!b->dqb_curspace && !b->dqb_curinodes && !b->dqb_bsoftlimit &&
366 !b->dqb_isoftlimit && !b->dqb_bhardlimit && !b->dqb_ihardlimit)
367 qtree_delete_dquot(dquot);
368 else
369 qtree_write_dquot(dquot);
370 return 0;
371 }
372
v2_scan_dquots(struct quota_handle * h,int (* process_dquot)(struct dquot *,void *),void * data)373 static int v2_scan_dquots(struct quota_handle *h,
374 int (*process_dquot) (struct dquot *, void *),
375 void *data)
376 {
377 return qtree_scan_dquots(h, process_dquot, data);
378 }
379
380 /* Report information about quotafile.
381 * TODO: Not used right now, but we should be able to use this when we add
382 * support to debugfs to read quota files.
383 */
v2_report(struct quota_handle * h EXT2FS_ATTR ((unused)),int verbose EXT2FS_ATTR ((unused)))384 static int v2_report(struct quota_handle *h EXT2FS_ATTR((unused)),
385 int verbose EXT2FS_ATTR((unused)))
386 {
387 log_err("Not Implemented.");
388 return -1;
389 }
390