From b.candler@pobox.com  Tue Oct  7 05:36:49 2003
Return-Path: <b.candler@pobox.com>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id BF05B16A4B3
	for <FreeBSD-gnats-submit@freebsd.org>; Tue,  7 Oct 2003 05:36:49 -0700 (PDT)
Received: from internal.mail.uk.tiscali.com (internal.mail.uk.tiscali.com [212.74.96.51])
	by mx1.FreeBSD.org (Postfix) with ESMTP id 13F3B43FF9
	for <FreeBSD-gnats-submit@freebsd.org>; Tue,  7 Oct 2003 05:36:34 -0700 (PDT)
	(envelope-from b.candler@pobox.com)
Received: from [10.44.66.27] (helo=bloodhound.uk.tiscali.com)
	by internal.mail.uk.tiscali.com with esmtp (Exim 4.12)
	id 1A6r4r-0000ve-00
	for FreeBSD-gnats-submit@freebsd.org; Tue, 07 Oct 2003 13:36:33 +0100
Received: from telinco by bloodhound.uk.tiscali.com with local (Exim 4.12)
	id 1A6r4k-0008qR-00
	for FreeBSD-gnats-submit@freebsd.org; Tue, 07 Oct 2003 13:36:26 +0100
Message-Id: <E1A6r4k-0008qR-00@bloodhound.uk.tiscali.com>
Date: Tue, 07 Oct 2003 13:36:26 +0100
From: Brian Candler <B.Candler@pobox.com>
Reply-To: Brian Candler <B.Candler@pobox.com>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: NFS client readdir terminates prematurely if renaming files
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         57696
>Category:       kern
>Synopsis:       [nfs] NFS client readdir terminates prematurely if renaming files
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Tue Oct 07 05:40:14 PDT 2003
>Closed-Date:    
>Last-Modified:  Sun Jul 01 16:10:39 UTC 2012
>Originator:     B.Candler@pobox.com
>Release:        FreeBSD 4.8p3 i386
>Organization:
>Environment:

FreeBSD-4.7-RELEASE, FreeBSD-4.8p3

>Description:

If you have an opendir/readdir loop which rename()s files out of that
directory while the directory is being read, and the directory is mounted
over NFS, the readdir terminates prematurely - i.e. not all files are seen.

Problem verified with the following combinations:

   FreeBSD client - NetApp server:         problem [1]
   FreeBSD client - Solaris 2.8 server:    problem [1]
   FreeBSD client - FreeBSD server:        problem [2]
   FreeBSD client - Linux server:          problem [3]
   Linux client - Linux server:            no problem [3]
   Solaris 2.8 client - Netapp server:     no problem [4]

That seems to nail it as a FreeBSD NFS client issue. References:
[1] http://www.mail-archive.com/sqwebmail%40inter7.com/msg06643.html
[2] http://www.mail-archive.com/sqwebmail%40inter7.com/msg06644.html
[3] not yet appeared on archive, message from Stefan Kaltenbrunner
[4] http://www.mail-archive.com/sqwebmail%40inter7.com/msg06657.html

It's a problem in particular for Maildir messages, when moving files from
Maildir/new/* to Maildir/cur/*

>How-To-Repeat:

Run the following program with an NFS-mounted directory as the command-line
argument. The failure mode is:

    bash-2.05a# ./testnfs /na0/testdir
    Transferred 169 out of 200 files

This program also posted at reference [1] above.

/* Demonstrate problem with NFS readdir */

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h> /* for mkdir */
#include <sys/stat.h>  /* for mkdir */
#include <dirent.h>    /* opendir/readdir etc */

#define TESTSIZE 200
    
int main(int argc, char *argv[])
{
  int i;
  char fnbuf[1024], fnbuf2[1024];
  char *dir = argv[1];
  int count;
  DIR *dp;
  struct dirent *de;
  
  if (argc < 2 || !dir || !dir[0])
    dir = ".";

  sprintf(fnbuf, "%s/new", dir);
  mkdir(fnbuf, 0777);
  sprintf(fnbuf, "%s/cur", dir);
  mkdir(fnbuf, 0777);
    
  for (i=0; i<TESTSIZE; i++) {
    FILE *f;
    sprintf(fnbuf, "%s/new/MYTESTFILE%d", dir, i);
    f = fopen(fnbuf, "w");
    if (!f) { perror("fopen"); exit(1); }
    fprintf(f, "Some dummy content\n");
    fclose(f);
  }

  sprintf(fnbuf, "%s/new", dir);
  dp = opendir(fnbuf);
  if (!dp) { perror("opendir"); exit(1); }
  count = 0;
  while ((de = readdir(dp))) {
    if (de->d_name[0] == '.') continue;
    sprintf(fnbuf, "%s/new/%s", dir, de->d_name);
    sprintf(fnbuf2,"%s/cur/%s:2,S", dir, de->d_name);
    if (rename(fnbuf, fnbuf2) < 0) {
      perror("rename");
      fprintf(stderr, "(from %s to %s)\n", fnbuf, fnbuf2);
      continue;
    }
    count++;
  }  

  fprintf(stderr, "Transferred %d out of %d files\n", count, TESTSIZE);
  return count != TESTSIZE;
}


>Fix:

No idea. Workaround implemented in courier-imap is to opendir, readdir 20
items into array, closedir, process the 20 items, rinse and repeat.

>Release-Note:
>Audit-Trail:

From: Brian Candler <B.Candler@pobox.com>
To: FreeBSD-gnats-submit@FreeBSD.org, freebsd-bugs@FreeBSD.org
Cc:  
Subject: Re: kern/57696: NFS client readdir terminates prematurely if renaming files
Date: Tue, 7 Oct 2003 16:45:38 +0100

 The other reference is:
 [3] http://sourceforge.net/mailarchive/message.php?msg_id=6224555

From: Chris Shenton <Chris.Shenton@hq.nasa.gov>
To: freebsd-gnats-submit@FreeBSD.org
Cc:  
Subject: Re: kern/57696: NFS client readdir terminates prematurely if
 renaming files
Date: Tue, 07 Oct 2003 17:43:35 -0400

 Same problem confirmed on FreeBSD-4.9-PRERELEASE.
 
 Solaris 2.9 does NOT exhibit this problem.

From: Chris Shenton <chris@shenton.org>
To: freebsd-gnats-submit@FreeBSD.org
Cc: B.Candler@pobox.com
Subject: Re: kern/57696: NFS client readdir terminates prematurely if
 renaming files
Date: Tue, 07 Oct 2003 20:33:25 -0400

 I hope I'm doing the follow-up right; I don't know how to see
 follow-ups in GNATS.
 
 At work I tested a FreeBSD-4.9-PRERELEASE client against our netapp
 and found the same failure with the "nfstest" program Brian posted. A
 Solaris-2.9 client did not exhibit this problem.
 
 When I got home, I ran the test with a FreeBSD-5.1 client to NetApp,
 then with FreeBSD client to FreeBSD server.  Both failed the same
 way.  This would tend to indicate an NFS client problem in 4.x and
 5.x.
 
 
 NFS from FreeBSD-5.1-CURRENT client to NetApp-6.3.2 fails:
 
   NetApp> version
   NetApp Release 6.3.2: Tue Mar 18 14:54:05 PST 2003
 
   chris@PECTOPAH<111> time ./nfstest /netapp/chris2/nfstestdir
   Transferred 169 out of 200 files
   0.016u 0.177s 0:00.66 27.2%	5+188k 0+200io 0pf+0w
 
   chris@PECTOPAH<112> uname -a
   FreeBSD PECTOPAH.shenton.org 5.1-CURRENT FreeBSD 5.1-CURRENT #10: Wed Sep 17 11:57:38 EDT 2003     root@PECTOPAH.shenton.org:/usr/obj/usr/src/sys/PECTOPAH  i386
 
 
 NFS from (diskless) FreeBSD-5.1-CURRENT client to FreeBSD-5.1-CURRENT
 NFS server also fails:
 
   chris@Kitchen<107> time ./nfstest kitchen
   Transferred 169 out of 200 files
   0.016u 0.937s 0:01.99 47.2%     4+174k 0+200io 0pf+0w
 
   chris@Kitchen<108> uname -a
   FreeBSD Kitchen.shenton.org 5.1-CURRENT FreeBSD 5.1-CURRENT #13: Sat Oct  4 14:21:23 EDT 2003     chris@PECTOPAH.shenton.org:/usr/obj/usr/src/sys/PECTOPAH  i386
 
 

From: Brian Candler <B.Candler@pobox.com>
To: Chris Shenton <chris@shenton.org>
Cc: freebsd-gnats-submit@FreeBSD.org
Subject: Re: kern/57696: NFS client readdir terminates prematurely if renaming files
Date: Wed, 8 Oct 2003 17:29:42 +0100

 On Tue, Oct 07, 2003 at 08:33:25PM -0400, Chris Shenton wrote:
 > At work I tested a FreeBSD-4.9-PRERELEASE client against our netapp
 > and found the same failure with the "nfstest" program Brian posted. A
 > Solaris-2.9 client did not exhibit this problem.
 
 Apparently a "bad cookie" message is logged for each failure event, and the
 following related thread has been pointed out to me:
 http://lists.freebsd.org/pipermail/freebsd-current/2003-August/008402.html
 
 The implication of what is written there is that readdir() is entitled to
 fail if the directory has been changed underneath it.
 
 I don't know how Linux and Solaris cope with this: do they take an in-memory
 snapshot of the whole directory at the client side, which readdir() then
 traverses? Or do they just blindly continue to traverse a directory which
 they know has changed?
 
 Regards,
 
 Brian.
State-Changed-From-To: open->analyzed 
State-Changed-By: kris 
State-Changed-When: Sat Nov 15 13:49:22 PST 2003 
State-Changed-Why:  
See also kern/26142.  This is a known problem in the nfs code; 
the ufs code contains code to deal with this situation.  Bug peter 
for more details :) 

http://www.freebsd.org/cgi/query-pr.cgi?pr=57696 
Responsible-Changed-From-To: freebsd-bugs->cel 
Responsible-Changed-By: cel 
Responsible-Changed-When: Wed May 24 19:08:06 UTC 2006 
Responsible-Changed-Why:  
Linux and Solaris watch the mtime of the directory while it's being 
read.  If the mtime changes, those clients know that the directory 
itself has changed, and effectively re-read the directory contents 
from the beginning. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=57696 
Responsible-Changed-From-To: cel->freebsd-bugs 
Responsible-Changed-By: cel 
Responsible-Changed-When: Mon Mar 12 15:21:55 UTC 2007 
Responsible-Changed-Why:  
Back to the public pool. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=57696 

From: Renaud Chaput <renchap@cocoa-x.com>
To: bug-followup@FreeBSD.org, B.Candler@pobox.com
Cc:  
Subject: Re: kern/57696: [nfs] NFS client readdir terminates prematurely if
 renaming files
Date: Thu, 10 Dec 2009 15:17:43 +0100

 Hi,
 
 I can reproduce this bug with FreeBSD 8.0 :
 % uname -a
 FreeBSD vty-testhp1 8.0-RELEASE FreeBSD 8.0-RELEASE #0: Sat Nov 21 15:02:08 UTC 2009 root@mason.cse.buffalo.edu:/usr/obj/usr/src/sys/GENERIC  amd64
 
 Steps to reproduce : install benchmark/bonnie++ port, and launch bonnie++ inside a NFS mount.
 
 Is it any plans to fix this ?
 
State-Changed-From-To: analyzed->open 
State-Changed-By: eadler 
State-Changed-When: Sun Jul 1 16:10:38 UTC 2012 
State-Changed-Why:  
unowned PRs must not be in analyzed state 

http://www.freebsd.org/cgi/query-pr.cgi?pr=57696 
>Unformatted:
