Re: Syncing a file's metadata in a portable way

From: Andrew Morton
Date: Fri Jul 09 2004 - 04:42:28 EST


Alberto Bertogli <albertogli@xxxxxxxxxxxxx> wrote:
>
>
> Hi!
>
> I wanted to know if there was a common, portable way of syncing a given
> file's metadata.
>
> In particular, I just want to create a file with open() and be sure that
> after some operation the file has been properly created and even if there
> is a crash, it can be accessed (modulo internal disk caches and all that
> stuff).
>
> I know that fsync() provides only data guarantees, and even the manpage
> says clearly that in order to sync metadata an "explicit fsync on the file
> descriptor of the directory is also needed".

It depends on the Linux filesystem. On ext3, for example, fsync() will
sync all of the filesytem's metadata (and data in journalled and ordered
data mode).

But on ext2 you'll need to fsync the directory. However, that only needs
to be done once, after the create.

> However, the O_DIRECTORY flag is Linux only, making this mechanism not
> portable.
>
> Is there a way of doing this in a portable way?

Doing a create, followed by a system-wide sync(), followed by
write/fsync/write/fsync/... will do what you want on all Linux
filesystems. That might be a bit of a performance problem if you're
creating a lot of files, although probably not.

This method should portable to other OS'es if they implement sync() sanely.

But note that they may not: according to the spec, sync() doesn't _have_ to
wait for all the queued I/O to complete prior to returning. It does on
Linux. Some additional sync()s may be needed on other OS'es.

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/