1/*
2 * quota.c --- code for handling ext4 quota inodes
3 *
4 */
5
6#include "config.h"
7#ifdef HAVE_SYS_MOUNT_H
8#include <sys/param.h>
9#include <sys/mount.h>
10#define MNT_FL (MS_MGC_VAL | MS_RDONLY)
11#endif
12#ifdef HAVE_SYS_STAT_H
13#include <sys/stat.h>
14#endif
15
16#include "e2fsck.h"
17#include "problem.h"
18
19static void move_quota_inode(ext2_filsys fs, ext2_ino_t from_ino,
20			     ext2_ino_t to_ino, enum quota_type qtype)
21{
22	struct ext2_inode	inode;
23	errcode_t		retval;
24	char			qf_name[QUOTA_NAME_LEN];
25
26	/* We need the inode bitmap to be loaded */
27	if (ext2fs_read_bitmaps(fs))
28		return;
29
30	retval = ext2fs_read_inode(fs, from_ino, &inode);
31	if (retval) {
32		com_err("ext2fs_read_inode", retval, "%s",
33			_("in move_quota_inode"));
34		return;
35	}
36
37	inode.i_links_count = 1;
38	inode.i_mode = LINUX_S_IFREG | 0600;
39	inode.i_flags = EXT2_IMMUTABLE_FL;
40	if (ext2fs_has_feature_extents(fs->super))
41		inode.i_flags |= EXT4_EXTENTS_FL;
42
43	retval = ext2fs_write_new_inode(fs, to_ino, &inode);
44	if (retval) {
45		com_err("ext2fs_write_new_inode", retval, "%s",
46			_("in move_quota_inode"));
47		return;
48	}
49
50	/* unlink the old inode */
51	quota_get_qf_name(qtype, QFMT_VFS_V1, qf_name);
52	ext2fs_unlink(fs, EXT2_ROOT_INO, qf_name, from_ino, 0);
53	ext2fs_inode_alloc_stats(fs, from_ino, -1);
54	/* Clear out the original inode in the inode-table block. */
55	memset(&inode, 0, sizeof(struct ext2_inode));
56	ext2fs_write_inode(fs, from_ino, &inode);
57}
58
59void e2fsck_hide_quota(e2fsck_t ctx)
60{
61	struct ext2_super_block *sb = ctx->fs->super;
62	struct problem_context	pctx;
63	ext2_filsys		fs = ctx->fs;
64	enum quota_type qtype;
65	ext2_ino_t quota_ino;
66
67	clear_problem_context(&pctx);
68
69	if ((ctx->options & E2F_OPT_READONLY) ||
70	    !ext2fs_has_feature_quota(sb))
71		return;
72
73	for (qtype = 0; qtype < MAXQUOTAS; qtype++) {
74		pctx.ino = *quota_sb_inump(sb, qtype);
75		quota_ino = quota_type2inum(qtype, fs->super);
76		if (pctx.ino && (pctx.ino != quota_ino) &&
77		    fix_problem(ctx, PR_0_HIDE_QUOTA, &pctx)) {
78			move_quota_inode(fs, pctx.ino, quota_ino, qtype);
79			*quota_sb_inump(sb, qtype) = quota_ino;
80		}
81	}
82
83	return;
84}
85