fs/sync.c
Source file repositories/reference/linux-study-clean/fs/sync.c
File Facts
- System
- Linux kernel
- Corpus path
fs/sync.c- Extension
.c- Size
- 10435 bytes
- Lines
- 381
- Domain
- Core OS
- Bucket
- VFS And Filesystem Core
- Inferred role
- Core OS: syscall or user/kernel boundary
- Status
- core implementation candidate
Why This File Exists
Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Defines or participates in a user/kernel boundary; inspect argument validation, copy_from_user/copy_to_user, credentials, and dispatch target.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/blkdev.hlinux/kernel.hlinux/file.hlinux/fs.hlinux/slab.hlinux/export.hlinux/namei.hlinux/sched.hlinux/writeback.hlinux/syscalls.hlinux/linkage.hlinux/pagemap.hlinux/quotaops.hlinux/backing-dev.hinternal.h
Detected Declarations
syscall syncsyscall syncfssyscall fsyncsyscall fdatasyncsyscall sync_file_rangesyscall sync_file_range2function sync_filesystemfunction sync_inodes_one_sbfunction sync_fs_one_sbfunction metadatafunction do_sync_workfunction emergency_syncfunction vfs_fsync_rangefunction vfs_fsyncfunction do_fsyncfunction sync_file_rangefunction ksys_sync_file_rangefunction compat_arg_u64_dualexport sync_filesystemexport vfs_fsync_rangeexport vfs_fsync
Annotated Snippet
SYSCALL_DEFINE0(sync)
{
ksys_sync();
return 0;
}
static void do_sync_work(struct work_struct *work)
{
int nowait = 0;
int wait = 1;
/*
* Sync twice to reduce the possibility we skipped some inodes / pages
* because they were temporarily locked
*/
iterate_supers(sync_inodes_one_sb, NULL);
iterate_supers(sync_fs_one_sb, &nowait);
sync_bdevs(false);
iterate_supers(sync_inodes_one_sb, NULL);
iterate_supers(sync_fs_one_sb, &wait);
sync_bdevs(false);
printk("Emergency Sync complete\n");
kfree(work);
}
void emergency_sync(void)
{
struct work_struct *work;
work = kmalloc_obj(*work, GFP_ATOMIC);
if (work) {
INIT_WORK(work, do_sync_work);
schedule_work(work);
}
}
/*
* sync a single super
*/
SYSCALL_DEFINE1(syncfs, int, fd)
{
CLASS(fd, f)(fd);
struct super_block *sb;
int ret, ret2;
if (fd_empty(f))
return -EBADF;
sb = fd_file(f)->f_path.dentry->d_sb;
down_read(&sb->s_umount);
ret = sync_filesystem(sb);
up_read(&sb->s_umount);
ret2 = errseq_check_and_advance(&sb->s_wb_err, &fd_file(f)->f_sb_err);
return ret ? ret : ret2;
}
/**
* vfs_fsync_range - helper to sync a range of data & metadata to disk
* @file: file to sync
* @start: offset in bytes of the beginning of data range to sync
* @end: offset in bytes of the end of data range (inclusive)
* @datasync: perform only datasync
*
* Write back data in range @start..@end and metadata for @file to disk. If
* @datasync is set only metadata needed to access modified file data is
* written.
*/
int vfs_fsync_range(struct file *file, loff_t start, loff_t end, int datasync)
{
struct inode *inode = file->f_mapping->host;
if (!file->f_op->fsync)
return -EINVAL;
if (!datasync)
sync_lazytime(inode);
return file->f_op->fsync(file, start, end, datasync);
}
EXPORT_SYMBOL(vfs_fsync_range);
/**
* vfs_fsync - perform a fsync or fdatasync on a file
* @file: file to sync
* @datasync: only perform a fdatasync operation
*
* Write back data and metadata for @file to disk. If @datasync is
* set only metadata needed to access modified file data is written.
*/
int vfs_fsync(struct file *file, int datasync)
Annotation
- Immediate include surface: `linux/blkdev.h`, `linux/kernel.h`, `linux/file.h`, `linux/fs.h`, `linux/slab.h`, `linux/export.h`, `linux/namei.h`, `linux/sched.h`.
- Detected declarations: `syscall sync`, `syscall syncfs`, `syscall fsync`, `syscall fdatasync`, `syscall sync_file_range`, `syscall sync_file_range2`, `function sync_filesystem`, `function sync_inodes_one_sb`, `function sync_fs_one_sb`, `function metadata`.
- Atlas domain: Core OS / VFS And Filesystem Core.
- Implementation status: core implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.