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"
23 /* Needed for architectures where sizeof(int) != sizeof(void *) */
24 #define UINT_TO_VOIDPTR(val) ((void *)(intptr_t)(val))
25 #define VOIDPTR_TO_UINT(ptr) ((unsigned int)(intptr_t)(ptr))
28 static void print_inode(struct ext2_inode *inode)
33 fprintf(stderr, " i_mode = %d\n", inode->i_mode);
34 fprintf(stderr, " i_uid = %d\n", inode->i_uid);
35 fprintf(stderr, " i_size = %d\n", inode->i_size);
36 fprintf(stderr, " i_atime = %d\n", inode->i_atime);
37 fprintf(stderr, " i_ctime = %d\n", inode->i_ctime);
38 fprintf(stderr, " i_mtime = %d\n", inode->i_mtime);
39 fprintf(stderr, " i_dtime = %d\n", inode->i_dtime);
40 fprintf(stderr, " i_gid = %d\n", inode->i_gid);
41 fprintf(stderr, " i_links_count = %d\n", inode->i_links_count);
42 fprintf(stderr, " i_blocks = %d\n", inode->i_blocks);
43 fprintf(stderr, " i_flags = %d\n", inode->i_flags);
48 static void print_dquot(const char *desc, struct dquot *dq)
51 fprintf(stderr, "%s: ", desc);
52 fprintf(stderr, "%u %lld:%lld:%lld %lld:%lld:%lld\n",
53 dq->dq_id, dq->dq_dqb.dqb_curspace,
54 dq->dq_dqb.dqb_bsoftlimit, dq->dq_dqb.dqb_bhardlimit,
55 dq->dq_dqb.dqb_curinodes,
56 dq->dq_dqb.dqb_isoftlimit, dq->dq_dqb.dqb_ihardlimit);
59 static void print_dquot(const char *desc, struct dquot *dq)
65 * Returns 0 if not able to find the quota file, otherwise returns its
68 int quota_file_exists(ext2_filsys fs, int qtype, int fmt)
74 if (qtype >= MAXQUOTAS)
77 quota_get_qf_name(qtype, QFMT_VFS_V1, qf_name);
79 ret = ext2fs_lookup(fs, EXT2_ROOT_INO, qf_name, strlen(qf_name), 0,
88 * Set the value for reserved quota inode number field in superblock.
90 void quota_set_sb_inum(ext2_filsys fs, ext2_ino_t ino, int qtype)
94 inump = (qtype == USRQUOTA) ? &fs->super->s_usr_quota_inum :
95 &fs->super->s_grp_quota_inum;
97 log_debug("setting quota ino in superblock: ino=%u, type=%d", ino,
100 ext2fs_mark_super_dirty(fs);
103 errcode_t quota_remove_inode(ext2_filsys fs, int qtype)
108 retval = ext2fs_read_bitmaps(fs);
110 log_err("Couldn't read bitmaps: %s", error_message(retval));
113 qf_ino = (qtype == USRQUOTA) ? fs->super->s_usr_quota_inum :
114 fs->super->s_grp_quota_inum;
115 quota_set_sb_inum(fs, 0, qtype);
116 /* Truncate the inode only if its a reserved one. */
117 if (qf_ino < EXT2_FIRST_INODE(fs->super))
118 quota_inode_truncate(fs, qf_ino);
120 ext2fs_mark_super_dirty(fs);
121 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
122 retval = ext2fs_write_bitmaps(fs);
124 log_err("Couldn't write bitmaps: %s", error_message(retval));
130 static void write_dquots(dict_t *dict, struct quota_handle *qh)
135 for (n = dict_first(dict); n; n = dict_next(dict, n)) {
138 print_dquot("write", dq);
140 update_grace_times(dq);
141 qh->qh_ops->commit_dquot(dq);
146 errcode_t quota_write_inode(quota_ctx_t qctx, int qtype)
151 struct quota_handle *h = NULL;
152 int fmt = QFMT_VFS_V1;
158 retval = ext2fs_get_mem(sizeof(struct quota_handle), &h);
160 log_err("Unable to allocate quota handle: %s",
161 error_message(retval));
165 retval = ext2fs_read_bitmaps(fs);
167 log_err("Couldn't read bitmaps: %s", error_message(retval));
171 for (i = 0; i < MAXQUOTAS; i++) {
172 if ((qtype != -1) && (i != qtype))
175 dict = qctx->quota_dict[i];
179 retval = quota_file_create(h, fs, i, fmt);
181 log_err("Cannot initialize io on quotafile");
185 write_dquots(dict, h);
186 retval = quota_file_close(qctx, h);
188 log_err("Cannot finish IO on new quotafile: %s",
190 if (h->qh_qf.e2_file)
191 ext2fs_file_close(h->qh_qf.e2_file);
192 quota_inode_truncate(fs, h->qh_qf.ino);
196 /* Set quota inode numbers in superblock. */
197 quota_set_sb_inum(fs, h->qh_qf.ino, i);
198 ext2fs_mark_super_dirty(fs);
199 ext2fs_mark_bb_dirty(fs);
200 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
203 retval = ext2fs_write_bitmaps(fs);
205 log_err("Couldn't write bitmaps: %s", error_message(retval));
214 /******************************************************************/
215 /* Helper functions for computing quota in memory. */
216 /******************************************************************/
218 static int dict_uint_cmp(const void *a, const void *b)
222 c = VOIDPTR_TO_UINT(a);
223 d = VOIDPTR_TO_UINT(b);
233 static inline qid_t get_qid(struct ext2_inode *inode, int qtype)
235 if (qtype == USRQUOTA)
236 return inode_uid(*inode);
237 return inode_gid(*inode);
240 static void quota_dnode_free(dnode_t *node,
241 void *context EXT2FS_ATTR((unused)))
243 void *ptr = node ? dnode_get(node) : 0;
245 ext2fs_free_mem(&ptr);
250 * Set up the quota tracking data structures.
252 errcode_t quota_init_context(quota_ctx_t *qctx, ext2_filsys fs, int qtype)
259 err = ext2fs_get_mem(sizeof(struct quota_ctx), &ctx);
261 log_err("Failed to allocate quota context");
265 memset(ctx, 0, sizeof(struct quota_ctx));
266 for (i = 0; i < MAXQUOTAS; i++) {
267 ctx->quota_file[i] = NULL;
268 if ((qtype != -1) && (i != qtype))
270 err = ext2fs_get_mem(sizeof(dict_t), &dict);
272 log_err("Failed to allocate dictionary");
273 quota_release_context(&ctx);
276 ctx->quota_dict[i] = dict;
277 dict_init(dict, DICTCOUNT_T_MAX, dict_uint_cmp);
278 dict_set_allocator(dict, NULL, quota_dnode_free, NULL);
286 void quota_release_context(quota_ctx_t *qctx)
297 for (i = 0; i < MAXQUOTAS; i++) {
298 dict = ctx->quota_dict[i];
299 ctx->quota_dict[i] = 0;
301 dict_free_nodes(dict);
304 if (ctx->quota_file[i]) {
305 err = quota_file_close(ctx, ctx->quota_file[i]);
307 log_err("Cannot close quotafile: %s",
309 ext2fs_free_mem(&ctx->quota_file[i]);
317 static struct dquot *get_dq(dict_t *dict, __u32 key)
322 n = dict_lookup(dict, UINT_TO_VOIDPTR(key));
326 if (ext2fs_get_mem(sizeof(struct dquot), &dq)) {
327 log_err("Unable to allocate dquot");
330 memset(dq, 0, sizeof(struct dquot));
331 dict_alloc_insert(dict, UINT_TO_VOIDPTR(key), dq);
339 * Called to update the blocks used by a particular inode
341 void quota_data_add(quota_ctx_t qctx, struct ext2_inode *inode, ext2_ino_t ino,
351 log_debug("ADD_DATA: Inode: %u, UID/GID: %u/%u, space: %ld", ino,
353 inode_gid(*inode), space);
354 for (i = 0; i < MAXQUOTAS; i++) {
355 dict = qctx->quota_dict[i];
357 dq = get_dq(dict, get_qid(inode, i));
359 dq->dq_dqb.dqb_curspace += space;
365 * Called to remove some blocks used by a particular inode
367 void quota_data_sub(quota_ctx_t qctx, struct ext2_inode *inode, ext2_ino_t ino,
377 log_debug("SUB_DATA: Inode: %u, UID/GID: %u/%u, space: %ld", ino,
379 inode_gid(*inode), space);
380 for (i = 0; i < MAXQUOTAS; i++) {
381 dict = qctx->quota_dict[i];
383 dq = get_dq(dict, get_qid(inode, i));
384 dq->dq_dqb.dqb_curspace -= space;
390 * Called to count the files used by an inode's user/group
392 void quota_data_inodes(quota_ctx_t qctx, struct ext2_inode *inode,
393 ext2_ino_t ino, int adjust)
402 log_debug("ADJ_INODE: Inode: %u, UID/GID: %u/%u, adjust: %d", ino,
404 inode_gid(*inode), adjust);
405 for (i = 0; i < MAXQUOTAS; i++) {
406 dict = qctx->quota_dict[i];
408 dq = get_dq(dict, get_qid(inode, i));
409 dq->dq_dqb.dqb_curinodes += adjust;
414 errcode_t quota_compute_usage(quota_ctx_t qctx)
419 struct ext2_inode* inode;
422 ext2_inode_scan scan;
427 inode_size = EXT2_INODE_SIZE(qctx->fs->super);
428 ret = ext2fs_get_mem(inode_size, &inode);
433 ret = ext2fs_open_inode_scan(fs, 0, &scan);
435 ext2fs_free_mem(&inode);
436 log_err("while opening inode scan. ret=%ld", ret);
441 ret = ext2fs_get_next_inode_full(scan, &ino, inode,
444 log_err("while getting next inode. ret=%ld", ret);
449 if (inode->i_links_count &&
450 (ino == EXT2_ROOT_INO ||
451 ino >= EXT2_FIRST_INODE(fs->super))) {
452 space = ext2fs_inode_i_blocks(fs, inode) << 9;
453 quota_data_add(qctx, inode, ino, space);
454 quota_data_inodes(qctx, inode, ino, +1);
459 ext2fs_close_inode_scan(scan);
460 ext2fs_free_mem(&inode);
465 struct scan_dquots_data {
467 int update_limits; /* update limits from disk */
469 int usage_is_inconsistent;
472 static int scan_dquots_callback(struct dquot *dquot, void *cb_data)
474 struct scan_dquots_data *scan_data = cb_data;
475 dict_t *quota_dict = scan_data->quota_dict;
478 dq = get_dq(quota_dict, dquot->dq_id);
479 dq->dq_id = dquot->dq_id;
480 dq->dq_flags |= DQF_SEEN;
482 print_dquot("mem", dq);
483 print_dquot("dsk", dquot);
485 /* Check if there is inconsistancy. */
486 if (dq->dq_dqb.dqb_curspace != dquot->dq_dqb.dqb_curspace ||
487 dq->dq_dqb.dqb_curinodes != dquot->dq_dqb.dqb_curinodes) {
488 scan_data->usage_is_inconsistent = 1;
489 fprintf(stderr, "[QUOTA WARNING] Usage inconsistent for ID %d:"
490 "actual (%llu, %llu) != expected (%llu, %llu)\n",
491 dq->dq_id, (long long)dq->dq_dqb.dqb_curspace,
492 (long long)dq->dq_dqb.dqb_curinodes,
493 (long long)dquot->dq_dqb.dqb_curspace,
494 (long long)dquot->dq_dqb.dqb_curinodes);
497 if (scan_data->update_limits) {
498 dq->dq_dqb.dqb_ihardlimit = dquot->dq_dqb.dqb_ihardlimit;
499 dq->dq_dqb.dqb_isoftlimit = dquot->dq_dqb.dqb_isoftlimit;
500 dq->dq_dqb.dqb_bhardlimit = dquot->dq_dqb.dqb_bhardlimit;
501 dq->dq_dqb.dqb_bsoftlimit = dquot->dq_dqb.dqb_bsoftlimit;
504 if (scan_data->update_usage) {
505 dq->dq_dqb.dqb_curspace = dquot->dq_dqb.dqb_curspace;
506 dq->dq_dqb.dqb_curinodes = dquot->dq_dqb.dqb_curinodes;
513 * Read all dquots from quota file into memory
515 static errcode_t quota_read_all_dquots(struct quota_handle *qh,
516 quota_ctx_t qctx, int update_limits)
518 struct scan_dquots_data scan_data;
520 scan_data.quota_dict = qctx->quota_dict[qh->qh_type];
521 scan_data.update_limits = update_limits;
522 scan_data.update_usage = 0;
524 return qh->qh_ops->scan_dquots(qh, scan_dquots_callback, &scan_data);
528 * Write all memory dquots into quota file
530 #if 0 /* currently unused, but may be useful in the future? */
531 static errcode_t quota_write_all_dquots(struct quota_handle *qh,
536 err = ext2fs_read_bitmaps(qctx->fs);
539 write_dquots(qctx->quota_dict[qh->qh_type], qh);
540 ext2fs_mark_bb_dirty(qctx->fs);
541 qctx->fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
542 ext2fs_write_bitmaps(qctx->fs);
548 * Updates the in-memory quota limits from the given quota inode.
550 errcode_t quota_update_limits(quota_ctx_t qctx, ext2_ino_t qf_ino, int type)
552 struct quota_handle *qh;
558 err = ext2fs_get_mem(sizeof(struct quota_handle), &qh);
560 log_err("Unable to allocate quota handle");
564 err = quota_file_open(qctx, qh, qf_ino, type, -1, 0);
566 log_err("Open quota file failed");
570 quota_read_all_dquots(qh, qctx, 1);
572 err = quota_file_close(qctx, qh);
574 log_err("Cannot finish IO on new quotafile: %s",
576 if (qh->qh_qf.e2_file)
577 ext2fs_file_close(qh->qh_qf.e2_file);
580 ext2fs_free_mem(&qh);
585 * Compares the measured quota in qctx->quota_dict with that in the quota inode
586 * on disk and updates the limits in qctx->quota_dict. 'usage_inconsistent' is
587 * set to 1 if the supplied and on-disk quota usage values are not identical.
589 errcode_t quota_compare_and_update(quota_ctx_t qctx, int qtype,
590 int *usage_inconsistent)
592 struct quota_handle qh;
593 struct scan_dquots_data scan_data;
596 dict_t *dict = qctx->quota_dict[qtype];
602 err = quota_file_open(qctx, &qh, 0, qtype, -1, 0);
604 log_err("Open quota file failed");
608 scan_data.quota_dict = qctx->quota_dict[qtype];
609 scan_data.update_limits = 1;
610 scan_data.update_usage = 0;
611 scan_data.usage_is_inconsistent = 0;
612 err = qh.qh_ops->scan_dquots(&qh, scan_dquots_callback, &scan_data);
614 log_err("Error scanning dquots");
618 for (n = dict_first(dict); n; n = dict_next(dict, n)) {
622 if ((dq->dq_flags & DQF_SEEN) == 0) {
623 fprintf(stderr, "[QUOTA WARNING] "
624 "Missing quota entry ID %d\n", dq->dq_id);
625 scan_data.usage_is_inconsistent = 1;
628 *usage_inconsistent = scan_data.usage_is_inconsistent;
631 err = quota_file_close(qctx, &qh);
633 log_err("Cannot close quotafile: %s", error_message(errno));
634 if (qh.qh_qf.e2_file)
635 ext2fs_file_close(qh.qh_qf.e2_file);