@@ -1575,52 +1575,172 @@ static inline void i_gid_write(struct inode *inode, gid_t gid)
15751575 inode -> i_gid = make_kgid (inode -> i_sb -> s_user_ns , gid );
15761576}
15771577
1578+ /**
1579+ * kuid_into_mnt - map a kuid down into a mnt_userns
1580+ * @mnt_userns: user namespace of the relevant mount
1581+ * @kuid: kuid to be mapped
1582+ *
1583+ * Return: @kuid mapped according to @mnt_userns.
1584+ * If @kuid has no mapping INVALID_UID is returned.
1585+ */
15781586static inline kuid_t kuid_into_mnt (struct user_namespace * mnt_userns ,
15791587 kuid_t kuid )
15801588{
15811589 return make_kuid (mnt_userns , __kuid_val (kuid ));
15821590}
15831591
1592+ /**
1593+ * kgid_into_mnt - map a kgid down into a mnt_userns
1594+ * @mnt_userns: user namespace of the relevant mount
1595+ * @kgid: kgid to be mapped
1596+ *
1597+ * Return: @kgid mapped according to @mnt_userns.
1598+ * If @kgid has no mapping INVALID_GID is returned.
1599+ */
15841600static inline kgid_t kgid_into_mnt (struct user_namespace * mnt_userns ,
15851601 kgid_t kgid )
15861602{
15871603 return make_kgid (mnt_userns , __kgid_val (kgid ));
15881604}
15891605
1606+ /**
1607+ * i_uid_into_mnt - map an inode's i_uid down into a mnt_userns
1608+ * @mnt_userns: user namespace of the mount the inode was found from
1609+ * @inode: inode to map
1610+ *
1611+ * Return: the inode's i_uid mapped down according to @mnt_userns.
1612+ * If the inode's i_uid has no mapping INVALID_UID is returned.
1613+ */
15901614static inline kuid_t i_uid_into_mnt (struct user_namespace * mnt_userns ,
15911615 const struct inode * inode )
15921616{
15931617 return kuid_into_mnt (mnt_userns , inode -> i_uid );
15941618}
15951619
1620+ /**
1621+ * i_gid_into_mnt - map an inode's i_gid down into a mnt_userns
1622+ * @mnt_userns: user namespace of the mount the inode was found from
1623+ * @inode: inode to map
1624+ *
1625+ * Return: the inode's i_gid mapped down according to @mnt_userns.
1626+ * If the inode's i_gid has no mapping INVALID_GID is returned.
1627+ */
15961628static inline kgid_t i_gid_into_mnt (struct user_namespace * mnt_userns ,
15971629 const struct inode * inode )
15981630{
15991631 return kgid_into_mnt (mnt_userns , inode -> i_gid );
16001632}
16011633
1634+ /**
1635+ * kuid_from_mnt - map a kuid up into a mnt_userns
1636+ * @mnt_userns: user namespace of the relevant mount
1637+ * @kuid: kuid to be mapped
1638+ *
1639+ * Return: @kuid mapped up according to @mnt_userns.
1640+ * If @kuid has no mapping INVALID_UID is returned.
1641+ */
16021642static inline kuid_t kuid_from_mnt (struct user_namespace * mnt_userns ,
16031643 kuid_t kuid )
16041644{
16051645 return KUIDT_INIT (from_kuid (mnt_userns , kuid ));
16061646}
16071647
1648+ /**
1649+ * kgid_from_mnt - map a kgid up into a mnt_userns
1650+ * @mnt_userns: user namespace of the relevant mount
1651+ * @kgid: kgid to be mapped
1652+ *
1653+ * Return: @kgid mapped up according to @mnt_userns.
1654+ * If @kgid has no mapping INVALID_GID is returned.
1655+ */
16081656static inline kgid_t kgid_from_mnt (struct user_namespace * mnt_userns ,
16091657 kgid_t kgid )
16101658{
16111659 return KGIDT_INIT (from_kgid (mnt_userns , kgid ));
16121660}
16131661
1614- static inline kuid_t fsuid_into_mnt (struct user_namespace * mnt_userns )
1662+ /**
1663+ * mapped_fsuid - return caller's fsuid mapped up into a mnt_userns
1664+ * @mnt_userns: user namespace of the relevant mount
1665+ *
1666+ * Use this helper to initialize a new vfs or filesystem object based on
1667+ * the caller's fsuid. A common example is initializing the i_uid field of
1668+ * a newly allocated inode triggered by a creation event such as mkdir or
1669+ * O_CREAT. Other examples include the allocation of quotas for a specific
1670+ * user.
1671+ *
1672+ * Return: the caller's current fsuid mapped up according to @mnt_userns.
1673+ */
1674+ static inline kuid_t mapped_fsuid (struct user_namespace * mnt_userns )
16151675{
16161676 return kuid_from_mnt (mnt_userns , current_fsuid ());
16171677}
16181678
1619- static inline kgid_t fsgid_into_mnt (struct user_namespace * mnt_userns )
1679+ /**
1680+ * mapped_fsgid - return caller's fsgid mapped up into a mnt_userns
1681+ * @mnt_userns: user namespace of the relevant mount
1682+ *
1683+ * Use this helper to initialize a new vfs or filesystem object based on
1684+ * the caller's fsgid. A common example is initializing the i_gid field of
1685+ * a newly allocated inode triggered by a creation event such as mkdir or
1686+ * O_CREAT. Other examples include the allocation of quotas for a specific
1687+ * user.
1688+ *
1689+ * Return: the caller's current fsgid mapped up according to @mnt_userns.
1690+ */
1691+ static inline kgid_t mapped_fsgid (struct user_namespace * mnt_userns )
16201692{
16211693 return kgid_from_mnt (mnt_userns , current_fsgid ());
16221694}
16231695
1696+ /**
1697+ * inode_fsuid_set - initialize inode's i_uid field with callers fsuid
1698+ * @inode: inode to initialize
1699+ * @mnt_userns: user namespace of the mount the inode was found from
1700+ *
1701+ * Initialize the i_uid field of @inode. If the inode was found/created via
1702+ * an idmapped mount map the caller's fsuid according to @mnt_users.
1703+ */
1704+ static inline void inode_fsuid_set (struct inode * inode ,
1705+ struct user_namespace * mnt_userns )
1706+ {
1707+ inode -> i_uid = mapped_fsuid (mnt_userns );
1708+ }
1709+
1710+ /**
1711+ * inode_fsgid_set - initialize inode's i_gid field with callers fsgid
1712+ * @inode: inode to initialize
1713+ * @mnt_userns: user namespace of the mount the inode was found from
1714+ *
1715+ * Initialize the i_gid field of @inode. If the inode was found/created via
1716+ * an idmapped mount map the caller's fsgid according to @mnt_users.
1717+ */
1718+ static inline void inode_fsgid_set (struct inode * inode ,
1719+ struct user_namespace * mnt_userns )
1720+ {
1721+ inode -> i_gid = mapped_fsgid (mnt_userns );
1722+ }
1723+
1724+ /**
1725+ * fsuidgid_has_mapping() - check whether caller's fsuid/fsgid is mapped
1726+ * @sb: the superblock we want a mapping in
1727+ * @mnt_userns: user namespace of the relevant mount
1728+ *
1729+ * Check whether the caller's fsuid and fsgid have a valid mapping in the
1730+ * s_user_ns of the superblock @sb. If the caller is on an idmapped mount map
1731+ * the caller's fsuid and fsgid according to the @mnt_userns first.
1732+ *
1733+ * Return: true if fsuid and fsgid is mapped, false if not.
1734+ */
1735+ static inline bool fsuidgid_has_mapping (struct super_block * sb ,
1736+ struct user_namespace * mnt_userns )
1737+ {
1738+ struct user_namespace * s_user_ns = sb -> s_user_ns ;
1739+
1740+ return kuid_has_mapping (s_user_ns , mapped_fsuid (mnt_userns )) &&
1741+ kgid_has_mapping (s_user_ns , mapped_fsgid (mnt_userns ));
1742+ }
1743+
16241744extern struct timespec64 current_time (struct inode * inode );
16251745
16261746/*
0 commit comments