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 146e18b890bSChristoph Lameter static struct kmem_cache *filelock_cache __read_mostly; 1471da177e4SLinus Torvalds 1481da177e4SLinus Torvalds /* Allocate an empty lock structure. */ 1491da177e4SLinus Torvalds static struct file_lock *locks_alloc_lock(void) 1501da177e4SLinus Torvalds { 151e94b1766SChristoph Lameter return kmem_cache_alloc(filelock_cache, GFP_KERNEL); 1521da177e4SLinus Torvalds } 1531da177e4SLinus Torvalds 15447831f35STrond Myklebust static void locks_release_private(struct file_lock *fl) 15547831f35STrond Myklebust { 15647831f35STrond Myklebust if (fl->fl_ops) { 15747831f35STrond Myklebust if (fl->fl_ops->fl_release_private) 15847831f35STrond Myklebust fl->fl_ops->fl_release_private(fl); 15947831f35STrond Myklebust fl->fl_ops = NULL; 16047831f35STrond Myklebust } 16147831f35STrond Myklebust if (fl->fl_lmops) { 16247831f35STrond Myklebust if (fl->fl_lmops->fl_release_private) 16347831f35STrond Myklebust fl->fl_lmops->fl_release_private(fl); 16447831f35STrond Myklebust fl->fl_lmops = NULL; 16547831f35STrond Myklebust } 16647831f35STrond Myklebust 16747831f35STrond Myklebust } 16847831f35STrond Myklebust 1691da177e4SLinus Torvalds /* Free a lock which is not in use. */ 17033443c42SMatt Mackall static void locks_free_lock(struct file_lock *fl) 1711da177e4SLinus Torvalds { 1725ce29646SMiklos Szeredi BUG_ON(waitqueue_active(&fl->fl_wait)); 1735ce29646SMiklos Szeredi BUG_ON(!list_empty(&fl->fl_block)); 1745ce29646SMiklos Szeredi BUG_ON(!list_empty(&fl->fl_link)); 1751da177e4SLinus Torvalds 17647831f35STrond Myklebust locks_release_private(fl); 1771da177e4SLinus Torvalds kmem_cache_free(filelock_cache, fl); 1781da177e4SLinus Torvalds } 1791da177e4SLinus Torvalds 1801da177e4SLinus Torvalds void locks_init_lock(struct file_lock *fl) 1811da177e4SLinus Torvalds { 1821da177e4SLinus Torvalds INIT_LIST_HEAD(&fl->fl_link); 1831da177e4SLinus Torvalds INIT_LIST_HEAD(&fl->fl_block); 1841da177e4SLinus Torvalds init_waitqueue_head(&fl->fl_wait); 1851da177e4SLinus Torvalds fl->fl_next = NULL; 1861da177e4SLinus Torvalds fl->fl_fasync = NULL; 1871da177e4SLinus Torvalds fl->fl_owner = NULL; 1881da177e4SLinus Torvalds fl->fl_pid = 0; 189ab1f1611SVitaliy Gusev fl->fl_nspid = NULL; 1901da177e4SLinus Torvalds fl->fl_file = NULL; 1911da177e4SLinus Torvalds fl->fl_flags = 0; 1921da177e4SLinus Torvalds fl->fl_type = 0; 1931da177e4SLinus Torvalds fl->fl_start = fl->fl_end = 0; 1941da177e4SLinus Torvalds fl->fl_ops = NULL; 1951da177e4SLinus Torvalds fl->fl_lmops = NULL; 1961da177e4SLinus Torvalds } 1971da177e4SLinus Torvalds 1981da177e4SLinus Torvalds EXPORT_SYMBOL(locks_init_lock); 1991da177e4SLinus Torvalds 2001da177e4SLinus Torvalds /* 2011da177e4SLinus Torvalds * Initialises the fields of the file lock which are invariant for 2021da177e4SLinus Torvalds * free file_locks. 2031da177e4SLinus Torvalds */ 20451cc5068SAlexey Dobriyan static void init_once(void *foo) 2051da177e4SLinus Torvalds { 2061da177e4SLinus Torvalds struct file_lock *lock = (struct file_lock *) foo; 2071da177e4SLinus Torvalds 2081da177e4SLinus Torvalds locks_init_lock(lock); 2091da177e4SLinus Torvalds } 2101da177e4SLinus Torvalds 21147831f35STrond Myklebust static void locks_copy_private(struct file_lock *new, struct file_lock *fl) 21247831f35STrond Myklebust { 21347831f35STrond Myklebust if (fl->fl_ops) { 21447831f35STrond Myklebust if (fl->fl_ops->fl_copy_lock) 21547831f35STrond Myklebust fl->fl_ops->fl_copy_lock(new, fl); 21647831f35STrond Myklebust new->fl_ops = fl->fl_ops; 21747831f35STrond Myklebust } 21847831f35STrond Myklebust if (fl->fl_lmops) { 21947831f35STrond Myklebust if (fl->fl_lmops->fl_copy_lock) 22047831f35STrond Myklebust fl->fl_lmops->fl_copy_lock(new, fl); 22147831f35STrond Myklebust new->fl_lmops = fl->fl_lmops; 22247831f35STrond Myklebust } 22347831f35STrond Myklebust } 22447831f35STrond Myklebust 2251da177e4SLinus Torvalds /* 2261da177e4SLinus Torvalds * Initialize a new lock from an existing file_lock structure. 2271da177e4SLinus Torvalds */ 2281a747ee0SJ. Bruce Fields void __locks_copy_lock(struct file_lock *new, const struct file_lock *fl) 2291da177e4SLinus Torvalds { 2301da177e4SLinus Torvalds new->fl_owner = fl->fl_owner; 2311da177e4SLinus Torvalds new->fl_pid = fl->fl_pid; 2320996905fSTrond Myklebust new->fl_file = NULL; 2331da177e4SLinus Torvalds new->fl_flags = fl->fl_flags; 2341da177e4SLinus Torvalds new->fl_type = fl->fl_type; 2351da177e4SLinus Torvalds new->fl_start = fl->fl_start; 2361da177e4SLinus Torvalds new->fl_end = fl->fl_end; 2370996905fSTrond Myklebust new->fl_ops = NULL; 2380996905fSTrond Myklebust new->fl_lmops = NULL; 2390996905fSTrond Myklebust } 2403dd7b71cSRoland Dreier EXPORT_SYMBOL(__locks_copy_lock); 2410996905fSTrond Myklebust 2420996905fSTrond Myklebust void locks_copy_lock(struct file_lock *new, struct file_lock *fl) 2430996905fSTrond Myklebust { 2440996905fSTrond Myklebust locks_release_private(new); 2450996905fSTrond Myklebust 2460996905fSTrond Myklebust __locks_copy_lock(new, fl); 2470996905fSTrond Myklebust new->fl_file = fl->fl_file; 2481da177e4SLinus Torvalds new->fl_ops = fl->fl_ops; 2491da177e4SLinus Torvalds new->fl_lmops = fl->fl_lmops; 25047831f35STrond Myklebust 25147831f35STrond Myklebust locks_copy_private(new, fl); 2521da177e4SLinus Torvalds } 2531da177e4SLinus Torvalds 2541da177e4SLinus Torvalds EXPORT_SYMBOL(locks_copy_lock); 2551da177e4SLinus Torvalds 2561da177e4SLinus Torvalds static inline int flock_translate_cmd(int cmd) { 2571da177e4SLinus Torvalds if (cmd & LOCK_MAND) 2581da177e4SLinus Torvalds return cmd & (LOCK_MAND | LOCK_RW); 2591da177e4SLinus Torvalds switch (cmd) { 2601da177e4SLinus Torvalds case LOCK_SH: 2611da177e4SLinus Torvalds return F_RDLCK; 2621da177e4SLinus Torvalds case LOCK_EX: 2631da177e4SLinus Torvalds return F_WRLCK; 2641da177e4SLinus Torvalds case LOCK_UN: 2651da177e4SLinus Torvalds return F_UNLCK; 2661da177e4SLinus Torvalds } 2671da177e4SLinus Torvalds return -EINVAL; 2681da177e4SLinus Torvalds } 2691da177e4SLinus Torvalds 2701da177e4SLinus Torvalds /* Fill in a file_lock structure with an appropriate FLOCK lock. */ 2711da177e4SLinus Torvalds static int flock_make_lock(struct file *filp, struct file_lock **lock, 2721da177e4SLinus Torvalds unsigned int cmd) 2731da177e4SLinus Torvalds { 2741da177e4SLinus Torvalds struct file_lock *fl; 2751da177e4SLinus Torvalds int type = flock_translate_cmd(cmd); 2761da177e4SLinus Torvalds if (type < 0) 2771da177e4SLinus Torvalds return type; 2781da177e4SLinus Torvalds 2791da177e4SLinus Torvalds fl = locks_alloc_lock(); 2801da177e4SLinus Torvalds if (fl == NULL) 2811da177e4SLinus Torvalds return -ENOMEM; 2821da177e4SLinus Torvalds 2831da177e4SLinus Torvalds fl->fl_file = filp; 2841da177e4SLinus Torvalds fl->fl_pid = current->tgid; 2851da177e4SLinus Torvalds fl->fl_flags = FL_FLOCK; 2861da177e4SLinus Torvalds fl->fl_type = type; 2871da177e4SLinus Torvalds fl->fl_end = OFFSET_MAX; 2881da177e4SLinus Torvalds 2891da177e4SLinus Torvalds *lock = fl; 2901da177e4SLinus Torvalds return 0; 2911da177e4SLinus Torvalds } 2921da177e4SLinus Torvalds 2931da177e4SLinus Torvalds static int assign_type(struct file_lock *fl, int type) 2941da177e4SLinus Torvalds { 2951da177e4SLinus Torvalds switch (type) { 2961da177e4SLinus Torvalds case F_RDLCK: 2971da177e4SLinus Torvalds case F_WRLCK: 2981da177e4SLinus Torvalds case F_UNLCK: 2991da177e4SLinus Torvalds fl->fl_type = type; 3001da177e4SLinus Torvalds break; 3011da177e4SLinus Torvalds default: 3021da177e4SLinus Torvalds return -EINVAL; 3031da177e4SLinus Torvalds } 3041da177e4SLinus Torvalds return 0; 3051da177e4SLinus Torvalds } 3061da177e4SLinus Torvalds 3071da177e4SLinus Torvalds /* Verify a "struct flock" and copy it to a "struct file_lock" as a POSIX 3081da177e4SLinus Torvalds * style lock. 3091da177e4SLinus Torvalds */ 3101da177e4SLinus Torvalds static int flock_to_posix_lock(struct file *filp, struct file_lock *fl, 3111da177e4SLinus Torvalds struct flock *l) 3121da177e4SLinus Torvalds { 3131da177e4SLinus Torvalds off_t start, end; 3141da177e4SLinus Torvalds 3151da177e4SLinus Torvalds switch (l->l_whence) { 316f5579f8cSJosef 'Jeff' Sipek case SEEK_SET: 3171da177e4SLinus Torvalds start = 0; 3181da177e4SLinus Torvalds break; 319f5579f8cSJosef 'Jeff' Sipek case SEEK_CUR: 3201da177e4SLinus Torvalds start = filp->f_pos; 3211da177e4SLinus Torvalds break; 322f5579f8cSJosef 'Jeff' Sipek case SEEK_END: 3230f7fc9e4SJosef "Jeff" Sipek start = i_size_read(filp->f_path.dentry->d_inode); 3241da177e4SLinus Torvalds break; 3251da177e4SLinus Torvalds default: 3261da177e4SLinus Torvalds return -EINVAL; 3271da177e4SLinus Torvalds } 3281da177e4SLinus Torvalds 3291da177e4SLinus Torvalds /* POSIX-1996 leaves the case l->l_len < 0 undefined; 3301da177e4SLinus Torvalds POSIX-2001 defines it. */ 3311da177e4SLinus Torvalds start += l->l_start; 3321da177e4SLinus Torvalds if (start < 0) 3331da177e4SLinus Torvalds return -EINVAL; 3341da177e4SLinus Torvalds fl->fl_end = OFFSET_MAX; 3354c780a46STrond Myklebust if (l->l_len > 0) { 3364c780a46STrond Myklebust end = start + l->l_len - 1; 3374c780a46STrond Myklebust fl->fl_end = end; 3384c780a46STrond Myklebust } else if (l->l_len < 0) { 3394c780a46STrond Myklebust end = start - 1; 3404c780a46STrond Myklebust fl->fl_end = end; 3414c780a46STrond Myklebust start += l->l_len; 3424c780a46STrond Myklebust if (start < 0) 3434c780a46STrond Myklebust return -EINVAL; 3444c780a46STrond Myklebust } 3454c780a46STrond Myklebust fl->fl_start = start; /* we record the absolute position */ 3464c780a46STrond Myklebust if (fl->fl_end < fl->fl_start) 3474c780a46STrond Myklebust return -EOVERFLOW; 3481da177e4SLinus Torvalds 3491da177e4SLinus Torvalds fl->fl_owner = current->files; 3501da177e4SLinus Torvalds fl->fl_pid = current->tgid; 3511da177e4SLinus Torvalds fl->fl_file = filp; 3521da177e4SLinus Torvalds fl->fl_flags = FL_POSIX; 3531da177e4SLinus Torvalds fl->fl_ops = NULL; 3541da177e4SLinus Torvalds fl->fl_lmops = NULL; 3551da177e4SLinus Torvalds 3561da177e4SLinus Torvalds return assign_type(fl, l->l_type); 3571da177e4SLinus Torvalds } 3581da177e4SLinus Torvalds 3591da177e4SLinus Torvalds #if BITS_PER_LONG == 32 3601da177e4SLinus Torvalds static int flock64_to_posix_lock(struct file *filp, struct file_lock *fl, 3611da177e4SLinus Torvalds struct flock64 *l) 3621da177e4SLinus Torvalds { 3631da177e4SLinus Torvalds loff_t start; 3641da177e4SLinus Torvalds 3651da177e4SLinus Torvalds switch (l->l_whence) { 366f5579f8cSJosef 'Jeff' Sipek case SEEK_SET: 3671da177e4SLinus Torvalds start = 0; 3681da177e4SLinus Torvalds break; 369f5579f8cSJosef 'Jeff' Sipek case SEEK_CUR: 3701da177e4SLinus Torvalds start = filp->f_pos; 3711da177e4SLinus Torvalds break; 372f5579f8cSJosef 'Jeff' Sipek case SEEK_END: 3730f7fc9e4SJosef "Jeff" Sipek start = i_size_read(filp->f_path.dentry->d_inode); 3741da177e4SLinus Torvalds break; 3751da177e4SLinus Torvalds default: 3761da177e4SLinus Torvalds return -EINVAL; 3771da177e4SLinus Torvalds } 3781da177e4SLinus Torvalds 3794c780a46STrond Myklebust start += l->l_start; 3804c780a46STrond Myklebust if (start < 0) 3811da177e4SLinus Torvalds return -EINVAL; 3821da177e4SLinus Torvalds fl->fl_end = OFFSET_MAX; 3834c780a46STrond Myklebust if (l->l_len > 0) { 3844c780a46STrond Myklebust fl->fl_end = start + l->l_len - 1; 3854c780a46STrond Myklebust } else if (l->l_len < 0) { 3864c780a46STrond Myklebust fl->fl_end = start - 1; 3874c780a46STrond Myklebust start += l->l_len; 3884c780a46STrond Myklebust if (start < 0) 3894c780a46STrond Myklebust return -EINVAL; 3904c780a46STrond Myklebust } 3914c780a46STrond Myklebust fl->fl_start = start; /* we record the absolute position */ 3924c780a46STrond Myklebust if (fl->fl_end < fl->fl_start) 3934c780a46STrond Myklebust return -EOVERFLOW; 3941da177e4SLinus Torvalds 3951da177e4SLinus Torvalds fl->fl_owner = current->files; 3961da177e4SLinus Torvalds fl->fl_pid = current->tgid; 3971da177e4SLinus Torvalds fl->fl_file = filp; 3981da177e4SLinus Torvalds fl->fl_flags = FL_POSIX; 3991da177e4SLinus Torvalds fl->fl_ops = NULL; 4001da177e4SLinus Torvalds fl->fl_lmops = NULL; 4011da177e4SLinus Torvalds 4021da177e4SLinus Torvalds switch (l->l_type) { 4031da177e4SLinus Torvalds case F_RDLCK: 4041da177e4SLinus Torvalds case F_WRLCK: 4051da177e4SLinus Torvalds case F_UNLCK: 4061da177e4SLinus Torvalds fl->fl_type = l->l_type; 4071da177e4SLinus Torvalds break; 4081da177e4SLinus Torvalds default: 4091da177e4SLinus Torvalds return -EINVAL; 4101da177e4SLinus Torvalds } 4111da177e4SLinus Torvalds 4121da177e4SLinus Torvalds return (0); 4131da177e4SLinus Torvalds } 4141da177e4SLinus Torvalds #endif 4151da177e4SLinus Torvalds 4161da177e4SLinus Torvalds /* default lease lock manager operations */ 4171da177e4SLinus Torvalds static void lease_break_callback(struct file_lock *fl) 4181da177e4SLinus Torvalds { 4191da177e4SLinus Torvalds kill_fasync(&fl->fl_fasync, SIGIO, POLL_MSG); 4201da177e4SLinus Torvalds } 4211da177e4SLinus Torvalds 4221da177e4SLinus Torvalds static void lease_release_private_callback(struct file_lock *fl) 4231da177e4SLinus Torvalds { 4241da177e4SLinus Torvalds if (!fl->fl_file) 4251da177e4SLinus Torvalds return; 4261da177e4SLinus Torvalds 4271da177e4SLinus Torvalds f_delown(fl->fl_file); 4281da177e4SLinus Torvalds fl->fl_file->f_owner.signum = 0; 4291da177e4SLinus Torvalds } 4301da177e4SLinus Torvalds 43175c96f85SAdrian Bunk static int lease_mylease_callback(struct file_lock *fl, struct file_lock *try) 4321da177e4SLinus Torvalds { 4331da177e4SLinus Torvalds return fl->fl_file == try->fl_file; 4341da177e4SLinus Torvalds } 4351da177e4SLinus Torvalds 43675c96f85SAdrian Bunk static struct lock_manager_operations lease_manager_ops = { 4371da177e4SLinus Torvalds .fl_break = lease_break_callback, 4381da177e4SLinus Torvalds .fl_release_private = lease_release_private_callback, 4391da177e4SLinus Torvalds .fl_mylease = lease_mylease_callback, 4401da177e4SLinus Torvalds .fl_change = lease_modify, 4411da177e4SLinus Torvalds }; 4421da177e4SLinus Torvalds 4431da177e4SLinus Torvalds /* 4441da177e4SLinus Torvalds * Initialize a lease, use the default lock manager operations 4451da177e4SLinus Torvalds */ 4461da177e4SLinus Torvalds static int lease_init(struct file *filp, int type, struct file_lock *fl) 4471da177e4SLinus Torvalds { 44875dff55aSTrond Myklebust if (assign_type(fl, type) != 0) 44975dff55aSTrond Myklebust return -EINVAL; 45075dff55aSTrond Myklebust 4511da177e4SLinus Torvalds fl->fl_owner = current->files; 4521da177e4SLinus Torvalds fl->fl_pid = current->tgid; 4531da177e4SLinus Torvalds 4541da177e4SLinus Torvalds fl->fl_file = filp; 4551da177e4SLinus Torvalds fl->fl_flags = FL_LEASE; 4561da177e4SLinus Torvalds fl->fl_start = 0; 4571da177e4SLinus Torvalds fl->fl_end = OFFSET_MAX; 4581da177e4SLinus Torvalds fl->fl_ops = NULL; 4591da177e4SLinus Torvalds fl->fl_lmops = &lease_manager_ops; 4601da177e4SLinus Torvalds return 0; 4611da177e4SLinus Torvalds } 4621da177e4SLinus Torvalds 4631da177e4SLinus Torvalds /* Allocate a file_lock initialised to this type of lease */ 464e32b8ee2SJ. Bruce Fields static struct file_lock *lease_alloc(struct file *filp, int type) 4651da177e4SLinus Torvalds { 4661da177e4SLinus Torvalds struct file_lock *fl = locks_alloc_lock(); 46775dff55aSTrond Myklebust int error = -ENOMEM; 4681da177e4SLinus Torvalds 4691da177e4SLinus Torvalds if (fl == NULL) 470e32b8ee2SJ. Bruce Fields return ERR_PTR(error); 4711da177e4SLinus Torvalds 4721da177e4SLinus Torvalds error = lease_init(filp, type, fl); 47375dff55aSTrond Myklebust if (error) { 47475dff55aSTrond Myklebust locks_free_lock(fl); 475e32b8ee2SJ. Bruce Fields return ERR_PTR(error); 47675dff55aSTrond Myklebust } 477e32b8ee2SJ. Bruce Fields return fl; 4781da177e4SLinus Torvalds } 4791da177e4SLinus Torvalds 4801da177e4SLinus Torvalds /* Check if two locks overlap each other. 4811da177e4SLinus Torvalds */ 4821da177e4SLinus Torvalds static inline int locks_overlap(struct file_lock *fl1, struct file_lock *fl2) 4831da177e4SLinus Torvalds { 4841da177e4SLinus Torvalds return ((fl1->fl_end >= fl2->fl_start) && 4851da177e4SLinus Torvalds (fl2->fl_end >= fl1->fl_start)); 4861da177e4SLinus Torvalds } 4871da177e4SLinus Torvalds 4881da177e4SLinus Torvalds /* 4891da177e4SLinus Torvalds * Check whether two locks have the same owner. 4901da177e4SLinus Torvalds */ 49133443c42SMatt Mackall static int posix_same_owner(struct file_lock *fl1, struct file_lock *fl2) 4921da177e4SLinus Torvalds { 4931da177e4SLinus Torvalds if (fl1->fl_lmops && fl1->fl_lmops->fl_compare_owner) 4941da177e4SLinus Torvalds return fl2->fl_lmops == fl1->fl_lmops && 4951da177e4SLinus Torvalds fl1->fl_lmops->fl_compare_owner(fl1, fl2); 4961da177e4SLinus Torvalds return fl1->fl_owner == fl2->fl_owner; 4971da177e4SLinus Torvalds } 4981da177e4SLinus Torvalds 4991da177e4SLinus Torvalds /* Remove waiter from blocker's block list. 5001da177e4SLinus Torvalds * When blocker ends up pointing to itself then the list is empty. 5011da177e4SLinus Torvalds */ 50233443c42SMatt Mackall static void __locks_delete_block(struct file_lock *waiter) 5031da177e4SLinus Torvalds { 5041da177e4SLinus Torvalds list_del_init(&waiter->fl_block); 5051da177e4SLinus Torvalds list_del_init(&waiter->fl_link); 5061da177e4SLinus Torvalds waiter->fl_next = NULL; 5071da177e4SLinus Torvalds } 5081da177e4SLinus Torvalds 5091da177e4SLinus Torvalds /* 5101da177e4SLinus Torvalds */ 5111da177e4SLinus Torvalds static void locks_delete_block(struct file_lock *waiter) 5121da177e4SLinus Torvalds { 5131da177e4SLinus Torvalds lock_kernel(); 5141da177e4SLinus Torvalds __locks_delete_block(waiter); 5151da177e4SLinus Torvalds unlock_kernel(); 5161da177e4SLinus Torvalds } 5171da177e4SLinus Torvalds 5181da177e4SLinus Torvalds /* Insert waiter into blocker's block list. 5191da177e4SLinus Torvalds * We use a circular list so that processes can be easily woken up in 5201da177e4SLinus Torvalds * the order they blocked. The documentation doesn't require this but 5211da177e4SLinus Torvalds * it seems like the reasonable thing to do. 5221da177e4SLinus Torvalds */ 5231da177e4SLinus Torvalds static void locks_insert_block(struct file_lock *blocker, 5241da177e4SLinus Torvalds struct file_lock *waiter) 5251da177e4SLinus Torvalds { 5266dc0fe8fSJ. Bruce Fields BUG_ON(!list_empty(&waiter->fl_block)); 5271da177e4SLinus Torvalds list_add_tail(&waiter->fl_block, &blocker->fl_block); 5281da177e4SLinus Torvalds waiter->fl_next = blocker; 5291da177e4SLinus Torvalds if (IS_POSIX(blocker)) 5301da177e4SLinus Torvalds list_add(&waiter->fl_link, &blocked_list); 5311da177e4SLinus Torvalds } 5321da177e4SLinus Torvalds 5331da177e4SLinus Torvalds /* Wake up processes blocked waiting for blocker. 5341da177e4SLinus Torvalds * If told to wait then schedule the processes until the block list 5351da177e4SLinus Torvalds * is empty, otherwise empty the block list ourselves. 5361da177e4SLinus Torvalds */ 5371da177e4SLinus Torvalds static void locks_wake_up_blocks(struct file_lock *blocker) 5381da177e4SLinus Torvalds { 5391da177e4SLinus Torvalds while (!list_empty(&blocker->fl_block)) { 540f0c1cd0eSPavel Emelyanov struct file_lock *waiter; 541f0c1cd0eSPavel Emelyanov 542f0c1cd0eSPavel Emelyanov waiter = list_first_entry(&blocker->fl_block, 5431da177e4SLinus Torvalds struct file_lock, fl_block); 5441da177e4SLinus Torvalds __locks_delete_block(waiter); 5451da177e4SLinus Torvalds if (waiter->fl_lmops && waiter->fl_lmops->fl_notify) 5461da177e4SLinus Torvalds waiter->fl_lmops->fl_notify(waiter); 5471da177e4SLinus Torvalds else 5481da177e4SLinus Torvalds wake_up(&waiter->fl_wait); 5491da177e4SLinus Torvalds } 5501da177e4SLinus Torvalds } 5511da177e4SLinus Torvalds 5521da177e4SLinus Torvalds /* Insert file lock fl into an inode's lock list at the position indicated 5531da177e4SLinus Torvalds * by pos. At the same time add the lock to the global file lock list. 5541da177e4SLinus Torvalds */ 5551da177e4SLinus Torvalds static void locks_insert_lock(struct file_lock **pos, struct file_lock *fl) 5561da177e4SLinus Torvalds { 5571da177e4SLinus Torvalds list_add(&fl->fl_link, &file_lock_list); 5581da177e4SLinus Torvalds 559ab1f1611SVitaliy Gusev fl->fl_nspid = get_pid(task_tgid(current)); 560ab1f1611SVitaliy Gusev 5611da177e4SLinus Torvalds /* insert into file's list */ 5621da177e4SLinus Torvalds fl->fl_next = *pos; 5631da177e4SLinus Torvalds *pos = fl; 5641da177e4SLinus Torvalds } 5651da177e4SLinus Torvalds 5661da177e4SLinus Torvalds /* 5671da177e4SLinus Torvalds * Delete a lock and then free it. 5681da177e4SLinus Torvalds * Wake up processes that are blocked waiting for this lock, 5691da177e4SLinus Torvalds * notify the FS that the lock has been cleared and 5701da177e4SLinus Torvalds * finally free the lock. 5711da177e4SLinus Torvalds */ 5721da177e4SLinus Torvalds static void locks_delete_lock(struct file_lock **thisfl_p) 5731da177e4SLinus Torvalds { 5741da177e4SLinus Torvalds struct file_lock *fl = *thisfl_p; 5751da177e4SLinus Torvalds 5761da177e4SLinus Torvalds *thisfl_p = fl->fl_next; 5771da177e4SLinus Torvalds fl->fl_next = NULL; 5781da177e4SLinus Torvalds list_del_init(&fl->fl_link); 5791da177e4SLinus Torvalds 5801da177e4SLinus Torvalds fasync_helper(0, fl->fl_file, 0, &fl->fl_fasync); 5811da177e4SLinus Torvalds if (fl->fl_fasync != NULL) { 5821da177e4SLinus Torvalds printk(KERN_ERR "locks_delete_lock: fasync == %p\n", fl->fl_fasync); 5831da177e4SLinus Torvalds fl->fl_fasync = NULL; 5841da177e4SLinus Torvalds } 5851da177e4SLinus Torvalds 586ab1f1611SVitaliy Gusev if (fl->fl_nspid) { 587ab1f1611SVitaliy Gusev put_pid(fl->fl_nspid); 588ab1f1611SVitaliy Gusev fl->fl_nspid = NULL; 589ab1f1611SVitaliy Gusev } 590ab1f1611SVitaliy Gusev 5911da177e4SLinus Torvalds locks_wake_up_blocks(fl); 5921da177e4SLinus Torvalds locks_free_lock(fl); 5931da177e4SLinus Torvalds } 5941da177e4SLinus Torvalds 5951da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. Common functionality 5961da177e4SLinus Torvalds * checks for shared/exclusive status of overlapping locks. 5971da177e4SLinus Torvalds */ 5981da177e4SLinus Torvalds static int locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl) 5991da177e4SLinus Torvalds { 6001da177e4SLinus Torvalds if (sys_fl->fl_type == F_WRLCK) 6011da177e4SLinus Torvalds return 1; 6021da177e4SLinus Torvalds if (caller_fl->fl_type == F_WRLCK) 6031da177e4SLinus Torvalds return 1; 6041da177e4SLinus Torvalds return 0; 6051da177e4SLinus Torvalds } 6061da177e4SLinus Torvalds 6071da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. POSIX specific 6081da177e4SLinus Torvalds * checking before calling the locks_conflict(). 6091da177e4SLinus Torvalds */ 6101da177e4SLinus Torvalds static int posix_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl) 6111da177e4SLinus Torvalds { 6121da177e4SLinus Torvalds /* POSIX locks owned by the same process do not conflict with 6131da177e4SLinus Torvalds * each other. 6141da177e4SLinus Torvalds */ 6151da177e4SLinus Torvalds if (!IS_POSIX(sys_fl) || posix_same_owner(caller_fl, sys_fl)) 6161da177e4SLinus Torvalds return (0); 6171da177e4SLinus Torvalds 6181da177e4SLinus Torvalds /* Check whether they overlap */ 6191da177e4SLinus Torvalds if (!locks_overlap(caller_fl, sys_fl)) 6201da177e4SLinus Torvalds return 0; 6211da177e4SLinus Torvalds 6221da177e4SLinus Torvalds return (locks_conflict(caller_fl, sys_fl)); 6231da177e4SLinus Torvalds } 6241da177e4SLinus Torvalds 6251da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. FLOCK specific 6261da177e4SLinus Torvalds * checking before calling the locks_conflict(). 6271da177e4SLinus Torvalds */ 6281da177e4SLinus Torvalds static int flock_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl) 6291da177e4SLinus Torvalds { 6301da177e4SLinus Torvalds /* FLOCK locks referring to the same filp do not conflict with 6311da177e4SLinus Torvalds * each other. 6321da177e4SLinus Torvalds */ 6331da177e4SLinus Torvalds if (!IS_FLOCK(sys_fl) || (caller_fl->fl_file == sys_fl->fl_file)) 6341da177e4SLinus Torvalds return (0); 6351da177e4SLinus Torvalds if ((caller_fl->fl_type & LOCK_MAND) || (sys_fl->fl_type & LOCK_MAND)) 6361da177e4SLinus Torvalds return 0; 6371da177e4SLinus Torvalds 6381da177e4SLinus Torvalds return (locks_conflict(caller_fl, sys_fl)); 6391da177e4SLinus Torvalds } 6401da177e4SLinus Torvalds 6416d34ac19SJ. Bruce Fields void 6429d6a8c5cSMarc Eshel posix_test_lock(struct file *filp, struct file_lock *fl) 6431da177e4SLinus Torvalds { 6441da177e4SLinus Torvalds struct file_lock *cfl; 6451da177e4SLinus Torvalds 6461da177e4SLinus Torvalds lock_kernel(); 6470f7fc9e4SJosef "Jeff" Sipek for (cfl = filp->f_path.dentry->d_inode->i_flock; cfl; cfl = cfl->fl_next) { 6481da177e4SLinus Torvalds if (!IS_POSIX(cfl)) 6491da177e4SLinus Torvalds continue; 650b842e240SJ. Bruce Fields if (posix_locks_conflict(fl, cfl)) 6511da177e4SLinus Torvalds break; 6521da177e4SLinus Torvalds } 653ab1f1611SVitaliy Gusev if (cfl) { 6549d6a8c5cSMarc Eshel __locks_copy_lock(fl, cfl); 655ab1f1611SVitaliy Gusev if (cfl->fl_nspid) 6566c5f3e7bSPavel Emelyanov fl->fl_pid = pid_vnr(cfl->fl_nspid); 657ab1f1611SVitaliy Gusev } else 658129a84deSJ. Bruce Fields fl->fl_type = F_UNLCK; 6598dc7c311SAndy Adamson unlock_kernel(); 6606d34ac19SJ. Bruce Fields return; 6611da177e4SLinus Torvalds } 6621da177e4SLinus Torvalds EXPORT_SYMBOL(posix_test_lock); 6631da177e4SLinus Torvalds 664b533184fSJ. Bruce Fields /* 665b533184fSJ. Bruce Fields * Deadlock detection: 6661da177e4SLinus Torvalds * 667b533184fSJ. Bruce Fields * We attempt to detect deadlocks that are due purely to posix file 668b533184fSJ. Bruce Fields * locks. 6691da177e4SLinus Torvalds * 670b533184fSJ. Bruce Fields * We assume that a task can be waiting for at most one lock at a time. 671b533184fSJ. Bruce Fields * So for any acquired lock, the process holding that lock may be 672b533184fSJ. Bruce Fields * waiting on at most one other lock. That lock in turns may be held by 673b533184fSJ. Bruce Fields * someone waiting for at most one other lock. Given a requested lock 674b533184fSJ. Bruce Fields * caller_fl which is about to wait for a conflicting lock block_fl, we 675b533184fSJ. Bruce Fields * follow this chain of waiters to ensure we are not about to create a 676b533184fSJ. Bruce Fields * cycle. 67797855b49SJ. Bruce Fields * 678b533184fSJ. Bruce Fields * Since we do this before we ever put a process to sleep on a lock, we 679b533184fSJ. Bruce Fields * are ensured that there is never a cycle; that is what guarantees that 680b533184fSJ. Bruce Fields * the while() loop in posix_locks_deadlock() eventually completes. 681b533184fSJ. Bruce Fields * 682b533184fSJ. Bruce Fields * Note: the above assumption may not be true when handling lock 683b533184fSJ. Bruce Fields * requests from a broken NFS client. It may also fail in the presence 684b533184fSJ. Bruce Fields * of tasks (such as posix threads) sharing the same open file table. 685b533184fSJ. Bruce Fields * 686b533184fSJ. Bruce Fields * To handle those cases, we just bail out after a few iterations. 6871da177e4SLinus Torvalds */ 68897855b49SJ. Bruce Fields 68997855b49SJ. Bruce Fields #define MAX_DEADLK_ITERATIONS 10 69097855b49SJ. Bruce Fields 691b533184fSJ. Bruce Fields /* Find a lock that the owner of the given block_fl is blocking on. */ 692b533184fSJ. Bruce Fields static struct file_lock *what_owner_is_waiting_for(struct file_lock *block_fl) 693b533184fSJ. Bruce Fields { 694b533184fSJ. Bruce Fields struct file_lock *fl; 695b533184fSJ. Bruce Fields 696b533184fSJ. Bruce Fields list_for_each_entry(fl, &blocked_list, fl_link) { 697b533184fSJ. Bruce Fields if (posix_same_owner(fl, block_fl)) 698b533184fSJ. Bruce Fields return fl->fl_next; 699b533184fSJ. Bruce Fields } 700b533184fSJ. Bruce Fields return NULL; 701b533184fSJ. Bruce Fields } 702b533184fSJ. Bruce Fields 703b0904e14SAdrian Bunk static int posix_locks_deadlock(struct file_lock *caller_fl, 7041da177e4SLinus Torvalds struct file_lock *block_fl) 7051da177e4SLinus Torvalds { 70697855b49SJ. Bruce Fields int i = 0; 7071da177e4SLinus Torvalds 708b533184fSJ. Bruce Fields while ((block_fl = what_owner_is_waiting_for(block_fl))) { 70997855b49SJ. Bruce Fields if (i++ > MAX_DEADLK_ITERATIONS) 71097855b49SJ. Bruce Fields return 0; 711b533184fSJ. Bruce Fields if (posix_same_owner(caller_fl, block_fl)) 712b533184fSJ. Bruce Fields return 1; 7131da177e4SLinus Torvalds } 7141da177e4SLinus Torvalds return 0; 7151da177e4SLinus Torvalds } 7161da177e4SLinus Torvalds 7171da177e4SLinus Torvalds /* Try to create a FLOCK lock on filp. We always insert new FLOCK locks 71802888f41SJ. Bruce Fields * after any leases, but before any posix locks. 719f475ae95STrond Myklebust * 720f475ae95STrond Myklebust * Note that if called with an FL_EXISTS argument, the caller may determine 721f475ae95STrond Myklebust * whether or not a lock was successfully freed by testing the return 722f475ae95STrond Myklebust * value for -ENOENT. 7231da177e4SLinus Torvalds */ 724993dfa87STrond Myklebust static int flock_lock_file(struct file *filp, struct file_lock *request) 7251da177e4SLinus Torvalds { 726993dfa87STrond Myklebust struct file_lock *new_fl = NULL; 7271da177e4SLinus Torvalds struct file_lock **before; 7280f7fc9e4SJosef "Jeff" Sipek struct inode * inode = filp->f_path.dentry->d_inode; 7291da177e4SLinus Torvalds int error = 0; 7301da177e4SLinus Torvalds int found = 0; 7311da177e4SLinus Torvalds 7321da177e4SLinus Torvalds lock_kernel(); 733f07f18ddSTrond Myklebust if (request->fl_flags & FL_ACCESS) 734f07f18ddSTrond Myklebust goto find_conflict; 73584d535adSPavel Emelyanov 73684d535adSPavel Emelyanov if (request->fl_type != F_UNLCK) { 73784d535adSPavel Emelyanov error = -ENOMEM; 73884d535adSPavel Emelyanov new_fl = locks_alloc_lock(); 73984d535adSPavel Emelyanov if (new_fl == NULL) 74084d535adSPavel Emelyanov goto out; 74184d535adSPavel Emelyanov error = 0; 74284d535adSPavel Emelyanov } 74384d535adSPavel Emelyanov 7441da177e4SLinus Torvalds for_each_lock(inode, before) { 7451da177e4SLinus Torvalds struct file_lock *fl = *before; 7461da177e4SLinus Torvalds if (IS_POSIX(fl)) 7471da177e4SLinus Torvalds break; 7481da177e4SLinus Torvalds if (IS_LEASE(fl)) 7491da177e4SLinus Torvalds continue; 7501da177e4SLinus Torvalds if (filp != fl->fl_file) 7511da177e4SLinus Torvalds continue; 752993dfa87STrond Myklebust if (request->fl_type == fl->fl_type) 7531da177e4SLinus Torvalds goto out; 7541da177e4SLinus Torvalds found = 1; 7551da177e4SLinus Torvalds locks_delete_lock(before); 7561da177e4SLinus Torvalds break; 7571da177e4SLinus Torvalds } 7581da177e4SLinus Torvalds 759f475ae95STrond Myklebust if (request->fl_type == F_UNLCK) { 760f475ae95STrond Myklebust if ((request->fl_flags & FL_EXISTS) && !found) 761f475ae95STrond Myklebust error = -ENOENT; 762993dfa87STrond Myklebust goto out; 763f475ae95STrond Myklebust } 7641da177e4SLinus Torvalds 7651da177e4SLinus Torvalds /* 7661da177e4SLinus Torvalds * If a higher-priority process was blocked on the old file lock, 7671da177e4SLinus Torvalds * give it the opportunity to lock the file. 7681da177e4SLinus Torvalds */ 7691da177e4SLinus Torvalds if (found) 770c3921ab7SLinus Torvalds cond_resched_bkl(); 7711da177e4SLinus Torvalds 772f07f18ddSTrond Myklebust find_conflict: 7731da177e4SLinus Torvalds for_each_lock(inode, before) { 7741da177e4SLinus Torvalds struct file_lock *fl = *before; 7751da177e4SLinus Torvalds if (IS_POSIX(fl)) 7761da177e4SLinus Torvalds break; 7771da177e4SLinus Torvalds if (IS_LEASE(fl)) 7781da177e4SLinus Torvalds continue; 779993dfa87STrond Myklebust if (!flock_locks_conflict(request, fl)) 7801da177e4SLinus Torvalds continue; 7811da177e4SLinus Torvalds error = -EAGAIN; 782bde74e4bSMiklos Szeredi if (!(request->fl_flags & FL_SLEEP)) 783bde74e4bSMiklos Szeredi goto out; 784bde74e4bSMiklos Szeredi error = FILE_LOCK_DEFERRED; 785993dfa87STrond Myklebust locks_insert_block(fl, request); 7861da177e4SLinus Torvalds goto out; 7871da177e4SLinus Torvalds } 788f07f18ddSTrond Myklebust if (request->fl_flags & FL_ACCESS) 789f07f18ddSTrond Myklebust goto out; 790993dfa87STrond Myklebust locks_copy_lock(new_fl, request); 7910e2f6db8SPavel Emelyanov locks_insert_lock(before, new_fl); 792993dfa87STrond Myklebust new_fl = NULL; 7939cedc194SKirill Korotaev error = 0; 7941da177e4SLinus Torvalds 7951da177e4SLinus Torvalds out: 7961da177e4SLinus Torvalds unlock_kernel(); 797993dfa87STrond Myklebust if (new_fl) 798993dfa87STrond Myklebust locks_free_lock(new_fl); 7991da177e4SLinus Torvalds return error; 8001da177e4SLinus Torvalds } 8011da177e4SLinus Torvalds 802150b3934SMarc Eshel static int __posix_lock_file(struct inode *inode, struct file_lock *request, struct file_lock *conflock) 8031da177e4SLinus Torvalds { 8041da177e4SLinus Torvalds struct file_lock *fl; 80539005d02SMiklos Szeredi struct file_lock *new_fl = NULL; 80639005d02SMiklos Szeredi struct file_lock *new_fl2 = NULL; 8071da177e4SLinus Torvalds struct file_lock *left = NULL; 8081da177e4SLinus Torvalds struct file_lock *right = NULL; 8091da177e4SLinus Torvalds struct file_lock **before; 8101da177e4SLinus Torvalds int error, added = 0; 8111da177e4SLinus Torvalds 8121da177e4SLinus Torvalds /* 8131da177e4SLinus Torvalds * We may need two file_lock structures for this operation, 8141da177e4SLinus Torvalds * so we get them in advance to avoid races. 81539005d02SMiklos Szeredi * 81639005d02SMiklos Szeredi * In some cases we can be sure, that no new locks will be needed 8171da177e4SLinus Torvalds */ 81839005d02SMiklos Szeredi if (!(request->fl_flags & FL_ACCESS) && 81939005d02SMiklos Szeredi (request->fl_type != F_UNLCK || 82039005d02SMiklos Szeredi request->fl_start != 0 || request->fl_end != OFFSET_MAX)) { 8211da177e4SLinus Torvalds new_fl = locks_alloc_lock(); 8221da177e4SLinus Torvalds new_fl2 = locks_alloc_lock(); 82339005d02SMiklos Szeredi } 8241da177e4SLinus Torvalds 8251da177e4SLinus Torvalds lock_kernel(); 8261da177e4SLinus Torvalds if (request->fl_type != F_UNLCK) { 8271da177e4SLinus Torvalds for_each_lock(inode, before) { 828526985b9SJ. Bruce Fields fl = *before; 8291da177e4SLinus Torvalds if (!IS_POSIX(fl)) 8301da177e4SLinus Torvalds continue; 8311da177e4SLinus Torvalds if (!posix_locks_conflict(request, fl)) 8321da177e4SLinus Torvalds continue; 8335842add2SAndy Adamson if (conflock) 8341a747ee0SJ. Bruce Fields __locks_copy_lock(conflock, fl); 8351da177e4SLinus Torvalds error = -EAGAIN; 8361da177e4SLinus Torvalds if (!(request->fl_flags & FL_SLEEP)) 8371da177e4SLinus Torvalds goto out; 8381da177e4SLinus Torvalds error = -EDEADLK; 8391da177e4SLinus Torvalds if (posix_locks_deadlock(request, fl)) 8401da177e4SLinus Torvalds goto out; 841bde74e4bSMiklos Szeredi error = FILE_LOCK_DEFERRED; 8421da177e4SLinus Torvalds locks_insert_block(fl, request); 8431da177e4SLinus Torvalds goto out; 8441da177e4SLinus Torvalds } 8451da177e4SLinus Torvalds } 8461da177e4SLinus Torvalds 8471da177e4SLinus Torvalds /* If we're just looking for a conflict, we're done. */ 8481da177e4SLinus Torvalds error = 0; 8491da177e4SLinus Torvalds if (request->fl_flags & FL_ACCESS) 8501da177e4SLinus Torvalds goto out; 8511da177e4SLinus Torvalds 8521da177e4SLinus Torvalds /* 8531da177e4SLinus Torvalds * Find the first old lock with the same owner as the new lock. 8541da177e4SLinus Torvalds */ 8551da177e4SLinus Torvalds 8561da177e4SLinus Torvalds before = &inode->i_flock; 8571da177e4SLinus Torvalds 8581da177e4SLinus Torvalds /* First skip locks owned by other processes. */ 8591da177e4SLinus Torvalds while ((fl = *before) && (!IS_POSIX(fl) || 8601da177e4SLinus Torvalds !posix_same_owner(request, fl))) { 8611da177e4SLinus Torvalds before = &fl->fl_next; 8621da177e4SLinus Torvalds } 8631da177e4SLinus Torvalds 8641da177e4SLinus Torvalds /* Process locks with this owner. */ 8651da177e4SLinus Torvalds while ((fl = *before) && posix_same_owner(request, fl)) { 8661da177e4SLinus Torvalds /* Detect adjacent or overlapping regions (if same lock type) 8671da177e4SLinus Torvalds */ 8681da177e4SLinus Torvalds if (request->fl_type == fl->fl_type) { 869449231d6SOlaf Kirch /* In all comparisons of start vs end, use 870449231d6SOlaf Kirch * "start - 1" rather than "end + 1". If end 871449231d6SOlaf Kirch * is OFFSET_MAX, end + 1 will become negative. 872449231d6SOlaf Kirch */ 8731da177e4SLinus Torvalds if (fl->fl_end < request->fl_start - 1) 8741da177e4SLinus Torvalds goto next_lock; 8751da177e4SLinus Torvalds /* If the next lock in the list has entirely bigger 8761da177e4SLinus Torvalds * addresses than the new one, insert the lock here. 8771da177e4SLinus Torvalds */ 878449231d6SOlaf Kirch if (fl->fl_start - 1 > request->fl_end) 8791da177e4SLinus Torvalds break; 8801da177e4SLinus Torvalds 8811da177e4SLinus Torvalds /* If we come here, the new and old lock are of the 8821da177e4SLinus Torvalds * same type and adjacent or overlapping. Make one 8831da177e4SLinus Torvalds * lock yielding from the lower start address of both 8841da177e4SLinus Torvalds * locks to the higher end address. 8851da177e4SLinus Torvalds */ 8861da177e4SLinus Torvalds if (fl->fl_start > request->fl_start) 8871da177e4SLinus Torvalds fl->fl_start = request->fl_start; 8881da177e4SLinus Torvalds else 8891da177e4SLinus Torvalds request->fl_start = fl->fl_start; 8901da177e4SLinus Torvalds if (fl->fl_end < request->fl_end) 8911da177e4SLinus Torvalds fl->fl_end = request->fl_end; 8921da177e4SLinus Torvalds else 8931da177e4SLinus Torvalds request->fl_end = fl->fl_end; 8941da177e4SLinus Torvalds if (added) { 8951da177e4SLinus Torvalds locks_delete_lock(before); 8961da177e4SLinus Torvalds continue; 8971da177e4SLinus Torvalds } 8981da177e4SLinus Torvalds request = fl; 8991da177e4SLinus Torvalds added = 1; 9001da177e4SLinus Torvalds } 9011da177e4SLinus Torvalds else { 9021da177e4SLinus Torvalds /* Processing for different lock types is a bit 9031da177e4SLinus Torvalds * more complex. 9041da177e4SLinus Torvalds */ 9051da177e4SLinus Torvalds if (fl->fl_end < request->fl_start) 9061da177e4SLinus Torvalds goto next_lock; 9071da177e4SLinus Torvalds if (fl->fl_start > request->fl_end) 9081da177e4SLinus Torvalds break; 9091da177e4SLinus Torvalds if (request->fl_type == F_UNLCK) 9101da177e4SLinus Torvalds added = 1; 9111da177e4SLinus Torvalds if (fl->fl_start < request->fl_start) 9121da177e4SLinus Torvalds left = fl; 9131da177e4SLinus Torvalds /* If the next lock in the list has a higher end 9141da177e4SLinus Torvalds * address than the new one, insert the new one here. 9151da177e4SLinus Torvalds */ 9161da177e4SLinus Torvalds if (fl->fl_end > request->fl_end) { 9171da177e4SLinus Torvalds right = fl; 9181da177e4SLinus Torvalds break; 9191da177e4SLinus Torvalds } 9201da177e4SLinus Torvalds if (fl->fl_start >= request->fl_start) { 9211da177e4SLinus Torvalds /* The new lock completely replaces an old 9221da177e4SLinus Torvalds * one (This may happen several times). 9231da177e4SLinus Torvalds */ 9241da177e4SLinus Torvalds if (added) { 9251da177e4SLinus Torvalds locks_delete_lock(before); 9261da177e4SLinus Torvalds continue; 9271da177e4SLinus Torvalds } 9281da177e4SLinus Torvalds /* Replace the old lock with the new one. 9291da177e4SLinus Torvalds * Wake up anybody waiting for the old one, 9301da177e4SLinus Torvalds * as the change in lock type might satisfy 9311da177e4SLinus Torvalds * their needs. 9321da177e4SLinus Torvalds */ 9331da177e4SLinus Torvalds locks_wake_up_blocks(fl); 9341da177e4SLinus Torvalds fl->fl_start = request->fl_start; 9351da177e4SLinus Torvalds fl->fl_end = request->fl_end; 9361da177e4SLinus Torvalds fl->fl_type = request->fl_type; 93747831f35STrond Myklebust locks_release_private(fl); 93847831f35STrond Myklebust locks_copy_private(fl, request); 9391da177e4SLinus Torvalds request = fl; 9401da177e4SLinus Torvalds added = 1; 9411da177e4SLinus Torvalds } 9421da177e4SLinus Torvalds } 9431da177e4SLinus Torvalds /* Go on to next lock. 9441da177e4SLinus Torvalds */ 9451da177e4SLinus Torvalds next_lock: 9461da177e4SLinus Torvalds before = &fl->fl_next; 9471da177e4SLinus Torvalds } 9481da177e4SLinus Torvalds 9490d9a490aSMiklos Szeredi /* 9500d9a490aSMiklos Szeredi * The above code only modifies existing locks in case of 9510d9a490aSMiklos Szeredi * merging or replacing. If new lock(s) need to be inserted 9520d9a490aSMiklos Szeredi * all modifications are done bellow this, so it's safe yet to 9530d9a490aSMiklos Szeredi * bail out. 9540d9a490aSMiklos Szeredi */ 9550d9a490aSMiklos Szeredi error = -ENOLCK; /* "no luck" */ 9560d9a490aSMiklos Szeredi if (right && left == right && !new_fl2) 9570d9a490aSMiklos Szeredi goto out; 9580d9a490aSMiklos Szeredi 9591da177e4SLinus Torvalds error = 0; 9601da177e4SLinus Torvalds if (!added) { 961f475ae95STrond Myklebust if (request->fl_type == F_UNLCK) { 962f475ae95STrond Myklebust if (request->fl_flags & FL_EXISTS) 963f475ae95STrond Myklebust error = -ENOENT; 9641da177e4SLinus Torvalds goto out; 965f475ae95STrond Myklebust } 9660d9a490aSMiklos Szeredi 9670d9a490aSMiklos Szeredi if (!new_fl) { 9680d9a490aSMiklos Szeredi error = -ENOLCK; 9690d9a490aSMiklos Szeredi goto out; 9700d9a490aSMiklos Szeredi } 9711da177e4SLinus Torvalds locks_copy_lock(new_fl, request); 9721da177e4SLinus Torvalds locks_insert_lock(before, new_fl); 9731da177e4SLinus Torvalds new_fl = NULL; 9741da177e4SLinus Torvalds } 9751da177e4SLinus Torvalds if (right) { 9761da177e4SLinus Torvalds if (left == right) { 9771da177e4SLinus Torvalds /* The new lock breaks the old one in two pieces, 9781da177e4SLinus Torvalds * so we have to use the second new lock. 9791da177e4SLinus Torvalds */ 9801da177e4SLinus Torvalds left = new_fl2; 9811da177e4SLinus Torvalds new_fl2 = NULL; 9821da177e4SLinus Torvalds locks_copy_lock(left, right); 9831da177e4SLinus Torvalds locks_insert_lock(before, left); 9841da177e4SLinus Torvalds } 9851da177e4SLinus Torvalds right->fl_start = request->fl_end + 1; 9861da177e4SLinus Torvalds locks_wake_up_blocks(right); 9871da177e4SLinus Torvalds } 9881da177e4SLinus Torvalds if (left) { 9891da177e4SLinus Torvalds left->fl_end = request->fl_start - 1; 9901da177e4SLinus Torvalds locks_wake_up_blocks(left); 9911da177e4SLinus Torvalds } 9921da177e4SLinus Torvalds out: 9931da177e4SLinus Torvalds unlock_kernel(); 9941da177e4SLinus Torvalds /* 9951da177e4SLinus Torvalds * Free any unused locks. 9961da177e4SLinus Torvalds */ 9971da177e4SLinus Torvalds if (new_fl) 9981da177e4SLinus Torvalds locks_free_lock(new_fl); 9991da177e4SLinus Torvalds if (new_fl2) 10001da177e4SLinus Torvalds locks_free_lock(new_fl2); 10011da177e4SLinus Torvalds return error; 10021da177e4SLinus Torvalds } 10031da177e4SLinus Torvalds 10041da177e4SLinus Torvalds /** 10051da177e4SLinus Torvalds * posix_lock_file - Apply a POSIX-style lock to a file 10061da177e4SLinus Torvalds * @filp: The file to apply the lock to 10071da177e4SLinus Torvalds * @fl: The lock to be applied 1008150b3934SMarc Eshel * @conflock: Place to return a copy of the conflicting lock, if found. 10091da177e4SLinus Torvalds * 10101da177e4SLinus Torvalds * Add a POSIX style lock to a file. 10111da177e4SLinus Torvalds * We merge adjacent & overlapping locks whenever possible. 10121da177e4SLinus Torvalds * POSIX locks are sorted by owner task, then by starting address 1013f475ae95STrond Myklebust * 1014f475ae95STrond Myklebust * Note that if called with an FL_EXISTS argument, the caller may determine 1015f475ae95STrond Myklebust * whether or not a lock was successfully freed by testing the return 1016f475ae95STrond Myklebust * value for -ENOENT. 10171da177e4SLinus Torvalds */ 1018150b3934SMarc Eshel int posix_lock_file(struct file *filp, struct file_lock *fl, 10195842add2SAndy Adamson struct file_lock *conflock) 10205842add2SAndy Adamson { 1021150b3934SMarc Eshel return __posix_lock_file(filp->f_path.dentry->d_inode, fl, conflock); 10225842add2SAndy Adamson } 1023150b3934SMarc Eshel EXPORT_SYMBOL(posix_lock_file); 10241da177e4SLinus Torvalds 10251da177e4SLinus Torvalds /** 10261da177e4SLinus Torvalds * posix_lock_file_wait - Apply a POSIX-style lock to a file 10271da177e4SLinus Torvalds * @filp: The file to apply the lock to 10281da177e4SLinus Torvalds * @fl: The lock to be applied 10291da177e4SLinus Torvalds * 10301da177e4SLinus Torvalds * Add a POSIX style lock to a file. 10311da177e4SLinus Torvalds * We merge adjacent & overlapping locks whenever possible. 10321da177e4SLinus Torvalds * POSIX locks are sorted by owner task, then by starting address 10331da177e4SLinus Torvalds */ 10341da177e4SLinus Torvalds int posix_lock_file_wait(struct file *filp, struct file_lock *fl) 10351da177e4SLinus Torvalds { 10361da177e4SLinus Torvalds int error; 10371da177e4SLinus Torvalds might_sleep (); 10381da177e4SLinus Torvalds for (;;) { 1039150b3934SMarc Eshel error = posix_lock_file(filp, fl, NULL); 1040bde74e4bSMiklos Szeredi if (error != FILE_LOCK_DEFERRED) 10411da177e4SLinus Torvalds break; 10421da177e4SLinus Torvalds error = wait_event_interruptible(fl->fl_wait, !fl->fl_next); 10431da177e4SLinus Torvalds if (!error) 10441da177e4SLinus Torvalds continue; 10451da177e4SLinus Torvalds 10461da177e4SLinus Torvalds locks_delete_block(fl); 10471da177e4SLinus Torvalds break; 10481da177e4SLinus Torvalds } 10491da177e4SLinus Torvalds return error; 10501da177e4SLinus Torvalds } 10511da177e4SLinus Torvalds EXPORT_SYMBOL(posix_lock_file_wait); 10521da177e4SLinus Torvalds 10531da177e4SLinus Torvalds /** 10541da177e4SLinus Torvalds * locks_mandatory_locked - Check for an active lock 10551da177e4SLinus Torvalds * @inode: the file to check 10561da177e4SLinus Torvalds * 10571da177e4SLinus Torvalds * Searches the inode's list of locks to find any POSIX locks which conflict. 10581da177e4SLinus Torvalds * This function is called from locks_verify_locked() only. 10591da177e4SLinus Torvalds */ 10601da177e4SLinus Torvalds int locks_mandatory_locked(struct inode *inode) 10611da177e4SLinus Torvalds { 10621da177e4SLinus Torvalds fl_owner_t owner = current->files; 10631da177e4SLinus Torvalds struct file_lock *fl; 10641da177e4SLinus Torvalds 10651da177e4SLinus Torvalds /* 10661da177e4SLinus Torvalds * Search the lock list for this inode for any POSIX locks. 10671da177e4SLinus Torvalds */ 10681da177e4SLinus Torvalds lock_kernel(); 10691da177e4SLinus Torvalds for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) { 10701da177e4SLinus Torvalds if (!IS_POSIX(fl)) 10711da177e4SLinus Torvalds continue; 10721da177e4SLinus Torvalds if (fl->fl_owner != owner) 10731da177e4SLinus Torvalds break; 10741da177e4SLinus Torvalds } 10751da177e4SLinus Torvalds unlock_kernel(); 10761da177e4SLinus Torvalds return fl ? -EAGAIN : 0; 10771da177e4SLinus Torvalds } 10781da177e4SLinus Torvalds 10791da177e4SLinus Torvalds /** 10801da177e4SLinus Torvalds * locks_mandatory_area - Check for a conflicting lock 10811da177e4SLinus Torvalds * @read_write: %FLOCK_VERIFY_WRITE for exclusive access, %FLOCK_VERIFY_READ 10821da177e4SLinus Torvalds * for shared 10831da177e4SLinus Torvalds * @inode: the file to check 10841da177e4SLinus Torvalds * @filp: how the file was opened (if it was) 10851da177e4SLinus Torvalds * @offset: start of area to check 10861da177e4SLinus Torvalds * @count: length of area to check 10871da177e4SLinus Torvalds * 10881da177e4SLinus Torvalds * Searches the inode's list of locks to find any POSIX locks which conflict. 10891da177e4SLinus Torvalds * This function is called from rw_verify_area() and 10901da177e4SLinus Torvalds * locks_verify_truncate(). 10911da177e4SLinus Torvalds */ 10921da177e4SLinus Torvalds int locks_mandatory_area(int read_write, struct inode *inode, 10931da177e4SLinus Torvalds struct file *filp, loff_t offset, 10941da177e4SLinus Torvalds size_t count) 10951da177e4SLinus Torvalds { 10961da177e4SLinus Torvalds struct file_lock fl; 10971da177e4SLinus Torvalds int error; 10981da177e4SLinus Torvalds 10991da177e4SLinus Torvalds locks_init_lock(&fl); 11001da177e4SLinus Torvalds fl.fl_owner = current->files; 11011da177e4SLinus Torvalds fl.fl_pid = current->tgid; 11021da177e4SLinus Torvalds fl.fl_file = filp; 11031da177e4SLinus Torvalds fl.fl_flags = FL_POSIX | FL_ACCESS; 11041da177e4SLinus Torvalds if (filp && !(filp->f_flags & O_NONBLOCK)) 11051da177e4SLinus Torvalds fl.fl_flags |= FL_SLEEP; 11061da177e4SLinus Torvalds fl.fl_type = (read_write == FLOCK_VERIFY_WRITE) ? F_WRLCK : F_RDLCK; 11071da177e4SLinus Torvalds fl.fl_start = offset; 11081da177e4SLinus Torvalds fl.fl_end = offset + count - 1; 11091da177e4SLinus Torvalds 11101da177e4SLinus Torvalds for (;;) { 1111150b3934SMarc Eshel error = __posix_lock_file(inode, &fl, NULL); 1112bde74e4bSMiklos Szeredi if (error != FILE_LOCK_DEFERRED) 11131da177e4SLinus Torvalds break; 11141da177e4SLinus Torvalds error = wait_event_interruptible(fl.fl_wait, !fl.fl_next); 11151da177e4SLinus Torvalds if (!error) { 11161da177e4SLinus Torvalds /* 11171da177e4SLinus Torvalds * If we've been sleeping someone might have 11181da177e4SLinus Torvalds * changed the permissions behind our back. 11191da177e4SLinus Torvalds */ 1120a16877caSPavel Emelyanov if (__mandatory_lock(inode)) 11211da177e4SLinus Torvalds continue; 11221da177e4SLinus Torvalds } 11231da177e4SLinus Torvalds 11241da177e4SLinus Torvalds locks_delete_block(&fl); 11251da177e4SLinus Torvalds break; 11261da177e4SLinus Torvalds } 11271da177e4SLinus Torvalds 11281da177e4SLinus Torvalds return error; 11291da177e4SLinus Torvalds } 11301da177e4SLinus Torvalds 11311da177e4SLinus Torvalds EXPORT_SYMBOL(locks_mandatory_area); 11321da177e4SLinus Torvalds 11331da177e4SLinus Torvalds /* We already had a lease on this file; just change its type */ 11341da177e4SLinus Torvalds int lease_modify(struct file_lock **before, int arg) 11351da177e4SLinus Torvalds { 11361da177e4SLinus Torvalds struct file_lock *fl = *before; 11371da177e4SLinus Torvalds int error = assign_type(fl, arg); 11381da177e4SLinus Torvalds 11391da177e4SLinus Torvalds if (error) 11401da177e4SLinus Torvalds return error; 11411da177e4SLinus Torvalds locks_wake_up_blocks(fl); 11421da177e4SLinus Torvalds if (arg == F_UNLCK) 11431da177e4SLinus Torvalds locks_delete_lock(before); 11441da177e4SLinus Torvalds return 0; 11451da177e4SLinus Torvalds } 11461da177e4SLinus Torvalds 11471da177e4SLinus Torvalds EXPORT_SYMBOL(lease_modify); 11481da177e4SLinus Torvalds 11491da177e4SLinus Torvalds static void time_out_leases(struct inode *inode) 11501da177e4SLinus Torvalds { 11511da177e4SLinus Torvalds struct file_lock **before; 11521da177e4SLinus Torvalds struct file_lock *fl; 11531da177e4SLinus Torvalds 11541da177e4SLinus Torvalds before = &inode->i_flock; 11551da177e4SLinus Torvalds while ((fl = *before) && IS_LEASE(fl) && (fl->fl_type & F_INPROGRESS)) { 11561da177e4SLinus Torvalds if ((fl->fl_break_time == 0) 11571da177e4SLinus Torvalds || time_before(jiffies, fl->fl_break_time)) { 11581da177e4SLinus Torvalds before = &fl->fl_next; 11591da177e4SLinus Torvalds continue; 11601da177e4SLinus Torvalds } 11611da177e4SLinus Torvalds lease_modify(before, fl->fl_type & ~F_INPROGRESS); 11621da177e4SLinus Torvalds if (fl == *before) /* lease_modify may have freed fl */ 11631da177e4SLinus Torvalds before = &fl->fl_next; 11641da177e4SLinus Torvalds } 11651da177e4SLinus Torvalds } 11661da177e4SLinus Torvalds 11671da177e4SLinus Torvalds /** 11681da177e4SLinus Torvalds * __break_lease - revoke all outstanding leases on file 11691da177e4SLinus Torvalds * @inode: the inode of the file to return 11701da177e4SLinus Torvalds * @mode: the open mode (read or write) 11711da177e4SLinus Torvalds * 117287250dd2Sdavid m. richter * break_lease (inlined for speed) has checked there already is at least 117387250dd2Sdavid m. richter * some kind of lock (maybe a lease) on this file. Leases are broken on 117487250dd2Sdavid m. richter * a call to open() or truncate(). This function can sleep unless you 11751da177e4SLinus Torvalds * specified %O_NONBLOCK to your open(). 11761da177e4SLinus Torvalds */ 11771da177e4SLinus Torvalds int __break_lease(struct inode *inode, unsigned int mode) 11781da177e4SLinus Torvalds { 11791da177e4SLinus Torvalds int error = 0, future; 11801da177e4SLinus Torvalds struct file_lock *new_fl, *flock; 11811da177e4SLinus Torvalds struct file_lock *fl; 11821da177e4SLinus Torvalds unsigned long break_time; 11831da177e4SLinus Torvalds int i_have_this_lease = 0; 11841da177e4SLinus Torvalds 1185e32b8ee2SJ. Bruce Fields new_fl = lease_alloc(NULL, mode & FMODE_WRITE ? F_WRLCK : F_RDLCK); 11861da177e4SLinus Torvalds 11871da177e4SLinus Torvalds lock_kernel(); 11881da177e4SLinus Torvalds 11891da177e4SLinus Torvalds time_out_leases(inode); 11901da177e4SLinus Torvalds 11911da177e4SLinus Torvalds flock = inode->i_flock; 11921da177e4SLinus Torvalds if ((flock == NULL) || !IS_LEASE(flock)) 11931da177e4SLinus Torvalds goto out; 11941da177e4SLinus Torvalds 11951da177e4SLinus Torvalds for (fl = flock; fl && IS_LEASE(fl); fl = fl->fl_next) 11961da177e4SLinus Torvalds if (fl->fl_owner == current->files) 11971da177e4SLinus Torvalds i_have_this_lease = 1; 11981da177e4SLinus Torvalds 11991da177e4SLinus Torvalds if (mode & FMODE_WRITE) { 12001da177e4SLinus Torvalds /* If we want write access, we have to revoke any lease. */ 12011da177e4SLinus Torvalds future = F_UNLCK | F_INPROGRESS; 12021da177e4SLinus Torvalds } else if (flock->fl_type & F_INPROGRESS) { 12031da177e4SLinus Torvalds /* If the lease is already being broken, we just leave it */ 12041da177e4SLinus Torvalds future = flock->fl_type; 12051da177e4SLinus Torvalds } else if (flock->fl_type & F_WRLCK) { 12061da177e4SLinus Torvalds /* Downgrade the exclusive lease to a read-only lease. */ 12071da177e4SLinus Torvalds future = F_RDLCK | F_INPROGRESS; 12081da177e4SLinus Torvalds } else { 12091da177e4SLinus Torvalds /* the existing lease was read-only, so we can read too. */ 12101da177e4SLinus Torvalds goto out; 12111da177e4SLinus Torvalds } 12121da177e4SLinus Torvalds 1213e32b8ee2SJ. Bruce Fields if (IS_ERR(new_fl) && !i_have_this_lease 1214e32b8ee2SJ. Bruce Fields && ((mode & O_NONBLOCK) == 0)) { 1215e32b8ee2SJ. Bruce Fields error = PTR_ERR(new_fl); 12161da177e4SLinus Torvalds goto out; 12171da177e4SLinus Torvalds } 12181da177e4SLinus Torvalds 12191da177e4SLinus Torvalds break_time = 0; 12201da177e4SLinus Torvalds if (lease_break_time > 0) { 12211da177e4SLinus Torvalds break_time = jiffies + lease_break_time * HZ; 12221da177e4SLinus Torvalds if (break_time == 0) 12231da177e4SLinus Torvalds break_time++; /* so that 0 means no break time */ 12241da177e4SLinus Torvalds } 12251da177e4SLinus Torvalds 12261da177e4SLinus Torvalds for (fl = flock; fl && IS_LEASE(fl); fl = fl->fl_next) { 12271da177e4SLinus Torvalds if (fl->fl_type != future) { 12281da177e4SLinus Torvalds fl->fl_type = future; 12291da177e4SLinus Torvalds fl->fl_break_time = break_time; 12301da177e4SLinus Torvalds /* lease must have lmops break callback */ 12311da177e4SLinus Torvalds fl->fl_lmops->fl_break(fl); 12321da177e4SLinus Torvalds } 12331da177e4SLinus Torvalds } 12341da177e4SLinus Torvalds 12351da177e4SLinus Torvalds if (i_have_this_lease || (mode & O_NONBLOCK)) { 12361da177e4SLinus Torvalds error = -EWOULDBLOCK; 12371da177e4SLinus Torvalds goto out; 12381da177e4SLinus Torvalds } 12391da177e4SLinus Torvalds 12401da177e4SLinus Torvalds restart: 12411da177e4SLinus Torvalds break_time = flock->fl_break_time; 12421da177e4SLinus Torvalds if (break_time != 0) { 12431da177e4SLinus Torvalds break_time -= jiffies; 12441da177e4SLinus Torvalds if (break_time == 0) 12451da177e4SLinus Torvalds break_time++; 12461da177e4SLinus Torvalds } 12474321e01eSMatthew Wilcox locks_insert_block(flock, new_fl); 12484321e01eSMatthew Wilcox error = wait_event_interruptible_timeout(new_fl->fl_wait, 12494321e01eSMatthew Wilcox !new_fl->fl_next, break_time); 12504321e01eSMatthew Wilcox __locks_delete_block(new_fl); 12511da177e4SLinus Torvalds if (error >= 0) { 12521da177e4SLinus Torvalds if (error == 0) 12531da177e4SLinus Torvalds time_out_leases(inode); 12541da177e4SLinus Torvalds /* Wait for the next lease that has not been broken yet */ 12551da177e4SLinus Torvalds for (flock = inode->i_flock; flock && IS_LEASE(flock); 12561da177e4SLinus Torvalds flock = flock->fl_next) { 12571da177e4SLinus Torvalds if (flock->fl_type & F_INPROGRESS) 12581da177e4SLinus Torvalds goto restart; 12591da177e4SLinus Torvalds } 12601da177e4SLinus Torvalds error = 0; 12611da177e4SLinus Torvalds } 12621da177e4SLinus Torvalds 12631da177e4SLinus Torvalds out: 12641da177e4SLinus Torvalds unlock_kernel(); 1265e32b8ee2SJ. Bruce Fields if (!IS_ERR(new_fl)) 12661da177e4SLinus Torvalds locks_free_lock(new_fl); 12671da177e4SLinus Torvalds return error; 12681da177e4SLinus Torvalds } 12691da177e4SLinus Torvalds 12701da177e4SLinus Torvalds EXPORT_SYMBOL(__break_lease); 12711da177e4SLinus Torvalds 12721da177e4SLinus Torvalds /** 1273a6b91919SRandy Dunlap * lease_get_mtime - get the last modified time of an inode 12741da177e4SLinus Torvalds * @inode: the inode 12751da177e4SLinus Torvalds * @time: pointer to a timespec which will contain the last modified time 12761da177e4SLinus Torvalds * 12771da177e4SLinus Torvalds * This is to force NFS clients to flush their caches for files with 12781da177e4SLinus Torvalds * exclusive leases. The justification is that if someone has an 1279a6b91919SRandy Dunlap * exclusive lease, then they could be modifying it. 12801da177e4SLinus Torvalds */ 12811da177e4SLinus Torvalds void lease_get_mtime(struct inode *inode, struct timespec *time) 12821da177e4SLinus Torvalds { 12831da177e4SLinus Torvalds struct file_lock *flock = inode->i_flock; 12841da177e4SLinus Torvalds if (flock && IS_LEASE(flock) && (flock->fl_type & F_WRLCK)) 12851da177e4SLinus Torvalds *time = current_fs_time(inode->i_sb); 12861da177e4SLinus Torvalds else 12871da177e4SLinus Torvalds *time = inode->i_mtime; 12881da177e4SLinus Torvalds } 12891da177e4SLinus Torvalds 12901da177e4SLinus Torvalds EXPORT_SYMBOL(lease_get_mtime); 12911da177e4SLinus Torvalds 12921da177e4SLinus Torvalds /** 12931da177e4SLinus Torvalds * fcntl_getlease - Enquire what lease is currently active 12941da177e4SLinus Torvalds * @filp: the file 12951da177e4SLinus Torvalds * 12961da177e4SLinus Torvalds * The value returned by this function will be one of 12971da177e4SLinus Torvalds * (if no lease break is pending): 12981da177e4SLinus Torvalds * 12991da177e4SLinus Torvalds * %F_RDLCK to indicate a shared lease is held. 13001da177e4SLinus Torvalds * 13011da177e4SLinus Torvalds * %F_WRLCK to indicate an exclusive lease is held. 13021da177e4SLinus Torvalds * 13031da177e4SLinus Torvalds * %F_UNLCK to indicate no lease is held. 13041da177e4SLinus Torvalds * 13051da177e4SLinus Torvalds * (if a lease break is pending): 13061da177e4SLinus Torvalds * 13071da177e4SLinus Torvalds * %F_RDLCK to indicate an exclusive lease needs to be 13081da177e4SLinus Torvalds * changed to a shared lease (or removed). 13091da177e4SLinus Torvalds * 13101da177e4SLinus Torvalds * %F_UNLCK to indicate the lease needs to be removed. 13111da177e4SLinus Torvalds * 13121da177e4SLinus Torvalds * XXX: sfr & willy disagree over whether F_INPROGRESS 13131da177e4SLinus Torvalds * should be returned to userspace. 13141da177e4SLinus Torvalds */ 13151da177e4SLinus Torvalds int fcntl_getlease(struct file *filp) 13161da177e4SLinus Torvalds { 13171da177e4SLinus Torvalds struct file_lock *fl; 13181da177e4SLinus Torvalds int type = F_UNLCK; 13191da177e4SLinus Torvalds 13201da177e4SLinus Torvalds lock_kernel(); 13210f7fc9e4SJosef "Jeff" Sipek time_out_leases(filp->f_path.dentry->d_inode); 13220f7fc9e4SJosef "Jeff" Sipek for (fl = filp->f_path.dentry->d_inode->i_flock; fl && IS_LEASE(fl); 13231da177e4SLinus Torvalds fl = fl->fl_next) { 13241da177e4SLinus Torvalds if (fl->fl_file == filp) { 13251da177e4SLinus Torvalds type = fl->fl_type & ~F_INPROGRESS; 13261da177e4SLinus Torvalds break; 13271da177e4SLinus Torvalds } 13281da177e4SLinus Torvalds } 13291da177e4SLinus Torvalds unlock_kernel(); 13301da177e4SLinus Torvalds return type; 13311da177e4SLinus Torvalds } 13321da177e4SLinus Torvalds 13331da177e4SLinus Torvalds /** 13340af1a450SChristoph Hellwig * generic_setlease - sets a lease on an open file 13351da177e4SLinus Torvalds * @filp: file pointer 13361da177e4SLinus Torvalds * @arg: type of lease to obtain 13371da177e4SLinus Torvalds * @flp: input - file_lock to use, output - file_lock inserted 13381da177e4SLinus Torvalds * 13391da177e4SLinus Torvalds * The (input) flp->fl_lmops->fl_break function is required 13401da177e4SLinus Torvalds * by break_lease(). 13411da177e4SLinus Torvalds * 13421da177e4SLinus Torvalds * Called with kernel lock held. 13431da177e4SLinus Torvalds */ 13440af1a450SChristoph Hellwig int generic_setlease(struct file *filp, long arg, struct file_lock **flp) 13451da177e4SLinus Torvalds { 13467eaae282SKAMBAROV, ZAUR struct file_lock *fl, **before, **my_before = NULL, *lease; 134785c59580SPavel Emelyanov struct file_lock *new_fl = NULL; 13480f7fc9e4SJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 13491da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 13501da177e4SLinus Torvalds int error, rdlease_count = 0, wrlease_count = 0; 13511da177e4SLinus Torvalds 1352*da9592edSDavid Howells if ((current_fsuid() != inode->i_uid) && !capable(CAP_LEASE)) 13536d5e8b05SJ. Bruce Fields return -EACCES; 13546d5e8b05SJ. Bruce Fields if (!S_ISREG(inode->i_mode)) 13556d5e8b05SJ. Bruce Fields return -EINVAL; 13566d5e8b05SJ. Bruce Fields error = security_file_lock(filp, arg); 13576d5e8b05SJ. Bruce Fields if (error) 13586d5e8b05SJ. Bruce Fields return error; 13596d5e8b05SJ. Bruce Fields 13601da177e4SLinus Torvalds time_out_leases(inode); 13611da177e4SLinus Torvalds 1362d2ab0b0cSJ. Bruce Fields BUG_ON(!(*flp)->fl_lmops->fl_break); 13631da177e4SLinus Torvalds 13647eaae282SKAMBAROV, ZAUR lease = *flp; 13657eaae282SKAMBAROV, ZAUR 1366288b2fd8SDavid M. Richter if (arg != F_UNLCK) { 136719085557SDavid M. Richter error = -ENOMEM; 136819085557SDavid M. Richter new_fl = locks_alloc_lock(); 136919085557SDavid M. Richter if (new_fl == NULL) 137019085557SDavid M. Richter goto out; 137119085557SDavid M. Richter 13721da177e4SLinus Torvalds error = -EAGAIN; 13731da177e4SLinus Torvalds if ((arg == F_RDLCK) && (atomic_read(&inode->i_writecount) > 0)) 13741da177e4SLinus Torvalds goto out; 13751da177e4SLinus Torvalds if ((arg == F_WRLCK) 13761da177e4SLinus Torvalds && ((atomic_read(&dentry->d_count) > 1) 13771da177e4SLinus Torvalds || (atomic_read(&inode->i_count) > 1))) 13781da177e4SLinus Torvalds goto out; 1379288b2fd8SDavid M. Richter } 138085c59580SPavel Emelyanov 13811da177e4SLinus Torvalds /* 13821da177e4SLinus Torvalds * At this point, we know that if there is an exclusive 13831da177e4SLinus Torvalds * lease on this file, then we hold it on this filp 13841da177e4SLinus Torvalds * (otherwise our open of this file would have blocked). 13851da177e4SLinus Torvalds * And if we are trying to acquire an exclusive lease, 13861da177e4SLinus Torvalds * then the file is not open by anyone (including us) 13871da177e4SLinus Torvalds * except for this filp. 13881da177e4SLinus Torvalds */ 13891da177e4SLinus Torvalds for (before = &inode->i_flock; 13901da177e4SLinus Torvalds ((fl = *before) != NULL) && IS_LEASE(fl); 13911da177e4SLinus Torvalds before = &fl->fl_next) { 13921da177e4SLinus Torvalds if (lease->fl_lmops->fl_mylease(fl, lease)) 13931da177e4SLinus Torvalds my_before = before; 13941da177e4SLinus Torvalds else if (fl->fl_type == (F_INPROGRESS | F_UNLCK)) 13951da177e4SLinus Torvalds /* 13961da177e4SLinus Torvalds * Someone is in the process of opening this 13971da177e4SLinus Torvalds * file for writing so we may not take an 13981da177e4SLinus Torvalds * exclusive lease on it. 13991da177e4SLinus Torvalds */ 14001da177e4SLinus Torvalds wrlease_count++; 14011da177e4SLinus Torvalds else 14021da177e4SLinus Torvalds rdlease_count++; 14031da177e4SLinus Torvalds } 14041da177e4SLinus Torvalds 14055fcc60c3SDavid M. Richter error = -EAGAIN; 14061da177e4SLinus Torvalds if ((arg == F_RDLCK && (wrlease_count > 0)) || 14071da177e4SLinus Torvalds (arg == F_WRLCK && ((rdlease_count + wrlease_count) > 0))) 14081da177e4SLinus Torvalds goto out; 14091da177e4SLinus Torvalds 14101da177e4SLinus Torvalds if (my_before != NULL) { 141175dff55aSTrond Myklebust *flp = *my_before; 14121da177e4SLinus Torvalds error = lease->fl_lmops->fl_change(my_before, arg); 14131da177e4SLinus Torvalds goto out; 14141da177e4SLinus Torvalds } 14151da177e4SLinus Torvalds 14161da177e4SLinus Torvalds error = 0; 14171da177e4SLinus Torvalds if (arg == F_UNLCK) 14181da177e4SLinus Torvalds goto out; 14191da177e4SLinus Torvalds 14201da177e4SLinus Torvalds error = -EINVAL; 14211da177e4SLinus Torvalds if (!leases_enable) 14221da177e4SLinus Torvalds goto out; 14231da177e4SLinus Torvalds 142485c59580SPavel Emelyanov locks_copy_lock(new_fl, lease); 142585c59580SPavel Emelyanov locks_insert_lock(before, new_fl); 14261da177e4SLinus Torvalds 142785c59580SPavel Emelyanov *flp = new_fl; 142885c59580SPavel Emelyanov return 0; 14291da177e4SLinus Torvalds 14301da177e4SLinus Torvalds out: 143185c59580SPavel Emelyanov if (new_fl != NULL) 143285c59580SPavel Emelyanov locks_free_lock(new_fl); 14331da177e4SLinus Torvalds return error; 14341da177e4SLinus Torvalds } 14350af1a450SChristoph Hellwig EXPORT_SYMBOL(generic_setlease); 14361da177e4SLinus Torvalds 14371da177e4SLinus Torvalds /** 1438a9933ceaSJ. Bruce Fields * vfs_setlease - sets a lease on an open file 14391da177e4SLinus Torvalds * @filp: file pointer 14401da177e4SLinus Torvalds * @arg: type of lease to obtain 14411da177e4SLinus Torvalds * @lease: file_lock to use 14421da177e4SLinus Torvalds * 14431da177e4SLinus Torvalds * Call this to establish a lease on the file. 1444f9ffed26SJ. Bruce Fields * The (*lease)->fl_lmops->fl_break operation must be set; if not, 1445f9ffed26SJ. Bruce Fields * break_lease will oops! 1446f9ffed26SJ. Bruce Fields * 1447f9ffed26SJ. Bruce Fields * This will call the filesystem's setlease file method, if 1448f9ffed26SJ. Bruce Fields * defined. Note that there is no getlease method; instead, the 1449f9ffed26SJ. Bruce Fields * filesystem setlease method should call back to setlease() to 1450f9ffed26SJ. Bruce Fields * add a lease to the inode's lease list, where fcntl_getlease() can 1451f9ffed26SJ. Bruce Fields * find it. Since fcntl_getlease() only reports whether the current 1452f9ffed26SJ. Bruce Fields * task holds a lease, a cluster filesystem need only do this for 1453f9ffed26SJ. Bruce Fields * leases held by processes on this node. 1454f9ffed26SJ. Bruce Fields * 1455f9ffed26SJ. Bruce Fields * There is also no break_lease method; filesystems that 1456f9ffed26SJ. Bruce Fields * handle their own leases shoud break leases themselves from the 1457f9ffed26SJ. Bruce Fields * filesystem's open, create, and (on truncate) setattr methods. 1458f9ffed26SJ. Bruce Fields * 1459f9ffed26SJ. Bruce Fields * Warning: the only current setlease methods exist only to disable 1460f9ffed26SJ. Bruce Fields * leases in certain cases. More vfs changes may be required to 1461f9ffed26SJ. Bruce Fields * allow a full filesystem lease implementation. 14621da177e4SLinus Torvalds */ 14631da177e4SLinus Torvalds 1464a9933ceaSJ. Bruce Fields int vfs_setlease(struct file *filp, long arg, struct file_lock **lease) 14651da177e4SLinus Torvalds { 14661da177e4SLinus Torvalds int error; 14671da177e4SLinus Torvalds 14681da177e4SLinus Torvalds lock_kernel(); 1469f9ffed26SJ. Bruce Fields if (filp->f_op && filp->f_op->setlease) 1470f9ffed26SJ. Bruce Fields error = filp->f_op->setlease(filp, arg, lease); 1471f9ffed26SJ. Bruce Fields else 14720af1a450SChristoph Hellwig error = generic_setlease(filp, arg, lease); 14731da177e4SLinus Torvalds unlock_kernel(); 14741da177e4SLinus Torvalds 14751da177e4SLinus Torvalds return error; 14761da177e4SLinus Torvalds } 1477a9933ceaSJ. Bruce Fields EXPORT_SYMBOL_GPL(vfs_setlease); 14781da177e4SLinus Torvalds 14791da177e4SLinus Torvalds /** 14801da177e4SLinus Torvalds * fcntl_setlease - sets a lease on an open file 14811da177e4SLinus Torvalds * @fd: open file descriptor 14821da177e4SLinus Torvalds * @filp: file pointer 14831da177e4SLinus Torvalds * @arg: type of lease to obtain 14841da177e4SLinus Torvalds * 14851da177e4SLinus Torvalds * Call this fcntl to establish a lease on the file. 14861da177e4SLinus Torvalds * Note that you also need to call %F_SETSIG to 14871da177e4SLinus Torvalds * receive a signal when the lease is broken. 14881da177e4SLinus Torvalds */ 14891da177e4SLinus Torvalds int fcntl_setlease(unsigned int fd, struct file *filp, long arg) 14901da177e4SLinus Torvalds { 14911da177e4SLinus Torvalds struct file_lock fl, *flp = &fl; 14929d91cdccSDavid M. Richter struct inode *inode = filp->f_path.dentry->d_inode; 14931da177e4SLinus Torvalds int error; 14941da177e4SLinus Torvalds 14951da177e4SLinus Torvalds locks_init_lock(&fl); 14961da177e4SLinus Torvalds error = lease_init(filp, arg, &fl); 14971da177e4SLinus Torvalds if (error) 14981da177e4SLinus Torvalds return error; 14991da177e4SLinus Torvalds 15001da177e4SLinus Torvalds lock_kernel(); 15011da177e4SLinus Torvalds 1502a9933ceaSJ. Bruce Fields error = vfs_setlease(filp, arg, &flp); 1503dc15ae14SJ. Bruce Fields if (error || arg == F_UNLCK) 15041da177e4SLinus Torvalds goto out_unlock; 15051da177e4SLinus Torvalds 15061da177e4SLinus Torvalds error = fasync_helper(fd, filp, 1, &flp->fl_fasync); 15071da177e4SLinus Torvalds if (error < 0) { 15086d5e8b05SJ. Bruce Fields /* remove lease just inserted by setlease */ 15091da177e4SLinus Torvalds flp->fl_type = F_UNLCK | F_INPROGRESS; 15101da177e4SLinus Torvalds flp->fl_break_time = jiffies - 10; 15111da177e4SLinus Torvalds time_out_leases(inode); 15121da177e4SLinus Torvalds goto out_unlock; 15131da177e4SLinus Torvalds } 15141da177e4SLinus Torvalds 1515609d7fa9SEric W. Biederman error = __f_setown(filp, task_pid(current), PIDTYPE_PID, 0); 15161da177e4SLinus Torvalds out_unlock: 15171da177e4SLinus Torvalds unlock_kernel(); 15181da177e4SLinus Torvalds return error; 15191da177e4SLinus Torvalds } 15201da177e4SLinus Torvalds 15211da177e4SLinus Torvalds /** 15221da177e4SLinus Torvalds * flock_lock_file_wait - Apply a FLOCK-style lock to a file 15231da177e4SLinus Torvalds * @filp: The file to apply the lock to 15241da177e4SLinus Torvalds * @fl: The lock to be applied 15251da177e4SLinus Torvalds * 15261da177e4SLinus Torvalds * Add a FLOCK style lock to a file. 15271da177e4SLinus Torvalds */ 15281da177e4SLinus Torvalds int flock_lock_file_wait(struct file *filp, struct file_lock *fl) 15291da177e4SLinus Torvalds { 15301da177e4SLinus Torvalds int error; 15311da177e4SLinus Torvalds might_sleep(); 15321da177e4SLinus Torvalds for (;;) { 15331da177e4SLinus Torvalds error = flock_lock_file(filp, fl); 1534bde74e4bSMiklos Szeredi if (error != FILE_LOCK_DEFERRED) 15351da177e4SLinus Torvalds break; 15361da177e4SLinus Torvalds error = wait_event_interruptible(fl->fl_wait, !fl->fl_next); 15371da177e4SLinus Torvalds if (!error) 15381da177e4SLinus Torvalds continue; 15391da177e4SLinus Torvalds 15401da177e4SLinus Torvalds locks_delete_block(fl); 15411da177e4SLinus Torvalds break; 15421da177e4SLinus Torvalds } 15431da177e4SLinus Torvalds return error; 15441da177e4SLinus Torvalds } 15451da177e4SLinus Torvalds 15461da177e4SLinus Torvalds EXPORT_SYMBOL(flock_lock_file_wait); 15471da177e4SLinus Torvalds 15481da177e4SLinus Torvalds /** 15491da177e4SLinus Torvalds * sys_flock: - flock() system call. 15501da177e4SLinus Torvalds * @fd: the file descriptor to lock. 15511da177e4SLinus Torvalds * @cmd: the type of lock to apply. 15521da177e4SLinus Torvalds * 15531da177e4SLinus Torvalds * Apply a %FL_FLOCK style lock to an open file descriptor. 15541da177e4SLinus Torvalds * The @cmd can be one of 15551da177e4SLinus Torvalds * 15561da177e4SLinus Torvalds * %LOCK_SH -- a shared lock. 15571da177e4SLinus Torvalds * 15581da177e4SLinus Torvalds * %LOCK_EX -- an exclusive lock. 15591da177e4SLinus Torvalds * 15601da177e4SLinus Torvalds * %LOCK_UN -- remove an existing lock. 15611da177e4SLinus Torvalds * 15621da177e4SLinus Torvalds * %LOCK_MAND -- a `mandatory' flock. This exists to emulate Windows Share Modes. 15631da177e4SLinus Torvalds * 15641da177e4SLinus Torvalds * %LOCK_MAND can be combined with %LOCK_READ or %LOCK_WRITE to allow other 15651da177e4SLinus Torvalds * processes read and write access respectively. 15661da177e4SLinus Torvalds */ 15671da177e4SLinus Torvalds asmlinkage long sys_flock(unsigned int fd, unsigned int cmd) 15681da177e4SLinus Torvalds { 15691da177e4SLinus Torvalds struct file *filp; 15701da177e4SLinus Torvalds struct file_lock *lock; 15711da177e4SLinus Torvalds int can_sleep, unlock; 15721da177e4SLinus Torvalds int error; 15731da177e4SLinus Torvalds 15741da177e4SLinus Torvalds error = -EBADF; 15751da177e4SLinus Torvalds filp = fget(fd); 15761da177e4SLinus Torvalds if (!filp) 15771da177e4SLinus Torvalds goto out; 15781da177e4SLinus Torvalds 15791da177e4SLinus Torvalds can_sleep = !(cmd & LOCK_NB); 15801da177e4SLinus Torvalds cmd &= ~LOCK_NB; 15811da177e4SLinus Torvalds unlock = (cmd == LOCK_UN); 15821da177e4SLinus Torvalds 1583aeb5d727SAl Viro if (!unlock && !(cmd & LOCK_MAND) && 1584aeb5d727SAl Viro !(filp->f_mode & (FMODE_READ|FMODE_WRITE))) 15851da177e4SLinus Torvalds goto out_putf; 15861da177e4SLinus Torvalds 15871da177e4SLinus Torvalds error = flock_make_lock(filp, &lock, cmd); 15881da177e4SLinus Torvalds if (error) 15891da177e4SLinus Torvalds goto out_putf; 15901da177e4SLinus Torvalds if (can_sleep) 15911da177e4SLinus Torvalds lock->fl_flags |= FL_SLEEP; 15921da177e4SLinus Torvalds 15931da177e4SLinus Torvalds error = security_file_lock(filp, cmd); 15941da177e4SLinus Torvalds if (error) 15951da177e4SLinus Torvalds goto out_free; 15961da177e4SLinus Torvalds 15971da177e4SLinus Torvalds if (filp->f_op && filp->f_op->flock) 15981da177e4SLinus Torvalds error = filp->f_op->flock(filp, 15991da177e4SLinus Torvalds (can_sleep) ? F_SETLKW : F_SETLK, 16001da177e4SLinus Torvalds lock); 16011da177e4SLinus Torvalds else 16021da177e4SLinus Torvalds error = flock_lock_file_wait(filp, lock); 16031da177e4SLinus Torvalds 16041da177e4SLinus Torvalds out_free: 16051da177e4SLinus Torvalds locks_free_lock(lock); 16061da177e4SLinus Torvalds 16071da177e4SLinus Torvalds out_putf: 16081da177e4SLinus Torvalds fput(filp); 16091da177e4SLinus Torvalds out: 16101da177e4SLinus Torvalds return error; 16111da177e4SLinus Torvalds } 16121da177e4SLinus Torvalds 16133ee17abdSJ. Bruce Fields /** 16143ee17abdSJ. Bruce Fields * vfs_test_lock - test file byte range lock 16153ee17abdSJ. Bruce Fields * @filp: The file to test lock for 16166924c554SJ. Bruce Fields * @fl: The lock to test; also used to hold result 16173ee17abdSJ. Bruce Fields * 16183ee17abdSJ. Bruce Fields * Returns -ERRNO on failure. Indicates presence of conflicting lock by 16193ee17abdSJ. Bruce Fields * setting conf->fl_type to something other than F_UNLCK. 16203ee17abdSJ. Bruce Fields */ 16213ee17abdSJ. Bruce Fields int vfs_test_lock(struct file *filp, struct file_lock *fl) 16223ee17abdSJ. Bruce Fields { 16233ee17abdSJ. Bruce Fields if (filp->f_op && filp->f_op->lock) 16243ee17abdSJ. Bruce Fields return filp->f_op->lock(filp, F_GETLK, fl); 16253ee17abdSJ. Bruce Fields posix_test_lock(filp, fl); 16263ee17abdSJ. Bruce Fields return 0; 16273ee17abdSJ. Bruce Fields } 16283ee17abdSJ. Bruce Fields EXPORT_SYMBOL_GPL(vfs_test_lock); 16293ee17abdSJ. Bruce Fields 1630c2fa1b8aSJ. Bruce Fields static int posix_lock_to_flock(struct flock *flock, struct file_lock *fl) 1631c2fa1b8aSJ. Bruce Fields { 1632c2fa1b8aSJ. Bruce Fields flock->l_pid = fl->fl_pid; 1633c2fa1b8aSJ. Bruce Fields #if BITS_PER_LONG == 32 1634c2fa1b8aSJ. Bruce Fields /* 1635c2fa1b8aSJ. Bruce Fields * Make sure we can represent the posix lock via 1636c2fa1b8aSJ. Bruce Fields * legacy 32bit flock. 1637c2fa1b8aSJ. Bruce Fields */ 1638c2fa1b8aSJ. Bruce Fields if (fl->fl_start > OFFT_OFFSET_MAX) 1639c2fa1b8aSJ. Bruce Fields return -EOVERFLOW; 1640c2fa1b8aSJ. Bruce Fields if (fl->fl_end != OFFSET_MAX && fl->fl_end > OFFT_OFFSET_MAX) 1641c2fa1b8aSJ. Bruce Fields return -EOVERFLOW; 1642c2fa1b8aSJ. Bruce Fields #endif 1643c2fa1b8aSJ. Bruce Fields flock->l_start = fl->fl_start; 1644c2fa1b8aSJ. Bruce Fields flock->l_len = fl->fl_end == OFFSET_MAX ? 0 : 1645c2fa1b8aSJ. Bruce Fields fl->fl_end - fl->fl_start + 1; 1646c2fa1b8aSJ. Bruce Fields flock->l_whence = 0; 1647129a84deSJ. Bruce Fields flock->l_type = fl->fl_type; 1648c2fa1b8aSJ. Bruce Fields return 0; 1649c2fa1b8aSJ. Bruce Fields } 1650c2fa1b8aSJ. Bruce Fields 1651c2fa1b8aSJ. Bruce Fields #if BITS_PER_LONG == 32 1652c2fa1b8aSJ. Bruce Fields static void posix_lock_to_flock64(struct flock64 *flock, struct file_lock *fl) 1653c2fa1b8aSJ. Bruce Fields { 1654c2fa1b8aSJ. Bruce Fields flock->l_pid = fl->fl_pid; 1655c2fa1b8aSJ. Bruce Fields flock->l_start = fl->fl_start; 1656c2fa1b8aSJ. Bruce Fields flock->l_len = fl->fl_end == OFFSET_MAX ? 0 : 1657c2fa1b8aSJ. Bruce Fields fl->fl_end - fl->fl_start + 1; 1658c2fa1b8aSJ. Bruce Fields flock->l_whence = 0; 1659c2fa1b8aSJ. Bruce Fields flock->l_type = fl->fl_type; 1660c2fa1b8aSJ. Bruce Fields } 1661c2fa1b8aSJ. Bruce Fields #endif 1662c2fa1b8aSJ. Bruce Fields 16631da177e4SLinus Torvalds /* Report the first existing lock that would conflict with l. 16641da177e4SLinus Torvalds * This implements the F_GETLK command of fcntl(). 16651da177e4SLinus Torvalds */ 16661da177e4SLinus Torvalds int fcntl_getlk(struct file *filp, struct flock __user *l) 16671da177e4SLinus Torvalds { 16689d6a8c5cSMarc Eshel struct file_lock file_lock; 16691da177e4SLinus Torvalds struct flock flock; 16701da177e4SLinus Torvalds int error; 16711da177e4SLinus Torvalds 16721da177e4SLinus Torvalds error = -EFAULT; 16731da177e4SLinus Torvalds if (copy_from_user(&flock, l, sizeof(flock))) 16741da177e4SLinus Torvalds goto out; 16751da177e4SLinus Torvalds error = -EINVAL; 16761da177e4SLinus Torvalds if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK)) 16771da177e4SLinus Torvalds goto out; 16781da177e4SLinus Torvalds 16791da177e4SLinus Torvalds error = flock_to_posix_lock(filp, &file_lock, &flock); 16801da177e4SLinus Torvalds if (error) 16811da177e4SLinus Torvalds goto out; 16821da177e4SLinus Torvalds 16833ee17abdSJ. Bruce Fields error = vfs_test_lock(filp, &file_lock); 16843ee17abdSJ. Bruce Fields if (error) 16851da177e4SLinus Torvalds goto out; 16861da177e4SLinus Torvalds 16879d6a8c5cSMarc Eshel flock.l_type = file_lock.fl_type; 16889d6a8c5cSMarc Eshel if (file_lock.fl_type != F_UNLCK) { 16899d6a8c5cSMarc Eshel error = posix_lock_to_flock(&flock, &file_lock); 1690c2fa1b8aSJ. Bruce Fields if (error) 16911da177e4SLinus Torvalds goto out; 16921da177e4SLinus Torvalds } 16931da177e4SLinus Torvalds error = -EFAULT; 16941da177e4SLinus Torvalds if (!copy_to_user(l, &flock, sizeof(flock))) 16951da177e4SLinus Torvalds error = 0; 16961da177e4SLinus Torvalds out: 16971da177e4SLinus Torvalds return error; 16981da177e4SLinus Torvalds } 16991da177e4SLinus Torvalds 17007723ec97SMarc Eshel /** 17017723ec97SMarc Eshel * vfs_lock_file - file byte range lock 17027723ec97SMarc Eshel * @filp: The file to apply the lock to 17037723ec97SMarc Eshel * @cmd: type of locking operation (F_SETLK, F_GETLK, etc.) 17047723ec97SMarc Eshel * @fl: The lock to be applied 1705150b3934SMarc Eshel * @conf: Place to return a copy of the conflicting lock, if found. 1706150b3934SMarc Eshel * 1707150b3934SMarc Eshel * A caller that doesn't care about the conflicting lock may pass NULL 1708150b3934SMarc Eshel * as the final argument. 1709150b3934SMarc Eshel * 1710150b3934SMarc Eshel * If the filesystem defines a private ->lock() method, then @conf will 1711150b3934SMarc Eshel * be left unchanged; so a caller that cares should initialize it to 1712150b3934SMarc Eshel * some acceptable default. 17132beb6614SMarc Eshel * 17142beb6614SMarc Eshel * To avoid blocking kernel daemons, such as lockd, that need to acquire POSIX 17152beb6614SMarc Eshel * locks, the ->lock() interface may return asynchronously, before the lock has 17162beb6614SMarc Eshel * been granted or denied by the underlying filesystem, if (and only if) 17172beb6614SMarc Eshel * fl_grant is set. Callers expecting ->lock() to return asynchronously 17182beb6614SMarc Eshel * will only use F_SETLK, not F_SETLKW; they will set FL_SLEEP if (and only if) 17192beb6614SMarc Eshel * the request is for a blocking lock. When ->lock() does return asynchronously, 1720bde74e4bSMiklos Szeredi * it must return FILE_LOCK_DEFERRED, and call ->fl_grant() when the lock 17212beb6614SMarc Eshel * request completes. 17222beb6614SMarc Eshel * If the request is for non-blocking lock the file system should return 1723bde74e4bSMiklos Szeredi * FILE_LOCK_DEFERRED then try to get the lock and call the callback routine 1724bde74e4bSMiklos Szeredi * with the result. If the request timed out the callback routine will return a 17252beb6614SMarc Eshel * nonzero return code and the file system should release the lock. The file 17262beb6614SMarc Eshel * system is also responsible to keep a corresponding posix lock when it 17272beb6614SMarc Eshel * grants a lock so the VFS can find out which locks are locally held and do 17282beb6614SMarc Eshel * the correct lock cleanup when required. 17292beb6614SMarc Eshel * The underlying filesystem must not drop the kernel lock or call 1730bde74e4bSMiklos Szeredi * ->fl_grant() before returning to the caller with a FILE_LOCK_DEFERRED 17312beb6614SMarc Eshel * return code. 17327723ec97SMarc Eshel */ 1733150b3934SMarc Eshel int vfs_lock_file(struct file *filp, unsigned int cmd, struct file_lock *fl, struct file_lock *conf) 17347723ec97SMarc Eshel { 17357723ec97SMarc Eshel if (filp->f_op && filp->f_op->lock) 17367723ec97SMarc Eshel return filp->f_op->lock(filp, cmd, fl); 17377723ec97SMarc Eshel else 1738150b3934SMarc Eshel return posix_lock_file(filp, fl, conf); 17397723ec97SMarc Eshel } 17407723ec97SMarc Eshel EXPORT_SYMBOL_GPL(vfs_lock_file); 17417723ec97SMarc Eshel 1742b648a6deSMiklos Szeredi static int do_lock_file_wait(struct file *filp, unsigned int cmd, 1743b648a6deSMiklos Szeredi struct file_lock *fl) 1744b648a6deSMiklos Szeredi { 1745b648a6deSMiklos Szeredi int error; 1746b648a6deSMiklos Szeredi 1747b648a6deSMiklos Szeredi error = security_file_lock(filp, fl->fl_type); 1748b648a6deSMiklos Szeredi if (error) 1749b648a6deSMiklos Szeredi return error; 1750b648a6deSMiklos Szeredi 1751b648a6deSMiklos Szeredi for (;;) { 1752764c76b3SMiklos Szeredi error = vfs_lock_file(filp, cmd, fl, NULL); 1753b648a6deSMiklos Szeredi if (error != FILE_LOCK_DEFERRED) 1754b648a6deSMiklos Szeredi break; 1755764c76b3SMiklos Szeredi error = wait_event_interruptible(fl->fl_wait, !fl->fl_next); 1756b648a6deSMiklos Szeredi if (!error) 1757b648a6deSMiklos Szeredi continue; 1758b648a6deSMiklos Szeredi 1759b648a6deSMiklos Szeredi locks_delete_block(fl); 1760b648a6deSMiklos Szeredi break; 1761b648a6deSMiklos Szeredi } 1762b648a6deSMiklos Szeredi 1763b648a6deSMiklos Szeredi return error; 1764b648a6deSMiklos Szeredi } 1765b648a6deSMiklos Szeredi 17661da177e4SLinus Torvalds /* Apply the lock described by l to an open file descriptor. 17671da177e4SLinus Torvalds * This implements both the F_SETLK and F_SETLKW commands of fcntl(). 17681da177e4SLinus Torvalds */ 1769c293621bSPeter Staubach int fcntl_setlk(unsigned int fd, struct file *filp, unsigned int cmd, 1770c293621bSPeter Staubach struct flock __user *l) 17711da177e4SLinus Torvalds { 17721da177e4SLinus Torvalds struct file_lock *file_lock = locks_alloc_lock(); 17731da177e4SLinus Torvalds struct flock flock; 17741da177e4SLinus Torvalds struct inode *inode; 17750b2bac2fSAl Viro struct file *f; 17761da177e4SLinus Torvalds int error; 17771da177e4SLinus Torvalds 17781da177e4SLinus Torvalds if (file_lock == NULL) 17791da177e4SLinus Torvalds return -ENOLCK; 17801da177e4SLinus Torvalds 17811da177e4SLinus Torvalds /* 17821da177e4SLinus Torvalds * This might block, so we do it before checking the inode. 17831da177e4SLinus Torvalds */ 17841da177e4SLinus Torvalds error = -EFAULT; 17851da177e4SLinus Torvalds if (copy_from_user(&flock, l, sizeof(flock))) 17861da177e4SLinus Torvalds goto out; 17871da177e4SLinus Torvalds 17880f7fc9e4SJosef "Jeff" Sipek inode = filp->f_path.dentry->d_inode; 17891da177e4SLinus Torvalds 17901da177e4SLinus Torvalds /* Don't allow mandatory locks on files that may be memory mapped 17911da177e4SLinus Torvalds * and shared. 17921da177e4SLinus Torvalds */ 1793a16877caSPavel Emelyanov if (mandatory_lock(inode) && mapping_writably_mapped(filp->f_mapping)) { 17941da177e4SLinus Torvalds error = -EAGAIN; 17951da177e4SLinus Torvalds goto out; 17961da177e4SLinus Torvalds } 17971da177e4SLinus Torvalds 1798c293621bSPeter Staubach again: 17991da177e4SLinus Torvalds error = flock_to_posix_lock(filp, file_lock, &flock); 18001da177e4SLinus Torvalds if (error) 18011da177e4SLinus Torvalds goto out; 18021da177e4SLinus Torvalds if (cmd == F_SETLKW) { 18031da177e4SLinus Torvalds file_lock->fl_flags |= FL_SLEEP; 18041da177e4SLinus Torvalds } 18051da177e4SLinus Torvalds 18061da177e4SLinus Torvalds error = -EBADF; 18071da177e4SLinus Torvalds switch (flock.l_type) { 18081da177e4SLinus Torvalds case F_RDLCK: 18091da177e4SLinus Torvalds if (!(filp->f_mode & FMODE_READ)) 18101da177e4SLinus Torvalds goto out; 18111da177e4SLinus Torvalds break; 18121da177e4SLinus Torvalds case F_WRLCK: 18131da177e4SLinus Torvalds if (!(filp->f_mode & FMODE_WRITE)) 18141da177e4SLinus Torvalds goto out; 18151da177e4SLinus Torvalds break; 18161da177e4SLinus Torvalds case F_UNLCK: 18171da177e4SLinus Torvalds break; 18181da177e4SLinus Torvalds default: 18191da177e4SLinus Torvalds error = -EINVAL; 18201da177e4SLinus Torvalds goto out; 18211da177e4SLinus Torvalds } 18221da177e4SLinus Torvalds 1823b648a6deSMiklos Szeredi error = do_lock_file_wait(filp, cmd, file_lock); 1824c293621bSPeter Staubach 1825c293621bSPeter Staubach /* 1826c293621bSPeter Staubach * Attempt to detect a close/fcntl race and recover by 1827c293621bSPeter Staubach * releasing the lock that was just acquired. 1828c293621bSPeter Staubach */ 18290b2bac2fSAl Viro /* 18300b2bac2fSAl Viro * we need that spin_lock here - it prevents reordering between 18310b2bac2fSAl Viro * update of inode->i_flock and check for it done in close(). 18320b2bac2fSAl Viro * rcu_read_lock() wouldn't do. 18330b2bac2fSAl Viro */ 18340b2bac2fSAl Viro spin_lock(¤t->files->file_lock); 18350b2bac2fSAl Viro f = fcheck(fd); 18360b2bac2fSAl Viro spin_unlock(¤t->files->file_lock); 18370b2bac2fSAl Viro if (!error && f != filp && flock.l_type != F_UNLCK) { 1838c293621bSPeter Staubach flock.l_type = F_UNLCK; 1839c293621bSPeter Staubach goto again; 1840c293621bSPeter Staubach } 18411da177e4SLinus Torvalds 18421da177e4SLinus Torvalds out: 18431da177e4SLinus Torvalds locks_free_lock(file_lock); 18441da177e4SLinus Torvalds return error; 18451da177e4SLinus Torvalds } 18461da177e4SLinus Torvalds 18471da177e4SLinus Torvalds #if BITS_PER_LONG == 32 18481da177e4SLinus Torvalds /* Report the first existing lock that would conflict with l. 18491da177e4SLinus Torvalds * This implements the F_GETLK command of fcntl(). 18501da177e4SLinus Torvalds */ 18511da177e4SLinus Torvalds int fcntl_getlk64(struct file *filp, struct flock64 __user *l) 18521da177e4SLinus Torvalds { 18539d6a8c5cSMarc Eshel struct file_lock file_lock; 18541da177e4SLinus Torvalds struct flock64 flock; 18551da177e4SLinus Torvalds int error; 18561da177e4SLinus Torvalds 18571da177e4SLinus Torvalds error = -EFAULT; 18581da177e4SLinus Torvalds if (copy_from_user(&flock, l, sizeof(flock))) 18591da177e4SLinus Torvalds goto out; 18601da177e4SLinus Torvalds error = -EINVAL; 18611da177e4SLinus Torvalds if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK)) 18621da177e4SLinus Torvalds goto out; 18631da177e4SLinus Torvalds 18641da177e4SLinus Torvalds error = flock64_to_posix_lock(filp, &file_lock, &flock); 18651da177e4SLinus Torvalds if (error) 18661da177e4SLinus Torvalds goto out; 18671da177e4SLinus Torvalds 18683ee17abdSJ. Bruce Fields error = vfs_test_lock(filp, &file_lock); 18693ee17abdSJ. Bruce Fields if (error) 18701da177e4SLinus Torvalds goto out; 18711da177e4SLinus Torvalds 18729d6a8c5cSMarc Eshel flock.l_type = file_lock.fl_type; 18739d6a8c5cSMarc Eshel if (file_lock.fl_type != F_UNLCK) 18749d6a8c5cSMarc Eshel posix_lock_to_flock64(&flock, &file_lock); 18759d6a8c5cSMarc Eshel 18761da177e4SLinus Torvalds error = -EFAULT; 18771da177e4SLinus Torvalds if (!copy_to_user(l, &flock, sizeof(flock))) 18781da177e4SLinus Torvalds error = 0; 18791da177e4SLinus Torvalds 18801da177e4SLinus Torvalds out: 18811da177e4SLinus Torvalds return error; 18821da177e4SLinus Torvalds } 18831da177e4SLinus Torvalds 18841da177e4SLinus Torvalds /* Apply the lock described by l to an open file descriptor. 18851da177e4SLinus Torvalds * This implements both the F_SETLK and F_SETLKW commands of fcntl(). 18861da177e4SLinus Torvalds */ 1887c293621bSPeter Staubach int fcntl_setlk64(unsigned int fd, struct file *filp, unsigned int cmd, 1888c293621bSPeter Staubach struct flock64 __user *l) 18891da177e4SLinus Torvalds { 18901da177e4SLinus Torvalds struct file_lock *file_lock = locks_alloc_lock(); 18911da177e4SLinus Torvalds struct flock64 flock; 18921da177e4SLinus Torvalds struct inode *inode; 18930b2bac2fSAl Viro struct file *f; 18941da177e4SLinus Torvalds int error; 18951da177e4SLinus Torvalds 18961da177e4SLinus Torvalds if (file_lock == NULL) 18971da177e4SLinus Torvalds return -ENOLCK; 18981da177e4SLinus Torvalds 18991da177e4SLinus Torvalds /* 19001da177e4SLinus Torvalds * This might block, so we do it before checking the inode. 19011da177e4SLinus Torvalds */ 19021da177e4SLinus Torvalds error = -EFAULT; 19031da177e4SLinus Torvalds if (copy_from_user(&flock, l, sizeof(flock))) 19041da177e4SLinus Torvalds goto out; 19051da177e4SLinus Torvalds 19060f7fc9e4SJosef "Jeff" Sipek inode = filp->f_path.dentry->d_inode; 19071da177e4SLinus Torvalds 19081da177e4SLinus Torvalds /* Don't allow mandatory locks on files that may be memory mapped 19091da177e4SLinus Torvalds * and shared. 19101da177e4SLinus Torvalds */ 1911a16877caSPavel Emelyanov if (mandatory_lock(inode) && mapping_writably_mapped(filp->f_mapping)) { 19121da177e4SLinus Torvalds error = -EAGAIN; 19131da177e4SLinus Torvalds goto out; 19141da177e4SLinus Torvalds } 19151da177e4SLinus Torvalds 1916c293621bSPeter Staubach again: 19171da177e4SLinus Torvalds error = flock64_to_posix_lock(filp, file_lock, &flock); 19181da177e4SLinus Torvalds if (error) 19191da177e4SLinus Torvalds goto out; 19201da177e4SLinus Torvalds if (cmd == F_SETLKW64) { 19211da177e4SLinus Torvalds file_lock->fl_flags |= FL_SLEEP; 19221da177e4SLinus Torvalds } 19231da177e4SLinus Torvalds 19241da177e4SLinus Torvalds error = -EBADF; 19251da177e4SLinus Torvalds switch (flock.l_type) { 19261da177e4SLinus Torvalds case F_RDLCK: 19271da177e4SLinus Torvalds if (!(filp->f_mode & FMODE_READ)) 19281da177e4SLinus Torvalds goto out; 19291da177e4SLinus Torvalds break; 19301da177e4SLinus Torvalds case F_WRLCK: 19311da177e4SLinus Torvalds if (!(filp->f_mode & FMODE_WRITE)) 19321da177e4SLinus Torvalds goto out; 19331da177e4SLinus Torvalds break; 19341da177e4SLinus Torvalds case F_UNLCK: 19351da177e4SLinus Torvalds break; 19361da177e4SLinus Torvalds default: 19371da177e4SLinus Torvalds error = -EINVAL; 19381da177e4SLinus Torvalds goto out; 19391da177e4SLinus Torvalds } 19401da177e4SLinus Torvalds 1941b648a6deSMiklos Szeredi error = do_lock_file_wait(filp, cmd, file_lock); 1942c293621bSPeter Staubach 1943c293621bSPeter Staubach /* 1944c293621bSPeter Staubach * Attempt to detect a close/fcntl race and recover by 1945c293621bSPeter Staubach * releasing the lock that was just acquired. 1946c293621bSPeter Staubach */ 19470b2bac2fSAl Viro spin_lock(¤t->files->file_lock); 19480b2bac2fSAl Viro f = fcheck(fd); 19490b2bac2fSAl Viro spin_unlock(¤t->files->file_lock); 19500b2bac2fSAl Viro if (!error && f != filp && flock.l_type != F_UNLCK) { 1951c293621bSPeter Staubach flock.l_type = F_UNLCK; 1952c293621bSPeter Staubach goto again; 1953c293621bSPeter Staubach } 19541da177e4SLinus Torvalds 19551da177e4SLinus Torvalds out: 19561da177e4SLinus Torvalds locks_free_lock(file_lock); 19571da177e4SLinus Torvalds return error; 19581da177e4SLinus Torvalds } 19591da177e4SLinus Torvalds #endif /* BITS_PER_LONG == 32 */ 19601da177e4SLinus Torvalds 19611da177e4SLinus Torvalds /* 19621da177e4SLinus Torvalds * This function is called when the file is being removed 19631da177e4SLinus Torvalds * from the task's fd array. POSIX locks belonging to this task 19641da177e4SLinus Torvalds * are deleted at this time. 19651da177e4SLinus Torvalds */ 19661da177e4SLinus Torvalds void locks_remove_posix(struct file *filp, fl_owner_t owner) 19671da177e4SLinus Torvalds { 1968ff7b86b8SMiklos Szeredi struct file_lock lock; 19691da177e4SLinus Torvalds 19701da177e4SLinus Torvalds /* 19711da177e4SLinus Torvalds * If there are no locks held on this file, we don't need to call 19721da177e4SLinus Torvalds * posix_lock_file(). Another process could be setting a lock on this 19731da177e4SLinus Torvalds * file at the same time, but we wouldn't remove that lock anyway. 19741da177e4SLinus Torvalds */ 19750f7fc9e4SJosef "Jeff" Sipek if (!filp->f_path.dentry->d_inode->i_flock) 19761da177e4SLinus Torvalds return; 19771da177e4SLinus Torvalds 19781da177e4SLinus Torvalds lock.fl_type = F_UNLCK; 197975e1fcc0SMiklos Szeredi lock.fl_flags = FL_POSIX | FL_CLOSE; 19801da177e4SLinus Torvalds lock.fl_start = 0; 19811da177e4SLinus Torvalds lock.fl_end = OFFSET_MAX; 19821da177e4SLinus Torvalds lock.fl_owner = owner; 19831da177e4SLinus Torvalds lock.fl_pid = current->tgid; 19841da177e4SLinus Torvalds lock.fl_file = filp; 19851da177e4SLinus Torvalds lock.fl_ops = NULL; 19861da177e4SLinus Torvalds lock.fl_lmops = NULL; 19871da177e4SLinus Torvalds 1988150b3934SMarc Eshel vfs_lock_file(filp, F_SETLK, &lock, NULL); 19891da177e4SLinus Torvalds 19901da177e4SLinus Torvalds if (lock.fl_ops && lock.fl_ops->fl_release_private) 19911da177e4SLinus Torvalds lock.fl_ops->fl_release_private(&lock); 19921da177e4SLinus Torvalds } 19931da177e4SLinus Torvalds 19941da177e4SLinus Torvalds EXPORT_SYMBOL(locks_remove_posix); 19951da177e4SLinus Torvalds 19961da177e4SLinus Torvalds /* 19971da177e4SLinus Torvalds * This function is called on the last close of an open file. 19981da177e4SLinus Torvalds */ 19991da177e4SLinus Torvalds void locks_remove_flock(struct file *filp) 20001da177e4SLinus Torvalds { 20010f7fc9e4SJosef "Jeff" Sipek struct inode * inode = filp->f_path.dentry->d_inode; 20021da177e4SLinus Torvalds struct file_lock *fl; 20031da177e4SLinus Torvalds struct file_lock **before; 20041da177e4SLinus Torvalds 20051da177e4SLinus Torvalds if (!inode->i_flock) 20061da177e4SLinus Torvalds return; 20071da177e4SLinus Torvalds 20081da177e4SLinus Torvalds if (filp->f_op && filp->f_op->flock) { 20091da177e4SLinus Torvalds struct file_lock fl = { 20101da177e4SLinus Torvalds .fl_pid = current->tgid, 20111da177e4SLinus Torvalds .fl_file = filp, 20121da177e4SLinus Torvalds .fl_flags = FL_FLOCK, 20131da177e4SLinus Torvalds .fl_type = F_UNLCK, 20141da177e4SLinus Torvalds .fl_end = OFFSET_MAX, 20151da177e4SLinus Torvalds }; 20161da177e4SLinus Torvalds filp->f_op->flock(filp, F_SETLKW, &fl); 201780fec4c6STrond Myklebust if (fl.fl_ops && fl.fl_ops->fl_release_private) 201880fec4c6STrond Myklebust fl.fl_ops->fl_release_private(&fl); 20191da177e4SLinus Torvalds } 20201da177e4SLinus Torvalds 20211da177e4SLinus Torvalds lock_kernel(); 20221da177e4SLinus Torvalds before = &inode->i_flock; 20231da177e4SLinus Torvalds 20241da177e4SLinus Torvalds while ((fl = *before) != NULL) { 20251da177e4SLinus Torvalds if (fl->fl_file == filp) { 2026c293621bSPeter Staubach if (IS_FLOCK(fl)) { 20271da177e4SLinus Torvalds locks_delete_lock(before); 20281da177e4SLinus Torvalds continue; 20291da177e4SLinus Torvalds } 20301da177e4SLinus Torvalds if (IS_LEASE(fl)) { 20311da177e4SLinus Torvalds lease_modify(before, F_UNLCK); 20321da177e4SLinus Torvalds continue; 20331da177e4SLinus Torvalds } 20341da177e4SLinus Torvalds /* What? */ 20351da177e4SLinus Torvalds BUG(); 20361da177e4SLinus Torvalds } 20371da177e4SLinus Torvalds before = &fl->fl_next; 20381da177e4SLinus Torvalds } 20391da177e4SLinus Torvalds unlock_kernel(); 20401da177e4SLinus Torvalds } 20411da177e4SLinus Torvalds 20421da177e4SLinus Torvalds /** 20431da177e4SLinus Torvalds * posix_unblock_lock - stop waiting for a file lock 20441da177e4SLinus Torvalds * @filp: how the file was opened 20451da177e4SLinus Torvalds * @waiter: the lock which was waiting 20461da177e4SLinus Torvalds * 20471da177e4SLinus Torvalds * lockd needs to block waiting for locks. 20481da177e4SLinus Torvalds */ 204964a318eeSJ. Bruce Fields int 20501da177e4SLinus Torvalds posix_unblock_lock(struct file *filp, struct file_lock *waiter) 20511da177e4SLinus Torvalds { 205264a318eeSJ. Bruce Fields int status = 0; 205364a318eeSJ. Bruce Fields 20541da177e4SLinus Torvalds lock_kernel(); 20555996a298SJ. Bruce Fields if (waiter->fl_next) 20561da177e4SLinus Torvalds __locks_delete_block(waiter); 205764a318eeSJ. Bruce Fields else 205864a318eeSJ. Bruce Fields status = -ENOENT; 20591da177e4SLinus Torvalds unlock_kernel(); 206064a318eeSJ. Bruce Fields return status; 20611da177e4SLinus Torvalds } 20621da177e4SLinus Torvalds 20631da177e4SLinus Torvalds EXPORT_SYMBOL(posix_unblock_lock); 20641da177e4SLinus Torvalds 20659b9d2ab4SMarc Eshel /** 20669b9d2ab4SMarc Eshel * vfs_cancel_lock - file byte range unblock lock 20679b9d2ab4SMarc Eshel * @filp: The file to apply the unblock to 20689b9d2ab4SMarc Eshel * @fl: The lock to be unblocked 20699b9d2ab4SMarc Eshel * 20709b9d2ab4SMarc Eshel * Used by lock managers to cancel blocked requests 20719b9d2ab4SMarc Eshel */ 20729b9d2ab4SMarc Eshel int vfs_cancel_lock(struct file *filp, struct file_lock *fl) 20739b9d2ab4SMarc Eshel { 20749b9d2ab4SMarc Eshel if (filp->f_op && filp->f_op->lock) 20759b9d2ab4SMarc Eshel return filp->f_op->lock(filp, F_CANCELLK, fl); 20769b9d2ab4SMarc Eshel return 0; 20779b9d2ab4SMarc Eshel } 20789b9d2ab4SMarc Eshel 20799b9d2ab4SMarc Eshel EXPORT_SYMBOL_GPL(vfs_cancel_lock); 20809b9d2ab4SMarc Eshel 20817f8ada98SPavel Emelyanov #ifdef CONFIG_PROC_FS 2082d8ba7a36SAlexey Dobriyan #include <linux/proc_fs.h> 20837f8ada98SPavel Emelyanov #include <linux/seq_file.h> 20847f8ada98SPavel Emelyanov 20857f8ada98SPavel Emelyanov static void lock_get_status(struct seq_file *f, struct file_lock *fl, 20867f8ada98SPavel Emelyanov int id, char *pfx) 20871da177e4SLinus Torvalds { 20881da177e4SLinus Torvalds struct inode *inode = NULL; 2089ab1f1611SVitaliy Gusev unsigned int fl_pid; 2090ab1f1611SVitaliy Gusev 2091ab1f1611SVitaliy Gusev if (fl->fl_nspid) 20926c5f3e7bSPavel Emelyanov fl_pid = pid_vnr(fl->fl_nspid); 2093ab1f1611SVitaliy Gusev else 2094ab1f1611SVitaliy Gusev fl_pid = fl->fl_pid; 20951da177e4SLinus Torvalds 20961da177e4SLinus Torvalds if (fl->fl_file != NULL) 20970f7fc9e4SJosef "Jeff" Sipek inode = fl->fl_file->f_path.dentry->d_inode; 20981da177e4SLinus Torvalds 20997f8ada98SPavel Emelyanov seq_printf(f, "%d:%s ", id, pfx); 21001da177e4SLinus Torvalds if (IS_POSIX(fl)) { 21017f8ada98SPavel Emelyanov seq_printf(f, "%6s %s ", 21021da177e4SLinus Torvalds (fl->fl_flags & FL_ACCESS) ? "ACCESS" : "POSIX ", 21031da177e4SLinus Torvalds (inode == NULL) ? "*NOINODE*" : 2104a16877caSPavel Emelyanov mandatory_lock(inode) ? "MANDATORY" : "ADVISORY "); 21051da177e4SLinus Torvalds } else if (IS_FLOCK(fl)) { 21061da177e4SLinus Torvalds if (fl->fl_type & LOCK_MAND) { 21077f8ada98SPavel Emelyanov seq_printf(f, "FLOCK MSNFS "); 21081da177e4SLinus Torvalds } else { 21097f8ada98SPavel Emelyanov seq_printf(f, "FLOCK ADVISORY "); 21101da177e4SLinus Torvalds } 21111da177e4SLinus Torvalds } else if (IS_LEASE(fl)) { 21127f8ada98SPavel Emelyanov seq_printf(f, "LEASE "); 21131da177e4SLinus Torvalds if (fl->fl_type & F_INPROGRESS) 21147f8ada98SPavel Emelyanov seq_printf(f, "BREAKING "); 21151da177e4SLinus Torvalds else if (fl->fl_file) 21167f8ada98SPavel Emelyanov seq_printf(f, "ACTIVE "); 21171da177e4SLinus Torvalds else 21187f8ada98SPavel Emelyanov seq_printf(f, "BREAKER "); 21191da177e4SLinus Torvalds } else { 21207f8ada98SPavel Emelyanov seq_printf(f, "UNKNOWN UNKNOWN "); 21211da177e4SLinus Torvalds } 21221da177e4SLinus Torvalds if (fl->fl_type & LOCK_MAND) { 21237f8ada98SPavel Emelyanov seq_printf(f, "%s ", 21241da177e4SLinus Torvalds (fl->fl_type & LOCK_READ) 21251da177e4SLinus Torvalds ? (fl->fl_type & LOCK_WRITE) ? "RW " : "READ " 21261da177e4SLinus Torvalds : (fl->fl_type & LOCK_WRITE) ? "WRITE" : "NONE "); 21271da177e4SLinus Torvalds } else { 21287f8ada98SPavel Emelyanov seq_printf(f, "%s ", 21291da177e4SLinus Torvalds (fl->fl_type & F_INPROGRESS) 21301da177e4SLinus Torvalds ? (fl->fl_type & F_UNLCK) ? "UNLCK" : "READ " 21311da177e4SLinus Torvalds : (fl->fl_type & F_WRLCK) ? "WRITE" : "READ "); 21321da177e4SLinus Torvalds } 21331da177e4SLinus Torvalds if (inode) { 21341da177e4SLinus Torvalds #ifdef WE_CAN_BREAK_LSLK_NOW 2135ab1f1611SVitaliy Gusev seq_printf(f, "%d %s:%ld ", fl_pid, 21361da177e4SLinus Torvalds inode->i_sb->s_id, inode->i_ino); 21371da177e4SLinus Torvalds #else 21381da177e4SLinus Torvalds /* userspace relies on this representation of dev_t ;-( */ 2139ab1f1611SVitaliy Gusev seq_printf(f, "%d %02x:%02x:%ld ", fl_pid, 21401da177e4SLinus Torvalds MAJOR(inode->i_sb->s_dev), 21411da177e4SLinus Torvalds MINOR(inode->i_sb->s_dev), inode->i_ino); 21421da177e4SLinus Torvalds #endif 21431da177e4SLinus Torvalds } else { 2144ab1f1611SVitaliy Gusev seq_printf(f, "%d <none>:0 ", fl_pid); 21451da177e4SLinus Torvalds } 21461da177e4SLinus Torvalds if (IS_POSIX(fl)) { 21471da177e4SLinus Torvalds if (fl->fl_end == OFFSET_MAX) 21487f8ada98SPavel Emelyanov seq_printf(f, "%Ld EOF\n", fl->fl_start); 21491da177e4SLinus Torvalds else 21507f8ada98SPavel Emelyanov seq_printf(f, "%Ld %Ld\n", fl->fl_start, fl->fl_end); 21511da177e4SLinus Torvalds } else { 21527f8ada98SPavel Emelyanov seq_printf(f, "0 EOF\n"); 21531da177e4SLinus Torvalds } 21541da177e4SLinus Torvalds } 21551da177e4SLinus Torvalds 21567f8ada98SPavel Emelyanov static int locks_show(struct seq_file *f, void *v) 21571da177e4SLinus Torvalds { 21587f8ada98SPavel Emelyanov struct file_lock *fl, *bfl; 21597f8ada98SPavel Emelyanov 21607f8ada98SPavel Emelyanov fl = list_entry(v, struct file_lock, fl_link); 21617f8ada98SPavel Emelyanov 21627f8ada98SPavel Emelyanov lock_get_status(f, fl, (long)f->private, ""); 21637f8ada98SPavel Emelyanov 21647f8ada98SPavel Emelyanov list_for_each_entry(bfl, &fl->fl_block, fl_block) 21657f8ada98SPavel Emelyanov lock_get_status(f, bfl, (long)f->private, " ->"); 21667f8ada98SPavel Emelyanov 21677f8ada98SPavel Emelyanov f->private++; 21687f8ada98SPavel Emelyanov return 0; 21691da177e4SLinus Torvalds } 21701da177e4SLinus Torvalds 21717f8ada98SPavel Emelyanov static void *locks_start(struct seq_file *f, loff_t *pos) 21721da177e4SLinus Torvalds { 21731da177e4SLinus Torvalds lock_kernel(); 21747f8ada98SPavel Emelyanov f->private = (void *)1; 21757f8ada98SPavel Emelyanov return seq_list_start(&file_lock_list, *pos); 21761da177e4SLinus Torvalds } 21777f8ada98SPavel Emelyanov 21787f8ada98SPavel Emelyanov static void *locks_next(struct seq_file *f, void *v, loff_t *pos) 21797f8ada98SPavel Emelyanov { 21807f8ada98SPavel Emelyanov return seq_list_next(v, &file_lock_list, pos); 21811da177e4SLinus Torvalds } 21827f8ada98SPavel Emelyanov 21837f8ada98SPavel Emelyanov static void locks_stop(struct seq_file *f, void *v) 21847f8ada98SPavel Emelyanov { 21851da177e4SLinus Torvalds unlock_kernel(); 21861da177e4SLinus Torvalds } 21871da177e4SLinus Torvalds 2188d8ba7a36SAlexey Dobriyan static const struct seq_operations locks_seq_operations = { 21897f8ada98SPavel Emelyanov .start = locks_start, 21907f8ada98SPavel Emelyanov .next = locks_next, 21917f8ada98SPavel Emelyanov .stop = locks_stop, 21927f8ada98SPavel Emelyanov .show = locks_show, 21937f8ada98SPavel Emelyanov }; 2194d8ba7a36SAlexey Dobriyan 2195d8ba7a36SAlexey Dobriyan static int locks_open(struct inode *inode, struct file *filp) 2196d8ba7a36SAlexey Dobriyan { 2197d8ba7a36SAlexey Dobriyan return seq_open(filp, &locks_seq_operations); 2198d8ba7a36SAlexey Dobriyan } 2199d8ba7a36SAlexey Dobriyan 2200d8ba7a36SAlexey Dobriyan static const struct file_operations proc_locks_operations = { 2201d8ba7a36SAlexey Dobriyan .open = locks_open, 2202d8ba7a36SAlexey Dobriyan .read = seq_read, 2203d8ba7a36SAlexey Dobriyan .llseek = seq_lseek, 2204d8ba7a36SAlexey Dobriyan .release = seq_release, 2205d8ba7a36SAlexey Dobriyan }; 2206d8ba7a36SAlexey Dobriyan 2207d8ba7a36SAlexey Dobriyan static int __init proc_locks_init(void) 2208d8ba7a36SAlexey Dobriyan { 2209d8ba7a36SAlexey Dobriyan proc_create("locks", 0, NULL, &proc_locks_operations); 2210d8ba7a36SAlexey Dobriyan return 0; 2211d8ba7a36SAlexey Dobriyan } 2212d8ba7a36SAlexey Dobriyan module_init(proc_locks_init); 22137f8ada98SPavel Emelyanov #endif 22147f8ada98SPavel Emelyanov 22151da177e4SLinus Torvalds /** 22161da177e4SLinus Torvalds * lock_may_read - checks that the region is free of locks 22171da177e4SLinus Torvalds * @inode: the inode that is being read 22181da177e4SLinus Torvalds * @start: the first byte to read 22191da177e4SLinus Torvalds * @len: the number of bytes to read 22201da177e4SLinus Torvalds * 22211da177e4SLinus Torvalds * Emulates Windows locking requirements. Whole-file 22221da177e4SLinus Torvalds * mandatory locks (share modes) can prohibit a read and 22231da177e4SLinus Torvalds * byte-range POSIX locks can prohibit a read if they overlap. 22241da177e4SLinus Torvalds * 22251da177e4SLinus Torvalds * N.B. this function is only ever called 22261da177e4SLinus Torvalds * from knfsd and ownership of locks is never checked. 22271da177e4SLinus Torvalds */ 22281da177e4SLinus Torvalds int lock_may_read(struct inode *inode, loff_t start, unsigned long len) 22291da177e4SLinus Torvalds { 22301da177e4SLinus Torvalds struct file_lock *fl; 22311da177e4SLinus Torvalds int result = 1; 22321da177e4SLinus Torvalds lock_kernel(); 22331da177e4SLinus Torvalds for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) { 22341da177e4SLinus Torvalds if (IS_POSIX(fl)) { 22351da177e4SLinus Torvalds if (fl->fl_type == F_RDLCK) 22361da177e4SLinus Torvalds continue; 22371da177e4SLinus Torvalds if ((fl->fl_end < start) || (fl->fl_start > (start + len))) 22381da177e4SLinus Torvalds continue; 22391da177e4SLinus Torvalds } else if (IS_FLOCK(fl)) { 22401da177e4SLinus Torvalds if (!(fl->fl_type & LOCK_MAND)) 22411da177e4SLinus Torvalds continue; 22421da177e4SLinus Torvalds if (fl->fl_type & LOCK_READ) 22431da177e4SLinus Torvalds continue; 22441da177e4SLinus Torvalds } else 22451da177e4SLinus Torvalds continue; 22461da177e4SLinus Torvalds result = 0; 22471da177e4SLinus Torvalds break; 22481da177e4SLinus Torvalds } 22491da177e4SLinus Torvalds unlock_kernel(); 22501da177e4SLinus Torvalds return result; 22511da177e4SLinus Torvalds } 22521da177e4SLinus Torvalds 22531da177e4SLinus Torvalds EXPORT_SYMBOL(lock_may_read); 22541da177e4SLinus Torvalds 22551da177e4SLinus Torvalds /** 22561da177e4SLinus Torvalds * lock_may_write - checks that the region is free of locks 22571da177e4SLinus Torvalds * @inode: the inode that is being written 22581da177e4SLinus Torvalds * @start: the first byte to write 22591da177e4SLinus Torvalds * @len: the number of bytes to write 22601da177e4SLinus Torvalds * 22611da177e4SLinus Torvalds * Emulates Windows locking requirements. Whole-file 22621da177e4SLinus Torvalds * mandatory locks (share modes) can prohibit a write and 22631da177e4SLinus Torvalds * byte-range POSIX locks can prohibit a write if they overlap. 22641da177e4SLinus Torvalds * 22651da177e4SLinus Torvalds * N.B. this function is only ever called 22661da177e4SLinus Torvalds * from knfsd and ownership of locks is never checked. 22671da177e4SLinus Torvalds */ 22681da177e4SLinus Torvalds int lock_may_write(struct inode *inode, loff_t start, unsigned long len) 22691da177e4SLinus Torvalds { 22701da177e4SLinus Torvalds struct file_lock *fl; 22711da177e4SLinus Torvalds int result = 1; 22721da177e4SLinus Torvalds lock_kernel(); 22731da177e4SLinus Torvalds for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) { 22741da177e4SLinus Torvalds if (IS_POSIX(fl)) { 22751da177e4SLinus Torvalds if ((fl->fl_end < start) || (fl->fl_start > (start + len))) 22761da177e4SLinus Torvalds continue; 22771da177e4SLinus Torvalds } else if (IS_FLOCK(fl)) { 22781da177e4SLinus Torvalds if (!(fl->fl_type & LOCK_MAND)) 22791da177e4SLinus Torvalds continue; 22801da177e4SLinus Torvalds if (fl->fl_type & LOCK_WRITE) 22811da177e4SLinus Torvalds continue; 22821da177e4SLinus Torvalds } else 22831da177e4SLinus Torvalds continue; 22841da177e4SLinus Torvalds result = 0; 22851da177e4SLinus Torvalds break; 22861da177e4SLinus Torvalds } 22871da177e4SLinus Torvalds unlock_kernel(); 22881da177e4SLinus Torvalds return result; 22891da177e4SLinus Torvalds } 22901da177e4SLinus Torvalds 22911da177e4SLinus Torvalds EXPORT_SYMBOL(lock_may_write); 22921da177e4SLinus Torvalds 22931da177e4SLinus Torvalds static int __init filelock_init(void) 22941da177e4SLinus Torvalds { 22951da177e4SLinus Torvalds filelock_cache = kmem_cache_create("file_lock_cache", 22961da177e4SLinus Torvalds sizeof(struct file_lock), 0, SLAB_PANIC, 229720c2df83SPaul Mundt init_once); 22981da177e4SLinus Torvalds return 0; 22991da177e4SLinus Torvalds } 23001da177e4SLinus Torvalds 23011da177e4SLinus Torvalds core_initcall(filelock_init); 2302