fs/locks.c
Source file repositories/reference/linux-study-clean/fs/locks.c
File Facts
- System
- Linux kernel
- Corpus path
fs/locks.c- Extension
.c- Size
- 85066 bytes
- Lines
- 3164
- 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.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/capability.hlinux/file.hlinux/fdtable.hlinux/filelock.hlinux/fs.hlinux/init.hlinux/security.hlinux/slab.hlinux/syscalls.hlinux/time.hlinux/rcupdate.hlinux/pid_namespace.hlinux/hashtable.hlinux/percpu.hlinux/sysctl.htrace/events/filelock.hlinux/uaccess.hlinux/proc_fs.hlinux/seq_file.h
Detected Declarations
syscall flockstruct file_lock_list_structstruct locks_iteratorfunction fcntlfunction lease_breakingfunction target_leasetypefunction init_fs_locks_sysctlsfunction locks_get_lock_contextfunction locks_dump_ctx_listfunction locks_check_ctx_listsfunction locks_check_ctx_file_listfunction locks_free_lock_contextfunction locks_init_lock_headsfunction locks_release_privatefunction locks_owner_has_blockersfunction locks_free_lockfunction locks_free_leasefunction locks_dispose_listfunction lease_dispose_listfunction locks_init_lockfunction locks_init_leasefunction locks_copy_conflockfunction locks_copy_lockfunction locks_move_blocksfunction flock_translate_cmdfunction flock_make_lockfunction assign_typefunction flock64_to_posix_lockfunction flock_to_posix_lockfunction lease_break_callbackfunction lease_setupfunction lease_open_conflictfunction lease_initfunction locks_overlapfunction posix_same_ownerfunction locks_insert_global_locksfunction locks_delete_global_locksfunction posix_owner_keyfunction locks_insert_global_blockedfunction locks_delete_global_blockedfunction __locks_unlink_blockfunction __locks_wake_up_blocksfunction __locks_delete_blockfunction locks_delete_blockfunction __locks_insert_blockfunction locks_insert_blockfunction locks_wake_up_blocksfunction locks_insert_lock_ctx
Annotated Snippet
SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd)
{
int can_sleep, error, type;
struct file_lock fl;
/*
* LOCK_MAND locks were broken for a long time in that they never
* conflicted with one another and didn't prevent any sort of open,
* read or write activity.
*
* Just ignore these requests now, to preserve legacy behavior, but
* throw a warning to let people know that they don't actually work.
*/
if (cmd & LOCK_MAND) {
pr_warn_once("%s(%d): Attempt to set a LOCK_MAND lock via flock(2). This support has been removed and the request ignored.\n", current->comm, current->pid);
return 0;
}
type = flock_translate_cmd(cmd & ~LOCK_NB);
if (type < 0)
return type;
CLASS(fd, f)(fd);
if (fd_empty(f))
return -EBADF;
if (type != F_UNLCK && !(fd_file(f)->f_mode & (FMODE_READ | FMODE_WRITE)))
return -EBADF;
flock_make_lock(fd_file(f), &fl, type);
error = security_file_lock(fd_file(f), fl.c.flc_type);
if (error)
return error;
can_sleep = !(cmd & LOCK_NB);
if (can_sleep)
fl.c.flc_flags |= FL_SLEEP;
if (fd_file(f)->f_op->flock)
error = fd_file(f)->f_op->flock(fd_file(f),
(can_sleep) ? F_SETLKW : F_SETLK,
&fl);
else
error = locks_lock_file_wait(fd_file(f), &fl);
locks_release_private(&fl);
return error;
}
/**
* vfs_test_lock - test file byte range lock
* @filp: The file to test lock for
* @fl: The byte-range in the file to test; also used to hold result
*
* On entry, @fl does not contain a lock, but identifies a range (fl_start, fl_end)
* in the file (c.flc_file), and an owner (c.flc_owner) for whom existing locks
* should be ignored. c.flc_type and c.flc_flags are ignored.
* Both fl_lmops and fl_ops in @fl must be NULL.
* Returns -ERRNO on failure. Indicates presence of conflicting lock by
* setting fl->fl_type to something other than F_UNLCK.
*
* If vfs_test_lock() does find a lock and return it, the caller must
* use locks_free_lock() or locks_release_private() on the returned lock.
*/
int vfs_test_lock(struct file *filp, struct file_lock *fl)
{
int error = 0;
WARN_ON_ONCE(fl->fl_ops || fl->fl_lmops);
WARN_ON_ONCE(filp != fl->c.flc_file);
if (filp->f_op->lock)
error = filp->f_op->lock(filp, F_GETLK, fl);
else
posix_test_lock(filp, fl);
/*
* We don't expect FILE_LOCK_DEFERRED and callers cannot
* handle it.
*/
if (WARN_ON_ONCE(error == FILE_LOCK_DEFERRED))
error = -EIO;
return error;
}
EXPORT_SYMBOL_GPL(vfs_test_lock);
/**
* locks_translate_pid - translate a file_lock's fl_pid number into a namespace
* @fl: The file_lock who's fl_pid should be translated
Annotation
- Immediate include surface: `linux/capability.h`, `linux/file.h`, `linux/fdtable.h`, `linux/filelock.h`, `linux/fs.h`, `linux/init.h`, `linux/security.h`, `linux/slab.h`.
- Detected declarations: `syscall flock`, `struct file_lock_list_struct`, `struct locks_iterator`, `function fcntl`, `function lease_breaking`, `function target_leasetype`, `function init_fs_locks_sysctls`, `function locks_get_lock_context`, `function locks_dump_ctx_list`, `function locks_check_ctx_lists`.
- Atlas domain: Core OS / VFS And Filesystem Core.
- Implementation status: core implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
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.