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