1 /*-
2  * Copyright (c) 2008 Peter Holm <pho@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  */
27 
28 /* Test lockf(3) with overlapping ranges */
29 
30 /* Provoked:
31 	lock order reversal:
32 	 1st 0xc50057a0 vnode interlock (vnode interlock) @ kern/kern_lockf.c:190
33 	 2nd 0xc14710e8 system map (system map) @ vm/vm_kern.c:296
34  */
35 
36 #include <sys/types.h>
37 
38 #include <err.h>
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <strings.h>
44 #include <unistd.h>
45 
46 #include "stress.h"
47 
48 static char file[128];
49 static int fd;
50 static int freespace;
51 
52 int
53 setup(int nb)
54 {
55 	int64_t bl;
56 	int64_t in;
57 	int64_t reserve_bl;
58 	int64_t reserve_in;
59 	int i;
60 	char buf[1024];
61 
62 	if (nb == 0) {
63 		getdf(&bl, &in);
64 
65 		/* Resource requirements: */
66 		reserve_in =       1 * op->incarnations;
67 		reserve_bl = 1081344 * op->incarnations;
68 		freespace = (reserve_bl <= bl && reserve_in <= in);
69 		if (!freespace)
70 			reserve_bl = reserve_in = 0;
71 
72 		if (op->verbose > 1)
73 			printf("lockf2(incarnations=%d). Free(%jdk, %jd), reserve(%jdk, %jd)\n",
74 			    op->incarnations, bl/1024, in, reserve_bl/1024, reserve_in);
75 		reservedf(reserve_bl, reserve_in);
76 		putval(freespace);
77 	} else {
78 		freespace = getval();
79 	}
80 	if (!freespace)
81 		exit (0);
82 
83 	sprintf(file, "lockf.%d", getpid());
84 	if ((fd = open(file,O_CREAT | O_TRUNC | O_RDWR, 0600)) == -1)
85 		err(1, "creat(%s)", file);
86 	bzero(buf, sizeof(buf));
87 	for (i = 0; i < 1024; i++)
88 		if (write(fd, &buf, sizeof(buf)) != sizeof(buf))
89 			err(1, "write");
90 	close(fd);
91         return (0);
92 }
93 
94 void
95 cleanup(void)
96 {
97 	unlink(file);
98 }
99 
100 int
101 test(void)
102 {
103 	off_t pos;
104 	off_t size;
105 	int i, r;
106 
107 	if ((fd = open(file, O_RDWR, 0600)) == -1)
108 		err(1, "open(%s)", file);
109 
110 	for (i = 0; i < 1024 && done_testing == 0; i++) {
111 		pos = random_int(0, 1024 * 1024 - 1);
112 		if (lseek(fd, pos, SEEK_SET) == -1)
113 			err(1, "lseek");
114 		size = random_int(1, 1024 * 1024 - pos);
115 		if (size > 64)
116 			size = 64;
117 		do {
118 			r = lockf(fd, F_LOCK, size);
119 		} while (r == -1 && errno == EINTR);
120 		if (r == -1)
121 			err(1, "lockf(%s, F_LOCK)", file);
122 		size = random_int(1, size);
123 		if (lockf(fd, F_ULOCK, size) == -1)
124 			err(1, "lockf(%s, F_ULOCK)", file);
125 
126 	}
127 	close(fd);
128 
129         return (0);
130 }
131