1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2011 GraÅžvydas Ignotas
7 * This test checks if no duplicate d_off values are returned and
8 * that these offsets are seekable to entry with the right inode.
19 #include <sys/syscall.h>
21 struct linux_dirent64 {
24 unsigned short d_reclen;
30 #define HISTORY_LEN 1024
32 static uint64_t d_off_history[HISTORY_LEN];
33 static uint64_t d_ino_history[HISTORY_LEN];
37 fprintf(stderr, "usage: t_dir_offset2: <dir> [[bufsize] [-|+]<filename> [-v]]\n");
41 int main(int argc, char *argv[])
45 int nread, bufsize = BUF_SIZE;
46 struct linux_dirent64 *d;
50 int retval = EXIT_SUCCESS;
51 const char *filename = NULL;
52 int exists = 0, found = 0;
53 int modify = 0, verbose = 0;
56 bufsize = atoi(argv[2]);
59 if (bufsize > BUF_SIZE)
64 /* +<filename> creates, -<filename> removes */
65 if (filename[0] == '+')
67 else if (filename[0] == '-')
71 if (argc > 4 && !strcmp(argv[4], "-v"))
74 } else if (argc < 2) {
78 fd = open(argv[1], O_RDONLY | O_DIRECTORY);
85 exists = !fstatat(fd, filename, &st, AT_SYMLINK_NOFOLLOW);
86 if (!exists && errno != ENOENT) {
94 nread = syscall(SYS_getdents64, fd, buf, bufsize);
100 if (modify && fd2 < 0 && total == 0) {
101 printf("getdents at offset 0 returned %d bytes\n", nread);
103 /* create/unlink entry after first getdents */
105 if (openat(fd, filename, O_CREAT, 0600) < 0) {
110 printf("created entry %s\n", filename);
111 } else if (modify < 0) {
112 if (unlinkat(fd, filename, 0) < 0) {
117 printf("unlinked entry %s\n", filename);
121 * Old fd may not return new entry and may return stale
122 * entries which is allowed. Keep old fd open and open
123 * a new fd to check for stale or missing entries later.
125 fd2 = open(argv[1], O_RDONLY | O_DIRECTORY);
133 if (fd2 < 0 || fd == fd2)
136 /* Re-iterate with new fd leaving old fd open */
143 for (bpos = 0; bpos < nread; total++) {
144 d = (struct linux_dirent64 *) (buf + bpos);
146 if (total >= HISTORY_LEN) {
147 fprintf(stderr, "too many files\n");
151 for (i = 0; i < total; i++)
153 if (d_off_history[i] == d->d_off) {
154 fprintf(stderr, "entries %d and %d have duplicate d_off %lld\n",
155 i, total, (long long int)d->d_off);
156 retval = EXIT_FAILURE;
159 d_off_history[total] = d->d_off;
160 d_ino_history[total] = d->d_ino;
163 printf("entry #%d: %s (d_ino=%lld, d_off=%lld)\n",
164 i, d->d_name, (long long int)d->d_ino,
165 (long long int)d->d_off);
166 if (!strcmp(filename, d->d_name)) {
168 if (st.st_ino && d->d_ino != st.st_ino) {
169 fprintf(stderr, "entry %s has inconsistent d_ino (%lld != %lld)\n",
171 (long long int)d->d_ino,
172 (long long int)st.st_ino);
182 if (exists == found) {
183 printf("entry %s %sfound as expected\n", filename, found ? "" : "not ");
185 fprintf(stderr, "%s entry %s\n",
186 exists ? "missing" : "stale", filename);
191 /* check if seek works correctly */
192 d = (struct linux_dirent64 *)buf;
193 for (i = total - 1; i >= 0; i--)
195 lret = lseek(fd, i > 0 ? d_off_history[i - 1] : 0, SEEK_SET);
201 nread = syscall(SYS_getdents64, fd, buf, bufsize);
208 fprintf(stderr, "getdents returned 0 on entry %d\n", i);
209 retval = EXIT_FAILURE;
212 if (d->d_ino != d_ino_history[i]) {
213 fprintf(stderr, "entry %d has inode %lld, expected %lld\n",
214 i, (long long int)d->d_ino, (long long int)d_ino_history[i]);
215 retval = EXIT_FAILURE;