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;
49 int retval = EXIT_SUCCESS;
50 const char *filename = NULL;
51 int exists = 0, found = 0;
55 bufsize = atoi(argv[2]);
58 if (bufsize > BUF_SIZE)
63 if (argc > 4 && !strcmp(argv[4], "-v"))
66 } else if (argc < 2) {
70 fd = open(argv[1], O_RDONLY | O_DIRECTORY);
77 exists = !faccessat(fd, filename, F_OK, AT_SYMLINK_NOFOLLOW);
78 if (!exists && errno != ENOENT) {
86 nread = syscall(SYS_getdents64, fd, buf, bufsize);
95 for (bpos = 0; bpos < nread; total++) {
96 d = (struct linux_dirent64 *) (buf + bpos);
98 if (total >= HISTORY_LEN) {
99 fprintf(stderr, "too many files\n");
103 for (i = 0; i < total; i++)
105 if (d_off_history[i] == d->d_off) {
106 fprintf(stderr, "entries %d and %d have duplicate d_off %lld\n",
107 i, total, (long long int)d->d_off);
108 retval = EXIT_FAILURE;
111 d_off_history[total] = d->d_off;
112 d_ino_history[total] = d->d_ino;
115 printf("entry #%d: %s (d_ino=%lld, d_off=%lld)\n",
116 i, d->d_name, (long long int)d->d_ino,
117 (long long int)d->d_off);
118 if (!strcmp(filename, d->d_name))
126 if (exists == found) {
127 printf("entry %s %sfound as expected\n", filename, found ? "" : "not ");
129 fprintf(stderr, "%s entry %s\n",
130 exists ? "missing" : "stale", filename);
135 /* check if seek works correctly */
136 d = (struct linux_dirent64 *)buf;
137 for (i = total - 1; i >= 0; i--)
139 lret = lseek(fd, i > 0 ? d_off_history[i - 1] : 0, SEEK_SET);
145 nread = syscall(SYS_getdents64, fd, buf, bufsize);
152 fprintf(stderr, "getdents returned 0 on entry %d\n", i);
153 retval = EXIT_FAILURE;
156 if (d->d_ino != d_ino_history[i]) {
157 fprintf(stderr, "entry %d has inode %lld, expected %lld\n",
158 i, (long long int)d->d_ino, (long long int)d_ino_history[i]);
159 retval = EXIT_FAILURE;