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,
103 ext2fs_mark_super_dirty(fs);
106 errcode_t quota_remove_inode(ext2_filsys fs, enum quota_type qtype)
111 retval = ext2fs_read_bitmaps(fs);
113 log_debug("Couldn't read bitmaps: %s", error_message(retval));
117 qf_ino = *quota_sb_inump(fs->super, qtype);
120 retval = quota_inode_truncate(fs, qf_ino);
123 if (qf_ino >= EXT2_FIRST_INODE(fs->super)) {
124 struct ext2_inode inode;
126 retval = ext2fs_read_inode(fs, qf_ino, &inode);
128 memset(&inode, 0, sizeof(struct ext2_inode));
129 ext2fs_write_inode(fs, qf_ino, &inode);
131 ext2fs_inode_alloc_stats2(fs, qf_ino, -1, 0);
132 ext2fs_mark_ib_dirty(fs);
135 quota_set_sb_inum(fs, 0, qtype);
137 ext2fs_mark_super_dirty(fs);
138 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
139 retval = ext2fs_write_bitmaps(fs);
141 log_debug("Couldn't write bitmaps: %s", error_message(retval));
147 static void write_dquots(dict_t *dict, struct quota_handle *qh)
152 for (n = dict_first(dict); n; n = dict_next(dict, n)) {
155 print_dquot("write", dq);
157 update_grace_times(dq);
158 qh->qh_ops->commit_dquot(dq);
163 errcode_t quota_write_inode(quota_ctx_t qctx, unsigned int qtype_bits)
166 enum quota_type qtype;
169 struct quota_handle *h = NULL;
170 int fmt = QFMT_VFS_V1;
176 retval = ext2fs_get_mem(sizeof(struct quota_handle), &h);
178 log_debug("Unable to allocate quota handle: %s",
179 error_message(retval));
183 retval = ext2fs_read_bitmaps(fs);
185 log_debug("Couldn't read bitmaps: %s", error_message(retval));
189 for (qtype = 0; qtype < MAXQUOTAS; qtype++) {
190 if (((1 << qtype) & qtype_bits) == 0)
193 dict = qctx->quota_dict[qtype];
197 retval = quota_file_create(h, fs, qtype, fmt);
199 log_debug("Cannot initialize io on quotafile: %s",
200 error_message(retval));
204 write_dquots(dict, h);
205 retval = quota_file_close(qctx, h);
207 log_debug("Cannot finish IO on new quotafile: %s",
209 if (h->qh_qf.e2_file)
210 ext2fs_file_close(h->qh_qf.e2_file);
211 (void) quota_inode_truncate(fs, h->qh_qf.ino);
215 /* Set quota inode numbers in superblock. */
216 quota_set_sb_inum(fs, h->qh_qf.ino, qtype);
217 ext2fs_mark_super_dirty(fs);
218 ext2fs_mark_bb_dirty(fs);
219 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
222 retval = ext2fs_write_bitmaps(fs);
224 log_debug("Couldn't write bitmaps: %s", error_message(retval));
233 /******************************************************************/
234 /* Helper functions for computing quota in memory. */
235 /******************************************************************/
237 static int dict_uint_cmp(const void *cmp_ctx EXT2FS_ATTR((unused)),
238 const void *a, const void *b)
242 c = VOIDPTR_TO_UINT(a);
243 d = VOIDPTR_TO_UINT(b);
253 static inline int project_quota_valid(quota_ctx_t qctx)
255 return (EXT2_INODE_SIZE(qctx->fs->super) > EXT2_GOOD_OLD_INODE_SIZE);
258 static inline qid_t get_qid(struct ext2_inode_large *inode, enum quota_type qtype)
260 unsigned int inode_size;
264 return inode_uid(*inode);
266 return inode_gid(*inode);
268 inode_size = EXT2_GOOD_OLD_INODE_SIZE +
269 inode->i_extra_isize;
270 if (inode_includes(inode_size, i_projid))
271 return inode_projid(*inode);
280 static void quota_dnode_free(dnode_t *node,
281 void *context EXT2FS_ATTR((unused)))
283 void *ptr = node ? dnode_get(node) : 0;
285 ext2fs_free_mem(&ptr);
290 * Set up the quota tracking data structures.
292 errcode_t quota_init_context(quota_ctx_t *qctx, ext2_filsys fs,
293 unsigned int qtype_bits)
298 enum quota_type qtype;
300 err = ext2fs_get_mem(sizeof(struct quota_ctx), &ctx);
302 log_debug("Failed to allocate quota context");
306 memset(ctx, 0, sizeof(struct quota_ctx));
307 for (qtype = 0; qtype < MAXQUOTAS; qtype++) {
308 ctx->quota_file[qtype] = NULL;
310 if (((1 << qtype) & qtype_bits) == 0)
313 if (*quota_sb_inump(fs->super, qtype) == 0)
316 err = ext2fs_get_mem(sizeof(dict_t), &dict);
318 log_debug("Failed to allocate dictionary");
319 quota_release_context(&ctx);
322 ctx->quota_dict[qtype] = dict;
323 dict_init(dict, DICTCOUNT_T_MAX, dict_uint_cmp);
324 dict_set_allocator(dict, NULL, quota_dnode_free, NULL);
332 void quota_release_context(quota_ctx_t *qctx)
336 enum quota_type qtype;
343 for (qtype = 0; qtype < MAXQUOTAS; qtype++) {
344 dict = ctx->quota_dict[qtype];
345 ctx->quota_dict[qtype] = 0;
347 dict_free_nodes(dict);
350 if (ctx->quota_file[qtype]) {
351 err = quota_file_close(ctx, ctx->quota_file[qtype]);
353 log_err("Cannot close quotafile: %s",
355 ext2fs_free_mem(&ctx->quota_file[qtype]);
363 static struct dquot *get_dq(dict_t *dict, __u32 key)
368 n = dict_lookup(dict, UINT_TO_VOIDPTR(key));
372 if (ext2fs_get_mem(sizeof(struct dquot), &dq)) {
373 log_err("Unable to allocate dquot");
376 memset(dq, 0, sizeof(struct dquot));
377 dict_alloc_insert(dict, UINT_TO_VOIDPTR(key), dq);
385 * Called to update the blocks used by a particular inode
387 void quota_data_add(quota_ctx_t qctx, struct ext2_inode_large *inode,
388 ext2_ino_t ino EXT2FS_ATTR((unused)),
393 enum quota_type qtype;
398 log_debug("ADD_DATA: Inode: %u, UID/GID: %u/%u, space: %ld", ino,
400 inode_gid(*inode), space);
401 for (qtype = 0; qtype < MAXQUOTAS; qtype++) {
402 if (qtype == PRJQUOTA && !project_quota_valid(qctx))
404 dict = qctx->quota_dict[qtype];
406 dq = get_dq(dict, get_qid(inode, qtype));
408 dq->dq_dqb.dqb_curspace += space;
414 * Called to remove some blocks used by a particular inode
416 void quota_data_sub(quota_ctx_t qctx, struct ext2_inode_large *inode,
417 ext2_ino_t ino EXT2FS_ATTR((unused)),
422 enum quota_type qtype;
427 log_debug("SUB_DATA: Inode: %u, UID/GID: %u/%u, space: %ld", ino,
429 inode_gid(*inode), space);
430 for (qtype = 0; qtype < MAXQUOTAS; qtype++) {
431 if (qtype == PRJQUOTA && !project_quota_valid(qctx))
433 dict = qctx->quota_dict[qtype];
435 dq = get_dq(dict, get_qid(inode, qtype));
436 dq->dq_dqb.dqb_curspace -= space;
442 * Called to count the files used by an inode's user/group
444 void quota_data_inodes(quota_ctx_t qctx, struct ext2_inode_large *inode,
445 ext2_ino_t ino EXT2FS_ATTR((unused)), int adjust)
449 enum quota_type qtype;
454 log_debug("ADJ_INODE: Inode: %u, UID/GID: %u/%u, adjust: %d", ino,
456 inode_gid(*inode), adjust);
457 for (qtype = 0; qtype < MAXQUOTAS; qtype++) {
458 if (qtype == PRJQUOTA && !project_quota_valid(qctx))
460 dict = qctx->quota_dict[qtype];
462 dq = get_dq(dict, get_qid(inode, qtype));
463 dq->dq_dqb.dqb_curinodes += adjust;
468 errcode_t quota_compute_usage(quota_ctx_t qctx)
473 struct ext2_inode_large *inode;
476 ext2_inode_scan scan;
482 ret = ext2fs_open_inode_scan(fs, 0, &scan);
484 log_err("while opening inode scan. ret=%ld", ret);
487 inode_size = fs->super->s_inode_size;
488 inode = malloc(inode_size);
490 ext2fs_close_inode_scan(scan);
494 ret = ext2fs_get_next_inode_full(scan, &ino,
495 EXT2_INODE(inode), inode_size);
497 log_err("while getting next inode. ret=%ld", ret);
498 ext2fs_close_inode_scan(scan);
504 if (inode->i_links_count &&
505 (ino == EXT2_ROOT_INO ||
506 ino >= EXT2_FIRST_INODE(fs->super))) {
507 space = ext2fs_get_stat_i_blocks(fs,
508 EXT2_INODE(inode)) << 9;
509 quota_data_add(qctx, inode, ino, space);
510 quota_data_inodes(qctx, inode, ino, +1);
514 ext2fs_close_inode_scan(scan);
519 struct scan_dquots_data {
521 int update_limits; /* update limits from disk */
523 int check_consistency;
524 int usage_is_inconsistent;
527 static int scan_dquots_callback(struct dquot *dquot, void *cb_data)
529 struct scan_dquots_data *scan_data = cb_data;
530 dict_t *quota_dict = scan_data->quota_dict;
533 dq = get_dq(quota_dict, dquot->dq_id);
534 dq->dq_id = dquot->dq_id;
535 dq->dq_flags |= DQF_SEEN;
537 print_dquot("mem", dq);
538 print_dquot("dsk", dquot);
540 /* Check if there is inconsistency */
541 if (scan_data->check_consistency &&
542 (dq->dq_dqb.dqb_curspace != dquot->dq_dqb.dqb_curspace ||
543 dq->dq_dqb.dqb_curinodes != dquot->dq_dqb.dqb_curinodes)) {
544 scan_data->usage_is_inconsistent = 1;
545 fprintf(stderr, "[QUOTA WARNING] Usage inconsistent for ID %u:"
546 "actual (%lld, %lld) != expected (%lld, %lld)\n",
547 dq->dq_id, (long long) dq->dq_dqb.dqb_curspace,
548 (long long) dq->dq_dqb.dqb_curinodes,
549 (long long) dquot->dq_dqb.dqb_curspace,
550 (long long) dquot->dq_dqb.dqb_curinodes);
553 if (scan_data->update_limits) {
554 dq->dq_dqb.dqb_ihardlimit = dquot->dq_dqb.dqb_ihardlimit;
555 dq->dq_dqb.dqb_isoftlimit = dquot->dq_dqb.dqb_isoftlimit;
556 dq->dq_dqb.dqb_bhardlimit = dquot->dq_dqb.dqb_bhardlimit;
557 dq->dq_dqb.dqb_bsoftlimit = dquot->dq_dqb.dqb_bsoftlimit;
560 if (scan_data->update_usage) {
561 dq->dq_dqb.dqb_curspace = dquot->dq_dqb.dqb_curspace;
562 dq->dq_dqb.dqb_curinodes = dquot->dq_dqb.dqb_curinodes;
569 * Read all dquots from quota file into memory
571 static errcode_t quota_read_all_dquots(struct quota_handle *qh,
573 int update_limits EXT2FS_ATTR((unused)))
575 struct scan_dquots_data scan_data;
577 scan_data.quota_dict = qctx->quota_dict[qh->qh_type];
578 scan_data.check_consistency = 0;
579 scan_data.update_limits = 0;
580 scan_data.update_usage = 1;
582 return qh->qh_ops->scan_dquots(qh, scan_dquots_callback, &scan_data);
586 * Write all memory dquots into quota file
588 #if 0 /* currently unused, but may be useful in the future? */
589 static errcode_t quota_write_all_dquots(struct quota_handle *qh,
594 err = ext2fs_read_bitmaps(qctx->fs);
597 write_dquots(qctx->quota_dict[qh->qh_type], qh);
598 ext2fs_mark_bb_dirty(qctx->fs);
599 qctx->fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
600 ext2fs_write_bitmaps(qctx->fs);
606 * Updates the in-memory quota limits from the given quota inode.
608 errcode_t quota_update_limits(quota_ctx_t qctx, ext2_ino_t qf_ino,
609 enum quota_type qtype)
611 struct quota_handle *qh;
617 err = ext2fs_get_mem(sizeof(struct quota_handle), &qh);
619 log_debug("Unable to allocate quota handle");
623 err = quota_file_open(qctx, qh, qf_ino, qtype, -1, 0);
625 log_debug("Open quota file failed");
629 quota_read_all_dquots(qh, qctx, 1);
631 err = quota_file_close(qctx, qh);
633 log_debug("Cannot finish IO on new quotafile: %s",
635 if (qh->qh_qf.e2_file)
636 ext2fs_file_close(qh->qh_qf.e2_file);
639 ext2fs_free_mem(&qh);
644 * Compares the measured quota in qctx->quota_dict with that in the quota inode
645 * on disk and updates the limits in qctx->quota_dict. 'usage_inconsistent' is
646 * set to 1 if the supplied and on-disk quota usage values are not identical.
648 errcode_t quota_compare_and_update(quota_ctx_t qctx, enum quota_type qtype,
649 int *usage_inconsistent)
651 struct quota_handle qh;
652 struct scan_dquots_data scan_data;
655 dict_t *dict = qctx->quota_dict[qtype];
661 err = quota_file_open(qctx, &qh, 0, qtype, -1, 0);
663 log_debug("Open quota file failed");
667 scan_data.quota_dict = qctx->quota_dict[qtype];
668 scan_data.update_limits = 1;
669 scan_data.update_usage = 0;
670 scan_data.check_consistency = 1;
671 scan_data.usage_is_inconsistent = 0;
672 err = qh.qh_ops->scan_dquots(&qh, scan_dquots_callback, &scan_data);
674 log_debug("Error scanning dquots");
675 *usage_inconsistent = 1;
679 for (n = dict_first(dict); n; n = dict_next(dict, n)) {
683 if ((dq->dq_flags & DQF_SEEN) == 0) {
684 fprintf(stderr, "[QUOTA WARNING] "
685 "Missing quota entry ID %d\n", dq->dq_id);
686 scan_data.usage_is_inconsistent = 1;
689 *usage_inconsistent = scan_data.usage_is_inconsistent;
692 err = quota_file_close(qctx, &qh);
694 log_debug("Cannot close quotafile: %s", error_message(errno));
695 if (qh.qh_qf.e2_file)
696 ext2fs_file_close(qh.qh_qf.e2_file);
702 int parse_quota_opts(const char *opts, int (*func)(char *))
704 char *buf, *token, *next, *p;
709 buf = malloc(len + 1);
712 "Couldn't allocate memory to parse quota options!\n");
716 for (token = buf; token && *token; token = next) {
717 p = strchr(token, ',');