1 // SPDX-License-Identifier: GPL-2.0-or-later
2 // Copyright (c) 2024 Christian Brauner <brauner@kernel.org>
3 
4 #define _GNU_SOURCE
5 #include <fcntl.h>
6 #include <sched.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <sys/stat.h>
10 #include <sys/mount.h>
11 #include <unistd.h>
12 
13 #include "statmount.h"
14 #include "../../kselftest_harness.h"
15 
16 #ifndef LISTMOUNT_REVERSE
17 #define LISTMOUNT_REVERSE    (1 << 0) /* List later mounts first */
18 #endif
19 
20 #define LISTMNT_BUFFER 10
21 
22 /* Check that all mount ids are in increasing order. */
TEST(listmount_forward)23 TEST(listmount_forward)
24 {
25 	uint64_t list[LISTMNT_BUFFER], last_mnt_id = 0;
26 
27 	for (;;) {
28 		ssize_t nr_mounts;
29 
30 		nr_mounts = listmount(LSMT_ROOT, 0, last_mnt_id,
31 				      list, LISTMNT_BUFFER, 0);
32 		ASSERT_GE(nr_mounts, 0);
33 		if (nr_mounts == 0)
34 			break;
35 
36 		for (size_t cur = 0; cur < nr_mounts; cur++) {
37 			if (cur < nr_mounts - 1)
38 				ASSERT_LT(list[cur], list[cur + 1]);
39 			last_mnt_id = list[cur];
40 		}
41 	}
42 }
43 
44 /* Check that all mount ids are in decreasing order. */
TEST(listmount_backward)45 TEST(listmount_backward)
46 {
47 	uint64_t list[LISTMNT_BUFFER], last_mnt_id = 0;
48 
49 	for (;;) {
50 		ssize_t nr_mounts;
51 
52 		nr_mounts = listmount(LSMT_ROOT, 0, last_mnt_id,
53 				      list, LISTMNT_BUFFER, LISTMOUNT_REVERSE);
54 		ASSERT_GE(nr_mounts, 0);
55 		if (nr_mounts == 0)
56 			break;
57 
58 		for (size_t cur = 0; cur < nr_mounts; cur++) {
59 			if (cur < nr_mounts - 1)
60 				ASSERT_GT(list[cur], list[cur + 1]);
61 			last_mnt_id = list[cur];
62 		}
63 	}
64 }
65 
66 TEST_HARNESS_MAIN
67