xref: /linux/fs/locks.c (revision e6f5c78930e409f3a6b37f5484313a416359ac7f)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/fs/locks.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Provide support for fcntl()'s F_GETLK, F_SETLK, and F_SETLKW calls.
51da177e4SLinus Torvalds  *  Doug Evans (dje@spiff.uucp), August 07, 1992
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *  Deadlock detection added.
81da177e4SLinus Torvalds  *  FIXME: one thing isn't handled yet:
91da177e4SLinus Torvalds  *	- mandatory locks (requires lots of changes elsewhere)
101da177e4SLinus Torvalds  *  Kelly Carmichael (kelly@[142.24.8.65]), September 17, 1994.
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  *  Miscellaneous edits, and a total rewrite of posix_lock_file() code.
131da177e4SLinus Torvalds  *  Kai Petzke (wpp@marie.physik.tu-berlin.de), 1994
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  *  Converted file_lock_table to a linked list from an array, which eliminates
161da177e4SLinus Torvalds  *  the limits on how many active file locks are open.
171da177e4SLinus Torvalds  *  Chad Page (pageone@netcom.com), November 27, 1994
181da177e4SLinus Torvalds  *
191da177e4SLinus Torvalds  *  Removed dependency on file descriptors. dup()'ed file descriptors now
201da177e4SLinus Torvalds  *  get the same locks as the original file descriptors, and a close() on
211da177e4SLinus Torvalds  *  any file descriptor removes ALL the locks on the file for the current
221da177e4SLinus Torvalds  *  process. Since locks still depend on the process id, locks are inherited
231da177e4SLinus Torvalds  *  after an exec() but not after a fork(). This agrees with POSIX, and both
241da177e4SLinus Torvalds  *  BSD and SVR4 practice.
251da177e4SLinus Torvalds  *  Andy Walker (andy@lysaker.kvaerner.no), February 14, 1995
261da177e4SLinus Torvalds  *
271da177e4SLinus Torvalds  *  Scrapped free list which is redundant now that we allocate locks
281da177e4SLinus Torvalds  *  dynamically with kmalloc()/kfree().
291da177e4SLinus Torvalds  *  Andy Walker (andy@lysaker.kvaerner.no), February 21, 1995
301da177e4SLinus Torvalds  *
311da177e4SLinus Torvalds  *  Implemented two lock personalities - FL_FLOCK and FL_POSIX.
321da177e4SLinus Torvalds  *
331da177e4SLinus Torvalds  *  FL_POSIX locks are created with calls to fcntl() and lockf() through the
341da177e4SLinus Torvalds  *  fcntl() system call. They have the semantics described above.
351da177e4SLinus Torvalds  *
361da177e4SLinus Torvalds  *  FL_FLOCK locks are created with calls to flock(), through the flock()
371da177e4SLinus Torvalds  *  system call, which is new. Old C libraries implement flock() via fcntl()
381da177e4SLinus Torvalds  *  and will continue to use the old, broken implementation.
391da177e4SLinus Torvalds  *
401da177e4SLinus Torvalds  *  FL_FLOCK locks follow the 4.4 BSD flock() semantics. They are associated
411da177e4SLinus Torvalds  *  with a file pointer (filp). As a result they can be shared by a parent
421da177e4SLinus Torvalds  *  process and its children after a fork(). They are removed when the last
431da177e4SLinus Torvalds  *  file descriptor referring to the file pointer is closed (unless explicitly
441da177e4SLinus Torvalds  *  unlocked).
451da177e4SLinus Torvalds  *
461da177e4SLinus Torvalds  *  FL_FLOCK locks never deadlock, an existing lock is always removed before
471da177e4SLinus Torvalds  *  upgrading from shared to exclusive (or vice versa). When this happens
481da177e4SLinus Torvalds  *  any processes blocked by the current lock are woken up and allowed to
491da177e4SLinus Torvalds  *  run before the new lock is applied.
501da177e4SLinus Torvalds  *  Andy Walker (andy@lysaker.kvaerner.no), June 09, 1995
511da177e4SLinus Torvalds  *
521da177e4SLinus Torvalds  *  Removed some race conditions in flock_lock_file(), marked other possible
531da177e4SLinus Torvalds  *  races. Just grep for FIXME to see them.
541da177e4SLinus Torvalds  *  Dmitry Gorodchanin (pgmdsg@ibi.com), February 09, 1996.
551da177e4SLinus Torvalds  *
561da177e4SLinus Torvalds  *  Addressed Dmitry's concerns. Deadlock checking no longer recursive.
571da177e4SLinus Torvalds  *  Lock allocation changed to GFP_ATOMIC as we can't afford to sleep
581da177e4SLinus Torvalds  *  once we've checked for blocking and deadlocking.
591da177e4SLinus Torvalds  *  Andy Walker (andy@lysaker.kvaerner.no), April 03, 1996.
601da177e4SLinus Torvalds  *
611da177e4SLinus Torvalds  *  Initial implementation of mandatory locks. SunOS turned out to be
621da177e4SLinus Torvalds  *  a rotten model, so I implemented the "obvious" semantics.
63395cf969SPaul Bolle  *  See 'Documentation/filesystems/mandatory-locking.txt' for details.
641da177e4SLinus Torvalds  *  Andy Walker (andy@lysaker.kvaerner.no), April 06, 1996.
651da177e4SLinus Torvalds  *
661da177e4SLinus Torvalds  *  Don't allow mandatory locks on mmap()'ed files. Added simple functions to
671da177e4SLinus Torvalds  *  check if a file has mandatory locks, used by mmap(), open() and creat() to
681da177e4SLinus Torvalds  *  see if system call should be rejected. Ref. HP-UX/SunOS/Solaris Reference
691da177e4SLinus Torvalds  *  Manual, Section 2.
701da177e4SLinus Torvalds  *  Andy Walker (andy@lysaker.kvaerner.no), April 09, 1996.
711da177e4SLinus Torvalds  *
721da177e4SLinus Torvalds  *  Tidied up block list handling. Added '/proc/locks' interface.
731da177e4SLinus Torvalds  *  Andy Walker (andy@lysaker.kvaerner.no), April 24, 1996.
741da177e4SLinus Torvalds  *
751da177e4SLinus Torvalds  *  Fixed deadlock condition for pathological code that mixes calls to
761da177e4SLinus Torvalds  *  flock() and fcntl().
771da177e4SLinus Torvalds  *  Andy Walker (andy@lysaker.kvaerner.no), April 29, 1996.
781da177e4SLinus Torvalds  *
791da177e4SLinus Torvalds  *  Allow only one type of locking scheme (FL_POSIX or FL_FLOCK) to be in use
801da177e4SLinus Torvalds  *  for a given file at a time. Changed the CONFIG_LOCK_MANDATORY scheme to
811da177e4SLinus Torvalds  *  guarantee sensible behaviour in the case where file system modules might
821da177e4SLinus Torvalds  *  be compiled with different options than the kernel itself.
831da177e4SLinus Torvalds  *  Andy Walker (andy@lysaker.kvaerner.no), May 15, 1996.
841da177e4SLinus Torvalds  *
851da177e4SLinus Torvalds  *  Added a couple of missing wake_up() calls. Thanks to Thomas Meckel
861da177e4SLinus Torvalds  *  (Thomas.Meckel@mni.fh-giessen.de) for spotting this.
871da177e4SLinus Torvalds  *  Andy Walker (andy@lysaker.kvaerner.no), May 15, 1996.
881da177e4SLinus Torvalds  *
891da177e4SLinus Torvalds  *  Changed FL_POSIX locks to use the block list in the same way as FL_FLOCK
901da177e4SLinus Torvalds  *  locks. Changed process synchronisation to avoid dereferencing locks that
911da177e4SLinus Torvalds  *  have already been freed.
921da177e4SLinus Torvalds  *  Andy Walker (andy@lysaker.kvaerner.no), Sep 21, 1996.
931da177e4SLinus Torvalds  *
941da177e4SLinus Torvalds  *  Made the block list a circular list to minimise searching in the list.
951da177e4SLinus Torvalds  *  Andy Walker (andy@lysaker.kvaerner.no), Sep 25, 1996.
961da177e4SLinus Torvalds  *
971da177e4SLinus Torvalds  *  Made mandatory locking a mount option. Default is not to allow mandatory
981da177e4SLinus Torvalds  *  locking.
991da177e4SLinus Torvalds  *  Andy Walker (andy@lysaker.kvaerner.no), Oct 04, 1996.
1001da177e4SLinus Torvalds  *
1011da177e4SLinus Torvalds  *  Some adaptations for NFS support.
1021da177e4SLinus Torvalds  *  Olaf Kirch (okir@monad.swb.de), Dec 1996,
1031da177e4SLinus Torvalds  *
1041da177e4SLinus Torvalds  *  Fixed /proc/locks interface so that we can't overrun the buffer we are handed.
1051da177e4SLinus Torvalds  *  Andy Walker (andy@lysaker.kvaerner.no), May 12, 1997.
1061da177e4SLinus Torvalds  *
1071da177e4SLinus Torvalds  *  Use slab allocator instead of kmalloc/kfree.
1081da177e4SLinus Torvalds  *  Use generic list implementation from <linux/list.h>.
1091da177e4SLinus Torvalds  *  Sped up posix_locks_deadlock by only considering blocked locks.
1101da177e4SLinus Torvalds  *  Matthew Wilcox <willy@debian.org>, March, 2000.
1111da177e4SLinus Torvalds  *
1121da177e4SLinus Torvalds  *  Leases and LOCK_MAND
1131da177e4SLinus Torvalds  *  Matthew Wilcox <willy@debian.org>, June, 2000.
1141da177e4SLinus Torvalds  *  Stephen Rothwell <sfr@canb.auug.org.au>, June, 2000.
1151da177e4SLinus Torvalds  */
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds #include <linux/capability.h>
1181da177e4SLinus Torvalds #include <linux/file.h>
1199f3acc31SAl Viro #include <linux/fdtable.h>
1201da177e4SLinus Torvalds #include <linux/fs.h>
1211da177e4SLinus Torvalds #include <linux/init.h>
1221da177e4SLinus Torvalds #include <linux/module.h>
1231da177e4SLinus Torvalds #include <linux/security.h>
1241da177e4SLinus Torvalds #include <linux/slab.h>
1251da177e4SLinus Torvalds #include <linux/syscalls.h>
1261da177e4SLinus Torvalds #include <linux/time.h>
1274fb3a538SDipankar Sarma #include <linux/rcupdate.h>
128ab1f1611SVitaliy Gusev #include <linux/pid_namespace.h>
12948f74186SJeff Layton #include <linux/hashtable.h>
1307012b02aSJeff Layton #include <linux/percpu.h>
1317012b02aSJeff Layton #include <linux/lglock.h>
1321da177e4SLinus Torvalds 
13362af4f1fSJeff Layton #define CREATE_TRACE_POINTS
13462af4f1fSJeff Layton #include <trace/events/filelock.h>
13562af4f1fSJeff Layton 
1361da177e4SLinus Torvalds #include <asm/uaccess.h>
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds #define IS_POSIX(fl)	(fl->fl_flags & FL_POSIX)
1391da177e4SLinus Torvalds #define IS_FLOCK(fl)	(fl->fl_flags & FL_FLOCK)
140617588d5SJ. Bruce Fields #define IS_LEASE(fl)	(fl->fl_flags & (FL_LEASE|FL_DELEG))
141cff2fce5SJeff Layton #define IS_OFDLCK(fl)	(fl->fl_flags & FL_OFDLCK)
1421da177e4SLinus Torvalds 
143ab83fa4bSJ. Bruce Fields static bool lease_breaking(struct file_lock *fl)
144ab83fa4bSJ. Bruce Fields {
145778fc546SJ. Bruce Fields 	return fl->fl_flags & (FL_UNLOCK_PENDING | FL_DOWNGRADE_PENDING);
146778fc546SJ. Bruce Fields }
147778fc546SJ. Bruce Fields 
148778fc546SJ. Bruce Fields static int target_leasetype(struct file_lock *fl)
149778fc546SJ. Bruce Fields {
150778fc546SJ. Bruce Fields 	if (fl->fl_flags & FL_UNLOCK_PENDING)
151778fc546SJ. Bruce Fields 		return F_UNLCK;
152778fc546SJ. Bruce Fields 	if (fl->fl_flags & FL_DOWNGRADE_PENDING)
153778fc546SJ. Bruce Fields 		return F_RDLCK;
154778fc546SJ. Bruce Fields 	return fl->fl_type;
155ab83fa4bSJ. Bruce Fields }
156ab83fa4bSJ. Bruce Fields 
1571da177e4SLinus Torvalds int leases_enable = 1;
1581da177e4SLinus Torvalds int lease_break_time = 45;
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds #define for_each_lock(inode, lockp) \
1611da177e4SLinus Torvalds 	for (lockp = &inode->i_flock; *lockp != NULL; lockp = &(*lockp)->fl_next)
1621da177e4SLinus Torvalds 
1631c8c601aSJeff Layton /*
1647012b02aSJeff Layton  * The global file_lock_list is only used for displaying /proc/locks, so we
1657012b02aSJeff Layton  * keep a list on each CPU, with each list protected by its own spinlock via
1667012b02aSJeff Layton  * the file_lock_lglock. Note that alterations to the list also require that
1677012b02aSJeff Layton  * the relevant i_lock is held.
1681c8c601aSJeff Layton  */
1697012b02aSJeff Layton DEFINE_STATIC_LGLOCK(file_lock_lglock);
1707012b02aSJeff Layton static DEFINE_PER_CPU(struct hlist_head, file_lock_list);
17188974691SJeff Layton 
1721c8c601aSJeff Layton /*
17348f74186SJeff Layton  * The blocked_hash is used to find POSIX lock loops for deadlock detection.
1747b2296afSJeff Layton  * It is protected by blocked_lock_lock.
17548f74186SJeff Layton  *
17648f74186SJeff Layton  * We hash locks by lockowner in order to optimize searching for the lock a
17748f74186SJeff Layton  * particular lockowner is waiting on.
17848f74186SJeff Layton  *
17948f74186SJeff Layton  * FIXME: make this value scale via some heuristic? We generally will want more
18048f74186SJeff Layton  * buckets when we have more lockowners holding locks, but that's a little
18148f74186SJeff Layton  * difficult to determine without knowing what the workload will look like.
1821c8c601aSJeff Layton  */
18348f74186SJeff Layton #define BLOCKED_HASH_BITS	7
18448f74186SJeff Layton static DEFINE_HASHTABLE(blocked_hash, BLOCKED_HASH_BITS);
18588974691SJeff Layton 
1861c8c601aSJeff Layton /*
1877b2296afSJeff Layton  * This lock protects the blocked_hash. Generally, if you're accessing it, you
1887b2296afSJeff Layton  * want to be holding this lock.
1891c8c601aSJeff Layton  *
1901c8c601aSJeff Layton  * In addition, it also protects the fl->fl_block list, and the fl->fl_next
1911c8c601aSJeff Layton  * pointer for file_lock structures that are acting as lock requests (in
1921c8c601aSJeff Layton  * contrast to those that are acting as records of acquired locks).
1931c8c601aSJeff Layton  *
1941c8c601aSJeff Layton  * Note that when we acquire this lock in order to change the above fields,
1951c8c601aSJeff Layton  * we often hold the i_lock as well. In certain cases, when reading the fields
1961c8c601aSJeff Layton  * protected by this lock, we can skip acquiring it iff we already hold the
1971c8c601aSJeff Layton  * i_lock.
1981c8c601aSJeff Layton  *
1991c8c601aSJeff Layton  * In particular, adding an entry to the fl_block list requires that you hold
2001c8c601aSJeff Layton  * both the i_lock and the blocked_lock_lock (acquired in that order). Deleting
2011c8c601aSJeff Layton  * an entry from the list however only requires the file_lock_lock.
2021c8c601aSJeff Layton  */
2037b2296afSJeff Layton static DEFINE_SPINLOCK(blocked_lock_lock);
2041da177e4SLinus Torvalds 
205e18b890bSChristoph Lameter static struct kmem_cache *filelock_cache __read_mostly;
2061da177e4SLinus Torvalds 
207ee19cc40SMiklos Szeredi static void locks_init_lock_heads(struct file_lock *fl)
208a51cb91dSMiklos Szeredi {
209139ca04eSJeff Layton 	INIT_HLIST_NODE(&fl->fl_link);
210ee19cc40SMiklos Szeredi 	INIT_LIST_HEAD(&fl->fl_block);
211ee19cc40SMiklos Szeredi 	init_waitqueue_head(&fl->fl_wait);
212a51cb91dSMiklos Szeredi }
213a51cb91dSMiklos Szeredi 
2141da177e4SLinus Torvalds /* Allocate an empty lock structure. */
215c5b1f0d9SArnd Bergmann struct file_lock *locks_alloc_lock(void)
2161da177e4SLinus Torvalds {
217ee19cc40SMiklos Szeredi 	struct file_lock *fl = kmem_cache_zalloc(filelock_cache, GFP_KERNEL);
218a51cb91dSMiklos Szeredi 
219a51cb91dSMiklos Szeredi 	if (fl)
220ee19cc40SMiklos Szeredi 		locks_init_lock_heads(fl);
221a51cb91dSMiklos Szeredi 
222a51cb91dSMiklos Szeredi 	return fl;
2231da177e4SLinus Torvalds }
224c5b1f0d9SArnd Bergmann EXPORT_SYMBOL_GPL(locks_alloc_lock);
2251da177e4SLinus Torvalds 
226a9e61e25SFelix Blyakher void locks_release_private(struct file_lock *fl)
22747831f35STrond Myklebust {
22847831f35STrond Myklebust 	if (fl->fl_ops) {
22947831f35STrond Myklebust 		if (fl->fl_ops->fl_release_private)
23047831f35STrond Myklebust 			fl->fl_ops->fl_release_private(fl);
23147831f35STrond Myklebust 		fl->fl_ops = NULL;
23247831f35STrond Myklebust 	}
23347831f35STrond Myklebust 
2345c97d7b1SKinglong Mee 	if (fl->fl_lmops) {
2355c97d7b1SKinglong Mee 		if (fl->fl_lmops->lm_put_owner)
2365c97d7b1SKinglong Mee 			fl->fl_lmops->lm_put_owner(fl);
2375c97d7b1SKinglong Mee 		fl->fl_lmops = NULL;
2385c97d7b1SKinglong Mee 	}
23947831f35STrond Myklebust }
240a9e61e25SFelix Blyakher EXPORT_SYMBOL_GPL(locks_release_private);
24147831f35STrond Myklebust 
2421da177e4SLinus Torvalds /* Free a lock which is not in use. */
24305fa3135SJ. Bruce Fields void locks_free_lock(struct file_lock *fl)
2441da177e4SLinus Torvalds {
2455ce29646SMiklos Szeredi 	BUG_ON(waitqueue_active(&fl->fl_wait));
2465ce29646SMiklos Szeredi 	BUG_ON(!list_empty(&fl->fl_block));
247139ca04eSJeff Layton 	BUG_ON(!hlist_unhashed(&fl->fl_link));
2481da177e4SLinus Torvalds 
24947831f35STrond Myklebust 	locks_release_private(fl);
2501da177e4SLinus Torvalds 	kmem_cache_free(filelock_cache, fl);
2511da177e4SLinus Torvalds }
25205fa3135SJ. Bruce Fields EXPORT_SYMBOL(locks_free_lock);
2531da177e4SLinus Torvalds 
254ed9814d8SJeff Layton static void
255ed9814d8SJeff Layton locks_dispose_list(struct list_head *dispose)
256ed9814d8SJeff Layton {
257ed9814d8SJeff Layton 	struct file_lock *fl;
258ed9814d8SJeff Layton 
259ed9814d8SJeff Layton 	while (!list_empty(dispose)) {
260ed9814d8SJeff Layton 		fl = list_first_entry(dispose, struct file_lock, fl_block);
261ed9814d8SJeff Layton 		list_del_init(&fl->fl_block);
262ed9814d8SJeff Layton 		locks_free_lock(fl);
263ed9814d8SJeff Layton 	}
264ed9814d8SJeff Layton }
265ed9814d8SJeff Layton 
2661da177e4SLinus Torvalds void locks_init_lock(struct file_lock *fl)
2671da177e4SLinus Torvalds {
268ee19cc40SMiklos Szeredi 	memset(fl, 0, sizeof(struct file_lock));
269ee19cc40SMiklos Szeredi 	locks_init_lock_heads(fl);
2701da177e4SLinus Torvalds }
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds EXPORT_SYMBOL(locks_init_lock);
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds /*
2751da177e4SLinus Torvalds  * Initialize a new lock from an existing file_lock structure.
2761da177e4SLinus Torvalds  */
2773fe0fff1SKinglong Mee void locks_copy_conflock(struct file_lock *new, struct file_lock *fl)
2781da177e4SLinus Torvalds {
2791da177e4SLinus Torvalds 	new->fl_owner = fl->fl_owner;
2801da177e4SLinus Torvalds 	new->fl_pid = fl->fl_pid;
2810996905fSTrond Myklebust 	new->fl_file = NULL;
2821da177e4SLinus Torvalds 	new->fl_flags = fl->fl_flags;
2831da177e4SLinus Torvalds 	new->fl_type = fl->fl_type;
2841da177e4SLinus Torvalds 	new->fl_start = fl->fl_start;
2851da177e4SLinus Torvalds 	new->fl_end = fl->fl_end;
286f328296eSKinglong Mee 	new->fl_lmops = fl->fl_lmops;
2870996905fSTrond Myklebust 	new->fl_ops = NULL;
288f328296eSKinglong Mee 
289f328296eSKinglong Mee 	if (fl->fl_lmops) {
290f328296eSKinglong Mee 		if (fl->fl_lmops->lm_get_owner)
291f328296eSKinglong Mee 			fl->fl_lmops->lm_get_owner(new, fl);
292f328296eSKinglong Mee 	}
2930996905fSTrond Myklebust }
2943fe0fff1SKinglong Mee EXPORT_SYMBOL(locks_copy_conflock);
2950996905fSTrond Myklebust 
2960996905fSTrond Myklebust void locks_copy_lock(struct file_lock *new, struct file_lock *fl)
2970996905fSTrond Myklebust {
298566709bdSJeff Layton 	/* "new" must be a freshly-initialized lock */
299566709bdSJeff Layton 	WARN_ON_ONCE(new->fl_ops);
3000996905fSTrond Myklebust 
3013fe0fff1SKinglong Mee 	locks_copy_conflock(new, fl);
302f328296eSKinglong Mee 
3030996905fSTrond Myklebust 	new->fl_file = fl->fl_file;
3041da177e4SLinus Torvalds 	new->fl_ops = fl->fl_ops;
30547831f35STrond Myklebust 
306f328296eSKinglong Mee 	if (fl->fl_ops) {
307f328296eSKinglong Mee 		if (fl->fl_ops->fl_copy_lock)
308f328296eSKinglong Mee 			fl->fl_ops->fl_copy_lock(new, fl);
309f328296eSKinglong Mee 	}
3101da177e4SLinus Torvalds }
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds EXPORT_SYMBOL(locks_copy_lock);
3131da177e4SLinus Torvalds 
3141da177e4SLinus Torvalds static inline int flock_translate_cmd(int cmd) {
3151da177e4SLinus Torvalds 	if (cmd & LOCK_MAND)
3161da177e4SLinus Torvalds 		return cmd & (LOCK_MAND | LOCK_RW);
3171da177e4SLinus Torvalds 	switch (cmd) {
3181da177e4SLinus Torvalds 	case LOCK_SH:
3191da177e4SLinus Torvalds 		return F_RDLCK;
3201da177e4SLinus Torvalds 	case LOCK_EX:
3211da177e4SLinus Torvalds 		return F_WRLCK;
3221da177e4SLinus Torvalds 	case LOCK_UN:
3231da177e4SLinus Torvalds 		return F_UNLCK;
3241da177e4SLinus Torvalds 	}
3251da177e4SLinus Torvalds 	return -EINVAL;
3261da177e4SLinus Torvalds }
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds /* Fill in a file_lock structure with an appropriate FLOCK lock. */
3291da177e4SLinus Torvalds static int flock_make_lock(struct file *filp, struct file_lock **lock,
3301da177e4SLinus Torvalds 		unsigned int cmd)
3311da177e4SLinus Torvalds {
3321da177e4SLinus Torvalds 	struct file_lock *fl;
3331da177e4SLinus Torvalds 	int type = flock_translate_cmd(cmd);
3341da177e4SLinus Torvalds 	if (type < 0)
3351da177e4SLinus Torvalds 		return type;
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds 	fl = locks_alloc_lock();
3381da177e4SLinus Torvalds 	if (fl == NULL)
3391da177e4SLinus Torvalds 		return -ENOMEM;
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds 	fl->fl_file = filp;
34273a8f5f7SChristoph Hellwig 	fl->fl_owner = filp;
3431da177e4SLinus Torvalds 	fl->fl_pid = current->tgid;
3441da177e4SLinus Torvalds 	fl->fl_flags = FL_FLOCK;
3451da177e4SLinus Torvalds 	fl->fl_type = type;
3461da177e4SLinus Torvalds 	fl->fl_end = OFFSET_MAX;
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds 	*lock = fl;
3491da177e4SLinus Torvalds 	return 0;
3501da177e4SLinus Torvalds }
3511da177e4SLinus Torvalds 
3520ec4f431SJ. Bruce Fields static int assign_type(struct file_lock *fl, long type)
3531da177e4SLinus Torvalds {
3541da177e4SLinus Torvalds 	switch (type) {
3551da177e4SLinus Torvalds 	case F_RDLCK:
3561da177e4SLinus Torvalds 	case F_WRLCK:
3571da177e4SLinus Torvalds 	case F_UNLCK:
3581da177e4SLinus Torvalds 		fl->fl_type = type;
3591da177e4SLinus Torvalds 		break;
3601da177e4SLinus Torvalds 	default:
3611da177e4SLinus Torvalds 		return -EINVAL;
3621da177e4SLinus Torvalds 	}
3631da177e4SLinus Torvalds 	return 0;
3641da177e4SLinus Torvalds }
3651da177e4SLinus Torvalds 
366ef12e72aSJ. Bruce Fields static int flock64_to_posix_lock(struct file *filp, struct file_lock *fl,
367ef12e72aSJ. Bruce Fields 				 struct flock64 *l)
368ef12e72aSJ. Bruce Fields {
369ef12e72aSJ. Bruce Fields 	switch (l->l_whence) {
370ef12e72aSJ. Bruce Fields 	case SEEK_SET:
371ef12e72aSJ. Bruce Fields 		fl->fl_start = 0;
372ef12e72aSJ. Bruce Fields 		break;
373ef12e72aSJ. Bruce Fields 	case SEEK_CUR:
374ef12e72aSJ. Bruce Fields 		fl->fl_start = filp->f_pos;
375ef12e72aSJ. Bruce Fields 		break;
376ef12e72aSJ. Bruce Fields 	case SEEK_END:
377ef12e72aSJ. Bruce Fields 		fl->fl_start = i_size_read(file_inode(filp));
378ef12e72aSJ. Bruce Fields 		break;
379ef12e72aSJ. Bruce Fields 	default:
380ef12e72aSJ. Bruce Fields 		return -EINVAL;
381ef12e72aSJ. Bruce Fields 	}
382ef12e72aSJ. Bruce Fields 	if (l->l_start > OFFSET_MAX - fl->fl_start)
383ef12e72aSJ. Bruce Fields 		return -EOVERFLOW;
384ef12e72aSJ. Bruce Fields 	fl->fl_start += l->l_start;
385ef12e72aSJ. Bruce Fields 	if (fl->fl_start < 0)
386ef12e72aSJ. Bruce Fields 		return -EINVAL;
387ef12e72aSJ. Bruce Fields 
388ef12e72aSJ. Bruce Fields 	/* POSIX-1996 leaves the case l->l_len < 0 undefined;
389ef12e72aSJ. Bruce Fields 	   POSIX-2001 defines it. */
390ef12e72aSJ. Bruce Fields 	if (l->l_len > 0) {
391ef12e72aSJ. Bruce Fields 		if (l->l_len - 1 > OFFSET_MAX - fl->fl_start)
392ef12e72aSJ. Bruce Fields 			return -EOVERFLOW;
393ef12e72aSJ. Bruce Fields 		fl->fl_end = fl->fl_start + l->l_len - 1;
394ef12e72aSJ. Bruce Fields 
395ef12e72aSJ. Bruce Fields 	} else if (l->l_len < 0) {
396ef12e72aSJ. Bruce Fields 		if (fl->fl_start + l->l_len < 0)
397ef12e72aSJ. Bruce Fields 			return -EINVAL;
398ef12e72aSJ. Bruce Fields 		fl->fl_end = fl->fl_start - 1;
399ef12e72aSJ. Bruce Fields 		fl->fl_start += l->l_len;
400ef12e72aSJ. Bruce Fields 	} else
401ef12e72aSJ. Bruce Fields 		fl->fl_end = OFFSET_MAX;
402ef12e72aSJ. Bruce Fields 
403ef12e72aSJ. Bruce Fields 	fl->fl_owner = current->files;
404ef12e72aSJ. Bruce Fields 	fl->fl_pid = current->tgid;
405ef12e72aSJ. Bruce Fields 	fl->fl_file = filp;
406ef12e72aSJ. Bruce Fields 	fl->fl_flags = FL_POSIX;
407ef12e72aSJ. Bruce Fields 	fl->fl_ops = NULL;
408ef12e72aSJ. Bruce Fields 	fl->fl_lmops = NULL;
409ef12e72aSJ. Bruce Fields 
410ef12e72aSJ. Bruce Fields 	return assign_type(fl, l->l_type);
411ef12e72aSJ. Bruce Fields }
412ef12e72aSJ. Bruce Fields 
4131da177e4SLinus Torvalds /* Verify a "struct flock" and copy it to a "struct file_lock" as a POSIX
4141da177e4SLinus Torvalds  * style lock.
4151da177e4SLinus Torvalds  */
4161da177e4SLinus Torvalds static int flock_to_posix_lock(struct file *filp, struct file_lock *fl,
4171da177e4SLinus Torvalds 			       struct flock *l)
4181da177e4SLinus Torvalds {
419ef12e72aSJ. Bruce Fields 	struct flock64 ll = {
420ef12e72aSJ. Bruce Fields 		.l_type = l->l_type,
421ef12e72aSJ. Bruce Fields 		.l_whence = l->l_whence,
422ef12e72aSJ. Bruce Fields 		.l_start = l->l_start,
423ef12e72aSJ. Bruce Fields 		.l_len = l->l_len,
424ef12e72aSJ. Bruce Fields 	};
4251da177e4SLinus Torvalds 
426ef12e72aSJ. Bruce Fields 	return flock64_to_posix_lock(filp, fl, &ll);
4271da177e4SLinus Torvalds }
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds /* default lease lock manager operations */
4301da177e4SLinus Torvalds static void lease_break_callback(struct file_lock *fl)
4311da177e4SLinus Torvalds {
4321da177e4SLinus Torvalds 	kill_fasync(&fl->fl_fasync, SIGIO, POLL_MSG);
4331da177e4SLinus Torvalds }
4341da177e4SLinus Torvalds 
4357b021967SAlexey Dobriyan static const struct lock_manager_operations lease_manager_ops = {
4368fb47a4fSJ. Bruce Fields 	.lm_break = lease_break_callback,
4378fb47a4fSJ. Bruce Fields 	.lm_change = lease_modify,
4381da177e4SLinus Torvalds };
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds /*
4411da177e4SLinus Torvalds  * Initialize a lease, use the default lock manager operations
4421da177e4SLinus Torvalds  */
4430ec4f431SJ. Bruce Fields static int lease_init(struct file *filp, long type, struct file_lock *fl)
4441da177e4SLinus Torvalds  {
44575dff55aSTrond Myklebust 	if (assign_type(fl, type) != 0)
44675dff55aSTrond Myklebust 		return -EINVAL;
44775dff55aSTrond Myklebust 
44873a8f5f7SChristoph Hellwig 	fl->fl_owner = current->files;
4491da177e4SLinus Torvalds 	fl->fl_pid = current->tgid;
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds 	fl->fl_file = filp;
4521da177e4SLinus Torvalds 	fl->fl_flags = FL_LEASE;
4531da177e4SLinus Torvalds 	fl->fl_start = 0;
4541da177e4SLinus Torvalds 	fl->fl_end = OFFSET_MAX;
4551da177e4SLinus Torvalds 	fl->fl_ops = NULL;
4561da177e4SLinus Torvalds 	fl->fl_lmops = &lease_manager_ops;
4571da177e4SLinus Torvalds 	return 0;
4581da177e4SLinus Torvalds }
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds /* Allocate a file_lock initialised to this type of lease */
4610ec4f431SJ. Bruce Fields static struct file_lock *lease_alloc(struct file *filp, long type)
4621da177e4SLinus Torvalds {
4631da177e4SLinus Torvalds 	struct file_lock *fl = locks_alloc_lock();
46475dff55aSTrond Myklebust 	int error = -ENOMEM;
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds 	if (fl == NULL)
467e32b8ee2SJ. Bruce Fields 		return ERR_PTR(error);
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds 	error = lease_init(filp, type, fl);
47075dff55aSTrond Myklebust 	if (error) {
47175dff55aSTrond Myklebust 		locks_free_lock(fl);
472e32b8ee2SJ. Bruce Fields 		return ERR_PTR(error);
47375dff55aSTrond Myklebust 	}
474e32b8ee2SJ. Bruce Fields 	return fl;
4751da177e4SLinus Torvalds }
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds /* Check if two locks overlap each other.
4781da177e4SLinus Torvalds  */
4791da177e4SLinus Torvalds static inline int locks_overlap(struct file_lock *fl1, struct file_lock *fl2)
4801da177e4SLinus Torvalds {
4811da177e4SLinus Torvalds 	return ((fl1->fl_end >= fl2->fl_start) &&
4821da177e4SLinus Torvalds 		(fl2->fl_end >= fl1->fl_start));
4831da177e4SLinus Torvalds }
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds /*
4861da177e4SLinus Torvalds  * Check whether two locks have the same owner.
4871da177e4SLinus Torvalds  */
48833443c42SMatt Mackall static int posix_same_owner(struct file_lock *fl1, struct file_lock *fl2)
4891da177e4SLinus Torvalds {
4908fb47a4fSJ. Bruce Fields 	if (fl1->fl_lmops && fl1->fl_lmops->lm_compare_owner)
4911da177e4SLinus Torvalds 		return fl2->fl_lmops == fl1->fl_lmops &&
4928fb47a4fSJ. Bruce Fields 			fl1->fl_lmops->lm_compare_owner(fl1, fl2);
4931da177e4SLinus Torvalds 	return fl1->fl_owner == fl2->fl_owner;
4941da177e4SLinus Torvalds }
4951da177e4SLinus Torvalds 
4967012b02aSJeff Layton /* Must be called with the i_lock held! */
4976ca10ed8SJeff Layton static void locks_insert_global_locks(struct file_lock *fl)
49888974691SJeff Layton {
4997012b02aSJeff Layton 	lg_local_lock(&file_lock_lglock);
5007012b02aSJeff Layton 	fl->fl_link_cpu = smp_processor_id();
5017012b02aSJeff Layton 	hlist_add_head(&fl->fl_link, this_cpu_ptr(&file_lock_list));
5027012b02aSJeff Layton 	lg_local_unlock(&file_lock_lglock);
50388974691SJeff Layton }
50488974691SJeff Layton 
5057012b02aSJeff Layton /* Must be called with the i_lock held! */
5066ca10ed8SJeff Layton static void locks_delete_global_locks(struct file_lock *fl)
50788974691SJeff Layton {
5087012b02aSJeff Layton 	/*
5097012b02aSJeff Layton 	 * Avoid taking lock if already unhashed. This is safe since this check
5107012b02aSJeff Layton 	 * is done while holding the i_lock, and new insertions into the list
5117012b02aSJeff Layton 	 * also require that it be held.
5127012b02aSJeff Layton 	 */
5137012b02aSJeff Layton 	if (hlist_unhashed(&fl->fl_link))
5147012b02aSJeff Layton 		return;
5157012b02aSJeff Layton 	lg_local_lock_cpu(&file_lock_lglock, fl->fl_link_cpu);
516139ca04eSJeff Layton 	hlist_del_init(&fl->fl_link);
5177012b02aSJeff Layton 	lg_local_unlock_cpu(&file_lock_lglock, fl->fl_link_cpu);
51888974691SJeff Layton }
51988974691SJeff Layton 
5203999e493SJeff Layton static unsigned long
5213999e493SJeff Layton posix_owner_key(struct file_lock *fl)
5223999e493SJeff Layton {
5233999e493SJeff Layton 	if (fl->fl_lmops && fl->fl_lmops->lm_owner_key)
5243999e493SJeff Layton 		return fl->fl_lmops->lm_owner_key(fl);
5253999e493SJeff Layton 	return (unsigned long)fl->fl_owner;
5263999e493SJeff Layton }
5273999e493SJeff Layton 
5286ca10ed8SJeff Layton static void locks_insert_global_blocked(struct file_lock *waiter)
52988974691SJeff Layton {
5303999e493SJeff Layton 	hash_add(blocked_hash, &waiter->fl_link, posix_owner_key(waiter));
53188974691SJeff Layton }
53288974691SJeff Layton 
5336ca10ed8SJeff Layton static void locks_delete_global_blocked(struct file_lock *waiter)
53488974691SJeff Layton {
53548f74186SJeff Layton 	hash_del(&waiter->fl_link);
53688974691SJeff Layton }
53788974691SJeff Layton 
5381da177e4SLinus Torvalds /* Remove waiter from blocker's block list.
5391da177e4SLinus Torvalds  * When blocker ends up pointing to itself then the list is empty.
5401c8c601aSJeff Layton  *
5417b2296afSJeff Layton  * Must be called with blocked_lock_lock held.
5421da177e4SLinus Torvalds  */
54333443c42SMatt Mackall static void __locks_delete_block(struct file_lock *waiter)
5441da177e4SLinus Torvalds {
54588974691SJeff Layton 	locks_delete_global_blocked(waiter);
5461da177e4SLinus Torvalds 	list_del_init(&waiter->fl_block);
5471da177e4SLinus Torvalds 	waiter->fl_next = NULL;
5481da177e4SLinus Torvalds }
5491da177e4SLinus Torvalds 
5501a9e64a7SJeff Layton static void locks_delete_block(struct file_lock *waiter)
5511da177e4SLinus Torvalds {
5527b2296afSJeff Layton 	spin_lock(&blocked_lock_lock);
5531da177e4SLinus Torvalds 	__locks_delete_block(waiter);
5547b2296afSJeff Layton 	spin_unlock(&blocked_lock_lock);
5551da177e4SLinus Torvalds }
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds /* Insert waiter into blocker's block list.
5581da177e4SLinus Torvalds  * We use a circular list so that processes can be easily woken up in
5591da177e4SLinus Torvalds  * the order they blocked. The documentation doesn't require this but
5601da177e4SLinus Torvalds  * it seems like the reasonable thing to do.
5611c8c601aSJeff Layton  *
5627b2296afSJeff Layton  * Must be called with both the i_lock and blocked_lock_lock held. The fl_block
56346dad760SJeff Layton  * list itself is protected by the blocked_lock_lock, but by ensuring that the
5647b2296afSJeff Layton  * i_lock is also held on insertions we can avoid taking the blocked_lock_lock
5654e8c765dSJeff Layton  * in some cases when we see that the fl_block list is empty.
5661da177e4SLinus Torvalds  */
5671c8c601aSJeff Layton static void __locks_insert_block(struct file_lock *blocker,
5681da177e4SLinus Torvalds 					struct file_lock *waiter)
5691da177e4SLinus Torvalds {
5706dc0fe8fSJ. Bruce Fields 	BUG_ON(!list_empty(&waiter->fl_block));
5711da177e4SLinus Torvalds 	waiter->fl_next = blocker;
57288974691SJeff Layton 	list_add_tail(&waiter->fl_block, &blocker->fl_block);
573cff2fce5SJeff Layton 	if (IS_POSIX(blocker) && !IS_OFDLCK(blocker))
5741c8c601aSJeff Layton 		locks_insert_global_blocked(waiter);
5751c8c601aSJeff Layton }
5761c8c601aSJeff Layton 
5771c8c601aSJeff Layton /* Must be called with i_lock held. */
5781c8c601aSJeff Layton static void locks_insert_block(struct file_lock *blocker,
5791c8c601aSJeff Layton 					struct file_lock *waiter)
5801c8c601aSJeff Layton {
5817b2296afSJeff Layton 	spin_lock(&blocked_lock_lock);
5821c8c601aSJeff Layton 	__locks_insert_block(blocker, waiter);
5837b2296afSJeff Layton 	spin_unlock(&blocked_lock_lock);
5841da177e4SLinus Torvalds }
5851da177e4SLinus Torvalds 
5861cb36012SJeff Layton /*
5871cb36012SJeff Layton  * Wake up processes blocked waiting for blocker.
5881cb36012SJeff Layton  *
5891c8c601aSJeff Layton  * Must be called with the inode->i_lock held!
5901da177e4SLinus Torvalds  */
5911da177e4SLinus Torvalds static void locks_wake_up_blocks(struct file_lock *blocker)
5921da177e4SLinus Torvalds {
5934e8c765dSJeff Layton 	/*
5944e8c765dSJeff Layton 	 * Avoid taking global lock if list is empty. This is safe since new
5954e8c765dSJeff Layton 	 * blocked requests are only added to the list under the i_lock, and
5964e8c765dSJeff Layton 	 * the i_lock is always held here. Note that removal from the fl_block
5974e8c765dSJeff Layton 	 * list does not require the i_lock, so we must recheck list_empty()
5987b2296afSJeff Layton 	 * after acquiring the blocked_lock_lock.
5994e8c765dSJeff Layton 	 */
6004e8c765dSJeff Layton 	if (list_empty(&blocker->fl_block))
6014e8c765dSJeff Layton 		return;
6024e8c765dSJeff Layton 
6037b2296afSJeff Layton 	spin_lock(&blocked_lock_lock);
6041da177e4SLinus Torvalds 	while (!list_empty(&blocker->fl_block)) {
605f0c1cd0eSPavel Emelyanov 		struct file_lock *waiter;
606f0c1cd0eSPavel Emelyanov 
607f0c1cd0eSPavel Emelyanov 		waiter = list_first_entry(&blocker->fl_block,
6081da177e4SLinus Torvalds 				struct file_lock, fl_block);
6091da177e4SLinus Torvalds 		__locks_delete_block(waiter);
6108fb47a4fSJ. Bruce Fields 		if (waiter->fl_lmops && waiter->fl_lmops->lm_notify)
6118fb47a4fSJ. Bruce Fields 			waiter->fl_lmops->lm_notify(waiter);
6121da177e4SLinus Torvalds 		else
6131da177e4SLinus Torvalds 			wake_up(&waiter->fl_wait);
6141da177e4SLinus Torvalds 	}
6157b2296afSJeff Layton 	spin_unlock(&blocked_lock_lock);
6161da177e4SLinus Torvalds }
6171da177e4SLinus Torvalds 
6181da177e4SLinus Torvalds /* Insert file lock fl into an inode's lock list at the position indicated
6191da177e4SLinus Torvalds  * by pos. At the same time add the lock to the global file lock list.
6201c8c601aSJeff Layton  *
6211c8c601aSJeff Layton  * Must be called with the i_lock held!
6221da177e4SLinus Torvalds  */
6231da177e4SLinus Torvalds static void locks_insert_lock(struct file_lock **pos, struct file_lock *fl)
6241da177e4SLinus Torvalds {
625ab1f1611SVitaliy Gusev 	fl->fl_nspid = get_pid(task_tgid(current));
626ab1f1611SVitaliy Gusev 
6271da177e4SLinus Torvalds 	/* insert into file's list */
6281da177e4SLinus Torvalds 	fl->fl_next = *pos;
6291da177e4SLinus Torvalds 	*pos = fl;
63088974691SJeff Layton 
63188974691SJeff Layton 	locks_insert_global_locks(fl);
6321da177e4SLinus Torvalds }
6331da177e4SLinus Torvalds 
63424cbe784SJeff Layton /**
63524cbe784SJeff Layton  * locks_delete_lock - Delete a lock and then free it.
63624cbe784SJeff Layton  * @thisfl_p: pointer that points to the fl_next field of the previous
63724cbe784SJeff Layton  * 	      inode->i_flock list entry
63824cbe784SJeff Layton  *
63924cbe784SJeff Layton  * Unlink a lock from all lists and free the namespace reference, but don't
64024cbe784SJeff Layton  * free it yet. Wake up processes that are blocked waiting for this lock and
64124cbe784SJeff Layton  * notify the FS that the lock has been cleared.
6421c8c601aSJeff Layton  *
6431c8c601aSJeff Layton  * Must be called with the i_lock held!
6441da177e4SLinus Torvalds  */
64524cbe784SJeff Layton static void locks_unlink_lock(struct file_lock **thisfl_p)
6461da177e4SLinus Torvalds {
6471da177e4SLinus Torvalds 	struct file_lock *fl = *thisfl_p;
6481da177e4SLinus Torvalds 
64988974691SJeff Layton 	locks_delete_global_locks(fl);
65088974691SJeff Layton 
6511da177e4SLinus Torvalds 	*thisfl_p = fl->fl_next;
6521da177e4SLinus Torvalds 	fl->fl_next = NULL;
6531da177e4SLinus Torvalds 
654ab1f1611SVitaliy Gusev 	if (fl->fl_nspid) {
655ab1f1611SVitaliy Gusev 		put_pid(fl->fl_nspid);
656ab1f1611SVitaliy Gusev 		fl->fl_nspid = NULL;
657ab1f1611SVitaliy Gusev 	}
658ab1f1611SVitaliy Gusev 
6591da177e4SLinus Torvalds 	locks_wake_up_blocks(fl);
66024cbe784SJeff Layton }
66124cbe784SJeff Layton 
66224cbe784SJeff Layton /*
66324cbe784SJeff Layton  * Unlink a lock from all lists and free it.
66424cbe784SJeff Layton  *
66524cbe784SJeff Layton  * Must be called with i_lock held!
66624cbe784SJeff Layton  */
667ed9814d8SJeff Layton static void locks_delete_lock(struct file_lock **thisfl_p,
668ed9814d8SJeff Layton 			      struct list_head *dispose)
66924cbe784SJeff Layton {
67024cbe784SJeff Layton 	struct file_lock *fl = *thisfl_p;
67124cbe784SJeff Layton 
67224cbe784SJeff Layton 	locks_unlink_lock(thisfl_p);
673ed9814d8SJeff Layton 	if (dispose)
674ed9814d8SJeff Layton 		list_add(&fl->fl_block, dispose);
675ed9814d8SJeff Layton 	else
6761da177e4SLinus Torvalds 		locks_free_lock(fl);
6771da177e4SLinus Torvalds }
6781da177e4SLinus Torvalds 
6791da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. Common functionality
6801da177e4SLinus Torvalds  * checks for shared/exclusive status of overlapping locks.
6811da177e4SLinus Torvalds  */
6821da177e4SLinus Torvalds static int locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
6831da177e4SLinus Torvalds {
6841da177e4SLinus Torvalds 	if (sys_fl->fl_type == F_WRLCK)
6851da177e4SLinus Torvalds 		return 1;
6861da177e4SLinus Torvalds 	if (caller_fl->fl_type == F_WRLCK)
6871da177e4SLinus Torvalds 		return 1;
6881da177e4SLinus Torvalds 	return 0;
6891da177e4SLinus Torvalds }
6901da177e4SLinus Torvalds 
6911da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. POSIX specific
6921da177e4SLinus Torvalds  * checking before calling the locks_conflict().
6931da177e4SLinus Torvalds  */
6941da177e4SLinus Torvalds static int posix_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
6951da177e4SLinus Torvalds {
6961da177e4SLinus Torvalds 	/* POSIX locks owned by the same process do not conflict with
6971da177e4SLinus Torvalds 	 * each other.
6981da177e4SLinus Torvalds 	 */
6991da177e4SLinus Torvalds 	if (!IS_POSIX(sys_fl) || posix_same_owner(caller_fl, sys_fl))
7001da177e4SLinus Torvalds 		return (0);
7011da177e4SLinus Torvalds 
7021da177e4SLinus Torvalds 	/* Check whether they overlap */
7031da177e4SLinus Torvalds 	if (!locks_overlap(caller_fl, sys_fl))
7041da177e4SLinus Torvalds 		return 0;
7051da177e4SLinus Torvalds 
7061da177e4SLinus Torvalds 	return (locks_conflict(caller_fl, sys_fl));
7071da177e4SLinus Torvalds }
7081da177e4SLinus Torvalds 
7091da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. FLOCK specific
7101da177e4SLinus Torvalds  * checking before calling the locks_conflict().
7111da177e4SLinus Torvalds  */
7121da177e4SLinus Torvalds static int flock_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
7131da177e4SLinus Torvalds {
7141da177e4SLinus Torvalds 	/* FLOCK locks referring to the same filp do not conflict with
7151da177e4SLinus Torvalds 	 * each other.
7161da177e4SLinus Torvalds 	 */
7171da177e4SLinus Torvalds 	if (!IS_FLOCK(sys_fl) || (caller_fl->fl_file == sys_fl->fl_file))
7181da177e4SLinus Torvalds 		return (0);
7191da177e4SLinus Torvalds 	if ((caller_fl->fl_type & LOCK_MAND) || (sys_fl->fl_type & LOCK_MAND))
7201da177e4SLinus Torvalds 		return 0;
7211da177e4SLinus Torvalds 
7221da177e4SLinus Torvalds 	return (locks_conflict(caller_fl, sys_fl));
7231da177e4SLinus Torvalds }
7241da177e4SLinus Torvalds 
7256d34ac19SJ. Bruce Fields void
7269d6a8c5cSMarc Eshel posix_test_lock(struct file *filp, struct file_lock *fl)
7271da177e4SLinus Torvalds {
7281da177e4SLinus Torvalds 	struct file_lock *cfl;
7291c8c601aSJeff Layton 	struct inode *inode = file_inode(filp);
7301da177e4SLinus Torvalds 
7311c8c601aSJeff Layton 	spin_lock(&inode->i_lock);
732496ad9aaSAl Viro 	for (cfl = file_inode(filp)->i_flock; cfl; cfl = cfl->fl_next) {
7331da177e4SLinus Torvalds 		if (!IS_POSIX(cfl))
7341da177e4SLinus Torvalds 			continue;
735b842e240SJ. Bruce Fields 		if (posix_locks_conflict(fl, cfl))
7361da177e4SLinus Torvalds 			break;
7371da177e4SLinus Torvalds 	}
738ab1f1611SVitaliy Gusev 	if (cfl) {
7393fe0fff1SKinglong Mee 		locks_copy_conflock(fl, cfl);
740ab1f1611SVitaliy Gusev 		if (cfl->fl_nspid)
7416c5f3e7bSPavel Emelyanov 			fl->fl_pid = pid_vnr(cfl->fl_nspid);
742ab1f1611SVitaliy Gusev 	} else
743129a84deSJ. Bruce Fields 		fl->fl_type = F_UNLCK;
7441c8c601aSJeff Layton 	spin_unlock(&inode->i_lock);
7456d34ac19SJ. Bruce Fields 	return;
7461da177e4SLinus Torvalds }
7471da177e4SLinus Torvalds EXPORT_SYMBOL(posix_test_lock);
7481da177e4SLinus Torvalds 
749b533184fSJ. Bruce Fields /*
750b533184fSJ. Bruce Fields  * Deadlock detection:
7511da177e4SLinus Torvalds  *
752b533184fSJ. Bruce Fields  * We attempt to detect deadlocks that are due purely to posix file
753b533184fSJ. Bruce Fields  * locks.
7541da177e4SLinus Torvalds  *
755b533184fSJ. Bruce Fields  * We assume that a task can be waiting for at most one lock at a time.
756b533184fSJ. Bruce Fields  * So for any acquired lock, the process holding that lock may be
757b533184fSJ. Bruce Fields  * waiting on at most one other lock.  That lock in turns may be held by
758b533184fSJ. Bruce Fields  * someone waiting for at most one other lock.  Given a requested lock
759b533184fSJ. Bruce Fields  * caller_fl which is about to wait for a conflicting lock block_fl, we
760b533184fSJ. Bruce Fields  * follow this chain of waiters to ensure we are not about to create a
761b533184fSJ. Bruce Fields  * cycle.
76297855b49SJ. Bruce Fields  *
763b533184fSJ. Bruce Fields  * Since we do this before we ever put a process to sleep on a lock, we
764b533184fSJ. Bruce Fields  * are ensured that there is never a cycle; that is what guarantees that
765b533184fSJ. Bruce Fields  * the while() loop in posix_locks_deadlock() eventually completes.
766b533184fSJ. Bruce Fields  *
767b533184fSJ. Bruce Fields  * Note: the above assumption may not be true when handling lock
768b533184fSJ. Bruce Fields  * requests from a broken NFS client. It may also fail in the presence
769b533184fSJ. Bruce Fields  * of tasks (such as posix threads) sharing the same open file table.
770b533184fSJ. Bruce Fields  * To handle those cases, we just bail out after a few iterations.
77157b65325SJeff Layton  *
772cff2fce5SJeff Layton  * For FL_OFDLCK locks, the owner is the filp, not the files_struct.
77357b65325SJeff Layton  * Because the owner is not even nominally tied to a thread of
77457b65325SJeff Layton  * execution, the deadlock detection below can't reasonably work well. Just
77557b65325SJeff Layton  * skip it for those.
77657b65325SJeff Layton  *
777cff2fce5SJeff Layton  * In principle, we could do a more limited deadlock detection on FL_OFDLCK
77857b65325SJeff Layton  * locks that just checks for the case where two tasks are attempting to
77957b65325SJeff Layton  * upgrade from read to write locks on the same inode.
7801da177e4SLinus Torvalds  */
78197855b49SJ. Bruce Fields 
78297855b49SJ. Bruce Fields #define MAX_DEADLK_ITERATIONS 10
78397855b49SJ. Bruce Fields 
784b533184fSJ. Bruce Fields /* Find a lock that the owner of the given block_fl is blocking on. */
785b533184fSJ. Bruce Fields static struct file_lock *what_owner_is_waiting_for(struct file_lock *block_fl)
786b533184fSJ. Bruce Fields {
787b533184fSJ. Bruce Fields 	struct file_lock *fl;
788b533184fSJ. Bruce Fields 
7893999e493SJeff Layton 	hash_for_each_possible(blocked_hash, fl, fl_link, posix_owner_key(block_fl)) {
790b533184fSJ. Bruce Fields 		if (posix_same_owner(fl, block_fl))
791b533184fSJ. Bruce Fields 			return fl->fl_next;
792b533184fSJ. Bruce Fields 	}
793b533184fSJ. Bruce Fields 	return NULL;
794b533184fSJ. Bruce Fields }
795b533184fSJ. Bruce Fields 
7967b2296afSJeff Layton /* Must be called with the blocked_lock_lock held! */
797b0904e14SAdrian Bunk static int posix_locks_deadlock(struct file_lock *caller_fl,
7981da177e4SLinus Torvalds 				struct file_lock *block_fl)
7991da177e4SLinus Torvalds {
80097855b49SJ. Bruce Fields 	int i = 0;
8011da177e4SLinus Torvalds 
80257b65325SJeff Layton 	/*
80357b65325SJeff Layton 	 * This deadlock detector can't reasonably detect deadlocks with
804cff2fce5SJeff Layton 	 * FL_OFDLCK locks, since they aren't owned by a process, per-se.
80557b65325SJeff Layton 	 */
806cff2fce5SJeff Layton 	if (IS_OFDLCK(caller_fl))
80757b65325SJeff Layton 		return 0;
80857b65325SJeff Layton 
809b533184fSJ. Bruce Fields 	while ((block_fl = what_owner_is_waiting_for(block_fl))) {
81097855b49SJ. Bruce Fields 		if (i++ > MAX_DEADLK_ITERATIONS)
81197855b49SJ. Bruce Fields 			return 0;
812b533184fSJ. Bruce Fields 		if (posix_same_owner(caller_fl, block_fl))
813b533184fSJ. Bruce Fields 			return 1;
8141da177e4SLinus Torvalds 	}
8151da177e4SLinus Torvalds 	return 0;
8161da177e4SLinus Torvalds }
8171da177e4SLinus Torvalds 
8181da177e4SLinus Torvalds /* Try to create a FLOCK lock on filp. We always insert new FLOCK locks
81902888f41SJ. Bruce Fields  * after any leases, but before any posix locks.
820f475ae95STrond Myklebust  *
821f475ae95STrond Myklebust  * Note that if called with an FL_EXISTS argument, the caller may determine
822f475ae95STrond Myklebust  * whether or not a lock was successfully freed by testing the return
823f475ae95STrond Myklebust  * value for -ENOENT.
8241da177e4SLinus Torvalds  */
825993dfa87STrond Myklebust static int flock_lock_file(struct file *filp, struct file_lock *request)
8261da177e4SLinus Torvalds {
827993dfa87STrond Myklebust 	struct file_lock *new_fl = NULL;
8281da177e4SLinus Torvalds 	struct file_lock **before;
829496ad9aaSAl Viro 	struct inode * inode = file_inode(filp);
8301da177e4SLinus Torvalds 	int error = 0;
8311da177e4SLinus Torvalds 	int found = 0;
832ed9814d8SJeff Layton 	LIST_HEAD(dispose);
8331da177e4SLinus Torvalds 
834b89f4321SArnd Bergmann 	if (!(request->fl_flags & FL_ACCESS) && (request->fl_type != F_UNLCK)) {
835b89f4321SArnd Bergmann 		new_fl = locks_alloc_lock();
836b89f4321SArnd Bergmann 		if (!new_fl)
837b89f4321SArnd Bergmann 			return -ENOMEM;
838b89f4321SArnd Bergmann 	}
839b89f4321SArnd Bergmann 
8401c8c601aSJeff Layton 	spin_lock(&inode->i_lock);
841f07f18ddSTrond Myklebust 	if (request->fl_flags & FL_ACCESS)
842f07f18ddSTrond Myklebust 		goto find_conflict;
84384d535adSPavel Emelyanov 
8441da177e4SLinus Torvalds 	for_each_lock(inode, before) {
8451da177e4SLinus Torvalds 		struct file_lock *fl = *before;
8461da177e4SLinus Torvalds 		if (IS_POSIX(fl))
8471da177e4SLinus Torvalds 			break;
8481da177e4SLinus Torvalds 		if (IS_LEASE(fl))
8491da177e4SLinus Torvalds 			continue;
8501da177e4SLinus Torvalds 		if (filp != fl->fl_file)
8511da177e4SLinus Torvalds 			continue;
852993dfa87STrond Myklebust 		if (request->fl_type == fl->fl_type)
8531da177e4SLinus Torvalds 			goto out;
8541da177e4SLinus Torvalds 		found = 1;
855ed9814d8SJeff Layton 		locks_delete_lock(before, &dispose);
8561da177e4SLinus Torvalds 		break;
8571da177e4SLinus Torvalds 	}
8581da177e4SLinus Torvalds 
859f475ae95STrond Myklebust 	if (request->fl_type == F_UNLCK) {
860f475ae95STrond Myklebust 		if ((request->fl_flags & FL_EXISTS) && !found)
861f475ae95STrond Myklebust 			error = -ENOENT;
862993dfa87STrond Myklebust 		goto out;
863f475ae95STrond Myklebust 	}
8641da177e4SLinus Torvalds 
8651da177e4SLinus Torvalds 	/*
8661da177e4SLinus Torvalds 	 * If a higher-priority process was blocked on the old file lock,
8671da177e4SLinus Torvalds 	 * give it the opportunity to lock the file.
8681da177e4SLinus Torvalds 	 */
869b89f4321SArnd Bergmann 	if (found) {
8701c8c601aSJeff Layton 		spin_unlock(&inode->i_lock);
871def01bc5SFrederic Weisbecker 		cond_resched();
8721c8c601aSJeff Layton 		spin_lock(&inode->i_lock);
873b89f4321SArnd Bergmann 	}
8741da177e4SLinus Torvalds 
875f07f18ddSTrond Myklebust find_conflict:
8761da177e4SLinus Torvalds 	for_each_lock(inode, before) {
8771da177e4SLinus Torvalds 		struct file_lock *fl = *before;
8781da177e4SLinus Torvalds 		if (IS_POSIX(fl))
8791da177e4SLinus Torvalds 			break;
8801da177e4SLinus Torvalds 		if (IS_LEASE(fl))
8811da177e4SLinus Torvalds 			continue;
882993dfa87STrond Myklebust 		if (!flock_locks_conflict(request, fl))
8831da177e4SLinus Torvalds 			continue;
8841da177e4SLinus Torvalds 		error = -EAGAIN;
885bde74e4bSMiklos Szeredi 		if (!(request->fl_flags & FL_SLEEP))
886bde74e4bSMiklos Szeredi 			goto out;
887bde74e4bSMiklos Szeredi 		error = FILE_LOCK_DEFERRED;
888993dfa87STrond Myklebust 		locks_insert_block(fl, request);
8891da177e4SLinus Torvalds 		goto out;
8901da177e4SLinus Torvalds 	}
891f07f18ddSTrond Myklebust 	if (request->fl_flags & FL_ACCESS)
892f07f18ddSTrond Myklebust 		goto out;
893993dfa87STrond Myklebust 	locks_copy_lock(new_fl, request);
8940e2f6db8SPavel Emelyanov 	locks_insert_lock(before, new_fl);
895993dfa87STrond Myklebust 	new_fl = NULL;
8969cedc194SKirill Korotaev 	error = 0;
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds out:
8991c8c601aSJeff Layton 	spin_unlock(&inode->i_lock);
900993dfa87STrond Myklebust 	if (new_fl)
901993dfa87STrond Myklebust 		locks_free_lock(new_fl);
902ed9814d8SJeff Layton 	locks_dispose_list(&dispose);
9031da177e4SLinus Torvalds 	return error;
9041da177e4SLinus Torvalds }
9051da177e4SLinus Torvalds 
906150b3934SMarc Eshel static int __posix_lock_file(struct inode *inode, struct file_lock *request, struct file_lock *conflock)
9071da177e4SLinus Torvalds {
9081da177e4SLinus Torvalds 	struct file_lock *fl;
90939005d02SMiklos Szeredi 	struct file_lock *new_fl = NULL;
91039005d02SMiklos Szeredi 	struct file_lock *new_fl2 = NULL;
9111da177e4SLinus Torvalds 	struct file_lock *left = NULL;
9121da177e4SLinus Torvalds 	struct file_lock *right = NULL;
9131da177e4SLinus Torvalds 	struct file_lock **before;
914b9746ef8SJeff Layton 	int error;
915b9746ef8SJeff Layton 	bool added = false;
916ed9814d8SJeff Layton 	LIST_HEAD(dispose);
9171da177e4SLinus Torvalds 
9181da177e4SLinus Torvalds 	/*
9191da177e4SLinus Torvalds 	 * We may need two file_lock structures for this operation,
9201da177e4SLinus Torvalds 	 * so we get them in advance to avoid races.
92139005d02SMiklos Szeredi 	 *
92239005d02SMiklos Szeredi 	 * In some cases we can be sure, that no new locks will be needed
9231da177e4SLinus Torvalds 	 */
92439005d02SMiklos Szeredi 	if (!(request->fl_flags & FL_ACCESS) &&
92539005d02SMiklos Szeredi 	    (request->fl_type != F_UNLCK ||
92639005d02SMiklos Szeredi 	     request->fl_start != 0 || request->fl_end != OFFSET_MAX)) {
9271da177e4SLinus Torvalds 		new_fl = locks_alloc_lock();
9281da177e4SLinus Torvalds 		new_fl2 = locks_alloc_lock();
92939005d02SMiklos Szeredi 	}
9301da177e4SLinus Torvalds 
9311c8c601aSJeff Layton 	spin_lock(&inode->i_lock);
9321cb36012SJeff Layton 	/*
9331cb36012SJeff Layton 	 * New lock request. Walk all POSIX locks and look for conflicts. If
9341cb36012SJeff Layton 	 * there are any, either return error or put the request on the
93548f74186SJeff Layton 	 * blocker's list of waiters and the global blocked_hash.
9361cb36012SJeff Layton 	 */
9371da177e4SLinus Torvalds 	if (request->fl_type != F_UNLCK) {
9381da177e4SLinus Torvalds 		for_each_lock(inode, before) {
939526985b9SJ. Bruce Fields 			fl = *before;
9401da177e4SLinus Torvalds 			if (!IS_POSIX(fl))
9411da177e4SLinus Torvalds 				continue;
9421da177e4SLinus Torvalds 			if (!posix_locks_conflict(request, fl))
9431da177e4SLinus Torvalds 				continue;
9445842add2SAndy Adamson 			if (conflock)
9453fe0fff1SKinglong Mee 				locks_copy_conflock(conflock, fl);
9461da177e4SLinus Torvalds 			error = -EAGAIN;
9471da177e4SLinus Torvalds 			if (!(request->fl_flags & FL_SLEEP))
9481da177e4SLinus Torvalds 				goto out;
9491c8c601aSJeff Layton 			/*
9501c8c601aSJeff Layton 			 * Deadlock detection and insertion into the blocked
9511c8c601aSJeff Layton 			 * locks list must be done while holding the same lock!
9521c8c601aSJeff Layton 			 */
9531da177e4SLinus Torvalds 			error = -EDEADLK;
9547b2296afSJeff Layton 			spin_lock(&blocked_lock_lock);
9551c8c601aSJeff Layton 			if (likely(!posix_locks_deadlock(request, fl))) {
956bde74e4bSMiklos Szeredi 				error = FILE_LOCK_DEFERRED;
9571c8c601aSJeff Layton 				__locks_insert_block(fl, request);
9581c8c601aSJeff Layton 			}
9597b2296afSJeff Layton 			spin_unlock(&blocked_lock_lock);
9601da177e4SLinus Torvalds 			goto out;
9611da177e4SLinus Torvalds   		}
9621da177e4SLinus Torvalds   	}
9631da177e4SLinus Torvalds 
9641da177e4SLinus Torvalds 	/* If we're just looking for a conflict, we're done. */
9651da177e4SLinus Torvalds 	error = 0;
9661da177e4SLinus Torvalds 	if (request->fl_flags & FL_ACCESS)
9671da177e4SLinus Torvalds 		goto out;
9681da177e4SLinus Torvalds 
9691da177e4SLinus Torvalds 	/*
9701da177e4SLinus Torvalds 	 * Find the first old lock with the same owner as the new lock.
9711da177e4SLinus Torvalds 	 */
9721da177e4SLinus Torvalds 
9731da177e4SLinus Torvalds 	before = &inode->i_flock;
9741da177e4SLinus Torvalds 
9751da177e4SLinus Torvalds 	/* First skip locks owned by other processes.  */
9761da177e4SLinus Torvalds 	while ((fl = *before) && (!IS_POSIX(fl) ||
9771da177e4SLinus Torvalds 				  !posix_same_owner(request, fl))) {
9781da177e4SLinus Torvalds 		before = &fl->fl_next;
9791da177e4SLinus Torvalds 	}
9801da177e4SLinus Torvalds 
9811da177e4SLinus Torvalds 	/* Process locks with this owner. */
9821da177e4SLinus Torvalds 	while ((fl = *before) && posix_same_owner(request, fl)) {
9831da177e4SLinus Torvalds 		/* Detect adjacent or overlapping regions (if same lock type)
9841da177e4SLinus Torvalds 		 */
9851da177e4SLinus Torvalds 		if (request->fl_type == fl->fl_type) {
986449231d6SOlaf Kirch 			/* In all comparisons of start vs end, use
987449231d6SOlaf Kirch 			 * "start - 1" rather than "end + 1". If end
988449231d6SOlaf Kirch 			 * is OFFSET_MAX, end + 1 will become negative.
989449231d6SOlaf Kirch 			 */
9901da177e4SLinus Torvalds 			if (fl->fl_end < request->fl_start - 1)
9911da177e4SLinus Torvalds 				goto next_lock;
9921da177e4SLinus Torvalds 			/* If the next lock in the list has entirely bigger
9931da177e4SLinus Torvalds 			 * addresses than the new one, insert the lock here.
9941da177e4SLinus Torvalds 			 */
995449231d6SOlaf Kirch 			if (fl->fl_start - 1 > request->fl_end)
9961da177e4SLinus Torvalds 				break;
9971da177e4SLinus Torvalds 
9981da177e4SLinus Torvalds 			/* If we come here, the new and old lock are of the
9991da177e4SLinus Torvalds 			 * same type and adjacent or overlapping. Make one
10001da177e4SLinus Torvalds 			 * lock yielding from the lower start address of both
10011da177e4SLinus Torvalds 			 * locks to the higher end address.
10021da177e4SLinus Torvalds 			 */
10031da177e4SLinus Torvalds 			if (fl->fl_start > request->fl_start)
10041da177e4SLinus Torvalds 				fl->fl_start = request->fl_start;
10051da177e4SLinus Torvalds 			else
10061da177e4SLinus Torvalds 				request->fl_start = fl->fl_start;
10071da177e4SLinus Torvalds 			if (fl->fl_end < request->fl_end)
10081da177e4SLinus Torvalds 				fl->fl_end = request->fl_end;
10091da177e4SLinus Torvalds 			else
10101da177e4SLinus Torvalds 				request->fl_end = fl->fl_end;
10111da177e4SLinus Torvalds 			if (added) {
1012ed9814d8SJeff Layton 				locks_delete_lock(before, &dispose);
10131da177e4SLinus Torvalds 				continue;
10141da177e4SLinus Torvalds 			}
10151da177e4SLinus Torvalds 			request = fl;
1016b9746ef8SJeff Layton 			added = true;
10171da177e4SLinus Torvalds 		}
10181da177e4SLinus Torvalds 		else {
10191da177e4SLinus Torvalds 			/* Processing for different lock types is a bit
10201da177e4SLinus Torvalds 			 * more complex.
10211da177e4SLinus Torvalds 			 */
10221da177e4SLinus Torvalds 			if (fl->fl_end < request->fl_start)
10231da177e4SLinus Torvalds 				goto next_lock;
10241da177e4SLinus Torvalds 			if (fl->fl_start > request->fl_end)
10251da177e4SLinus Torvalds 				break;
10261da177e4SLinus Torvalds 			if (request->fl_type == F_UNLCK)
1027b9746ef8SJeff Layton 				added = true;
10281da177e4SLinus Torvalds 			if (fl->fl_start < request->fl_start)
10291da177e4SLinus Torvalds 				left = fl;
10301da177e4SLinus Torvalds 			/* If the next lock in the list has a higher end
10311da177e4SLinus Torvalds 			 * address than the new one, insert the new one here.
10321da177e4SLinus Torvalds 			 */
10331da177e4SLinus Torvalds 			if (fl->fl_end > request->fl_end) {
10341da177e4SLinus Torvalds 				right = fl;
10351da177e4SLinus Torvalds 				break;
10361da177e4SLinus Torvalds 			}
10371da177e4SLinus Torvalds 			if (fl->fl_start >= request->fl_start) {
10381da177e4SLinus Torvalds 				/* The new lock completely replaces an old
10391da177e4SLinus Torvalds 				 * one (This may happen several times).
10401da177e4SLinus Torvalds 				 */
10411da177e4SLinus Torvalds 				if (added) {
1042ed9814d8SJeff Layton 					locks_delete_lock(before, &dispose);
10431da177e4SLinus Torvalds 					continue;
10441da177e4SLinus Torvalds 				}
1045b84d49f9SJeff Layton 				/*
1046b84d49f9SJeff Layton 				 * Replace the old lock with new_fl, and
1047b84d49f9SJeff Layton 				 * remove the old one. It's safe to do the
1048b84d49f9SJeff Layton 				 * insert here since we know that we won't be
1049b84d49f9SJeff Layton 				 * using new_fl later, and that the lock is
1050b84d49f9SJeff Layton 				 * just replacing an existing lock.
10511da177e4SLinus Torvalds 				 */
1052b84d49f9SJeff Layton 				error = -ENOLCK;
1053b84d49f9SJeff Layton 				if (!new_fl)
1054b84d49f9SJeff Layton 					goto out;
1055b84d49f9SJeff Layton 				locks_copy_lock(new_fl, request);
1056b84d49f9SJeff Layton 				request = new_fl;
1057b84d49f9SJeff Layton 				new_fl = NULL;
1058ed9814d8SJeff Layton 				locks_delete_lock(before, &dispose);
1059b84d49f9SJeff Layton 				locks_insert_lock(before, request);
1060b9746ef8SJeff Layton 				added = true;
10611da177e4SLinus Torvalds 			}
10621da177e4SLinus Torvalds 		}
10631da177e4SLinus Torvalds 		/* Go on to next lock.
10641da177e4SLinus Torvalds 		 */
10651da177e4SLinus Torvalds 	next_lock:
10661da177e4SLinus Torvalds 		before = &fl->fl_next;
10671da177e4SLinus Torvalds 	}
10681da177e4SLinus Torvalds 
10690d9a490aSMiklos Szeredi 	/*
10701cb36012SJeff Layton 	 * The above code only modifies existing locks in case of merging or
10711cb36012SJeff Layton 	 * replacing. If new lock(s) need to be inserted all modifications are
10721cb36012SJeff Layton 	 * done below this, so it's safe yet to bail out.
10730d9a490aSMiklos Szeredi 	 */
10740d9a490aSMiklos Szeredi 	error = -ENOLCK; /* "no luck" */
10750d9a490aSMiklos Szeredi 	if (right && left == right && !new_fl2)
10760d9a490aSMiklos Szeredi 		goto out;
10770d9a490aSMiklos Szeredi 
10781da177e4SLinus Torvalds 	error = 0;
10791da177e4SLinus Torvalds 	if (!added) {
1080f475ae95STrond Myklebust 		if (request->fl_type == F_UNLCK) {
1081f475ae95STrond Myklebust 			if (request->fl_flags & FL_EXISTS)
1082f475ae95STrond Myklebust 				error = -ENOENT;
10831da177e4SLinus Torvalds 			goto out;
1084f475ae95STrond Myklebust 		}
10850d9a490aSMiklos Szeredi 
10860d9a490aSMiklos Szeredi 		if (!new_fl) {
10870d9a490aSMiklos Szeredi 			error = -ENOLCK;
10880d9a490aSMiklos Szeredi 			goto out;
10890d9a490aSMiklos Szeredi 		}
10901da177e4SLinus Torvalds 		locks_copy_lock(new_fl, request);
10911da177e4SLinus Torvalds 		locks_insert_lock(before, new_fl);
10921da177e4SLinus Torvalds 		new_fl = NULL;
10931da177e4SLinus Torvalds 	}
10941da177e4SLinus Torvalds 	if (right) {
10951da177e4SLinus Torvalds 		if (left == right) {
10961da177e4SLinus Torvalds 			/* The new lock breaks the old one in two pieces,
10971da177e4SLinus Torvalds 			 * so we have to use the second new lock.
10981da177e4SLinus Torvalds 			 */
10991da177e4SLinus Torvalds 			left = new_fl2;
11001da177e4SLinus Torvalds 			new_fl2 = NULL;
11011da177e4SLinus Torvalds 			locks_copy_lock(left, right);
11021da177e4SLinus Torvalds 			locks_insert_lock(before, left);
11031da177e4SLinus Torvalds 		}
11041da177e4SLinus Torvalds 		right->fl_start = request->fl_end + 1;
11051da177e4SLinus Torvalds 		locks_wake_up_blocks(right);
11061da177e4SLinus Torvalds 	}
11071da177e4SLinus Torvalds 	if (left) {
11081da177e4SLinus Torvalds 		left->fl_end = request->fl_start - 1;
11091da177e4SLinus Torvalds 		locks_wake_up_blocks(left);
11101da177e4SLinus Torvalds 	}
11111da177e4SLinus Torvalds  out:
11121c8c601aSJeff Layton 	spin_unlock(&inode->i_lock);
11131da177e4SLinus Torvalds 	/*
11141da177e4SLinus Torvalds 	 * Free any unused locks.
11151da177e4SLinus Torvalds 	 */
11161da177e4SLinus Torvalds 	if (new_fl)
11171da177e4SLinus Torvalds 		locks_free_lock(new_fl);
11181da177e4SLinus Torvalds 	if (new_fl2)
11191da177e4SLinus Torvalds 		locks_free_lock(new_fl2);
1120ed9814d8SJeff Layton 	locks_dispose_list(&dispose);
11211da177e4SLinus Torvalds 	return error;
11221da177e4SLinus Torvalds }
11231da177e4SLinus Torvalds 
11241da177e4SLinus Torvalds /**
11251da177e4SLinus Torvalds  * posix_lock_file - Apply a POSIX-style lock to a file
11261da177e4SLinus Torvalds  * @filp: The file to apply the lock to
11271da177e4SLinus Torvalds  * @fl: The lock to be applied
1128150b3934SMarc Eshel  * @conflock: Place to return a copy of the conflicting lock, if found.
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
1133f475ae95STrond Myklebust  *
1134f475ae95STrond Myklebust  * Note that if called with an FL_EXISTS argument, the caller may determine
1135f475ae95STrond Myklebust  * whether or not a lock was successfully freed by testing the return
1136f475ae95STrond Myklebust  * value for -ENOENT.
11371da177e4SLinus Torvalds  */
1138150b3934SMarc Eshel int posix_lock_file(struct file *filp, struct file_lock *fl,
11395842add2SAndy Adamson 			struct file_lock *conflock)
11405842add2SAndy Adamson {
1141496ad9aaSAl Viro 	return __posix_lock_file(file_inode(filp), fl, conflock);
11425842add2SAndy Adamson }
1143150b3934SMarc Eshel EXPORT_SYMBOL(posix_lock_file);
11441da177e4SLinus Torvalds 
11451da177e4SLinus Torvalds /**
11461da177e4SLinus Torvalds  * posix_lock_file_wait - Apply a POSIX-style lock to a file
11471da177e4SLinus Torvalds  * @filp: The file to apply the lock to
11481da177e4SLinus Torvalds  * @fl: The lock to be applied
11491da177e4SLinus Torvalds  *
11501da177e4SLinus Torvalds  * Add a POSIX style lock to a file.
11511da177e4SLinus Torvalds  * We merge adjacent & overlapping locks whenever possible.
11521da177e4SLinus Torvalds  * POSIX locks are sorted by owner task, then by starting address
11531da177e4SLinus Torvalds  */
11541da177e4SLinus Torvalds int posix_lock_file_wait(struct file *filp, struct file_lock *fl)
11551da177e4SLinus Torvalds {
11561da177e4SLinus Torvalds 	int error;
11571da177e4SLinus Torvalds 	might_sleep ();
11581da177e4SLinus Torvalds 	for (;;) {
1159150b3934SMarc Eshel 		error = posix_lock_file(filp, fl, NULL);
1160bde74e4bSMiklos Szeredi 		if (error != FILE_LOCK_DEFERRED)
11611da177e4SLinus Torvalds 			break;
11621da177e4SLinus Torvalds 		error = wait_event_interruptible(fl->fl_wait, !fl->fl_next);
11631da177e4SLinus Torvalds 		if (!error)
11641da177e4SLinus Torvalds 			continue;
11651da177e4SLinus Torvalds 
11661da177e4SLinus Torvalds 		locks_delete_block(fl);
11671da177e4SLinus Torvalds 		break;
11681da177e4SLinus Torvalds 	}
11691da177e4SLinus Torvalds 	return error;
11701da177e4SLinus Torvalds }
11711da177e4SLinus Torvalds EXPORT_SYMBOL(posix_lock_file_wait);
11721da177e4SLinus Torvalds 
11731da177e4SLinus Torvalds /**
11741da177e4SLinus Torvalds  * locks_mandatory_locked - Check for an active lock
1175d7a06983SJeff Layton  * @file: the file to check
11761da177e4SLinus Torvalds  *
11771da177e4SLinus Torvalds  * Searches the inode's list of locks to find any POSIX locks which conflict.
11781da177e4SLinus Torvalds  * This function is called from locks_verify_locked() only.
11791da177e4SLinus Torvalds  */
1180d7a06983SJeff Layton int locks_mandatory_locked(struct file *file)
11811da177e4SLinus Torvalds {
1182d7a06983SJeff Layton 	struct inode *inode = file_inode(file);
11831da177e4SLinus Torvalds 	struct file_lock *fl;
11841da177e4SLinus Torvalds 
11851da177e4SLinus Torvalds 	/*
11861da177e4SLinus Torvalds 	 * Search the lock list for this inode for any POSIX locks.
11871da177e4SLinus Torvalds 	 */
11881c8c601aSJeff Layton 	spin_lock(&inode->i_lock);
11891da177e4SLinus Torvalds 	for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
11901da177e4SLinus Torvalds 		if (!IS_POSIX(fl))
11911da177e4SLinus Torvalds 			continue;
119273a8f5f7SChristoph Hellwig 		if (fl->fl_owner != current->files &&
119373a8f5f7SChristoph Hellwig 		    fl->fl_owner != file)
11941da177e4SLinus Torvalds 			break;
11951da177e4SLinus Torvalds 	}
11961c8c601aSJeff Layton 	spin_unlock(&inode->i_lock);
11971da177e4SLinus Torvalds 	return fl ? -EAGAIN : 0;
11981da177e4SLinus Torvalds }
11991da177e4SLinus Torvalds 
12001da177e4SLinus Torvalds /**
12011da177e4SLinus Torvalds  * locks_mandatory_area - Check for a conflicting lock
12021da177e4SLinus Torvalds  * @read_write: %FLOCK_VERIFY_WRITE for exclusive access, %FLOCK_VERIFY_READ
12031da177e4SLinus Torvalds  *		for shared
12041da177e4SLinus Torvalds  * @inode:      the file to check
12051da177e4SLinus Torvalds  * @filp:       how the file was opened (if it was)
12061da177e4SLinus Torvalds  * @offset:     start of area to check
12071da177e4SLinus Torvalds  * @count:      length of area to check
12081da177e4SLinus Torvalds  *
12091da177e4SLinus Torvalds  * Searches the inode's list of locks to find any POSIX locks which conflict.
12101da177e4SLinus Torvalds  * This function is called from rw_verify_area() and
12111da177e4SLinus Torvalds  * locks_verify_truncate().
12121da177e4SLinus Torvalds  */
12131da177e4SLinus Torvalds int locks_mandatory_area(int read_write, struct inode *inode,
12141da177e4SLinus Torvalds 			 struct file *filp, loff_t offset,
12151da177e4SLinus Torvalds 			 size_t count)
12161da177e4SLinus Torvalds {
12171da177e4SLinus Torvalds 	struct file_lock fl;
12181da177e4SLinus Torvalds 	int error;
121929723adeSJeff Layton 	bool sleep = false;
12201da177e4SLinus Torvalds 
12211da177e4SLinus Torvalds 	locks_init_lock(&fl);
12221da177e4SLinus Torvalds 	fl.fl_pid = current->tgid;
12231da177e4SLinus Torvalds 	fl.fl_file = filp;
12241da177e4SLinus Torvalds 	fl.fl_flags = FL_POSIX | FL_ACCESS;
12251da177e4SLinus Torvalds 	if (filp && !(filp->f_flags & O_NONBLOCK))
122629723adeSJeff Layton 		sleep = true;
12271da177e4SLinus Torvalds 	fl.fl_type = (read_write == FLOCK_VERIFY_WRITE) ? F_WRLCK : F_RDLCK;
12281da177e4SLinus Torvalds 	fl.fl_start = offset;
12291da177e4SLinus Torvalds 	fl.fl_end = offset + count - 1;
12301da177e4SLinus Torvalds 
12311da177e4SLinus Torvalds 	for (;;) {
123229723adeSJeff Layton 		if (filp) {
123373a8f5f7SChristoph Hellwig 			fl.fl_owner = filp;
123429723adeSJeff Layton 			fl.fl_flags &= ~FL_SLEEP;
123529723adeSJeff Layton 			error = __posix_lock_file(inode, &fl, NULL);
123629723adeSJeff Layton 			if (!error)
123729723adeSJeff Layton 				break;
123829723adeSJeff Layton 		}
123929723adeSJeff Layton 
124029723adeSJeff Layton 		if (sleep)
124129723adeSJeff Layton 			fl.fl_flags |= FL_SLEEP;
124229723adeSJeff Layton 		fl.fl_owner = current->files;
1243150b3934SMarc Eshel 		error = __posix_lock_file(inode, &fl, NULL);
1244bde74e4bSMiklos Szeredi 		if (error != FILE_LOCK_DEFERRED)
12451da177e4SLinus Torvalds 			break;
12461da177e4SLinus Torvalds 		error = wait_event_interruptible(fl.fl_wait, !fl.fl_next);
12471da177e4SLinus Torvalds 		if (!error) {
12481da177e4SLinus Torvalds 			/*
12491da177e4SLinus Torvalds 			 * If we've been sleeping someone might have
12501da177e4SLinus Torvalds 			 * changed the permissions behind our back.
12511da177e4SLinus Torvalds 			 */
1252a16877caSPavel Emelyanov 			if (__mandatory_lock(inode))
12531da177e4SLinus Torvalds 				continue;
12541da177e4SLinus Torvalds 		}
12551da177e4SLinus Torvalds 
12561da177e4SLinus Torvalds 		locks_delete_block(&fl);
12571da177e4SLinus Torvalds 		break;
12581da177e4SLinus Torvalds 	}
12591da177e4SLinus Torvalds 
12601da177e4SLinus Torvalds 	return error;
12611da177e4SLinus Torvalds }
12621da177e4SLinus Torvalds 
12631da177e4SLinus Torvalds EXPORT_SYMBOL(locks_mandatory_area);
12641da177e4SLinus Torvalds 
1265778fc546SJ. Bruce Fields static void lease_clear_pending(struct file_lock *fl, int arg)
1266778fc546SJ. Bruce Fields {
1267778fc546SJ. Bruce Fields 	switch (arg) {
1268778fc546SJ. Bruce Fields 	case F_UNLCK:
1269778fc546SJ. Bruce Fields 		fl->fl_flags &= ~FL_UNLOCK_PENDING;
1270778fc546SJ. Bruce Fields 		/* fall through: */
1271778fc546SJ. Bruce Fields 	case F_RDLCK:
1272778fc546SJ. Bruce Fields 		fl->fl_flags &= ~FL_DOWNGRADE_PENDING;
1273778fc546SJ. Bruce Fields 	}
1274778fc546SJ. Bruce Fields }
1275778fc546SJ. Bruce Fields 
12761da177e4SLinus Torvalds /* We already had a lease on this file; just change its type */
12771da177e4SLinus Torvalds int lease_modify(struct file_lock **before, int arg)
12781da177e4SLinus Torvalds {
12791da177e4SLinus Torvalds 	struct file_lock *fl = *before;
12801da177e4SLinus Torvalds 	int error = assign_type(fl, arg);
12811da177e4SLinus Torvalds 
12821da177e4SLinus Torvalds 	if (error)
12831da177e4SLinus Torvalds 		return error;
1284778fc546SJ. Bruce Fields 	lease_clear_pending(fl, arg);
12851da177e4SLinus Torvalds 	locks_wake_up_blocks(fl);
12863b6e2723SFilipe Brandenburger 	if (arg == F_UNLCK) {
12873b6e2723SFilipe Brandenburger 		struct file *filp = fl->fl_file;
12883b6e2723SFilipe Brandenburger 
12893b6e2723SFilipe Brandenburger 		f_delown(filp);
12903b6e2723SFilipe Brandenburger 		filp->f_owner.signum = 0;
129196d6d59cSJ. Bruce Fields 		fasync_helper(0, fl->fl_file, 0, &fl->fl_fasync);
129296d6d59cSJ. Bruce Fields 		if (fl->fl_fasync != NULL) {
129396d6d59cSJ. Bruce Fields 			printk(KERN_ERR "locks_delete_lock: fasync == %p\n", fl->fl_fasync);
129496d6d59cSJ. Bruce Fields 			fl->fl_fasync = NULL;
129596d6d59cSJ. Bruce Fields 		}
1296ed9814d8SJeff Layton 		locks_delete_lock(before, NULL);
12973b6e2723SFilipe Brandenburger 	}
12981da177e4SLinus Torvalds 	return 0;
12991da177e4SLinus Torvalds }
13001da177e4SLinus Torvalds EXPORT_SYMBOL(lease_modify);
13011da177e4SLinus Torvalds 
1302778fc546SJ. Bruce Fields static bool past_time(unsigned long then)
1303778fc546SJ. Bruce Fields {
1304778fc546SJ. Bruce Fields 	if (!then)
1305778fc546SJ. Bruce Fields 		/* 0 is a special value meaning "this never expires": */
1306778fc546SJ. Bruce Fields 		return false;
1307778fc546SJ. Bruce Fields 	return time_after(jiffies, then);
1308778fc546SJ. Bruce Fields }
1309778fc546SJ. Bruce Fields 
13101da177e4SLinus Torvalds static void time_out_leases(struct inode *inode)
13111da177e4SLinus Torvalds {
13121da177e4SLinus Torvalds 	struct file_lock **before;
13131da177e4SLinus Torvalds 	struct file_lock *fl;
13141da177e4SLinus Torvalds 
13151da177e4SLinus Torvalds 	before = &inode->i_flock;
1316ab83fa4bSJ. Bruce Fields 	while ((fl = *before) && IS_LEASE(fl) && lease_breaking(fl)) {
131762af4f1fSJeff Layton 		trace_time_out_leases(inode, fl);
1318778fc546SJ. Bruce Fields 		if (past_time(fl->fl_downgrade_time))
1319778fc546SJ. Bruce Fields 			lease_modify(before, F_RDLCK);
1320778fc546SJ. Bruce Fields 		if (past_time(fl->fl_break_time))
1321778fc546SJ. Bruce Fields 			lease_modify(before, F_UNLCK);
13221da177e4SLinus Torvalds 		if (fl == *before)	/* lease_modify may have freed fl */
13231da177e4SLinus Torvalds 			before = &fl->fl_next;
13241da177e4SLinus Torvalds 	}
13251da177e4SLinus Torvalds }
13261da177e4SLinus Torvalds 
1327df4e8d2cSJ. Bruce Fields static bool leases_conflict(struct file_lock *lease, struct file_lock *breaker)
1328df4e8d2cSJ. Bruce Fields {
1329df4e8d2cSJ. Bruce Fields 	if ((breaker->fl_flags & FL_DELEG) && (lease->fl_flags & FL_LEASE))
1330df4e8d2cSJ. Bruce Fields 		return false;
1331df4e8d2cSJ. Bruce Fields 	return locks_conflict(breaker, lease);
1332df4e8d2cSJ. Bruce Fields }
1333df4e8d2cSJ. Bruce Fields 
13341da177e4SLinus Torvalds /**
13351da177e4SLinus Torvalds  *	__break_lease	-	revoke all outstanding leases on file
13361da177e4SLinus Torvalds  *	@inode: the inode of the file to return
1337df4e8d2cSJ. Bruce Fields  *	@mode: O_RDONLY: break only write leases; O_WRONLY or O_RDWR:
1338df4e8d2cSJ. Bruce Fields  *	    break all leases
1339df4e8d2cSJ. Bruce Fields  *	@type: FL_LEASE: break leases and delegations; FL_DELEG: break
1340df4e8d2cSJ. Bruce Fields  *	    only delegations
13411da177e4SLinus Torvalds  *
134287250dd2Sdavid m. richter  *	break_lease (inlined for speed) has checked there already is at least
134387250dd2Sdavid m. richter  *	some kind of lock (maybe a lease) on this file.  Leases are broken on
134487250dd2Sdavid m. richter  *	a call to open() or truncate().  This function can sleep unless you
13451da177e4SLinus Torvalds  *	specified %O_NONBLOCK to your open().
13461da177e4SLinus Torvalds  */
1347df4e8d2cSJ. Bruce Fields int __break_lease(struct inode *inode, unsigned int mode, unsigned int type)
13481da177e4SLinus Torvalds {
1349778fc546SJ. Bruce Fields 	int error = 0;
13501da177e4SLinus Torvalds 	struct file_lock *new_fl, *flock;
13511da177e4SLinus Torvalds 	struct file_lock *fl;
13521da177e4SLinus Torvalds 	unsigned long break_time;
13531da177e4SLinus Torvalds 	int i_have_this_lease = 0;
1354df4e8d2cSJ. Bruce Fields 	bool lease_conflict = false;
13558737c930SAl Viro 	int want_write = (mode & O_ACCMODE) != O_RDONLY;
13561da177e4SLinus Torvalds 
13578737c930SAl Viro 	new_fl = lease_alloc(NULL, want_write ? F_WRLCK : F_RDLCK);
13586d4b9e38SLinus Torvalds 	if (IS_ERR(new_fl))
13596d4b9e38SLinus Torvalds 		return PTR_ERR(new_fl);
1360df4e8d2cSJ. Bruce Fields 	new_fl->fl_flags = type;
13611da177e4SLinus Torvalds 
13621c8c601aSJeff Layton 	spin_lock(&inode->i_lock);
13631da177e4SLinus Torvalds 
13641da177e4SLinus Torvalds 	time_out_leases(inode);
13651da177e4SLinus Torvalds 
13661da177e4SLinus Torvalds 	flock = inode->i_flock;
13671da177e4SLinus Torvalds 	if ((flock == NULL) || !IS_LEASE(flock))
13681da177e4SLinus Torvalds 		goto out;
13691da177e4SLinus Torvalds 
1370df4e8d2cSJ. Bruce Fields 	for (fl = flock; fl && IS_LEASE(fl); fl = fl->fl_next) {
1371df4e8d2cSJ. Bruce Fields 		if (leases_conflict(fl, new_fl)) {
1372df4e8d2cSJ. Bruce Fields 			lease_conflict = true;
13731da177e4SLinus Torvalds 			if (fl->fl_owner == current->files)
13741da177e4SLinus Torvalds 				i_have_this_lease = 1;
1375df4e8d2cSJ. Bruce Fields 		}
1376df4e8d2cSJ. Bruce Fields 	}
1377df4e8d2cSJ. Bruce Fields 	if (!lease_conflict)
1378df4e8d2cSJ. Bruce Fields 		goto out;
13791da177e4SLinus Torvalds 
13801da177e4SLinus Torvalds 	break_time = 0;
13811da177e4SLinus Torvalds 	if (lease_break_time > 0) {
13821da177e4SLinus Torvalds 		break_time = jiffies + lease_break_time * HZ;
13831da177e4SLinus Torvalds 		if (break_time == 0)
13841da177e4SLinus Torvalds 			break_time++;	/* so that 0 means no break time */
13851da177e4SLinus Torvalds 	}
13861da177e4SLinus Torvalds 
13871da177e4SLinus Torvalds 	for (fl = flock; fl && IS_LEASE(fl); fl = fl->fl_next) {
1388df4e8d2cSJ. Bruce Fields 		if (!leases_conflict(fl, new_fl))
1389df4e8d2cSJ. Bruce Fields 			continue;
1390778fc546SJ. Bruce Fields 		if (want_write) {
1391778fc546SJ. Bruce Fields 			if (fl->fl_flags & FL_UNLOCK_PENDING)
1392778fc546SJ. Bruce Fields 				continue;
1393778fc546SJ. Bruce Fields 			fl->fl_flags |= FL_UNLOCK_PENDING;
13941da177e4SLinus Torvalds 			fl->fl_break_time = break_time;
1395778fc546SJ. Bruce Fields 		} else {
1396778fc546SJ. Bruce Fields 			if (lease_breaking(flock))
1397778fc546SJ. Bruce Fields 				continue;
1398778fc546SJ. Bruce Fields 			fl->fl_flags |= FL_DOWNGRADE_PENDING;
1399778fc546SJ. Bruce Fields 			fl->fl_downgrade_time = break_time;
14001da177e4SLinus Torvalds 		}
1401778fc546SJ. Bruce Fields 		fl->fl_lmops->lm_break(fl);
14021da177e4SLinus Torvalds 	}
14031da177e4SLinus Torvalds 
14041da177e4SLinus Torvalds 	if (i_have_this_lease || (mode & O_NONBLOCK)) {
140562af4f1fSJeff Layton 		trace_break_lease_noblock(inode, new_fl);
14061da177e4SLinus Torvalds 		error = -EWOULDBLOCK;
14071da177e4SLinus Torvalds 		goto out;
14081da177e4SLinus Torvalds 	}
14091da177e4SLinus Torvalds 
14101da177e4SLinus Torvalds restart:
14111da177e4SLinus Torvalds 	break_time = flock->fl_break_time;
1412f1c6bb2cSJeff Layton 	if (break_time != 0)
14131da177e4SLinus Torvalds 		break_time -= jiffies;
14141da177e4SLinus Torvalds 	if (break_time == 0)
14151da177e4SLinus Torvalds 		break_time++;
14164321e01eSMatthew Wilcox 	locks_insert_block(flock, new_fl);
141762af4f1fSJeff Layton 	trace_break_lease_block(inode, new_fl);
14181c8c601aSJeff Layton 	spin_unlock(&inode->i_lock);
14194321e01eSMatthew Wilcox 	error = wait_event_interruptible_timeout(new_fl->fl_wait,
14204321e01eSMatthew Wilcox 						!new_fl->fl_next, break_time);
14211c8c601aSJeff Layton 	spin_lock(&inode->i_lock);
142262af4f1fSJeff Layton 	trace_break_lease_unblock(inode, new_fl);
14231c8c601aSJeff Layton 	locks_delete_block(new_fl);
14241da177e4SLinus Torvalds 	if (error >= 0) {
14251da177e4SLinus Torvalds 		if (error == 0)
14261da177e4SLinus Torvalds 			time_out_leases(inode);
1427778fc546SJ. Bruce Fields 		/*
1428778fc546SJ. Bruce Fields 		 * Wait for the next conflicting lease that has not been
1429778fc546SJ. Bruce Fields 		 * broken yet
1430778fc546SJ. Bruce Fields 		 */
14311da177e4SLinus Torvalds 		for (flock = inode->i_flock; flock && IS_LEASE(flock);
14321da177e4SLinus Torvalds 				flock = flock->fl_next) {
1433df4e8d2cSJ. Bruce Fields 			if (leases_conflict(new_fl, flock))
14341da177e4SLinus Torvalds 				goto restart;
14351da177e4SLinus Torvalds 		}
14361da177e4SLinus Torvalds 		error = 0;
14371da177e4SLinus Torvalds 	}
14381da177e4SLinus Torvalds 
14391da177e4SLinus Torvalds out:
14401c8c601aSJeff Layton 	spin_unlock(&inode->i_lock);
14411da177e4SLinus Torvalds 	locks_free_lock(new_fl);
14421da177e4SLinus Torvalds 	return error;
14431da177e4SLinus Torvalds }
14441da177e4SLinus Torvalds 
14451da177e4SLinus Torvalds EXPORT_SYMBOL(__break_lease);
14461da177e4SLinus Torvalds 
14471da177e4SLinus Torvalds /**
1448a6b91919SRandy Dunlap  *	lease_get_mtime - get the last modified time of an inode
14491da177e4SLinus Torvalds  *	@inode: the inode
14501da177e4SLinus Torvalds  *      @time:  pointer to a timespec which will contain the last modified time
14511da177e4SLinus Torvalds  *
14521da177e4SLinus Torvalds  * This is to force NFS clients to flush their caches for files with
14531da177e4SLinus Torvalds  * exclusive leases.  The justification is that if someone has an
1454a6b91919SRandy Dunlap  * exclusive lease, then they could be modifying it.
14551da177e4SLinus Torvalds  */
14561da177e4SLinus Torvalds void lease_get_mtime(struct inode *inode, struct timespec *time)
14571da177e4SLinus Torvalds {
1458bfe86024SJeff Layton 	bool has_lease = false;
1459bfe86024SJeff Layton 	struct file_lock *flock;
1460bfe86024SJeff Layton 
1461bfe86024SJeff Layton 	if (inode->i_flock) {
1462bfe86024SJeff Layton 		spin_lock(&inode->i_lock);
1463bfe86024SJeff Layton 		flock = inode->i_flock;
14640ee5c6d6SJeff Layton 		if (flock && IS_LEASE(flock) && (flock->fl_type == F_WRLCK))
1465bfe86024SJeff Layton 			has_lease = true;
1466bfe86024SJeff Layton 		spin_unlock(&inode->i_lock);
1467bfe86024SJeff Layton 	}
1468bfe86024SJeff Layton 
1469bfe86024SJeff Layton 	if (has_lease)
14701da177e4SLinus Torvalds 		*time = current_fs_time(inode->i_sb);
14711da177e4SLinus Torvalds 	else
14721da177e4SLinus Torvalds 		*time = inode->i_mtime;
14731da177e4SLinus Torvalds }
14741da177e4SLinus Torvalds 
14751da177e4SLinus Torvalds EXPORT_SYMBOL(lease_get_mtime);
14761da177e4SLinus Torvalds 
14771da177e4SLinus Torvalds /**
14781da177e4SLinus Torvalds  *	fcntl_getlease - Enquire what lease is currently active
14791da177e4SLinus Torvalds  *	@filp: the file
14801da177e4SLinus Torvalds  *
14811da177e4SLinus Torvalds  *	The value returned by this function will be one of
14821da177e4SLinus Torvalds  *	(if no lease break is pending):
14831da177e4SLinus Torvalds  *
14841da177e4SLinus Torvalds  *	%F_RDLCK to indicate a shared lease is held.
14851da177e4SLinus Torvalds  *
14861da177e4SLinus Torvalds  *	%F_WRLCK to indicate an exclusive lease is held.
14871da177e4SLinus Torvalds  *
14881da177e4SLinus Torvalds  *	%F_UNLCK to indicate no lease is held.
14891da177e4SLinus Torvalds  *
14901da177e4SLinus Torvalds  *	(if a lease break is pending):
14911da177e4SLinus Torvalds  *
14921da177e4SLinus Torvalds  *	%F_RDLCK to indicate an exclusive lease needs to be
14931da177e4SLinus Torvalds  *		changed to a shared lease (or removed).
14941da177e4SLinus Torvalds  *
14951da177e4SLinus Torvalds  *	%F_UNLCK to indicate the lease needs to be removed.
14961da177e4SLinus Torvalds  *
14971da177e4SLinus Torvalds  *	XXX: sfr & willy disagree over whether F_INPROGRESS
14981da177e4SLinus Torvalds  *	should be returned to userspace.
14991da177e4SLinus Torvalds  */
15001da177e4SLinus Torvalds int fcntl_getlease(struct file *filp)
15011da177e4SLinus Torvalds {
15021da177e4SLinus Torvalds 	struct file_lock *fl;
15031c8c601aSJeff Layton 	struct inode *inode = file_inode(filp);
15041da177e4SLinus Torvalds 	int type = F_UNLCK;
15051da177e4SLinus Torvalds 
15061c8c601aSJeff Layton 	spin_lock(&inode->i_lock);
1507496ad9aaSAl Viro 	time_out_leases(file_inode(filp));
1508496ad9aaSAl Viro 	for (fl = file_inode(filp)->i_flock; fl && IS_LEASE(fl);
15091da177e4SLinus Torvalds 			fl = fl->fl_next) {
15101da177e4SLinus Torvalds 		if (fl->fl_file == filp) {
1511778fc546SJ. Bruce Fields 			type = target_leasetype(fl);
15121da177e4SLinus Torvalds 			break;
15131da177e4SLinus Torvalds 		}
15141da177e4SLinus Torvalds 	}
15151c8c601aSJeff Layton 	spin_unlock(&inode->i_lock);
15161da177e4SLinus Torvalds 	return type;
15171da177e4SLinus Torvalds }
15181da177e4SLinus Torvalds 
151924cbe784SJeff Layton /**
152024cbe784SJeff Layton  * check_conflicting_open - see if the given dentry points to a file that has
152124cbe784SJeff Layton  * 			    an existing open that would conflict with the
152224cbe784SJeff Layton  * 			    desired lease.
152324cbe784SJeff Layton  * @dentry:	dentry to check
152424cbe784SJeff Layton  * @arg:	type of lease that we're trying to acquire
152524cbe784SJeff Layton  *
152624cbe784SJeff Layton  * Check to see if there's an existing open fd on this file that would
152724cbe784SJeff Layton  * conflict with the lease we're trying to set.
152824cbe784SJeff Layton  */
152924cbe784SJeff Layton static int
153024cbe784SJeff Layton check_conflicting_open(const struct dentry *dentry, const long arg)
153124cbe784SJeff Layton {
153224cbe784SJeff Layton 	int ret = 0;
153324cbe784SJeff Layton 	struct inode *inode = dentry->d_inode;
153424cbe784SJeff Layton 
153524cbe784SJeff Layton 	if ((arg == F_RDLCK) && (atomic_read(&inode->i_writecount) > 0))
153624cbe784SJeff Layton 		return -EAGAIN;
153724cbe784SJeff Layton 
153824cbe784SJeff Layton 	if ((arg == F_WRLCK) && ((d_count(dentry) > 1) ||
153924cbe784SJeff Layton 	    (atomic_read(&inode->i_count) > 1)))
154024cbe784SJeff Layton 		ret = -EAGAIN;
154124cbe784SJeff Layton 
154224cbe784SJeff Layton 	return ret;
154324cbe784SJeff Layton }
154424cbe784SJeff Layton 
1545*e6f5c789SJeff Layton static int
1546*e6f5c789SJeff Layton generic_add_lease(struct file *filp, long arg, struct file_lock **flp, void **priv)
15471da177e4SLinus Torvalds {
15487eaae282SKAMBAROV, ZAUR 	struct file_lock *fl, **before, **my_before = NULL, *lease;
15490f7fc9e4SJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
15501da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
1551df4e8d2cSJ. Bruce Fields 	bool is_deleg = (*flp)->fl_flags & FL_DELEG;
1552c1f24ef4SJ. Bruce Fields 	int error;
15531da177e4SLinus Torvalds 
1554096657b6SJ. Bruce Fields 	lease = *flp;
155562af4f1fSJeff Layton 	trace_generic_add_lease(inode, lease);
155662af4f1fSJeff Layton 
1557df4e8d2cSJ. Bruce Fields 	/*
1558df4e8d2cSJ. Bruce Fields 	 * In the delegation case we need mutual exclusion with
1559df4e8d2cSJ. Bruce Fields 	 * a number of operations that take the i_mutex.  We trylock
1560df4e8d2cSJ. Bruce Fields 	 * because delegations are an optional optimization, and if
1561df4e8d2cSJ. Bruce Fields 	 * there's some chance of a conflict--we'd rather not
1562df4e8d2cSJ. Bruce Fields 	 * bother, maybe that's a sign this just isn't a good file to
1563df4e8d2cSJ. Bruce Fields 	 * hand out a delegation on.
1564df4e8d2cSJ. Bruce Fields 	 */
1565df4e8d2cSJ. Bruce Fields 	if (is_deleg && !mutex_trylock(&inode->i_mutex))
1566df4e8d2cSJ. Bruce Fields 		return -EAGAIN;
1567df4e8d2cSJ. Bruce Fields 
1568df4e8d2cSJ. Bruce Fields 	if (is_deleg && arg == F_WRLCK) {
1569df4e8d2cSJ. Bruce Fields 		/* Write delegations are not currently supported: */
15704fdb793fSDan Carpenter 		mutex_unlock(&inode->i_mutex);
1571df4e8d2cSJ. Bruce Fields 		WARN_ON_ONCE(1);
1572df4e8d2cSJ. Bruce Fields 		return -EINVAL;
1573df4e8d2cSJ. Bruce Fields 	}
1574096657b6SJ. Bruce Fields 
157524cbe784SJeff Layton 	error = check_conflicting_open(dentry, arg);
157624cbe784SJeff Layton 	if (error)
15771da177e4SLinus Torvalds 		goto out;
157885c59580SPavel Emelyanov 
15791da177e4SLinus Torvalds 	/*
15801da177e4SLinus Torvalds 	 * At this point, we know that if there is an exclusive
15811da177e4SLinus Torvalds 	 * lease on this file, then we hold it on this filp
15821da177e4SLinus Torvalds 	 * (otherwise our open of this file would have blocked).
15831da177e4SLinus Torvalds 	 * And if we are trying to acquire an exclusive lease,
15841da177e4SLinus Torvalds 	 * then the file is not open by anyone (including us)
15851da177e4SLinus Torvalds 	 * except for this filp.
15861da177e4SLinus Torvalds 	 */
1587c1f24ef4SJ. Bruce Fields 	error = -EAGAIN;
15881da177e4SLinus Torvalds 	for (before = &inode->i_flock;
15891da177e4SLinus Torvalds 			((fl = *before) != NULL) && IS_LEASE(fl);
15901da177e4SLinus Torvalds 			before = &fl->fl_next) {
1591c1f24ef4SJ. Bruce Fields 		if (fl->fl_file == filp) {
15921da177e4SLinus Torvalds 			my_before = before;
1593c1f24ef4SJ. Bruce Fields 			continue;
15941da177e4SLinus Torvalds 		}
1595c1f24ef4SJ. Bruce Fields 		/*
1596c1f24ef4SJ. Bruce Fields 		 * No exclusive leases if someone else has a lease on
1597c1f24ef4SJ. Bruce Fields 		 * this file:
1598c1f24ef4SJ. Bruce Fields 		 */
1599c1f24ef4SJ. Bruce Fields 		if (arg == F_WRLCK)
16001da177e4SLinus Torvalds 			goto out;
1601c1f24ef4SJ. Bruce Fields 		/*
1602c1f24ef4SJ. Bruce Fields 		 * Modifying our existing lease is OK, but no getting a
1603c1f24ef4SJ. Bruce Fields 		 * new lease if someone else is opening for write:
1604c1f24ef4SJ. Bruce Fields 		 */
1605c1f24ef4SJ. Bruce Fields 		if (fl->fl_flags & FL_UNLOCK_PENDING)
1606c1f24ef4SJ. Bruce Fields 			goto out;
1607c1f24ef4SJ. Bruce Fields 	}
16081da177e4SLinus Torvalds 
16091da177e4SLinus Torvalds 	if (my_before != NULL) {
16108fb47a4fSJ. Bruce Fields 		error = lease->fl_lmops->lm_change(my_before, arg);
161151ee4b84SChristoph Hellwig 		if (!error)
161251ee4b84SChristoph Hellwig 			*flp = *my_before;
16131da177e4SLinus Torvalds 		goto out;
16141da177e4SLinus Torvalds 	}
16151da177e4SLinus Torvalds 
16161da177e4SLinus Torvalds 	error = -EINVAL;
16171da177e4SLinus Torvalds 	if (!leases_enable)
16181da177e4SLinus Torvalds 		goto out;
16191da177e4SLinus Torvalds 
1620c5b1f0d9SArnd Bergmann 	locks_insert_lock(before, lease);
162124cbe784SJeff Layton 	/*
162224cbe784SJeff Layton 	 * The check in break_lease() is lockless. It's possible for another
162324cbe784SJeff Layton 	 * open to race in after we did the earlier check for a conflicting
162424cbe784SJeff Layton 	 * open but before the lease was inserted. Check again for a
162524cbe784SJeff Layton 	 * conflicting open and cancel the lease if there is one.
162624cbe784SJeff Layton 	 *
162724cbe784SJeff Layton 	 * We also add a barrier here to ensure that the insertion of the lock
162824cbe784SJeff Layton 	 * precedes these checks.
162924cbe784SJeff Layton 	 */
163024cbe784SJeff Layton 	smp_mb();
163124cbe784SJeff Layton 	error = check_conflicting_open(dentry, arg);
163224cbe784SJeff Layton 	if (error)
1633*e6f5c789SJeff Layton 		goto out_unlink;
16341da177e4SLinus Torvalds out:
1635df4e8d2cSJ. Bruce Fields 	if (is_deleg)
1636df4e8d2cSJ. Bruce Fields 		mutex_unlock(&inode->i_mutex);
16371da177e4SLinus Torvalds 	return error;
1638*e6f5c789SJeff Layton out_unlink:
1639*e6f5c789SJeff Layton 	locks_unlink_lock(before);
1640*e6f5c789SJeff Layton 	goto out;
16411da177e4SLinus Torvalds }
16428335ebd9SJ. Bruce Fields 
16430efaa7e8SJeff Layton static int generic_delete_lease(struct file *filp)
16448335ebd9SJ. Bruce Fields {
16450efaa7e8SJeff Layton 	int error = -EAGAIN;
16468335ebd9SJ. Bruce Fields 	struct file_lock *fl, **before;
16478335ebd9SJ. Bruce Fields 	struct dentry *dentry = filp->f_path.dentry;
16488335ebd9SJ. Bruce Fields 	struct inode *inode = dentry->d_inode;
16498335ebd9SJ. Bruce Fields 
16508335ebd9SJ. Bruce Fields 	for (before = &inode->i_flock;
16518335ebd9SJ. Bruce Fields 			((fl = *before) != NULL) && IS_LEASE(fl);
16528335ebd9SJ. Bruce Fields 			before = &fl->fl_next) {
16530efaa7e8SJeff Layton 		if (fl->fl_file == filp)
16540efaa7e8SJeff Layton 			break;
16558335ebd9SJ. Bruce Fields 	}
16560efaa7e8SJeff Layton 	trace_generic_delete_lease(inode, fl);
16570efaa7e8SJeff Layton 	if (fl)
16580efaa7e8SJeff Layton 		error = fl->fl_lmops->lm_change(before, F_UNLCK);
16590efaa7e8SJeff Layton 	return error;
16608335ebd9SJ. Bruce Fields }
16618335ebd9SJ. Bruce Fields 
16621da177e4SLinus Torvalds /**
16631da177e4SLinus Torvalds  *	generic_setlease	-	sets a lease on an open file
16641da177e4SLinus Torvalds  *	@filp: file pointer
16651da177e4SLinus Torvalds  *	@arg: type of lease to obtain
16661da177e4SLinus Torvalds  *	@flp: input - file_lock to use, output - file_lock inserted
1667*e6f5c789SJeff Layton  *	@priv: private data for lm_setup
16681da177e4SLinus Torvalds  *
16691da177e4SLinus Torvalds  *	The (input) flp->fl_lmops->lm_break function is required
16701da177e4SLinus Torvalds  *	by break_lease().
16711da177e4SLinus Torvalds  *
16721c8c601aSJeff Layton  *	Called with inode->i_lock held.
16731da177e4SLinus Torvalds  */
1674*e6f5c789SJeff Layton int generic_setlease(struct file *filp, long arg, struct file_lock **flp,
1675*e6f5c789SJeff Layton 			void **priv)
16761da177e4SLinus Torvalds {
16771da177e4SLinus Torvalds 	struct dentry *dentry = filp->f_path.dentry;
16781da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
16798335ebd9SJ. Bruce Fields 	int error;
16801da177e4SLinus Torvalds 
16818e96e3b7SEric W. Biederman 	if ((!uid_eq(current_fsuid(), inode->i_uid)) && !capable(CAP_LEASE))
16828335ebd9SJ. Bruce Fields 		return -EACCES;
16831da177e4SLinus Torvalds 	if (!S_ISREG(inode->i_mode))
16848335ebd9SJ. Bruce Fields 		return -EINVAL;
16851da177e4SLinus Torvalds 	error = security_file_lock(filp, arg);
16861da177e4SLinus Torvalds 	if (error)
16878335ebd9SJ. Bruce Fields 		return error;
16881da177e4SLinus Torvalds 
16891da177e4SLinus Torvalds 	time_out_leases(inode);
16901da177e4SLinus Torvalds 
16918335ebd9SJ. Bruce Fields 	switch (arg) {
16928335ebd9SJ. Bruce Fields 	case F_UNLCK:
16930efaa7e8SJeff Layton 		return generic_delete_lease(filp);
16948335ebd9SJ. Bruce Fields 	case F_RDLCK:
16958335ebd9SJ. Bruce Fields 	case F_WRLCK:
16960efaa7e8SJeff Layton 		if (!(*flp)->fl_lmops->lm_break) {
16970efaa7e8SJeff Layton 			WARN_ON_ONCE(1);
16980efaa7e8SJeff Layton 			return -ENOLCK;
16990efaa7e8SJeff Layton 		}
1700*e6f5c789SJeff Layton 		return generic_add_lease(filp, arg, flp, priv);
17018335ebd9SJ. Bruce Fields 	default:
17028d657eb3SDave Jones 		return -EINVAL;
17031da177e4SLinus Torvalds 	}
17041da177e4SLinus Torvalds }
17050af1a450SChristoph Hellwig EXPORT_SYMBOL(generic_setlease);
17061da177e4SLinus Torvalds 
1707*e6f5c789SJeff Layton static int
1708*e6f5c789SJeff Layton __vfs_setlease(struct file *filp, long arg, struct file_lock **lease, void **priv)
1709b89f4321SArnd Bergmann {
171072c2d531SAl Viro 	if (filp->f_op->setlease)
1711*e6f5c789SJeff Layton 		return filp->f_op->setlease(filp, arg, lease, priv);
1712b89f4321SArnd Bergmann 	else
1713*e6f5c789SJeff Layton 		return generic_setlease(filp, arg, lease, priv);
1714b89f4321SArnd Bergmann }
1715b89f4321SArnd Bergmann 
17161da177e4SLinus Torvalds /**
1717a9933ceaSJ. Bruce Fields  * vfs_setlease        -       sets a lease on an open file
17181da177e4SLinus Torvalds  * @filp: file pointer
17191da177e4SLinus Torvalds  * @arg: type of lease to obtain
1720e51673aaSJeff Layton  * @lease: file_lock to use when adding a lease
1721*e6f5c789SJeff Layton  * @priv: private info for lm_setup when adding a lease
17221da177e4SLinus Torvalds  *
1723e51673aaSJeff Layton  * Call this to establish a lease on the file. The "lease" argument is not
1724e51673aaSJeff Layton  * used for F_UNLCK requests and may be NULL. For commands that set or alter
1725e51673aaSJeff Layton  * an existing lease, the (*lease)->fl_lmops->lm_break operation must be set;
1726e51673aaSJeff Layton  * if not, this function will return -ENOLCK (and generate a scary-looking
1727e51673aaSJeff Layton  * stack trace).
17281da177e4SLinus Torvalds  */
17291da177e4SLinus Torvalds 
1730*e6f5c789SJeff Layton int
1731*e6f5c789SJeff Layton vfs_setlease(struct file *filp, long arg, struct file_lock **lease, void **priv)
17321da177e4SLinus Torvalds {
17331c8c601aSJeff Layton 	struct inode *inode = file_inode(filp);
17341da177e4SLinus Torvalds 	int error;
17351da177e4SLinus Torvalds 
17361c8c601aSJeff Layton 	spin_lock(&inode->i_lock);
1737*e6f5c789SJeff Layton 	error = __vfs_setlease(filp, arg, lease, priv);
17381c8c601aSJeff Layton 	spin_unlock(&inode->i_lock);
17391da177e4SLinus Torvalds 
17401da177e4SLinus Torvalds 	return error;
17411da177e4SLinus Torvalds }
1742a9933ceaSJ. Bruce Fields EXPORT_SYMBOL_GPL(vfs_setlease);
17431da177e4SLinus Torvalds 
17440ceaf6c7SJ. Bruce Fields static int do_fcntl_add_lease(unsigned int fd, struct file *filp, long arg)
17451da177e4SLinus Torvalds {
17463df057acSJ. Bruce Fields 	struct file_lock *fl, *ret;
17471c8c601aSJeff Layton 	struct inode *inode = file_inode(filp);
1748f7347ce4SLinus Torvalds 	struct fasync_struct *new;
17491da177e4SLinus Torvalds 	int error;
17501da177e4SLinus Torvalds 
1751c5b1f0d9SArnd Bergmann 	fl = lease_alloc(filp, arg);
1752c5b1f0d9SArnd Bergmann 	if (IS_ERR(fl))
1753c5b1f0d9SArnd Bergmann 		return PTR_ERR(fl);
17541da177e4SLinus Torvalds 
1755f7347ce4SLinus Torvalds 	new = fasync_alloc();
1756f7347ce4SLinus Torvalds 	if (!new) {
1757f7347ce4SLinus Torvalds 		locks_free_lock(fl);
1758f7347ce4SLinus Torvalds 		return -ENOMEM;
1759f7347ce4SLinus Torvalds 	}
17603df057acSJ. Bruce Fields 	ret = fl;
17611c8c601aSJeff Layton 	spin_lock(&inode->i_lock);
1762*e6f5c789SJeff Layton 	error = __vfs_setlease(filp, arg, &ret, NULL);
17632dfb928fSJeff Layton 	if (error)
17642dfb928fSJeff Layton 		goto out_unlock;
17652dfb928fSJeff Layton 	if (ret == fl)
17662dfb928fSJeff Layton 		fl = NULL;
17671da177e4SLinus Torvalds 
1768f7347ce4SLinus Torvalds 	/*
1769f7347ce4SLinus Torvalds 	 * fasync_insert_entry() returns the old entry if any.
1770f7347ce4SLinus Torvalds 	 * If there was no old entry, then it used 'new' and
1771f7347ce4SLinus Torvalds 	 * inserted it into the fasync list. Clear new so that
1772f7347ce4SLinus Torvalds 	 * we don't release it here.
1773f7347ce4SLinus Torvalds 	 */
17743df057acSJ. Bruce Fields 	if (!fasync_insert_entry(fd, filp, &ret->fl_fasync, new))
1775f7347ce4SLinus Torvalds 		new = NULL;
1776f7347ce4SLinus Torvalds 
1777e0b93eddSJeff Layton 	__f_setown(filp, task_pid(current), PIDTYPE_PID, 0);
17782dfb928fSJeff Layton out_unlock:
17791c8c601aSJeff Layton 	spin_unlock(&inode->i_lock);
17802dfb928fSJeff Layton 	if (fl)
17812dfb928fSJeff Layton 		locks_free_lock(fl);
1782f7347ce4SLinus Torvalds 	if (new)
1783f7347ce4SLinus Torvalds 		fasync_free(new);
17841da177e4SLinus Torvalds 	return error;
17851da177e4SLinus Torvalds }
17861da177e4SLinus Torvalds 
17871da177e4SLinus Torvalds /**
17880ceaf6c7SJ. Bruce Fields  *	fcntl_setlease	-	sets a lease on an open file
17890ceaf6c7SJ. Bruce Fields  *	@fd: open file descriptor
17900ceaf6c7SJ. Bruce Fields  *	@filp: file pointer
17910ceaf6c7SJ. Bruce Fields  *	@arg: type of lease to obtain
17920ceaf6c7SJ. Bruce Fields  *
17930ceaf6c7SJ. Bruce Fields  *	Call this fcntl to establish a lease on the file.
17940ceaf6c7SJ. Bruce Fields  *	Note that you also need to call %F_SETSIG to
17950ceaf6c7SJ. Bruce Fields  *	receive a signal when the lease is broken.
17960ceaf6c7SJ. Bruce Fields  */
17970ceaf6c7SJ. Bruce Fields int fcntl_setlease(unsigned int fd, struct file *filp, long arg)
17980ceaf6c7SJ. Bruce Fields {
17990ceaf6c7SJ. Bruce Fields 	if (arg == F_UNLCK)
1800*e6f5c789SJeff Layton 		return vfs_setlease(filp, F_UNLCK, NULL, NULL);
18010ceaf6c7SJ. Bruce Fields 	return do_fcntl_add_lease(fd, filp, arg);
18020ceaf6c7SJ. Bruce Fields }
18030ceaf6c7SJ. Bruce Fields 
18040ceaf6c7SJ. Bruce Fields /**
18051da177e4SLinus Torvalds  * flock_lock_file_wait - Apply a FLOCK-style lock to a file
18061da177e4SLinus Torvalds  * @filp: The file to apply the lock to
18071da177e4SLinus Torvalds  * @fl: The lock to be applied
18081da177e4SLinus Torvalds  *
18091da177e4SLinus Torvalds  * Add a FLOCK style lock to a file.
18101da177e4SLinus Torvalds  */
18111da177e4SLinus Torvalds int flock_lock_file_wait(struct file *filp, struct file_lock *fl)
18121da177e4SLinus Torvalds {
18131da177e4SLinus Torvalds 	int error;
18141da177e4SLinus Torvalds 	might_sleep();
18151da177e4SLinus Torvalds 	for (;;) {
18161da177e4SLinus Torvalds 		error = flock_lock_file(filp, fl);
1817bde74e4bSMiklos Szeredi 		if (error != FILE_LOCK_DEFERRED)
18181da177e4SLinus Torvalds 			break;
18191da177e4SLinus Torvalds 		error = wait_event_interruptible(fl->fl_wait, !fl->fl_next);
18201da177e4SLinus Torvalds 		if (!error)
18211da177e4SLinus Torvalds 			continue;
18221da177e4SLinus Torvalds 
18231da177e4SLinus Torvalds 		locks_delete_block(fl);
18241da177e4SLinus Torvalds 		break;
18251da177e4SLinus Torvalds 	}
18261da177e4SLinus Torvalds 	return error;
18271da177e4SLinus Torvalds }
18281da177e4SLinus Torvalds 
18291da177e4SLinus Torvalds EXPORT_SYMBOL(flock_lock_file_wait);
18301da177e4SLinus Torvalds 
18311da177e4SLinus Torvalds /**
18321da177e4SLinus Torvalds  *	sys_flock: - flock() system call.
18331da177e4SLinus Torvalds  *	@fd: the file descriptor to lock.
18341da177e4SLinus Torvalds  *	@cmd: the type of lock to apply.
18351da177e4SLinus Torvalds  *
18361da177e4SLinus Torvalds  *	Apply a %FL_FLOCK style lock to an open file descriptor.
18371da177e4SLinus Torvalds  *	The @cmd can be one of
18381da177e4SLinus Torvalds  *
18391da177e4SLinus Torvalds  *	%LOCK_SH -- a shared lock.
18401da177e4SLinus Torvalds  *
18411da177e4SLinus Torvalds  *	%LOCK_EX -- an exclusive lock.
18421da177e4SLinus Torvalds  *
18431da177e4SLinus Torvalds  *	%LOCK_UN -- remove an existing lock.
18441da177e4SLinus Torvalds  *
18451da177e4SLinus Torvalds  *	%LOCK_MAND -- a `mandatory' flock.  This exists to emulate Windows Share Modes.
18461da177e4SLinus Torvalds  *
18471da177e4SLinus Torvalds  *	%LOCK_MAND can be combined with %LOCK_READ or %LOCK_WRITE to allow other
18481da177e4SLinus Torvalds  *	processes read and write access respectively.
18491da177e4SLinus Torvalds  */
1850002c8976SHeiko Carstens SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd)
18511da177e4SLinus Torvalds {
18522903ff01SAl Viro 	struct fd f = fdget(fd);
18531da177e4SLinus Torvalds 	struct file_lock *lock;
18541da177e4SLinus Torvalds 	int can_sleep, unlock;
18551da177e4SLinus Torvalds 	int error;
18561da177e4SLinus Torvalds 
18571da177e4SLinus Torvalds 	error = -EBADF;
18582903ff01SAl Viro 	if (!f.file)
18591da177e4SLinus Torvalds 		goto out;
18601da177e4SLinus Torvalds 
18611da177e4SLinus Torvalds 	can_sleep = !(cmd & LOCK_NB);
18621da177e4SLinus Torvalds 	cmd &= ~LOCK_NB;
18631da177e4SLinus Torvalds 	unlock = (cmd == LOCK_UN);
18641da177e4SLinus Torvalds 
1865aeb5d727SAl Viro 	if (!unlock && !(cmd & LOCK_MAND) &&
18662903ff01SAl Viro 	    !(f.file->f_mode & (FMODE_READ|FMODE_WRITE)))
18671da177e4SLinus Torvalds 		goto out_putf;
18681da177e4SLinus Torvalds 
18692903ff01SAl Viro 	error = flock_make_lock(f.file, &lock, cmd);
18701da177e4SLinus Torvalds 	if (error)
18711da177e4SLinus Torvalds 		goto out_putf;
18721da177e4SLinus Torvalds 	if (can_sleep)
18731da177e4SLinus Torvalds 		lock->fl_flags |= FL_SLEEP;
18741da177e4SLinus Torvalds 
18752903ff01SAl Viro 	error = security_file_lock(f.file, lock->fl_type);
18761da177e4SLinus Torvalds 	if (error)
18771da177e4SLinus Torvalds 		goto out_free;
18781da177e4SLinus Torvalds 
187972c2d531SAl Viro 	if (f.file->f_op->flock)
18802903ff01SAl Viro 		error = f.file->f_op->flock(f.file,
18811da177e4SLinus Torvalds 					  (can_sleep) ? F_SETLKW : F_SETLK,
18821da177e4SLinus Torvalds 					  lock);
18831da177e4SLinus Torvalds 	else
18842903ff01SAl Viro 		error = flock_lock_file_wait(f.file, lock);
18851da177e4SLinus Torvalds 
18861da177e4SLinus Torvalds  out_free:
18871da177e4SLinus Torvalds 	locks_free_lock(lock);
18881da177e4SLinus Torvalds 
18891da177e4SLinus Torvalds  out_putf:
18902903ff01SAl Viro 	fdput(f);
18911da177e4SLinus Torvalds  out:
18921da177e4SLinus Torvalds 	return error;
18931da177e4SLinus Torvalds }
18941da177e4SLinus Torvalds 
18953ee17abdSJ. Bruce Fields /**
18963ee17abdSJ. Bruce Fields  * vfs_test_lock - test file byte range lock
18973ee17abdSJ. Bruce Fields  * @filp: The file to test lock for
18986924c554SJ. Bruce Fields  * @fl: The lock to test; also used to hold result
18993ee17abdSJ. Bruce Fields  *
19003ee17abdSJ. Bruce Fields  * Returns -ERRNO on failure.  Indicates presence of conflicting lock by
19013ee17abdSJ. Bruce Fields  * setting conf->fl_type to something other than F_UNLCK.
19023ee17abdSJ. Bruce Fields  */
19033ee17abdSJ. Bruce Fields int vfs_test_lock(struct file *filp, struct file_lock *fl)
19043ee17abdSJ. Bruce Fields {
190572c2d531SAl Viro 	if (filp->f_op->lock)
19063ee17abdSJ. Bruce Fields 		return filp->f_op->lock(filp, F_GETLK, fl);
19073ee17abdSJ. Bruce Fields 	posix_test_lock(filp, fl);
19083ee17abdSJ. Bruce Fields 	return 0;
19093ee17abdSJ. Bruce Fields }
19103ee17abdSJ. Bruce Fields EXPORT_SYMBOL_GPL(vfs_test_lock);
19113ee17abdSJ. Bruce Fields 
1912c2fa1b8aSJ. Bruce Fields static int posix_lock_to_flock(struct flock *flock, struct file_lock *fl)
1913c2fa1b8aSJ. Bruce Fields {
1914cff2fce5SJeff Layton 	flock->l_pid = IS_OFDLCK(fl) ? -1 : fl->fl_pid;
1915c2fa1b8aSJ. Bruce Fields #if BITS_PER_LONG == 32
1916c2fa1b8aSJ. Bruce Fields 	/*
1917c2fa1b8aSJ. Bruce Fields 	 * Make sure we can represent the posix lock via
1918c2fa1b8aSJ. Bruce Fields 	 * legacy 32bit flock.
1919c2fa1b8aSJ. Bruce Fields 	 */
1920c2fa1b8aSJ. Bruce Fields 	if (fl->fl_start > OFFT_OFFSET_MAX)
1921c2fa1b8aSJ. Bruce Fields 		return -EOVERFLOW;
1922c2fa1b8aSJ. Bruce Fields 	if (fl->fl_end != OFFSET_MAX && fl->fl_end > OFFT_OFFSET_MAX)
1923c2fa1b8aSJ. Bruce Fields 		return -EOVERFLOW;
1924c2fa1b8aSJ. Bruce Fields #endif
1925c2fa1b8aSJ. Bruce Fields 	flock->l_start = fl->fl_start;
1926c2fa1b8aSJ. Bruce Fields 	flock->l_len = fl->fl_end == OFFSET_MAX ? 0 :
1927c2fa1b8aSJ. Bruce Fields 		fl->fl_end - fl->fl_start + 1;
1928c2fa1b8aSJ. Bruce Fields 	flock->l_whence = 0;
1929129a84deSJ. Bruce Fields 	flock->l_type = fl->fl_type;
1930c2fa1b8aSJ. Bruce Fields 	return 0;
1931c2fa1b8aSJ. Bruce Fields }
1932c2fa1b8aSJ. Bruce Fields 
1933c2fa1b8aSJ. Bruce Fields #if BITS_PER_LONG == 32
1934c2fa1b8aSJ. Bruce Fields static void posix_lock_to_flock64(struct flock64 *flock, struct file_lock *fl)
1935c2fa1b8aSJ. Bruce Fields {
1936cff2fce5SJeff Layton 	flock->l_pid = IS_OFDLCK(fl) ? -1 : fl->fl_pid;
1937c2fa1b8aSJ. Bruce Fields 	flock->l_start = fl->fl_start;
1938c2fa1b8aSJ. Bruce Fields 	flock->l_len = fl->fl_end == OFFSET_MAX ? 0 :
1939c2fa1b8aSJ. Bruce Fields 		fl->fl_end - fl->fl_start + 1;
1940c2fa1b8aSJ. Bruce Fields 	flock->l_whence = 0;
1941c2fa1b8aSJ. Bruce Fields 	flock->l_type = fl->fl_type;
1942c2fa1b8aSJ. Bruce Fields }
1943c2fa1b8aSJ. Bruce Fields #endif
1944c2fa1b8aSJ. Bruce Fields 
19451da177e4SLinus Torvalds /* Report the first existing lock that would conflict with l.
19461da177e4SLinus Torvalds  * This implements the F_GETLK command of fcntl().
19471da177e4SLinus Torvalds  */
1948c1e62b8fSJeff Layton int fcntl_getlk(struct file *filp, unsigned int cmd, struct flock __user *l)
19491da177e4SLinus Torvalds {
19509d6a8c5cSMarc Eshel 	struct file_lock file_lock;
19511da177e4SLinus Torvalds 	struct flock flock;
19521da177e4SLinus Torvalds 	int error;
19531da177e4SLinus Torvalds 
19541da177e4SLinus Torvalds 	error = -EFAULT;
19551da177e4SLinus Torvalds 	if (copy_from_user(&flock, l, sizeof(flock)))
19561da177e4SLinus Torvalds 		goto out;
19571da177e4SLinus Torvalds 	error = -EINVAL;
19581da177e4SLinus Torvalds 	if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK))
19591da177e4SLinus Torvalds 		goto out;
19601da177e4SLinus Torvalds 
19611da177e4SLinus Torvalds 	error = flock_to_posix_lock(filp, &file_lock, &flock);
19621da177e4SLinus Torvalds 	if (error)
19631da177e4SLinus Torvalds 		goto out;
19641da177e4SLinus Torvalds 
19650d3f7a2dSJeff Layton 	if (cmd == F_OFD_GETLK) {
196690478939SJeff Layton 		error = -EINVAL;
196790478939SJeff Layton 		if (flock.l_pid != 0)
196890478939SJeff Layton 			goto out;
196990478939SJeff Layton 
19705d50ffd7SJeff Layton 		cmd = F_GETLK;
1971cff2fce5SJeff Layton 		file_lock.fl_flags |= FL_OFDLCK;
197273a8f5f7SChristoph Hellwig 		file_lock.fl_owner = filp;
19735d50ffd7SJeff Layton 	}
19745d50ffd7SJeff Layton 
19753ee17abdSJ. Bruce Fields 	error = vfs_test_lock(filp, &file_lock);
19763ee17abdSJ. Bruce Fields 	if (error)
19771da177e4SLinus Torvalds 		goto out;
19781da177e4SLinus Torvalds 
19799d6a8c5cSMarc Eshel 	flock.l_type = file_lock.fl_type;
19809d6a8c5cSMarc Eshel 	if (file_lock.fl_type != F_UNLCK) {
19819d6a8c5cSMarc Eshel 		error = posix_lock_to_flock(&flock, &file_lock);
1982c2fa1b8aSJ. Bruce Fields 		if (error)
1983f328296eSKinglong Mee 			goto rel_priv;
19841da177e4SLinus Torvalds 	}
19851da177e4SLinus Torvalds 	error = -EFAULT;
19861da177e4SLinus Torvalds 	if (!copy_to_user(l, &flock, sizeof(flock)))
19871da177e4SLinus Torvalds 		error = 0;
1988f328296eSKinglong Mee rel_priv:
1989f328296eSKinglong Mee 	locks_release_private(&file_lock);
19901da177e4SLinus Torvalds out:
19911da177e4SLinus Torvalds 	return error;
19921da177e4SLinus Torvalds }
19931da177e4SLinus Torvalds 
19947723ec97SMarc Eshel /**
19957723ec97SMarc Eshel  * vfs_lock_file - file byte range lock
19967723ec97SMarc Eshel  * @filp: The file to apply the lock to
19977723ec97SMarc Eshel  * @cmd: type of locking operation (F_SETLK, F_GETLK, etc.)
19987723ec97SMarc Eshel  * @fl: The lock to be applied
1999150b3934SMarc Eshel  * @conf: Place to return a copy of the conflicting lock, if found.
2000150b3934SMarc Eshel  *
2001150b3934SMarc Eshel  * A caller that doesn't care about the conflicting lock may pass NULL
2002150b3934SMarc Eshel  * as the final argument.
2003150b3934SMarc Eshel  *
2004150b3934SMarc Eshel  * If the filesystem defines a private ->lock() method, then @conf will
2005150b3934SMarc Eshel  * be left unchanged; so a caller that cares should initialize it to
2006150b3934SMarc Eshel  * some acceptable default.
20072beb6614SMarc Eshel  *
20082beb6614SMarc Eshel  * To avoid blocking kernel daemons, such as lockd, that need to acquire POSIX
20092beb6614SMarc Eshel  * locks, the ->lock() interface may return asynchronously, before the lock has
20102beb6614SMarc Eshel  * been granted or denied by the underlying filesystem, if (and only if)
20118fb47a4fSJ. Bruce Fields  * lm_grant is set. Callers expecting ->lock() to return asynchronously
20122beb6614SMarc Eshel  * will only use F_SETLK, not F_SETLKW; they will set FL_SLEEP if (and only if)
20132beb6614SMarc Eshel  * the request is for a blocking lock. When ->lock() does return asynchronously,
20148fb47a4fSJ. Bruce Fields  * it must return FILE_LOCK_DEFERRED, and call ->lm_grant() when the lock
20152beb6614SMarc Eshel  * request completes.
20162beb6614SMarc Eshel  * If the request is for non-blocking lock the file system should return
2017bde74e4bSMiklos Szeredi  * FILE_LOCK_DEFERRED then try to get the lock and call the callback routine
2018bde74e4bSMiklos Szeredi  * with the result. If the request timed out the callback routine will return a
20192beb6614SMarc Eshel  * nonzero return code and the file system should release the lock. The file
20202beb6614SMarc Eshel  * system is also responsible to keep a corresponding posix lock when it
20212beb6614SMarc Eshel  * grants a lock so the VFS can find out which locks are locally held and do
20222beb6614SMarc Eshel  * the correct lock cleanup when required.
20232beb6614SMarc Eshel  * The underlying filesystem must not drop the kernel lock or call
20248fb47a4fSJ. Bruce Fields  * ->lm_grant() before returning to the caller with a FILE_LOCK_DEFERRED
20252beb6614SMarc Eshel  * return code.
20267723ec97SMarc Eshel  */
2027150b3934SMarc Eshel int vfs_lock_file(struct file *filp, unsigned int cmd, struct file_lock *fl, struct file_lock *conf)
20287723ec97SMarc Eshel {
202972c2d531SAl Viro 	if (filp->f_op->lock)
20307723ec97SMarc Eshel 		return filp->f_op->lock(filp, cmd, fl);
20317723ec97SMarc Eshel 	else
2032150b3934SMarc Eshel 		return posix_lock_file(filp, fl, conf);
20337723ec97SMarc Eshel }
20347723ec97SMarc Eshel EXPORT_SYMBOL_GPL(vfs_lock_file);
20357723ec97SMarc Eshel 
2036b648a6deSMiklos Szeredi static int do_lock_file_wait(struct file *filp, unsigned int cmd,
2037b648a6deSMiklos Szeredi 			     struct file_lock *fl)
2038b648a6deSMiklos Szeredi {
2039b648a6deSMiklos Szeredi 	int error;
2040b648a6deSMiklos Szeredi 
2041b648a6deSMiklos Szeredi 	error = security_file_lock(filp, fl->fl_type);
2042b648a6deSMiklos Szeredi 	if (error)
2043b648a6deSMiklos Szeredi 		return error;
2044b648a6deSMiklos Szeredi 
2045b648a6deSMiklos Szeredi 	for (;;) {
2046764c76b3SMiklos Szeredi 		error = vfs_lock_file(filp, cmd, fl, NULL);
2047b648a6deSMiklos Szeredi 		if (error != FILE_LOCK_DEFERRED)
2048b648a6deSMiklos Szeredi 			break;
2049764c76b3SMiklos Szeredi 		error = wait_event_interruptible(fl->fl_wait, !fl->fl_next);
2050b648a6deSMiklos Szeredi 		if (!error)
2051b648a6deSMiklos Szeredi 			continue;
2052b648a6deSMiklos Szeredi 
2053b648a6deSMiklos Szeredi 		locks_delete_block(fl);
2054b648a6deSMiklos Szeredi 		break;
2055b648a6deSMiklos Szeredi 	}
2056b648a6deSMiklos Szeredi 
2057b648a6deSMiklos Szeredi 	return error;
2058b648a6deSMiklos Szeredi }
2059b648a6deSMiklos Szeredi 
2060cf01f4eeSJeff Layton /* Ensure that fl->fl_filp has compatible f_mode for F_SETLK calls */
2061cf01f4eeSJeff Layton static int
2062cf01f4eeSJeff Layton check_fmode_for_setlk(struct file_lock *fl)
2063cf01f4eeSJeff Layton {
2064cf01f4eeSJeff Layton 	switch (fl->fl_type) {
2065cf01f4eeSJeff Layton 	case F_RDLCK:
2066cf01f4eeSJeff Layton 		if (!(fl->fl_file->f_mode & FMODE_READ))
2067cf01f4eeSJeff Layton 			return -EBADF;
2068cf01f4eeSJeff Layton 		break;
2069cf01f4eeSJeff Layton 	case F_WRLCK:
2070cf01f4eeSJeff Layton 		if (!(fl->fl_file->f_mode & FMODE_WRITE))
2071cf01f4eeSJeff Layton 			return -EBADF;
2072cf01f4eeSJeff Layton 	}
2073cf01f4eeSJeff Layton 	return 0;
2074cf01f4eeSJeff Layton }
2075cf01f4eeSJeff Layton 
20761da177e4SLinus Torvalds /* Apply the lock described by l to an open file descriptor.
20771da177e4SLinus Torvalds  * This implements both the F_SETLK and F_SETLKW commands of fcntl().
20781da177e4SLinus Torvalds  */
2079c293621bSPeter Staubach int fcntl_setlk(unsigned int fd, struct file *filp, unsigned int cmd,
2080c293621bSPeter Staubach 		struct flock __user *l)
20811da177e4SLinus Torvalds {
20821da177e4SLinus Torvalds 	struct file_lock *file_lock = locks_alloc_lock();
20831da177e4SLinus Torvalds 	struct flock flock;
20841da177e4SLinus Torvalds 	struct inode *inode;
20850b2bac2fSAl Viro 	struct file *f;
20861da177e4SLinus Torvalds 	int error;
20871da177e4SLinus Torvalds 
20881da177e4SLinus Torvalds 	if (file_lock == NULL)
20891da177e4SLinus Torvalds 		return -ENOLCK;
20901da177e4SLinus Torvalds 
20911da177e4SLinus Torvalds 	/*
20921da177e4SLinus Torvalds 	 * This might block, so we do it before checking the inode.
20931da177e4SLinus Torvalds 	 */
20941da177e4SLinus Torvalds 	error = -EFAULT;
20951da177e4SLinus Torvalds 	if (copy_from_user(&flock, l, sizeof(flock)))
20961da177e4SLinus Torvalds 		goto out;
20971da177e4SLinus Torvalds 
2098496ad9aaSAl Viro 	inode = file_inode(filp);
20991da177e4SLinus Torvalds 
21001da177e4SLinus Torvalds 	/* Don't allow mandatory locks on files that may be memory mapped
21011da177e4SLinus Torvalds 	 * and shared.
21021da177e4SLinus Torvalds 	 */
2103a16877caSPavel Emelyanov 	if (mandatory_lock(inode) && mapping_writably_mapped(filp->f_mapping)) {
21041da177e4SLinus Torvalds 		error = -EAGAIN;
21051da177e4SLinus Torvalds 		goto out;
21061da177e4SLinus Torvalds 	}
21071da177e4SLinus Torvalds 
2108c293621bSPeter Staubach again:
21091da177e4SLinus Torvalds 	error = flock_to_posix_lock(filp, file_lock, &flock);
21101da177e4SLinus Torvalds 	if (error)
21111da177e4SLinus Torvalds 		goto out;
21125d50ffd7SJeff Layton 
2113cf01f4eeSJeff Layton 	error = check_fmode_for_setlk(file_lock);
2114cf01f4eeSJeff Layton 	if (error)
2115cf01f4eeSJeff Layton 		goto out;
2116cf01f4eeSJeff Layton 
21175d50ffd7SJeff Layton 	/*
21185d50ffd7SJeff Layton 	 * If the cmd is requesting file-private locks, then set the
2119cff2fce5SJeff Layton 	 * FL_OFDLCK flag and override the owner.
21205d50ffd7SJeff Layton 	 */
21215d50ffd7SJeff Layton 	switch (cmd) {
21220d3f7a2dSJeff Layton 	case F_OFD_SETLK:
212390478939SJeff Layton 		error = -EINVAL;
212490478939SJeff Layton 		if (flock.l_pid != 0)
212590478939SJeff Layton 			goto out;
212690478939SJeff Layton 
21275d50ffd7SJeff Layton 		cmd = F_SETLK;
2128cff2fce5SJeff Layton 		file_lock->fl_flags |= FL_OFDLCK;
212973a8f5f7SChristoph Hellwig 		file_lock->fl_owner = filp;
21305d50ffd7SJeff Layton 		break;
21310d3f7a2dSJeff Layton 	case F_OFD_SETLKW:
213290478939SJeff Layton 		error = -EINVAL;
213390478939SJeff Layton 		if (flock.l_pid != 0)
213490478939SJeff Layton 			goto out;
213590478939SJeff Layton 
21365d50ffd7SJeff Layton 		cmd = F_SETLKW;
2137cff2fce5SJeff Layton 		file_lock->fl_flags |= FL_OFDLCK;
213873a8f5f7SChristoph Hellwig 		file_lock->fl_owner = filp;
21395d50ffd7SJeff Layton 		/* Fallthrough */
21405d50ffd7SJeff Layton 	case F_SETLKW:
21411da177e4SLinus Torvalds 		file_lock->fl_flags |= FL_SLEEP;
21421da177e4SLinus Torvalds 	}
21431da177e4SLinus Torvalds 
2144b648a6deSMiklos Szeredi 	error = do_lock_file_wait(filp, cmd, file_lock);
2145c293621bSPeter Staubach 
2146c293621bSPeter Staubach 	/*
2147c293621bSPeter Staubach 	 * Attempt to detect a close/fcntl race and recover by
2148c293621bSPeter Staubach 	 * releasing the lock that was just acquired.
2149c293621bSPeter Staubach 	 */
21500b2bac2fSAl Viro 	/*
21510b2bac2fSAl Viro 	 * we need that spin_lock here - it prevents reordering between
21520b2bac2fSAl Viro 	 * update of inode->i_flock and check for it done in close().
21530b2bac2fSAl Viro 	 * rcu_read_lock() wouldn't do.
21540b2bac2fSAl Viro 	 */
21550b2bac2fSAl Viro 	spin_lock(&current->files->file_lock);
21560b2bac2fSAl Viro 	f = fcheck(fd);
21570b2bac2fSAl Viro 	spin_unlock(&current->files->file_lock);
21580b2bac2fSAl Viro 	if (!error && f != filp && flock.l_type != F_UNLCK) {
2159c293621bSPeter Staubach 		flock.l_type = F_UNLCK;
2160c293621bSPeter Staubach 		goto again;
2161c293621bSPeter Staubach 	}
21621da177e4SLinus Torvalds 
21631da177e4SLinus Torvalds out:
21641da177e4SLinus Torvalds 	locks_free_lock(file_lock);
21651da177e4SLinus Torvalds 	return error;
21661da177e4SLinus Torvalds }
21671da177e4SLinus Torvalds 
21681da177e4SLinus Torvalds #if BITS_PER_LONG == 32
21691da177e4SLinus Torvalds /* Report the first existing lock that would conflict with l.
21701da177e4SLinus Torvalds  * This implements the F_GETLK command of fcntl().
21711da177e4SLinus Torvalds  */
2172c1e62b8fSJeff Layton int fcntl_getlk64(struct file *filp, unsigned int cmd, struct flock64 __user *l)
21731da177e4SLinus Torvalds {
21749d6a8c5cSMarc Eshel 	struct file_lock file_lock;
21751da177e4SLinus Torvalds 	struct flock64 flock;
21761da177e4SLinus Torvalds 	int error;
21771da177e4SLinus Torvalds 
21781da177e4SLinus Torvalds 	error = -EFAULT;
21791da177e4SLinus Torvalds 	if (copy_from_user(&flock, l, sizeof(flock)))
21801da177e4SLinus Torvalds 		goto out;
21811da177e4SLinus Torvalds 	error = -EINVAL;
21821da177e4SLinus Torvalds 	if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK))
21831da177e4SLinus Torvalds 		goto out;
21841da177e4SLinus Torvalds 
21851da177e4SLinus Torvalds 	error = flock64_to_posix_lock(filp, &file_lock, &flock);
21861da177e4SLinus Torvalds 	if (error)
21871da177e4SLinus Torvalds 		goto out;
21881da177e4SLinus Torvalds 
21890d3f7a2dSJeff Layton 	if (cmd == F_OFD_GETLK) {
219090478939SJeff Layton 		error = -EINVAL;
219190478939SJeff Layton 		if (flock.l_pid != 0)
219290478939SJeff Layton 			goto out;
219390478939SJeff Layton 
21945d50ffd7SJeff Layton 		cmd = F_GETLK64;
2195cff2fce5SJeff Layton 		file_lock.fl_flags |= FL_OFDLCK;
219673a8f5f7SChristoph Hellwig 		file_lock.fl_owner = filp;
21975d50ffd7SJeff Layton 	}
21985d50ffd7SJeff Layton 
21993ee17abdSJ. Bruce Fields 	error = vfs_test_lock(filp, &file_lock);
22003ee17abdSJ. Bruce Fields 	if (error)
22011da177e4SLinus Torvalds 		goto out;
22021da177e4SLinus Torvalds 
22039d6a8c5cSMarc Eshel 	flock.l_type = file_lock.fl_type;
22049d6a8c5cSMarc Eshel 	if (file_lock.fl_type != F_UNLCK)
22059d6a8c5cSMarc Eshel 		posix_lock_to_flock64(&flock, &file_lock);
22069d6a8c5cSMarc Eshel 
22071da177e4SLinus Torvalds 	error = -EFAULT;
22081da177e4SLinus Torvalds 	if (!copy_to_user(l, &flock, sizeof(flock)))
22091da177e4SLinus Torvalds 		error = 0;
22101da177e4SLinus Torvalds 
2211f328296eSKinglong Mee 	locks_release_private(&file_lock);
22121da177e4SLinus Torvalds out:
22131da177e4SLinus Torvalds 	return error;
22141da177e4SLinus Torvalds }
22151da177e4SLinus Torvalds 
22161da177e4SLinus Torvalds /* Apply the lock described by l to an open file descriptor.
22171da177e4SLinus Torvalds  * This implements both the F_SETLK and F_SETLKW commands of fcntl().
22181da177e4SLinus Torvalds  */
2219c293621bSPeter Staubach int fcntl_setlk64(unsigned int fd, struct file *filp, unsigned int cmd,
2220c293621bSPeter Staubach 		struct flock64 __user *l)
22211da177e4SLinus Torvalds {
22221da177e4SLinus Torvalds 	struct file_lock *file_lock = locks_alloc_lock();
22231da177e4SLinus Torvalds 	struct flock64 flock;
22241da177e4SLinus Torvalds 	struct inode *inode;
22250b2bac2fSAl Viro 	struct file *f;
22261da177e4SLinus Torvalds 	int error;
22271da177e4SLinus Torvalds 
22281da177e4SLinus Torvalds 	if (file_lock == NULL)
22291da177e4SLinus Torvalds 		return -ENOLCK;
22301da177e4SLinus Torvalds 
22311da177e4SLinus Torvalds 	/*
22321da177e4SLinus Torvalds 	 * This might block, so we do it before checking the inode.
22331da177e4SLinus Torvalds 	 */
22341da177e4SLinus Torvalds 	error = -EFAULT;
22351da177e4SLinus Torvalds 	if (copy_from_user(&flock, l, sizeof(flock)))
22361da177e4SLinus Torvalds 		goto out;
22371da177e4SLinus Torvalds 
2238496ad9aaSAl Viro 	inode = file_inode(filp);
22391da177e4SLinus Torvalds 
22401da177e4SLinus Torvalds 	/* Don't allow mandatory locks on files that may be memory mapped
22411da177e4SLinus Torvalds 	 * and shared.
22421da177e4SLinus Torvalds 	 */
2243a16877caSPavel Emelyanov 	if (mandatory_lock(inode) && mapping_writably_mapped(filp->f_mapping)) {
22441da177e4SLinus Torvalds 		error = -EAGAIN;
22451da177e4SLinus Torvalds 		goto out;
22461da177e4SLinus Torvalds 	}
22471da177e4SLinus Torvalds 
2248c293621bSPeter Staubach again:
22491da177e4SLinus Torvalds 	error = flock64_to_posix_lock(filp, file_lock, &flock);
22501da177e4SLinus Torvalds 	if (error)
22511da177e4SLinus Torvalds 		goto out;
22525d50ffd7SJeff Layton 
2253cf01f4eeSJeff Layton 	error = check_fmode_for_setlk(file_lock);
2254cf01f4eeSJeff Layton 	if (error)
2255cf01f4eeSJeff Layton 		goto out;
2256cf01f4eeSJeff Layton 
22575d50ffd7SJeff Layton 	/*
22585d50ffd7SJeff Layton 	 * If the cmd is requesting file-private locks, then set the
2259cff2fce5SJeff Layton 	 * FL_OFDLCK flag and override the owner.
22605d50ffd7SJeff Layton 	 */
22615d50ffd7SJeff Layton 	switch (cmd) {
22620d3f7a2dSJeff Layton 	case F_OFD_SETLK:
226390478939SJeff Layton 		error = -EINVAL;
226490478939SJeff Layton 		if (flock.l_pid != 0)
226590478939SJeff Layton 			goto out;
226690478939SJeff Layton 
22675d50ffd7SJeff Layton 		cmd = F_SETLK64;
2268cff2fce5SJeff Layton 		file_lock->fl_flags |= FL_OFDLCK;
226973a8f5f7SChristoph Hellwig 		file_lock->fl_owner = filp;
22705d50ffd7SJeff Layton 		break;
22710d3f7a2dSJeff Layton 	case F_OFD_SETLKW:
227290478939SJeff Layton 		error = -EINVAL;
227390478939SJeff Layton 		if (flock.l_pid != 0)
227490478939SJeff Layton 			goto out;
227590478939SJeff Layton 
22765d50ffd7SJeff Layton 		cmd = F_SETLKW64;
2277cff2fce5SJeff Layton 		file_lock->fl_flags |= FL_OFDLCK;
227873a8f5f7SChristoph Hellwig 		file_lock->fl_owner = filp;
22795d50ffd7SJeff Layton 		/* Fallthrough */
22805d50ffd7SJeff Layton 	case F_SETLKW64:
22811da177e4SLinus Torvalds 		file_lock->fl_flags |= FL_SLEEP;
22821da177e4SLinus Torvalds 	}
22831da177e4SLinus Torvalds 
2284b648a6deSMiklos Szeredi 	error = do_lock_file_wait(filp, cmd, file_lock);
2285c293621bSPeter Staubach 
2286c293621bSPeter Staubach 	/*
2287c293621bSPeter Staubach 	 * Attempt to detect a close/fcntl race and recover by
2288c293621bSPeter Staubach 	 * releasing the lock that was just acquired.
2289c293621bSPeter Staubach 	 */
22900b2bac2fSAl Viro 	spin_lock(&current->files->file_lock);
22910b2bac2fSAl Viro 	f = fcheck(fd);
22920b2bac2fSAl Viro 	spin_unlock(&current->files->file_lock);
22930b2bac2fSAl Viro 	if (!error && f != filp && flock.l_type != F_UNLCK) {
2294c293621bSPeter Staubach 		flock.l_type = F_UNLCK;
2295c293621bSPeter Staubach 		goto again;
2296c293621bSPeter Staubach 	}
22971da177e4SLinus Torvalds 
22981da177e4SLinus Torvalds out:
22991da177e4SLinus Torvalds 	locks_free_lock(file_lock);
23001da177e4SLinus Torvalds 	return error;
23011da177e4SLinus Torvalds }
23021da177e4SLinus Torvalds #endif /* BITS_PER_LONG == 32 */
23031da177e4SLinus Torvalds 
23041da177e4SLinus Torvalds /*
23051da177e4SLinus Torvalds  * This function is called when the file is being removed
23061da177e4SLinus Torvalds  * from the task's fd array.  POSIX locks belonging to this task
23071da177e4SLinus Torvalds  * are deleted at this time.
23081da177e4SLinus Torvalds  */
23091da177e4SLinus Torvalds void locks_remove_posix(struct file *filp, fl_owner_t owner)
23101da177e4SLinus Torvalds {
2311ff7b86b8SMiklos Szeredi 	struct file_lock lock;
23121da177e4SLinus Torvalds 
23131da177e4SLinus Torvalds 	/*
23141da177e4SLinus Torvalds 	 * If there are no locks held on this file, we don't need to call
23151da177e4SLinus Torvalds 	 * posix_lock_file().  Another process could be setting a lock on this
23161da177e4SLinus Torvalds 	 * file at the same time, but we wouldn't remove that lock anyway.
23171da177e4SLinus Torvalds 	 */
2318496ad9aaSAl Viro 	if (!file_inode(filp)->i_flock)
23191da177e4SLinus Torvalds 		return;
23201da177e4SLinus Torvalds 
23211da177e4SLinus Torvalds 	lock.fl_type = F_UNLCK;
232275e1fcc0SMiklos Szeredi 	lock.fl_flags = FL_POSIX | FL_CLOSE;
23231da177e4SLinus Torvalds 	lock.fl_start = 0;
23241da177e4SLinus Torvalds 	lock.fl_end = OFFSET_MAX;
23251da177e4SLinus Torvalds 	lock.fl_owner = owner;
23261da177e4SLinus Torvalds 	lock.fl_pid = current->tgid;
23271da177e4SLinus Torvalds 	lock.fl_file = filp;
23281da177e4SLinus Torvalds 	lock.fl_ops = NULL;
23291da177e4SLinus Torvalds 	lock.fl_lmops = NULL;
23301da177e4SLinus Torvalds 
2331150b3934SMarc Eshel 	vfs_lock_file(filp, F_SETLK, &lock, NULL);
23321da177e4SLinus Torvalds 
23331da177e4SLinus Torvalds 	if (lock.fl_ops && lock.fl_ops->fl_release_private)
23341da177e4SLinus Torvalds 		lock.fl_ops->fl_release_private(&lock);
23351da177e4SLinus Torvalds }
23361da177e4SLinus Torvalds 
23371da177e4SLinus Torvalds EXPORT_SYMBOL(locks_remove_posix);
23381da177e4SLinus Torvalds 
23391da177e4SLinus Torvalds /*
23401da177e4SLinus Torvalds  * This function is called on the last close of an open file.
23411da177e4SLinus Torvalds  */
234278ed8a13SJeff Layton void locks_remove_file(struct file *filp)
23431da177e4SLinus Torvalds {
2344496ad9aaSAl Viro 	struct inode * inode = file_inode(filp);
23451da177e4SLinus Torvalds 	struct file_lock *fl;
23461da177e4SLinus Torvalds 	struct file_lock **before;
2347ed9814d8SJeff Layton 	LIST_HEAD(dispose);
23481da177e4SLinus Torvalds 
23491da177e4SLinus Torvalds 	if (!inode->i_flock)
23501da177e4SLinus Torvalds 		return;
23511da177e4SLinus Torvalds 
235273a8f5f7SChristoph Hellwig 	locks_remove_posix(filp, filp);
23535d50ffd7SJeff Layton 
235472c2d531SAl Viro 	if (filp->f_op->flock) {
23551da177e4SLinus Torvalds 		struct file_lock fl = {
235673a8f5f7SChristoph Hellwig 			.fl_owner = filp,
23571da177e4SLinus Torvalds 			.fl_pid = current->tgid,
23581da177e4SLinus Torvalds 			.fl_file = filp,
23591da177e4SLinus Torvalds 			.fl_flags = FL_FLOCK,
23601da177e4SLinus Torvalds 			.fl_type = F_UNLCK,
23611da177e4SLinus Torvalds 			.fl_end = OFFSET_MAX,
23621da177e4SLinus Torvalds 		};
23631da177e4SLinus Torvalds 		filp->f_op->flock(filp, F_SETLKW, &fl);
236480fec4c6STrond Myklebust 		if (fl.fl_ops && fl.fl_ops->fl_release_private)
236580fec4c6STrond Myklebust 			fl.fl_ops->fl_release_private(&fl);
23661da177e4SLinus Torvalds 	}
23671da177e4SLinus Torvalds 
23681c8c601aSJeff Layton 	spin_lock(&inode->i_lock);
23691da177e4SLinus Torvalds 	before = &inode->i_flock;
23701da177e4SLinus Torvalds 
23711da177e4SLinus Torvalds 	while ((fl = *before) != NULL) {
23721da177e4SLinus Torvalds 		if (fl->fl_file == filp) {
23731da177e4SLinus Torvalds 			if (IS_LEASE(fl)) {
23741da177e4SLinus Torvalds 				lease_modify(before, F_UNLCK);
23751da177e4SLinus Torvalds 				continue;
23761da177e4SLinus Torvalds 			}
23778c3cac5eSJeff Layton 
23788c3cac5eSJeff Layton 			/*
23798c3cac5eSJeff Layton 			 * There's a leftover lock on the list of a type that
23808c3cac5eSJeff Layton 			 * we didn't expect to see. Most likely a classic
23818c3cac5eSJeff Layton 			 * POSIX lock that ended up not getting released
23828c3cac5eSJeff Layton 			 * properly, or that raced onto the list somehow. Log
23838c3cac5eSJeff Layton 			 * some info about it and then just remove it from
23848c3cac5eSJeff Layton 			 * the list.
23858c3cac5eSJeff Layton 			 */
23868c3cac5eSJeff Layton 			WARN(!IS_FLOCK(fl),
23878c3cac5eSJeff Layton 				"leftover lock: dev=%u:%u ino=%lu type=%hhd flags=0x%x start=%lld end=%lld\n",
23888c3cac5eSJeff Layton 				MAJOR(inode->i_sb->s_dev),
23898c3cac5eSJeff Layton 				MINOR(inode->i_sb->s_dev), inode->i_ino,
23908c3cac5eSJeff Layton 				fl->fl_type, fl->fl_flags,
23918c3cac5eSJeff Layton 				fl->fl_start, fl->fl_end);
23928c3cac5eSJeff Layton 
2393ed9814d8SJeff Layton 			locks_delete_lock(before, &dispose);
23948c3cac5eSJeff Layton 			continue;
23951da177e4SLinus Torvalds  		}
23961da177e4SLinus Torvalds 		before = &fl->fl_next;
23971da177e4SLinus Torvalds 	}
23981c8c601aSJeff Layton 	spin_unlock(&inode->i_lock);
2399ed9814d8SJeff Layton 	locks_dispose_list(&dispose);
24001da177e4SLinus Torvalds }
24011da177e4SLinus Torvalds 
24021da177e4SLinus Torvalds /**
24031da177e4SLinus Torvalds  *	posix_unblock_lock - stop waiting for a file lock
24041da177e4SLinus Torvalds  *	@waiter: the lock which was waiting
24051da177e4SLinus Torvalds  *
24061da177e4SLinus Torvalds  *	lockd needs to block waiting for locks.
24071da177e4SLinus Torvalds  */
240864a318eeSJ. Bruce Fields int
2409f891a29fSJeff Layton posix_unblock_lock(struct file_lock *waiter)
24101da177e4SLinus Torvalds {
241164a318eeSJ. Bruce Fields 	int status = 0;
241264a318eeSJ. Bruce Fields 
24137b2296afSJeff Layton 	spin_lock(&blocked_lock_lock);
24145996a298SJ. Bruce Fields 	if (waiter->fl_next)
24151da177e4SLinus Torvalds 		__locks_delete_block(waiter);
241664a318eeSJ. Bruce Fields 	else
241764a318eeSJ. Bruce Fields 		status = -ENOENT;
24187b2296afSJeff Layton 	spin_unlock(&blocked_lock_lock);
241964a318eeSJ. Bruce Fields 	return status;
24201da177e4SLinus Torvalds }
24211da177e4SLinus Torvalds EXPORT_SYMBOL(posix_unblock_lock);
24221da177e4SLinus Torvalds 
24239b9d2ab4SMarc Eshel /**
24249b9d2ab4SMarc Eshel  * vfs_cancel_lock - file byte range unblock lock
24259b9d2ab4SMarc Eshel  * @filp: The file to apply the unblock to
24269b9d2ab4SMarc Eshel  * @fl: The lock to be unblocked
24279b9d2ab4SMarc Eshel  *
24289b9d2ab4SMarc Eshel  * Used by lock managers to cancel blocked requests
24299b9d2ab4SMarc Eshel  */
24309b9d2ab4SMarc Eshel int vfs_cancel_lock(struct file *filp, struct file_lock *fl)
24319b9d2ab4SMarc Eshel {
243272c2d531SAl Viro 	if (filp->f_op->lock)
24339b9d2ab4SMarc Eshel 		return filp->f_op->lock(filp, F_CANCELLK, fl);
24349b9d2ab4SMarc Eshel 	return 0;
24359b9d2ab4SMarc Eshel }
24369b9d2ab4SMarc Eshel 
24379b9d2ab4SMarc Eshel EXPORT_SYMBOL_GPL(vfs_cancel_lock);
24389b9d2ab4SMarc Eshel 
24397f8ada98SPavel Emelyanov #ifdef CONFIG_PROC_FS
2440d8ba7a36SAlexey Dobriyan #include <linux/proc_fs.h>
24417f8ada98SPavel Emelyanov #include <linux/seq_file.h>
24427f8ada98SPavel Emelyanov 
24437012b02aSJeff Layton struct locks_iterator {
24447012b02aSJeff Layton 	int	li_cpu;
24457012b02aSJeff Layton 	loff_t	li_pos;
24467012b02aSJeff Layton };
24477012b02aSJeff Layton 
24487f8ada98SPavel Emelyanov static void lock_get_status(struct seq_file *f, struct file_lock *fl,
244999dc8292SJerome Marchand 			    loff_t id, char *pfx)
24501da177e4SLinus Torvalds {
24511da177e4SLinus Torvalds 	struct inode *inode = NULL;
2452ab1f1611SVitaliy Gusev 	unsigned int fl_pid;
2453ab1f1611SVitaliy Gusev 
2454ab1f1611SVitaliy Gusev 	if (fl->fl_nspid)
24556c5f3e7bSPavel Emelyanov 		fl_pid = pid_vnr(fl->fl_nspid);
2456ab1f1611SVitaliy Gusev 	else
2457ab1f1611SVitaliy Gusev 		fl_pid = fl->fl_pid;
24581da177e4SLinus Torvalds 
24591da177e4SLinus Torvalds 	if (fl->fl_file != NULL)
2460496ad9aaSAl Viro 		inode = file_inode(fl->fl_file);
24611da177e4SLinus Torvalds 
246299dc8292SJerome Marchand 	seq_printf(f, "%lld:%s ", id, pfx);
24631da177e4SLinus Torvalds 	if (IS_POSIX(fl)) {
2464c918d42aSJeff Layton 		if (fl->fl_flags & FL_ACCESS)
24655315c26aSFabian Frederick 			seq_puts(f, "ACCESS");
2466cff2fce5SJeff Layton 		else if (IS_OFDLCK(fl))
24675315c26aSFabian Frederick 			seq_puts(f, "OFDLCK");
2468c918d42aSJeff Layton 		else
24695315c26aSFabian Frederick 			seq_puts(f, "POSIX ");
2470c918d42aSJeff Layton 
2471c918d42aSJeff Layton 		seq_printf(f, " %s ",
24721da177e4SLinus Torvalds 			     (inode == NULL) ? "*NOINODE*" :
2473a16877caSPavel Emelyanov 			     mandatory_lock(inode) ? "MANDATORY" : "ADVISORY ");
24741da177e4SLinus Torvalds 	} else if (IS_FLOCK(fl)) {
24751da177e4SLinus Torvalds 		if (fl->fl_type & LOCK_MAND) {
24765315c26aSFabian Frederick 			seq_puts(f, "FLOCK  MSNFS     ");
24771da177e4SLinus Torvalds 		} else {
24785315c26aSFabian Frederick 			seq_puts(f, "FLOCK  ADVISORY  ");
24791da177e4SLinus Torvalds 		}
24801da177e4SLinus Torvalds 	} else if (IS_LEASE(fl)) {
24818144f1f6SJeff Layton 		if (fl->fl_flags & FL_DELEG)
24828144f1f6SJeff Layton 			seq_puts(f, "DELEG  ");
24838144f1f6SJeff Layton 		else
24845315c26aSFabian Frederick 			seq_puts(f, "LEASE  ");
24858144f1f6SJeff Layton 
2486ab83fa4bSJ. Bruce Fields 		if (lease_breaking(fl))
24875315c26aSFabian Frederick 			seq_puts(f, "BREAKING  ");
24881da177e4SLinus Torvalds 		else if (fl->fl_file)
24895315c26aSFabian Frederick 			seq_puts(f, "ACTIVE    ");
24901da177e4SLinus Torvalds 		else
24915315c26aSFabian Frederick 			seq_puts(f, "BREAKER   ");
24921da177e4SLinus Torvalds 	} else {
24935315c26aSFabian Frederick 		seq_puts(f, "UNKNOWN UNKNOWN  ");
24941da177e4SLinus Torvalds 	}
24951da177e4SLinus Torvalds 	if (fl->fl_type & LOCK_MAND) {
24967f8ada98SPavel Emelyanov 		seq_printf(f, "%s ",
24971da177e4SLinus Torvalds 			       (fl->fl_type & LOCK_READ)
24981da177e4SLinus Torvalds 			       ? (fl->fl_type & LOCK_WRITE) ? "RW   " : "READ "
24991da177e4SLinus Torvalds 			       : (fl->fl_type & LOCK_WRITE) ? "WRITE" : "NONE ");
25001da177e4SLinus Torvalds 	} else {
25017f8ada98SPavel Emelyanov 		seq_printf(f, "%s ",
2502ab83fa4bSJ. Bruce Fields 			       (lease_breaking(fl))
25030ee5c6d6SJeff Layton 			       ? (fl->fl_type == F_UNLCK) ? "UNLCK" : "READ "
25040ee5c6d6SJeff Layton 			       : (fl->fl_type == F_WRLCK) ? "WRITE" : "READ ");
25051da177e4SLinus Torvalds 	}
25061da177e4SLinus Torvalds 	if (inode) {
25071da177e4SLinus Torvalds #ifdef WE_CAN_BREAK_LSLK_NOW
2508ab1f1611SVitaliy Gusev 		seq_printf(f, "%d %s:%ld ", fl_pid,
25091da177e4SLinus Torvalds 				inode->i_sb->s_id, inode->i_ino);
25101da177e4SLinus Torvalds #else
25111da177e4SLinus Torvalds 		/* userspace relies on this representation of dev_t ;-( */
2512ab1f1611SVitaliy Gusev 		seq_printf(f, "%d %02x:%02x:%ld ", fl_pid,
25131da177e4SLinus Torvalds 				MAJOR(inode->i_sb->s_dev),
25141da177e4SLinus Torvalds 				MINOR(inode->i_sb->s_dev), inode->i_ino);
25151da177e4SLinus Torvalds #endif
25161da177e4SLinus Torvalds 	} else {
2517ab1f1611SVitaliy Gusev 		seq_printf(f, "%d <none>:0 ", fl_pid);
25181da177e4SLinus Torvalds 	}
25191da177e4SLinus Torvalds 	if (IS_POSIX(fl)) {
25201da177e4SLinus Torvalds 		if (fl->fl_end == OFFSET_MAX)
25217f8ada98SPavel Emelyanov 			seq_printf(f, "%Ld EOF\n", fl->fl_start);
25221da177e4SLinus Torvalds 		else
25237f8ada98SPavel Emelyanov 			seq_printf(f, "%Ld %Ld\n", fl->fl_start, fl->fl_end);
25241da177e4SLinus Torvalds 	} else {
25255315c26aSFabian Frederick 		seq_puts(f, "0 EOF\n");
25261da177e4SLinus Torvalds 	}
25271da177e4SLinus Torvalds }
25281da177e4SLinus Torvalds 
25297f8ada98SPavel Emelyanov static int locks_show(struct seq_file *f, void *v)
25301da177e4SLinus Torvalds {
25317012b02aSJeff Layton 	struct locks_iterator *iter = f->private;
25327f8ada98SPavel Emelyanov 	struct file_lock *fl, *bfl;
25337f8ada98SPavel Emelyanov 
2534139ca04eSJeff Layton 	fl = hlist_entry(v, struct file_lock, fl_link);
25357f8ada98SPavel Emelyanov 
25367012b02aSJeff Layton 	lock_get_status(f, fl, iter->li_pos, "");
25377f8ada98SPavel Emelyanov 
25387f8ada98SPavel Emelyanov 	list_for_each_entry(bfl, &fl->fl_block, fl_block)
25397012b02aSJeff Layton 		lock_get_status(f, bfl, iter->li_pos, " ->");
25407f8ada98SPavel Emelyanov 
25417f8ada98SPavel Emelyanov 	return 0;
25421da177e4SLinus Torvalds }
25431da177e4SLinus Torvalds 
25447f8ada98SPavel Emelyanov static void *locks_start(struct seq_file *f, loff_t *pos)
2545b03dfdecSJeff Layton 	__acquires(&blocked_lock_lock)
25461da177e4SLinus Torvalds {
25477012b02aSJeff Layton 	struct locks_iterator *iter = f->private;
254899dc8292SJerome Marchand 
25497012b02aSJeff Layton 	iter->li_pos = *pos + 1;
25507012b02aSJeff Layton 	lg_global_lock(&file_lock_lglock);
25517b2296afSJeff Layton 	spin_lock(&blocked_lock_lock);
25527012b02aSJeff Layton 	return seq_hlist_start_percpu(&file_lock_list, &iter->li_cpu, *pos);
25531da177e4SLinus Torvalds }
25547f8ada98SPavel Emelyanov 
25557f8ada98SPavel Emelyanov static void *locks_next(struct seq_file *f, void *v, loff_t *pos)
25567f8ada98SPavel Emelyanov {
25577012b02aSJeff Layton 	struct locks_iterator *iter = f->private;
25587012b02aSJeff Layton 
25597012b02aSJeff Layton 	++iter->li_pos;
25607012b02aSJeff Layton 	return seq_hlist_next_percpu(v, &file_lock_list, &iter->li_cpu, pos);
25611da177e4SLinus Torvalds }
25627f8ada98SPavel Emelyanov 
25637f8ada98SPavel Emelyanov static void locks_stop(struct seq_file *f, void *v)
2564b03dfdecSJeff Layton 	__releases(&blocked_lock_lock)
25657f8ada98SPavel Emelyanov {
25667b2296afSJeff Layton 	spin_unlock(&blocked_lock_lock);
25677012b02aSJeff Layton 	lg_global_unlock(&file_lock_lglock);
25681da177e4SLinus Torvalds }
25691da177e4SLinus Torvalds 
2570d8ba7a36SAlexey Dobriyan static const struct seq_operations locks_seq_operations = {
25717f8ada98SPavel Emelyanov 	.start	= locks_start,
25727f8ada98SPavel Emelyanov 	.next	= locks_next,
25737f8ada98SPavel Emelyanov 	.stop	= locks_stop,
25747f8ada98SPavel Emelyanov 	.show	= locks_show,
25757f8ada98SPavel Emelyanov };
2576d8ba7a36SAlexey Dobriyan 
2577d8ba7a36SAlexey Dobriyan static int locks_open(struct inode *inode, struct file *filp)
2578d8ba7a36SAlexey Dobriyan {
25797012b02aSJeff Layton 	return seq_open_private(filp, &locks_seq_operations,
25807012b02aSJeff Layton 					sizeof(struct locks_iterator));
2581d8ba7a36SAlexey Dobriyan }
2582d8ba7a36SAlexey Dobriyan 
2583d8ba7a36SAlexey Dobriyan static const struct file_operations proc_locks_operations = {
2584d8ba7a36SAlexey Dobriyan 	.open		= locks_open,
2585d8ba7a36SAlexey Dobriyan 	.read		= seq_read,
2586d8ba7a36SAlexey Dobriyan 	.llseek		= seq_lseek,
258799dc8292SJerome Marchand 	.release	= seq_release_private,
2588d8ba7a36SAlexey Dobriyan };
2589d8ba7a36SAlexey Dobriyan 
2590d8ba7a36SAlexey Dobriyan static int __init proc_locks_init(void)
2591d8ba7a36SAlexey Dobriyan {
2592d8ba7a36SAlexey Dobriyan 	proc_create("locks", 0, NULL, &proc_locks_operations);
2593d8ba7a36SAlexey Dobriyan 	return 0;
2594d8ba7a36SAlexey Dobriyan }
2595d8ba7a36SAlexey Dobriyan module_init(proc_locks_init);
25967f8ada98SPavel Emelyanov #endif
25977f8ada98SPavel Emelyanov 
25981da177e4SLinus Torvalds static int __init filelock_init(void)
25991da177e4SLinus Torvalds {
26007012b02aSJeff Layton 	int i;
26017012b02aSJeff Layton 
26021da177e4SLinus Torvalds 	filelock_cache = kmem_cache_create("file_lock_cache",
2603ee19cc40SMiklos Szeredi 			sizeof(struct file_lock), 0, SLAB_PANIC, NULL);
2604ee19cc40SMiklos Szeredi 
26057012b02aSJeff Layton 	lg_lock_init(&file_lock_lglock, "file_lock_lglock");
26067012b02aSJeff Layton 
26077012b02aSJeff Layton 	for_each_possible_cpu(i)
26087012b02aSJeff Layton 		INIT_HLIST_HEAD(per_cpu_ptr(&file_lock_list, i));
26097012b02aSJeff Layton 
26101da177e4SLinus Torvalds 	return 0;
26111da177e4SLinus Torvalds }
26121da177e4SLinus Torvalds 
26131da177e4SLinus Torvalds core_initcall(filelock_init);
2614