Path: blob/main/tests/sys/cddl/zfs/bin/dir_rd_update.c
39536 views
/*1* CDDL HEADER START2*3* The contents of this file are subject to the terms of the4* Common Development and Distribution License (the "License").5* You may not use this file except in compliance with the License.6*7* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE8* or http://www.opensolaris.org/os/licensing.9* See the License for the specific language governing permissions10* and limitations under the License.11*12* When distributing Covered Code, include this CDDL HEADER in each13* file and include the License file at usr/src/OPENSOLARIS.LICENSE.14* If applicable, add the following below this CDDL HEADER, with the15* fields enclosed by brackets "[]" replaced with your own identifying16* information: Portions Copyright [yyyy] [name of copyright owner]17*18* CDDL HEADER END19*/2021/*22* Copyright 2007 Sun Microsystems, Inc. All rights reserved.23* Use is subject to license terms.24*/252627/*28* Assertion:29*30* A read operation and directory update operation performed31* concurrently on the same directory can lead to deadlock32* on a UFS logging file system, but not on a ZFS file system.33*/3435#include <sys/types.h>36#include <sys/stat.h>37#include <errno.h>38#include <fcntl.h>39#include <string.h>40#include <stdio.h>41#include <stdlib.h>42#include <unistd.h>43#define TMP_DIR /tmp4445static char dirpath[256];4647int48main(int argc, char **argv)49{50char *cp1 = "";51int i = 0;52int ret = 0;53int testdd = 0;54pid_t pid;55static const int op_num = 5;5657if (argc == 1) {58(void) printf("Usage: %s <mount point>\n", argv[0]);59exit(-1);60}61for (i = 0; i < 256; i++) {62dirpath[i] = 0;63}6465cp1 = argv[1];66(void) strcpy(&dirpath[0], (const char *)cp1);67(void) strcat(&dirpath[strlen(dirpath)], "TMP_DIR");6869ret = mkdir(dirpath, 0777);70if (ret != 0) {71if (errno != EEXIST) {72(void) printf(73"%s: mkdir(<%s>, 0777) failed: errno (decimal)=%d\n",74argv[0], dirpath, errno);75exit(-1);76}77}78testdd = open(dirpath, O_RDONLY|O_SYNC);79if (testdd < 0) {80(void) printf(81"%s: open(<%s>, O_RDONLY|O_SYNC) failed: errno (decimal)=%d\n",82argv[0], dirpath, errno);83exit(-1);84} else {85(void) close(testdd);86}87pid = fork();88if (pid > 0) {89int fd = open(dirpath, O_RDONLY|O_SYNC);90char buf[16];91int rdret;92int j = 0;9394while (j < op_num) {95(void) sleep(1);96rdret = read(fd, buf, 16);97if (rdret == -1) {98(void) printf("readdir failed");99}100j++;101}102} else if (pid == 0) {103int fd = open(dirpath, O_RDONLY);104int chownret;105int k = 0;106107while (k < op_num) {108(void) sleep(1);109chownret = fchown(fd, 0, 0);110if (chownret == -1) {111(void) printf("chown failed");112}113114k++;115}116}117118return (0);119}120121122