Mmap file local v / s nfs: what happens when the base file is swapped out to disk?

Assuming the file is replaced by (same name) by the wholesale and not changed in place (i.e. create a temp and then rename the file name).

My guess is that the change will not affect the local mmap as the kernel still has an old file (inode) handle on disk.

However, since NFS is stateless, will mmap get confused and mix up the contents of the old / new file during the (long) mmap time?

It seems to me that read / readv is the only secure way to handle NFS mounted files.

+3


source to share


1 answer


If you have an open link to a file, that link will continue to link to the same file as long as that link persists, even if the file itself is deleted or renamed, and even if its name is re-branded by the new file after it is deleted. The link can be a file descriptor or a memory map. This is part of POSIX and is true (or should be!) No matter what type of filesystem is used.

In other words: if you open a file on an NFS filesystem and map it to memory, you can continue to use that memory mapping until you cancel it, even if some other process (or the same process) deletes the file and replaces it with a new one with the same name.



It is true that the NFS protocol is stateless, so special steps must be taken to implement to ensure that this case is handled correctly. It's been a very long time since I looked at how this was done, but last time (on Solaris) it was done by renaming the files to special hidden names ( .nfsXXXXX

) instead of deleting them if their link count went down to zero while there were still open links to them. Anyway, whichever trick is used in implementation, you, as a filesystem user, shouldn't worry about it.

+6


source







All Articles