11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/fs/locks.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Provide support for fcntl()'s F_GETLK, F_SETLK, and F_SETLKW calls. 51da177e4SLinus Torvalds * Doug Evans (dje@spiff.uucp), August 07, 1992 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * Deadlock detection added. 81da177e4SLinus Torvalds * FIXME: one thing isn't handled yet: 91da177e4SLinus Torvalds * - mandatory locks (requires lots of changes elsewhere) 101da177e4SLinus Torvalds * Kelly Carmichael (kelly@[142.24.8.65]), September 17, 1994. 111da177e4SLinus Torvalds * 121da177e4SLinus Torvalds * Miscellaneous edits, and a total rewrite of posix_lock_file() code. 131da177e4SLinus Torvalds * Kai Petzke (wpp@marie.physik.tu-berlin.de), 1994 141da177e4SLinus Torvalds * 151da177e4SLinus Torvalds * Converted file_lock_table to a linked list from an array, which eliminates 161da177e4SLinus Torvalds * the limits on how many active file locks are open. 171da177e4SLinus Torvalds * Chad Page (pageone@netcom.com), November 27, 1994 181da177e4SLinus Torvalds * 191da177e4SLinus Torvalds * Removed dependency on file descriptors. dup()'ed file descriptors now 201da177e4SLinus Torvalds * get the same locks as the original file descriptors, and a close() on 211da177e4SLinus Torvalds * any file descriptor removes ALL the locks on the file for the current 221da177e4SLinus Torvalds * process. Since locks still depend on the process id, locks are inherited 231da177e4SLinus Torvalds * after an exec() but not after a fork(). This agrees with POSIX, and both 241da177e4SLinus Torvalds * BSD and SVR4 practice. 251da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), February 14, 1995 261da177e4SLinus Torvalds * 271da177e4SLinus Torvalds * Scrapped free list which is redundant now that we allocate locks 281da177e4SLinus Torvalds * dynamically with kmalloc()/kfree(). 291da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), February 21, 1995 301da177e4SLinus Torvalds * 311da177e4SLinus Torvalds * Implemented two lock personalities - FL_FLOCK and FL_POSIX. 321da177e4SLinus Torvalds * 331da177e4SLinus Torvalds * FL_POSIX locks are created with calls to fcntl() and lockf() through the 341da177e4SLinus Torvalds * fcntl() system call. They have the semantics described above. 351da177e4SLinus Torvalds * 361da177e4SLinus Torvalds * FL_FLOCK locks are created with calls to flock(), through the flock() 371da177e4SLinus Torvalds * system call, which is new. Old C libraries implement flock() via fcntl() 381da177e4SLinus Torvalds * and will continue to use the old, broken implementation. 391da177e4SLinus Torvalds * 401da177e4SLinus Torvalds * FL_FLOCK locks follow the 4.4 BSD flock() semantics. They are associated 411da177e4SLinus Torvalds * with a file pointer (filp). As a result they can be shared by a parent 421da177e4SLinus Torvalds * process and its children after a fork(). They are removed when the last 431da177e4SLinus Torvalds * file descriptor referring to the file pointer is closed (unless explicitly 441da177e4SLinus Torvalds * unlocked). 451da177e4SLinus Torvalds * 461da177e4SLinus Torvalds * FL_FLOCK locks never deadlock, an existing lock is always removed before 471da177e4SLinus Torvalds * upgrading from shared to exclusive (or vice versa). When this happens 481da177e4SLinus Torvalds * any processes blocked by the current lock are woken up and allowed to 491da177e4SLinus Torvalds * run before the new lock is applied. 501da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), June 09, 1995 511da177e4SLinus Torvalds * 521da177e4SLinus Torvalds * Removed some race conditions in flock_lock_file(), marked other possible 531da177e4SLinus Torvalds * races. Just grep for FIXME to see them. 541da177e4SLinus Torvalds * Dmitry Gorodchanin (pgmdsg@ibi.com), February 09, 1996. 551da177e4SLinus Torvalds * 561da177e4SLinus Torvalds * Addressed Dmitry's concerns. Deadlock checking no longer recursive. 571da177e4SLinus Torvalds * Lock allocation changed to GFP_ATOMIC as we can't afford to sleep 581da177e4SLinus Torvalds * once we've checked for blocking and deadlocking. 591da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), April 03, 1996. 601da177e4SLinus Torvalds * 611da177e4SLinus Torvalds * Initial implementation of mandatory locks. SunOS turned out to be 621da177e4SLinus Torvalds * a rotten model, so I implemented the "obvious" semantics. 63395cf969SPaul Bolle * See 'Documentation/filesystems/mandatory-locking.txt' for details. 641da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), April 06, 1996. 651da177e4SLinus Torvalds * 661da177e4SLinus Torvalds * Don't allow mandatory locks on mmap()'ed files. Added simple functions to 671da177e4SLinus Torvalds * check if a file has mandatory locks, used by mmap(), open() and creat() to 681da177e4SLinus Torvalds * see if system call should be rejected. Ref. HP-UX/SunOS/Solaris Reference 691da177e4SLinus Torvalds * Manual, Section 2. 701da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), April 09, 1996. 711da177e4SLinus Torvalds * 721da177e4SLinus Torvalds * Tidied up block list handling. Added '/proc/locks' interface. 731da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), April 24, 1996. 741da177e4SLinus Torvalds * 751da177e4SLinus Torvalds * Fixed deadlock condition for pathological code that mixes calls to 761da177e4SLinus Torvalds * flock() and fcntl(). 771da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), April 29, 1996. 781da177e4SLinus Torvalds * 791da177e4SLinus Torvalds * Allow only one type of locking scheme (FL_POSIX or FL_FLOCK) to be in use 801da177e4SLinus Torvalds * for a given file at a time. Changed the CONFIG_LOCK_MANDATORY scheme to 811da177e4SLinus Torvalds * guarantee sensible behaviour in the case where file system modules might 821da177e4SLinus Torvalds * be compiled with different options than the kernel itself. 831da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), May 15, 1996. 841da177e4SLinus Torvalds * 851da177e4SLinus Torvalds * Added a couple of missing wake_up() calls. Thanks to Thomas Meckel 861da177e4SLinus Torvalds * (Thomas.Meckel@mni.fh-giessen.de) for spotting this. 871da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), May 15, 1996. 881da177e4SLinus Torvalds * 891da177e4SLinus Torvalds * Changed FL_POSIX locks to use the block list in the same way as FL_FLOCK 901da177e4SLinus Torvalds * locks. Changed process synchronisation to avoid dereferencing locks that 911da177e4SLinus Torvalds * have already been freed. 921da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), Sep 21, 1996. 931da177e4SLinus Torvalds * 941da177e4SLinus Torvalds * Made the block list a circular list to minimise searching in the list. 951da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), Sep 25, 1996. 961da177e4SLinus Torvalds * 971da177e4SLinus Torvalds * Made mandatory locking a mount option. Default is not to allow mandatory 981da177e4SLinus Torvalds * locking. 991da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), Oct 04, 1996. 1001da177e4SLinus Torvalds * 1011da177e4SLinus Torvalds * Some adaptations for NFS support. 1021da177e4SLinus Torvalds * Olaf Kirch (okir@monad.swb.de), Dec 1996, 1031da177e4SLinus Torvalds * 1041da177e4SLinus Torvalds * Fixed /proc/locks interface so that we can't overrun the buffer we are handed. 1051da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), May 12, 1997. 1061da177e4SLinus Torvalds * 1071da177e4SLinus Torvalds * Use slab allocator instead of kmalloc/kfree. 1081da177e4SLinus Torvalds * Use generic list implementation from <linux/list.h>. 1091da177e4SLinus Torvalds * Sped up posix_locks_deadlock by only considering blocked locks. 1101da177e4SLinus Torvalds * Matthew Wilcox <willy@debian.org>, March, 2000. 1111da177e4SLinus Torvalds * 1121da177e4SLinus Torvalds * Leases and LOCK_MAND 1131da177e4SLinus Torvalds * Matthew Wilcox <willy@debian.org>, June, 2000. 1141da177e4SLinus Torvalds * Stephen Rothwell <sfr@canb.auug.org.au>, June, 2000. 1151da177e4SLinus Torvalds */ 1161da177e4SLinus Torvalds 1171da177e4SLinus Torvalds #include <linux/capability.h> 1181da177e4SLinus Torvalds #include <linux/file.h> 1199f3acc31SAl Viro #include <linux/fdtable.h> 1201da177e4SLinus Torvalds #include <linux/fs.h> 1211da177e4SLinus Torvalds #include <linux/init.h> 1221da177e4SLinus Torvalds #include <linux/module.h> 1231da177e4SLinus Torvalds #include <linux/security.h> 1241da177e4SLinus Torvalds #include <linux/slab.h> 1251da177e4SLinus Torvalds #include <linux/syscalls.h> 1261da177e4SLinus Torvalds #include <linux/time.h> 1274fb3a538SDipankar Sarma #include <linux/rcupdate.h> 128ab1f1611SVitaliy Gusev #include <linux/pid_namespace.h> 12948f74186SJeff Layton #include <linux/hashtable.h> 1307012b02aSJeff Layton #include <linux/percpu.h> 1317012b02aSJeff Layton #include <linux/lglock.h> 1321da177e4SLinus Torvalds 13362af4f1fSJeff Layton #define CREATE_TRACE_POINTS 13462af4f1fSJeff Layton #include <trace/events/filelock.h> 13562af4f1fSJeff Layton 1361da177e4SLinus Torvalds #include <asm/uaccess.h> 1371da177e4SLinus Torvalds 1381da177e4SLinus Torvalds #define IS_POSIX(fl) (fl->fl_flags & FL_POSIX) 1391da177e4SLinus Torvalds #define IS_FLOCK(fl) (fl->fl_flags & FL_FLOCK) 140617588d5SJ. Bruce Fields #define IS_LEASE(fl) (fl->fl_flags & (FL_LEASE|FL_DELEG)) 141cff2fce5SJeff Layton #define IS_OFDLCK(fl) (fl->fl_flags & FL_OFDLCK) 1421da177e4SLinus Torvalds 143ab83fa4bSJ. Bruce Fields static bool lease_breaking(struct file_lock *fl) 144ab83fa4bSJ. Bruce Fields { 145778fc546SJ. Bruce Fields return fl->fl_flags & (FL_UNLOCK_PENDING | FL_DOWNGRADE_PENDING); 146778fc546SJ. Bruce Fields } 147778fc546SJ. Bruce Fields 148778fc546SJ. Bruce Fields static int target_leasetype(struct file_lock *fl) 149778fc546SJ. Bruce Fields { 150778fc546SJ. Bruce Fields if (fl->fl_flags & FL_UNLOCK_PENDING) 151778fc546SJ. Bruce Fields return F_UNLCK; 152778fc546SJ. Bruce Fields if (fl->fl_flags & FL_DOWNGRADE_PENDING) 153778fc546SJ. Bruce Fields return F_RDLCK; 154778fc546SJ. Bruce Fields return fl->fl_type; 155ab83fa4bSJ. Bruce Fields } 156ab83fa4bSJ. Bruce Fields 1571da177e4SLinus Torvalds int leases_enable = 1; 1581da177e4SLinus Torvalds int lease_break_time = 45; 1591da177e4SLinus Torvalds 1601da177e4SLinus Torvalds #define for_each_lock(inode, lockp) \ 1611da177e4SLinus Torvalds for (lockp = &inode->i_flock; *lockp != NULL; lockp = &(*lockp)->fl_next) 1621da177e4SLinus Torvalds 1631c8c601aSJeff Layton /* 1647012b02aSJeff Layton * The global file_lock_list is only used for displaying /proc/locks, so we 1657012b02aSJeff Layton * keep a list on each CPU, with each list protected by its own spinlock via 1667012b02aSJeff Layton * the file_lock_lglock. Note that alterations to the list also require that 1677012b02aSJeff Layton * the relevant i_lock is held. 1681c8c601aSJeff Layton */ 1697012b02aSJeff Layton DEFINE_STATIC_LGLOCK(file_lock_lglock); 1707012b02aSJeff Layton static DEFINE_PER_CPU(struct hlist_head, file_lock_list); 17188974691SJeff Layton 1721c8c601aSJeff Layton /* 17348f74186SJeff Layton * The blocked_hash is used to find POSIX lock loops for deadlock detection. 1747b2296afSJeff Layton * It is protected by blocked_lock_lock. 17548f74186SJeff Layton * 17648f74186SJeff Layton * We hash locks by lockowner in order to optimize searching for the lock a 17748f74186SJeff Layton * particular lockowner is waiting on. 17848f74186SJeff Layton * 17948f74186SJeff Layton * FIXME: make this value scale via some heuristic? We generally will want more 18048f74186SJeff Layton * buckets when we have more lockowners holding locks, but that's a little 18148f74186SJeff Layton * difficult to determine without knowing what the workload will look like. 1821c8c601aSJeff Layton */ 18348f74186SJeff Layton #define BLOCKED_HASH_BITS 7 18448f74186SJeff Layton static DEFINE_HASHTABLE(blocked_hash, BLOCKED_HASH_BITS); 18588974691SJeff Layton 1861c8c601aSJeff Layton /* 1877b2296afSJeff Layton * This lock protects the blocked_hash. Generally, if you're accessing it, you 1887b2296afSJeff Layton * want to be holding this lock. 1891c8c601aSJeff Layton * 1901c8c601aSJeff Layton * In addition, it also protects the fl->fl_block list, and the fl->fl_next 1911c8c601aSJeff Layton * pointer for file_lock structures that are acting as lock requests (in 1921c8c601aSJeff Layton * contrast to those that are acting as records of acquired locks). 1931c8c601aSJeff Layton * 1941c8c601aSJeff Layton * Note that when we acquire this lock in order to change the above fields, 1951c8c601aSJeff Layton * we often hold the i_lock as well. In certain cases, when reading the fields 1961c8c601aSJeff Layton * protected by this lock, we can skip acquiring it iff we already hold the 1971c8c601aSJeff Layton * i_lock. 1981c8c601aSJeff Layton * 1991c8c601aSJeff Layton * In particular, adding an entry to the fl_block list requires that you hold 2001c8c601aSJeff Layton * both the i_lock and the blocked_lock_lock (acquired in that order). Deleting 2011c8c601aSJeff Layton * an entry from the list however only requires the file_lock_lock. 2021c8c601aSJeff Layton */ 2037b2296afSJeff Layton static DEFINE_SPINLOCK(blocked_lock_lock); 2041da177e4SLinus Torvalds 205e18b890bSChristoph Lameter static struct kmem_cache *filelock_cache __read_mostly; 2061da177e4SLinus Torvalds 207ee19cc40SMiklos Szeredi static void locks_init_lock_heads(struct file_lock *fl) 208a51cb91dSMiklos Szeredi { 209139ca04eSJeff Layton INIT_HLIST_NODE(&fl->fl_link); 210ee19cc40SMiklos Szeredi INIT_LIST_HEAD(&fl->fl_block); 211ee19cc40SMiklos Szeredi init_waitqueue_head(&fl->fl_wait); 212a51cb91dSMiklos Szeredi } 213a51cb91dSMiklos Szeredi 2141da177e4SLinus Torvalds /* Allocate an empty lock structure. */ 215c5b1f0d9SArnd Bergmann struct file_lock *locks_alloc_lock(void) 2161da177e4SLinus Torvalds { 217ee19cc40SMiklos Szeredi struct file_lock *fl = kmem_cache_zalloc(filelock_cache, GFP_KERNEL); 218a51cb91dSMiklos Szeredi 219a51cb91dSMiklos Szeredi if (fl) 220ee19cc40SMiklos Szeredi locks_init_lock_heads(fl); 221a51cb91dSMiklos Szeredi 222a51cb91dSMiklos Szeredi return fl; 2231da177e4SLinus Torvalds } 224c5b1f0d9SArnd Bergmann EXPORT_SYMBOL_GPL(locks_alloc_lock); 2251da177e4SLinus Torvalds 226a9e61e25SFelix Blyakher void locks_release_private(struct file_lock *fl) 22747831f35STrond Myklebust { 22847831f35STrond Myklebust if (fl->fl_ops) { 22947831f35STrond Myklebust if (fl->fl_ops->fl_release_private) 23047831f35STrond Myklebust fl->fl_ops->fl_release_private(fl); 23147831f35STrond Myklebust fl->fl_ops = NULL; 23247831f35STrond Myklebust } 23347831f35STrond Myklebust 2345c97d7b1SKinglong Mee if (fl->fl_lmops) { 2355c97d7b1SKinglong Mee if (fl->fl_lmops->lm_put_owner) 2365c97d7b1SKinglong Mee fl->fl_lmops->lm_put_owner(fl); 2375c97d7b1SKinglong Mee fl->fl_lmops = NULL; 2385c97d7b1SKinglong Mee } 23947831f35STrond Myklebust } 240a9e61e25SFelix Blyakher EXPORT_SYMBOL_GPL(locks_release_private); 24147831f35STrond Myklebust 2421da177e4SLinus Torvalds /* Free a lock which is not in use. */ 24305fa3135SJ. Bruce Fields void locks_free_lock(struct file_lock *fl) 2441da177e4SLinus Torvalds { 2455ce29646SMiklos Szeredi BUG_ON(waitqueue_active(&fl->fl_wait)); 2465ce29646SMiklos Szeredi BUG_ON(!list_empty(&fl->fl_block)); 247139ca04eSJeff Layton BUG_ON(!hlist_unhashed(&fl->fl_link)); 2481da177e4SLinus Torvalds 24947831f35STrond Myklebust locks_release_private(fl); 2501da177e4SLinus Torvalds kmem_cache_free(filelock_cache, fl); 2511da177e4SLinus Torvalds } 25205fa3135SJ. Bruce Fields EXPORT_SYMBOL(locks_free_lock); 2531da177e4SLinus Torvalds 254ed9814d8SJeff Layton static void 255ed9814d8SJeff Layton locks_dispose_list(struct list_head *dispose) 256ed9814d8SJeff Layton { 257ed9814d8SJeff Layton struct file_lock *fl; 258ed9814d8SJeff Layton 259ed9814d8SJeff Layton while (!list_empty(dispose)) { 260ed9814d8SJeff Layton fl = list_first_entry(dispose, struct file_lock, fl_block); 261ed9814d8SJeff Layton list_del_init(&fl->fl_block); 262ed9814d8SJeff Layton locks_free_lock(fl); 263ed9814d8SJeff Layton } 264ed9814d8SJeff Layton } 265ed9814d8SJeff Layton 2661da177e4SLinus Torvalds void locks_init_lock(struct file_lock *fl) 2671da177e4SLinus Torvalds { 268ee19cc40SMiklos Szeredi memset(fl, 0, sizeof(struct file_lock)); 269ee19cc40SMiklos Szeredi locks_init_lock_heads(fl); 2701da177e4SLinus Torvalds } 2711da177e4SLinus Torvalds 2721da177e4SLinus Torvalds EXPORT_SYMBOL(locks_init_lock); 2731da177e4SLinus Torvalds 2741da177e4SLinus Torvalds /* 2751da177e4SLinus Torvalds * Initialize a new lock from an existing file_lock structure. 2761da177e4SLinus Torvalds */ 2773fe0fff1SKinglong Mee void locks_copy_conflock(struct file_lock *new, struct file_lock *fl) 2781da177e4SLinus Torvalds { 2791da177e4SLinus Torvalds new->fl_owner = fl->fl_owner; 2801da177e4SLinus Torvalds new->fl_pid = fl->fl_pid; 2810996905fSTrond Myklebust new->fl_file = NULL; 2821da177e4SLinus Torvalds new->fl_flags = fl->fl_flags; 2831da177e4SLinus Torvalds new->fl_type = fl->fl_type; 2841da177e4SLinus Torvalds new->fl_start = fl->fl_start; 2851da177e4SLinus Torvalds new->fl_end = fl->fl_end; 286f328296eSKinglong Mee new->fl_lmops = fl->fl_lmops; 2870996905fSTrond Myklebust new->fl_ops = NULL; 288f328296eSKinglong Mee 289f328296eSKinglong Mee if (fl->fl_lmops) { 290f328296eSKinglong Mee if (fl->fl_lmops->lm_get_owner) 291f328296eSKinglong Mee fl->fl_lmops->lm_get_owner(new, fl); 292f328296eSKinglong Mee } 2930996905fSTrond Myklebust } 2943fe0fff1SKinglong Mee EXPORT_SYMBOL(locks_copy_conflock); 2950996905fSTrond Myklebust 2960996905fSTrond Myklebust void locks_copy_lock(struct file_lock *new, struct file_lock *fl) 2970996905fSTrond Myklebust { 298566709bdSJeff Layton /* "new" must be a freshly-initialized lock */ 299566709bdSJeff Layton WARN_ON_ONCE(new->fl_ops); 3000996905fSTrond Myklebust 3013fe0fff1SKinglong Mee locks_copy_conflock(new, fl); 302f328296eSKinglong Mee 3030996905fSTrond Myklebust new->fl_file = fl->fl_file; 3041da177e4SLinus Torvalds new->fl_ops = fl->fl_ops; 30547831f35STrond Myklebust 306f328296eSKinglong Mee if (fl->fl_ops) { 307f328296eSKinglong Mee if (fl->fl_ops->fl_copy_lock) 308f328296eSKinglong Mee fl->fl_ops->fl_copy_lock(new, fl); 309f328296eSKinglong Mee } 3101da177e4SLinus Torvalds } 3111da177e4SLinus Torvalds 3121da177e4SLinus Torvalds EXPORT_SYMBOL(locks_copy_lock); 3131da177e4SLinus Torvalds 3141da177e4SLinus Torvalds static inline int flock_translate_cmd(int cmd) { 3151da177e4SLinus Torvalds if (cmd & LOCK_MAND) 3161da177e4SLinus Torvalds return cmd & (LOCK_MAND | LOCK_RW); 3171da177e4SLinus Torvalds switch (cmd) { 3181da177e4SLinus Torvalds case LOCK_SH: 3191da177e4SLinus Torvalds return F_RDLCK; 3201da177e4SLinus Torvalds case LOCK_EX: 3211da177e4SLinus Torvalds return F_WRLCK; 3221da177e4SLinus Torvalds case LOCK_UN: 3231da177e4SLinus Torvalds return F_UNLCK; 3241da177e4SLinus Torvalds } 3251da177e4SLinus Torvalds return -EINVAL; 3261da177e4SLinus Torvalds } 3271da177e4SLinus Torvalds 3281da177e4SLinus Torvalds /* Fill in a file_lock structure with an appropriate FLOCK lock. */ 3291da177e4SLinus Torvalds static int flock_make_lock(struct file *filp, struct file_lock **lock, 3301da177e4SLinus Torvalds unsigned int cmd) 3311da177e4SLinus Torvalds { 3321da177e4SLinus Torvalds struct file_lock *fl; 3331da177e4SLinus Torvalds int type = flock_translate_cmd(cmd); 3341da177e4SLinus Torvalds if (type < 0) 3351da177e4SLinus Torvalds return type; 3361da177e4SLinus Torvalds 3371da177e4SLinus Torvalds fl = locks_alloc_lock(); 3381da177e4SLinus Torvalds if (fl == NULL) 3391da177e4SLinus Torvalds return -ENOMEM; 3401da177e4SLinus Torvalds 3411da177e4SLinus Torvalds fl->fl_file = filp; 34273a8f5f7SChristoph Hellwig fl->fl_owner = filp; 3431da177e4SLinus Torvalds fl->fl_pid = current->tgid; 3441da177e4SLinus Torvalds fl->fl_flags = FL_FLOCK; 3451da177e4SLinus Torvalds fl->fl_type = type; 3461da177e4SLinus Torvalds fl->fl_end = OFFSET_MAX; 3471da177e4SLinus Torvalds 3481da177e4SLinus Torvalds *lock = fl; 3491da177e4SLinus Torvalds return 0; 3501da177e4SLinus Torvalds } 3511da177e4SLinus Torvalds 3520ec4f431SJ. Bruce Fields static int assign_type(struct file_lock *fl, long type) 3531da177e4SLinus Torvalds { 3541da177e4SLinus Torvalds switch (type) { 3551da177e4SLinus Torvalds case F_RDLCK: 3561da177e4SLinus Torvalds case F_WRLCK: 3571da177e4SLinus Torvalds case F_UNLCK: 3581da177e4SLinus Torvalds fl->fl_type = type; 3591da177e4SLinus Torvalds break; 3601da177e4SLinus Torvalds default: 3611da177e4SLinus Torvalds return -EINVAL; 3621da177e4SLinus Torvalds } 3631da177e4SLinus Torvalds return 0; 3641da177e4SLinus Torvalds } 3651da177e4SLinus Torvalds 366ef12e72aSJ. Bruce Fields static int flock64_to_posix_lock(struct file *filp, struct file_lock *fl, 367ef12e72aSJ. Bruce Fields struct flock64 *l) 368ef12e72aSJ. Bruce Fields { 369ef12e72aSJ. Bruce Fields switch (l->l_whence) { 370ef12e72aSJ. Bruce Fields case SEEK_SET: 371ef12e72aSJ. Bruce Fields fl->fl_start = 0; 372ef12e72aSJ. Bruce Fields break; 373ef12e72aSJ. Bruce Fields case SEEK_CUR: 374ef12e72aSJ. Bruce Fields fl->fl_start = filp->f_pos; 375ef12e72aSJ. Bruce Fields break; 376ef12e72aSJ. Bruce Fields case SEEK_END: 377ef12e72aSJ. Bruce Fields fl->fl_start = i_size_read(file_inode(filp)); 378ef12e72aSJ. Bruce Fields break; 379ef12e72aSJ. Bruce Fields default: 380ef12e72aSJ. Bruce Fields return -EINVAL; 381ef12e72aSJ. Bruce Fields } 382ef12e72aSJ. Bruce Fields if (l->l_start > OFFSET_MAX - fl->fl_start) 383ef12e72aSJ. Bruce Fields return -EOVERFLOW; 384ef12e72aSJ. Bruce Fields fl->fl_start += l->l_start; 385ef12e72aSJ. Bruce Fields if (fl->fl_start < 0) 386ef12e72aSJ. Bruce Fields return -EINVAL; 387ef12e72aSJ. Bruce Fields 388ef12e72aSJ. Bruce Fields /* POSIX-1996 leaves the case l->l_len < 0 undefined; 389ef12e72aSJ. Bruce Fields POSIX-2001 defines it. */ 390ef12e72aSJ. Bruce Fields if (l->l_len > 0) { 391ef12e72aSJ. Bruce Fields if (l->l_len - 1 > OFFSET_MAX - fl->fl_start) 392ef12e72aSJ. Bruce Fields return -EOVERFLOW; 393ef12e72aSJ. Bruce Fields fl->fl_end = fl->fl_start + l->l_len - 1; 394ef12e72aSJ. Bruce Fields 395ef12e72aSJ. Bruce Fields } else if (l->l_len < 0) { 396ef12e72aSJ. Bruce Fields if (fl->fl_start + l->l_len < 0) 397ef12e72aSJ. Bruce Fields return -EINVAL; 398ef12e72aSJ. Bruce Fields fl->fl_end = fl->fl_start - 1; 399ef12e72aSJ. Bruce Fields fl->fl_start += l->l_len; 400ef12e72aSJ. Bruce Fields } else 401ef12e72aSJ. Bruce Fields fl->fl_end = OFFSET_MAX; 402ef12e72aSJ. Bruce Fields 403ef12e72aSJ. Bruce Fields fl->fl_owner = current->files; 404ef12e72aSJ. Bruce Fields fl->fl_pid = current->tgid; 405ef12e72aSJ. Bruce Fields fl->fl_file = filp; 406ef12e72aSJ. Bruce Fields fl->fl_flags = FL_POSIX; 407ef12e72aSJ. Bruce Fields fl->fl_ops = NULL; 408ef12e72aSJ. Bruce Fields fl->fl_lmops = NULL; 409ef12e72aSJ. Bruce Fields 410ef12e72aSJ. Bruce Fields return assign_type(fl, l->l_type); 411ef12e72aSJ. Bruce Fields } 412ef12e72aSJ. Bruce Fields 4131da177e4SLinus Torvalds /* Verify a "struct flock" and copy it to a "struct file_lock" as a POSIX 4141da177e4SLinus Torvalds * style lock. 4151da177e4SLinus Torvalds */ 4161da177e4SLinus Torvalds static int flock_to_posix_lock(struct file *filp, struct file_lock *fl, 4171da177e4SLinus Torvalds struct flock *l) 4181da177e4SLinus Torvalds { 419ef12e72aSJ. Bruce Fields struct flock64 ll = { 420ef12e72aSJ. Bruce Fields .l_type = l->l_type, 421ef12e72aSJ. Bruce Fields .l_whence = l->l_whence, 422ef12e72aSJ. Bruce Fields .l_start = l->l_start, 423ef12e72aSJ. Bruce Fields .l_len = l->l_len, 424ef12e72aSJ. Bruce Fields }; 4251da177e4SLinus Torvalds 426ef12e72aSJ. Bruce Fields return flock64_to_posix_lock(filp, fl, &ll); 4271da177e4SLinus Torvalds } 4281da177e4SLinus Torvalds 4291da177e4SLinus Torvalds /* default lease lock manager operations */ 430*4d01b7f5SJeff Layton static bool 431*4d01b7f5SJeff Layton lease_break_callback(struct file_lock *fl) 4321da177e4SLinus Torvalds { 4331da177e4SLinus Torvalds kill_fasync(&fl->fl_fasync, SIGIO, POLL_MSG); 434*4d01b7f5SJeff Layton return false; 4351da177e4SLinus Torvalds } 4361da177e4SLinus Torvalds 4371c7dd2ffSJeff Layton static void 4381c7dd2ffSJeff Layton lease_setup(struct file_lock *fl, void **priv) 4391c7dd2ffSJeff Layton { 4401c7dd2ffSJeff Layton struct file *filp = fl->fl_file; 4411c7dd2ffSJeff Layton struct fasync_struct *fa = *priv; 4421c7dd2ffSJeff Layton 4431c7dd2ffSJeff Layton /* 4441c7dd2ffSJeff Layton * fasync_insert_entry() returns the old entry if any. If there was no 4451c7dd2ffSJeff Layton * old entry, then it used "priv" and inserted it into the fasync list. 4461c7dd2ffSJeff Layton * Clear the pointer to indicate that it shouldn't be freed. 4471c7dd2ffSJeff Layton */ 4481c7dd2ffSJeff Layton if (!fasync_insert_entry(fa->fa_fd, filp, &fl->fl_fasync, fa)) 4491c7dd2ffSJeff Layton *priv = NULL; 4501c7dd2ffSJeff Layton 4511c7dd2ffSJeff Layton __f_setown(filp, task_pid(current), PIDTYPE_PID, 0); 4521c7dd2ffSJeff Layton } 4531c7dd2ffSJeff Layton 4547b021967SAlexey Dobriyan static const struct lock_manager_operations lease_manager_ops = { 4558fb47a4fSJ. Bruce Fields .lm_break = lease_break_callback, 4568fb47a4fSJ. Bruce Fields .lm_change = lease_modify, 4571c7dd2ffSJeff Layton .lm_setup = lease_setup, 4581da177e4SLinus Torvalds }; 4591da177e4SLinus Torvalds 4601da177e4SLinus Torvalds /* 4611da177e4SLinus Torvalds * Initialize a lease, use the default lock manager operations 4621da177e4SLinus Torvalds */ 4630ec4f431SJ. Bruce Fields static int lease_init(struct file *filp, long type, struct file_lock *fl) 4641da177e4SLinus Torvalds { 46575dff55aSTrond Myklebust if (assign_type(fl, type) != 0) 46675dff55aSTrond Myklebust return -EINVAL; 46775dff55aSTrond Myklebust 46873a8f5f7SChristoph Hellwig fl->fl_owner = current->files; 4691da177e4SLinus Torvalds fl->fl_pid = current->tgid; 4701da177e4SLinus Torvalds 4711da177e4SLinus Torvalds fl->fl_file = filp; 4721da177e4SLinus Torvalds fl->fl_flags = FL_LEASE; 4731da177e4SLinus Torvalds fl->fl_start = 0; 4741da177e4SLinus Torvalds fl->fl_end = OFFSET_MAX; 4751da177e4SLinus Torvalds fl->fl_ops = NULL; 4761da177e4SLinus Torvalds fl->fl_lmops = &lease_manager_ops; 4771da177e4SLinus Torvalds return 0; 4781da177e4SLinus Torvalds } 4791da177e4SLinus Torvalds 4801da177e4SLinus Torvalds /* Allocate a file_lock initialised to this type of lease */ 4810ec4f431SJ. Bruce Fields static struct file_lock *lease_alloc(struct file *filp, long type) 4821da177e4SLinus Torvalds { 4831da177e4SLinus Torvalds struct file_lock *fl = locks_alloc_lock(); 48475dff55aSTrond Myklebust int error = -ENOMEM; 4851da177e4SLinus Torvalds 4861da177e4SLinus Torvalds if (fl == NULL) 487e32b8ee2SJ. Bruce Fields return ERR_PTR(error); 4881da177e4SLinus Torvalds 4891da177e4SLinus Torvalds error = lease_init(filp, type, fl); 49075dff55aSTrond Myklebust if (error) { 49175dff55aSTrond Myklebust locks_free_lock(fl); 492e32b8ee2SJ. Bruce Fields return ERR_PTR(error); 49375dff55aSTrond Myklebust } 494e32b8ee2SJ. Bruce Fields return fl; 4951da177e4SLinus Torvalds } 4961da177e4SLinus Torvalds 4971da177e4SLinus Torvalds /* Check if two locks overlap each other. 4981da177e4SLinus Torvalds */ 4991da177e4SLinus Torvalds static inline int locks_overlap(struct file_lock *fl1, struct file_lock *fl2) 5001da177e4SLinus Torvalds { 5011da177e4SLinus Torvalds return ((fl1->fl_end >= fl2->fl_start) && 5021da177e4SLinus Torvalds (fl2->fl_end >= fl1->fl_start)); 5031da177e4SLinus Torvalds } 5041da177e4SLinus Torvalds 5051da177e4SLinus Torvalds /* 5061da177e4SLinus Torvalds * Check whether two locks have the same owner. 5071da177e4SLinus Torvalds */ 50833443c42SMatt Mackall static int posix_same_owner(struct file_lock *fl1, struct file_lock *fl2) 5091da177e4SLinus Torvalds { 5108fb47a4fSJ. Bruce Fields if (fl1->fl_lmops && fl1->fl_lmops->lm_compare_owner) 5111da177e4SLinus Torvalds return fl2->fl_lmops == fl1->fl_lmops && 5128fb47a4fSJ. Bruce Fields fl1->fl_lmops->lm_compare_owner(fl1, fl2); 5131da177e4SLinus Torvalds return fl1->fl_owner == fl2->fl_owner; 5141da177e4SLinus Torvalds } 5151da177e4SLinus Torvalds 5167012b02aSJeff Layton /* Must be called with the i_lock held! */ 5176ca10ed8SJeff Layton static void locks_insert_global_locks(struct file_lock *fl) 51888974691SJeff Layton { 5197012b02aSJeff Layton lg_local_lock(&file_lock_lglock); 5207012b02aSJeff Layton fl->fl_link_cpu = smp_processor_id(); 5217012b02aSJeff Layton hlist_add_head(&fl->fl_link, this_cpu_ptr(&file_lock_list)); 5227012b02aSJeff Layton lg_local_unlock(&file_lock_lglock); 52388974691SJeff Layton } 52488974691SJeff Layton 5257012b02aSJeff Layton /* Must be called with the i_lock held! */ 5266ca10ed8SJeff Layton static void locks_delete_global_locks(struct file_lock *fl) 52788974691SJeff Layton { 5287012b02aSJeff Layton /* 5297012b02aSJeff Layton * Avoid taking lock if already unhashed. This is safe since this check 5307012b02aSJeff Layton * is done while holding the i_lock, and new insertions into the list 5317012b02aSJeff Layton * also require that it be held. 5327012b02aSJeff Layton */ 5337012b02aSJeff Layton if (hlist_unhashed(&fl->fl_link)) 5347012b02aSJeff Layton return; 5357012b02aSJeff Layton lg_local_lock_cpu(&file_lock_lglock, fl->fl_link_cpu); 536139ca04eSJeff Layton hlist_del_init(&fl->fl_link); 5377012b02aSJeff Layton lg_local_unlock_cpu(&file_lock_lglock, fl->fl_link_cpu); 53888974691SJeff Layton } 53988974691SJeff Layton 5403999e493SJeff Layton static unsigned long 5413999e493SJeff Layton posix_owner_key(struct file_lock *fl) 5423999e493SJeff Layton { 5433999e493SJeff Layton if (fl->fl_lmops && fl->fl_lmops->lm_owner_key) 5443999e493SJeff Layton return fl->fl_lmops->lm_owner_key(fl); 5453999e493SJeff Layton return (unsigned long)fl->fl_owner; 5463999e493SJeff Layton } 5473999e493SJeff Layton 5486ca10ed8SJeff Layton static void locks_insert_global_blocked(struct file_lock *waiter) 54988974691SJeff Layton { 5503999e493SJeff Layton hash_add(blocked_hash, &waiter->fl_link, posix_owner_key(waiter)); 55188974691SJeff Layton } 55288974691SJeff Layton 5536ca10ed8SJeff Layton static void locks_delete_global_blocked(struct file_lock *waiter) 55488974691SJeff Layton { 55548f74186SJeff Layton hash_del(&waiter->fl_link); 55688974691SJeff Layton } 55788974691SJeff Layton 5581da177e4SLinus Torvalds /* Remove waiter from blocker's block list. 5591da177e4SLinus Torvalds * When blocker ends up pointing to itself then the list is empty. 5601c8c601aSJeff Layton * 5617b2296afSJeff Layton * Must be called with blocked_lock_lock held. 5621da177e4SLinus Torvalds */ 56333443c42SMatt Mackall static void __locks_delete_block(struct file_lock *waiter) 5641da177e4SLinus Torvalds { 56588974691SJeff Layton locks_delete_global_blocked(waiter); 5661da177e4SLinus Torvalds list_del_init(&waiter->fl_block); 5671da177e4SLinus Torvalds waiter->fl_next = NULL; 5681da177e4SLinus Torvalds } 5691da177e4SLinus Torvalds 5701a9e64a7SJeff Layton static void locks_delete_block(struct file_lock *waiter) 5711da177e4SLinus Torvalds { 5727b2296afSJeff Layton spin_lock(&blocked_lock_lock); 5731da177e4SLinus Torvalds __locks_delete_block(waiter); 5747b2296afSJeff Layton spin_unlock(&blocked_lock_lock); 5751da177e4SLinus Torvalds } 5761da177e4SLinus Torvalds 5771da177e4SLinus Torvalds /* Insert waiter into blocker's block list. 5781da177e4SLinus Torvalds * We use a circular list so that processes can be easily woken up in 5791da177e4SLinus Torvalds * the order they blocked. The documentation doesn't require this but 5801da177e4SLinus Torvalds * it seems like the reasonable thing to do. 5811c8c601aSJeff Layton * 5827b2296afSJeff Layton * Must be called with both the i_lock and blocked_lock_lock held. The fl_block 58346dad760SJeff Layton * list itself is protected by the blocked_lock_lock, but by ensuring that the 5847b2296afSJeff Layton * i_lock is also held on insertions we can avoid taking the blocked_lock_lock 5854e8c765dSJeff Layton * in some cases when we see that the fl_block list is empty. 5861da177e4SLinus Torvalds */ 5871c8c601aSJeff Layton static void __locks_insert_block(struct file_lock *blocker, 5881da177e4SLinus Torvalds struct file_lock *waiter) 5891da177e4SLinus Torvalds { 5906dc0fe8fSJ. Bruce Fields BUG_ON(!list_empty(&waiter->fl_block)); 5911da177e4SLinus Torvalds waiter->fl_next = blocker; 59288974691SJeff Layton list_add_tail(&waiter->fl_block, &blocker->fl_block); 593cff2fce5SJeff Layton if (IS_POSIX(blocker) && !IS_OFDLCK(blocker)) 5941c8c601aSJeff Layton locks_insert_global_blocked(waiter); 5951c8c601aSJeff Layton } 5961c8c601aSJeff Layton 5971c8c601aSJeff Layton /* Must be called with i_lock held. */ 5981c8c601aSJeff Layton static void locks_insert_block(struct file_lock *blocker, 5991c8c601aSJeff Layton struct file_lock *waiter) 6001c8c601aSJeff Layton { 6017b2296afSJeff Layton spin_lock(&blocked_lock_lock); 6021c8c601aSJeff Layton __locks_insert_block(blocker, waiter); 6037b2296afSJeff Layton spin_unlock(&blocked_lock_lock); 6041da177e4SLinus Torvalds } 6051da177e4SLinus Torvalds 6061cb36012SJeff Layton /* 6071cb36012SJeff Layton * Wake up processes blocked waiting for blocker. 6081cb36012SJeff Layton * 6091c8c601aSJeff Layton * Must be called with the inode->i_lock held! 6101da177e4SLinus Torvalds */ 6111da177e4SLinus Torvalds static void locks_wake_up_blocks(struct file_lock *blocker) 6121da177e4SLinus Torvalds { 6134e8c765dSJeff Layton /* 6144e8c765dSJeff Layton * Avoid taking global lock if list is empty. This is safe since new 6154e8c765dSJeff Layton * blocked requests are only added to the list under the i_lock, and 6164e8c765dSJeff Layton * the i_lock is always held here. Note that removal from the fl_block 6174e8c765dSJeff Layton * list does not require the i_lock, so we must recheck list_empty() 6187b2296afSJeff Layton * after acquiring the blocked_lock_lock. 6194e8c765dSJeff Layton */ 6204e8c765dSJeff Layton if (list_empty(&blocker->fl_block)) 6214e8c765dSJeff Layton return; 6224e8c765dSJeff Layton 6237b2296afSJeff Layton spin_lock(&blocked_lock_lock); 6241da177e4SLinus Torvalds while (!list_empty(&blocker->fl_block)) { 625f0c1cd0eSPavel Emelyanov struct file_lock *waiter; 626f0c1cd0eSPavel Emelyanov 627f0c1cd0eSPavel Emelyanov waiter = list_first_entry(&blocker->fl_block, 6281da177e4SLinus Torvalds struct file_lock, fl_block); 6291da177e4SLinus Torvalds __locks_delete_block(waiter); 6308fb47a4fSJ. Bruce Fields if (waiter->fl_lmops && waiter->fl_lmops->lm_notify) 6318fb47a4fSJ. Bruce Fields waiter->fl_lmops->lm_notify(waiter); 6321da177e4SLinus Torvalds else 6331da177e4SLinus Torvalds wake_up(&waiter->fl_wait); 6341da177e4SLinus Torvalds } 6357b2296afSJeff Layton spin_unlock(&blocked_lock_lock); 6361da177e4SLinus Torvalds } 6371da177e4SLinus Torvalds 6381da177e4SLinus Torvalds /* Insert file lock fl into an inode's lock list at the position indicated 6391da177e4SLinus Torvalds * by pos. At the same time add the lock to the global file lock list. 6401c8c601aSJeff Layton * 6411c8c601aSJeff Layton * Must be called with the i_lock held! 6421da177e4SLinus Torvalds */ 6431da177e4SLinus Torvalds static void locks_insert_lock(struct file_lock **pos, struct file_lock *fl) 6441da177e4SLinus Torvalds { 645ab1f1611SVitaliy Gusev fl->fl_nspid = get_pid(task_tgid(current)); 646ab1f1611SVitaliy Gusev 6471da177e4SLinus Torvalds /* insert into file's list */ 6481da177e4SLinus Torvalds fl->fl_next = *pos; 6491da177e4SLinus Torvalds *pos = fl; 65088974691SJeff Layton 65188974691SJeff Layton locks_insert_global_locks(fl); 6521da177e4SLinus Torvalds } 6531da177e4SLinus Torvalds 65424cbe784SJeff Layton /** 65524cbe784SJeff Layton * locks_delete_lock - Delete a lock and then free it. 65624cbe784SJeff Layton * @thisfl_p: pointer that points to the fl_next field of the previous 65724cbe784SJeff Layton * inode->i_flock list entry 65824cbe784SJeff Layton * 65924cbe784SJeff Layton * Unlink a lock from all lists and free the namespace reference, but don't 66024cbe784SJeff Layton * free it yet. Wake up processes that are blocked waiting for this lock and 66124cbe784SJeff Layton * notify the FS that the lock has been cleared. 6621c8c601aSJeff Layton * 6631c8c601aSJeff Layton * Must be called with the i_lock held! 6641da177e4SLinus Torvalds */ 66524cbe784SJeff Layton static void locks_unlink_lock(struct file_lock **thisfl_p) 6661da177e4SLinus Torvalds { 6671da177e4SLinus Torvalds struct file_lock *fl = *thisfl_p; 6681da177e4SLinus Torvalds 66988974691SJeff Layton locks_delete_global_locks(fl); 67088974691SJeff Layton 6711da177e4SLinus Torvalds *thisfl_p = fl->fl_next; 6721da177e4SLinus Torvalds fl->fl_next = NULL; 6731da177e4SLinus Torvalds 674ab1f1611SVitaliy Gusev if (fl->fl_nspid) { 675ab1f1611SVitaliy Gusev put_pid(fl->fl_nspid); 676ab1f1611SVitaliy Gusev fl->fl_nspid = NULL; 677ab1f1611SVitaliy Gusev } 678ab1f1611SVitaliy Gusev 6791da177e4SLinus Torvalds locks_wake_up_blocks(fl); 68024cbe784SJeff Layton } 68124cbe784SJeff Layton 68224cbe784SJeff Layton /* 68324cbe784SJeff Layton * Unlink a lock from all lists and free it. 68424cbe784SJeff Layton * 68524cbe784SJeff Layton * Must be called with i_lock held! 68624cbe784SJeff Layton */ 687ed9814d8SJeff Layton static void locks_delete_lock(struct file_lock **thisfl_p, 688ed9814d8SJeff Layton struct list_head *dispose) 68924cbe784SJeff Layton { 69024cbe784SJeff Layton struct file_lock *fl = *thisfl_p; 69124cbe784SJeff Layton 69224cbe784SJeff Layton locks_unlink_lock(thisfl_p); 693ed9814d8SJeff Layton if (dispose) 694ed9814d8SJeff Layton list_add(&fl->fl_block, dispose); 695ed9814d8SJeff Layton else 6961da177e4SLinus Torvalds locks_free_lock(fl); 6971da177e4SLinus Torvalds } 6981da177e4SLinus Torvalds 6991da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. Common functionality 7001da177e4SLinus Torvalds * checks for shared/exclusive status of overlapping locks. 7011da177e4SLinus Torvalds */ 7021da177e4SLinus Torvalds static int locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl) 7031da177e4SLinus Torvalds { 7041da177e4SLinus Torvalds if (sys_fl->fl_type == F_WRLCK) 7051da177e4SLinus Torvalds return 1; 7061da177e4SLinus Torvalds if (caller_fl->fl_type == F_WRLCK) 7071da177e4SLinus Torvalds return 1; 7081da177e4SLinus Torvalds return 0; 7091da177e4SLinus Torvalds } 7101da177e4SLinus Torvalds 7111da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. POSIX specific 7121da177e4SLinus Torvalds * checking before calling the locks_conflict(). 7131da177e4SLinus Torvalds */ 7141da177e4SLinus Torvalds static int posix_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl) 7151da177e4SLinus Torvalds { 7161da177e4SLinus Torvalds /* POSIX locks owned by the same process do not conflict with 7171da177e4SLinus Torvalds * each other. 7181da177e4SLinus Torvalds */ 7191da177e4SLinus Torvalds if (!IS_POSIX(sys_fl) || posix_same_owner(caller_fl, sys_fl)) 7201da177e4SLinus Torvalds return (0); 7211da177e4SLinus Torvalds 7221da177e4SLinus Torvalds /* Check whether they overlap */ 7231da177e4SLinus Torvalds if (!locks_overlap(caller_fl, sys_fl)) 7241da177e4SLinus Torvalds return 0; 7251da177e4SLinus Torvalds 7261da177e4SLinus Torvalds return (locks_conflict(caller_fl, sys_fl)); 7271da177e4SLinus Torvalds } 7281da177e4SLinus Torvalds 7291da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. FLOCK specific 7301da177e4SLinus Torvalds * checking before calling the locks_conflict(). 7311da177e4SLinus Torvalds */ 7321da177e4SLinus Torvalds static int flock_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl) 7331da177e4SLinus Torvalds { 7341da177e4SLinus Torvalds /* FLOCK locks referring to the same filp do not conflict with 7351da177e4SLinus Torvalds * each other. 7361da177e4SLinus Torvalds */ 7371da177e4SLinus Torvalds if (!IS_FLOCK(sys_fl) || (caller_fl->fl_file == sys_fl->fl_file)) 7381da177e4SLinus Torvalds return (0); 7391da177e4SLinus Torvalds if ((caller_fl->fl_type & LOCK_MAND) || (sys_fl->fl_type & LOCK_MAND)) 7401da177e4SLinus Torvalds return 0; 7411da177e4SLinus Torvalds 7421da177e4SLinus Torvalds return (locks_conflict(caller_fl, sys_fl)); 7431da177e4SLinus Torvalds } 7441da177e4SLinus Torvalds 7456d34ac19SJ. Bruce Fields void 7469d6a8c5cSMarc Eshel posix_test_lock(struct file *filp, struct file_lock *fl) 7471da177e4SLinus Torvalds { 7481da177e4SLinus Torvalds struct file_lock *cfl; 7491c8c601aSJeff Layton struct inode *inode = file_inode(filp); 7501da177e4SLinus Torvalds 7511c8c601aSJeff Layton spin_lock(&inode->i_lock); 752496ad9aaSAl Viro for (cfl = file_inode(filp)->i_flock; cfl; cfl = cfl->fl_next) { 7531da177e4SLinus Torvalds if (!IS_POSIX(cfl)) 7541da177e4SLinus Torvalds continue; 755b842e240SJ. Bruce Fields if (posix_locks_conflict(fl, cfl)) 7561da177e4SLinus Torvalds break; 7571da177e4SLinus Torvalds } 758ab1f1611SVitaliy Gusev if (cfl) { 7593fe0fff1SKinglong Mee locks_copy_conflock(fl, cfl); 760ab1f1611SVitaliy Gusev if (cfl->fl_nspid) 7616c5f3e7bSPavel Emelyanov fl->fl_pid = pid_vnr(cfl->fl_nspid); 762ab1f1611SVitaliy Gusev } else 763129a84deSJ. Bruce Fields fl->fl_type = F_UNLCK; 7641c8c601aSJeff Layton spin_unlock(&inode->i_lock); 7656d34ac19SJ. Bruce Fields return; 7661da177e4SLinus Torvalds } 7671da177e4SLinus Torvalds EXPORT_SYMBOL(posix_test_lock); 7681da177e4SLinus Torvalds 769b533184fSJ. Bruce Fields /* 770b533184fSJ. Bruce Fields * Deadlock detection: 7711da177e4SLinus Torvalds * 772b533184fSJ. Bruce Fields * We attempt to detect deadlocks that are due purely to posix file 773b533184fSJ. Bruce Fields * locks. 7741da177e4SLinus Torvalds * 775b533184fSJ. Bruce Fields * We assume that a task can be waiting for at most one lock at a time. 776b533184fSJ. Bruce Fields * So for any acquired lock, the process holding that lock may be 777b533184fSJ. Bruce Fields * waiting on at most one other lock. That lock in turns may be held by 778b533184fSJ. Bruce Fields * someone waiting for at most one other lock. Given a requested lock 779b533184fSJ. Bruce Fields * caller_fl which is about to wait for a conflicting lock block_fl, we 780b533184fSJ. Bruce Fields * follow this chain of waiters to ensure we are not about to create a 781b533184fSJ. Bruce Fields * cycle. 78297855b49SJ. Bruce Fields * 783b533184fSJ. Bruce Fields * Since we do this before we ever put a process to sleep on a lock, we 784b533184fSJ. Bruce Fields * are ensured that there is never a cycle; that is what guarantees that 785b533184fSJ. Bruce Fields * the while() loop in posix_locks_deadlock() eventually completes. 786b533184fSJ. Bruce Fields * 787b533184fSJ. Bruce Fields * Note: the above assumption may not be true when handling lock 788b533184fSJ. Bruce Fields * requests from a broken NFS client. It may also fail in the presence 789b533184fSJ. Bruce Fields * of tasks (such as posix threads) sharing the same open file table. 790b533184fSJ. Bruce Fields * To handle those cases, we just bail out after a few iterations. 79157b65325SJeff Layton * 792cff2fce5SJeff Layton * For FL_OFDLCK locks, the owner is the filp, not the files_struct. 79357b65325SJeff Layton * Because the owner is not even nominally tied to a thread of 79457b65325SJeff Layton * execution, the deadlock detection below can't reasonably work well. Just 79557b65325SJeff Layton * skip it for those. 79657b65325SJeff Layton * 797cff2fce5SJeff Layton * In principle, we could do a more limited deadlock detection on FL_OFDLCK 79857b65325SJeff Layton * locks that just checks for the case where two tasks are attempting to 79957b65325SJeff Layton * upgrade from read to write locks on the same inode. 8001da177e4SLinus Torvalds */ 80197855b49SJ. Bruce Fields 80297855b49SJ. Bruce Fields #define MAX_DEADLK_ITERATIONS 10 80397855b49SJ. Bruce Fields 804b533184fSJ. Bruce Fields /* Find a lock that the owner of the given block_fl is blocking on. */ 805b533184fSJ. Bruce Fields static struct file_lock *what_owner_is_waiting_for(struct file_lock *block_fl) 806b533184fSJ. Bruce Fields { 807b533184fSJ. Bruce Fields struct file_lock *fl; 808b533184fSJ. Bruce Fields 8093999e493SJeff Layton hash_for_each_possible(blocked_hash, fl, fl_link, posix_owner_key(block_fl)) { 810b533184fSJ. Bruce Fields if (posix_same_owner(fl, block_fl)) 811b533184fSJ. Bruce Fields return fl->fl_next; 812b533184fSJ. Bruce Fields } 813b533184fSJ. Bruce Fields return NULL; 814b533184fSJ. Bruce Fields } 815b533184fSJ. Bruce Fields 8167b2296afSJeff Layton /* Must be called with the blocked_lock_lock held! */ 817b0904e14SAdrian Bunk static int posix_locks_deadlock(struct file_lock *caller_fl, 8181da177e4SLinus Torvalds struct file_lock *block_fl) 8191da177e4SLinus Torvalds { 82097855b49SJ. Bruce Fields int i = 0; 8211da177e4SLinus Torvalds 82257b65325SJeff Layton /* 82357b65325SJeff Layton * This deadlock detector can't reasonably detect deadlocks with 824cff2fce5SJeff Layton * FL_OFDLCK locks, since they aren't owned by a process, per-se. 82557b65325SJeff Layton */ 826cff2fce5SJeff Layton if (IS_OFDLCK(caller_fl)) 82757b65325SJeff Layton return 0; 82857b65325SJeff Layton 829b533184fSJ. Bruce Fields while ((block_fl = what_owner_is_waiting_for(block_fl))) { 83097855b49SJ. Bruce Fields if (i++ > MAX_DEADLK_ITERATIONS) 83197855b49SJ. Bruce Fields return 0; 832b533184fSJ. Bruce Fields if (posix_same_owner(caller_fl, block_fl)) 833b533184fSJ. Bruce Fields return 1; 8341da177e4SLinus Torvalds } 8351da177e4SLinus Torvalds return 0; 8361da177e4SLinus Torvalds } 8371da177e4SLinus Torvalds 8381da177e4SLinus Torvalds /* Try to create a FLOCK lock on filp. We always insert new FLOCK locks 83902888f41SJ. Bruce Fields * after any leases, but before any posix locks. 840f475ae95STrond Myklebust * 841f475ae95STrond Myklebust * Note that if called with an FL_EXISTS argument, the caller may determine 842f475ae95STrond Myklebust * whether or not a lock was successfully freed by testing the return 843f475ae95STrond Myklebust * value for -ENOENT. 8441da177e4SLinus Torvalds */ 845993dfa87STrond Myklebust static int flock_lock_file(struct file *filp, struct file_lock *request) 8461da177e4SLinus Torvalds { 847993dfa87STrond Myklebust struct file_lock *new_fl = NULL; 8481da177e4SLinus Torvalds struct file_lock **before; 849496ad9aaSAl Viro struct inode * inode = file_inode(filp); 8501da177e4SLinus Torvalds int error = 0; 8511da177e4SLinus Torvalds int found = 0; 852ed9814d8SJeff Layton LIST_HEAD(dispose); 8531da177e4SLinus Torvalds 854b89f4321SArnd Bergmann if (!(request->fl_flags & FL_ACCESS) && (request->fl_type != F_UNLCK)) { 855b89f4321SArnd Bergmann new_fl = locks_alloc_lock(); 856b89f4321SArnd Bergmann if (!new_fl) 857b89f4321SArnd Bergmann return -ENOMEM; 858b89f4321SArnd Bergmann } 859b89f4321SArnd Bergmann 8601c8c601aSJeff Layton spin_lock(&inode->i_lock); 861f07f18ddSTrond Myklebust if (request->fl_flags & FL_ACCESS) 862f07f18ddSTrond Myklebust goto find_conflict; 86384d535adSPavel Emelyanov 8641da177e4SLinus Torvalds for_each_lock(inode, before) { 8651da177e4SLinus Torvalds struct file_lock *fl = *before; 8661da177e4SLinus Torvalds if (IS_POSIX(fl)) 8671da177e4SLinus Torvalds break; 8681da177e4SLinus Torvalds if (IS_LEASE(fl)) 8691da177e4SLinus Torvalds continue; 8701da177e4SLinus Torvalds if (filp != fl->fl_file) 8711da177e4SLinus Torvalds continue; 872993dfa87STrond Myklebust if (request->fl_type == fl->fl_type) 8731da177e4SLinus Torvalds goto out; 8741da177e4SLinus Torvalds found = 1; 875ed9814d8SJeff Layton locks_delete_lock(before, &dispose); 8761da177e4SLinus Torvalds break; 8771da177e4SLinus Torvalds } 8781da177e4SLinus Torvalds 879f475ae95STrond Myklebust if (request->fl_type == F_UNLCK) { 880f475ae95STrond Myklebust if ((request->fl_flags & FL_EXISTS) && !found) 881f475ae95STrond Myklebust error = -ENOENT; 882993dfa87STrond Myklebust goto out; 883f475ae95STrond Myklebust } 8841da177e4SLinus Torvalds 8851da177e4SLinus Torvalds /* 8861da177e4SLinus Torvalds * If a higher-priority process was blocked on the old file lock, 8871da177e4SLinus Torvalds * give it the opportunity to lock the file. 8881da177e4SLinus Torvalds */ 889b89f4321SArnd Bergmann if (found) { 8901c8c601aSJeff Layton spin_unlock(&inode->i_lock); 891def01bc5SFrederic Weisbecker cond_resched(); 8921c8c601aSJeff Layton spin_lock(&inode->i_lock); 893b89f4321SArnd Bergmann } 8941da177e4SLinus Torvalds 895f07f18ddSTrond Myklebust find_conflict: 8961da177e4SLinus Torvalds for_each_lock(inode, before) { 8971da177e4SLinus Torvalds struct file_lock *fl = *before; 8981da177e4SLinus Torvalds if (IS_POSIX(fl)) 8991da177e4SLinus Torvalds break; 9001da177e4SLinus Torvalds if (IS_LEASE(fl)) 9011da177e4SLinus Torvalds continue; 902993dfa87STrond Myklebust if (!flock_locks_conflict(request, fl)) 9031da177e4SLinus Torvalds continue; 9041da177e4SLinus Torvalds error = -EAGAIN; 905bde74e4bSMiklos Szeredi if (!(request->fl_flags & FL_SLEEP)) 906bde74e4bSMiklos Szeredi goto out; 907bde74e4bSMiklos Szeredi error = FILE_LOCK_DEFERRED; 908993dfa87STrond Myklebust locks_insert_block(fl, request); 9091da177e4SLinus Torvalds goto out; 9101da177e4SLinus Torvalds } 911f07f18ddSTrond Myklebust if (request->fl_flags & FL_ACCESS) 912f07f18ddSTrond Myklebust goto out; 913993dfa87STrond Myklebust locks_copy_lock(new_fl, request); 9140e2f6db8SPavel Emelyanov locks_insert_lock(before, new_fl); 915993dfa87STrond Myklebust new_fl = NULL; 9169cedc194SKirill Korotaev error = 0; 9171da177e4SLinus Torvalds 9181da177e4SLinus Torvalds out: 9191c8c601aSJeff Layton spin_unlock(&inode->i_lock); 920993dfa87STrond Myklebust if (new_fl) 921993dfa87STrond Myklebust locks_free_lock(new_fl); 922ed9814d8SJeff Layton locks_dispose_list(&dispose); 9231da177e4SLinus Torvalds return error; 9241da177e4SLinus Torvalds } 9251da177e4SLinus Torvalds 926150b3934SMarc Eshel static int __posix_lock_file(struct inode *inode, struct file_lock *request, struct file_lock *conflock) 9271da177e4SLinus Torvalds { 9281da177e4SLinus Torvalds struct file_lock *fl; 92939005d02SMiklos Szeredi struct file_lock *new_fl = NULL; 93039005d02SMiklos Szeredi struct file_lock *new_fl2 = NULL; 9311da177e4SLinus Torvalds struct file_lock *left = NULL; 9321da177e4SLinus Torvalds struct file_lock *right = NULL; 9331da177e4SLinus Torvalds struct file_lock **before; 934b9746ef8SJeff Layton int error; 935b9746ef8SJeff Layton bool added = false; 936ed9814d8SJeff Layton LIST_HEAD(dispose); 9371da177e4SLinus Torvalds 9381da177e4SLinus Torvalds /* 9391da177e4SLinus Torvalds * We may need two file_lock structures for this operation, 9401da177e4SLinus Torvalds * so we get them in advance to avoid races. 94139005d02SMiklos Szeredi * 94239005d02SMiklos Szeredi * In some cases we can be sure, that no new locks will be needed 9431da177e4SLinus Torvalds */ 94439005d02SMiklos Szeredi if (!(request->fl_flags & FL_ACCESS) && 94539005d02SMiklos Szeredi (request->fl_type != F_UNLCK || 94639005d02SMiklos Szeredi request->fl_start != 0 || request->fl_end != OFFSET_MAX)) { 9471da177e4SLinus Torvalds new_fl = locks_alloc_lock(); 9481da177e4SLinus Torvalds new_fl2 = locks_alloc_lock(); 94939005d02SMiklos Szeredi } 9501da177e4SLinus Torvalds 9511c8c601aSJeff Layton spin_lock(&inode->i_lock); 9521cb36012SJeff Layton /* 9531cb36012SJeff Layton * New lock request. Walk all POSIX locks and look for conflicts. If 9541cb36012SJeff Layton * there are any, either return error or put the request on the 95548f74186SJeff Layton * blocker's list of waiters and the global blocked_hash. 9561cb36012SJeff Layton */ 9571da177e4SLinus Torvalds if (request->fl_type != F_UNLCK) { 9581da177e4SLinus Torvalds for_each_lock(inode, before) { 959526985b9SJ. Bruce Fields fl = *before; 9601da177e4SLinus Torvalds if (!IS_POSIX(fl)) 9611da177e4SLinus Torvalds continue; 9621da177e4SLinus Torvalds if (!posix_locks_conflict(request, fl)) 9631da177e4SLinus Torvalds continue; 9645842add2SAndy Adamson if (conflock) 9653fe0fff1SKinglong Mee locks_copy_conflock(conflock, fl); 9661da177e4SLinus Torvalds error = -EAGAIN; 9671da177e4SLinus Torvalds if (!(request->fl_flags & FL_SLEEP)) 9681da177e4SLinus Torvalds goto out; 9691c8c601aSJeff Layton /* 9701c8c601aSJeff Layton * Deadlock detection and insertion into the blocked 9711c8c601aSJeff Layton * locks list must be done while holding the same lock! 9721c8c601aSJeff Layton */ 9731da177e4SLinus Torvalds error = -EDEADLK; 9747b2296afSJeff Layton spin_lock(&blocked_lock_lock); 9751c8c601aSJeff Layton if (likely(!posix_locks_deadlock(request, fl))) { 976bde74e4bSMiklos Szeredi error = FILE_LOCK_DEFERRED; 9771c8c601aSJeff Layton __locks_insert_block(fl, request); 9781c8c601aSJeff Layton } 9797b2296afSJeff Layton spin_unlock(&blocked_lock_lock); 9801da177e4SLinus Torvalds goto out; 9811da177e4SLinus Torvalds } 9821da177e4SLinus Torvalds } 9831da177e4SLinus Torvalds 9841da177e4SLinus Torvalds /* If we're just looking for a conflict, we're done. */ 9851da177e4SLinus Torvalds error = 0; 9861da177e4SLinus Torvalds if (request->fl_flags & FL_ACCESS) 9871da177e4SLinus Torvalds goto out; 9881da177e4SLinus Torvalds 9891da177e4SLinus Torvalds /* 9901da177e4SLinus Torvalds * Find the first old lock with the same owner as the new lock. 9911da177e4SLinus Torvalds */ 9921da177e4SLinus Torvalds 9931da177e4SLinus Torvalds before = &inode->i_flock; 9941da177e4SLinus Torvalds 9951da177e4SLinus Torvalds /* First skip locks owned by other processes. */ 9961da177e4SLinus Torvalds while ((fl = *before) && (!IS_POSIX(fl) || 9971da177e4SLinus Torvalds !posix_same_owner(request, fl))) { 9981da177e4SLinus Torvalds before = &fl->fl_next; 9991da177e4SLinus Torvalds } 10001da177e4SLinus Torvalds 10011da177e4SLinus Torvalds /* Process locks with this owner. */ 10021da177e4SLinus Torvalds while ((fl = *before) && posix_same_owner(request, fl)) { 10031da177e4SLinus Torvalds /* Detect adjacent or overlapping regions (if same lock type) 10041da177e4SLinus Torvalds */ 10051da177e4SLinus Torvalds if (request->fl_type == fl->fl_type) { 1006449231d6SOlaf Kirch /* In all comparisons of start vs end, use 1007449231d6SOlaf Kirch * "start - 1" rather than "end + 1". If end 1008449231d6SOlaf Kirch * is OFFSET_MAX, end + 1 will become negative. 1009449231d6SOlaf Kirch */ 10101da177e4SLinus Torvalds if (fl->fl_end < request->fl_start - 1) 10111da177e4SLinus Torvalds goto next_lock; 10121da177e4SLinus Torvalds /* If the next lock in the list has entirely bigger 10131da177e4SLinus Torvalds * addresses than the new one, insert the lock here. 10141da177e4SLinus Torvalds */ 1015449231d6SOlaf Kirch if (fl->fl_start - 1 > request->fl_end) 10161da177e4SLinus Torvalds break; 10171da177e4SLinus Torvalds 10181da177e4SLinus Torvalds /* If we come here, the new and old lock are of the 10191da177e4SLinus Torvalds * same type and adjacent or overlapping. Make one 10201da177e4SLinus Torvalds * lock yielding from the lower start address of both 10211da177e4SLinus Torvalds * locks to the higher end address. 10221da177e4SLinus Torvalds */ 10231da177e4SLinus Torvalds if (fl->fl_start > request->fl_start) 10241da177e4SLinus Torvalds fl->fl_start = request->fl_start; 10251da177e4SLinus Torvalds else 10261da177e4SLinus Torvalds request->fl_start = fl->fl_start; 10271da177e4SLinus Torvalds if (fl->fl_end < request->fl_end) 10281da177e4SLinus Torvalds fl->fl_end = request->fl_end; 10291da177e4SLinus Torvalds else 10301da177e4SLinus Torvalds request->fl_end = fl->fl_end; 10311da177e4SLinus Torvalds if (added) { 1032ed9814d8SJeff Layton locks_delete_lock(before, &dispose); 10331da177e4SLinus Torvalds continue; 10341da177e4SLinus Torvalds } 10351da177e4SLinus Torvalds request = fl; 1036b9746ef8SJeff Layton added = true; 10371da177e4SLinus Torvalds } 10381da177e4SLinus Torvalds else { 10391da177e4SLinus Torvalds /* Processing for different lock types is a bit 10401da177e4SLinus Torvalds * more complex. 10411da177e4SLinus Torvalds */ 10421da177e4SLinus Torvalds if (fl->fl_end < request->fl_start) 10431da177e4SLinus Torvalds goto next_lock; 10441da177e4SLinus Torvalds if (fl->fl_start > request->fl_end) 10451da177e4SLinus Torvalds break; 10461da177e4SLinus Torvalds if (request->fl_type == F_UNLCK) 1047b9746ef8SJeff Layton added = true; 10481da177e4SLinus Torvalds if (fl->fl_start < request->fl_start) 10491da177e4SLinus Torvalds left = fl; 10501da177e4SLinus Torvalds /* If the next lock in the list has a higher end 10511da177e4SLinus Torvalds * address than the new one, insert the new one here. 10521da177e4SLinus Torvalds */ 10531da177e4SLinus Torvalds if (fl->fl_end > request->fl_end) { 10541da177e4SLinus Torvalds right = fl; 10551da177e4SLinus Torvalds break; 10561da177e4SLinus Torvalds } 10571da177e4SLinus Torvalds if (fl->fl_start >= request->fl_start) { 10581da177e4SLinus Torvalds /* The new lock completely replaces an old 10591da177e4SLinus Torvalds * one (This may happen several times). 10601da177e4SLinus Torvalds */ 10611da177e4SLinus Torvalds if (added) { 1062ed9814d8SJeff Layton locks_delete_lock(before, &dispose); 10631da177e4SLinus Torvalds continue; 10641da177e4SLinus Torvalds } 1065b84d49f9SJeff Layton /* 1066b84d49f9SJeff Layton * Replace the old lock with new_fl, and 1067b84d49f9SJeff Layton * remove the old one. It's safe to do the 1068b84d49f9SJeff Layton * insert here since we know that we won't be 1069b84d49f9SJeff Layton * using new_fl later, and that the lock is 1070b84d49f9SJeff Layton * just replacing an existing lock. 10711da177e4SLinus Torvalds */ 1072b84d49f9SJeff Layton error = -ENOLCK; 1073b84d49f9SJeff Layton if (!new_fl) 1074b84d49f9SJeff Layton goto out; 1075b84d49f9SJeff Layton locks_copy_lock(new_fl, request); 1076b84d49f9SJeff Layton request = new_fl; 1077b84d49f9SJeff Layton new_fl = NULL; 1078ed9814d8SJeff Layton locks_delete_lock(before, &dispose); 1079b84d49f9SJeff Layton locks_insert_lock(before, request); 1080b9746ef8SJeff Layton added = true; 10811da177e4SLinus Torvalds } 10821da177e4SLinus Torvalds } 10831da177e4SLinus Torvalds /* Go on to next lock. 10841da177e4SLinus Torvalds */ 10851da177e4SLinus Torvalds next_lock: 10861da177e4SLinus Torvalds before = &fl->fl_next; 10871da177e4SLinus Torvalds } 10881da177e4SLinus Torvalds 10890d9a490aSMiklos Szeredi /* 10901cb36012SJeff Layton * The above code only modifies existing locks in case of merging or 10911cb36012SJeff Layton * replacing. If new lock(s) need to be inserted all modifications are 10921cb36012SJeff Layton * done below this, so it's safe yet to bail out. 10930d9a490aSMiklos Szeredi */ 10940d9a490aSMiklos Szeredi error = -ENOLCK; /* "no luck" */ 10950d9a490aSMiklos Szeredi if (right && left == right && !new_fl2) 10960d9a490aSMiklos Szeredi goto out; 10970d9a490aSMiklos Szeredi 10981da177e4SLinus Torvalds error = 0; 10991da177e4SLinus Torvalds if (!added) { 1100f475ae95STrond Myklebust if (request->fl_type == F_UNLCK) { 1101f475ae95STrond Myklebust if (request->fl_flags & FL_EXISTS) 1102f475ae95STrond Myklebust error = -ENOENT; 11031da177e4SLinus Torvalds goto out; 1104f475ae95STrond Myklebust } 11050d9a490aSMiklos Szeredi 11060d9a490aSMiklos Szeredi if (!new_fl) { 11070d9a490aSMiklos Szeredi error = -ENOLCK; 11080d9a490aSMiklos Szeredi goto out; 11090d9a490aSMiklos Szeredi } 11101da177e4SLinus Torvalds locks_copy_lock(new_fl, request); 11111da177e4SLinus Torvalds locks_insert_lock(before, new_fl); 11121da177e4SLinus Torvalds new_fl = NULL; 11131da177e4SLinus Torvalds } 11141da177e4SLinus Torvalds if (right) { 11151da177e4SLinus Torvalds if (left == right) { 11161da177e4SLinus Torvalds /* The new lock breaks the old one in two pieces, 11171da177e4SLinus Torvalds * so we have to use the second new lock. 11181da177e4SLinus Torvalds */ 11191da177e4SLinus Torvalds left = new_fl2; 11201da177e4SLinus Torvalds new_fl2 = NULL; 11211da177e4SLinus Torvalds locks_copy_lock(left, right); 11221da177e4SLinus Torvalds locks_insert_lock(before, left); 11231da177e4SLinus Torvalds } 11241da177e4SLinus Torvalds right->fl_start = request->fl_end + 1; 11251da177e4SLinus Torvalds locks_wake_up_blocks(right); 11261da177e4SLinus Torvalds } 11271da177e4SLinus Torvalds if (left) { 11281da177e4SLinus Torvalds left->fl_end = request->fl_start - 1; 11291da177e4SLinus Torvalds locks_wake_up_blocks(left); 11301da177e4SLinus Torvalds } 11311da177e4SLinus Torvalds out: 11321c8c601aSJeff Layton spin_unlock(&inode->i_lock); 11331da177e4SLinus Torvalds /* 11341da177e4SLinus Torvalds * Free any unused locks. 11351da177e4SLinus Torvalds */ 11361da177e4SLinus Torvalds if (new_fl) 11371da177e4SLinus Torvalds locks_free_lock(new_fl); 11381da177e4SLinus Torvalds if (new_fl2) 11391da177e4SLinus Torvalds locks_free_lock(new_fl2); 1140ed9814d8SJeff Layton locks_dispose_list(&dispose); 11411da177e4SLinus Torvalds return error; 11421da177e4SLinus Torvalds } 11431da177e4SLinus Torvalds 11441da177e4SLinus Torvalds /** 11451da177e4SLinus Torvalds * posix_lock_file - Apply a POSIX-style lock to a file 11461da177e4SLinus Torvalds * @filp: The file to apply the lock to 11471da177e4SLinus Torvalds * @fl: The lock to be applied 1148150b3934SMarc Eshel * @conflock: Place to return a copy of the conflicting lock, if found. 11491da177e4SLinus Torvalds * 11501da177e4SLinus Torvalds * Add a POSIX style lock to a file. 11511da177e4SLinus Torvalds * We merge adjacent & overlapping locks whenever possible. 11521da177e4SLinus Torvalds * POSIX locks are sorted by owner task, then by starting address 1153f475ae95STrond Myklebust * 1154f475ae95STrond Myklebust * Note that if called with an FL_EXISTS argument, the caller may determine 1155f475ae95STrond Myklebust * whether or not a lock was successfully freed by testing the return 1156f475ae95STrond Myklebust * value for -ENOENT. 11571da177e4SLinus Torvalds */ 1158150b3934SMarc Eshel int posix_lock_file(struct file *filp, struct file_lock *fl, 11595842add2SAndy Adamson struct file_lock *conflock) 11605842add2SAndy Adamson { 1161496ad9aaSAl Viro return __posix_lock_file(file_inode(filp), fl, conflock); 11625842add2SAndy Adamson } 1163150b3934SMarc Eshel EXPORT_SYMBOL(posix_lock_file); 11641da177e4SLinus Torvalds 11651da177e4SLinus Torvalds /** 11661da177e4SLinus Torvalds * posix_lock_file_wait - Apply a POSIX-style lock to a file 11671da177e4SLinus Torvalds * @filp: The file to apply the lock to 11681da177e4SLinus Torvalds * @fl: The lock to be applied 11691da177e4SLinus Torvalds * 11701da177e4SLinus Torvalds * Add a POSIX style lock to a file. 11711da177e4SLinus Torvalds * We merge adjacent & overlapping locks whenever possible. 11721da177e4SLinus Torvalds * POSIX locks are sorted by owner task, then by starting address 11731da177e4SLinus Torvalds */ 11741da177e4SLinus Torvalds int posix_lock_file_wait(struct file *filp, struct file_lock *fl) 11751da177e4SLinus Torvalds { 11761da177e4SLinus Torvalds int error; 11771da177e4SLinus Torvalds might_sleep (); 11781da177e4SLinus Torvalds for (;;) { 1179150b3934SMarc Eshel error = posix_lock_file(filp, fl, NULL); 1180bde74e4bSMiklos Szeredi if (error != FILE_LOCK_DEFERRED) 11811da177e4SLinus Torvalds break; 11821da177e4SLinus Torvalds error = wait_event_interruptible(fl->fl_wait, !fl->fl_next); 11831da177e4SLinus Torvalds if (!error) 11841da177e4SLinus Torvalds continue; 11851da177e4SLinus Torvalds 11861da177e4SLinus Torvalds locks_delete_block(fl); 11871da177e4SLinus Torvalds break; 11881da177e4SLinus Torvalds } 11891da177e4SLinus Torvalds return error; 11901da177e4SLinus Torvalds } 11911da177e4SLinus Torvalds EXPORT_SYMBOL(posix_lock_file_wait); 11921da177e4SLinus Torvalds 11931da177e4SLinus Torvalds /** 11941da177e4SLinus Torvalds * locks_mandatory_locked - Check for an active lock 1195d7a06983SJeff Layton * @file: the file to check 11961da177e4SLinus Torvalds * 11971da177e4SLinus Torvalds * Searches the inode's list of locks to find any POSIX locks which conflict. 11981da177e4SLinus Torvalds * This function is called from locks_verify_locked() only. 11991da177e4SLinus Torvalds */ 1200d7a06983SJeff Layton int locks_mandatory_locked(struct file *file) 12011da177e4SLinus Torvalds { 1202d7a06983SJeff Layton struct inode *inode = file_inode(file); 12031da177e4SLinus Torvalds struct file_lock *fl; 12041da177e4SLinus Torvalds 12051da177e4SLinus Torvalds /* 12061da177e4SLinus Torvalds * Search the lock list for this inode for any POSIX locks. 12071da177e4SLinus Torvalds */ 12081c8c601aSJeff Layton spin_lock(&inode->i_lock); 12091da177e4SLinus Torvalds for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) { 12101da177e4SLinus Torvalds if (!IS_POSIX(fl)) 12111da177e4SLinus Torvalds continue; 121273a8f5f7SChristoph Hellwig if (fl->fl_owner != current->files && 121373a8f5f7SChristoph Hellwig fl->fl_owner != file) 12141da177e4SLinus Torvalds break; 12151da177e4SLinus Torvalds } 12161c8c601aSJeff Layton spin_unlock(&inode->i_lock); 12171da177e4SLinus Torvalds return fl ? -EAGAIN : 0; 12181da177e4SLinus Torvalds } 12191da177e4SLinus Torvalds 12201da177e4SLinus Torvalds /** 12211da177e4SLinus Torvalds * locks_mandatory_area - Check for a conflicting lock 12221da177e4SLinus Torvalds * @read_write: %FLOCK_VERIFY_WRITE for exclusive access, %FLOCK_VERIFY_READ 12231da177e4SLinus Torvalds * for shared 12241da177e4SLinus Torvalds * @inode: the file to check 12251da177e4SLinus Torvalds * @filp: how the file was opened (if it was) 12261da177e4SLinus Torvalds * @offset: start of area to check 12271da177e4SLinus Torvalds * @count: length of area to check 12281da177e4SLinus Torvalds * 12291da177e4SLinus Torvalds * Searches the inode's list of locks to find any POSIX locks which conflict. 12301da177e4SLinus Torvalds * This function is called from rw_verify_area() and 12311da177e4SLinus Torvalds * locks_verify_truncate(). 12321da177e4SLinus Torvalds */ 12331da177e4SLinus Torvalds int locks_mandatory_area(int read_write, struct inode *inode, 12341da177e4SLinus Torvalds struct file *filp, loff_t offset, 12351da177e4SLinus Torvalds size_t count) 12361da177e4SLinus Torvalds { 12371da177e4SLinus Torvalds struct file_lock fl; 12381da177e4SLinus Torvalds int error; 123929723adeSJeff Layton bool sleep = false; 12401da177e4SLinus Torvalds 12411da177e4SLinus Torvalds locks_init_lock(&fl); 12421da177e4SLinus Torvalds fl.fl_pid = current->tgid; 12431da177e4SLinus Torvalds fl.fl_file = filp; 12441da177e4SLinus Torvalds fl.fl_flags = FL_POSIX | FL_ACCESS; 12451da177e4SLinus Torvalds if (filp && !(filp->f_flags & O_NONBLOCK)) 124629723adeSJeff Layton sleep = true; 12471da177e4SLinus Torvalds fl.fl_type = (read_write == FLOCK_VERIFY_WRITE) ? F_WRLCK : F_RDLCK; 12481da177e4SLinus Torvalds fl.fl_start = offset; 12491da177e4SLinus Torvalds fl.fl_end = offset + count - 1; 12501da177e4SLinus Torvalds 12511da177e4SLinus Torvalds for (;;) { 125229723adeSJeff Layton if (filp) { 125373a8f5f7SChristoph Hellwig fl.fl_owner = filp; 125429723adeSJeff Layton fl.fl_flags &= ~FL_SLEEP; 125529723adeSJeff Layton error = __posix_lock_file(inode, &fl, NULL); 125629723adeSJeff Layton if (!error) 125729723adeSJeff Layton break; 125829723adeSJeff Layton } 125929723adeSJeff Layton 126029723adeSJeff Layton if (sleep) 126129723adeSJeff Layton fl.fl_flags |= FL_SLEEP; 126229723adeSJeff Layton fl.fl_owner = current->files; 1263150b3934SMarc Eshel error = __posix_lock_file(inode, &fl, NULL); 1264bde74e4bSMiklos Szeredi if (error != FILE_LOCK_DEFERRED) 12651da177e4SLinus Torvalds break; 12661da177e4SLinus Torvalds error = wait_event_interruptible(fl.fl_wait, !fl.fl_next); 12671da177e4SLinus Torvalds if (!error) { 12681da177e4SLinus Torvalds /* 12691da177e4SLinus Torvalds * If we've been sleeping someone might have 12701da177e4SLinus Torvalds * changed the permissions behind our back. 12711da177e4SLinus Torvalds */ 1272a16877caSPavel Emelyanov if (__mandatory_lock(inode)) 12731da177e4SLinus Torvalds continue; 12741da177e4SLinus Torvalds } 12751da177e4SLinus Torvalds 12761da177e4SLinus Torvalds locks_delete_block(&fl); 12771da177e4SLinus Torvalds break; 12781da177e4SLinus Torvalds } 12791da177e4SLinus Torvalds 12801da177e4SLinus Torvalds return error; 12811da177e4SLinus Torvalds } 12821da177e4SLinus Torvalds 12831da177e4SLinus Torvalds EXPORT_SYMBOL(locks_mandatory_area); 12841da177e4SLinus Torvalds 1285778fc546SJ. Bruce Fields static void lease_clear_pending(struct file_lock *fl, int arg) 1286778fc546SJ. Bruce Fields { 1287778fc546SJ. Bruce Fields switch (arg) { 1288778fc546SJ. Bruce Fields case F_UNLCK: 1289778fc546SJ. Bruce Fields fl->fl_flags &= ~FL_UNLOCK_PENDING; 1290778fc546SJ. Bruce Fields /* fall through: */ 1291778fc546SJ. Bruce Fields case F_RDLCK: 1292778fc546SJ. Bruce Fields fl->fl_flags &= ~FL_DOWNGRADE_PENDING; 1293778fc546SJ. Bruce Fields } 1294778fc546SJ. Bruce Fields } 1295778fc546SJ. Bruce Fields 12961da177e4SLinus Torvalds /* We already had a lease on this file; just change its type */ 1297c45198edSJeff Layton int lease_modify(struct file_lock **before, int arg, struct list_head *dispose) 12981da177e4SLinus Torvalds { 12991da177e4SLinus Torvalds struct file_lock *fl = *before; 13001da177e4SLinus Torvalds int error = assign_type(fl, arg); 13011da177e4SLinus Torvalds 13021da177e4SLinus Torvalds if (error) 13031da177e4SLinus Torvalds return error; 1304778fc546SJ. Bruce Fields lease_clear_pending(fl, arg); 13051da177e4SLinus Torvalds locks_wake_up_blocks(fl); 13063b6e2723SFilipe Brandenburger if (arg == F_UNLCK) { 13073b6e2723SFilipe Brandenburger struct file *filp = fl->fl_file; 13083b6e2723SFilipe Brandenburger 13093b6e2723SFilipe Brandenburger f_delown(filp); 13103b6e2723SFilipe Brandenburger filp->f_owner.signum = 0; 131196d6d59cSJ. Bruce Fields fasync_helper(0, fl->fl_file, 0, &fl->fl_fasync); 131296d6d59cSJ. Bruce Fields if (fl->fl_fasync != NULL) { 131396d6d59cSJ. Bruce Fields printk(KERN_ERR "locks_delete_lock: fasync == %p\n", fl->fl_fasync); 131496d6d59cSJ. Bruce Fields fl->fl_fasync = NULL; 131596d6d59cSJ. Bruce Fields } 1316c45198edSJeff Layton locks_delete_lock(before, dispose); 13173b6e2723SFilipe Brandenburger } 13181da177e4SLinus Torvalds return 0; 13191da177e4SLinus Torvalds } 13201da177e4SLinus Torvalds EXPORT_SYMBOL(lease_modify); 13211da177e4SLinus Torvalds 1322778fc546SJ. Bruce Fields static bool past_time(unsigned long then) 1323778fc546SJ. Bruce Fields { 1324778fc546SJ. Bruce Fields if (!then) 1325778fc546SJ. Bruce Fields /* 0 is a special value meaning "this never expires": */ 1326778fc546SJ. Bruce Fields return false; 1327778fc546SJ. Bruce Fields return time_after(jiffies, then); 1328778fc546SJ. Bruce Fields } 1329778fc546SJ. Bruce Fields 1330c45198edSJeff Layton static void time_out_leases(struct inode *inode, struct list_head *dispose) 13311da177e4SLinus Torvalds { 13321da177e4SLinus Torvalds struct file_lock **before; 13331da177e4SLinus Torvalds struct file_lock *fl; 13341da177e4SLinus Torvalds 1335f82b4b67SJeff Layton lockdep_assert_held(&inode->i_lock); 1336f82b4b67SJeff Layton 13371da177e4SLinus Torvalds before = &inode->i_flock; 1338ab83fa4bSJ. Bruce Fields while ((fl = *before) && IS_LEASE(fl) && lease_breaking(fl)) { 133962af4f1fSJeff Layton trace_time_out_leases(inode, fl); 1340778fc546SJ. Bruce Fields if (past_time(fl->fl_downgrade_time)) 1341c45198edSJeff Layton lease_modify(before, F_RDLCK, dispose); 1342778fc546SJ. Bruce Fields if (past_time(fl->fl_break_time)) 1343c45198edSJeff Layton lease_modify(before, F_UNLCK, dispose); 13441da177e4SLinus Torvalds if (fl == *before) /* lease_modify may have freed fl */ 13451da177e4SLinus Torvalds before = &fl->fl_next; 13461da177e4SLinus Torvalds } 13471da177e4SLinus Torvalds } 13481da177e4SLinus Torvalds 1349df4e8d2cSJ. Bruce Fields static bool leases_conflict(struct file_lock *lease, struct file_lock *breaker) 1350df4e8d2cSJ. Bruce Fields { 1351df4e8d2cSJ. Bruce Fields if ((breaker->fl_flags & FL_DELEG) && (lease->fl_flags & FL_LEASE)) 1352df4e8d2cSJ. Bruce Fields return false; 1353df4e8d2cSJ. Bruce Fields return locks_conflict(breaker, lease); 1354df4e8d2cSJ. Bruce Fields } 1355df4e8d2cSJ. Bruce Fields 135603d12ddfSJeff Layton static bool 135703d12ddfSJeff Layton any_leases_conflict(struct inode *inode, struct file_lock *breaker) 135803d12ddfSJeff Layton { 135903d12ddfSJeff Layton struct file_lock *fl; 136003d12ddfSJeff Layton 136103d12ddfSJeff Layton lockdep_assert_held(&inode->i_lock); 136203d12ddfSJeff Layton 136303d12ddfSJeff Layton for (fl = inode->i_flock ; fl && IS_LEASE(fl); fl = fl->fl_next) { 136403d12ddfSJeff Layton if (leases_conflict(fl, breaker)) 136503d12ddfSJeff Layton return true; 136603d12ddfSJeff Layton } 136703d12ddfSJeff Layton return false; 136803d12ddfSJeff Layton } 136903d12ddfSJeff Layton 13701da177e4SLinus Torvalds /** 13711da177e4SLinus Torvalds * __break_lease - revoke all outstanding leases on file 13721da177e4SLinus Torvalds * @inode: the inode of the file to return 1373df4e8d2cSJ. Bruce Fields * @mode: O_RDONLY: break only write leases; O_WRONLY or O_RDWR: 1374df4e8d2cSJ. Bruce Fields * break all leases 1375df4e8d2cSJ. Bruce Fields * @type: FL_LEASE: break leases and delegations; FL_DELEG: break 1376df4e8d2cSJ. Bruce Fields * only delegations 13771da177e4SLinus Torvalds * 137887250dd2Sdavid m. richter * break_lease (inlined for speed) has checked there already is at least 137987250dd2Sdavid m. richter * some kind of lock (maybe a lease) on this file. Leases are broken on 138087250dd2Sdavid m. richter * a call to open() or truncate(). This function can sleep unless you 13811da177e4SLinus Torvalds * specified %O_NONBLOCK to your open(). 13821da177e4SLinus Torvalds */ 1383df4e8d2cSJ. Bruce Fields int __break_lease(struct inode *inode, unsigned int mode, unsigned int type) 13841da177e4SLinus Torvalds { 1385778fc546SJ. Bruce Fields int error = 0; 138603d12ddfSJeff Layton struct file_lock *new_fl; 1387*4d01b7f5SJeff Layton struct file_lock *fl, **before; 13881da177e4SLinus Torvalds unsigned long break_time; 13898737c930SAl Viro int want_write = (mode & O_ACCMODE) != O_RDONLY; 1390c45198edSJeff Layton LIST_HEAD(dispose); 13911da177e4SLinus Torvalds 13928737c930SAl Viro new_fl = lease_alloc(NULL, want_write ? F_WRLCK : F_RDLCK); 13936d4b9e38SLinus Torvalds if (IS_ERR(new_fl)) 13946d4b9e38SLinus Torvalds return PTR_ERR(new_fl); 1395df4e8d2cSJ. Bruce Fields new_fl->fl_flags = type; 13961da177e4SLinus Torvalds 13971c8c601aSJeff Layton spin_lock(&inode->i_lock); 13981da177e4SLinus Torvalds 1399c45198edSJeff Layton time_out_leases(inode, &dispose); 14001da177e4SLinus Torvalds 140103d12ddfSJeff Layton if (!any_leases_conflict(inode, new_fl)) 1402df4e8d2cSJ. Bruce Fields goto out; 14031da177e4SLinus Torvalds 14041da177e4SLinus Torvalds break_time = 0; 14051da177e4SLinus Torvalds if (lease_break_time > 0) { 14061da177e4SLinus Torvalds break_time = jiffies + lease_break_time * HZ; 14071da177e4SLinus Torvalds if (break_time == 0) 14081da177e4SLinus Torvalds break_time++; /* so that 0 means no break time */ 14091da177e4SLinus Torvalds } 14101da177e4SLinus Torvalds 1411*4d01b7f5SJeff Layton for (before = &inode->i_flock; 1412*4d01b7f5SJeff Layton ((fl = *before) != NULL) && IS_LEASE(fl); 1413*4d01b7f5SJeff Layton before = &fl->fl_next) { 1414df4e8d2cSJ. Bruce Fields if (!leases_conflict(fl, new_fl)) 1415df4e8d2cSJ. Bruce Fields continue; 1416778fc546SJ. Bruce Fields if (want_write) { 1417778fc546SJ. Bruce Fields if (fl->fl_flags & FL_UNLOCK_PENDING) 1418778fc546SJ. Bruce Fields continue; 1419778fc546SJ. Bruce Fields fl->fl_flags |= FL_UNLOCK_PENDING; 14201da177e4SLinus Torvalds fl->fl_break_time = break_time; 1421778fc546SJ. Bruce Fields } else { 142203d12ddfSJeff Layton if (lease_breaking(inode->i_flock)) 1423778fc546SJ. Bruce Fields continue; 1424778fc546SJ. Bruce Fields fl->fl_flags |= FL_DOWNGRADE_PENDING; 1425778fc546SJ. Bruce Fields fl->fl_downgrade_time = break_time; 14261da177e4SLinus Torvalds } 1427*4d01b7f5SJeff Layton if (fl->fl_lmops->lm_break(fl)) 1428*4d01b7f5SJeff Layton locks_delete_lock(before, &dispose); 14291da177e4SLinus Torvalds } 14301da177e4SLinus Torvalds 1431*4d01b7f5SJeff Layton fl = inode->i_flock; 1432*4d01b7f5SJeff Layton if (!fl || !IS_LEASE(fl)) 1433*4d01b7f5SJeff Layton goto out; 1434*4d01b7f5SJeff Layton 1435843c6b2fSJeff Layton if (mode & O_NONBLOCK) { 143662af4f1fSJeff Layton trace_break_lease_noblock(inode, new_fl); 14371da177e4SLinus Torvalds error = -EWOULDBLOCK; 14381da177e4SLinus Torvalds goto out; 14391da177e4SLinus Torvalds } 14401da177e4SLinus Torvalds 14411da177e4SLinus Torvalds restart: 144203d12ddfSJeff Layton break_time = inode->i_flock->fl_break_time; 1443f1c6bb2cSJeff Layton if (break_time != 0) 14441da177e4SLinus Torvalds break_time -= jiffies; 14451da177e4SLinus Torvalds if (break_time == 0) 14461da177e4SLinus Torvalds break_time++; 144703d12ddfSJeff Layton locks_insert_block(inode->i_flock, new_fl); 144862af4f1fSJeff Layton trace_break_lease_block(inode, new_fl); 14491c8c601aSJeff Layton spin_unlock(&inode->i_lock); 1450c45198edSJeff Layton locks_dispose_list(&dispose); 14514321e01eSMatthew Wilcox error = wait_event_interruptible_timeout(new_fl->fl_wait, 14524321e01eSMatthew Wilcox !new_fl->fl_next, break_time); 14531c8c601aSJeff Layton spin_lock(&inode->i_lock); 145462af4f1fSJeff Layton trace_break_lease_unblock(inode, new_fl); 14551c8c601aSJeff Layton locks_delete_block(new_fl); 14561da177e4SLinus Torvalds if (error >= 0) { 1457778fc546SJ. Bruce Fields /* 1458778fc546SJ. Bruce Fields * Wait for the next conflicting lease that has not been 1459778fc546SJ. Bruce Fields * broken yet 1460778fc546SJ. Bruce Fields */ 146103d12ddfSJeff Layton if (error == 0) 146203d12ddfSJeff Layton time_out_leases(inode, &dispose); 146303d12ddfSJeff Layton if (any_leases_conflict(inode, new_fl)) 14641da177e4SLinus Torvalds goto restart; 146503d12ddfSJeff Layton 14661da177e4SLinus Torvalds error = 0; 14671da177e4SLinus Torvalds } 14681da177e4SLinus Torvalds 14691da177e4SLinus Torvalds out: 14701c8c601aSJeff Layton spin_unlock(&inode->i_lock); 1471c45198edSJeff Layton locks_dispose_list(&dispose); 14721da177e4SLinus Torvalds locks_free_lock(new_fl); 14731da177e4SLinus Torvalds return error; 14741da177e4SLinus Torvalds } 14751da177e4SLinus Torvalds 14761da177e4SLinus Torvalds EXPORT_SYMBOL(__break_lease); 14771da177e4SLinus Torvalds 14781da177e4SLinus Torvalds /** 1479a6b91919SRandy Dunlap * lease_get_mtime - get the last modified time of an inode 14801da177e4SLinus Torvalds * @inode: the inode 14811da177e4SLinus Torvalds * @time: pointer to a timespec which will contain the last modified time 14821da177e4SLinus Torvalds * 14831da177e4SLinus Torvalds * This is to force NFS clients to flush their caches for files with 14841da177e4SLinus Torvalds * exclusive leases. The justification is that if someone has an 1485a6b91919SRandy Dunlap * exclusive lease, then they could be modifying it. 14861da177e4SLinus Torvalds */ 14871da177e4SLinus Torvalds void lease_get_mtime(struct inode *inode, struct timespec *time) 14881da177e4SLinus Torvalds { 1489bfe86024SJeff Layton bool has_lease = false; 1490bfe86024SJeff Layton struct file_lock *flock; 1491bfe86024SJeff Layton 1492bfe86024SJeff Layton if (inode->i_flock) { 1493bfe86024SJeff Layton spin_lock(&inode->i_lock); 1494bfe86024SJeff Layton flock = inode->i_flock; 14950ee5c6d6SJeff Layton if (flock && IS_LEASE(flock) && (flock->fl_type == F_WRLCK)) 1496bfe86024SJeff Layton has_lease = true; 1497bfe86024SJeff Layton spin_unlock(&inode->i_lock); 1498bfe86024SJeff Layton } 1499bfe86024SJeff Layton 1500bfe86024SJeff Layton if (has_lease) 15011da177e4SLinus Torvalds *time = current_fs_time(inode->i_sb); 15021da177e4SLinus Torvalds else 15031da177e4SLinus Torvalds *time = inode->i_mtime; 15041da177e4SLinus Torvalds } 15051da177e4SLinus Torvalds 15061da177e4SLinus Torvalds EXPORT_SYMBOL(lease_get_mtime); 15071da177e4SLinus Torvalds 15081da177e4SLinus Torvalds /** 15091da177e4SLinus Torvalds * fcntl_getlease - Enquire what lease is currently active 15101da177e4SLinus Torvalds * @filp: the file 15111da177e4SLinus Torvalds * 15121da177e4SLinus Torvalds * The value returned by this function will be one of 15131da177e4SLinus Torvalds * (if no lease break is pending): 15141da177e4SLinus Torvalds * 15151da177e4SLinus Torvalds * %F_RDLCK to indicate a shared lease is held. 15161da177e4SLinus Torvalds * 15171da177e4SLinus Torvalds * %F_WRLCK to indicate an exclusive lease is held. 15181da177e4SLinus Torvalds * 15191da177e4SLinus Torvalds * %F_UNLCK to indicate no lease is held. 15201da177e4SLinus Torvalds * 15211da177e4SLinus Torvalds * (if a lease break is pending): 15221da177e4SLinus Torvalds * 15231da177e4SLinus Torvalds * %F_RDLCK to indicate an exclusive lease needs to be 15241da177e4SLinus Torvalds * changed to a shared lease (or removed). 15251da177e4SLinus Torvalds * 15261da177e4SLinus Torvalds * %F_UNLCK to indicate the lease needs to be removed. 15271da177e4SLinus Torvalds * 15281da177e4SLinus Torvalds * XXX: sfr & willy disagree over whether F_INPROGRESS 15291da177e4SLinus Torvalds * should be returned to userspace. 15301da177e4SLinus Torvalds */ 15311da177e4SLinus Torvalds int fcntl_getlease(struct file *filp) 15321da177e4SLinus Torvalds { 15331da177e4SLinus Torvalds struct file_lock *fl; 15341c8c601aSJeff Layton struct inode *inode = file_inode(filp); 15351da177e4SLinus Torvalds int type = F_UNLCK; 1536c45198edSJeff Layton LIST_HEAD(dispose); 15371da177e4SLinus Torvalds 15381c8c601aSJeff Layton spin_lock(&inode->i_lock); 1539c45198edSJeff Layton time_out_leases(file_inode(filp), &dispose); 1540496ad9aaSAl Viro for (fl = file_inode(filp)->i_flock; fl && IS_LEASE(fl); 15411da177e4SLinus Torvalds fl = fl->fl_next) { 15421da177e4SLinus Torvalds if (fl->fl_file == filp) { 1543778fc546SJ. Bruce Fields type = target_leasetype(fl); 15441da177e4SLinus Torvalds break; 15451da177e4SLinus Torvalds } 15461da177e4SLinus Torvalds } 15471c8c601aSJeff Layton spin_unlock(&inode->i_lock); 1548c45198edSJeff Layton locks_dispose_list(&dispose); 15491da177e4SLinus Torvalds return type; 15501da177e4SLinus Torvalds } 15511da177e4SLinus Torvalds 155224cbe784SJeff Layton /** 155324cbe784SJeff Layton * check_conflicting_open - see if the given dentry points to a file that has 155424cbe784SJeff Layton * an existing open that would conflict with the 155524cbe784SJeff Layton * desired lease. 155624cbe784SJeff Layton * @dentry: dentry to check 155724cbe784SJeff Layton * @arg: type of lease that we're trying to acquire 155824cbe784SJeff Layton * 155924cbe784SJeff Layton * Check to see if there's an existing open fd on this file that would 156024cbe784SJeff Layton * conflict with the lease we're trying to set. 156124cbe784SJeff Layton */ 156224cbe784SJeff Layton static int 156324cbe784SJeff Layton check_conflicting_open(const struct dentry *dentry, const long arg) 156424cbe784SJeff Layton { 156524cbe784SJeff Layton int ret = 0; 156624cbe784SJeff Layton struct inode *inode = dentry->d_inode; 156724cbe784SJeff Layton 156824cbe784SJeff Layton if ((arg == F_RDLCK) && (atomic_read(&inode->i_writecount) > 0)) 156924cbe784SJeff Layton return -EAGAIN; 157024cbe784SJeff Layton 157124cbe784SJeff Layton if ((arg == F_WRLCK) && ((d_count(dentry) > 1) || 157224cbe784SJeff Layton (atomic_read(&inode->i_count) > 1))) 157324cbe784SJeff Layton ret = -EAGAIN; 157424cbe784SJeff Layton 157524cbe784SJeff Layton return ret; 157624cbe784SJeff Layton } 157724cbe784SJeff Layton 1578e6f5c789SJeff Layton static int 1579e6f5c789SJeff Layton generic_add_lease(struct file *filp, long arg, struct file_lock **flp, void **priv) 15801da177e4SLinus Torvalds { 15817eaae282SKAMBAROV, ZAUR struct file_lock *fl, **before, **my_before = NULL, *lease; 15820f7fc9e4SJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 15831da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 1584df4e8d2cSJ. Bruce Fields bool is_deleg = (*flp)->fl_flags & FL_DELEG; 1585c1f24ef4SJ. Bruce Fields int error; 1586c45198edSJeff Layton LIST_HEAD(dispose); 15871da177e4SLinus Torvalds 1588096657b6SJ. Bruce Fields lease = *flp; 158962af4f1fSJeff Layton trace_generic_add_lease(inode, lease); 159062af4f1fSJeff Layton 1591df4e8d2cSJ. Bruce Fields /* 1592df4e8d2cSJ. Bruce Fields * In the delegation case we need mutual exclusion with 1593df4e8d2cSJ. Bruce Fields * a number of operations that take the i_mutex. We trylock 1594df4e8d2cSJ. Bruce Fields * because delegations are an optional optimization, and if 1595df4e8d2cSJ. Bruce Fields * there's some chance of a conflict--we'd rather not 1596df4e8d2cSJ. Bruce Fields * bother, maybe that's a sign this just isn't a good file to 1597df4e8d2cSJ. Bruce Fields * hand out a delegation on. 1598df4e8d2cSJ. Bruce Fields */ 1599df4e8d2cSJ. Bruce Fields if (is_deleg && !mutex_trylock(&inode->i_mutex)) 1600df4e8d2cSJ. Bruce Fields return -EAGAIN; 1601df4e8d2cSJ. Bruce Fields 1602df4e8d2cSJ. Bruce Fields if (is_deleg && arg == F_WRLCK) { 1603df4e8d2cSJ. Bruce Fields /* Write delegations are not currently supported: */ 16044fdb793fSDan Carpenter mutex_unlock(&inode->i_mutex); 1605df4e8d2cSJ. Bruce Fields WARN_ON_ONCE(1); 1606df4e8d2cSJ. Bruce Fields return -EINVAL; 1607df4e8d2cSJ. Bruce Fields } 1608096657b6SJ. Bruce Fields 1609f82b4b67SJeff Layton spin_lock(&inode->i_lock); 1610c45198edSJeff Layton time_out_leases(inode, &dispose); 161124cbe784SJeff Layton error = check_conflicting_open(dentry, arg); 161224cbe784SJeff Layton if (error) 16131da177e4SLinus Torvalds goto out; 161485c59580SPavel Emelyanov 16151da177e4SLinus Torvalds /* 16161da177e4SLinus Torvalds * At this point, we know that if there is an exclusive 16171da177e4SLinus Torvalds * lease on this file, then we hold it on this filp 16181da177e4SLinus Torvalds * (otherwise our open of this file would have blocked). 16191da177e4SLinus Torvalds * And if we are trying to acquire an exclusive lease, 16201da177e4SLinus Torvalds * then the file is not open by anyone (including us) 16211da177e4SLinus Torvalds * except for this filp. 16221da177e4SLinus Torvalds */ 1623c1f24ef4SJ. Bruce Fields error = -EAGAIN; 16241da177e4SLinus Torvalds for (before = &inode->i_flock; 16251da177e4SLinus Torvalds ((fl = *before) != NULL) && IS_LEASE(fl); 16261da177e4SLinus Torvalds before = &fl->fl_next) { 1627c1f24ef4SJ. Bruce Fields if (fl->fl_file == filp) { 16281da177e4SLinus Torvalds my_before = before; 1629c1f24ef4SJ. Bruce Fields continue; 16301da177e4SLinus Torvalds } 1631c1f24ef4SJ. Bruce Fields /* 1632c1f24ef4SJ. Bruce Fields * No exclusive leases if someone else has a lease on 1633c1f24ef4SJ. Bruce Fields * this file: 1634c1f24ef4SJ. Bruce Fields */ 1635c1f24ef4SJ. Bruce Fields if (arg == F_WRLCK) 16361da177e4SLinus Torvalds goto out; 1637c1f24ef4SJ. Bruce Fields /* 1638c1f24ef4SJ. Bruce Fields * Modifying our existing lease is OK, but no getting a 1639c1f24ef4SJ. Bruce Fields * new lease if someone else is opening for write: 1640c1f24ef4SJ. Bruce Fields */ 1641c1f24ef4SJ. Bruce Fields if (fl->fl_flags & FL_UNLOCK_PENDING) 1642c1f24ef4SJ. Bruce Fields goto out; 1643c1f24ef4SJ. Bruce Fields } 16441da177e4SLinus Torvalds 16451da177e4SLinus Torvalds if (my_before != NULL) { 16461c7dd2ffSJeff Layton lease = *my_before; 1647c45198edSJeff Layton error = lease->fl_lmops->lm_change(my_before, arg, &dispose); 16481c7dd2ffSJeff Layton if (error) 16491da177e4SLinus Torvalds goto out; 16501c7dd2ffSJeff Layton goto out_setup; 16511da177e4SLinus Torvalds } 16521da177e4SLinus Torvalds 16531da177e4SLinus Torvalds error = -EINVAL; 16541da177e4SLinus Torvalds if (!leases_enable) 16551da177e4SLinus Torvalds goto out; 16561da177e4SLinus Torvalds 1657c5b1f0d9SArnd Bergmann locks_insert_lock(before, lease); 165824cbe784SJeff Layton /* 165924cbe784SJeff Layton * The check in break_lease() is lockless. It's possible for another 166024cbe784SJeff Layton * open to race in after we did the earlier check for a conflicting 166124cbe784SJeff Layton * open but before the lease was inserted. Check again for a 166224cbe784SJeff Layton * conflicting open and cancel the lease if there is one. 166324cbe784SJeff Layton * 166424cbe784SJeff Layton * We also add a barrier here to ensure that the insertion of the lock 166524cbe784SJeff Layton * precedes these checks. 166624cbe784SJeff Layton */ 166724cbe784SJeff Layton smp_mb(); 166824cbe784SJeff Layton error = check_conflicting_open(dentry, arg); 166924cbe784SJeff Layton if (error) 1670e6f5c789SJeff Layton goto out_unlink; 16711c7dd2ffSJeff Layton 16721c7dd2ffSJeff Layton out_setup: 16731c7dd2ffSJeff Layton if (lease->fl_lmops->lm_setup) 16741c7dd2ffSJeff Layton lease->fl_lmops->lm_setup(lease, priv); 16751da177e4SLinus Torvalds out: 1676f82b4b67SJeff Layton spin_unlock(&inode->i_lock); 1677c45198edSJeff Layton locks_dispose_list(&dispose); 1678df4e8d2cSJ. Bruce Fields if (is_deleg) 1679df4e8d2cSJ. Bruce Fields mutex_unlock(&inode->i_mutex); 16801c7dd2ffSJeff Layton if (!error && !my_before) 16811c7dd2ffSJeff Layton *flp = NULL; 16821da177e4SLinus Torvalds return error; 1683e6f5c789SJeff Layton out_unlink: 1684e6f5c789SJeff Layton locks_unlink_lock(before); 1685e6f5c789SJeff Layton goto out; 16861da177e4SLinus Torvalds } 16878335ebd9SJ. Bruce Fields 16880efaa7e8SJeff Layton static int generic_delete_lease(struct file *filp) 16898335ebd9SJ. Bruce Fields { 16900efaa7e8SJeff Layton int error = -EAGAIN; 16918335ebd9SJ. Bruce Fields struct file_lock *fl, **before; 16928335ebd9SJ. Bruce Fields struct dentry *dentry = filp->f_path.dentry; 16938335ebd9SJ. Bruce Fields struct inode *inode = dentry->d_inode; 1694c45198edSJeff Layton LIST_HEAD(dispose); 16958335ebd9SJ. Bruce Fields 1696f82b4b67SJeff Layton spin_lock(&inode->i_lock); 1697c45198edSJeff Layton time_out_leases(inode, &dispose); 16988335ebd9SJ. Bruce Fields for (before = &inode->i_flock; 16998335ebd9SJ. Bruce Fields ((fl = *before) != NULL) && IS_LEASE(fl); 17008335ebd9SJ. Bruce Fields before = &fl->fl_next) { 17010efaa7e8SJeff Layton if (fl->fl_file == filp) 17020efaa7e8SJeff Layton break; 17038335ebd9SJ. Bruce Fields } 17040efaa7e8SJeff Layton trace_generic_delete_lease(inode, fl); 17050efaa7e8SJeff Layton if (fl) 1706c45198edSJeff Layton error = fl->fl_lmops->lm_change(before, F_UNLCK, &dispose); 1707f82b4b67SJeff Layton spin_unlock(&inode->i_lock); 1708c45198edSJeff Layton locks_dispose_list(&dispose); 17090efaa7e8SJeff Layton return error; 17108335ebd9SJ. Bruce Fields } 17118335ebd9SJ. Bruce Fields 17121da177e4SLinus Torvalds /** 17131da177e4SLinus Torvalds * generic_setlease - sets a lease on an open file 17141da177e4SLinus Torvalds * @filp: file pointer 17151da177e4SLinus Torvalds * @arg: type of lease to obtain 17161da177e4SLinus Torvalds * @flp: input - file_lock to use, output - file_lock inserted 17171c7dd2ffSJeff Layton * @priv: private data for lm_setup (may be NULL if lm_setup 17181c7dd2ffSJeff Layton * doesn't require it) 17191da177e4SLinus Torvalds * 17201da177e4SLinus Torvalds * The (input) flp->fl_lmops->lm_break function is required 17211da177e4SLinus Torvalds * by break_lease(). 17221da177e4SLinus Torvalds */ 1723e6f5c789SJeff Layton int generic_setlease(struct file *filp, long arg, struct file_lock **flp, 1724e6f5c789SJeff Layton void **priv) 17251da177e4SLinus Torvalds { 17261da177e4SLinus Torvalds struct dentry *dentry = filp->f_path.dentry; 17271da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 17288335ebd9SJ. Bruce Fields int error; 17291da177e4SLinus Torvalds 17308e96e3b7SEric W. Biederman if ((!uid_eq(current_fsuid(), inode->i_uid)) && !capable(CAP_LEASE)) 17318335ebd9SJ. Bruce Fields return -EACCES; 17321da177e4SLinus Torvalds if (!S_ISREG(inode->i_mode)) 17338335ebd9SJ. Bruce Fields return -EINVAL; 17341da177e4SLinus Torvalds error = security_file_lock(filp, arg); 17351da177e4SLinus Torvalds if (error) 17368335ebd9SJ. Bruce Fields return error; 17371da177e4SLinus Torvalds 17388335ebd9SJ. Bruce Fields switch (arg) { 17398335ebd9SJ. Bruce Fields case F_UNLCK: 17400efaa7e8SJeff Layton return generic_delete_lease(filp); 17418335ebd9SJ. Bruce Fields case F_RDLCK: 17428335ebd9SJ. Bruce Fields case F_WRLCK: 17430efaa7e8SJeff Layton if (!(*flp)->fl_lmops->lm_break) { 17440efaa7e8SJeff Layton WARN_ON_ONCE(1); 17450efaa7e8SJeff Layton return -ENOLCK; 17460efaa7e8SJeff Layton } 1747e6f5c789SJeff Layton return generic_add_lease(filp, arg, flp, priv); 17488335ebd9SJ. Bruce Fields default: 17498d657eb3SDave Jones return -EINVAL; 17501da177e4SLinus Torvalds } 17511da177e4SLinus Torvalds } 17520af1a450SChristoph Hellwig EXPORT_SYMBOL(generic_setlease); 17531da177e4SLinus Torvalds 17541da177e4SLinus Torvalds /** 1755a9933ceaSJ. Bruce Fields * vfs_setlease - sets a lease on an open file 17561da177e4SLinus Torvalds * @filp: file pointer 17571da177e4SLinus Torvalds * @arg: type of lease to obtain 1758e51673aaSJeff Layton * @lease: file_lock to use when adding a lease 17591c7dd2ffSJeff Layton * @priv: private info for lm_setup when adding a lease (may be 17601c7dd2ffSJeff Layton * NULL if lm_setup doesn't require it) 17611da177e4SLinus Torvalds * 1762e51673aaSJeff Layton * Call this to establish a lease on the file. The "lease" argument is not 1763e51673aaSJeff Layton * used for F_UNLCK requests and may be NULL. For commands that set or alter 1764e51673aaSJeff Layton * an existing lease, the (*lease)->fl_lmops->lm_break operation must be set; 1765e51673aaSJeff Layton * if not, this function will return -ENOLCK (and generate a scary-looking 1766e51673aaSJeff Layton * stack trace). 17671c7dd2ffSJeff Layton * 17681c7dd2ffSJeff Layton * The "priv" pointer is passed directly to the lm_setup function as-is. It 17691c7dd2ffSJeff Layton * may be NULL if the lm_setup operation doesn't require it. 17701da177e4SLinus Torvalds */ 1771e6f5c789SJeff Layton int 1772e6f5c789SJeff Layton vfs_setlease(struct file *filp, long arg, struct file_lock **lease, void **priv) 17731da177e4SLinus Torvalds { 17741c7dd2ffSJeff Layton if (filp->f_op->setlease) 1775f82b4b67SJeff Layton return filp->f_op->setlease(filp, arg, lease, priv); 17761c7dd2ffSJeff Layton else 1777f82b4b67SJeff Layton return generic_setlease(filp, arg, lease, priv); 17781da177e4SLinus Torvalds } 1779a9933ceaSJ. Bruce Fields EXPORT_SYMBOL_GPL(vfs_setlease); 17801da177e4SLinus Torvalds 17810ceaf6c7SJ. Bruce Fields static int do_fcntl_add_lease(unsigned int fd, struct file *filp, long arg) 17821da177e4SLinus Torvalds { 17831c7dd2ffSJeff Layton struct file_lock *fl; 1784f7347ce4SLinus Torvalds struct fasync_struct *new; 17851da177e4SLinus Torvalds int error; 17861da177e4SLinus Torvalds 1787c5b1f0d9SArnd Bergmann fl = lease_alloc(filp, arg); 1788c5b1f0d9SArnd Bergmann if (IS_ERR(fl)) 1789c5b1f0d9SArnd Bergmann return PTR_ERR(fl); 17901da177e4SLinus Torvalds 1791f7347ce4SLinus Torvalds new = fasync_alloc(); 1792f7347ce4SLinus Torvalds if (!new) { 1793f7347ce4SLinus Torvalds locks_free_lock(fl); 1794f7347ce4SLinus Torvalds return -ENOMEM; 1795f7347ce4SLinus Torvalds } 17961c7dd2ffSJeff Layton new->fa_fd = fd; 17971da177e4SLinus Torvalds 17981c7dd2ffSJeff Layton error = vfs_setlease(filp, arg, &fl, (void **)&new); 17992dfb928fSJeff Layton if (fl) 18002dfb928fSJeff Layton locks_free_lock(fl); 1801f7347ce4SLinus Torvalds if (new) 1802f7347ce4SLinus Torvalds fasync_free(new); 18031da177e4SLinus Torvalds return error; 18041da177e4SLinus Torvalds } 18051da177e4SLinus Torvalds 18061da177e4SLinus Torvalds /** 18070ceaf6c7SJ. Bruce Fields * fcntl_setlease - sets a lease on an open file 18080ceaf6c7SJ. Bruce Fields * @fd: open file descriptor 18090ceaf6c7SJ. Bruce Fields * @filp: file pointer 18100ceaf6c7SJ. Bruce Fields * @arg: type of lease to obtain 18110ceaf6c7SJ. Bruce Fields * 18120ceaf6c7SJ. Bruce Fields * Call this fcntl to establish a lease on the file. 18130ceaf6c7SJ. Bruce Fields * Note that you also need to call %F_SETSIG to 18140ceaf6c7SJ. Bruce Fields * receive a signal when the lease is broken. 18150ceaf6c7SJ. Bruce Fields */ 18160ceaf6c7SJ. Bruce Fields int fcntl_setlease(unsigned int fd, struct file *filp, long arg) 18170ceaf6c7SJ. Bruce Fields { 18180ceaf6c7SJ. Bruce Fields if (arg == F_UNLCK) 1819e6f5c789SJeff Layton return vfs_setlease(filp, F_UNLCK, NULL, NULL); 18200ceaf6c7SJ. Bruce Fields return do_fcntl_add_lease(fd, filp, arg); 18210ceaf6c7SJ. Bruce Fields } 18220ceaf6c7SJ. Bruce Fields 18230ceaf6c7SJ. Bruce Fields /** 18241da177e4SLinus Torvalds * flock_lock_file_wait - Apply a FLOCK-style lock to a file 18251da177e4SLinus Torvalds * @filp: The file to apply the lock to 18261da177e4SLinus Torvalds * @fl: The lock to be applied 18271da177e4SLinus Torvalds * 18281da177e4SLinus Torvalds * Add a FLOCK style lock to a file. 18291da177e4SLinus Torvalds */ 18301da177e4SLinus Torvalds int flock_lock_file_wait(struct file *filp, struct file_lock *fl) 18311da177e4SLinus Torvalds { 18321da177e4SLinus Torvalds int error; 18331da177e4SLinus Torvalds might_sleep(); 18341da177e4SLinus Torvalds for (;;) { 18351da177e4SLinus Torvalds error = flock_lock_file(filp, fl); 1836bde74e4bSMiklos Szeredi if (error != FILE_LOCK_DEFERRED) 18371da177e4SLinus Torvalds break; 18381da177e4SLinus Torvalds error = wait_event_interruptible(fl->fl_wait, !fl->fl_next); 18391da177e4SLinus Torvalds if (!error) 18401da177e4SLinus Torvalds continue; 18411da177e4SLinus Torvalds 18421da177e4SLinus Torvalds locks_delete_block(fl); 18431da177e4SLinus Torvalds break; 18441da177e4SLinus Torvalds } 18451da177e4SLinus Torvalds return error; 18461da177e4SLinus Torvalds } 18471da177e4SLinus Torvalds 18481da177e4SLinus Torvalds EXPORT_SYMBOL(flock_lock_file_wait); 18491da177e4SLinus Torvalds 18501da177e4SLinus Torvalds /** 18511da177e4SLinus Torvalds * sys_flock: - flock() system call. 18521da177e4SLinus Torvalds * @fd: the file descriptor to lock. 18531da177e4SLinus Torvalds * @cmd: the type of lock to apply. 18541da177e4SLinus Torvalds * 18551da177e4SLinus Torvalds * Apply a %FL_FLOCK style lock to an open file descriptor. 18561da177e4SLinus Torvalds * The @cmd can be one of 18571da177e4SLinus Torvalds * 18581da177e4SLinus Torvalds * %LOCK_SH -- a shared lock. 18591da177e4SLinus Torvalds * 18601da177e4SLinus Torvalds * %LOCK_EX -- an exclusive lock. 18611da177e4SLinus Torvalds * 18621da177e4SLinus Torvalds * %LOCK_UN -- remove an existing lock. 18631da177e4SLinus Torvalds * 18641da177e4SLinus Torvalds * %LOCK_MAND -- a `mandatory' flock. This exists to emulate Windows Share Modes. 18651da177e4SLinus Torvalds * 18661da177e4SLinus Torvalds * %LOCK_MAND can be combined with %LOCK_READ or %LOCK_WRITE to allow other 18671da177e4SLinus Torvalds * processes read and write access respectively. 18681da177e4SLinus Torvalds */ 1869002c8976SHeiko Carstens SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd) 18701da177e4SLinus Torvalds { 18712903ff01SAl Viro struct fd f = fdget(fd); 18721da177e4SLinus Torvalds struct file_lock *lock; 18731da177e4SLinus Torvalds int can_sleep, unlock; 18741da177e4SLinus Torvalds int error; 18751da177e4SLinus Torvalds 18761da177e4SLinus Torvalds error = -EBADF; 18772903ff01SAl Viro if (!f.file) 18781da177e4SLinus Torvalds goto out; 18791da177e4SLinus Torvalds 18801da177e4SLinus Torvalds can_sleep = !(cmd & LOCK_NB); 18811da177e4SLinus Torvalds cmd &= ~LOCK_NB; 18821da177e4SLinus Torvalds unlock = (cmd == LOCK_UN); 18831da177e4SLinus Torvalds 1884aeb5d727SAl Viro if (!unlock && !(cmd & LOCK_MAND) && 18852903ff01SAl Viro !(f.file->f_mode & (FMODE_READ|FMODE_WRITE))) 18861da177e4SLinus Torvalds goto out_putf; 18871da177e4SLinus Torvalds 18882903ff01SAl Viro error = flock_make_lock(f.file, &lock, cmd); 18891da177e4SLinus Torvalds if (error) 18901da177e4SLinus Torvalds goto out_putf; 18911da177e4SLinus Torvalds if (can_sleep) 18921da177e4SLinus Torvalds lock->fl_flags |= FL_SLEEP; 18931da177e4SLinus Torvalds 18942903ff01SAl Viro error = security_file_lock(f.file, lock->fl_type); 18951da177e4SLinus Torvalds if (error) 18961da177e4SLinus Torvalds goto out_free; 18971da177e4SLinus Torvalds 189872c2d531SAl Viro if (f.file->f_op->flock) 18992903ff01SAl Viro error = f.file->f_op->flock(f.file, 19001da177e4SLinus Torvalds (can_sleep) ? F_SETLKW : F_SETLK, 19011da177e4SLinus Torvalds lock); 19021da177e4SLinus Torvalds else 19032903ff01SAl Viro error = flock_lock_file_wait(f.file, lock); 19041da177e4SLinus Torvalds 19051da177e4SLinus Torvalds out_free: 19061da177e4SLinus Torvalds locks_free_lock(lock); 19071da177e4SLinus Torvalds 19081da177e4SLinus Torvalds out_putf: 19092903ff01SAl Viro fdput(f); 19101da177e4SLinus Torvalds out: 19111da177e4SLinus Torvalds return error; 19121da177e4SLinus Torvalds } 19131da177e4SLinus Torvalds 19143ee17abdSJ. Bruce Fields /** 19153ee17abdSJ. Bruce Fields * vfs_test_lock - test file byte range lock 19163ee17abdSJ. Bruce Fields * @filp: The file to test lock for 19176924c554SJ. Bruce Fields * @fl: The lock to test; also used to hold result 19183ee17abdSJ. Bruce Fields * 19193ee17abdSJ. Bruce Fields * Returns -ERRNO on failure. Indicates presence of conflicting lock by 19203ee17abdSJ. Bruce Fields * setting conf->fl_type to something other than F_UNLCK. 19213ee17abdSJ. Bruce Fields */ 19223ee17abdSJ. Bruce Fields int vfs_test_lock(struct file *filp, struct file_lock *fl) 19233ee17abdSJ. Bruce Fields { 192472c2d531SAl Viro if (filp->f_op->lock) 19253ee17abdSJ. Bruce Fields return filp->f_op->lock(filp, F_GETLK, fl); 19263ee17abdSJ. Bruce Fields posix_test_lock(filp, fl); 19273ee17abdSJ. Bruce Fields return 0; 19283ee17abdSJ. Bruce Fields } 19293ee17abdSJ. Bruce Fields EXPORT_SYMBOL_GPL(vfs_test_lock); 19303ee17abdSJ. Bruce Fields 1931c2fa1b8aSJ. Bruce Fields static int posix_lock_to_flock(struct flock *flock, struct file_lock *fl) 1932c2fa1b8aSJ. Bruce Fields { 1933cff2fce5SJeff Layton flock->l_pid = IS_OFDLCK(fl) ? -1 : fl->fl_pid; 1934c2fa1b8aSJ. Bruce Fields #if BITS_PER_LONG == 32 1935c2fa1b8aSJ. Bruce Fields /* 1936c2fa1b8aSJ. Bruce Fields * Make sure we can represent the posix lock via 1937c2fa1b8aSJ. Bruce Fields * legacy 32bit flock. 1938c2fa1b8aSJ. Bruce Fields */ 1939c2fa1b8aSJ. Bruce Fields if (fl->fl_start > OFFT_OFFSET_MAX) 1940c2fa1b8aSJ. Bruce Fields return -EOVERFLOW; 1941c2fa1b8aSJ. Bruce Fields if (fl->fl_end != OFFSET_MAX && fl->fl_end > OFFT_OFFSET_MAX) 1942c2fa1b8aSJ. Bruce Fields return -EOVERFLOW; 1943c2fa1b8aSJ. Bruce Fields #endif 1944c2fa1b8aSJ. Bruce Fields flock->l_start = fl->fl_start; 1945c2fa1b8aSJ. Bruce Fields flock->l_len = fl->fl_end == OFFSET_MAX ? 0 : 1946c2fa1b8aSJ. Bruce Fields fl->fl_end - fl->fl_start + 1; 1947c2fa1b8aSJ. Bruce Fields flock->l_whence = 0; 1948129a84deSJ. Bruce Fields flock->l_type = fl->fl_type; 1949c2fa1b8aSJ. Bruce Fields return 0; 1950c2fa1b8aSJ. Bruce Fields } 1951c2fa1b8aSJ. Bruce Fields 1952c2fa1b8aSJ. Bruce Fields #if BITS_PER_LONG == 32 1953c2fa1b8aSJ. Bruce Fields static void posix_lock_to_flock64(struct flock64 *flock, struct file_lock *fl) 1954c2fa1b8aSJ. Bruce Fields { 1955cff2fce5SJeff Layton flock->l_pid = IS_OFDLCK(fl) ? -1 : fl->fl_pid; 1956c2fa1b8aSJ. Bruce Fields flock->l_start = fl->fl_start; 1957c2fa1b8aSJ. Bruce Fields flock->l_len = fl->fl_end == OFFSET_MAX ? 0 : 1958c2fa1b8aSJ. Bruce Fields fl->fl_end - fl->fl_start + 1; 1959c2fa1b8aSJ. Bruce Fields flock->l_whence = 0; 1960c2fa1b8aSJ. Bruce Fields flock->l_type = fl->fl_type; 1961c2fa1b8aSJ. Bruce Fields } 1962c2fa1b8aSJ. Bruce Fields #endif 1963c2fa1b8aSJ. Bruce Fields 19641da177e4SLinus Torvalds /* Report the first existing lock that would conflict with l. 19651da177e4SLinus Torvalds * This implements the F_GETLK command of fcntl(). 19661da177e4SLinus Torvalds */ 1967c1e62b8fSJeff Layton int fcntl_getlk(struct file *filp, unsigned int cmd, struct flock __user *l) 19681da177e4SLinus Torvalds { 19699d6a8c5cSMarc Eshel struct file_lock file_lock; 19701da177e4SLinus Torvalds struct flock flock; 19711da177e4SLinus Torvalds int error; 19721da177e4SLinus Torvalds 19731da177e4SLinus Torvalds error = -EFAULT; 19741da177e4SLinus Torvalds if (copy_from_user(&flock, l, sizeof(flock))) 19751da177e4SLinus Torvalds goto out; 19761da177e4SLinus Torvalds error = -EINVAL; 19771da177e4SLinus Torvalds if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK)) 19781da177e4SLinus Torvalds goto out; 19791da177e4SLinus Torvalds 19801da177e4SLinus Torvalds error = flock_to_posix_lock(filp, &file_lock, &flock); 19811da177e4SLinus Torvalds if (error) 19821da177e4SLinus Torvalds goto out; 19831da177e4SLinus Torvalds 19840d3f7a2dSJeff Layton if (cmd == F_OFD_GETLK) { 198590478939SJeff Layton error = -EINVAL; 198690478939SJeff Layton if (flock.l_pid != 0) 198790478939SJeff Layton goto out; 198890478939SJeff Layton 19895d50ffd7SJeff Layton cmd = F_GETLK; 1990cff2fce5SJeff Layton file_lock.fl_flags |= FL_OFDLCK; 199173a8f5f7SChristoph Hellwig file_lock.fl_owner = filp; 19925d50ffd7SJeff Layton } 19935d50ffd7SJeff Layton 19943ee17abdSJ. Bruce Fields error = vfs_test_lock(filp, &file_lock); 19953ee17abdSJ. Bruce Fields if (error) 19961da177e4SLinus Torvalds goto out; 19971da177e4SLinus Torvalds 19989d6a8c5cSMarc Eshel flock.l_type = file_lock.fl_type; 19999d6a8c5cSMarc Eshel if (file_lock.fl_type != F_UNLCK) { 20009d6a8c5cSMarc Eshel error = posix_lock_to_flock(&flock, &file_lock); 2001c2fa1b8aSJ. Bruce Fields if (error) 2002f328296eSKinglong Mee goto rel_priv; 20031da177e4SLinus Torvalds } 20041da177e4SLinus Torvalds error = -EFAULT; 20051da177e4SLinus Torvalds if (!copy_to_user(l, &flock, sizeof(flock))) 20061da177e4SLinus Torvalds error = 0; 2007f328296eSKinglong Mee rel_priv: 2008f328296eSKinglong Mee locks_release_private(&file_lock); 20091da177e4SLinus Torvalds out: 20101da177e4SLinus Torvalds return error; 20111da177e4SLinus Torvalds } 20121da177e4SLinus Torvalds 20137723ec97SMarc Eshel /** 20147723ec97SMarc Eshel * vfs_lock_file - file byte range lock 20157723ec97SMarc Eshel * @filp: The file to apply the lock to 20167723ec97SMarc Eshel * @cmd: type of locking operation (F_SETLK, F_GETLK, etc.) 20177723ec97SMarc Eshel * @fl: The lock to be applied 2018150b3934SMarc Eshel * @conf: Place to return a copy of the conflicting lock, if found. 2019150b3934SMarc Eshel * 2020150b3934SMarc Eshel * A caller that doesn't care about the conflicting lock may pass NULL 2021150b3934SMarc Eshel * as the final argument. 2022150b3934SMarc Eshel * 2023150b3934SMarc Eshel * If the filesystem defines a private ->lock() method, then @conf will 2024150b3934SMarc Eshel * be left unchanged; so a caller that cares should initialize it to 2025150b3934SMarc Eshel * some acceptable default. 20262beb6614SMarc Eshel * 20272beb6614SMarc Eshel * To avoid blocking kernel daemons, such as lockd, that need to acquire POSIX 20282beb6614SMarc Eshel * locks, the ->lock() interface may return asynchronously, before the lock has 20292beb6614SMarc Eshel * been granted or denied by the underlying filesystem, if (and only if) 20308fb47a4fSJ. Bruce Fields * lm_grant is set. Callers expecting ->lock() to return asynchronously 20312beb6614SMarc Eshel * will only use F_SETLK, not F_SETLKW; they will set FL_SLEEP if (and only if) 20322beb6614SMarc Eshel * the request is for a blocking lock. When ->lock() does return asynchronously, 20338fb47a4fSJ. Bruce Fields * it must return FILE_LOCK_DEFERRED, and call ->lm_grant() when the lock 20342beb6614SMarc Eshel * request completes. 20352beb6614SMarc Eshel * If the request is for non-blocking lock the file system should return 2036bde74e4bSMiklos Szeredi * FILE_LOCK_DEFERRED then try to get the lock and call the callback routine 2037bde74e4bSMiklos Szeredi * with the result. If the request timed out the callback routine will return a 20382beb6614SMarc Eshel * nonzero return code and the file system should release the lock. The file 20392beb6614SMarc Eshel * system is also responsible to keep a corresponding posix lock when it 20402beb6614SMarc Eshel * grants a lock so the VFS can find out which locks are locally held and do 20412beb6614SMarc Eshel * the correct lock cleanup when required. 20422beb6614SMarc Eshel * The underlying filesystem must not drop the kernel lock or call 20438fb47a4fSJ. Bruce Fields * ->lm_grant() before returning to the caller with a FILE_LOCK_DEFERRED 20442beb6614SMarc Eshel * return code. 20457723ec97SMarc Eshel */ 2046150b3934SMarc Eshel int vfs_lock_file(struct file *filp, unsigned int cmd, struct file_lock *fl, struct file_lock *conf) 20477723ec97SMarc Eshel { 204872c2d531SAl Viro if (filp->f_op->lock) 20497723ec97SMarc Eshel return filp->f_op->lock(filp, cmd, fl); 20507723ec97SMarc Eshel else 2051150b3934SMarc Eshel return posix_lock_file(filp, fl, conf); 20527723ec97SMarc Eshel } 20537723ec97SMarc Eshel EXPORT_SYMBOL_GPL(vfs_lock_file); 20547723ec97SMarc Eshel 2055b648a6deSMiklos Szeredi static int do_lock_file_wait(struct file *filp, unsigned int cmd, 2056b648a6deSMiklos Szeredi struct file_lock *fl) 2057b648a6deSMiklos Szeredi { 2058b648a6deSMiklos Szeredi int error; 2059b648a6deSMiklos Szeredi 2060b648a6deSMiklos Szeredi error = security_file_lock(filp, fl->fl_type); 2061b648a6deSMiklos Szeredi if (error) 2062b648a6deSMiklos Szeredi return error; 2063b648a6deSMiklos Szeredi 2064b648a6deSMiklos Szeredi for (;;) { 2065764c76b3SMiklos Szeredi error = vfs_lock_file(filp, cmd, fl, NULL); 2066b648a6deSMiklos Szeredi if (error != FILE_LOCK_DEFERRED) 2067b648a6deSMiklos Szeredi break; 2068764c76b3SMiklos Szeredi error = wait_event_interruptible(fl->fl_wait, !fl->fl_next); 2069b648a6deSMiklos Szeredi if (!error) 2070b648a6deSMiklos Szeredi continue; 2071b648a6deSMiklos Szeredi 2072b648a6deSMiklos Szeredi locks_delete_block(fl); 2073b648a6deSMiklos Szeredi break; 2074b648a6deSMiklos Szeredi } 2075b648a6deSMiklos Szeredi 2076b648a6deSMiklos Szeredi return error; 2077b648a6deSMiklos Szeredi } 2078b648a6deSMiklos Szeredi 2079cf01f4eeSJeff Layton /* Ensure that fl->fl_filp has compatible f_mode for F_SETLK calls */ 2080cf01f4eeSJeff Layton static int 2081cf01f4eeSJeff Layton check_fmode_for_setlk(struct file_lock *fl) 2082cf01f4eeSJeff Layton { 2083cf01f4eeSJeff Layton switch (fl->fl_type) { 2084cf01f4eeSJeff Layton case F_RDLCK: 2085cf01f4eeSJeff Layton if (!(fl->fl_file->f_mode & FMODE_READ)) 2086cf01f4eeSJeff Layton return -EBADF; 2087cf01f4eeSJeff Layton break; 2088cf01f4eeSJeff Layton case F_WRLCK: 2089cf01f4eeSJeff Layton if (!(fl->fl_file->f_mode & FMODE_WRITE)) 2090cf01f4eeSJeff Layton return -EBADF; 2091cf01f4eeSJeff Layton } 2092cf01f4eeSJeff Layton return 0; 2093cf01f4eeSJeff Layton } 2094cf01f4eeSJeff Layton 20951da177e4SLinus Torvalds /* Apply the lock described by l to an open file descriptor. 20961da177e4SLinus Torvalds * This implements both the F_SETLK and F_SETLKW commands of fcntl(). 20971da177e4SLinus Torvalds */ 2098c293621bSPeter Staubach int fcntl_setlk(unsigned int fd, struct file *filp, unsigned int cmd, 2099c293621bSPeter Staubach struct flock __user *l) 21001da177e4SLinus Torvalds { 21011da177e4SLinus Torvalds struct file_lock *file_lock = locks_alloc_lock(); 21021da177e4SLinus Torvalds struct flock flock; 21031da177e4SLinus Torvalds struct inode *inode; 21040b2bac2fSAl Viro struct file *f; 21051da177e4SLinus Torvalds int error; 21061da177e4SLinus Torvalds 21071da177e4SLinus Torvalds if (file_lock == NULL) 21081da177e4SLinus Torvalds return -ENOLCK; 21091da177e4SLinus Torvalds 21101da177e4SLinus Torvalds /* 21111da177e4SLinus Torvalds * This might block, so we do it before checking the inode. 21121da177e4SLinus Torvalds */ 21131da177e4SLinus Torvalds error = -EFAULT; 21141da177e4SLinus Torvalds if (copy_from_user(&flock, l, sizeof(flock))) 21151da177e4SLinus Torvalds goto out; 21161da177e4SLinus Torvalds 2117496ad9aaSAl Viro inode = file_inode(filp); 21181da177e4SLinus Torvalds 21191da177e4SLinus Torvalds /* Don't allow mandatory locks on files that may be memory mapped 21201da177e4SLinus Torvalds * and shared. 21211da177e4SLinus Torvalds */ 2122a16877caSPavel Emelyanov if (mandatory_lock(inode) && mapping_writably_mapped(filp->f_mapping)) { 21231da177e4SLinus Torvalds error = -EAGAIN; 21241da177e4SLinus Torvalds goto out; 21251da177e4SLinus Torvalds } 21261da177e4SLinus Torvalds 2127c293621bSPeter Staubach again: 21281da177e4SLinus Torvalds error = flock_to_posix_lock(filp, file_lock, &flock); 21291da177e4SLinus Torvalds if (error) 21301da177e4SLinus Torvalds goto out; 21315d50ffd7SJeff Layton 2132cf01f4eeSJeff Layton error = check_fmode_for_setlk(file_lock); 2133cf01f4eeSJeff Layton if (error) 2134cf01f4eeSJeff Layton goto out; 2135cf01f4eeSJeff Layton 21365d50ffd7SJeff Layton /* 21375d50ffd7SJeff Layton * If the cmd is requesting file-private locks, then set the 2138cff2fce5SJeff Layton * FL_OFDLCK flag and override the owner. 21395d50ffd7SJeff Layton */ 21405d50ffd7SJeff Layton switch (cmd) { 21410d3f7a2dSJeff Layton case F_OFD_SETLK: 214290478939SJeff Layton error = -EINVAL; 214390478939SJeff Layton if (flock.l_pid != 0) 214490478939SJeff Layton goto out; 214590478939SJeff Layton 21465d50ffd7SJeff Layton cmd = F_SETLK; 2147cff2fce5SJeff Layton file_lock->fl_flags |= FL_OFDLCK; 214873a8f5f7SChristoph Hellwig file_lock->fl_owner = filp; 21495d50ffd7SJeff Layton break; 21500d3f7a2dSJeff Layton case F_OFD_SETLKW: 215190478939SJeff Layton error = -EINVAL; 215290478939SJeff Layton if (flock.l_pid != 0) 215390478939SJeff Layton goto out; 215490478939SJeff Layton 21555d50ffd7SJeff Layton cmd = F_SETLKW; 2156cff2fce5SJeff Layton file_lock->fl_flags |= FL_OFDLCK; 215773a8f5f7SChristoph Hellwig file_lock->fl_owner = filp; 21585d50ffd7SJeff Layton /* Fallthrough */ 21595d50ffd7SJeff Layton case F_SETLKW: 21601da177e4SLinus Torvalds file_lock->fl_flags |= FL_SLEEP; 21611da177e4SLinus Torvalds } 21621da177e4SLinus Torvalds 2163b648a6deSMiklos Szeredi error = do_lock_file_wait(filp, cmd, file_lock); 2164c293621bSPeter Staubach 2165c293621bSPeter Staubach /* 2166c293621bSPeter Staubach * Attempt to detect a close/fcntl race and recover by 2167c293621bSPeter Staubach * releasing the lock that was just acquired. 2168c293621bSPeter Staubach */ 21690b2bac2fSAl Viro /* 21700b2bac2fSAl Viro * we need that spin_lock here - it prevents reordering between 21710b2bac2fSAl Viro * update of inode->i_flock and check for it done in close(). 21720b2bac2fSAl Viro * rcu_read_lock() wouldn't do. 21730b2bac2fSAl Viro */ 21740b2bac2fSAl Viro spin_lock(¤t->files->file_lock); 21750b2bac2fSAl Viro f = fcheck(fd); 21760b2bac2fSAl Viro spin_unlock(¤t->files->file_lock); 21770b2bac2fSAl Viro if (!error && f != filp && flock.l_type != F_UNLCK) { 2178c293621bSPeter Staubach flock.l_type = F_UNLCK; 2179c293621bSPeter Staubach goto again; 2180c293621bSPeter Staubach } 21811da177e4SLinus Torvalds 21821da177e4SLinus Torvalds out: 21831da177e4SLinus Torvalds locks_free_lock(file_lock); 21841da177e4SLinus Torvalds return error; 21851da177e4SLinus Torvalds } 21861da177e4SLinus Torvalds 21871da177e4SLinus Torvalds #if BITS_PER_LONG == 32 21881da177e4SLinus Torvalds /* Report the first existing lock that would conflict with l. 21891da177e4SLinus Torvalds * This implements the F_GETLK command of fcntl(). 21901da177e4SLinus Torvalds */ 2191c1e62b8fSJeff Layton int fcntl_getlk64(struct file *filp, unsigned int cmd, struct flock64 __user *l) 21921da177e4SLinus Torvalds { 21939d6a8c5cSMarc Eshel struct file_lock file_lock; 21941da177e4SLinus Torvalds struct flock64 flock; 21951da177e4SLinus Torvalds int error; 21961da177e4SLinus Torvalds 21971da177e4SLinus Torvalds error = -EFAULT; 21981da177e4SLinus Torvalds if (copy_from_user(&flock, l, sizeof(flock))) 21991da177e4SLinus Torvalds goto out; 22001da177e4SLinus Torvalds error = -EINVAL; 22011da177e4SLinus Torvalds if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK)) 22021da177e4SLinus Torvalds goto out; 22031da177e4SLinus Torvalds 22041da177e4SLinus Torvalds error = flock64_to_posix_lock(filp, &file_lock, &flock); 22051da177e4SLinus Torvalds if (error) 22061da177e4SLinus Torvalds goto out; 22071da177e4SLinus Torvalds 22080d3f7a2dSJeff Layton if (cmd == F_OFD_GETLK) { 220990478939SJeff Layton error = -EINVAL; 221090478939SJeff Layton if (flock.l_pid != 0) 221190478939SJeff Layton goto out; 221290478939SJeff Layton 22135d50ffd7SJeff Layton cmd = F_GETLK64; 2214cff2fce5SJeff Layton file_lock.fl_flags |= FL_OFDLCK; 221573a8f5f7SChristoph Hellwig file_lock.fl_owner = filp; 22165d50ffd7SJeff Layton } 22175d50ffd7SJeff Layton 22183ee17abdSJ. Bruce Fields error = vfs_test_lock(filp, &file_lock); 22193ee17abdSJ. Bruce Fields if (error) 22201da177e4SLinus Torvalds goto out; 22211da177e4SLinus Torvalds 22229d6a8c5cSMarc Eshel flock.l_type = file_lock.fl_type; 22239d6a8c5cSMarc Eshel if (file_lock.fl_type != F_UNLCK) 22249d6a8c5cSMarc Eshel posix_lock_to_flock64(&flock, &file_lock); 22259d6a8c5cSMarc Eshel 22261da177e4SLinus Torvalds error = -EFAULT; 22271da177e4SLinus Torvalds if (!copy_to_user(l, &flock, sizeof(flock))) 22281da177e4SLinus Torvalds error = 0; 22291da177e4SLinus Torvalds 2230f328296eSKinglong Mee locks_release_private(&file_lock); 22311da177e4SLinus Torvalds out: 22321da177e4SLinus Torvalds return error; 22331da177e4SLinus Torvalds } 22341da177e4SLinus Torvalds 22351da177e4SLinus Torvalds /* Apply the lock described by l to an open file descriptor. 22361da177e4SLinus Torvalds * This implements both the F_SETLK and F_SETLKW commands of fcntl(). 22371da177e4SLinus Torvalds */ 2238c293621bSPeter Staubach int fcntl_setlk64(unsigned int fd, struct file *filp, unsigned int cmd, 2239c293621bSPeter Staubach struct flock64 __user *l) 22401da177e4SLinus Torvalds { 22411da177e4SLinus Torvalds struct file_lock *file_lock = locks_alloc_lock(); 22421da177e4SLinus Torvalds struct flock64 flock; 22431da177e4SLinus Torvalds struct inode *inode; 22440b2bac2fSAl Viro struct file *f; 22451da177e4SLinus Torvalds int error; 22461da177e4SLinus Torvalds 22471da177e4SLinus Torvalds if (file_lock == NULL) 22481da177e4SLinus Torvalds return -ENOLCK; 22491da177e4SLinus Torvalds 22501da177e4SLinus Torvalds /* 22511da177e4SLinus Torvalds * This might block, so we do it before checking the inode. 22521da177e4SLinus Torvalds */ 22531da177e4SLinus Torvalds error = -EFAULT; 22541da177e4SLinus Torvalds if (copy_from_user(&flock, l, sizeof(flock))) 22551da177e4SLinus Torvalds goto out; 22561da177e4SLinus Torvalds 2257496ad9aaSAl Viro inode = file_inode(filp); 22581da177e4SLinus Torvalds 22591da177e4SLinus Torvalds /* Don't allow mandatory locks on files that may be memory mapped 22601da177e4SLinus Torvalds * and shared. 22611da177e4SLinus Torvalds */ 2262a16877caSPavel Emelyanov if (mandatory_lock(inode) && mapping_writably_mapped(filp->f_mapping)) { 22631da177e4SLinus Torvalds error = -EAGAIN; 22641da177e4SLinus Torvalds goto out; 22651da177e4SLinus Torvalds } 22661da177e4SLinus Torvalds 2267c293621bSPeter Staubach again: 22681da177e4SLinus Torvalds error = flock64_to_posix_lock(filp, file_lock, &flock); 22691da177e4SLinus Torvalds if (error) 22701da177e4SLinus Torvalds goto out; 22715d50ffd7SJeff Layton 2272cf01f4eeSJeff Layton error = check_fmode_for_setlk(file_lock); 2273cf01f4eeSJeff Layton if (error) 2274cf01f4eeSJeff Layton goto out; 2275cf01f4eeSJeff Layton 22765d50ffd7SJeff Layton /* 22775d50ffd7SJeff Layton * If the cmd is requesting file-private locks, then set the 2278cff2fce5SJeff Layton * FL_OFDLCK flag and override the owner. 22795d50ffd7SJeff Layton */ 22805d50ffd7SJeff Layton switch (cmd) { 22810d3f7a2dSJeff Layton case F_OFD_SETLK: 228290478939SJeff Layton error = -EINVAL; 228390478939SJeff Layton if (flock.l_pid != 0) 228490478939SJeff Layton goto out; 228590478939SJeff Layton 22865d50ffd7SJeff Layton cmd = F_SETLK64; 2287cff2fce5SJeff Layton file_lock->fl_flags |= FL_OFDLCK; 228873a8f5f7SChristoph Hellwig file_lock->fl_owner = filp; 22895d50ffd7SJeff Layton break; 22900d3f7a2dSJeff Layton case F_OFD_SETLKW: 229190478939SJeff Layton error = -EINVAL; 229290478939SJeff Layton if (flock.l_pid != 0) 229390478939SJeff Layton goto out; 229490478939SJeff Layton 22955d50ffd7SJeff Layton cmd = F_SETLKW64; 2296cff2fce5SJeff Layton file_lock->fl_flags |= FL_OFDLCK; 229773a8f5f7SChristoph Hellwig file_lock->fl_owner = filp; 22985d50ffd7SJeff Layton /* Fallthrough */ 22995d50ffd7SJeff Layton case F_SETLKW64: 23001da177e4SLinus Torvalds file_lock->fl_flags |= FL_SLEEP; 23011da177e4SLinus Torvalds } 23021da177e4SLinus Torvalds 2303b648a6deSMiklos Szeredi error = do_lock_file_wait(filp, cmd, file_lock); 2304c293621bSPeter Staubach 2305c293621bSPeter Staubach /* 2306c293621bSPeter Staubach * Attempt to detect a close/fcntl race and recover by 2307c293621bSPeter Staubach * releasing the lock that was just acquired. 2308c293621bSPeter Staubach */ 23090b2bac2fSAl Viro spin_lock(¤t->files->file_lock); 23100b2bac2fSAl Viro f = fcheck(fd); 23110b2bac2fSAl Viro spin_unlock(¤t->files->file_lock); 23120b2bac2fSAl Viro if (!error && f != filp && flock.l_type != F_UNLCK) { 2313c293621bSPeter Staubach flock.l_type = F_UNLCK; 2314c293621bSPeter Staubach goto again; 2315c293621bSPeter Staubach } 23161da177e4SLinus Torvalds 23171da177e4SLinus Torvalds out: 23181da177e4SLinus Torvalds locks_free_lock(file_lock); 23191da177e4SLinus Torvalds return error; 23201da177e4SLinus Torvalds } 23211da177e4SLinus Torvalds #endif /* BITS_PER_LONG == 32 */ 23221da177e4SLinus Torvalds 23231da177e4SLinus Torvalds /* 23241da177e4SLinus Torvalds * This function is called when the file is being removed 23251da177e4SLinus Torvalds * from the task's fd array. POSIX locks belonging to this task 23261da177e4SLinus Torvalds * are deleted at this time. 23271da177e4SLinus Torvalds */ 23281da177e4SLinus Torvalds void locks_remove_posix(struct file *filp, fl_owner_t owner) 23291da177e4SLinus Torvalds { 2330ff7b86b8SMiklos Szeredi struct file_lock lock; 23311da177e4SLinus Torvalds 23321da177e4SLinus Torvalds /* 23331da177e4SLinus Torvalds * If there are no locks held on this file, we don't need to call 23341da177e4SLinus Torvalds * posix_lock_file(). Another process could be setting a lock on this 23351da177e4SLinus Torvalds * file at the same time, but we wouldn't remove that lock anyway. 23361da177e4SLinus Torvalds */ 2337496ad9aaSAl Viro if (!file_inode(filp)->i_flock) 23381da177e4SLinus Torvalds return; 23391da177e4SLinus Torvalds 23401da177e4SLinus Torvalds lock.fl_type = F_UNLCK; 234175e1fcc0SMiklos Szeredi lock.fl_flags = FL_POSIX | FL_CLOSE; 23421da177e4SLinus Torvalds lock.fl_start = 0; 23431da177e4SLinus Torvalds lock.fl_end = OFFSET_MAX; 23441da177e4SLinus Torvalds lock.fl_owner = owner; 23451da177e4SLinus Torvalds lock.fl_pid = current->tgid; 23461da177e4SLinus Torvalds lock.fl_file = filp; 23471da177e4SLinus Torvalds lock.fl_ops = NULL; 23481da177e4SLinus Torvalds lock.fl_lmops = NULL; 23491da177e4SLinus Torvalds 2350150b3934SMarc Eshel vfs_lock_file(filp, F_SETLK, &lock, NULL); 23511da177e4SLinus Torvalds 23521da177e4SLinus Torvalds if (lock.fl_ops && lock.fl_ops->fl_release_private) 23531da177e4SLinus Torvalds lock.fl_ops->fl_release_private(&lock); 23541da177e4SLinus Torvalds } 23551da177e4SLinus Torvalds 23561da177e4SLinus Torvalds EXPORT_SYMBOL(locks_remove_posix); 23571da177e4SLinus Torvalds 23581da177e4SLinus Torvalds /* 23591da177e4SLinus Torvalds * This function is called on the last close of an open file. 23601da177e4SLinus Torvalds */ 236178ed8a13SJeff Layton void locks_remove_file(struct file *filp) 23621da177e4SLinus Torvalds { 2363496ad9aaSAl Viro struct inode * inode = file_inode(filp); 23641da177e4SLinus Torvalds struct file_lock *fl; 23651da177e4SLinus Torvalds struct file_lock **before; 2366ed9814d8SJeff Layton LIST_HEAD(dispose); 23671da177e4SLinus Torvalds 23681da177e4SLinus Torvalds if (!inode->i_flock) 23691da177e4SLinus Torvalds return; 23701da177e4SLinus Torvalds 237173a8f5f7SChristoph Hellwig locks_remove_posix(filp, filp); 23725d50ffd7SJeff Layton 237372c2d531SAl Viro if (filp->f_op->flock) { 23741da177e4SLinus Torvalds struct file_lock fl = { 237573a8f5f7SChristoph Hellwig .fl_owner = filp, 23761da177e4SLinus Torvalds .fl_pid = current->tgid, 23771da177e4SLinus Torvalds .fl_file = filp, 23781da177e4SLinus Torvalds .fl_flags = FL_FLOCK, 23791da177e4SLinus Torvalds .fl_type = F_UNLCK, 23801da177e4SLinus Torvalds .fl_end = OFFSET_MAX, 23811da177e4SLinus Torvalds }; 23821da177e4SLinus Torvalds filp->f_op->flock(filp, F_SETLKW, &fl); 238380fec4c6STrond Myklebust if (fl.fl_ops && fl.fl_ops->fl_release_private) 238480fec4c6STrond Myklebust fl.fl_ops->fl_release_private(&fl); 23851da177e4SLinus Torvalds } 23861da177e4SLinus Torvalds 23871c8c601aSJeff Layton spin_lock(&inode->i_lock); 23881da177e4SLinus Torvalds before = &inode->i_flock; 23891da177e4SLinus Torvalds 23901da177e4SLinus Torvalds while ((fl = *before) != NULL) { 23911da177e4SLinus Torvalds if (fl->fl_file == filp) { 23921da177e4SLinus Torvalds if (IS_LEASE(fl)) { 2393c45198edSJeff Layton lease_modify(before, F_UNLCK, &dispose); 23941da177e4SLinus Torvalds continue; 23951da177e4SLinus Torvalds } 23968c3cac5eSJeff Layton 23978c3cac5eSJeff Layton /* 23988c3cac5eSJeff Layton * There's a leftover lock on the list of a type that 23998c3cac5eSJeff Layton * we didn't expect to see. Most likely a classic 24008c3cac5eSJeff Layton * POSIX lock that ended up not getting released 24018c3cac5eSJeff Layton * properly, or that raced onto the list somehow. Log 24028c3cac5eSJeff Layton * some info about it and then just remove it from 24038c3cac5eSJeff Layton * the list. 24048c3cac5eSJeff Layton */ 24058c3cac5eSJeff Layton WARN(!IS_FLOCK(fl), 24068c3cac5eSJeff Layton "leftover lock: dev=%u:%u ino=%lu type=%hhd flags=0x%x start=%lld end=%lld\n", 24078c3cac5eSJeff Layton MAJOR(inode->i_sb->s_dev), 24088c3cac5eSJeff Layton MINOR(inode->i_sb->s_dev), inode->i_ino, 24098c3cac5eSJeff Layton fl->fl_type, fl->fl_flags, 24108c3cac5eSJeff Layton fl->fl_start, fl->fl_end); 24118c3cac5eSJeff Layton 2412ed9814d8SJeff Layton locks_delete_lock(before, &dispose); 24138c3cac5eSJeff Layton continue; 24141da177e4SLinus Torvalds } 24151da177e4SLinus Torvalds before = &fl->fl_next; 24161da177e4SLinus Torvalds } 24171c8c601aSJeff Layton spin_unlock(&inode->i_lock); 2418ed9814d8SJeff Layton locks_dispose_list(&dispose); 24191da177e4SLinus Torvalds } 24201da177e4SLinus Torvalds 24211da177e4SLinus Torvalds /** 24221da177e4SLinus Torvalds * posix_unblock_lock - stop waiting for a file lock 24231da177e4SLinus Torvalds * @waiter: the lock which was waiting 24241da177e4SLinus Torvalds * 24251da177e4SLinus Torvalds * lockd needs to block waiting for locks. 24261da177e4SLinus Torvalds */ 242764a318eeSJ. Bruce Fields int 2428f891a29fSJeff Layton posix_unblock_lock(struct file_lock *waiter) 24291da177e4SLinus Torvalds { 243064a318eeSJ. Bruce Fields int status = 0; 243164a318eeSJ. Bruce Fields 24327b2296afSJeff Layton spin_lock(&blocked_lock_lock); 24335996a298SJ. Bruce Fields if (waiter->fl_next) 24341da177e4SLinus Torvalds __locks_delete_block(waiter); 243564a318eeSJ. Bruce Fields else 243664a318eeSJ. Bruce Fields status = -ENOENT; 24377b2296afSJeff Layton spin_unlock(&blocked_lock_lock); 243864a318eeSJ. Bruce Fields return status; 24391da177e4SLinus Torvalds } 24401da177e4SLinus Torvalds EXPORT_SYMBOL(posix_unblock_lock); 24411da177e4SLinus Torvalds 24429b9d2ab4SMarc Eshel /** 24439b9d2ab4SMarc Eshel * vfs_cancel_lock - file byte range unblock lock 24449b9d2ab4SMarc Eshel * @filp: The file to apply the unblock to 24459b9d2ab4SMarc Eshel * @fl: The lock to be unblocked 24469b9d2ab4SMarc Eshel * 24479b9d2ab4SMarc Eshel * Used by lock managers to cancel blocked requests 24489b9d2ab4SMarc Eshel */ 24499b9d2ab4SMarc Eshel int vfs_cancel_lock(struct file *filp, struct file_lock *fl) 24509b9d2ab4SMarc Eshel { 245172c2d531SAl Viro if (filp->f_op->lock) 24529b9d2ab4SMarc Eshel return filp->f_op->lock(filp, F_CANCELLK, fl); 24539b9d2ab4SMarc Eshel return 0; 24549b9d2ab4SMarc Eshel } 24559b9d2ab4SMarc Eshel 24569b9d2ab4SMarc Eshel EXPORT_SYMBOL_GPL(vfs_cancel_lock); 24579b9d2ab4SMarc Eshel 24587f8ada98SPavel Emelyanov #ifdef CONFIG_PROC_FS 2459d8ba7a36SAlexey Dobriyan #include <linux/proc_fs.h> 24607f8ada98SPavel Emelyanov #include <linux/seq_file.h> 24617f8ada98SPavel Emelyanov 24627012b02aSJeff Layton struct locks_iterator { 24637012b02aSJeff Layton int li_cpu; 24647012b02aSJeff Layton loff_t li_pos; 24657012b02aSJeff Layton }; 24667012b02aSJeff Layton 24677f8ada98SPavel Emelyanov static void lock_get_status(struct seq_file *f, struct file_lock *fl, 246899dc8292SJerome Marchand loff_t id, char *pfx) 24691da177e4SLinus Torvalds { 24701da177e4SLinus Torvalds struct inode *inode = NULL; 2471ab1f1611SVitaliy Gusev unsigned int fl_pid; 2472ab1f1611SVitaliy Gusev 2473ab1f1611SVitaliy Gusev if (fl->fl_nspid) 24746c5f3e7bSPavel Emelyanov fl_pid = pid_vnr(fl->fl_nspid); 2475ab1f1611SVitaliy Gusev else 2476ab1f1611SVitaliy Gusev fl_pid = fl->fl_pid; 24771da177e4SLinus Torvalds 24781da177e4SLinus Torvalds if (fl->fl_file != NULL) 2479496ad9aaSAl Viro inode = file_inode(fl->fl_file); 24801da177e4SLinus Torvalds 248199dc8292SJerome Marchand seq_printf(f, "%lld:%s ", id, pfx); 24821da177e4SLinus Torvalds if (IS_POSIX(fl)) { 2483c918d42aSJeff Layton if (fl->fl_flags & FL_ACCESS) 24845315c26aSFabian Frederick seq_puts(f, "ACCESS"); 2485cff2fce5SJeff Layton else if (IS_OFDLCK(fl)) 24865315c26aSFabian Frederick seq_puts(f, "OFDLCK"); 2487c918d42aSJeff Layton else 24885315c26aSFabian Frederick seq_puts(f, "POSIX "); 2489c918d42aSJeff Layton 2490c918d42aSJeff Layton seq_printf(f, " %s ", 24911da177e4SLinus Torvalds (inode == NULL) ? "*NOINODE*" : 2492a16877caSPavel Emelyanov mandatory_lock(inode) ? "MANDATORY" : "ADVISORY "); 24931da177e4SLinus Torvalds } else if (IS_FLOCK(fl)) { 24941da177e4SLinus Torvalds if (fl->fl_type & LOCK_MAND) { 24955315c26aSFabian Frederick seq_puts(f, "FLOCK MSNFS "); 24961da177e4SLinus Torvalds } else { 24975315c26aSFabian Frederick seq_puts(f, "FLOCK ADVISORY "); 24981da177e4SLinus Torvalds } 24991da177e4SLinus Torvalds } else if (IS_LEASE(fl)) { 25008144f1f6SJeff Layton if (fl->fl_flags & FL_DELEG) 25018144f1f6SJeff Layton seq_puts(f, "DELEG "); 25028144f1f6SJeff Layton else 25035315c26aSFabian Frederick seq_puts(f, "LEASE "); 25048144f1f6SJeff Layton 2505ab83fa4bSJ. Bruce Fields if (lease_breaking(fl)) 25065315c26aSFabian Frederick seq_puts(f, "BREAKING "); 25071da177e4SLinus Torvalds else if (fl->fl_file) 25085315c26aSFabian Frederick seq_puts(f, "ACTIVE "); 25091da177e4SLinus Torvalds else 25105315c26aSFabian Frederick seq_puts(f, "BREAKER "); 25111da177e4SLinus Torvalds } else { 25125315c26aSFabian Frederick seq_puts(f, "UNKNOWN UNKNOWN "); 25131da177e4SLinus Torvalds } 25141da177e4SLinus Torvalds if (fl->fl_type & LOCK_MAND) { 25157f8ada98SPavel Emelyanov seq_printf(f, "%s ", 25161da177e4SLinus Torvalds (fl->fl_type & LOCK_READ) 25171da177e4SLinus Torvalds ? (fl->fl_type & LOCK_WRITE) ? "RW " : "READ " 25181da177e4SLinus Torvalds : (fl->fl_type & LOCK_WRITE) ? "WRITE" : "NONE "); 25191da177e4SLinus Torvalds } else { 25207f8ada98SPavel Emelyanov seq_printf(f, "%s ", 2521ab83fa4bSJ. Bruce Fields (lease_breaking(fl)) 25220ee5c6d6SJeff Layton ? (fl->fl_type == F_UNLCK) ? "UNLCK" : "READ " 25230ee5c6d6SJeff Layton : (fl->fl_type == F_WRLCK) ? "WRITE" : "READ "); 25241da177e4SLinus Torvalds } 25251da177e4SLinus Torvalds if (inode) { 25261da177e4SLinus Torvalds #ifdef WE_CAN_BREAK_LSLK_NOW 2527ab1f1611SVitaliy Gusev seq_printf(f, "%d %s:%ld ", fl_pid, 25281da177e4SLinus Torvalds inode->i_sb->s_id, inode->i_ino); 25291da177e4SLinus Torvalds #else 25301da177e4SLinus Torvalds /* userspace relies on this representation of dev_t ;-( */ 2531ab1f1611SVitaliy Gusev seq_printf(f, "%d %02x:%02x:%ld ", fl_pid, 25321da177e4SLinus Torvalds MAJOR(inode->i_sb->s_dev), 25331da177e4SLinus Torvalds MINOR(inode->i_sb->s_dev), inode->i_ino); 25341da177e4SLinus Torvalds #endif 25351da177e4SLinus Torvalds } else { 2536ab1f1611SVitaliy Gusev seq_printf(f, "%d <none>:0 ", fl_pid); 25371da177e4SLinus Torvalds } 25381da177e4SLinus Torvalds if (IS_POSIX(fl)) { 25391da177e4SLinus Torvalds if (fl->fl_end == OFFSET_MAX) 25407f8ada98SPavel Emelyanov seq_printf(f, "%Ld EOF\n", fl->fl_start); 25411da177e4SLinus Torvalds else 25427f8ada98SPavel Emelyanov seq_printf(f, "%Ld %Ld\n", fl->fl_start, fl->fl_end); 25431da177e4SLinus Torvalds } else { 25445315c26aSFabian Frederick seq_puts(f, "0 EOF\n"); 25451da177e4SLinus Torvalds } 25461da177e4SLinus Torvalds } 25471da177e4SLinus Torvalds 25487f8ada98SPavel Emelyanov static int locks_show(struct seq_file *f, void *v) 25491da177e4SLinus Torvalds { 25507012b02aSJeff Layton struct locks_iterator *iter = f->private; 25517f8ada98SPavel Emelyanov struct file_lock *fl, *bfl; 25527f8ada98SPavel Emelyanov 2553139ca04eSJeff Layton fl = hlist_entry(v, struct file_lock, fl_link); 25547f8ada98SPavel Emelyanov 25557012b02aSJeff Layton lock_get_status(f, fl, iter->li_pos, ""); 25567f8ada98SPavel Emelyanov 25577f8ada98SPavel Emelyanov list_for_each_entry(bfl, &fl->fl_block, fl_block) 25587012b02aSJeff Layton lock_get_status(f, bfl, iter->li_pos, " ->"); 25597f8ada98SPavel Emelyanov 25607f8ada98SPavel Emelyanov return 0; 25611da177e4SLinus Torvalds } 25621da177e4SLinus Torvalds 25637f8ada98SPavel Emelyanov static void *locks_start(struct seq_file *f, loff_t *pos) 2564b03dfdecSJeff Layton __acquires(&blocked_lock_lock) 25651da177e4SLinus Torvalds { 25667012b02aSJeff Layton struct locks_iterator *iter = f->private; 256799dc8292SJerome Marchand 25687012b02aSJeff Layton iter->li_pos = *pos + 1; 25697012b02aSJeff Layton lg_global_lock(&file_lock_lglock); 25707b2296afSJeff Layton spin_lock(&blocked_lock_lock); 25717012b02aSJeff Layton return seq_hlist_start_percpu(&file_lock_list, &iter->li_cpu, *pos); 25721da177e4SLinus Torvalds } 25737f8ada98SPavel Emelyanov 25747f8ada98SPavel Emelyanov static void *locks_next(struct seq_file *f, void *v, loff_t *pos) 25757f8ada98SPavel Emelyanov { 25767012b02aSJeff Layton struct locks_iterator *iter = f->private; 25777012b02aSJeff Layton 25787012b02aSJeff Layton ++iter->li_pos; 25797012b02aSJeff Layton return seq_hlist_next_percpu(v, &file_lock_list, &iter->li_cpu, pos); 25801da177e4SLinus Torvalds } 25817f8ada98SPavel Emelyanov 25827f8ada98SPavel Emelyanov static void locks_stop(struct seq_file *f, void *v) 2583b03dfdecSJeff Layton __releases(&blocked_lock_lock) 25847f8ada98SPavel Emelyanov { 25857b2296afSJeff Layton spin_unlock(&blocked_lock_lock); 25867012b02aSJeff Layton lg_global_unlock(&file_lock_lglock); 25871da177e4SLinus Torvalds } 25881da177e4SLinus Torvalds 2589d8ba7a36SAlexey Dobriyan static const struct seq_operations locks_seq_operations = { 25907f8ada98SPavel Emelyanov .start = locks_start, 25917f8ada98SPavel Emelyanov .next = locks_next, 25927f8ada98SPavel Emelyanov .stop = locks_stop, 25937f8ada98SPavel Emelyanov .show = locks_show, 25947f8ada98SPavel Emelyanov }; 2595d8ba7a36SAlexey Dobriyan 2596d8ba7a36SAlexey Dobriyan static int locks_open(struct inode *inode, struct file *filp) 2597d8ba7a36SAlexey Dobriyan { 25987012b02aSJeff Layton return seq_open_private(filp, &locks_seq_operations, 25997012b02aSJeff Layton sizeof(struct locks_iterator)); 2600d8ba7a36SAlexey Dobriyan } 2601d8ba7a36SAlexey Dobriyan 2602d8ba7a36SAlexey Dobriyan static const struct file_operations proc_locks_operations = { 2603d8ba7a36SAlexey Dobriyan .open = locks_open, 2604d8ba7a36SAlexey Dobriyan .read = seq_read, 2605d8ba7a36SAlexey Dobriyan .llseek = seq_lseek, 260699dc8292SJerome Marchand .release = seq_release_private, 2607d8ba7a36SAlexey Dobriyan }; 2608d8ba7a36SAlexey Dobriyan 2609d8ba7a36SAlexey Dobriyan static int __init proc_locks_init(void) 2610d8ba7a36SAlexey Dobriyan { 2611d8ba7a36SAlexey Dobriyan proc_create("locks", 0, NULL, &proc_locks_operations); 2612d8ba7a36SAlexey Dobriyan return 0; 2613d8ba7a36SAlexey Dobriyan } 2614d8ba7a36SAlexey Dobriyan module_init(proc_locks_init); 26157f8ada98SPavel Emelyanov #endif 26167f8ada98SPavel Emelyanov 26171da177e4SLinus Torvalds static int __init filelock_init(void) 26181da177e4SLinus Torvalds { 26197012b02aSJeff Layton int i; 26207012b02aSJeff Layton 26211da177e4SLinus Torvalds filelock_cache = kmem_cache_create("file_lock_cache", 2622ee19cc40SMiklos Szeredi sizeof(struct file_lock), 0, SLAB_PANIC, NULL); 2623ee19cc40SMiklos Szeredi 26247012b02aSJeff Layton lg_lock_init(&file_lock_lglock, "file_lock_lglock"); 26257012b02aSJeff Layton 26267012b02aSJeff Layton for_each_possible_cpu(i) 26277012b02aSJeff Layton INIT_HLIST_HEAD(per_cpu_ptr(&file_lock_list, i)); 26287012b02aSJeff Layton 26291da177e4SLinus Torvalds return 0; 26301da177e4SLinus Torvalds } 26311da177e4SLinus Torvalds 26321da177e4SLinus Torvalds core_initcall(filelock_init); 2633