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 space = ext2fs_get_stat_i_blocks(fs,
514 EXT2_INODE(inode)) << 9;
515 quota_data_add(qctx, inode, ino, space);
516 quota_data_inodes(qctx, inode, ino, +1);
520 ext2fs_close_inode_scan(scan);
525 struct scan_dquots_data {
527 int update_limits; /* update limits from disk */
529 int check_consistency;
530 int usage_is_inconsistent;
533 static int scan_dquots_callback(struct dquot *dquot, void *cb_data)
535 struct scan_dquots_data *scan_data = cb_data;
536 dict_t *quota_dict = scan_data->quota_dict;
539 dq = get_dq(quota_dict, dquot->dq_id);
542 dq->dq_id = dquot->dq_id;
543 dq->dq_flags |= DQF_SEEN;
545 print_dquot("mem", dq);
546 print_dquot("dsk", dquot);
548 /* Check if there is inconsistency */
549 if (scan_data->check_consistency &&
550 (dq->dq_dqb.dqb_curspace != dquot->dq_dqb.dqb_curspace ||
551 dq->dq_dqb.dqb_curinodes != dquot->dq_dqb.dqb_curinodes)) {
552 scan_data->usage_is_inconsistent = 1;
553 fprintf(stderr, "[QUOTA WARNING] Usage inconsistent for ID %u:"
554 "actual (%lld, %lld) != expected (%lld, %lld)\n",
555 dq->dq_id, (long long) dq->dq_dqb.dqb_curspace,
556 (long long) dq->dq_dqb.dqb_curinodes,
557 (long long) dquot->dq_dqb.dqb_curspace,
558 (long long) dquot->dq_dqb.dqb_curinodes);
561 if (scan_data->update_limits) {
562 dq->dq_dqb.dqb_ihardlimit = dquot->dq_dqb.dqb_ihardlimit;
563 dq->dq_dqb.dqb_isoftlimit = dquot->dq_dqb.dqb_isoftlimit;
564 dq->dq_dqb.dqb_bhardlimit = dquot->dq_dqb.dqb_bhardlimit;
565 dq->dq_dqb.dqb_bsoftlimit = dquot->dq_dqb.dqb_bsoftlimit;
568 if (scan_data->update_usage) {
569 dq->dq_dqb.dqb_curspace = dquot->dq_dqb.dqb_curspace;
570 dq->dq_dqb.dqb_curinodes = dquot->dq_dqb.dqb_curinodes;
577 * Read quotas from disk and updates the in-memory information determined by
578 * 'flags' from the on-disk data.
580 errcode_t quota_read_all_dquots(quota_ctx_t qctx, ext2_ino_t qf_ino,
581 enum quota_type qtype, unsigned int flags)
583 struct scan_dquots_data scan_data;
584 struct quota_handle *qh;
590 err = ext2fs_get_mem(sizeof(struct quota_handle), &qh);
592 log_debug("Unable to allocate quota handle");
596 err = quota_file_open(qctx, qh, qf_ino, qtype, -1, 0);
598 log_debug("Open quota file failed");
602 scan_data.quota_dict = qctx->quota_dict[qh->qh_type];
603 scan_data.check_consistency = 0;
604 scan_data.update_limits = !!(flags & QREAD_LIMITS);
605 scan_data.update_usage = !!(flags & QREAD_USAGE);
606 qh->qh_ops->scan_dquots(qh, scan_dquots_callback, &scan_data);
608 err = quota_file_close(qctx, qh);
610 log_debug("Cannot finish IO on new quotafile: %s",
612 if (qh->qh_qf.e2_file)
613 ext2fs_file_close(qh->qh_qf.e2_file);
616 ext2fs_free_mem(&qh);
620 static errcode_t merge_usage(dict_t *dest, dict_t *src)
623 struct dquot *src_dq, *dest_dq;
625 for (n = dict_first(src); n; n = dict_next(src, n)) {
626 src_dq = dnode_get(n);
629 dest_dq = get_dq(dest, src_dq->dq_id);
632 dest_dq->dq_dqb.dqb_curspace += src_dq->dq_dqb.dqb_curspace;
633 dest_dq->dq_dqb.dqb_curinodes += src_dq->dq_dqb.dqb_curinodes;
640 errcode_t quota_merge_and_update_usage(quota_ctx_t dest_qctx,
641 quota_ctx_t src_qctx)
644 enum quota_type qtype;
645 errcode_t retval = 0;
647 for (qtype = 0; qtype < MAXQUOTAS; qtype++) {
648 dict = src_qctx->quota_dict[qtype];
651 retval = merge_usage(dest_qctx->quota_dict[qtype], dict);
660 * Compares the measured quota in qctx->quota_dict with that in the quota inode
661 * on disk and updates the limits in qctx->quota_dict. 'usage_inconsistent' is
662 * set to 1 if the supplied and on-disk quota usage values are not identical.
664 errcode_t quota_compare_and_update(quota_ctx_t qctx, enum quota_type qtype,
665 int *usage_inconsistent)
667 struct quota_handle qh;
668 struct scan_dquots_data scan_data;
671 dict_t *dict = qctx->quota_dict[qtype];
677 err = quota_file_open(qctx, &qh, 0, qtype, -1, 0);
679 log_debug("Open quota file failed");
683 scan_data.quota_dict = qctx->quota_dict[qtype];
684 scan_data.update_limits = 1;
685 scan_data.update_usage = 0;
686 scan_data.check_consistency = 1;
687 scan_data.usage_is_inconsistent = 0;
688 err = qh.qh_ops->scan_dquots(&qh, scan_dquots_callback, &scan_data);
690 log_debug("Error scanning dquots");
691 *usage_inconsistent = 1;
695 for (n = dict_first(dict); n; n = dict_next(dict, n)) {
699 if ((dq->dq_flags & DQF_SEEN) == 0) {
700 fprintf(stderr, "[QUOTA WARNING] "
701 "Missing quota entry ID %d\n", dq->dq_id);
702 scan_data.usage_is_inconsistent = 1;
705 *usage_inconsistent = scan_data.usage_is_inconsistent;
708 err = quota_file_close(qctx, &qh);
710 log_debug("Cannot close quotafile: %s", error_message(errno));
711 if (qh.qh_qf.e2_file)
712 ext2fs_file_close(qh.qh_qf.e2_file);
718 int parse_quota_opts(const char *opts, int (*func)(char *))
720 char *buf, *token, *next, *p;
725 buf = malloc(len + 1);
728 "Couldn't allocate memory to parse quota options!\n");
732 for (token = buf; token && *token; token = next) {
733 p = strchr(token, ',');