[PATCH] ldconfig: Call fsync on temporary files before renaming them [BZ #20890]

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

[PATCH] ldconfig: Call fsync on temporary files before renaming them [BZ #20890]

Florian Weimer-5
If the system crashes before the file data has been written to disk, the
file system recovery upon the next mount may restore a partially
rewritten temporary file under the non-temporary (final) name (after the
rename operation).

Some file systems perform an implicit fsync before renaming a file over
another one, but XFS does not, for example.

2018-01-12  Florian Weimer  <[hidden email]>

        [BZ #20890]
        * elf/cache.c (save_cache): Call fsync on temporary file before
        renaming it.
        (save_aux_cache): Likewise.

diff --git a/elf/cache.c b/elf/cache.c
index 1ec6ab36e7..2165f8d305 100644
--- a/elf/cache.c
+++ b/elf/cache.c
@@ -449,7 +449,8 @@ save_cache (const char *cache_name)
     }
 
   if (write (fd, strings, total_strlen) != (ssize_t) total_strlen
-      || close (fd))
+      || fsync (fd) != 0
+      || close (fd) != 0)
     error (EXIT_FAILURE, errno, _("Writing of cache data failed"));
 
   /* Make sure user can always read cache file */
@@ -812,7 +813,8 @@ save_aux_cache (const char *aux_cache_name)
 
   if (write (fd, file_entries, file_entries_size + total_strlen)
       != (ssize_t) (file_entries_size + total_strlen)
-      || close (fd))
+      || fsync (fd) != 0
+      || close (fd) != 0)
     {
       unlink (temp_name);
       goto out_fail;
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] ldconfig: Call fsync on temporary files before renaming them [BZ #20890]

Christoph Hellwig-2
On Fri, Jan 12, 2018 at 12:07:57PM +0100, Florian Weimer wrote:
> If the system crashes before the file data has been written to disk, the
> file system recovery upon the next mount may restore a partially
> rewritten temporary file under the non-temporary (final) name (after the
> rename operation).
>
> Some file systems perform an implicit fsync before renaming a file over
> another one, but XFS does not, for example.

At least on Linux no file system performs an actual fsync equivalent.
A few do start a writeout, but don't actually wait on it.
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] ldconfig: Call fsync on temporary files before renaming them [BZ #20890]

Florian Weimer-5
On 01/12/2018 12:37 PM, Christoph Hellwig wrote:

> On Fri, Jan 12, 2018 at 12:07:57PM +0100, Florian Weimer wrote:
>> If the system crashes before the file data has been written to disk, the
>> file system recovery upon the next mount may restore a partially
>> rewritten temporary file under the non-temporary (final) name (after the
>> rename operation).
>>
>> Some file systems perform an implicit fsync before renaming a file over
>> another one, but XFS does not, for example.
>
> At least on Linux no file system performs an actual fsync equivalent.
> A few do start a writeout, but don't actually wait on it.

Oh.  I'll drop the last paragraph from the commit message, then.

Thanks,
Florian
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] ldconfig: Call fsync on temporary files before renaming them [BZ #20890]

Paul Eggert
In reply to this post by Florian Weimer-5
Florian Weimer wrote:
>     if (write (fd, strings, total_strlen) != (ssize_t) total_strlen
> -      || close (fd))
> +      || fsync (fd) != 0
> +      || close (fd) != 0)
>       error (EXIT_FAILURE, errno, _("Writing of cache data failed"));

Would fdatasync suffice, instead of fsync?
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] ldconfig: Call fsync on temporary files before renaming them [BZ #20890]

Florian Weimer-5
On 01/13/2018 03:28 AM, Paul Eggert wrote:
> Florian Weimer wrote:
>>     if (write (fd, strings, total_strlen) != (ssize_t) total_strlen
>> -      || close (fd))
>> +      || fsync (fd) != 0
>> +      || close (fd) != 0)
>>       error (EXIT_FAILURE, errno, _("Writing of cache data failed"));
>
> Would fdatasync suffice, instead of fsync?

One can be fdatasync, I think.

The first one should actually happen after the chmod call, and then we
need fsync.

Thanks,
Florian

ldconfig-fsync.patch (1K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] ldconfig: Call fsync on temporary files before renaming them [BZ #20890]

Florian Weimer-5
On 01/19/2018 05:39 PM, Florian Weimer wrote:
> 2018-01-12  Florian Weimer<[hidden email]>
>
> [BZ #20890]
> * elf/cache.c (save_cache): Call fsync on temporary file before
> renaming it.
> (save_aux_cache): Call fdatasync on temporary file before renaming
> it.

Ping?

<https://sourceware.org/ml/libc-alpha/2018-01/msg00660.html>

Thanks,
Florian
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] ldconfig: Call fsync on temporary files before renaming them [BZ #20890]

Adhemerval Zanella-2


On 20/02/2018 10:59, Florian Weimer wrote:

> On 01/19/2018 05:39 PM, Florian Weimer wrote:
>> 2018-01-12  Florian Weimer<[hidden email]>
>>
>>     [BZ #20890]
>>     * elf/cache.c (save_cache): Call fsync on temporary file before
>>     renaming it.
>>     (save_aux_cache): Call fdatasync on temporary file before renaming
>>     it.
>
> Ping?

LGTM.

Reviewed-by: Adhemerval Zanella <[hidden email]>

>
> <https://sourceware.org/ml/libc-alpha/2018-01/msg00660.html>
>
> Thanks,
> Florian