Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for re-reading /etc/mtab, part 2. #2215

Closed
wants to merge 1 commit into from

Conversation

jmlayman
Copy link
Contributor

This is a continuation of fb5c53e:

When /etc/mtab is updated on Linux it's done atomically with
rename(2).  A new mtab is written, the existing mtab is unlinked,
and the new mtab is renamed to /etc/mtab.  This means that we
must close the old file and open the new file to get the updated
contents.  Using rewind(3) will just move the file pointer back
to the start of the file, freopen(3) will close and open the file.

In this commit, a few more rewind(3) calls were replaced with freopen(3)
to allow updated mtab entries to be picked up immediately.

Note that technically only libzfs_mount.c & libzfs_util.c need to be modified as they are libraries and so the functions within them can be called multiple times (during which time /etc/mtab could change). Since zfs_main.c is a utility front-end, it probably isn't necessary to modify it but I went ahead & did so anyway. If this is not desired, those changes can be omitted.

This is a continuation of fb5c53e:

    When /etc/mtab is updated on Linux it's done atomically with
    rename(2).  A new mtab is written, the existing mtab is unlinked,
    and the new mtab is renamed to /etc/mtab.  This means that we
    must close the old file and open the new file to get the updated
    contents.  Using rewind(3) will just move the file pointer back
    to the start of the file, freopen(3) will close and open the file.

In this commit, a few more rewind(3) calls were replaced with freopen(3)
to allow updated mtab entries to be picked up immediately.
@behlendorf
Copy link
Contributor

Do you have a specific use case where you were seeing a stale /etc/mtab?

@jmlayman
Copy link
Contributor Author

Yes - I am trying to import/export volumes programatically without using popen to invoke zpool. In the case of export (where it fails), I basically recreated what zpool_do_export() in zpool_main.c is doing in my code. Everything works great if the pool I am exporting is in /etc/mtab before my program starts because the logic inside zpool_disable_datasets() finds the pool in /etc/mtab & exports it. If I then import that pool (or another, for that matter) & then try to export it, the logic in zpool_disable_datasets() doesn't find the pool in /etc/mtab because it hasn't re-read it (it simply does a rewind). This causes zpool_disable_datasets() to fail & so the call to zpool_export() never happens. This all works fine when calling zpool import/export from the command-line because each invocation causes /etc/mtab to be re-read. However, since I am doing it programmatically the existing logic never re-reads /etc/mtab & so it never notices a volume has been imported. I suppose I could call libzfs_init() every time, but changing the logic to use freopen() seemed cleaner to me.

@behlendorf behlendorf added this to the 0.6.3 milestone Apr 4, 2014
@behlendorf behlendorf closed this in cbca607 Apr 4, 2014
ryao pushed a commit to ryao/zfs that referenced this pull request Apr 9, 2014
This is a continuation of fb5c53e:

    When /etc/mtab is updated on Linux it's done atomically with
    rename(2).  A new mtab is written, the existing mtab is unlinked,
    and the new mtab is renamed to /etc/mtab.  This means that we
    must close the old file and open the new file to get the updated
    contents.  Using rewind(3) will just move the file pointer back
    to the start of the file, freopen(3) will close and open the file.

In this commit, a few more rewind(3) calls were replaced with freopen(3)
to allow updated mtab entries to be picked up immediately.

Signed-off-by: John M. Layman <jml@frijid.net>
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Closes openzfs#2215
Issue openzfs#1611
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants