1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) Linux Test Project, 2021-2022
4 * Copyright (c) 2020 Linaro Limited. All rights reserved.
5 * Author: Viresh Kumar <viresh.kumar@linaro.org>
6 */
7
8 #ifndef LAPI_FSMOUNT_H__
9 #define LAPI_FSMOUNT_H__
10
11 #include "config.h"
12 #include <sys/syscall.h>
13 #include <sys/types.h>
14 #include <sys/mount.h>
15
16 #ifndef HAVE_FSOPEN
17 # ifdef HAVE_LINUX_MOUNT_H
18 # include <linux/mount.h>
19 # endif
20 #endif
21
22 #include "lapi/fcntl.h"
23 #include "lapi/syscalls.h"
24
25 /*
26 * Mount attributes.
27 */
28 #ifndef MOUNT_ATTR_RDONLY
29 # define MOUNT_ATTR_RDONLY 0x00000001 /* Mount read-only */
30 #endif
31 #ifndef MOUNT_ATTR_NOSUID
32 # define MOUNT_ATTR_NOSUID 0x00000002 /* Ignore suid and sgid bits */
33 #endif
34 #ifndef MOUNT_ATTR_NODEV
35 # define MOUNT_ATTR_NODEV 0x00000004 /* Disallow access to device special files */
36 #endif
37 #ifndef MOUNT_ATTR_NOEXEC
38 # define MOUNT_ATTR_NOEXEC 0x00000008 /* Disallow program execution */
39 #endif
40 #ifndef MOUNT_ATTR_NODIRATIME
41 # define MOUNT_ATTR_NODIRATIME 0x00000080 /* Do not update directory access times */
42 #endif
43 #ifndef MOUNT_ATTR_NOSYMFOLLOW
44 # define MOUNT_ATTR_NOSYMFOLLOW 0x00200000 /* Do not follow symlinks */
45 #endif
46
47 #ifndef ST_NOSYMFOLLOW
48 # define ST_NOSYMFOLLOW 0x2000 /* do not follow symlinks */
49 #endif
50
51 #ifndef HAVE_STRUCT_MOUNT_ATTR
52 /*
53 * mount_setattr()
54 */
55 struct mount_attr {
56 uint64_t attr_set;
57 uint64_t attr_clr;
58 uint64_t propagation;
59 uint64_t userns_fd;
60 };
61 #endif
62
63 #ifndef HAVE_FSOPEN
fsopen(const char * fsname,unsigned int flags)64 static inline int fsopen(const char *fsname, unsigned int flags)
65 {
66 return tst_syscall(__NR_fsopen, fsname, flags);
67 }
68 #endif /* HAVE_FSOPEN */
69
70 #ifndef HAVE_FSCONFIG
fsconfig(int fd,unsigned int cmd,const char * key,const void * value,int aux)71 static inline int fsconfig(int fd, unsigned int cmd, const char *key,
72 const void *value, int aux)
73 {
74 return tst_syscall(__NR_fsconfig, fd, cmd, key, value, aux);
75 }
76 #endif /* HAVE_FSCONFIG */
77
78 #ifndef HAVE_FSMOUNT
fsmount(int fd,unsigned int flags,unsigned int mount_attrs)79 static inline int fsmount(int fd, unsigned int flags, unsigned int mount_attrs)
80 {
81 return tst_syscall(__NR_fsmount, fd, flags, mount_attrs);
82 }
83 #endif /* HAVE_FSMOUNT */
84
85 #ifndef HAVE_FSPICK
fspick(int dirfd,const char * pathname,unsigned int flags)86 static inline int fspick(int dirfd, const char *pathname, unsigned int flags)
87 {
88 return tst_syscall(__NR_fspick, dirfd, pathname, flags);
89 }
90 #endif /* HAVE_FSPICK */
91
92 #ifndef HAVE_MOVE_MOUNT
move_mount(int from_dirfd,const char * from_pathname,int to_dirfd,const char * to_pathname,unsigned int flags)93 static inline int move_mount(int from_dirfd, const char *from_pathname,
94 int to_dirfd, const char *to_pathname,
95 unsigned int flags)
96 {
97 return tst_syscall(__NR_move_mount, from_dirfd, from_pathname, to_dirfd,
98 to_pathname, flags);
99 }
100 #endif /* HAVE_MOVE_MOUNT */
101
102 #ifndef HAVE_OPEN_TREE
open_tree(int dirfd,const char * pathname,unsigned int flags)103 static inline int open_tree(int dirfd, const char *pathname, unsigned int flags)
104 {
105 return tst_syscall(__NR_open_tree, dirfd, pathname, flags);
106 }
107 #endif /* HAVE_OPEN_TREE */
108
109 #ifndef HAVE_MOUNT_SETATTR
mount_setattr(int dirfd,const char * from_pathname,unsigned int flags,struct mount_attr * attr,size_t size)110 static inline int mount_setattr(int dirfd, const char *from_pathname, unsigned int flags,
111 struct mount_attr *attr, size_t size)
112 {
113 return tst_syscall(__NR_mount_setattr, dirfd, from_pathname, flags,
114 attr, size);
115 }
116 #endif /* HAVE_MOUNT_SETATTR */
117
118 /*
119 * New headers added in kernel after 5.2 release, create them for old userspace.
120 */
121
122 #ifndef OPEN_TREE_CLONE
123
124 /*
125 * open_tree() flags.
126 */
127 #define OPEN_TREE_CLONE 1 /* Clone the target tree and attach the clone */
128 #define OPEN_TREE_CLOEXEC O_CLOEXEC /* Close the file on execve() */
129
130 /*
131 * move_mount() flags.
132 */
133 #define MOVE_MOUNT_F_SYMLINKS 0x00000001 /* Follow symlinks on from path */
134 #define MOVE_MOUNT_F_AUTOMOUNTS 0x00000002 /* Follow automounts on from path */
135 #define MOVE_MOUNT_F_EMPTY_PATH 0x00000004 /* Empty from path permitted */
136 #define MOVE_MOUNT_T_SYMLINKS 0x00000010 /* Follow symlinks on to path */
137 #define MOVE_MOUNT_T_AUTOMOUNTS 0x00000020 /* Follow automounts on to path */
138 #define MOVE_MOUNT_T_EMPTY_PATH 0x00000040 /* Empty to path permitted */
139 #define MOVE_MOUNT__MASK 0x00000077
140
141 /*
142 * fsopen() flags.
143 */
144 #define FSOPEN_CLOEXEC 0x00000001
145
146 /*
147 * fspick() flags.
148 */
149 #define FSPICK_CLOEXEC 0x00000001
150 #define FSPICK_SYMLINK_NOFOLLOW 0x00000002
151 #define FSPICK_NO_AUTOMOUNT 0x00000004
152 #define FSPICK_EMPTY_PATH 0x00000008
153
154 /*
155 * The type of fsconfig() call made.
156 */
157 enum fsconfig_command {
158 FSCONFIG_SET_FLAG = 0, /* Set parameter, supplying no value */
159 FSCONFIG_SET_STRING = 1, /* Set parameter, supplying a string value */
160 FSCONFIG_SET_BINARY = 2, /* Set parameter, supplying a binary blob value */
161 FSCONFIG_SET_PATH = 3, /* Set parameter, supplying an object by path */
162 FSCONFIG_SET_PATH_EMPTY = 4, /* Set parameter, supplying an object by (empty) path */
163 FSCONFIG_SET_FD = 5, /* Set parameter, supplying an object by fd */
164 FSCONFIG_CMD_CREATE = 6, /* Invoke superblock creation */
165 FSCONFIG_CMD_RECONFIGURE = 7, /* Invoke superblock reconfiguration */
166 };
167
168 /*
169 * fsmount() flags.
170 */
171 #define FSMOUNT_CLOEXEC 0x00000001
172
173 /*
174 * Mount attributes.
175 */
176 #define MOUNT_ATTR_RDONLY 0x00000001 /* Mount read-only */
177 #define MOUNT_ATTR_NOSUID 0x00000002 /* Ignore suid and sgid bits */
178 #define MOUNT_ATTR_NODEV 0x00000004 /* Disallow access to device special files */
179 #define MOUNT_ATTR_NOEXEC 0x00000008 /* Disallow program execution */
180 #define MOUNT_ATTR__ATIME 0x00000070 /* Setting on how atime should be updated */
181 #define MOUNT_ATTR_RELATIME 0x00000000 /* - Update atime relative to mtime/ctime. */
182 #define MOUNT_ATTR_NOATIME 0x00000010 /* - Do not update access times. */
183 #define MOUNT_ATTR_STRICTATIME 0x00000020 /* - Always perform atime updates */
184 #define MOUNT_ATTR_NODIRATIME 0x00000080 /* Do not update directory access times */
185
186 #endif /* OPEN_TREE_CLONE */
187
fsopen_supported_by_kernel(void)188 static inline void fsopen_supported_by_kernel(void)
189 {
190 long ret;
191
192 if ((tst_kvercmp(5, 2, 0)) < 0) {
193 /* Check if the syscall is backported on an older kernel */
194 ret = syscall(__NR_fsopen, NULL, 0);
195 if (ret != -1)
196 SAFE_CLOSE(ret);
197 else if (errno == ENOSYS)
198 tst_brk(TCONF, "Test not supported on kernel version < v5.2");
199 }
200 }
201
202 #endif /* LAPI_FSMOUNT_H__ */
203