xref: /freebsd/tests/sys/cddl/zfs/bin/readmmap.c (revision ac00d4d59b18a76c6148ca5d7439bb446d38da5c)
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  */
212fae26bdSAlan Somers 
222fae26bdSAlan Somers /*
232fae26bdSAlan Somers  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
242fae26bdSAlan Somers  * Use is subject to license terms.
252fae26bdSAlan Somers  */
262fae26bdSAlan Somers 
272fae26bdSAlan Somers 
282fae26bdSAlan Somers /*
292fae26bdSAlan Somers  * --------------------------------------------------------------
302fae26bdSAlan Somers  *	BugId 5047993 : Getting bad read data.
312fae26bdSAlan Somers  *
322fae26bdSAlan Somers  *	Usage: readmmap <filename>
332fae26bdSAlan Somers  *
342fae26bdSAlan Somers  *	where:
352fae26bdSAlan Somers  *		filename is an absolute path to the file name.
362fae26bdSAlan Somers  *
372fae26bdSAlan Somers  *	Return values:
382fae26bdSAlan Somers  *		1 : error
392fae26bdSAlan Somers  *		0 : no errors
402fae26bdSAlan Somers  * --------------------------------------------------------------
412fae26bdSAlan Somers  */
422fae26bdSAlan Somers #include <stdio.h>
432fae26bdSAlan Somers #include <stdlib.h>
44*47be4845SDimitry Andric #include <time.h>
452fae26bdSAlan Somers #include <unistd.h>
462fae26bdSAlan Somers #include <fcntl.h>
472fae26bdSAlan Somers #include <errno.h>
482fae26bdSAlan Somers #include <sys/mman.h>
492fae26bdSAlan Somers 
502fae26bdSAlan Somers int
main(int argc,char ** argv)512fae26bdSAlan Somers main(int argc, char **argv)
522fae26bdSAlan Somers {
532fae26bdSAlan Somers 	char *filename = "badfile";
542fae26bdSAlan Somers 	size_t size = 4395;
552fae26bdSAlan Somers 	size_t idx = 0;
562fae26bdSAlan Somers 	char *buf = NULL;
572fae26bdSAlan Somers 	char *map = NULL;
582fae26bdSAlan Somers 	int fd = -1, bytes, retval = 0;
592fae26bdSAlan Somers 	unsigned seed;
602fae26bdSAlan Somers 
612fae26bdSAlan Somers 	if (argc < 2 || optind == argc) {
622fae26bdSAlan Somers 		(void) fprintf(stderr,
632fae26bdSAlan Somers 		    "usage: %s <file name>\n", argv[0]);
642fae26bdSAlan Somers 		exit(1);
652fae26bdSAlan Somers 	}
662fae26bdSAlan Somers 
672fae26bdSAlan Somers 	if ((buf = calloc(1, size)) == NULL) {
682fae26bdSAlan Somers 		perror("calloc");
692fae26bdSAlan Somers 		exit(1);
702fae26bdSAlan Somers 	}
712fae26bdSAlan Somers 
722fae26bdSAlan Somers 	filename = argv[optind];
732fae26bdSAlan Somers 
742fae26bdSAlan Somers 	(void) remove(filename);
752fae26bdSAlan Somers 
762fae26bdSAlan Somers 	fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0666);
772fae26bdSAlan Somers 	if (fd == -1) {
782fae26bdSAlan Somers 		perror("open to create");
792fae26bdSAlan Somers 		retval = 1;
802fae26bdSAlan Somers 		goto end;
812fae26bdSAlan Somers 	}
822fae26bdSAlan Somers 
832fae26bdSAlan Somers 	bytes = write(fd, buf, size);
842fae26bdSAlan Somers 	if (bytes != size) {
85f27fb06cSEd Maste 		(void) printf("short write: %d != %zu\n", bytes, size);
862fae26bdSAlan Somers 		retval = 1;
872fae26bdSAlan Somers 		goto end;
882fae26bdSAlan Somers 	}
892fae26bdSAlan Somers 
902fae26bdSAlan Somers 	map = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
912fae26bdSAlan Somers 	if (map == MAP_FAILED) {
922fae26bdSAlan Somers 		perror("mmap");
932fae26bdSAlan Somers 		retval = 1;
942fae26bdSAlan Somers 		goto end;
952fae26bdSAlan Somers 	}
962fae26bdSAlan Somers 	seed = time(NULL);
972fae26bdSAlan Somers 	srandom(seed);
982fae26bdSAlan Somers 
992fae26bdSAlan Somers 	idx = random() % size;
1002fae26bdSAlan Somers 	map[idx] = 1;
1012fae26bdSAlan Somers 
1022fae26bdSAlan Somers 	if (msync(map, size, MS_SYNC) != 0) {
1032fae26bdSAlan Somers 		perror("msync");
1042fae26bdSAlan Somers 		retval = 1;
1052fae26bdSAlan Somers 		goto end;
1062fae26bdSAlan Somers 	}
1072fae26bdSAlan Somers 
1082fae26bdSAlan Somers 	if (munmap(map, size) != 0) {
1092fae26bdSAlan Somers 		perror("munmap");
1102fae26bdSAlan Somers 		retval = 1;
1112fae26bdSAlan Somers 		goto end;
1122fae26bdSAlan Somers 	}
1132fae26bdSAlan Somers 
1142fae26bdSAlan Somers 	bytes = pread(fd, buf, size, 0);
1152fae26bdSAlan Somers 	if (bytes != size) {
116f27fb06cSEd Maste 		(void) printf("short read: %d != %zu\n", bytes, size);
1172fae26bdSAlan Somers 		retval = 1;
1182fae26bdSAlan Somers 		goto end;
1192fae26bdSAlan Somers 	}
1202fae26bdSAlan Somers 
1212fae26bdSAlan Somers 	if (buf[idx] != 1) {
1222fae26bdSAlan Somers 		(void) printf(
123f27fb06cSEd Maste 		    "bad data from read!  got buf[%zu]=%d, expected 1\n",
1242fae26bdSAlan Somers 		    idx, buf[idx]);
1252fae26bdSAlan Somers 		retval = 1;
1262fae26bdSAlan Somers 		goto end;
1272fae26bdSAlan Somers 	}
1282fae26bdSAlan Somers 
129f27fb06cSEd Maste 	(void) printf("good data from read: buf[%zu]=1\n", idx);
1302fae26bdSAlan Somers end:
1312fae26bdSAlan Somers 	if (fd != -1) {
1322fae26bdSAlan Somers 		(void) close(fd);
1332fae26bdSAlan Somers 	}
1342fae26bdSAlan Somers 	if (buf != NULL) {
1352fae26bdSAlan Somers 		free(buf);
1362fae26bdSAlan Somers 	}
1372fae26bdSAlan Somers 
1382fae26bdSAlan Somers 	return (retval);
1392fae26bdSAlan Somers }
140