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, (long long) dq->dq_dqb.dqb_curspace,
55 (long long) dq->dq_dqb.dqb_bsoftlimit,
56 (long long) dq->dq_dqb.dqb_bhardlimit,
57 (long long) dq->dq_dqb.dqb_curinodes,
58 (long long) dq->dq_dqb.dqb_isoftlimit,
59 (long long) dq->dq_dqb.dqb_ihardlimit);
62 static void print_dquot(const char *desc EXT2FS_ATTR((unused)),
63 struct dquot *dq EXT2FS_ATTR((unused)))
69 * Returns 0 if not able to find the quota file, otherwise returns its
72 int quota_file_exists(ext2_filsys fs, enum quota_type qtype)
78 if (qtype >= MAXQUOTAS)
81 quota_get_qf_name(qtype, QFMT_VFS_V1, qf_name);
83 ret = ext2fs_lookup(fs, EXT2_ROOT_INO, qf_name, strlen(qf_name), 0,
92 * Set the value for reserved quota inode number field in superblock.
94 void quota_set_sb_inum(ext2_filsys fs, ext2_ino_t ino, enum quota_type qtype)
98 inump = quota_sb_inump(fs->super, qtype);
100 log_debug("setting quota ino in superblock: ino=%u, type=%d", ino,
105 ext2fs_mark_super_dirty(fs);
108 errcode_t quota_remove_inode(ext2_filsys fs, enum quota_type qtype)
113 retval = ext2fs_read_bitmaps(fs);
115 log_debug("Couldn't read bitmaps: %s", error_message(retval));
119 qf_ino = *quota_sb_inump(fs->super, qtype);
122 retval = quota_inode_truncate(fs, qf_ino);
125 if (qf_ino >= EXT2_FIRST_INODE(fs->super)) {
126 struct ext2_inode inode;
128 retval = ext2fs_read_inode(fs, qf_ino, &inode);
130 memset(&inode, 0, sizeof(struct ext2_inode));
131 ext2fs_write_inode(fs, qf_ino, &inode);
133 ext2fs_inode_alloc_stats2(fs, qf_ino, -1, 0);
134 ext2fs_mark_ib_dirty(fs);
137 quota_set_sb_inum(fs, 0, qtype);
139 ext2fs_mark_super_dirty(fs);
140 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
141 retval = ext2fs_write_bitmaps(fs);
143 log_debug("Couldn't write bitmaps: %s", error_message(retval));
149 static void write_dquots(dict_t *dict, struct quota_handle *qh)
154 for (n = dict_first(dict); n; n = dict_next(dict, n)) {
157 print_dquot("write", dq);
159 update_grace_times(dq);
160 qh->qh_ops->commit_dquot(dq);
165 errcode_t quota_write_inode(quota_ctx_t qctx, unsigned int qtype_bits)
168 enum quota_type qtype;
171 struct quota_handle *h = NULL;
172 int fmt = QFMT_VFS_V1;
178 retval = ext2fs_get_mem(sizeof(struct quota_handle), &h);
180 log_debug("Unable to allocate quota handle: %s",
181 error_message(retval));
185 retval = ext2fs_read_bitmaps(fs);
187 log_debug("Couldn't read bitmaps: %s", error_message(retval));
191 for (qtype = 0; qtype < MAXQUOTAS; qtype++) {
192 if (((1 << qtype) & qtype_bits) == 0)
195 dict = qctx->quota_dict[qtype];
199 retval = quota_file_create(h, fs, qtype, fmt);
201 log_debug("Cannot initialize io on quotafile: %s",
202 error_message(retval));
206 write_dquots(dict, h);
207 retval = quota_file_close(qctx, h);
209 log_debug("Cannot finish IO on new quotafile: %s",
211 if (h->qh_qf.e2_file)
212 ext2fs_file_close(h->qh_qf.e2_file);
213 (void) quota_inode_truncate(fs, h->qh_qf.ino);
217 /* Set quota inode numbers in superblock. */
218 quota_set_sb_inum(fs, h->qh_qf.ino, qtype);
219 ext2fs_mark_super_dirty(fs);
220 ext2fs_mark_bb_dirty(fs);
221 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
224 retval = ext2fs_write_bitmaps(fs);
226 log_debug("Couldn't write bitmaps: %s", error_message(retval));
235 /******************************************************************/
236 /* Helper functions for computing quota in memory. */
237 /******************************************************************/
239 static int dict_uint_cmp(const void *cmp_ctx EXT2FS_ATTR((unused)),
240 const void *a, const void *b)
244 c = VOIDPTR_TO_UINT(a);
245 d = VOIDPTR_TO_UINT(b);
255 static inline int project_quota_valid(quota_ctx_t qctx)
257 return (EXT2_INODE_SIZE(qctx->fs->super) > EXT2_GOOD_OLD_INODE_SIZE);
260 static inline qid_t get_qid(struct ext2_inode_large *inode, enum quota_type qtype)
262 unsigned int inode_size;
266 return inode_uid(*inode);
268 return inode_gid(*inode);
270 inode_size = EXT2_GOOD_OLD_INODE_SIZE +
271 inode->i_extra_isize;
272 if (inode_includes(inode_size, i_projid))
273 return inode_projid(*inode);
282 static void quota_dnode_free(dnode_t *node,
283 void *context EXT2FS_ATTR((unused)))
285 void *ptr = node ? dnode_get(node) : 0;
287 ext2fs_free_mem(&ptr);
292 * Set up the quota tracking data structures.
294 errcode_t quota_init_context(quota_ctx_t *qctx, ext2_filsys fs,
295 unsigned int qtype_bits)
300 enum quota_type qtype;
302 err = ext2fs_get_mem(sizeof(struct quota_ctx), &ctx);
304 log_debug("Failed to allocate quota context");
308 memset(ctx, 0, sizeof(struct quota_ctx));
309 for (qtype = 0; qtype < MAXQUOTAS; qtype++) {
310 ctx->quota_file[qtype] = NULL;
312 if (((1 << qtype) & qtype_bits) == 0)
315 if (*quota_sb_inump(fs->super, qtype) == 0)
318 err = ext2fs_get_mem(sizeof(dict_t), &dict);
320 log_debug("Failed to allocate dictionary");
321 quota_release_context(&ctx);
324 ctx->quota_dict[qtype] = dict;
325 dict_init(dict, DICTCOUNT_T_MAX, dict_uint_cmp);
326 dict_set_allocator(dict, NULL, quota_dnode_free, NULL);
334 void quota_release_context(quota_ctx_t *qctx)
338 enum quota_type qtype;
345 for (qtype = 0; qtype < MAXQUOTAS; qtype++) {
346 dict = ctx->quota_dict[qtype];
347 ctx->quota_dict[qtype] = 0;
349 dict_free_nodes(dict);
352 if (ctx->quota_file[qtype]) {
353 err = quota_file_close(ctx, ctx->quota_file[qtype]);
355 log_err("Cannot close quotafile: %s",
357 ext2fs_free_mem(&ctx->quota_file[qtype]);
365 static struct dquot *get_dq(dict_t *dict, __u32 key)
370 n = dict_lookup(dict, UINT_TO_VOIDPTR(key));
374 if (ext2fs_get_mem(sizeof(struct dquot), &dq)) {
375 log_err("Unable to allocate dquot");
378 memset(dq, 0, sizeof(struct dquot));
379 dict_alloc_insert(dict, UINT_TO_VOIDPTR(key), dq);
387 * Called to update the blocks used by a particular inode
389 void quota_data_add(quota_ctx_t qctx, struct ext2_inode_large *inode,
390 ext2_ino_t ino EXT2FS_ATTR((unused)),
395 enum quota_type qtype;
400 log_debug("ADD_DATA: Inode: %u, UID/GID: %u/%u, space: %ld", ino,
402 inode_gid(*inode), space);
403 for (qtype = 0; qtype < MAXQUOTAS; qtype++) {
404 if (qtype == PRJQUOTA && !project_quota_valid(qctx))
406 dict = qctx->quota_dict[qtype];
408 dq = get_dq(dict, get_qid(inode, qtype));
410 dq->dq_dqb.dqb_curspace += space;
416 * Called to remove some blocks used by a particular inode
418 void quota_data_sub(quota_ctx_t qctx, struct ext2_inode_large *inode,
419 ext2_ino_t ino EXT2FS_ATTR((unused)),
424 enum quota_type qtype;
429 log_debug("SUB_DATA: Inode: %u, UID/GID: %u/%u, space: %ld", ino,
431 inode_gid(*inode), space);
432 for (qtype = 0; qtype < MAXQUOTAS; qtype++) {
433 if (qtype == PRJQUOTA && !project_quota_valid(qctx))
435 dict = qctx->quota_dict[qtype];
437 dq = get_dq(dict, get_qid(inode, qtype));
439 dq->dq_dqb.dqb_curspace -= space;
445 * Called to count the files used by an inode's user/group
447 void quota_data_inodes(quota_ctx_t qctx, struct ext2_inode_large *inode,
448 ext2_ino_t ino EXT2FS_ATTR((unused)), int adjust)
452 enum quota_type qtype;
457 log_debug("ADJ_INODE: Inode: %u, UID/GID: %u/%u, adjust: %d", ino,
459 inode_gid(*inode), adjust);
460 for (qtype = 0; qtype < MAXQUOTAS; qtype++) {
461 if (qtype == PRJQUOTA && !project_quota_valid(qctx))
463 dict = qctx->quota_dict[qtype];
465 dq = get_dq(dict, get_qid(inode, qtype));
467 dq->dq_dqb.dqb_curinodes += adjust;
472 errcode_t quota_compute_usage(quota_ctx_t qctx)
477 struct ext2_inode_large *inode;
480 ext2_inode_scan scan;
486 ret = ext2fs_open_inode_scan(fs, 0, &scan);
488 log_err("while opening inode scan. ret=%ld", ret);
491 inode_size = fs->super->s_inode_size;
492 inode = malloc(inode_size);
494 ext2fs_close_inode_scan(scan);
498 ret = ext2fs_get_next_inode_full(scan, &ino,
499 EXT2_INODE(inode), inode_size);
501 log_err("while getting next inode. ret=%ld", ret);
502 ext2fs_close_inode_scan(scan);
508 if (!inode->i_links_count)
510 if (ino == EXT2_ROOT_INO ||
511 (ino >= EXT2_FIRST_INODE(fs->super) &&
512 ino != quota_type2inum(PRJQUOTA, fs->super) &&
513 ino != fs->super->s_orphan_file_inum)) {
514 space = ext2fs_get_stat_i_blocks(fs,
515 EXT2_INODE(inode)) << 9;
516 quota_data_add(qctx, inode, ino, space);
517 quota_data_inodes(qctx, inode, ino, +1);
521 ext2fs_close_inode_scan(scan);
526 struct scan_dquots_data {
528 int update_limits; /* update limits from disk */
530 int check_consistency;
531 int usage_is_inconsistent;
534 static int scan_dquots_callback(struct dquot *dquot, void *cb_data)
536 struct scan_dquots_data *scan_data = cb_data;
537 dict_t *quota_dict = scan_data->quota_dict;
540 dq = get_dq(quota_dict, dquot->dq_id);
543 dq->dq_id = dquot->dq_id;
544 dq->dq_flags |= DQF_SEEN;
546 print_dquot("mem", dq);
547 print_dquot("dsk", dquot);
549 /* Check if there is inconsistency */
550 if (scan_data->check_consistency &&
551 (dq->dq_dqb.dqb_curspace != dquot->dq_dqb.dqb_curspace ||
552 dq->dq_dqb.dqb_curinodes != dquot->dq_dqb.dqb_curinodes)) {
553 scan_data->usage_is_inconsistent = 1;
554 fprintf(stderr, "[QUOTA WARNING] Usage inconsistent for ID %u:"
555 "actual (%lld, %lld) != expected (%lld, %lld)\n",
556 dq->dq_id, (long long) dq->dq_dqb.dqb_curspace,
557 (long long) dq->dq_dqb.dqb_curinodes,
558 (long long) dquot->dq_dqb.dqb_curspace,
559 (long long) dquot->dq_dqb.dqb_curinodes);
562 if (scan_data->update_limits) {
563 dq->dq_dqb.dqb_ihardlimit = dquot->dq_dqb.dqb_ihardlimit;
564 dq->dq_dqb.dqb_isoftlimit = dquot->dq_dqb.dqb_isoftlimit;
565 dq->dq_dqb.dqb_bhardlimit = dquot->dq_dqb.dqb_bhardlimit;
566 dq->dq_dqb.dqb_bsoftlimit = dquot->dq_dqb.dqb_bsoftlimit;
569 if (scan_data->update_usage) {
570 dq->dq_dqb.dqb_curspace = dquot->dq_dqb.dqb_curspace;
571 dq->dq_dqb.dqb_curinodes = dquot->dq_dqb.dqb_curinodes;
578 * Read quotas from disk and updates the in-memory information determined by
579 * 'flags' from the on-disk data.
581 errcode_t quota_read_all_dquots(quota_ctx_t qctx, ext2_ino_t qf_ino,
582 enum quota_type qtype, unsigned int flags)
584 struct scan_dquots_data scan_data;
585 struct quota_handle *qh;
591 err = ext2fs_get_mem(sizeof(struct quota_handle), &qh);
593 log_debug("Unable to allocate quota handle");
597 err = quota_file_open(qctx, qh, qf_ino, qtype, -1, 0);
599 log_debug("Open quota file failed");
603 scan_data.quota_dict = qctx->quota_dict[qh->qh_type];
604 scan_data.check_consistency = 0;
605 scan_data.update_limits = !!(flags & QREAD_LIMITS);
606 scan_data.update_usage = !!(flags & QREAD_USAGE);
607 qh->qh_ops->scan_dquots(qh, scan_dquots_callback, &scan_data);
609 err = quota_file_close(qctx, qh);
611 log_debug("Cannot finish IO on new quotafile: %s",
613 if (qh->qh_qf.e2_file)
614 ext2fs_file_close(qh->qh_qf.e2_file);
617 ext2fs_free_mem(&qh);
621 static errcode_t merge_usage(dict_t *dest, dict_t *src)
624 struct dquot *src_dq, *dest_dq;
626 for (n = dict_first(src); n; n = dict_next(src, n)) {
627 src_dq = dnode_get(n);
630 dest_dq = get_dq(dest, src_dq->dq_id);
633 dest_dq->dq_dqb.dqb_curspace += src_dq->dq_dqb.dqb_curspace;
634 dest_dq->dq_dqb.dqb_curinodes += src_dq->dq_dqb.dqb_curinodes;
641 errcode_t quota_merge_and_update_usage(quota_ctx_t dest_qctx,
642 quota_ctx_t src_qctx)
645 enum quota_type qtype;
646 errcode_t retval = 0;
648 for (qtype = 0; qtype < MAXQUOTAS; qtype++) {
649 dict = src_qctx->quota_dict[qtype];
652 retval = merge_usage(dest_qctx->quota_dict[qtype], dict);
661 * Compares the measured quota in qctx->quota_dict with that in the quota inode
662 * on disk and updates the limits in qctx->quota_dict. 'usage_inconsistent' is
663 * set to 1 if the supplied and on-disk quota usage values are not identical.
665 errcode_t quota_compare_and_update(quota_ctx_t qctx, enum quota_type qtype,
666 int *usage_inconsistent)
668 struct quota_handle qh;
669 struct scan_dquots_data scan_data;
672 dict_t *dict = qctx->quota_dict[qtype];
678 err = quota_file_open(qctx, &qh, 0, qtype, -1, 0);
680 log_debug("Open quota file failed");
684 scan_data.quota_dict = qctx->quota_dict[qtype];
685 scan_data.update_limits = 1;
686 scan_data.update_usage = 0;
687 scan_data.check_consistency = 1;
688 scan_data.usage_is_inconsistent = 0;
689 err = qh.qh_ops->scan_dquots(&qh, scan_dquots_callback, &scan_data);
691 log_debug("Error scanning dquots");
692 *usage_inconsistent = 1;
696 for (n = dict_first(dict); n; n = dict_next(dict, n)) {
700 if ((dq->dq_flags & DQF_SEEN) == 0) {
701 fprintf(stderr, "[QUOTA WARNING] "
702 "Missing quota entry ID %d\n", dq->dq_id);
703 scan_data.usage_is_inconsistent = 1;
706 *usage_inconsistent = scan_data.usage_is_inconsistent;
709 err = quota_file_close(qctx, &qh);
711 log_debug("Cannot close quotafile: %s", error_message(errno));
712 if (qh.qh_qf.e2_file)
713 ext2fs_file_close(qh.qh_qf.e2_file);
719 int parse_quota_opts(const char *opts, int (*func)(char *))
721 char *buf, *token, *next, *p;
726 buf = malloc(len + 1);
729 "Couldn't allocate memory to parse quota options!\n");
733 for (token = buf; token && *token; token = next) {
734 p = strchr(token, ',');