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> 1199f3acc31SAl Viro #include <linux/fdtable.h> 1201da177e4SLinus Torvalds #include <linux/fs.h> 1211da177e4SLinus Torvalds #include <linux/init.h> 1221da177e4SLinus Torvalds #include <linux/module.h> 1231da177e4SLinus Torvalds #include <linux/security.h> 1241da177e4SLinus Torvalds #include <linux/slab.h> 1251da177e4SLinus Torvalds #include <linux/smp_lock.h> 1261da177e4SLinus Torvalds #include <linux/syscalls.h> 1271da177e4SLinus Torvalds #include <linux/time.h> 1284fb3a538SDipankar Sarma #include <linux/rcupdate.h> 129ab1f1611SVitaliy Gusev #include <linux/pid_namespace.h> 1301da177e4SLinus Torvalds 1311da177e4SLinus Torvalds #include <asm/uaccess.h> 1321da177e4SLinus Torvalds 1331da177e4SLinus Torvalds #define IS_POSIX(fl) (fl->fl_flags & FL_POSIX) 1341da177e4SLinus Torvalds #define IS_FLOCK(fl) (fl->fl_flags & FL_FLOCK) 1351da177e4SLinus Torvalds #define IS_LEASE(fl) (fl->fl_flags & FL_LEASE) 1361da177e4SLinus Torvalds 1371da177e4SLinus Torvalds int leases_enable = 1; 1381da177e4SLinus Torvalds int lease_break_time = 45; 1391da177e4SLinus Torvalds 1401da177e4SLinus Torvalds #define for_each_lock(inode, lockp) \ 1411da177e4SLinus Torvalds for (lockp = &inode->i_flock; *lockp != NULL; lockp = &(*lockp)->fl_next) 1421da177e4SLinus Torvalds 14326bcbf96SChristoph Hellwig static LIST_HEAD(file_lock_list); 1441da177e4SLinus Torvalds static LIST_HEAD(blocked_list); 1451da177e4SLinus Torvalds 146b89f4321SArnd Bergmann /* 147b89f4321SArnd Bergmann * Protects the two list heads above, plus the inode->i_flock list 148b89f4321SArnd Bergmann * FIXME: should use a spinlock, once lockd and ceph are ready. 149b89f4321SArnd Bergmann */ 150b89f4321SArnd Bergmann void lock_flocks(void) 151b89f4321SArnd Bergmann { 152b89f4321SArnd Bergmann lock_kernel(); 153b89f4321SArnd Bergmann } 154b89f4321SArnd Bergmann EXPORT_SYMBOL_GPL(lock_flocks); 155b89f4321SArnd Bergmann 156b89f4321SArnd Bergmann void unlock_flocks(void) 157b89f4321SArnd Bergmann { 158b89f4321SArnd Bergmann unlock_kernel(); 159b89f4321SArnd Bergmann } 160b89f4321SArnd Bergmann EXPORT_SYMBOL_GPL(unlock_flocks); 161b89f4321SArnd Bergmann 162e18b890bSChristoph Lameter static struct kmem_cache *filelock_cache __read_mostly; 1631da177e4SLinus Torvalds 1641da177e4SLinus Torvalds /* Allocate an empty lock structure. */ 1651da177e4SLinus Torvalds static struct file_lock *locks_alloc_lock(void) 1661da177e4SLinus Torvalds { 167e94b1766SChristoph Lameter return kmem_cache_alloc(filelock_cache, GFP_KERNEL); 1681da177e4SLinus Torvalds } 1691da177e4SLinus Torvalds 170a9e61e25SFelix Blyakher void locks_release_private(struct file_lock *fl) 17147831f35STrond Myklebust { 17247831f35STrond Myklebust if (fl->fl_ops) { 17347831f35STrond Myklebust if (fl->fl_ops->fl_release_private) 17447831f35STrond Myklebust fl->fl_ops->fl_release_private(fl); 17547831f35STrond Myklebust fl->fl_ops = NULL; 17647831f35STrond Myklebust } 17747831f35STrond Myklebust if (fl->fl_lmops) { 17847831f35STrond Myklebust if (fl->fl_lmops->fl_release_private) 17947831f35STrond Myklebust fl->fl_lmops->fl_release_private(fl); 18047831f35STrond Myklebust fl->fl_lmops = NULL; 18147831f35STrond Myklebust } 18247831f35STrond Myklebust 18347831f35STrond Myklebust } 184a9e61e25SFelix Blyakher EXPORT_SYMBOL_GPL(locks_release_private); 18547831f35STrond Myklebust 1861da177e4SLinus Torvalds /* Free a lock which is not in use. */ 18733443c42SMatt Mackall static void locks_free_lock(struct file_lock *fl) 1881da177e4SLinus Torvalds { 1895ce29646SMiklos Szeredi BUG_ON(waitqueue_active(&fl->fl_wait)); 1905ce29646SMiklos Szeredi BUG_ON(!list_empty(&fl->fl_block)); 1915ce29646SMiklos Szeredi BUG_ON(!list_empty(&fl->fl_link)); 1921da177e4SLinus Torvalds 19347831f35STrond Myklebust locks_release_private(fl); 1941da177e4SLinus Torvalds kmem_cache_free(filelock_cache, fl); 1951da177e4SLinus Torvalds } 1961da177e4SLinus Torvalds 1971da177e4SLinus Torvalds void locks_init_lock(struct file_lock *fl) 1981da177e4SLinus Torvalds { 1991da177e4SLinus Torvalds INIT_LIST_HEAD(&fl->fl_link); 2001da177e4SLinus Torvalds INIT_LIST_HEAD(&fl->fl_block); 2011da177e4SLinus Torvalds init_waitqueue_head(&fl->fl_wait); 2021da177e4SLinus Torvalds fl->fl_next = NULL; 2031da177e4SLinus Torvalds fl->fl_fasync = NULL; 2041da177e4SLinus Torvalds fl->fl_owner = NULL; 2051da177e4SLinus Torvalds fl->fl_pid = 0; 206ab1f1611SVitaliy Gusev fl->fl_nspid = NULL; 2071da177e4SLinus Torvalds fl->fl_file = NULL; 2081da177e4SLinus Torvalds fl->fl_flags = 0; 2091da177e4SLinus Torvalds fl->fl_type = 0; 2101da177e4SLinus Torvalds fl->fl_start = fl->fl_end = 0; 2111da177e4SLinus Torvalds fl->fl_ops = NULL; 2121da177e4SLinus Torvalds fl->fl_lmops = NULL; 2131da177e4SLinus Torvalds } 2141da177e4SLinus Torvalds 2151da177e4SLinus Torvalds EXPORT_SYMBOL(locks_init_lock); 2161da177e4SLinus Torvalds 2171da177e4SLinus Torvalds /* 2181da177e4SLinus Torvalds * Initialises the fields of the file lock which are invariant for 2191da177e4SLinus Torvalds * free file_locks. 2201da177e4SLinus Torvalds */ 22151cc5068SAlexey Dobriyan static void init_once(void *foo) 2221da177e4SLinus Torvalds { 2231da177e4SLinus Torvalds struct file_lock *lock = (struct file_lock *) foo; 2241da177e4SLinus Torvalds 2251da177e4SLinus Torvalds locks_init_lock(lock); 2261da177e4SLinus Torvalds } 2271da177e4SLinus Torvalds 22847831f35STrond Myklebust static void locks_copy_private(struct file_lock *new, struct file_lock *fl) 22947831f35STrond Myklebust { 23047831f35STrond Myklebust if (fl->fl_ops) { 23147831f35STrond Myklebust if (fl->fl_ops->fl_copy_lock) 23247831f35STrond Myklebust fl->fl_ops->fl_copy_lock(new, fl); 23347831f35STrond Myklebust new->fl_ops = fl->fl_ops; 23447831f35STrond Myklebust } 23547831f35STrond Myklebust if (fl->fl_lmops) { 23647831f35STrond Myklebust if (fl->fl_lmops->fl_copy_lock) 23747831f35STrond Myklebust fl->fl_lmops->fl_copy_lock(new, fl); 23847831f35STrond Myklebust new->fl_lmops = fl->fl_lmops; 23947831f35STrond Myklebust } 24047831f35STrond Myklebust } 24147831f35STrond Myklebust 2421da177e4SLinus Torvalds /* 2431da177e4SLinus Torvalds * Initialize a new lock from an existing file_lock structure. 2441da177e4SLinus Torvalds */ 2451a747ee0SJ. Bruce Fields void __locks_copy_lock(struct file_lock *new, const struct file_lock *fl) 2461da177e4SLinus Torvalds { 2471da177e4SLinus Torvalds new->fl_owner = fl->fl_owner; 2481da177e4SLinus Torvalds new->fl_pid = fl->fl_pid; 2490996905fSTrond Myklebust new->fl_file = NULL; 2501da177e4SLinus Torvalds new->fl_flags = fl->fl_flags; 2511da177e4SLinus Torvalds new->fl_type = fl->fl_type; 2521da177e4SLinus Torvalds new->fl_start = fl->fl_start; 2531da177e4SLinus Torvalds new->fl_end = fl->fl_end; 2540996905fSTrond Myklebust new->fl_ops = NULL; 2550996905fSTrond Myklebust new->fl_lmops = NULL; 2560996905fSTrond Myklebust } 2573dd7b71cSRoland Dreier EXPORT_SYMBOL(__locks_copy_lock); 2580996905fSTrond Myklebust 2590996905fSTrond Myklebust void locks_copy_lock(struct file_lock *new, struct file_lock *fl) 2600996905fSTrond Myklebust { 2610996905fSTrond Myklebust locks_release_private(new); 2620996905fSTrond Myklebust 2630996905fSTrond Myklebust __locks_copy_lock(new, fl); 2640996905fSTrond Myklebust new->fl_file = fl->fl_file; 2651da177e4SLinus Torvalds new->fl_ops = fl->fl_ops; 2661da177e4SLinus Torvalds new->fl_lmops = fl->fl_lmops; 26747831f35STrond Myklebust 26847831f35STrond Myklebust locks_copy_private(new, fl); 2691da177e4SLinus Torvalds } 2701da177e4SLinus Torvalds 2711da177e4SLinus Torvalds EXPORT_SYMBOL(locks_copy_lock); 2721da177e4SLinus Torvalds 2731da177e4SLinus Torvalds static inline int flock_translate_cmd(int cmd) { 2741da177e4SLinus Torvalds if (cmd & LOCK_MAND) 2751da177e4SLinus Torvalds return cmd & (LOCK_MAND | LOCK_RW); 2761da177e4SLinus Torvalds switch (cmd) { 2771da177e4SLinus Torvalds case LOCK_SH: 2781da177e4SLinus Torvalds return F_RDLCK; 2791da177e4SLinus Torvalds case LOCK_EX: 2801da177e4SLinus Torvalds return F_WRLCK; 2811da177e4SLinus Torvalds case LOCK_UN: 2821da177e4SLinus Torvalds return F_UNLCK; 2831da177e4SLinus Torvalds } 2841da177e4SLinus Torvalds return -EINVAL; 2851da177e4SLinus Torvalds } 2861da177e4SLinus Torvalds 2871da177e4SLinus Torvalds /* Fill in a file_lock structure with an appropriate FLOCK lock. */ 2881da177e4SLinus Torvalds static int flock_make_lock(struct file *filp, struct file_lock **lock, 2891da177e4SLinus Torvalds unsigned int cmd) 2901da177e4SLinus Torvalds { 2911da177e4SLinus Torvalds struct file_lock *fl; 2921da177e4SLinus Torvalds int type = flock_translate_cmd(cmd); 2931da177e4SLinus Torvalds if (type < 0) 2941da177e4SLinus Torvalds return type; 2951da177e4SLinus Torvalds 2961da177e4SLinus Torvalds fl = locks_alloc_lock(); 2971da177e4SLinus Torvalds if (fl == NULL) 2981da177e4SLinus Torvalds return -ENOMEM; 2991da177e4SLinus Torvalds 3001da177e4SLinus Torvalds fl->fl_file = filp; 3011da177e4SLinus Torvalds fl->fl_pid = current->tgid; 3021da177e4SLinus Torvalds fl->fl_flags = FL_FLOCK; 3031da177e4SLinus Torvalds fl->fl_type = type; 3041da177e4SLinus Torvalds fl->fl_end = OFFSET_MAX; 3051da177e4SLinus Torvalds 3061da177e4SLinus Torvalds *lock = fl; 3071da177e4SLinus Torvalds return 0; 3081da177e4SLinus Torvalds } 3091da177e4SLinus Torvalds 3101da177e4SLinus Torvalds static int assign_type(struct file_lock *fl, int type) 3111da177e4SLinus Torvalds { 3121da177e4SLinus Torvalds switch (type) { 3131da177e4SLinus Torvalds case F_RDLCK: 3141da177e4SLinus Torvalds case F_WRLCK: 3151da177e4SLinus Torvalds case F_UNLCK: 3161da177e4SLinus Torvalds fl->fl_type = type; 3171da177e4SLinus Torvalds break; 3181da177e4SLinus Torvalds default: 3191da177e4SLinus Torvalds return -EINVAL; 3201da177e4SLinus Torvalds } 3211da177e4SLinus Torvalds return 0; 3221da177e4SLinus Torvalds } 3231da177e4SLinus Torvalds 3241da177e4SLinus Torvalds /* Verify a "struct flock" and copy it to a "struct file_lock" as a POSIX 3251da177e4SLinus Torvalds * style lock. 3261da177e4SLinus Torvalds */ 3271da177e4SLinus Torvalds static int flock_to_posix_lock(struct file *filp, struct file_lock *fl, 3281da177e4SLinus Torvalds struct flock *l) 3291da177e4SLinus Torvalds { 3301da177e4SLinus Torvalds off_t start, end; 3311da177e4SLinus Torvalds 3321da177e4SLinus Torvalds switch (l->l_whence) { 333f5579f8cSJosef 'Jeff' Sipek case SEEK_SET: 3341da177e4SLinus Torvalds start = 0; 3351da177e4SLinus Torvalds break; 336f5579f8cSJosef 'Jeff' Sipek case SEEK_CUR: 3371da177e4SLinus Torvalds start = filp->f_pos; 3381da177e4SLinus Torvalds break; 339f5579f8cSJosef 'Jeff' Sipek case SEEK_END: 3400f7fc9e4SJosef "Jeff" Sipek start = i_size_read(filp->f_path.dentry->d_inode); 3411da177e4SLinus Torvalds break; 3421da177e4SLinus Torvalds default: 3431da177e4SLinus Torvalds return -EINVAL; 3441da177e4SLinus Torvalds } 3451da177e4SLinus Torvalds 3461da177e4SLinus Torvalds /* POSIX-1996 leaves the case l->l_len < 0 undefined; 3471da177e4SLinus Torvalds POSIX-2001 defines it. */ 3481da177e4SLinus Torvalds start += l->l_start; 3491da177e4SLinus Torvalds if (start < 0) 3501da177e4SLinus Torvalds return -EINVAL; 3511da177e4SLinus Torvalds fl->fl_end = OFFSET_MAX; 3524c780a46STrond Myklebust if (l->l_len > 0) { 3534c780a46STrond Myklebust end = start + l->l_len - 1; 3544c780a46STrond Myklebust fl->fl_end = end; 3554c780a46STrond Myklebust } else if (l->l_len < 0) { 3564c780a46STrond Myklebust end = start - 1; 3574c780a46STrond Myklebust fl->fl_end = end; 3584c780a46STrond Myklebust start += l->l_len; 3594c780a46STrond Myklebust if (start < 0) 3604c780a46STrond Myklebust return -EINVAL; 3614c780a46STrond Myklebust } 3624c780a46STrond Myklebust fl->fl_start = start; /* we record the absolute position */ 3634c780a46STrond Myklebust if (fl->fl_end < fl->fl_start) 3644c780a46STrond Myklebust return -EOVERFLOW; 3651da177e4SLinus Torvalds 3661da177e4SLinus Torvalds fl->fl_owner = current->files; 3671da177e4SLinus Torvalds fl->fl_pid = current->tgid; 3681da177e4SLinus Torvalds fl->fl_file = filp; 3691da177e4SLinus Torvalds fl->fl_flags = FL_POSIX; 3701da177e4SLinus Torvalds fl->fl_ops = NULL; 3711da177e4SLinus Torvalds fl->fl_lmops = NULL; 3721da177e4SLinus Torvalds 3731da177e4SLinus Torvalds return assign_type(fl, l->l_type); 3741da177e4SLinus Torvalds } 3751da177e4SLinus Torvalds 3761da177e4SLinus Torvalds #if BITS_PER_LONG == 32 3771da177e4SLinus Torvalds static int flock64_to_posix_lock(struct file *filp, struct file_lock *fl, 3781da177e4SLinus Torvalds struct flock64 *l) 3791da177e4SLinus Torvalds { 3801da177e4SLinus Torvalds loff_t start; 3811da177e4SLinus Torvalds 3821da177e4SLinus Torvalds switch (l->l_whence) { 383f5579f8cSJosef 'Jeff' Sipek case SEEK_SET: 3841da177e4SLinus Torvalds start = 0; 3851da177e4SLinus Torvalds break; 386f5579f8cSJosef 'Jeff' Sipek case SEEK_CUR: 3871da177e4SLinus Torvalds start = filp->f_pos; 3881da177e4SLinus Torvalds break; 389f5579f8cSJosef 'Jeff' Sipek case SEEK_END: 3900f7fc9e4SJosef "Jeff" Sipek start = i_size_read(filp->f_path.dentry->d_inode); 3911da177e4SLinus Torvalds break; 3921da177e4SLinus Torvalds default: 3931da177e4SLinus Torvalds return -EINVAL; 3941da177e4SLinus Torvalds } 3951da177e4SLinus Torvalds 3964c780a46STrond Myklebust start += l->l_start; 3974c780a46STrond Myklebust if (start < 0) 3981da177e4SLinus Torvalds return -EINVAL; 3991da177e4SLinus Torvalds fl->fl_end = OFFSET_MAX; 4004c780a46STrond Myklebust if (l->l_len > 0) { 4014c780a46STrond Myklebust fl->fl_end = start + l->l_len - 1; 4024c780a46STrond Myklebust } else if (l->l_len < 0) { 4034c780a46STrond Myklebust fl->fl_end = start - 1; 4044c780a46STrond Myklebust start += l->l_len; 4054c780a46STrond Myklebust if (start < 0) 4064c780a46STrond Myklebust return -EINVAL; 4074c780a46STrond Myklebust } 4084c780a46STrond Myklebust fl->fl_start = start; /* we record the absolute position */ 4094c780a46STrond Myklebust if (fl->fl_end < fl->fl_start) 4104c780a46STrond Myklebust return -EOVERFLOW; 4111da177e4SLinus Torvalds 4121da177e4SLinus Torvalds fl->fl_owner = current->files; 4131da177e4SLinus Torvalds fl->fl_pid = current->tgid; 4141da177e4SLinus Torvalds fl->fl_file = filp; 4151da177e4SLinus Torvalds fl->fl_flags = FL_POSIX; 4161da177e4SLinus Torvalds fl->fl_ops = NULL; 4171da177e4SLinus Torvalds fl->fl_lmops = NULL; 4181da177e4SLinus Torvalds 4191da177e4SLinus Torvalds switch (l->l_type) { 4201da177e4SLinus Torvalds case F_RDLCK: 4211da177e4SLinus Torvalds case F_WRLCK: 4221da177e4SLinus Torvalds case F_UNLCK: 4231da177e4SLinus Torvalds fl->fl_type = l->l_type; 4241da177e4SLinus Torvalds break; 4251da177e4SLinus Torvalds default: 4261da177e4SLinus Torvalds return -EINVAL; 4271da177e4SLinus Torvalds } 4281da177e4SLinus Torvalds 4291da177e4SLinus Torvalds return (0); 4301da177e4SLinus Torvalds } 4311da177e4SLinus Torvalds #endif 4321da177e4SLinus Torvalds 4331da177e4SLinus Torvalds /* default lease lock manager operations */ 4341da177e4SLinus Torvalds static void lease_break_callback(struct file_lock *fl) 4351da177e4SLinus Torvalds { 4361da177e4SLinus Torvalds kill_fasync(&fl->fl_fasync, SIGIO, POLL_MSG); 4371da177e4SLinus Torvalds } 4381da177e4SLinus Torvalds 4391da177e4SLinus Torvalds static void lease_release_private_callback(struct file_lock *fl) 4401da177e4SLinus Torvalds { 4411da177e4SLinus Torvalds if (!fl->fl_file) 4421da177e4SLinus Torvalds return; 4431da177e4SLinus Torvalds 4441da177e4SLinus Torvalds f_delown(fl->fl_file); 4451da177e4SLinus Torvalds fl->fl_file->f_owner.signum = 0; 4461da177e4SLinus Torvalds } 4471da177e4SLinus Torvalds 44875c96f85SAdrian Bunk static int lease_mylease_callback(struct file_lock *fl, struct file_lock *try) 4491da177e4SLinus Torvalds { 4501da177e4SLinus Torvalds return fl->fl_file == try->fl_file; 4511da177e4SLinus Torvalds } 4521da177e4SLinus Torvalds 4537b021967SAlexey Dobriyan static const struct lock_manager_operations lease_manager_ops = { 4541da177e4SLinus Torvalds .fl_break = lease_break_callback, 4551da177e4SLinus Torvalds .fl_release_private = lease_release_private_callback, 4561da177e4SLinus Torvalds .fl_mylease = lease_mylease_callback, 4571da177e4SLinus Torvalds .fl_change = lease_modify, 4581da177e4SLinus Torvalds }; 4591da177e4SLinus Torvalds 4601da177e4SLinus Torvalds /* 4611da177e4SLinus Torvalds * Initialize a lease, use the default lock manager operations 4621da177e4SLinus Torvalds */ 4631da177e4SLinus Torvalds static int lease_init(struct file *filp, int type, struct file_lock *fl) 4641da177e4SLinus Torvalds { 46575dff55aSTrond Myklebust if (assign_type(fl, type) != 0) 46675dff55aSTrond Myklebust return -EINVAL; 46775dff55aSTrond Myklebust 4681da177e4SLinus Torvalds fl->fl_owner = current->files; 4691da177e4SLinus Torvalds fl->fl_pid = current->tgid; 4701da177e4SLinus Torvalds 4711da177e4SLinus Torvalds fl->fl_file = filp; 4721da177e4SLinus Torvalds fl->fl_flags = FL_LEASE; 4731da177e4SLinus Torvalds fl->fl_start = 0; 4741da177e4SLinus Torvalds fl->fl_end = OFFSET_MAX; 4751da177e4SLinus Torvalds fl->fl_ops = NULL; 4761da177e4SLinus Torvalds fl->fl_lmops = &lease_manager_ops; 4771da177e4SLinus Torvalds return 0; 4781da177e4SLinus Torvalds } 4791da177e4SLinus Torvalds 4801da177e4SLinus Torvalds /* Allocate a file_lock initialised to this type of lease */ 481e32b8ee2SJ. Bruce Fields static struct file_lock *lease_alloc(struct file *filp, int type) 4821da177e4SLinus Torvalds { 4831da177e4SLinus Torvalds struct file_lock *fl = locks_alloc_lock(); 48475dff55aSTrond Myklebust int error = -ENOMEM; 4851da177e4SLinus Torvalds 4861da177e4SLinus Torvalds if (fl == NULL) 487e32b8ee2SJ. Bruce Fields return ERR_PTR(error); 4881da177e4SLinus Torvalds 4891da177e4SLinus Torvalds error = lease_init(filp, type, fl); 49075dff55aSTrond Myklebust if (error) { 49175dff55aSTrond Myklebust locks_free_lock(fl); 492e32b8ee2SJ. Bruce Fields return ERR_PTR(error); 49375dff55aSTrond Myklebust } 494e32b8ee2SJ. Bruce Fields return fl; 4951da177e4SLinus Torvalds } 4961da177e4SLinus Torvalds 4971da177e4SLinus Torvalds /* Check if two locks overlap each other. 4981da177e4SLinus Torvalds */ 4991da177e4SLinus Torvalds static inline int locks_overlap(struct file_lock *fl1, struct file_lock *fl2) 5001da177e4SLinus Torvalds { 5011da177e4SLinus Torvalds return ((fl1->fl_end >= fl2->fl_start) && 5021da177e4SLinus Torvalds (fl2->fl_end >= fl1->fl_start)); 5031da177e4SLinus Torvalds } 5041da177e4SLinus Torvalds 5051da177e4SLinus Torvalds /* 5061da177e4SLinus Torvalds * Check whether two locks have the same owner. 5071da177e4SLinus Torvalds */ 50833443c42SMatt Mackall static int posix_same_owner(struct file_lock *fl1, struct file_lock *fl2) 5091da177e4SLinus Torvalds { 5101da177e4SLinus Torvalds if (fl1->fl_lmops && fl1->fl_lmops->fl_compare_owner) 5111da177e4SLinus Torvalds return fl2->fl_lmops == fl1->fl_lmops && 5121da177e4SLinus Torvalds fl1->fl_lmops->fl_compare_owner(fl1, fl2); 5131da177e4SLinus Torvalds return fl1->fl_owner == fl2->fl_owner; 5141da177e4SLinus Torvalds } 5151da177e4SLinus Torvalds 5161da177e4SLinus Torvalds /* Remove waiter from blocker's block list. 5171da177e4SLinus Torvalds * When blocker ends up pointing to itself then the list is empty. 5181da177e4SLinus Torvalds */ 51933443c42SMatt Mackall static void __locks_delete_block(struct file_lock *waiter) 5201da177e4SLinus Torvalds { 5211da177e4SLinus Torvalds list_del_init(&waiter->fl_block); 5221da177e4SLinus Torvalds list_del_init(&waiter->fl_link); 5231da177e4SLinus Torvalds waiter->fl_next = NULL; 5241da177e4SLinus Torvalds } 5251da177e4SLinus Torvalds 5261da177e4SLinus Torvalds /* 5271da177e4SLinus Torvalds */ 5281da177e4SLinus Torvalds static void locks_delete_block(struct file_lock *waiter) 5291da177e4SLinus Torvalds { 530b89f4321SArnd Bergmann lock_flocks(); 5311da177e4SLinus Torvalds __locks_delete_block(waiter); 532b89f4321SArnd Bergmann unlock_flocks(); 5331da177e4SLinus Torvalds } 5341da177e4SLinus Torvalds 5351da177e4SLinus Torvalds /* Insert waiter into blocker's block list. 5361da177e4SLinus Torvalds * We use a circular list so that processes can be easily woken up in 5371da177e4SLinus Torvalds * the order they blocked. The documentation doesn't require this but 5381da177e4SLinus Torvalds * it seems like the reasonable thing to do. 5391da177e4SLinus Torvalds */ 5401da177e4SLinus Torvalds static void locks_insert_block(struct file_lock *blocker, 5411da177e4SLinus Torvalds struct file_lock *waiter) 5421da177e4SLinus Torvalds { 5436dc0fe8fSJ. Bruce Fields BUG_ON(!list_empty(&waiter->fl_block)); 5441da177e4SLinus Torvalds list_add_tail(&waiter->fl_block, &blocker->fl_block); 5451da177e4SLinus Torvalds waiter->fl_next = blocker; 5461da177e4SLinus Torvalds if (IS_POSIX(blocker)) 5471da177e4SLinus Torvalds list_add(&waiter->fl_link, &blocked_list); 5481da177e4SLinus Torvalds } 5491da177e4SLinus Torvalds 5501da177e4SLinus Torvalds /* Wake up processes blocked waiting for blocker. 5511da177e4SLinus Torvalds * If told to wait then schedule the processes until the block list 5521da177e4SLinus Torvalds * is empty, otherwise empty the block list ourselves. 5531da177e4SLinus Torvalds */ 5541da177e4SLinus Torvalds static void locks_wake_up_blocks(struct file_lock *blocker) 5551da177e4SLinus Torvalds { 5561da177e4SLinus Torvalds while (!list_empty(&blocker->fl_block)) { 557f0c1cd0eSPavel Emelyanov struct file_lock *waiter; 558f0c1cd0eSPavel Emelyanov 559f0c1cd0eSPavel Emelyanov waiter = list_first_entry(&blocker->fl_block, 5601da177e4SLinus Torvalds struct file_lock, fl_block); 5611da177e4SLinus Torvalds __locks_delete_block(waiter); 5621da177e4SLinus Torvalds if (waiter->fl_lmops && waiter->fl_lmops->fl_notify) 5631da177e4SLinus Torvalds waiter->fl_lmops->fl_notify(waiter); 5641da177e4SLinus Torvalds else 5651da177e4SLinus Torvalds wake_up(&waiter->fl_wait); 5661da177e4SLinus Torvalds } 5671da177e4SLinus Torvalds } 5681da177e4SLinus Torvalds 5691da177e4SLinus Torvalds /* Insert file lock fl into an inode's lock list at the position indicated 5701da177e4SLinus Torvalds * by pos. At the same time add the lock to the global file lock list. 5711da177e4SLinus Torvalds */ 5721da177e4SLinus Torvalds static void locks_insert_lock(struct file_lock **pos, struct file_lock *fl) 5731da177e4SLinus Torvalds { 5741da177e4SLinus Torvalds list_add(&fl->fl_link, &file_lock_list); 5751da177e4SLinus Torvalds 576ab1f1611SVitaliy Gusev fl->fl_nspid = get_pid(task_tgid(current)); 577ab1f1611SVitaliy Gusev 5781da177e4SLinus Torvalds /* insert into file's list */ 5791da177e4SLinus Torvalds fl->fl_next = *pos; 5801da177e4SLinus Torvalds *pos = fl; 5811da177e4SLinus Torvalds } 5821da177e4SLinus Torvalds 5831da177e4SLinus Torvalds /* 5841da177e4SLinus Torvalds * Delete a lock and then free it. 5851da177e4SLinus Torvalds * Wake up processes that are blocked waiting for this lock, 5861da177e4SLinus Torvalds * notify the FS that the lock has been cleared and 5871da177e4SLinus Torvalds * finally free the lock. 5881da177e4SLinus Torvalds */ 5891da177e4SLinus Torvalds static void locks_delete_lock(struct file_lock **thisfl_p) 5901da177e4SLinus Torvalds { 5911da177e4SLinus Torvalds struct file_lock *fl = *thisfl_p; 5921da177e4SLinus Torvalds 5931da177e4SLinus Torvalds *thisfl_p = fl->fl_next; 5941da177e4SLinus Torvalds fl->fl_next = NULL; 5951da177e4SLinus Torvalds list_del_init(&fl->fl_link); 5961da177e4SLinus Torvalds 5971da177e4SLinus Torvalds fasync_helper(0, fl->fl_file, 0, &fl->fl_fasync); 5981da177e4SLinus Torvalds if (fl->fl_fasync != NULL) { 5991da177e4SLinus Torvalds printk(KERN_ERR "locks_delete_lock: fasync == %p\n", fl->fl_fasync); 6001da177e4SLinus Torvalds fl->fl_fasync = NULL; 6011da177e4SLinus Torvalds } 6021da177e4SLinus Torvalds 603ab1f1611SVitaliy Gusev if (fl->fl_nspid) { 604ab1f1611SVitaliy Gusev put_pid(fl->fl_nspid); 605ab1f1611SVitaliy Gusev fl->fl_nspid = NULL; 606ab1f1611SVitaliy Gusev } 607ab1f1611SVitaliy Gusev 6081da177e4SLinus Torvalds locks_wake_up_blocks(fl); 6091da177e4SLinus Torvalds locks_free_lock(fl); 6101da177e4SLinus Torvalds } 6111da177e4SLinus Torvalds 6121da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. Common functionality 6131da177e4SLinus Torvalds * checks for shared/exclusive status of overlapping locks. 6141da177e4SLinus Torvalds */ 6151da177e4SLinus Torvalds static int locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl) 6161da177e4SLinus Torvalds { 6171da177e4SLinus Torvalds if (sys_fl->fl_type == F_WRLCK) 6181da177e4SLinus Torvalds return 1; 6191da177e4SLinus Torvalds if (caller_fl->fl_type == F_WRLCK) 6201da177e4SLinus Torvalds return 1; 6211da177e4SLinus Torvalds return 0; 6221da177e4SLinus Torvalds } 6231da177e4SLinus Torvalds 6241da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. POSIX specific 6251da177e4SLinus Torvalds * checking before calling the locks_conflict(). 6261da177e4SLinus Torvalds */ 6271da177e4SLinus Torvalds static int posix_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl) 6281da177e4SLinus Torvalds { 6291da177e4SLinus Torvalds /* POSIX locks owned by the same process do not conflict with 6301da177e4SLinus Torvalds * each other. 6311da177e4SLinus Torvalds */ 6321da177e4SLinus Torvalds if (!IS_POSIX(sys_fl) || posix_same_owner(caller_fl, sys_fl)) 6331da177e4SLinus Torvalds return (0); 6341da177e4SLinus Torvalds 6351da177e4SLinus Torvalds /* Check whether they overlap */ 6361da177e4SLinus Torvalds if (!locks_overlap(caller_fl, sys_fl)) 6371da177e4SLinus Torvalds return 0; 6381da177e4SLinus Torvalds 6391da177e4SLinus Torvalds return (locks_conflict(caller_fl, sys_fl)); 6401da177e4SLinus Torvalds } 6411da177e4SLinus Torvalds 6421da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. FLOCK specific 6431da177e4SLinus Torvalds * checking before calling the locks_conflict(). 6441da177e4SLinus Torvalds */ 6451da177e4SLinus Torvalds static int flock_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl) 6461da177e4SLinus Torvalds { 6471da177e4SLinus Torvalds /* FLOCK locks referring to the same filp do not conflict with 6481da177e4SLinus Torvalds * each other. 6491da177e4SLinus Torvalds */ 6501da177e4SLinus Torvalds if (!IS_FLOCK(sys_fl) || (caller_fl->fl_file == sys_fl->fl_file)) 6511da177e4SLinus Torvalds return (0); 6521da177e4SLinus Torvalds if ((caller_fl->fl_type & LOCK_MAND) || (sys_fl->fl_type & LOCK_MAND)) 6531da177e4SLinus Torvalds return 0; 6541da177e4SLinus Torvalds 6551da177e4SLinus Torvalds return (locks_conflict(caller_fl, sys_fl)); 6561da177e4SLinus Torvalds } 6571da177e4SLinus Torvalds 6586d34ac19SJ. Bruce Fields void 6599d6a8c5cSMarc Eshel posix_test_lock(struct file *filp, struct file_lock *fl) 6601da177e4SLinus Torvalds { 6611da177e4SLinus Torvalds struct file_lock *cfl; 6621da177e4SLinus Torvalds 663b89f4321SArnd Bergmann lock_flocks(); 6640f7fc9e4SJosef "Jeff" Sipek for (cfl = filp->f_path.dentry->d_inode->i_flock; cfl; cfl = cfl->fl_next) { 6651da177e4SLinus Torvalds if (!IS_POSIX(cfl)) 6661da177e4SLinus Torvalds continue; 667b842e240SJ. Bruce Fields if (posix_locks_conflict(fl, cfl)) 6681da177e4SLinus Torvalds break; 6691da177e4SLinus Torvalds } 670ab1f1611SVitaliy Gusev if (cfl) { 6719d6a8c5cSMarc Eshel __locks_copy_lock(fl, cfl); 672ab1f1611SVitaliy Gusev if (cfl->fl_nspid) 6736c5f3e7bSPavel Emelyanov fl->fl_pid = pid_vnr(cfl->fl_nspid); 674ab1f1611SVitaliy Gusev } else 675129a84deSJ. Bruce Fields fl->fl_type = F_UNLCK; 676b89f4321SArnd Bergmann unlock_flocks(); 6776d34ac19SJ. Bruce Fields return; 6781da177e4SLinus Torvalds } 6791da177e4SLinus Torvalds EXPORT_SYMBOL(posix_test_lock); 6801da177e4SLinus Torvalds 681b533184fSJ. Bruce Fields /* 682b533184fSJ. Bruce Fields * Deadlock detection: 6831da177e4SLinus Torvalds * 684b533184fSJ. Bruce Fields * We attempt to detect deadlocks that are due purely to posix file 685b533184fSJ. Bruce Fields * locks. 6861da177e4SLinus Torvalds * 687b533184fSJ. Bruce Fields * We assume that a task can be waiting for at most one lock at a time. 688b533184fSJ. Bruce Fields * So for any acquired lock, the process holding that lock may be 689b533184fSJ. Bruce Fields * waiting on at most one other lock. That lock in turns may be held by 690b533184fSJ. Bruce Fields * someone waiting for at most one other lock. Given a requested lock 691b533184fSJ. Bruce Fields * caller_fl which is about to wait for a conflicting lock block_fl, we 692b533184fSJ. Bruce Fields * follow this chain of waiters to ensure we are not about to create a 693b533184fSJ. Bruce Fields * cycle. 69497855b49SJ. Bruce Fields * 695b533184fSJ. Bruce Fields * Since we do this before we ever put a process to sleep on a lock, we 696b533184fSJ. Bruce Fields * are ensured that there is never a cycle; that is what guarantees that 697b533184fSJ. Bruce Fields * the while() loop in posix_locks_deadlock() eventually completes. 698b533184fSJ. Bruce Fields * 699b533184fSJ. Bruce Fields * Note: the above assumption may not be true when handling lock 700b533184fSJ. Bruce Fields * requests from a broken NFS client. It may also fail in the presence 701b533184fSJ. Bruce Fields * of tasks (such as posix threads) sharing the same open file table. 702b533184fSJ. Bruce Fields * 703b533184fSJ. Bruce Fields * To handle those cases, we just bail out after a few iterations. 7041da177e4SLinus Torvalds */ 70597855b49SJ. Bruce Fields 70697855b49SJ. Bruce Fields #define MAX_DEADLK_ITERATIONS 10 70797855b49SJ. Bruce Fields 708b533184fSJ. Bruce Fields /* Find a lock that the owner of the given block_fl is blocking on. */ 709b533184fSJ. Bruce Fields static struct file_lock *what_owner_is_waiting_for(struct file_lock *block_fl) 710b533184fSJ. Bruce Fields { 711b533184fSJ. Bruce Fields struct file_lock *fl; 712b533184fSJ. Bruce Fields 713b533184fSJ. Bruce Fields list_for_each_entry(fl, &blocked_list, fl_link) { 714b533184fSJ. Bruce Fields if (posix_same_owner(fl, block_fl)) 715b533184fSJ. Bruce Fields return fl->fl_next; 716b533184fSJ. Bruce Fields } 717b533184fSJ. Bruce Fields return NULL; 718b533184fSJ. Bruce Fields } 719b533184fSJ. Bruce Fields 720b0904e14SAdrian Bunk static int posix_locks_deadlock(struct file_lock *caller_fl, 7211da177e4SLinus Torvalds struct file_lock *block_fl) 7221da177e4SLinus Torvalds { 72397855b49SJ. Bruce Fields int i = 0; 7241da177e4SLinus Torvalds 725b533184fSJ. Bruce Fields while ((block_fl = what_owner_is_waiting_for(block_fl))) { 72697855b49SJ. Bruce Fields if (i++ > MAX_DEADLK_ITERATIONS) 72797855b49SJ. Bruce Fields return 0; 728b533184fSJ. Bruce Fields if (posix_same_owner(caller_fl, block_fl)) 729b533184fSJ. Bruce Fields return 1; 7301da177e4SLinus Torvalds } 7311da177e4SLinus Torvalds return 0; 7321da177e4SLinus Torvalds } 7331da177e4SLinus Torvalds 7341da177e4SLinus Torvalds /* Try to create a FLOCK lock on filp. We always insert new FLOCK locks 73502888f41SJ. Bruce Fields * after any leases, but before any posix locks. 736f475ae95STrond Myklebust * 737f475ae95STrond Myklebust * Note that if called with an FL_EXISTS argument, the caller may determine 738f475ae95STrond Myklebust * whether or not a lock was successfully freed by testing the return 739f475ae95STrond Myklebust * value for -ENOENT. 7401da177e4SLinus Torvalds */ 741993dfa87STrond Myklebust static int flock_lock_file(struct file *filp, struct file_lock *request) 7421da177e4SLinus Torvalds { 743993dfa87STrond Myklebust struct file_lock *new_fl = NULL; 7441da177e4SLinus Torvalds struct file_lock **before; 7450f7fc9e4SJosef "Jeff" Sipek struct inode * inode = filp->f_path.dentry->d_inode; 7461da177e4SLinus Torvalds int error = 0; 7471da177e4SLinus Torvalds int found = 0; 7481da177e4SLinus Torvalds 749b89f4321SArnd Bergmann if (!(request->fl_flags & FL_ACCESS) && (request->fl_type != F_UNLCK)) { 750b89f4321SArnd Bergmann new_fl = locks_alloc_lock(); 751b89f4321SArnd Bergmann if (!new_fl) 752b89f4321SArnd Bergmann return -ENOMEM; 753b89f4321SArnd Bergmann } 754b89f4321SArnd Bergmann 755b89f4321SArnd Bergmann lock_flocks(); 756f07f18ddSTrond Myklebust if (request->fl_flags & FL_ACCESS) 757f07f18ddSTrond Myklebust goto find_conflict; 75884d535adSPavel Emelyanov 7591da177e4SLinus Torvalds for_each_lock(inode, before) { 7601da177e4SLinus Torvalds struct file_lock *fl = *before; 7611da177e4SLinus Torvalds if (IS_POSIX(fl)) 7621da177e4SLinus Torvalds break; 7631da177e4SLinus Torvalds if (IS_LEASE(fl)) 7641da177e4SLinus Torvalds continue; 7651da177e4SLinus Torvalds if (filp != fl->fl_file) 7661da177e4SLinus Torvalds continue; 767993dfa87STrond Myklebust if (request->fl_type == fl->fl_type) 7681da177e4SLinus Torvalds goto out; 7691da177e4SLinus Torvalds found = 1; 7701da177e4SLinus Torvalds locks_delete_lock(before); 7711da177e4SLinus Torvalds break; 7721da177e4SLinus Torvalds } 7731da177e4SLinus Torvalds 774f475ae95STrond Myklebust if (request->fl_type == F_UNLCK) { 775f475ae95STrond Myklebust if ((request->fl_flags & FL_EXISTS) && !found) 776f475ae95STrond Myklebust error = -ENOENT; 777993dfa87STrond Myklebust goto out; 778f475ae95STrond Myklebust } 7791da177e4SLinus Torvalds 7801da177e4SLinus Torvalds /* 7811da177e4SLinus Torvalds * If a higher-priority process was blocked on the old file lock, 7821da177e4SLinus Torvalds * give it the opportunity to lock the file. 7831da177e4SLinus Torvalds */ 784b89f4321SArnd Bergmann if (found) { 785b89f4321SArnd Bergmann unlock_flocks(); 786def01bc5SFrederic Weisbecker cond_resched(); 787b89f4321SArnd Bergmann lock_flocks(); 788b89f4321SArnd Bergmann } 7891da177e4SLinus Torvalds 790f07f18ddSTrond Myklebust find_conflict: 7911da177e4SLinus Torvalds for_each_lock(inode, before) { 7921da177e4SLinus Torvalds struct file_lock *fl = *before; 7931da177e4SLinus Torvalds if (IS_POSIX(fl)) 7941da177e4SLinus Torvalds break; 7951da177e4SLinus Torvalds if (IS_LEASE(fl)) 7961da177e4SLinus Torvalds continue; 797993dfa87STrond Myklebust if (!flock_locks_conflict(request, fl)) 7981da177e4SLinus Torvalds continue; 7991da177e4SLinus Torvalds error = -EAGAIN; 800bde74e4bSMiklos Szeredi if (!(request->fl_flags & FL_SLEEP)) 801bde74e4bSMiklos Szeredi goto out; 802bde74e4bSMiklos Szeredi error = FILE_LOCK_DEFERRED; 803993dfa87STrond Myklebust locks_insert_block(fl, request); 8041da177e4SLinus Torvalds goto out; 8051da177e4SLinus Torvalds } 806f07f18ddSTrond Myklebust if (request->fl_flags & FL_ACCESS) 807f07f18ddSTrond Myklebust goto out; 808993dfa87STrond Myklebust locks_copy_lock(new_fl, request); 8090e2f6db8SPavel Emelyanov locks_insert_lock(before, new_fl); 810993dfa87STrond Myklebust new_fl = NULL; 8119cedc194SKirill Korotaev error = 0; 8121da177e4SLinus Torvalds 8131da177e4SLinus Torvalds out: 814b89f4321SArnd Bergmann unlock_flocks(); 815993dfa87STrond Myklebust if (new_fl) 816993dfa87STrond Myklebust locks_free_lock(new_fl); 8171da177e4SLinus Torvalds return error; 8181da177e4SLinus Torvalds } 8191da177e4SLinus Torvalds 820150b3934SMarc Eshel static int __posix_lock_file(struct inode *inode, struct file_lock *request, struct file_lock *conflock) 8211da177e4SLinus Torvalds { 8221da177e4SLinus Torvalds struct file_lock *fl; 82339005d02SMiklos Szeredi struct file_lock *new_fl = NULL; 82439005d02SMiklos Szeredi struct file_lock *new_fl2 = NULL; 8251da177e4SLinus Torvalds struct file_lock *left = NULL; 8261da177e4SLinus Torvalds struct file_lock *right = NULL; 8271da177e4SLinus Torvalds struct file_lock **before; 8281da177e4SLinus Torvalds int error, added = 0; 8291da177e4SLinus Torvalds 8301da177e4SLinus Torvalds /* 8311da177e4SLinus Torvalds * We may need two file_lock structures for this operation, 8321da177e4SLinus Torvalds * so we get them in advance to avoid races. 83339005d02SMiklos Szeredi * 83439005d02SMiklos Szeredi * In some cases we can be sure, that no new locks will be needed 8351da177e4SLinus Torvalds */ 83639005d02SMiklos Szeredi if (!(request->fl_flags & FL_ACCESS) && 83739005d02SMiklos Szeredi (request->fl_type != F_UNLCK || 83839005d02SMiklos Szeredi request->fl_start != 0 || request->fl_end != OFFSET_MAX)) { 8391da177e4SLinus Torvalds new_fl = locks_alloc_lock(); 8401da177e4SLinus Torvalds new_fl2 = locks_alloc_lock(); 84139005d02SMiklos Szeredi } 8421da177e4SLinus Torvalds 843b89f4321SArnd Bergmann lock_flocks(); 8441da177e4SLinus Torvalds if (request->fl_type != F_UNLCK) { 8451da177e4SLinus Torvalds for_each_lock(inode, before) { 846526985b9SJ. Bruce Fields fl = *before; 8471da177e4SLinus Torvalds if (!IS_POSIX(fl)) 8481da177e4SLinus Torvalds continue; 8491da177e4SLinus Torvalds if (!posix_locks_conflict(request, fl)) 8501da177e4SLinus Torvalds continue; 8515842add2SAndy Adamson if (conflock) 8521a747ee0SJ. Bruce Fields __locks_copy_lock(conflock, fl); 8531da177e4SLinus Torvalds error = -EAGAIN; 8541da177e4SLinus Torvalds if (!(request->fl_flags & FL_SLEEP)) 8551da177e4SLinus Torvalds goto out; 8561da177e4SLinus Torvalds error = -EDEADLK; 8571da177e4SLinus Torvalds if (posix_locks_deadlock(request, fl)) 8581da177e4SLinus Torvalds goto out; 859bde74e4bSMiklos Szeredi error = FILE_LOCK_DEFERRED; 8601da177e4SLinus Torvalds locks_insert_block(fl, request); 8611da177e4SLinus Torvalds goto out; 8621da177e4SLinus Torvalds } 8631da177e4SLinus Torvalds } 8641da177e4SLinus Torvalds 8651da177e4SLinus Torvalds /* If we're just looking for a conflict, we're done. */ 8661da177e4SLinus Torvalds error = 0; 8671da177e4SLinus Torvalds if (request->fl_flags & FL_ACCESS) 8681da177e4SLinus Torvalds goto out; 8691da177e4SLinus Torvalds 8701da177e4SLinus Torvalds /* 8711da177e4SLinus Torvalds * Find the first old lock with the same owner as the new lock. 8721da177e4SLinus Torvalds */ 8731da177e4SLinus Torvalds 8741da177e4SLinus Torvalds before = &inode->i_flock; 8751da177e4SLinus Torvalds 8761da177e4SLinus Torvalds /* First skip locks owned by other processes. */ 8771da177e4SLinus Torvalds while ((fl = *before) && (!IS_POSIX(fl) || 8781da177e4SLinus Torvalds !posix_same_owner(request, fl))) { 8791da177e4SLinus Torvalds before = &fl->fl_next; 8801da177e4SLinus Torvalds } 8811da177e4SLinus Torvalds 8821da177e4SLinus Torvalds /* Process locks with this owner. */ 8831da177e4SLinus Torvalds while ((fl = *before) && posix_same_owner(request, fl)) { 8841da177e4SLinus Torvalds /* Detect adjacent or overlapping regions (if same lock type) 8851da177e4SLinus Torvalds */ 8861da177e4SLinus Torvalds if (request->fl_type == fl->fl_type) { 887449231d6SOlaf Kirch /* In all comparisons of start vs end, use 888449231d6SOlaf Kirch * "start - 1" rather than "end + 1". If end 889449231d6SOlaf Kirch * is OFFSET_MAX, end + 1 will become negative. 890449231d6SOlaf Kirch */ 8911da177e4SLinus Torvalds if (fl->fl_end < request->fl_start - 1) 8921da177e4SLinus Torvalds goto next_lock; 8931da177e4SLinus Torvalds /* If the next lock in the list has entirely bigger 8941da177e4SLinus Torvalds * addresses than the new one, insert the lock here. 8951da177e4SLinus Torvalds */ 896449231d6SOlaf Kirch if (fl->fl_start - 1 > request->fl_end) 8971da177e4SLinus Torvalds break; 8981da177e4SLinus Torvalds 8991da177e4SLinus Torvalds /* If we come here, the new and old lock are of the 9001da177e4SLinus Torvalds * same type and adjacent or overlapping. Make one 9011da177e4SLinus Torvalds * lock yielding from the lower start address of both 9021da177e4SLinus Torvalds * locks to the higher end address. 9031da177e4SLinus Torvalds */ 9041da177e4SLinus Torvalds if (fl->fl_start > request->fl_start) 9051da177e4SLinus Torvalds fl->fl_start = request->fl_start; 9061da177e4SLinus Torvalds else 9071da177e4SLinus Torvalds request->fl_start = fl->fl_start; 9081da177e4SLinus Torvalds if (fl->fl_end < request->fl_end) 9091da177e4SLinus Torvalds fl->fl_end = request->fl_end; 9101da177e4SLinus Torvalds else 9111da177e4SLinus Torvalds request->fl_end = fl->fl_end; 9121da177e4SLinus Torvalds if (added) { 9131da177e4SLinus Torvalds locks_delete_lock(before); 9141da177e4SLinus Torvalds continue; 9151da177e4SLinus Torvalds } 9161da177e4SLinus Torvalds request = fl; 9171da177e4SLinus Torvalds added = 1; 9181da177e4SLinus Torvalds } 9191da177e4SLinus Torvalds else { 9201da177e4SLinus Torvalds /* Processing for different lock types is a bit 9211da177e4SLinus Torvalds * more complex. 9221da177e4SLinus Torvalds */ 9231da177e4SLinus Torvalds if (fl->fl_end < request->fl_start) 9241da177e4SLinus Torvalds goto next_lock; 9251da177e4SLinus Torvalds if (fl->fl_start > request->fl_end) 9261da177e4SLinus Torvalds break; 9271da177e4SLinus Torvalds if (request->fl_type == F_UNLCK) 9281da177e4SLinus Torvalds added = 1; 9291da177e4SLinus Torvalds if (fl->fl_start < request->fl_start) 9301da177e4SLinus Torvalds left = fl; 9311da177e4SLinus Torvalds /* If the next lock in the list has a higher end 9321da177e4SLinus Torvalds * address than the new one, insert the new one here. 9331da177e4SLinus Torvalds */ 9341da177e4SLinus Torvalds if (fl->fl_end > request->fl_end) { 9351da177e4SLinus Torvalds right = fl; 9361da177e4SLinus Torvalds break; 9371da177e4SLinus Torvalds } 9381da177e4SLinus Torvalds if (fl->fl_start >= request->fl_start) { 9391da177e4SLinus Torvalds /* The new lock completely replaces an old 9401da177e4SLinus Torvalds * one (This may happen several times). 9411da177e4SLinus Torvalds */ 9421da177e4SLinus Torvalds if (added) { 9431da177e4SLinus Torvalds locks_delete_lock(before); 9441da177e4SLinus Torvalds continue; 9451da177e4SLinus Torvalds } 9461da177e4SLinus Torvalds /* Replace the old lock with the new one. 9471da177e4SLinus Torvalds * Wake up anybody waiting for the old one, 9481da177e4SLinus Torvalds * as the change in lock type might satisfy 9491da177e4SLinus Torvalds * their needs. 9501da177e4SLinus Torvalds */ 9511da177e4SLinus Torvalds locks_wake_up_blocks(fl); 9521da177e4SLinus Torvalds fl->fl_start = request->fl_start; 9531da177e4SLinus Torvalds fl->fl_end = request->fl_end; 9541da177e4SLinus Torvalds fl->fl_type = request->fl_type; 95547831f35STrond Myklebust locks_release_private(fl); 95647831f35STrond Myklebust locks_copy_private(fl, request); 9571da177e4SLinus Torvalds request = fl; 9581da177e4SLinus Torvalds added = 1; 9591da177e4SLinus Torvalds } 9601da177e4SLinus Torvalds } 9611da177e4SLinus Torvalds /* Go on to next lock. 9621da177e4SLinus Torvalds */ 9631da177e4SLinus Torvalds next_lock: 9641da177e4SLinus Torvalds before = &fl->fl_next; 9651da177e4SLinus Torvalds } 9661da177e4SLinus Torvalds 9670d9a490aSMiklos Szeredi /* 9680d9a490aSMiklos Szeredi * The above code only modifies existing locks in case of 9690d9a490aSMiklos Szeredi * merging or replacing. If new lock(s) need to be inserted 9700d9a490aSMiklos Szeredi * all modifications are done bellow this, so it's safe yet to 9710d9a490aSMiklos Szeredi * bail out. 9720d9a490aSMiklos Szeredi */ 9730d9a490aSMiklos Szeredi error = -ENOLCK; /* "no luck" */ 9740d9a490aSMiklos Szeredi if (right && left == right && !new_fl2) 9750d9a490aSMiklos Szeredi goto out; 9760d9a490aSMiklos Szeredi 9771da177e4SLinus Torvalds error = 0; 9781da177e4SLinus Torvalds if (!added) { 979f475ae95STrond Myklebust if (request->fl_type == F_UNLCK) { 980f475ae95STrond Myklebust if (request->fl_flags & FL_EXISTS) 981f475ae95STrond Myklebust error = -ENOENT; 9821da177e4SLinus Torvalds goto out; 983f475ae95STrond Myklebust } 9840d9a490aSMiklos Szeredi 9850d9a490aSMiklos Szeredi if (!new_fl) { 9860d9a490aSMiklos Szeredi error = -ENOLCK; 9870d9a490aSMiklos Szeredi goto out; 9880d9a490aSMiklos Szeredi } 9891da177e4SLinus Torvalds locks_copy_lock(new_fl, request); 9901da177e4SLinus Torvalds locks_insert_lock(before, new_fl); 9911da177e4SLinus Torvalds new_fl = NULL; 9921da177e4SLinus Torvalds } 9931da177e4SLinus Torvalds if (right) { 9941da177e4SLinus Torvalds if (left == right) { 9951da177e4SLinus Torvalds /* The new lock breaks the old one in two pieces, 9961da177e4SLinus Torvalds * so we have to use the second new lock. 9971da177e4SLinus Torvalds */ 9981da177e4SLinus Torvalds left = new_fl2; 9991da177e4SLinus Torvalds new_fl2 = NULL; 10001da177e4SLinus Torvalds locks_copy_lock(left, right); 10011da177e4SLinus Torvalds locks_insert_lock(before, left); 10021da177e4SLinus Torvalds } 10031da177e4SLinus Torvalds right->fl_start = request->fl_end + 1; 10041da177e4SLinus Torvalds locks_wake_up_blocks(right); 10051da177e4SLinus Torvalds } 10061da177e4SLinus Torvalds if (left) { 10071da177e4SLinus Torvalds left->fl_end = request->fl_start - 1; 10081da177e4SLinus Torvalds locks_wake_up_blocks(left); 10091da177e4SLinus Torvalds } 10101da177e4SLinus Torvalds out: 1011b89f4321SArnd Bergmann unlock_flocks(); 10121da177e4SLinus Torvalds /* 10131da177e4SLinus Torvalds * Free any unused locks. 10141da177e4SLinus Torvalds */ 10151da177e4SLinus Torvalds if (new_fl) 10161da177e4SLinus Torvalds locks_free_lock(new_fl); 10171da177e4SLinus Torvalds if (new_fl2) 10181da177e4SLinus Torvalds locks_free_lock(new_fl2); 10191da177e4SLinus Torvalds return error; 10201da177e4SLinus Torvalds } 10211da177e4SLinus Torvalds 10221da177e4SLinus Torvalds /** 10231da177e4SLinus Torvalds * posix_lock_file - Apply a POSIX-style lock to a file 10241da177e4SLinus Torvalds * @filp: The file to apply the lock to 10251da177e4SLinus Torvalds * @fl: The lock to be applied 1026150b3934SMarc Eshel * @conflock: Place to return a copy of the conflicting lock, if found. 10271da177e4SLinus Torvalds * 10281da177e4SLinus Torvalds * Add a POSIX style lock to a file. 10291da177e4SLinus Torvalds * We merge adjacent & overlapping locks whenever possible. 10301da177e4SLinus Torvalds * POSIX locks are sorted by owner task, then by starting address 1031f475ae95STrond Myklebust * 1032f475ae95STrond Myklebust * Note that if called with an FL_EXISTS argument, the caller may determine 1033f475ae95STrond Myklebust * whether or not a lock was successfully freed by testing the return 1034f475ae95STrond Myklebust * value for -ENOENT. 10351da177e4SLinus Torvalds */ 1036150b3934SMarc Eshel int posix_lock_file(struct file *filp, struct file_lock *fl, 10375842add2SAndy Adamson struct file_lock *conflock) 10385842add2SAndy Adamson { 1039150b3934SMarc Eshel return __posix_lock_file(filp->f_path.dentry->d_inode, fl, conflock); 10405842add2SAndy Adamson } 1041150b3934SMarc Eshel EXPORT_SYMBOL(posix_lock_file); 10421da177e4SLinus Torvalds 10431da177e4SLinus Torvalds /** 10441da177e4SLinus Torvalds * posix_lock_file_wait - Apply a POSIX-style lock to a file 10451da177e4SLinus Torvalds * @filp: The file to apply the lock to 10461da177e4SLinus Torvalds * @fl: The lock to be applied 10471da177e4SLinus Torvalds * 10481da177e4SLinus Torvalds * Add a POSIX style lock to a file. 10491da177e4SLinus Torvalds * We merge adjacent & overlapping locks whenever possible. 10501da177e4SLinus Torvalds * POSIX locks are sorted by owner task, then by starting address 10511da177e4SLinus Torvalds */ 10521da177e4SLinus Torvalds int posix_lock_file_wait(struct file *filp, struct file_lock *fl) 10531da177e4SLinus Torvalds { 10541da177e4SLinus Torvalds int error; 10551da177e4SLinus Torvalds might_sleep (); 10561da177e4SLinus Torvalds for (;;) { 1057150b3934SMarc Eshel error = posix_lock_file(filp, fl, NULL); 1058bde74e4bSMiklos Szeredi if (error != FILE_LOCK_DEFERRED) 10591da177e4SLinus Torvalds break; 10601da177e4SLinus Torvalds error = wait_event_interruptible(fl->fl_wait, !fl->fl_next); 10611da177e4SLinus Torvalds if (!error) 10621da177e4SLinus Torvalds continue; 10631da177e4SLinus Torvalds 10641da177e4SLinus Torvalds locks_delete_block(fl); 10651da177e4SLinus Torvalds break; 10661da177e4SLinus Torvalds } 10671da177e4SLinus Torvalds return error; 10681da177e4SLinus Torvalds } 10691da177e4SLinus Torvalds EXPORT_SYMBOL(posix_lock_file_wait); 10701da177e4SLinus Torvalds 10711da177e4SLinus Torvalds /** 10721da177e4SLinus Torvalds * locks_mandatory_locked - Check for an active lock 10731da177e4SLinus Torvalds * @inode: the file to check 10741da177e4SLinus Torvalds * 10751da177e4SLinus Torvalds * Searches the inode's list of locks to find any POSIX locks which conflict. 10761da177e4SLinus Torvalds * This function is called from locks_verify_locked() only. 10771da177e4SLinus Torvalds */ 10781da177e4SLinus Torvalds int locks_mandatory_locked(struct inode *inode) 10791da177e4SLinus Torvalds { 10801da177e4SLinus Torvalds fl_owner_t owner = current->files; 10811da177e4SLinus Torvalds struct file_lock *fl; 10821da177e4SLinus Torvalds 10831da177e4SLinus Torvalds /* 10841da177e4SLinus Torvalds * Search the lock list for this inode for any POSIX locks. 10851da177e4SLinus Torvalds */ 1086b89f4321SArnd Bergmann lock_flocks(); 10871da177e4SLinus Torvalds for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) { 10881da177e4SLinus Torvalds if (!IS_POSIX(fl)) 10891da177e4SLinus Torvalds continue; 10901da177e4SLinus Torvalds if (fl->fl_owner != owner) 10911da177e4SLinus Torvalds break; 10921da177e4SLinus Torvalds } 1093b89f4321SArnd Bergmann unlock_flocks(); 10941da177e4SLinus Torvalds return fl ? -EAGAIN : 0; 10951da177e4SLinus Torvalds } 10961da177e4SLinus Torvalds 10971da177e4SLinus Torvalds /** 10981da177e4SLinus Torvalds * locks_mandatory_area - Check for a conflicting lock 10991da177e4SLinus Torvalds * @read_write: %FLOCK_VERIFY_WRITE for exclusive access, %FLOCK_VERIFY_READ 11001da177e4SLinus Torvalds * for shared 11011da177e4SLinus Torvalds * @inode: the file to check 11021da177e4SLinus Torvalds * @filp: how the file was opened (if it was) 11031da177e4SLinus Torvalds * @offset: start of area to check 11041da177e4SLinus Torvalds * @count: length of area to check 11051da177e4SLinus Torvalds * 11061da177e4SLinus Torvalds * Searches the inode's list of locks to find any POSIX locks which conflict. 11071da177e4SLinus Torvalds * This function is called from rw_verify_area() and 11081da177e4SLinus Torvalds * locks_verify_truncate(). 11091da177e4SLinus Torvalds */ 11101da177e4SLinus Torvalds int locks_mandatory_area(int read_write, struct inode *inode, 11111da177e4SLinus Torvalds struct file *filp, loff_t offset, 11121da177e4SLinus Torvalds size_t count) 11131da177e4SLinus Torvalds { 11141da177e4SLinus Torvalds struct file_lock fl; 11151da177e4SLinus Torvalds int error; 11161da177e4SLinus Torvalds 11171da177e4SLinus Torvalds locks_init_lock(&fl); 11181da177e4SLinus Torvalds fl.fl_owner = current->files; 11191da177e4SLinus Torvalds fl.fl_pid = current->tgid; 11201da177e4SLinus Torvalds fl.fl_file = filp; 11211da177e4SLinus Torvalds fl.fl_flags = FL_POSIX | FL_ACCESS; 11221da177e4SLinus Torvalds if (filp && !(filp->f_flags & O_NONBLOCK)) 11231da177e4SLinus Torvalds fl.fl_flags |= FL_SLEEP; 11241da177e4SLinus Torvalds fl.fl_type = (read_write == FLOCK_VERIFY_WRITE) ? F_WRLCK : F_RDLCK; 11251da177e4SLinus Torvalds fl.fl_start = offset; 11261da177e4SLinus Torvalds fl.fl_end = offset + count - 1; 11271da177e4SLinus Torvalds 11281da177e4SLinus Torvalds for (;;) { 1129150b3934SMarc Eshel error = __posix_lock_file(inode, &fl, NULL); 1130bde74e4bSMiklos Szeredi if (error != FILE_LOCK_DEFERRED) 11311da177e4SLinus Torvalds break; 11321da177e4SLinus Torvalds error = wait_event_interruptible(fl.fl_wait, !fl.fl_next); 11331da177e4SLinus Torvalds if (!error) { 11341da177e4SLinus Torvalds /* 11351da177e4SLinus Torvalds * If we've been sleeping someone might have 11361da177e4SLinus Torvalds * changed the permissions behind our back. 11371da177e4SLinus Torvalds */ 1138a16877caSPavel Emelyanov if (__mandatory_lock(inode)) 11391da177e4SLinus Torvalds continue; 11401da177e4SLinus Torvalds } 11411da177e4SLinus Torvalds 11421da177e4SLinus Torvalds locks_delete_block(&fl); 11431da177e4SLinus Torvalds break; 11441da177e4SLinus Torvalds } 11451da177e4SLinus Torvalds 11461da177e4SLinus Torvalds return error; 11471da177e4SLinus Torvalds } 11481da177e4SLinus Torvalds 11491da177e4SLinus Torvalds EXPORT_SYMBOL(locks_mandatory_area); 11501da177e4SLinus Torvalds 11511da177e4SLinus Torvalds /* We already had a lease on this file; just change its type */ 11521da177e4SLinus Torvalds int lease_modify(struct file_lock **before, int arg) 11531da177e4SLinus Torvalds { 11541da177e4SLinus Torvalds struct file_lock *fl = *before; 11551da177e4SLinus Torvalds int error = assign_type(fl, arg); 11561da177e4SLinus Torvalds 11571da177e4SLinus Torvalds if (error) 11581da177e4SLinus Torvalds return error; 11591da177e4SLinus Torvalds locks_wake_up_blocks(fl); 11601da177e4SLinus Torvalds if (arg == F_UNLCK) 11611da177e4SLinus Torvalds locks_delete_lock(before); 11621da177e4SLinus Torvalds return 0; 11631da177e4SLinus Torvalds } 11641da177e4SLinus Torvalds 11651da177e4SLinus Torvalds EXPORT_SYMBOL(lease_modify); 11661da177e4SLinus Torvalds 11671da177e4SLinus Torvalds static void time_out_leases(struct inode *inode) 11681da177e4SLinus Torvalds { 11691da177e4SLinus Torvalds struct file_lock **before; 11701da177e4SLinus Torvalds struct file_lock *fl; 11711da177e4SLinus Torvalds 11721da177e4SLinus Torvalds before = &inode->i_flock; 11731da177e4SLinus Torvalds while ((fl = *before) && IS_LEASE(fl) && (fl->fl_type & F_INPROGRESS)) { 11741da177e4SLinus Torvalds if ((fl->fl_break_time == 0) 11751da177e4SLinus Torvalds || time_before(jiffies, fl->fl_break_time)) { 11761da177e4SLinus Torvalds before = &fl->fl_next; 11771da177e4SLinus Torvalds continue; 11781da177e4SLinus Torvalds } 11791da177e4SLinus Torvalds lease_modify(before, fl->fl_type & ~F_INPROGRESS); 11801da177e4SLinus Torvalds if (fl == *before) /* lease_modify may have freed fl */ 11811da177e4SLinus Torvalds before = &fl->fl_next; 11821da177e4SLinus Torvalds } 11831da177e4SLinus Torvalds } 11841da177e4SLinus Torvalds 11851da177e4SLinus Torvalds /** 11861da177e4SLinus Torvalds * __break_lease - revoke all outstanding leases on file 11871da177e4SLinus Torvalds * @inode: the inode of the file to return 11881da177e4SLinus Torvalds * @mode: the open mode (read or write) 11891da177e4SLinus Torvalds * 119087250dd2Sdavid m. richter * break_lease (inlined for speed) has checked there already is at least 119187250dd2Sdavid m. richter * some kind of lock (maybe a lease) on this file. Leases are broken on 119287250dd2Sdavid m. richter * a call to open() or truncate(). This function can sleep unless you 11931da177e4SLinus Torvalds * specified %O_NONBLOCK to your open(). 11941da177e4SLinus Torvalds */ 11951da177e4SLinus Torvalds int __break_lease(struct inode *inode, unsigned int mode) 11961da177e4SLinus Torvalds { 11971da177e4SLinus Torvalds int error = 0, future; 11981da177e4SLinus Torvalds struct file_lock *new_fl, *flock; 11991da177e4SLinus Torvalds struct file_lock *fl; 12001da177e4SLinus Torvalds unsigned long break_time; 12011da177e4SLinus Torvalds int i_have_this_lease = 0; 12028737c930SAl Viro int want_write = (mode & O_ACCMODE) != O_RDONLY; 12031da177e4SLinus Torvalds 12048737c930SAl Viro new_fl = lease_alloc(NULL, want_write ? F_WRLCK : F_RDLCK); 12051da177e4SLinus Torvalds 1206b89f4321SArnd Bergmann lock_flocks(); 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 12188737c930SAl Viro if (want_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 1232e32b8ee2SJ. Bruce Fields if (IS_ERR(new_fl) && !i_have_this_lease 1233e32b8ee2SJ. Bruce Fields && ((mode & O_NONBLOCK) == 0)) { 1234e32b8ee2SJ. Bruce Fields error = PTR_ERR(new_fl); 12351da177e4SLinus Torvalds goto out; 12361da177e4SLinus Torvalds } 12371da177e4SLinus Torvalds 12381da177e4SLinus Torvalds break_time = 0; 12391da177e4SLinus Torvalds if (lease_break_time > 0) { 12401da177e4SLinus Torvalds break_time = jiffies + lease_break_time * HZ; 12411da177e4SLinus Torvalds if (break_time == 0) 12421da177e4SLinus Torvalds break_time++; /* so that 0 means no break time */ 12431da177e4SLinus Torvalds } 12441da177e4SLinus Torvalds 12451da177e4SLinus Torvalds for (fl = flock; fl && IS_LEASE(fl); fl = fl->fl_next) { 12461da177e4SLinus Torvalds if (fl->fl_type != future) { 12471da177e4SLinus Torvalds fl->fl_type = future; 12481da177e4SLinus Torvalds fl->fl_break_time = break_time; 12491da177e4SLinus Torvalds /* lease must have lmops break callback */ 12501da177e4SLinus Torvalds fl->fl_lmops->fl_break(fl); 12511da177e4SLinus Torvalds } 12521da177e4SLinus Torvalds } 12531da177e4SLinus Torvalds 12541da177e4SLinus Torvalds if (i_have_this_lease || (mode & O_NONBLOCK)) { 12551da177e4SLinus Torvalds error = -EWOULDBLOCK; 12561da177e4SLinus Torvalds goto out; 12571da177e4SLinus Torvalds } 12581da177e4SLinus Torvalds 12591da177e4SLinus Torvalds restart: 12601da177e4SLinus Torvalds break_time = flock->fl_break_time; 12611da177e4SLinus Torvalds if (break_time != 0) { 12621da177e4SLinus Torvalds break_time -= jiffies; 12631da177e4SLinus Torvalds if (break_time == 0) 12641da177e4SLinus Torvalds break_time++; 12651da177e4SLinus Torvalds } 12664321e01eSMatthew Wilcox locks_insert_block(flock, new_fl); 1267b89f4321SArnd Bergmann unlock_flocks(); 12684321e01eSMatthew Wilcox error = wait_event_interruptible_timeout(new_fl->fl_wait, 12694321e01eSMatthew Wilcox !new_fl->fl_next, break_time); 1270b89f4321SArnd Bergmann lock_flocks(); 12714321e01eSMatthew Wilcox __locks_delete_block(new_fl); 12721da177e4SLinus Torvalds if (error >= 0) { 12731da177e4SLinus Torvalds if (error == 0) 12741da177e4SLinus Torvalds time_out_leases(inode); 12751da177e4SLinus Torvalds /* Wait for the next lease that has not been broken yet */ 12761da177e4SLinus Torvalds for (flock = inode->i_flock; flock && IS_LEASE(flock); 12771da177e4SLinus Torvalds flock = flock->fl_next) { 12781da177e4SLinus Torvalds if (flock->fl_type & F_INPROGRESS) 12791da177e4SLinus Torvalds goto restart; 12801da177e4SLinus Torvalds } 12811da177e4SLinus Torvalds error = 0; 12821da177e4SLinus Torvalds } 12831da177e4SLinus Torvalds 12841da177e4SLinus Torvalds out: 1285b89f4321SArnd Bergmann unlock_flocks(); 1286e32b8ee2SJ. Bruce Fields if (!IS_ERR(new_fl)) 12871da177e4SLinus Torvalds locks_free_lock(new_fl); 12881da177e4SLinus Torvalds return error; 12891da177e4SLinus Torvalds } 12901da177e4SLinus Torvalds 12911da177e4SLinus Torvalds EXPORT_SYMBOL(__break_lease); 12921da177e4SLinus Torvalds 12931da177e4SLinus Torvalds /** 1294a6b91919SRandy Dunlap * lease_get_mtime - get the last modified time of an inode 12951da177e4SLinus Torvalds * @inode: the inode 12961da177e4SLinus Torvalds * @time: pointer to a timespec which will contain the last modified time 12971da177e4SLinus Torvalds * 12981da177e4SLinus Torvalds * This is to force NFS clients to flush their caches for files with 12991da177e4SLinus Torvalds * exclusive leases. The justification is that if someone has an 1300a6b91919SRandy Dunlap * exclusive lease, then they could be modifying it. 13011da177e4SLinus Torvalds */ 13021da177e4SLinus Torvalds void lease_get_mtime(struct inode *inode, struct timespec *time) 13031da177e4SLinus Torvalds { 13041da177e4SLinus Torvalds struct file_lock *flock = inode->i_flock; 13051da177e4SLinus Torvalds if (flock && IS_LEASE(flock) && (flock->fl_type & F_WRLCK)) 13061da177e4SLinus Torvalds *time = current_fs_time(inode->i_sb); 13071da177e4SLinus Torvalds else 13081da177e4SLinus Torvalds *time = inode->i_mtime; 13091da177e4SLinus Torvalds } 13101da177e4SLinus Torvalds 13111da177e4SLinus Torvalds EXPORT_SYMBOL(lease_get_mtime); 13121da177e4SLinus Torvalds 13131da177e4SLinus Torvalds /** 13141da177e4SLinus Torvalds * fcntl_getlease - Enquire what lease is currently active 13151da177e4SLinus Torvalds * @filp: the file 13161da177e4SLinus Torvalds * 13171da177e4SLinus Torvalds * The value returned by this function will be one of 13181da177e4SLinus Torvalds * (if no lease break is pending): 13191da177e4SLinus Torvalds * 13201da177e4SLinus Torvalds * %F_RDLCK to indicate a shared lease is held. 13211da177e4SLinus Torvalds * 13221da177e4SLinus Torvalds * %F_WRLCK to indicate an exclusive lease is held. 13231da177e4SLinus Torvalds * 13241da177e4SLinus Torvalds * %F_UNLCK to indicate no lease is held. 13251da177e4SLinus Torvalds * 13261da177e4SLinus Torvalds * (if a lease break is pending): 13271da177e4SLinus Torvalds * 13281da177e4SLinus Torvalds * %F_RDLCK to indicate an exclusive lease needs to be 13291da177e4SLinus Torvalds * changed to a shared lease (or removed). 13301da177e4SLinus Torvalds * 13311da177e4SLinus Torvalds * %F_UNLCK to indicate the lease needs to be removed. 13321da177e4SLinus Torvalds * 13331da177e4SLinus Torvalds * XXX: sfr & willy disagree over whether F_INPROGRESS 13341da177e4SLinus Torvalds * should be returned to userspace. 13351da177e4SLinus Torvalds */ 13361da177e4SLinus Torvalds int fcntl_getlease(struct file *filp) 13371da177e4SLinus Torvalds { 13381da177e4SLinus Torvalds struct file_lock *fl; 13391da177e4SLinus Torvalds int type = F_UNLCK; 13401da177e4SLinus Torvalds 1341b89f4321SArnd Bergmann lock_flocks(); 13420f7fc9e4SJosef "Jeff" Sipek time_out_leases(filp->f_path.dentry->d_inode); 13430f7fc9e4SJosef "Jeff" Sipek for (fl = filp->f_path.dentry->d_inode->i_flock; fl && IS_LEASE(fl); 13441da177e4SLinus Torvalds fl = fl->fl_next) { 13451da177e4SLinus Torvalds if (fl->fl_file == filp) { 13461da177e4SLinus Torvalds type = fl->fl_type & ~F_INPROGRESS; 13471da177e4SLinus Torvalds break; 13481da177e4SLinus Torvalds } 13491da177e4SLinus Torvalds } 1350b89f4321SArnd Bergmann unlock_flocks(); 13511da177e4SLinus Torvalds return type; 13521da177e4SLinus Torvalds } 13531da177e4SLinus Torvalds 13541da177e4SLinus Torvalds /** 13550af1a450SChristoph Hellwig * generic_setlease - sets a lease on an open file 13561da177e4SLinus Torvalds * @filp: file pointer 13571da177e4SLinus Torvalds * @arg: type of lease to obtain 13581da177e4SLinus Torvalds * @flp: input - file_lock to use, output - file_lock inserted 13591da177e4SLinus Torvalds * 13601da177e4SLinus Torvalds * The (input) flp->fl_lmops->fl_break function is required 13611da177e4SLinus Torvalds * by break_lease(). 13621da177e4SLinus Torvalds * 1363b89f4321SArnd Bergmann * Called with file_lock_lock held. 13641da177e4SLinus Torvalds */ 13650af1a450SChristoph Hellwig int generic_setlease(struct file *filp, long arg, struct file_lock **flp) 13661da177e4SLinus Torvalds { 13677eaae282SKAMBAROV, ZAUR struct file_lock *fl, **before, **my_before = NULL, *lease; 136885c59580SPavel Emelyanov struct file_lock *new_fl = NULL; 13690f7fc9e4SJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 13701da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 13711da177e4SLinus Torvalds int error, rdlease_count = 0, wrlease_count = 0; 13721da177e4SLinus Torvalds 1373da9592edSDavid Howells if ((current_fsuid() != inode->i_uid) && !capable(CAP_LEASE)) 13746d5e8b05SJ. Bruce Fields return -EACCES; 13756d5e8b05SJ. Bruce Fields if (!S_ISREG(inode->i_mode)) 13766d5e8b05SJ. Bruce Fields return -EINVAL; 13776d5e8b05SJ. Bruce Fields error = security_file_lock(filp, arg); 13786d5e8b05SJ. Bruce Fields if (error) 13796d5e8b05SJ. Bruce Fields return error; 13806d5e8b05SJ. Bruce Fields 13811da177e4SLinus Torvalds time_out_leases(inode); 13821da177e4SLinus Torvalds 1383d2ab0b0cSJ. Bruce Fields BUG_ON(!(*flp)->fl_lmops->fl_break); 13841da177e4SLinus Torvalds 13857eaae282SKAMBAROV, ZAUR lease = *flp; 13867eaae282SKAMBAROV, ZAUR 1387288b2fd8SDavid M. Richter if (arg != F_UNLCK) { 138819085557SDavid M. Richter error = -ENOMEM; 138919085557SDavid M. Richter new_fl = locks_alloc_lock(); 139019085557SDavid M. Richter if (new_fl == NULL) 139119085557SDavid M. Richter goto out; 139219085557SDavid M. Richter 13931da177e4SLinus Torvalds error = -EAGAIN; 13941da177e4SLinus Torvalds if ((arg == F_RDLCK) && (atomic_read(&inode->i_writecount) > 0)) 13951da177e4SLinus Torvalds goto out; 13961da177e4SLinus Torvalds if ((arg == F_WRLCK) 13971da177e4SLinus Torvalds && ((atomic_read(&dentry->d_count) > 1) 13981da177e4SLinus Torvalds || (atomic_read(&inode->i_count) > 1))) 13991da177e4SLinus Torvalds goto out; 1400288b2fd8SDavid M. Richter } 140185c59580SPavel Emelyanov 14021da177e4SLinus Torvalds /* 14031da177e4SLinus Torvalds * At this point, we know that if there is an exclusive 14041da177e4SLinus Torvalds * lease on this file, then we hold it on this filp 14051da177e4SLinus Torvalds * (otherwise our open of this file would have blocked). 14061da177e4SLinus Torvalds * And if we are trying to acquire an exclusive lease, 14071da177e4SLinus Torvalds * then the file is not open by anyone (including us) 14081da177e4SLinus Torvalds * except for this filp. 14091da177e4SLinus Torvalds */ 14101da177e4SLinus Torvalds for (before = &inode->i_flock; 14111da177e4SLinus Torvalds ((fl = *before) != NULL) && IS_LEASE(fl); 14121da177e4SLinus Torvalds before = &fl->fl_next) { 14131da177e4SLinus Torvalds if (lease->fl_lmops->fl_mylease(fl, lease)) 14141da177e4SLinus Torvalds my_before = before; 14151da177e4SLinus Torvalds else if (fl->fl_type == (F_INPROGRESS | F_UNLCK)) 14161da177e4SLinus Torvalds /* 14171da177e4SLinus Torvalds * Someone is in the process of opening this 14181da177e4SLinus Torvalds * file for writing so we may not take an 14191da177e4SLinus Torvalds * exclusive lease on it. 14201da177e4SLinus Torvalds */ 14211da177e4SLinus Torvalds wrlease_count++; 14221da177e4SLinus Torvalds else 14231da177e4SLinus Torvalds rdlease_count++; 14241da177e4SLinus Torvalds } 14251da177e4SLinus Torvalds 14265fcc60c3SDavid M. Richter error = -EAGAIN; 14271da177e4SLinus Torvalds if ((arg == F_RDLCK && (wrlease_count > 0)) || 14281da177e4SLinus Torvalds (arg == F_WRLCK && ((rdlease_count + wrlease_count) > 0))) 14291da177e4SLinus Torvalds goto out; 14301da177e4SLinus Torvalds 14311da177e4SLinus Torvalds if (my_before != NULL) { 143275dff55aSTrond Myklebust *flp = *my_before; 14331da177e4SLinus Torvalds error = lease->fl_lmops->fl_change(my_before, arg); 14341da177e4SLinus Torvalds goto out; 14351da177e4SLinus Torvalds } 14361da177e4SLinus Torvalds 14371da177e4SLinus Torvalds error = 0; 14381da177e4SLinus Torvalds if (arg == F_UNLCK) 14391da177e4SLinus Torvalds goto out; 14401da177e4SLinus Torvalds 14411da177e4SLinus Torvalds error = -EINVAL; 14421da177e4SLinus Torvalds if (!leases_enable) 14431da177e4SLinus Torvalds goto out; 14441da177e4SLinus Torvalds 144585c59580SPavel Emelyanov locks_copy_lock(new_fl, lease); 144685c59580SPavel Emelyanov locks_insert_lock(before, new_fl); 14471da177e4SLinus Torvalds 144885c59580SPavel Emelyanov *flp = new_fl; 144985c59580SPavel Emelyanov return 0; 14501da177e4SLinus Torvalds 14511da177e4SLinus Torvalds out: 145285c59580SPavel Emelyanov if (new_fl != NULL) 145385c59580SPavel Emelyanov locks_free_lock(new_fl); 14541da177e4SLinus Torvalds return error; 14551da177e4SLinus Torvalds } 14560af1a450SChristoph Hellwig EXPORT_SYMBOL(generic_setlease); 14571da177e4SLinus Torvalds 1458b89f4321SArnd Bergmann static int __vfs_setlease(struct file *filp, long arg, struct file_lock **lease) 1459b89f4321SArnd Bergmann { 1460b89f4321SArnd Bergmann if (filp->f_op && filp->f_op->setlease) 1461b89f4321SArnd Bergmann return filp->f_op->setlease(filp, arg, lease); 1462b89f4321SArnd Bergmann else 1463b89f4321SArnd Bergmann return generic_setlease(filp, arg, lease); 1464b89f4321SArnd Bergmann } 1465b89f4321SArnd Bergmann 14661da177e4SLinus Torvalds /** 1467a9933ceaSJ. Bruce Fields * vfs_setlease - sets a lease on an open file 14681da177e4SLinus Torvalds * @filp: file pointer 14691da177e4SLinus Torvalds * @arg: type of lease to obtain 14701da177e4SLinus Torvalds * @lease: file_lock to use 14711da177e4SLinus Torvalds * 14721da177e4SLinus Torvalds * Call this to establish a lease on the file. 1473f9ffed26SJ. Bruce Fields * The (*lease)->fl_lmops->fl_break operation must be set; if not, 1474f9ffed26SJ. Bruce Fields * break_lease will oops! 1475f9ffed26SJ. Bruce Fields * 1476f9ffed26SJ. Bruce Fields * This will call the filesystem's setlease file method, if 1477f9ffed26SJ. Bruce Fields * defined. Note that there is no getlease method; instead, the 1478f9ffed26SJ. Bruce Fields * filesystem setlease method should call back to setlease() to 1479f9ffed26SJ. Bruce Fields * add a lease to the inode's lease list, where fcntl_getlease() can 1480f9ffed26SJ. Bruce Fields * find it. Since fcntl_getlease() only reports whether the current 1481f9ffed26SJ. Bruce Fields * task holds a lease, a cluster filesystem need only do this for 1482f9ffed26SJ. Bruce Fields * leases held by processes on this node. 1483f9ffed26SJ. Bruce Fields * 1484f9ffed26SJ. Bruce Fields * There is also no break_lease method; filesystems that 1485c9404c9cSAdam Buchbinder * handle their own leases should break leases themselves from the 1486f9ffed26SJ. Bruce Fields * filesystem's open, create, and (on truncate) setattr methods. 1487f9ffed26SJ. Bruce Fields * 1488f9ffed26SJ. Bruce Fields * Warning: the only current setlease methods exist only to disable 1489f9ffed26SJ. Bruce Fields * leases in certain cases. More vfs changes may be required to 1490f9ffed26SJ. Bruce Fields * allow a full filesystem lease implementation. 14911da177e4SLinus Torvalds */ 14921da177e4SLinus Torvalds 1493a9933ceaSJ. Bruce Fields int vfs_setlease(struct file *filp, long arg, struct file_lock **lease) 14941da177e4SLinus Torvalds { 14951da177e4SLinus Torvalds int error; 14961da177e4SLinus Torvalds 1497b89f4321SArnd Bergmann lock_flocks(); 1498b89f4321SArnd Bergmann error = __vfs_setlease(filp, arg, lease); 1499b89f4321SArnd Bergmann unlock_flocks(); 15001da177e4SLinus Torvalds 15011da177e4SLinus Torvalds return error; 15021da177e4SLinus Torvalds } 1503a9933ceaSJ. Bruce Fields EXPORT_SYMBOL_GPL(vfs_setlease); 15041da177e4SLinus Torvalds 15051da177e4SLinus Torvalds /** 15061da177e4SLinus Torvalds * fcntl_setlease - sets a lease on an open file 15071da177e4SLinus Torvalds * @fd: open file descriptor 15081da177e4SLinus Torvalds * @filp: file pointer 15091da177e4SLinus Torvalds * @arg: type of lease to obtain 15101da177e4SLinus Torvalds * 15111da177e4SLinus Torvalds * Call this fcntl to establish a lease on the file. 15121da177e4SLinus Torvalds * Note that you also need to call %F_SETSIG to 15131da177e4SLinus Torvalds * receive a signal when the lease is broken. 15141da177e4SLinus Torvalds */ 15151da177e4SLinus Torvalds int fcntl_setlease(unsigned int fd, struct file *filp, long arg) 15161da177e4SLinus Torvalds { 15171da177e4SLinus Torvalds struct file_lock fl, *flp = &fl; 15189d91cdccSDavid M. Richter struct inode *inode = filp->f_path.dentry->d_inode; 15191da177e4SLinus Torvalds int error; 15201da177e4SLinus Torvalds 15211da177e4SLinus Torvalds locks_init_lock(&fl); 15221da177e4SLinus Torvalds error = lease_init(filp, arg, &fl); 15231da177e4SLinus Torvalds if (error) 15241da177e4SLinus Torvalds return error; 15251da177e4SLinus Torvalds 1526b89f4321SArnd Bergmann lock_flocks(); 15271da177e4SLinus Torvalds 1528b89f4321SArnd Bergmann error = __vfs_setlease(filp, arg, &flp); 1529dc15ae14SJ. Bruce Fields if (error || arg == F_UNLCK) 15301da177e4SLinus Torvalds goto out_unlock; 15311da177e4SLinus Torvalds 15321da177e4SLinus Torvalds error = fasync_helper(fd, filp, 1, &flp->fl_fasync); 15331da177e4SLinus Torvalds if (error < 0) { 15346d5e8b05SJ. Bruce Fields /* remove lease just inserted by setlease */ 15351da177e4SLinus Torvalds flp->fl_type = F_UNLCK | F_INPROGRESS; 15361da177e4SLinus Torvalds flp->fl_break_time = jiffies - 10; 15371da177e4SLinus Torvalds time_out_leases(inode); 15381da177e4SLinus Torvalds goto out_unlock; 15391da177e4SLinus Torvalds } 15401da177e4SLinus Torvalds 1541609d7fa9SEric W. Biederman error = __f_setown(filp, task_pid(current), PIDTYPE_PID, 0); 15421da177e4SLinus Torvalds out_unlock: 1543b89f4321SArnd Bergmann unlock_flocks(); 15441da177e4SLinus Torvalds return error; 15451da177e4SLinus Torvalds } 15461da177e4SLinus Torvalds 15471da177e4SLinus Torvalds /** 15481da177e4SLinus Torvalds * flock_lock_file_wait - Apply a FLOCK-style lock to a file 15491da177e4SLinus Torvalds * @filp: The file to apply the lock to 15501da177e4SLinus Torvalds * @fl: The lock to be applied 15511da177e4SLinus Torvalds * 15521da177e4SLinus Torvalds * Add a FLOCK style lock to a file. 15531da177e4SLinus Torvalds */ 15541da177e4SLinus Torvalds int flock_lock_file_wait(struct file *filp, struct file_lock *fl) 15551da177e4SLinus Torvalds { 15561da177e4SLinus Torvalds int error; 15571da177e4SLinus Torvalds might_sleep(); 15581da177e4SLinus Torvalds for (;;) { 15591da177e4SLinus Torvalds error = flock_lock_file(filp, fl); 1560bde74e4bSMiklos Szeredi if (error != FILE_LOCK_DEFERRED) 15611da177e4SLinus Torvalds break; 15621da177e4SLinus Torvalds error = wait_event_interruptible(fl->fl_wait, !fl->fl_next); 15631da177e4SLinus Torvalds if (!error) 15641da177e4SLinus Torvalds continue; 15651da177e4SLinus Torvalds 15661da177e4SLinus Torvalds locks_delete_block(fl); 15671da177e4SLinus Torvalds break; 15681da177e4SLinus Torvalds } 15691da177e4SLinus Torvalds return error; 15701da177e4SLinus Torvalds } 15711da177e4SLinus Torvalds 15721da177e4SLinus Torvalds EXPORT_SYMBOL(flock_lock_file_wait); 15731da177e4SLinus Torvalds 15741da177e4SLinus Torvalds /** 15751da177e4SLinus Torvalds * sys_flock: - flock() system call. 15761da177e4SLinus Torvalds * @fd: the file descriptor to lock. 15771da177e4SLinus Torvalds * @cmd: the type of lock to apply. 15781da177e4SLinus Torvalds * 15791da177e4SLinus Torvalds * Apply a %FL_FLOCK style lock to an open file descriptor. 15801da177e4SLinus Torvalds * The @cmd can be one of 15811da177e4SLinus Torvalds * 15821da177e4SLinus Torvalds * %LOCK_SH -- a shared lock. 15831da177e4SLinus Torvalds * 15841da177e4SLinus Torvalds * %LOCK_EX -- an exclusive lock. 15851da177e4SLinus Torvalds * 15861da177e4SLinus Torvalds * %LOCK_UN -- remove an existing lock. 15871da177e4SLinus Torvalds * 15881da177e4SLinus Torvalds * %LOCK_MAND -- a `mandatory' flock. This exists to emulate Windows Share Modes. 15891da177e4SLinus Torvalds * 15901da177e4SLinus Torvalds * %LOCK_MAND can be combined with %LOCK_READ or %LOCK_WRITE to allow other 15911da177e4SLinus Torvalds * processes read and write access respectively. 15921da177e4SLinus Torvalds */ 1593002c8976SHeiko Carstens SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd) 15941da177e4SLinus Torvalds { 15951da177e4SLinus Torvalds struct file *filp; 15961da177e4SLinus Torvalds struct file_lock *lock; 15971da177e4SLinus Torvalds int can_sleep, unlock; 15981da177e4SLinus Torvalds int error; 15991da177e4SLinus Torvalds 16001da177e4SLinus Torvalds error = -EBADF; 16011da177e4SLinus Torvalds filp = fget(fd); 16021da177e4SLinus Torvalds if (!filp) 16031da177e4SLinus Torvalds goto out; 16041da177e4SLinus Torvalds 16051da177e4SLinus Torvalds can_sleep = !(cmd & LOCK_NB); 16061da177e4SLinus Torvalds cmd &= ~LOCK_NB; 16071da177e4SLinus Torvalds unlock = (cmd == LOCK_UN); 16081da177e4SLinus Torvalds 1609aeb5d727SAl Viro if (!unlock && !(cmd & LOCK_MAND) && 1610aeb5d727SAl Viro !(filp->f_mode & (FMODE_READ|FMODE_WRITE))) 16111da177e4SLinus Torvalds goto out_putf; 16121da177e4SLinus Torvalds 16131da177e4SLinus Torvalds error = flock_make_lock(filp, &lock, cmd); 16141da177e4SLinus Torvalds if (error) 16151da177e4SLinus Torvalds goto out_putf; 16161da177e4SLinus Torvalds if (can_sleep) 16171da177e4SLinus Torvalds lock->fl_flags |= FL_SLEEP; 16181da177e4SLinus Torvalds 1619713c0ecdSSten Spans error = security_file_lock(filp, lock->fl_type); 16201da177e4SLinus Torvalds if (error) 16211da177e4SLinus Torvalds goto out_free; 16221da177e4SLinus Torvalds 16231da177e4SLinus Torvalds if (filp->f_op && filp->f_op->flock) 16241da177e4SLinus Torvalds error = filp->f_op->flock(filp, 16251da177e4SLinus Torvalds (can_sleep) ? F_SETLKW : F_SETLK, 16261da177e4SLinus Torvalds lock); 16271da177e4SLinus Torvalds else 16281da177e4SLinus Torvalds error = flock_lock_file_wait(filp, lock); 16291da177e4SLinus Torvalds 16301da177e4SLinus Torvalds out_free: 16311da177e4SLinus Torvalds locks_free_lock(lock); 16321da177e4SLinus Torvalds 16331da177e4SLinus Torvalds out_putf: 16341da177e4SLinus Torvalds fput(filp); 16351da177e4SLinus Torvalds out: 16361da177e4SLinus Torvalds return error; 16371da177e4SLinus Torvalds } 16381da177e4SLinus Torvalds 16393ee17abdSJ. Bruce Fields /** 16403ee17abdSJ. Bruce Fields * vfs_test_lock - test file byte range lock 16413ee17abdSJ. Bruce Fields * @filp: The file to test lock for 16426924c554SJ. Bruce Fields * @fl: The lock to test; also used to hold result 16433ee17abdSJ. Bruce Fields * 16443ee17abdSJ. Bruce Fields * Returns -ERRNO on failure. Indicates presence of conflicting lock by 16453ee17abdSJ. Bruce Fields * setting conf->fl_type to something other than F_UNLCK. 16463ee17abdSJ. Bruce Fields */ 16473ee17abdSJ. Bruce Fields int vfs_test_lock(struct file *filp, struct file_lock *fl) 16483ee17abdSJ. Bruce Fields { 16493ee17abdSJ. Bruce Fields if (filp->f_op && filp->f_op->lock) 16503ee17abdSJ. Bruce Fields return filp->f_op->lock(filp, F_GETLK, fl); 16513ee17abdSJ. Bruce Fields posix_test_lock(filp, fl); 16523ee17abdSJ. Bruce Fields return 0; 16533ee17abdSJ. Bruce Fields } 16543ee17abdSJ. Bruce Fields EXPORT_SYMBOL_GPL(vfs_test_lock); 16553ee17abdSJ. Bruce Fields 1656c2fa1b8aSJ. Bruce Fields static int posix_lock_to_flock(struct flock *flock, struct file_lock *fl) 1657c2fa1b8aSJ. Bruce Fields { 1658c2fa1b8aSJ. Bruce Fields flock->l_pid = fl->fl_pid; 1659c2fa1b8aSJ. Bruce Fields #if BITS_PER_LONG == 32 1660c2fa1b8aSJ. Bruce Fields /* 1661c2fa1b8aSJ. Bruce Fields * Make sure we can represent the posix lock via 1662c2fa1b8aSJ. Bruce Fields * legacy 32bit flock. 1663c2fa1b8aSJ. Bruce Fields */ 1664c2fa1b8aSJ. Bruce Fields if (fl->fl_start > OFFT_OFFSET_MAX) 1665c2fa1b8aSJ. Bruce Fields return -EOVERFLOW; 1666c2fa1b8aSJ. Bruce Fields if (fl->fl_end != OFFSET_MAX && fl->fl_end > OFFT_OFFSET_MAX) 1667c2fa1b8aSJ. Bruce Fields return -EOVERFLOW; 1668c2fa1b8aSJ. Bruce Fields #endif 1669c2fa1b8aSJ. Bruce Fields flock->l_start = fl->fl_start; 1670c2fa1b8aSJ. Bruce Fields flock->l_len = fl->fl_end == OFFSET_MAX ? 0 : 1671c2fa1b8aSJ. Bruce Fields fl->fl_end - fl->fl_start + 1; 1672c2fa1b8aSJ. Bruce Fields flock->l_whence = 0; 1673129a84deSJ. Bruce Fields flock->l_type = fl->fl_type; 1674c2fa1b8aSJ. Bruce Fields return 0; 1675c2fa1b8aSJ. Bruce Fields } 1676c2fa1b8aSJ. Bruce Fields 1677c2fa1b8aSJ. Bruce Fields #if BITS_PER_LONG == 32 1678c2fa1b8aSJ. Bruce Fields static void posix_lock_to_flock64(struct flock64 *flock, struct file_lock *fl) 1679c2fa1b8aSJ. Bruce Fields { 1680c2fa1b8aSJ. Bruce Fields flock->l_pid = fl->fl_pid; 1681c2fa1b8aSJ. Bruce Fields flock->l_start = fl->fl_start; 1682c2fa1b8aSJ. Bruce Fields flock->l_len = fl->fl_end == OFFSET_MAX ? 0 : 1683c2fa1b8aSJ. Bruce Fields fl->fl_end - fl->fl_start + 1; 1684c2fa1b8aSJ. Bruce Fields flock->l_whence = 0; 1685c2fa1b8aSJ. Bruce Fields flock->l_type = fl->fl_type; 1686c2fa1b8aSJ. Bruce Fields } 1687c2fa1b8aSJ. Bruce Fields #endif 1688c2fa1b8aSJ. Bruce Fields 16891da177e4SLinus Torvalds /* Report the first existing lock that would conflict with l. 16901da177e4SLinus Torvalds * This implements the F_GETLK command of fcntl(). 16911da177e4SLinus Torvalds */ 16921da177e4SLinus Torvalds int fcntl_getlk(struct file *filp, struct flock __user *l) 16931da177e4SLinus Torvalds { 16949d6a8c5cSMarc Eshel struct file_lock file_lock; 16951da177e4SLinus Torvalds struct flock flock; 16961da177e4SLinus Torvalds int error; 16971da177e4SLinus Torvalds 16981da177e4SLinus Torvalds error = -EFAULT; 16991da177e4SLinus Torvalds if (copy_from_user(&flock, l, sizeof(flock))) 17001da177e4SLinus Torvalds goto out; 17011da177e4SLinus Torvalds error = -EINVAL; 17021da177e4SLinus Torvalds if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK)) 17031da177e4SLinus Torvalds goto out; 17041da177e4SLinus Torvalds 17051da177e4SLinus Torvalds error = flock_to_posix_lock(filp, &file_lock, &flock); 17061da177e4SLinus Torvalds if (error) 17071da177e4SLinus Torvalds goto out; 17081da177e4SLinus Torvalds 17093ee17abdSJ. Bruce Fields error = vfs_test_lock(filp, &file_lock); 17103ee17abdSJ. Bruce Fields if (error) 17111da177e4SLinus Torvalds goto out; 17121da177e4SLinus Torvalds 17139d6a8c5cSMarc Eshel flock.l_type = file_lock.fl_type; 17149d6a8c5cSMarc Eshel if (file_lock.fl_type != F_UNLCK) { 17159d6a8c5cSMarc Eshel error = posix_lock_to_flock(&flock, &file_lock); 1716c2fa1b8aSJ. Bruce Fields if (error) 17171da177e4SLinus Torvalds goto out; 17181da177e4SLinus Torvalds } 17191da177e4SLinus Torvalds error = -EFAULT; 17201da177e4SLinus Torvalds if (!copy_to_user(l, &flock, sizeof(flock))) 17211da177e4SLinus Torvalds error = 0; 17221da177e4SLinus Torvalds out: 17231da177e4SLinus Torvalds return error; 17241da177e4SLinus Torvalds } 17251da177e4SLinus Torvalds 17267723ec97SMarc Eshel /** 17277723ec97SMarc Eshel * vfs_lock_file - file byte range lock 17287723ec97SMarc Eshel * @filp: The file to apply the lock to 17297723ec97SMarc Eshel * @cmd: type of locking operation (F_SETLK, F_GETLK, etc.) 17307723ec97SMarc Eshel * @fl: The lock to be applied 1731150b3934SMarc Eshel * @conf: Place to return a copy of the conflicting lock, if found. 1732150b3934SMarc Eshel * 1733150b3934SMarc Eshel * A caller that doesn't care about the conflicting lock may pass NULL 1734150b3934SMarc Eshel * as the final argument. 1735150b3934SMarc Eshel * 1736150b3934SMarc Eshel * If the filesystem defines a private ->lock() method, then @conf will 1737150b3934SMarc Eshel * be left unchanged; so a caller that cares should initialize it to 1738150b3934SMarc Eshel * some acceptable default. 17392beb6614SMarc Eshel * 17402beb6614SMarc Eshel * To avoid blocking kernel daemons, such as lockd, that need to acquire POSIX 17412beb6614SMarc Eshel * locks, the ->lock() interface may return asynchronously, before the lock has 17422beb6614SMarc Eshel * been granted or denied by the underlying filesystem, if (and only if) 17432beb6614SMarc Eshel * fl_grant is set. Callers expecting ->lock() to return asynchronously 17442beb6614SMarc Eshel * will only use F_SETLK, not F_SETLKW; they will set FL_SLEEP if (and only if) 17452beb6614SMarc Eshel * the request is for a blocking lock. When ->lock() does return asynchronously, 1746bde74e4bSMiklos Szeredi * it must return FILE_LOCK_DEFERRED, and call ->fl_grant() when the lock 17472beb6614SMarc Eshel * request completes. 17482beb6614SMarc Eshel * If the request is for non-blocking lock the file system should return 1749bde74e4bSMiklos Szeredi * FILE_LOCK_DEFERRED then try to get the lock and call the callback routine 1750bde74e4bSMiklos Szeredi * with the result. If the request timed out the callback routine will return a 17512beb6614SMarc Eshel * nonzero return code and the file system should release the lock. The file 17522beb6614SMarc Eshel * system is also responsible to keep a corresponding posix lock when it 17532beb6614SMarc Eshel * grants a lock so the VFS can find out which locks are locally held and do 17542beb6614SMarc Eshel * the correct lock cleanup when required. 17552beb6614SMarc Eshel * The underlying filesystem must not drop the kernel lock or call 1756bde74e4bSMiklos Szeredi * ->fl_grant() before returning to the caller with a FILE_LOCK_DEFERRED 17572beb6614SMarc Eshel * return code. 17587723ec97SMarc Eshel */ 1759150b3934SMarc Eshel int vfs_lock_file(struct file *filp, unsigned int cmd, struct file_lock *fl, struct file_lock *conf) 17607723ec97SMarc Eshel { 17617723ec97SMarc Eshel if (filp->f_op && filp->f_op->lock) 17627723ec97SMarc Eshel return filp->f_op->lock(filp, cmd, fl); 17637723ec97SMarc Eshel else 1764150b3934SMarc Eshel return posix_lock_file(filp, fl, conf); 17657723ec97SMarc Eshel } 17667723ec97SMarc Eshel EXPORT_SYMBOL_GPL(vfs_lock_file); 17677723ec97SMarc Eshel 1768b648a6deSMiklos Szeredi static int do_lock_file_wait(struct file *filp, unsigned int cmd, 1769b648a6deSMiklos Szeredi struct file_lock *fl) 1770b648a6deSMiklos Szeredi { 1771b648a6deSMiklos Szeredi int error; 1772b648a6deSMiklos Szeredi 1773b648a6deSMiklos Szeredi error = security_file_lock(filp, fl->fl_type); 1774b648a6deSMiklos Szeredi if (error) 1775b648a6deSMiklos Szeredi return error; 1776b648a6deSMiklos Szeredi 1777b648a6deSMiklos Szeredi for (;;) { 1778764c76b3SMiklos Szeredi error = vfs_lock_file(filp, cmd, fl, NULL); 1779b648a6deSMiklos Szeredi if (error != FILE_LOCK_DEFERRED) 1780b648a6deSMiklos Szeredi break; 1781764c76b3SMiklos Szeredi error = wait_event_interruptible(fl->fl_wait, !fl->fl_next); 1782b648a6deSMiklos Szeredi if (!error) 1783b648a6deSMiklos Szeredi continue; 1784b648a6deSMiklos Szeredi 1785b648a6deSMiklos Szeredi locks_delete_block(fl); 1786b648a6deSMiklos Szeredi break; 1787b648a6deSMiklos Szeredi } 1788b648a6deSMiklos Szeredi 1789b648a6deSMiklos Szeredi return error; 1790b648a6deSMiklos Szeredi } 1791b648a6deSMiklos Szeredi 17921da177e4SLinus Torvalds /* Apply the lock described by l to an open file descriptor. 17931da177e4SLinus Torvalds * This implements both the F_SETLK and F_SETLKW commands of fcntl(). 17941da177e4SLinus Torvalds */ 1795c293621bSPeter Staubach int fcntl_setlk(unsigned int fd, struct file *filp, unsigned int cmd, 1796c293621bSPeter Staubach struct flock __user *l) 17971da177e4SLinus Torvalds { 17981da177e4SLinus Torvalds struct file_lock *file_lock = locks_alloc_lock(); 17991da177e4SLinus Torvalds struct flock flock; 18001da177e4SLinus Torvalds struct inode *inode; 18010b2bac2fSAl Viro struct file *f; 18021da177e4SLinus Torvalds int error; 18031da177e4SLinus Torvalds 18041da177e4SLinus Torvalds if (file_lock == NULL) 18051da177e4SLinus Torvalds return -ENOLCK; 18061da177e4SLinus Torvalds 18071da177e4SLinus Torvalds /* 18081da177e4SLinus Torvalds * This might block, so we do it before checking the inode. 18091da177e4SLinus Torvalds */ 18101da177e4SLinus Torvalds error = -EFAULT; 18111da177e4SLinus Torvalds if (copy_from_user(&flock, l, sizeof(flock))) 18121da177e4SLinus Torvalds goto out; 18131da177e4SLinus Torvalds 18140f7fc9e4SJosef "Jeff" Sipek inode = filp->f_path.dentry->d_inode; 18151da177e4SLinus Torvalds 18161da177e4SLinus Torvalds /* Don't allow mandatory locks on files that may be memory mapped 18171da177e4SLinus Torvalds * and shared. 18181da177e4SLinus Torvalds */ 1819a16877caSPavel Emelyanov if (mandatory_lock(inode) && mapping_writably_mapped(filp->f_mapping)) { 18201da177e4SLinus Torvalds error = -EAGAIN; 18211da177e4SLinus Torvalds goto out; 18221da177e4SLinus Torvalds } 18231da177e4SLinus Torvalds 1824c293621bSPeter Staubach again: 18251da177e4SLinus Torvalds error = flock_to_posix_lock(filp, file_lock, &flock); 18261da177e4SLinus Torvalds if (error) 18271da177e4SLinus Torvalds goto out; 18281da177e4SLinus Torvalds if (cmd == F_SETLKW) { 18291da177e4SLinus Torvalds file_lock->fl_flags |= FL_SLEEP; 18301da177e4SLinus Torvalds } 18311da177e4SLinus Torvalds 18321da177e4SLinus Torvalds error = -EBADF; 18331da177e4SLinus Torvalds switch (flock.l_type) { 18341da177e4SLinus Torvalds case F_RDLCK: 18351da177e4SLinus Torvalds if (!(filp->f_mode & FMODE_READ)) 18361da177e4SLinus Torvalds goto out; 18371da177e4SLinus Torvalds break; 18381da177e4SLinus Torvalds case F_WRLCK: 18391da177e4SLinus Torvalds if (!(filp->f_mode & FMODE_WRITE)) 18401da177e4SLinus Torvalds goto out; 18411da177e4SLinus Torvalds break; 18421da177e4SLinus Torvalds case F_UNLCK: 18431da177e4SLinus Torvalds break; 18441da177e4SLinus Torvalds default: 18451da177e4SLinus Torvalds error = -EINVAL; 18461da177e4SLinus Torvalds goto out; 18471da177e4SLinus Torvalds } 18481da177e4SLinus Torvalds 1849b648a6deSMiklos Szeredi error = do_lock_file_wait(filp, cmd, file_lock); 1850c293621bSPeter Staubach 1851c293621bSPeter Staubach /* 1852c293621bSPeter Staubach * Attempt to detect a close/fcntl race and recover by 1853c293621bSPeter Staubach * releasing the lock that was just acquired. 1854c293621bSPeter Staubach */ 18550b2bac2fSAl Viro /* 18560b2bac2fSAl Viro * we need that spin_lock here - it prevents reordering between 18570b2bac2fSAl Viro * update of inode->i_flock and check for it done in close(). 18580b2bac2fSAl Viro * rcu_read_lock() wouldn't do. 18590b2bac2fSAl Viro */ 18600b2bac2fSAl Viro spin_lock(¤t->files->file_lock); 18610b2bac2fSAl Viro f = fcheck(fd); 18620b2bac2fSAl Viro spin_unlock(¤t->files->file_lock); 18630b2bac2fSAl Viro if (!error && f != filp && flock.l_type != F_UNLCK) { 1864c293621bSPeter Staubach flock.l_type = F_UNLCK; 1865c293621bSPeter Staubach goto again; 1866c293621bSPeter Staubach } 18671da177e4SLinus Torvalds 18681da177e4SLinus Torvalds out: 18691da177e4SLinus Torvalds locks_free_lock(file_lock); 18701da177e4SLinus Torvalds return error; 18711da177e4SLinus Torvalds } 18721da177e4SLinus Torvalds 18731da177e4SLinus Torvalds #if BITS_PER_LONG == 32 18741da177e4SLinus Torvalds /* Report the first existing lock that would conflict with l. 18751da177e4SLinus Torvalds * This implements the F_GETLK command of fcntl(). 18761da177e4SLinus Torvalds */ 18771da177e4SLinus Torvalds int fcntl_getlk64(struct file *filp, struct flock64 __user *l) 18781da177e4SLinus Torvalds { 18799d6a8c5cSMarc Eshel struct file_lock file_lock; 18801da177e4SLinus Torvalds struct flock64 flock; 18811da177e4SLinus Torvalds int error; 18821da177e4SLinus Torvalds 18831da177e4SLinus Torvalds error = -EFAULT; 18841da177e4SLinus Torvalds if (copy_from_user(&flock, l, sizeof(flock))) 18851da177e4SLinus Torvalds goto out; 18861da177e4SLinus Torvalds error = -EINVAL; 18871da177e4SLinus Torvalds if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK)) 18881da177e4SLinus Torvalds goto out; 18891da177e4SLinus Torvalds 18901da177e4SLinus Torvalds error = flock64_to_posix_lock(filp, &file_lock, &flock); 18911da177e4SLinus Torvalds if (error) 18921da177e4SLinus Torvalds goto out; 18931da177e4SLinus Torvalds 18943ee17abdSJ. Bruce Fields error = vfs_test_lock(filp, &file_lock); 18953ee17abdSJ. Bruce Fields if (error) 18961da177e4SLinus Torvalds goto out; 18971da177e4SLinus Torvalds 18989d6a8c5cSMarc Eshel flock.l_type = file_lock.fl_type; 18999d6a8c5cSMarc Eshel if (file_lock.fl_type != F_UNLCK) 19009d6a8c5cSMarc Eshel posix_lock_to_flock64(&flock, &file_lock); 19019d6a8c5cSMarc Eshel 19021da177e4SLinus Torvalds error = -EFAULT; 19031da177e4SLinus Torvalds if (!copy_to_user(l, &flock, sizeof(flock))) 19041da177e4SLinus Torvalds error = 0; 19051da177e4SLinus Torvalds 19061da177e4SLinus Torvalds out: 19071da177e4SLinus Torvalds return error; 19081da177e4SLinus Torvalds } 19091da177e4SLinus Torvalds 19101da177e4SLinus Torvalds /* Apply the lock described by l to an open file descriptor. 19111da177e4SLinus Torvalds * This implements both the F_SETLK and F_SETLKW commands of fcntl(). 19121da177e4SLinus Torvalds */ 1913c293621bSPeter Staubach int fcntl_setlk64(unsigned int fd, struct file *filp, unsigned int cmd, 1914c293621bSPeter Staubach struct flock64 __user *l) 19151da177e4SLinus Torvalds { 19161da177e4SLinus Torvalds struct file_lock *file_lock = locks_alloc_lock(); 19171da177e4SLinus Torvalds struct flock64 flock; 19181da177e4SLinus Torvalds struct inode *inode; 19190b2bac2fSAl Viro struct file *f; 19201da177e4SLinus Torvalds int error; 19211da177e4SLinus Torvalds 19221da177e4SLinus Torvalds if (file_lock == NULL) 19231da177e4SLinus Torvalds return -ENOLCK; 19241da177e4SLinus Torvalds 19251da177e4SLinus Torvalds /* 19261da177e4SLinus Torvalds * This might block, so we do it before checking the inode. 19271da177e4SLinus Torvalds */ 19281da177e4SLinus Torvalds error = -EFAULT; 19291da177e4SLinus Torvalds if (copy_from_user(&flock, l, sizeof(flock))) 19301da177e4SLinus Torvalds goto out; 19311da177e4SLinus Torvalds 19320f7fc9e4SJosef "Jeff" Sipek inode = filp->f_path.dentry->d_inode; 19331da177e4SLinus Torvalds 19341da177e4SLinus Torvalds /* Don't allow mandatory locks on files that may be memory mapped 19351da177e4SLinus Torvalds * and shared. 19361da177e4SLinus Torvalds */ 1937a16877caSPavel Emelyanov if (mandatory_lock(inode) && mapping_writably_mapped(filp->f_mapping)) { 19381da177e4SLinus Torvalds error = -EAGAIN; 19391da177e4SLinus Torvalds goto out; 19401da177e4SLinus Torvalds } 19411da177e4SLinus Torvalds 1942c293621bSPeter Staubach again: 19431da177e4SLinus Torvalds error = flock64_to_posix_lock(filp, file_lock, &flock); 19441da177e4SLinus Torvalds if (error) 19451da177e4SLinus Torvalds goto out; 19461da177e4SLinus Torvalds if (cmd == F_SETLKW64) { 19471da177e4SLinus Torvalds file_lock->fl_flags |= FL_SLEEP; 19481da177e4SLinus Torvalds } 19491da177e4SLinus Torvalds 19501da177e4SLinus Torvalds error = -EBADF; 19511da177e4SLinus Torvalds switch (flock.l_type) { 19521da177e4SLinus Torvalds case F_RDLCK: 19531da177e4SLinus Torvalds if (!(filp->f_mode & FMODE_READ)) 19541da177e4SLinus Torvalds goto out; 19551da177e4SLinus Torvalds break; 19561da177e4SLinus Torvalds case F_WRLCK: 19571da177e4SLinus Torvalds if (!(filp->f_mode & FMODE_WRITE)) 19581da177e4SLinus Torvalds goto out; 19591da177e4SLinus Torvalds break; 19601da177e4SLinus Torvalds case F_UNLCK: 19611da177e4SLinus Torvalds break; 19621da177e4SLinus Torvalds default: 19631da177e4SLinus Torvalds error = -EINVAL; 19641da177e4SLinus Torvalds goto out; 19651da177e4SLinus Torvalds } 19661da177e4SLinus Torvalds 1967b648a6deSMiklos Szeredi error = do_lock_file_wait(filp, cmd, file_lock); 1968c293621bSPeter Staubach 1969c293621bSPeter Staubach /* 1970c293621bSPeter Staubach * Attempt to detect a close/fcntl race and recover by 1971c293621bSPeter Staubach * releasing the lock that was just acquired. 1972c293621bSPeter Staubach */ 19730b2bac2fSAl Viro spin_lock(¤t->files->file_lock); 19740b2bac2fSAl Viro f = fcheck(fd); 19750b2bac2fSAl Viro spin_unlock(¤t->files->file_lock); 19760b2bac2fSAl Viro if (!error && f != filp && flock.l_type != F_UNLCK) { 1977c293621bSPeter Staubach flock.l_type = F_UNLCK; 1978c293621bSPeter Staubach goto again; 1979c293621bSPeter Staubach } 19801da177e4SLinus Torvalds 19811da177e4SLinus Torvalds out: 19821da177e4SLinus Torvalds locks_free_lock(file_lock); 19831da177e4SLinus Torvalds return error; 19841da177e4SLinus Torvalds } 19851da177e4SLinus Torvalds #endif /* BITS_PER_LONG == 32 */ 19861da177e4SLinus Torvalds 19871da177e4SLinus Torvalds /* 19881da177e4SLinus Torvalds * This function is called when the file is being removed 19891da177e4SLinus Torvalds * from the task's fd array. POSIX locks belonging to this task 19901da177e4SLinus Torvalds * are deleted at this time. 19911da177e4SLinus Torvalds */ 19921da177e4SLinus Torvalds void locks_remove_posix(struct file *filp, fl_owner_t owner) 19931da177e4SLinus Torvalds { 1994ff7b86b8SMiklos Szeredi struct file_lock lock; 19951da177e4SLinus Torvalds 19961da177e4SLinus Torvalds /* 19971da177e4SLinus Torvalds * If there are no locks held on this file, we don't need to call 19981da177e4SLinus Torvalds * posix_lock_file(). Another process could be setting a lock on this 19991da177e4SLinus Torvalds * file at the same time, but we wouldn't remove that lock anyway. 20001da177e4SLinus Torvalds */ 20010f7fc9e4SJosef "Jeff" Sipek if (!filp->f_path.dentry->d_inode->i_flock) 20021da177e4SLinus Torvalds return; 20031da177e4SLinus Torvalds 20041da177e4SLinus Torvalds lock.fl_type = F_UNLCK; 200575e1fcc0SMiklos Szeredi lock.fl_flags = FL_POSIX | FL_CLOSE; 20061da177e4SLinus Torvalds lock.fl_start = 0; 20071da177e4SLinus Torvalds lock.fl_end = OFFSET_MAX; 20081da177e4SLinus Torvalds lock.fl_owner = owner; 20091da177e4SLinus Torvalds lock.fl_pid = current->tgid; 20101da177e4SLinus Torvalds lock.fl_file = filp; 20111da177e4SLinus Torvalds lock.fl_ops = NULL; 20121da177e4SLinus Torvalds lock.fl_lmops = NULL; 20131da177e4SLinus Torvalds 2014150b3934SMarc Eshel vfs_lock_file(filp, F_SETLK, &lock, NULL); 20151da177e4SLinus Torvalds 20161da177e4SLinus Torvalds if (lock.fl_ops && lock.fl_ops->fl_release_private) 20171da177e4SLinus Torvalds lock.fl_ops->fl_release_private(&lock); 20181da177e4SLinus Torvalds } 20191da177e4SLinus Torvalds 20201da177e4SLinus Torvalds EXPORT_SYMBOL(locks_remove_posix); 20211da177e4SLinus Torvalds 20221da177e4SLinus Torvalds /* 20231da177e4SLinus Torvalds * This function is called on the last close of an open file. 20241da177e4SLinus Torvalds */ 20251da177e4SLinus Torvalds void locks_remove_flock(struct file *filp) 20261da177e4SLinus Torvalds { 20270f7fc9e4SJosef "Jeff" Sipek struct inode * inode = filp->f_path.dentry->d_inode; 20281da177e4SLinus Torvalds struct file_lock *fl; 20291da177e4SLinus Torvalds struct file_lock **before; 20301da177e4SLinus Torvalds 20311da177e4SLinus Torvalds if (!inode->i_flock) 20321da177e4SLinus Torvalds return; 20331da177e4SLinus Torvalds 20341da177e4SLinus Torvalds if (filp->f_op && filp->f_op->flock) { 20351da177e4SLinus Torvalds struct file_lock fl = { 20361da177e4SLinus Torvalds .fl_pid = current->tgid, 20371da177e4SLinus Torvalds .fl_file = filp, 20381da177e4SLinus Torvalds .fl_flags = FL_FLOCK, 20391da177e4SLinus Torvalds .fl_type = F_UNLCK, 20401da177e4SLinus Torvalds .fl_end = OFFSET_MAX, 20411da177e4SLinus Torvalds }; 20421da177e4SLinus Torvalds filp->f_op->flock(filp, F_SETLKW, &fl); 204380fec4c6STrond Myklebust if (fl.fl_ops && fl.fl_ops->fl_release_private) 204480fec4c6STrond Myklebust fl.fl_ops->fl_release_private(&fl); 20451da177e4SLinus Torvalds } 20461da177e4SLinus Torvalds 2047b89f4321SArnd Bergmann lock_flocks(); 20481da177e4SLinus Torvalds before = &inode->i_flock; 20491da177e4SLinus Torvalds 20501da177e4SLinus Torvalds while ((fl = *before) != NULL) { 20511da177e4SLinus Torvalds if (fl->fl_file == filp) { 2052c293621bSPeter Staubach if (IS_FLOCK(fl)) { 20531da177e4SLinus Torvalds locks_delete_lock(before); 20541da177e4SLinus Torvalds continue; 20551da177e4SLinus Torvalds } 20561da177e4SLinus Torvalds if (IS_LEASE(fl)) { 20571da177e4SLinus Torvalds lease_modify(before, F_UNLCK); 20581da177e4SLinus Torvalds continue; 20591da177e4SLinus Torvalds } 20601da177e4SLinus Torvalds /* What? */ 20611da177e4SLinus Torvalds BUG(); 20621da177e4SLinus Torvalds } 20631da177e4SLinus Torvalds before = &fl->fl_next; 20641da177e4SLinus Torvalds } 2065b89f4321SArnd Bergmann unlock_flocks(); 20661da177e4SLinus Torvalds } 20671da177e4SLinus Torvalds 20681da177e4SLinus Torvalds /** 20691da177e4SLinus Torvalds * posix_unblock_lock - stop waiting for a file lock 20701da177e4SLinus Torvalds * @filp: how the file was opened 20711da177e4SLinus Torvalds * @waiter: the lock which was waiting 20721da177e4SLinus Torvalds * 20731da177e4SLinus Torvalds * lockd needs to block waiting for locks. 20741da177e4SLinus Torvalds */ 207564a318eeSJ. Bruce Fields int 20761da177e4SLinus Torvalds posix_unblock_lock(struct file *filp, struct file_lock *waiter) 20771da177e4SLinus Torvalds { 207864a318eeSJ. Bruce Fields int status = 0; 207964a318eeSJ. Bruce Fields 2080b89f4321SArnd Bergmann lock_flocks(); 20815996a298SJ. Bruce Fields if (waiter->fl_next) 20821da177e4SLinus Torvalds __locks_delete_block(waiter); 208364a318eeSJ. Bruce Fields else 208464a318eeSJ. Bruce Fields status = -ENOENT; 2085b89f4321SArnd Bergmann unlock_flocks(); 208664a318eeSJ. Bruce Fields return status; 20871da177e4SLinus Torvalds } 20881da177e4SLinus Torvalds 20891da177e4SLinus Torvalds EXPORT_SYMBOL(posix_unblock_lock); 20901da177e4SLinus Torvalds 20919b9d2ab4SMarc Eshel /** 20929b9d2ab4SMarc Eshel * vfs_cancel_lock - file byte range unblock lock 20939b9d2ab4SMarc Eshel * @filp: The file to apply the unblock to 20949b9d2ab4SMarc Eshel * @fl: The lock to be unblocked 20959b9d2ab4SMarc Eshel * 20969b9d2ab4SMarc Eshel * Used by lock managers to cancel blocked requests 20979b9d2ab4SMarc Eshel */ 20989b9d2ab4SMarc Eshel int vfs_cancel_lock(struct file *filp, struct file_lock *fl) 20999b9d2ab4SMarc Eshel { 21009b9d2ab4SMarc Eshel if (filp->f_op && filp->f_op->lock) 21019b9d2ab4SMarc Eshel return filp->f_op->lock(filp, F_CANCELLK, fl); 21029b9d2ab4SMarc Eshel return 0; 21039b9d2ab4SMarc Eshel } 21049b9d2ab4SMarc Eshel 21059b9d2ab4SMarc Eshel EXPORT_SYMBOL_GPL(vfs_cancel_lock); 21069b9d2ab4SMarc Eshel 21077f8ada98SPavel Emelyanov #ifdef CONFIG_PROC_FS 2108d8ba7a36SAlexey Dobriyan #include <linux/proc_fs.h> 21097f8ada98SPavel Emelyanov #include <linux/seq_file.h> 21107f8ada98SPavel Emelyanov 21117f8ada98SPavel Emelyanov static void lock_get_status(struct seq_file *f, struct file_lock *fl, 2112*99dc8292SJerome Marchand loff_t id, char *pfx) 21131da177e4SLinus Torvalds { 21141da177e4SLinus Torvalds struct inode *inode = NULL; 2115ab1f1611SVitaliy Gusev unsigned int fl_pid; 2116ab1f1611SVitaliy Gusev 2117ab1f1611SVitaliy Gusev if (fl->fl_nspid) 21186c5f3e7bSPavel Emelyanov fl_pid = pid_vnr(fl->fl_nspid); 2119ab1f1611SVitaliy Gusev else 2120ab1f1611SVitaliy Gusev fl_pid = fl->fl_pid; 21211da177e4SLinus Torvalds 21221da177e4SLinus Torvalds if (fl->fl_file != NULL) 21230f7fc9e4SJosef "Jeff" Sipek inode = fl->fl_file->f_path.dentry->d_inode; 21241da177e4SLinus Torvalds 2125*99dc8292SJerome Marchand seq_printf(f, "%lld:%s ", id, pfx); 21261da177e4SLinus Torvalds if (IS_POSIX(fl)) { 21277f8ada98SPavel Emelyanov seq_printf(f, "%6s %s ", 21281da177e4SLinus Torvalds (fl->fl_flags & FL_ACCESS) ? "ACCESS" : "POSIX ", 21291da177e4SLinus Torvalds (inode == NULL) ? "*NOINODE*" : 2130a16877caSPavel Emelyanov mandatory_lock(inode) ? "MANDATORY" : "ADVISORY "); 21311da177e4SLinus Torvalds } else if (IS_FLOCK(fl)) { 21321da177e4SLinus Torvalds if (fl->fl_type & LOCK_MAND) { 21337f8ada98SPavel Emelyanov seq_printf(f, "FLOCK MSNFS "); 21341da177e4SLinus Torvalds } else { 21357f8ada98SPavel Emelyanov seq_printf(f, "FLOCK ADVISORY "); 21361da177e4SLinus Torvalds } 21371da177e4SLinus Torvalds } else if (IS_LEASE(fl)) { 21387f8ada98SPavel Emelyanov seq_printf(f, "LEASE "); 21391da177e4SLinus Torvalds if (fl->fl_type & F_INPROGRESS) 21407f8ada98SPavel Emelyanov seq_printf(f, "BREAKING "); 21411da177e4SLinus Torvalds else if (fl->fl_file) 21427f8ada98SPavel Emelyanov seq_printf(f, "ACTIVE "); 21431da177e4SLinus Torvalds else 21447f8ada98SPavel Emelyanov seq_printf(f, "BREAKER "); 21451da177e4SLinus Torvalds } else { 21467f8ada98SPavel Emelyanov seq_printf(f, "UNKNOWN UNKNOWN "); 21471da177e4SLinus Torvalds } 21481da177e4SLinus Torvalds if (fl->fl_type & LOCK_MAND) { 21497f8ada98SPavel Emelyanov seq_printf(f, "%s ", 21501da177e4SLinus Torvalds (fl->fl_type & LOCK_READ) 21511da177e4SLinus Torvalds ? (fl->fl_type & LOCK_WRITE) ? "RW " : "READ " 21521da177e4SLinus Torvalds : (fl->fl_type & LOCK_WRITE) ? "WRITE" : "NONE "); 21531da177e4SLinus Torvalds } else { 21547f8ada98SPavel Emelyanov seq_printf(f, "%s ", 21551da177e4SLinus Torvalds (fl->fl_type & F_INPROGRESS) 21561da177e4SLinus Torvalds ? (fl->fl_type & F_UNLCK) ? "UNLCK" : "READ " 21571da177e4SLinus Torvalds : (fl->fl_type & F_WRLCK) ? "WRITE" : "READ "); 21581da177e4SLinus Torvalds } 21591da177e4SLinus Torvalds if (inode) { 21601da177e4SLinus Torvalds #ifdef WE_CAN_BREAK_LSLK_NOW 2161ab1f1611SVitaliy Gusev seq_printf(f, "%d %s:%ld ", fl_pid, 21621da177e4SLinus Torvalds inode->i_sb->s_id, inode->i_ino); 21631da177e4SLinus Torvalds #else 21641da177e4SLinus Torvalds /* userspace relies on this representation of dev_t ;-( */ 2165ab1f1611SVitaliy Gusev seq_printf(f, "%d %02x:%02x:%ld ", fl_pid, 21661da177e4SLinus Torvalds MAJOR(inode->i_sb->s_dev), 21671da177e4SLinus Torvalds MINOR(inode->i_sb->s_dev), inode->i_ino); 21681da177e4SLinus Torvalds #endif 21691da177e4SLinus Torvalds } else { 2170ab1f1611SVitaliy Gusev seq_printf(f, "%d <none>:0 ", fl_pid); 21711da177e4SLinus Torvalds } 21721da177e4SLinus Torvalds if (IS_POSIX(fl)) { 21731da177e4SLinus Torvalds if (fl->fl_end == OFFSET_MAX) 21747f8ada98SPavel Emelyanov seq_printf(f, "%Ld EOF\n", fl->fl_start); 21751da177e4SLinus Torvalds else 21767f8ada98SPavel Emelyanov seq_printf(f, "%Ld %Ld\n", fl->fl_start, fl->fl_end); 21771da177e4SLinus Torvalds } else { 21787f8ada98SPavel Emelyanov seq_printf(f, "0 EOF\n"); 21791da177e4SLinus Torvalds } 21801da177e4SLinus Torvalds } 21811da177e4SLinus Torvalds 21827f8ada98SPavel Emelyanov static int locks_show(struct seq_file *f, void *v) 21831da177e4SLinus Torvalds { 21847f8ada98SPavel Emelyanov struct file_lock *fl, *bfl; 21857f8ada98SPavel Emelyanov 21867f8ada98SPavel Emelyanov fl = list_entry(v, struct file_lock, fl_link); 21877f8ada98SPavel Emelyanov 2188*99dc8292SJerome Marchand lock_get_status(f, fl, *((loff_t *)f->private), ""); 21897f8ada98SPavel Emelyanov 21907f8ada98SPavel Emelyanov list_for_each_entry(bfl, &fl->fl_block, fl_block) 2191*99dc8292SJerome Marchand lock_get_status(f, bfl, *((loff_t *)f->private), " ->"); 21927f8ada98SPavel Emelyanov 21937f8ada98SPavel Emelyanov return 0; 21941da177e4SLinus Torvalds } 21951da177e4SLinus Torvalds 21967f8ada98SPavel Emelyanov static void *locks_start(struct seq_file *f, loff_t *pos) 21971da177e4SLinus Torvalds { 2198*99dc8292SJerome Marchand loff_t *p = f->private; 2199*99dc8292SJerome Marchand 2200b89f4321SArnd Bergmann lock_flocks(); 2201*99dc8292SJerome Marchand *p = (*pos + 1); 22027f8ada98SPavel Emelyanov return seq_list_start(&file_lock_list, *pos); 22031da177e4SLinus Torvalds } 22047f8ada98SPavel Emelyanov 22057f8ada98SPavel Emelyanov static void *locks_next(struct seq_file *f, void *v, loff_t *pos) 22067f8ada98SPavel Emelyanov { 2207*99dc8292SJerome Marchand loff_t *p = f->private; 2208*99dc8292SJerome Marchand ++*p; 22097f8ada98SPavel Emelyanov return seq_list_next(v, &file_lock_list, pos); 22101da177e4SLinus Torvalds } 22117f8ada98SPavel Emelyanov 22127f8ada98SPavel Emelyanov static void locks_stop(struct seq_file *f, void *v) 22137f8ada98SPavel Emelyanov { 2214b89f4321SArnd Bergmann unlock_flocks(); 22151da177e4SLinus Torvalds } 22161da177e4SLinus Torvalds 2217d8ba7a36SAlexey Dobriyan static const struct seq_operations locks_seq_operations = { 22187f8ada98SPavel Emelyanov .start = locks_start, 22197f8ada98SPavel Emelyanov .next = locks_next, 22207f8ada98SPavel Emelyanov .stop = locks_stop, 22217f8ada98SPavel Emelyanov .show = locks_show, 22227f8ada98SPavel Emelyanov }; 2223d8ba7a36SAlexey Dobriyan 2224d8ba7a36SAlexey Dobriyan static int locks_open(struct inode *inode, struct file *filp) 2225d8ba7a36SAlexey Dobriyan { 2226*99dc8292SJerome Marchand return seq_open_private(filp, &locks_seq_operations, sizeof(loff_t)); 2227d8ba7a36SAlexey Dobriyan } 2228d8ba7a36SAlexey Dobriyan 2229d8ba7a36SAlexey Dobriyan static const struct file_operations proc_locks_operations = { 2230d8ba7a36SAlexey Dobriyan .open = locks_open, 2231d8ba7a36SAlexey Dobriyan .read = seq_read, 2232d8ba7a36SAlexey Dobriyan .llseek = seq_lseek, 2233*99dc8292SJerome Marchand .release = seq_release_private, 2234d8ba7a36SAlexey Dobriyan }; 2235d8ba7a36SAlexey Dobriyan 2236d8ba7a36SAlexey Dobriyan static int __init proc_locks_init(void) 2237d8ba7a36SAlexey Dobriyan { 2238d8ba7a36SAlexey Dobriyan proc_create("locks", 0, NULL, &proc_locks_operations); 2239d8ba7a36SAlexey Dobriyan return 0; 2240d8ba7a36SAlexey Dobriyan } 2241d8ba7a36SAlexey Dobriyan module_init(proc_locks_init); 22427f8ada98SPavel Emelyanov #endif 22437f8ada98SPavel Emelyanov 22441da177e4SLinus Torvalds /** 22451da177e4SLinus Torvalds * lock_may_read - checks that the region is free of locks 22461da177e4SLinus Torvalds * @inode: the inode that is being read 22471da177e4SLinus Torvalds * @start: the first byte to read 22481da177e4SLinus Torvalds * @len: the number of bytes to read 22491da177e4SLinus Torvalds * 22501da177e4SLinus Torvalds * Emulates Windows locking requirements. Whole-file 22511da177e4SLinus Torvalds * mandatory locks (share modes) can prohibit a read and 22521da177e4SLinus Torvalds * byte-range POSIX locks can prohibit a read if they overlap. 22531da177e4SLinus Torvalds * 22541da177e4SLinus Torvalds * N.B. this function is only ever called 22551da177e4SLinus Torvalds * from knfsd and ownership of locks is never checked. 22561da177e4SLinus Torvalds */ 22571da177e4SLinus Torvalds int lock_may_read(struct inode *inode, loff_t start, unsigned long len) 22581da177e4SLinus Torvalds { 22591da177e4SLinus Torvalds struct file_lock *fl; 22601da177e4SLinus Torvalds int result = 1; 2261b89f4321SArnd Bergmann lock_flocks(); 22621da177e4SLinus Torvalds for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) { 22631da177e4SLinus Torvalds if (IS_POSIX(fl)) { 22641da177e4SLinus Torvalds if (fl->fl_type == F_RDLCK) 22651da177e4SLinus Torvalds continue; 22661da177e4SLinus Torvalds if ((fl->fl_end < start) || (fl->fl_start > (start + len))) 22671da177e4SLinus Torvalds continue; 22681da177e4SLinus Torvalds } else if (IS_FLOCK(fl)) { 22691da177e4SLinus Torvalds if (!(fl->fl_type & LOCK_MAND)) 22701da177e4SLinus Torvalds continue; 22711da177e4SLinus Torvalds if (fl->fl_type & LOCK_READ) 22721da177e4SLinus Torvalds continue; 22731da177e4SLinus Torvalds } else 22741da177e4SLinus Torvalds continue; 22751da177e4SLinus Torvalds result = 0; 22761da177e4SLinus Torvalds break; 22771da177e4SLinus Torvalds } 2278b89f4321SArnd Bergmann unlock_flocks(); 22791da177e4SLinus Torvalds return result; 22801da177e4SLinus Torvalds } 22811da177e4SLinus Torvalds 22821da177e4SLinus Torvalds EXPORT_SYMBOL(lock_may_read); 22831da177e4SLinus Torvalds 22841da177e4SLinus Torvalds /** 22851da177e4SLinus Torvalds * lock_may_write - checks that the region is free of locks 22861da177e4SLinus Torvalds * @inode: the inode that is being written 22871da177e4SLinus Torvalds * @start: the first byte to write 22881da177e4SLinus Torvalds * @len: the number of bytes to write 22891da177e4SLinus Torvalds * 22901da177e4SLinus Torvalds * Emulates Windows locking requirements. Whole-file 22911da177e4SLinus Torvalds * mandatory locks (share modes) can prohibit a write and 22921da177e4SLinus Torvalds * byte-range POSIX locks can prohibit a write if they overlap. 22931da177e4SLinus Torvalds * 22941da177e4SLinus Torvalds * N.B. this function is only ever called 22951da177e4SLinus Torvalds * from knfsd and ownership of locks is never checked. 22961da177e4SLinus Torvalds */ 22971da177e4SLinus Torvalds int lock_may_write(struct inode *inode, loff_t start, unsigned long len) 22981da177e4SLinus Torvalds { 22991da177e4SLinus Torvalds struct file_lock *fl; 23001da177e4SLinus Torvalds int result = 1; 2301b89f4321SArnd Bergmann lock_flocks(); 23021da177e4SLinus Torvalds for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) { 23031da177e4SLinus Torvalds if (IS_POSIX(fl)) { 23041da177e4SLinus Torvalds if ((fl->fl_end < start) || (fl->fl_start > (start + len))) 23051da177e4SLinus Torvalds continue; 23061da177e4SLinus Torvalds } else if (IS_FLOCK(fl)) { 23071da177e4SLinus Torvalds if (!(fl->fl_type & LOCK_MAND)) 23081da177e4SLinus Torvalds continue; 23091da177e4SLinus Torvalds if (fl->fl_type & LOCK_WRITE) 23101da177e4SLinus Torvalds continue; 23111da177e4SLinus Torvalds } else 23121da177e4SLinus Torvalds continue; 23131da177e4SLinus Torvalds result = 0; 23141da177e4SLinus Torvalds break; 23151da177e4SLinus Torvalds } 2316b89f4321SArnd Bergmann unlock_flocks(); 23171da177e4SLinus Torvalds return result; 23181da177e4SLinus Torvalds } 23191da177e4SLinus Torvalds 23201da177e4SLinus Torvalds EXPORT_SYMBOL(lock_may_write); 23211da177e4SLinus Torvalds 23221da177e4SLinus Torvalds static int __init filelock_init(void) 23231da177e4SLinus Torvalds { 23241da177e4SLinus Torvalds filelock_cache = kmem_cache_create("file_lock_cache", 23251da177e4SLinus Torvalds sizeof(struct file_lock), 0, SLAB_PANIC, 232620c2df83SPaul Mundt init_once); 23271da177e4SLinus Torvalds return 0; 23281da177e4SLinus Torvalds } 23291da177e4SLinus Torvalds 23301da177e4SLinus Torvalds core_initcall(filelock_init); 2331