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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
|
/** quotaio.c
*
* Generic IO operations on quotafiles
* Jan Kara <jack@suse.cz> - sponsored by SuSE CR
* Aditya Kali <adityakali@google.com> - Ported to e2fsprogs
*/
#include "config.h"
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <assert.h>
#include "common.h"
#include "quotaio.h"
static const char * const extensions[MAXQUOTAS] = {
[USRQUOTA] = "user",
[GRPQUOTA] = "group",
[PRJQUOTA] = "project",
};
static const char * const basenames[] = {
"", /* undefined */
"quota", /* QFMT_VFS_OLD */
"aquota", /* QFMT_VFS_V0 */
"", /* QFMT_OCFS2 */
"aquota" /* QFMT_VFS_V1 */
};
/* Header in all newer quotafiles */
struct disk_dqheader {
__le32 dqh_magic;
__le32 dqh_version;
} __attribute__ ((packed));
/**
* Convert type of quota to written representation
*/
const char *quota_type2name(enum quota_type qtype)
{
if (qtype >= MAXQUOTAS)
return "unknown";
return extensions[qtype];
}
ext2_ino_t quota_type2inum(enum quota_type qtype,
struct ext2_super_block *sb)
{
switch (qtype) {
case USRQUOTA:
return EXT4_USR_QUOTA_INO;
case GRPQUOTA:
return EXT4_GRP_QUOTA_INO;
case PRJQUOTA:
return sb->s_prj_quota_inum;
default:
return 0;
}
return 0;
}
/**
* Creates a quota file name for given type and format.
*/
const char *quota_get_qf_name(enum quota_type type, int fmt, char *buf)
{
if (!buf)
return NULL;
snprintf(buf, QUOTA_NAME_LEN, "%s.%s",
basenames[fmt], extensions[type]);
return buf;
}
/*
* Set grace time if needed
*/
void update_grace_times(struct dquot *q)
{
time_t now;
time(&now);
if (q->dq_dqb.dqb_bsoftlimit && toqb(q->dq_dqb.dqb_curspace) >
q->dq_dqb.dqb_bsoftlimit) {
if (!q->dq_dqb.dqb_btime)
q->dq_dqb.dqb_btime =
now + q->dq_h->qh_info.dqi_bgrace;
} else {
q->dq_dqb.dqb_btime = 0;
}
if (q->dq_dqb.dqb_isoftlimit && q->dq_dqb.dqb_curinodes >
q->dq_dqb.dqb_isoftlimit) {
if (!q->dq_dqb.dqb_itime)
q->dq_dqb.dqb_itime =
now + q->dq_h->qh_info.dqi_igrace;
} else {
q->dq_dqb.dqb_itime = 0;
}
}
errcode_t quota_inode_truncate(ext2_filsys fs, ext2_ino_t ino)
{
struct ext2_inode inode;
errcode_t err;
enum quota_type qtype;
if ((err = ext2fs_read_inode(fs, ino, &inode)))
return err;
for (qtype = 0; qtype < MAXQUOTAS; qtype++)
if (ino == quota_type2inum(qtype, fs->super))
break;
if (qtype != MAXQUOTAS) {
ext2fs_set_dtime(fs, &inode);
if (!ext2fs_inode_has_valid_blocks2(fs, &inode))
return 0;
err = ext2fs_punch(fs, ino, &inode, NULL, 0, ~0ULL);
if (err)
return err;
fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
memset(&inode, 0, sizeof(struct ext2_inode));
} else {
inode.i_flags &= ~EXT2_IMMUTABLE_FL;
}
err = ext2fs_write_inode(fs, ino, &inode);
return err;
}
/* Functions to read/write quota file. */
static unsigned int quota_write_nomount(struct quota_file *qf,
ext2_loff_t offset,
void *buf, unsigned int size)
{
ext2_file_t e2_file = qf->e2_file;
unsigned int bytes_written = 0;
errcode_t err;
err = ext2fs_file_llseek(e2_file, offset, EXT2_SEEK_SET, NULL);
if (err) {
log_err("ext2fs_file_llseek failed: %ld", err);
return 0;
}
err = ext2fs_file_write(e2_file, buf, size, &bytes_written);
if (err) {
log_err("ext2fs_file_write failed: %ld", err);
return 0;
}
/* Correct inode.i_size is set in end_io. */
return bytes_written;
}
static unsigned int quota_read_nomount(struct quota_file *qf,
ext2_loff_t offset,
void *buf, unsigned int size)
{
ext2_file_t e2_file = qf->e2_file;
unsigned int bytes_read = 0;
errcode_t err;
err = ext2fs_file_llseek(e2_file, offset, EXT2_SEEK_SET, NULL);
if (err) {
log_err("ext2fs_file_llseek failed: %ld", err);
return 0;
}
err = ext2fs_file_read(e2_file, buf, size, &bytes_read);
if (err) {
log_err("ext2fs_file_read failed: %ld", err);
return 0;
}
return bytes_read;
}
/*
* Detect quota format and initialize quota IO
*/
errcode_t quota_file_open(quota_ctx_t qctx, struct quota_handle *h,
ext2_ino_t qf_ino, enum quota_type qtype,
int fmt, int flags)
{
ext2_filsys fs = qctx->fs;
ext2_file_t e2_file;
errcode_t err;
int allocated_handle = 0;
if (qtype >= MAXQUOTAS)
return EINVAL;
if (fmt == -1)
fmt = QFMT_VFS_V1;
err = ext2fs_read_bitmaps(fs);
if (err)
return err;
if (qf_ino == 0)
qf_ino = *quota_sb_inump(fs->super, qtype);
log_debug("Opening quota ino=%u, type=%d", qf_ino, qtype);
err = ext2fs_file_open(fs, qf_ino, flags, &e2_file);
if (err) {
log_err("ext2fs_file_open failed: %s", error_message(err));
return err;
}
if (!h) {
if (qctx->quota_file[qtype]) {
h = qctx->quota_file[qtype];
if (((flags & EXT2_FILE_WRITE) == 0) ||
(h->qh_file_flags & EXT2_FILE_WRITE)) {
ext2fs_file_close(e2_file);
return 0;
}
(void) quota_file_close(qctx, h);
}
err = ext2fs_get_mem(sizeof(struct quota_handle), &h);
if (err) {
log_err("Unable to allocate quota handle");
ext2fs_file_close(e2_file);
return err;
}
allocated_handle = 1;
}
h->qh_qf.e2_file = e2_file;
h->qh_qf.fs = fs;
h->qh_qf.ino = qf_ino;
h->e2fs_write = quota_write_nomount;
h->e2fs_read = quota_read_nomount;
h->qh_file_flags = flags;
h->qh_io_flags = 0;
h->qh_type = qtype;
h->qh_fmt = fmt;
memset(&h->qh_info, 0, sizeof(h->qh_info));
h->qh_ops = "afile_ops_2;
if (h->qh_ops->check_file &&
(h->qh_ops->check_file(h, qtype, fmt) == 0)) {
log_err("qh_ops->check_file failed");
err = EIO;
goto errout;
}
if (h->qh_ops->init_io && (h->qh_ops->init_io(h) < 0)) {
log_err("qh_ops->init_io failed");
err = EIO;
goto errout;
}
if (allocated_handle)
qctx->quota_file[qtype] = h;
return 0;
errout:
ext2fs_file_close(e2_file);
if (allocated_handle)
ext2fs_free_mem(&h);
return err;
}
static errcode_t quota_inode_init_new(ext2_filsys fs, ext2_ino_t ino)
{
struct ext2_inode inode;
errcode_t err = 0;
time_t now;
err = ext2fs_read_inode(fs, ino, &inode);
if (err) {
log_err("ex2fs_read_inode failed");
return err;
}
if (EXT2_I_SIZE(&inode)) {
err = quota_inode_truncate(fs, ino);
if (err)
return err;
}
memset(&inode, 0, sizeof(struct ext2_inode));
ext2fs_iblk_set(fs, &inode, 0);
now = fs->now ? fs->now : time(0);
ext2fs_inode_xtime_set(&inode, i_atime, now);
ext2fs_inode_xtime_set(&inode, i_ctime, now);
ext2fs_inode_xtime_set(&inode, i_mtime, now);
inode.i_links_count = 1;
inode.i_mode = LINUX_S_IFREG | 0600;
inode.i_flags |= EXT2_IMMUTABLE_FL;
if (ext2fs_has_feature_extents(fs->super))
inode.i_flags |= EXT4_EXTENTS_FL;
err = ext2fs_write_new_inode(fs, ino, &inode);
if (err) {
log_err("ext2fs_write_new_inode failed: %ld", err);
return err;
}
return err;
}
/*
* Create new quotafile of specified format on given filesystem
*/
errcode_t quota_file_create(struct quota_handle *h, ext2_filsys fs,
enum quota_type qtype, int fmt)
{
ext2_file_t e2_file;
errcode_t err;
ext2_ino_t qf_inum = 0;
if (fmt == -1)
fmt = QFMT_VFS_V1;
h->qh_qf.fs = fs;
qf_inum = quota_type2inum(qtype, fs->super);
if (qf_inum == 0 && qtype == PRJQUOTA) {
err = ext2fs_new_inode(fs, EXT2_ROOT_INO, LINUX_S_IFREG | 0600,
0, &qf_inum);
if (err)
return err;
ext2fs_inode_alloc_stats2(fs, qf_inum, +1, 0);
ext2fs_mark_ib_dirty(fs);
} else if (qf_inum == 0) {
return EXT2_ET_BAD_INODE_NUM;
}
err = ext2fs_read_bitmaps(fs);
if (err)
goto out_err;
err = quota_inode_init_new(fs, qf_inum);
if (err) {
log_err("init_new_quota_inode failed");
goto out_err;
}
h->qh_qf.ino = qf_inum;
h->qh_file_flags = EXT2_FILE_WRITE | EXT2_FILE_CREATE;
h->e2fs_write = quota_write_nomount;
h->e2fs_read = quota_read_nomount;
log_debug("Creating quota ino=%u, type=%d", qf_inum, qtype);
err = ext2fs_file_open(fs, qf_inum, h->qh_file_flags, &e2_file);
if (err) {
log_err("ext2fs_file_open failed: %ld", err);
goto out_err;
}
h->qh_qf.e2_file = e2_file;
h->qh_io_flags = 0;
h->qh_type = qtype;
h->qh_fmt = fmt;
memset(&h->qh_info, 0, sizeof(h->qh_info));
h->qh_ops = "afile_ops_2;
if (h->qh_ops->new_io && (h->qh_ops->new_io(h) < 0)) {
log_err("qh_ops->new_io failed");
err = EIO;
goto out_err1;
}
return 0;
out_err1:
ext2fs_file_close(e2_file);
out_err:
if (qf_inum)
quota_inode_truncate(fs, qf_inum);
return err;
}
/*
* Close quotafile and release handle
*/
errcode_t quota_file_close(quota_ctx_t qctx, struct quota_handle *h)
{
if (h->qh_io_flags & IOFL_INFODIRTY) {
if (h->qh_ops->write_info && h->qh_ops->write_info(h) < 0)
return EIO;
h->qh_io_flags &= ~IOFL_INFODIRTY;
}
if (h->qh_ops->end_io && h->qh_ops->end_io(h) < 0)
return EIO;
if (h->qh_qf.e2_file)
ext2fs_file_close(h->qh_qf.e2_file);
if (qctx->quota_file[h->qh_type] == h)
ext2fs_free_mem(&qctx->quota_file[h->qh_type]);
return 0;
}
/*
* Create empty quota structure
*/
struct dquot *get_empty_dquot(void)
{
struct dquot *dquot;
if (ext2fs_get_memzero(sizeof(struct dquot), &dquot)) {
log_err("Failed to allocate dquot");
return NULL;
}
dquot->dq_id = -1;
return dquot;
}
|