1 /* $NetBSD: t_rwtoro.c,v 1.1 2017/01/27 10:45:11 hannken Exp $ */
4 * Copyright (c) 2017 The NetBSD Foundation, Inc.
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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/types.h>
30 #include <sys/mount.h>
32 #include <sys/statvfs.h>
40 #include <rump/rump_syscalls.h>
41 #include <rump/rump.h>
43 #include <miscfs/nullfs/null.h>
44 #include <fs/tmpfs/tmpfs_args.h>
46 #include "../common/h_fsmacros.h"
47 #include "../../h_macros.h"
49 static const char *unsupported = "fs does not support r/o remount";
50 static char file_path[MAXPATHLEN];
54 * Remount the filesystem read-only and test errno.
55 * Skip filesystems that don't implement read-write -> read-only.
58 remount_ro(const atf_tc_t *tc, const char *mp, int expected_errno)
62 struct tmpfs_args tmpfs;
65 int mount_args_length;
69 atf_tc_skip("%s", unsupported);
71 /* Prepare mount arguments. */
72 RL(rump_sys_statvfs1(mp, &sbuf, ST_WAIT));
73 mount_args_length = sizeof(mount_args);
74 memset(&mount_args, 0, mount_args_length);
76 mount_args.tmpfs.ta_version = TMPFS_ARGS_VERSION;
77 mount_args_length = rump_sys_mount(sbuf.f_fstypename, mp, MNT_GETARGS,
78 &mount_args, mount_args_length);
79 ATF_CHECK(mount_args_length >= 0);
81 /* Remount and test result. */
82 error = rump_sys_mount(sbuf.f_fstypename, mp, MNT_UPDATE | MNT_RDONLY,
83 &mount_args, mount_args_length);
84 if (errno == EOPNOTSUPP)
85 atf_tc_skip("%s", unsupported);
86 if (expected_errno == 0)
87 ATF_CHECK(error == 0);
89 ATF_CHECK_ERRNO(expected_errno, error == -1);
93 open_file_ro(const char *prefix)
96 snprintf(file_path, sizeof(file_path), "%s/file", prefix);
97 RL(file_fd = rump_sys_open(file_path, O_CREAT | O_RDWR, 0777));
98 RL(rump_sys_close(file_fd));
99 RL(file_fd = rump_sys_open(file_path, O_RDONLY));
103 open_file_ro_unlink(const char *prefix)
106 snprintf(file_path, sizeof(file_path), "%s/file", prefix);
107 RL(file_fd = rump_sys_open(file_path, O_CREAT | O_RDWR, 0777));
108 RL(rump_sys_close(file_fd));
109 RL(file_fd = rump_sys_open(file_path, O_RDONLY));
110 RL(rump_sys_unlink(file_path));
114 open_file_rw(const char *prefix)
117 snprintf(file_path, sizeof(file_path), "%s/file", prefix);
118 RL(file_fd = rump_sys_open(file_path, O_CREAT | O_RDWR, 0777));
122 close_file(const char *unused)
125 RL(rump_sys_close(file_fd));
129 basic_test(const atf_tc_t *tc, const char *mp, int expected_errno,
130 bool use_layer, void (*pre)(const char *), void (*post)(const char *))
132 const char *null_mount = "/nullm";
133 struct null_args nargs;
136 RL(rump_sys_mkdir(null_mount, 0777));
137 memset(&nargs, 0, sizeof(nargs));
138 nargs.nulla_target = __UNCONST(mp);;
139 RL(rump_sys_mount(MOUNT_NULL, null_mount, 0,
140 &nargs, sizeof(nargs)));
143 (*pre)(use_layer ? null_mount : mp);
144 remount_ro(tc, mp, expected_errno);
146 (*post)(use_layer ? null_mount : mp);
148 RL(rump_sys_unmount(null_mount, 0));
152 noneopen(const atf_tc_t *tc, const char *mp)
155 basic_test(tc, mp, 0, false, NULL, NULL);
159 readopen(const atf_tc_t *tc, const char *mp)
162 basic_test(tc, mp, 0, false, open_file_ro, close_file);
166 writeopen(const atf_tc_t *tc, const char *mp)
169 basic_test(tc, mp, EBUSY, false, open_file_rw, close_file);
173 read_unlinked(const atf_tc_t *tc, const char *mp)
176 basic_test(tc, mp, EBUSY, false, open_file_ro_unlink, close_file);
180 layer_noneopen(const atf_tc_t *tc, const char *mp)
183 basic_test(tc, mp, 0, true, NULL, NULL);
187 layer_readopen(const atf_tc_t *tc, const char *mp)
190 basic_test(tc, mp, 0, true, open_file_ro, close_file);
194 layer_writeopen(const atf_tc_t *tc, const char *mp)
197 basic_test(tc, mp, EBUSY, true, open_file_rw, close_file);
201 layer_read_unlinked(const atf_tc_t *tc, const char *mp)
204 basic_test(tc, mp, EBUSY, true, open_file_ro_unlink, close_file);
207 ATF_TC_FSAPPLY(noneopen, "remount r/o with no file open");
208 ATF_TC_FSAPPLY(readopen, "remount r/o with file open for reading");
209 ATF_TC_FSAPPLY(writeopen, "remount r/o with file open for writing");
210 ATF_TC_FSAPPLY(read_unlinked,
211 "remount r/o with unlinked file open for reading");
212 ATF_TC_FSAPPLY(layer_noneopen, "remount r/o with no file open on layer");
213 ATF_TC_FSAPPLY(layer_readopen,
214 "remount r/o with file open for reading on layer");
215 ATF_TC_FSAPPLY(layer_writeopen,
216 "remount r/o with file open for writing on layer");
217 ATF_TC_FSAPPLY(layer_read_unlinked,
218 "remount r/o with unlinked file open for reading on layer");
223 ATF_TP_FSAPPLY(noneopen);
224 ATF_TP_FSAPPLY(readopen);
225 ATF_TP_FSAPPLY(writeopen);
226 ATF_TP_FSAPPLY(read_unlinked);
227 ATF_TP_FSAPPLY(layer_noneopen);
228 ATF_TP_FSAPPLY(layer_readopen);
229 ATF_TP_FSAPPLY(layer_writeopen);
230 ATF_TP_FSAPPLY(layer_read_unlinked);
232 return atf_no_error();