1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * linux/fs/locks.c 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Provide support for fcntl()'s F_GETLK, F_SETLK, and F_SETLKW calls. 61da177e4SLinus Torvalds * Doug Evans (dje@spiff.uucp), August 07, 1992 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * Deadlock detection added. 91da177e4SLinus Torvalds * FIXME: one thing isn't handled yet: 101da177e4SLinus Torvalds * - mandatory locks (requires lots of changes elsewhere) 111da177e4SLinus Torvalds * Kelly Carmichael (kelly@[142.24.8.65]), September 17, 1994. 121da177e4SLinus Torvalds * 131da177e4SLinus Torvalds * Miscellaneous edits, and a total rewrite of posix_lock_file() code. 141da177e4SLinus Torvalds * Kai Petzke (wpp@marie.physik.tu-berlin.de), 1994 151da177e4SLinus Torvalds * 161da177e4SLinus Torvalds * Converted file_lock_table to a linked list from an array, which eliminates 171da177e4SLinus Torvalds * the limits on how many active file locks are open. 181da177e4SLinus Torvalds * Chad Page (pageone@netcom.com), November 27, 1994 191da177e4SLinus Torvalds * 201da177e4SLinus Torvalds * Removed dependency on file descriptors. dup()'ed file descriptors now 211da177e4SLinus Torvalds * get the same locks as the original file descriptors, and a close() on 221da177e4SLinus Torvalds * any file descriptor removes ALL the locks on the file for the current 231da177e4SLinus Torvalds * process. Since locks still depend on the process id, locks are inherited 241da177e4SLinus Torvalds * after an exec() but not after a fork(). This agrees with POSIX, and both 251da177e4SLinus Torvalds * BSD and SVR4 practice. 261da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), February 14, 1995 271da177e4SLinus Torvalds * 281da177e4SLinus Torvalds * Scrapped free list which is redundant now that we allocate locks 291da177e4SLinus Torvalds * dynamically with kmalloc()/kfree(). 301da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), February 21, 1995 311da177e4SLinus Torvalds * 321da177e4SLinus Torvalds * Implemented two lock personalities - FL_FLOCK and FL_POSIX. 331da177e4SLinus Torvalds * 341da177e4SLinus Torvalds * FL_POSIX locks are created with calls to fcntl() and lockf() through the 351da177e4SLinus Torvalds * fcntl() system call. They have the semantics described above. 361da177e4SLinus Torvalds * 371da177e4SLinus Torvalds * FL_FLOCK locks are created with calls to flock(), through the flock() 381da177e4SLinus Torvalds * system call, which is new. Old C libraries implement flock() via fcntl() 391da177e4SLinus Torvalds * and will continue to use the old, broken implementation. 401da177e4SLinus Torvalds * 411da177e4SLinus Torvalds * FL_FLOCK locks follow the 4.4 BSD flock() semantics. They are associated 421da177e4SLinus Torvalds * with a file pointer (filp). As a result they can be shared by a parent 431da177e4SLinus Torvalds * process and its children after a fork(). They are removed when the last 441da177e4SLinus Torvalds * file descriptor referring to the file pointer is closed (unless explicitly 451da177e4SLinus Torvalds * unlocked). 461da177e4SLinus Torvalds * 471da177e4SLinus Torvalds * FL_FLOCK locks never deadlock, an existing lock is always removed before 481da177e4SLinus Torvalds * upgrading from shared to exclusive (or vice versa). When this happens 491da177e4SLinus Torvalds * any processes blocked by the current lock are woken up and allowed to 501da177e4SLinus Torvalds * run before the new lock is applied. 511da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), June 09, 1995 521da177e4SLinus Torvalds * 531da177e4SLinus Torvalds * Removed some race conditions in flock_lock_file(), marked other possible 541da177e4SLinus Torvalds * races. Just grep for FIXME to see them. 551da177e4SLinus Torvalds * Dmitry Gorodchanin (pgmdsg@ibi.com), February 09, 1996. 561da177e4SLinus Torvalds * 571da177e4SLinus Torvalds * Addressed Dmitry's concerns. Deadlock checking no longer recursive. 581da177e4SLinus Torvalds * Lock allocation changed to GFP_ATOMIC as we can't afford to sleep 591da177e4SLinus Torvalds * once we've checked for blocking and deadlocking. 601da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), April 03, 1996. 611da177e4SLinus Torvalds * 621da177e4SLinus Torvalds * Initial implementation of mandatory locks. SunOS turned out to be 631da177e4SLinus Torvalds * a rotten model, so I implemented the "obvious" semantics. 64a02dcdf6SMauro Carvalho Chehab * See 'Documentation/filesystems/mandatory-locking.rst' for details. 651da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), April 06, 1996. 661da177e4SLinus Torvalds * 671da177e4SLinus Torvalds * Don't allow mandatory locks on mmap()'ed files. Added simple functions to 681da177e4SLinus Torvalds * check if a file has mandatory locks, used by mmap(), open() and creat() to 691da177e4SLinus Torvalds * see if system call should be rejected. Ref. HP-UX/SunOS/Solaris Reference 701da177e4SLinus Torvalds * Manual, Section 2. 711da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), April 09, 1996. 721da177e4SLinus Torvalds * 731da177e4SLinus Torvalds * Tidied up block list handling. Added '/proc/locks' interface. 741da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), April 24, 1996. 751da177e4SLinus Torvalds * 761da177e4SLinus Torvalds * Fixed deadlock condition for pathological code that mixes calls to 771da177e4SLinus Torvalds * flock() and fcntl(). 781da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), April 29, 1996. 791da177e4SLinus Torvalds * 801da177e4SLinus Torvalds * Allow only one type of locking scheme (FL_POSIX or FL_FLOCK) to be in use 811da177e4SLinus Torvalds * for a given file at a time. Changed the CONFIG_LOCK_MANDATORY scheme to 821da177e4SLinus Torvalds * guarantee sensible behaviour in the case where file system modules might 831da177e4SLinus Torvalds * be compiled with different options than the kernel itself. 841da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), May 15, 1996. 851da177e4SLinus Torvalds * 861da177e4SLinus Torvalds * Added a couple of missing wake_up() calls. Thanks to Thomas Meckel 871da177e4SLinus Torvalds * (Thomas.Meckel@mni.fh-giessen.de) for spotting this. 881da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), May 15, 1996. 891da177e4SLinus Torvalds * 901da177e4SLinus Torvalds * Changed FL_POSIX locks to use the block list in the same way as FL_FLOCK 911da177e4SLinus Torvalds * locks. Changed process synchronisation to avoid dereferencing locks that 921da177e4SLinus Torvalds * have already been freed. 931da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), Sep 21, 1996. 941da177e4SLinus Torvalds * 951da177e4SLinus Torvalds * Made the block list a circular list to minimise searching in the list. 961da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), Sep 25, 1996. 971da177e4SLinus Torvalds * 981da177e4SLinus Torvalds * Made mandatory locking a mount option. Default is not to allow mandatory 991da177e4SLinus Torvalds * locking. 1001da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), Oct 04, 1996. 1011da177e4SLinus Torvalds * 1021da177e4SLinus Torvalds * Some adaptations for NFS support. 1031da177e4SLinus Torvalds * Olaf Kirch (okir@monad.swb.de), Dec 1996, 1041da177e4SLinus Torvalds * 1051da177e4SLinus Torvalds * Fixed /proc/locks interface so that we can't overrun the buffer we are handed. 1061da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), May 12, 1997. 1071da177e4SLinus Torvalds * 1081da177e4SLinus Torvalds * Use slab allocator instead of kmalloc/kfree. 1091da177e4SLinus Torvalds * Use generic list implementation from <linux/list.h>. 1101da177e4SLinus Torvalds * Sped up posix_locks_deadlock by only considering blocked locks. 1111da177e4SLinus Torvalds * Matthew Wilcox <willy@debian.org>, March, 2000. 1121da177e4SLinus Torvalds * 1131da177e4SLinus Torvalds * Leases and LOCK_MAND 1141da177e4SLinus Torvalds * Matthew Wilcox <willy@debian.org>, June, 2000. 1151da177e4SLinus Torvalds * Stephen Rothwell <sfr@canb.auug.org.au>, June, 2000. 116fd7732e0SNeilBrown * 117fd7732e0SNeilBrown * Locking conflicts and dependencies: 118fd7732e0SNeilBrown * If multiple threads attempt to lock the same byte (or flock the same file) 119fd7732e0SNeilBrown * only one can be granted the lock, and other must wait their turn. 120fd7732e0SNeilBrown * The first lock has been "applied" or "granted", the others are "waiting" 121fd7732e0SNeilBrown * and are "blocked" by the "applied" lock.. 122fd7732e0SNeilBrown * 123fd7732e0SNeilBrown * Waiting and applied locks are all kept in trees whose properties are: 124fd7732e0SNeilBrown * 125fd7732e0SNeilBrown * - the root of a tree may be an applied or waiting lock. 126fd7732e0SNeilBrown * - every other node in the tree is a waiting lock that 127fd7732e0SNeilBrown * conflicts with every ancestor of that node. 128fd7732e0SNeilBrown * 129fd7732e0SNeilBrown * Every such tree begins life as a waiting singleton which obviously 130fd7732e0SNeilBrown * satisfies the above properties. 131fd7732e0SNeilBrown * 132fd7732e0SNeilBrown * The only ways we modify trees preserve these properties: 133fd7732e0SNeilBrown * 134fd7732e0SNeilBrown * 1. We may add a new leaf node, but only after first verifying that it 135fd7732e0SNeilBrown * conflicts with all of its ancestors. 136fd7732e0SNeilBrown * 2. We may remove the root of a tree, creating a new singleton 137fd7732e0SNeilBrown * tree from the root and N new trees rooted in the immediate 138fd7732e0SNeilBrown * children. 139fd7732e0SNeilBrown * 3. If the root of a tree is not currently an applied lock, we may 140fd7732e0SNeilBrown * apply it (if possible). 141fd7732e0SNeilBrown * 4. We may upgrade the root of the tree (either extend its range, 142fd7732e0SNeilBrown * or upgrade its entire range from read to write). 143fd7732e0SNeilBrown * 144fd7732e0SNeilBrown * When an applied lock is modified in a way that reduces or downgrades any 145fd7732e0SNeilBrown * part of its range, we remove all its children (2 above). This particularly 146fd7732e0SNeilBrown * happens when a lock is unlocked. 147fd7732e0SNeilBrown * 148fd7732e0SNeilBrown * For each of those child trees we "wake up" the thread which is 149fd7732e0SNeilBrown * waiting for the lock so it can continue handling as follows: if the 150fd7732e0SNeilBrown * root of the tree applies, we do so (3). If it doesn't, it must 151fd7732e0SNeilBrown * conflict with some applied lock. We remove (wake up) all of its children 152fd7732e0SNeilBrown * (2), and add it is a new leaf to the tree rooted in the applied 153fd7732e0SNeilBrown * lock (1). We then repeat the process recursively with those 154fd7732e0SNeilBrown * children. 155fd7732e0SNeilBrown * 1561da177e4SLinus Torvalds */ 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds #include <linux/capability.h> 1591da177e4SLinus Torvalds #include <linux/file.h> 1609f3acc31SAl Viro #include <linux/fdtable.h> 1611da177e4SLinus Torvalds #include <linux/fs.h> 1621da177e4SLinus Torvalds #include <linux/init.h> 1631da177e4SLinus Torvalds #include <linux/security.h> 1641da177e4SLinus Torvalds #include <linux/slab.h> 1651da177e4SLinus Torvalds #include <linux/syscalls.h> 1661da177e4SLinus Torvalds #include <linux/time.h> 1674fb3a538SDipankar Sarma #include <linux/rcupdate.h> 168ab1f1611SVitaliy Gusev #include <linux/pid_namespace.h> 16948f74186SJeff Layton #include <linux/hashtable.h> 1707012b02aSJeff Layton #include <linux/percpu.h> 1711da177e4SLinus Torvalds 17262af4f1fSJeff Layton #define CREATE_TRACE_POINTS 17362af4f1fSJeff Layton #include <trace/events/filelock.h> 17462af4f1fSJeff Layton 1757c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 1761da177e4SLinus Torvalds 1771da177e4SLinus Torvalds #define IS_POSIX(fl) (fl->fl_flags & FL_POSIX) 1781da177e4SLinus Torvalds #define IS_FLOCK(fl) (fl->fl_flags & FL_FLOCK) 17911afe9f7SChristoph Hellwig #define IS_LEASE(fl) (fl->fl_flags & (FL_LEASE|FL_DELEG|FL_LAYOUT)) 180cff2fce5SJeff Layton #define IS_OFDLCK(fl) (fl->fl_flags & FL_OFDLCK) 1819d5b86acSBenjamin Coddington #define IS_REMOTELCK(fl) (fl->fl_pid <= 0) 1821da177e4SLinus Torvalds 183ab83fa4bSJ. Bruce Fields static bool lease_breaking(struct file_lock *fl) 184ab83fa4bSJ. Bruce Fields { 185778fc546SJ. Bruce Fields return fl->fl_flags & (FL_UNLOCK_PENDING | FL_DOWNGRADE_PENDING); 186778fc546SJ. Bruce Fields } 187778fc546SJ. Bruce Fields 188778fc546SJ. Bruce Fields static int target_leasetype(struct file_lock *fl) 189778fc546SJ. Bruce Fields { 190778fc546SJ. Bruce Fields if (fl->fl_flags & FL_UNLOCK_PENDING) 191778fc546SJ. Bruce Fields return F_UNLCK; 192778fc546SJ. Bruce Fields if (fl->fl_flags & FL_DOWNGRADE_PENDING) 193778fc546SJ. Bruce Fields return F_RDLCK; 194778fc546SJ. Bruce Fields return fl->fl_type; 195ab83fa4bSJ. Bruce Fields } 196ab83fa4bSJ. Bruce Fields 1971da177e4SLinus Torvalds int leases_enable = 1; 1981da177e4SLinus Torvalds int lease_break_time = 45; 1991da177e4SLinus Torvalds 2001c8c601aSJeff Layton /* 2017012b02aSJeff Layton * The global file_lock_list is only used for displaying /proc/locks, so we 2027c3f654dSPeter Zijlstra * keep a list on each CPU, with each list protected by its own spinlock. 2037c3f654dSPeter Zijlstra * Global serialization is done using file_rwsem. 2047c3f654dSPeter Zijlstra * 2057c3f654dSPeter Zijlstra * Note that alterations to the list also require that the relevant flc_lock is 2067c3f654dSPeter Zijlstra * held. 2071c8c601aSJeff Layton */ 2087c3f654dSPeter Zijlstra struct file_lock_list_struct { 2097c3f654dSPeter Zijlstra spinlock_t lock; 2107c3f654dSPeter Zijlstra struct hlist_head hlist; 2117c3f654dSPeter Zijlstra }; 2127c3f654dSPeter Zijlstra static DEFINE_PER_CPU(struct file_lock_list_struct, file_lock_list); 213aba37660SPeter Zijlstra DEFINE_STATIC_PERCPU_RWSEM(file_rwsem); 21488974691SJeff Layton 215eb82dd39SJeff Layton 2161c8c601aSJeff Layton /* 21748f74186SJeff Layton * The blocked_hash is used to find POSIX lock loops for deadlock detection. 2187b2296afSJeff Layton * It is protected by blocked_lock_lock. 21948f74186SJeff Layton * 22048f74186SJeff Layton * We hash locks by lockowner in order to optimize searching for the lock a 22148f74186SJeff Layton * particular lockowner is waiting on. 22248f74186SJeff Layton * 22348f74186SJeff Layton * FIXME: make this value scale via some heuristic? We generally will want more 22448f74186SJeff Layton * buckets when we have more lockowners holding locks, but that's a little 22548f74186SJeff Layton * difficult to determine without knowing what the workload will look like. 2261c8c601aSJeff Layton */ 22748f74186SJeff Layton #define BLOCKED_HASH_BITS 7 22848f74186SJeff Layton static DEFINE_HASHTABLE(blocked_hash, BLOCKED_HASH_BITS); 22988974691SJeff Layton 2301c8c601aSJeff Layton /* 2317b2296afSJeff Layton * This lock protects the blocked_hash. Generally, if you're accessing it, you 2327b2296afSJeff Layton * want to be holding this lock. 2331c8c601aSJeff Layton * 234ada5c1daSNeilBrown * In addition, it also protects the fl->fl_blocked_requests list, and the 235ada5c1daSNeilBrown * fl->fl_blocker pointer for file_lock structures that are acting as lock 236ada5c1daSNeilBrown * requests (in contrast to those that are acting as records of acquired locks). 2371c8c601aSJeff Layton * 2381c8c601aSJeff Layton * Note that when we acquire this lock in order to change the above fields, 2396109c850SJeff Layton * we often hold the flc_lock as well. In certain cases, when reading the fields 2401c8c601aSJeff Layton * protected by this lock, we can skip acquiring it iff we already hold the 2416109c850SJeff Layton * flc_lock. 2421c8c601aSJeff Layton */ 2437b2296afSJeff Layton static DEFINE_SPINLOCK(blocked_lock_lock); 2441da177e4SLinus Torvalds 2454a075e39SJeff Layton static struct kmem_cache *flctx_cache __read_mostly; 246e18b890bSChristoph Lameter static struct kmem_cache *filelock_cache __read_mostly; 2471da177e4SLinus Torvalds 2484a075e39SJeff Layton static struct file_lock_context * 2495c1c669aSJeff Layton locks_get_lock_context(struct inode *inode, int type) 2504a075e39SJeff Layton { 251128a3785SDmitry Vyukov struct file_lock_context *ctx; 2524a075e39SJeff Layton 253128a3785SDmitry Vyukov /* paired with cmpxchg() below */ 254128a3785SDmitry Vyukov ctx = smp_load_acquire(&inode->i_flctx); 255128a3785SDmitry Vyukov if (likely(ctx) || type == F_UNLCK) 2564a075e39SJeff Layton goto out; 2574a075e39SJeff Layton 258128a3785SDmitry Vyukov ctx = kmem_cache_alloc(flctx_cache, GFP_KERNEL); 259128a3785SDmitry Vyukov if (!ctx) 2604a075e39SJeff Layton goto out; 2614a075e39SJeff Layton 262128a3785SDmitry Vyukov spin_lock_init(&ctx->flc_lock); 263128a3785SDmitry Vyukov INIT_LIST_HEAD(&ctx->flc_flock); 264128a3785SDmitry Vyukov INIT_LIST_HEAD(&ctx->flc_posix); 265128a3785SDmitry Vyukov INIT_LIST_HEAD(&ctx->flc_lease); 2664a075e39SJeff Layton 2674a075e39SJeff Layton /* 2684a075e39SJeff Layton * Assign the pointer if it's not already assigned. If it is, then 2694a075e39SJeff Layton * free the context we just allocated. 2704a075e39SJeff Layton */ 271128a3785SDmitry Vyukov if (cmpxchg(&inode->i_flctx, NULL, ctx)) { 272128a3785SDmitry Vyukov kmem_cache_free(flctx_cache, ctx); 273128a3785SDmitry Vyukov ctx = smp_load_acquire(&inode->i_flctx); 274128a3785SDmitry Vyukov } 2754a075e39SJeff Layton out: 2761890910fSJeff Layton trace_locks_get_lock_context(inode, type, ctx); 277128a3785SDmitry Vyukov return ctx; 2784a075e39SJeff Layton } 2794a075e39SJeff Layton 280e24dadabSJeff Layton static void 281e24dadabSJeff Layton locks_dump_ctx_list(struct list_head *list, char *list_type) 282e24dadabSJeff Layton { 283e24dadabSJeff Layton struct file_lock *fl; 284e24dadabSJeff Layton 285e24dadabSJeff Layton list_for_each_entry(fl, list, fl_list) { 286e24dadabSJeff Layton pr_warn("%s: fl_owner=%p fl_flags=0x%x fl_type=0x%x fl_pid=%u\n", list_type, fl->fl_owner, fl->fl_flags, fl->fl_type, fl->fl_pid); 287e24dadabSJeff Layton } 288e24dadabSJeff Layton } 289e24dadabSJeff Layton 290e24dadabSJeff Layton static void 291e24dadabSJeff Layton locks_check_ctx_lists(struct inode *inode) 292e24dadabSJeff Layton { 293e24dadabSJeff Layton struct file_lock_context *ctx = inode->i_flctx; 294e24dadabSJeff Layton 295e24dadabSJeff Layton if (unlikely(!list_empty(&ctx->flc_flock) || 296e24dadabSJeff Layton !list_empty(&ctx->flc_posix) || 297e24dadabSJeff Layton !list_empty(&ctx->flc_lease))) { 298e24dadabSJeff Layton pr_warn("Leaked locks on dev=0x%x:0x%x ino=0x%lx:\n", 299e24dadabSJeff Layton MAJOR(inode->i_sb->s_dev), MINOR(inode->i_sb->s_dev), 300e24dadabSJeff Layton inode->i_ino); 301e24dadabSJeff Layton locks_dump_ctx_list(&ctx->flc_flock, "FLOCK"); 302e24dadabSJeff Layton locks_dump_ctx_list(&ctx->flc_posix, "POSIX"); 303e24dadabSJeff Layton locks_dump_ctx_list(&ctx->flc_lease, "LEASE"); 304e24dadabSJeff Layton } 305e24dadabSJeff Layton } 306e24dadabSJeff Layton 3073953704fSBenjamin Coddington static void 3083953704fSBenjamin Coddington locks_check_ctx_file_list(struct file *filp, struct list_head *list, 3093953704fSBenjamin Coddington char *list_type) 3103953704fSBenjamin Coddington { 3113953704fSBenjamin Coddington struct file_lock *fl; 3123953704fSBenjamin Coddington struct inode *inode = locks_inode(filp); 3133953704fSBenjamin Coddington 3143953704fSBenjamin Coddington list_for_each_entry(fl, list, fl_list) 3153953704fSBenjamin Coddington if (fl->fl_file == filp) 3163953704fSBenjamin Coddington pr_warn("Leaked %s lock on dev=0x%x:0x%x ino=0x%lx " 3173953704fSBenjamin Coddington " fl_owner=%p fl_flags=0x%x fl_type=0x%x fl_pid=%u\n", 3183953704fSBenjamin Coddington list_type, MAJOR(inode->i_sb->s_dev), 3193953704fSBenjamin Coddington MINOR(inode->i_sb->s_dev), inode->i_ino, 3203953704fSBenjamin Coddington fl->fl_owner, fl->fl_flags, fl->fl_type, fl->fl_pid); 3213953704fSBenjamin Coddington } 3223953704fSBenjamin Coddington 3234a075e39SJeff Layton void 324f27a0fe0SJeff Layton locks_free_lock_context(struct inode *inode) 3254a075e39SJeff Layton { 326f27a0fe0SJeff Layton struct file_lock_context *ctx = inode->i_flctx; 327f27a0fe0SJeff Layton 328e24dadabSJeff Layton if (unlikely(ctx)) { 329e24dadabSJeff Layton locks_check_ctx_lists(inode); 3304a075e39SJeff Layton kmem_cache_free(flctx_cache, ctx); 3314a075e39SJeff Layton } 3324a075e39SJeff Layton } 3334a075e39SJeff Layton 334ee19cc40SMiklos Szeredi static void locks_init_lock_heads(struct file_lock *fl) 335a51cb91dSMiklos Szeredi { 336139ca04eSJeff Layton INIT_HLIST_NODE(&fl->fl_link); 3376dee60f6SJeff Layton INIT_LIST_HEAD(&fl->fl_list); 338ada5c1daSNeilBrown INIT_LIST_HEAD(&fl->fl_blocked_requests); 339ada5c1daSNeilBrown INIT_LIST_HEAD(&fl->fl_blocked_member); 340ee19cc40SMiklos Szeredi init_waitqueue_head(&fl->fl_wait); 341a51cb91dSMiklos Szeredi } 342a51cb91dSMiklos Szeredi 3431da177e4SLinus Torvalds /* Allocate an empty lock structure. */ 344c5b1f0d9SArnd Bergmann struct file_lock *locks_alloc_lock(void) 3451da177e4SLinus Torvalds { 346ee19cc40SMiklos Szeredi struct file_lock *fl = kmem_cache_zalloc(filelock_cache, GFP_KERNEL); 347a51cb91dSMiklos Szeredi 348a51cb91dSMiklos Szeredi if (fl) 349ee19cc40SMiklos Szeredi locks_init_lock_heads(fl); 350a51cb91dSMiklos Szeredi 351a51cb91dSMiklos Szeredi return fl; 3521da177e4SLinus Torvalds } 353c5b1f0d9SArnd Bergmann EXPORT_SYMBOL_GPL(locks_alloc_lock); 3541da177e4SLinus Torvalds 355a9e61e25SFelix Blyakher void locks_release_private(struct file_lock *fl) 35647831f35STrond Myklebust { 3575926459eSNeilBrown BUG_ON(waitqueue_active(&fl->fl_wait)); 3585926459eSNeilBrown BUG_ON(!list_empty(&fl->fl_list)); 3595926459eSNeilBrown BUG_ON(!list_empty(&fl->fl_blocked_requests)); 3605926459eSNeilBrown BUG_ON(!list_empty(&fl->fl_blocked_member)); 3615926459eSNeilBrown BUG_ON(!hlist_unhashed(&fl->fl_link)); 3625926459eSNeilBrown 36347831f35STrond Myklebust if (fl->fl_ops) { 36447831f35STrond Myklebust if (fl->fl_ops->fl_release_private) 36547831f35STrond Myklebust fl->fl_ops->fl_release_private(fl); 36647831f35STrond Myklebust fl->fl_ops = NULL; 36747831f35STrond Myklebust } 36847831f35STrond Myklebust 3695c97d7b1SKinglong Mee if (fl->fl_lmops) { 370cae80b30SJeff Layton if (fl->fl_lmops->lm_put_owner) { 371cae80b30SJeff Layton fl->fl_lmops->lm_put_owner(fl->fl_owner); 372cae80b30SJeff Layton fl->fl_owner = NULL; 373cae80b30SJeff Layton } 3745c97d7b1SKinglong Mee fl->fl_lmops = NULL; 3755c97d7b1SKinglong Mee } 37647831f35STrond Myklebust } 377a9e61e25SFelix Blyakher EXPORT_SYMBOL_GPL(locks_release_private); 37847831f35STrond Myklebust 3791da177e4SLinus Torvalds /* Free a lock which is not in use. */ 38005fa3135SJ. Bruce Fields void locks_free_lock(struct file_lock *fl) 3811da177e4SLinus Torvalds { 38247831f35STrond Myklebust locks_release_private(fl); 3831da177e4SLinus Torvalds kmem_cache_free(filelock_cache, fl); 3841da177e4SLinus Torvalds } 38505fa3135SJ. Bruce Fields EXPORT_SYMBOL(locks_free_lock); 3861da177e4SLinus Torvalds 387ed9814d8SJeff Layton static void 388ed9814d8SJeff Layton locks_dispose_list(struct list_head *dispose) 389ed9814d8SJeff Layton { 390ed9814d8SJeff Layton struct file_lock *fl; 391ed9814d8SJeff Layton 392ed9814d8SJeff Layton while (!list_empty(dispose)) { 3936dee60f6SJeff Layton fl = list_first_entry(dispose, struct file_lock, fl_list); 3946dee60f6SJeff Layton list_del_init(&fl->fl_list); 395ed9814d8SJeff Layton locks_free_lock(fl); 396ed9814d8SJeff Layton } 397ed9814d8SJeff Layton } 398ed9814d8SJeff Layton 3991da177e4SLinus Torvalds void locks_init_lock(struct file_lock *fl) 4001da177e4SLinus Torvalds { 401ee19cc40SMiklos Szeredi memset(fl, 0, sizeof(struct file_lock)); 402ee19cc40SMiklos Szeredi locks_init_lock_heads(fl); 4031da177e4SLinus Torvalds } 4041da177e4SLinus Torvalds EXPORT_SYMBOL(locks_init_lock); 4051da177e4SLinus Torvalds 4061da177e4SLinus Torvalds /* 4071da177e4SLinus Torvalds * Initialize a new lock from an existing file_lock structure. 4081da177e4SLinus Torvalds */ 4093fe0fff1SKinglong Mee void locks_copy_conflock(struct file_lock *new, struct file_lock *fl) 4101da177e4SLinus Torvalds { 4111da177e4SLinus Torvalds new->fl_owner = fl->fl_owner; 4121da177e4SLinus Torvalds new->fl_pid = fl->fl_pid; 4130996905fSTrond Myklebust new->fl_file = NULL; 4141da177e4SLinus Torvalds new->fl_flags = fl->fl_flags; 4151da177e4SLinus Torvalds new->fl_type = fl->fl_type; 4161da177e4SLinus Torvalds new->fl_start = fl->fl_start; 4171da177e4SLinus Torvalds new->fl_end = fl->fl_end; 418f328296eSKinglong Mee new->fl_lmops = fl->fl_lmops; 4190996905fSTrond Myklebust new->fl_ops = NULL; 420f328296eSKinglong Mee 421f328296eSKinglong Mee if (fl->fl_lmops) { 422f328296eSKinglong Mee if (fl->fl_lmops->lm_get_owner) 423cae80b30SJeff Layton fl->fl_lmops->lm_get_owner(fl->fl_owner); 424f328296eSKinglong Mee } 4250996905fSTrond Myklebust } 4263fe0fff1SKinglong Mee EXPORT_SYMBOL(locks_copy_conflock); 4270996905fSTrond Myklebust 4280996905fSTrond Myklebust void locks_copy_lock(struct file_lock *new, struct file_lock *fl) 4290996905fSTrond Myklebust { 430566709bdSJeff Layton /* "new" must be a freshly-initialized lock */ 431566709bdSJeff Layton WARN_ON_ONCE(new->fl_ops); 4320996905fSTrond Myklebust 4333fe0fff1SKinglong Mee locks_copy_conflock(new, fl); 434f328296eSKinglong Mee 4350996905fSTrond Myklebust new->fl_file = fl->fl_file; 4361da177e4SLinus Torvalds new->fl_ops = fl->fl_ops; 43747831f35STrond Myklebust 438f328296eSKinglong Mee if (fl->fl_ops) { 439f328296eSKinglong Mee if (fl->fl_ops->fl_copy_lock) 440f328296eSKinglong Mee fl->fl_ops->fl_copy_lock(new, fl); 441f328296eSKinglong Mee } 4421da177e4SLinus Torvalds } 4431da177e4SLinus Torvalds EXPORT_SYMBOL(locks_copy_lock); 4441da177e4SLinus Torvalds 4455946c431SNeilBrown static void locks_move_blocks(struct file_lock *new, struct file_lock *fl) 4465946c431SNeilBrown { 4475946c431SNeilBrown struct file_lock *f; 4485946c431SNeilBrown 4495946c431SNeilBrown /* 4505946c431SNeilBrown * As ctx->flc_lock is held, new requests cannot be added to 4515946c431SNeilBrown * ->fl_blocked_requests, so we don't need a lock to check if it 4525946c431SNeilBrown * is empty. 4535946c431SNeilBrown */ 4545946c431SNeilBrown if (list_empty(&fl->fl_blocked_requests)) 4555946c431SNeilBrown return; 4565946c431SNeilBrown spin_lock(&blocked_lock_lock); 4575946c431SNeilBrown list_splice_init(&fl->fl_blocked_requests, &new->fl_blocked_requests); 458bf77ae4cSNeilBrown list_for_each_entry(f, &new->fl_blocked_requests, fl_blocked_member) 4595946c431SNeilBrown f->fl_blocker = new; 4605946c431SNeilBrown spin_unlock(&blocked_lock_lock); 4615946c431SNeilBrown } 4625946c431SNeilBrown 4631da177e4SLinus Torvalds static inline int flock_translate_cmd(int cmd) { 4641da177e4SLinus Torvalds if (cmd & LOCK_MAND) 4651da177e4SLinus Torvalds return cmd & (LOCK_MAND | LOCK_RW); 4661da177e4SLinus Torvalds switch (cmd) { 4671da177e4SLinus Torvalds case LOCK_SH: 4681da177e4SLinus Torvalds return F_RDLCK; 4691da177e4SLinus Torvalds case LOCK_EX: 4701da177e4SLinus Torvalds return F_WRLCK; 4711da177e4SLinus Torvalds case LOCK_UN: 4721da177e4SLinus Torvalds return F_UNLCK; 4731da177e4SLinus Torvalds } 4741da177e4SLinus Torvalds return -EINVAL; 4751da177e4SLinus Torvalds } 4761da177e4SLinus Torvalds 4771da177e4SLinus Torvalds /* Fill in a file_lock structure with an appropriate FLOCK lock. */ 4786e129d00SJeff Layton static struct file_lock * 479d6367d62SNeilBrown flock_make_lock(struct file *filp, unsigned int cmd, struct file_lock *fl) 4801da177e4SLinus Torvalds { 4811da177e4SLinus Torvalds int type = flock_translate_cmd(cmd); 4826e129d00SJeff Layton 4831da177e4SLinus Torvalds if (type < 0) 4846e129d00SJeff Layton return ERR_PTR(type); 4851da177e4SLinus Torvalds 486d6367d62SNeilBrown if (fl == NULL) { 4871da177e4SLinus Torvalds fl = locks_alloc_lock(); 4881da177e4SLinus Torvalds if (fl == NULL) 4896e129d00SJeff Layton return ERR_PTR(-ENOMEM); 490d6367d62SNeilBrown } else { 491d6367d62SNeilBrown locks_init_lock(fl); 492d6367d62SNeilBrown } 4931da177e4SLinus Torvalds 4941da177e4SLinus Torvalds fl->fl_file = filp; 49573a8f5f7SChristoph Hellwig fl->fl_owner = filp; 4961da177e4SLinus Torvalds fl->fl_pid = current->tgid; 4971da177e4SLinus Torvalds fl->fl_flags = FL_FLOCK; 4981da177e4SLinus Torvalds fl->fl_type = type; 4991da177e4SLinus Torvalds fl->fl_end = OFFSET_MAX; 5001da177e4SLinus Torvalds 5016e129d00SJeff Layton return fl; 5021da177e4SLinus Torvalds } 5031da177e4SLinus Torvalds 5040ec4f431SJ. Bruce Fields static int assign_type(struct file_lock *fl, long type) 5051da177e4SLinus Torvalds { 5061da177e4SLinus Torvalds switch (type) { 5071da177e4SLinus Torvalds case F_RDLCK: 5081da177e4SLinus Torvalds case F_WRLCK: 5091da177e4SLinus Torvalds case F_UNLCK: 5101da177e4SLinus Torvalds fl->fl_type = type; 5111da177e4SLinus Torvalds break; 5121da177e4SLinus Torvalds default: 5131da177e4SLinus Torvalds return -EINVAL; 5141da177e4SLinus Torvalds } 5151da177e4SLinus Torvalds return 0; 5161da177e4SLinus Torvalds } 5171da177e4SLinus Torvalds 518ef12e72aSJ. Bruce Fields static int flock64_to_posix_lock(struct file *filp, struct file_lock *fl, 519ef12e72aSJ. Bruce Fields struct flock64 *l) 520ef12e72aSJ. Bruce Fields { 521ef12e72aSJ. Bruce Fields switch (l->l_whence) { 522ef12e72aSJ. Bruce Fields case SEEK_SET: 523ef12e72aSJ. Bruce Fields fl->fl_start = 0; 524ef12e72aSJ. Bruce Fields break; 525ef12e72aSJ. Bruce Fields case SEEK_CUR: 526ef12e72aSJ. Bruce Fields fl->fl_start = filp->f_pos; 527ef12e72aSJ. Bruce Fields break; 528ef12e72aSJ. Bruce Fields case SEEK_END: 529ef12e72aSJ. Bruce Fields fl->fl_start = i_size_read(file_inode(filp)); 530ef12e72aSJ. Bruce Fields break; 531ef12e72aSJ. Bruce Fields default: 532ef12e72aSJ. Bruce Fields return -EINVAL; 533ef12e72aSJ. Bruce Fields } 534ef12e72aSJ. Bruce Fields if (l->l_start > OFFSET_MAX - fl->fl_start) 535ef12e72aSJ. Bruce Fields return -EOVERFLOW; 536ef12e72aSJ. Bruce Fields fl->fl_start += l->l_start; 537ef12e72aSJ. Bruce Fields if (fl->fl_start < 0) 538ef12e72aSJ. Bruce Fields return -EINVAL; 539ef12e72aSJ. Bruce Fields 540ef12e72aSJ. Bruce Fields /* POSIX-1996 leaves the case l->l_len < 0 undefined; 541ef12e72aSJ. Bruce Fields POSIX-2001 defines it. */ 542ef12e72aSJ. Bruce Fields if (l->l_len > 0) { 543ef12e72aSJ. Bruce Fields if (l->l_len - 1 > OFFSET_MAX - fl->fl_start) 544ef12e72aSJ. Bruce Fields return -EOVERFLOW; 54516238415SLuo Meng fl->fl_end = fl->fl_start + (l->l_len - 1); 546ef12e72aSJ. Bruce Fields 547ef12e72aSJ. Bruce Fields } else if (l->l_len < 0) { 548ef12e72aSJ. Bruce Fields if (fl->fl_start + l->l_len < 0) 549ef12e72aSJ. Bruce Fields return -EINVAL; 550ef12e72aSJ. Bruce Fields fl->fl_end = fl->fl_start - 1; 551ef12e72aSJ. Bruce Fields fl->fl_start += l->l_len; 552ef12e72aSJ. Bruce Fields } else 553ef12e72aSJ. Bruce Fields fl->fl_end = OFFSET_MAX; 554ef12e72aSJ. Bruce Fields 555ef12e72aSJ. Bruce Fields fl->fl_owner = current->files; 556ef12e72aSJ. Bruce Fields fl->fl_pid = current->tgid; 557ef12e72aSJ. Bruce Fields fl->fl_file = filp; 558ef12e72aSJ. Bruce Fields fl->fl_flags = FL_POSIX; 559ef12e72aSJ. Bruce Fields fl->fl_ops = NULL; 560ef12e72aSJ. Bruce Fields fl->fl_lmops = NULL; 561ef12e72aSJ. Bruce Fields 562ef12e72aSJ. Bruce Fields return assign_type(fl, l->l_type); 563ef12e72aSJ. Bruce Fields } 564ef12e72aSJ. Bruce Fields 5651da177e4SLinus Torvalds /* Verify a "struct flock" and copy it to a "struct file_lock" as a POSIX 5661da177e4SLinus Torvalds * style lock. 5671da177e4SLinus Torvalds */ 5681da177e4SLinus Torvalds static int flock_to_posix_lock(struct file *filp, struct file_lock *fl, 5691da177e4SLinus Torvalds struct flock *l) 5701da177e4SLinus Torvalds { 571ef12e72aSJ. Bruce Fields struct flock64 ll = { 572ef12e72aSJ. Bruce Fields .l_type = l->l_type, 573ef12e72aSJ. Bruce Fields .l_whence = l->l_whence, 574ef12e72aSJ. Bruce Fields .l_start = l->l_start, 575ef12e72aSJ. Bruce Fields .l_len = l->l_len, 576ef12e72aSJ. Bruce Fields }; 5771da177e4SLinus Torvalds 578ef12e72aSJ. Bruce Fields return flock64_to_posix_lock(filp, fl, &ll); 5791da177e4SLinus Torvalds } 5801da177e4SLinus Torvalds 5811da177e4SLinus Torvalds /* default lease lock manager operations */ 5824d01b7f5SJeff Layton static bool 5834d01b7f5SJeff Layton lease_break_callback(struct file_lock *fl) 5841da177e4SLinus Torvalds { 5851da177e4SLinus Torvalds kill_fasync(&fl->fl_fasync, SIGIO, POLL_MSG); 5864d01b7f5SJeff Layton return false; 5871da177e4SLinus Torvalds } 5881da177e4SLinus Torvalds 5891c7dd2ffSJeff Layton static void 5901c7dd2ffSJeff Layton lease_setup(struct file_lock *fl, void **priv) 5911c7dd2ffSJeff Layton { 5921c7dd2ffSJeff Layton struct file *filp = fl->fl_file; 5931c7dd2ffSJeff Layton struct fasync_struct *fa = *priv; 5941c7dd2ffSJeff Layton 5951c7dd2ffSJeff Layton /* 5961c7dd2ffSJeff Layton * fasync_insert_entry() returns the old entry if any. If there was no 5971c7dd2ffSJeff Layton * old entry, then it used "priv" and inserted it into the fasync list. 5981c7dd2ffSJeff Layton * Clear the pointer to indicate that it shouldn't be freed. 5991c7dd2ffSJeff Layton */ 6001c7dd2ffSJeff Layton if (!fasync_insert_entry(fa->fa_fd, filp, &fl->fl_fasync, fa)) 6011c7dd2ffSJeff Layton *priv = NULL; 6021c7dd2ffSJeff Layton 60301919134SEric W. Biederman __f_setown(filp, task_pid(current), PIDTYPE_TGID, 0); 6041c7dd2ffSJeff Layton } 6051c7dd2ffSJeff Layton 6067b021967SAlexey Dobriyan static const struct lock_manager_operations lease_manager_ops = { 6078fb47a4fSJ. Bruce Fields .lm_break = lease_break_callback, 6088fb47a4fSJ. Bruce Fields .lm_change = lease_modify, 6091c7dd2ffSJeff Layton .lm_setup = lease_setup, 6101da177e4SLinus Torvalds }; 6111da177e4SLinus Torvalds 6121da177e4SLinus Torvalds /* 6131da177e4SLinus Torvalds * Initialize a lease, use the default lock manager operations 6141da177e4SLinus Torvalds */ 6150ec4f431SJ. Bruce Fields static int lease_init(struct file *filp, long type, struct file_lock *fl) 6161da177e4SLinus Torvalds { 61775dff55aSTrond Myklebust if (assign_type(fl, type) != 0) 61875dff55aSTrond Myklebust return -EINVAL; 61975dff55aSTrond Myklebust 6207ca76311SJeff Layton fl->fl_owner = filp; 6211da177e4SLinus Torvalds fl->fl_pid = current->tgid; 6221da177e4SLinus Torvalds 6231da177e4SLinus Torvalds fl->fl_file = filp; 6241da177e4SLinus Torvalds fl->fl_flags = FL_LEASE; 6251da177e4SLinus Torvalds fl->fl_start = 0; 6261da177e4SLinus Torvalds fl->fl_end = OFFSET_MAX; 6271da177e4SLinus Torvalds fl->fl_ops = NULL; 6281da177e4SLinus Torvalds fl->fl_lmops = &lease_manager_ops; 6291da177e4SLinus Torvalds return 0; 6301da177e4SLinus Torvalds } 6311da177e4SLinus Torvalds 6321da177e4SLinus Torvalds /* Allocate a file_lock initialised to this type of lease */ 6330ec4f431SJ. Bruce Fields static struct file_lock *lease_alloc(struct file *filp, long type) 6341da177e4SLinus Torvalds { 6351da177e4SLinus Torvalds struct file_lock *fl = locks_alloc_lock(); 63675dff55aSTrond Myklebust int error = -ENOMEM; 6371da177e4SLinus Torvalds 6381da177e4SLinus Torvalds if (fl == NULL) 639e32b8ee2SJ. Bruce Fields return ERR_PTR(error); 6401da177e4SLinus Torvalds 6411da177e4SLinus Torvalds error = lease_init(filp, type, fl); 64275dff55aSTrond Myklebust if (error) { 64375dff55aSTrond Myklebust locks_free_lock(fl); 644e32b8ee2SJ. Bruce Fields return ERR_PTR(error); 64575dff55aSTrond Myklebust } 646e32b8ee2SJ. Bruce Fields return fl; 6471da177e4SLinus Torvalds } 6481da177e4SLinus Torvalds 6491da177e4SLinus Torvalds /* Check if two locks overlap each other. 6501da177e4SLinus Torvalds */ 6511da177e4SLinus Torvalds static inline int locks_overlap(struct file_lock *fl1, struct file_lock *fl2) 6521da177e4SLinus Torvalds { 6531da177e4SLinus Torvalds return ((fl1->fl_end >= fl2->fl_start) && 6541da177e4SLinus Torvalds (fl2->fl_end >= fl1->fl_start)); 6551da177e4SLinus Torvalds } 6561da177e4SLinus Torvalds 6571da177e4SLinus Torvalds /* 6581da177e4SLinus Torvalds * Check whether two locks have the same owner. 6591da177e4SLinus Torvalds */ 66033443c42SMatt Mackall static int posix_same_owner(struct file_lock *fl1, struct file_lock *fl2) 6611da177e4SLinus Torvalds { 6621da177e4SLinus Torvalds return fl1->fl_owner == fl2->fl_owner; 6631da177e4SLinus Torvalds } 6641da177e4SLinus Torvalds 6656109c850SJeff Layton /* Must be called with the flc_lock held! */ 6666ca10ed8SJeff Layton static void locks_insert_global_locks(struct file_lock *fl) 66788974691SJeff Layton { 6687c3f654dSPeter Zijlstra struct file_lock_list_struct *fll = this_cpu_ptr(&file_lock_list); 6697c3f654dSPeter Zijlstra 670aba37660SPeter Zijlstra percpu_rwsem_assert_held(&file_rwsem); 671aba37660SPeter Zijlstra 6727c3f654dSPeter Zijlstra spin_lock(&fll->lock); 6737012b02aSJeff Layton fl->fl_link_cpu = smp_processor_id(); 6747c3f654dSPeter Zijlstra hlist_add_head(&fl->fl_link, &fll->hlist); 6757c3f654dSPeter Zijlstra spin_unlock(&fll->lock); 67688974691SJeff Layton } 67788974691SJeff Layton 6786109c850SJeff Layton /* Must be called with the flc_lock held! */ 6796ca10ed8SJeff Layton static void locks_delete_global_locks(struct file_lock *fl) 68088974691SJeff Layton { 6817c3f654dSPeter Zijlstra struct file_lock_list_struct *fll; 6827c3f654dSPeter Zijlstra 683aba37660SPeter Zijlstra percpu_rwsem_assert_held(&file_rwsem); 684aba37660SPeter Zijlstra 6857012b02aSJeff Layton /* 6867012b02aSJeff Layton * Avoid taking lock if already unhashed. This is safe since this check 6876109c850SJeff Layton * is done while holding the flc_lock, and new insertions into the list 6887012b02aSJeff Layton * also require that it be held. 6897012b02aSJeff Layton */ 6907012b02aSJeff Layton if (hlist_unhashed(&fl->fl_link)) 6917012b02aSJeff Layton return; 6927c3f654dSPeter Zijlstra 6937c3f654dSPeter Zijlstra fll = per_cpu_ptr(&file_lock_list, fl->fl_link_cpu); 6947c3f654dSPeter Zijlstra spin_lock(&fll->lock); 695139ca04eSJeff Layton hlist_del_init(&fl->fl_link); 6967c3f654dSPeter Zijlstra spin_unlock(&fll->lock); 69788974691SJeff Layton } 69888974691SJeff Layton 6993999e493SJeff Layton static unsigned long 7003999e493SJeff Layton posix_owner_key(struct file_lock *fl) 7013999e493SJeff Layton { 7023999e493SJeff Layton return (unsigned long)fl->fl_owner; 7033999e493SJeff Layton } 7043999e493SJeff Layton 7056ca10ed8SJeff Layton static void locks_insert_global_blocked(struct file_lock *waiter) 70688974691SJeff Layton { 707663d5af7SDaniel Wagner lockdep_assert_held(&blocked_lock_lock); 708663d5af7SDaniel Wagner 7093999e493SJeff Layton hash_add(blocked_hash, &waiter->fl_link, posix_owner_key(waiter)); 71088974691SJeff Layton } 71188974691SJeff Layton 7126ca10ed8SJeff Layton static void locks_delete_global_blocked(struct file_lock *waiter) 71388974691SJeff Layton { 714663d5af7SDaniel Wagner lockdep_assert_held(&blocked_lock_lock); 715663d5af7SDaniel Wagner 71648f74186SJeff Layton hash_del(&waiter->fl_link); 71788974691SJeff Layton } 71888974691SJeff Layton 7191da177e4SLinus Torvalds /* Remove waiter from blocker's block list. 7201da177e4SLinus Torvalds * When blocker ends up pointing to itself then the list is empty. 7211c8c601aSJeff Layton * 7227b2296afSJeff Layton * Must be called with blocked_lock_lock held. 7231da177e4SLinus Torvalds */ 72433443c42SMatt Mackall static void __locks_delete_block(struct file_lock *waiter) 7251da177e4SLinus Torvalds { 72688974691SJeff Layton locks_delete_global_blocked(waiter); 727ada5c1daSNeilBrown list_del_init(&waiter->fl_blocked_member); 7281da177e4SLinus Torvalds } 7291da177e4SLinus Torvalds 730ad6bbd8bSNeilBrown static void __locks_wake_up_blocks(struct file_lock *blocker) 731ad6bbd8bSNeilBrown { 732ad6bbd8bSNeilBrown while (!list_empty(&blocker->fl_blocked_requests)) { 733ad6bbd8bSNeilBrown struct file_lock *waiter; 734ad6bbd8bSNeilBrown 735ad6bbd8bSNeilBrown waiter = list_first_entry(&blocker->fl_blocked_requests, 736ad6bbd8bSNeilBrown struct file_lock, fl_blocked_member); 737ad6bbd8bSNeilBrown __locks_delete_block(waiter); 738ad6bbd8bSNeilBrown if (waiter->fl_lmops && waiter->fl_lmops->lm_notify) 739ad6bbd8bSNeilBrown waiter->fl_lmops->lm_notify(waiter); 740ad6bbd8bSNeilBrown else 741ad6bbd8bSNeilBrown wake_up(&waiter->fl_wait); 742dcf23ac3SLinus Torvalds 743dcf23ac3SLinus Torvalds /* 744dcf23ac3SLinus Torvalds * The setting of fl_blocker to NULL marks the "done" 745dcf23ac3SLinus Torvalds * point in deleting a block. Paired with acquire at the top 746dcf23ac3SLinus Torvalds * of locks_delete_block(). 747dcf23ac3SLinus Torvalds */ 748dcf23ac3SLinus Torvalds smp_store_release(&waiter->fl_blocker, NULL); 749ad6bbd8bSNeilBrown } 750ad6bbd8bSNeilBrown } 751ad6bbd8bSNeilBrown 752cb03f94fSNeilBrown /** 753529adfe8SMauro Carvalho Chehab * locks_delete_block - stop waiting for a file lock 754cb03f94fSNeilBrown * @waiter: the lock which was waiting 755cb03f94fSNeilBrown * 756cb03f94fSNeilBrown * lockd/nfsd need to disconnect the lock while working on it. 757cb03f94fSNeilBrown */ 758cb03f94fSNeilBrown int locks_delete_block(struct file_lock *waiter) 7591da177e4SLinus Torvalds { 760cb03f94fSNeilBrown int status = -ENOENT; 761cb03f94fSNeilBrown 762dcf23ac3SLinus Torvalds /* 763dcf23ac3SLinus Torvalds * If fl_blocker is NULL, it won't be set again as this thread "owns" 764dcf23ac3SLinus Torvalds * the lock and is the only one that might try to claim the lock. 765dcf23ac3SLinus Torvalds * 766dcf23ac3SLinus Torvalds * We use acquire/release to manage fl_blocker so that we can 767dcf23ac3SLinus Torvalds * optimize away taking the blocked_lock_lock in many cases. 768dcf23ac3SLinus Torvalds * 769dcf23ac3SLinus Torvalds * The smp_load_acquire guarantees two things: 770dcf23ac3SLinus Torvalds * 771dcf23ac3SLinus Torvalds * 1/ that fl_blocked_requests can be tested locklessly. If something 772dcf23ac3SLinus Torvalds * was recently added to that list it must have been in a locked region 773dcf23ac3SLinus Torvalds * *before* the locked region when fl_blocker was set to NULL. 774dcf23ac3SLinus Torvalds * 775dcf23ac3SLinus Torvalds * 2/ that no other thread is accessing 'waiter', so it is safe to free 776dcf23ac3SLinus Torvalds * it. __locks_wake_up_blocks is careful not to touch waiter after 777dcf23ac3SLinus Torvalds * fl_blocker is released. 778dcf23ac3SLinus Torvalds * 779dcf23ac3SLinus Torvalds * If a lockless check of fl_blocker shows it to be NULL, we know that 780dcf23ac3SLinus Torvalds * no new locks can be inserted into its fl_blocked_requests list, and 781dcf23ac3SLinus Torvalds * can avoid doing anything further if the list is empty. 782dcf23ac3SLinus Torvalds */ 783dcf23ac3SLinus Torvalds if (!smp_load_acquire(&waiter->fl_blocker) && 784dcf23ac3SLinus Torvalds list_empty(&waiter->fl_blocked_requests)) 785dcf23ac3SLinus Torvalds return status; 786dcf23ac3SLinus Torvalds 7877b2296afSJeff Layton spin_lock(&blocked_lock_lock); 788cb03f94fSNeilBrown if (waiter->fl_blocker) 789cb03f94fSNeilBrown status = 0; 7905946c431SNeilBrown __locks_wake_up_blocks(waiter); 7911da177e4SLinus Torvalds __locks_delete_block(waiter); 792dcf23ac3SLinus Torvalds 793dcf23ac3SLinus Torvalds /* 794dcf23ac3SLinus Torvalds * The setting of fl_blocker to NULL marks the "done" point in deleting 795dcf23ac3SLinus Torvalds * a block. Paired with acquire at the top of this function. 796dcf23ac3SLinus Torvalds */ 797dcf23ac3SLinus Torvalds smp_store_release(&waiter->fl_blocker, NULL); 7987b2296afSJeff Layton spin_unlock(&blocked_lock_lock); 799cb03f94fSNeilBrown return status; 8001da177e4SLinus Torvalds } 801cb03f94fSNeilBrown EXPORT_SYMBOL(locks_delete_block); 8021da177e4SLinus Torvalds 8031da177e4SLinus Torvalds /* Insert waiter into blocker's block list. 8041da177e4SLinus Torvalds * We use a circular list so that processes can be easily woken up in 8051da177e4SLinus Torvalds * the order they blocked. The documentation doesn't require this but 8061da177e4SLinus Torvalds * it seems like the reasonable thing to do. 8071c8c601aSJeff Layton * 8086109c850SJeff Layton * Must be called with both the flc_lock and blocked_lock_lock held. The 809ada5c1daSNeilBrown * fl_blocked_requests list itself is protected by the blocked_lock_lock, 810ada5c1daSNeilBrown * but by ensuring that the flc_lock is also held on insertions we can avoid 811ada5c1daSNeilBrown * taking the blocked_lock_lock in some cases when we see that the 812ada5c1daSNeilBrown * fl_blocked_requests list is empty. 813fd7732e0SNeilBrown * 814fd7732e0SNeilBrown * Rather than just adding to the list, we check for conflicts with any existing 815fd7732e0SNeilBrown * waiters, and add beneath any waiter that blocks the new waiter. 816fd7732e0SNeilBrown * Thus wakeups don't happen until needed. 8171da177e4SLinus Torvalds */ 8181c8c601aSJeff Layton static void __locks_insert_block(struct file_lock *blocker, 819fd7732e0SNeilBrown struct file_lock *waiter, 820fd7732e0SNeilBrown bool conflict(struct file_lock *, 821fd7732e0SNeilBrown struct file_lock *)) 8221da177e4SLinus Torvalds { 823fd7732e0SNeilBrown struct file_lock *fl; 824ada5c1daSNeilBrown BUG_ON(!list_empty(&waiter->fl_blocked_member)); 825fd7732e0SNeilBrown 826fd7732e0SNeilBrown new_blocker: 827fd7732e0SNeilBrown list_for_each_entry(fl, &blocker->fl_blocked_requests, fl_blocked_member) 828fd7732e0SNeilBrown if (conflict(fl, waiter)) { 829fd7732e0SNeilBrown blocker = fl; 830fd7732e0SNeilBrown goto new_blocker; 831fd7732e0SNeilBrown } 832ada5c1daSNeilBrown waiter->fl_blocker = blocker; 833ada5c1daSNeilBrown list_add_tail(&waiter->fl_blocked_member, &blocker->fl_blocked_requests); 834cff2fce5SJeff Layton if (IS_POSIX(blocker) && !IS_OFDLCK(blocker)) 8351c8c601aSJeff Layton locks_insert_global_blocked(waiter); 8365946c431SNeilBrown 8375946c431SNeilBrown /* The requests in waiter->fl_blocked are known to conflict with 8385946c431SNeilBrown * waiter, but might not conflict with blocker, or the requests 8395946c431SNeilBrown * and lock which block it. So they all need to be woken. 8405946c431SNeilBrown */ 8415946c431SNeilBrown __locks_wake_up_blocks(waiter); 8421c8c601aSJeff Layton } 8431c8c601aSJeff Layton 8446109c850SJeff Layton /* Must be called with flc_lock held. */ 8451c8c601aSJeff Layton static void locks_insert_block(struct file_lock *blocker, 846fd7732e0SNeilBrown struct file_lock *waiter, 847fd7732e0SNeilBrown bool conflict(struct file_lock *, 848fd7732e0SNeilBrown struct file_lock *)) 8491c8c601aSJeff Layton { 8507b2296afSJeff Layton spin_lock(&blocked_lock_lock); 851fd7732e0SNeilBrown __locks_insert_block(blocker, waiter, conflict); 8527b2296afSJeff Layton spin_unlock(&blocked_lock_lock); 8531da177e4SLinus Torvalds } 8541da177e4SLinus Torvalds 8551cb36012SJeff Layton /* 8561cb36012SJeff Layton * Wake up processes blocked waiting for blocker. 8571cb36012SJeff Layton * 8586109c850SJeff Layton * Must be called with the inode->flc_lock held! 8591da177e4SLinus Torvalds */ 8601da177e4SLinus Torvalds static void locks_wake_up_blocks(struct file_lock *blocker) 8611da177e4SLinus Torvalds { 8624e8c765dSJeff Layton /* 8634e8c765dSJeff Layton * Avoid taking global lock if list is empty. This is safe since new 8646109c850SJeff Layton * blocked requests are only added to the list under the flc_lock, and 865ada5c1daSNeilBrown * the flc_lock is always held here. Note that removal from the 866ada5c1daSNeilBrown * fl_blocked_requests list does not require the flc_lock, so we must 867ada5c1daSNeilBrown * recheck list_empty() after acquiring the blocked_lock_lock. 8684e8c765dSJeff Layton */ 869ada5c1daSNeilBrown if (list_empty(&blocker->fl_blocked_requests)) 8704e8c765dSJeff Layton return; 8714e8c765dSJeff Layton 8727b2296afSJeff Layton spin_lock(&blocked_lock_lock); 873ad6bbd8bSNeilBrown __locks_wake_up_blocks(blocker); 8747b2296afSJeff Layton spin_unlock(&blocked_lock_lock); 8751da177e4SLinus Torvalds } 8761da177e4SLinus Torvalds 8775263e31eSJeff Layton static void 878e084c1bdSJeff Layton locks_insert_lock_ctx(struct file_lock *fl, struct list_head *before) 8795263e31eSJeff Layton { 8805263e31eSJeff Layton list_add_tail(&fl->fl_list, before); 8815263e31eSJeff Layton locks_insert_global_locks(fl); 8825263e31eSJeff Layton } 8835263e31eSJeff Layton 8848634b51fSJeff Layton static void 885e084c1bdSJeff Layton locks_unlink_lock_ctx(struct file_lock *fl) 8861da177e4SLinus Torvalds { 88788974691SJeff Layton locks_delete_global_locks(fl); 8888634b51fSJeff Layton list_del_init(&fl->fl_list); 8891da177e4SLinus Torvalds locks_wake_up_blocks(fl); 89024cbe784SJeff Layton } 89124cbe784SJeff Layton 8925263e31eSJeff Layton static void 893e084c1bdSJeff Layton locks_delete_lock_ctx(struct file_lock *fl, struct list_head *dispose) 8945263e31eSJeff Layton { 895e084c1bdSJeff Layton locks_unlink_lock_ctx(fl); 8968634b51fSJeff Layton if (dispose) 8978634b51fSJeff Layton list_add(&fl->fl_list, dispose); 8988634b51fSJeff Layton else 8998634b51fSJeff Layton locks_free_lock(fl); 9005263e31eSJeff Layton } 9015263e31eSJeff Layton 9021da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. Common functionality 9031da177e4SLinus Torvalds * checks for shared/exclusive status of overlapping locks. 9041da177e4SLinus Torvalds */ 905c0e15908SNeilBrown static bool locks_conflict(struct file_lock *caller_fl, 906c0e15908SNeilBrown struct file_lock *sys_fl) 9071da177e4SLinus Torvalds { 9081da177e4SLinus Torvalds if (sys_fl->fl_type == F_WRLCK) 909c0e15908SNeilBrown return true; 9101da177e4SLinus Torvalds if (caller_fl->fl_type == F_WRLCK) 911c0e15908SNeilBrown return true; 912c0e15908SNeilBrown return false; 9131da177e4SLinus Torvalds } 9141da177e4SLinus Torvalds 9151da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. POSIX specific 9161da177e4SLinus Torvalds * checking before calling the locks_conflict(). 9171da177e4SLinus Torvalds */ 918c0e15908SNeilBrown static bool posix_locks_conflict(struct file_lock *caller_fl, 919c0e15908SNeilBrown struct file_lock *sys_fl) 9201da177e4SLinus Torvalds { 9211da177e4SLinus Torvalds /* POSIX locks owned by the same process do not conflict with 9221da177e4SLinus Torvalds * each other. 9231da177e4SLinus Torvalds */ 9249b8c8695SJeff Layton if (posix_same_owner(caller_fl, sys_fl)) 925c0e15908SNeilBrown return false; 9261da177e4SLinus Torvalds 9271da177e4SLinus Torvalds /* Check whether they overlap */ 9281da177e4SLinus Torvalds if (!locks_overlap(caller_fl, sys_fl)) 929c0e15908SNeilBrown return false; 9301da177e4SLinus Torvalds 931c0e15908SNeilBrown return locks_conflict(caller_fl, sys_fl); 9321da177e4SLinus Torvalds } 9331da177e4SLinus Torvalds 9341da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. FLOCK specific 9351da177e4SLinus Torvalds * checking before calling the locks_conflict(). 9361da177e4SLinus Torvalds */ 937c0e15908SNeilBrown static bool flock_locks_conflict(struct file_lock *caller_fl, 938c0e15908SNeilBrown struct file_lock *sys_fl) 9391da177e4SLinus Torvalds { 9401da177e4SLinus Torvalds /* FLOCK locks referring to the same filp do not conflict with 9411da177e4SLinus Torvalds * each other. 9421da177e4SLinus Torvalds */ 9439b8c8695SJeff Layton if (caller_fl->fl_file == sys_fl->fl_file) 944c0e15908SNeilBrown return false; 9451da177e4SLinus Torvalds if ((caller_fl->fl_type & LOCK_MAND) || (sys_fl->fl_type & LOCK_MAND)) 946c0e15908SNeilBrown return false; 9471da177e4SLinus Torvalds 948c0e15908SNeilBrown return locks_conflict(caller_fl, sys_fl); 9491da177e4SLinus Torvalds } 9501da177e4SLinus Torvalds 9516d34ac19SJ. Bruce Fields void 9529d6a8c5cSMarc Eshel posix_test_lock(struct file *filp, struct file_lock *fl) 9531da177e4SLinus Torvalds { 9541da177e4SLinus Torvalds struct file_lock *cfl; 955bd61e0a9SJeff Layton struct file_lock_context *ctx; 956c568d683SMiklos Szeredi struct inode *inode = locks_inode(filp); 9571da177e4SLinus Torvalds 958128a3785SDmitry Vyukov ctx = smp_load_acquire(&inode->i_flctx); 959bd61e0a9SJeff Layton if (!ctx || list_empty_careful(&ctx->flc_posix)) { 960bd61e0a9SJeff Layton fl->fl_type = F_UNLCK; 961bd61e0a9SJeff Layton return; 9621da177e4SLinus Torvalds } 963bd61e0a9SJeff Layton 9646109c850SJeff Layton spin_lock(&ctx->flc_lock); 965bd61e0a9SJeff Layton list_for_each_entry(cfl, &ctx->flc_posix, fl_list) { 966bd61e0a9SJeff Layton if (posix_locks_conflict(fl, cfl)) { 9673fe0fff1SKinglong Mee locks_copy_conflock(fl, cfl); 968bd61e0a9SJeff Layton goto out; 969bd61e0a9SJeff Layton } 970bd61e0a9SJeff Layton } 971129a84deSJ. Bruce Fields fl->fl_type = F_UNLCK; 972bd61e0a9SJeff Layton out: 9736109c850SJeff Layton spin_unlock(&ctx->flc_lock); 9746d34ac19SJ. Bruce Fields return; 9751da177e4SLinus Torvalds } 9761da177e4SLinus Torvalds EXPORT_SYMBOL(posix_test_lock); 9771da177e4SLinus Torvalds 978b533184fSJ. Bruce Fields /* 979b533184fSJ. Bruce Fields * Deadlock detection: 9801da177e4SLinus Torvalds * 981b533184fSJ. Bruce Fields * We attempt to detect deadlocks that are due purely to posix file 982b533184fSJ. Bruce Fields * locks. 9831da177e4SLinus Torvalds * 984b533184fSJ. Bruce Fields * We assume that a task can be waiting for at most one lock at a time. 985b533184fSJ. Bruce Fields * So for any acquired lock, the process holding that lock may be 986b533184fSJ. Bruce Fields * waiting on at most one other lock. That lock in turns may be held by 987b533184fSJ. Bruce Fields * someone waiting for at most one other lock. Given a requested lock 988b533184fSJ. Bruce Fields * caller_fl which is about to wait for a conflicting lock block_fl, we 989b533184fSJ. Bruce Fields * follow this chain of waiters to ensure we are not about to create a 990b533184fSJ. Bruce Fields * cycle. 99197855b49SJ. Bruce Fields * 992b533184fSJ. Bruce Fields * Since we do this before we ever put a process to sleep on a lock, we 993b533184fSJ. Bruce Fields * are ensured that there is never a cycle; that is what guarantees that 994b533184fSJ. Bruce Fields * the while() loop in posix_locks_deadlock() eventually completes. 995b533184fSJ. Bruce Fields * 996b533184fSJ. Bruce Fields * Note: the above assumption may not be true when handling lock 997b533184fSJ. Bruce Fields * requests from a broken NFS client. It may also fail in the presence 998b533184fSJ. Bruce Fields * of tasks (such as posix threads) sharing the same open file table. 999b533184fSJ. Bruce Fields * To handle those cases, we just bail out after a few iterations. 100057b65325SJeff Layton * 1001cff2fce5SJeff Layton * For FL_OFDLCK locks, the owner is the filp, not the files_struct. 100257b65325SJeff Layton * Because the owner is not even nominally tied to a thread of 100357b65325SJeff Layton * execution, the deadlock detection below can't reasonably work well. Just 100457b65325SJeff Layton * skip it for those. 100557b65325SJeff Layton * 1006cff2fce5SJeff Layton * In principle, we could do a more limited deadlock detection on FL_OFDLCK 100757b65325SJeff Layton * locks that just checks for the case where two tasks are attempting to 100857b65325SJeff Layton * upgrade from read to write locks on the same inode. 10091da177e4SLinus Torvalds */ 101097855b49SJ. Bruce Fields 101197855b49SJ. Bruce Fields #define MAX_DEADLK_ITERATIONS 10 101297855b49SJ. Bruce Fields 1013b533184fSJ. Bruce Fields /* Find a lock that the owner of the given block_fl is blocking on. */ 1014b533184fSJ. Bruce Fields static struct file_lock *what_owner_is_waiting_for(struct file_lock *block_fl) 1015b533184fSJ. Bruce Fields { 1016b533184fSJ. Bruce Fields struct file_lock *fl; 1017b533184fSJ. Bruce Fields 10183999e493SJeff Layton hash_for_each_possible(blocked_hash, fl, fl_link, posix_owner_key(block_fl)) { 10195946c431SNeilBrown if (posix_same_owner(fl, block_fl)) { 10205946c431SNeilBrown while (fl->fl_blocker) 10215946c431SNeilBrown fl = fl->fl_blocker; 10225946c431SNeilBrown return fl; 10235946c431SNeilBrown } 1024b533184fSJ. Bruce Fields } 1025b533184fSJ. Bruce Fields return NULL; 1026b533184fSJ. Bruce Fields } 1027b533184fSJ. Bruce Fields 10287b2296afSJeff Layton /* Must be called with the blocked_lock_lock held! */ 1029b0904e14SAdrian Bunk static int posix_locks_deadlock(struct file_lock *caller_fl, 10301da177e4SLinus Torvalds struct file_lock *block_fl) 10311da177e4SLinus Torvalds { 103297855b49SJ. Bruce Fields int i = 0; 10331da177e4SLinus Torvalds 1034663d5af7SDaniel Wagner lockdep_assert_held(&blocked_lock_lock); 1035663d5af7SDaniel Wagner 103657b65325SJeff Layton /* 103757b65325SJeff Layton * This deadlock detector can't reasonably detect deadlocks with 1038cff2fce5SJeff Layton * FL_OFDLCK locks, since they aren't owned by a process, per-se. 103957b65325SJeff Layton */ 1040cff2fce5SJeff Layton if (IS_OFDLCK(caller_fl)) 104157b65325SJeff Layton return 0; 104257b65325SJeff Layton 1043b533184fSJ. Bruce Fields while ((block_fl = what_owner_is_waiting_for(block_fl))) { 104497855b49SJ. Bruce Fields if (i++ > MAX_DEADLK_ITERATIONS) 104597855b49SJ. Bruce Fields return 0; 1046b533184fSJ. Bruce Fields if (posix_same_owner(caller_fl, block_fl)) 1047b533184fSJ. Bruce Fields return 1; 10481da177e4SLinus Torvalds } 10491da177e4SLinus Torvalds return 0; 10501da177e4SLinus Torvalds } 10511da177e4SLinus Torvalds 10521da177e4SLinus Torvalds /* Try to create a FLOCK lock on filp. We always insert new FLOCK locks 105302888f41SJ. Bruce Fields * after any leases, but before any posix locks. 1054f475ae95STrond Myklebust * 1055f475ae95STrond Myklebust * Note that if called with an FL_EXISTS argument, the caller may determine 1056f475ae95STrond Myklebust * whether or not a lock was successfully freed by testing the return 1057f475ae95STrond Myklebust * value for -ENOENT. 10581da177e4SLinus Torvalds */ 1059bcd7f78dSJeff Layton static int flock_lock_inode(struct inode *inode, struct file_lock *request) 10601da177e4SLinus Torvalds { 1061993dfa87STrond Myklebust struct file_lock *new_fl = NULL; 10625263e31eSJeff Layton struct file_lock *fl; 10635263e31eSJeff Layton struct file_lock_context *ctx; 10641da177e4SLinus Torvalds int error = 0; 10655263e31eSJeff Layton bool found = false; 1066ed9814d8SJeff Layton LIST_HEAD(dispose); 10671da177e4SLinus Torvalds 10685c1c669aSJeff Layton ctx = locks_get_lock_context(inode, request->fl_type); 10695c1c669aSJeff Layton if (!ctx) { 10705c1c669aSJeff Layton if (request->fl_type != F_UNLCK) 10715263e31eSJeff Layton return -ENOMEM; 10725c1c669aSJeff Layton return (request->fl_flags & FL_EXISTS) ? -ENOENT : 0; 10735c1c669aSJeff Layton } 10745263e31eSJeff Layton 1075b89f4321SArnd Bergmann if (!(request->fl_flags & FL_ACCESS) && (request->fl_type != F_UNLCK)) { 1076b89f4321SArnd Bergmann new_fl = locks_alloc_lock(); 1077b89f4321SArnd Bergmann if (!new_fl) 1078b89f4321SArnd Bergmann return -ENOMEM; 1079b89f4321SArnd Bergmann } 1080b89f4321SArnd Bergmann 108102e525b2SPeter Zijlstra percpu_down_read(&file_rwsem); 10826109c850SJeff Layton spin_lock(&ctx->flc_lock); 1083f07f18ddSTrond Myklebust if (request->fl_flags & FL_ACCESS) 1084f07f18ddSTrond Myklebust goto find_conflict; 108584d535adSPavel Emelyanov 10865263e31eSJeff Layton list_for_each_entry(fl, &ctx->flc_flock, fl_list) { 1087bcd7f78dSJeff Layton if (request->fl_file != fl->fl_file) 10881da177e4SLinus Torvalds continue; 1089993dfa87STrond Myklebust if (request->fl_type == fl->fl_type) 10901da177e4SLinus Torvalds goto out; 10915263e31eSJeff Layton found = true; 1092e084c1bdSJeff Layton locks_delete_lock_ctx(fl, &dispose); 10931da177e4SLinus Torvalds break; 10941da177e4SLinus Torvalds } 10951da177e4SLinus Torvalds 1096f475ae95STrond Myklebust if (request->fl_type == F_UNLCK) { 1097f475ae95STrond Myklebust if ((request->fl_flags & FL_EXISTS) && !found) 1098f475ae95STrond Myklebust error = -ENOENT; 1099993dfa87STrond Myklebust goto out; 1100f475ae95STrond Myklebust } 11011da177e4SLinus Torvalds 1102f07f18ddSTrond Myklebust find_conflict: 11035263e31eSJeff Layton list_for_each_entry(fl, &ctx->flc_flock, fl_list) { 1104993dfa87STrond Myklebust if (!flock_locks_conflict(request, fl)) 11051da177e4SLinus Torvalds continue; 11061da177e4SLinus Torvalds error = -EAGAIN; 1107bde74e4bSMiklos Szeredi if (!(request->fl_flags & FL_SLEEP)) 1108bde74e4bSMiklos Szeredi goto out; 1109bde74e4bSMiklos Szeredi error = FILE_LOCK_DEFERRED; 1110fd7732e0SNeilBrown locks_insert_block(fl, request, flock_locks_conflict); 11111da177e4SLinus Torvalds goto out; 11121da177e4SLinus Torvalds } 1113f07f18ddSTrond Myklebust if (request->fl_flags & FL_ACCESS) 1114f07f18ddSTrond Myklebust goto out; 1115993dfa87STrond Myklebust locks_copy_lock(new_fl, request); 11165946c431SNeilBrown locks_move_blocks(new_fl, request); 1117e084c1bdSJeff Layton locks_insert_lock_ctx(new_fl, &ctx->flc_flock); 1118993dfa87STrond Myklebust new_fl = NULL; 11199cedc194SKirill Korotaev error = 0; 11201da177e4SLinus Torvalds 11211da177e4SLinus Torvalds out: 11226109c850SJeff Layton spin_unlock(&ctx->flc_lock); 112302e525b2SPeter Zijlstra percpu_up_read(&file_rwsem); 1124993dfa87STrond Myklebust if (new_fl) 1125993dfa87STrond Myklebust locks_free_lock(new_fl); 1126ed9814d8SJeff Layton locks_dispose_list(&dispose); 1127c883da31SJeff Layton trace_flock_lock_inode(inode, request, error); 11281da177e4SLinus Torvalds return error; 11291da177e4SLinus Torvalds } 11301da177e4SLinus Torvalds 1131b4d629a3SJeff Layton static int posix_lock_inode(struct inode *inode, struct file_lock *request, 1132b4d629a3SJeff Layton struct file_lock *conflock) 11331da177e4SLinus Torvalds { 1134bd61e0a9SJeff Layton struct file_lock *fl, *tmp; 113539005d02SMiklos Szeredi struct file_lock *new_fl = NULL; 113639005d02SMiklos Szeredi struct file_lock *new_fl2 = NULL; 11371da177e4SLinus Torvalds struct file_lock *left = NULL; 11381da177e4SLinus Torvalds struct file_lock *right = NULL; 1139bd61e0a9SJeff Layton struct file_lock_context *ctx; 1140b9746ef8SJeff Layton int error; 1141b9746ef8SJeff Layton bool added = false; 1142ed9814d8SJeff Layton LIST_HEAD(dispose); 11431da177e4SLinus Torvalds 11445c1c669aSJeff Layton ctx = locks_get_lock_context(inode, request->fl_type); 1145bd61e0a9SJeff Layton if (!ctx) 11465c1c669aSJeff Layton return (request->fl_type == F_UNLCK) ? 0 : -ENOMEM; 1147bd61e0a9SJeff Layton 11481da177e4SLinus Torvalds /* 11491da177e4SLinus Torvalds * We may need two file_lock structures for this operation, 11501da177e4SLinus Torvalds * so we get them in advance to avoid races. 115139005d02SMiklos Szeredi * 115239005d02SMiklos Szeredi * In some cases we can be sure, that no new locks will be needed 11531da177e4SLinus Torvalds */ 115439005d02SMiklos Szeredi if (!(request->fl_flags & FL_ACCESS) && 115539005d02SMiklos Szeredi (request->fl_type != F_UNLCK || 115639005d02SMiklos Szeredi request->fl_start != 0 || request->fl_end != OFFSET_MAX)) { 11571da177e4SLinus Torvalds new_fl = locks_alloc_lock(); 11581da177e4SLinus Torvalds new_fl2 = locks_alloc_lock(); 115939005d02SMiklos Szeredi } 11601da177e4SLinus Torvalds 116102e525b2SPeter Zijlstra percpu_down_read(&file_rwsem); 11626109c850SJeff Layton spin_lock(&ctx->flc_lock); 11631cb36012SJeff Layton /* 11641cb36012SJeff Layton * New lock request. Walk all POSIX locks and look for conflicts. If 11651cb36012SJeff Layton * there are any, either return error or put the request on the 116648f74186SJeff Layton * blocker's list of waiters and the global blocked_hash. 11671cb36012SJeff Layton */ 11681da177e4SLinus Torvalds if (request->fl_type != F_UNLCK) { 1169bd61e0a9SJeff Layton list_for_each_entry(fl, &ctx->flc_posix, fl_list) { 11701da177e4SLinus Torvalds if (!posix_locks_conflict(request, fl)) 11711da177e4SLinus Torvalds continue; 11725842add2SAndy Adamson if (conflock) 11733fe0fff1SKinglong Mee locks_copy_conflock(conflock, fl); 11741da177e4SLinus Torvalds error = -EAGAIN; 11751da177e4SLinus Torvalds if (!(request->fl_flags & FL_SLEEP)) 11761da177e4SLinus Torvalds goto out; 11771c8c601aSJeff Layton /* 11781c8c601aSJeff Layton * Deadlock detection and insertion into the blocked 11791c8c601aSJeff Layton * locks list must be done while holding the same lock! 11801c8c601aSJeff Layton */ 11811da177e4SLinus Torvalds error = -EDEADLK; 11827b2296afSJeff Layton spin_lock(&blocked_lock_lock); 1183945ab8f6SJeff Layton /* 1184945ab8f6SJeff Layton * Ensure that we don't find any locks blocked on this 1185945ab8f6SJeff Layton * request during deadlock detection. 1186945ab8f6SJeff Layton */ 1187945ab8f6SJeff Layton __locks_wake_up_blocks(request); 11881c8c601aSJeff Layton if (likely(!posix_locks_deadlock(request, fl))) { 1189bde74e4bSMiklos Szeredi error = FILE_LOCK_DEFERRED; 1190fd7732e0SNeilBrown __locks_insert_block(fl, request, 1191fd7732e0SNeilBrown posix_locks_conflict); 11921c8c601aSJeff Layton } 11937b2296afSJeff Layton spin_unlock(&blocked_lock_lock); 11941da177e4SLinus Torvalds goto out; 11951da177e4SLinus Torvalds } 11961da177e4SLinus Torvalds } 11971da177e4SLinus Torvalds 11981da177e4SLinus Torvalds /* If we're just looking for a conflict, we're done. */ 11991da177e4SLinus Torvalds error = 0; 12001da177e4SLinus Torvalds if (request->fl_flags & FL_ACCESS) 12011da177e4SLinus Torvalds goto out; 12021da177e4SLinus Torvalds 1203bd61e0a9SJeff Layton /* Find the first old lock with the same owner as the new lock */ 1204bd61e0a9SJeff Layton list_for_each_entry(fl, &ctx->flc_posix, fl_list) { 1205bd61e0a9SJeff Layton if (posix_same_owner(request, fl)) 1206bd61e0a9SJeff Layton break; 12071da177e4SLinus Torvalds } 12081da177e4SLinus Torvalds 12091da177e4SLinus Torvalds /* Process locks with this owner. */ 1210bd61e0a9SJeff Layton list_for_each_entry_safe_from(fl, tmp, &ctx->flc_posix, fl_list) { 1211bd61e0a9SJeff Layton if (!posix_same_owner(request, fl)) 1212bd61e0a9SJeff Layton break; 1213bd61e0a9SJeff Layton 1214bd61e0a9SJeff Layton /* Detect adjacent or overlapping regions (if same lock type) */ 12151da177e4SLinus Torvalds if (request->fl_type == fl->fl_type) { 1216449231d6SOlaf Kirch /* In all comparisons of start vs end, use 1217449231d6SOlaf Kirch * "start - 1" rather than "end + 1". If end 1218449231d6SOlaf Kirch * is OFFSET_MAX, end + 1 will become negative. 1219449231d6SOlaf Kirch */ 12201da177e4SLinus Torvalds if (fl->fl_end < request->fl_start - 1) 1221bd61e0a9SJeff Layton continue; 12221da177e4SLinus Torvalds /* If the next lock in the list has entirely bigger 12231da177e4SLinus Torvalds * addresses than the new one, insert the lock here. 12241da177e4SLinus Torvalds */ 1225449231d6SOlaf Kirch if (fl->fl_start - 1 > request->fl_end) 12261da177e4SLinus Torvalds break; 12271da177e4SLinus Torvalds 12281da177e4SLinus Torvalds /* If we come here, the new and old lock are of the 12291da177e4SLinus Torvalds * same type and adjacent or overlapping. Make one 12301da177e4SLinus Torvalds * lock yielding from the lower start address of both 12311da177e4SLinus Torvalds * locks to the higher end address. 12321da177e4SLinus Torvalds */ 12331da177e4SLinus Torvalds if (fl->fl_start > request->fl_start) 12341da177e4SLinus Torvalds fl->fl_start = request->fl_start; 12351da177e4SLinus Torvalds else 12361da177e4SLinus Torvalds request->fl_start = fl->fl_start; 12371da177e4SLinus Torvalds if (fl->fl_end < request->fl_end) 12381da177e4SLinus Torvalds fl->fl_end = request->fl_end; 12391da177e4SLinus Torvalds else 12401da177e4SLinus Torvalds request->fl_end = fl->fl_end; 12411da177e4SLinus Torvalds if (added) { 1242e084c1bdSJeff Layton locks_delete_lock_ctx(fl, &dispose); 12431da177e4SLinus Torvalds continue; 12441da177e4SLinus Torvalds } 12451da177e4SLinus Torvalds request = fl; 1246b9746ef8SJeff Layton added = true; 1247bd61e0a9SJeff Layton } else { 12481da177e4SLinus Torvalds /* Processing for different lock types is a bit 12491da177e4SLinus Torvalds * more complex. 12501da177e4SLinus Torvalds */ 12511da177e4SLinus Torvalds if (fl->fl_end < request->fl_start) 1252bd61e0a9SJeff Layton continue; 12531da177e4SLinus Torvalds if (fl->fl_start > request->fl_end) 12541da177e4SLinus Torvalds break; 12551da177e4SLinus Torvalds if (request->fl_type == F_UNLCK) 1256b9746ef8SJeff Layton added = true; 12571da177e4SLinus Torvalds if (fl->fl_start < request->fl_start) 12581da177e4SLinus Torvalds left = fl; 12591da177e4SLinus Torvalds /* If the next lock in the list has a higher end 12601da177e4SLinus Torvalds * address than the new one, insert the new one here. 12611da177e4SLinus Torvalds */ 12621da177e4SLinus Torvalds if (fl->fl_end > request->fl_end) { 12631da177e4SLinus Torvalds right = fl; 12641da177e4SLinus Torvalds break; 12651da177e4SLinus Torvalds } 12661da177e4SLinus Torvalds if (fl->fl_start >= request->fl_start) { 12671da177e4SLinus Torvalds /* The new lock completely replaces an old 12681da177e4SLinus Torvalds * one (This may happen several times). 12691da177e4SLinus Torvalds */ 12701da177e4SLinus Torvalds if (added) { 1271e084c1bdSJeff Layton locks_delete_lock_ctx(fl, &dispose); 12721da177e4SLinus Torvalds continue; 12731da177e4SLinus Torvalds } 1274b84d49f9SJeff Layton /* 1275b84d49f9SJeff Layton * Replace the old lock with new_fl, and 1276b84d49f9SJeff Layton * remove the old one. It's safe to do the 1277b84d49f9SJeff Layton * insert here since we know that we won't be 1278b84d49f9SJeff Layton * using new_fl later, and that the lock is 1279b84d49f9SJeff Layton * just replacing an existing lock. 12801da177e4SLinus Torvalds */ 1281b84d49f9SJeff Layton error = -ENOLCK; 1282b84d49f9SJeff Layton if (!new_fl) 1283b84d49f9SJeff Layton goto out; 1284b84d49f9SJeff Layton locks_copy_lock(new_fl, request); 12855ef15968Syangerkun locks_move_blocks(new_fl, request); 1286b84d49f9SJeff Layton request = new_fl; 1287b84d49f9SJeff Layton new_fl = NULL; 1288e084c1bdSJeff Layton locks_insert_lock_ctx(request, &fl->fl_list); 1289e084c1bdSJeff Layton locks_delete_lock_ctx(fl, &dispose); 1290b9746ef8SJeff Layton added = true; 12911da177e4SLinus Torvalds } 12921da177e4SLinus Torvalds } 12931da177e4SLinus Torvalds } 12941da177e4SLinus Torvalds 12950d9a490aSMiklos Szeredi /* 12961cb36012SJeff Layton * The above code only modifies existing locks in case of merging or 12971cb36012SJeff Layton * replacing. If new lock(s) need to be inserted all modifications are 12981cb36012SJeff Layton * done below this, so it's safe yet to bail out. 12990d9a490aSMiklos Szeredi */ 13000d9a490aSMiklos Szeredi error = -ENOLCK; /* "no luck" */ 13010d9a490aSMiklos Szeredi if (right && left == right && !new_fl2) 13020d9a490aSMiklos Szeredi goto out; 13030d9a490aSMiklos Szeredi 13041da177e4SLinus Torvalds error = 0; 13051da177e4SLinus Torvalds if (!added) { 1306f475ae95STrond Myklebust if (request->fl_type == F_UNLCK) { 1307f475ae95STrond Myklebust if (request->fl_flags & FL_EXISTS) 1308f475ae95STrond Myklebust error = -ENOENT; 13091da177e4SLinus Torvalds goto out; 1310f475ae95STrond Myklebust } 13110d9a490aSMiklos Szeredi 13120d9a490aSMiklos Szeredi if (!new_fl) { 13130d9a490aSMiklos Szeredi error = -ENOLCK; 13140d9a490aSMiklos Szeredi goto out; 13150d9a490aSMiklos Szeredi } 13161da177e4SLinus Torvalds locks_copy_lock(new_fl, request); 13175946c431SNeilBrown locks_move_blocks(new_fl, request); 1318e084c1bdSJeff Layton locks_insert_lock_ctx(new_fl, &fl->fl_list); 13192e2f756fSJeff Layton fl = new_fl; 13201da177e4SLinus Torvalds new_fl = NULL; 13211da177e4SLinus Torvalds } 13221da177e4SLinus Torvalds if (right) { 13231da177e4SLinus Torvalds if (left == right) { 13241da177e4SLinus Torvalds /* The new lock breaks the old one in two pieces, 13251da177e4SLinus Torvalds * so we have to use the second new lock. 13261da177e4SLinus Torvalds */ 13271da177e4SLinus Torvalds left = new_fl2; 13281da177e4SLinus Torvalds new_fl2 = NULL; 13291da177e4SLinus Torvalds locks_copy_lock(left, right); 1330e084c1bdSJeff Layton locks_insert_lock_ctx(left, &fl->fl_list); 13311da177e4SLinus Torvalds } 13321da177e4SLinus Torvalds right->fl_start = request->fl_end + 1; 13331da177e4SLinus Torvalds locks_wake_up_blocks(right); 13341da177e4SLinus Torvalds } 13351da177e4SLinus Torvalds if (left) { 13361da177e4SLinus Torvalds left->fl_end = request->fl_start - 1; 13371da177e4SLinus Torvalds locks_wake_up_blocks(left); 13381da177e4SLinus Torvalds } 13391da177e4SLinus Torvalds out: 13406109c850SJeff Layton spin_unlock(&ctx->flc_lock); 134102e525b2SPeter Zijlstra percpu_up_read(&file_rwsem); 13421da177e4SLinus Torvalds /* 13431da177e4SLinus Torvalds * Free any unused locks. 13441da177e4SLinus Torvalds */ 13451da177e4SLinus Torvalds if (new_fl) 13461da177e4SLinus Torvalds locks_free_lock(new_fl); 13471da177e4SLinus Torvalds if (new_fl2) 13481da177e4SLinus Torvalds locks_free_lock(new_fl2); 1349ed9814d8SJeff Layton locks_dispose_list(&dispose); 13501890910fSJeff Layton trace_posix_lock_inode(inode, request, error); 13511890910fSJeff Layton 13521da177e4SLinus Torvalds return error; 13531da177e4SLinus Torvalds } 13541da177e4SLinus Torvalds 13551da177e4SLinus Torvalds /** 13561da177e4SLinus Torvalds * posix_lock_file - Apply a POSIX-style lock to a file 13571da177e4SLinus Torvalds * @filp: The file to apply the lock to 13581da177e4SLinus Torvalds * @fl: The lock to be applied 1359150b3934SMarc Eshel * @conflock: Place to return a copy of the conflicting lock, if found. 13601da177e4SLinus Torvalds * 13611da177e4SLinus Torvalds * Add a POSIX style lock to a file. 13621da177e4SLinus Torvalds * We merge adjacent & overlapping locks whenever possible. 13631da177e4SLinus Torvalds * POSIX locks are sorted by owner task, then by starting address 1364f475ae95STrond Myklebust * 1365f475ae95STrond Myklebust * Note that if called with an FL_EXISTS argument, the caller may determine 1366f475ae95STrond Myklebust * whether or not a lock was successfully freed by testing the return 1367f475ae95STrond Myklebust * value for -ENOENT. 13681da177e4SLinus Torvalds */ 1369150b3934SMarc Eshel int posix_lock_file(struct file *filp, struct file_lock *fl, 13705842add2SAndy Adamson struct file_lock *conflock) 13715842add2SAndy Adamson { 1372c568d683SMiklos Szeredi return posix_lock_inode(locks_inode(filp), fl, conflock); 13735842add2SAndy Adamson } 1374150b3934SMarc Eshel EXPORT_SYMBOL(posix_lock_file); 13751da177e4SLinus Torvalds 13761da177e4SLinus Torvalds /** 137729d01b22SJeff Layton * posix_lock_inode_wait - Apply a POSIX-style lock to a file 137829d01b22SJeff Layton * @inode: inode of file to which lock request should be applied 13791da177e4SLinus Torvalds * @fl: The lock to be applied 13801da177e4SLinus Torvalds * 1381616fb38fSBenjamin Coddington * Apply a POSIX style lock request to an inode. 13821da177e4SLinus Torvalds */ 1383616fb38fSBenjamin Coddington static int posix_lock_inode_wait(struct inode *inode, struct file_lock *fl) 13841da177e4SLinus Torvalds { 13851da177e4SLinus Torvalds int error; 13861da177e4SLinus Torvalds might_sleep (); 13871da177e4SLinus Torvalds for (;;) { 1388b4d629a3SJeff Layton error = posix_lock_inode(inode, fl, NULL); 1389bde74e4bSMiklos Szeredi if (error != FILE_LOCK_DEFERRED) 13901da177e4SLinus Torvalds break; 1391dcf23ac3SLinus Torvalds error = wait_event_interruptible(fl->fl_wait, 1392dcf23ac3SLinus Torvalds list_empty(&fl->fl_blocked_member)); 139316306a61SNeilBrown if (error) 13941da177e4SLinus Torvalds break; 13951da177e4SLinus Torvalds } 139616306a61SNeilBrown locks_delete_block(fl); 13971da177e4SLinus Torvalds return error; 13981da177e4SLinus Torvalds } 139929d01b22SJeff Layton 1400778fc546SJ. Bruce Fields static void lease_clear_pending(struct file_lock *fl, int arg) 1401778fc546SJ. Bruce Fields { 1402778fc546SJ. Bruce Fields switch (arg) { 1403778fc546SJ. Bruce Fields case F_UNLCK: 1404778fc546SJ. Bruce Fields fl->fl_flags &= ~FL_UNLOCK_PENDING; 1405df561f66SGustavo A. R. Silva fallthrough; 1406778fc546SJ. Bruce Fields case F_RDLCK: 1407778fc546SJ. Bruce Fields fl->fl_flags &= ~FL_DOWNGRADE_PENDING; 1408778fc546SJ. Bruce Fields } 1409778fc546SJ. Bruce Fields } 1410778fc546SJ. Bruce Fields 14111da177e4SLinus Torvalds /* We already had a lease on this file; just change its type */ 14127448cc37SJeff Layton int lease_modify(struct file_lock *fl, int arg, struct list_head *dispose) 14131da177e4SLinus Torvalds { 14141da177e4SLinus Torvalds int error = assign_type(fl, arg); 14151da177e4SLinus Torvalds 14161da177e4SLinus Torvalds if (error) 14171da177e4SLinus Torvalds return error; 1418778fc546SJ. Bruce Fields lease_clear_pending(fl, arg); 14191da177e4SLinus Torvalds locks_wake_up_blocks(fl); 14203b6e2723SFilipe Brandenburger if (arg == F_UNLCK) { 14213b6e2723SFilipe Brandenburger struct file *filp = fl->fl_file; 14223b6e2723SFilipe Brandenburger 14233b6e2723SFilipe Brandenburger f_delown(filp); 14243b6e2723SFilipe Brandenburger filp->f_owner.signum = 0; 142596d6d59cSJ. Bruce Fields fasync_helper(0, fl->fl_file, 0, &fl->fl_fasync); 142696d6d59cSJ. Bruce Fields if (fl->fl_fasync != NULL) { 142796d6d59cSJ. Bruce Fields printk(KERN_ERR "locks_delete_lock: fasync == %p\n", fl->fl_fasync); 142896d6d59cSJ. Bruce Fields fl->fl_fasync = NULL; 142996d6d59cSJ. Bruce Fields } 1430e084c1bdSJeff Layton locks_delete_lock_ctx(fl, dispose); 14313b6e2723SFilipe Brandenburger } 14321da177e4SLinus Torvalds return 0; 14331da177e4SLinus Torvalds } 14341da177e4SLinus Torvalds EXPORT_SYMBOL(lease_modify); 14351da177e4SLinus Torvalds 1436778fc546SJ. Bruce Fields static bool past_time(unsigned long then) 1437778fc546SJ. Bruce Fields { 1438778fc546SJ. Bruce Fields if (!then) 1439778fc546SJ. Bruce Fields /* 0 is a special value meaning "this never expires": */ 1440778fc546SJ. Bruce Fields return false; 1441778fc546SJ. Bruce Fields return time_after(jiffies, then); 1442778fc546SJ. Bruce Fields } 1443778fc546SJ. Bruce Fields 1444c45198edSJeff Layton static void time_out_leases(struct inode *inode, struct list_head *dispose) 14451da177e4SLinus Torvalds { 14468634b51fSJeff Layton struct file_lock_context *ctx = inode->i_flctx; 14478634b51fSJeff Layton struct file_lock *fl, *tmp; 14481da177e4SLinus Torvalds 14496109c850SJeff Layton lockdep_assert_held(&ctx->flc_lock); 1450f82b4b67SJeff Layton 14518634b51fSJeff Layton list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list) { 145262af4f1fSJeff Layton trace_time_out_leases(inode, fl); 1453778fc546SJ. Bruce Fields if (past_time(fl->fl_downgrade_time)) 14547448cc37SJeff Layton lease_modify(fl, F_RDLCK, dispose); 1455778fc546SJ. Bruce Fields if (past_time(fl->fl_break_time)) 14567448cc37SJeff Layton lease_modify(fl, F_UNLCK, dispose); 14571da177e4SLinus Torvalds } 14581da177e4SLinus Torvalds } 14591da177e4SLinus Torvalds 1460df4e8d2cSJ. Bruce Fields static bool leases_conflict(struct file_lock *lease, struct file_lock *breaker) 1461df4e8d2cSJ. Bruce Fields { 1462d51f527fSIra Weiny bool rc; 1463d51f527fSIra Weiny 146428df3d15SJ. Bruce Fields if (lease->fl_lmops->lm_breaker_owns_lease 146528df3d15SJ. Bruce Fields && lease->fl_lmops->lm_breaker_owns_lease(lease)) 146628df3d15SJ. Bruce Fields return false; 1467d51f527fSIra Weiny if ((breaker->fl_flags & FL_LAYOUT) != (lease->fl_flags & FL_LAYOUT)) { 1468d51f527fSIra Weiny rc = false; 1469d51f527fSIra Weiny goto trace; 1470d51f527fSIra Weiny } 1471d51f527fSIra Weiny if ((breaker->fl_flags & FL_DELEG) && (lease->fl_flags & FL_LEASE)) { 1472d51f527fSIra Weiny rc = false; 1473d51f527fSIra Weiny goto trace; 1474d51f527fSIra Weiny } 1475d51f527fSIra Weiny 1476d51f527fSIra Weiny rc = locks_conflict(breaker, lease); 1477d51f527fSIra Weiny trace: 1478d51f527fSIra Weiny trace_leases_conflict(rc, lease, breaker); 1479d51f527fSIra Weiny return rc; 1480df4e8d2cSJ. Bruce Fields } 1481df4e8d2cSJ. Bruce Fields 148203d12ddfSJeff Layton static bool 148303d12ddfSJeff Layton any_leases_conflict(struct inode *inode, struct file_lock *breaker) 148403d12ddfSJeff Layton { 14858634b51fSJeff Layton struct file_lock_context *ctx = inode->i_flctx; 148603d12ddfSJeff Layton struct file_lock *fl; 148703d12ddfSJeff Layton 14886109c850SJeff Layton lockdep_assert_held(&ctx->flc_lock); 148903d12ddfSJeff Layton 14908634b51fSJeff Layton list_for_each_entry(fl, &ctx->flc_lease, fl_list) { 149103d12ddfSJeff Layton if (leases_conflict(fl, breaker)) 149203d12ddfSJeff Layton return true; 149303d12ddfSJeff Layton } 149403d12ddfSJeff Layton return false; 149503d12ddfSJeff Layton } 149603d12ddfSJeff Layton 14971da177e4SLinus Torvalds /** 14981da177e4SLinus Torvalds * __break_lease - revoke all outstanding leases on file 14991da177e4SLinus Torvalds * @inode: the inode of the file to return 1500df4e8d2cSJ. Bruce Fields * @mode: O_RDONLY: break only write leases; O_WRONLY or O_RDWR: 1501df4e8d2cSJ. Bruce Fields * break all leases 1502df4e8d2cSJ. Bruce Fields * @type: FL_LEASE: break leases and delegations; FL_DELEG: break 1503df4e8d2cSJ. Bruce Fields * only delegations 15041da177e4SLinus Torvalds * 150587250dd2Sdavid m. richter * break_lease (inlined for speed) has checked there already is at least 150687250dd2Sdavid m. richter * some kind of lock (maybe a lease) on this file. Leases are broken on 150787250dd2Sdavid m. richter * a call to open() or truncate(). This function can sleep unless you 15081da177e4SLinus Torvalds * specified %O_NONBLOCK to your open(). 15091da177e4SLinus Torvalds */ 1510df4e8d2cSJ. Bruce Fields int __break_lease(struct inode *inode, unsigned int mode, unsigned int type) 15111da177e4SLinus Torvalds { 1512778fc546SJ. Bruce Fields int error = 0; 1513128a3785SDmitry Vyukov struct file_lock_context *ctx; 1514a901125cSYan, Zheng struct file_lock *new_fl, *fl, *tmp; 15151da177e4SLinus Torvalds unsigned long break_time; 15168737c930SAl Viro int want_write = (mode & O_ACCMODE) != O_RDONLY; 1517c45198edSJeff Layton LIST_HEAD(dispose); 15181da177e4SLinus Torvalds 15198737c930SAl Viro new_fl = lease_alloc(NULL, want_write ? F_WRLCK : F_RDLCK); 15206d4b9e38SLinus Torvalds if (IS_ERR(new_fl)) 15216d4b9e38SLinus Torvalds return PTR_ERR(new_fl); 1522df4e8d2cSJ. Bruce Fields new_fl->fl_flags = type; 15231da177e4SLinus Torvalds 15248634b51fSJeff Layton /* typically we will check that ctx is non-NULL before calling */ 1525128a3785SDmitry Vyukov ctx = smp_load_acquire(&inode->i_flctx); 15268634b51fSJeff Layton if (!ctx) { 15278634b51fSJeff Layton WARN_ON_ONCE(1); 1528cfddf9f4SWenwen Wang goto free_lock; 15298634b51fSJeff Layton } 15308634b51fSJeff Layton 153102e525b2SPeter Zijlstra percpu_down_read(&file_rwsem); 15326109c850SJeff Layton spin_lock(&ctx->flc_lock); 15331da177e4SLinus Torvalds 1534c45198edSJeff Layton time_out_leases(inode, &dispose); 15351da177e4SLinus Torvalds 153603d12ddfSJeff Layton if (!any_leases_conflict(inode, new_fl)) 1537df4e8d2cSJ. Bruce Fields goto out; 15381da177e4SLinus Torvalds 15391da177e4SLinus Torvalds break_time = 0; 15401da177e4SLinus Torvalds if (lease_break_time > 0) { 15411da177e4SLinus Torvalds break_time = jiffies + lease_break_time * HZ; 15421da177e4SLinus Torvalds if (break_time == 0) 15431da177e4SLinus Torvalds break_time++; /* so that 0 means no break time */ 15441da177e4SLinus Torvalds } 15451da177e4SLinus Torvalds 1546a901125cSYan, Zheng list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list) { 1547df4e8d2cSJ. Bruce Fields if (!leases_conflict(fl, new_fl)) 1548df4e8d2cSJ. Bruce Fields continue; 1549778fc546SJ. Bruce Fields if (want_write) { 1550778fc546SJ. Bruce Fields if (fl->fl_flags & FL_UNLOCK_PENDING) 1551778fc546SJ. Bruce Fields continue; 1552778fc546SJ. Bruce Fields fl->fl_flags |= FL_UNLOCK_PENDING; 15531da177e4SLinus Torvalds fl->fl_break_time = break_time; 1554778fc546SJ. Bruce Fields } else { 15558634b51fSJeff Layton if (lease_breaking(fl)) 1556778fc546SJ. Bruce Fields continue; 1557778fc546SJ. Bruce Fields fl->fl_flags |= FL_DOWNGRADE_PENDING; 1558778fc546SJ. Bruce Fields fl->fl_downgrade_time = break_time; 15591da177e4SLinus Torvalds } 15604d01b7f5SJeff Layton if (fl->fl_lmops->lm_break(fl)) 1561e084c1bdSJeff Layton locks_delete_lock_ctx(fl, &dispose); 15621da177e4SLinus Torvalds } 15631da177e4SLinus Torvalds 15648634b51fSJeff Layton if (list_empty(&ctx->flc_lease)) 15654d01b7f5SJeff Layton goto out; 15664d01b7f5SJeff Layton 1567843c6b2fSJeff Layton if (mode & O_NONBLOCK) { 156862af4f1fSJeff Layton trace_break_lease_noblock(inode, new_fl); 15691da177e4SLinus Torvalds error = -EWOULDBLOCK; 15701da177e4SLinus Torvalds goto out; 15711da177e4SLinus Torvalds } 15721da177e4SLinus Torvalds 15731da177e4SLinus Torvalds restart: 15748634b51fSJeff Layton fl = list_first_entry(&ctx->flc_lease, struct file_lock, fl_list); 15758634b51fSJeff Layton break_time = fl->fl_break_time; 1576f1c6bb2cSJeff Layton if (break_time != 0) 15771da177e4SLinus Torvalds break_time -= jiffies; 15781da177e4SLinus Torvalds if (break_time == 0) 15791da177e4SLinus Torvalds break_time++; 1580fd7732e0SNeilBrown locks_insert_block(fl, new_fl, leases_conflict); 158162af4f1fSJeff Layton trace_break_lease_block(inode, new_fl); 15826109c850SJeff Layton spin_unlock(&ctx->flc_lock); 158302e525b2SPeter Zijlstra percpu_up_read(&file_rwsem); 1584aba37660SPeter Zijlstra 1585c45198edSJeff Layton locks_dispose_list(&dispose); 15864321e01eSMatthew Wilcox error = wait_event_interruptible_timeout(new_fl->fl_wait, 1587dcf23ac3SLinus Torvalds list_empty(&new_fl->fl_blocked_member), 1588dcf23ac3SLinus Torvalds break_time); 1589aba37660SPeter Zijlstra 159002e525b2SPeter Zijlstra percpu_down_read(&file_rwsem); 15916109c850SJeff Layton spin_lock(&ctx->flc_lock); 159262af4f1fSJeff Layton trace_break_lease_unblock(inode, new_fl); 15931c8c601aSJeff Layton locks_delete_block(new_fl); 15941da177e4SLinus Torvalds if (error >= 0) { 1595778fc546SJ. Bruce Fields /* 1596778fc546SJ. Bruce Fields * Wait for the next conflicting lease that has not been 1597778fc546SJ. Bruce Fields * broken yet 1598778fc546SJ. Bruce Fields */ 159903d12ddfSJeff Layton if (error == 0) 160003d12ddfSJeff Layton time_out_leases(inode, &dispose); 160103d12ddfSJeff Layton if (any_leases_conflict(inode, new_fl)) 16021da177e4SLinus Torvalds goto restart; 16031da177e4SLinus Torvalds error = 0; 16041da177e4SLinus Torvalds } 16051da177e4SLinus Torvalds out: 16066109c850SJeff Layton spin_unlock(&ctx->flc_lock); 160702e525b2SPeter Zijlstra percpu_up_read(&file_rwsem); 1608c45198edSJeff Layton locks_dispose_list(&dispose); 1609cfddf9f4SWenwen Wang free_lock: 16101da177e4SLinus Torvalds locks_free_lock(new_fl); 16111da177e4SLinus Torvalds return error; 16121da177e4SLinus Torvalds } 16131da177e4SLinus Torvalds EXPORT_SYMBOL(__break_lease); 16141da177e4SLinus Torvalds 16151da177e4SLinus Torvalds /** 161676c47948SAmir Goldstein * lease_get_mtime - update modified time of an inode with exclusive lease 16171da177e4SLinus Torvalds * @inode: the inode 161876c47948SAmir Goldstein * @time: pointer to a timespec which contains the last modified time 16191da177e4SLinus Torvalds * 16201da177e4SLinus Torvalds * This is to force NFS clients to flush their caches for files with 16211da177e4SLinus Torvalds * exclusive leases. The justification is that if someone has an 1622a6b91919SRandy Dunlap * exclusive lease, then they could be modifying it. 16231da177e4SLinus Torvalds */ 162495582b00SDeepa Dinamani void lease_get_mtime(struct inode *inode, struct timespec64 *time) 16251da177e4SLinus Torvalds { 1626bfe86024SJeff Layton bool has_lease = false; 1627128a3785SDmitry Vyukov struct file_lock_context *ctx; 16288634b51fSJeff Layton struct file_lock *fl; 1629bfe86024SJeff Layton 1630128a3785SDmitry Vyukov ctx = smp_load_acquire(&inode->i_flctx); 16318634b51fSJeff Layton if (ctx && !list_empty_careful(&ctx->flc_lease)) { 16326109c850SJeff Layton spin_lock(&ctx->flc_lock); 16338ace5dfbSGeliang Tang fl = list_first_entry_or_null(&ctx->flc_lease, 16348634b51fSJeff Layton struct file_lock, fl_list); 16358ace5dfbSGeliang Tang if (fl && (fl->fl_type == F_WRLCK)) 1636bfe86024SJeff Layton has_lease = true; 16376109c850SJeff Layton spin_unlock(&ctx->flc_lock); 1638bfe86024SJeff Layton } 1639bfe86024SJeff Layton 1640bfe86024SJeff Layton if (has_lease) 1641c2050a45SDeepa Dinamani *time = current_time(inode); 16421da177e4SLinus Torvalds } 16431da177e4SLinus Torvalds EXPORT_SYMBOL(lease_get_mtime); 16441da177e4SLinus Torvalds 16451da177e4SLinus Torvalds /** 16461da177e4SLinus Torvalds * fcntl_getlease - Enquire what lease is currently active 16471da177e4SLinus Torvalds * @filp: the file 16481da177e4SLinus Torvalds * 16491da177e4SLinus Torvalds * The value returned by this function will be one of 16501da177e4SLinus Torvalds * (if no lease break is pending): 16511da177e4SLinus Torvalds * 16521da177e4SLinus Torvalds * %F_RDLCK to indicate a shared lease is held. 16531da177e4SLinus Torvalds * 16541da177e4SLinus Torvalds * %F_WRLCK to indicate an exclusive lease is held. 16551da177e4SLinus Torvalds * 16561da177e4SLinus Torvalds * %F_UNLCK to indicate no lease is held. 16571da177e4SLinus Torvalds * 16581da177e4SLinus Torvalds * (if a lease break is pending): 16591da177e4SLinus Torvalds * 16601da177e4SLinus Torvalds * %F_RDLCK to indicate an exclusive lease needs to be 16611da177e4SLinus Torvalds * changed to a shared lease (or removed). 16621da177e4SLinus Torvalds * 16631da177e4SLinus Torvalds * %F_UNLCK to indicate the lease needs to be removed. 16641da177e4SLinus Torvalds * 16651da177e4SLinus Torvalds * XXX: sfr & willy disagree over whether F_INPROGRESS 16661da177e4SLinus Torvalds * should be returned to userspace. 16671da177e4SLinus Torvalds */ 16681da177e4SLinus Torvalds int fcntl_getlease(struct file *filp) 16691da177e4SLinus Torvalds { 16701da177e4SLinus Torvalds struct file_lock *fl; 1671c568d683SMiklos Szeredi struct inode *inode = locks_inode(filp); 1672128a3785SDmitry Vyukov struct file_lock_context *ctx; 16731da177e4SLinus Torvalds int type = F_UNLCK; 1674c45198edSJeff Layton LIST_HEAD(dispose); 16751da177e4SLinus Torvalds 1676128a3785SDmitry Vyukov ctx = smp_load_acquire(&inode->i_flctx); 16778634b51fSJeff Layton if (ctx && !list_empty_careful(&ctx->flc_lease)) { 167802e525b2SPeter Zijlstra percpu_down_read(&file_rwsem); 16796109c850SJeff Layton spin_lock(&ctx->flc_lock); 1680c568d683SMiklos Szeredi time_out_leases(inode, &dispose); 16818634b51fSJeff Layton list_for_each_entry(fl, &ctx->flc_lease, fl_list) { 16828634b51fSJeff Layton if (fl->fl_file != filp) 16838634b51fSJeff Layton continue; 1684778fc546SJ. Bruce Fields type = target_leasetype(fl); 16851da177e4SLinus Torvalds break; 16861da177e4SLinus Torvalds } 16876109c850SJeff Layton spin_unlock(&ctx->flc_lock); 168802e525b2SPeter Zijlstra percpu_up_read(&file_rwsem); 16895f43086bSPeter Zijlstra 1690c45198edSJeff Layton locks_dispose_list(&dispose); 16918634b51fSJeff Layton } 16921da177e4SLinus Torvalds return type; 16931da177e4SLinus Torvalds } 16941da177e4SLinus Torvalds 169524cbe784SJeff Layton /** 1696387e3746SAmir Goldstein * check_conflicting_open - see if the given file points to an inode that has 169724cbe784SJeff Layton * an existing open that would conflict with the 169824cbe784SJeff Layton * desired lease. 1699387e3746SAmir Goldstein * @filp: file to check 170024cbe784SJeff Layton * @arg: type of lease that we're trying to acquire 17017fadc59cSRandy Dunlap * @flags: current lock flags 170224cbe784SJeff Layton * 170324cbe784SJeff Layton * Check to see if there's an existing open fd on this file that would 170424cbe784SJeff Layton * conflict with the lease we're trying to set. 170524cbe784SJeff Layton */ 170624cbe784SJeff Layton static int 1707387e3746SAmir Goldstein check_conflicting_open(struct file *filp, const long arg, int flags) 170824cbe784SJeff Layton { 1709387e3746SAmir Goldstein struct inode *inode = locks_inode(filp); 1710387e3746SAmir Goldstein int self_wcount = 0, self_rcount = 0; 171124cbe784SJeff Layton 171211afe9f7SChristoph Hellwig if (flags & FL_LAYOUT) 171311afe9f7SChristoph Hellwig return 0; 1714aba2072fSJ. Bruce Fields if (flags & FL_DELEG) 1715aba2072fSJ. Bruce Fields /* We leave these checks to the caller */ 1716aba2072fSJ. Bruce Fields return 0; 171711afe9f7SChristoph Hellwig 1718387e3746SAmir Goldstein if (arg == F_RDLCK) 1719387e3746SAmir Goldstein return inode_is_open_for_write(inode) ? -EAGAIN : 0; 1720387e3746SAmir Goldstein else if (arg != F_WRLCK) 1721387e3746SAmir Goldstein return 0; 1722387e3746SAmir Goldstein 1723387e3746SAmir Goldstein /* 1724387e3746SAmir Goldstein * Make sure that only read/write count is from lease requestor. 1725387e3746SAmir Goldstein * Note that this will result in denying write leases when i_writecount 1726387e3746SAmir Goldstein * is negative, which is what we want. (We shouldn't grant write leases 1727387e3746SAmir Goldstein * on files open for execution.) 1728387e3746SAmir Goldstein */ 1729387e3746SAmir Goldstein if (filp->f_mode & FMODE_WRITE) 1730387e3746SAmir Goldstein self_wcount = 1; 1731387e3746SAmir Goldstein else if (filp->f_mode & FMODE_READ) 1732387e3746SAmir Goldstein self_rcount = 1; 1733387e3746SAmir Goldstein 1734387e3746SAmir Goldstein if (atomic_read(&inode->i_writecount) != self_wcount || 1735387e3746SAmir Goldstein atomic_read(&inode->i_readcount) != self_rcount) 173624cbe784SJeff Layton return -EAGAIN; 173724cbe784SJeff Layton 1738387e3746SAmir Goldstein return 0; 173924cbe784SJeff Layton } 174024cbe784SJeff Layton 1741e6f5c789SJeff Layton static int 1742e6f5c789SJeff Layton generic_add_lease(struct file *filp, long arg, struct file_lock **flp, void **priv) 17431da177e4SLinus Torvalds { 17448634b51fSJeff Layton struct file_lock *fl, *my_fl = NULL, *lease; 1745387e3746SAmir Goldstein struct inode *inode = locks_inode(filp); 17468634b51fSJeff Layton struct file_lock_context *ctx; 1747df4e8d2cSJ. Bruce Fields bool is_deleg = (*flp)->fl_flags & FL_DELEG; 1748c1f24ef4SJ. Bruce Fields int error; 1749c45198edSJeff Layton LIST_HEAD(dispose); 17501da177e4SLinus Torvalds 1751096657b6SJ. Bruce Fields lease = *flp; 175262af4f1fSJeff Layton trace_generic_add_lease(inode, lease); 175362af4f1fSJeff Layton 17545c1c669aSJeff Layton /* Note that arg is never F_UNLCK here */ 17555c1c669aSJeff Layton ctx = locks_get_lock_context(inode, arg); 17568634b51fSJeff Layton if (!ctx) 17578634b51fSJeff Layton return -ENOMEM; 17588634b51fSJeff Layton 1759df4e8d2cSJ. Bruce Fields /* 1760df4e8d2cSJ. Bruce Fields * In the delegation case we need mutual exclusion with 1761df4e8d2cSJ. Bruce Fields * a number of operations that take the i_mutex. We trylock 1762df4e8d2cSJ. Bruce Fields * because delegations are an optional optimization, and if 1763df4e8d2cSJ. Bruce Fields * there's some chance of a conflict--we'd rather not 1764df4e8d2cSJ. Bruce Fields * bother, maybe that's a sign this just isn't a good file to 1765df4e8d2cSJ. Bruce Fields * hand out a delegation on. 1766df4e8d2cSJ. Bruce Fields */ 17675955102cSAl Viro if (is_deleg && !inode_trylock(inode)) 1768df4e8d2cSJ. Bruce Fields return -EAGAIN; 1769df4e8d2cSJ. Bruce Fields 1770df4e8d2cSJ. Bruce Fields if (is_deleg && arg == F_WRLCK) { 1771df4e8d2cSJ. Bruce Fields /* Write delegations are not currently supported: */ 17725955102cSAl Viro inode_unlock(inode); 1773df4e8d2cSJ. Bruce Fields WARN_ON_ONCE(1); 1774df4e8d2cSJ. Bruce Fields return -EINVAL; 1775df4e8d2cSJ. Bruce Fields } 1776096657b6SJ. Bruce Fields 177702e525b2SPeter Zijlstra percpu_down_read(&file_rwsem); 17786109c850SJeff Layton spin_lock(&ctx->flc_lock); 1779c45198edSJeff Layton time_out_leases(inode, &dispose); 1780387e3746SAmir Goldstein error = check_conflicting_open(filp, arg, lease->fl_flags); 178124cbe784SJeff Layton if (error) 17821da177e4SLinus Torvalds goto out; 178385c59580SPavel Emelyanov 17841da177e4SLinus Torvalds /* 17851da177e4SLinus Torvalds * At this point, we know that if there is an exclusive 17861da177e4SLinus Torvalds * lease on this file, then we hold it on this filp 17871da177e4SLinus Torvalds * (otherwise our open of this file would have blocked). 17881da177e4SLinus Torvalds * And if we are trying to acquire an exclusive lease, 17891da177e4SLinus Torvalds * then the file is not open by anyone (including us) 17901da177e4SLinus Torvalds * except for this filp. 17911da177e4SLinus Torvalds */ 1792c1f24ef4SJ. Bruce Fields error = -EAGAIN; 17938634b51fSJeff Layton list_for_each_entry(fl, &ctx->flc_lease, fl_list) { 17942ab99ee1SChristoph Hellwig if (fl->fl_file == filp && 17952ab99ee1SChristoph Hellwig fl->fl_owner == lease->fl_owner) { 17968634b51fSJeff Layton my_fl = fl; 1797c1f24ef4SJ. Bruce Fields continue; 17981da177e4SLinus Torvalds } 17998634b51fSJeff Layton 1800c1f24ef4SJ. Bruce Fields /* 1801c1f24ef4SJ. Bruce Fields * No exclusive leases if someone else has a lease on 1802c1f24ef4SJ. Bruce Fields * this file: 1803c1f24ef4SJ. Bruce Fields */ 1804c1f24ef4SJ. Bruce Fields if (arg == F_WRLCK) 18051da177e4SLinus Torvalds goto out; 1806c1f24ef4SJ. Bruce Fields /* 1807c1f24ef4SJ. Bruce Fields * Modifying our existing lease is OK, but no getting a 1808c1f24ef4SJ. Bruce Fields * new lease if someone else is opening for write: 1809c1f24ef4SJ. Bruce Fields */ 1810c1f24ef4SJ. Bruce Fields if (fl->fl_flags & FL_UNLOCK_PENDING) 1811c1f24ef4SJ. Bruce Fields goto out; 1812c1f24ef4SJ. Bruce Fields } 18131da177e4SLinus Torvalds 18148634b51fSJeff Layton if (my_fl != NULL) { 18150164bf02SJeff Layton lease = my_fl; 18160164bf02SJeff Layton error = lease->fl_lmops->lm_change(lease, arg, &dispose); 18171c7dd2ffSJeff Layton if (error) 18181da177e4SLinus Torvalds goto out; 18191c7dd2ffSJeff Layton goto out_setup; 18201da177e4SLinus Torvalds } 18211da177e4SLinus Torvalds 18221da177e4SLinus Torvalds error = -EINVAL; 18231da177e4SLinus Torvalds if (!leases_enable) 18241da177e4SLinus Torvalds goto out; 18251da177e4SLinus Torvalds 1826e084c1bdSJeff Layton locks_insert_lock_ctx(lease, &ctx->flc_lease); 182724cbe784SJeff Layton /* 182824cbe784SJeff Layton * The check in break_lease() is lockless. It's possible for another 182924cbe784SJeff Layton * open to race in after we did the earlier check for a conflicting 183024cbe784SJeff Layton * open but before the lease was inserted. Check again for a 183124cbe784SJeff Layton * conflicting open and cancel the lease if there is one. 183224cbe784SJeff Layton * 183324cbe784SJeff Layton * We also add a barrier here to ensure that the insertion of the lock 183424cbe784SJeff Layton * precedes these checks. 183524cbe784SJeff Layton */ 183624cbe784SJeff Layton smp_mb(); 1837387e3746SAmir Goldstein error = check_conflicting_open(filp, arg, lease->fl_flags); 18388634b51fSJeff Layton if (error) { 1839e084c1bdSJeff Layton locks_unlink_lock_ctx(lease); 18408634b51fSJeff Layton goto out; 18418634b51fSJeff Layton } 18421c7dd2ffSJeff Layton 18431c7dd2ffSJeff Layton out_setup: 18441c7dd2ffSJeff Layton if (lease->fl_lmops->lm_setup) 18451c7dd2ffSJeff Layton lease->fl_lmops->lm_setup(lease, priv); 18461da177e4SLinus Torvalds out: 18476109c850SJeff Layton spin_unlock(&ctx->flc_lock); 184802e525b2SPeter Zijlstra percpu_up_read(&file_rwsem); 1849c45198edSJeff Layton locks_dispose_list(&dispose); 1850df4e8d2cSJ. Bruce Fields if (is_deleg) 18515955102cSAl Viro inode_unlock(inode); 18528634b51fSJeff Layton if (!error && !my_fl) 18531c7dd2ffSJeff Layton *flp = NULL; 18541da177e4SLinus Torvalds return error; 18551da177e4SLinus Torvalds } 18568335ebd9SJ. Bruce Fields 18572ab99ee1SChristoph Hellwig static int generic_delete_lease(struct file *filp, void *owner) 18588335ebd9SJ. Bruce Fields { 18590efaa7e8SJeff Layton int error = -EAGAIN; 18608634b51fSJeff Layton struct file_lock *fl, *victim = NULL; 1861c568d683SMiklos Szeredi struct inode *inode = locks_inode(filp); 1862128a3785SDmitry Vyukov struct file_lock_context *ctx; 1863c45198edSJeff Layton LIST_HEAD(dispose); 18648335ebd9SJ. Bruce Fields 1865128a3785SDmitry Vyukov ctx = smp_load_acquire(&inode->i_flctx); 18668634b51fSJeff Layton if (!ctx) { 18678634b51fSJeff Layton trace_generic_delete_lease(inode, NULL); 18688634b51fSJeff Layton return error; 18698634b51fSJeff Layton } 18708634b51fSJeff Layton 187102e525b2SPeter Zijlstra percpu_down_read(&file_rwsem); 18726109c850SJeff Layton spin_lock(&ctx->flc_lock); 18738634b51fSJeff Layton list_for_each_entry(fl, &ctx->flc_lease, fl_list) { 18742ab99ee1SChristoph Hellwig if (fl->fl_file == filp && 18752ab99ee1SChristoph Hellwig fl->fl_owner == owner) { 18768634b51fSJeff Layton victim = fl; 18770efaa7e8SJeff Layton break; 18788335ebd9SJ. Bruce Fields } 18798634b51fSJeff Layton } 1880a9b1b455SJeff Layton trace_generic_delete_lease(inode, victim); 18818634b51fSJeff Layton if (victim) 18827448cc37SJeff Layton error = fl->fl_lmops->lm_change(victim, F_UNLCK, &dispose); 18836109c850SJeff Layton spin_unlock(&ctx->flc_lock); 188402e525b2SPeter Zijlstra percpu_up_read(&file_rwsem); 1885c45198edSJeff Layton locks_dispose_list(&dispose); 18860efaa7e8SJeff Layton return error; 18878335ebd9SJ. Bruce Fields } 18888335ebd9SJ. Bruce Fields 18891da177e4SLinus Torvalds /** 18901da177e4SLinus Torvalds * generic_setlease - sets a lease on an open file 18911da177e4SLinus Torvalds * @filp: file pointer 18921da177e4SLinus Torvalds * @arg: type of lease to obtain 18931da177e4SLinus Torvalds * @flp: input - file_lock to use, output - file_lock inserted 18941c7dd2ffSJeff Layton * @priv: private data for lm_setup (may be NULL if lm_setup 18951c7dd2ffSJeff Layton * doesn't require it) 18961da177e4SLinus Torvalds * 18971da177e4SLinus Torvalds * The (input) flp->fl_lmops->lm_break function is required 18981da177e4SLinus Torvalds * by break_lease(). 18991da177e4SLinus Torvalds */ 1900e6f5c789SJeff Layton int generic_setlease(struct file *filp, long arg, struct file_lock **flp, 1901e6f5c789SJeff Layton void **priv) 19021da177e4SLinus Torvalds { 1903c568d683SMiklos Szeredi struct inode *inode = locks_inode(filp); 19048335ebd9SJ. Bruce Fields int error; 19051da177e4SLinus Torvalds 19068e96e3b7SEric W. Biederman if ((!uid_eq(current_fsuid(), inode->i_uid)) && !capable(CAP_LEASE)) 19078335ebd9SJ. Bruce Fields return -EACCES; 19081da177e4SLinus Torvalds if (!S_ISREG(inode->i_mode)) 19098335ebd9SJ. Bruce Fields return -EINVAL; 19101da177e4SLinus Torvalds error = security_file_lock(filp, arg); 19111da177e4SLinus Torvalds if (error) 19128335ebd9SJ. Bruce Fields return error; 19131da177e4SLinus Torvalds 19148335ebd9SJ. Bruce Fields switch (arg) { 19158335ebd9SJ. Bruce Fields case F_UNLCK: 19162ab99ee1SChristoph Hellwig return generic_delete_lease(filp, *priv); 19178335ebd9SJ. Bruce Fields case F_RDLCK: 19188335ebd9SJ. Bruce Fields case F_WRLCK: 19190efaa7e8SJeff Layton if (!(*flp)->fl_lmops->lm_break) { 19200efaa7e8SJeff Layton WARN_ON_ONCE(1); 19210efaa7e8SJeff Layton return -ENOLCK; 19220efaa7e8SJeff Layton } 192311afe9f7SChristoph Hellwig 1924e6f5c789SJeff Layton return generic_add_lease(filp, arg, flp, priv); 19258335ebd9SJ. Bruce Fields default: 19268d657eb3SDave Jones return -EINVAL; 19271da177e4SLinus Torvalds } 19281da177e4SLinus Torvalds } 19290af1a450SChristoph Hellwig EXPORT_SYMBOL(generic_setlease); 19301da177e4SLinus Torvalds 193118f6622eSJeff Layton #if IS_ENABLED(CONFIG_SRCU) 193218f6622eSJeff Layton /* 193318f6622eSJeff Layton * Kernel subsystems can register to be notified on any attempt to set 193418f6622eSJeff Layton * a new lease with the lease_notifier_chain. This is used by (e.g.) nfsd 193518f6622eSJeff Layton * to close files that it may have cached when there is an attempt to set a 193618f6622eSJeff Layton * conflicting lease. 193718f6622eSJeff Layton */ 193818f6622eSJeff Layton static struct srcu_notifier_head lease_notifier_chain; 193918f6622eSJeff Layton 194018f6622eSJeff Layton static inline void 194118f6622eSJeff Layton lease_notifier_chain_init(void) 194218f6622eSJeff Layton { 194318f6622eSJeff Layton srcu_init_notifier_head(&lease_notifier_chain); 194418f6622eSJeff Layton } 194518f6622eSJeff Layton 194618f6622eSJeff Layton static inline void 194718f6622eSJeff Layton setlease_notifier(long arg, struct file_lock *lease) 194818f6622eSJeff Layton { 194918f6622eSJeff Layton if (arg != F_UNLCK) 195018f6622eSJeff Layton srcu_notifier_call_chain(&lease_notifier_chain, arg, lease); 195118f6622eSJeff Layton } 195218f6622eSJeff Layton 195318f6622eSJeff Layton int lease_register_notifier(struct notifier_block *nb) 195418f6622eSJeff Layton { 195518f6622eSJeff Layton return srcu_notifier_chain_register(&lease_notifier_chain, nb); 195618f6622eSJeff Layton } 195718f6622eSJeff Layton EXPORT_SYMBOL_GPL(lease_register_notifier); 195818f6622eSJeff Layton 195918f6622eSJeff Layton void lease_unregister_notifier(struct notifier_block *nb) 196018f6622eSJeff Layton { 196118f6622eSJeff Layton srcu_notifier_chain_unregister(&lease_notifier_chain, nb); 196218f6622eSJeff Layton } 196318f6622eSJeff Layton EXPORT_SYMBOL_GPL(lease_unregister_notifier); 196418f6622eSJeff Layton 196518f6622eSJeff Layton #else /* !IS_ENABLED(CONFIG_SRCU) */ 196618f6622eSJeff Layton static inline void 196718f6622eSJeff Layton lease_notifier_chain_init(void) 196818f6622eSJeff Layton { 196918f6622eSJeff Layton } 197018f6622eSJeff Layton 197118f6622eSJeff Layton static inline void 197218f6622eSJeff Layton setlease_notifier(long arg, struct file_lock *lease) 197318f6622eSJeff Layton { 197418f6622eSJeff Layton } 197518f6622eSJeff Layton 197618f6622eSJeff Layton int lease_register_notifier(struct notifier_block *nb) 197718f6622eSJeff Layton { 197818f6622eSJeff Layton return 0; 197918f6622eSJeff Layton } 198018f6622eSJeff Layton EXPORT_SYMBOL_GPL(lease_register_notifier); 198118f6622eSJeff Layton 198218f6622eSJeff Layton void lease_unregister_notifier(struct notifier_block *nb) 198318f6622eSJeff Layton { 198418f6622eSJeff Layton } 198518f6622eSJeff Layton EXPORT_SYMBOL_GPL(lease_unregister_notifier); 198618f6622eSJeff Layton 198718f6622eSJeff Layton #endif /* IS_ENABLED(CONFIG_SRCU) */ 198818f6622eSJeff Layton 19891da177e4SLinus Torvalds /** 1990a9933ceaSJ. Bruce Fields * vfs_setlease - sets a lease on an open file 19911da177e4SLinus Torvalds * @filp: file pointer 19921da177e4SLinus Torvalds * @arg: type of lease to obtain 1993e51673aaSJeff Layton * @lease: file_lock to use when adding a lease 19941c7dd2ffSJeff Layton * @priv: private info for lm_setup when adding a lease (may be 19951c7dd2ffSJeff Layton * NULL if lm_setup doesn't require it) 19961da177e4SLinus Torvalds * 1997e51673aaSJeff Layton * Call this to establish a lease on the file. The "lease" argument is not 1998e51673aaSJeff Layton * used for F_UNLCK requests and may be NULL. For commands that set or alter 199980b79dd0SMauro Carvalho Chehab * an existing lease, the ``(*lease)->fl_lmops->lm_break`` operation must be 200080b79dd0SMauro Carvalho Chehab * set; if not, this function will return -ENOLCK (and generate a scary-looking 2001e51673aaSJeff Layton * stack trace). 20021c7dd2ffSJeff Layton * 20031c7dd2ffSJeff Layton * The "priv" pointer is passed directly to the lm_setup function as-is. It 20041c7dd2ffSJeff Layton * may be NULL if the lm_setup operation doesn't require it. 20051da177e4SLinus Torvalds */ 2006e6f5c789SJeff Layton int 2007e6f5c789SJeff Layton vfs_setlease(struct file *filp, long arg, struct file_lock **lease, void **priv) 20081da177e4SLinus Torvalds { 200918f6622eSJeff Layton if (lease) 201018f6622eSJeff Layton setlease_notifier(arg, *lease); 2011de2a4a50SMiklos Szeredi if (filp->f_op->setlease) 2012f82b4b67SJeff Layton return filp->f_op->setlease(filp, arg, lease, priv); 20131c7dd2ffSJeff Layton else 2014f82b4b67SJeff Layton return generic_setlease(filp, arg, lease, priv); 20151da177e4SLinus Torvalds } 2016a9933ceaSJ. Bruce Fields EXPORT_SYMBOL_GPL(vfs_setlease); 20171da177e4SLinus Torvalds 20180ceaf6c7SJ. Bruce Fields static int do_fcntl_add_lease(unsigned int fd, struct file *filp, long arg) 20191da177e4SLinus Torvalds { 20201c7dd2ffSJeff Layton struct file_lock *fl; 2021f7347ce4SLinus Torvalds struct fasync_struct *new; 20221da177e4SLinus Torvalds int error; 20231da177e4SLinus Torvalds 2024c5b1f0d9SArnd Bergmann fl = lease_alloc(filp, arg); 2025c5b1f0d9SArnd Bergmann if (IS_ERR(fl)) 2026c5b1f0d9SArnd Bergmann return PTR_ERR(fl); 20271da177e4SLinus Torvalds 2028f7347ce4SLinus Torvalds new = fasync_alloc(); 2029f7347ce4SLinus Torvalds if (!new) { 2030f7347ce4SLinus Torvalds locks_free_lock(fl); 2031f7347ce4SLinus Torvalds return -ENOMEM; 2032f7347ce4SLinus Torvalds } 20331c7dd2ffSJeff Layton new->fa_fd = fd; 20341da177e4SLinus Torvalds 20351c7dd2ffSJeff Layton error = vfs_setlease(filp, arg, &fl, (void **)&new); 20362dfb928fSJeff Layton if (fl) 20372dfb928fSJeff Layton locks_free_lock(fl); 2038f7347ce4SLinus Torvalds if (new) 2039f7347ce4SLinus Torvalds fasync_free(new); 20401da177e4SLinus Torvalds return error; 20411da177e4SLinus Torvalds } 20421da177e4SLinus Torvalds 20431da177e4SLinus Torvalds /** 20440ceaf6c7SJ. Bruce Fields * fcntl_setlease - sets a lease on an open file 20450ceaf6c7SJ. Bruce Fields * @fd: open file descriptor 20460ceaf6c7SJ. Bruce Fields * @filp: file pointer 20470ceaf6c7SJ. Bruce Fields * @arg: type of lease to obtain 20480ceaf6c7SJ. Bruce Fields * 20490ceaf6c7SJ. Bruce Fields * Call this fcntl to establish a lease on the file. 20500ceaf6c7SJ. Bruce Fields * Note that you also need to call %F_SETSIG to 20510ceaf6c7SJ. Bruce Fields * receive a signal when the lease is broken. 20520ceaf6c7SJ. Bruce Fields */ 20530ceaf6c7SJ. Bruce Fields int fcntl_setlease(unsigned int fd, struct file *filp, long arg) 20540ceaf6c7SJ. Bruce Fields { 20550ceaf6c7SJ. Bruce Fields if (arg == F_UNLCK) 20562ab99ee1SChristoph Hellwig return vfs_setlease(filp, F_UNLCK, NULL, (void **)&filp); 20570ceaf6c7SJ. Bruce Fields return do_fcntl_add_lease(fd, filp, arg); 20580ceaf6c7SJ. Bruce Fields } 20590ceaf6c7SJ. Bruce Fields 20600ceaf6c7SJ. Bruce Fields /** 206129d01b22SJeff Layton * flock_lock_inode_wait - Apply a FLOCK-style lock to a file 206229d01b22SJeff Layton * @inode: inode of the file to apply to 20631da177e4SLinus Torvalds * @fl: The lock to be applied 20641da177e4SLinus Torvalds * 206529d01b22SJeff Layton * Apply a FLOCK style lock request to an inode. 20661da177e4SLinus Torvalds */ 2067616fb38fSBenjamin Coddington static int flock_lock_inode_wait(struct inode *inode, struct file_lock *fl) 20681da177e4SLinus Torvalds { 20691da177e4SLinus Torvalds int error; 20701da177e4SLinus Torvalds might_sleep(); 20711da177e4SLinus Torvalds for (;;) { 207229d01b22SJeff Layton error = flock_lock_inode(inode, fl); 2073bde74e4bSMiklos Szeredi if (error != FILE_LOCK_DEFERRED) 20741da177e4SLinus Torvalds break; 2075dcf23ac3SLinus Torvalds error = wait_event_interruptible(fl->fl_wait, 2076dcf23ac3SLinus Torvalds list_empty(&fl->fl_blocked_member)); 207716306a61SNeilBrown if (error) 20781da177e4SLinus Torvalds break; 20791da177e4SLinus Torvalds } 208016306a61SNeilBrown locks_delete_block(fl); 20811da177e4SLinus Torvalds return error; 20821da177e4SLinus Torvalds } 20831da177e4SLinus Torvalds 208429d01b22SJeff Layton /** 2085e55c34a6SBenjamin Coddington * locks_lock_inode_wait - Apply a lock to an inode 2086e55c34a6SBenjamin Coddington * @inode: inode of the file to apply to 2087e55c34a6SBenjamin Coddington * @fl: The lock to be applied 2088e55c34a6SBenjamin Coddington * 2089e55c34a6SBenjamin Coddington * Apply a POSIX or FLOCK style lock request to an inode. 2090e55c34a6SBenjamin Coddington */ 2091e55c34a6SBenjamin Coddington int locks_lock_inode_wait(struct inode *inode, struct file_lock *fl) 2092e55c34a6SBenjamin Coddington { 2093e55c34a6SBenjamin Coddington int res = 0; 2094e55c34a6SBenjamin Coddington switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) { 2095e55c34a6SBenjamin Coddington case FL_POSIX: 2096e55c34a6SBenjamin Coddington res = posix_lock_inode_wait(inode, fl); 2097e55c34a6SBenjamin Coddington break; 2098e55c34a6SBenjamin Coddington case FL_FLOCK: 2099e55c34a6SBenjamin Coddington res = flock_lock_inode_wait(inode, fl); 2100e55c34a6SBenjamin Coddington break; 2101e55c34a6SBenjamin Coddington default: 2102e55c34a6SBenjamin Coddington BUG(); 2103e55c34a6SBenjamin Coddington } 2104e55c34a6SBenjamin Coddington return res; 2105e55c34a6SBenjamin Coddington } 2106e55c34a6SBenjamin Coddington EXPORT_SYMBOL(locks_lock_inode_wait); 2107e55c34a6SBenjamin Coddington 2108e55c34a6SBenjamin Coddington /** 21091da177e4SLinus Torvalds * sys_flock: - flock() system call. 21101da177e4SLinus Torvalds * @fd: the file descriptor to lock. 21111da177e4SLinus Torvalds * @cmd: the type of lock to apply. 21121da177e4SLinus Torvalds * 21131da177e4SLinus Torvalds * Apply a %FL_FLOCK style lock to an open file descriptor. 211480b79dd0SMauro Carvalho Chehab * The @cmd can be one of: 21151da177e4SLinus Torvalds * 211680b79dd0SMauro Carvalho Chehab * - %LOCK_SH -- a shared lock. 211780b79dd0SMauro Carvalho Chehab * - %LOCK_EX -- an exclusive lock. 211880b79dd0SMauro Carvalho Chehab * - %LOCK_UN -- remove an existing lock. 211980b79dd0SMauro Carvalho Chehab * - %LOCK_MAND -- a 'mandatory' flock. 212080b79dd0SMauro Carvalho Chehab * This exists to emulate Windows Share Modes. 21211da177e4SLinus Torvalds * 21221da177e4SLinus Torvalds * %LOCK_MAND can be combined with %LOCK_READ or %LOCK_WRITE to allow other 21231da177e4SLinus Torvalds * processes read and write access respectively. 21241da177e4SLinus Torvalds */ 2125002c8976SHeiko Carstens SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd) 21261da177e4SLinus Torvalds { 21272903ff01SAl Viro struct fd f = fdget(fd); 21281da177e4SLinus Torvalds struct file_lock *lock; 21291da177e4SLinus Torvalds int can_sleep, unlock; 21301da177e4SLinus Torvalds int error; 21311da177e4SLinus Torvalds 21321da177e4SLinus Torvalds error = -EBADF; 21332903ff01SAl Viro if (!f.file) 21341da177e4SLinus Torvalds goto out; 21351da177e4SLinus Torvalds 21361da177e4SLinus Torvalds can_sleep = !(cmd & LOCK_NB); 21371da177e4SLinus Torvalds cmd &= ~LOCK_NB; 21381da177e4SLinus Torvalds unlock = (cmd == LOCK_UN); 21391da177e4SLinus Torvalds 2140aeb5d727SAl Viro if (!unlock && !(cmd & LOCK_MAND) && 21412903ff01SAl Viro !(f.file->f_mode & (FMODE_READ|FMODE_WRITE))) 21421da177e4SLinus Torvalds goto out_putf; 21431da177e4SLinus Torvalds 2144d6367d62SNeilBrown lock = flock_make_lock(f.file, cmd, NULL); 21456e129d00SJeff Layton if (IS_ERR(lock)) { 21466e129d00SJeff Layton error = PTR_ERR(lock); 21471da177e4SLinus Torvalds goto out_putf; 21486e129d00SJeff Layton } 21496e129d00SJeff Layton 21501da177e4SLinus Torvalds if (can_sleep) 21511da177e4SLinus Torvalds lock->fl_flags |= FL_SLEEP; 21521da177e4SLinus Torvalds 21532903ff01SAl Viro error = security_file_lock(f.file, lock->fl_type); 21541da177e4SLinus Torvalds if (error) 21551da177e4SLinus Torvalds goto out_free; 21561da177e4SLinus Torvalds 2157de2a4a50SMiklos Szeredi if (f.file->f_op->flock) 21582903ff01SAl Viro error = f.file->f_op->flock(f.file, 21591da177e4SLinus Torvalds (can_sleep) ? F_SETLKW : F_SETLK, 21601da177e4SLinus Torvalds lock); 21611da177e4SLinus Torvalds else 21624f656367SBenjamin Coddington error = locks_lock_file_wait(f.file, lock); 21631da177e4SLinus Torvalds 21641da177e4SLinus Torvalds out_free: 21651da177e4SLinus Torvalds locks_free_lock(lock); 21661da177e4SLinus Torvalds 21671da177e4SLinus Torvalds out_putf: 21682903ff01SAl Viro fdput(f); 21691da177e4SLinus Torvalds out: 21701da177e4SLinus Torvalds return error; 21711da177e4SLinus Torvalds } 21721da177e4SLinus Torvalds 21733ee17abdSJ. Bruce Fields /** 21743ee17abdSJ. Bruce Fields * vfs_test_lock - test file byte range lock 21753ee17abdSJ. Bruce Fields * @filp: The file to test lock for 21766924c554SJ. Bruce Fields * @fl: The lock to test; also used to hold result 21773ee17abdSJ. Bruce Fields * 21783ee17abdSJ. Bruce Fields * Returns -ERRNO on failure. Indicates presence of conflicting lock by 21793ee17abdSJ. Bruce Fields * setting conf->fl_type to something other than F_UNLCK. 21803ee17abdSJ. Bruce Fields */ 21813ee17abdSJ. Bruce Fields int vfs_test_lock(struct file *filp, struct file_lock *fl) 21823ee17abdSJ. Bruce Fields { 2183de2a4a50SMiklos Szeredi if (filp->f_op->lock) 21843ee17abdSJ. Bruce Fields return filp->f_op->lock(filp, F_GETLK, fl); 21853ee17abdSJ. Bruce Fields posix_test_lock(filp, fl); 21863ee17abdSJ. Bruce Fields return 0; 21873ee17abdSJ. Bruce Fields } 21883ee17abdSJ. Bruce Fields EXPORT_SYMBOL_GPL(vfs_test_lock); 21893ee17abdSJ. Bruce Fields 21909d5b86acSBenjamin Coddington /** 21919d5b86acSBenjamin Coddington * locks_translate_pid - translate a file_lock's fl_pid number into a namespace 21929d5b86acSBenjamin Coddington * @fl: The file_lock who's fl_pid should be translated 21939d5b86acSBenjamin Coddington * @ns: The namespace into which the pid should be translated 21949d5b86acSBenjamin Coddington * 21959d5b86acSBenjamin Coddington * Used to tranlate a fl_pid into a namespace virtual pid number 21969d5b86acSBenjamin Coddington */ 21979d5b86acSBenjamin Coddington static pid_t locks_translate_pid(struct file_lock *fl, struct pid_namespace *ns) 21989d5b86acSBenjamin Coddington { 21999d5b86acSBenjamin Coddington pid_t vnr; 22009d5b86acSBenjamin Coddington struct pid *pid; 22019d5b86acSBenjamin Coddington 22029d5b86acSBenjamin Coddington if (IS_OFDLCK(fl)) 22039d5b86acSBenjamin Coddington return -1; 22049d5b86acSBenjamin Coddington if (IS_REMOTELCK(fl)) 22059d5b86acSBenjamin Coddington return fl->fl_pid; 2206826d7bc9SKonstantin Khorenko /* 2207826d7bc9SKonstantin Khorenko * If the flock owner process is dead and its pid has been already 2208826d7bc9SKonstantin Khorenko * freed, the translation below won't work, but we still want to show 2209826d7bc9SKonstantin Khorenko * flock owner pid number in init pidns. 2210826d7bc9SKonstantin Khorenko */ 2211826d7bc9SKonstantin Khorenko if (ns == &init_pid_ns) 2212826d7bc9SKonstantin Khorenko return (pid_t)fl->fl_pid; 22139d5b86acSBenjamin Coddington 22149d5b86acSBenjamin Coddington rcu_read_lock(); 22159d5b86acSBenjamin Coddington pid = find_pid_ns(fl->fl_pid, &init_pid_ns); 22169d5b86acSBenjamin Coddington vnr = pid_nr_ns(pid, ns); 22179d5b86acSBenjamin Coddington rcu_read_unlock(); 22189d5b86acSBenjamin Coddington return vnr; 22199d5b86acSBenjamin Coddington } 22209d5b86acSBenjamin Coddington 2221c2fa1b8aSJ. Bruce Fields static int posix_lock_to_flock(struct flock *flock, struct file_lock *fl) 2222c2fa1b8aSJ. Bruce Fields { 22239d5b86acSBenjamin Coddington flock->l_pid = locks_translate_pid(fl, task_active_pid_ns(current)); 2224c2fa1b8aSJ. Bruce Fields #if BITS_PER_LONG == 32 2225c2fa1b8aSJ. Bruce Fields /* 2226c2fa1b8aSJ. Bruce Fields * Make sure we can represent the posix lock via 2227c2fa1b8aSJ. Bruce Fields * legacy 32bit flock. 2228c2fa1b8aSJ. Bruce Fields */ 2229c2fa1b8aSJ. Bruce Fields if (fl->fl_start > OFFT_OFFSET_MAX) 2230c2fa1b8aSJ. Bruce Fields return -EOVERFLOW; 2231c2fa1b8aSJ. Bruce Fields if (fl->fl_end != OFFSET_MAX && fl->fl_end > OFFT_OFFSET_MAX) 2232c2fa1b8aSJ. Bruce Fields return -EOVERFLOW; 2233c2fa1b8aSJ. Bruce Fields #endif 2234c2fa1b8aSJ. Bruce Fields flock->l_start = fl->fl_start; 2235c2fa1b8aSJ. Bruce Fields flock->l_len = fl->fl_end == OFFSET_MAX ? 0 : 2236c2fa1b8aSJ. Bruce Fields fl->fl_end - fl->fl_start + 1; 2237c2fa1b8aSJ. Bruce Fields flock->l_whence = 0; 2238129a84deSJ. Bruce Fields flock->l_type = fl->fl_type; 2239c2fa1b8aSJ. Bruce Fields return 0; 2240c2fa1b8aSJ. Bruce Fields } 2241c2fa1b8aSJ. Bruce Fields 2242c2fa1b8aSJ. Bruce Fields #if BITS_PER_LONG == 32 2243c2fa1b8aSJ. Bruce Fields static void posix_lock_to_flock64(struct flock64 *flock, struct file_lock *fl) 2244c2fa1b8aSJ. Bruce Fields { 22459d5b86acSBenjamin Coddington flock->l_pid = locks_translate_pid(fl, task_active_pid_ns(current)); 2246c2fa1b8aSJ. Bruce Fields flock->l_start = fl->fl_start; 2247c2fa1b8aSJ. Bruce Fields flock->l_len = fl->fl_end == OFFSET_MAX ? 0 : 2248c2fa1b8aSJ. Bruce Fields fl->fl_end - fl->fl_start + 1; 2249c2fa1b8aSJ. Bruce Fields flock->l_whence = 0; 2250c2fa1b8aSJ. Bruce Fields flock->l_type = fl->fl_type; 2251c2fa1b8aSJ. Bruce Fields } 2252c2fa1b8aSJ. Bruce Fields #endif 2253c2fa1b8aSJ. Bruce Fields 22541da177e4SLinus Torvalds /* Report the first existing lock that would conflict with l. 22551da177e4SLinus Torvalds * This implements the F_GETLK command of fcntl(). 22561da177e4SLinus Torvalds */ 2257a75d30c7SChristoph Hellwig int fcntl_getlk(struct file *filp, unsigned int cmd, struct flock *flock) 22581da177e4SLinus Torvalds { 225952306e88SBenjamin Coddington struct file_lock *fl; 22601da177e4SLinus Torvalds int error; 22611da177e4SLinus Torvalds 226252306e88SBenjamin Coddington fl = locks_alloc_lock(); 226352306e88SBenjamin Coddington if (fl == NULL) 226452306e88SBenjamin Coddington return -ENOMEM; 22651da177e4SLinus Torvalds error = -EINVAL; 2266a75d30c7SChristoph Hellwig if (flock->l_type != F_RDLCK && flock->l_type != F_WRLCK) 22671da177e4SLinus Torvalds goto out; 22681da177e4SLinus Torvalds 226952306e88SBenjamin Coddington error = flock_to_posix_lock(filp, fl, flock); 22701da177e4SLinus Torvalds if (error) 22711da177e4SLinus Torvalds goto out; 22721da177e4SLinus Torvalds 22730d3f7a2dSJeff Layton if (cmd == F_OFD_GETLK) { 227490478939SJeff Layton error = -EINVAL; 2275a75d30c7SChristoph Hellwig if (flock->l_pid != 0) 227690478939SJeff Layton goto out; 227790478939SJeff Layton 227852306e88SBenjamin Coddington fl->fl_flags |= FL_OFDLCK; 227952306e88SBenjamin Coddington fl->fl_owner = filp; 22805d50ffd7SJeff Layton } 22815d50ffd7SJeff Layton 228252306e88SBenjamin Coddington error = vfs_test_lock(filp, fl); 22833ee17abdSJ. Bruce Fields if (error) 22841da177e4SLinus Torvalds goto out; 22851da177e4SLinus Torvalds 228652306e88SBenjamin Coddington flock->l_type = fl->fl_type; 228752306e88SBenjamin Coddington if (fl->fl_type != F_UNLCK) { 228852306e88SBenjamin Coddington error = posix_lock_to_flock(flock, fl); 2289c2fa1b8aSJ. Bruce Fields if (error) 229052306e88SBenjamin Coddington goto out; 22911da177e4SLinus Torvalds } 22921da177e4SLinus Torvalds out: 229352306e88SBenjamin Coddington locks_free_lock(fl); 22941da177e4SLinus Torvalds return error; 22951da177e4SLinus Torvalds } 22961da177e4SLinus Torvalds 22977723ec97SMarc Eshel /** 22987723ec97SMarc Eshel * vfs_lock_file - file byte range lock 22997723ec97SMarc Eshel * @filp: The file to apply the lock to 23007723ec97SMarc Eshel * @cmd: type of locking operation (F_SETLK, F_GETLK, etc.) 23017723ec97SMarc Eshel * @fl: The lock to be applied 2302150b3934SMarc Eshel * @conf: Place to return a copy of the conflicting lock, if found. 2303150b3934SMarc Eshel * 2304150b3934SMarc Eshel * A caller that doesn't care about the conflicting lock may pass NULL 2305150b3934SMarc Eshel * as the final argument. 2306150b3934SMarc Eshel * 2307150b3934SMarc Eshel * If the filesystem defines a private ->lock() method, then @conf will 2308150b3934SMarc Eshel * be left unchanged; so a caller that cares should initialize it to 2309150b3934SMarc Eshel * some acceptable default. 23102beb6614SMarc Eshel * 23112beb6614SMarc Eshel * To avoid blocking kernel daemons, such as lockd, that need to acquire POSIX 23122beb6614SMarc Eshel * locks, the ->lock() interface may return asynchronously, before the lock has 23132beb6614SMarc Eshel * been granted or denied by the underlying filesystem, if (and only if) 23148fb47a4fSJ. Bruce Fields * lm_grant is set. Callers expecting ->lock() to return asynchronously 23152beb6614SMarc Eshel * will only use F_SETLK, not F_SETLKW; they will set FL_SLEEP if (and only if) 23162beb6614SMarc Eshel * the request is for a blocking lock. When ->lock() does return asynchronously, 23178fb47a4fSJ. Bruce Fields * it must return FILE_LOCK_DEFERRED, and call ->lm_grant() when the lock 23182beb6614SMarc Eshel * request completes. 23192beb6614SMarc Eshel * If the request is for non-blocking lock the file system should return 2320bde74e4bSMiklos Szeredi * FILE_LOCK_DEFERRED then try to get the lock and call the callback routine 2321bde74e4bSMiklos Szeredi * with the result. If the request timed out the callback routine will return a 23222beb6614SMarc Eshel * nonzero return code and the file system should release the lock. The file 23232beb6614SMarc Eshel * system is also responsible to keep a corresponding posix lock when it 23242beb6614SMarc Eshel * grants a lock so the VFS can find out which locks are locally held and do 23252beb6614SMarc Eshel * the correct lock cleanup when required. 23262beb6614SMarc Eshel * The underlying filesystem must not drop the kernel lock or call 23278fb47a4fSJ. Bruce Fields * ->lm_grant() before returning to the caller with a FILE_LOCK_DEFERRED 23282beb6614SMarc Eshel * return code. 23297723ec97SMarc Eshel */ 2330150b3934SMarc Eshel int vfs_lock_file(struct file *filp, unsigned int cmd, struct file_lock *fl, struct file_lock *conf) 23317723ec97SMarc Eshel { 2332de2a4a50SMiklos Szeredi if (filp->f_op->lock) 23337723ec97SMarc Eshel return filp->f_op->lock(filp, cmd, fl); 23347723ec97SMarc Eshel else 2335150b3934SMarc Eshel return posix_lock_file(filp, fl, conf); 23367723ec97SMarc Eshel } 23377723ec97SMarc Eshel EXPORT_SYMBOL_GPL(vfs_lock_file); 23387723ec97SMarc Eshel 2339b648a6deSMiklos Szeredi static int do_lock_file_wait(struct file *filp, unsigned int cmd, 2340b648a6deSMiklos Szeredi struct file_lock *fl) 2341b648a6deSMiklos Szeredi { 2342b648a6deSMiklos Szeredi int error; 2343b648a6deSMiklos Szeredi 2344b648a6deSMiklos Szeredi error = security_file_lock(filp, fl->fl_type); 2345b648a6deSMiklos Szeredi if (error) 2346b648a6deSMiklos Szeredi return error; 2347b648a6deSMiklos Szeredi 2348b648a6deSMiklos Szeredi for (;;) { 2349764c76b3SMiklos Szeredi error = vfs_lock_file(filp, cmd, fl, NULL); 2350b648a6deSMiklos Szeredi if (error != FILE_LOCK_DEFERRED) 2351b648a6deSMiklos Szeredi break; 2352dcf23ac3SLinus Torvalds error = wait_event_interruptible(fl->fl_wait, 2353dcf23ac3SLinus Torvalds list_empty(&fl->fl_blocked_member)); 235416306a61SNeilBrown if (error) 2355b648a6deSMiklos Szeredi break; 2356b648a6deSMiklos Szeredi } 235716306a61SNeilBrown locks_delete_block(fl); 2358b648a6deSMiklos Szeredi 2359b648a6deSMiklos Szeredi return error; 2360b648a6deSMiklos Szeredi } 2361b648a6deSMiklos Szeredi 23626ca7d910SBenjamin Coddington /* Ensure that fl->fl_file has compatible f_mode for F_SETLK calls */ 2363cf01f4eeSJeff Layton static int 2364cf01f4eeSJeff Layton check_fmode_for_setlk(struct file_lock *fl) 2365cf01f4eeSJeff Layton { 2366cf01f4eeSJeff Layton switch (fl->fl_type) { 2367cf01f4eeSJeff Layton case F_RDLCK: 2368cf01f4eeSJeff Layton if (!(fl->fl_file->f_mode & FMODE_READ)) 2369cf01f4eeSJeff Layton return -EBADF; 2370cf01f4eeSJeff Layton break; 2371cf01f4eeSJeff Layton case F_WRLCK: 2372cf01f4eeSJeff Layton if (!(fl->fl_file->f_mode & FMODE_WRITE)) 2373cf01f4eeSJeff Layton return -EBADF; 2374cf01f4eeSJeff Layton } 2375cf01f4eeSJeff Layton return 0; 2376cf01f4eeSJeff Layton } 2377cf01f4eeSJeff Layton 23781da177e4SLinus Torvalds /* Apply the lock described by l to an open file descriptor. 23791da177e4SLinus Torvalds * This implements both the F_SETLK and F_SETLKW commands of fcntl(). 23801da177e4SLinus Torvalds */ 2381c293621bSPeter Staubach int fcntl_setlk(unsigned int fd, struct file *filp, unsigned int cmd, 2382a75d30c7SChristoph Hellwig struct flock *flock) 23831da177e4SLinus Torvalds { 23841da177e4SLinus Torvalds struct file_lock *file_lock = locks_alloc_lock(); 2385a75d30c7SChristoph Hellwig struct inode *inode = locks_inode(filp); 23860b2bac2fSAl Viro struct file *f; 23871da177e4SLinus Torvalds int error; 23881da177e4SLinus Torvalds 23891da177e4SLinus Torvalds if (file_lock == NULL) 23901da177e4SLinus Torvalds return -ENOLCK; 23911da177e4SLinus Torvalds 2392a75d30c7SChristoph Hellwig error = flock_to_posix_lock(filp, file_lock, flock); 23931da177e4SLinus Torvalds if (error) 23941da177e4SLinus Torvalds goto out; 23955d50ffd7SJeff Layton 2396cf01f4eeSJeff Layton error = check_fmode_for_setlk(file_lock); 2397cf01f4eeSJeff Layton if (error) 2398cf01f4eeSJeff Layton goto out; 2399cf01f4eeSJeff Layton 24005d50ffd7SJeff Layton /* 24015d50ffd7SJeff Layton * If the cmd is requesting file-private locks, then set the 2402cff2fce5SJeff Layton * FL_OFDLCK flag and override the owner. 24035d50ffd7SJeff Layton */ 24045d50ffd7SJeff Layton switch (cmd) { 24050d3f7a2dSJeff Layton case F_OFD_SETLK: 240690478939SJeff Layton error = -EINVAL; 2407a75d30c7SChristoph Hellwig if (flock->l_pid != 0) 240890478939SJeff Layton goto out; 240990478939SJeff Layton 24105d50ffd7SJeff Layton cmd = F_SETLK; 2411cff2fce5SJeff Layton file_lock->fl_flags |= FL_OFDLCK; 241273a8f5f7SChristoph Hellwig file_lock->fl_owner = filp; 24135d50ffd7SJeff Layton break; 24140d3f7a2dSJeff Layton case F_OFD_SETLKW: 241590478939SJeff Layton error = -EINVAL; 2416a75d30c7SChristoph Hellwig if (flock->l_pid != 0) 241790478939SJeff Layton goto out; 241890478939SJeff Layton 24195d50ffd7SJeff Layton cmd = F_SETLKW; 2420cff2fce5SJeff Layton file_lock->fl_flags |= FL_OFDLCK; 242173a8f5f7SChristoph Hellwig file_lock->fl_owner = filp; 2422df561f66SGustavo A. R. Silva fallthrough; 24235d50ffd7SJeff Layton case F_SETLKW: 24241da177e4SLinus Torvalds file_lock->fl_flags |= FL_SLEEP; 24251da177e4SLinus Torvalds } 24261da177e4SLinus Torvalds 2427b648a6deSMiklos Szeredi error = do_lock_file_wait(filp, cmd, file_lock); 2428c293621bSPeter Staubach 2429c293621bSPeter Staubach /* 24300752ba80SJeff Layton * Attempt to detect a close/fcntl race and recover by releasing the 24310752ba80SJeff Layton * lock that was just acquired. There is no need to do that when we're 24320752ba80SJeff Layton * unlocking though, or for OFD locks. 2433c293621bSPeter Staubach */ 24340752ba80SJeff Layton if (!error && file_lock->fl_type != F_UNLCK && 24350752ba80SJeff Layton !(file_lock->fl_flags & FL_OFDLCK)) { 2436120ce2b0SEric W. Biederman struct files_struct *files = current->files; 24370b2bac2fSAl Viro /* 24387f3697e2SJeff Layton * We need that spin_lock here - it prevents reordering between 24397f3697e2SJeff Layton * update of i_flctx->flc_posix and check for it done in 24407f3697e2SJeff Layton * close(). rcu_read_lock() wouldn't do. 24410b2bac2fSAl Viro */ 2442120ce2b0SEric W. Biederman spin_lock(&files->file_lock); 2443120ce2b0SEric W. Biederman f = files_lookup_fd_locked(files, fd); 2444120ce2b0SEric W. Biederman spin_unlock(&files->file_lock); 24457f3697e2SJeff Layton if (f != filp) { 24467f3697e2SJeff Layton file_lock->fl_type = F_UNLCK; 24477f3697e2SJeff Layton error = do_lock_file_wait(filp, cmd, file_lock); 24487f3697e2SJeff Layton WARN_ON_ONCE(error); 24497f3697e2SJeff Layton error = -EBADF; 2450c293621bSPeter Staubach } 24517f3697e2SJeff Layton } 24521da177e4SLinus Torvalds out: 24531890910fSJeff Layton trace_fcntl_setlk(inode, file_lock, error); 24541da177e4SLinus Torvalds locks_free_lock(file_lock); 24551da177e4SLinus Torvalds return error; 24561da177e4SLinus Torvalds } 24571da177e4SLinus Torvalds 24581da177e4SLinus Torvalds #if BITS_PER_LONG == 32 24591da177e4SLinus Torvalds /* Report the first existing lock that would conflict with l. 24601da177e4SLinus Torvalds * This implements the F_GETLK command of fcntl(). 24611da177e4SLinus Torvalds */ 2462a75d30c7SChristoph Hellwig int fcntl_getlk64(struct file *filp, unsigned int cmd, struct flock64 *flock) 24631da177e4SLinus Torvalds { 246452306e88SBenjamin Coddington struct file_lock *fl; 24651da177e4SLinus Torvalds int error; 24661da177e4SLinus Torvalds 246752306e88SBenjamin Coddington fl = locks_alloc_lock(); 246852306e88SBenjamin Coddington if (fl == NULL) 246952306e88SBenjamin Coddington return -ENOMEM; 247052306e88SBenjamin Coddington 24711da177e4SLinus Torvalds error = -EINVAL; 2472a75d30c7SChristoph Hellwig if (flock->l_type != F_RDLCK && flock->l_type != F_WRLCK) 24731da177e4SLinus Torvalds goto out; 24741da177e4SLinus Torvalds 247552306e88SBenjamin Coddington error = flock64_to_posix_lock(filp, fl, flock); 24761da177e4SLinus Torvalds if (error) 24771da177e4SLinus Torvalds goto out; 24781da177e4SLinus Torvalds 24790d3f7a2dSJeff Layton if (cmd == F_OFD_GETLK) { 248090478939SJeff Layton error = -EINVAL; 2481a75d30c7SChristoph Hellwig if (flock->l_pid != 0) 248290478939SJeff Layton goto out; 248390478939SJeff Layton 24845d50ffd7SJeff Layton cmd = F_GETLK64; 248552306e88SBenjamin Coddington fl->fl_flags |= FL_OFDLCK; 248652306e88SBenjamin Coddington fl->fl_owner = filp; 24875d50ffd7SJeff Layton } 24885d50ffd7SJeff Layton 248952306e88SBenjamin Coddington error = vfs_test_lock(filp, fl); 24903ee17abdSJ. Bruce Fields if (error) 24911da177e4SLinus Torvalds goto out; 24921da177e4SLinus Torvalds 249352306e88SBenjamin Coddington flock->l_type = fl->fl_type; 249452306e88SBenjamin Coddington if (fl->fl_type != F_UNLCK) 249552306e88SBenjamin Coddington posix_lock_to_flock64(flock, fl); 24961da177e4SLinus Torvalds 24971da177e4SLinus Torvalds out: 249852306e88SBenjamin Coddington locks_free_lock(fl); 24991da177e4SLinus Torvalds return error; 25001da177e4SLinus Torvalds } 25011da177e4SLinus Torvalds 25021da177e4SLinus Torvalds /* Apply the lock described by l to an open file descriptor. 25031da177e4SLinus Torvalds * This implements both the F_SETLK and F_SETLKW commands of fcntl(). 25041da177e4SLinus Torvalds */ 2505c293621bSPeter Staubach int fcntl_setlk64(unsigned int fd, struct file *filp, unsigned int cmd, 2506a75d30c7SChristoph Hellwig struct flock64 *flock) 25071da177e4SLinus Torvalds { 25081da177e4SLinus Torvalds struct file_lock *file_lock = locks_alloc_lock(); 25090b2bac2fSAl Viro struct file *f; 25101da177e4SLinus Torvalds int error; 25111da177e4SLinus Torvalds 25121da177e4SLinus Torvalds if (file_lock == NULL) 25131da177e4SLinus Torvalds return -ENOLCK; 25141da177e4SLinus Torvalds 2515a75d30c7SChristoph Hellwig error = flock64_to_posix_lock(filp, file_lock, flock); 25161da177e4SLinus Torvalds if (error) 25171da177e4SLinus Torvalds goto out; 25185d50ffd7SJeff Layton 2519cf01f4eeSJeff Layton error = check_fmode_for_setlk(file_lock); 2520cf01f4eeSJeff Layton if (error) 2521cf01f4eeSJeff Layton goto out; 2522cf01f4eeSJeff Layton 25235d50ffd7SJeff Layton /* 25245d50ffd7SJeff Layton * If the cmd is requesting file-private locks, then set the 2525cff2fce5SJeff Layton * FL_OFDLCK flag and override the owner. 25265d50ffd7SJeff Layton */ 25275d50ffd7SJeff Layton switch (cmd) { 25280d3f7a2dSJeff Layton case F_OFD_SETLK: 252990478939SJeff Layton error = -EINVAL; 2530a75d30c7SChristoph Hellwig if (flock->l_pid != 0) 253190478939SJeff Layton goto out; 253290478939SJeff Layton 25335d50ffd7SJeff Layton cmd = F_SETLK64; 2534cff2fce5SJeff Layton file_lock->fl_flags |= FL_OFDLCK; 253573a8f5f7SChristoph Hellwig file_lock->fl_owner = filp; 25365d50ffd7SJeff Layton break; 25370d3f7a2dSJeff Layton case F_OFD_SETLKW: 253890478939SJeff Layton error = -EINVAL; 2539a75d30c7SChristoph Hellwig if (flock->l_pid != 0) 254090478939SJeff Layton goto out; 254190478939SJeff Layton 25425d50ffd7SJeff Layton cmd = F_SETLKW64; 2543cff2fce5SJeff Layton file_lock->fl_flags |= FL_OFDLCK; 254473a8f5f7SChristoph Hellwig file_lock->fl_owner = filp; 2545df561f66SGustavo A. R. Silva fallthrough; 25465d50ffd7SJeff Layton case F_SETLKW64: 25471da177e4SLinus Torvalds file_lock->fl_flags |= FL_SLEEP; 25481da177e4SLinus Torvalds } 25491da177e4SLinus Torvalds 2550b648a6deSMiklos Szeredi error = do_lock_file_wait(filp, cmd, file_lock); 2551c293621bSPeter Staubach 2552c293621bSPeter Staubach /* 25530752ba80SJeff Layton * Attempt to detect a close/fcntl race and recover by releasing the 25540752ba80SJeff Layton * lock that was just acquired. There is no need to do that when we're 25550752ba80SJeff Layton * unlocking though, or for OFD locks. 2556c293621bSPeter Staubach */ 25570752ba80SJeff Layton if (!error && file_lock->fl_type != F_UNLCK && 25580752ba80SJeff Layton !(file_lock->fl_flags & FL_OFDLCK)) { 2559120ce2b0SEric W. Biederman struct files_struct *files = current->files; 25607f3697e2SJeff Layton /* 25617f3697e2SJeff Layton * We need that spin_lock here - it prevents reordering between 25627f3697e2SJeff Layton * update of i_flctx->flc_posix and check for it done in 25637f3697e2SJeff Layton * close(). rcu_read_lock() wouldn't do. 25647f3697e2SJeff Layton */ 2565120ce2b0SEric W. Biederman spin_lock(&files->file_lock); 2566120ce2b0SEric W. Biederman f = files_lookup_fd_locked(files, fd); 2567120ce2b0SEric W. Biederman spin_unlock(&files->file_lock); 25687f3697e2SJeff Layton if (f != filp) { 25697f3697e2SJeff Layton file_lock->fl_type = F_UNLCK; 25707f3697e2SJeff Layton error = do_lock_file_wait(filp, cmd, file_lock); 25717f3697e2SJeff Layton WARN_ON_ONCE(error); 25727f3697e2SJeff Layton error = -EBADF; 2573c293621bSPeter Staubach } 25747f3697e2SJeff Layton } 25751da177e4SLinus Torvalds out: 25761da177e4SLinus Torvalds locks_free_lock(file_lock); 25771da177e4SLinus Torvalds return error; 25781da177e4SLinus Torvalds } 25791da177e4SLinus Torvalds #endif /* BITS_PER_LONG == 32 */ 25801da177e4SLinus Torvalds 25811da177e4SLinus Torvalds /* 25821da177e4SLinus Torvalds * This function is called when the file is being removed 25831da177e4SLinus Torvalds * from the task's fd array. POSIX locks belonging to this task 25841da177e4SLinus Torvalds * are deleted at this time. 25851da177e4SLinus Torvalds */ 25861da177e4SLinus Torvalds void locks_remove_posix(struct file *filp, fl_owner_t owner) 25871da177e4SLinus Torvalds { 25881890910fSJeff Layton int error; 2589c568d683SMiklos Szeredi struct inode *inode = locks_inode(filp); 2590ff7b86b8SMiklos Szeredi struct file_lock lock; 2591128a3785SDmitry Vyukov struct file_lock_context *ctx; 25921da177e4SLinus Torvalds 25931da177e4SLinus Torvalds /* 25941da177e4SLinus Torvalds * If there are no locks held on this file, we don't need to call 25951da177e4SLinus Torvalds * posix_lock_file(). Another process could be setting a lock on this 25961da177e4SLinus Torvalds * file at the same time, but we wouldn't remove that lock anyway. 25971da177e4SLinus Torvalds */ 2598c568d683SMiklos Szeredi ctx = smp_load_acquire(&inode->i_flctx); 2599bd61e0a9SJeff Layton if (!ctx || list_empty(&ctx->flc_posix)) 26001da177e4SLinus Torvalds return; 26011da177e4SLinus Torvalds 2602d6367d62SNeilBrown locks_init_lock(&lock); 26031da177e4SLinus Torvalds lock.fl_type = F_UNLCK; 260475e1fcc0SMiklos Szeredi lock.fl_flags = FL_POSIX | FL_CLOSE; 26051da177e4SLinus Torvalds lock.fl_start = 0; 26061da177e4SLinus Torvalds lock.fl_end = OFFSET_MAX; 26071da177e4SLinus Torvalds lock.fl_owner = owner; 26081da177e4SLinus Torvalds lock.fl_pid = current->tgid; 26091da177e4SLinus Torvalds lock.fl_file = filp; 26101da177e4SLinus Torvalds lock.fl_ops = NULL; 26111da177e4SLinus Torvalds lock.fl_lmops = NULL; 26121da177e4SLinus Torvalds 26131890910fSJeff Layton error = vfs_lock_file(filp, F_SETLK, &lock, NULL); 26141da177e4SLinus Torvalds 26151da177e4SLinus Torvalds if (lock.fl_ops && lock.fl_ops->fl_release_private) 26161da177e4SLinus Torvalds lock.fl_ops->fl_release_private(&lock); 2617c568d683SMiklos Szeredi trace_locks_remove_posix(inode, &lock, error); 26181da177e4SLinus Torvalds } 26191da177e4SLinus Torvalds EXPORT_SYMBOL(locks_remove_posix); 26201da177e4SLinus Torvalds 26213d8e560dSJeff Layton /* The i_flctx must be valid when calling into here */ 2622dd459bb1SJeff Layton static void 2623128a3785SDmitry Vyukov locks_remove_flock(struct file *filp, struct file_lock_context *flctx) 2624dd459bb1SJeff Layton { 2625d6367d62SNeilBrown struct file_lock fl; 2626c568d683SMiklos Szeredi struct inode *inode = locks_inode(filp); 2627dd459bb1SJeff Layton 26283d8e560dSJeff Layton if (list_empty(&flctx->flc_flock)) 2629dd459bb1SJeff Layton return; 2630dd459bb1SJeff Layton 2631d6367d62SNeilBrown flock_make_lock(filp, LOCK_UN, &fl); 2632d6367d62SNeilBrown fl.fl_flags |= FL_CLOSE; 2633d6367d62SNeilBrown 2634de2a4a50SMiklos Szeredi if (filp->f_op->flock) 2635dd459bb1SJeff Layton filp->f_op->flock(filp, F_SETLKW, &fl); 2636dd459bb1SJeff Layton else 2637bcd7f78dSJeff Layton flock_lock_inode(inode, &fl); 2638dd459bb1SJeff Layton 2639dd459bb1SJeff Layton if (fl.fl_ops && fl.fl_ops->fl_release_private) 2640dd459bb1SJeff Layton fl.fl_ops->fl_release_private(&fl); 2641dd459bb1SJeff Layton } 2642dd459bb1SJeff Layton 26433d8e560dSJeff Layton /* The i_flctx must be valid when calling into here */ 26448634b51fSJeff Layton static void 2645128a3785SDmitry Vyukov locks_remove_lease(struct file *filp, struct file_lock_context *ctx) 26468634b51fSJeff Layton { 26478634b51fSJeff Layton struct file_lock *fl, *tmp; 26488634b51fSJeff Layton LIST_HEAD(dispose); 26498634b51fSJeff Layton 26503d8e560dSJeff Layton if (list_empty(&ctx->flc_lease)) 26518634b51fSJeff Layton return; 26528634b51fSJeff Layton 265302e525b2SPeter Zijlstra percpu_down_read(&file_rwsem); 26546109c850SJeff Layton spin_lock(&ctx->flc_lock); 26558634b51fSJeff Layton list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list) 2656c4e136cdSJeff Layton if (filp == fl->fl_file) 26577448cc37SJeff Layton lease_modify(fl, F_UNLCK, &dispose); 26586109c850SJeff Layton spin_unlock(&ctx->flc_lock); 265902e525b2SPeter Zijlstra percpu_up_read(&file_rwsem); 26605f43086bSPeter Zijlstra 26618634b51fSJeff Layton locks_dispose_list(&dispose); 26628634b51fSJeff Layton } 26638634b51fSJeff Layton 26641da177e4SLinus Torvalds /* 26651da177e4SLinus Torvalds * This function is called on the last close of an open file. 26661da177e4SLinus Torvalds */ 266778ed8a13SJeff Layton void locks_remove_file(struct file *filp) 26681da177e4SLinus Torvalds { 2669128a3785SDmitry Vyukov struct file_lock_context *ctx; 2670128a3785SDmitry Vyukov 2671c568d683SMiklos Szeredi ctx = smp_load_acquire(&locks_inode(filp)->i_flctx); 2672128a3785SDmitry Vyukov if (!ctx) 26733d8e560dSJeff Layton return; 26743d8e560dSJeff Layton 2675dd459bb1SJeff Layton /* remove any OFD locks */ 267673a8f5f7SChristoph Hellwig locks_remove_posix(filp, filp); 26775d50ffd7SJeff Layton 2678dd459bb1SJeff Layton /* remove flock locks */ 2679128a3785SDmitry Vyukov locks_remove_flock(filp, ctx); 2680dd459bb1SJeff Layton 26818634b51fSJeff Layton /* remove any leases */ 2682128a3785SDmitry Vyukov locks_remove_lease(filp, ctx); 26833953704fSBenjamin Coddington 26843953704fSBenjamin Coddington spin_lock(&ctx->flc_lock); 26853953704fSBenjamin Coddington locks_check_ctx_file_list(filp, &ctx->flc_posix, "POSIX"); 26863953704fSBenjamin Coddington locks_check_ctx_file_list(filp, &ctx->flc_flock, "FLOCK"); 26873953704fSBenjamin Coddington locks_check_ctx_file_list(filp, &ctx->flc_lease, "LEASE"); 26883953704fSBenjamin Coddington spin_unlock(&ctx->flc_lock); 26891da177e4SLinus Torvalds } 26901da177e4SLinus Torvalds 26911da177e4SLinus Torvalds /** 26929b9d2ab4SMarc Eshel * vfs_cancel_lock - file byte range unblock lock 26939b9d2ab4SMarc Eshel * @filp: The file to apply the unblock to 26949b9d2ab4SMarc Eshel * @fl: The lock to be unblocked 26959b9d2ab4SMarc Eshel * 26969b9d2ab4SMarc Eshel * Used by lock managers to cancel blocked requests 26979b9d2ab4SMarc Eshel */ 26989b9d2ab4SMarc Eshel int vfs_cancel_lock(struct file *filp, struct file_lock *fl) 26999b9d2ab4SMarc Eshel { 2700de2a4a50SMiklos Szeredi if (filp->f_op->lock) 27019b9d2ab4SMarc Eshel return filp->f_op->lock(filp, F_CANCELLK, fl); 27029b9d2ab4SMarc Eshel return 0; 27039b9d2ab4SMarc Eshel } 27049b9d2ab4SMarc Eshel EXPORT_SYMBOL_GPL(vfs_cancel_lock); 27059b9d2ab4SMarc Eshel 27067f8ada98SPavel Emelyanov #ifdef CONFIG_PROC_FS 2707d8ba7a36SAlexey Dobriyan #include <linux/proc_fs.h> 27087f8ada98SPavel Emelyanov #include <linux/seq_file.h> 27097f8ada98SPavel Emelyanov 27107012b02aSJeff Layton struct locks_iterator { 27117012b02aSJeff Layton int li_cpu; 27127012b02aSJeff Layton loff_t li_pos; 27137012b02aSJeff Layton }; 27147012b02aSJeff Layton 27157f8ada98SPavel Emelyanov static void lock_get_status(struct seq_file *f, struct file_lock *fl, 2716b8da9b10SLuo Longjun loff_t id, char *pfx, int repeat) 27171da177e4SLinus Torvalds { 27181da177e4SLinus Torvalds struct inode *inode = NULL; 2719ab1f1611SVitaliy Gusev unsigned int fl_pid; 27209d78edeaSAlexey Gladkov struct pid_namespace *proc_pidns = proc_pid_ns(file_inode(f->file)->i_sb); 2721d67fd44fSNikolay Borisov 27229d5b86acSBenjamin Coddington fl_pid = locks_translate_pid(fl, proc_pidns); 2723d67fd44fSNikolay Borisov /* 27241cf8e5deSKonstantin Khorenko * If lock owner is dead (and pid is freed) or not visible in current 27251cf8e5deSKonstantin Khorenko * pidns, zero is shown as a pid value. Check lock info from 27261cf8e5deSKonstantin Khorenko * init_pid_ns to get saved lock pid value. 2727d67fd44fSNikolay Borisov */ 27281da177e4SLinus Torvalds 27291da177e4SLinus Torvalds if (fl->fl_file != NULL) 2730c568d683SMiklos Szeredi inode = locks_inode(fl->fl_file); 27311da177e4SLinus Torvalds 2732b8da9b10SLuo Longjun seq_printf(f, "%lld: ", id); 2733b8da9b10SLuo Longjun 2734b8da9b10SLuo Longjun if (repeat) 2735b8da9b10SLuo Longjun seq_printf(f, "%*s", repeat - 1 + (int)strlen(pfx), pfx); 2736b8da9b10SLuo Longjun 27371da177e4SLinus Torvalds if (IS_POSIX(fl)) { 2738c918d42aSJeff Layton if (fl->fl_flags & FL_ACCESS) 27395315c26aSFabian Frederick seq_puts(f, "ACCESS"); 2740cff2fce5SJeff Layton else if (IS_OFDLCK(fl)) 27415315c26aSFabian Frederick seq_puts(f, "OFDLCK"); 2742c918d42aSJeff Layton else 27435315c26aSFabian Frederick seq_puts(f, "POSIX "); 2744c918d42aSJeff Layton 2745c918d42aSJeff Layton seq_printf(f, " %s ", 2746*f7e33bdbSJeff Layton (inode == NULL) ? "*NOINODE*" : "ADVISORY "); 27471da177e4SLinus Torvalds } else if (IS_FLOCK(fl)) { 27481da177e4SLinus Torvalds if (fl->fl_type & LOCK_MAND) { 27495315c26aSFabian Frederick seq_puts(f, "FLOCK MSNFS "); 27501da177e4SLinus Torvalds } else { 27515315c26aSFabian Frederick seq_puts(f, "FLOCK ADVISORY "); 27521da177e4SLinus Torvalds } 27531da177e4SLinus Torvalds } else if (IS_LEASE(fl)) { 27548144f1f6SJeff Layton if (fl->fl_flags & FL_DELEG) 27558144f1f6SJeff Layton seq_puts(f, "DELEG "); 27568144f1f6SJeff Layton else 27575315c26aSFabian Frederick seq_puts(f, "LEASE "); 27588144f1f6SJeff Layton 2759ab83fa4bSJ. Bruce Fields if (lease_breaking(fl)) 27605315c26aSFabian Frederick seq_puts(f, "BREAKING "); 27611da177e4SLinus Torvalds else if (fl->fl_file) 27625315c26aSFabian Frederick seq_puts(f, "ACTIVE "); 27631da177e4SLinus Torvalds else 27645315c26aSFabian Frederick seq_puts(f, "BREAKER "); 27651da177e4SLinus Torvalds } else { 27665315c26aSFabian Frederick seq_puts(f, "UNKNOWN UNKNOWN "); 27671da177e4SLinus Torvalds } 27681da177e4SLinus Torvalds if (fl->fl_type & LOCK_MAND) { 27697f8ada98SPavel Emelyanov seq_printf(f, "%s ", 27701da177e4SLinus Torvalds (fl->fl_type & LOCK_READ) 27711da177e4SLinus Torvalds ? (fl->fl_type & LOCK_WRITE) ? "RW " : "READ " 27721da177e4SLinus Torvalds : (fl->fl_type & LOCK_WRITE) ? "WRITE" : "NONE "); 27731da177e4SLinus Torvalds } else { 277443e4cb94SPavel Begunkov int type = IS_LEASE(fl) ? target_leasetype(fl) : fl->fl_type; 277543e4cb94SPavel Begunkov 277643e4cb94SPavel Begunkov seq_printf(f, "%s ", (type == F_WRLCK) ? "WRITE" : 277743e4cb94SPavel Begunkov (type == F_RDLCK) ? "READ" : "UNLCK"); 27781da177e4SLinus Torvalds } 27791da177e4SLinus Torvalds if (inode) { 27803648888eSJeff Layton /* userspace relies on this representation of dev_t */ 278198ca480aSAmir Goldstein seq_printf(f, "%d %02x:%02x:%lu ", fl_pid, 27821da177e4SLinus Torvalds MAJOR(inode->i_sb->s_dev), 27831da177e4SLinus Torvalds MINOR(inode->i_sb->s_dev), inode->i_ino); 27841da177e4SLinus Torvalds } else { 2785ab1f1611SVitaliy Gusev seq_printf(f, "%d <none>:0 ", fl_pid); 27861da177e4SLinus Torvalds } 27871da177e4SLinus Torvalds if (IS_POSIX(fl)) { 27881da177e4SLinus Torvalds if (fl->fl_end == OFFSET_MAX) 27897f8ada98SPavel Emelyanov seq_printf(f, "%Ld EOF\n", fl->fl_start); 27901da177e4SLinus Torvalds else 27917f8ada98SPavel Emelyanov seq_printf(f, "%Ld %Ld\n", fl->fl_start, fl->fl_end); 27921da177e4SLinus Torvalds } else { 27935315c26aSFabian Frederick seq_puts(f, "0 EOF\n"); 27941da177e4SLinus Torvalds } 27951da177e4SLinus Torvalds } 27961da177e4SLinus Torvalds 2797b8da9b10SLuo Longjun static struct file_lock *get_next_blocked_member(struct file_lock *node) 2798b8da9b10SLuo Longjun { 2799b8da9b10SLuo Longjun struct file_lock *tmp; 2800b8da9b10SLuo Longjun 2801b8da9b10SLuo Longjun /* NULL node or root node */ 2802b8da9b10SLuo Longjun if (node == NULL || node->fl_blocker == NULL) 2803b8da9b10SLuo Longjun return NULL; 2804b8da9b10SLuo Longjun 2805b8da9b10SLuo Longjun /* Next member in the linked list could be itself */ 2806b8da9b10SLuo Longjun tmp = list_next_entry(node, fl_blocked_member); 2807b8da9b10SLuo Longjun if (list_entry_is_head(tmp, &node->fl_blocker->fl_blocked_requests, fl_blocked_member) 2808b8da9b10SLuo Longjun || tmp == node) { 2809b8da9b10SLuo Longjun return NULL; 2810b8da9b10SLuo Longjun } 2811b8da9b10SLuo Longjun 2812b8da9b10SLuo Longjun return tmp; 2813b8da9b10SLuo Longjun } 2814b8da9b10SLuo Longjun 28157f8ada98SPavel Emelyanov static int locks_show(struct seq_file *f, void *v) 28161da177e4SLinus Torvalds { 28177012b02aSJeff Layton struct locks_iterator *iter = f->private; 2818b8da9b10SLuo Longjun struct file_lock *cur, *tmp; 28199d78edeaSAlexey Gladkov struct pid_namespace *proc_pidns = proc_pid_ns(file_inode(f->file)->i_sb); 2820b8da9b10SLuo Longjun int level = 0; 28217f8ada98SPavel Emelyanov 2822b8da9b10SLuo Longjun cur = hlist_entry(v, struct file_lock, fl_link); 28237f8ada98SPavel Emelyanov 2824b8da9b10SLuo Longjun if (locks_translate_pid(cur, proc_pidns) == 0) 2825d67fd44fSNikolay Borisov return 0; 2826d67fd44fSNikolay Borisov 2827b8da9b10SLuo Longjun /* View this crossed linked list as a binary tree, the first member of fl_blocked_requests 2828b8da9b10SLuo Longjun * is the left child of current node, the next silibing in fl_blocked_member is the 2829b8da9b10SLuo Longjun * right child, we can alse get the parent of current node from fl_blocker, so this 2830b8da9b10SLuo Longjun * question becomes traversal of a binary tree 2831b8da9b10SLuo Longjun */ 2832b8da9b10SLuo Longjun while (cur != NULL) { 2833b8da9b10SLuo Longjun if (level) 2834b8da9b10SLuo Longjun lock_get_status(f, cur, iter->li_pos, "-> ", level); 2835b8da9b10SLuo Longjun else 2836b8da9b10SLuo Longjun lock_get_status(f, cur, iter->li_pos, "", level); 28377f8ada98SPavel Emelyanov 2838b8da9b10SLuo Longjun if (!list_empty(&cur->fl_blocked_requests)) { 2839b8da9b10SLuo Longjun /* Turn left */ 2840b8da9b10SLuo Longjun cur = list_first_entry_or_null(&cur->fl_blocked_requests, 2841b8da9b10SLuo Longjun struct file_lock, fl_blocked_member); 2842b8da9b10SLuo Longjun level++; 2843b8da9b10SLuo Longjun } else { 2844b8da9b10SLuo Longjun /* Turn right */ 2845b8da9b10SLuo Longjun tmp = get_next_blocked_member(cur); 2846b8da9b10SLuo Longjun /* Fall back to parent node */ 2847b8da9b10SLuo Longjun while (tmp == NULL && cur->fl_blocker != NULL) { 2848b8da9b10SLuo Longjun cur = cur->fl_blocker; 2849b8da9b10SLuo Longjun level--; 2850b8da9b10SLuo Longjun tmp = get_next_blocked_member(cur); 2851b8da9b10SLuo Longjun } 2852b8da9b10SLuo Longjun cur = tmp; 2853b8da9b10SLuo Longjun } 2854b8da9b10SLuo Longjun } 28557f8ada98SPavel Emelyanov 28567f8ada98SPavel Emelyanov return 0; 28571da177e4SLinus Torvalds } 28581da177e4SLinus Torvalds 28596c8c9031SAndrey Vagin static void __show_fd_locks(struct seq_file *f, 28606c8c9031SAndrey Vagin struct list_head *head, int *id, 28616c8c9031SAndrey Vagin struct file *filp, struct files_struct *files) 28626c8c9031SAndrey Vagin { 28636c8c9031SAndrey Vagin struct file_lock *fl; 28646c8c9031SAndrey Vagin 28656c8c9031SAndrey Vagin list_for_each_entry(fl, head, fl_list) { 28666c8c9031SAndrey Vagin 28676c8c9031SAndrey Vagin if (filp != fl->fl_file) 28686c8c9031SAndrey Vagin continue; 28696c8c9031SAndrey Vagin if (fl->fl_owner != files && 28706c8c9031SAndrey Vagin fl->fl_owner != filp) 28716c8c9031SAndrey Vagin continue; 28726c8c9031SAndrey Vagin 28736c8c9031SAndrey Vagin (*id)++; 28746c8c9031SAndrey Vagin seq_puts(f, "lock:\t"); 2875b8da9b10SLuo Longjun lock_get_status(f, fl, *id, "", 0); 28766c8c9031SAndrey Vagin } 28776c8c9031SAndrey Vagin } 28786c8c9031SAndrey Vagin 28796c8c9031SAndrey Vagin void show_fd_locks(struct seq_file *f, 28806c8c9031SAndrey Vagin struct file *filp, struct files_struct *files) 28816c8c9031SAndrey Vagin { 2882c568d683SMiklos Szeredi struct inode *inode = locks_inode(filp); 28836c8c9031SAndrey Vagin struct file_lock_context *ctx; 28846c8c9031SAndrey Vagin int id = 0; 28856c8c9031SAndrey Vagin 2886128a3785SDmitry Vyukov ctx = smp_load_acquire(&inode->i_flctx); 28876c8c9031SAndrey Vagin if (!ctx) 28886c8c9031SAndrey Vagin return; 28896c8c9031SAndrey Vagin 28906c8c9031SAndrey Vagin spin_lock(&ctx->flc_lock); 28916c8c9031SAndrey Vagin __show_fd_locks(f, &ctx->flc_flock, &id, filp, files); 28926c8c9031SAndrey Vagin __show_fd_locks(f, &ctx->flc_posix, &id, filp, files); 28936c8c9031SAndrey Vagin __show_fd_locks(f, &ctx->flc_lease, &id, filp, files); 28946c8c9031SAndrey Vagin spin_unlock(&ctx->flc_lock); 28956c8c9031SAndrey Vagin } 28966c8c9031SAndrey Vagin 28977f8ada98SPavel Emelyanov static void *locks_start(struct seq_file *f, loff_t *pos) 2898b03dfdecSJeff Layton __acquires(&blocked_lock_lock) 28991da177e4SLinus Torvalds { 29007012b02aSJeff Layton struct locks_iterator *iter = f->private; 290199dc8292SJerome Marchand 29027012b02aSJeff Layton iter->li_pos = *pos + 1; 2903aba37660SPeter Zijlstra percpu_down_write(&file_rwsem); 29047b2296afSJeff Layton spin_lock(&blocked_lock_lock); 29057c3f654dSPeter Zijlstra return seq_hlist_start_percpu(&file_lock_list.hlist, &iter->li_cpu, *pos); 29061da177e4SLinus Torvalds } 29077f8ada98SPavel Emelyanov 29087f8ada98SPavel Emelyanov static void *locks_next(struct seq_file *f, void *v, loff_t *pos) 29097f8ada98SPavel Emelyanov { 29107012b02aSJeff Layton struct locks_iterator *iter = f->private; 29117012b02aSJeff Layton 29127012b02aSJeff Layton ++iter->li_pos; 29137c3f654dSPeter Zijlstra return seq_hlist_next_percpu(v, &file_lock_list.hlist, &iter->li_cpu, pos); 29141da177e4SLinus Torvalds } 29157f8ada98SPavel Emelyanov 29167f8ada98SPavel Emelyanov static void locks_stop(struct seq_file *f, void *v) 2917b03dfdecSJeff Layton __releases(&blocked_lock_lock) 29187f8ada98SPavel Emelyanov { 29197b2296afSJeff Layton spin_unlock(&blocked_lock_lock); 2920aba37660SPeter Zijlstra percpu_up_write(&file_rwsem); 29211da177e4SLinus Torvalds } 29221da177e4SLinus Torvalds 2923d8ba7a36SAlexey Dobriyan static const struct seq_operations locks_seq_operations = { 29247f8ada98SPavel Emelyanov .start = locks_start, 29257f8ada98SPavel Emelyanov .next = locks_next, 29267f8ada98SPavel Emelyanov .stop = locks_stop, 29277f8ada98SPavel Emelyanov .show = locks_show, 29287f8ada98SPavel Emelyanov }; 2929d8ba7a36SAlexey Dobriyan 2930d8ba7a36SAlexey Dobriyan static int __init proc_locks_init(void) 2931d8ba7a36SAlexey Dobriyan { 293244414d82SChristoph Hellwig proc_create_seq_private("locks", 0, NULL, &locks_seq_operations, 293344414d82SChristoph Hellwig sizeof(struct locks_iterator), NULL); 2934d8ba7a36SAlexey Dobriyan return 0; 2935d8ba7a36SAlexey Dobriyan } 293691899226SPaul Gortmaker fs_initcall(proc_locks_init); 29377f8ada98SPavel Emelyanov #endif 29387f8ada98SPavel Emelyanov 29391da177e4SLinus Torvalds static int __init filelock_init(void) 29401da177e4SLinus Torvalds { 29417012b02aSJeff Layton int i; 29427012b02aSJeff Layton 29434a075e39SJeff Layton flctx_cache = kmem_cache_create("file_lock_ctx", 29444a075e39SJeff Layton sizeof(struct file_lock_context), 0, SLAB_PANIC, NULL); 29454a075e39SJeff Layton 29461da177e4SLinus Torvalds filelock_cache = kmem_cache_create("file_lock_cache", 2947ee19cc40SMiklos Szeredi sizeof(struct file_lock), 0, SLAB_PANIC, NULL); 2948ee19cc40SMiklos Szeredi 29497c3f654dSPeter Zijlstra for_each_possible_cpu(i) { 29507c3f654dSPeter Zijlstra struct file_lock_list_struct *fll = per_cpu_ptr(&file_lock_list, i); 29517c3f654dSPeter Zijlstra 29527c3f654dSPeter Zijlstra spin_lock_init(&fll->lock); 29537c3f654dSPeter Zijlstra INIT_HLIST_HEAD(&fll->hlist); 29547c3f654dSPeter Zijlstra } 29557012b02aSJeff Layton 295618f6622eSJeff Layton lease_notifier_chain_init(); 29571da177e4SLinus Torvalds return 0; 29581da177e4SLinus Torvalds } 29591da177e4SLinus Torvalds core_initcall(filelock_init); 2960