AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
flock.c File Reference

Advisory locks on whole files, for flock(). More...

#include <core/system.h>
#include <fs/vfs.h>
#include <net/socket.h>
#include <sys/sched.h>
#include <ds/queue.h>
#include <aquila/fcntl.h>
#include <aquila/errno.h>

Go to the source code of this file.

Data Structures

struct  file_lock
 An open file's lock. More...

Functions

static int lock_conflicts (struct file_lock *lock, int type)
 Says whether taking a lock of type conflicts with a lock that another open file holds on the same file.
static void lock_unlock (struct file_lock *lock)
 Releases a lock, and wakes up the threads waiting for one.
int vfs_file_flock (struct file *file, int op)
 Takes, changes or releases an open file's lock.
void vfs_file_lock_release (struct file *file)
 Releases an open file's lock when the file is closed.

Variables

static struct queue locks = {0}
static struct waitq waiters = {0}

Detailed Description

Advisory locks on whole files, for flock().

A lock is shared or exclusive, and an open file holds it, so the descriptors that dup() and fork() copied share it. It goes with LOCK_UN, or when the open file is closed.

Definition in file flock.c.

Function Documentation

◆ lock_conflicts()

int lock_conflicts ( struct file_lock * lock,
int type )
static

Says whether taking a lock of type conflicts with a lock that another open file holds on the same file.

Definition at line 31 of file flock.c.

References lock_conflicts(), locks, queue_for, file_lock::type, and file_lock::vnode.

Referenced by lock_conflicts(), and vfs_file_flock().

◆ lock_unlock()

void lock_unlock ( struct file_lock * lock)
static

Releases a lock, and wakes up the threads waiting for one.

Definition at line 45 of file flock.c.

References lock_unlock(), locks, file_lock::type, waiters, and waitq_wakeup().

Referenced by lock_unlock(), vfs_file_flock(), and vfs_file_lock_release().

◆ vfs_file_flock()

int vfs_file_flock ( struct file * file,
int op )

Takes, changes or releases an open file's lock.

Taking a lock waits until no other open file holds a conflicting one, unless LOCK_NB is set.

Parameters
filethe open file
opLOCK_SH (shared), LOCK_EX (exclusive) or LOCK_UN (release), with LOCK_NB not to wait
Returns
0, -EWOULDBLOCK if LOCK_NB is set and it would wait, -EINVAL, -ENOMEM, or -ERESTARTSYS if a signal came while it waited

Definition at line 67 of file flock.c.

References FILE_SOCKET, file::flags, kmalloc(), file::lock, lock_conflicts(), lock_unlock(), locks, M_ZERO, file_lock::type, vfs_file_flock(), file_lock::vnode, waiters, waitq_sleep(), and waitq_wakeup().

Referenced by sys_flock(), and vfs_file_flock().

◆ vfs_file_lock_release()

void vfs_file_lock_release ( struct file * file)

Releases an open file's lock when the file is closed.

Parameters
filethe open file

Definition at line 119 of file flock.c.

References kfree(), file::lock, lock_unlock(), NULL, and vfs_file_lock_release().

Referenced by vfs_file_close(), and vfs_file_lock_release().

Variable Documentation

◆ locks

struct queue locks = {0}
static

Definition at line 26 of file flock.c.

Referenced by lock_conflicts(), lock_unlock(), and vfs_file_flock().

◆ waiters

struct waitq waiters = {0}
static

Definition at line 27 of file flock.c.

Referenced by lock_unlock(), and vfs_file_flock().