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;
421 ext2_inode_scan scan;
427 ret = ext2fs_open_inode_scan(fs, 0, &scan);
429 log_err("while opening inode scan. ret=%ld", ret);
434 ret = ext2fs_get_next_inode(scan, &ino, &inode);
436 log_err("while getting next inode. ret=%ld", ret);
437 ext2fs_close_inode_scan(scan);
442 if (inode.i_links_count &&
443 (ino == EXT2_ROOT_INO ||
444 ino >= EXT2_FIRST_INODE(fs->super))) {
445 space = ext2fs_inode_i_blocks(fs, &inode) << 9;
446 quota_data_add(qctx, &inode, ino, space);
447 quota_data_inodes(qctx, &inode, ino, +1);
451 ext2fs_close_inode_scan(scan);
456 struct scan_dquots_data {
458 int update_limits; /* update limits from disk */
460 int usage_is_inconsistent;
463 static int scan_dquots_callback(struct dquot *dquot, void *cb_data)
465 struct scan_dquots_data *scan_data = cb_data;
466 dict_t *quota_dict = scan_data->quota_dict;
469 dq = get_dq(quota_dict, dquot->dq_id);
470 dq->dq_id = dquot->dq_id;
471 dq->dq_flags |= DQF_SEEN;
473 print_dquot("mem", dq);
474 print_dquot("dsk", dquot);
476 /* Check if there is inconsistancy. */
477 if (dq->dq_dqb.dqb_curspace != dquot->dq_dqb.dqb_curspace ||
478 dq->dq_dqb.dqb_curinodes != dquot->dq_dqb.dqb_curinodes) {
479 scan_data->usage_is_inconsistent = 1;
480 fprintf(stderr, "[QUOTA WARNING] Usage inconsistent for ID %d:"
481 "actual (%llu, %llu) != expected (%llu, %llu)\n",
482 dq->dq_id, (long long)dq->dq_dqb.dqb_curspace,
483 (long long)dq->dq_dqb.dqb_curinodes,
484 (long long)dquot->dq_dqb.dqb_curspace,
485 (long long)dquot->dq_dqb.dqb_curinodes);
488 if (scan_data->update_limits) {
489 dq->dq_dqb.dqb_ihardlimit = dquot->dq_dqb.dqb_ihardlimit;
490 dq->dq_dqb.dqb_isoftlimit = dquot->dq_dqb.dqb_isoftlimit;
491 dq->dq_dqb.dqb_bhardlimit = dquot->dq_dqb.dqb_bhardlimit;
492 dq->dq_dqb.dqb_bsoftlimit = dquot->dq_dqb.dqb_bsoftlimit;
495 if (scan_data->update_usage) {
496 dq->dq_dqb.dqb_curspace = dquot->dq_dqb.dqb_curspace;
497 dq->dq_dqb.dqb_curinodes = dquot->dq_dqb.dqb_curinodes;
504 * Read all dquots from quota file into memory
506 static errcode_t quota_read_all_dquots(struct quota_handle *qh,
507 quota_ctx_t qctx, int update_limits)
509 struct scan_dquots_data scan_data;
511 scan_data.quota_dict = qctx->quota_dict[qh->qh_type];
512 scan_data.update_limits = update_limits;
513 scan_data.update_usage = 0;
515 return qh->qh_ops->scan_dquots(qh, scan_dquots_callback, &scan_data);
519 * Write all memory dquots into quota file
521 #if 0 /* currently unused, but may be useful in the future? */
522 static errcode_t quota_write_all_dquots(struct quota_handle *qh,
527 err = ext2fs_read_bitmaps(qctx->fs);
530 write_dquots(qctx->quota_dict[qh->qh_type], qh);
531 ext2fs_mark_bb_dirty(qctx->fs);
532 qctx->fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
533 ext2fs_write_bitmaps(qctx->fs);
539 * Updates the in-memory quota limits from the given quota inode.
541 errcode_t quota_update_limits(quota_ctx_t qctx, ext2_ino_t qf_ino, int type)
543 struct quota_handle *qh;
549 err = ext2fs_get_mem(sizeof(struct quota_handle), &qh);
551 log_err("Unable to allocate quota handle");
555 err = quota_file_open(qctx, qh, qf_ino, type, -1, 0);
557 log_err("Open quota file failed");
561 quota_read_all_dquots(qh, qctx, 1);
563 err = quota_file_close(qctx, qh);
565 log_err("Cannot finish IO on new quotafile: %s",
567 if (qh->qh_qf.e2_file)
568 ext2fs_file_close(qh->qh_qf.e2_file);
571 ext2fs_free_mem(&qh);
576 * Compares the measured quota in qctx->quota_dict with that in the quota inode
577 * on disk and updates the limits in qctx->quota_dict. 'usage_inconsistent' is
578 * set to 1 if the supplied and on-disk quota usage values are not identical.
580 errcode_t quota_compare_and_update(quota_ctx_t qctx, int qtype,
581 int *usage_inconsistent)
583 struct quota_handle qh;
584 struct scan_dquots_data scan_data;
587 dict_t *dict = qctx->quota_dict[qtype];
593 err = quota_file_open(qctx, &qh, 0, qtype, -1, 0);
595 log_err("Open quota file failed");
599 scan_data.quota_dict = qctx->quota_dict[qtype];
600 scan_data.update_limits = 1;
601 scan_data.update_usage = 0;
602 scan_data.usage_is_inconsistent = 0;
603 err = qh.qh_ops->scan_dquots(&qh, scan_dquots_callback, &scan_data);
605 log_err("Error scanning dquots");
609 for (n = dict_first(dict); n; n = dict_next(dict, n)) {
613 if ((dq->dq_flags & DQF_SEEN) == 0) {
614 fprintf(stderr, "[QUOTA WARNING] "
615 "Missing quota entry ID %d\n", dq->dq_id);
616 scan_data.usage_is_inconsistent = 1;
619 *usage_inconsistent = scan_data.usage_is_inconsistent;
622 err = quota_file_close(qctx, &qh);
624 log_err("Cannot close quotafile: %s", error_message(errno));
625 if (qh.qh_qf.e2_file)
626 ext2fs_file_close(qh.qh_qf.e2_file);