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, dq->dq_dqb.dqb_curspace,
55 dq->dq_dqb.dqb_bsoftlimit, dq->dq_dqb.dqb_bhardlimit,
56 dq->dq_dqb.dqb_curinodes,
57 dq->dq_dqb.dqb_isoftlimit, dq->dq_dqb.dqb_ihardlimit);
60 static void print_dquot(const char *desc, struct dquot *dq)
66 * Returns 0 if not able to find the quota file, otherwise returns its
69 int quota_file_exists(ext2_filsys fs, int qtype, int fmt)
75 if (qtype >= MAXQUOTAS)
78 quota_get_qf_name(qtype, QFMT_VFS_V1, qf_name);
80 ret = ext2fs_lookup(fs, EXT2_ROOT_INO, qf_name, strlen(qf_name), 0,
89 * Set the value for reserved quota inode number field in superblock.
91 void quota_set_sb_inum(ext2_filsys fs, ext2_ino_t ino, int qtype)
95 inump = (qtype == USRQUOTA) ? &fs->super->s_usr_quota_inum :
96 &fs->super->s_grp_quota_inum;
98 log_debug("setting quota ino in superblock: ino=%u, type=%d", ino,
101 ext2fs_mark_super_dirty(fs);
104 errcode_t quota_remove_inode(ext2_filsys fs, int qtype)
109 retval = ext2fs_read_bitmaps(fs);
111 log_err("Couldn't read bitmaps: %s", error_message(retval));
114 qf_ino = (qtype == USRQUOTA) ? fs->super->s_usr_quota_inum :
115 fs->super->s_grp_quota_inum;
116 quota_set_sb_inum(fs, 0, qtype);
117 /* Truncate the inode only if its a reserved one. */
118 if (qf_ino < EXT2_FIRST_INODE(fs->super))
119 quota_inode_truncate(fs, qf_ino);
121 ext2fs_mark_super_dirty(fs);
122 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
123 retval = ext2fs_write_bitmaps(fs);
125 log_err("Couldn't write bitmaps: %s", error_message(retval));
131 static void write_dquots(dict_t *dict, struct quota_handle *qh)
136 for (n = dict_first(dict); n; n = dict_next(dict, n)) {
139 print_dquot("write", dq);
141 update_grace_times(dq);
142 qh->qh_ops->commit_dquot(dq);
147 errcode_t quota_write_inode(quota_ctx_t qctx, int qtype)
152 struct quota_handle *h = NULL;
153 int fmt = QFMT_VFS_V1;
159 retval = ext2fs_get_mem(sizeof(struct quota_handle), &h);
161 log_err("Unable to allocate quota handle: %s",
162 error_message(retval));
166 retval = ext2fs_read_bitmaps(fs);
168 log_err("Couldn't read bitmaps: %s", error_message(retval));
172 for (i = 0; i < MAXQUOTAS; i++) {
173 if ((qtype != -1) && (i != qtype))
176 dict = qctx->quota_dict[i];
180 retval = quota_file_create(h, fs, i, fmt);
182 log_err("Cannot initialize io on quotafile");
186 write_dquots(dict, h);
187 retval = quota_file_close(h);
189 log_err("Cannot finish IO on new quotafile: %s",
191 if (h->qh_qf.e2_file)
192 ext2fs_file_close(h->qh_qf.e2_file);
193 quota_inode_truncate(fs, h->qh_qf.ino);
197 /* Set quota inode numbers in superblock. */
198 quota_set_sb_inum(fs, h->qh_qf.ino, i);
199 ext2fs_mark_super_dirty(fs);
200 ext2fs_mark_bb_dirty(fs);
201 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
204 retval = ext2fs_write_bitmaps(fs);
206 log_err("Couldn't write bitmaps: %s", error_message(retval));
215 /******************************************************************/
216 /* Helper functions for computing quota in memory. */
217 /******************************************************************/
219 static int dict_uint_cmp(const void *a, const void *b)
223 c = VOIDPTR_TO_UINT(a);
224 d = VOIDPTR_TO_UINT(b);
234 static inline qid_t get_qid(struct ext2_inode *inode, int qtype)
236 if (qtype == USRQUOTA)
237 return inode_uid(*inode);
238 return inode_gid(*inode);
241 static void quota_dnode_free(dnode_t *node,
242 void *context EXT2FS_ATTR((unused)))
244 void *ptr = node ? dnode_get(node) : 0;
246 ext2fs_free_mem(&ptr);
251 * Set up the quota tracking data structures.
253 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 if ((qtype != -1) && (i != qtype))
269 err = ext2fs_get_mem(sizeof(dict_t), &dict);
271 log_err("Failed to allocate dictionary");
272 quota_release_context(&ctx);
275 ctx->quota_dict[i] = dict;
276 dict_init(dict, DICTCOUNT_T_MAX, dict_uint_cmp);
277 dict_set_allocator(dict, NULL, quota_dnode_free, NULL);
285 void quota_release_context(quota_ctx_t *qctx)
295 for (i = 0; i < MAXQUOTAS; i++) {
296 dict = ctx->quota_dict[i];
297 ctx->quota_dict[i] = 0;
299 dict_free_nodes(dict);
307 static struct dquot *get_dq(dict_t *dict, __u32 key)
312 n = dict_lookup(dict, UINT_TO_VOIDPTR(key));
316 if (ext2fs_get_mem(sizeof(struct dquot), &dq)) {
317 log_err("Unable to allocate dquot");
320 memset(dq, 0, sizeof(struct dquot));
321 dict_alloc_insert(dict, UINT_TO_VOIDPTR(key), dq);
329 * Called to update the blocks used by a particular inode
331 void quota_data_add(quota_ctx_t qctx, struct ext2_inode *inode, ext2_ino_t ino,
341 log_debug("ADD_DATA: Inode: %u, UID/GID: %u/%u, space: %ld", ino,
343 inode_gid(*inode), space);
344 for (i = 0; i < MAXQUOTAS; i++) {
345 dict = qctx->quota_dict[i];
347 dq = get_dq(dict, get_qid(inode, i));
349 dq->dq_dqb.dqb_curspace += space;
355 * Called to remove some blocks used by a particular inode
357 void quota_data_sub(quota_ctx_t qctx, struct ext2_inode *inode, ext2_ino_t ino,
367 log_debug("SUB_DATA: Inode: %u, UID/GID: %u/%u, space: %ld", ino,
369 inode_gid(*inode), space);
370 for (i = 0; i < MAXQUOTAS; i++) {
371 dict = qctx->quota_dict[i];
373 dq = get_dq(dict, get_qid(inode, i));
374 dq->dq_dqb.dqb_curspace -= space;
380 * Called to count the files used by an inode's user/group
382 void quota_data_inodes(quota_ctx_t qctx, struct ext2_inode *inode,
383 ext2_ino_t ino, int adjust)
392 log_debug("ADJ_INODE: Inode: %u, UID/GID: %u/%u, adjust: %d", ino,
394 inode_gid(*inode), adjust);
395 for (i = 0; i < MAXQUOTAS; i++) {
396 dict = qctx->quota_dict[i];
398 dq = get_dq(dict, get_qid(inode, i));
399 dq->dq_dqb.dqb_curinodes += adjust;
404 errcode_t quota_compute_usage(quota_ctx_t qctx)
409 struct ext2_inode inode;
411 ext2_inode_scan scan;
417 ret = ext2fs_open_inode_scan(fs, 0, &scan);
419 log_err("while opening inode scan. ret=%ld", ret);
424 ret = ext2fs_get_next_inode(scan, &ino, &inode);
426 log_err("while getting next inode. ret=%ld", ret);
427 ext2fs_close_inode_scan(scan);
432 if (inode.i_links_count &&
433 (ino == EXT2_ROOT_INO ||
434 ino >= EXT2_FIRST_INODE(fs->super))) {
435 space = ext2fs_inode_i_blocks(fs, &inode) << 9;
436 quota_data_add(qctx, &inode, ino, space);
437 quota_data_inodes(qctx, &inode, ino, +1);
441 ext2fs_close_inode_scan(scan);
446 struct scan_dquots_data {
448 int update_limits; /* update limits from disk */
450 int usage_is_inconsistent;
453 static int scan_dquots_callback(struct dquot *dquot, void *cb_data)
455 struct scan_dquots_data *scan_data = cb_data;
456 dict_t *quota_dict = scan_data->quota_dict;
459 dq = get_dq(quota_dict, dquot->dq_id);
460 dq->dq_id = dquot->dq_id;
461 dq->dq_flags |= DQF_SEEN;
463 print_dquot("mem", dq);
464 print_dquot("dsk", dquot);
466 /* Check if there is inconsistancy. */
467 if (dq->dq_dqb.dqb_curspace != dquot->dq_dqb.dqb_curspace ||
468 dq->dq_dqb.dqb_curinodes != dquot->dq_dqb.dqb_curinodes) {
469 scan_data->usage_is_inconsistent = 1;
470 fprintf(stderr, "[QUOTA WARNING] Usage inconsistent for ID %d:"
471 "actual (%llu, %llu) != expected (%llu, %llu)\n",
472 dq->dq_id, (long long)dq->dq_dqb.dqb_curspace,
473 (long long)dq->dq_dqb.dqb_curinodes,
474 (long long)dquot->dq_dqb.dqb_curspace,
475 (long long)dquot->dq_dqb.dqb_curinodes);
478 if (scan_data->update_limits) {
479 dq->dq_dqb.dqb_ihardlimit = dquot->dq_dqb.dqb_ihardlimit;
480 dq->dq_dqb.dqb_isoftlimit = dquot->dq_dqb.dqb_isoftlimit;
481 dq->dq_dqb.dqb_bhardlimit = dquot->dq_dqb.dqb_bhardlimit;
482 dq->dq_dqb.dqb_bsoftlimit = dquot->dq_dqb.dqb_bsoftlimit;
485 if (scan_data->update_usage) {
486 dq->dq_dqb.dqb_curspace = dquot->dq_dqb.dqb_curspace;
487 dq->dq_dqb.dqb_curinodes = dquot->dq_dqb.dqb_curinodes;
494 * Read all dquots from quota file into memory
496 static errcode_t quota_read_all_dquots(struct quota_handle *qh,
497 quota_ctx_t qctx, int update_limits)
499 struct scan_dquots_data scan_data;
501 scan_data.quota_dict = qctx->quota_dict[qh->qh_type];
502 scan_data.update_limits = update_limits;
503 scan_data.update_usage = 0;
505 return qh->qh_ops->scan_dquots(qh, scan_dquots_callback, &scan_data);
509 * Write all memory dquots into quota file
511 #if 0 /* currently unused, but may be useful in the future? */
512 static errcode_t quota_write_all_dquots(struct quota_handle *qh,
517 err = ext2fs_read_bitmaps(qctx->fs);
520 write_dquots(qctx->quota_dict[qh->qh_type], qh);
521 ext2fs_mark_bb_dirty(qctx->fs);
522 qctx->fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
523 ext2fs_write_bitmaps(qctx->fs);
529 * Updates the in-memory quota limits from the given quota inode.
531 errcode_t quota_update_limits(quota_ctx_t qctx, ext2_ino_t qf_ino, int type)
533 struct quota_handle *qh;
539 err = ext2fs_get_mem(sizeof(struct quota_handle), &qh);
541 log_err("Unable to allocate quota handle");
545 err = quota_file_open(qh, qctx->fs, qf_ino, type, -1, 0);
547 log_err("Open quota file failed");
551 quota_read_all_dquots(qh, qctx, 1);
553 err = quota_file_close(qh);
555 log_err("Cannot finish IO on new quotafile: %s",
557 if (qh->qh_qf.e2_file)
558 ext2fs_file_close(qh->qh_qf.e2_file);
561 ext2fs_free_mem(&qh);
566 * Compares the measured quota in qctx->quota_dict with that in the quota inode
567 * on disk and updates the limits in qctx->quota_dict. 'usage_inconsistent' is
568 * set to 1 if the supplied and on-disk quota usage values are not identical.
570 errcode_t quota_compare_and_update(quota_ctx_t qctx, int qtype,
571 int *usage_inconsistent)
573 ext2_filsys fs = qctx->fs;
574 struct quota_handle qh;
575 struct scan_dquots_data scan_data;
578 dict_t *dict = qctx->quota_dict[qtype];
585 qf_ino = qtype == USRQUOTA ? fs->super->s_usr_quota_inum :
586 fs->super->s_grp_quota_inum;
587 err = quota_file_open(&qh, fs, qf_ino, qtype, -1, 0);
589 log_err("Open quota file failed");
593 scan_data.quota_dict = qctx->quota_dict[qtype];
594 scan_data.update_limits = 1;
595 scan_data.update_usage = 0;
596 scan_data.usage_is_inconsistent = 0;
597 err = qh.qh_ops->scan_dquots(&qh, scan_dquots_callback, &scan_data);
599 log_err("Error scanning dquots");
603 for (n = dict_first(dict); n; n = dict_next(dict, n)) {
607 if ((dq->dq_flags & DQF_SEEN) == 0) {
608 fprintf(stderr, "[QUOTA WARNING] "
609 "Missing quota entry ID %d\n", dq->dq_id);
610 scan_data.usage_is_inconsistent = 1;
613 *usage_inconsistent = scan_data.usage_is_inconsistent;
616 err = quota_file_close(&qh);
618 log_err("Cannot close quotafile: %s", error_message(errno));
619 if (qh.qh_qf.e2_file)
620 ext2fs_file_close(qh.qh_qf.e2_file);