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);
51 * Returns 0 if not able to find the quota file, otherwise returns its
54 int quota_file_exists(ext2_filsys fs, int qtype, int fmt)
60 if (qtype >= MAXQUOTAS)
63 quota_get_qf_name(qtype, QFMT_VFS_V1, qf_name);
65 ret = ext2fs_lookup(fs, EXT2_ROOT_INO, qf_name, strlen(qf_name), 0,
74 * Set the value for reserved quota inode number field in superblock.
76 void quota_set_sb_inum(ext2_filsys fs, ext2_ino_t ino, int qtype)
80 inump = (qtype == USRQUOTA) ? &fs->super->s_usr_quota_inum :
81 &fs->super->s_grp_quota_inum;
83 log_debug("setting quota ino in superblock: ino=%u, type=%d", ino,
86 ext2fs_mark_super_dirty(fs);
89 errcode_t quota_remove_inode(ext2_filsys fs, int qtype)
93 ext2fs_read_bitmaps(fs);
94 qf_ino = (qtype == USRQUOTA) ? fs->super->s_usr_quota_inum :
95 fs->super->s_grp_quota_inum;
96 quota_set_sb_inum(fs, 0, qtype);
97 /* Truncate the inode only if its a reserved one. */
98 if (qf_ino < EXT2_FIRST_INODE(fs->super))
99 quota_inode_truncate(fs, qf_ino);
101 ext2fs_mark_super_dirty(fs);
102 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
103 ext2fs_write_bitmaps(fs);
107 static void write_dquots(dict_t *dict, struct quota_handle *qh)
112 for (n = dict_first(dict); n; n = dict_next(dict, n)) {
116 update_grace_times(dq);
117 qh->qh_ops->commit_dquot(dq);
122 errcode_t quota_write_inode(quota_ctx_t qctx, int qtype)
127 struct quota_handle *h = NULL;
128 int fmt = QFMT_VFS_V1;
134 retval = ext2fs_get_mem(sizeof(struct quota_handle), &h);
136 log_err("Unable to allocate quota handle");
140 ext2fs_read_bitmaps(fs);
142 for (i = 0; i < MAXQUOTAS; i++) {
143 if ((qtype != -1) && (i != qtype))
146 dict = qctx->quota_dict[i];
150 retval = quota_file_create(h, fs, i, fmt);
152 log_err("Cannot initialize io on quotafile");
156 write_dquots(dict, h);
157 retval = quota_file_close(h);
159 log_err("Cannot finish IO on new quotafile: %s",
161 if (h->qh_qf.e2_file)
162 ext2fs_file_close(h->qh_qf.e2_file);
163 quota_inode_truncate(fs, h->qh_qf.ino);
167 /* Set quota inode numbers in superblock. */
168 quota_set_sb_inum(fs, h->qh_qf.ino, i);
169 ext2fs_mark_super_dirty(fs);
170 ext2fs_mark_bb_dirty(fs);
171 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
174 ext2fs_write_bitmaps(fs);
181 /******************************************************************/
182 /* Helper functions for computing quota in memory. */
183 /******************************************************************/
185 static int dict_uint_cmp(const void *a, const void *b)
189 c = VOIDPTR_TO_UINT(a);
190 d = VOIDPTR_TO_UINT(b);
195 static inline qid_t get_qid(struct ext2_inode *inode, int qtype)
197 if (qtype == USRQUOTA)
198 return inode_uid(*inode);
199 return inode_gid(*inode);
202 static void quota_dnode_free(dnode_t *node,
203 void *context EXT2FS_ATTR((unused)))
205 void *ptr = node ? dnode_get(node) : 0;
207 ext2fs_free_mem(&ptr);
212 * Set up the quota tracking data structures.
214 errcode_t quota_init_context(quota_ctx_t *qctx, ext2_filsys fs, int qtype)
220 err = ext2fs_get_mem(sizeof(struct quota_ctx), &ctx);
222 log_err("Failed to allocate quota context");
226 memset(ctx, 0, sizeof(struct quota_ctx));
227 for (i = 0; i < MAXQUOTAS; i++) {
228 if ((qtype != -1) && (i != qtype))
230 err = ext2fs_get_mem(sizeof(dict_t), &dict);
232 log_err("Failed to allocate dictionary");
235 ctx->quota_dict[i] = dict;
236 dict_init(dict, DICTCOUNT_T_MAX, dict_uint_cmp);
237 dict_set_allocator(dict, NULL, quota_dnode_free, NULL);
245 void quota_release_context(quota_ctx_t *qctx)
255 for (i = 0; i < MAXQUOTAS; i++) {
256 dict = ctx->quota_dict[i];
257 ctx->quota_dict[i] = 0;
259 dict_free_nodes(dict);
267 static struct dquot *get_dq(dict_t *dict, __u32 key)
272 n = dict_lookup(dict, UINT_TO_VOIDPTR(key));
276 if (ext2fs_get_mem(sizeof(struct dquot), &dq)) {
277 log_err("Unable to allocate dquot");
280 memset(dq, 0, sizeof(struct dquot));
281 dict_alloc_insert(dict, UINT_TO_VOIDPTR(key), dq);
289 * Called to update the blocks used by a particular inode
291 void quota_data_add(quota_ctx_t qctx, struct ext2_inode *inode, ext2_ino_t ino,
301 log_debug("ADD_DATA: Inode: %u, UID/GID: %u/%u, space: %ld", ino,
303 inode_gid(*inode), space);
304 for (i = 0; i < MAXQUOTAS; i++) {
305 dict = qctx->quota_dict[i];
307 dq = get_dq(dict, get_qid(inode, i));
309 dq->dq_dqb.dqb_curspace += space;
315 * Called to remove some blocks used by a particular inode
317 void quota_data_sub(quota_ctx_t qctx, struct ext2_inode *inode, ext2_ino_t ino,
327 log_debug("SUB_DATA: Inode: %u, UID/GID: %u/%u, space: %ld", ino,
329 inode_gid(*inode), space);
330 for (i = 0; i < MAXQUOTAS; i++) {
331 dict = qctx->quota_dict[i];
333 dq = get_dq(dict, get_qid(inode, i));
334 dq->dq_dqb.dqb_curspace -= space;
340 * Called to count the files used by an inode's user/group
342 void quota_data_inodes(quota_ctx_t qctx, struct ext2_inode *inode,
343 ext2_ino_t ino, int adjust)
352 log_debug("ADJ_INODE: Inode: %u, UID/GID: %u/%u, adjust: %d", ino,
354 inode_gid(*inode), adjust);
355 for (i = 0; i < MAXQUOTAS; i++) {
356 dict = qctx->quota_dict[i];
358 dq = get_dq(dict, get_qid(inode, i));
359 dq->dq_dqb.dqb_curinodes += adjust;
364 errcode_t quota_compute_usage(quota_ctx_t qctx)
369 struct ext2_inode inode;
371 ext2_inode_scan scan;
377 ret = ext2fs_open_inode_scan(fs, 0, &scan);
379 log_err("while opening inode scan. ret=%ld", ret);
384 ret = ext2fs_get_next_inode(scan, &ino, &inode);
386 log_err("while getting next inode. ret=%ld", ret);
387 ext2fs_close_inode_scan(scan);
392 if (inode.i_links_count &&
393 (ino == EXT2_ROOT_INO ||
394 ino >= EXT2_FIRST_INODE(fs->super))) {
395 space = ext2fs_inode_i_blocks(fs, &inode) << 9;
396 quota_data_add(qctx, &inode, ino, space);
397 quota_data_inodes(qctx, &inode, ino, +1);
401 ext2fs_close_inode_scan(scan);
406 struct scan_dquots_data {
408 int update_limits; /* update limits from disk */
410 int usage_is_inconsistent;
413 static int scan_dquots_callback(struct dquot *dquot, void *cb_data)
415 struct scan_dquots_data *scan_data = cb_data;
416 dict_t *quota_dict = scan_data->quota_dict;
419 dq = get_dq(quota_dict, dquot->dq_id);
420 dq->dq_id = dquot->dq_id;
421 dq->dq_dqb.u.v2_mdqb.dqb_off = dquot->dq_dqb.u.v2_mdqb.dqb_off;
423 /* Check if there is inconsistancy. */
424 if (dq->dq_dqb.dqb_curspace != dquot->dq_dqb.dqb_curspace ||
425 dq->dq_dqb.dqb_curinodes != dquot->dq_dqb.dqb_curinodes) {
426 scan_data->usage_is_inconsistent = 1;
427 fprintf(stderr, "[QUOTA WARNING] Usage inconsistent for ID %d:"
428 "actual (%llu, %llu) != expected (%llu, %llu)\n",
429 dq->dq_id, (long long)dq->dq_dqb.dqb_curspace,
430 (long long)dq->dq_dqb.dqb_curinodes,
431 (long long)dquot->dq_dqb.dqb_curspace,
432 (long long)dquot->dq_dqb.dqb_curinodes);
435 if (scan_data->update_limits) {
436 dq->dq_dqb.dqb_ihardlimit = dquot->dq_dqb.dqb_ihardlimit;
437 dq->dq_dqb.dqb_isoftlimit = dquot->dq_dqb.dqb_isoftlimit;
438 dq->dq_dqb.dqb_bhardlimit = dquot->dq_dqb.dqb_bhardlimit;
439 dq->dq_dqb.dqb_bsoftlimit = dquot->dq_dqb.dqb_bsoftlimit;
442 if (scan_data->update_usage) {
443 dq->dq_dqb.dqb_curspace = dquot->dq_dqb.dqb_curspace;
444 dq->dq_dqb.dqb_curinodes = dquot->dq_dqb.dqb_curinodes;
451 * Read all dquots from quota file into memory
453 static errcode_t quota_read_all_dquots(struct quota_handle *qh,
454 quota_ctx_t qctx, int update_limits)
456 struct scan_dquots_data scan_data;
458 scan_data.quota_dict = qctx->quota_dict[qh->qh_type];
459 scan_data.update_limits = update_limits;
460 scan_data.update_usage = 0;
462 return qh->qh_ops->scan_dquots(qh, scan_dquots_callback, &scan_data);
466 * Write all memory dquots into quota file
468 #if 0 /* currently unused, but may be useful in the future? */
469 static errcode_t quota_write_all_dquots(struct quota_handle *qh,
474 err = ext2fs_read_bitmaps(qctx->fs);
477 write_dquots(qctx->quota_dict[qh->qh_type], qh);
478 ext2fs_mark_bb_dirty(qctx->fs);
479 qctx->fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
480 ext2fs_write_bitmaps(qctx->fs);
486 * Updates the in-memory quota limits from the given quota inode.
488 errcode_t quota_update_limits(quota_ctx_t qctx, ext2_ino_t qf_ino, int type)
490 struct quota_handle *qh;
496 err = ext2fs_get_mem(sizeof(struct quota_handle), &qh);
498 log_err("Unable to allocate quota handle");
502 err = quota_file_open(qh, qctx->fs, qf_ino, type, -1, 0);
504 log_err("Open quota file failed");
508 quota_read_all_dquots(qh, qctx, 1);
510 err = quota_file_close(qh);
512 log_err("Cannot finish IO on new quotafile: %s",
514 if (qh->qh_qf.e2_file)
515 ext2fs_file_close(qh->qh_qf.e2_file);
518 ext2fs_free_mem(&qh);
523 * Compares the measured quota in qctx->quota_dict with that in the quota inode
524 * on disk and updates the limits in qctx->quota_dict. 'usage_inconsistent' is
525 * set to 1 if the supplied and on-disk quota usage values are not identical.
527 errcode_t quota_compare_and_update(quota_ctx_t qctx, int qtype,
528 int *usage_inconsistent)
530 ext2_filsys fs = qctx->fs;
531 struct quota_handle qh;
532 struct scan_dquots_data scan_data;
536 if (!qctx->quota_dict[qtype])
539 qf_ino = qtype == USRQUOTA ? fs->super->s_usr_quota_inum :
540 fs->super->s_grp_quota_inum;
541 err = quota_file_open(&qh, fs, qf_ino, qtype, -1, 0);
543 log_err("Open quota file failed");
547 scan_data.quota_dict = qctx->quota_dict[qtype];
548 scan_data.update_limits = 1;
549 scan_data.update_usage = 0;
550 scan_data.usage_is_inconsistent = 0;
551 err = qh.qh_ops->scan_dquots(&qh, scan_dquots_callback, &scan_data);
553 log_err("Error scanning dquots");
556 *usage_inconsistent = scan_data.usage_is_inconsistent;