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 14226bcbf96SChristoph Hellwig static LIST_HEAD(file_lock_list); 1431da177e4SLinus Torvalds static LIST_HEAD(blocked_list); 1441da177e4SLinus Torvalds 145fa3536ccSEric Dumazet static kmem_cache_t *filelock_cache __read_mostly; 1461da177e4SLinus Torvalds 1471da177e4SLinus Torvalds /* Allocate an empty lock structure. */ 1481da177e4SLinus Torvalds static struct file_lock *locks_alloc_lock(void) 1491da177e4SLinus Torvalds { 150*e94b1766SChristoph Lameter return kmem_cache_alloc(filelock_cache, GFP_KERNEL); 1511da177e4SLinus Torvalds } 1521da177e4SLinus Torvalds 15347831f35STrond Myklebust static void locks_release_private(struct file_lock *fl) 15447831f35STrond Myklebust { 15547831f35STrond Myklebust if (fl->fl_ops) { 15647831f35STrond Myklebust if (fl->fl_ops->fl_release_private) 15747831f35STrond Myklebust fl->fl_ops->fl_release_private(fl); 15847831f35STrond Myklebust fl->fl_ops = NULL; 15947831f35STrond Myklebust } 16047831f35STrond Myklebust if (fl->fl_lmops) { 16147831f35STrond Myklebust if (fl->fl_lmops->fl_release_private) 16247831f35STrond Myklebust fl->fl_lmops->fl_release_private(fl); 16347831f35STrond Myklebust fl->fl_lmops = NULL; 16447831f35STrond Myklebust } 16547831f35STrond Myklebust 16647831f35STrond Myklebust } 16747831f35STrond Myklebust 1681da177e4SLinus Torvalds /* Free a lock which is not in use. */ 16933443c42SMatt Mackall static void locks_free_lock(struct file_lock *fl) 1701da177e4SLinus Torvalds { 1715ce29646SMiklos Szeredi BUG_ON(waitqueue_active(&fl->fl_wait)); 1725ce29646SMiklos Szeredi BUG_ON(!list_empty(&fl->fl_block)); 1735ce29646SMiklos Szeredi BUG_ON(!list_empty(&fl->fl_link)); 1741da177e4SLinus Torvalds 17547831f35STrond Myklebust locks_release_private(fl); 1761da177e4SLinus Torvalds kmem_cache_free(filelock_cache, fl); 1771da177e4SLinus Torvalds } 1781da177e4SLinus Torvalds 1791da177e4SLinus Torvalds void locks_init_lock(struct file_lock *fl) 1801da177e4SLinus Torvalds { 1811da177e4SLinus Torvalds INIT_LIST_HEAD(&fl->fl_link); 1821da177e4SLinus Torvalds INIT_LIST_HEAD(&fl->fl_block); 1831da177e4SLinus Torvalds init_waitqueue_head(&fl->fl_wait); 1841da177e4SLinus Torvalds fl->fl_next = NULL; 1851da177e4SLinus Torvalds fl->fl_fasync = NULL; 1861da177e4SLinus Torvalds fl->fl_owner = NULL; 1871da177e4SLinus Torvalds fl->fl_pid = 0; 1881da177e4SLinus Torvalds fl->fl_file = NULL; 1891da177e4SLinus Torvalds fl->fl_flags = 0; 1901da177e4SLinus Torvalds fl->fl_type = 0; 1911da177e4SLinus Torvalds fl->fl_start = fl->fl_end = 0; 1921da177e4SLinus Torvalds fl->fl_ops = NULL; 1931da177e4SLinus Torvalds fl->fl_lmops = NULL; 1941da177e4SLinus Torvalds } 1951da177e4SLinus Torvalds 1961da177e4SLinus Torvalds EXPORT_SYMBOL(locks_init_lock); 1971da177e4SLinus Torvalds 1981da177e4SLinus Torvalds /* 1991da177e4SLinus Torvalds * Initialises the fields of the file lock which are invariant for 2001da177e4SLinus Torvalds * free file_locks. 2011da177e4SLinus Torvalds */ 2021da177e4SLinus Torvalds static void init_once(void *foo, kmem_cache_t *cache, unsigned long flags) 2031da177e4SLinus Torvalds { 2041da177e4SLinus Torvalds struct file_lock *lock = (struct file_lock *) foo; 2051da177e4SLinus Torvalds 2061da177e4SLinus Torvalds if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) != 2071da177e4SLinus Torvalds SLAB_CTOR_CONSTRUCTOR) 2081da177e4SLinus Torvalds return; 2091da177e4SLinus Torvalds 2101da177e4SLinus Torvalds locks_init_lock(lock); 2111da177e4SLinus Torvalds } 2121da177e4SLinus Torvalds 21347831f35STrond Myklebust static void locks_copy_private(struct file_lock *new, struct file_lock *fl) 21447831f35STrond Myklebust { 21547831f35STrond Myklebust if (fl->fl_ops) { 21647831f35STrond Myklebust if (fl->fl_ops->fl_copy_lock) 21747831f35STrond Myklebust fl->fl_ops->fl_copy_lock(new, fl); 21847831f35STrond Myklebust new->fl_ops = fl->fl_ops; 21947831f35STrond Myklebust } 22047831f35STrond Myklebust if (fl->fl_lmops) { 22147831f35STrond Myklebust if (fl->fl_lmops->fl_copy_lock) 22247831f35STrond Myklebust fl->fl_lmops->fl_copy_lock(new, fl); 22347831f35STrond Myklebust new->fl_lmops = fl->fl_lmops; 22447831f35STrond Myklebust } 22547831f35STrond Myklebust } 22647831f35STrond Myklebust 2271da177e4SLinus Torvalds /* 2281da177e4SLinus Torvalds * Initialize a new lock from an existing file_lock structure. 2291da177e4SLinus Torvalds */ 2300996905fSTrond Myklebust static void __locks_copy_lock(struct file_lock *new, const struct file_lock *fl) 2311da177e4SLinus Torvalds { 2321da177e4SLinus Torvalds new->fl_owner = fl->fl_owner; 2331da177e4SLinus Torvalds new->fl_pid = fl->fl_pid; 2340996905fSTrond Myklebust new->fl_file = NULL; 2351da177e4SLinus Torvalds new->fl_flags = fl->fl_flags; 2361da177e4SLinus Torvalds new->fl_type = fl->fl_type; 2371da177e4SLinus Torvalds new->fl_start = fl->fl_start; 2381da177e4SLinus Torvalds new->fl_end = fl->fl_end; 2390996905fSTrond Myklebust new->fl_ops = NULL; 2400996905fSTrond Myklebust new->fl_lmops = NULL; 2410996905fSTrond Myklebust } 2420996905fSTrond Myklebust 2430996905fSTrond Myklebust void locks_copy_lock(struct file_lock *new, struct file_lock *fl) 2440996905fSTrond Myklebust { 2450996905fSTrond Myklebust locks_release_private(new); 2460996905fSTrond Myklebust 2470996905fSTrond Myklebust __locks_copy_lock(new, fl); 2480996905fSTrond Myklebust new->fl_file = fl->fl_file; 2491da177e4SLinus Torvalds new->fl_ops = fl->fl_ops; 2501da177e4SLinus Torvalds new->fl_lmops = fl->fl_lmops; 25147831f35STrond Myklebust 25247831f35STrond Myklebust locks_copy_private(new, fl); 2531da177e4SLinus Torvalds } 2541da177e4SLinus Torvalds 2551da177e4SLinus Torvalds EXPORT_SYMBOL(locks_copy_lock); 2561da177e4SLinus Torvalds 2571da177e4SLinus Torvalds static inline int flock_translate_cmd(int cmd) { 2581da177e4SLinus Torvalds if (cmd & LOCK_MAND) 2591da177e4SLinus Torvalds return cmd & (LOCK_MAND | LOCK_RW); 2601da177e4SLinus Torvalds switch (cmd) { 2611da177e4SLinus Torvalds case LOCK_SH: 2621da177e4SLinus Torvalds return F_RDLCK; 2631da177e4SLinus Torvalds case LOCK_EX: 2641da177e4SLinus Torvalds return F_WRLCK; 2651da177e4SLinus Torvalds case LOCK_UN: 2661da177e4SLinus Torvalds return F_UNLCK; 2671da177e4SLinus Torvalds } 2681da177e4SLinus Torvalds return -EINVAL; 2691da177e4SLinus Torvalds } 2701da177e4SLinus Torvalds 2711da177e4SLinus Torvalds /* Fill in a file_lock structure with an appropriate FLOCK lock. */ 2721da177e4SLinus Torvalds static int flock_make_lock(struct file *filp, struct file_lock **lock, 2731da177e4SLinus Torvalds unsigned int cmd) 2741da177e4SLinus Torvalds { 2751da177e4SLinus Torvalds struct file_lock *fl; 2761da177e4SLinus Torvalds int type = flock_translate_cmd(cmd); 2771da177e4SLinus Torvalds if (type < 0) 2781da177e4SLinus Torvalds return type; 2791da177e4SLinus Torvalds 2801da177e4SLinus Torvalds fl = locks_alloc_lock(); 2811da177e4SLinus Torvalds if (fl == NULL) 2821da177e4SLinus Torvalds return -ENOMEM; 2831da177e4SLinus Torvalds 2841da177e4SLinus Torvalds fl->fl_file = filp; 2851da177e4SLinus Torvalds fl->fl_pid = current->tgid; 2861da177e4SLinus Torvalds fl->fl_flags = FL_FLOCK; 2871da177e4SLinus Torvalds fl->fl_type = type; 2881da177e4SLinus Torvalds fl->fl_end = OFFSET_MAX; 2891da177e4SLinus Torvalds 2901da177e4SLinus Torvalds *lock = fl; 2911da177e4SLinus Torvalds return 0; 2921da177e4SLinus Torvalds } 2931da177e4SLinus Torvalds 2941da177e4SLinus Torvalds static int assign_type(struct file_lock *fl, int type) 2951da177e4SLinus Torvalds { 2961da177e4SLinus Torvalds switch (type) { 2971da177e4SLinus Torvalds case F_RDLCK: 2981da177e4SLinus Torvalds case F_WRLCK: 2991da177e4SLinus Torvalds case F_UNLCK: 3001da177e4SLinus Torvalds fl->fl_type = type; 3011da177e4SLinus Torvalds break; 3021da177e4SLinus Torvalds default: 3031da177e4SLinus Torvalds return -EINVAL; 3041da177e4SLinus Torvalds } 3051da177e4SLinus Torvalds return 0; 3061da177e4SLinus Torvalds } 3071da177e4SLinus Torvalds 3081da177e4SLinus Torvalds /* Verify a "struct flock" and copy it to a "struct file_lock" as a POSIX 3091da177e4SLinus Torvalds * style lock. 3101da177e4SLinus Torvalds */ 3111da177e4SLinus Torvalds static int flock_to_posix_lock(struct file *filp, struct file_lock *fl, 3121da177e4SLinus Torvalds struct flock *l) 3131da177e4SLinus Torvalds { 3141da177e4SLinus Torvalds off_t start, end; 3151da177e4SLinus Torvalds 3161da177e4SLinus Torvalds switch (l->l_whence) { 317f5579f8cSJosef 'Jeff' Sipek case SEEK_SET: 3181da177e4SLinus Torvalds start = 0; 3191da177e4SLinus Torvalds break; 320f5579f8cSJosef 'Jeff' Sipek case SEEK_CUR: 3211da177e4SLinus Torvalds start = filp->f_pos; 3221da177e4SLinus Torvalds break; 323f5579f8cSJosef 'Jeff' Sipek case SEEK_END: 3241da177e4SLinus Torvalds start = i_size_read(filp->f_dentry->d_inode); 3251da177e4SLinus Torvalds break; 3261da177e4SLinus Torvalds default: 3271da177e4SLinus Torvalds return -EINVAL; 3281da177e4SLinus Torvalds } 3291da177e4SLinus Torvalds 3301da177e4SLinus Torvalds /* POSIX-1996 leaves the case l->l_len < 0 undefined; 3311da177e4SLinus Torvalds POSIX-2001 defines it. */ 3321da177e4SLinus Torvalds start += l->l_start; 3331da177e4SLinus Torvalds if (start < 0) 3341da177e4SLinus Torvalds return -EINVAL; 3351da177e4SLinus Torvalds fl->fl_end = OFFSET_MAX; 3364c780a46STrond Myklebust if (l->l_len > 0) { 3374c780a46STrond Myklebust end = start + l->l_len - 1; 3384c780a46STrond Myklebust fl->fl_end = end; 3394c780a46STrond Myklebust } else if (l->l_len < 0) { 3404c780a46STrond Myklebust end = start - 1; 3414c780a46STrond Myklebust fl->fl_end = end; 3424c780a46STrond Myklebust start += l->l_len; 3434c780a46STrond Myklebust if (start < 0) 3444c780a46STrond Myklebust return -EINVAL; 3454c780a46STrond Myklebust } 3464c780a46STrond Myklebust fl->fl_start = start; /* we record the absolute position */ 3474c780a46STrond Myklebust if (fl->fl_end < fl->fl_start) 3484c780a46STrond Myklebust return -EOVERFLOW; 3491da177e4SLinus Torvalds 3501da177e4SLinus Torvalds fl->fl_owner = current->files; 3511da177e4SLinus Torvalds fl->fl_pid = current->tgid; 3521da177e4SLinus Torvalds fl->fl_file = filp; 3531da177e4SLinus Torvalds fl->fl_flags = FL_POSIX; 3541da177e4SLinus Torvalds fl->fl_ops = NULL; 3551da177e4SLinus Torvalds fl->fl_lmops = NULL; 3561da177e4SLinus Torvalds 3571da177e4SLinus Torvalds return assign_type(fl, l->l_type); 3581da177e4SLinus Torvalds } 3591da177e4SLinus Torvalds 3601da177e4SLinus Torvalds #if BITS_PER_LONG == 32 3611da177e4SLinus Torvalds static int flock64_to_posix_lock(struct file *filp, struct file_lock *fl, 3621da177e4SLinus Torvalds struct flock64 *l) 3631da177e4SLinus Torvalds { 3641da177e4SLinus Torvalds loff_t start; 3651da177e4SLinus Torvalds 3661da177e4SLinus Torvalds switch (l->l_whence) { 367f5579f8cSJosef 'Jeff' Sipek case SEEK_SET: 3681da177e4SLinus Torvalds start = 0; 3691da177e4SLinus Torvalds break; 370f5579f8cSJosef 'Jeff' Sipek case SEEK_CUR: 3711da177e4SLinus Torvalds start = filp->f_pos; 3721da177e4SLinus Torvalds break; 373f5579f8cSJosef 'Jeff' Sipek case SEEK_END: 3741da177e4SLinus Torvalds start = i_size_read(filp->f_dentry->d_inode); 3751da177e4SLinus Torvalds break; 3761da177e4SLinus Torvalds default: 3771da177e4SLinus Torvalds return -EINVAL; 3781da177e4SLinus Torvalds } 3791da177e4SLinus Torvalds 3804c780a46STrond Myklebust start += l->l_start; 3814c780a46STrond Myklebust if (start < 0) 3821da177e4SLinus Torvalds return -EINVAL; 3831da177e4SLinus Torvalds fl->fl_end = OFFSET_MAX; 3844c780a46STrond Myklebust if (l->l_len > 0) { 3854c780a46STrond Myklebust fl->fl_end = start + l->l_len - 1; 3864c780a46STrond Myklebust } else if (l->l_len < 0) { 3874c780a46STrond Myklebust fl->fl_end = start - 1; 3884c780a46STrond Myklebust start += l->l_len; 3894c780a46STrond Myklebust if (start < 0) 3904c780a46STrond Myklebust return -EINVAL; 3914c780a46STrond Myklebust } 3924c780a46STrond Myklebust fl->fl_start = start; /* we record the absolute position */ 3934c780a46STrond Myklebust if (fl->fl_end < fl->fl_start) 3944c780a46STrond Myklebust return -EOVERFLOW; 3951da177e4SLinus Torvalds 3961da177e4SLinus Torvalds fl->fl_owner = current->files; 3971da177e4SLinus Torvalds fl->fl_pid = current->tgid; 3981da177e4SLinus Torvalds fl->fl_file = filp; 3991da177e4SLinus Torvalds fl->fl_flags = FL_POSIX; 4001da177e4SLinus Torvalds fl->fl_ops = NULL; 4011da177e4SLinus Torvalds fl->fl_lmops = NULL; 4021da177e4SLinus Torvalds 4031da177e4SLinus Torvalds switch (l->l_type) { 4041da177e4SLinus Torvalds case F_RDLCK: 4051da177e4SLinus Torvalds case F_WRLCK: 4061da177e4SLinus Torvalds case F_UNLCK: 4071da177e4SLinus Torvalds fl->fl_type = l->l_type; 4081da177e4SLinus Torvalds break; 4091da177e4SLinus Torvalds default: 4101da177e4SLinus Torvalds return -EINVAL; 4111da177e4SLinus Torvalds } 4121da177e4SLinus Torvalds 4131da177e4SLinus Torvalds return (0); 4141da177e4SLinus Torvalds } 4151da177e4SLinus Torvalds #endif 4161da177e4SLinus Torvalds 4171da177e4SLinus Torvalds /* default lease lock manager operations */ 4181da177e4SLinus Torvalds static void lease_break_callback(struct file_lock *fl) 4191da177e4SLinus Torvalds { 4201da177e4SLinus Torvalds kill_fasync(&fl->fl_fasync, SIGIO, POLL_MSG); 4211da177e4SLinus Torvalds } 4221da177e4SLinus Torvalds 4231da177e4SLinus Torvalds static void lease_release_private_callback(struct file_lock *fl) 4241da177e4SLinus Torvalds { 4251da177e4SLinus Torvalds if (!fl->fl_file) 4261da177e4SLinus Torvalds return; 4271da177e4SLinus Torvalds 4281da177e4SLinus Torvalds f_delown(fl->fl_file); 4291da177e4SLinus Torvalds fl->fl_file->f_owner.signum = 0; 4301da177e4SLinus Torvalds } 4311da177e4SLinus Torvalds 43275c96f85SAdrian Bunk static int lease_mylease_callback(struct file_lock *fl, struct file_lock *try) 4331da177e4SLinus Torvalds { 4341da177e4SLinus Torvalds return fl->fl_file == try->fl_file; 4351da177e4SLinus Torvalds } 4361da177e4SLinus Torvalds 43775c96f85SAdrian Bunk static struct lock_manager_operations lease_manager_ops = { 4381da177e4SLinus Torvalds .fl_break = lease_break_callback, 4391da177e4SLinus Torvalds .fl_release_private = lease_release_private_callback, 4401da177e4SLinus Torvalds .fl_mylease = lease_mylease_callback, 4411da177e4SLinus Torvalds .fl_change = lease_modify, 4421da177e4SLinus Torvalds }; 4431da177e4SLinus Torvalds 4441da177e4SLinus Torvalds /* 4451da177e4SLinus Torvalds * Initialize a lease, use the default lock manager operations 4461da177e4SLinus Torvalds */ 4471da177e4SLinus Torvalds static int lease_init(struct file *filp, int type, struct file_lock *fl) 4481da177e4SLinus Torvalds { 44975dff55aSTrond Myklebust if (assign_type(fl, type) != 0) 45075dff55aSTrond Myklebust return -EINVAL; 45175dff55aSTrond Myklebust 4521da177e4SLinus Torvalds fl->fl_owner = current->files; 4531da177e4SLinus Torvalds fl->fl_pid = current->tgid; 4541da177e4SLinus Torvalds 4551da177e4SLinus Torvalds fl->fl_file = filp; 4561da177e4SLinus Torvalds fl->fl_flags = FL_LEASE; 4571da177e4SLinus Torvalds fl->fl_start = 0; 4581da177e4SLinus Torvalds fl->fl_end = OFFSET_MAX; 4591da177e4SLinus Torvalds fl->fl_ops = NULL; 4601da177e4SLinus Torvalds fl->fl_lmops = &lease_manager_ops; 4611da177e4SLinus Torvalds return 0; 4621da177e4SLinus Torvalds } 4631da177e4SLinus Torvalds 4641da177e4SLinus Torvalds /* Allocate a file_lock initialised to this type of lease */ 4651da177e4SLinus Torvalds static int lease_alloc(struct file *filp, int type, struct file_lock **flp) 4661da177e4SLinus Torvalds { 4671da177e4SLinus Torvalds struct file_lock *fl = locks_alloc_lock(); 46875dff55aSTrond Myklebust int error = -ENOMEM; 4691da177e4SLinus Torvalds 4701da177e4SLinus Torvalds if (fl == NULL) 47175dff55aSTrond Myklebust goto out; 4721da177e4SLinus Torvalds 4731da177e4SLinus Torvalds error = lease_init(filp, type, fl); 47475dff55aSTrond Myklebust if (error) { 47575dff55aSTrond Myklebust locks_free_lock(fl); 47675dff55aSTrond Myklebust fl = NULL; 47775dff55aSTrond Myklebust } 47875dff55aSTrond Myklebust out: 4791da177e4SLinus Torvalds *flp = fl; 48075dff55aSTrond Myklebust return error; 4811da177e4SLinus Torvalds } 4821da177e4SLinus Torvalds 4831da177e4SLinus Torvalds /* Check if two locks overlap each other. 4841da177e4SLinus Torvalds */ 4851da177e4SLinus Torvalds static inline int locks_overlap(struct file_lock *fl1, struct file_lock *fl2) 4861da177e4SLinus Torvalds { 4871da177e4SLinus Torvalds return ((fl1->fl_end >= fl2->fl_start) && 4881da177e4SLinus Torvalds (fl2->fl_end >= fl1->fl_start)); 4891da177e4SLinus Torvalds } 4901da177e4SLinus Torvalds 4911da177e4SLinus Torvalds /* 4921da177e4SLinus Torvalds * Check whether two locks have the same owner. 4931da177e4SLinus Torvalds */ 49433443c42SMatt Mackall static int posix_same_owner(struct file_lock *fl1, struct file_lock *fl2) 4951da177e4SLinus Torvalds { 4961da177e4SLinus Torvalds if (fl1->fl_lmops && fl1->fl_lmops->fl_compare_owner) 4971da177e4SLinus Torvalds return fl2->fl_lmops == fl1->fl_lmops && 4981da177e4SLinus Torvalds fl1->fl_lmops->fl_compare_owner(fl1, fl2); 4991da177e4SLinus Torvalds return fl1->fl_owner == fl2->fl_owner; 5001da177e4SLinus Torvalds } 5011da177e4SLinus Torvalds 5021da177e4SLinus Torvalds /* Remove waiter from blocker's block list. 5031da177e4SLinus Torvalds * When blocker ends up pointing to itself then the list is empty. 5041da177e4SLinus Torvalds */ 50533443c42SMatt Mackall static void __locks_delete_block(struct file_lock *waiter) 5061da177e4SLinus Torvalds { 5071da177e4SLinus Torvalds list_del_init(&waiter->fl_block); 5081da177e4SLinus Torvalds list_del_init(&waiter->fl_link); 5091da177e4SLinus Torvalds waiter->fl_next = NULL; 5101da177e4SLinus Torvalds } 5111da177e4SLinus Torvalds 5121da177e4SLinus Torvalds /* 5131da177e4SLinus Torvalds */ 5141da177e4SLinus Torvalds static void locks_delete_block(struct file_lock *waiter) 5151da177e4SLinus Torvalds { 5161da177e4SLinus Torvalds lock_kernel(); 5171da177e4SLinus Torvalds __locks_delete_block(waiter); 5181da177e4SLinus Torvalds unlock_kernel(); 5191da177e4SLinus Torvalds } 5201da177e4SLinus Torvalds 5211da177e4SLinus Torvalds /* Insert waiter into blocker's block list. 5221da177e4SLinus Torvalds * We use a circular list so that processes can be easily woken up in 5231da177e4SLinus Torvalds * the order they blocked. The documentation doesn't require this but 5241da177e4SLinus Torvalds * it seems like the reasonable thing to do. 5251da177e4SLinus Torvalds */ 5261da177e4SLinus Torvalds static void locks_insert_block(struct file_lock *blocker, 5271da177e4SLinus Torvalds struct file_lock *waiter) 5281da177e4SLinus Torvalds { 5296dc0fe8fSJ. Bruce Fields BUG_ON(!list_empty(&waiter->fl_block)); 5301da177e4SLinus Torvalds list_add_tail(&waiter->fl_block, &blocker->fl_block); 5311da177e4SLinus Torvalds waiter->fl_next = blocker; 5321da177e4SLinus Torvalds if (IS_POSIX(blocker)) 5331da177e4SLinus Torvalds list_add(&waiter->fl_link, &blocked_list); 5341da177e4SLinus Torvalds } 5351da177e4SLinus Torvalds 5361da177e4SLinus Torvalds /* Wake up processes blocked waiting for blocker. 5371da177e4SLinus Torvalds * If told to wait then schedule the processes until the block list 5381da177e4SLinus Torvalds * is empty, otherwise empty the block list ourselves. 5391da177e4SLinus Torvalds */ 5401da177e4SLinus Torvalds static void locks_wake_up_blocks(struct file_lock *blocker) 5411da177e4SLinus Torvalds { 5421da177e4SLinus Torvalds while (!list_empty(&blocker->fl_block)) { 5431da177e4SLinus Torvalds struct file_lock *waiter = list_entry(blocker->fl_block.next, 5441da177e4SLinus Torvalds struct file_lock, fl_block); 5451da177e4SLinus Torvalds __locks_delete_block(waiter); 5461da177e4SLinus Torvalds if (waiter->fl_lmops && waiter->fl_lmops->fl_notify) 5471da177e4SLinus Torvalds waiter->fl_lmops->fl_notify(waiter); 5481da177e4SLinus Torvalds else 5491da177e4SLinus Torvalds wake_up(&waiter->fl_wait); 5501da177e4SLinus Torvalds } 5511da177e4SLinus Torvalds } 5521da177e4SLinus Torvalds 5531da177e4SLinus Torvalds /* Insert file lock fl into an inode's lock list at the position indicated 5541da177e4SLinus Torvalds * by pos. At the same time add the lock to the global file lock list. 5551da177e4SLinus Torvalds */ 5561da177e4SLinus Torvalds static void locks_insert_lock(struct file_lock **pos, struct file_lock *fl) 5571da177e4SLinus Torvalds { 5581da177e4SLinus Torvalds list_add(&fl->fl_link, &file_lock_list); 5591da177e4SLinus Torvalds 5601da177e4SLinus Torvalds /* insert into file's list */ 5611da177e4SLinus Torvalds fl->fl_next = *pos; 5621da177e4SLinus Torvalds *pos = fl; 5631da177e4SLinus Torvalds 5641da177e4SLinus Torvalds if (fl->fl_ops && fl->fl_ops->fl_insert) 5651da177e4SLinus Torvalds fl->fl_ops->fl_insert(fl); 5661da177e4SLinus Torvalds } 5671da177e4SLinus Torvalds 5681da177e4SLinus Torvalds /* 5691da177e4SLinus Torvalds * Delete a lock and then free it. 5701da177e4SLinus Torvalds * Wake up processes that are blocked waiting for this lock, 5711da177e4SLinus Torvalds * notify the FS that the lock has been cleared and 5721da177e4SLinus Torvalds * finally free the lock. 5731da177e4SLinus Torvalds */ 5741da177e4SLinus Torvalds static void locks_delete_lock(struct file_lock **thisfl_p) 5751da177e4SLinus Torvalds { 5761da177e4SLinus Torvalds struct file_lock *fl = *thisfl_p; 5771da177e4SLinus Torvalds 5781da177e4SLinus Torvalds *thisfl_p = fl->fl_next; 5791da177e4SLinus Torvalds fl->fl_next = NULL; 5801da177e4SLinus Torvalds list_del_init(&fl->fl_link); 5811da177e4SLinus Torvalds 5821da177e4SLinus Torvalds fasync_helper(0, fl->fl_file, 0, &fl->fl_fasync); 5831da177e4SLinus Torvalds if (fl->fl_fasync != NULL) { 5841da177e4SLinus Torvalds printk(KERN_ERR "locks_delete_lock: fasync == %p\n", fl->fl_fasync); 5851da177e4SLinus Torvalds fl->fl_fasync = NULL; 5861da177e4SLinus Torvalds } 5871da177e4SLinus Torvalds 5881da177e4SLinus Torvalds if (fl->fl_ops && fl->fl_ops->fl_remove) 5891da177e4SLinus Torvalds fl->fl_ops->fl_remove(fl); 5901da177e4SLinus Torvalds 5911da177e4SLinus Torvalds locks_wake_up_blocks(fl); 5921da177e4SLinus Torvalds locks_free_lock(fl); 5931da177e4SLinus Torvalds } 5941da177e4SLinus Torvalds 5951da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. Common functionality 5961da177e4SLinus Torvalds * checks for shared/exclusive status of overlapping locks. 5971da177e4SLinus Torvalds */ 5981da177e4SLinus Torvalds static int locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl) 5991da177e4SLinus Torvalds { 6001da177e4SLinus Torvalds if (sys_fl->fl_type == F_WRLCK) 6011da177e4SLinus Torvalds return 1; 6021da177e4SLinus Torvalds if (caller_fl->fl_type == F_WRLCK) 6031da177e4SLinus Torvalds return 1; 6041da177e4SLinus Torvalds return 0; 6051da177e4SLinus Torvalds } 6061da177e4SLinus Torvalds 6071da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. POSIX specific 6081da177e4SLinus Torvalds * checking before calling the locks_conflict(). 6091da177e4SLinus Torvalds */ 6101da177e4SLinus Torvalds static int posix_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl) 6111da177e4SLinus Torvalds { 6121da177e4SLinus Torvalds /* POSIX locks owned by the same process do not conflict with 6131da177e4SLinus Torvalds * each other. 6141da177e4SLinus Torvalds */ 6151da177e4SLinus Torvalds if (!IS_POSIX(sys_fl) || posix_same_owner(caller_fl, sys_fl)) 6161da177e4SLinus Torvalds return (0); 6171da177e4SLinus Torvalds 6181da177e4SLinus Torvalds /* Check whether they overlap */ 6191da177e4SLinus Torvalds if (!locks_overlap(caller_fl, sys_fl)) 6201da177e4SLinus Torvalds return 0; 6211da177e4SLinus Torvalds 6221da177e4SLinus Torvalds return (locks_conflict(caller_fl, sys_fl)); 6231da177e4SLinus Torvalds } 6241da177e4SLinus Torvalds 6251da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. FLOCK specific 6261da177e4SLinus Torvalds * checking before calling the locks_conflict(). 6271da177e4SLinus Torvalds */ 6281da177e4SLinus Torvalds static int flock_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl) 6291da177e4SLinus Torvalds { 6301da177e4SLinus Torvalds /* FLOCK locks referring to the same filp do not conflict with 6311da177e4SLinus Torvalds * each other. 6321da177e4SLinus Torvalds */ 6331da177e4SLinus Torvalds if (!IS_FLOCK(sys_fl) || (caller_fl->fl_file == sys_fl->fl_file)) 6341da177e4SLinus Torvalds return (0); 6351da177e4SLinus Torvalds if ((caller_fl->fl_type & LOCK_MAND) || (sys_fl->fl_type & LOCK_MAND)) 6361da177e4SLinus Torvalds return 0; 6371da177e4SLinus Torvalds 6381da177e4SLinus Torvalds return (locks_conflict(caller_fl, sys_fl)); 6391da177e4SLinus Torvalds } 6401da177e4SLinus Torvalds 6411da177e4SLinus Torvalds static int interruptible_sleep_on_locked(wait_queue_head_t *fl_wait, int timeout) 6421da177e4SLinus Torvalds { 6431da177e4SLinus Torvalds int result = 0; 6441da177e4SLinus Torvalds DECLARE_WAITQUEUE(wait, current); 6451da177e4SLinus Torvalds 6461da177e4SLinus Torvalds __set_current_state(TASK_INTERRUPTIBLE); 6471da177e4SLinus Torvalds add_wait_queue(fl_wait, &wait); 6481da177e4SLinus Torvalds if (timeout == 0) 6491da177e4SLinus Torvalds schedule(); 6501da177e4SLinus Torvalds else 6511da177e4SLinus Torvalds result = schedule_timeout(timeout); 6521da177e4SLinus Torvalds if (signal_pending(current)) 6531da177e4SLinus Torvalds result = -ERESTARTSYS; 6541da177e4SLinus Torvalds remove_wait_queue(fl_wait, &wait); 6551da177e4SLinus Torvalds __set_current_state(TASK_RUNNING); 6561da177e4SLinus Torvalds return result; 6571da177e4SLinus Torvalds } 6581da177e4SLinus Torvalds 6591da177e4SLinus Torvalds static int locks_block_on_timeout(struct file_lock *blocker, struct file_lock *waiter, int time) 6601da177e4SLinus Torvalds { 6611da177e4SLinus Torvalds int result; 6621da177e4SLinus Torvalds locks_insert_block(blocker, waiter); 6631da177e4SLinus Torvalds result = interruptible_sleep_on_locked(&waiter->fl_wait, time); 6641da177e4SLinus Torvalds __locks_delete_block(waiter); 6651da177e4SLinus Torvalds return result; 6661da177e4SLinus Torvalds } 6671da177e4SLinus Torvalds 6688dc7c311SAndy Adamson int 6698dc7c311SAndy Adamson posix_test_lock(struct file *filp, struct file_lock *fl, 6708dc7c311SAndy Adamson struct file_lock *conflock) 6711da177e4SLinus Torvalds { 6721da177e4SLinus Torvalds struct file_lock *cfl; 6731da177e4SLinus Torvalds 6741da177e4SLinus Torvalds lock_kernel(); 6751da177e4SLinus Torvalds for (cfl = filp->f_dentry->d_inode->i_flock; cfl; cfl = cfl->fl_next) { 6761da177e4SLinus Torvalds if (!IS_POSIX(cfl)) 6771da177e4SLinus Torvalds continue; 6781da177e4SLinus Torvalds if (posix_locks_conflict(cfl, fl)) 6791da177e4SLinus Torvalds break; 6801da177e4SLinus Torvalds } 6818dc7c311SAndy Adamson if (cfl) { 6820996905fSTrond Myklebust __locks_copy_lock(conflock, cfl); 6831da177e4SLinus Torvalds unlock_kernel(); 6848dc7c311SAndy Adamson return 1; 6858dc7c311SAndy Adamson } 6868dc7c311SAndy Adamson unlock_kernel(); 6878dc7c311SAndy Adamson return 0; 6881da177e4SLinus Torvalds } 6891da177e4SLinus Torvalds 6901da177e4SLinus Torvalds EXPORT_SYMBOL(posix_test_lock); 6911da177e4SLinus Torvalds 6921da177e4SLinus Torvalds /* This function tests for deadlock condition before putting a process to 6931da177e4SLinus Torvalds * sleep. The detection scheme is no longer recursive. Recursive was neat, 6941da177e4SLinus Torvalds * but dangerous - we risked stack corruption if the lock data was bad, or 6951da177e4SLinus Torvalds * if the recursion was too deep for any other reason. 6961da177e4SLinus Torvalds * 6971da177e4SLinus Torvalds * We rely on the fact that a task can only be on one lock's wait queue 6981da177e4SLinus Torvalds * at a time. When we find blocked_task on a wait queue we can re-search 6991da177e4SLinus Torvalds * with blocked_task equal to that queue's owner, until either blocked_task 7001da177e4SLinus Torvalds * isn't found, or blocked_task is found on a queue owned by my_task. 7011da177e4SLinus Torvalds * 7021da177e4SLinus Torvalds * Note: the above assumption may not be true when handling lock requests 7031da177e4SLinus Torvalds * from a broken NFS client. But broken NFS clients have a lot more to 7041da177e4SLinus Torvalds * worry about than proper deadlock detection anyway... --okir 7051da177e4SLinus Torvalds */ 706b0904e14SAdrian Bunk static int posix_locks_deadlock(struct file_lock *caller_fl, 7071da177e4SLinus Torvalds struct file_lock *block_fl) 7081da177e4SLinus Torvalds { 7091da177e4SLinus Torvalds struct list_head *tmp; 7101da177e4SLinus Torvalds 7111da177e4SLinus Torvalds next_task: 7121da177e4SLinus Torvalds if (posix_same_owner(caller_fl, block_fl)) 7131da177e4SLinus Torvalds return 1; 7141da177e4SLinus Torvalds list_for_each(tmp, &blocked_list) { 7151da177e4SLinus Torvalds struct file_lock *fl = list_entry(tmp, struct file_lock, fl_link); 7161da177e4SLinus Torvalds if (posix_same_owner(fl, block_fl)) { 7171da177e4SLinus Torvalds fl = fl->fl_next; 7181da177e4SLinus Torvalds block_fl = fl; 7191da177e4SLinus Torvalds goto next_task; 7201da177e4SLinus Torvalds } 7211da177e4SLinus Torvalds } 7221da177e4SLinus Torvalds return 0; 7231da177e4SLinus Torvalds } 7241da177e4SLinus Torvalds 7251da177e4SLinus Torvalds /* Try to create a FLOCK lock on filp. We always insert new FLOCK locks 7261da177e4SLinus Torvalds * at the head of the list, but that's secret knowledge known only to 7271da177e4SLinus Torvalds * flock_lock_file and posix_lock_file. 728f475ae95STrond Myklebust * 729f475ae95STrond Myklebust * Note that if called with an FL_EXISTS argument, the caller may determine 730f475ae95STrond Myklebust * whether or not a lock was successfully freed by testing the return 731f475ae95STrond Myklebust * value for -ENOENT. 7321da177e4SLinus Torvalds */ 733993dfa87STrond Myklebust static int flock_lock_file(struct file *filp, struct file_lock *request) 7341da177e4SLinus Torvalds { 735993dfa87STrond Myklebust struct file_lock *new_fl = NULL; 7361da177e4SLinus Torvalds struct file_lock **before; 7371da177e4SLinus Torvalds struct inode * inode = filp->f_dentry->d_inode; 7381da177e4SLinus Torvalds int error = 0; 7391da177e4SLinus Torvalds int found = 0; 7401da177e4SLinus Torvalds 7411da177e4SLinus Torvalds lock_kernel(); 742f07f18ddSTrond Myklebust if (request->fl_flags & FL_ACCESS) 743f07f18ddSTrond Myklebust goto find_conflict; 7441da177e4SLinus Torvalds for_each_lock(inode, before) { 7451da177e4SLinus Torvalds struct file_lock *fl = *before; 7461da177e4SLinus Torvalds if (IS_POSIX(fl)) 7471da177e4SLinus Torvalds break; 7481da177e4SLinus Torvalds if (IS_LEASE(fl)) 7491da177e4SLinus Torvalds continue; 7501da177e4SLinus Torvalds if (filp != fl->fl_file) 7511da177e4SLinus Torvalds continue; 752993dfa87STrond Myklebust if (request->fl_type == fl->fl_type) 7531da177e4SLinus Torvalds goto out; 7541da177e4SLinus Torvalds found = 1; 7551da177e4SLinus Torvalds locks_delete_lock(before); 7561da177e4SLinus Torvalds break; 7571da177e4SLinus Torvalds } 7581da177e4SLinus Torvalds 759f475ae95STrond Myklebust if (request->fl_type == F_UNLCK) { 760f475ae95STrond Myklebust if ((request->fl_flags & FL_EXISTS) && !found) 761f475ae95STrond Myklebust error = -ENOENT; 762993dfa87STrond Myklebust goto out; 763f475ae95STrond Myklebust } 7641da177e4SLinus Torvalds 7659cedc194SKirill Korotaev error = -ENOMEM; 766993dfa87STrond Myklebust new_fl = locks_alloc_lock(); 767993dfa87STrond Myklebust if (new_fl == NULL) 768993dfa87STrond Myklebust goto out; 7691da177e4SLinus Torvalds /* 7701da177e4SLinus Torvalds * If a higher-priority process was blocked on the old file lock, 7711da177e4SLinus Torvalds * give it the opportunity to lock the file. 7721da177e4SLinus Torvalds */ 7731da177e4SLinus Torvalds if (found) 7741da177e4SLinus Torvalds cond_resched(); 7751da177e4SLinus Torvalds 776f07f18ddSTrond Myklebust find_conflict: 7771da177e4SLinus Torvalds for_each_lock(inode, before) { 7781da177e4SLinus Torvalds struct file_lock *fl = *before; 7791da177e4SLinus Torvalds if (IS_POSIX(fl)) 7801da177e4SLinus Torvalds break; 7811da177e4SLinus Torvalds if (IS_LEASE(fl)) 7821da177e4SLinus Torvalds continue; 783993dfa87STrond Myklebust if (!flock_locks_conflict(request, fl)) 7841da177e4SLinus Torvalds continue; 7851da177e4SLinus Torvalds error = -EAGAIN; 786993dfa87STrond Myklebust if (request->fl_flags & FL_SLEEP) 787993dfa87STrond Myklebust locks_insert_block(fl, request); 7881da177e4SLinus Torvalds goto out; 7891da177e4SLinus Torvalds } 790f07f18ddSTrond Myklebust if (request->fl_flags & FL_ACCESS) 791f07f18ddSTrond Myklebust goto out; 792993dfa87STrond Myklebust locks_copy_lock(new_fl, request); 7931da177e4SLinus Torvalds locks_insert_lock(&inode->i_flock, new_fl); 794993dfa87STrond Myklebust new_fl = NULL; 7959cedc194SKirill Korotaev error = 0; 7961da177e4SLinus Torvalds 7971da177e4SLinus Torvalds out: 7981da177e4SLinus Torvalds unlock_kernel(); 799993dfa87STrond Myklebust if (new_fl) 800993dfa87STrond Myklebust locks_free_lock(new_fl); 8011da177e4SLinus Torvalds return error; 8021da177e4SLinus Torvalds } 8031da177e4SLinus Torvalds 8045842add2SAndy Adamson static int __posix_lock_file_conf(struct inode *inode, struct file_lock *request, struct file_lock *conflock) 8051da177e4SLinus Torvalds { 8061da177e4SLinus Torvalds struct file_lock *fl; 80739005d02SMiklos Szeredi struct file_lock *new_fl = NULL; 80839005d02SMiklos Szeredi struct file_lock *new_fl2 = NULL; 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. 81739005d02SMiklos Szeredi * 81839005d02SMiklos Szeredi * In some cases we can be sure, that no new locks will be needed 8191da177e4SLinus Torvalds */ 82039005d02SMiklos Szeredi if (!(request->fl_flags & FL_ACCESS) && 82139005d02SMiklos Szeredi (request->fl_type != F_UNLCK || 82239005d02SMiklos Szeredi request->fl_start != 0 || request->fl_end != OFFSET_MAX)) { 8231da177e4SLinus Torvalds new_fl = locks_alloc_lock(); 8241da177e4SLinus Torvalds new_fl2 = locks_alloc_lock(); 82539005d02SMiklos Szeredi } 8261da177e4SLinus Torvalds 8271da177e4SLinus Torvalds lock_kernel(); 8281da177e4SLinus Torvalds if (request->fl_type != F_UNLCK) { 8291da177e4SLinus Torvalds for_each_lock(inode, before) { 8301da177e4SLinus Torvalds struct file_lock *fl = *before; 8311da177e4SLinus Torvalds if (!IS_POSIX(fl)) 8321da177e4SLinus Torvalds continue; 8331da177e4SLinus Torvalds if (!posix_locks_conflict(request, fl)) 8341da177e4SLinus Torvalds continue; 8355842add2SAndy Adamson if (conflock) 8365842add2SAndy Adamson locks_copy_lock(conflock, fl); 8371da177e4SLinus Torvalds error = -EAGAIN; 8381da177e4SLinus Torvalds if (!(request->fl_flags & FL_SLEEP)) 8391da177e4SLinus Torvalds goto out; 8401da177e4SLinus Torvalds error = -EDEADLK; 8411da177e4SLinus Torvalds if (posix_locks_deadlock(request, fl)) 8421da177e4SLinus Torvalds goto out; 8431da177e4SLinus Torvalds error = -EAGAIN; 8441da177e4SLinus Torvalds locks_insert_block(fl, request); 8451da177e4SLinus Torvalds goto out; 8461da177e4SLinus Torvalds } 8471da177e4SLinus Torvalds } 8481da177e4SLinus Torvalds 8491da177e4SLinus Torvalds /* If we're just looking for a conflict, we're done. */ 8501da177e4SLinus Torvalds error = 0; 8511da177e4SLinus Torvalds if (request->fl_flags & FL_ACCESS) 8521da177e4SLinus Torvalds goto out; 8531da177e4SLinus Torvalds 8541da177e4SLinus Torvalds /* 8551da177e4SLinus Torvalds * Find the first old lock with the same owner as the new lock. 8561da177e4SLinus Torvalds */ 8571da177e4SLinus Torvalds 8581da177e4SLinus Torvalds before = &inode->i_flock; 8591da177e4SLinus Torvalds 8601da177e4SLinus Torvalds /* First skip locks owned by other processes. */ 8611da177e4SLinus Torvalds while ((fl = *before) && (!IS_POSIX(fl) || 8621da177e4SLinus Torvalds !posix_same_owner(request, fl))) { 8631da177e4SLinus Torvalds before = &fl->fl_next; 8641da177e4SLinus Torvalds } 8651da177e4SLinus Torvalds 8661da177e4SLinus Torvalds /* Process locks with this owner. */ 8671da177e4SLinus Torvalds while ((fl = *before) && posix_same_owner(request, fl)) { 8681da177e4SLinus Torvalds /* Detect adjacent or overlapping regions (if same lock type) 8691da177e4SLinus Torvalds */ 8701da177e4SLinus Torvalds if (request->fl_type == fl->fl_type) { 871449231d6SOlaf Kirch /* In all comparisons of start vs end, use 872449231d6SOlaf Kirch * "start - 1" rather than "end + 1". If end 873449231d6SOlaf Kirch * is OFFSET_MAX, end + 1 will become negative. 874449231d6SOlaf Kirch */ 8751da177e4SLinus Torvalds if (fl->fl_end < request->fl_start - 1) 8761da177e4SLinus Torvalds goto next_lock; 8771da177e4SLinus Torvalds /* If the next lock in the list has entirely bigger 8781da177e4SLinus Torvalds * addresses than the new one, insert the lock here. 8791da177e4SLinus Torvalds */ 880449231d6SOlaf Kirch if (fl->fl_start - 1 > request->fl_end) 8811da177e4SLinus Torvalds break; 8821da177e4SLinus Torvalds 8831da177e4SLinus Torvalds /* If we come here, the new and old lock are of the 8841da177e4SLinus Torvalds * same type and adjacent or overlapping. Make one 8851da177e4SLinus Torvalds * lock yielding from the lower start address of both 8861da177e4SLinus Torvalds * locks to the higher end address. 8871da177e4SLinus Torvalds */ 8881da177e4SLinus Torvalds if (fl->fl_start > request->fl_start) 8891da177e4SLinus Torvalds fl->fl_start = request->fl_start; 8901da177e4SLinus Torvalds else 8911da177e4SLinus Torvalds request->fl_start = fl->fl_start; 8921da177e4SLinus Torvalds if (fl->fl_end < request->fl_end) 8931da177e4SLinus Torvalds fl->fl_end = request->fl_end; 8941da177e4SLinus Torvalds else 8951da177e4SLinus Torvalds request->fl_end = fl->fl_end; 8961da177e4SLinus Torvalds if (added) { 8971da177e4SLinus Torvalds locks_delete_lock(before); 8981da177e4SLinus Torvalds continue; 8991da177e4SLinus Torvalds } 9001da177e4SLinus Torvalds request = fl; 9011da177e4SLinus Torvalds added = 1; 9021da177e4SLinus Torvalds } 9031da177e4SLinus Torvalds else { 9041da177e4SLinus Torvalds /* Processing for different lock types is a bit 9051da177e4SLinus Torvalds * more complex. 9061da177e4SLinus Torvalds */ 9071da177e4SLinus Torvalds if (fl->fl_end < request->fl_start) 9081da177e4SLinus Torvalds goto next_lock; 9091da177e4SLinus Torvalds if (fl->fl_start > request->fl_end) 9101da177e4SLinus Torvalds break; 9111da177e4SLinus Torvalds if (request->fl_type == F_UNLCK) 9121da177e4SLinus Torvalds added = 1; 9131da177e4SLinus Torvalds if (fl->fl_start < request->fl_start) 9141da177e4SLinus Torvalds left = fl; 9151da177e4SLinus Torvalds /* If the next lock in the list has a higher end 9161da177e4SLinus Torvalds * address than the new one, insert the new one here. 9171da177e4SLinus Torvalds */ 9181da177e4SLinus Torvalds if (fl->fl_end > request->fl_end) { 9191da177e4SLinus Torvalds right = fl; 9201da177e4SLinus Torvalds break; 9211da177e4SLinus Torvalds } 9221da177e4SLinus Torvalds if (fl->fl_start >= request->fl_start) { 9231da177e4SLinus Torvalds /* The new lock completely replaces an old 9241da177e4SLinus Torvalds * one (This may happen several times). 9251da177e4SLinus Torvalds */ 9261da177e4SLinus Torvalds if (added) { 9271da177e4SLinus Torvalds locks_delete_lock(before); 9281da177e4SLinus Torvalds continue; 9291da177e4SLinus Torvalds } 9301da177e4SLinus Torvalds /* Replace the old lock with the new one. 9311da177e4SLinus Torvalds * Wake up anybody waiting for the old one, 9321da177e4SLinus Torvalds * as the change in lock type might satisfy 9331da177e4SLinus Torvalds * their needs. 9341da177e4SLinus Torvalds */ 9351da177e4SLinus Torvalds locks_wake_up_blocks(fl); 9361da177e4SLinus Torvalds fl->fl_start = request->fl_start; 9371da177e4SLinus Torvalds fl->fl_end = request->fl_end; 9381da177e4SLinus Torvalds fl->fl_type = request->fl_type; 93947831f35STrond Myklebust locks_release_private(fl); 94047831f35STrond Myklebust locks_copy_private(fl, request); 9411da177e4SLinus Torvalds request = fl; 9421da177e4SLinus Torvalds added = 1; 9431da177e4SLinus Torvalds } 9441da177e4SLinus Torvalds } 9451da177e4SLinus Torvalds /* Go on to next lock. 9461da177e4SLinus Torvalds */ 9471da177e4SLinus Torvalds next_lock: 9481da177e4SLinus Torvalds before = &fl->fl_next; 9491da177e4SLinus Torvalds } 9501da177e4SLinus Torvalds 9510d9a490aSMiklos Szeredi /* 9520d9a490aSMiklos Szeredi * The above code only modifies existing locks in case of 9530d9a490aSMiklos Szeredi * merging or replacing. If new lock(s) need to be inserted 9540d9a490aSMiklos Szeredi * all modifications are done bellow this, so it's safe yet to 9550d9a490aSMiklos Szeredi * bail out. 9560d9a490aSMiklos Szeredi */ 9570d9a490aSMiklos Szeredi error = -ENOLCK; /* "no luck" */ 9580d9a490aSMiklos Szeredi if (right && left == right && !new_fl2) 9590d9a490aSMiklos Szeredi goto out; 9600d9a490aSMiklos Szeredi 9611da177e4SLinus Torvalds error = 0; 9621da177e4SLinus Torvalds if (!added) { 963f475ae95STrond Myklebust if (request->fl_type == F_UNLCK) { 964f475ae95STrond Myklebust if (request->fl_flags & FL_EXISTS) 965f475ae95STrond Myklebust error = -ENOENT; 9661da177e4SLinus Torvalds goto out; 967f475ae95STrond Myklebust } 9680d9a490aSMiklos Szeredi 9690d9a490aSMiklos Szeredi if (!new_fl) { 9700d9a490aSMiklos Szeredi error = -ENOLCK; 9710d9a490aSMiklos Szeredi goto out; 9720d9a490aSMiklos Szeredi } 9731da177e4SLinus Torvalds locks_copy_lock(new_fl, request); 9741da177e4SLinus Torvalds locks_insert_lock(before, new_fl); 9751da177e4SLinus Torvalds new_fl = NULL; 9761da177e4SLinus Torvalds } 9771da177e4SLinus Torvalds if (right) { 9781da177e4SLinus Torvalds if (left == right) { 9791da177e4SLinus Torvalds /* The new lock breaks the old one in two pieces, 9801da177e4SLinus Torvalds * so we have to use the second new lock. 9811da177e4SLinus Torvalds */ 9821da177e4SLinus Torvalds left = new_fl2; 9831da177e4SLinus Torvalds new_fl2 = NULL; 9841da177e4SLinus Torvalds locks_copy_lock(left, right); 9851da177e4SLinus Torvalds locks_insert_lock(before, left); 9861da177e4SLinus Torvalds } 9871da177e4SLinus Torvalds right->fl_start = request->fl_end + 1; 9881da177e4SLinus Torvalds locks_wake_up_blocks(right); 9891da177e4SLinus Torvalds } 9901da177e4SLinus Torvalds if (left) { 9911da177e4SLinus Torvalds left->fl_end = request->fl_start - 1; 9921da177e4SLinus Torvalds locks_wake_up_blocks(left); 9931da177e4SLinus Torvalds } 9941da177e4SLinus Torvalds out: 9951da177e4SLinus Torvalds unlock_kernel(); 9961da177e4SLinus Torvalds /* 9971da177e4SLinus Torvalds * Free any unused locks. 9981da177e4SLinus Torvalds */ 9991da177e4SLinus Torvalds if (new_fl) 10001da177e4SLinus Torvalds locks_free_lock(new_fl); 10011da177e4SLinus Torvalds if (new_fl2) 10021da177e4SLinus Torvalds locks_free_lock(new_fl2); 10031da177e4SLinus Torvalds return error; 10041da177e4SLinus Torvalds } 10051da177e4SLinus Torvalds 10061da177e4SLinus Torvalds /** 10071da177e4SLinus Torvalds * posix_lock_file - Apply a POSIX-style lock to a file 10081da177e4SLinus Torvalds * @filp: The file to apply the lock to 10091da177e4SLinus Torvalds * @fl: The lock to be applied 10101da177e4SLinus Torvalds * 10111da177e4SLinus Torvalds * Add a POSIX style lock to a file. 10121da177e4SLinus Torvalds * We merge adjacent & overlapping locks whenever possible. 10131da177e4SLinus Torvalds * POSIX locks are sorted by owner task, then by starting address 1014f475ae95STrond Myklebust * 1015f475ae95STrond Myklebust * Note that if called with an FL_EXISTS argument, the caller may determine 1016f475ae95STrond Myklebust * whether or not a lock was successfully freed by testing the return 1017f475ae95STrond Myklebust * value for -ENOENT. 10181da177e4SLinus Torvalds */ 10191da177e4SLinus Torvalds int posix_lock_file(struct file *filp, struct file_lock *fl) 10201da177e4SLinus Torvalds { 10215842add2SAndy Adamson return __posix_lock_file_conf(filp->f_dentry->d_inode, fl, NULL); 10221da177e4SLinus Torvalds } 10235842add2SAndy Adamson EXPORT_SYMBOL(posix_lock_file); 10245842add2SAndy Adamson 10255842add2SAndy Adamson /** 10265842add2SAndy Adamson * posix_lock_file_conf - Apply a POSIX-style lock to a file 10275842add2SAndy Adamson * @filp: The file to apply the lock to 10285842add2SAndy Adamson * @fl: The lock to be applied 10295842add2SAndy Adamson * @conflock: Place to return a copy of the conflicting lock, if found. 10305842add2SAndy Adamson * 10315842add2SAndy Adamson * Except for the conflock parameter, acts just like posix_lock_file. 10325842add2SAndy Adamson */ 10335842add2SAndy Adamson int posix_lock_file_conf(struct file *filp, struct file_lock *fl, 10345842add2SAndy Adamson struct file_lock *conflock) 10355842add2SAndy Adamson { 10365842add2SAndy Adamson return __posix_lock_file_conf(filp->f_dentry->d_inode, fl, conflock); 10375842add2SAndy Adamson } 10385842add2SAndy Adamson EXPORT_SYMBOL(posix_lock_file_conf); 10391da177e4SLinus Torvalds 10401da177e4SLinus Torvalds /** 10411da177e4SLinus Torvalds * posix_lock_file_wait - Apply a POSIX-style lock to a file 10421da177e4SLinus Torvalds * @filp: The file to apply the lock to 10431da177e4SLinus Torvalds * @fl: The lock to be applied 10441da177e4SLinus Torvalds * 10451da177e4SLinus Torvalds * Add a POSIX style lock to a file. 10461da177e4SLinus Torvalds * We merge adjacent & overlapping locks whenever possible. 10471da177e4SLinus Torvalds * POSIX locks are sorted by owner task, then by starting address 10481da177e4SLinus Torvalds */ 10491da177e4SLinus Torvalds int posix_lock_file_wait(struct file *filp, struct file_lock *fl) 10501da177e4SLinus Torvalds { 10511da177e4SLinus Torvalds int error; 10521da177e4SLinus Torvalds might_sleep (); 10531da177e4SLinus Torvalds for (;;) { 10545842add2SAndy Adamson error = posix_lock_file(filp, fl); 10551da177e4SLinus Torvalds if ((error != -EAGAIN) || !(fl->fl_flags & FL_SLEEP)) 10561da177e4SLinus Torvalds break; 10571da177e4SLinus Torvalds error = wait_event_interruptible(fl->fl_wait, !fl->fl_next); 10581da177e4SLinus Torvalds if (!error) 10591da177e4SLinus Torvalds continue; 10601da177e4SLinus Torvalds 10611da177e4SLinus Torvalds locks_delete_block(fl); 10621da177e4SLinus Torvalds break; 10631da177e4SLinus Torvalds } 10641da177e4SLinus Torvalds return error; 10651da177e4SLinus Torvalds } 10661da177e4SLinus Torvalds EXPORT_SYMBOL(posix_lock_file_wait); 10671da177e4SLinus Torvalds 10681da177e4SLinus Torvalds /** 10691da177e4SLinus Torvalds * locks_mandatory_locked - Check for an active lock 10701da177e4SLinus Torvalds * @inode: the file to check 10711da177e4SLinus Torvalds * 10721da177e4SLinus Torvalds * Searches the inode's list of locks to find any POSIX locks which conflict. 10731da177e4SLinus Torvalds * This function is called from locks_verify_locked() only. 10741da177e4SLinus Torvalds */ 10751da177e4SLinus Torvalds int locks_mandatory_locked(struct inode *inode) 10761da177e4SLinus Torvalds { 10771da177e4SLinus Torvalds fl_owner_t owner = current->files; 10781da177e4SLinus Torvalds struct file_lock *fl; 10791da177e4SLinus Torvalds 10801da177e4SLinus Torvalds /* 10811da177e4SLinus Torvalds * Search the lock list for this inode for any POSIX locks. 10821da177e4SLinus Torvalds */ 10831da177e4SLinus Torvalds lock_kernel(); 10841da177e4SLinus Torvalds for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) { 10851da177e4SLinus Torvalds if (!IS_POSIX(fl)) 10861da177e4SLinus Torvalds continue; 10871da177e4SLinus Torvalds if (fl->fl_owner != owner) 10881da177e4SLinus Torvalds break; 10891da177e4SLinus Torvalds } 10901da177e4SLinus Torvalds unlock_kernel(); 10911da177e4SLinus Torvalds return fl ? -EAGAIN : 0; 10921da177e4SLinus Torvalds } 10931da177e4SLinus Torvalds 10941da177e4SLinus Torvalds /** 10951da177e4SLinus Torvalds * locks_mandatory_area - Check for a conflicting lock 10961da177e4SLinus Torvalds * @read_write: %FLOCK_VERIFY_WRITE for exclusive access, %FLOCK_VERIFY_READ 10971da177e4SLinus Torvalds * for shared 10981da177e4SLinus Torvalds * @inode: the file to check 10991da177e4SLinus Torvalds * @filp: how the file was opened (if it was) 11001da177e4SLinus Torvalds * @offset: start of area to check 11011da177e4SLinus Torvalds * @count: length of area to check 11021da177e4SLinus Torvalds * 11031da177e4SLinus Torvalds * Searches the inode's list of locks to find any POSIX locks which conflict. 11041da177e4SLinus Torvalds * This function is called from rw_verify_area() and 11051da177e4SLinus Torvalds * locks_verify_truncate(). 11061da177e4SLinus Torvalds */ 11071da177e4SLinus Torvalds int locks_mandatory_area(int read_write, struct inode *inode, 11081da177e4SLinus Torvalds struct file *filp, loff_t offset, 11091da177e4SLinus Torvalds size_t count) 11101da177e4SLinus Torvalds { 11111da177e4SLinus Torvalds struct file_lock fl; 11121da177e4SLinus Torvalds int error; 11131da177e4SLinus Torvalds 11141da177e4SLinus Torvalds locks_init_lock(&fl); 11151da177e4SLinus Torvalds fl.fl_owner = current->files; 11161da177e4SLinus Torvalds fl.fl_pid = current->tgid; 11171da177e4SLinus Torvalds fl.fl_file = filp; 11181da177e4SLinus Torvalds fl.fl_flags = FL_POSIX | FL_ACCESS; 11191da177e4SLinus Torvalds if (filp && !(filp->f_flags & O_NONBLOCK)) 11201da177e4SLinus Torvalds fl.fl_flags |= FL_SLEEP; 11211da177e4SLinus Torvalds fl.fl_type = (read_write == FLOCK_VERIFY_WRITE) ? F_WRLCK : F_RDLCK; 11221da177e4SLinus Torvalds fl.fl_start = offset; 11231da177e4SLinus Torvalds fl.fl_end = offset + count - 1; 11241da177e4SLinus Torvalds 11251da177e4SLinus Torvalds for (;;) { 11265842add2SAndy Adamson error = __posix_lock_file_conf(inode, &fl, NULL); 11271da177e4SLinus Torvalds if (error != -EAGAIN) 11281da177e4SLinus Torvalds break; 11291da177e4SLinus Torvalds if (!(fl.fl_flags & FL_SLEEP)) 11301da177e4SLinus Torvalds break; 11311da177e4SLinus Torvalds error = wait_event_interruptible(fl.fl_wait, !fl.fl_next); 11321da177e4SLinus Torvalds if (!error) { 11331da177e4SLinus Torvalds /* 11341da177e4SLinus Torvalds * If we've been sleeping someone might have 11351da177e4SLinus Torvalds * changed the permissions behind our back. 11361da177e4SLinus Torvalds */ 11371da177e4SLinus Torvalds if ((inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID) 11381da177e4SLinus Torvalds continue; 11391da177e4SLinus Torvalds } 11401da177e4SLinus Torvalds 11411da177e4SLinus Torvalds locks_delete_block(&fl); 11421da177e4SLinus Torvalds break; 11431da177e4SLinus Torvalds } 11441da177e4SLinus Torvalds 11451da177e4SLinus Torvalds return error; 11461da177e4SLinus Torvalds } 11471da177e4SLinus Torvalds 11481da177e4SLinus Torvalds EXPORT_SYMBOL(locks_mandatory_area); 11491da177e4SLinus Torvalds 11501da177e4SLinus Torvalds /* We already had a lease on this file; just change its type */ 11511da177e4SLinus Torvalds int lease_modify(struct file_lock **before, int arg) 11521da177e4SLinus Torvalds { 11531da177e4SLinus Torvalds struct file_lock *fl = *before; 11541da177e4SLinus Torvalds int error = assign_type(fl, arg); 11551da177e4SLinus Torvalds 11561da177e4SLinus Torvalds if (error) 11571da177e4SLinus Torvalds return error; 11581da177e4SLinus Torvalds locks_wake_up_blocks(fl); 11591da177e4SLinus Torvalds if (arg == F_UNLCK) 11601da177e4SLinus Torvalds locks_delete_lock(before); 11611da177e4SLinus Torvalds return 0; 11621da177e4SLinus Torvalds } 11631da177e4SLinus Torvalds 11641da177e4SLinus Torvalds EXPORT_SYMBOL(lease_modify); 11651da177e4SLinus Torvalds 11661da177e4SLinus Torvalds static void time_out_leases(struct inode *inode) 11671da177e4SLinus Torvalds { 11681da177e4SLinus Torvalds struct file_lock **before; 11691da177e4SLinus Torvalds struct file_lock *fl; 11701da177e4SLinus Torvalds 11711da177e4SLinus Torvalds before = &inode->i_flock; 11721da177e4SLinus Torvalds while ((fl = *before) && IS_LEASE(fl) && (fl->fl_type & F_INPROGRESS)) { 11731da177e4SLinus Torvalds if ((fl->fl_break_time == 0) 11741da177e4SLinus Torvalds || time_before(jiffies, fl->fl_break_time)) { 11751da177e4SLinus Torvalds before = &fl->fl_next; 11761da177e4SLinus Torvalds continue; 11771da177e4SLinus Torvalds } 11781da177e4SLinus Torvalds lease_modify(before, fl->fl_type & ~F_INPROGRESS); 11791da177e4SLinus Torvalds if (fl == *before) /* lease_modify may have freed fl */ 11801da177e4SLinus Torvalds before = &fl->fl_next; 11811da177e4SLinus Torvalds } 11821da177e4SLinus Torvalds } 11831da177e4SLinus Torvalds 11841da177e4SLinus Torvalds /** 11851da177e4SLinus Torvalds * __break_lease - revoke all outstanding leases on file 11861da177e4SLinus Torvalds * @inode: the inode of the file to return 11871da177e4SLinus Torvalds * @mode: the open mode (read or write) 11881da177e4SLinus Torvalds * 11891da177e4SLinus Torvalds * break_lease (inlined for speed) has checked there already 11901da177e4SLinus Torvalds * is a lease on this file. Leases are broken on a call to open() 11911da177e4SLinus Torvalds * or truncate(). This function can sleep unless you 11921da177e4SLinus Torvalds * specified %O_NONBLOCK to your open(). 11931da177e4SLinus Torvalds */ 11941da177e4SLinus Torvalds int __break_lease(struct inode *inode, unsigned int mode) 11951da177e4SLinus Torvalds { 11961da177e4SLinus Torvalds int error = 0, future; 11971da177e4SLinus Torvalds struct file_lock *new_fl, *flock; 11981da177e4SLinus Torvalds struct file_lock *fl; 11991da177e4SLinus Torvalds int alloc_err; 12001da177e4SLinus Torvalds unsigned long break_time; 12011da177e4SLinus Torvalds int i_have_this_lease = 0; 12021da177e4SLinus Torvalds 12031da177e4SLinus Torvalds alloc_err = lease_alloc(NULL, mode & FMODE_WRITE ? F_WRLCK : F_RDLCK, 12041da177e4SLinus Torvalds &new_fl); 12051da177e4SLinus Torvalds 12061da177e4SLinus Torvalds lock_kernel(); 12071da177e4SLinus Torvalds 12081da177e4SLinus Torvalds time_out_leases(inode); 12091da177e4SLinus Torvalds 12101da177e4SLinus Torvalds flock = inode->i_flock; 12111da177e4SLinus Torvalds if ((flock == NULL) || !IS_LEASE(flock)) 12121da177e4SLinus Torvalds goto out; 12131da177e4SLinus Torvalds 12141da177e4SLinus Torvalds for (fl = flock; fl && IS_LEASE(fl); fl = fl->fl_next) 12151da177e4SLinus Torvalds if (fl->fl_owner == current->files) 12161da177e4SLinus Torvalds i_have_this_lease = 1; 12171da177e4SLinus Torvalds 12181da177e4SLinus Torvalds if (mode & FMODE_WRITE) { 12191da177e4SLinus Torvalds /* If we want write access, we have to revoke any lease. */ 12201da177e4SLinus Torvalds future = F_UNLCK | F_INPROGRESS; 12211da177e4SLinus Torvalds } else if (flock->fl_type & F_INPROGRESS) { 12221da177e4SLinus Torvalds /* If the lease is already being broken, we just leave it */ 12231da177e4SLinus Torvalds future = flock->fl_type; 12241da177e4SLinus Torvalds } else if (flock->fl_type & F_WRLCK) { 12251da177e4SLinus Torvalds /* Downgrade the exclusive lease to a read-only lease. */ 12261da177e4SLinus Torvalds future = F_RDLCK | F_INPROGRESS; 12271da177e4SLinus Torvalds } else { 12281da177e4SLinus Torvalds /* the existing lease was read-only, so we can read too. */ 12291da177e4SLinus Torvalds goto out; 12301da177e4SLinus Torvalds } 12311da177e4SLinus Torvalds 12321da177e4SLinus Torvalds if (alloc_err && !i_have_this_lease && ((mode & O_NONBLOCK) == 0)) { 12331da177e4SLinus Torvalds error = alloc_err; 12341da177e4SLinus Torvalds goto out; 12351da177e4SLinus Torvalds } 12361da177e4SLinus Torvalds 12371da177e4SLinus Torvalds break_time = 0; 12381da177e4SLinus Torvalds if (lease_break_time > 0) { 12391da177e4SLinus Torvalds break_time = jiffies + lease_break_time * HZ; 12401da177e4SLinus Torvalds if (break_time == 0) 12411da177e4SLinus Torvalds break_time++; /* so that 0 means no break time */ 12421da177e4SLinus Torvalds } 12431da177e4SLinus Torvalds 12441da177e4SLinus Torvalds for (fl = flock; fl && IS_LEASE(fl); fl = fl->fl_next) { 12451da177e4SLinus Torvalds if (fl->fl_type != future) { 12461da177e4SLinus Torvalds fl->fl_type = future; 12471da177e4SLinus Torvalds fl->fl_break_time = break_time; 12481da177e4SLinus Torvalds /* lease must have lmops break callback */ 12491da177e4SLinus Torvalds fl->fl_lmops->fl_break(fl); 12501da177e4SLinus Torvalds } 12511da177e4SLinus Torvalds } 12521da177e4SLinus Torvalds 12531da177e4SLinus Torvalds if (i_have_this_lease || (mode & O_NONBLOCK)) { 12541da177e4SLinus Torvalds error = -EWOULDBLOCK; 12551da177e4SLinus Torvalds goto out; 12561da177e4SLinus Torvalds } 12571da177e4SLinus Torvalds 12581da177e4SLinus Torvalds restart: 12591da177e4SLinus Torvalds break_time = flock->fl_break_time; 12601da177e4SLinus Torvalds if (break_time != 0) { 12611da177e4SLinus Torvalds break_time -= jiffies; 12621da177e4SLinus Torvalds if (break_time == 0) 12631da177e4SLinus Torvalds break_time++; 12641da177e4SLinus Torvalds } 12651da177e4SLinus Torvalds error = locks_block_on_timeout(flock, new_fl, break_time); 12661da177e4SLinus Torvalds if (error >= 0) { 12671da177e4SLinus Torvalds if (error == 0) 12681da177e4SLinus Torvalds time_out_leases(inode); 12691da177e4SLinus Torvalds /* Wait for the next lease that has not been broken yet */ 12701da177e4SLinus Torvalds for (flock = inode->i_flock; flock && IS_LEASE(flock); 12711da177e4SLinus Torvalds flock = flock->fl_next) { 12721da177e4SLinus Torvalds if (flock->fl_type & F_INPROGRESS) 12731da177e4SLinus Torvalds goto restart; 12741da177e4SLinus Torvalds } 12751da177e4SLinus Torvalds error = 0; 12761da177e4SLinus Torvalds } 12771da177e4SLinus Torvalds 12781da177e4SLinus Torvalds out: 12791da177e4SLinus Torvalds unlock_kernel(); 12801da177e4SLinus Torvalds if (!alloc_err) 12811da177e4SLinus Torvalds locks_free_lock(new_fl); 12821da177e4SLinus Torvalds return error; 12831da177e4SLinus Torvalds } 12841da177e4SLinus Torvalds 12851da177e4SLinus Torvalds EXPORT_SYMBOL(__break_lease); 12861da177e4SLinus Torvalds 12871da177e4SLinus Torvalds /** 12881da177e4SLinus Torvalds * lease_get_mtime 12891da177e4SLinus Torvalds * @inode: the inode 12901da177e4SLinus Torvalds * @time: pointer to a timespec which will contain the last modified time 12911da177e4SLinus Torvalds * 12921da177e4SLinus Torvalds * This is to force NFS clients to flush their caches for files with 12931da177e4SLinus Torvalds * exclusive leases. The justification is that if someone has an 12941da177e4SLinus Torvalds * exclusive lease, then they could be modifiying it. 12951da177e4SLinus Torvalds */ 12961da177e4SLinus Torvalds void lease_get_mtime(struct inode *inode, struct timespec *time) 12971da177e4SLinus Torvalds { 12981da177e4SLinus Torvalds struct file_lock *flock = inode->i_flock; 12991da177e4SLinus Torvalds if (flock && IS_LEASE(flock) && (flock->fl_type & F_WRLCK)) 13001da177e4SLinus Torvalds *time = current_fs_time(inode->i_sb); 13011da177e4SLinus Torvalds else 13021da177e4SLinus Torvalds *time = inode->i_mtime; 13031da177e4SLinus Torvalds } 13041da177e4SLinus Torvalds 13051da177e4SLinus Torvalds EXPORT_SYMBOL(lease_get_mtime); 13061da177e4SLinus Torvalds 13071da177e4SLinus Torvalds /** 13081da177e4SLinus Torvalds * fcntl_getlease - Enquire what lease is currently active 13091da177e4SLinus Torvalds * @filp: the file 13101da177e4SLinus Torvalds * 13111da177e4SLinus Torvalds * The value returned by this function will be one of 13121da177e4SLinus Torvalds * (if no lease break is pending): 13131da177e4SLinus Torvalds * 13141da177e4SLinus Torvalds * %F_RDLCK to indicate a shared lease is held. 13151da177e4SLinus Torvalds * 13161da177e4SLinus Torvalds * %F_WRLCK to indicate an exclusive lease is held. 13171da177e4SLinus Torvalds * 13181da177e4SLinus Torvalds * %F_UNLCK to indicate no lease is held. 13191da177e4SLinus Torvalds * 13201da177e4SLinus Torvalds * (if a lease break is pending): 13211da177e4SLinus Torvalds * 13221da177e4SLinus Torvalds * %F_RDLCK to indicate an exclusive lease needs to be 13231da177e4SLinus Torvalds * changed to a shared lease (or removed). 13241da177e4SLinus Torvalds * 13251da177e4SLinus Torvalds * %F_UNLCK to indicate the lease needs to be removed. 13261da177e4SLinus Torvalds * 13271da177e4SLinus Torvalds * XXX: sfr & willy disagree over whether F_INPROGRESS 13281da177e4SLinus Torvalds * should be returned to userspace. 13291da177e4SLinus Torvalds */ 13301da177e4SLinus Torvalds int fcntl_getlease(struct file *filp) 13311da177e4SLinus Torvalds { 13321da177e4SLinus Torvalds struct file_lock *fl; 13331da177e4SLinus Torvalds int type = F_UNLCK; 13341da177e4SLinus Torvalds 13351da177e4SLinus Torvalds lock_kernel(); 13361da177e4SLinus Torvalds time_out_leases(filp->f_dentry->d_inode); 13371da177e4SLinus Torvalds for (fl = filp->f_dentry->d_inode->i_flock; fl && IS_LEASE(fl); 13381da177e4SLinus Torvalds fl = fl->fl_next) { 13391da177e4SLinus Torvalds if (fl->fl_file == filp) { 13401da177e4SLinus Torvalds type = fl->fl_type & ~F_INPROGRESS; 13411da177e4SLinus Torvalds break; 13421da177e4SLinus Torvalds } 13431da177e4SLinus Torvalds } 13441da177e4SLinus Torvalds unlock_kernel(); 13451da177e4SLinus Torvalds return type; 13461da177e4SLinus Torvalds } 13471da177e4SLinus Torvalds 13481da177e4SLinus Torvalds /** 13491da177e4SLinus Torvalds * __setlease - sets a lease on an open file 13501da177e4SLinus Torvalds * @filp: file pointer 13511da177e4SLinus Torvalds * @arg: type of lease to obtain 13521da177e4SLinus Torvalds * @flp: input - file_lock to use, output - file_lock inserted 13531da177e4SLinus Torvalds * 13541da177e4SLinus Torvalds * The (input) flp->fl_lmops->fl_break function is required 13551da177e4SLinus Torvalds * by break_lease(). 13561da177e4SLinus Torvalds * 13571da177e4SLinus Torvalds * Called with kernel lock held. 13581da177e4SLinus Torvalds */ 135975c96f85SAdrian Bunk static int __setlease(struct file *filp, long arg, struct file_lock **flp) 13601da177e4SLinus Torvalds { 13617eaae282SKAMBAROV, ZAUR struct file_lock *fl, **before, **my_before = NULL, *lease; 13621da177e4SLinus Torvalds struct dentry *dentry = filp->f_dentry; 13631da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 13641da177e4SLinus Torvalds int error, rdlease_count = 0, wrlease_count = 0; 13651da177e4SLinus Torvalds 13661da177e4SLinus Torvalds time_out_leases(inode); 13671da177e4SLinus Torvalds 13681da177e4SLinus Torvalds error = -EINVAL; 13691da177e4SLinus Torvalds if (!flp || !(*flp) || !(*flp)->fl_lmops || !(*flp)->fl_lmops->fl_break) 13701da177e4SLinus Torvalds goto out; 13711da177e4SLinus Torvalds 13727eaae282SKAMBAROV, ZAUR lease = *flp; 13737eaae282SKAMBAROV, ZAUR 13741da177e4SLinus Torvalds error = -EAGAIN; 13751da177e4SLinus Torvalds if ((arg == F_RDLCK) && (atomic_read(&inode->i_writecount) > 0)) 13761da177e4SLinus Torvalds goto out; 13771da177e4SLinus Torvalds if ((arg == F_WRLCK) 13781da177e4SLinus Torvalds && ((atomic_read(&dentry->d_count) > 1) 13791da177e4SLinus Torvalds || (atomic_read(&inode->i_count) > 1))) 13801da177e4SLinus Torvalds goto out; 13811da177e4SLinus Torvalds 13821da177e4SLinus Torvalds /* 13831da177e4SLinus Torvalds * At this point, we know that if there is an exclusive 13841da177e4SLinus Torvalds * lease on this file, then we hold it on this filp 13851da177e4SLinus Torvalds * (otherwise our open of this file would have blocked). 13861da177e4SLinus Torvalds * And if we are trying to acquire an exclusive lease, 13871da177e4SLinus Torvalds * then the file is not open by anyone (including us) 13881da177e4SLinus Torvalds * except for this filp. 13891da177e4SLinus Torvalds */ 13901da177e4SLinus Torvalds for (before = &inode->i_flock; 13911da177e4SLinus Torvalds ((fl = *before) != NULL) && IS_LEASE(fl); 13921da177e4SLinus Torvalds before = &fl->fl_next) { 13931da177e4SLinus Torvalds if (lease->fl_lmops->fl_mylease(fl, lease)) 13941da177e4SLinus Torvalds my_before = before; 13951da177e4SLinus Torvalds else if (fl->fl_type == (F_INPROGRESS | F_UNLCK)) 13961da177e4SLinus Torvalds /* 13971da177e4SLinus Torvalds * Someone is in the process of opening this 13981da177e4SLinus Torvalds * file for writing so we may not take an 13991da177e4SLinus Torvalds * exclusive lease on it. 14001da177e4SLinus Torvalds */ 14011da177e4SLinus Torvalds wrlease_count++; 14021da177e4SLinus Torvalds else 14031da177e4SLinus Torvalds rdlease_count++; 14041da177e4SLinus Torvalds } 14051da177e4SLinus Torvalds 14061da177e4SLinus Torvalds if ((arg == F_RDLCK && (wrlease_count > 0)) || 14071da177e4SLinus Torvalds (arg == F_WRLCK && ((rdlease_count + wrlease_count) > 0))) 14081da177e4SLinus Torvalds goto out; 14091da177e4SLinus Torvalds 14101da177e4SLinus Torvalds if (my_before != NULL) { 141175dff55aSTrond Myklebust *flp = *my_before; 14121da177e4SLinus Torvalds error = lease->fl_lmops->fl_change(my_before, arg); 14131da177e4SLinus Torvalds goto out; 14141da177e4SLinus Torvalds } 14151da177e4SLinus Torvalds 14161da177e4SLinus Torvalds error = 0; 14171da177e4SLinus Torvalds if (arg == F_UNLCK) 14181da177e4SLinus Torvalds goto out; 14191da177e4SLinus Torvalds 14201da177e4SLinus Torvalds error = -EINVAL; 14211da177e4SLinus Torvalds if (!leases_enable) 14221da177e4SLinus Torvalds goto out; 14231da177e4SLinus Torvalds 142474361cb6STrond Myklebust error = -ENOMEM; 142574361cb6STrond Myklebust fl = locks_alloc_lock(); 142674361cb6STrond Myklebust if (fl == NULL) 14271da177e4SLinus Torvalds goto out; 14281da177e4SLinus Torvalds 14291da177e4SLinus Torvalds locks_copy_lock(fl, lease); 14301da177e4SLinus Torvalds 14311da177e4SLinus Torvalds locks_insert_lock(before, fl); 14321da177e4SLinus Torvalds 14331da177e4SLinus Torvalds *flp = fl; 143474361cb6STrond Myklebust error = 0; 14351da177e4SLinus Torvalds out: 14361da177e4SLinus Torvalds return error; 14371da177e4SLinus Torvalds } 14381da177e4SLinus Torvalds 14391da177e4SLinus Torvalds /** 14401da177e4SLinus Torvalds * setlease - sets a lease on an open file 14411da177e4SLinus Torvalds * @filp: file pointer 14421da177e4SLinus Torvalds * @arg: type of lease to obtain 14431da177e4SLinus Torvalds * @lease: file_lock to use 14441da177e4SLinus Torvalds * 14451da177e4SLinus Torvalds * Call this to establish a lease on the file. 14461da177e4SLinus Torvalds * The fl_lmops fl_break function is required by break_lease 14471da177e4SLinus Torvalds */ 14481da177e4SLinus Torvalds 14491da177e4SLinus Torvalds int setlease(struct file *filp, long arg, struct file_lock **lease) 14501da177e4SLinus Torvalds { 14511da177e4SLinus Torvalds struct dentry *dentry = filp->f_dentry; 14521da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 14531da177e4SLinus Torvalds int error; 14541da177e4SLinus Torvalds 14551da177e4SLinus Torvalds if ((current->fsuid != inode->i_uid) && !capable(CAP_LEASE)) 14561da177e4SLinus Torvalds return -EACCES; 14571da177e4SLinus Torvalds if (!S_ISREG(inode->i_mode)) 14581da177e4SLinus Torvalds return -EINVAL; 14591da177e4SLinus Torvalds error = security_file_lock(filp, arg); 14601da177e4SLinus Torvalds if (error) 14611da177e4SLinus Torvalds return error; 14621da177e4SLinus Torvalds 14631da177e4SLinus Torvalds lock_kernel(); 14641da177e4SLinus Torvalds error = __setlease(filp, arg, lease); 14651da177e4SLinus Torvalds unlock_kernel(); 14661da177e4SLinus Torvalds 14671da177e4SLinus Torvalds return error; 14681da177e4SLinus Torvalds } 14691da177e4SLinus Torvalds 14701da177e4SLinus Torvalds EXPORT_SYMBOL(setlease); 14711da177e4SLinus Torvalds 14721da177e4SLinus Torvalds /** 14731da177e4SLinus Torvalds * fcntl_setlease - sets a lease on an open file 14741da177e4SLinus Torvalds * @fd: open file descriptor 14751da177e4SLinus Torvalds * @filp: file pointer 14761da177e4SLinus Torvalds * @arg: type of lease to obtain 14771da177e4SLinus Torvalds * 14781da177e4SLinus Torvalds * Call this fcntl to establish a lease on the file. 14791da177e4SLinus Torvalds * Note that you also need to call %F_SETSIG to 14801da177e4SLinus Torvalds * receive a signal when the lease is broken. 14811da177e4SLinus Torvalds */ 14821da177e4SLinus Torvalds int fcntl_setlease(unsigned int fd, struct file *filp, long arg) 14831da177e4SLinus Torvalds { 14841da177e4SLinus Torvalds struct file_lock fl, *flp = &fl; 14851da177e4SLinus Torvalds struct dentry *dentry = filp->f_dentry; 14861da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 14871da177e4SLinus Torvalds int error; 14881da177e4SLinus Torvalds 14891da177e4SLinus Torvalds if ((current->fsuid != inode->i_uid) && !capable(CAP_LEASE)) 14901da177e4SLinus Torvalds return -EACCES; 14911da177e4SLinus Torvalds if (!S_ISREG(inode->i_mode)) 14921da177e4SLinus Torvalds return -EINVAL; 14931da177e4SLinus Torvalds error = security_file_lock(filp, arg); 14941da177e4SLinus Torvalds if (error) 14951da177e4SLinus Torvalds return error; 14961da177e4SLinus Torvalds 14971da177e4SLinus Torvalds locks_init_lock(&fl); 14981da177e4SLinus Torvalds error = lease_init(filp, arg, &fl); 14991da177e4SLinus Torvalds if (error) 15001da177e4SLinus Torvalds return error; 15011da177e4SLinus Torvalds 15021da177e4SLinus Torvalds lock_kernel(); 15031da177e4SLinus Torvalds 15041da177e4SLinus Torvalds error = __setlease(filp, arg, &flp); 1505dc15ae14SJ. Bruce Fields if (error || arg == F_UNLCK) 15061da177e4SLinus Torvalds goto out_unlock; 15071da177e4SLinus Torvalds 15081da177e4SLinus Torvalds error = fasync_helper(fd, filp, 1, &flp->fl_fasync); 15091da177e4SLinus Torvalds if (error < 0) { 15101da177e4SLinus Torvalds /* remove lease just inserted by __setlease */ 15111da177e4SLinus Torvalds flp->fl_type = F_UNLCK | F_INPROGRESS; 15121da177e4SLinus Torvalds flp->fl_break_time = jiffies- 10; 15131da177e4SLinus Torvalds time_out_leases(inode); 15141da177e4SLinus Torvalds goto out_unlock; 15151da177e4SLinus Torvalds } 15161da177e4SLinus Torvalds 1517609d7fa9SEric W. Biederman error = __f_setown(filp, task_pid(current), PIDTYPE_PID, 0); 15181da177e4SLinus Torvalds out_unlock: 15191da177e4SLinus Torvalds unlock_kernel(); 15201da177e4SLinus Torvalds return error; 15211da177e4SLinus Torvalds } 15221da177e4SLinus Torvalds 15231da177e4SLinus Torvalds /** 15241da177e4SLinus Torvalds * flock_lock_file_wait - Apply a FLOCK-style lock to a file 15251da177e4SLinus Torvalds * @filp: The file to apply the lock to 15261da177e4SLinus Torvalds * @fl: The lock to be applied 15271da177e4SLinus Torvalds * 15281da177e4SLinus Torvalds * Add a FLOCK style lock to a file. 15291da177e4SLinus Torvalds */ 15301da177e4SLinus Torvalds int flock_lock_file_wait(struct file *filp, struct file_lock *fl) 15311da177e4SLinus Torvalds { 15321da177e4SLinus Torvalds int error; 15331da177e4SLinus Torvalds might_sleep(); 15341da177e4SLinus Torvalds for (;;) { 15351da177e4SLinus Torvalds error = flock_lock_file(filp, fl); 15361da177e4SLinus Torvalds if ((error != -EAGAIN) || !(fl->fl_flags & FL_SLEEP)) 15371da177e4SLinus Torvalds break; 15381da177e4SLinus Torvalds error = wait_event_interruptible(fl->fl_wait, !fl->fl_next); 15391da177e4SLinus Torvalds if (!error) 15401da177e4SLinus Torvalds continue; 15411da177e4SLinus Torvalds 15421da177e4SLinus Torvalds locks_delete_block(fl); 15431da177e4SLinus Torvalds break; 15441da177e4SLinus Torvalds } 15451da177e4SLinus Torvalds return error; 15461da177e4SLinus Torvalds } 15471da177e4SLinus Torvalds 15481da177e4SLinus Torvalds EXPORT_SYMBOL(flock_lock_file_wait); 15491da177e4SLinus Torvalds 15501da177e4SLinus Torvalds /** 15511da177e4SLinus Torvalds * sys_flock: - flock() system call. 15521da177e4SLinus Torvalds * @fd: the file descriptor to lock. 15531da177e4SLinus Torvalds * @cmd: the type of lock to apply. 15541da177e4SLinus Torvalds * 15551da177e4SLinus Torvalds * Apply a %FL_FLOCK style lock to an open file descriptor. 15561da177e4SLinus Torvalds * The @cmd can be one of 15571da177e4SLinus Torvalds * 15581da177e4SLinus Torvalds * %LOCK_SH -- a shared lock. 15591da177e4SLinus Torvalds * 15601da177e4SLinus Torvalds * %LOCK_EX -- an exclusive lock. 15611da177e4SLinus Torvalds * 15621da177e4SLinus Torvalds * %LOCK_UN -- remove an existing lock. 15631da177e4SLinus Torvalds * 15641da177e4SLinus Torvalds * %LOCK_MAND -- a `mandatory' flock. This exists to emulate Windows Share Modes. 15651da177e4SLinus Torvalds * 15661da177e4SLinus Torvalds * %LOCK_MAND can be combined with %LOCK_READ or %LOCK_WRITE to allow other 15671da177e4SLinus Torvalds * processes read and write access respectively. 15681da177e4SLinus Torvalds */ 15691da177e4SLinus Torvalds asmlinkage long sys_flock(unsigned int fd, unsigned int cmd) 15701da177e4SLinus Torvalds { 15711da177e4SLinus Torvalds struct file *filp; 15721da177e4SLinus Torvalds struct file_lock *lock; 15731da177e4SLinus Torvalds int can_sleep, unlock; 15741da177e4SLinus Torvalds int error; 15751da177e4SLinus Torvalds 15761da177e4SLinus Torvalds error = -EBADF; 15771da177e4SLinus Torvalds filp = fget(fd); 15781da177e4SLinus Torvalds if (!filp) 15791da177e4SLinus Torvalds goto out; 15801da177e4SLinus Torvalds 15811da177e4SLinus Torvalds can_sleep = !(cmd & LOCK_NB); 15821da177e4SLinus Torvalds cmd &= ~LOCK_NB; 15831da177e4SLinus Torvalds unlock = (cmd == LOCK_UN); 15841da177e4SLinus Torvalds 15851da177e4SLinus Torvalds if (!unlock && !(cmd & LOCK_MAND) && !(filp->f_mode & 3)) 15861da177e4SLinus Torvalds goto out_putf; 15871da177e4SLinus Torvalds 15881da177e4SLinus Torvalds error = flock_make_lock(filp, &lock, cmd); 15891da177e4SLinus Torvalds if (error) 15901da177e4SLinus Torvalds goto out_putf; 15911da177e4SLinus Torvalds if (can_sleep) 15921da177e4SLinus Torvalds lock->fl_flags |= FL_SLEEP; 15931da177e4SLinus Torvalds 15941da177e4SLinus Torvalds error = security_file_lock(filp, cmd); 15951da177e4SLinus Torvalds if (error) 15961da177e4SLinus Torvalds goto out_free; 15971da177e4SLinus Torvalds 15981da177e4SLinus Torvalds if (filp->f_op && filp->f_op->flock) 15991da177e4SLinus Torvalds error = filp->f_op->flock(filp, 16001da177e4SLinus Torvalds (can_sleep) ? F_SETLKW : F_SETLK, 16011da177e4SLinus Torvalds lock); 16021da177e4SLinus Torvalds else 16031da177e4SLinus Torvalds error = flock_lock_file_wait(filp, lock); 16041da177e4SLinus Torvalds 16051da177e4SLinus Torvalds out_free: 16061da177e4SLinus Torvalds locks_free_lock(lock); 16071da177e4SLinus Torvalds 16081da177e4SLinus Torvalds out_putf: 16091da177e4SLinus Torvalds fput(filp); 16101da177e4SLinus Torvalds out: 16111da177e4SLinus Torvalds return error; 16121da177e4SLinus Torvalds } 16131da177e4SLinus Torvalds 16141da177e4SLinus Torvalds /* Report the first existing lock that would conflict with l. 16151da177e4SLinus Torvalds * This implements the F_GETLK command of fcntl(). 16161da177e4SLinus Torvalds */ 16171da177e4SLinus Torvalds int fcntl_getlk(struct file *filp, struct flock __user *l) 16181da177e4SLinus Torvalds { 16198dc7c311SAndy Adamson struct file_lock *fl, cfl, file_lock; 16201da177e4SLinus Torvalds struct flock flock; 16211da177e4SLinus Torvalds int error; 16221da177e4SLinus Torvalds 16231da177e4SLinus Torvalds error = -EFAULT; 16241da177e4SLinus Torvalds if (copy_from_user(&flock, l, sizeof(flock))) 16251da177e4SLinus Torvalds goto out; 16261da177e4SLinus Torvalds error = -EINVAL; 16271da177e4SLinus Torvalds if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK)) 16281da177e4SLinus Torvalds goto out; 16291da177e4SLinus Torvalds 16301da177e4SLinus Torvalds error = flock_to_posix_lock(filp, &file_lock, &flock); 16311da177e4SLinus Torvalds if (error) 16321da177e4SLinus Torvalds goto out; 16331da177e4SLinus Torvalds 16341da177e4SLinus Torvalds if (filp->f_op && filp->f_op->lock) { 16351da177e4SLinus Torvalds error = filp->f_op->lock(filp, F_GETLK, &file_lock); 163680fec4c6STrond Myklebust if (file_lock.fl_ops && file_lock.fl_ops->fl_release_private) 163780fec4c6STrond Myklebust file_lock.fl_ops->fl_release_private(&file_lock); 16381da177e4SLinus Torvalds if (error < 0) 16391da177e4SLinus Torvalds goto out; 16401da177e4SLinus Torvalds else 16411da177e4SLinus Torvalds fl = (file_lock.fl_type == F_UNLCK ? NULL : &file_lock); 16421da177e4SLinus Torvalds } else { 16438dc7c311SAndy Adamson fl = (posix_test_lock(filp, &file_lock, &cfl) ? &cfl : NULL); 16441da177e4SLinus Torvalds } 16451da177e4SLinus Torvalds 16461da177e4SLinus Torvalds flock.l_type = F_UNLCK; 16471da177e4SLinus Torvalds if (fl != NULL) { 16481da177e4SLinus Torvalds flock.l_pid = fl->fl_pid; 16491da177e4SLinus Torvalds #if BITS_PER_LONG == 32 16501da177e4SLinus Torvalds /* 16511da177e4SLinus Torvalds * Make sure we can represent the posix lock via 16521da177e4SLinus Torvalds * legacy 32bit flock. 16531da177e4SLinus Torvalds */ 16541da177e4SLinus Torvalds error = -EOVERFLOW; 16551da177e4SLinus Torvalds if (fl->fl_start > OFFT_OFFSET_MAX) 16561da177e4SLinus Torvalds goto out; 16571da177e4SLinus Torvalds if ((fl->fl_end != OFFSET_MAX) 16581da177e4SLinus Torvalds && (fl->fl_end > OFFT_OFFSET_MAX)) 16591da177e4SLinus Torvalds goto out; 16601da177e4SLinus Torvalds #endif 16611da177e4SLinus Torvalds flock.l_start = fl->fl_start; 16621da177e4SLinus Torvalds flock.l_len = fl->fl_end == OFFSET_MAX ? 0 : 16631da177e4SLinus Torvalds fl->fl_end - fl->fl_start + 1; 16641da177e4SLinus Torvalds flock.l_whence = 0; 16651da177e4SLinus Torvalds flock.l_type = fl->fl_type; 16661da177e4SLinus Torvalds } 16671da177e4SLinus Torvalds error = -EFAULT; 16681da177e4SLinus Torvalds if (!copy_to_user(l, &flock, sizeof(flock))) 16691da177e4SLinus Torvalds error = 0; 16701da177e4SLinus Torvalds out: 16711da177e4SLinus Torvalds return error; 16721da177e4SLinus Torvalds } 16731da177e4SLinus Torvalds 16741da177e4SLinus Torvalds /* Apply the lock described by l to an open file descriptor. 16751da177e4SLinus Torvalds * This implements both the F_SETLK and F_SETLKW commands of fcntl(). 16761da177e4SLinus Torvalds */ 1677c293621bSPeter Staubach int fcntl_setlk(unsigned int fd, struct file *filp, unsigned int cmd, 1678c293621bSPeter Staubach struct flock __user *l) 16791da177e4SLinus Torvalds { 16801da177e4SLinus Torvalds struct file_lock *file_lock = locks_alloc_lock(); 16811da177e4SLinus Torvalds struct flock flock; 16821da177e4SLinus Torvalds struct inode *inode; 16831da177e4SLinus Torvalds int error; 16841da177e4SLinus Torvalds 16851da177e4SLinus Torvalds if (file_lock == NULL) 16861da177e4SLinus Torvalds return -ENOLCK; 16871da177e4SLinus Torvalds 16881da177e4SLinus Torvalds /* 16891da177e4SLinus Torvalds * This might block, so we do it before checking the inode. 16901da177e4SLinus Torvalds */ 16911da177e4SLinus Torvalds error = -EFAULT; 16921da177e4SLinus Torvalds if (copy_from_user(&flock, l, sizeof(flock))) 16931da177e4SLinus Torvalds goto out; 16941da177e4SLinus Torvalds 16951da177e4SLinus Torvalds inode = filp->f_dentry->d_inode; 16961da177e4SLinus Torvalds 16971da177e4SLinus Torvalds /* Don't allow mandatory locks on files that may be memory mapped 16981da177e4SLinus Torvalds * and shared. 16991da177e4SLinus Torvalds */ 17001da177e4SLinus Torvalds if (IS_MANDLOCK(inode) && 17011da177e4SLinus Torvalds (inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID && 17021da177e4SLinus Torvalds mapping_writably_mapped(filp->f_mapping)) { 17031da177e4SLinus Torvalds error = -EAGAIN; 17041da177e4SLinus Torvalds goto out; 17051da177e4SLinus Torvalds } 17061da177e4SLinus Torvalds 1707c293621bSPeter Staubach again: 17081da177e4SLinus Torvalds error = flock_to_posix_lock(filp, file_lock, &flock); 17091da177e4SLinus Torvalds if (error) 17101da177e4SLinus Torvalds goto out; 17111da177e4SLinus Torvalds if (cmd == F_SETLKW) { 17121da177e4SLinus Torvalds file_lock->fl_flags |= FL_SLEEP; 17131da177e4SLinus Torvalds } 17141da177e4SLinus Torvalds 17151da177e4SLinus Torvalds error = -EBADF; 17161da177e4SLinus Torvalds switch (flock.l_type) { 17171da177e4SLinus Torvalds case F_RDLCK: 17181da177e4SLinus Torvalds if (!(filp->f_mode & FMODE_READ)) 17191da177e4SLinus Torvalds goto out; 17201da177e4SLinus Torvalds break; 17211da177e4SLinus Torvalds case F_WRLCK: 17221da177e4SLinus Torvalds if (!(filp->f_mode & FMODE_WRITE)) 17231da177e4SLinus Torvalds goto out; 17241da177e4SLinus Torvalds break; 17251da177e4SLinus Torvalds case F_UNLCK: 17261da177e4SLinus Torvalds break; 17271da177e4SLinus Torvalds default: 17281da177e4SLinus Torvalds error = -EINVAL; 17291da177e4SLinus Torvalds goto out; 17301da177e4SLinus Torvalds } 17311da177e4SLinus Torvalds 17321da177e4SLinus Torvalds error = security_file_lock(filp, file_lock->fl_type); 17331da177e4SLinus Torvalds if (error) 17341da177e4SLinus Torvalds goto out; 17351da177e4SLinus Torvalds 1736c293621bSPeter Staubach if (filp->f_op && filp->f_op->lock != NULL) 17371da177e4SLinus Torvalds error = filp->f_op->lock(filp, cmd, file_lock); 1738c293621bSPeter Staubach else { 17391da177e4SLinus Torvalds for (;;) { 17405842add2SAndy Adamson error = posix_lock_file(filp, file_lock); 17411da177e4SLinus Torvalds if ((error != -EAGAIN) || (cmd == F_SETLK)) 17421da177e4SLinus Torvalds break; 17431da177e4SLinus Torvalds error = wait_event_interruptible(file_lock->fl_wait, 17441da177e4SLinus Torvalds !file_lock->fl_next); 17451da177e4SLinus Torvalds if (!error) 17461da177e4SLinus Torvalds continue; 17471da177e4SLinus Torvalds 17481da177e4SLinus Torvalds locks_delete_block(file_lock); 17491da177e4SLinus Torvalds break; 17501da177e4SLinus Torvalds } 1751c293621bSPeter Staubach } 1752c293621bSPeter Staubach 1753c293621bSPeter Staubach /* 1754c293621bSPeter Staubach * Attempt to detect a close/fcntl race and recover by 1755c293621bSPeter Staubach * releasing the lock that was just acquired. 1756c293621bSPeter Staubach */ 1757c293621bSPeter Staubach if (!error && fcheck(fd) != filp && flock.l_type != F_UNLCK) { 1758c293621bSPeter Staubach flock.l_type = F_UNLCK; 1759c293621bSPeter Staubach goto again; 1760c293621bSPeter Staubach } 17611da177e4SLinus Torvalds 17621da177e4SLinus Torvalds out: 17631da177e4SLinus Torvalds locks_free_lock(file_lock); 17641da177e4SLinus Torvalds return error; 17651da177e4SLinus Torvalds } 17661da177e4SLinus Torvalds 17671da177e4SLinus Torvalds #if BITS_PER_LONG == 32 17681da177e4SLinus Torvalds /* Report the first existing lock that would conflict with l. 17691da177e4SLinus Torvalds * This implements the F_GETLK command of fcntl(). 17701da177e4SLinus Torvalds */ 17711da177e4SLinus Torvalds int fcntl_getlk64(struct file *filp, struct flock64 __user *l) 17721da177e4SLinus Torvalds { 17738dc7c311SAndy Adamson struct file_lock *fl, cfl, file_lock; 17741da177e4SLinus Torvalds struct flock64 flock; 17751da177e4SLinus Torvalds int error; 17761da177e4SLinus Torvalds 17771da177e4SLinus Torvalds error = -EFAULT; 17781da177e4SLinus Torvalds if (copy_from_user(&flock, l, sizeof(flock))) 17791da177e4SLinus Torvalds goto out; 17801da177e4SLinus Torvalds error = -EINVAL; 17811da177e4SLinus Torvalds if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK)) 17821da177e4SLinus Torvalds goto out; 17831da177e4SLinus Torvalds 17841da177e4SLinus Torvalds error = flock64_to_posix_lock(filp, &file_lock, &flock); 17851da177e4SLinus Torvalds if (error) 17861da177e4SLinus Torvalds goto out; 17871da177e4SLinus Torvalds 17881da177e4SLinus Torvalds if (filp->f_op && filp->f_op->lock) { 17891da177e4SLinus Torvalds error = filp->f_op->lock(filp, F_GETLK, &file_lock); 179080fec4c6STrond Myklebust if (file_lock.fl_ops && file_lock.fl_ops->fl_release_private) 179180fec4c6STrond Myklebust file_lock.fl_ops->fl_release_private(&file_lock); 17921da177e4SLinus Torvalds if (error < 0) 17931da177e4SLinus Torvalds goto out; 17941da177e4SLinus Torvalds else 17951da177e4SLinus Torvalds fl = (file_lock.fl_type == F_UNLCK ? NULL : &file_lock); 17961da177e4SLinus Torvalds } else { 17978dc7c311SAndy Adamson fl = (posix_test_lock(filp, &file_lock, &cfl) ? &cfl : NULL); 17981da177e4SLinus Torvalds } 17991da177e4SLinus Torvalds 18001da177e4SLinus Torvalds flock.l_type = F_UNLCK; 18011da177e4SLinus Torvalds if (fl != NULL) { 18021da177e4SLinus Torvalds flock.l_pid = fl->fl_pid; 18031da177e4SLinus Torvalds flock.l_start = fl->fl_start; 18041da177e4SLinus Torvalds flock.l_len = fl->fl_end == OFFSET_MAX ? 0 : 18051da177e4SLinus Torvalds fl->fl_end - fl->fl_start + 1; 18061da177e4SLinus Torvalds flock.l_whence = 0; 18071da177e4SLinus Torvalds flock.l_type = fl->fl_type; 18081da177e4SLinus Torvalds } 18091da177e4SLinus Torvalds error = -EFAULT; 18101da177e4SLinus Torvalds if (!copy_to_user(l, &flock, sizeof(flock))) 18111da177e4SLinus Torvalds error = 0; 18121da177e4SLinus Torvalds 18131da177e4SLinus Torvalds out: 18141da177e4SLinus Torvalds return error; 18151da177e4SLinus Torvalds } 18161da177e4SLinus Torvalds 18171da177e4SLinus Torvalds /* Apply the lock described by l to an open file descriptor. 18181da177e4SLinus Torvalds * This implements both the F_SETLK and F_SETLKW commands of fcntl(). 18191da177e4SLinus Torvalds */ 1820c293621bSPeter Staubach int fcntl_setlk64(unsigned int fd, struct file *filp, unsigned int cmd, 1821c293621bSPeter Staubach struct flock64 __user *l) 18221da177e4SLinus Torvalds { 18231da177e4SLinus Torvalds struct file_lock *file_lock = locks_alloc_lock(); 18241da177e4SLinus Torvalds struct flock64 flock; 18251da177e4SLinus Torvalds struct inode *inode; 18261da177e4SLinus Torvalds int error; 18271da177e4SLinus Torvalds 18281da177e4SLinus Torvalds if (file_lock == NULL) 18291da177e4SLinus Torvalds return -ENOLCK; 18301da177e4SLinus Torvalds 18311da177e4SLinus Torvalds /* 18321da177e4SLinus Torvalds * This might block, so we do it before checking the inode. 18331da177e4SLinus Torvalds */ 18341da177e4SLinus Torvalds error = -EFAULT; 18351da177e4SLinus Torvalds if (copy_from_user(&flock, l, sizeof(flock))) 18361da177e4SLinus Torvalds goto out; 18371da177e4SLinus Torvalds 18381da177e4SLinus Torvalds inode = filp->f_dentry->d_inode; 18391da177e4SLinus Torvalds 18401da177e4SLinus Torvalds /* Don't allow mandatory locks on files that may be memory mapped 18411da177e4SLinus Torvalds * and shared. 18421da177e4SLinus Torvalds */ 18431da177e4SLinus Torvalds if (IS_MANDLOCK(inode) && 18441da177e4SLinus Torvalds (inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID && 18451da177e4SLinus Torvalds mapping_writably_mapped(filp->f_mapping)) { 18461da177e4SLinus Torvalds error = -EAGAIN; 18471da177e4SLinus Torvalds goto out; 18481da177e4SLinus Torvalds } 18491da177e4SLinus Torvalds 1850c293621bSPeter Staubach again: 18511da177e4SLinus Torvalds error = flock64_to_posix_lock(filp, file_lock, &flock); 18521da177e4SLinus Torvalds if (error) 18531da177e4SLinus Torvalds goto out; 18541da177e4SLinus Torvalds if (cmd == F_SETLKW64) { 18551da177e4SLinus Torvalds file_lock->fl_flags |= FL_SLEEP; 18561da177e4SLinus Torvalds } 18571da177e4SLinus Torvalds 18581da177e4SLinus Torvalds error = -EBADF; 18591da177e4SLinus Torvalds switch (flock.l_type) { 18601da177e4SLinus Torvalds case F_RDLCK: 18611da177e4SLinus Torvalds if (!(filp->f_mode & FMODE_READ)) 18621da177e4SLinus Torvalds goto out; 18631da177e4SLinus Torvalds break; 18641da177e4SLinus Torvalds case F_WRLCK: 18651da177e4SLinus Torvalds if (!(filp->f_mode & FMODE_WRITE)) 18661da177e4SLinus Torvalds goto out; 18671da177e4SLinus Torvalds break; 18681da177e4SLinus Torvalds case F_UNLCK: 18691da177e4SLinus Torvalds break; 18701da177e4SLinus Torvalds default: 18711da177e4SLinus Torvalds error = -EINVAL; 18721da177e4SLinus Torvalds goto out; 18731da177e4SLinus Torvalds } 18741da177e4SLinus Torvalds 18751da177e4SLinus Torvalds error = security_file_lock(filp, file_lock->fl_type); 18761da177e4SLinus Torvalds if (error) 18771da177e4SLinus Torvalds goto out; 18781da177e4SLinus Torvalds 1879c293621bSPeter Staubach if (filp->f_op && filp->f_op->lock != NULL) 18801da177e4SLinus Torvalds error = filp->f_op->lock(filp, cmd, file_lock); 1881c293621bSPeter Staubach else { 18821da177e4SLinus Torvalds for (;;) { 18835842add2SAndy Adamson error = posix_lock_file(filp, file_lock); 18841da177e4SLinus Torvalds if ((error != -EAGAIN) || (cmd == F_SETLK64)) 18851da177e4SLinus Torvalds break; 18861da177e4SLinus Torvalds error = wait_event_interruptible(file_lock->fl_wait, 18871da177e4SLinus Torvalds !file_lock->fl_next); 18881da177e4SLinus Torvalds if (!error) 18891da177e4SLinus Torvalds continue; 18901da177e4SLinus Torvalds 18911da177e4SLinus Torvalds locks_delete_block(file_lock); 18921da177e4SLinus Torvalds break; 18931da177e4SLinus Torvalds } 1894c293621bSPeter Staubach } 1895c293621bSPeter Staubach 1896c293621bSPeter Staubach /* 1897c293621bSPeter Staubach * Attempt to detect a close/fcntl race and recover by 1898c293621bSPeter Staubach * releasing the lock that was just acquired. 1899c293621bSPeter Staubach */ 1900c293621bSPeter Staubach if (!error && fcheck(fd) != filp && flock.l_type != F_UNLCK) { 1901c293621bSPeter Staubach flock.l_type = F_UNLCK; 1902c293621bSPeter Staubach goto again; 1903c293621bSPeter Staubach } 19041da177e4SLinus Torvalds 19051da177e4SLinus Torvalds out: 19061da177e4SLinus Torvalds locks_free_lock(file_lock); 19071da177e4SLinus Torvalds return error; 19081da177e4SLinus Torvalds } 19091da177e4SLinus Torvalds #endif /* BITS_PER_LONG == 32 */ 19101da177e4SLinus Torvalds 19111da177e4SLinus Torvalds /* 19121da177e4SLinus Torvalds * This function is called when the file is being removed 19131da177e4SLinus Torvalds * from the task's fd array. POSIX locks belonging to this task 19141da177e4SLinus Torvalds * are deleted at this time. 19151da177e4SLinus Torvalds */ 19161da177e4SLinus Torvalds void locks_remove_posix(struct file *filp, fl_owner_t owner) 19171da177e4SLinus Torvalds { 1918ff7b86b8SMiklos Szeredi struct file_lock lock; 19191da177e4SLinus Torvalds 19201da177e4SLinus Torvalds /* 19211da177e4SLinus Torvalds * If there are no locks held on this file, we don't need to call 19221da177e4SLinus Torvalds * posix_lock_file(). Another process could be setting a lock on this 19231da177e4SLinus Torvalds * file at the same time, but we wouldn't remove that lock anyway. 19241da177e4SLinus Torvalds */ 1925ff7b86b8SMiklos Szeredi if (!filp->f_dentry->d_inode->i_flock) 19261da177e4SLinus Torvalds return; 19271da177e4SLinus Torvalds 19281da177e4SLinus Torvalds lock.fl_type = F_UNLCK; 192975e1fcc0SMiklos Szeredi lock.fl_flags = FL_POSIX | FL_CLOSE; 19301da177e4SLinus Torvalds lock.fl_start = 0; 19311da177e4SLinus Torvalds lock.fl_end = OFFSET_MAX; 19321da177e4SLinus Torvalds lock.fl_owner = owner; 19331da177e4SLinus Torvalds lock.fl_pid = current->tgid; 19341da177e4SLinus Torvalds lock.fl_file = filp; 19351da177e4SLinus Torvalds lock.fl_ops = NULL; 19361da177e4SLinus Torvalds lock.fl_lmops = NULL; 19371da177e4SLinus Torvalds 1938ff7b86b8SMiklos Szeredi if (filp->f_op && filp->f_op->lock != NULL) 19391da177e4SLinus Torvalds filp->f_op->lock(filp, F_SETLK, &lock); 1940ff7b86b8SMiklos Szeredi else 1941ff7b86b8SMiklos Szeredi posix_lock_file(filp, &lock); 19421da177e4SLinus Torvalds 19431da177e4SLinus Torvalds if (lock.fl_ops && lock.fl_ops->fl_release_private) 19441da177e4SLinus Torvalds lock.fl_ops->fl_release_private(&lock); 19451da177e4SLinus Torvalds } 19461da177e4SLinus Torvalds 19471da177e4SLinus Torvalds EXPORT_SYMBOL(locks_remove_posix); 19481da177e4SLinus Torvalds 19491da177e4SLinus Torvalds /* 19501da177e4SLinus Torvalds * This function is called on the last close of an open file. 19511da177e4SLinus Torvalds */ 19521da177e4SLinus Torvalds void locks_remove_flock(struct file *filp) 19531da177e4SLinus Torvalds { 19541da177e4SLinus Torvalds struct inode * inode = filp->f_dentry->d_inode; 19551da177e4SLinus Torvalds struct file_lock *fl; 19561da177e4SLinus Torvalds struct file_lock **before; 19571da177e4SLinus Torvalds 19581da177e4SLinus Torvalds if (!inode->i_flock) 19591da177e4SLinus Torvalds return; 19601da177e4SLinus Torvalds 19611da177e4SLinus Torvalds if (filp->f_op && filp->f_op->flock) { 19621da177e4SLinus Torvalds struct file_lock fl = { 19631da177e4SLinus Torvalds .fl_pid = current->tgid, 19641da177e4SLinus Torvalds .fl_file = filp, 19651da177e4SLinus Torvalds .fl_flags = FL_FLOCK, 19661da177e4SLinus Torvalds .fl_type = F_UNLCK, 19671da177e4SLinus Torvalds .fl_end = OFFSET_MAX, 19681da177e4SLinus Torvalds }; 19691da177e4SLinus Torvalds filp->f_op->flock(filp, F_SETLKW, &fl); 197080fec4c6STrond Myklebust if (fl.fl_ops && fl.fl_ops->fl_release_private) 197180fec4c6STrond Myklebust fl.fl_ops->fl_release_private(&fl); 19721da177e4SLinus Torvalds } 19731da177e4SLinus Torvalds 19741da177e4SLinus Torvalds lock_kernel(); 19751da177e4SLinus Torvalds before = &inode->i_flock; 19761da177e4SLinus Torvalds 19771da177e4SLinus Torvalds while ((fl = *before) != NULL) { 19781da177e4SLinus Torvalds if (fl->fl_file == filp) { 1979c293621bSPeter Staubach if (IS_FLOCK(fl)) { 19801da177e4SLinus Torvalds locks_delete_lock(before); 19811da177e4SLinus Torvalds continue; 19821da177e4SLinus Torvalds } 19831da177e4SLinus Torvalds if (IS_LEASE(fl)) { 19841da177e4SLinus Torvalds lease_modify(before, F_UNLCK); 19851da177e4SLinus Torvalds continue; 19861da177e4SLinus Torvalds } 19871da177e4SLinus Torvalds /* What? */ 19881da177e4SLinus Torvalds BUG(); 19891da177e4SLinus Torvalds } 19901da177e4SLinus Torvalds before = &fl->fl_next; 19911da177e4SLinus Torvalds } 19921da177e4SLinus Torvalds unlock_kernel(); 19931da177e4SLinus Torvalds } 19941da177e4SLinus Torvalds 19951da177e4SLinus Torvalds /** 19961da177e4SLinus Torvalds * posix_unblock_lock - stop waiting for a file lock 19971da177e4SLinus Torvalds * @filp: how the file was opened 19981da177e4SLinus Torvalds * @waiter: the lock which was waiting 19991da177e4SLinus Torvalds * 20001da177e4SLinus Torvalds * lockd needs to block waiting for locks. 20011da177e4SLinus Torvalds */ 200264a318eeSJ. Bruce Fields int 20031da177e4SLinus Torvalds posix_unblock_lock(struct file *filp, struct file_lock *waiter) 20041da177e4SLinus Torvalds { 200564a318eeSJ. Bruce Fields int status = 0; 200664a318eeSJ. Bruce Fields 20071da177e4SLinus Torvalds lock_kernel(); 20085996a298SJ. Bruce Fields if (waiter->fl_next) 20091da177e4SLinus Torvalds __locks_delete_block(waiter); 201064a318eeSJ. Bruce Fields else 201164a318eeSJ. Bruce Fields status = -ENOENT; 20121da177e4SLinus Torvalds unlock_kernel(); 201364a318eeSJ. Bruce Fields return status; 20141da177e4SLinus Torvalds } 20151da177e4SLinus Torvalds 20161da177e4SLinus Torvalds EXPORT_SYMBOL(posix_unblock_lock); 20171da177e4SLinus Torvalds 20181da177e4SLinus Torvalds static void lock_get_status(char* out, struct file_lock *fl, int id, char *pfx) 20191da177e4SLinus Torvalds { 20201da177e4SLinus Torvalds struct inode *inode = NULL; 20211da177e4SLinus Torvalds 20221da177e4SLinus Torvalds if (fl->fl_file != NULL) 20231da177e4SLinus Torvalds inode = fl->fl_file->f_dentry->d_inode; 20241da177e4SLinus Torvalds 20251da177e4SLinus Torvalds out += sprintf(out, "%d:%s ", id, pfx); 20261da177e4SLinus Torvalds if (IS_POSIX(fl)) { 20271da177e4SLinus Torvalds out += sprintf(out, "%6s %s ", 20281da177e4SLinus Torvalds (fl->fl_flags & FL_ACCESS) ? "ACCESS" : "POSIX ", 20291da177e4SLinus Torvalds (inode == NULL) ? "*NOINODE*" : 20301da177e4SLinus Torvalds (IS_MANDLOCK(inode) && 20311da177e4SLinus Torvalds (inode->i_mode & (S_IXGRP | S_ISGID)) == S_ISGID) ? 20321da177e4SLinus Torvalds "MANDATORY" : "ADVISORY "); 20331da177e4SLinus Torvalds } else if (IS_FLOCK(fl)) { 20341da177e4SLinus Torvalds if (fl->fl_type & LOCK_MAND) { 20351da177e4SLinus Torvalds out += sprintf(out, "FLOCK MSNFS "); 20361da177e4SLinus Torvalds } else { 20371da177e4SLinus Torvalds out += sprintf(out, "FLOCK ADVISORY "); 20381da177e4SLinus Torvalds } 20391da177e4SLinus Torvalds } else if (IS_LEASE(fl)) { 20401da177e4SLinus Torvalds out += sprintf(out, "LEASE "); 20411da177e4SLinus Torvalds if (fl->fl_type & F_INPROGRESS) 20421da177e4SLinus Torvalds out += sprintf(out, "BREAKING "); 20431da177e4SLinus Torvalds else if (fl->fl_file) 20441da177e4SLinus Torvalds out += sprintf(out, "ACTIVE "); 20451da177e4SLinus Torvalds else 20461da177e4SLinus Torvalds out += sprintf(out, "BREAKER "); 20471da177e4SLinus Torvalds } else { 20481da177e4SLinus Torvalds out += sprintf(out, "UNKNOWN UNKNOWN "); 20491da177e4SLinus Torvalds } 20501da177e4SLinus Torvalds if (fl->fl_type & LOCK_MAND) { 20511da177e4SLinus Torvalds out += sprintf(out, "%s ", 20521da177e4SLinus Torvalds (fl->fl_type & LOCK_READ) 20531da177e4SLinus Torvalds ? (fl->fl_type & LOCK_WRITE) ? "RW " : "READ " 20541da177e4SLinus Torvalds : (fl->fl_type & LOCK_WRITE) ? "WRITE" : "NONE "); 20551da177e4SLinus Torvalds } else { 20561da177e4SLinus Torvalds out += sprintf(out, "%s ", 20571da177e4SLinus Torvalds (fl->fl_type & F_INPROGRESS) 20581da177e4SLinus Torvalds ? (fl->fl_type & F_UNLCK) ? "UNLCK" : "READ " 20591da177e4SLinus Torvalds : (fl->fl_type & F_WRLCK) ? "WRITE" : "READ "); 20601da177e4SLinus Torvalds } 20611da177e4SLinus Torvalds if (inode) { 20621da177e4SLinus Torvalds #ifdef WE_CAN_BREAK_LSLK_NOW 20631da177e4SLinus Torvalds out += sprintf(out, "%d %s:%ld ", fl->fl_pid, 20641da177e4SLinus Torvalds inode->i_sb->s_id, inode->i_ino); 20651da177e4SLinus Torvalds #else 20661da177e4SLinus Torvalds /* userspace relies on this representation of dev_t ;-( */ 20671da177e4SLinus Torvalds out += sprintf(out, "%d %02x:%02x:%ld ", fl->fl_pid, 20681da177e4SLinus Torvalds MAJOR(inode->i_sb->s_dev), 20691da177e4SLinus Torvalds MINOR(inode->i_sb->s_dev), inode->i_ino); 20701da177e4SLinus Torvalds #endif 20711da177e4SLinus Torvalds } else { 20721da177e4SLinus Torvalds out += sprintf(out, "%d <none>:0 ", fl->fl_pid); 20731da177e4SLinus Torvalds } 20741da177e4SLinus Torvalds if (IS_POSIX(fl)) { 20751da177e4SLinus Torvalds if (fl->fl_end == OFFSET_MAX) 20761da177e4SLinus Torvalds out += sprintf(out, "%Ld EOF\n", fl->fl_start); 20771da177e4SLinus Torvalds else 20781da177e4SLinus Torvalds out += sprintf(out, "%Ld %Ld\n", fl->fl_start, 20791da177e4SLinus Torvalds fl->fl_end); 20801da177e4SLinus Torvalds } else { 20811da177e4SLinus Torvalds out += sprintf(out, "0 EOF\n"); 20821da177e4SLinus Torvalds } 20831da177e4SLinus Torvalds } 20841da177e4SLinus Torvalds 20851da177e4SLinus Torvalds static void move_lock_status(char **p, off_t* pos, off_t offset) 20861da177e4SLinus Torvalds { 20871da177e4SLinus Torvalds int len; 20881da177e4SLinus Torvalds len = strlen(*p); 20891da177e4SLinus Torvalds if(*pos >= offset) { 20901da177e4SLinus Torvalds /* the complete line is valid */ 20911da177e4SLinus Torvalds *p += len; 20921da177e4SLinus Torvalds *pos += len; 20931da177e4SLinus Torvalds return; 20941da177e4SLinus Torvalds } 20951da177e4SLinus Torvalds if(*pos+len > offset) { 20961da177e4SLinus Torvalds /* use the second part of the line */ 20971da177e4SLinus Torvalds int i = offset-*pos; 20981da177e4SLinus Torvalds memmove(*p,*p+i,len-i); 20991da177e4SLinus Torvalds *p += len-i; 21001da177e4SLinus Torvalds *pos += len; 21011da177e4SLinus Torvalds return; 21021da177e4SLinus Torvalds } 21031da177e4SLinus Torvalds /* discard the complete line */ 21041da177e4SLinus Torvalds *pos += len; 21051da177e4SLinus Torvalds } 21061da177e4SLinus Torvalds 21071da177e4SLinus Torvalds /** 21081da177e4SLinus Torvalds * get_locks_status - reports lock usage in /proc/locks 21091da177e4SLinus Torvalds * @buffer: address in userspace to write into 21101da177e4SLinus Torvalds * @start: ? 21111da177e4SLinus Torvalds * @offset: how far we are through the buffer 21121da177e4SLinus Torvalds * @length: how much to read 21131da177e4SLinus Torvalds */ 21141da177e4SLinus Torvalds 21151da177e4SLinus Torvalds int get_locks_status(char *buffer, char **start, off_t offset, int length) 21161da177e4SLinus Torvalds { 21171da177e4SLinus Torvalds struct list_head *tmp; 21181da177e4SLinus Torvalds char *q = buffer; 21191da177e4SLinus Torvalds off_t pos = 0; 21201da177e4SLinus Torvalds int i = 0; 21211da177e4SLinus Torvalds 21221da177e4SLinus Torvalds lock_kernel(); 21231da177e4SLinus Torvalds list_for_each(tmp, &file_lock_list) { 21241da177e4SLinus Torvalds struct list_head *btmp; 21251da177e4SLinus Torvalds struct file_lock *fl = list_entry(tmp, struct file_lock, fl_link); 21261da177e4SLinus Torvalds lock_get_status(q, fl, ++i, ""); 21271da177e4SLinus Torvalds move_lock_status(&q, &pos, offset); 21281da177e4SLinus Torvalds 21291da177e4SLinus Torvalds if(pos >= offset+length) 21301da177e4SLinus Torvalds goto done; 21311da177e4SLinus Torvalds 21321da177e4SLinus Torvalds list_for_each(btmp, &fl->fl_block) { 21331da177e4SLinus Torvalds struct file_lock *bfl = list_entry(btmp, 21341da177e4SLinus Torvalds struct file_lock, fl_block); 21351da177e4SLinus Torvalds lock_get_status(q, bfl, i, " ->"); 21361da177e4SLinus Torvalds move_lock_status(&q, &pos, offset); 21371da177e4SLinus Torvalds 21381da177e4SLinus Torvalds if(pos >= offset+length) 21391da177e4SLinus Torvalds goto done; 21401da177e4SLinus Torvalds } 21411da177e4SLinus Torvalds } 21421da177e4SLinus Torvalds done: 21431da177e4SLinus Torvalds unlock_kernel(); 21441da177e4SLinus Torvalds *start = buffer; 21451da177e4SLinus Torvalds if(q-buffer < length) 21461da177e4SLinus Torvalds return (q-buffer); 21471da177e4SLinus Torvalds return length; 21481da177e4SLinus Torvalds } 21491da177e4SLinus Torvalds 21501da177e4SLinus Torvalds /** 21511da177e4SLinus Torvalds * lock_may_read - checks that the region is free of locks 21521da177e4SLinus Torvalds * @inode: the inode that is being read 21531da177e4SLinus Torvalds * @start: the first byte to read 21541da177e4SLinus Torvalds * @len: the number of bytes to read 21551da177e4SLinus Torvalds * 21561da177e4SLinus Torvalds * Emulates Windows locking requirements. Whole-file 21571da177e4SLinus Torvalds * mandatory locks (share modes) can prohibit a read and 21581da177e4SLinus Torvalds * byte-range POSIX locks can prohibit a read if they overlap. 21591da177e4SLinus Torvalds * 21601da177e4SLinus Torvalds * N.B. this function is only ever called 21611da177e4SLinus Torvalds * from knfsd and ownership of locks is never checked. 21621da177e4SLinus Torvalds */ 21631da177e4SLinus Torvalds int lock_may_read(struct inode *inode, loff_t start, unsigned long len) 21641da177e4SLinus Torvalds { 21651da177e4SLinus Torvalds struct file_lock *fl; 21661da177e4SLinus Torvalds int result = 1; 21671da177e4SLinus Torvalds lock_kernel(); 21681da177e4SLinus Torvalds for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) { 21691da177e4SLinus Torvalds if (IS_POSIX(fl)) { 21701da177e4SLinus Torvalds if (fl->fl_type == F_RDLCK) 21711da177e4SLinus Torvalds continue; 21721da177e4SLinus Torvalds if ((fl->fl_end < start) || (fl->fl_start > (start + len))) 21731da177e4SLinus Torvalds continue; 21741da177e4SLinus Torvalds } else if (IS_FLOCK(fl)) { 21751da177e4SLinus Torvalds if (!(fl->fl_type & LOCK_MAND)) 21761da177e4SLinus Torvalds continue; 21771da177e4SLinus Torvalds if (fl->fl_type & LOCK_READ) 21781da177e4SLinus Torvalds continue; 21791da177e4SLinus Torvalds } else 21801da177e4SLinus Torvalds continue; 21811da177e4SLinus Torvalds result = 0; 21821da177e4SLinus Torvalds break; 21831da177e4SLinus Torvalds } 21841da177e4SLinus Torvalds unlock_kernel(); 21851da177e4SLinus Torvalds return result; 21861da177e4SLinus Torvalds } 21871da177e4SLinus Torvalds 21881da177e4SLinus Torvalds EXPORT_SYMBOL(lock_may_read); 21891da177e4SLinus Torvalds 21901da177e4SLinus Torvalds /** 21911da177e4SLinus Torvalds * lock_may_write - checks that the region is free of locks 21921da177e4SLinus Torvalds * @inode: the inode that is being written 21931da177e4SLinus Torvalds * @start: the first byte to write 21941da177e4SLinus Torvalds * @len: the number of bytes to write 21951da177e4SLinus Torvalds * 21961da177e4SLinus Torvalds * Emulates Windows locking requirements. Whole-file 21971da177e4SLinus Torvalds * mandatory locks (share modes) can prohibit a write and 21981da177e4SLinus Torvalds * byte-range POSIX locks can prohibit a write if they overlap. 21991da177e4SLinus Torvalds * 22001da177e4SLinus Torvalds * N.B. this function is only ever called 22011da177e4SLinus Torvalds * from knfsd and ownership of locks is never checked. 22021da177e4SLinus Torvalds */ 22031da177e4SLinus Torvalds int lock_may_write(struct inode *inode, loff_t start, unsigned long len) 22041da177e4SLinus Torvalds { 22051da177e4SLinus Torvalds struct file_lock *fl; 22061da177e4SLinus Torvalds int result = 1; 22071da177e4SLinus Torvalds lock_kernel(); 22081da177e4SLinus Torvalds for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) { 22091da177e4SLinus Torvalds if (IS_POSIX(fl)) { 22101da177e4SLinus Torvalds if ((fl->fl_end < start) || (fl->fl_start > (start + len))) 22111da177e4SLinus Torvalds continue; 22121da177e4SLinus Torvalds } else if (IS_FLOCK(fl)) { 22131da177e4SLinus Torvalds if (!(fl->fl_type & LOCK_MAND)) 22141da177e4SLinus Torvalds continue; 22151da177e4SLinus Torvalds if (fl->fl_type & LOCK_WRITE) 22161da177e4SLinus Torvalds continue; 22171da177e4SLinus Torvalds } else 22181da177e4SLinus Torvalds continue; 22191da177e4SLinus Torvalds result = 0; 22201da177e4SLinus Torvalds break; 22211da177e4SLinus Torvalds } 22221da177e4SLinus Torvalds unlock_kernel(); 22231da177e4SLinus Torvalds return result; 22241da177e4SLinus Torvalds } 22251da177e4SLinus Torvalds 22261da177e4SLinus Torvalds EXPORT_SYMBOL(lock_may_write); 22271da177e4SLinus Torvalds 22281da177e4SLinus Torvalds static int __init filelock_init(void) 22291da177e4SLinus Torvalds { 22301da177e4SLinus Torvalds filelock_cache = kmem_cache_create("file_lock_cache", 22311da177e4SLinus Torvalds sizeof(struct file_lock), 0, SLAB_PANIC, 22321da177e4SLinus Torvalds init_once, NULL); 22331da177e4SLinus Torvalds return 0; 22341da177e4SLinus Torvalds } 22351da177e4SLinus Torvalds 22361da177e4SLinus Torvalds core_initcall(filelock_init); 2237