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))
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);
49 * Returns 0 if not able to find the quota file, otherwise returns its
52 int quota_file_exists(ext2_filsys fs, int qtype, int fmt)
58 if (qtype >= MAXQUOTAS)
61 quota_get_qf_name(qtype, QFMT_VFS_V1, qf_name);
63 ret = ext2fs_lookup(fs, EXT2_ROOT_INO, qf_name, strlen(qf_name), 0,
72 * Set the value for reserved quota inode number field in superblock.
74 void quota_set_sb_inum(ext2_filsys fs, ext2_ino_t ino, int qtype)
78 inump = (qtype == USRQUOTA) ? &fs->super->s_usr_quota_inum :
79 &fs->super->s_grp_quota_inum;
81 log_debug("setting quota ino in superblock: ino=%u, type=%d", ino,
84 ext2fs_mark_super_dirty(fs);
87 errcode_t quota_remove_inode(ext2_filsys fs, int qtype)
91 ext2fs_read_bitmaps(fs);
92 qf_ino = (qtype == USRQUOTA) ? fs->super->s_usr_quota_inum :
93 fs->super->s_grp_quota_inum;
94 quota_set_sb_inum(fs, 0, qtype);
95 /* Truncate the inode only if its a reserved one. */
96 if (qf_ino < EXT2_FIRST_INODE(fs->super))
97 quota_inode_truncate(fs, qf_ino);
99 ext2fs_mark_super_dirty(fs);
100 ext2fs_write_bitmaps(fs);
104 static void write_dquots(dict_t *dict, struct quota_handle *qh)
109 for (n = dict_first(dict); n; n = dict_next(dict, n)) {
113 update_grace_times(dq);
114 qh->qh_ops->commit_dquot(dq);
119 errcode_t quota_write_inode(quota_ctx_t qctx, int qtype)
124 struct quota_handle *h;
125 int fmt = QFMT_VFS_V1;
131 retval = ext2fs_get_mem(sizeof(struct quota_handle), &h);
133 log_err("Unable to allocate quota handle", "");
137 ext2fs_read_bitmaps(fs);
139 for (i = 0; i < MAXQUOTAS; i++) {
140 if ((qtype != -1) && (i != qtype))
143 dict = qctx->quota_dict[i];
147 retval = quota_file_create(h, fs, i, fmt);
149 log_err("Cannot initialize io on quotafile", "");
153 write_dquots(dict, h);
154 retval = quota_file_close(h);
156 log_err("Cannot finish IO on new quotafile: %s",
158 if (h->qh_qf.e2_file)
159 ext2fs_file_close(h->qh_qf.e2_file);
160 quota_inode_truncate(fs, h->qh_qf.ino);
164 /* Set quota inode numbers in superblock. */
165 quota_set_sb_inum(fs, h->qh_qf.ino, i);
166 ext2fs_mark_super_dirty(fs);
167 ext2fs_mark_bb_dirty(fs);
168 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
171 ext2fs_write_bitmaps(fs);
178 /******************************************************************/
179 /* Helper functions for computing quota in memory. */
180 /******************************************************************/
182 static int dict_uint_cmp(const void *a, const void *b)
186 c = VOIDPTR_TO_UINT(a);
187 d = VOIDPTR_TO_UINT(b);
192 static inline qid_t get_qid(struct ext2_inode *inode, int qtype)
194 if (qtype == USRQUOTA)
195 return inode_uid(*inode);
196 return inode_gid(*inode);
199 static void quota_dnode_free(dnode_t *node,
200 void *context EXT2FS_ATTR((unused)))
202 void *ptr = node ? dnode_get(node) : 0;
204 ext2fs_free_mem(&ptr);
209 * Set up the quota tracking data structures.
211 errcode_t quota_init_context(quota_ctx_t *qctx, ext2_filsys fs, int qtype)
217 err = ext2fs_get_mem(sizeof(struct quota_ctx), &ctx);
219 log_err("Failed to allocate quota context", "");
223 memset(ctx, 0, sizeof(struct quota_ctx));
224 for (i = 0; i < MAXQUOTAS; i++) {
225 if ((qtype != -1) && (i != qtype))
227 err = ext2fs_get_mem(sizeof(dict_t), &dict);
229 log_err("Failed to allocate dictionary", "");
232 ctx->quota_dict[i] = dict;
233 dict_init(dict, DICTCOUNT_T_MAX, dict_uint_cmp);
234 dict_set_allocator(dict, NULL, quota_dnode_free, NULL);
242 void quota_release_context(quota_ctx_t *qctx)
252 for (i = 0; i < MAXQUOTAS; i++) {
253 dict = ctx->quota_dict[i];
254 ctx->quota_dict[i] = 0;
256 dict_free_nodes(dict);
264 static struct dquot *get_dq(dict_t *dict, __u32 key)
269 n = dict_lookup(dict, UINT_TO_VOIDPTR(key));
273 if (ext2fs_get_mem(sizeof(struct dquot), &dq)) {
274 log_err("Unable to allocate dquot", "");
277 memset(dq, 0, sizeof(struct dquot));
278 dict_alloc_insert(dict, UINT_TO_VOIDPTR(key), dq);
286 * Called to update the blocks used by a particular inode
288 void quota_data_add(quota_ctx_t qctx, struct ext2_inode *inode, ext2_ino_t ino,
298 log_debug("ADD_DATA: Inode: %u, UID/GID: %u/%u, space: %ld", ino,
300 inode_gid(*inode), space);
301 for (i = 0; i < MAXQUOTAS; i++) {
302 dict = qctx->quota_dict[i];
304 dq = get_dq(dict, get_qid(inode, i));
306 dq->dq_dqb.dqb_curspace += space;
312 * Called to remove some blocks used by a particular inode
314 void quota_data_sub(quota_ctx_t qctx, struct ext2_inode *inode, ext2_ino_t ino,
324 log_debug("SUB_DATA: Inode: %u, UID/GID: %u/%u, space: %ld", ino,
326 inode_gid(*inode), space);
327 for (i = 0; i < MAXQUOTAS; i++) {
328 dict = qctx->quota_dict[i];
330 dq = get_dq(dict, get_qid(inode, i));
331 dq->dq_dqb.dqb_curspace -= space;
337 * Called to count the files used by an inode's user/group
339 void quota_data_inodes(quota_ctx_t qctx, struct ext2_inode *inode,
340 ext2_ino_t ino, int adjust)
349 log_debug("ADJ_INODE: Inode: %u, UID/GID: %u/%u, adjust: %d", ino,
351 inode_gid(*inode), adjust);
352 for (i = 0; i < MAXQUOTAS; i++) {
353 dict = qctx->quota_dict[i];
355 dq = get_dq(dict, get_qid(inode, i));
356 dq->dq_dqb.dqb_curinodes += adjust;
361 errcode_t quota_compute_usage(quota_ctx_t qctx)
366 struct ext2_inode inode;
368 ext2_inode_scan scan;
374 ret = ext2fs_open_inode_scan(fs, 0, &scan);
376 log_err("while opening inode scan. ret=%ld", ret);
381 ret = ext2fs_get_next_inode(scan, &ino, &inode);
383 log_err("while getting next inode. ret=%ld", ret);
384 ext2fs_close_inode_scan(scan);
389 if (inode.i_links_count &&
390 (ino == EXT2_ROOT_INO ||
391 ino >= EXT2_FIRST_INODE(fs->super))) {
392 space = ext2fs_inode_i_blocks(fs, &inode) << 9;
393 quota_data_add(qctx, &inode, ino, space);
394 quota_data_inodes(qctx, &inode, ino, +1);
398 ext2fs_close_inode_scan(scan);
403 struct scan_dquots_data {
405 int update_limits; /* update limits from disk */
407 int usage_is_inconsistent;
410 static int scan_dquots_callback(struct dquot *dquot, void *cb_data)
412 struct scan_dquots_data *scan_data = cb_data;
413 dict_t *quota_dict = scan_data->quota_dict;
416 dq = get_dq(quota_dict, dquot->dq_id);
417 dq->dq_id = dquot->dq_id;
418 dq->dq_dqb.u.v2_mdqb.dqb_off = dquot->dq_dqb.u.v2_mdqb.dqb_off;
420 /* Check if there is inconsistancy. */
421 if (dq->dq_dqb.dqb_curspace != dquot->dq_dqb.dqb_curspace ||
422 dq->dq_dqb.dqb_curinodes != dquot->dq_dqb.dqb_curinodes) {
423 scan_data->usage_is_inconsistent = 1;
424 fprintf(stderr, "[QUOTA WARNING] Usage inconsistent for ID %d:"
425 "actual (%llu, %llu) != expected (%llu, %llu)\n",
426 dq->dq_id, dq->dq_dqb.dqb_curspace,
427 dq->dq_dqb.dqb_curinodes, dquot->dq_dqb.dqb_curspace,
428 dquot->dq_dqb.dqb_curinodes);
431 if (scan_data->update_limits) {
432 dq->dq_dqb.dqb_ihardlimit = dquot->dq_dqb.dqb_ihardlimit;
433 dq->dq_dqb.dqb_isoftlimit = dquot->dq_dqb.dqb_isoftlimit;
434 dq->dq_dqb.dqb_bhardlimit = dquot->dq_dqb.dqb_bhardlimit;
435 dq->dq_dqb.dqb_bsoftlimit = dquot->dq_dqb.dqb_bsoftlimit;
438 if (scan_data->update_usage) {
439 dq->dq_dqb.dqb_curspace = dquot->dq_dqb.dqb_curspace;
440 dq->dq_dqb.dqb_curinodes = dquot->dq_dqb.dqb_curinodes;
447 * Read all dquots from quota file into memory
449 static errcode_t quota_read_all_dquots(struct quota_handle *qh,
450 quota_ctx_t qctx, int update_limits)
452 struct scan_dquots_data scan_data;
454 scan_data.quota_dict = qctx->quota_dict[qh->qh_type];
455 scan_data.update_limits = update_limits;
456 scan_data.update_usage = 0;
458 return qh->qh_ops->scan_dquots(qh, scan_dquots_callback, &scan_data);
462 * Write all memory dquots into quota file
464 static errcode_t quota_write_all_dquots(struct quota_handle *qh,
469 err = ext2fs_read_bitmaps(qctx->fs);
472 write_dquots(qctx->quota_dict[qh->qh_type], qh);
473 ext2fs_mark_bb_dirty(qctx->fs);
474 qctx->fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
475 ext2fs_write_bitmaps(qctx->fs);
480 * Updates the in-memory quota limits from the given quota inode.
482 errcode_t quota_update_limits(quota_ctx_t qctx, ext2_ino_t qf_ino, int type)
484 struct quota_handle *qh;
490 err = ext2fs_get_mem(sizeof(struct quota_handle), &qh);
492 log_err("Unable to allocate quota handle", "");
496 err = quota_file_open(qh, qctx->fs, qf_ino, type, -1, 0);
498 log_err("Open quota file failed", "");
502 quota_read_all_dquots(qh, qctx, 1);
504 err = quota_file_close(qh);
506 log_err("Cannot finish IO on new quotafile: %s",
508 if (qh->qh_qf.e2_file)
509 ext2fs_file_close(qh->qh_qf.e2_file);
512 ext2fs_free_mem(&qh);
517 * Compares the measured quota in qctx->quota_dict with that in the quota inode
518 * on disk and updates the limits in qctx->quota_dict. 'usage_inconsistent' is
519 * set to 1 if the supplied and on-disk quota usage values are not identical.
521 errcode_t quota_compare_and_update(quota_ctx_t qctx, int qtype,
522 int *usage_inconsistent)
524 ext2_filsys fs = qctx->fs;
525 struct quota_handle qh;
526 struct scan_dquots_data scan_data;
530 if (!qctx->quota_dict[qtype])
533 qf_ino = qtype == USRQUOTA ? fs->super->s_usr_quota_inum :
534 fs->super->s_grp_quota_inum;
535 err = quota_file_open(&qh, fs, qf_ino, qtype, -1, 0);
537 log_err("Open quota file failed", "");
541 scan_data.quota_dict = qctx->quota_dict[qtype];
542 scan_data.update_limits = 1;
543 scan_data.update_usage = 0;
544 scan_data.usage_is_inconsistent = 0;
545 err = qh.qh_ops->scan_dquots(&qh, scan_dquots_callback, &scan_data);
547 log_err("Error scanning dquots", "");
550 *usage_inconsistent = scan_data.usage_is_inconsistent;