|
| 1 | +# `fsync` in Linux |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +`fsync()` is a POSIX system call used to ensure that all modified data and metadata associated with a file descriptor are flushed from volatile memory (page cache) to stable storage (e.g., disk). It is commonly used in durability-critical software such as databases, filesystems, and transactional systems. |
| 6 | + |
| 7 | +```c |
| 8 | +int fsync(int fd); |
| 9 | +``` |
| 10 | +
|
| 11 | +On success, `fsync()` returns `0`. On failure, it returns `-1` and sets `errno`. |
| 12 | +
|
| 13 | +## What `fsync()` Guarantees |
| 14 | +
|
| 15 | +When `fsync(fd)` returns successfully: |
| 16 | +
|
| 17 | +- All dirty **file data** for `fd` is written to disk |
| 18 | +- All necessary **metadata** (e.g., file size, timestamps, allocation info) is committed |
| 19 | +- Data is guaranteed to survive a **power loss or system crash** |
| 20 | +
|
| 21 | +This guarantee applies only to the specific file referenced by the file descriptor. |
| 22 | +
|
| 23 | +## What `fsync()` Does _Not_ Guarantee |
| 24 | +
|
| 25 | +- It does **not** guarantee persistence of directory entries unless the directory itself is also synced |
| 26 | +- It does **not** guarantee ordering between multiple file descriptors unless explicitly controlled |
| 27 | +- It does **not** ensure durability of memory-mapped (`mmap`) writes unless paired with `msync()` |
| 28 | +
|
| 29 | +## Common Usage Pattern |
| 30 | +
|
| 31 | +```c |
| 32 | +int fd = open("data.log", O_WRONLY | O_CREAT | O_TRUNC, 0644); |
| 33 | +write(fd, buffer, len); |
| 34 | +fsync(fd); |
| 35 | +close(fd); |
| 36 | +``` |
| 37 | + |
| 38 | +To ensure the file **name and existence** are durable: |
| 39 | + |
| 40 | +```c |
| 41 | +int dfd = open(".", O_DIRECTORY | O_RDONLY); |
| 42 | +fsync(dfd); |
| 43 | +close(dfd); |
| 44 | +``` |
| 45 | +
|
| 46 | +## `fsync()` vs Related Calls |
| 47 | +
|
| 48 | +### `fdatasync()` |
| 49 | +
|
| 50 | +- Flushes **data** only |
| 51 | +- May skip some metadata (e.g., timestamps) |
| 52 | +- Often faster than `fsync()` |
| 53 | +
|
| 54 | +```c |
| 55 | +fdatasync(fd); |
| 56 | +``` |
| 57 | + |
| 58 | +### `sync()` |
| 59 | + |
| 60 | +- Flushes **all** dirty buffers system-wide |
| 61 | +- Asynchronous on many systems |
| 62 | +- Not suitable for per-file durability guarantees |
| 63 | + |
| 64 | +### `msync()` |
| 65 | + |
| 66 | +- Used for `mmap()`-based I/O |
| 67 | +- Required to flush memory-mapped changes |
| 68 | + |
| 69 | +## Filesystem-Specific Behavior |
| 70 | + |
| 71 | +- **ext4** |
| 72 | + |
| 73 | + - Honors `fsync()` fully |
| 74 | + - Behavior affected by mount options (`data=ordered`, `data=journal`) |
| 75 | + |
| 76 | +- **XFS** |
| 77 | + |
| 78 | + - Strong `fsync()` guarantees |
| 79 | + - Directory `fsync()` often required for durability |
| 80 | + |
| 81 | +- **btrfs** |
| 82 | + |
| 83 | + - Copy-on-write semantics |
| 84 | + - `fsync()` may trigger more extensive metadata writes |
| 85 | + |
| 86 | +## Performance Characteristics |
| 87 | + |
| 88 | +- `fsync()` is expensive: |
| 89 | + |
| 90 | + - Forces cache flushes |
| 91 | + - Often triggers disk barriers or cache flush commands (e.g., `FLUSH CACHE`) |
| 92 | + |
| 93 | +- High-frequency `fsync()` calls can dominate latency |
| 94 | +- Common optimizations: |
| 95 | + |
| 96 | + - Group commit |
| 97 | + - Batched writes |
| 98 | + - Asynchronous I/O with explicit durability boundaries |
| 99 | + |
| 100 | +## Error Handling |
| 101 | + |
| 102 | +Common `errno` values: |
| 103 | + |
| 104 | +- `EBADF` – Invalid file descriptor |
| 105 | +- `EIO` – I/O error during writeback |
| 106 | +- `EINVAL` – Descriptor does not support syncing |
| 107 | + |
| 108 | +Errors may indicate **data loss risk** and should be treated as fatal in durability-sensitive applications. |
| 109 | + |
| 110 | +## Best Practices |
| 111 | + |
| 112 | +- Call `fsync()` only at well-defined durability points |
| 113 | +- Sync directories when creating, deleting, or renaming files |
| 114 | +- Prefer `fdatasync()` when metadata durability is unnecessary |
| 115 | +- Avoid relying on `close()` for durability guarantees |
| 116 | +- Test behavior under crash/power-failure scenarios |
0 commit comments