Subject: Re: race in mkdir(1)
To: <>
From: David Laight <david@l8s.co.uk>
List: tech-userlevel
Date: 11/24/2002 21:38:11
On Sun, Nov 24, 2002 at 08:21:03AM -0800, Chuck Silvers wrote:
> I do build.sh with "-j 8", and occasionally during the "make obj" pass
> mkdir errors out with "File exists".  this is due to a race between
> multiple "mkdir -p" operations.  in mkdir.c:mkpath() there's
> 
> 		if (stat(path, &sb)) {
> 			if (errno != ENOENT ||
> 			    mkdir(path, done ? mode : dir_mode)) {
> 				warn("%s", path);
> 				return (-1);
> 			}
> 
> 
> if another process creates the directory between the stat() and the mkdir(),
> then a spurious error is returned.

You don't actually (probably) need two processes.
I've seen mkdir fail EEXIST on an NFS filessystem when the response
message gets timed out and the request retransmitted.
Was fairly repeatable on the setup I had; when the mkdir required
a flash erase and perturbed the normal timeout/retry code.
(this wasn't netbsd, the nfs client might do something very
clever...)

> does anyone see anything wrong with the attached patch?

saves some namei calls as well :-)

	David

-- 
David Laight: david@l8s.co.uk