1*96c8483aSYuri Pankov /*
2*96c8483aSYuri Pankov  * This file and its contents are supplied under the terms of the
3*96c8483aSYuri Pankov  * Common Development and Distribution License ("CDDL"), version 1.0.
4*96c8483aSYuri Pankov  * You may only use this file in accordance with the terms of version
5*96c8483aSYuri Pankov  * 1.0 of the CDDL.
6*96c8483aSYuri Pankov  *
7*96c8483aSYuri Pankov  * A full copy of the text of the CDDL should have accompanied this
8*96c8483aSYuri Pankov  * source.  A copy of the CDDL is also available via the Internet at
9*96c8483aSYuri Pankov  * http://www.illumos.org/license/CDDL.
10*96c8483aSYuri Pankov  */
11*96c8483aSYuri Pankov 
12*96c8483aSYuri Pankov /*
13*96c8483aSYuri Pankov  * Copyright 2012 Jilin Xpd <jilinxpd@gmail.com>
14*96c8483aSYuri Pankov  * Copyright 2018 Nexenta Systems, Inc.
15*96c8483aSYuri Pankov  */
16*96c8483aSYuri Pankov 
17*96c8483aSYuri Pankov /*
18*96c8483aSYuri Pankov  * After close file but before munmap it, test if we can still write into
19*96c8483aSYuri Pankov  * mapped pages and the dirty pages are eventually synced to file,
20*96c8483aSYuri Pankov  * the result should be that we can do it as long as we dont munmap it.
21*96c8483aSYuri Pankov  * When userland attempts to close mapped file, smbfs will keep SMB FID
22*96c8483aSYuri Pankov  * alive if there are mapped pages(not unmapped yet), so the otW will stay
23*96c8483aSYuri Pankov  * open until last ref. to vnode goes away.
24*96c8483aSYuri Pankov  * This program tests if smbfs works as we said.
25*96c8483aSYuri Pankov  */
26*96c8483aSYuri Pankov 
27*96c8483aSYuri Pankov #include <sys/mman.h>
28*96c8483aSYuri Pankov #include <sys/types.h>
29*96c8483aSYuri Pankov #include <sys/stat.h>
30*96c8483aSYuri Pankov #include <fcntl.h>
31*96c8483aSYuri Pankov #include <stdio.h>
32*96c8483aSYuri Pankov #include <stdlib.h>
33*96c8483aSYuri Pankov #include <unistd.h>
34*96c8483aSYuri Pankov #include <string.h>
35*96c8483aSYuri Pankov #include <errno.h>
36*96c8483aSYuri Pankov 
37*96c8483aSYuri Pankov int
main(int argc,char ** argv)38*96c8483aSYuri Pankov main(int argc, char **argv)
39*96c8483aSYuri Pankov {
40*96c8483aSYuri Pankov 	char *file_addr;
41*96c8483aSYuri Pankov 	char *p;
42*96c8483aSYuri Pankov 	size_t filesize;
43*96c8483aSYuri Pankov 	size_t blksize;
44*96c8483aSYuri Pankov 	int fid;
45*96c8483aSYuri Pankov 	int i;
46*96c8483aSYuri Pankov 	char *c = "?#*%&";
47*96c8483aSYuri Pankov 
48*96c8483aSYuri Pankov 	if (argc != 2) {
49*96c8483aSYuri Pankov 		fprintf(stderr, "\tusage:\n\tclose_wr <filename>\n");
50*96c8483aSYuri Pankov 		return (1);
51*96c8483aSYuri Pankov 	}
52*96c8483aSYuri Pankov 
53*96c8483aSYuri Pankov 	/* open test file */
54*96c8483aSYuri Pankov 	fid = open(argv[1], O_RDWR | O_CREAT | O_TRUNC,
55*96c8483aSYuri Pankov 	    S_IRUSR | S_IWUSR | S_IROTH | S_IWOTH);
56*96c8483aSYuri Pankov 	if (fid == -1) {
57*96c8483aSYuri Pankov 		fprintf(stderr, "open %s error=%d\n", argv[1], errno);
58*96c8483aSYuri Pankov 		return (1);
59*96c8483aSYuri Pankov 	}
60*96c8483aSYuri Pankov 
61*96c8483aSYuri Pankov 	/* extend file */
62*96c8483aSYuri Pankov 	filesize = 64 * 1024;
63*96c8483aSYuri Pankov 	if (ftruncate(fid, filesize) == -1) {
64*96c8483aSYuri Pankov 		fprintf(stderr, "ftrunc %s error=%d\n", argv[1], errno);
65*96c8483aSYuri Pankov 		return (1);
66*96c8483aSYuri Pankov 	}
67*96c8483aSYuri Pankov 
68*96c8483aSYuri Pankov 	/* map file */
69*96c8483aSYuri Pankov 	file_addr = mmap(NULL, filesize,
70*96c8483aSYuri Pankov 	    PROT_READ | PROT_WRITE, MAP_SHARED, fid, 0);
71*96c8483aSYuri Pankov 	if (file_addr == MAP_FAILED) {
72*96c8483aSYuri Pankov 		fprintf(stderr, "mmap %s error=%d\n", argv[1], errno);
73*96c8483aSYuri Pankov 		return (1);
74*96c8483aSYuri Pankov 	}
75*96c8483aSYuri Pankov 
76*96c8483aSYuri Pankov 	/* erase file */
77*96c8483aSYuri Pankov 	memset(file_addr, 0, filesize);
78*96c8483aSYuri Pankov 
79*96c8483aSYuri Pankov 	/* close file here! */
80*96c8483aSYuri Pankov 	if (close(fid) == -1) {
81*96c8483aSYuri Pankov 		fprintf(stderr, "close %s error=%d\n", argv[1], errno);
82*96c8483aSYuri Pankov 		return (1);
83*96c8483aSYuri Pankov 	}
84*96c8483aSYuri Pankov 
85*96c8483aSYuri Pankov 	/*
86*96c8483aSYuri Pankov 	 * write somthing into mapped addr after close file,
87*96c8483aSYuri Pankov 	 * it should be ok before munmap
88*96c8483aSYuri Pankov 	 */
89*96c8483aSYuri Pankov 	blksize = filesize / 4;
90*96c8483aSYuri Pankov 	for (i = 0, p = file_addr; i < 4; i++, p += blksize) {
91*96c8483aSYuri Pankov 		memset(p, c[i], blksize);
92*96c8483aSYuri Pankov 	}
93*96c8483aSYuri Pankov 
94*96c8483aSYuri Pankov 	/* sync pages to file */
95*96c8483aSYuri Pankov 	if (msync(file_addr, filesize, MS_SYNC) == -1) {
96*96c8483aSYuri Pankov 		fprintf(stderr, "msync %s error=%d\n", argv[1], errno);
97*96c8483aSYuri Pankov 		return (1);
98*96c8483aSYuri Pankov 	}
99*96c8483aSYuri Pankov 
100*96c8483aSYuri Pankov 	/* unmap file */
101*96c8483aSYuri Pankov 	if (munmap(file_addr, filesize) == -1) {
102*96c8483aSYuri Pankov 		fprintf(stderr, "munmap %s error=%d\n", argv[1], errno);
103*96c8483aSYuri Pankov 		return (1);
104*96c8483aSYuri Pankov 	}
105*96c8483aSYuri Pankov 
106*96c8483aSYuri Pankov 	return (0);
107*96c8483aSYuri Pankov }