xref: /linux/fs/locks.c (revision 617588d5186c887eb94321b021bb5a46f896f4b3)
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 
1331da177e4SLinus Torvalds #include <asm/uaccess.h>
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds #define IS_POSIX(fl)	(fl->fl_flags & FL_POSIX)
1361da177e4SLinus Torvalds #define IS_FLOCK(fl)	(fl->fl_flags & FL_FLOCK)
137*617588d5SJ. Bruce Fields #define IS_LEASE(fl)	(fl->fl_flags & (FL_LEASE|FL_DELEG))
1381da177e4SLinus Torvalds 
139ab83fa4bSJ. Bruce Fields static bool lease_breaking(struct file_lock *fl)
140ab83fa4bSJ. Bruce Fields {
141778fc546SJ. Bruce Fields 	return fl->fl_flags & (FL_UNLOCK_PENDING | FL_DOWNGRADE_PENDING);
142778fc546SJ. Bruce Fields }
143778fc546SJ. Bruce Fields 
144778fc546SJ. Bruce Fields static int target_leasetype(struct file_lock *fl)
145778fc546SJ. Bruce Fields {
146778fc546SJ. Bruce Fields 	if (fl->fl_flags & FL_UNLOCK_PENDING)
147778fc546SJ. Bruce Fields 		return F_UNLCK;
148778fc546SJ. Bruce Fields 	if (fl->fl_flags & FL_DOWNGRADE_PENDING)
149778fc546SJ. Bruce Fields 		return F_RDLCK;
150778fc546SJ. Bruce Fields 	return fl->fl_type;
151ab83fa4bSJ. Bruce Fields }
152ab83fa4bSJ. Bruce Fields 
1531da177e4SLinus Torvalds int leases_enable = 1;
1541da177e4SLinus Torvalds int lease_break_time = 45;
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds #define for_each_lock(inode, lockp) \
1571da177e4SLinus Torvalds 	for (lockp = &inode->i_flock; *lockp != NULL; lockp = &(*lockp)->fl_next)
1581da177e4SLinus Torvalds 
1591c8c601aSJeff Layton /*
1607012b02aSJeff Layton  * The global file_lock_list is only used for displaying /proc/locks, so we
1617012b02aSJeff Layton  * keep a list on each CPU, with each list protected by its own spinlock via
1627012b02aSJeff Layton  * the file_lock_lglock. Note that alterations to the list also require that
1637012b02aSJeff Layton  * the relevant i_lock is held.
1641c8c601aSJeff Layton  */
1657012b02aSJeff Layton DEFINE_STATIC_LGLOCK(file_lock_lglock);
1667012b02aSJeff Layton static DEFINE_PER_CPU(struct hlist_head, file_lock_list);
16788974691SJeff Layton 
1681c8c601aSJeff Layton /*
16948f74186SJeff Layton  * The blocked_hash is used to find POSIX lock loops for deadlock detection.
1707b2296afSJeff Layton  * It is protected by blocked_lock_lock.
17148f74186SJeff Layton  *
17248f74186SJeff Layton  * We hash locks by lockowner in order to optimize searching for the lock a
17348f74186SJeff Layton  * particular lockowner is waiting on.
17448f74186SJeff Layton  *
17548f74186SJeff Layton  * FIXME: make this value scale via some heuristic? We generally will want more
17648f74186SJeff Layton  * buckets when we have more lockowners holding locks, but that's a little
17748f74186SJeff Layton  * difficult to determine without knowing what the workload will look like.
1781c8c601aSJeff Layton  */
17948f74186SJeff Layton #define BLOCKED_HASH_BITS	7
18048f74186SJeff Layton static DEFINE_HASHTABLE(blocked_hash, BLOCKED_HASH_BITS);
18188974691SJeff Layton 
1821c8c601aSJeff Layton /*
1837b2296afSJeff Layton  * This lock protects the blocked_hash. Generally, if you're accessing it, you
1847b2296afSJeff Layton  * want to be holding this lock.
1851c8c601aSJeff Layton  *
1861c8c601aSJeff Layton  * In addition, it also protects the fl->fl_block list, and the fl->fl_next
1871c8c601aSJeff Layton  * pointer for file_lock structures that are acting as lock requests (in
1881c8c601aSJeff Layton  * contrast to those that are acting as records of acquired locks).
1891c8c601aSJeff Layton  *
1901c8c601aSJeff Layton  * Note that when we acquire this lock in order to change the above fields,
1911c8c601aSJeff Layton  * we often hold the i_lock as well. In certain cases, when reading the fields
1921c8c601aSJeff Layton  * protected by this lock, we can skip acquiring it iff we already hold the
1931c8c601aSJeff Layton  * i_lock.
1941c8c601aSJeff Layton  *
1951c8c601aSJeff Layton  * In particular, adding an entry to the fl_block list requires that you hold
1961c8c601aSJeff Layton  * both the i_lock and the blocked_lock_lock (acquired in that order). Deleting
1971c8c601aSJeff Layton  * an entry from the list however only requires the file_lock_lock.
1981c8c601aSJeff Layton  */
1997b2296afSJeff Layton static DEFINE_SPINLOCK(blocked_lock_lock);
2001da177e4SLinus Torvalds 
201e18b890bSChristoph Lameter static struct kmem_cache *filelock_cache __read_mostly;
2021da177e4SLinus Torvalds 
203ee19cc40SMiklos Szeredi static void locks_init_lock_heads(struct file_lock *fl)
204a51cb91dSMiklos Szeredi {
205139ca04eSJeff Layton 	INIT_HLIST_NODE(&fl->fl_link);
206ee19cc40SMiklos Szeredi 	INIT_LIST_HEAD(&fl->fl_block);
207ee19cc40SMiklos Szeredi 	init_waitqueue_head(&fl->fl_wait);
208a51cb91dSMiklos Szeredi }
209a51cb91dSMiklos Szeredi 
2101da177e4SLinus Torvalds /* Allocate an empty lock structure. */
211c5b1f0d9SArnd Bergmann struct file_lock *locks_alloc_lock(void)
2121da177e4SLinus Torvalds {
213ee19cc40SMiklos Szeredi 	struct file_lock *fl = kmem_cache_zalloc(filelock_cache, GFP_KERNEL);
214a51cb91dSMiklos Szeredi 
215a51cb91dSMiklos Szeredi 	if (fl)
216ee19cc40SMiklos Szeredi 		locks_init_lock_heads(fl);
217a51cb91dSMiklos Szeredi 
218a51cb91dSMiklos Szeredi 	return fl;
2191da177e4SLinus Torvalds }
220c5b1f0d9SArnd Bergmann EXPORT_SYMBOL_GPL(locks_alloc_lock);
2211da177e4SLinus Torvalds 
222a9e61e25SFelix Blyakher void locks_release_private(struct file_lock *fl)
22347831f35STrond Myklebust {
22447831f35STrond Myklebust 	if (fl->fl_ops) {
22547831f35STrond Myklebust 		if (fl->fl_ops->fl_release_private)
22647831f35STrond Myklebust 			fl->fl_ops->fl_release_private(fl);
22747831f35STrond Myklebust 		fl->fl_ops = NULL;
22847831f35STrond Myklebust 	}
22947831f35STrond Myklebust 	fl->fl_lmops = NULL;
23047831f35STrond Myklebust 
23147831f35STrond Myklebust }
232a9e61e25SFelix Blyakher EXPORT_SYMBOL_GPL(locks_release_private);
23347831f35STrond Myklebust 
2341da177e4SLinus Torvalds /* Free a lock which is not in use. */
23505fa3135SJ. Bruce Fields void locks_free_lock(struct file_lock *fl)
2361da177e4SLinus Torvalds {
2375ce29646SMiklos Szeredi 	BUG_ON(waitqueue_active(&fl->fl_wait));
2385ce29646SMiklos Szeredi 	BUG_ON(!list_empty(&fl->fl_block));
239139ca04eSJeff Layton 	BUG_ON(!hlist_unhashed(&fl->fl_link));
2401da177e4SLinus Torvalds 
24147831f35STrond Myklebust 	locks_release_private(fl);
2421da177e4SLinus Torvalds 	kmem_cache_free(filelock_cache, fl);
2431da177e4SLinus Torvalds }
24405fa3135SJ. Bruce Fields EXPORT_SYMBOL(locks_free_lock);
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds void locks_init_lock(struct file_lock *fl)
2471da177e4SLinus Torvalds {
248ee19cc40SMiklos Szeredi 	memset(fl, 0, sizeof(struct file_lock));
249ee19cc40SMiklos Szeredi 	locks_init_lock_heads(fl);
2501da177e4SLinus Torvalds }
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds EXPORT_SYMBOL(locks_init_lock);
2531da177e4SLinus Torvalds 
25447831f35STrond Myklebust static void locks_copy_private(struct file_lock *new, struct file_lock *fl)
25547831f35STrond Myklebust {
25647831f35STrond Myklebust 	if (fl->fl_ops) {
25747831f35STrond Myklebust 		if (fl->fl_ops->fl_copy_lock)
25847831f35STrond Myklebust 			fl->fl_ops->fl_copy_lock(new, fl);
25947831f35STrond Myklebust 		new->fl_ops = fl->fl_ops;
26047831f35STrond Myklebust 	}
261bb8430a2SChristoph Hellwig 	if (fl->fl_lmops)
26247831f35STrond Myklebust 		new->fl_lmops = fl->fl_lmops;
26347831f35STrond Myklebust }
26447831f35STrond Myklebust 
2651da177e4SLinus Torvalds /*
2661da177e4SLinus Torvalds  * Initialize a new lock from an existing file_lock structure.
2671da177e4SLinus Torvalds  */
2681a747ee0SJ. Bruce Fields void __locks_copy_lock(struct file_lock *new, const struct file_lock *fl)
2691da177e4SLinus Torvalds {
2701da177e4SLinus Torvalds 	new->fl_owner = fl->fl_owner;
2711da177e4SLinus Torvalds 	new->fl_pid = fl->fl_pid;
2720996905fSTrond Myklebust 	new->fl_file = NULL;
2731da177e4SLinus Torvalds 	new->fl_flags = fl->fl_flags;
2741da177e4SLinus Torvalds 	new->fl_type = fl->fl_type;
2751da177e4SLinus Torvalds 	new->fl_start = fl->fl_start;
2761da177e4SLinus Torvalds 	new->fl_end = fl->fl_end;
2770996905fSTrond Myklebust 	new->fl_ops = NULL;
2780996905fSTrond Myklebust 	new->fl_lmops = NULL;
2790996905fSTrond Myklebust }
2803dd7b71cSRoland Dreier EXPORT_SYMBOL(__locks_copy_lock);
2810996905fSTrond Myklebust 
2820996905fSTrond Myklebust void locks_copy_lock(struct file_lock *new, struct file_lock *fl)
2830996905fSTrond Myklebust {
2840996905fSTrond Myklebust 	locks_release_private(new);
2850996905fSTrond Myklebust 
2860996905fSTrond Myklebust 	__locks_copy_lock(new, fl);
2870996905fSTrond Myklebust 	new->fl_file = fl->fl_file;
2881da177e4SLinus Torvalds 	new->fl_ops = fl->fl_ops;
2891da177e4SLinus Torvalds 	new->fl_lmops = fl->fl_lmops;
29047831f35STrond Myklebust 
29147831f35STrond Myklebust 	locks_copy_private(new, fl);
2921da177e4SLinus Torvalds }
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds EXPORT_SYMBOL(locks_copy_lock);
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds static inline int flock_translate_cmd(int cmd) {
2971da177e4SLinus Torvalds 	if (cmd & LOCK_MAND)
2981da177e4SLinus Torvalds 		return cmd & (LOCK_MAND | LOCK_RW);
2991da177e4SLinus Torvalds 	switch (cmd) {
3001da177e4SLinus Torvalds 	case LOCK_SH:
3011da177e4SLinus Torvalds 		return F_RDLCK;
3021da177e4SLinus Torvalds 	case LOCK_EX:
3031da177e4SLinus Torvalds 		return F_WRLCK;
3041da177e4SLinus Torvalds 	case LOCK_UN:
3051da177e4SLinus Torvalds 		return F_UNLCK;
3061da177e4SLinus Torvalds 	}
3071da177e4SLinus Torvalds 	return -EINVAL;
3081da177e4SLinus Torvalds }
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds /* Fill in a file_lock structure with an appropriate FLOCK lock. */
3111da177e4SLinus Torvalds static int flock_make_lock(struct file *filp, struct file_lock **lock,
3121da177e4SLinus Torvalds 		unsigned int cmd)
3131da177e4SLinus Torvalds {
3141da177e4SLinus Torvalds 	struct file_lock *fl;
3151da177e4SLinus Torvalds 	int type = flock_translate_cmd(cmd);
3161da177e4SLinus Torvalds 	if (type < 0)
3171da177e4SLinus Torvalds 		return type;
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds 	fl = locks_alloc_lock();
3201da177e4SLinus Torvalds 	if (fl == NULL)
3211da177e4SLinus Torvalds 		return -ENOMEM;
3221da177e4SLinus Torvalds 
3231da177e4SLinus Torvalds 	fl->fl_file = filp;
3241da177e4SLinus Torvalds 	fl->fl_pid = current->tgid;
3251da177e4SLinus Torvalds 	fl->fl_flags = FL_FLOCK;
3261da177e4SLinus Torvalds 	fl->fl_type = type;
3271da177e4SLinus Torvalds 	fl->fl_end = OFFSET_MAX;
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds 	*lock = fl;
3301da177e4SLinus Torvalds 	return 0;
3311da177e4SLinus Torvalds }
3321da177e4SLinus Torvalds 
3330ec4f431SJ. Bruce Fields static int assign_type(struct file_lock *fl, long type)
3341da177e4SLinus Torvalds {
3351da177e4SLinus Torvalds 	switch (type) {
3361da177e4SLinus Torvalds 	case F_RDLCK:
3371da177e4SLinus Torvalds 	case F_WRLCK:
3381da177e4SLinus Torvalds 	case F_UNLCK:
3391da177e4SLinus Torvalds 		fl->fl_type = type;
3401da177e4SLinus Torvalds 		break;
3411da177e4SLinus Torvalds 	default:
3421da177e4SLinus Torvalds 		return -EINVAL;
3431da177e4SLinus Torvalds 	}
3441da177e4SLinus Torvalds 	return 0;
3451da177e4SLinus Torvalds }
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds /* Verify a "struct flock" and copy it to a "struct file_lock" as a POSIX
3481da177e4SLinus Torvalds  * style lock.
3491da177e4SLinus Torvalds  */
3501da177e4SLinus Torvalds static int flock_to_posix_lock(struct file *filp, struct file_lock *fl,
3511da177e4SLinus Torvalds 			       struct flock *l)
3521da177e4SLinus Torvalds {
3531da177e4SLinus Torvalds 	off_t start, end;
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds 	switch (l->l_whence) {
356f5579f8cSJosef 'Jeff' Sipek 	case SEEK_SET:
3571da177e4SLinus Torvalds 		start = 0;
3581da177e4SLinus Torvalds 		break;
359f5579f8cSJosef 'Jeff' Sipek 	case SEEK_CUR:
3601da177e4SLinus Torvalds 		start = filp->f_pos;
3611da177e4SLinus Torvalds 		break;
362f5579f8cSJosef 'Jeff' Sipek 	case SEEK_END:
363496ad9aaSAl Viro 		start = i_size_read(file_inode(filp));
3641da177e4SLinus Torvalds 		break;
3651da177e4SLinus Torvalds 	default:
3661da177e4SLinus Torvalds 		return -EINVAL;
3671da177e4SLinus Torvalds 	}
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds 	/* POSIX-1996 leaves the case l->l_len < 0 undefined;
3701da177e4SLinus Torvalds 	   POSIX-2001 defines it. */
3711da177e4SLinus Torvalds 	start += l->l_start;
3721da177e4SLinus Torvalds 	if (start < 0)
3731da177e4SLinus Torvalds 		return -EINVAL;
3741da177e4SLinus Torvalds 	fl->fl_end = OFFSET_MAX;
3754c780a46STrond Myklebust 	if (l->l_len > 0) {
3764c780a46STrond Myklebust 		end = start + l->l_len - 1;
3774c780a46STrond Myklebust 		fl->fl_end = end;
3784c780a46STrond Myklebust 	} else if (l->l_len < 0) {
3794c780a46STrond Myklebust 		end = start - 1;
3804c780a46STrond Myklebust 		fl->fl_end = end;
3814c780a46STrond Myklebust 		start += l->l_len;
3824c780a46STrond Myklebust 		if (start < 0)
3834c780a46STrond Myklebust 			return -EINVAL;
3844c780a46STrond Myklebust 	}
3854c780a46STrond Myklebust 	fl->fl_start = start;	/* we record the absolute position */
3864c780a46STrond Myklebust 	if (fl->fl_end < fl->fl_start)
3874c780a46STrond Myklebust 		return -EOVERFLOW;
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds 	fl->fl_owner = current->files;
3901da177e4SLinus Torvalds 	fl->fl_pid = current->tgid;
3911da177e4SLinus Torvalds 	fl->fl_file = filp;
3921da177e4SLinus Torvalds 	fl->fl_flags = FL_POSIX;
3931da177e4SLinus Torvalds 	fl->fl_ops = NULL;
3941da177e4SLinus Torvalds 	fl->fl_lmops = NULL;
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds 	return assign_type(fl, l->l_type);
3971da177e4SLinus Torvalds }
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds #if BITS_PER_LONG == 32
4001da177e4SLinus Torvalds static int flock64_to_posix_lock(struct file *filp, struct file_lock *fl,
4011da177e4SLinus Torvalds 				 struct flock64 *l)
4021da177e4SLinus Torvalds {
4031da177e4SLinus Torvalds 	loff_t start;
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds 	switch (l->l_whence) {
406f5579f8cSJosef 'Jeff' Sipek 	case SEEK_SET:
4071da177e4SLinus Torvalds 		start = 0;
4081da177e4SLinus Torvalds 		break;
409f5579f8cSJosef 'Jeff' Sipek 	case SEEK_CUR:
4101da177e4SLinus Torvalds 		start = filp->f_pos;
4111da177e4SLinus Torvalds 		break;
412f5579f8cSJosef 'Jeff' Sipek 	case SEEK_END:
413496ad9aaSAl Viro 		start = i_size_read(file_inode(filp));
4141da177e4SLinus Torvalds 		break;
4151da177e4SLinus Torvalds 	default:
4161da177e4SLinus Torvalds 		return -EINVAL;
4171da177e4SLinus Torvalds 	}
4181da177e4SLinus Torvalds 
4194c780a46STrond Myklebust 	start += l->l_start;
4204c780a46STrond Myklebust 	if (start < 0)
4211da177e4SLinus Torvalds 		return -EINVAL;
4221da177e4SLinus Torvalds 	fl->fl_end = OFFSET_MAX;
4234c780a46STrond Myklebust 	if (l->l_len > 0) {
4244c780a46STrond Myklebust 		fl->fl_end = start + l->l_len - 1;
4254c780a46STrond Myklebust 	} else if (l->l_len < 0) {
4264c780a46STrond Myklebust 		fl->fl_end = start - 1;
4274c780a46STrond Myklebust 		start += l->l_len;
4284c780a46STrond Myklebust 		if (start < 0)
4294c780a46STrond Myklebust 			return -EINVAL;
4304c780a46STrond Myklebust 	}
4314c780a46STrond Myklebust 	fl->fl_start = start;	/* we record the absolute position */
4324c780a46STrond Myklebust 	if (fl->fl_end < fl->fl_start)
4334c780a46STrond Myklebust 		return -EOVERFLOW;
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds 	fl->fl_owner = current->files;
4361da177e4SLinus Torvalds 	fl->fl_pid = current->tgid;
4371da177e4SLinus Torvalds 	fl->fl_file = filp;
4381da177e4SLinus Torvalds 	fl->fl_flags = FL_POSIX;
4391da177e4SLinus Torvalds 	fl->fl_ops = NULL;
4401da177e4SLinus Torvalds 	fl->fl_lmops = NULL;
4411da177e4SLinus Torvalds 
442f32cb532SNamhyung Kim 	return assign_type(fl, l->l_type);
4431da177e4SLinus Torvalds }
4441da177e4SLinus Torvalds #endif
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds /* default lease lock manager operations */
4471da177e4SLinus Torvalds static void lease_break_callback(struct file_lock *fl)
4481da177e4SLinus Torvalds {
4491da177e4SLinus Torvalds 	kill_fasync(&fl->fl_fasync, SIGIO, POLL_MSG);
4501da177e4SLinus Torvalds }
4511da177e4SLinus Torvalds 
4527b021967SAlexey Dobriyan static const struct lock_manager_operations lease_manager_ops = {
4538fb47a4fSJ. Bruce Fields 	.lm_break = lease_break_callback,
4548fb47a4fSJ. Bruce Fields 	.lm_change = lease_modify,
4551da177e4SLinus Torvalds };
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds /*
4581da177e4SLinus Torvalds  * Initialize a lease, use the default lock manager operations
4591da177e4SLinus Torvalds  */
4600ec4f431SJ. Bruce Fields static int lease_init(struct file *filp, long type, struct file_lock *fl)
4611da177e4SLinus Torvalds  {
46275dff55aSTrond Myklebust 	if (assign_type(fl, type) != 0)
46375dff55aSTrond Myklebust 		return -EINVAL;
46475dff55aSTrond Myklebust 
4651da177e4SLinus Torvalds 	fl->fl_owner = current->files;
4661da177e4SLinus Torvalds 	fl->fl_pid = current->tgid;
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds 	fl->fl_file = filp;
4691da177e4SLinus Torvalds 	fl->fl_flags = FL_LEASE;
4701da177e4SLinus Torvalds 	fl->fl_start = 0;
4711da177e4SLinus Torvalds 	fl->fl_end = OFFSET_MAX;
4721da177e4SLinus Torvalds 	fl->fl_ops = NULL;
4731da177e4SLinus Torvalds 	fl->fl_lmops = &lease_manager_ops;
4741da177e4SLinus Torvalds 	return 0;
4751da177e4SLinus Torvalds }
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds /* Allocate a file_lock initialised to this type of lease */
4780ec4f431SJ. Bruce Fields static struct file_lock *lease_alloc(struct file *filp, long type)
4791da177e4SLinus Torvalds {
4801da177e4SLinus Torvalds 	struct file_lock *fl = locks_alloc_lock();
48175dff55aSTrond Myklebust 	int error = -ENOMEM;
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds 	if (fl == NULL)
484e32b8ee2SJ. Bruce Fields 		return ERR_PTR(error);
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds 	error = lease_init(filp, type, fl);
48775dff55aSTrond Myklebust 	if (error) {
48875dff55aSTrond Myklebust 		locks_free_lock(fl);
489e32b8ee2SJ. Bruce Fields 		return ERR_PTR(error);
49075dff55aSTrond Myklebust 	}
491e32b8ee2SJ. Bruce Fields 	return fl;
4921da177e4SLinus Torvalds }
4931da177e4SLinus Torvalds 
4941da177e4SLinus Torvalds /* Check if two locks overlap each other.
4951da177e4SLinus Torvalds  */
4961da177e4SLinus Torvalds static inline int locks_overlap(struct file_lock *fl1, struct file_lock *fl2)
4971da177e4SLinus Torvalds {
4981da177e4SLinus Torvalds 	return ((fl1->fl_end >= fl2->fl_start) &&
4991da177e4SLinus Torvalds 		(fl2->fl_end >= fl1->fl_start));
5001da177e4SLinus Torvalds }
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds /*
5031da177e4SLinus Torvalds  * Check whether two locks have the same owner.
5041da177e4SLinus Torvalds  */
50533443c42SMatt Mackall static int posix_same_owner(struct file_lock *fl1, struct file_lock *fl2)
5061da177e4SLinus Torvalds {
5078fb47a4fSJ. Bruce Fields 	if (fl1->fl_lmops && fl1->fl_lmops->lm_compare_owner)
5081da177e4SLinus Torvalds 		return fl2->fl_lmops == fl1->fl_lmops &&
5098fb47a4fSJ. Bruce Fields 			fl1->fl_lmops->lm_compare_owner(fl1, fl2);
5101da177e4SLinus Torvalds 	return fl1->fl_owner == fl2->fl_owner;
5111da177e4SLinus Torvalds }
5121da177e4SLinus Torvalds 
5137012b02aSJeff Layton /* Must be called with the i_lock held! */
51488974691SJeff Layton static inline void
51588974691SJeff Layton locks_insert_global_locks(struct file_lock *fl)
51688974691SJeff Layton {
5177012b02aSJeff Layton 	lg_local_lock(&file_lock_lglock);
5187012b02aSJeff Layton 	fl->fl_link_cpu = smp_processor_id();
5197012b02aSJeff Layton 	hlist_add_head(&fl->fl_link, this_cpu_ptr(&file_lock_list));
5207012b02aSJeff Layton 	lg_local_unlock(&file_lock_lglock);
52188974691SJeff Layton }
52288974691SJeff Layton 
5237012b02aSJeff Layton /* Must be called with the i_lock held! */
52488974691SJeff Layton static inline void
52588974691SJeff Layton locks_delete_global_locks(struct file_lock *fl)
52688974691SJeff Layton {
5277012b02aSJeff Layton 	/*
5287012b02aSJeff Layton 	 * Avoid taking lock if already unhashed. This is safe since this check
5297012b02aSJeff Layton 	 * is done while holding the i_lock, and new insertions into the list
5307012b02aSJeff Layton 	 * also require that it be held.
5317012b02aSJeff Layton 	 */
5327012b02aSJeff Layton 	if (hlist_unhashed(&fl->fl_link))
5337012b02aSJeff Layton 		return;
5347012b02aSJeff Layton 	lg_local_lock_cpu(&file_lock_lglock, fl->fl_link_cpu);
535139ca04eSJeff Layton 	hlist_del_init(&fl->fl_link);
5367012b02aSJeff Layton 	lg_local_unlock_cpu(&file_lock_lglock, fl->fl_link_cpu);
53788974691SJeff Layton }
53888974691SJeff Layton 
5393999e493SJeff Layton static unsigned long
5403999e493SJeff Layton posix_owner_key(struct file_lock *fl)
5413999e493SJeff Layton {
5423999e493SJeff Layton 	if (fl->fl_lmops && fl->fl_lmops->lm_owner_key)
5433999e493SJeff Layton 		return fl->fl_lmops->lm_owner_key(fl);
5443999e493SJeff Layton 	return (unsigned long)fl->fl_owner;
5453999e493SJeff Layton }
5463999e493SJeff Layton 
54788974691SJeff Layton static inline void
54888974691SJeff Layton 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 
55388974691SJeff Layton static inline void
55488974691SJeff Layton locks_delete_global_blocked(struct file_lock *waiter)
55588974691SJeff Layton {
55648f74186SJeff Layton 	hash_del(&waiter->fl_link);
55788974691SJeff Layton }
55888974691SJeff Layton 
5591da177e4SLinus Torvalds /* Remove waiter from blocker's block list.
5601da177e4SLinus Torvalds  * When blocker ends up pointing to itself then the list is empty.
5611c8c601aSJeff Layton  *
5627b2296afSJeff Layton  * Must be called with blocked_lock_lock held.
5631da177e4SLinus Torvalds  */
56433443c42SMatt Mackall static void __locks_delete_block(struct file_lock *waiter)
5651da177e4SLinus Torvalds {
56688974691SJeff Layton 	locks_delete_global_blocked(waiter);
5671da177e4SLinus Torvalds 	list_del_init(&waiter->fl_block);
5681da177e4SLinus Torvalds 	waiter->fl_next = NULL;
5691da177e4SLinus Torvalds }
5701da177e4SLinus Torvalds 
5711a9e64a7SJeff Layton static void locks_delete_block(struct file_lock *waiter)
5721da177e4SLinus Torvalds {
5737b2296afSJeff Layton 	spin_lock(&blocked_lock_lock);
5741da177e4SLinus Torvalds 	__locks_delete_block(waiter);
5757b2296afSJeff Layton 	spin_unlock(&blocked_lock_lock);
5761da177e4SLinus Torvalds }
5771da177e4SLinus Torvalds 
5781da177e4SLinus Torvalds /* Insert waiter into blocker's block list.
5791da177e4SLinus Torvalds  * We use a circular list so that processes can be easily woken up in
5801da177e4SLinus Torvalds  * the order they blocked. The documentation doesn't require this but
5811da177e4SLinus Torvalds  * it seems like the reasonable thing to do.
5821c8c601aSJeff Layton  *
5837b2296afSJeff Layton  * Must be called with both the i_lock and blocked_lock_lock held. The fl_block
5844e8c765dSJeff Layton  * list itself is protected by the file_lock_list, but by ensuring that the
5857b2296afSJeff Layton  * i_lock is also held on insertions we can avoid taking the blocked_lock_lock
5864e8c765dSJeff Layton  * in some cases when we see that the fl_block list is empty.
5871da177e4SLinus Torvalds  */
5881c8c601aSJeff Layton static void __locks_insert_block(struct file_lock *blocker,
5891da177e4SLinus Torvalds 					struct file_lock *waiter)
5901da177e4SLinus Torvalds {
5916dc0fe8fSJ. Bruce Fields 	BUG_ON(!list_empty(&waiter->fl_block));
5921da177e4SLinus Torvalds 	waiter->fl_next = blocker;
59388974691SJeff Layton 	list_add_tail(&waiter->fl_block, &blocker->fl_block);
5941da177e4SLinus Torvalds 	if (IS_POSIX(blocker))
5951c8c601aSJeff Layton 		locks_insert_global_blocked(waiter);
5961c8c601aSJeff Layton }
5971c8c601aSJeff Layton 
5981c8c601aSJeff Layton /* Must be called with i_lock held. */
5991c8c601aSJeff Layton static void locks_insert_block(struct file_lock *blocker,
6001c8c601aSJeff Layton 					struct file_lock *waiter)
6011c8c601aSJeff Layton {
6027b2296afSJeff Layton 	spin_lock(&blocked_lock_lock);
6031c8c601aSJeff Layton 	__locks_insert_block(blocker, waiter);
6047b2296afSJeff Layton 	spin_unlock(&blocked_lock_lock);
6051da177e4SLinus Torvalds }
6061da177e4SLinus Torvalds 
6071cb36012SJeff Layton /*
6081cb36012SJeff Layton  * Wake up processes blocked waiting for blocker.
6091cb36012SJeff Layton  *
6101c8c601aSJeff Layton  * Must be called with the inode->i_lock held!
6111da177e4SLinus Torvalds  */
6121da177e4SLinus Torvalds static void locks_wake_up_blocks(struct file_lock *blocker)
6131da177e4SLinus Torvalds {
6144e8c765dSJeff Layton 	/*
6154e8c765dSJeff Layton 	 * Avoid taking global lock if list is empty. This is safe since new
6164e8c765dSJeff Layton 	 * blocked requests are only added to the list under the i_lock, and
6174e8c765dSJeff Layton 	 * the i_lock is always held here. Note that removal from the fl_block
6184e8c765dSJeff Layton 	 * list does not require the i_lock, so we must recheck list_empty()
6197b2296afSJeff Layton 	 * after acquiring the blocked_lock_lock.
6204e8c765dSJeff Layton 	 */
6214e8c765dSJeff Layton 	if (list_empty(&blocker->fl_block))
6224e8c765dSJeff Layton 		return;
6234e8c765dSJeff Layton 
6247b2296afSJeff Layton 	spin_lock(&blocked_lock_lock);
6251da177e4SLinus Torvalds 	while (!list_empty(&blocker->fl_block)) {
626f0c1cd0eSPavel Emelyanov 		struct file_lock *waiter;
627f0c1cd0eSPavel Emelyanov 
628f0c1cd0eSPavel Emelyanov 		waiter = list_first_entry(&blocker->fl_block,
6291da177e4SLinus Torvalds 				struct file_lock, fl_block);
6301da177e4SLinus Torvalds 		__locks_delete_block(waiter);
6318fb47a4fSJ. Bruce Fields 		if (waiter->fl_lmops && waiter->fl_lmops->lm_notify)
6328fb47a4fSJ. Bruce Fields 			waiter->fl_lmops->lm_notify(waiter);
6331da177e4SLinus Torvalds 		else
6341da177e4SLinus Torvalds 			wake_up(&waiter->fl_wait);
6351da177e4SLinus Torvalds 	}
6367b2296afSJeff Layton 	spin_unlock(&blocked_lock_lock);
6371da177e4SLinus Torvalds }
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds /* Insert file lock fl into an inode's lock list at the position indicated
6401da177e4SLinus Torvalds  * by pos. At the same time add the lock to the global file lock list.
6411c8c601aSJeff Layton  *
6421c8c601aSJeff Layton  * Must be called with the i_lock held!
6431da177e4SLinus Torvalds  */
6441da177e4SLinus Torvalds static void locks_insert_lock(struct file_lock **pos, struct file_lock *fl)
6451da177e4SLinus Torvalds {
646ab1f1611SVitaliy Gusev 	fl->fl_nspid = get_pid(task_tgid(current));
647ab1f1611SVitaliy Gusev 
6481da177e4SLinus Torvalds 	/* insert into file's list */
6491da177e4SLinus Torvalds 	fl->fl_next = *pos;
6501da177e4SLinus Torvalds 	*pos = fl;
65188974691SJeff Layton 
65288974691SJeff Layton 	locks_insert_global_locks(fl);
6531da177e4SLinus Torvalds }
6541da177e4SLinus Torvalds 
6551da177e4SLinus Torvalds /*
6561da177e4SLinus Torvalds  * Delete a lock and then free it.
6571da177e4SLinus Torvalds  * Wake up processes that are blocked waiting for this lock,
6581da177e4SLinus Torvalds  * notify the FS that the lock has been cleared and
6591da177e4SLinus Torvalds  * finally free the lock.
6601c8c601aSJeff Layton  *
6611c8c601aSJeff Layton  * Must be called with the i_lock held!
6621da177e4SLinus Torvalds  */
6631da177e4SLinus Torvalds static void locks_delete_lock(struct file_lock **thisfl_p)
6641da177e4SLinus Torvalds {
6651da177e4SLinus Torvalds 	struct file_lock *fl = *thisfl_p;
6661da177e4SLinus Torvalds 
66788974691SJeff Layton 	locks_delete_global_locks(fl);
66888974691SJeff Layton 
6691da177e4SLinus Torvalds 	*thisfl_p = fl->fl_next;
6701da177e4SLinus Torvalds 	fl->fl_next = NULL;
6711da177e4SLinus Torvalds 
672ab1f1611SVitaliy Gusev 	if (fl->fl_nspid) {
673ab1f1611SVitaliy Gusev 		put_pid(fl->fl_nspid);
674ab1f1611SVitaliy Gusev 		fl->fl_nspid = NULL;
675ab1f1611SVitaliy Gusev 	}
676ab1f1611SVitaliy Gusev 
6771da177e4SLinus Torvalds 	locks_wake_up_blocks(fl);
6781da177e4SLinus Torvalds 	locks_free_lock(fl);
6791da177e4SLinus Torvalds }
6801da177e4SLinus Torvalds 
6811da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. Common functionality
6821da177e4SLinus Torvalds  * checks for shared/exclusive status of overlapping locks.
6831da177e4SLinus Torvalds  */
6841da177e4SLinus Torvalds static int locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
6851da177e4SLinus Torvalds {
6861da177e4SLinus Torvalds 	if (sys_fl->fl_type == F_WRLCK)
6871da177e4SLinus Torvalds 		return 1;
6881da177e4SLinus Torvalds 	if (caller_fl->fl_type == F_WRLCK)
6891da177e4SLinus Torvalds 		return 1;
6901da177e4SLinus Torvalds 	return 0;
6911da177e4SLinus Torvalds }
6921da177e4SLinus Torvalds 
6931da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. POSIX specific
6941da177e4SLinus Torvalds  * checking before calling the locks_conflict().
6951da177e4SLinus Torvalds  */
6961da177e4SLinus Torvalds static int posix_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
6971da177e4SLinus Torvalds {
6981da177e4SLinus Torvalds 	/* POSIX locks owned by the same process do not conflict with
6991da177e4SLinus Torvalds 	 * each other.
7001da177e4SLinus Torvalds 	 */
7011da177e4SLinus Torvalds 	if (!IS_POSIX(sys_fl) || posix_same_owner(caller_fl, sys_fl))
7021da177e4SLinus Torvalds 		return (0);
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds 	/* Check whether they overlap */
7051da177e4SLinus Torvalds 	if (!locks_overlap(caller_fl, sys_fl))
7061da177e4SLinus Torvalds 		return 0;
7071da177e4SLinus Torvalds 
7081da177e4SLinus Torvalds 	return (locks_conflict(caller_fl, sys_fl));
7091da177e4SLinus Torvalds }
7101da177e4SLinus Torvalds 
7111da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. FLOCK specific
7121da177e4SLinus Torvalds  * checking before calling the locks_conflict().
7131da177e4SLinus Torvalds  */
7141da177e4SLinus Torvalds static int flock_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
7151da177e4SLinus Torvalds {
7161da177e4SLinus Torvalds 	/* FLOCK locks referring to the same filp do not conflict with
7171da177e4SLinus Torvalds 	 * each other.
7181da177e4SLinus Torvalds 	 */
7191da177e4SLinus Torvalds 	if (!IS_FLOCK(sys_fl) || (caller_fl->fl_file == sys_fl->fl_file))
7201da177e4SLinus Torvalds 		return (0);
7211da177e4SLinus Torvalds 	if ((caller_fl->fl_type & LOCK_MAND) || (sys_fl->fl_type & LOCK_MAND))
7221da177e4SLinus Torvalds 		return 0;
7231da177e4SLinus Torvalds 
7241da177e4SLinus Torvalds 	return (locks_conflict(caller_fl, sys_fl));
7251da177e4SLinus Torvalds }
7261da177e4SLinus Torvalds 
7276d34ac19SJ. Bruce Fields void
7289d6a8c5cSMarc Eshel posix_test_lock(struct file *filp, struct file_lock *fl)
7291da177e4SLinus Torvalds {
7301da177e4SLinus Torvalds 	struct file_lock *cfl;
7311c8c601aSJeff Layton 	struct inode *inode = file_inode(filp);
7321da177e4SLinus Torvalds 
7331c8c601aSJeff Layton 	spin_lock(&inode->i_lock);
734496ad9aaSAl Viro 	for (cfl = file_inode(filp)->i_flock; cfl; cfl = cfl->fl_next) {
7351da177e4SLinus Torvalds 		if (!IS_POSIX(cfl))
7361da177e4SLinus Torvalds 			continue;
737b842e240SJ. Bruce Fields 		if (posix_locks_conflict(fl, cfl))
7381da177e4SLinus Torvalds 			break;
7391da177e4SLinus Torvalds 	}
740ab1f1611SVitaliy Gusev 	if (cfl) {
7419d6a8c5cSMarc Eshel 		__locks_copy_lock(fl, cfl);
742ab1f1611SVitaliy Gusev 		if (cfl->fl_nspid)
7436c5f3e7bSPavel Emelyanov 			fl->fl_pid = pid_vnr(cfl->fl_nspid);
744ab1f1611SVitaliy Gusev 	} else
745129a84deSJ. Bruce Fields 		fl->fl_type = F_UNLCK;
7461c8c601aSJeff Layton 	spin_unlock(&inode->i_lock);
7476d34ac19SJ. Bruce Fields 	return;
7481da177e4SLinus Torvalds }
7491da177e4SLinus Torvalds EXPORT_SYMBOL(posix_test_lock);
7501da177e4SLinus Torvalds 
751b533184fSJ. Bruce Fields /*
752b533184fSJ. Bruce Fields  * Deadlock detection:
7531da177e4SLinus Torvalds  *
754b533184fSJ. Bruce Fields  * We attempt to detect deadlocks that are due purely to posix file
755b533184fSJ. Bruce Fields  * locks.
7561da177e4SLinus Torvalds  *
757b533184fSJ. Bruce Fields  * We assume that a task can be waiting for at most one lock at a time.
758b533184fSJ. Bruce Fields  * So for any acquired lock, the process holding that lock may be
759b533184fSJ. Bruce Fields  * waiting on at most one other lock.  That lock in turns may be held by
760b533184fSJ. Bruce Fields  * someone waiting for at most one other lock.  Given a requested lock
761b533184fSJ. Bruce Fields  * caller_fl which is about to wait for a conflicting lock block_fl, we
762b533184fSJ. Bruce Fields  * follow this chain of waiters to ensure we are not about to create a
763b533184fSJ. Bruce Fields  * cycle.
76497855b49SJ. Bruce Fields  *
765b533184fSJ. Bruce Fields  * Since we do this before we ever put a process to sleep on a lock, we
766b533184fSJ. Bruce Fields  * are ensured that there is never a cycle; that is what guarantees that
767b533184fSJ. Bruce Fields  * the while() loop in posix_locks_deadlock() eventually completes.
768b533184fSJ. Bruce Fields  *
769b533184fSJ. Bruce Fields  * Note: the above assumption may not be true when handling lock
770b533184fSJ. Bruce Fields  * requests from a broken NFS client. It may also fail in the presence
771b533184fSJ. Bruce Fields  * of tasks (such as posix threads) sharing the same open file table.
772b533184fSJ. Bruce Fields  *
773b533184fSJ. Bruce Fields  * To handle those cases, we just bail out after a few iterations.
7741da177e4SLinus Torvalds  */
77597855b49SJ. Bruce Fields 
77697855b49SJ. Bruce Fields #define MAX_DEADLK_ITERATIONS 10
77797855b49SJ. Bruce Fields 
778b533184fSJ. Bruce Fields /* Find a lock that the owner of the given block_fl is blocking on. */
779b533184fSJ. Bruce Fields static struct file_lock *what_owner_is_waiting_for(struct file_lock *block_fl)
780b533184fSJ. Bruce Fields {
781b533184fSJ. Bruce Fields 	struct file_lock *fl;
782b533184fSJ. Bruce Fields 
7833999e493SJeff Layton 	hash_for_each_possible(blocked_hash, fl, fl_link, posix_owner_key(block_fl)) {
784b533184fSJ. Bruce Fields 		if (posix_same_owner(fl, block_fl))
785b533184fSJ. Bruce Fields 			return fl->fl_next;
786b533184fSJ. Bruce Fields 	}
787b533184fSJ. Bruce Fields 	return NULL;
788b533184fSJ. Bruce Fields }
789b533184fSJ. Bruce Fields 
7907b2296afSJeff Layton /* Must be called with the blocked_lock_lock held! */
791b0904e14SAdrian Bunk static int posix_locks_deadlock(struct file_lock *caller_fl,
7921da177e4SLinus Torvalds 				struct file_lock *block_fl)
7931da177e4SLinus Torvalds {
79497855b49SJ. Bruce Fields 	int i = 0;
7951da177e4SLinus Torvalds 
796b533184fSJ. Bruce Fields 	while ((block_fl = what_owner_is_waiting_for(block_fl))) {
79797855b49SJ. Bruce Fields 		if (i++ > MAX_DEADLK_ITERATIONS)
79897855b49SJ. Bruce Fields 			return 0;
799b533184fSJ. Bruce Fields 		if (posix_same_owner(caller_fl, block_fl))
800b533184fSJ. Bruce Fields 			return 1;
8011da177e4SLinus Torvalds 	}
8021da177e4SLinus Torvalds 	return 0;
8031da177e4SLinus Torvalds }
8041da177e4SLinus Torvalds 
8051da177e4SLinus Torvalds /* Try to create a FLOCK lock on filp. We always insert new FLOCK locks
80602888f41SJ. Bruce Fields  * after any leases, but before any posix locks.
807f475ae95STrond Myklebust  *
808f475ae95STrond Myklebust  * Note that if called with an FL_EXISTS argument, the caller may determine
809f475ae95STrond Myklebust  * whether or not a lock was successfully freed by testing the return
810f475ae95STrond Myklebust  * value for -ENOENT.
8111da177e4SLinus Torvalds  */
812993dfa87STrond Myklebust static int flock_lock_file(struct file *filp, struct file_lock *request)
8131da177e4SLinus Torvalds {
814993dfa87STrond Myklebust 	struct file_lock *new_fl = NULL;
8151da177e4SLinus Torvalds 	struct file_lock **before;
816496ad9aaSAl Viro 	struct inode * inode = file_inode(filp);
8171da177e4SLinus Torvalds 	int error = 0;
8181da177e4SLinus Torvalds 	int found = 0;
8191da177e4SLinus Torvalds 
820b89f4321SArnd Bergmann 	if (!(request->fl_flags & FL_ACCESS) && (request->fl_type != F_UNLCK)) {
821b89f4321SArnd Bergmann 		new_fl = locks_alloc_lock();
822b89f4321SArnd Bergmann 		if (!new_fl)
823b89f4321SArnd Bergmann 			return -ENOMEM;
824b89f4321SArnd Bergmann 	}
825b89f4321SArnd Bergmann 
8261c8c601aSJeff Layton 	spin_lock(&inode->i_lock);
827f07f18ddSTrond Myklebust 	if (request->fl_flags & FL_ACCESS)
828f07f18ddSTrond Myklebust 		goto find_conflict;
82984d535adSPavel Emelyanov 
8301da177e4SLinus Torvalds 	for_each_lock(inode, before) {
8311da177e4SLinus Torvalds 		struct file_lock *fl = *before;
8321da177e4SLinus Torvalds 		if (IS_POSIX(fl))
8331da177e4SLinus Torvalds 			break;
8341da177e4SLinus Torvalds 		if (IS_LEASE(fl))
8351da177e4SLinus Torvalds 			continue;
8361da177e4SLinus Torvalds 		if (filp != fl->fl_file)
8371da177e4SLinus Torvalds 			continue;
838993dfa87STrond Myklebust 		if (request->fl_type == fl->fl_type)
8391da177e4SLinus Torvalds 			goto out;
8401da177e4SLinus Torvalds 		found = 1;
8411da177e4SLinus Torvalds 		locks_delete_lock(before);
8421da177e4SLinus Torvalds 		break;
8431da177e4SLinus Torvalds 	}
8441da177e4SLinus Torvalds 
845f475ae95STrond Myklebust 	if (request->fl_type == F_UNLCK) {
846f475ae95STrond Myklebust 		if ((request->fl_flags & FL_EXISTS) && !found)
847f475ae95STrond Myklebust 			error = -ENOENT;
848993dfa87STrond Myklebust 		goto out;
849f475ae95STrond Myklebust 	}
8501da177e4SLinus Torvalds 
8511da177e4SLinus Torvalds 	/*
8521da177e4SLinus Torvalds 	 * If a higher-priority process was blocked on the old file lock,
8531da177e4SLinus Torvalds 	 * give it the opportunity to lock the file.
8541da177e4SLinus Torvalds 	 */
855b89f4321SArnd Bergmann 	if (found) {
8561c8c601aSJeff Layton 		spin_unlock(&inode->i_lock);
857def01bc5SFrederic Weisbecker 		cond_resched();
8581c8c601aSJeff Layton 		spin_lock(&inode->i_lock);
859b89f4321SArnd Bergmann 	}
8601da177e4SLinus Torvalds 
861f07f18ddSTrond Myklebust find_conflict:
8621da177e4SLinus Torvalds 	for_each_lock(inode, before) {
8631da177e4SLinus Torvalds 		struct file_lock *fl = *before;
8641da177e4SLinus Torvalds 		if (IS_POSIX(fl))
8651da177e4SLinus Torvalds 			break;
8661da177e4SLinus Torvalds 		if (IS_LEASE(fl))
8671da177e4SLinus Torvalds 			continue;
868993dfa87STrond Myklebust 		if (!flock_locks_conflict(request, fl))
8691da177e4SLinus Torvalds 			continue;
8701da177e4SLinus Torvalds 		error = -EAGAIN;
871bde74e4bSMiklos Szeredi 		if (!(request->fl_flags & FL_SLEEP))
872bde74e4bSMiklos Szeredi 			goto out;
873bde74e4bSMiklos Szeredi 		error = FILE_LOCK_DEFERRED;
874993dfa87STrond Myklebust 		locks_insert_block(fl, request);
8751da177e4SLinus Torvalds 		goto out;
8761da177e4SLinus Torvalds 	}
877f07f18ddSTrond Myklebust 	if (request->fl_flags & FL_ACCESS)
878f07f18ddSTrond Myklebust 		goto out;
879993dfa87STrond Myklebust 	locks_copy_lock(new_fl, request);
8800e2f6db8SPavel Emelyanov 	locks_insert_lock(before, new_fl);
881993dfa87STrond Myklebust 	new_fl = NULL;
8829cedc194SKirill Korotaev 	error = 0;
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds out:
8851c8c601aSJeff Layton 	spin_unlock(&inode->i_lock);
886993dfa87STrond Myklebust 	if (new_fl)
887993dfa87STrond Myklebust 		locks_free_lock(new_fl);
8881da177e4SLinus Torvalds 	return error;
8891da177e4SLinus Torvalds }
8901da177e4SLinus Torvalds 
891150b3934SMarc Eshel static int __posix_lock_file(struct inode *inode, struct file_lock *request, struct file_lock *conflock)
8921da177e4SLinus Torvalds {
8931da177e4SLinus Torvalds 	struct file_lock *fl;
89439005d02SMiklos Szeredi 	struct file_lock *new_fl = NULL;
89539005d02SMiklos Szeredi 	struct file_lock *new_fl2 = NULL;
8961da177e4SLinus Torvalds 	struct file_lock *left = NULL;
8971da177e4SLinus Torvalds 	struct file_lock *right = NULL;
8981da177e4SLinus Torvalds 	struct file_lock **before;
899b9746ef8SJeff Layton 	int error;
900b9746ef8SJeff Layton 	bool added = false;
9011da177e4SLinus Torvalds 
9021da177e4SLinus Torvalds 	/*
9031da177e4SLinus Torvalds 	 * We may need two file_lock structures for this operation,
9041da177e4SLinus Torvalds 	 * so we get them in advance to avoid races.
90539005d02SMiklos Szeredi 	 *
90639005d02SMiklos Szeredi 	 * In some cases we can be sure, that no new locks will be needed
9071da177e4SLinus Torvalds 	 */
90839005d02SMiklos Szeredi 	if (!(request->fl_flags & FL_ACCESS) &&
90939005d02SMiklos Szeredi 	    (request->fl_type != F_UNLCK ||
91039005d02SMiklos Szeredi 	     request->fl_start != 0 || request->fl_end != OFFSET_MAX)) {
9111da177e4SLinus Torvalds 		new_fl = locks_alloc_lock();
9121da177e4SLinus Torvalds 		new_fl2 = locks_alloc_lock();
91339005d02SMiklos Szeredi 	}
9141da177e4SLinus Torvalds 
9151c8c601aSJeff Layton 	spin_lock(&inode->i_lock);
9161cb36012SJeff Layton 	/*
9171cb36012SJeff Layton 	 * New lock request. Walk all POSIX locks and look for conflicts. If
9181cb36012SJeff Layton 	 * there are any, either return error or put the request on the
91948f74186SJeff Layton 	 * blocker's list of waiters and the global blocked_hash.
9201cb36012SJeff Layton 	 */
9211da177e4SLinus Torvalds 	if (request->fl_type != F_UNLCK) {
9221da177e4SLinus Torvalds 		for_each_lock(inode, before) {
923526985b9SJ. Bruce Fields 			fl = *before;
9241da177e4SLinus Torvalds 			if (!IS_POSIX(fl))
9251da177e4SLinus Torvalds 				continue;
9261da177e4SLinus Torvalds 			if (!posix_locks_conflict(request, fl))
9271da177e4SLinus Torvalds 				continue;
9285842add2SAndy Adamson 			if (conflock)
9291a747ee0SJ. Bruce Fields 				__locks_copy_lock(conflock, fl);
9301da177e4SLinus Torvalds 			error = -EAGAIN;
9311da177e4SLinus Torvalds 			if (!(request->fl_flags & FL_SLEEP))
9321da177e4SLinus Torvalds 				goto out;
9331c8c601aSJeff Layton 			/*
9341c8c601aSJeff Layton 			 * Deadlock detection and insertion into the blocked
9351c8c601aSJeff Layton 			 * locks list must be done while holding the same lock!
9361c8c601aSJeff Layton 			 */
9371da177e4SLinus Torvalds 			error = -EDEADLK;
9387b2296afSJeff Layton 			spin_lock(&blocked_lock_lock);
9391c8c601aSJeff Layton 			if (likely(!posix_locks_deadlock(request, fl))) {
940bde74e4bSMiklos Szeredi 				error = FILE_LOCK_DEFERRED;
9411c8c601aSJeff Layton 				__locks_insert_block(fl, request);
9421c8c601aSJeff Layton 			}
9437b2296afSJeff Layton 			spin_unlock(&blocked_lock_lock);
9441da177e4SLinus Torvalds 			goto out;
9451da177e4SLinus Torvalds   		}
9461da177e4SLinus Torvalds   	}
9471da177e4SLinus Torvalds 
9481da177e4SLinus Torvalds 	/* If we're just looking for a conflict, we're done. */
9491da177e4SLinus Torvalds 	error = 0;
9501da177e4SLinus Torvalds 	if (request->fl_flags & FL_ACCESS)
9511da177e4SLinus Torvalds 		goto out;
9521da177e4SLinus Torvalds 
9531da177e4SLinus Torvalds 	/*
9541da177e4SLinus Torvalds 	 * Find the first old lock with the same owner as the new lock.
9551da177e4SLinus Torvalds 	 */
9561da177e4SLinus Torvalds 
9571da177e4SLinus Torvalds 	before = &inode->i_flock;
9581da177e4SLinus Torvalds 
9591da177e4SLinus Torvalds 	/* First skip locks owned by other processes.  */
9601da177e4SLinus Torvalds 	while ((fl = *before) && (!IS_POSIX(fl) ||
9611da177e4SLinus Torvalds 				  !posix_same_owner(request, fl))) {
9621da177e4SLinus Torvalds 		before = &fl->fl_next;
9631da177e4SLinus Torvalds 	}
9641da177e4SLinus Torvalds 
9651da177e4SLinus Torvalds 	/* Process locks with this owner. */
9661da177e4SLinus Torvalds 	while ((fl = *before) && posix_same_owner(request, fl)) {
9671da177e4SLinus Torvalds 		/* Detect adjacent or overlapping regions (if same lock type)
9681da177e4SLinus Torvalds 		 */
9691da177e4SLinus Torvalds 		if (request->fl_type == fl->fl_type) {
970449231d6SOlaf Kirch 			/* In all comparisons of start vs end, use
971449231d6SOlaf Kirch 			 * "start - 1" rather than "end + 1". If end
972449231d6SOlaf Kirch 			 * is OFFSET_MAX, end + 1 will become negative.
973449231d6SOlaf Kirch 			 */
9741da177e4SLinus Torvalds 			if (fl->fl_end < request->fl_start - 1)
9751da177e4SLinus Torvalds 				goto next_lock;
9761da177e4SLinus Torvalds 			/* If the next lock in the list has entirely bigger
9771da177e4SLinus Torvalds 			 * addresses than the new one, insert the lock here.
9781da177e4SLinus Torvalds 			 */
979449231d6SOlaf Kirch 			if (fl->fl_start - 1 > request->fl_end)
9801da177e4SLinus Torvalds 				break;
9811da177e4SLinus Torvalds 
9821da177e4SLinus Torvalds 			/* If we come here, the new and old lock are of the
9831da177e4SLinus Torvalds 			 * same type and adjacent or overlapping. Make one
9841da177e4SLinus Torvalds 			 * lock yielding from the lower start address of both
9851da177e4SLinus Torvalds 			 * locks to the higher end address.
9861da177e4SLinus Torvalds 			 */
9871da177e4SLinus Torvalds 			if (fl->fl_start > request->fl_start)
9881da177e4SLinus Torvalds 				fl->fl_start = request->fl_start;
9891da177e4SLinus Torvalds 			else
9901da177e4SLinus Torvalds 				request->fl_start = fl->fl_start;
9911da177e4SLinus Torvalds 			if (fl->fl_end < request->fl_end)
9921da177e4SLinus Torvalds 				fl->fl_end = request->fl_end;
9931da177e4SLinus Torvalds 			else
9941da177e4SLinus Torvalds 				request->fl_end = fl->fl_end;
9951da177e4SLinus Torvalds 			if (added) {
9961da177e4SLinus Torvalds 				locks_delete_lock(before);
9971da177e4SLinus Torvalds 				continue;
9981da177e4SLinus Torvalds 			}
9991da177e4SLinus Torvalds 			request = fl;
1000b9746ef8SJeff Layton 			added = true;
10011da177e4SLinus Torvalds 		}
10021da177e4SLinus Torvalds 		else {
10031da177e4SLinus Torvalds 			/* Processing for different lock types is a bit
10041da177e4SLinus Torvalds 			 * more complex.
10051da177e4SLinus Torvalds 			 */
10061da177e4SLinus Torvalds 			if (fl->fl_end < request->fl_start)
10071da177e4SLinus Torvalds 				goto next_lock;
10081da177e4SLinus Torvalds 			if (fl->fl_start > request->fl_end)
10091da177e4SLinus Torvalds 				break;
10101da177e4SLinus Torvalds 			if (request->fl_type == F_UNLCK)
1011b9746ef8SJeff Layton 				added = true;
10121da177e4SLinus Torvalds 			if (fl->fl_start < request->fl_start)
10131da177e4SLinus Torvalds 				left = fl;
10141da177e4SLinus Torvalds 			/* If the next lock in the list has a higher end
10151da177e4SLinus Torvalds 			 * address than the new one, insert the new one here.
10161da177e4SLinus Torvalds 			 */
10171da177e4SLinus Torvalds 			if (fl->fl_end > request->fl_end) {
10181da177e4SLinus Torvalds 				right = fl;
10191da177e4SLinus Torvalds 				break;
10201da177e4SLinus Torvalds 			}
10211da177e4SLinus Torvalds 			if (fl->fl_start >= request->fl_start) {
10221da177e4SLinus Torvalds 				/* The new lock completely replaces an old
10231da177e4SLinus Torvalds 				 * one (This may happen several times).
10241da177e4SLinus Torvalds 				 */
10251da177e4SLinus Torvalds 				if (added) {
10261da177e4SLinus Torvalds 					locks_delete_lock(before);
10271da177e4SLinus Torvalds 					continue;
10281da177e4SLinus Torvalds 				}
10291da177e4SLinus Torvalds 				/* Replace the old lock with the new one.
10301da177e4SLinus Torvalds 				 * Wake up anybody waiting for the old one,
10311da177e4SLinus Torvalds 				 * as the change in lock type might satisfy
10321da177e4SLinus Torvalds 				 * their needs.
10331da177e4SLinus Torvalds 				 */
10341da177e4SLinus Torvalds 				locks_wake_up_blocks(fl);
10351da177e4SLinus Torvalds 				fl->fl_start = request->fl_start;
10361da177e4SLinus Torvalds 				fl->fl_end = request->fl_end;
10371da177e4SLinus Torvalds 				fl->fl_type = request->fl_type;
103847831f35STrond Myklebust 				locks_release_private(fl);
103947831f35STrond Myklebust 				locks_copy_private(fl, request);
10401da177e4SLinus Torvalds 				request = fl;
1041b9746ef8SJeff Layton 				added = true;
10421da177e4SLinus Torvalds 			}
10431da177e4SLinus Torvalds 		}
10441da177e4SLinus Torvalds 		/* Go on to next lock.
10451da177e4SLinus Torvalds 		 */
10461da177e4SLinus Torvalds 	next_lock:
10471da177e4SLinus Torvalds 		before = &fl->fl_next;
10481da177e4SLinus Torvalds 	}
10491da177e4SLinus Torvalds 
10500d9a490aSMiklos Szeredi 	/*
10511cb36012SJeff Layton 	 * The above code only modifies existing locks in case of merging or
10521cb36012SJeff Layton 	 * replacing. If new lock(s) need to be inserted all modifications are
10531cb36012SJeff Layton 	 * done below this, so it's safe yet to bail out.
10540d9a490aSMiklos Szeredi 	 */
10550d9a490aSMiklos Szeredi 	error = -ENOLCK; /* "no luck" */
10560d9a490aSMiklos Szeredi 	if (right && left == right && !new_fl2)
10570d9a490aSMiklos Szeredi 		goto out;
10580d9a490aSMiklos Szeredi 
10591da177e4SLinus Torvalds 	error = 0;
10601da177e4SLinus Torvalds 	if (!added) {
1061f475ae95STrond Myklebust 		if (request->fl_type == F_UNLCK) {
1062f475ae95STrond Myklebust 			if (request->fl_flags & FL_EXISTS)
1063f475ae95STrond Myklebust 				error = -ENOENT;
10641da177e4SLinus Torvalds 			goto out;
1065f475ae95STrond Myklebust 		}
10660d9a490aSMiklos Szeredi 
10670d9a490aSMiklos Szeredi 		if (!new_fl) {
10680d9a490aSMiklos Szeredi 			error = -ENOLCK;
10690d9a490aSMiklos Szeredi 			goto out;
10700d9a490aSMiklos Szeredi 		}
10711da177e4SLinus Torvalds 		locks_copy_lock(new_fl, request);
10721da177e4SLinus Torvalds 		locks_insert_lock(before, new_fl);
10731da177e4SLinus Torvalds 		new_fl = NULL;
10741da177e4SLinus Torvalds 	}
10751da177e4SLinus Torvalds 	if (right) {
10761da177e4SLinus Torvalds 		if (left == right) {
10771da177e4SLinus Torvalds 			/* The new lock breaks the old one in two pieces,
10781da177e4SLinus Torvalds 			 * so we have to use the second new lock.
10791da177e4SLinus Torvalds 			 */
10801da177e4SLinus Torvalds 			left = new_fl2;
10811da177e4SLinus Torvalds 			new_fl2 = NULL;
10821da177e4SLinus Torvalds 			locks_copy_lock(left, right);
10831da177e4SLinus Torvalds 			locks_insert_lock(before, left);
10841da177e4SLinus Torvalds 		}
10851da177e4SLinus Torvalds 		right->fl_start = request->fl_end + 1;
10861da177e4SLinus Torvalds 		locks_wake_up_blocks(right);
10871da177e4SLinus Torvalds 	}
10881da177e4SLinus Torvalds 	if (left) {
10891da177e4SLinus Torvalds 		left->fl_end = request->fl_start - 1;
10901da177e4SLinus Torvalds 		locks_wake_up_blocks(left);
10911da177e4SLinus Torvalds 	}
10921da177e4SLinus Torvalds  out:
10931c8c601aSJeff Layton 	spin_unlock(&inode->i_lock);
10941da177e4SLinus Torvalds 	/*
10951da177e4SLinus Torvalds 	 * Free any unused locks.
10961da177e4SLinus Torvalds 	 */
10971da177e4SLinus Torvalds 	if (new_fl)
10981da177e4SLinus Torvalds 		locks_free_lock(new_fl);
10991da177e4SLinus Torvalds 	if (new_fl2)
11001da177e4SLinus Torvalds 		locks_free_lock(new_fl2);
11011da177e4SLinus Torvalds 	return error;
11021da177e4SLinus Torvalds }
11031da177e4SLinus Torvalds 
11041da177e4SLinus Torvalds /**
11051da177e4SLinus Torvalds  * posix_lock_file - Apply a POSIX-style lock to a file
11061da177e4SLinus Torvalds  * @filp: The file to apply the lock to
11071da177e4SLinus Torvalds  * @fl: The lock to be applied
1108150b3934SMarc Eshel  * @conflock: Place to return a copy of the conflicting lock, if found.
11091da177e4SLinus Torvalds  *
11101da177e4SLinus Torvalds  * Add a POSIX style lock to a file.
11111da177e4SLinus Torvalds  * We merge adjacent & overlapping locks whenever possible.
11121da177e4SLinus Torvalds  * POSIX locks are sorted by owner task, then by starting address
1113f475ae95STrond Myklebust  *
1114f475ae95STrond Myklebust  * Note that if called with an FL_EXISTS argument, the caller may determine
1115f475ae95STrond Myklebust  * whether or not a lock was successfully freed by testing the return
1116f475ae95STrond Myklebust  * value for -ENOENT.
11171da177e4SLinus Torvalds  */
1118150b3934SMarc Eshel int posix_lock_file(struct file *filp, struct file_lock *fl,
11195842add2SAndy Adamson 			struct file_lock *conflock)
11205842add2SAndy Adamson {
1121496ad9aaSAl Viro 	return __posix_lock_file(file_inode(filp), fl, conflock);
11225842add2SAndy Adamson }
1123150b3934SMarc Eshel EXPORT_SYMBOL(posix_lock_file);
11241da177e4SLinus Torvalds 
11251da177e4SLinus Torvalds /**
11261da177e4SLinus Torvalds  * posix_lock_file_wait - Apply a POSIX-style lock to a file
11271da177e4SLinus Torvalds  * @filp: The file to apply the lock to
11281da177e4SLinus Torvalds  * @fl: The lock to be applied
11291da177e4SLinus Torvalds  *
11301da177e4SLinus Torvalds  * Add a POSIX style lock to a file.
11311da177e4SLinus Torvalds  * We merge adjacent & overlapping locks whenever possible.
11321da177e4SLinus Torvalds  * POSIX locks are sorted by owner task, then by starting address
11331da177e4SLinus Torvalds  */
11341da177e4SLinus Torvalds int posix_lock_file_wait(struct file *filp, struct file_lock *fl)
11351da177e4SLinus Torvalds {
11361da177e4SLinus Torvalds 	int error;
11371da177e4SLinus Torvalds 	might_sleep ();
11381da177e4SLinus Torvalds 	for (;;) {
1139150b3934SMarc Eshel 		error = posix_lock_file(filp, fl, NULL);
1140bde74e4bSMiklos Szeredi 		if (error != FILE_LOCK_DEFERRED)
11411da177e4SLinus Torvalds 			break;
11421da177e4SLinus Torvalds 		error = wait_event_interruptible(fl->fl_wait, !fl->fl_next);
11431da177e4SLinus Torvalds 		if (!error)
11441da177e4SLinus Torvalds 			continue;
11451da177e4SLinus Torvalds 
11461da177e4SLinus Torvalds 		locks_delete_block(fl);
11471da177e4SLinus Torvalds 		break;
11481da177e4SLinus Torvalds 	}
11491da177e4SLinus Torvalds 	return error;
11501da177e4SLinus Torvalds }
11511da177e4SLinus Torvalds EXPORT_SYMBOL(posix_lock_file_wait);
11521da177e4SLinus Torvalds 
11531da177e4SLinus Torvalds /**
11541da177e4SLinus Torvalds  * locks_mandatory_locked - Check for an active lock
11551da177e4SLinus Torvalds  * @inode: the file to check
11561da177e4SLinus Torvalds  *
11571da177e4SLinus Torvalds  * Searches the inode's list of locks to find any POSIX locks which conflict.
11581da177e4SLinus Torvalds  * This function is called from locks_verify_locked() only.
11591da177e4SLinus Torvalds  */
11601da177e4SLinus Torvalds int locks_mandatory_locked(struct inode *inode)
11611da177e4SLinus Torvalds {
11621da177e4SLinus Torvalds 	fl_owner_t owner = current->files;
11631da177e4SLinus Torvalds 	struct file_lock *fl;
11641da177e4SLinus Torvalds 
11651da177e4SLinus Torvalds 	/*
11661da177e4SLinus Torvalds 	 * Search the lock list for this inode for any POSIX locks.
11671da177e4SLinus Torvalds 	 */
11681c8c601aSJeff Layton 	spin_lock(&inode->i_lock);
11691da177e4SLinus Torvalds 	for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
11701da177e4SLinus Torvalds 		if (!IS_POSIX(fl))
11711da177e4SLinus Torvalds 			continue;
11721da177e4SLinus Torvalds 		if (fl->fl_owner != owner)
11731da177e4SLinus Torvalds 			break;
11741da177e4SLinus Torvalds 	}
11751c8c601aSJeff Layton 	spin_unlock(&inode->i_lock);
11761da177e4SLinus Torvalds 	return fl ? -EAGAIN : 0;
11771da177e4SLinus Torvalds }
11781da177e4SLinus Torvalds 
11791da177e4SLinus Torvalds /**
11801da177e4SLinus Torvalds  * locks_mandatory_area - Check for a conflicting lock
11811da177e4SLinus Torvalds  * @read_write: %FLOCK_VERIFY_WRITE for exclusive access, %FLOCK_VERIFY_READ
11821da177e4SLinus Torvalds  *		for shared
11831da177e4SLinus Torvalds  * @inode:      the file to check
11841da177e4SLinus Torvalds  * @filp:       how the file was opened (if it was)
11851da177e4SLinus Torvalds  * @offset:     start of area to check
11861da177e4SLinus Torvalds  * @count:      length of area to check
11871da177e4SLinus Torvalds  *
11881da177e4SLinus Torvalds  * Searches the inode's list of locks to find any POSIX locks which conflict.
11891da177e4SLinus Torvalds  * This function is called from rw_verify_area() and
11901da177e4SLinus Torvalds  * locks_verify_truncate().
11911da177e4SLinus Torvalds  */
11921da177e4SLinus Torvalds int locks_mandatory_area(int read_write, struct inode *inode,
11931da177e4SLinus Torvalds 			 struct file *filp, loff_t offset,
11941da177e4SLinus Torvalds 			 size_t count)
11951da177e4SLinus Torvalds {
11961da177e4SLinus Torvalds 	struct file_lock fl;
11971da177e4SLinus Torvalds 	int error;
11981da177e4SLinus Torvalds 
11991da177e4SLinus Torvalds 	locks_init_lock(&fl);
12001da177e4SLinus Torvalds 	fl.fl_owner = current->files;
12011da177e4SLinus Torvalds 	fl.fl_pid = current->tgid;
12021da177e4SLinus Torvalds 	fl.fl_file = filp;
12031da177e4SLinus Torvalds 	fl.fl_flags = FL_POSIX | FL_ACCESS;
12041da177e4SLinus Torvalds 	if (filp && !(filp->f_flags & O_NONBLOCK))
12051da177e4SLinus Torvalds 		fl.fl_flags |= FL_SLEEP;
12061da177e4SLinus Torvalds 	fl.fl_type = (read_write == FLOCK_VERIFY_WRITE) ? F_WRLCK : F_RDLCK;
12071da177e4SLinus Torvalds 	fl.fl_start = offset;
12081da177e4SLinus Torvalds 	fl.fl_end = offset + count - 1;
12091da177e4SLinus Torvalds 
12101da177e4SLinus Torvalds 	for (;;) {
1211150b3934SMarc Eshel 		error = __posix_lock_file(inode, &fl, NULL);
1212bde74e4bSMiklos Szeredi 		if (error != FILE_LOCK_DEFERRED)
12131da177e4SLinus Torvalds 			break;
12141da177e4SLinus Torvalds 		error = wait_event_interruptible(fl.fl_wait, !fl.fl_next);
12151da177e4SLinus Torvalds 		if (!error) {
12161da177e4SLinus Torvalds 			/*
12171da177e4SLinus Torvalds 			 * If we've been sleeping someone might have
12181da177e4SLinus Torvalds 			 * changed the permissions behind our back.
12191da177e4SLinus Torvalds 			 */
1220a16877caSPavel Emelyanov 			if (__mandatory_lock(inode))
12211da177e4SLinus Torvalds 				continue;
12221da177e4SLinus Torvalds 		}
12231da177e4SLinus Torvalds 
12241da177e4SLinus Torvalds 		locks_delete_block(&fl);
12251da177e4SLinus Torvalds 		break;
12261da177e4SLinus Torvalds 	}
12271da177e4SLinus Torvalds 
12281da177e4SLinus Torvalds 	return error;
12291da177e4SLinus Torvalds }
12301da177e4SLinus Torvalds 
12311da177e4SLinus Torvalds EXPORT_SYMBOL(locks_mandatory_area);
12321da177e4SLinus Torvalds 
1233778fc546SJ. Bruce Fields static void lease_clear_pending(struct file_lock *fl, int arg)
1234778fc546SJ. Bruce Fields {
1235778fc546SJ. Bruce Fields 	switch (arg) {
1236778fc546SJ. Bruce Fields 	case F_UNLCK:
1237778fc546SJ. Bruce Fields 		fl->fl_flags &= ~FL_UNLOCK_PENDING;
1238778fc546SJ. Bruce Fields 		/* fall through: */
1239778fc546SJ. Bruce Fields 	case F_RDLCK:
1240778fc546SJ. Bruce Fields 		fl->fl_flags &= ~FL_DOWNGRADE_PENDING;
1241778fc546SJ. Bruce Fields 	}
1242778fc546SJ. Bruce Fields }
1243778fc546SJ. Bruce Fields 
12441da177e4SLinus Torvalds /* We already had a lease on this file; just change its type */
12451da177e4SLinus Torvalds int lease_modify(struct file_lock **before, int arg)
12461da177e4SLinus Torvalds {
12471da177e4SLinus Torvalds 	struct file_lock *fl = *before;
12481da177e4SLinus Torvalds 	int error = assign_type(fl, arg);
12491da177e4SLinus Torvalds 
12501da177e4SLinus Torvalds 	if (error)
12511da177e4SLinus Torvalds 		return error;
1252778fc546SJ. Bruce Fields 	lease_clear_pending(fl, arg);
12531da177e4SLinus Torvalds 	locks_wake_up_blocks(fl);
12543b6e2723SFilipe Brandenburger 	if (arg == F_UNLCK) {
12553b6e2723SFilipe Brandenburger 		struct file *filp = fl->fl_file;
12563b6e2723SFilipe Brandenburger 
12573b6e2723SFilipe Brandenburger 		f_delown(filp);
12583b6e2723SFilipe Brandenburger 		filp->f_owner.signum = 0;
125996d6d59cSJ. Bruce Fields 		fasync_helper(0, fl->fl_file, 0, &fl->fl_fasync);
126096d6d59cSJ. Bruce Fields 		if (fl->fl_fasync != NULL) {
126196d6d59cSJ. Bruce Fields 			printk(KERN_ERR "locks_delete_lock: fasync == %p\n", fl->fl_fasync);
126296d6d59cSJ. Bruce Fields 			fl->fl_fasync = NULL;
126396d6d59cSJ. Bruce Fields 		}
12641da177e4SLinus Torvalds 		locks_delete_lock(before);
12653b6e2723SFilipe Brandenburger 	}
12661da177e4SLinus Torvalds 	return 0;
12671da177e4SLinus Torvalds }
12681da177e4SLinus Torvalds 
12691da177e4SLinus Torvalds EXPORT_SYMBOL(lease_modify);
12701da177e4SLinus Torvalds 
1271778fc546SJ. Bruce Fields static bool past_time(unsigned long then)
1272778fc546SJ. Bruce Fields {
1273778fc546SJ. Bruce Fields 	if (!then)
1274778fc546SJ. Bruce Fields 		/* 0 is a special value meaning "this never expires": */
1275778fc546SJ. Bruce Fields 		return false;
1276778fc546SJ. Bruce Fields 	return time_after(jiffies, then);
1277778fc546SJ. Bruce Fields }
1278778fc546SJ. Bruce Fields 
12791da177e4SLinus Torvalds static void time_out_leases(struct inode *inode)
12801da177e4SLinus Torvalds {
12811da177e4SLinus Torvalds 	struct file_lock **before;
12821da177e4SLinus Torvalds 	struct file_lock *fl;
12831da177e4SLinus Torvalds 
12841da177e4SLinus Torvalds 	before = &inode->i_flock;
1285ab83fa4bSJ. Bruce Fields 	while ((fl = *before) && IS_LEASE(fl) && lease_breaking(fl)) {
1286778fc546SJ. Bruce Fields 		if (past_time(fl->fl_downgrade_time))
1287778fc546SJ. Bruce Fields 			lease_modify(before, F_RDLCK);
1288778fc546SJ. Bruce Fields 		if (past_time(fl->fl_break_time))
1289778fc546SJ. Bruce Fields 			lease_modify(before, F_UNLCK);
12901da177e4SLinus Torvalds 		if (fl == *before)	/* lease_modify may have freed fl */
12911da177e4SLinus Torvalds 			before = &fl->fl_next;
12921da177e4SLinus Torvalds 	}
12931da177e4SLinus Torvalds }
12941da177e4SLinus Torvalds 
12951da177e4SLinus Torvalds /**
12961da177e4SLinus Torvalds  *	__break_lease	-	revoke all outstanding leases on file
12971da177e4SLinus Torvalds  *	@inode: the inode of the file to return
12981da177e4SLinus Torvalds  *	@mode: the open mode (read or write)
12991da177e4SLinus Torvalds  *
130087250dd2Sdavid m. richter  *	break_lease (inlined for speed) has checked there already is at least
130187250dd2Sdavid m. richter  *	some kind of lock (maybe a lease) on this file.  Leases are broken on
130287250dd2Sdavid m. richter  *	a call to open() or truncate().  This function can sleep unless you
13031da177e4SLinus Torvalds  *	specified %O_NONBLOCK to your open().
13041da177e4SLinus Torvalds  */
13051da177e4SLinus Torvalds int __break_lease(struct inode *inode, unsigned int mode)
13061da177e4SLinus Torvalds {
1307778fc546SJ. Bruce Fields 	int error = 0;
13081da177e4SLinus Torvalds 	struct file_lock *new_fl, *flock;
13091da177e4SLinus Torvalds 	struct file_lock *fl;
13101da177e4SLinus Torvalds 	unsigned long break_time;
13111da177e4SLinus Torvalds 	int i_have_this_lease = 0;
13128737c930SAl Viro 	int want_write = (mode & O_ACCMODE) != O_RDONLY;
13131da177e4SLinus Torvalds 
13148737c930SAl Viro 	new_fl = lease_alloc(NULL, want_write ? F_WRLCK : F_RDLCK);
13156d4b9e38SLinus Torvalds 	if (IS_ERR(new_fl))
13166d4b9e38SLinus Torvalds 		return PTR_ERR(new_fl);
13171da177e4SLinus Torvalds 
13181c8c601aSJeff Layton 	spin_lock(&inode->i_lock);
13191da177e4SLinus Torvalds 
13201da177e4SLinus Torvalds 	time_out_leases(inode);
13211da177e4SLinus Torvalds 
13221da177e4SLinus Torvalds 	flock = inode->i_flock;
13231da177e4SLinus Torvalds 	if ((flock == NULL) || !IS_LEASE(flock))
13241da177e4SLinus Torvalds 		goto out;
13251da177e4SLinus Torvalds 
1326778fc546SJ. Bruce Fields 	if (!locks_conflict(flock, new_fl))
1327778fc546SJ. Bruce Fields 		goto out;
1328778fc546SJ. Bruce Fields 
13291da177e4SLinus Torvalds 	for (fl = flock; fl && IS_LEASE(fl); fl = fl->fl_next)
13301da177e4SLinus Torvalds 		if (fl->fl_owner == current->files)
13311da177e4SLinus Torvalds 			i_have_this_lease = 1;
13321da177e4SLinus Torvalds 
13331da177e4SLinus Torvalds 	break_time = 0;
13341da177e4SLinus Torvalds 	if (lease_break_time > 0) {
13351da177e4SLinus Torvalds 		break_time = jiffies + lease_break_time * HZ;
13361da177e4SLinus Torvalds 		if (break_time == 0)
13371da177e4SLinus Torvalds 			break_time++;	/* so that 0 means no break time */
13381da177e4SLinus Torvalds 	}
13391da177e4SLinus Torvalds 
13401da177e4SLinus Torvalds 	for (fl = flock; fl && IS_LEASE(fl); fl = fl->fl_next) {
1341778fc546SJ. Bruce Fields 		if (want_write) {
1342778fc546SJ. Bruce Fields 			if (fl->fl_flags & FL_UNLOCK_PENDING)
1343778fc546SJ. Bruce Fields 				continue;
1344778fc546SJ. Bruce Fields 			fl->fl_flags |= FL_UNLOCK_PENDING;
13451da177e4SLinus Torvalds 			fl->fl_break_time = break_time;
1346778fc546SJ. Bruce Fields 		} else {
1347778fc546SJ. Bruce Fields 			if (lease_breaking(flock))
1348778fc546SJ. Bruce Fields 				continue;
1349778fc546SJ. Bruce Fields 			fl->fl_flags |= FL_DOWNGRADE_PENDING;
1350778fc546SJ. Bruce Fields 			fl->fl_downgrade_time = break_time;
13511da177e4SLinus Torvalds 		}
1352778fc546SJ. Bruce Fields 		fl->fl_lmops->lm_break(fl);
13531da177e4SLinus Torvalds 	}
13541da177e4SLinus Torvalds 
13551da177e4SLinus Torvalds 	if (i_have_this_lease || (mode & O_NONBLOCK)) {
13561da177e4SLinus Torvalds 		error = -EWOULDBLOCK;
13571da177e4SLinus Torvalds 		goto out;
13581da177e4SLinus Torvalds 	}
13591da177e4SLinus Torvalds 
13601da177e4SLinus Torvalds restart:
13611da177e4SLinus Torvalds 	break_time = flock->fl_break_time;
13621da177e4SLinus Torvalds 	if (break_time != 0) {
13631da177e4SLinus Torvalds 		break_time -= jiffies;
13641da177e4SLinus Torvalds 		if (break_time == 0)
13651da177e4SLinus Torvalds 			break_time++;
13661da177e4SLinus Torvalds 	}
13674321e01eSMatthew Wilcox 	locks_insert_block(flock, new_fl);
13681c8c601aSJeff Layton 	spin_unlock(&inode->i_lock);
13694321e01eSMatthew Wilcox 	error = wait_event_interruptible_timeout(new_fl->fl_wait,
13704321e01eSMatthew Wilcox 						!new_fl->fl_next, break_time);
13711c8c601aSJeff Layton 	spin_lock(&inode->i_lock);
13721c8c601aSJeff Layton 	locks_delete_block(new_fl);
13731da177e4SLinus Torvalds 	if (error >= 0) {
13741da177e4SLinus Torvalds 		if (error == 0)
13751da177e4SLinus Torvalds 			time_out_leases(inode);
1376778fc546SJ. Bruce Fields 		/*
1377778fc546SJ. Bruce Fields 		 * Wait for the next conflicting lease that has not been
1378778fc546SJ. Bruce Fields 		 * broken yet
1379778fc546SJ. Bruce Fields 		 */
13801da177e4SLinus Torvalds 		for (flock = inode->i_flock; flock && IS_LEASE(flock);
13811da177e4SLinus Torvalds 				flock = flock->fl_next) {
1382778fc546SJ. Bruce Fields 			if (locks_conflict(new_fl, flock))
13831da177e4SLinus Torvalds 				goto restart;
13841da177e4SLinus Torvalds 		}
13851da177e4SLinus Torvalds 		error = 0;
13861da177e4SLinus Torvalds 	}
13871da177e4SLinus Torvalds 
13881da177e4SLinus Torvalds out:
13891c8c601aSJeff Layton 	spin_unlock(&inode->i_lock);
13901da177e4SLinus Torvalds 	locks_free_lock(new_fl);
13911da177e4SLinus Torvalds 	return error;
13921da177e4SLinus Torvalds }
13931da177e4SLinus Torvalds 
13941da177e4SLinus Torvalds EXPORT_SYMBOL(__break_lease);
13951da177e4SLinus Torvalds 
13961da177e4SLinus Torvalds /**
1397a6b91919SRandy Dunlap  *	lease_get_mtime - get the last modified time of an inode
13981da177e4SLinus Torvalds  *	@inode: the inode
13991da177e4SLinus Torvalds  *      @time:  pointer to a timespec which will contain the last modified time
14001da177e4SLinus Torvalds  *
14011da177e4SLinus Torvalds  * This is to force NFS clients to flush their caches for files with
14021da177e4SLinus Torvalds  * exclusive leases.  The justification is that if someone has an
1403a6b91919SRandy Dunlap  * exclusive lease, then they could be modifying it.
14041da177e4SLinus Torvalds  */
14051da177e4SLinus Torvalds void lease_get_mtime(struct inode *inode, struct timespec *time)
14061da177e4SLinus Torvalds {
14071da177e4SLinus Torvalds 	struct file_lock *flock = inode->i_flock;
14080ee5c6d6SJeff Layton 	if (flock && IS_LEASE(flock) && (flock->fl_type == F_WRLCK))
14091da177e4SLinus Torvalds 		*time = current_fs_time(inode->i_sb);
14101da177e4SLinus Torvalds 	else
14111da177e4SLinus Torvalds 		*time = inode->i_mtime;
14121da177e4SLinus Torvalds }
14131da177e4SLinus Torvalds 
14141da177e4SLinus Torvalds EXPORT_SYMBOL(lease_get_mtime);
14151da177e4SLinus Torvalds 
14161da177e4SLinus Torvalds /**
14171da177e4SLinus Torvalds  *	fcntl_getlease - Enquire what lease is currently active
14181da177e4SLinus Torvalds  *	@filp: the file
14191da177e4SLinus Torvalds  *
14201da177e4SLinus Torvalds  *	The value returned by this function will be one of
14211da177e4SLinus Torvalds  *	(if no lease break is pending):
14221da177e4SLinus Torvalds  *
14231da177e4SLinus Torvalds  *	%F_RDLCK to indicate a shared lease is held.
14241da177e4SLinus Torvalds  *
14251da177e4SLinus Torvalds  *	%F_WRLCK to indicate an exclusive lease is held.
14261da177e4SLinus Torvalds  *
14271da177e4SLinus Torvalds  *	%F_UNLCK to indicate no lease is held.
14281da177e4SLinus Torvalds  *
14291da177e4SLinus Torvalds  *	(if a lease break is pending):
14301da177e4SLinus Torvalds  *
14311da177e4SLinus Torvalds  *	%F_RDLCK to indicate an exclusive lease needs to be
14321da177e4SLinus Torvalds  *		changed to a shared lease (or removed).
14331da177e4SLinus Torvalds  *
14341da177e4SLinus Torvalds  *	%F_UNLCK to indicate the lease needs to be removed.
14351da177e4SLinus Torvalds  *
14361da177e4SLinus Torvalds  *	XXX: sfr & willy disagree over whether F_INPROGRESS
14371da177e4SLinus Torvalds  *	should be returned to userspace.
14381da177e4SLinus Torvalds  */
14391da177e4SLinus Torvalds int fcntl_getlease(struct file *filp)
14401da177e4SLinus Torvalds {
14411da177e4SLinus Torvalds 	struct file_lock *fl;
14421c8c601aSJeff Layton 	struct inode *inode = file_inode(filp);
14431da177e4SLinus Torvalds 	int type = F_UNLCK;
14441da177e4SLinus Torvalds 
14451c8c601aSJeff Layton 	spin_lock(&inode->i_lock);
1446496ad9aaSAl Viro 	time_out_leases(file_inode(filp));
1447496ad9aaSAl Viro 	for (fl = file_inode(filp)->i_flock; fl && IS_LEASE(fl);
14481da177e4SLinus Torvalds 			fl = fl->fl_next) {
14491da177e4SLinus Torvalds 		if (fl->fl_file == filp) {
1450778fc546SJ. Bruce Fields 			type = target_leasetype(fl);
14511da177e4SLinus Torvalds 			break;
14521da177e4SLinus Torvalds 		}
14531da177e4SLinus Torvalds 	}
14541c8c601aSJeff Layton 	spin_unlock(&inode->i_lock);
14551da177e4SLinus Torvalds 	return type;
14561da177e4SLinus Torvalds }
14571da177e4SLinus Torvalds 
1458d4f22d19SJeff Layton static int generic_add_lease(struct file *filp, long arg, struct file_lock **flp)
14591da177e4SLinus Torvalds {
14607eaae282SKAMBAROV, ZAUR 	struct file_lock *fl, **before, **my_before = NULL, *lease;
14610f7fc9e4SJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
14621da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
1463c1f24ef4SJ. Bruce Fields 	int error;
14641da177e4SLinus Torvalds 
1465096657b6SJ. Bruce Fields 	lease = *flp;
1466096657b6SJ. Bruce Fields 
14671da177e4SLinus Torvalds 	error = -EAGAIN;
14681da177e4SLinus Torvalds 	if ((arg == F_RDLCK) && (atomic_read(&inode->i_writecount) > 0))
14691da177e4SLinus Torvalds 		goto out;
14701da177e4SLinus Torvalds 	if ((arg == F_WRLCK)
147184d08fa8SAl Viro 	    && ((d_count(dentry) > 1)
14721da177e4SLinus Torvalds 		|| (atomic_read(&inode->i_count) > 1)))
14731da177e4SLinus Torvalds 		goto out;
147485c59580SPavel Emelyanov 
14751da177e4SLinus Torvalds 	/*
14761da177e4SLinus Torvalds 	 * At this point, we know that if there is an exclusive
14771da177e4SLinus Torvalds 	 * lease on this file, then we hold it on this filp
14781da177e4SLinus Torvalds 	 * (otherwise our open of this file would have blocked).
14791da177e4SLinus Torvalds 	 * And if we are trying to acquire an exclusive lease,
14801da177e4SLinus Torvalds 	 * then the file is not open by anyone (including us)
14811da177e4SLinus Torvalds 	 * except for this filp.
14821da177e4SLinus Torvalds 	 */
1483c1f24ef4SJ. Bruce Fields 	error = -EAGAIN;
14841da177e4SLinus Torvalds 	for (before = &inode->i_flock;
14851da177e4SLinus Torvalds 			((fl = *before) != NULL) && IS_LEASE(fl);
14861da177e4SLinus Torvalds 			before = &fl->fl_next) {
1487c1f24ef4SJ. Bruce Fields 		if (fl->fl_file == filp) {
14881da177e4SLinus Torvalds 			my_before = before;
1489c1f24ef4SJ. Bruce Fields 			continue;
14901da177e4SLinus Torvalds 		}
1491c1f24ef4SJ. Bruce Fields 		/*
1492c1f24ef4SJ. Bruce Fields 		 * No exclusive leases if someone else has a lease on
1493c1f24ef4SJ. Bruce Fields 		 * this file:
1494c1f24ef4SJ. Bruce Fields 		 */
1495c1f24ef4SJ. Bruce Fields 		if (arg == F_WRLCK)
14961da177e4SLinus Torvalds 			goto out;
1497c1f24ef4SJ. Bruce Fields 		/*
1498c1f24ef4SJ. Bruce Fields 		 * Modifying our existing lease is OK, but no getting a
1499c1f24ef4SJ. Bruce Fields 		 * new lease if someone else is opening for write:
1500c1f24ef4SJ. Bruce Fields 		 */
1501c1f24ef4SJ. Bruce Fields 		if (fl->fl_flags & FL_UNLOCK_PENDING)
1502c1f24ef4SJ. Bruce Fields 			goto out;
1503c1f24ef4SJ. Bruce Fields 	}
15041da177e4SLinus Torvalds 
15051da177e4SLinus Torvalds 	if (my_before != NULL) {
15068fb47a4fSJ. Bruce Fields 		error = lease->fl_lmops->lm_change(my_before, arg);
150751ee4b84SChristoph Hellwig 		if (!error)
150851ee4b84SChristoph Hellwig 			*flp = *my_before;
15091da177e4SLinus Torvalds 		goto out;
15101da177e4SLinus Torvalds 	}
15111da177e4SLinus Torvalds 
15121da177e4SLinus Torvalds 	error = -EINVAL;
15131da177e4SLinus Torvalds 	if (!leases_enable)
15141da177e4SLinus Torvalds 		goto out;
15151da177e4SLinus Torvalds 
1516c5b1f0d9SArnd Bergmann 	locks_insert_lock(before, lease);
151785c59580SPavel Emelyanov 	return 0;
15181da177e4SLinus Torvalds 
15191da177e4SLinus Torvalds out:
15201da177e4SLinus Torvalds 	return error;
15211da177e4SLinus Torvalds }
15228335ebd9SJ. Bruce Fields 
1523d4f22d19SJeff Layton static int generic_delete_lease(struct file *filp, struct file_lock **flp)
15248335ebd9SJ. Bruce Fields {
15258335ebd9SJ. Bruce Fields 	struct file_lock *fl, **before;
15268335ebd9SJ. Bruce Fields 	struct dentry *dentry = filp->f_path.dentry;
15278335ebd9SJ. Bruce Fields 	struct inode *inode = dentry->d_inode;
15288335ebd9SJ. Bruce Fields 
15298335ebd9SJ. Bruce Fields 	for (before = &inode->i_flock;
15308335ebd9SJ. Bruce Fields 			((fl = *before) != NULL) && IS_LEASE(fl);
15318335ebd9SJ. Bruce Fields 			before = &fl->fl_next) {
15328335ebd9SJ. Bruce Fields 		if (fl->fl_file != filp)
15338335ebd9SJ. Bruce Fields 			continue;
15348335ebd9SJ. Bruce Fields 		return (*flp)->fl_lmops->lm_change(before, F_UNLCK);
15358335ebd9SJ. Bruce Fields 	}
15368335ebd9SJ. Bruce Fields 	return -EAGAIN;
15378335ebd9SJ. Bruce Fields }
15388335ebd9SJ. Bruce Fields 
15391da177e4SLinus Torvalds /**
15401da177e4SLinus Torvalds  *	generic_setlease	-	sets a lease on an open file
15411da177e4SLinus Torvalds  *	@filp: file pointer
15421da177e4SLinus Torvalds  *	@arg: type of lease to obtain
15431da177e4SLinus Torvalds  *	@flp: input - file_lock to use, output - file_lock inserted
15441da177e4SLinus Torvalds  *
15451da177e4SLinus Torvalds  *	The (input) flp->fl_lmops->lm_break function is required
15461da177e4SLinus Torvalds  *	by break_lease().
15471da177e4SLinus Torvalds  *
15481c8c601aSJeff Layton  *	Called with inode->i_lock held.
15491da177e4SLinus Torvalds  */
15501da177e4SLinus Torvalds int generic_setlease(struct file *filp, long arg, struct file_lock **flp)
15511da177e4SLinus Torvalds {
15521da177e4SLinus Torvalds 	struct dentry *dentry = filp->f_path.dentry;
15531da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
15548335ebd9SJ. Bruce Fields 	int error;
15551da177e4SLinus Torvalds 
15568e96e3b7SEric W. Biederman 	if ((!uid_eq(current_fsuid(), inode->i_uid)) && !capable(CAP_LEASE))
15578335ebd9SJ. Bruce Fields 		return -EACCES;
15581da177e4SLinus Torvalds 	if (!S_ISREG(inode->i_mode))
15598335ebd9SJ. Bruce Fields 		return -EINVAL;
15601da177e4SLinus Torvalds 	error = security_file_lock(filp, arg);
15611da177e4SLinus Torvalds 	if (error)
15628335ebd9SJ. Bruce Fields 		return error;
15631da177e4SLinus Torvalds 
15641da177e4SLinus Torvalds 	time_out_leases(inode);
15651da177e4SLinus Torvalds 
15661da177e4SLinus Torvalds 	BUG_ON(!(*flp)->fl_lmops->lm_break);
15671da177e4SLinus Torvalds 
15688335ebd9SJ. Bruce Fields 	switch (arg) {
15698335ebd9SJ. Bruce Fields 	case F_UNLCK:
15708335ebd9SJ. Bruce Fields 		return generic_delete_lease(filp, flp);
15718335ebd9SJ. Bruce Fields 	case F_RDLCK:
15728335ebd9SJ. Bruce Fields 	case F_WRLCK:
15738335ebd9SJ. Bruce Fields 		return generic_add_lease(filp, arg, flp);
15748335ebd9SJ. Bruce Fields 	default:
15758d657eb3SDave Jones 		return -EINVAL;
15761da177e4SLinus Torvalds 	}
15771da177e4SLinus Torvalds }
15780af1a450SChristoph Hellwig EXPORT_SYMBOL(generic_setlease);
15791da177e4SLinus Torvalds 
1580b89f4321SArnd Bergmann static int __vfs_setlease(struct file *filp, long arg, struct file_lock **lease)
1581b89f4321SArnd Bergmann {
158272c2d531SAl Viro 	if (filp->f_op->setlease)
1583b89f4321SArnd Bergmann 		return filp->f_op->setlease(filp, arg, lease);
1584b89f4321SArnd Bergmann 	else
1585b89f4321SArnd Bergmann 		return generic_setlease(filp, arg, lease);
1586b89f4321SArnd Bergmann }
1587b89f4321SArnd Bergmann 
15881da177e4SLinus Torvalds /**
1589a9933ceaSJ. Bruce Fields  *	vfs_setlease        -       sets a lease on an open file
15901da177e4SLinus Torvalds  *	@filp: file pointer
15911da177e4SLinus Torvalds  *	@arg: type of lease to obtain
15921da177e4SLinus Torvalds  *	@lease: file_lock to use
15931da177e4SLinus Torvalds  *
15941da177e4SLinus Torvalds  *	Call this to establish a lease on the file.
15958fb47a4fSJ. Bruce Fields  *	The (*lease)->fl_lmops->lm_break operation must be set; if not,
1596f9ffed26SJ. Bruce Fields  *	break_lease will oops!
1597f9ffed26SJ. Bruce Fields  *
1598f9ffed26SJ. Bruce Fields  *	This will call the filesystem's setlease file method, if
1599f9ffed26SJ. Bruce Fields  *	defined.  Note that there is no getlease method; instead, the
1600f9ffed26SJ. Bruce Fields  *	filesystem setlease method should call back to setlease() to
1601f9ffed26SJ. Bruce Fields  *	add a lease to the inode's lease list, where fcntl_getlease() can
1602f9ffed26SJ. Bruce Fields  *	find it.  Since fcntl_getlease() only reports whether the current
1603f9ffed26SJ. Bruce Fields  *	task holds a lease, a cluster filesystem need only do this for
1604f9ffed26SJ. Bruce Fields  *	leases held by processes on this node.
1605f9ffed26SJ. Bruce Fields  *
1606f9ffed26SJ. Bruce Fields  *	There is also no break_lease method; filesystems that
1607c9404c9cSAdam Buchbinder  *	handle their own leases should break leases themselves from the
1608f9ffed26SJ. Bruce Fields  *	filesystem's open, create, and (on truncate) setattr methods.
1609f9ffed26SJ. Bruce Fields  *
1610f9ffed26SJ. Bruce Fields  *	Warning: the only current setlease methods exist only to disable
1611f9ffed26SJ. Bruce Fields  *	leases in certain cases.  More vfs changes may be required to
1612f9ffed26SJ. Bruce Fields  *	allow a full filesystem lease implementation.
16131da177e4SLinus Torvalds  */
16141da177e4SLinus Torvalds 
1615a9933ceaSJ. Bruce Fields int vfs_setlease(struct file *filp, long arg, struct file_lock **lease)
16161da177e4SLinus Torvalds {
16171c8c601aSJeff Layton 	struct inode *inode = file_inode(filp);
16181da177e4SLinus Torvalds 	int error;
16191da177e4SLinus Torvalds 
16201c8c601aSJeff Layton 	spin_lock(&inode->i_lock);
1621b89f4321SArnd Bergmann 	error = __vfs_setlease(filp, arg, lease);
16221c8c601aSJeff Layton 	spin_unlock(&inode->i_lock);
16231da177e4SLinus Torvalds 
16241da177e4SLinus Torvalds 	return error;
16251da177e4SLinus Torvalds }
1626a9933ceaSJ. Bruce Fields EXPORT_SYMBOL_GPL(vfs_setlease);
16271da177e4SLinus Torvalds 
16280ceaf6c7SJ. Bruce Fields static int do_fcntl_delete_lease(struct file *filp)
16290ceaf6c7SJ. Bruce Fields {
16300ceaf6c7SJ. Bruce Fields 	struct file_lock fl, *flp = &fl;
16310ceaf6c7SJ. Bruce Fields 
16320ceaf6c7SJ. Bruce Fields 	lease_init(filp, F_UNLCK, flp);
16330ceaf6c7SJ. Bruce Fields 
16340ceaf6c7SJ. Bruce Fields 	return vfs_setlease(filp, F_UNLCK, &flp);
16350ceaf6c7SJ. Bruce Fields }
16360ceaf6c7SJ. Bruce Fields 
16370ceaf6c7SJ. Bruce Fields static int do_fcntl_add_lease(unsigned int fd, struct file *filp, long arg)
16381da177e4SLinus Torvalds {
16393df057acSJ. Bruce Fields 	struct file_lock *fl, *ret;
16401c8c601aSJeff Layton 	struct inode *inode = file_inode(filp);
1641f7347ce4SLinus Torvalds 	struct fasync_struct *new;
16421da177e4SLinus Torvalds 	int error;
16431da177e4SLinus Torvalds 
1644c5b1f0d9SArnd Bergmann 	fl = lease_alloc(filp, arg);
1645c5b1f0d9SArnd Bergmann 	if (IS_ERR(fl))
1646c5b1f0d9SArnd Bergmann 		return PTR_ERR(fl);
16471da177e4SLinus Torvalds 
1648f7347ce4SLinus Torvalds 	new = fasync_alloc();
1649f7347ce4SLinus Torvalds 	if (!new) {
1650f7347ce4SLinus Torvalds 		locks_free_lock(fl);
1651f7347ce4SLinus Torvalds 		return -ENOMEM;
1652f7347ce4SLinus Torvalds 	}
16533df057acSJ. Bruce Fields 	ret = fl;
16541c8c601aSJeff Layton 	spin_lock(&inode->i_lock);
16558896b93fSJ. Bruce Fields 	error = __vfs_setlease(filp, arg, &ret);
165651ee4b84SChristoph Hellwig 	if (error) {
16571c8c601aSJeff Layton 		spin_unlock(&inode->i_lock);
165851ee4b84SChristoph Hellwig 		locks_free_lock(fl);
165951ee4b84SChristoph Hellwig 		goto out_free_fasync;
166051ee4b84SChristoph Hellwig 	}
16613df057acSJ. Bruce Fields 	if (ret != fl)
16623df057acSJ. Bruce Fields 		locks_free_lock(fl);
16631da177e4SLinus Torvalds 
1664f7347ce4SLinus Torvalds 	/*
1665f7347ce4SLinus Torvalds 	 * fasync_insert_entry() returns the old entry if any.
1666f7347ce4SLinus Torvalds 	 * If there was no old entry, then it used 'new' and
1667f7347ce4SLinus Torvalds 	 * inserted it into the fasync list. Clear new so that
1668f7347ce4SLinus Torvalds 	 * we don't release it here.
1669f7347ce4SLinus Torvalds 	 */
16703df057acSJ. Bruce Fields 	if (!fasync_insert_entry(fd, filp, &ret->fl_fasync, new))
1671f7347ce4SLinus Torvalds 		new = NULL;
1672f7347ce4SLinus Torvalds 
1673609d7fa9SEric W. Biederman 	error = __f_setown(filp, task_pid(current), PIDTYPE_PID, 0);
16741c8c601aSJeff Layton 	spin_unlock(&inode->i_lock);
167551ee4b84SChristoph Hellwig 
167651ee4b84SChristoph Hellwig out_free_fasync:
1677f7347ce4SLinus Torvalds 	if (new)
1678f7347ce4SLinus Torvalds 		fasync_free(new);
16791da177e4SLinus Torvalds 	return error;
16801da177e4SLinus Torvalds }
16811da177e4SLinus Torvalds 
16821da177e4SLinus Torvalds /**
16830ceaf6c7SJ. Bruce Fields  *	fcntl_setlease	-	sets a lease on an open file
16840ceaf6c7SJ. Bruce Fields  *	@fd: open file descriptor
16850ceaf6c7SJ. Bruce Fields  *	@filp: file pointer
16860ceaf6c7SJ. Bruce Fields  *	@arg: type of lease to obtain
16870ceaf6c7SJ. Bruce Fields  *
16880ceaf6c7SJ. Bruce Fields  *	Call this fcntl to establish a lease on the file.
16890ceaf6c7SJ. Bruce Fields  *	Note that you also need to call %F_SETSIG to
16900ceaf6c7SJ. Bruce Fields  *	receive a signal when the lease is broken.
16910ceaf6c7SJ. Bruce Fields  */
16920ceaf6c7SJ. Bruce Fields int fcntl_setlease(unsigned int fd, struct file *filp, long arg)
16930ceaf6c7SJ. Bruce Fields {
16940ceaf6c7SJ. Bruce Fields 	if (arg == F_UNLCK)
16950ceaf6c7SJ. Bruce Fields 		return do_fcntl_delete_lease(filp);
16960ceaf6c7SJ. Bruce Fields 	return do_fcntl_add_lease(fd, filp, arg);
16970ceaf6c7SJ. Bruce Fields }
16980ceaf6c7SJ. Bruce Fields 
16990ceaf6c7SJ. Bruce Fields /**
17001da177e4SLinus Torvalds  * flock_lock_file_wait - Apply a FLOCK-style lock to a file
17011da177e4SLinus Torvalds  * @filp: The file to apply the lock to
17021da177e4SLinus Torvalds  * @fl: The lock to be applied
17031da177e4SLinus Torvalds  *
17041da177e4SLinus Torvalds  * Add a FLOCK style lock to a file.
17051da177e4SLinus Torvalds  */
17061da177e4SLinus Torvalds int flock_lock_file_wait(struct file *filp, struct file_lock *fl)
17071da177e4SLinus Torvalds {
17081da177e4SLinus Torvalds 	int error;
17091da177e4SLinus Torvalds 	might_sleep();
17101da177e4SLinus Torvalds 	for (;;) {
17111da177e4SLinus Torvalds 		error = flock_lock_file(filp, fl);
1712bde74e4bSMiklos Szeredi 		if (error != FILE_LOCK_DEFERRED)
17131da177e4SLinus Torvalds 			break;
17141da177e4SLinus Torvalds 		error = wait_event_interruptible(fl->fl_wait, !fl->fl_next);
17151da177e4SLinus Torvalds 		if (!error)
17161da177e4SLinus Torvalds 			continue;
17171da177e4SLinus Torvalds 
17181da177e4SLinus Torvalds 		locks_delete_block(fl);
17191da177e4SLinus Torvalds 		break;
17201da177e4SLinus Torvalds 	}
17211da177e4SLinus Torvalds 	return error;
17221da177e4SLinus Torvalds }
17231da177e4SLinus Torvalds 
17241da177e4SLinus Torvalds EXPORT_SYMBOL(flock_lock_file_wait);
17251da177e4SLinus Torvalds 
17261da177e4SLinus Torvalds /**
17271da177e4SLinus Torvalds  *	sys_flock: - flock() system call.
17281da177e4SLinus Torvalds  *	@fd: the file descriptor to lock.
17291da177e4SLinus Torvalds  *	@cmd: the type of lock to apply.
17301da177e4SLinus Torvalds  *
17311da177e4SLinus Torvalds  *	Apply a %FL_FLOCK style lock to an open file descriptor.
17321da177e4SLinus Torvalds  *	The @cmd can be one of
17331da177e4SLinus Torvalds  *
17341da177e4SLinus Torvalds  *	%LOCK_SH -- a shared lock.
17351da177e4SLinus Torvalds  *
17361da177e4SLinus Torvalds  *	%LOCK_EX -- an exclusive lock.
17371da177e4SLinus Torvalds  *
17381da177e4SLinus Torvalds  *	%LOCK_UN -- remove an existing lock.
17391da177e4SLinus Torvalds  *
17401da177e4SLinus Torvalds  *	%LOCK_MAND -- a `mandatory' flock.  This exists to emulate Windows Share Modes.
17411da177e4SLinus Torvalds  *
17421da177e4SLinus Torvalds  *	%LOCK_MAND can be combined with %LOCK_READ or %LOCK_WRITE to allow other
17431da177e4SLinus Torvalds  *	processes read and write access respectively.
17441da177e4SLinus Torvalds  */
1745002c8976SHeiko Carstens SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd)
17461da177e4SLinus Torvalds {
17472903ff01SAl Viro 	struct fd f = fdget(fd);
17481da177e4SLinus Torvalds 	struct file_lock *lock;
17491da177e4SLinus Torvalds 	int can_sleep, unlock;
17501da177e4SLinus Torvalds 	int error;
17511da177e4SLinus Torvalds 
17521da177e4SLinus Torvalds 	error = -EBADF;
17532903ff01SAl Viro 	if (!f.file)
17541da177e4SLinus Torvalds 		goto out;
17551da177e4SLinus Torvalds 
17561da177e4SLinus Torvalds 	can_sleep = !(cmd & LOCK_NB);
17571da177e4SLinus Torvalds 	cmd &= ~LOCK_NB;
17581da177e4SLinus Torvalds 	unlock = (cmd == LOCK_UN);
17591da177e4SLinus Torvalds 
1760aeb5d727SAl Viro 	if (!unlock && !(cmd & LOCK_MAND) &&
17612903ff01SAl Viro 	    !(f.file->f_mode & (FMODE_READ|FMODE_WRITE)))
17621da177e4SLinus Torvalds 		goto out_putf;
17631da177e4SLinus Torvalds 
17642903ff01SAl Viro 	error = flock_make_lock(f.file, &lock, cmd);
17651da177e4SLinus Torvalds 	if (error)
17661da177e4SLinus Torvalds 		goto out_putf;
17671da177e4SLinus Torvalds 	if (can_sleep)
17681da177e4SLinus Torvalds 		lock->fl_flags |= FL_SLEEP;
17691da177e4SLinus Torvalds 
17702903ff01SAl Viro 	error = security_file_lock(f.file, lock->fl_type);
17711da177e4SLinus Torvalds 	if (error)
17721da177e4SLinus Torvalds 		goto out_free;
17731da177e4SLinus Torvalds 
177472c2d531SAl Viro 	if (f.file->f_op->flock)
17752903ff01SAl Viro 		error = f.file->f_op->flock(f.file,
17761da177e4SLinus Torvalds 					  (can_sleep) ? F_SETLKW : F_SETLK,
17771da177e4SLinus Torvalds 					  lock);
17781da177e4SLinus Torvalds 	else
17792903ff01SAl Viro 		error = flock_lock_file_wait(f.file, lock);
17801da177e4SLinus Torvalds 
17811da177e4SLinus Torvalds  out_free:
17821da177e4SLinus Torvalds 	locks_free_lock(lock);
17831da177e4SLinus Torvalds 
17841da177e4SLinus Torvalds  out_putf:
17852903ff01SAl Viro 	fdput(f);
17861da177e4SLinus Torvalds  out:
17871da177e4SLinus Torvalds 	return error;
17881da177e4SLinus Torvalds }
17891da177e4SLinus Torvalds 
17903ee17abdSJ. Bruce Fields /**
17913ee17abdSJ. Bruce Fields  * vfs_test_lock - test file byte range lock
17923ee17abdSJ. Bruce Fields  * @filp: The file to test lock for
17936924c554SJ. Bruce Fields  * @fl: The lock to test; also used to hold result
17943ee17abdSJ. Bruce Fields  *
17953ee17abdSJ. Bruce Fields  * Returns -ERRNO on failure.  Indicates presence of conflicting lock by
17963ee17abdSJ. Bruce Fields  * setting conf->fl_type to something other than F_UNLCK.
17973ee17abdSJ. Bruce Fields  */
17983ee17abdSJ. Bruce Fields int vfs_test_lock(struct file *filp, struct file_lock *fl)
17993ee17abdSJ. Bruce Fields {
180072c2d531SAl Viro 	if (filp->f_op->lock)
18013ee17abdSJ. Bruce Fields 		return filp->f_op->lock(filp, F_GETLK, fl);
18023ee17abdSJ. Bruce Fields 	posix_test_lock(filp, fl);
18033ee17abdSJ. Bruce Fields 	return 0;
18043ee17abdSJ. Bruce Fields }
18053ee17abdSJ. Bruce Fields EXPORT_SYMBOL_GPL(vfs_test_lock);
18063ee17abdSJ. Bruce Fields 
1807c2fa1b8aSJ. Bruce Fields static int posix_lock_to_flock(struct flock *flock, struct file_lock *fl)
1808c2fa1b8aSJ. Bruce Fields {
1809c2fa1b8aSJ. Bruce Fields 	flock->l_pid = fl->fl_pid;
1810c2fa1b8aSJ. Bruce Fields #if BITS_PER_LONG == 32
1811c2fa1b8aSJ. Bruce Fields 	/*
1812c2fa1b8aSJ. Bruce Fields 	 * Make sure we can represent the posix lock via
1813c2fa1b8aSJ. Bruce Fields 	 * legacy 32bit flock.
1814c2fa1b8aSJ. Bruce Fields 	 */
1815c2fa1b8aSJ. Bruce Fields 	if (fl->fl_start > OFFT_OFFSET_MAX)
1816c2fa1b8aSJ. Bruce Fields 		return -EOVERFLOW;
1817c2fa1b8aSJ. Bruce Fields 	if (fl->fl_end != OFFSET_MAX && fl->fl_end > OFFT_OFFSET_MAX)
1818c2fa1b8aSJ. Bruce Fields 		return -EOVERFLOW;
1819c2fa1b8aSJ. Bruce Fields #endif
1820c2fa1b8aSJ. Bruce Fields 	flock->l_start = fl->fl_start;
1821c2fa1b8aSJ. Bruce Fields 	flock->l_len = fl->fl_end == OFFSET_MAX ? 0 :
1822c2fa1b8aSJ. Bruce Fields 		fl->fl_end - fl->fl_start + 1;
1823c2fa1b8aSJ. Bruce Fields 	flock->l_whence = 0;
1824129a84deSJ. Bruce Fields 	flock->l_type = fl->fl_type;
1825c2fa1b8aSJ. Bruce Fields 	return 0;
1826c2fa1b8aSJ. Bruce Fields }
1827c2fa1b8aSJ. Bruce Fields 
1828c2fa1b8aSJ. Bruce Fields #if BITS_PER_LONG == 32
1829c2fa1b8aSJ. Bruce Fields static void posix_lock_to_flock64(struct flock64 *flock, struct file_lock *fl)
1830c2fa1b8aSJ. Bruce Fields {
1831c2fa1b8aSJ. Bruce Fields 	flock->l_pid = fl->fl_pid;
1832c2fa1b8aSJ. Bruce Fields 	flock->l_start = fl->fl_start;
1833c2fa1b8aSJ. Bruce Fields 	flock->l_len = fl->fl_end == OFFSET_MAX ? 0 :
1834c2fa1b8aSJ. Bruce Fields 		fl->fl_end - fl->fl_start + 1;
1835c2fa1b8aSJ. Bruce Fields 	flock->l_whence = 0;
1836c2fa1b8aSJ. Bruce Fields 	flock->l_type = fl->fl_type;
1837c2fa1b8aSJ. Bruce Fields }
1838c2fa1b8aSJ. Bruce Fields #endif
1839c2fa1b8aSJ. Bruce Fields 
18401da177e4SLinus Torvalds /* Report the first existing lock that would conflict with l.
18411da177e4SLinus Torvalds  * This implements the F_GETLK command of fcntl().
18421da177e4SLinus Torvalds  */
18431da177e4SLinus Torvalds int fcntl_getlk(struct file *filp, struct flock __user *l)
18441da177e4SLinus Torvalds {
18459d6a8c5cSMarc Eshel 	struct file_lock file_lock;
18461da177e4SLinus Torvalds 	struct flock flock;
18471da177e4SLinus Torvalds 	int error;
18481da177e4SLinus Torvalds 
18491da177e4SLinus Torvalds 	error = -EFAULT;
18501da177e4SLinus Torvalds 	if (copy_from_user(&flock, l, sizeof(flock)))
18511da177e4SLinus Torvalds 		goto out;
18521da177e4SLinus Torvalds 	error = -EINVAL;
18531da177e4SLinus Torvalds 	if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK))
18541da177e4SLinus Torvalds 		goto out;
18551da177e4SLinus Torvalds 
18561da177e4SLinus Torvalds 	error = flock_to_posix_lock(filp, &file_lock, &flock);
18571da177e4SLinus Torvalds 	if (error)
18581da177e4SLinus Torvalds 		goto out;
18591da177e4SLinus Torvalds 
18603ee17abdSJ. Bruce Fields 	error = vfs_test_lock(filp, &file_lock);
18613ee17abdSJ. Bruce Fields 	if (error)
18621da177e4SLinus Torvalds 		goto out;
18631da177e4SLinus Torvalds 
18649d6a8c5cSMarc Eshel 	flock.l_type = file_lock.fl_type;
18659d6a8c5cSMarc Eshel 	if (file_lock.fl_type != F_UNLCK) {
18669d6a8c5cSMarc Eshel 		error = posix_lock_to_flock(&flock, &file_lock);
1867c2fa1b8aSJ. Bruce Fields 		if (error)
18681da177e4SLinus Torvalds 			goto out;
18691da177e4SLinus Torvalds 	}
18701da177e4SLinus Torvalds 	error = -EFAULT;
18711da177e4SLinus Torvalds 	if (!copy_to_user(l, &flock, sizeof(flock)))
18721da177e4SLinus Torvalds 		error = 0;
18731da177e4SLinus Torvalds out:
18741da177e4SLinus Torvalds 	return error;
18751da177e4SLinus Torvalds }
18761da177e4SLinus Torvalds 
18777723ec97SMarc Eshel /**
18787723ec97SMarc Eshel  * vfs_lock_file - file byte range lock
18797723ec97SMarc Eshel  * @filp: The file to apply the lock to
18807723ec97SMarc Eshel  * @cmd: type of locking operation (F_SETLK, F_GETLK, etc.)
18817723ec97SMarc Eshel  * @fl: The lock to be applied
1882150b3934SMarc Eshel  * @conf: Place to return a copy of the conflicting lock, if found.
1883150b3934SMarc Eshel  *
1884150b3934SMarc Eshel  * A caller that doesn't care about the conflicting lock may pass NULL
1885150b3934SMarc Eshel  * as the final argument.
1886150b3934SMarc Eshel  *
1887150b3934SMarc Eshel  * If the filesystem defines a private ->lock() method, then @conf will
1888150b3934SMarc Eshel  * be left unchanged; so a caller that cares should initialize it to
1889150b3934SMarc Eshel  * some acceptable default.
18902beb6614SMarc Eshel  *
18912beb6614SMarc Eshel  * To avoid blocking kernel daemons, such as lockd, that need to acquire POSIX
18922beb6614SMarc Eshel  * locks, the ->lock() interface may return asynchronously, before the lock has
18932beb6614SMarc Eshel  * been granted or denied by the underlying filesystem, if (and only if)
18948fb47a4fSJ. Bruce Fields  * lm_grant is set. Callers expecting ->lock() to return asynchronously
18952beb6614SMarc Eshel  * will only use F_SETLK, not F_SETLKW; they will set FL_SLEEP if (and only if)
18962beb6614SMarc Eshel  * the request is for a blocking lock. When ->lock() does return asynchronously,
18978fb47a4fSJ. Bruce Fields  * it must return FILE_LOCK_DEFERRED, and call ->lm_grant() when the lock
18982beb6614SMarc Eshel  * request completes.
18992beb6614SMarc Eshel  * If the request is for non-blocking lock the file system should return
1900bde74e4bSMiklos Szeredi  * FILE_LOCK_DEFERRED then try to get the lock and call the callback routine
1901bde74e4bSMiklos Szeredi  * with the result. If the request timed out the callback routine will return a
19022beb6614SMarc Eshel  * nonzero return code and the file system should release the lock. The file
19032beb6614SMarc Eshel  * system is also responsible to keep a corresponding posix lock when it
19042beb6614SMarc Eshel  * grants a lock so the VFS can find out which locks are locally held and do
19052beb6614SMarc Eshel  * the correct lock cleanup when required.
19062beb6614SMarc Eshel  * The underlying filesystem must not drop the kernel lock or call
19078fb47a4fSJ. Bruce Fields  * ->lm_grant() before returning to the caller with a FILE_LOCK_DEFERRED
19082beb6614SMarc Eshel  * return code.
19097723ec97SMarc Eshel  */
1910150b3934SMarc Eshel int vfs_lock_file(struct file *filp, unsigned int cmd, struct file_lock *fl, struct file_lock *conf)
19117723ec97SMarc Eshel {
191272c2d531SAl Viro 	if (filp->f_op->lock)
19137723ec97SMarc Eshel 		return filp->f_op->lock(filp, cmd, fl);
19147723ec97SMarc Eshel 	else
1915150b3934SMarc Eshel 		return posix_lock_file(filp, fl, conf);
19167723ec97SMarc Eshel }
19177723ec97SMarc Eshel EXPORT_SYMBOL_GPL(vfs_lock_file);
19187723ec97SMarc Eshel 
1919b648a6deSMiklos Szeredi static int do_lock_file_wait(struct file *filp, unsigned int cmd,
1920b648a6deSMiklos Szeredi 			     struct file_lock *fl)
1921b648a6deSMiklos Szeredi {
1922b648a6deSMiklos Szeredi 	int error;
1923b648a6deSMiklos Szeredi 
1924b648a6deSMiklos Szeredi 	error = security_file_lock(filp, fl->fl_type);
1925b648a6deSMiklos Szeredi 	if (error)
1926b648a6deSMiklos Szeredi 		return error;
1927b648a6deSMiklos Szeredi 
1928b648a6deSMiklos Szeredi 	for (;;) {
1929764c76b3SMiklos Szeredi 		error = vfs_lock_file(filp, cmd, fl, NULL);
1930b648a6deSMiklos Szeredi 		if (error != FILE_LOCK_DEFERRED)
1931b648a6deSMiklos Szeredi 			break;
1932764c76b3SMiklos Szeredi 		error = wait_event_interruptible(fl->fl_wait, !fl->fl_next);
1933b648a6deSMiklos Szeredi 		if (!error)
1934b648a6deSMiklos Szeredi 			continue;
1935b648a6deSMiklos Szeredi 
1936b648a6deSMiklos Szeredi 		locks_delete_block(fl);
1937b648a6deSMiklos Szeredi 		break;
1938b648a6deSMiklos Szeredi 	}
1939b648a6deSMiklos Szeredi 
1940b648a6deSMiklos Szeredi 	return error;
1941b648a6deSMiklos Szeredi }
1942b648a6deSMiklos Szeredi 
19431da177e4SLinus Torvalds /* Apply the lock described by l to an open file descriptor.
19441da177e4SLinus Torvalds  * This implements both the F_SETLK and F_SETLKW commands of fcntl().
19451da177e4SLinus Torvalds  */
1946c293621bSPeter Staubach int fcntl_setlk(unsigned int fd, struct file *filp, unsigned int cmd,
1947c293621bSPeter Staubach 		struct flock __user *l)
19481da177e4SLinus Torvalds {
19491da177e4SLinus Torvalds 	struct file_lock *file_lock = locks_alloc_lock();
19501da177e4SLinus Torvalds 	struct flock flock;
19511da177e4SLinus Torvalds 	struct inode *inode;
19520b2bac2fSAl Viro 	struct file *f;
19531da177e4SLinus Torvalds 	int error;
19541da177e4SLinus Torvalds 
19551da177e4SLinus Torvalds 	if (file_lock == NULL)
19561da177e4SLinus Torvalds 		return -ENOLCK;
19571da177e4SLinus Torvalds 
19581da177e4SLinus Torvalds 	/*
19591da177e4SLinus Torvalds 	 * This might block, so we do it before checking the inode.
19601da177e4SLinus Torvalds 	 */
19611da177e4SLinus Torvalds 	error = -EFAULT;
19621da177e4SLinus Torvalds 	if (copy_from_user(&flock, l, sizeof(flock)))
19631da177e4SLinus Torvalds 		goto out;
19641da177e4SLinus Torvalds 
1965496ad9aaSAl Viro 	inode = file_inode(filp);
19661da177e4SLinus Torvalds 
19671da177e4SLinus Torvalds 	/* Don't allow mandatory locks on files that may be memory mapped
19681da177e4SLinus Torvalds 	 * and shared.
19691da177e4SLinus Torvalds 	 */
1970a16877caSPavel Emelyanov 	if (mandatory_lock(inode) && mapping_writably_mapped(filp->f_mapping)) {
19711da177e4SLinus Torvalds 		error = -EAGAIN;
19721da177e4SLinus Torvalds 		goto out;
19731da177e4SLinus Torvalds 	}
19741da177e4SLinus Torvalds 
1975c293621bSPeter Staubach again:
19761da177e4SLinus Torvalds 	error = flock_to_posix_lock(filp, file_lock, &flock);
19771da177e4SLinus Torvalds 	if (error)
19781da177e4SLinus Torvalds 		goto out;
19791da177e4SLinus Torvalds 	if (cmd == F_SETLKW) {
19801da177e4SLinus Torvalds 		file_lock->fl_flags |= FL_SLEEP;
19811da177e4SLinus Torvalds 	}
19821da177e4SLinus Torvalds 
19831da177e4SLinus Torvalds 	error = -EBADF;
19841da177e4SLinus Torvalds 	switch (flock.l_type) {
19851da177e4SLinus Torvalds 	case F_RDLCK:
19861da177e4SLinus Torvalds 		if (!(filp->f_mode & FMODE_READ))
19871da177e4SLinus Torvalds 			goto out;
19881da177e4SLinus Torvalds 		break;
19891da177e4SLinus Torvalds 	case F_WRLCK:
19901da177e4SLinus Torvalds 		if (!(filp->f_mode & FMODE_WRITE))
19911da177e4SLinus Torvalds 			goto out;
19921da177e4SLinus Torvalds 		break;
19931da177e4SLinus Torvalds 	case F_UNLCK:
19941da177e4SLinus Torvalds 		break;
19951da177e4SLinus Torvalds 	default:
19961da177e4SLinus Torvalds 		error = -EINVAL;
19971da177e4SLinus Torvalds 		goto out;
19981da177e4SLinus Torvalds 	}
19991da177e4SLinus Torvalds 
2000b648a6deSMiklos Szeredi 	error = do_lock_file_wait(filp, cmd, file_lock);
2001c293621bSPeter Staubach 
2002c293621bSPeter Staubach 	/*
2003c293621bSPeter Staubach 	 * Attempt to detect a close/fcntl race and recover by
2004c293621bSPeter Staubach 	 * releasing the lock that was just acquired.
2005c293621bSPeter Staubach 	 */
20060b2bac2fSAl Viro 	/*
20070b2bac2fSAl Viro 	 * we need that spin_lock here - it prevents reordering between
20080b2bac2fSAl Viro 	 * update of inode->i_flock and check for it done in close().
20090b2bac2fSAl Viro 	 * rcu_read_lock() wouldn't do.
20100b2bac2fSAl Viro 	 */
20110b2bac2fSAl Viro 	spin_lock(&current->files->file_lock);
20120b2bac2fSAl Viro 	f = fcheck(fd);
20130b2bac2fSAl Viro 	spin_unlock(&current->files->file_lock);
20140b2bac2fSAl Viro 	if (!error && f != filp && flock.l_type != F_UNLCK) {
2015c293621bSPeter Staubach 		flock.l_type = F_UNLCK;
2016c293621bSPeter Staubach 		goto again;
2017c293621bSPeter Staubach 	}
20181da177e4SLinus Torvalds 
20191da177e4SLinus Torvalds out:
20201da177e4SLinus Torvalds 	locks_free_lock(file_lock);
20211da177e4SLinus Torvalds 	return error;
20221da177e4SLinus Torvalds }
20231da177e4SLinus Torvalds 
20241da177e4SLinus Torvalds #if BITS_PER_LONG == 32
20251da177e4SLinus Torvalds /* Report the first existing lock that would conflict with l.
20261da177e4SLinus Torvalds  * This implements the F_GETLK command of fcntl().
20271da177e4SLinus Torvalds  */
20281da177e4SLinus Torvalds int fcntl_getlk64(struct file *filp, struct flock64 __user *l)
20291da177e4SLinus Torvalds {
20309d6a8c5cSMarc Eshel 	struct file_lock file_lock;
20311da177e4SLinus Torvalds 	struct flock64 flock;
20321da177e4SLinus Torvalds 	int error;
20331da177e4SLinus Torvalds 
20341da177e4SLinus Torvalds 	error = -EFAULT;
20351da177e4SLinus Torvalds 	if (copy_from_user(&flock, l, sizeof(flock)))
20361da177e4SLinus Torvalds 		goto out;
20371da177e4SLinus Torvalds 	error = -EINVAL;
20381da177e4SLinus Torvalds 	if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK))
20391da177e4SLinus Torvalds 		goto out;
20401da177e4SLinus Torvalds 
20411da177e4SLinus Torvalds 	error = flock64_to_posix_lock(filp, &file_lock, &flock);
20421da177e4SLinus Torvalds 	if (error)
20431da177e4SLinus Torvalds 		goto out;
20441da177e4SLinus Torvalds 
20453ee17abdSJ. Bruce Fields 	error = vfs_test_lock(filp, &file_lock);
20463ee17abdSJ. Bruce Fields 	if (error)
20471da177e4SLinus Torvalds 		goto out;
20481da177e4SLinus Torvalds 
20499d6a8c5cSMarc Eshel 	flock.l_type = file_lock.fl_type;
20509d6a8c5cSMarc Eshel 	if (file_lock.fl_type != F_UNLCK)
20519d6a8c5cSMarc Eshel 		posix_lock_to_flock64(&flock, &file_lock);
20529d6a8c5cSMarc Eshel 
20531da177e4SLinus Torvalds 	error = -EFAULT;
20541da177e4SLinus Torvalds 	if (!copy_to_user(l, &flock, sizeof(flock)))
20551da177e4SLinus Torvalds 		error = 0;
20561da177e4SLinus Torvalds 
20571da177e4SLinus Torvalds out:
20581da177e4SLinus Torvalds 	return error;
20591da177e4SLinus Torvalds }
20601da177e4SLinus Torvalds 
20611da177e4SLinus Torvalds /* Apply the lock described by l to an open file descriptor.
20621da177e4SLinus Torvalds  * This implements both the F_SETLK and F_SETLKW commands of fcntl().
20631da177e4SLinus Torvalds  */
2064c293621bSPeter Staubach int fcntl_setlk64(unsigned int fd, struct file *filp, unsigned int cmd,
2065c293621bSPeter Staubach 		struct flock64 __user *l)
20661da177e4SLinus Torvalds {
20671da177e4SLinus Torvalds 	struct file_lock *file_lock = locks_alloc_lock();
20681da177e4SLinus Torvalds 	struct flock64 flock;
20691da177e4SLinus Torvalds 	struct inode *inode;
20700b2bac2fSAl Viro 	struct file *f;
20711da177e4SLinus Torvalds 	int error;
20721da177e4SLinus Torvalds 
20731da177e4SLinus Torvalds 	if (file_lock == NULL)
20741da177e4SLinus Torvalds 		return -ENOLCK;
20751da177e4SLinus Torvalds 
20761da177e4SLinus Torvalds 	/*
20771da177e4SLinus Torvalds 	 * This might block, so we do it before checking the inode.
20781da177e4SLinus Torvalds 	 */
20791da177e4SLinus Torvalds 	error = -EFAULT;
20801da177e4SLinus Torvalds 	if (copy_from_user(&flock, l, sizeof(flock)))
20811da177e4SLinus Torvalds 		goto out;
20821da177e4SLinus Torvalds 
2083496ad9aaSAl Viro 	inode = file_inode(filp);
20841da177e4SLinus Torvalds 
20851da177e4SLinus Torvalds 	/* Don't allow mandatory locks on files that may be memory mapped
20861da177e4SLinus Torvalds 	 * and shared.
20871da177e4SLinus Torvalds 	 */
2088a16877caSPavel Emelyanov 	if (mandatory_lock(inode) && mapping_writably_mapped(filp->f_mapping)) {
20891da177e4SLinus Torvalds 		error = -EAGAIN;
20901da177e4SLinus Torvalds 		goto out;
20911da177e4SLinus Torvalds 	}
20921da177e4SLinus Torvalds 
2093c293621bSPeter Staubach again:
20941da177e4SLinus Torvalds 	error = flock64_to_posix_lock(filp, file_lock, &flock);
20951da177e4SLinus Torvalds 	if (error)
20961da177e4SLinus Torvalds 		goto out;
20971da177e4SLinus Torvalds 	if (cmd == F_SETLKW64) {
20981da177e4SLinus Torvalds 		file_lock->fl_flags |= FL_SLEEP;
20991da177e4SLinus Torvalds 	}
21001da177e4SLinus Torvalds 
21011da177e4SLinus Torvalds 	error = -EBADF;
21021da177e4SLinus Torvalds 	switch (flock.l_type) {
21031da177e4SLinus Torvalds 	case F_RDLCK:
21041da177e4SLinus Torvalds 		if (!(filp->f_mode & FMODE_READ))
21051da177e4SLinus Torvalds 			goto out;
21061da177e4SLinus Torvalds 		break;
21071da177e4SLinus Torvalds 	case F_WRLCK:
21081da177e4SLinus Torvalds 		if (!(filp->f_mode & FMODE_WRITE))
21091da177e4SLinus Torvalds 			goto out;
21101da177e4SLinus Torvalds 		break;
21111da177e4SLinus Torvalds 	case F_UNLCK:
21121da177e4SLinus Torvalds 		break;
21131da177e4SLinus Torvalds 	default:
21141da177e4SLinus Torvalds 		error = -EINVAL;
21151da177e4SLinus Torvalds 		goto out;
21161da177e4SLinus Torvalds 	}
21171da177e4SLinus Torvalds 
2118b648a6deSMiklos Szeredi 	error = do_lock_file_wait(filp, cmd, file_lock);
2119c293621bSPeter Staubach 
2120c293621bSPeter Staubach 	/*
2121c293621bSPeter Staubach 	 * Attempt to detect a close/fcntl race and recover by
2122c293621bSPeter Staubach 	 * releasing the lock that was just acquired.
2123c293621bSPeter Staubach 	 */
21240b2bac2fSAl Viro 	spin_lock(&current->files->file_lock);
21250b2bac2fSAl Viro 	f = fcheck(fd);
21260b2bac2fSAl Viro 	spin_unlock(&current->files->file_lock);
21270b2bac2fSAl Viro 	if (!error && f != filp && flock.l_type != F_UNLCK) {
2128c293621bSPeter Staubach 		flock.l_type = F_UNLCK;
2129c293621bSPeter Staubach 		goto again;
2130c293621bSPeter Staubach 	}
21311da177e4SLinus Torvalds 
21321da177e4SLinus Torvalds out:
21331da177e4SLinus Torvalds 	locks_free_lock(file_lock);
21341da177e4SLinus Torvalds 	return error;
21351da177e4SLinus Torvalds }
21361da177e4SLinus Torvalds #endif /* BITS_PER_LONG == 32 */
21371da177e4SLinus Torvalds 
21381da177e4SLinus Torvalds /*
21391da177e4SLinus Torvalds  * This function is called when the file is being removed
21401da177e4SLinus Torvalds  * from the task's fd array.  POSIX locks belonging to this task
21411da177e4SLinus Torvalds  * are deleted at this time.
21421da177e4SLinus Torvalds  */
21431da177e4SLinus Torvalds void locks_remove_posix(struct file *filp, fl_owner_t owner)
21441da177e4SLinus Torvalds {
2145ff7b86b8SMiklos Szeredi 	struct file_lock lock;
21461da177e4SLinus Torvalds 
21471da177e4SLinus Torvalds 	/*
21481da177e4SLinus Torvalds 	 * If there are no locks held on this file, we don't need to call
21491da177e4SLinus Torvalds 	 * posix_lock_file().  Another process could be setting a lock on this
21501da177e4SLinus Torvalds 	 * file at the same time, but we wouldn't remove that lock anyway.
21511da177e4SLinus Torvalds 	 */
2152496ad9aaSAl Viro 	if (!file_inode(filp)->i_flock)
21531da177e4SLinus Torvalds 		return;
21541da177e4SLinus Torvalds 
21551da177e4SLinus Torvalds 	lock.fl_type = F_UNLCK;
215675e1fcc0SMiklos Szeredi 	lock.fl_flags = FL_POSIX | FL_CLOSE;
21571da177e4SLinus Torvalds 	lock.fl_start = 0;
21581da177e4SLinus Torvalds 	lock.fl_end = OFFSET_MAX;
21591da177e4SLinus Torvalds 	lock.fl_owner = owner;
21601da177e4SLinus Torvalds 	lock.fl_pid = current->tgid;
21611da177e4SLinus Torvalds 	lock.fl_file = filp;
21621da177e4SLinus Torvalds 	lock.fl_ops = NULL;
21631da177e4SLinus Torvalds 	lock.fl_lmops = NULL;
21641da177e4SLinus Torvalds 
2165150b3934SMarc Eshel 	vfs_lock_file(filp, F_SETLK, &lock, NULL);
21661da177e4SLinus Torvalds 
21671da177e4SLinus Torvalds 	if (lock.fl_ops && lock.fl_ops->fl_release_private)
21681da177e4SLinus Torvalds 		lock.fl_ops->fl_release_private(&lock);
21691da177e4SLinus Torvalds }
21701da177e4SLinus Torvalds 
21711da177e4SLinus Torvalds EXPORT_SYMBOL(locks_remove_posix);
21721da177e4SLinus Torvalds 
21731da177e4SLinus Torvalds /*
21741da177e4SLinus Torvalds  * This function is called on the last close of an open file.
21751da177e4SLinus Torvalds  */
21761da177e4SLinus Torvalds void locks_remove_flock(struct file *filp)
21771da177e4SLinus Torvalds {
2178496ad9aaSAl Viro 	struct inode * inode = file_inode(filp);
21791da177e4SLinus Torvalds 	struct file_lock *fl;
21801da177e4SLinus Torvalds 	struct file_lock **before;
21811da177e4SLinus Torvalds 
21821da177e4SLinus Torvalds 	if (!inode->i_flock)
21831da177e4SLinus Torvalds 		return;
21841da177e4SLinus Torvalds 
218572c2d531SAl Viro 	if (filp->f_op->flock) {
21861da177e4SLinus Torvalds 		struct file_lock fl = {
21871da177e4SLinus Torvalds 			.fl_pid = current->tgid,
21881da177e4SLinus Torvalds 			.fl_file = filp,
21891da177e4SLinus Torvalds 			.fl_flags = FL_FLOCK,
21901da177e4SLinus Torvalds 			.fl_type = F_UNLCK,
21911da177e4SLinus Torvalds 			.fl_end = OFFSET_MAX,
21921da177e4SLinus Torvalds 		};
21931da177e4SLinus Torvalds 		filp->f_op->flock(filp, F_SETLKW, &fl);
219480fec4c6STrond Myklebust 		if (fl.fl_ops && fl.fl_ops->fl_release_private)
219580fec4c6STrond Myklebust 			fl.fl_ops->fl_release_private(&fl);
21961da177e4SLinus Torvalds 	}
21971da177e4SLinus Torvalds 
21981c8c601aSJeff Layton 	spin_lock(&inode->i_lock);
21991da177e4SLinus Torvalds 	before = &inode->i_flock;
22001da177e4SLinus Torvalds 
22011da177e4SLinus Torvalds 	while ((fl = *before) != NULL) {
22021da177e4SLinus Torvalds 		if (fl->fl_file == filp) {
2203c293621bSPeter Staubach 			if (IS_FLOCK(fl)) {
22041da177e4SLinus Torvalds 				locks_delete_lock(before);
22051da177e4SLinus Torvalds 				continue;
22061da177e4SLinus Torvalds 			}
22071da177e4SLinus Torvalds 			if (IS_LEASE(fl)) {
22081da177e4SLinus Torvalds 				lease_modify(before, F_UNLCK);
22091da177e4SLinus Torvalds 				continue;
22101da177e4SLinus Torvalds 			}
22111da177e4SLinus Torvalds 			/* What? */
22121da177e4SLinus Torvalds 			BUG();
22131da177e4SLinus Torvalds  		}
22141da177e4SLinus Torvalds 		before = &fl->fl_next;
22151da177e4SLinus Torvalds 	}
22161c8c601aSJeff Layton 	spin_unlock(&inode->i_lock);
22171da177e4SLinus Torvalds }
22181da177e4SLinus Torvalds 
22191da177e4SLinus Torvalds /**
22201da177e4SLinus Torvalds  *	posix_unblock_lock - stop waiting for a file lock
22211da177e4SLinus Torvalds  *	@waiter: the lock which was waiting
22221da177e4SLinus Torvalds  *
22231da177e4SLinus Torvalds  *	lockd needs to block waiting for locks.
22241da177e4SLinus Torvalds  */
222564a318eeSJ. Bruce Fields int
2226f891a29fSJeff Layton posix_unblock_lock(struct file_lock *waiter)
22271da177e4SLinus Torvalds {
222864a318eeSJ. Bruce Fields 	int status = 0;
222964a318eeSJ. Bruce Fields 
22307b2296afSJeff Layton 	spin_lock(&blocked_lock_lock);
22315996a298SJ. Bruce Fields 	if (waiter->fl_next)
22321da177e4SLinus Torvalds 		__locks_delete_block(waiter);
223364a318eeSJ. Bruce Fields 	else
223464a318eeSJ. Bruce Fields 		status = -ENOENT;
22357b2296afSJeff Layton 	spin_unlock(&blocked_lock_lock);
223664a318eeSJ. Bruce Fields 	return status;
22371da177e4SLinus Torvalds }
22381da177e4SLinus Torvalds EXPORT_SYMBOL(posix_unblock_lock);
22391da177e4SLinus Torvalds 
22409b9d2ab4SMarc Eshel /**
22419b9d2ab4SMarc Eshel  * vfs_cancel_lock - file byte range unblock lock
22429b9d2ab4SMarc Eshel  * @filp: The file to apply the unblock to
22439b9d2ab4SMarc Eshel  * @fl: The lock to be unblocked
22449b9d2ab4SMarc Eshel  *
22459b9d2ab4SMarc Eshel  * Used by lock managers to cancel blocked requests
22469b9d2ab4SMarc Eshel  */
22479b9d2ab4SMarc Eshel int vfs_cancel_lock(struct file *filp, struct file_lock *fl)
22489b9d2ab4SMarc Eshel {
224972c2d531SAl Viro 	if (filp->f_op->lock)
22509b9d2ab4SMarc Eshel 		return filp->f_op->lock(filp, F_CANCELLK, fl);
22519b9d2ab4SMarc Eshel 	return 0;
22529b9d2ab4SMarc Eshel }
22539b9d2ab4SMarc Eshel 
22549b9d2ab4SMarc Eshel EXPORT_SYMBOL_GPL(vfs_cancel_lock);
22559b9d2ab4SMarc Eshel 
22567f8ada98SPavel Emelyanov #ifdef CONFIG_PROC_FS
2257d8ba7a36SAlexey Dobriyan #include <linux/proc_fs.h>
22587f8ada98SPavel Emelyanov #include <linux/seq_file.h>
22597f8ada98SPavel Emelyanov 
22607012b02aSJeff Layton struct locks_iterator {
22617012b02aSJeff Layton 	int	li_cpu;
22627012b02aSJeff Layton 	loff_t	li_pos;
22637012b02aSJeff Layton };
22647012b02aSJeff Layton 
22657f8ada98SPavel Emelyanov static void lock_get_status(struct seq_file *f, struct file_lock *fl,
226699dc8292SJerome Marchand 			    loff_t id, char *pfx)
22671da177e4SLinus Torvalds {
22681da177e4SLinus Torvalds 	struct inode *inode = NULL;
2269ab1f1611SVitaliy Gusev 	unsigned int fl_pid;
2270ab1f1611SVitaliy Gusev 
2271ab1f1611SVitaliy Gusev 	if (fl->fl_nspid)
22726c5f3e7bSPavel Emelyanov 		fl_pid = pid_vnr(fl->fl_nspid);
2273ab1f1611SVitaliy Gusev 	else
2274ab1f1611SVitaliy Gusev 		fl_pid = fl->fl_pid;
22751da177e4SLinus Torvalds 
22761da177e4SLinus Torvalds 	if (fl->fl_file != NULL)
2277496ad9aaSAl Viro 		inode = file_inode(fl->fl_file);
22781da177e4SLinus Torvalds 
227999dc8292SJerome Marchand 	seq_printf(f, "%lld:%s ", id, pfx);
22801da177e4SLinus Torvalds 	if (IS_POSIX(fl)) {
22817f8ada98SPavel Emelyanov 		seq_printf(f, "%6s %s ",
22821da177e4SLinus Torvalds 			     (fl->fl_flags & FL_ACCESS) ? "ACCESS" : "POSIX ",
22831da177e4SLinus Torvalds 			     (inode == NULL) ? "*NOINODE*" :
2284a16877caSPavel Emelyanov 			     mandatory_lock(inode) ? "MANDATORY" : "ADVISORY ");
22851da177e4SLinus Torvalds 	} else if (IS_FLOCK(fl)) {
22861da177e4SLinus Torvalds 		if (fl->fl_type & LOCK_MAND) {
22877f8ada98SPavel Emelyanov 			seq_printf(f, "FLOCK  MSNFS     ");
22881da177e4SLinus Torvalds 		} else {
22897f8ada98SPavel Emelyanov 			seq_printf(f, "FLOCK  ADVISORY  ");
22901da177e4SLinus Torvalds 		}
22911da177e4SLinus Torvalds 	} else if (IS_LEASE(fl)) {
22927f8ada98SPavel Emelyanov 		seq_printf(f, "LEASE  ");
2293ab83fa4bSJ. Bruce Fields 		if (lease_breaking(fl))
22947f8ada98SPavel Emelyanov 			seq_printf(f, "BREAKING  ");
22951da177e4SLinus Torvalds 		else if (fl->fl_file)
22967f8ada98SPavel Emelyanov 			seq_printf(f, "ACTIVE    ");
22971da177e4SLinus Torvalds 		else
22987f8ada98SPavel Emelyanov 			seq_printf(f, "BREAKER   ");
22991da177e4SLinus Torvalds 	} else {
23007f8ada98SPavel Emelyanov 		seq_printf(f, "UNKNOWN UNKNOWN  ");
23011da177e4SLinus Torvalds 	}
23021da177e4SLinus Torvalds 	if (fl->fl_type & LOCK_MAND) {
23037f8ada98SPavel Emelyanov 		seq_printf(f, "%s ",
23041da177e4SLinus Torvalds 			       (fl->fl_type & LOCK_READ)
23051da177e4SLinus Torvalds 			       ? (fl->fl_type & LOCK_WRITE) ? "RW   " : "READ "
23061da177e4SLinus Torvalds 			       : (fl->fl_type & LOCK_WRITE) ? "WRITE" : "NONE ");
23071da177e4SLinus Torvalds 	} else {
23087f8ada98SPavel Emelyanov 		seq_printf(f, "%s ",
2309ab83fa4bSJ. Bruce Fields 			       (lease_breaking(fl))
23100ee5c6d6SJeff Layton 			       ? (fl->fl_type == F_UNLCK) ? "UNLCK" : "READ "
23110ee5c6d6SJeff Layton 			       : (fl->fl_type == F_WRLCK) ? "WRITE" : "READ ");
23121da177e4SLinus Torvalds 	}
23131da177e4SLinus Torvalds 	if (inode) {
23141da177e4SLinus Torvalds #ifdef WE_CAN_BREAK_LSLK_NOW
2315ab1f1611SVitaliy Gusev 		seq_printf(f, "%d %s:%ld ", fl_pid,
23161da177e4SLinus Torvalds 				inode->i_sb->s_id, inode->i_ino);
23171da177e4SLinus Torvalds #else
23181da177e4SLinus Torvalds 		/* userspace relies on this representation of dev_t ;-( */
2319ab1f1611SVitaliy Gusev 		seq_printf(f, "%d %02x:%02x:%ld ", fl_pid,
23201da177e4SLinus Torvalds 				MAJOR(inode->i_sb->s_dev),
23211da177e4SLinus Torvalds 				MINOR(inode->i_sb->s_dev), inode->i_ino);
23221da177e4SLinus Torvalds #endif
23231da177e4SLinus Torvalds 	} else {
2324ab1f1611SVitaliy Gusev 		seq_printf(f, "%d <none>:0 ", fl_pid);
23251da177e4SLinus Torvalds 	}
23261da177e4SLinus Torvalds 	if (IS_POSIX(fl)) {
23271da177e4SLinus Torvalds 		if (fl->fl_end == OFFSET_MAX)
23287f8ada98SPavel Emelyanov 			seq_printf(f, "%Ld EOF\n", fl->fl_start);
23291da177e4SLinus Torvalds 		else
23307f8ada98SPavel Emelyanov 			seq_printf(f, "%Ld %Ld\n", fl->fl_start, fl->fl_end);
23311da177e4SLinus Torvalds 	} else {
23327f8ada98SPavel Emelyanov 		seq_printf(f, "0 EOF\n");
23331da177e4SLinus Torvalds 	}
23341da177e4SLinus Torvalds }
23351da177e4SLinus Torvalds 
23367f8ada98SPavel Emelyanov static int locks_show(struct seq_file *f, void *v)
23371da177e4SLinus Torvalds {
23387012b02aSJeff Layton 	struct locks_iterator *iter = f->private;
23397f8ada98SPavel Emelyanov 	struct file_lock *fl, *bfl;
23407f8ada98SPavel Emelyanov 
2341139ca04eSJeff Layton 	fl = hlist_entry(v, struct file_lock, fl_link);
23427f8ada98SPavel Emelyanov 
23437012b02aSJeff Layton 	lock_get_status(f, fl, iter->li_pos, "");
23447f8ada98SPavel Emelyanov 
23457f8ada98SPavel Emelyanov 	list_for_each_entry(bfl, &fl->fl_block, fl_block)
23467012b02aSJeff Layton 		lock_get_status(f, bfl, iter->li_pos, " ->");
23477f8ada98SPavel Emelyanov 
23487f8ada98SPavel Emelyanov 	return 0;
23491da177e4SLinus Torvalds }
23501da177e4SLinus Torvalds 
23517f8ada98SPavel Emelyanov static void *locks_start(struct seq_file *f, loff_t *pos)
23521da177e4SLinus Torvalds {
23537012b02aSJeff Layton 	struct locks_iterator *iter = f->private;
235499dc8292SJerome Marchand 
23557012b02aSJeff Layton 	iter->li_pos = *pos + 1;
23567012b02aSJeff Layton 	lg_global_lock(&file_lock_lglock);
23577b2296afSJeff Layton 	spin_lock(&blocked_lock_lock);
23587012b02aSJeff Layton 	return seq_hlist_start_percpu(&file_lock_list, &iter->li_cpu, *pos);
23591da177e4SLinus Torvalds }
23607f8ada98SPavel Emelyanov 
23617f8ada98SPavel Emelyanov static void *locks_next(struct seq_file *f, void *v, loff_t *pos)
23627f8ada98SPavel Emelyanov {
23637012b02aSJeff Layton 	struct locks_iterator *iter = f->private;
23647012b02aSJeff Layton 
23657012b02aSJeff Layton 	++iter->li_pos;
23667012b02aSJeff Layton 	return seq_hlist_next_percpu(v, &file_lock_list, &iter->li_cpu, pos);
23671da177e4SLinus Torvalds }
23687f8ada98SPavel Emelyanov 
23697f8ada98SPavel Emelyanov static void locks_stop(struct seq_file *f, void *v)
23707f8ada98SPavel Emelyanov {
23717b2296afSJeff Layton 	spin_unlock(&blocked_lock_lock);
23727012b02aSJeff Layton 	lg_global_unlock(&file_lock_lglock);
23731da177e4SLinus Torvalds }
23741da177e4SLinus Torvalds 
2375d8ba7a36SAlexey Dobriyan static const struct seq_operations locks_seq_operations = {
23767f8ada98SPavel Emelyanov 	.start	= locks_start,
23777f8ada98SPavel Emelyanov 	.next	= locks_next,
23787f8ada98SPavel Emelyanov 	.stop	= locks_stop,
23797f8ada98SPavel Emelyanov 	.show	= locks_show,
23807f8ada98SPavel Emelyanov };
2381d8ba7a36SAlexey Dobriyan 
2382d8ba7a36SAlexey Dobriyan static int locks_open(struct inode *inode, struct file *filp)
2383d8ba7a36SAlexey Dobriyan {
23847012b02aSJeff Layton 	return seq_open_private(filp, &locks_seq_operations,
23857012b02aSJeff Layton 					sizeof(struct locks_iterator));
2386d8ba7a36SAlexey Dobriyan }
2387d8ba7a36SAlexey Dobriyan 
2388d8ba7a36SAlexey Dobriyan static const struct file_operations proc_locks_operations = {
2389d8ba7a36SAlexey Dobriyan 	.open		= locks_open,
2390d8ba7a36SAlexey Dobriyan 	.read		= seq_read,
2391d8ba7a36SAlexey Dobriyan 	.llseek		= seq_lseek,
239299dc8292SJerome Marchand 	.release	= seq_release_private,
2393d8ba7a36SAlexey Dobriyan };
2394d8ba7a36SAlexey Dobriyan 
2395d8ba7a36SAlexey Dobriyan static int __init proc_locks_init(void)
2396d8ba7a36SAlexey Dobriyan {
2397d8ba7a36SAlexey Dobriyan 	proc_create("locks", 0, NULL, &proc_locks_operations);
2398d8ba7a36SAlexey Dobriyan 	return 0;
2399d8ba7a36SAlexey Dobriyan }
2400d8ba7a36SAlexey Dobriyan module_init(proc_locks_init);
24017f8ada98SPavel Emelyanov #endif
24027f8ada98SPavel Emelyanov 
24031da177e4SLinus Torvalds /**
24041da177e4SLinus Torvalds  *	lock_may_read - checks that the region is free of locks
24051da177e4SLinus Torvalds  *	@inode: the inode that is being read
24061da177e4SLinus Torvalds  *	@start: the first byte to read
24071da177e4SLinus Torvalds  *	@len: the number of bytes to read
24081da177e4SLinus Torvalds  *
24091da177e4SLinus Torvalds  *	Emulates Windows locking requirements.  Whole-file
24101da177e4SLinus Torvalds  *	mandatory locks (share modes) can prohibit a read and
24111da177e4SLinus Torvalds  *	byte-range POSIX locks can prohibit a read if they overlap.
24121da177e4SLinus Torvalds  *
24131da177e4SLinus Torvalds  *	N.B. this function is only ever called
24141da177e4SLinus Torvalds  *	from knfsd and ownership of locks is never checked.
24151da177e4SLinus Torvalds  */
24161da177e4SLinus Torvalds int lock_may_read(struct inode *inode, loff_t start, unsigned long len)
24171da177e4SLinus Torvalds {
24181da177e4SLinus Torvalds 	struct file_lock *fl;
24191da177e4SLinus Torvalds 	int result = 1;
24201c8c601aSJeff Layton 
24211c8c601aSJeff Layton 	spin_lock(&inode->i_lock);
24221da177e4SLinus Torvalds 	for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
24231da177e4SLinus Torvalds 		if (IS_POSIX(fl)) {
24241da177e4SLinus Torvalds 			if (fl->fl_type == F_RDLCK)
24251da177e4SLinus Torvalds 				continue;
24261da177e4SLinus Torvalds 			if ((fl->fl_end < start) || (fl->fl_start > (start + len)))
24271da177e4SLinus Torvalds 				continue;
24281da177e4SLinus Torvalds 		} else if (IS_FLOCK(fl)) {
24291da177e4SLinus Torvalds 			if (!(fl->fl_type & LOCK_MAND))
24301da177e4SLinus Torvalds 				continue;
24311da177e4SLinus Torvalds 			if (fl->fl_type & LOCK_READ)
24321da177e4SLinus Torvalds 				continue;
24331da177e4SLinus Torvalds 		} else
24341da177e4SLinus Torvalds 			continue;
24351da177e4SLinus Torvalds 		result = 0;
24361da177e4SLinus Torvalds 		break;
24371da177e4SLinus Torvalds 	}
24381c8c601aSJeff Layton 	spin_unlock(&inode->i_lock);
24391da177e4SLinus Torvalds 	return result;
24401da177e4SLinus Torvalds }
24411da177e4SLinus Torvalds 
24421da177e4SLinus Torvalds EXPORT_SYMBOL(lock_may_read);
24431da177e4SLinus Torvalds 
24441da177e4SLinus Torvalds /**
24451da177e4SLinus Torvalds  *	lock_may_write - checks that the region is free of locks
24461da177e4SLinus Torvalds  *	@inode: the inode that is being written
24471da177e4SLinus Torvalds  *	@start: the first byte to write
24481da177e4SLinus Torvalds  *	@len: the number of bytes to write
24491da177e4SLinus Torvalds  *
24501da177e4SLinus Torvalds  *	Emulates Windows locking requirements.  Whole-file
24511da177e4SLinus Torvalds  *	mandatory locks (share modes) can prohibit a write and
24521da177e4SLinus Torvalds  *	byte-range POSIX locks can prohibit a write if they overlap.
24531da177e4SLinus Torvalds  *
24541da177e4SLinus Torvalds  *	N.B. this function is only ever called
24551da177e4SLinus Torvalds  *	from knfsd and ownership of locks is never checked.
24561da177e4SLinus Torvalds  */
24571da177e4SLinus Torvalds int lock_may_write(struct inode *inode, loff_t start, unsigned long len)
24581da177e4SLinus Torvalds {
24591da177e4SLinus Torvalds 	struct file_lock *fl;
24601da177e4SLinus Torvalds 	int result = 1;
24611c8c601aSJeff Layton 
24621c8c601aSJeff Layton 	spin_lock(&inode->i_lock);
24631da177e4SLinus Torvalds 	for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
24641da177e4SLinus Torvalds 		if (IS_POSIX(fl)) {
24651da177e4SLinus Torvalds 			if ((fl->fl_end < start) || (fl->fl_start > (start + len)))
24661da177e4SLinus Torvalds 				continue;
24671da177e4SLinus Torvalds 		} else if (IS_FLOCK(fl)) {
24681da177e4SLinus Torvalds 			if (!(fl->fl_type & LOCK_MAND))
24691da177e4SLinus Torvalds 				continue;
24701da177e4SLinus Torvalds 			if (fl->fl_type & LOCK_WRITE)
24711da177e4SLinus Torvalds 				continue;
24721da177e4SLinus Torvalds 		} else
24731da177e4SLinus Torvalds 			continue;
24741da177e4SLinus Torvalds 		result = 0;
24751da177e4SLinus Torvalds 		break;
24761da177e4SLinus Torvalds 	}
24771c8c601aSJeff Layton 	spin_unlock(&inode->i_lock);
24781da177e4SLinus Torvalds 	return result;
24791da177e4SLinus Torvalds }
24801da177e4SLinus Torvalds 
24811da177e4SLinus Torvalds EXPORT_SYMBOL(lock_may_write);
24821da177e4SLinus Torvalds 
24831da177e4SLinus Torvalds static int __init filelock_init(void)
24841da177e4SLinus Torvalds {
24857012b02aSJeff Layton 	int i;
24867012b02aSJeff Layton 
24871da177e4SLinus Torvalds 	filelock_cache = kmem_cache_create("file_lock_cache",
2488ee19cc40SMiklos Szeredi 			sizeof(struct file_lock), 0, SLAB_PANIC, NULL);
2489ee19cc40SMiklos Szeredi 
24907012b02aSJeff Layton 	lg_lock_init(&file_lock_lglock, "file_lock_lglock");
24917012b02aSJeff Layton 
24927012b02aSJeff Layton 	for_each_possible_cpu(i)
24937012b02aSJeff Layton 		INIT_HLIST_HEAD(per_cpu_ptr(&file_lock_list, i));
24947012b02aSJeff Layton 
24951da177e4SLinus Torvalds 	return 0;
24961da177e4SLinus Torvalds }
24971da177e4SLinus Torvalds 
24981da177e4SLinus Torvalds core_initcall(filelock_init);
2499