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. 64395cf969SPaul Bolle * See 'Documentation/filesystems/mandatory-locking.txt' 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 2151c8c601aSJeff Layton /* 21648f74186SJeff Layton * The blocked_hash is used to find POSIX lock loops for deadlock detection. 2177b2296afSJeff Layton * It is protected by blocked_lock_lock. 21848f74186SJeff Layton * 21948f74186SJeff Layton * We hash locks by lockowner in order to optimize searching for the lock a 22048f74186SJeff Layton * particular lockowner is waiting on. 22148f74186SJeff Layton * 22248f74186SJeff Layton * FIXME: make this value scale via some heuristic? We generally will want more 22348f74186SJeff Layton * buckets when we have more lockowners holding locks, but that's a little 22448f74186SJeff Layton * difficult to determine without knowing what the workload will look like. 2251c8c601aSJeff Layton */ 22648f74186SJeff Layton #define BLOCKED_HASH_BITS 7 22748f74186SJeff Layton static DEFINE_HASHTABLE(blocked_hash, BLOCKED_HASH_BITS); 22888974691SJeff Layton 2291c8c601aSJeff Layton /* 2307b2296afSJeff Layton * This lock protects the blocked_hash. Generally, if you're accessing it, you 2317b2296afSJeff Layton * want to be holding this lock. 2321c8c601aSJeff Layton * 233ada5c1daSNeilBrown * In addition, it also protects the fl->fl_blocked_requests list, and the 234ada5c1daSNeilBrown * fl->fl_blocker pointer for file_lock structures that are acting as lock 235ada5c1daSNeilBrown * requests (in contrast to those that are acting as records of acquired locks). 2361c8c601aSJeff Layton * 2371c8c601aSJeff Layton * Note that when we acquire this lock in order to change the above fields, 2386109c850SJeff Layton * we often hold the flc_lock as well. In certain cases, when reading the fields 2391c8c601aSJeff Layton * protected by this lock, we can skip acquiring it iff we already hold the 2406109c850SJeff Layton * flc_lock. 2411c8c601aSJeff Layton */ 2427b2296afSJeff Layton static DEFINE_SPINLOCK(blocked_lock_lock); 2431da177e4SLinus Torvalds 2444a075e39SJeff Layton static struct kmem_cache *flctx_cache __read_mostly; 245e18b890bSChristoph Lameter static struct kmem_cache *filelock_cache __read_mostly; 2461da177e4SLinus Torvalds 2474a075e39SJeff Layton static struct file_lock_context * 2485c1c669aSJeff Layton locks_get_lock_context(struct inode *inode, int type) 2494a075e39SJeff Layton { 250128a3785SDmitry Vyukov struct file_lock_context *ctx; 2514a075e39SJeff Layton 252128a3785SDmitry Vyukov /* paired with cmpxchg() below */ 253128a3785SDmitry Vyukov ctx = smp_load_acquire(&inode->i_flctx); 254128a3785SDmitry Vyukov if (likely(ctx) || type == F_UNLCK) 2554a075e39SJeff Layton goto out; 2564a075e39SJeff Layton 257128a3785SDmitry Vyukov ctx = kmem_cache_alloc(flctx_cache, GFP_KERNEL); 258128a3785SDmitry Vyukov if (!ctx) 2594a075e39SJeff Layton goto out; 2604a075e39SJeff Layton 261128a3785SDmitry Vyukov spin_lock_init(&ctx->flc_lock); 262128a3785SDmitry Vyukov INIT_LIST_HEAD(&ctx->flc_flock); 263128a3785SDmitry Vyukov INIT_LIST_HEAD(&ctx->flc_posix); 264128a3785SDmitry Vyukov INIT_LIST_HEAD(&ctx->flc_lease); 2654a075e39SJeff Layton 2664a075e39SJeff Layton /* 2674a075e39SJeff Layton * Assign the pointer if it's not already assigned. If it is, then 2684a075e39SJeff Layton * free the context we just allocated. 2694a075e39SJeff Layton */ 270128a3785SDmitry Vyukov if (cmpxchg(&inode->i_flctx, NULL, ctx)) { 271128a3785SDmitry Vyukov kmem_cache_free(flctx_cache, ctx); 272128a3785SDmitry Vyukov ctx = smp_load_acquire(&inode->i_flctx); 273128a3785SDmitry Vyukov } 2744a075e39SJeff Layton out: 2751890910fSJeff Layton trace_locks_get_lock_context(inode, type, ctx); 276128a3785SDmitry Vyukov return ctx; 2774a075e39SJeff Layton } 2784a075e39SJeff Layton 279e24dadabSJeff Layton static void 280e24dadabSJeff Layton locks_dump_ctx_list(struct list_head *list, char *list_type) 281e24dadabSJeff Layton { 282e24dadabSJeff Layton struct file_lock *fl; 283e24dadabSJeff Layton 284e24dadabSJeff Layton list_for_each_entry(fl, list, fl_list) { 285e24dadabSJeff 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); 286e24dadabSJeff Layton } 287e24dadabSJeff Layton } 288e24dadabSJeff Layton 289e24dadabSJeff Layton static void 290e24dadabSJeff Layton locks_check_ctx_lists(struct inode *inode) 291e24dadabSJeff Layton { 292e24dadabSJeff Layton struct file_lock_context *ctx = inode->i_flctx; 293e24dadabSJeff Layton 294e24dadabSJeff Layton if (unlikely(!list_empty(&ctx->flc_flock) || 295e24dadabSJeff Layton !list_empty(&ctx->flc_posix) || 296e24dadabSJeff Layton !list_empty(&ctx->flc_lease))) { 297e24dadabSJeff Layton pr_warn("Leaked locks on dev=0x%x:0x%x ino=0x%lx:\n", 298e24dadabSJeff Layton MAJOR(inode->i_sb->s_dev), MINOR(inode->i_sb->s_dev), 299e24dadabSJeff Layton inode->i_ino); 300e24dadabSJeff Layton locks_dump_ctx_list(&ctx->flc_flock, "FLOCK"); 301e24dadabSJeff Layton locks_dump_ctx_list(&ctx->flc_posix, "POSIX"); 302e24dadabSJeff Layton locks_dump_ctx_list(&ctx->flc_lease, "LEASE"); 303e24dadabSJeff Layton } 304e24dadabSJeff Layton } 305e24dadabSJeff Layton 3063953704fSBenjamin Coddington static void 3073953704fSBenjamin Coddington locks_check_ctx_file_list(struct file *filp, struct list_head *list, 3083953704fSBenjamin Coddington char *list_type) 3093953704fSBenjamin Coddington { 3103953704fSBenjamin Coddington struct file_lock *fl; 3113953704fSBenjamin Coddington struct inode *inode = locks_inode(filp); 3123953704fSBenjamin Coddington 3133953704fSBenjamin Coddington list_for_each_entry(fl, list, fl_list) 3143953704fSBenjamin Coddington if (fl->fl_file == filp) 3153953704fSBenjamin Coddington pr_warn("Leaked %s lock on dev=0x%x:0x%x ino=0x%lx " 3163953704fSBenjamin Coddington " fl_owner=%p fl_flags=0x%x fl_type=0x%x fl_pid=%u\n", 3173953704fSBenjamin Coddington list_type, MAJOR(inode->i_sb->s_dev), 3183953704fSBenjamin Coddington MINOR(inode->i_sb->s_dev), inode->i_ino, 3193953704fSBenjamin Coddington fl->fl_owner, fl->fl_flags, fl->fl_type, fl->fl_pid); 3203953704fSBenjamin Coddington } 3213953704fSBenjamin Coddington 3224a075e39SJeff Layton void 323f27a0fe0SJeff Layton locks_free_lock_context(struct inode *inode) 3244a075e39SJeff Layton { 325f27a0fe0SJeff Layton struct file_lock_context *ctx = inode->i_flctx; 326f27a0fe0SJeff Layton 327e24dadabSJeff Layton if (unlikely(ctx)) { 328e24dadabSJeff Layton locks_check_ctx_lists(inode); 3294a075e39SJeff Layton kmem_cache_free(flctx_cache, ctx); 3304a075e39SJeff Layton } 3314a075e39SJeff Layton } 3324a075e39SJeff Layton 333ee19cc40SMiklos Szeredi static void locks_init_lock_heads(struct file_lock *fl) 334a51cb91dSMiklos Szeredi { 335139ca04eSJeff Layton INIT_HLIST_NODE(&fl->fl_link); 3366dee60f6SJeff Layton INIT_LIST_HEAD(&fl->fl_list); 337ada5c1daSNeilBrown INIT_LIST_HEAD(&fl->fl_blocked_requests); 338ada5c1daSNeilBrown INIT_LIST_HEAD(&fl->fl_blocked_member); 339ee19cc40SMiklos Szeredi init_waitqueue_head(&fl->fl_wait); 340a51cb91dSMiklos Szeredi } 341a51cb91dSMiklos Szeredi 3421da177e4SLinus Torvalds /* Allocate an empty lock structure. */ 343c5b1f0d9SArnd Bergmann struct file_lock *locks_alloc_lock(void) 3441da177e4SLinus Torvalds { 345ee19cc40SMiklos Szeredi struct file_lock *fl = kmem_cache_zalloc(filelock_cache, GFP_KERNEL); 346a51cb91dSMiklos Szeredi 347a51cb91dSMiklos Szeredi if (fl) 348ee19cc40SMiklos Szeredi locks_init_lock_heads(fl); 349a51cb91dSMiklos Szeredi 350a51cb91dSMiklos Szeredi return fl; 3511da177e4SLinus Torvalds } 352c5b1f0d9SArnd Bergmann EXPORT_SYMBOL_GPL(locks_alloc_lock); 3531da177e4SLinus Torvalds 354a9e61e25SFelix Blyakher void locks_release_private(struct file_lock *fl) 35547831f35STrond Myklebust { 3565926459eSNeilBrown BUG_ON(waitqueue_active(&fl->fl_wait)); 3575926459eSNeilBrown BUG_ON(!list_empty(&fl->fl_list)); 3585926459eSNeilBrown BUG_ON(!list_empty(&fl->fl_blocked_requests)); 3595926459eSNeilBrown BUG_ON(!list_empty(&fl->fl_blocked_member)); 3605926459eSNeilBrown BUG_ON(!hlist_unhashed(&fl->fl_link)); 3615926459eSNeilBrown 36247831f35STrond Myklebust if (fl->fl_ops) { 36347831f35STrond Myklebust if (fl->fl_ops->fl_release_private) 36447831f35STrond Myklebust fl->fl_ops->fl_release_private(fl); 36547831f35STrond Myklebust fl->fl_ops = NULL; 36647831f35STrond Myklebust } 36747831f35STrond Myklebust 3685c97d7b1SKinglong Mee if (fl->fl_lmops) { 369cae80b30SJeff Layton if (fl->fl_lmops->lm_put_owner) { 370cae80b30SJeff Layton fl->fl_lmops->lm_put_owner(fl->fl_owner); 371cae80b30SJeff Layton fl->fl_owner = NULL; 372cae80b30SJeff Layton } 3735c97d7b1SKinglong Mee fl->fl_lmops = NULL; 3745c97d7b1SKinglong Mee } 37547831f35STrond Myklebust } 376a9e61e25SFelix Blyakher EXPORT_SYMBOL_GPL(locks_release_private); 37747831f35STrond Myklebust 3781da177e4SLinus Torvalds /* Free a lock which is not in use. */ 37905fa3135SJ. Bruce Fields void locks_free_lock(struct file_lock *fl) 3801da177e4SLinus Torvalds { 38147831f35STrond Myklebust locks_release_private(fl); 3821da177e4SLinus Torvalds kmem_cache_free(filelock_cache, fl); 3831da177e4SLinus Torvalds } 38405fa3135SJ. Bruce Fields EXPORT_SYMBOL(locks_free_lock); 3851da177e4SLinus Torvalds 386ed9814d8SJeff Layton static void 387ed9814d8SJeff Layton locks_dispose_list(struct list_head *dispose) 388ed9814d8SJeff Layton { 389ed9814d8SJeff Layton struct file_lock *fl; 390ed9814d8SJeff Layton 391ed9814d8SJeff Layton while (!list_empty(dispose)) { 3926dee60f6SJeff Layton fl = list_first_entry(dispose, struct file_lock, fl_list); 3936dee60f6SJeff Layton list_del_init(&fl->fl_list); 394ed9814d8SJeff Layton locks_free_lock(fl); 395ed9814d8SJeff Layton } 396ed9814d8SJeff Layton } 397ed9814d8SJeff Layton 3981da177e4SLinus Torvalds void locks_init_lock(struct file_lock *fl) 3991da177e4SLinus Torvalds { 400ee19cc40SMiklos Szeredi memset(fl, 0, sizeof(struct file_lock)); 401ee19cc40SMiklos Szeredi locks_init_lock_heads(fl); 4021da177e4SLinus Torvalds } 4031da177e4SLinus Torvalds EXPORT_SYMBOL(locks_init_lock); 4041da177e4SLinus Torvalds 4051da177e4SLinus Torvalds /* 4061da177e4SLinus Torvalds * Initialize a new lock from an existing file_lock structure. 4071da177e4SLinus Torvalds */ 4083fe0fff1SKinglong Mee void locks_copy_conflock(struct file_lock *new, struct file_lock *fl) 4091da177e4SLinus Torvalds { 4101da177e4SLinus Torvalds new->fl_owner = fl->fl_owner; 4111da177e4SLinus Torvalds new->fl_pid = fl->fl_pid; 4120996905fSTrond Myklebust new->fl_file = NULL; 4131da177e4SLinus Torvalds new->fl_flags = fl->fl_flags; 4141da177e4SLinus Torvalds new->fl_type = fl->fl_type; 4151da177e4SLinus Torvalds new->fl_start = fl->fl_start; 4161da177e4SLinus Torvalds new->fl_end = fl->fl_end; 417f328296eSKinglong Mee new->fl_lmops = fl->fl_lmops; 4180996905fSTrond Myklebust new->fl_ops = NULL; 419f328296eSKinglong Mee 420f328296eSKinglong Mee if (fl->fl_lmops) { 421f328296eSKinglong Mee if (fl->fl_lmops->lm_get_owner) 422cae80b30SJeff Layton fl->fl_lmops->lm_get_owner(fl->fl_owner); 423f328296eSKinglong Mee } 4240996905fSTrond Myklebust } 4253fe0fff1SKinglong Mee EXPORT_SYMBOL(locks_copy_conflock); 4260996905fSTrond Myklebust 4270996905fSTrond Myklebust void locks_copy_lock(struct file_lock *new, struct file_lock *fl) 4280996905fSTrond Myklebust { 429566709bdSJeff Layton /* "new" must be a freshly-initialized lock */ 430566709bdSJeff Layton WARN_ON_ONCE(new->fl_ops); 4310996905fSTrond Myklebust 4323fe0fff1SKinglong Mee locks_copy_conflock(new, fl); 433f328296eSKinglong Mee 4340996905fSTrond Myklebust new->fl_file = fl->fl_file; 4351da177e4SLinus Torvalds new->fl_ops = fl->fl_ops; 43647831f35STrond Myklebust 437f328296eSKinglong Mee if (fl->fl_ops) { 438f328296eSKinglong Mee if (fl->fl_ops->fl_copy_lock) 439f328296eSKinglong Mee fl->fl_ops->fl_copy_lock(new, fl); 440f328296eSKinglong Mee } 4411da177e4SLinus Torvalds } 4421da177e4SLinus Torvalds EXPORT_SYMBOL(locks_copy_lock); 4431da177e4SLinus Torvalds 4445946c431SNeilBrown static void locks_move_blocks(struct file_lock *new, struct file_lock *fl) 4455946c431SNeilBrown { 4465946c431SNeilBrown struct file_lock *f; 4475946c431SNeilBrown 4485946c431SNeilBrown /* 4495946c431SNeilBrown * As ctx->flc_lock is held, new requests cannot be added to 4505946c431SNeilBrown * ->fl_blocked_requests, so we don't need a lock to check if it 4515946c431SNeilBrown * is empty. 4525946c431SNeilBrown */ 4535946c431SNeilBrown if (list_empty(&fl->fl_blocked_requests)) 4545946c431SNeilBrown return; 4555946c431SNeilBrown spin_lock(&blocked_lock_lock); 4565946c431SNeilBrown list_splice_init(&fl->fl_blocked_requests, &new->fl_blocked_requests); 457bf77ae4cSNeilBrown list_for_each_entry(f, &new->fl_blocked_requests, fl_blocked_member) 4585946c431SNeilBrown f->fl_blocker = new; 4595946c431SNeilBrown spin_unlock(&blocked_lock_lock); 4605946c431SNeilBrown } 4615946c431SNeilBrown 4621da177e4SLinus Torvalds static inline int flock_translate_cmd(int cmd) { 4631da177e4SLinus Torvalds if (cmd & LOCK_MAND) 4641da177e4SLinus Torvalds return cmd & (LOCK_MAND | LOCK_RW); 4651da177e4SLinus Torvalds switch (cmd) { 4661da177e4SLinus Torvalds case LOCK_SH: 4671da177e4SLinus Torvalds return F_RDLCK; 4681da177e4SLinus Torvalds case LOCK_EX: 4691da177e4SLinus Torvalds return F_WRLCK; 4701da177e4SLinus Torvalds case LOCK_UN: 4711da177e4SLinus Torvalds return F_UNLCK; 4721da177e4SLinus Torvalds } 4731da177e4SLinus Torvalds return -EINVAL; 4741da177e4SLinus Torvalds } 4751da177e4SLinus Torvalds 4761da177e4SLinus Torvalds /* Fill in a file_lock structure with an appropriate FLOCK lock. */ 4776e129d00SJeff Layton static struct file_lock * 478d6367d62SNeilBrown flock_make_lock(struct file *filp, unsigned int cmd, struct file_lock *fl) 4791da177e4SLinus Torvalds { 4801da177e4SLinus Torvalds int type = flock_translate_cmd(cmd); 4816e129d00SJeff Layton 4821da177e4SLinus Torvalds if (type < 0) 4836e129d00SJeff Layton return ERR_PTR(type); 4841da177e4SLinus Torvalds 485d6367d62SNeilBrown if (fl == NULL) { 4861da177e4SLinus Torvalds fl = locks_alloc_lock(); 4871da177e4SLinus Torvalds if (fl == NULL) 4886e129d00SJeff Layton return ERR_PTR(-ENOMEM); 489d6367d62SNeilBrown } else { 490d6367d62SNeilBrown locks_init_lock(fl); 491d6367d62SNeilBrown } 4921da177e4SLinus Torvalds 4931da177e4SLinus Torvalds fl->fl_file = filp; 49473a8f5f7SChristoph Hellwig fl->fl_owner = filp; 4951da177e4SLinus Torvalds fl->fl_pid = current->tgid; 4961da177e4SLinus Torvalds fl->fl_flags = FL_FLOCK; 4971da177e4SLinus Torvalds fl->fl_type = type; 4981da177e4SLinus Torvalds fl->fl_end = OFFSET_MAX; 4991da177e4SLinus Torvalds 5006e129d00SJeff Layton return fl; 5011da177e4SLinus Torvalds } 5021da177e4SLinus Torvalds 5030ec4f431SJ. Bruce Fields static int assign_type(struct file_lock *fl, long type) 5041da177e4SLinus Torvalds { 5051da177e4SLinus Torvalds switch (type) { 5061da177e4SLinus Torvalds case F_RDLCK: 5071da177e4SLinus Torvalds case F_WRLCK: 5081da177e4SLinus Torvalds case F_UNLCK: 5091da177e4SLinus Torvalds fl->fl_type = type; 5101da177e4SLinus Torvalds break; 5111da177e4SLinus Torvalds default: 5121da177e4SLinus Torvalds return -EINVAL; 5131da177e4SLinus Torvalds } 5141da177e4SLinus Torvalds return 0; 5151da177e4SLinus Torvalds } 5161da177e4SLinus Torvalds 517ef12e72aSJ. Bruce Fields static int flock64_to_posix_lock(struct file *filp, struct file_lock *fl, 518ef12e72aSJ. Bruce Fields struct flock64 *l) 519ef12e72aSJ. Bruce Fields { 520ef12e72aSJ. Bruce Fields switch (l->l_whence) { 521ef12e72aSJ. Bruce Fields case SEEK_SET: 522ef12e72aSJ. Bruce Fields fl->fl_start = 0; 523ef12e72aSJ. Bruce Fields break; 524ef12e72aSJ. Bruce Fields case SEEK_CUR: 525ef12e72aSJ. Bruce Fields fl->fl_start = filp->f_pos; 526ef12e72aSJ. Bruce Fields break; 527ef12e72aSJ. Bruce Fields case SEEK_END: 528ef12e72aSJ. Bruce Fields fl->fl_start = i_size_read(file_inode(filp)); 529ef12e72aSJ. Bruce Fields break; 530ef12e72aSJ. Bruce Fields default: 531ef12e72aSJ. Bruce Fields return -EINVAL; 532ef12e72aSJ. Bruce Fields } 533ef12e72aSJ. Bruce Fields if (l->l_start > OFFSET_MAX - fl->fl_start) 534ef12e72aSJ. Bruce Fields return -EOVERFLOW; 535ef12e72aSJ. Bruce Fields fl->fl_start += l->l_start; 536ef12e72aSJ. Bruce Fields if (fl->fl_start < 0) 537ef12e72aSJ. Bruce Fields return -EINVAL; 538ef12e72aSJ. Bruce Fields 539ef12e72aSJ. Bruce Fields /* POSIX-1996 leaves the case l->l_len < 0 undefined; 540ef12e72aSJ. Bruce Fields POSIX-2001 defines it. */ 541ef12e72aSJ. Bruce Fields if (l->l_len > 0) { 542ef12e72aSJ. Bruce Fields if (l->l_len - 1 > OFFSET_MAX - fl->fl_start) 543ef12e72aSJ. Bruce Fields return -EOVERFLOW; 544ef12e72aSJ. Bruce Fields fl->fl_end = fl->fl_start + l->l_len - 1; 545ef12e72aSJ. Bruce Fields 546ef12e72aSJ. Bruce Fields } else if (l->l_len < 0) { 547ef12e72aSJ. Bruce Fields if (fl->fl_start + l->l_len < 0) 548ef12e72aSJ. Bruce Fields return -EINVAL; 549ef12e72aSJ. Bruce Fields fl->fl_end = fl->fl_start - 1; 550ef12e72aSJ. Bruce Fields fl->fl_start += l->l_len; 551ef12e72aSJ. Bruce Fields } else 552ef12e72aSJ. Bruce Fields fl->fl_end = OFFSET_MAX; 553ef12e72aSJ. Bruce Fields 554ef12e72aSJ. Bruce Fields fl->fl_owner = current->files; 555ef12e72aSJ. Bruce Fields fl->fl_pid = current->tgid; 556ef12e72aSJ. Bruce Fields fl->fl_file = filp; 557ef12e72aSJ. Bruce Fields fl->fl_flags = FL_POSIX; 558ef12e72aSJ. Bruce Fields fl->fl_ops = NULL; 559ef12e72aSJ. Bruce Fields fl->fl_lmops = NULL; 560ef12e72aSJ. Bruce Fields 561ef12e72aSJ. Bruce Fields return assign_type(fl, l->l_type); 562ef12e72aSJ. Bruce Fields } 563ef12e72aSJ. Bruce Fields 5641da177e4SLinus Torvalds /* Verify a "struct flock" and copy it to a "struct file_lock" as a POSIX 5651da177e4SLinus Torvalds * style lock. 5661da177e4SLinus Torvalds */ 5671da177e4SLinus Torvalds static int flock_to_posix_lock(struct file *filp, struct file_lock *fl, 5681da177e4SLinus Torvalds struct flock *l) 5691da177e4SLinus Torvalds { 570ef12e72aSJ. Bruce Fields struct flock64 ll = { 571ef12e72aSJ. Bruce Fields .l_type = l->l_type, 572ef12e72aSJ. Bruce Fields .l_whence = l->l_whence, 573ef12e72aSJ. Bruce Fields .l_start = l->l_start, 574ef12e72aSJ. Bruce Fields .l_len = l->l_len, 575ef12e72aSJ. Bruce Fields }; 5761da177e4SLinus Torvalds 577ef12e72aSJ. Bruce Fields return flock64_to_posix_lock(filp, fl, &ll); 5781da177e4SLinus Torvalds } 5791da177e4SLinus Torvalds 5801da177e4SLinus Torvalds /* default lease lock manager operations */ 5814d01b7f5SJeff Layton static bool 5824d01b7f5SJeff Layton lease_break_callback(struct file_lock *fl) 5831da177e4SLinus Torvalds { 5841da177e4SLinus Torvalds kill_fasync(&fl->fl_fasync, SIGIO, POLL_MSG); 5854d01b7f5SJeff Layton return false; 5861da177e4SLinus Torvalds } 5871da177e4SLinus Torvalds 5881c7dd2ffSJeff Layton static void 5891c7dd2ffSJeff Layton lease_setup(struct file_lock *fl, void **priv) 5901c7dd2ffSJeff Layton { 5911c7dd2ffSJeff Layton struct file *filp = fl->fl_file; 5921c7dd2ffSJeff Layton struct fasync_struct *fa = *priv; 5931c7dd2ffSJeff Layton 5941c7dd2ffSJeff Layton /* 5951c7dd2ffSJeff Layton * fasync_insert_entry() returns the old entry if any. If there was no 5961c7dd2ffSJeff Layton * old entry, then it used "priv" and inserted it into the fasync list. 5971c7dd2ffSJeff Layton * Clear the pointer to indicate that it shouldn't be freed. 5981c7dd2ffSJeff Layton */ 5991c7dd2ffSJeff Layton if (!fasync_insert_entry(fa->fa_fd, filp, &fl->fl_fasync, fa)) 6001c7dd2ffSJeff Layton *priv = NULL; 6011c7dd2ffSJeff Layton 60201919134SEric W. Biederman __f_setown(filp, task_pid(current), PIDTYPE_TGID, 0); 6031c7dd2ffSJeff Layton } 6041c7dd2ffSJeff Layton 6057b021967SAlexey Dobriyan static const struct lock_manager_operations lease_manager_ops = { 6068fb47a4fSJ. Bruce Fields .lm_break = lease_break_callback, 6078fb47a4fSJ. Bruce Fields .lm_change = lease_modify, 6081c7dd2ffSJeff Layton .lm_setup = lease_setup, 6091da177e4SLinus Torvalds }; 6101da177e4SLinus Torvalds 6111da177e4SLinus Torvalds /* 6121da177e4SLinus Torvalds * Initialize a lease, use the default lock manager operations 6131da177e4SLinus Torvalds */ 6140ec4f431SJ. Bruce Fields static int lease_init(struct file *filp, long type, struct file_lock *fl) 6151da177e4SLinus Torvalds { 61675dff55aSTrond Myklebust if (assign_type(fl, type) != 0) 61775dff55aSTrond Myklebust return -EINVAL; 61875dff55aSTrond Myklebust 6197ca76311SJeff Layton fl->fl_owner = filp; 6201da177e4SLinus Torvalds fl->fl_pid = current->tgid; 6211da177e4SLinus Torvalds 6221da177e4SLinus Torvalds fl->fl_file = filp; 6231da177e4SLinus Torvalds fl->fl_flags = FL_LEASE; 6241da177e4SLinus Torvalds fl->fl_start = 0; 6251da177e4SLinus Torvalds fl->fl_end = OFFSET_MAX; 6261da177e4SLinus Torvalds fl->fl_ops = NULL; 6271da177e4SLinus Torvalds fl->fl_lmops = &lease_manager_ops; 6281da177e4SLinus Torvalds return 0; 6291da177e4SLinus Torvalds } 6301da177e4SLinus Torvalds 6311da177e4SLinus Torvalds /* Allocate a file_lock initialised to this type of lease */ 6320ec4f431SJ. Bruce Fields static struct file_lock *lease_alloc(struct file *filp, long type) 6331da177e4SLinus Torvalds { 6341da177e4SLinus Torvalds struct file_lock *fl = locks_alloc_lock(); 63575dff55aSTrond Myklebust int error = -ENOMEM; 6361da177e4SLinus Torvalds 6371da177e4SLinus Torvalds if (fl == NULL) 638e32b8ee2SJ. Bruce Fields return ERR_PTR(error); 6391da177e4SLinus Torvalds 6401da177e4SLinus Torvalds error = lease_init(filp, type, fl); 64175dff55aSTrond Myklebust if (error) { 64275dff55aSTrond Myklebust locks_free_lock(fl); 643e32b8ee2SJ. Bruce Fields return ERR_PTR(error); 64475dff55aSTrond Myklebust } 645e32b8ee2SJ. Bruce Fields return fl; 6461da177e4SLinus Torvalds } 6471da177e4SLinus Torvalds 6481da177e4SLinus Torvalds /* Check if two locks overlap each other. 6491da177e4SLinus Torvalds */ 6501da177e4SLinus Torvalds static inline int locks_overlap(struct file_lock *fl1, struct file_lock *fl2) 6511da177e4SLinus Torvalds { 6521da177e4SLinus Torvalds return ((fl1->fl_end >= fl2->fl_start) && 6531da177e4SLinus Torvalds (fl2->fl_end >= fl1->fl_start)); 6541da177e4SLinus Torvalds } 6551da177e4SLinus Torvalds 6561da177e4SLinus Torvalds /* 6571da177e4SLinus Torvalds * Check whether two locks have the same owner. 6581da177e4SLinus Torvalds */ 65933443c42SMatt Mackall static int posix_same_owner(struct file_lock *fl1, struct file_lock *fl2) 6601da177e4SLinus Torvalds { 6611da177e4SLinus Torvalds return fl1->fl_owner == fl2->fl_owner; 6621da177e4SLinus Torvalds } 6631da177e4SLinus Torvalds 6646109c850SJeff Layton /* Must be called with the flc_lock held! */ 6656ca10ed8SJeff Layton static void locks_insert_global_locks(struct file_lock *fl) 66688974691SJeff Layton { 6677c3f654dSPeter Zijlstra struct file_lock_list_struct *fll = this_cpu_ptr(&file_lock_list); 6687c3f654dSPeter Zijlstra 669aba37660SPeter Zijlstra percpu_rwsem_assert_held(&file_rwsem); 670aba37660SPeter Zijlstra 6717c3f654dSPeter Zijlstra spin_lock(&fll->lock); 6727012b02aSJeff Layton fl->fl_link_cpu = smp_processor_id(); 6737c3f654dSPeter Zijlstra hlist_add_head(&fl->fl_link, &fll->hlist); 6747c3f654dSPeter Zijlstra spin_unlock(&fll->lock); 67588974691SJeff Layton } 67688974691SJeff Layton 6776109c850SJeff Layton /* Must be called with the flc_lock held! */ 6786ca10ed8SJeff Layton static void locks_delete_global_locks(struct file_lock *fl) 67988974691SJeff Layton { 6807c3f654dSPeter Zijlstra struct file_lock_list_struct *fll; 6817c3f654dSPeter Zijlstra 682aba37660SPeter Zijlstra percpu_rwsem_assert_held(&file_rwsem); 683aba37660SPeter Zijlstra 6847012b02aSJeff Layton /* 6857012b02aSJeff Layton * Avoid taking lock if already unhashed. This is safe since this check 6866109c850SJeff Layton * is done while holding the flc_lock, and new insertions into the list 6877012b02aSJeff Layton * also require that it be held. 6887012b02aSJeff Layton */ 6897012b02aSJeff Layton if (hlist_unhashed(&fl->fl_link)) 6907012b02aSJeff Layton return; 6917c3f654dSPeter Zijlstra 6927c3f654dSPeter Zijlstra fll = per_cpu_ptr(&file_lock_list, fl->fl_link_cpu); 6937c3f654dSPeter Zijlstra spin_lock(&fll->lock); 694139ca04eSJeff Layton hlist_del_init(&fl->fl_link); 6957c3f654dSPeter Zijlstra spin_unlock(&fll->lock); 69688974691SJeff Layton } 69788974691SJeff Layton 6983999e493SJeff Layton static unsigned long 6993999e493SJeff Layton posix_owner_key(struct file_lock *fl) 7003999e493SJeff Layton { 7013999e493SJeff Layton return (unsigned long)fl->fl_owner; 7023999e493SJeff Layton } 7033999e493SJeff Layton 7046ca10ed8SJeff Layton static void locks_insert_global_blocked(struct file_lock *waiter) 70588974691SJeff Layton { 706663d5af7SDaniel Wagner lockdep_assert_held(&blocked_lock_lock); 707663d5af7SDaniel Wagner 7083999e493SJeff Layton hash_add(blocked_hash, &waiter->fl_link, posix_owner_key(waiter)); 70988974691SJeff Layton } 71088974691SJeff Layton 7116ca10ed8SJeff Layton static void locks_delete_global_blocked(struct file_lock *waiter) 71288974691SJeff Layton { 713663d5af7SDaniel Wagner lockdep_assert_held(&blocked_lock_lock); 714663d5af7SDaniel Wagner 71548f74186SJeff Layton hash_del(&waiter->fl_link); 71688974691SJeff Layton } 71788974691SJeff Layton 7181da177e4SLinus Torvalds /* Remove waiter from blocker's block list. 7191da177e4SLinus Torvalds * When blocker ends up pointing to itself then the list is empty. 7201c8c601aSJeff Layton * 7217b2296afSJeff Layton * Must be called with blocked_lock_lock held. 7221da177e4SLinus Torvalds */ 72333443c42SMatt Mackall static void __locks_delete_block(struct file_lock *waiter) 7241da177e4SLinus Torvalds { 72588974691SJeff Layton locks_delete_global_blocked(waiter); 726ada5c1daSNeilBrown list_del_init(&waiter->fl_blocked_member); 727ada5c1daSNeilBrown waiter->fl_blocker = NULL; 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); 742ad6bbd8bSNeilBrown } 743ad6bbd8bSNeilBrown } 744ad6bbd8bSNeilBrown 745cb03f94fSNeilBrown /** 746cb03f94fSNeilBrown * locks_delete_lock - stop waiting for a file lock 747cb03f94fSNeilBrown * @waiter: the lock which was waiting 748cb03f94fSNeilBrown * 749cb03f94fSNeilBrown * lockd/nfsd need to disconnect the lock while working on it. 750cb03f94fSNeilBrown */ 751cb03f94fSNeilBrown int locks_delete_block(struct file_lock *waiter) 7521da177e4SLinus Torvalds { 753cb03f94fSNeilBrown int status = -ENOENT; 754cb03f94fSNeilBrown 75516306a61SNeilBrown /* 75616306a61SNeilBrown * If fl_blocker is NULL, it won't be set again as this thread 75716306a61SNeilBrown * "owns" the lock and is the only one that might try to claim 75816306a61SNeilBrown * the lock. So it is safe to test fl_blocker locklessly. 75916306a61SNeilBrown * Also if fl_blocker is NULL, this waiter is not listed on 76016306a61SNeilBrown * fl_blocked_requests for some lock, so no other request can 76116306a61SNeilBrown * be added to the list of fl_blocked_requests for this 76216306a61SNeilBrown * request. So if fl_blocker is NULL, it is safe to 76316306a61SNeilBrown * locklessly check if fl_blocked_requests is empty. If both 76416306a61SNeilBrown * of these checks succeed, there is no need to take the lock. 76516306a61SNeilBrown */ 76616306a61SNeilBrown if (waiter->fl_blocker == NULL && 76716306a61SNeilBrown list_empty(&waiter->fl_blocked_requests)) 768cb03f94fSNeilBrown return status; 7697b2296afSJeff Layton spin_lock(&blocked_lock_lock); 770cb03f94fSNeilBrown if (waiter->fl_blocker) 771cb03f94fSNeilBrown status = 0; 7725946c431SNeilBrown __locks_wake_up_blocks(waiter); 7731da177e4SLinus Torvalds __locks_delete_block(waiter); 7747b2296afSJeff Layton spin_unlock(&blocked_lock_lock); 775cb03f94fSNeilBrown return status; 7761da177e4SLinus Torvalds } 777cb03f94fSNeilBrown EXPORT_SYMBOL(locks_delete_block); 7781da177e4SLinus Torvalds 7791da177e4SLinus Torvalds /* Insert waiter into blocker's block list. 7801da177e4SLinus Torvalds * We use a circular list so that processes can be easily woken up in 7811da177e4SLinus Torvalds * the order they blocked. The documentation doesn't require this but 7821da177e4SLinus Torvalds * it seems like the reasonable thing to do. 7831c8c601aSJeff Layton * 7846109c850SJeff Layton * Must be called with both the flc_lock and blocked_lock_lock held. The 785ada5c1daSNeilBrown * fl_blocked_requests list itself is protected by the blocked_lock_lock, 786ada5c1daSNeilBrown * but by ensuring that the flc_lock is also held on insertions we can avoid 787ada5c1daSNeilBrown * taking the blocked_lock_lock in some cases when we see that the 788ada5c1daSNeilBrown * fl_blocked_requests list is empty. 789fd7732e0SNeilBrown * 790fd7732e0SNeilBrown * Rather than just adding to the list, we check for conflicts with any existing 791fd7732e0SNeilBrown * waiters, and add beneath any waiter that blocks the new waiter. 792fd7732e0SNeilBrown * Thus wakeups don't happen until needed. 7931da177e4SLinus Torvalds */ 7941c8c601aSJeff Layton static void __locks_insert_block(struct file_lock *blocker, 795fd7732e0SNeilBrown struct file_lock *waiter, 796fd7732e0SNeilBrown bool conflict(struct file_lock *, 797fd7732e0SNeilBrown struct file_lock *)) 7981da177e4SLinus Torvalds { 799fd7732e0SNeilBrown struct file_lock *fl; 800ada5c1daSNeilBrown BUG_ON(!list_empty(&waiter->fl_blocked_member)); 801fd7732e0SNeilBrown 802fd7732e0SNeilBrown new_blocker: 803fd7732e0SNeilBrown list_for_each_entry(fl, &blocker->fl_blocked_requests, fl_blocked_member) 804fd7732e0SNeilBrown if (conflict(fl, waiter)) { 805fd7732e0SNeilBrown blocker = fl; 806fd7732e0SNeilBrown goto new_blocker; 807fd7732e0SNeilBrown } 808ada5c1daSNeilBrown waiter->fl_blocker = blocker; 809ada5c1daSNeilBrown list_add_tail(&waiter->fl_blocked_member, &blocker->fl_blocked_requests); 810cff2fce5SJeff Layton if (IS_POSIX(blocker) && !IS_OFDLCK(blocker)) 8111c8c601aSJeff Layton locks_insert_global_blocked(waiter); 8125946c431SNeilBrown 8135946c431SNeilBrown /* The requests in waiter->fl_blocked are known to conflict with 8145946c431SNeilBrown * waiter, but might not conflict with blocker, or the requests 8155946c431SNeilBrown * and lock which block it. So they all need to be woken. 8165946c431SNeilBrown */ 8175946c431SNeilBrown __locks_wake_up_blocks(waiter); 8181c8c601aSJeff Layton } 8191c8c601aSJeff Layton 8206109c850SJeff Layton /* Must be called with flc_lock held. */ 8211c8c601aSJeff Layton static void locks_insert_block(struct file_lock *blocker, 822fd7732e0SNeilBrown struct file_lock *waiter, 823fd7732e0SNeilBrown bool conflict(struct file_lock *, 824fd7732e0SNeilBrown struct file_lock *)) 8251c8c601aSJeff Layton { 8267b2296afSJeff Layton spin_lock(&blocked_lock_lock); 827fd7732e0SNeilBrown __locks_insert_block(blocker, waiter, conflict); 8287b2296afSJeff Layton spin_unlock(&blocked_lock_lock); 8291da177e4SLinus Torvalds } 8301da177e4SLinus Torvalds 8311cb36012SJeff Layton /* 8321cb36012SJeff Layton * Wake up processes blocked waiting for blocker. 8331cb36012SJeff Layton * 8346109c850SJeff Layton * Must be called with the inode->flc_lock held! 8351da177e4SLinus Torvalds */ 8361da177e4SLinus Torvalds static void locks_wake_up_blocks(struct file_lock *blocker) 8371da177e4SLinus Torvalds { 8384e8c765dSJeff Layton /* 8394e8c765dSJeff Layton * Avoid taking global lock if list is empty. This is safe since new 8406109c850SJeff Layton * blocked requests are only added to the list under the flc_lock, and 841ada5c1daSNeilBrown * the flc_lock is always held here. Note that removal from the 842ada5c1daSNeilBrown * fl_blocked_requests list does not require the flc_lock, so we must 843ada5c1daSNeilBrown * recheck list_empty() after acquiring the blocked_lock_lock. 8444e8c765dSJeff Layton */ 845ada5c1daSNeilBrown if (list_empty(&blocker->fl_blocked_requests)) 8464e8c765dSJeff Layton return; 8474e8c765dSJeff Layton 8487b2296afSJeff Layton spin_lock(&blocked_lock_lock); 849ad6bbd8bSNeilBrown __locks_wake_up_blocks(blocker); 8507b2296afSJeff Layton spin_unlock(&blocked_lock_lock); 8511da177e4SLinus Torvalds } 8521da177e4SLinus Torvalds 8535263e31eSJeff Layton static void 854e084c1bdSJeff Layton locks_insert_lock_ctx(struct file_lock *fl, struct list_head *before) 8555263e31eSJeff Layton { 8565263e31eSJeff Layton list_add_tail(&fl->fl_list, before); 8575263e31eSJeff Layton locks_insert_global_locks(fl); 8585263e31eSJeff Layton } 8595263e31eSJeff Layton 8608634b51fSJeff Layton static void 861e084c1bdSJeff Layton locks_unlink_lock_ctx(struct file_lock *fl) 8621da177e4SLinus Torvalds { 86388974691SJeff Layton locks_delete_global_locks(fl); 8648634b51fSJeff Layton list_del_init(&fl->fl_list); 8651da177e4SLinus Torvalds locks_wake_up_blocks(fl); 86624cbe784SJeff Layton } 86724cbe784SJeff Layton 8685263e31eSJeff Layton static void 869e084c1bdSJeff Layton locks_delete_lock_ctx(struct file_lock *fl, struct list_head *dispose) 8705263e31eSJeff Layton { 871e084c1bdSJeff Layton locks_unlink_lock_ctx(fl); 8728634b51fSJeff Layton if (dispose) 8738634b51fSJeff Layton list_add(&fl->fl_list, dispose); 8748634b51fSJeff Layton else 8758634b51fSJeff Layton locks_free_lock(fl); 8765263e31eSJeff Layton } 8775263e31eSJeff Layton 8781da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. Common functionality 8791da177e4SLinus Torvalds * checks for shared/exclusive status of overlapping locks. 8801da177e4SLinus Torvalds */ 881c0e15908SNeilBrown static bool locks_conflict(struct file_lock *caller_fl, 882c0e15908SNeilBrown struct file_lock *sys_fl) 8831da177e4SLinus Torvalds { 8841da177e4SLinus Torvalds if (sys_fl->fl_type == F_WRLCK) 885c0e15908SNeilBrown return true; 8861da177e4SLinus Torvalds if (caller_fl->fl_type == F_WRLCK) 887c0e15908SNeilBrown return true; 888c0e15908SNeilBrown return false; 8891da177e4SLinus Torvalds } 8901da177e4SLinus Torvalds 8911da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. POSIX specific 8921da177e4SLinus Torvalds * checking before calling the locks_conflict(). 8931da177e4SLinus Torvalds */ 894c0e15908SNeilBrown static bool posix_locks_conflict(struct file_lock *caller_fl, 895c0e15908SNeilBrown struct file_lock *sys_fl) 8961da177e4SLinus Torvalds { 8971da177e4SLinus Torvalds /* POSIX locks owned by the same process do not conflict with 8981da177e4SLinus Torvalds * each other. 8991da177e4SLinus Torvalds */ 9009b8c8695SJeff Layton if (posix_same_owner(caller_fl, sys_fl)) 901c0e15908SNeilBrown return false; 9021da177e4SLinus Torvalds 9031da177e4SLinus Torvalds /* Check whether they overlap */ 9041da177e4SLinus Torvalds if (!locks_overlap(caller_fl, sys_fl)) 905c0e15908SNeilBrown return false; 9061da177e4SLinus Torvalds 907c0e15908SNeilBrown return locks_conflict(caller_fl, sys_fl); 9081da177e4SLinus Torvalds } 9091da177e4SLinus Torvalds 9101da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. FLOCK specific 9111da177e4SLinus Torvalds * checking before calling the locks_conflict(). 9121da177e4SLinus Torvalds */ 913c0e15908SNeilBrown static bool flock_locks_conflict(struct file_lock *caller_fl, 914c0e15908SNeilBrown struct file_lock *sys_fl) 9151da177e4SLinus Torvalds { 9161da177e4SLinus Torvalds /* FLOCK locks referring to the same filp do not conflict with 9171da177e4SLinus Torvalds * each other. 9181da177e4SLinus Torvalds */ 9199b8c8695SJeff Layton if (caller_fl->fl_file == sys_fl->fl_file) 920c0e15908SNeilBrown return false; 9211da177e4SLinus Torvalds if ((caller_fl->fl_type & LOCK_MAND) || (sys_fl->fl_type & LOCK_MAND)) 922c0e15908SNeilBrown return false; 9231da177e4SLinus Torvalds 924c0e15908SNeilBrown return locks_conflict(caller_fl, sys_fl); 9251da177e4SLinus Torvalds } 9261da177e4SLinus Torvalds 9276d34ac19SJ. Bruce Fields void 9289d6a8c5cSMarc Eshel posix_test_lock(struct file *filp, struct file_lock *fl) 9291da177e4SLinus Torvalds { 9301da177e4SLinus Torvalds struct file_lock *cfl; 931bd61e0a9SJeff Layton struct file_lock_context *ctx; 932c568d683SMiklos Szeredi struct inode *inode = locks_inode(filp); 9331da177e4SLinus Torvalds 934128a3785SDmitry Vyukov ctx = smp_load_acquire(&inode->i_flctx); 935bd61e0a9SJeff Layton if (!ctx || list_empty_careful(&ctx->flc_posix)) { 936bd61e0a9SJeff Layton fl->fl_type = F_UNLCK; 937bd61e0a9SJeff Layton return; 9381da177e4SLinus Torvalds } 939bd61e0a9SJeff Layton 9406109c850SJeff Layton spin_lock(&ctx->flc_lock); 941bd61e0a9SJeff Layton list_for_each_entry(cfl, &ctx->flc_posix, fl_list) { 942bd61e0a9SJeff Layton if (posix_locks_conflict(fl, cfl)) { 9433fe0fff1SKinglong Mee locks_copy_conflock(fl, cfl); 944bd61e0a9SJeff Layton goto out; 945bd61e0a9SJeff Layton } 946bd61e0a9SJeff Layton } 947129a84deSJ. Bruce Fields fl->fl_type = F_UNLCK; 948bd61e0a9SJeff Layton out: 9496109c850SJeff Layton spin_unlock(&ctx->flc_lock); 9506d34ac19SJ. Bruce Fields return; 9511da177e4SLinus Torvalds } 9521da177e4SLinus Torvalds EXPORT_SYMBOL(posix_test_lock); 9531da177e4SLinus Torvalds 954b533184fSJ. Bruce Fields /* 955b533184fSJ. Bruce Fields * Deadlock detection: 9561da177e4SLinus Torvalds * 957b533184fSJ. Bruce Fields * We attempt to detect deadlocks that are due purely to posix file 958b533184fSJ. Bruce Fields * locks. 9591da177e4SLinus Torvalds * 960b533184fSJ. Bruce Fields * We assume that a task can be waiting for at most one lock at a time. 961b533184fSJ. Bruce Fields * So for any acquired lock, the process holding that lock may be 962b533184fSJ. Bruce Fields * waiting on at most one other lock. That lock in turns may be held by 963b533184fSJ. Bruce Fields * someone waiting for at most one other lock. Given a requested lock 964b533184fSJ. Bruce Fields * caller_fl which is about to wait for a conflicting lock block_fl, we 965b533184fSJ. Bruce Fields * follow this chain of waiters to ensure we are not about to create a 966b533184fSJ. Bruce Fields * cycle. 96797855b49SJ. Bruce Fields * 968b533184fSJ. Bruce Fields * Since we do this before we ever put a process to sleep on a lock, we 969b533184fSJ. Bruce Fields * are ensured that there is never a cycle; that is what guarantees that 970b533184fSJ. Bruce Fields * the while() loop in posix_locks_deadlock() eventually completes. 971b533184fSJ. Bruce Fields * 972b533184fSJ. Bruce Fields * Note: the above assumption may not be true when handling lock 973b533184fSJ. Bruce Fields * requests from a broken NFS client. It may also fail in the presence 974b533184fSJ. Bruce Fields * of tasks (such as posix threads) sharing the same open file table. 975b533184fSJ. Bruce Fields * To handle those cases, we just bail out after a few iterations. 97657b65325SJeff Layton * 977cff2fce5SJeff Layton * For FL_OFDLCK locks, the owner is the filp, not the files_struct. 97857b65325SJeff Layton * Because the owner is not even nominally tied to a thread of 97957b65325SJeff Layton * execution, the deadlock detection below can't reasonably work well. Just 98057b65325SJeff Layton * skip it for those. 98157b65325SJeff Layton * 982cff2fce5SJeff Layton * In principle, we could do a more limited deadlock detection on FL_OFDLCK 98357b65325SJeff Layton * locks that just checks for the case where two tasks are attempting to 98457b65325SJeff Layton * upgrade from read to write locks on the same inode. 9851da177e4SLinus Torvalds */ 98697855b49SJ. Bruce Fields 98797855b49SJ. Bruce Fields #define MAX_DEADLK_ITERATIONS 10 98897855b49SJ. Bruce Fields 989b533184fSJ. Bruce Fields /* Find a lock that the owner of the given block_fl is blocking on. */ 990b533184fSJ. Bruce Fields static struct file_lock *what_owner_is_waiting_for(struct file_lock *block_fl) 991b533184fSJ. Bruce Fields { 992b533184fSJ. Bruce Fields struct file_lock *fl; 993b533184fSJ. Bruce Fields 9943999e493SJeff Layton hash_for_each_possible(blocked_hash, fl, fl_link, posix_owner_key(block_fl)) { 9955946c431SNeilBrown if (posix_same_owner(fl, block_fl)) { 9965946c431SNeilBrown while (fl->fl_blocker) 9975946c431SNeilBrown fl = fl->fl_blocker; 9985946c431SNeilBrown return fl; 9995946c431SNeilBrown } 1000b533184fSJ. Bruce Fields } 1001b533184fSJ. Bruce Fields return NULL; 1002b533184fSJ. Bruce Fields } 1003b533184fSJ. Bruce Fields 10047b2296afSJeff Layton /* Must be called with the blocked_lock_lock held! */ 1005b0904e14SAdrian Bunk static int posix_locks_deadlock(struct file_lock *caller_fl, 10061da177e4SLinus Torvalds struct file_lock *block_fl) 10071da177e4SLinus Torvalds { 100897855b49SJ. Bruce Fields int i = 0; 10091da177e4SLinus Torvalds 1010663d5af7SDaniel Wagner lockdep_assert_held(&blocked_lock_lock); 1011663d5af7SDaniel Wagner 101257b65325SJeff Layton /* 101357b65325SJeff Layton * This deadlock detector can't reasonably detect deadlocks with 1014cff2fce5SJeff Layton * FL_OFDLCK locks, since they aren't owned by a process, per-se. 101557b65325SJeff Layton */ 1016cff2fce5SJeff Layton if (IS_OFDLCK(caller_fl)) 101757b65325SJeff Layton return 0; 101857b65325SJeff Layton 1019b533184fSJ. Bruce Fields while ((block_fl = what_owner_is_waiting_for(block_fl))) { 102097855b49SJ. Bruce Fields if (i++ > MAX_DEADLK_ITERATIONS) 102197855b49SJ. Bruce Fields return 0; 1022b533184fSJ. Bruce Fields if (posix_same_owner(caller_fl, block_fl)) 1023b533184fSJ. Bruce Fields return 1; 10241da177e4SLinus Torvalds } 10251da177e4SLinus Torvalds return 0; 10261da177e4SLinus Torvalds } 10271da177e4SLinus Torvalds 10281da177e4SLinus Torvalds /* Try to create a FLOCK lock on filp. We always insert new FLOCK locks 102902888f41SJ. Bruce Fields * after any leases, but before any posix locks. 1030f475ae95STrond Myklebust * 1031f475ae95STrond Myklebust * Note that if called with an FL_EXISTS argument, the caller may determine 1032f475ae95STrond Myklebust * whether or not a lock was successfully freed by testing the return 1033f475ae95STrond Myklebust * value for -ENOENT. 10341da177e4SLinus Torvalds */ 1035bcd7f78dSJeff Layton static int flock_lock_inode(struct inode *inode, struct file_lock *request) 10361da177e4SLinus Torvalds { 1037993dfa87STrond Myklebust struct file_lock *new_fl = NULL; 10385263e31eSJeff Layton struct file_lock *fl; 10395263e31eSJeff Layton struct file_lock_context *ctx; 10401da177e4SLinus Torvalds int error = 0; 10415263e31eSJeff Layton bool found = false; 1042ed9814d8SJeff Layton LIST_HEAD(dispose); 10431da177e4SLinus Torvalds 10445c1c669aSJeff Layton ctx = locks_get_lock_context(inode, request->fl_type); 10455c1c669aSJeff Layton if (!ctx) { 10465c1c669aSJeff Layton if (request->fl_type != F_UNLCK) 10475263e31eSJeff Layton return -ENOMEM; 10485c1c669aSJeff Layton return (request->fl_flags & FL_EXISTS) ? -ENOENT : 0; 10495c1c669aSJeff Layton } 10505263e31eSJeff Layton 1051b89f4321SArnd Bergmann if (!(request->fl_flags & FL_ACCESS) && (request->fl_type != F_UNLCK)) { 1052b89f4321SArnd Bergmann new_fl = locks_alloc_lock(); 1053b89f4321SArnd Bergmann if (!new_fl) 1054b89f4321SArnd Bergmann return -ENOMEM; 1055b89f4321SArnd Bergmann } 1056b89f4321SArnd Bergmann 105702e525b2SPeter Zijlstra percpu_down_read(&file_rwsem); 10586109c850SJeff Layton spin_lock(&ctx->flc_lock); 1059f07f18ddSTrond Myklebust if (request->fl_flags & FL_ACCESS) 1060f07f18ddSTrond Myklebust goto find_conflict; 106184d535adSPavel Emelyanov 10625263e31eSJeff Layton list_for_each_entry(fl, &ctx->flc_flock, fl_list) { 1063bcd7f78dSJeff Layton if (request->fl_file != fl->fl_file) 10641da177e4SLinus Torvalds continue; 1065993dfa87STrond Myklebust if (request->fl_type == fl->fl_type) 10661da177e4SLinus Torvalds goto out; 10675263e31eSJeff Layton found = true; 1068e084c1bdSJeff Layton locks_delete_lock_ctx(fl, &dispose); 10691da177e4SLinus Torvalds break; 10701da177e4SLinus Torvalds } 10711da177e4SLinus Torvalds 1072f475ae95STrond Myklebust if (request->fl_type == F_UNLCK) { 1073f475ae95STrond Myklebust if ((request->fl_flags & FL_EXISTS) && !found) 1074f475ae95STrond Myklebust error = -ENOENT; 1075993dfa87STrond Myklebust goto out; 1076f475ae95STrond Myklebust } 10771da177e4SLinus Torvalds 1078f07f18ddSTrond Myklebust find_conflict: 10795263e31eSJeff Layton list_for_each_entry(fl, &ctx->flc_flock, fl_list) { 1080993dfa87STrond Myklebust if (!flock_locks_conflict(request, fl)) 10811da177e4SLinus Torvalds continue; 10821da177e4SLinus Torvalds error = -EAGAIN; 1083bde74e4bSMiklos Szeredi if (!(request->fl_flags & FL_SLEEP)) 1084bde74e4bSMiklos Szeredi goto out; 1085bde74e4bSMiklos Szeredi error = FILE_LOCK_DEFERRED; 1086fd7732e0SNeilBrown locks_insert_block(fl, request, flock_locks_conflict); 10871da177e4SLinus Torvalds goto out; 10881da177e4SLinus Torvalds } 1089f07f18ddSTrond Myklebust if (request->fl_flags & FL_ACCESS) 1090f07f18ddSTrond Myklebust goto out; 1091993dfa87STrond Myklebust locks_copy_lock(new_fl, request); 10925946c431SNeilBrown locks_move_blocks(new_fl, request); 1093e084c1bdSJeff Layton locks_insert_lock_ctx(new_fl, &ctx->flc_flock); 1094993dfa87STrond Myklebust new_fl = NULL; 10959cedc194SKirill Korotaev error = 0; 10961da177e4SLinus Torvalds 10971da177e4SLinus Torvalds out: 10986109c850SJeff Layton spin_unlock(&ctx->flc_lock); 109902e525b2SPeter Zijlstra percpu_up_read(&file_rwsem); 1100993dfa87STrond Myklebust if (new_fl) 1101993dfa87STrond Myklebust locks_free_lock(new_fl); 1102ed9814d8SJeff Layton locks_dispose_list(&dispose); 1103c883da31SJeff Layton trace_flock_lock_inode(inode, request, error); 11041da177e4SLinus Torvalds return error; 11051da177e4SLinus Torvalds } 11061da177e4SLinus Torvalds 1107b4d629a3SJeff Layton static int posix_lock_inode(struct inode *inode, struct file_lock *request, 1108b4d629a3SJeff Layton struct file_lock *conflock) 11091da177e4SLinus Torvalds { 1110bd61e0a9SJeff Layton struct file_lock *fl, *tmp; 111139005d02SMiklos Szeredi struct file_lock *new_fl = NULL; 111239005d02SMiklos Szeredi struct file_lock *new_fl2 = NULL; 11131da177e4SLinus Torvalds struct file_lock *left = NULL; 11141da177e4SLinus Torvalds struct file_lock *right = NULL; 1115bd61e0a9SJeff Layton struct file_lock_context *ctx; 1116b9746ef8SJeff Layton int error; 1117b9746ef8SJeff Layton bool added = false; 1118ed9814d8SJeff Layton LIST_HEAD(dispose); 11191da177e4SLinus Torvalds 11205c1c669aSJeff Layton ctx = locks_get_lock_context(inode, request->fl_type); 1121bd61e0a9SJeff Layton if (!ctx) 11225c1c669aSJeff Layton return (request->fl_type == F_UNLCK) ? 0 : -ENOMEM; 1123bd61e0a9SJeff Layton 11241da177e4SLinus Torvalds /* 11251da177e4SLinus Torvalds * We may need two file_lock structures for this operation, 11261da177e4SLinus Torvalds * so we get them in advance to avoid races. 112739005d02SMiklos Szeredi * 112839005d02SMiklos Szeredi * In some cases we can be sure, that no new locks will be needed 11291da177e4SLinus Torvalds */ 113039005d02SMiklos Szeredi if (!(request->fl_flags & FL_ACCESS) && 113139005d02SMiklos Szeredi (request->fl_type != F_UNLCK || 113239005d02SMiklos Szeredi request->fl_start != 0 || request->fl_end != OFFSET_MAX)) { 11331da177e4SLinus Torvalds new_fl = locks_alloc_lock(); 11341da177e4SLinus Torvalds new_fl2 = locks_alloc_lock(); 113539005d02SMiklos Szeredi } 11361da177e4SLinus Torvalds 113702e525b2SPeter Zijlstra percpu_down_read(&file_rwsem); 11386109c850SJeff Layton spin_lock(&ctx->flc_lock); 11391cb36012SJeff Layton /* 11401cb36012SJeff Layton * New lock request. Walk all POSIX locks and look for conflicts. If 11411cb36012SJeff Layton * there are any, either return error or put the request on the 114248f74186SJeff Layton * blocker's list of waiters and the global blocked_hash. 11431cb36012SJeff Layton */ 11441da177e4SLinus Torvalds if (request->fl_type != F_UNLCK) { 1145bd61e0a9SJeff Layton list_for_each_entry(fl, &ctx->flc_posix, fl_list) { 11461da177e4SLinus Torvalds if (!posix_locks_conflict(request, fl)) 11471da177e4SLinus Torvalds continue; 11485842add2SAndy Adamson if (conflock) 11493fe0fff1SKinglong Mee locks_copy_conflock(conflock, fl); 11501da177e4SLinus Torvalds error = -EAGAIN; 11511da177e4SLinus Torvalds if (!(request->fl_flags & FL_SLEEP)) 11521da177e4SLinus Torvalds goto out; 11531c8c601aSJeff Layton /* 11541c8c601aSJeff Layton * Deadlock detection and insertion into the blocked 11551c8c601aSJeff Layton * locks list must be done while holding the same lock! 11561c8c601aSJeff Layton */ 11571da177e4SLinus Torvalds error = -EDEADLK; 11587b2296afSJeff Layton spin_lock(&blocked_lock_lock); 1159945ab8f6SJeff Layton /* 1160945ab8f6SJeff Layton * Ensure that we don't find any locks blocked on this 1161945ab8f6SJeff Layton * request during deadlock detection. 1162945ab8f6SJeff Layton */ 1163945ab8f6SJeff Layton __locks_wake_up_blocks(request); 11641c8c601aSJeff Layton if (likely(!posix_locks_deadlock(request, fl))) { 1165bde74e4bSMiklos Szeredi error = FILE_LOCK_DEFERRED; 1166fd7732e0SNeilBrown __locks_insert_block(fl, request, 1167fd7732e0SNeilBrown posix_locks_conflict); 11681c8c601aSJeff Layton } 11697b2296afSJeff Layton spin_unlock(&blocked_lock_lock); 11701da177e4SLinus Torvalds goto out; 11711da177e4SLinus Torvalds } 11721da177e4SLinus Torvalds } 11731da177e4SLinus Torvalds 11741da177e4SLinus Torvalds /* If we're just looking for a conflict, we're done. */ 11751da177e4SLinus Torvalds error = 0; 11761da177e4SLinus Torvalds if (request->fl_flags & FL_ACCESS) 11771da177e4SLinus Torvalds goto out; 11781da177e4SLinus Torvalds 1179bd61e0a9SJeff Layton /* Find the first old lock with the same owner as the new lock */ 1180bd61e0a9SJeff Layton list_for_each_entry(fl, &ctx->flc_posix, fl_list) { 1181bd61e0a9SJeff Layton if (posix_same_owner(request, fl)) 1182bd61e0a9SJeff Layton break; 11831da177e4SLinus Torvalds } 11841da177e4SLinus Torvalds 11851da177e4SLinus Torvalds /* Process locks with this owner. */ 1186bd61e0a9SJeff Layton list_for_each_entry_safe_from(fl, tmp, &ctx->flc_posix, fl_list) { 1187bd61e0a9SJeff Layton if (!posix_same_owner(request, fl)) 1188bd61e0a9SJeff Layton break; 1189bd61e0a9SJeff Layton 1190bd61e0a9SJeff Layton /* Detect adjacent or overlapping regions (if same lock type) */ 11911da177e4SLinus Torvalds if (request->fl_type == fl->fl_type) { 1192449231d6SOlaf Kirch /* In all comparisons of start vs end, use 1193449231d6SOlaf Kirch * "start - 1" rather than "end + 1". If end 1194449231d6SOlaf Kirch * is OFFSET_MAX, end + 1 will become negative. 1195449231d6SOlaf Kirch */ 11961da177e4SLinus Torvalds if (fl->fl_end < request->fl_start - 1) 1197bd61e0a9SJeff Layton continue; 11981da177e4SLinus Torvalds /* If the next lock in the list has entirely bigger 11991da177e4SLinus Torvalds * addresses than the new one, insert the lock here. 12001da177e4SLinus Torvalds */ 1201449231d6SOlaf Kirch if (fl->fl_start - 1 > request->fl_end) 12021da177e4SLinus Torvalds break; 12031da177e4SLinus Torvalds 12041da177e4SLinus Torvalds /* If we come here, the new and old lock are of the 12051da177e4SLinus Torvalds * same type and adjacent or overlapping. Make one 12061da177e4SLinus Torvalds * lock yielding from the lower start address of both 12071da177e4SLinus Torvalds * locks to the higher end address. 12081da177e4SLinus Torvalds */ 12091da177e4SLinus Torvalds if (fl->fl_start > request->fl_start) 12101da177e4SLinus Torvalds fl->fl_start = request->fl_start; 12111da177e4SLinus Torvalds else 12121da177e4SLinus Torvalds request->fl_start = fl->fl_start; 12131da177e4SLinus Torvalds if (fl->fl_end < request->fl_end) 12141da177e4SLinus Torvalds fl->fl_end = request->fl_end; 12151da177e4SLinus Torvalds else 12161da177e4SLinus Torvalds request->fl_end = fl->fl_end; 12171da177e4SLinus Torvalds if (added) { 1218e084c1bdSJeff Layton locks_delete_lock_ctx(fl, &dispose); 12191da177e4SLinus Torvalds continue; 12201da177e4SLinus Torvalds } 12211da177e4SLinus Torvalds request = fl; 1222b9746ef8SJeff Layton added = true; 1223bd61e0a9SJeff Layton } else { 12241da177e4SLinus Torvalds /* Processing for different lock types is a bit 12251da177e4SLinus Torvalds * more complex. 12261da177e4SLinus Torvalds */ 12271da177e4SLinus Torvalds if (fl->fl_end < request->fl_start) 1228bd61e0a9SJeff Layton continue; 12291da177e4SLinus Torvalds if (fl->fl_start > request->fl_end) 12301da177e4SLinus Torvalds break; 12311da177e4SLinus Torvalds if (request->fl_type == F_UNLCK) 1232b9746ef8SJeff Layton added = true; 12331da177e4SLinus Torvalds if (fl->fl_start < request->fl_start) 12341da177e4SLinus Torvalds left = fl; 12351da177e4SLinus Torvalds /* If the next lock in the list has a higher end 12361da177e4SLinus Torvalds * address than the new one, insert the new one here. 12371da177e4SLinus Torvalds */ 12381da177e4SLinus Torvalds if (fl->fl_end > request->fl_end) { 12391da177e4SLinus Torvalds right = fl; 12401da177e4SLinus Torvalds break; 12411da177e4SLinus Torvalds } 12421da177e4SLinus Torvalds if (fl->fl_start >= request->fl_start) { 12431da177e4SLinus Torvalds /* The new lock completely replaces an old 12441da177e4SLinus Torvalds * one (This may happen several times). 12451da177e4SLinus Torvalds */ 12461da177e4SLinus Torvalds if (added) { 1247e084c1bdSJeff Layton locks_delete_lock_ctx(fl, &dispose); 12481da177e4SLinus Torvalds continue; 12491da177e4SLinus Torvalds } 1250b84d49f9SJeff Layton /* 1251b84d49f9SJeff Layton * Replace the old lock with new_fl, and 1252b84d49f9SJeff Layton * remove the old one. It's safe to do the 1253b84d49f9SJeff Layton * insert here since we know that we won't be 1254b84d49f9SJeff Layton * using new_fl later, and that the lock is 1255b84d49f9SJeff Layton * just replacing an existing lock. 12561da177e4SLinus Torvalds */ 1257b84d49f9SJeff Layton error = -ENOLCK; 1258b84d49f9SJeff Layton if (!new_fl) 1259b84d49f9SJeff Layton goto out; 1260b84d49f9SJeff Layton locks_copy_lock(new_fl, request); 1261b84d49f9SJeff Layton request = new_fl; 1262b84d49f9SJeff Layton new_fl = NULL; 1263e084c1bdSJeff Layton locks_insert_lock_ctx(request, &fl->fl_list); 1264e084c1bdSJeff Layton locks_delete_lock_ctx(fl, &dispose); 1265b9746ef8SJeff Layton added = true; 12661da177e4SLinus Torvalds } 12671da177e4SLinus Torvalds } 12681da177e4SLinus Torvalds } 12691da177e4SLinus Torvalds 12700d9a490aSMiklos Szeredi /* 12711cb36012SJeff Layton * The above code only modifies existing locks in case of merging or 12721cb36012SJeff Layton * replacing. If new lock(s) need to be inserted all modifications are 12731cb36012SJeff Layton * done below this, so it's safe yet to bail out. 12740d9a490aSMiklos Szeredi */ 12750d9a490aSMiklos Szeredi error = -ENOLCK; /* "no luck" */ 12760d9a490aSMiklos Szeredi if (right && left == right && !new_fl2) 12770d9a490aSMiklos Szeredi goto out; 12780d9a490aSMiklos Szeredi 12791da177e4SLinus Torvalds error = 0; 12801da177e4SLinus Torvalds if (!added) { 1281f475ae95STrond Myklebust if (request->fl_type == F_UNLCK) { 1282f475ae95STrond Myklebust if (request->fl_flags & FL_EXISTS) 1283f475ae95STrond Myklebust error = -ENOENT; 12841da177e4SLinus Torvalds goto out; 1285f475ae95STrond Myklebust } 12860d9a490aSMiklos Szeredi 12870d9a490aSMiklos Szeredi if (!new_fl) { 12880d9a490aSMiklos Szeredi error = -ENOLCK; 12890d9a490aSMiklos Szeredi goto out; 12900d9a490aSMiklos Szeredi } 12911da177e4SLinus Torvalds locks_copy_lock(new_fl, request); 12925946c431SNeilBrown locks_move_blocks(new_fl, request); 1293e084c1bdSJeff Layton locks_insert_lock_ctx(new_fl, &fl->fl_list); 12942e2f756fSJeff Layton fl = new_fl; 12951da177e4SLinus Torvalds new_fl = NULL; 12961da177e4SLinus Torvalds } 12971da177e4SLinus Torvalds if (right) { 12981da177e4SLinus Torvalds if (left == right) { 12991da177e4SLinus Torvalds /* The new lock breaks the old one in two pieces, 13001da177e4SLinus Torvalds * so we have to use the second new lock. 13011da177e4SLinus Torvalds */ 13021da177e4SLinus Torvalds left = new_fl2; 13031da177e4SLinus Torvalds new_fl2 = NULL; 13041da177e4SLinus Torvalds locks_copy_lock(left, right); 1305e084c1bdSJeff Layton locks_insert_lock_ctx(left, &fl->fl_list); 13061da177e4SLinus Torvalds } 13071da177e4SLinus Torvalds right->fl_start = request->fl_end + 1; 13081da177e4SLinus Torvalds locks_wake_up_blocks(right); 13091da177e4SLinus Torvalds } 13101da177e4SLinus Torvalds if (left) { 13111da177e4SLinus Torvalds left->fl_end = request->fl_start - 1; 13121da177e4SLinus Torvalds locks_wake_up_blocks(left); 13131da177e4SLinus Torvalds } 13141da177e4SLinus Torvalds out: 13156109c850SJeff Layton spin_unlock(&ctx->flc_lock); 131602e525b2SPeter Zijlstra percpu_up_read(&file_rwsem); 13171da177e4SLinus Torvalds /* 13181da177e4SLinus Torvalds * Free any unused locks. 13191da177e4SLinus Torvalds */ 13201da177e4SLinus Torvalds if (new_fl) 13211da177e4SLinus Torvalds locks_free_lock(new_fl); 13221da177e4SLinus Torvalds if (new_fl2) 13231da177e4SLinus Torvalds locks_free_lock(new_fl2); 1324ed9814d8SJeff Layton locks_dispose_list(&dispose); 13251890910fSJeff Layton trace_posix_lock_inode(inode, request, error); 13261890910fSJeff Layton 13271da177e4SLinus Torvalds return error; 13281da177e4SLinus Torvalds } 13291da177e4SLinus Torvalds 13301da177e4SLinus Torvalds /** 13311da177e4SLinus Torvalds * posix_lock_file - Apply a POSIX-style lock to a file 13321da177e4SLinus Torvalds * @filp: The file to apply the lock to 13331da177e4SLinus Torvalds * @fl: The lock to be applied 1334150b3934SMarc Eshel * @conflock: Place to return a copy of the conflicting lock, if found. 13351da177e4SLinus Torvalds * 13361da177e4SLinus Torvalds * Add a POSIX style lock to a file. 13371da177e4SLinus Torvalds * We merge adjacent & overlapping locks whenever possible. 13381da177e4SLinus Torvalds * POSIX locks are sorted by owner task, then by starting address 1339f475ae95STrond Myklebust * 1340f475ae95STrond Myklebust * Note that if called with an FL_EXISTS argument, the caller may determine 1341f475ae95STrond Myklebust * whether or not a lock was successfully freed by testing the return 1342f475ae95STrond Myklebust * value for -ENOENT. 13431da177e4SLinus Torvalds */ 1344150b3934SMarc Eshel int posix_lock_file(struct file *filp, struct file_lock *fl, 13455842add2SAndy Adamson struct file_lock *conflock) 13465842add2SAndy Adamson { 1347c568d683SMiklos Szeredi return posix_lock_inode(locks_inode(filp), fl, conflock); 13485842add2SAndy Adamson } 1349150b3934SMarc Eshel EXPORT_SYMBOL(posix_lock_file); 13501da177e4SLinus Torvalds 13511da177e4SLinus Torvalds /** 135229d01b22SJeff Layton * posix_lock_inode_wait - Apply a POSIX-style lock to a file 135329d01b22SJeff Layton * @inode: inode of file to which lock request should be applied 13541da177e4SLinus Torvalds * @fl: The lock to be applied 13551da177e4SLinus Torvalds * 1356616fb38fSBenjamin Coddington * Apply a POSIX style lock request to an inode. 13571da177e4SLinus Torvalds */ 1358616fb38fSBenjamin Coddington static int posix_lock_inode_wait(struct inode *inode, struct file_lock *fl) 13591da177e4SLinus Torvalds { 13601da177e4SLinus Torvalds int error; 13611da177e4SLinus Torvalds might_sleep (); 13621da177e4SLinus Torvalds for (;;) { 1363b4d629a3SJeff Layton error = posix_lock_inode(inode, fl, NULL); 1364bde74e4bSMiklos Szeredi if (error != FILE_LOCK_DEFERRED) 13651da177e4SLinus Torvalds break; 1366ada5c1daSNeilBrown error = wait_event_interruptible(fl->fl_wait, !fl->fl_blocker); 136716306a61SNeilBrown if (error) 13681da177e4SLinus Torvalds break; 13691da177e4SLinus Torvalds } 137016306a61SNeilBrown locks_delete_block(fl); 13711da177e4SLinus Torvalds return error; 13721da177e4SLinus Torvalds } 137329d01b22SJeff Layton 13749e8925b6SJeff Layton #ifdef CONFIG_MANDATORY_FILE_LOCKING 137529d01b22SJeff Layton /** 13761da177e4SLinus Torvalds * locks_mandatory_locked - Check for an active lock 1377d7a06983SJeff Layton * @file: the file to check 13781da177e4SLinus Torvalds * 13791da177e4SLinus Torvalds * Searches the inode's list of locks to find any POSIX locks which conflict. 13801da177e4SLinus Torvalds * This function is called from locks_verify_locked() only. 13811da177e4SLinus Torvalds */ 1382d7a06983SJeff Layton int locks_mandatory_locked(struct file *file) 13831da177e4SLinus Torvalds { 1384bd61e0a9SJeff Layton int ret; 1385c568d683SMiklos Szeredi struct inode *inode = locks_inode(file); 1386bd61e0a9SJeff Layton struct file_lock_context *ctx; 13871da177e4SLinus Torvalds struct file_lock *fl; 13881da177e4SLinus Torvalds 1389128a3785SDmitry Vyukov ctx = smp_load_acquire(&inode->i_flctx); 1390bd61e0a9SJeff Layton if (!ctx || list_empty_careful(&ctx->flc_posix)) 1391bd61e0a9SJeff Layton return 0; 1392bd61e0a9SJeff Layton 13931da177e4SLinus Torvalds /* 13941da177e4SLinus Torvalds * Search the lock list for this inode for any POSIX locks. 13951da177e4SLinus Torvalds */ 13966109c850SJeff Layton spin_lock(&ctx->flc_lock); 1397bd61e0a9SJeff Layton ret = 0; 1398bd61e0a9SJeff Layton list_for_each_entry(fl, &ctx->flc_posix, fl_list) { 139973a8f5f7SChristoph Hellwig if (fl->fl_owner != current->files && 1400bd61e0a9SJeff Layton fl->fl_owner != file) { 1401bd61e0a9SJeff Layton ret = -EAGAIN; 14021da177e4SLinus Torvalds break; 14031da177e4SLinus Torvalds } 1404bd61e0a9SJeff Layton } 14056109c850SJeff Layton spin_unlock(&ctx->flc_lock); 1406bd61e0a9SJeff Layton return ret; 14071da177e4SLinus Torvalds } 14081da177e4SLinus Torvalds 14091da177e4SLinus Torvalds /** 14101da177e4SLinus Torvalds * locks_mandatory_area - Check for a conflicting lock 14111da177e4SLinus Torvalds * @inode: the file to check 14121da177e4SLinus Torvalds * @filp: how the file was opened (if it was) 1413acc15575SChristoph Hellwig * @start: first byte in the file to check 1414acc15575SChristoph Hellwig * @end: lastbyte in the file to check 1415acc15575SChristoph Hellwig * @type: %F_WRLCK for a write lock, else %F_RDLCK 14161da177e4SLinus Torvalds * 14171da177e4SLinus Torvalds * Searches the inode's list of locks to find any POSIX locks which conflict. 14181da177e4SLinus Torvalds */ 1419acc15575SChristoph Hellwig int locks_mandatory_area(struct inode *inode, struct file *filp, loff_t start, 1420acc15575SChristoph Hellwig loff_t end, unsigned char type) 14211da177e4SLinus Torvalds { 14221da177e4SLinus Torvalds struct file_lock fl; 14231da177e4SLinus Torvalds int error; 142429723adeSJeff Layton bool sleep = false; 14251da177e4SLinus Torvalds 14261da177e4SLinus Torvalds locks_init_lock(&fl); 14271da177e4SLinus Torvalds fl.fl_pid = current->tgid; 14281da177e4SLinus Torvalds fl.fl_file = filp; 14291da177e4SLinus Torvalds fl.fl_flags = FL_POSIX | FL_ACCESS; 14301da177e4SLinus Torvalds if (filp && !(filp->f_flags & O_NONBLOCK)) 143129723adeSJeff Layton sleep = true; 1432acc15575SChristoph Hellwig fl.fl_type = type; 1433acc15575SChristoph Hellwig fl.fl_start = start; 1434acc15575SChristoph Hellwig fl.fl_end = end; 14351da177e4SLinus Torvalds 14361da177e4SLinus Torvalds for (;;) { 143729723adeSJeff Layton if (filp) { 143873a8f5f7SChristoph Hellwig fl.fl_owner = filp; 143929723adeSJeff Layton fl.fl_flags &= ~FL_SLEEP; 1440b4d629a3SJeff Layton error = posix_lock_inode(inode, &fl, NULL); 144129723adeSJeff Layton if (!error) 144229723adeSJeff Layton break; 144329723adeSJeff Layton } 144429723adeSJeff Layton 144529723adeSJeff Layton if (sleep) 144629723adeSJeff Layton fl.fl_flags |= FL_SLEEP; 144729723adeSJeff Layton fl.fl_owner = current->files; 1448b4d629a3SJeff Layton error = posix_lock_inode(inode, &fl, NULL); 1449bde74e4bSMiklos Szeredi if (error != FILE_LOCK_DEFERRED) 14501da177e4SLinus Torvalds break; 1451ada5c1daSNeilBrown error = wait_event_interruptible(fl.fl_wait, !fl.fl_blocker); 14521da177e4SLinus Torvalds if (!error) { 14531da177e4SLinus Torvalds /* 14541da177e4SLinus Torvalds * If we've been sleeping someone might have 14551da177e4SLinus Torvalds * changed the permissions behind our back. 14561da177e4SLinus Torvalds */ 1457a16877caSPavel Emelyanov if (__mandatory_lock(inode)) 14581da177e4SLinus Torvalds continue; 14591da177e4SLinus Torvalds } 14601da177e4SLinus Torvalds 14611da177e4SLinus Torvalds break; 14621da177e4SLinus Torvalds } 146316306a61SNeilBrown locks_delete_block(&fl); 14641da177e4SLinus Torvalds 14651da177e4SLinus Torvalds return error; 14661da177e4SLinus Torvalds } 14671da177e4SLinus Torvalds EXPORT_SYMBOL(locks_mandatory_area); 14689e8925b6SJeff Layton #endif /* CONFIG_MANDATORY_FILE_LOCKING */ 14691da177e4SLinus Torvalds 1470778fc546SJ. Bruce Fields static void lease_clear_pending(struct file_lock *fl, int arg) 1471778fc546SJ. Bruce Fields { 1472778fc546SJ. Bruce Fields switch (arg) { 1473778fc546SJ. Bruce Fields case F_UNLCK: 1474778fc546SJ. Bruce Fields fl->fl_flags &= ~FL_UNLOCK_PENDING; 14750a4c9265SGustavo A. R. Silva /* fall through */ 1476778fc546SJ. Bruce Fields case F_RDLCK: 1477778fc546SJ. Bruce Fields fl->fl_flags &= ~FL_DOWNGRADE_PENDING; 1478778fc546SJ. Bruce Fields } 1479778fc546SJ. Bruce Fields } 1480778fc546SJ. Bruce Fields 14811da177e4SLinus Torvalds /* We already had a lease on this file; just change its type */ 14827448cc37SJeff Layton int lease_modify(struct file_lock *fl, int arg, struct list_head *dispose) 14831da177e4SLinus Torvalds { 14841da177e4SLinus Torvalds int error = assign_type(fl, arg); 14851da177e4SLinus Torvalds 14861da177e4SLinus Torvalds if (error) 14871da177e4SLinus Torvalds return error; 1488778fc546SJ. Bruce Fields lease_clear_pending(fl, arg); 14891da177e4SLinus Torvalds locks_wake_up_blocks(fl); 14903b6e2723SFilipe Brandenburger if (arg == F_UNLCK) { 14913b6e2723SFilipe Brandenburger struct file *filp = fl->fl_file; 14923b6e2723SFilipe Brandenburger 14933b6e2723SFilipe Brandenburger f_delown(filp); 14943b6e2723SFilipe Brandenburger filp->f_owner.signum = 0; 149596d6d59cSJ. Bruce Fields fasync_helper(0, fl->fl_file, 0, &fl->fl_fasync); 149696d6d59cSJ. Bruce Fields if (fl->fl_fasync != NULL) { 149796d6d59cSJ. Bruce Fields printk(KERN_ERR "locks_delete_lock: fasync == %p\n", fl->fl_fasync); 149896d6d59cSJ. Bruce Fields fl->fl_fasync = NULL; 149996d6d59cSJ. Bruce Fields } 1500e084c1bdSJeff Layton locks_delete_lock_ctx(fl, dispose); 15013b6e2723SFilipe Brandenburger } 15021da177e4SLinus Torvalds return 0; 15031da177e4SLinus Torvalds } 15041da177e4SLinus Torvalds EXPORT_SYMBOL(lease_modify); 15051da177e4SLinus Torvalds 1506778fc546SJ. Bruce Fields static bool past_time(unsigned long then) 1507778fc546SJ. Bruce Fields { 1508778fc546SJ. Bruce Fields if (!then) 1509778fc546SJ. Bruce Fields /* 0 is a special value meaning "this never expires": */ 1510778fc546SJ. Bruce Fields return false; 1511778fc546SJ. Bruce Fields return time_after(jiffies, then); 1512778fc546SJ. Bruce Fields } 1513778fc546SJ. Bruce Fields 1514c45198edSJeff Layton static void time_out_leases(struct inode *inode, struct list_head *dispose) 15151da177e4SLinus Torvalds { 15168634b51fSJeff Layton struct file_lock_context *ctx = inode->i_flctx; 15178634b51fSJeff Layton struct file_lock *fl, *tmp; 15181da177e4SLinus Torvalds 15196109c850SJeff Layton lockdep_assert_held(&ctx->flc_lock); 1520f82b4b67SJeff Layton 15218634b51fSJeff Layton list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list) { 152262af4f1fSJeff Layton trace_time_out_leases(inode, fl); 1523778fc546SJ. Bruce Fields if (past_time(fl->fl_downgrade_time)) 15247448cc37SJeff Layton lease_modify(fl, F_RDLCK, dispose); 1525778fc546SJ. Bruce Fields if (past_time(fl->fl_break_time)) 15267448cc37SJeff Layton lease_modify(fl, F_UNLCK, dispose); 15271da177e4SLinus Torvalds } 15281da177e4SLinus Torvalds } 15291da177e4SLinus Torvalds 1530df4e8d2cSJ. Bruce Fields static bool leases_conflict(struct file_lock *lease, struct file_lock *breaker) 1531df4e8d2cSJ. Bruce Fields { 1532d51f527fSIra Weiny bool rc; 1533d51f527fSIra Weiny 1534d51f527fSIra Weiny if ((breaker->fl_flags & FL_LAYOUT) != (lease->fl_flags & FL_LAYOUT)) { 1535d51f527fSIra Weiny rc = false; 1536d51f527fSIra Weiny goto trace; 1537d51f527fSIra Weiny } 1538d51f527fSIra Weiny if ((breaker->fl_flags & FL_DELEG) && (lease->fl_flags & FL_LEASE)) { 1539d51f527fSIra Weiny rc = false; 1540d51f527fSIra Weiny goto trace; 1541d51f527fSIra Weiny } 1542d51f527fSIra Weiny 1543d51f527fSIra Weiny rc = locks_conflict(breaker, lease); 1544d51f527fSIra Weiny trace: 1545d51f527fSIra Weiny trace_leases_conflict(rc, lease, breaker); 1546d51f527fSIra Weiny return rc; 1547df4e8d2cSJ. Bruce Fields } 1548df4e8d2cSJ. Bruce Fields 154903d12ddfSJeff Layton static bool 155003d12ddfSJeff Layton any_leases_conflict(struct inode *inode, struct file_lock *breaker) 155103d12ddfSJeff Layton { 15528634b51fSJeff Layton struct file_lock_context *ctx = inode->i_flctx; 155303d12ddfSJeff Layton struct file_lock *fl; 155403d12ddfSJeff Layton 15556109c850SJeff Layton lockdep_assert_held(&ctx->flc_lock); 155603d12ddfSJeff Layton 15578634b51fSJeff Layton list_for_each_entry(fl, &ctx->flc_lease, fl_list) { 155803d12ddfSJeff Layton if (leases_conflict(fl, breaker)) 155903d12ddfSJeff Layton return true; 156003d12ddfSJeff Layton } 156103d12ddfSJeff Layton return false; 156203d12ddfSJeff Layton } 156303d12ddfSJeff Layton 15641da177e4SLinus Torvalds /** 15651da177e4SLinus Torvalds * __break_lease - revoke all outstanding leases on file 15661da177e4SLinus Torvalds * @inode: the inode of the file to return 1567df4e8d2cSJ. Bruce Fields * @mode: O_RDONLY: break only write leases; O_WRONLY or O_RDWR: 1568df4e8d2cSJ. Bruce Fields * break all leases 1569df4e8d2cSJ. Bruce Fields * @type: FL_LEASE: break leases and delegations; FL_DELEG: break 1570df4e8d2cSJ. Bruce Fields * only delegations 15711da177e4SLinus Torvalds * 157287250dd2Sdavid m. richter * break_lease (inlined for speed) has checked there already is at least 157387250dd2Sdavid m. richter * some kind of lock (maybe a lease) on this file. Leases are broken on 157487250dd2Sdavid m. richter * a call to open() or truncate(). This function can sleep unless you 15751da177e4SLinus Torvalds * specified %O_NONBLOCK to your open(). 15761da177e4SLinus Torvalds */ 1577df4e8d2cSJ. Bruce Fields int __break_lease(struct inode *inode, unsigned int mode, unsigned int type) 15781da177e4SLinus Torvalds { 1579778fc546SJ. Bruce Fields int error = 0; 1580128a3785SDmitry Vyukov struct file_lock_context *ctx; 1581a901125cSYan, Zheng struct file_lock *new_fl, *fl, *tmp; 15821da177e4SLinus Torvalds unsigned long break_time; 15838737c930SAl Viro int want_write = (mode & O_ACCMODE) != O_RDONLY; 1584c45198edSJeff Layton LIST_HEAD(dispose); 15851da177e4SLinus Torvalds 15868737c930SAl Viro new_fl = lease_alloc(NULL, want_write ? F_WRLCK : F_RDLCK); 15876d4b9e38SLinus Torvalds if (IS_ERR(new_fl)) 15886d4b9e38SLinus Torvalds return PTR_ERR(new_fl); 1589df4e8d2cSJ. Bruce Fields new_fl->fl_flags = type; 15901da177e4SLinus Torvalds 15918634b51fSJeff Layton /* typically we will check that ctx is non-NULL before calling */ 1592128a3785SDmitry Vyukov ctx = smp_load_acquire(&inode->i_flctx); 15938634b51fSJeff Layton if (!ctx) { 15948634b51fSJeff Layton WARN_ON_ONCE(1); 15958634b51fSJeff Layton return error; 15968634b51fSJeff Layton } 15978634b51fSJeff Layton 159802e525b2SPeter Zijlstra percpu_down_read(&file_rwsem); 15996109c850SJeff Layton spin_lock(&ctx->flc_lock); 16001da177e4SLinus Torvalds 1601c45198edSJeff Layton time_out_leases(inode, &dispose); 16021da177e4SLinus Torvalds 160303d12ddfSJeff Layton if (!any_leases_conflict(inode, new_fl)) 1604df4e8d2cSJ. Bruce Fields goto out; 16051da177e4SLinus Torvalds 16061da177e4SLinus Torvalds break_time = 0; 16071da177e4SLinus Torvalds if (lease_break_time > 0) { 16081da177e4SLinus Torvalds break_time = jiffies + lease_break_time * HZ; 16091da177e4SLinus Torvalds if (break_time == 0) 16101da177e4SLinus Torvalds break_time++; /* so that 0 means no break time */ 16111da177e4SLinus Torvalds } 16121da177e4SLinus Torvalds 1613a901125cSYan, Zheng list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list) { 1614df4e8d2cSJ. Bruce Fields if (!leases_conflict(fl, new_fl)) 1615df4e8d2cSJ. Bruce Fields continue; 1616778fc546SJ. Bruce Fields if (want_write) { 1617778fc546SJ. Bruce Fields if (fl->fl_flags & FL_UNLOCK_PENDING) 1618778fc546SJ. Bruce Fields continue; 1619778fc546SJ. Bruce Fields fl->fl_flags |= FL_UNLOCK_PENDING; 16201da177e4SLinus Torvalds fl->fl_break_time = break_time; 1621778fc546SJ. Bruce Fields } else { 16228634b51fSJeff Layton if (lease_breaking(fl)) 1623778fc546SJ. Bruce Fields continue; 1624778fc546SJ. Bruce Fields fl->fl_flags |= FL_DOWNGRADE_PENDING; 1625778fc546SJ. Bruce Fields fl->fl_downgrade_time = break_time; 16261da177e4SLinus Torvalds } 16274d01b7f5SJeff Layton if (fl->fl_lmops->lm_break(fl)) 1628e084c1bdSJeff Layton locks_delete_lock_ctx(fl, &dispose); 16291da177e4SLinus Torvalds } 16301da177e4SLinus Torvalds 16318634b51fSJeff Layton if (list_empty(&ctx->flc_lease)) 16324d01b7f5SJeff Layton goto out; 16334d01b7f5SJeff Layton 1634843c6b2fSJeff Layton if (mode & O_NONBLOCK) { 163562af4f1fSJeff Layton trace_break_lease_noblock(inode, new_fl); 16361da177e4SLinus Torvalds error = -EWOULDBLOCK; 16371da177e4SLinus Torvalds goto out; 16381da177e4SLinus Torvalds } 16391da177e4SLinus Torvalds 16401da177e4SLinus Torvalds restart: 16418634b51fSJeff Layton fl = list_first_entry(&ctx->flc_lease, struct file_lock, fl_list); 16428634b51fSJeff Layton break_time = fl->fl_break_time; 1643f1c6bb2cSJeff Layton if (break_time != 0) 16441da177e4SLinus Torvalds break_time -= jiffies; 16451da177e4SLinus Torvalds if (break_time == 0) 16461da177e4SLinus Torvalds break_time++; 1647fd7732e0SNeilBrown locks_insert_block(fl, new_fl, leases_conflict); 164862af4f1fSJeff Layton trace_break_lease_block(inode, new_fl); 16496109c850SJeff Layton spin_unlock(&ctx->flc_lock); 165002e525b2SPeter Zijlstra percpu_up_read(&file_rwsem); 1651aba37660SPeter Zijlstra 1652c45198edSJeff Layton locks_dispose_list(&dispose); 16534321e01eSMatthew Wilcox error = wait_event_interruptible_timeout(new_fl->fl_wait, 1654ada5c1daSNeilBrown !new_fl->fl_blocker, break_time); 1655aba37660SPeter Zijlstra 165602e525b2SPeter Zijlstra percpu_down_read(&file_rwsem); 16576109c850SJeff Layton spin_lock(&ctx->flc_lock); 165862af4f1fSJeff Layton trace_break_lease_unblock(inode, new_fl); 16591c8c601aSJeff Layton locks_delete_block(new_fl); 16601da177e4SLinus Torvalds if (error >= 0) { 1661778fc546SJ. Bruce Fields /* 1662778fc546SJ. Bruce Fields * Wait for the next conflicting lease that has not been 1663778fc546SJ. Bruce Fields * broken yet 1664778fc546SJ. Bruce Fields */ 166503d12ddfSJeff Layton if (error == 0) 166603d12ddfSJeff Layton time_out_leases(inode, &dispose); 166703d12ddfSJeff Layton if (any_leases_conflict(inode, new_fl)) 16681da177e4SLinus Torvalds goto restart; 16691da177e4SLinus Torvalds error = 0; 16701da177e4SLinus Torvalds } 16711da177e4SLinus Torvalds out: 16726109c850SJeff Layton spin_unlock(&ctx->flc_lock); 167302e525b2SPeter Zijlstra percpu_up_read(&file_rwsem); 1674c45198edSJeff Layton locks_dispose_list(&dispose); 16751da177e4SLinus Torvalds locks_free_lock(new_fl); 16761da177e4SLinus Torvalds return error; 16771da177e4SLinus Torvalds } 16781da177e4SLinus Torvalds EXPORT_SYMBOL(__break_lease); 16791da177e4SLinus Torvalds 16801da177e4SLinus Torvalds /** 168176c47948SAmir Goldstein * lease_get_mtime - update modified time of an inode with exclusive lease 16821da177e4SLinus Torvalds * @inode: the inode 168376c47948SAmir Goldstein * @time: pointer to a timespec which contains the last modified time 16841da177e4SLinus Torvalds * 16851da177e4SLinus Torvalds * This is to force NFS clients to flush their caches for files with 16861da177e4SLinus Torvalds * exclusive leases. The justification is that if someone has an 1687a6b91919SRandy Dunlap * exclusive lease, then they could be modifying it. 16881da177e4SLinus Torvalds */ 168995582b00SDeepa Dinamani void lease_get_mtime(struct inode *inode, struct timespec64 *time) 16901da177e4SLinus Torvalds { 1691bfe86024SJeff Layton bool has_lease = false; 1692128a3785SDmitry Vyukov struct file_lock_context *ctx; 16938634b51fSJeff Layton struct file_lock *fl; 1694bfe86024SJeff Layton 1695128a3785SDmitry Vyukov ctx = smp_load_acquire(&inode->i_flctx); 16968634b51fSJeff Layton if (ctx && !list_empty_careful(&ctx->flc_lease)) { 16976109c850SJeff Layton spin_lock(&ctx->flc_lock); 16988ace5dfbSGeliang Tang fl = list_first_entry_or_null(&ctx->flc_lease, 16998634b51fSJeff Layton struct file_lock, fl_list); 17008ace5dfbSGeliang Tang if (fl && (fl->fl_type == F_WRLCK)) 1701bfe86024SJeff Layton has_lease = true; 17026109c850SJeff Layton spin_unlock(&ctx->flc_lock); 1703bfe86024SJeff Layton } 1704bfe86024SJeff Layton 1705bfe86024SJeff Layton if (has_lease) 1706c2050a45SDeepa Dinamani *time = current_time(inode); 17071da177e4SLinus Torvalds } 17081da177e4SLinus Torvalds EXPORT_SYMBOL(lease_get_mtime); 17091da177e4SLinus Torvalds 17101da177e4SLinus Torvalds /** 17111da177e4SLinus Torvalds * fcntl_getlease - Enquire what lease is currently active 17121da177e4SLinus Torvalds * @filp: the file 17131da177e4SLinus Torvalds * 17141da177e4SLinus Torvalds * The value returned by this function will be one of 17151da177e4SLinus Torvalds * (if no lease break is pending): 17161da177e4SLinus Torvalds * 17171da177e4SLinus Torvalds * %F_RDLCK to indicate a shared lease is held. 17181da177e4SLinus Torvalds * 17191da177e4SLinus Torvalds * %F_WRLCK to indicate an exclusive lease is held. 17201da177e4SLinus Torvalds * 17211da177e4SLinus Torvalds * %F_UNLCK to indicate no lease is held. 17221da177e4SLinus Torvalds * 17231da177e4SLinus Torvalds * (if a lease break is pending): 17241da177e4SLinus Torvalds * 17251da177e4SLinus Torvalds * %F_RDLCK to indicate an exclusive lease needs to be 17261da177e4SLinus Torvalds * changed to a shared lease (or removed). 17271da177e4SLinus Torvalds * 17281da177e4SLinus Torvalds * %F_UNLCK to indicate the lease needs to be removed. 17291da177e4SLinus Torvalds * 17301da177e4SLinus Torvalds * XXX: sfr & willy disagree over whether F_INPROGRESS 17311da177e4SLinus Torvalds * should be returned to userspace. 17321da177e4SLinus Torvalds */ 17331da177e4SLinus Torvalds int fcntl_getlease(struct file *filp) 17341da177e4SLinus Torvalds { 17351da177e4SLinus Torvalds struct file_lock *fl; 1736c568d683SMiklos Szeredi struct inode *inode = locks_inode(filp); 1737128a3785SDmitry Vyukov struct file_lock_context *ctx; 17381da177e4SLinus Torvalds int type = F_UNLCK; 1739c45198edSJeff Layton LIST_HEAD(dispose); 17401da177e4SLinus Torvalds 1741128a3785SDmitry Vyukov ctx = smp_load_acquire(&inode->i_flctx); 17428634b51fSJeff Layton if (ctx && !list_empty_careful(&ctx->flc_lease)) { 174302e525b2SPeter Zijlstra percpu_down_read(&file_rwsem); 17446109c850SJeff Layton spin_lock(&ctx->flc_lock); 1745c568d683SMiklos Szeredi time_out_leases(inode, &dispose); 17468634b51fSJeff Layton list_for_each_entry(fl, &ctx->flc_lease, fl_list) { 17478634b51fSJeff Layton if (fl->fl_file != filp) 17488634b51fSJeff Layton continue; 1749778fc546SJ. Bruce Fields type = target_leasetype(fl); 17501da177e4SLinus Torvalds break; 17511da177e4SLinus Torvalds } 17526109c850SJeff Layton spin_unlock(&ctx->flc_lock); 175302e525b2SPeter Zijlstra percpu_up_read(&file_rwsem); 17545f43086bSPeter Zijlstra 1755c45198edSJeff Layton locks_dispose_list(&dispose); 17568634b51fSJeff Layton } 17571da177e4SLinus Torvalds return type; 17581da177e4SLinus Torvalds } 17591da177e4SLinus Torvalds 176024cbe784SJeff Layton /** 1761387e3746SAmir Goldstein * check_conflicting_open - see if the given file points to an inode that has 176224cbe784SJeff Layton * an existing open that would conflict with the 176324cbe784SJeff Layton * desired lease. 1764387e3746SAmir Goldstein * @filp: file to check 176524cbe784SJeff Layton * @arg: type of lease that we're trying to acquire 17667fadc59cSRandy Dunlap * @flags: current lock flags 176724cbe784SJeff Layton * 176824cbe784SJeff Layton * Check to see if there's an existing open fd on this file that would 176924cbe784SJeff Layton * conflict with the lease we're trying to set. 177024cbe784SJeff Layton */ 177124cbe784SJeff Layton static int 1772387e3746SAmir Goldstein check_conflicting_open(struct file *filp, const long arg, int flags) 177324cbe784SJeff Layton { 1774387e3746SAmir Goldstein struct inode *inode = locks_inode(filp); 1775387e3746SAmir Goldstein int self_wcount = 0, self_rcount = 0; 177624cbe784SJeff Layton 177711afe9f7SChristoph Hellwig if (flags & FL_LAYOUT) 177811afe9f7SChristoph Hellwig return 0; 177911afe9f7SChristoph Hellwig 1780387e3746SAmir Goldstein if (arg == F_RDLCK) 1781387e3746SAmir Goldstein return inode_is_open_for_write(inode) ? -EAGAIN : 0; 1782387e3746SAmir Goldstein else if (arg != F_WRLCK) 1783387e3746SAmir Goldstein return 0; 1784387e3746SAmir Goldstein 1785387e3746SAmir Goldstein /* 1786387e3746SAmir Goldstein * Make sure that only read/write count is from lease requestor. 1787387e3746SAmir Goldstein * Note that this will result in denying write leases when i_writecount 1788387e3746SAmir Goldstein * is negative, which is what we want. (We shouldn't grant write leases 1789387e3746SAmir Goldstein * on files open for execution.) 1790387e3746SAmir Goldstein */ 1791387e3746SAmir Goldstein if (filp->f_mode & FMODE_WRITE) 1792387e3746SAmir Goldstein self_wcount = 1; 1793387e3746SAmir Goldstein else if (filp->f_mode & FMODE_READ) 1794387e3746SAmir Goldstein self_rcount = 1; 1795387e3746SAmir Goldstein 1796387e3746SAmir Goldstein if (atomic_read(&inode->i_writecount) != self_wcount || 1797387e3746SAmir Goldstein atomic_read(&inode->i_readcount) != self_rcount) 179824cbe784SJeff Layton return -EAGAIN; 179924cbe784SJeff Layton 1800387e3746SAmir Goldstein return 0; 180124cbe784SJeff Layton } 180224cbe784SJeff Layton 1803e6f5c789SJeff Layton static int 1804e6f5c789SJeff Layton generic_add_lease(struct file *filp, long arg, struct file_lock **flp, void **priv) 18051da177e4SLinus Torvalds { 18068634b51fSJeff Layton struct file_lock *fl, *my_fl = NULL, *lease; 1807387e3746SAmir Goldstein struct inode *inode = locks_inode(filp); 18088634b51fSJeff Layton struct file_lock_context *ctx; 1809df4e8d2cSJ. Bruce Fields bool is_deleg = (*flp)->fl_flags & FL_DELEG; 1810c1f24ef4SJ. Bruce Fields int error; 1811c45198edSJeff Layton LIST_HEAD(dispose); 18121da177e4SLinus Torvalds 1813096657b6SJ. Bruce Fields lease = *flp; 181462af4f1fSJeff Layton trace_generic_add_lease(inode, lease); 181562af4f1fSJeff Layton 18165c1c669aSJeff Layton /* Note that arg is never F_UNLCK here */ 18175c1c669aSJeff Layton ctx = locks_get_lock_context(inode, arg); 18188634b51fSJeff Layton if (!ctx) 18198634b51fSJeff Layton return -ENOMEM; 18208634b51fSJeff Layton 1821df4e8d2cSJ. Bruce Fields /* 1822df4e8d2cSJ. Bruce Fields * In the delegation case we need mutual exclusion with 1823df4e8d2cSJ. Bruce Fields * a number of operations that take the i_mutex. We trylock 1824df4e8d2cSJ. Bruce Fields * because delegations are an optional optimization, and if 1825df4e8d2cSJ. Bruce Fields * there's some chance of a conflict--we'd rather not 1826df4e8d2cSJ. Bruce Fields * bother, maybe that's a sign this just isn't a good file to 1827df4e8d2cSJ. Bruce Fields * hand out a delegation on. 1828df4e8d2cSJ. Bruce Fields */ 18295955102cSAl Viro if (is_deleg && !inode_trylock(inode)) 1830df4e8d2cSJ. Bruce Fields return -EAGAIN; 1831df4e8d2cSJ. Bruce Fields 1832df4e8d2cSJ. Bruce Fields if (is_deleg && arg == F_WRLCK) { 1833df4e8d2cSJ. Bruce Fields /* Write delegations are not currently supported: */ 18345955102cSAl Viro inode_unlock(inode); 1835df4e8d2cSJ. Bruce Fields WARN_ON_ONCE(1); 1836df4e8d2cSJ. Bruce Fields return -EINVAL; 1837df4e8d2cSJ. Bruce Fields } 1838096657b6SJ. Bruce Fields 183902e525b2SPeter Zijlstra percpu_down_read(&file_rwsem); 18406109c850SJeff Layton spin_lock(&ctx->flc_lock); 1841c45198edSJeff Layton time_out_leases(inode, &dispose); 1842387e3746SAmir Goldstein error = check_conflicting_open(filp, arg, lease->fl_flags); 184324cbe784SJeff Layton if (error) 18441da177e4SLinus Torvalds goto out; 184585c59580SPavel Emelyanov 18461da177e4SLinus Torvalds /* 18471da177e4SLinus Torvalds * At this point, we know that if there is an exclusive 18481da177e4SLinus Torvalds * lease on this file, then we hold it on this filp 18491da177e4SLinus Torvalds * (otherwise our open of this file would have blocked). 18501da177e4SLinus Torvalds * And if we are trying to acquire an exclusive lease, 18511da177e4SLinus Torvalds * then the file is not open by anyone (including us) 18521da177e4SLinus Torvalds * except for this filp. 18531da177e4SLinus Torvalds */ 1854c1f24ef4SJ. Bruce Fields error = -EAGAIN; 18558634b51fSJeff Layton list_for_each_entry(fl, &ctx->flc_lease, fl_list) { 18562ab99ee1SChristoph Hellwig if (fl->fl_file == filp && 18572ab99ee1SChristoph Hellwig fl->fl_owner == lease->fl_owner) { 18588634b51fSJeff Layton my_fl = fl; 1859c1f24ef4SJ. Bruce Fields continue; 18601da177e4SLinus Torvalds } 18618634b51fSJeff Layton 1862c1f24ef4SJ. Bruce Fields /* 1863c1f24ef4SJ. Bruce Fields * No exclusive leases if someone else has a lease on 1864c1f24ef4SJ. Bruce Fields * this file: 1865c1f24ef4SJ. Bruce Fields */ 1866c1f24ef4SJ. Bruce Fields if (arg == F_WRLCK) 18671da177e4SLinus Torvalds goto out; 1868c1f24ef4SJ. Bruce Fields /* 1869c1f24ef4SJ. Bruce Fields * Modifying our existing lease is OK, but no getting a 1870c1f24ef4SJ. Bruce Fields * new lease if someone else is opening for write: 1871c1f24ef4SJ. Bruce Fields */ 1872c1f24ef4SJ. Bruce Fields if (fl->fl_flags & FL_UNLOCK_PENDING) 1873c1f24ef4SJ. Bruce Fields goto out; 1874c1f24ef4SJ. Bruce Fields } 18751da177e4SLinus Torvalds 18768634b51fSJeff Layton if (my_fl != NULL) { 18770164bf02SJeff Layton lease = my_fl; 18780164bf02SJeff Layton error = lease->fl_lmops->lm_change(lease, arg, &dispose); 18791c7dd2ffSJeff Layton if (error) 18801da177e4SLinus Torvalds goto out; 18811c7dd2ffSJeff Layton goto out_setup; 18821da177e4SLinus Torvalds } 18831da177e4SLinus Torvalds 18841da177e4SLinus Torvalds error = -EINVAL; 18851da177e4SLinus Torvalds if (!leases_enable) 18861da177e4SLinus Torvalds goto out; 18871da177e4SLinus Torvalds 1888e084c1bdSJeff Layton locks_insert_lock_ctx(lease, &ctx->flc_lease); 188924cbe784SJeff Layton /* 189024cbe784SJeff Layton * The check in break_lease() is lockless. It's possible for another 189124cbe784SJeff Layton * open to race in after we did the earlier check for a conflicting 189224cbe784SJeff Layton * open but before the lease was inserted. Check again for a 189324cbe784SJeff Layton * conflicting open and cancel the lease if there is one. 189424cbe784SJeff Layton * 189524cbe784SJeff Layton * We also add a barrier here to ensure that the insertion of the lock 189624cbe784SJeff Layton * precedes these checks. 189724cbe784SJeff Layton */ 189824cbe784SJeff Layton smp_mb(); 1899387e3746SAmir Goldstein error = check_conflicting_open(filp, arg, lease->fl_flags); 19008634b51fSJeff Layton if (error) { 1901e084c1bdSJeff Layton locks_unlink_lock_ctx(lease); 19028634b51fSJeff Layton goto out; 19038634b51fSJeff Layton } 19041c7dd2ffSJeff Layton 19051c7dd2ffSJeff Layton out_setup: 19061c7dd2ffSJeff Layton if (lease->fl_lmops->lm_setup) 19071c7dd2ffSJeff Layton lease->fl_lmops->lm_setup(lease, priv); 19081da177e4SLinus Torvalds out: 19096109c850SJeff Layton spin_unlock(&ctx->flc_lock); 191002e525b2SPeter Zijlstra percpu_up_read(&file_rwsem); 1911c45198edSJeff Layton locks_dispose_list(&dispose); 1912df4e8d2cSJ. Bruce Fields if (is_deleg) 19135955102cSAl Viro inode_unlock(inode); 19148634b51fSJeff Layton if (!error && !my_fl) 19151c7dd2ffSJeff Layton *flp = NULL; 19161da177e4SLinus Torvalds return error; 19171da177e4SLinus Torvalds } 19188335ebd9SJ. Bruce Fields 19192ab99ee1SChristoph Hellwig static int generic_delete_lease(struct file *filp, void *owner) 19208335ebd9SJ. Bruce Fields { 19210efaa7e8SJeff Layton int error = -EAGAIN; 19228634b51fSJeff Layton struct file_lock *fl, *victim = NULL; 1923c568d683SMiklos Szeredi struct inode *inode = locks_inode(filp); 1924128a3785SDmitry Vyukov struct file_lock_context *ctx; 1925c45198edSJeff Layton LIST_HEAD(dispose); 19268335ebd9SJ. Bruce Fields 1927128a3785SDmitry Vyukov ctx = smp_load_acquire(&inode->i_flctx); 19288634b51fSJeff Layton if (!ctx) { 19298634b51fSJeff Layton trace_generic_delete_lease(inode, NULL); 19308634b51fSJeff Layton return error; 19318634b51fSJeff Layton } 19328634b51fSJeff Layton 193302e525b2SPeter Zijlstra percpu_down_read(&file_rwsem); 19346109c850SJeff Layton spin_lock(&ctx->flc_lock); 19358634b51fSJeff Layton list_for_each_entry(fl, &ctx->flc_lease, fl_list) { 19362ab99ee1SChristoph Hellwig if (fl->fl_file == filp && 19372ab99ee1SChristoph Hellwig fl->fl_owner == owner) { 19388634b51fSJeff Layton victim = fl; 19390efaa7e8SJeff Layton break; 19408335ebd9SJ. Bruce Fields } 19418634b51fSJeff Layton } 1942a9b1b455SJeff Layton trace_generic_delete_lease(inode, victim); 19438634b51fSJeff Layton if (victim) 19447448cc37SJeff Layton error = fl->fl_lmops->lm_change(victim, F_UNLCK, &dispose); 19456109c850SJeff Layton spin_unlock(&ctx->flc_lock); 194602e525b2SPeter Zijlstra percpu_up_read(&file_rwsem); 1947c45198edSJeff Layton locks_dispose_list(&dispose); 19480efaa7e8SJeff Layton return error; 19498335ebd9SJ. Bruce Fields } 19508335ebd9SJ. Bruce Fields 19511da177e4SLinus Torvalds /** 19521da177e4SLinus Torvalds * generic_setlease - sets a lease on an open file 19531da177e4SLinus Torvalds * @filp: file pointer 19541da177e4SLinus Torvalds * @arg: type of lease to obtain 19551da177e4SLinus Torvalds * @flp: input - file_lock to use, output - file_lock inserted 19561c7dd2ffSJeff Layton * @priv: private data for lm_setup (may be NULL if lm_setup 19571c7dd2ffSJeff Layton * doesn't require it) 19581da177e4SLinus Torvalds * 19591da177e4SLinus Torvalds * The (input) flp->fl_lmops->lm_break function is required 19601da177e4SLinus Torvalds * by break_lease(). 19611da177e4SLinus Torvalds */ 1962e6f5c789SJeff Layton int generic_setlease(struct file *filp, long arg, struct file_lock **flp, 1963e6f5c789SJeff Layton void **priv) 19641da177e4SLinus Torvalds { 1965c568d683SMiklos Szeredi struct inode *inode = locks_inode(filp); 19668335ebd9SJ. Bruce Fields int error; 19671da177e4SLinus Torvalds 19688e96e3b7SEric W. Biederman if ((!uid_eq(current_fsuid(), inode->i_uid)) && !capable(CAP_LEASE)) 19698335ebd9SJ. Bruce Fields return -EACCES; 19701da177e4SLinus Torvalds if (!S_ISREG(inode->i_mode)) 19718335ebd9SJ. Bruce Fields return -EINVAL; 19721da177e4SLinus Torvalds error = security_file_lock(filp, arg); 19731da177e4SLinus Torvalds if (error) 19748335ebd9SJ. Bruce Fields return error; 19751da177e4SLinus Torvalds 19768335ebd9SJ. Bruce Fields switch (arg) { 19778335ebd9SJ. Bruce Fields case F_UNLCK: 19782ab99ee1SChristoph Hellwig return generic_delete_lease(filp, *priv); 19798335ebd9SJ. Bruce Fields case F_RDLCK: 19808335ebd9SJ. Bruce Fields case F_WRLCK: 19810efaa7e8SJeff Layton if (!(*flp)->fl_lmops->lm_break) { 19820efaa7e8SJeff Layton WARN_ON_ONCE(1); 19830efaa7e8SJeff Layton return -ENOLCK; 19840efaa7e8SJeff Layton } 198511afe9f7SChristoph Hellwig 1986e6f5c789SJeff Layton return generic_add_lease(filp, arg, flp, priv); 19878335ebd9SJ. Bruce Fields default: 19888d657eb3SDave Jones return -EINVAL; 19891da177e4SLinus Torvalds } 19901da177e4SLinus Torvalds } 19910af1a450SChristoph Hellwig EXPORT_SYMBOL(generic_setlease); 19921da177e4SLinus Torvalds 1993*18f6622eSJeff Layton #if IS_ENABLED(CONFIG_SRCU) 1994*18f6622eSJeff Layton /* 1995*18f6622eSJeff Layton * Kernel subsystems can register to be notified on any attempt to set 1996*18f6622eSJeff Layton * a new lease with the lease_notifier_chain. This is used by (e.g.) nfsd 1997*18f6622eSJeff Layton * to close files that it may have cached when there is an attempt to set a 1998*18f6622eSJeff Layton * conflicting lease. 1999*18f6622eSJeff Layton */ 2000*18f6622eSJeff Layton static struct srcu_notifier_head lease_notifier_chain; 2001*18f6622eSJeff Layton 2002*18f6622eSJeff Layton static inline void 2003*18f6622eSJeff Layton lease_notifier_chain_init(void) 2004*18f6622eSJeff Layton { 2005*18f6622eSJeff Layton srcu_init_notifier_head(&lease_notifier_chain); 2006*18f6622eSJeff Layton } 2007*18f6622eSJeff Layton 2008*18f6622eSJeff Layton static inline void 2009*18f6622eSJeff Layton setlease_notifier(long arg, struct file_lock *lease) 2010*18f6622eSJeff Layton { 2011*18f6622eSJeff Layton if (arg != F_UNLCK) 2012*18f6622eSJeff Layton srcu_notifier_call_chain(&lease_notifier_chain, arg, lease); 2013*18f6622eSJeff Layton } 2014*18f6622eSJeff Layton 2015*18f6622eSJeff Layton int lease_register_notifier(struct notifier_block *nb) 2016*18f6622eSJeff Layton { 2017*18f6622eSJeff Layton return srcu_notifier_chain_register(&lease_notifier_chain, nb); 2018*18f6622eSJeff Layton } 2019*18f6622eSJeff Layton EXPORT_SYMBOL_GPL(lease_register_notifier); 2020*18f6622eSJeff Layton 2021*18f6622eSJeff Layton void lease_unregister_notifier(struct notifier_block *nb) 2022*18f6622eSJeff Layton { 2023*18f6622eSJeff Layton srcu_notifier_chain_unregister(&lease_notifier_chain, nb); 2024*18f6622eSJeff Layton } 2025*18f6622eSJeff Layton EXPORT_SYMBOL_GPL(lease_unregister_notifier); 2026*18f6622eSJeff Layton 2027*18f6622eSJeff Layton #else /* !IS_ENABLED(CONFIG_SRCU) */ 2028*18f6622eSJeff Layton static inline void 2029*18f6622eSJeff Layton lease_notifier_chain_init(void) 2030*18f6622eSJeff Layton { 2031*18f6622eSJeff Layton } 2032*18f6622eSJeff Layton 2033*18f6622eSJeff Layton static inline void 2034*18f6622eSJeff Layton setlease_notifier(long arg, struct file_lock *lease) 2035*18f6622eSJeff Layton { 2036*18f6622eSJeff Layton } 2037*18f6622eSJeff Layton 2038*18f6622eSJeff Layton int lease_register_notifier(struct notifier_block *nb) 2039*18f6622eSJeff Layton { 2040*18f6622eSJeff Layton return 0; 2041*18f6622eSJeff Layton } 2042*18f6622eSJeff Layton EXPORT_SYMBOL_GPL(lease_register_notifier); 2043*18f6622eSJeff Layton 2044*18f6622eSJeff Layton void lease_unregister_notifier(struct notifier_block *nb) 2045*18f6622eSJeff Layton { 2046*18f6622eSJeff Layton } 2047*18f6622eSJeff Layton EXPORT_SYMBOL_GPL(lease_unregister_notifier); 2048*18f6622eSJeff Layton 2049*18f6622eSJeff Layton #endif /* IS_ENABLED(CONFIG_SRCU) */ 2050*18f6622eSJeff Layton 20511da177e4SLinus Torvalds /** 2052a9933ceaSJ. Bruce Fields * vfs_setlease - sets a lease on an open file 20531da177e4SLinus Torvalds * @filp: file pointer 20541da177e4SLinus Torvalds * @arg: type of lease to obtain 2055e51673aaSJeff Layton * @lease: file_lock to use when adding a lease 20561c7dd2ffSJeff Layton * @priv: private info for lm_setup when adding a lease (may be 20571c7dd2ffSJeff Layton * NULL if lm_setup doesn't require it) 20581da177e4SLinus Torvalds * 2059e51673aaSJeff Layton * Call this to establish a lease on the file. The "lease" argument is not 2060e51673aaSJeff Layton * used for F_UNLCK requests and may be NULL. For commands that set or alter 206180b79dd0SMauro Carvalho Chehab * an existing lease, the ``(*lease)->fl_lmops->lm_break`` operation must be 206280b79dd0SMauro Carvalho Chehab * set; if not, this function will return -ENOLCK (and generate a scary-looking 2063e51673aaSJeff Layton * stack trace). 20641c7dd2ffSJeff Layton * 20651c7dd2ffSJeff Layton * The "priv" pointer is passed directly to the lm_setup function as-is. It 20661c7dd2ffSJeff Layton * may be NULL if the lm_setup operation doesn't require it. 20671da177e4SLinus Torvalds */ 2068e6f5c789SJeff Layton int 2069e6f5c789SJeff Layton vfs_setlease(struct file *filp, long arg, struct file_lock **lease, void **priv) 20701da177e4SLinus Torvalds { 2071*18f6622eSJeff Layton if (lease) 2072*18f6622eSJeff Layton setlease_notifier(arg, *lease); 2073de2a4a50SMiklos Szeredi if (filp->f_op->setlease) 2074f82b4b67SJeff Layton return filp->f_op->setlease(filp, arg, lease, priv); 20751c7dd2ffSJeff Layton else 2076f82b4b67SJeff Layton return generic_setlease(filp, arg, lease, priv); 20771da177e4SLinus Torvalds } 2078a9933ceaSJ. Bruce Fields EXPORT_SYMBOL_GPL(vfs_setlease); 20791da177e4SLinus Torvalds 20800ceaf6c7SJ. Bruce Fields static int do_fcntl_add_lease(unsigned int fd, struct file *filp, long arg) 20811da177e4SLinus Torvalds { 20821c7dd2ffSJeff Layton struct file_lock *fl; 2083f7347ce4SLinus Torvalds struct fasync_struct *new; 20841da177e4SLinus Torvalds int error; 20851da177e4SLinus Torvalds 2086c5b1f0d9SArnd Bergmann fl = lease_alloc(filp, arg); 2087c5b1f0d9SArnd Bergmann if (IS_ERR(fl)) 2088c5b1f0d9SArnd Bergmann return PTR_ERR(fl); 20891da177e4SLinus Torvalds 2090f7347ce4SLinus Torvalds new = fasync_alloc(); 2091f7347ce4SLinus Torvalds if (!new) { 2092f7347ce4SLinus Torvalds locks_free_lock(fl); 2093f7347ce4SLinus Torvalds return -ENOMEM; 2094f7347ce4SLinus Torvalds } 20951c7dd2ffSJeff Layton new->fa_fd = fd; 20961da177e4SLinus Torvalds 20971c7dd2ffSJeff Layton error = vfs_setlease(filp, arg, &fl, (void **)&new); 20982dfb928fSJeff Layton if (fl) 20992dfb928fSJeff Layton locks_free_lock(fl); 2100f7347ce4SLinus Torvalds if (new) 2101f7347ce4SLinus Torvalds fasync_free(new); 21021da177e4SLinus Torvalds return error; 21031da177e4SLinus Torvalds } 21041da177e4SLinus Torvalds 21051da177e4SLinus Torvalds /** 21060ceaf6c7SJ. Bruce Fields * fcntl_setlease - sets a lease on an open file 21070ceaf6c7SJ. Bruce Fields * @fd: open file descriptor 21080ceaf6c7SJ. Bruce Fields * @filp: file pointer 21090ceaf6c7SJ. Bruce Fields * @arg: type of lease to obtain 21100ceaf6c7SJ. Bruce Fields * 21110ceaf6c7SJ. Bruce Fields * Call this fcntl to establish a lease on the file. 21120ceaf6c7SJ. Bruce Fields * Note that you also need to call %F_SETSIG to 21130ceaf6c7SJ. Bruce Fields * receive a signal when the lease is broken. 21140ceaf6c7SJ. Bruce Fields */ 21150ceaf6c7SJ. Bruce Fields int fcntl_setlease(unsigned int fd, struct file *filp, long arg) 21160ceaf6c7SJ. Bruce Fields { 21170ceaf6c7SJ. Bruce Fields if (arg == F_UNLCK) 21182ab99ee1SChristoph Hellwig return vfs_setlease(filp, F_UNLCK, NULL, (void **)&filp); 21190ceaf6c7SJ. Bruce Fields return do_fcntl_add_lease(fd, filp, arg); 21200ceaf6c7SJ. Bruce Fields } 21210ceaf6c7SJ. Bruce Fields 21220ceaf6c7SJ. Bruce Fields /** 212329d01b22SJeff Layton * flock_lock_inode_wait - Apply a FLOCK-style lock to a file 212429d01b22SJeff Layton * @inode: inode of the file to apply to 21251da177e4SLinus Torvalds * @fl: The lock to be applied 21261da177e4SLinus Torvalds * 212729d01b22SJeff Layton * Apply a FLOCK style lock request to an inode. 21281da177e4SLinus Torvalds */ 2129616fb38fSBenjamin Coddington static int flock_lock_inode_wait(struct inode *inode, struct file_lock *fl) 21301da177e4SLinus Torvalds { 21311da177e4SLinus Torvalds int error; 21321da177e4SLinus Torvalds might_sleep(); 21331da177e4SLinus Torvalds for (;;) { 213429d01b22SJeff Layton error = flock_lock_inode(inode, fl); 2135bde74e4bSMiklos Szeredi if (error != FILE_LOCK_DEFERRED) 21361da177e4SLinus Torvalds break; 2137ada5c1daSNeilBrown error = wait_event_interruptible(fl->fl_wait, !fl->fl_blocker); 213816306a61SNeilBrown if (error) 21391da177e4SLinus Torvalds break; 21401da177e4SLinus Torvalds } 214116306a61SNeilBrown locks_delete_block(fl); 21421da177e4SLinus Torvalds return error; 21431da177e4SLinus Torvalds } 21441da177e4SLinus Torvalds 214529d01b22SJeff Layton /** 2146e55c34a6SBenjamin Coddington * locks_lock_inode_wait - Apply a lock to an inode 2147e55c34a6SBenjamin Coddington * @inode: inode of the file to apply to 2148e55c34a6SBenjamin Coddington * @fl: The lock to be applied 2149e55c34a6SBenjamin Coddington * 2150e55c34a6SBenjamin Coddington * Apply a POSIX or FLOCK style lock request to an inode. 2151e55c34a6SBenjamin Coddington */ 2152e55c34a6SBenjamin Coddington int locks_lock_inode_wait(struct inode *inode, struct file_lock *fl) 2153e55c34a6SBenjamin Coddington { 2154e55c34a6SBenjamin Coddington int res = 0; 2155e55c34a6SBenjamin Coddington switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) { 2156e55c34a6SBenjamin Coddington case FL_POSIX: 2157e55c34a6SBenjamin Coddington res = posix_lock_inode_wait(inode, fl); 2158e55c34a6SBenjamin Coddington break; 2159e55c34a6SBenjamin Coddington case FL_FLOCK: 2160e55c34a6SBenjamin Coddington res = flock_lock_inode_wait(inode, fl); 2161e55c34a6SBenjamin Coddington break; 2162e55c34a6SBenjamin Coddington default: 2163e55c34a6SBenjamin Coddington BUG(); 2164e55c34a6SBenjamin Coddington } 2165e55c34a6SBenjamin Coddington return res; 2166e55c34a6SBenjamin Coddington } 2167e55c34a6SBenjamin Coddington EXPORT_SYMBOL(locks_lock_inode_wait); 2168e55c34a6SBenjamin Coddington 2169e55c34a6SBenjamin Coddington /** 21701da177e4SLinus Torvalds * sys_flock: - flock() system call. 21711da177e4SLinus Torvalds * @fd: the file descriptor to lock. 21721da177e4SLinus Torvalds * @cmd: the type of lock to apply. 21731da177e4SLinus Torvalds * 21741da177e4SLinus Torvalds * Apply a %FL_FLOCK style lock to an open file descriptor. 217580b79dd0SMauro Carvalho Chehab * The @cmd can be one of: 21761da177e4SLinus Torvalds * 217780b79dd0SMauro Carvalho Chehab * - %LOCK_SH -- a shared lock. 217880b79dd0SMauro Carvalho Chehab * - %LOCK_EX -- an exclusive lock. 217980b79dd0SMauro Carvalho Chehab * - %LOCK_UN -- remove an existing lock. 218080b79dd0SMauro Carvalho Chehab * - %LOCK_MAND -- a 'mandatory' flock. 218180b79dd0SMauro Carvalho Chehab * This exists to emulate Windows Share Modes. 21821da177e4SLinus Torvalds * 21831da177e4SLinus Torvalds * %LOCK_MAND can be combined with %LOCK_READ or %LOCK_WRITE to allow other 21841da177e4SLinus Torvalds * processes read and write access respectively. 21851da177e4SLinus Torvalds */ 2186002c8976SHeiko Carstens SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd) 21871da177e4SLinus Torvalds { 21882903ff01SAl Viro struct fd f = fdget(fd); 21891da177e4SLinus Torvalds struct file_lock *lock; 21901da177e4SLinus Torvalds int can_sleep, unlock; 21911da177e4SLinus Torvalds int error; 21921da177e4SLinus Torvalds 21931da177e4SLinus Torvalds error = -EBADF; 21942903ff01SAl Viro if (!f.file) 21951da177e4SLinus Torvalds goto out; 21961da177e4SLinus Torvalds 21971da177e4SLinus Torvalds can_sleep = !(cmd & LOCK_NB); 21981da177e4SLinus Torvalds cmd &= ~LOCK_NB; 21991da177e4SLinus Torvalds unlock = (cmd == LOCK_UN); 22001da177e4SLinus Torvalds 2201aeb5d727SAl Viro if (!unlock && !(cmd & LOCK_MAND) && 22022903ff01SAl Viro !(f.file->f_mode & (FMODE_READ|FMODE_WRITE))) 22031da177e4SLinus Torvalds goto out_putf; 22041da177e4SLinus Torvalds 2205d6367d62SNeilBrown lock = flock_make_lock(f.file, cmd, NULL); 22066e129d00SJeff Layton if (IS_ERR(lock)) { 22076e129d00SJeff Layton error = PTR_ERR(lock); 22081da177e4SLinus Torvalds goto out_putf; 22096e129d00SJeff Layton } 22106e129d00SJeff Layton 22111da177e4SLinus Torvalds if (can_sleep) 22121da177e4SLinus Torvalds lock->fl_flags |= FL_SLEEP; 22131da177e4SLinus Torvalds 22142903ff01SAl Viro error = security_file_lock(f.file, lock->fl_type); 22151da177e4SLinus Torvalds if (error) 22161da177e4SLinus Torvalds goto out_free; 22171da177e4SLinus Torvalds 2218de2a4a50SMiklos Szeredi if (f.file->f_op->flock) 22192903ff01SAl Viro error = f.file->f_op->flock(f.file, 22201da177e4SLinus Torvalds (can_sleep) ? F_SETLKW : F_SETLK, 22211da177e4SLinus Torvalds lock); 22221da177e4SLinus Torvalds else 22234f656367SBenjamin Coddington error = locks_lock_file_wait(f.file, lock); 22241da177e4SLinus Torvalds 22251da177e4SLinus Torvalds out_free: 22261da177e4SLinus Torvalds locks_free_lock(lock); 22271da177e4SLinus Torvalds 22281da177e4SLinus Torvalds out_putf: 22292903ff01SAl Viro fdput(f); 22301da177e4SLinus Torvalds out: 22311da177e4SLinus Torvalds return error; 22321da177e4SLinus Torvalds } 22331da177e4SLinus Torvalds 22343ee17abdSJ. Bruce Fields /** 22353ee17abdSJ. Bruce Fields * vfs_test_lock - test file byte range lock 22363ee17abdSJ. Bruce Fields * @filp: The file to test lock for 22376924c554SJ. Bruce Fields * @fl: The lock to test; also used to hold result 22383ee17abdSJ. Bruce Fields * 22393ee17abdSJ. Bruce Fields * Returns -ERRNO on failure. Indicates presence of conflicting lock by 22403ee17abdSJ. Bruce Fields * setting conf->fl_type to something other than F_UNLCK. 22413ee17abdSJ. Bruce Fields */ 22423ee17abdSJ. Bruce Fields int vfs_test_lock(struct file *filp, struct file_lock *fl) 22433ee17abdSJ. Bruce Fields { 2244de2a4a50SMiklos Szeredi if (filp->f_op->lock) 22453ee17abdSJ. Bruce Fields return filp->f_op->lock(filp, F_GETLK, fl); 22463ee17abdSJ. Bruce Fields posix_test_lock(filp, fl); 22473ee17abdSJ. Bruce Fields return 0; 22483ee17abdSJ. Bruce Fields } 22493ee17abdSJ. Bruce Fields EXPORT_SYMBOL_GPL(vfs_test_lock); 22503ee17abdSJ. Bruce Fields 22519d5b86acSBenjamin Coddington /** 22529d5b86acSBenjamin Coddington * locks_translate_pid - translate a file_lock's fl_pid number into a namespace 22539d5b86acSBenjamin Coddington * @fl: The file_lock who's fl_pid should be translated 22549d5b86acSBenjamin Coddington * @ns: The namespace into which the pid should be translated 22559d5b86acSBenjamin Coddington * 22569d5b86acSBenjamin Coddington * Used to tranlate a fl_pid into a namespace virtual pid number 22579d5b86acSBenjamin Coddington */ 22589d5b86acSBenjamin Coddington static pid_t locks_translate_pid(struct file_lock *fl, struct pid_namespace *ns) 22599d5b86acSBenjamin Coddington { 22609d5b86acSBenjamin Coddington pid_t vnr; 22619d5b86acSBenjamin Coddington struct pid *pid; 22629d5b86acSBenjamin Coddington 22639d5b86acSBenjamin Coddington if (IS_OFDLCK(fl)) 22649d5b86acSBenjamin Coddington return -1; 22659d5b86acSBenjamin Coddington if (IS_REMOTELCK(fl)) 22669d5b86acSBenjamin Coddington return fl->fl_pid; 2267826d7bc9SKonstantin Khorenko /* 2268826d7bc9SKonstantin Khorenko * If the flock owner process is dead and its pid has been already 2269826d7bc9SKonstantin Khorenko * freed, the translation below won't work, but we still want to show 2270826d7bc9SKonstantin Khorenko * flock owner pid number in init pidns. 2271826d7bc9SKonstantin Khorenko */ 2272826d7bc9SKonstantin Khorenko if (ns == &init_pid_ns) 2273826d7bc9SKonstantin Khorenko return (pid_t)fl->fl_pid; 22749d5b86acSBenjamin Coddington 22759d5b86acSBenjamin Coddington rcu_read_lock(); 22769d5b86acSBenjamin Coddington pid = find_pid_ns(fl->fl_pid, &init_pid_ns); 22779d5b86acSBenjamin Coddington vnr = pid_nr_ns(pid, ns); 22789d5b86acSBenjamin Coddington rcu_read_unlock(); 22799d5b86acSBenjamin Coddington return vnr; 22809d5b86acSBenjamin Coddington } 22819d5b86acSBenjamin Coddington 2282c2fa1b8aSJ. Bruce Fields static int posix_lock_to_flock(struct flock *flock, struct file_lock *fl) 2283c2fa1b8aSJ. Bruce Fields { 22849d5b86acSBenjamin Coddington flock->l_pid = locks_translate_pid(fl, task_active_pid_ns(current)); 2285c2fa1b8aSJ. Bruce Fields #if BITS_PER_LONG == 32 2286c2fa1b8aSJ. Bruce Fields /* 2287c2fa1b8aSJ. Bruce Fields * Make sure we can represent the posix lock via 2288c2fa1b8aSJ. Bruce Fields * legacy 32bit flock. 2289c2fa1b8aSJ. Bruce Fields */ 2290c2fa1b8aSJ. Bruce Fields if (fl->fl_start > OFFT_OFFSET_MAX) 2291c2fa1b8aSJ. Bruce Fields return -EOVERFLOW; 2292c2fa1b8aSJ. Bruce Fields if (fl->fl_end != OFFSET_MAX && fl->fl_end > OFFT_OFFSET_MAX) 2293c2fa1b8aSJ. Bruce Fields return -EOVERFLOW; 2294c2fa1b8aSJ. Bruce Fields #endif 2295c2fa1b8aSJ. Bruce Fields flock->l_start = fl->fl_start; 2296c2fa1b8aSJ. Bruce Fields flock->l_len = fl->fl_end == OFFSET_MAX ? 0 : 2297c2fa1b8aSJ. Bruce Fields fl->fl_end - fl->fl_start + 1; 2298c2fa1b8aSJ. Bruce Fields flock->l_whence = 0; 2299129a84deSJ. Bruce Fields flock->l_type = fl->fl_type; 2300c2fa1b8aSJ. Bruce Fields return 0; 2301c2fa1b8aSJ. Bruce Fields } 2302c2fa1b8aSJ. Bruce Fields 2303c2fa1b8aSJ. Bruce Fields #if BITS_PER_LONG == 32 2304c2fa1b8aSJ. Bruce Fields static void posix_lock_to_flock64(struct flock64 *flock, struct file_lock *fl) 2305c2fa1b8aSJ. Bruce Fields { 23069d5b86acSBenjamin Coddington flock->l_pid = locks_translate_pid(fl, task_active_pid_ns(current)); 2307c2fa1b8aSJ. Bruce Fields flock->l_start = fl->fl_start; 2308c2fa1b8aSJ. Bruce Fields flock->l_len = fl->fl_end == OFFSET_MAX ? 0 : 2309c2fa1b8aSJ. Bruce Fields fl->fl_end - fl->fl_start + 1; 2310c2fa1b8aSJ. Bruce Fields flock->l_whence = 0; 2311c2fa1b8aSJ. Bruce Fields flock->l_type = fl->fl_type; 2312c2fa1b8aSJ. Bruce Fields } 2313c2fa1b8aSJ. Bruce Fields #endif 2314c2fa1b8aSJ. Bruce Fields 23151da177e4SLinus Torvalds /* Report the first existing lock that would conflict with l. 23161da177e4SLinus Torvalds * This implements the F_GETLK command of fcntl(). 23171da177e4SLinus Torvalds */ 2318a75d30c7SChristoph Hellwig int fcntl_getlk(struct file *filp, unsigned int cmd, struct flock *flock) 23191da177e4SLinus Torvalds { 232052306e88SBenjamin Coddington struct file_lock *fl; 23211da177e4SLinus Torvalds int error; 23221da177e4SLinus Torvalds 232352306e88SBenjamin Coddington fl = locks_alloc_lock(); 232452306e88SBenjamin Coddington if (fl == NULL) 232552306e88SBenjamin Coddington return -ENOMEM; 23261da177e4SLinus Torvalds error = -EINVAL; 2327a75d30c7SChristoph Hellwig if (flock->l_type != F_RDLCK && flock->l_type != F_WRLCK) 23281da177e4SLinus Torvalds goto out; 23291da177e4SLinus Torvalds 233052306e88SBenjamin Coddington error = flock_to_posix_lock(filp, fl, flock); 23311da177e4SLinus Torvalds if (error) 23321da177e4SLinus Torvalds goto out; 23331da177e4SLinus Torvalds 23340d3f7a2dSJeff Layton if (cmd == F_OFD_GETLK) { 233590478939SJeff Layton error = -EINVAL; 2336a75d30c7SChristoph Hellwig if (flock->l_pid != 0) 233790478939SJeff Layton goto out; 233890478939SJeff Layton 23395d50ffd7SJeff Layton cmd = F_GETLK; 234052306e88SBenjamin Coddington fl->fl_flags |= FL_OFDLCK; 234152306e88SBenjamin Coddington fl->fl_owner = filp; 23425d50ffd7SJeff Layton } 23435d50ffd7SJeff Layton 234452306e88SBenjamin Coddington error = vfs_test_lock(filp, fl); 23453ee17abdSJ. Bruce Fields if (error) 23461da177e4SLinus Torvalds goto out; 23471da177e4SLinus Torvalds 234852306e88SBenjamin Coddington flock->l_type = fl->fl_type; 234952306e88SBenjamin Coddington if (fl->fl_type != F_UNLCK) { 235052306e88SBenjamin Coddington error = posix_lock_to_flock(flock, fl); 2351c2fa1b8aSJ. Bruce Fields if (error) 235252306e88SBenjamin Coddington goto out; 23531da177e4SLinus Torvalds } 23541da177e4SLinus Torvalds out: 235552306e88SBenjamin Coddington locks_free_lock(fl); 23561da177e4SLinus Torvalds return error; 23571da177e4SLinus Torvalds } 23581da177e4SLinus Torvalds 23597723ec97SMarc Eshel /** 23607723ec97SMarc Eshel * vfs_lock_file - file byte range lock 23617723ec97SMarc Eshel * @filp: The file to apply the lock to 23627723ec97SMarc Eshel * @cmd: type of locking operation (F_SETLK, F_GETLK, etc.) 23637723ec97SMarc Eshel * @fl: The lock to be applied 2364150b3934SMarc Eshel * @conf: Place to return a copy of the conflicting lock, if found. 2365150b3934SMarc Eshel * 2366150b3934SMarc Eshel * A caller that doesn't care about the conflicting lock may pass NULL 2367150b3934SMarc Eshel * as the final argument. 2368150b3934SMarc Eshel * 2369150b3934SMarc Eshel * If the filesystem defines a private ->lock() method, then @conf will 2370150b3934SMarc Eshel * be left unchanged; so a caller that cares should initialize it to 2371150b3934SMarc Eshel * some acceptable default. 23722beb6614SMarc Eshel * 23732beb6614SMarc Eshel * To avoid blocking kernel daemons, such as lockd, that need to acquire POSIX 23742beb6614SMarc Eshel * locks, the ->lock() interface may return asynchronously, before the lock has 23752beb6614SMarc Eshel * been granted or denied by the underlying filesystem, if (and only if) 23768fb47a4fSJ. Bruce Fields * lm_grant is set. Callers expecting ->lock() to return asynchronously 23772beb6614SMarc Eshel * will only use F_SETLK, not F_SETLKW; they will set FL_SLEEP if (and only if) 23782beb6614SMarc Eshel * the request is for a blocking lock. When ->lock() does return asynchronously, 23798fb47a4fSJ. Bruce Fields * it must return FILE_LOCK_DEFERRED, and call ->lm_grant() when the lock 23802beb6614SMarc Eshel * request completes. 23812beb6614SMarc Eshel * If the request is for non-blocking lock the file system should return 2382bde74e4bSMiklos Szeredi * FILE_LOCK_DEFERRED then try to get the lock and call the callback routine 2383bde74e4bSMiklos Szeredi * with the result. If the request timed out the callback routine will return a 23842beb6614SMarc Eshel * nonzero return code and the file system should release the lock. The file 23852beb6614SMarc Eshel * system is also responsible to keep a corresponding posix lock when it 23862beb6614SMarc Eshel * grants a lock so the VFS can find out which locks are locally held and do 23872beb6614SMarc Eshel * the correct lock cleanup when required. 23882beb6614SMarc Eshel * The underlying filesystem must not drop the kernel lock or call 23898fb47a4fSJ. Bruce Fields * ->lm_grant() before returning to the caller with a FILE_LOCK_DEFERRED 23902beb6614SMarc Eshel * return code. 23917723ec97SMarc Eshel */ 2392150b3934SMarc Eshel int vfs_lock_file(struct file *filp, unsigned int cmd, struct file_lock *fl, struct file_lock *conf) 23937723ec97SMarc Eshel { 2394de2a4a50SMiklos Szeredi if (filp->f_op->lock) 23957723ec97SMarc Eshel return filp->f_op->lock(filp, cmd, fl); 23967723ec97SMarc Eshel else 2397150b3934SMarc Eshel return posix_lock_file(filp, fl, conf); 23987723ec97SMarc Eshel } 23997723ec97SMarc Eshel EXPORT_SYMBOL_GPL(vfs_lock_file); 24007723ec97SMarc Eshel 2401b648a6deSMiklos Szeredi static int do_lock_file_wait(struct file *filp, unsigned int cmd, 2402b648a6deSMiklos Szeredi struct file_lock *fl) 2403b648a6deSMiklos Szeredi { 2404b648a6deSMiklos Szeredi int error; 2405b648a6deSMiklos Szeredi 2406b648a6deSMiklos Szeredi error = security_file_lock(filp, fl->fl_type); 2407b648a6deSMiklos Szeredi if (error) 2408b648a6deSMiklos Szeredi return error; 2409b648a6deSMiklos Szeredi 2410b648a6deSMiklos Szeredi for (;;) { 2411764c76b3SMiklos Szeredi error = vfs_lock_file(filp, cmd, fl, NULL); 2412b648a6deSMiklos Szeredi if (error != FILE_LOCK_DEFERRED) 2413b648a6deSMiklos Szeredi break; 2414ada5c1daSNeilBrown error = wait_event_interruptible(fl->fl_wait, !fl->fl_blocker); 241516306a61SNeilBrown if (error) 2416b648a6deSMiklos Szeredi break; 2417b648a6deSMiklos Szeredi } 241816306a61SNeilBrown locks_delete_block(fl); 2419b648a6deSMiklos Szeredi 2420b648a6deSMiklos Szeredi return error; 2421b648a6deSMiklos Szeredi } 2422b648a6deSMiklos Szeredi 24236ca7d910SBenjamin Coddington /* Ensure that fl->fl_file has compatible f_mode for F_SETLK calls */ 2424cf01f4eeSJeff Layton static int 2425cf01f4eeSJeff Layton check_fmode_for_setlk(struct file_lock *fl) 2426cf01f4eeSJeff Layton { 2427cf01f4eeSJeff Layton switch (fl->fl_type) { 2428cf01f4eeSJeff Layton case F_RDLCK: 2429cf01f4eeSJeff Layton if (!(fl->fl_file->f_mode & FMODE_READ)) 2430cf01f4eeSJeff Layton return -EBADF; 2431cf01f4eeSJeff Layton break; 2432cf01f4eeSJeff Layton case F_WRLCK: 2433cf01f4eeSJeff Layton if (!(fl->fl_file->f_mode & FMODE_WRITE)) 2434cf01f4eeSJeff Layton return -EBADF; 2435cf01f4eeSJeff Layton } 2436cf01f4eeSJeff Layton return 0; 2437cf01f4eeSJeff Layton } 2438cf01f4eeSJeff Layton 24391da177e4SLinus Torvalds /* Apply the lock described by l to an open file descriptor. 24401da177e4SLinus Torvalds * This implements both the F_SETLK and F_SETLKW commands of fcntl(). 24411da177e4SLinus Torvalds */ 2442c293621bSPeter Staubach int fcntl_setlk(unsigned int fd, struct file *filp, unsigned int cmd, 2443a75d30c7SChristoph Hellwig struct flock *flock) 24441da177e4SLinus Torvalds { 24451da177e4SLinus Torvalds struct file_lock *file_lock = locks_alloc_lock(); 2446a75d30c7SChristoph Hellwig struct inode *inode = locks_inode(filp); 24470b2bac2fSAl Viro struct file *f; 24481da177e4SLinus Torvalds int error; 24491da177e4SLinus Torvalds 24501da177e4SLinus Torvalds if (file_lock == NULL) 24511da177e4SLinus Torvalds return -ENOLCK; 24521da177e4SLinus Torvalds 24531da177e4SLinus Torvalds /* Don't allow mandatory locks on files that may be memory mapped 24541da177e4SLinus Torvalds * and shared. 24551da177e4SLinus Torvalds */ 2456a16877caSPavel Emelyanov if (mandatory_lock(inode) && mapping_writably_mapped(filp->f_mapping)) { 24571da177e4SLinus Torvalds error = -EAGAIN; 24581da177e4SLinus Torvalds goto out; 24591da177e4SLinus Torvalds } 24601da177e4SLinus Torvalds 2461a75d30c7SChristoph Hellwig error = flock_to_posix_lock(filp, file_lock, flock); 24621da177e4SLinus Torvalds if (error) 24631da177e4SLinus Torvalds goto out; 24645d50ffd7SJeff Layton 2465cf01f4eeSJeff Layton error = check_fmode_for_setlk(file_lock); 2466cf01f4eeSJeff Layton if (error) 2467cf01f4eeSJeff Layton goto out; 2468cf01f4eeSJeff Layton 24695d50ffd7SJeff Layton /* 24705d50ffd7SJeff Layton * If the cmd is requesting file-private locks, then set the 2471cff2fce5SJeff Layton * FL_OFDLCK flag and override the owner. 24725d50ffd7SJeff Layton */ 24735d50ffd7SJeff Layton switch (cmd) { 24740d3f7a2dSJeff Layton case F_OFD_SETLK: 247590478939SJeff Layton error = -EINVAL; 2476a75d30c7SChristoph Hellwig if (flock->l_pid != 0) 247790478939SJeff Layton goto out; 247890478939SJeff Layton 24795d50ffd7SJeff Layton cmd = F_SETLK; 2480cff2fce5SJeff Layton file_lock->fl_flags |= FL_OFDLCK; 248173a8f5f7SChristoph Hellwig file_lock->fl_owner = filp; 24825d50ffd7SJeff Layton break; 24830d3f7a2dSJeff Layton case F_OFD_SETLKW: 248490478939SJeff Layton error = -EINVAL; 2485a75d30c7SChristoph Hellwig if (flock->l_pid != 0) 248690478939SJeff Layton goto out; 248790478939SJeff Layton 24885d50ffd7SJeff Layton cmd = F_SETLKW; 2489cff2fce5SJeff Layton file_lock->fl_flags |= FL_OFDLCK; 249073a8f5f7SChristoph Hellwig file_lock->fl_owner = filp; 24915d50ffd7SJeff Layton /* Fallthrough */ 24925d50ffd7SJeff Layton case F_SETLKW: 24931da177e4SLinus Torvalds file_lock->fl_flags |= FL_SLEEP; 24941da177e4SLinus Torvalds } 24951da177e4SLinus Torvalds 2496b648a6deSMiklos Szeredi error = do_lock_file_wait(filp, cmd, file_lock); 2497c293621bSPeter Staubach 2498c293621bSPeter Staubach /* 24990752ba80SJeff Layton * Attempt to detect a close/fcntl race and recover by releasing the 25000752ba80SJeff Layton * lock that was just acquired. There is no need to do that when we're 25010752ba80SJeff Layton * unlocking though, or for OFD locks. 2502c293621bSPeter Staubach */ 25030752ba80SJeff Layton if (!error && file_lock->fl_type != F_UNLCK && 25040752ba80SJeff Layton !(file_lock->fl_flags & FL_OFDLCK)) { 25050b2bac2fSAl Viro /* 25067f3697e2SJeff Layton * We need that spin_lock here - it prevents reordering between 25077f3697e2SJeff Layton * update of i_flctx->flc_posix and check for it done in 25087f3697e2SJeff Layton * close(). rcu_read_lock() wouldn't do. 25090b2bac2fSAl Viro */ 25100b2bac2fSAl Viro spin_lock(¤t->files->file_lock); 25110b2bac2fSAl Viro f = fcheck(fd); 25120b2bac2fSAl Viro spin_unlock(¤t->files->file_lock); 25137f3697e2SJeff Layton if (f != filp) { 25147f3697e2SJeff Layton file_lock->fl_type = F_UNLCK; 25157f3697e2SJeff Layton error = do_lock_file_wait(filp, cmd, file_lock); 25167f3697e2SJeff Layton WARN_ON_ONCE(error); 25177f3697e2SJeff Layton error = -EBADF; 2518c293621bSPeter Staubach } 25197f3697e2SJeff Layton } 25201da177e4SLinus Torvalds out: 25211890910fSJeff Layton trace_fcntl_setlk(inode, file_lock, error); 25221da177e4SLinus Torvalds locks_free_lock(file_lock); 25231da177e4SLinus Torvalds return error; 25241da177e4SLinus Torvalds } 25251da177e4SLinus Torvalds 25261da177e4SLinus Torvalds #if BITS_PER_LONG == 32 25271da177e4SLinus Torvalds /* Report the first existing lock that would conflict with l. 25281da177e4SLinus Torvalds * This implements the F_GETLK command of fcntl(). 25291da177e4SLinus Torvalds */ 2530a75d30c7SChristoph Hellwig int fcntl_getlk64(struct file *filp, unsigned int cmd, struct flock64 *flock) 25311da177e4SLinus Torvalds { 253252306e88SBenjamin Coddington struct file_lock *fl; 25331da177e4SLinus Torvalds int error; 25341da177e4SLinus Torvalds 253552306e88SBenjamin Coddington fl = locks_alloc_lock(); 253652306e88SBenjamin Coddington if (fl == NULL) 253752306e88SBenjamin Coddington return -ENOMEM; 253852306e88SBenjamin Coddington 25391da177e4SLinus Torvalds error = -EINVAL; 2540a75d30c7SChristoph Hellwig if (flock->l_type != F_RDLCK && flock->l_type != F_WRLCK) 25411da177e4SLinus Torvalds goto out; 25421da177e4SLinus Torvalds 254352306e88SBenjamin Coddington error = flock64_to_posix_lock(filp, fl, flock); 25441da177e4SLinus Torvalds if (error) 25451da177e4SLinus Torvalds goto out; 25461da177e4SLinus Torvalds 25470d3f7a2dSJeff Layton if (cmd == F_OFD_GETLK) { 254890478939SJeff Layton error = -EINVAL; 2549a75d30c7SChristoph Hellwig if (flock->l_pid != 0) 255090478939SJeff Layton goto out; 255190478939SJeff Layton 25525d50ffd7SJeff Layton cmd = F_GETLK64; 255352306e88SBenjamin Coddington fl->fl_flags |= FL_OFDLCK; 255452306e88SBenjamin Coddington fl->fl_owner = filp; 25555d50ffd7SJeff Layton } 25565d50ffd7SJeff Layton 255752306e88SBenjamin Coddington error = vfs_test_lock(filp, fl); 25583ee17abdSJ. Bruce Fields if (error) 25591da177e4SLinus Torvalds goto out; 25601da177e4SLinus Torvalds 256152306e88SBenjamin Coddington flock->l_type = fl->fl_type; 256252306e88SBenjamin Coddington if (fl->fl_type != F_UNLCK) 256352306e88SBenjamin Coddington posix_lock_to_flock64(flock, fl); 25641da177e4SLinus Torvalds 25651da177e4SLinus Torvalds out: 256652306e88SBenjamin Coddington locks_free_lock(fl); 25671da177e4SLinus Torvalds return error; 25681da177e4SLinus Torvalds } 25691da177e4SLinus Torvalds 25701da177e4SLinus Torvalds /* Apply the lock described by l to an open file descriptor. 25711da177e4SLinus Torvalds * This implements both the F_SETLK and F_SETLKW commands of fcntl(). 25721da177e4SLinus Torvalds */ 2573c293621bSPeter Staubach int fcntl_setlk64(unsigned int fd, struct file *filp, unsigned int cmd, 2574a75d30c7SChristoph Hellwig struct flock64 *flock) 25751da177e4SLinus Torvalds { 25761da177e4SLinus Torvalds struct file_lock *file_lock = locks_alloc_lock(); 2577a75d30c7SChristoph Hellwig struct inode *inode = locks_inode(filp); 25780b2bac2fSAl Viro struct file *f; 25791da177e4SLinus Torvalds int error; 25801da177e4SLinus Torvalds 25811da177e4SLinus Torvalds if (file_lock == NULL) 25821da177e4SLinus Torvalds return -ENOLCK; 25831da177e4SLinus Torvalds 25841da177e4SLinus Torvalds /* Don't allow mandatory locks on files that may be memory mapped 25851da177e4SLinus Torvalds * and shared. 25861da177e4SLinus Torvalds */ 2587a16877caSPavel Emelyanov if (mandatory_lock(inode) && mapping_writably_mapped(filp->f_mapping)) { 25881da177e4SLinus Torvalds error = -EAGAIN; 25891da177e4SLinus Torvalds goto out; 25901da177e4SLinus Torvalds } 25911da177e4SLinus Torvalds 2592a75d30c7SChristoph Hellwig error = flock64_to_posix_lock(filp, file_lock, flock); 25931da177e4SLinus Torvalds if (error) 25941da177e4SLinus Torvalds goto out; 25955d50ffd7SJeff Layton 2596cf01f4eeSJeff Layton error = check_fmode_for_setlk(file_lock); 2597cf01f4eeSJeff Layton if (error) 2598cf01f4eeSJeff Layton goto out; 2599cf01f4eeSJeff Layton 26005d50ffd7SJeff Layton /* 26015d50ffd7SJeff Layton * If the cmd is requesting file-private locks, then set the 2602cff2fce5SJeff Layton * FL_OFDLCK flag and override the owner. 26035d50ffd7SJeff Layton */ 26045d50ffd7SJeff Layton switch (cmd) { 26050d3f7a2dSJeff Layton case F_OFD_SETLK: 260690478939SJeff Layton error = -EINVAL; 2607a75d30c7SChristoph Hellwig if (flock->l_pid != 0) 260890478939SJeff Layton goto out; 260990478939SJeff Layton 26105d50ffd7SJeff Layton cmd = F_SETLK64; 2611cff2fce5SJeff Layton file_lock->fl_flags |= FL_OFDLCK; 261273a8f5f7SChristoph Hellwig file_lock->fl_owner = filp; 26135d50ffd7SJeff Layton break; 26140d3f7a2dSJeff Layton case F_OFD_SETLKW: 261590478939SJeff Layton error = -EINVAL; 2616a75d30c7SChristoph Hellwig if (flock->l_pid != 0) 261790478939SJeff Layton goto out; 261890478939SJeff Layton 26195d50ffd7SJeff Layton cmd = F_SETLKW64; 2620cff2fce5SJeff Layton file_lock->fl_flags |= FL_OFDLCK; 262173a8f5f7SChristoph Hellwig file_lock->fl_owner = filp; 26225d50ffd7SJeff Layton /* Fallthrough */ 26235d50ffd7SJeff Layton case F_SETLKW64: 26241da177e4SLinus Torvalds file_lock->fl_flags |= FL_SLEEP; 26251da177e4SLinus Torvalds } 26261da177e4SLinus Torvalds 2627b648a6deSMiklos Szeredi error = do_lock_file_wait(filp, cmd, file_lock); 2628c293621bSPeter Staubach 2629c293621bSPeter Staubach /* 26300752ba80SJeff Layton * Attempt to detect a close/fcntl race and recover by releasing the 26310752ba80SJeff Layton * lock that was just acquired. There is no need to do that when we're 26320752ba80SJeff Layton * unlocking though, or for OFD locks. 2633c293621bSPeter Staubach */ 26340752ba80SJeff Layton if (!error && file_lock->fl_type != F_UNLCK && 26350752ba80SJeff Layton !(file_lock->fl_flags & FL_OFDLCK)) { 26367f3697e2SJeff Layton /* 26377f3697e2SJeff Layton * We need that spin_lock here - it prevents reordering between 26387f3697e2SJeff Layton * update of i_flctx->flc_posix and check for it done in 26397f3697e2SJeff Layton * close(). rcu_read_lock() wouldn't do. 26407f3697e2SJeff Layton */ 26410b2bac2fSAl Viro spin_lock(¤t->files->file_lock); 26420b2bac2fSAl Viro f = fcheck(fd); 26430b2bac2fSAl Viro spin_unlock(¤t->files->file_lock); 26447f3697e2SJeff Layton if (f != filp) { 26457f3697e2SJeff Layton file_lock->fl_type = F_UNLCK; 26467f3697e2SJeff Layton error = do_lock_file_wait(filp, cmd, file_lock); 26477f3697e2SJeff Layton WARN_ON_ONCE(error); 26487f3697e2SJeff Layton error = -EBADF; 2649c293621bSPeter Staubach } 26507f3697e2SJeff Layton } 26511da177e4SLinus Torvalds out: 26521da177e4SLinus Torvalds locks_free_lock(file_lock); 26531da177e4SLinus Torvalds return error; 26541da177e4SLinus Torvalds } 26551da177e4SLinus Torvalds #endif /* BITS_PER_LONG == 32 */ 26561da177e4SLinus Torvalds 26571da177e4SLinus Torvalds /* 26581da177e4SLinus Torvalds * This function is called when the file is being removed 26591da177e4SLinus Torvalds * from the task's fd array. POSIX locks belonging to this task 26601da177e4SLinus Torvalds * are deleted at this time. 26611da177e4SLinus Torvalds */ 26621da177e4SLinus Torvalds void locks_remove_posix(struct file *filp, fl_owner_t owner) 26631da177e4SLinus Torvalds { 26641890910fSJeff Layton int error; 2665c568d683SMiklos Szeredi struct inode *inode = locks_inode(filp); 2666ff7b86b8SMiklos Szeredi struct file_lock lock; 2667128a3785SDmitry Vyukov struct file_lock_context *ctx; 26681da177e4SLinus Torvalds 26691da177e4SLinus Torvalds /* 26701da177e4SLinus Torvalds * If there are no locks held on this file, we don't need to call 26711da177e4SLinus Torvalds * posix_lock_file(). Another process could be setting a lock on this 26721da177e4SLinus Torvalds * file at the same time, but we wouldn't remove that lock anyway. 26731da177e4SLinus Torvalds */ 2674c568d683SMiklos Szeredi ctx = smp_load_acquire(&inode->i_flctx); 2675bd61e0a9SJeff Layton if (!ctx || list_empty(&ctx->flc_posix)) 26761da177e4SLinus Torvalds return; 26771da177e4SLinus Torvalds 2678d6367d62SNeilBrown locks_init_lock(&lock); 26791da177e4SLinus Torvalds lock.fl_type = F_UNLCK; 268075e1fcc0SMiklos Szeredi lock.fl_flags = FL_POSIX | FL_CLOSE; 26811da177e4SLinus Torvalds lock.fl_start = 0; 26821da177e4SLinus Torvalds lock.fl_end = OFFSET_MAX; 26831da177e4SLinus Torvalds lock.fl_owner = owner; 26841da177e4SLinus Torvalds lock.fl_pid = current->tgid; 26851da177e4SLinus Torvalds lock.fl_file = filp; 26861da177e4SLinus Torvalds lock.fl_ops = NULL; 26871da177e4SLinus Torvalds lock.fl_lmops = NULL; 26881da177e4SLinus Torvalds 26891890910fSJeff Layton error = vfs_lock_file(filp, F_SETLK, &lock, NULL); 26901da177e4SLinus Torvalds 26911da177e4SLinus Torvalds if (lock.fl_ops && lock.fl_ops->fl_release_private) 26921da177e4SLinus Torvalds lock.fl_ops->fl_release_private(&lock); 2693c568d683SMiklos Szeredi trace_locks_remove_posix(inode, &lock, error); 26941da177e4SLinus Torvalds } 26951da177e4SLinus Torvalds EXPORT_SYMBOL(locks_remove_posix); 26961da177e4SLinus Torvalds 26973d8e560dSJeff Layton /* The i_flctx must be valid when calling into here */ 2698dd459bb1SJeff Layton static void 2699128a3785SDmitry Vyukov locks_remove_flock(struct file *filp, struct file_lock_context *flctx) 2700dd459bb1SJeff Layton { 2701d6367d62SNeilBrown struct file_lock fl; 2702c568d683SMiklos Szeredi struct inode *inode = locks_inode(filp); 2703dd459bb1SJeff Layton 27043d8e560dSJeff Layton if (list_empty(&flctx->flc_flock)) 2705dd459bb1SJeff Layton return; 2706dd459bb1SJeff Layton 2707d6367d62SNeilBrown flock_make_lock(filp, LOCK_UN, &fl); 2708d6367d62SNeilBrown fl.fl_flags |= FL_CLOSE; 2709d6367d62SNeilBrown 2710de2a4a50SMiklos Szeredi if (filp->f_op->flock) 2711dd459bb1SJeff Layton filp->f_op->flock(filp, F_SETLKW, &fl); 2712dd459bb1SJeff Layton else 2713bcd7f78dSJeff Layton flock_lock_inode(inode, &fl); 2714dd459bb1SJeff Layton 2715dd459bb1SJeff Layton if (fl.fl_ops && fl.fl_ops->fl_release_private) 2716dd459bb1SJeff Layton fl.fl_ops->fl_release_private(&fl); 2717dd459bb1SJeff Layton } 2718dd459bb1SJeff Layton 27193d8e560dSJeff Layton /* The i_flctx must be valid when calling into here */ 27208634b51fSJeff Layton static void 2721128a3785SDmitry Vyukov locks_remove_lease(struct file *filp, struct file_lock_context *ctx) 27228634b51fSJeff Layton { 27238634b51fSJeff Layton struct file_lock *fl, *tmp; 27248634b51fSJeff Layton LIST_HEAD(dispose); 27258634b51fSJeff Layton 27263d8e560dSJeff Layton if (list_empty(&ctx->flc_lease)) 27278634b51fSJeff Layton return; 27288634b51fSJeff Layton 272902e525b2SPeter Zijlstra percpu_down_read(&file_rwsem); 27306109c850SJeff Layton spin_lock(&ctx->flc_lock); 27318634b51fSJeff Layton list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list) 2732c4e136cdSJeff Layton if (filp == fl->fl_file) 27337448cc37SJeff Layton lease_modify(fl, F_UNLCK, &dispose); 27346109c850SJeff Layton spin_unlock(&ctx->flc_lock); 273502e525b2SPeter Zijlstra percpu_up_read(&file_rwsem); 27365f43086bSPeter Zijlstra 27378634b51fSJeff Layton locks_dispose_list(&dispose); 27388634b51fSJeff Layton } 27398634b51fSJeff Layton 27401da177e4SLinus Torvalds /* 27411da177e4SLinus Torvalds * This function is called on the last close of an open file. 27421da177e4SLinus Torvalds */ 274378ed8a13SJeff Layton void locks_remove_file(struct file *filp) 27441da177e4SLinus Torvalds { 2745128a3785SDmitry Vyukov struct file_lock_context *ctx; 2746128a3785SDmitry Vyukov 2747c568d683SMiklos Szeredi ctx = smp_load_acquire(&locks_inode(filp)->i_flctx); 2748128a3785SDmitry Vyukov if (!ctx) 27493d8e560dSJeff Layton return; 27503d8e560dSJeff Layton 2751dd459bb1SJeff Layton /* remove any OFD locks */ 275273a8f5f7SChristoph Hellwig locks_remove_posix(filp, filp); 27535d50ffd7SJeff Layton 2754dd459bb1SJeff Layton /* remove flock locks */ 2755128a3785SDmitry Vyukov locks_remove_flock(filp, ctx); 2756dd459bb1SJeff Layton 27578634b51fSJeff Layton /* remove any leases */ 2758128a3785SDmitry Vyukov locks_remove_lease(filp, ctx); 27593953704fSBenjamin Coddington 27603953704fSBenjamin Coddington spin_lock(&ctx->flc_lock); 27613953704fSBenjamin Coddington locks_check_ctx_file_list(filp, &ctx->flc_posix, "POSIX"); 27623953704fSBenjamin Coddington locks_check_ctx_file_list(filp, &ctx->flc_flock, "FLOCK"); 27633953704fSBenjamin Coddington locks_check_ctx_file_list(filp, &ctx->flc_lease, "LEASE"); 27643953704fSBenjamin Coddington spin_unlock(&ctx->flc_lock); 27651da177e4SLinus Torvalds } 27661da177e4SLinus Torvalds 27671da177e4SLinus Torvalds /** 27689b9d2ab4SMarc Eshel * vfs_cancel_lock - file byte range unblock lock 27699b9d2ab4SMarc Eshel * @filp: The file to apply the unblock to 27709b9d2ab4SMarc Eshel * @fl: The lock to be unblocked 27719b9d2ab4SMarc Eshel * 27729b9d2ab4SMarc Eshel * Used by lock managers to cancel blocked requests 27739b9d2ab4SMarc Eshel */ 27749b9d2ab4SMarc Eshel int vfs_cancel_lock(struct file *filp, struct file_lock *fl) 27759b9d2ab4SMarc Eshel { 2776de2a4a50SMiklos Szeredi if (filp->f_op->lock) 27779b9d2ab4SMarc Eshel return filp->f_op->lock(filp, F_CANCELLK, fl); 27789b9d2ab4SMarc Eshel return 0; 27799b9d2ab4SMarc Eshel } 27809b9d2ab4SMarc Eshel EXPORT_SYMBOL_GPL(vfs_cancel_lock); 27819b9d2ab4SMarc Eshel 27827f8ada98SPavel Emelyanov #ifdef CONFIG_PROC_FS 2783d8ba7a36SAlexey Dobriyan #include <linux/proc_fs.h> 27847f8ada98SPavel Emelyanov #include <linux/seq_file.h> 27857f8ada98SPavel Emelyanov 27867012b02aSJeff Layton struct locks_iterator { 27877012b02aSJeff Layton int li_cpu; 27887012b02aSJeff Layton loff_t li_pos; 27897012b02aSJeff Layton }; 27907012b02aSJeff Layton 27917f8ada98SPavel Emelyanov static void lock_get_status(struct seq_file *f, struct file_lock *fl, 279299dc8292SJerome Marchand loff_t id, char *pfx) 27931da177e4SLinus Torvalds { 27941da177e4SLinus Torvalds struct inode *inode = NULL; 2795ab1f1611SVitaliy Gusev unsigned int fl_pid; 2796d67fd44fSNikolay Borisov struct pid_namespace *proc_pidns = file_inode(f->file)->i_sb->s_fs_info; 2797d67fd44fSNikolay Borisov 27989d5b86acSBenjamin Coddington fl_pid = locks_translate_pid(fl, proc_pidns); 2799d67fd44fSNikolay Borisov /* 28001cf8e5deSKonstantin Khorenko * If lock owner is dead (and pid is freed) or not visible in current 28011cf8e5deSKonstantin Khorenko * pidns, zero is shown as a pid value. Check lock info from 28021cf8e5deSKonstantin Khorenko * init_pid_ns to get saved lock pid value. 2803d67fd44fSNikolay Borisov */ 28041da177e4SLinus Torvalds 28051da177e4SLinus Torvalds if (fl->fl_file != NULL) 2806c568d683SMiklos Szeredi inode = locks_inode(fl->fl_file); 28071da177e4SLinus Torvalds 280899dc8292SJerome Marchand seq_printf(f, "%lld:%s ", id, pfx); 28091da177e4SLinus Torvalds if (IS_POSIX(fl)) { 2810c918d42aSJeff Layton if (fl->fl_flags & FL_ACCESS) 28115315c26aSFabian Frederick seq_puts(f, "ACCESS"); 2812cff2fce5SJeff Layton else if (IS_OFDLCK(fl)) 28135315c26aSFabian Frederick seq_puts(f, "OFDLCK"); 2814c918d42aSJeff Layton else 28155315c26aSFabian Frederick seq_puts(f, "POSIX "); 2816c918d42aSJeff Layton 2817c918d42aSJeff Layton seq_printf(f, " %s ", 28181da177e4SLinus Torvalds (inode == NULL) ? "*NOINODE*" : 2819a16877caSPavel Emelyanov mandatory_lock(inode) ? "MANDATORY" : "ADVISORY "); 28201da177e4SLinus Torvalds } else if (IS_FLOCK(fl)) { 28211da177e4SLinus Torvalds if (fl->fl_type & LOCK_MAND) { 28225315c26aSFabian Frederick seq_puts(f, "FLOCK MSNFS "); 28231da177e4SLinus Torvalds } else { 28245315c26aSFabian Frederick seq_puts(f, "FLOCK ADVISORY "); 28251da177e4SLinus Torvalds } 28261da177e4SLinus Torvalds } else if (IS_LEASE(fl)) { 28278144f1f6SJeff Layton if (fl->fl_flags & FL_DELEG) 28288144f1f6SJeff Layton seq_puts(f, "DELEG "); 28298144f1f6SJeff Layton else 28305315c26aSFabian Frederick seq_puts(f, "LEASE "); 28318144f1f6SJeff Layton 2832ab83fa4bSJ. Bruce Fields if (lease_breaking(fl)) 28335315c26aSFabian Frederick seq_puts(f, "BREAKING "); 28341da177e4SLinus Torvalds else if (fl->fl_file) 28355315c26aSFabian Frederick seq_puts(f, "ACTIVE "); 28361da177e4SLinus Torvalds else 28375315c26aSFabian Frederick seq_puts(f, "BREAKER "); 28381da177e4SLinus Torvalds } else { 28395315c26aSFabian Frederick seq_puts(f, "UNKNOWN UNKNOWN "); 28401da177e4SLinus Torvalds } 28411da177e4SLinus Torvalds if (fl->fl_type & LOCK_MAND) { 28427f8ada98SPavel Emelyanov seq_printf(f, "%s ", 28431da177e4SLinus Torvalds (fl->fl_type & LOCK_READ) 28441da177e4SLinus Torvalds ? (fl->fl_type & LOCK_WRITE) ? "RW " : "READ " 28451da177e4SLinus Torvalds : (fl->fl_type & LOCK_WRITE) ? "WRITE" : "NONE "); 28461da177e4SLinus Torvalds } else { 28477f8ada98SPavel Emelyanov seq_printf(f, "%s ", 2848ab83fa4bSJ. Bruce Fields (lease_breaking(fl)) 28490ee5c6d6SJeff Layton ? (fl->fl_type == F_UNLCK) ? "UNLCK" : "READ " 28500ee5c6d6SJeff Layton : (fl->fl_type == F_WRLCK) ? "WRITE" : "READ "); 28511da177e4SLinus Torvalds } 28521da177e4SLinus Torvalds if (inode) { 28533648888eSJeff Layton /* userspace relies on this representation of dev_t */ 2854ab1f1611SVitaliy Gusev seq_printf(f, "%d %02x:%02x:%ld ", fl_pid, 28551da177e4SLinus Torvalds MAJOR(inode->i_sb->s_dev), 28561da177e4SLinus Torvalds MINOR(inode->i_sb->s_dev), inode->i_ino); 28571da177e4SLinus Torvalds } else { 2858ab1f1611SVitaliy Gusev seq_printf(f, "%d <none>:0 ", fl_pid); 28591da177e4SLinus Torvalds } 28601da177e4SLinus Torvalds if (IS_POSIX(fl)) { 28611da177e4SLinus Torvalds if (fl->fl_end == OFFSET_MAX) 28627f8ada98SPavel Emelyanov seq_printf(f, "%Ld EOF\n", fl->fl_start); 28631da177e4SLinus Torvalds else 28647f8ada98SPavel Emelyanov seq_printf(f, "%Ld %Ld\n", fl->fl_start, fl->fl_end); 28651da177e4SLinus Torvalds } else { 28665315c26aSFabian Frederick seq_puts(f, "0 EOF\n"); 28671da177e4SLinus Torvalds } 28681da177e4SLinus Torvalds } 28691da177e4SLinus Torvalds 28707f8ada98SPavel Emelyanov static int locks_show(struct seq_file *f, void *v) 28711da177e4SLinus Torvalds { 28727012b02aSJeff Layton struct locks_iterator *iter = f->private; 28737f8ada98SPavel Emelyanov struct file_lock *fl, *bfl; 2874d67fd44fSNikolay Borisov struct pid_namespace *proc_pidns = file_inode(f->file)->i_sb->s_fs_info; 28757f8ada98SPavel Emelyanov 2876139ca04eSJeff Layton fl = hlist_entry(v, struct file_lock, fl_link); 28777f8ada98SPavel Emelyanov 28789d5b86acSBenjamin Coddington if (locks_translate_pid(fl, proc_pidns) == 0) 2879d67fd44fSNikolay Borisov return 0; 2880d67fd44fSNikolay Borisov 28817012b02aSJeff Layton lock_get_status(f, fl, iter->li_pos, ""); 28827f8ada98SPavel Emelyanov 2883ada5c1daSNeilBrown list_for_each_entry(bfl, &fl->fl_blocked_requests, fl_blocked_member) 28847012b02aSJeff Layton lock_get_status(f, bfl, iter->li_pos, " ->"); 28857f8ada98SPavel Emelyanov 28867f8ada98SPavel Emelyanov return 0; 28871da177e4SLinus Torvalds } 28881da177e4SLinus Torvalds 28896c8c9031SAndrey Vagin static void __show_fd_locks(struct seq_file *f, 28906c8c9031SAndrey Vagin struct list_head *head, int *id, 28916c8c9031SAndrey Vagin struct file *filp, struct files_struct *files) 28926c8c9031SAndrey Vagin { 28936c8c9031SAndrey Vagin struct file_lock *fl; 28946c8c9031SAndrey Vagin 28956c8c9031SAndrey Vagin list_for_each_entry(fl, head, fl_list) { 28966c8c9031SAndrey Vagin 28976c8c9031SAndrey Vagin if (filp != fl->fl_file) 28986c8c9031SAndrey Vagin continue; 28996c8c9031SAndrey Vagin if (fl->fl_owner != files && 29006c8c9031SAndrey Vagin fl->fl_owner != filp) 29016c8c9031SAndrey Vagin continue; 29026c8c9031SAndrey Vagin 29036c8c9031SAndrey Vagin (*id)++; 29046c8c9031SAndrey Vagin seq_puts(f, "lock:\t"); 29056c8c9031SAndrey Vagin lock_get_status(f, fl, *id, ""); 29066c8c9031SAndrey Vagin } 29076c8c9031SAndrey Vagin } 29086c8c9031SAndrey Vagin 29096c8c9031SAndrey Vagin void show_fd_locks(struct seq_file *f, 29106c8c9031SAndrey Vagin struct file *filp, struct files_struct *files) 29116c8c9031SAndrey Vagin { 2912c568d683SMiklos Szeredi struct inode *inode = locks_inode(filp); 29136c8c9031SAndrey Vagin struct file_lock_context *ctx; 29146c8c9031SAndrey Vagin int id = 0; 29156c8c9031SAndrey Vagin 2916128a3785SDmitry Vyukov ctx = smp_load_acquire(&inode->i_flctx); 29176c8c9031SAndrey Vagin if (!ctx) 29186c8c9031SAndrey Vagin return; 29196c8c9031SAndrey Vagin 29206c8c9031SAndrey Vagin spin_lock(&ctx->flc_lock); 29216c8c9031SAndrey Vagin __show_fd_locks(f, &ctx->flc_flock, &id, filp, files); 29226c8c9031SAndrey Vagin __show_fd_locks(f, &ctx->flc_posix, &id, filp, files); 29236c8c9031SAndrey Vagin __show_fd_locks(f, &ctx->flc_lease, &id, filp, files); 29246c8c9031SAndrey Vagin spin_unlock(&ctx->flc_lock); 29256c8c9031SAndrey Vagin } 29266c8c9031SAndrey Vagin 29277f8ada98SPavel Emelyanov static void *locks_start(struct seq_file *f, loff_t *pos) 2928b03dfdecSJeff Layton __acquires(&blocked_lock_lock) 29291da177e4SLinus Torvalds { 29307012b02aSJeff Layton struct locks_iterator *iter = f->private; 293199dc8292SJerome Marchand 29327012b02aSJeff Layton iter->li_pos = *pos + 1; 2933aba37660SPeter Zijlstra percpu_down_write(&file_rwsem); 29347b2296afSJeff Layton spin_lock(&blocked_lock_lock); 29357c3f654dSPeter Zijlstra return seq_hlist_start_percpu(&file_lock_list.hlist, &iter->li_cpu, *pos); 29361da177e4SLinus Torvalds } 29377f8ada98SPavel Emelyanov 29387f8ada98SPavel Emelyanov static void *locks_next(struct seq_file *f, void *v, loff_t *pos) 29397f8ada98SPavel Emelyanov { 29407012b02aSJeff Layton struct locks_iterator *iter = f->private; 29417012b02aSJeff Layton 29427012b02aSJeff Layton ++iter->li_pos; 29437c3f654dSPeter Zijlstra return seq_hlist_next_percpu(v, &file_lock_list.hlist, &iter->li_cpu, pos); 29441da177e4SLinus Torvalds } 29457f8ada98SPavel Emelyanov 29467f8ada98SPavel Emelyanov static void locks_stop(struct seq_file *f, void *v) 2947b03dfdecSJeff Layton __releases(&blocked_lock_lock) 29487f8ada98SPavel Emelyanov { 29497b2296afSJeff Layton spin_unlock(&blocked_lock_lock); 2950aba37660SPeter Zijlstra percpu_up_write(&file_rwsem); 29511da177e4SLinus Torvalds } 29521da177e4SLinus Torvalds 2953d8ba7a36SAlexey Dobriyan static const struct seq_operations locks_seq_operations = { 29547f8ada98SPavel Emelyanov .start = locks_start, 29557f8ada98SPavel Emelyanov .next = locks_next, 29567f8ada98SPavel Emelyanov .stop = locks_stop, 29577f8ada98SPavel Emelyanov .show = locks_show, 29587f8ada98SPavel Emelyanov }; 2959d8ba7a36SAlexey Dobriyan 2960d8ba7a36SAlexey Dobriyan static int __init proc_locks_init(void) 2961d8ba7a36SAlexey Dobriyan { 296244414d82SChristoph Hellwig proc_create_seq_private("locks", 0, NULL, &locks_seq_operations, 296344414d82SChristoph Hellwig sizeof(struct locks_iterator), NULL); 2964d8ba7a36SAlexey Dobriyan return 0; 2965d8ba7a36SAlexey Dobriyan } 296691899226SPaul Gortmaker fs_initcall(proc_locks_init); 29677f8ada98SPavel Emelyanov #endif 29687f8ada98SPavel Emelyanov 29691da177e4SLinus Torvalds static int __init filelock_init(void) 29701da177e4SLinus Torvalds { 29717012b02aSJeff Layton int i; 29727012b02aSJeff Layton 29734a075e39SJeff Layton flctx_cache = kmem_cache_create("file_lock_ctx", 29744a075e39SJeff Layton sizeof(struct file_lock_context), 0, SLAB_PANIC, NULL); 29754a075e39SJeff Layton 29761da177e4SLinus Torvalds filelock_cache = kmem_cache_create("file_lock_cache", 2977ee19cc40SMiklos Szeredi sizeof(struct file_lock), 0, SLAB_PANIC, NULL); 2978ee19cc40SMiklos Szeredi 29797c3f654dSPeter Zijlstra for_each_possible_cpu(i) { 29807c3f654dSPeter Zijlstra struct file_lock_list_struct *fll = per_cpu_ptr(&file_lock_list, i); 29817c3f654dSPeter Zijlstra 29827c3f654dSPeter Zijlstra spin_lock_init(&fll->lock); 29837c3f654dSPeter Zijlstra INIT_HLIST_HEAD(&fll->hlist); 29847c3f654dSPeter Zijlstra } 29857012b02aSJeff Layton 2986*18f6622eSJeff Layton lease_notifier_chain_init(); 29871da177e4SLinus Torvalds return 0; 29881da177e4SLinus Torvalds } 29891da177e4SLinus Torvalds core_initcall(filelock_init); 2990