fs: move mapping helpers
[linux-block.git] / include / linux / mnt_idmapping.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_MNT_IDMAPPING_H
3 #define _LINUX_MNT_IDMAPPING_H
4
5 #include <linux/types.h>
6 #include <linux/uidgid.h>
7
8 struct user_namespace;
9 extern struct user_namespace init_user_ns;
10
11 /**
12  * kuid_into_mnt - map a kuid down into a mnt_userns
13  * @mnt_userns: user namespace of the relevant mount
14  * @kuid: kuid to be mapped
15  *
16  * Return: @kuid mapped according to @mnt_userns.
17  * If @kuid has no mapping INVALID_UID is returned.
18  */
19 static inline kuid_t kuid_into_mnt(struct user_namespace *mnt_userns,
20                                    kuid_t kuid)
21 {
22         return make_kuid(mnt_userns, __kuid_val(kuid));
23 }
24
25 /**
26  * kgid_into_mnt - map a kgid down into a mnt_userns
27  * @mnt_userns: user namespace of the relevant mount
28  * @kgid: kgid to be mapped
29  *
30  * Return: @kgid mapped according to @mnt_userns.
31  * If @kgid has no mapping INVALID_GID is returned.
32  */
33 static inline kgid_t kgid_into_mnt(struct user_namespace *mnt_userns,
34                                    kgid_t kgid)
35 {
36         return make_kgid(mnt_userns, __kgid_val(kgid));
37 }
38
39 /**
40  * kuid_from_mnt - map a kuid up into a mnt_userns
41  * @mnt_userns: user namespace of the relevant mount
42  * @kuid: kuid to be mapped
43  *
44  * Return: @kuid mapped up according to @mnt_userns.
45  * If @kuid has no mapping INVALID_UID is returned.
46  */
47 static inline kuid_t kuid_from_mnt(struct user_namespace *mnt_userns,
48                                    kuid_t kuid)
49 {
50         return KUIDT_INIT(from_kuid(mnt_userns, kuid));
51 }
52
53 /**
54  * kgid_from_mnt - map a kgid up into a mnt_userns
55  * @mnt_userns: user namespace of the relevant mount
56  * @kgid: kgid to be mapped
57  *
58  * Return: @kgid mapped up according to @mnt_userns.
59  * If @kgid has no mapping INVALID_GID is returned.
60  */
61 static inline kgid_t kgid_from_mnt(struct user_namespace *mnt_userns,
62                                    kgid_t kgid)
63 {
64         return KGIDT_INIT(from_kgid(mnt_userns, kgid));
65 }
66
67 /**
68  * mapped_fsuid - return caller's fsuid mapped up into a mnt_userns
69  * @mnt_userns: user namespace of the relevant mount
70  *
71  * Use this helper to initialize a new vfs or filesystem object based on
72  * the caller's fsuid. A common example is initializing the i_uid field of
73  * a newly allocated inode triggered by a creation event such as mkdir or
74  * O_CREAT. Other examples include the allocation of quotas for a specific
75  * user.
76  *
77  * Return: the caller's current fsuid mapped up according to @mnt_userns.
78  */
79 static inline kuid_t mapped_fsuid(struct user_namespace *mnt_userns)
80 {
81         return kuid_from_mnt(mnt_userns, current_fsuid());
82 }
83
84 /**
85  * mapped_fsgid - return caller's fsgid mapped up into a mnt_userns
86  * @mnt_userns: user namespace of the relevant mount
87  *
88  * Use this helper to initialize a new vfs or filesystem object based on
89  * the caller's fsgid. A common example is initializing the i_gid field of
90  * a newly allocated inode triggered by a creation event such as mkdir or
91  * O_CREAT. Other examples include the allocation of quotas for a specific
92  * user.
93  *
94  * Return: the caller's current fsgid mapped up according to @mnt_userns.
95  */
96 static inline kgid_t mapped_fsgid(struct user_namespace *mnt_userns)
97 {
98         return kgid_from_mnt(mnt_userns, current_fsgid());
99 }
100
101 #endif /* _LINUX_MNT_IDMAPPING_H */