Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Macros | Functions | Variables
locks.c File Reference
#include <linux/capability.h>
#include <linux/file.h>
#include <linux/fdtable.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/security.h>
#include <linux/slab.h>
#include <linux/syscalls.h>
#include <linux/time.h>
#include <linux/rcupdate.h>
#include <linux/pid_namespace.h>
#include <asm/uaccess.h>

Go to the source code of this file.

Macros

#define IS_POSIX(fl)   (fl->fl_flags & FL_POSIX)
 
#define IS_FLOCK(fl)   (fl->fl_flags & FL_FLOCK)
 
#define IS_LEASE(fl)   (fl->fl_flags & FL_LEASE)
 
#define for_each_lock(inode, lockp)   for (lockp = &inode->i_flock; *lockp != NULL; lockp = &(*lockp)->fl_next)
 
#define MAX_DEADLK_ITERATIONS   10
 

Functions

void lock_flocks (void)
 
 EXPORT_SYMBOL_GPL (lock_flocks)
 
void unlock_flocks (void)
 
 EXPORT_SYMBOL_GPL (unlock_flocks)
 
struct file_locklocks_alloc_lock (void)
 
 EXPORT_SYMBOL_GPL (locks_alloc_lock)
 
void locks_release_private (struct file_lock *fl)
 
 EXPORT_SYMBOL_GPL (locks_release_private)
 
void locks_free_lock (struct file_lock *fl)
 
 EXPORT_SYMBOL (locks_free_lock)
 
void locks_init_lock (struct file_lock *fl)
 
 EXPORT_SYMBOL (locks_init_lock)
 
void __locks_copy_lock (struct file_lock *new, const struct file_lock *fl)
 
 EXPORT_SYMBOL (__locks_copy_lock)
 
void locks_copy_lock (struct file_lock *new, struct file_lock *fl)
 
 EXPORT_SYMBOL (locks_copy_lock)
 
void locks_delete_block (struct file_lock *waiter)
 
 EXPORT_SYMBOL (locks_delete_block)
 
void posix_test_lock (struct file *filp, struct file_lock *fl)
 
 EXPORT_SYMBOL (posix_test_lock)
 
int posix_lock_file (struct file *filp, struct file_lock *fl, struct file_lock *conflock)
 
 EXPORT_SYMBOL (posix_lock_file)
 
int posix_lock_file_wait (struct file *filp, struct file_lock *fl)
 
 EXPORT_SYMBOL (posix_lock_file_wait)
 
int locks_mandatory_locked (struct inode *inode)
 
int locks_mandatory_area (int read_write, struct inode *inode, struct file *filp, loff_t offset, size_t count)
 
 EXPORT_SYMBOL (locks_mandatory_area)
 
int lease_modify (struct file_lock **before, int arg)
 
 EXPORT_SYMBOL (lease_modify)
 
int __break_lease (struct inode *inode, unsigned int mode)
 
 EXPORT_SYMBOL (__break_lease)
 
void lease_get_mtime (struct inode *inode, struct timespec *time)
 
 EXPORT_SYMBOL (lease_get_mtime)
 
int fcntl_getlease (struct file *filp)
 
int generic_add_lease (struct file *filp, long arg, struct file_lock **flp)
 
int generic_delete_lease (struct file *filp, struct file_lock **flp)
 
int generic_setlease (struct file *filp, long arg, struct file_lock **flp)
 
 EXPORT_SYMBOL (generic_setlease)
 
int vfs_setlease (struct file *filp, long arg, struct file_lock **lease)
 
 EXPORT_SYMBOL_GPL (vfs_setlease)
 
int fcntl_setlease (unsigned int fd, struct file *filp, long arg)
 
int flock_lock_file_wait (struct file *filp, struct file_lock *fl)
 
 EXPORT_SYMBOL (flock_lock_file_wait)
 
 SYSCALL_DEFINE2 (flock, unsigned int, fd, unsigned int, cmd)
 
int vfs_test_lock (struct file *filp, struct file_lock *fl)
 
 EXPORT_SYMBOL_GPL (vfs_test_lock)
 
int fcntl_getlk (struct file *filp, struct flock __user *l)
 
int vfs_lock_file (struct file *filp, unsigned int cmd, struct file_lock *fl, struct file_lock *conf)
 
 EXPORT_SYMBOL_GPL (vfs_lock_file)
 
int fcntl_setlk (unsigned int fd, struct file *filp, unsigned int cmd, struct flock __user *l)
 
void locks_remove_posix (struct file *filp, fl_owner_t owner)
 
 EXPORT_SYMBOL (locks_remove_posix)
 
void locks_remove_flock (struct file *filp)
 
int posix_unblock_lock (struct file *filp, struct file_lock *waiter)
 
 EXPORT_SYMBOL (posix_unblock_lock)
 
int vfs_cancel_lock (struct file *filp, struct file_lock *fl)
 
 EXPORT_SYMBOL_GPL (vfs_cancel_lock)
 
int lock_may_read (struct inode *inode, loff_t start, unsigned long len)
 
 EXPORT_SYMBOL (lock_may_read)
 
int lock_may_write (struct inode *inode, loff_t start, unsigned long len)
 
 EXPORT_SYMBOL (lock_may_write)
 
 core_initcall (filelock_init)
 

Variables

int leases_enable = 1
 
int lease_break_time = 45
 

Macro Definition Documentation

#define for_each_lock (   inode,
  lockp 
)    for (lockp = &inode->i_flock; *lockp != NULL; lockp = &(*lockp)->fl_next)

Definition at line 153 of file locks.c.

#define IS_FLOCK (   fl)    (fl->fl_flags & FL_FLOCK)

Definition at line 133 of file locks.c.

#define IS_LEASE (   fl)    (fl->fl_flags & FL_LEASE)

Definition at line 134 of file locks.c.

#define IS_POSIX (   fl)    (fl->fl_flags & FL_POSIX)

Definition at line 132 of file locks.c.

#define MAX_DEADLK_ITERATIONS   10

Definition at line 672 of file locks.c.

Function Documentation

int __break_lease ( struct inode inode,
unsigned int  mode 
)

__break_lease - revoke all outstanding leases on file : the inode of the file to return : the open mode (read or write)

break_lease (inlined for speed) has checked there already is at least some kind of lock (maybe a lease) on this file. Leases are broken on a call to open() or truncate(). This function can sleep unless you specified O_NONBLOCK to your open().

Definition at line 1189 of file locks.c.

void __locks_copy_lock ( struct file_lock new,
const struct file_lock fl 
)

Definition at line 242 of file locks.c.

core_initcall ( filelock_init  )
EXPORT_SYMBOL ( locks_free_lock  )
EXPORT_SYMBOL ( locks_init_lock  )
EXPORT_SYMBOL ( __locks_copy_lock  )
EXPORT_SYMBOL ( locks_copy_lock  )
EXPORT_SYMBOL ( locks_delete_block  )
EXPORT_SYMBOL ( posix_test_lock  )
EXPORT_SYMBOL ( posix_lock_file  )
EXPORT_SYMBOL ( posix_lock_file_wait  )
EXPORT_SYMBOL ( locks_mandatory_area  )
EXPORT_SYMBOL ( lease_modify  )
EXPORT_SYMBOL ( __break_lease  )
EXPORT_SYMBOL ( lease_get_mtime  )
EXPORT_SYMBOL ( generic_setlease  )
EXPORT_SYMBOL ( flock_lock_file_wait  )
EXPORT_SYMBOL ( locks_remove_posix  )
EXPORT_SYMBOL ( posix_unblock_lock  )
EXPORT_SYMBOL ( lock_may_read  )
EXPORT_SYMBOL ( lock_may_write  )
EXPORT_SYMBOL_GPL ( lock_flocks  )
EXPORT_SYMBOL_GPL ( unlock_flocks  )
EXPORT_SYMBOL_GPL ( locks_alloc_lock  )
EXPORT_SYMBOL_GPL ( locks_release_private  )
EXPORT_SYMBOL_GPL ( vfs_setlease  )
EXPORT_SYMBOL_GPL ( vfs_test_lock  )
EXPORT_SYMBOL_GPL ( vfs_lock_file  )
EXPORT_SYMBOL_GPL ( vfs_cancel_lock  )
int fcntl_getlease ( struct file filp)

fcntl_getlease - Enquire what lease is currently active : the file

The value returned by this function will be one of (if no lease break is pending):

F_RDLCK to indicate a shared lease is held.

F_WRLCK to indicate an exclusive lease is held.

F_UNLCK to indicate no lease is held.

(if a lease break is pending):

F_RDLCK to indicate an exclusive lease needs to be changed to a shared lease (or removed).

F_UNLCK to indicate the lease needs to be removed.

XXX: sfr & willy disagree over whether F_INPROGRESS should be returned to userspace.

Definition at line 1323 of file locks.c.

int fcntl_getlk ( struct file filp,
struct flock __user l 
)

Definition at line 1724 of file locks.c.

int fcntl_setlease ( unsigned int  fd,
struct file filp,
long  arg 
)

fcntl_setlease - sets a lease on an open file : open file descriptor : file pointer

  • : type of lease to obtain

Call this fcntl to establish a lease on the file. Note that you also need to call F_SETSIG to receive a signal when the lease is broken.

Definition at line 1573 of file locks.c.

int fcntl_setlk ( unsigned int  fd,
struct file filp,
unsigned int  cmd,
struct flock __user l 
)

Definition at line 1827 of file locks.c.

int flock_lock_file_wait ( struct file filp,
struct file_lock fl 
)

flock_lock_file_wait - Apply a FLOCK-style lock to a file : The file to apply the lock to : The lock to be applied

Add a FLOCK style lock to a file.

Definition at line 1587 of file locks.c.

int generic_add_lease ( struct file filp,
long  arg,
struct file_lock **  flp 
)

Definition at line 1341 of file locks.c.

int generic_delete_lease ( struct file filp,
struct file_lock **  flp 
)

Definition at line 1406 of file locks.c.

int generic_setlease ( struct file filp,
long  arg,
struct file_lock **  flp 
)

generic_setlease - sets a lease on an open file : file pointer

The (input) flp->fl_lmops->lm_break function is required by break_lease().

Called with file_lock_lock held.

Definition at line 1433 of file locks.c.

void lease_get_mtime ( struct inode inode,
struct timespec time 
)

lease_get_mtime - get the last modified time of an inode : the inode : pointer to a timespec which will contain the last modified time

This is to force NFS clients to flush their caches for files with exclusive leases. The justification is that if someone has an exclusive lease, then they could be modifying it.

Definition at line 1289 of file locks.c.

int lease_modify ( struct file_lock **  before,
int  arg 
)

Definition at line 1129 of file locks.c.

void lock_flocks ( void  )

Definition at line 163 of file locks.c.

int lock_may_read ( struct inode inode,
loff_t  start,
unsigned long  len 
)

lock_may_read - checks that the region is free of locks : the inode that is being read : the first byte to read : the number of bytes to read

Emulates Windows locking requirements. Whole-file mandatory locks (share modes) can prohibit a read and byte-range POSIX locks can prohibit a read if they overlap.

N.B. this function is only ever called from knfsd and ownership of locks is never checked.

Definition at line 2289 of file locks.c.

int lock_may_write ( struct inode inode,
loff_t  start,
unsigned long  len 
)

lock_may_write - checks that the region is free of locks : the inode that is being written : the first byte to write : the number of bytes to write

Emulates Windows locking requirements. Whole-file mandatory locks (share modes) can prohibit a write and byte-range POSIX locks can prohibit a write if they overlap.

N.B. this function is only ever called from knfsd and ownership of locks is never checked.

Definition at line 2329 of file locks.c.

struct file_lock* locks_alloc_lock ( void  )
read

Definition at line 185 of file locks.c.

void locks_copy_lock ( struct file_lock new,
struct file_lock fl 
)

Definition at line 256 of file locks.c.

void locks_delete_block ( struct file_lock waiter)

Definition at line 499 of file locks.c.

void locks_free_lock ( struct file_lock fl)

Definition at line 209 of file locks.c.

void locks_init_lock ( struct file_lock fl)

Definition at line 220 of file locks.c.

int locks_mandatory_area ( int  read_write,
struct inode inode,
struct file filp,
loff_t  offset,
size_t  count 
)

locks_mandatory_area - Check for a conflicting lock : FLOCK_VERIFY_WRITE for exclusive access, FLOCK_VERIFY_READ for shared : the file to check : how the file was opened (if it was) : start of area to check : length of area to check

Searches the inode's list of locks to find any POSIX locks which conflict. This function is called from rw_verify_area() and locks_verify_truncate().

Definition at line 1076 of file locks.c.

int locks_mandatory_locked ( struct inode inode)

locks_mandatory_locked - Check for an active lock : the file to check

Searches the inode's list of locks to find any POSIX locks which conflict. This function is called from locks_verify_locked() only.

Definition at line 1044 of file locks.c.

void locks_release_private ( struct file_lock fl)

Definition at line 196 of file locks.c.

void locks_remove_flock ( struct file filp)

Definition at line 2057 of file locks.c.

void locks_remove_posix ( struct file filp,
fl_owner_t  owner 
)

Definition at line 2024 of file locks.c.

int posix_lock_file ( struct file filp,
struct file_lock fl,
struct file_lock conflock 
)

posix_lock_file - Apply a POSIX-style lock to a file : The file to apply the lock to : The lock to be applied : Place to return a copy of the conflicting lock, if found.

Add a POSIX style lock to a file. We merge adjacent & overlapping locks whenever possible. POSIX locks are sorted by owner task, then by starting address

Note that if called with an FL_EXISTS argument, the caller may determine whether or not a lock was successfully freed by testing the return value for -ENOENT.

Definition at line 1002 of file locks.c.

int posix_lock_file_wait ( struct file filp,
struct file_lock fl 
)

posix_lock_file_wait - Apply a POSIX-style lock to a file : The file to apply the lock to : The lock to be applied

Add a POSIX style lock to a file. We merge adjacent & overlapping locks whenever possible. POSIX locks are sorted by owner task, then by starting address

Definition at line 1018 of file locks.c.

void posix_test_lock ( struct file filp,
struct file_lock fl 
)

Definition at line 625 of file locks.c.

int posix_unblock_lock ( struct file filp,
struct file_lock waiter 
)

posix_unblock_lock - stop waiting for a file lock : how the file was opened : the lock which was waiting

lockd needs to block waiting for locks.

Definition at line 2108 of file locks.c.

SYSCALL_DEFINE2 ( flock  ,
unsigned  int,
fd  ,
unsigned  int,
cmd   
)

sys_flock: - flock() system call. : the file descriptor to lock. : the type of lock to apply.

Apply a FL_FLOCK style lock to an open file descriptor. The can be one of

LOCK_SH – a shared lock.

LOCK_EX – an exclusive lock.

LOCK_UN – remove an existing lock.

LOCK_MAND – a `mandatory' flock. This exists to emulate Windows Share Modes.

LOCK_MAND can be combined with LOCK_READ or LOCK_WRITE to allow other processes read and write access respectively.

Definition at line 1626 of file locks.c.

void unlock_flocks ( void  )

Definition at line 169 of file locks.c.

int vfs_cancel_lock ( struct file filp,
struct file_lock fl 
)

vfs_cancel_lock - file byte range unblock lock : The file to apply the unblock to : The lock to be unblocked

Used by lock managers to cancel blocked requests

Definition at line 2130 of file locks.c.

int vfs_lock_file ( struct file filp,
unsigned int  cmd,
struct file_lock fl,
struct file_lock conf 
)

vfs_lock_file - file byte range lock : The file to apply the lock to : type of locking operation (F_SETLK, F_GETLK, etc.) : The lock to be applied : Place to return a copy of the conflicting lock, if found.

A caller that doesn't care about the conflicting lock may pass NULL as the final argument.

If the filesystem defines a private ->lock() method, then will be left unchanged; so a caller that cares should initialize it to some acceptable default.

To avoid blocking kernel daemons, such as lockd, that need to acquire POSIX locks, the ->lock() interface may return asynchronously, before the lock has been granted or denied by the underlying filesystem, if (and only if) lm_grant is set. Callers expecting ->lock() to return asynchronously will only use F_SETLK, not F_SETLKW; they will set FL_SLEEP if (and only if) the request is for a blocking lock. When ->lock() does return asynchronously, it must return FILE_LOCK_DEFERRED, and call ->lm_grant() when the lock request completes. If the request is for non-blocking lock the file system should return FILE_LOCK_DEFERRED then try to get the lock and call the callback routine with the result. If the request timed out the callback routine will return a nonzero return code and the file system should release the lock. The file system is also responsible to keep a corresponding posix lock when it grants a lock so the VFS can find out which locks are locally held and do the correct lock cleanup when required. The underlying filesystem must not drop the kernel lock or call ->lm_grant() before returning to the caller with a FILE_LOCK_DEFERRED return code.

Definition at line 1791 of file locks.c.

int vfs_setlease ( struct file filp,
long  arg,
struct file_lock **  lease 
)

vfs_setlease - sets a lease on an open file : file pointer

Call this to establish a lease on the file. The (*lease)->fl_lmops->lm_break operation must be set; if not, break_lease will oops!

This will call the filesystem's setlease file method, if defined. Note that there is no getlease method; instead, the filesystem setlease method should call back to setlease() to add a lease to the inode's lease list, where fcntl_getlease() can find it. Since fcntl_getlease() only reports whether the current task holds a lease, a cluster filesystem need only do this for leases held by processes on this node.

There is also no break_lease method; filesystems that handle their own leases should break leases themselves from the filesystem's open, create, and (on truncate) setattr methods.

Warning: the only current setlease methods exist only to disable leases in certain cases. More vfs changes may be required to allow a full filesystem lease implementation.

Definition at line 1498 of file locks.c.

int vfs_test_lock ( struct file filp,
struct file_lock fl 
)

vfs_test_lock - test file byte range lock : The file to test lock for : The lock to test; also used to hold result

Returns -ERRNO on failure. Indicates presence of conflicting lock by setting conf->fl_type to something other than F_UNLCK.

Definition at line 1679 of file locks.c.

Variable Documentation

int lease_break_time = 45

Definition at line 151 of file locks.c.

int leases_enable = 1

Definition at line 150 of file locks.c.