mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* Memory mapped files on NFS mounted drives
@ 2002-07-22 16:28 Gregory Giguashvili
  0 siblings, 0 replies; only message in thread
From: Gregory Giguashvili @ 2002-07-22 16:28 UTC (permalink / raw)
  To: Linux Kernel (E-mail); +Cc: Alex Shaltiel, Oleg Meshbey

[-- Attachment #1: Type: text/plain, Size: 1536 bytes --]

Hello,

OS/Kernel checked:
===============
RH 7.2 (kernel 2.4.7-10) and RH 7.3 (kernel 2.4.18-3) (I haven't checked the
others).

Problem description:
===============
I attempt to read/write memory mapped file from two Linux machines, which
resides on NFS mounted drive. The file gets corrupted since the changes made
on one machine aren't immediately available on the other. The sample program
is attached to this e-mail - simply run and play with it on NFS mounted
drive to make sure there is some inconsistency.
The problematic API set includes (mmap, munmap and msync system calls).

The original code uses NFS locking to assure file consistency, but the
example misses this part to simplicity (locking is simulated by the user).
The same code works on a variety of other operating systems, but fails to
work between two Linux or Linux/Other OS machines.

Known workarounds/patches:
======================
I decided to give up on the performance issue and even tried to remap the
whole file before every attempt to read/write the mapped file. Surprisingly,
even this extreme measure didn't help (the code is commented out using
preprocessor directives in the sample program).
I couldn't find any patch, which specifically fixes this problem, though I
have seen some patches related to msync, which I don't think to be relevant
(Am I wrong?).


I'm sure that someone has come across this problem and I sure hope there is
some workaround/patch available. 
Any help will be greatly appreciated.

Thanks in advance.
Giga

 <<mmap.cc>> 

[-- Attachment #2: mmap.cc --]
[-- Type: application/octet-stream, Size: 1433 bytes --]

#include <stdio.h>
#include <unistd.h>
#include <sys/mman.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main (int argc, char* argv []) 
{
    char buffer [BUFSIZ];
    char* pMap;

    int fd = open ("MAPPED.FILE", O_CREAT | O_RDWR | O_SYNC, 0666);
    if (fd == -1) {
	perror ("open");
	return -1;
    }

    write (fd, "\0", 1);
    if (lseek (fd, BUFSIZ - 1, SEEK_SET) != BUFSIZ - 1) {
	perror ("lseek");
	return -1;
    }
    write (fd, "\0", 1);

    pMap = (char*) mmap (NULL, BUFSIZ, PROT_READ | PROT_WRITE, 
			 MAP_SHARED, fd, 0);
    if ((size_t) pMap == -1) {
	perror ("mmap");
	return -1;
    }
    
    while (1) {
	fprintf (stderr, "<Press ENTER to read> OR <Enter string to write> $ ");
	fgets (buffer, sizeof (buffer) - 1, stdin);

#if 0
	if (munmap (pMap, BUFSIZ)) {
	    perror ("munmap");
	    return -1;
	}
	pMap = (char*) mmap (NULL, BUFSIZ, PROT_READ | PROT_WRITE, 
				   MAP_SHARED, fd, 0);
	if ((size_t) pMap == -1) {
	    perror ("mmap");
	    return -1;
	}
#endif

	if (buffer [0] == '\n') {
	    fprintf (stderr, "Mapped file contents: %s\n", pMap);
	} else {
	    strncpy (pMap, buffer, strlen (buffer) - 1);
	    fprintf (stderr, "Written to mapped file: %s\n", pMap);
	}

	if (msync (pMap, BUFSIZ, MS_SYNC | MS_INVALIDATE)) {
	    perror ("msync");
	    return -1;
	}
    }

    return 0;
}

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2002-07-22 15:28 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-07-22 16:28 Memory mapped files on NFS mounted drives Gregory Giguashvili

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®