xref: /linux/fs/locks.c (revision 0996905f9301c2ff4c021982c42a15b35e74bf1c)
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.
631da177e4SLinus Torvalds  *  See 'Documentation/mandatory.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>
1191da177e4SLinus Torvalds #include <linux/fs.h>
1201da177e4SLinus Torvalds #include <linux/init.h>
1211da177e4SLinus Torvalds #include <linux/module.h>
1221da177e4SLinus Torvalds #include <linux/security.h>
1231da177e4SLinus Torvalds #include <linux/slab.h>
1241da177e4SLinus Torvalds #include <linux/smp_lock.h>
1251da177e4SLinus Torvalds #include <linux/syscalls.h>
1261da177e4SLinus Torvalds #include <linux/time.h>
1274fb3a538SDipankar Sarma #include <linux/rcupdate.h>
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds #include <asm/semaphore.h>
1301da177e4SLinus Torvalds #include <asm/uaccess.h>
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds #define IS_POSIX(fl)	(fl->fl_flags & FL_POSIX)
1331da177e4SLinus Torvalds #define IS_FLOCK(fl)	(fl->fl_flags & FL_FLOCK)
1341da177e4SLinus Torvalds #define IS_LEASE(fl)	(fl->fl_flags & FL_LEASE)
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds int leases_enable = 1;
1371da177e4SLinus Torvalds int lease_break_time = 45;
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds #define for_each_lock(inode, lockp) \
1401da177e4SLinus Torvalds 	for (lockp = &inode->i_flock; *lockp != NULL; lockp = &(*lockp)->fl_next)
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds LIST_HEAD(file_lock_list);
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds EXPORT_SYMBOL(file_lock_list);
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds static LIST_HEAD(blocked_list);
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds static kmem_cache_t *filelock_cache;
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds /* Allocate an empty lock structure. */
1511da177e4SLinus Torvalds static struct file_lock *locks_alloc_lock(void)
1521da177e4SLinus Torvalds {
1531da177e4SLinus Torvalds 	return kmem_cache_alloc(filelock_cache, SLAB_KERNEL);
1541da177e4SLinus Torvalds }
1551da177e4SLinus Torvalds 
15647831f35STrond Myklebust static void locks_release_private(struct file_lock *fl)
15747831f35STrond Myklebust {
15847831f35STrond Myklebust 	if (fl->fl_ops) {
15947831f35STrond Myklebust 		if (fl->fl_ops->fl_release_private)
16047831f35STrond Myklebust 			fl->fl_ops->fl_release_private(fl);
16147831f35STrond Myklebust 		fl->fl_ops = NULL;
16247831f35STrond Myklebust 	}
16347831f35STrond Myklebust 	if (fl->fl_lmops) {
16447831f35STrond Myklebust 		if (fl->fl_lmops->fl_release_private)
16547831f35STrond Myklebust 			fl->fl_lmops->fl_release_private(fl);
16647831f35STrond Myklebust 		fl->fl_lmops = NULL;
16747831f35STrond Myklebust 	}
16847831f35STrond Myklebust 
16947831f35STrond Myklebust }
17047831f35STrond Myklebust 
1711da177e4SLinus Torvalds /* Free a lock which is not in use. */
17233443c42SMatt Mackall static void locks_free_lock(struct file_lock *fl)
1731da177e4SLinus Torvalds {
1741da177e4SLinus Torvalds 	if (fl == NULL) {
1751da177e4SLinus Torvalds 		BUG();
1761da177e4SLinus Torvalds 		return;
1771da177e4SLinus Torvalds 	}
1781da177e4SLinus Torvalds 	if (waitqueue_active(&fl->fl_wait))
1791da177e4SLinus Torvalds 		panic("Attempting to free lock with active wait queue");
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds 	if (!list_empty(&fl->fl_block))
1821da177e4SLinus Torvalds 		panic("Attempting to free lock with active block list");
1831da177e4SLinus Torvalds 
1841da177e4SLinus Torvalds 	if (!list_empty(&fl->fl_link))
1851da177e4SLinus Torvalds 		panic("Attempting to free lock on active lock list");
1861da177e4SLinus Torvalds 
18747831f35STrond Myklebust 	locks_release_private(fl);
1881da177e4SLinus Torvalds 	kmem_cache_free(filelock_cache, fl);
1891da177e4SLinus Torvalds }
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds void locks_init_lock(struct file_lock *fl)
1921da177e4SLinus Torvalds {
1931da177e4SLinus Torvalds 	INIT_LIST_HEAD(&fl->fl_link);
1941da177e4SLinus Torvalds 	INIT_LIST_HEAD(&fl->fl_block);
1951da177e4SLinus Torvalds 	init_waitqueue_head(&fl->fl_wait);
1961da177e4SLinus Torvalds 	fl->fl_next = NULL;
1971da177e4SLinus Torvalds 	fl->fl_fasync = NULL;
1981da177e4SLinus Torvalds 	fl->fl_owner = NULL;
1991da177e4SLinus Torvalds 	fl->fl_pid = 0;
2001da177e4SLinus Torvalds 	fl->fl_file = NULL;
2011da177e4SLinus Torvalds 	fl->fl_flags = 0;
2021da177e4SLinus Torvalds 	fl->fl_type = 0;
2031da177e4SLinus Torvalds 	fl->fl_start = fl->fl_end = 0;
2041da177e4SLinus Torvalds 	fl->fl_ops = NULL;
2051da177e4SLinus Torvalds 	fl->fl_lmops = NULL;
2061da177e4SLinus Torvalds }
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds EXPORT_SYMBOL(locks_init_lock);
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds /*
2111da177e4SLinus Torvalds  * Initialises the fields of the file lock which are invariant for
2121da177e4SLinus Torvalds  * free file_locks.
2131da177e4SLinus Torvalds  */
2141da177e4SLinus Torvalds static void init_once(void *foo, kmem_cache_t *cache, unsigned long flags)
2151da177e4SLinus Torvalds {
2161da177e4SLinus Torvalds 	struct file_lock *lock = (struct file_lock *) foo;
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds 	if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) !=
2191da177e4SLinus Torvalds 					SLAB_CTOR_CONSTRUCTOR)
2201da177e4SLinus Torvalds 		return;
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds 	locks_init_lock(lock);
2231da177e4SLinus Torvalds }
2241da177e4SLinus Torvalds 
22547831f35STrond Myklebust static void locks_copy_private(struct file_lock *new, struct file_lock *fl)
22647831f35STrond Myklebust {
22747831f35STrond Myklebust 	if (fl->fl_ops) {
22847831f35STrond Myklebust 		if (fl->fl_ops->fl_copy_lock)
22947831f35STrond Myklebust 			fl->fl_ops->fl_copy_lock(new, fl);
23047831f35STrond Myklebust 		new->fl_ops = fl->fl_ops;
23147831f35STrond Myklebust 	}
23247831f35STrond Myklebust 	if (fl->fl_lmops) {
23347831f35STrond Myklebust 		if (fl->fl_lmops->fl_copy_lock)
23447831f35STrond Myklebust 			fl->fl_lmops->fl_copy_lock(new, fl);
23547831f35STrond Myklebust 		new->fl_lmops = fl->fl_lmops;
23647831f35STrond Myklebust 	}
23747831f35STrond Myklebust }
23847831f35STrond Myklebust 
2391da177e4SLinus Torvalds /*
2401da177e4SLinus Torvalds  * Initialize a new lock from an existing file_lock structure.
2411da177e4SLinus Torvalds  */
242*0996905fSTrond Myklebust static void __locks_copy_lock(struct file_lock *new, const struct file_lock *fl)
2431da177e4SLinus Torvalds {
2441da177e4SLinus Torvalds 	new->fl_owner = fl->fl_owner;
2451da177e4SLinus Torvalds 	new->fl_pid = fl->fl_pid;
246*0996905fSTrond Myklebust 	new->fl_file = NULL;
2471da177e4SLinus Torvalds 	new->fl_flags = fl->fl_flags;
2481da177e4SLinus Torvalds 	new->fl_type = fl->fl_type;
2491da177e4SLinus Torvalds 	new->fl_start = fl->fl_start;
2501da177e4SLinus Torvalds 	new->fl_end = fl->fl_end;
251*0996905fSTrond Myklebust 	new->fl_ops = NULL;
252*0996905fSTrond Myklebust 	new->fl_lmops = NULL;
253*0996905fSTrond Myklebust }
254*0996905fSTrond Myklebust 
255*0996905fSTrond Myklebust void locks_copy_lock(struct file_lock *new, struct file_lock *fl)
256*0996905fSTrond Myklebust {
257*0996905fSTrond Myklebust 	locks_release_private(new);
258*0996905fSTrond Myklebust 
259*0996905fSTrond Myklebust 	__locks_copy_lock(new, fl);
260*0996905fSTrond Myklebust 	new->fl_file = fl->fl_file;
2611da177e4SLinus Torvalds 	new->fl_ops = fl->fl_ops;
2621da177e4SLinus Torvalds 	new->fl_lmops = fl->fl_lmops;
26347831f35STrond Myklebust 
26447831f35STrond Myklebust 	locks_copy_private(new, fl);
2651da177e4SLinus Torvalds }
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds EXPORT_SYMBOL(locks_copy_lock);
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds static inline int flock_translate_cmd(int cmd) {
2701da177e4SLinus Torvalds 	if (cmd & LOCK_MAND)
2711da177e4SLinus Torvalds 		return cmd & (LOCK_MAND | LOCK_RW);
2721da177e4SLinus Torvalds 	switch (cmd) {
2731da177e4SLinus Torvalds 	case LOCK_SH:
2741da177e4SLinus Torvalds 		return F_RDLCK;
2751da177e4SLinus Torvalds 	case LOCK_EX:
2761da177e4SLinus Torvalds 		return F_WRLCK;
2771da177e4SLinus Torvalds 	case LOCK_UN:
2781da177e4SLinus Torvalds 		return F_UNLCK;
2791da177e4SLinus Torvalds 	}
2801da177e4SLinus Torvalds 	return -EINVAL;
2811da177e4SLinus Torvalds }
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds /* Fill in a file_lock structure with an appropriate FLOCK lock. */
2841da177e4SLinus Torvalds static int flock_make_lock(struct file *filp, struct file_lock **lock,
2851da177e4SLinus Torvalds 		unsigned int cmd)
2861da177e4SLinus Torvalds {
2871da177e4SLinus Torvalds 	struct file_lock *fl;
2881da177e4SLinus Torvalds 	int type = flock_translate_cmd(cmd);
2891da177e4SLinus Torvalds 	if (type < 0)
2901da177e4SLinus Torvalds 		return type;
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds 	fl = locks_alloc_lock();
2931da177e4SLinus Torvalds 	if (fl == NULL)
2941da177e4SLinus Torvalds 		return -ENOMEM;
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 	fl->fl_file = filp;
2971da177e4SLinus Torvalds 	fl->fl_pid = current->tgid;
2981da177e4SLinus Torvalds 	fl->fl_flags = FL_FLOCK;
2991da177e4SLinus Torvalds 	fl->fl_type = type;
3001da177e4SLinus Torvalds 	fl->fl_end = OFFSET_MAX;
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds 	*lock = fl;
3031da177e4SLinus Torvalds 	return 0;
3041da177e4SLinus Torvalds }
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds static int assign_type(struct file_lock *fl, int type)
3071da177e4SLinus Torvalds {
3081da177e4SLinus Torvalds 	switch (type) {
3091da177e4SLinus Torvalds 	case F_RDLCK:
3101da177e4SLinus Torvalds 	case F_WRLCK:
3111da177e4SLinus Torvalds 	case F_UNLCK:
3121da177e4SLinus Torvalds 		fl->fl_type = type;
3131da177e4SLinus Torvalds 		break;
3141da177e4SLinus Torvalds 	default:
3151da177e4SLinus Torvalds 		return -EINVAL;
3161da177e4SLinus Torvalds 	}
3171da177e4SLinus Torvalds 	return 0;
3181da177e4SLinus Torvalds }
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds /* Verify a "struct flock" and copy it to a "struct file_lock" as a POSIX
3211da177e4SLinus Torvalds  * style lock.
3221da177e4SLinus Torvalds  */
3231da177e4SLinus Torvalds static int flock_to_posix_lock(struct file *filp, struct file_lock *fl,
3241da177e4SLinus Torvalds 			       struct flock *l)
3251da177e4SLinus Torvalds {
3261da177e4SLinus Torvalds 	off_t start, end;
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds 	switch (l->l_whence) {
3291da177e4SLinus Torvalds 	case 0: /*SEEK_SET*/
3301da177e4SLinus Torvalds 		start = 0;
3311da177e4SLinus Torvalds 		break;
3321da177e4SLinus Torvalds 	case 1: /*SEEK_CUR*/
3331da177e4SLinus Torvalds 		start = filp->f_pos;
3341da177e4SLinus Torvalds 		break;
3351da177e4SLinus Torvalds 	case 2: /*SEEK_END*/
3361da177e4SLinus Torvalds 		start = i_size_read(filp->f_dentry->d_inode);
3371da177e4SLinus Torvalds 		break;
3381da177e4SLinus Torvalds 	default:
3391da177e4SLinus Torvalds 		return -EINVAL;
3401da177e4SLinus Torvalds 	}
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds 	/* POSIX-1996 leaves the case l->l_len < 0 undefined;
3431da177e4SLinus Torvalds 	   POSIX-2001 defines it. */
3441da177e4SLinus Torvalds 	start += l->l_start;
3451da177e4SLinus Torvalds 	if (start < 0)
3461da177e4SLinus Torvalds 		return -EINVAL;
3471da177e4SLinus Torvalds 	fl->fl_end = OFFSET_MAX;
3484c780a46STrond Myklebust 	if (l->l_len > 0) {
3494c780a46STrond Myklebust 		end = start + l->l_len - 1;
3504c780a46STrond Myklebust 		fl->fl_end = end;
3514c780a46STrond Myklebust 	} else if (l->l_len < 0) {
3524c780a46STrond Myklebust 		end = start - 1;
3534c780a46STrond Myklebust 		fl->fl_end = end;
3544c780a46STrond Myklebust 		start += l->l_len;
3554c780a46STrond Myklebust 		if (start < 0)
3564c780a46STrond Myklebust 			return -EINVAL;
3574c780a46STrond Myklebust 	}
3584c780a46STrond Myklebust 	fl->fl_start = start;	/* we record the absolute position */
3594c780a46STrond Myklebust 	if (fl->fl_end < fl->fl_start)
3604c780a46STrond Myklebust 		return -EOVERFLOW;
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds 	fl->fl_owner = current->files;
3631da177e4SLinus Torvalds 	fl->fl_pid = current->tgid;
3641da177e4SLinus Torvalds 	fl->fl_file = filp;
3651da177e4SLinus Torvalds 	fl->fl_flags = FL_POSIX;
3661da177e4SLinus Torvalds 	fl->fl_ops = NULL;
3671da177e4SLinus Torvalds 	fl->fl_lmops = NULL;
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds 	return assign_type(fl, l->l_type);
3701da177e4SLinus Torvalds }
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds #if BITS_PER_LONG == 32
3731da177e4SLinus Torvalds static int flock64_to_posix_lock(struct file *filp, struct file_lock *fl,
3741da177e4SLinus Torvalds 				 struct flock64 *l)
3751da177e4SLinus Torvalds {
3761da177e4SLinus Torvalds 	loff_t start;
3771da177e4SLinus Torvalds 
3781da177e4SLinus Torvalds 	switch (l->l_whence) {
3791da177e4SLinus Torvalds 	case 0: /*SEEK_SET*/
3801da177e4SLinus Torvalds 		start = 0;
3811da177e4SLinus Torvalds 		break;
3821da177e4SLinus Torvalds 	case 1: /*SEEK_CUR*/
3831da177e4SLinus Torvalds 		start = filp->f_pos;
3841da177e4SLinus Torvalds 		break;
3851da177e4SLinus Torvalds 	case 2: /*SEEK_END*/
3861da177e4SLinus Torvalds 		start = i_size_read(filp->f_dentry->d_inode);
3871da177e4SLinus Torvalds 		break;
3881da177e4SLinus Torvalds 	default:
3891da177e4SLinus Torvalds 		return -EINVAL;
3901da177e4SLinus Torvalds 	}
3911da177e4SLinus Torvalds 
3924c780a46STrond Myklebust 	start += l->l_start;
3934c780a46STrond Myklebust 	if (start < 0)
3941da177e4SLinus Torvalds 		return -EINVAL;
3951da177e4SLinus Torvalds 	fl->fl_end = OFFSET_MAX;
3964c780a46STrond Myklebust 	if (l->l_len > 0) {
3974c780a46STrond Myklebust 		fl->fl_end = start + l->l_len - 1;
3984c780a46STrond Myklebust 	} else if (l->l_len < 0) {
3994c780a46STrond Myklebust 		fl->fl_end = start - 1;
4004c780a46STrond Myklebust 		start += l->l_len;
4014c780a46STrond Myklebust 		if (start < 0)
4024c780a46STrond Myklebust 			return -EINVAL;
4034c780a46STrond Myklebust 	}
4044c780a46STrond Myklebust 	fl->fl_start = start;	/* we record the absolute position */
4054c780a46STrond Myklebust 	if (fl->fl_end < fl->fl_start)
4064c780a46STrond Myklebust 		return -EOVERFLOW;
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds 	fl->fl_owner = current->files;
4091da177e4SLinus Torvalds 	fl->fl_pid = current->tgid;
4101da177e4SLinus Torvalds 	fl->fl_file = filp;
4111da177e4SLinus Torvalds 	fl->fl_flags = FL_POSIX;
4121da177e4SLinus Torvalds 	fl->fl_ops = NULL;
4131da177e4SLinus Torvalds 	fl->fl_lmops = NULL;
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds 	switch (l->l_type) {
4161da177e4SLinus Torvalds 	case F_RDLCK:
4171da177e4SLinus Torvalds 	case F_WRLCK:
4181da177e4SLinus Torvalds 	case F_UNLCK:
4191da177e4SLinus Torvalds 		fl->fl_type = l->l_type;
4201da177e4SLinus Torvalds 		break;
4211da177e4SLinus Torvalds 	default:
4221da177e4SLinus Torvalds 		return -EINVAL;
4231da177e4SLinus Torvalds 	}
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds 	return (0);
4261da177e4SLinus Torvalds }
4271da177e4SLinus Torvalds #endif
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds /* default lease lock manager operations */
4301da177e4SLinus Torvalds static void lease_break_callback(struct file_lock *fl)
4311da177e4SLinus Torvalds {
4321da177e4SLinus Torvalds 	kill_fasync(&fl->fl_fasync, SIGIO, POLL_MSG);
4331da177e4SLinus Torvalds }
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds static void lease_release_private_callback(struct file_lock *fl)
4361da177e4SLinus Torvalds {
4371da177e4SLinus Torvalds 	if (!fl->fl_file)
4381da177e4SLinus Torvalds 		return;
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds 	f_delown(fl->fl_file);
4411da177e4SLinus Torvalds 	fl->fl_file->f_owner.signum = 0;
4421da177e4SLinus Torvalds }
4431da177e4SLinus Torvalds 
44475c96f85SAdrian Bunk static int lease_mylease_callback(struct file_lock *fl, struct file_lock *try)
4451da177e4SLinus Torvalds {
4461da177e4SLinus Torvalds 	return fl->fl_file == try->fl_file;
4471da177e4SLinus Torvalds }
4481da177e4SLinus Torvalds 
44975c96f85SAdrian Bunk static struct lock_manager_operations lease_manager_ops = {
4501da177e4SLinus Torvalds 	.fl_break = lease_break_callback,
4511da177e4SLinus Torvalds 	.fl_release_private = lease_release_private_callback,
4521da177e4SLinus Torvalds 	.fl_mylease = lease_mylease_callback,
4531da177e4SLinus Torvalds 	.fl_change = lease_modify,
4541da177e4SLinus Torvalds };
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds /*
4571da177e4SLinus Torvalds  * Initialize a lease, use the default lock manager operations
4581da177e4SLinus Torvalds  */
4591da177e4SLinus Torvalds static int lease_init(struct file *filp, int type, struct file_lock *fl)
4601da177e4SLinus Torvalds  {
4611da177e4SLinus Torvalds 	fl->fl_owner = current->files;
4621da177e4SLinus Torvalds 	fl->fl_pid = current->tgid;
4631da177e4SLinus Torvalds 
4641da177e4SLinus Torvalds 	fl->fl_file = filp;
4651da177e4SLinus Torvalds 	fl->fl_flags = FL_LEASE;
4661da177e4SLinus Torvalds 	if (assign_type(fl, type) != 0) {
4671da177e4SLinus Torvalds 		locks_free_lock(fl);
4681da177e4SLinus Torvalds 		return -EINVAL;
4691da177e4SLinus Torvalds 	}
4701da177e4SLinus Torvalds 	fl->fl_start = 0;
4711da177e4SLinus Torvalds 	fl->fl_end = OFFSET_MAX;
4721da177e4SLinus Torvalds 	fl->fl_ops = NULL;
4731da177e4SLinus Torvalds 	fl->fl_lmops = &lease_manager_ops;
4741da177e4SLinus Torvalds 	return 0;
4751da177e4SLinus Torvalds }
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds /* Allocate a file_lock initialised to this type of lease */
4781da177e4SLinus Torvalds static int lease_alloc(struct file *filp, int type, struct file_lock **flp)
4791da177e4SLinus Torvalds {
4801da177e4SLinus Torvalds 	struct file_lock *fl = locks_alloc_lock();
4811da177e4SLinus Torvalds 	int error;
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds 	if (fl == NULL)
4841da177e4SLinus Torvalds 		return -ENOMEM;
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds 	error = lease_init(filp, type, fl);
4871da177e4SLinus Torvalds 	if (error)
4881da177e4SLinus Torvalds 		return error;
4891da177e4SLinus Torvalds 	*flp = fl;
4901da177e4SLinus Torvalds 	return 0;
4911da177e4SLinus Torvalds }
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds /* Check if two locks overlap each other.
4941da177e4SLinus Torvalds  */
4951da177e4SLinus Torvalds static inline int locks_overlap(struct file_lock *fl1, struct file_lock *fl2)
4961da177e4SLinus Torvalds {
4971da177e4SLinus Torvalds 	return ((fl1->fl_end >= fl2->fl_start) &&
4981da177e4SLinus Torvalds 		(fl2->fl_end >= fl1->fl_start));
4991da177e4SLinus Torvalds }
5001da177e4SLinus Torvalds 
5011da177e4SLinus Torvalds /*
5021da177e4SLinus Torvalds  * Check whether two locks have the same owner.
5031da177e4SLinus Torvalds  */
50433443c42SMatt Mackall static int posix_same_owner(struct file_lock *fl1, struct file_lock *fl2)
5051da177e4SLinus Torvalds {
5061da177e4SLinus Torvalds 	if (fl1->fl_lmops && fl1->fl_lmops->fl_compare_owner)
5071da177e4SLinus Torvalds 		return fl2->fl_lmops == fl1->fl_lmops &&
5081da177e4SLinus Torvalds 			fl1->fl_lmops->fl_compare_owner(fl1, fl2);
5091da177e4SLinus Torvalds 	return fl1->fl_owner == fl2->fl_owner;
5101da177e4SLinus Torvalds }
5111da177e4SLinus Torvalds 
5121da177e4SLinus Torvalds /* Remove waiter from blocker's block list.
5131da177e4SLinus Torvalds  * When blocker ends up pointing to itself then the list is empty.
5141da177e4SLinus Torvalds  */
51533443c42SMatt Mackall static void __locks_delete_block(struct file_lock *waiter)
5161da177e4SLinus Torvalds {
5171da177e4SLinus Torvalds 	list_del_init(&waiter->fl_block);
5181da177e4SLinus Torvalds 	list_del_init(&waiter->fl_link);
5191da177e4SLinus Torvalds 	waiter->fl_next = NULL;
5201da177e4SLinus Torvalds }
5211da177e4SLinus Torvalds 
5221da177e4SLinus Torvalds /*
5231da177e4SLinus Torvalds  */
5241da177e4SLinus Torvalds static void locks_delete_block(struct file_lock *waiter)
5251da177e4SLinus Torvalds {
5261da177e4SLinus Torvalds 	lock_kernel();
5271da177e4SLinus Torvalds 	__locks_delete_block(waiter);
5281da177e4SLinus Torvalds 	unlock_kernel();
5291da177e4SLinus Torvalds }
5301da177e4SLinus Torvalds 
5311da177e4SLinus Torvalds /* Insert waiter into blocker's block list.
5321da177e4SLinus Torvalds  * We use a circular list so that processes can be easily woken up in
5331da177e4SLinus Torvalds  * the order they blocked. The documentation doesn't require this but
5341da177e4SLinus Torvalds  * it seems like the reasonable thing to do.
5351da177e4SLinus Torvalds  */
5361da177e4SLinus Torvalds static void locks_insert_block(struct file_lock *blocker,
5371da177e4SLinus Torvalds 			       struct file_lock *waiter)
5381da177e4SLinus Torvalds {
5391da177e4SLinus Torvalds 	if (!list_empty(&waiter->fl_block)) {
5401da177e4SLinus Torvalds 		printk(KERN_ERR "locks_insert_block: removing duplicated lock "
5411da177e4SLinus Torvalds 			"(pid=%d %Ld-%Ld type=%d)\n", waiter->fl_pid,
5421da177e4SLinus Torvalds 			waiter->fl_start, waiter->fl_end, waiter->fl_type);
5431da177e4SLinus Torvalds 		__locks_delete_block(waiter);
5441da177e4SLinus Torvalds 	}
5451da177e4SLinus Torvalds 	list_add_tail(&waiter->fl_block, &blocker->fl_block);
5461da177e4SLinus Torvalds 	waiter->fl_next = blocker;
5471da177e4SLinus Torvalds 	if (IS_POSIX(blocker))
5481da177e4SLinus Torvalds 		list_add(&waiter->fl_link, &blocked_list);
5491da177e4SLinus Torvalds }
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds /* Wake up processes blocked waiting for blocker.
5521da177e4SLinus Torvalds  * If told to wait then schedule the processes until the block list
5531da177e4SLinus Torvalds  * is empty, otherwise empty the block list ourselves.
5541da177e4SLinus Torvalds  */
5551da177e4SLinus Torvalds static void locks_wake_up_blocks(struct file_lock *blocker)
5561da177e4SLinus Torvalds {
5571da177e4SLinus Torvalds 	while (!list_empty(&blocker->fl_block)) {
5581da177e4SLinus Torvalds 		struct file_lock *waiter = list_entry(blocker->fl_block.next,
5591da177e4SLinus Torvalds 				struct file_lock, fl_block);
5601da177e4SLinus Torvalds 		__locks_delete_block(waiter);
5611da177e4SLinus Torvalds 		if (waiter->fl_lmops && waiter->fl_lmops->fl_notify)
5621da177e4SLinus Torvalds 			waiter->fl_lmops->fl_notify(waiter);
5631da177e4SLinus Torvalds 		else
5641da177e4SLinus Torvalds 			wake_up(&waiter->fl_wait);
5651da177e4SLinus Torvalds 	}
5661da177e4SLinus Torvalds }
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds /* Insert file lock fl into an inode's lock list at the position indicated
5691da177e4SLinus Torvalds  * by pos. At the same time add the lock to the global file lock list.
5701da177e4SLinus Torvalds  */
5711da177e4SLinus Torvalds static void locks_insert_lock(struct file_lock **pos, struct file_lock *fl)
5721da177e4SLinus Torvalds {
5731da177e4SLinus Torvalds 	list_add(&fl->fl_link, &file_lock_list);
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds 	/* insert into file's list */
5761da177e4SLinus Torvalds 	fl->fl_next = *pos;
5771da177e4SLinus Torvalds 	*pos = fl;
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds 	if (fl->fl_ops && fl->fl_ops->fl_insert)
5801da177e4SLinus Torvalds 		fl->fl_ops->fl_insert(fl);
5811da177e4SLinus Torvalds }
5821da177e4SLinus Torvalds 
5831da177e4SLinus Torvalds /*
5841da177e4SLinus Torvalds  * Delete a lock and then free it.
5851da177e4SLinus Torvalds  * Wake up processes that are blocked waiting for this lock,
5861da177e4SLinus Torvalds  * notify the FS that the lock has been cleared and
5871da177e4SLinus Torvalds  * finally free the lock.
5881da177e4SLinus Torvalds  */
5891da177e4SLinus Torvalds static void locks_delete_lock(struct file_lock **thisfl_p)
5901da177e4SLinus Torvalds {
5911da177e4SLinus Torvalds 	struct file_lock *fl = *thisfl_p;
5921da177e4SLinus Torvalds 
5931da177e4SLinus Torvalds 	*thisfl_p = fl->fl_next;
5941da177e4SLinus Torvalds 	fl->fl_next = NULL;
5951da177e4SLinus Torvalds 	list_del_init(&fl->fl_link);
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds 	fasync_helper(0, fl->fl_file, 0, &fl->fl_fasync);
5981da177e4SLinus Torvalds 	if (fl->fl_fasync != NULL) {
5991da177e4SLinus Torvalds 		printk(KERN_ERR "locks_delete_lock: fasync == %p\n", fl->fl_fasync);
6001da177e4SLinus Torvalds 		fl->fl_fasync = NULL;
6011da177e4SLinus Torvalds 	}
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds 	if (fl->fl_ops && fl->fl_ops->fl_remove)
6041da177e4SLinus Torvalds 		fl->fl_ops->fl_remove(fl);
6051da177e4SLinus Torvalds 
6061da177e4SLinus Torvalds 	locks_wake_up_blocks(fl);
6071da177e4SLinus Torvalds 	locks_free_lock(fl);
6081da177e4SLinus Torvalds }
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. Common functionality
6111da177e4SLinus Torvalds  * checks for shared/exclusive status of overlapping locks.
6121da177e4SLinus Torvalds  */
6131da177e4SLinus Torvalds static int locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
6141da177e4SLinus Torvalds {
6151da177e4SLinus Torvalds 	if (sys_fl->fl_type == F_WRLCK)
6161da177e4SLinus Torvalds 		return 1;
6171da177e4SLinus Torvalds 	if (caller_fl->fl_type == F_WRLCK)
6181da177e4SLinus Torvalds 		return 1;
6191da177e4SLinus Torvalds 	return 0;
6201da177e4SLinus Torvalds }
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. POSIX specific
6231da177e4SLinus Torvalds  * checking before calling the locks_conflict().
6241da177e4SLinus Torvalds  */
6251da177e4SLinus Torvalds static int posix_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
6261da177e4SLinus Torvalds {
6271da177e4SLinus Torvalds 	/* POSIX locks owned by the same process do not conflict with
6281da177e4SLinus Torvalds 	 * each other.
6291da177e4SLinus Torvalds 	 */
6301da177e4SLinus Torvalds 	if (!IS_POSIX(sys_fl) || posix_same_owner(caller_fl, sys_fl))
6311da177e4SLinus Torvalds 		return (0);
6321da177e4SLinus Torvalds 
6331da177e4SLinus Torvalds 	/* Check whether they overlap */
6341da177e4SLinus Torvalds 	if (!locks_overlap(caller_fl, sys_fl))
6351da177e4SLinus Torvalds 		return 0;
6361da177e4SLinus Torvalds 
6371da177e4SLinus Torvalds 	return (locks_conflict(caller_fl, sys_fl));
6381da177e4SLinus Torvalds }
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. FLOCK specific
6411da177e4SLinus Torvalds  * checking before calling the locks_conflict().
6421da177e4SLinus Torvalds  */
6431da177e4SLinus Torvalds static int flock_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
6441da177e4SLinus Torvalds {
6451da177e4SLinus Torvalds 	/* FLOCK locks referring to the same filp do not conflict with
6461da177e4SLinus Torvalds 	 * each other.
6471da177e4SLinus Torvalds 	 */
6481da177e4SLinus Torvalds 	if (!IS_FLOCK(sys_fl) || (caller_fl->fl_file == sys_fl->fl_file))
6491da177e4SLinus Torvalds 		return (0);
6501da177e4SLinus Torvalds 	if ((caller_fl->fl_type & LOCK_MAND) || (sys_fl->fl_type & LOCK_MAND))
6511da177e4SLinus Torvalds 		return 0;
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds 	return (locks_conflict(caller_fl, sys_fl));
6541da177e4SLinus Torvalds }
6551da177e4SLinus Torvalds 
6561da177e4SLinus Torvalds static int interruptible_sleep_on_locked(wait_queue_head_t *fl_wait, int timeout)
6571da177e4SLinus Torvalds {
6581da177e4SLinus Torvalds 	int result = 0;
6591da177e4SLinus Torvalds 	DECLARE_WAITQUEUE(wait, current);
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds 	__set_current_state(TASK_INTERRUPTIBLE);
6621da177e4SLinus Torvalds 	add_wait_queue(fl_wait, &wait);
6631da177e4SLinus Torvalds 	if (timeout == 0)
6641da177e4SLinus Torvalds 		schedule();
6651da177e4SLinus Torvalds 	else
6661da177e4SLinus Torvalds 		result = schedule_timeout(timeout);
6671da177e4SLinus Torvalds 	if (signal_pending(current))
6681da177e4SLinus Torvalds 		result = -ERESTARTSYS;
6691da177e4SLinus Torvalds 	remove_wait_queue(fl_wait, &wait);
6701da177e4SLinus Torvalds 	__set_current_state(TASK_RUNNING);
6711da177e4SLinus Torvalds 	return result;
6721da177e4SLinus Torvalds }
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds static int locks_block_on_timeout(struct file_lock *blocker, struct file_lock *waiter, int time)
6751da177e4SLinus Torvalds {
6761da177e4SLinus Torvalds 	int result;
6771da177e4SLinus Torvalds 	locks_insert_block(blocker, waiter);
6781da177e4SLinus Torvalds 	result = interruptible_sleep_on_locked(&waiter->fl_wait, time);
6791da177e4SLinus Torvalds 	__locks_delete_block(waiter);
6801da177e4SLinus Torvalds 	return result;
6811da177e4SLinus Torvalds }
6821da177e4SLinus Torvalds 
6838dc7c311SAndy Adamson int
6848dc7c311SAndy Adamson posix_test_lock(struct file *filp, struct file_lock *fl,
6858dc7c311SAndy Adamson 		struct file_lock *conflock)
6861da177e4SLinus Torvalds {
6871da177e4SLinus Torvalds 	struct file_lock *cfl;
6881da177e4SLinus Torvalds 
6891da177e4SLinus Torvalds 	lock_kernel();
6901da177e4SLinus Torvalds 	for (cfl = filp->f_dentry->d_inode->i_flock; cfl; cfl = cfl->fl_next) {
6911da177e4SLinus Torvalds 		if (!IS_POSIX(cfl))
6921da177e4SLinus Torvalds 			continue;
6931da177e4SLinus Torvalds 		if (posix_locks_conflict(cfl, fl))
6941da177e4SLinus Torvalds 			break;
6951da177e4SLinus Torvalds 	}
6968dc7c311SAndy Adamson 	if (cfl) {
697*0996905fSTrond Myklebust 		__locks_copy_lock(conflock, cfl);
6981da177e4SLinus Torvalds 		unlock_kernel();
6998dc7c311SAndy Adamson 		return 1;
7008dc7c311SAndy Adamson 	}
7018dc7c311SAndy Adamson 	unlock_kernel();
7028dc7c311SAndy Adamson 	return 0;
7031da177e4SLinus Torvalds }
7041da177e4SLinus Torvalds 
7051da177e4SLinus Torvalds EXPORT_SYMBOL(posix_test_lock);
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds /* This function tests for deadlock condition before putting a process to
7081da177e4SLinus Torvalds  * sleep. The detection scheme is no longer recursive. Recursive was neat,
7091da177e4SLinus Torvalds  * but dangerous - we risked stack corruption if the lock data was bad, or
7101da177e4SLinus Torvalds  * if the recursion was too deep for any other reason.
7111da177e4SLinus Torvalds  *
7121da177e4SLinus Torvalds  * We rely on the fact that a task can only be on one lock's wait queue
7131da177e4SLinus Torvalds  * at a time. When we find blocked_task on a wait queue we can re-search
7141da177e4SLinus Torvalds  * with blocked_task equal to that queue's owner, until either blocked_task
7151da177e4SLinus Torvalds  * isn't found, or blocked_task is found on a queue owned by my_task.
7161da177e4SLinus Torvalds  *
7171da177e4SLinus Torvalds  * Note: the above assumption may not be true when handling lock requests
7181da177e4SLinus Torvalds  * from a broken NFS client. But broken NFS clients have a lot more to
7191da177e4SLinus Torvalds  * worry about than proper deadlock detection anyway... --okir
7201da177e4SLinus Torvalds  */
7211da177e4SLinus Torvalds int posix_locks_deadlock(struct file_lock *caller_fl,
7221da177e4SLinus Torvalds 				struct file_lock *block_fl)
7231da177e4SLinus Torvalds {
7241da177e4SLinus Torvalds 	struct list_head *tmp;
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds next_task:
7271da177e4SLinus Torvalds 	if (posix_same_owner(caller_fl, block_fl))
7281da177e4SLinus Torvalds 		return 1;
7291da177e4SLinus Torvalds 	list_for_each(tmp, &blocked_list) {
7301da177e4SLinus Torvalds 		struct file_lock *fl = list_entry(tmp, struct file_lock, fl_link);
7311da177e4SLinus Torvalds 		if (posix_same_owner(fl, block_fl)) {
7321da177e4SLinus Torvalds 			fl = fl->fl_next;
7331da177e4SLinus Torvalds 			block_fl = fl;
7341da177e4SLinus Torvalds 			goto next_task;
7351da177e4SLinus Torvalds 		}
7361da177e4SLinus Torvalds 	}
7371da177e4SLinus Torvalds 	return 0;
7381da177e4SLinus Torvalds }
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds EXPORT_SYMBOL(posix_locks_deadlock);
7411da177e4SLinus Torvalds 
7421da177e4SLinus Torvalds /* Try to create a FLOCK lock on filp. We always insert new FLOCK locks
7431da177e4SLinus Torvalds  * at the head of the list, but that's secret knowledge known only to
7441da177e4SLinus Torvalds  * flock_lock_file and posix_lock_file.
7451da177e4SLinus Torvalds  */
7461da177e4SLinus Torvalds static int flock_lock_file(struct file *filp, struct file_lock *new_fl)
7471da177e4SLinus Torvalds {
7481da177e4SLinus Torvalds 	struct file_lock **before;
7491da177e4SLinus Torvalds 	struct inode * inode = filp->f_dentry->d_inode;
7501da177e4SLinus Torvalds 	int error = 0;
7511da177e4SLinus Torvalds 	int found = 0;
7521da177e4SLinus Torvalds 
7531da177e4SLinus Torvalds 	lock_kernel();
7541da177e4SLinus Torvalds 	for_each_lock(inode, before) {
7551da177e4SLinus Torvalds 		struct file_lock *fl = *before;
7561da177e4SLinus Torvalds 		if (IS_POSIX(fl))
7571da177e4SLinus Torvalds 			break;
7581da177e4SLinus Torvalds 		if (IS_LEASE(fl))
7591da177e4SLinus Torvalds 			continue;
7601da177e4SLinus Torvalds 		if (filp != fl->fl_file)
7611da177e4SLinus Torvalds 			continue;
7621da177e4SLinus Torvalds 		if (new_fl->fl_type == fl->fl_type)
7631da177e4SLinus Torvalds 			goto out;
7641da177e4SLinus Torvalds 		found = 1;
7651da177e4SLinus Torvalds 		locks_delete_lock(before);
7661da177e4SLinus Torvalds 		break;
7671da177e4SLinus Torvalds 	}
7681da177e4SLinus Torvalds 	unlock_kernel();
7691da177e4SLinus Torvalds 
7701da177e4SLinus Torvalds 	if (new_fl->fl_type == F_UNLCK)
7711da177e4SLinus Torvalds 		return 0;
7721da177e4SLinus Torvalds 
7731da177e4SLinus Torvalds 	/*
7741da177e4SLinus Torvalds 	 * If a higher-priority process was blocked on the old file lock,
7751da177e4SLinus Torvalds 	 * give it the opportunity to lock the file.
7761da177e4SLinus Torvalds 	 */
7771da177e4SLinus Torvalds 	if (found)
7781da177e4SLinus Torvalds 		cond_resched();
7791da177e4SLinus Torvalds 
7801da177e4SLinus Torvalds 	lock_kernel();
7811da177e4SLinus Torvalds 	for_each_lock(inode, before) {
7821da177e4SLinus Torvalds 		struct file_lock *fl = *before;
7831da177e4SLinus Torvalds 		if (IS_POSIX(fl))
7841da177e4SLinus Torvalds 			break;
7851da177e4SLinus Torvalds 		if (IS_LEASE(fl))
7861da177e4SLinus Torvalds 			continue;
7871da177e4SLinus Torvalds 		if (!flock_locks_conflict(new_fl, fl))
7881da177e4SLinus Torvalds 			continue;
7891da177e4SLinus Torvalds 		error = -EAGAIN;
7901da177e4SLinus Torvalds 		if (new_fl->fl_flags & FL_SLEEP) {
7911da177e4SLinus Torvalds 			locks_insert_block(fl, new_fl);
7921da177e4SLinus Torvalds 		}
7931da177e4SLinus Torvalds 		goto out;
7941da177e4SLinus Torvalds 	}
7951da177e4SLinus Torvalds 	locks_insert_lock(&inode->i_flock, new_fl);
7961da177e4SLinus Torvalds 	error = 0;
7971da177e4SLinus Torvalds 
7981da177e4SLinus Torvalds out:
7991da177e4SLinus Torvalds 	unlock_kernel();
8001da177e4SLinus Torvalds 	return error;
8011da177e4SLinus Torvalds }
8021da177e4SLinus Torvalds 
8031da177e4SLinus Torvalds EXPORT_SYMBOL(posix_lock_file);
8041da177e4SLinus Torvalds 
8051da177e4SLinus Torvalds static int __posix_lock_file(struct inode *inode, struct file_lock *request)
8061da177e4SLinus Torvalds {
8071da177e4SLinus Torvalds 	struct file_lock *fl;
8081da177e4SLinus Torvalds 	struct file_lock *new_fl, *new_fl2;
8091da177e4SLinus Torvalds 	struct file_lock *left = NULL;
8101da177e4SLinus Torvalds 	struct file_lock *right = NULL;
8111da177e4SLinus Torvalds 	struct file_lock **before;
8121da177e4SLinus Torvalds 	int error, added = 0;
8131da177e4SLinus Torvalds 
8141da177e4SLinus Torvalds 	/*
8151da177e4SLinus Torvalds 	 * We may need two file_lock structures for this operation,
8161da177e4SLinus Torvalds 	 * so we get them in advance to avoid races.
8171da177e4SLinus Torvalds 	 */
8181da177e4SLinus Torvalds 	new_fl = locks_alloc_lock();
8191da177e4SLinus Torvalds 	new_fl2 = locks_alloc_lock();
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds 	lock_kernel();
8221da177e4SLinus Torvalds 	if (request->fl_type != F_UNLCK) {
8231da177e4SLinus Torvalds 		for_each_lock(inode, before) {
8241da177e4SLinus Torvalds 			struct file_lock *fl = *before;
8251da177e4SLinus Torvalds 			if (!IS_POSIX(fl))
8261da177e4SLinus Torvalds 				continue;
8271da177e4SLinus Torvalds 			if (!posix_locks_conflict(request, fl))
8281da177e4SLinus Torvalds 				continue;
8291da177e4SLinus Torvalds 			error = -EAGAIN;
8301da177e4SLinus Torvalds 			if (!(request->fl_flags & FL_SLEEP))
8311da177e4SLinus Torvalds 				goto out;
8321da177e4SLinus Torvalds 			error = -EDEADLK;
8331da177e4SLinus Torvalds 			if (posix_locks_deadlock(request, fl))
8341da177e4SLinus Torvalds 				goto out;
8351da177e4SLinus Torvalds 			error = -EAGAIN;
8361da177e4SLinus Torvalds 			locks_insert_block(fl, request);
8371da177e4SLinus Torvalds 			goto out;
8381da177e4SLinus Torvalds   		}
8391da177e4SLinus Torvalds   	}
8401da177e4SLinus Torvalds 
8411da177e4SLinus Torvalds 	/* If we're just looking for a conflict, we're done. */
8421da177e4SLinus Torvalds 	error = 0;
8431da177e4SLinus Torvalds 	if (request->fl_flags & FL_ACCESS)
8441da177e4SLinus Torvalds 		goto out;
8451da177e4SLinus Torvalds 
8461da177e4SLinus Torvalds 	error = -ENOLCK; /* "no luck" */
8471da177e4SLinus Torvalds 	if (!(new_fl && new_fl2))
8481da177e4SLinus Torvalds 		goto out;
8491da177e4SLinus Torvalds 
8501da177e4SLinus Torvalds 	/*
8511da177e4SLinus Torvalds 	 * We've allocated the new locks in advance, so there are no
8521da177e4SLinus Torvalds 	 * errors possible (and no blocking operations) from here on.
8531da177e4SLinus Torvalds 	 *
8541da177e4SLinus Torvalds 	 * Find the first old lock with the same owner as the new lock.
8551da177e4SLinus Torvalds 	 */
8561da177e4SLinus Torvalds 
8571da177e4SLinus Torvalds 	before = &inode->i_flock;
8581da177e4SLinus Torvalds 
8591da177e4SLinus Torvalds 	/* First skip locks owned by other processes.  */
8601da177e4SLinus Torvalds 	while ((fl = *before) && (!IS_POSIX(fl) ||
8611da177e4SLinus Torvalds 				  !posix_same_owner(request, fl))) {
8621da177e4SLinus Torvalds 		before = &fl->fl_next;
8631da177e4SLinus Torvalds 	}
8641da177e4SLinus Torvalds 
8651da177e4SLinus Torvalds 	/* Process locks with this owner.  */
8661da177e4SLinus Torvalds 	while ((fl = *before) && posix_same_owner(request, fl)) {
8671da177e4SLinus Torvalds 		/* Detect adjacent or overlapping regions (if same lock type)
8681da177e4SLinus Torvalds 		 */
8691da177e4SLinus Torvalds 		if (request->fl_type == fl->fl_type) {
870449231d6SOlaf Kirch 			/* In all comparisons of start vs end, use
871449231d6SOlaf Kirch 			 * "start - 1" rather than "end + 1". If end
872449231d6SOlaf Kirch 			 * is OFFSET_MAX, end + 1 will become negative.
873449231d6SOlaf Kirch 			 */
8741da177e4SLinus Torvalds 			if (fl->fl_end < request->fl_start - 1)
8751da177e4SLinus Torvalds 				goto next_lock;
8761da177e4SLinus Torvalds 			/* If the next lock in the list has entirely bigger
8771da177e4SLinus Torvalds 			 * addresses than the new one, insert the lock here.
8781da177e4SLinus Torvalds 			 */
879449231d6SOlaf Kirch 			if (fl->fl_start - 1 > request->fl_end)
8801da177e4SLinus Torvalds 				break;
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds 			/* If we come here, the new and old lock are of the
8831da177e4SLinus Torvalds 			 * same type and adjacent or overlapping. Make one
8841da177e4SLinus Torvalds 			 * lock yielding from the lower start address of both
8851da177e4SLinus Torvalds 			 * locks to the higher end address.
8861da177e4SLinus Torvalds 			 */
8871da177e4SLinus Torvalds 			if (fl->fl_start > request->fl_start)
8881da177e4SLinus Torvalds 				fl->fl_start = request->fl_start;
8891da177e4SLinus Torvalds 			else
8901da177e4SLinus Torvalds 				request->fl_start = fl->fl_start;
8911da177e4SLinus Torvalds 			if (fl->fl_end < request->fl_end)
8921da177e4SLinus Torvalds 				fl->fl_end = request->fl_end;
8931da177e4SLinus Torvalds 			else
8941da177e4SLinus Torvalds 				request->fl_end = fl->fl_end;
8951da177e4SLinus Torvalds 			if (added) {
8961da177e4SLinus Torvalds 				locks_delete_lock(before);
8971da177e4SLinus Torvalds 				continue;
8981da177e4SLinus Torvalds 			}
8991da177e4SLinus Torvalds 			request = fl;
9001da177e4SLinus Torvalds 			added = 1;
9011da177e4SLinus Torvalds 		}
9021da177e4SLinus Torvalds 		else {
9031da177e4SLinus Torvalds 			/* Processing for different lock types is a bit
9041da177e4SLinus Torvalds 			 * more complex.
9051da177e4SLinus Torvalds 			 */
9061da177e4SLinus Torvalds 			if (fl->fl_end < request->fl_start)
9071da177e4SLinus Torvalds 				goto next_lock;
9081da177e4SLinus Torvalds 			if (fl->fl_start > request->fl_end)
9091da177e4SLinus Torvalds 				break;
9101da177e4SLinus Torvalds 			if (request->fl_type == F_UNLCK)
9111da177e4SLinus Torvalds 				added = 1;
9121da177e4SLinus Torvalds 			if (fl->fl_start < request->fl_start)
9131da177e4SLinus Torvalds 				left = fl;
9141da177e4SLinus Torvalds 			/* If the next lock in the list has a higher end
9151da177e4SLinus Torvalds 			 * address than the new one, insert the new one here.
9161da177e4SLinus Torvalds 			 */
9171da177e4SLinus Torvalds 			if (fl->fl_end > request->fl_end) {
9181da177e4SLinus Torvalds 				right = fl;
9191da177e4SLinus Torvalds 				break;
9201da177e4SLinus Torvalds 			}
9211da177e4SLinus Torvalds 			if (fl->fl_start >= request->fl_start) {
9221da177e4SLinus Torvalds 				/* The new lock completely replaces an old
9231da177e4SLinus Torvalds 				 * one (This may happen several times).
9241da177e4SLinus Torvalds 				 */
9251da177e4SLinus Torvalds 				if (added) {
9261da177e4SLinus Torvalds 					locks_delete_lock(before);
9271da177e4SLinus Torvalds 					continue;
9281da177e4SLinus Torvalds 				}
9291da177e4SLinus Torvalds 				/* Replace the old lock with the new one.
9301da177e4SLinus Torvalds 				 * Wake up anybody waiting for the old one,
9311da177e4SLinus Torvalds 				 * as the change in lock type might satisfy
9321da177e4SLinus Torvalds 				 * their needs.
9331da177e4SLinus Torvalds 				 */
9341da177e4SLinus Torvalds 				locks_wake_up_blocks(fl);
9351da177e4SLinus Torvalds 				fl->fl_start = request->fl_start;
9361da177e4SLinus Torvalds 				fl->fl_end = request->fl_end;
9371da177e4SLinus Torvalds 				fl->fl_type = request->fl_type;
93847831f35STrond Myklebust 				locks_release_private(fl);
93947831f35STrond Myklebust 				locks_copy_private(fl, request);
9401da177e4SLinus Torvalds 				request = fl;
9411da177e4SLinus Torvalds 				added = 1;
9421da177e4SLinus Torvalds 			}
9431da177e4SLinus Torvalds 		}
9441da177e4SLinus Torvalds 		/* Go on to next lock.
9451da177e4SLinus Torvalds 		 */
9461da177e4SLinus Torvalds 	next_lock:
9471da177e4SLinus Torvalds 		before = &fl->fl_next;
9481da177e4SLinus Torvalds 	}
9491da177e4SLinus Torvalds 
9501da177e4SLinus Torvalds 	error = 0;
9511da177e4SLinus Torvalds 	if (!added) {
9521da177e4SLinus Torvalds 		if (request->fl_type == F_UNLCK)
9531da177e4SLinus Torvalds 			goto out;
9541da177e4SLinus Torvalds 		locks_copy_lock(new_fl, request);
9551da177e4SLinus Torvalds 		locks_insert_lock(before, new_fl);
9561da177e4SLinus Torvalds 		new_fl = NULL;
9571da177e4SLinus Torvalds 	}
9581da177e4SLinus Torvalds 	if (right) {
9591da177e4SLinus Torvalds 		if (left == right) {
9601da177e4SLinus Torvalds 			/* The new lock breaks the old one in two pieces,
9611da177e4SLinus Torvalds 			 * so we have to use the second new lock.
9621da177e4SLinus Torvalds 			 */
9631da177e4SLinus Torvalds 			left = new_fl2;
9641da177e4SLinus Torvalds 			new_fl2 = NULL;
9651da177e4SLinus Torvalds 			locks_copy_lock(left, right);
9661da177e4SLinus Torvalds 			locks_insert_lock(before, left);
9671da177e4SLinus Torvalds 		}
9681da177e4SLinus Torvalds 		right->fl_start = request->fl_end + 1;
9691da177e4SLinus Torvalds 		locks_wake_up_blocks(right);
9701da177e4SLinus Torvalds 	}
9711da177e4SLinus Torvalds 	if (left) {
9721da177e4SLinus Torvalds 		left->fl_end = request->fl_start - 1;
9731da177e4SLinus Torvalds 		locks_wake_up_blocks(left);
9741da177e4SLinus Torvalds 	}
9751da177e4SLinus Torvalds  out:
9761da177e4SLinus Torvalds 	unlock_kernel();
9771da177e4SLinus Torvalds 	/*
9781da177e4SLinus Torvalds 	 * Free any unused locks.
9791da177e4SLinus Torvalds 	 */
9801da177e4SLinus Torvalds 	if (new_fl)
9811da177e4SLinus Torvalds 		locks_free_lock(new_fl);
9821da177e4SLinus Torvalds 	if (new_fl2)
9831da177e4SLinus Torvalds 		locks_free_lock(new_fl2);
9841da177e4SLinus Torvalds 	return error;
9851da177e4SLinus Torvalds }
9861da177e4SLinus Torvalds 
9871da177e4SLinus Torvalds /**
9881da177e4SLinus Torvalds  * posix_lock_file - Apply a POSIX-style lock to a file
9891da177e4SLinus Torvalds  * @filp: The file to apply the lock to
9901da177e4SLinus Torvalds  * @fl: The lock to be applied
9911da177e4SLinus Torvalds  *
9921da177e4SLinus Torvalds  * Add a POSIX style lock to a file.
9931da177e4SLinus Torvalds  * We merge adjacent & overlapping locks whenever possible.
9941da177e4SLinus Torvalds  * POSIX locks are sorted by owner task, then by starting address
9951da177e4SLinus Torvalds  */
9961da177e4SLinus Torvalds int posix_lock_file(struct file *filp, struct file_lock *fl)
9971da177e4SLinus Torvalds {
9981da177e4SLinus Torvalds 	return __posix_lock_file(filp->f_dentry->d_inode, fl);
9991da177e4SLinus Torvalds }
10001da177e4SLinus Torvalds 
10011da177e4SLinus Torvalds /**
10021da177e4SLinus Torvalds  * posix_lock_file_wait - Apply a POSIX-style lock to a file
10031da177e4SLinus Torvalds  * @filp: The file to apply the lock to
10041da177e4SLinus Torvalds  * @fl: The lock to be applied
10051da177e4SLinus Torvalds  *
10061da177e4SLinus Torvalds  * Add a POSIX style lock to a file.
10071da177e4SLinus Torvalds  * We merge adjacent & overlapping locks whenever possible.
10081da177e4SLinus Torvalds  * POSIX locks are sorted by owner task, then by starting address
10091da177e4SLinus Torvalds  */
10101da177e4SLinus Torvalds int posix_lock_file_wait(struct file *filp, struct file_lock *fl)
10111da177e4SLinus Torvalds {
10121da177e4SLinus Torvalds 	int error;
10131da177e4SLinus Torvalds 	might_sleep ();
10141da177e4SLinus Torvalds 	for (;;) {
10151da177e4SLinus Torvalds 		error = __posix_lock_file(filp->f_dentry->d_inode, fl);
10161da177e4SLinus Torvalds 		if ((error != -EAGAIN) || !(fl->fl_flags & FL_SLEEP))
10171da177e4SLinus Torvalds 			break;
10181da177e4SLinus Torvalds 		error = wait_event_interruptible(fl->fl_wait, !fl->fl_next);
10191da177e4SLinus Torvalds 		if (!error)
10201da177e4SLinus Torvalds 			continue;
10211da177e4SLinus Torvalds 
10221da177e4SLinus Torvalds 		locks_delete_block(fl);
10231da177e4SLinus Torvalds 		break;
10241da177e4SLinus Torvalds 	}
10251da177e4SLinus Torvalds 	return error;
10261da177e4SLinus Torvalds }
10271da177e4SLinus Torvalds EXPORT_SYMBOL(posix_lock_file_wait);
10281da177e4SLinus Torvalds 
10291da177e4SLinus Torvalds /**
10301da177e4SLinus Torvalds  * locks_mandatory_locked - Check for an active lock
10311da177e4SLinus Torvalds  * @inode: the file to check
10321da177e4SLinus Torvalds  *
10331da177e4SLinus Torvalds  * Searches the inode's list of locks to find any POSIX locks which conflict.
10341da177e4SLinus Torvalds  * This function is called from locks_verify_locked() only.
10351da177e4SLinus Torvalds  */
10361da177e4SLinus Torvalds int locks_mandatory_locked(struct inode *inode)
10371da177e4SLinus Torvalds {
10381da177e4SLinus Torvalds 	fl_owner_t owner = current->files;
10391da177e4SLinus Torvalds 	struct file_lock *fl;
10401da177e4SLinus Torvalds 
10411da177e4SLinus Torvalds 	/*
10421da177e4SLinus Torvalds 	 * Search the lock list for this inode for any POSIX locks.
10431da177e4SLinus Torvalds 	 */
10441da177e4SLinus Torvalds 	lock_kernel();
10451da177e4SLinus Torvalds 	for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
10461da177e4SLinus Torvalds 		if (!IS_POSIX(fl))
10471da177e4SLinus Torvalds 			continue;
10481da177e4SLinus Torvalds 		if (fl->fl_owner != owner)
10491da177e4SLinus Torvalds 			break;
10501da177e4SLinus Torvalds 	}
10511da177e4SLinus Torvalds 	unlock_kernel();
10521da177e4SLinus Torvalds 	return fl ? -EAGAIN : 0;
10531da177e4SLinus Torvalds }
10541da177e4SLinus Torvalds 
10551da177e4SLinus Torvalds /**
10561da177e4SLinus Torvalds  * locks_mandatory_area - Check for a conflicting lock
10571da177e4SLinus Torvalds  * @read_write: %FLOCK_VERIFY_WRITE for exclusive access, %FLOCK_VERIFY_READ
10581da177e4SLinus Torvalds  *		for shared
10591da177e4SLinus Torvalds  * @inode:      the file to check
10601da177e4SLinus Torvalds  * @filp:       how the file was opened (if it was)
10611da177e4SLinus Torvalds  * @offset:     start of area to check
10621da177e4SLinus Torvalds  * @count:      length of area to check
10631da177e4SLinus Torvalds  *
10641da177e4SLinus Torvalds  * Searches the inode's list of locks to find any POSIX locks which conflict.
10651da177e4SLinus Torvalds  * This function is called from rw_verify_area() and
10661da177e4SLinus Torvalds  * locks_verify_truncate().
10671da177e4SLinus Torvalds  */
10681da177e4SLinus Torvalds int locks_mandatory_area(int read_write, struct inode *inode,
10691da177e4SLinus Torvalds 			 struct file *filp, loff_t offset,
10701da177e4SLinus Torvalds 			 size_t count)
10711da177e4SLinus Torvalds {
10721da177e4SLinus Torvalds 	struct file_lock fl;
10731da177e4SLinus Torvalds 	int error;
10741da177e4SLinus Torvalds 
10751da177e4SLinus Torvalds 	locks_init_lock(&fl);
10761da177e4SLinus Torvalds 	fl.fl_owner = current->files;
10771da177e4SLinus Torvalds 	fl.fl_pid = current->tgid;
10781da177e4SLinus Torvalds 	fl.fl_file = filp;
10791da177e4SLinus Torvalds 	fl.fl_flags = FL_POSIX | FL_ACCESS;
10801da177e4SLinus Torvalds 	if (filp && !(filp->f_flags & O_NONBLOCK))
10811da177e4SLinus Torvalds 		fl.fl_flags |= FL_SLEEP;
10821da177e4SLinus Torvalds 	fl.fl_type = (read_write == FLOCK_VERIFY_WRITE) ? F_WRLCK : F_RDLCK;
10831da177e4SLinus Torvalds 	fl.fl_start = offset;
10841da177e4SLinus Torvalds 	fl.fl_end = offset + count - 1;
10851da177e4SLinus Torvalds 
10861da177e4SLinus Torvalds 	for (;;) {
10871da177e4SLinus Torvalds 		error = __posix_lock_file(inode, &fl);
10881da177e4SLinus Torvalds 		if (error != -EAGAIN)
10891da177e4SLinus Torvalds 			break;
10901da177e4SLinus Torvalds 		if (!(fl.fl_flags & FL_SLEEP))
10911da177e4SLinus Torvalds 			break;
10921da177e4SLinus Torvalds 		error = wait_event_interruptible(fl.fl_wait, !fl.fl_next);
10931da177e4SLinus Torvalds 		if (!error) {
10941da177e4SLinus Torvalds 			/*
10951da177e4SLinus Torvalds 			 * If we've been sleeping someone might have
10961da177e4SLinus Torvalds 			 * changed the permissions behind our back.
10971da177e4SLinus Torvalds 			 */
10981da177e4SLinus Torvalds 			if ((inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
10991da177e4SLinus Torvalds 				continue;
11001da177e4SLinus Torvalds 		}
11011da177e4SLinus Torvalds 
11021da177e4SLinus Torvalds 		locks_delete_block(&fl);
11031da177e4SLinus Torvalds 		break;
11041da177e4SLinus Torvalds 	}
11051da177e4SLinus Torvalds 
11061da177e4SLinus Torvalds 	return error;
11071da177e4SLinus Torvalds }
11081da177e4SLinus Torvalds 
11091da177e4SLinus Torvalds EXPORT_SYMBOL(locks_mandatory_area);
11101da177e4SLinus Torvalds 
11111da177e4SLinus Torvalds /* We already had a lease on this file; just change its type */
11121da177e4SLinus Torvalds int lease_modify(struct file_lock **before, int arg)
11131da177e4SLinus Torvalds {
11141da177e4SLinus Torvalds 	struct file_lock *fl = *before;
11151da177e4SLinus Torvalds 	int error = assign_type(fl, arg);
11161da177e4SLinus Torvalds 
11171da177e4SLinus Torvalds 	if (error)
11181da177e4SLinus Torvalds 		return error;
11191da177e4SLinus Torvalds 	locks_wake_up_blocks(fl);
11201da177e4SLinus Torvalds 	if (arg == F_UNLCK)
11211da177e4SLinus Torvalds 		locks_delete_lock(before);
11221da177e4SLinus Torvalds 	return 0;
11231da177e4SLinus Torvalds }
11241da177e4SLinus Torvalds 
11251da177e4SLinus Torvalds EXPORT_SYMBOL(lease_modify);
11261da177e4SLinus Torvalds 
11271da177e4SLinus Torvalds static void time_out_leases(struct inode *inode)
11281da177e4SLinus Torvalds {
11291da177e4SLinus Torvalds 	struct file_lock **before;
11301da177e4SLinus Torvalds 	struct file_lock *fl;
11311da177e4SLinus Torvalds 
11321da177e4SLinus Torvalds 	before = &inode->i_flock;
11331da177e4SLinus Torvalds 	while ((fl = *before) && IS_LEASE(fl) && (fl->fl_type & F_INPROGRESS)) {
11341da177e4SLinus Torvalds 		if ((fl->fl_break_time == 0)
11351da177e4SLinus Torvalds 				|| time_before(jiffies, fl->fl_break_time)) {
11361da177e4SLinus Torvalds 			before = &fl->fl_next;
11371da177e4SLinus Torvalds 			continue;
11381da177e4SLinus Torvalds 		}
11391da177e4SLinus Torvalds 		lease_modify(before, fl->fl_type & ~F_INPROGRESS);
11401da177e4SLinus Torvalds 		if (fl == *before)	/* lease_modify may have freed fl */
11411da177e4SLinus Torvalds 			before = &fl->fl_next;
11421da177e4SLinus Torvalds 	}
11431da177e4SLinus Torvalds }
11441da177e4SLinus Torvalds 
11451da177e4SLinus Torvalds /**
11461da177e4SLinus Torvalds  *	__break_lease	-	revoke all outstanding leases on file
11471da177e4SLinus Torvalds  *	@inode: the inode of the file to return
11481da177e4SLinus Torvalds  *	@mode: the open mode (read or write)
11491da177e4SLinus Torvalds  *
11501da177e4SLinus Torvalds  *	break_lease (inlined for speed) has checked there already
11511da177e4SLinus Torvalds  *	is a lease on this file.  Leases are broken on a call to open()
11521da177e4SLinus Torvalds  *	or truncate().  This function can sleep unless you
11531da177e4SLinus Torvalds  *	specified %O_NONBLOCK to your open().
11541da177e4SLinus Torvalds  */
11551da177e4SLinus Torvalds int __break_lease(struct inode *inode, unsigned int mode)
11561da177e4SLinus Torvalds {
11571da177e4SLinus Torvalds 	int error = 0, future;
11581da177e4SLinus Torvalds 	struct file_lock *new_fl, *flock;
11591da177e4SLinus Torvalds 	struct file_lock *fl;
11601da177e4SLinus Torvalds 	int alloc_err;
11611da177e4SLinus Torvalds 	unsigned long break_time;
11621da177e4SLinus Torvalds 	int i_have_this_lease = 0;
11631da177e4SLinus Torvalds 
11641da177e4SLinus Torvalds 	alloc_err = lease_alloc(NULL, mode & FMODE_WRITE ? F_WRLCK : F_RDLCK,
11651da177e4SLinus Torvalds 			&new_fl);
11661da177e4SLinus Torvalds 
11671da177e4SLinus Torvalds 	lock_kernel();
11681da177e4SLinus Torvalds 
11691da177e4SLinus Torvalds 	time_out_leases(inode);
11701da177e4SLinus Torvalds 
11711da177e4SLinus Torvalds 	flock = inode->i_flock;
11721da177e4SLinus Torvalds 	if ((flock == NULL) || !IS_LEASE(flock))
11731da177e4SLinus Torvalds 		goto out;
11741da177e4SLinus Torvalds 
11751da177e4SLinus Torvalds 	for (fl = flock; fl && IS_LEASE(fl); fl = fl->fl_next)
11761da177e4SLinus Torvalds 		if (fl->fl_owner == current->files)
11771da177e4SLinus Torvalds 			i_have_this_lease = 1;
11781da177e4SLinus Torvalds 
11791da177e4SLinus Torvalds 	if (mode & FMODE_WRITE) {
11801da177e4SLinus Torvalds 		/* If we want write access, we have to revoke any lease. */
11811da177e4SLinus Torvalds 		future = F_UNLCK | F_INPROGRESS;
11821da177e4SLinus Torvalds 	} else if (flock->fl_type & F_INPROGRESS) {
11831da177e4SLinus Torvalds 		/* If the lease is already being broken, we just leave it */
11841da177e4SLinus Torvalds 		future = flock->fl_type;
11851da177e4SLinus Torvalds 	} else if (flock->fl_type & F_WRLCK) {
11861da177e4SLinus Torvalds 		/* Downgrade the exclusive lease to a read-only lease. */
11871da177e4SLinus Torvalds 		future = F_RDLCK | F_INPROGRESS;
11881da177e4SLinus Torvalds 	} else {
11891da177e4SLinus Torvalds 		/* the existing lease was read-only, so we can read too. */
11901da177e4SLinus Torvalds 		goto out;
11911da177e4SLinus Torvalds 	}
11921da177e4SLinus Torvalds 
11931da177e4SLinus Torvalds 	if (alloc_err && !i_have_this_lease && ((mode & O_NONBLOCK) == 0)) {
11941da177e4SLinus Torvalds 		error = alloc_err;
11951da177e4SLinus Torvalds 		goto out;
11961da177e4SLinus Torvalds 	}
11971da177e4SLinus Torvalds 
11981da177e4SLinus Torvalds 	break_time = 0;
11991da177e4SLinus Torvalds 	if (lease_break_time > 0) {
12001da177e4SLinus Torvalds 		break_time = jiffies + lease_break_time * HZ;
12011da177e4SLinus Torvalds 		if (break_time == 0)
12021da177e4SLinus Torvalds 			break_time++;	/* so that 0 means no break time */
12031da177e4SLinus Torvalds 	}
12041da177e4SLinus Torvalds 
12051da177e4SLinus Torvalds 	for (fl = flock; fl && IS_LEASE(fl); fl = fl->fl_next) {
12061da177e4SLinus Torvalds 		if (fl->fl_type != future) {
12071da177e4SLinus Torvalds 			fl->fl_type = future;
12081da177e4SLinus Torvalds 			fl->fl_break_time = break_time;
12091da177e4SLinus Torvalds 			/* lease must have lmops break callback */
12101da177e4SLinus Torvalds 			fl->fl_lmops->fl_break(fl);
12111da177e4SLinus Torvalds 		}
12121da177e4SLinus Torvalds 	}
12131da177e4SLinus Torvalds 
12141da177e4SLinus Torvalds 	if (i_have_this_lease || (mode & O_NONBLOCK)) {
12151da177e4SLinus Torvalds 		error = -EWOULDBLOCK;
12161da177e4SLinus Torvalds 		goto out;
12171da177e4SLinus Torvalds 	}
12181da177e4SLinus Torvalds 
12191da177e4SLinus Torvalds restart:
12201da177e4SLinus Torvalds 	break_time = flock->fl_break_time;
12211da177e4SLinus Torvalds 	if (break_time != 0) {
12221da177e4SLinus Torvalds 		break_time -= jiffies;
12231da177e4SLinus Torvalds 		if (break_time == 0)
12241da177e4SLinus Torvalds 			break_time++;
12251da177e4SLinus Torvalds 	}
12261da177e4SLinus Torvalds 	error = locks_block_on_timeout(flock, new_fl, break_time);
12271da177e4SLinus Torvalds 	if (error >= 0) {
12281da177e4SLinus Torvalds 		if (error == 0)
12291da177e4SLinus Torvalds 			time_out_leases(inode);
12301da177e4SLinus Torvalds 		/* Wait for the next lease that has not been broken yet */
12311da177e4SLinus Torvalds 		for (flock = inode->i_flock; flock && IS_LEASE(flock);
12321da177e4SLinus Torvalds 				flock = flock->fl_next) {
12331da177e4SLinus Torvalds 			if (flock->fl_type & F_INPROGRESS)
12341da177e4SLinus Torvalds 				goto restart;
12351da177e4SLinus Torvalds 		}
12361da177e4SLinus Torvalds 		error = 0;
12371da177e4SLinus Torvalds 	}
12381da177e4SLinus Torvalds 
12391da177e4SLinus Torvalds out:
12401da177e4SLinus Torvalds 	unlock_kernel();
12411da177e4SLinus Torvalds 	if (!alloc_err)
12421da177e4SLinus Torvalds 		locks_free_lock(new_fl);
12431da177e4SLinus Torvalds 	return error;
12441da177e4SLinus Torvalds }
12451da177e4SLinus Torvalds 
12461da177e4SLinus Torvalds EXPORT_SYMBOL(__break_lease);
12471da177e4SLinus Torvalds 
12481da177e4SLinus Torvalds /**
12491da177e4SLinus Torvalds  *	lease_get_mtime
12501da177e4SLinus Torvalds  *	@inode: the inode
12511da177e4SLinus Torvalds  *      @time:  pointer to a timespec which will contain the last modified time
12521da177e4SLinus Torvalds  *
12531da177e4SLinus Torvalds  * This is to force NFS clients to flush their caches for files with
12541da177e4SLinus Torvalds  * exclusive leases.  The justification is that if someone has an
12551da177e4SLinus Torvalds  * exclusive lease, then they could be modifiying it.
12561da177e4SLinus Torvalds  */
12571da177e4SLinus Torvalds void lease_get_mtime(struct inode *inode, struct timespec *time)
12581da177e4SLinus Torvalds {
12591da177e4SLinus Torvalds 	struct file_lock *flock = inode->i_flock;
12601da177e4SLinus Torvalds 	if (flock && IS_LEASE(flock) && (flock->fl_type & F_WRLCK))
12611da177e4SLinus Torvalds 		*time = current_fs_time(inode->i_sb);
12621da177e4SLinus Torvalds 	else
12631da177e4SLinus Torvalds 		*time = inode->i_mtime;
12641da177e4SLinus Torvalds }
12651da177e4SLinus Torvalds 
12661da177e4SLinus Torvalds EXPORT_SYMBOL(lease_get_mtime);
12671da177e4SLinus Torvalds 
12681da177e4SLinus Torvalds /**
12691da177e4SLinus Torvalds  *	fcntl_getlease - Enquire what lease is currently active
12701da177e4SLinus Torvalds  *	@filp: the file
12711da177e4SLinus Torvalds  *
12721da177e4SLinus Torvalds  *	The value returned by this function will be one of
12731da177e4SLinus Torvalds  *	(if no lease break is pending):
12741da177e4SLinus Torvalds  *
12751da177e4SLinus Torvalds  *	%F_RDLCK to indicate a shared lease is held.
12761da177e4SLinus Torvalds  *
12771da177e4SLinus Torvalds  *	%F_WRLCK to indicate an exclusive lease is held.
12781da177e4SLinus Torvalds  *
12791da177e4SLinus Torvalds  *	%F_UNLCK to indicate no lease is held.
12801da177e4SLinus Torvalds  *
12811da177e4SLinus Torvalds  *	(if a lease break is pending):
12821da177e4SLinus Torvalds  *
12831da177e4SLinus Torvalds  *	%F_RDLCK to indicate an exclusive lease needs to be
12841da177e4SLinus Torvalds  *		changed to a shared lease (or removed).
12851da177e4SLinus Torvalds  *
12861da177e4SLinus Torvalds  *	%F_UNLCK to indicate the lease needs to be removed.
12871da177e4SLinus Torvalds  *
12881da177e4SLinus Torvalds  *	XXX: sfr & willy disagree over whether F_INPROGRESS
12891da177e4SLinus Torvalds  *	should be returned to userspace.
12901da177e4SLinus Torvalds  */
12911da177e4SLinus Torvalds int fcntl_getlease(struct file *filp)
12921da177e4SLinus Torvalds {
12931da177e4SLinus Torvalds 	struct file_lock *fl;
12941da177e4SLinus Torvalds 	int type = F_UNLCK;
12951da177e4SLinus Torvalds 
12961da177e4SLinus Torvalds 	lock_kernel();
12971da177e4SLinus Torvalds 	time_out_leases(filp->f_dentry->d_inode);
12981da177e4SLinus Torvalds 	for (fl = filp->f_dentry->d_inode->i_flock; fl && IS_LEASE(fl);
12991da177e4SLinus Torvalds 			fl = fl->fl_next) {
13001da177e4SLinus Torvalds 		if (fl->fl_file == filp) {
13011da177e4SLinus Torvalds 			type = fl->fl_type & ~F_INPROGRESS;
13021da177e4SLinus Torvalds 			break;
13031da177e4SLinus Torvalds 		}
13041da177e4SLinus Torvalds 	}
13051da177e4SLinus Torvalds 	unlock_kernel();
13061da177e4SLinus Torvalds 	return type;
13071da177e4SLinus Torvalds }
13081da177e4SLinus Torvalds 
13091da177e4SLinus Torvalds /**
13101da177e4SLinus Torvalds  *	__setlease	-	sets a lease on an open file
13111da177e4SLinus Torvalds  *	@filp: file pointer
13121da177e4SLinus Torvalds  *	@arg: type of lease to obtain
13131da177e4SLinus Torvalds  *	@flp: input - file_lock to use, output - file_lock inserted
13141da177e4SLinus Torvalds  *
13151da177e4SLinus Torvalds  *	The (input) flp->fl_lmops->fl_break function is required
13161da177e4SLinus Torvalds  *	by break_lease().
13171da177e4SLinus Torvalds  *
13181da177e4SLinus Torvalds  *	Called with kernel lock held.
13191da177e4SLinus Torvalds  */
132075c96f85SAdrian Bunk static int __setlease(struct file *filp, long arg, struct file_lock **flp)
13211da177e4SLinus Torvalds {
13227eaae282SKAMBAROV, ZAUR 	struct file_lock *fl, **before, **my_before = NULL, *lease;
13231da177e4SLinus Torvalds 	struct dentry *dentry = filp->f_dentry;
13241da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
13251da177e4SLinus Torvalds 	int error, rdlease_count = 0, wrlease_count = 0;
13261da177e4SLinus Torvalds 
13271da177e4SLinus Torvalds 	time_out_leases(inode);
13281da177e4SLinus Torvalds 
13291da177e4SLinus Torvalds 	error = -EINVAL;
13301da177e4SLinus Torvalds 	if (!flp || !(*flp) || !(*flp)->fl_lmops || !(*flp)->fl_lmops->fl_break)
13311da177e4SLinus Torvalds 		goto out;
13321da177e4SLinus Torvalds 
13337eaae282SKAMBAROV, ZAUR 	lease = *flp;
13347eaae282SKAMBAROV, ZAUR 
13351da177e4SLinus Torvalds 	error = -EAGAIN;
13361da177e4SLinus Torvalds 	if ((arg == F_RDLCK) && (atomic_read(&inode->i_writecount) > 0))
13371da177e4SLinus Torvalds 		goto out;
13381da177e4SLinus Torvalds 	if ((arg == F_WRLCK)
13391da177e4SLinus Torvalds 	    && ((atomic_read(&dentry->d_count) > 1)
13401da177e4SLinus Torvalds 		|| (atomic_read(&inode->i_count) > 1)))
13411da177e4SLinus Torvalds 		goto out;
13421da177e4SLinus Torvalds 
13431da177e4SLinus Torvalds 	/*
13441da177e4SLinus Torvalds 	 * At this point, we know that if there is an exclusive
13451da177e4SLinus Torvalds 	 * lease on this file, then we hold it on this filp
13461da177e4SLinus Torvalds 	 * (otherwise our open of this file would have blocked).
13471da177e4SLinus Torvalds 	 * And if we are trying to acquire an exclusive lease,
13481da177e4SLinus Torvalds 	 * then the file is not open by anyone (including us)
13491da177e4SLinus Torvalds 	 * except for this filp.
13501da177e4SLinus Torvalds 	 */
13511da177e4SLinus Torvalds 	for (before = &inode->i_flock;
13521da177e4SLinus Torvalds 			((fl = *before) != NULL) && IS_LEASE(fl);
13531da177e4SLinus Torvalds 			before = &fl->fl_next) {
13541da177e4SLinus Torvalds 		if (lease->fl_lmops->fl_mylease(fl, lease))
13551da177e4SLinus Torvalds 			my_before = before;
13561da177e4SLinus Torvalds 		else if (fl->fl_type == (F_INPROGRESS | F_UNLCK))
13571da177e4SLinus Torvalds 			/*
13581da177e4SLinus Torvalds 			 * Someone is in the process of opening this
13591da177e4SLinus Torvalds 			 * file for writing so we may not take an
13601da177e4SLinus Torvalds 			 * exclusive lease on it.
13611da177e4SLinus Torvalds 			 */
13621da177e4SLinus Torvalds 			wrlease_count++;
13631da177e4SLinus Torvalds 		else
13641da177e4SLinus Torvalds 			rdlease_count++;
13651da177e4SLinus Torvalds 	}
13661da177e4SLinus Torvalds 
13671da177e4SLinus Torvalds 	if ((arg == F_RDLCK && (wrlease_count > 0)) ||
13681da177e4SLinus Torvalds 	    (arg == F_WRLCK && ((rdlease_count + wrlease_count) > 0)))
13691da177e4SLinus Torvalds 		goto out;
13701da177e4SLinus Torvalds 
13711da177e4SLinus Torvalds 	if (my_before != NULL) {
13721da177e4SLinus Torvalds 		error = lease->fl_lmops->fl_change(my_before, arg);
13731da177e4SLinus Torvalds 		goto out;
13741da177e4SLinus Torvalds 	}
13751da177e4SLinus Torvalds 
13761da177e4SLinus Torvalds 	error = 0;
13771da177e4SLinus Torvalds 	if (arg == F_UNLCK)
13781da177e4SLinus Torvalds 		goto out;
13791da177e4SLinus Torvalds 
13801da177e4SLinus Torvalds 	error = -EINVAL;
13811da177e4SLinus Torvalds 	if (!leases_enable)
13821da177e4SLinus Torvalds 		goto out;
13831da177e4SLinus Torvalds 
13841da177e4SLinus Torvalds 	error = lease_alloc(filp, arg, &fl);
13851da177e4SLinus Torvalds 	if (error)
13861da177e4SLinus Torvalds 		goto out;
13871da177e4SLinus Torvalds 
13881da177e4SLinus Torvalds 	locks_copy_lock(fl, lease);
13891da177e4SLinus Torvalds 
13901da177e4SLinus Torvalds 	locks_insert_lock(before, fl);
13911da177e4SLinus Torvalds 
13921da177e4SLinus Torvalds 	*flp = fl;
13931da177e4SLinus Torvalds out:
13941da177e4SLinus Torvalds 	return error;
13951da177e4SLinus Torvalds }
13961da177e4SLinus Torvalds 
13971da177e4SLinus Torvalds  /**
13981da177e4SLinus Torvalds  *	setlease        -       sets a lease on an open file
13991da177e4SLinus Torvalds  *	@filp: file pointer
14001da177e4SLinus Torvalds  *	@arg: type of lease to obtain
14011da177e4SLinus Torvalds  *	@lease: file_lock to use
14021da177e4SLinus Torvalds  *
14031da177e4SLinus Torvalds  *	Call this to establish a lease on the file.
14041da177e4SLinus Torvalds  *	The fl_lmops fl_break function is required by break_lease
14051da177e4SLinus Torvalds  */
14061da177e4SLinus Torvalds 
14071da177e4SLinus Torvalds int setlease(struct file *filp, long arg, struct file_lock **lease)
14081da177e4SLinus Torvalds {
14091da177e4SLinus Torvalds 	struct dentry *dentry = filp->f_dentry;
14101da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
14111da177e4SLinus Torvalds 	int error;
14121da177e4SLinus Torvalds 
14131da177e4SLinus Torvalds 	if ((current->fsuid != inode->i_uid) && !capable(CAP_LEASE))
14141da177e4SLinus Torvalds 		return -EACCES;
14151da177e4SLinus Torvalds 	if (!S_ISREG(inode->i_mode))
14161da177e4SLinus Torvalds 		return -EINVAL;
14171da177e4SLinus Torvalds 	error = security_file_lock(filp, arg);
14181da177e4SLinus Torvalds 	if (error)
14191da177e4SLinus Torvalds 		return error;
14201da177e4SLinus Torvalds 
14211da177e4SLinus Torvalds 	lock_kernel();
14221da177e4SLinus Torvalds 	error = __setlease(filp, arg, lease);
14231da177e4SLinus Torvalds 	unlock_kernel();
14241da177e4SLinus Torvalds 
14251da177e4SLinus Torvalds 	return error;
14261da177e4SLinus Torvalds }
14271da177e4SLinus Torvalds 
14281da177e4SLinus Torvalds EXPORT_SYMBOL(setlease);
14291da177e4SLinus Torvalds 
14301da177e4SLinus Torvalds /**
14311da177e4SLinus Torvalds  *	fcntl_setlease	-	sets a lease on an open file
14321da177e4SLinus Torvalds  *	@fd: open file descriptor
14331da177e4SLinus Torvalds  *	@filp: file pointer
14341da177e4SLinus Torvalds  *	@arg: type of lease to obtain
14351da177e4SLinus Torvalds  *
14361da177e4SLinus Torvalds  *	Call this fcntl to establish a lease on the file.
14371da177e4SLinus Torvalds  *	Note that you also need to call %F_SETSIG to
14381da177e4SLinus Torvalds  *	receive a signal when the lease is broken.
14391da177e4SLinus Torvalds  */
14401da177e4SLinus Torvalds int fcntl_setlease(unsigned int fd, struct file *filp, long arg)
14411da177e4SLinus Torvalds {
14421da177e4SLinus Torvalds 	struct file_lock fl, *flp = &fl;
14431da177e4SLinus Torvalds 	struct dentry *dentry = filp->f_dentry;
14441da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
14451da177e4SLinus Torvalds 	int error;
14461da177e4SLinus Torvalds 
14471da177e4SLinus Torvalds 	if ((current->fsuid != inode->i_uid) && !capable(CAP_LEASE))
14481da177e4SLinus Torvalds 		return -EACCES;
14491da177e4SLinus Torvalds 	if (!S_ISREG(inode->i_mode))
14501da177e4SLinus Torvalds 		return -EINVAL;
14511da177e4SLinus Torvalds 	error = security_file_lock(filp, arg);
14521da177e4SLinus Torvalds 	if (error)
14531da177e4SLinus Torvalds 		return error;
14541da177e4SLinus Torvalds 
14551da177e4SLinus Torvalds 	locks_init_lock(&fl);
14561da177e4SLinus Torvalds 	error = lease_init(filp, arg, &fl);
14571da177e4SLinus Torvalds 	if (error)
14581da177e4SLinus Torvalds 		return error;
14591da177e4SLinus Torvalds 
14601da177e4SLinus Torvalds 	lock_kernel();
14611da177e4SLinus Torvalds 
14621da177e4SLinus Torvalds 	error = __setlease(filp, arg, &flp);
1463dc15ae14SJ. Bruce Fields 	if (error || arg == F_UNLCK)
14641da177e4SLinus Torvalds 		goto out_unlock;
14651da177e4SLinus Torvalds 
14661da177e4SLinus Torvalds 	error = fasync_helper(fd, filp, 1, &flp->fl_fasync);
14671da177e4SLinus Torvalds 	if (error < 0) {
14681da177e4SLinus Torvalds 		/* remove lease just inserted by __setlease */
14691da177e4SLinus Torvalds 		flp->fl_type = F_UNLCK | F_INPROGRESS;
14701da177e4SLinus Torvalds 		flp->fl_break_time = jiffies- 10;
14711da177e4SLinus Torvalds 		time_out_leases(inode);
14721da177e4SLinus Torvalds 		goto out_unlock;
14731da177e4SLinus Torvalds 	}
14741da177e4SLinus Torvalds 
14751da177e4SLinus Torvalds 	error = f_setown(filp, current->pid, 0);
14761da177e4SLinus Torvalds out_unlock:
14771da177e4SLinus Torvalds 	unlock_kernel();
14781da177e4SLinus Torvalds 	return error;
14791da177e4SLinus Torvalds }
14801da177e4SLinus Torvalds 
14811da177e4SLinus Torvalds /**
14821da177e4SLinus Torvalds  * flock_lock_file_wait - Apply a FLOCK-style lock to a file
14831da177e4SLinus Torvalds  * @filp: The file to apply the lock to
14841da177e4SLinus Torvalds  * @fl: The lock to be applied
14851da177e4SLinus Torvalds  *
14861da177e4SLinus Torvalds  * Add a FLOCK style lock to a file.
14871da177e4SLinus Torvalds  */
14881da177e4SLinus Torvalds int flock_lock_file_wait(struct file *filp, struct file_lock *fl)
14891da177e4SLinus Torvalds {
14901da177e4SLinus Torvalds 	int error;
14911da177e4SLinus Torvalds 	might_sleep();
14921da177e4SLinus Torvalds 	for (;;) {
14931da177e4SLinus Torvalds 		error = flock_lock_file(filp, fl);
14941da177e4SLinus Torvalds 		if ((error != -EAGAIN) || !(fl->fl_flags & FL_SLEEP))
14951da177e4SLinus Torvalds 			break;
14961da177e4SLinus Torvalds 		error = wait_event_interruptible(fl->fl_wait, !fl->fl_next);
14971da177e4SLinus Torvalds 		if (!error)
14981da177e4SLinus Torvalds 			continue;
14991da177e4SLinus Torvalds 
15001da177e4SLinus Torvalds 		locks_delete_block(fl);
15011da177e4SLinus Torvalds 		break;
15021da177e4SLinus Torvalds 	}
15031da177e4SLinus Torvalds 	return error;
15041da177e4SLinus Torvalds }
15051da177e4SLinus Torvalds 
15061da177e4SLinus Torvalds EXPORT_SYMBOL(flock_lock_file_wait);
15071da177e4SLinus Torvalds 
15081da177e4SLinus Torvalds /**
15091da177e4SLinus Torvalds  *	sys_flock: - flock() system call.
15101da177e4SLinus Torvalds  *	@fd: the file descriptor to lock.
15111da177e4SLinus Torvalds  *	@cmd: the type of lock to apply.
15121da177e4SLinus Torvalds  *
15131da177e4SLinus Torvalds  *	Apply a %FL_FLOCK style lock to an open file descriptor.
15141da177e4SLinus Torvalds  *	The @cmd can be one of
15151da177e4SLinus Torvalds  *
15161da177e4SLinus Torvalds  *	%LOCK_SH -- a shared lock.
15171da177e4SLinus Torvalds  *
15181da177e4SLinus Torvalds  *	%LOCK_EX -- an exclusive lock.
15191da177e4SLinus Torvalds  *
15201da177e4SLinus Torvalds  *	%LOCK_UN -- remove an existing lock.
15211da177e4SLinus Torvalds  *
15221da177e4SLinus Torvalds  *	%LOCK_MAND -- a `mandatory' flock.  This exists to emulate Windows Share Modes.
15231da177e4SLinus Torvalds  *
15241da177e4SLinus Torvalds  *	%LOCK_MAND can be combined with %LOCK_READ or %LOCK_WRITE to allow other
15251da177e4SLinus Torvalds  *	processes read and write access respectively.
15261da177e4SLinus Torvalds  */
15271da177e4SLinus Torvalds asmlinkage long sys_flock(unsigned int fd, unsigned int cmd)
15281da177e4SLinus Torvalds {
15291da177e4SLinus Torvalds 	struct file *filp;
15301da177e4SLinus Torvalds 	struct file_lock *lock;
15311da177e4SLinus Torvalds 	int can_sleep, unlock;
15321da177e4SLinus Torvalds 	int error;
15331da177e4SLinus Torvalds 
15341da177e4SLinus Torvalds 	error = -EBADF;
15351da177e4SLinus Torvalds 	filp = fget(fd);
15361da177e4SLinus Torvalds 	if (!filp)
15371da177e4SLinus Torvalds 		goto out;
15381da177e4SLinus Torvalds 
15391da177e4SLinus Torvalds 	can_sleep = !(cmd & LOCK_NB);
15401da177e4SLinus Torvalds 	cmd &= ~LOCK_NB;
15411da177e4SLinus Torvalds 	unlock = (cmd == LOCK_UN);
15421da177e4SLinus Torvalds 
15431da177e4SLinus Torvalds 	if (!unlock && !(cmd & LOCK_MAND) && !(filp->f_mode & 3))
15441da177e4SLinus Torvalds 		goto out_putf;
15451da177e4SLinus Torvalds 
15461da177e4SLinus Torvalds 	error = flock_make_lock(filp, &lock, cmd);
15471da177e4SLinus Torvalds 	if (error)
15481da177e4SLinus Torvalds 		goto out_putf;
15491da177e4SLinus Torvalds 	if (can_sleep)
15501da177e4SLinus Torvalds 		lock->fl_flags |= FL_SLEEP;
15511da177e4SLinus Torvalds 
15521da177e4SLinus Torvalds 	error = security_file_lock(filp, cmd);
15531da177e4SLinus Torvalds 	if (error)
15541da177e4SLinus Torvalds 		goto out_free;
15551da177e4SLinus Torvalds 
15561da177e4SLinus Torvalds 	if (filp->f_op && filp->f_op->flock)
15571da177e4SLinus Torvalds 		error = filp->f_op->flock(filp,
15581da177e4SLinus Torvalds 					  (can_sleep) ? F_SETLKW : F_SETLK,
15591da177e4SLinus Torvalds 					  lock);
15601da177e4SLinus Torvalds 	else
15611da177e4SLinus Torvalds 		error = flock_lock_file_wait(filp, lock);
15621da177e4SLinus Torvalds 
15631da177e4SLinus Torvalds  out_free:
15641da177e4SLinus Torvalds 	if (list_empty(&lock->fl_link)) {
15651da177e4SLinus Torvalds 		locks_free_lock(lock);
15661da177e4SLinus Torvalds 	}
15671da177e4SLinus Torvalds 
15681da177e4SLinus Torvalds  out_putf:
15691da177e4SLinus Torvalds 	fput(filp);
15701da177e4SLinus Torvalds  out:
15711da177e4SLinus Torvalds 	return error;
15721da177e4SLinus Torvalds }
15731da177e4SLinus Torvalds 
15741da177e4SLinus Torvalds /* Report the first existing lock that would conflict with l.
15751da177e4SLinus Torvalds  * This implements the F_GETLK command of fcntl().
15761da177e4SLinus Torvalds  */
15771da177e4SLinus Torvalds int fcntl_getlk(struct file *filp, struct flock __user *l)
15781da177e4SLinus Torvalds {
15798dc7c311SAndy Adamson 	struct file_lock *fl, cfl, file_lock;
15801da177e4SLinus Torvalds 	struct flock flock;
15811da177e4SLinus Torvalds 	int error;
15821da177e4SLinus Torvalds 
15831da177e4SLinus Torvalds 	error = -EFAULT;
15841da177e4SLinus Torvalds 	if (copy_from_user(&flock, l, sizeof(flock)))
15851da177e4SLinus Torvalds 		goto out;
15861da177e4SLinus Torvalds 	error = -EINVAL;
15871da177e4SLinus Torvalds 	if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK))
15881da177e4SLinus Torvalds 		goto out;
15891da177e4SLinus Torvalds 
15901da177e4SLinus Torvalds 	error = flock_to_posix_lock(filp, &file_lock, &flock);
15911da177e4SLinus Torvalds 	if (error)
15921da177e4SLinus Torvalds 		goto out;
15931da177e4SLinus Torvalds 
15941da177e4SLinus Torvalds 	if (filp->f_op && filp->f_op->lock) {
15951da177e4SLinus Torvalds 		error = filp->f_op->lock(filp, F_GETLK, &file_lock);
159680fec4c6STrond Myklebust 		if (file_lock.fl_ops && file_lock.fl_ops->fl_release_private)
159780fec4c6STrond Myklebust 			file_lock.fl_ops->fl_release_private(&file_lock);
15981da177e4SLinus Torvalds 		if (error < 0)
15991da177e4SLinus Torvalds 			goto out;
16001da177e4SLinus Torvalds 		else
16011da177e4SLinus Torvalds 		  fl = (file_lock.fl_type == F_UNLCK ? NULL : &file_lock);
16021da177e4SLinus Torvalds 	} else {
16038dc7c311SAndy Adamson 		fl = (posix_test_lock(filp, &file_lock, &cfl) ? &cfl : NULL);
16041da177e4SLinus Torvalds 	}
16051da177e4SLinus Torvalds 
16061da177e4SLinus Torvalds 	flock.l_type = F_UNLCK;
16071da177e4SLinus Torvalds 	if (fl != NULL) {
16081da177e4SLinus Torvalds 		flock.l_pid = fl->fl_pid;
16091da177e4SLinus Torvalds #if BITS_PER_LONG == 32
16101da177e4SLinus Torvalds 		/*
16111da177e4SLinus Torvalds 		 * Make sure we can represent the posix lock via
16121da177e4SLinus Torvalds 		 * legacy 32bit flock.
16131da177e4SLinus Torvalds 		 */
16141da177e4SLinus Torvalds 		error = -EOVERFLOW;
16151da177e4SLinus Torvalds 		if (fl->fl_start > OFFT_OFFSET_MAX)
16161da177e4SLinus Torvalds 			goto out;
16171da177e4SLinus Torvalds 		if ((fl->fl_end != OFFSET_MAX)
16181da177e4SLinus Torvalds 		    && (fl->fl_end > OFFT_OFFSET_MAX))
16191da177e4SLinus Torvalds 			goto out;
16201da177e4SLinus Torvalds #endif
16211da177e4SLinus Torvalds 		flock.l_start = fl->fl_start;
16221da177e4SLinus Torvalds 		flock.l_len = fl->fl_end == OFFSET_MAX ? 0 :
16231da177e4SLinus Torvalds 			fl->fl_end - fl->fl_start + 1;
16241da177e4SLinus Torvalds 		flock.l_whence = 0;
16251da177e4SLinus Torvalds 		flock.l_type = fl->fl_type;
16261da177e4SLinus Torvalds 	}
16271da177e4SLinus Torvalds 	error = -EFAULT;
16281da177e4SLinus Torvalds 	if (!copy_to_user(l, &flock, sizeof(flock)))
16291da177e4SLinus Torvalds 		error = 0;
16301da177e4SLinus Torvalds out:
16311da177e4SLinus Torvalds 	return error;
16321da177e4SLinus Torvalds }
16331da177e4SLinus Torvalds 
16341da177e4SLinus Torvalds /* Apply the lock described by l to an open file descriptor.
16351da177e4SLinus Torvalds  * This implements both the F_SETLK and F_SETLKW commands of fcntl().
16361da177e4SLinus Torvalds  */
1637c293621bSPeter Staubach int fcntl_setlk(unsigned int fd, struct file *filp, unsigned int cmd,
1638c293621bSPeter Staubach 		struct flock __user *l)
16391da177e4SLinus Torvalds {
16401da177e4SLinus Torvalds 	struct file_lock *file_lock = locks_alloc_lock();
16411da177e4SLinus Torvalds 	struct flock flock;
16421da177e4SLinus Torvalds 	struct inode *inode;
16431da177e4SLinus Torvalds 	int error;
16441da177e4SLinus Torvalds 
16451da177e4SLinus Torvalds 	if (file_lock == NULL)
16461da177e4SLinus Torvalds 		return -ENOLCK;
16471da177e4SLinus Torvalds 
16481da177e4SLinus Torvalds 	/*
16491da177e4SLinus Torvalds 	 * This might block, so we do it before checking the inode.
16501da177e4SLinus Torvalds 	 */
16511da177e4SLinus Torvalds 	error = -EFAULT;
16521da177e4SLinus Torvalds 	if (copy_from_user(&flock, l, sizeof(flock)))
16531da177e4SLinus Torvalds 		goto out;
16541da177e4SLinus Torvalds 
16551da177e4SLinus Torvalds 	inode = filp->f_dentry->d_inode;
16561da177e4SLinus Torvalds 
16571da177e4SLinus Torvalds 	/* Don't allow mandatory locks on files that may be memory mapped
16581da177e4SLinus Torvalds 	 * and shared.
16591da177e4SLinus Torvalds 	 */
16601da177e4SLinus Torvalds 	if (IS_MANDLOCK(inode) &&
16611da177e4SLinus Torvalds 	    (inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID &&
16621da177e4SLinus Torvalds 	    mapping_writably_mapped(filp->f_mapping)) {
16631da177e4SLinus Torvalds 		error = -EAGAIN;
16641da177e4SLinus Torvalds 		goto out;
16651da177e4SLinus Torvalds 	}
16661da177e4SLinus Torvalds 
1667c293621bSPeter Staubach again:
16681da177e4SLinus Torvalds 	error = flock_to_posix_lock(filp, file_lock, &flock);
16691da177e4SLinus Torvalds 	if (error)
16701da177e4SLinus Torvalds 		goto out;
16711da177e4SLinus Torvalds 	if (cmd == F_SETLKW) {
16721da177e4SLinus Torvalds 		file_lock->fl_flags |= FL_SLEEP;
16731da177e4SLinus Torvalds 	}
16741da177e4SLinus Torvalds 
16751da177e4SLinus Torvalds 	error = -EBADF;
16761da177e4SLinus Torvalds 	switch (flock.l_type) {
16771da177e4SLinus Torvalds 	case F_RDLCK:
16781da177e4SLinus Torvalds 		if (!(filp->f_mode & FMODE_READ))
16791da177e4SLinus Torvalds 			goto out;
16801da177e4SLinus Torvalds 		break;
16811da177e4SLinus Torvalds 	case F_WRLCK:
16821da177e4SLinus Torvalds 		if (!(filp->f_mode & FMODE_WRITE))
16831da177e4SLinus Torvalds 			goto out;
16841da177e4SLinus Torvalds 		break;
16851da177e4SLinus Torvalds 	case F_UNLCK:
16861da177e4SLinus Torvalds 		break;
16871da177e4SLinus Torvalds 	default:
16881da177e4SLinus Torvalds 		error = -EINVAL;
16891da177e4SLinus Torvalds 		goto out;
16901da177e4SLinus Torvalds 	}
16911da177e4SLinus Torvalds 
16921da177e4SLinus Torvalds 	error = security_file_lock(filp, file_lock->fl_type);
16931da177e4SLinus Torvalds 	if (error)
16941da177e4SLinus Torvalds 		goto out;
16951da177e4SLinus Torvalds 
1696c293621bSPeter Staubach 	if (filp->f_op && filp->f_op->lock != NULL)
16971da177e4SLinus Torvalds 		error = filp->f_op->lock(filp, cmd, file_lock);
1698c293621bSPeter Staubach 	else {
16991da177e4SLinus Torvalds 		for (;;) {
17001da177e4SLinus Torvalds 			error = __posix_lock_file(inode, file_lock);
17011da177e4SLinus Torvalds 			if ((error != -EAGAIN) || (cmd == F_SETLK))
17021da177e4SLinus Torvalds 				break;
17031da177e4SLinus Torvalds 			error = wait_event_interruptible(file_lock->fl_wait,
17041da177e4SLinus Torvalds 					!file_lock->fl_next);
17051da177e4SLinus Torvalds 			if (!error)
17061da177e4SLinus Torvalds 				continue;
17071da177e4SLinus Torvalds 
17081da177e4SLinus Torvalds 			locks_delete_block(file_lock);
17091da177e4SLinus Torvalds 			break;
17101da177e4SLinus Torvalds 		}
1711c293621bSPeter Staubach 	}
1712c293621bSPeter Staubach 
1713c293621bSPeter Staubach 	/*
1714c293621bSPeter Staubach 	 * Attempt to detect a close/fcntl race and recover by
1715c293621bSPeter Staubach 	 * releasing the lock that was just acquired.
1716c293621bSPeter Staubach 	 */
1717c293621bSPeter Staubach 	if (!error && fcheck(fd) != filp && flock.l_type != F_UNLCK) {
1718c293621bSPeter Staubach 		flock.l_type = F_UNLCK;
1719c293621bSPeter Staubach 		goto again;
1720c293621bSPeter Staubach 	}
17211da177e4SLinus Torvalds 
17221da177e4SLinus Torvalds out:
17231da177e4SLinus Torvalds 	locks_free_lock(file_lock);
17241da177e4SLinus Torvalds 	return error;
17251da177e4SLinus Torvalds }
17261da177e4SLinus Torvalds 
17271da177e4SLinus Torvalds #if BITS_PER_LONG == 32
17281da177e4SLinus Torvalds /* Report the first existing lock that would conflict with l.
17291da177e4SLinus Torvalds  * This implements the F_GETLK command of fcntl().
17301da177e4SLinus Torvalds  */
17311da177e4SLinus Torvalds int fcntl_getlk64(struct file *filp, struct flock64 __user *l)
17321da177e4SLinus Torvalds {
17338dc7c311SAndy Adamson 	struct file_lock *fl, cfl, file_lock;
17341da177e4SLinus Torvalds 	struct flock64 flock;
17351da177e4SLinus Torvalds 	int error;
17361da177e4SLinus Torvalds 
17371da177e4SLinus Torvalds 	error = -EFAULT;
17381da177e4SLinus Torvalds 	if (copy_from_user(&flock, l, sizeof(flock)))
17391da177e4SLinus Torvalds 		goto out;
17401da177e4SLinus Torvalds 	error = -EINVAL;
17411da177e4SLinus Torvalds 	if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK))
17421da177e4SLinus Torvalds 		goto out;
17431da177e4SLinus Torvalds 
17441da177e4SLinus Torvalds 	error = flock64_to_posix_lock(filp, &file_lock, &flock);
17451da177e4SLinus Torvalds 	if (error)
17461da177e4SLinus Torvalds 		goto out;
17471da177e4SLinus Torvalds 
17481da177e4SLinus Torvalds 	if (filp->f_op && filp->f_op->lock) {
17491da177e4SLinus Torvalds 		error = filp->f_op->lock(filp, F_GETLK, &file_lock);
175080fec4c6STrond Myklebust 		if (file_lock.fl_ops && file_lock.fl_ops->fl_release_private)
175180fec4c6STrond Myklebust 			file_lock.fl_ops->fl_release_private(&file_lock);
17521da177e4SLinus Torvalds 		if (error < 0)
17531da177e4SLinus Torvalds 			goto out;
17541da177e4SLinus Torvalds 		else
17551da177e4SLinus Torvalds 		  fl = (file_lock.fl_type == F_UNLCK ? NULL : &file_lock);
17561da177e4SLinus Torvalds 	} else {
17578dc7c311SAndy Adamson 		fl = (posix_test_lock(filp, &file_lock, &cfl) ? &cfl : NULL);
17581da177e4SLinus Torvalds 	}
17591da177e4SLinus Torvalds 
17601da177e4SLinus Torvalds 	flock.l_type = F_UNLCK;
17611da177e4SLinus Torvalds 	if (fl != NULL) {
17621da177e4SLinus Torvalds 		flock.l_pid = fl->fl_pid;
17631da177e4SLinus Torvalds 		flock.l_start = fl->fl_start;
17641da177e4SLinus Torvalds 		flock.l_len = fl->fl_end == OFFSET_MAX ? 0 :
17651da177e4SLinus Torvalds 			fl->fl_end - fl->fl_start + 1;
17661da177e4SLinus Torvalds 		flock.l_whence = 0;
17671da177e4SLinus Torvalds 		flock.l_type = fl->fl_type;
17681da177e4SLinus Torvalds 	}
17691da177e4SLinus Torvalds 	error = -EFAULT;
17701da177e4SLinus Torvalds 	if (!copy_to_user(l, &flock, sizeof(flock)))
17711da177e4SLinus Torvalds 		error = 0;
17721da177e4SLinus Torvalds 
17731da177e4SLinus Torvalds out:
17741da177e4SLinus Torvalds 	return error;
17751da177e4SLinus Torvalds }
17761da177e4SLinus Torvalds 
17771da177e4SLinus Torvalds /* Apply the lock described by l to an open file descriptor.
17781da177e4SLinus Torvalds  * This implements both the F_SETLK and F_SETLKW commands of fcntl().
17791da177e4SLinus Torvalds  */
1780c293621bSPeter Staubach int fcntl_setlk64(unsigned int fd, struct file *filp, unsigned int cmd,
1781c293621bSPeter Staubach 		struct flock64 __user *l)
17821da177e4SLinus Torvalds {
17831da177e4SLinus Torvalds 	struct file_lock *file_lock = locks_alloc_lock();
17841da177e4SLinus Torvalds 	struct flock64 flock;
17851da177e4SLinus Torvalds 	struct inode *inode;
17861da177e4SLinus Torvalds 	int error;
17871da177e4SLinus Torvalds 
17881da177e4SLinus Torvalds 	if (file_lock == NULL)
17891da177e4SLinus Torvalds 		return -ENOLCK;
17901da177e4SLinus Torvalds 
17911da177e4SLinus Torvalds 	/*
17921da177e4SLinus Torvalds 	 * This might block, so we do it before checking the inode.
17931da177e4SLinus Torvalds 	 */
17941da177e4SLinus Torvalds 	error = -EFAULT;
17951da177e4SLinus Torvalds 	if (copy_from_user(&flock, l, sizeof(flock)))
17961da177e4SLinus Torvalds 		goto out;
17971da177e4SLinus Torvalds 
17981da177e4SLinus Torvalds 	inode = filp->f_dentry->d_inode;
17991da177e4SLinus Torvalds 
18001da177e4SLinus Torvalds 	/* Don't allow mandatory locks on files that may be memory mapped
18011da177e4SLinus Torvalds 	 * and shared.
18021da177e4SLinus Torvalds 	 */
18031da177e4SLinus Torvalds 	if (IS_MANDLOCK(inode) &&
18041da177e4SLinus Torvalds 	    (inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID &&
18051da177e4SLinus Torvalds 	    mapping_writably_mapped(filp->f_mapping)) {
18061da177e4SLinus Torvalds 		error = -EAGAIN;
18071da177e4SLinus Torvalds 		goto out;
18081da177e4SLinus Torvalds 	}
18091da177e4SLinus Torvalds 
1810c293621bSPeter Staubach again:
18111da177e4SLinus Torvalds 	error = flock64_to_posix_lock(filp, file_lock, &flock);
18121da177e4SLinus Torvalds 	if (error)
18131da177e4SLinus Torvalds 		goto out;
18141da177e4SLinus Torvalds 	if (cmd == F_SETLKW64) {
18151da177e4SLinus Torvalds 		file_lock->fl_flags |= FL_SLEEP;
18161da177e4SLinus Torvalds 	}
18171da177e4SLinus Torvalds 
18181da177e4SLinus Torvalds 	error = -EBADF;
18191da177e4SLinus Torvalds 	switch (flock.l_type) {
18201da177e4SLinus Torvalds 	case F_RDLCK:
18211da177e4SLinus Torvalds 		if (!(filp->f_mode & FMODE_READ))
18221da177e4SLinus Torvalds 			goto out;
18231da177e4SLinus Torvalds 		break;
18241da177e4SLinus Torvalds 	case F_WRLCK:
18251da177e4SLinus Torvalds 		if (!(filp->f_mode & FMODE_WRITE))
18261da177e4SLinus Torvalds 			goto out;
18271da177e4SLinus Torvalds 		break;
18281da177e4SLinus Torvalds 	case F_UNLCK:
18291da177e4SLinus Torvalds 		break;
18301da177e4SLinus Torvalds 	default:
18311da177e4SLinus Torvalds 		error = -EINVAL;
18321da177e4SLinus Torvalds 		goto out;
18331da177e4SLinus Torvalds 	}
18341da177e4SLinus Torvalds 
18351da177e4SLinus Torvalds 	error = security_file_lock(filp, file_lock->fl_type);
18361da177e4SLinus Torvalds 	if (error)
18371da177e4SLinus Torvalds 		goto out;
18381da177e4SLinus Torvalds 
1839c293621bSPeter Staubach 	if (filp->f_op && filp->f_op->lock != NULL)
18401da177e4SLinus Torvalds 		error = filp->f_op->lock(filp, cmd, file_lock);
1841c293621bSPeter Staubach 	else {
18421da177e4SLinus Torvalds 		for (;;) {
18431da177e4SLinus Torvalds 			error = __posix_lock_file(inode, file_lock);
18441da177e4SLinus Torvalds 			if ((error != -EAGAIN) || (cmd == F_SETLK64))
18451da177e4SLinus Torvalds 				break;
18461da177e4SLinus Torvalds 			error = wait_event_interruptible(file_lock->fl_wait,
18471da177e4SLinus Torvalds 					!file_lock->fl_next);
18481da177e4SLinus Torvalds 			if (!error)
18491da177e4SLinus Torvalds 				continue;
18501da177e4SLinus Torvalds 
18511da177e4SLinus Torvalds 			locks_delete_block(file_lock);
18521da177e4SLinus Torvalds 			break;
18531da177e4SLinus Torvalds 		}
1854c293621bSPeter Staubach 	}
1855c293621bSPeter Staubach 
1856c293621bSPeter Staubach 	/*
1857c293621bSPeter Staubach 	 * Attempt to detect a close/fcntl race and recover by
1858c293621bSPeter Staubach 	 * releasing the lock that was just acquired.
1859c293621bSPeter Staubach 	 */
1860c293621bSPeter Staubach 	if (!error && fcheck(fd) != filp && flock.l_type != F_UNLCK) {
1861c293621bSPeter Staubach 		flock.l_type = F_UNLCK;
1862c293621bSPeter Staubach 		goto again;
1863c293621bSPeter Staubach 	}
18641da177e4SLinus Torvalds 
18651da177e4SLinus Torvalds out:
18661da177e4SLinus Torvalds 	locks_free_lock(file_lock);
18671da177e4SLinus Torvalds 	return error;
18681da177e4SLinus Torvalds }
18691da177e4SLinus Torvalds #endif /* BITS_PER_LONG == 32 */
18701da177e4SLinus Torvalds 
18711da177e4SLinus Torvalds /*
18721da177e4SLinus Torvalds  * This function is called when the file is being removed
18731da177e4SLinus Torvalds  * from the task's fd array.  POSIX locks belonging to this task
18741da177e4SLinus Torvalds  * are deleted at this time.
18751da177e4SLinus Torvalds  */
18761da177e4SLinus Torvalds void locks_remove_posix(struct file *filp, fl_owner_t owner)
18771da177e4SLinus Torvalds {
18781da177e4SLinus Torvalds 	struct file_lock lock, **before;
18791da177e4SLinus Torvalds 
18801da177e4SLinus Torvalds 	/*
18811da177e4SLinus Torvalds 	 * If there are no locks held on this file, we don't need to call
18821da177e4SLinus Torvalds 	 * posix_lock_file().  Another process could be setting a lock on this
18831da177e4SLinus Torvalds 	 * file at the same time, but we wouldn't remove that lock anyway.
18841da177e4SLinus Torvalds 	 */
18851da177e4SLinus Torvalds 	before = &filp->f_dentry->d_inode->i_flock;
18861da177e4SLinus Torvalds 	if (*before == NULL)
18871da177e4SLinus Torvalds 		return;
18881da177e4SLinus Torvalds 
18891da177e4SLinus Torvalds 	lock.fl_type = F_UNLCK;
18901da177e4SLinus Torvalds 	lock.fl_flags = FL_POSIX;
18911da177e4SLinus Torvalds 	lock.fl_start = 0;
18921da177e4SLinus Torvalds 	lock.fl_end = OFFSET_MAX;
18931da177e4SLinus Torvalds 	lock.fl_owner = owner;
18941da177e4SLinus Torvalds 	lock.fl_pid = current->tgid;
18951da177e4SLinus Torvalds 	lock.fl_file = filp;
18961da177e4SLinus Torvalds 	lock.fl_ops = NULL;
18971da177e4SLinus Torvalds 	lock.fl_lmops = NULL;
18981da177e4SLinus Torvalds 
18991da177e4SLinus Torvalds 	if (filp->f_op && filp->f_op->lock != NULL) {
19001da177e4SLinus Torvalds 		filp->f_op->lock(filp, F_SETLK, &lock);
19011da177e4SLinus Torvalds 		goto out;
19021da177e4SLinus Torvalds 	}
19031da177e4SLinus Torvalds 
19041da177e4SLinus Torvalds 	/* Can't use posix_lock_file here; we need to remove it no matter
19051da177e4SLinus Torvalds 	 * which pid we have.
19061da177e4SLinus Torvalds 	 */
19071da177e4SLinus Torvalds 	lock_kernel();
19081da177e4SLinus Torvalds 	while (*before != NULL) {
19091da177e4SLinus Torvalds 		struct file_lock *fl = *before;
19101da177e4SLinus Torvalds 		if (IS_POSIX(fl) && posix_same_owner(fl, &lock)) {
19111da177e4SLinus Torvalds 			locks_delete_lock(before);
19121da177e4SLinus Torvalds 			continue;
19131da177e4SLinus Torvalds 		}
19141da177e4SLinus Torvalds 		before = &fl->fl_next;
19151da177e4SLinus Torvalds 	}
19161da177e4SLinus Torvalds 	unlock_kernel();
19171da177e4SLinus Torvalds out:
19181da177e4SLinus Torvalds 	if (lock.fl_ops && lock.fl_ops->fl_release_private)
19191da177e4SLinus Torvalds 		lock.fl_ops->fl_release_private(&lock);
19201da177e4SLinus Torvalds }
19211da177e4SLinus Torvalds 
19221da177e4SLinus Torvalds EXPORT_SYMBOL(locks_remove_posix);
19231da177e4SLinus Torvalds 
19241da177e4SLinus Torvalds /*
19251da177e4SLinus Torvalds  * This function is called on the last close of an open file.
19261da177e4SLinus Torvalds  */
19271da177e4SLinus Torvalds void locks_remove_flock(struct file *filp)
19281da177e4SLinus Torvalds {
19291da177e4SLinus Torvalds 	struct inode * inode = filp->f_dentry->d_inode;
19301da177e4SLinus Torvalds 	struct file_lock *fl;
19311da177e4SLinus Torvalds 	struct file_lock **before;
19321da177e4SLinus Torvalds 
19331da177e4SLinus Torvalds 	if (!inode->i_flock)
19341da177e4SLinus Torvalds 		return;
19351da177e4SLinus Torvalds 
19361da177e4SLinus Torvalds 	if (filp->f_op && filp->f_op->flock) {
19371da177e4SLinus Torvalds 		struct file_lock fl = {
19381da177e4SLinus Torvalds 			.fl_pid = current->tgid,
19391da177e4SLinus Torvalds 			.fl_file = filp,
19401da177e4SLinus Torvalds 			.fl_flags = FL_FLOCK,
19411da177e4SLinus Torvalds 			.fl_type = F_UNLCK,
19421da177e4SLinus Torvalds 			.fl_end = OFFSET_MAX,
19431da177e4SLinus Torvalds 		};
19441da177e4SLinus Torvalds 		filp->f_op->flock(filp, F_SETLKW, &fl);
194580fec4c6STrond Myklebust 		if (fl.fl_ops && fl.fl_ops->fl_release_private)
194680fec4c6STrond Myklebust 			fl.fl_ops->fl_release_private(&fl);
19471da177e4SLinus Torvalds 	}
19481da177e4SLinus Torvalds 
19491da177e4SLinus Torvalds 	lock_kernel();
19501da177e4SLinus Torvalds 	before = &inode->i_flock;
19511da177e4SLinus Torvalds 
19521da177e4SLinus Torvalds 	while ((fl = *before) != NULL) {
19531da177e4SLinus Torvalds 		if (fl->fl_file == filp) {
1954c293621bSPeter Staubach 			if (IS_FLOCK(fl)) {
19551da177e4SLinus Torvalds 				locks_delete_lock(before);
19561da177e4SLinus Torvalds 				continue;
19571da177e4SLinus Torvalds 			}
19581da177e4SLinus Torvalds 			if (IS_LEASE(fl)) {
19591da177e4SLinus Torvalds 				lease_modify(before, F_UNLCK);
19601da177e4SLinus Torvalds 				continue;
19611da177e4SLinus Torvalds 			}
19621da177e4SLinus Torvalds 			/* What? */
19631da177e4SLinus Torvalds 			BUG();
19641da177e4SLinus Torvalds  		}
19651da177e4SLinus Torvalds 		before = &fl->fl_next;
19661da177e4SLinus Torvalds 	}
19671da177e4SLinus Torvalds 	unlock_kernel();
19681da177e4SLinus Torvalds }
19691da177e4SLinus Torvalds 
19701da177e4SLinus Torvalds /**
19711da177e4SLinus Torvalds  *	posix_unblock_lock - stop waiting for a file lock
19721da177e4SLinus Torvalds  *      @filp:   how the file was opened
19731da177e4SLinus Torvalds  *	@waiter: the lock which was waiting
19741da177e4SLinus Torvalds  *
19751da177e4SLinus Torvalds  *	lockd needs to block waiting for locks.
19761da177e4SLinus Torvalds  */
197764a318eeSJ. Bruce Fields int
19781da177e4SLinus Torvalds posix_unblock_lock(struct file *filp, struct file_lock *waiter)
19791da177e4SLinus Torvalds {
198064a318eeSJ. Bruce Fields 	int status = 0;
198164a318eeSJ. Bruce Fields 
19821da177e4SLinus Torvalds 	lock_kernel();
19835996a298SJ. Bruce Fields 	if (waiter->fl_next)
19841da177e4SLinus Torvalds 		__locks_delete_block(waiter);
198564a318eeSJ. Bruce Fields 	else
198664a318eeSJ. Bruce Fields 		status = -ENOENT;
19871da177e4SLinus Torvalds 	unlock_kernel();
198864a318eeSJ. Bruce Fields 	return status;
19891da177e4SLinus Torvalds }
19901da177e4SLinus Torvalds 
19911da177e4SLinus Torvalds EXPORT_SYMBOL(posix_unblock_lock);
19921da177e4SLinus Torvalds 
19931da177e4SLinus Torvalds static void lock_get_status(char* out, struct file_lock *fl, int id, char *pfx)
19941da177e4SLinus Torvalds {
19951da177e4SLinus Torvalds 	struct inode *inode = NULL;
19961da177e4SLinus Torvalds 
19971da177e4SLinus Torvalds 	if (fl->fl_file != NULL)
19981da177e4SLinus Torvalds 		inode = fl->fl_file->f_dentry->d_inode;
19991da177e4SLinus Torvalds 
20001da177e4SLinus Torvalds 	out += sprintf(out, "%d:%s ", id, pfx);
20011da177e4SLinus Torvalds 	if (IS_POSIX(fl)) {
20021da177e4SLinus Torvalds 		out += sprintf(out, "%6s %s ",
20031da177e4SLinus Torvalds 			     (fl->fl_flags & FL_ACCESS) ? "ACCESS" : "POSIX ",
20041da177e4SLinus Torvalds 			     (inode == NULL) ? "*NOINODE*" :
20051da177e4SLinus Torvalds 			     (IS_MANDLOCK(inode) &&
20061da177e4SLinus Torvalds 			      (inode->i_mode & (S_IXGRP | S_ISGID)) == S_ISGID) ?
20071da177e4SLinus Torvalds 			     "MANDATORY" : "ADVISORY ");
20081da177e4SLinus Torvalds 	} else if (IS_FLOCK(fl)) {
20091da177e4SLinus Torvalds 		if (fl->fl_type & LOCK_MAND) {
20101da177e4SLinus Torvalds 			out += sprintf(out, "FLOCK  MSNFS     ");
20111da177e4SLinus Torvalds 		} else {
20121da177e4SLinus Torvalds 			out += sprintf(out, "FLOCK  ADVISORY  ");
20131da177e4SLinus Torvalds 		}
20141da177e4SLinus Torvalds 	} else if (IS_LEASE(fl)) {
20151da177e4SLinus Torvalds 		out += sprintf(out, "LEASE  ");
20161da177e4SLinus Torvalds 		if (fl->fl_type & F_INPROGRESS)
20171da177e4SLinus Torvalds 			out += sprintf(out, "BREAKING  ");
20181da177e4SLinus Torvalds 		else if (fl->fl_file)
20191da177e4SLinus Torvalds 			out += sprintf(out, "ACTIVE    ");
20201da177e4SLinus Torvalds 		else
20211da177e4SLinus Torvalds 			out += sprintf(out, "BREAKER   ");
20221da177e4SLinus Torvalds 	} else {
20231da177e4SLinus Torvalds 		out += sprintf(out, "UNKNOWN UNKNOWN  ");
20241da177e4SLinus Torvalds 	}
20251da177e4SLinus Torvalds 	if (fl->fl_type & LOCK_MAND) {
20261da177e4SLinus Torvalds 		out += sprintf(out, "%s ",
20271da177e4SLinus Torvalds 			       (fl->fl_type & LOCK_READ)
20281da177e4SLinus Torvalds 			       ? (fl->fl_type & LOCK_WRITE) ? "RW   " : "READ "
20291da177e4SLinus Torvalds 			       : (fl->fl_type & LOCK_WRITE) ? "WRITE" : "NONE ");
20301da177e4SLinus Torvalds 	} else {
20311da177e4SLinus Torvalds 		out += sprintf(out, "%s ",
20321da177e4SLinus Torvalds 			       (fl->fl_type & F_INPROGRESS)
20331da177e4SLinus Torvalds 			       ? (fl->fl_type & F_UNLCK) ? "UNLCK" : "READ "
20341da177e4SLinus Torvalds 			       : (fl->fl_type & F_WRLCK) ? "WRITE" : "READ ");
20351da177e4SLinus Torvalds 	}
20361da177e4SLinus Torvalds 	if (inode) {
20371da177e4SLinus Torvalds #ifdef WE_CAN_BREAK_LSLK_NOW
20381da177e4SLinus Torvalds 		out += sprintf(out, "%d %s:%ld ", fl->fl_pid,
20391da177e4SLinus Torvalds 				inode->i_sb->s_id, inode->i_ino);
20401da177e4SLinus Torvalds #else
20411da177e4SLinus Torvalds 		/* userspace relies on this representation of dev_t ;-( */
20421da177e4SLinus Torvalds 		out += sprintf(out, "%d %02x:%02x:%ld ", fl->fl_pid,
20431da177e4SLinus Torvalds 				MAJOR(inode->i_sb->s_dev),
20441da177e4SLinus Torvalds 				MINOR(inode->i_sb->s_dev), inode->i_ino);
20451da177e4SLinus Torvalds #endif
20461da177e4SLinus Torvalds 	} else {
20471da177e4SLinus Torvalds 		out += sprintf(out, "%d <none>:0 ", fl->fl_pid);
20481da177e4SLinus Torvalds 	}
20491da177e4SLinus Torvalds 	if (IS_POSIX(fl)) {
20501da177e4SLinus Torvalds 		if (fl->fl_end == OFFSET_MAX)
20511da177e4SLinus Torvalds 			out += sprintf(out, "%Ld EOF\n", fl->fl_start);
20521da177e4SLinus Torvalds 		else
20531da177e4SLinus Torvalds 			out += sprintf(out, "%Ld %Ld\n", fl->fl_start,
20541da177e4SLinus Torvalds 					fl->fl_end);
20551da177e4SLinus Torvalds 	} else {
20561da177e4SLinus Torvalds 		out += sprintf(out, "0 EOF\n");
20571da177e4SLinus Torvalds 	}
20581da177e4SLinus Torvalds }
20591da177e4SLinus Torvalds 
20601da177e4SLinus Torvalds static void move_lock_status(char **p, off_t* pos, off_t offset)
20611da177e4SLinus Torvalds {
20621da177e4SLinus Torvalds 	int len;
20631da177e4SLinus Torvalds 	len = strlen(*p);
20641da177e4SLinus Torvalds 	if(*pos >= offset) {
20651da177e4SLinus Torvalds 		/* the complete line is valid */
20661da177e4SLinus Torvalds 		*p += len;
20671da177e4SLinus Torvalds 		*pos += len;
20681da177e4SLinus Torvalds 		return;
20691da177e4SLinus Torvalds 	}
20701da177e4SLinus Torvalds 	if(*pos+len > offset) {
20711da177e4SLinus Torvalds 		/* use the second part of the line */
20721da177e4SLinus Torvalds 		int i = offset-*pos;
20731da177e4SLinus Torvalds 		memmove(*p,*p+i,len-i);
20741da177e4SLinus Torvalds 		*p += len-i;
20751da177e4SLinus Torvalds 		*pos += len;
20761da177e4SLinus Torvalds 		return;
20771da177e4SLinus Torvalds 	}
20781da177e4SLinus Torvalds 	/* discard the complete line */
20791da177e4SLinus Torvalds 	*pos += len;
20801da177e4SLinus Torvalds }
20811da177e4SLinus Torvalds 
20821da177e4SLinus Torvalds /**
20831da177e4SLinus Torvalds  *	get_locks_status	-	reports lock usage in /proc/locks
20841da177e4SLinus Torvalds  *	@buffer: address in userspace to write into
20851da177e4SLinus Torvalds  *	@start: ?
20861da177e4SLinus Torvalds  *	@offset: how far we are through the buffer
20871da177e4SLinus Torvalds  *	@length: how much to read
20881da177e4SLinus Torvalds  */
20891da177e4SLinus Torvalds 
20901da177e4SLinus Torvalds int get_locks_status(char *buffer, char **start, off_t offset, int length)
20911da177e4SLinus Torvalds {
20921da177e4SLinus Torvalds 	struct list_head *tmp;
20931da177e4SLinus Torvalds 	char *q = buffer;
20941da177e4SLinus Torvalds 	off_t pos = 0;
20951da177e4SLinus Torvalds 	int i = 0;
20961da177e4SLinus Torvalds 
20971da177e4SLinus Torvalds 	lock_kernel();
20981da177e4SLinus Torvalds 	list_for_each(tmp, &file_lock_list) {
20991da177e4SLinus Torvalds 		struct list_head *btmp;
21001da177e4SLinus Torvalds 		struct file_lock *fl = list_entry(tmp, struct file_lock, fl_link);
21011da177e4SLinus Torvalds 		lock_get_status(q, fl, ++i, "");
21021da177e4SLinus Torvalds 		move_lock_status(&q, &pos, offset);
21031da177e4SLinus Torvalds 
21041da177e4SLinus Torvalds 		if(pos >= offset+length)
21051da177e4SLinus Torvalds 			goto done;
21061da177e4SLinus Torvalds 
21071da177e4SLinus Torvalds 		list_for_each(btmp, &fl->fl_block) {
21081da177e4SLinus Torvalds 			struct file_lock *bfl = list_entry(btmp,
21091da177e4SLinus Torvalds 					struct file_lock, fl_block);
21101da177e4SLinus Torvalds 			lock_get_status(q, bfl, i, " ->");
21111da177e4SLinus Torvalds 			move_lock_status(&q, &pos, offset);
21121da177e4SLinus Torvalds 
21131da177e4SLinus Torvalds 			if(pos >= offset+length)
21141da177e4SLinus Torvalds 				goto done;
21151da177e4SLinus Torvalds 		}
21161da177e4SLinus Torvalds 	}
21171da177e4SLinus Torvalds done:
21181da177e4SLinus Torvalds 	unlock_kernel();
21191da177e4SLinus Torvalds 	*start = buffer;
21201da177e4SLinus Torvalds 	if(q-buffer < length)
21211da177e4SLinus Torvalds 		return (q-buffer);
21221da177e4SLinus Torvalds 	return length;
21231da177e4SLinus Torvalds }
21241da177e4SLinus Torvalds 
21251da177e4SLinus Torvalds /**
21261da177e4SLinus Torvalds  *	lock_may_read - checks that the region is free of locks
21271da177e4SLinus Torvalds  *	@inode: the inode that is being read
21281da177e4SLinus Torvalds  *	@start: the first byte to read
21291da177e4SLinus Torvalds  *	@len: the number of bytes to read
21301da177e4SLinus Torvalds  *
21311da177e4SLinus Torvalds  *	Emulates Windows locking requirements.  Whole-file
21321da177e4SLinus Torvalds  *	mandatory locks (share modes) can prohibit a read and
21331da177e4SLinus Torvalds  *	byte-range POSIX locks can prohibit a read if they overlap.
21341da177e4SLinus Torvalds  *
21351da177e4SLinus Torvalds  *	N.B. this function is only ever called
21361da177e4SLinus Torvalds  *	from knfsd and ownership of locks is never checked.
21371da177e4SLinus Torvalds  */
21381da177e4SLinus Torvalds int lock_may_read(struct inode *inode, loff_t start, unsigned long len)
21391da177e4SLinus Torvalds {
21401da177e4SLinus Torvalds 	struct file_lock *fl;
21411da177e4SLinus Torvalds 	int result = 1;
21421da177e4SLinus Torvalds 	lock_kernel();
21431da177e4SLinus Torvalds 	for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
21441da177e4SLinus Torvalds 		if (IS_POSIX(fl)) {
21451da177e4SLinus Torvalds 			if (fl->fl_type == F_RDLCK)
21461da177e4SLinus Torvalds 				continue;
21471da177e4SLinus Torvalds 			if ((fl->fl_end < start) || (fl->fl_start > (start + len)))
21481da177e4SLinus Torvalds 				continue;
21491da177e4SLinus Torvalds 		} else if (IS_FLOCK(fl)) {
21501da177e4SLinus Torvalds 			if (!(fl->fl_type & LOCK_MAND))
21511da177e4SLinus Torvalds 				continue;
21521da177e4SLinus Torvalds 			if (fl->fl_type & LOCK_READ)
21531da177e4SLinus Torvalds 				continue;
21541da177e4SLinus Torvalds 		} else
21551da177e4SLinus Torvalds 			continue;
21561da177e4SLinus Torvalds 		result = 0;
21571da177e4SLinus Torvalds 		break;
21581da177e4SLinus Torvalds 	}
21591da177e4SLinus Torvalds 	unlock_kernel();
21601da177e4SLinus Torvalds 	return result;
21611da177e4SLinus Torvalds }
21621da177e4SLinus Torvalds 
21631da177e4SLinus Torvalds EXPORT_SYMBOL(lock_may_read);
21641da177e4SLinus Torvalds 
21651da177e4SLinus Torvalds /**
21661da177e4SLinus Torvalds  *	lock_may_write - checks that the region is free of locks
21671da177e4SLinus Torvalds  *	@inode: the inode that is being written
21681da177e4SLinus Torvalds  *	@start: the first byte to write
21691da177e4SLinus Torvalds  *	@len: the number of bytes to write
21701da177e4SLinus Torvalds  *
21711da177e4SLinus Torvalds  *	Emulates Windows locking requirements.  Whole-file
21721da177e4SLinus Torvalds  *	mandatory locks (share modes) can prohibit a write and
21731da177e4SLinus Torvalds  *	byte-range POSIX locks can prohibit a write if they overlap.
21741da177e4SLinus Torvalds  *
21751da177e4SLinus Torvalds  *	N.B. this function is only ever called
21761da177e4SLinus Torvalds  *	from knfsd and ownership of locks is never checked.
21771da177e4SLinus Torvalds  */
21781da177e4SLinus Torvalds int lock_may_write(struct inode *inode, loff_t start, unsigned long len)
21791da177e4SLinus Torvalds {
21801da177e4SLinus Torvalds 	struct file_lock *fl;
21811da177e4SLinus Torvalds 	int result = 1;
21821da177e4SLinus Torvalds 	lock_kernel();
21831da177e4SLinus Torvalds 	for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
21841da177e4SLinus Torvalds 		if (IS_POSIX(fl)) {
21851da177e4SLinus Torvalds 			if ((fl->fl_end < start) || (fl->fl_start > (start + len)))
21861da177e4SLinus Torvalds 				continue;
21871da177e4SLinus Torvalds 		} else if (IS_FLOCK(fl)) {
21881da177e4SLinus Torvalds 			if (!(fl->fl_type & LOCK_MAND))
21891da177e4SLinus Torvalds 				continue;
21901da177e4SLinus Torvalds 			if (fl->fl_type & LOCK_WRITE)
21911da177e4SLinus Torvalds 				continue;
21921da177e4SLinus Torvalds 		} else
21931da177e4SLinus Torvalds 			continue;
21941da177e4SLinus Torvalds 		result = 0;
21951da177e4SLinus Torvalds 		break;
21961da177e4SLinus Torvalds 	}
21971da177e4SLinus Torvalds 	unlock_kernel();
21981da177e4SLinus Torvalds 	return result;
21991da177e4SLinus Torvalds }
22001da177e4SLinus Torvalds 
22011da177e4SLinus Torvalds EXPORT_SYMBOL(lock_may_write);
22021da177e4SLinus Torvalds 
22031da177e4SLinus Torvalds static inline void __steal_locks(struct file *file, fl_owner_t from)
22041da177e4SLinus Torvalds {
22051da177e4SLinus Torvalds 	struct inode *inode = file->f_dentry->d_inode;
22061da177e4SLinus Torvalds 	struct file_lock *fl = inode->i_flock;
22071da177e4SLinus Torvalds 
22081da177e4SLinus Torvalds 	while (fl) {
22091da177e4SLinus Torvalds 		if (fl->fl_file == file && fl->fl_owner == from)
22101da177e4SLinus Torvalds 			fl->fl_owner = current->files;
22111da177e4SLinus Torvalds 		fl = fl->fl_next;
22121da177e4SLinus Torvalds 	}
22131da177e4SLinus Torvalds }
22141da177e4SLinus Torvalds 
22151da177e4SLinus Torvalds /* When getting ready for executing a binary, we make sure that current
22161da177e4SLinus Torvalds  * has a files_struct on its own. Before dropping the old files_struct,
22171da177e4SLinus Torvalds  * we take over ownership of all locks for all file descriptors we own.
22181da177e4SLinus Torvalds  * Note that we may accidentally steal a lock for a file that a sibling
22191da177e4SLinus Torvalds  * has created since the unshare_files() call.
22201da177e4SLinus Torvalds  */
22211da177e4SLinus Torvalds void steal_locks(fl_owner_t from)
22221da177e4SLinus Torvalds {
22231da177e4SLinus Torvalds 	struct files_struct *files = current->files;
22241da177e4SLinus Torvalds 	int i, j;
2225badf1662SDipankar Sarma 	struct fdtable *fdt;
22261da177e4SLinus Torvalds 
22271da177e4SLinus Torvalds 	if (from == files)
22281da177e4SLinus Torvalds 		return;
22291da177e4SLinus Torvalds 
22301da177e4SLinus Torvalds 	lock_kernel();
22311da177e4SLinus Torvalds 	j = 0;
22324fb3a538SDipankar Sarma 	rcu_read_lock();
2233badf1662SDipankar Sarma 	fdt = files_fdtable(files);
22341da177e4SLinus Torvalds 	for (;;) {
22351da177e4SLinus Torvalds 		unsigned long set;
22361da177e4SLinus Torvalds 		i = j * __NFDBITS;
2237badf1662SDipankar Sarma 		if (i >= fdt->max_fdset || i >= fdt->max_fds)
22381da177e4SLinus Torvalds 			break;
2239badf1662SDipankar Sarma 		set = fdt->open_fds->fds_bits[j++];
22401da177e4SLinus Torvalds 		while (set) {
22411da177e4SLinus Torvalds 			if (set & 1) {
2242badf1662SDipankar Sarma 				struct file *file = fdt->fd[i];
22431da177e4SLinus Torvalds 				if (file)
22441da177e4SLinus Torvalds 					__steal_locks(file, from);
22451da177e4SLinus Torvalds 			}
22461da177e4SLinus Torvalds 			i++;
22471da177e4SLinus Torvalds 			set >>= 1;
22481da177e4SLinus Torvalds 		}
22491da177e4SLinus Torvalds 	}
22504fb3a538SDipankar Sarma 	rcu_read_unlock();
22511da177e4SLinus Torvalds 	unlock_kernel();
22521da177e4SLinus Torvalds }
22531da177e4SLinus Torvalds EXPORT_SYMBOL(steal_locks);
22541da177e4SLinus Torvalds 
22551da177e4SLinus Torvalds static int __init filelock_init(void)
22561da177e4SLinus Torvalds {
22571da177e4SLinus Torvalds 	filelock_cache = kmem_cache_create("file_lock_cache",
22581da177e4SLinus Torvalds 			sizeof(struct file_lock), 0, SLAB_PANIC,
22591da177e4SLinus Torvalds 			init_once, NULL);
22601da177e4SLinus Torvalds 	return 0;
22611da177e4SLinus Torvalds }
22621da177e4SLinus Torvalds 
22631da177e4SLinus Torvalds core_initcall(filelock_init);
2264