1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
/*
* Implementation of new quotafile format
*
* Jan Kara <jack@suse.cz> - sponsored by SuSE CR
*/
#include "config.h"
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "common.h"
#include "quotaio_v2.h"
#include "dqblk_v2.h"
#include "quotaio.h"
#include "quotaio_tree.h"
typedef char *dqbuf_t;
static int v2_check_file(struct quota_handle *h, int type, int fmt);
static int v2_init_io(struct quota_handle *h);
static int v2_new_io(struct quota_handle *h);
static int v2_write_info(struct quota_handle *h);
static struct dquot *v2_read_dquot(struct quota_handle *h, qid_t id);
static int v2_commit_dquot(struct dquot *dquot, int flags);
static int v2_scan_dquots(struct quota_handle *h,
int (*process_dquot) (struct dquot *dquot,
char *dqname));
static int v2_report(struct quota_handle *h, int verbose);
struct quotafile_ops quotafile_ops_2 = {
.check_file = v2_check_file,
.init_io = v2_init_io,
.new_io = v2_new_io,
.write_info = v2_write_info,
.read_dquot = v2_read_dquot,
.commit_dquot = v2_commit_dquot,
.scan_dquots = v2_scan_dquots,
.report = v2_report,
};
#define getdqbuf() smalloc(V2_DQBLKSIZE)
#define freedqbuf(buf) free(buf)
/*
* Copy dquot from disk to memory
*/
static void v2r1_disk2memdqblk(struct dquot *dquot, void *dp)
{
struct util_dqblk *m = &dquot->dq_dqb;
struct v2r1_disk_dqblk *d = dp, empty;
dquot->dq_id = ext2fs_le32_to_cpu(d->dqb_id);
m->dqb_ihardlimit = ext2fs_le64_to_cpu(d->dqb_ihardlimit);
m->dqb_isoftlimit = ext2fs_le64_to_cpu(d->dqb_isoftlimit);
m->dqb_bhardlimit = ext2fs_le64_to_cpu(d->dqb_bhardlimit);
m->dqb_bsoftlimit = ext2fs_le64_to_cpu(d->dqb_bsoftlimit);
m->dqb_curinodes = ext2fs_le64_to_cpu(d->dqb_curinodes);
m->dqb_curspace = ext2fs_le64_to_cpu(d->dqb_curspace);
m->dqb_itime = ext2fs_le64_to_cpu(d->dqb_itime);
m->dqb_btime = ext2fs_le64_to_cpu(d->dqb_btime);
memset(&empty, 0, sizeof(struct v2r1_disk_dqblk));
empty.dqb_itime = ext2fs_cpu_to_le64(1);
if (!memcmp(&empty, dp, sizeof(struct v2r1_disk_dqblk)))
m->dqb_itime = 0;
}
/*
* Copy dquot from memory to disk
*/
static void v2r1_mem2diskdqblk(void *dp, struct dquot *dquot)
{
struct util_dqblk *m = &dquot->dq_dqb;
struct v2r1_disk_dqblk *d = dp;
d->dqb_ihardlimit = ext2fs_cpu_to_le64(m->dqb_ihardlimit);
d->dqb_isoftlimit = ext2fs_cpu_to_le64(m->dqb_isoftlimit);
d->dqb_bhardlimit = ext2fs_cpu_to_le64(m->dqb_bhardlimit);
d->dqb_bsoftlimit = ext2fs_cpu_to_le64(m->dqb_bsoftlimit);
d->dqb_curinodes = ext2fs_cpu_to_le64(m->dqb_curinodes);
d->dqb_curspace = ext2fs_cpu_to_le64(m->dqb_curspace);
d->dqb_itime = ext2fs_cpu_to_le64(m->dqb_itime);
d->dqb_btime = ext2fs_cpu_to_le64(m->dqb_btime);
d->dqb_id = ext2fs_cpu_to_le32(dquot->dq_id);
if (qtree_entry_unused(&dquot->dq_h->qh_info.u.v2_mdqi.dqi_qtree, dp))
d->dqb_itime = ext2fs_cpu_to_le64(1);
}
static int v2r1_is_id(void *dp, struct dquot *dquot)
{
struct v2r1_disk_dqblk *d = dp;
struct qtree_mem_dqinfo *info =
&dquot->dq_h->qh_info.u.v2_mdqi.dqi_qtree;
if (qtree_entry_unused(info, dp))
return 0;
return ext2fs_le32_to_cpu(d->dqb_id) == dquot->dq_id;
}
static struct qtree_fmt_operations v2r1_fmt_ops = {
.mem2disk_dqblk = v2r1_mem2diskdqblk,
.disk2mem_dqblk = v2r1_disk2memdqblk,
.is_id = v2r1_is_id,
};
/*
* Copy dqinfo from disk to memory
*/
static inline void v2_disk2memdqinfo(struct util_dqinfo *m,
struct v2_disk_dqinfo *d)
{
m->dqi_bgrace = ext2fs_le32_to_cpu(d->dqi_bgrace);
m->dqi_igrace = ext2fs_le32_to_cpu(d->dqi_igrace);
m->u.v2_mdqi.dqi_flags = ext2fs_le32_to_cpu(d->dqi_flags) & V2_DQF_MASK;
m->u.v2_mdqi.dqi_qtree.dqi_blocks = ext2fs_le32_to_cpu(d->dqi_blocks);
m->u.v2_mdqi.dqi_qtree.dqi_free_blk =
ext2fs_le32_to_cpu(d->dqi_free_blk);
m->u.v2_mdqi.dqi_qtree.dqi_free_entry =
ext2fs_le32_to_cpu(d->dqi_free_entry);
}
/*
* Copy dqinfo from memory to disk
*/
static inline void v2_mem2diskdqinfo(struct v2_disk_dqinfo *d,
struct util_dqinfo *m)
{
d->dqi_bgrace = ext2fs_cpu_to_le32(m->dqi_bgrace);
d->dqi_igrace = ext2fs_cpu_to_le32(m->dqi_igrace);
d->dqi_flags = ext2fs_cpu_to_le32(m->u.v2_mdqi.dqi_flags & V2_DQF_MASK);
d->dqi_blocks = ext2fs_cpu_to_le32(m->u.v2_mdqi.dqi_qtree.dqi_blocks);
d->dqi_free_blk =
ext2fs_cpu_to_le32(m->u.v2_mdqi.dqi_qtree.dqi_free_blk);
d->dqi_free_entry =
ext2fs_cpu_to_le32(m->u.v2_mdqi.dqi_qtree.dqi_free_entry);
}
/* Convert kernel quotablock format to utility one */
static inline void v2_kern2utildqblk(struct util_dqblk *u,
struct v2_kern_dqblk *k)
{
u->dqb_ihardlimit = k->dqb_ihardlimit;
u->dqb_isoftlimit = k->dqb_isoftlimit;
u->dqb_bhardlimit = k->dqb_bhardlimit;
u->dqb_bsoftlimit = k->dqb_bsoftlimit;
u->dqb_curinodes = k->dqb_curinodes;
u->dqb_curspace = k->dqb_curspace;
u->dqb_itime = k->dqb_itime;
u->dqb_btime = k->dqb_btime;
}
/* Convert utility quotablock format to kernel one */
static inline void v2_util2kerndqblk(struct v2_kern_dqblk *k,
struct util_dqblk *u)
{
k->dqb_ihardlimit = u->dqb_ihardlimit;
k->dqb_isoftlimit = u->dqb_isoftlimit;
k->dqb_bhardlimit = u->dqb_bhardlimit;
k->dqb_bsoftlimit = u->dqb_bsoftlimit;
k->dqb_curinodes = u->dqb_curinodes;
k->dqb_curspace = u->dqb_curspace;
k->dqb_itime = u->dqb_itime;
k->dqb_btime = u->dqb_btime;
}
static int v2_read_header(struct quota_handle *h, struct v2_disk_dqheader *dqh)
{
if (h->e2fs_read(&h->qh_qf, 0, dqh, sizeof(struct v2_disk_dqheader)) !=
sizeof(struct v2_disk_dqheader))
return 0;
return 1;
}
/*
* Check whether given quota file is in our format
*/
static int v2_check_file(struct quota_handle *h, int type, int fmt)
{
struct v2_disk_dqheader dqh;
int file_magics[] = INITQMAGICS;
int known_versions[] = INIT_V2_VERSIONS;
int version;
if (!v2_read_header(h, &dqh))
return 0;
if (fmt == QFMT_VFS_V0)
version = 0;
else if (fmt == QFMT_VFS_V1)
version = 1;
else
return 0;
if (ext2fs_le32_to_cpu(dqh.dqh_magic) != file_magics[type]) {
if (ext2fs_be32_to_cpu(dqh.dqh_magic) == file_magics[type])
log_fatal(3, "Your quota file is stored in wrong "
"endianity.", "");
return 0;
}
if (ext2fs_le32_to_cpu(dqh.dqh_version) > known_versions[type])
return 0;
if (version != ext2fs_le32_to_cpu(dqh.dqh_version))
return 0;
return 1;
}
/*
* Open quotafile
*/
static int v2_init_io(struct quota_handle *h)
{
log_err("Not Implemented.", "");
BUG_ON(1);
return 0;
}
/*
* Initialize new quotafile
*/
static int v2_new_io(struct quota_handle *h)
{
int file_magics[] = INITQMAGICS;
struct v2_disk_dqheader ddqheader;
struct v2_disk_dqinfo ddqinfo;
int version = 1;
BUG_ON(h->qh_fmt != QFMT_VFS_V1);
/* Write basic quota header */
ddqheader.dqh_magic = ext2fs_cpu_to_le32(file_magics[h->qh_type]);
ddqheader.dqh_version = ext2fs_cpu_to_le32(version);
if (h->e2fs_write(&h->qh_qf, 0, &ddqheader, sizeof(ddqheader)) !=
sizeof(ddqheader))
return -1;
/* Write information about quotafile */
h->qh_info.dqi_bgrace = MAX_DQ_TIME;
h->qh_info.dqi_igrace = MAX_IQ_TIME;
h->qh_info.u.v2_mdqi.dqi_flags = 0;
h->qh_info.u.v2_mdqi.dqi_qtree.dqi_blocks = QT_TREEOFF + 1;
h->qh_info.u.v2_mdqi.dqi_qtree.dqi_free_blk = 0;
h->qh_info.u.v2_mdqi.dqi_qtree.dqi_free_entry = 0;
h->qh_info.u.v2_mdqi.dqi_qtree.dqi_entry_size =
sizeof(struct v2r1_disk_dqblk);
h->qh_info.u.v2_mdqi.dqi_qtree.dqi_ops = &v2r1_fmt_ops;
v2_mem2diskdqinfo(&ddqinfo, &h->qh_info);
if (h->e2fs_write(&h->qh_qf, V2_DQINFOOFF, &ddqinfo,
sizeof(ddqinfo)) !=
sizeof(ddqinfo))
return -1;
return 0;
}
/*
* Write information (grace times to file)
*/
static int v2_write_info(struct quota_handle *h)
{
struct v2_disk_dqinfo ddqinfo;
v2_mem2diskdqinfo(&ddqinfo, &h->qh_info);
if (h->e2fs_write(&h->qh_qf, V2_DQINFOOFF, &ddqinfo, sizeof(ddqinfo)) !=
sizeof(ddqinfo))
return -1;
return 0;
}
/*
* Read dquot (either from disk or from kernel)
* User can use errno to detect errstr when NULL is returned
*/
static struct dquot *v2_read_dquot(struct quota_handle *h, qid_t id)
{
return qtree_read_dquot(h, id);
}
/*
* Commit changes of dquot to disk - it might also mean deleting it when quota
* became fake one and user has no blocks.
* User can process use 'errno' to detect errstr.
*/
static int v2_commit_dquot(struct dquot *dquot, int flags)
{
struct util_dqblk *b = &dquot->dq_dqb;
if (!b->dqb_curspace && !b->dqb_curinodes && !b->dqb_bsoftlimit &&
!b->dqb_isoftlimit && !b->dqb_bhardlimit && !b->dqb_ihardlimit)
qtree_delete_dquot(dquot);
else
qtree_write_dquot(dquot);
return 0;
}
static int v2_scan_dquots(struct quota_handle *h,
int (*process_dquot) (struct dquot *, char *))
{
return qtree_scan_dquots(h, process_dquot);
}
/* Report information about quotafile.
* TODO: Not used right now, but we should be able to use this when we add
* support to debugfs to read quota files.
*/
static int v2_report(struct quota_handle *h, int verbose)
{
log_err("Not Implemented.", "");
BUG_ON(1);
return 0;
}
|