Whamcloud - gitweb
e2fsck: add support for checking the built-in quota files
[tools/e2fsprogs.git] / e2fsck / quota.c
1 /*
2  * quota.c --- code for handling ext4 quota inodes
3  *
4  */
5
6 #ifdef HAVE_SYS_MOUNT_H
7 #include <sys/param.h>
8 #include <sys/mount.h>
9 #define MNT_FL (MS_MGC_VAL | MS_RDONLY)
10 #endif
11 #ifdef HAVE_SYS_STAT_H
12 #include <sys/stat.h>
13 #endif
14
15 #include "e2fsck.h"
16 #include "problem.h"
17 #include "quota/mkquota.h"
18
19 static void move_quota_inode(ext2_filsys fs, ext2_ino_t from_ino,
20                              ext2_ino_t to_ino, int qtype)
21 {
22         struct ext2_super_block *sb = fs->super;
23         ext2_ino_t              ino;
24         struct ext2_inode       inode;
25         errcode_t               retval;
26         char                    qf_name[255];
27
28         if (ext2fs_read_inode(fs, from_ino, &inode))
29                 return;
30
31         inode.i_links_count = 1;
32         inode.i_mode = LINUX_S_IFREG | 0600;
33         inode.i_flags = EXT2_IMMUTABLE_FL;
34         if (fs->super->s_feature_incompat &
35                         EXT3_FEATURE_INCOMPAT_EXTENTS)
36                 inode.i_flags |= EXT4_EXTENTS_FL;
37
38         ext2fs_write_new_inode(fs, to_ino, &inode);
39         /* unlink the old inode */
40         get_qf_name(qtype, QFMT_VFS_V1, qf_name);
41         ext2fs_unlink(fs, EXT2_ROOT_INO, qf_name, from_ino, 0);
42         ext2fs_inode_alloc_stats(fs, from_ino, -1);
43 }
44
45 void e2fsck_hide_quota(e2fsck_t ctx)
46 {
47         struct ext2_super_block *sb = ctx->fs->super;
48         struct problem_context  pctx;
49         ext2_filsys             fs = ctx->fs;
50
51         clear_problem_context(&pctx);
52
53         if ((ctx->options & E2F_OPT_READONLY) ||
54             !(sb->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_QUOTA))
55                 return;
56
57         /* We need the inode bitmap to be loaded */
58         if (ext2fs_read_bitmaps(fs))
59                 return;
60
61         if (!sb->s_usr_quota_inum && !sb->s_grp_quota_inum)
62                 /* nothing to do */
63                 return;
64
65         if (sb->s_usr_quota_inum == EXT4_USR_QUOTA_INO &&
66             sb->s_grp_quota_inum == EXT4_GRP_QUOTA_INO)
67                 /* nothing to do */
68                 return;
69
70         if (!fix_problem(ctx, PR_0_HIDE_QUOTA, &pctx))
71                 return;
72
73         if (sb->s_usr_quota_inum &&
74             sb->s_usr_quota_inum != EXT4_USR_QUOTA_INO) {
75                 move_quota_inode(fs, sb->s_usr_quota_inum, EXT4_USR_QUOTA_INO,
76                                  USRQUOTA);
77                 sb->s_usr_quota_inum = EXT4_USR_QUOTA_INO;
78         }
79
80         if (sb->s_grp_quota_inum &&
81             sb->s_grp_quota_inum != EXT4_GRP_QUOTA_INO) {
82                 move_quota_inode(fs, sb->s_grp_quota_inum, EXT4_GRP_QUOTA_INO,
83                                  GRPQUOTA);
84                 sb->s_grp_quota_inum = EXT4_GRP_QUOTA_INO;
85         }
86
87         return;
88 }