xref: /freebsd/tests/sys/cddl/zfs/bin/readmmap.c (revision f27fb06cadc6a8e75ebebd3b60c8a9dc9ae1b833)
12fae26bdSAlan Somers /*
22fae26bdSAlan Somers  * CDDL HEADER START
32fae26bdSAlan Somers  *
42fae26bdSAlan Somers  * The contents of this file are subject to the terms of the
52fae26bdSAlan Somers  * Common Development and Distribution License (the "License").
62fae26bdSAlan Somers  * You may not use this file except in compliance with the License.
72fae26bdSAlan Somers  *
82fae26bdSAlan Somers  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
92fae26bdSAlan Somers  * or http://www.opensolaris.org/os/licensing.
102fae26bdSAlan Somers  * See the License for the specific language governing permissions
112fae26bdSAlan Somers  * and limitations under the License.
122fae26bdSAlan Somers  *
132fae26bdSAlan Somers  * When distributing Covered Code, include this CDDL HEADER in each
142fae26bdSAlan Somers  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
152fae26bdSAlan Somers  * If applicable, add the following below this CDDL HEADER, with the
162fae26bdSAlan Somers  * fields enclosed by brackets "[]" replaced with your own identifying
172fae26bdSAlan Somers  * information: Portions Copyright [yyyy] [name of copyright owner]
182fae26bdSAlan Somers  *
192fae26bdSAlan Somers  * CDDL HEADER END
202fae26bdSAlan Somers  * $FreeBSD$
212fae26bdSAlan Somers  */
222fae26bdSAlan Somers 
232fae26bdSAlan Somers /*
242fae26bdSAlan Somers  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
252fae26bdSAlan Somers  * Use is subject to license terms.
262fae26bdSAlan Somers  */
272fae26bdSAlan Somers 
282fae26bdSAlan Somers #pragma ident	"@(#)readmmap.c	1.4	07/05/25 SMI"
292fae26bdSAlan Somers 
302fae26bdSAlan Somers /*
312fae26bdSAlan Somers  * --------------------------------------------------------------
322fae26bdSAlan Somers  *	BugId 5047993 : Getting bad read data.
332fae26bdSAlan Somers  *
342fae26bdSAlan Somers  *	Usage: readmmap <filename>
352fae26bdSAlan Somers  *
362fae26bdSAlan Somers  *	where:
372fae26bdSAlan Somers  *		filename is an absolute path to the file name.
382fae26bdSAlan Somers  *
392fae26bdSAlan Somers  *	Return values:
402fae26bdSAlan Somers  *		1 : error
412fae26bdSAlan Somers  *		0 : no errors
422fae26bdSAlan Somers  * --------------------------------------------------------------
432fae26bdSAlan Somers  */
442fae26bdSAlan Somers #include <stdio.h>
452fae26bdSAlan Somers #include <stdlib.h>
462fae26bdSAlan Somers #include <unistd.h>
472fae26bdSAlan Somers #include <fcntl.h>
482fae26bdSAlan Somers #include <errno.h>
492fae26bdSAlan Somers #include <sys/mman.h>
502fae26bdSAlan Somers 
512fae26bdSAlan Somers int
522fae26bdSAlan Somers main(int argc, char **argv)
532fae26bdSAlan Somers {
542fae26bdSAlan Somers 	char *filename = "badfile";
552fae26bdSAlan Somers 	size_t size = 4395;
562fae26bdSAlan Somers 	size_t idx = 0;
572fae26bdSAlan Somers 	char *buf = NULL;
582fae26bdSAlan Somers 	char *map = NULL;
592fae26bdSAlan Somers 	int fd = -1, bytes, retval = 0;
602fae26bdSAlan Somers 	unsigned seed;
612fae26bdSAlan Somers 
622fae26bdSAlan Somers 	if (argc < 2 || optind == argc) {
632fae26bdSAlan Somers 		(void) fprintf(stderr,
642fae26bdSAlan Somers 		    "usage: %s <file name>\n", argv[0]);
652fae26bdSAlan Somers 		exit(1);
662fae26bdSAlan Somers 	}
672fae26bdSAlan Somers 
682fae26bdSAlan Somers 	if ((buf = calloc(1, size)) == NULL) {
692fae26bdSAlan Somers 		perror("calloc");
702fae26bdSAlan Somers 		exit(1);
712fae26bdSAlan Somers 	}
722fae26bdSAlan Somers 
732fae26bdSAlan Somers 	filename = argv[optind];
742fae26bdSAlan Somers 
752fae26bdSAlan Somers 	(void) remove(filename);
762fae26bdSAlan Somers 
772fae26bdSAlan Somers 	fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0666);
782fae26bdSAlan Somers 	if (fd == -1) {
792fae26bdSAlan Somers 		perror("open to create");
802fae26bdSAlan Somers 		retval = 1;
812fae26bdSAlan Somers 		goto end;
822fae26bdSAlan Somers 	}
832fae26bdSAlan Somers 
842fae26bdSAlan Somers 	bytes = write(fd, buf, size);
852fae26bdSAlan Somers 	if (bytes != size) {
86*f27fb06cSEd Maste 		(void) printf("short write: %d != %zu\n", bytes, size);
872fae26bdSAlan Somers 		retval = 1;
882fae26bdSAlan Somers 		goto end;
892fae26bdSAlan Somers 	}
902fae26bdSAlan Somers 
912fae26bdSAlan Somers 	map = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
922fae26bdSAlan Somers 	if (map == MAP_FAILED) {
932fae26bdSAlan Somers 		perror("mmap");
942fae26bdSAlan Somers 		retval = 1;
952fae26bdSAlan Somers 		goto end;
962fae26bdSAlan Somers 	}
972fae26bdSAlan Somers 	seed = time(NULL);
982fae26bdSAlan Somers 	srandom(seed);
992fae26bdSAlan Somers 
1002fae26bdSAlan Somers 	idx = random() % size;
1012fae26bdSAlan Somers 	map[idx] = 1;
1022fae26bdSAlan Somers 
1032fae26bdSAlan Somers 	if (msync(map, size, MS_SYNC) != 0) {
1042fae26bdSAlan Somers 		perror("msync");
1052fae26bdSAlan Somers 		retval = 1;
1062fae26bdSAlan Somers 		goto end;
1072fae26bdSAlan Somers 	}
1082fae26bdSAlan Somers 
1092fae26bdSAlan Somers 	if (munmap(map, size) != 0) {
1102fae26bdSAlan Somers 		perror("munmap");
1112fae26bdSAlan Somers 		retval = 1;
1122fae26bdSAlan Somers 		goto end;
1132fae26bdSAlan Somers 	}
1142fae26bdSAlan Somers 
1152fae26bdSAlan Somers 	bytes = pread(fd, buf, size, 0);
1162fae26bdSAlan Somers 	if (bytes != size) {
117*f27fb06cSEd Maste 		(void) printf("short read: %d != %zu\n", bytes, size);
1182fae26bdSAlan Somers 		retval = 1;
1192fae26bdSAlan Somers 		goto end;
1202fae26bdSAlan Somers 	}
1212fae26bdSAlan Somers 
1222fae26bdSAlan Somers 	if (buf[idx] != 1) {
1232fae26bdSAlan Somers 		(void) printf(
124*f27fb06cSEd Maste 		    "bad data from read!  got buf[%zu]=%d, expected 1\n",
1252fae26bdSAlan Somers 		    idx, buf[idx]);
1262fae26bdSAlan Somers 		retval = 1;
1272fae26bdSAlan Somers 		goto end;
1282fae26bdSAlan Somers 	}
1292fae26bdSAlan Somers 
130*f27fb06cSEd Maste 	(void) printf("good data from read: buf[%zu]=1\n", idx);
1312fae26bdSAlan Somers end:
1322fae26bdSAlan Somers 	if (fd != -1) {
1332fae26bdSAlan Somers 		(void) close(fd);
1342fae26bdSAlan Somers 	}
1352fae26bdSAlan Somers 	if (buf != NULL) {
1362fae26bdSAlan Somers 		free(buf);
1372fae26bdSAlan Somers 	}
1382fae26bdSAlan Somers 
1392fae26bdSAlan Somers 	return (retval);
1402fae26bdSAlan Somers }
141