Skip to content

Commit db99855

Browse files
author
Christian Brauner
committed
fs: introduce two inode i_{u,g}id initialization helpers
Give filesystem two little helpers that do the right thing when initializing the i_uid and i_gid fields on idmapped and non-idmapped mounts. Filesystems shouldn't have to be concerned with too many details. Link: https://lore.kernel.org/r/[email protected] Inspired-by: Vivek Goyal <[email protected]> Cc: Christoph Hellwig <[email protected]> Cc: Al Viro <[email protected]> Cc: [email protected] Reviewed-by: Christoph Hellwig <[email protected]> Signed-off-by: Christian Brauner <[email protected]>
1 parent 8e53891 commit db99855

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

fs/ext4/ialloc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -970,7 +970,7 @@ struct inode *__ext4_new_inode(struct user_namespace *mnt_userns,
970970
i_gid_write(inode, owner[1]);
971971
} else if (test_opt(sb, GRPID)) {
972972
inode->i_mode = mode;
973-
inode->i_uid = mapped_fsuid(mnt_userns);
973+
inode_fsuid_set(inode, mnt_userns);
974974
inode->i_gid = dir->i_gid;
975975
} else
976976
inode_init_owner(mnt_userns, inode, dir, mode);

fs/inode.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2148,7 +2148,7 @@ EXPORT_SYMBOL(init_special_inode);
21482148
void inode_init_owner(struct user_namespace *mnt_userns, struct inode *inode,
21492149
const struct inode *dir, umode_t mode)
21502150
{
2151-
inode->i_uid = mapped_fsuid(mnt_userns);
2151+
inode_fsuid_set(inode, mnt_userns);
21522152
if (dir && dir->i_mode & S_ISGID) {
21532153
inode->i_gid = dir->i_gid;
21542154

@@ -2160,7 +2160,7 @@ void inode_init_owner(struct user_namespace *mnt_userns, struct inode *inode,
21602160
!capable_wrt_inode_uidgid(mnt_userns, dir, CAP_FSETID))
21612161
mode &= ~S_ISGID;
21622162
} else
2163-
inode->i_gid = mapped_fsgid(mnt_userns);
2163+
inode_fsgid_set(inode, mnt_userns);
21642164
inode->i_mode = mode;
21652165
}
21662166
EXPORT_SYMBOL(inode_init_owner);

fs/xfs/xfs_inode.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ xfs_init_new_inode(
812812

813813
if (dir && !(dir->i_mode & S_ISGID) &&
814814
(mp->m_flags & XFS_MOUNT_GRPID)) {
815-
inode->i_uid = mapped_fsuid(mnt_userns);
815+
inode_fsuid_set(inode, mnt_userns);
816816
inode->i_gid = dir->i_gid;
817817
inode->i_mode = mode;
818818
} else {

include/linux/fs.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1692,6 +1692,34 @@ static inline kgid_t mapped_fsgid(struct user_namespace *mnt_userns)
16921692
return kgid_from_mnt(mnt_userns, current_fsgid());
16931693
}
16941694

1695+
/**
1696+
* inode_fsuid_set - initialize inode's i_uid field with callers fsuid
1697+
* @inode: inode to initialize
1698+
* @mnt_userns: user namespace of the mount the inode was found from
1699+
*
1700+
* Initialize the i_uid field of @inode. If the inode was found/created via
1701+
* an idmapped mount map the caller's fsuid according to @mnt_users.
1702+
*/
1703+
static inline void inode_fsuid_set(struct inode *inode,
1704+
struct user_namespace *mnt_userns)
1705+
{
1706+
inode->i_uid = mapped_fsuid(mnt_userns);
1707+
}
1708+
1709+
/**
1710+
* inode_fsgid_set - initialize inode's i_gid field with callers fsgid
1711+
* @inode: inode to initialize
1712+
* @mnt_userns: user namespace of the mount the inode was found from
1713+
*
1714+
* Initialize the i_gid field of @inode. If the inode was found/created via
1715+
* an idmapped mount map the caller's fsgid according to @mnt_users.
1716+
*/
1717+
static inline void inode_fsgid_set(struct inode *inode,
1718+
struct user_namespace *mnt_userns)
1719+
{
1720+
inode->i_gid = mapped_fsgid(mnt_userns);
1721+
}
1722+
16951723
/**
16961724
* fsuidgid_has_mapping() - check whether caller's fsuid/fsgid is mapped
16971725
* @sb: the superblock we want a mapping in

0 commit comments

Comments
 (0)