Skip to content

Race-free file-based compare-and-swap #58

Description

@aagor

It would be nice to be able to replace a file, but only if the replaced file still points to the specified inode.
linkat2(olddirfd, oldpath, newdirfd, newpath, inode_fd, flags)

Typical pattern would be:

  • Read the content of a file.
  • Update it and write the modified content into a new O_TMPFILE file.
  • Atomically replace the existing file with the temporary file from above, but only if it still refers to the one read at step 1.
  • If that failed, go back to step 1.

Use case would be a file-based key-value store or similar.
You want to add or remove an item from a list or update a single value from a key-value-pair.
When multiple threads want to do such updates, these updates must be visible atomically.

The idea allows to implement Compare-and-swap algorithms on files and is an extension to the
Linking of O_TMPFILE files with replacement idea.

Unlinking via two file descriptors uses the same concept of an additional fd to check the operation is done on the desired inode to avoid TOCTOU races.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions