4 # Copyright (c) 2009 Peter Holm <pho@FreeBSD.org>
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
10 # 1. Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution.
16 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 # Test case for vfs.lookup_shared=1 that shows possible name cache
34 # $ ls -l /tmp/file.05015?
35 # ls: /tmp/file.050150: No such file or directory
36 # $ fsdb -r /dev/ad4s1e
37 # ** /dev/ad4s1e (NO WRITE)
38 # Examining file system `/dev/ad4s1e'
39 # Last Mounted on /tmp
40 # current inode: directory
41 # I=2 MODE=41777 SIZE=5120
42 # BTIME=May 7 05:54:47 2006 [0 nsec]
43 # MTIME=Apr 2 11:27:36 2009 [0 nsec]
44 # CTIME=Apr 2 11:27:36 2009 [0 nsec]
45 # ATIME=Apr 2 12:00:30 2009 [0 nsec]
46 # OWNER=root GRP=wheel LINKCNT=35 FLAGS=0 BLKCNT=c GEN=65f71df4
47 # fsdb (inum: 2)> lookup file.050150
48 # component `file.050150': current inode: regular file
49 # I=198 MODE=100600 SIZE=0
50 # BTIME=Apr 2 11:24:33 2009 [0 nsec]
51 # MTIME=Apr 2 11:24:33 2009 [0 nsec]
52 # CTIME=Apr 2 11:24:33 2009 [0 nsec]
53 # ATIME=Apr 2 11:24:33 2009 [0 nsec]
54 # OWNER=pho GRP=wheel LINKCNT=1 FLAGS=0 BLKCNT=0 GEN=1deaab3a
55 # fsdb (inum: 198)> quit
58 # Consistency is restored by a umount + mount of the FS
61 # No problems seen with vfs.lookup_shared=0.
62 # Does not fail in a "private" subdirectory
68 sed '1,/^EOF/d' < $odir/$0 > namecache.c
69 cc -o namecache -Wall namecache.c
72 #dir=/tmp/namecache.dir # No problems seen
74 [ -d $dir ] || mkdir -p $dir
87 if ls -l ${dir}/file.0* 2>&1 | egrep "file.0[0-9]" | grep -q "No such file"; then
88 echo "ls -l ${dir}/file.0*"
92 rm -f /tmp/namecache # /${dir}/file.0*
95 /* Test scenario for possible name cache problem */
97 #include <sys/types.h>
105 #include <sys/param.h>
106 #include <sys/stat.h>
107 #include <sys/wait.h>
110 static char path[MAXPATHLEN+1];
111 static char buf[64 * 1024];
117 int space = sizeof(buf);
123 if ((fd = open(".", O_RDONLY)) == -1)
124 err(1, "open(%s)", ".");
127 if ((n = getdirentries(fd, bp, space, &base)) == -1)
128 err(1, "getdirentries");
135 dp = (struct dirent *)bp;
137 if (strcmp(path, dp->d_name) == 0) {
139 if (stat(dp->d_name, &statb) == -1) {
140 warn("stat(%s)", dp->d_name);
141 printf("name: %-10s, inode %7d, type %2d, namelen %d, d_reclen %d\n",
142 dp->d_name, dp->d_fileno, dp->d_type, dp->d_namlen,
146 printf("stat(%s) succeeded!\n", path);
151 bp = bp + dp->d_reclen;
152 dp = (struct dirent *)bp;
153 if (dp->d_reclen <= 0)
162 if ((fd = open(path, O_RDWR, 0600)) < 0) {
163 warn("open(%s). %s:%d", path, __FILE__, __LINE__);
175 if ((fd = open(path, O_RDWR, 0600)) < 0) {
176 warn("open(%s). %s:%d", path, __FILE__, __LINE__);
185 main(int argc, char **argv)
190 for (i = 0; i < 10000; i++) {
191 if (sprintf(path, "file.0%d", getpid()) < 0)
193 if ((fd = open(path, O_CREAT | O_RDWR, 0600)) == -1)
194 err(1, "open(%s)", path);
197 if ((pid = fork()) == 0) {
201 } else if (pid > 0) {
203 if (waitpid(pid, &status, 0) == -1)
204 warn("waitpid(%d)", pid);
206 err(1, "fork(), %s:%d", __FILE__, __LINE__);
208 if (unlink(path) == -1)
209 err(1, "unlink(%s). %s:%d", path, __FILE__, __LINE__);