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