1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * linux/fs/locks.c 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Provide support for fcntl()'s F_GETLK, F_SETLK, and F_SETLKW calls. 61da177e4SLinus Torvalds * Doug Evans (dje@spiff.uucp), August 07, 1992 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * Deadlock detection added. 91da177e4SLinus Torvalds * FIXME: one thing isn't handled yet: 101da177e4SLinus Torvalds * - mandatory locks (requires lots of changes elsewhere) 111da177e4SLinus Torvalds * Kelly Carmichael (kelly@[142.24.8.65]), September 17, 1994. 121da177e4SLinus Torvalds * 131da177e4SLinus Torvalds * Miscellaneous edits, and a total rewrite of posix_lock_file() code. 141da177e4SLinus Torvalds * Kai Petzke (wpp@marie.physik.tu-berlin.de), 1994 151da177e4SLinus Torvalds * 161da177e4SLinus Torvalds * Converted file_lock_table to a linked list from an array, which eliminates 171da177e4SLinus Torvalds * the limits on how many active file locks are open. 181da177e4SLinus Torvalds * Chad Page (pageone@netcom.com), November 27, 1994 191da177e4SLinus Torvalds * 201da177e4SLinus Torvalds * Removed dependency on file descriptors. dup()'ed file descriptors now 211da177e4SLinus Torvalds * get the same locks as the original file descriptors, and a close() on 221da177e4SLinus Torvalds * any file descriptor removes ALL the locks on the file for the current 231da177e4SLinus Torvalds * process. Since locks still depend on the process id, locks are inherited 241da177e4SLinus Torvalds * after an exec() but not after a fork(). This agrees with POSIX, and both 251da177e4SLinus Torvalds * BSD and SVR4 practice. 261da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), February 14, 1995 271da177e4SLinus Torvalds * 281da177e4SLinus Torvalds * Scrapped free list which is redundant now that we allocate locks 291da177e4SLinus Torvalds * dynamically with kmalloc()/kfree(). 301da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), February 21, 1995 311da177e4SLinus Torvalds * 321da177e4SLinus Torvalds * Implemented two lock personalities - FL_FLOCK and FL_POSIX. 331da177e4SLinus Torvalds * 341da177e4SLinus Torvalds * FL_POSIX locks are created with calls to fcntl() and lockf() through the 351da177e4SLinus Torvalds * fcntl() system call. They have the semantics described above. 361da177e4SLinus Torvalds * 371da177e4SLinus Torvalds * FL_FLOCK locks are created with calls to flock(), through the flock() 381da177e4SLinus Torvalds * system call, which is new. Old C libraries implement flock() via fcntl() 391da177e4SLinus Torvalds * and will continue to use the old, broken implementation. 401da177e4SLinus Torvalds * 411da177e4SLinus Torvalds * FL_FLOCK locks follow the 4.4 BSD flock() semantics. They are associated 421da177e4SLinus Torvalds * with a file pointer (filp). As a result they can be shared by a parent 431da177e4SLinus Torvalds * process and its children after a fork(). They are removed when the last 441da177e4SLinus Torvalds * file descriptor referring to the file pointer is closed (unless explicitly 451da177e4SLinus Torvalds * unlocked). 461da177e4SLinus Torvalds * 471da177e4SLinus Torvalds * FL_FLOCK locks never deadlock, an existing lock is always removed before 481da177e4SLinus Torvalds * upgrading from shared to exclusive (or vice versa). When this happens 491da177e4SLinus Torvalds * any processes blocked by the current lock are woken up and allowed to 501da177e4SLinus Torvalds * run before the new lock is applied. 511da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), June 09, 1995 521da177e4SLinus Torvalds * 531da177e4SLinus Torvalds * Removed some race conditions in flock_lock_file(), marked other possible 541da177e4SLinus Torvalds * races. Just grep for FIXME to see them. 551da177e4SLinus Torvalds * Dmitry Gorodchanin (pgmdsg@ibi.com), February 09, 1996. 561da177e4SLinus Torvalds * 571da177e4SLinus Torvalds * Addressed Dmitry's concerns. Deadlock checking no longer recursive. 581da177e4SLinus Torvalds * Lock allocation changed to GFP_ATOMIC as we can't afford to sleep 591da177e4SLinus Torvalds * once we've checked for blocking and deadlocking. 601da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), April 03, 1996. 611da177e4SLinus Torvalds * 621da177e4SLinus Torvalds * Initial implementation of mandatory locks. SunOS turned out to be 631da177e4SLinus Torvalds * a rotten model, so I implemented the "obvious" semantics. 64a02dcdf6SMauro Carvalho Chehab * See 'Documentation/filesystems/mandatory-locking.rst' for details. 651da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), April 06, 1996. 661da177e4SLinus Torvalds * 671da177e4SLinus Torvalds * Don't allow mandatory locks on mmap()'ed files. Added simple functions to 681da177e4SLinus Torvalds * check if a file has mandatory locks, used by mmap(), open() and creat() to 691da177e4SLinus Torvalds * see if system call should be rejected. Ref. HP-UX/SunOS/Solaris Reference 701da177e4SLinus Torvalds * Manual, Section 2. 711da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), April 09, 1996. 721da177e4SLinus Torvalds * 731da177e4SLinus Torvalds * Tidied up block list handling. Added '/proc/locks' interface. 741da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), April 24, 1996. 751da177e4SLinus Torvalds * 761da177e4SLinus Torvalds * Fixed deadlock condition for pathological code that mixes calls to 771da177e4SLinus Torvalds * flock() and fcntl(). 781da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), April 29, 1996. 791da177e4SLinus Torvalds * 801da177e4SLinus Torvalds * Allow only one type of locking scheme (FL_POSIX or FL_FLOCK) to be in use 811da177e4SLinus Torvalds * for a given file at a time. Changed the CONFIG_LOCK_MANDATORY scheme to 821da177e4SLinus Torvalds * guarantee sensible behaviour in the case where file system modules might 831da177e4SLinus Torvalds * be compiled with different options than the kernel itself. 841da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), May 15, 1996. 851da177e4SLinus Torvalds * 861da177e4SLinus Torvalds * Added a couple of missing wake_up() calls. Thanks to Thomas Meckel 871da177e4SLinus Torvalds * (Thomas.Meckel@mni.fh-giessen.de) for spotting this. 881da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), May 15, 1996. 891da177e4SLinus Torvalds * 901da177e4SLinus Torvalds * Changed FL_POSIX locks to use the block list in the same way as FL_FLOCK 911da177e4SLinus Torvalds * locks. Changed process synchronisation to avoid dereferencing locks that 921da177e4SLinus Torvalds * have already been freed. 931da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), Sep 21, 1996. 941da177e4SLinus Torvalds * 951da177e4SLinus Torvalds * Made the block list a circular list to minimise searching in the list. 961da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), Sep 25, 1996. 971da177e4SLinus Torvalds * 981da177e4SLinus Torvalds * Made mandatory locking a mount option. Default is not to allow mandatory 991da177e4SLinus Torvalds * locking. 1001da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), Oct 04, 1996. 1011da177e4SLinus Torvalds * 1021da177e4SLinus Torvalds * Some adaptations for NFS support. 1031da177e4SLinus Torvalds * Olaf Kirch (okir@monad.swb.de), Dec 1996, 1041da177e4SLinus Torvalds * 1051da177e4SLinus Torvalds * Fixed /proc/locks interface so that we can't overrun the buffer we are handed. 1061da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), May 12, 1997. 1071da177e4SLinus Torvalds * 1081da177e4SLinus Torvalds * Use slab allocator instead of kmalloc/kfree. 1091da177e4SLinus Torvalds * Use generic list implementation from <linux/list.h>. 1101da177e4SLinus Torvalds * Sped up posix_locks_deadlock by only considering blocked locks. 1111da177e4SLinus Torvalds * Matthew Wilcox <willy@debian.org>, March, 2000. 1121da177e4SLinus Torvalds * 1131da177e4SLinus Torvalds * Leases and LOCK_MAND 1141da177e4SLinus Torvalds * Matthew Wilcox <willy@debian.org>, June, 2000. 1151da177e4SLinus Torvalds * Stephen Rothwell <sfr@canb.auug.org.au>, June, 2000. 116fd7732e0SNeilBrown * 117fd7732e0SNeilBrown * Locking conflicts and dependencies: 118fd7732e0SNeilBrown * If multiple threads attempt to lock the same byte (or flock the same file) 119fd7732e0SNeilBrown * only one can be granted the lock, and other must wait their turn. 120fd7732e0SNeilBrown * The first lock has been "applied" or "granted", the others are "waiting" 121fd7732e0SNeilBrown * and are "blocked" by the "applied" lock.. 122fd7732e0SNeilBrown * 123fd7732e0SNeilBrown * Waiting and applied locks are all kept in trees whose properties are: 124fd7732e0SNeilBrown * 125fd7732e0SNeilBrown * - the root of a tree may be an applied or waiting lock. 126fd7732e0SNeilBrown * - every other node in the tree is a waiting lock that 127fd7732e0SNeilBrown * conflicts with every ancestor of that node. 128fd7732e0SNeilBrown * 129fd7732e0SNeilBrown * Every such tree begins life as a waiting singleton which obviously 130fd7732e0SNeilBrown * satisfies the above properties. 131fd7732e0SNeilBrown * 132fd7732e0SNeilBrown * The only ways we modify trees preserve these properties: 133fd7732e0SNeilBrown * 134fd7732e0SNeilBrown * 1. We may add a new leaf node, but only after first verifying that it 135fd7732e0SNeilBrown * conflicts with all of its ancestors. 136fd7732e0SNeilBrown * 2. We may remove the root of a tree, creating a new singleton 137fd7732e0SNeilBrown * tree from the root and N new trees rooted in the immediate 138fd7732e0SNeilBrown * children. 139fd7732e0SNeilBrown * 3. If the root of a tree is not currently an applied lock, we may 140fd7732e0SNeilBrown * apply it (if possible). 141fd7732e0SNeilBrown * 4. We may upgrade the root of the tree (either extend its range, 142fd7732e0SNeilBrown * or upgrade its entire range from read to write). 143fd7732e0SNeilBrown * 144fd7732e0SNeilBrown * When an applied lock is modified in a way that reduces or downgrades any 145fd7732e0SNeilBrown * part of its range, we remove all its children (2 above). This particularly 146fd7732e0SNeilBrown * happens when a lock is unlocked. 147fd7732e0SNeilBrown * 148fd7732e0SNeilBrown * For each of those child trees we "wake up" the thread which is 149fd7732e0SNeilBrown * waiting for the lock so it can continue handling as follows: if the 150fd7732e0SNeilBrown * root of the tree applies, we do so (3). If it doesn't, it must 151fd7732e0SNeilBrown * conflict with some applied lock. We remove (wake up) all of its children 152fd7732e0SNeilBrown * (2), and add it is a new leaf to the tree rooted in the applied 153fd7732e0SNeilBrown * lock (1). We then repeat the process recursively with those 154fd7732e0SNeilBrown * children. 155fd7732e0SNeilBrown * 1561da177e4SLinus Torvalds */ 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds #include <linux/capability.h> 1591da177e4SLinus Torvalds #include <linux/file.h> 1609f3acc31SAl Viro #include <linux/fdtable.h> 1611da177e4SLinus Torvalds #include <linux/fs.h> 1621da177e4SLinus Torvalds #include <linux/init.h> 1631da177e4SLinus Torvalds #include <linux/security.h> 1641da177e4SLinus Torvalds #include <linux/slab.h> 1651da177e4SLinus Torvalds #include <linux/syscalls.h> 1661da177e4SLinus Torvalds #include <linux/time.h> 1674fb3a538SDipankar Sarma #include <linux/rcupdate.h> 168ab1f1611SVitaliy Gusev #include <linux/pid_namespace.h> 16948f74186SJeff Layton #include <linux/hashtable.h> 1707012b02aSJeff Layton #include <linux/percpu.h> 1711da177e4SLinus Torvalds 17262af4f1fSJeff Layton #define CREATE_TRACE_POINTS 17362af4f1fSJeff Layton #include <trace/events/filelock.h> 17462af4f1fSJeff Layton 1757c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 1761da177e4SLinus Torvalds 1771da177e4SLinus Torvalds #define IS_POSIX(fl) (fl->fl_flags & FL_POSIX) 1781da177e4SLinus Torvalds #define IS_FLOCK(fl) (fl->fl_flags & FL_FLOCK) 17911afe9f7SChristoph Hellwig #define IS_LEASE(fl) (fl->fl_flags & (FL_LEASE|FL_DELEG|FL_LAYOUT)) 180cff2fce5SJeff Layton #define IS_OFDLCK(fl) (fl->fl_flags & FL_OFDLCK) 1819d5b86acSBenjamin Coddington #define IS_REMOTELCK(fl) (fl->fl_pid <= 0) 1821da177e4SLinus Torvalds 183ab83fa4bSJ. Bruce Fields static bool lease_breaking(struct file_lock *fl) 184ab83fa4bSJ. Bruce Fields { 185778fc546SJ. Bruce Fields return fl->fl_flags & (FL_UNLOCK_PENDING | FL_DOWNGRADE_PENDING); 186778fc546SJ. Bruce Fields } 187778fc546SJ. Bruce Fields 188778fc546SJ. Bruce Fields static int target_leasetype(struct file_lock *fl) 189778fc546SJ. Bruce Fields { 190778fc546SJ. Bruce Fields if (fl->fl_flags & FL_UNLOCK_PENDING) 191778fc546SJ. Bruce Fields return F_UNLCK; 192778fc546SJ. Bruce Fields if (fl->fl_flags & FL_DOWNGRADE_PENDING) 193778fc546SJ. Bruce Fields return F_RDLCK; 194778fc546SJ. Bruce Fields return fl->fl_type; 195ab83fa4bSJ. Bruce Fields } 196ab83fa4bSJ. Bruce Fields 1971da177e4SLinus Torvalds int leases_enable = 1; 1981da177e4SLinus Torvalds int lease_break_time = 45; 1991da177e4SLinus Torvalds 2001c8c601aSJeff Layton /* 2017012b02aSJeff Layton * The global file_lock_list is only used for displaying /proc/locks, so we 2027c3f654dSPeter Zijlstra * keep a list on each CPU, with each list protected by its own spinlock. 2037c3f654dSPeter Zijlstra * Global serialization is done using file_rwsem. 2047c3f654dSPeter Zijlstra * 2057c3f654dSPeter Zijlstra * Note that alterations to the list also require that the relevant flc_lock is 2067c3f654dSPeter Zijlstra * held. 2071c8c601aSJeff Layton */ 2087c3f654dSPeter Zijlstra struct file_lock_list_struct { 2097c3f654dSPeter Zijlstra spinlock_t lock; 2107c3f654dSPeter Zijlstra struct hlist_head hlist; 2117c3f654dSPeter Zijlstra }; 2127c3f654dSPeter Zijlstra static DEFINE_PER_CPU(struct file_lock_list_struct, file_lock_list); 213aba37660SPeter Zijlstra DEFINE_STATIC_PERCPU_RWSEM(file_rwsem); 21488974691SJeff Layton 215eb82dd39SJeff Layton 2161c8c601aSJeff Layton /* 21748f74186SJeff Layton * The blocked_hash is used to find POSIX lock loops for deadlock detection. 2187b2296afSJeff Layton * It is protected by blocked_lock_lock. 21948f74186SJeff Layton * 22048f74186SJeff Layton * We hash locks by lockowner in order to optimize searching for the lock a 22148f74186SJeff Layton * particular lockowner is waiting on. 22248f74186SJeff Layton * 22348f74186SJeff Layton * FIXME: make this value scale via some heuristic? We generally will want more 22448f74186SJeff Layton * buckets when we have more lockowners holding locks, but that's a little 22548f74186SJeff Layton * difficult to determine without knowing what the workload will look like. 2261c8c601aSJeff Layton */ 22748f74186SJeff Layton #define BLOCKED_HASH_BITS 7 22848f74186SJeff Layton static DEFINE_HASHTABLE(blocked_hash, BLOCKED_HASH_BITS); 22988974691SJeff Layton 2301c8c601aSJeff Layton /* 2317b2296afSJeff Layton * This lock protects the blocked_hash. Generally, if you're accessing it, you 2327b2296afSJeff Layton * want to be holding this lock. 2331c8c601aSJeff Layton * 234ada5c1daSNeilBrown * In addition, it also protects the fl->fl_blocked_requests list, and the 235ada5c1daSNeilBrown * fl->fl_blocker pointer for file_lock structures that are acting as lock 236ada5c1daSNeilBrown * requests (in contrast to those that are acting as records of acquired locks). 2371c8c601aSJeff Layton * 2381c8c601aSJeff Layton * Note that when we acquire this lock in order to change the above fields, 2396109c850SJeff Layton * we often hold the flc_lock as well. In certain cases, when reading the fields 2401c8c601aSJeff Layton * protected by this lock, we can skip acquiring it iff we already hold the 2416109c850SJeff Layton * flc_lock. 2421c8c601aSJeff Layton */ 2437b2296afSJeff Layton static DEFINE_SPINLOCK(blocked_lock_lock); 2441da177e4SLinus Torvalds 2454a075e39SJeff Layton static struct kmem_cache *flctx_cache __read_mostly; 246e18b890bSChristoph Lameter static struct kmem_cache *filelock_cache __read_mostly; 2471da177e4SLinus Torvalds 2484a075e39SJeff Layton static struct file_lock_context * 2495c1c669aSJeff Layton locks_get_lock_context(struct inode *inode, int type) 2504a075e39SJeff Layton { 251128a3785SDmitry Vyukov struct file_lock_context *ctx; 2524a075e39SJeff Layton 253128a3785SDmitry Vyukov /* paired with cmpxchg() below */ 254128a3785SDmitry Vyukov ctx = smp_load_acquire(&inode->i_flctx); 255128a3785SDmitry Vyukov if (likely(ctx) || type == F_UNLCK) 2564a075e39SJeff Layton goto out; 2574a075e39SJeff Layton 258128a3785SDmitry Vyukov ctx = kmem_cache_alloc(flctx_cache, GFP_KERNEL); 259128a3785SDmitry Vyukov if (!ctx) 2604a075e39SJeff Layton goto out; 2614a075e39SJeff Layton 262128a3785SDmitry Vyukov spin_lock_init(&ctx->flc_lock); 263128a3785SDmitry Vyukov INIT_LIST_HEAD(&ctx->flc_flock); 264128a3785SDmitry Vyukov INIT_LIST_HEAD(&ctx->flc_posix); 265128a3785SDmitry Vyukov INIT_LIST_HEAD(&ctx->flc_lease); 2664a075e39SJeff Layton 2674a075e39SJeff Layton /* 2684a075e39SJeff Layton * Assign the pointer if it's not already assigned. If it is, then 2694a075e39SJeff Layton * free the context we just allocated. 2704a075e39SJeff Layton */ 271128a3785SDmitry Vyukov if (cmpxchg(&inode->i_flctx, NULL, ctx)) { 272128a3785SDmitry Vyukov kmem_cache_free(flctx_cache, ctx); 273128a3785SDmitry Vyukov ctx = smp_load_acquire(&inode->i_flctx); 274128a3785SDmitry Vyukov } 2754a075e39SJeff Layton out: 2761890910fSJeff Layton trace_locks_get_lock_context(inode, type, ctx); 277128a3785SDmitry Vyukov return ctx; 2784a075e39SJeff Layton } 2794a075e39SJeff Layton 280e24dadabSJeff Layton static void 281e24dadabSJeff Layton locks_dump_ctx_list(struct list_head *list, char *list_type) 282e24dadabSJeff Layton { 283e24dadabSJeff Layton struct file_lock *fl; 284e24dadabSJeff Layton 285e24dadabSJeff Layton list_for_each_entry(fl, list, fl_list) { 286e24dadabSJeff Layton pr_warn("%s: fl_owner=%p fl_flags=0x%x fl_type=0x%x fl_pid=%u\n", list_type, fl->fl_owner, fl->fl_flags, fl->fl_type, fl->fl_pid); 287e24dadabSJeff Layton } 288e24dadabSJeff Layton } 289e24dadabSJeff Layton 290e24dadabSJeff Layton static void 291e24dadabSJeff Layton locks_check_ctx_lists(struct inode *inode) 292e24dadabSJeff Layton { 293e24dadabSJeff Layton struct file_lock_context *ctx = inode->i_flctx; 294e24dadabSJeff Layton 295e24dadabSJeff Layton if (unlikely(!list_empty(&ctx->flc_flock) || 296e24dadabSJeff Layton !list_empty(&ctx->flc_posix) || 297e24dadabSJeff Layton !list_empty(&ctx->flc_lease))) { 298e24dadabSJeff Layton pr_warn("Leaked locks on dev=0x%x:0x%x ino=0x%lx:\n", 299e24dadabSJeff Layton MAJOR(inode->i_sb->s_dev), MINOR(inode->i_sb->s_dev), 300e24dadabSJeff Layton inode->i_ino); 301e24dadabSJeff Layton locks_dump_ctx_list(&ctx->flc_flock, "FLOCK"); 302e24dadabSJeff Layton locks_dump_ctx_list(&ctx->flc_posix, "POSIX"); 303e24dadabSJeff Layton locks_dump_ctx_list(&ctx->flc_lease, "LEASE"); 304e24dadabSJeff Layton } 305e24dadabSJeff Layton } 306e24dadabSJeff Layton 3073953704fSBenjamin Coddington static void 3083953704fSBenjamin Coddington locks_check_ctx_file_list(struct file *filp, struct list_head *list, 3093953704fSBenjamin Coddington char *list_type) 3103953704fSBenjamin Coddington { 3113953704fSBenjamin Coddington struct file_lock *fl; 3123953704fSBenjamin Coddington struct inode *inode = locks_inode(filp); 3133953704fSBenjamin Coddington 3143953704fSBenjamin Coddington list_for_each_entry(fl, list, fl_list) 3153953704fSBenjamin Coddington if (fl->fl_file == filp) 3163953704fSBenjamin Coddington pr_warn("Leaked %s lock on dev=0x%x:0x%x ino=0x%lx " 3173953704fSBenjamin Coddington " fl_owner=%p fl_flags=0x%x fl_type=0x%x fl_pid=%u\n", 3183953704fSBenjamin Coddington list_type, MAJOR(inode->i_sb->s_dev), 3193953704fSBenjamin Coddington MINOR(inode->i_sb->s_dev), inode->i_ino, 3203953704fSBenjamin Coddington fl->fl_owner, fl->fl_flags, fl->fl_type, fl->fl_pid); 3213953704fSBenjamin Coddington } 3223953704fSBenjamin Coddington 3234a075e39SJeff Layton void 324f27a0fe0SJeff Layton locks_free_lock_context(struct inode *inode) 3254a075e39SJeff Layton { 326f27a0fe0SJeff Layton struct file_lock_context *ctx = inode->i_flctx; 327f27a0fe0SJeff Layton 328e24dadabSJeff Layton if (unlikely(ctx)) { 329e24dadabSJeff Layton locks_check_ctx_lists(inode); 3304a075e39SJeff Layton kmem_cache_free(flctx_cache, ctx); 3314a075e39SJeff Layton } 3324a075e39SJeff Layton } 3334a075e39SJeff Layton 334ee19cc40SMiklos Szeredi static void locks_init_lock_heads(struct file_lock *fl) 335a51cb91dSMiklos Szeredi { 336139ca04eSJeff Layton INIT_HLIST_NODE(&fl->fl_link); 3376dee60f6SJeff Layton INIT_LIST_HEAD(&fl->fl_list); 338ada5c1daSNeilBrown INIT_LIST_HEAD(&fl->fl_blocked_requests); 339ada5c1daSNeilBrown INIT_LIST_HEAD(&fl->fl_blocked_member); 340ee19cc40SMiklos Szeredi init_waitqueue_head(&fl->fl_wait); 341a51cb91dSMiklos Szeredi } 342a51cb91dSMiklos Szeredi 3431da177e4SLinus Torvalds /* Allocate an empty lock structure. */ 344c5b1f0d9SArnd Bergmann struct file_lock *locks_alloc_lock(void) 3451da177e4SLinus Torvalds { 346ee19cc40SMiklos Szeredi struct file_lock *fl = kmem_cache_zalloc(filelock_cache, GFP_KERNEL); 347a51cb91dSMiklos Szeredi 348a51cb91dSMiklos Szeredi if (fl) 349ee19cc40SMiklos Szeredi locks_init_lock_heads(fl); 350a51cb91dSMiklos Szeredi 351a51cb91dSMiklos Szeredi return fl; 3521da177e4SLinus Torvalds } 353c5b1f0d9SArnd Bergmann EXPORT_SYMBOL_GPL(locks_alloc_lock); 3541da177e4SLinus Torvalds 355a9e61e25SFelix Blyakher void locks_release_private(struct file_lock *fl) 35647831f35STrond Myklebust { 3575926459eSNeilBrown BUG_ON(waitqueue_active(&fl->fl_wait)); 3585926459eSNeilBrown BUG_ON(!list_empty(&fl->fl_list)); 3595926459eSNeilBrown BUG_ON(!list_empty(&fl->fl_blocked_requests)); 3605926459eSNeilBrown BUG_ON(!list_empty(&fl->fl_blocked_member)); 3615926459eSNeilBrown BUG_ON(!hlist_unhashed(&fl->fl_link)); 3625926459eSNeilBrown 36347831f35STrond Myklebust if (fl->fl_ops) { 36447831f35STrond Myklebust if (fl->fl_ops->fl_release_private) 36547831f35STrond Myklebust fl->fl_ops->fl_release_private(fl); 36647831f35STrond Myklebust fl->fl_ops = NULL; 36747831f35STrond Myklebust } 36847831f35STrond Myklebust 3695c97d7b1SKinglong Mee if (fl->fl_lmops) { 370cae80b30SJeff Layton if (fl->fl_lmops->lm_put_owner) { 371cae80b30SJeff Layton fl->fl_lmops->lm_put_owner(fl->fl_owner); 372cae80b30SJeff Layton fl->fl_owner = NULL; 373cae80b30SJeff Layton } 3745c97d7b1SKinglong Mee fl->fl_lmops = NULL; 3755c97d7b1SKinglong Mee } 37647831f35STrond Myklebust } 377a9e61e25SFelix Blyakher EXPORT_SYMBOL_GPL(locks_release_private); 37847831f35STrond Myklebust 3791da177e4SLinus Torvalds /* Free a lock which is not in use. */ 38005fa3135SJ. Bruce Fields void locks_free_lock(struct file_lock *fl) 3811da177e4SLinus Torvalds { 38247831f35STrond Myklebust locks_release_private(fl); 3831da177e4SLinus Torvalds kmem_cache_free(filelock_cache, fl); 3841da177e4SLinus Torvalds } 38505fa3135SJ. Bruce Fields EXPORT_SYMBOL(locks_free_lock); 3861da177e4SLinus Torvalds 387ed9814d8SJeff Layton static void 388ed9814d8SJeff Layton locks_dispose_list(struct list_head *dispose) 389ed9814d8SJeff Layton { 390ed9814d8SJeff Layton struct file_lock *fl; 391ed9814d8SJeff Layton 392ed9814d8SJeff Layton while (!list_empty(dispose)) { 3936dee60f6SJeff Layton fl = list_first_entry(dispose, struct file_lock, fl_list); 3946dee60f6SJeff Layton list_del_init(&fl->fl_list); 395ed9814d8SJeff Layton locks_free_lock(fl); 396ed9814d8SJeff Layton } 397ed9814d8SJeff Layton } 398ed9814d8SJeff Layton 3991da177e4SLinus Torvalds void locks_init_lock(struct file_lock *fl) 4001da177e4SLinus Torvalds { 401ee19cc40SMiklos Szeredi memset(fl, 0, sizeof(struct file_lock)); 402ee19cc40SMiklos Szeredi locks_init_lock_heads(fl); 4031da177e4SLinus Torvalds } 4041da177e4SLinus Torvalds EXPORT_SYMBOL(locks_init_lock); 4051da177e4SLinus Torvalds 4061da177e4SLinus Torvalds /* 4071da177e4SLinus Torvalds * Initialize a new lock from an existing file_lock structure. 4081da177e4SLinus Torvalds */ 4093fe0fff1SKinglong Mee void locks_copy_conflock(struct file_lock *new, struct file_lock *fl) 4101da177e4SLinus Torvalds { 4111da177e4SLinus Torvalds new->fl_owner = fl->fl_owner; 4121da177e4SLinus Torvalds new->fl_pid = fl->fl_pid; 4130996905fSTrond Myklebust new->fl_file = NULL; 4141da177e4SLinus Torvalds new->fl_flags = fl->fl_flags; 4151da177e4SLinus Torvalds new->fl_type = fl->fl_type; 4161da177e4SLinus Torvalds new->fl_start = fl->fl_start; 4171da177e4SLinus Torvalds new->fl_end = fl->fl_end; 418f328296eSKinglong Mee new->fl_lmops = fl->fl_lmops; 4190996905fSTrond Myklebust new->fl_ops = NULL; 420f328296eSKinglong Mee 421f328296eSKinglong Mee if (fl->fl_lmops) { 422f328296eSKinglong Mee if (fl->fl_lmops->lm_get_owner) 423cae80b30SJeff Layton fl->fl_lmops->lm_get_owner(fl->fl_owner); 424f328296eSKinglong Mee } 4250996905fSTrond Myklebust } 4263fe0fff1SKinglong Mee EXPORT_SYMBOL(locks_copy_conflock); 4270996905fSTrond Myklebust 4280996905fSTrond Myklebust void locks_copy_lock(struct file_lock *new, struct file_lock *fl) 4290996905fSTrond Myklebust { 430566709bdSJeff Layton /* "new" must be a freshly-initialized lock */ 431566709bdSJeff Layton WARN_ON_ONCE(new->fl_ops); 4320996905fSTrond Myklebust 4333fe0fff1SKinglong Mee locks_copy_conflock(new, fl); 434f328296eSKinglong Mee 4350996905fSTrond Myklebust new->fl_file = fl->fl_file; 4361da177e4SLinus Torvalds new->fl_ops = fl->fl_ops; 43747831f35STrond Myklebust 438f328296eSKinglong Mee if (fl->fl_ops) { 439f328296eSKinglong Mee if (fl->fl_ops->fl_copy_lock) 440f328296eSKinglong Mee fl->fl_ops->fl_copy_lock(new, fl); 441f328296eSKinglong Mee } 4421da177e4SLinus Torvalds } 4431da177e4SLinus Torvalds EXPORT_SYMBOL(locks_copy_lock); 4441da177e4SLinus Torvalds 4455946c431SNeilBrown static void locks_move_blocks(struct file_lock *new, struct file_lock *fl) 4465946c431SNeilBrown { 4475946c431SNeilBrown struct file_lock *f; 4485946c431SNeilBrown 4495946c431SNeilBrown /* 4505946c431SNeilBrown * As ctx->flc_lock is held, new requests cannot be added to 4515946c431SNeilBrown * ->fl_blocked_requests, so we don't need a lock to check if it 4525946c431SNeilBrown * is empty. 4535946c431SNeilBrown */ 4545946c431SNeilBrown if (list_empty(&fl->fl_blocked_requests)) 4555946c431SNeilBrown return; 4565946c431SNeilBrown spin_lock(&blocked_lock_lock); 4575946c431SNeilBrown list_splice_init(&fl->fl_blocked_requests, &new->fl_blocked_requests); 458bf77ae4cSNeilBrown list_for_each_entry(f, &new->fl_blocked_requests, fl_blocked_member) 4595946c431SNeilBrown f->fl_blocker = new; 4605946c431SNeilBrown spin_unlock(&blocked_lock_lock); 4615946c431SNeilBrown } 4625946c431SNeilBrown 4631da177e4SLinus Torvalds static inline int flock_translate_cmd(int cmd) { 4641da177e4SLinus Torvalds if (cmd & LOCK_MAND) 4651da177e4SLinus Torvalds return cmd & (LOCK_MAND | LOCK_RW); 4661da177e4SLinus Torvalds switch (cmd) { 4671da177e4SLinus Torvalds case LOCK_SH: 4681da177e4SLinus Torvalds return F_RDLCK; 4691da177e4SLinus Torvalds case LOCK_EX: 4701da177e4SLinus Torvalds return F_WRLCK; 4711da177e4SLinus Torvalds case LOCK_UN: 4721da177e4SLinus Torvalds return F_UNLCK; 4731da177e4SLinus Torvalds } 4741da177e4SLinus Torvalds return -EINVAL; 4751da177e4SLinus Torvalds } 4761da177e4SLinus Torvalds 4771da177e4SLinus Torvalds /* Fill in a file_lock structure with an appropriate FLOCK lock. */ 4786e129d00SJeff Layton static struct file_lock * 479d6367d62SNeilBrown flock_make_lock(struct file *filp, unsigned int cmd, struct file_lock *fl) 4801da177e4SLinus Torvalds { 4811da177e4SLinus Torvalds int type = flock_translate_cmd(cmd); 4826e129d00SJeff Layton 4831da177e4SLinus Torvalds if (type < 0) 4846e129d00SJeff Layton return ERR_PTR(type); 4851da177e4SLinus Torvalds 486d6367d62SNeilBrown if (fl == NULL) { 4871da177e4SLinus Torvalds fl = locks_alloc_lock(); 4881da177e4SLinus Torvalds if (fl == NULL) 4896e129d00SJeff Layton return ERR_PTR(-ENOMEM); 490d6367d62SNeilBrown } else { 491d6367d62SNeilBrown locks_init_lock(fl); 492d6367d62SNeilBrown } 4931da177e4SLinus Torvalds 4941da177e4SLinus Torvalds fl->fl_file = filp; 49573a8f5f7SChristoph Hellwig fl->fl_owner = filp; 4961da177e4SLinus Torvalds fl->fl_pid = current->tgid; 4971da177e4SLinus Torvalds fl->fl_flags = FL_FLOCK; 4981da177e4SLinus Torvalds fl->fl_type = type; 4991da177e4SLinus Torvalds fl->fl_end = OFFSET_MAX; 5001da177e4SLinus Torvalds 5016e129d00SJeff Layton return fl; 5021da177e4SLinus Torvalds } 5031da177e4SLinus Torvalds 5040ec4f431SJ. Bruce Fields static int assign_type(struct file_lock *fl, long type) 5051da177e4SLinus Torvalds { 5061da177e4SLinus Torvalds switch (type) { 5071da177e4SLinus Torvalds case F_RDLCK: 5081da177e4SLinus Torvalds case F_WRLCK: 5091da177e4SLinus Torvalds case F_UNLCK: 5101da177e4SLinus Torvalds fl->fl_type = type; 5111da177e4SLinus Torvalds break; 5121da177e4SLinus Torvalds default: 5131da177e4SLinus Torvalds return -EINVAL; 5141da177e4SLinus Torvalds } 5151da177e4SLinus Torvalds return 0; 5161da177e4SLinus Torvalds } 5171da177e4SLinus Torvalds 518ef12e72aSJ. Bruce Fields static int flock64_to_posix_lock(struct file *filp, struct file_lock *fl, 519ef12e72aSJ. Bruce Fields struct flock64 *l) 520ef12e72aSJ. Bruce Fields { 521ef12e72aSJ. Bruce Fields switch (l->l_whence) { 522ef12e72aSJ. Bruce Fields case SEEK_SET: 523ef12e72aSJ. Bruce Fields fl->fl_start = 0; 524ef12e72aSJ. Bruce Fields break; 525ef12e72aSJ. Bruce Fields case SEEK_CUR: 526ef12e72aSJ. Bruce Fields fl->fl_start = filp->f_pos; 527ef12e72aSJ. Bruce Fields break; 528ef12e72aSJ. Bruce Fields case SEEK_END: 529ef12e72aSJ. Bruce Fields fl->fl_start = i_size_read(file_inode(filp)); 530ef12e72aSJ. Bruce Fields break; 531ef12e72aSJ. Bruce Fields default: 532ef12e72aSJ. Bruce Fields return -EINVAL; 533ef12e72aSJ. Bruce Fields } 534ef12e72aSJ. Bruce Fields if (l->l_start > OFFSET_MAX - fl->fl_start) 535ef12e72aSJ. Bruce Fields return -EOVERFLOW; 536ef12e72aSJ. Bruce Fields fl->fl_start += l->l_start; 537ef12e72aSJ. Bruce Fields if (fl->fl_start < 0) 538ef12e72aSJ. Bruce Fields return -EINVAL; 539ef12e72aSJ. Bruce Fields 540ef12e72aSJ. Bruce Fields /* POSIX-1996 leaves the case l->l_len < 0 undefined; 541ef12e72aSJ. Bruce Fields POSIX-2001 defines it. */ 542ef12e72aSJ. Bruce Fields if (l->l_len > 0) { 543ef12e72aSJ. Bruce Fields if (l->l_len - 1 > OFFSET_MAX - fl->fl_start) 544ef12e72aSJ. Bruce Fields return -EOVERFLOW; 545*16238415SLuo Meng fl->fl_end = fl->fl_start + (l->l_len - 1); 546ef12e72aSJ. Bruce Fields 547ef12e72aSJ. Bruce Fields } else if (l->l_len < 0) { 548ef12e72aSJ. Bruce Fields if (fl->fl_start + l->l_len < 0) 549ef12e72aSJ. Bruce Fields return -EINVAL; 550ef12e72aSJ. Bruce Fields fl->fl_end = fl->fl_start - 1; 551ef12e72aSJ. Bruce Fields fl->fl_start += l->l_len; 552ef12e72aSJ. Bruce Fields } else 553ef12e72aSJ. Bruce Fields fl->fl_end = OFFSET_MAX; 554ef12e72aSJ. Bruce Fields 555ef12e72aSJ. Bruce Fields fl->fl_owner = current->files; 556ef12e72aSJ. Bruce Fields fl->fl_pid = current->tgid; 557ef12e72aSJ. Bruce Fields fl->fl_file = filp; 558ef12e72aSJ. Bruce Fields fl->fl_flags = FL_POSIX; 559ef12e72aSJ. Bruce Fields fl->fl_ops = NULL; 560ef12e72aSJ. Bruce Fields fl->fl_lmops = NULL; 561ef12e72aSJ. Bruce Fields 562ef12e72aSJ. Bruce Fields return assign_type(fl, l->l_type); 563ef12e72aSJ. Bruce Fields } 564ef12e72aSJ. Bruce Fields 5651da177e4SLinus Torvalds /* Verify a "struct flock" and copy it to a "struct file_lock" as a POSIX 5661da177e4SLinus Torvalds * style lock. 5671da177e4SLinus Torvalds */ 5681da177e4SLinus Torvalds static int flock_to_posix_lock(struct file *filp, struct file_lock *fl, 5691da177e4SLinus Torvalds struct flock *l) 5701da177e4SLinus Torvalds { 571ef12e72aSJ. Bruce Fields struct flock64 ll = { 572ef12e72aSJ. Bruce Fields .l_type = l->l_type, 573ef12e72aSJ. Bruce Fields .l_whence = l->l_whence, 574ef12e72aSJ. Bruce Fields .l_start = l->l_start, 575ef12e72aSJ. Bruce Fields .l_len = l->l_len, 576ef12e72aSJ. Bruce Fields }; 5771da177e4SLinus Torvalds 578ef12e72aSJ. Bruce Fields return flock64_to_posix_lock(filp, fl, &ll); 5791da177e4SLinus Torvalds } 5801da177e4SLinus Torvalds 5811da177e4SLinus Torvalds /* default lease lock manager operations */ 5824d01b7f5SJeff Layton static bool 5834d01b7f5SJeff Layton lease_break_callback(struct file_lock *fl) 5841da177e4SLinus Torvalds { 5851da177e4SLinus Torvalds kill_fasync(&fl->fl_fasync, SIGIO, POLL_MSG); 5864d01b7f5SJeff Layton return false; 5871da177e4SLinus Torvalds } 5881da177e4SLinus Torvalds 5891c7dd2ffSJeff Layton static void 5901c7dd2ffSJeff Layton lease_setup(struct file_lock *fl, void **priv) 5911c7dd2ffSJeff Layton { 5921c7dd2ffSJeff Layton struct file *filp = fl->fl_file; 5931c7dd2ffSJeff Layton struct fasync_struct *fa = *priv; 5941c7dd2ffSJeff Layton 5951c7dd2ffSJeff Layton /* 5961c7dd2ffSJeff Layton * fasync_insert_entry() returns the old entry if any. If there was no 5971c7dd2ffSJeff Layton * old entry, then it used "priv" and inserted it into the fasync list. 5981c7dd2ffSJeff Layton * Clear the pointer to indicate that it shouldn't be freed. 5991c7dd2ffSJeff Layton */ 6001c7dd2ffSJeff Layton if (!fasync_insert_entry(fa->fa_fd, filp, &fl->fl_fasync, fa)) 6011c7dd2ffSJeff Layton *priv = NULL; 6021c7dd2ffSJeff Layton 60301919134SEric W. Biederman __f_setown(filp, task_pid(current), PIDTYPE_TGID, 0); 6041c7dd2ffSJeff Layton } 6051c7dd2ffSJeff Layton 6067b021967SAlexey Dobriyan static const struct lock_manager_operations lease_manager_ops = { 6078fb47a4fSJ. Bruce Fields .lm_break = lease_break_callback, 6088fb47a4fSJ. Bruce Fields .lm_change = lease_modify, 6091c7dd2ffSJeff Layton .lm_setup = lease_setup, 6101da177e4SLinus Torvalds }; 6111da177e4SLinus Torvalds 6121da177e4SLinus Torvalds /* 6131da177e4SLinus Torvalds * Initialize a lease, use the default lock manager operations 6141da177e4SLinus Torvalds */ 6150ec4f431SJ. Bruce Fields static int lease_init(struct file *filp, long type, struct file_lock *fl) 6161da177e4SLinus Torvalds { 61775dff55aSTrond Myklebust if (assign_type(fl, type) != 0) 61875dff55aSTrond Myklebust return -EINVAL; 61975dff55aSTrond Myklebust 6207ca76311SJeff Layton fl->fl_owner = filp; 6211da177e4SLinus Torvalds fl->fl_pid = current->tgid; 6221da177e4SLinus Torvalds 6231da177e4SLinus Torvalds fl->fl_file = filp; 6241da177e4SLinus Torvalds fl->fl_flags = FL_LEASE; 6251da177e4SLinus Torvalds fl->fl_start = 0; 6261da177e4SLinus Torvalds fl->fl_end = OFFSET_MAX; 6271da177e4SLinus Torvalds fl->fl_ops = NULL; 6281da177e4SLinus Torvalds fl->fl_lmops = &lease_manager_ops; 6291da177e4SLinus Torvalds return 0; 6301da177e4SLinus Torvalds } 6311da177e4SLinus Torvalds 6321da177e4SLinus Torvalds /* Allocate a file_lock initialised to this type of lease */ 6330ec4f431SJ. Bruce Fields static struct file_lock *lease_alloc(struct file *filp, long type) 6341da177e4SLinus Torvalds { 6351da177e4SLinus Torvalds struct file_lock *fl = locks_alloc_lock(); 63675dff55aSTrond Myklebust int error = -ENOMEM; 6371da177e4SLinus Torvalds 6381da177e4SLinus Torvalds if (fl == NULL) 639e32b8ee2SJ. Bruce Fields return ERR_PTR(error); 6401da177e4SLinus Torvalds 6411da177e4SLinus Torvalds error = lease_init(filp, type, fl); 64275dff55aSTrond Myklebust if (error) { 64375dff55aSTrond Myklebust locks_free_lock(fl); 644e32b8ee2SJ. Bruce Fields return ERR_PTR(error); 64575dff55aSTrond Myklebust } 646e32b8ee2SJ. Bruce Fields return fl; 6471da177e4SLinus Torvalds } 6481da177e4SLinus Torvalds 6491da177e4SLinus Torvalds /* Check if two locks overlap each other. 6501da177e4SLinus Torvalds */ 6511da177e4SLinus Torvalds static inline int locks_overlap(struct file_lock *fl1, struct file_lock *fl2) 6521da177e4SLinus Torvalds { 6531da177e4SLinus Torvalds return ((fl1->fl_end >= fl2->fl_start) && 6541da177e4SLinus Torvalds (fl2->fl_end >= fl1->fl_start)); 6551da177e4SLinus Torvalds } 6561da177e4SLinus Torvalds 6571da177e4SLinus Torvalds /* 6581da177e4SLinus Torvalds * Check whether two locks have the same owner. 6591da177e4SLinus Torvalds */ 66033443c42SMatt Mackall static int posix_same_owner(struct file_lock *fl1, struct file_lock *fl2) 6611da177e4SLinus Torvalds { 6621da177e4SLinus Torvalds return fl1->fl_owner == fl2->fl_owner; 6631da177e4SLinus Torvalds } 6641da177e4SLinus Torvalds 6656109c850SJeff Layton /* Must be called with the flc_lock held! */ 6666ca10ed8SJeff Layton static void locks_insert_global_locks(struct file_lock *fl) 66788974691SJeff Layton { 6687c3f654dSPeter Zijlstra struct file_lock_list_struct *fll = this_cpu_ptr(&file_lock_list); 6697c3f654dSPeter Zijlstra 670aba37660SPeter Zijlstra percpu_rwsem_assert_held(&file_rwsem); 671aba37660SPeter Zijlstra 6727c3f654dSPeter Zijlstra spin_lock(&fll->lock); 6737012b02aSJeff Layton fl->fl_link_cpu = smp_processor_id(); 6747c3f654dSPeter Zijlstra hlist_add_head(&fl->fl_link, &fll->hlist); 6757c3f654dSPeter Zijlstra spin_unlock(&fll->lock); 67688974691SJeff Layton } 67788974691SJeff Layton 6786109c850SJeff Layton /* Must be called with the flc_lock held! */ 6796ca10ed8SJeff Layton static void locks_delete_global_locks(struct file_lock *fl) 68088974691SJeff Layton { 6817c3f654dSPeter Zijlstra struct file_lock_list_struct *fll; 6827c3f654dSPeter Zijlstra 683aba37660SPeter Zijlstra percpu_rwsem_assert_held(&file_rwsem); 684aba37660SPeter Zijlstra 6857012b02aSJeff Layton /* 6867012b02aSJeff Layton * Avoid taking lock if already unhashed. This is safe since this check 6876109c850SJeff Layton * is done while holding the flc_lock, and new insertions into the list 6887012b02aSJeff Layton * also require that it be held. 6897012b02aSJeff Layton */ 6907012b02aSJeff Layton if (hlist_unhashed(&fl->fl_link)) 6917012b02aSJeff Layton return; 6927c3f654dSPeter Zijlstra 6937c3f654dSPeter Zijlstra fll = per_cpu_ptr(&file_lock_list, fl->fl_link_cpu); 6947c3f654dSPeter Zijlstra spin_lock(&fll->lock); 695139ca04eSJeff Layton hlist_del_init(&fl->fl_link); 6967c3f654dSPeter Zijlstra spin_unlock(&fll->lock); 69788974691SJeff Layton } 69888974691SJeff Layton 6993999e493SJeff Layton static unsigned long 7003999e493SJeff Layton posix_owner_key(struct file_lock *fl) 7013999e493SJeff Layton { 7023999e493SJeff Layton return (unsigned long)fl->fl_owner; 7033999e493SJeff Layton } 7043999e493SJeff Layton 7056ca10ed8SJeff Layton static void locks_insert_global_blocked(struct file_lock *waiter) 70688974691SJeff Layton { 707663d5af7SDaniel Wagner lockdep_assert_held(&blocked_lock_lock); 708663d5af7SDaniel Wagner 7093999e493SJeff Layton hash_add(blocked_hash, &waiter->fl_link, posix_owner_key(waiter)); 71088974691SJeff Layton } 71188974691SJeff Layton 7126ca10ed8SJeff Layton static void locks_delete_global_blocked(struct file_lock *waiter) 71388974691SJeff Layton { 714663d5af7SDaniel Wagner lockdep_assert_held(&blocked_lock_lock); 715663d5af7SDaniel Wagner 71648f74186SJeff Layton hash_del(&waiter->fl_link); 71788974691SJeff Layton } 71888974691SJeff Layton 7191da177e4SLinus Torvalds /* Remove waiter from blocker's block list. 7201da177e4SLinus Torvalds * When blocker ends up pointing to itself then the list is empty. 7211c8c601aSJeff Layton * 7227b2296afSJeff Layton * Must be called with blocked_lock_lock held. 7231da177e4SLinus Torvalds */ 72433443c42SMatt Mackall static void __locks_delete_block(struct file_lock *waiter) 7251da177e4SLinus Torvalds { 72688974691SJeff Layton locks_delete_global_blocked(waiter); 727ada5c1daSNeilBrown list_del_init(&waiter->fl_blocked_member); 7281da177e4SLinus Torvalds } 7291da177e4SLinus Torvalds 730ad6bbd8bSNeilBrown static void __locks_wake_up_blocks(struct file_lock *blocker) 731ad6bbd8bSNeilBrown { 732ad6bbd8bSNeilBrown while (!list_empty(&blocker->fl_blocked_requests)) { 733ad6bbd8bSNeilBrown struct file_lock *waiter; 734ad6bbd8bSNeilBrown 735ad6bbd8bSNeilBrown waiter = list_first_entry(&blocker->fl_blocked_requests, 736ad6bbd8bSNeilBrown struct file_lock, fl_blocked_member); 737ad6bbd8bSNeilBrown __locks_delete_block(waiter); 738ad6bbd8bSNeilBrown if (waiter->fl_lmops && waiter->fl_lmops->lm_notify) 739ad6bbd8bSNeilBrown waiter->fl_lmops->lm_notify(waiter); 740ad6bbd8bSNeilBrown else 741ad6bbd8bSNeilBrown wake_up(&waiter->fl_wait); 742dcf23ac3SLinus Torvalds 743dcf23ac3SLinus Torvalds /* 744dcf23ac3SLinus Torvalds * The setting of fl_blocker to NULL marks the "done" 745dcf23ac3SLinus Torvalds * point in deleting a block. Paired with acquire at the top 746dcf23ac3SLinus Torvalds * of locks_delete_block(). 747dcf23ac3SLinus Torvalds */ 748dcf23ac3SLinus Torvalds smp_store_release(&waiter->fl_blocker, NULL); 749ad6bbd8bSNeilBrown } 750ad6bbd8bSNeilBrown } 751ad6bbd8bSNeilBrown 752cb03f94fSNeilBrown /** 753cb03f94fSNeilBrown * locks_delete_lock - stop waiting for a file lock 754cb03f94fSNeilBrown * @waiter: the lock which was waiting 755cb03f94fSNeilBrown * 756cb03f94fSNeilBrown * lockd/nfsd need to disconnect the lock while working on it. 757cb03f94fSNeilBrown */ 758cb03f94fSNeilBrown int locks_delete_block(struct file_lock *waiter) 7591da177e4SLinus Torvalds { 760cb03f94fSNeilBrown int status = -ENOENT; 761cb03f94fSNeilBrown 762dcf23ac3SLinus Torvalds /* 763dcf23ac3SLinus Torvalds * If fl_blocker is NULL, it won't be set again as this thread "owns" 764dcf23ac3SLinus Torvalds * the lock and is the only one that might try to claim the lock. 765dcf23ac3SLinus Torvalds * 766dcf23ac3SLinus Torvalds * We use acquire/release to manage fl_blocker so that we can 767dcf23ac3SLinus Torvalds * optimize away taking the blocked_lock_lock in many cases. 768dcf23ac3SLinus Torvalds * 769dcf23ac3SLinus Torvalds * The smp_load_acquire guarantees two things: 770dcf23ac3SLinus Torvalds * 771dcf23ac3SLinus Torvalds * 1/ that fl_blocked_requests can be tested locklessly. If something 772dcf23ac3SLinus Torvalds * was recently added to that list it must have been in a locked region 773dcf23ac3SLinus Torvalds * *before* the locked region when fl_blocker was set to NULL. 774dcf23ac3SLinus Torvalds * 775dcf23ac3SLinus Torvalds * 2/ that no other thread is accessing 'waiter', so it is safe to free 776dcf23ac3SLinus Torvalds * it. __locks_wake_up_blocks is careful not to touch waiter after 777dcf23ac3SLinus Torvalds * fl_blocker is released. 778dcf23ac3SLinus Torvalds * 779dcf23ac3SLinus Torvalds * If a lockless check of fl_blocker shows it to be NULL, we know that 780dcf23ac3SLinus Torvalds * no new locks can be inserted into its fl_blocked_requests list, and 781dcf23ac3SLinus Torvalds * can avoid doing anything further if the list is empty. 782dcf23ac3SLinus Torvalds */ 783dcf23ac3SLinus Torvalds if (!smp_load_acquire(&waiter->fl_blocker) && 784dcf23ac3SLinus Torvalds list_empty(&waiter->fl_blocked_requests)) 785dcf23ac3SLinus Torvalds return status; 786dcf23ac3SLinus Torvalds 7877b2296afSJeff Layton spin_lock(&blocked_lock_lock); 788cb03f94fSNeilBrown if (waiter->fl_blocker) 789cb03f94fSNeilBrown status = 0; 7905946c431SNeilBrown __locks_wake_up_blocks(waiter); 7911da177e4SLinus Torvalds __locks_delete_block(waiter); 792dcf23ac3SLinus Torvalds 793dcf23ac3SLinus Torvalds /* 794dcf23ac3SLinus Torvalds * The setting of fl_blocker to NULL marks the "done" point in deleting 795dcf23ac3SLinus Torvalds * a block. Paired with acquire at the top of this function. 796dcf23ac3SLinus Torvalds */ 797dcf23ac3SLinus Torvalds smp_store_release(&waiter->fl_blocker, NULL); 7987b2296afSJeff Layton spin_unlock(&blocked_lock_lock); 799cb03f94fSNeilBrown return status; 8001da177e4SLinus Torvalds } 801cb03f94fSNeilBrown EXPORT_SYMBOL(locks_delete_block); 8021da177e4SLinus Torvalds 8031da177e4SLinus Torvalds /* Insert waiter into blocker's block list. 8041da177e4SLinus Torvalds * We use a circular list so that processes can be easily woken up in 8051da177e4SLinus Torvalds * the order they blocked. The documentation doesn't require this but 8061da177e4SLinus Torvalds * it seems like the reasonable thing to do. 8071c8c601aSJeff Layton * 8086109c850SJeff Layton * Must be called with both the flc_lock and blocked_lock_lock held. The 809ada5c1daSNeilBrown * fl_blocked_requests list itself is protected by the blocked_lock_lock, 810ada5c1daSNeilBrown * but by ensuring that the flc_lock is also held on insertions we can avoid 811ada5c1daSNeilBrown * taking the blocked_lock_lock in some cases when we see that the 812ada5c1daSNeilBrown * fl_blocked_requests list is empty. 813fd7732e0SNeilBrown * 814fd7732e0SNeilBrown * Rather than just adding to the list, we check for conflicts with any existing 815fd7732e0SNeilBrown * waiters, and add beneath any waiter that blocks the new waiter. 816fd7732e0SNeilBrown * Thus wakeups don't happen until needed. 8171da177e4SLinus Torvalds */ 8181c8c601aSJeff Layton static void __locks_insert_block(struct file_lock *blocker, 819fd7732e0SNeilBrown struct file_lock *waiter, 820fd7732e0SNeilBrown bool conflict(struct file_lock *, 821fd7732e0SNeilBrown struct file_lock *)) 8221da177e4SLinus Torvalds { 823fd7732e0SNeilBrown struct file_lock *fl; 824ada5c1daSNeilBrown BUG_ON(!list_empty(&waiter->fl_blocked_member)); 825fd7732e0SNeilBrown 826fd7732e0SNeilBrown new_blocker: 827fd7732e0SNeilBrown list_for_each_entry(fl, &blocker->fl_blocked_requests, fl_blocked_member) 828fd7732e0SNeilBrown if (conflict(fl, waiter)) { 829fd7732e0SNeilBrown blocker = fl; 830fd7732e0SNeilBrown goto new_blocker; 831fd7732e0SNeilBrown } 832ada5c1daSNeilBrown waiter->fl_blocker = blocker; 833ada5c1daSNeilBrown list_add_tail(&waiter->fl_blocked_member, &blocker->fl_blocked_requests); 834cff2fce5SJeff Layton if (IS_POSIX(blocker) && !IS_OFDLCK(blocker)) 8351c8c601aSJeff Layton locks_insert_global_blocked(waiter); 8365946c431SNeilBrown 8375946c431SNeilBrown /* The requests in waiter->fl_blocked are known to conflict with 8385946c431SNeilBrown * waiter, but might not conflict with blocker, or the requests 8395946c431SNeilBrown * and lock which block it. So they all need to be woken. 8405946c431SNeilBrown */ 8415946c431SNeilBrown __locks_wake_up_blocks(waiter); 8421c8c601aSJeff Layton } 8431c8c601aSJeff Layton 8446109c850SJeff Layton /* Must be called with flc_lock held. */ 8451c8c601aSJeff Layton static void locks_insert_block(struct file_lock *blocker, 846fd7732e0SNeilBrown struct file_lock *waiter, 847fd7732e0SNeilBrown bool conflict(struct file_lock *, 848fd7732e0SNeilBrown struct file_lock *)) 8491c8c601aSJeff Layton { 8507b2296afSJeff Layton spin_lock(&blocked_lock_lock); 851fd7732e0SNeilBrown __locks_insert_block(blocker, waiter, conflict); 8527b2296afSJeff Layton spin_unlock(&blocked_lock_lock); 8531da177e4SLinus Torvalds } 8541da177e4SLinus Torvalds 8551cb36012SJeff Layton /* 8561cb36012SJeff Layton * Wake up processes blocked waiting for blocker. 8571cb36012SJeff Layton * 8586109c850SJeff Layton * Must be called with the inode->flc_lock held! 8591da177e4SLinus Torvalds */ 8601da177e4SLinus Torvalds static void locks_wake_up_blocks(struct file_lock *blocker) 8611da177e4SLinus Torvalds { 8624e8c765dSJeff Layton /* 8634e8c765dSJeff Layton * Avoid taking global lock if list is empty. This is safe since new 8646109c850SJeff Layton * blocked requests are only added to the list under the flc_lock, and 865ada5c1daSNeilBrown * the flc_lock is always held here. Note that removal from the 866ada5c1daSNeilBrown * fl_blocked_requests list does not require the flc_lock, so we must 867ada5c1daSNeilBrown * recheck list_empty() after acquiring the blocked_lock_lock. 8684e8c765dSJeff Layton */ 869ada5c1daSNeilBrown if (list_empty(&blocker->fl_blocked_requests)) 8704e8c765dSJeff Layton return; 8714e8c765dSJeff Layton 8727b2296afSJeff Layton spin_lock(&blocked_lock_lock); 873ad6bbd8bSNeilBrown __locks_wake_up_blocks(blocker); 8747b2296afSJeff Layton spin_unlock(&blocked_lock_lock); 8751da177e4SLinus Torvalds } 8761da177e4SLinus Torvalds 8775263e31eSJeff Layton static void 878e084c1bdSJeff Layton locks_insert_lock_ctx(struct file_lock *fl, struct list_head *before) 8795263e31eSJeff Layton { 8805263e31eSJeff Layton list_add_tail(&fl->fl_list, before); 8815263e31eSJeff Layton locks_insert_global_locks(fl); 8825263e31eSJeff Layton } 8835263e31eSJeff Layton 8848634b51fSJeff Layton static void 885e084c1bdSJeff Layton locks_unlink_lock_ctx(struct file_lock *fl) 8861da177e4SLinus Torvalds { 88788974691SJeff Layton locks_delete_global_locks(fl); 8888634b51fSJeff Layton list_del_init(&fl->fl_list); 8891da177e4SLinus Torvalds locks_wake_up_blocks(fl); 89024cbe784SJeff Layton } 89124cbe784SJeff Layton 8925263e31eSJeff Layton static void 893e084c1bdSJeff Layton locks_delete_lock_ctx(struct file_lock *fl, struct list_head *dispose) 8945263e31eSJeff Layton { 895e084c1bdSJeff Layton locks_unlink_lock_ctx(fl); 8968634b51fSJeff Layton if (dispose) 8978634b51fSJeff Layton list_add(&fl->fl_list, dispose); 8988634b51fSJeff Layton else 8998634b51fSJeff Layton locks_free_lock(fl); 9005263e31eSJeff Layton } 9015263e31eSJeff Layton 9021da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. Common functionality 9031da177e4SLinus Torvalds * checks for shared/exclusive status of overlapping locks. 9041da177e4SLinus Torvalds */ 905c0e15908SNeilBrown static bool locks_conflict(struct file_lock *caller_fl, 906c0e15908SNeilBrown struct file_lock *sys_fl) 9071da177e4SLinus Torvalds { 9081da177e4SLinus Torvalds if (sys_fl->fl_type == F_WRLCK) 909c0e15908SNeilBrown return true; 9101da177e4SLinus Torvalds if (caller_fl->fl_type == F_WRLCK) 911c0e15908SNeilBrown return true; 912c0e15908SNeilBrown return false; 9131da177e4SLinus Torvalds } 9141da177e4SLinus Torvalds 9151da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. POSIX specific 9161da177e4SLinus Torvalds * checking before calling the locks_conflict(). 9171da177e4SLinus Torvalds */ 918c0e15908SNeilBrown static bool posix_locks_conflict(struct file_lock *caller_fl, 919c0e15908SNeilBrown struct file_lock *sys_fl) 9201da177e4SLinus Torvalds { 9211da177e4SLinus Torvalds /* POSIX locks owned by the same process do not conflict with 9221da177e4SLinus Torvalds * each other. 9231da177e4SLinus Torvalds */ 9249b8c8695SJeff Layton if (posix_same_owner(caller_fl, sys_fl)) 925c0e15908SNeilBrown return false; 9261da177e4SLinus Torvalds 9271da177e4SLinus Torvalds /* Check whether they overlap */ 9281da177e4SLinus Torvalds if (!locks_overlap(caller_fl, sys_fl)) 929c0e15908SNeilBrown return false; 9301da177e4SLinus Torvalds 931c0e15908SNeilBrown return locks_conflict(caller_fl, sys_fl); 9321da177e4SLinus Torvalds } 9331da177e4SLinus Torvalds 9341da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. FLOCK specific 9351da177e4SLinus Torvalds * checking before calling the locks_conflict(). 9361da177e4SLinus Torvalds */ 937c0e15908SNeilBrown static bool flock_locks_conflict(struct file_lock *caller_fl, 938c0e15908SNeilBrown struct file_lock *sys_fl) 9391da177e4SLinus Torvalds { 9401da177e4SLinus Torvalds /* FLOCK locks referring to the same filp do not conflict with 9411da177e4SLinus Torvalds * each other. 9421da177e4SLinus Torvalds */ 9439b8c8695SJeff Layton if (caller_fl->fl_file == sys_fl->fl_file) 944c0e15908SNeilBrown return false; 9451da177e4SLinus Torvalds if ((caller_fl->fl_type & LOCK_MAND) || (sys_fl->fl_type & LOCK_MAND)) 946c0e15908SNeilBrown return false; 9471da177e4SLinus Torvalds 948c0e15908SNeilBrown return locks_conflict(caller_fl, sys_fl); 9491da177e4SLinus Torvalds } 9501da177e4SLinus Torvalds 9516d34ac19SJ. Bruce Fields void 9529d6a8c5cSMarc Eshel posix_test_lock(struct file *filp, struct file_lock *fl) 9531da177e4SLinus Torvalds { 9541da177e4SLinus Torvalds struct file_lock *cfl; 955bd61e0a9SJeff Layton struct file_lock_context *ctx; 956c568d683SMiklos Szeredi struct inode *inode = locks_inode(filp); 9571da177e4SLinus Torvalds 958128a3785SDmitry Vyukov ctx = smp_load_acquire(&inode->i_flctx); 959bd61e0a9SJeff Layton if (!ctx || list_empty_careful(&ctx->flc_posix)) { 960bd61e0a9SJeff Layton fl->fl_type = F_UNLCK; 961bd61e0a9SJeff Layton return; 9621da177e4SLinus Torvalds } 963bd61e0a9SJeff Layton 9646109c850SJeff Layton spin_lock(&ctx->flc_lock); 965bd61e0a9SJeff Layton list_for_each_entry(cfl, &ctx->flc_posix, fl_list) { 966bd61e0a9SJeff Layton if (posix_locks_conflict(fl, cfl)) { 9673fe0fff1SKinglong Mee locks_copy_conflock(fl, cfl); 968bd61e0a9SJeff Layton goto out; 969bd61e0a9SJeff Layton } 970bd61e0a9SJeff Layton } 971129a84deSJ. Bruce Fields fl->fl_type = F_UNLCK; 972bd61e0a9SJeff Layton out: 9736109c850SJeff Layton spin_unlock(&ctx->flc_lock); 9746d34ac19SJ. Bruce Fields return; 9751da177e4SLinus Torvalds } 9761da177e4SLinus Torvalds EXPORT_SYMBOL(posix_test_lock); 9771da177e4SLinus Torvalds 978b533184fSJ. Bruce Fields /* 979b533184fSJ. Bruce Fields * Deadlock detection: 9801da177e4SLinus Torvalds * 981b533184fSJ. Bruce Fields * We attempt to detect deadlocks that are due purely to posix file 982b533184fSJ. Bruce Fields * locks. 9831da177e4SLinus Torvalds * 984b533184fSJ. Bruce Fields * We assume that a task can be waiting for at most one lock at a time. 985b533184fSJ. Bruce Fields * So for any acquired lock, the process holding that lock may be 986b533184fSJ. Bruce Fields * waiting on at most one other lock. That lock in turns may be held by 987b533184fSJ. Bruce Fields * someone waiting for at most one other lock. Given a requested lock 988b533184fSJ. Bruce Fields * caller_fl which is about to wait for a conflicting lock block_fl, we 989b533184fSJ. Bruce Fields * follow this chain of waiters to ensure we are not about to create a 990b533184fSJ. Bruce Fields * cycle. 99197855b49SJ. Bruce Fields * 992b533184fSJ. Bruce Fields * Since we do this before we ever put a process to sleep on a lock, we 993b533184fSJ. Bruce Fields * are ensured that there is never a cycle; that is what guarantees that 994b533184fSJ. Bruce Fields * the while() loop in posix_locks_deadlock() eventually completes. 995b533184fSJ. Bruce Fields * 996b533184fSJ. Bruce Fields * Note: the above assumption may not be true when handling lock 997b533184fSJ. Bruce Fields * requests from a broken NFS client. It may also fail in the presence 998b533184fSJ. Bruce Fields * of tasks (such as posix threads) sharing the same open file table. 999b533184fSJ. Bruce Fields * To handle those cases, we just bail out after a few iterations. 100057b65325SJeff Layton * 1001cff2fce5SJeff Layton * For FL_OFDLCK locks, the owner is the filp, not the files_struct. 100257b65325SJeff Layton * Because the owner is not even nominally tied to a thread of 100357b65325SJeff Layton * execution, the deadlock detection below can't reasonably work well. Just 100457b65325SJeff Layton * skip it for those. 100557b65325SJeff Layton * 1006cff2fce5SJeff Layton * In principle, we could do a more limited deadlock detection on FL_OFDLCK 100757b65325SJeff Layton * locks that just checks for the case where two tasks are attempting to 100857b65325SJeff Layton * upgrade from read to write locks on the same inode. 10091da177e4SLinus Torvalds */ 101097855b49SJ. Bruce Fields 101197855b49SJ. Bruce Fields #define MAX_DEADLK_ITERATIONS 10 101297855b49SJ. Bruce Fields 1013b533184fSJ. Bruce Fields /* Find a lock that the owner of the given block_fl is blocking on. */ 1014b533184fSJ. Bruce Fields static struct file_lock *what_owner_is_waiting_for(struct file_lock *block_fl) 1015b533184fSJ. Bruce Fields { 1016b533184fSJ. Bruce Fields struct file_lock *fl; 1017b533184fSJ. Bruce Fields 10183999e493SJeff Layton hash_for_each_possible(blocked_hash, fl, fl_link, posix_owner_key(block_fl)) { 10195946c431SNeilBrown if (posix_same_owner(fl, block_fl)) { 10205946c431SNeilBrown while (fl->fl_blocker) 10215946c431SNeilBrown fl = fl->fl_blocker; 10225946c431SNeilBrown return fl; 10235946c431SNeilBrown } 1024b533184fSJ. Bruce Fields } 1025b533184fSJ. Bruce Fields return NULL; 1026b533184fSJ. Bruce Fields } 1027b533184fSJ. Bruce Fields 10287b2296afSJeff Layton /* Must be called with the blocked_lock_lock held! */ 1029b0904e14SAdrian Bunk static int posix_locks_deadlock(struct file_lock *caller_fl, 10301da177e4SLinus Torvalds struct file_lock *block_fl) 10311da177e4SLinus Torvalds { 103297855b49SJ. Bruce Fields int i = 0; 10331da177e4SLinus Torvalds 1034663d5af7SDaniel Wagner lockdep_assert_held(&blocked_lock_lock); 1035663d5af7SDaniel Wagner 103657b65325SJeff Layton /* 103757b65325SJeff Layton * This deadlock detector can't reasonably detect deadlocks with 1038cff2fce5SJeff Layton * FL_OFDLCK locks, since they aren't owned by a process, per-se. 103957b65325SJeff Layton */ 1040cff2fce5SJeff Layton if (IS_OFDLCK(caller_fl)) 104157b65325SJeff Layton return 0; 104257b65325SJeff Layton 1043b533184fSJ. Bruce Fields while ((block_fl = what_owner_is_waiting_for(block_fl))) { 104497855b49SJ. Bruce Fields if (i++ > MAX_DEADLK_ITERATIONS) 104597855b49SJ. Bruce Fields return 0; 1046b533184fSJ. Bruce Fields if (posix_same_owner(caller_fl, block_fl)) 1047b533184fSJ. Bruce Fields return 1; 10481da177e4SLinus Torvalds } 10491da177e4SLinus Torvalds return 0; 10501da177e4SLinus Torvalds } 10511da177e4SLinus Torvalds 10521da177e4SLinus Torvalds /* Try to create a FLOCK lock on filp. We always insert new FLOCK locks 105302888f41SJ. Bruce Fields * after any leases, but before any posix locks. 1054f475ae95STrond Myklebust * 1055f475ae95STrond Myklebust * Note that if called with an FL_EXISTS argument, the caller may determine 1056f475ae95STrond Myklebust * whether or not a lock was successfully freed by testing the return 1057f475ae95STrond Myklebust * value for -ENOENT. 10581da177e4SLinus Torvalds */ 1059bcd7f78dSJeff Layton static int flock_lock_inode(struct inode *inode, struct file_lock *request) 10601da177e4SLinus Torvalds { 1061993dfa87STrond Myklebust struct file_lock *new_fl = NULL; 10625263e31eSJeff Layton struct file_lock *fl; 10635263e31eSJeff Layton struct file_lock_context *ctx; 10641da177e4SLinus Torvalds int error = 0; 10655263e31eSJeff Layton bool found = false; 1066ed9814d8SJeff Layton LIST_HEAD(dispose); 10671da177e4SLinus Torvalds 10685c1c669aSJeff Layton ctx = locks_get_lock_context(inode, request->fl_type); 10695c1c669aSJeff Layton if (!ctx) { 10705c1c669aSJeff Layton if (request->fl_type != F_UNLCK) 10715263e31eSJeff Layton return -ENOMEM; 10725c1c669aSJeff Layton return (request->fl_flags & FL_EXISTS) ? -ENOENT : 0; 10735c1c669aSJeff Layton } 10745263e31eSJeff Layton 1075b89f4321SArnd Bergmann if (!(request->fl_flags & FL_ACCESS) && (request->fl_type != F_UNLCK)) { 1076b89f4321SArnd Bergmann new_fl = locks_alloc_lock(); 1077b89f4321SArnd Bergmann if (!new_fl) 1078b89f4321SArnd Bergmann return -ENOMEM; 1079b89f4321SArnd Bergmann } 1080b89f4321SArnd Bergmann 108102e525b2SPeter Zijlstra percpu_down_read(&file_rwsem); 10826109c850SJeff Layton spin_lock(&ctx->flc_lock); 1083f07f18ddSTrond Myklebust if (request->fl_flags & FL_ACCESS) 1084f07f18ddSTrond Myklebust goto find_conflict; 108584d535adSPavel Emelyanov 10865263e31eSJeff Layton list_for_each_entry(fl, &ctx->flc_flock, fl_list) { 1087bcd7f78dSJeff Layton if (request->fl_file != fl->fl_file) 10881da177e4SLinus Torvalds continue; 1089993dfa87STrond Myklebust if (request->fl_type == fl->fl_type) 10901da177e4SLinus Torvalds goto out; 10915263e31eSJeff Layton found = true; 1092e084c1bdSJeff Layton locks_delete_lock_ctx(fl, &dispose); 10931da177e4SLinus Torvalds break; 10941da177e4SLinus Torvalds } 10951da177e4SLinus Torvalds 1096f475ae95STrond Myklebust if (request->fl_type == F_UNLCK) { 1097f475ae95STrond Myklebust if ((request->fl_flags & FL_EXISTS) && !found) 1098f475ae95STrond Myklebust error = -ENOENT; 1099993dfa87STrond Myklebust goto out; 1100f475ae95STrond Myklebust } 11011da177e4SLinus Torvalds 1102f07f18ddSTrond Myklebust find_conflict: 11035263e31eSJeff Layton list_for_each_entry(fl, &ctx->flc_flock, fl_list) { 1104993dfa87STrond Myklebust if (!flock_locks_conflict(request, fl)) 11051da177e4SLinus Torvalds continue; 11061da177e4SLinus Torvalds error = -EAGAIN; 1107bde74e4bSMiklos Szeredi if (!(request->fl_flags & FL_SLEEP)) 1108bde74e4bSMiklos Szeredi goto out; 1109bde74e4bSMiklos Szeredi error = FILE_LOCK_DEFERRED; 1110fd7732e0SNeilBrown locks_insert_block(fl, request, flock_locks_conflict); 11111da177e4SLinus Torvalds goto out; 11121da177e4SLinus Torvalds } 1113f07f18ddSTrond Myklebust if (request->fl_flags & FL_ACCESS) 1114f07f18ddSTrond Myklebust goto out; 1115993dfa87STrond Myklebust locks_copy_lock(new_fl, request); 11165946c431SNeilBrown locks_move_blocks(new_fl, request); 1117e084c1bdSJeff Layton locks_insert_lock_ctx(new_fl, &ctx->flc_flock); 1118993dfa87STrond Myklebust new_fl = NULL; 11199cedc194SKirill Korotaev error = 0; 11201da177e4SLinus Torvalds 11211da177e4SLinus Torvalds out: 11226109c850SJeff Layton spin_unlock(&ctx->flc_lock); 112302e525b2SPeter Zijlstra percpu_up_read(&file_rwsem); 1124993dfa87STrond Myklebust if (new_fl) 1125993dfa87STrond Myklebust locks_free_lock(new_fl); 1126ed9814d8SJeff Layton locks_dispose_list(&dispose); 1127c883da31SJeff Layton trace_flock_lock_inode(inode, request, error); 11281da177e4SLinus Torvalds return error; 11291da177e4SLinus Torvalds } 11301da177e4SLinus Torvalds 1131b4d629a3SJeff Layton static int posix_lock_inode(struct inode *inode, struct file_lock *request, 1132b4d629a3SJeff Layton struct file_lock *conflock) 11331da177e4SLinus Torvalds { 1134bd61e0a9SJeff Layton struct file_lock *fl, *tmp; 113539005d02SMiklos Szeredi struct file_lock *new_fl = NULL; 113639005d02SMiklos Szeredi struct file_lock *new_fl2 = NULL; 11371da177e4SLinus Torvalds struct file_lock *left = NULL; 11381da177e4SLinus Torvalds struct file_lock *right = NULL; 1139bd61e0a9SJeff Layton struct file_lock_context *ctx; 1140b9746ef8SJeff Layton int error; 1141b9746ef8SJeff Layton bool added = false; 1142ed9814d8SJeff Layton LIST_HEAD(dispose); 11431da177e4SLinus Torvalds 11445c1c669aSJeff Layton ctx = locks_get_lock_context(inode, request->fl_type); 1145bd61e0a9SJeff Layton if (!ctx) 11465c1c669aSJeff Layton return (request->fl_type == F_UNLCK) ? 0 : -ENOMEM; 1147bd61e0a9SJeff Layton 11481da177e4SLinus Torvalds /* 11491da177e4SLinus Torvalds * We may need two file_lock structures for this operation, 11501da177e4SLinus Torvalds * so we get them in advance to avoid races. 115139005d02SMiklos Szeredi * 115239005d02SMiklos Szeredi * In some cases we can be sure, that no new locks will be needed 11531da177e4SLinus Torvalds */ 115439005d02SMiklos Szeredi if (!(request->fl_flags & FL_ACCESS) && 115539005d02SMiklos Szeredi (request->fl_type != F_UNLCK || 115639005d02SMiklos Szeredi request->fl_start != 0 || request->fl_end != OFFSET_MAX)) { 11571da177e4SLinus Torvalds new_fl = locks_alloc_lock(); 11581da177e4SLinus Torvalds new_fl2 = locks_alloc_lock(); 115939005d02SMiklos Szeredi } 11601da177e4SLinus Torvalds 116102e525b2SPeter Zijlstra percpu_down_read(&file_rwsem); 11626109c850SJeff Layton spin_lock(&ctx->flc_lock); 11631cb36012SJeff Layton /* 11641cb36012SJeff Layton * New lock request. Walk all POSIX locks and look for conflicts. If 11651cb36012SJeff Layton * there are any, either return error or put the request on the 116648f74186SJeff Layton * blocker's list of waiters and the global blocked_hash. 11671cb36012SJeff Layton */ 11681da177e4SLinus Torvalds if (request->fl_type != F_UNLCK) { 1169bd61e0a9SJeff Layton list_for_each_entry(fl, &ctx->flc_posix, fl_list) { 11701da177e4SLinus Torvalds if (!posix_locks_conflict(request, fl)) 11711da177e4SLinus Torvalds continue; 11725842add2SAndy Adamson if (conflock) 11733fe0fff1SKinglong Mee locks_copy_conflock(conflock, fl); 11741da177e4SLinus Torvalds error = -EAGAIN; 11751da177e4SLinus Torvalds if (!(request->fl_flags & FL_SLEEP)) 11761da177e4SLinus Torvalds goto out; 11771c8c601aSJeff Layton /* 11781c8c601aSJeff Layton * Deadlock detection and insertion into the blocked 11791c8c601aSJeff Layton * locks list must be done while holding the same lock! 11801c8c601aSJeff Layton */ 11811da177e4SLinus Torvalds error = -EDEADLK; 11827b2296afSJeff Layton spin_lock(&blocked_lock_lock); 1183945ab8f6SJeff Layton /* 1184945ab8f6SJeff Layton * Ensure that we don't find any locks blocked on this 1185945ab8f6SJeff Layton * request during deadlock detection. 1186945ab8f6SJeff Layton */ 1187945ab8f6SJeff Layton __locks_wake_up_blocks(request); 11881c8c601aSJeff Layton if (likely(!posix_locks_deadlock(request, fl))) { 1189bde74e4bSMiklos Szeredi error = FILE_LOCK_DEFERRED; 1190fd7732e0SNeilBrown __locks_insert_block(fl, request, 1191fd7732e0SNeilBrown posix_locks_conflict); 11921c8c601aSJeff Layton } 11937b2296afSJeff Layton spin_unlock(&blocked_lock_lock); 11941da177e4SLinus Torvalds goto out; 11951da177e4SLinus Torvalds } 11961da177e4SLinus Torvalds } 11971da177e4SLinus Torvalds 11981da177e4SLinus Torvalds /* If we're just looking for a conflict, we're done. */ 11991da177e4SLinus Torvalds error = 0; 12001da177e4SLinus Torvalds if (request->fl_flags & FL_ACCESS) 12011da177e4SLinus Torvalds goto out; 12021da177e4SLinus Torvalds 1203bd61e0a9SJeff Layton /* Find the first old lock with the same owner as the new lock */ 1204bd61e0a9SJeff Layton list_for_each_entry(fl, &ctx->flc_posix, fl_list) { 1205bd61e0a9SJeff Layton if (posix_same_owner(request, fl)) 1206bd61e0a9SJeff Layton break; 12071da177e4SLinus Torvalds } 12081da177e4SLinus Torvalds 12091da177e4SLinus Torvalds /* Process locks with this owner. */ 1210bd61e0a9SJeff Layton list_for_each_entry_safe_from(fl, tmp, &ctx->flc_posix, fl_list) { 1211bd61e0a9SJeff Layton if (!posix_same_owner(request, fl)) 1212bd61e0a9SJeff Layton break; 1213bd61e0a9SJeff Layton 1214bd61e0a9SJeff Layton /* Detect adjacent or overlapping regions (if same lock type) */ 12151da177e4SLinus Torvalds if (request->fl_type == fl->fl_type) { 1216449231d6SOlaf Kirch /* In all comparisons of start vs end, use 1217449231d6SOlaf Kirch * "start - 1" rather than "end + 1". If end 1218449231d6SOlaf Kirch * is OFFSET_MAX, end + 1 will become negative. 1219449231d6SOlaf Kirch */ 12201da177e4SLinus Torvalds if (fl->fl_end < request->fl_start - 1) 1221bd61e0a9SJeff Layton continue; 12221da177e4SLinus Torvalds /* If the next lock in the list has entirely bigger 12231da177e4SLinus Torvalds * addresses than the new one, insert the lock here. 12241da177e4SLinus Torvalds */ 1225449231d6SOlaf Kirch if (fl->fl_start - 1 > request->fl_end) 12261da177e4SLinus Torvalds break; 12271da177e4SLinus Torvalds 12281da177e4SLinus Torvalds /* If we come here, the new and old lock are of the 12291da177e4SLinus Torvalds * same type and adjacent or overlapping. Make one 12301da177e4SLinus Torvalds * lock yielding from the lower start address of both 12311da177e4SLinus Torvalds * locks to the higher end address. 12321da177e4SLinus Torvalds */ 12331da177e4SLinus Torvalds if (fl->fl_start > request->fl_start) 12341da177e4SLinus Torvalds fl->fl_start = request->fl_start; 12351da177e4SLinus Torvalds else 12361da177e4SLinus Torvalds request->fl_start = fl->fl_start; 12371da177e4SLinus Torvalds if (fl->fl_end < request->fl_end) 12381da177e4SLinus Torvalds fl->fl_end = request->fl_end; 12391da177e4SLinus Torvalds else 12401da177e4SLinus Torvalds request->fl_end = fl->fl_end; 12411da177e4SLinus Torvalds if (added) { 1242e084c1bdSJeff Layton locks_delete_lock_ctx(fl, &dispose); 12431da177e4SLinus Torvalds continue; 12441da177e4SLinus Torvalds } 12451da177e4SLinus Torvalds request = fl; 1246b9746ef8SJeff Layton added = true; 1247bd61e0a9SJeff Layton } else { 12481da177e4SLinus Torvalds /* Processing for different lock types is a bit 12491da177e4SLinus Torvalds * more complex. 12501da177e4SLinus Torvalds */ 12511da177e4SLinus Torvalds if (fl->fl_end < request->fl_start) 1252bd61e0a9SJeff Layton continue; 12531da177e4SLinus Torvalds if (fl->fl_start > request->fl_end) 12541da177e4SLinus Torvalds break; 12551da177e4SLinus Torvalds if (request->fl_type == F_UNLCK) 1256b9746ef8SJeff Layton added = true; 12571da177e4SLinus Torvalds if (fl->fl_start < request->fl_start) 12581da177e4SLinus Torvalds left = fl; 12591da177e4SLinus Torvalds /* If the next lock in the list has a higher end 12601da177e4SLinus Torvalds * address than the new one, insert the new one here. 12611da177e4SLinus Torvalds */ 12621da177e4SLinus Torvalds if (fl->fl_end > request->fl_end) { 12631da177e4SLinus Torvalds right = fl; 12641da177e4SLinus Torvalds break; 12651da177e4SLinus Torvalds } 12661da177e4SLinus Torvalds if (fl->fl_start >= request->fl_start) { 12671da177e4SLinus Torvalds /* The new lock completely replaces an old 12681da177e4SLinus Torvalds * one (This may happen several times). 12691da177e4SLinus Torvalds */ 12701da177e4SLinus Torvalds if (added) { 1271e084c1bdSJeff Layton locks_delete_lock_ctx(fl, &dispose); 12721da177e4SLinus Torvalds continue; 12731da177e4SLinus Torvalds } 1274b84d49f9SJeff Layton /* 1275b84d49f9SJeff Layton * Replace the old lock with new_fl, and 1276b84d49f9SJeff Layton * remove the old one. It's safe to do the 1277b84d49f9SJeff Layton * insert here since we know that we won't be 1278b84d49f9SJeff Layton * using new_fl later, and that the lock is 1279b84d49f9SJeff Layton * just replacing an existing lock. 12801da177e4SLinus Torvalds */ 1281b84d49f9SJeff Layton error = -ENOLCK; 1282b84d49f9SJeff Layton if (!new_fl) 1283b84d49f9SJeff Layton goto out; 1284b84d49f9SJeff Layton locks_copy_lock(new_fl, request); 12855ef15968Syangerkun locks_move_blocks(new_fl, request); 1286b84d49f9SJeff Layton request = new_fl; 1287b84d49f9SJeff Layton new_fl = NULL; 1288e084c1bdSJeff Layton locks_insert_lock_ctx(request, &fl->fl_list); 1289e084c1bdSJeff Layton locks_delete_lock_ctx(fl, &dispose); 1290b9746ef8SJeff Layton added = true; 12911da177e4SLinus Torvalds } 12921da177e4SLinus Torvalds } 12931da177e4SLinus Torvalds } 12941da177e4SLinus Torvalds 12950d9a490aSMiklos Szeredi /* 12961cb36012SJeff Layton * The above code only modifies existing locks in case of merging or 12971cb36012SJeff Layton * replacing. If new lock(s) need to be inserted all modifications are 12981cb36012SJeff Layton * done below this, so it's safe yet to bail out. 12990d9a490aSMiklos Szeredi */ 13000d9a490aSMiklos Szeredi error = -ENOLCK; /* "no luck" */ 13010d9a490aSMiklos Szeredi if (right && left == right && !new_fl2) 13020d9a490aSMiklos Szeredi goto out; 13030d9a490aSMiklos Szeredi 13041da177e4SLinus Torvalds error = 0; 13051da177e4SLinus Torvalds if (!added) { 1306f475ae95STrond Myklebust if (request->fl_type == F_UNLCK) { 1307f475ae95STrond Myklebust if (request->fl_flags & FL_EXISTS) 1308f475ae95STrond Myklebust error = -ENOENT; 13091da177e4SLinus Torvalds goto out; 1310f475ae95STrond Myklebust } 13110d9a490aSMiklos Szeredi 13120d9a490aSMiklos Szeredi if (!new_fl) { 13130d9a490aSMiklos Szeredi error = -ENOLCK; 13140d9a490aSMiklos Szeredi goto out; 13150d9a490aSMiklos Szeredi } 13161da177e4SLinus Torvalds locks_copy_lock(new_fl, request); 13175946c431SNeilBrown locks_move_blocks(new_fl, request); 1318e084c1bdSJeff Layton locks_insert_lock_ctx(new_fl, &fl->fl_list); 13192e2f756fSJeff Layton fl = new_fl; 13201da177e4SLinus Torvalds new_fl = NULL; 13211da177e4SLinus Torvalds } 13221da177e4SLinus Torvalds if (right) { 13231da177e4SLinus Torvalds if (left == right) { 13241da177e4SLinus Torvalds /* The new lock breaks the old one in two pieces, 13251da177e4SLinus Torvalds * so we have to use the second new lock. 13261da177e4SLinus Torvalds */ 13271da177e4SLinus Torvalds left = new_fl2; 13281da177e4SLinus Torvalds new_fl2 = NULL; 13291da177e4SLinus Torvalds locks_copy_lock(left, right); 1330e084c1bdSJeff Layton locks_insert_lock_ctx(left, &fl->fl_list); 13311da177e4SLinus Torvalds } 13321da177e4SLinus Torvalds right->fl_start = request->fl_end + 1; 13331da177e4SLinus Torvalds locks_wake_up_blocks(right); 13341da177e4SLinus Torvalds } 13351da177e4SLinus Torvalds if (left) { 13361da177e4SLinus Torvalds left->fl_end = request->fl_start - 1; 13371da177e4SLinus Torvalds locks_wake_up_blocks(left); 13381da177e4SLinus Torvalds } 13391da177e4SLinus Torvalds out: 13406109c850SJeff Layton spin_unlock(&ctx->flc_lock); 134102e525b2SPeter Zijlstra percpu_up_read(&file_rwsem); 13421da177e4SLinus Torvalds /* 13431da177e4SLinus Torvalds * Free any unused locks. 13441da177e4SLinus Torvalds */ 13451da177e4SLinus Torvalds if (new_fl) 13461da177e4SLinus Torvalds locks_free_lock(new_fl); 13471da177e4SLinus Torvalds if (new_fl2) 13481da177e4SLinus Torvalds locks_free_lock(new_fl2); 1349ed9814d8SJeff Layton locks_dispose_list(&dispose); 13501890910fSJeff Layton trace_posix_lock_inode(inode, request, error); 13511890910fSJeff Layton 13521da177e4SLinus Torvalds return error; 13531da177e4SLinus Torvalds } 13541da177e4SLinus Torvalds 13551da177e4SLinus Torvalds /** 13561da177e4SLinus Torvalds * posix_lock_file - Apply a POSIX-style lock to a file 13571da177e4SLinus Torvalds * @filp: The file to apply the lock to 13581da177e4SLinus Torvalds * @fl: The lock to be applied 1359150b3934SMarc Eshel * @conflock: Place to return a copy of the conflicting lock, if found. 13601da177e4SLinus Torvalds * 13611da177e4SLinus Torvalds * Add a POSIX style lock to a file. 13621da177e4SLinus Torvalds * We merge adjacent & overlapping locks whenever possible. 13631da177e4SLinus Torvalds * POSIX locks are sorted by owner task, then by starting address 1364f475ae95STrond Myklebust * 1365f475ae95STrond Myklebust * Note that if called with an FL_EXISTS argument, the caller may determine 1366f475ae95STrond Myklebust * whether or not a lock was successfully freed by testing the return 1367f475ae95STrond Myklebust * value for -ENOENT. 13681da177e4SLinus Torvalds */ 1369150b3934SMarc Eshel int posix_lock_file(struct file *filp, struct file_lock *fl, 13705842add2SAndy Adamson struct file_lock *conflock) 13715842add2SAndy Adamson { 1372c568d683SMiklos Szeredi return posix_lock_inode(locks_inode(filp), fl, conflock); 13735842add2SAndy Adamson } 1374150b3934SMarc Eshel EXPORT_SYMBOL(posix_lock_file); 13751da177e4SLinus Torvalds 13761da177e4SLinus Torvalds /** 137729d01b22SJeff Layton * posix_lock_inode_wait - Apply a POSIX-style lock to a file 137829d01b22SJeff Layton * @inode: inode of file to which lock request should be applied 13791da177e4SLinus Torvalds * @fl: The lock to be applied 13801da177e4SLinus Torvalds * 1381616fb38fSBenjamin Coddington * Apply a POSIX style lock request to an inode. 13821da177e4SLinus Torvalds */ 1383616fb38fSBenjamin Coddington static int posix_lock_inode_wait(struct inode *inode, struct file_lock *fl) 13841da177e4SLinus Torvalds { 13851da177e4SLinus Torvalds int error; 13861da177e4SLinus Torvalds might_sleep (); 13871da177e4SLinus Torvalds for (;;) { 1388b4d629a3SJeff Layton error = posix_lock_inode(inode, fl, NULL); 1389bde74e4bSMiklos Szeredi if (error != FILE_LOCK_DEFERRED) 13901da177e4SLinus Torvalds break; 1391dcf23ac3SLinus Torvalds error = wait_event_interruptible(fl->fl_wait, 1392dcf23ac3SLinus Torvalds list_empty(&fl->fl_blocked_member)); 139316306a61SNeilBrown if (error) 13941da177e4SLinus Torvalds break; 13951da177e4SLinus Torvalds } 139616306a61SNeilBrown locks_delete_block(fl); 13971da177e4SLinus Torvalds return error; 13981da177e4SLinus Torvalds } 139929d01b22SJeff Layton 14009e8925b6SJeff Layton #ifdef CONFIG_MANDATORY_FILE_LOCKING 140129d01b22SJeff Layton /** 14021da177e4SLinus Torvalds * locks_mandatory_locked - Check for an active lock 1403d7a06983SJeff Layton * @file: the file to check 14041da177e4SLinus Torvalds * 14051da177e4SLinus Torvalds * Searches the inode's list of locks to find any POSIX locks which conflict. 14061da177e4SLinus Torvalds * This function is called from locks_verify_locked() only. 14071da177e4SLinus Torvalds */ 1408d7a06983SJeff Layton int locks_mandatory_locked(struct file *file) 14091da177e4SLinus Torvalds { 1410bd61e0a9SJeff Layton int ret; 1411c568d683SMiklos Szeredi struct inode *inode = locks_inode(file); 1412bd61e0a9SJeff Layton struct file_lock_context *ctx; 14131da177e4SLinus Torvalds struct file_lock *fl; 14141da177e4SLinus Torvalds 1415128a3785SDmitry Vyukov ctx = smp_load_acquire(&inode->i_flctx); 1416bd61e0a9SJeff Layton if (!ctx || list_empty_careful(&ctx->flc_posix)) 1417bd61e0a9SJeff Layton return 0; 1418bd61e0a9SJeff Layton 14191da177e4SLinus Torvalds /* 14201da177e4SLinus Torvalds * Search the lock list for this inode for any POSIX locks. 14211da177e4SLinus Torvalds */ 14226109c850SJeff Layton spin_lock(&ctx->flc_lock); 1423bd61e0a9SJeff Layton ret = 0; 1424bd61e0a9SJeff Layton list_for_each_entry(fl, &ctx->flc_posix, fl_list) { 142573a8f5f7SChristoph Hellwig if (fl->fl_owner != current->files && 1426bd61e0a9SJeff Layton fl->fl_owner != file) { 1427bd61e0a9SJeff Layton ret = -EAGAIN; 14281da177e4SLinus Torvalds break; 14291da177e4SLinus Torvalds } 1430bd61e0a9SJeff Layton } 14316109c850SJeff Layton spin_unlock(&ctx->flc_lock); 1432bd61e0a9SJeff Layton return ret; 14331da177e4SLinus Torvalds } 14341da177e4SLinus Torvalds 14351da177e4SLinus Torvalds /** 14361da177e4SLinus Torvalds * locks_mandatory_area - Check for a conflicting lock 14371da177e4SLinus Torvalds * @inode: the file to check 14381da177e4SLinus Torvalds * @filp: how the file was opened (if it was) 1439acc15575SChristoph Hellwig * @start: first byte in the file to check 1440acc15575SChristoph Hellwig * @end: lastbyte in the file to check 1441acc15575SChristoph Hellwig * @type: %F_WRLCK for a write lock, else %F_RDLCK 14421da177e4SLinus Torvalds * 14431da177e4SLinus Torvalds * Searches the inode's list of locks to find any POSIX locks which conflict. 14441da177e4SLinus Torvalds */ 1445acc15575SChristoph Hellwig int locks_mandatory_area(struct inode *inode, struct file *filp, loff_t start, 1446acc15575SChristoph Hellwig loff_t end, unsigned char type) 14471da177e4SLinus Torvalds { 14481da177e4SLinus Torvalds struct file_lock fl; 14491da177e4SLinus Torvalds int error; 145029723adeSJeff Layton bool sleep = false; 14511da177e4SLinus Torvalds 14521da177e4SLinus Torvalds locks_init_lock(&fl); 14531da177e4SLinus Torvalds fl.fl_pid = current->tgid; 14541da177e4SLinus Torvalds fl.fl_file = filp; 14551da177e4SLinus Torvalds fl.fl_flags = FL_POSIX | FL_ACCESS; 14561da177e4SLinus Torvalds if (filp && !(filp->f_flags & O_NONBLOCK)) 145729723adeSJeff Layton sleep = true; 1458acc15575SChristoph Hellwig fl.fl_type = type; 1459acc15575SChristoph Hellwig fl.fl_start = start; 1460acc15575SChristoph Hellwig fl.fl_end = end; 14611da177e4SLinus Torvalds 14621da177e4SLinus Torvalds for (;;) { 146329723adeSJeff Layton if (filp) { 146473a8f5f7SChristoph Hellwig fl.fl_owner = filp; 146529723adeSJeff Layton fl.fl_flags &= ~FL_SLEEP; 1466b4d629a3SJeff Layton error = posix_lock_inode(inode, &fl, NULL); 146729723adeSJeff Layton if (!error) 146829723adeSJeff Layton break; 146929723adeSJeff Layton } 147029723adeSJeff Layton 147129723adeSJeff Layton if (sleep) 147229723adeSJeff Layton fl.fl_flags |= FL_SLEEP; 147329723adeSJeff Layton fl.fl_owner = current->files; 1474b4d629a3SJeff Layton error = posix_lock_inode(inode, &fl, NULL); 1475bde74e4bSMiklos Szeredi if (error != FILE_LOCK_DEFERRED) 14761da177e4SLinus Torvalds break; 1477dcf23ac3SLinus Torvalds error = wait_event_interruptible(fl.fl_wait, 1478dcf23ac3SLinus Torvalds list_empty(&fl.fl_blocked_member)); 14791da177e4SLinus Torvalds if (!error) { 14801da177e4SLinus Torvalds /* 14811da177e4SLinus Torvalds * If we've been sleeping someone might have 14821da177e4SLinus Torvalds * changed the permissions behind our back. 14831da177e4SLinus Torvalds */ 1484a16877caSPavel Emelyanov if (__mandatory_lock(inode)) 14851da177e4SLinus Torvalds continue; 14861da177e4SLinus Torvalds } 14871da177e4SLinus Torvalds 14881da177e4SLinus Torvalds break; 14891da177e4SLinus Torvalds } 149016306a61SNeilBrown locks_delete_block(&fl); 14911da177e4SLinus Torvalds 14921da177e4SLinus Torvalds return error; 14931da177e4SLinus Torvalds } 14941da177e4SLinus Torvalds EXPORT_SYMBOL(locks_mandatory_area); 14959e8925b6SJeff Layton #endif /* CONFIG_MANDATORY_FILE_LOCKING */ 14961da177e4SLinus Torvalds 1497778fc546SJ. Bruce Fields static void lease_clear_pending(struct file_lock *fl, int arg) 1498778fc546SJ. Bruce Fields { 1499778fc546SJ. Bruce Fields switch (arg) { 1500778fc546SJ. Bruce Fields case F_UNLCK: 1501778fc546SJ. Bruce Fields fl->fl_flags &= ~FL_UNLOCK_PENDING; 1502df561f66SGustavo A. R. Silva fallthrough; 1503778fc546SJ. Bruce Fields case F_RDLCK: 1504778fc546SJ. Bruce Fields fl->fl_flags &= ~FL_DOWNGRADE_PENDING; 1505778fc546SJ. Bruce Fields } 1506778fc546SJ. Bruce Fields } 1507778fc546SJ. Bruce Fields 15081da177e4SLinus Torvalds /* We already had a lease on this file; just change its type */ 15097448cc37SJeff Layton int lease_modify(struct file_lock *fl, int arg, struct list_head *dispose) 15101da177e4SLinus Torvalds { 15111da177e4SLinus Torvalds int error = assign_type(fl, arg); 15121da177e4SLinus Torvalds 15131da177e4SLinus Torvalds if (error) 15141da177e4SLinus Torvalds return error; 1515778fc546SJ. Bruce Fields lease_clear_pending(fl, arg); 15161da177e4SLinus Torvalds locks_wake_up_blocks(fl); 15173b6e2723SFilipe Brandenburger if (arg == F_UNLCK) { 15183b6e2723SFilipe Brandenburger struct file *filp = fl->fl_file; 15193b6e2723SFilipe Brandenburger 15203b6e2723SFilipe Brandenburger f_delown(filp); 15213b6e2723SFilipe Brandenburger filp->f_owner.signum = 0; 152296d6d59cSJ. Bruce Fields fasync_helper(0, fl->fl_file, 0, &fl->fl_fasync); 152396d6d59cSJ. Bruce Fields if (fl->fl_fasync != NULL) { 152496d6d59cSJ. Bruce Fields printk(KERN_ERR "locks_delete_lock: fasync == %p\n", fl->fl_fasync); 152596d6d59cSJ. Bruce Fields fl->fl_fasync = NULL; 152696d6d59cSJ. Bruce Fields } 1527e084c1bdSJeff Layton locks_delete_lock_ctx(fl, dispose); 15283b6e2723SFilipe Brandenburger } 15291da177e4SLinus Torvalds return 0; 15301da177e4SLinus Torvalds } 15311da177e4SLinus Torvalds EXPORT_SYMBOL(lease_modify); 15321da177e4SLinus Torvalds 1533778fc546SJ. Bruce Fields static bool past_time(unsigned long then) 1534778fc546SJ. Bruce Fields { 1535778fc546SJ. Bruce Fields if (!then) 1536778fc546SJ. Bruce Fields /* 0 is a special value meaning "this never expires": */ 1537778fc546SJ. Bruce Fields return false; 1538778fc546SJ. Bruce Fields return time_after(jiffies, then); 1539778fc546SJ. Bruce Fields } 1540778fc546SJ. Bruce Fields 1541c45198edSJeff Layton static void time_out_leases(struct inode *inode, struct list_head *dispose) 15421da177e4SLinus Torvalds { 15438634b51fSJeff Layton struct file_lock_context *ctx = inode->i_flctx; 15448634b51fSJeff Layton struct file_lock *fl, *tmp; 15451da177e4SLinus Torvalds 15466109c850SJeff Layton lockdep_assert_held(&ctx->flc_lock); 1547f82b4b67SJeff Layton 15488634b51fSJeff Layton list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list) { 154962af4f1fSJeff Layton trace_time_out_leases(inode, fl); 1550778fc546SJ. Bruce Fields if (past_time(fl->fl_downgrade_time)) 15517448cc37SJeff Layton lease_modify(fl, F_RDLCK, dispose); 1552778fc546SJ. Bruce Fields if (past_time(fl->fl_break_time)) 15537448cc37SJeff Layton lease_modify(fl, F_UNLCK, dispose); 15541da177e4SLinus Torvalds } 15551da177e4SLinus Torvalds } 15561da177e4SLinus Torvalds 1557df4e8d2cSJ. Bruce Fields static bool leases_conflict(struct file_lock *lease, struct file_lock *breaker) 1558df4e8d2cSJ. Bruce Fields { 1559d51f527fSIra Weiny bool rc; 1560d51f527fSIra Weiny 156128df3d15SJ. Bruce Fields if (lease->fl_lmops->lm_breaker_owns_lease 156228df3d15SJ. Bruce Fields && lease->fl_lmops->lm_breaker_owns_lease(lease)) 156328df3d15SJ. Bruce Fields return false; 1564d51f527fSIra Weiny if ((breaker->fl_flags & FL_LAYOUT) != (lease->fl_flags & FL_LAYOUT)) { 1565d51f527fSIra Weiny rc = false; 1566d51f527fSIra Weiny goto trace; 1567d51f527fSIra Weiny } 1568d51f527fSIra Weiny if ((breaker->fl_flags & FL_DELEG) && (lease->fl_flags & FL_LEASE)) { 1569d51f527fSIra Weiny rc = false; 1570d51f527fSIra Weiny goto trace; 1571d51f527fSIra Weiny } 1572d51f527fSIra Weiny 1573d51f527fSIra Weiny rc = locks_conflict(breaker, lease); 1574d51f527fSIra Weiny trace: 1575d51f527fSIra Weiny trace_leases_conflict(rc, lease, breaker); 1576d51f527fSIra Weiny return rc; 1577df4e8d2cSJ. Bruce Fields } 1578df4e8d2cSJ. Bruce Fields 157903d12ddfSJeff Layton static bool 158003d12ddfSJeff Layton any_leases_conflict(struct inode *inode, struct file_lock *breaker) 158103d12ddfSJeff Layton { 15828634b51fSJeff Layton struct file_lock_context *ctx = inode->i_flctx; 158303d12ddfSJeff Layton struct file_lock *fl; 158403d12ddfSJeff Layton 15856109c850SJeff Layton lockdep_assert_held(&ctx->flc_lock); 158603d12ddfSJeff Layton 15878634b51fSJeff Layton list_for_each_entry(fl, &ctx->flc_lease, fl_list) { 158803d12ddfSJeff Layton if (leases_conflict(fl, breaker)) 158903d12ddfSJeff Layton return true; 159003d12ddfSJeff Layton } 159103d12ddfSJeff Layton return false; 159203d12ddfSJeff Layton } 159303d12ddfSJeff Layton 15941da177e4SLinus Torvalds /** 15951da177e4SLinus Torvalds * __break_lease - revoke all outstanding leases on file 15961da177e4SLinus Torvalds * @inode: the inode of the file to return 1597df4e8d2cSJ. Bruce Fields * @mode: O_RDONLY: break only write leases; O_WRONLY or O_RDWR: 1598df4e8d2cSJ. Bruce Fields * break all leases 1599df4e8d2cSJ. Bruce Fields * @type: FL_LEASE: break leases and delegations; FL_DELEG: break 1600df4e8d2cSJ. Bruce Fields * only delegations 16011da177e4SLinus Torvalds * 160287250dd2Sdavid m. richter * break_lease (inlined for speed) has checked there already is at least 160387250dd2Sdavid m. richter * some kind of lock (maybe a lease) on this file. Leases are broken on 160487250dd2Sdavid m. richter * a call to open() or truncate(). This function can sleep unless you 16051da177e4SLinus Torvalds * specified %O_NONBLOCK to your open(). 16061da177e4SLinus Torvalds */ 1607df4e8d2cSJ. Bruce Fields int __break_lease(struct inode *inode, unsigned int mode, unsigned int type) 16081da177e4SLinus Torvalds { 1609778fc546SJ. Bruce Fields int error = 0; 1610128a3785SDmitry Vyukov struct file_lock_context *ctx; 1611a901125cSYan, Zheng struct file_lock *new_fl, *fl, *tmp; 16121da177e4SLinus Torvalds unsigned long break_time; 16138737c930SAl Viro int want_write = (mode & O_ACCMODE) != O_RDONLY; 1614c45198edSJeff Layton LIST_HEAD(dispose); 16151da177e4SLinus Torvalds 16168737c930SAl Viro new_fl = lease_alloc(NULL, want_write ? F_WRLCK : F_RDLCK); 16176d4b9e38SLinus Torvalds if (IS_ERR(new_fl)) 16186d4b9e38SLinus Torvalds return PTR_ERR(new_fl); 1619df4e8d2cSJ. Bruce Fields new_fl->fl_flags = type; 16201da177e4SLinus Torvalds 16218634b51fSJeff Layton /* typically we will check that ctx is non-NULL before calling */ 1622128a3785SDmitry Vyukov ctx = smp_load_acquire(&inode->i_flctx); 16238634b51fSJeff Layton if (!ctx) { 16248634b51fSJeff Layton WARN_ON_ONCE(1); 1625cfddf9f4SWenwen Wang goto free_lock; 16268634b51fSJeff Layton } 16278634b51fSJeff Layton 162802e525b2SPeter Zijlstra percpu_down_read(&file_rwsem); 16296109c850SJeff Layton spin_lock(&ctx->flc_lock); 16301da177e4SLinus Torvalds 1631c45198edSJeff Layton time_out_leases(inode, &dispose); 16321da177e4SLinus Torvalds 163303d12ddfSJeff Layton if (!any_leases_conflict(inode, new_fl)) 1634df4e8d2cSJ. Bruce Fields goto out; 16351da177e4SLinus Torvalds 16361da177e4SLinus Torvalds break_time = 0; 16371da177e4SLinus Torvalds if (lease_break_time > 0) { 16381da177e4SLinus Torvalds break_time = jiffies + lease_break_time * HZ; 16391da177e4SLinus Torvalds if (break_time == 0) 16401da177e4SLinus Torvalds break_time++; /* so that 0 means no break time */ 16411da177e4SLinus Torvalds } 16421da177e4SLinus Torvalds 1643a901125cSYan, Zheng list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list) { 1644df4e8d2cSJ. Bruce Fields if (!leases_conflict(fl, new_fl)) 1645df4e8d2cSJ. Bruce Fields continue; 1646778fc546SJ. Bruce Fields if (want_write) { 1647778fc546SJ. Bruce Fields if (fl->fl_flags & FL_UNLOCK_PENDING) 1648778fc546SJ. Bruce Fields continue; 1649778fc546SJ. Bruce Fields fl->fl_flags |= FL_UNLOCK_PENDING; 16501da177e4SLinus Torvalds fl->fl_break_time = break_time; 1651778fc546SJ. Bruce Fields } else { 16528634b51fSJeff Layton if (lease_breaking(fl)) 1653778fc546SJ. Bruce Fields continue; 1654778fc546SJ. Bruce Fields fl->fl_flags |= FL_DOWNGRADE_PENDING; 1655778fc546SJ. Bruce Fields fl->fl_downgrade_time = break_time; 16561da177e4SLinus Torvalds } 16574d01b7f5SJeff Layton if (fl->fl_lmops->lm_break(fl)) 1658e084c1bdSJeff Layton locks_delete_lock_ctx(fl, &dispose); 16591da177e4SLinus Torvalds } 16601da177e4SLinus Torvalds 16618634b51fSJeff Layton if (list_empty(&ctx->flc_lease)) 16624d01b7f5SJeff Layton goto out; 16634d01b7f5SJeff Layton 1664843c6b2fSJeff Layton if (mode & O_NONBLOCK) { 166562af4f1fSJeff Layton trace_break_lease_noblock(inode, new_fl); 16661da177e4SLinus Torvalds error = -EWOULDBLOCK; 16671da177e4SLinus Torvalds goto out; 16681da177e4SLinus Torvalds } 16691da177e4SLinus Torvalds 16701da177e4SLinus Torvalds restart: 16718634b51fSJeff Layton fl = list_first_entry(&ctx->flc_lease, struct file_lock, fl_list); 16728634b51fSJeff Layton break_time = fl->fl_break_time; 1673f1c6bb2cSJeff Layton if (break_time != 0) 16741da177e4SLinus Torvalds break_time -= jiffies; 16751da177e4SLinus Torvalds if (break_time == 0) 16761da177e4SLinus Torvalds break_time++; 1677fd7732e0SNeilBrown locks_insert_block(fl, new_fl, leases_conflict); 167862af4f1fSJeff Layton trace_break_lease_block(inode, new_fl); 16796109c850SJeff Layton spin_unlock(&ctx->flc_lock); 168002e525b2SPeter Zijlstra percpu_up_read(&file_rwsem); 1681aba37660SPeter Zijlstra 1682c45198edSJeff Layton locks_dispose_list(&dispose); 16834321e01eSMatthew Wilcox error = wait_event_interruptible_timeout(new_fl->fl_wait, 1684dcf23ac3SLinus Torvalds list_empty(&new_fl->fl_blocked_member), 1685dcf23ac3SLinus Torvalds break_time); 1686aba37660SPeter Zijlstra 168702e525b2SPeter Zijlstra percpu_down_read(&file_rwsem); 16886109c850SJeff Layton spin_lock(&ctx->flc_lock); 168962af4f1fSJeff Layton trace_break_lease_unblock(inode, new_fl); 16901c8c601aSJeff Layton locks_delete_block(new_fl); 16911da177e4SLinus Torvalds if (error >= 0) { 1692778fc546SJ. Bruce Fields /* 1693778fc546SJ. Bruce Fields * Wait for the next conflicting lease that has not been 1694778fc546SJ. Bruce Fields * broken yet 1695778fc546SJ. Bruce Fields */ 169603d12ddfSJeff Layton if (error == 0) 169703d12ddfSJeff Layton time_out_leases(inode, &dispose); 169803d12ddfSJeff Layton if (any_leases_conflict(inode, new_fl)) 16991da177e4SLinus Torvalds goto restart; 17001da177e4SLinus Torvalds error = 0; 17011da177e4SLinus Torvalds } 17021da177e4SLinus Torvalds out: 17036109c850SJeff Layton spin_unlock(&ctx->flc_lock); 170402e525b2SPeter Zijlstra percpu_up_read(&file_rwsem); 1705c45198edSJeff Layton locks_dispose_list(&dispose); 1706cfddf9f4SWenwen Wang free_lock: 17071da177e4SLinus Torvalds locks_free_lock(new_fl); 17081da177e4SLinus Torvalds return error; 17091da177e4SLinus Torvalds } 17101da177e4SLinus Torvalds EXPORT_SYMBOL(__break_lease); 17111da177e4SLinus Torvalds 17121da177e4SLinus Torvalds /** 171376c47948SAmir Goldstein * lease_get_mtime - update modified time of an inode with exclusive lease 17141da177e4SLinus Torvalds * @inode: the inode 171576c47948SAmir Goldstein * @time: pointer to a timespec which contains the last modified time 17161da177e4SLinus Torvalds * 17171da177e4SLinus Torvalds * This is to force NFS clients to flush their caches for files with 17181da177e4SLinus Torvalds * exclusive leases. The justification is that if someone has an 1719a6b91919SRandy Dunlap * exclusive lease, then they could be modifying it. 17201da177e4SLinus Torvalds */ 172195582b00SDeepa Dinamani void lease_get_mtime(struct inode *inode, struct timespec64 *time) 17221da177e4SLinus Torvalds { 1723bfe86024SJeff Layton bool has_lease = false; 1724128a3785SDmitry Vyukov struct file_lock_context *ctx; 17258634b51fSJeff Layton struct file_lock *fl; 1726bfe86024SJeff Layton 1727128a3785SDmitry Vyukov ctx = smp_load_acquire(&inode->i_flctx); 17288634b51fSJeff Layton if (ctx && !list_empty_careful(&ctx->flc_lease)) { 17296109c850SJeff Layton spin_lock(&ctx->flc_lock); 17308ace5dfbSGeliang Tang fl = list_first_entry_or_null(&ctx->flc_lease, 17318634b51fSJeff Layton struct file_lock, fl_list); 17328ace5dfbSGeliang Tang if (fl && (fl->fl_type == F_WRLCK)) 1733bfe86024SJeff Layton has_lease = true; 17346109c850SJeff Layton spin_unlock(&ctx->flc_lock); 1735bfe86024SJeff Layton } 1736bfe86024SJeff Layton 1737bfe86024SJeff Layton if (has_lease) 1738c2050a45SDeepa Dinamani *time = current_time(inode); 17391da177e4SLinus Torvalds } 17401da177e4SLinus Torvalds EXPORT_SYMBOL(lease_get_mtime); 17411da177e4SLinus Torvalds 17421da177e4SLinus Torvalds /** 17431da177e4SLinus Torvalds * fcntl_getlease - Enquire what lease is currently active 17441da177e4SLinus Torvalds * @filp: the file 17451da177e4SLinus Torvalds * 17461da177e4SLinus Torvalds * The value returned by this function will be one of 17471da177e4SLinus Torvalds * (if no lease break is pending): 17481da177e4SLinus Torvalds * 17491da177e4SLinus Torvalds * %F_RDLCK to indicate a shared lease is held. 17501da177e4SLinus Torvalds * 17511da177e4SLinus Torvalds * %F_WRLCK to indicate an exclusive lease is held. 17521da177e4SLinus Torvalds * 17531da177e4SLinus Torvalds * %F_UNLCK to indicate no lease is held. 17541da177e4SLinus Torvalds * 17551da177e4SLinus Torvalds * (if a lease break is pending): 17561da177e4SLinus Torvalds * 17571da177e4SLinus Torvalds * %F_RDLCK to indicate an exclusive lease needs to be 17581da177e4SLinus Torvalds * changed to a shared lease (or removed). 17591da177e4SLinus Torvalds * 17601da177e4SLinus Torvalds * %F_UNLCK to indicate the lease needs to be removed. 17611da177e4SLinus Torvalds * 17621da177e4SLinus Torvalds * XXX: sfr & willy disagree over whether F_INPROGRESS 17631da177e4SLinus Torvalds * should be returned to userspace. 17641da177e4SLinus Torvalds */ 17651da177e4SLinus Torvalds int fcntl_getlease(struct file *filp) 17661da177e4SLinus Torvalds { 17671da177e4SLinus Torvalds struct file_lock *fl; 1768c568d683SMiklos Szeredi struct inode *inode = locks_inode(filp); 1769128a3785SDmitry Vyukov struct file_lock_context *ctx; 17701da177e4SLinus Torvalds int type = F_UNLCK; 1771c45198edSJeff Layton LIST_HEAD(dispose); 17721da177e4SLinus Torvalds 1773128a3785SDmitry Vyukov ctx = smp_load_acquire(&inode->i_flctx); 17748634b51fSJeff Layton if (ctx && !list_empty_careful(&ctx->flc_lease)) { 177502e525b2SPeter Zijlstra percpu_down_read(&file_rwsem); 17766109c850SJeff Layton spin_lock(&ctx->flc_lock); 1777c568d683SMiklos Szeredi time_out_leases(inode, &dispose); 17788634b51fSJeff Layton list_for_each_entry(fl, &ctx->flc_lease, fl_list) { 17798634b51fSJeff Layton if (fl->fl_file != filp) 17808634b51fSJeff Layton continue; 1781778fc546SJ. Bruce Fields type = target_leasetype(fl); 17821da177e4SLinus Torvalds break; 17831da177e4SLinus Torvalds } 17846109c850SJeff Layton spin_unlock(&ctx->flc_lock); 178502e525b2SPeter Zijlstra percpu_up_read(&file_rwsem); 17865f43086bSPeter Zijlstra 1787c45198edSJeff Layton locks_dispose_list(&dispose); 17888634b51fSJeff Layton } 17891da177e4SLinus Torvalds return type; 17901da177e4SLinus Torvalds } 17911da177e4SLinus Torvalds 179224cbe784SJeff Layton /** 1793387e3746SAmir Goldstein * check_conflicting_open - see if the given file points to an inode that has 179424cbe784SJeff Layton * an existing open that would conflict with the 179524cbe784SJeff Layton * desired lease. 1796387e3746SAmir Goldstein * @filp: file to check 179724cbe784SJeff Layton * @arg: type of lease that we're trying to acquire 17987fadc59cSRandy Dunlap * @flags: current lock flags 179924cbe784SJeff Layton * 180024cbe784SJeff Layton * Check to see if there's an existing open fd on this file that would 180124cbe784SJeff Layton * conflict with the lease we're trying to set. 180224cbe784SJeff Layton */ 180324cbe784SJeff Layton static int 1804387e3746SAmir Goldstein check_conflicting_open(struct file *filp, const long arg, int flags) 180524cbe784SJeff Layton { 1806387e3746SAmir Goldstein struct inode *inode = locks_inode(filp); 1807387e3746SAmir Goldstein int self_wcount = 0, self_rcount = 0; 180824cbe784SJeff Layton 180911afe9f7SChristoph Hellwig if (flags & FL_LAYOUT) 181011afe9f7SChristoph Hellwig return 0; 181194415b06SJ. Bruce Fields if (flags & FL_DELEG) 181294415b06SJ. Bruce Fields /* We leave these checks to the caller. */ 181394415b06SJ. Bruce Fields return 0; 181411afe9f7SChristoph Hellwig 1815387e3746SAmir Goldstein if (arg == F_RDLCK) 1816387e3746SAmir Goldstein return inode_is_open_for_write(inode) ? -EAGAIN : 0; 1817387e3746SAmir Goldstein else if (arg != F_WRLCK) 1818387e3746SAmir Goldstein return 0; 1819387e3746SAmir Goldstein 1820387e3746SAmir Goldstein /* 1821387e3746SAmir Goldstein * Make sure that only read/write count is from lease requestor. 1822387e3746SAmir Goldstein * Note that this will result in denying write leases when i_writecount 1823387e3746SAmir Goldstein * is negative, which is what we want. (We shouldn't grant write leases 1824387e3746SAmir Goldstein * on files open for execution.) 1825387e3746SAmir Goldstein */ 1826387e3746SAmir Goldstein if (filp->f_mode & FMODE_WRITE) 1827387e3746SAmir Goldstein self_wcount = 1; 1828387e3746SAmir Goldstein else if (filp->f_mode & FMODE_READ) 1829387e3746SAmir Goldstein self_rcount = 1; 1830387e3746SAmir Goldstein 1831387e3746SAmir Goldstein if (atomic_read(&inode->i_writecount) != self_wcount || 1832387e3746SAmir Goldstein atomic_read(&inode->i_readcount) != self_rcount) 183324cbe784SJeff Layton return -EAGAIN; 183424cbe784SJeff Layton 1835387e3746SAmir Goldstein return 0; 183624cbe784SJeff Layton } 183724cbe784SJeff Layton 1838e6f5c789SJeff Layton static int 1839e6f5c789SJeff Layton generic_add_lease(struct file *filp, long arg, struct file_lock **flp, void **priv) 18401da177e4SLinus Torvalds { 18418634b51fSJeff Layton struct file_lock *fl, *my_fl = NULL, *lease; 1842387e3746SAmir Goldstein struct inode *inode = locks_inode(filp); 18438634b51fSJeff Layton struct file_lock_context *ctx; 1844df4e8d2cSJ. Bruce Fields bool is_deleg = (*flp)->fl_flags & FL_DELEG; 1845c1f24ef4SJ. Bruce Fields int error; 1846c45198edSJeff Layton LIST_HEAD(dispose); 18471da177e4SLinus Torvalds 1848096657b6SJ. Bruce Fields lease = *flp; 184962af4f1fSJeff Layton trace_generic_add_lease(inode, lease); 185062af4f1fSJeff Layton 18515c1c669aSJeff Layton /* Note that arg is never F_UNLCK here */ 18525c1c669aSJeff Layton ctx = locks_get_lock_context(inode, arg); 18538634b51fSJeff Layton if (!ctx) 18548634b51fSJeff Layton return -ENOMEM; 18558634b51fSJeff Layton 1856df4e8d2cSJ. Bruce Fields /* 1857df4e8d2cSJ. Bruce Fields * In the delegation case we need mutual exclusion with 1858df4e8d2cSJ. Bruce Fields * a number of operations that take the i_mutex. We trylock 1859df4e8d2cSJ. Bruce Fields * because delegations are an optional optimization, and if 1860df4e8d2cSJ. Bruce Fields * there's some chance of a conflict--we'd rather not 1861df4e8d2cSJ. Bruce Fields * bother, maybe that's a sign this just isn't a good file to 1862df4e8d2cSJ. Bruce Fields * hand out a delegation on. 1863df4e8d2cSJ. Bruce Fields */ 18645955102cSAl Viro if (is_deleg && !inode_trylock(inode)) 1865df4e8d2cSJ. Bruce Fields return -EAGAIN; 1866df4e8d2cSJ. Bruce Fields 1867df4e8d2cSJ. Bruce Fields if (is_deleg && arg == F_WRLCK) { 1868df4e8d2cSJ. Bruce Fields /* Write delegations are not currently supported: */ 18695955102cSAl Viro inode_unlock(inode); 1870df4e8d2cSJ. Bruce Fields WARN_ON_ONCE(1); 1871df4e8d2cSJ. Bruce Fields return -EINVAL; 1872df4e8d2cSJ. Bruce Fields } 1873096657b6SJ. Bruce Fields 187402e525b2SPeter Zijlstra percpu_down_read(&file_rwsem); 18756109c850SJeff Layton spin_lock(&ctx->flc_lock); 1876c45198edSJeff Layton time_out_leases(inode, &dispose); 1877387e3746SAmir Goldstein error = check_conflicting_open(filp, arg, lease->fl_flags); 187824cbe784SJeff Layton if (error) 18791da177e4SLinus Torvalds goto out; 188085c59580SPavel Emelyanov 18811da177e4SLinus Torvalds /* 18821da177e4SLinus Torvalds * At this point, we know that if there is an exclusive 18831da177e4SLinus Torvalds * lease on this file, then we hold it on this filp 18841da177e4SLinus Torvalds * (otherwise our open of this file would have blocked). 18851da177e4SLinus Torvalds * And if we are trying to acquire an exclusive lease, 18861da177e4SLinus Torvalds * then the file is not open by anyone (including us) 18871da177e4SLinus Torvalds * except for this filp. 18881da177e4SLinus Torvalds */ 1889c1f24ef4SJ. Bruce Fields error = -EAGAIN; 18908634b51fSJeff Layton list_for_each_entry(fl, &ctx->flc_lease, fl_list) { 18912ab99ee1SChristoph Hellwig if (fl->fl_file == filp && 18922ab99ee1SChristoph Hellwig fl->fl_owner == lease->fl_owner) { 18938634b51fSJeff Layton my_fl = fl; 1894c1f24ef4SJ. Bruce Fields continue; 18951da177e4SLinus Torvalds } 18968634b51fSJeff Layton 1897c1f24ef4SJ. Bruce Fields /* 1898c1f24ef4SJ. Bruce Fields * No exclusive leases if someone else has a lease on 1899c1f24ef4SJ. Bruce Fields * this file: 1900c1f24ef4SJ. Bruce Fields */ 1901c1f24ef4SJ. Bruce Fields if (arg == F_WRLCK) 19021da177e4SLinus Torvalds goto out; 1903c1f24ef4SJ. Bruce Fields /* 1904c1f24ef4SJ. Bruce Fields * Modifying our existing lease is OK, but no getting a 1905c1f24ef4SJ. Bruce Fields * new lease if someone else is opening for write: 1906c1f24ef4SJ. Bruce Fields */ 1907c1f24ef4SJ. Bruce Fields if (fl->fl_flags & FL_UNLOCK_PENDING) 1908c1f24ef4SJ. Bruce Fields goto out; 1909c1f24ef4SJ. Bruce Fields } 19101da177e4SLinus Torvalds 19118634b51fSJeff Layton if (my_fl != NULL) { 19120164bf02SJeff Layton lease = my_fl; 19130164bf02SJeff Layton error = lease->fl_lmops->lm_change(lease, arg, &dispose); 19141c7dd2ffSJeff Layton if (error) 19151da177e4SLinus Torvalds goto out; 19161c7dd2ffSJeff Layton goto out_setup; 19171da177e4SLinus Torvalds } 19181da177e4SLinus Torvalds 19191da177e4SLinus Torvalds error = -EINVAL; 19201da177e4SLinus Torvalds if (!leases_enable) 19211da177e4SLinus Torvalds goto out; 19221da177e4SLinus Torvalds 1923e084c1bdSJeff Layton locks_insert_lock_ctx(lease, &ctx->flc_lease); 192424cbe784SJeff Layton /* 192524cbe784SJeff Layton * The check in break_lease() is lockless. It's possible for another 192624cbe784SJeff Layton * open to race in after we did the earlier check for a conflicting 192724cbe784SJeff Layton * open but before the lease was inserted. Check again for a 192824cbe784SJeff Layton * conflicting open and cancel the lease if there is one. 192924cbe784SJeff Layton * 193024cbe784SJeff Layton * We also add a barrier here to ensure that the insertion of the lock 193124cbe784SJeff Layton * precedes these checks. 193224cbe784SJeff Layton */ 193324cbe784SJeff Layton smp_mb(); 1934387e3746SAmir Goldstein error = check_conflicting_open(filp, arg, lease->fl_flags); 19358634b51fSJeff Layton if (error) { 1936e084c1bdSJeff Layton locks_unlink_lock_ctx(lease); 19378634b51fSJeff Layton goto out; 19388634b51fSJeff Layton } 19391c7dd2ffSJeff Layton 19401c7dd2ffSJeff Layton out_setup: 19411c7dd2ffSJeff Layton if (lease->fl_lmops->lm_setup) 19421c7dd2ffSJeff Layton lease->fl_lmops->lm_setup(lease, priv); 19431da177e4SLinus Torvalds out: 19446109c850SJeff Layton spin_unlock(&ctx->flc_lock); 194502e525b2SPeter Zijlstra percpu_up_read(&file_rwsem); 1946c45198edSJeff Layton locks_dispose_list(&dispose); 1947df4e8d2cSJ. Bruce Fields if (is_deleg) 19485955102cSAl Viro inode_unlock(inode); 19498634b51fSJeff Layton if (!error && !my_fl) 19501c7dd2ffSJeff Layton *flp = NULL; 19511da177e4SLinus Torvalds return error; 19521da177e4SLinus Torvalds } 19538335ebd9SJ. Bruce Fields 19542ab99ee1SChristoph Hellwig static int generic_delete_lease(struct file *filp, void *owner) 19558335ebd9SJ. Bruce Fields { 19560efaa7e8SJeff Layton int error = -EAGAIN; 19578634b51fSJeff Layton struct file_lock *fl, *victim = NULL; 1958c568d683SMiklos Szeredi struct inode *inode = locks_inode(filp); 1959128a3785SDmitry Vyukov struct file_lock_context *ctx; 1960c45198edSJeff Layton LIST_HEAD(dispose); 19618335ebd9SJ. Bruce Fields 1962128a3785SDmitry Vyukov ctx = smp_load_acquire(&inode->i_flctx); 19638634b51fSJeff Layton if (!ctx) { 19648634b51fSJeff Layton trace_generic_delete_lease(inode, NULL); 19658634b51fSJeff Layton return error; 19668634b51fSJeff Layton } 19678634b51fSJeff Layton 196802e525b2SPeter Zijlstra percpu_down_read(&file_rwsem); 19696109c850SJeff Layton spin_lock(&ctx->flc_lock); 19708634b51fSJeff Layton list_for_each_entry(fl, &ctx->flc_lease, fl_list) { 19712ab99ee1SChristoph Hellwig if (fl->fl_file == filp && 19722ab99ee1SChristoph Hellwig fl->fl_owner == owner) { 19738634b51fSJeff Layton victim = fl; 19740efaa7e8SJeff Layton break; 19758335ebd9SJ. Bruce Fields } 19768634b51fSJeff Layton } 1977a9b1b455SJeff Layton trace_generic_delete_lease(inode, victim); 19788634b51fSJeff Layton if (victim) 19797448cc37SJeff Layton error = fl->fl_lmops->lm_change(victim, F_UNLCK, &dispose); 19806109c850SJeff Layton spin_unlock(&ctx->flc_lock); 198102e525b2SPeter Zijlstra percpu_up_read(&file_rwsem); 1982c45198edSJeff Layton locks_dispose_list(&dispose); 19830efaa7e8SJeff Layton return error; 19848335ebd9SJ. Bruce Fields } 19858335ebd9SJ. Bruce Fields 19861da177e4SLinus Torvalds /** 19871da177e4SLinus Torvalds * generic_setlease - sets a lease on an open file 19881da177e4SLinus Torvalds * @filp: file pointer 19891da177e4SLinus Torvalds * @arg: type of lease to obtain 19901da177e4SLinus Torvalds * @flp: input - file_lock to use, output - file_lock inserted 19911c7dd2ffSJeff Layton * @priv: private data for lm_setup (may be NULL if lm_setup 19921c7dd2ffSJeff Layton * doesn't require it) 19931da177e4SLinus Torvalds * 19941da177e4SLinus Torvalds * The (input) flp->fl_lmops->lm_break function is required 19951da177e4SLinus Torvalds * by break_lease(). 19961da177e4SLinus Torvalds */ 1997e6f5c789SJeff Layton int generic_setlease(struct file *filp, long arg, struct file_lock **flp, 1998e6f5c789SJeff Layton void **priv) 19991da177e4SLinus Torvalds { 2000c568d683SMiklos Szeredi struct inode *inode = locks_inode(filp); 20018335ebd9SJ. Bruce Fields int error; 20021da177e4SLinus Torvalds 20038e96e3b7SEric W. Biederman if ((!uid_eq(current_fsuid(), inode->i_uid)) && !capable(CAP_LEASE)) 20048335ebd9SJ. Bruce Fields return -EACCES; 20051da177e4SLinus Torvalds if (!S_ISREG(inode->i_mode)) 20068335ebd9SJ. Bruce Fields return -EINVAL; 20071da177e4SLinus Torvalds error = security_file_lock(filp, arg); 20081da177e4SLinus Torvalds if (error) 20098335ebd9SJ. Bruce Fields return error; 20101da177e4SLinus Torvalds 20118335ebd9SJ. Bruce Fields switch (arg) { 20128335ebd9SJ. Bruce Fields case F_UNLCK: 20132ab99ee1SChristoph Hellwig return generic_delete_lease(filp, *priv); 20148335ebd9SJ. Bruce Fields case F_RDLCK: 20158335ebd9SJ. Bruce Fields case F_WRLCK: 20160efaa7e8SJeff Layton if (!(*flp)->fl_lmops->lm_break) { 20170efaa7e8SJeff Layton WARN_ON_ONCE(1); 20180efaa7e8SJeff Layton return -ENOLCK; 20190efaa7e8SJeff Layton } 202011afe9f7SChristoph Hellwig 2021e6f5c789SJeff Layton return generic_add_lease(filp, arg, flp, priv); 20228335ebd9SJ. Bruce Fields default: 20238d657eb3SDave Jones return -EINVAL; 20241da177e4SLinus Torvalds } 20251da177e4SLinus Torvalds } 20260af1a450SChristoph Hellwig EXPORT_SYMBOL(generic_setlease); 20271da177e4SLinus Torvalds 202818f6622eSJeff Layton #if IS_ENABLED(CONFIG_SRCU) 202918f6622eSJeff Layton /* 203018f6622eSJeff Layton * Kernel subsystems can register to be notified on any attempt to set 203118f6622eSJeff Layton * a new lease with the lease_notifier_chain. This is used by (e.g.) nfsd 203218f6622eSJeff Layton * to close files that it may have cached when there is an attempt to set a 203318f6622eSJeff Layton * conflicting lease. 203418f6622eSJeff Layton */ 203518f6622eSJeff Layton static struct srcu_notifier_head lease_notifier_chain; 203618f6622eSJeff Layton 203718f6622eSJeff Layton static inline void 203818f6622eSJeff Layton lease_notifier_chain_init(void) 203918f6622eSJeff Layton { 204018f6622eSJeff Layton srcu_init_notifier_head(&lease_notifier_chain); 204118f6622eSJeff Layton } 204218f6622eSJeff Layton 204318f6622eSJeff Layton static inline void 204418f6622eSJeff Layton setlease_notifier(long arg, struct file_lock *lease) 204518f6622eSJeff Layton { 204618f6622eSJeff Layton if (arg != F_UNLCK) 204718f6622eSJeff Layton srcu_notifier_call_chain(&lease_notifier_chain, arg, lease); 204818f6622eSJeff Layton } 204918f6622eSJeff Layton 205018f6622eSJeff Layton int lease_register_notifier(struct notifier_block *nb) 205118f6622eSJeff Layton { 205218f6622eSJeff Layton return srcu_notifier_chain_register(&lease_notifier_chain, nb); 205318f6622eSJeff Layton } 205418f6622eSJeff Layton EXPORT_SYMBOL_GPL(lease_register_notifier); 205518f6622eSJeff Layton 205618f6622eSJeff Layton void lease_unregister_notifier(struct notifier_block *nb) 205718f6622eSJeff Layton { 205818f6622eSJeff Layton srcu_notifier_chain_unregister(&lease_notifier_chain, nb); 205918f6622eSJeff Layton } 206018f6622eSJeff Layton EXPORT_SYMBOL_GPL(lease_unregister_notifier); 206118f6622eSJeff Layton 206218f6622eSJeff Layton #else /* !IS_ENABLED(CONFIG_SRCU) */ 206318f6622eSJeff Layton static inline void 206418f6622eSJeff Layton lease_notifier_chain_init(void) 206518f6622eSJeff Layton { 206618f6622eSJeff Layton } 206718f6622eSJeff Layton 206818f6622eSJeff Layton static inline void 206918f6622eSJeff Layton setlease_notifier(long arg, struct file_lock *lease) 207018f6622eSJeff Layton { 207118f6622eSJeff Layton } 207218f6622eSJeff Layton 207318f6622eSJeff Layton int lease_register_notifier(struct notifier_block *nb) 207418f6622eSJeff Layton { 207518f6622eSJeff Layton return 0; 207618f6622eSJeff Layton } 207718f6622eSJeff Layton EXPORT_SYMBOL_GPL(lease_register_notifier); 207818f6622eSJeff Layton 207918f6622eSJeff Layton void lease_unregister_notifier(struct notifier_block *nb) 208018f6622eSJeff Layton { 208118f6622eSJeff Layton } 208218f6622eSJeff Layton EXPORT_SYMBOL_GPL(lease_unregister_notifier); 208318f6622eSJeff Layton 208418f6622eSJeff Layton #endif /* IS_ENABLED(CONFIG_SRCU) */ 208518f6622eSJeff Layton 20861da177e4SLinus Torvalds /** 2087a9933ceaSJ. Bruce Fields * vfs_setlease - sets a lease on an open file 20881da177e4SLinus Torvalds * @filp: file pointer 20891da177e4SLinus Torvalds * @arg: type of lease to obtain 2090e51673aaSJeff Layton * @lease: file_lock to use when adding a lease 20911c7dd2ffSJeff Layton * @priv: private info for lm_setup when adding a lease (may be 20921c7dd2ffSJeff Layton * NULL if lm_setup doesn't require it) 20931da177e4SLinus Torvalds * 2094e51673aaSJeff Layton * Call this to establish a lease on the file. The "lease" argument is not 2095e51673aaSJeff Layton * used for F_UNLCK requests and may be NULL. For commands that set or alter 209680b79dd0SMauro Carvalho Chehab * an existing lease, the ``(*lease)->fl_lmops->lm_break`` operation must be 209780b79dd0SMauro Carvalho Chehab * set; if not, this function will return -ENOLCK (and generate a scary-looking 2098e51673aaSJeff Layton * stack trace). 20991c7dd2ffSJeff Layton * 21001c7dd2ffSJeff Layton * The "priv" pointer is passed directly to the lm_setup function as-is. It 21011c7dd2ffSJeff Layton * may be NULL if the lm_setup operation doesn't require it. 21021da177e4SLinus Torvalds */ 2103e6f5c789SJeff Layton int 2104e6f5c789SJeff Layton vfs_setlease(struct file *filp, long arg, struct file_lock **lease, void **priv) 21051da177e4SLinus Torvalds { 210618f6622eSJeff Layton if (lease) 210718f6622eSJeff Layton setlease_notifier(arg, *lease); 2108de2a4a50SMiklos Szeredi if (filp->f_op->setlease) 2109f82b4b67SJeff Layton return filp->f_op->setlease(filp, arg, lease, priv); 21101c7dd2ffSJeff Layton else 2111f82b4b67SJeff Layton return generic_setlease(filp, arg, lease, priv); 21121da177e4SLinus Torvalds } 2113a9933ceaSJ. Bruce Fields EXPORT_SYMBOL_GPL(vfs_setlease); 21141da177e4SLinus Torvalds 21150ceaf6c7SJ. Bruce Fields static int do_fcntl_add_lease(unsigned int fd, struct file *filp, long arg) 21161da177e4SLinus Torvalds { 21171c7dd2ffSJeff Layton struct file_lock *fl; 2118f7347ce4SLinus Torvalds struct fasync_struct *new; 21191da177e4SLinus Torvalds int error; 21201da177e4SLinus Torvalds 2121c5b1f0d9SArnd Bergmann fl = lease_alloc(filp, arg); 2122c5b1f0d9SArnd Bergmann if (IS_ERR(fl)) 2123c5b1f0d9SArnd Bergmann return PTR_ERR(fl); 21241da177e4SLinus Torvalds 2125f7347ce4SLinus Torvalds new = fasync_alloc(); 2126f7347ce4SLinus Torvalds if (!new) { 2127f7347ce4SLinus Torvalds locks_free_lock(fl); 2128f7347ce4SLinus Torvalds return -ENOMEM; 2129f7347ce4SLinus Torvalds } 21301c7dd2ffSJeff Layton new->fa_fd = fd; 21311da177e4SLinus Torvalds 21321c7dd2ffSJeff Layton error = vfs_setlease(filp, arg, &fl, (void **)&new); 21332dfb928fSJeff Layton if (fl) 21342dfb928fSJeff Layton locks_free_lock(fl); 2135f7347ce4SLinus Torvalds if (new) 2136f7347ce4SLinus Torvalds fasync_free(new); 21371da177e4SLinus Torvalds return error; 21381da177e4SLinus Torvalds } 21391da177e4SLinus Torvalds 21401da177e4SLinus Torvalds /** 21410ceaf6c7SJ. Bruce Fields * fcntl_setlease - sets a lease on an open file 21420ceaf6c7SJ. Bruce Fields * @fd: open file descriptor 21430ceaf6c7SJ. Bruce Fields * @filp: file pointer 21440ceaf6c7SJ. Bruce Fields * @arg: type of lease to obtain 21450ceaf6c7SJ. Bruce Fields * 21460ceaf6c7SJ. Bruce Fields * Call this fcntl to establish a lease on the file. 21470ceaf6c7SJ. Bruce Fields * Note that you also need to call %F_SETSIG to 21480ceaf6c7SJ. Bruce Fields * receive a signal when the lease is broken. 21490ceaf6c7SJ. Bruce Fields */ 21500ceaf6c7SJ. Bruce Fields int fcntl_setlease(unsigned int fd, struct file *filp, long arg) 21510ceaf6c7SJ. Bruce Fields { 21520ceaf6c7SJ. Bruce Fields if (arg == F_UNLCK) 21532ab99ee1SChristoph Hellwig return vfs_setlease(filp, F_UNLCK, NULL, (void **)&filp); 21540ceaf6c7SJ. Bruce Fields return do_fcntl_add_lease(fd, filp, arg); 21550ceaf6c7SJ. Bruce Fields } 21560ceaf6c7SJ. Bruce Fields 21570ceaf6c7SJ. Bruce Fields /** 215829d01b22SJeff Layton * flock_lock_inode_wait - Apply a FLOCK-style lock to a file 215929d01b22SJeff Layton * @inode: inode of the file to apply to 21601da177e4SLinus Torvalds * @fl: The lock to be applied 21611da177e4SLinus Torvalds * 216229d01b22SJeff Layton * Apply a FLOCK style lock request to an inode. 21631da177e4SLinus Torvalds */ 2164616fb38fSBenjamin Coddington static int flock_lock_inode_wait(struct inode *inode, struct file_lock *fl) 21651da177e4SLinus Torvalds { 21661da177e4SLinus Torvalds int error; 21671da177e4SLinus Torvalds might_sleep(); 21681da177e4SLinus Torvalds for (;;) { 216929d01b22SJeff Layton error = flock_lock_inode(inode, fl); 2170bde74e4bSMiklos Szeredi if (error != FILE_LOCK_DEFERRED) 21711da177e4SLinus Torvalds break; 2172dcf23ac3SLinus Torvalds error = wait_event_interruptible(fl->fl_wait, 2173dcf23ac3SLinus Torvalds list_empty(&fl->fl_blocked_member)); 217416306a61SNeilBrown if (error) 21751da177e4SLinus Torvalds break; 21761da177e4SLinus Torvalds } 217716306a61SNeilBrown locks_delete_block(fl); 21781da177e4SLinus Torvalds return error; 21791da177e4SLinus Torvalds } 21801da177e4SLinus Torvalds 218129d01b22SJeff Layton /** 2182e55c34a6SBenjamin Coddington * locks_lock_inode_wait - Apply a lock to an inode 2183e55c34a6SBenjamin Coddington * @inode: inode of the file to apply to 2184e55c34a6SBenjamin Coddington * @fl: The lock to be applied 2185e55c34a6SBenjamin Coddington * 2186e55c34a6SBenjamin Coddington * Apply a POSIX or FLOCK style lock request to an inode. 2187e55c34a6SBenjamin Coddington */ 2188e55c34a6SBenjamin Coddington int locks_lock_inode_wait(struct inode *inode, struct file_lock *fl) 2189e55c34a6SBenjamin Coddington { 2190e55c34a6SBenjamin Coddington int res = 0; 2191e55c34a6SBenjamin Coddington switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) { 2192e55c34a6SBenjamin Coddington case FL_POSIX: 2193e55c34a6SBenjamin Coddington res = posix_lock_inode_wait(inode, fl); 2194e55c34a6SBenjamin Coddington break; 2195e55c34a6SBenjamin Coddington case FL_FLOCK: 2196e55c34a6SBenjamin Coddington res = flock_lock_inode_wait(inode, fl); 2197e55c34a6SBenjamin Coddington break; 2198e55c34a6SBenjamin Coddington default: 2199e55c34a6SBenjamin Coddington BUG(); 2200e55c34a6SBenjamin Coddington } 2201e55c34a6SBenjamin Coddington return res; 2202e55c34a6SBenjamin Coddington } 2203e55c34a6SBenjamin Coddington EXPORT_SYMBOL(locks_lock_inode_wait); 2204e55c34a6SBenjamin Coddington 2205e55c34a6SBenjamin Coddington /** 22061da177e4SLinus Torvalds * sys_flock: - flock() system call. 22071da177e4SLinus Torvalds * @fd: the file descriptor to lock. 22081da177e4SLinus Torvalds * @cmd: the type of lock to apply. 22091da177e4SLinus Torvalds * 22101da177e4SLinus Torvalds * Apply a %FL_FLOCK style lock to an open file descriptor. 221180b79dd0SMauro Carvalho Chehab * The @cmd can be one of: 22121da177e4SLinus Torvalds * 221380b79dd0SMauro Carvalho Chehab * - %LOCK_SH -- a shared lock. 221480b79dd0SMauro Carvalho Chehab * - %LOCK_EX -- an exclusive lock. 221580b79dd0SMauro Carvalho Chehab * - %LOCK_UN -- remove an existing lock. 221680b79dd0SMauro Carvalho Chehab * - %LOCK_MAND -- a 'mandatory' flock. 221780b79dd0SMauro Carvalho Chehab * This exists to emulate Windows Share Modes. 22181da177e4SLinus Torvalds * 22191da177e4SLinus Torvalds * %LOCK_MAND can be combined with %LOCK_READ or %LOCK_WRITE to allow other 22201da177e4SLinus Torvalds * processes read and write access respectively. 22211da177e4SLinus Torvalds */ 2222002c8976SHeiko Carstens SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd) 22231da177e4SLinus Torvalds { 22242903ff01SAl Viro struct fd f = fdget(fd); 22251da177e4SLinus Torvalds struct file_lock *lock; 22261da177e4SLinus Torvalds int can_sleep, unlock; 22271da177e4SLinus Torvalds int error; 22281da177e4SLinus Torvalds 22291da177e4SLinus Torvalds error = -EBADF; 22302903ff01SAl Viro if (!f.file) 22311da177e4SLinus Torvalds goto out; 22321da177e4SLinus Torvalds 22331da177e4SLinus Torvalds can_sleep = !(cmd & LOCK_NB); 22341da177e4SLinus Torvalds cmd &= ~LOCK_NB; 22351da177e4SLinus Torvalds unlock = (cmd == LOCK_UN); 22361da177e4SLinus Torvalds 2237aeb5d727SAl Viro if (!unlock && !(cmd & LOCK_MAND) && 22382903ff01SAl Viro !(f.file->f_mode & (FMODE_READ|FMODE_WRITE))) 22391da177e4SLinus Torvalds goto out_putf; 22401da177e4SLinus Torvalds 2241d6367d62SNeilBrown lock = flock_make_lock(f.file, cmd, NULL); 22426e129d00SJeff Layton if (IS_ERR(lock)) { 22436e129d00SJeff Layton error = PTR_ERR(lock); 22441da177e4SLinus Torvalds goto out_putf; 22456e129d00SJeff Layton } 22466e129d00SJeff Layton 22471da177e4SLinus Torvalds if (can_sleep) 22481da177e4SLinus Torvalds lock->fl_flags |= FL_SLEEP; 22491da177e4SLinus Torvalds 22502903ff01SAl Viro error = security_file_lock(f.file, lock->fl_type); 22511da177e4SLinus Torvalds if (error) 22521da177e4SLinus Torvalds goto out_free; 22531da177e4SLinus Torvalds 2254de2a4a50SMiklos Szeredi if (f.file->f_op->flock) 22552903ff01SAl Viro error = f.file->f_op->flock(f.file, 22561da177e4SLinus Torvalds (can_sleep) ? F_SETLKW : F_SETLK, 22571da177e4SLinus Torvalds lock); 22581da177e4SLinus Torvalds else 22594f656367SBenjamin Coddington error = locks_lock_file_wait(f.file, lock); 22601da177e4SLinus Torvalds 22611da177e4SLinus Torvalds out_free: 22621da177e4SLinus Torvalds locks_free_lock(lock); 22631da177e4SLinus Torvalds 22641da177e4SLinus Torvalds out_putf: 22652903ff01SAl Viro fdput(f); 22661da177e4SLinus Torvalds out: 22671da177e4SLinus Torvalds return error; 22681da177e4SLinus Torvalds } 22691da177e4SLinus Torvalds 22703ee17abdSJ. Bruce Fields /** 22713ee17abdSJ. Bruce Fields * vfs_test_lock - test file byte range lock 22723ee17abdSJ. Bruce Fields * @filp: The file to test lock for 22736924c554SJ. Bruce Fields * @fl: The lock to test; also used to hold result 22743ee17abdSJ. Bruce Fields * 22753ee17abdSJ. Bruce Fields * Returns -ERRNO on failure. Indicates presence of conflicting lock by 22763ee17abdSJ. Bruce Fields * setting conf->fl_type to something other than F_UNLCK. 22773ee17abdSJ. Bruce Fields */ 22783ee17abdSJ. Bruce Fields int vfs_test_lock(struct file *filp, struct file_lock *fl) 22793ee17abdSJ. Bruce Fields { 2280de2a4a50SMiklos Szeredi if (filp->f_op->lock) 22813ee17abdSJ. Bruce Fields return filp->f_op->lock(filp, F_GETLK, fl); 22823ee17abdSJ. Bruce Fields posix_test_lock(filp, fl); 22833ee17abdSJ. Bruce Fields return 0; 22843ee17abdSJ. Bruce Fields } 22853ee17abdSJ. Bruce Fields EXPORT_SYMBOL_GPL(vfs_test_lock); 22863ee17abdSJ. Bruce Fields 22879d5b86acSBenjamin Coddington /** 22889d5b86acSBenjamin Coddington * locks_translate_pid - translate a file_lock's fl_pid number into a namespace 22899d5b86acSBenjamin Coddington * @fl: The file_lock who's fl_pid should be translated 22909d5b86acSBenjamin Coddington * @ns: The namespace into which the pid should be translated 22919d5b86acSBenjamin Coddington * 22929d5b86acSBenjamin Coddington * Used to tranlate a fl_pid into a namespace virtual pid number 22939d5b86acSBenjamin Coddington */ 22949d5b86acSBenjamin Coddington static pid_t locks_translate_pid(struct file_lock *fl, struct pid_namespace *ns) 22959d5b86acSBenjamin Coddington { 22969d5b86acSBenjamin Coddington pid_t vnr; 22979d5b86acSBenjamin Coddington struct pid *pid; 22989d5b86acSBenjamin Coddington 22999d5b86acSBenjamin Coddington if (IS_OFDLCK(fl)) 23009d5b86acSBenjamin Coddington return -1; 23019d5b86acSBenjamin Coddington if (IS_REMOTELCK(fl)) 23029d5b86acSBenjamin Coddington return fl->fl_pid; 2303826d7bc9SKonstantin Khorenko /* 2304826d7bc9SKonstantin Khorenko * If the flock owner process is dead and its pid has been already 2305826d7bc9SKonstantin Khorenko * freed, the translation below won't work, but we still want to show 2306826d7bc9SKonstantin Khorenko * flock owner pid number in init pidns. 2307826d7bc9SKonstantin Khorenko */ 2308826d7bc9SKonstantin Khorenko if (ns == &init_pid_ns) 2309826d7bc9SKonstantin Khorenko return (pid_t)fl->fl_pid; 23109d5b86acSBenjamin Coddington 23119d5b86acSBenjamin Coddington rcu_read_lock(); 23129d5b86acSBenjamin Coddington pid = find_pid_ns(fl->fl_pid, &init_pid_ns); 23139d5b86acSBenjamin Coddington vnr = pid_nr_ns(pid, ns); 23149d5b86acSBenjamin Coddington rcu_read_unlock(); 23159d5b86acSBenjamin Coddington return vnr; 23169d5b86acSBenjamin Coddington } 23179d5b86acSBenjamin Coddington 2318c2fa1b8aSJ. Bruce Fields static int posix_lock_to_flock(struct flock *flock, struct file_lock *fl) 2319c2fa1b8aSJ. Bruce Fields { 23209d5b86acSBenjamin Coddington flock->l_pid = locks_translate_pid(fl, task_active_pid_ns(current)); 2321c2fa1b8aSJ. Bruce Fields #if BITS_PER_LONG == 32 2322c2fa1b8aSJ. Bruce Fields /* 2323c2fa1b8aSJ. Bruce Fields * Make sure we can represent the posix lock via 2324c2fa1b8aSJ. Bruce Fields * legacy 32bit flock. 2325c2fa1b8aSJ. Bruce Fields */ 2326c2fa1b8aSJ. Bruce Fields if (fl->fl_start > OFFT_OFFSET_MAX) 2327c2fa1b8aSJ. Bruce Fields return -EOVERFLOW; 2328c2fa1b8aSJ. Bruce Fields if (fl->fl_end != OFFSET_MAX && fl->fl_end > OFFT_OFFSET_MAX) 2329c2fa1b8aSJ. Bruce Fields return -EOVERFLOW; 2330c2fa1b8aSJ. Bruce Fields #endif 2331c2fa1b8aSJ. Bruce Fields flock->l_start = fl->fl_start; 2332c2fa1b8aSJ. Bruce Fields flock->l_len = fl->fl_end == OFFSET_MAX ? 0 : 2333c2fa1b8aSJ. Bruce Fields fl->fl_end - fl->fl_start + 1; 2334c2fa1b8aSJ. Bruce Fields flock->l_whence = 0; 2335129a84deSJ. Bruce Fields flock->l_type = fl->fl_type; 2336c2fa1b8aSJ. Bruce Fields return 0; 2337c2fa1b8aSJ. Bruce Fields } 2338c2fa1b8aSJ. Bruce Fields 2339c2fa1b8aSJ. Bruce Fields #if BITS_PER_LONG == 32 2340c2fa1b8aSJ. Bruce Fields static void posix_lock_to_flock64(struct flock64 *flock, struct file_lock *fl) 2341c2fa1b8aSJ. Bruce Fields { 23429d5b86acSBenjamin Coddington flock->l_pid = locks_translate_pid(fl, task_active_pid_ns(current)); 2343c2fa1b8aSJ. Bruce Fields flock->l_start = fl->fl_start; 2344c2fa1b8aSJ. Bruce Fields flock->l_len = fl->fl_end == OFFSET_MAX ? 0 : 2345c2fa1b8aSJ. Bruce Fields fl->fl_end - fl->fl_start + 1; 2346c2fa1b8aSJ. Bruce Fields flock->l_whence = 0; 2347c2fa1b8aSJ. Bruce Fields flock->l_type = fl->fl_type; 2348c2fa1b8aSJ. Bruce Fields } 2349c2fa1b8aSJ. Bruce Fields #endif 2350c2fa1b8aSJ. Bruce Fields 23511da177e4SLinus Torvalds /* Report the first existing lock that would conflict with l. 23521da177e4SLinus Torvalds * This implements the F_GETLK command of fcntl(). 23531da177e4SLinus Torvalds */ 2354a75d30c7SChristoph Hellwig int fcntl_getlk(struct file *filp, unsigned int cmd, struct flock *flock) 23551da177e4SLinus Torvalds { 235652306e88SBenjamin Coddington struct file_lock *fl; 23571da177e4SLinus Torvalds int error; 23581da177e4SLinus Torvalds 235952306e88SBenjamin Coddington fl = locks_alloc_lock(); 236052306e88SBenjamin Coddington if (fl == NULL) 236152306e88SBenjamin Coddington return -ENOMEM; 23621da177e4SLinus Torvalds error = -EINVAL; 2363a75d30c7SChristoph Hellwig if (flock->l_type != F_RDLCK && flock->l_type != F_WRLCK) 23641da177e4SLinus Torvalds goto out; 23651da177e4SLinus Torvalds 236652306e88SBenjamin Coddington error = flock_to_posix_lock(filp, fl, flock); 23671da177e4SLinus Torvalds if (error) 23681da177e4SLinus Torvalds goto out; 23691da177e4SLinus Torvalds 23700d3f7a2dSJeff Layton if (cmd == F_OFD_GETLK) { 237190478939SJeff Layton error = -EINVAL; 2372a75d30c7SChristoph Hellwig if (flock->l_pid != 0) 237390478939SJeff Layton goto out; 237490478939SJeff Layton 23755d50ffd7SJeff Layton cmd = F_GETLK; 237652306e88SBenjamin Coddington fl->fl_flags |= FL_OFDLCK; 237752306e88SBenjamin Coddington fl->fl_owner = filp; 23785d50ffd7SJeff Layton } 23795d50ffd7SJeff Layton 238052306e88SBenjamin Coddington error = vfs_test_lock(filp, fl); 23813ee17abdSJ. Bruce Fields if (error) 23821da177e4SLinus Torvalds goto out; 23831da177e4SLinus Torvalds 238452306e88SBenjamin Coddington flock->l_type = fl->fl_type; 238552306e88SBenjamin Coddington if (fl->fl_type != F_UNLCK) { 238652306e88SBenjamin Coddington error = posix_lock_to_flock(flock, fl); 2387c2fa1b8aSJ. Bruce Fields if (error) 238852306e88SBenjamin Coddington goto out; 23891da177e4SLinus Torvalds } 23901da177e4SLinus Torvalds out: 239152306e88SBenjamin Coddington locks_free_lock(fl); 23921da177e4SLinus Torvalds return error; 23931da177e4SLinus Torvalds } 23941da177e4SLinus Torvalds 23957723ec97SMarc Eshel /** 23967723ec97SMarc Eshel * vfs_lock_file - file byte range lock 23977723ec97SMarc Eshel * @filp: The file to apply the lock to 23987723ec97SMarc Eshel * @cmd: type of locking operation (F_SETLK, F_GETLK, etc.) 23997723ec97SMarc Eshel * @fl: The lock to be applied 2400150b3934SMarc Eshel * @conf: Place to return a copy of the conflicting lock, if found. 2401150b3934SMarc Eshel * 2402150b3934SMarc Eshel * A caller that doesn't care about the conflicting lock may pass NULL 2403150b3934SMarc Eshel * as the final argument. 2404150b3934SMarc Eshel * 2405150b3934SMarc Eshel * If the filesystem defines a private ->lock() method, then @conf will 2406150b3934SMarc Eshel * be left unchanged; so a caller that cares should initialize it to 2407150b3934SMarc Eshel * some acceptable default. 24082beb6614SMarc Eshel * 24092beb6614SMarc Eshel * To avoid blocking kernel daemons, such as lockd, that need to acquire POSIX 24102beb6614SMarc Eshel * locks, the ->lock() interface may return asynchronously, before the lock has 24112beb6614SMarc Eshel * been granted or denied by the underlying filesystem, if (and only if) 24128fb47a4fSJ. Bruce Fields * lm_grant is set. Callers expecting ->lock() to return asynchronously 24132beb6614SMarc Eshel * will only use F_SETLK, not F_SETLKW; they will set FL_SLEEP if (and only if) 24142beb6614SMarc Eshel * the request is for a blocking lock. When ->lock() does return asynchronously, 24158fb47a4fSJ. Bruce Fields * it must return FILE_LOCK_DEFERRED, and call ->lm_grant() when the lock 24162beb6614SMarc Eshel * request completes. 24172beb6614SMarc Eshel * If the request is for non-blocking lock the file system should return 2418bde74e4bSMiklos Szeredi * FILE_LOCK_DEFERRED then try to get the lock and call the callback routine 2419bde74e4bSMiklos Szeredi * with the result. If the request timed out the callback routine will return a 24202beb6614SMarc Eshel * nonzero return code and the file system should release the lock. The file 24212beb6614SMarc Eshel * system is also responsible to keep a corresponding posix lock when it 24222beb6614SMarc Eshel * grants a lock so the VFS can find out which locks are locally held and do 24232beb6614SMarc Eshel * the correct lock cleanup when required. 24242beb6614SMarc Eshel * The underlying filesystem must not drop the kernel lock or call 24258fb47a4fSJ. Bruce Fields * ->lm_grant() before returning to the caller with a FILE_LOCK_DEFERRED 24262beb6614SMarc Eshel * return code. 24277723ec97SMarc Eshel */ 2428150b3934SMarc Eshel int vfs_lock_file(struct file *filp, unsigned int cmd, struct file_lock *fl, struct file_lock *conf) 24297723ec97SMarc Eshel { 2430de2a4a50SMiklos Szeredi if (filp->f_op->lock) 24317723ec97SMarc Eshel return filp->f_op->lock(filp, cmd, fl); 24327723ec97SMarc Eshel else 2433150b3934SMarc Eshel return posix_lock_file(filp, fl, conf); 24347723ec97SMarc Eshel } 24357723ec97SMarc Eshel EXPORT_SYMBOL_GPL(vfs_lock_file); 24367723ec97SMarc Eshel 2437b648a6deSMiklos Szeredi static int do_lock_file_wait(struct file *filp, unsigned int cmd, 2438b648a6deSMiklos Szeredi struct file_lock *fl) 2439b648a6deSMiklos Szeredi { 2440b648a6deSMiklos Szeredi int error; 2441b648a6deSMiklos Szeredi 2442b648a6deSMiklos Szeredi error = security_file_lock(filp, fl->fl_type); 2443b648a6deSMiklos Szeredi if (error) 2444b648a6deSMiklos Szeredi return error; 2445b648a6deSMiklos Szeredi 2446b648a6deSMiklos Szeredi for (;;) { 2447764c76b3SMiklos Szeredi error = vfs_lock_file(filp, cmd, fl, NULL); 2448b648a6deSMiklos Szeredi if (error != FILE_LOCK_DEFERRED) 2449b648a6deSMiklos Szeredi break; 2450dcf23ac3SLinus Torvalds error = wait_event_interruptible(fl->fl_wait, 2451dcf23ac3SLinus Torvalds list_empty(&fl->fl_blocked_member)); 245216306a61SNeilBrown if (error) 2453b648a6deSMiklos Szeredi break; 2454b648a6deSMiklos Szeredi } 245516306a61SNeilBrown locks_delete_block(fl); 2456b648a6deSMiklos Szeredi 2457b648a6deSMiklos Szeredi return error; 2458b648a6deSMiklos Szeredi } 2459b648a6deSMiklos Szeredi 24606ca7d910SBenjamin Coddington /* Ensure that fl->fl_file has compatible f_mode for F_SETLK calls */ 2461cf01f4eeSJeff Layton static int 2462cf01f4eeSJeff Layton check_fmode_for_setlk(struct file_lock *fl) 2463cf01f4eeSJeff Layton { 2464cf01f4eeSJeff Layton switch (fl->fl_type) { 2465cf01f4eeSJeff Layton case F_RDLCK: 2466cf01f4eeSJeff Layton if (!(fl->fl_file->f_mode & FMODE_READ)) 2467cf01f4eeSJeff Layton return -EBADF; 2468cf01f4eeSJeff Layton break; 2469cf01f4eeSJeff Layton case F_WRLCK: 2470cf01f4eeSJeff Layton if (!(fl->fl_file->f_mode & FMODE_WRITE)) 2471cf01f4eeSJeff Layton return -EBADF; 2472cf01f4eeSJeff Layton } 2473cf01f4eeSJeff Layton return 0; 2474cf01f4eeSJeff Layton } 2475cf01f4eeSJeff Layton 24761da177e4SLinus Torvalds /* Apply the lock described by l to an open file descriptor. 24771da177e4SLinus Torvalds * This implements both the F_SETLK and F_SETLKW commands of fcntl(). 24781da177e4SLinus Torvalds */ 2479c293621bSPeter Staubach int fcntl_setlk(unsigned int fd, struct file *filp, unsigned int cmd, 2480a75d30c7SChristoph Hellwig struct flock *flock) 24811da177e4SLinus Torvalds { 24821da177e4SLinus Torvalds struct file_lock *file_lock = locks_alloc_lock(); 2483a75d30c7SChristoph Hellwig struct inode *inode = locks_inode(filp); 24840b2bac2fSAl Viro struct file *f; 24851da177e4SLinus Torvalds int error; 24861da177e4SLinus Torvalds 24871da177e4SLinus Torvalds if (file_lock == NULL) 24881da177e4SLinus Torvalds return -ENOLCK; 24891da177e4SLinus Torvalds 24901da177e4SLinus Torvalds /* Don't allow mandatory locks on files that may be memory mapped 24911da177e4SLinus Torvalds * and shared. 24921da177e4SLinus Torvalds */ 2493a16877caSPavel Emelyanov if (mandatory_lock(inode) && mapping_writably_mapped(filp->f_mapping)) { 24941da177e4SLinus Torvalds error = -EAGAIN; 24951da177e4SLinus Torvalds goto out; 24961da177e4SLinus Torvalds } 24971da177e4SLinus Torvalds 2498a75d30c7SChristoph Hellwig error = flock_to_posix_lock(filp, file_lock, flock); 24991da177e4SLinus Torvalds if (error) 25001da177e4SLinus Torvalds goto out; 25015d50ffd7SJeff Layton 2502cf01f4eeSJeff Layton error = check_fmode_for_setlk(file_lock); 2503cf01f4eeSJeff Layton if (error) 2504cf01f4eeSJeff Layton goto out; 2505cf01f4eeSJeff Layton 25065d50ffd7SJeff Layton /* 25075d50ffd7SJeff Layton * If the cmd is requesting file-private locks, then set the 2508cff2fce5SJeff Layton * FL_OFDLCK flag and override the owner. 25095d50ffd7SJeff Layton */ 25105d50ffd7SJeff Layton switch (cmd) { 25110d3f7a2dSJeff Layton case F_OFD_SETLK: 251290478939SJeff Layton error = -EINVAL; 2513a75d30c7SChristoph Hellwig if (flock->l_pid != 0) 251490478939SJeff Layton goto out; 251590478939SJeff Layton 25165d50ffd7SJeff Layton cmd = F_SETLK; 2517cff2fce5SJeff Layton file_lock->fl_flags |= FL_OFDLCK; 251873a8f5f7SChristoph Hellwig file_lock->fl_owner = filp; 25195d50ffd7SJeff Layton break; 25200d3f7a2dSJeff Layton case F_OFD_SETLKW: 252190478939SJeff Layton error = -EINVAL; 2522a75d30c7SChristoph Hellwig if (flock->l_pid != 0) 252390478939SJeff Layton goto out; 252490478939SJeff Layton 25255d50ffd7SJeff Layton cmd = F_SETLKW; 2526cff2fce5SJeff Layton file_lock->fl_flags |= FL_OFDLCK; 252773a8f5f7SChristoph Hellwig file_lock->fl_owner = filp; 2528df561f66SGustavo A. R. Silva fallthrough; 25295d50ffd7SJeff Layton case F_SETLKW: 25301da177e4SLinus Torvalds file_lock->fl_flags |= FL_SLEEP; 25311da177e4SLinus Torvalds } 25321da177e4SLinus Torvalds 2533b648a6deSMiklos Szeredi error = do_lock_file_wait(filp, cmd, file_lock); 2534c293621bSPeter Staubach 2535c293621bSPeter Staubach /* 25360752ba80SJeff Layton * Attempt to detect a close/fcntl race and recover by releasing the 25370752ba80SJeff Layton * lock that was just acquired. There is no need to do that when we're 25380752ba80SJeff Layton * unlocking though, or for OFD locks. 2539c293621bSPeter Staubach */ 25400752ba80SJeff Layton if (!error && file_lock->fl_type != F_UNLCK && 25410752ba80SJeff Layton !(file_lock->fl_flags & FL_OFDLCK)) { 25420b2bac2fSAl Viro /* 25437f3697e2SJeff Layton * We need that spin_lock here - it prevents reordering between 25447f3697e2SJeff Layton * update of i_flctx->flc_posix and check for it done in 25457f3697e2SJeff Layton * close(). rcu_read_lock() wouldn't do. 25460b2bac2fSAl Viro */ 25470b2bac2fSAl Viro spin_lock(¤t->files->file_lock); 25480b2bac2fSAl Viro f = fcheck(fd); 25490b2bac2fSAl Viro spin_unlock(¤t->files->file_lock); 25507f3697e2SJeff Layton if (f != filp) { 25517f3697e2SJeff Layton file_lock->fl_type = F_UNLCK; 25527f3697e2SJeff Layton error = do_lock_file_wait(filp, cmd, file_lock); 25537f3697e2SJeff Layton WARN_ON_ONCE(error); 25547f3697e2SJeff Layton error = -EBADF; 2555c293621bSPeter Staubach } 25567f3697e2SJeff Layton } 25571da177e4SLinus Torvalds out: 25581890910fSJeff Layton trace_fcntl_setlk(inode, file_lock, error); 25591da177e4SLinus Torvalds locks_free_lock(file_lock); 25601da177e4SLinus Torvalds return error; 25611da177e4SLinus Torvalds } 25621da177e4SLinus Torvalds 25631da177e4SLinus Torvalds #if BITS_PER_LONG == 32 25641da177e4SLinus Torvalds /* Report the first existing lock that would conflict with l. 25651da177e4SLinus Torvalds * This implements the F_GETLK command of fcntl(). 25661da177e4SLinus Torvalds */ 2567a75d30c7SChristoph Hellwig int fcntl_getlk64(struct file *filp, unsigned int cmd, struct flock64 *flock) 25681da177e4SLinus Torvalds { 256952306e88SBenjamin Coddington struct file_lock *fl; 25701da177e4SLinus Torvalds int error; 25711da177e4SLinus Torvalds 257252306e88SBenjamin Coddington fl = locks_alloc_lock(); 257352306e88SBenjamin Coddington if (fl == NULL) 257452306e88SBenjamin Coddington return -ENOMEM; 257552306e88SBenjamin Coddington 25761da177e4SLinus Torvalds error = -EINVAL; 2577a75d30c7SChristoph Hellwig if (flock->l_type != F_RDLCK && flock->l_type != F_WRLCK) 25781da177e4SLinus Torvalds goto out; 25791da177e4SLinus Torvalds 258052306e88SBenjamin Coddington error = flock64_to_posix_lock(filp, fl, flock); 25811da177e4SLinus Torvalds if (error) 25821da177e4SLinus Torvalds goto out; 25831da177e4SLinus Torvalds 25840d3f7a2dSJeff Layton if (cmd == F_OFD_GETLK) { 258590478939SJeff Layton error = -EINVAL; 2586a75d30c7SChristoph Hellwig if (flock->l_pid != 0) 258790478939SJeff Layton goto out; 258890478939SJeff Layton 25895d50ffd7SJeff Layton cmd = F_GETLK64; 259052306e88SBenjamin Coddington fl->fl_flags |= FL_OFDLCK; 259152306e88SBenjamin Coddington fl->fl_owner = filp; 25925d50ffd7SJeff Layton } 25935d50ffd7SJeff Layton 259452306e88SBenjamin Coddington error = vfs_test_lock(filp, fl); 25953ee17abdSJ. Bruce Fields if (error) 25961da177e4SLinus Torvalds goto out; 25971da177e4SLinus Torvalds 259852306e88SBenjamin Coddington flock->l_type = fl->fl_type; 259952306e88SBenjamin Coddington if (fl->fl_type != F_UNLCK) 260052306e88SBenjamin Coddington posix_lock_to_flock64(flock, fl); 26011da177e4SLinus Torvalds 26021da177e4SLinus Torvalds out: 260352306e88SBenjamin Coddington locks_free_lock(fl); 26041da177e4SLinus Torvalds return error; 26051da177e4SLinus Torvalds } 26061da177e4SLinus Torvalds 26071da177e4SLinus Torvalds /* Apply the lock described by l to an open file descriptor. 26081da177e4SLinus Torvalds * This implements both the F_SETLK and F_SETLKW commands of fcntl(). 26091da177e4SLinus Torvalds */ 2610c293621bSPeter Staubach int fcntl_setlk64(unsigned int fd, struct file *filp, unsigned int cmd, 2611a75d30c7SChristoph Hellwig struct flock64 *flock) 26121da177e4SLinus Torvalds { 26131da177e4SLinus Torvalds struct file_lock *file_lock = locks_alloc_lock(); 2614a75d30c7SChristoph Hellwig struct inode *inode = locks_inode(filp); 26150b2bac2fSAl Viro struct file *f; 26161da177e4SLinus Torvalds int error; 26171da177e4SLinus Torvalds 26181da177e4SLinus Torvalds if (file_lock == NULL) 26191da177e4SLinus Torvalds return -ENOLCK; 26201da177e4SLinus Torvalds 26211da177e4SLinus Torvalds /* Don't allow mandatory locks on files that may be memory mapped 26221da177e4SLinus Torvalds * and shared. 26231da177e4SLinus Torvalds */ 2624a16877caSPavel Emelyanov if (mandatory_lock(inode) && mapping_writably_mapped(filp->f_mapping)) { 26251da177e4SLinus Torvalds error = -EAGAIN; 26261da177e4SLinus Torvalds goto out; 26271da177e4SLinus Torvalds } 26281da177e4SLinus Torvalds 2629a75d30c7SChristoph Hellwig error = flock64_to_posix_lock(filp, file_lock, flock); 26301da177e4SLinus Torvalds if (error) 26311da177e4SLinus Torvalds goto out; 26325d50ffd7SJeff Layton 2633cf01f4eeSJeff Layton error = check_fmode_for_setlk(file_lock); 2634cf01f4eeSJeff Layton if (error) 2635cf01f4eeSJeff Layton goto out; 2636cf01f4eeSJeff Layton 26375d50ffd7SJeff Layton /* 26385d50ffd7SJeff Layton * If the cmd is requesting file-private locks, then set the 2639cff2fce5SJeff Layton * FL_OFDLCK flag and override the owner. 26405d50ffd7SJeff Layton */ 26415d50ffd7SJeff Layton switch (cmd) { 26420d3f7a2dSJeff Layton case F_OFD_SETLK: 264390478939SJeff Layton error = -EINVAL; 2644a75d30c7SChristoph Hellwig if (flock->l_pid != 0) 264590478939SJeff Layton goto out; 264690478939SJeff Layton 26475d50ffd7SJeff Layton cmd = F_SETLK64; 2648cff2fce5SJeff Layton file_lock->fl_flags |= FL_OFDLCK; 264973a8f5f7SChristoph Hellwig file_lock->fl_owner = filp; 26505d50ffd7SJeff Layton break; 26510d3f7a2dSJeff Layton case F_OFD_SETLKW: 265290478939SJeff Layton error = -EINVAL; 2653a75d30c7SChristoph Hellwig if (flock->l_pid != 0) 265490478939SJeff Layton goto out; 265590478939SJeff Layton 26565d50ffd7SJeff Layton cmd = F_SETLKW64; 2657cff2fce5SJeff Layton file_lock->fl_flags |= FL_OFDLCK; 265873a8f5f7SChristoph Hellwig file_lock->fl_owner = filp; 2659df561f66SGustavo A. R. Silva fallthrough; 26605d50ffd7SJeff Layton case F_SETLKW64: 26611da177e4SLinus Torvalds file_lock->fl_flags |= FL_SLEEP; 26621da177e4SLinus Torvalds } 26631da177e4SLinus Torvalds 2664b648a6deSMiklos Szeredi error = do_lock_file_wait(filp, cmd, file_lock); 2665c293621bSPeter Staubach 2666c293621bSPeter Staubach /* 26670752ba80SJeff Layton * Attempt to detect a close/fcntl race and recover by releasing the 26680752ba80SJeff Layton * lock that was just acquired. There is no need to do that when we're 26690752ba80SJeff Layton * unlocking though, or for OFD locks. 2670c293621bSPeter Staubach */ 26710752ba80SJeff Layton if (!error && file_lock->fl_type != F_UNLCK && 26720752ba80SJeff Layton !(file_lock->fl_flags & FL_OFDLCK)) { 26737f3697e2SJeff Layton /* 26747f3697e2SJeff Layton * We need that spin_lock here - it prevents reordering between 26757f3697e2SJeff Layton * update of i_flctx->flc_posix and check for it done in 26767f3697e2SJeff Layton * close(). rcu_read_lock() wouldn't do. 26777f3697e2SJeff Layton */ 26780b2bac2fSAl Viro spin_lock(¤t->files->file_lock); 26790b2bac2fSAl Viro f = fcheck(fd); 26800b2bac2fSAl Viro spin_unlock(¤t->files->file_lock); 26817f3697e2SJeff Layton if (f != filp) { 26827f3697e2SJeff Layton file_lock->fl_type = F_UNLCK; 26837f3697e2SJeff Layton error = do_lock_file_wait(filp, cmd, file_lock); 26847f3697e2SJeff Layton WARN_ON_ONCE(error); 26857f3697e2SJeff Layton error = -EBADF; 2686c293621bSPeter Staubach } 26877f3697e2SJeff Layton } 26881da177e4SLinus Torvalds out: 26891da177e4SLinus Torvalds locks_free_lock(file_lock); 26901da177e4SLinus Torvalds return error; 26911da177e4SLinus Torvalds } 26921da177e4SLinus Torvalds #endif /* BITS_PER_LONG == 32 */ 26931da177e4SLinus Torvalds 26941da177e4SLinus Torvalds /* 26951da177e4SLinus Torvalds * This function is called when the file is being removed 26961da177e4SLinus Torvalds * from the task's fd array. POSIX locks belonging to this task 26971da177e4SLinus Torvalds * are deleted at this time. 26981da177e4SLinus Torvalds */ 26991da177e4SLinus Torvalds void locks_remove_posix(struct file *filp, fl_owner_t owner) 27001da177e4SLinus Torvalds { 27011890910fSJeff Layton int error; 2702c568d683SMiklos Szeredi struct inode *inode = locks_inode(filp); 2703ff7b86b8SMiklos Szeredi struct file_lock lock; 2704128a3785SDmitry Vyukov struct file_lock_context *ctx; 27051da177e4SLinus Torvalds 27061da177e4SLinus Torvalds /* 27071da177e4SLinus Torvalds * If there are no locks held on this file, we don't need to call 27081da177e4SLinus Torvalds * posix_lock_file(). Another process could be setting a lock on this 27091da177e4SLinus Torvalds * file at the same time, but we wouldn't remove that lock anyway. 27101da177e4SLinus Torvalds */ 2711c568d683SMiklos Szeredi ctx = smp_load_acquire(&inode->i_flctx); 2712bd61e0a9SJeff Layton if (!ctx || list_empty(&ctx->flc_posix)) 27131da177e4SLinus Torvalds return; 27141da177e4SLinus Torvalds 2715d6367d62SNeilBrown locks_init_lock(&lock); 27161da177e4SLinus Torvalds lock.fl_type = F_UNLCK; 271775e1fcc0SMiklos Szeredi lock.fl_flags = FL_POSIX | FL_CLOSE; 27181da177e4SLinus Torvalds lock.fl_start = 0; 27191da177e4SLinus Torvalds lock.fl_end = OFFSET_MAX; 27201da177e4SLinus Torvalds lock.fl_owner = owner; 27211da177e4SLinus Torvalds lock.fl_pid = current->tgid; 27221da177e4SLinus Torvalds lock.fl_file = filp; 27231da177e4SLinus Torvalds lock.fl_ops = NULL; 27241da177e4SLinus Torvalds lock.fl_lmops = NULL; 27251da177e4SLinus Torvalds 27261890910fSJeff Layton error = vfs_lock_file(filp, F_SETLK, &lock, NULL); 27271da177e4SLinus Torvalds 27281da177e4SLinus Torvalds if (lock.fl_ops && lock.fl_ops->fl_release_private) 27291da177e4SLinus Torvalds lock.fl_ops->fl_release_private(&lock); 2730c568d683SMiklos Szeredi trace_locks_remove_posix(inode, &lock, error); 27311da177e4SLinus Torvalds } 27321da177e4SLinus Torvalds EXPORT_SYMBOL(locks_remove_posix); 27331da177e4SLinus Torvalds 27343d8e560dSJeff Layton /* The i_flctx must be valid when calling into here */ 2735dd459bb1SJeff Layton static void 2736128a3785SDmitry Vyukov locks_remove_flock(struct file *filp, struct file_lock_context *flctx) 2737dd459bb1SJeff Layton { 2738d6367d62SNeilBrown struct file_lock fl; 2739c568d683SMiklos Szeredi struct inode *inode = locks_inode(filp); 2740dd459bb1SJeff Layton 27413d8e560dSJeff Layton if (list_empty(&flctx->flc_flock)) 2742dd459bb1SJeff Layton return; 2743dd459bb1SJeff Layton 2744d6367d62SNeilBrown flock_make_lock(filp, LOCK_UN, &fl); 2745d6367d62SNeilBrown fl.fl_flags |= FL_CLOSE; 2746d6367d62SNeilBrown 2747de2a4a50SMiklos Szeredi if (filp->f_op->flock) 2748dd459bb1SJeff Layton filp->f_op->flock(filp, F_SETLKW, &fl); 2749dd459bb1SJeff Layton else 2750bcd7f78dSJeff Layton flock_lock_inode(inode, &fl); 2751dd459bb1SJeff Layton 2752dd459bb1SJeff Layton if (fl.fl_ops && fl.fl_ops->fl_release_private) 2753dd459bb1SJeff Layton fl.fl_ops->fl_release_private(&fl); 2754dd459bb1SJeff Layton } 2755dd459bb1SJeff Layton 27563d8e560dSJeff Layton /* The i_flctx must be valid when calling into here */ 27578634b51fSJeff Layton static void 2758128a3785SDmitry Vyukov locks_remove_lease(struct file *filp, struct file_lock_context *ctx) 27598634b51fSJeff Layton { 27608634b51fSJeff Layton struct file_lock *fl, *tmp; 27618634b51fSJeff Layton LIST_HEAD(dispose); 27628634b51fSJeff Layton 27633d8e560dSJeff Layton if (list_empty(&ctx->flc_lease)) 27648634b51fSJeff Layton return; 27658634b51fSJeff Layton 276602e525b2SPeter Zijlstra percpu_down_read(&file_rwsem); 27676109c850SJeff Layton spin_lock(&ctx->flc_lock); 27688634b51fSJeff Layton list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list) 2769c4e136cdSJeff Layton if (filp == fl->fl_file) 27707448cc37SJeff Layton lease_modify(fl, F_UNLCK, &dispose); 27716109c850SJeff Layton spin_unlock(&ctx->flc_lock); 277202e525b2SPeter Zijlstra percpu_up_read(&file_rwsem); 27735f43086bSPeter Zijlstra 27748634b51fSJeff Layton locks_dispose_list(&dispose); 27758634b51fSJeff Layton } 27768634b51fSJeff Layton 27771da177e4SLinus Torvalds /* 27781da177e4SLinus Torvalds * This function is called on the last close of an open file. 27791da177e4SLinus Torvalds */ 278078ed8a13SJeff Layton void locks_remove_file(struct file *filp) 27811da177e4SLinus Torvalds { 2782128a3785SDmitry Vyukov struct file_lock_context *ctx; 2783128a3785SDmitry Vyukov 2784c568d683SMiklos Szeredi ctx = smp_load_acquire(&locks_inode(filp)->i_flctx); 2785128a3785SDmitry Vyukov if (!ctx) 27863d8e560dSJeff Layton return; 27873d8e560dSJeff Layton 2788dd459bb1SJeff Layton /* remove any OFD locks */ 278973a8f5f7SChristoph Hellwig locks_remove_posix(filp, filp); 27905d50ffd7SJeff Layton 2791dd459bb1SJeff Layton /* remove flock locks */ 2792128a3785SDmitry Vyukov locks_remove_flock(filp, ctx); 2793dd459bb1SJeff Layton 27948634b51fSJeff Layton /* remove any leases */ 2795128a3785SDmitry Vyukov locks_remove_lease(filp, ctx); 27963953704fSBenjamin Coddington 27973953704fSBenjamin Coddington spin_lock(&ctx->flc_lock); 27983953704fSBenjamin Coddington locks_check_ctx_file_list(filp, &ctx->flc_posix, "POSIX"); 27993953704fSBenjamin Coddington locks_check_ctx_file_list(filp, &ctx->flc_flock, "FLOCK"); 28003953704fSBenjamin Coddington locks_check_ctx_file_list(filp, &ctx->flc_lease, "LEASE"); 28013953704fSBenjamin Coddington spin_unlock(&ctx->flc_lock); 28021da177e4SLinus Torvalds } 28031da177e4SLinus Torvalds 28041da177e4SLinus Torvalds /** 28059b9d2ab4SMarc Eshel * vfs_cancel_lock - file byte range unblock lock 28069b9d2ab4SMarc Eshel * @filp: The file to apply the unblock to 28079b9d2ab4SMarc Eshel * @fl: The lock to be unblocked 28089b9d2ab4SMarc Eshel * 28099b9d2ab4SMarc Eshel * Used by lock managers to cancel blocked requests 28109b9d2ab4SMarc Eshel */ 28119b9d2ab4SMarc Eshel int vfs_cancel_lock(struct file *filp, struct file_lock *fl) 28129b9d2ab4SMarc Eshel { 2813de2a4a50SMiklos Szeredi if (filp->f_op->lock) 28149b9d2ab4SMarc Eshel return filp->f_op->lock(filp, F_CANCELLK, fl); 28159b9d2ab4SMarc Eshel return 0; 28169b9d2ab4SMarc Eshel } 28179b9d2ab4SMarc Eshel EXPORT_SYMBOL_GPL(vfs_cancel_lock); 28189b9d2ab4SMarc Eshel 28197f8ada98SPavel Emelyanov #ifdef CONFIG_PROC_FS 2820d8ba7a36SAlexey Dobriyan #include <linux/proc_fs.h> 28217f8ada98SPavel Emelyanov #include <linux/seq_file.h> 28227f8ada98SPavel Emelyanov 28237012b02aSJeff Layton struct locks_iterator { 28247012b02aSJeff Layton int li_cpu; 28257012b02aSJeff Layton loff_t li_pos; 28267012b02aSJeff Layton }; 28277012b02aSJeff Layton 28287f8ada98SPavel Emelyanov static void lock_get_status(struct seq_file *f, struct file_lock *fl, 282999dc8292SJerome Marchand loff_t id, char *pfx) 28301da177e4SLinus Torvalds { 28311da177e4SLinus Torvalds struct inode *inode = NULL; 2832ab1f1611SVitaliy Gusev unsigned int fl_pid; 28339d78edeaSAlexey Gladkov struct pid_namespace *proc_pidns = proc_pid_ns(file_inode(f->file)->i_sb); 2834d67fd44fSNikolay Borisov 28359d5b86acSBenjamin Coddington fl_pid = locks_translate_pid(fl, proc_pidns); 2836d67fd44fSNikolay Borisov /* 28371cf8e5deSKonstantin Khorenko * If lock owner is dead (and pid is freed) or not visible in current 28381cf8e5deSKonstantin Khorenko * pidns, zero is shown as a pid value. Check lock info from 28391cf8e5deSKonstantin Khorenko * init_pid_ns to get saved lock pid value. 2840d67fd44fSNikolay Borisov */ 28411da177e4SLinus Torvalds 28421da177e4SLinus Torvalds if (fl->fl_file != NULL) 2843c568d683SMiklos Szeredi inode = locks_inode(fl->fl_file); 28441da177e4SLinus Torvalds 284599dc8292SJerome Marchand seq_printf(f, "%lld:%s ", id, pfx); 28461da177e4SLinus Torvalds if (IS_POSIX(fl)) { 2847c918d42aSJeff Layton if (fl->fl_flags & FL_ACCESS) 28485315c26aSFabian Frederick seq_puts(f, "ACCESS"); 2849cff2fce5SJeff Layton else if (IS_OFDLCK(fl)) 28505315c26aSFabian Frederick seq_puts(f, "OFDLCK"); 2851c918d42aSJeff Layton else 28525315c26aSFabian Frederick seq_puts(f, "POSIX "); 2853c918d42aSJeff Layton 2854c918d42aSJeff Layton seq_printf(f, " %s ", 28551da177e4SLinus Torvalds (inode == NULL) ? "*NOINODE*" : 2856a16877caSPavel Emelyanov mandatory_lock(inode) ? "MANDATORY" : "ADVISORY "); 28571da177e4SLinus Torvalds } else if (IS_FLOCK(fl)) { 28581da177e4SLinus Torvalds if (fl->fl_type & LOCK_MAND) { 28595315c26aSFabian Frederick seq_puts(f, "FLOCK MSNFS "); 28601da177e4SLinus Torvalds } else { 28615315c26aSFabian Frederick seq_puts(f, "FLOCK ADVISORY "); 28621da177e4SLinus Torvalds } 28631da177e4SLinus Torvalds } else if (IS_LEASE(fl)) { 28648144f1f6SJeff Layton if (fl->fl_flags & FL_DELEG) 28658144f1f6SJeff Layton seq_puts(f, "DELEG "); 28668144f1f6SJeff Layton else 28675315c26aSFabian Frederick seq_puts(f, "LEASE "); 28688144f1f6SJeff Layton 2869ab83fa4bSJ. Bruce Fields if (lease_breaking(fl)) 28705315c26aSFabian Frederick seq_puts(f, "BREAKING "); 28711da177e4SLinus Torvalds else if (fl->fl_file) 28725315c26aSFabian Frederick seq_puts(f, "ACTIVE "); 28731da177e4SLinus Torvalds else 28745315c26aSFabian Frederick seq_puts(f, "BREAKER "); 28751da177e4SLinus Torvalds } else { 28765315c26aSFabian Frederick seq_puts(f, "UNKNOWN UNKNOWN "); 28771da177e4SLinus Torvalds } 28781da177e4SLinus Torvalds if (fl->fl_type & LOCK_MAND) { 28797f8ada98SPavel Emelyanov seq_printf(f, "%s ", 28801da177e4SLinus Torvalds (fl->fl_type & LOCK_READ) 28811da177e4SLinus Torvalds ? (fl->fl_type & LOCK_WRITE) ? "RW " : "READ " 28821da177e4SLinus Torvalds : (fl->fl_type & LOCK_WRITE) ? "WRITE" : "NONE "); 28831da177e4SLinus Torvalds } else { 288443e4cb94SPavel Begunkov int type = IS_LEASE(fl) ? target_leasetype(fl) : fl->fl_type; 288543e4cb94SPavel Begunkov 288643e4cb94SPavel Begunkov seq_printf(f, "%s ", (type == F_WRLCK) ? "WRITE" : 288743e4cb94SPavel Begunkov (type == F_RDLCK) ? "READ" : "UNLCK"); 28881da177e4SLinus Torvalds } 28891da177e4SLinus Torvalds if (inode) { 28903648888eSJeff Layton /* userspace relies on this representation of dev_t */ 289198ca480aSAmir Goldstein seq_printf(f, "%d %02x:%02x:%lu ", fl_pid, 28921da177e4SLinus Torvalds MAJOR(inode->i_sb->s_dev), 28931da177e4SLinus Torvalds MINOR(inode->i_sb->s_dev), inode->i_ino); 28941da177e4SLinus Torvalds } else { 2895ab1f1611SVitaliy Gusev seq_printf(f, "%d <none>:0 ", fl_pid); 28961da177e4SLinus Torvalds } 28971da177e4SLinus Torvalds if (IS_POSIX(fl)) { 28981da177e4SLinus Torvalds if (fl->fl_end == OFFSET_MAX) 28997f8ada98SPavel Emelyanov seq_printf(f, "%Ld EOF\n", fl->fl_start); 29001da177e4SLinus Torvalds else 29017f8ada98SPavel Emelyanov seq_printf(f, "%Ld %Ld\n", fl->fl_start, fl->fl_end); 29021da177e4SLinus Torvalds } else { 29035315c26aSFabian Frederick seq_puts(f, "0 EOF\n"); 29041da177e4SLinus Torvalds } 29051da177e4SLinus Torvalds } 29061da177e4SLinus Torvalds 29077f8ada98SPavel Emelyanov static int locks_show(struct seq_file *f, void *v) 29081da177e4SLinus Torvalds { 29097012b02aSJeff Layton struct locks_iterator *iter = f->private; 29107f8ada98SPavel Emelyanov struct file_lock *fl, *bfl; 29119d78edeaSAlexey Gladkov struct pid_namespace *proc_pidns = proc_pid_ns(file_inode(f->file)->i_sb); 29127f8ada98SPavel Emelyanov 2913139ca04eSJeff Layton fl = hlist_entry(v, struct file_lock, fl_link); 29147f8ada98SPavel Emelyanov 29159d5b86acSBenjamin Coddington if (locks_translate_pid(fl, proc_pidns) == 0) 2916d67fd44fSNikolay Borisov return 0; 2917d67fd44fSNikolay Borisov 29187012b02aSJeff Layton lock_get_status(f, fl, iter->li_pos, ""); 29197f8ada98SPavel Emelyanov 2920ada5c1daSNeilBrown list_for_each_entry(bfl, &fl->fl_blocked_requests, fl_blocked_member) 29217012b02aSJeff Layton lock_get_status(f, bfl, iter->li_pos, " ->"); 29227f8ada98SPavel Emelyanov 29237f8ada98SPavel Emelyanov return 0; 29241da177e4SLinus Torvalds } 29251da177e4SLinus Torvalds 29266c8c9031SAndrey Vagin static void __show_fd_locks(struct seq_file *f, 29276c8c9031SAndrey Vagin struct list_head *head, int *id, 29286c8c9031SAndrey Vagin struct file *filp, struct files_struct *files) 29296c8c9031SAndrey Vagin { 29306c8c9031SAndrey Vagin struct file_lock *fl; 29316c8c9031SAndrey Vagin 29326c8c9031SAndrey Vagin list_for_each_entry(fl, head, fl_list) { 29336c8c9031SAndrey Vagin 29346c8c9031SAndrey Vagin if (filp != fl->fl_file) 29356c8c9031SAndrey Vagin continue; 29366c8c9031SAndrey Vagin if (fl->fl_owner != files && 29376c8c9031SAndrey Vagin fl->fl_owner != filp) 29386c8c9031SAndrey Vagin continue; 29396c8c9031SAndrey Vagin 29406c8c9031SAndrey Vagin (*id)++; 29416c8c9031SAndrey Vagin seq_puts(f, "lock:\t"); 29426c8c9031SAndrey Vagin lock_get_status(f, fl, *id, ""); 29436c8c9031SAndrey Vagin } 29446c8c9031SAndrey Vagin } 29456c8c9031SAndrey Vagin 29466c8c9031SAndrey Vagin void show_fd_locks(struct seq_file *f, 29476c8c9031SAndrey Vagin struct file *filp, struct files_struct *files) 29486c8c9031SAndrey Vagin { 2949c568d683SMiklos Szeredi struct inode *inode = locks_inode(filp); 29506c8c9031SAndrey Vagin struct file_lock_context *ctx; 29516c8c9031SAndrey Vagin int id = 0; 29526c8c9031SAndrey Vagin 2953128a3785SDmitry Vyukov ctx = smp_load_acquire(&inode->i_flctx); 29546c8c9031SAndrey Vagin if (!ctx) 29556c8c9031SAndrey Vagin return; 29566c8c9031SAndrey Vagin 29576c8c9031SAndrey Vagin spin_lock(&ctx->flc_lock); 29586c8c9031SAndrey Vagin __show_fd_locks(f, &ctx->flc_flock, &id, filp, files); 29596c8c9031SAndrey Vagin __show_fd_locks(f, &ctx->flc_posix, &id, filp, files); 29606c8c9031SAndrey Vagin __show_fd_locks(f, &ctx->flc_lease, &id, filp, files); 29616c8c9031SAndrey Vagin spin_unlock(&ctx->flc_lock); 29626c8c9031SAndrey Vagin } 29636c8c9031SAndrey Vagin 29647f8ada98SPavel Emelyanov static void *locks_start(struct seq_file *f, loff_t *pos) 2965b03dfdecSJeff Layton __acquires(&blocked_lock_lock) 29661da177e4SLinus Torvalds { 29677012b02aSJeff Layton struct locks_iterator *iter = f->private; 296899dc8292SJerome Marchand 29697012b02aSJeff Layton iter->li_pos = *pos + 1; 2970aba37660SPeter Zijlstra percpu_down_write(&file_rwsem); 29717b2296afSJeff Layton spin_lock(&blocked_lock_lock); 29727c3f654dSPeter Zijlstra return seq_hlist_start_percpu(&file_lock_list.hlist, &iter->li_cpu, *pos); 29731da177e4SLinus Torvalds } 29747f8ada98SPavel Emelyanov 29757f8ada98SPavel Emelyanov static void *locks_next(struct seq_file *f, void *v, loff_t *pos) 29767f8ada98SPavel Emelyanov { 29777012b02aSJeff Layton struct locks_iterator *iter = f->private; 29787012b02aSJeff Layton 29797012b02aSJeff Layton ++iter->li_pos; 29807c3f654dSPeter Zijlstra return seq_hlist_next_percpu(v, &file_lock_list.hlist, &iter->li_cpu, pos); 29811da177e4SLinus Torvalds } 29827f8ada98SPavel Emelyanov 29837f8ada98SPavel Emelyanov static void locks_stop(struct seq_file *f, void *v) 2984b03dfdecSJeff Layton __releases(&blocked_lock_lock) 29857f8ada98SPavel Emelyanov { 29867b2296afSJeff Layton spin_unlock(&blocked_lock_lock); 2987aba37660SPeter Zijlstra percpu_up_write(&file_rwsem); 29881da177e4SLinus Torvalds } 29891da177e4SLinus Torvalds 2990d8ba7a36SAlexey Dobriyan static const struct seq_operations locks_seq_operations = { 29917f8ada98SPavel Emelyanov .start = locks_start, 29927f8ada98SPavel Emelyanov .next = locks_next, 29937f8ada98SPavel Emelyanov .stop = locks_stop, 29947f8ada98SPavel Emelyanov .show = locks_show, 29957f8ada98SPavel Emelyanov }; 2996d8ba7a36SAlexey Dobriyan 2997d8ba7a36SAlexey Dobriyan static int __init proc_locks_init(void) 2998d8ba7a36SAlexey Dobriyan { 299944414d82SChristoph Hellwig proc_create_seq_private("locks", 0, NULL, &locks_seq_operations, 300044414d82SChristoph Hellwig sizeof(struct locks_iterator), NULL); 3001d8ba7a36SAlexey Dobriyan return 0; 3002d8ba7a36SAlexey Dobriyan } 300391899226SPaul Gortmaker fs_initcall(proc_locks_init); 30047f8ada98SPavel Emelyanov #endif 30057f8ada98SPavel Emelyanov 30061da177e4SLinus Torvalds static int __init filelock_init(void) 30071da177e4SLinus Torvalds { 30087012b02aSJeff Layton int i; 30097012b02aSJeff Layton 30104a075e39SJeff Layton flctx_cache = kmem_cache_create("file_lock_ctx", 30114a075e39SJeff Layton sizeof(struct file_lock_context), 0, SLAB_PANIC, NULL); 30124a075e39SJeff Layton 30131da177e4SLinus Torvalds filelock_cache = kmem_cache_create("file_lock_cache", 3014ee19cc40SMiklos Szeredi sizeof(struct file_lock), 0, SLAB_PANIC, NULL); 3015ee19cc40SMiklos Szeredi 30167c3f654dSPeter Zijlstra for_each_possible_cpu(i) { 30177c3f654dSPeter Zijlstra struct file_lock_list_struct *fll = per_cpu_ptr(&file_lock_list, i); 30187c3f654dSPeter Zijlstra 30197c3f654dSPeter Zijlstra spin_lock_init(&fll->lock); 30207c3f654dSPeter Zijlstra INIT_HLIST_HEAD(&fll->hlist); 30217c3f654dSPeter Zijlstra } 30227012b02aSJeff Layton 302318f6622eSJeff Layton lease_notifier_chain_init(); 30241da177e4SLinus Torvalds return 0; 30251da177e4SLinus Torvalds } 30261da177e4SLinus Torvalds core_initcall(filelock_init); 3027