11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/fs/locks.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Provide support for fcntl()'s F_GETLK, F_SETLK, and F_SETLKW calls. 51da177e4SLinus Torvalds * Doug Evans (dje@spiff.uucp), August 07, 1992 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * Deadlock detection added. 81da177e4SLinus Torvalds * FIXME: one thing isn't handled yet: 91da177e4SLinus Torvalds * - mandatory locks (requires lots of changes elsewhere) 101da177e4SLinus Torvalds * Kelly Carmichael (kelly@[142.24.8.65]), September 17, 1994. 111da177e4SLinus Torvalds * 121da177e4SLinus Torvalds * Miscellaneous edits, and a total rewrite of posix_lock_file() code. 131da177e4SLinus Torvalds * Kai Petzke (wpp@marie.physik.tu-berlin.de), 1994 141da177e4SLinus Torvalds * 151da177e4SLinus Torvalds * Converted file_lock_table to a linked list from an array, which eliminates 161da177e4SLinus Torvalds * the limits on how many active file locks are open. 171da177e4SLinus Torvalds * Chad Page (pageone@netcom.com), November 27, 1994 181da177e4SLinus Torvalds * 191da177e4SLinus Torvalds * Removed dependency on file descriptors. dup()'ed file descriptors now 201da177e4SLinus Torvalds * get the same locks as the original file descriptors, and a close() on 211da177e4SLinus Torvalds * any file descriptor removes ALL the locks on the file for the current 221da177e4SLinus Torvalds * process. Since locks still depend on the process id, locks are inherited 231da177e4SLinus Torvalds * after an exec() but not after a fork(). This agrees with POSIX, and both 241da177e4SLinus Torvalds * BSD and SVR4 practice. 251da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), February 14, 1995 261da177e4SLinus Torvalds * 271da177e4SLinus Torvalds * Scrapped free list which is redundant now that we allocate locks 281da177e4SLinus Torvalds * dynamically with kmalloc()/kfree(). 291da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), February 21, 1995 301da177e4SLinus Torvalds * 311da177e4SLinus Torvalds * Implemented two lock personalities - FL_FLOCK and FL_POSIX. 321da177e4SLinus Torvalds * 331da177e4SLinus Torvalds * FL_POSIX locks are created with calls to fcntl() and lockf() through the 341da177e4SLinus Torvalds * fcntl() system call. They have the semantics described above. 351da177e4SLinus Torvalds * 361da177e4SLinus Torvalds * FL_FLOCK locks are created with calls to flock(), through the flock() 371da177e4SLinus Torvalds * system call, which is new. Old C libraries implement flock() via fcntl() 381da177e4SLinus Torvalds * and will continue to use the old, broken implementation. 391da177e4SLinus Torvalds * 401da177e4SLinus Torvalds * FL_FLOCK locks follow the 4.4 BSD flock() semantics. They are associated 411da177e4SLinus Torvalds * with a file pointer (filp). As a result they can be shared by a parent 421da177e4SLinus Torvalds * process and its children after a fork(). They are removed when the last 431da177e4SLinus Torvalds * file descriptor referring to the file pointer is closed (unless explicitly 441da177e4SLinus Torvalds * unlocked). 451da177e4SLinus Torvalds * 461da177e4SLinus Torvalds * FL_FLOCK locks never deadlock, an existing lock is always removed before 471da177e4SLinus Torvalds * upgrading from shared to exclusive (or vice versa). When this happens 481da177e4SLinus Torvalds * any processes blocked by the current lock are woken up and allowed to 491da177e4SLinus Torvalds * run before the new lock is applied. 501da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), June 09, 1995 511da177e4SLinus Torvalds * 521da177e4SLinus Torvalds * Removed some race conditions in flock_lock_file(), marked other possible 531da177e4SLinus Torvalds * races. Just grep for FIXME to see them. 541da177e4SLinus Torvalds * Dmitry Gorodchanin (pgmdsg@ibi.com), February 09, 1996. 551da177e4SLinus Torvalds * 561da177e4SLinus Torvalds * Addressed Dmitry's concerns. Deadlock checking no longer recursive. 571da177e4SLinus Torvalds * Lock allocation changed to GFP_ATOMIC as we can't afford to sleep 581da177e4SLinus Torvalds * once we've checked for blocking and deadlocking. 591da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), April 03, 1996. 601da177e4SLinus Torvalds * 611da177e4SLinus Torvalds * Initial implementation of mandatory locks. SunOS turned out to be 621da177e4SLinus Torvalds * a rotten model, so I implemented the "obvious" semantics. 63395cf969SPaul Bolle * See 'Documentation/filesystems/mandatory-locking.txt' for details. 641da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), April 06, 1996. 651da177e4SLinus Torvalds * 661da177e4SLinus Torvalds * Don't allow mandatory locks on mmap()'ed files. Added simple functions to 671da177e4SLinus Torvalds * check if a file has mandatory locks, used by mmap(), open() and creat() to 681da177e4SLinus Torvalds * see if system call should be rejected. Ref. HP-UX/SunOS/Solaris Reference 691da177e4SLinus Torvalds * Manual, Section 2. 701da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), April 09, 1996. 711da177e4SLinus Torvalds * 721da177e4SLinus Torvalds * Tidied up block list handling. Added '/proc/locks' interface. 731da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), April 24, 1996. 741da177e4SLinus Torvalds * 751da177e4SLinus Torvalds * Fixed deadlock condition for pathological code that mixes calls to 761da177e4SLinus Torvalds * flock() and fcntl(). 771da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), April 29, 1996. 781da177e4SLinus Torvalds * 791da177e4SLinus Torvalds * Allow only one type of locking scheme (FL_POSIX or FL_FLOCK) to be in use 801da177e4SLinus Torvalds * for a given file at a time. Changed the CONFIG_LOCK_MANDATORY scheme to 811da177e4SLinus Torvalds * guarantee sensible behaviour in the case where file system modules might 821da177e4SLinus Torvalds * be compiled with different options than the kernel itself. 831da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), May 15, 1996. 841da177e4SLinus Torvalds * 851da177e4SLinus Torvalds * Added a couple of missing wake_up() calls. Thanks to Thomas Meckel 861da177e4SLinus Torvalds * (Thomas.Meckel@mni.fh-giessen.de) for spotting this. 871da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), May 15, 1996. 881da177e4SLinus Torvalds * 891da177e4SLinus Torvalds * Changed FL_POSIX locks to use the block list in the same way as FL_FLOCK 901da177e4SLinus Torvalds * locks. Changed process synchronisation to avoid dereferencing locks that 911da177e4SLinus Torvalds * have already been freed. 921da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), Sep 21, 1996. 931da177e4SLinus Torvalds * 941da177e4SLinus Torvalds * Made the block list a circular list to minimise searching in the list. 951da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), Sep 25, 1996. 961da177e4SLinus Torvalds * 971da177e4SLinus Torvalds * Made mandatory locking a mount option. Default is not to allow mandatory 981da177e4SLinus Torvalds * locking. 991da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), Oct 04, 1996. 1001da177e4SLinus Torvalds * 1011da177e4SLinus Torvalds * Some adaptations for NFS support. 1021da177e4SLinus Torvalds * Olaf Kirch (okir@monad.swb.de), Dec 1996, 1031da177e4SLinus Torvalds * 1041da177e4SLinus Torvalds * Fixed /proc/locks interface so that we can't overrun the buffer we are handed. 1051da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), May 12, 1997. 1061da177e4SLinus Torvalds * 1071da177e4SLinus Torvalds * Use slab allocator instead of kmalloc/kfree. 1081da177e4SLinus Torvalds * Use generic list implementation from <linux/list.h>. 1091da177e4SLinus Torvalds * Sped up posix_locks_deadlock by only considering blocked locks. 1101da177e4SLinus Torvalds * Matthew Wilcox <willy@debian.org>, March, 2000. 1111da177e4SLinus Torvalds * 1121da177e4SLinus Torvalds * Leases and LOCK_MAND 1131da177e4SLinus Torvalds * Matthew Wilcox <willy@debian.org>, June, 2000. 1141da177e4SLinus Torvalds * Stephen Rothwell <sfr@canb.auug.org.au>, June, 2000. 1151da177e4SLinus Torvalds */ 1161da177e4SLinus Torvalds 1171da177e4SLinus Torvalds #include <linux/capability.h> 1181da177e4SLinus Torvalds #include <linux/file.h> 1199f3acc31SAl Viro #include <linux/fdtable.h> 1201da177e4SLinus Torvalds #include <linux/fs.h> 1211da177e4SLinus Torvalds #include <linux/init.h> 1221da177e4SLinus Torvalds #include <linux/security.h> 1231da177e4SLinus Torvalds #include <linux/slab.h> 1241da177e4SLinus Torvalds #include <linux/syscalls.h> 1251da177e4SLinus Torvalds #include <linux/time.h> 1264fb3a538SDipankar Sarma #include <linux/rcupdate.h> 127ab1f1611SVitaliy Gusev #include <linux/pid_namespace.h> 12848f74186SJeff Layton #include <linux/hashtable.h> 1297012b02aSJeff Layton #include <linux/percpu.h> 1307012b02aSJeff Layton #include <linux/lglock.h> 1311da177e4SLinus Torvalds 13262af4f1fSJeff Layton #define CREATE_TRACE_POINTS 13362af4f1fSJeff Layton #include <trace/events/filelock.h> 13462af4f1fSJeff Layton 1351da177e4SLinus Torvalds #include <asm/uaccess.h> 1361da177e4SLinus Torvalds 1371da177e4SLinus Torvalds #define IS_POSIX(fl) (fl->fl_flags & FL_POSIX) 1381da177e4SLinus Torvalds #define IS_FLOCK(fl) (fl->fl_flags & FL_FLOCK) 13911afe9f7SChristoph Hellwig #define IS_LEASE(fl) (fl->fl_flags & (FL_LEASE|FL_DELEG|FL_LAYOUT)) 140cff2fce5SJeff Layton #define IS_OFDLCK(fl) (fl->fl_flags & FL_OFDLCK) 1411da177e4SLinus Torvalds 142ab83fa4bSJ. Bruce Fields static bool lease_breaking(struct file_lock *fl) 143ab83fa4bSJ. Bruce Fields { 144778fc546SJ. Bruce Fields return fl->fl_flags & (FL_UNLOCK_PENDING | FL_DOWNGRADE_PENDING); 145778fc546SJ. Bruce Fields } 146778fc546SJ. Bruce Fields 147778fc546SJ. Bruce Fields static int target_leasetype(struct file_lock *fl) 148778fc546SJ. Bruce Fields { 149778fc546SJ. Bruce Fields if (fl->fl_flags & FL_UNLOCK_PENDING) 150778fc546SJ. Bruce Fields return F_UNLCK; 151778fc546SJ. Bruce Fields if (fl->fl_flags & FL_DOWNGRADE_PENDING) 152778fc546SJ. Bruce Fields return F_RDLCK; 153778fc546SJ. Bruce Fields return fl->fl_type; 154ab83fa4bSJ. Bruce Fields } 155ab83fa4bSJ. Bruce Fields 1561da177e4SLinus Torvalds int leases_enable = 1; 1571da177e4SLinus Torvalds int lease_break_time = 45; 1581da177e4SLinus Torvalds 1591c8c601aSJeff Layton /* 1607012b02aSJeff Layton * The global file_lock_list is only used for displaying /proc/locks, so we 1617012b02aSJeff Layton * keep a list on each CPU, with each list protected by its own spinlock via 1627012b02aSJeff Layton * the file_lock_lglock. Note that alterations to the list also require that 1636109c850SJeff Layton * the relevant flc_lock is held. 1641c8c601aSJeff Layton */ 1657012b02aSJeff Layton DEFINE_STATIC_LGLOCK(file_lock_lglock); 1667012b02aSJeff Layton static DEFINE_PER_CPU(struct hlist_head, file_lock_list); 16788974691SJeff Layton 1681c8c601aSJeff Layton /* 16948f74186SJeff Layton * The blocked_hash is used to find POSIX lock loops for deadlock detection. 1707b2296afSJeff Layton * It is protected by blocked_lock_lock. 17148f74186SJeff Layton * 17248f74186SJeff Layton * We hash locks by lockowner in order to optimize searching for the lock a 17348f74186SJeff Layton * particular lockowner is waiting on. 17448f74186SJeff Layton * 17548f74186SJeff Layton * FIXME: make this value scale via some heuristic? We generally will want more 17648f74186SJeff Layton * buckets when we have more lockowners holding locks, but that's a little 17748f74186SJeff Layton * difficult to determine without knowing what the workload will look like. 1781c8c601aSJeff Layton */ 17948f74186SJeff Layton #define BLOCKED_HASH_BITS 7 18048f74186SJeff Layton static DEFINE_HASHTABLE(blocked_hash, BLOCKED_HASH_BITS); 18188974691SJeff Layton 1821c8c601aSJeff Layton /* 1837b2296afSJeff Layton * This lock protects the blocked_hash. Generally, if you're accessing it, you 1847b2296afSJeff Layton * want to be holding this lock. 1851c8c601aSJeff Layton * 1861c8c601aSJeff Layton * In addition, it also protects the fl->fl_block list, and the fl->fl_next 1871c8c601aSJeff Layton * pointer for file_lock structures that are acting as lock requests (in 1881c8c601aSJeff Layton * contrast to those that are acting as records of acquired locks). 1891c8c601aSJeff Layton * 1901c8c601aSJeff Layton * Note that when we acquire this lock in order to change the above fields, 1916109c850SJeff Layton * we often hold the flc_lock as well. In certain cases, when reading the fields 1921c8c601aSJeff Layton * protected by this lock, we can skip acquiring it iff we already hold the 1936109c850SJeff Layton * flc_lock. 1941c8c601aSJeff Layton * 1951c8c601aSJeff Layton * In particular, adding an entry to the fl_block list requires that you hold 1966109c850SJeff Layton * both the flc_lock and the blocked_lock_lock (acquired in that order). 1976109c850SJeff Layton * Deleting an entry from the list however only requires the file_lock_lock. 1981c8c601aSJeff Layton */ 1997b2296afSJeff Layton static DEFINE_SPINLOCK(blocked_lock_lock); 2001da177e4SLinus Torvalds 2014a075e39SJeff Layton static struct kmem_cache *flctx_cache __read_mostly; 202e18b890bSChristoph Lameter static struct kmem_cache *filelock_cache __read_mostly; 2031da177e4SLinus Torvalds 2044a075e39SJeff Layton static struct file_lock_context * 2055c1c669aSJeff Layton locks_get_lock_context(struct inode *inode, int type) 2064a075e39SJeff Layton { 207128a3785SDmitry Vyukov struct file_lock_context *ctx; 2084a075e39SJeff Layton 209128a3785SDmitry Vyukov /* paired with cmpxchg() below */ 210128a3785SDmitry Vyukov ctx = smp_load_acquire(&inode->i_flctx); 211128a3785SDmitry Vyukov if (likely(ctx) || type == F_UNLCK) 2124a075e39SJeff Layton goto out; 2134a075e39SJeff Layton 214128a3785SDmitry Vyukov ctx = kmem_cache_alloc(flctx_cache, GFP_KERNEL); 215128a3785SDmitry Vyukov if (!ctx) 2164a075e39SJeff Layton goto out; 2174a075e39SJeff Layton 218128a3785SDmitry Vyukov spin_lock_init(&ctx->flc_lock); 219128a3785SDmitry Vyukov INIT_LIST_HEAD(&ctx->flc_flock); 220128a3785SDmitry Vyukov INIT_LIST_HEAD(&ctx->flc_posix); 221128a3785SDmitry Vyukov INIT_LIST_HEAD(&ctx->flc_lease); 2224a075e39SJeff Layton 2234a075e39SJeff Layton /* 2244a075e39SJeff Layton * Assign the pointer if it's not already assigned. If it is, then 2254a075e39SJeff Layton * free the context we just allocated. 2264a075e39SJeff Layton */ 227128a3785SDmitry Vyukov if (cmpxchg(&inode->i_flctx, NULL, ctx)) { 228128a3785SDmitry Vyukov kmem_cache_free(flctx_cache, ctx); 229128a3785SDmitry Vyukov ctx = smp_load_acquire(&inode->i_flctx); 230128a3785SDmitry Vyukov } 2314a075e39SJeff Layton out: 232128a3785SDmitry Vyukov return ctx; 2334a075e39SJeff Layton } 2344a075e39SJeff Layton 2354a075e39SJeff Layton void 2364a075e39SJeff Layton locks_free_lock_context(struct file_lock_context *ctx) 2374a075e39SJeff Layton { 2384a075e39SJeff Layton if (ctx) { 2394a075e39SJeff Layton WARN_ON_ONCE(!list_empty(&ctx->flc_flock)); 240bd61e0a9SJeff Layton WARN_ON_ONCE(!list_empty(&ctx->flc_posix)); 2418634b51fSJeff Layton WARN_ON_ONCE(!list_empty(&ctx->flc_lease)); 2424a075e39SJeff Layton kmem_cache_free(flctx_cache, ctx); 2434a075e39SJeff Layton } 2444a075e39SJeff Layton } 2454a075e39SJeff Layton 246ee19cc40SMiklos Szeredi static void locks_init_lock_heads(struct file_lock *fl) 247a51cb91dSMiklos Szeredi { 248139ca04eSJeff Layton INIT_HLIST_NODE(&fl->fl_link); 2496dee60f6SJeff Layton INIT_LIST_HEAD(&fl->fl_list); 250ee19cc40SMiklos Szeredi INIT_LIST_HEAD(&fl->fl_block); 251ee19cc40SMiklos Szeredi init_waitqueue_head(&fl->fl_wait); 252a51cb91dSMiklos Szeredi } 253a51cb91dSMiklos Szeredi 2541da177e4SLinus Torvalds /* Allocate an empty lock structure. */ 255c5b1f0d9SArnd Bergmann struct file_lock *locks_alloc_lock(void) 2561da177e4SLinus Torvalds { 257ee19cc40SMiklos Szeredi struct file_lock *fl = kmem_cache_zalloc(filelock_cache, GFP_KERNEL); 258a51cb91dSMiklos Szeredi 259a51cb91dSMiklos Szeredi if (fl) 260ee19cc40SMiklos Szeredi locks_init_lock_heads(fl); 261a51cb91dSMiklos Szeredi 262a51cb91dSMiklos Szeredi return fl; 2631da177e4SLinus Torvalds } 264c5b1f0d9SArnd Bergmann EXPORT_SYMBOL_GPL(locks_alloc_lock); 2651da177e4SLinus Torvalds 266a9e61e25SFelix Blyakher void locks_release_private(struct file_lock *fl) 26747831f35STrond Myklebust { 26847831f35STrond Myklebust if (fl->fl_ops) { 26947831f35STrond Myklebust if (fl->fl_ops->fl_release_private) 27047831f35STrond Myklebust fl->fl_ops->fl_release_private(fl); 27147831f35STrond Myklebust fl->fl_ops = NULL; 27247831f35STrond Myklebust } 27347831f35STrond Myklebust 2745c97d7b1SKinglong Mee if (fl->fl_lmops) { 275cae80b30SJeff Layton if (fl->fl_lmops->lm_put_owner) { 276cae80b30SJeff Layton fl->fl_lmops->lm_put_owner(fl->fl_owner); 277cae80b30SJeff Layton fl->fl_owner = NULL; 278cae80b30SJeff Layton } 2795c97d7b1SKinglong Mee fl->fl_lmops = NULL; 2805c97d7b1SKinglong Mee } 28147831f35STrond Myklebust } 282a9e61e25SFelix Blyakher EXPORT_SYMBOL_GPL(locks_release_private); 28347831f35STrond Myklebust 2841da177e4SLinus Torvalds /* Free a lock which is not in use. */ 28505fa3135SJ. Bruce Fields void locks_free_lock(struct file_lock *fl) 2861da177e4SLinus Torvalds { 2875ce29646SMiklos Szeredi BUG_ON(waitqueue_active(&fl->fl_wait)); 2886dee60f6SJeff Layton BUG_ON(!list_empty(&fl->fl_list)); 2895ce29646SMiklos Szeredi BUG_ON(!list_empty(&fl->fl_block)); 290139ca04eSJeff Layton BUG_ON(!hlist_unhashed(&fl->fl_link)); 2911da177e4SLinus Torvalds 29247831f35STrond Myklebust locks_release_private(fl); 2931da177e4SLinus Torvalds kmem_cache_free(filelock_cache, fl); 2941da177e4SLinus Torvalds } 29505fa3135SJ. Bruce Fields EXPORT_SYMBOL(locks_free_lock); 2961da177e4SLinus Torvalds 297ed9814d8SJeff Layton static void 298ed9814d8SJeff Layton locks_dispose_list(struct list_head *dispose) 299ed9814d8SJeff Layton { 300ed9814d8SJeff Layton struct file_lock *fl; 301ed9814d8SJeff Layton 302ed9814d8SJeff Layton while (!list_empty(dispose)) { 3036dee60f6SJeff Layton fl = list_first_entry(dispose, struct file_lock, fl_list); 3046dee60f6SJeff Layton list_del_init(&fl->fl_list); 305ed9814d8SJeff Layton locks_free_lock(fl); 306ed9814d8SJeff Layton } 307ed9814d8SJeff Layton } 308ed9814d8SJeff Layton 3091da177e4SLinus Torvalds void locks_init_lock(struct file_lock *fl) 3101da177e4SLinus Torvalds { 311ee19cc40SMiklos Szeredi memset(fl, 0, sizeof(struct file_lock)); 312ee19cc40SMiklos Szeredi locks_init_lock_heads(fl); 3131da177e4SLinus Torvalds } 3141da177e4SLinus Torvalds 3151da177e4SLinus Torvalds EXPORT_SYMBOL(locks_init_lock); 3161da177e4SLinus Torvalds 3171da177e4SLinus Torvalds /* 3181da177e4SLinus Torvalds * Initialize a new lock from an existing file_lock structure. 3191da177e4SLinus Torvalds */ 3203fe0fff1SKinglong Mee void locks_copy_conflock(struct file_lock *new, struct file_lock *fl) 3211da177e4SLinus Torvalds { 3221da177e4SLinus Torvalds new->fl_owner = fl->fl_owner; 3231da177e4SLinus Torvalds new->fl_pid = fl->fl_pid; 3240996905fSTrond Myklebust new->fl_file = NULL; 3251da177e4SLinus Torvalds new->fl_flags = fl->fl_flags; 3261da177e4SLinus Torvalds new->fl_type = fl->fl_type; 3271da177e4SLinus Torvalds new->fl_start = fl->fl_start; 3281da177e4SLinus Torvalds new->fl_end = fl->fl_end; 329f328296eSKinglong Mee new->fl_lmops = fl->fl_lmops; 3300996905fSTrond Myklebust new->fl_ops = NULL; 331f328296eSKinglong Mee 332f328296eSKinglong Mee if (fl->fl_lmops) { 333f328296eSKinglong Mee if (fl->fl_lmops->lm_get_owner) 334cae80b30SJeff Layton fl->fl_lmops->lm_get_owner(fl->fl_owner); 335f328296eSKinglong Mee } 3360996905fSTrond Myklebust } 3373fe0fff1SKinglong Mee EXPORT_SYMBOL(locks_copy_conflock); 3380996905fSTrond Myklebust 3390996905fSTrond Myklebust void locks_copy_lock(struct file_lock *new, struct file_lock *fl) 3400996905fSTrond Myklebust { 341566709bdSJeff Layton /* "new" must be a freshly-initialized lock */ 342566709bdSJeff Layton WARN_ON_ONCE(new->fl_ops); 3430996905fSTrond Myklebust 3443fe0fff1SKinglong Mee locks_copy_conflock(new, fl); 345f328296eSKinglong Mee 3460996905fSTrond Myklebust new->fl_file = fl->fl_file; 3471da177e4SLinus Torvalds new->fl_ops = fl->fl_ops; 34847831f35STrond Myklebust 349f328296eSKinglong Mee if (fl->fl_ops) { 350f328296eSKinglong Mee if (fl->fl_ops->fl_copy_lock) 351f328296eSKinglong Mee fl->fl_ops->fl_copy_lock(new, fl); 352f328296eSKinglong Mee } 3531da177e4SLinus Torvalds } 3541da177e4SLinus Torvalds 3551da177e4SLinus Torvalds EXPORT_SYMBOL(locks_copy_lock); 3561da177e4SLinus Torvalds 3571da177e4SLinus Torvalds static inline int flock_translate_cmd(int cmd) { 3581da177e4SLinus Torvalds if (cmd & LOCK_MAND) 3591da177e4SLinus Torvalds return cmd & (LOCK_MAND | LOCK_RW); 3601da177e4SLinus Torvalds switch (cmd) { 3611da177e4SLinus Torvalds case LOCK_SH: 3621da177e4SLinus Torvalds return F_RDLCK; 3631da177e4SLinus Torvalds case LOCK_EX: 3641da177e4SLinus Torvalds return F_WRLCK; 3651da177e4SLinus Torvalds case LOCK_UN: 3661da177e4SLinus Torvalds return F_UNLCK; 3671da177e4SLinus Torvalds } 3681da177e4SLinus Torvalds return -EINVAL; 3691da177e4SLinus Torvalds } 3701da177e4SLinus Torvalds 3711da177e4SLinus Torvalds /* Fill in a file_lock structure with an appropriate FLOCK lock. */ 3726e129d00SJeff Layton static struct file_lock * 3736e129d00SJeff Layton flock_make_lock(struct file *filp, unsigned int cmd) 3741da177e4SLinus Torvalds { 3751da177e4SLinus Torvalds struct file_lock *fl; 3761da177e4SLinus Torvalds int type = flock_translate_cmd(cmd); 3776e129d00SJeff Layton 3781da177e4SLinus Torvalds if (type < 0) 3796e129d00SJeff Layton return ERR_PTR(type); 3801da177e4SLinus Torvalds 3811da177e4SLinus Torvalds fl = locks_alloc_lock(); 3821da177e4SLinus Torvalds if (fl == NULL) 3836e129d00SJeff Layton return ERR_PTR(-ENOMEM); 3841da177e4SLinus Torvalds 3851da177e4SLinus Torvalds fl->fl_file = filp; 38673a8f5f7SChristoph Hellwig fl->fl_owner = filp; 3871da177e4SLinus Torvalds fl->fl_pid = current->tgid; 3881da177e4SLinus Torvalds fl->fl_flags = FL_FLOCK; 3891da177e4SLinus Torvalds fl->fl_type = type; 3901da177e4SLinus Torvalds fl->fl_end = OFFSET_MAX; 3911da177e4SLinus Torvalds 3926e129d00SJeff Layton return fl; 3931da177e4SLinus Torvalds } 3941da177e4SLinus Torvalds 3950ec4f431SJ. Bruce Fields static int assign_type(struct file_lock *fl, long type) 3961da177e4SLinus Torvalds { 3971da177e4SLinus Torvalds switch (type) { 3981da177e4SLinus Torvalds case F_RDLCK: 3991da177e4SLinus Torvalds case F_WRLCK: 4001da177e4SLinus Torvalds case F_UNLCK: 4011da177e4SLinus Torvalds fl->fl_type = type; 4021da177e4SLinus Torvalds break; 4031da177e4SLinus Torvalds default: 4041da177e4SLinus Torvalds return -EINVAL; 4051da177e4SLinus Torvalds } 4061da177e4SLinus Torvalds return 0; 4071da177e4SLinus Torvalds } 4081da177e4SLinus Torvalds 409ef12e72aSJ. Bruce Fields static int flock64_to_posix_lock(struct file *filp, struct file_lock *fl, 410ef12e72aSJ. Bruce Fields struct flock64 *l) 411ef12e72aSJ. Bruce Fields { 412ef12e72aSJ. Bruce Fields switch (l->l_whence) { 413ef12e72aSJ. Bruce Fields case SEEK_SET: 414ef12e72aSJ. Bruce Fields fl->fl_start = 0; 415ef12e72aSJ. Bruce Fields break; 416ef12e72aSJ. Bruce Fields case SEEK_CUR: 417ef12e72aSJ. Bruce Fields fl->fl_start = filp->f_pos; 418ef12e72aSJ. Bruce Fields break; 419ef12e72aSJ. Bruce Fields case SEEK_END: 420ef12e72aSJ. Bruce Fields fl->fl_start = i_size_read(file_inode(filp)); 421ef12e72aSJ. Bruce Fields break; 422ef12e72aSJ. Bruce Fields default: 423ef12e72aSJ. Bruce Fields return -EINVAL; 424ef12e72aSJ. Bruce Fields } 425ef12e72aSJ. Bruce Fields if (l->l_start > OFFSET_MAX - fl->fl_start) 426ef12e72aSJ. Bruce Fields return -EOVERFLOW; 427ef12e72aSJ. Bruce Fields fl->fl_start += l->l_start; 428ef12e72aSJ. Bruce Fields if (fl->fl_start < 0) 429ef12e72aSJ. Bruce Fields return -EINVAL; 430ef12e72aSJ. Bruce Fields 431ef12e72aSJ. Bruce Fields /* POSIX-1996 leaves the case l->l_len < 0 undefined; 432ef12e72aSJ. Bruce Fields POSIX-2001 defines it. */ 433ef12e72aSJ. Bruce Fields if (l->l_len > 0) { 434ef12e72aSJ. Bruce Fields if (l->l_len - 1 > OFFSET_MAX - fl->fl_start) 435ef12e72aSJ. Bruce Fields return -EOVERFLOW; 436ef12e72aSJ. Bruce Fields fl->fl_end = fl->fl_start + l->l_len - 1; 437ef12e72aSJ. Bruce Fields 438ef12e72aSJ. Bruce Fields } else if (l->l_len < 0) { 439ef12e72aSJ. Bruce Fields if (fl->fl_start + l->l_len < 0) 440ef12e72aSJ. Bruce Fields return -EINVAL; 441ef12e72aSJ. Bruce Fields fl->fl_end = fl->fl_start - 1; 442ef12e72aSJ. Bruce Fields fl->fl_start += l->l_len; 443ef12e72aSJ. Bruce Fields } else 444ef12e72aSJ. Bruce Fields fl->fl_end = OFFSET_MAX; 445ef12e72aSJ. Bruce Fields 446ef12e72aSJ. Bruce Fields fl->fl_owner = current->files; 447ef12e72aSJ. Bruce Fields fl->fl_pid = current->tgid; 448ef12e72aSJ. Bruce Fields fl->fl_file = filp; 449ef12e72aSJ. Bruce Fields fl->fl_flags = FL_POSIX; 450ef12e72aSJ. Bruce Fields fl->fl_ops = NULL; 451ef12e72aSJ. Bruce Fields fl->fl_lmops = NULL; 452ef12e72aSJ. Bruce Fields 453ef12e72aSJ. Bruce Fields return assign_type(fl, l->l_type); 454ef12e72aSJ. Bruce Fields } 455ef12e72aSJ. Bruce Fields 4561da177e4SLinus Torvalds /* Verify a "struct flock" and copy it to a "struct file_lock" as a POSIX 4571da177e4SLinus Torvalds * style lock. 4581da177e4SLinus Torvalds */ 4591da177e4SLinus Torvalds static int flock_to_posix_lock(struct file *filp, struct file_lock *fl, 4601da177e4SLinus Torvalds struct flock *l) 4611da177e4SLinus Torvalds { 462ef12e72aSJ. Bruce Fields struct flock64 ll = { 463ef12e72aSJ. Bruce Fields .l_type = l->l_type, 464ef12e72aSJ. Bruce Fields .l_whence = l->l_whence, 465ef12e72aSJ. Bruce Fields .l_start = l->l_start, 466ef12e72aSJ. Bruce Fields .l_len = l->l_len, 467ef12e72aSJ. Bruce Fields }; 4681da177e4SLinus Torvalds 469ef12e72aSJ. Bruce Fields return flock64_to_posix_lock(filp, fl, &ll); 4701da177e4SLinus Torvalds } 4711da177e4SLinus Torvalds 4721da177e4SLinus Torvalds /* default lease lock manager operations */ 4734d01b7f5SJeff Layton static bool 4744d01b7f5SJeff Layton lease_break_callback(struct file_lock *fl) 4751da177e4SLinus Torvalds { 4761da177e4SLinus Torvalds kill_fasync(&fl->fl_fasync, SIGIO, POLL_MSG); 4774d01b7f5SJeff Layton return false; 4781da177e4SLinus Torvalds } 4791da177e4SLinus Torvalds 4801c7dd2ffSJeff Layton static void 4811c7dd2ffSJeff Layton lease_setup(struct file_lock *fl, void **priv) 4821c7dd2ffSJeff Layton { 4831c7dd2ffSJeff Layton struct file *filp = fl->fl_file; 4841c7dd2ffSJeff Layton struct fasync_struct *fa = *priv; 4851c7dd2ffSJeff Layton 4861c7dd2ffSJeff Layton /* 4871c7dd2ffSJeff Layton * fasync_insert_entry() returns the old entry if any. If there was no 4881c7dd2ffSJeff Layton * old entry, then it used "priv" and inserted it into the fasync list. 4891c7dd2ffSJeff Layton * Clear the pointer to indicate that it shouldn't be freed. 4901c7dd2ffSJeff Layton */ 4911c7dd2ffSJeff Layton if (!fasync_insert_entry(fa->fa_fd, filp, &fl->fl_fasync, fa)) 4921c7dd2ffSJeff Layton *priv = NULL; 4931c7dd2ffSJeff Layton 4941c7dd2ffSJeff Layton __f_setown(filp, task_pid(current), PIDTYPE_PID, 0); 4951c7dd2ffSJeff Layton } 4961c7dd2ffSJeff Layton 4977b021967SAlexey Dobriyan static const struct lock_manager_operations lease_manager_ops = { 4988fb47a4fSJ. Bruce Fields .lm_break = lease_break_callback, 4998fb47a4fSJ. Bruce Fields .lm_change = lease_modify, 5001c7dd2ffSJeff Layton .lm_setup = lease_setup, 5011da177e4SLinus Torvalds }; 5021da177e4SLinus Torvalds 5031da177e4SLinus Torvalds /* 5041da177e4SLinus Torvalds * Initialize a lease, use the default lock manager operations 5051da177e4SLinus Torvalds */ 5060ec4f431SJ. Bruce Fields static int lease_init(struct file *filp, long type, struct file_lock *fl) 5071da177e4SLinus Torvalds { 50875dff55aSTrond Myklebust if (assign_type(fl, type) != 0) 50975dff55aSTrond Myklebust return -EINVAL; 51075dff55aSTrond Myklebust 5117ca76311SJeff Layton fl->fl_owner = filp; 5121da177e4SLinus Torvalds fl->fl_pid = current->tgid; 5131da177e4SLinus Torvalds 5141da177e4SLinus Torvalds fl->fl_file = filp; 5151da177e4SLinus Torvalds fl->fl_flags = FL_LEASE; 5161da177e4SLinus Torvalds fl->fl_start = 0; 5171da177e4SLinus Torvalds fl->fl_end = OFFSET_MAX; 5181da177e4SLinus Torvalds fl->fl_ops = NULL; 5191da177e4SLinus Torvalds fl->fl_lmops = &lease_manager_ops; 5201da177e4SLinus Torvalds return 0; 5211da177e4SLinus Torvalds } 5221da177e4SLinus Torvalds 5231da177e4SLinus Torvalds /* Allocate a file_lock initialised to this type of lease */ 5240ec4f431SJ. Bruce Fields static struct file_lock *lease_alloc(struct file *filp, long type) 5251da177e4SLinus Torvalds { 5261da177e4SLinus Torvalds struct file_lock *fl = locks_alloc_lock(); 52775dff55aSTrond Myklebust int error = -ENOMEM; 5281da177e4SLinus Torvalds 5291da177e4SLinus Torvalds if (fl == NULL) 530e32b8ee2SJ. Bruce Fields return ERR_PTR(error); 5311da177e4SLinus Torvalds 5321da177e4SLinus Torvalds error = lease_init(filp, type, fl); 53375dff55aSTrond Myklebust if (error) { 53475dff55aSTrond Myklebust locks_free_lock(fl); 535e32b8ee2SJ. Bruce Fields return ERR_PTR(error); 53675dff55aSTrond Myklebust } 537e32b8ee2SJ. Bruce Fields return fl; 5381da177e4SLinus Torvalds } 5391da177e4SLinus Torvalds 5401da177e4SLinus Torvalds /* Check if two locks overlap each other. 5411da177e4SLinus Torvalds */ 5421da177e4SLinus Torvalds static inline int locks_overlap(struct file_lock *fl1, struct file_lock *fl2) 5431da177e4SLinus Torvalds { 5441da177e4SLinus Torvalds return ((fl1->fl_end >= fl2->fl_start) && 5451da177e4SLinus Torvalds (fl2->fl_end >= fl1->fl_start)); 5461da177e4SLinus Torvalds } 5471da177e4SLinus Torvalds 5481da177e4SLinus Torvalds /* 5491da177e4SLinus Torvalds * Check whether two locks have the same owner. 5501da177e4SLinus Torvalds */ 55133443c42SMatt Mackall static int posix_same_owner(struct file_lock *fl1, struct file_lock *fl2) 5521da177e4SLinus Torvalds { 5538fb47a4fSJ. Bruce Fields if (fl1->fl_lmops && fl1->fl_lmops->lm_compare_owner) 5541da177e4SLinus Torvalds return fl2->fl_lmops == fl1->fl_lmops && 5558fb47a4fSJ. Bruce Fields fl1->fl_lmops->lm_compare_owner(fl1, fl2); 5561da177e4SLinus Torvalds return fl1->fl_owner == fl2->fl_owner; 5571da177e4SLinus Torvalds } 5581da177e4SLinus Torvalds 5596109c850SJeff Layton /* Must be called with the flc_lock held! */ 5606ca10ed8SJeff Layton static void locks_insert_global_locks(struct file_lock *fl) 56188974691SJeff Layton { 5627012b02aSJeff Layton lg_local_lock(&file_lock_lglock); 5637012b02aSJeff Layton fl->fl_link_cpu = smp_processor_id(); 5647012b02aSJeff Layton hlist_add_head(&fl->fl_link, this_cpu_ptr(&file_lock_list)); 5657012b02aSJeff Layton lg_local_unlock(&file_lock_lglock); 56688974691SJeff Layton } 56788974691SJeff Layton 5686109c850SJeff Layton /* Must be called with the flc_lock held! */ 5696ca10ed8SJeff Layton static void locks_delete_global_locks(struct file_lock *fl) 57088974691SJeff Layton { 5717012b02aSJeff Layton /* 5727012b02aSJeff Layton * Avoid taking lock if already unhashed. This is safe since this check 5736109c850SJeff Layton * is done while holding the flc_lock, and new insertions into the list 5747012b02aSJeff Layton * also require that it be held. 5757012b02aSJeff Layton */ 5767012b02aSJeff Layton if (hlist_unhashed(&fl->fl_link)) 5777012b02aSJeff Layton return; 5787012b02aSJeff Layton lg_local_lock_cpu(&file_lock_lglock, fl->fl_link_cpu); 579139ca04eSJeff Layton hlist_del_init(&fl->fl_link); 5807012b02aSJeff Layton lg_local_unlock_cpu(&file_lock_lglock, fl->fl_link_cpu); 58188974691SJeff Layton } 58288974691SJeff Layton 5833999e493SJeff Layton static unsigned long 5843999e493SJeff Layton posix_owner_key(struct file_lock *fl) 5853999e493SJeff Layton { 5863999e493SJeff Layton if (fl->fl_lmops && fl->fl_lmops->lm_owner_key) 5873999e493SJeff Layton return fl->fl_lmops->lm_owner_key(fl); 5883999e493SJeff Layton return (unsigned long)fl->fl_owner; 5893999e493SJeff Layton } 5903999e493SJeff Layton 5916ca10ed8SJeff Layton static void locks_insert_global_blocked(struct file_lock *waiter) 59288974691SJeff Layton { 593663d5af7SDaniel Wagner lockdep_assert_held(&blocked_lock_lock); 594663d5af7SDaniel Wagner 5953999e493SJeff Layton hash_add(blocked_hash, &waiter->fl_link, posix_owner_key(waiter)); 59688974691SJeff Layton } 59788974691SJeff Layton 5986ca10ed8SJeff Layton static void locks_delete_global_blocked(struct file_lock *waiter) 59988974691SJeff Layton { 600663d5af7SDaniel Wagner lockdep_assert_held(&blocked_lock_lock); 601663d5af7SDaniel Wagner 60248f74186SJeff Layton hash_del(&waiter->fl_link); 60388974691SJeff Layton } 60488974691SJeff Layton 6051da177e4SLinus Torvalds /* Remove waiter from blocker's block list. 6061da177e4SLinus Torvalds * When blocker ends up pointing to itself then the list is empty. 6071c8c601aSJeff Layton * 6087b2296afSJeff Layton * Must be called with blocked_lock_lock held. 6091da177e4SLinus Torvalds */ 61033443c42SMatt Mackall static void __locks_delete_block(struct file_lock *waiter) 6111da177e4SLinus Torvalds { 61288974691SJeff Layton locks_delete_global_blocked(waiter); 6131da177e4SLinus Torvalds list_del_init(&waiter->fl_block); 6141da177e4SLinus Torvalds waiter->fl_next = NULL; 6151da177e4SLinus Torvalds } 6161da177e4SLinus Torvalds 6171a9e64a7SJeff Layton static void locks_delete_block(struct file_lock *waiter) 6181da177e4SLinus Torvalds { 6197b2296afSJeff Layton spin_lock(&blocked_lock_lock); 6201da177e4SLinus Torvalds __locks_delete_block(waiter); 6217b2296afSJeff Layton spin_unlock(&blocked_lock_lock); 6221da177e4SLinus Torvalds } 6231da177e4SLinus Torvalds 6241da177e4SLinus Torvalds /* Insert waiter into blocker's block list. 6251da177e4SLinus Torvalds * We use a circular list so that processes can be easily woken up in 6261da177e4SLinus Torvalds * the order they blocked. The documentation doesn't require this but 6271da177e4SLinus Torvalds * it seems like the reasonable thing to do. 6281c8c601aSJeff Layton * 6296109c850SJeff Layton * Must be called with both the flc_lock and blocked_lock_lock held. The 6306109c850SJeff Layton * fl_block list itself is protected by the blocked_lock_lock, but by ensuring 6316109c850SJeff Layton * that the flc_lock is also held on insertions we can avoid taking the 6326109c850SJeff Layton * blocked_lock_lock in some cases when we see that the fl_block list is empty. 6331da177e4SLinus Torvalds */ 6341c8c601aSJeff Layton static void __locks_insert_block(struct file_lock *blocker, 6351da177e4SLinus Torvalds struct file_lock *waiter) 6361da177e4SLinus Torvalds { 6376dc0fe8fSJ. Bruce Fields BUG_ON(!list_empty(&waiter->fl_block)); 6381da177e4SLinus Torvalds waiter->fl_next = blocker; 63988974691SJeff Layton list_add_tail(&waiter->fl_block, &blocker->fl_block); 640cff2fce5SJeff Layton if (IS_POSIX(blocker) && !IS_OFDLCK(blocker)) 6411c8c601aSJeff Layton locks_insert_global_blocked(waiter); 6421c8c601aSJeff Layton } 6431c8c601aSJeff Layton 6446109c850SJeff Layton /* Must be called with flc_lock held. */ 6451c8c601aSJeff Layton static void locks_insert_block(struct file_lock *blocker, 6461c8c601aSJeff Layton struct file_lock *waiter) 6471c8c601aSJeff Layton { 6487b2296afSJeff Layton spin_lock(&blocked_lock_lock); 6491c8c601aSJeff Layton __locks_insert_block(blocker, waiter); 6507b2296afSJeff Layton spin_unlock(&blocked_lock_lock); 6511da177e4SLinus Torvalds } 6521da177e4SLinus Torvalds 6531cb36012SJeff Layton /* 6541cb36012SJeff Layton * Wake up processes blocked waiting for blocker. 6551cb36012SJeff Layton * 6566109c850SJeff Layton * Must be called with the inode->flc_lock held! 6571da177e4SLinus Torvalds */ 6581da177e4SLinus Torvalds static void locks_wake_up_blocks(struct file_lock *blocker) 6591da177e4SLinus Torvalds { 6604e8c765dSJeff Layton /* 6614e8c765dSJeff Layton * Avoid taking global lock if list is empty. This is safe since new 6626109c850SJeff Layton * blocked requests are only added to the list under the flc_lock, and 6636109c850SJeff Layton * the flc_lock is always held here. Note that removal from the fl_block 6646109c850SJeff Layton * list does not require the flc_lock, so we must recheck list_empty() 6657b2296afSJeff Layton * after acquiring the blocked_lock_lock. 6664e8c765dSJeff Layton */ 6674e8c765dSJeff Layton if (list_empty(&blocker->fl_block)) 6684e8c765dSJeff Layton return; 6694e8c765dSJeff Layton 6707b2296afSJeff Layton spin_lock(&blocked_lock_lock); 6711da177e4SLinus Torvalds while (!list_empty(&blocker->fl_block)) { 672f0c1cd0eSPavel Emelyanov struct file_lock *waiter; 673f0c1cd0eSPavel Emelyanov 674f0c1cd0eSPavel Emelyanov waiter = list_first_entry(&blocker->fl_block, 6751da177e4SLinus Torvalds struct file_lock, fl_block); 6761da177e4SLinus Torvalds __locks_delete_block(waiter); 6778fb47a4fSJ. Bruce Fields if (waiter->fl_lmops && waiter->fl_lmops->lm_notify) 6788fb47a4fSJ. Bruce Fields waiter->fl_lmops->lm_notify(waiter); 6791da177e4SLinus Torvalds else 6801da177e4SLinus Torvalds wake_up(&waiter->fl_wait); 6811da177e4SLinus Torvalds } 6827b2296afSJeff Layton spin_unlock(&blocked_lock_lock); 6831da177e4SLinus Torvalds } 6841da177e4SLinus Torvalds 6855263e31eSJeff Layton static void 686e084c1bdSJeff Layton locks_insert_lock_ctx(struct file_lock *fl, struct list_head *before) 6875263e31eSJeff Layton { 6885263e31eSJeff Layton fl->fl_nspid = get_pid(task_tgid(current)); 6895263e31eSJeff Layton list_add_tail(&fl->fl_list, before); 6905263e31eSJeff Layton locks_insert_global_locks(fl); 6915263e31eSJeff Layton } 6925263e31eSJeff Layton 6938634b51fSJeff Layton static void 694e084c1bdSJeff Layton locks_unlink_lock_ctx(struct file_lock *fl) 6951da177e4SLinus Torvalds { 69688974691SJeff Layton locks_delete_global_locks(fl); 6978634b51fSJeff Layton list_del_init(&fl->fl_list); 698ab1f1611SVitaliy Gusev if (fl->fl_nspid) { 699ab1f1611SVitaliy Gusev put_pid(fl->fl_nspid); 700ab1f1611SVitaliy Gusev fl->fl_nspid = NULL; 701ab1f1611SVitaliy Gusev } 7021da177e4SLinus Torvalds locks_wake_up_blocks(fl); 70324cbe784SJeff Layton } 70424cbe784SJeff Layton 7055263e31eSJeff Layton static void 706e084c1bdSJeff Layton locks_delete_lock_ctx(struct file_lock *fl, struct list_head *dispose) 7075263e31eSJeff Layton { 708e084c1bdSJeff Layton locks_unlink_lock_ctx(fl); 7098634b51fSJeff Layton if (dispose) 7108634b51fSJeff Layton list_add(&fl->fl_list, dispose); 7118634b51fSJeff Layton else 7128634b51fSJeff Layton locks_free_lock(fl); 7135263e31eSJeff Layton } 7145263e31eSJeff Layton 7151da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. Common functionality 7161da177e4SLinus Torvalds * checks for shared/exclusive status of overlapping locks. 7171da177e4SLinus Torvalds */ 7181da177e4SLinus Torvalds static int locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl) 7191da177e4SLinus Torvalds { 7201da177e4SLinus Torvalds if (sys_fl->fl_type == F_WRLCK) 7211da177e4SLinus Torvalds return 1; 7221da177e4SLinus Torvalds if (caller_fl->fl_type == F_WRLCK) 7231da177e4SLinus Torvalds return 1; 7241da177e4SLinus Torvalds return 0; 7251da177e4SLinus Torvalds } 7261da177e4SLinus Torvalds 7271da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. POSIX specific 7281da177e4SLinus Torvalds * checking before calling the locks_conflict(). 7291da177e4SLinus Torvalds */ 7301da177e4SLinus Torvalds static int posix_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl) 7311da177e4SLinus Torvalds { 7321da177e4SLinus Torvalds /* POSIX locks owned by the same process do not conflict with 7331da177e4SLinus Torvalds * each other. 7341da177e4SLinus Torvalds */ 7359b8c8695SJeff Layton if (posix_same_owner(caller_fl, sys_fl)) 7361da177e4SLinus Torvalds return (0); 7371da177e4SLinus Torvalds 7381da177e4SLinus Torvalds /* Check whether they overlap */ 7391da177e4SLinus Torvalds if (!locks_overlap(caller_fl, sys_fl)) 7401da177e4SLinus Torvalds return 0; 7411da177e4SLinus Torvalds 7421da177e4SLinus Torvalds return (locks_conflict(caller_fl, sys_fl)); 7431da177e4SLinus Torvalds } 7441da177e4SLinus Torvalds 7451da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. FLOCK specific 7461da177e4SLinus Torvalds * checking before calling the locks_conflict(). 7471da177e4SLinus Torvalds */ 7481da177e4SLinus Torvalds static int flock_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl) 7491da177e4SLinus Torvalds { 7501da177e4SLinus Torvalds /* FLOCK locks referring to the same filp do not conflict with 7511da177e4SLinus Torvalds * each other. 7521da177e4SLinus Torvalds */ 7539b8c8695SJeff Layton if (caller_fl->fl_file == sys_fl->fl_file) 7541da177e4SLinus Torvalds return (0); 7551da177e4SLinus Torvalds if ((caller_fl->fl_type & LOCK_MAND) || (sys_fl->fl_type & LOCK_MAND)) 7561da177e4SLinus Torvalds return 0; 7571da177e4SLinus Torvalds 7581da177e4SLinus Torvalds return (locks_conflict(caller_fl, sys_fl)); 7591da177e4SLinus Torvalds } 7601da177e4SLinus Torvalds 7616d34ac19SJ. Bruce Fields void 7629d6a8c5cSMarc Eshel posix_test_lock(struct file *filp, struct file_lock *fl) 7631da177e4SLinus Torvalds { 7641da177e4SLinus Torvalds struct file_lock *cfl; 765bd61e0a9SJeff Layton struct file_lock_context *ctx; 7661c8c601aSJeff Layton struct inode *inode = file_inode(filp); 7671da177e4SLinus Torvalds 768128a3785SDmitry Vyukov ctx = smp_load_acquire(&inode->i_flctx); 769bd61e0a9SJeff Layton if (!ctx || list_empty_careful(&ctx->flc_posix)) { 770bd61e0a9SJeff Layton fl->fl_type = F_UNLCK; 771bd61e0a9SJeff Layton return; 7721da177e4SLinus Torvalds } 773bd61e0a9SJeff Layton 7746109c850SJeff Layton spin_lock(&ctx->flc_lock); 775bd61e0a9SJeff Layton list_for_each_entry(cfl, &ctx->flc_posix, fl_list) { 776bd61e0a9SJeff Layton if (posix_locks_conflict(fl, cfl)) { 7773fe0fff1SKinglong Mee locks_copy_conflock(fl, cfl); 778ab1f1611SVitaliy Gusev if (cfl->fl_nspid) 7796c5f3e7bSPavel Emelyanov fl->fl_pid = pid_vnr(cfl->fl_nspid); 780bd61e0a9SJeff Layton goto out; 781bd61e0a9SJeff Layton } 782bd61e0a9SJeff Layton } 783129a84deSJ. Bruce Fields fl->fl_type = F_UNLCK; 784bd61e0a9SJeff Layton out: 7856109c850SJeff Layton spin_unlock(&ctx->flc_lock); 7866d34ac19SJ. Bruce Fields return; 7871da177e4SLinus Torvalds } 7881da177e4SLinus Torvalds EXPORT_SYMBOL(posix_test_lock); 7891da177e4SLinus Torvalds 790b533184fSJ. Bruce Fields /* 791b533184fSJ. Bruce Fields * Deadlock detection: 7921da177e4SLinus Torvalds * 793b533184fSJ. Bruce Fields * We attempt to detect deadlocks that are due purely to posix file 794b533184fSJ. Bruce Fields * locks. 7951da177e4SLinus Torvalds * 796b533184fSJ. Bruce Fields * We assume that a task can be waiting for at most one lock at a time. 797b533184fSJ. Bruce Fields * So for any acquired lock, the process holding that lock may be 798b533184fSJ. Bruce Fields * waiting on at most one other lock. That lock in turns may be held by 799b533184fSJ. Bruce Fields * someone waiting for at most one other lock. Given a requested lock 800b533184fSJ. Bruce Fields * caller_fl which is about to wait for a conflicting lock block_fl, we 801b533184fSJ. Bruce Fields * follow this chain of waiters to ensure we are not about to create a 802b533184fSJ. Bruce Fields * cycle. 80397855b49SJ. Bruce Fields * 804b533184fSJ. Bruce Fields * Since we do this before we ever put a process to sleep on a lock, we 805b533184fSJ. Bruce Fields * are ensured that there is never a cycle; that is what guarantees that 806b533184fSJ. Bruce Fields * the while() loop in posix_locks_deadlock() eventually completes. 807b533184fSJ. Bruce Fields * 808b533184fSJ. Bruce Fields * Note: the above assumption may not be true when handling lock 809b533184fSJ. Bruce Fields * requests from a broken NFS client. It may also fail in the presence 810b533184fSJ. Bruce Fields * of tasks (such as posix threads) sharing the same open file table. 811b533184fSJ. Bruce Fields * To handle those cases, we just bail out after a few iterations. 81257b65325SJeff Layton * 813cff2fce5SJeff Layton * For FL_OFDLCK locks, the owner is the filp, not the files_struct. 81457b65325SJeff Layton * Because the owner is not even nominally tied to a thread of 81557b65325SJeff Layton * execution, the deadlock detection below can't reasonably work well. Just 81657b65325SJeff Layton * skip it for those. 81757b65325SJeff Layton * 818cff2fce5SJeff Layton * In principle, we could do a more limited deadlock detection on FL_OFDLCK 81957b65325SJeff Layton * locks that just checks for the case where two tasks are attempting to 82057b65325SJeff Layton * upgrade from read to write locks on the same inode. 8211da177e4SLinus Torvalds */ 82297855b49SJ. Bruce Fields 82397855b49SJ. Bruce Fields #define MAX_DEADLK_ITERATIONS 10 82497855b49SJ. Bruce Fields 825b533184fSJ. Bruce Fields /* Find a lock that the owner of the given block_fl is blocking on. */ 826b533184fSJ. Bruce Fields static struct file_lock *what_owner_is_waiting_for(struct file_lock *block_fl) 827b533184fSJ. Bruce Fields { 828b533184fSJ. Bruce Fields struct file_lock *fl; 829b533184fSJ. Bruce Fields 8303999e493SJeff Layton hash_for_each_possible(blocked_hash, fl, fl_link, posix_owner_key(block_fl)) { 831b533184fSJ. Bruce Fields if (posix_same_owner(fl, block_fl)) 832b533184fSJ. Bruce Fields return fl->fl_next; 833b533184fSJ. Bruce Fields } 834b533184fSJ. Bruce Fields return NULL; 835b533184fSJ. Bruce Fields } 836b533184fSJ. Bruce Fields 8377b2296afSJeff Layton /* Must be called with the blocked_lock_lock held! */ 838b0904e14SAdrian Bunk static int posix_locks_deadlock(struct file_lock *caller_fl, 8391da177e4SLinus Torvalds struct file_lock *block_fl) 8401da177e4SLinus Torvalds { 84197855b49SJ. Bruce Fields int i = 0; 8421da177e4SLinus Torvalds 843663d5af7SDaniel Wagner lockdep_assert_held(&blocked_lock_lock); 844663d5af7SDaniel Wagner 84557b65325SJeff Layton /* 84657b65325SJeff Layton * This deadlock detector can't reasonably detect deadlocks with 847cff2fce5SJeff Layton * FL_OFDLCK locks, since they aren't owned by a process, per-se. 84857b65325SJeff Layton */ 849cff2fce5SJeff Layton if (IS_OFDLCK(caller_fl)) 85057b65325SJeff Layton return 0; 85157b65325SJeff Layton 852b533184fSJ. Bruce Fields while ((block_fl = what_owner_is_waiting_for(block_fl))) { 85397855b49SJ. Bruce Fields if (i++ > MAX_DEADLK_ITERATIONS) 85497855b49SJ. Bruce Fields return 0; 855b533184fSJ. Bruce Fields if (posix_same_owner(caller_fl, block_fl)) 856b533184fSJ. Bruce Fields return 1; 8571da177e4SLinus Torvalds } 8581da177e4SLinus Torvalds return 0; 8591da177e4SLinus Torvalds } 8601da177e4SLinus Torvalds 8611da177e4SLinus Torvalds /* Try to create a FLOCK lock on filp. We always insert new FLOCK locks 86202888f41SJ. Bruce Fields * after any leases, but before any posix locks. 863f475ae95STrond Myklebust * 864f475ae95STrond Myklebust * Note that if called with an FL_EXISTS argument, the caller may determine 865f475ae95STrond Myklebust * whether or not a lock was successfully freed by testing the return 866f475ae95STrond Myklebust * value for -ENOENT. 8671da177e4SLinus Torvalds */ 868bcd7f78dSJeff Layton static int flock_lock_inode(struct inode *inode, struct file_lock *request) 8691da177e4SLinus Torvalds { 870993dfa87STrond Myklebust struct file_lock *new_fl = NULL; 8715263e31eSJeff Layton struct file_lock *fl; 8725263e31eSJeff Layton struct file_lock_context *ctx; 8731da177e4SLinus Torvalds int error = 0; 8745263e31eSJeff Layton bool found = false; 875ed9814d8SJeff Layton LIST_HEAD(dispose); 8761da177e4SLinus Torvalds 8775c1c669aSJeff Layton ctx = locks_get_lock_context(inode, request->fl_type); 8785c1c669aSJeff Layton if (!ctx) { 8795c1c669aSJeff Layton if (request->fl_type != F_UNLCK) 8805263e31eSJeff Layton return -ENOMEM; 8815c1c669aSJeff Layton return (request->fl_flags & FL_EXISTS) ? -ENOENT : 0; 8825c1c669aSJeff Layton } 8835263e31eSJeff Layton 884b89f4321SArnd Bergmann if (!(request->fl_flags & FL_ACCESS) && (request->fl_type != F_UNLCK)) { 885b89f4321SArnd Bergmann new_fl = locks_alloc_lock(); 886b89f4321SArnd Bergmann if (!new_fl) 887b89f4321SArnd Bergmann return -ENOMEM; 888b89f4321SArnd Bergmann } 889b89f4321SArnd Bergmann 8906109c850SJeff Layton spin_lock(&ctx->flc_lock); 891f07f18ddSTrond Myklebust if (request->fl_flags & FL_ACCESS) 892f07f18ddSTrond Myklebust goto find_conflict; 89384d535adSPavel Emelyanov 8945263e31eSJeff Layton list_for_each_entry(fl, &ctx->flc_flock, fl_list) { 895bcd7f78dSJeff Layton if (request->fl_file != fl->fl_file) 8961da177e4SLinus Torvalds continue; 897993dfa87STrond Myklebust if (request->fl_type == fl->fl_type) 8981da177e4SLinus Torvalds goto out; 8995263e31eSJeff Layton found = true; 900e084c1bdSJeff Layton locks_delete_lock_ctx(fl, &dispose); 9011da177e4SLinus Torvalds break; 9021da177e4SLinus Torvalds } 9031da177e4SLinus Torvalds 904f475ae95STrond Myklebust if (request->fl_type == F_UNLCK) { 905f475ae95STrond Myklebust if ((request->fl_flags & FL_EXISTS) && !found) 906f475ae95STrond Myklebust error = -ENOENT; 907993dfa87STrond Myklebust goto out; 908f475ae95STrond Myklebust } 9091da177e4SLinus Torvalds 910f07f18ddSTrond Myklebust find_conflict: 9115263e31eSJeff Layton list_for_each_entry(fl, &ctx->flc_flock, fl_list) { 912993dfa87STrond Myklebust if (!flock_locks_conflict(request, fl)) 9131da177e4SLinus Torvalds continue; 9141da177e4SLinus Torvalds error = -EAGAIN; 915bde74e4bSMiklos Szeredi if (!(request->fl_flags & FL_SLEEP)) 916bde74e4bSMiklos Szeredi goto out; 917bde74e4bSMiklos Szeredi error = FILE_LOCK_DEFERRED; 918993dfa87STrond Myklebust locks_insert_block(fl, request); 9191da177e4SLinus Torvalds goto out; 9201da177e4SLinus Torvalds } 921f07f18ddSTrond Myklebust if (request->fl_flags & FL_ACCESS) 922f07f18ddSTrond Myklebust goto out; 923993dfa87STrond Myklebust locks_copy_lock(new_fl, request); 924e084c1bdSJeff Layton locks_insert_lock_ctx(new_fl, &ctx->flc_flock); 925993dfa87STrond Myklebust new_fl = NULL; 9269cedc194SKirill Korotaev error = 0; 9271da177e4SLinus Torvalds 9281da177e4SLinus Torvalds out: 9296109c850SJeff Layton spin_unlock(&ctx->flc_lock); 930993dfa87STrond Myklebust if (new_fl) 931993dfa87STrond Myklebust locks_free_lock(new_fl); 932ed9814d8SJeff Layton locks_dispose_list(&dispose); 9331da177e4SLinus Torvalds return error; 9341da177e4SLinus Torvalds } 9351da177e4SLinus Torvalds 936150b3934SMarc Eshel static int __posix_lock_file(struct inode *inode, struct file_lock *request, struct file_lock *conflock) 9371da177e4SLinus Torvalds { 938bd61e0a9SJeff Layton struct file_lock *fl, *tmp; 93939005d02SMiklos Szeredi struct file_lock *new_fl = NULL; 94039005d02SMiklos Szeredi struct file_lock *new_fl2 = NULL; 9411da177e4SLinus Torvalds struct file_lock *left = NULL; 9421da177e4SLinus Torvalds struct file_lock *right = NULL; 943bd61e0a9SJeff Layton struct file_lock_context *ctx; 944b9746ef8SJeff Layton int error; 945b9746ef8SJeff Layton bool added = false; 946ed9814d8SJeff Layton LIST_HEAD(dispose); 9471da177e4SLinus Torvalds 9485c1c669aSJeff Layton ctx = locks_get_lock_context(inode, request->fl_type); 949bd61e0a9SJeff Layton if (!ctx) 9505c1c669aSJeff Layton return (request->fl_type == F_UNLCK) ? 0 : -ENOMEM; 951bd61e0a9SJeff Layton 9521da177e4SLinus Torvalds /* 9531da177e4SLinus Torvalds * We may need two file_lock structures for this operation, 9541da177e4SLinus Torvalds * so we get them in advance to avoid races. 95539005d02SMiklos Szeredi * 95639005d02SMiklos Szeredi * In some cases we can be sure, that no new locks will be needed 9571da177e4SLinus Torvalds */ 95839005d02SMiklos Szeredi if (!(request->fl_flags & FL_ACCESS) && 95939005d02SMiklos Szeredi (request->fl_type != F_UNLCK || 96039005d02SMiklos Szeredi request->fl_start != 0 || request->fl_end != OFFSET_MAX)) { 9611da177e4SLinus Torvalds new_fl = locks_alloc_lock(); 9621da177e4SLinus Torvalds new_fl2 = locks_alloc_lock(); 96339005d02SMiklos Szeredi } 9641da177e4SLinus Torvalds 9656109c850SJeff Layton spin_lock(&ctx->flc_lock); 9661cb36012SJeff Layton /* 9671cb36012SJeff Layton * New lock request. Walk all POSIX locks and look for conflicts. If 9681cb36012SJeff Layton * there are any, either return error or put the request on the 96948f74186SJeff Layton * blocker's list of waiters and the global blocked_hash. 9701cb36012SJeff Layton */ 9711da177e4SLinus Torvalds if (request->fl_type != F_UNLCK) { 972bd61e0a9SJeff Layton list_for_each_entry(fl, &ctx->flc_posix, fl_list) { 9731da177e4SLinus Torvalds if (!posix_locks_conflict(request, fl)) 9741da177e4SLinus Torvalds continue; 9755842add2SAndy Adamson if (conflock) 9763fe0fff1SKinglong Mee locks_copy_conflock(conflock, fl); 9771da177e4SLinus Torvalds error = -EAGAIN; 9781da177e4SLinus Torvalds if (!(request->fl_flags & FL_SLEEP)) 9791da177e4SLinus Torvalds goto out; 9801c8c601aSJeff Layton /* 9811c8c601aSJeff Layton * Deadlock detection and insertion into the blocked 9821c8c601aSJeff Layton * locks list must be done while holding the same lock! 9831c8c601aSJeff Layton */ 9841da177e4SLinus Torvalds error = -EDEADLK; 9857b2296afSJeff Layton spin_lock(&blocked_lock_lock); 9861c8c601aSJeff Layton if (likely(!posix_locks_deadlock(request, fl))) { 987bde74e4bSMiklos Szeredi error = FILE_LOCK_DEFERRED; 9881c8c601aSJeff Layton __locks_insert_block(fl, request); 9891c8c601aSJeff Layton } 9907b2296afSJeff Layton spin_unlock(&blocked_lock_lock); 9911da177e4SLinus Torvalds goto out; 9921da177e4SLinus Torvalds } 9931da177e4SLinus Torvalds } 9941da177e4SLinus Torvalds 9951da177e4SLinus Torvalds /* If we're just looking for a conflict, we're done. */ 9961da177e4SLinus Torvalds error = 0; 9971da177e4SLinus Torvalds if (request->fl_flags & FL_ACCESS) 9981da177e4SLinus Torvalds goto out; 9991da177e4SLinus Torvalds 1000bd61e0a9SJeff Layton /* Find the first old lock with the same owner as the new lock */ 1001bd61e0a9SJeff Layton list_for_each_entry(fl, &ctx->flc_posix, fl_list) { 1002bd61e0a9SJeff Layton if (posix_same_owner(request, fl)) 1003bd61e0a9SJeff Layton break; 10041da177e4SLinus Torvalds } 10051da177e4SLinus Torvalds 10061da177e4SLinus Torvalds /* Process locks with this owner. */ 1007bd61e0a9SJeff Layton list_for_each_entry_safe_from(fl, tmp, &ctx->flc_posix, fl_list) { 1008bd61e0a9SJeff Layton if (!posix_same_owner(request, fl)) 1009bd61e0a9SJeff Layton break; 1010bd61e0a9SJeff Layton 1011bd61e0a9SJeff Layton /* Detect adjacent or overlapping regions (if same lock type) */ 10121da177e4SLinus Torvalds if (request->fl_type == fl->fl_type) { 1013449231d6SOlaf Kirch /* In all comparisons of start vs end, use 1014449231d6SOlaf Kirch * "start - 1" rather than "end + 1". If end 1015449231d6SOlaf Kirch * is OFFSET_MAX, end + 1 will become negative. 1016449231d6SOlaf Kirch */ 10171da177e4SLinus Torvalds if (fl->fl_end < request->fl_start - 1) 1018bd61e0a9SJeff Layton continue; 10191da177e4SLinus Torvalds /* If the next lock in the list has entirely bigger 10201da177e4SLinus Torvalds * addresses than the new one, insert the lock here. 10211da177e4SLinus Torvalds */ 1022449231d6SOlaf Kirch if (fl->fl_start - 1 > request->fl_end) 10231da177e4SLinus Torvalds break; 10241da177e4SLinus Torvalds 10251da177e4SLinus Torvalds /* If we come here, the new and old lock are of the 10261da177e4SLinus Torvalds * same type and adjacent or overlapping. Make one 10271da177e4SLinus Torvalds * lock yielding from the lower start address of both 10281da177e4SLinus Torvalds * locks to the higher end address. 10291da177e4SLinus Torvalds */ 10301da177e4SLinus Torvalds if (fl->fl_start > request->fl_start) 10311da177e4SLinus Torvalds fl->fl_start = request->fl_start; 10321da177e4SLinus Torvalds else 10331da177e4SLinus Torvalds request->fl_start = fl->fl_start; 10341da177e4SLinus Torvalds if (fl->fl_end < request->fl_end) 10351da177e4SLinus Torvalds fl->fl_end = request->fl_end; 10361da177e4SLinus Torvalds else 10371da177e4SLinus Torvalds request->fl_end = fl->fl_end; 10381da177e4SLinus Torvalds if (added) { 1039e084c1bdSJeff Layton locks_delete_lock_ctx(fl, &dispose); 10401da177e4SLinus Torvalds continue; 10411da177e4SLinus Torvalds } 10421da177e4SLinus Torvalds request = fl; 1043b9746ef8SJeff Layton added = true; 1044bd61e0a9SJeff Layton } else { 10451da177e4SLinus Torvalds /* Processing for different lock types is a bit 10461da177e4SLinus Torvalds * more complex. 10471da177e4SLinus Torvalds */ 10481da177e4SLinus Torvalds if (fl->fl_end < request->fl_start) 1049bd61e0a9SJeff Layton continue; 10501da177e4SLinus Torvalds if (fl->fl_start > request->fl_end) 10511da177e4SLinus Torvalds break; 10521da177e4SLinus Torvalds if (request->fl_type == F_UNLCK) 1053b9746ef8SJeff Layton added = true; 10541da177e4SLinus Torvalds if (fl->fl_start < request->fl_start) 10551da177e4SLinus Torvalds left = fl; 10561da177e4SLinus Torvalds /* If the next lock in the list has a higher end 10571da177e4SLinus Torvalds * address than the new one, insert the new one here. 10581da177e4SLinus Torvalds */ 10591da177e4SLinus Torvalds if (fl->fl_end > request->fl_end) { 10601da177e4SLinus Torvalds right = fl; 10611da177e4SLinus Torvalds break; 10621da177e4SLinus Torvalds } 10631da177e4SLinus Torvalds if (fl->fl_start >= request->fl_start) { 10641da177e4SLinus Torvalds /* The new lock completely replaces an old 10651da177e4SLinus Torvalds * one (This may happen several times). 10661da177e4SLinus Torvalds */ 10671da177e4SLinus Torvalds if (added) { 1068e084c1bdSJeff Layton locks_delete_lock_ctx(fl, &dispose); 10691da177e4SLinus Torvalds continue; 10701da177e4SLinus Torvalds } 1071b84d49f9SJeff Layton /* 1072b84d49f9SJeff Layton * Replace the old lock with new_fl, and 1073b84d49f9SJeff Layton * remove the old one. It's safe to do the 1074b84d49f9SJeff Layton * insert here since we know that we won't be 1075b84d49f9SJeff Layton * using new_fl later, and that the lock is 1076b84d49f9SJeff Layton * just replacing an existing lock. 10771da177e4SLinus Torvalds */ 1078b84d49f9SJeff Layton error = -ENOLCK; 1079b84d49f9SJeff Layton if (!new_fl) 1080b84d49f9SJeff Layton goto out; 1081b84d49f9SJeff Layton locks_copy_lock(new_fl, request); 1082b84d49f9SJeff Layton request = new_fl; 1083b84d49f9SJeff Layton new_fl = NULL; 1084e084c1bdSJeff Layton locks_insert_lock_ctx(request, &fl->fl_list); 1085e084c1bdSJeff Layton locks_delete_lock_ctx(fl, &dispose); 1086b9746ef8SJeff Layton added = true; 10871da177e4SLinus Torvalds } 10881da177e4SLinus Torvalds } 10891da177e4SLinus Torvalds } 10901da177e4SLinus Torvalds 10910d9a490aSMiklos Szeredi /* 10921cb36012SJeff Layton * The above code only modifies existing locks in case of merging or 10931cb36012SJeff Layton * replacing. If new lock(s) need to be inserted all modifications are 10941cb36012SJeff Layton * done below this, so it's safe yet to bail out. 10950d9a490aSMiklos Szeredi */ 10960d9a490aSMiklos Szeredi error = -ENOLCK; /* "no luck" */ 10970d9a490aSMiklos Szeredi if (right && left == right && !new_fl2) 10980d9a490aSMiklos Szeredi goto out; 10990d9a490aSMiklos Szeredi 11001da177e4SLinus Torvalds error = 0; 11011da177e4SLinus Torvalds if (!added) { 1102f475ae95STrond Myklebust if (request->fl_type == F_UNLCK) { 1103f475ae95STrond Myklebust if (request->fl_flags & FL_EXISTS) 1104f475ae95STrond Myklebust error = -ENOENT; 11051da177e4SLinus Torvalds goto out; 1106f475ae95STrond Myklebust } 11070d9a490aSMiklos Szeredi 11080d9a490aSMiklos Szeredi if (!new_fl) { 11090d9a490aSMiklos Szeredi error = -ENOLCK; 11100d9a490aSMiklos Szeredi goto out; 11110d9a490aSMiklos Szeredi } 11121da177e4SLinus Torvalds locks_copy_lock(new_fl, request); 1113e084c1bdSJeff Layton locks_insert_lock_ctx(new_fl, &fl->fl_list); 11142e2f756fSJeff Layton fl = new_fl; 11151da177e4SLinus Torvalds new_fl = NULL; 11161da177e4SLinus Torvalds } 11171da177e4SLinus Torvalds if (right) { 11181da177e4SLinus Torvalds if (left == right) { 11191da177e4SLinus Torvalds /* The new lock breaks the old one in two pieces, 11201da177e4SLinus Torvalds * so we have to use the second new lock. 11211da177e4SLinus Torvalds */ 11221da177e4SLinus Torvalds left = new_fl2; 11231da177e4SLinus Torvalds new_fl2 = NULL; 11241da177e4SLinus Torvalds locks_copy_lock(left, right); 1125e084c1bdSJeff Layton locks_insert_lock_ctx(left, &fl->fl_list); 11261da177e4SLinus Torvalds } 11271da177e4SLinus Torvalds right->fl_start = request->fl_end + 1; 11281da177e4SLinus Torvalds locks_wake_up_blocks(right); 11291da177e4SLinus Torvalds } 11301da177e4SLinus Torvalds if (left) { 11311da177e4SLinus Torvalds left->fl_end = request->fl_start - 1; 11321da177e4SLinus Torvalds locks_wake_up_blocks(left); 11331da177e4SLinus Torvalds } 11341da177e4SLinus Torvalds out: 11356109c850SJeff Layton spin_unlock(&ctx->flc_lock); 11361da177e4SLinus Torvalds /* 11371da177e4SLinus Torvalds * Free any unused locks. 11381da177e4SLinus Torvalds */ 11391da177e4SLinus Torvalds if (new_fl) 11401da177e4SLinus Torvalds locks_free_lock(new_fl); 11411da177e4SLinus Torvalds if (new_fl2) 11421da177e4SLinus Torvalds locks_free_lock(new_fl2); 1143ed9814d8SJeff Layton locks_dispose_list(&dispose); 11441da177e4SLinus Torvalds return error; 11451da177e4SLinus Torvalds } 11461da177e4SLinus Torvalds 11471da177e4SLinus Torvalds /** 11481da177e4SLinus Torvalds * posix_lock_file - Apply a POSIX-style lock to a file 11491da177e4SLinus Torvalds * @filp: The file to apply the lock to 11501da177e4SLinus Torvalds * @fl: The lock to be applied 1151150b3934SMarc Eshel * @conflock: Place to return a copy of the conflicting lock, if found. 11521da177e4SLinus Torvalds * 11531da177e4SLinus Torvalds * Add a POSIX style lock to a file. 11541da177e4SLinus Torvalds * We merge adjacent & overlapping locks whenever possible. 11551da177e4SLinus Torvalds * POSIX locks are sorted by owner task, then by starting address 1156f475ae95STrond Myklebust * 1157f475ae95STrond Myklebust * Note that if called with an FL_EXISTS argument, the caller may determine 1158f475ae95STrond Myklebust * whether or not a lock was successfully freed by testing the return 1159f475ae95STrond Myklebust * value for -ENOENT. 11601da177e4SLinus Torvalds */ 1161150b3934SMarc Eshel int posix_lock_file(struct file *filp, struct file_lock *fl, 11625842add2SAndy Adamson struct file_lock *conflock) 11635842add2SAndy Adamson { 1164496ad9aaSAl Viro return __posix_lock_file(file_inode(filp), fl, conflock); 11655842add2SAndy Adamson } 1166150b3934SMarc Eshel EXPORT_SYMBOL(posix_lock_file); 11671da177e4SLinus Torvalds 11681da177e4SLinus Torvalds /** 116929d01b22SJeff Layton * posix_lock_inode_wait - Apply a POSIX-style lock to a file 117029d01b22SJeff Layton * @inode: inode of file to which lock request should be applied 11711da177e4SLinus Torvalds * @fl: The lock to be applied 11721da177e4SLinus Torvalds * 1173616fb38fSBenjamin Coddington * Apply a POSIX style lock request to an inode. 11741da177e4SLinus Torvalds */ 1175616fb38fSBenjamin Coddington static int posix_lock_inode_wait(struct inode *inode, struct file_lock *fl) 11761da177e4SLinus Torvalds { 11771da177e4SLinus Torvalds int error; 11781da177e4SLinus Torvalds might_sleep (); 11791da177e4SLinus Torvalds for (;;) { 118029d01b22SJeff Layton error = __posix_lock_file(inode, fl, NULL); 1181bde74e4bSMiklos Szeredi if (error != FILE_LOCK_DEFERRED) 11821da177e4SLinus Torvalds break; 11831da177e4SLinus Torvalds error = wait_event_interruptible(fl->fl_wait, !fl->fl_next); 11841da177e4SLinus Torvalds if (!error) 11851da177e4SLinus Torvalds continue; 11861da177e4SLinus Torvalds 11871da177e4SLinus Torvalds locks_delete_block(fl); 11881da177e4SLinus Torvalds break; 11891da177e4SLinus Torvalds } 11901da177e4SLinus Torvalds return error; 11911da177e4SLinus Torvalds } 119229d01b22SJeff Layton 11939e8925b6SJeff Layton #ifdef CONFIG_MANDATORY_FILE_LOCKING 119429d01b22SJeff Layton /** 11951da177e4SLinus Torvalds * locks_mandatory_locked - Check for an active lock 1196d7a06983SJeff Layton * @file: the file to check 11971da177e4SLinus Torvalds * 11981da177e4SLinus Torvalds * Searches the inode's list of locks to find any POSIX locks which conflict. 11991da177e4SLinus Torvalds * This function is called from locks_verify_locked() only. 12001da177e4SLinus Torvalds */ 1201d7a06983SJeff Layton int locks_mandatory_locked(struct file *file) 12021da177e4SLinus Torvalds { 1203bd61e0a9SJeff Layton int ret; 1204d7a06983SJeff Layton struct inode *inode = file_inode(file); 1205bd61e0a9SJeff Layton struct file_lock_context *ctx; 12061da177e4SLinus Torvalds struct file_lock *fl; 12071da177e4SLinus Torvalds 1208128a3785SDmitry Vyukov ctx = smp_load_acquire(&inode->i_flctx); 1209bd61e0a9SJeff Layton if (!ctx || list_empty_careful(&ctx->flc_posix)) 1210bd61e0a9SJeff Layton return 0; 1211bd61e0a9SJeff Layton 12121da177e4SLinus Torvalds /* 12131da177e4SLinus Torvalds * Search the lock list for this inode for any POSIX locks. 12141da177e4SLinus Torvalds */ 12156109c850SJeff Layton spin_lock(&ctx->flc_lock); 1216bd61e0a9SJeff Layton ret = 0; 1217bd61e0a9SJeff Layton list_for_each_entry(fl, &ctx->flc_posix, fl_list) { 121873a8f5f7SChristoph Hellwig if (fl->fl_owner != current->files && 1219bd61e0a9SJeff Layton fl->fl_owner != file) { 1220bd61e0a9SJeff Layton ret = -EAGAIN; 12211da177e4SLinus Torvalds break; 12221da177e4SLinus Torvalds } 1223bd61e0a9SJeff Layton } 12246109c850SJeff Layton spin_unlock(&ctx->flc_lock); 1225bd61e0a9SJeff Layton return ret; 12261da177e4SLinus Torvalds } 12271da177e4SLinus Torvalds 12281da177e4SLinus Torvalds /** 12291da177e4SLinus Torvalds * locks_mandatory_area - Check for a conflicting lock 12301da177e4SLinus Torvalds * @read_write: %FLOCK_VERIFY_WRITE for exclusive access, %FLOCK_VERIFY_READ 12311da177e4SLinus Torvalds * for shared 12321da177e4SLinus Torvalds * @inode: the file to check 12331da177e4SLinus Torvalds * @filp: how the file was opened (if it was) 12341da177e4SLinus Torvalds * @offset: start of area to check 12351da177e4SLinus Torvalds * @count: length of area to check 12361da177e4SLinus Torvalds * 12371da177e4SLinus Torvalds * Searches the inode's list of locks to find any POSIX locks which conflict. 12381da177e4SLinus Torvalds * This function is called from rw_verify_area() and 12391da177e4SLinus Torvalds * locks_verify_truncate(). 12401da177e4SLinus Torvalds */ 12411da177e4SLinus Torvalds int locks_mandatory_area(int read_write, struct inode *inode, 12421da177e4SLinus Torvalds struct file *filp, loff_t offset, 12431da177e4SLinus Torvalds size_t count) 12441da177e4SLinus Torvalds { 12451da177e4SLinus Torvalds struct file_lock fl; 12461da177e4SLinus Torvalds int error; 124729723adeSJeff Layton bool sleep = false; 12481da177e4SLinus Torvalds 12491da177e4SLinus Torvalds locks_init_lock(&fl); 12501da177e4SLinus Torvalds fl.fl_pid = current->tgid; 12511da177e4SLinus Torvalds fl.fl_file = filp; 12521da177e4SLinus Torvalds fl.fl_flags = FL_POSIX | FL_ACCESS; 12531da177e4SLinus Torvalds if (filp && !(filp->f_flags & O_NONBLOCK)) 125429723adeSJeff Layton sleep = true; 12551da177e4SLinus Torvalds fl.fl_type = (read_write == FLOCK_VERIFY_WRITE) ? F_WRLCK : F_RDLCK; 12561da177e4SLinus Torvalds fl.fl_start = offset; 12571da177e4SLinus Torvalds fl.fl_end = offset + count - 1; 12581da177e4SLinus Torvalds 12591da177e4SLinus Torvalds for (;;) { 126029723adeSJeff Layton if (filp) { 126173a8f5f7SChristoph Hellwig fl.fl_owner = filp; 126229723adeSJeff Layton fl.fl_flags &= ~FL_SLEEP; 126329723adeSJeff Layton error = __posix_lock_file(inode, &fl, NULL); 126429723adeSJeff Layton if (!error) 126529723adeSJeff Layton break; 126629723adeSJeff Layton } 126729723adeSJeff Layton 126829723adeSJeff Layton if (sleep) 126929723adeSJeff Layton fl.fl_flags |= FL_SLEEP; 127029723adeSJeff Layton fl.fl_owner = current->files; 1271150b3934SMarc Eshel error = __posix_lock_file(inode, &fl, NULL); 1272bde74e4bSMiklos Szeredi if (error != FILE_LOCK_DEFERRED) 12731da177e4SLinus Torvalds break; 12741da177e4SLinus Torvalds error = wait_event_interruptible(fl.fl_wait, !fl.fl_next); 12751da177e4SLinus Torvalds if (!error) { 12761da177e4SLinus Torvalds /* 12771da177e4SLinus Torvalds * If we've been sleeping someone might have 12781da177e4SLinus Torvalds * changed the permissions behind our back. 12791da177e4SLinus Torvalds */ 1280a16877caSPavel Emelyanov if (__mandatory_lock(inode)) 12811da177e4SLinus Torvalds continue; 12821da177e4SLinus Torvalds } 12831da177e4SLinus Torvalds 12841da177e4SLinus Torvalds locks_delete_block(&fl); 12851da177e4SLinus Torvalds break; 12861da177e4SLinus Torvalds } 12871da177e4SLinus Torvalds 12881da177e4SLinus Torvalds return error; 12891da177e4SLinus Torvalds } 12901da177e4SLinus Torvalds 12911da177e4SLinus Torvalds EXPORT_SYMBOL(locks_mandatory_area); 12929e8925b6SJeff Layton #endif /* CONFIG_MANDATORY_FILE_LOCKING */ 12931da177e4SLinus Torvalds 1294778fc546SJ. Bruce Fields static void lease_clear_pending(struct file_lock *fl, int arg) 1295778fc546SJ. Bruce Fields { 1296778fc546SJ. Bruce Fields switch (arg) { 1297778fc546SJ. Bruce Fields case F_UNLCK: 1298778fc546SJ. Bruce Fields fl->fl_flags &= ~FL_UNLOCK_PENDING; 1299778fc546SJ. Bruce Fields /* fall through: */ 1300778fc546SJ. Bruce Fields case F_RDLCK: 1301778fc546SJ. Bruce Fields fl->fl_flags &= ~FL_DOWNGRADE_PENDING; 1302778fc546SJ. Bruce Fields } 1303778fc546SJ. Bruce Fields } 1304778fc546SJ. Bruce Fields 13051da177e4SLinus Torvalds /* We already had a lease on this file; just change its type */ 13067448cc37SJeff Layton int lease_modify(struct file_lock *fl, int arg, struct list_head *dispose) 13071da177e4SLinus Torvalds { 13081da177e4SLinus Torvalds int error = assign_type(fl, arg); 13091da177e4SLinus Torvalds 13101da177e4SLinus Torvalds if (error) 13111da177e4SLinus Torvalds return error; 1312778fc546SJ. Bruce Fields lease_clear_pending(fl, arg); 13131da177e4SLinus Torvalds locks_wake_up_blocks(fl); 13143b6e2723SFilipe Brandenburger if (arg == F_UNLCK) { 13153b6e2723SFilipe Brandenburger struct file *filp = fl->fl_file; 13163b6e2723SFilipe Brandenburger 13173b6e2723SFilipe Brandenburger f_delown(filp); 13183b6e2723SFilipe Brandenburger filp->f_owner.signum = 0; 131996d6d59cSJ. Bruce Fields fasync_helper(0, fl->fl_file, 0, &fl->fl_fasync); 132096d6d59cSJ. Bruce Fields if (fl->fl_fasync != NULL) { 132196d6d59cSJ. Bruce Fields printk(KERN_ERR "locks_delete_lock: fasync == %p\n", fl->fl_fasync); 132296d6d59cSJ. Bruce Fields fl->fl_fasync = NULL; 132396d6d59cSJ. Bruce Fields } 1324e084c1bdSJeff Layton locks_delete_lock_ctx(fl, dispose); 13253b6e2723SFilipe Brandenburger } 13261da177e4SLinus Torvalds return 0; 13271da177e4SLinus Torvalds } 13281da177e4SLinus Torvalds EXPORT_SYMBOL(lease_modify); 13291da177e4SLinus Torvalds 1330778fc546SJ. Bruce Fields static bool past_time(unsigned long then) 1331778fc546SJ. Bruce Fields { 1332778fc546SJ. Bruce Fields if (!then) 1333778fc546SJ. Bruce Fields /* 0 is a special value meaning "this never expires": */ 1334778fc546SJ. Bruce Fields return false; 1335778fc546SJ. Bruce Fields return time_after(jiffies, then); 1336778fc546SJ. Bruce Fields } 1337778fc546SJ. Bruce Fields 1338c45198edSJeff Layton static void time_out_leases(struct inode *inode, struct list_head *dispose) 13391da177e4SLinus Torvalds { 13408634b51fSJeff Layton struct file_lock_context *ctx = inode->i_flctx; 13418634b51fSJeff Layton struct file_lock *fl, *tmp; 13421da177e4SLinus Torvalds 13436109c850SJeff Layton lockdep_assert_held(&ctx->flc_lock); 1344f82b4b67SJeff Layton 13458634b51fSJeff Layton list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list) { 134662af4f1fSJeff Layton trace_time_out_leases(inode, fl); 1347778fc546SJ. Bruce Fields if (past_time(fl->fl_downgrade_time)) 13487448cc37SJeff Layton lease_modify(fl, F_RDLCK, dispose); 1349778fc546SJ. Bruce Fields if (past_time(fl->fl_break_time)) 13507448cc37SJeff Layton lease_modify(fl, F_UNLCK, dispose); 13511da177e4SLinus Torvalds } 13521da177e4SLinus Torvalds } 13531da177e4SLinus Torvalds 1354df4e8d2cSJ. Bruce Fields static bool leases_conflict(struct file_lock *lease, struct file_lock *breaker) 1355df4e8d2cSJ. Bruce Fields { 135611afe9f7SChristoph Hellwig if ((breaker->fl_flags & FL_LAYOUT) != (lease->fl_flags & FL_LAYOUT)) 135711afe9f7SChristoph Hellwig return false; 1358df4e8d2cSJ. Bruce Fields if ((breaker->fl_flags & FL_DELEG) && (lease->fl_flags & FL_LEASE)) 1359df4e8d2cSJ. Bruce Fields return false; 1360df4e8d2cSJ. Bruce Fields return locks_conflict(breaker, lease); 1361df4e8d2cSJ. Bruce Fields } 1362df4e8d2cSJ. Bruce Fields 136303d12ddfSJeff Layton static bool 136403d12ddfSJeff Layton any_leases_conflict(struct inode *inode, struct file_lock *breaker) 136503d12ddfSJeff Layton { 13668634b51fSJeff Layton struct file_lock_context *ctx = inode->i_flctx; 136703d12ddfSJeff Layton struct file_lock *fl; 136803d12ddfSJeff Layton 13696109c850SJeff Layton lockdep_assert_held(&ctx->flc_lock); 137003d12ddfSJeff Layton 13718634b51fSJeff Layton list_for_each_entry(fl, &ctx->flc_lease, fl_list) { 137203d12ddfSJeff Layton if (leases_conflict(fl, breaker)) 137303d12ddfSJeff Layton return true; 137403d12ddfSJeff Layton } 137503d12ddfSJeff Layton return false; 137603d12ddfSJeff Layton } 137703d12ddfSJeff Layton 13781da177e4SLinus Torvalds /** 13791da177e4SLinus Torvalds * __break_lease - revoke all outstanding leases on file 13801da177e4SLinus Torvalds * @inode: the inode of the file to return 1381df4e8d2cSJ. Bruce Fields * @mode: O_RDONLY: break only write leases; O_WRONLY or O_RDWR: 1382df4e8d2cSJ. Bruce Fields * break all leases 1383df4e8d2cSJ. Bruce Fields * @type: FL_LEASE: break leases and delegations; FL_DELEG: break 1384df4e8d2cSJ. Bruce Fields * only delegations 13851da177e4SLinus Torvalds * 138687250dd2Sdavid m. richter * break_lease (inlined for speed) has checked there already is at least 138787250dd2Sdavid m. richter * some kind of lock (maybe a lease) on this file. Leases are broken on 138887250dd2Sdavid m. richter * a call to open() or truncate(). This function can sleep unless you 13891da177e4SLinus Torvalds * specified %O_NONBLOCK to your open(). 13901da177e4SLinus Torvalds */ 1391df4e8d2cSJ. Bruce Fields int __break_lease(struct inode *inode, unsigned int mode, unsigned int type) 13921da177e4SLinus Torvalds { 1393778fc546SJ. Bruce Fields int error = 0; 1394128a3785SDmitry Vyukov struct file_lock_context *ctx; 1395a901125cSYan, Zheng struct file_lock *new_fl, *fl, *tmp; 13961da177e4SLinus Torvalds unsigned long break_time; 13978737c930SAl Viro int want_write = (mode & O_ACCMODE) != O_RDONLY; 1398c45198edSJeff Layton LIST_HEAD(dispose); 13991da177e4SLinus Torvalds 14008737c930SAl Viro new_fl = lease_alloc(NULL, want_write ? F_WRLCK : F_RDLCK); 14016d4b9e38SLinus Torvalds if (IS_ERR(new_fl)) 14026d4b9e38SLinus Torvalds return PTR_ERR(new_fl); 1403df4e8d2cSJ. Bruce Fields new_fl->fl_flags = type; 14041da177e4SLinus Torvalds 14058634b51fSJeff Layton /* typically we will check that ctx is non-NULL before calling */ 1406128a3785SDmitry Vyukov ctx = smp_load_acquire(&inode->i_flctx); 14078634b51fSJeff Layton if (!ctx) { 14088634b51fSJeff Layton WARN_ON_ONCE(1); 14098634b51fSJeff Layton return error; 14108634b51fSJeff Layton } 14118634b51fSJeff Layton 14126109c850SJeff Layton spin_lock(&ctx->flc_lock); 14131da177e4SLinus Torvalds 1414c45198edSJeff Layton time_out_leases(inode, &dispose); 14151da177e4SLinus Torvalds 141603d12ddfSJeff Layton if (!any_leases_conflict(inode, new_fl)) 1417df4e8d2cSJ. Bruce Fields goto out; 14181da177e4SLinus Torvalds 14191da177e4SLinus Torvalds break_time = 0; 14201da177e4SLinus Torvalds if (lease_break_time > 0) { 14211da177e4SLinus Torvalds break_time = jiffies + lease_break_time * HZ; 14221da177e4SLinus Torvalds if (break_time == 0) 14231da177e4SLinus Torvalds break_time++; /* so that 0 means no break time */ 14241da177e4SLinus Torvalds } 14251da177e4SLinus Torvalds 1426a901125cSYan, Zheng list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list) { 1427df4e8d2cSJ. Bruce Fields if (!leases_conflict(fl, new_fl)) 1428df4e8d2cSJ. Bruce Fields continue; 1429778fc546SJ. Bruce Fields if (want_write) { 1430778fc546SJ. Bruce Fields if (fl->fl_flags & FL_UNLOCK_PENDING) 1431778fc546SJ. Bruce Fields continue; 1432778fc546SJ. Bruce Fields fl->fl_flags |= FL_UNLOCK_PENDING; 14331da177e4SLinus Torvalds fl->fl_break_time = break_time; 1434778fc546SJ. Bruce Fields } else { 14358634b51fSJeff Layton if (lease_breaking(fl)) 1436778fc546SJ. Bruce Fields continue; 1437778fc546SJ. Bruce Fields fl->fl_flags |= FL_DOWNGRADE_PENDING; 1438778fc546SJ. Bruce Fields fl->fl_downgrade_time = break_time; 14391da177e4SLinus Torvalds } 14404d01b7f5SJeff Layton if (fl->fl_lmops->lm_break(fl)) 1441e084c1bdSJeff Layton locks_delete_lock_ctx(fl, &dispose); 14421da177e4SLinus Torvalds } 14431da177e4SLinus Torvalds 14448634b51fSJeff Layton if (list_empty(&ctx->flc_lease)) 14454d01b7f5SJeff Layton goto out; 14464d01b7f5SJeff Layton 1447843c6b2fSJeff Layton if (mode & O_NONBLOCK) { 144862af4f1fSJeff Layton trace_break_lease_noblock(inode, new_fl); 14491da177e4SLinus Torvalds error = -EWOULDBLOCK; 14501da177e4SLinus Torvalds goto out; 14511da177e4SLinus Torvalds } 14521da177e4SLinus Torvalds 14531da177e4SLinus Torvalds restart: 14548634b51fSJeff Layton fl = list_first_entry(&ctx->flc_lease, struct file_lock, fl_list); 14558634b51fSJeff Layton break_time = fl->fl_break_time; 1456f1c6bb2cSJeff Layton if (break_time != 0) 14571da177e4SLinus Torvalds break_time -= jiffies; 14581da177e4SLinus Torvalds if (break_time == 0) 14591da177e4SLinus Torvalds break_time++; 14608634b51fSJeff Layton locks_insert_block(fl, new_fl); 146162af4f1fSJeff Layton trace_break_lease_block(inode, new_fl); 14626109c850SJeff Layton spin_unlock(&ctx->flc_lock); 1463c45198edSJeff Layton locks_dispose_list(&dispose); 14644321e01eSMatthew Wilcox error = wait_event_interruptible_timeout(new_fl->fl_wait, 14654321e01eSMatthew Wilcox !new_fl->fl_next, break_time); 14666109c850SJeff Layton spin_lock(&ctx->flc_lock); 146762af4f1fSJeff Layton trace_break_lease_unblock(inode, new_fl); 14681c8c601aSJeff Layton locks_delete_block(new_fl); 14691da177e4SLinus Torvalds if (error >= 0) { 1470778fc546SJ. Bruce Fields /* 1471778fc546SJ. Bruce Fields * Wait for the next conflicting lease that has not been 1472778fc546SJ. Bruce Fields * broken yet 1473778fc546SJ. Bruce Fields */ 147403d12ddfSJeff Layton if (error == 0) 147503d12ddfSJeff Layton time_out_leases(inode, &dispose); 147603d12ddfSJeff Layton if (any_leases_conflict(inode, new_fl)) 14771da177e4SLinus Torvalds goto restart; 14781da177e4SLinus Torvalds error = 0; 14791da177e4SLinus Torvalds } 14801da177e4SLinus Torvalds out: 14816109c850SJeff Layton spin_unlock(&ctx->flc_lock); 1482c45198edSJeff Layton locks_dispose_list(&dispose); 14831da177e4SLinus Torvalds locks_free_lock(new_fl); 14841da177e4SLinus Torvalds return error; 14851da177e4SLinus Torvalds } 14861da177e4SLinus Torvalds 14871da177e4SLinus Torvalds EXPORT_SYMBOL(__break_lease); 14881da177e4SLinus Torvalds 14891da177e4SLinus Torvalds /** 1490a6b91919SRandy Dunlap * lease_get_mtime - get the last modified time of an inode 14911da177e4SLinus Torvalds * @inode: the inode 14921da177e4SLinus Torvalds * @time: pointer to a timespec which will contain the last modified time 14931da177e4SLinus Torvalds * 14941da177e4SLinus Torvalds * This is to force NFS clients to flush their caches for files with 14951da177e4SLinus Torvalds * exclusive leases. The justification is that if someone has an 1496a6b91919SRandy Dunlap * exclusive lease, then they could be modifying it. 14971da177e4SLinus Torvalds */ 14981da177e4SLinus Torvalds void lease_get_mtime(struct inode *inode, struct timespec *time) 14991da177e4SLinus Torvalds { 1500bfe86024SJeff Layton bool has_lease = false; 1501128a3785SDmitry Vyukov struct file_lock_context *ctx; 15028634b51fSJeff Layton struct file_lock *fl; 1503bfe86024SJeff Layton 1504128a3785SDmitry Vyukov ctx = smp_load_acquire(&inode->i_flctx); 15058634b51fSJeff Layton if (ctx && !list_empty_careful(&ctx->flc_lease)) { 15066109c850SJeff Layton spin_lock(&ctx->flc_lock); 15078ace5dfbSGeliang Tang fl = list_first_entry_or_null(&ctx->flc_lease, 15088634b51fSJeff Layton struct file_lock, fl_list); 15098ace5dfbSGeliang Tang if (fl && (fl->fl_type == F_WRLCK)) 1510bfe86024SJeff Layton has_lease = true; 15116109c850SJeff Layton spin_unlock(&ctx->flc_lock); 1512bfe86024SJeff Layton } 1513bfe86024SJeff Layton 1514bfe86024SJeff Layton if (has_lease) 15151da177e4SLinus Torvalds *time = current_fs_time(inode->i_sb); 15161da177e4SLinus Torvalds else 15171da177e4SLinus Torvalds *time = inode->i_mtime; 15181da177e4SLinus Torvalds } 15191da177e4SLinus Torvalds 15201da177e4SLinus Torvalds EXPORT_SYMBOL(lease_get_mtime); 15211da177e4SLinus Torvalds 15221da177e4SLinus Torvalds /** 15231da177e4SLinus Torvalds * fcntl_getlease - Enquire what lease is currently active 15241da177e4SLinus Torvalds * @filp: the file 15251da177e4SLinus Torvalds * 15261da177e4SLinus Torvalds * The value returned by this function will be one of 15271da177e4SLinus Torvalds * (if no lease break is pending): 15281da177e4SLinus Torvalds * 15291da177e4SLinus Torvalds * %F_RDLCK to indicate a shared lease is held. 15301da177e4SLinus Torvalds * 15311da177e4SLinus Torvalds * %F_WRLCK to indicate an exclusive lease is held. 15321da177e4SLinus Torvalds * 15331da177e4SLinus Torvalds * %F_UNLCK to indicate no lease is held. 15341da177e4SLinus Torvalds * 15351da177e4SLinus Torvalds * (if a lease break is pending): 15361da177e4SLinus Torvalds * 15371da177e4SLinus Torvalds * %F_RDLCK to indicate an exclusive lease needs to be 15381da177e4SLinus Torvalds * changed to a shared lease (or removed). 15391da177e4SLinus Torvalds * 15401da177e4SLinus Torvalds * %F_UNLCK to indicate the lease needs to be removed. 15411da177e4SLinus Torvalds * 15421da177e4SLinus Torvalds * XXX: sfr & willy disagree over whether F_INPROGRESS 15431da177e4SLinus Torvalds * should be returned to userspace. 15441da177e4SLinus Torvalds */ 15451da177e4SLinus Torvalds int fcntl_getlease(struct file *filp) 15461da177e4SLinus Torvalds { 15471da177e4SLinus Torvalds struct file_lock *fl; 15481c8c601aSJeff Layton struct inode *inode = file_inode(filp); 1549128a3785SDmitry Vyukov struct file_lock_context *ctx; 15501da177e4SLinus Torvalds int type = F_UNLCK; 1551c45198edSJeff Layton LIST_HEAD(dispose); 15521da177e4SLinus Torvalds 1553128a3785SDmitry Vyukov ctx = smp_load_acquire(&inode->i_flctx); 15548634b51fSJeff Layton if (ctx && !list_empty_careful(&ctx->flc_lease)) { 15556109c850SJeff Layton spin_lock(&ctx->flc_lock); 1556c45198edSJeff Layton time_out_leases(file_inode(filp), &dispose); 15578634b51fSJeff Layton list_for_each_entry(fl, &ctx->flc_lease, fl_list) { 15588634b51fSJeff Layton if (fl->fl_file != filp) 15598634b51fSJeff Layton continue; 1560778fc546SJ. Bruce Fields type = target_leasetype(fl); 15611da177e4SLinus Torvalds break; 15621da177e4SLinus Torvalds } 15636109c850SJeff Layton spin_unlock(&ctx->flc_lock); 1564c45198edSJeff Layton locks_dispose_list(&dispose); 15658634b51fSJeff Layton } 15661da177e4SLinus Torvalds return type; 15671da177e4SLinus Torvalds } 15681da177e4SLinus Torvalds 156924cbe784SJeff Layton /** 157024cbe784SJeff Layton * check_conflicting_open - see if the given dentry points to a file that has 157124cbe784SJeff Layton * an existing open that would conflict with the 157224cbe784SJeff Layton * desired lease. 157324cbe784SJeff Layton * @dentry: dentry to check 157424cbe784SJeff Layton * @arg: type of lease that we're trying to acquire 15757fadc59cSRandy Dunlap * @flags: current lock flags 157624cbe784SJeff Layton * 157724cbe784SJeff Layton * Check to see if there's an existing open fd on this file that would 157824cbe784SJeff Layton * conflict with the lease we're trying to set. 157924cbe784SJeff Layton */ 158024cbe784SJeff Layton static int 158111afe9f7SChristoph Hellwig check_conflicting_open(const struct dentry *dentry, const long arg, int flags) 158224cbe784SJeff Layton { 158324cbe784SJeff Layton int ret = 0; 158424cbe784SJeff Layton struct inode *inode = dentry->d_inode; 158524cbe784SJeff Layton 158611afe9f7SChristoph Hellwig if (flags & FL_LAYOUT) 158711afe9f7SChristoph Hellwig return 0; 158811afe9f7SChristoph Hellwig 158924cbe784SJeff Layton if ((arg == F_RDLCK) && (atomic_read(&inode->i_writecount) > 0)) 159024cbe784SJeff Layton return -EAGAIN; 159124cbe784SJeff Layton 159224cbe784SJeff Layton if ((arg == F_WRLCK) && ((d_count(dentry) > 1) || 159324cbe784SJeff Layton (atomic_read(&inode->i_count) > 1))) 159424cbe784SJeff Layton ret = -EAGAIN; 159524cbe784SJeff Layton 159624cbe784SJeff Layton return ret; 159724cbe784SJeff Layton } 159824cbe784SJeff Layton 1599e6f5c789SJeff Layton static int 1600e6f5c789SJeff Layton generic_add_lease(struct file *filp, long arg, struct file_lock **flp, void **priv) 16011da177e4SLinus Torvalds { 16028634b51fSJeff Layton struct file_lock *fl, *my_fl = NULL, *lease; 16030f7fc9e4SJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 16041da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 16058634b51fSJeff Layton struct file_lock_context *ctx; 1606df4e8d2cSJ. Bruce Fields bool is_deleg = (*flp)->fl_flags & FL_DELEG; 1607c1f24ef4SJ. Bruce Fields int error; 1608c45198edSJeff Layton LIST_HEAD(dispose); 16091da177e4SLinus Torvalds 1610096657b6SJ. Bruce Fields lease = *flp; 161162af4f1fSJeff Layton trace_generic_add_lease(inode, lease); 161262af4f1fSJeff Layton 16135c1c669aSJeff Layton /* Note that arg is never F_UNLCK here */ 16145c1c669aSJeff Layton ctx = locks_get_lock_context(inode, arg); 16158634b51fSJeff Layton if (!ctx) 16168634b51fSJeff Layton return -ENOMEM; 16178634b51fSJeff Layton 1618df4e8d2cSJ. Bruce Fields /* 1619df4e8d2cSJ. Bruce Fields * In the delegation case we need mutual exclusion with 1620df4e8d2cSJ. Bruce Fields * a number of operations that take the i_mutex. We trylock 1621df4e8d2cSJ. Bruce Fields * because delegations are an optional optimization, and if 1622df4e8d2cSJ. Bruce Fields * there's some chance of a conflict--we'd rather not 1623df4e8d2cSJ. Bruce Fields * bother, maybe that's a sign this just isn't a good file to 1624df4e8d2cSJ. Bruce Fields * hand out a delegation on. 1625df4e8d2cSJ. Bruce Fields */ 1626df4e8d2cSJ. Bruce Fields if (is_deleg && !mutex_trylock(&inode->i_mutex)) 1627df4e8d2cSJ. Bruce Fields return -EAGAIN; 1628df4e8d2cSJ. Bruce Fields 1629df4e8d2cSJ. Bruce Fields if (is_deleg && arg == F_WRLCK) { 1630df4e8d2cSJ. Bruce Fields /* Write delegations are not currently supported: */ 16314fdb793fSDan Carpenter mutex_unlock(&inode->i_mutex); 1632df4e8d2cSJ. Bruce Fields WARN_ON_ONCE(1); 1633df4e8d2cSJ. Bruce Fields return -EINVAL; 1634df4e8d2cSJ. Bruce Fields } 1635096657b6SJ. Bruce Fields 16366109c850SJeff Layton spin_lock(&ctx->flc_lock); 1637c45198edSJeff Layton time_out_leases(inode, &dispose); 163811afe9f7SChristoph Hellwig error = check_conflicting_open(dentry, arg, lease->fl_flags); 163924cbe784SJeff Layton if (error) 16401da177e4SLinus Torvalds goto out; 164185c59580SPavel Emelyanov 16421da177e4SLinus Torvalds /* 16431da177e4SLinus Torvalds * At this point, we know that if there is an exclusive 16441da177e4SLinus Torvalds * lease on this file, then we hold it on this filp 16451da177e4SLinus Torvalds * (otherwise our open of this file would have blocked). 16461da177e4SLinus Torvalds * And if we are trying to acquire an exclusive lease, 16471da177e4SLinus Torvalds * then the file is not open by anyone (including us) 16481da177e4SLinus Torvalds * except for this filp. 16491da177e4SLinus Torvalds */ 1650c1f24ef4SJ. Bruce Fields error = -EAGAIN; 16518634b51fSJeff Layton list_for_each_entry(fl, &ctx->flc_lease, fl_list) { 16522ab99ee1SChristoph Hellwig if (fl->fl_file == filp && 16532ab99ee1SChristoph Hellwig fl->fl_owner == lease->fl_owner) { 16548634b51fSJeff Layton my_fl = fl; 1655c1f24ef4SJ. Bruce Fields continue; 16561da177e4SLinus Torvalds } 16578634b51fSJeff Layton 1658c1f24ef4SJ. Bruce Fields /* 1659c1f24ef4SJ. Bruce Fields * No exclusive leases if someone else has a lease on 1660c1f24ef4SJ. Bruce Fields * this file: 1661c1f24ef4SJ. Bruce Fields */ 1662c1f24ef4SJ. Bruce Fields if (arg == F_WRLCK) 16631da177e4SLinus Torvalds goto out; 1664c1f24ef4SJ. Bruce Fields /* 1665c1f24ef4SJ. Bruce Fields * Modifying our existing lease is OK, but no getting a 1666c1f24ef4SJ. Bruce Fields * new lease if someone else is opening for write: 1667c1f24ef4SJ. Bruce Fields */ 1668c1f24ef4SJ. Bruce Fields if (fl->fl_flags & FL_UNLOCK_PENDING) 1669c1f24ef4SJ. Bruce Fields goto out; 1670c1f24ef4SJ. Bruce Fields } 16711da177e4SLinus Torvalds 16728634b51fSJeff Layton if (my_fl != NULL) { 16730164bf02SJeff Layton lease = my_fl; 16740164bf02SJeff Layton error = lease->fl_lmops->lm_change(lease, arg, &dispose); 16751c7dd2ffSJeff Layton if (error) 16761da177e4SLinus Torvalds goto out; 16771c7dd2ffSJeff Layton goto out_setup; 16781da177e4SLinus Torvalds } 16791da177e4SLinus Torvalds 16801da177e4SLinus Torvalds error = -EINVAL; 16811da177e4SLinus Torvalds if (!leases_enable) 16821da177e4SLinus Torvalds goto out; 16831da177e4SLinus Torvalds 1684e084c1bdSJeff Layton locks_insert_lock_ctx(lease, &ctx->flc_lease); 168524cbe784SJeff Layton /* 168624cbe784SJeff Layton * The check in break_lease() is lockless. It's possible for another 168724cbe784SJeff Layton * open to race in after we did the earlier check for a conflicting 168824cbe784SJeff Layton * open but before the lease was inserted. Check again for a 168924cbe784SJeff Layton * conflicting open and cancel the lease if there is one. 169024cbe784SJeff Layton * 169124cbe784SJeff Layton * We also add a barrier here to ensure that the insertion of the lock 169224cbe784SJeff Layton * precedes these checks. 169324cbe784SJeff Layton */ 169424cbe784SJeff Layton smp_mb(); 169511afe9f7SChristoph Hellwig error = check_conflicting_open(dentry, arg, lease->fl_flags); 16968634b51fSJeff Layton if (error) { 1697e084c1bdSJeff Layton locks_unlink_lock_ctx(lease); 16988634b51fSJeff Layton goto out; 16998634b51fSJeff Layton } 17001c7dd2ffSJeff Layton 17011c7dd2ffSJeff Layton out_setup: 17021c7dd2ffSJeff Layton if (lease->fl_lmops->lm_setup) 17031c7dd2ffSJeff Layton lease->fl_lmops->lm_setup(lease, priv); 17041da177e4SLinus Torvalds out: 17056109c850SJeff Layton spin_unlock(&ctx->flc_lock); 1706c45198edSJeff Layton locks_dispose_list(&dispose); 1707df4e8d2cSJ. Bruce Fields if (is_deleg) 1708df4e8d2cSJ. Bruce Fields mutex_unlock(&inode->i_mutex); 17098634b51fSJeff Layton if (!error && !my_fl) 17101c7dd2ffSJeff Layton *flp = NULL; 17111da177e4SLinus Torvalds return error; 17121da177e4SLinus Torvalds } 17138335ebd9SJ. Bruce Fields 17142ab99ee1SChristoph Hellwig static int generic_delete_lease(struct file *filp, void *owner) 17158335ebd9SJ. Bruce Fields { 17160efaa7e8SJeff Layton int error = -EAGAIN; 17178634b51fSJeff Layton struct file_lock *fl, *victim = NULL; 17186ca7d910SBenjamin Coddington struct inode *inode = file_inode(filp); 1719128a3785SDmitry Vyukov struct file_lock_context *ctx; 1720c45198edSJeff Layton LIST_HEAD(dispose); 17218335ebd9SJ. Bruce Fields 1722128a3785SDmitry Vyukov ctx = smp_load_acquire(&inode->i_flctx); 17238634b51fSJeff Layton if (!ctx) { 17248634b51fSJeff Layton trace_generic_delete_lease(inode, NULL); 17258634b51fSJeff Layton return error; 17268634b51fSJeff Layton } 17278634b51fSJeff Layton 17286109c850SJeff Layton spin_lock(&ctx->flc_lock); 17298634b51fSJeff Layton list_for_each_entry(fl, &ctx->flc_lease, fl_list) { 17302ab99ee1SChristoph Hellwig if (fl->fl_file == filp && 17312ab99ee1SChristoph Hellwig fl->fl_owner == owner) { 17328634b51fSJeff Layton victim = fl; 17330efaa7e8SJeff Layton break; 17348335ebd9SJ. Bruce Fields } 17358634b51fSJeff Layton } 1736a9b1b455SJeff Layton trace_generic_delete_lease(inode, victim); 17378634b51fSJeff Layton if (victim) 17387448cc37SJeff Layton error = fl->fl_lmops->lm_change(victim, F_UNLCK, &dispose); 17396109c850SJeff Layton spin_unlock(&ctx->flc_lock); 1740c45198edSJeff Layton locks_dispose_list(&dispose); 17410efaa7e8SJeff Layton return error; 17428335ebd9SJ. Bruce Fields } 17438335ebd9SJ. Bruce Fields 17441da177e4SLinus Torvalds /** 17451da177e4SLinus Torvalds * generic_setlease - sets a lease on an open file 17461da177e4SLinus Torvalds * @filp: file pointer 17471da177e4SLinus Torvalds * @arg: type of lease to obtain 17481da177e4SLinus Torvalds * @flp: input - file_lock to use, output - file_lock inserted 17491c7dd2ffSJeff Layton * @priv: private data for lm_setup (may be NULL if lm_setup 17501c7dd2ffSJeff Layton * doesn't require it) 17511da177e4SLinus Torvalds * 17521da177e4SLinus Torvalds * The (input) flp->fl_lmops->lm_break function is required 17531da177e4SLinus Torvalds * by break_lease(). 17541da177e4SLinus Torvalds */ 1755e6f5c789SJeff Layton int generic_setlease(struct file *filp, long arg, struct file_lock **flp, 1756e6f5c789SJeff Layton void **priv) 17571da177e4SLinus Torvalds { 17586ca7d910SBenjamin Coddington struct inode *inode = file_inode(filp); 17598335ebd9SJ. Bruce Fields int error; 17601da177e4SLinus Torvalds 17618e96e3b7SEric W. Biederman if ((!uid_eq(current_fsuid(), inode->i_uid)) && !capable(CAP_LEASE)) 17628335ebd9SJ. Bruce Fields return -EACCES; 17631da177e4SLinus Torvalds if (!S_ISREG(inode->i_mode)) 17648335ebd9SJ. Bruce Fields return -EINVAL; 17651da177e4SLinus Torvalds error = security_file_lock(filp, arg); 17661da177e4SLinus Torvalds if (error) 17678335ebd9SJ. Bruce Fields return error; 17681da177e4SLinus Torvalds 17698335ebd9SJ. Bruce Fields switch (arg) { 17708335ebd9SJ. Bruce Fields case F_UNLCK: 17712ab99ee1SChristoph Hellwig return generic_delete_lease(filp, *priv); 17728335ebd9SJ. Bruce Fields case F_RDLCK: 17738335ebd9SJ. Bruce Fields case F_WRLCK: 17740efaa7e8SJeff Layton if (!(*flp)->fl_lmops->lm_break) { 17750efaa7e8SJeff Layton WARN_ON_ONCE(1); 17760efaa7e8SJeff Layton return -ENOLCK; 17770efaa7e8SJeff Layton } 177811afe9f7SChristoph Hellwig 1779e6f5c789SJeff Layton return generic_add_lease(filp, arg, flp, priv); 17808335ebd9SJ. Bruce Fields default: 17818d657eb3SDave Jones return -EINVAL; 17821da177e4SLinus Torvalds } 17831da177e4SLinus Torvalds } 17840af1a450SChristoph Hellwig EXPORT_SYMBOL(generic_setlease); 17851da177e4SLinus Torvalds 17861da177e4SLinus Torvalds /** 1787a9933ceaSJ. Bruce Fields * vfs_setlease - sets a lease on an open file 17881da177e4SLinus Torvalds * @filp: file pointer 17891da177e4SLinus Torvalds * @arg: type of lease to obtain 1790e51673aaSJeff Layton * @lease: file_lock to use when adding a lease 17911c7dd2ffSJeff Layton * @priv: private info for lm_setup when adding a lease (may be 17921c7dd2ffSJeff Layton * NULL if lm_setup doesn't require it) 17931da177e4SLinus Torvalds * 1794e51673aaSJeff Layton * Call this to establish a lease on the file. The "lease" argument is not 1795e51673aaSJeff Layton * used for F_UNLCK requests and may be NULL. For commands that set or alter 1796e51673aaSJeff Layton * an existing lease, the (*lease)->fl_lmops->lm_break operation must be set; 1797e51673aaSJeff Layton * if not, this function will return -ENOLCK (and generate a scary-looking 1798e51673aaSJeff Layton * stack trace). 17991c7dd2ffSJeff Layton * 18001c7dd2ffSJeff Layton * The "priv" pointer is passed directly to the lm_setup function as-is. It 18011c7dd2ffSJeff Layton * may be NULL if the lm_setup operation doesn't require it. 18021da177e4SLinus Torvalds */ 1803e6f5c789SJeff Layton int 1804e6f5c789SJeff Layton vfs_setlease(struct file *filp, long arg, struct file_lock **lease, void **priv) 18051da177e4SLinus Torvalds { 18061c7dd2ffSJeff Layton if (filp->f_op->setlease) 1807f82b4b67SJeff Layton return filp->f_op->setlease(filp, arg, lease, priv); 18081c7dd2ffSJeff Layton else 1809f82b4b67SJeff Layton return generic_setlease(filp, arg, lease, priv); 18101da177e4SLinus Torvalds } 1811a9933ceaSJ. Bruce Fields EXPORT_SYMBOL_GPL(vfs_setlease); 18121da177e4SLinus Torvalds 18130ceaf6c7SJ. Bruce Fields static int do_fcntl_add_lease(unsigned int fd, struct file *filp, long arg) 18141da177e4SLinus Torvalds { 18151c7dd2ffSJeff Layton struct file_lock *fl; 1816f7347ce4SLinus Torvalds struct fasync_struct *new; 18171da177e4SLinus Torvalds int error; 18181da177e4SLinus Torvalds 1819c5b1f0d9SArnd Bergmann fl = lease_alloc(filp, arg); 1820c5b1f0d9SArnd Bergmann if (IS_ERR(fl)) 1821c5b1f0d9SArnd Bergmann return PTR_ERR(fl); 18221da177e4SLinus Torvalds 1823f7347ce4SLinus Torvalds new = fasync_alloc(); 1824f7347ce4SLinus Torvalds if (!new) { 1825f7347ce4SLinus Torvalds locks_free_lock(fl); 1826f7347ce4SLinus Torvalds return -ENOMEM; 1827f7347ce4SLinus Torvalds } 18281c7dd2ffSJeff Layton new->fa_fd = fd; 18291da177e4SLinus Torvalds 18301c7dd2ffSJeff Layton error = vfs_setlease(filp, arg, &fl, (void **)&new); 18312dfb928fSJeff Layton if (fl) 18322dfb928fSJeff Layton locks_free_lock(fl); 1833f7347ce4SLinus Torvalds if (new) 1834f7347ce4SLinus Torvalds fasync_free(new); 18351da177e4SLinus Torvalds return error; 18361da177e4SLinus Torvalds } 18371da177e4SLinus Torvalds 18381da177e4SLinus Torvalds /** 18390ceaf6c7SJ. Bruce Fields * fcntl_setlease - sets a lease on an open file 18400ceaf6c7SJ. Bruce Fields * @fd: open file descriptor 18410ceaf6c7SJ. Bruce Fields * @filp: file pointer 18420ceaf6c7SJ. Bruce Fields * @arg: type of lease to obtain 18430ceaf6c7SJ. Bruce Fields * 18440ceaf6c7SJ. Bruce Fields * Call this fcntl to establish a lease on the file. 18450ceaf6c7SJ. Bruce Fields * Note that you also need to call %F_SETSIG to 18460ceaf6c7SJ. Bruce Fields * receive a signal when the lease is broken. 18470ceaf6c7SJ. Bruce Fields */ 18480ceaf6c7SJ. Bruce Fields int fcntl_setlease(unsigned int fd, struct file *filp, long arg) 18490ceaf6c7SJ. Bruce Fields { 18500ceaf6c7SJ. Bruce Fields if (arg == F_UNLCK) 18512ab99ee1SChristoph Hellwig return vfs_setlease(filp, F_UNLCK, NULL, (void **)&filp); 18520ceaf6c7SJ. Bruce Fields return do_fcntl_add_lease(fd, filp, arg); 18530ceaf6c7SJ. Bruce Fields } 18540ceaf6c7SJ. Bruce Fields 18550ceaf6c7SJ. Bruce Fields /** 185629d01b22SJeff Layton * flock_lock_inode_wait - Apply a FLOCK-style lock to a file 185729d01b22SJeff Layton * @inode: inode of the file to apply to 18581da177e4SLinus Torvalds * @fl: The lock to be applied 18591da177e4SLinus Torvalds * 186029d01b22SJeff Layton * Apply a FLOCK style lock request to an inode. 18611da177e4SLinus Torvalds */ 1862616fb38fSBenjamin Coddington static int flock_lock_inode_wait(struct inode *inode, struct file_lock *fl) 18631da177e4SLinus Torvalds { 18641da177e4SLinus Torvalds int error; 18651da177e4SLinus Torvalds might_sleep(); 18661da177e4SLinus Torvalds for (;;) { 186729d01b22SJeff Layton error = flock_lock_inode(inode, fl); 1868bde74e4bSMiklos Szeredi if (error != FILE_LOCK_DEFERRED) 18691da177e4SLinus Torvalds break; 18701da177e4SLinus Torvalds error = wait_event_interruptible(fl->fl_wait, !fl->fl_next); 18711da177e4SLinus Torvalds if (!error) 18721da177e4SLinus Torvalds continue; 18731da177e4SLinus Torvalds 18741da177e4SLinus Torvalds locks_delete_block(fl); 18751da177e4SLinus Torvalds break; 18761da177e4SLinus Torvalds } 18771da177e4SLinus Torvalds return error; 18781da177e4SLinus Torvalds } 18791da177e4SLinus Torvalds 188029d01b22SJeff Layton /** 1881e55c34a6SBenjamin Coddington * locks_lock_inode_wait - Apply a lock to an inode 1882e55c34a6SBenjamin Coddington * @inode: inode of the file to apply to 1883e55c34a6SBenjamin Coddington * @fl: The lock to be applied 1884e55c34a6SBenjamin Coddington * 1885e55c34a6SBenjamin Coddington * Apply a POSIX or FLOCK style lock request to an inode. 1886e55c34a6SBenjamin Coddington */ 1887e55c34a6SBenjamin Coddington int locks_lock_inode_wait(struct inode *inode, struct file_lock *fl) 1888e55c34a6SBenjamin Coddington { 1889e55c34a6SBenjamin Coddington int res = 0; 1890e55c34a6SBenjamin Coddington switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) { 1891e55c34a6SBenjamin Coddington case FL_POSIX: 1892e55c34a6SBenjamin Coddington res = posix_lock_inode_wait(inode, fl); 1893e55c34a6SBenjamin Coddington break; 1894e55c34a6SBenjamin Coddington case FL_FLOCK: 1895e55c34a6SBenjamin Coddington res = flock_lock_inode_wait(inode, fl); 1896e55c34a6SBenjamin Coddington break; 1897e55c34a6SBenjamin Coddington default: 1898e55c34a6SBenjamin Coddington BUG(); 1899e55c34a6SBenjamin Coddington } 1900e55c34a6SBenjamin Coddington return res; 1901e55c34a6SBenjamin Coddington } 1902e55c34a6SBenjamin Coddington EXPORT_SYMBOL(locks_lock_inode_wait); 1903e55c34a6SBenjamin Coddington 1904e55c34a6SBenjamin Coddington /** 19051da177e4SLinus Torvalds * sys_flock: - flock() system call. 19061da177e4SLinus Torvalds * @fd: the file descriptor to lock. 19071da177e4SLinus Torvalds * @cmd: the type of lock to apply. 19081da177e4SLinus Torvalds * 19091da177e4SLinus Torvalds * Apply a %FL_FLOCK style lock to an open file descriptor. 19101da177e4SLinus Torvalds * The @cmd can be one of 19111da177e4SLinus Torvalds * 19121da177e4SLinus Torvalds * %LOCK_SH -- a shared lock. 19131da177e4SLinus Torvalds * 19141da177e4SLinus Torvalds * %LOCK_EX -- an exclusive lock. 19151da177e4SLinus Torvalds * 19161da177e4SLinus Torvalds * %LOCK_UN -- remove an existing lock. 19171da177e4SLinus Torvalds * 19181da177e4SLinus Torvalds * %LOCK_MAND -- a `mandatory' flock. This exists to emulate Windows Share Modes. 19191da177e4SLinus Torvalds * 19201da177e4SLinus Torvalds * %LOCK_MAND can be combined with %LOCK_READ or %LOCK_WRITE to allow other 19211da177e4SLinus Torvalds * processes read and write access respectively. 19221da177e4SLinus Torvalds */ 1923002c8976SHeiko Carstens SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd) 19241da177e4SLinus Torvalds { 19252903ff01SAl Viro struct fd f = fdget(fd); 19261da177e4SLinus Torvalds struct file_lock *lock; 19271da177e4SLinus Torvalds int can_sleep, unlock; 19281da177e4SLinus Torvalds int error; 19291da177e4SLinus Torvalds 19301da177e4SLinus Torvalds error = -EBADF; 19312903ff01SAl Viro if (!f.file) 19321da177e4SLinus Torvalds goto out; 19331da177e4SLinus Torvalds 19341da177e4SLinus Torvalds can_sleep = !(cmd & LOCK_NB); 19351da177e4SLinus Torvalds cmd &= ~LOCK_NB; 19361da177e4SLinus Torvalds unlock = (cmd == LOCK_UN); 19371da177e4SLinus Torvalds 1938aeb5d727SAl Viro if (!unlock && !(cmd & LOCK_MAND) && 19392903ff01SAl Viro !(f.file->f_mode & (FMODE_READ|FMODE_WRITE))) 19401da177e4SLinus Torvalds goto out_putf; 19411da177e4SLinus Torvalds 19426e129d00SJeff Layton lock = flock_make_lock(f.file, cmd); 19436e129d00SJeff Layton if (IS_ERR(lock)) { 19446e129d00SJeff Layton error = PTR_ERR(lock); 19451da177e4SLinus Torvalds goto out_putf; 19466e129d00SJeff Layton } 19476e129d00SJeff Layton 19481da177e4SLinus Torvalds if (can_sleep) 19491da177e4SLinus Torvalds lock->fl_flags |= FL_SLEEP; 19501da177e4SLinus Torvalds 19512903ff01SAl Viro error = security_file_lock(f.file, lock->fl_type); 19521da177e4SLinus Torvalds if (error) 19531da177e4SLinus Torvalds goto out_free; 19541da177e4SLinus Torvalds 195572c2d531SAl Viro if (f.file->f_op->flock) 19562903ff01SAl Viro error = f.file->f_op->flock(f.file, 19571da177e4SLinus Torvalds (can_sleep) ? F_SETLKW : F_SETLK, 19581da177e4SLinus Torvalds lock); 19591da177e4SLinus Torvalds else 19604f656367SBenjamin Coddington error = locks_lock_file_wait(f.file, lock); 19611da177e4SLinus Torvalds 19621da177e4SLinus Torvalds out_free: 19631da177e4SLinus Torvalds locks_free_lock(lock); 19641da177e4SLinus Torvalds 19651da177e4SLinus Torvalds out_putf: 19662903ff01SAl Viro fdput(f); 19671da177e4SLinus Torvalds out: 19681da177e4SLinus Torvalds return error; 19691da177e4SLinus Torvalds } 19701da177e4SLinus Torvalds 19713ee17abdSJ. Bruce Fields /** 19723ee17abdSJ. Bruce Fields * vfs_test_lock - test file byte range lock 19733ee17abdSJ. Bruce Fields * @filp: The file to test lock for 19746924c554SJ. Bruce Fields * @fl: The lock to test; also used to hold result 19753ee17abdSJ. Bruce Fields * 19763ee17abdSJ. Bruce Fields * Returns -ERRNO on failure. Indicates presence of conflicting lock by 19773ee17abdSJ. Bruce Fields * setting conf->fl_type to something other than F_UNLCK. 19783ee17abdSJ. Bruce Fields */ 19793ee17abdSJ. Bruce Fields int vfs_test_lock(struct file *filp, struct file_lock *fl) 19803ee17abdSJ. Bruce Fields { 198172c2d531SAl Viro if (filp->f_op->lock) 19823ee17abdSJ. Bruce Fields return filp->f_op->lock(filp, F_GETLK, fl); 19833ee17abdSJ. Bruce Fields posix_test_lock(filp, fl); 19843ee17abdSJ. Bruce Fields return 0; 19853ee17abdSJ. Bruce Fields } 19863ee17abdSJ. Bruce Fields EXPORT_SYMBOL_GPL(vfs_test_lock); 19873ee17abdSJ. Bruce Fields 1988c2fa1b8aSJ. Bruce Fields static int posix_lock_to_flock(struct flock *flock, struct file_lock *fl) 1989c2fa1b8aSJ. Bruce Fields { 1990cff2fce5SJeff Layton flock->l_pid = IS_OFDLCK(fl) ? -1 : fl->fl_pid; 1991c2fa1b8aSJ. Bruce Fields #if BITS_PER_LONG == 32 1992c2fa1b8aSJ. Bruce Fields /* 1993c2fa1b8aSJ. Bruce Fields * Make sure we can represent the posix lock via 1994c2fa1b8aSJ. Bruce Fields * legacy 32bit flock. 1995c2fa1b8aSJ. Bruce Fields */ 1996c2fa1b8aSJ. Bruce Fields if (fl->fl_start > OFFT_OFFSET_MAX) 1997c2fa1b8aSJ. Bruce Fields return -EOVERFLOW; 1998c2fa1b8aSJ. Bruce Fields if (fl->fl_end != OFFSET_MAX && fl->fl_end > OFFT_OFFSET_MAX) 1999c2fa1b8aSJ. Bruce Fields return -EOVERFLOW; 2000c2fa1b8aSJ. Bruce Fields #endif 2001c2fa1b8aSJ. Bruce Fields flock->l_start = fl->fl_start; 2002c2fa1b8aSJ. Bruce Fields flock->l_len = fl->fl_end == OFFSET_MAX ? 0 : 2003c2fa1b8aSJ. Bruce Fields fl->fl_end - fl->fl_start + 1; 2004c2fa1b8aSJ. Bruce Fields flock->l_whence = 0; 2005129a84deSJ. Bruce Fields flock->l_type = fl->fl_type; 2006c2fa1b8aSJ. Bruce Fields return 0; 2007c2fa1b8aSJ. Bruce Fields } 2008c2fa1b8aSJ. Bruce Fields 2009c2fa1b8aSJ. Bruce Fields #if BITS_PER_LONG == 32 2010c2fa1b8aSJ. Bruce Fields static void posix_lock_to_flock64(struct flock64 *flock, struct file_lock *fl) 2011c2fa1b8aSJ. Bruce Fields { 2012cff2fce5SJeff Layton flock->l_pid = IS_OFDLCK(fl) ? -1 : fl->fl_pid; 2013c2fa1b8aSJ. Bruce Fields flock->l_start = fl->fl_start; 2014c2fa1b8aSJ. Bruce Fields flock->l_len = fl->fl_end == OFFSET_MAX ? 0 : 2015c2fa1b8aSJ. Bruce Fields fl->fl_end - fl->fl_start + 1; 2016c2fa1b8aSJ. Bruce Fields flock->l_whence = 0; 2017c2fa1b8aSJ. Bruce Fields flock->l_type = fl->fl_type; 2018c2fa1b8aSJ. Bruce Fields } 2019c2fa1b8aSJ. Bruce Fields #endif 2020c2fa1b8aSJ. Bruce Fields 20211da177e4SLinus Torvalds /* Report the first existing lock that would conflict with l. 20221da177e4SLinus Torvalds * This implements the F_GETLK command of fcntl(). 20231da177e4SLinus Torvalds */ 2024c1e62b8fSJeff Layton int fcntl_getlk(struct file *filp, unsigned int cmd, struct flock __user *l) 20251da177e4SLinus Torvalds { 20269d6a8c5cSMarc Eshel struct file_lock file_lock; 20271da177e4SLinus Torvalds struct flock flock; 20281da177e4SLinus Torvalds int error; 20291da177e4SLinus Torvalds 20301da177e4SLinus Torvalds error = -EFAULT; 20311da177e4SLinus Torvalds if (copy_from_user(&flock, l, sizeof(flock))) 20321da177e4SLinus Torvalds goto out; 20331da177e4SLinus Torvalds error = -EINVAL; 20341da177e4SLinus Torvalds if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK)) 20351da177e4SLinus Torvalds goto out; 20361da177e4SLinus Torvalds 20371da177e4SLinus Torvalds error = flock_to_posix_lock(filp, &file_lock, &flock); 20381da177e4SLinus Torvalds if (error) 20391da177e4SLinus Torvalds goto out; 20401da177e4SLinus Torvalds 20410d3f7a2dSJeff Layton if (cmd == F_OFD_GETLK) { 204290478939SJeff Layton error = -EINVAL; 204390478939SJeff Layton if (flock.l_pid != 0) 204490478939SJeff Layton goto out; 204590478939SJeff Layton 20465d50ffd7SJeff Layton cmd = F_GETLK; 2047cff2fce5SJeff Layton file_lock.fl_flags |= FL_OFDLCK; 204873a8f5f7SChristoph Hellwig file_lock.fl_owner = filp; 20495d50ffd7SJeff Layton } 20505d50ffd7SJeff Layton 20513ee17abdSJ. Bruce Fields error = vfs_test_lock(filp, &file_lock); 20523ee17abdSJ. Bruce Fields if (error) 20531da177e4SLinus Torvalds goto out; 20541da177e4SLinus Torvalds 20559d6a8c5cSMarc Eshel flock.l_type = file_lock.fl_type; 20569d6a8c5cSMarc Eshel if (file_lock.fl_type != F_UNLCK) { 20579d6a8c5cSMarc Eshel error = posix_lock_to_flock(&flock, &file_lock); 2058c2fa1b8aSJ. Bruce Fields if (error) 2059f328296eSKinglong Mee goto rel_priv; 20601da177e4SLinus Torvalds } 20611da177e4SLinus Torvalds error = -EFAULT; 20621da177e4SLinus Torvalds if (!copy_to_user(l, &flock, sizeof(flock))) 20631da177e4SLinus Torvalds error = 0; 2064f328296eSKinglong Mee rel_priv: 2065f328296eSKinglong Mee locks_release_private(&file_lock); 20661da177e4SLinus Torvalds out: 20671da177e4SLinus Torvalds return error; 20681da177e4SLinus Torvalds } 20691da177e4SLinus Torvalds 20707723ec97SMarc Eshel /** 20717723ec97SMarc Eshel * vfs_lock_file - file byte range lock 20727723ec97SMarc Eshel * @filp: The file to apply the lock to 20737723ec97SMarc Eshel * @cmd: type of locking operation (F_SETLK, F_GETLK, etc.) 20747723ec97SMarc Eshel * @fl: The lock to be applied 2075150b3934SMarc Eshel * @conf: Place to return a copy of the conflicting lock, if found. 2076150b3934SMarc Eshel * 2077150b3934SMarc Eshel * A caller that doesn't care about the conflicting lock may pass NULL 2078150b3934SMarc Eshel * as the final argument. 2079150b3934SMarc Eshel * 2080150b3934SMarc Eshel * If the filesystem defines a private ->lock() method, then @conf will 2081150b3934SMarc Eshel * be left unchanged; so a caller that cares should initialize it to 2082150b3934SMarc Eshel * some acceptable default. 20832beb6614SMarc Eshel * 20842beb6614SMarc Eshel * To avoid blocking kernel daemons, such as lockd, that need to acquire POSIX 20852beb6614SMarc Eshel * locks, the ->lock() interface may return asynchronously, before the lock has 20862beb6614SMarc Eshel * been granted or denied by the underlying filesystem, if (and only if) 20878fb47a4fSJ. Bruce Fields * lm_grant is set. Callers expecting ->lock() to return asynchronously 20882beb6614SMarc Eshel * will only use F_SETLK, not F_SETLKW; they will set FL_SLEEP if (and only if) 20892beb6614SMarc Eshel * the request is for a blocking lock. When ->lock() does return asynchronously, 20908fb47a4fSJ. Bruce Fields * it must return FILE_LOCK_DEFERRED, and call ->lm_grant() when the lock 20912beb6614SMarc Eshel * request completes. 20922beb6614SMarc Eshel * If the request is for non-blocking lock the file system should return 2093bde74e4bSMiklos Szeredi * FILE_LOCK_DEFERRED then try to get the lock and call the callback routine 2094bde74e4bSMiklos Szeredi * with the result. If the request timed out the callback routine will return a 20952beb6614SMarc Eshel * nonzero return code and the file system should release the lock. The file 20962beb6614SMarc Eshel * system is also responsible to keep a corresponding posix lock when it 20972beb6614SMarc Eshel * grants a lock so the VFS can find out which locks are locally held and do 20982beb6614SMarc Eshel * the correct lock cleanup when required. 20992beb6614SMarc Eshel * The underlying filesystem must not drop the kernel lock or call 21008fb47a4fSJ. Bruce Fields * ->lm_grant() before returning to the caller with a FILE_LOCK_DEFERRED 21012beb6614SMarc Eshel * return code. 21027723ec97SMarc Eshel */ 2103150b3934SMarc Eshel int vfs_lock_file(struct file *filp, unsigned int cmd, struct file_lock *fl, struct file_lock *conf) 21047723ec97SMarc Eshel { 210572c2d531SAl Viro if (filp->f_op->lock) 21067723ec97SMarc Eshel return filp->f_op->lock(filp, cmd, fl); 21077723ec97SMarc Eshel else 2108150b3934SMarc Eshel return posix_lock_file(filp, fl, conf); 21097723ec97SMarc Eshel } 21107723ec97SMarc Eshel EXPORT_SYMBOL_GPL(vfs_lock_file); 21117723ec97SMarc Eshel 2112b648a6deSMiklos Szeredi static int do_lock_file_wait(struct file *filp, unsigned int cmd, 2113b648a6deSMiklos Szeredi struct file_lock *fl) 2114b648a6deSMiklos Szeredi { 2115b648a6deSMiklos Szeredi int error; 2116b648a6deSMiklos Szeredi 2117b648a6deSMiklos Szeredi error = security_file_lock(filp, fl->fl_type); 2118b648a6deSMiklos Szeredi if (error) 2119b648a6deSMiklos Szeredi return error; 2120b648a6deSMiklos Szeredi 2121b648a6deSMiklos Szeredi for (;;) { 2122764c76b3SMiklos Szeredi error = vfs_lock_file(filp, cmd, fl, NULL); 2123b648a6deSMiklos Szeredi if (error != FILE_LOCK_DEFERRED) 2124b648a6deSMiklos Szeredi break; 2125764c76b3SMiklos Szeredi error = wait_event_interruptible(fl->fl_wait, !fl->fl_next); 2126b648a6deSMiklos Szeredi if (!error) 2127b648a6deSMiklos Szeredi continue; 2128b648a6deSMiklos Szeredi 2129b648a6deSMiklos Szeredi locks_delete_block(fl); 2130b648a6deSMiklos Szeredi break; 2131b648a6deSMiklos Szeredi } 2132b648a6deSMiklos Szeredi 2133b648a6deSMiklos Szeredi return error; 2134b648a6deSMiklos Szeredi } 2135b648a6deSMiklos Szeredi 21366ca7d910SBenjamin Coddington /* Ensure that fl->fl_file has compatible f_mode for F_SETLK calls */ 2137cf01f4eeSJeff Layton static int 2138cf01f4eeSJeff Layton check_fmode_for_setlk(struct file_lock *fl) 2139cf01f4eeSJeff Layton { 2140cf01f4eeSJeff Layton switch (fl->fl_type) { 2141cf01f4eeSJeff Layton case F_RDLCK: 2142cf01f4eeSJeff Layton if (!(fl->fl_file->f_mode & FMODE_READ)) 2143cf01f4eeSJeff Layton return -EBADF; 2144cf01f4eeSJeff Layton break; 2145cf01f4eeSJeff Layton case F_WRLCK: 2146cf01f4eeSJeff Layton if (!(fl->fl_file->f_mode & FMODE_WRITE)) 2147cf01f4eeSJeff Layton return -EBADF; 2148cf01f4eeSJeff Layton } 2149cf01f4eeSJeff Layton return 0; 2150cf01f4eeSJeff Layton } 2151cf01f4eeSJeff Layton 21521da177e4SLinus Torvalds /* Apply the lock described by l to an open file descriptor. 21531da177e4SLinus Torvalds * This implements both the F_SETLK and F_SETLKW commands of fcntl(). 21541da177e4SLinus Torvalds */ 2155c293621bSPeter Staubach int fcntl_setlk(unsigned int fd, struct file *filp, unsigned int cmd, 2156c293621bSPeter Staubach struct flock __user *l) 21571da177e4SLinus Torvalds { 21581da177e4SLinus Torvalds struct file_lock *file_lock = locks_alloc_lock(); 21591da177e4SLinus Torvalds struct flock flock; 21601da177e4SLinus Torvalds struct inode *inode; 21610b2bac2fSAl Viro struct file *f; 21621da177e4SLinus Torvalds int error; 21631da177e4SLinus Torvalds 21641da177e4SLinus Torvalds if (file_lock == NULL) 21651da177e4SLinus Torvalds return -ENOLCK; 21661da177e4SLinus Torvalds 21671da177e4SLinus Torvalds /* 21681da177e4SLinus Torvalds * This might block, so we do it before checking the inode. 21691da177e4SLinus Torvalds */ 21701da177e4SLinus Torvalds error = -EFAULT; 21711da177e4SLinus Torvalds if (copy_from_user(&flock, l, sizeof(flock))) 21721da177e4SLinus Torvalds goto out; 21731da177e4SLinus Torvalds 2174496ad9aaSAl Viro inode = file_inode(filp); 21751da177e4SLinus Torvalds 21761da177e4SLinus Torvalds /* Don't allow mandatory locks on files that may be memory mapped 21771da177e4SLinus Torvalds * and shared. 21781da177e4SLinus Torvalds */ 2179a16877caSPavel Emelyanov if (mandatory_lock(inode) && mapping_writably_mapped(filp->f_mapping)) { 21801da177e4SLinus Torvalds error = -EAGAIN; 21811da177e4SLinus Torvalds goto out; 21821da177e4SLinus Torvalds } 21831da177e4SLinus Torvalds 21841da177e4SLinus Torvalds error = flock_to_posix_lock(filp, file_lock, &flock); 21851da177e4SLinus Torvalds if (error) 21861da177e4SLinus Torvalds goto out; 21875d50ffd7SJeff Layton 2188cf01f4eeSJeff Layton error = check_fmode_for_setlk(file_lock); 2189cf01f4eeSJeff Layton if (error) 2190cf01f4eeSJeff Layton goto out; 2191cf01f4eeSJeff Layton 21925d50ffd7SJeff Layton /* 21935d50ffd7SJeff Layton * If the cmd is requesting file-private locks, then set the 2194cff2fce5SJeff Layton * FL_OFDLCK flag and override the owner. 21955d50ffd7SJeff Layton */ 21965d50ffd7SJeff Layton switch (cmd) { 21970d3f7a2dSJeff Layton case F_OFD_SETLK: 219890478939SJeff Layton error = -EINVAL; 219990478939SJeff Layton if (flock.l_pid != 0) 220090478939SJeff Layton goto out; 220190478939SJeff Layton 22025d50ffd7SJeff Layton cmd = F_SETLK; 2203cff2fce5SJeff Layton file_lock->fl_flags |= FL_OFDLCK; 220473a8f5f7SChristoph Hellwig file_lock->fl_owner = filp; 22055d50ffd7SJeff Layton break; 22060d3f7a2dSJeff Layton case F_OFD_SETLKW: 220790478939SJeff Layton error = -EINVAL; 220890478939SJeff Layton if (flock.l_pid != 0) 220990478939SJeff Layton goto out; 221090478939SJeff Layton 22115d50ffd7SJeff Layton cmd = F_SETLKW; 2212cff2fce5SJeff Layton file_lock->fl_flags |= FL_OFDLCK; 221373a8f5f7SChristoph Hellwig file_lock->fl_owner = filp; 22145d50ffd7SJeff Layton /* Fallthrough */ 22155d50ffd7SJeff Layton case F_SETLKW: 22161da177e4SLinus Torvalds file_lock->fl_flags |= FL_SLEEP; 22171da177e4SLinus Torvalds } 22181da177e4SLinus Torvalds 2219b648a6deSMiklos Szeredi error = do_lock_file_wait(filp, cmd, file_lock); 2220c293621bSPeter Staubach 2221c293621bSPeter Staubach /* 2222*0752ba80SJeff Layton * Attempt to detect a close/fcntl race and recover by releasing the 2223*0752ba80SJeff Layton * lock that was just acquired. There is no need to do that when we're 2224*0752ba80SJeff Layton * unlocking though, or for OFD locks. 2225c293621bSPeter Staubach */ 2226*0752ba80SJeff Layton if (!error && file_lock->fl_type != F_UNLCK && 2227*0752ba80SJeff Layton !(file_lock->fl_flags & FL_OFDLCK)) { 22280b2bac2fSAl Viro /* 22297f3697e2SJeff Layton * We need that spin_lock here - it prevents reordering between 22307f3697e2SJeff Layton * update of i_flctx->flc_posix and check for it done in 22317f3697e2SJeff Layton * close(). rcu_read_lock() wouldn't do. 22320b2bac2fSAl Viro */ 22330b2bac2fSAl Viro spin_lock(¤t->files->file_lock); 22340b2bac2fSAl Viro f = fcheck(fd); 22350b2bac2fSAl Viro spin_unlock(¤t->files->file_lock); 22367f3697e2SJeff Layton if (f != filp) { 22377f3697e2SJeff Layton file_lock->fl_type = F_UNLCK; 22387f3697e2SJeff Layton error = do_lock_file_wait(filp, cmd, file_lock); 22397f3697e2SJeff Layton WARN_ON_ONCE(error); 22407f3697e2SJeff Layton error = -EBADF; 2241c293621bSPeter Staubach } 22427f3697e2SJeff Layton } 22431da177e4SLinus Torvalds out: 22441da177e4SLinus Torvalds locks_free_lock(file_lock); 22451da177e4SLinus Torvalds return error; 22461da177e4SLinus Torvalds } 22471da177e4SLinus Torvalds 22481da177e4SLinus Torvalds #if BITS_PER_LONG == 32 22491da177e4SLinus Torvalds /* Report the first existing lock that would conflict with l. 22501da177e4SLinus Torvalds * This implements the F_GETLK command of fcntl(). 22511da177e4SLinus Torvalds */ 2252c1e62b8fSJeff Layton int fcntl_getlk64(struct file *filp, unsigned int cmd, struct flock64 __user *l) 22531da177e4SLinus Torvalds { 22549d6a8c5cSMarc Eshel struct file_lock file_lock; 22551da177e4SLinus Torvalds struct flock64 flock; 22561da177e4SLinus Torvalds int error; 22571da177e4SLinus Torvalds 22581da177e4SLinus Torvalds error = -EFAULT; 22591da177e4SLinus Torvalds if (copy_from_user(&flock, l, sizeof(flock))) 22601da177e4SLinus Torvalds goto out; 22611da177e4SLinus Torvalds error = -EINVAL; 22621da177e4SLinus Torvalds if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK)) 22631da177e4SLinus Torvalds goto out; 22641da177e4SLinus Torvalds 22651da177e4SLinus Torvalds error = flock64_to_posix_lock(filp, &file_lock, &flock); 22661da177e4SLinus Torvalds if (error) 22671da177e4SLinus Torvalds goto out; 22681da177e4SLinus Torvalds 22690d3f7a2dSJeff Layton if (cmd == F_OFD_GETLK) { 227090478939SJeff Layton error = -EINVAL; 227190478939SJeff Layton if (flock.l_pid != 0) 227290478939SJeff Layton goto out; 227390478939SJeff Layton 22745d50ffd7SJeff Layton cmd = F_GETLK64; 2275cff2fce5SJeff Layton file_lock.fl_flags |= FL_OFDLCK; 227673a8f5f7SChristoph Hellwig file_lock.fl_owner = filp; 22775d50ffd7SJeff Layton } 22785d50ffd7SJeff Layton 22793ee17abdSJ. Bruce Fields error = vfs_test_lock(filp, &file_lock); 22803ee17abdSJ. Bruce Fields if (error) 22811da177e4SLinus Torvalds goto out; 22821da177e4SLinus Torvalds 22839d6a8c5cSMarc Eshel flock.l_type = file_lock.fl_type; 22849d6a8c5cSMarc Eshel if (file_lock.fl_type != F_UNLCK) 22859d6a8c5cSMarc Eshel posix_lock_to_flock64(&flock, &file_lock); 22869d6a8c5cSMarc Eshel 22871da177e4SLinus Torvalds error = -EFAULT; 22881da177e4SLinus Torvalds if (!copy_to_user(l, &flock, sizeof(flock))) 22891da177e4SLinus Torvalds error = 0; 22901da177e4SLinus Torvalds 2291f328296eSKinglong Mee locks_release_private(&file_lock); 22921da177e4SLinus Torvalds out: 22931da177e4SLinus Torvalds return error; 22941da177e4SLinus Torvalds } 22951da177e4SLinus Torvalds 22961da177e4SLinus Torvalds /* Apply the lock described by l to an open file descriptor. 22971da177e4SLinus Torvalds * This implements both the F_SETLK and F_SETLKW commands of fcntl(). 22981da177e4SLinus Torvalds */ 2299c293621bSPeter Staubach int fcntl_setlk64(unsigned int fd, struct file *filp, unsigned int cmd, 2300c293621bSPeter Staubach struct flock64 __user *l) 23011da177e4SLinus Torvalds { 23021da177e4SLinus Torvalds struct file_lock *file_lock = locks_alloc_lock(); 23031da177e4SLinus Torvalds struct flock64 flock; 23041da177e4SLinus Torvalds struct inode *inode; 23050b2bac2fSAl Viro struct file *f; 23061da177e4SLinus Torvalds int error; 23071da177e4SLinus Torvalds 23081da177e4SLinus Torvalds if (file_lock == NULL) 23091da177e4SLinus Torvalds return -ENOLCK; 23101da177e4SLinus Torvalds 23111da177e4SLinus Torvalds /* 23121da177e4SLinus Torvalds * This might block, so we do it before checking the inode. 23131da177e4SLinus Torvalds */ 23141da177e4SLinus Torvalds error = -EFAULT; 23151da177e4SLinus Torvalds if (copy_from_user(&flock, l, sizeof(flock))) 23161da177e4SLinus Torvalds goto out; 23171da177e4SLinus Torvalds 2318496ad9aaSAl Viro inode = file_inode(filp); 23191da177e4SLinus Torvalds 23201da177e4SLinus Torvalds /* Don't allow mandatory locks on files that may be memory mapped 23211da177e4SLinus Torvalds * and shared. 23221da177e4SLinus Torvalds */ 2323a16877caSPavel Emelyanov if (mandatory_lock(inode) && mapping_writably_mapped(filp->f_mapping)) { 23241da177e4SLinus Torvalds error = -EAGAIN; 23251da177e4SLinus Torvalds goto out; 23261da177e4SLinus Torvalds } 23271da177e4SLinus Torvalds 23281da177e4SLinus Torvalds error = flock64_to_posix_lock(filp, file_lock, &flock); 23291da177e4SLinus Torvalds if (error) 23301da177e4SLinus Torvalds goto out; 23315d50ffd7SJeff Layton 2332cf01f4eeSJeff Layton error = check_fmode_for_setlk(file_lock); 2333cf01f4eeSJeff Layton if (error) 2334cf01f4eeSJeff Layton goto out; 2335cf01f4eeSJeff Layton 23365d50ffd7SJeff Layton /* 23375d50ffd7SJeff Layton * If the cmd is requesting file-private locks, then set the 2338cff2fce5SJeff Layton * FL_OFDLCK flag and override the owner. 23395d50ffd7SJeff Layton */ 23405d50ffd7SJeff Layton switch (cmd) { 23410d3f7a2dSJeff Layton case F_OFD_SETLK: 234290478939SJeff Layton error = -EINVAL; 234390478939SJeff Layton if (flock.l_pid != 0) 234490478939SJeff Layton goto out; 234590478939SJeff Layton 23465d50ffd7SJeff Layton cmd = F_SETLK64; 2347cff2fce5SJeff Layton file_lock->fl_flags |= FL_OFDLCK; 234873a8f5f7SChristoph Hellwig file_lock->fl_owner = filp; 23495d50ffd7SJeff Layton break; 23500d3f7a2dSJeff Layton case F_OFD_SETLKW: 235190478939SJeff Layton error = -EINVAL; 235290478939SJeff Layton if (flock.l_pid != 0) 235390478939SJeff Layton goto out; 235490478939SJeff Layton 23555d50ffd7SJeff Layton cmd = F_SETLKW64; 2356cff2fce5SJeff Layton file_lock->fl_flags |= FL_OFDLCK; 235773a8f5f7SChristoph Hellwig file_lock->fl_owner = filp; 23585d50ffd7SJeff Layton /* Fallthrough */ 23595d50ffd7SJeff Layton case F_SETLKW64: 23601da177e4SLinus Torvalds file_lock->fl_flags |= FL_SLEEP; 23611da177e4SLinus Torvalds } 23621da177e4SLinus Torvalds 2363b648a6deSMiklos Szeredi error = do_lock_file_wait(filp, cmd, file_lock); 2364c293621bSPeter Staubach 2365c293621bSPeter Staubach /* 2366*0752ba80SJeff Layton * Attempt to detect a close/fcntl race and recover by releasing the 2367*0752ba80SJeff Layton * lock that was just acquired. There is no need to do that when we're 2368*0752ba80SJeff Layton * unlocking though, or for OFD locks. 2369c293621bSPeter Staubach */ 2370*0752ba80SJeff Layton if (!error && file_lock->fl_type != F_UNLCK && 2371*0752ba80SJeff Layton !(file_lock->fl_flags & FL_OFDLCK)) { 23727f3697e2SJeff Layton /* 23737f3697e2SJeff Layton * We need that spin_lock here - it prevents reordering between 23747f3697e2SJeff Layton * update of i_flctx->flc_posix and check for it done in 23757f3697e2SJeff Layton * close(). rcu_read_lock() wouldn't do. 23767f3697e2SJeff Layton */ 23770b2bac2fSAl Viro spin_lock(¤t->files->file_lock); 23780b2bac2fSAl Viro f = fcheck(fd); 23790b2bac2fSAl Viro spin_unlock(¤t->files->file_lock); 23807f3697e2SJeff Layton if (f != filp) { 23817f3697e2SJeff Layton file_lock->fl_type = F_UNLCK; 23827f3697e2SJeff Layton error = do_lock_file_wait(filp, cmd, file_lock); 23837f3697e2SJeff Layton WARN_ON_ONCE(error); 23847f3697e2SJeff Layton error = -EBADF; 2385c293621bSPeter Staubach } 23867f3697e2SJeff Layton } 23871da177e4SLinus Torvalds out: 23881da177e4SLinus Torvalds locks_free_lock(file_lock); 23891da177e4SLinus Torvalds return error; 23901da177e4SLinus Torvalds } 23911da177e4SLinus Torvalds #endif /* BITS_PER_LONG == 32 */ 23921da177e4SLinus Torvalds 23931da177e4SLinus Torvalds /* 23941da177e4SLinus Torvalds * This function is called when the file is being removed 23951da177e4SLinus Torvalds * from the task's fd array. POSIX locks belonging to this task 23961da177e4SLinus Torvalds * are deleted at this time. 23971da177e4SLinus Torvalds */ 23981da177e4SLinus Torvalds void locks_remove_posix(struct file *filp, fl_owner_t owner) 23991da177e4SLinus Torvalds { 2400ff7b86b8SMiklos Szeredi struct file_lock lock; 2401128a3785SDmitry Vyukov struct file_lock_context *ctx; 24021da177e4SLinus Torvalds 24031da177e4SLinus Torvalds /* 24041da177e4SLinus Torvalds * If there are no locks held on this file, we don't need to call 24051da177e4SLinus Torvalds * posix_lock_file(). Another process could be setting a lock on this 24061da177e4SLinus Torvalds * file at the same time, but we wouldn't remove that lock anyway. 24071da177e4SLinus Torvalds */ 2408128a3785SDmitry Vyukov ctx = smp_load_acquire(&file_inode(filp)->i_flctx); 2409bd61e0a9SJeff Layton if (!ctx || list_empty(&ctx->flc_posix)) 24101da177e4SLinus Torvalds return; 24111da177e4SLinus Torvalds 24121da177e4SLinus Torvalds lock.fl_type = F_UNLCK; 241375e1fcc0SMiklos Szeredi lock.fl_flags = FL_POSIX | FL_CLOSE; 24141da177e4SLinus Torvalds lock.fl_start = 0; 24151da177e4SLinus Torvalds lock.fl_end = OFFSET_MAX; 24161da177e4SLinus Torvalds lock.fl_owner = owner; 24171da177e4SLinus Torvalds lock.fl_pid = current->tgid; 24181da177e4SLinus Torvalds lock.fl_file = filp; 24191da177e4SLinus Torvalds lock.fl_ops = NULL; 24201da177e4SLinus Torvalds lock.fl_lmops = NULL; 24211da177e4SLinus Torvalds 2422150b3934SMarc Eshel vfs_lock_file(filp, F_SETLK, &lock, NULL); 24231da177e4SLinus Torvalds 24241da177e4SLinus Torvalds if (lock.fl_ops && lock.fl_ops->fl_release_private) 24251da177e4SLinus Torvalds lock.fl_ops->fl_release_private(&lock); 24261da177e4SLinus Torvalds } 24271da177e4SLinus Torvalds 24281da177e4SLinus Torvalds EXPORT_SYMBOL(locks_remove_posix); 24291da177e4SLinus Torvalds 24303d8e560dSJeff Layton /* The i_flctx must be valid when calling into here */ 2431dd459bb1SJeff Layton static void 2432128a3785SDmitry Vyukov locks_remove_flock(struct file *filp, struct file_lock_context *flctx) 2433dd459bb1SJeff Layton { 2434dd459bb1SJeff Layton struct file_lock fl = { 2435dd459bb1SJeff Layton .fl_owner = filp, 2436dd459bb1SJeff Layton .fl_pid = current->tgid, 2437dd459bb1SJeff Layton .fl_file = filp, 2438dd459bb1SJeff Layton .fl_flags = FL_FLOCK, 2439dd459bb1SJeff Layton .fl_type = F_UNLCK, 2440dd459bb1SJeff Layton .fl_end = OFFSET_MAX, 2441dd459bb1SJeff Layton }; 2442bcd7f78dSJeff Layton struct inode *inode = file_inode(filp); 2443dd459bb1SJeff Layton 24443d8e560dSJeff Layton if (list_empty(&flctx->flc_flock)) 2445dd459bb1SJeff Layton return; 2446dd459bb1SJeff Layton 2447dd459bb1SJeff Layton if (filp->f_op->flock) 2448dd459bb1SJeff Layton filp->f_op->flock(filp, F_SETLKW, &fl); 2449dd459bb1SJeff Layton else 2450bcd7f78dSJeff Layton flock_lock_inode(inode, &fl); 2451dd459bb1SJeff Layton 2452dd459bb1SJeff Layton if (fl.fl_ops && fl.fl_ops->fl_release_private) 2453dd459bb1SJeff Layton fl.fl_ops->fl_release_private(&fl); 2454dd459bb1SJeff Layton } 2455dd459bb1SJeff Layton 24563d8e560dSJeff Layton /* The i_flctx must be valid when calling into here */ 24578634b51fSJeff Layton static void 2458128a3785SDmitry Vyukov locks_remove_lease(struct file *filp, struct file_lock_context *ctx) 24598634b51fSJeff Layton { 24608634b51fSJeff Layton struct file_lock *fl, *tmp; 24618634b51fSJeff Layton LIST_HEAD(dispose); 24628634b51fSJeff Layton 24633d8e560dSJeff Layton if (list_empty(&ctx->flc_lease)) 24648634b51fSJeff Layton return; 24658634b51fSJeff Layton 24666109c850SJeff Layton spin_lock(&ctx->flc_lock); 24678634b51fSJeff Layton list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list) 2468c4e136cdSJeff Layton if (filp == fl->fl_file) 24697448cc37SJeff Layton lease_modify(fl, F_UNLCK, &dispose); 24706109c850SJeff Layton spin_unlock(&ctx->flc_lock); 24718634b51fSJeff Layton locks_dispose_list(&dispose); 24728634b51fSJeff Layton } 24738634b51fSJeff Layton 24741da177e4SLinus Torvalds /* 24751da177e4SLinus Torvalds * This function is called on the last close of an open file. 24761da177e4SLinus Torvalds */ 247778ed8a13SJeff Layton void locks_remove_file(struct file *filp) 24781da177e4SLinus Torvalds { 2479128a3785SDmitry Vyukov struct file_lock_context *ctx; 2480128a3785SDmitry Vyukov 2481128a3785SDmitry Vyukov ctx = smp_load_acquire(&file_inode(filp)->i_flctx); 2482128a3785SDmitry Vyukov if (!ctx) 24833d8e560dSJeff Layton return; 24843d8e560dSJeff Layton 2485dd459bb1SJeff Layton /* remove any OFD locks */ 248673a8f5f7SChristoph Hellwig locks_remove_posix(filp, filp); 24875d50ffd7SJeff Layton 2488dd459bb1SJeff Layton /* remove flock locks */ 2489128a3785SDmitry Vyukov locks_remove_flock(filp, ctx); 2490dd459bb1SJeff Layton 24918634b51fSJeff Layton /* remove any leases */ 2492128a3785SDmitry Vyukov locks_remove_lease(filp, ctx); 24931da177e4SLinus Torvalds } 24941da177e4SLinus Torvalds 24951da177e4SLinus Torvalds /** 24961da177e4SLinus Torvalds * posix_unblock_lock - stop waiting for a file lock 24971da177e4SLinus Torvalds * @waiter: the lock which was waiting 24981da177e4SLinus Torvalds * 24991da177e4SLinus Torvalds * lockd needs to block waiting for locks. 25001da177e4SLinus Torvalds */ 250164a318eeSJ. Bruce Fields int 2502f891a29fSJeff Layton posix_unblock_lock(struct file_lock *waiter) 25031da177e4SLinus Torvalds { 250464a318eeSJ. Bruce Fields int status = 0; 250564a318eeSJ. Bruce Fields 25067b2296afSJeff Layton spin_lock(&blocked_lock_lock); 25075996a298SJ. Bruce Fields if (waiter->fl_next) 25081da177e4SLinus Torvalds __locks_delete_block(waiter); 250964a318eeSJ. Bruce Fields else 251064a318eeSJ. Bruce Fields status = -ENOENT; 25117b2296afSJeff Layton spin_unlock(&blocked_lock_lock); 251264a318eeSJ. Bruce Fields return status; 25131da177e4SLinus Torvalds } 25141da177e4SLinus Torvalds EXPORT_SYMBOL(posix_unblock_lock); 25151da177e4SLinus Torvalds 25169b9d2ab4SMarc Eshel /** 25179b9d2ab4SMarc Eshel * vfs_cancel_lock - file byte range unblock lock 25189b9d2ab4SMarc Eshel * @filp: The file to apply the unblock to 25199b9d2ab4SMarc Eshel * @fl: The lock to be unblocked 25209b9d2ab4SMarc Eshel * 25219b9d2ab4SMarc Eshel * Used by lock managers to cancel blocked requests 25229b9d2ab4SMarc Eshel */ 25239b9d2ab4SMarc Eshel int vfs_cancel_lock(struct file *filp, struct file_lock *fl) 25249b9d2ab4SMarc Eshel { 252572c2d531SAl Viro if (filp->f_op->lock) 25269b9d2ab4SMarc Eshel return filp->f_op->lock(filp, F_CANCELLK, fl); 25279b9d2ab4SMarc Eshel return 0; 25289b9d2ab4SMarc Eshel } 25299b9d2ab4SMarc Eshel 25309b9d2ab4SMarc Eshel EXPORT_SYMBOL_GPL(vfs_cancel_lock); 25319b9d2ab4SMarc Eshel 25327f8ada98SPavel Emelyanov #ifdef CONFIG_PROC_FS 2533d8ba7a36SAlexey Dobriyan #include <linux/proc_fs.h> 25347f8ada98SPavel Emelyanov #include <linux/seq_file.h> 25357f8ada98SPavel Emelyanov 25367012b02aSJeff Layton struct locks_iterator { 25377012b02aSJeff Layton int li_cpu; 25387012b02aSJeff Layton loff_t li_pos; 25397012b02aSJeff Layton }; 25407012b02aSJeff Layton 25417f8ada98SPavel Emelyanov static void lock_get_status(struct seq_file *f, struct file_lock *fl, 254299dc8292SJerome Marchand loff_t id, char *pfx) 25431da177e4SLinus Torvalds { 25441da177e4SLinus Torvalds struct inode *inode = NULL; 2545ab1f1611SVitaliy Gusev unsigned int fl_pid; 2546ab1f1611SVitaliy Gusev 2547ab1f1611SVitaliy Gusev if (fl->fl_nspid) 25486c5f3e7bSPavel Emelyanov fl_pid = pid_vnr(fl->fl_nspid); 2549ab1f1611SVitaliy Gusev else 2550ab1f1611SVitaliy Gusev fl_pid = fl->fl_pid; 25511da177e4SLinus Torvalds 25521da177e4SLinus Torvalds if (fl->fl_file != NULL) 2553496ad9aaSAl Viro inode = file_inode(fl->fl_file); 25541da177e4SLinus Torvalds 255599dc8292SJerome Marchand seq_printf(f, "%lld:%s ", id, pfx); 25561da177e4SLinus Torvalds if (IS_POSIX(fl)) { 2557c918d42aSJeff Layton if (fl->fl_flags & FL_ACCESS) 25585315c26aSFabian Frederick seq_puts(f, "ACCESS"); 2559cff2fce5SJeff Layton else if (IS_OFDLCK(fl)) 25605315c26aSFabian Frederick seq_puts(f, "OFDLCK"); 2561c918d42aSJeff Layton else 25625315c26aSFabian Frederick seq_puts(f, "POSIX "); 2563c918d42aSJeff Layton 2564c918d42aSJeff Layton seq_printf(f, " %s ", 25651da177e4SLinus Torvalds (inode == NULL) ? "*NOINODE*" : 2566a16877caSPavel Emelyanov mandatory_lock(inode) ? "MANDATORY" : "ADVISORY "); 25671da177e4SLinus Torvalds } else if (IS_FLOCK(fl)) { 25681da177e4SLinus Torvalds if (fl->fl_type & LOCK_MAND) { 25695315c26aSFabian Frederick seq_puts(f, "FLOCK MSNFS "); 25701da177e4SLinus Torvalds } else { 25715315c26aSFabian Frederick seq_puts(f, "FLOCK ADVISORY "); 25721da177e4SLinus Torvalds } 25731da177e4SLinus Torvalds } else if (IS_LEASE(fl)) { 25748144f1f6SJeff Layton if (fl->fl_flags & FL_DELEG) 25758144f1f6SJeff Layton seq_puts(f, "DELEG "); 25768144f1f6SJeff Layton else 25775315c26aSFabian Frederick seq_puts(f, "LEASE "); 25788144f1f6SJeff Layton 2579ab83fa4bSJ. Bruce Fields if (lease_breaking(fl)) 25805315c26aSFabian Frederick seq_puts(f, "BREAKING "); 25811da177e4SLinus Torvalds else if (fl->fl_file) 25825315c26aSFabian Frederick seq_puts(f, "ACTIVE "); 25831da177e4SLinus Torvalds else 25845315c26aSFabian Frederick seq_puts(f, "BREAKER "); 25851da177e4SLinus Torvalds } else { 25865315c26aSFabian Frederick seq_puts(f, "UNKNOWN UNKNOWN "); 25871da177e4SLinus Torvalds } 25881da177e4SLinus Torvalds if (fl->fl_type & LOCK_MAND) { 25897f8ada98SPavel Emelyanov seq_printf(f, "%s ", 25901da177e4SLinus Torvalds (fl->fl_type & LOCK_READ) 25911da177e4SLinus Torvalds ? (fl->fl_type & LOCK_WRITE) ? "RW " : "READ " 25921da177e4SLinus Torvalds : (fl->fl_type & LOCK_WRITE) ? "WRITE" : "NONE "); 25931da177e4SLinus Torvalds } else { 25947f8ada98SPavel Emelyanov seq_printf(f, "%s ", 2595ab83fa4bSJ. Bruce Fields (lease_breaking(fl)) 25960ee5c6d6SJeff Layton ? (fl->fl_type == F_UNLCK) ? "UNLCK" : "READ " 25970ee5c6d6SJeff Layton : (fl->fl_type == F_WRLCK) ? "WRITE" : "READ "); 25981da177e4SLinus Torvalds } 25991da177e4SLinus Torvalds if (inode) { 26003648888eSJeff Layton /* userspace relies on this representation of dev_t */ 2601ab1f1611SVitaliy Gusev seq_printf(f, "%d %02x:%02x:%ld ", fl_pid, 26021da177e4SLinus Torvalds MAJOR(inode->i_sb->s_dev), 26031da177e4SLinus Torvalds MINOR(inode->i_sb->s_dev), inode->i_ino); 26041da177e4SLinus Torvalds } else { 2605ab1f1611SVitaliy Gusev seq_printf(f, "%d <none>:0 ", fl_pid); 26061da177e4SLinus Torvalds } 26071da177e4SLinus Torvalds if (IS_POSIX(fl)) { 26081da177e4SLinus Torvalds if (fl->fl_end == OFFSET_MAX) 26097f8ada98SPavel Emelyanov seq_printf(f, "%Ld EOF\n", fl->fl_start); 26101da177e4SLinus Torvalds else 26117f8ada98SPavel Emelyanov seq_printf(f, "%Ld %Ld\n", fl->fl_start, fl->fl_end); 26121da177e4SLinus Torvalds } else { 26135315c26aSFabian Frederick seq_puts(f, "0 EOF\n"); 26141da177e4SLinus Torvalds } 26151da177e4SLinus Torvalds } 26161da177e4SLinus Torvalds 26177f8ada98SPavel Emelyanov static int locks_show(struct seq_file *f, void *v) 26181da177e4SLinus Torvalds { 26197012b02aSJeff Layton struct locks_iterator *iter = f->private; 26207f8ada98SPavel Emelyanov struct file_lock *fl, *bfl; 26217f8ada98SPavel Emelyanov 2622139ca04eSJeff Layton fl = hlist_entry(v, struct file_lock, fl_link); 26237f8ada98SPavel Emelyanov 26247012b02aSJeff Layton lock_get_status(f, fl, iter->li_pos, ""); 26257f8ada98SPavel Emelyanov 26267f8ada98SPavel Emelyanov list_for_each_entry(bfl, &fl->fl_block, fl_block) 26277012b02aSJeff Layton lock_get_status(f, bfl, iter->li_pos, " ->"); 26287f8ada98SPavel Emelyanov 26297f8ada98SPavel Emelyanov return 0; 26301da177e4SLinus Torvalds } 26311da177e4SLinus Torvalds 26326c8c9031SAndrey Vagin static void __show_fd_locks(struct seq_file *f, 26336c8c9031SAndrey Vagin struct list_head *head, int *id, 26346c8c9031SAndrey Vagin struct file *filp, struct files_struct *files) 26356c8c9031SAndrey Vagin { 26366c8c9031SAndrey Vagin struct file_lock *fl; 26376c8c9031SAndrey Vagin 26386c8c9031SAndrey Vagin list_for_each_entry(fl, head, fl_list) { 26396c8c9031SAndrey Vagin 26406c8c9031SAndrey Vagin if (filp != fl->fl_file) 26416c8c9031SAndrey Vagin continue; 26426c8c9031SAndrey Vagin if (fl->fl_owner != files && 26436c8c9031SAndrey Vagin fl->fl_owner != filp) 26446c8c9031SAndrey Vagin continue; 26456c8c9031SAndrey Vagin 26466c8c9031SAndrey Vagin (*id)++; 26476c8c9031SAndrey Vagin seq_puts(f, "lock:\t"); 26486c8c9031SAndrey Vagin lock_get_status(f, fl, *id, ""); 26496c8c9031SAndrey Vagin } 26506c8c9031SAndrey Vagin } 26516c8c9031SAndrey Vagin 26526c8c9031SAndrey Vagin void show_fd_locks(struct seq_file *f, 26536c8c9031SAndrey Vagin struct file *filp, struct files_struct *files) 26546c8c9031SAndrey Vagin { 26556c8c9031SAndrey Vagin struct inode *inode = file_inode(filp); 26566c8c9031SAndrey Vagin struct file_lock_context *ctx; 26576c8c9031SAndrey Vagin int id = 0; 26586c8c9031SAndrey Vagin 2659128a3785SDmitry Vyukov ctx = smp_load_acquire(&inode->i_flctx); 26606c8c9031SAndrey Vagin if (!ctx) 26616c8c9031SAndrey Vagin return; 26626c8c9031SAndrey Vagin 26636c8c9031SAndrey Vagin spin_lock(&ctx->flc_lock); 26646c8c9031SAndrey Vagin __show_fd_locks(f, &ctx->flc_flock, &id, filp, files); 26656c8c9031SAndrey Vagin __show_fd_locks(f, &ctx->flc_posix, &id, filp, files); 26666c8c9031SAndrey Vagin __show_fd_locks(f, &ctx->flc_lease, &id, filp, files); 26676c8c9031SAndrey Vagin spin_unlock(&ctx->flc_lock); 26686c8c9031SAndrey Vagin } 26696c8c9031SAndrey Vagin 26707f8ada98SPavel Emelyanov static void *locks_start(struct seq_file *f, loff_t *pos) 2671b03dfdecSJeff Layton __acquires(&blocked_lock_lock) 26721da177e4SLinus Torvalds { 26737012b02aSJeff Layton struct locks_iterator *iter = f->private; 267499dc8292SJerome Marchand 26757012b02aSJeff Layton iter->li_pos = *pos + 1; 26767012b02aSJeff Layton lg_global_lock(&file_lock_lglock); 26777b2296afSJeff Layton spin_lock(&blocked_lock_lock); 26787012b02aSJeff Layton return seq_hlist_start_percpu(&file_lock_list, &iter->li_cpu, *pos); 26791da177e4SLinus Torvalds } 26807f8ada98SPavel Emelyanov 26817f8ada98SPavel Emelyanov static void *locks_next(struct seq_file *f, void *v, loff_t *pos) 26827f8ada98SPavel Emelyanov { 26837012b02aSJeff Layton struct locks_iterator *iter = f->private; 26847012b02aSJeff Layton 26857012b02aSJeff Layton ++iter->li_pos; 26867012b02aSJeff Layton return seq_hlist_next_percpu(v, &file_lock_list, &iter->li_cpu, pos); 26871da177e4SLinus Torvalds } 26887f8ada98SPavel Emelyanov 26897f8ada98SPavel Emelyanov static void locks_stop(struct seq_file *f, void *v) 2690b03dfdecSJeff Layton __releases(&blocked_lock_lock) 26917f8ada98SPavel Emelyanov { 26927b2296afSJeff Layton spin_unlock(&blocked_lock_lock); 26937012b02aSJeff Layton lg_global_unlock(&file_lock_lglock); 26941da177e4SLinus Torvalds } 26951da177e4SLinus Torvalds 2696d8ba7a36SAlexey Dobriyan static const struct seq_operations locks_seq_operations = { 26977f8ada98SPavel Emelyanov .start = locks_start, 26987f8ada98SPavel Emelyanov .next = locks_next, 26997f8ada98SPavel Emelyanov .stop = locks_stop, 27007f8ada98SPavel Emelyanov .show = locks_show, 27017f8ada98SPavel Emelyanov }; 2702d8ba7a36SAlexey Dobriyan 2703d8ba7a36SAlexey Dobriyan static int locks_open(struct inode *inode, struct file *filp) 2704d8ba7a36SAlexey Dobriyan { 27057012b02aSJeff Layton return seq_open_private(filp, &locks_seq_operations, 27067012b02aSJeff Layton sizeof(struct locks_iterator)); 2707d8ba7a36SAlexey Dobriyan } 2708d8ba7a36SAlexey Dobriyan 2709d8ba7a36SAlexey Dobriyan static const struct file_operations proc_locks_operations = { 2710d8ba7a36SAlexey Dobriyan .open = locks_open, 2711d8ba7a36SAlexey Dobriyan .read = seq_read, 2712d8ba7a36SAlexey Dobriyan .llseek = seq_lseek, 271399dc8292SJerome Marchand .release = seq_release_private, 2714d8ba7a36SAlexey Dobriyan }; 2715d8ba7a36SAlexey Dobriyan 2716d8ba7a36SAlexey Dobriyan static int __init proc_locks_init(void) 2717d8ba7a36SAlexey Dobriyan { 2718d8ba7a36SAlexey Dobriyan proc_create("locks", 0, NULL, &proc_locks_operations); 2719d8ba7a36SAlexey Dobriyan return 0; 2720d8ba7a36SAlexey Dobriyan } 272191899226SPaul Gortmaker fs_initcall(proc_locks_init); 27227f8ada98SPavel Emelyanov #endif 27237f8ada98SPavel Emelyanov 27241da177e4SLinus Torvalds static int __init filelock_init(void) 27251da177e4SLinus Torvalds { 27267012b02aSJeff Layton int i; 27277012b02aSJeff Layton 27284a075e39SJeff Layton flctx_cache = kmem_cache_create("file_lock_ctx", 27294a075e39SJeff Layton sizeof(struct file_lock_context), 0, SLAB_PANIC, NULL); 27304a075e39SJeff Layton 27311da177e4SLinus Torvalds filelock_cache = kmem_cache_create("file_lock_cache", 2732ee19cc40SMiklos Szeredi sizeof(struct file_lock), 0, SLAB_PANIC, NULL); 2733ee19cc40SMiklos Szeredi 27347012b02aSJeff Layton lg_lock_init(&file_lock_lglock, "file_lock_lglock"); 27357012b02aSJeff Layton 27367012b02aSJeff Layton for_each_possible_cpu(i) 27377012b02aSJeff Layton INIT_HLIST_HEAD(per_cpu_ptr(&file_lock_list, i)); 27387012b02aSJeff Layton 27391da177e4SLinus Torvalds return 0; 27401da177e4SLinus Torvalds } 27411da177e4SLinus Torvalds 27421da177e4SLinus Torvalds core_initcall(filelock_init); 2743