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"
20 #include "quotaio_v2.h"
21 #include "quotaio_tree.h"
25 /* Needed for architectures where sizeof(int) != sizeof(void *) */
26 #define UINT_TO_VOIDPTR(val) ((void *)(intptr_t)(val))
27 #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 int quota_is_on(ext2_filsys fs, int type)
52 qid_t id = (type == USRQUOTA) ? getuid() : getgid();
55 if (!quotactl(QCMD(Q_V2_GETQUOTA, type), fs->device_name, id, tmp))
62 * Returns 0 if not able to find the quota file, otherwise returns its
65 int quota_file_exists(ext2_filsys fs, int qtype, int fmt)
71 if (qtype >= MAXQUOTAS)
74 quota_get_qf_name(qtype, fmt, qf_name);
76 ret = ext2fs_lookup(fs, EXT2_ROOT_INO, qf_name, strlen(qf_name), 0,
85 * Set the value for reserved quota inode number field in superblock.
87 void quota_set_sb_inum(ext2_filsys fs, ext2_ino_t ino, int qtype)
91 inump = (qtype == USRQUOTA) ? &fs->super->s_usr_quota_inum :
92 &fs->super->s_grp_quota_inum;
94 log_debug("setting quota ino in superblock: ino=%u, type=%d", ino,
97 ext2fs_mark_super_dirty(fs);
100 errcode_t quota_remove_inode(ext2_filsys fs, int qtype)
104 ext2fs_read_bitmaps(fs);
105 qf_ino = (qtype == USRQUOTA) ? fs->super->s_usr_quota_inum :
106 fs->super->s_grp_quota_inum;
107 quota_set_sb_inum(fs, 0, qtype);
108 /* Truncate the inode only if its a reserved one. */
109 if (qf_ino < EXT2_FIRST_INODE(fs->super))
110 quota_inode_truncate(fs, qf_ino);
112 ext2fs_mark_super_dirty(fs);
113 ext2fs_write_bitmaps(fs);
117 static void write_dquots(dict_t *dict, struct quota_handle *qh)
122 for (n = dict_first(dict); n; n = dict_next(dict, n)) {
126 update_grace_times(dq);
127 qh->qh_ops->commit_dquot(dq);
132 errcode_t quota_write_inode(quota_ctx_t qctx, int qtype)
137 struct quota_handle *h;
138 int fmt = QFMT_VFS_V1;
144 retval = ext2fs_get_mem(sizeof(struct quota_handle), &h);
146 log_err("Unable to allocate quota handle", "");
150 ext2fs_read_bitmaps(fs);
152 for (i = 0; i < MAXQUOTAS; i++) {
153 if ((qtype != -1) && (i != qtype))
156 dict = qctx->quota_dict[i];
160 retval = quota_file_create(h, fs, i, fmt);
162 log_err("Cannot initialize io on quotafile", "");
166 write_dquots(dict, h);
167 retval = quota_file_close(h);
169 log_err("Cannot finish IO on new quotafile: %s",
171 if (h->qh_qf.e2_file)
172 ext2fs_file_close(h->qh_qf.e2_file);
173 quota_inode_truncate(fs, h->qh_qf.ino);
177 /* Set quota inode numbers in superblock. */
178 quota_set_sb_inum(fs, h->qh_qf.ino, i);
179 ext2fs_mark_super_dirty(fs);
180 ext2fs_mark_bb_dirty(fs);
181 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
184 ext2fs_write_bitmaps(fs);
191 /******************************************************************/
192 /* Helper functions for computing quota in memory. */
193 /******************************************************************/
195 static int dict_uint_cmp(const void *a, const void *b)
199 c = VOIDPTR_TO_UINT(a);
200 d = VOIDPTR_TO_UINT(b);
205 static inline qid_t get_qid(struct ext2_inode *inode, int qtype)
207 if (qtype == USRQUOTA)
208 return inode_uid(*inode);
209 return inode_gid(*inode);
212 static void quota_dnode_free(dnode_t *node,
213 void *context EXT2FS_ATTR((unused)))
215 void *ptr = node ? dnode_get(node) : 0;
217 ext2fs_free_mem(&ptr);
222 * Set up the quota tracking data structures.
224 errcode_t quota_init_context(quota_ctx_t *qctx, ext2_filsys fs, int qtype)
230 err = ext2fs_get_mem(sizeof(struct quota_ctx), &ctx);
232 log_err("Failed to allocate quota context", "");
236 memset(ctx, 0, sizeof(struct quota_ctx));
237 for (i = 0; i < MAXQUOTAS; i++) {
238 if ((qtype != -1) && (i != qtype))
240 err = ext2fs_get_mem(sizeof(dict_t), &dict);
242 log_err("Failed to allocate dictionary", "");
245 ctx->quota_dict[i] = dict;
246 dict_init(dict, DICTCOUNT_T_MAX, dict_uint_cmp);
247 dict_set_allocator(dict, NULL, quota_dnode_free, NULL);
255 void quota_release_context(quota_ctx_t *qctx)
265 for (i = 0; i < MAXQUOTAS; i++) {
266 dict = ctx->quota_dict[i];
267 ctx->quota_dict[i] = 0;
269 dict_free_nodes(dict);
277 static struct dquot *get_dq(dict_t *dict, __u32 key)
282 n = dict_lookup(dict, UINT_TO_VOIDPTR(key));
286 if (ext2fs_get_mem(sizeof(struct dquot), &dq)) {
287 log_err("Unable to allocate dquot", "");
290 memset(dq, 0, sizeof(struct dquot));
291 dict_alloc_insert(dict, UINT_TO_VOIDPTR(key), dq);
299 * Called to update the blocks used by a particular inode
301 void quota_data_add(quota_ctx_t qctx, struct ext2_inode *inode, ext2_ino_t ino,
311 log_debug("ADD_DATA: Inode: %u, UID/GID: %u/%u, space: %ld", ino,
313 inode_gid(*inode), space);
314 for (i = 0; i < MAXQUOTAS; i++) {
315 dict = qctx->quota_dict[i];
317 dq = get_dq(dict, get_qid(inode, i));
319 dq->dq_dqb.dqb_curspace += space;
325 * Called to remove some blocks used by a particular inode
327 void quota_data_sub(quota_ctx_t qctx, struct ext2_inode *inode, ext2_ino_t ino,
337 log_debug("SUB_DATA: Inode: %u, UID/GID: %u/%u, space: %ld", ino,
339 inode_gid(*inode), space);
340 for (i = 0; i < MAXQUOTAS; i++) {
341 dict = qctx->quota_dict[i];
343 dq = get_dq(dict, get_qid(inode, i));
344 dq->dq_dqb.dqb_curspace -= space;
350 * Called to count the files used by an inode's user/group
352 void quota_data_inodes(quota_ctx_t qctx, struct ext2_inode *inode,
353 ext2_ino_t ino, int adjust)
362 log_debug("ADJ_INODE: Inode: %u, UID/GID: %u/%u, adjust: %d", ino,
364 inode_gid(*inode), adjust);
365 for (i = 0; i < MAXQUOTAS; i++) {
366 dict = qctx->quota_dict[i];
368 dq = get_dq(dict, get_qid(inode, i));
369 dq->dq_dqb.dqb_curinodes += adjust;
374 errcode_t quota_compute_usage(quota_ctx_t qctx)
379 struct ext2_inode inode;
381 ext2_inode_scan scan;
387 ret = ext2fs_open_inode_scan(fs, 0, &scan);
389 log_err("while opening inode scan. ret=%ld", ret);
394 ret = ext2fs_get_next_inode(scan, &ino, &inode);
396 log_err("while getting next inode. ret=%ld", ret);
397 ext2fs_close_inode_scan(scan);
402 if (inode.i_links_count) {
403 space = ext2fs_inode_i_blocks(fs, &inode) << 9;
404 quota_data_add(qctx, &inode, ino, space);
405 quota_data_inodes(qctx, &inode, ino, +1);
409 ext2fs_close_inode_scan(scan);
414 struct scan_dquots_data {
416 int limit_only; /* read limit only */
419 static int scan_dquots_callback(struct dquot *dquot, void *cb_data)
421 struct scan_dquots_data *scan_data =
422 (struct scan_dquots_data *)cb_data;
423 quota_ctx_t qctx = scan_data->qctx;
426 dq = get_dq(qctx->quota_dict[dquot->dq_h->qh_type], dquot->dq_id);
428 dq->dq_id = dquot->dq_id;
429 if (scan_data->limit_only) {
430 dq->dq_dqb.u.v2_mdqb.dqb_off = dquot->dq_dqb.u.v2_mdqb.dqb_off;
431 dq->dq_dqb.dqb_ihardlimit = dquot->dq_dqb.dqb_ihardlimit;
432 dq->dq_dqb.dqb_isoftlimit = dquot->dq_dqb.dqb_isoftlimit;
433 dq->dq_dqb.dqb_bhardlimit = dquot->dq_dqb.dqb_bhardlimit;
434 dq->dq_dqb.dqb_bsoftlimit = dquot->dq_dqb.dqb_bsoftlimit;
436 dq->dq_dqb = dquot->dq_dqb;
442 * Read all dquots from quota file into memory
444 static errcode_t quota_read_all_dquots(struct quota_handle *qh,
445 quota_ctx_t qctx, int limit_only)
447 struct scan_dquots_data scan_data;
449 scan_data.qctx = qctx;
450 scan_data.limit_only = limit_only;
452 return qh->qh_ops->scan_dquots(qh, scan_dquots_callback, &scan_data);
456 * Write all memory dquots into quota file
458 static errcode_t quota_write_all_dquots(struct quota_handle *qh,
463 err = ext2fs_read_bitmaps(qctx->fs);
466 write_dquots(qctx->quota_dict[qh->qh_type], qh);
467 ext2fs_mark_bb_dirty(qctx->fs);
468 qctx->fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
469 ext2fs_write_bitmaps(qctx->fs);
474 * Update usage of in quota file, limits keep unchaged
476 errcode_t quota_update_inode(quota_ctx_t qctx, ext2_ino_t qf_ino, int type)
478 struct quota_handle *qh;
484 err = ext2fs_get_mem(sizeof(struct quota_handle), &qh);
486 log_err("Unable to allocate quota handle", "");
490 err = quota_file_open(qh, qctx->fs, qf_ino, type, -1, EXT2_FILE_WRITE);
492 log_err("Open quota file failed", "");
496 quota_read_all_dquots(qh, qctx, 1);
497 quota_write_all_dquots(qh, qctx);
499 err = quota_file_close(qh);
501 log_err("Cannot finish IO on new quotafile: %s",
503 if (qh->qh_qf.e2_file)
504 ext2fs_file_close(qh->qh_qf.e2_file);
507 ext2fs_free_mem(&qh);