xref: /linux/fs/locks.c (revision 0d3f7a2dd2f5cf9642982515e020c1aee2cf7af6)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/fs/locks.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Provide support for fcntl()'s F_GETLK, F_SETLK, and F_SETLKW calls.
51da177e4SLinus Torvalds  *  Doug Evans (dje@spiff.uucp), August 07, 1992
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *  Deadlock detection added.
81da177e4SLinus Torvalds  *  FIXME: one thing isn't handled yet:
91da177e4SLinus Torvalds  *	- mandatory locks (requires lots of changes elsewhere)
101da177e4SLinus Torvalds  *  Kelly Carmichael (kelly@[142.24.8.65]), September 17, 1994.
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  *  Miscellaneous edits, and a total rewrite of posix_lock_file() code.
131da177e4SLinus Torvalds  *  Kai Petzke (wpp@marie.physik.tu-berlin.de), 1994
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  *  Converted file_lock_table to a linked list from an array, which eliminates
161da177e4SLinus Torvalds  *  the limits on how many active file locks are open.
171da177e4SLinus Torvalds  *  Chad Page (pageone@netcom.com), November 27, 1994
181da177e4SLinus Torvalds  *
191da177e4SLinus Torvalds  *  Removed dependency on file descriptors. dup()'ed file descriptors now
201da177e4SLinus Torvalds  *  get the same locks as the original file descriptors, and a close() on
211da177e4SLinus Torvalds  *  any file descriptor removes ALL the locks on the file for the current
221da177e4SLinus Torvalds  *  process. Since locks still depend on the process id, locks are inherited
231da177e4SLinus Torvalds  *  after an exec() but not after a fork(). This agrees with POSIX, and both
241da177e4SLinus Torvalds  *  BSD and SVR4 practice.
251da177e4SLinus Torvalds  *  Andy Walker (andy@lysaker.kvaerner.no), February 14, 1995
261da177e4SLinus Torvalds  *
271da177e4SLinus Torvalds  *  Scrapped free list which is redundant now that we allocate locks
281da177e4SLinus Torvalds  *  dynamically with kmalloc()/kfree().
291da177e4SLinus Torvalds  *  Andy Walker (andy@lysaker.kvaerner.no), February 21, 1995
301da177e4SLinus Torvalds  *
311da177e4SLinus Torvalds  *  Implemented two lock personalities - FL_FLOCK and FL_POSIX.
321da177e4SLinus Torvalds  *
331da177e4SLinus Torvalds  *  FL_POSIX locks are created with calls to fcntl() and lockf() through the
341da177e4SLinus Torvalds  *  fcntl() system call. They have the semantics described above.
351da177e4SLinus Torvalds  *
361da177e4SLinus Torvalds  *  FL_FLOCK locks are created with calls to flock(), through the flock()
371da177e4SLinus Torvalds  *  system call, which is new. Old C libraries implement flock() via fcntl()
381da177e4SLinus Torvalds  *  and will continue to use the old, broken implementation.
391da177e4SLinus Torvalds  *
401da177e4SLinus Torvalds  *  FL_FLOCK locks follow the 4.4 BSD flock() semantics. They are associated
411da177e4SLinus Torvalds  *  with a file pointer (filp). As a result they can be shared by a parent
421da177e4SLinus Torvalds  *  process and its children after a fork(). They are removed when the last
431da177e4SLinus Torvalds  *  file descriptor referring to the file pointer is closed (unless explicitly
441da177e4SLinus Torvalds  *  unlocked).
451da177e4SLinus Torvalds  *
461da177e4SLinus Torvalds  *  FL_FLOCK locks never deadlock, an existing lock is always removed before
471da177e4SLinus Torvalds  *  upgrading from shared to exclusive (or vice versa). When this happens
481da177e4SLinus Torvalds  *  any processes blocked by the current lock are woken up and allowed to
491da177e4SLinus Torvalds  *  run before the new lock is applied.
501da177e4SLinus Torvalds  *  Andy Walker (andy@lysaker.kvaerner.no), June 09, 1995
511da177e4SLinus Torvalds  *
521da177e4SLinus Torvalds  *  Removed some race conditions in flock_lock_file(), marked other possible
531da177e4SLinus Torvalds  *  races. Just grep for FIXME to see them.
541da177e4SLinus Torvalds  *  Dmitry Gorodchanin (pgmdsg@ibi.com), February 09, 1996.
551da177e4SLinus Torvalds  *
561da177e4SLinus Torvalds  *  Addressed Dmitry's concerns. Deadlock checking no longer recursive.
571da177e4SLinus Torvalds  *  Lock allocation changed to GFP_ATOMIC as we can't afford to sleep
581da177e4SLinus Torvalds  *  once we've checked for blocking and deadlocking.
591da177e4SLinus Torvalds  *  Andy Walker (andy@lysaker.kvaerner.no), April 03, 1996.
601da177e4SLinus Torvalds  *
611da177e4SLinus Torvalds  *  Initial implementation of mandatory locks. SunOS turned out to be
621da177e4SLinus Torvalds  *  a rotten model, so I implemented the "obvious" semantics.
63395cf969SPaul Bolle  *  See 'Documentation/filesystems/mandatory-locking.txt' for details.
641da177e4SLinus Torvalds  *  Andy Walker (andy@lysaker.kvaerner.no), April 06, 1996.
651da177e4SLinus Torvalds  *
661da177e4SLinus Torvalds  *  Don't allow mandatory locks on mmap()'ed files. Added simple functions to
671da177e4SLinus Torvalds  *  check if a file has mandatory locks, used by mmap(), open() and creat() to
681da177e4SLinus Torvalds  *  see if system call should be rejected. Ref. HP-UX/SunOS/Solaris Reference
691da177e4SLinus Torvalds  *  Manual, Section 2.
701da177e4SLinus Torvalds  *  Andy Walker (andy@lysaker.kvaerner.no), April 09, 1996.
711da177e4SLinus Torvalds  *
721da177e4SLinus Torvalds  *  Tidied up block list handling. Added '/proc/locks' interface.
731da177e4SLinus Torvalds  *  Andy Walker (andy@lysaker.kvaerner.no), April 24, 1996.
741da177e4SLinus Torvalds  *
751da177e4SLinus Torvalds  *  Fixed deadlock condition for pathological code that mixes calls to
761da177e4SLinus Torvalds  *  flock() and fcntl().
771da177e4SLinus Torvalds  *  Andy Walker (andy@lysaker.kvaerner.no), April 29, 1996.
781da177e4SLinus Torvalds  *
791da177e4SLinus Torvalds  *  Allow only one type of locking scheme (FL_POSIX or FL_FLOCK) to be in use
801da177e4SLinus Torvalds  *  for a given file at a time. Changed the CONFIG_LOCK_MANDATORY scheme to
811da177e4SLinus Torvalds  *  guarantee sensible behaviour in the case where file system modules might
821da177e4SLinus Torvalds  *  be compiled with different options than the kernel itself.
831da177e4SLinus Torvalds  *  Andy Walker (andy@lysaker.kvaerner.no), May 15, 1996.
841da177e4SLinus Torvalds  *
851da177e4SLinus Torvalds  *  Added a couple of missing wake_up() calls. Thanks to Thomas Meckel
861da177e4SLinus Torvalds  *  (Thomas.Meckel@mni.fh-giessen.de) for spotting this.
871da177e4SLinus Torvalds  *  Andy Walker (andy@lysaker.kvaerner.no), May 15, 1996.
881da177e4SLinus Torvalds  *
891da177e4SLinus Torvalds  *  Changed FL_POSIX locks to use the block list in the same way as FL_FLOCK
901da177e4SLinus Torvalds  *  locks. Changed process synchronisation to avoid dereferencing locks that
911da177e4SLinus Torvalds  *  have already been freed.
921da177e4SLinus Torvalds  *  Andy Walker (andy@lysaker.kvaerner.no), Sep 21, 1996.
931da177e4SLinus Torvalds  *
941da177e4SLinus Torvalds  *  Made the block list a circular list to minimise searching in the list.
951da177e4SLinus Torvalds  *  Andy Walker (andy@lysaker.kvaerner.no), Sep 25, 1996.
961da177e4SLinus Torvalds  *
971da177e4SLinus Torvalds  *  Made mandatory locking a mount option. Default is not to allow mandatory
981da177e4SLinus Torvalds  *  locking.
991da177e4SLinus Torvalds  *  Andy Walker (andy@lysaker.kvaerner.no), Oct 04, 1996.
1001da177e4SLinus Torvalds  *
1011da177e4SLinus Torvalds  *  Some adaptations for NFS support.
1021da177e4SLinus Torvalds  *  Olaf Kirch (okir@monad.swb.de), Dec 1996,
1031da177e4SLinus Torvalds  *
1041da177e4SLinus Torvalds  *  Fixed /proc/locks interface so that we can't overrun the buffer we are handed.
1051da177e4SLinus Torvalds  *  Andy Walker (andy@lysaker.kvaerner.no), May 12, 1997.
1061da177e4SLinus Torvalds  *
1071da177e4SLinus Torvalds  *  Use slab allocator instead of kmalloc/kfree.
1081da177e4SLinus Torvalds  *  Use generic list implementation from <linux/list.h>.
1091da177e4SLinus Torvalds  *  Sped up posix_locks_deadlock by only considering blocked locks.
1101da177e4SLinus Torvalds  *  Matthew Wilcox <willy@debian.org>, March, 2000.
1111da177e4SLinus Torvalds  *
1121da177e4SLinus Torvalds  *  Leases and LOCK_MAND
1131da177e4SLinus Torvalds  *  Matthew Wilcox <willy@debian.org>, June, 2000.
1141da177e4SLinus Torvalds  *  Stephen Rothwell <sfr@canb.auug.org.au>, June, 2000.
1151da177e4SLinus Torvalds  */
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds #include <linux/capability.h>
1181da177e4SLinus Torvalds #include <linux/file.h>
1199f3acc31SAl Viro #include <linux/fdtable.h>
1201da177e4SLinus Torvalds #include <linux/fs.h>
1211da177e4SLinus Torvalds #include <linux/init.h>
1221da177e4SLinus Torvalds #include <linux/module.h>
1231da177e4SLinus Torvalds #include <linux/security.h>
1241da177e4SLinus Torvalds #include <linux/slab.h>
1251da177e4SLinus Torvalds #include <linux/syscalls.h>
1261da177e4SLinus Torvalds #include <linux/time.h>
1274fb3a538SDipankar Sarma #include <linux/rcupdate.h>
128ab1f1611SVitaliy Gusev #include <linux/pid_namespace.h>
12948f74186SJeff Layton #include <linux/hashtable.h>
1307012b02aSJeff Layton #include <linux/percpu.h>
1317012b02aSJeff Layton #include <linux/lglock.h>
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds #include <asm/uaccess.h>
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds #define IS_POSIX(fl)	(fl->fl_flags & FL_POSIX)
1361da177e4SLinus Torvalds #define IS_FLOCK(fl)	(fl->fl_flags & FL_FLOCK)
137617588d5SJ. Bruce Fields #define IS_LEASE(fl)	(fl->fl_flags & (FL_LEASE|FL_DELEG))
138c918d42aSJeff Layton #define IS_FILE_PVT(fl)	(fl->fl_flags & FL_FILE_PVT)
1391da177e4SLinus Torvalds 
140ab83fa4bSJ. Bruce Fields static bool lease_breaking(struct file_lock *fl)
141ab83fa4bSJ. Bruce Fields {
142778fc546SJ. Bruce Fields 	return fl->fl_flags & (FL_UNLOCK_PENDING | FL_DOWNGRADE_PENDING);
143778fc546SJ. Bruce Fields }
144778fc546SJ. Bruce Fields 
145778fc546SJ. Bruce Fields static int target_leasetype(struct file_lock *fl)
146778fc546SJ. Bruce Fields {
147778fc546SJ. Bruce Fields 	if (fl->fl_flags & FL_UNLOCK_PENDING)
148778fc546SJ. Bruce Fields 		return F_UNLCK;
149778fc546SJ. Bruce Fields 	if (fl->fl_flags & FL_DOWNGRADE_PENDING)
150778fc546SJ. Bruce Fields 		return F_RDLCK;
151778fc546SJ. Bruce Fields 	return fl->fl_type;
152ab83fa4bSJ. Bruce Fields }
153ab83fa4bSJ. Bruce Fields 
1541da177e4SLinus Torvalds int leases_enable = 1;
1551da177e4SLinus Torvalds int lease_break_time = 45;
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds #define for_each_lock(inode, lockp) \
1581da177e4SLinus Torvalds 	for (lockp = &inode->i_flock; *lockp != NULL; lockp = &(*lockp)->fl_next)
1591da177e4SLinus Torvalds 
1601c8c601aSJeff Layton /*
1617012b02aSJeff Layton  * The global file_lock_list is only used for displaying /proc/locks, so we
1627012b02aSJeff Layton  * keep a list on each CPU, with each list protected by its own spinlock via
1637012b02aSJeff Layton  * the file_lock_lglock. Note that alterations to the list also require that
1647012b02aSJeff Layton  * the relevant i_lock is held.
1651c8c601aSJeff Layton  */
1667012b02aSJeff Layton DEFINE_STATIC_LGLOCK(file_lock_lglock);
1677012b02aSJeff Layton static DEFINE_PER_CPU(struct hlist_head, file_lock_list);
16888974691SJeff Layton 
1691c8c601aSJeff Layton /*
17048f74186SJeff Layton  * The blocked_hash is used to find POSIX lock loops for deadlock detection.
1717b2296afSJeff Layton  * It is protected by blocked_lock_lock.
17248f74186SJeff Layton  *
17348f74186SJeff Layton  * We hash locks by lockowner in order to optimize searching for the lock a
17448f74186SJeff Layton  * particular lockowner is waiting on.
17548f74186SJeff Layton  *
17648f74186SJeff Layton  * FIXME: make this value scale via some heuristic? We generally will want more
17748f74186SJeff Layton  * buckets when we have more lockowners holding locks, but that's a little
17848f74186SJeff Layton  * difficult to determine without knowing what the workload will look like.
1791c8c601aSJeff Layton  */
18048f74186SJeff Layton #define BLOCKED_HASH_BITS	7
18148f74186SJeff Layton static DEFINE_HASHTABLE(blocked_hash, BLOCKED_HASH_BITS);
18288974691SJeff Layton 
1831c8c601aSJeff Layton /*
1847b2296afSJeff Layton  * This lock protects the blocked_hash. Generally, if you're accessing it, you
1857b2296afSJeff Layton  * want to be holding this lock.
1861c8c601aSJeff Layton  *
1871c8c601aSJeff Layton  * In addition, it also protects the fl->fl_block list, and the fl->fl_next
1881c8c601aSJeff Layton  * pointer for file_lock structures that are acting as lock requests (in
1891c8c601aSJeff Layton  * contrast to those that are acting as records of acquired locks).
1901c8c601aSJeff Layton  *
1911c8c601aSJeff Layton  * Note that when we acquire this lock in order to change the above fields,
1921c8c601aSJeff Layton  * we often hold the i_lock as well. In certain cases, when reading the fields
1931c8c601aSJeff Layton  * protected by this lock, we can skip acquiring it iff we already hold the
1941c8c601aSJeff Layton  * i_lock.
1951c8c601aSJeff Layton  *
1961c8c601aSJeff Layton  * In particular, adding an entry to the fl_block list requires that you hold
1971c8c601aSJeff Layton  * both the i_lock and the blocked_lock_lock (acquired in that order). Deleting
1981c8c601aSJeff Layton  * an entry from the list however only requires the file_lock_lock.
1991c8c601aSJeff Layton  */
2007b2296afSJeff Layton static DEFINE_SPINLOCK(blocked_lock_lock);
2011da177e4SLinus Torvalds 
202e18b890bSChristoph Lameter static struct kmem_cache *filelock_cache __read_mostly;
2031da177e4SLinus Torvalds 
204ee19cc40SMiklos Szeredi static void locks_init_lock_heads(struct file_lock *fl)
205a51cb91dSMiklos Szeredi {
206139ca04eSJeff Layton 	INIT_HLIST_NODE(&fl->fl_link);
207ee19cc40SMiklos Szeredi 	INIT_LIST_HEAD(&fl->fl_block);
208ee19cc40SMiklos Szeredi 	init_waitqueue_head(&fl->fl_wait);
209a51cb91dSMiklos Szeredi }
210a51cb91dSMiklos Szeredi 
2111da177e4SLinus Torvalds /* Allocate an empty lock structure. */
212c5b1f0d9SArnd Bergmann struct file_lock *locks_alloc_lock(void)
2131da177e4SLinus Torvalds {
214ee19cc40SMiklos Szeredi 	struct file_lock *fl = kmem_cache_zalloc(filelock_cache, GFP_KERNEL);
215a51cb91dSMiklos Szeredi 
216a51cb91dSMiklos Szeredi 	if (fl)
217ee19cc40SMiklos Szeredi 		locks_init_lock_heads(fl);
218a51cb91dSMiklos Szeredi 
219a51cb91dSMiklos Szeredi 	return fl;
2201da177e4SLinus Torvalds }
221c5b1f0d9SArnd Bergmann EXPORT_SYMBOL_GPL(locks_alloc_lock);
2221da177e4SLinus Torvalds 
223a9e61e25SFelix Blyakher void locks_release_private(struct file_lock *fl)
22447831f35STrond Myklebust {
22547831f35STrond Myklebust 	if (fl->fl_ops) {
22647831f35STrond Myklebust 		if (fl->fl_ops->fl_release_private)
22747831f35STrond Myklebust 			fl->fl_ops->fl_release_private(fl);
22847831f35STrond Myklebust 		fl->fl_ops = NULL;
22947831f35STrond Myklebust 	}
23047831f35STrond Myklebust 	fl->fl_lmops = NULL;
23147831f35STrond Myklebust 
23247831f35STrond Myklebust }
233a9e61e25SFelix Blyakher EXPORT_SYMBOL_GPL(locks_release_private);
23447831f35STrond Myklebust 
2351da177e4SLinus Torvalds /* Free a lock which is not in use. */
23605fa3135SJ. Bruce Fields void locks_free_lock(struct file_lock *fl)
2371da177e4SLinus Torvalds {
2385ce29646SMiklos Szeredi 	BUG_ON(waitqueue_active(&fl->fl_wait));
2395ce29646SMiklos Szeredi 	BUG_ON(!list_empty(&fl->fl_block));
240139ca04eSJeff Layton 	BUG_ON(!hlist_unhashed(&fl->fl_link));
2411da177e4SLinus Torvalds 
24247831f35STrond Myklebust 	locks_release_private(fl);
2431da177e4SLinus Torvalds 	kmem_cache_free(filelock_cache, fl);
2441da177e4SLinus Torvalds }
24505fa3135SJ. Bruce Fields EXPORT_SYMBOL(locks_free_lock);
2461da177e4SLinus Torvalds 
2471da177e4SLinus Torvalds void locks_init_lock(struct file_lock *fl)
2481da177e4SLinus Torvalds {
249ee19cc40SMiklos Szeredi 	memset(fl, 0, sizeof(struct file_lock));
250ee19cc40SMiklos Szeredi 	locks_init_lock_heads(fl);
2511da177e4SLinus Torvalds }
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds EXPORT_SYMBOL(locks_init_lock);
2541da177e4SLinus Torvalds 
25547831f35STrond Myklebust static void locks_copy_private(struct file_lock *new, struct file_lock *fl)
25647831f35STrond Myklebust {
25747831f35STrond Myklebust 	if (fl->fl_ops) {
25847831f35STrond Myklebust 		if (fl->fl_ops->fl_copy_lock)
25947831f35STrond Myklebust 			fl->fl_ops->fl_copy_lock(new, fl);
26047831f35STrond Myklebust 		new->fl_ops = fl->fl_ops;
26147831f35STrond Myklebust 	}
262bb8430a2SChristoph Hellwig 	if (fl->fl_lmops)
26347831f35STrond Myklebust 		new->fl_lmops = fl->fl_lmops;
26447831f35STrond Myklebust }
26547831f35STrond Myklebust 
2661da177e4SLinus Torvalds /*
2671da177e4SLinus Torvalds  * Initialize a new lock from an existing file_lock structure.
2681da177e4SLinus Torvalds  */
2691a747ee0SJ. Bruce Fields void __locks_copy_lock(struct file_lock *new, const struct file_lock *fl)
2701da177e4SLinus Torvalds {
2711da177e4SLinus Torvalds 	new->fl_owner = fl->fl_owner;
2721da177e4SLinus Torvalds 	new->fl_pid = fl->fl_pid;
2730996905fSTrond Myklebust 	new->fl_file = NULL;
2741da177e4SLinus Torvalds 	new->fl_flags = fl->fl_flags;
2751da177e4SLinus Torvalds 	new->fl_type = fl->fl_type;
2761da177e4SLinus Torvalds 	new->fl_start = fl->fl_start;
2771da177e4SLinus Torvalds 	new->fl_end = fl->fl_end;
2780996905fSTrond Myklebust 	new->fl_ops = NULL;
2790996905fSTrond Myklebust 	new->fl_lmops = NULL;
2800996905fSTrond Myklebust }
2813dd7b71cSRoland Dreier EXPORT_SYMBOL(__locks_copy_lock);
2820996905fSTrond Myklebust 
2830996905fSTrond Myklebust void locks_copy_lock(struct file_lock *new, struct file_lock *fl)
2840996905fSTrond Myklebust {
2850996905fSTrond Myklebust 	locks_release_private(new);
2860996905fSTrond Myklebust 
2870996905fSTrond Myklebust 	__locks_copy_lock(new, fl);
2880996905fSTrond Myklebust 	new->fl_file = fl->fl_file;
2891da177e4SLinus Torvalds 	new->fl_ops = fl->fl_ops;
2901da177e4SLinus Torvalds 	new->fl_lmops = fl->fl_lmops;
29147831f35STrond Myklebust 
29247831f35STrond Myklebust 	locks_copy_private(new, fl);
2931da177e4SLinus Torvalds }
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds EXPORT_SYMBOL(locks_copy_lock);
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds static inline int flock_translate_cmd(int cmd) {
2981da177e4SLinus Torvalds 	if (cmd & LOCK_MAND)
2991da177e4SLinus Torvalds 		return cmd & (LOCK_MAND | LOCK_RW);
3001da177e4SLinus Torvalds 	switch (cmd) {
3011da177e4SLinus Torvalds 	case LOCK_SH:
3021da177e4SLinus Torvalds 		return F_RDLCK;
3031da177e4SLinus Torvalds 	case LOCK_EX:
3041da177e4SLinus Torvalds 		return F_WRLCK;
3051da177e4SLinus Torvalds 	case LOCK_UN:
3061da177e4SLinus Torvalds 		return F_UNLCK;
3071da177e4SLinus Torvalds 	}
3081da177e4SLinus Torvalds 	return -EINVAL;
3091da177e4SLinus Torvalds }
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds /* Fill in a file_lock structure with an appropriate FLOCK lock. */
3121da177e4SLinus Torvalds static int flock_make_lock(struct file *filp, struct file_lock **lock,
3131da177e4SLinus Torvalds 		unsigned int cmd)
3141da177e4SLinus Torvalds {
3151da177e4SLinus Torvalds 	struct file_lock *fl;
3161da177e4SLinus Torvalds 	int type = flock_translate_cmd(cmd);
3171da177e4SLinus Torvalds 	if (type < 0)
3181da177e4SLinus Torvalds 		return type;
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds 	fl = locks_alloc_lock();
3211da177e4SLinus Torvalds 	if (fl == NULL)
3221da177e4SLinus Torvalds 		return -ENOMEM;
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds 	fl->fl_file = filp;
3251da177e4SLinus Torvalds 	fl->fl_pid = current->tgid;
3261da177e4SLinus Torvalds 	fl->fl_flags = FL_FLOCK;
3271da177e4SLinus Torvalds 	fl->fl_type = type;
3281da177e4SLinus Torvalds 	fl->fl_end = OFFSET_MAX;
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds 	*lock = fl;
3311da177e4SLinus Torvalds 	return 0;
3321da177e4SLinus Torvalds }
3331da177e4SLinus Torvalds 
3340ec4f431SJ. Bruce Fields static int assign_type(struct file_lock *fl, long type)
3351da177e4SLinus Torvalds {
3361da177e4SLinus Torvalds 	switch (type) {
3371da177e4SLinus Torvalds 	case F_RDLCK:
3381da177e4SLinus Torvalds 	case F_WRLCK:
3391da177e4SLinus Torvalds 	case F_UNLCK:
3401da177e4SLinus Torvalds 		fl->fl_type = type;
3411da177e4SLinus Torvalds 		break;
3421da177e4SLinus Torvalds 	default:
3431da177e4SLinus Torvalds 		return -EINVAL;
3441da177e4SLinus Torvalds 	}
3451da177e4SLinus Torvalds 	return 0;
3461da177e4SLinus Torvalds }
3471da177e4SLinus Torvalds 
348ef12e72aSJ. Bruce Fields static int flock64_to_posix_lock(struct file *filp, struct file_lock *fl,
349ef12e72aSJ. Bruce Fields 				 struct flock64 *l)
350ef12e72aSJ. Bruce Fields {
351ef12e72aSJ. Bruce Fields 	switch (l->l_whence) {
352ef12e72aSJ. Bruce Fields 	case SEEK_SET:
353ef12e72aSJ. Bruce Fields 		fl->fl_start = 0;
354ef12e72aSJ. Bruce Fields 		break;
355ef12e72aSJ. Bruce Fields 	case SEEK_CUR:
356ef12e72aSJ. Bruce Fields 		fl->fl_start = filp->f_pos;
357ef12e72aSJ. Bruce Fields 		break;
358ef12e72aSJ. Bruce Fields 	case SEEK_END:
359ef12e72aSJ. Bruce Fields 		fl->fl_start = i_size_read(file_inode(filp));
360ef12e72aSJ. Bruce Fields 		break;
361ef12e72aSJ. Bruce Fields 	default:
362ef12e72aSJ. Bruce Fields 		return -EINVAL;
363ef12e72aSJ. Bruce Fields 	}
364ef12e72aSJ. Bruce Fields 	if (l->l_start > OFFSET_MAX - fl->fl_start)
365ef12e72aSJ. Bruce Fields 		return -EOVERFLOW;
366ef12e72aSJ. Bruce Fields 	fl->fl_start += l->l_start;
367ef12e72aSJ. Bruce Fields 	if (fl->fl_start < 0)
368ef12e72aSJ. Bruce Fields 		return -EINVAL;
369ef12e72aSJ. Bruce Fields 
370ef12e72aSJ. Bruce Fields 	/* POSIX-1996 leaves the case l->l_len < 0 undefined;
371ef12e72aSJ. Bruce Fields 	   POSIX-2001 defines it. */
372ef12e72aSJ. Bruce Fields 	if (l->l_len > 0) {
373ef12e72aSJ. Bruce Fields 		if (l->l_len - 1 > OFFSET_MAX - fl->fl_start)
374ef12e72aSJ. Bruce Fields 			return -EOVERFLOW;
375ef12e72aSJ. Bruce Fields 		fl->fl_end = fl->fl_start + l->l_len - 1;
376ef12e72aSJ. Bruce Fields 
377ef12e72aSJ. Bruce Fields 	} else if (l->l_len < 0) {
378ef12e72aSJ. Bruce Fields 		if (fl->fl_start + l->l_len < 0)
379ef12e72aSJ. Bruce Fields 			return -EINVAL;
380ef12e72aSJ. Bruce Fields 		fl->fl_end = fl->fl_start - 1;
381ef12e72aSJ. Bruce Fields 		fl->fl_start += l->l_len;
382ef12e72aSJ. Bruce Fields 	} else
383ef12e72aSJ. Bruce Fields 		fl->fl_end = OFFSET_MAX;
384ef12e72aSJ. Bruce Fields 
385ef12e72aSJ. Bruce Fields 	fl->fl_owner = current->files;
386ef12e72aSJ. Bruce Fields 	fl->fl_pid = current->tgid;
387ef12e72aSJ. Bruce Fields 	fl->fl_file = filp;
388ef12e72aSJ. Bruce Fields 	fl->fl_flags = FL_POSIX;
389ef12e72aSJ. Bruce Fields 	fl->fl_ops = NULL;
390ef12e72aSJ. Bruce Fields 	fl->fl_lmops = NULL;
391ef12e72aSJ. Bruce Fields 
392bce7560dSJeff Layton 	/* Ensure that fl->fl_filp has compatible f_mode */
393bce7560dSJeff Layton 	switch (l->l_type) {
394bce7560dSJeff Layton 	case F_RDLCK:
395bce7560dSJeff Layton 		if (!(filp->f_mode & FMODE_READ))
396bce7560dSJeff Layton 			return -EBADF;
397bce7560dSJeff Layton 		break;
398bce7560dSJeff Layton 	case F_WRLCK:
399bce7560dSJeff Layton 		if (!(filp->f_mode & FMODE_WRITE))
400bce7560dSJeff Layton 			return -EBADF;
401bce7560dSJeff Layton 		break;
402bce7560dSJeff Layton 	}
403bce7560dSJeff Layton 
404ef12e72aSJ. Bruce Fields 	return assign_type(fl, l->l_type);
405ef12e72aSJ. Bruce Fields }
406ef12e72aSJ. Bruce Fields 
4071da177e4SLinus Torvalds /* Verify a "struct flock" and copy it to a "struct file_lock" as a POSIX
4081da177e4SLinus Torvalds  * style lock.
4091da177e4SLinus Torvalds  */
4101da177e4SLinus Torvalds static int flock_to_posix_lock(struct file *filp, struct file_lock *fl,
4111da177e4SLinus Torvalds 			       struct flock *l)
4121da177e4SLinus Torvalds {
413ef12e72aSJ. Bruce Fields 	struct flock64 ll = {
414ef12e72aSJ. Bruce Fields 		.l_type = l->l_type,
415ef12e72aSJ. Bruce Fields 		.l_whence = l->l_whence,
416ef12e72aSJ. Bruce Fields 		.l_start = l->l_start,
417ef12e72aSJ. Bruce Fields 		.l_len = l->l_len,
418ef12e72aSJ. Bruce Fields 	};
4191da177e4SLinus Torvalds 
420ef12e72aSJ. Bruce Fields 	return flock64_to_posix_lock(filp, fl, &ll);
4211da177e4SLinus Torvalds }
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds /* default lease lock manager operations */
4241da177e4SLinus Torvalds static void lease_break_callback(struct file_lock *fl)
4251da177e4SLinus Torvalds {
4261da177e4SLinus Torvalds 	kill_fasync(&fl->fl_fasync, SIGIO, POLL_MSG);
4271da177e4SLinus Torvalds }
4281da177e4SLinus Torvalds 
4297b021967SAlexey Dobriyan static const struct lock_manager_operations lease_manager_ops = {
4308fb47a4fSJ. Bruce Fields 	.lm_break = lease_break_callback,
4318fb47a4fSJ. Bruce Fields 	.lm_change = lease_modify,
4321da177e4SLinus Torvalds };
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds /*
4351da177e4SLinus Torvalds  * Initialize a lease, use the default lock manager operations
4361da177e4SLinus Torvalds  */
4370ec4f431SJ. Bruce Fields static int lease_init(struct file *filp, long type, struct file_lock *fl)
4381da177e4SLinus Torvalds  {
43975dff55aSTrond Myklebust 	if (assign_type(fl, type) != 0)
44075dff55aSTrond Myklebust 		return -EINVAL;
44175dff55aSTrond Myklebust 
4421da177e4SLinus Torvalds 	fl->fl_owner = current->files;
4431da177e4SLinus Torvalds 	fl->fl_pid = current->tgid;
4441da177e4SLinus Torvalds 
4451da177e4SLinus Torvalds 	fl->fl_file = filp;
4461da177e4SLinus Torvalds 	fl->fl_flags = FL_LEASE;
4471da177e4SLinus Torvalds 	fl->fl_start = 0;
4481da177e4SLinus Torvalds 	fl->fl_end = OFFSET_MAX;
4491da177e4SLinus Torvalds 	fl->fl_ops = NULL;
4501da177e4SLinus Torvalds 	fl->fl_lmops = &lease_manager_ops;
4511da177e4SLinus Torvalds 	return 0;
4521da177e4SLinus Torvalds }
4531da177e4SLinus Torvalds 
4541da177e4SLinus Torvalds /* Allocate a file_lock initialised to this type of lease */
4550ec4f431SJ. Bruce Fields static struct file_lock *lease_alloc(struct file *filp, long type)
4561da177e4SLinus Torvalds {
4571da177e4SLinus Torvalds 	struct file_lock *fl = locks_alloc_lock();
45875dff55aSTrond Myklebust 	int error = -ENOMEM;
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds 	if (fl == NULL)
461e32b8ee2SJ. Bruce Fields 		return ERR_PTR(error);
4621da177e4SLinus Torvalds 
4631da177e4SLinus Torvalds 	error = lease_init(filp, type, fl);
46475dff55aSTrond Myklebust 	if (error) {
46575dff55aSTrond Myklebust 		locks_free_lock(fl);
466e32b8ee2SJ. Bruce Fields 		return ERR_PTR(error);
46775dff55aSTrond Myklebust 	}
468e32b8ee2SJ. Bruce Fields 	return fl;
4691da177e4SLinus Torvalds }
4701da177e4SLinus Torvalds 
4711da177e4SLinus Torvalds /* Check if two locks overlap each other.
4721da177e4SLinus Torvalds  */
4731da177e4SLinus Torvalds static inline int locks_overlap(struct file_lock *fl1, struct file_lock *fl2)
4741da177e4SLinus Torvalds {
4751da177e4SLinus Torvalds 	return ((fl1->fl_end >= fl2->fl_start) &&
4761da177e4SLinus Torvalds 		(fl2->fl_end >= fl1->fl_start));
4771da177e4SLinus Torvalds }
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds /*
4801da177e4SLinus Torvalds  * Check whether two locks have the same owner.
4811da177e4SLinus Torvalds  */
48233443c42SMatt Mackall static int posix_same_owner(struct file_lock *fl1, struct file_lock *fl2)
4831da177e4SLinus Torvalds {
4848fb47a4fSJ. Bruce Fields 	if (fl1->fl_lmops && fl1->fl_lmops->lm_compare_owner)
4851da177e4SLinus Torvalds 		return fl2->fl_lmops == fl1->fl_lmops &&
4868fb47a4fSJ. Bruce Fields 			fl1->fl_lmops->lm_compare_owner(fl1, fl2);
4871da177e4SLinus Torvalds 	return fl1->fl_owner == fl2->fl_owner;
4881da177e4SLinus Torvalds }
4891da177e4SLinus Torvalds 
4907012b02aSJeff Layton /* Must be called with the i_lock held! */
4916ca10ed8SJeff Layton static void locks_insert_global_locks(struct file_lock *fl)
49288974691SJeff Layton {
4937012b02aSJeff Layton 	lg_local_lock(&file_lock_lglock);
4947012b02aSJeff Layton 	fl->fl_link_cpu = smp_processor_id();
4957012b02aSJeff Layton 	hlist_add_head(&fl->fl_link, this_cpu_ptr(&file_lock_list));
4967012b02aSJeff Layton 	lg_local_unlock(&file_lock_lglock);
49788974691SJeff Layton }
49888974691SJeff Layton 
4997012b02aSJeff Layton /* Must be called with the i_lock held! */
5006ca10ed8SJeff Layton static void locks_delete_global_locks(struct file_lock *fl)
50188974691SJeff Layton {
5027012b02aSJeff Layton 	/*
5037012b02aSJeff Layton 	 * Avoid taking lock if already unhashed. This is safe since this check
5047012b02aSJeff Layton 	 * is done while holding the i_lock, and new insertions into the list
5057012b02aSJeff Layton 	 * also require that it be held.
5067012b02aSJeff Layton 	 */
5077012b02aSJeff Layton 	if (hlist_unhashed(&fl->fl_link))
5087012b02aSJeff Layton 		return;
5097012b02aSJeff Layton 	lg_local_lock_cpu(&file_lock_lglock, fl->fl_link_cpu);
510139ca04eSJeff Layton 	hlist_del_init(&fl->fl_link);
5117012b02aSJeff Layton 	lg_local_unlock_cpu(&file_lock_lglock, fl->fl_link_cpu);
51288974691SJeff Layton }
51388974691SJeff Layton 
5143999e493SJeff Layton static unsigned long
5153999e493SJeff Layton posix_owner_key(struct file_lock *fl)
5163999e493SJeff Layton {
5173999e493SJeff Layton 	if (fl->fl_lmops && fl->fl_lmops->lm_owner_key)
5183999e493SJeff Layton 		return fl->fl_lmops->lm_owner_key(fl);
5193999e493SJeff Layton 	return (unsigned long)fl->fl_owner;
5203999e493SJeff Layton }
5213999e493SJeff Layton 
5226ca10ed8SJeff Layton static void locks_insert_global_blocked(struct file_lock *waiter)
52388974691SJeff Layton {
5243999e493SJeff Layton 	hash_add(blocked_hash, &waiter->fl_link, posix_owner_key(waiter));
52588974691SJeff Layton }
52688974691SJeff Layton 
5276ca10ed8SJeff Layton static void locks_delete_global_blocked(struct file_lock *waiter)
52888974691SJeff Layton {
52948f74186SJeff Layton 	hash_del(&waiter->fl_link);
53088974691SJeff Layton }
53188974691SJeff Layton 
5321da177e4SLinus Torvalds /* Remove waiter from blocker's block list.
5331da177e4SLinus Torvalds  * When blocker ends up pointing to itself then the list is empty.
5341c8c601aSJeff Layton  *
5357b2296afSJeff Layton  * Must be called with blocked_lock_lock held.
5361da177e4SLinus Torvalds  */
53733443c42SMatt Mackall static void __locks_delete_block(struct file_lock *waiter)
5381da177e4SLinus Torvalds {
53988974691SJeff Layton 	locks_delete_global_blocked(waiter);
5401da177e4SLinus Torvalds 	list_del_init(&waiter->fl_block);
5411da177e4SLinus Torvalds 	waiter->fl_next = NULL;
5421da177e4SLinus Torvalds }
5431da177e4SLinus Torvalds 
5441a9e64a7SJeff Layton static void locks_delete_block(struct file_lock *waiter)
5451da177e4SLinus Torvalds {
5467b2296afSJeff Layton 	spin_lock(&blocked_lock_lock);
5471da177e4SLinus Torvalds 	__locks_delete_block(waiter);
5487b2296afSJeff Layton 	spin_unlock(&blocked_lock_lock);
5491da177e4SLinus Torvalds }
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds /* Insert waiter into blocker's block list.
5521da177e4SLinus Torvalds  * We use a circular list so that processes can be easily woken up in
5531da177e4SLinus Torvalds  * the order they blocked. The documentation doesn't require this but
5541da177e4SLinus Torvalds  * it seems like the reasonable thing to do.
5551c8c601aSJeff Layton  *
5567b2296afSJeff Layton  * Must be called with both the i_lock and blocked_lock_lock held. The fl_block
55746dad760SJeff Layton  * list itself is protected by the blocked_lock_lock, but by ensuring that the
5587b2296afSJeff Layton  * i_lock is also held on insertions we can avoid taking the blocked_lock_lock
5594e8c765dSJeff Layton  * in some cases when we see that the fl_block list is empty.
5601da177e4SLinus Torvalds  */
5611c8c601aSJeff Layton static void __locks_insert_block(struct file_lock *blocker,
5621da177e4SLinus Torvalds 					struct file_lock *waiter)
5631da177e4SLinus Torvalds {
5646dc0fe8fSJ. Bruce Fields 	BUG_ON(!list_empty(&waiter->fl_block));
5651da177e4SLinus Torvalds 	waiter->fl_next = blocker;
56688974691SJeff Layton 	list_add_tail(&waiter->fl_block, &blocker->fl_block);
56757b65325SJeff Layton 	if (IS_POSIX(blocker) && !IS_FILE_PVT(blocker))
5681c8c601aSJeff Layton 		locks_insert_global_blocked(waiter);
5691c8c601aSJeff Layton }
5701c8c601aSJeff Layton 
5711c8c601aSJeff Layton /* Must be called with i_lock held. */
5721c8c601aSJeff Layton static void locks_insert_block(struct file_lock *blocker,
5731c8c601aSJeff Layton 					struct file_lock *waiter)
5741c8c601aSJeff Layton {
5757b2296afSJeff Layton 	spin_lock(&blocked_lock_lock);
5761c8c601aSJeff Layton 	__locks_insert_block(blocker, waiter);
5777b2296afSJeff Layton 	spin_unlock(&blocked_lock_lock);
5781da177e4SLinus Torvalds }
5791da177e4SLinus Torvalds 
5801cb36012SJeff Layton /*
5811cb36012SJeff Layton  * Wake up processes blocked waiting for blocker.
5821cb36012SJeff Layton  *
5831c8c601aSJeff Layton  * Must be called with the inode->i_lock held!
5841da177e4SLinus Torvalds  */
5851da177e4SLinus Torvalds static void locks_wake_up_blocks(struct file_lock *blocker)
5861da177e4SLinus Torvalds {
5874e8c765dSJeff Layton 	/*
5884e8c765dSJeff Layton 	 * Avoid taking global lock if list is empty. This is safe since new
5894e8c765dSJeff Layton 	 * blocked requests are only added to the list under the i_lock, and
5904e8c765dSJeff Layton 	 * the i_lock is always held here. Note that removal from the fl_block
5914e8c765dSJeff Layton 	 * list does not require the i_lock, so we must recheck list_empty()
5927b2296afSJeff Layton 	 * after acquiring the blocked_lock_lock.
5934e8c765dSJeff Layton 	 */
5944e8c765dSJeff Layton 	if (list_empty(&blocker->fl_block))
5954e8c765dSJeff Layton 		return;
5964e8c765dSJeff Layton 
5977b2296afSJeff Layton 	spin_lock(&blocked_lock_lock);
5981da177e4SLinus Torvalds 	while (!list_empty(&blocker->fl_block)) {
599f0c1cd0eSPavel Emelyanov 		struct file_lock *waiter;
600f0c1cd0eSPavel Emelyanov 
601f0c1cd0eSPavel Emelyanov 		waiter = list_first_entry(&blocker->fl_block,
6021da177e4SLinus Torvalds 				struct file_lock, fl_block);
6031da177e4SLinus Torvalds 		__locks_delete_block(waiter);
6048fb47a4fSJ. Bruce Fields 		if (waiter->fl_lmops && waiter->fl_lmops->lm_notify)
6058fb47a4fSJ. Bruce Fields 			waiter->fl_lmops->lm_notify(waiter);
6061da177e4SLinus Torvalds 		else
6071da177e4SLinus Torvalds 			wake_up(&waiter->fl_wait);
6081da177e4SLinus Torvalds 	}
6097b2296afSJeff Layton 	spin_unlock(&blocked_lock_lock);
6101da177e4SLinus Torvalds }
6111da177e4SLinus Torvalds 
6121da177e4SLinus Torvalds /* Insert file lock fl into an inode's lock list at the position indicated
6131da177e4SLinus Torvalds  * by pos. At the same time add the lock to the global file lock list.
6141c8c601aSJeff Layton  *
6151c8c601aSJeff Layton  * Must be called with the i_lock held!
6161da177e4SLinus Torvalds  */
6171da177e4SLinus Torvalds static void locks_insert_lock(struct file_lock **pos, struct file_lock *fl)
6181da177e4SLinus Torvalds {
619ab1f1611SVitaliy Gusev 	fl->fl_nspid = get_pid(task_tgid(current));
620ab1f1611SVitaliy Gusev 
6211da177e4SLinus Torvalds 	/* insert into file's list */
6221da177e4SLinus Torvalds 	fl->fl_next = *pos;
6231da177e4SLinus Torvalds 	*pos = fl;
62488974691SJeff Layton 
62588974691SJeff Layton 	locks_insert_global_locks(fl);
6261da177e4SLinus Torvalds }
6271da177e4SLinus Torvalds 
62824cbe784SJeff Layton /**
62924cbe784SJeff Layton  * locks_delete_lock - Delete a lock and then free it.
63024cbe784SJeff Layton  * @thisfl_p: pointer that points to the fl_next field of the previous
63124cbe784SJeff Layton  * 	      inode->i_flock list entry
63224cbe784SJeff Layton  *
63324cbe784SJeff Layton  * Unlink a lock from all lists and free the namespace reference, but don't
63424cbe784SJeff Layton  * free it yet. Wake up processes that are blocked waiting for this lock and
63524cbe784SJeff Layton  * notify the FS that the lock has been cleared.
6361c8c601aSJeff Layton  *
6371c8c601aSJeff Layton  * Must be called with the i_lock held!
6381da177e4SLinus Torvalds  */
63924cbe784SJeff Layton static void locks_unlink_lock(struct file_lock **thisfl_p)
6401da177e4SLinus Torvalds {
6411da177e4SLinus Torvalds 	struct file_lock *fl = *thisfl_p;
6421da177e4SLinus Torvalds 
64388974691SJeff Layton 	locks_delete_global_locks(fl);
64488974691SJeff Layton 
6451da177e4SLinus Torvalds 	*thisfl_p = fl->fl_next;
6461da177e4SLinus Torvalds 	fl->fl_next = NULL;
6471da177e4SLinus Torvalds 
648ab1f1611SVitaliy Gusev 	if (fl->fl_nspid) {
649ab1f1611SVitaliy Gusev 		put_pid(fl->fl_nspid);
650ab1f1611SVitaliy Gusev 		fl->fl_nspid = NULL;
651ab1f1611SVitaliy Gusev 	}
652ab1f1611SVitaliy Gusev 
6531da177e4SLinus Torvalds 	locks_wake_up_blocks(fl);
65424cbe784SJeff Layton }
65524cbe784SJeff Layton 
65624cbe784SJeff Layton /*
65724cbe784SJeff Layton  * Unlink a lock from all lists and free it.
65824cbe784SJeff Layton  *
65924cbe784SJeff Layton  * Must be called with i_lock held!
66024cbe784SJeff Layton  */
66124cbe784SJeff Layton static void locks_delete_lock(struct file_lock **thisfl_p)
66224cbe784SJeff Layton {
66324cbe784SJeff Layton 	struct file_lock *fl = *thisfl_p;
66424cbe784SJeff Layton 
66524cbe784SJeff Layton 	locks_unlink_lock(thisfl_p);
6661da177e4SLinus Torvalds 	locks_free_lock(fl);
6671da177e4SLinus Torvalds }
6681da177e4SLinus Torvalds 
6691da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. Common functionality
6701da177e4SLinus Torvalds  * checks for shared/exclusive status of overlapping locks.
6711da177e4SLinus Torvalds  */
6721da177e4SLinus Torvalds static int locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
6731da177e4SLinus Torvalds {
6741da177e4SLinus Torvalds 	if (sys_fl->fl_type == F_WRLCK)
6751da177e4SLinus Torvalds 		return 1;
6761da177e4SLinus Torvalds 	if (caller_fl->fl_type == F_WRLCK)
6771da177e4SLinus Torvalds 		return 1;
6781da177e4SLinus Torvalds 	return 0;
6791da177e4SLinus Torvalds }
6801da177e4SLinus Torvalds 
6811da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. POSIX specific
6821da177e4SLinus Torvalds  * checking before calling the locks_conflict().
6831da177e4SLinus Torvalds  */
6841da177e4SLinus Torvalds static int posix_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
6851da177e4SLinus Torvalds {
6861da177e4SLinus Torvalds 	/* POSIX locks owned by the same process do not conflict with
6871da177e4SLinus Torvalds 	 * each other.
6881da177e4SLinus Torvalds 	 */
6891da177e4SLinus Torvalds 	if (!IS_POSIX(sys_fl) || posix_same_owner(caller_fl, sys_fl))
6901da177e4SLinus Torvalds 		return (0);
6911da177e4SLinus Torvalds 
6921da177e4SLinus Torvalds 	/* Check whether they overlap */
6931da177e4SLinus Torvalds 	if (!locks_overlap(caller_fl, sys_fl))
6941da177e4SLinus Torvalds 		return 0;
6951da177e4SLinus Torvalds 
6961da177e4SLinus Torvalds 	return (locks_conflict(caller_fl, sys_fl));
6971da177e4SLinus Torvalds }
6981da177e4SLinus Torvalds 
6991da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. FLOCK specific
7001da177e4SLinus Torvalds  * checking before calling the locks_conflict().
7011da177e4SLinus Torvalds  */
7021da177e4SLinus Torvalds static int flock_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
7031da177e4SLinus Torvalds {
7041da177e4SLinus Torvalds 	/* FLOCK locks referring to the same filp do not conflict with
7051da177e4SLinus Torvalds 	 * each other.
7061da177e4SLinus Torvalds 	 */
7071da177e4SLinus Torvalds 	if (!IS_FLOCK(sys_fl) || (caller_fl->fl_file == sys_fl->fl_file))
7081da177e4SLinus Torvalds 		return (0);
7091da177e4SLinus Torvalds 	if ((caller_fl->fl_type & LOCK_MAND) || (sys_fl->fl_type & LOCK_MAND))
7101da177e4SLinus Torvalds 		return 0;
7111da177e4SLinus Torvalds 
7121da177e4SLinus Torvalds 	return (locks_conflict(caller_fl, sys_fl));
7131da177e4SLinus Torvalds }
7141da177e4SLinus Torvalds 
7156d34ac19SJ. Bruce Fields void
7169d6a8c5cSMarc Eshel posix_test_lock(struct file *filp, struct file_lock *fl)
7171da177e4SLinus Torvalds {
7181da177e4SLinus Torvalds 	struct file_lock *cfl;
7191c8c601aSJeff Layton 	struct inode *inode = file_inode(filp);
7201da177e4SLinus Torvalds 
7211c8c601aSJeff Layton 	spin_lock(&inode->i_lock);
722496ad9aaSAl Viro 	for (cfl = file_inode(filp)->i_flock; cfl; cfl = cfl->fl_next) {
7231da177e4SLinus Torvalds 		if (!IS_POSIX(cfl))
7241da177e4SLinus Torvalds 			continue;
725b842e240SJ. Bruce Fields 		if (posix_locks_conflict(fl, cfl))
7261da177e4SLinus Torvalds 			break;
7271da177e4SLinus Torvalds 	}
728ab1f1611SVitaliy Gusev 	if (cfl) {
7299d6a8c5cSMarc Eshel 		__locks_copy_lock(fl, cfl);
730ab1f1611SVitaliy Gusev 		if (cfl->fl_nspid)
7316c5f3e7bSPavel Emelyanov 			fl->fl_pid = pid_vnr(cfl->fl_nspid);
732ab1f1611SVitaliy Gusev 	} else
733129a84deSJ. Bruce Fields 		fl->fl_type = F_UNLCK;
7341c8c601aSJeff Layton 	spin_unlock(&inode->i_lock);
7356d34ac19SJ. Bruce Fields 	return;
7361da177e4SLinus Torvalds }
7371da177e4SLinus Torvalds EXPORT_SYMBOL(posix_test_lock);
7381da177e4SLinus Torvalds 
739b533184fSJ. Bruce Fields /*
740b533184fSJ. Bruce Fields  * Deadlock detection:
7411da177e4SLinus Torvalds  *
742b533184fSJ. Bruce Fields  * We attempt to detect deadlocks that are due purely to posix file
743b533184fSJ. Bruce Fields  * locks.
7441da177e4SLinus Torvalds  *
745b533184fSJ. Bruce Fields  * We assume that a task can be waiting for at most one lock at a time.
746b533184fSJ. Bruce Fields  * So for any acquired lock, the process holding that lock may be
747b533184fSJ. Bruce Fields  * waiting on at most one other lock.  That lock in turns may be held by
748b533184fSJ. Bruce Fields  * someone waiting for at most one other lock.  Given a requested lock
749b533184fSJ. Bruce Fields  * caller_fl which is about to wait for a conflicting lock block_fl, we
750b533184fSJ. Bruce Fields  * follow this chain of waiters to ensure we are not about to create a
751b533184fSJ. Bruce Fields  * cycle.
75297855b49SJ. Bruce Fields  *
753b533184fSJ. Bruce Fields  * Since we do this before we ever put a process to sleep on a lock, we
754b533184fSJ. Bruce Fields  * are ensured that there is never a cycle; that is what guarantees that
755b533184fSJ. Bruce Fields  * the while() loop in posix_locks_deadlock() eventually completes.
756b533184fSJ. Bruce Fields  *
757b533184fSJ. Bruce Fields  * Note: the above assumption may not be true when handling lock
758b533184fSJ. Bruce Fields  * requests from a broken NFS client. It may also fail in the presence
759b533184fSJ. Bruce Fields  * of tasks (such as posix threads) sharing the same open file table.
760b533184fSJ. Bruce Fields  * To handle those cases, we just bail out after a few iterations.
76157b65325SJeff Layton  *
76257b65325SJeff Layton  * For FL_FILE_PVT locks, the owner is the filp, not the files_struct.
76357b65325SJeff Layton  * Because the owner is not even nominally tied to a thread of
76457b65325SJeff Layton  * execution, the deadlock detection below can't reasonably work well. Just
76557b65325SJeff Layton  * skip it for those.
76657b65325SJeff Layton  *
76757b65325SJeff Layton  * In principle, we could do a more limited deadlock detection on FL_FILE_PVT
76857b65325SJeff Layton  * locks that just checks for the case where two tasks are attempting to
76957b65325SJeff Layton  * upgrade from read to write locks on the same inode.
7701da177e4SLinus Torvalds  */
77197855b49SJ. Bruce Fields 
77297855b49SJ. Bruce Fields #define MAX_DEADLK_ITERATIONS 10
77397855b49SJ. Bruce Fields 
774b533184fSJ. Bruce Fields /* Find a lock that the owner of the given block_fl is blocking on. */
775b533184fSJ. Bruce Fields static struct file_lock *what_owner_is_waiting_for(struct file_lock *block_fl)
776b533184fSJ. Bruce Fields {
777b533184fSJ. Bruce Fields 	struct file_lock *fl;
778b533184fSJ. Bruce Fields 
7793999e493SJeff Layton 	hash_for_each_possible(blocked_hash, fl, fl_link, posix_owner_key(block_fl)) {
780b533184fSJ. Bruce Fields 		if (posix_same_owner(fl, block_fl))
781b533184fSJ. Bruce Fields 			return fl->fl_next;
782b533184fSJ. Bruce Fields 	}
783b533184fSJ. Bruce Fields 	return NULL;
784b533184fSJ. Bruce Fields }
785b533184fSJ. Bruce Fields 
7867b2296afSJeff Layton /* Must be called with the blocked_lock_lock held! */
787b0904e14SAdrian Bunk static int posix_locks_deadlock(struct file_lock *caller_fl,
7881da177e4SLinus Torvalds 				struct file_lock *block_fl)
7891da177e4SLinus Torvalds {
79097855b49SJ. Bruce Fields 	int i = 0;
7911da177e4SLinus Torvalds 
79257b65325SJeff Layton 	/*
79357b65325SJeff Layton 	 * This deadlock detector can't reasonably detect deadlocks with
79457b65325SJeff Layton 	 * FL_FILE_PVT locks, since they aren't owned by a process, per-se.
79557b65325SJeff Layton 	 */
79657b65325SJeff Layton 	if (IS_FILE_PVT(caller_fl))
79757b65325SJeff Layton 		return 0;
79857b65325SJeff Layton 
799b533184fSJ. Bruce Fields 	while ((block_fl = what_owner_is_waiting_for(block_fl))) {
80097855b49SJ. Bruce Fields 		if (i++ > MAX_DEADLK_ITERATIONS)
80197855b49SJ. Bruce Fields 			return 0;
802b533184fSJ. Bruce Fields 		if (posix_same_owner(caller_fl, block_fl))
803b533184fSJ. Bruce Fields 			return 1;
8041da177e4SLinus Torvalds 	}
8051da177e4SLinus Torvalds 	return 0;
8061da177e4SLinus Torvalds }
8071da177e4SLinus Torvalds 
8081da177e4SLinus Torvalds /* Try to create a FLOCK lock on filp. We always insert new FLOCK locks
80902888f41SJ. Bruce Fields  * after any leases, but before any posix locks.
810f475ae95STrond Myklebust  *
811f475ae95STrond Myklebust  * Note that if called with an FL_EXISTS argument, the caller may determine
812f475ae95STrond Myklebust  * whether or not a lock was successfully freed by testing the return
813f475ae95STrond Myklebust  * value for -ENOENT.
8141da177e4SLinus Torvalds  */
815993dfa87STrond Myklebust static int flock_lock_file(struct file *filp, struct file_lock *request)
8161da177e4SLinus Torvalds {
817993dfa87STrond Myklebust 	struct file_lock *new_fl = NULL;
8181da177e4SLinus Torvalds 	struct file_lock **before;
819496ad9aaSAl Viro 	struct inode * inode = file_inode(filp);
8201da177e4SLinus Torvalds 	int error = 0;
8211da177e4SLinus Torvalds 	int found = 0;
8221da177e4SLinus Torvalds 
823b89f4321SArnd Bergmann 	if (!(request->fl_flags & FL_ACCESS) && (request->fl_type != F_UNLCK)) {
824b89f4321SArnd Bergmann 		new_fl = locks_alloc_lock();
825b89f4321SArnd Bergmann 		if (!new_fl)
826b89f4321SArnd Bergmann 			return -ENOMEM;
827b89f4321SArnd Bergmann 	}
828b89f4321SArnd Bergmann 
8291c8c601aSJeff Layton 	spin_lock(&inode->i_lock);
830f07f18ddSTrond Myklebust 	if (request->fl_flags & FL_ACCESS)
831f07f18ddSTrond Myklebust 		goto find_conflict;
83284d535adSPavel Emelyanov 
8331da177e4SLinus Torvalds 	for_each_lock(inode, before) {
8341da177e4SLinus Torvalds 		struct file_lock *fl = *before;
8351da177e4SLinus Torvalds 		if (IS_POSIX(fl))
8361da177e4SLinus Torvalds 			break;
8371da177e4SLinus Torvalds 		if (IS_LEASE(fl))
8381da177e4SLinus Torvalds 			continue;
8391da177e4SLinus Torvalds 		if (filp != fl->fl_file)
8401da177e4SLinus Torvalds 			continue;
841993dfa87STrond Myklebust 		if (request->fl_type == fl->fl_type)
8421da177e4SLinus Torvalds 			goto out;
8431da177e4SLinus Torvalds 		found = 1;
8441da177e4SLinus Torvalds 		locks_delete_lock(before);
8451da177e4SLinus Torvalds 		break;
8461da177e4SLinus Torvalds 	}
8471da177e4SLinus Torvalds 
848f475ae95STrond Myklebust 	if (request->fl_type == F_UNLCK) {
849f475ae95STrond Myklebust 		if ((request->fl_flags & FL_EXISTS) && !found)
850f475ae95STrond Myklebust 			error = -ENOENT;
851993dfa87STrond Myklebust 		goto out;
852f475ae95STrond Myklebust 	}
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds 	/*
8551da177e4SLinus Torvalds 	 * If a higher-priority process was blocked on the old file lock,
8561da177e4SLinus Torvalds 	 * give it the opportunity to lock the file.
8571da177e4SLinus Torvalds 	 */
858b89f4321SArnd Bergmann 	if (found) {
8591c8c601aSJeff Layton 		spin_unlock(&inode->i_lock);
860def01bc5SFrederic Weisbecker 		cond_resched();
8611c8c601aSJeff Layton 		spin_lock(&inode->i_lock);
862b89f4321SArnd Bergmann 	}
8631da177e4SLinus Torvalds 
864f07f18ddSTrond Myklebust find_conflict:
8651da177e4SLinus Torvalds 	for_each_lock(inode, before) {
8661da177e4SLinus Torvalds 		struct file_lock *fl = *before;
8671da177e4SLinus Torvalds 		if (IS_POSIX(fl))
8681da177e4SLinus Torvalds 			break;
8691da177e4SLinus Torvalds 		if (IS_LEASE(fl))
8701da177e4SLinus Torvalds 			continue;
871993dfa87STrond Myklebust 		if (!flock_locks_conflict(request, fl))
8721da177e4SLinus Torvalds 			continue;
8731da177e4SLinus Torvalds 		error = -EAGAIN;
874bde74e4bSMiklos Szeredi 		if (!(request->fl_flags & FL_SLEEP))
875bde74e4bSMiklos Szeredi 			goto out;
876bde74e4bSMiklos Szeredi 		error = FILE_LOCK_DEFERRED;
877993dfa87STrond Myklebust 		locks_insert_block(fl, request);
8781da177e4SLinus Torvalds 		goto out;
8791da177e4SLinus Torvalds 	}
880f07f18ddSTrond Myklebust 	if (request->fl_flags & FL_ACCESS)
881f07f18ddSTrond Myklebust 		goto out;
882993dfa87STrond Myklebust 	locks_copy_lock(new_fl, request);
8830e2f6db8SPavel Emelyanov 	locks_insert_lock(before, new_fl);
884993dfa87STrond Myklebust 	new_fl = NULL;
8859cedc194SKirill Korotaev 	error = 0;
8861da177e4SLinus Torvalds 
8871da177e4SLinus Torvalds out:
8881c8c601aSJeff Layton 	spin_unlock(&inode->i_lock);
889993dfa87STrond Myklebust 	if (new_fl)
890993dfa87STrond Myklebust 		locks_free_lock(new_fl);
8911da177e4SLinus Torvalds 	return error;
8921da177e4SLinus Torvalds }
8931da177e4SLinus Torvalds 
894150b3934SMarc Eshel static int __posix_lock_file(struct inode *inode, struct file_lock *request, struct file_lock *conflock)
8951da177e4SLinus Torvalds {
8961da177e4SLinus Torvalds 	struct file_lock *fl;
89739005d02SMiklos Szeredi 	struct file_lock *new_fl = NULL;
89839005d02SMiklos Szeredi 	struct file_lock *new_fl2 = NULL;
8991da177e4SLinus Torvalds 	struct file_lock *left = NULL;
9001da177e4SLinus Torvalds 	struct file_lock *right = NULL;
9011da177e4SLinus Torvalds 	struct file_lock **before;
902b9746ef8SJeff Layton 	int error;
903b9746ef8SJeff Layton 	bool added = false;
9041da177e4SLinus Torvalds 
9051da177e4SLinus Torvalds 	/*
9061da177e4SLinus Torvalds 	 * We may need two file_lock structures for this operation,
9071da177e4SLinus Torvalds 	 * so we get them in advance to avoid races.
90839005d02SMiklos Szeredi 	 *
90939005d02SMiklos Szeredi 	 * In some cases we can be sure, that no new locks will be needed
9101da177e4SLinus Torvalds 	 */
91139005d02SMiklos Szeredi 	if (!(request->fl_flags & FL_ACCESS) &&
91239005d02SMiklos Szeredi 	    (request->fl_type != F_UNLCK ||
91339005d02SMiklos Szeredi 	     request->fl_start != 0 || request->fl_end != OFFSET_MAX)) {
9141da177e4SLinus Torvalds 		new_fl = locks_alloc_lock();
9151da177e4SLinus Torvalds 		new_fl2 = locks_alloc_lock();
91639005d02SMiklos Szeredi 	}
9171da177e4SLinus Torvalds 
9181c8c601aSJeff Layton 	spin_lock(&inode->i_lock);
9191cb36012SJeff Layton 	/*
9201cb36012SJeff Layton 	 * New lock request. Walk all POSIX locks and look for conflicts. If
9211cb36012SJeff Layton 	 * there are any, either return error or put the request on the
92248f74186SJeff Layton 	 * blocker's list of waiters and the global blocked_hash.
9231cb36012SJeff Layton 	 */
9241da177e4SLinus Torvalds 	if (request->fl_type != F_UNLCK) {
9251da177e4SLinus Torvalds 		for_each_lock(inode, before) {
926526985b9SJ. Bruce Fields 			fl = *before;
9271da177e4SLinus Torvalds 			if (!IS_POSIX(fl))
9281da177e4SLinus Torvalds 				continue;
9291da177e4SLinus Torvalds 			if (!posix_locks_conflict(request, fl))
9301da177e4SLinus Torvalds 				continue;
9315842add2SAndy Adamson 			if (conflock)
9321a747ee0SJ. Bruce Fields 				__locks_copy_lock(conflock, fl);
9331da177e4SLinus Torvalds 			error = -EAGAIN;
9341da177e4SLinus Torvalds 			if (!(request->fl_flags & FL_SLEEP))
9351da177e4SLinus Torvalds 				goto out;
9361c8c601aSJeff Layton 			/*
9371c8c601aSJeff Layton 			 * Deadlock detection and insertion into the blocked
9381c8c601aSJeff Layton 			 * locks list must be done while holding the same lock!
9391c8c601aSJeff Layton 			 */
9401da177e4SLinus Torvalds 			error = -EDEADLK;
9417b2296afSJeff Layton 			spin_lock(&blocked_lock_lock);
9421c8c601aSJeff Layton 			if (likely(!posix_locks_deadlock(request, fl))) {
943bde74e4bSMiklos Szeredi 				error = FILE_LOCK_DEFERRED;
9441c8c601aSJeff Layton 				__locks_insert_block(fl, request);
9451c8c601aSJeff Layton 			}
9467b2296afSJeff Layton 			spin_unlock(&blocked_lock_lock);
9471da177e4SLinus Torvalds 			goto out;
9481da177e4SLinus Torvalds   		}
9491da177e4SLinus Torvalds   	}
9501da177e4SLinus Torvalds 
9511da177e4SLinus Torvalds 	/* If we're just looking for a conflict, we're done. */
9521da177e4SLinus Torvalds 	error = 0;
9531da177e4SLinus Torvalds 	if (request->fl_flags & FL_ACCESS)
9541da177e4SLinus Torvalds 		goto out;
9551da177e4SLinus Torvalds 
9561da177e4SLinus Torvalds 	/*
9571da177e4SLinus Torvalds 	 * Find the first old lock with the same owner as the new lock.
9581da177e4SLinus Torvalds 	 */
9591da177e4SLinus Torvalds 
9601da177e4SLinus Torvalds 	before = &inode->i_flock;
9611da177e4SLinus Torvalds 
9621da177e4SLinus Torvalds 	/* First skip locks owned by other processes.  */
9631da177e4SLinus Torvalds 	while ((fl = *before) && (!IS_POSIX(fl) ||
9641da177e4SLinus Torvalds 				  !posix_same_owner(request, fl))) {
9651da177e4SLinus Torvalds 		before = &fl->fl_next;
9661da177e4SLinus Torvalds 	}
9671da177e4SLinus Torvalds 
9681da177e4SLinus Torvalds 	/* Process locks with this owner. */
9691da177e4SLinus Torvalds 	while ((fl = *before) && posix_same_owner(request, fl)) {
9701da177e4SLinus Torvalds 		/* Detect adjacent or overlapping regions (if same lock type)
9711da177e4SLinus Torvalds 		 */
9721da177e4SLinus Torvalds 		if (request->fl_type == fl->fl_type) {
973449231d6SOlaf Kirch 			/* In all comparisons of start vs end, use
974449231d6SOlaf Kirch 			 * "start - 1" rather than "end + 1". If end
975449231d6SOlaf Kirch 			 * is OFFSET_MAX, end + 1 will become negative.
976449231d6SOlaf Kirch 			 */
9771da177e4SLinus Torvalds 			if (fl->fl_end < request->fl_start - 1)
9781da177e4SLinus Torvalds 				goto next_lock;
9791da177e4SLinus Torvalds 			/* If the next lock in the list has entirely bigger
9801da177e4SLinus Torvalds 			 * addresses than the new one, insert the lock here.
9811da177e4SLinus Torvalds 			 */
982449231d6SOlaf Kirch 			if (fl->fl_start - 1 > request->fl_end)
9831da177e4SLinus Torvalds 				break;
9841da177e4SLinus Torvalds 
9851da177e4SLinus Torvalds 			/* If we come here, the new and old lock are of the
9861da177e4SLinus Torvalds 			 * same type and adjacent or overlapping. Make one
9871da177e4SLinus Torvalds 			 * lock yielding from the lower start address of both
9881da177e4SLinus Torvalds 			 * locks to the higher end address.
9891da177e4SLinus Torvalds 			 */
9901da177e4SLinus Torvalds 			if (fl->fl_start > request->fl_start)
9911da177e4SLinus Torvalds 				fl->fl_start = request->fl_start;
9921da177e4SLinus Torvalds 			else
9931da177e4SLinus Torvalds 				request->fl_start = fl->fl_start;
9941da177e4SLinus Torvalds 			if (fl->fl_end < request->fl_end)
9951da177e4SLinus Torvalds 				fl->fl_end = request->fl_end;
9961da177e4SLinus Torvalds 			else
9971da177e4SLinus Torvalds 				request->fl_end = fl->fl_end;
9981da177e4SLinus Torvalds 			if (added) {
9991da177e4SLinus Torvalds 				locks_delete_lock(before);
10001da177e4SLinus Torvalds 				continue;
10011da177e4SLinus Torvalds 			}
10021da177e4SLinus Torvalds 			request = fl;
1003b9746ef8SJeff Layton 			added = true;
10041da177e4SLinus Torvalds 		}
10051da177e4SLinus Torvalds 		else {
10061da177e4SLinus Torvalds 			/* Processing for different lock types is a bit
10071da177e4SLinus Torvalds 			 * more complex.
10081da177e4SLinus Torvalds 			 */
10091da177e4SLinus Torvalds 			if (fl->fl_end < request->fl_start)
10101da177e4SLinus Torvalds 				goto next_lock;
10111da177e4SLinus Torvalds 			if (fl->fl_start > request->fl_end)
10121da177e4SLinus Torvalds 				break;
10131da177e4SLinus Torvalds 			if (request->fl_type == F_UNLCK)
1014b9746ef8SJeff Layton 				added = true;
10151da177e4SLinus Torvalds 			if (fl->fl_start < request->fl_start)
10161da177e4SLinus Torvalds 				left = fl;
10171da177e4SLinus Torvalds 			/* If the next lock in the list has a higher end
10181da177e4SLinus Torvalds 			 * address than the new one, insert the new one here.
10191da177e4SLinus Torvalds 			 */
10201da177e4SLinus Torvalds 			if (fl->fl_end > request->fl_end) {
10211da177e4SLinus Torvalds 				right = fl;
10221da177e4SLinus Torvalds 				break;
10231da177e4SLinus Torvalds 			}
10241da177e4SLinus Torvalds 			if (fl->fl_start >= request->fl_start) {
10251da177e4SLinus Torvalds 				/* The new lock completely replaces an old
10261da177e4SLinus Torvalds 				 * one (This may happen several times).
10271da177e4SLinus Torvalds 				 */
10281da177e4SLinus Torvalds 				if (added) {
10291da177e4SLinus Torvalds 					locks_delete_lock(before);
10301da177e4SLinus Torvalds 					continue;
10311da177e4SLinus Torvalds 				}
10321da177e4SLinus Torvalds 				/* Replace the old lock with the new one.
10331da177e4SLinus Torvalds 				 * Wake up anybody waiting for the old one,
10341da177e4SLinus Torvalds 				 * as the change in lock type might satisfy
10351da177e4SLinus Torvalds 				 * their needs.
10361da177e4SLinus Torvalds 				 */
10371da177e4SLinus Torvalds 				locks_wake_up_blocks(fl);
10381da177e4SLinus Torvalds 				fl->fl_start = request->fl_start;
10391da177e4SLinus Torvalds 				fl->fl_end = request->fl_end;
10401da177e4SLinus Torvalds 				fl->fl_type = request->fl_type;
104147831f35STrond Myklebust 				locks_release_private(fl);
104247831f35STrond Myklebust 				locks_copy_private(fl, request);
10431da177e4SLinus Torvalds 				request = fl;
1044b9746ef8SJeff Layton 				added = true;
10451da177e4SLinus Torvalds 			}
10461da177e4SLinus Torvalds 		}
10471da177e4SLinus Torvalds 		/* Go on to next lock.
10481da177e4SLinus Torvalds 		 */
10491da177e4SLinus Torvalds 	next_lock:
10501da177e4SLinus Torvalds 		before = &fl->fl_next;
10511da177e4SLinus Torvalds 	}
10521da177e4SLinus Torvalds 
10530d9a490aSMiklos Szeredi 	/*
10541cb36012SJeff Layton 	 * The above code only modifies existing locks in case of merging or
10551cb36012SJeff Layton 	 * replacing. If new lock(s) need to be inserted all modifications are
10561cb36012SJeff Layton 	 * done below this, so it's safe yet to bail out.
10570d9a490aSMiklos Szeredi 	 */
10580d9a490aSMiklos Szeredi 	error = -ENOLCK; /* "no luck" */
10590d9a490aSMiklos Szeredi 	if (right && left == right && !new_fl2)
10600d9a490aSMiklos Szeredi 		goto out;
10610d9a490aSMiklos Szeredi 
10621da177e4SLinus Torvalds 	error = 0;
10631da177e4SLinus Torvalds 	if (!added) {
1064f475ae95STrond Myklebust 		if (request->fl_type == F_UNLCK) {
1065f475ae95STrond Myklebust 			if (request->fl_flags & FL_EXISTS)
1066f475ae95STrond Myklebust 				error = -ENOENT;
10671da177e4SLinus Torvalds 			goto out;
1068f475ae95STrond Myklebust 		}
10690d9a490aSMiklos Szeredi 
10700d9a490aSMiklos Szeredi 		if (!new_fl) {
10710d9a490aSMiklos Szeredi 			error = -ENOLCK;
10720d9a490aSMiklos Szeredi 			goto out;
10730d9a490aSMiklos Szeredi 		}
10741da177e4SLinus Torvalds 		locks_copy_lock(new_fl, request);
10751da177e4SLinus Torvalds 		locks_insert_lock(before, new_fl);
10761da177e4SLinus Torvalds 		new_fl = NULL;
10771da177e4SLinus Torvalds 	}
10781da177e4SLinus Torvalds 	if (right) {
10791da177e4SLinus Torvalds 		if (left == right) {
10801da177e4SLinus Torvalds 			/* The new lock breaks the old one in two pieces,
10811da177e4SLinus Torvalds 			 * so we have to use the second new lock.
10821da177e4SLinus Torvalds 			 */
10831da177e4SLinus Torvalds 			left = new_fl2;
10841da177e4SLinus Torvalds 			new_fl2 = NULL;
10851da177e4SLinus Torvalds 			locks_copy_lock(left, right);
10861da177e4SLinus Torvalds 			locks_insert_lock(before, left);
10871da177e4SLinus Torvalds 		}
10881da177e4SLinus Torvalds 		right->fl_start = request->fl_end + 1;
10891da177e4SLinus Torvalds 		locks_wake_up_blocks(right);
10901da177e4SLinus Torvalds 	}
10911da177e4SLinus Torvalds 	if (left) {
10921da177e4SLinus Torvalds 		left->fl_end = request->fl_start - 1;
10931da177e4SLinus Torvalds 		locks_wake_up_blocks(left);
10941da177e4SLinus Torvalds 	}
10951da177e4SLinus Torvalds  out:
10961c8c601aSJeff Layton 	spin_unlock(&inode->i_lock);
10971da177e4SLinus Torvalds 	/*
10981da177e4SLinus Torvalds 	 * Free any unused locks.
10991da177e4SLinus Torvalds 	 */
11001da177e4SLinus Torvalds 	if (new_fl)
11011da177e4SLinus Torvalds 		locks_free_lock(new_fl);
11021da177e4SLinus Torvalds 	if (new_fl2)
11031da177e4SLinus Torvalds 		locks_free_lock(new_fl2);
11041da177e4SLinus Torvalds 	return error;
11051da177e4SLinus Torvalds }
11061da177e4SLinus Torvalds 
11071da177e4SLinus Torvalds /**
11081da177e4SLinus Torvalds  * posix_lock_file - Apply a POSIX-style lock to a file
11091da177e4SLinus Torvalds  * @filp: The file to apply the lock to
11101da177e4SLinus Torvalds  * @fl: The lock to be applied
1111150b3934SMarc Eshel  * @conflock: Place to return a copy of the conflicting lock, if found.
11121da177e4SLinus Torvalds  *
11131da177e4SLinus Torvalds  * Add a POSIX style lock to a file.
11141da177e4SLinus Torvalds  * We merge adjacent & overlapping locks whenever possible.
11151da177e4SLinus Torvalds  * POSIX locks are sorted by owner task, then by starting address
1116f475ae95STrond Myklebust  *
1117f475ae95STrond Myklebust  * Note that if called with an FL_EXISTS argument, the caller may determine
1118f475ae95STrond Myklebust  * whether or not a lock was successfully freed by testing the return
1119f475ae95STrond Myklebust  * value for -ENOENT.
11201da177e4SLinus Torvalds  */
1121150b3934SMarc Eshel int posix_lock_file(struct file *filp, struct file_lock *fl,
11225842add2SAndy Adamson 			struct file_lock *conflock)
11235842add2SAndy Adamson {
1124496ad9aaSAl Viro 	return __posix_lock_file(file_inode(filp), fl, conflock);
11255842add2SAndy Adamson }
1126150b3934SMarc Eshel EXPORT_SYMBOL(posix_lock_file);
11271da177e4SLinus Torvalds 
11281da177e4SLinus Torvalds /**
11291da177e4SLinus Torvalds  * posix_lock_file_wait - Apply a POSIX-style lock to a file
11301da177e4SLinus Torvalds  * @filp: The file to apply the lock to
11311da177e4SLinus Torvalds  * @fl: The lock to be applied
11321da177e4SLinus Torvalds  *
11331da177e4SLinus Torvalds  * Add a POSIX style lock to a file.
11341da177e4SLinus Torvalds  * We merge adjacent & overlapping locks whenever possible.
11351da177e4SLinus Torvalds  * POSIX locks are sorted by owner task, then by starting address
11361da177e4SLinus Torvalds  */
11371da177e4SLinus Torvalds int posix_lock_file_wait(struct file *filp, struct file_lock *fl)
11381da177e4SLinus Torvalds {
11391da177e4SLinus Torvalds 	int error;
11401da177e4SLinus Torvalds 	might_sleep ();
11411da177e4SLinus Torvalds 	for (;;) {
1142150b3934SMarc Eshel 		error = posix_lock_file(filp, fl, NULL);
1143bde74e4bSMiklos Szeredi 		if (error != FILE_LOCK_DEFERRED)
11441da177e4SLinus Torvalds 			break;
11451da177e4SLinus Torvalds 		error = wait_event_interruptible(fl->fl_wait, !fl->fl_next);
11461da177e4SLinus Torvalds 		if (!error)
11471da177e4SLinus Torvalds 			continue;
11481da177e4SLinus Torvalds 
11491da177e4SLinus Torvalds 		locks_delete_block(fl);
11501da177e4SLinus Torvalds 		break;
11511da177e4SLinus Torvalds 	}
11521da177e4SLinus Torvalds 	return error;
11531da177e4SLinus Torvalds }
11541da177e4SLinus Torvalds EXPORT_SYMBOL(posix_lock_file_wait);
11551da177e4SLinus Torvalds 
11561da177e4SLinus Torvalds /**
11571da177e4SLinus Torvalds  * locks_mandatory_locked - Check for an active lock
1158d7a06983SJeff Layton  * @file: the file to check
11591da177e4SLinus Torvalds  *
11601da177e4SLinus Torvalds  * Searches the inode's list of locks to find any POSIX locks which conflict.
11611da177e4SLinus Torvalds  * This function is called from locks_verify_locked() only.
11621da177e4SLinus Torvalds  */
1163d7a06983SJeff Layton int locks_mandatory_locked(struct file *file)
11641da177e4SLinus Torvalds {
1165d7a06983SJeff Layton 	struct inode *inode = file_inode(file);
11661da177e4SLinus Torvalds 	fl_owner_t owner = current->files;
11671da177e4SLinus Torvalds 	struct file_lock *fl;
11681da177e4SLinus Torvalds 
11691da177e4SLinus Torvalds 	/*
11701da177e4SLinus Torvalds 	 * Search the lock list for this inode for any POSIX locks.
11711da177e4SLinus Torvalds 	 */
11721c8c601aSJeff Layton 	spin_lock(&inode->i_lock);
11731da177e4SLinus Torvalds 	for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
11741da177e4SLinus Torvalds 		if (!IS_POSIX(fl))
11751da177e4SLinus Torvalds 			continue;
1176d7a06983SJeff Layton 		if (fl->fl_owner != owner && fl->fl_owner != (fl_owner_t)file)
11771da177e4SLinus Torvalds 			break;
11781da177e4SLinus Torvalds 	}
11791c8c601aSJeff Layton 	spin_unlock(&inode->i_lock);
11801da177e4SLinus Torvalds 	return fl ? -EAGAIN : 0;
11811da177e4SLinus Torvalds }
11821da177e4SLinus Torvalds 
11831da177e4SLinus Torvalds /**
11841da177e4SLinus Torvalds  * locks_mandatory_area - Check for a conflicting lock
11851da177e4SLinus Torvalds  * @read_write: %FLOCK_VERIFY_WRITE for exclusive access, %FLOCK_VERIFY_READ
11861da177e4SLinus Torvalds  *		for shared
11871da177e4SLinus Torvalds  * @inode:      the file to check
11881da177e4SLinus Torvalds  * @filp:       how the file was opened (if it was)
11891da177e4SLinus Torvalds  * @offset:     start of area to check
11901da177e4SLinus Torvalds  * @count:      length of area to check
11911da177e4SLinus Torvalds  *
11921da177e4SLinus Torvalds  * Searches the inode's list of locks to find any POSIX locks which conflict.
11931da177e4SLinus Torvalds  * This function is called from rw_verify_area() and
11941da177e4SLinus Torvalds  * locks_verify_truncate().
11951da177e4SLinus Torvalds  */
11961da177e4SLinus Torvalds int locks_mandatory_area(int read_write, struct inode *inode,
11971da177e4SLinus Torvalds 			 struct file *filp, loff_t offset,
11981da177e4SLinus Torvalds 			 size_t count)
11991da177e4SLinus Torvalds {
12001da177e4SLinus Torvalds 	struct file_lock fl;
12011da177e4SLinus Torvalds 	int error;
120229723adeSJeff Layton 	bool sleep = false;
12031da177e4SLinus Torvalds 
12041da177e4SLinus Torvalds 	locks_init_lock(&fl);
12051da177e4SLinus Torvalds 	fl.fl_pid = current->tgid;
12061da177e4SLinus Torvalds 	fl.fl_file = filp;
12071da177e4SLinus Torvalds 	fl.fl_flags = FL_POSIX | FL_ACCESS;
12081da177e4SLinus Torvalds 	if (filp && !(filp->f_flags & O_NONBLOCK))
120929723adeSJeff Layton 		sleep = true;
12101da177e4SLinus Torvalds 	fl.fl_type = (read_write == FLOCK_VERIFY_WRITE) ? F_WRLCK : F_RDLCK;
12111da177e4SLinus Torvalds 	fl.fl_start = offset;
12121da177e4SLinus Torvalds 	fl.fl_end = offset + count - 1;
12131da177e4SLinus Torvalds 
12141da177e4SLinus Torvalds 	for (;;) {
121529723adeSJeff Layton 		if (filp) {
121629723adeSJeff Layton 			fl.fl_owner = (fl_owner_t)filp;
121729723adeSJeff Layton 			fl.fl_flags &= ~FL_SLEEP;
121829723adeSJeff Layton 			error = __posix_lock_file(inode, &fl, NULL);
121929723adeSJeff Layton 			if (!error)
122029723adeSJeff Layton 				break;
122129723adeSJeff Layton 		}
122229723adeSJeff Layton 
122329723adeSJeff Layton 		if (sleep)
122429723adeSJeff Layton 			fl.fl_flags |= FL_SLEEP;
122529723adeSJeff Layton 		fl.fl_owner = current->files;
1226150b3934SMarc Eshel 		error = __posix_lock_file(inode, &fl, NULL);
1227bde74e4bSMiklos Szeredi 		if (error != FILE_LOCK_DEFERRED)
12281da177e4SLinus Torvalds 			break;
12291da177e4SLinus Torvalds 		error = wait_event_interruptible(fl.fl_wait, !fl.fl_next);
12301da177e4SLinus Torvalds 		if (!error) {
12311da177e4SLinus Torvalds 			/*
12321da177e4SLinus Torvalds 			 * If we've been sleeping someone might have
12331da177e4SLinus Torvalds 			 * changed the permissions behind our back.
12341da177e4SLinus Torvalds 			 */
1235a16877caSPavel Emelyanov 			if (__mandatory_lock(inode))
12361da177e4SLinus Torvalds 				continue;
12371da177e4SLinus Torvalds 		}
12381da177e4SLinus Torvalds 
12391da177e4SLinus Torvalds 		locks_delete_block(&fl);
12401da177e4SLinus Torvalds 		break;
12411da177e4SLinus Torvalds 	}
12421da177e4SLinus Torvalds 
12431da177e4SLinus Torvalds 	return error;
12441da177e4SLinus Torvalds }
12451da177e4SLinus Torvalds 
12461da177e4SLinus Torvalds EXPORT_SYMBOL(locks_mandatory_area);
12471da177e4SLinus Torvalds 
1248778fc546SJ. Bruce Fields static void lease_clear_pending(struct file_lock *fl, int arg)
1249778fc546SJ. Bruce Fields {
1250778fc546SJ. Bruce Fields 	switch (arg) {
1251778fc546SJ. Bruce Fields 	case F_UNLCK:
1252778fc546SJ. Bruce Fields 		fl->fl_flags &= ~FL_UNLOCK_PENDING;
1253778fc546SJ. Bruce Fields 		/* fall through: */
1254778fc546SJ. Bruce Fields 	case F_RDLCK:
1255778fc546SJ. Bruce Fields 		fl->fl_flags &= ~FL_DOWNGRADE_PENDING;
1256778fc546SJ. Bruce Fields 	}
1257778fc546SJ. Bruce Fields }
1258778fc546SJ. Bruce Fields 
12591da177e4SLinus Torvalds /* We already had a lease on this file; just change its type */
12601da177e4SLinus Torvalds int lease_modify(struct file_lock **before, int arg)
12611da177e4SLinus Torvalds {
12621da177e4SLinus Torvalds 	struct file_lock *fl = *before;
12631da177e4SLinus Torvalds 	int error = assign_type(fl, arg);
12641da177e4SLinus Torvalds 
12651da177e4SLinus Torvalds 	if (error)
12661da177e4SLinus Torvalds 		return error;
1267778fc546SJ. Bruce Fields 	lease_clear_pending(fl, arg);
12681da177e4SLinus Torvalds 	locks_wake_up_blocks(fl);
12693b6e2723SFilipe Brandenburger 	if (arg == F_UNLCK) {
12703b6e2723SFilipe Brandenburger 		struct file *filp = fl->fl_file;
12713b6e2723SFilipe Brandenburger 
12723b6e2723SFilipe Brandenburger 		f_delown(filp);
12733b6e2723SFilipe Brandenburger 		filp->f_owner.signum = 0;
127496d6d59cSJ. Bruce Fields 		fasync_helper(0, fl->fl_file, 0, &fl->fl_fasync);
127596d6d59cSJ. Bruce Fields 		if (fl->fl_fasync != NULL) {
127696d6d59cSJ. Bruce Fields 			printk(KERN_ERR "locks_delete_lock: fasync == %p\n", fl->fl_fasync);
127796d6d59cSJ. Bruce Fields 			fl->fl_fasync = NULL;
127896d6d59cSJ. Bruce Fields 		}
12791da177e4SLinus Torvalds 		locks_delete_lock(before);
12803b6e2723SFilipe Brandenburger 	}
12811da177e4SLinus Torvalds 	return 0;
12821da177e4SLinus Torvalds }
12831da177e4SLinus Torvalds 
12841da177e4SLinus Torvalds EXPORT_SYMBOL(lease_modify);
12851da177e4SLinus Torvalds 
1286778fc546SJ. Bruce Fields static bool past_time(unsigned long then)
1287778fc546SJ. Bruce Fields {
1288778fc546SJ. Bruce Fields 	if (!then)
1289778fc546SJ. Bruce Fields 		/* 0 is a special value meaning "this never expires": */
1290778fc546SJ. Bruce Fields 		return false;
1291778fc546SJ. Bruce Fields 	return time_after(jiffies, then);
1292778fc546SJ. Bruce Fields }
1293778fc546SJ. Bruce Fields 
12941da177e4SLinus Torvalds static void time_out_leases(struct inode *inode)
12951da177e4SLinus Torvalds {
12961da177e4SLinus Torvalds 	struct file_lock **before;
12971da177e4SLinus Torvalds 	struct file_lock *fl;
12981da177e4SLinus Torvalds 
12991da177e4SLinus Torvalds 	before = &inode->i_flock;
1300ab83fa4bSJ. Bruce Fields 	while ((fl = *before) && IS_LEASE(fl) && lease_breaking(fl)) {
1301778fc546SJ. Bruce Fields 		if (past_time(fl->fl_downgrade_time))
1302778fc546SJ. Bruce Fields 			lease_modify(before, F_RDLCK);
1303778fc546SJ. Bruce Fields 		if (past_time(fl->fl_break_time))
1304778fc546SJ. Bruce Fields 			lease_modify(before, F_UNLCK);
13051da177e4SLinus Torvalds 		if (fl == *before)	/* lease_modify may have freed fl */
13061da177e4SLinus Torvalds 			before = &fl->fl_next;
13071da177e4SLinus Torvalds 	}
13081da177e4SLinus Torvalds }
13091da177e4SLinus Torvalds 
1310df4e8d2cSJ. Bruce Fields static bool leases_conflict(struct file_lock *lease, struct file_lock *breaker)
1311df4e8d2cSJ. Bruce Fields {
1312df4e8d2cSJ. Bruce Fields 	if ((breaker->fl_flags & FL_DELEG) && (lease->fl_flags & FL_LEASE))
1313df4e8d2cSJ. Bruce Fields 		return false;
1314df4e8d2cSJ. Bruce Fields 	return locks_conflict(breaker, lease);
1315df4e8d2cSJ. Bruce Fields }
1316df4e8d2cSJ. Bruce Fields 
13171da177e4SLinus Torvalds /**
13181da177e4SLinus Torvalds  *	__break_lease	-	revoke all outstanding leases on file
13191da177e4SLinus Torvalds  *	@inode: the inode of the file to return
1320df4e8d2cSJ. Bruce Fields  *	@mode: O_RDONLY: break only write leases; O_WRONLY or O_RDWR:
1321df4e8d2cSJ. Bruce Fields  *	    break all leases
1322df4e8d2cSJ. Bruce Fields  *	@type: FL_LEASE: break leases and delegations; FL_DELEG: break
1323df4e8d2cSJ. Bruce Fields  *	    only delegations
13241da177e4SLinus Torvalds  *
132587250dd2Sdavid m. richter  *	break_lease (inlined for speed) has checked there already is at least
132687250dd2Sdavid m. richter  *	some kind of lock (maybe a lease) on this file.  Leases are broken on
132787250dd2Sdavid m. richter  *	a call to open() or truncate().  This function can sleep unless you
13281da177e4SLinus Torvalds  *	specified %O_NONBLOCK to your open().
13291da177e4SLinus Torvalds  */
1330df4e8d2cSJ. Bruce Fields int __break_lease(struct inode *inode, unsigned int mode, unsigned int type)
13311da177e4SLinus Torvalds {
1332778fc546SJ. Bruce Fields 	int error = 0;
13331da177e4SLinus Torvalds 	struct file_lock *new_fl, *flock;
13341da177e4SLinus Torvalds 	struct file_lock *fl;
13351da177e4SLinus Torvalds 	unsigned long break_time;
13361da177e4SLinus Torvalds 	int i_have_this_lease = 0;
1337df4e8d2cSJ. Bruce Fields 	bool lease_conflict = false;
13388737c930SAl Viro 	int want_write = (mode & O_ACCMODE) != O_RDONLY;
13391da177e4SLinus Torvalds 
13408737c930SAl Viro 	new_fl = lease_alloc(NULL, want_write ? F_WRLCK : F_RDLCK);
13416d4b9e38SLinus Torvalds 	if (IS_ERR(new_fl))
13426d4b9e38SLinus Torvalds 		return PTR_ERR(new_fl);
1343df4e8d2cSJ. Bruce Fields 	new_fl->fl_flags = type;
13441da177e4SLinus Torvalds 
13451c8c601aSJeff Layton 	spin_lock(&inode->i_lock);
13461da177e4SLinus Torvalds 
13471da177e4SLinus Torvalds 	time_out_leases(inode);
13481da177e4SLinus Torvalds 
13491da177e4SLinus Torvalds 	flock = inode->i_flock;
13501da177e4SLinus Torvalds 	if ((flock == NULL) || !IS_LEASE(flock))
13511da177e4SLinus Torvalds 		goto out;
13521da177e4SLinus Torvalds 
1353df4e8d2cSJ. Bruce Fields 	for (fl = flock; fl && IS_LEASE(fl); fl = fl->fl_next) {
1354df4e8d2cSJ. Bruce Fields 		if (leases_conflict(fl, new_fl)) {
1355df4e8d2cSJ. Bruce Fields 			lease_conflict = true;
13561da177e4SLinus Torvalds 			if (fl->fl_owner == current->files)
13571da177e4SLinus Torvalds 				i_have_this_lease = 1;
1358df4e8d2cSJ. Bruce Fields 		}
1359df4e8d2cSJ. Bruce Fields 	}
1360df4e8d2cSJ. Bruce Fields 	if (!lease_conflict)
1361df4e8d2cSJ. Bruce Fields 		goto out;
13621da177e4SLinus Torvalds 
13631da177e4SLinus Torvalds 	break_time = 0;
13641da177e4SLinus Torvalds 	if (lease_break_time > 0) {
13651da177e4SLinus Torvalds 		break_time = jiffies + lease_break_time * HZ;
13661da177e4SLinus Torvalds 		if (break_time == 0)
13671da177e4SLinus Torvalds 			break_time++;	/* so that 0 means no break time */
13681da177e4SLinus Torvalds 	}
13691da177e4SLinus Torvalds 
13701da177e4SLinus Torvalds 	for (fl = flock; fl && IS_LEASE(fl); fl = fl->fl_next) {
1371df4e8d2cSJ. Bruce Fields 		if (!leases_conflict(fl, new_fl))
1372df4e8d2cSJ. Bruce Fields 			continue;
1373778fc546SJ. Bruce Fields 		if (want_write) {
1374778fc546SJ. Bruce Fields 			if (fl->fl_flags & FL_UNLOCK_PENDING)
1375778fc546SJ. Bruce Fields 				continue;
1376778fc546SJ. Bruce Fields 			fl->fl_flags |= FL_UNLOCK_PENDING;
13771da177e4SLinus Torvalds 			fl->fl_break_time = break_time;
1378778fc546SJ. Bruce Fields 		} else {
1379778fc546SJ. Bruce Fields 			if (lease_breaking(flock))
1380778fc546SJ. Bruce Fields 				continue;
1381778fc546SJ. Bruce Fields 			fl->fl_flags |= FL_DOWNGRADE_PENDING;
1382778fc546SJ. Bruce Fields 			fl->fl_downgrade_time = break_time;
13831da177e4SLinus Torvalds 		}
1384778fc546SJ. Bruce Fields 		fl->fl_lmops->lm_break(fl);
13851da177e4SLinus Torvalds 	}
13861da177e4SLinus Torvalds 
13871da177e4SLinus Torvalds 	if (i_have_this_lease || (mode & O_NONBLOCK)) {
13881da177e4SLinus Torvalds 		error = -EWOULDBLOCK;
13891da177e4SLinus Torvalds 		goto out;
13901da177e4SLinus Torvalds 	}
13911da177e4SLinus Torvalds 
13921da177e4SLinus Torvalds restart:
13931da177e4SLinus Torvalds 	break_time = flock->fl_break_time;
1394f1c6bb2cSJeff Layton 	if (break_time != 0)
13951da177e4SLinus Torvalds 		break_time -= jiffies;
13961da177e4SLinus Torvalds 	if (break_time == 0)
13971da177e4SLinus Torvalds 		break_time++;
13984321e01eSMatthew Wilcox 	locks_insert_block(flock, new_fl);
13991c8c601aSJeff Layton 	spin_unlock(&inode->i_lock);
14004321e01eSMatthew Wilcox 	error = wait_event_interruptible_timeout(new_fl->fl_wait,
14014321e01eSMatthew Wilcox 						!new_fl->fl_next, break_time);
14021c8c601aSJeff Layton 	spin_lock(&inode->i_lock);
14031c8c601aSJeff Layton 	locks_delete_block(new_fl);
14041da177e4SLinus Torvalds 	if (error >= 0) {
14051da177e4SLinus Torvalds 		if (error == 0)
14061da177e4SLinus Torvalds 			time_out_leases(inode);
1407778fc546SJ. Bruce Fields 		/*
1408778fc546SJ. Bruce Fields 		 * Wait for the next conflicting lease that has not been
1409778fc546SJ. Bruce Fields 		 * broken yet
1410778fc546SJ. Bruce Fields 		 */
14111da177e4SLinus Torvalds 		for (flock = inode->i_flock; flock && IS_LEASE(flock);
14121da177e4SLinus Torvalds 				flock = flock->fl_next) {
1413df4e8d2cSJ. Bruce Fields 			if (leases_conflict(new_fl, flock))
14141da177e4SLinus Torvalds 				goto restart;
14151da177e4SLinus Torvalds 		}
14161da177e4SLinus Torvalds 		error = 0;
14171da177e4SLinus Torvalds 	}
14181da177e4SLinus Torvalds 
14191da177e4SLinus Torvalds out:
14201c8c601aSJeff Layton 	spin_unlock(&inode->i_lock);
14211da177e4SLinus Torvalds 	locks_free_lock(new_fl);
14221da177e4SLinus Torvalds 	return error;
14231da177e4SLinus Torvalds }
14241da177e4SLinus Torvalds 
14251da177e4SLinus Torvalds EXPORT_SYMBOL(__break_lease);
14261da177e4SLinus Torvalds 
14271da177e4SLinus Torvalds /**
1428a6b91919SRandy Dunlap  *	lease_get_mtime - get the last modified time of an inode
14291da177e4SLinus Torvalds  *	@inode: the inode
14301da177e4SLinus Torvalds  *      @time:  pointer to a timespec which will contain the last modified time
14311da177e4SLinus Torvalds  *
14321da177e4SLinus Torvalds  * This is to force NFS clients to flush their caches for files with
14331da177e4SLinus Torvalds  * exclusive leases.  The justification is that if someone has an
1434a6b91919SRandy Dunlap  * exclusive lease, then they could be modifying it.
14351da177e4SLinus Torvalds  */
14361da177e4SLinus Torvalds void lease_get_mtime(struct inode *inode, struct timespec *time)
14371da177e4SLinus Torvalds {
14381da177e4SLinus Torvalds 	struct file_lock *flock = inode->i_flock;
14390ee5c6d6SJeff Layton 	if (flock && IS_LEASE(flock) && (flock->fl_type == F_WRLCK))
14401da177e4SLinus Torvalds 		*time = current_fs_time(inode->i_sb);
14411da177e4SLinus Torvalds 	else
14421da177e4SLinus Torvalds 		*time = inode->i_mtime;
14431da177e4SLinus Torvalds }
14441da177e4SLinus Torvalds 
14451da177e4SLinus Torvalds EXPORT_SYMBOL(lease_get_mtime);
14461da177e4SLinus Torvalds 
14471da177e4SLinus Torvalds /**
14481da177e4SLinus Torvalds  *	fcntl_getlease - Enquire what lease is currently active
14491da177e4SLinus Torvalds  *	@filp: the file
14501da177e4SLinus Torvalds  *
14511da177e4SLinus Torvalds  *	The value returned by this function will be one of
14521da177e4SLinus Torvalds  *	(if no lease break is pending):
14531da177e4SLinus Torvalds  *
14541da177e4SLinus Torvalds  *	%F_RDLCK to indicate a shared lease is held.
14551da177e4SLinus Torvalds  *
14561da177e4SLinus Torvalds  *	%F_WRLCK to indicate an exclusive lease is held.
14571da177e4SLinus Torvalds  *
14581da177e4SLinus Torvalds  *	%F_UNLCK to indicate no lease is held.
14591da177e4SLinus Torvalds  *
14601da177e4SLinus Torvalds  *	(if a lease break is pending):
14611da177e4SLinus Torvalds  *
14621da177e4SLinus Torvalds  *	%F_RDLCK to indicate an exclusive lease needs to be
14631da177e4SLinus Torvalds  *		changed to a shared lease (or removed).
14641da177e4SLinus Torvalds  *
14651da177e4SLinus Torvalds  *	%F_UNLCK to indicate the lease needs to be removed.
14661da177e4SLinus Torvalds  *
14671da177e4SLinus Torvalds  *	XXX: sfr & willy disagree over whether F_INPROGRESS
14681da177e4SLinus Torvalds  *	should be returned to userspace.
14691da177e4SLinus Torvalds  */
14701da177e4SLinus Torvalds int fcntl_getlease(struct file *filp)
14711da177e4SLinus Torvalds {
14721da177e4SLinus Torvalds 	struct file_lock *fl;
14731c8c601aSJeff Layton 	struct inode *inode = file_inode(filp);
14741da177e4SLinus Torvalds 	int type = F_UNLCK;
14751da177e4SLinus Torvalds 
14761c8c601aSJeff Layton 	spin_lock(&inode->i_lock);
1477496ad9aaSAl Viro 	time_out_leases(file_inode(filp));
1478496ad9aaSAl Viro 	for (fl = file_inode(filp)->i_flock; fl && IS_LEASE(fl);
14791da177e4SLinus Torvalds 			fl = fl->fl_next) {
14801da177e4SLinus Torvalds 		if (fl->fl_file == filp) {
1481778fc546SJ. Bruce Fields 			type = target_leasetype(fl);
14821da177e4SLinus Torvalds 			break;
14831da177e4SLinus Torvalds 		}
14841da177e4SLinus Torvalds 	}
14851c8c601aSJeff Layton 	spin_unlock(&inode->i_lock);
14861da177e4SLinus Torvalds 	return type;
14871da177e4SLinus Torvalds }
14881da177e4SLinus Torvalds 
148924cbe784SJeff Layton /**
149024cbe784SJeff Layton  * check_conflicting_open - see if the given dentry points to a file that has
149124cbe784SJeff Layton  * 			    an existing open that would conflict with the
149224cbe784SJeff Layton  * 			    desired lease.
149324cbe784SJeff Layton  * @dentry:	dentry to check
149424cbe784SJeff Layton  * @arg:	type of lease that we're trying to acquire
149524cbe784SJeff Layton  *
149624cbe784SJeff Layton  * Check to see if there's an existing open fd on this file that would
149724cbe784SJeff Layton  * conflict with the lease we're trying to set.
149824cbe784SJeff Layton  */
149924cbe784SJeff Layton static int
150024cbe784SJeff Layton check_conflicting_open(const struct dentry *dentry, const long arg)
150124cbe784SJeff Layton {
150224cbe784SJeff Layton 	int ret = 0;
150324cbe784SJeff Layton 	struct inode *inode = dentry->d_inode;
150424cbe784SJeff Layton 
150524cbe784SJeff Layton 	if ((arg == F_RDLCK) && (atomic_read(&inode->i_writecount) > 0))
150624cbe784SJeff Layton 		return -EAGAIN;
150724cbe784SJeff Layton 
150824cbe784SJeff Layton 	if ((arg == F_WRLCK) && ((d_count(dentry) > 1) ||
150924cbe784SJeff Layton 	    (atomic_read(&inode->i_count) > 1)))
151024cbe784SJeff Layton 		ret = -EAGAIN;
151124cbe784SJeff Layton 
151224cbe784SJeff Layton 	return ret;
151324cbe784SJeff Layton }
151424cbe784SJeff Layton 
1515d4f22d19SJeff Layton static int generic_add_lease(struct file *filp, long arg, struct file_lock **flp)
15161da177e4SLinus Torvalds {
15177eaae282SKAMBAROV, ZAUR 	struct file_lock *fl, **before, **my_before = NULL, *lease;
15180f7fc9e4SJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
15191da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
1520df4e8d2cSJ. Bruce Fields 	bool is_deleg = (*flp)->fl_flags & FL_DELEG;
1521c1f24ef4SJ. Bruce Fields 	int error;
15221da177e4SLinus Torvalds 
1523096657b6SJ. Bruce Fields 	lease = *flp;
1524df4e8d2cSJ. Bruce Fields 	/*
1525df4e8d2cSJ. Bruce Fields 	 * In the delegation case we need mutual exclusion with
1526df4e8d2cSJ. Bruce Fields 	 * a number of operations that take the i_mutex.  We trylock
1527df4e8d2cSJ. Bruce Fields 	 * because delegations are an optional optimization, and if
1528df4e8d2cSJ. Bruce Fields 	 * there's some chance of a conflict--we'd rather not
1529df4e8d2cSJ. Bruce Fields 	 * bother, maybe that's a sign this just isn't a good file to
1530df4e8d2cSJ. Bruce Fields 	 * hand out a delegation on.
1531df4e8d2cSJ. Bruce Fields 	 */
1532df4e8d2cSJ. Bruce Fields 	if (is_deleg && !mutex_trylock(&inode->i_mutex))
1533df4e8d2cSJ. Bruce Fields 		return -EAGAIN;
1534df4e8d2cSJ. Bruce Fields 
1535df4e8d2cSJ. Bruce Fields 	if (is_deleg && arg == F_WRLCK) {
1536df4e8d2cSJ. Bruce Fields 		/* Write delegations are not currently supported: */
15374fdb793fSDan Carpenter 		mutex_unlock(&inode->i_mutex);
1538df4e8d2cSJ. Bruce Fields 		WARN_ON_ONCE(1);
1539df4e8d2cSJ. Bruce Fields 		return -EINVAL;
1540df4e8d2cSJ. Bruce Fields 	}
1541096657b6SJ. Bruce Fields 
154224cbe784SJeff Layton 	error = check_conflicting_open(dentry, arg);
154324cbe784SJeff Layton 	if (error)
15441da177e4SLinus Torvalds 		goto out;
154585c59580SPavel Emelyanov 
15461da177e4SLinus Torvalds 	/*
15471da177e4SLinus Torvalds 	 * At this point, we know that if there is an exclusive
15481da177e4SLinus Torvalds 	 * lease on this file, then we hold it on this filp
15491da177e4SLinus Torvalds 	 * (otherwise our open of this file would have blocked).
15501da177e4SLinus Torvalds 	 * And if we are trying to acquire an exclusive lease,
15511da177e4SLinus Torvalds 	 * then the file is not open by anyone (including us)
15521da177e4SLinus Torvalds 	 * except for this filp.
15531da177e4SLinus Torvalds 	 */
1554c1f24ef4SJ. Bruce Fields 	error = -EAGAIN;
15551da177e4SLinus Torvalds 	for (before = &inode->i_flock;
15561da177e4SLinus Torvalds 			((fl = *before) != NULL) && IS_LEASE(fl);
15571da177e4SLinus Torvalds 			before = &fl->fl_next) {
1558c1f24ef4SJ. Bruce Fields 		if (fl->fl_file == filp) {
15591da177e4SLinus Torvalds 			my_before = before;
1560c1f24ef4SJ. Bruce Fields 			continue;
15611da177e4SLinus Torvalds 		}
1562c1f24ef4SJ. Bruce Fields 		/*
1563c1f24ef4SJ. Bruce Fields 		 * No exclusive leases if someone else has a lease on
1564c1f24ef4SJ. Bruce Fields 		 * this file:
1565c1f24ef4SJ. Bruce Fields 		 */
1566c1f24ef4SJ. Bruce Fields 		if (arg == F_WRLCK)
15671da177e4SLinus Torvalds 			goto out;
1568c1f24ef4SJ. Bruce Fields 		/*
1569c1f24ef4SJ. Bruce Fields 		 * Modifying our existing lease is OK, but no getting a
1570c1f24ef4SJ. Bruce Fields 		 * new lease if someone else is opening for write:
1571c1f24ef4SJ. Bruce Fields 		 */
1572c1f24ef4SJ. Bruce Fields 		if (fl->fl_flags & FL_UNLOCK_PENDING)
1573c1f24ef4SJ. Bruce Fields 			goto out;
1574c1f24ef4SJ. Bruce Fields 	}
15751da177e4SLinus Torvalds 
15761da177e4SLinus Torvalds 	if (my_before != NULL) {
15778fb47a4fSJ. Bruce Fields 		error = lease->fl_lmops->lm_change(my_before, arg);
157851ee4b84SChristoph Hellwig 		if (!error)
157951ee4b84SChristoph Hellwig 			*flp = *my_before;
15801da177e4SLinus Torvalds 		goto out;
15811da177e4SLinus Torvalds 	}
15821da177e4SLinus Torvalds 
15831da177e4SLinus Torvalds 	error = -EINVAL;
15841da177e4SLinus Torvalds 	if (!leases_enable)
15851da177e4SLinus Torvalds 		goto out;
15861da177e4SLinus Torvalds 
1587c5b1f0d9SArnd Bergmann 	locks_insert_lock(before, lease);
158824cbe784SJeff Layton 	/*
158924cbe784SJeff Layton 	 * The check in break_lease() is lockless. It's possible for another
159024cbe784SJeff Layton 	 * open to race in after we did the earlier check for a conflicting
159124cbe784SJeff Layton 	 * open but before the lease was inserted. Check again for a
159224cbe784SJeff Layton 	 * conflicting open and cancel the lease if there is one.
159324cbe784SJeff Layton 	 *
159424cbe784SJeff Layton 	 * We also add a barrier here to ensure that the insertion of the lock
159524cbe784SJeff Layton 	 * precedes these checks.
159624cbe784SJeff Layton 	 */
159724cbe784SJeff Layton 	smp_mb();
159824cbe784SJeff Layton 	error = check_conflicting_open(dentry, arg);
159924cbe784SJeff Layton 	if (error)
160024cbe784SJeff Layton 		locks_unlink_lock(flp);
16011da177e4SLinus Torvalds out:
1602df4e8d2cSJ. Bruce Fields 	if (is_deleg)
1603df4e8d2cSJ. Bruce Fields 		mutex_unlock(&inode->i_mutex);
16041da177e4SLinus Torvalds 	return error;
16051da177e4SLinus Torvalds }
16068335ebd9SJ. Bruce Fields 
1607d4f22d19SJeff Layton static int generic_delete_lease(struct file *filp, struct file_lock **flp)
16088335ebd9SJ. Bruce Fields {
16098335ebd9SJ. Bruce Fields 	struct file_lock *fl, **before;
16108335ebd9SJ. Bruce Fields 	struct dentry *dentry = filp->f_path.dentry;
16118335ebd9SJ. Bruce Fields 	struct inode *inode = dentry->d_inode;
16128335ebd9SJ. Bruce Fields 
16138335ebd9SJ. Bruce Fields 	for (before = &inode->i_flock;
16148335ebd9SJ. Bruce Fields 			((fl = *before) != NULL) && IS_LEASE(fl);
16158335ebd9SJ. Bruce Fields 			before = &fl->fl_next) {
16168335ebd9SJ. Bruce Fields 		if (fl->fl_file != filp)
16178335ebd9SJ. Bruce Fields 			continue;
16188335ebd9SJ. Bruce Fields 		return (*flp)->fl_lmops->lm_change(before, F_UNLCK);
16198335ebd9SJ. Bruce Fields 	}
16208335ebd9SJ. Bruce Fields 	return -EAGAIN;
16218335ebd9SJ. Bruce Fields }
16228335ebd9SJ. Bruce Fields 
16231da177e4SLinus Torvalds /**
16241da177e4SLinus Torvalds  *	generic_setlease	-	sets a lease on an open file
16251da177e4SLinus Torvalds  *	@filp: file pointer
16261da177e4SLinus Torvalds  *	@arg: type of lease to obtain
16271da177e4SLinus Torvalds  *	@flp: input - file_lock to use, output - file_lock inserted
16281da177e4SLinus Torvalds  *
16291da177e4SLinus Torvalds  *	The (input) flp->fl_lmops->lm_break function is required
16301da177e4SLinus Torvalds  *	by break_lease().
16311da177e4SLinus Torvalds  *
16321c8c601aSJeff Layton  *	Called with inode->i_lock held.
16331da177e4SLinus Torvalds  */
16341da177e4SLinus Torvalds int generic_setlease(struct file *filp, long arg, struct file_lock **flp)
16351da177e4SLinus Torvalds {
16361da177e4SLinus Torvalds 	struct dentry *dentry = filp->f_path.dentry;
16371da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
16388335ebd9SJ. Bruce Fields 	int error;
16391da177e4SLinus Torvalds 
16408e96e3b7SEric W. Biederman 	if ((!uid_eq(current_fsuid(), inode->i_uid)) && !capable(CAP_LEASE))
16418335ebd9SJ. Bruce Fields 		return -EACCES;
16421da177e4SLinus Torvalds 	if (!S_ISREG(inode->i_mode))
16438335ebd9SJ. Bruce Fields 		return -EINVAL;
16441da177e4SLinus Torvalds 	error = security_file_lock(filp, arg);
16451da177e4SLinus Torvalds 	if (error)
16468335ebd9SJ. Bruce Fields 		return error;
16471da177e4SLinus Torvalds 
16481da177e4SLinus Torvalds 	time_out_leases(inode);
16491da177e4SLinus Torvalds 
16501da177e4SLinus Torvalds 	BUG_ON(!(*flp)->fl_lmops->lm_break);
16511da177e4SLinus Torvalds 
16528335ebd9SJ. Bruce Fields 	switch (arg) {
16538335ebd9SJ. Bruce Fields 	case F_UNLCK:
16548335ebd9SJ. Bruce Fields 		return generic_delete_lease(filp, flp);
16558335ebd9SJ. Bruce Fields 	case F_RDLCK:
16568335ebd9SJ. Bruce Fields 	case F_WRLCK:
16578335ebd9SJ. Bruce Fields 		return generic_add_lease(filp, arg, flp);
16588335ebd9SJ. Bruce Fields 	default:
16598d657eb3SDave Jones 		return -EINVAL;
16601da177e4SLinus Torvalds 	}
16611da177e4SLinus Torvalds }
16620af1a450SChristoph Hellwig EXPORT_SYMBOL(generic_setlease);
16631da177e4SLinus Torvalds 
1664b89f4321SArnd Bergmann static int __vfs_setlease(struct file *filp, long arg, struct file_lock **lease)
1665b89f4321SArnd Bergmann {
166672c2d531SAl Viro 	if (filp->f_op->setlease)
1667b89f4321SArnd Bergmann 		return filp->f_op->setlease(filp, arg, lease);
1668b89f4321SArnd Bergmann 	else
1669b89f4321SArnd Bergmann 		return generic_setlease(filp, arg, lease);
1670b89f4321SArnd Bergmann }
1671b89f4321SArnd Bergmann 
16721da177e4SLinus Torvalds /**
1673a9933ceaSJ. Bruce Fields  *	vfs_setlease        -       sets a lease on an open file
16741da177e4SLinus Torvalds  *	@filp: file pointer
16751da177e4SLinus Torvalds  *	@arg: type of lease to obtain
16761da177e4SLinus Torvalds  *	@lease: file_lock to use
16771da177e4SLinus Torvalds  *
16781da177e4SLinus Torvalds  *	Call this to establish a lease on the file.
16798fb47a4fSJ. Bruce Fields  *	The (*lease)->fl_lmops->lm_break operation must be set; if not,
1680f9ffed26SJ. Bruce Fields  *	break_lease will oops!
1681f9ffed26SJ. Bruce Fields  *
1682f9ffed26SJ. Bruce Fields  *	This will call the filesystem's setlease file method, if
1683f9ffed26SJ. Bruce Fields  *	defined.  Note that there is no getlease method; instead, the
1684f9ffed26SJ. Bruce Fields  *	filesystem setlease method should call back to setlease() to
1685f9ffed26SJ. Bruce Fields  *	add a lease to the inode's lease list, where fcntl_getlease() can
1686f9ffed26SJ. Bruce Fields  *	find it.  Since fcntl_getlease() only reports whether the current
1687f9ffed26SJ. Bruce Fields  *	task holds a lease, a cluster filesystem need only do this for
1688f9ffed26SJ. Bruce Fields  *	leases held by processes on this node.
1689f9ffed26SJ. Bruce Fields  *
1690f9ffed26SJ. Bruce Fields  *	There is also no break_lease method; filesystems that
1691c9404c9cSAdam Buchbinder  *	handle their own leases should break leases themselves from the
1692f9ffed26SJ. Bruce Fields  *	filesystem's open, create, and (on truncate) setattr methods.
1693f9ffed26SJ. Bruce Fields  *
1694f9ffed26SJ. Bruce Fields  *	Warning: the only current setlease methods exist only to disable
1695f9ffed26SJ. Bruce Fields  *	leases in certain cases.  More vfs changes may be required to
1696f9ffed26SJ. Bruce Fields  *	allow a full filesystem lease implementation.
16971da177e4SLinus Torvalds  */
16981da177e4SLinus Torvalds 
1699a9933ceaSJ. Bruce Fields int vfs_setlease(struct file *filp, long arg, struct file_lock **lease)
17001da177e4SLinus Torvalds {
17011c8c601aSJeff Layton 	struct inode *inode = file_inode(filp);
17021da177e4SLinus Torvalds 	int error;
17031da177e4SLinus Torvalds 
17041c8c601aSJeff Layton 	spin_lock(&inode->i_lock);
1705b89f4321SArnd Bergmann 	error = __vfs_setlease(filp, arg, lease);
17061c8c601aSJeff Layton 	spin_unlock(&inode->i_lock);
17071da177e4SLinus Torvalds 
17081da177e4SLinus Torvalds 	return error;
17091da177e4SLinus Torvalds }
1710a9933ceaSJ. Bruce Fields EXPORT_SYMBOL_GPL(vfs_setlease);
17111da177e4SLinus Torvalds 
17120ceaf6c7SJ. Bruce Fields static int do_fcntl_delete_lease(struct file *filp)
17130ceaf6c7SJ. Bruce Fields {
17140ceaf6c7SJ. Bruce Fields 	struct file_lock fl, *flp = &fl;
17150ceaf6c7SJ. Bruce Fields 
17160ceaf6c7SJ. Bruce Fields 	lease_init(filp, F_UNLCK, flp);
17170ceaf6c7SJ. Bruce Fields 
17180ceaf6c7SJ. Bruce Fields 	return vfs_setlease(filp, F_UNLCK, &flp);
17190ceaf6c7SJ. Bruce Fields }
17200ceaf6c7SJ. Bruce Fields 
17210ceaf6c7SJ. Bruce Fields static int do_fcntl_add_lease(unsigned int fd, struct file *filp, long arg)
17221da177e4SLinus Torvalds {
17233df057acSJ. Bruce Fields 	struct file_lock *fl, *ret;
17241c8c601aSJeff Layton 	struct inode *inode = file_inode(filp);
1725f7347ce4SLinus Torvalds 	struct fasync_struct *new;
17261da177e4SLinus Torvalds 	int error;
17271da177e4SLinus Torvalds 
1728c5b1f0d9SArnd Bergmann 	fl = lease_alloc(filp, arg);
1729c5b1f0d9SArnd Bergmann 	if (IS_ERR(fl))
1730c5b1f0d9SArnd Bergmann 		return PTR_ERR(fl);
17311da177e4SLinus Torvalds 
1732f7347ce4SLinus Torvalds 	new = fasync_alloc();
1733f7347ce4SLinus Torvalds 	if (!new) {
1734f7347ce4SLinus Torvalds 		locks_free_lock(fl);
1735f7347ce4SLinus Torvalds 		return -ENOMEM;
1736f7347ce4SLinus Torvalds 	}
17373df057acSJ. Bruce Fields 	ret = fl;
17381c8c601aSJeff Layton 	spin_lock(&inode->i_lock);
17398896b93fSJ. Bruce Fields 	error = __vfs_setlease(filp, arg, &ret);
174051ee4b84SChristoph Hellwig 	if (error) {
17411c8c601aSJeff Layton 		spin_unlock(&inode->i_lock);
174251ee4b84SChristoph Hellwig 		locks_free_lock(fl);
174351ee4b84SChristoph Hellwig 		goto out_free_fasync;
174451ee4b84SChristoph Hellwig 	}
17453df057acSJ. Bruce Fields 	if (ret != fl)
17463df057acSJ. Bruce Fields 		locks_free_lock(fl);
17471da177e4SLinus Torvalds 
1748f7347ce4SLinus Torvalds 	/*
1749f7347ce4SLinus Torvalds 	 * fasync_insert_entry() returns the old entry if any.
1750f7347ce4SLinus Torvalds 	 * If there was no old entry, then it used 'new' and
1751f7347ce4SLinus Torvalds 	 * inserted it into the fasync list. Clear new so that
1752f7347ce4SLinus Torvalds 	 * we don't release it here.
1753f7347ce4SLinus Torvalds 	 */
17543df057acSJ. Bruce Fields 	if (!fasync_insert_entry(fd, filp, &ret->fl_fasync, new))
1755f7347ce4SLinus Torvalds 		new = NULL;
1756f7347ce4SLinus Torvalds 
1757609d7fa9SEric W. Biederman 	error = __f_setown(filp, task_pid(current), PIDTYPE_PID, 0);
17581c8c601aSJeff Layton 	spin_unlock(&inode->i_lock);
175951ee4b84SChristoph Hellwig 
176051ee4b84SChristoph Hellwig out_free_fasync:
1761f7347ce4SLinus Torvalds 	if (new)
1762f7347ce4SLinus Torvalds 		fasync_free(new);
17631da177e4SLinus Torvalds 	return error;
17641da177e4SLinus Torvalds }
17651da177e4SLinus Torvalds 
17661da177e4SLinus Torvalds /**
17670ceaf6c7SJ. Bruce Fields  *	fcntl_setlease	-	sets a lease on an open file
17680ceaf6c7SJ. Bruce Fields  *	@fd: open file descriptor
17690ceaf6c7SJ. Bruce Fields  *	@filp: file pointer
17700ceaf6c7SJ. Bruce Fields  *	@arg: type of lease to obtain
17710ceaf6c7SJ. Bruce Fields  *
17720ceaf6c7SJ. Bruce Fields  *	Call this fcntl to establish a lease on the file.
17730ceaf6c7SJ. Bruce Fields  *	Note that you also need to call %F_SETSIG to
17740ceaf6c7SJ. Bruce Fields  *	receive a signal when the lease is broken.
17750ceaf6c7SJ. Bruce Fields  */
17760ceaf6c7SJ. Bruce Fields int fcntl_setlease(unsigned int fd, struct file *filp, long arg)
17770ceaf6c7SJ. Bruce Fields {
17780ceaf6c7SJ. Bruce Fields 	if (arg == F_UNLCK)
17790ceaf6c7SJ. Bruce Fields 		return do_fcntl_delete_lease(filp);
17800ceaf6c7SJ. Bruce Fields 	return do_fcntl_add_lease(fd, filp, arg);
17810ceaf6c7SJ. Bruce Fields }
17820ceaf6c7SJ. Bruce Fields 
17830ceaf6c7SJ. Bruce Fields /**
17841da177e4SLinus Torvalds  * flock_lock_file_wait - Apply a FLOCK-style lock to a file
17851da177e4SLinus Torvalds  * @filp: The file to apply the lock to
17861da177e4SLinus Torvalds  * @fl: The lock to be applied
17871da177e4SLinus Torvalds  *
17881da177e4SLinus Torvalds  * Add a FLOCK style lock to a file.
17891da177e4SLinus Torvalds  */
17901da177e4SLinus Torvalds int flock_lock_file_wait(struct file *filp, struct file_lock *fl)
17911da177e4SLinus Torvalds {
17921da177e4SLinus Torvalds 	int error;
17931da177e4SLinus Torvalds 	might_sleep();
17941da177e4SLinus Torvalds 	for (;;) {
17951da177e4SLinus Torvalds 		error = flock_lock_file(filp, fl);
1796bde74e4bSMiklos Szeredi 		if (error != FILE_LOCK_DEFERRED)
17971da177e4SLinus Torvalds 			break;
17981da177e4SLinus Torvalds 		error = wait_event_interruptible(fl->fl_wait, !fl->fl_next);
17991da177e4SLinus Torvalds 		if (!error)
18001da177e4SLinus Torvalds 			continue;
18011da177e4SLinus Torvalds 
18021da177e4SLinus Torvalds 		locks_delete_block(fl);
18031da177e4SLinus Torvalds 		break;
18041da177e4SLinus Torvalds 	}
18051da177e4SLinus Torvalds 	return error;
18061da177e4SLinus Torvalds }
18071da177e4SLinus Torvalds 
18081da177e4SLinus Torvalds EXPORT_SYMBOL(flock_lock_file_wait);
18091da177e4SLinus Torvalds 
18101da177e4SLinus Torvalds /**
18111da177e4SLinus Torvalds  *	sys_flock: - flock() system call.
18121da177e4SLinus Torvalds  *	@fd: the file descriptor to lock.
18131da177e4SLinus Torvalds  *	@cmd: the type of lock to apply.
18141da177e4SLinus Torvalds  *
18151da177e4SLinus Torvalds  *	Apply a %FL_FLOCK style lock to an open file descriptor.
18161da177e4SLinus Torvalds  *	The @cmd can be one of
18171da177e4SLinus Torvalds  *
18181da177e4SLinus Torvalds  *	%LOCK_SH -- a shared lock.
18191da177e4SLinus Torvalds  *
18201da177e4SLinus Torvalds  *	%LOCK_EX -- an exclusive lock.
18211da177e4SLinus Torvalds  *
18221da177e4SLinus Torvalds  *	%LOCK_UN -- remove an existing lock.
18231da177e4SLinus Torvalds  *
18241da177e4SLinus Torvalds  *	%LOCK_MAND -- a `mandatory' flock.  This exists to emulate Windows Share Modes.
18251da177e4SLinus Torvalds  *
18261da177e4SLinus Torvalds  *	%LOCK_MAND can be combined with %LOCK_READ or %LOCK_WRITE to allow other
18271da177e4SLinus Torvalds  *	processes read and write access respectively.
18281da177e4SLinus Torvalds  */
1829002c8976SHeiko Carstens SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd)
18301da177e4SLinus Torvalds {
18312903ff01SAl Viro 	struct fd f = fdget(fd);
18321da177e4SLinus Torvalds 	struct file_lock *lock;
18331da177e4SLinus Torvalds 	int can_sleep, unlock;
18341da177e4SLinus Torvalds 	int error;
18351da177e4SLinus Torvalds 
18361da177e4SLinus Torvalds 	error = -EBADF;
18372903ff01SAl Viro 	if (!f.file)
18381da177e4SLinus Torvalds 		goto out;
18391da177e4SLinus Torvalds 
18401da177e4SLinus Torvalds 	can_sleep = !(cmd & LOCK_NB);
18411da177e4SLinus Torvalds 	cmd &= ~LOCK_NB;
18421da177e4SLinus Torvalds 	unlock = (cmd == LOCK_UN);
18431da177e4SLinus Torvalds 
1844aeb5d727SAl Viro 	if (!unlock && !(cmd & LOCK_MAND) &&
18452903ff01SAl Viro 	    !(f.file->f_mode & (FMODE_READ|FMODE_WRITE)))
18461da177e4SLinus Torvalds 		goto out_putf;
18471da177e4SLinus Torvalds 
18482903ff01SAl Viro 	error = flock_make_lock(f.file, &lock, cmd);
18491da177e4SLinus Torvalds 	if (error)
18501da177e4SLinus Torvalds 		goto out_putf;
18511da177e4SLinus Torvalds 	if (can_sleep)
18521da177e4SLinus Torvalds 		lock->fl_flags |= FL_SLEEP;
18531da177e4SLinus Torvalds 
18542903ff01SAl Viro 	error = security_file_lock(f.file, lock->fl_type);
18551da177e4SLinus Torvalds 	if (error)
18561da177e4SLinus Torvalds 		goto out_free;
18571da177e4SLinus Torvalds 
185872c2d531SAl Viro 	if (f.file->f_op->flock)
18592903ff01SAl Viro 		error = f.file->f_op->flock(f.file,
18601da177e4SLinus Torvalds 					  (can_sleep) ? F_SETLKW : F_SETLK,
18611da177e4SLinus Torvalds 					  lock);
18621da177e4SLinus Torvalds 	else
18632903ff01SAl Viro 		error = flock_lock_file_wait(f.file, lock);
18641da177e4SLinus Torvalds 
18651da177e4SLinus Torvalds  out_free:
18661da177e4SLinus Torvalds 	locks_free_lock(lock);
18671da177e4SLinus Torvalds 
18681da177e4SLinus Torvalds  out_putf:
18692903ff01SAl Viro 	fdput(f);
18701da177e4SLinus Torvalds  out:
18711da177e4SLinus Torvalds 	return error;
18721da177e4SLinus Torvalds }
18731da177e4SLinus Torvalds 
18743ee17abdSJ. Bruce Fields /**
18753ee17abdSJ. Bruce Fields  * vfs_test_lock - test file byte range lock
18763ee17abdSJ. Bruce Fields  * @filp: The file to test lock for
18776924c554SJ. Bruce Fields  * @fl: The lock to test; also used to hold result
18783ee17abdSJ. Bruce Fields  *
18793ee17abdSJ. Bruce Fields  * Returns -ERRNO on failure.  Indicates presence of conflicting lock by
18803ee17abdSJ. Bruce Fields  * setting conf->fl_type to something other than F_UNLCK.
18813ee17abdSJ. Bruce Fields  */
18823ee17abdSJ. Bruce Fields int vfs_test_lock(struct file *filp, struct file_lock *fl)
18833ee17abdSJ. Bruce Fields {
188472c2d531SAl Viro 	if (filp->f_op->lock)
18853ee17abdSJ. Bruce Fields 		return filp->f_op->lock(filp, F_GETLK, fl);
18863ee17abdSJ. Bruce Fields 	posix_test_lock(filp, fl);
18873ee17abdSJ. Bruce Fields 	return 0;
18883ee17abdSJ. Bruce Fields }
18893ee17abdSJ. Bruce Fields EXPORT_SYMBOL_GPL(vfs_test_lock);
18903ee17abdSJ. Bruce Fields 
1891c2fa1b8aSJ. Bruce Fields static int posix_lock_to_flock(struct flock *flock, struct file_lock *fl)
1892c2fa1b8aSJ. Bruce Fields {
18933fd80cddSJeff Layton 	flock->l_pid = IS_FILE_PVT(fl) ? -1 : fl->fl_pid;
1894c2fa1b8aSJ. Bruce Fields #if BITS_PER_LONG == 32
1895c2fa1b8aSJ. Bruce Fields 	/*
1896c2fa1b8aSJ. Bruce Fields 	 * Make sure we can represent the posix lock via
1897c2fa1b8aSJ. Bruce Fields 	 * legacy 32bit flock.
1898c2fa1b8aSJ. Bruce Fields 	 */
1899c2fa1b8aSJ. Bruce Fields 	if (fl->fl_start > OFFT_OFFSET_MAX)
1900c2fa1b8aSJ. Bruce Fields 		return -EOVERFLOW;
1901c2fa1b8aSJ. Bruce Fields 	if (fl->fl_end != OFFSET_MAX && fl->fl_end > OFFT_OFFSET_MAX)
1902c2fa1b8aSJ. Bruce Fields 		return -EOVERFLOW;
1903c2fa1b8aSJ. Bruce Fields #endif
1904c2fa1b8aSJ. Bruce Fields 	flock->l_start = fl->fl_start;
1905c2fa1b8aSJ. Bruce Fields 	flock->l_len = fl->fl_end == OFFSET_MAX ? 0 :
1906c2fa1b8aSJ. Bruce Fields 		fl->fl_end - fl->fl_start + 1;
1907c2fa1b8aSJ. Bruce Fields 	flock->l_whence = 0;
1908129a84deSJ. Bruce Fields 	flock->l_type = fl->fl_type;
1909c2fa1b8aSJ. Bruce Fields 	return 0;
1910c2fa1b8aSJ. Bruce Fields }
1911c2fa1b8aSJ. Bruce Fields 
1912c2fa1b8aSJ. Bruce Fields #if BITS_PER_LONG == 32
1913c2fa1b8aSJ. Bruce Fields static void posix_lock_to_flock64(struct flock64 *flock, struct file_lock *fl)
1914c2fa1b8aSJ. Bruce Fields {
19153fd80cddSJeff Layton 	flock->l_pid = IS_FILE_PVT(fl) ? -1 : fl->fl_pid;
1916c2fa1b8aSJ. Bruce Fields 	flock->l_start = fl->fl_start;
1917c2fa1b8aSJ. Bruce Fields 	flock->l_len = fl->fl_end == OFFSET_MAX ? 0 :
1918c2fa1b8aSJ. Bruce Fields 		fl->fl_end - fl->fl_start + 1;
1919c2fa1b8aSJ. Bruce Fields 	flock->l_whence = 0;
1920c2fa1b8aSJ. Bruce Fields 	flock->l_type = fl->fl_type;
1921c2fa1b8aSJ. Bruce Fields }
1922c2fa1b8aSJ. Bruce Fields #endif
1923c2fa1b8aSJ. Bruce Fields 
19241da177e4SLinus Torvalds /* Report the first existing lock that would conflict with l.
19251da177e4SLinus Torvalds  * This implements the F_GETLK command of fcntl().
19261da177e4SLinus Torvalds  */
1927c1e62b8fSJeff Layton int fcntl_getlk(struct file *filp, unsigned int cmd, struct flock __user *l)
19281da177e4SLinus Torvalds {
19299d6a8c5cSMarc Eshel 	struct file_lock file_lock;
19301da177e4SLinus Torvalds 	struct flock flock;
19311da177e4SLinus Torvalds 	int error;
19321da177e4SLinus Torvalds 
19331da177e4SLinus Torvalds 	error = -EFAULT;
19341da177e4SLinus Torvalds 	if (copy_from_user(&flock, l, sizeof(flock)))
19351da177e4SLinus Torvalds 		goto out;
19361da177e4SLinus Torvalds 	error = -EINVAL;
19371da177e4SLinus Torvalds 	if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK))
19381da177e4SLinus Torvalds 		goto out;
19391da177e4SLinus Torvalds 
19401da177e4SLinus Torvalds 	error = flock_to_posix_lock(filp, &file_lock, &flock);
19411da177e4SLinus Torvalds 	if (error)
19421da177e4SLinus Torvalds 		goto out;
19431da177e4SLinus Torvalds 
1944*0d3f7a2dSJeff Layton 	if (cmd == F_OFD_GETLK) {
194590478939SJeff Layton 		error = -EINVAL;
194690478939SJeff Layton 		if (flock.l_pid != 0)
194790478939SJeff Layton 			goto out;
194890478939SJeff Layton 
19495d50ffd7SJeff Layton 		cmd = F_GETLK;
19505d50ffd7SJeff Layton 		file_lock.fl_flags |= FL_FILE_PVT;
19515d50ffd7SJeff Layton 		file_lock.fl_owner = (fl_owner_t)filp;
19525d50ffd7SJeff Layton 	}
19535d50ffd7SJeff Layton 
19543ee17abdSJ. Bruce Fields 	error = vfs_test_lock(filp, &file_lock);
19553ee17abdSJ. Bruce Fields 	if (error)
19561da177e4SLinus Torvalds 		goto out;
19571da177e4SLinus Torvalds 
19589d6a8c5cSMarc Eshel 	flock.l_type = file_lock.fl_type;
19599d6a8c5cSMarc Eshel 	if (file_lock.fl_type != F_UNLCK) {
19609d6a8c5cSMarc Eshel 		error = posix_lock_to_flock(&flock, &file_lock);
1961c2fa1b8aSJ. Bruce Fields 		if (error)
19621da177e4SLinus Torvalds 			goto out;
19631da177e4SLinus Torvalds 	}
19641da177e4SLinus Torvalds 	error = -EFAULT;
19651da177e4SLinus Torvalds 	if (!copy_to_user(l, &flock, sizeof(flock)))
19661da177e4SLinus Torvalds 		error = 0;
19671da177e4SLinus Torvalds out:
19681da177e4SLinus Torvalds 	return error;
19691da177e4SLinus Torvalds }
19701da177e4SLinus Torvalds 
19717723ec97SMarc Eshel /**
19727723ec97SMarc Eshel  * vfs_lock_file - file byte range lock
19737723ec97SMarc Eshel  * @filp: The file to apply the lock to
19747723ec97SMarc Eshel  * @cmd: type of locking operation (F_SETLK, F_GETLK, etc.)
19757723ec97SMarc Eshel  * @fl: The lock to be applied
1976150b3934SMarc Eshel  * @conf: Place to return a copy of the conflicting lock, if found.
1977150b3934SMarc Eshel  *
1978150b3934SMarc Eshel  * A caller that doesn't care about the conflicting lock may pass NULL
1979150b3934SMarc Eshel  * as the final argument.
1980150b3934SMarc Eshel  *
1981150b3934SMarc Eshel  * If the filesystem defines a private ->lock() method, then @conf will
1982150b3934SMarc Eshel  * be left unchanged; so a caller that cares should initialize it to
1983150b3934SMarc Eshel  * some acceptable default.
19842beb6614SMarc Eshel  *
19852beb6614SMarc Eshel  * To avoid blocking kernel daemons, such as lockd, that need to acquire POSIX
19862beb6614SMarc Eshel  * locks, the ->lock() interface may return asynchronously, before the lock has
19872beb6614SMarc Eshel  * been granted or denied by the underlying filesystem, if (and only if)
19888fb47a4fSJ. Bruce Fields  * lm_grant is set. Callers expecting ->lock() to return asynchronously
19892beb6614SMarc Eshel  * will only use F_SETLK, not F_SETLKW; they will set FL_SLEEP if (and only if)
19902beb6614SMarc Eshel  * the request is for a blocking lock. When ->lock() does return asynchronously,
19918fb47a4fSJ. Bruce Fields  * it must return FILE_LOCK_DEFERRED, and call ->lm_grant() when the lock
19922beb6614SMarc Eshel  * request completes.
19932beb6614SMarc Eshel  * If the request is for non-blocking lock the file system should return
1994bde74e4bSMiklos Szeredi  * FILE_LOCK_DEFERRED then try to get the lock and call the callback routine
1995bde74e4bSMiklos Szeredi  * with the result. If the request timed out the callback routine will return a
19962beb6614SMarc Eshel  * nonzero return code and the file system should release the lock. The file
19972beb6614SMarc Eshel  * system is also responsible to keep a corresponding posix lock when it
19982beb6614SMarc Eshel  * grants a lock so the VFS can find out which locks are locally held and do
19992beb6614SMarc Eshel  * the correct lock cleanup when required.
20002beb6614SMarc Eshel  * The underlying filesystem must not drop the kernel lock or call
20018fb47a4fSJ. Bruce Fields  * ->lm_grant() before returning to the caller with a FILE_LOCK_DEFERRED
20022beb6614SMarc Eshel  * return code.
20037723ec97SMarc Eshel  */
2004150b3934SMarc Eshel int vfs_lock_file(struct file *filp, unsigned int cmd, struct file_lock *fl, struct file_lock *conf)
20057723ec97SMarc Eshel {
200672c2d531SAl Viro 	if (filp->f_op->lock)
20077723ec97SMarc Eshel 		return filp->f_op->lock(filp, cmd, fl);
20087723ec97SMarc Eshel 	else
2009150b3934SMarc Eshel 		return posix_lock_file(filp, fl, conf);
20107723ec97SMarc Eshel }
20117723ec97SMarc Eshel EXPORT_SYMBOL_GPL(vfs_lock_file);
20127723ec97SMarc Eshel 
2013b648a6deSMiklos Szeredi static int do_lock_file_wait(struct file *filp, unsigned int cmd,
2014b648a6deSMiklos Szeredi 			     struct file_lock *fl)
2015b648a6deSMiklos Szeredi {
2016b648a6deSMiklos Szeredi 	int error;
2017b648a6deSMiklos Szeredi 
2018b648a6deSMiklos Szeredi 	error = security_file_lock(filp, fl->fl_type);
2019b648a6deSMiklos Szeredi 	if (error)
2020b648a6deSMiklos Szeredi 		return error;
2021b648a6deSMiklos Szeredi 
2022b648a6deSMiklos Szeredi 	for (;;) {
2023764c76b3SMiklos Szeredi 		error = vfs_lock_file(filp, cmd, fl, NULL);
2024b648a6deSMiklos Szeredi 		if (error != FILE_LOCK_DEFERRED)
2025b648a6deSMiklos Szeredi 			break;
2026764c76b3SMiklos Szeredi 		error = wait_event_interruptible(fl->fl_wait, !fl->fl_next);
2027b648a6deSMiklos Szeredi 		if (!error)
2028b648a6deSMiklos Szeredi 			continue;
2029b648a6deSMiklos Szeredi 
2030b648a6deSMiklos Szeredi 		locks_delete_block(fl);
2031b648a6deSMiklos Szeredi 		break;
2032b648a6deSMiklos Szeredi 	}
2033b648a6deSMiklos Szeredi 
2034b648a6deSMiklos Szeredi 	return error;
2035b648a6deSMiklos Szeredi }
2036b648a6deSMiklos Szeredi 
20371da177e4SLinus Torvalds /* Apply the lock described by l to an open file descriptor.
20381da177e4SLinus Torvalds  * This implements both the F_SETLK and F_SETLKW commands of fcntl().
20391da177e4SLinus Torvalds  */
2040c293621bSPeter Staubach int fcntl_setlk(unsigned int fd, struct file *filp, unsigned int cmd,
2041c293621bSPeter Staubach 		struct flock __user *l)
20421da177e4SLinus Torvalds {
20431da177e4SLinus Torvalds 	struct file_lock *file_lock = locks_alloc_lock();
20441da177e4SLinus Torvalds 	struct flock flock;
20451da177e4SLinus Torvalds 	struct inode *inode;
20460b2bac2fSAl Viro 	struct file *f;
20471da177e4SLinus Torvalds 	int error;
20481da177e4SLinus Torvalds 
20491da177e4SLinus Torvalds 	if (file_lock == NULL)
20501da177e4SLinus Torvalds 		return -ENOLCK;
20511da177e4SLinus Torvalds 
20521da177e4SLinus Torvalds 	/*
20531da177e4SLinus Torvalds 	 * This might block, so we do it before checking the inode.
20541da177e4SLinus Torvalds 	 */
20551da177e4SLinus Torvalds 	error = -EFAULT;
20561da177e4SLinus Torvalds 	if (copy_from_user(&flock, l, sizeof(flock)))
20571da177e4SLinus Torvalds 		goto out;
20581da177e4SLinus Torvalds 
2059496ad9aaSAl Viro 	inode = file_inode(filp);
20601da177e4SLinus Torvalds 
20611da177e4SLinus Torvalds 	/* Don't allow mandatory locks on files that may be memory mapped
20621da177e4SLinus Torvalds 	 * and shared.
20631da177e4SLinus Torvalds 	 */
2064a16877caSPavel Emelyanov 	if (mandatory_lock(inode) && mapping_writably_mapped(filp->f_mapping)) {
20651da177e4SLinus Torvalds 		error = -EAGAIN;
20661da177e4SLinus Torvalds 		goto out;
20671da177e4SLinus Torvalds 	}
20681da177e4SLinus Torvalds 
2069c293621bSPeter Staubach again:
20701da177e4SLinus Torvalds 	error = flock_to_posix_lock(filp, file_lock, &flock);
20711da177e4SLinus Torvalds 	if (error)
20721da177e4SLinus Torvalds 		goto out;
20735d50ffd7SJeff Layton 
20745d50ffd7SJeff Layton 	/*
20755d50ffd7SJeff Layton 	 * If the cmd is requesting file-private locks, then set the
20765d50ffd7SJeff Layton 	 * FL_FILE_PVT flag and override the owner.
20775d50ffd7SJeff Layton 	 */
20785d50ffd7SJeff Layton 	switch (cmd) {
2079*0d3f7a2dSJeff Layton 	case F_OFD_SETLK:
208090478939SJeff Layton 		error = -EINVAL;
208190478939SJeff Layton 		if (flock.l_pid != 0)
208290478939SJeff Layton 			goto out;
208390478939SJeff Layton 
20845d50ffd7SJeff Layton 		cmd = F_SETLK;
20855d50ffd7SJeff Layton 		file_lock->fl_flags |= FL_FILE_PVT;
20865d50ffd7SJeff Layton 		file_lock->fl_owner = (fl_owner_t)filp;
20875d50ffd7SJeff Layton 		break;
2088*0d3f7a2dSJeff Layton 	case F_OFD_SETLKW:
208990478939SJeff Layton 		error = -EINVAL;
209090478939SJeff Layton 		if (flock.l_pid != 0)
209190478939SJeff Layton 			goto out;
209290478939SJeff Layton 
20935d50ffd7SJeff Layton 		cmd = F_SETLKW;
20945d50ffd7SJeff Layton 		file_lock->fl_flags |= FL_FILE_PVT;
20955d50ffd7SJeff Layton 		file_lock->fl_owner = (fl_owner_t)filp;
20965d50ffd7SJeff Layton 		/* Fallthrough */
20975d50ffd7SJeff Layton 	case F_SETLKW:
20981da177e4SLinus Torvalds 		file_lock->fl_flags |= FL_SLEEP;
20991da177e4SLinus Torvalds 	}
21001da177e4SLinus Torvalds 
2101b648a6deSMiklos Szeredi 	error = do_lock_file_wait(filp, cmd, file_lock);
2102c293621bSPeter Staubach 
2103c293621bSPeter Staubach 	/*
2104c293621bSPeter Staubach 	 * Attempt to detect a close/fcntl race and recover by
2105c293621bSPeter Staubach 	 * releasing the lock that was just acquired.
2106c293621bSPeter Staubach 	 */
21070b2bac2fSAl Viro 	/*
21080b2bac2fSAl Viro 	 * we need that spin_lock here - it prevents reordering between
21090b2bac2fSAl Viro 	 * update of inode->i_flock and check for it done in close().
21100b2bac2fSAl Viro 	 * rcu_read_lock() wouldn't do.
21110b2bac2fSAl Viro 	 */
21120b2bac2fSAl Viro 	spin_lock(&current->files->file_lock);
21130b2bac2fSAl Viro 	f = fcheck(fd);
21140b2bac2fSAl Viro 	spin_unlock(&current->files->file_lock);
21150b2bac2fSAl Viro 	if (!error && f != filp && flock.l_type != F_UNLCK) {
2116c293621bSPeter Staubach 		flock.l_type = F_UNLCK;
2117c293621bSPeter Staubach 		goto again;
2118c293621bSPeter Staubach 	}
21191da177e4SLinus Torvalds 
21201da177e4SLinus Torvalds out:
21211da177e4SLinus Torvalds 	locks_free_lock(file_lock);
21221da177e4SLinus Torvalds 	return error;
21231da177e4SLinus Torvalds }
21241da177e4SLinus Torvalds 
21251da177e4SLinus Torvalds #if BITS_PER_LONG == 32
21261da177e4SLinus Torvalds /* Report the first existing lock that would conflict with l.
21271da177e4SLinus Torvalds  * This implements the F_GETLK command of fcntl().
21281da177e4SLinus Torvalds  */
2129c1e62b8fSJeff Layton int fcntl_getlk64(struct file *filp, unsigned int cmd, struct flock64 __user *l)
21301da177e4SLinus Torvalds {
21319d6a8c5cSMarc Eshel 	struct file_lock file_lock;
21321da177e4SLinus Torvalds 	struct flock64 flock;
21331da177e4SLinus Torvalds 	int error;
21341da177e4SLinus Torvalds 
21351da177e4SLinus Torvalds 	error = -EFAULT;
21361da177e4SLinus Torvalds 	if (copy_from_user(&flock, l, sizeof(flock)))
21371da177e4SLinus Torvalds 		goto out;
21381da177e4SLinus Torvalds 	error = -EINVAL;
21391da177e4SLinus Torvalds 	if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK))
21401da177e4SLinus Torvalds 		goto out;
21411da177e4SLinus Torvalds 
21421da177e4SLinus Torvalds 	error = flock64_to_posix_lock(filp, &file_lock, &flock);
21431da177e4SLinus Torvalds 	if (error)
21441da177e4SLinus Torvalds 		goto out;
21451da177e4SLinus Torvalds 
2146*0d3f7a2dSJeff Layton 	if (cmd == F_OFD_GETLK) {
214790478939SJeff Layton 		error = -EINVAL;
214890478939SJeff Layton 		if (flock.l_pid != 0)
214990478939SJeff Layton 			goto out;
215090478939SJeff Layton 
21515d50ffd7SJeff Layton 		cmd = F_GETLK64;
21525d50ffd7SJeff Layton 		file_lock.fl_flags |= FL_FILE_PVT;
21535d50ffd7SJeff Layton 		file_lock.fl_owner = (fl_owner_t)filp;
21545d50ffd7SJeff Layton 	}
21555d50ffd7SJeff Layton 
21563ee17abdSJ. Bruce Fields 	error = vfs_test_lock(filp, &file_lock);
21573ee17abdSJ. Bruce Fields 	if (error)
21581da177e4SLinus Torvalds 		goto out;
21591da177e4SLinus Torvalds 
21609d6a8c5cSMarc Eshel 	flock.l_type = file_lock.fl_type;
21619d6a8c5cSMarc Eshel 	if (file_lock.fl_type != F_UNLCK)
21629d6a8c5cSMarc Eshel 		posix_lock_to_flock64(&flock, &file_lock);
21639d6a8c5cSMarc Eshel 
21641da177e4SLinus Torvalds 	error = -EFAULT;
21651da177e4SLinus Torvalds 	if (!copy_to_user(l, &flock, sizeof(flock)))
21661da177e4SLinus Torvalds 		error = 0;
21671da177e4SLinus Torvalds 
21681da177e4SLinus Torvalds out:
21691da177e4SLinus Torvalds 	return error;
21701da177e4SLinus Torvalds }
21711da177e4SLinus Torvalds 
21721da177e4SLinus Torvalds /* Apply the lock described by l to an open file descriptor.
21731da177e4SLinus Torvalds  * This implements both the F_SETLK and F_SETLKW commands of fcntl().
21741da177e4SLinus Torvalds  */
2175c293621bSPeter Staubach int fcntl_setlk64(unsigned int fd, struct file *filp, unsigned int cmd,
2176c293621bSPeter Staubach 		struct flock64 __user *l)
21771da177e4SLinus Torvalds {
21781da177e4SLinus Torvalds 	struct file_lock *file_lock = locks_alloc_lock();
21791da177e4SLinus Torvalds 	struct flock64 flock;
21801da177e4SLinus Torvalds 	struct inode *inode;
21810b2bac2fSAl Viro 	struct file *f;
21821da177e4SLinus Torvalds 	int error;
21831da177e4SLinus Torvalds 
21841da177e4SLinus Torvalds 	if (file_lock == NULL)
21851da177e4SLinus Torvalds 		return -ENOLCK;
21861da177e4SLinus Torvalds 
21871da177e4SLinus Torvalds 	/*
21881da177e4SLinus Torvalds 	 * This might block, so we do it before checking the inode.
21891da177e4SLinus Torvalds 	 */
21901da177e4SLinus Torvalds 	error = -EFAULT;
21911da177e4SLinus Torvalds 	if (copy_from_user(&flock, l, sizeof(flock)))
21921da177e4SLinus Torvalds 		goto out;
21931da177e4SLinus Torvalds 
2194496ad9aaSAl Viro 	inode = file_inode(filp);
21951da177e4SLinus Torvalds 
21961da177e4SLinus Torvalds 	/* Don't allow mandatory locks on files that may be memory mapped
21971da177e4SLinus Torvalds 	 * and shared.
21981da177e4SLinus Torvalds 	 */
2199a16877caSPavel Emelyanov 	if (mandatory_lock(inode) && mapping_writably_mapped(filp->f_mapping)) {
22001da177e4SLinus Torvalds 		error = -EAGAIN;
22011da177e4SLinus Torvalds 		goto out;
22021da177e4SLinus Torvalds 	}
22031da177e4SLinus Torvalds 
2204c293621bSPeter Staubach again:
22051da177e4SLinus Torvalds 	error = flock64_to_posix_lock(filp, file_lock, &flock);
22061da177e4SLinus Torvalds 	if (error)
22071da177e4SLinus Torvalds 		goto out;
22085d50ffd7SJeff Layton 
22095d50ffd7SJeff Layton 	/*
22105d50ffd7SJeff Layton 	 * If the cmd is requesting file-private locks, then set the
22115d50ffd7SJeff Layton 	 * FL_FILE_PVT flag and override the owner.
22125d50ffd7SJeff Layton 	 */
22135d50ffd7SJeff Layton 	switch (cmd) {
2214*0d3f7a2dSJeff Layton 	case F_OFD_SETLK:
221590478939SJeff Layton 		error = -EINVAL;
221690478939SJeff Layton 		if (flock.l_pid != 0)
221790478939SJeff Layton 			goto out;
221890478939SJeff Layton 
22195d50ffd7SJeff Layton 		cmd = F_SETLK64;
22205d50ffd7SJeff Layton 		file_lock->fl_flags |= FL_FILE_PVT;
22215d50ffd7SJeff Layton 		file_lock->fl_owner = (fl_owner_t)filp;
22225d50ffd7SJeff Layton 		break;
2223*0d3f7a2dSJeff Layton 	case F_OFD_SETLKW:
222490478939SJeff Layton 		error = -EINVAL;
222590478939SJeff Layton 		if (flock.l_pid != 0)
222690478939SJeff Layton 			goto out;
222790478939SJeff Layton 
22285d50ffd7SJeff Layton 		cmd = F_SETLKW64;
22295d50ffd7SJeff Layton 		file_lock->fl_flags |= FL_FILE_PVT;
22305d50ffd7SJeff Layton 		file_lock->fl_owner = (fl_owner_t)filp;
22315d50ffd7SJeff Layton 		/* Fallthrough */
22325d50ffd7SJeff Layton 	case F_SETLKW64:
22331da177e4SLinus Torvalds 		file_lock->fl_flags |= FL_SLEEP;
22341da177e4SLinus Torvalds 	}
22351da177e4SLinus Torvalds 
2236b648a6deSMiklos Szeredi 	error = do_lock_file_wait(filp, cmd, file_lock);
2237c293621bSPeter Staubach 
2238c293621bSPeter Staubach 	/*
2239c293621bSPeter Staubach 	 * Attempt to detect a close/fcntl race and recover by
2240c293621bSPeter Staubach 	 * releasing the lock that was just acquired.
2241c293621bSPeter Staubach 	 */
22420b2bac2fSAl Viro 	spin_lock(&current->files->file_lock);
22430b2bac2fSAl Viro 	f = fcheck(fd);
22440b2bac2fSAl Viro 	spin_unlock(&current->files->file_lock);
22450b2bac2fSAl Viro 	if (!error && f != filp && flock.l_type != F_UNLCK) {
2246c293621bSPeter Staubach 		flock.l_type = F_UNLCK;
2247c293621bSPeter Staubach 		goto again;
2248c293621bSPeter Staubach 	}
22491da177e4SLinus Torvalds 
22501da177e4SLinus Torvalds out:
22511da177e4SLinus Torvalds 	locks_free_lock(file_lock);
22521da177e4SLinus Torvalds 	return error;
22531da177e4SLinus Torvalds }
22541da177e4SLinus Torvalds #endif /* BITS_PER_LONG == 32 */
22551da177e4SLinus Torvalds 
22561da177e4SLinus Torvalds /*
22571da177e4SLinus Torvalds  * This function is called when the file is being removed
22581da177e4SLinus Torvalds  * from the task's fd array.  POSIX locks belonging to this task
22591da177e4SLinus Torvalds  * are deleted at this time.
22601da177e4SLinus Torvalds  */
22611da177e4SLinus Torvalds void locks_remove_posix(struct file *filp, fl_owner_t owner)
22621da177e4SLinus Torvalds {
2263ff7b86b8SMiklos Szeredi 	struct file_lock lock;
22641da177e4SLinus Torvalds 
22651da177e4SLinus Torvalds 	/*
22661da177e4SLinus Torvalds 	 * If there are no locks held on this file, we don't need to call
22671da177e4SLinus Torvalds 	 * posix_lock_file().  Another process could be setting a lock on this
22681da177e4SLinus Torvalds 	 * file at the same time, but we wouldn't remove that lock anyway.
22691da177e4SLinus Torvalds 	 */
2270496ad9aaSAl Viro 	if (!file_inode(filp)->i_flock)
22711da177e4SLinus Torvalds 		return;
22721da177e4SLinus Torvalds 
22731da177e4SLinus Torvalds 	lock.fl_type = F_UNLCK;
227475e1fcc0SMiklos Szeredi 	lock.fl_flags = FL_POSIX | FL_CLOSE;
22751da177e4SLinus Torvalds 	lock.fl_start = 0;
22761da177e4SLinus Torvalds 	lock.fl_end = OFFSET_MAX;
22771da177e4SLinus Torvalds 	lock.fl_owner = owner;
22781da177e4SLinus Torvalds 	lock.fl_pid = current->tgid;
22791da177e4SLinus Torvalds 	lock.fl_file = filp;
22801da177e4SLinus Torvalds 	lock.fl_ops = NULL;
22811da177e4SLinus Torvalds 	lock.fl_lmops = NULL;
22821da177e4SLinus Torvalds 
2283150b3934SMarc Eshel 	vfs_lock_file(filp, F_SETLK, &lock, NULL);
22841da177e4SLinus Torvalds 
22851da177e4SLinus Torvalds 	if (lock.fl_ops && lock.fl_ops->fl_release_private)
22861da177e4SLinus Torvalds 		lock.fl_ops->fl_release_private(&lock);
22871da177e4SLinus Torvalds }
22881da177e4SLinus Torvalds 
22891da177e4SLinus Torvalds EXPORT_SYMBOL(locks_remove_posix);
22901da177e4SLinus Torvalds 
22911da177e4SLinus Torvalds /*
22921da177e4SLinus Torvalds  * This function is called on the last close of an open file.
22931da177e4SLinus Torvalds  */
229478ed8a13SJeff Layton void locks_remove_file(struct file *filp)
22951da177e4SLinus Torvalds {
2296496ad9aaSAl Viro 	struct inode * inode = file_inode(filp);
22971da177e4SLinus Torvalds 	struct file_lock *fl;
22981da177e4SLinus Torvalds 	struct file_lock **before;
22991da177e4SLinus Torvalds 
23001da177e4SLinus Torvalds 	if (!inode->i_flock)
23011da177e4SLinus Torvalds 		return;
23021da177e4SLinus Torvalds 
23035d50ffd7SJeff Layton 	locks_remove_posix(filp, (fl_owner_t)filp);
23045d50ffd7SJeff Layton 
230572c2d531SAl Viro 	if (filp->f_op->flock) {
23061da177e4SLinus Torvalds 		struct file_lock fl = {
23071da177e4SLinus Torvalds 			.fl_pid = current->tgid,
23081da177e4SLinus Torvalds 			.fl_file = filp,
23091da177e4SLinus Torvalds 			.fl_flags = FL_FLOCK,
23101da177e4SLinus Torvalds 			.fl_type = F_UNLCK,
23111da177e4SLinus Torvalds 			.fl_end = OFFSET_MAX,
23121da177e4SLinus Torvalds 		};
23131da177e4SLinus Torvalds 		filp->f_op->flock(filp, F_SETLKW, &fl);
231480fec4c6STrond Myklebust 		if (fl.fl_ops && fl.fl_ops->fl_release_private)
231580fec4c6STrond Myklebust 			fl.fl_ops->fl_release_private(&fl);
23161da177e4SLinus Torvalds 	}
23171da177e4SLinus Torvalds 
23181c8c601aSJeff Layton 	spin_lock(&inode->i_lock);
23191da177e4SLinus Torvalds 	before = &inode->i_flock;
23201da177e4SLinus Torvalds 
23211da177e4SLinus Torvalds 	while ((fl = *before) != NULL) {
23221da177e4SLinus Torvalds 		if (fl->fl_file == filp) {
23231da177e4SLinus Torvalds 			if (IS_LEASE(fl)) {
23241da177e4SLinus Torvalds 				lease_modify(before, F_UNLCK);
23251da177e4SLinus Torvalds 				continue;
23261da177e4SLinus Torvalds 			}
23278c3cac5eSJeff Layton 
23288c3cac5eSJeff Layton 			/*
23298c3cac5eSJeff Layton 			 * There's a leftover lock on the list of a type that
23308c3cac5eSJeff Layton 			 * we didn't expect to see. Most likely a classic
23318c3cac5eSJeff Layton 			 * POSIX lock that ended up not getting released
23328c3cac5eSJeff Layton 			 * properly, or that raced onto the list somehow. Log
23338c3cac5eSJeff Layton 			 * some info about it and then just remove it from
23348c3cac5eSJeff Layton 			 * the list.
23358c3cac5eSJeff Layton 			 */
23368c3cac5eSJeff Layton 			WARN(!IS_FLOCK(fl),
23378c3cac5eSJeff Layton 				"leftover lock: dev=%u:%u ino=%lu type=%hhd flags=0x%x start=%lld end=%lld\n",
23388c3cac5eSJeff Layton 				MAJOR(inode->i_sb->s_dev),
23398c3cac5eSJeff Layton 				MINOR(inode->i_sb->s_dev), inode->i_ino,
23408c3cac5eSJeff Layton 				fl->fl_type, fl->fl_flags,
23418c3cac5eSJeff Layton 				fl->fl_start, fl->fl_end);
23428c3cac5eSJeff Layton 
23438c3cac5eSJeff Layton 			locks_delete_lock(before);
23448c3cac5eSJeff Layton 			continue;
23451da177e4SLinus Torvalds  		}
23461da177e4SLinus Torvalds 		before = &fl->fl_next;
23471da177e4SLinus Torvalds 	}
23481c8c601aSJeff Layton 	spin_unlock(&inode->i_lock);
23491da177e4SLinus Torvalds }
23501da177e4SLinus Torvalds 
23511da177e4SLinus Torvalds /**
23521da177e4SLinus Torvalds  *	posix_unblock_lock - stop waiting for a file lock
23531da177e4SLinus Torvalds  *	@waiter: the lock which was waiting
23541da177e4SLinus Torvalds  *
23551da177e4SLinus Torvalds  *	lockd needs to block waiting for locks.
23561da177e4SLinus Torvalds  */
235764a318eeSJ. Bruce Fields int
2358f891a29fSJeff Layton posix_unblock_lock(struct file_lock *waiter)
23591da177e4SLinus Torvalds {
236064a318eeSJ. Bruce Fields 	int status = 0;
236164a318eeSJ. Bruce Fields 
23627b2296afSJeff Layton 	spin_lock(&blocked_lock_lock);
23635996a298SJ. Bruce Fields 	if (waiter->fl_next)
23641da177e4SLinus Torvalds 		__locks_delete_block(waiter);
236564a318eeSJ. Bruce Fields 	else
236664a318eeSJ. Bruce Fields 		status = -ENOENT;
23677b2296afSJeff Layton 	spin_unlock(&blocked_lock_lock);
236864a318eeSJ. Bruce Fields 	return status;
23691da177e4SLinus Torvalds }
23701da177e4SLinus Torvalds EXPORT_SYMBOL(posix_unblock_lock);
23711da177e4SLinus Torvalds 
23729b9d2ab4SMarc Eshel /**
23739b9d2ab4SMarc Eshel  * vfs_cancel_lock - file byte range unblock lock
23749b9d2ab4SMarc Eshel  * @filp: The file to apply the unblock to
23759b9d2ab4SMarc Eshel  * @fl: The lock to be unblocked
23769b9d2ab4SMarc Eshel  *
23779b9d2ab4SMarc Eshel  * Used by lock managers to cancel blocked requests
23789b9d2ab4SMarc Eshel  */
23799b9d2ab4SMarc Eshel int vfs_cancel_lock(struct file *filp, struct file_lock *fl)
23809b9d2ab4SMarc Eshel {
238172c2d531SAl Viro 	if (filp->f_op->lock)
23829b9d2ab4SMarc Eshel 		return filp->f_op->lock(filp, F_CANCELLK, fl);
23839b9d2ab4SMarc Eshel 	return 0;
23849b9d2ab4SMarc Eshel }
23859b9d2ab4SMarc Eshel 
23869b9d2ab4SMarc Eshel EXPORT_SYMBOL_GPL(vfs_cancel_lock);
23879b9d2ab4SMarc Eshel 
23887f8ada98SPavel Emelyanov #ifdef CONFIG_PROC_FS
2389d8ba7a36SAlexey Dobriyan #include <linux/proc_fs.h>
23907f8ada98SPavel Emelyanov #include <linux/seq_file.h>
23917f8ada98SPavel Emelyanov 
23927012b02aSJeff Layton struct locks_iterator {
23937012b02aSJeff Layton 	int	li_cpu;
23947012b02aSJeff Layton 	loff_t	li_pos;
23957012b02aSJeff Layton };
23967012b02aSJeff Layton 
23977f8ada98SPavel Emelyanov static void lock_get_status(struct seq_file *f, struct file_lock *fl,
239899dc8292SJerome Marchand 			    loff_t id, char *pfx)
23991da177e4SLinus Torvalds {
24001da177e4SLinus Torvalds 	struct inode *inode = NULL;
2401ab1f1611SVitaliy Gusev 	unsigned int fl_pid;
2402ab1f1611SVitaliy Gusev 
2403ab1f1611SVitaliy Gusev 	if (fl->fl_nspid)
24046c5f3e7bSPavel Emelyanov 		fl_pid = pid_vnr(fl->fl_nspid);
2405ab1f1611SVitaliy Gusev 	else
2406ab1f1611SVitaliy Gusev 		fl_pid = fl->fl_pid;
24071da177e4SLinus Torvalds 
24081da177e4SLinus Torvalds 	if (fl->fl_file != NULL)
2409496ad9aaSAl Viro 		inode = file_inode(fl->fl_file);
24101da177e4SLinus Torvalds 
241199dc8292SJerome Marchand 	seq_printf(f, "%lld:%s ", id, pfx);
24121da177e4SLinus Torvalds 	if (IS_POSIX(fl)) {
2413c918d42aSJeff Layton 		if (fl->fl_flags & FL_ACCESS)
2414c918d42aSJeff Layton 			seq_printf(f, "ACCESS");
2415c918d42aSJeff Layton 		else if (IS_FILE_PVT(fl))
2416*0d3f7a2dSJeff Layton 			seq_printf(f, "OFDLCK");
2417c918d42aSJeff Layton 		else
2418c918d42aSJeff Layton 			seq_printf(f, "POSIX ");
2419c918d42aSJeff Layton 
2420c918d42aSJeff Layton 		seq_printf(f, " %s ",
24211da177e4SLinus Torvalds 			     (inode == NULL) ? "*NOINODE*" :
2422a16877caSPavel Emelyanov 			     mandatory_lock(inode) ? "MANDATORY" : "ADVISORY ");
24231da177e4SLinus Torvalds 	} else if (IS_FLOCK(fl)) {
24241da177e4SLinus Torvalds 		if (fl->fl_type & LOCK_MAND) {
24257f8ada98SPavel Emelyanov 			seq_printf(f, "FLOCK  MSNFS     ");
24261da177e4SLinus Torvalds 		} else {
24277f8ada98SPavel Emelyanov 			seq_printf(f, "FLOCK  ADVISORY  ");
24281da177e4SLinus Torvalds 		}
24291da177e4SLinus Torvalds 	} else if (IS_LEASE(fl)) {
24307f8ada98SPavel Emelyanov 		seq_printf(f, "LEASE  ");
2431ab83fa4bSJ. Bruce Fields 		if (lease_breaking(fl))
24327f8ada98SPavel Emelyanov 			seq_printf(f, "BREAKING  ");
24331da177e4SLinus Torvalds 		else if (fl->fl_file)
24347f8ada98SPavel Emelyanov 			seq_printf(f, "ACTIVE    ");
24351da177e4SLinus Torvalds 		else
24367f8ada98SPavel Emelyanov 			seq_printf(f, "BREAKER   ");
24371da177e4SLinus Torvalds 	} else {
24387f8ada98SPavel Emelyanov 		seq_printf(f, "UNKNOWN UNKNOWN  ");
24391da177e4SLinus Torvalds 	}
24401da177e4SLinus Torvalds 	if (fl->fl_type & LOCK_MAND) {
24417f8ada98SPavel Emelyanov 		seq_printf(f, "%s ",
24421da177e4SLinus Torvalds 			       (fl->fl_type & LOCK_READ)
24431da177e4SLinus Torvalds 			       ? (fl->fl_type & LOCK_WRITE) ? "RW   " : "READ "
24441da177e4SLinus Torvalds 			       : (fl->fl_type & LOCK_WRITE) ? "WRITE" : "NONE ");
24451da177e4SLinus Torvalds 	} else {
24467f8ada98SPavel Emelyanov 		seq_printf(f, "%s ",
2447ab83fa4bSJ. Bruce Fields 			       (lease_breaking(fl))
24480ee5c6d6SJeff Layton 			       ? (fl->fl_type == F_UNLCK) ? "UNLCK" : "READ "
24490ee5c6d6SJeff Layton 			       : (fl->fl_type == F_WRLCK) ? "WRITE" : "READ ");
24501da177e4SLinus Torvalds 	}
24511da177e4SLinus Torvalds 	if (inode) {
24521da177e4SLinus Torvalds #ifdef WE_CAN_BREAK_LSLK_NOW
2453ab1f1611SVitaliy Gusev 		seq_printf(f, "%d %s:%ld ", fl_pid,
24541da177e4SLinus Torvalds 				inode->i_sb->s_id, inode->i_ino);
24551da177e4SLinus Torvalds #else
24561da177e4SLinus Torvalds 		/* userspace relies on this representation of dev_t ;-( */
2457ab1f1611SVitaliy Gusev 		seq_printf(f, "%d %02x:%02x:%ld ", fl_pid,
24581da177e4SLinus Torvalds 				MAJOR(inode->i_sb->s_dev),
24591da177e4SLinus Torvalds 				MINOR(inode->i_sb->s_dev), inode->i_ino);
24601da177e4SLinus Torvalds #endif
24611da177e4SLinus Torvalds 	} else {
2462ab1f1611SVitaliy Gusev 		seq_printf(f, "%d <none>:0 ", fl_pid);
24631da177e4SLinus Torvalds 	}
24641da177e4SLinus Torvalds 	if (IS_POSIX(fl)) {
24651da177e4SLinus Torvalds 		if (fl->fl_end == OFFSET_MAX)
24667f8ada98SPavel Emelyanov 			seq_printf(f, "%Ld EOF\n", fl->fl_start);
24671da177e4SLinus Torvalds 		else
24687f8ada98SPavel Emelyanov 			seq_printf(f, "%Ld %Ld\n", fl->fl_start, fl->fl_end);
24691da177e4SLinus Torvalds 	} else {
24707f8ada98SPavel Emelyanov 		seq_printf(f, "0 EOF\n");
24711da177e4SLinus Torvalds 	}
24721da177e4SLinus Torvalds }
24731da177e4SLinus Torvalds 
24747f8ada98SPavel Emelyanov static int locks_show(struct seq_file *f, void *v)
24751da177e4SLinus Torvalds {
24767012b02aSJeff Layton 	struct locks_iterator *iter = f->private;
24777f8ada98SPavel Emelyanov 	struct file_lock *fl, *bfl;
24787f8ada98SPavel Emelyanov 
2479139ca04eSJeff Layton 	fl = hlist_entry(v, struct file_lock, fl_link);
24807f8ada98SPavel Emelyanov 
24817012b02aSJeff Layton 	lock_get_status(f, fl, iter->li_pos, "");
24827f8ada98SPavel Emelyanov 
24837f8ada98SPavel Emelyanov 	list_for_each_entry(bfl, &fl->fl_block, fl_block)
24847012b02aSJeff Layton 		lock_get_status(f, bfl, iter->li_pos, " ->");
24857f8ada98SPavel Emelyanov 
24867f8ada98SPavel Emelyanov 	return 0;
24871da177e4SLinus Torvalds }
24881da177e4SLinus Torvalds 
24897f8ada98SPavel Emelyanov static void *locks_start(struct seq_file *f, loff_t *pos)
2490b03dfdecSJeff Layton 	__acquires(&blocked_lock_lock)
24911da177e4SLinus Torvalds {
24927012b02aSJeff Layton 	struct locks_iterator *iter = f->private;
249399dc8292SJerome Marchand 
24947012b02aSJeff Layton 	iter->li_pos = *pos + 1;
24957012b02aSJeff Layton 	lg_global_lock(&file_lock_lglock);
24967b2296afSJeff Layton 	spin_lock(&blocked_lock_lock);
24977012b02aSJeff Layton 	return seq_hlist_start_percpu(&file_lock_list, &iter->li_cpu, *pos);
24981da177e4SLinus Torvalds }
24997f8ada98SPavel Emelyanov 
25007f8ada98SPavel Emelyanov static void *locks_next(struct seq_file *f, void *v, loff_t *pos)
25017f8ada98SPavel Emelyanov {
25027012b02aSJeff Layton 	struct locks_iterator *iter = f->private;
25037012b02aSJeff Layton 
25047012b02aSJeff Layton 	++iter->li_pos;
25057012b02aSJeff Layton 	return seq_hlist_next_percpu(v, &file_lock_list, &iter->li_cpu, pos);
25061da177e4SLinus Torvalds }
25077f8ada98SPavel Emelyanov 
25087f8ada98SPavel Emelyanov static void locks_stop(struct seq_file *f, void *v)
2509b03dfdecSJeff Layton 	__releases(&blocked_lock_lock)
25107f8ada98SPavel Emelyanov {
25117b2296afSJeff Layton 	spin_unlock(&blocked_lock_lock);
25127012b02aSJeff Layton 	lg_global_unlock(&file_lock_lglock);
25131da177e4SLinus Torvalds }
25141da177e4SLinus Torvalds 
2515d8ba7a36SAlexey Dobriyan static const struct seq_operations locks_seq_operations = {
25167f8ada98SPavel Emelyanov 	.start	= locks_start,
25177f8ada98SPavel Emelyanov 	.next	= locks_next,
25187f8ada98SPavel Emelyanov 	.stop	= locks_stop,
25197f8ada98SPavel Emelyanov 	.show	= locks_show,
25207f8ada98SPavel Emelyanov };
2521d8ba7a36SAlexey Dobriyan 
2522d8ba7a36SAlexey Dobriyan static int locks_open(struct inode *inode, struct file *filp)
2523d8ba7a36SAlexey Dobriyan {
25247012b02aSJeff Layton 	return seq_open_private(filp, &locks_seq_operations,
25257012b02aSJeff Layton 					sizeof(struct locks_iterator));
2526d8ba7a36SAlexey Dobriyan }
2527d8ba7a36SAlexey Dobriyan 
2528d8ba7a36SAlexey Dobriyan static const struct file_operations proc_locks_operations = {
2529d8ba7a36SAlexey Dobriyan 	.open		= locks_open,
2530d8ba7a36SAlexey Dobriyan 	.read		= seq_read,
2531d8ba7a36SAlexey Dobriyan 	.llseek		= seq_lseek,
253299dc8292SJerome Marchand 	.release	= seq_release_private,
2533d8ba7a36SAlexey Dobriyan };
2534d8ba7a36SAlexey Dobriyan 
2535d8ba7a36SAlexey Dobriyan static int __init proc_locks_init(void)
2536d8ba7a36SAlexey Dobriyan {
2537d8ba7a36SAlexey Dobriyan 	proc_create("locks", 0, NULL, &proc_locks_operations);
2538d8ba7a36SAlexey Dobriyan 	return 0;
2539d8ba7a36SAlexey Dobriyan }
2540d8ba7a36SAlexey Dobriyan module_init(proc_locks_init);
25417f8ada98SPavel Emelyanov #endif
25427f8ada98SPavel Emelyanov 
25431da177e4SLinus Torvalds /**
25441da177e4SLinus Torvalds  *	lock_may_read - checks that the region is free of locks
25451da177e4SLinus Torvalds  *	@inode: the inode that is being read
25461da177e4SLinus Torvalds  *	@start: the first byte to read
25471da177e4SLinus Torvalds  *	@len: the number of bytes to read
25481da177e4SLinus Torvalds  *
25491da177e4SLinus Torvalds  *	Emulates Windows locking requirements.  Whole-file
25501da177e4SLinus Torvalds  *	mandatory locks (share modes) can prohibit a read and
25511da177e4SLinus Torvalds  *	byte-range POSIX locks can prohibit a read if they overlap.
25521da177e4SLinus Torvalds  *
25531da177e4SLinus Torvalds  *	N.B. this function is only ever called
25541da177e4SLinus Torvalds  *	from knfsd and ownership of locks is never checked.
25551da177e4SLinus Torvalds  */
25561da177e4SLinus Torvalds int lock_may_read(struct inode *inode, loff_t start, unsigned long len)
25571da177e4SLinus Torvalds {
25581da177e4SLinus Torvalds 	struct file_lock *fl;
25591da177e4SLinus Torvalds 	int result = 1;
25601c8c601aSJeff Layton 
25611c8c601aSJeff Layton 	spin_lock(&inode->i_lock);
25621da177e4SLinus Torvalds 	for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
25631da177e4SLinus Torvalds 		if (IS_POSIX(fl)) {
25641da177e4SLinus Torvalds 			if (fl->fl_type == F_RDLCK)
25651da177e4SLinus Torvalds 				continue;
25661da177e4SLinus Torvalds 			if ((fl->fl_end < start) || (fl->fl_start > (start + len)))
25671da177e4SLinus Torvalds 				continue;
25681da177e4SLinus Torvalds 		} else if (IS_FLOCK(fl)) {
25691da177e4SLinus Torvalds 			if (!(fl->fl_type & LOCK_MAND))
25701da177e4SLinus Torvalds 				continue;
25711da177e4SLinus Torvalds 			if (fl->fl_type & LOCK_READ)
25721da177e4SLinus Torvalds 				continue;
25731da177e4SLinus Torvalds 		} else
25741da177e4SLinus Torvalds 			continue;
25751da177e4SLinus Torvalds 		result = 0;
25761da177e4SLinus Torvalds 		break;
25771da177e4SLinus Torvalds 	}
25781c8c601aSJeff Layton 	spin_unlock(&inode->i_lock);
25791da177e4SLinus Torvalds 	return result;
25801da177e4SLinus Torvalds }
25811da177e4SLinus Torvalds 
25821da177e4SLinus Torvalds EXPORT_SYMBOL(lock_may_read);
25831da177e4SLinus Torvalds 
25841da177e4SLinus Torvalds /**
25851da177e4SLinus Torvalds  *	lock_may_write - checks that the region is free of locks
25861da177e4SLinus Torvalds  *	@inode: the inode that is being written
25871da177e4SLinus Torvalds  *	@start: the first byte to write
25881da177e4SLinus Torvalds  *	@len: the number of bytes to write
25891da177e4SLinus Torvalds  *
25901da177e4SLinus Torvalds  *	Emulates Windows locking requirements.  Whole-file
25911da177e4SLinus Torvalds  *	mandatory locks (share modes) can prohibit a write and
25921da177e4SLinus Torvalds  *	byte-range POSIX locks can prohibit a write if they overlap.
25931da177e4SLinus Torvalds  *
25941da177e4SLinus Torvalds  *	N.B. this function is only ever called
25951da177e4SLinus Torvalds  *	from knfsd and ownership of locks is never checked.
25961da177e4SLinus Torvalds  */
25971da177e4SLinus Torvalds int lock_may_write(struct inode *inode, loff_t start, unsigned long len)
25981da177e4SLinus Torvalds {
25991da177e4SLinus Torvalds 	struct file_lock *fl;
26001da177e4SLinus Torvalds 	int result = 1;
26011c8c601aSJeff Layton 
26021c8c601aSJeff Layton 	spin_lock(&inode->i_lock);
26031da177e4SLinus Torvalds 	for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
26041da177e4SLinus Torvalds 		if (IS_POSIX(fl)) {
26051da177e4SLinus Torvalds 			if ((fl->fl_end < start) || (fl->fl_start > (start + len)))
26061da177e4SLinus Torvalds 				continue;
26071da177e4SLinus Torvalds 		} else if (IS_FLOCK(fl)) {
26081da177e4SLinus Torvalds 			if (!(fl->fl_type & LOCK_MAND))
26091da177e4SLinus Torvalds 				continue;
26101da177e4SLinus Torvalds 			if (fl->fl_type & LOCK_WRITE)
26111da177e4SLinus Torvalds 				continue;
26121da177e4SLinus Torvalds 		} else
26131da177e4SLinus Torvalds 			continue;
26141da177e4SLinus Torvalds 		result = 0;
26151da177e4SLinus Torvalds 		break;
26161da177e4SLinus Torvalds 	}
26171c8c601aSJeff Layton 	spin_unlock(&inode->i_lock);
26181da177e4SLinus Torvalds 	return result;
26191da177e4SLinus Torvalds }
26201da177e4SLinus Torvalds 
26211da177e4SLinus Torvalds EXPORT_SYMBOL(lock_may_write);
26221da177e4SLinus Torvalds 
26231da177e4SLinus Torvalds static int __init filelock_init(void)
26241da177e4SLinus Torvalds {
26257012b02aSJeff Layton 	int i;
26267012b02aSJeff Layton 
26271da177e4SLinus Torvalds 	filelock_cache = kmem_cache_create("file_lock_cache",
2628ee19cc40SMiklos Szeredi 			sizeof(struct file_lock), 0, SLAB_PANIC, NULL);
2629ee19cc40SMiklos Szeredi 
26307012b02aSJeff Layton 	lg_lock_init(&file_lock_lglock, "file_lock_lglock");
26317012b02aSJeff Layton 
26327012b02aSJeff Layton 	for_each_possible_cpu(i)
26337012b02aSJeff Layton 		INIT_HLIST_HEAD(per_cpu_ptr(&file_lock_list, i));
26347012b02aSJeff Layton 
26351da177e4SLinus Torvalds 	return 0;
26361da177e4SLinus Torvalds }
26371da177e4SLinus Torvalds 
26381da177e4SLinus Torvalds core_initcall(filelock_init);
2639