Lines Matching +full:input +full:- +full:justification
1 // SPDX-License-Identifier: GPL-2.0-only
19 * - the root of a tree may be an applied or waiting lock.
20 * - every other node in the tree is a waiting lock that
72 #define IS_POSIX(fl) (fl->fl_flags & FL_POSIX)
73 #define IS_FLOCK(fl) (fl->fl_flags & FL_FLOCK)
74 #define IS_LEASE(fl) (fl->fl_flags & (FL_LEASE|FL_DELEG|FL_LAYOUT))
75 #define IS_OFDLCK(fl) (fl->fl_flags & FL_OFDLCK)
76 #define IS_REMOTELCK(fl) (fl->fl_pid <= 0)
80 return fl->fl_flags & (FL_UNLOCK_PENDING | FL_DOWNGRADE_PENDING); in lease_breaking()
85 if (fl->fl_flags & FL_UNLOCK_PENDING) in target_leasetype()
87 if (fl->fl_flags & FL_DOWNGRADE_PENDING) in target_leasetype()
89 return fl->fl_type; in target_leasetype()
98 .procname = "leases-enable",
106 .procname = "lease-break-time",
158 * In addition, it also protects the fl->fl_blocked_requests list, and the
159 * fl->fl_blocker pointer for file_lock structures that are acting as lock
178 ctx = smp_load_acquire(&inode->i_flctx); in locks_get_lock_context()
186 spin_lock_init(&ctx->flc_lock); in locks_get_lock_context()
187 INIT_LIST_HEAD(&ctx->flc_flock); in locks_get_lock_context()
188 INIT_LIST_HEAD(&ctx->flc_posix); in locks_get_lock_context()
189 INIT_LIST_HEAD(&ctx->flc_lease); in locks_get_lock_context()
195 if (cmpxchg(&inode->i_flctx, NULL, ctx)) { in locks_get_lock_context()
197 ctx = smp_load_acquire(&inode->i_flctx); in locks_get_lock_context()
210 … fl_flags=0x%x fl_type=0x%x fl_pid=%u\n", list_type, fl->fl_owner, fl->fl_flags, fl->fl_type, fl->… in locks_dump_ctx_list()
217 struct file_lock_context *ctx = inode->i_flctx; in locks_check_ctx_lists()
219 if (unlikely(!list_empty(&ctx->flc_flock) || in locks_check_ctx_lists()
220 !list_empty(&ctx->flc_posix) || in locks_check_ctx_lists()
221 !list_empty(&ctx->flc_lease))) { in locks_check_ctx_lists()
223 MAJOR(inode->i_sb->s_dev), MINOR(inode->i_sb->s_dev), in locks_check_ctx_lists()
224 inode->i_ino); in locks_check_ctx_lists()
225 locks_dump_ctx_list(&ctx->flc_flock, "FLOCK"); in locks_check_ctx_lists()
226 locks_dump_ctx_list(&ctx->flc_posix, "POSIX"); in locks_check_ctx_lists()
227 locks_dump_ctx_list(&ctx->flc_lease, "LEASE"); in locks_check_ctx_lists()
239 if (fl->fl_file == filp) in locks_check_ctx_file_list()
242 list_type, MAJOR(inode->i_sb->s_dev), in locks_check_ctx_file_list()
243 MINOR(inode->i_sb->s_dev), inode->i_ino, in locks_check_ctx_file_list()
244 fl->fl_owner, fl->fl_flags, fl->fl_type, fl->fl_pid); in locks_check_ctx_file_list()
250 struct file_lock_context *ctx = inode->i_flctx; in locks_free_lock_context()
260 INIT_HLIST_NODE(&fl->fl_link); in locks_init_lock_heads()
261 INIT_LIST_HEAD(&fl->fl_list); in locks_init_lock_heads()
262 INIT_LIST_HEAD(&fl->fl_blocked_requests); in locks_init_lock_heads()
263 INIT_LIST_HEAD(&fl->fl_blocked_member); in locks_init_lock_heads()
264 init_waitqueue_head(&fl->fl_wait); in locks_init_lock_heads()
281 BUG_ON(waitqueue_active(&fl->fl_wait)); in locks_release_private()
282 BUG_ON(!list_empty(&fl->fl_list)); in locks_release_private()
283 BUG_ON(!list_empty(&fl->fl_blocked_requests)); in locks_release_private()
284 BUG_ON(!list_empty(&fl->fl_blocked_member)); in locks_release_private()
285 BUG_ON(!hlist_unhashed(&fl->fl_link)); in locks_release_private()
287 if (fl->fl_ops) { in locks_release_private()
288 if (fl->fl_ops->fl_release_private) in locks_release_private()
289 fl->fl_ops->fl_release_private(fl); in locks_release_private()
290 fl->fl_ops = NULL; in locks_release_private()
293 if (fl->fl_lmops) { in locks_release_private()
294 if (fl->fl_lmops->lm_put_owner) { in locks_release_private()
295 fl->fl_lmops->lm_put_owner(fl->fl_owner); in locks_release_private()
296 fl->fl_owner = NULL; in locks_release_private()
298 fl->fl_lmops = NULL; in locks_release_private()
304 * locks_owner_has_blockers - Check for blocking lock requests
317 spin_lock(&flctx->flc_lock); in locks_owner_has_blockers()
318 list_for_each_entry(fl, &flctx->flc_posix, fl_list) { in locks_owner_has_blockers()
319 if (fl->fl_owner != owner) in locks_owner_has_blockers()
321 if (!list_empty(&fl->fl_blocked_requests)) { in locks_owner_has_blockers()
322 spin_unlock(&flctx->flc_lock); in locks_owner_has_blockers()
326 spin_unlock(&flctx->flc_lock); in locks_owner_has_blockers()
346 list_del_init(&fl->fl_list); in locks_dispose_list()
363 new->fl_owner = fl->fl_owner; in locks_copy_conflock()
364 new->fl_pid = fl->fl_pid; in locks_copy_conflock()
365 new->fl_file = NULL; in locks_copy_conflock()
366 new->fl_flags = fl->fl_flags; in locks_copy_conflock()
367 new->fl_type = fl->fl_type; in locks_copy_conflock()
368 new->fl_start = fl->fl_start; in locks_copy_conflock()
369 new->fl_end = fl->fl_end; in locks_copy_conflock()
370 new->fl_lmops = fl->fl_lmops; in locks_copy_conflock()
371 new->fl_ops = NULL; in locks_copy_conflock()
373 if (fl->fl_lmops) { in locks_copy_conflock()
374 if (fl->fl_lmops->lm_get_owner) in locks_copy_conflock()
375 fl->fl_lmops->lm_get_owner(fl->fl_owner); in locks_copy_conflock()
382 /* "new" must be a freshly-initialized lock */ in locks_copy_lock()
383 WARN_ON_ONCE(new->fl_ops); in locks_copy_lock()
387 new->fl_file = fl->fl_file; in locks_copy_lock()
388 new->fl_ops = fl->fl_ops; in locks_copy_lock()
390 if (fl->fl_ops) { in locks_copy_lock()
391 if (fl->fl_ops->fl_copy_lock) in locks_copy_lock()
392 fl->fl_ops->fl_copy_lock(new, fl); in locks_copy_lock()
402 * As ctx->flc_lock is held, new requests cannot be added to in locks_move_blocks()
403 * ->fl_blocked_requests, so we don't need a lock to check if it in locks_move_blocks()
406 if (list_empty(&fl->fl_blocked_requests)) in locks_move_blocks()
409 list_splice_init(&fl->fl_blocked_requests, &new->fl_blocked_requests); in locks_move_blocks()
410 list_for_each_entry(f, &new->fl_blocked_requests, fl_blocked_member) in locks_move_blocks()
411 f->fl_blocker = new; in locks_move_blocks()
424 return -EINVAL; in flock_translate_cmd()
432 fl->fl_file = filp; in flock_make_lock()
433 fl->fl_owner = filp; in flock_make_lock()
434 fl->fl_pid = current->tgid; in flock_make_lock()
435 fl->fl_flags = FL_FLOCK; in flock_make_lock()
436 fl->fl_type = type; in flock_make_lock()
437 fl->fl_end = OFFSET_MAX; in flock_make_lock()
446 fl->fl_type = type; in assign_type()
449 return -EINVAL; in assign_type()
457 switch (l->l_whence) { in flock64_to_posix_lock()
459 fl->fl_start = 0; in flock64_to_posix_lock()
462 fl->fl_start = filp->f_pos; in flock64_to_posix_lock()
465 fl->fl_start = i_size_read(file_inode(filp)); in flock64_to_posix_lock()
468 return -EINVAL; in flock64_to_posix_lock()
470 if (l->l_start > OFFSET_MAX - fl->fl_start) in flock64_to_posix_lock()
471 return -EOVERFLOW; in flock64_to_posix_lock()
472 fl->fl_start += l->l_start; in flock64_to_posix_lock()
473 if (fl->fl_start < 0) in flock64_to_posix_lock()
474 return -EINVAL; in flock64_to_posix_lock()
476 /* POSIX-1996 leaves the case l->l_len < 0 undefined; in flock64_to_posix_lock()
477 POSIX-2001 defines it. */ in flock64_to_posix_lock()
478 if (l->l_len > 0) { in flock64_to_posix_lock()
479 if (l->l_len - 1 > OFFSET_MAX - fl->fl_start) in flock64_to_posix_lock()
480 return -EOVERFLOW; in flock64_to_posix_lock()
481 fl->fl_end = fl->fl_start + (l->l_len - 1); in flock64_to_posix_lock()
483 } else if (l->l_len < 0) { in flock64_to_posix_lock()
484 if (fl->fl_start + l->l_len < 0) in flock64_to_posix_lock()
485 return -EINVAL; in flock64_to_posix_lock()
486 fl->fl_end = fl->fl_start - 1; in flock64_to_posix_lock()
487 fl->fl_start += l->l_len; in flock64_to_posix_lock()
489 fl->fl_end = OFFSET_MAX; in flock64_to_posix_lock()
491 fl->fl_owner = current->files; in flock64_to_posix_lock()
492 fl->fl_pid = current->tgid; in flock64_to_posix_lock()
493 fl->fl_file = filp; in flock64_to_posix_lock()
494 fl->fl_flags = FL_POSIX; in flock64_to_posix_lock()
495 fl->fl_ops = NULL; in flock64_to_posix_lock()
496 fl->fl_lmops = NULL; in flock64_to_posix_lock()
498 return assign_type(fl, l->l_type); in flock64_to_posix_lock()
508 .l_type = l->l_type, in flock_to_posix_lock()
509 .l_whence = l->l_whence, in flock_to_posix_lock()
510 .l_start = l->l_start, in flock_to_posix_lock()
511 .l_len = l->l_len, in flock_to_posix_lock()
521 kill_fasync(&fl->fl_fasync, SIGIO, POLL_MSG); in lease_break_callback()
528 struct file *filp = fl->fl_file; in lease_setup()
536 if (!fasync_insert_entry(fa->fa_fd, filp, &fl->fl_fasync, fa)) in lease_setup()
554 return -EINVAL; in lease_init()
556 fl->fl_owner = filp; in lease_init()
557 fl->fl_pid = current->tgid; in lease_init()
559 fl->fl_file = filp; in lease_init()
560 fl->fl_flags = FL_LEASE; in lease_init()
561 fl->fl_start = 0; in lease_init()
562 fl->fl_end = OFFSET_MAX; in lease_init()
563 fl->fl_ops = NULL; in lease_init()
564 fl->fl_lmops = &lease_manager_ops; in lease_init()
572 int error = -ENOMEM; in lease_alloc()
589 return ((fl1->fl_end >= fl2->fl_start) && in locks_overlap()
590 (fl2->fl_end >= fl1->fl_start)); in locks_overlap()
598 return fl1->fl_owner == fl2->fl_owner; in posix_same_owner()
608 spin_lock(&fll->lock); in locks_insert_global_locks()
609 fl->fl_link_cpu = smp_processor_id(); in locks_insert_global_locks()
610 hlist_add_head(&fl->fl_link, &fll->hlist); in locks_insert_global_locks()
611 spin_unlock(&fll->lock); in locks_insert_global_locks()
626 if (hlist_unhashed(&fl->fl_link)) in locks_delete_global_locks()
629 fll = per_cpu_ptr(&file_lock_list, fl->fl_link_cpu); in locks_delete_global_locks()
630 spin_lock(&fll->lock); in locks_delete_global_locks()
631 hlist_del_init(&fl->fl_link); in locks_delete_global_locks()
632 spin_unlock(&fll->lock); in locks_delete_global_locks()
638 return (unsigned long)fl->fl_owner; in posix_owner_key()
645 hash_add(blocked_hash, &waiter->fl_link, posix_owner_key(waiter)); in locks_insert_global_blocked()
652 hash_del(&waiter->fl_link); in locks_delete_global_blocked()
663 list_del_init(&waiter->fl_blocked_member); in __locks_delete_block()
668 while (!list_empty(&blocker->fl_blocked_requests)) { in __locks_wake_up_blocks()
671 waiter = list_first_entry(&blocker->fl_blocked_requests, in __locks_wake_up_blocks()
674 if (waiter->fl_lmops && waiter->fl_lmops->lm_notify) in __locks_wake_up_blocks()
675 waiter->fl_lmops->lm_notify(waiter); in __locks_wake_up_blocks()
677 wake_up(&waiter->fl_wait); in __locks_wake_up_blocks()
684 smp_store_release(&waiter->fl_blocker, NULL); in __locks_wake_up_blocks()
689 * locks_delete_block - stop waiting for a file lock
696 int status = -ENOENT; in locks_delete_block()
719 if (!smp_load_acquire(&waiter->fl_blocker) && in locks_delete_block()
720 list_empty(&waiter->fl_blocked_requests)) in locks_delete_block()
724 if (waiter->fl_blocker) in locks_delete_block()
733 smp_store_release(&waiter->fl_blocker, NULL); in locks_delete_block()
760 BUG_ON(!list_empty(&waiter->fl_blocked_member)); in __locks_insert_block()
763 list_for_each_entry(fl, &blocker->fl_blocked_requests, fl_blocked_member) in __locks_insert_block()
768 waiter->fl_blocker = blocker; in __locks_insert_block()
769 list_add_tail(&waiter->fl_blocked_member, &blocker->fl_blocked_requests); in __locks_insert_block()
773 /* The requests in waiter->fl_blocked are known to conflict with in __locks_insert_block()
794 * Must be called with the inode->flc_lock held!
805 if (list_empty(&blocker->fl_blocked_requests)) in locks_wake_up_blocks()
816 list_add_tail(&fl->fl_list, before); in locks_insert_lock_ctx()
824 list_del_init(&fl->fl_list); in locks_unlink_lock_ctx()
833 list_add(&fl->fl_list, dispose); in locks_delete_lock_ctx()
844 if (sys_fl->fl_type == F_WRLCK) in locks_conflict()
846 if (caller_fl->fl_type == F_WRLCK) in locks_conflict()
879 if (caller_fl->fl_file == sys_fl->fl_file) in flock_locks_conflict()
894 ctx = smp_load_acquire(&inode->i_flctx); in posix_test_lock()
895 if (!ctx || list_empty_careful(&ctx->flc_posix)) { in posix_test_lock()
896 fl->fl_type = F_UNLCK; in posix_test_lock()
901 spin_lock(&ctx->flc_lock); in posix_test_lock()
902 list_for_each_entry(cfl, &ctx->flc_posix, fl_list) { in posix_test_lock()
905 if (cfl->fl_lmops && cfl->fl_lmops->lm_lock_expirable in posix_test_lock()
906 && (*cfl->fl_lmops->lm_lock_expirable)(cfl)) { in posix_test_lock()
907 owner = cfl->fl_lmops->lm_mod_owner; in posix_test_lock()
908 func = cfl->fl_lmops->lm_expire_lock; in posix_test_lock()
910 spin_unlock(&ctx->flc_lock); in posix_test_lock()
918 fl->fl_type = F_UNLCK; in posix_test_lock()
920 spin_unlock(&ctx->flc_lock); in posix_test_lock()
967 while (fl->fl_blocker) in what_owner_is_waiting_for()
968 fl = fl->fl_blocker; in what_owner_is_waiting_for()
985 * FL_OFDLCK locks, since they aren't owned by a process, per-se. in posix_locks_deadlock()
1004 * value for -ENOENT.
1015 ctx = locks_get_lock_context(inode, request->fl_type); in flock_lock_inode()
1017 if (request->fl_type != F_UNLCK) in flock_lock_inode()
1018 return -ENOMEM; in flock_lock_inode()
1019 return (request->fl_flags & FL_EXISTS) ? -ENOENT : 0; in flock_lock_inode()
1022 if (!(request->fl_flags & FL_ACCESS) && (request->fl_type != F_UNLCK)) { in flock_lock_inode()
1025 return -ENOMEM; in flock_lock_inode()
1029 spin_lock(&ctx->flc_lock); in flock_lock_inode()
1030 if (request->fl_flags & FL_ACCESS) in flock_lock_inode()
1033 list_for_each_entry(fl, &ctx->flc_flock, fl_list) { in flock_lock_inode()
1034 if (request->fl_file != fl->fl_file) in flock_lock_inode()
1036 if (request->fl_type == fl->fl_type) in flock_lock_inode()
1043 if (request->fl_type == F_UNLCK) { in flock_lock_inode()
1044 if ((request->fl_flags & FL_EXISTS) && !found) in flock_lock_inode()
1045 error = -ENOENT; in flock_lock_inode()
1050 list_for_each_entry(fl, &ctx->flc_flock, fl_list) { in flock_lock_inode()
1053 error = -EAGAIN; in flock_lock_inode()
1054 if (!(request->fl_flags & FL_SLEEP)) in flock_lock_inode()
1060 if (request->fl_flags & FL_ACCESS) in flock_lock_inode()
1064 locks_insert_lock_ctx(new_fl, &ctx->flc_flock); in flock_lock_inode()
1069 spin_unlock(&ctx->flc_lock); in flock_lock_inode()
1093 ctx = locks_get_lock_context(inode, request->fl_type); in posix_lock_inode()
1095 return (request->fl_type == F_UNLCK) ? 0 : -ENOMEM; in posix_lock_inode()
1103 if (!(request->fl_flags & FL_ACCESS) && in posix_lock_inode()
1104 (request->fl_type != F_UNLCK || in posix_lock_inode()
1105 request->fl_start != 0 || request->fl_end != OFFSET_MAX)) { in posix_lock_inode()
1112 spin_lock(&ctx->flc_lock); in posix_lock_inode()
1118 if (request->fl_type != F_UNLCK) { in posix_lock_inode()
1119 list_for_each_entry(fl, &ctx->flc_posix, fl_list) { in posix_lock_inode()
1122 if (fl->fl_lmops && fl->fl_lmops->lm_lock_expirable in posix_lock_inode()
1123 && (*fl->fl_lmops->lm_lock_expirable)(fl)) { in posix_lock_inode()
1124 owner = fl->fl_lmops->lm_mod_owner; in posix_lock_inode()
1125 func = fl->fl_lmops->lm_expire_lock; in posix_lock_inode()
1127 spin_unlock(&ctx->flc_lock); in posix_lock_inode()
1135 error = -EAGAIN; in posix_lock_inode()
1136 if (!(request->fl_flags & FL_SLEEP)) in posix_lock_inode()
1142 error = -EDEADLK; in posix_lock_inode()
1161 if (request->fl_flags & FL_ACCESS) in posix_lock_inode()
1165 list_for_each_entry(fl, &ctx->flc_posix, fl_list) { in posix_lock_inode()
1171 list_for_each_entry_safe_from(fl, tmp, &ctx->flc_posix, fl_list) { in posix_lock_inode()
1176 if (request->fl_type == fl->fl_type) { in posix_lock_inode()
1178 * "start - 1" rather than "end + 1". If end in posix_lock_inode()
1181 if (fl->fl_end < request->fl_start - 1) in posix_lock_inode()
1186 if (fl->fl_start - 1 > request->fl_end) in posix_lock_inode()
1194 if (fl->fl_start > request->fl_start) in posix_lock_inode()
1195 fl->fl_start = request->fl_start; in posix_lock_inode()
1197 request->fl_start = fl->fl_start; in posix_lock_inode()
1198 if (fl->fl_end < request->fl_end) in posix_lock_inode()
1199 fl->fl_end = request->fl_end; in posix_lock_inode()
1201 request->fl_end = fl->fl_end; in posix_lock_inode()
1212 if (fl->fl_end < request->fl_start) in posix_lock_inode()
1214 if (fl->fl_start > request->fl_end) in posix_lock_inode()
1216 if (request->fl_type == F_UNLCK) in posix_lock_inode()
1218 if (fl->fl_start < request->fl_start) in posix_lock_inode()
1223 if (fl->fl_end > request->fl_end) { in posix_lock_inode()
1227 if (fl->fl_start >= request->fl_start) { in posix_lock_inode()
1242 error = -ENOLCK; in posix_lock_inode()
1249 locks_insert_lock_ctx(request, &fl->fl_list); in posix_lock_inode()
1261 error = -ENOLCK; /* "no luck" */ in posix_lock_inode()
1267 if (request->fl_type == F_UNLCK) { in posix_lock_inode()
1268 if (request->fl_flags & FL_EXISTS) in posix_lock_inode()
1269 error = -ENOENT; in posix_lock_inode()
1274 error = -ENOLCK; in posix_lock_inode()
1279 locks_insert_lock_ctx(new_fl, &fl->fl_list); in posix_lock_inode()
1291 locks_insert_lock_ctx(left, &fl->fl_list); in posix_lock_inode()
1293 right->fl_start = request->fl_end + 1; in posix_lock_inode()
1297 left->fl_end = request->fl_start - 1; in posix_lock_inode()
1301 spin_unlock(&ctx->flc_lock); in posix_lock_inode()
1317 * posix_lock_file - Apply a POSIX-style lock to a file
1328 * value for -ENOENT.
1338 * posix_lock_inode_wait - Apply a POSIX-style lock to a file
1352 error = wait_event_interruptible(fl->fl_wait, in posix_lock_inode_wait()
1353 list_empty(&fl->fl_blocked_member)); in posix_lock_inode_wait()
1365 fl->fl_flags &= ~FL_UNLOCK_PENDING; in lease_clear_pending()
1368 fl->fl_flags &= ~FL_DOWNGRADE_PENDING; in lease_clear_pending()
1382 struct file *filp = fl->fl_file; in lease_modify()
1385 filp->f_owner.signum = 0; in lease_modify()
1386 fasync_helper(0, fl->fl_file, 0, &fl->fl_fasync); in lease_modify()
1387 if (fl->fl_fasync != NULL) { in lease_modify()
1388 printk(KERN_ERR "locks_delete_lock: fasync == %p\n", fl->fl_fasync); in lease_modify()
1389 fl->fl_fasync = NULL; in lease_modify()
1407 struct file_lock_context *ctx = inode->i_flctx; in time_out_leases()
1410 lockdep_assert_held(&ctx->flc_lock); in time_out_leases()
1412 list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list) { in time_out_leases()
1414 if (past_time(fl->fl_downgrade_time)) in time_out_leases()
1416 if (past_time(fl->fl_break_time)) in time_out_leases()
1425 if (lease->fl_lmops->lm_breaker_owns_lease in leases_conflict()
1426 && lease->fl_lmops->lm_breaker_owns_lease(lease)) in leases_conflict()
1428 if ((breaker->fl_flags & FL_LAYOUT) != (lease->fl_flags & FL_LAYOUT)) { in leases_conflict()
1432 if ((breaker->fl_flags & FL_DELEG) && (lease->fl_flags & FL_LEASE)) { in leases_conflict()
1446 struct file_lock_context *ctx = inode->i_flctx; in any_leases_conflict()
1449 lockdep_assert_held(&ctx->flc_lock); in any_leases_conflict()
1451 list_for_each_entry(fl, &ctx->flc_lease, fl_list) { in any_leases_conflict()
1459 * __break_lease - revoke all outstanding leases on file
1483 new_fl->fl_flags = type; in __break_lease()
1485 /* typically we will check that ctx is non-NULL before calling */ in __break_lease()
1486 ctx = smp_load_acquire(&inode->i_flctx); in __break_lease()
1493 spin_lock(&ctx->flc_lock); in __break_lease()
1507 list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list) { in __break_lease()
1511 if (fl->fl_flags & FL_UNLOCK_PENDING) in __break_lease()
1513 fl->fl_flags |= FL_UNLOCK_PENDING; in __break_lease()
1514 fl->fl_break_time = break_time; in __break_lease()
1518 fl->fl_flags |= FL_DOWNGRADE_PENDING; in __break_lease()
1519 fl->fl_downgrade_time = break_time; in __break_lease()
1521 if (fl->fl_lmops->lm_break(fl)) in __break_lease()
1525 if (list_empty(&ctx->flc_lease)) in __break_lease()
1530 error = -EWOULDBLOCK; in __break_lease()
1535 fl = list_first_entry(&ctx->flc_lease, struct file_lock, fl_list); in __break_lease()
1536 break_time = fl->fl_break_time; in __break_lease()
1538 break_time -= jiffies; in __break_lease()
1543 spin_unlock(&ctx->flc_lock); in __break_lease()
1547 error = wait_event_interruptible_timeout(new_fl->fl_wait, in __break_lease()
1548 list_empty(&new_fl->fl_blocked_member), in __break_lease()
1552 spin_lock(&ctx->flc_lock); in __break_lease()
1567 spin_unlock(&ctx->flc_lock); in __break_lease()
1577 * lease_get_mtime - update modified time of an inode with exclusive lease
1582 * exclusive leases. The justification is that if someone has an
1591 ctx = smp_load_acquire(&inode->i_flctx); in lease_get_mtime()
1592 if (ctx && !list_empty_careful(&ctx->flc_lease)) { in lease_get_mtime()
1593 spin_lock(&ctx->flc_lock); in lease_get_mtime()
1594 fl = list_first_entry_or_null(&ctx->flc_lease, in lease_get_mtime()
1596 if (fl && (fl->fl_type == F_WRLCK)) in lease_get_mtime()
1598 spin_unlock(&ctx->flc_lock); in lease_get_mtime()
1607 * fcntl_getlease - Enquire what lease is currently active
1637 ctx = smp_load_acquire(&inode->i_flctx); in fcntl_getlease()
1638 if (ctx && !list_empty_careful(&ctx->flc_lease)) { in fcntl_getlease()
1640 spin_lock(&ctx->flc_lock); in fcntl_getlease()
1642 list_for_each_entry(fl, &ctx->flc_lease, fl_list) { in fcntl_getlease()
1643 if (fl->fl_file != filp) in fcntl_getlease()
1648 spin_unlock(&ctx->flc_lock); in fcntl_getlease()
1657 * check_conflicting_open - see if the given file points to an inode that has
1680 return inode_is_open_for_write(inode) ? -EAGAIN : 0; in check_conflicting_open()
1690 if (filp->f_mode & FMODE_WRITE) in check_conflicting_open()
1692 else if (filp->f_mode & FMODE_READ) in check_conflicting_open()
1695 if (atomic_read(&inode->i_writecount) != self_wcount || in check_conflicting_open()
1696 atomic_read(&inode->i_readcount) != self_rcount) in check_conflicting_open()
1697 return -EAGAIN; in check_conflicting_open()
1708 bool is_deleg = (*flp)->fl_flags & FL_DELEG; in generic_add_lease()
1718 return -ENOMEM; in generic_add_lease()
1724 * there's some chance of a conflict--we'd rather not in generic_add_lease()
1729 return -EAGAIN; in generic_add_lease()
1735 return -EINVAL; in generic_add_lease()
1739 spin_lock(&ctx->flc_lock); in generic_add_lease()
1741 error = check_conflicting_open(filp, arg, lease->fl_flags); in generic_add_lease()
1753 error = -EAGAIN; in generic_add_lease()
1754 list_for_each_entry(fl, &ctx->flc_lease, fl_list) { in generic_add_lease()
1755 if (fl->fl_file == filp && in generic_add_lease()
1756 fl->fl_owner == lease->fl_owner) { in generic_add_lease()
1771 if (fl->fl_flags & FL_UNLOCK_PENDING) in generic_add_lease()
1777 error = lease->fl_lmops->lm_change(lease, arg, &dispose); in generic_add_lease()
1783 error = -EINVAL; in generic_add_lease()
1787 locks_insert_lock_ctx(lease, &ctx->flc_lease); in generic_add_lease()
1798 error = check_conflicting_open(filp, arg, lease->fl_flags); in generic_add_lease()
1805 if (lease->fl_lmops->lm_setup) in generic_add_lease()
1806 lease->fl_lmops->lm_setup(lease, priv); in generic_add_lease()
1808 spin_unlock(&ctx->flc_lock); in generic_add_lease()
1820 int error = -EAGAIN; in generic_delete_lease()
1826 ctx = smp_load_acquire(&inode->i_flctx); in generic_delete_lease()
1833 spin_lock(&ctx->flc_lock); in generic_delete_lease()
1834 list_for_each_entry(fl, &ctx->flc_lease, fl_list) { in generic_delete_lease()
1835 if (fl->fl_file == filp && in generic_delete_lease()
1836 fl->fl_owner == owner) { in generic_delete_lease()
1843 error = fl->fl_lmops->lm_change(victim, F_UNLCK, &dispose); in generic_delete_lease()
1844 spin_unlock(&ctx->flc_lock); in generic_delete_lease()
1851 * generic_setlease - sets a lease on an open file
1854 * @flp: input - file_lock to use, output - file_lock inserted
1858 * The (input) flp->fl_lmops->lm_break function is required
1867 if ((!uid_eq(current_fsuid(), inode->i_uid)) && !capable(CAP_LEASE)) in generic_setlease()
1868 return -EACCES; in generic_setlease()
1869 if (!S_ISREG(inode->i_mode)) in generic_setlease()
1870 return -EINVAL; in generic_setlease()
1880 if (!(*flp)->fl_lmops->lm_break) { in generic_setlease()
1882 return -ENOLCK; in generic_setlease()
1887 return -EINVAL; in generic_setlease()
1951 * vfs_setlease - sets a lease on an open file
1960 * an existing lease, the ``(*lease)->fl_lmops->lm_break`` operation must be
1961 * set; if not, this function will return -ENOLCK (and generate a scary-looking
1964 * The "priv" pointer is passed directly to the lm_setup function as-is. It
1972 if (filp->f_op->setlease) in vfs_setlease()
1973 return filp->f_op->setlease(filp, arg, lease, priv); in vfs_setlease()
1992 return -ENOMEM; in do_fcntl_add_lease()
1994 new->fa_fd = fd; in do_fcntl_add_lease()
2005 * fcntl_setlease - sets a lease on an open file
2022 * flock_lock_inode_wait - Apply a FLOCK-style lock to a file
2036 error = wait_event_interruptible(fl->fl_wait, in flock_lock_inode_wait()
2037 list_empty(&fl->fl_blocked_member)); in flock_lock_inode_wait()
2046 * locks_lock_inode_wait - Apply a lock to an inode
2055 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) { in locks_lock_inode_wait()
2070 * sys_flock: - flock() system call.
2077 * - %LOCK_SH -- a shared lock.
2078 * - %LOCK_EX -- an exclusive lock.
2079 * - %LOCK_UN -- remove an existing lock.
2080 * - %LOCK_MAND -- a 'mandatory' flock. (DEPRECATED)
2107 error = -EBADF; in SYSCALL_DEFINE2()
2112 if (type != F_UNLCK && !(f.file->f_mode & (FMODE_READ | FMODE_WRITE))) in SYSCALL_DEFINE2()
2125 if (f.file->f_op->flock) in SYSCALL_DEFINE2()
2126 error = f.file->f_op->flock(f.file, in SYSCALL_DEFINE2()
2140 * vfs_test_lock - test file byte range lock
2144 * Returns -ERRNO on failure. Indicates presence of conflicting lock by
2145 * setting conf->fl_type to something other than F_UNLCK.
2149 if (filp->f_op->lock) in vfs_test_lock()
2150 return filp->f_op->lock(filp, F_GETLK, fl); in vfs_test_lock()
2157 * locks_translate_pid - translate a file_lock's fl_pid number into a namespace
2169 return -1; in locks_translate_pid()
2171 return fl->fl_pid; in locks_translate_pid()
2178 return (pid_t)fl->fl_pid; in locks_translate_pid()
2181 pid = find_pid_ns(fl->fl_pid, &init_pid_ns); in locks_translate_pid()
2189 flock->l_pid = locks_translate_pid(fl, task_active_pid_ns(current)); in posix_lock_to_flock()
2195 if (fl->fl_start > OFFT_OFFSET_MAX) in posix_lock_to_flock()
2196 return -EOVERFLOW; in posix_lock_to_flock()
2197 if (fl->fl_end != OFFSET_MAX && fl->fl_end > OFFT_OFFSET_MAX) in posix_lock_to_flock()
2198 return -EOVERFLOW; in posix_lock_to_flock()
2200 flock->l_start = fl->fl_start; in posix_lock_to_flock()
2201 flock->l_len = fl->fl_end == OFFSET_MAX ? 0 : in posix_lock_to_flock()
2202 fl->fl_end - fl->fl_start + 1; in posix_lock_to_flock()
2203 flock->l_whence = 0; in posix_lock_to_flock()
2204 flock->l_type = fl->fl_type; in posix_lock_to_flock()
2211 flock->l_pid = locks_translate_pid(fl, task_active_pid_ns(current)); in posix_lock_to_flock64()
2212 flock->l_start = fl->fl_start; in posix_lock_to_flock64()
2213 flock->l_len = fl->fl_end == OFFSET_MAX ? 0 : in posix_lock_to_flock64()
2214 fl->fl_end - fl->fl_start + 1; in posix_lock_to_flock64()
2215 flock->l_whence = 0; in posix_lock_to_flock64()
2216 flock->l_type = fl->fl_type; in posix_lock_to_flock64()
2230 return -ENOMEM; in fcntl_getlk()
2231 error = -EINVAL; in fcntl_getlk()
2232 if (flock->l_type != F_RDLCK && flock->l_type != F_WRLCK) in fcntl_getlk()
2240 error = -EINVAL; in fcntl_getlk()
2241 if (flock->l_pid != 0) in fcntl_getlk()
2244 fl->fl_flags |= FL_OFDLCK; in fcntl_getlk()
2245 fl->fl_owner = filp; in fcntl_getlk()
2252 flock->l_type = fl->fl_type; in fcntl_getlk()
2253 if (fl->fl_type != F_UNLCK) { in fcntl_getlk()
2264 * vfs_lock_file - file byte range lock
2273 * If the filesystem defines a private ->lock() method, then @conf will
2278 * locks, the ->lock() interface may return asynchronously, before the lock has
2280 * lm_grant is set. Callers expecting ->lock() to return asynchronously
2282 * the request is for a blocking lock. When ->lock() does return asynchronously,
2283 * it must return FILE_LOCK_DEFERRED, and call ->lm_grant() when the lock
2285 * If the request is for non-blocking lock the file system should return
2293 * ->lm_grant() before returning to the caller with a FILE_LOCK_DEFERRED
2298 if (filp->f_op->lock) in vfs_lock_file()
2299 return filp->f_op->lock(filp, cmd, fl); in vfs_lock_file()
2310 error = security_file_lock(filp, fl->fl_type); in do_lock_file_wait()
2318 error = wait_event_interruptible(fl->fl_wait, in do_lock_file_wait()
2319 list_empty(&fl->fl_blocked_member)); in do_lock_file_wait()
2328 /* Ensure that fl->fl_file has compatible f_mode for F_SETLK calls */
2332 switch (fl->fl_type) { in check_fmode_for_setlk()
2334 if (!(fl->fl_file->f_mode & FMODE_READ)) in check_fmode_for_setlk()
2335 return -EBADF; in check_fmode_for_setlk()
2338 if (!(fl->fl_file->f_mode & FMODE_WRITE)) in check_fmode_for_setlk()
2339 return -EBADF; in check_fmode_for_setlk()
2356 return -ENOLCK; in fcntl_setlk()
2367 * If the cmd is requesting file-private locks, then set the in fcntl_setlk()
2372 error = -EINVAL; in fcntl_setlk()
2373 if (flock->l_pid != 0) in fcntl_setlk()
2377 file_lock->fl_flags |= FL_OFDLCK; in fcntl_setlk()
2378 file_lock->fl_owner = filp; in fcntl_setlk()
2381 error = -EINVAL; in fcntl_setlk()
2382 if (flock->l_pid != 0) in fcntl_setlk()
2386 file_lock->fl_flags |= FL_OFDLCK; in fcntl_setlk()
2387 file_lock->fl_owner = filp; in fcntl_setlk()
2390 file_lock->fl_flags |= FL_SLEEP; in fcntl_setlk()
2400 if (!error && file_lock->fl_type != F_UNLCK && in fcntl_setlk()
2401 !(file_lock->fl_flags & FL_OFDLCK)) { in fcntl_setlk()
2402 struct files_struct *files = current->files; in fcntl_setlk()
2404 * We need that spin_lock here - it prevents reordering between in fcntl_setlk()
2405 * update of i_flctx->flc_posix and check for it done in in fcntl_setlk()
2408 spin_lock(&files->file_lock); in fcntl_setlk()
2410 spin_unlock(&files->file_lock); in fcntl_setlk()
2412 file_lock->fl_type = F_UNLCK; in fcntl_setlk()
2415 error = -EBADF; in fcntl_setlk()
2435 return -ENOMEM; in fcntl_getlk64()
2437 error = -EINVAL; in fcntl_getlk64()
2438 if (flock->l_type != F_RDLCK && flock->l_type != F_WRLCK) in fcntl_getlk64()
2446 error = -EINVAL; in fcntl_getlk64()
2447 if (flock->l_pid != 0) in fcntl_getlk64()
2451 fl->fl_flags |= FL_OFDLCK; in fcntl_getlk64()
2452 fl->fl_owner = filp; in fcntl_getlk64()
2459 flock->l_type = fl->fl_type; in fcntl_getlk64()
2460 if (fl->fl_type != F_UNLCK) in fcntl_getlk64()
2479 return -ENOLCK; in fcntl_setlk64()
2490 * If the cmd is requesting file-private locks, then set the in fcntl_setlk64()
2495 error = -EINVAL; in fcntl_setlk64()
2496 if (flock->l_pid != 0) in fcntl_setlk64()
2500 file_lock->fl_flags |= FL_OFDLCK; in fcntl_setlk64()
2501 file_lock->fl_owner = filp; in fcntl_setlk64()
2504 error = -EINVAL; in fcntl_setlk64()
2505 if (flock->l_pid != 0) in fcntl_setlk64()
2509 file_lock->fl_flags |= FL_OFDLCK; in fcntl_setlk64()
2510 file_lock->fl_owner = filp; in fcntl_setlk64()
2513 file_lock->fl_flags |= FL_SLEEP; in fcntl_setlk64()
2523 if (!error && file_lock->fl_type != F_UNLCK && in fcntl_setlk64()
2524 !(file_lock->fl_flags & FL_OFDLCK)) { in fcntl_setlk64()
2525 struct files_struct *files = current->files; in fcntl_setlk64()
2527 * We need that spin_lock here - it prevents reordering between in fcntl_setlk64()
2528 * update of i_flctx->flc_posix and check for it done in in fcntl_setlk64()
2531 spin_lock(&files->file_lock); in fcntl_setlk64()
2533 spin_unlock(&files->file_lock); in fcntl_setlk64()
2535 file_lock->fl_type = F_UNLCK; in fcntl_setlk64()
2538 error = -EBADF; in fcntl_setlk64()
2564 ctx = smp_load_acquire(&inode->i_flctx); in locks_remove_posix()
2565 if (!ctx || list_empty(&ctx->flc_posix)) in locks_remove_posix()
2574 lock.fl_pid = current->tgid; in locks_remove_posix()
2581 if (lock.fl_ops && lock.fl_ops->fl_release_private) in locks_remove_posix()
2582 lock.fl_ops->fl_release_private(&lock); in locks_remove_posix()
2594 if (list_empty(&flctx->flc_flock)) in locks_remove_flock()
2600 if (filp->f_op->flock) in locks_remove_flock()
2601 filp->f_op->flock(filp, F_SETLKW, &fl); in locks_remove_flock()
2605 if (fl.fl_ops && fl.fl_ops->fl_release_private) in locks_remove_flock()
2606 fl.fl_ops->fl_release_private(&fl); in locks_remove_flock()
2616 if (list_empty(&ctx->flc_lease)) in locks_remove_lease()
2620 spin_lock(&ctx->flc_lock); in locks_remove_lease()
2621 list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list) in locks_remove_lease()
2622 if (filp == fl->fl_file) in locks_remove_lease()
2624 spin_unlock(&ctx->flc_lock); in locks_remove_lease()
2637 ctx = smp_load_acquire(&locks_inode(filp)->i_flctx); in locks_remove_file()
2650 spin_lock(&ctx->flc_lock); in locks_remove_file()
2651 locks_check_ctx_file_list(filp, &ctx->flc_posix, "POSIX"); in locks_remove_file()
2652 locks_check_ctx_file_list(filp, &ctx->flc_flock, "FLOCK"); in locks_remove_file()
2653 locks_check_ctx_file_list(filp, &ctx->flc_lease, "LEASE"); in locks_remove_file()
2654 spin_unlock(&ctx->flc_lock); in locks_remove_file()
2658 * vfs_cancel_lock - file byte range unblock lock
2666 if (filp->f_op->lock) in vfs_cancel_lock()
2667 return filp->f_op->lock(filp, F_CANCELLK, fl); in vfs_cancel_lock()
2686 struct pid_namespace *proc_pidns = proc_pid_ns(file_inode(f->file)->i_sb); in lock_get_status()
2696 if (fl->fl_file != NULL) in lock_get_status()
2697 inode = locks_inode(fl->fl_file); in lock_get_status()
2702 seq_printf(f, "%*s", repeat - 1 + (int)strlen(pfx), pfx); in lock_get_status()
2705 if (fl->fl_flags & FL_ACCESS) in lock_get_status()
2717 if (fl->fl_flags & FL_DELEG) in lock_get_status()
2724 else if (fl->fl_file) in lock_get_status()
2731 type = IS_LEASE(fl) ? target_leasetype(fl) : fl->fl_type; in lock_get_status()
2738 MAJOR(inode->i_sb->s_dev), in lock_get_status()
2739 MINOR(inode->i_sb->s_dev), inode->i_ino); in lock_get_status()
2744 if (fl->fl_end == OFFSET_MAX) in lock_get_status()
2745 seq_printf(f, "%Ld EOF\n", fl->fl_start); in lock_get_status()
2747 seq_printf(f, "%Ld %Ld\n", fl->fl_start, fl->fl_end); in lock_get_status()
2758 if (node == NULL || node->fl_blocker == NULL) in get_next_blocked_member()
2763 if (list_entry_is_head(tmp, &node->fl_blocker->fl_blocked_requests, fl_blocked_member) in get_next_blocked_member()
2773 struct locks_iterator *iter = f->private; in locks_show()
2775 struct pid_namespace *proc_pidns = proc_pid_ns(file_inode(f->file)->i_sb); in locks_show()
2790 lock_get_status(f, cur, iter->li_pos, "-> ", level); in locks_show()
2792 lock_get_status(f, cur, iter->li_pos, "", level); in locks_show()
2794 if (!list_empty(&cur->fl_blocked_requests)) { in locks_show()
2796 cur = list_first_entry_or_null(&cur->fl_blocked_requests, in locks_show()
2803 while (tmp == NULL && cur->fl_blocker != NULL) { in locks_show()
2804 cur = cur->fl_blocker; in locks_show()
2805 level--; in locks_show()
2823 if (filp != fl->fl_file) in __show_fd_locks()
2825 if (fl->fl_owner != files && in __show_fd_locks()
2826 fl->fl_owner != filp) in __show_fd_locks()
2842 ctx = smp_load_acquire(&inode->i_flctx); in show_fd_locks()
2846 spin_lock(&ctx->flc_lock); in show_fd_locks()
2847 __show_fd_locks(f, &ctx->flc_flock, &id, filp, files); in show_fd_locks()
2848 __show_fd_locks(f, &ctx->flc_posix, &id, filp, files); in show_fd_locks()
2849 __show_fd_locks(f, &ctx->flc_lease, &id, filp, files); in show_fd_locks()
2850 spin_unlock(&ctx->flc_lock); in show_fd_locks()
2856 struct locks_iterator *iter = f->private; in locks_start()
2858 iter->li_pos = *pos + 1; in locks_start()
2861 return seq_hlist_start_percpu(&file_lock_list.hlist, &iter->li_cpu, *pos); in locks_start()
2866 struct locks_iterator *iter = f->private; in locks_next()
2868 ++iter->li_pos; in locks_next()
2869 return seq_hlist_next_percpu(v, &file_lock_list.hlist, &iter->li_cpu, pos); in locks_next()
2908 spin_lock_init(&fll->lock); in filelock_init()
2909 INIT_HLIST_HEAD(&fll->hlist); in filelock_init()