/*-
* Copyright (c) 2010 The NetBSD Foundation, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* The lookup is permitted to modify the access time of
* any directories searched - such a directory is the
* subject of this test. Any difference should cause
* the 2nd lookup atime to be >= the first, if it is ==, all is
* OK (atime is not required to be modified by the search, or
* both references may happen within the same clock tick), if the
* 2nd lookup atime is > the first, but not "too much" greater,
* just set it back, so the memcmp just below succeeds
* (assuming all else is OK).
*/
onesec.tv_sec = 1;
onesec.tv_nsec = 0;
timespecadd(&sb1.st_atimespec, &onesec, &atplus1);
if (timespeccmp(&sb2.st_atimespec, &sb1.st_atimespec, >) &&
timespeccmp(&sb2.st_atimespec, &atplus1, <))
sb2.st_atimespec = sb1.st_atimespec;
if (memcmp(&sb1, &sb2, sizeof(sb1)) != 0) {
printf("what\tsb1\t\tsb2\n");
/* check we can create directories */
snprintf(pb, sizeof(pb), "%s/dir", mountpath);
if (rump_sys_mkdir(pb, 0777) == -1)
atf_tc_fail_errno("mkdir");
if (rump_sys_stat(pb, &sb) == -1)
atf_tc_fail_errno("stat new directory");
/* check we can remove them and that it makes them unreachable */
if (rump_sys_rmdir(pb) == -1)
atf_tc_fail_errno("rmdir");
if (rump_sys_stat(pb, &sb) != -1 || errno != ENOENT)
atf_tc_fail("ENOENT expected from stat");
}
/* check we can create directories with one or more / appended */
snprintf(plain, sizeof(plain), "%s/dir%s", mountpath, addend);
snprintf(with_slash, sizeof(with_slash), "%s/dir/", mountpath);
if (rump_sys_mkdir(with_slash, 0777) == -1)
atf_tc_fail_errno("mkdir");
if (rump_sys_stat(plain, &sb) == -1)
atf_tc_fail_errno("stat new directory");
if (rump_sys_rmdir(plain) == -1)
atf_tc_fail_errno("rmdir");
if (rump_sys_stat(with_slash, &sb) != -1 || errno != ENOENT)
atf_tc_fail("ENOENT expected from stat");
}
/* takes forever with many files */
if (FSTYPE_MSDOS(tc))
nfiles /= 4;
RL(rump_sys_chdir(mp));
if (FSTYPE_SYSVBFS(tc)) {
/* fs doesn't support many files or subdirectories */
nfiles = 5;
} else {
/* msdosfs doesn't like many entries in the root directory */
RL(rump_sys_mkdir("subdir", 0777));
RL(rump_sys_chdir("subdir"));
}
/* create them */
#define TESTFN "testfile"
for (i = 0; i < nfiles; i++) {
int fd;
/* wipe them out */
for (i = 0; i < nfiles; i++) {
snprintf(buf, sizeof(buf), TESTFN "%d", i);
RLF(rump_sys_unlink(buf), "%s", buf);
}
#undef TESTFN
rump_sys_chdir("/");
}
/*
* Test creating files with one-character names using all possible
* character values. Failures to create the file are ignored as the
* characters allowed in file names vary by file system, but at least
* we can check that the fs does not crash, and if the file is
* successfully created, unlinking it should also succeed.
*/
static void
create_nonalphanum(const atf_tc_t *tc, const char *mp)
{
char buf[64];
int i;
RL(rump_sys_chdir(mp));
for (i = 0; i < 256; i++) {
int fd;
snprintf(buf, sizeof(buf), "%c", i);
fd = rump_sys_open(buf, O_RDWR|O_CREAT|O_EXCL, 0666);
if (fd == -1)
continue;
RLF(rump_sys_close(fd), "%d", fd);
RLF(rump_sys_unlink(buf), "%s", buf);
}
printf("\n");
rump_sys_chdir("/");
}
static void
create_nametoolong(const atf_tc_t *tc, const char *mp)
{
char *name;
int fd;
long val;
size_t len;
if (rump_sys_chdir(mp) == -1)
atf_tc_fail_errno("chdir mountpoint");
val = rump_sys_pathconf(".", _PC_NAME_MAX);
if (val == -1)
atf_tc_fail_errno("pathconf");
len = val + 1;
name = malloc(len+1);
if (name == NULL)
atf_tc_fail_errno("malloc");
memset(name, 'a', len);
*(name+len) = '\0';
val = rump_sys_pathconf(".", _PC_NO_TRUNC);
if (val == -1)
atf_tc_fail_errno("pathconf");
static void
rename_nametoolong(const atf_tc_t *tc, const char *mp)
{
char *name;
int res, fd;
long val;
size_t len;
if (FSTYPE_RUMPFS(tc))
atf_tc_skip("rename not supported by file system");
if (rump_sys_chdir(mp) == -1)
atf_tc_fail_errno("chdir mountpoint");
val = rump_sys_pathconf(".", _PC_NAME_MAX);
if (val == -1)
atf_tc_fail_errno("pathconf");
len = val + 1;
name = malloc(len+1);
if (name == NULL)
atf_tc_fail_errno("malloc");
memset(name, 'a', len);
*(name+len) = '\0';
fd = rump_sys_open("dummy", O_RDWR|O_CREAT, 0666);
if (fd == -1)
atf_tc_fail_errno("open");
if (rump_sys_close(fd) == -1)
atf_tc_fail_errno("close");
val = rump_sys_pathconf(".", _PC_NO_TRUNC);
if (val == -1)
atf_tc_fail_errno("pathconf");
res = rump_sys_rename("dummy", name);
if (val != 0 && (res != -1 || errno != ENAMETOOLONG))
atf_tc_fail_errno("rename");
if (val == 0 && rump_sys_unlink(name) == -1)
atf_tc_fail_errno("unlink");
free(name);
rump_sys_chdir("/");
}
/*
* Test creating a symlink whose length is "len" bytes, not including
* the terminating NUL.
*/
static void
symlink_len(const atf_tc_t *tc, const char *mp, size_t len)
{
char *buf;
int r;
static void
symlink_long(const atf_tc_t *tc, const char *mp)
{
/*
* Test lengths close to powers of two, as those are likely
* to be edge cases.
*/
size_t len;
int fuzz;
for (len = 2; len <= 65536; len *= 2) {
for (fuzz = -1; fuzz <= 1; fuzz++) {
symlink_len(tc, mp, len + fuzz);
}
}
}
/* Next, we fork and try to lock the same area */
RZ(rump_pub_lwproc_rfork(RUMP_RFCFDG));
lwp2 = rump_pub_lwproc_curlwp();
RL(fd2 = rump_sys_open(TESTFILE, O_RDWR, 0));
ATF_REQUIRE_ERRNO(EAGAIN, rump_sys_fcntl(fd2, F_SETLK, &l));
/* Switch back and unlock... */
rump_pub_lwproc_switch(lwp1);
l.l_type = F_UNLCK;
RL(rump_sys_fcntl(fd, F_SETLK, &l));
/* ... and try to lock again */
rump_pub_lwproc_switch(lwp2);
l.l_type = F_RDLCK | F_WRLCK;
RL(rump_sys_fcntl(fd2, F_SETLK, &l));
static int
flock_compare(const void *p, const void *q)
{
int a = ((const struct flock *)p)->l_start;
int b = ((const struct flock *)q)->l_start;
return a < b ? -1 : (a > b ? 1 : 0);
}
/*
* Find all locks set by fcntl_getlock_pids test
* using GETLK for a range [start, start+end], and,
* if there is a blocking lock, recursively find
* all locks to the left (toward the beginning of
* a file) and to the right of the lock.
* The function also understands "until end of file"
* convention when len==0.
*/
static unsigned int
fcntl_getlocks(int fildes, off_t start, off_t len,
struct flock *lock, struct flock *end)
{
unsigned int rv = 0;
const struct flock l = { start, len, 0, F_RDLCK, SEEK_SET };
if (lock == end)
return rv;
RL(rump_sys_fcntl(fildes, F_GETLK, &l));
if (l.l_type == F_UNLCK)
return rv;
*lock++ = l;
rv += 1;
ATF_REQUIRE(l.l_whence == SEEK_SET);
if (l.l_start > start) {
unsigned int n =
fcntl_getlocks(fildes, start, l.l_start - start, lock, end);
rv += n;
lock += n;
if (lock == end)
return rv;
}
if (l.l_len == 0) /* does l spans until the end? */
return rv;
if (len == 0) /* are we looking for locks until the end? */ {
rv += fcntl_getlocks(fildes, l.l_start + l.l_len, len, lock, end);
} else if (l.l_start + l.l_len < start + len) {
len -= l.l_start + l.l_len - start;
rv += fcntl_getlocks(fildes, l.l_start + l.l_len, len, lock, end);
}
/* Add extra element to make sure recursion does't stop at array end */
struct flock result[5];
/* Add 5th process */
int fd[5];
pid_t pid[5];
struct lwp *lwp[5];
unsigned int i, j;
const off_t sz = 8192;
int omode = 0755;
int oflags = O_RDWR | O_CREAT;
memcpy(expect, lock, sizeof(lock));
FSTEST_ENTER();
/*
* First, we create 4 processes and let each lock a range of the
* file. Note that the third and fourth processes lock in
* "reverse" order, i.e. the greater pid locks a range before
* the lesser pid.
* Then, we create 5th process which doesn't lock anything.
*/
for (i = 0; i < __arraycount(lwp); i++) {
RZ(rump_pub_lwproc_rfork(RUMP_RFCFDG));
/*
* In the context of each process, recursively find all locks
* that would block the current process. Processes 1-4 don't
* see their own lock, we insert it to simplify checks.
* Process 5 sees all 4 locks.
*/
for (i = 0; i < __arraycount(lwp); i++) {
unsigned int nlocks;
/*
* Release processes. This also releases the fds and locks
* making fs unmount possible
*/
for (i = 0; i < __arraycount(lwp); i++) {
rump_pub_lwproc_switch(lwp[i]);
rump_pub_lwproc_releaselwp();
}
FSTEST_EXIT();
}
static void
access_simple(const atf_tc_t *tc, const char *mp)
{
int fd;
int tmode;
ATF_TC_FSAPPLY(lookup_simple, "simple lookup (./.. on root)");
ATF_TC_FSAPPLY(lookup_complex, "lookup of non-dot entries");
ATF_TC_FSAPPLY(dir_simple, "mkdir/rmdir");
ATF_TC_FSAPPLY(dir_slash, "mkdir with appended slash");
ATF_TC_FSAPPLY(dir_2slash, "mkdir with two slashes appended");
ATF_TC_FSAPPLY(dir_3slash, "mkdir with three slashes appended");
ATF_TC_FSAPPLY(dir_notempty, "non-empty directories cannot be removed");
ATF_TC_FSAPPLY(dir_rmdirdotdot, "remove .. and try to cd out (PR kern/44657)");
ATF_TC_FSAPPLY(rename_dir, "exercise various directory renaming ops "
"(PR kern/44288)");
ATF_TC_FSAPPLY(rename_dotdot, "rename dir .. (PR kern/43617)");
ATF_TC_FSAPPLY(rename_reg_nodir, "rename regular files, no subdirectories");
ATF_TC_FSAPPLY(create_nametoolong, "create file with name too long");
ATF_TC_FSAPPLY(create_exist, "create with O_EXCL");
ATF_TC_FSAPPLY(rename_nametoolong, "rename to file with name too long");
ATF_TC_FSAPPLY(symlink_zerolen, "symlink with target of length 0");
ATF_TC_FSAPPLY(symlink_long, "symlink with target of length > 0");
ATF_TC_FSAPPLY(symlink_root, "symlink to root directory");
ATF_TC_FSAPPLY(attrs, "check setting attributes works");
ATF_TC_FSAPPLY(fcntl_lock, "check fcntl F_SETLK");
ATF_TC_FSAPPLY(fcntl_getlock_pids,"fcntl F_GETLK w/ many procs, PR kern/44494");
ATF_TC_FSAPPLY(access_simple, "access(2)");
ATF_TC_FSAPPLY(read_directory, "read(2) on directories");
ATF_TC_FSAPPLY(lstat_symlink, "lstat(2) values for symbolic links");