1272343Sngie/*	$NetBSD: t_full.c,v 1.8 2013/03/16 05:45:37 jmmv Exp $	*/
2272343Sngie
3272343Sngie/*-
4272343Sngie * Copyright (c) 2010 The NetBSD Foundation, Inc.
5272343Sngie * All rights reserved.
6272343Sngie *
7272343Sngie * Redistribution and use in source and binary forms, with or without
8272343Sngie * modification, are permitted provided that the following conditions
9272343Sngie * are met:
10272343Sngie * 1. Redistributions of source code must retain the above copyright
11272343Sngie *    notice, this list of conditions and the following disclaimer.
12272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
13272343Sngie *    notice, this list of conditions and the following disclaimer in the
14272343Sngie *    documentation and/or other materials provided with the distribution.
15272343Sngie *
16272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19272343Sngie * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26272343Sngie * POSSIBILITY OF SUCH DAMAGE.
27272343Sngie */
28272343Sngie
29272343Sngie#include <sys/stat.h>
30272343Sngie#include <sys/statvfs.h>
31272343Sngie
32272343Sngie#include <atf-c.h>
33272343Sngie#include <fcntl.h>
34272343Sngie#include <libgen.h>
35272343Sngie#include <stdlib.h>
36272343Sngie#include <unistd.h>
37272343Sngie
38272343Sngie#include <rump/rump_syscalls.h>
39272343Sngie#include <rump/rump.h>
40272343Sngie
41272343Sngie#include "../common/h_fsmacros.h"
42272343Sngie#include "../../h_macros.h"
43272343Sngie
44272343Sngie/*
45272343Sngie * Write this much over the image size.  This is to force an NFS commit,
46272343Sngie * since we might just stuff data into the cache and miss the problem.
47272343Sngie */
48272343Sngie#define NFSBONUS (1<<16)
49272343Sngie
50272343Sngiestatic void
51272343Sngiefillfs(const atf_tc_t *tc, const char *mp)
52272343Sngie{
53272343Sngie	char buf[8192];
54272343Sngie	size_t written;
55272343Sngie	ssize_t n = 0; /* xxxgcc */
56272343Sngie	size_t bonus;
57272343Sngie	int fd, i = 0;
58272343Sngie
59272343Sngie	if (FSTYPE_P2K_FFS(tc) || FSTYPE_PUFFS(tc) || FSTYPE_RUMPFS(tc)) {
60272343Sngie		atf_tc_skip("fs does not support explicit block allocation "
61272343Sngie		    "(GOP_ALLOC)");
62272343Sngie	}
63272343Sngie
64272343Sngie	bonus = 0;
65272343Sngie	if (FSTYPE_NFS(tc))
66272343Sngie		bonus = NFSBONUS;
67272343Sngie
68272343Sngie	if (rump_sys_chdir(mp) == -1)
69272343Sngie		atf_tc_fail_errno("chdir mountpoint");
70272343Sngie	fd = rump_sys_open("afile", O_CREAT | O_RDWR);
71272343Sngie	if (fd == -1)
72272343Sngie		atf_tc_fail_errno("create file");
73272343Sngie
74272343Sngie	for (written = 0; written < FSTEST_IMGSIZE + bonus; written +=n) {
75272343Sngie		memset(buf, i++, sizeof(buf)); /* known garbage */
76272343Sngie		n = rump_sys_write(fd, buf, sizeof(buf));
77272343Sngie		if (n == -1)
78272343Sngie			break;
79272343Sngie	}
80272343Sngie	if (FSTYPE_ZFS(tc))
81272343Sngie		atf_tc_expect_fail("PR kern/47656: Test known to be broken");
82272343Sngie	if (n == -1) {
83272343Sngie		if (errno != ENOSPC)
84272343Sngie			atf_tc_fail_errno("write");
85272343Sngie	} else {
86272343Sngie		atf_tc_fail("filled file system over size limit");
87272343Sngie	}
88272343Sngie
89272343Sngie	rump_sys_close(fd);
90272343Sngie	rump_sys_chdir("/");
91272343Sngie}
92272343Sngie
93272343SngieATF_TC_FSAPPLY(fillfs, "fills file system, expects ENOSPC");
94272343Sngie
95272343SngieATF_TP_ADD_TCS(tp)
96272343Sngie{
97272343Sngie
98272343Sngie	ATF_TP_FSAPPLY(fillfs);
99272343Sngie
100272343Sngie	return atf_no_error();
101272343Sngie}
102