xref: /linux/tools/testing/selftests/filesystems/empty_mntns/empty_mntns.h (revision 32f54f2bbccfdeff81d930d18ccf3161a1c203b9)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 #ifndef EMPTY_MNTNS_H
3 #define EMPTY_MNTNS_H
4 
5 #include <errno.h>
6 #include <stdlib.h>
7 
8 #include "../statmount/statmount.h"
9 
10 #ifndef UNSHARE_EMPTY_MNTNS
11 #define UNSHARE_EMPTY_MNTNS	0x00100000
12 #endif
13 
14 #ifndef CLONE_EMPTY_MNTNS
15 #define CLONE_EMPTY_MNTNS	(1ULL << 37)
16 #endif
17 
18 static inline ssize_t count_mounts(void)
19 {
20 	uint64_t list[4096];
21 
22 	return listmount(LSMT_ROOT, 0, 0, list, sizeof(list) / sizeof(list[0]), 0);
23 }
24 
25 static inline struct statmount *statmount_alloc(uint64_t mnt_id,
26 						uint64_t mnt_ns_id,
27 						uint64_t mask)
28 {
29 	size_t bufsize = 1 << 15;
30 	struct statmount *buf;
31 	int ret;
32 
33 	for (;;) {
34 		buf = malloc(bufsize);
35 		if (!buf)
36 			return NULL;
37 
38 		ret = statmount(mnt_id, mnt_ns_id, 0, mask, buf, bufsize, 0);
39 		if (ret == 0)
40 			return buf;
41 
42 		free(buf);
43 		if (errno != EOVERFLOW)
44 			return NULL;
45 
46 		bufsize <<= 1;
47 	}
48 }
49 
50 #endif /* EMPTY_MNTNS_H */
51