1 /* 2 * linux/fs/locks.c 3 * 4 * Provide support for fcntl()'s F_GETLK, F_SETLK, and F_SETLKW calls. 5 * Doug Evans (dje@spiff.uucp), August 07, 1992 6 * 7 * Deadlock detection added. 8 * FIXME: one thing isn't handled yet: 9 * - mandatory locks (requires lots of changes elsewhere) 10 * Kelly Carmichael (kelly@[142.24.8.65]), September 17, 1994. 11 * 12 * Miscellaneous edits, and a total rewrite of posix_lock_file() code. 13 * Kai Petzke (wpp@marie.physik.tu-berlin.de), 1994 14 * 15 * Converted file_lock_table to a linked list from an array, which eliminates 16 * the limits on how many active file locks are open. 17 * Chad Page (pageone@netcom.com), November 27, 1994 18 * 19 * Removed dependency on file descriptors. dup()'ed file descriptors now 20 * get the same locks as the original file descriptors, and a close() on 21 * any file descriptor removes ALL the locks on the file for the current 22 * process. Since locks still depend on the process id, locks are inherited 23 * after an exec() but not after a fork(). This agrees with POSIX, and both 24 * BSD and SVR4 practice. 25 * Andy Walker (andy@lysaker.kvaerner.no), February 14, 1995 26 * 27 * Scrapped free list which is redundant now that we allocate locks 28 * dynamically with kmalloc()/kfree(). 29 * Andy Walker (andy@lysaker.kvaerner.no), February 21, 1995 30 * 31 * Implemented two lock personalities - FL_FLOCK and FL_POSIX. 32 * 33 * FL_POSIX locks are created with calls to fcntl() and lockf() through the 34 * fcntl() system call. They have the semantics described above. 35 * 36 * FL_FLOCK locks are created with calls to flock(), through the flock() 37 * system call, which is new. Old C libraries implement flock() via fcntl() 38 * and will continue to use the old, broken implementation. 39 * 40 * FL_FLOCK locks follow the 4.4 BSD flock() semantics. They are associated 41 * with a file pointer (filp). As a result they can be shared by a parent 42 * process and its children after a fork(). They are removed when the last 43 * file descriptor referring to the file pointer is closed (unless explicitly 44 * unlocked). 45 * 46 * FL_FLOCK locks never deadlock, an existing lock is always removed before 47 * upgrading from shared to exclusive (or vice versa). When this happens 48 * any processes blocked by the current lock are woken up and allowed to 49 * run before the new lock is applied. 50 * Andy Walker (andy@lysaker.kvaerner.no), June 09, 1995 51 * 52 * Removed some race conditions in flock_lock_file(), marked other possible 53 * races. Just grep for FIXME to see them. 54 * Dmitry Gorodchanin (pgmdsg@ibi.com), February 09, 1996. 55 * 56 * Addressed Dmitry's concerns. Deadlock checking no longer recursive. 57 * Lock allocation changed to GFP_ATOMIC as we can't afford to sleep 58 * once we've checked for blocking and deadlocking. 59 * Andy Walker (andy@lysaker.kvaerner.no), April 03, 1996. 60 * 61 * Initial implementation of mandatory locks. SunOS turned out to be 62 * a rotten model, so I implemented the "obvious" semantics. 63 * See 'Documentation/filesystems/mandatory-locking.txt' for details. 64 * Andy Walker (andy@lysaker.kvaerner.no), April 06, 1996. 65 * 66 * Don't allow mandatory locks on mmap()'ed files. Added simple functions to 67 * check if a file has mandatory locks, used by mmap(), open() and creat() to 68 * see if system call should be rejected. Ref. HP-UX/SunOS/Solaris Reference 69 * Manual, Section 2. 70 * Andy Walker (andy@lysaker.kvaerner.no), April 09, 1996. 71 * 72 * Tidied up block list handling. Added '/proc/locks' interface. 73 * Andy Walker (andy@lysaker.kvaerner.no), April 24, 1996. 74 * 75 * Fixed deadlock condition for pathological code that mixes calls to 76 * flock() and fcntl(). 77 * Andy Walker (andy@lysaker.kvaerner.no), April 29, 1996. 78 * 79 * Allow only one type of locking scheme (FL_POSIX or FL_FLOCK) to be in use 80 * for a given file at a time. Changed the CONFIG_LOCK_MANDATORY scheme to 81 * guarantee sensible behaviour in the case where file system modules might 82 * be compiled with different options than the kernel itself. 83 * Andy Walker (andy@lysaker.kvaerner.no), May 15, 1996. 84 * 85 * Added a couple of missing wake_up() calls. Thanks to Thomas Meckel 86 * (Thomas.Meckel@mni.fh-giessen.de) for spotting this. 87 * Andy Walker (andy@lysaker.kvaerner.no), May 15, 1996. 88 * 89 * Changed FL_POSIX locks to use the block list in the same way as FL_FLOCK 90 * locks. Changed process synchronisation to avoid dereferencing locks that 91 * have already been freed. 92 * Andy Walker (andy@lysaker.kvaerner.no), Sep 21, 1996. 93 * 94 * Made the block list a circular list to minimise searching in the list. 95 * Andy Walker (andy@lysaker.kvaerner.no), Sep 25, 1996. 96 * 97 * Made mandatory locking a mount option. Default is not to allow mandatory 98 * locking. 99 * Andy Walker (andy@lysaker.kvaerner.no), Oct 04, 1996. 100 * 101 * Some adaptations for NFS support. 102 * Olaf Kirch (okir@monad.swb.de), Dec 1996, 103 * 104 * Fixed /proc/locks interface so that we can't overrun the buffer we are handed. 105 * Andy Walker (andy@lysaker.kvaerner.no), May 12, 1997. 106 * 107 * Use slab allocator instead of kmalloc/kfree. 108 * Use generic list implementation from <linux/list.h>. 109 * Sped up posix_locks_deadlock by only considering blocked locks. 110 * Matthew Wilcox <willy@debian.org>, March, 2000. 111 * 112 * Leases and LOCK_MAND 113 * Matthew Wilcox <willy@debian.org>, June, 2000. 114 * Stephen Rothwell <sfr@canb.auug.org.au>, June, 2000. 115 */ 116 117 #include <linux/capability.h> 118 #include <linux/file.h> 119 #include <linux/fdtable.h> 120 #include <linux/fs.h> 121 #include <linux/init.h> 122 #include <linux/security.h> 123 #include <linux/slab.h> 124 #include <linux/syscalls.h> 125 #include <linux/time.h> 126 #include <linux/rcupdate.h> 127 #include <linux/pid_namespace.h> 128 #include <linux/hashtable.h> 129 #include <linux/percpu.h> 130 131 #define CREATE_TRACE_POINTS 132 #include <trace/events/filelock.h> 133 134 #include <linux/uaccess.h> 135 136 #define IS_POSIX(fl) (fl->fl_flags & FL_POSIX) 137 #define IS_FLOCK(fl) (fl->fl_flags & FL_FLOCK) 138 #define IS_LEASE(fl) (fl->fl_flags & (FL_LEASE|FL_DELEG|FL_LAYOUT)) 139 #define IS_OFDLCK(fl) (fl->fl_flags & FL_OFDLCK) 140 #define IS_REMOTELCK(fl) (fl->fl_pid <= 0) 141 142 static inline bool is_remote_lock(struct file *filp) 143 { 144 return likely(!(filp->f_path.dentry->d_sb->s_flags & SB_NOREMOTELOCK)); 145 } 146 147 static bool lease_breaking(struct file_lock *fl) 148 { 149 return fl->fl_flags & (FL_UNLOCK_PENDING | FL_DOWNGRADE_PENDING); 150 } 151 152 static int target_leasetype(struct file_lock *fl) 153 { 154 if (fl->fl_flags & FL_UNLOCK_PENDING) 155 return F_UNLCK; 156 if (fl->fl_flags & FL_DOWNGRADE_PENDING) 157 return F_RDLCK; 158 return fl->fl_type; 159 } 160 161 int leases_enable = 1; 162 int lease_break_time = 45; 163 164 /* 165 * The global file_lock_list is only used for displaying /proc/locks, so we 166 * keep a list on each CPU, with each list protected by its own spinlock. 167 * Global serialization is done using file_rwsem. 168 * 169 * Note that alterations to the list also require that the relevant flc_lock is 170 * held. 171 */ 172 struct file_lock_list_struct { 173 spinlock_t lock; 174 struct hlist_head hlist; 175 }; 176 static DEFINE_PER_CPU(struct file_lock_list_struct, file_lock_list); 177 DEFINE_STATIC_PERCPU_RWSEM(file_rwsem); 178 179 /* 180 * The blocked_hash is used to find POSIX lock loops for deadlock detection. 181 * It is protected by blocked_lock_lock. 182 * 183 * We hash locks by lockowner in order to optimize searching for the lock a 184 * particular lockowner is waiting on. 185 * 186 * FIXME: make this value scale via some heuristic? We generally will want more 187 * buckets when we have more lockowners holding locks, but that's a little 188 * difficult to determine without knowing what the workload will look like. 189 */ 190 #define BLOCKED_HASH_BITS 7 191 static DEFINE_HASHTABLE(blocked_hash, BLOCKED_HASH_BITS); 192 193 /* 194 * This lock protects the blocked_hash. Generally, if you're accessing it, you 195 * want to be holding this lock. 196 * 197 * In addition, it also protects the fl->fl_block list, and the fl->fl_next 198 * pointer for file_lock structures that are acting as lock requests (in 199 * contrast to those that are acting as records of acquired locks). 200 * 201 * Note that when we acquire this lock in order to change the above fields, 202 * we often hold the flc_lock as well. In certain cases, when reading the fields 203 * protected by this lock, we can skip acquiring it iff we already hold the 204 * flc_lock. 205 */ 206 static DEFINE_SPINLOCK(blocked_lock_lock); 207 208 static struct kmem_cache *flctx_cache __read_mostly; 209 static struct kmem_cache *filelock_cache __read_mostly; 210 211 static struct file_lock_context * 212 locks_get_lock_context(struct inode *inode, int type) 213 { 214 struct file_lock_context *ctx; 215 216 /* paired with cmpxchg() below */ 217 ctx = smp_load_acquire(&inode->i_flctx); 218 if (likely(ctx) || type == F_UNLCK) 219 goto out; 220 221 ctx = kmem_cache_alloc(flctx_cache, GFP_KERNEL); 222 if (!ctx) 223 goto out; 224 225 spin_lock_init(&ctx->flc_lock); 226 INIT_LIST_HEAD(&ctx->flc_flock); 227 INIT_LIST_HEAD(&ctx->flc_posix); 228 INIT_LIST_HEAD(&ctx->flc_lease); 229 230 /* 231 * Assign the pointer if it's not already assigned. If it is, then 232 * free the context we just allocated. 233 */ 234 if (cmpxchg(&inode->i_flctx, NULL, ctx)) { 235 kmem_cache_free(flctx_cache, ctx); 236 ctx = smp_load_acquire(&inode->i_flctx); 237 } 238 out: 239 trace_locks_get_lock_context(inode, type, ctx); 240 return ctx; 241 } 242 243 static void 244 locks_dump_ctx_list(struct list_head *list, char *list_type) 245 { 246 struct file_lock *fl; 247 248 list_for_each_entry(fl, list, fl_list) { 249 pr_warn("%s: fl_owner=%p fl_flags=0x%x fl_type=0x%x fl_pid=%u\n", list_type, fl->fl_owner, fl->fl_flags, fl->fl_type, fl->fl_pid); 250 } 251 } 252 253 static void 254 locks_check_ctx_lists(struct inode *inode) 255 { 256 struct file_lock_context *ctx = inode->i_flctx; 257 258 if (unlikely(!list_empty(&ctx->flc_flock) || 259 !list_empty(&ctx->flc_posix) || 260 !list_empty(&ctx->flc_lease))) { 261 pr_warn("Leaked locks on dev=0x%x:0x%x ino=0x%lx:\n", 262 MAJOR(inode->i_sb->s_dev), MINOR(inode->i_sb->s_dev), 263 inode->i_ino); 264 locks_dump_ctx_list(&ctx->flc_flock, "FLOCK"); 265 locks_dump_ctx_list(&ctx->flc_posix, "POSIX"); 266 locks_dump_ctx_list(&ctx->flc_lease, "LEASE"); 267 } 268 } 269 270 static void 271 locks_check_ctx_file_list(struct file *filp, struct list_head *list, 272 char *list_type) 273 { 274 struct file_lock *fl; 275 struct inode *inode = locks_inode(filp); 276 277 list_for_each_entry(fl, list, fl_list) 278 if (fl->fl_file == filp) 279 pr_warn("Leaked %s lock on dev=0x%x:0x%x ino=0x%lx " 280 " fl_owner=%p fl_flags=0x%x fl_type=0x%x fl_pid=%u\n", 281 list_type, MAJOR(inode->i_sb->s_dev), 282 MINOR(inode->i_sb->s_dev), inode->i_ino, 283 fl->fl_owner, fl->fl_flags, fl->fl_type, fl->fl_pid); 284 } 285 286 void 287 locks_free_lock_context(struct inode *inode) 288 { 289 struct file_lock_context *ctx = inode->i_flctx; 290 291 if (unlikely(ctx)) { 292 locks_check_ctx_lists(inode); 293 kmem_cache_free(flctx_cache, ctx); 294 } 295 } 296 297 static void locks_init_lock_heads(struct file_lock *fl) 298 { 299 INIT_HLIST_NODE(&fl->fl_link); 300 INIT_LIST_HEAD(&fl->fl_list); 301 INIT_LIST_HEAD(&fl->fl_block); 302 init_waitqueue_head(&fl->fl_wait); 303 } 304 305 /* Allocate an empty lock structure. */ 306 struct file_lock *locks_alloc_lock(void) 307 { 308 struct file_lock *fl = kmem_cache_zalloc(filelock_cache, GFP_KERNEL); 309 310 if (fl) 311 locks_init_lock_heads(fl); 312 313 return fl; 314 } 315 EXPORT_SYMBOL_GPL(locks_alloc_lock); 316 317 void locks_release_private(struct file_lock *fl) 318 { 319 if (fl->fl_ops) { 320 if (fl->fl_ops->fl_release_private) 321 fl->fl_ops->fl_release_private(fl); 322 fl->fl_ops = NULL; 323 } 324 325 if (fl->fl_lmops) { 326 if (fl->fl_lmops->lm_put_owner) { 327 fl->fl_lmops->lm_put_owner(fl->fl_owner); 328 fl->fl_owner = NULL; 329 } 330 fl->fl_lmops = NULL; 331 } 332 } 333 EXPORT_SYMBOL_GPL(locks_release_private); 334 335 /* Free a lock which is not in use. */ 336 void locks_free_lock(struct file_lock *fl) 337 { 338 BUG_ON(waitqueue_active(&fl->fl_wait)); 339 BUG_ON(!list_empty(&fl->fl_list)); 340 BUG_ON(!list_empty(&fl->fl_block)); 341 BUG_ON(!hlist_unhashed(&fl->fl_link)); 342 343 locks_release_private(fl); 344 kmem_cache_free(filelock_cache, fl); 345 } 346 EXPORT_SYMBOL(locks_free_lock); 347 348 static void 349 locks_dispose_list(struct list_head *dispose) 350 { 351 struct file_lock *fl; 352 353 while (!list_empty(dispose)) { 354 fl = list_first_entry(dispose, struct file_lock, fl_list); 355 list_del_init(&fl->fl_list); 356 locks_free_lock(fl); 357 } 358 } 359 360 void locks_init_lock(struct file_lock *fl) 361 { 362 memset(fl, 0, sizeof(struct file_lock)); 363 locks_init_lock_heads(fl); 364 } 365 366 EXPORT_SYMBOL(locks_init_lock); 367 368 /* 369 * Initialize a new lock from an existing file_lock structure. 370 */ 371 void locks_copy_conflock(struct file_lock *new, struct file_lock *fl) 372 { 373 new->fl_owner = fl->fl_owner; 374 new->fl_pid = fl->fl_pid; 375 new->fl_file = NULL; 376 new->fl_flags = fl->fl_flags; 377 new->fl_type = fl->fl_type; 378 new->fl_start = fl->fl_start; 379 new->fl_end = fl->fl_end; 380 new->fl_lmops = fl->fl_lmops; 381 new->fl_ops = NULL; 382 383 if (fl->fl_lmops) { 384 if (fl->fl_lmops->lm_get_owner) 385 fl->fl_lmops->lm_get_owner(fl->fl_owner); 386 } 387 } 388 EXPORT_SYMBOL(locks_copy_conflock); 389 390 void locks_copy_lock(struct file_lock *new, struct file_lock *fl) 391 { 392 /* "new" must be a freshly-initialized lock */ 393 WARN_ON_ONCE(new->fl_ops); 394 395 locks_copy_conflock(new, fl); 396 397 new->fl_file = fl->fl_file; 398 new->fl_ops = fl->fl_ops; 399 400 if (fl->fl_ops) { 401 if (fl->fl_ops->fl_copy_lock) 402 fl->fl_ops->fl_copy_lock(new, fl); 403 } 404 } 405 406 EXPORT_SYMBOL(locks_copy_lock); 407 408 static inline int flock_translate_cmd(int cmd) { 409 if (cmd & LOCK_MAND) 410 return cmd & (LOCK_MAND | LOCK_RW); 411 switch (cmd) { 412 case LOCK_SH: 413 return F_RDLCK; 414 case LOCK_EX: 415 return F_WRLCK; 416 case LOCK_UN: 417 return F_UNLCK; 418 } 419 return -EINVAL; 420 } 421 422 /* Fill in a file_lock structure with an appropriate FLOCK lock. */ 423 static struct file_lock * 424 flock_make_lock(struct file *filp, unsigned int cmd) 425 { 426 struct file_lock *fl; 427 int type = flock_translate_cmd(cmd); 428 429 if (type < 0) 430 return ERR_PTR(type); 431 432 fl = locks_alloc_lock(); 433 if (fl == NULL) 434 return ERR_PTR(-ENOMEM); 435 436 fl->fl_file = filp; 437 fl->fl_owner = filp; 438 fl->fl_pid = current->tgid; 439 fl->fl_flags = FL_FLOCK; 440 fl->fl_type = type; 441 fl->fl_end = OFFSET_MAX; 442 443 return fl; 444 } 445 446 static int assign_type(struct file_lock *fl, long type) 447 { 448 switch (type) { 449 case F_RDLCK: 450 case F_WRLCK: 451 case F_UNLCK: 452 fl->fl_type = type; 453 break; 454 default: 455 return -EINVAL; 456 } 457 return 0; 458 } 459 460 static int flock64_to_posix_lock(struct file *filp, struct file_lock *fl, 461 struct flock64 *l) 462 { 463 switch (l->l_whence) { 464 case SEEK_SET: 465 fl->fl_start = 0; 466 break; 467 case SEEK_CUR: 468 fl->fl_start = filp->f_pos; 469 break; 470 case SEEK_END: 471 fl->fl_start = i_size_read(file_inode(filp)); 472 break; 473 default: 474 return -EINVAL; 475 } 476 if (l->l_start > OFFSET_MAX - fl->fl_start) 477 return -EOVERFLOW; 478 fl->fl_start += l->l_start; 479 if (fl->fl_start < 0) 480 return -EINVAL; 481 482 /* POSIX-1996 leaves the case l->l_len < 0 undefined; 483 POSIX-2001 defines it. */ 484 if (l->l_len > 0) { 485 if (l->l_len - 1 > OFFSET_MAX - fl->fl_start) 486 return -EOVERFLOW; 487 fl->fl_end = fl->fl_start + l->l_len - 1; 488 489 } else if (l->l_len < 0) { 490 if (fl->fl_start + l->l_len < 0) 491 return -EINVAL; 492 fl->fl_end = fl->fl_start - 1; 493 fl->fl_start += l->l_len; 494 } else 495 fl->fl_end = OFFSET_MAX; 496 497 fl->fl_owner = current->files; 498 fl->fl_pid = current->tgid; 499 fl->fl_file = filp; 500 fl->fl_flags = FL_POSIX; 501 fl->fl_ops = NULL; 502 fl->fl_lmops = NULL; 503 504 return assign_type(fl, l->l_type); 505 } 506 507 /* Verify a "struct flock" and copy it to a "struct file_lock" as a POSIX 508 * style lock. 509 */ 510 static int flock_to_posix_lock(struct file *filp, struct file_lock *fl, 511 struct flock *l) 512 { 513 struct flock64 ll = { 514 .l_type = l->l_type, 515 .l_whence = l->l_whence, 516 .l_start = l->l_start, 517 .l_len = l->l_len, 518 }; 519 520 return flock64_to_posix_lock(filp, fl, &ll); 521 } 522 523 /* default lease lock manager operations */ 524 static bool 525 lease_break_callback(struct file_lock *fl) 526 { 527 kill_fasync(&fl->fl_fasync, SIGIO, POLL_MSG); 528 return false; 529 } 530 531 static void 532 lease_setup(struct file_lock *fl, void **priv) 533 { 534 struct file *filp = fl->fl_file; 535 struct fasync_struct *fa = *priv; 536 537 /* 538 * fasync_insert_entry() returns the old entry if any. If there was no 539 * old entry, then it used "priv" and inserted it into the fasync list. 540 * Clear the pointer to indicate that it shouldn't be freed. 541 */ 542 if (!fasync_insert_entry(fa->fa_fd, filp, &fl->fl_fasync, fa)) 543 *priv = NULL; 544 545 __f_setown(filp, task_pid(current), PIDTYPE_PID, 0); 546 } 547 548 static const struct lock_manager_operations lease_manager_ops = { 549 .lm_break = lease_break_callback, 550 .lm_change = lease_modify, 551 .lm_setup = lease_setup, 552 }; 553 554 /* 555 * Initialize a lease, use the default lock manager operations 556 */ 557 static int lease_init(struct file *filp, long type, struct file_lock *fl) 558 { 559 if (assign_type(fl, type) != 0) 560 return -EINVAL; 561 562 fl->fl_owner = filp; 563 fl->fl_pid = current->tgid; 564 565 fl->fl_file = filp; 566 fl->fl_flags = FL_LEASE; 567 fl->fl_start = 0; 568 fl->fl_end = OFFSET_MAX; 569 fl->fl_ops = NULL; 570 fl->fl_lmops = &lease_manager_ops; 571 return 0; 572 } 573 574 /* Allocate a file_lock initialised to this type of lease */ 575 static struct file_lock *lease_alloc(struct file *filp, long type) 576 { 577 struct file_lock *fl = locks_alloc_lock(); 578 int error = -ENOMEM; 579 580 if (fl == NULL) 581 return ERR_PTR(error); 582 583 error = lease_init(filp, type, fl); 584 if (error) { 585 locks_free_lock(fl); 586 return ERR_PTR(error); 587 } 588 return fl; 589 } 590 591 /* Check if two locks overlap each other. 592 */ 593 static inline int locks_overlap(struct file_lock *fl1, struct file_lock *fl2) 594 { 595 return ((fl1->fl_end >= fl2->fl_start) && 596 (fl2->fl_end >= fl1->fl_start)); 597 } 598 599 /* 600 * Check whether two locks have the same owner. 601 */ 602 static int posix_same_owner(struct file_lock *fl1, struct file_lock *fl2) 603 { 604 if (fl1->fl_lmops && fl1->fl_lmops->lm_compare_owner) 605 return fl2->fl_lmops == fl1->fl_lmops && 606 fl1->fl_lmops->lm_compare_owner(fl1, fl2); 607 return fl1->fl_owner == fl2->fl_owner; 608 } 609 610 /* Must be called with the flc_lock held! */ 611 static void locks_insert_global_locks(struct file_lock *fl) 612 { 613 struct file_lock_list_struct *fll = this_cpu_ptr(&file_lock_list); 614 615 percpu_rwsem_assert_held(&file_rwsem); 616 617 spin_lock(&fll->lock); 618 fl->fl_link_cpu = smp_processor_id(); 619 hlist_add_head(&fl->fl_link, &fll->hlist); 620 spin_unlock(&fll->lock); 621 } 622 623 /* Must be called with the flc_lock held! */ 624 static void locks_delete_global_locks(struct file_lock *fl) 625 { 626 struct file_lock_list_struct *fll; 627 628 percpu_rwsem_assert_held(&file_rwsem); 629 630 /* 631 * Avoid taking lock if already unhashed. This is safe since this check 632 * is done while holding the flc_lock, and new insertions into the list 633 * also require that it be held. 634 */ 635 if (hlist_unhashed(&fl->fl_link)) 636 return; 637 638 fll = per_cpu_ptr(&file_lock_list, fl->fl_link_cpu); 639 spin_lock(&fll->lock); 640 hlist_del_init(&fl->fl_link); 641 spin_unlock(&fll->lock); 642 } 643 644 static unsigned long 645 posix_owner_key(struct file_lock *fl) 646 { 647 if (fl->fl_lmops && fl->fl_lmops->lm_owner_key) 648 return fl->fl_lmops->lm_owner_key(fl); 649 return (unsigned long)fl->fl_owner; 650 } 651 652 static void locks_insert_global_blocked(struct file_lock *waiter) 653 { 654 lockdep_assert_held(&blocked_lock_lock); 655 656 hash_add(blocked_hash, &waiter->fl_link, posix_owner_key(waiter)); 657 } 658 659 static void locks_delete_global_blocked(struct file_lock *waiter) 660 { 661 lockdep_assert_held(&blocked_lock_lock); 662 663 hash_del(&waiter->fl_link); 664 } 665 666 /* Remove waiter from blocker's block list. 667 * When blocker ends up pointing to itself then the list is empty. 668 * 669 * Must be called with blocked_lock_lock held. 670 */ 671 static void __locks_delete_block(struct file_lock *waiter) 672 { 673 locks_delete_global_blocked(waiter); 674 list_del_init(&waiter->fl_block); 675 waiter->fl_next = NULL; 676 } 677 678 static void locks_delete_block(struct file_lock *waiter) 679 { 680 spin_lock(&blocked_lock_lock); 681 __locks_delete_block(waiter); 682 spin_unlock(&blocked_lock_lock); 683 } 684 685 /* Insert waiter into blocker's block list. 686 * We use a circular list so that processes can be easily woken up in 687 * the order they blocked. The documentation doesn't require this but 688 * it seems like the reasonable thing to do. 689 * 690 * Must be called with both the flc_lock and blocked_lock_lock held. The 691 * fl_block list itself is protected by the blocked_lock_lock, but by ensuring 692 * that the flc_lock is also held on insertions we can avoid taking the 693 * blocked_lock_lock in some cases when we see that the fl_block list is empty. 694 */ 695 static void __locks_insert_block(struct file_lock *blocker, 696 struct file_lock *waiter) 697 { 698 BUG_ON(!list_empty(&waiter->fl_block)); 699 waiter->fl_next = blocker; 700 list_add_tail(&waiter->fl_block, &blocker->fl_block); 701 if (IS_POSIX(blocker) && !IS_OFDLCK(blocker)) 702 locks_insert_global_blocked(waiter); 703 } 704 705 /* Must be called with flc_lock held. */ 706 static void locks_insert_block(struct file_lock *blocker, 707 struct file_lock *waiter) 708 { 709 spin_lock(&blocked_lock_lock); 710 __locks_insert_block(blocker, waiter); 711 spin_unlock(&blocked_lock_lock); 712 } 713 714 /* 715 * Wake up processes blocked waiting for blocker. 716 * 717 * Must be called with the inode->flc_lock held! 718 */ 719 static void locks_wake_up_blocks(struct file_lock *blocker) 720 { 721 /* 722 * Avoid taking global lock if list is empty. This is safe since new 723 * blocked requests are only added to the list under the flc_lock, and 724 * the flc_lock is always held here. Note that removal from the fl_block 725 * list does not require the flc_lock, so we must recheck list_empty() 726 * after acquiring the blocked_lock_lock. 727 */ 728 if (list_empty(&blocker->fl_block)) 729 return; 730 731 spin_lock(&blocked_lock_lock); 732 while (!list_empty(&blocker->fl_block)) { 733 struct file_lock *waiter; 734 735 waiter = list_first_entry(&blocker->fl_block, 736 struct file_lock, fl_block); 737 __locks_delete_block(waiter); 738 if (waiter->fl_lmops && waiter->fl_lmops->lm_notify) 739 waiter->fl_lmops->lm_notify(waiter); 740 else 741 wake_up(&waiter->fl_wait); 742 } 743 spin_unlock(&blocked_lock_lock); 744 } 745 746 static void 747 locks_insert_lock_ctx(struct file_lock *fl, struct list_head *before) 748 { 749 list_add_tail(&fl->fl_list, before); 750 locks_insert_global_locks(fl); 751 } 752 753 static void 754 locks_unlink_lock_ctx(struct file_lock *fl) 755 { 756 locks_delete_global_locks(fl); 757 list_del_init(&fl->fl_list); 758 locks_wake_up_blocks(fl); 759 } 760 761 static void 762 locks_delete_lock_ctx(struct file_lock *fl, struct list_head *dispose) 763 { 764 locks_unlink_lock_ctx(fl); 765 if (dispose) 766 list_add(&fl->fl_list, dispose); 767 else 768 locks_free_lock(fl); 769 } 770 771 /* Determine if lock sys_fl blocks lock caller_fl. Common functionality 772 * checks for shared/exclusive status of overlapping locks. 773 */ 774 static int locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl) 775 { 776 if (sys_fl->fl_type == F_WRLCK) 777 return 1; 778 if (caller_fl->fl_type == F_WRLCK) 779 return 1; 780 return 0; 781 } 782 783 /* Determine if lock sys_fl blocks lock caller_fl. POSIX specific 784 * checking before calling the locks_conflict(). 785 */ 786 static int posix_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl) 787 { 788 /* POSIX locks owned by the same process do not conflict with 789 * each other. 790 */ 791 if (posix_same_owner(caller_fl, sys_fl)) 792 return (0); 793 794 /* Check whether they overlap */ 795 if (!locks_overlap(caller_fl, sys_fl)) 796 return 0; 797 798 return (locks_conflict(caller_fl, sys_fl)); 799 } 800 801 /* Determine if lock sys_fl blocks lock caller_fl. FLOCK specific 802 * checking before calling the locks_conflict(). 803 */ 804 static int flock_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl) 805 { 806 /* FLOCK locks referring to the same filp do not conflict with 807 * each other. 808 */ 809 if (caller_fl->fl_file == sys_fl->fl_file) 810 return (0); 811 if ((caller_fl->fl_type & LOCK_MAND) || (sys_fl->fl_type & LOCK_MAND)) 812 return 0; 813 814 return (locks_conflict(caller_fl, sys_fl)); 815 } 816 817 void 818 posix_test_lock(struct file *filp, struct file_lock *fl) 819 { 820 struct file_lock *cfl; 821 struct file_lock_context *ctx; 822 struct inode *inode = locks_inode(filp); 823 824 ctx = smp_load_acquire(&inode->i_flctx); 825 if (!ctx || list_empty_careful(&ctx->flc_posix)) { 826 fl->fl_type = F_UNLCK; 827 return; 828 } 829 830 spin_lock(&ctx->flc_lock); 831 list_for_each_entry(cfl, &ctx->flc_posix, fl_list) { 832 if (posix_locks_conflict(fl, cfl)) { 833 locks_copy_conflock(fl, cfl); 834 goto out; 835 } 836 } 837 fl->fl_type = F_UNLCK; 838 out: 839 spin_unlock(&ctx->flc_lock); 840 return; 841 } 842 EXPORT_SYMBOL(posix_test_lock); 843 844 /* 845 * Deadlock detection: 846 * 847 * We attempt to detect deadlocks that are due purely to posix file 848 * locks. 849 * 850 * We assume that a task can be waiting for at most one lock at a time. 851 * So for any acquired lock, the process holding that lock may be 852 * waiting on at most one other lock. That lock in turns may be held by 853 * someone waiting for at most one other lock. Given a requested lock 854 * caller_fl which is about to wait for a conflicting lock block_fl, we 855 * follow this chain of waiters to ensure we are not about to create a 856 * cycle. 857 * 858 * Since we do this before we ever put a process to sleep on a lock, we 859 * are ensured that there is never a cycle; that is what guarantees that 860 * the while() loop in posix_locks_deadlock() eventually completes. 861 * 862 * Note: the above assumption may not be true when handling lock 863 * requests from a broken NFS client. It may also fail in the presence 864 * of tasks (such as posix threads) sharing the same open file table. 865 * To handle those cases, we just bail out after a few iterations. 866 * 867 * For FL_OFDLCK locks, the owner is the filp, not the files_struct. 868 * Because the owner is not even nominally tied to a thread of 869 * execution, the deadlock detection below can't reasonably work well. Just 870 * skip it for those. 871 * 872 * In principle, we could do a more limited deadlock detection on FL_OFDLCK 873 * locks that just checks for the case where two tasks are attempting to 874 * upgrade from read to write locks on the same inode. 875 */ 876 877 #define MAX_DEADLK_ITERATIONS 10 878 879 /* Find a lock that the owner of the given block_fl is blocking on. */ 880 static struct file_lock *what_owner_is_waiting_for(struct file_lock *block_fl) 881 { 882 struct file_lock *fl; 883 884 hash_for_each_possible(blocked_hash, fl, fl_link, posix_owner_key(block_fl)) { 885 if (posix_same_owner(fl, block_fl)) 886 return fl->fl_next; 887 } 888 return NULL; 889 } 890 891 /* Must be called with the blocked_lock_lock held! */ 892 static int posix_locks_deadlock(struct file_lock *caller_fl, 893 struct file_lock *block_fl) 894 { 895 int i = 0; 896 897 lockdep_assert_held(&blocked_lock_lock); 898 899 /* 900 * This deadlock detector can't reasonably detect deadlocks with 901 * FL_OFDLCK locks, since they aren't owned by a process, per-se. 902 */ 903 if (IS_OFDLCK(caller_fl)) 904 return 0; 905 906 while ((block_fl = what_owner_is_waiting_for(block_fl))) { 907 if (i++ > MAX_DEADLK_ITERATIONS) 908 return 0; 909 if (posix_same_owner(caller_fl, block_fl)) 910 return 1; 911 } 912 return 0; 913 } 914 915 /* Try to create a FLOCK lock on filp. We always insert new FLOCK locks 916 * after any leases, but before any posix locks. 917 * 918 * Note that if called with an FL_EXISTS argument, the caller may determine 919 * whether or not a lock was successfully freed by testing the return 920 * value for -ENOENT. 921 */ 922 static int flock_lock_inode(struct inode *inode, struct file_lock *request) 923 { 924 struct file_lock *new_fl = NULL; 925 struct file_lock *fl; 926 struct file_lock_context *ctx; 927 int error = 0; 928 bool found = false; 929 LIST_HEAD(dispose); 930 931 ctx = locks_get_lock_context(inode, request->fl_type); 932 if (!ctx) { 933 if (request->fl_type != F_UNLCK) 934 return -ENOMEM; 935 return (request->fl_flags & FL_EXISTS) ? -ENOENT : 0; 936 } 937 938 if (!(request->fl_flags & FL_ACCESS) && (request->fl_type != F_UNLCK)) { 939 new_fl = locks_alloc_lock(); 940 if (!new_fl) 941 return -ENOMEM; 942 } 943 944 percpu_down_read_preempt_disable(&file_rwsem); 945 spin_lock(&ctx->flc_lock); 946 if (request->fl_flags & FL_ACCESS) 947 goto find_conflict; 948 949 list_for_each_entry(fl, &ctx->flc_flock, fl_list) { 950 if (request->fl_file != fl->fl_file) 951 continue; 952 if (request->fl_type == fl->fl_type) 953 goto out; 954 found = true; 955 locks_delete_lock_ctx(fl, &dispose); 956 break; 957 } 958 959 if (request->fl_type == F_UNLCK) { 960 if ((request->fl_flags & FL_EXISTS) && !found) 961 error = -ENOENT; 962 goto out; 963 } 964 965 find_conflict: 966 list_for_each_entry(fl, &ctx->flc_flock, fl_list) { 967 if (!flock_locks_conflict(request, fl)) 968 continue; 969 error = -EAGAIN; 970 if (!(request->fl_flags & FL_SLEEP)) 971 goto out; 972 error = FILE_LOCK_DEFERRED; 973 locks_insert_block(fl, request); 974 goto out; 975 } 976 if (request->fl_flags & FL_ACCESS) 977 goto out; 978 locks_copy_lock(new_fl, request); 979 locks_insert_lock_ctx(new_fl, &ctx->flc_flock); 980 new_fl = NULL; 981 error = 0; 982 983 out: 984 spin_unlock(&ctx->flc_lock); 985 percpu_up_read_preempt_enable(&file_rwsem); 986 if (new_fl) 987 locks_free_lock(new_fl); 988 locks_dispose_list(&dispose); 989 trace_flock_lock_inode(inode, request, error); 990 return error; 991 } 992 993 static int posix_lock_inode(struct inode *inode, struct file_lock *request, 994 struct file_lock *conflock) 995 { 996 struct file_lock *fl, *tmp; 997 struct file_lock *new_fl = NULL; 998 struct file_lock *new_fl2 = NULL; 999 struct file_lock *left = NULL; 1000 struct file_lock *right = NULL; 1001 struct file_lock_context *ctx; 1002 int error; 1003 bool added = false; 1004 LIST_HEAD(dispose); 1005 1006 ctx = locks_get_lock_context(inode, request->fl_type); 1007 if (!ctx) 1008 return (request->fl_type == F_UNLCK) ? 0 : -ENOMEM; 1009 1010 /* 1011 * We may need two file_lock structures for this operation, 1012 * so we get them in advance to avoid races. 1013 * 1014 * In some cases we can be sure, that no new locks will be needed 1015 */ 1016 if (!(request->fl_flags & FL_ACCESS) && 1017 (request->fl_type != F_UNLCK || 1018 request->fl_start != 0 || request->fl_end != OFFSET_MAX)) { 1019 new_fl = locks_alloc_lock(); 1020 new_fl2 = locks_alloc_lock(); 1021 } 1022 1023 percpu_down_read_preempt_disable(&file_rwsem); 1024 spin_lock(&ctx->flc_lock); 1025 /* 1026 * New lock request. Walk all POSIX locks and look for conflicts. If 1027 * there are any, either return error or put the request on the 1028 * blocker's list of waiters and the global blocked_hash. 1029 */ 1030 if (request->fl_type != F_UNLCK) { 1031 list_for_each_entry(fl, &ctx->flc_posix, fl_list) { 1032 if (!posix_locks_conflict(request, fl)) 1033 continue; 1034 if (conflock) 1035 locks_copy_conflock(conflock, fl); 1036 error = -EAGAIN; 1037 if (!(request->fl_flags & FL_SLEEP)) 1038 goto out; 1039 /* 1040 * Deadlock detection and insertion into the blocked 1041 * locks list must be done while holding the same lock! 1042 */ 1043 error = -EDEADLK; 1044 spin_lock(&blocked_lock_lock); 1045 if (likely(!posix_locks_deadlock(request, fl))) { 1046 error = FILE_LOCK_DEFERRED; 1047 __locks_insert_block(fl, request); 1048 } 1049 spin_unlock(&blocked_lock_lock); 1050 goto out; 1051 } 1052 } 1053 1054 /* If we're just looking for a conflict, we're done. */ 1055 error = 0; 1056 if (request->fl_flags & FL_ACCESS) 1057 goto out; 1058 1059 /* Find the first old lock with the same owner as the new lock */ 1060 list_for_each_entry(fl, &ctx->flc_posix, fl_list) { 1061 if (posix_same_owner(request, fl)) 1062 break; 1063 } 1064 1065 /* Process locks with this owner. */ 1066 list_for_each_entry_safe_from(fl, tmp, &ctx->flc_posix, fl_list) { 1067 if (!posix_same_owner(request, fl)) 1068 break; 1069 1070 /* Detect adjacent or overlapping regions (if same lock type) */ 1071 if (request->fl_type == fl->fl_type) { 1072 /* In all comparisons of start vs end, use 1073 * "start - 1" rather than "end + 1". If end 1074 * is OFFSET_MAX, end + 1 will become negative. 1075 */ 1076 if (fl->fl_end < request->fl_start - 1) 1077 continue; 1078 /* If the next lock in the list has entirely bigger 1079 * addresses than the new one, insert the lock here. 1080 */ 1081 if (fl->fl_start - 1 > request->fl_end) 1082 break; 1083 1084 /* If we come here, the new and old lock are of the 1085 * same type and adjacent or overlapping. Make one 1086 * lock yielding from the lower start address of both 1087 * locks to the higher end address. 1088 */ 1089 if (fl->fl_start > request->fl_start) 1090 fl->fl_start = request->fl_start; 1091 else 1092 request->fl_start = fl->fl_start; 1093 if (fl->fl_end < request->fl_end) 1094 fl->fl_end = request->fl_end; 1095 else 1096 request->fl_end = fl->fl_end; 1097 if (added) { 1098 locks_delete_lock_ctx(fl, &dispose); 1099 continue; 1100 } 1101 request = fl; 1102 added = true; 1103 } else { 1104 /* Processing for different lock types is a bit 1105 * more complex. 1106 */ 1107 if (fl->fl_end < request->fl_start) 1108 continue; 1109 if (fl->fl_start > request->fl_end) 1110 break; 1111 if (request->fl_type == F_UNLCK) 1112 added = true; 1113 if (fl->fl_start < request->fl_start) 1114 left = fl; 1115 /* If the next lock in the list has a higher end 1116 * address than the new one, insert the new one here. 1117 */ 1118 if (fl->fl_end > request->fl_end) { 1119 right = fl; 1120 break; 1121 } 1122 if (fl->fl_start >= request->fl_start) { 1123 /* The new lock completely replaces an old 1124 * one (This may happen several times). 1125 */ 1126 if (added) { 1127 locks_delete_lock_ctx(fl, &dispose); 1128 continue; 1129 } 1130 /* 1131 * Replace the old lock with new_fl, and 1132 * remove the old one. It's safe to do the 1133 * insert here since we know that we won't be 1134 * using new_fl later, and that the lock is 1135 * just replacing an existing lock. 1136 */ 1137 error = -ENOLCK; 1138 if (!new_fl) 1139 goto out; 1140 locks_copy_lock(new_fl, request); 1141 request = new_fl; 1142 new_fl = NULL; 1143 locks_insert_lock_ctx(request, &fl->fl_list); 1144 locks_delete_lock_ctx(fl, &dispose); 1145 added = true; 1146 } 1147 } 1148 } 1149 1150 /* 1151 * The above code only modifies existing locks in case of merging or 1152 * replacing. If new lock(s) need to be inserted all modifications are 1153 * done below this, so it's safe yet to bail out. 1154 */ 1155 error = -ENOLCK; /* "no luck" */ 1156 if (right && left == right && !new_fl2) 1157 goto out; 1158 1159 error = 0; 1160 if (!added) { 1161 if (request->fl_type == F_UNLCK) { 1162 if (request->fl_flags & FL_EXISTS) 1163 error = -ENOENT; 1164 goto out; 1165 } 1166 1167 if (!new_fl) { 1168 error = -ENOLCK; 1169 goto out; 1170 } 1171 locks_copy_lock(new_fl, request); 1172 locks_insert_lock_ctx(new_fl, &fl->fl_list); 1173 fl = new_fl; 1174 new_fl = NULL; 1175 } 1176 if (right) { 1177 if (left == right) { 1178 /* The new lock breaks the old one in two pieces, 1179 * so we have to use the second new lock. 1180 */ 1181 left = new_fl2; 1182 new_fl2 = NULL; 1183 locks_copy_lock(left, right); 1184 locks_insert_lock_ctx(left, &fl->fl_list); 1185 } 1186 right->fl_start = request->fl_end + 1; 1187 locks_wake_up_blocks(right); 1188 } 1189 if (left) { 1190 left->fl_end = request->fl_start - 1; 1191 locks_wake_up_blocks(left); 1192 } 1193 out: 1194 spin_unlock(&ctx->flc_lock); 1195 percpu_up_read_preempt_enable(&file_rwsem); 1196 /* 1197 * Free any unused locks. 1198 */ 1199 if (new_fl) 1200 locks_free_lock(new_fl); 1201 if (new_fl2) 1202 locks_free_lock(new_fl2); 1203 locks_dispose_list(&dispose); 1204 trace_posix_lock_inode(inode, request, error); 1205 1206 return error; 1207 } 1208 1209 /** 1210 * posix_lock_file - Apply a POSIX-style lock to a file 1211 * @filp: The file to apply the lock to 1212 * @fl: The lock to be applied 1213 * @conflock: Place to return a copy of the conflicting lock, if found. 1214 * 1215 * Add a POSIX style lock to a file. 1216 * We merge adjacent & overlapping locks whenever possible. 1217 * POSIX locks are sorted by owner task, then by starting address 1218 * 1219 * Note that if called with an FL_EXISTS argument, the caller may determine 1220 * whether or not a lock was successfully freed by testing the return 1221 * value for -ENOENT. 1222 */ 1223 int posix_lock_file(struct file *filp, struct file_lock *fl, 1224 struct file_lock *conflock) 1225 { 1226 return posix_lock_inode(locks_inode(filp), fl, conflock); 1227 } 1228 EXPORT_SYMBOL(posix_lock_file); 1229 1230 /** 1231 * posix_lock_inode_wait - Apply a POSIX-style lock to a file 1232 * @inode: inode of file to which lock request should be applied 1233 * @fl: The lock to be applied 1234 * 1235 * Apply a POSIX style lock request to an inode. 1236 */ 1237 static int posix_lock_inode_wait(struct inode *inode, struct file_lock *fl) 1238 { 1239 int error; 1240 might_sleep (); 1241 for (;;) { 1242 error = posix_lock_inode(inode, fl, NULL); 1243 if (error != FILE_LOCK_DEFERRED) 1244 break; 1245 error = wait_event_interruptible(fl->fl_wait, !fl->fl_next); 1246 if (!error) 1247 continue; 1248 1249 locks_delete_block(fl); 1250 break; 1251 } 1252 return error; 1253 } 1254 1255 #ifdef CONFIG_MANDATORY_FILE_LOCKING 1256 /** 1257 * locks_mandatory_locked - Check for an active lock 1258 * @file: the file to check 1259 * 1260 * Searches the inode's list of locks to find any POSIX locks which conflict. 1261 * This function is called from locks_verify_locked() only. 1262 */ 1263 int locks_mandatory_locked(struct file *file) 1264 { 1265 int ret; 1266 struct inode *inode = locks_inode(file); 1267 struct file_lock_context *ctx; 1268 struct file_lock *fl; 1269 1270 ctx = smp_load_acquire(&inode->i_flctx); 1271 if (!ctx || list_empty_careful(&ctx->flc_posix)) 1272 return 0; 1273 1274 /* 1275 * Search the lock list for this inode for any POSIX locks. 1276 */ 1277 spin_lock(&ctx->flc_lock); 1278 ret = 0; 1279 list_for_each_entry(fl, &ctx->flc_posix, fl_list) { 1280 if (fl->fl_owner != current->files && 1281 fl->fl_owner != file) { 1282 ret = -EAGAIN; 1283 break; 1284 } 1285 } 1286 spin_unlock(&ctx->flc_lock); 1287 return ret; 1288 } 1289 1290 /** 1291 * locks_mandatory_area - Check for a conflicting lock 1292 * @inode: the file to check 1293 * @filp: how the file was opened (if it was) 1294 * @start: first byte in the file to check 1295 * @end: lastbyte in the file to check 1296 * @type: %F_WRLCK for a write lock, else %F_RDLCK 1297 * 1298 * Searches the inode's list of locks to find any POSIX locks which conflict. 1299 */ 1300 int locks_mandatory_area(struct inode *inode, struct file *filp, loff_t start, 1301 loff_t end, unsigned char type) 1302 { 1303 struct file_lock fl; 1304 int error; 1305 bool sleep = false; 1306 1307 locks_init_lock(&fl); 1308 fl.fl_pid = current->tgid; 1309 fl.fl_file = filp; 1310 fl.fl_flags = FL_POSIX | FL_ACCESS; 1311 if (filp && !(filp->f_flags & O_NONBLOCK)) 1312 sleep = true; 1313 fl.fl_type = type; 1314 fl.fl_start = start; 1315 fl.fl_end = end; 1316 1317 for (;;) { 1318 if (filp) { 1319 fl.fl_owner = filp; 1320 fl.fl_flags &= ~FL_SLEEP; 1321 error = posix_lock_inode(inode, &fl, NULL); 1322 if (!error) 1323 break; 1324 } 1325 1326 if (sleep) 1327 fl.fl_flags |= FL_SLEEP; 1328 fl.fl_owner = current->files; 1329 error = posix_lock_inode(inode, &fl, NULL); 1330 if (error != FILE_LOCK_DEFERRED) 1331 break; 1332 error = wait_event_interruptible(fl.fl_wait, !fl.fl_next); 1333 if (!error) { 1334 /* 1335 * If we've been sleeping someone might have 1336 * changed the permissions behind our back. 1337 */ 1338 if (__mandatory_lock(inode)) 1339 continue; 1340 } 1341 1342 locks_delete_block(&fl); 1343 break; 1344 } 1345 1346 return error; 1347 } 1348 1349 EXPORT_SYMBOL(locks_mandatory_area); 1350 #endif /* CONFIG_MANDATORY_FILE_LOCKING */ 1351 1352 static void lease_clear_pending(struct file_lock *fl, int arg) 1353 { 1354 switch (arg) { 1355 case F_UNLCK: 1356 fl->fl_flags &= ~FL_UNLOCK_PENDING; 1357 /* fall through: */ 1358 case F_RDLCK: 1359 fl->fl_flags &= ~FL_DOWNGRADE_PENDING; 1360 } 1361 } 1362 1363 /* We already had a lease on this file; just change its type */ 1364 int lease_modify(struct file_lock *fl, int arg, struct list_head *dispose) 1365 { 1366 int error = assign_type(fl, arg); 1367 1368 if (error) 1369 return error; 1370 lease_clear_pending(fl, arg); 1371 locks_wake_up_blocks(fl); 1372 if (arg == F_UNLCK) { 1373 struct file *filp = fl->fl_file; 1374 1375 f_delown(filp); 1376 filp->f_owner.signum = 0; 1377 fasync_helper(0, fl->fl_file, 0, &fl->fl_fasync); 1378 if (fl->fl_fasync != NULL) { 1379 printk(KERN_ERR "locks_delete_lock: fasync == %p\n", fl->fl_fasync); 1380 fl->fl_fasync = NULL; 1381 } 1382 locks_delete_lock_ctx(fl, dispose); 1383 } 1384 return 0; 1385 } 1386 EXPORT_SYMBOL(lease_modify); 1387 1388 static bool past_time(unsigned long then) 1389 { 1390 if (!then) 1391 /* 0 is a special value meaning "this never expires": */ 1392 return false; 1393 return time_after(jiffies, then); 1394 } 1395 1396 static void time_out_leases(struct inode *inode, struct list_head *dispose) 1397 { 1398 struct file_lock_context *ctx = inode->i_flctx; 1399 struct file_lock *fl, *tmp; 1400 1401 lockdep_assert_held(&ctx->flc_lock); 1402 1403 list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list) { 1404 trace_time_out_leases(inode, fl); 1405 if (past_time(fl->fl_downgrade_time)) 1406 lease_modify(fl, F_RDLCK, dispose); 1407 if (past_time(fl->fl_break_time)) 1408 lease_modify(fl, F_UNLCK, dispose); 1409 } 1410 } 1411 1412 static bool leases_conflict(struct file_lock *lease, struct file_lock *breaker) 1413 { 1414 if ((breaker->fl_flags & FL_LAYOUT) != (lease->fl_flags & FL_LAYOUT)) 1415 return false; 1416 if ((breaker->fl_flags & FL_DELEG) && (lease->fl_flags & FL_LEASE)) 1417 return false; 1418 return locks_conflict(breaker, lease); 1419 } 1420 1421 static bool 1422 any_leases_conflict(struct inode *inode, struct file_lock *breaker) 1423 { 1424 struct file_lock_context *ctx = inode->i_flctx; 1425 struct file_lock *fl; 1426 1427 lockdep_assert_held(&ctx->flc_lock); 1428 1429 list_for_each_entry(fl, &ctx->flc_lease, fl_list) { 1430 if (leases_conflict(fl, breaker)) 1431 return true; 1432 } 1433 return false; 1434 } 1435 1436 /** 1437 * __break_lease - revoke all outstanding leases on file 1438 * @inode: the inode of the file to return 1439 * @mode: O_RDONLY: break only write leases; O_WRONLY or O_RDWR: 1440 * break all leases 1441 * @type: FL_LEASE: break leases and delegations; FL_DELEG: break 1442 * only delegations 1443 * 1444 * break_lease (inlined for speed) has checked there already is at least 1445 * some kind of lock (maybe a lease) on this file. Leases are broken on 1446 * a call to open() or truncate(). This function can sleep unless you 1447 * specified %O_NONBLOCK to your open(). 1448 */ 1449 int __break_lease(struct inode *inode, unsigned int mode, unsigned int type) 1450 { 1451 int error = 0; 1452 struct file_lock_context *ctx; 1453 struct file_lock *new_fl, *fl, *tmp; 1454 unsigned long break_time; 1455 int want_write = (mode & O_ACCMODE) != O_RDONLY; 1456 LIST_HEAD(dispose); 1457 1458 new_fl = lease_alloc(NULL, want_write ? F_WRLCK : F_RDLCK); 1459 if (IS_ERR(new_fl)) 1460 return PTR_ERR(new_fl); 1461 new_fl->fl_flags = type; 1462 1463 /* typically we will check that ctx is non-NULL before calling */ 1464 ctx = smp_load_acquire(&inode->i_flctx); 1465 if (!ctx) { 1466 WARN_ON_ONCE(1); 1467 return error; 1468 } 1469 1470 percpu_down_read_preempt_disable(&file_rwsem); 1471 spin_lock(&ctx->flc_lock); 1472 1473 time_out_leases(inode, &dispose); 1474 1475 if (!any_leases_conflict(inode, new_fl)) 1476 goto out; 1477 1478 break_time = 0; 1479 if (lease_break_time > 0) { 1480 break_time = jiffies + lease_break_time * HZ; 1481 if (break_time == 0) 1482 break_time++; /* so that 0 means no break time */ 1483 } 1484 1485 list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list) { 1486 if (!leases_conflict(fl, new_fl)) 1487 continue; 1488 if (want_write) { 1489 if (fl->fl_flags & FL_UNLOCK_PENDING) 1490 continue; 1491 fl->fl_flags |= FL_UNLOCK_PENDING; 1492 fl->fl_break_time = break_time; 1493 } else { 1494 if (lease_breaking(fl)) 1495 continue; 1496 fl->fl_flags |= FL_DOWNGRADE_PENDING; 1497 fl->fl_downgrade_time = break_time; 1498 } 1499 if (fl->fl_lmops->lm_break(fl)) 1500 locks_delete_lock_ctx(fl, &dispose); 1501 } 1502 1503 if (list_empty(&ctx->flc_lease)) 1504 goto out; 1505 1506 if (mode & O_NONBLOCK) { 1507 trace_break_lease_noblock(inode, new_fl); 1508 error = -EWOULDBLOCK; 1509 goto out; 1510 } 1511 1512 restart: 1513 fl = list_first_entry(&ctx->flc_lease, struct file_lock, fl_list); 1514 break_time = fl->fl_break_time; 1515 if (break_time != 0) 1516 break_time -= jiffies; 1517 if (break_time == 0) 1518 break_time++; 1519 locks_insert_block(fl, new_fl); 1520 trace_break_lease_block(inode, new_fl); 1521 spin_unlock(&ctx->flc_lock); 1522 percpu_up_read_preempt_enable(&file_rwsem); 1523 1524 locks_dispose_list(&dispose); 1525 error = wait_event_interruptible_timeout(new_fl->fl_wait, 1526 !new_fl->fl_next, break_time); 1527 1528 percpu_down_read_preempt_disable(&file_rwsem); 1529 spin_lock(&ctx->flc_lock); 1530 trace_break_lease_unblock(inode, new_fl); 1531 locks_delete_block(new_fl); 1532 if (error >= 0) { 1533 /* 1534 * Wait for the next conflicting lease that has not been 1535 * broken yet 1536 */ 1537 if (error == 0) 1538 time_out_leases(inode, &dispose); 1539 if (any_leases_conflict(inode, new_fl)) 1540 goto restart; 1541 error = 0; 1542 } 1543 out: 1544 spin_unlock(&ctx->flc_lock); 1545 percpu_up_read_preempt_enable(&file_rwsem); 1546 locks_dispose_list(&dispose); 1547 locks_free_lock(new_fl); 1548 return error; 1549 } 1550 1551 EXPORT_SYMBOL(__break_lease); 1552 1553 /** 1554 * lease_get_mtime - update modified time of an inode with exclusive lease 1555 * @inode: the inode 1556 * @time: pointer to a timespec which contains the last modified time 1557 * 1558 * This is to force NFS clients to flush their caches for files with 1559 * exclusive leases. The justification is that if someone has an 1560 * exclusive lease, then they could be modifying it. 1561 */ 1562 void lease_get_mtime(struct inode *inode, struct timespec64 *time) 1563 { 1564 bool has_lease = false; 1565 struct file_lock_context *ctx; 1566 struct file_lock *fl; 1567 1568 ctx = smp_load_acquire(&inode->i_flctx); 1569 if (ctx && !list_empty_careful(&ctx->flc_lease)) { 1570 spin_lock(&ctx->flc_lock); 1571 fl = list_first_entry_or_null(&ctx->flc_lease, 1572 struct file_lock, fl_list); 1573 if (fl && (fl->fl_type == F_WRLCK)) 1574 has_lease = true; 1575 spin_unlock(&ctx->flc_lock); 1576 } 1577 1578 if (has_lease) 1579 *time = current_time(inode); 1580 } 1581 1582 EXPORT_SYMBOL(lease_get_mtime); 1583 1584 /** 1585 * fcntl_getlease - Enquire what lease is currently active 1586 * @filp: the file 1587 * 1588 * The value returned by this function will be one of 1589 * (if no lease break is pending): 1590 * 1591 * %F_RDLCK to indicate a shared lease is held. 1592 * 1593 * %F_WRLCK to indicate an exclusive lease is held. 1594 * 1595 * %F_UNLCK to indicate no lease is held. 1596 * 1597 * (if a lease break is pending): 1598 * 1599 * %F_RDLCK to indicate an exclusive lease needs to be 1600 * changed to a shared lease (or removed). 1601 * 1602 * %F_UNLCK to indicate the lease needs to be removed. 1603 * 1604 * XXX: sfr & willy disagree over whether F_INPROGRESS 1605 * should be returned to userspace. 1606 */ 1607 int fcntl_getlease(struct file *filp) 1608 { 1609 struct file_lock *fl; 1610 struct inode *inode = locks_inode(filp); 1611 struct file_lock_context *ctx; 1612 int type = F_UNLCK; 1613 LIST_HEAD(dispose); 1614 1615 ctx = smp_load_acquire(&inode->i_flctx); 1616 if (ctx && !list_empty_careful(&ctx->flc_lease)) { 1617 percpu_down_read_preempt_disable(&file_rwsem); 1618 spin_lock(&ctx->flc_lock); 1619 time_out_leases(inode, &dispose); 1620 list_for_each_entry(fl, &ctx->flc_lease, fl_list) { 1621 if (fl->fl_file != filp) 1622 continue; 1623 type = target_leasetype(fl); 1624 break; 1625 } 1626 spin_unlock(&ctx->flc_lock); 1627 percpu_up_read_preempt_enable(&file_rwsem); 1628 1629 locks_dispose_list(&dispose); 1630 } 1631 return type; 1632 } 1633 1634 /** 1635 * check_conflicting_open - see if the given dentry points to a file that has 1636 * an existing open that would conflict with the 1637 * desired lease. 1638 * @dentry: dentry to check 1639 * @arg: type of lease that we're trying to acquire 1640 * @flags: current lock flags 1641 * 1642 * Check to see if there's an existing open fd on this file that would 1643 * conflict with the lease we're trying to set. 1644 */ 1645 static int 1646 check_conflicting_open(const struct dentry *dentry, const long arg, int flags) 1647 { 1648 int ret = 0; 1649 struct inode *inode = dentry->d_inode; 1650 1651 if (flags & FL_LAYOUT) 1652 return 0; 1653 1654 if ((arg == F_RDLCK) && 1655 (atomic_read(&d_real_inode(dentry)->i_writecount) > 0)) 1656 return -EAGAIN; 1657 1658 if ((arg == F_WRLCK) && ((d_count(dentry) > 1) || 1659 (atomic_read(&inode->i_count) > 1))) 1660 ret = -EAGAIN; 1661 1662 return ret; 1663 } 1664 1665 static int 1666 generic_add_lease(struct file *filp, long arg, struct file_lock **flp, void **priv) 1667 { 1668 struct file_lock *fl, *my_fl = NULL, *lease; 1669 struct dentry *dentry = filp->f_path.dentry; 1670 struct inode *inode = dentry->d_inode; 1671 struct file_lock_context *ctx; 1672 bool is_deleg = (*flp)->fl_flags & FL_DELEG; 1673 int error; 1674 LIST_HEAD(dispose); 1675 1676 lease = *flp; 1677 trace_generic_add_lease(inode, lease); 1678 1679 /* Note that arg is never F_UNLCK here */ 1680 ctx = locks_get_lock_context(inode, arg); 1681 if (!ctx) 1682 return -ENOMEM; 1683 1684 /* 1685 * In the delegation case we need mutual exclusion with 1686 * a number of operations that take the i_mutex. We trylock 1687 * because delegations are an optional optimization, and if 1688 * there's some chance of a conflict--we'd rather not 1689 * bother, maybe that's a sign this just isn't a good file to 1690 * hand out a delegation on. 1691 */ 1692 if (is_deleg && !inode_trylock(inode)) 1693 return -EAGAIN; 1694 1695 if (is_deleg && arg == F_WRLCK) { 1696 /* Write delegations are not currently supported: */ 1697 inode_unlock(inode); 1698 WARN_ON_ONCE(1); 1699 return -EINVAL; 1700 } 1701 1702 percpu_down_read_preempt_disable(&file_rwsem); 1703 spin_lock(&ctx->flc_lock); 1704 time_out_leases(inode, &dispose); 1705 error = check_conflicting_open(dentry, arg, lease->fl_flags); 1706 if (error) 1707 goto out; 1708 1709 /* 1710 * At this point, we know that if there is an exclusive 1711 * lease on this file, then we hold it on this filp 1712 * (otherwise our open of this file would have blocked). 1713 * And if we are trying to acquire an exclusive lease, 1714 * then the file is not open by anyone (including us) 1715 * except for this filp. 1716 */ 1717 error = -EAGAIN; 1718 list_for_each_entry(fl, &ctx->flc_lease, fl_list) { 1719 if (fl->fl_file == filp && 1720 fl->fl_owner == lease->fl_owner) { 1721 my_fl = fl; 1722 continue; 1723 } 1724 1725 /* 1726 * No exclusive leases if someone else has a lease on 1727 * this file: 1728 */ 1729 if (arg == F_WRLCK) 1730 goto out; 1731 /* 1732 * Modifying our existing lease is OK, but no getting a 1733 * new lease if someone else is opening for write: 1734 */ 1735 if (fl->fl_flags & FL_UNLOCK_PENDING) 1736 goto out; 1737 } 1738 1739 if (my_fl != NULL) { 1740 lease = my_fl; 1741 error = lease->fl_lmops->lm_change(lease, arg, &dispose); 1742 if (error) 1743 goto out; 1744 goto out_setup; 1745 } 1746 1747 error = -EINVAL; 1748 if (!leases_enable) 1749 goto out; 1750 1751 locks_insert_lock_ctx(lease, &ctx->flc_lease); 1752 /* 1753 * The check in break_lease() is lockless. It's possible for another 1754 * open to race in after we did the earlier check for a conflicting 1755 * open but before the lease was inserted. Check again for a 1756 * conflicting open and cancel the lease if there is one. 1757 * 1758 * We also add a barrier here to ensure that the insertion of the lock 1759 * precedes these checks. 1760 */ 1761 smp_mb(); 1762 error = check_conflicting_open(dentry, arg, lease->fl_flags); 1763 if (error) { 1764 locks_unlink_lock_ctx(lease); 1765 goto out; 1766 } 1767 1768 out_setup: 1769 if (lease->fl_lmops->lm_setup) 1770 lease->fl_lmops->lm_setup(lease, priv); 1771 out: 1772 spin_unlock(&ctx->flc_lock); 1773 percpu_up_read_preempt_enable(&file_rwsem); 1774 locks_dispose_list(&dispose); 1775 if (is_deleg) 1776 inode_unlock(inode); 1777 if (!error && !my_fl) 1778 *flp = NULL; 1779 return error; 1780 } 1781 1782 static int generic_delete_lease(struct file *filp, void *owner) 1783 { 1784 int error = -EAGAIN; 1785 struct file_lock *fl, *victim = NULL; 1786 struct inode *inode = locks_inode(filp); 1787 struct file_lock_context *ctx; 1788 LIST_HEAD(dispose); 1789 1790 ctx = smp_load_acquire(&inode->i_flctx); 1791 if (!ctx) { 1792 trace_generic_delete_lease(inode, NULL); 1793 return error; 1794 } 1795 1796 percpu_down_read_preempt_disable(&file_rwsem); 1797 spin_lock(&ctx->flc_lock); 1798 list_for_each_entry(fl, &ctx->flc_lease, fl_list) { 1799 if (fl->fl_file == filp && 1800 fl->fl_owner == owner) { 1801 victim = fl; 1802 break; 1803 } 1804 } 1805 trace_generic_delete_lease(inode, victim); 1806 if (victim) 1807 error = fl->fl_lmops->lm_change(victim, F_UNLCK, &dispose); 1808 spin_unlock(&ctx->flc_lock); 1809 percpu_up_read_preempt_enable(&file_rwsem); 1810 locks_dispose_list(&dispose); 1811 return error; 1812 } 1813 1814 /** 1815 * generic_setlease - sets a lease on an open file 1816 * @filp: file pointer 1817 * @arg: type of lease to obtain 1818 * @flp: input - file_lock to use, output - file_lock inserted 1819 * @priv: private data for lm_setup (may be NULL if lm_setup 1820 * doesn't require it) 1821 * 1822 * The (input) flp->fl_lmops->lm_break function is required 1823 * by break_lease(). 1824 */ 1825 int generic_setlease(struct file *filp, long arg, struct file_lock **flp, 1826 void **priv) 1827 { 1828 struct inode *inode = locks_inode(filp); 1829 int error; 1830 1831 if ((!uid_eq(current_fsuid(), inode->i_uid)) && !capable(CAP_LEASE)) 1832 return -EACCES; 1833 if (!S_ISREG(inode->i_mode)) 1834 return -EINVAL; 1835 error = security_file_lock(filp, arg); 1836 if (error) 1837 return error; 1838 1839 switch (arg) { 1840 case F_UNLCK: 1841 return generic_delete_lease(filp, *priv); 1842 case F_RDLCK: 1843 case F_WRLCK: 1844 if (!(*flp)->fl_lmops->lm_break) { 1845 WARN_ON_ONCE(1); 1846 return -ENOLCK; 1847 } 1848 1849 return generic_add_lease(filp, arg, flp, priv); 1850 default: 1851 return -EINVAL; 1852 } 1853 } 1854 EXPORT_SYMBOL(generic_setlease); 1855 1856 /** 1857 * vfs_setlease - sets a lease on an open file 1858 * @filp: file pointer 1859 * @arg: type of lease to obtain 1860 * @lease: file_lock to use when adding a lease 1861 * @priv: private info for lm_setup when adding a lease (may be 1862 * NULL if lm_setup doesn't require it) 1863 * 1864 * Call this to establish a lease on the file. The "lease" argument is not 1865 * used for F_UNLCK requests and may be NULL. For commands that set or alter 1866 * an existing lease, the ``(*lease)->fl_lmops->lm_break`` operation must be 1867 * set; if not, this function will return -ENOLCK (and generate a scary-looking 1868 * stack trace). 1869 * 1870 * The "priv" pointer is passed directly to the lm_setup function as-is. It 1871 * may be NULL if the lm_setup operation doesn't require it. 1872 */ 1873 int 1874 vfs_setlease(struct file *filp, long arg, struct file_lock **lease, void **priv) 1875 { 1876 if (filp->f_op->setlease && is_remote_lock(filp)) 1877 return filp->f_op->setlease(filp, arg, lease, priv); 1878 else 1879 return generic_setlease(filp, arg, lease, priv); 1880 } 1881 EXPORT_SYMBOL_GPL(vfs_setlease); 1882 1883 static int do_fcntl_add_lease(unsigned int fd, struct file *filp, long arg) 1884 { 1885 struct file_lock *fl; 1886 struct fasync_struct *new; 1887 int error; 1888 1889 fl = lease_alloc(filp, arg); 1890 if (IS_ERR(fl)) 1891 return PTR_ERR(fl); 1892 1893 new = fasync_alloc(); 1894 if (!new) { 1895 locks_free_lock(fl); 1896 return -ENOMEM; 1897 } 1898 new->fa_fd = fd; 1899 1900 error = vfs_setlease(filp, arg, &fl, (void **)&new); 1901 if (fl) 1902 locks_free_lock(fl); 1903 if (new) 1904 fasync_free(new); 1905 return error; 1906 } 1907 1908 /** 1909 * fcntl_setlease - sets a lease on an open file 1910 * @fd: open file descriptor 1911 * @filp: file pointer 1912 * @arg: type of lease to obtain 1913 * 1914 * Call this fcntl to establish a lease on the file. 1915 * Note that you also need to call %F_SETSIG to 1916 * receive a signal when the lease is broken. 1917 */ 1918 int fcntl_setlease(unsigned int fd, struct file *filp, long arg) 1919 { 1920 if (arg == F_UNLCK) 1921 return vfs_setlease(filp, F_UNLCK, NULL, (void **)&filp); 1922 return do_fcntl_add_lease(fd, filp, arg); 1923 } 1924 1925 /** 1926 * flock_lock_inode_wait - Apply a FLOCK-style lock to a file 1927 * @inode: inode of the file to apply to 1928 * @fl: The lock to be applied 1929 * 1930 * Apply a FLOCK style lock request to an inode. 1931 */ 1932 static int flock_lock_inode_wait(struct inode *inode, struct file_lock *fl) 1933 { 1934 int error; 1935 might_sleep(); 1936 for (;;) { 1937 error = flock_lock_inode(inode, fl); 1938 if (error != FILE_LOCK_DEFERRED) 1939 break; 1940 error = wait_event_interruptible(fl->fl_wait, !fl->fl_next); 1941 if (!error) 1942 continue; 1943 1944 locks_delete_block(fl); 1945 break; 1946 } 1947 return error; 1948 } 1949 1950 /** 1951 * locks_lock_inode_wait - Apply a lock to an inode 1952 * @inode: inode of the file to apply to 1953 * @fl: The lock to be applied 1954 * 1955 * Apply a POSIX or FLOCK style lock request to an inode. 1956 */ 1957 int locks_lock_inode_wait(struct inode *inode, struct file_lock *fl) 1958 { 1959 int res = 0; 1960 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) { 1961 case FL_POSIX: 1962 res = posix_lock_inode_wait(inode, fl); 1963 break; 1964 case FL_FLOCK: 1965 res = flock_lock_inode_wait(inode, fl); 1966 break; 1967 default: 1968 BUG(); 1969 } 1970 return res; 1971 } 1972 EXPORT_SYMBOL(locks_lock_inode_wait); 1973 1974 /** 1975 * sys_flock: - flock() system call. 1976 * @fd: the file descriptor to lock. 1977 * @cmd: the type of lock to apply. 1978 * 1979 * Apply a %FL_FLOCK style lock to an open file descriptor. 1980 * The @cmd can be one of: 1981 * 1982 * - %LOCK_SH -- a shared lock. 1983 * - %LOCK_EX -- an exclusive lock. 1984 * - %LOCK_UN -- remove an existing lock. 1985 * - %LOCK_MAND -- a 'mandatory' flock. 1986 * This exists to emulate Windows Share Modes. 1987 * 1988 * %LOCK_MAND can be combined with %LOCK_READ or %LOCK_WRITE to allow other 1989 * processes read and write access respectively. 1990 */ 1991 SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd) 1992 { 1993 struct fd f = fdget(fd); 1994 struct file_lock *lock; 1995 int can_sleep, unlock; 1996 int error; 1997 1998 error = -EBADF; 1999 if (!f.file) 2000 goto out; 2001 2002 can_sleep = !(cmd & LOCK_NB); 2003 cmd &= ~LOCK_NB; 2004 unlock = (cmd == LOCK_UN); 2005 2006 if (!unlock && !(cmd & LOCK_MAND) && 2007 !(f.file->f_mode & (FMODE_READ|FMODE_WRITE))) 2008 goto out_putf; 2009 2010 lock = flock_make_lock(f.file, cmd); 2011 if (IS_ERR(lock)) { 2012 error = PTR_ERR(lock); 2013 goto out_putf; 2014 } 2015 2016 if (can_sleep) 2017 lock->fl_flags |= FL_SLEEP; 2018 2019 error = security_file_lock(f.file, lock->fl_type); 2020 if (error) 2021 goto out_free; 2022 2023 if (f.file->f_op->flock && is_remote_lock(f.file)) 2024 error = f.file->f_op->flock(f.file, 2025 (can_sleep) ? F_SETLKW : F_SETLK, 2026 lock); 2027 else 2028 error = locks_lock_file_wait(f.file, lock); 2029 2030 out_free: 2031 locks_free_lock(lock); 2032 2033 out_putf: 2034 fdput(f); 2035 out: 2036 return error; 2037 } 2038 2039 /** 2040 * vfs_test_lock - test file byte range lock 2041 * @filp: The file to test lock for 2042 * @fl: The lock to test; also used to hold result 2043 * 2044 * Returns -ERRNO on failure. Indicates presence of conflicting lock by 2045 * setting conf->fl_type to something other than F_UNLCK. 2046 */ 2047 int vfs_test_lock(struct file *filp, struct file_lock *fl) 2048 { 2049 if (filp->f_op->lock && is_remote_lock(filp)) 2050 return filp->f_op->lock(filp, F_GETLK, fl); 2051 posix_test_lock(filp, fl); 2052 return 0; 2053 } 2054 EXPORT_SYMBOL_GPL(vfs_test_lock); 2055 2056 /** 2057 * locks_translate_pid - translate a file_lock's fl_pid number into a namespace 2058 * @fl: The file_lock who's fl_pid should be translated 2059 * @ns: The namespace into which the pid should be translated 2060 * 2061 * Used to tranlate a fl_pid into a namespace virtual pid number 2062 */ 2063 static pid_t locks_translate_pid(struct file_lock *fl, struct pid_namespace *ns) 2064 { 2065 pid_t vnr; 2066 struct pid *pid; 2067 2068 if (IS_OFDLCK(fl)) 2069 return -1; 2070 if (IS_REMOTELCK(fl)) 2071 return fl->fl_pid; 2072 /* 2073 * If the flock owner process is dead and its pid has been already 2074 * freed, the translation below won't work, but we still want to show 2075 * flock owner pid number in init pidns. 2076 */ 2077 if (ns == &init_pid_ns) 2078 return (pid_t)fl->fl_pid; 2079 2080 rcu_read_lock(); 2081 pid = find_pid_ns(fl->fl_pid, &init_pid_ns); 2082 vnr = pid_nr_ns(pid, ns); 2083 rcu_read_unlock(); 2084 return vnr; 2085 } 2086 2087 static int posix_lock_to_flock(struct flock *flock, struct file_lock *fl) 2088 { 2089 flock->l_pid = locks_translate_pid(fl, task_active_pid_ns(current)); 2090 #if BITS_PER_LONG == 32 2091 /* 2092 * Make sure we can represent the posix lock via 2093 * legacy 32bit flock. 2094 */ 2095 if (fl->fl_start > OFFT_OFFSET_MAX) 2096 return -EOVERFLOW; 2097 if (fl->fl_end != OFFSET_MAX && fl->fl_end > OFFT_OFFSET_MAX) 2098 return -EOVERFLOW; 2099 #endif 2100 flock->l_start = fl->fl_start; 2101 flock->l_len = fl->fl_end == OFFSET_MAX ? 0 : 2102 fl->fl_end - fl->fl_start + 1; 2103 flock->l_whence = 0; 2104 flock->l_type = fl->fl_type; 2105 return 0; 2106 } 2107 2108 #if BITS_PER_LONG == 32 2109 static void posix_lock_to_flock64(struct flock64 *flock, struct file_lock *fl) 2110 { 2111 flock->l_pid = locks_translate_pid(fl, task_active_pid_ns(current)); 2112 flock->l_start = fl->fl_start; 2113 flock->l_len = fl->fl_end == OFFSET_MAX ? 0 : 2114 fl->fl_end - fl->fl_start + 1; 2115 flock->l_whence = 0; 2116 flock->l_type = fl->fl_type; 2117 } 2118 #endif 2119 2120 /* Report the first existing lock that would conflict with l. 2121 * This implements the F_GETLK command of fcntl(). 2122 */ 2123 int fcntl_getlk(struct file *filp, unsigned int cmd, struct flock *flock) 2124 { 2125 struct file_lock *fl; 2126 int error; 2127 2128 fl = locks_alloc_lock(); 2129 if (fl == NULL) 2130 return -ENOMEM; 2131 error = -EINVAL; 2132 if (flock->l_type != F_RDLCK && flock->l_type != F_WRLCK) 2133 goto out; 2134 2135 error = flock_to_posix_lock(filp, fl, flock); 2136 if (error) 2137 goto out; 2138 2139 if (cmd == F_OFD_GETLK) { 2140 error = -EINVAL; 2141 if (flock->l_pid != 0) 2142 goto out; 2143 2144 cmd = F_GETLK; 2145 fl->fl_flags |= FL_OFDLCK; 2146 fl->fl_owner = filp; 2147 } 2148 2149 error = vfs_test_lock(filp, fl); 2150 if (error) 2151 goto out; 2152 2153 flock->l_type = fl->fl_type; 2154 if (fl->fl_type != F_UNLCK) { 2155 error = posix_lock_to_flock(flock, fl); 2156 if (error) 2157 goto out; 2158 } 2159 out: 2160 locks_free_lock(fl); 2161 return error; 2162 } 2163 2164 /** 2165 * vfs_lock_file - file byte range lock 2166 * @filp: The file to apply the lock to 2167 * @cmd: type of locking operation (F_SETLK, F_GETLK, etc.) 2168 * @fl: The lock to be applied 2169 * @conf: Place to return a copy of the conflicting lock, if found. 2170 * 2171 * A caller that doesn't care about the conflicting lock may pass NULL 2172 * as the final argument. 2173 * 2174 * If the filesystem defines a private ->lock() method, then @conf will 2175 * be left unchanged; so a caller that cares should initialize it to 2176 * some acceptable default. 2177 * 2178 * To avoid blocking kernel daemons, such as lockd, that need to acquire POSIX 2179 * locks, the ->lock() interface may return asynchronously, before the lock has 2180 * been granted or denied by the underlying filesystem, if (and only if) 2181 * lm_grant is set. Callers expecting ->lock() to return asynchronously 2182 * will only use F_SETLK, not F_SETLKW; they will set FL_SLEEP if (and only if) 2183 * the request is for a blocking lock. When ->lock() does return asynchronously, 2184 * it must return FILE_LOCK_DEFERRED, and call ->lm_grant() when the lock 2185 * request completes. 2186 * If the request is for non-blocking lock the file system should return 2187 * FILE_LOCK_DEFERRED then try to get the lock and call the callback routine 2188 * with the result. If the request timed out the callback routine will return a 2189 * nonzero return code and the file system should release the lock. The file 2190 * system is also responsible to keep a corresponding posix lock when it 2191 * grants a lock so the VFS can find out which locks are locally held and do 2192 * the correct lock cleanup when required. 2193 * The underlying filesystem must not drop the kernel lock or call 2194 * ->lm_grant() before returning to the caller with a FILE_LOCK_DEFERRED 2195 * return code. 2196 */ 2197 int vfs_lock_file(struct file *filp, unsigned int cmd, struct file_lock *fl, struct file_lock *conf) 2198 { 2199 if (filp->f_op->lock && is_remote_lock(filp)) 2200 return filp->f_op->lock(filp, cmd, fl); 2201 else 2202 return posix_lock_file(filp, fl, conf); 2203 } 2204 EXPORT_SYMBOL_GPL(vfs_lock_file); 2205 2206 static int do_lock_file_wait(struct file *filp, unsigned int cmd, 2207 struct file_lock *fl) 2208 { 2209 int error; 2210 2211 error = security_file_lock(filp, fl->fl_type); 2212 if (error) 2213 return error; 2214 2215 for (;;) { 2216 error = vfs_lock_file(filp, cmd, fl, NULL); 2217 if (error != FILE_LOCK_DEFERRED) 2218 break; 2219 error = wait_event_interruptible(fl->fl_wait, !fl->fl_next); 2220 if (!error) 2221 continue; 2222 2223 locks_delete_block(fl); 2224 break; 2225 } 2226 2227 return error; 2228 } 2229 2230 /* Ensure that fl->fl_file has compatible f_mode for F_SETLK calls */ 2231 static int 2232 check_fmode_for_setlk(struct file_lock *fl) 2233 { 2234 switch (fl->fl_type) { 2235 case F_RDLCK: 2236 if (!(fl->fl_file->f_mode & FMODE_READ)) 2237 return -EBADF; 2238 break; 2239 case F_WRLCK: 2240 if (!(fl->fl_file->f_mode & FMODE_WRITE)) 2241 return -EBADF; 2242 } 2243 return 0; 2244 } 2245 2246 /* Apply the lock described by l to an open file descriptor. 2247 * This implements both the F_SETLK and F_SETLKW commands of fcntl(). 2248 */ 2249 int fcntl_setlk(unsigned int fd, struct file *filp, unsigned int cmd, 2250 struct flock *flock) 2251 { 2252 struct file_lock *file_lock = locks_alloc_lock(); 2253 struct inode *inode = locks_inode(filp); 2254 struct file *f; 2255 int error; 2256 2257 if (file_lock == NULL) 2258 return -ENOLCK; 2259 2260 /* Don't allow mandatory locks on files that may be memory mapped 2261 * and shared. 2262 */ 2263 if (mandatory_lock(inode) && mapping_writably_mapped(filp->f_mapping)) { 2264 error = -EAGAIN; 2265 goto out; 2266 } 2267 2268 error = flock_to_posix_lock(filp, file_lock, flock); 2269 if (error) 2270 goto out; 2271 2272 error = check_fmode_for_setlk(file_lock); 2273 if (error) 2274 goto out; 2275 2276 /* 2277 * If the cmd is requesting file-private locks, then set the 2278 * FL_OFDLCK flag and override the owner. 2279 */ 2280 switch (cmd) { 2281 case F_OFD_SETLK: 2282 error = -EINVAL; 2283 if (flock->l_pid != 0) 2284 goto out; 2285 2286 cmd = F_SETLK; 2287 file_lock->fl_flags |= FL_OFDLCK; 2288 file_lock->fl_owner = filp; 2289 break; 2290 case F_OFD_SETLKW: 2291 error = -EINVAL; 2292 if (flock->l_pid != 0) 2293 goto out; 2294 2295 cmd = F_SETLKW; 2296 file_lock->fl_flags |= FL_OFDLCK; 2297 file_lock->fl_owner = filp; 2298 /* Fallthrough */ 2299 case F_SETLKW: 2300 file_lock->fl_flags |= FL_SLEEP; 2301 } 2302 2303 error = do_lock_file_wait(filp, cmd, file_lock); 2304 2305 /* 2306 * Attempt to detect a close/fcntl race and recover by releasing the 2307 * lock that was just acquired. There is no need to do that when we're 2308 * unlocking though, or for OFD locks. 2309 */ 2310 if (!error && file_lock->fl_type != F_UNLCK && 2311 !(file_lock->fl_flags & FL_OFDLCK)) { 2312 /* 2313 * We need that spin_lock here - it prevents reordering between 2314 * update of i_flctx->flc_posix and check for it done in 2315 * close(). rcu_read_lock() wouldn't do. 2316 */ 2317 spin_lock(¤t->files->file_lock); 2318 f = fcheck(fd); 2319 spin_unlock(¤t->files->file_lock); 2320 if (f != filp) { 2321 file_lock->fl_type = F_UNLCK; 2322 error = do_lock_file_wait(filp, cmd, file_lock); 2323 WARN_ON_ONCE(error); 2324 error = -EBADF; 2325 } 2326 } 2327 out: 2328 trace_fcntl_setlk(inode, file_lock, error); 2329 locks_free_lock(file_lock); 2330 return error; 2331 } 2332 2333 #if BITS_PER_LONG == 32 2334 /* Report the first existing lock that would conflict with l. 2335 * This implements the F_GETLK command of fcntl(). 2336 */ 2337 int fcntl_getlk64(struct file *filp, unsigned int cmd, struct flock64 *flock) 2338 { 2339 struct file_lock *fl; 2340 int error; 2341 2342 fl = locks_alloc_lock(); 2343 if (fl == NULL) 2344 return -ENOMEM; 2345 2346 error = -EINVAL; 2347 if (flock->l_type != F_RDLCK && flock->l_type != F_WRLCK) 2348 goto out; 2349 2350 error = flock64_to_posix_lock(filp, fl, flock); 2351 if (error) 2352 goto out; 2353 2354 if (cmd == F_OFD_GETLK) { 2355 error = -EINVAL; 2356 if (flock->l_pid != 0) 2357 goto out; 2358 2359 cmd = F_GETLK64; 2360 fl->fl_flags |= FL_OFDLCK; 2361 fl->fl_owner = filp; 2362 } 2363 2364 error = vfs_test_lock(filp, fl); 2365 if (error) 2366 goto out; 2367 2368 flock->l_type = fl->fl_type; 2369 if (fl->fl_type != F_UNLCK) 2370 posix_lock_to_flock64(flock, fl); 2371 2372 out: 2373 locks_free_lock(fl); 2374 return error; 2375 } 2376 2377 /* Apply the lock described by l to an open file descriptor. 2378 * This implements both the F_SETLK and F_SETLKW commands of fcntl(). 2379 */ 2380 int fcntl_setlk64(unsigned int fd, struct file *filp, unsigned int cmd, 2381 struct flock64 *flock) 2382 { 2383 struct file_lock *file_lock = locks_alloc_lock(); 2384 struct inode *inode = locks_inode(filp); 2385 struct file *f; 2386 int error; 2387 2388 if (file_lock == NULL) 2389 return -ENOLCK; 2390 2391 /* Don't allow mandatory locks on files that may be memory mapped 2392 * and shared. 2393 */ 2394 if (mandatory_lock(inode) && mapping_writably_mapped(filp->f_mapping)) { 2395 error = -EAGAIN; 2396 goto out; 2397 } 2398 2399 error = flock64_to_posix_lock(filp, file_lock, flock); 2400 if (error) 2401 goto out; 2402 2403 error = check_fmode_for_setlk(file_lock); 2404 if (error) 2405 goto out; 2406 2407 /* 2408 * If the cmd is requesting file-private locks, then set the 2409 * FL_OFDLCK flag and override the owner. 2410 */ 2411 switch (cmd) { 2412 case F_OFD_SETLK: 2413 error = -EINVAL; 2414 if (flock->l_pid != 0) 2415 goto out; 2416 2417 cmd = F_SETLK64; 2418 file_lock->fl_flags |= FL_OFDLCK; 2419 file_lock->fl_owner = filp; 2420 break; 2421 case F_OFD_SETLKW: 2422 error = -EINVAL; 2423 if (flock->l_pid != 0) 2424 goto out; 2425 2426 cmd = F_SETLKW64; 2427 file_lock->fl_flags |= FL_OFDLCK; 2428 file_lock->fl_owner = filp; 2429 /* Fallthrough */ 2430 case F_SETLKW64: 2431 file_lock->fl_flags |= FL_SLEEP; 2432 } 2433 2434 error = do_lock_file_wait(filp, cmd, file_lock); 2435 2436 /* 2437 * Attempt to detect a close/fcntl race and recover by releasing the 2438 * lock that was just acquired. There is no need to do that when we're 2439 * unlocking though, or for OFD locks. 2440 */ 2441 if (!error && file_lock->fl_type != F_UNLCK && 2442 !(file_lock->fl_flags & FL_OFDLCK)) { 2443 /* 2444 * We need that spin_lock here - it prevents reordering between 2445 * update of i_flctx->flc_posix and check for it done in 2446 * close(). rcu_read_lock() wouldn't do. 2447 */ 2448 spin_lock(¤t->files->file_lock); 2449 f = fcheck(fd); 2450 spin_unlock(¤t->files->file_lock); 2451 if (f != filp) { 2452 file_lock->fl_type = F_UNLCK; 2453 error = do_lock_file_wait(filp, cmd, file_lock); 2454 WARN_ON_ONCE(error); 2455 error = -EBADF; 2456 } 2457 } 2458 out: 2459 locks_free_lock(file_lock); 2460 return error; 2461 } 2462 #endif /* BITS_PER_LONG == 32 */ 2463 2464 /* 2465 * This function is called when the file is being removed 2466 * from the task's fd array. POSIX locks belonging to this task 2467 * are deleted at this time. 2468 */ 2469 void locks_remove_posix(struct file *filp, fl_owner_t owner) 2470 { 2471 int error; 2472 struct inode *inode = locks_inode(filp); 2473 struct file_lock lock; 2474 struct file_lock_context *ctx; 2475 2476 /* 2477 * If there are no locks held on this file, we don't need to call 2478 * posix_lock_file(). Another process could be setting a lock on this 2479 * file at the same time, but we wouldn't remove that lock anyway. 2480 */ 2481 ctx = smp_load_acquire(&inode->i_flctx); 2482 if (!ctx || list_empty(&ctx->flc_posix)) 2483 return; 2484 2485 lock.fl_type = F_UNLCK; 2486 lock.fl_flags = FL_POSIX | FL_CLOSE; 2487 lock.fl_start = 0; 2488 lock.fl_end = OFFSET_MAX; 2489 lock.fl_owner = owner; 2490 lock.fl_pid = current->tgid; 2491 lock.fl_file = filp; 2492 lock.fl_ops = NULL; 2493 lock.fl_lmops = NULL; 2494 2495 error = vfs_lock_file(filp, F_SETLK, &lock, NULL); 2496 2497 if (lock.fl_ops && lock.fl_ops->fl_release_private) 2498 lock.fl_ops->fl_release_private(&lock); 2499 trace_locks_remove_posix(inode, &lock, error); 2500 } 2501 2502 EXPORT_SYMBOL(locks_remove_posix); 2503 2504 /* The i_flctx must be valid when calling into here */ 2505 static void 2506 locks_remove_flock(struct file *filp, struct file_lock_context *flctx) 2507 { 2508 struct file_lock fl = { 2509 .fl_owner = filp, 2510 .fl_pid = current->tgid, 2511 .fl_file = filp, 2512 .fl_flags = FL_FLOCK | FL_CLOSE, 2513 .fl_type = F_UNLCK, 2514 .fl_end = OFFSET_MAX, 2515 }; 2516 struct inode *inode = locks_inode(filp); 2517 2518 if (list_empty(&flctx->flc_flock)) 2519 return; 2520 2521 if (filp->f_op->flock && is_remote_lock(filp)) 2522 filp->f_op->flock(filp, F_SETLKW, &fl); 2523 else 2524 flock_lock_inode(inode, &fl); 2525 2526 if (fl.fl_ops && fl.fl_ops->fl_release_private) 2527 fl.fl_ops->fl_release_private(&fl); 2528 } 2529 2530 /* The i_flctx must be valid when calling into here */ 2531 static void 2532 locks_remove_lease(struct file *filp, struct file_lock_context *ctx) 2533 { 2534 struct file_lock *fl, *tmp; 2535 LIST_HEAD(dispose); 2536 2537 if (list_empty(&ctx->flc_lease)) 2538 return; 2539 2540 percpu_down_read_preempt_disable(&file_rwsem); 2541 spin_lock(&ctx->flc_lock); 2542 list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list) 2543 if (filp == fl->fl_file) 2544 lease_modify(fl, F_UNLCK, &dispose); 2545 spin_unlock(&ctx->flc_lock); 2546 percpu_up_read_preempt_enable(&file_rwsem); 2547 2548 locks_dispose_list(&dispose); 2549 } 2550 2551 /* 2552 * This function is called on the last close of an open file. 2553 */ 2554 void locks_remove_file(struct file *filp) 2555 { 2556 struct file_lock_context *ctx; 2557 2558 ctx = smp_load_acquire(&locks_inode(filp)->i_flctx); 2559 if (!ctx) 2560 return; 2561 2562 /* remove any OFD locks */ 2563 locks_remove_posix(filp, filp); 2564 2565 /* remove flock locks */ 2566 locks_remove_flock(filp, ctx); 2567 2568 /* remove any leases */ 2569 locks_remove_lease(filp, ctx); 2570 2571 spin_lock(&ctx->flc_lock); 2572 locks_check_ctx_file_list(filp, &ctx->flc_posix, "POSIX"); 2573 locks_check_ctx_file_list(filp, &ctx->flc_flock, "FLOCK"); 2574 locks_check_ctx_file_list(filp, &ctx->flc_lease, "LEASE"); 2575 spin_unlock(&ctx->flc_lock); 2576 } 2577 2578 /** 2579 * posix_unblock_lock - stop waiting for a file lock 2580 * @waiter: the lock which was waiting 2581 * 2582 * lockd needs to block waiting for locks. 2583 */ 2584 int 2585 posix_unblock_lock(struct file_lock *waiter) 2586 { 2587 int status = 0; 2588 2589 spin_lock(&blocked_lock_lock); 2590 if (waiter->fl_next) 2591 __locks_delete_block(waiter); 2592 else 2593 status = -ENOENT; 2594 spin_unlock(&blocked_lock_lock); 2595 return status; 2596 } 2597 EXPORT_SYMBOL(posix_unblock_lock); 2598 2599 /** 2600 * vfs_cancel_lock - file byte range unblock lock 2601 * @filp: The file to apply the unblock to 2602 * @fl: The lock to be unblocked 2603 * 2604 * Used by lock managers to cancel blocked requests 2605 */ 2606 int vfs_cancel_lock(struct file *filp, struct file_lock *fl) 2607 { 2608 if (filp->f_op->lock && is_remote_lock(filp)) 2609 return filp->f_op->lock(filp, F_CANCELLK, fl); 2610 return 0; 2611 } 2612 2613 EXPORT_SYMBOL_GPL(vfs_cancel_lock); 2614 2615 #ifdef CONFIG_PROC_FS 2616 #include <linux/proc_fs.h> 2617 #include <linux/seq_file.h> 2618 2619 struct locks_iterator { 2620 int li_cpu; 2621 loff_t li_pos; 2622 }; 2623 2624 static void lock_get_status(struct seq_file *f, struct file_lock *fl, 2625 loff_t id, char *pfx) 2626 { 2627 struct inode *inode = NULL; 2628 unsigned int fl_pid; 2629 struct pid_namespace *proc_pidns = file_inode(f->file)->i_sb->s_fs_info; 2630 2631 fl_pid = locks_translate_pid(fl, proc_pidns); 2632 /* 2633 * If lock owner is dead (and pid is freed) or not visible in current 2634 * pidns, zero is shown as a pid value. Check lock info from 2635 * init_pid_ns to get saved lock pid value. 2636 */ 2637 2638 if (fl->fl_file != NULL) 2639 inode = locks_inode(fl->fl_file); 2640 2641 seq_printf(f, "%lld:%s ", id, pfx); 2642 if (IS_POSIX(fl)) { 2643 if (fl->fl_flags & FL_ACCESS) 2644 seq_puts(f, "ACCESS"); 2645 else if (IS_OFDLCK(fl)) 2646 seq_puts(f, "OFDLCK"); 2647 else 2648 seq_puts(f, "POSIX "); 2649 2650 seq_printf(f, " %s ", 2651 (inode == NULL) ? "*NOINODE*" : 2652 mandatory_lock(inode) ? "MANDATORY" : "ADVISORY "); 2653 } else if (IS_FLOCK(fl)) { 2654 if (fl->fl_type & LOCK_MAND) { 2655 seq_puts(f, "FLOCK MSNFS "); 2656 } else { 2657 seq_puts(f, "FLOCK ADVISORY "); 2658 } 2659 } else if (IS_LEASE(fl)) { 2660 if (fl->fl_flags & FL_DELEG) 2661 seq_puts(f, "DELEG "); 2662 else 2663 seq_puts(f, "LEASE "); 2664 2665 if (lease_breaking(fl)) 2666 seq_puts(f, "BREAKING "); 2667 else if (fl->fl_file) 2668 seq_puts(f, "ACTIVE "); 2669 else 2670 seq_puts(f, "BREAKER "); 2671 } else { 2672 seq_puts(f, "UNKNOWN UNKNOWN "); 2673 } 2674 if (fl->fl_type & LOCK_MAND) { 2675 seq_printf(f, "%s ", 2676 (fl->fl_type & LOCK_READ) 2677 ? (fl->fl_type & LOCK_WRITE) ? "RW " : "READ " 2678 : (fl->fl_type & LOCK_WRITE) ? "WRITE" : "NONE "); 2679 } else { 2680 seq_printf(f, "%s ", 2681 (lease_breaking(fl)) 2682 ? (fl->fl_type == F_UNLCK) ? "UNLCK" : "READ " 2683 : (fl->fl_type == F_WRLCK) ? "WRITE" : "READ "); 2684 } 2685 if (inode) { 2686 /* userspace relies on this representation of dev_t */ 2687 seq_printf(f, "%d %02x:%02x:%ld ", fl_pid, 2688 MAJOR(inode->i_sb->s_dev), 2689 MINOR(inode->i_sb->s_dev), inode->i_ino); 2690 } else { 2691 seq_printf(f, "%d <none>:0 ", fl_pid); 2692 } 2693 if (IS_POSIX(fl)) { 2694 if (fl->fl_end == OFFSET_MAX) 2695 seq_printf(f, "%Ld EOF\n", fl->fl_start); 2696 else 2697 seq_printf(f, "%Ld %Ld\n", fl->fl_start, fl->fl_end); 2698 } else { 2699 seq_puts(f, "0 EOF\n"); 2700 } 2701 } 2702 2703 static int locks_show(struct seq_file *f, void *v) 2704 { 2705 struct locks_iterator *iter = f->private; 2706 struct file_lock *fl, *bfl; 2707 struct pid_namespace *proc_pidns = file_inode(f->file)->i_sb->s_fs_info; 2708 2709 fl = hlist_entry(v, struct file_lock, fl_link); 2710 2711 if (locks_translate_pid(fl, proc_pidns) == 0) 2712 return 0; 2713 2714 lock_get_status(f, fl, iter->li_pos, ""); 2715 2716 list_for_each_entry(bfl, &fl->fl_block, fl_block) 2717 lock_get_status(f, bfl, iter->li_pos, " ->"); 2718 2719 return 0; 2720 } 2721 2722 static void __show_fd_locks(struct seq_file *f, 2723 struct list_head *head, int *id, 2724 struct file *filp, struct files_struct *files) 2725 { 2726 struct file_lock *fl; 2727 2728 list_for_each_entry(fl, head, fl_list) { 2729 2730 if (filp != fl->fl_file) 2731 continue; 2732 if (fl->fl_owner != files && 2733 fl->fl_owner != filp) 2734 continue; 2735 2736 (*id)++; 2737 seq_puts(f, "lock:\t"); 2738 lock_get_status(f, fl, *id, ""); 2739 } 2740 } 2741 2742 void show_fd_locks(struct seq_file *f, 2743 struct file *filp, struct files_struct *files) 2744 { 2745 struct inode *inode = locks_inode(filp); 2746 struct file_lock_context *ctx; 2747 int id = 0; 2748 2749 ctx = smp_load_acquire(&inode->i_flctx); 2750 if (!ctx) 2751 return; 2752 2753 spin_lock(&ctx->flc_lock); 2754 __show_fd_locks(f, &ctx->flc_flock, &id, filp, files); 2755 __show_fd_locks(f, &ctx->flc_posix, &id, filp, files); 2756 __show_fd_locks(f, &ctx->flc_lease, &id, filp, files); 2757 spin_unlock(&ctx->flc_lock); 2758 } 2759 2760 static void *locks_start(struct seq_file *f, loff_t *pos) 2761 __acquires(&blocked_lock_lock) 2762 { 2763 struct locks_iterator *iter = f->private; 2764 2765 iter->li_pos = *pos + 1; 2766 percpu_down_write(&file_rwsem); 2767 spin_lock(&blocked_lock_lock); 2768 return seq_hlist_start_percpu(&file_lock_list.hlist, &iter->li_cpu, *pos); 2769 } 2770 2771 static void *locks_next(struct seq_file *f, void *v, loff_t *pos) 2772 { 2773 struct locks_iterator *iter = f->private; 2774 2775 ++iter->li_pos; 2776 return seq_hlist_next_percpu(v, &file_lock_list.hlist, &iter->li_cpu, pos); 2777 } 2778 2779 static void locks_stop(struct seq_file *f, void *v) 2780 __releases(&blocked_lock_lock) 2781 { 2782 spin_unlock(&blocked_lock_lock); 2783 percpu_up_write(&file_rwsem); 2784 } 2785 2786 static const struct seq_operations locks_seq_operations = { 2787 .start = locks_start, 2788 .next = locks_next, 2789 .stop = locks_stop, 2790 .show = locks_show, 2791 }; 2792 2793 static int __init proc_locks_init(void) 2794 { 2795 proc_create_seq_private("locks", 0, NULL, &locks_seq_operations, 2796 sizeof(struct locks_iterator), NULL); 2797 return 0; 2798 } 2799 fs_initcall(proc_locks_init); 2800 #endif 2801 2802 static int __init filelock_init(void) 2803 { 2804 int i; 2805 2806 flctx_cache = kmem_cache_create("file_lock_ctx", 2807 sizeof(struct file_lock_context), 0, SLAB_PANIC, NULL); 2808 2809 filelock_cache = kmem_cache_create("file_lock_cache", 2810 sizeof(struct file_lock), 0, SLAB_PANIC, NULL); 2811 2812 2813 for_each_possible_cpu(i) { 2814 struct file_lock_list_struct *fll = per_cpu_ptr(&file_lock_list, i); 2815 2816 spin_lock_init(&fll->lock); 2817 INIT_HLIST_HEAD(&fll->hlist); 2818 } 2819 2820 return 0; 2821 } 2822 2823 core_initcall(filelock_init); 2824