11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/fs/locks.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Provide support for fcntl()'s F_GETLK, F_SETLK, and F_SETLKW calls. 51da177e4SLinus Torvalds * Doug Evans (dje@spiff.uucp), August 07, 1992 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * Deadlock detection added. 81da177e4SLinus Torvalds * FIXME: one thing isn't handled yet: 91da177e4SLinus Torvalds * - mandatory locks (requires lots of changes elsewhere) 101da177e4SLinus Torvalds * Kelly Carmichael (kelly@[142.24.8.65]), September 17, 1994. 111da177e4SLinus Torvalds * 121da177e4SLinus Torvalds * Miscellaneous edits, and a total rewrite of posix_lock_file() code. 131da177e4SLinus Torvalds * Kai Petzke (wpp@marie.physik.tu-berlin.de), 1994 141da177e4SLinus Torvalds * 151da177e4SLinus Torvalds * Converted file_lock_table to a linked list from an array, which eliminates 161da177e4SLinus Torvalds * the limits on how many active file locks are open. 171da177e4SLinus Torvalds * Chad Page (pageone@netcom.com), November 27, 1994 181da177e4SLinus Torvalds * 191da177e4SLinus Torvalds * Removed dependency on file descriptors. dup()'ed file descriptors now 201da177e4SLinus Torvalds * get the same locks as the original file descriptors, and a close() on 211da177e4SLinus Torvalds * any file descriptor removes ALL the locks on the file for the current 221da177e4SLinus Torvalds * process. Since locks still depend on the process id, locks are inherited 231da177e4SLinus Torvalds * after an exec() but not after a fork(). This agrees with POSIX, and both 241da177e4SLinus Torvalds * BSD and SVR4 practice. 251da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), February 14, 1995 261da177e4SLinus Torvalds * 271da177e4SLinus Torvalds * Scrapped free list which is redundant now that we allocate locks 281da177e4SLinus Torvalds * dynamically with kmalloc()/kfree(). 291da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), February 21, 1995 301da177e4SLinus Torvalds * 311da177e4SLinus Torvalds * Implemented two lock personalities - FL_FLOCK and FL_POSIX. 321da177e4SLinus Torvalds * 331da177e4SLinus Torvalds * FL_POSIX locks are created with calls to fcntl() and lockf() through the 341da177e4SLinus Torvalds * fcntl() system call. They have the semantics described above. 351da177e4SLinus Torvalds * 361da177e4SLinus Torvalds * FL_FLOCK locks are created with calls to flock(), through the flock() 371da177e4SLinus Torvalds * system call, which is new. Old C libraries implement flock() via fcntl() 381da177e4SLinus Torvalds * and will continue to use the old, broken implementation. 391da177e4SLinus Torvalds * 401da177e4SLinus Torvalds * FL_FLOCK locks follow the 4.4 BSD flock() semantics. They are associated 411da177e4SLinus Torvalds * with a file pointer (filp). As a result they can be shared by a parent 421da177e4SLinus Torvalds * process and its children after a fork(). They are removed when the last 431da177e4SLinus Torvalds * file descriptor referring to the file pointer is closed (unless explicitly 441da177e4SLinus Torvalds * unlocked). 451da177e4SLinus Torvalds * 461da177e4SLinus Torvalds * FL_FLOCK locks never deadlock, an existing lock is always removed before 471da177e4SLinus Torvalds * upgrading from shared to exclusive (or vice versa). When this happens 481da177e4SLinus Torvalds * any processes blocked by the current lock are woken up and allowed to 491da177e4SLinus Torvalds * run before the new lock is applied. 501da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), June 09, 1995 511da177e4SLinus Torvalds * 521da177e4SLinus Torvalds * Removed some race conditions in flock_lock_file(), marked other possible 531da177e4SLinus Torvalds * races. Just grep for FIXME to see them. 541da177e4SLinus Torvalds * Dmitry Gorodchanin (pgmdsg@ibi.com), February 09, 1996. 551da177e4SLinus Torvalds * 561da177e4SLinus Torvalds * Addressed Dmitry's concerns. Deadlock checking no longer recursive. 571da177e4SLinus Torvalds * Lock allocation changed to GFP_ATOMIC as we can't afford to sleep 581da177e4SLinus Torvalds * once we've checked for blocking and deadlocking. 591da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), April 03, 1996. 601da177e4SLinus Torvalds * 611da177e4SLinus Torvalds * Initial implementation of mandatory locks. SunOS turned out to be 621da177e4SLinus Torvalds * a rotten model, so I implemented the "obvious" semantics. 63395cf969SPaul Bolle * See 'Documentation/filesystems/mandatory-locking.txt' for details. 641da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), April 06, 1996. 651da177e4SLinus Torvalds * 661da177e4SLinus Torvalds * Don't allow mandatory locks on mmap()'ed files. Added simple functions to 671da177e4SLinus Torvalds * check if a file has mandatory locks, used by mmap(), open() and creat() to 681da177e4SLinus Torvalds * see if system call should be rejected. Ref. HP-UX/SunOS/Solaris Reference 691da177e4SLinus Torvalds * Manual, Section 2. 701da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), April 09, 1996. 711da177e4SLinus Torvalds * 721da177e4SLinus Torvalds * Tidied up block list handling. Added '/proc/locks' interface. 731da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), April 24, 1996. 741da177e4SLinus Torvalds * 751da177e4SLinus Torvalds * Fixed deadlock condition for pathological code that mixes calls to 761da177e4SLinus Torvalds * flock() and fcntl(). 771da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), April 29, 1996. 781da177e4SLinus Torvalds * 791da177e4SLinus Torvalds * Allow only one type of locking scheme (FL_POSIX or FL_FLOCK) to be in use 801da177e4SLinus Torvalds * for a given file at a time. Changed the CONFIG_LOCK_MANDATORY scheme to 811da177e4SLinus Torvalds * guarantee sensible behaviour in the case where file system modules might 821da177e4SLinus Torvalds * be compiled with different options than the kernel itself. 831da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), May 15, 1996. 841da177e4SLinus Torvalds * 851da177e4SLinus Torvalds * Added a couple of missing wake_up() calls. Thanks to Thomas Meckel 861da177e4SLinus Torvalds * (Thomas.Meckel@mni.fh-giessen.de) for spotting this. 871da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), May 15, 1996. 881da177e4SLinus Torvalds * 891da177e4SLinus Torvalds * Changed FL_POSIX locks to use the block list in the same way as FL_FLOCK 901da177e4SLinus Torvalds * locks. Changed process synchronisation to avoid dereferencing locks that 911da177e4SLinus Torvalds * have already been freed. 921da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), Sep 21, 1996. 931da177e4SLinus Torvalds * 941da177e4SLinus Torvalds * Made the block list a circular list to minimise searching in the list. 951da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), Sep 25, 1996. 961da177e4SLinus Torvalds * 971da177e4SLinus Torvalds * Made mandatory locking a mount option. Default is not to allow mandatory 981da177e4SLinus Torvalds * locking. 991da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), Oct 04, 1996. 1001da177e4SLinus Torvalds * 1011da177e4SLinus Torvalds * Some adaptations for NFS support. 1021da177e4SLinus Torvalds * Olaf Kirch (okir@monad.swb.de), Dec 1996, 1031da177e4SLinus Torvalds * 1041da177e4SLinus Torvalds * Fixed /proc/locks interface so that we can't overrun the buffer we are handed. 1051da177e4SLinus Torvalds * Andy Walker (andy@lysaker.kvaerner.no), May 12, 1997. 1061da177e4SLinus Torvalds * 1071da177e4SLinus Torvalds * Use slab allocator instead of kmalloc/kfree. 1081da177e4SLinus Torvalds * Use generic list implementation from <linux/list.h>. 1091da177e4SLinus Torvalds * Sped up posix_locks_deadlock by only considering blocked locks. 1101da177e4SLinus Torvalds * Matthew Wilcox <willy@debian.org>, March, 2000. 1111da177e4SLinus Torvalds * 1121da177e4SLinus Torvalds * Leases and LOCK_MAND 1131da177e4SLinus Torvalds * Matthew Wilcox <willy@debian.org>, June, 2000. 1141da177e4SLinus Torvalds * Stephen Rothwell <sfr@canb.auug.org.au>, June, 2000. 1151da177e4SLinus Torvalds */ 1161da177e4SLinus Torvalds 1171da177e4SLinus Torvalds #include <linux/capability.h> 1181da177e4SLinus Torvalds #include <linux/file.h> 1199f3acc31SAl Viro #include <linux/fdtable.h> 1201da177e4SLinus Torvalds #include <linux/fs.h> 1211da177e4SLinus Torvalds #include <linux/init.h> 1221da177e4SLinus Torvalds #include <linux/module.h> 1231da177e4SLinus Torvalds #include <linux/security.h> 1241da177e4SLinus Torvalds #include <linux/slab.h> 1251da177e4SLinus Torvalds #include <linux/syscalls.h> 1261da177e4SLinus Torvalds #include <linux/time.h> 1274fb3a538SDipankar Sarma #include <linux/rcupdate.h> 128ab1f1611SVitaliy Gusev #include <linux/pid_namespace.h> 12948f74186SJeff Layton #include <linux/hashtable.h> 1307012b02aSJeff Layton #include <linux/percpu.h> 1317012b02aSJeff Layton #include <linux/lglock.h> 1321da177e4SLinus Torvalds 13362af4f1fSJeff Layton #define CREATE_TRACE_POINTS 13462af4f1fSJeff Layton #include <trace/events/filelock.h> 13562af4f1fSJeff Layton 1361da177e4SLinus Torvalds #include <asm/uaccess.h> 1371da177e4SLinus Torvalds 1381da177e4SLinus Torvalds #define IS_POSIX(fl) (fl->fl_flags & FL_POSIX) 1391da177e4SLinus Torvalds #define IS_FLOCK(fl) (fl->fl_flags & FL_FLOCK) 14011afe9f7SChristoph Hellwig #define IS_LEASE(fl) (fl->fl_flags & (FL_LEASE|FL_DELEG|FL_LAYOUT)) 141cff2fce5SJeff Layton #define IS_OFDLCK(fl) (fl->fl_flags & FL_OFDLCK) 1421da177e4SLinus Torvalds 143ab83fa4bSJ. Bruce Fields static bool lease_breaking(struct file_lock *fl) 144ab83fa4bSJ. Bruce Fields { 145778fc546SJ. Bruce Fields return fl->fl_flags & (FL_UNLOCK_PENDING | FL_DOWNGRADE_PENDING); 146778fc546SJ. Bruce Fields } 147778fc546SJ. Bruce Fields 148778fc546SJ. Bruce Fields static int target_leasetype(struct file_lock *fl) 149778fc546SJ. Bruce Fields { 150778fc546SJ. Bruce Fields if (fl->fl_flags & FL_UNLOCK_PENDING) 151778fc546SJ. Bruce Fields return F_UNLCK; 152778fc546SJ. Bruce Fields if (fl->fl_flags & FL_DOWNGRADE_PENDING) 153778fc546SJ. Bruce Fields return F_RDLCK; 154778fc546SJ. Bruce Fields return fl->fl_type; 155ab83fa4bSJ. Bruce Fields } 156ab83fa4bSJ. Bruce Fields 1571da177e4SLinus Torvalds int leases_enable = 1; 1581da177e4SLinus Torvalds int lease_break_time = 45; 1591da177e4SLinus Torvalds 1601c8c601aSJeff Layton /* 1617012b02aSJeff Layton * The global file_lock_list is only used for displaying /proc/locks, so we 1627012b02aSJeff Layton * keep a list on each CPU, with each list protected by its own spinlock via 1637012b02aSJeff Layton * the file_lock_lglock. Note that alterations to the list also require that 1646109c850SJeff Layton * the relevant flc_lock is held. 1651c8c601aSJeff Layton */ 1667012b02aSJeff Layton DEFINE_STATIC_LGLOCK(file_lock_lglock); 1677012b02aSJeff Layton static DEFINE_PER_CPU(struct hlist_head, file_lock_list); 16888974691SJeff Layton 1691c8c601aSJeff Layton /* 17048f74186SJeff Layton * The blocked_hash is used to find POSIX lock loops for deadlock detection. 1717b2296afSJeff Layton * It is protected by blocked_lock_lock. 17248f74186SJeff Layton * 17348f74186SJeff Layton * We hash locks by lockowner in order to optimize searching for the lock a 17448f74186SJeff Layton * particular lockowner is waiting on. 17548f74186SJeff Layton * 17648f74186SJeff Layton * FIXME: make this value scale via some heuristic? We generally will want more 17748f74186SJeff Layton * buckets when we have more lockowners holding locks, but that's a little 17848f74186SJeff Layton * difficult to determine without knowing what the workload will look like. 1791c8c601aSJeff Layton */ 18048f74186SJeff Layton #define BLOCKED_HASH_BITS 7 18148f74186SJeff Layton static DEFINE_HASHTABLE(blocked_hash, BLOCKED_HASH_BITS); 18288974691SJeff Layton 1831c8c601aSJeff Layton /* 1847b2296afSJeff Layton * This lock protects the blocked_hash. Generally, if you're accessing it, you 1857b2296afSJeff Layton * want to be holding this lock. 1861c8c601aSJeff Layton * 1871c8c601aSJeff Layton * In addition, it also protects the fl->fl_block list, and the fl->fl_next 1881c8c601aSJeff Layton * pointer for file_lock structures that are acting as lock requests (in 1891c8c601aSJeff Layton * contrast to those that are acting as records of acquired locks). 1901c8c601aSJeff Layton * 1911c8c601aSJeff Layton * Note that when we acquire this lock in order to change the above fields, 1926109c850SJeff Layton * we often hold the flc_lock as well. In certain cases, when reading the fields 1931c8c601aSJeff Layton * protected by this lock, we can skip acquiring it iff we already hold the 1946109c850SJeff Layton * flc_lock. 1951c8c601aSJeff Layton * 1961c8c601aSJeff Layton * In particular, adding an entry to the fl_block list requires that you hold 1976109c850SJeff Layton * both the flc_lock and the blocked_lock_lock (acquired in that order). 1986109c850SJeff Layton * Deleting an entry from the list however only requires the file_lock_lock. 1991c8c601aSJeff Layton */ 2007b2296afSJeff Layton static DEFINE_SPINLOCK(blocked_lock_lock); 2011da177e4SLinus Torvalds 2024a075e39SJeff Layton static struct kmem_cache *flctx_cache __read_mostly; 203e18b890bSChristoph Lameter static struct kmem_cache *filelock_cache __read_mostly; 2041da177e4SLinus Torvalds 2054a075e39SJeff Layton static struct file_lock_context * 2064a075e39SJeff Layton locks_get_lock_context(struct inode *inode) 2074a075e39SJeff Layton { 2084a075e39SJeff Layton struct file_lock_context *new; 2094a075e39SJeff Layton 2104a075e39SJeff Layton if (likely(inode->i_flctx)) 2114a075e39SJeff Layton goto out; 2124a075e39SJeff Layton 2134a075e39SJeff Layton new = kmem_cache_alloc(flctx_cache, GFP_KERNEL); 2144a075e39SJeff Layton if (!new) 2154a075e39SJeff Layton goto out; 2164a075e39SJeff Layton 2176109c850SJeff Layton spin_lock_init(&new->flc_lock); 2184a075e39SJeff Layton INIT_LIST_HEAD(&new->flc_flock); 219bd61e0a9SJeff Layton INIT_LIST_HEAD(&new->flc_posix); 2208634b51fSJeff Layton INIT_LIST_HEAD(&new->flc_lease); 2214a075e39SJeff Layton 2224a075e39SJeff Layton /* 2234a075e39SJeff Layton * Assign the pointer if it's not already assigned. If it is, then 2244a075e39SJeff Layton * free the context we just allocated. 2254a075e39SJeff Layton */ 2264a075e39SJeff Layton spin_lock(&inode->i_lock); 2274a075e39SJeff Layton if (likely(!inode->i_flctx)) { 2284a075e39SJeff Layton inode->i_flctx = new; 2294a075e39SJeff Layton new = NULL; 2304a075e39SJeff Layton } 2314a075e39SJeff Layton spin_unlock(&inode->i_lock); 2324a075e39SJeff Layton 2334a075e39SJeff Layton if (new) 2344a075e39SJeff Layton kmem_cache_free(flctx_cache, new); 2354a075e39SJeff Layton out: 2364a075e39SJeff Layton return inode->i_flctx; 2374a075e39SJeff Layton } 2384a075e39SJeff Layton 2394a075e39SJeff Layton void 2404a075e39SJeff Layton locks_free_lock_context(struct file_lock_context *ctx) 2414a075e39SJeff Layton { 2424a075e39SJeff Layton if (ctx) { 2434a075e39SJeff Layton WARN_ON_ONCE(!list_empty(&ctx->flc_flock)); 244bd61e0a9SJeff Layton WARN_ON_ONCE(!list_empty(&ctx->flc_posix)); 2458634b51fSJeff Layton WARN_ON_ONCE(!list_empty(&ctx->flc_lease)); 2464a075e39SJeff Layton kmem_cache_free(flctx_cache, ctx); 2474a075e39SJeff Layton } 2484a075e39SJeff Layton } 2494a075e39SJeff Layton 250ee19cc40SMiklos Szeredi static void locks_init_lock_heads(struct file_lock *fl) 251a51cb91dSMiklos Szeredi { 252139ca04eSJeff Layton INIT_HLIST_NODE(&fl->fl_link); 2536dee60f6SJeff Layton INIT_LIST_HEAD(&fl->fl_list); 254ee19cc40SMiklos Szeredi INIT_LIST_HEAD(&fl->fl_block); 255ee19cc40SMiklos Szeredi init_waitqueue_head(&fl->fl_wait); 256a51cb91dSMiklos Szeredi } 257a51cb91dSMiklos Szeredi 2581da177e4SLinus Torvalds /* Allocate an empty lock structure. */ 259c5b1f0d9SArnd Bergmann struct file_lock *locks_alloc_lock(void) 2601da177e4SLinus Torvalds { 261ee19cc40SMiklos Szeredi struct file_lock *fl = kmem_cache_zalloc(filelock_cache, GFP_KERNEL); 262a51cb91dSMiklos Szeredi 263a51cb91dSMiklos Szeredi if (fl) 264ee19cc40SMiklos Szeredi locks_init_lock_heads(fl); 265a51cb91dSMiklos Szeredi 266a51cb91dSMiklos Szeredi return fl; 2671da177e4SLinus Torvalds } 268c5b1f0d9SArnd Bergmann EXPORT_SYMBOL_GPL(locks_alloc_lock); 2691da177e4SLinus Torvalds 270a9e61e25SFelix Blyakher void locks_release_private(struct file_lock *fl) 27147831f35STrond Myklebust { 27247831f35STrond Myklebust if (fl->fl_ops) { 27347831f35STrond Myklebust if (fl->fl_ops->fl_release_private) 27447831f35STrond Myklebust fl->fl_ops->fl_release_private(fl); 27547831f35STrond Myklebust fl->fl_ops = NULL; 27647831f35STrond Myklebust } 27747831f35STrond Myklebust 2785c97d7b1SKinglong Mee if (fl->fl_lmops) { 2795c97d7b1SKinglong Mee if (fl->fl_lmops->lm_put_owner) 2805c97d7b1SKinglong Mee fl->fl_lmops->lm_put_owner(fl); 2815c97d7b1SKinglong Mee fl->fl_lmops = NULL; 2825c97d7b1SKinglong Mee } 28347831f35STrond Myklebust } 284a9e61e25SFelix Blyakher EXPORT_SYMBOL_GPL(locks_release_private); 28547831f35STrond Myklebust 2861da177e4SLinus Torvalds /* Free a lock which is not in use. */ 28705fa3135SJ. Bruce Fields void locks_free_lock(struct file_lock *fl) 2881da177e4SLinus Torvalds { 2895ce29646SMiklos Szeredi BUG_ON(waitqueue_active(&fl->fl_wait)); 2906dee60f6SJeff Layton BUG_ON(!list_empty(&fl->fl_list)); 2915ce29646SMiklos Szeredi BUG_ON(!list_empty(&fl->fl_block)); 292139ca04eSJeff Layton BUG_ON(!hlist_unhashed(&fl->fl_link)); 2931da177e4SLinus Torvalds 29447831f35STrond Myklebust locks_release_private(fl); 2951da177e4SLinus Torvalds kmem_cache_free(filelock_cache, fl); 2961da177e4SLinus Torvalds } 29705fa3135SJ. Bruce Fields EXPORT_SYMBOL(locks_free_lock); 2981da177e4SLinus Torvalds 299ed9814d8SJeff Layton static void 300ed9814d8SJeff Layton locks_dispose_list(struct list_head *dispose) 301ed9814d8SJeff Layton { 302ed9814d8SJeff Layton struct file_lock *fl; 303ed9814d8SJeff Layton 304ed9814d8SJeff Layton while (!list_empty(dispose)) { 3056dee60f6SJeff Layton fl = list_first_entry(dispose, struct file_lock, fl_list); 3066dee60f6SJeff Layton list_del_init(&fl->fl_list); 307ed9814d8SJeff Layton locks_free_lock(fl); 308ed9814d8SJeff Layton } 309ed9814d8SJeff Layton } 310ed9814d8SJeff Layton 3111da177e4SLinus Torvalds void locks_init_lock(struct file_lock *fl) 3121da177e4SLinus Torvalds { 313ee19cc40SMiklos Szeredi memset(fl, 0, sizeof(struct file_lock)); 314ee19cc40SMiklos Szeredi locks_init_lock_heads(fl); 3151da177e4SLinus Torvalds } 3161da177e4SLinus Torvalds 3171da177e4SLinus Torvalds EXPORT_SYMBOL(locks_init_lock); 3181da177e4SLinus Torvalds 3191da177e4SLinus Torvalds /* 3201da177e4SLinus Torvalds * Initialize a new lock from an existing file_lock structure. 3211da177e4SLinus Torvalds */ 3223fe0fff1SKinglong Mee void locks_copy_conflock(struct file_lock *new, struct file_lock *fl) 3231da177e4SLinus Torvalds { 3241da177e4SLinus Torvalds new->fl_owner = fl->fl_owner; 3251da177e4SLinus Torvalds new->fl_pid = fl->fl_pid; 3260996905fSTrond Myklebust new->fl_file = NULL; 3271da177e4SLinus Torvalds new->fl_flags = fl->fl_flags; 3281da177e4SLinus Torvalds new->fl_type = fl->fl_type; 3291da177e4SLinus Torvalds new->fl_start = fl->fl_start; 3301da177e4SLinus Torvalds new->fl_end = fl->fl_end; 331f328296eSKinglong Mee new->fl_lmops = fl->fl_lmops; 3320996905fSTrond Myklebust new->fl_ops = NULL; 333f328296eSKinglong Mee 334f328296eSKinglong Mee if (fl->fl_lmops) { 335f328296eSKinglong Mee if (fl->fl_lmops->lm_get_owner) 336f328296eSKinglong Mee fl->fl_lmops->lm_get_owner(new, fl); 337f328296eSKinglong Mee } 3380996905fSTrond Myklebust } 3393fe0fff1SKinglong Mee EXPORT_SYMBOL(locks_copy_conflock); 3400996905fSTrond Myklebust 3410996905fSTrond Myklebust void locks_copy_lock(struct file_lock *new, struct file_lock *fl) 3420996905fSTrond Myklebust { 343566709bdSJeff Layton /* "new" must be a freshly-initialized lock */ 344566709bdSJeff Layton WARN_ON_ONCE(new->fl_ops); 3450996905fSTrond Myklebust 3463fe0fff1SKinglong Mee locks_copy_conflock(new, fl); 347f328296eSKinglong Mee 3480996905fSTrond Myklebust new->fl_file = fl->fl_file; 3491da177e4SLinus Torvalds new->fl_ops = fl->fl_ops; 35047831f35STrond Myklebust 351f328296eSKinglong Mee if (fl->fl_ops) { 352f328296eSKinglong Mee if (fl->fl_ops->fl_copy_lock) 353f328296eSKinglong Mee fl->fl_ops->fl_copy_lock(new, fl); 354f328296eSKinglong Mee } 3551da177e4SLinus Torvalds } 3561da177e4SLinus Torvalds 3571da177e4SLinus Torvalds EXPORT_SYMBOL(locks_copy_lock); 3581da177e4SLinus Torvalds 3591da177e4SLinus Torvalds static inline int flock_translate_cmd(int cmd) { 3601da177e4SLinus Torvalds if (cmd & LOCK_MAND) 3611da177e4SLinus Torvalds return cmd & (LOCK_MAND | LOCK_RW); 3621da177e4SLinus Torvalds switch (cmd) { 3631da177e4SLinus Torvalds case LOCK_SH: 3641da177e4SLinus Torvalds return F_RDLCK; 3651da177e4SLinus Torvalds case LOCK_EX: 3661da177e4SLinus Torvalds return F_WRLCK; 3671da177e4SLinus Torvalds case LOCK_UN: 3681da177e4SLinus Torvalds return F_UNLCK; 3691da177e4SLinus Torvalds } 3701da177e4SLinus Torvalds return -EINVAL; 3711da177e4SLinus Torvalds } 3721da177e4SLinus Torvalds 3731da177e4SLinus Torvalds /* Fill in a file_lock structure with an appropriate FLOCK lock. */ 3746e129d00SJeff Layton static struct file_lock * 3756e129d00SJeff Layton flock_make_lock(struct file *filp, unsigned int cmd) 3761da177e4SLinus Torvalds { 3771da177e4SLinus Torvalds struct file_lock *fl; 3781da177e4SLinus Torvalds int type = flock_translate_cmd(cmd); 3796e129d00SJeff Layton 3801da177e4SLinus Torvalds if (type < 0) 3816e129d00SJeff Layton return ERR_PTR(type); 3821da177e4SLinus Torvalds 3831da177e4SLinus Torvalds fl = locks_alloc_lock(); 3841da177e4SLinus Torvalds if (fl == NULL) 3856e129d00SJeff Layton return ERR_PTR(-ENOMEM); 3861da177e4SLinus Torvalds 3871da177e4SLinus Torvalds fl->fl_file = filp; 38873a8f5f7SChristoph Hellwig fl->fl_owner = filp; 3891da177e4SLinus Torvalds fl->fl_pid = current->tgid; 3901da177e4SLinus Torvalds fl->fl_flags = FL_FLOCK; 3911da177e4SLinus Torvalds fl->fl_type = type; 3921da177e4SLinus Torvalds fl->fl_end = OFFSET_MAX; 3931da177e4SLinus Torvalds 3946e129d00SJeff Layton return fl; 3951da177e4SLinus Torvalds } 3961da177e4SLinus Torvalds 3970ec4f431SJ. Bruce Fields static int assign_type(struct file_lock *fl, long type) 3981da177e4SLinus Torvalds { 3991da177e4SLinus Torvalds switch (type) { 4001da177e4SLinus Torvalds case F_RDLCK: 4011da177e4SLinus Torvalds case F_WRLCK: 4021da177e4SLinus Torvalds case F_UNLCK: 4031da177e4SLinus Torvalds fl->fl_type = type; 4041da177e4SLinus Torvalds break; 4051da177e4SLinus Torvalds default: 4061da177e4SLinus Torvalds return -EINVAL; 4071da177e4SLinus Torvalds } 4081da177e4SLinus Torvalds return 0; 4091da177e4SLinus Torvalds } 4101da177e4SLinus Torvalds 411ef12e72aSJ. Bruce Fields static int flock64_to_posix_lock(struct file *filp, struct file_lock *fl, 412ef12e72aSJ. Bruce Fields struct flock64 *l) 413ef12e72aSJ. Bruce Fields { 414ef12e72aSJ. Bruce Fields switch (l->l_whence) { 415ef12e72aSJ. Bruce Fields case SEEK_SET: 416ef12e72aSJ. Bruce Fields fl->fl_start = 0; 417ef12e72aSJ. Bruce Fields break; 418ef12e72aSJ. Bruce Fields case SEEK_CUR: 419ef12e72aSJ. Bruce Fields fl->fl_start = filp->f_pos; 420ef12e72aSJ. Bruce Fields break; 421ef12e72aSJ. Bruce Fields case SEEK_END: 422ef12e72aSJ. Bruce Fields fl->fl_start = i_size_read(file_inode(filp)); 423ef12e72aSJ. Bruce Fields break; 424ef12e72aSJ. Bruce Fields default: 425ef12e72aSJ. Bruce Fields return -EINVAL; 426ef12e72aSJ. Bruce Fields } 427ef12e72aSJ. Bruce Fields if (l->l_start > OFFSET_MAX - fl->fl_start) 428ef12e72aSJ. Bruce Fields return -EOVERFLOW; 429ef12e72aSJ. Bruce Fields fl->fl_start += l->l_start; 430ef12e72aSJ. Bruce Fields if (fl->fl_start < 0) 431ef12e72aSJ. Bruce Fields return -EINVAL; 432ef12e72aSJ. Bruce Fields 433ef12e72aSJ. Bruce Fields /* POSIX-1996 leaves the case l->l_len < 0 undefined; 434ef12e72aSJ. Bruce Fields POSIX-2001 defines it. */ 435ef12e72aSJ. Bruce Fields if (l->l_len > 0) { 436ef12e72aSJ. Bruce Fields if (l->l_len - 1 > OFFSET_MAX - fl->fl_start) 437ef12e72aSJ. Bruce Fields return -EOVERFLOW; 438ef12e72aSJ. Bruce Fields fl->fl_end = fl->fl_start + l->l_len - 1; 439ef12e72aSJ. Bruce Fields 440ef12e72aSJ. Bruce Fields } else if (l->l_len < 0) { 441ef12e72aSJ. Bruce Fields if (fl->fl_start + l->l_len < 0) 442ef12e72aSJ. Bruce Fields return -EINVAL; 443ef12e72aSJ. Bruce Fields fl->fl_end = fl->fl_start - 1; 444ef12e72aSJ. Bruce Fields fl->fl_start += l->l_len; 445ef12e72aSJ. Bruce Fields } else 446ef12e72aSJ. Bruce Fields fl->fl_end = OFFSET_MAX; 447ef12e72aSJ. Bruce Fields 448ef12e72aSJ. Bruce Fields fl->fl_owner = current->files; 449ef12e72aSJ. Bruce Fields fl->fl_pid = current->tgid; 450ef12e72aSJ. Bruce Fields fl->fl_file = filp; 451ef12e72aSJ. Bruce Fields fl->fl_flags = FL_POSIX; 452ef12e72aSJ. Bruce Fields fl->fl_ops = NULL; 453ef12e72aSJ. Bruce Fields fl->fl_lmops = NULL; 454ef12e72aSJ. Bruce Fields 455ef12e72aSJ. Bruce Fields return assign_type(fl, l->l_type); 456ef12e72aSJ. Bruce Fields } 457ef12e72aSJ. Bruce Fields 4581da177e4SLinus Torvalds /* Verify a "struct flock" and copy it to a "struct file_lock" as a POSIX 4591da177e4SLinus Torvalds * style lock. 4601da177e4SLinus Torvalds */ 4611da177e4SLinus Torvalds static int flock_to_posix_lock(struct file *filp, struct file_lock *fl, 4621da177e4SLinus Torvalds struct flock *l) 4631da177e4SLinus Torvalds { 464ef12e72aSJ. Bruce Fields struct flock64 ll = { 465ef12e72aSJ. Bruce Fields .l_type = l->l_type, 466ef12e72aSJ. Bruce Fields .l_whence = l->l_whence, 467ef12e72aSJ. Bruce Fields .l_start = l->l_start, 468ef12e72aSJ. Bruce Fields .l_len = l->l_len, 469ef12e72aSJ. Bruce Fields }; 4701da177e4SLinus Torvalds 471ef12e72aSJ. Bruce Fields return flock64_to_posix_lock(filp, fl, &ll); 4721da177e4SLinus Torvalds } 4731da177e4SLinus Torvalds 4741da177e4SLinus Torvalds /* default lease lock manager operations */ 4754d01b7f5SJeff Layton static bool 4764d01b7f5SJeff Layton lease_break_callback(struct file_lock *fl) 4771da177e4SLinus Torvalds { 4781da177e4SLinus Torvalds kill_fasync(&fl->fl_fasync, SIGIO, POLL_MSG); 4794d01b7f5SJeff Layton return false; 4801da177e4SLinus Torvalds } 4811da177e4SLinus Torvalds 4821c7dd2ffSJeff Layton static void 4831c7dd2ffSJeff Layton lease_setup(struct file_lock *fl, void **priv) 4841c7dd2ffSJeff Layton { 4851c7dd2ffSJeff Layton struct file *filp = fl->fl_file; 4861c7dd2ffSJeff Layton struct fasync_struct *fa = *priv; 4871c7dd2ffSJeff Layton 4881c7dd2ffSJeff Layton /* 4891c7dd2ffSJeff Layton * fasync_insert_entry() returns the old entry if any. If there was no 4901c7dd2ffSJeff Layton * old entry, then it used "priv" and inserted it into the fasync list. 4911c7dd2ffSJeff Layton * Clear the pointer to indicate that it shouldn't be freed. 4921c7dd2ffSJeff Layton */ 4931c7dd2ffSJeff Layton if (!fasync_insert_entry(fa->fa_fd, filp, &fl->fl_fasync, fa)) 4941c7dd2ffSJeff Layton *priv = NULL; 4951c7dd2ffSJeff Layton 4961c7dd2ffSJeff Layton __f_setown(filp, task_pid(current), PIDTYPE_PID, 0); 4971c7dd2ffSJeff Layton } 4981c7dd2ffSJeff Layton 4997b021967SAlexey Dobriyan static const struct lock_manager_operations lease_manager_ops = { 5008fb47a4fSJ. Bruce Fields .lm_break = lease_break_callback, 5018fb47a4fSJ. Bruce Fields .lm_change = lease_modify, 5021c7dd2ffSJeff Layton .lm_setup = lease_setup, 5031da177e4SLinus Torvalds }; 5041da177e4SLinus Torvalds 5051da177e4SLinus Torvalds /* 5061da177e4SLinus Torvalds * Initialize a lease, use the default lock manager operations 5071da177e4SLinus Torvalds */ 5080ec4f431SJ. Bruce Fields static int lease_init(struct file *filp, long type, struct file_lock *fl) 5091da177e4SLinus Torvalds { 51075dff55aSTrond Myklebust if (assign_type(fl, type) != 0) 51175dff55aSTrond Myklebust return -EINVAL; 51275dff55aSTrond Myklebust 5137ca76311SJeff Layton fl->fl_owner = filp; 5141da177e4SLinus Torvalds fl->fl_pid = current->tgid; 5151da177e4SLinus Torvalds 5161da177e4SLinus Torvalds fl->fl_file = filp; 5171da177e4SLinus Torvalds fl->fl_flags = FL_LEASE; 5181da177e4SLinus Torvalds fl->fl_start = 0; 5191da177e4SLinus Torvalds fl->fl_end = OFFSET_MAX; 5201da177e4SLinus Torvalds fl->fl_ops = NULL; 5211da177e4SLinus Torvalds fl->fl_lmops = &lease_manager_ops; 5221da177e4SLinus Torvalds return 0; 5231da177e4SLinus Torvalds } 5241da177e4SLinus Torvalds 5251da177e4SLinus Torvalds /* Allocate a file_lock initialised to this type of lease */ 5260ec4f431SJ. Bruce Fields static struct file_lock *lease_alloc(struct file *filp, long type) 5271da177e4SLinus Torvalds { 5281da177e4SLinus Torvalds struct file_lock *fl = locks_alloc_lock(); 52975dff55aSTrond Myklebust int error = -ENOMEM; 5301da177e4SLinus Torvalds 5311da177e4SLinus Torvalds if (fl == NULL) 532e32b8ee2SJ. Bruce Fields return ERR_PTR(error); 5331da177e4SLinus Torvalds 5341da177e4SLinus Torvalds error = lease_init(filp, type, fl); 53575dff55aSTrond Myklebust if (error) { 53675dff55aSTrond Myklebust locks_free_lock(fl); 537e32b8ee2SJ. Bruce Fields return ERR_PTR(error); 53875dff55aSTrond Myklebust } 539e32b8ee2SJ. Bruce Fields return fl; 5401da177e4SLinus Torvalds } 5411da177e4SLinus Torvalds 5421da177e4SLinus Torvalds /* Check if two locks overlap each other. 5431da177e4SLinus Torvalds */ 5441da177e4SLinus Torvalds static inline int locks_overlap(struct file_lock *fl1, struct file_lock *fl2) 5451da177e4SLinus Torvalds { 5461da177e4SLinus Torvalds return ((fl1->fl_end >= fl2->fl_start) && 5471da177e4SLinus Torvalds (fl2->fl_end >= fl1->fl_start)); 5481da177e4SLinus Torvalds } 5491da177e4SLinus Torvalds 5501da177e4SLinus Torvalds /* 5511da177e4SLinus Torvalds * Check whether two locks have the same owner. 5521da177e4SLinus Torvalds */ 55333443c42SMatt Mackall static int posix_same_owner(struct file_lock *fl1, struct file_lock *fl2) 5541da177e4SLinus Torvalds { 5558fb47a4fSJ. Bruce Fields if (fl1->fl_lmops && fl1->fl_lmops->lm_compare_owner) 5561da177e4SLinus Torvalds return fl2->fl_lmops == fl1->fl_lmops && 5578fb47a4fSJ. Bruce Fields fl1->fl_lmops->lm_compare_owner(fl1, fl2); 5581da177e4SLinus Torvalds return fl1->fl_owner == fl2->fl_owner; 5591da177e4SLinus Torvalds } 5601da177e4SLinus Torvalds 5616109c850SJeff Layton /* Must be called with the flc_lock held! */ 5626ca10ed8SJeff Layton static void locks_insert_global_locks(struct file_lock *fl) 56388974691SJeff Layton { 5647012b02aSJeff Layton lg_local_lock(&file_lock_lglock); 5657012b02aSJeff Layton fl->fl_link_cpu = smp_processor_id(); 5667012b02aSJeff Layton hlist_add_head(&fl->fl_link, this_cpu_ptr(&file_lock_list)); 5677012b02aSJeff Layton lg_local_unlock(&file_lock_lglock); 56888974691SJeff Layton } 56988974691SJeff Layton 5706109c850SJeff Layton /* Must be called with the flc_lock held! */ 5716ca10ed8SJeff Layton static void locks_delete_global_locks(struct file_lock *fl) 57288974691SJeff Layton { 5737012b02aSJeff Layton /* 5747012b02aSJeff Layton * Avoid taking lock if already unhashed. This is safe since this check 5756109c850SJeff Layton * is done while holding the flc_lock, and new insertions into the list 5767012b02aSJeff Layton * also require that it be held. 5777012b02aSJeff Layton */ 5787012b02aSJeff Layton if (hlist_unhashed(&fl->fl_link)) 5797012b02aSJeff Layton return; 5807012b02aSJeff Layton lg_local_lock_cpu(&file_lock_lglock, fl->fl_link_cpu); 581139ca04eSJeff Layton hlist_del_init(&fl->fl_link); 5827012b02aSJeff Layton lg_local_unlock_cpu(&file_lock_lglock, fl->fl_link_cpu); 58388974691SJeff Layton } 58488974691SJeff Layton 5853999e493SJeff Layton static unsigned long 5863999e493SJeff Layton posix_owner_key(struct file_lock *fl) 5873999e493SJeff Layton { 5883999e493SJeff Layton if (fl->fl_lmops && fl->fl_lmops->lm_owner_key) 5893999e493SJeff Layton return fl->fl_lmops->lm_owner_key(fl); 5903999e493SJeff Layton return (unsigned long)fl->fl_owner; 5913999e493SJeff Layton } 5923999e493SJeff Layton 5936ca10ed8SJeff Layton static void locks_insert_global_blocked(struct file_lock *waiter) 59488974691SJeff Layton { 595*663d5af7SDaniel Wagner lockdep_assert_held(&blocked_lock_lock); 596*663d5af7SDaniel Wagner 5973999e493SJeff Layton hash_add(blocked_hash, &waiter->fl_link, posix_owner_key(waiter)); 59888974691SJeff Layton } 59988974691SJeff Layton 6006ca10ed8SJeff Layton static void locks_delete_global_blocked(struct file_lock *waiter) 60188974691SJeff Layton { 602*663d5af7SDaniel Wagner lockdep_assert_held(&blocked_lock_lock); 603*663d5af7SDaniel Wagner 60448f74186SJeff Layton hash_del(&waiter->fl_link); 60588974691SJeff Layton } 60688974691SJeff Layton 6071da177e4SLinus Torvalds /* Remove waiter from blocker's block list. 6081da177e4SLinus Torvalds * When blocker ends up pointing to itself then the list is empty. 6091c8c601aSJeff Layton * 6107b2296afSJeff Layton * Must be called with blocked_lock_lock held. 6111da177e4SLinus Torvalds */ 61233443c42SMatt Mackall static void __locks_delete_block(struct file_lock *waiter) 6131da177e4SLinus Torvalds { 61488974691SJeff Layton locks_delete_global_blocked(waiter); 6151da177e4SLinus Torvalds list_del_init(&waiter->fl_block); 6161da177e4SLinus Torvalds waiter->fl_next = NULL; 6171da177e4SLinus Torvalds } 6181da177e4SLinus Torvalds 6191a9e64a7SJeff Layton static void locks_delete_block(struct file_lock *waiter) 6201da177e4SLinus Torvalds { 6217b2296afSJeff Layton spin_lock(&blocked_lock_lock); 6221da177e4SLinus Torvalds __locks_delete_block(waiter); 6237b2296afSJeff Layton spin_unlock(&blocked_lock_lock); 6241da177e4SLinus Torvalds } 6251da177e4SLinus Torvalds 6261da177e4SLinus Torvalds /* Insert waiter into blocker's block list. 6271da177e4SLinus Torvalds * We use a circular list so that processes can be easily woken up in 6281da177e4SLinus Torvalds * the order they blocked. The documentation doesn't require this but 6291da177e4SLinus Torvalds * it seems like the reasonable thing to do. 6301c8c601aSJeff Layton * 6316109c850SJeff Layton * Must be called with both the flc_lock and blocked_lock_lock held. The 6326109c850SJeff Layton * fl_block list itself is protected by the blocked_lock_lock, but by ensuring 6336109c850SJeff Layton * that the flc_lock is also held on insertions we can avoid taking the 6346109c850SJeff Layton * blocked_lock_lock in some cases when we see that the fl_block list is empty. 6351da177e4SLinus Torvalds */ 6361c8c601aSJeff Layton static void __locks_insert_block(struct file_lock *blocker, 6371da177e4SLinus Torvalds struct file_lock *waiter) 6381da177e4SLinus Torvalds { 6396dc0fe8fSJ. Bruce Fields BUG_ON(!list_empty(&waiter->fl_block)); 6401da177e4SLinus Torvalds waiter->fl_next = blocker; 64188974691SJeff Layton list_add_tail(&waiter->fl_block, &blocker->fl_block); 642cff2fce5SJeff Layton if (IS_POSIX(blocker) && !IS_OFDLCK(blocker)) 6431c8c601aSJeff Layton locks_insert_global_blocked(waiter); 6441c8c601aSJeff Layton } 6451c8c601aSJeff Layton 6466109c850SJeff Layton /* Must be called with flc_lock held. */ 6471c8c601aSJeff Layton static void locks_insert_block(struct file_lock *blocker, 6481c8c601aSJeff Layton struct file_lock *waiter) 6491c8c601aSJeff Layton { 6507b2296afSJeff Layton spin_lock(&blocked_lock_lock); 6511c8c601aSJeff Layton __locks_insert_block(blocker, waiter); 6527b2296afSJeff Layton spin_unlock(&blocked_lock_lock); 6531da177e4SLinus Torvalds } 6541da177e4SLinus Torvalds 6551cb36012SJeff Layton /* 6561cb36012SJeff Layton * Wake up processes blocked waiting for blocker. 6571cb36012SJeff Layton * 6586109c850SJeff Layton * Must be called with the inode->flc_lock held! 6591da177e4SLinus Torvalds */ 6601da177e4SLinus Torvalds static void locks_wake_up_blocks(struct file_lock *blocker) 6611da177e4SLinus Torvalds { 6624e8c765dSJeff Layton /* 6634e8c765dSJeff Layton * Avoid taking global lock if list is empty. This is safe since new 6646109c850SJeff Layton * blocked requests are only added to the list under the flc_lock, and 6656109c850SJeff Layton * the flc_lock is always held here. Note that removal from the fl_block 6666109c850SJeff Layton * list does not require the flc_lock, so we must recheck list_empty() 6677b2296afSJeff Layton * after acquiring the blocked_lock_lock. 6684e8c765dSJeff Layton */ 6694e8c765dSJeff Layton if (list_empty(&blocker->fl_block)) 6704e8c765dSJeff Layton return; 6714e8c765dSJeff Layton 6727b2296afSJeff Layton spin_lock(&blocked_lock_lock); 6731da177e4SLinus Torvalds while (!list_empty(&blocker->fl_block)) { 674f0c1cd0eSPavel Emelyanov struct file_lock *waiter; 675f0c1cd0eSPavel Emelyanov 676f0c1cd0eSPavel Emelyanov waiter = list_first_entry(&blocker->fl_block, 6771da177e4SLinus Torvalds struct file_lock, fl_block); 6781da177e4SLinus Torvalds __locks_delete_block(waiter); 6798fb47a4fSJ. Bruce Fields if (waiter->fl_lmops && waiter->fl_lmops->lm_notify) 6808fb47a4fSJ. Bruce Fields waiter->fl_lmops->lm_notify(waiter); 6811da177e4SLinus Torvalds else 6821da177e4SLinus Torvalds wake_up(&waiter->fl_wait); 6831da177e4SLinus Torvalds } 6847b2296afSJeff Layton spin_unlock(&blocked_lock_lock); 6851da177e4SLinus Torvalds } 6861da177e4SLinus Torvalds 6875263e31eSJeff Layton static void 688e084c1bdSJeff Layton locks_insert_lock_ctx(struct file_lock *fl, struct list_head *before) 6895263e31eSJeff Layton { 6905263e31eSJeff Layton fl->fl_nspid = get_pid(task_tgid(current)); 6915263e31eSJeff Layton list_add_tail(&fl->fl_list, before); 6925263e31eSJeff Layton locks_insert_global_locks(fl); 6935263e31eSJeff Layton } 6945263e31eSJeff Layton 6958634b51fSJeff Layton static void 696e084c1bdSJeff Layton locks_unlink_lock_ctx(struct file_lock *fl) 6971da177e4SLinus Torvalds { 69888974691SJeff Layton locks_delete_global_locks(fl); 6998634b51fSJeff Layton list_del_init(&fl->fl_list); 700ab1f1611SVitaliy Gusev if (fl->fl_nspid) { 701ab1f1611SVitaliy Gusev put_pid(fl->fl_nspid); 702ab1f1611SVitaliy Gusev fl->fl_nspid = NULL; 703ab1f1611SVitaliy Gusev } 7041da177e4SLinus Torvalds locks_wake_up_blocks(fl); 70524cbe784SJeff Layton } 70624cbe784SJeff Layton 7075263e31eSJeff Layton static void 708e084c1bdSJeff Layton locks_delete_lock_ctx(struct file_lock *fl, struct list_head *dispose) 7095263e31eSJeff Layton { 710e084c1bdSJeff Layton locks_unlink_lock_ctx(fl); 7118634b51fSJeff Layton if (dispose) 7128634b51fSJeff Layton list_add(&fl->fl_list, dispose); 7138634b51fSJeff Layton else 7148634b51fSJeff Layton locks_free_lock(fl); 7155263e31eSJeff Layton } 7165263e31eSJeff Layton 7171da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. Common functionality 7181da177e4SLinus Torvalds * checks for shared/exclusive status of overlapping locks. 7191da177e4SLinus Torvalds */ 7201da177e4SLinus Torvalds static int locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl) 7211da177e4SLinus Torvalds { 7221da177e4SLinus Torvalds if (sys_fl->fl_type == F_WRLCK) 7231da177e4SLinus Torvalds return 1; 7241da177e4SLinus Torvalds if (caller_fl->fl_type == F_WRLCK) 7251da177e4SLinus Torvalds return 1; 7261da177e4SLinus Torvalds return 0; 7271da177e4SLinus Torvalds } 7281da177e4SLinus Torvalds 7291da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. POSIX specific 7301da177e4SLinus Torvalds * checking before calling the locks_conflict(). 7311da177e4SLinus Torvalds */ 7321da177e4SLinus Torvalds static int posix_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl) 7331da177e4SLinus Torvalds { 7341da177e4SLinus Torvalds /* POSIX locks owned by the same process do not conflict with 7351da177e4SLinus Torvalds * each other. 7361da177e4SLinus Torvalds */ 7379b8c8695SJeff Layton if (posix_same_owner(caller_fl, sys_fl)) 7381da177e4SLinus Torvalds return (0); 7391da177e4SLinus Torvalds 7401da177e4SLinus Torvalds /* Check whether they overlap */ 7411da177e4SLinus Torvalds if (!locks_overlap(caller_fl, sys_fl)) 7421da177e4SLinus Torvalds return 0; 7431da177e4SLinus Torvalds 7441da177e4SLinus Torvalds return (locks_conflict(caller_fl, sys_fl)); 7451da177e4SLinus Torvalds } 7461da177e4SLinus Torvalds 7471da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. FLOCK specific 7481da177e4SLinus Torvalds * checking before calling the locks_conflict(). 7491da177e4SLinus Torvalds */ 7501da177e4SLinus Torvalds static int flock_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl) 7511da177e4SLinus Torvalds { 7521da177e4SLinus Torvalds /* FLOCK locks referring to the same filp do not conflict with 7531da177e4SLinus Torvalds * each other. 7541da177e4SLinus Torvalds */ 7559b8c8695SJeff Layton if (caller_fl->fl_file == sys_fl->fl_file) 7561da177e4SLinus Torvalds return (0); 7571da177e4SLinus Torvalds if ((caller_fl->fl_type & LOCK_MAND) || (sys_fl->fl_type & LOCK_MAND)) 7581da177e4SLinus Torvalds return 0; 7591da177e4SLinus Torvalds 7601da177e4SLinus Torvalds return (locks_conflict(caller_fl, sys_fl)); 7611da177e4SLinus Torvalds } 7621da177e4SLinus Torvalds 7636d34ac19SJ. Bruce Fields void 7649d6a8c5cSMarc Eshel posix_test_lock(struct file *filp, struct file_lock *fl) 7651da177e4SLinus Torvalds { 7661da177e4SLinus Torvalds struct file_lock *cfl; 767bd61e0a9SJeff Layton struct file_lock_context *ctx; 7681c8c601aSJeff Layton struct inode *inode = file_inode(filp); 7691da177e4SLinus Torvalds 770bd61e0a9SJeff Layton ctx = inode->i_flctx; 771bd61e0a9SJeff Layton if (!ctx || list_empty_careful(&ctx->flc_posix)) { 772bd61e0a9SJeff Layton fl->fl_type = F_UNLCK; 773bd61e0a9SJeff Layton return; 7741da177e4SLinus Torvalds } 775bd61e0a9SJeff Layton 7766109c850SJeff Layton spin_lock(&ctx->flc_lock); 777bd61e0a9SJeff Layton list_for_each_entry(cfl, &ctx->flc_posix, fl_list) { 778bd61e0a9SJeff Layton if (posix_locks_conflict(fl, cfl)) { 7793fe0fff1SKinglong Mee locks_copy_conflock(fl, cfl); 780ab1f1611SVitaliy Gusev if (cfl->fl_nspid) 7816c5f3e7bSPavel Emelyanov fl->fl_pid = pid_vnr(cfl->fl_nspid); 782bd61e0a9SJeff Layton goto out; 783bd61e0a9SJeff Layton } 784bd61e0a9SJeff Layton } 785129a84deSJ. Bruce Fields fl->fl_type = F_UNLCK; 786bd61e0a9SJeff Layton out: 7876109c850SJeff Layton spin_unlock(&ctx->flc_lock); 7886d34ac19SJ. Bruce Fields return; 7891da177e4SLinus Torvalds } 7901da177e4SLinus Torvalds EXPORT_SYMBOL(posix_test_lock); 7911da177e4SLinus Torvalds 792b533184fSJ. Bruce Fields /* 793b533184fSJ. Bruce Fields * Deadlock detection: 7941da177e4SLinus Torvalds * 795b533184fSJ. Bruce Fields * We attempt to detect deadlocks that are due purely to posix file 796b533184fSJ. Bruce Fields * locks. 7971da177e4SLinus Torvalds * 798b533184fSJ. Bruce Fields * We assume that a task can be waiting for at most one lock at a time. 799b533184fSJ. Bruce Fields * So for any acquired lock, the process holding that lock may be 800b533184fSJ. Bruce Fields * waiting on at most one other lock. That lock in turns may be held by 801b533184fSJ. Bruce Fields * someone waiting for at most one other lock. Given a requested lock 802b533184fSJ. Bruce Fields * caller_fl which is about to wait for a conflicting lock block_fl, we 803b533184fSJ. Bruce Fields * follow this chain of waiters to ensure we are not about to create a 804b533184fSJ. Bruce Fields * cycle. 80597855b49SJ. Bruce Fields * 806b533184fSJ. Bruce Fields * Since we do this before we ever put a process to sleep on a lock, we 807b533184fSJ. Bruce Fields * are ensured that there is never a cycle; that is what guarantees that 808b533184fSJ. Bruce Fields * the while() loop in posix_locks_deadlock() eventually completes. 809b533184fSJ. Bruce Fields * 810b533184fSJ. Bruce Fields * Note: the above assumption may not be true when handling lock 811b533184fSJ. Bruce Fields * requests from a broken NFS client. It may also fail in the presence 812b533184fSJ. Bruce Fields * of tasks (such as posix threads) sharing the same open file table. 813b533184fSJ. Bruce Fields * To handle those cases, we just bail out after a few iterations. 81457b65325SJeff Layton * 815cff2fce5SJeff Layton * For FL_OFDLCK locks, the owner is the filp, not the files_struct. 81657b65325SJeff Layton * Because the owner is not even nominally tied to a thread of 81757b65325SJeff Layton * execution, the deadlock detection below can't reasonably work well. Just 81857b65325SJeff Layton * skip it for those. 81957b65325SJeff Layton * 820cff2fce5SJeff Layton * In principle, we could do a more limited deadlock detection on FL_OFDLCK 82157b65325SJeff Layton * locks that just checks for the case where two tasks are attempting to 82257b65325SJeff Layton * upgrade from read to write locks on the same inode. 8231da177e4SLinus Torvalds */ 82497855b49SJ. Bruce Fields 82597855b49SJ. Bruce Fields #define MAX_DEADLK_ITERATIONS 10 82697855b49SJ. Bruce Fields 827b533184fSJ. Bruce Fields /* Find a lock that the owner of the given block_fl is blocking on. */ 828b533184fSJ. Bruce Fields static struct file_lock *what_owner_is_waiting_for(struct file_lock *block_fl) 829b533184fSJ. Bruce Fields { 830b533184fSJ. Bruce Fields struct file_lock *fl; 831b533184fSJ. Bruce Fields 8323999e493SJeff Layton hash_for_each_possible(blocked_hash, fl, fl_link, posix_owner_key(block_fl)) { 833b533184fSJ. Bruce Fields if (posix_same_owner(fl, block_fl)) 834b533184fSJ. Bruce Fields return fl->fl_next; 835b533184fSJ. Bruce Fields } 836b533184fSJ. Bruce Fields return NULL; 837b533184fSJ. Bruce Fields } 838b533184fSJ. Bruce Fields 8397b2296afSJeff Layton /* Must be called with the blocked_lock_lock held! */ 840b0904e14SAdrian Bunk static int posix_locks_deadlock(struct file_lock *caller_fl, 8411da177e4SLinus Torvalds struct file_lock *block_fl) 8421da177e4SLinus Torvalds { 84397855b49SJ. Bruce Fields int i = 0; 8441da177e4SLinus Torvalds 845*663d5af7SDaniel Wagner lockdep_assert_held(&blocked_lock_lock); 846*663d5af7SDaniel Wagner 84757b65325SJeff Layton /* 84857b65325SJeff Layton * This deadlock detector can't reasonably detect deadlocks with 849cff2fce5SJeff Layton * FL_OFDLCK locks, since they aren't owned by a process, per-se. 85057b65325SJeff Layton */ 851cff2fce5SJeff Layton if (IS_OFDLCK(caller_fl)) 85257b65325SJeff Layton return 0; 85357b65325SJeff Layton 854b533184fSJ. Bruce Fields while ((block_fl = what_owner_is_waiting_for(block_fl))) { 85597855b49SJ. Bruce Fields if (i++ > MAX_DEADLK_ITERATIONS) 85697855b49SJ. Bruce Fields return 0; 857b533184fSJ. Bruce Fields if (posix_same_owner(caller_fl, block_fl)) 858b533184fSJ. Bruce Fields return 1; 8591da177e4SLinus Torvalds } 8601da177e4SLinus Torvalds return 0; 8611da177e4SLinus Torvalds } 8621da177e4SLinus Torvalds 8631da177e4SLinus Torvalds /* Try to create a FLOCK lock on filp. We always insert new FLOCK locks 86402888f41SJ. Bruce Fields * after any leases, but before any posix locks. 865f475ae95STrond Myklebust * 866f475ae95STrond Myklebust * Note that if called with an FL_EXISTS argument, the caller may determine 867f475ae95STrond Myklebust * whether or not a lock was successfully freed by testing the return 868f475ae95STrond Myklebust * value for -ENOENT. 8691da177e4SLinus Torvalds */ 870993dfa87STrond Myklebust static int flock_lock_file(struct file *filp, struct file_lock *request) 8711da177e4SLinus Torvalds { 872993dfa87STrond Myklebust struct file_lock *new_fl = NULL; 8735263e31eSJeff Layton struct file_lock *fl; 8745263e31eSJeff Layton struct file_lock_context *ctx; 875496ad9aaSAl Viro struct inode *inode = file_inode(filp); 8761da177e4SLinus Torvalds int error = 0; 8775263e31eSJeff Layton bool found = false; 878ed9814d8SJeff Layton LIST_HEAD(dispose); 8791da177e4SLinus Torvalds 8805263e31eSJeff Layton ctx = locks_get_lock_context(inode); 8815263e31eSJeff Layton if (!ctx) 8825263e31eSJeff Layton return -ENOMEM; 8835263e31eSJeff Layton 884b89f4321SArnd Bergmann if (!(request->fl_flags & FL_ACCESS) && (request->fl_type != F_UNLCK)) { 885b89f4321SArnd Bergmann new_fl = locks_alloc_lock(); 886b89f4321SArnd Bergmann if (!new_fl) 887b89f4321SArnd Bergmann return -ENOMEM; 888b89f4321SArnd Bergmann } 889b89f4321SArnd Bergmann 8906109c850SJeff Layton spin_lock(&ctx->flc_lock); 891f07f18ddSTrond Myklebust if (request->fl_flags & FL_ACCESS) 892f07f18ddSTrond Myklebust goto find_conflict; 89384d535adSPavel Emelyanov 8945263e31eSJeff Layton list_for_each_entry(fl, &ctx->flc_flock, fl_list) { 8951da177e4SLinus Torvalds if (filp != fl->fl_file) 8961da177e4SLinus Torvalds continue; 897993dfa87STrond Myklebust if (request->fl_type == fl->fl_type) 8981da177e4SLinus Torvalds goto out; 8995263e31eSJeff Layton found = true; 900e084c1bdSJeff Layton locks_delete_lock_ctx(fl, &dispose); 9011da177e4SLinus Torvalds break; 9021da177e4SLinus Torvalds } 9031da177e4SLinus Torvalds 904f475ae95STrond Myklebust if (request->fl_type == F_UNLCK) { 905f475ae95STrond Myklebust if ((request->fl_flags & FL_EXISTS) && !found) 906f475ae95STrond Myklebust error = -ENOENT; 907993dfa87STrond Myklebust goto out; 908f475ae95STrond Myklebust } 9091da177e4SLinus Torvalds 910f07f18ddSTrond Myklebust find_conflict: 9115263e31eSJeff Layton list_for_each_entry(fl, &ctx->flc_flock, fl_list) { 912993dfa87STrond Myklebust if (!flock_locks_conflict(request, fl)) 9131da177e4SLinus Torvalds continue; 9141da177e4SLinus Torvalds error = -EAGAIN; 915bde74e4bSMiklos Szeredi if (!(request->fl_flags & FL_SLEEP)) 916bde74e4bSMiklos Szeredi goto out; 917bde74e4bSMiklos Szeredi error = FILE_LOCK_DEFERRED; 918993dfa87STrond Myklebust locks_insert_block(fl, request); 9191da177e4SLinus Torvalds goto out; 9201da177e4SLinus Torvalds } 921f07f18ddSTrond Myklebust if (request->fl_flags & FL_ACCESS) 922f07f18ddSTrond Myklebust goto out; 923993dfa87STrond Myklebust locks_copy_lock(new_fl, request); 924e084c1bdSJeff Layton locks_insert_lock_ctx(new_fl, &ctx->flc_flock); 925993dfa87STrond Myklebust new_fl = NULL; 9269cedc194SKirill Korotaev error = 0; 9271da177e4SLinus Torvalds 9281da177e4SLinus Torvalds out: 9296109c850SJeff Layton spin_unlock(&ctx->flc_lock); 930993dfa87STrond Myklebust if (new_fl) 931993dfa87STrond Myklebust locks_free_lock(new_fl); 932ed9814d8SJeff Layton locks_dispose_list(&dispose); 9331da177e4SLinus Torvalds return error; 9341da177e4SLinus Torvalds } 9351da177e4SLinus Torvalds 936150b3934SMarc Eshel static int __posix_lock_file(struct inode *inode, struct file_lock *request, struct file_lock *conflock) 9371da177e4SLinus Torvalds { 938bd61e0a9SJeff Layton struct file_lock *fl, *tmp; 93939005d02SMiklos Szeredi struct file_lock *new_fl = NULL; 94039005d02SMiklos Szeredi struct file_lock *new_fl2 = NULL; 9411da177e4SLinus Torvalds struct file_lock *left = NULL; 9421da177e4SLinus Torvalds struct file_lock *right = NULL; 943bd61e0a9SJeff Layton struct file_lock_context *ctx; 944b9746ef8SJeff Layton int error; 945b9746ef8SJeff Layton bool added = false; 946ed9814d8SJeff Layton LIST_HEAD(dispose); 9471da177e4SLinus Torvalds 948bd61e0a9SJeff Layton ctx = locks_get_lock_context(inode); 949bd61e0a9SJeff Layton if (!ctx) 950bd61e0a9SJeff Layton return -ENOMEM; 951bd61e0a9SJeff Layton 9521da177e4SLinus Torvalds /* 9531da177e4SLinus Torvalds * We may need two file_lock structures for this operation, 9541da177e4SLinus Torvalds * so we get them in advance to avoid races. 95539005d02SMiklos Szeredi * 95639005d02SMiklos Szeredi * In some cases we can be sure, that no new locks will be needed 9571da177e4SLinus Torvalds */ 95839005d02SMiklos Szeredi if (!(request->fl_flags & FL_ACCESS) && 95939005d02SMiklos Szeredi (request->fl_type != F_UNLCK || 96039005d02SMiklos Szeredi request->fl_start != 0 || request->fl_end != OFFSET_MAX)) { 9611da177e4SLinus Torvalds new_fl = locks_alloc_lock(); 9621da177e4SLinus Torvalds new_fl2 = locks_alloc_lock(); 96339005d02SMiklos Szeredi } 9641da177e4SLinus Torvalds 9656109c850SJeff Layton spin_lock(&ctx->flc_lock); 9661cb36012SJeff Layton /* 9671cb36012SJeff Layton * New lock request. Walk all POSIX locks and look for conflicts. If 9681cb36012SJeff Layton * there are any, either return error or put the request on the 96948f74186SJeff Layton * blocker's list of waiters and the global blocked_hash. 9701cb36012SJeff Layton */ 9711da177e4SLinus Torvalds if (request->fl_type != F_UNLCK) { 972bd61e0a9SJeff Layton list_for_each_entry(fl, &ctx->flc_posix, fl_list) { 9731da177e4SLinus Torvalds if (!posix_locks_conflict(request, fl)) 9741da177e4SLinus Torvalds continue; 9755842add2SAndy Adamson if (conflock) 9763fe0fff1SKinglong Mee locks_copy_conflock(conflock, fl); 9771da177e4SLinus Torvalds error = -EAGAIN; 9781da177e4SLinus Torvalds if (!(request->fl_flags & FL_SLEEP)) 9791da177e4SLinus Torvalds goto out; 9801c8c601aSJeff Layton /* 9811c8c601aSJeff Layton * Deadlock detection and insertion into the blocked 9821c8c601aSJeff Layton * locks list must be done while holding the same lock! 9831c8c601aSJeff Layton */ 9841da177e4SLinus Torvalds error = -EDEADLK; 9857b2296afSJeff Layton spin_lock(&blocked_lock_lock); 9861c8c601aSJeff Layton if (likely(!posix_locks_deadlock(request, fl))) { 987bde74e4bSMiklos Szeredi error = FILE_LOCK_DEFERRED; 9881c8c601aSJeff Layton __locks_insert_block(fl, request); 9891c8c601aSJeff Layton } 9907b2296afSJeff Layton spin_unlock(&blocked_lock_lock); 9911da177e4SLinus Torvalds goto out; 9921da177e4SLinus Torvalds } 9931da177e4SLinus Torvalds } 9941da177e4SLinus Torvalds 9951da177e4SLinus Torvalds /* If we're just looking for a conflict, we're done. */ 9961da177e4SLinus Torvalds error = 0; 9971da177e4SLinus Torvalds if (request->fl_flags & FL_ACCESS) 9981da177e4SLinus Torvalds goto out; 9991da177e4SLinus Torvalds 1000bd61e0a9SJeff Layton /* Find the first old lock with the same owner as the new lock */ 1001bd61e0a9SJeff Layton list_for_each_entry(fl, &ctx->flc_posix, fl_list) { 1002bd61e0a9SJeff Layton if (posix_same_owner(request, fl)) 1003bd61e0a9SJeff Layton break; 10041da177e4SLinus Torvalds } 10051da177e4SLinus Torvalds 10061da177e4SLinus Torvalds /* Process locks with this owner. */ 1007bd61e0a9SJeff Layton list_for_each_entry_safe_from(fl, tmp, &ctx->flc_posix, fl_list) { 1008bd61e0a9SJeff Layton if (!posix_same_owner(request, fl)) 1009bd61e0a9SJeff Layton break; 1010bd61e0a9SJeff Layton 1011bd61e0a9SJeff Layton /* Detect adjacent or overlapping regions (if same lock type) */ 10121da177e4SLinus Torvalds if (request->fl_type == fl->fl_type) { 1013449231d6SOlaf Kirch /* In all comparisons of start vs end, use 1014449231d6SOlaf Kirch * "start - 1" rather than "end + 1". If end 1015449231d6SOlaf Kirch * is OFFSET_MAX, end + 1 will become negative. 1016449231d6SOlaf Kirch */ 10171da177e4SLinus Torvalds if (fl->fl_end < request->fl_start - 1) 1018bd61e0a9SJeff Layton continue; 10191da177e4SLinus Torvalds /* If the next lock in the list has entirely bigger 10201da177e4SLinus Torvalds * addresses than the new one, insert the lock here. 10211da177e4SLinus Torvalds */ 1022449231d6SOlaf Kirch if (fl->fl_start - 1 > request->fl_end) 10231da177e4SLinus Torvalds break; 10241da177e4SLinus Torvalds 10251da177e4SLinus Torvalds /* If we come here, the new and old lock are of the 10261da177e4SLinus Torvalds * same type and adjacent or overlapping. Make one 10271da177e4SLinus Torvalds * lock yielding from the lower start address of both 10281da177e4SLinus Torvalds * locks to the higher end address. 10291da177e4SLinus Torvalds */ 10301da177e4SLinus Torvalds if (fl->fl_start > request->fl_start) 10311da177e4SLinus Torvalds fl->fl_start = request->fl_start; 10321da177e4SLinus Torvalds else 10331da177e4SLinus Torvalds request->fl_start = fl->fl_start; 10341da177e4SLinus Torvalds if (fl->fl_end < request->fl_end) 10351da177e4SLinus Torvalds fl->fl_end = request->fl_end; 10361da177e4SLinus Torvalds else 10371da177e4SLinus Torvalds request->fl_end = fl->fl_end; 10381da177e4SLinus Torvalds if (added) { 1039e084c1bdSJeff Layton locks_delete_lock_ctx(fl, &dispose); 10401da177e4SLinus Torvalds continue; 10411da177e4SLinus Torvalds } 10421da177e4SLinus Torvalds request = fl; 1043b9746ef8SJeff Layton added = true; 1044bd61e0a9SJeff Layton } else { 10451da177e4SLinus Torvalds /* Processing for different lock types is a bit 10461da177e4SLinus Torvalds * more complex. 10471da177e4SLinus Torvalds */ 10481da177e4SLinus Torvalds if (fl->fl_end < request->fl_start) 1049bd61e0a9SJeff Layton continue; 10501da177e4SLinus Torvalds if (fl->fl_start > request->fl_end) 10511da177e4SLinus Torvalds break; 10521da177e4SLinus Torvalds if (request->fl_type == F_UNLCK) 1053b9746ef8SJeff Layton added = true; 10541da177e4SLinus Torvalds if (fl->fl_start < request->fl_start) 10551da177e4SLinus Torvalds left = fl; 10561da177e4SLinus Torvalds /* If the next lock in the list has a higher end 10571da177e4SLinus Torvalds * address than the new one, insert the new one here. 10581da177e4SLinus Torvalds */ 10591da177e4SLinus Torvalds if (fl->fl_end > request->fl_end) { 10601da177e4SLinus Torvalds right = fl; 10611da177e4SLinus Torvalds break; 10621da177e4SLinus Torvalds } 10631da177e4SLinus Torvalds if (fl->fl_start >= request->fl_start) { 10641da177e4SLinus Torvalds /* The new lock completely replaces an old 10651da177e4SLinus Torvalds * one (This may happen several times). 10661da177e4SLinus Torvalds */ 10671da177e4SLinus Torvalds if (added) { 1068e084c1bdSJeff Layton locks_delete_lock_ctx(fl, &dispose); 10691da177e4SLinus Torvalds continue; 10701da177e4SLinus Torvalds } 1071b84d49f9SJeff Layton /* 1072b84d49f9SJeff Layton * Replace the old lock with new_fl, and 1073b84d49f9SJeff Layton * remove the old one. It's safe to do the 1074b84d49f9SJeff Layton * insert here since we know that we won't be 1075b84d49f9SJeff Layton * using new_fl later, and that the lock is 1076b84d49f9SJeff Layton * just replacing an existing lock. 10771da177e4SLinus Torvalds */ 1078b84d49f9SJeff Layton error = -ENOLCK; 1079b84d49f9SJeff Layton if (!new_fl) 1080b84d49f9SJeff Layton goto out; 1081b84d49f9SJeff Layton locks_copy_lock(new_fl, request); 1082b84d49f9SJeff Layton request = new_fl; 1083b84d49f9SJeff Layton new_fl = NULL; 1084e084c1bdSJeff Layton locks_insert_lock_ctx(request, &fl->fl_list); 1085e084c1bdSJeff Layton locks_delete_lock_ctx(fl, &dispose); 1086b9746ef8SJeff Layton added = true; 10871da177e4SLinus Torvalds } 10881da177e4SLinus Torvalds } 10891da177e4SLinus Torvalds } 10901da177e4SLinus Torvalds 10910d9a490aSMiklos Szeredi /* 10921cb36012SJeff Layton * The above code only modifies existing locks in case of merging or 10931cb36012SJeff Layton * replacing. If new lock(s) need to be inserted all modifications are 10941cb36012SJeff Layton * done below this, so it's safe yet to bail out. 10950d9a490aSMiklos Szeredi */ 10960d9a490aSMiklos Szeredi error = -ENOLCK; /* "no luck" */ 10970d9a490aSMiklos Szeredi if (right && left == right && !new_fl2) 10980d9a490aSMiklos Szeredi goto out; 10990d9a490aSMiklos Szeredi 11001da177e4SLinus Torvalds error = 0; 11011da177e4SLinus Torvalds if (!added) { 1102f475ae95STrond Myklebust if (request->fl_type == F_UNLCK) { 1103f475ae95STrond Myklebust if (request->fl_flags & FL_EXISTS) 1104f475ae95STrond Myklebust error = -ENOENT; 11051da177e4SLinus Torvalds goto out; 1106f475ae95STrond Myklebust } 11070d9a490aSMiklos Szeredi 11080d9a490aSMiklos Szeredi if (!new_fl) { 11090d9a490aSMiklos Szeredi error = -ENOLCK; 11100d9a490aSMiklos Szeredi goto out; 11110d9a490aSMiklos Szeredi } 11121da177e4SLinus Torvalds locks_copy_lock(new_fl, request); 1113e084c1bdSJeff Layton locks_insert_lock_ctx(new_fl, &fl->fl_list); 11142e2f756fSJeff Layton fl = new_fl; 11151da177e4SLinus Torvalds new_fl = NULL; 11161da177e4SLinus Torvalds } 11171da177e4SLinus Torvalds if (right) { 11181da177e4SLinus Torvalds if (left == right) { 11191da177e4SLinus Torvalds /* The new lock breaks the old one in two pieces, 11201da177e4SLinus Torvalds * so we have to use the second new lock. 11211da177e4SLinus Torvalds */ 11221da177e4SLinus Torvalds left = new_fl2; 11231da177e4SLinus Torvalds new_fl2 = NULL; 11241da177e4SLinus Torvalds locks_copy_lock(left, right); 1125e084c1bdSJeff Layton locks_insert_lock_ctx(left, &fl->fl_list); 11261da177e4SLinus Torvalds } 11271da177e4SLinus Torvalds right->fl_start = request->fl_end + 1; 11281da177e4SLinus Torvalds locks_wake_up_blocks(right); 11291da177e4SLinus Torvalds } 11301da177e4SLinus Torvalds if (left) { 11311da177e4SLinus Torvalds left->fl_end = request->fl_start - 1; 11321da177e4SLinus Torvalds locks_wake_up_blocks(left); 11331da177e4SLinus Torvalds } 11341da177e4SLinus Torvalds out: 11356109c850SJeff Layton spin_unlock(&ctx->flc_lock); 11361da177e4SLinus Torvalds /* 11371da177e4SLinus Torvalds * Free any unused locks. 11381da177e4SLinus Torvalds */ 11391da177e4SLinus Torvalds if (new_fl) 11401da177e4SLinus Torvalds locks_free_lock(new_fl); 11411da177e4SLinus Torvalds if (new_fl2) 11421da177e4SLinus Torvalds locks_free_lock(new_fl2); 1143ed9814d8SJeff Layton locks_dispose_list(&dispose); 11441da177e4SLinus Torvalds return error; 11451da177e4SLinus Torvalds } 11461da177e4SLinus Torvalds 11471da177e4SLinus Torvalds /** 11481da177e4SLinus Torvalds * posix_lock_file - Apply a POSIX-style lock to a file 11491da177e4SLinus Torvalds * @filp: The file to apply the lock to 11501da177e4SLinus Torvalds * @fl: The lock to be applied 1151150b3934SMarc Eshel * @conflock: Place to return a copy of the conflicting lock, if found. 11521da177e4SLinus Torvalds * 11531da177e4SLinus Torvalds * Add a POSIX style lock to a file. 11541da177e4SLinus Torvalds * We merge adjacent & overlapping locks whenever possible. 11551da177e4SLinus Torvalds * POSIX locks are sorted by owner task, then by starting address 1156f475ae95STrond Myklebust * 1157f475ae95STrond Myklebust * Note that if called with an FL_EXISTS argument, the caller may determine 1158f475ae95STrond Myklebust * whether or not a lock was successfully freed by testing the return 1159f475ae95STrond Myklebust * value for -ENOENT. 11601da177e4SLinus Torvalds */ 1161150b3934SMarc Eshel int posix_lock_file(struct file *filp, struct file_lock *fl, 11625842add2SAndy Adamson struct file_lock *conflock) 11635842add2SAndy Adamson { 1164496ad9aaSAl Viro return __posix_lock_file(file_inode(filp), fl, conflock); 11655842add2SAndy Adamson } 1166150b3934SMarc Eshel EXPORT_SYMBOL(posix_lock_file); 11671da177e4SLinus Torvalds 11681da177e4SLinus Torvalds /** 11691da177e4SLinus Torvalds * posix_lock_file_wait - Apply a POSIX-style lock to a file 11701da177e4SLinus Torvalds * @filp: The file to apply the lock to 11711da177e4SLinus Torvalds * @fl: The lock to be applied 11721da177e4SLinus Torvalds * 11731da177e4SLinus Torvalds * Add a POSIX style lock to a file. 11741da177e4SLinus Torvalds * We merge adjacent & overlapping locks whenever possible. 11751da177e4SLinus Torvalds * POSIX locks are sorted by owner task, then by starting address 11761da177e4SLinus Torvalds */ 11771da177e4SLinus Torvalds int posix_lock_file_wait(struct file *filp, struct file_lock *fl) 11781da177e4SLinus Torvalds { 11791da177e4SLinus Torvalds int error; 11801da177e4SLinus Torvalds might_sleep (); 11811da177e4SLinus Torvalds for (;;) { 1182150b3934SMarc Eshel error = posix_lock_file(filp, fl, NULL); 1183bde74e4bSMiklos Szeredi if (error != FILE_LOCK_DEFERRED) 11841da177e4SLinus Torvalds break; 11851da177e4SLinus Torvalds error = wait_event_interruptible(fl->fl_wait, !fl->fl_next); 11861da177e4SLinus Torvalds if (!error) 11871da177e4SLinus Torvalds continue; 11881da177e4SLinus Torvalds 11891da177e4SLinus Torvalds locks_delete_block(fl); 11901da177e4SLinus Torvalds break; 11911da177e4SLinus Torvalds } 11921da177e4SLinus Torvalds return error; 11931da177e4SLinus Torvalds } 11941da177e4SLinus Torvalds EXPORT_SYMBOL(posix_lock_file_wait); 11951da177e4SLinus Torvalds 11961da177e4SLinus Torvalds /** 11971da177e4SLinus Torvalds * locks_mandatory_locked - Check for an active lock 1198d7a06983SJeff Layton * @file: the file to check 11991da177e4SLinus Torvalds * 12001da177e4SLinus Torvalds * Searches the inode's list of locks to find any POSIX locks which conflict. 12011da177e4SLinus Torvalds * This function is called from locks_verify_locked() only. 12021da177e4SLinus Torvalds */ 1203d7a06983SJeff Layton int locks_mandatory_locked(struct file *file) 12041da177e4SLinus Torvalds { 1205bd61e0a9SJeff Layton int ret; 1206d7a06983SJeff Layton struct inode *inode = file_inode(file); 1207bd61e0a9SJeff Layton struct file_lock_context *ctx; 12081da177e4SLinus Torvalds struct file_lock *fl; 12091da177e4SLinus Torvalds 1210bd61e0a9SJeff Layton ctx = inode->i_flctx; 1211bd61e0a9SJeff Layton if (!ctx || list_empty_careful(&ctx->flc_posix)) 1212bd61e0a9SJeff Layton return 0; 1213bd61e0a9SJeff Layton 12141da177e4SLinus Torvalds /* 12151da177e4SLinus Torvalds * Search the lock list for this inode for any POSIX locks. 12161da177e4SLinus Torvalds */ 12176109c850SJeff Layton spin_lock(&ctx->flc_lock); 1218bd61e0a9SJeff Layton ret = 0; 1219bd61e0a9SJeff Layton list_for_each_entry(fl, &ctx->flc_posix, fl_list) { 122073a8f5f7SChristoph Hellwig if (fl->fl_owner != current->files && 1221bd61e0a9SJeff Layton fl->fl_owner != file) { 1222bd61e0a9SJeff Layton ret = -EAGAIN; 12231da177e4SLinus Torvalds break; 12241da177e4SLinus Torvalds } 1225bd61e0a9SJeff Layton } 12266109c850SJeff Layton spin_unlock(&ctx->flc_lock); 1227bd61e0a9SJeff Layton return ret; 12281da177e4SLinus Torvalds } 12291da177e4SLinus Torvalds 12301da177e4SLinus Torvalds /** 12311da177e4SLinus Torvalds * locks_mandatory_area - Check for a conflicting lock 12321da177e4SLinus Torvalds * @read_write: %FLOCK_VERIFY_WRITE for exclusive access, %FLOCK_VERIFY_READ 12331da177e4SLinus Torvalds * for shared 12341da177e4SLinus Torvalds * @inode: the file to check 12351da177e4SLinus Torvalds * @filp: how the file was opened (if it was) 12361da177e4SLinus Torvalds * @offset: start of area to check 12371da177e4SLinus Torvalds * @count: length of area to check 12381da177e4SLinus Torvalds * 12391da177e4SLinus Torvalds * Searches the inode's list of locks to find any POSIX locks which conflict. 12401da177e4SLinus Torvalds * This function is called from rw_verify_area() and 12411da177e4SLinus Torvalds * locks_verify_truncate(). 12421da177e4SLinus Torvalds */ 12431da177e4SLinus Torvalds int locks_mandatory_area(int read_write, struct inode *inode, 12441da177e4SLinus Torvalds struct file *filp, loff_t offset, 12451da177e4SLinus Torvalds size_t count) 12461da177e4SLinus Torvalds { 12471da177e4SLinus Torvalds struct file_lock fl; 12481da177e4SLinus Torvalds int error; 124929723adeSJeff Layton bool sleep = false; 12501da177e4SLinus Torvalds 12511da177e4SLinus Torvalds locks_init_lock(&fl); 12521da177e4SLinus Torvalds fl.fl_pid = current->tgid; 12531da177e4SLinus Torvalds fl.fl_file = filp; 12541da177e4SLinus Torvalds fl.fl_flags = FL_POSIX | FL_ACCESS; 12551da177e4SLinus Torvalds if (filp && !(filp->f_flags & O_NONBLOCK)) 125629723adeSJeff Layton sleep = true; 12571da177e4SLinus Torvalds fl.fl_type = (read_write == FLOCK_VERIFY_WRITE) ? F_WRLCK : F_RDLCK; 12581da177e4SLinus Torvalds fl.fl_start = offset; 12591da177e4SLinus Torvalds fl.fl_end = offset + count - 1; 12601da177e4SLinus Torvalds 12611da177e4SLinus Torvalds for (;;) { 126229723adeSJeff Layton if (filp) { 126373a8f5f7SChristoph Hellwig fl.fl_owner = filp; 126429723adeSJeff Layton fl.fl_flags &= ~FL_SLEEP; 126529723adeSJeff Layton error = __posix_lock_file(inode, &fl, NULL); 126629723adeSJeff Layton if (!error) 126729723adeSJeff Layton break; 126829723adeSJeff Layton } 126929723adeSJeff Layton 127029723adeSJeff Layton if (sleep) 127129723adeSJeff Layton fl.fl_flags |= FL_SLEEP; 127229723adeSJeff Layton fl.fl_owner = current->files; 1273150b3934SMarc Eshel error = __posix_lock_file(inode, &fl, NULL); 1274bde74e4bSMiklos Szeredi if (error != FILE_LOCK_DEFERRED) 12751da177e4SLinus Torvalds break; 12761da177e4SLinus Torvalds error = wait_event_interruptible(fl.fl_wait, !fl.fl_next); 12771da177e4SLinus Torvalds if (!error) { 12781da177e4SLinus Torvalds /* 12791da177e4SLinus Torvalds * If we've been sleeping someone might have 12801da177e4SLinus Torvalds * changed the permissions behind our back. 12811da177e4SLinus Torvalds */ 1282a16877caSPavel Emelyanov if (__mandatory_lock(inode)) 12831da177e4SLinus Torvalds continue; 12841da177e4SLinus Torvalds } 12851da177e4SLinus Torvalds 12861da177e4SLinus Torvalds locks_delete_block(&fl); 12871da177e4SLinus Torvalds break; 12881da177e4SLinus Torvalds } 12891da177e4SLinus Torvalds 12901da177e4SLinus Torvalds return error; 12911da177e4SLinus Torvalds } 12921da177e4SLinus Torvalds 12931da177e4SLinus Torvalds EXPORT_SYMBOL(locks_mandatory_area); 12941da177e4SLinus Torvalds 1295778fc546SJ. Bruce Fields static void lease_clear_pending(struct file_lock *fl, int arg) 1296778fc546SJ. Bruce Fields { 1297778fc546SJ. Bruce Fields switch (arg) { 1298778fc546SJ. Bruce Fields case F_UNLCK: 1299778fc546SJ. Bruce Fields fl->fl_flags &= ~FL_UNLOCK_PENDING; 1300778fc546SJ. Bruce Fields /* fall through: */ 1301778fc546SJ. Bruce Fields case F_RDLCK: 1302778fc546SJ. Bruce Fields fl->fl_flags &= ~FL_DOWNGRADE_PENDING; 1303778fc546SJ. Bruce Fields } 1304778fc546SJ. Bruce Fields } 1305778fc546SJ. Bruce Fields 13061da177e4SLinus Torvalds /* We already had a lease on this file; just change its type */ 13077448cc37SJeff Layton int lease_modify(struct file_lock *fl, int arg, struct list_head *dispose) 13081da177e4SLinus Torvalds { 13091da177e4SLinus Torvalds int error = assign_type(fl, arg); 13101da177e4SLinus Torvalds 13111da177e4SLinus Torvalds if (error) 13121da177e4SLinus Torvalds return error; 1313778fc546SJ. Bruce Fields lease_clear_pending(fl, arg); 13141da177e4SLinus Torvalds locks_wake_up_blocks(fl); 13153b6e2723SFilipe Brandenburger if (arg == F_UNLCK) { 13163b6e2723SFilipe Brandenburger struct file *filp = fl->fl_file; 13173b6e2723SFilipe Brandenburger 13183b6e2723SFilipe Brandenburger f_delown(filp); 13193b6e2723SFilipe Brandenburger filp->f_owner.signum = 0; 132096d6d59cSJ. Bruce Fields fasync_helper(0, fl->fl_file, 0, &fl->fl_fasync); 132196d6d59cSJ. Bruce Fields if (fl->fl_fasync != NULL) { 132296d6d59cSJ. Bruce Fields printk(KERN_ERR "locks_delete_lock: fasync == %p\n", fl->fl_fasync); 132396d6d59cSJ. Bruce Fields fl->fl_fasync = NULL; 132496d6d59cSJ. Bruce Fields } 1325e084c1bdSJeff Layton locks_delete_lock_ctx(fl, dispose); 13263b6e2723SFilipe Brandenburger } 13271da177e4SLinus Torvalds return 0; 13281da177e4SLinus Torvalds } 13291da177e4SLinus Torvalds EXPORT_SYMBOL(lease_modify); 13301da177e4SLinus Torvalds 1331778fc546SJ. Bruce Fields static bool past_time(unsigned long then) 1332778fc546SJ. Bruce Fields { 1333778fc546SJ. Bruce Fields if (!then) 1334778fc546SJ. Bruce Fields /* 0 is a special value meaning "this never expires": */ 1335778fc546SJ. Bruce Fields return false; 1336778fc546SJ. Bruce Fields return time_after(jiffies, then); 1337778fc546SJ. Bruce Fields } 1338778fc546SJ. Bruce Fields 1339c45198edSJeff Layton static void time_out_leases(struct inode *inode, struct list_head *dispose) 13401da177e4SLinus Torvalds { 13418634b51fSJeff Layton struct file_lock_context *ctx = inode->i_flctx; 13428634b51fSJeff Layton struct file_lock *fl, *tmp; 13431da177e4SLinus Torvalds 13446109c850SJeff Layton lockdep_assert_held(&ctx->flc_lock); 1345f82b4b67SJeff Layton 13468634b51fSJeff Layton list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list) { 134762af4f1fSJeff Layton trace_time_out_leases(inode, fl); 1348778fc546SJ. Bruce Fields if (past_time(fl->fl_downgrade_time)) 13497448cc37SJeff Layton lease_modify(fl, F_RDLCK, dispose); 1350778fc546SJ. Bruce Fields if (past_time(fl->fl_break_time)) 13517448cc37SJeff Layton lease_modify(fl, F_UNLCK, dispose); 13521da177e4SLinus Torvalds } 13531da177e4SLinus Torvalds } 13541da177e4SLinus Torvalds 1355df4e8d2cSJ. Bruce Fields static bool leases_conflict(struct file_lock *lease, struct file_lock *breaker) 1356df4e8d2cSJ. Bruce Fields { 135711afe9f7SChristoph Hellwig if ((breaker->fl_flags & FL_LAYOUT) != (lease->fl_flags & FL_LAYOUT)) 135811afe9f7SChristoph Hellwig return false; 1359df4e8d2cSJ. Bruce Fields if ((breaker->fl_flags & FL_DELEG) && (lease->fl_flags & FL_LEASE)) 1360df4e8d2cSJ. Bruce Fields return false; 1361df4e8d2cSJ. Bruce Fields return locks_conflict(breaker, lease); 1362df4e8d2cSJ. Bruce Fields } 1363df4e8d2cSJ. Bruce Fields 136403d12ddfSJeff Layton static bool 136503d12ddfSJeff Layton any_leases_conflict(struct inode *inode, struct file_lock *breaker) 136603d12ddfSJeff Layton { 13678634b51fSJeff Layton struct file_lock_context *ctx = inode->i_flctx; 136803d12ddfSJeff Layton struct file_lock *fl; 136903d12ddfSJeff Layton 13706109c850SJeff Layton lockdep_assert_held(&ctx->flc_lock); 137103d12ddfSJeff Layton 13728634b51fSJeff Layton list_for_each_entry(fl, &ctx->flc_lease, fl_list) { 137303d12ddfSJeff Layton if (leases_conflict(fl, breaker)) 137403d12ddfSJeff Layton return true; 137503d12ddfSJeff Layton } 137603d12ddfSJeff Layton return false; 137703d12ddfSJeff Layton } 137803d12ddfSJeff Layton 13791da177e4SLinus Torvalds /** 13801da177e4SLinus Torvalds * __break_lease - revoke all outstanding leases on file 13811da177e4SLinus Torvalds * @inode: the inode of the file to return 1382df4e8d2cSJ. Bruce Fields * @mode: O_RDONLY: break only write leases; O_WRONLY or O_RDWR: 1383df4e8d2cSJ. Bruce Fields * break all leases 1384df4e8d2cSJ. Bruce Fields * @type: FL_LEASE: break leases and delegations; FL_DELEG: break 1385df4e8d2cSJ. Bruce Fields * only delegations 13861da177e4SLinus Torvalds * 138787250dd2Sdavid m. richter * break_lease (inlined for speed) has checked there already is at least 138887250dd2Sdavid m. richter * some kind of lock (maybe a lease) on this file. Leases are broken on 138987250dd2Sdavid m. richter * a call to open() or truncate(). This function can sleep unless you 13901da177e4SLinus Torvalds * specified %O_NONBLOCK to your open(). 13911da177e4SLinus Torvalds */ 1392df4e8d2cSJ. Bruce Fields int __break_lease(struct inode *inode, unsigned int mode, unsigned int type) 13931da177e4SLinus Torvalds { 1394778fc546SJ. Bruce Fields int error = 0; 13958634b51fSJeff Layton struct file_lock_context *ctx = inode->i_flctx; 1396a901125cSYan, Zheng struct file_lock *new_fl, *fl, *tmp; 13971da177e4SLinus Torvalds unsigned long break_time; 13988737c930SAl Viro int want_write = (mode & O_ACCMODE) != O_RDONLY; 1399c45198edSJeff Layton LIST_HEAD(dispose); 14001da177e4SLinus Torvalds 14018737c930SAl Viro new_fl = lease_alloc(NULL, want_write ? F_WRLCK : F_RDLCK); 14026d4b9e38SLinus Torvalds if (IS_ERR(new_fl)) 14036d4b9e38SLinus Torvalds return PTR_ERR(new_fl); 1404df4e8d2cSJ. Bruce Fields new_fl->fl_flags = type; 14051da177e4SLinus Torvalds 14068634b51fSJeff Layton /* typically we will check that ctx is non-NULL before calling */ 14078634b51fSJeff Layton if (!ctx) { 14088634b51fSJeff Layton WARN_ON_ONCE(1); 14098634b51fSJeff Layton return error; 14108634b51fSJeff Layton } 14118634b51fSJeff Layton 14126109c850SJeff Layton spin_lock(&ctx->flc_lock); 14131da177e4SLinus Torvalds 1414c45198edSJeff Layton time_out_leases(inode, &dispose); 14151da177e4SLinus Torvalds 141603d12ddfSJeff Layton if (!any_leases_conflict(inode, new_fl)) 1417df4e8d2cSJ. Bruce Fields goto out; 14181da177e4SLinus Torvalds 14191da177e4SLinus Torvalds break_time = 0; 14201da177e4SLinus Torvalds if (lease_break_time > 0) { 14211da177e4SLinus Torvalds break_time = jiffies + lease_break_time * HZ; 14221da177e4SLinus Torvalds if (break_time == 0) 14231da177e4SLinus Torvalds break_time++; /* so that 0 means no break time */ 14241da177e4SLinus Torvalds } 14251da177e4SLinus Torvalds 1426a901125cSYan, Zheng list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list) { 1427df4e8d2cSJ. Bruce Fields if (!leases_conflict(fl, new_fl)) 1428df4e8d2cSJ. Bruce Fields continue; 1429778fc546SJ. Bruce Fields if (want_write) { 1430778fc546SJ. Bruce Fields if (fl->fl_flags & FL_UNLOCK_PENDING) 1431778fc546SJ. Bruce Fields continue; 1432778fc546SJ. Bruce Fields fl->fl_flags |= FL_UNLOCK_PENDING; 14331da177e4SLinus Torvalds fl->fl_break_time = break_time; 1434778fc546SJ. Bruce Fields } else { 14358634b51fSJeff Layton if (lease_breaking(fl)) 1436778fc546SJ. Bruce Fields continue; 1437778fc546SJ. Bruce Fields fl->fl_flags |= FL_DOWNGRADE_PENDING; 1438778fc546SJ. Bruce Fields fl->fl_downgrade_time = break_time; 14391da177e4SLinus Torvalds } 14404d01b7f5SJeff Layton if (fl->fl_lmops->lm_break(fl)) 1441e084c1bdSJeff Layton locks_delete_lock_ctx(fl, &dispose); 14421da177e4SLinus Torvalds } 14431da177e4SLinus Torvalds 14448634b51fSJeff Layton if (list_empty(&ctx->flc_lease)) 14454d01b7f5SJeff Layton goto out; 14464d01b7f5SJeff Layton 1447843c6b2fSJeff Layton if (mode & O_NONBLOCK) { 144862af4f1fSJeff Layton trace_break_lease_noblock(inode, new_fl); 14491da177e4SLinus Torvalds error = -EWOULDBLOCK; 14501da177e4SLinus Torvalds goto out; 14511da177e4SLinus Torvalds } 14521da177e4SLinus Torvalds 14531da177e4SLinus Torvalds restart: 14548634b51fSJeff Layton fl = list_first_entry(&ctx->flc_lease, struct file_lock, fl_list); 14558634b51fSJeff Layton break_time = fl->fl_break_time; 1456f1c6bb2cSJeff Layton if (break_time != 0) 14571da177e4SLinus Torvalds break_time -= jiffies; 14581da177e4SLinus Torvalds if (break_time == 0) 14591da177e4SLinus Torvalds break_time++; 14608634b51fSJeff Layton locks_insert_block(fl, new_fl); 146162af4f1fSJeff Layton trace_break_lease_block(inode, new_fl); 14626109c850SJeff Layton spin_unlock(&ctx->flc_lock); 1463c45198edSJeff Layton locks_dispose_list(&dispose); 14644321e01eSMatthew Wilcox error = wait_event_interruptible_timeout(new_fl->fl_wait, 14654321e01eSMatthew Wilcox !new_fl->fl_next, break_time); 14666109c850SJeff Layton spin_lock(&ctx->flc_lock); 146762af4f1fSJeff Layton trace_break_lease_unblock(inode, new_fl); 14681c8c601aSJeff Layton locks_delete_block(new_fl); 14691da177e4SLinus Torvalds if (error >= 0) { 1470778fc546SJ. Bruce Fields /* 1471778fc546SJ. Bruce Fields * Wait for the next conflicting lease that has not been 1472778fc546SJ. Bruce Fields * broken yet 1473778fc546SJ. Bruce Fields */ 147403d12ddfSJeff Layton if (error == 0) 147503d12ddfSJeff Layton time_out_leases(inode, &dispose); 147603d12ddfSJeff Layton if (any_leases_conflict(inode, new_fl)) 14771da177e4SLinus Torvalds goto restart; 14781da177e4SLinus Torvalds error = 0; 14791da177e4SLinus Torvalds } 14801da177e4SLinus Torvalds out: 14816109c850SJeff Layton spin_unlock(&ctx->flc_lock); 1482c45198edSJeff Layton locks_dispose_list(&dispose); 14831da177e4SLinus Torvalds locks_free_lock(new_fl); 14841da177e4SLinus Torvalds return error; 14851da177e4SLinus Torvalds } 14861da177e4SLinus Torvalds 14871da177e4SLinus Torvalds EXPORT_SYMBOL(__break_lease); 14881da177e4SLinus Torvalds 14891da177e4SLinus Torvalds /** 1490a6b91919SRandy Dunlap * lease_get_mtime - get the last modified time of an inode 14911da177e4SLinus Torvalds * @inode: the inode 14921da177e4SLinus Torvalds * @time: pointer to a timespec which will contain the last modified time 14931da177e4SLinus Torvalds * 14941da177e4SLinus Torvalds * This is to force NFS clients to flush their caches for files with 14951da177e4SLinus Torvalds * exclusive leases. The justification is that if someone has an 1496a6b91919SRandy Dunlap * exclusive lease, then they could be modifying it. 14971da177e4SLinus Torvalds */ 14981da177e4SLinus Torvalds void lease_get_mtime(struct inode *inode, struct timespec *time) 14991da177e4SLinus Torvalds { 1500bfe86024SJeff Layton bool has_lease = false; 15018634b51fSJeff Layton struct file_lock_context *ctx = inode->i_flctx; 15028634b51fSJeff Layton struct file_lock *fl; 1503bfe86024SJeff Layton 15048634b51fSJeff Layton if (ctx && !list_empty_careful(&ctx->flc_lease)) { 15056109c850SJeff Layton spin_lock(&ctx->flc_lock); 15068634b51fSJeff Layton if (!list_empty(&ctx->flc_lease)) { 15078634b51fSJeff Layton fl = list_first_entry(&ctx->flc_lease, 15088634b51fSJeff Layton struct file_lock, fl_list); 15098634b51fSJeff Layton if (fl->fl_type == F_WRLCK) 1510bfe86024SJeff Layton has_lease = true; 15118634b51fSJeff Layton } 15126109c850SJeff Layton spin_unlock(&ctx->flc_lock); 1513bfe86024SJeff Layton } 1514bfe86024SJeff Layton 1515bfe86024SJeff Layton if (has_lease) 15161da177e4SLinus Torvalds *time = current_fs_time(inode->i_sb); 15171da177e4SLinus Torvalds else 15181da177e4SLinus Torvalds *time = inode->i_mtime; 15191da177e4SLinus Torvalds } 15201da177e4SLinus Torvalds 15211da177e4SLinus Torvalds EXPORT_SYMBOL(lease_get_mtime); 15221da177e4SLinus Torvalds 15231da177e4SLinus Torvalds /** 15241da177e4SLinus Torvalds * fcntl_getlease - Enquire what lease is currently active 15251da177e4SLinus Torvalds * @filp: the file 15261da177e4SLinus Torvalds * 15271da177e4SLinus Torvalds * The value returned by this function will be one of 15281da177e4SLinus Torvalds * (if no lease break is pending): 15291da177e4SLinus Torvalds * 15301da177e4SLinus Torvalds * %F_RDLCK to indicate a shared lease is held. 15311da177e4SLinus Torvalds * 15321da177e4SLinus Torvalds * %F_WRLCK to indicate an exclusive lease is held. 15331da177e4SLinus Torvalds * 15341da177e4SLinus Torvalds * %F_UNLCK to indicate no lease is held. 15351da177e4SLinus Torvalds * 15361da177e4SLinus Torvalds * (if a lease break is pending): 15371da177e4SLinus Torvalds * 15381da177e4SLinus Torvalds * %F_RDLCK to indicate an exclusive lease needs to be 15391da177e4SLinus Torvalds * changed to a shared lease (or removed). 15401da177e4SLinus Torvalds * 15411da177e4SLinus Torvalds * %F_UNLCK to indicate the lease needs to be removed. 15421da177e4SLinus Torvalds * 15431da177e4SLinus Torvalds * XXX: sfr & willy disagree over whether F_INPROGRESS 15441da177e4SLinus Torvalds * should be returned to userspace. 15451da177e4SLinus Torvalds */ 15461da177e4SLinus Torvalds int fcntl_getlease(struct file *filp) 15471da177e4SLinus Torvalds { 15481da177e4SLinus Torvalds struct file_lock *fl; 15491c8c601aSJeff Layton struct inode *inode = file_inode(filp); 15508634b51fSJeff Layton struct file_lock_context *ctx = inode->i_flctx; 15511da177e4SLinus Torvalds int type = F_UNLCK; 1552c45198edSJeff Layton LIST_HEAD(dispose); 15531da177e4SLinus Torvalds 15548634b51fSJeff Layton if (ctx && !list_empty_careful(&ctx->flc_lease)) { 15556109c850SJeff Layton spin_lock(&ctx->flc_lock); 1556c45198edSJeff Layton time_out_leases(file_inode(filp), &dispose); 15578634b51fSJeff Layton list_for_each_entry(fl, &ctx->flc_lease, fl_list) { 15588634b51fSJeff Layton if (fl->fl_file != filp) 15598634b51fSJeff Layton continue; 1560778fc546SJ. Bruce Fields type = target_leasetype(fl); 15611da177e4SLinus Torvalds break; 15621da177e4SLinus Torvalds } 15636109c850SJeff Layton spin_unlock(&ctx->flc_lock); 1564c45198edSJeff Layton locks_dispose_list(&dispose); 15658634b51fSJeff Layton } 15661da177e4SLinus Torvalds return type; 15671da177e4SLinus Torvalds } 15681da177e4SLinus Torvalds 156924cbe784SJeff Layton /** 157024cbe784SJeff Layton * check_conflicting_open - see if the given dentry points to a file that has 157124cbe784SJeff Layton * an existing open that would conflict with the 157224cbe784SJeff Layton * desired lease. 157324cbe784SJeff Layton * @dentry: dentry to check 157424cbe784SJeff Layton * @arg: type of lease that we're trying to acquire 157524cbe784SJeff Layton * 157624cbe784SJeff Layton * Check to see if there's an existing open fd on this file that would 157724cbe784SJeff Layton * conflict with the lease we're trying to set. 157824cbe784SJeff Layton */ 157924cbe784SJeff Layton static int 158011afe9f7SChristoph Hellwig check_conflicting_open(const struct dentry *dentry, const long arg, int flags) 158124cbe784SJeff Layton { 158224cbe784SJeff Layton int ret = 0; 158324cbe784SJeff Layton struct inode *inode = dentry->d_inode; 158424cbe784SJeff Layton 158511afe9f7SChristoph Hellwig if (flags & FL_LAYOUT) 158611afe9f7SChristoph Hellwig return 0; 158711afe9f7SChristoph Hellwig 158824cbe784SJeff Layton if ((arg == F_RDLCK) && (atomic_read(&inode->i_writecount) > 0)) 158924cbe784SJeff Layton return -EAGAIN; 159024cbe784SJeff Layton 159124cbe784SJeff Layton if ((arg == F_WRLCK) && ((d_count(dentry) > 1) || 159224cbe784SJeff Layton (atomic_read(&inode->i_count) > 1))) 159324cbe784SJeff Layton ret = -EAGAIN; 159424cbe784SJeff Layton 159524cbe784SJeff Layton return ret; 159624cbe784SJeff Layton } 159724cbe784SJeff Layton 1598e6f5c789SJeff Layton static int 1599e6f5c789SJeff Layton generic_add_lease(struct file *filp, long arg, struct file_lock **flp, void **priv) 16001da177e4SLinus Torvalds { 16018634b51fSJeff Layton struct file_lock *fl, *my_fl = NULL, *lease; 16020f7fc9e4SJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 16031da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 16048634b51fSJeff Layton struct file_lock_context *ctx; 1605df4e8d2cSJ. Bruce Fields bool is_deleg = (*flp)->fl_flags & FL_DELEG; 1606c1f24ef4SJ. Bruce Fields int error; 1607c45198edSJeff Layton LIST_HEAD(dispose); 16081da177e4SLinus Torvalds 1609096657b6SJ. Bruce Fields lease = *flp; 161062af4f1fSJeff Layton trace_generic_add_lease(inode, lease); 161162af4f1fSJeff Layton 16128634b51fSJeff Layton ctx = locks_get_lock_context(inode); 16138634b51fSJeff Layton if (!ctx) 16148634b51fSJeff Layton return -ENOMEM; 16158634b51fSJeff Layton 1616df4e8d2cSJ. Bruce Fields /* 1617df4e8d2cSJ. Bruce Fields * In the delegation case we need mutual exclusion with 1618df4e8d2cSJ. Bruce Fields * a number of operations that take the i_mutex. We trylock 1619df4e8d2cSJ. Bruce Fields * because delegations are an optional optimization, and if 1620df4e8d2cSJ. Bruce Fields * there's some chance of a conflict--we'd rather not 1621df4e8d2cSJ. Bruce Fields * bother, maybe that's a sign this just isn't a good file to 1622df4e8d2cSJ. Bruce Fields * hand out a delegation on. 1623df4e8d2cSJ. Bruce Fields */ 1624df4e8d2cSJ. Bruce Fields if (is_deleg && !mutex_trylock(&inode->i_mutex)) 1625df4e8d2cSJ. Bruce Fields return -EAGAIN; 1626df4e8d2cSJ. Bruce Fields 1627df4e8d2cSJ. Bruce Fields if (is_deleg && arg == F_WRLCK) { 1628df4e8d2cSJ. Bruce Fields /* Write delegations are not currently supported: */ 16294fdb793fSDan Carpenter mutex_unlock(&inode->i_mutex); 1630df4e8d2cSJ. Bruce Fields WARN_ON_ONCE(1); 1631df4e8d2cSJ. Bruce Fields return -EINVAL; 1632df4e8d2cSJ. Bruce Fields } 1633096657b6SJ. Bruce Fields 16346109c850SJeff Layton spin_lock(&ctx->flc_lock); 1635c45198edSJeff Layton time_out_leases(inode, &dispose); 163611afe9f7SChristoph Hellwig error = check_conflicting_open(dentry, arg, lease->fl_flags); 163724cbe784SJeff Layton if (error) 16381da177e4SLinus Torvalds goto out; 163985c59580SPavel Emelyanov 16401da177e4SLinus Torvalds /* 16411da177e4SLinus Torvalds * At this point, we know that if there is an exclusive 16421da177e4SLinus Torvalds * lease on this file, then we hold it on this filp 16431da177e4SLinus Torvalds * (otherwise our open of this file would have blocked). 16441da177e4SLinus Torvalds * And if we are trying to acquire an exclusive lease, 16451da177e4SLinus Torvalds * then the file is not open by anyone (including us) 16461da177e4SLinus Torvalds * except for this filp. 16471da177e4SLinus Torvalds */ 1648c1f24ef4SJ. Bruce Fields error = -EAGAIN; 16498634b51fSJeff Layton list_for_each_entry(fl, &ctx->flc_lease, fl_list) { 16502ab99ee1SChristoph Hellwig if (fl->fl_file == filp && 16512ab99ee1SChristoph Hellwig fl->fl_owner == lease->fl_owner) { 16528634b51fSJeff Layton my_fl = fl; 1653c1f24ef4SJ. Bruce Fields continue; 16541da177e4SLinus Torvalds } 16558634b51fSJeff Layton 1656c1f24ef4SJ. Bruce Fields /* 1657c1f24ef4SJ. Bruce Fields * No exclusive leases if someone else has a lease on 1658c1f24ef4SJ. Bruce Fields * this file: 1659c1f24ef4SJ. Bruce Fields */ 1660c1f24ef4SJ. Bruce Fields if (arg == F_WRLCK) 16611da177e4SLinus Torvalds goto out; 1662c1f24ef4SJ. Bruce Fields /* 1663c1f24ef4SJ. Bruce Fields * Modifying our existing lease is OK, but no getting a 1664c1f24ef4SJ. Bruce Fields * new lease if someone else is opening for write: 1665c1f24ef4SJ. Bruce Fields */ 1666c1f24ef4SJ. Bruce Fields if (fl->fl_flags & FL_UNLOCK_PENDING) 1667c1f24ef4SJ. Bruce Fields goto out; 1668c1f24ef4SJ. Bruce Fields } 16691da177e4SLinus Torvalds 16708634b51fSJeff Layton if (my_fl != NULL) { 16710164bf02SJeff Layton lease = my_fl; 16720164bf02SJeff Layton error = lease->fl_lmops->lm_change(lease, arg, &dispose); 16731c7dd2ffSJeff Layton if (error) 16741da177e4SLinus Torvalds goto out; 16751c7dd2ffSJeff Layton goto out_setup; 16761da177e4SLinus Torvalds } 16771da177e4SLinus Torvalds 16781da177e4SLinus Torvalds error = -EINVAL; 16791da177e4SLinus Torvalds if (!leases_enable) 16801da177e4SLinus Torvalds goto out; 16811da177e4SLinus Torvalds 1682e084c1bdSJeff Layton locks_insert_lock_ctx(lease, &ctx->flc_lease); 168324cbe784SJeff Layton /* 168424cbe784SJeff Layton * The check in break_lease() is lockless. It's possible for another 168524cbe784SJeff Layton * open to race in after we did the earlier check for a conflicting 168624cbe784SJeff Layton * open but before the lease was inserted. Check again for a 168724cbe784SJeff Layton * conflicting open and cancel the lease if there is one. 168824cbe784SJeff Layton * 168924cbe784SJeff Layton * We also add a barrier here to ensure that the insertion of the lock 169024cbe784SJeff Layton * precedes these checks. 169124cbe784SJeff Layton */ 169224cbe784SJeff Layton smp_mb(); 169311afe9f7SChristoph Hellwig error = check_conflicting_open(dentry, arg, lease->fl_flags); 16948634b51fSJeff Layton if (error) { 1695e084c1bdSJeff Layton locks_unlink_lock_ctx(lease); 16968634b51fSJeff Layton goto out; 16978634b51fSJeff Layton } 16981c7dd2ffSJeff Layton 16991c7dd2ffSJeff Layton out_setup: 17001c7dd2ffSJeff Layton if (lease->fl_lmops->lm_setup) 17011c7dd2ffSJeff Layton lease->fl_lmops->lm_setup(lease, priv); 17021da177e4SLinus Torvalds out: 17036109c850SJeff Layton spin_unlock(&ctx->flc_lock); 1704c45198edSJeff Layton locks_dispose_list(&dispose); 1705df4e8d2cSJ. Bruce Fields if (is_deleg) 1706df4e8d2cSJ. Bruce Fields mutex_unlock(&inode->i_mutex); 17078634b51fSJeff Layton if (!error && !my_fl) 17081c7dd2ffSJeff Layton *flp = NULL; 17091da177e4SLinus Torvalds return error; 17101da177e4SLinus Torvalds } 17118335ebd9SJ. Bruce Fields 17122ab99ee1SChristoph Hellwig static int generic_delete_lease(struct file *filp, void *owner) 17138335ebd9SJ. Bruce Fields { 17140efaa7e8SJeff Layton int error = -EAGAIN; 17158634b51fSJeff Layton struct file_lock *fl, *victim = NULL; 17168335ebd9SJ. Bruce Fields struct dentry *dentry = filp->f_path.dentry; 17178335ebd9SJ. Bruce Fields struct inode *inode = dentry->d_inode; 17188634b51fSJeff Layton struct file_lock_context *ctx = inode->i_flctx; 1719c45198edSJeff Layton LIST_HEAD(dispose); 17208335ebd9SJ. Bruce Fields 17218634b51fSJeff Layton if (!ctx) { 17228634b51fSJeff Layton trace_generic_delete_lease(inode, NULL); 17238634b51fSJeff Layton return error; 17248634b51fSJeff Layton } 17258634b51fSJeff Layton 17266109c850SJeff Layton spin_lock(&ctx->flc_lock); 17278634b51fSJeff Layton list_for_each_entry(fl, &ctx->flc_lease, fl_list) { 17282ab99ee1SChristoph Hellwig if (fl->fl_file == filp && 17292ab99ee1SChristoph Hellwig fl->fl_owner == owner) { 17308634b51fSJeff Layton victim = fl; 17310efaa7e8SJeff Layton break; 17328335ebd9SJ. Bruce Fields } 17338634b51fSJeff Layton } 1734a9b1b455SJeff Layton trace_generic_delete_lease(inode, victim); 17358634b51fSJeff Layton if (victim) 17367448cc37SJeff Layton error = fl->fl_lmops->lm_change(victim, F_UNLCK, &dispose); 17376109c850SJeff Layton spin_unlock(&ctx->flc_lock); 1738c45198edSJeff Layton locks_dispose_list(&dispose); 17390efaa7e8SJeff Layton return error; 17408335ebd9SJ. Bruce Fields } 17418335ebd9SJ. Bruce Fields 17421da177e4SLinus Torvalds /** 17431da177e4SLinus Torvalds * generic_setlease - sets a lease on an open file 17441da177e4SLinus Torvalds * @filp: file pointer 17451da177e4SLinus Torvalds * @arg: type of lease to obtain 17461da177e4SLinus Torvalds * @flp: input - file_lock to use, output - file_lock inserted 17471c7dd2ffSJeff Layton * @priv: private data for lm_setup (may be NULL if lm_setup 17481c7dd2ffSJeff Layton * doesn't require it) 17491da177e4SLinus Torvalds * 17501da177e4SLinus Torvalds * The (input) flp->fl_lmops->lm_break function is required 17511da177e4SLinus Torvalds * by break_lease(). 17521da177e4SLinus Torvalds */ 1753e6f5c789SJeff Layton int generic_setlease(struct file *filp, long arg, struct file_lock **flp, 1754e6f5c789SJeff Layton void **priv) 17551da177e4SLinus Torvalds { 17561da177e4SLinus Torvalds struct dentry *dentry = filp->f_path.dentry; 17571da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 17588335ebd9SJ. Bruce Fields int error; 17591da177e4SLinus Torvalds 17608e96e3b7SEric W. Biederman if ((!uid_eq(current_fsuid(), inode->i_uid)) && !capable(CAP_LEASE)) 17618335ebd9SJ. Bruce Fields return -EACCES; 17621da177e4SLinus Torvalds if (!S_ISREG(inode->i_mode)) 17638335ebd9SJ. Bruce Fields return -EINVAL; 17641da177e4SLinus Torvalds error = security_file_lock(filp, arg); 17651da177e4SLinus Torvalds if (error) 17668335ebd9SJ. Bruce Fields return error; 17671da177e4SLinus Torvalds 17688335ebd9SJ. Bruce Fields switch (arg) { 17698335ebd9SJ. Bruce Fields case F_UNLCK: 17702ab99ee1SChristoph Hellwig return generic_delete_lease(filp, *priv); 17718335ebd9SJ. Bruce Fields case F_RDLCK: 17728335ebd9SJ. Bruce Fields case F_WRLCK: 17730efaa7e8SJeff Layton if (!(*flp)->fl_lmops->lm_break) { 17740efaa7e8SJeff Layton WARN_ON_ONCE(1); 17750efaa7e8SJeff Layton return -ENOLCK; 17760efaa7e8SJeff Layton } 177711afe9f7SChristoph Hellwig 1778e6f5c789SJeff Layton return generic_add_lease(filp, arg, flp, priv); 17798335ebd9SJ. Bruce Fields default: 17808d657eb3SDave Jones return -EINVAL; 17811da177e4SLinus Torvalds } 17821da177e4SLinus Torvalds } 17830af1a450SChristoph Hellwig EXPORT_SYMBOL(generic_setlease); 17841da177e4SLinus Torvalds 17851da177e4SLinus Torvalds /** 1786a9933ceaSJ. Bruce Fields * vfs_setlease - sets a lease on an open file 17871da177e4SLinus Torvalds * @filp: file pointer 17881da177e4SLinus Torvalds * @arg: type of lease to obtain 1789e51673aaSJeff Layton * @lease: file_lock to use when adding a lease 17901c7dd2ffSJeff Layton * @priv: private info for lm_setup when adding a lease (may be 17911c7dd2ffSJeff Layton * NULL if lm_setup doesn't require it) 17921da177e4SLinus Torvalds * 1793e51673aaSJeff Layton * Call this to establish a lease on the file. The "lease" argument is not 1794e51673aaSJeff Layton * used for F_UNLCK requests and may be NULL. For commands that set or alter 1795e51673aaSJeff Layton * an existing lease, the (*lease)->fl_lmops->lm_break operation must be set; 1796e51673aaSJeff Layton * if not, this function will return -ENOLCK (and generate a scary-looking 1797e51673aaSJeff Layton * stack trace). 17981c7dd2ffSJeff Layton * 17991c7dd2ffSJeff Layton * The "priv" pointer is passed directly to the lm_setup function as-is. It 18001c7dd2ffSJeff Layton * may be NULL if the lm_setup operation doesn't require it. 18011da177e4SLinus Torvalds */ 1802e6f5c789SJeff Layton int 1803e6f5c789SJeff Layton vfs_setlease(struct file *filp, long arg, struct file_lock **lease, void **priv) 18041da177e4SLinus Torvalds { 18051c7dd2ffSJeff Layton if (filp->f_op->setlease) 1806f82b4b67SJeff Layton return filp->f_op->setlease(filp, arg, lease, priv); 18071c7dd2ffSJeff Layton else 1808f82b4b67SJeff Layton return generic_setlease(filp, arg, lease, priv); 18091da177e4SLinus Torvalds } 1810a9933ceaSJ. Bruce Fields EXPORT_SYMBOL_GPL(vfs_setlease); 18111da177e4SLinus Torvalds 18120ceaf6c7SJ. Bruce Fields static int do_fcntl_add_lease(unsigned int fd, struct file *filp, long arg) 18131da177e4SLinus Torvalds { 18141c7dd2ffSJeff Layton struct file_lock *fl; 1815f7347ce4SLinus Torvalds struct fasync_struct *new; 18161da177e4SLinus Torvalds int error; 18171da177e4SLinus Torvalds 1818c5b1f0d9SArnd Bergmann fl = lease_alloc(filp, arg); 1819c5b1f0d9SArnd Bergmann if (IS_ERR(fl)) 1820c5b1f0d9SArnd Bergmann return PTR_ERR(fl); 18211da177e4SLinus Torvalds 1822f7347ce4SLinus Torvalds new = fasync_alloc(); 1823f7347ce4SLinus Torvalds if (!new) { 1824f7347ce4SLinus Torvalds locks_free_lock(fl); 1825f7347ce4SLinus Torvalds return -ENOMEM; 1826f7347ce4SLinus Torvalds } 18271c7dd2ffSJeff Layton new->fa_fd = fd; 18281da177e4SLinus Torvalds 18291c7dd2ffSJeff Layton error = vfs_setlease(filp, arg, &fl, (void **)&new); 18302dfb928fSJeff Layton if (fl) 18312dfb928fSJeff Layton locks_free_lock(fl); 1832f7347ce4SLinus Torvalds if (new) 1833f7347ce4SLinus Torvalds fasync_free(new); 18341da177e4SLinus Torvalds return error; 18351da177e4SLinus Torvalds } 18361da177e4SLinus Torvalds 18371da177e4SLinus Torvalds /** 18380ceaf6c7SJ. Bruce Fields * fcntl_setlease - sets a lease on an open file 18390ceaf6c7SJ. Bruce Fields * @fd: open file descriptor 18400ceaf6c7SJ. Bruce Fields * @filp: file pointer 18410ceaf6c7SJ. Bruce Fields * @arg: type of lease to obtain 18420ceaf6c7SJ. Bruce Fields * 18430ceaf6c7SJ. Bruce Fields * Call this fcntl to establish a lease on the file. 18440ceaf6c7SJ. Bruce Fields * Note that you also need to call %F_SETSIG to 18450ceaf6c7SJ. Bruce Fields * receive a signal when the lease is broken. 18460ceaf6c7SJ. Bruce Fields */ 18470ceaf6c7SJ. Bruce Fields int fcntl_setlease(unsigned int fd, struct file *filp, long arg) 18480ceaf6c7SJ. Bruce Fields { 18490ceaf6c7SJ. Bruce Fields if (arg == F_UNLCK) 18502ab99ee1SChristoph Hellwig return vfs_setlease(filp, F_UNLCK, NULL, (void **)&filp); 18510ceaf6c7SJ. Bruce Fields return do_fcntl_add_lease(fd, filp, arg); 18520ceaf6c7SJ. Bruce Fields } 18530ceaf6c7SJ. Bruce Fields 18540ceaf6c7SJ. Bruce Fields /** 18551da177e4SLinus Torvalds * flock_lock_file_wait - Apply a FLOCK-style lock to a file 18561da177e4SLinus Torvalds * @filp: The file to apply the lock to 18571da177e4SLinus Torvalds * @fl: The lock to be applied 18581da177e4SLinus Torvalds * 18591da177e4SLinus Torvalds * Add a FLOCK style lock to a file. 18601da177e4SLinus Torvalds */ 18611da177e4SLinus Torvalds int flock_lock_file_wait(struct file *filp, struct file_lock *fl) 18621da177e4SLinus Torvalds { 18631da177e4SLinus Torvalds int error; 18641da177e4SLinus Torvalds might_sleep(); 18651da177e4SLinus Torvalds for (;;) { 18661da177e4SLinus Torvalds error = flock_lock_file(filp, fl); 1867bde74e4bSMiklos Szeredi if (error != FILE_LOCK_DEFERRED) 18681da177e4SLinus Torvalds break; 18691da177e4SLinus Torvalds error = wait_event_interruptible(fl->fl_wait, !fl->fl_next); 18701da177e4SLinus Torvalds if (!error) 18711da177e4SLinus Torvalds continue; 18721da177e4SLinus Torvalds 18731da177e4SLinus Torvalds locks_delete_block(fl); 18741da177e4SLinus Torvalds break; 18751da177e4SLinus Torvalds } 18761da177e4SLinus Torvalds return error; 18771da177e4SLinus Torvalds } 18781da177e4SLinus Torvalds 18791da177e4SLinus Torvalds EXPORT_SYMBOL(flock_lock_file_wait); 18801da177e4SLinus Torvalds 18811da177e4SLinus Torvalds /** 18821da177e4SLinus Torvalds * sys_flock: - flock() system call. 18831da177e4SLinus Torvalds * @fd: the file descriptor to lock. 18841da177e4SLinus Torvalds * @cmd: the type of lock to apply. 18851da177e4SLinus Torvalds * 18861da177e4SLinus Torvalds * Apply a %FL_FLOCK style lock to an open file descriptor. 18871da177e4SLinus Torvalds * The @cmd can be one of 18881da177e4SLinus Torvalds * 18891da177e4SLinus Torvalds * %LOCK_SH -- a shared lock. 18901da177e4SLinus Torvalds * 18911da177e4SLinus Torvalds * %LOCK_EX -- an exclusive lock. 18921da177e4SLinus Torvalds * 18931da177e4SLinus Torvalds * %LOCK_UN -- remove an existing lock. 18941da177e4SLinus Torvalds * 18951da177e4SLinus Torvalds * %LOCK_MAND -- a `mandatory' flock. This exists to emulate Windows Share Modes. 18961da177e4SLinus Torvalds * 18971da177e4SLinus Torvalds * %LOCK_MAND can be combined with %LOCK_READ or %LOCK_WRITE to allow other 18981da177e4SLinus Torvalds * processes read and write access respectively. 18991da177e4SLinus Torvalds */ 1900002c8976SHeiko Carstens SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd) 19011da177e4SLinus Torvalds { 19022903ff01SAl Viro struct fd f = fdget(fd); 19031da177e4SLinus Torvalds struct file_lock *lock; 19041da177e4SLinus Torvalds int can_sleep, unlock; 19051da177e4SLinus Torvalds int error; 19061da177e4SLinus Torvalds 19071da177e4SLinus Torvalds error = -EBADF; 19082903ff01SAl Viro if (!f.file) 19091da177e4SLinus Torvalds goto out; 19101da177e4SLinus Torvalds 19111da177e4SLinus Torvalds can_sleep = !(cmd & LOCK_NB); 19121da177e4SLinus Torvalds cmd &= ~LOCK_NB; 19131da177e4SLinus Torvalds unlock = (cmd == LOCK_UN); 19141da177e4SLinus Torvalds 1915aeb5d727SAl Viro if (!unlock && !(cmd & LOCK_MAND) && 19162903ff01SAl Viro !(f.file->f_mode & (FMODE_READ|FMODE_WRITE))) 19171da177e4SLinus Torvalds goto out_putf; 19181da177e4SLinus Torvalds 19196e129d00SJeff Layton lock = flock_make_lock(f.file, cmd); 19206e129d00SJeff Layton if (IS_ERR(lock)) { 19216e129d00SJeff Layton error = PTR_ERR(lock); 19221da177e4SLinus Torvalds goto out_putf; 19236e129d00SJeff Layton } 19246e129d00SJeff Layton 19251da177e4SLinus Torvalds if (can_sleep) 19261da177e4SLinus Torvalds lock->fl_flags |= FL_SLEEP; 19271da177e4SLinus Torvalds 19282903ff01SAl Viro error = security_file_lock(f.file, lock->fl_type); 19291da177e4SLinus Torvalds if (error) 19301da177e4SLinus Torvalds goto out_free; 19311da177e4SLinus Torvalds 193272c2d531SAl Viro if (f.file->f_op->flock) 19332903ff01SAl Viro error = f.file->f_op->flock(f.file, 19341da177e4SLinus Torvalds (can_sleep) ? F_SETLKW : F_SETLK, 19351da177e4SLinus Torvalds lock); 19361da177e4SLinus Torvalds else 19372903ff01SAl Viro error = flock_lock_file_wait(f.file, lock); 19381da177e4SLinus Torvalds 19391da177e4SLinus Torvalds out_free: 19401da177e4SLinus Torvalds locks_free_lock(lock); 19411da177e4SLinus Torvalds 19421da177e4SLinus Torvalds out_putf: 19432903ff01SAl Viro fdput(f); 19441da177e4SLinus Torvalds out: 19451da177e4SLinus Torvalds return error; 19461da177e4SLinus Torvalds } 19471da177e4SLinus Torvalds 19483ee17abdSJ. Bruce Fields /** 19493ee17abdSJ. Bruce Fields * vfs_test_lock - test file byte range lock 19503ee17abdSJ. Bruce Fields * @filp: The file to test lock for 19516924c554SJ. Bruce Fields * @fl: The lock to test; also used to hold result 19523ee17abdSJ. Bruce Fields * 19533ee17abdSJ. Bruce Fields * Returns -ERRNO on failure. Indicates presence of conflicting lock by 19543ee17abdSJ. Bruce Fields * setting conf->fl_type to something other than F_UNLCK. 19553ee17abdSJ. Bruce Fields */ 19563ee17abdSJ. Bruce Fields int vfs_test_lock(struct file *filp, struct file_lock *fl) 19573ee17abdSJ. Bruce Fields { 195872c2d531SAl Viro if (filp->f_op->lock) 19593ee17abdSJ. Bruce Fields return filp->f_op->lock(filp, F_GETLK, fl); 19603ee17abdSJ. Bruce Fields posix_test_lock(filp, fl); 19613ee17abdSJ. Bruce Fields return 0; 19623ee17abdSJ. Bruce Fields } 19633ee17abdSJ. Bruce Fields EXPORT_SYMBOL_GPL(vfs_test_lock); 19643ee17abdSJ. Bruce Fields 1965c2fa1b8aSJ. Bruce Fields static int posix_lock_to_flock(struct flock *flock, struct file_lock *fl) 1966c2fa1b8aSJ. Bruce Fields { 1967cff2fce5SJeff Layton flock->l_pid = IS_OFDLCK(fl) ? -1 : fl->fl_pid; 1968c2fa1b8aSJ. Bruce Fields #if BITS_PER_LONG == 32 1969c2fa1b8aSJ. Bruce Fields /* 1970c2fa1b8aSJ. Bruce Fields * Make sure we can represent the posix lock via 1971c2fa1b8aSJ. Bruce Fields * legacy 32bit flock. 1972c2fa1b8aSJ. Bruce Fields */ 1973c2fa1b8aSJ. Bruce Fields if (fl->fl_start > OFFT_OFFSET_MAX) 1974c2fa1b8aSJ. Bruce Fields return -EOVERFLOW; 1975c2fa1b8aSJ. Bruce Fields if (fl->fl_end != OFFSET_MAX && fl->fl_end > OFFT_OFFSET_MAX) 1976c2fa1b8aSJ. Bruce Fields return -EOVERFLOW; 1977c2fa1b8aSJ. Bruce Fields #endif 1978c2fa1b8aSJ. Bruce Fields flock->l_start = fl->fl_start; 1979c2fa1b8aSJ. Bruce Fields flock->l_len = fl->fl_end == OFFSET_MAX ? 0 : 1980c2fa1b8aSJ. Bruce Fields fl->fl_end - fl->fl_start + 1; 1981c2fa1b8aSJ. Bruce Fields flock->l_whence = 0; 1982129a84deSJ. Bruce Fields flock->l_type = fl->fl_type; 1983c2fa1b8aSJ. Bruce Fields return 0; 1984c2fa1b8aSJ. Bruce Fields } 1985c2fa1b8aSJ. Bruce Fields 1986c2fa1b8aSJ. Bruce Fields #if BITS_PER_LONG == 32 1987c2fa1b8aSJ. Bruce Fields static void posix_lock_to_flock64(struct flock64 *flock, struct file_lock *fl) 1988c2fa1b8aSJ. Bruce Fields { 1989cff2fce5SJeff Layton flock->l_pid = IS_OFDLCK(fl) ? -1 : fl->fl_pid; 1990c2fa1b8aSJ. Bruce Fields flock->l_start = fl->fl_start; 1991c2fa1b8aSJ. Bruce Fields flock->l_len = fl->fl_end == OFFSET_MAX ? 0 : 1992c2fa1b8aSJ. Bruce Fields fl->fl_end - fl->fl_start + 1; 1993c2fa1b8aSJ. Bruce Fields flock->l_whence = 0; 1994c2fa1b8aSJ. Bruce Fields flock->l_type = fl->fl_type; 1995c2fa1b8aSJ. Bruce Fields } 1996c2fa1b8aSJ. Bruce Fields #endif 1997c2fa1b8aSJ. Bruce Fields 19981da177e4SLinus Torvalds /* Report the first existing lock that would conflict with l. 19991da177e4SLinus Torvalds * This implements the F_GETLK command of fcntl(). 20001da177e4SLinus Torvalds */ 2001c1e62b8fSJeff Layton int fcntl_getlk(struct file *filp, unsigned int cmd, struct flock __user *l) 20021da177e4SLinus Torvalds { 20039d6a8c5cSMarc Eshel struct file_lock file_lock; 20041da177e4SLinus Torvalds struct flock flock; 20051da177e4SLinus Torvalds int error; 20061da177e4SLinus Torvalds 20071da177e4SLinus Torvalds error = -EFAULT; 20081da177e4SLinus Torvalds if (copy_from_user(&flock, l, sizeof(flock))) 20091da177e4SLinus Torvalds goto out; 20101da177e4SLinus Torvalds error = -EINVAL; 20111da177e4SLinus Torvalds if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK)) 20121da177e4SLinus Torvalds goto out; 20131da177e4SLinus Torvalds 20141da177e4SLinus Torvalds error = flock_to_posix_lock(filp, &file_lock, &flock); 20151da177e4SLinus Torvalds if (error) 20161da177e4SLinus Torvalds goto out; 20171da177e4SLinus Torvalds 20180d3f7a2dSJeff Layton if (cmd == F_OFD_GETLK) { 201990478939SJeff Layton error = -EINVAL; 202090478939SJeff Layton if (flock.l_pid != 0) 202190478939SJeff Layton goto out; 202290478939SJeff Layton 20235d50ffd7SJeff Layton cmd = F_GETLK; 2024cff2fce5SJeff Layton file_lock.fl_flags |= FL_OFDLCK; 202573a8f5f7SChristoph Hellwig file_lock.fl_owner = filp; 20265d50ffd7SJeff Layton } 20275d50ffd7SJeff Layton 20283ee17abdSJ. Bruce Fields error = vfs_test_lock(filp, &file_lock); 20293ee17abdSJ. Bruce Fields if (error) 20301da177e4SLinus Torvalds goto out; 20311da177e4SLinus Torvalds 20329d6a8c5cSMarc Eshel flock.l_type = file_lock.fl_type; 20339d6a8c5cSMarc Eshel if (file_lock.fl_type != F_UNLCK) { 20349d6a8c5cSMarc Eshel error = posix_lock_to_flock(&flock, &file_lock); 2035c2fa1b8aSJ. Bruce Fields if (error) 2036f328296eSKinglong Mee goto rel_priv; 20371da177e4SLinus Torvalds } 20381da177e4SLinus Torvalds error = -EFAULT; 20391da177e4SLinus Torvalds if (!copy_to_user(l, &flock, sizeof(flock))) 20401da177e4SLinus Torvalds error = 0; 2041f328296eSKinglong Mee rel_priv: 2042f328296eSKinglong Mee locks_release_private(&file_lock); 20431da177e4SLinus Torvalds out: 20441da177e4SLinus Torvalds return error; 20451da177e4SLinus Torvalds } 20461da177e4SLinus Torvalds 20477723ec97SMarc Eshel /** 20487723ec97SMarc Eshel * vfs_lock_file - file byte range lock 20497723ec97SMarc Eshel * @filp: The file to apply the lock to 20507723ec97SMarc Eshel * @cmd: type of locking operation (F_SETLK, F_GETLK, etc.) 20517723ec97SMarc Eshel * @fl: The lock to be applied 2052150b3934SMarc Eshel * @conf: Place to return a copy of the conflicting lock, if found. 2053150b3934SMarc Eshel * 2054150b3934SMarc Eshel * A caller that doesn't care about the conflicting lock may pass NULL 2055150b3934SMarc Eshel * as the final argument. 2056150b3934SMarc Eshel * 2057150b3934SMarc Eshel * If the filesystem defines a private ->lock() method, then @conf will 2058150b3934SMarc Eshel * be left unchanged; so a caller that cares should initialize it to 2059150b3934SMarc Eshel * some acceptable default. 20602beb6614SMarc Eshel * 20612beb6614SMarc Eshel * To avoid blocking kernel daemons, such as lockd, that need to acquire POSIX 20622beb6614SMarc Eshel * locks, the ->lock() interface may return asynchronously, before the lock has 20632beb6614SMarc Eshel * been granted or denied by the underlying filesystem, if (and only if) 20648fb47a4fSJ. Bruce Fields * lm_grant is set. Callers expecting ->lock() to return asynchronously 20652beb6614SMarc Eshel * will only use F_SETLK, not F_SETLKW; they will set FL_SLEEP if (and only if) 20662beb6614SMarc Eshel * the request is for a blocking lock. When ->lock() does return asynchronously, 20678fb47a4fSJ. Bruce Fields * it must return FILE_LOCK_DEFERRED, and call ->lm_grant() when the lock 20682beb6614SMarc Eshel * request completes. 20692beb6614SMarc Eshel * If the request is for non-blocking lock the file system should return 2070bde74e4bSMiklos Szeredi * FILE_LOCK_DEFERRED then try to get the lock and call the callback routine 2071bde74e4bSMiklos Szeredi * with the result. If the request timed out the callback routine will return a 20722beb6614SMarc Eshel * nonzero return code and the file system should release the lock. The file 20732beb6614SMarc Eshel * system is also responsible to keep a corresponding posix lock when it 20742beb6614SMarc Eshel * grants a lock so the VFS can find out which locks are locally held and do 20752beb6614SMarc Eshel * the correct lock cleanup when required. 20762beb6614SMarc Eshel * The underlying filesystem must not drop the kernel lock or call 20778fb47a4fSJ. Bruce Fields * ->lm_grant() before returning to the caller with a FILE_LOCK_DEFERRED 20782beb6614SMarc Eshel * return code. 20797723ec97SMarc Eshel */ 2080150b3934SMarc Eshel int vfs_lock_file(struct file *filp, unsigned int cmd, struct file_lock *fl, struct file_lock *conf) 20817723ec97SMarc Eshel { 208272c2d531SAl Viro if (filp->f_op->lock) 20837723ec97SMarc Eshel return filp->f_op->lock(filp, cmd, fl); 20847723ec97SMarc Eshel else 2085150b3934SMarc Eshel return posix_lock_file(filp, fl, conf); 20867723ec97SMarc Eshel } 20877723ec97SMarc Eshel EXPORT_SYMBOL_GPL(vfs_lock_file); 20887723ec97SMarc Eshel 2089b648a6deSMiklos Szeredi static int do_lock_file_wait(struct file *filp, unsigned int cmd, 2090b648a6deSMiklos Szeredi struct file_lock *fl) 2091b648a6deSMiklos Szeredi { 2092b648a6deSMiklos Szeredi int error; 2093b648a6deSMiklos Szeredi 2094b648a6deSMiklos Szeredi error = security_file_lock(filp, fl->fl_type); 2095b648a6deSMiklos Szeredi if (error) 2096b648a6deSMiklos Szeredi return error; 2097b648a6deSMiklos Szeredi 2098b648a6deSMiklos Szeredi for (;;) { 2099764c76b3SMiklos Szeredi error = vfs_lock_file(filp, cmd, fl, NULL); 2100b648a6deSMiklos Szeredi if (error != FILE_LOCK_DEFERRED) 2101b648a6deSMiklos Szeredi break; 2102764c76b3SMiklos Szeredi error = wait_event_interruptible(fl->fl_wait, !fl->fl_next); 2103b648a6deSMiklos Szeredi if (!error) 2104b648a6deSMiklos Szeredi continue; 2105b648a6deSMiklos Szeredi 2106b648a6deSMiklos Szeredi locks_delete_block(fl); 2107b648a6deSMiklos Szeredi break; 2108b648a6deSMiklos Szeredi } 2109b648a6deSMiklos Szeredi 2110b648a6deSMiklos Szeredi return error; 2111b648a6deSMiklos Szeredi } 2112b648a6deSMiklos Szeredi 2113cf01f4eeSJeff Layton /* Ensure that fl->fl_filp has compatible f_mode for F_SETLK calls */ 2114cf01f4eeSJeff Layton static int 2115cf01f4eeSJeff Layton check_fmode_for_setlk(struct file_lock *fl) 2116cf01f4eeSJeff Layton { 2117cf01f4eeSJeff Layton switch (fl->fl_type) { 2118cf01f4eeSJeff Layton case F_RDLCK: 2119cf01f4eeSJeff Layton if (!(fl->fl_file->f_mode & FMODE_READ)) 2120cf01f4eeSJeff Layton return -EBADF; 2121cf01f4eeSJeff Layton break; 2122cf01f4eeSJeff Layton case F_WRLCK: 2123cf01f4eeSJeff Layton if (!(fl->fl_file->f_mode & FMODE_WRITE)) 2124cf01f4eeSJeff Layton return -EBADF; 2125cf01f4eeSJeff Layton } 2126cf01f4eeSJeff Layton return 0; 2127cf01f4eeSJeff Layton } 2128cf01f4eeSJeff Layton 21291da177e4SLinus Torvalds /* Apply the lock described by l to an open file descriptor. 21301da177e4SLinus Torvalds * This implements both the F_SETLK and F_SETLKW commands of fcntl(). 21311da177e4SLinus Torvalds */ 2132c293621bSPeter Staubach int fcntl_setlk(unsigned int fd, struct file *filp, unsigned int cmd, 2133c293621bSPeter Staubach struct flock __user *l) 21341da177e4SLinus Torvalds { 21351da177e4SLinus Torvalds struct file_lock *file_lock = locks_alloc_lock(); 21361da177e4SLinus Torvalds struct flock flock; 21371da177e4SLinus Torvalds struct inode *inode; 21380b2bac2fSAl Viro struct file *f; 21391da177e4SLinus Torvalds int error; 21401da177e4SLinus Torvalds 21411da177e4SLinus Torvalds if (file_lock == NULL) 21421da177e4SLinus Torvalds return -ENOLCK; 21431da177e4SLinus Torvalds 21441da177e4SLinus Torvalds /* 21451da177e4SLinus Torvalds * This might block, so we do it before checking the inode. 21461da177e4SLinus Torvalds */ 21471da177e4SLinus Torvalds error = -EFAULT; 21481da177e4SLinus Torvalds if (copy_from_user(&flock, l, sizeof(flock))) 21491da177e4SLinus Torvalds goto out; 21501da177e4SLinus Torvalds 2151496ad9aaSAl Viro inode = file_inode(filp); 21521da177e4SLinus Torvalds 21531da177e4SLinus Torvalds /* Don't allow mandatory locks on files that may be memory mapped 21541da177e4SLinus Torvalds * and shared. 21551da177e4SLinus Torvalds */ 2156a16877caSPavel Emelyanov if (mandatory_lock(inode) && mapping_writably_mapped(filp->f_mapping)) { 21571da177e4SLinus Torvalds error = -EAGAIN; 21581da177e4SLinus Torvalds goto out; 21591da177e4SLinus Torvalds } 21601da177e4SLinus Torvalds 2161c293621bSPeter Staubach again: 21621da177e4SLinus Torvalds error = flock_to_posix_lock(filp, file_lock, &flock); 21631da177e4SLinus Torvalds if (error) 21641da177e4SLinus Torvalds goto out; 21655d50ffd7SJeff Layton 2166cf01f4eeSJeff Layton error = check_fmode_for_setlk(file_lock); 2167cf01f4eeSJeff Layton if (error) 2168cf01f4eeSJeff Layton goto out; 2169cf01f4eeSJeff Layton 21705d50ffd7SJeff Layton /* 21715d50ffd7SJeff Layton * If the cmd is requesting file-private locks, then set the 2172cff2fce5SJeff Layton * FL_OFDLCK flag and override the owner. 21735d50ffd7SJeff Layton */ 21745d50ffd7SJeff Layton switch (cmd) { 21750d3f7a2dSJeff Layton case F_OFD_SETLK: 217690478939SJeff Layton error = -EINVAL; 217790478939SJeff Layton if (flock.l_pid != 0) 217890478939SJeff Layton goto out; 217990478939SJeff Layton 21805d50ffd7SJeff Layton cmd = F_SETLK; 2181cff2fce5SJeff Layton file_lock->fl_flags |= FL_OFDLCK; 218273a8f5f7SChristoph Hellwig file_lock->fl_owner = filp; 21835d50ffd7SJeff Layton break; 21840d3f7a2dSJeff Layton case F_OFD_SETLKW: 218590478939SJeff Layton error = -EINVAL; 218690478939SJeff Layton if (flock.l_pid != 0) 218790478939SJeff Layton goto out; 218890478939SJeff Layton 21895d50ffd7SJeff Layton cmd = F_SETLKW; 2190cff2fce5SJeff Layton file_lock->fl_flags |= FL_OFDLCK; 219173a8f5f7SChristoph Hellwig file_lock->fl_owner = filp; 21925d50ffd7SJeff Layton /* Fallthrough */ 21935d50ffd7SJeff Layton case F_SETLKW: 21941da177e4SLinus Torvalds file_lock->fl_flags |= FL_SLEEP; 21951da177e4SLinus Torvalds } 21961da177e4SLinus Torvalds 2197b648a6deSMiklos Szeredi error = do_lock_file_wait(filp, cmd, file_lock); 2198c293621bSPeter Staubach 2199c293621bSPeter Staubach /* 2200c293621bSPeter Staubach * Attempt to detect a close/fcntl race and recover by 2201c293621bSPeter Staubach * releasing the lock that was just acquired. 2202c293621bSPeter Staubach */ 22030b2bac2fSAl Viro /* 22040b2bac2fSAl Viro * we need that spin_lock here - it prevents reordering between 22058116bf4cSJeff Layton * update of i_flctx->flc_posix and check for it done in close(). 22060b2bac2fSAl Viro * rcu_read_lock() wouldn't do. 22070b2bac2fSAl Viro */ 22080b2bac2fSAl Viro spin_lock(¤t->files->file_lock); 22090b2bac2fSAl Viro f = fcheck(fd); 22100b2bac2fSAl Viro spin_unlock(¤t->files->file_lock); 22110b2bac2fSAl Viro if (!error && f != filp && flock.l_type != F_UNLCK) { 2212c293621bSPeter Staubach flock.l_type = F_UNLCK; 2213c293621bSPeter Staubach goto again; 2214c293621bSPeter Staubach } 22151da177e4SLinus Torvalds 22161da177e4SLinus Torvalds out: 22171da177e4SLinus Torvalds locks_free_lock(file_lock); 22181da177e4SLinus Torvalds return error; 22191da177e4SLinus Torvalds } 22201da177e4SLinus Torvalds 22211da177e4SLinus Torvalds #if BITS_PER_LONG == 32 22221da177e4SLinus Torvalds /* Report the first existing lock that would conflict with l. 22231da177e4SLinus Torvalds * This implements the F_GETLK command of fcntl(). 22241da177e4SLinus Torvalds */ 2225c1e62b8fSJeff Layton int fcntl_getlk64(struct file *filp, unsigned int cmd, struct flock64 __user *l) 22261da177e4SLinus Torvalds { 22279d6a8c5cSMarc Eshel struct file_lock file_lock; 22281da177e4SLinus Torvalds struct flock64 flock; 22291da177e4SLinus Torvalds int error; 22301da177e4SLinus Torvalds 22311da177e4SLinus Torvalds error = -EFAULT; 22321da177e4SLinus Torvalds if (copy_from_user(&flock, l, sizeof(flock))) 22331da177e4SLinus Torvalds goto out; 22341da177e4SLinus Torvalds error = -EINVAL; 22351da177e4SLinus Torvalds if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK)) 22361da177e4SLinus Torvalds goto out; 22371da177e4SLinus Torvalds 22381da177e4SLinus Torvalds error = flock64_to_posix_lock(filp, &file_lock, &flock); 22391da177e4SLinus Torvalds if (error) 22401da177e4SLinus Torvalds goto out; 22411da177e4SLinus Torvalds 22420d3f7a2dSJeff Layton if (cmd == F_OFD_GETLK) { 224390478939SJeff Layton error = -EINVAL; 224490478939SJeff Layton if (flock.l_pid != 0) 224590478939SJeff Layton goto out; 224690478939SJeff Layton 22475d50ffd7SJeff Layton cmd = F_GETLK64; 2248cff2fce5SJeff Layton file_lock.fl_flags |= FL_OFDLCK; 224973a8f5f7SChristoph Hellwig file_lock.fl_owner = filp; 22505d50ffd7SJeff Layton } 22515d50ffd7SJeff Layton 22523ee17abdSJ. Bruce Fields error = vfs_test_lock(filp, &file_lock); 22533ee17abdSJ. Bruce Fields if (error) 22541da177e4SLinus Torvalds goto out; 22551da177e4SLinus Torvalds 22569d6a8c5cSMarc Eshel flock.l_type = file_lock.fl_type; 22579d6a8c5cSMarc Eshel if (file_lock.fl_type != F_UNLCK) 22589d6a8c5cSMarc Eshel posix_lock_to_flock64(&flock, &file_lock); 22599d6a8c5cSMarc Eshel 22601da177e4SLinus Torvalds error = -EFAULT; 22611da177e4SLinus Torvalds if (!copy_to_user(l, &flock, sizeof(flock))) 22621da177e4SLinus Torvalds error = 0; 22631da177e4SLinus Torvalds 2264f328296eSKinglong Mee locks_release_private(&file_lock); 22651da177e4SLinus Torvalds out: 22661da177e4SLinus Torvalds return error; 22671da177e4SLinus Torvalds } 22681da177e4SLinus Torvalds 22691da177e4SLinus Torvalds /* Apply the lock described by l to an open file descriptor. 22701da177e4SLinus Torvalds * This implements both the F_SETLK and F_SETLKW commands of fcntl(). 22711da177e4SLinus Torvalds */ 2272c293621bSPeter Staubach int fcntl_setlk64(unsigned int fd, struct file *filp, unsigned int cmd, 2273c293621bSPeter Staubach struct flock64 __user *l) 22741da177e4SLinus Torvalds { 22751da177e4SLinus Torvalds struct file_lock *file_lock = locks_alloc_lock(); 22761da177e4SLinus Torvalds struct flock64 flock; 22771da177e4SLinus Torvalds struct inode *inode; 22780b2bac2fSAl Viro struct file *f; 22791da177e4SLinus Torvalds int error; 22801da177e4SLinus Torvalds 22811da177e4SLinus Torvalds if (file_lock == NULL) 22821da177e4SLinus Torvalds return -ENOLCK; 22831da177e4SLinus Torvalds 22841da177e4SLinus Torvalds /* 22851da177e4SLinus Torvalds * This might block, so we do it before checking the inode. 22861da177e4SLinus Torvalds */ 22871da177e4SLinus Torvalds error = -EFAULT; 22881da177e4SLinus Torvalds if (copy_from_user(&flock, l, sizeof(flock))) 22891da177e4SLinus Torvalds goto out; 22901da177e4SLinus Torvalds 2291496ad9aaSAl Viro inode = file_inode(filp); 22921da177e4SLinus Torvalds 22931da177e4SLinus Torvalds /* Don't allow mandatory locks on files that may be memory mapped 22941da177e4SLinus Torvalds * and shared. 22951da177e4SLinus Torvalds */ 2296a16877caSPavel Emelyanov if (mandatory_lock(inode) && mapping_writably_mapped(filp->f_mapping)) { 22971da177e4SLinus Torvalds error = -EAGAIN; 22981da177e4SLinus Torvalds goto out; 22991da177e4SLinus Torvalds } 23001da177e4SLinus Torvalds 2301c293621bSPeter Staubach again: 23021da177e4SLinus Torvalds error = flock64_to_posix_lock(filp, file_lock, &flock); 23031da177e4SLinus Torvalds if (error) 23041da177e4SLinus Torvalds goto out; 23055d50ffd7SJeff Layton 2306cf01f4eeSJeff Layton error = check_fmode_for_setlk(file_lock); 2307cf01f4eeSJeff Layton if (error) 2308cf01f4eeSJeff Layton goto out; 2309cf01f4eeSJeff Layton 23105d50ffd7SJeff Layton /* 23115d50ffd7SJeff Layton * If the cmd is requesting file-private locks, then set the 2312cff2fce5SJeff Layton * FL_OFDLCK flag and override the owner. 23135d50ffd7SJeff Layton */ 23145d50ffd7SJeff Layton switch (cmd) { 23150d3f7a2dSJeff Layton case F_OFD_SETLK: 231690478939SJeff Layton error = -EINVAL; 231790478939SJeff Layton if (flock.l_pid != 0) 231890478939SJeff Layton goto out; 231990478939SJeff Layton 23205d50ffd7SJeff Layton cmd = F_SETLK64; 2321cff2fce5SJeff Layton file_lock->fl_flags |= FL_OFDLCK; 232273a8f5f7SChristoph Hellwig file_lock->fl_owner = filp; 23235d50ffd7SJeff Layton break; 23240d3f7a2dSJeff Layton case F_OFD_SETLKW: 232590478939SJeff Layton error = -EINVAL; 232690478939SJeff Layton if (flock.l_pid != 0) 232790478939SJeff Layton goto out; 232890478939SJeff Layton 23295d50ffd7SJeff Layton cmd = F_SETLKW64; 2330cff2fce5SJeff Layton file_lock->fl_flags |= FL_OFDLCK; 233173a8f5f7SChristoph Hellwig file_lock->fl_owner = filp; 23325d50ffd7SJeff Layton /* Fallthrough */ 23335d50ffd7SJeff Layton case F_SETLKW64: 23341da177e4SLinus Torvalds file_lock->fl_flags |= FL_SLEEP; 23351da177e4SLinus Torvalds } 23361da177e4SLinus Torvalds 2337b648a6deSMiklos Szeredi error = do_lock_file_wait(filp, cmd, file_lock); 2338c293621bSPeter Staubach 2339c293621bSPeter Staubach /* 2340c293621bSPeter Staubach * Attempt to detect a close/fcntl race and recover by 2341c293621bSPeter Staubach * releasing the lock that was just acquired. 2342c293621bSPeter Staubach */ 23430b2bac2fSAl Viro spin_lock(¤t->files->file_lock); 23440b2bac2fSAl Viro f = fcheck(fd); 23450b2bac2fSAl Viro spin_unlock(¤t->files->file_lock); 23460b2bac2fSAl Viro if (!error && f != filp && flock.l_type != F_UNLCK) { 2347c293621bSPeter Staubach flock.l_type = F_UNLCK; 2348c293621bSPeter Staubach goto again; 2349c293621bSPeter Staubach } 23501da177e4SLinus Torvalds 23511da177e4SLinus Torvalds out: 23521da177e4SLinus Torvalds locks_free_lock(file_lock); 23531da177e4SLinus Torvalds return error; 23541da177e4SLinus Torvalds } 23551da177e4SLinus Torvalds #endif /* BITS_PER_LONG == 32 */ 23561da177e4SLinus Torvalds 23571da177e4SLinus Torvalds /* 23581da177e4SLinus Torvalds * This function is called when the file is being removed 23591da177e4SLinus Torvalds * from the task's fd array. POSIX locks belonging to this task 23601da177e4SLinus Torvalds * are deleted at this time. 23611da177e4SLinus Torvalds */ 23621da177e4SLinus Torvalds void locks_remove_posix(struct file *filp, fl_owner_t owner) 23631da177e4SLinus Torvalds { 2364ff7b86b8SMiklos Szeredi struct file_lock lock; 2365bd61e0a9SJeff Layton struct file_lock_context *ctx = file_inode(filp)->i_flctx; 23661da177e4SLinus Torvalds 23671da177e4SLinus Torvalds /* 23681da177e4SLinus Torvalds * If there are no locks held on this file, we don't need to call 23691da177e4SLinus Torvalds * posix_lock_file(). Another process could be setting a lock on this 23701da177e4SLinus Torvalds * file at the same time, but we wouldn't remove that lock anyway. 23711da177e4SLinus Torvalds */ 2372bd61e0a9SJeff Layton if (!ctx || list_empty(&ctx->flc_posix)) 23731da177e4SLinus Torvalds return; 23741da177e4SLinus Torvalds 23751da177e4SLinus Torvalds lock.fl_type = F_UNLCK; 237675e1fcc0SMiklos Szeredi lock.fl_flags = FL_POSIX | FL_CLOSE; 23771da177e4SLinus Torvalds lock.fl_start = 0; 23781da177e4SLinus Torvalds lock.fl_end = OFFSET_MAX; 23791da177e4SLinus Torvalds lock.fl_owner = owner; 23801da177e4SLinus Torvalds lock.fl_pid = current->tgid; 23811da177e4SLinus Torvalds lock.fl_file = filp; 23821da177e4SLinus Torvalds lock.fl_ops = NULL; 23831da177e4SLinus Torvalds lock.fl_lmops = NULL; 23841da177e4SLinus Torvalds 2385150b3934SMarc Eshel vfs_lock_file(filp, F_SETLK, &lock, NULL); 23861da177e4SLinus Torvalds 23871da177e4SLinus Torvalds if (lock.fl_ops && lock.fl_ops->fl_release_private) 23881da177e4SLinus Torvalds lock.fl_ops->fl_release_private(&lock); 23891da177e4SLinus Torvalds } 23901da177e4SLinus Torvalds 23911da177e4SLinus Torvalds EXPORT_SYMBOL(locks_remove_posix); 23921da177e4SLinus Torvalds 23933d8e560dSJeff Layton /* The i_flctx must be valid when calling into here */ 2394dd459bb1SJeff Layton static void 2395dd459bb1SJeff Layton locks_remove_flock(struct file *filp) 2396dd459bb1SJeff Layton { 2397dd459bb1SJeff Layton struct file_lock fl = { 2398dd459bb1SJeff Layton .fl_owner = filp, 2399dd459bb1SJeff Layton .fl_pid = current->tgid, 2400dd459bb1SJeff Layton .fl_file = filp, 2401dd459bb1SJeff Layton .fl_flags = FL_FLOCK, 2402dd459bb1SJeff Layton .fl_type = F_UNLCK, 2403dd459bb1SJeff Layton .fl_end = OFFSET_MAX, 2404dd459bb1SJeff Layton }; 24055263e31eSJeff Layton struct file_lock_context *flctx = file_inode(filp)->i_flctx; 2406dd459bb1SJeff Layton 24073d8e560dSJeff Layton if (list_empty(&flctx->flc_flock)) 2408dd459bb1SJeff Layton return; 2409dd459bb1SJeff Layton 2410dd459bb1SJeff Layton if (filp->f_op->flock) 2411dd459bb1SJeff Layton filp->f_op->flock(filp, F_SETLKW, &fl); 2412dd459bb1SJeff Layton else 2413dd459bb1SJeff Layton flock_lock_file(filp, &fl); 2414dd459bb1SJeff Layton 2415dd459bb1SJeff Layton if (fl.fl_ops && fl.fl_ops->fl_release_private) 2416dd459bb1SJeff Layton fl.fl_ops->fl_release_private(&fl); 2417dd459bb1SJeff Layton } 2418dd459bb1SJeff Layton 24193d8e560dSJeff Layton /* The i_flctx must be valid when calling into here */ 24208634b51fSJeff Layton static void 24218634b51fSJeff Layton locks_remove_lease(struct file *filp) 24228634b51fSJeff Layton { 24238634b51fSJeff Layton struct inode *inode = file_inode(filp); 24248634b51fSJeff Layton struct file_lock_context *ctx = inode->i_flctx; 24258634b51fSJeff Layton struct file_lock *fl, *tmp; 24268634b51fSJeff Layton LIST_HEAD(dispose); 24278634b51fSJeff Layton 24283d8e560dSJeff Layton if (list_empty(&ctx->flc_lease)) 24298634b51fSJeff Layton return; 24308634b51fSJeff Layton 24316109c850SJeff Layton spin_lock(&ctx->flc_lock); 24328634b51fSJeff Layton list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list) 2433c4e136cdSJeff Layton if (filp == fl->fl_file) 24347448cc37SJeff Layton lease_modify(fl, F_UNLCK, &dispose); 24356109c850SJeff Layton spin_unlock(&ctx->flc_lock); 24368634b51fSJeff Layton locks_dispose_list(&dispose); 24378634b51fSJeff Layton } 24388634b51fSJeff Layton 24391da177e4SLinus Torvalds /* 24401da177e4SLinus Torvalds * This function is called on the last close of an open file. 24411da177e4SLinus Torvalds */ 244278ed8a13SJeff Layton void locks_remove_file(struct file *filp) 24431da177e4SLinus Torvalds { 24443d8e560dSJeff Layton if (!file_inode(filp)->i_flctx) 24453d8e560dSJeff Layton return; 24463d8e560dSJeff Layton 2447dd459bb1SJeff Layton /* remove any OFD locks */ 244873a8f5f7SChristoph Hellwig locks_remove_posix(filp, filp); 24495d50ffd7SJeff Layton 2450dd459bb1SJeff Layton /* remove flock locks */ 2451dd459bb1SJeff Layton locks_remove_flock(filp); 2452dd459bb1SJeff Layton 24538634b51fSJeff Layton /* remove any leases */ 24548634b51fSJeff Layton locks_remove_lease(filp); 24551da177e4SLinus Torvalds } 24561da177e4SLinus Torvalds 24571da177e4SLinus Torvalds /** 24581da177e4SLinus Torvalds * posix_unblock_lock - stop waiting for a file lock 24591da177e4SLinus Torvalds * @waiter: the lock which was waiting 24601da177e4SLinus Torvalds * 24611da177e4SLinus Torvalds * lockd needs to block waiting for locks. 24621da177e4SLinus Torvalds */ 246364a318eeSJ. Bruce Fields int 2464f891a29fSJeff Layton posix_unblock_lock(struct file_lock *waiter) 24651da177e4SLinus Torvalds { 246664a318eeSJ. Bruce Fields int status = 0; 246764a318eeSJ. Bruce Fields 24687b2296afSJeff Layton spin_lock(&blocked_lock_lock); 24695996a298SJ. Bruce Fields if (waiter->fl_next) 24701da177e4SLinus Torvalds __locks_delete_block(waiter); 247164a318eeSJ. Bruce Fields else 247264a318eeSJ. Bruce Fields status = -ENOENT; 24737b2296afSJeff Layton spin_unlock(&blocked_lock_lock); 247464a318eeSJ. Bruce Fields return status; 24751da177e4SLinus Torvalds } 24761da177e4SLinus Torvalds EXPORT_SYMBOL(posix_unblock_lock); 24771da177e4SLinus Torvalds 24789b9d2ab4SMarc Eshel /** 24799b9d2ab4SMarc Eshel * vfs_cancel_lock - file byte range unblock lock 24809b9d2ab4SMarc Eshel * @filp: The file to apply the unblock to 24819b9d2ab4SMarc Eshel * @fl: The lock to be unblocked 24829b9d2ab4SMarc Eshel * 24839b9d2ab4SMarc Eshel * Used by lock managers to cancel blocked requests 24849b9d2ab4SMarc Eshel */ 24859b9d2ab4SMarc Eshel int vfs_cancel_lock(struct file *filp, struct file_lock *fl) 24869b9d2ab4SMarc Eshel { 248772c2d531SAl Viro if (filp->f_op->lock) 24889b9d2ab4SMarc Eshel return filp->f_op->lock(filp, F_CANCELLK, fl); 24899b9d2ab4SMarc Eshel return 0; 24909b9d2ab4SMarc Eshel } 24919b9d2ab4SMarc Eshel 24929b9d2ab4SMarc Eshel EXPORT_SYMBOL_GPL(vfs_cancel_lock); 24939b9d2ab4SMarc Eshel 24947f8ada98SPavel Emelyanov #ifdef CONFIG_PROC_FS 2495d8ba7a36SAlexey Dobriyan #include <linux/proc_fs.h> 24967f8ada98SPavel Emelyanov #include <linux/seq_file.h> 24977f8ada98SPavel Emelyanov 24987012b02aSJeff Layton struct locks_iterator { 24997012b02aSJeff Layton int li_cpu; 25007012b02aSJeff Layton loff_t li_pos; 25017012b02aSJeff Layton }; 25027012b02aSJeff Layton 25037f8ada98SPavel Emelyanov static void lock_get_status(struct seq_file *f, struct file_lock *fl, 250499dc8292SJerome Marchand loff_t id, char *pfx) 25051da177e4SLinus Torvalds { 25061da177e4SLinus Torvalds struct inode *inode = NULL; 2507ab1f1611SVitaliy Gusev unsigned int fl_pid; 2508ab1f1611SVitaliy Gusev 2509ab1f1611SVitaliy Gusev if (fl->fl_nspid) 25106c5f3e7bSPavel Emelyanov fl_pid = pid_vnr(fl->fl_nspid); 2511ab1f1611SVitaliy Gusev else 2512ab1f1611SVitaliy Gusev fl_pid = fl->fl_pid; 25131da177e4SLinus Torvalds 25141da177e4SLinus Torvalds if (fl->fl_file != NULL) 2515496ad9aaSAl Viro inode = file_inode(fl->fl_file); 25161da177e4SLinus Torvalds 251799dc8292SJerome Marchand seq_printf(f, "%lld:%s ", id, pfx); 25181da177e4SLinus Torvalds if (IS_POSIX(fl)) { 2519c918d42aSJeff Layton if (fl->fl_flags & FL_ACCESS) 25205315c26aSFabian Frederick seq_puts(f, "ACCESS"); 2521cff2fce5SJeff Layton else if (IS_OFDLCK(fl)) 25225315c26aSFabian Frederick seq_puts(f, "OFDLCK"); 2523c918d42aSJeff Layton else 25245315c26aSFabian Frederick seq_puts(f, "POSIX "); 2525c918d42aSJeff Layton 2526c918d42aSJeff Layton seq_printf(f, " %s ", 25271da177e4SLinus Torvalds (inode == NULL) ? "*NOINODE*" : 2528a16877caSPavel Emelyanov mandatory_lock(inode) ? "MANDATORY" : "ADVISORY "); 25291da177e4SLinus Torvalds } else if (IS_FLOCK(fl)) { 25301da177e4SLinus Torvalds if (fl->fl_type & LOCK_MAND) { 25315315c26aSFabian Frederick seq_puts(f, "FLOCK MSNFS "); 25321da177e4SLinus Torvalds } else { 25335315c26aSFabian Frederick seq_puts(f, "FLOCK ADVISORY "); 25341da177e4SLinus Torvalds } 25351da177e4SLinus Torvalds } else if (IS_LEASE(fl)) { 25368144f1f6SJeff Layton if (fl->fl_flags & FL_DELEG) 25378144f1f6SJeff Layton seq_puts(f, "DELEG "); 25388144f1f6SJeff Layton else 25395315c26aSFabian Frederick seq_puts(f, "LEASE "); 25408144f1f6SJeff Layton 2541ab83fa4bSJ. Bruce Fields if (lease_breaking(fl)) 25425315c26aSFabian Frederick seq_puts(f, "BREAKING "); 25431da177e4SLinus Torvalds else if (fl->fl_file) 25445315c26aSFabian Frederick seq_puts(f, "ACTIVE "); 25451da177e4SLinus Torvalds else 25465315c26aSFabian Frederick seq_puts(f, "BREAKER "); 25471da177e4SLinus Torvalds } else { 25485315c26aSFabian Frederick seq_puts(f, "UNKNOWN UNKNOWN "); 25491da177e4SLinus Torvalds } 25501da177e4SLinus Torvalds if (fl->fl_type & LOCK_MAND) { 25517f8ada98SPavel Emelyanov seq_printf(f, "%s ", 25521da177e4SLinus Torvalds (fl->fl_type & LOCK_READ) 25531da177e4SLinus Torvalds ? (fl->fl_type & LOCK_WRITE) ? "RW " : "READ " 25541da177e4SLinus Torvalds : (fl->fl_type & LOCK_WRITE) ? "WRITE" : "NONE "); 25551da177e4SLinus Torvalds } else { 25567f8ada98SPavel Emelyanov seq_printf(f, "%s ", 2557ab83fa4bSJ. Bruce Fields (lease_breaking(fl)) 25580ee5c6d6SJeff Layton ? (fl->fl_type == F_UNLCK) ? "UNLCK" : "READ " 25590ee5c6d6SJeff Layton : (fl->fl_type == F_WRLCK) ? "WRITE" : "READ "); 25601da177e4SLinus Torvalds } 25611da177e4SLinus Torvalds if (inode) { 25621da177e4SLinus Torvalds #ifdef WE_CAN_BREAK_LSLK_NOW 2563ab1f1611SVitaliy Gusev seq_printf(f, "%d %s:%ld ", fl_pid, 25641da177e4SLinus Torvalds inode->i_sb->s_id, inode->i_ino); 25651da177e4SLinus Torvalds #else 25661da177e4SLinus Torvalds /* userspace relies on this representation of dev_t ;-( */ 2567ab1f1611SVitaliy Gusev seq_printf(f, "%d %02x:%02x:%ld ", fl_pid, 25681da177e4SLinus Torvalds MAJOR(inode->i_sb->s_dev), 25691da177e4SLinus Torvalds MINOR(inode->i_sb->s_dev), inode->i_ino); 25701da177e4SLinus Torvalds #endif 25711da177e4SLinus Torvalds } else { 2572ab1f1611SVitaliy Gusev seq_printf(f, "%d <none>:0 ", fl_pid); 25731da177e4SLinus Torvalds } 25741da177e4SLinus Torvalds if (IS_POSIX(fl)) { 25751da177e4SLinus Torvalds if (fl->fl_end == OFFSET_MAX) 25767f8ada98SPavel Emelyanov seq_printf(f, "%Ld EOF\n", fl->fl_start); 25771da177e4SLinus Torvalds else 25787f8ada98SPavel Emelyanov seq_printf(f, "%Ld %Ld\n", fl->fl_start, fl->fl_end); 25791da177e4SLinus Torvalds } else { 25805315c26aSFabian Frederick seq_puts(f, "0 EOF\n"); 25811da177e4SLinus Torvalds } 25821da177e4SLinus Torvalds } 25831da177e4SLinus Torvalds 25847f8ada98SPavel Emelyanov static int locks_show(struct seq_file *f, void *v) 25851da177e4SLinus Torvalds { 25867012b02aSJeff Layton struct locks_iterator *iter = f->private; 25877f8ada98SPavel Emelyanov struct file_lock *fl, *bfl; 25887f8ada98SPavel Emelyanov 2589139ca04eSJeff Layton fl = hlist_entry(v, struct file_lock, fl_link); 25907f8ada98SPavel Emelyanov 25917012b02aSJeff Layton lock_get_status(f, fl, iter->li_pos, ""); 25927f8ada98SPavel Emelyanov 25937f8ada98SPavel Emelyanov list_for_each_entry(bfl, &fl->fl_block, fl_block) 25947012b02aSJeff Layton lock_get_status(f, bfl, iter->li_pos, " ->"); 25957f8ada98SPavel Emelyanov 25967f8ada98SPavel Emelyanov return 0; 25971da177e4SLinus Torvalds } 25981da177e4SLinus Torvalds 25997f8ada98SPavel Emelyanov static void *locks_start(struct seq_file *f, loff_t *pos) 2600b03dfdecSJeff Layton __acquires(&blocked_lock_lock) 26011da177e4SLinus Torvalds { 26027012b02aSJeff Layton struct locks_iterator *iter = f->private; 260399dc8292SJerome Marchand 26047012b02aSJeff Layton iter->li_pos = *pos + 1; 26057012b02aSJeff Layton lg_global_lock(&file_lock_lglock); 26067b2296afSJeff Layton spin_lock(&blocked_lock_lock); 26077012b02aSJeff Layton return seq_hlist_start_percpu(&file_lock_list, &iter->li_cpu, *pos); 26081da177e4SLinus Torvalds } 26097f8ada98SPavel Emelyanov 26107f8ada98SPavel Emelyanov static void *locks_next(struct seq_file *f, void *v, loff_t *pos) 26117f8ada98SPavel Emelyanov { 26127012b02aSJeff Layton struct locks_iterator *iter = f->private; 26137012b02aSJeff Layton 26147012b02aSJeff Layton ++iter->li_pos; 26157012b02aSJeff Layton return seq_hlist_next_percpu(v, &file_lock_list, &iter->li_cpu, pos); 26161da177e4SLinus Torvalds } 26177f8ada98SPavel Emelyanov 26187f8ada98SPavel Emelyanov static void locks_stop(struct seq_file *f, void *v) 2619b03dfdecSJeff Layton __releases(&blocked_lock_lock) 26207f8ada98SPavel Emelyanov { 26217b2296afSJeff Layton spin_unlock(&blocked_lock_lock); 26227012b02aSJeff Layton lg_global_unlock(&file_lock_lglock); 26231da177e4SLinus Torvalds } 26241da177e4SLinus Torvalds 2625d8ba7a36SAlexey Dobriyan static const struct seq_operations locks_seq_operations = { 26267f8ada98SPavel Emelyanov .start = locks_start, 26277f8ada98SPavel Emelyanov .next = locks_next, 26287f8ada98SPavel Emelyanov .stop = locks_stop, 26297f8ada98SPavel Emelyanov .show = locks_show, 26307f8ada98SPavel Emelyanov }; 2631d8ba7a36SAlexey Dobriyan 2632d8ba7a36SAlexey Dobriyan static int locks_open(struct inode *inode, struct file *filp) 2633d8ba7a36SAlexey Dobriyan { 26347012b02aSJeff Layton return seq_open_private(filp, &locks_seq_operations, 26357012b02aSJeff Layton sizeof(struct locks_iterator)); 2636d8ba7a36SAlexey Dobriyan } 2637d8ba7a36SAlexey Dobriyan 2638d8ba7a36SAlexey Dobriyan static const struct file_operations proc_locks_operations = { 2639d8ba7a36SAlexey Dobriyan .open = locks_open, 2640d8ba7a36SAlexey Dobriyan .read = seq_read, 2641d8ba7a36SAlexey Dobriyan .llseek = seq_lseek, 264299dc8292SJerome Marchand .release = seq_release_private, 2643d8ba7a36SAlexey Dobriyan }; 2644d8ba7a36SAlexey Dobriyan 2645d8ba7a36SAlexey Dobriyan static int __init proc_locks_init(void) 2646d8ba7a36SAlexey Dobriyan { 2647d8ba7a36SAlexey Dobriyan proc_create("locks", 0, NULL, &proc_locks_operations); 2648d8ba7a36SAlexey Dobriyan return 0; 2649d8ba7a36SAlexey Dobriyan } 2650d8ba7a36SAlexey Dobriyan module_init(proc_locks_init); 26517f8ada98SPavel Emelyanov #endif 26527f8ada98SPavel Emelyanov 26531da177e4SLinus Torvalds static int __init filelock_init(void) 26541da177e4SLinus Torvalds { 26557012b02aSJeff Layton int i; 26567012b02aSJeff Layton 26574a075e39SJeff Layton flctx_cache = kmem_cache_create("file_lock_ctx", 26584a075e39SJeff Layton sizeof(struct file_lock_context), 0, SLAB_PANIC, NULL); 26594a075e39SJeff Layton 26601da177e4SLinus Torvalds filelock_cache = kmem_cache_create("file_lock_cache", 2661ee19cc40SMiklos Szeredi sizeof(struct file_lock), 0, SLAB_PANIC, NULL); 2662ee19cc40SMiklos Szeredi 26637012b02aSJeff Layton lg_lock_init(&file_lock_lglock, "file_lock_lglock"); 26647012b02aSJeff Layton 26657012b02aSJeff Layton for_each_possible_cpu(i) 26667012b02aSJeff Layton INIT_HLIST_HEAD(per_cpu_ptr(&file_lock_list, i)); 26677012b02aSJeff Layton 26681da177e4SLinus Torvalds return 0; 26691da177e4SLinus Torvalds } 26701da177e4SLinus Torvalds 26711da177e4SLinus Torvalds core_initcall(filelock_init); 2672