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;
52 int modify = 0, verbose = 0;
55 bufsize = atoi(argv[2]);
58 if (bufsize > BUF_SIZE)
63 /* +<filename> creates, -<filename> removes */
64 if (filename[0] == '+')
66 else if (filename[0] == '-')
70 if (argc > 4 && !strcmp(argv[4], "-v"))
73 } else if (argc < 2) {
77 fd = open(argv[1], O_RDONLY | O_DIRECTORY);
84 exists = !faccessat(fd, filename, F_OK, AT_SYMLINK_NOFOLLOW);
85 if (!exists && errno != ENOENT) {
93 nread = syscall(SYS_getdents64, fd, buf, bufsize);
99 if (modify && fd2 < 0 && total == 0) {
100 printf("getdents at offset 0 returned %d bytes\n", nread);
102 /* create/unlink entry after first getdents */
104 if (openat(fd, filename, O_CREAT, 0600) < 0) {
109 printf("created entry %s\n", filename);
110 } else if (modify < 0) {
111 if (unlinkat(fd, filename, 0) < 0) {
116 printf("unlinked entry %s\n", filename);
120 * Old fd may not return new entry and may return stale
121 * entries which is allowed. Keep old fd open and open
122 * a new fd to check for stale or missing entries later.
124 fd2 = open(argv[1], O_RDONLY | O_DIRECTORY);
132 if (fd2 < 0 || fd == fd2)
135 /* Re-iterate with new fd leaving old fd open */
145 for (bpos = 0; bpos < nread; total++) {
146 d = (struct linux_dirent64 *) (buf + bpos);
148 if (total >= HISTORY_LEN) {
149 fprintf(stderr, "too many files\n");
153 for (i = 0; i < total; i++)
155 if (d_off_history[i] == d->d_off) {
156 fprintf(stderr, "entries %d and %d have duplicate d_off %lld\n",
157 i, total, (long long int)d->d_off);
158 retval = EXIT_FAILURE;
161 d_off_history[total] = d->d_off;
162 d_ino_history[total] = d->d_ino;
165 printf("entry #%d: %s (d_ino=%lld, d_off=%lld)\n",
166 i, d->d_name, (long long int)d->d_ino,
167 (long long int)d->d_off);
168 if (!strcmp(filename, d->d_name))
176 if (exists == found) {
177 printf("entry %s %sfound as expected\n", filename, found ? "" : "not ");
179 fprintf(stderr, "%s entry %s\n",
180 exists ? "missing" : "stale", filename);
185 /* check if seek works correctly */
186 d = (struct linux_dirent64 *)buf;
187 for (i = total - 1; i >= 0; i--)
189 lret = lseek(fd, i > 0 ? d_off_history[i - 1] : 0, SEEK_SET);
195 nread = syscall(SYS_getdents64, fd, buf, bufsize);
202 fprintf(stderr, "getdents returned 0 on entry %d\n", i);
203 retval = EXIT_FAILURE;
206 if (d->d_ino != d_ino_history[i]) {
207 fprintf(stderr, "entry %d has inode %lld, expected %lld\n",
208 i, (long long int)d->d_ino, (long long int)d_ino_history[i]);
209 retval = EXIT_FAILURE;