2 * mkquota.c --- create quota files for a filesystem
4 * Aditya Kali <adityakali@google.com>
14 #include "ext2fs/ext2_fs.h"
15 #include "ext2fs/ext2fs.h"
19 #include "quotaio_v2.h"
20 #include "quotaio_tree.h"
24 /* Needed for architectures where sizeof(int) != sizeof(void *) */
25 #define UINT_TO_VOIDPTR(val) ((void *)(intptr_t)(val))
26 #define VOIDPTR_TO_UINT(ptr) ((unsigned int)(intptr_t)(ptr))
29 static void print_inode(struct ext2_inode *inode)
34 fprintf(stderr, " i_mode = %d\n", inode->i_mode);
35 fprintf(stderr, " i_uid = %d\n", inode->i_uid);
36 fprintf(stderr, " i_size = %d\n", inode->i_size);
37 fprintf(stderr, " i_atime = %d\n", inode->i_atime);
38 fprintf(stderr, " i_ctime = %d\n", inode->i_ctime);
39 fprintf(stderr, " i_mtime = %d\n", inode->i_mtime);
40 fprintf(stderr, " i_dtime = %d\n", inode->i_dtime);
41 fprintf(stderr, " i_gid = %d\n", inode->i_gid);
42 fprintf(stderr, " i_links_count = %d\n", inode->i_links_count);
43 fprintf(stderr, " i_blocks = %d\n", inode->i_blocks);
44 fprintf(stderr, " i_flags = %d\n", inode->i_flags);
49 static void print_dquot(const char *desc, struct dquot *dq)
52 fprintf(stderr, "%s: ", desc);
53 fprintf(stderr, "%u %lld:%lld:%lld %lld:%lld:%lld\n",
54 dq->dq_id, dq->dq_dqb.dqb_curspace,
55 dq->dq_dqb.dqb_bsoftlimit, dq->dq_dqb.dqb_bhardlimit,
56 dq->dq_dqb.dqb_curinodes,
57 dq->dq_dqb.dqb_isoftlimit, dq->dq_dqb.dqb_ihardlimit);
60 static void print_dquot(const char *desc EXT2FS_ATTR((unused)),
61 struct dquot *dq EXT2FS_ATTR((unused)))
67 * Returns 0 if not able to find the quota file, otherwise returns its
70 int quota_file_exists(ext2_filsys fs, enum quota_type qtype)
76 if (qtype >= MAXQUOTAS)
79 quota_get_qf_name(qtype, QFMT_VFS_V1, qf_name);
81 ret = ext2fs_lookup(fs, EXT2_ROOT_INO, qf_name, strlen(qf_name), 0,
90 * Set the value for reserved quota inode number field in superblock.
92 void quota_set_sb_inum(ext2_filsys fs, ext2_ino_t ino, enum quota_type qtype)
96 inump = quota_sb_inump(fs->super, qtype);
98 log_debug("setting quota ino in superblock: ino=%u, type=%d", ino,
101 ext2fs_mark_super_dirty(fs);
104 errcode_t quota_remove_inode(ext2_filsys fs, enum quota_type qtype)
109 retval = ext2fs_read_bitmaps(fs);
111 log_err("Couldn't read bitmaps: %s", error_message(retval));
114 qf_ino = *quota_sb_inump(fs->super, qtype);
115 if (qf_ino < EXT2_FIRST_INODE(fs->super)) {
116 quota_inode_truncate(fs, qf_ino);
118 struct ext2_inode inode;
120 quota_inode_truncate(fs, qf_ino);
121 retval = ext2fs_read_inode(fs, qf_ino, &inode);
123 memset(&inode, 0, sizeof(struct ext2_inode));
124 ext2fs_write_inode(fs, qf_ino, &inode);
126 ext2fs_inode_alloc_stats2(fs, qf_ino, -1, 0);
127 ext2fs_mark_ib_dirty(fs);
130 quota_set_sb_inum(fs, 0, qtype);
132 ext2fs_mark_super_dirty(fs);
133 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
134 retval = ext2fs_write_bitmaps(fs);
136 log_err("Couldn't write bitmaps: %s", error_message(retval));
142 static void write_dquots(dict_t *dict, struct quota_handle *qh)
147 for (n = dict_first(dict); n; n = dict_next(dict, n)) {
150 print_dquot("write", dq);
152 update_grace_times(dq);
153 qh->qh_ops->commit_dquot(dq);
158 errcode_t quota_write_inode(quota_ctx_t qctx, unsigned int qtype_bits)
161 enum quota_type qtype;
164 struct quota_handle *h = NULL;
165 int fmt = QFMT_VFS_V1;
171 retval = ext2fs_get_mem(sizeof(struct quota_handle), &h);
173 log_err("Unable to allocate quota handle: %s",
174 error_message(retval));
178 retval = ext2fs_read_bitmaps(fs);
180 log_err("Couldn't read bitmaps: %s", error_message(retval));
184 for (qtype = 0; qtype < MAXQUOTAS; qtype++) {
185 if (((1 << qtype) & qtype_bits) == 0)
188 dict = qctx->quota_dict[qtype];
192 retval = quota_file_create(h, fs, qtype, fmt);
194 log_err("Cannot initialize io on quotafile");
198 write_dquots(dict, h);
199 retval = quota_file_close(qctx, h);
201 log_err("Cannot finish IO on new quotafile: %s",
203 if (h->qh_qf.e2_file)
204 ext2fs_file_close(h->qh_qf.e2_file);
205 quota_inode_truncate(fs, h->qh_qf.ino);
209 /* Set quota inode numbers in superblock. */
210 quota_set_sb_inum(fs, h->qh_qf.ino, qtype);
211 ext2fs_mark_super_dirty(fs);
212 ext2fs_mark_bb_dirty(fs);
213 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
216 retval = ext2fs_write_bitmaps(fs);
218 log_err("Couldn't write bitmaps: %s", error_message(retval));
227 /******************************************************************/
228 /* Helper functions for computing quota in memory. */
229 /******************************************************************/
231 static int dict_uint_cmp(const void *a, const void *b)
235 c = VOIDPTR_TO_UINT(a);
236 d = VOIDPTR_TO_UINT(b);
246 static inline qid_t get_qid(struct ext2_inode *inode, enum quota_type qtype)
248 struct ext2_inode_large *large_inode;
253 return inode_uid(*inode);
255 return inode_gid(*inode);
257 large_inode = (struct ext2_inode_large *)inode;
258 inode_size = EXT2_GOOD_OLD_INODE_SIZE +
259 large_inode->i_extra_isize;
260 if (inode_includes(inode_size, i_projid))
261 return inode_projid(*large_inode);
269 static void quota_dnode_free(dnode_t *node,
270 void *context EXT2FS_ATTR((unused)))
272 void *ptr = node ? dnode_get(node) : 0;
274 ext2fs_free_mem(&ptr);
279 * Set up the quota tracking data structures.
281 errcode_t quota_init_context(quota_ctx_t *qctx, ext2_filsys fs,
282 unsigned int qtype_bits)
287 enum quota_type qtype;
289 err = ext2fs_get_mem(sizeof(struct quota_ctx), &ctx);
291 log_err("Failed to allocate quota context");
295 memset(ctx, 0, sizeof(struct quota_ctx));
296 for (qtype = 0; qtype < MAXQUOTAS; qtype++) {
297 ctx->quota_file[qtype] = NULL;
298 if (((1 << qtype) & qtype_bits) == 0)
300 err = ext2fs_get_mem(sizeof(dict_t), &dict);
302 log_err("Failed to allocate dictionary");
303 quota_release_context(&ctx);
306 ctx->quota_dict[qtype] = dict;
307 dict_init(dict, DICTCOUNT_T_MAX, dict_uint_cmp);
308 dict_set_allocator(dict, NULL, quota_dnode_free, NULL);
316 void quota_release_context(quota_ctx_t *qctx)
320 enum quota_type qtype;
327 for (qtype = 0; qtype < MAXQUOTAS; qtype++) {
328 dict = ctx->quota_dict[qtype];
329 ctx->quota_dict[qtype] = 0;
331 dict_free_nodes(dict);
334 if (ctx->quota_file[qtype]) {
335 err = quota_file_close(ctx, ctx->quota_file[qtype]);
337 log_err("Cannot close quotafile: %s",
339 ext2fs_free_mem(&ctx->quota_file[qtype]);
347 static struct dquot *get_dq(dict_t *dict, __u32 key)
352 n = dict_lookup(dict, UINT_TO_VOIDPTR(key));
356 if (ext2fs_get_mem(sizeof(struct dquot), &dq)) {
357 log_err("Unable to allocate dquot");
360 memset(dq, 0, sizeof(struct dquot));
361 dict_alloc_insert(dict, UINT_TO_VOIDPTR(key), dq);
369 * Called to update the blocks used by a particular inode
371 void quota_data_add(quota_ctx_t qctx, struct ext2_inode *inode,
372 ext2_ino_t ino EXT2FS_ATTR((unused)),
377 enum quota_type qtype;
382 log_debug("ADD_DATA: Inode: %u, UID/GID: %u/%u, space: %ld", ino,
384 inode_gid(*inode), space);
385 for (qtype = 0; qtype < MAXQUOTAS; qtype++) {
386 dict = qctx->quota_dict[qtype];
388 dq = get_dq(dict, get_qid(inode, qtype));
390 dq->dq_dqb.dqb_curspace += space;
396 * Called to remove some blocks used by a particular inode
398 void quota_data_sub(quota_ctx_t qctx, struct ext2_inode *inode,
399 ext2_ino_t ino EXT2FS_ATTR((unused)),
404 enum quota_type qtype;
409 log_debug("SUB_DATA: Inode: %u, UID/GID: %u/%u, space: %ld", ino,
411 inode_gid(*inode), space);
412 for (qtype = 0; qtype < MAXQUOTAS; qtype++) {
413 dict = qctx->quota_dict[qtype];
415 dq = get_dq(dict, get_qid(inode, qtype));
416 dq->dq_dqb.dqb_curspace -= space;
422 * Called to count the files used by an inode's user/group
424 void quota_data_inodes(quota_ctx_t qctx, struct ext2_inode *inode,
425 ext2_ino_t ino EXT2FS_ATTR((unused)), int adjust)
429 enum quota_type qtype;
434 log_debug("ADJ_INODE: Inode: %u, UID/GID: %u/%u, adjust: %d", ino,
436 inode_gid(*inode), adjust);
437 for (qtype = 0; qtype < MAXQUOTAS; qtype++) {
438 dict = qctx->quota_dict[qtype];
440 dq = get_dq(dict, get_qid(inode, qtype));
441 dq->dq_dqb.dqb_curinodes += adjust;
446 errcode_t quota_compute_usage(quota_ctx_t qctx)
451 struct ext2_inode inode;
453 ext2_inode_scan scan;
459 ret = ext2fs_open_inode_scan(fs, 0, &scan);
461 log_err("while opening inode scan. ret=%ld", ret);
466 ret = ext2fs_get_next_inode(scan, &ino, &inode);
468 log_err("while getting next inode. ret=%ld", ret);
469 ext2fs_close_inode_scan(scan);
474 if (inode.i_links_count &&
475 (ino == EXT2_ROOT_INO ||
476 ino >= EXT2_FIRST_INODE(fs->super))) {
477 space = ext2fs_inode_i_blocks(fs, &inode) << 9;
478 quota_data_add(qctx, &inode, ino, space);
479 quota_data_inodes(qctx, &inode, ino, +1);
483 ext2fs_close_inode_scan(scan);
488 struct scan_dquots_data {
490 int update_limits; /* update limits from disk */
492 int usage_is_inconsistent;
495 static int scan_dquots_callback(struct dquot *dquot, void *cb_data)
497 struct scan_dquots_data *scan_data = cb_data;
498 dict_t *quota_dict = scan_data->quota_dict;
501 dq = get_dq(quota_dict, dquot->dq_id);
502 dq->dq_id = dquot->dq_id;
503 dq->dq_flags |= DQF_SEEN;
505 print_dquot("mem", dq);
506 print_dquot("dsk", dquot);
508 /* Check if there is inconsistancy. */
509 if (dq->dq_dqb.dqb_curspace != dquot->dq_dqb.dqb_curspace ||
510 dq->dq_dqb.dqb_curinodes != dquot->dq_dqb.dqb_curinodes) {
511 scan_data->usage_is_inconsistent = 1;
512 fprintf(stderr, "[QUOTA WARNING] Usage inconsistent for ID %d:"
513 "actual (%llu, %llu) != expected (%llu, %llu)\n",
514 dq->dq_id, (long long)dq->dq_dqb.dqb_curspace,
515 (long long)dq->dq_dqb.dqb_curinodes,
516 (long long)dquot->dq_dqb.dqb_curspace,
517 (long long)dquot->dq_dqb.dqb_curinodes);
520 if (scan_data->update_limits) {
521 dq->dq_dqb.dqb_ihardlimit = dquot->dq_dqb.dqb_ihardlimit;
522 dq->dq_dqb.dqb_isoftlimit = dquot->dq_dqb.dqb_isoftlimit;
523 dq->dq_dqb.dqb_bhardlimit = dquot->dq_dqb.dqb_bhardlimit;
524 dq->dq_dqb.dqb_bsoftlimit = dquot->dq_dqb.dqb_bsoftlimit;
527 if (scan_data->update_usage) {
528 dq->dq_dqb.dqb_curspace = dquot->dq_dqb.dqb_curspace;
529 dq->dq_dqb.dqb_curinodes = dquot->dq_dqb.dqb_curinodes;
536 * Read all dquots from quota file into memory
538 static errcode_t quota_read_all_dquots(struct quota_handle *qh,
539 quota_ctx_t qctx, int update_limits)
541 struct scan_dquots_data scan_data;
543 scan_data.quota_dict = qctx->quota_dict[qh->qh_type];
544 scan_data.update_limits = update_limits;
545 scan_data.update_usage = 0;
547 return qh->qh_ops->scan_dquots(qh, scan_dquots_callback, &scan_data);
551 * Write all memory dquots into quota file
553 #if 0 /* currently unused, but may be useful in the future? */
554 static errcode_t quota_write_all_dquots(struct quota_handle *qh,
559 err = ext2fs_read_bitmaps(qctx->fs);
562 write_dquots(qctx->quota_dict[qh->qh_type], qh);
563 ext2fs_mark_bb_dirty(qctx->fs);
564 qctx->fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
565 ext2fs_write_bitmaps(qctx->fs);
571 * Updates the in-memory quota limits from the given quota inode.
573 errcode_t quota_update_limits(quota_ctx_t qctx, ext2_ino_t qf_ino,
574 enum quota_type qtype)
576 struct quota_handle *qh;
582 err = ext2fs_get_mem(sizeof(struct quota_handle), &qh);
584 log_err("Unable to allocate quota handle");
588 err = quota_file_open(qctx, qh, qf_ino, qtype, -1, 0);
590 log_err("Open quota file failed");
594 quota_read_all_dquots(qh, qctx, 1);
596 err = quota_file_close(qctx, qh);
598 log_err("Cannot finish IO on new quotafile: %s",
600 if (qh->qh_qf.e2_file)
601 ext2fs_file_close(qh->qh_qf.e2_file);
604 ext2fs_free_mem(&qh);
609 * Compares the measured quota in qctx->quota_dict with that in the quota inode
610 * on disk and updates the limits in qctx->quota_dict. 'usage_inconsistent' is
611 * set to 1 if the supplied and on-disk quota usage values are not identical.
613 errcode_t quota_compare_and_update(quota_ctx_t qctx, enum quota_type qtype,
614 int *usage_inconsistent)
616 struct quota_handle qh;
617 struct scan_dquots_data scan_data;
620 dict_t *dict = qctx->quota_dict[qtype];
626 err = quota_file_open(qctx, &qh, 0, qtype, -1, 0);
628 log_err("Open quota file failed");
632 scan_data.quota_dict = qctx->quota_dict[qtype];
633 scan_data.update_limits = 1;
634 scan_data.update_usage = 0;
635 scan_data.usage_is_inconsistent = 0;
636 err = qh.qh_ops->scan_dquots(&qh, scan_dquots_callback, &scan_data);
638 log_err("Error scanning dquots");
642 for (n = dict_first(dict); n; n = dict_next(dict, n)) {
646 if ((dq->dq_flags & DQF_SEEN) == 0) {
647 fprintf(stderr, "[QUOTA WARNING] "
648 "Missing quota entry ID %d\n", dq->dq_id);
649 scan_data.usage_is_inconsistent = 1;
652 *usage_inconsistent = scan_data.usage_is_inconsistent;
655 err = quota_file_close(qctx, &qh);
657 log_err("Cannot close quotafile: %s", error_message(errno));
658 if (qh.qh_qf.e2_file)
659 ext2fs_file_close(qh.qh_qf.e2_file);
665 int parse_quota_opts(const char *opts, int (*func)(), void *data)
667 char *buf, *token, *next, *p;
672 buf = malloc(len + 1);
675 "Couldn't allocate memory to parse quota options!\n");
679 for (token = buf; token && *token; token = next) {
680 p = strchr(token, ',');
686 ret = func(token, data);