16

When a file is created on a Linux NFS shared mount, the client is either Linux o r mac machines. The existence or absence of a file is the key for what to do next, but the checking is not always return the right result:

e.g I am doing this in perl, this still not working well, esp with mac machines

write_key_file();  # write a file that must be checked before proceeding

The following checks cannot always return true when the file does exist

Issue - this command in perl did not return correct status in NFS system:

if( -e $file){}

Overheared solution that did not work:

 sleep(5);   # wait 5 seconds
 system("ls -ltr"); # force to cache?
 if(-e $file){}

I do not intend to check every file like this, but there are a few key places where it is important to get the proper file status.

Do we have better ways to force refresh nfs cache on a specific file on a specific directory? Thanks.

I am not sure that this is a XY problem, but there are a few weakest points that can all be solutions.

A -- NFS clients setting

If there is a solution at this stage, it would be great!

B -- exit_code or return code of writing function

$exit_code = write_key_file();

The problem with it, not all writing are within the scope of the code block. this only solve part of the problem.

C -- Disable NFS cache for the specific file or directory for file checking

I need to make sure is this possible and how? if no and why?

and I am open to all possible solutions, no solution or other possibilities.

Filburt
  • 17,626
  • 12
  • 64
  • 115
Gang
  • 2,658
  • 3
  • 17
  • 38
  • 2
    This is an ugly thing to do. What are you actually trying to accomplish? It is likely there is a much better way. NFS caches by design for efficiency. Non-cached filesystem IO can be done, but it's a lot slower than you might imagine. Usually, the answer is - try doing it a different way. – Sobrique Jan 27 '16 at 09:24
  • @Sobrique, I am not looking for ways to disable NFS cache or recache for all files, just for a few important files in program, for the the particular file in a particular directory. I cannot think of an alternative ways to accomplish this. I can only offer this much for the reputation I have so far. thanks for your attention – Gang Jan 29 '16 at 01:15
  • 2
    Here's [a more straightforward approach](http://serverfault.com/q/278349/131167). – ThisSuitIsBlackNot Jan 29 '16 at 04:34
  • 1
    My point is, this may be an XY problem. Something that is best solved by taking s step back and looking at what you are trying to accomplish – Sobrique Jan 29 '16 at 07:28
  • 2
    You *might* be able to use `sysopen` with `O_RDWR|O_CREAT|O_EXCL` to force NFS cache refresh - if the call succeeds then the file didn't exist (but not all NFS implementations handle O_EXCL correctly). I used such technique in python (I'm NULL at perl). See my answer to http://stackoverflow.com/questions/29979624/can-i-prevent-a-script-from-launching-twice-using-open2-with-o-creat-and-flock/29986073#29986073 – Dan Cornilescu Jan 29 '16 at 07:39
  • @DanCornilescu, are you sure `sysopen` can open a file that `-e $file` does not exist? – Gang Jan 30 '16 at 00:20
  • 1
    yes, trying to *write* the file will make the NFS client contact the server right away, while `-e $file`, being a read operation, will likely use the cache. – Dan Cornilescu Jan 30 '16 at 03:40
  • @DanCornilescu, I changed to include python, a python answer is fine with me. – Gang Jan 31 '16 at 00:25
  • 5
    Perhaps this is worth a try: http://unix.stackexchange.com/a/207383. He does an `opendir/closedir` on the parent directory. – PerlDuck Jan 31 '16 at 17:52
  • That `ls` should work? See also https://stackoverflow.com/a/30630912/32453 – rogerdpack Jun 16 '21 at 21:41

1 Answers1

19

This solution belongs to the Category B : exit_code or return code of writing function

...only open() and fopen() need to guarantee that they get a consistent handle to a particular file for reading and writing. stat and friends are not required to retrieve fresh attributes. Thus, for the sake of close-to-open cache coherence, only open() and fopen() are considered an "open event" where fresh attributes need to be fetched immediately from the server[1].


The following solutions belong to the Category A : NFS clients setting
i.e. if you do NOT expect cached file/dir entries to be served to the client, disable caching.

Setup a shared cache

If the file in the NFS mount (whose existence is being checked) is created by another application on the same client (possibly using another mount point to the same NFS export) the consider using a single shared NFS cache on the client.

Use the sharecache option to setup the NFS mounts on the client.

This option determines how the client's data-cache and attribute-cache are shared when mounting the same export more than once concurrently. Using the same cache reduces memory requirements on the client and presents identical file contents to applications when the same remote file is accessed via different mount points.


Setting-up a NFS-mount without caching

Disable attribute caching.

Mount the NFS share on the client with the noac option.

Alternately, disable cached directory attributes from being served.

Use acdirmin=0,acdirmax=0 to set the cache timeouts to 0 (effectively disabling caching).


Setting-up a NFS-mount to ignore lookup caches

Use lookupcache=positive OR lookupcache=none

(available options : all, positive and none)

When attempting to access a directory entry over a NFS mount,
if the requested directory entry exists on the server, the result is referred to as positive.
if the requested directory entry does not exist on the server, the result is referred to as negative.

If the lookupcache option is not specified, or if all is specified, the client assumes both types of directory cache entries are valid until their parent directory's cached attributes expire.

If pos or positive is specified, the client assumes positive entries are valid until their parent directory's cached attributes expire, but always revalidates negative entires before an application can use them.

If none is specified, the client revalidates both types of directory cache entries before an application can use them. This permits quick detection of files that were created or removed by other clients, but can impact application and server performance.


References:
1. Close-To-Open Cache Consistency in the Linux NFS Client
2. NFS - Detecting remotely created files programmatically?
3. NFS cache : file content not updated on client when modified on server
4. NFS man page. Especially the "Data And Metadata Coherence" section.

Community
  • 1
  • 1
TheCodeArtist
  • 21,479
  • 4
  • 69
  • 130
  • The answer is good enough for me, I will just check the exit code of the writing function, it the exit code of out of reach, I will just leave as is, waiting systems to be updated. Thanks – Gang Feb 03 '16 at 23:02
  • 1
    Using the "noac" option worked for me. However it can't be added to the "remount" option. But unmounting and mounting again works of course. – Marcell Jun 12 '23 at 11:27