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
73 #define IS_POSIX(fl) (fl->fl_flags & FL_POSIX)
74 #define IS_FLOCK(fl) (fl->fl_flags & FL_FLOCK)
75 #define IS_LEASE(fl) (fl->fl_flags & (FL_LEASE|FL_DELEG|FL_LAYOUT))
76 #define IS_OFDLCK(fl) (fl->fl_flags & FL_OFDLCK)
77 #define IS_REMOTELCK(fl) (fl->fl_pid <= 0)
81 return fl->fl_flags & (FL_UNLOCK_PENDING | FL_DOWNGRADE_PENDING); in lease_breaking()
86 if (fl->fl_flags & FL_UNLOCK_PENDING) in target_leasetype()
88 if (fl->fl_flags & FL_DOWNGRADE_PENDING) in target_leasetype()
90 return fl->fl_type; in target_leasetype()
99 .procname = "leases-enable",
107 .procname = "lease-break-time",
159 * In addition, it also protects the fl->fl_blocked_requests list, and the
160 * fl->fl_blocker pointer for file_lock structures that are acting as lock
187 spin_lock_init(&ctx->flc_lock); in locks_get_lock_context()
188 INIT_LIST_HEAD(&ctx->flc_flock); in locks_get_lock_context()
189 INIT_LIST_HEAD(&ctx->flc_posix); in locks_get_lock_context()
190 INIT_LIST_HEAD(&ctx->flc_lease); in locks_get_lock_context()
196 if (cmpxchg(&inode->i_flctx, NULL, ctx)) { in locks_get_lock_context()
211 … 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()
218 struct file_lock_context *ctx = inode->i_flctx; in locks_check_ctx_lists()
220 if (unlikely(!list_empty(&ctx->flc_flock) || in locks_check_ctx_lists()
221 !list_empty(&ctx->flc_posix) || in locks_check_ctx_lists()
222 !list_empty(&ctx->flc_lease))) { in locks_check_ctx_lists()
224 MAJOR(inode->i_sb->s_dev), MINOR(inode->i_sb->s_dev), in locks_check_ctx_lists()
225 inode->i_ino); in locks_check_ctx_lists()
226 locks_dump_ctx_list(&ctx->flc_flock, "FLOCK"); in locks_check_ctx_lists()
227 locks_dump_ctx_list(&ctx->flc_posix, "POSIX"); in locks_check_ctx_lists()
228 locks_dump_ctx_list(&ctx->flc_lease, "LEASE"); in locks_check_ctx_lists()
240 if (fl->fl_file == filp) in locks_check_ctx_file_list()
243 list_type, MAJOR(inode->i_sb->s_dev), in locks_check_ctx_file_list()
244 MINOR(inode->i_sb->s_dev), inode->i_ino, in locks_check_ctx_file_list()
245 fl->fl_owner, fl->fl_flags, fl->fl_type, fl->fl_pid); in locks_check_ctx_file_list()
261 INIT_HLIST_NODE(&fl->fl_link); in locks_init_lock_heads()
262 INIT_LIST_HEAD(&fl->fl_list); in locks_init_lock_heads()
263 INIT_LIST_HEAD(&fl->fl_blocked_requests); in locks_init_lock_heads()
264 INIT_LIST_HEAD(&fl->fl_blocked_member); in locks_init_lock_heads()
265 init_waitqueue_head(&fl->fl_wait); in locks_init_lock_heads()
282 BUG_ON(waitqueue_active(&fl->fl_wait)); in locks_release_private()
283 BUG_ON(!list_empty(&fl->fl_list)); in locks_release_private()
284 BUG_ON(!list_empty(&fl->fl_blocked_requests)); in locks_release_private()
285 BUG_ON(!list_empty(&fl->fl_blocked_member)); in locks_release_private()
286 BUG_ON(!hlist_unhashed(&fl->fl_link)); in locks_release_private()
288 if (fl->fl_ops) { in locks_release_private()
289 if (fl->fl_ops->fl_release_private) in locks_release_private()
290 fl->fl_ops->fl_release_private(fl); in locks_release_private()
291 fl->fl_ops = NULL; in locks_release_private()
294 if (fl->fl_lmops) { in locks_release_private()
295 if (fl->fl_lmops->lm_put_owner) { in locks_release_private()
296 fl->fl_lmops->lm_put_owner(fl->fl_owner); in locks_release_private()
297 fl->fl_owner = NULL; in locks_release_private()
299 fl->fl_lmops = NULL; in locks_release_private()
305 * locks_owner_has_blockers - Check for blocking lock requests
318 spin_lock(&flctx->flc_lock); in locks_owner_has_blockers()
319 list_for_each_entry(fl, &flctx->flc_posix, fl_list) { in locks_owner_has_blockers()
320 if (fl->fl_owner != owner) in locks_owner_has_blockers()
322 if (!list_empty(&fl->fl_blocked_requests)) { in locks_owner_has_blockers()
323 spin_unlock(&flctx->flc_lock); in locks_owner_has_blockers()
327 spin_unlock(&flctx->flc_lock); in locks_owner_has_blockers()
347 list_del_init(&fl->fl_list); in locks_dispose_list()
364 new->fl_owner = fl->fl_owner; in locks_copy_conflock()
365 new->fl_pid = fl->fl_pid; in locks_copy_conflock()
366 new->fl_file = NULL; in locks_copy_conflock()
367 new->fl_flags = fl->fl_flags; in locks_copy_conflock()
368 new->fl_type = fl->fl_type; in locks_copy_conflock()
369 new->fl_start = fl->fl_start; in locks_copy_conflock()
370 new->fl_end = fl->fl_end; in locks_copy_conflock()
371 new->fl_lmops = fl->fl_lmops; in locks_copy_conflock()
372 new->fl_ops = NULL; in locks_copy_conflock()
374 if (fl->fl_lmops) { in locks_copy_conflock()
375 if (fl->fl_lmops->lm_get_owner) in locks_copy_conflock()
376 fl->fl_lmops->lm_get_owner(fl->fl_owner); in locks_copy_conflock()
383 /* "new" must be a freshly-initialized lock */ in locks_copy_lock()
384 WARN_ON_ONCE(new->fl_ops); in locks_copy_lock()
388 new->fl_file = fl->fl_file; in locks_copy_lock()
389 new->fl_ops = fl->fl_ops; in locks_copy_lock()
391 if (fl->fl_ops) { in locks_copy_lock()
392 if (fl->fl_ops->fl_copy_lock) in locks_copy_lock()
393 fl->fl_ops->fl_copy_lock(new, fl); in locks_copy_lock()
403 * As ctx->flc_lock is held, new requests cannot be added to in locks_move_blocks()
404 * ->fl_blocked_requests, so we don't need a lock to check if it in locks_move_blocks()
407 if (list_empty(&fl->fl_blocked_requests)) in locks_move_blocks()
410 list_splice_init(&fl->fl_blocked_requests, &new->fl_blocked_requests); in locks_move_blocks()
411 list_for_each_entry(f, &new->fl_blocked_requests, fl_blocked_member) in locks_move_blocks()
412 f->fl_blocker = new; in locks_move_blocks()
425 return -EINVAL; in flock_translate_cmd()
433 fl->fl_file = filp; in flock_make_lock()
434 fl->fl_owner = filp; in flock_make_lock()
435 fl->fl_pid = current->tgid; in flock_make_lock()
436 fl->fl_flags = FL_FLOCK; in flock_make_lock()
437 fl->fl_type = type; in flock_make_lock()
438 fl->fl_end = OFFSET_MAX; in flock_make_lock()
447 fl->fl_type = type; in assign_type()
450 return -EINVAL; in assign_type()
458 switch (l->l_whence) { in flock64_to_posix_lock()
460 fl->fl_start = 0; in flock64_to_posix_lock()
463 fl->fl_start = filp->f_pos; in flock64_to_posix_lock()
466 fl->fl_start = i_size_read(file_inode(filp)); in flock64_to_posix_lock()
469 return -EINVAL; in flock64_to_posix_lock()
471 if (l->l_start > OFFSET_MAX - fl->fl_start) in flock64_to_posix_lock()
472 return -EOVERFLOW; in flock64_to_posix_lock()
473 fl->fl_start += l->l_start; in flock64_to_posix_lock()
474 if (fl->fl_start < 0) in flock64_to_posix_lock()
475 return -EINVAL; in flock64_to_posix_lock()
477 /* POSIX-1996 leaves the case l->l_len < 0 undefined; in flock64_to_posix_lock()
478 POSIX-2001 defines it. */ in flock64_to_posix_lock()
479 if (l->l_len > 0) { in flock64_to_posix_lock()
480 if (l->l_len - 1 > OFFSET_MAX - fl->fl_start) in flock64_to_posix_lock()
481 return -EOVERFLOW; in flock64_to_posix_lock()
482 fl->fl_end = fl->fl_start + (l->l_len - 1); in flock64_to_posix_lock()
484 } else if (l->l_len < 0) { in flock64_to_posix_lock()
485 if (fl->fl_start + l->l_len < 0) in flock64_to_posix_lock()
486 return -EINVAL; in flock64_to_posix_lock()
487 fl->fl_end = fl->fl_start - 1; in flock64_to_posix_lock()
488 fl->fl_start += l->l_len; in flock64_to_posix_lock()
490 fl->fl_end = OFFSET_MAX; in flock64_to_posix_lock()
492 fl->fl_owner = current->files; in flock64_to_posix_lock()
493 fl->fl_pid = current->tgid; in flock64_to_posix_lock()
494 fl->fl_file = filp; in flock64_to_posix_lock()
495 fl->fl_flags = FL_POSIX; in flock64_to_posix_lock()
496 fl->fl_ops = NULL; in flock64_to_posix_lock()
497 fl->fl_lmops = NULL; in flock64_to_posix_lock()
499 return assign_type(fl, l->l_type); in flock64_to_posix_lock()
509 .l_type = l->l_type, in flock_to_posix_lock()
510 .l_whence = l->l_whence, in flock_to_posix_lock()
511 .l_start = l->l_start, in flock_to_posix_lock()
512 .l_len = l->l_len, in flock_to_posix_lock()
522 kill_fasync(&fl->fl_fasync, SIGIO, POLL_MSG); in lease_break_callback()
529 struct file *filp = fl->fl_file; in lease_setup()
537 if (!fasync_insert_entry(fa->fa_fd, filp, &fl->fl_fasync, fa)) in lease_setup()
555 return -EINVAL; in lease_init()
557 fl->fl_owner = filp; in lease_init()
558 fl->fl_pid = current->tgid; in lease_init()
560 fl->fl_file = filp; in lease_init()
561 fl->fl_flags = FL_LEASE; in lease_init()
562 fl->fl_start = 0; in lease_init()
563 fl->fl_end = OFFSET_MAX; in lease_init()
564 fl->fl_ops = NULL; in lease_init()
565 fl->fl_lmops = &lease_manager_ops; in lease_init()
573 int error = -ENOMEM; in lease_alloc()
590 return ((fl1->fl_end >= fl2->fl_start) && in locks_overlap()
591 (fl2->fl_end >= fl1->fl_start)); in locks_overlap()
599 return fl1->fl_owner == fl2->fl_owner; in posix_same_owner()
609 spin_lock(&fll->lock); in locks_insert_global_locks()
610 fl->fl_link_cpu = smp_processor_id(); in locks_insert_global_locks()
611 hlist_add_head(&fl->fl_link, &fll->hlist); in locks_insert_global_locks()
612 spin_unlock(&fll->lock); in locks_insert_global_locks()
627 if (hlist_unhashed(&fl->fl_link)) in locks_delete_global_locks()
630 fll = per_cpu_ptr(&file_lock_list, fl->fl_link_cpu); in locks_delete_global_locks()
631 spin_lock(&fll->lock); in locks_delete_global_locks()
632 hlist_del_init(&fl->fl_link); in locks_delete_global_locks()
633 spin_unlock(&fll->lock); in locks_delete_global_locks()
639 return (unsigned long)fl->fl_owner; in posix_owner_key()
646 hash_add(blocked_hash, &waiter->fl_link, posix_owner_key(waiter)); in locks_insert_global_blocked()
653 hash_del(&waiter->fl_link); in locks_delete_global_blocked()
664 list_del_init(&waiter->fl_blocked_member); in __locks_delete_block()
669 while (!list_empty(&blocker->fl_blocked_requests)) { in __locks_wake_up_blocks()
672 waiter = list_first_entry(&blocker->fl_blocked_requests, in __locks_wake_up_blocks()
675 if (waiter->fl_lmops && waiter->fl_lmops->lm_notify) in __locks_wake_up_blocks()
676 waiter->fl_lmops->lm_notify(waiter); in __locks_wake_up_blocks()
678 wake_up(&waiter->fl_wait); in __locks_wake_up_blocks()
685 smp_store_release(&waiter->fl_blocker, NULL); in __locks_wake_up_blocks()
690 * locks_delete_block - stop waiting for a file lock
697 int status = -ENOENT; in locks_delete_block()
720 if (!smp_load_acquire(&waiter->fl_blocker) && in locks_delete_block()
721 list_empty(&waiter->fl_blocked_requests)) in locks_delete_block()
725 if (waiter->fl_blocker) in locks_delete_block()
734 smp_store_release(&waiter->fl_blocker, NULL); in locks_delete_block()
761 BUG_ON(!list_empty(&waiter->fl_blocked_member)); in __locks_insert_block()
764 list_for_each_entry(fl, &blocker->fl_blocked_requests, fl_blocked_member) in __locks_insert_block()
769 waiter->fl_blocker = blocker; in __locks_insert_block()
770 list_add_tail(&waiter->fl_blocked_member, &blocker->fl_blocked_requests); in __locks_insert_block()
774 /* The requests in waiter->fl_blocked are known to conflict with in __locks_insert_block()
795 * Must be called with the inode->flc_lock held!
806 if (list_empty(&blocker->fl_blocked_requests)) in locks_wake_up_blocks()
817 list_add_tail(&fl->fl_list, before); in locks_insert_lock_ctx()
825 list_del_init(&fl->fl_list); in locks_unlink_lock_ctx()
834 list_add(&fl->fl_list, dispose); in locks_delete_lock_ctx()
845 if (sys_fl->fl_type == F_WRLCK) in locks_conflict()
847 if (caller_fl->fl_type == F_WRLCK) in locks_conflict()
872 * path so checks for additional GETLK-specific things like F_UNLCK.
878 if (caller_fl->fl_type == F_UNLCK) { in posix_test_locks_conflict()
895 if (caller_fl->fl_file == sys_fl->fl_file) in flock_locks_conflict()
911 if (!ctx || list_empty_careful(&ctx->flc_posix)) { in posix_test_lock()
912 fl->fl_type = F_UNLCK; in posix_test_lock()
917 spin_lock(&ctx->flc_lock); in posix_test_lock()
918 list_for_each_entry(cfl, &ctx->flc_posix, fl_list) { in posix_test_lock()
921 if (cfl->fl_lmops && cfl->fl_lmops->lm_lock_expirable in posix_test_lock()
922 && (*cfl->fl_lmops->lm_lock_expirable)(cfl)) { in posix_test_lock()
923 owner = cfl->fl_lmops->lm_mod_owner; in posix_test_lock()
924 func = cfl->fl_lmops->lm_expire_lock; in posix_test_lock()
926 spin_unlock(&ctx->flc_lock); in posix_test_lock()
934 fl->fl_type = F_UNLCK; in posix_test_lock()
936 spin_unlock(&ctx->flc_lock); in posix_test_lock()
983 while (fl->fl_blocker) in what_owner_is_waiting_for()
984 fl = fl->fl_blocker; in what_owner_is_waiting_for()
1001 * FL_OFDLCK locks, since they aren't owned by a process, per-se. in posix_locks_deadlock()
1020 * value for -ENOENT.
1031 ctx = locks_get_lock_context(inode, request->fl_type); in flock_lock_inode()
1033 if (request->fl_type != F_UNLCK) in flock_lock_inode()
1034 return -ENOMEM; in flock_lock_inode()
1035 return (request->fl_flags & FL_EXISTS) ? -ENOENT : 0; in flock_lock_inode()
1038 if (!(request->fl_flags & FL_ACCESS) && (request->fl_type != F_UNLCK)) { in flock_lock_inode()
1041 return -ENOMEM; in flock_lock_inode()
1045 spin_lock(&ctx->flc_lock); in flock_lock_inode()
1046 if (request->fl_flags & FL_ACCESS) in flock_lock_inode()
1049 list_for_each_entry(fl, &ctx->flc_flock, fl_list) { in flock_lock_inode()
1050 if (request->fl_file != fl->fl_file) in flock_lock_inode()
1052 if (request->fl_type == fl->fl_type) in flock_lock_inode()
1059 if (request->fl_type == F_UNLCK) { in flock_lock_inode()
1060 if ((request->fl_flags & FL_EXISTS) && !found) in flock_lock_inode()
1061 error = -ENOENT; in flock_lock_inode()
1066 list_for_each_entry(fl, &ctx->flc_flock, fl_list) { in flock_lock_inode()
1069 error = -EAGAIN; in flock_lock_inode()
1070 if (!(request->fl_flags & FL_SLEEP)) in flock_lock_inode()
1076 if (request->fl_flags & FL_ACCESS) in flock_lock_inode()
1080 locks_insert_lock_ctx(new_fl, &ctx->flc_flock); in flock_lock_inode()
1085 spin_unlock(&ctx->flc_lock); in flock_lock_inode()
1109 ctx = locks_get_lock_context(inode, request->fl_type); in posix_lock_inode()
1111 return (request->fl_type == F_UNLCK) ? 0 : -ENOMEM; in posix_lock_inode()
1119 if (!(request->fl_flags & FL_ACCESS) && in posix_lock_inode()
1120 (request->fl_type != F_UNLCK || in posix_lock_inode()
1121 request->fl_start != 0 || request->fl_end != OFFSET_MAX)) { in posix_lock_inode()
1128 spin_lock(&ctx->flc_lock); in posix_lock_inode()
1134 if (request->fl_type != F_UNLCK) { in posix_lock_inode()
1135 list_for_each_entry(fl, &ctx->flc_posix, fl_list) { in posix_lock_inode()
1138 if (fl->fl_lmops && fl->fl_lmops->lm_lock_expirable in posix_lock_inode()
1139 && (*fl->fl_lmops->lm_lock_expirable)(fl)) { in posix_lock_inode()
1140 owner = fl->fl_lmops->lm_mod_owner; in posix_lock_inode()
1141 func = fl->fl_lmops->lm_expire_lock; in posix_lock_inode()
1143 spin_unlock(&ctx->flc_lock); in posix_lock_inode()
1151 error = -EAGAIN; in posix_lock_inode()
1152 if (!(request->fl_flags & FL_SLEEP)) in posix_lock_inode()
1158 error = -EDEADLK; in posix_lock_inode()
1177 if (request->fl_flags & FL_ACCESS) in posix_lock_inode()
1181 list_for_each_entry(fl, &ctx->flc_posix, fl_list) { in posix_lock_inode()
1187 list_for_each_entry_safe_from(fl, tmp, &ctx->flc_posix, fl_list) { in posix_lock_inode()
1192 if (request->fl_type == fl->fl_type) { in posix_lock_inode()
1194 * "start - 1" rather than "end + 1". If end in posix_lock_inode()
1197 if (fl->fl_end < request->fl_start - 1) in posix_lock_inode()
1202 if (fl->fl_start - 1 > request->fl_end) in posix_lock_inode()
1210 if (fl->fl_start > request->fl_start) in posix_lock_inode()
1211 fl->fl_start = request->fl_start; in posix_lock_inode()
1213 request->fl_start = fl->fl_start; in posix_lock_inode()
1214 if (fl->fl_end < request->fl_end) in posix_lock_inode()
1215 fl->fl_end = request->fl_end; in posix_lock_inode()
1217 request->fl_end = fl->fl_end; in posix_lock_inode()
1228 if (fl->fl_end < request->fl_start) in posix_lock_inode()
1230 if (fl->fl_start > request->fl_end) in posix_lock_inode()
1232 if (request->fl_type == F_UNLCK) in posix_lock_inode()
1234 if (fl->fl_start < request->fl_start) in posix_lock_inode()
1239 if (fl->fl_end > request->fl_end) { in posix_lock_inode()
1243 if (fl->fl_start >= request->fl_start) { in posix_lock_inode()
1258 error = -ENOLCK; in posix_lock_inode()
1265 locks_insert_lock_ctx(request, &fl->fl_list); in posix_lock_inode()
1277 error = -ENOLCK; /* "no luck" */ in posix_lock_inode()
1283 if (request->fl_type == F_UNLCK) { in posix_lock_inode()
1284 if (request->fl_flags & FL_EXISTS) in posix_lock_inode()
1285 error = -ENOENT; in posix_lock_inode()
1290 error = -ENOLCK; in posix_lock_inode()
1295 locks_insert_lock_ctx(new_fl, &fl->fl_list); in posix_lock_inode()
1307 locks_insert_lock_ctx(left, &fl->fl_list); in posix_lock_inode()
1309 right->fl_start = request->fl_end + 1; in posix_lock_inode()
1313 left->fl_end = request->fl_start - 1; in posix_lock_inode()
1318 spin_unlock(&ctx->flc_lock); in posix_lock_inode()
1333 * posix_lock_file - Apply a POSIX-style lock to a file
1344 * value for -ENOENT.
1354 * posix_lock_inode_wait - Apply a POSIX-style lock to a file
1368 error = wait_event_interruptible(fl->fl_wait, in posix_lock_inode_wait()
1369 list_empty(&fl->fl_blocked_member)); in posix_lock_inode_wait()
1381 fl->fl_flags &= ~FL_UNLOCK_PENDING; in lease_clear_pending()
1384 fl->fl_flags &= ~FL_DOWNGRADE_PENDING; in lease_clear_pending()
1398 struct file *filp = fl->fl_file; in lease_modify()
1401 filp->f_owner.signum = 0; in lease_modify()
1402 fasync_helper(0, fl->fl_file, 0, &fl->fl_fasync); in lease_modify()
1403 if (fl->fl_fasync != NULL) { in lease_modify()
1404 printk(KERN_ERR "locks_delete_lock: fasync == %p\n", fl->fl_fasync); in lease_modify()
1405 fl->fl_fasync = NULL; in lease_modify()
1423 struct file_lock_context *ctx = inode->i_flctx; in time_out_leases()
1426 lockdep_assert_held(&ctx->flc_lock); in time_out_leases()
1428 list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list) { in time_out_leases()
1430 if (past_time(fl->fl_downgrade_time)) in time_out_leases()
1432 if (past_time(fl->fl_break_time)) in time_out_leases()
1441 if (lease->fl_lmops->lm_breaker_owns_lease in leases_conflict()
1442 && lease->fl_lmops->lm_breaker_owns_lease(lease)) in leases_conflict()
1444 if ((breaker->fl_flags & FL_LAYOUT) != (lease->fl_flags & FL_LAYOUT)) { in leases_conflict()
1448 if ((breaker->fl_flags & FL_DELEG) && (lease->fl_flags & FL_LEASE)) { in leases_conflict()
1462 struct file_lock_context *ctx = inode->i_flctx; in any_leases_conflict()
1465 lockdep_assert_held(&ctx->flc_lock); in any_leases_conflict()
1467 list_for_each_entry(fl, &ctx->flc_lease, fl_list) { in any_leases_conflict()
1475 * __break_lease - revoke all outstanding leases on file
1499 new_fl->fl_flags = type; in __break_lease()
1501 /* typically we will check that ctx is non-NULL before calling */ in __break_lease()
1509 spin_lock(&ctx->flc_lock); in __break_lease()
1523 list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list) { in __break_lease()
1527 if (fl->fl_flags & FL_UNLOCK_PENDING) in __break_lease()
1529 fl->fl_flags |= FL_UNLOCK_PENDING; in __break_lease()
1530 fl->fl_break_time = break_time; in __break_lease()
1534 fl->fl_flags |= FL_DOWNGRADE_PENDING; in __break_lease()
1535 fl->fl_downgrade_time = break_time; in __break_lease()
1537 if (fl->fl_lmops->lm_break(fl)) in __break_lease()
1541 if (list_empty(&ctx->flc_lease)) in __break_lease()
1546 error = -EWOULDBLOCK; in __break_lease()
1551 fl = list_first_entry(&ctx->flc_lease, struct file_lock, fl_list); in __break_lease()
1552 break_time = fl->fl_break_time; in __break_lease()
1554 break_time -= jiffies; in __break_lease()
1559 spin_unlock(&ctx->flc_lock); in __break_lease()
1563 error = wait_event_interruptible_timeout(new_fl->fl_wait, in __break_lease()
1564 list_empty(&new_fl->fl_blocked_member), in __break_lease()
1568 spin_lock(&ctx->flc_lock); in __break_lease()
1583 spin_unlock(&ctx->flc_lock); in __break_lease()
1593 * lease_get_mtime - update modified time of an inode with exclusive lease
1598 * exclusive leases. The justification is that if someone has an
1608 if (ctx && !list_empty_careful(&ctx->flc_lease)) { in lease_get_mtime()
1609 spin_lock(&ctx->flc_lock); in lease_get_mtime()
1610 fl = list_first_entry_or_null(&ctx->flc_lease, in lease_get_mtime()
1612 if (fl && (fl->fl_type == F_WRLCK)) in lease_get_mtime()
1614 spin_unlock(&ctx->flc_lock); in lease_get_mtime()
1623 * fcntl_getlease - Enquire what lease is currently active
1654 if (ctx && !list_empty_careful(&ctx->flc_lease)) { in fcntl_getlease()
1656 spin_lock(&ctx->flc_lock); in fcntl_getlease()
1658 list_for_each_entry(fl, &ctx->flc_lease, fl_list) { in fcntl_getlease()
1659 if (fl->fl_file != filp) in fcntl_getlease()
1664 spin_unlock(&ctx->flc_lock); in fcntl_getlease()
1673 * check_conflicting_open - see if the given file points to an inode that has
1696 return inode_is_open_for_write(inode) ? -EAGAIN : 0; in check_conflicting_open()
1706 if (filp->f_mode & FMODE_WRITE) in check_conflicting_open()
1708 else if (filp->f_mode & FMODE_READ) in check_conflicting_open()
1711 if (atomic_read(&inode->i_writecount) != self_wcount || in check_conflicting_open()
1712 atomic_read(&inode->i_readcount) != self_rcount) in check_conflicting_open()
1713 return -EAGAIN; in check_conflicting_open()
1724 bool is_deleg = (*flp)->fl_flags & FL_DELEG; in generic_add_lease()
1734 return -ENOMEM; in generic_add_lease()
1740 * there's some chance of a conflict--we'd rather not in generic_add_lease()
1745 return -EAGAIN; in generic_add_lease()
1748 spin_lock(&ctx->flc_lock); in generic_add_lease()
1750 error = check_conflicting_open(filp, arg, lease->fl_flags); in generic_add_lease()
1762 error = -EAGAIN; in generic_add_lease()
1763 list_for_each_entry(fl, &ctx->flc_lease, fl_list) { in generic_add_lease()
1764 if (fl->fl_file == filp && in generic_add_lease()
1765 fl->fl_owner == lease->fl_owner) { in generic_add_lease()
1780 if (fl->fl_flags & FL_UNLOCK_PENDING) in generic_add_lease()
1786 error = lease->fl_lmops->lm_change(lease, arg, &dispose); in generic_add_lease()
1792 error = -EINVAL; in generic_add_lease()
1796 locks_insert_lock_ctx(lease, &ctx->flc_lease); in generic_add_lease()
1807 error = check_conflicting_open(filp, arg, lease->fl_flags); in generic_add_lease()
1814 if (lease->fl_lmops->lm_setup) in generic_add_lease()
1815 lease->fl_lmops->lm_setup(lease, priv); in generic_add_lease()
1817 spin_unlock(&ctx->flc_lock); in generic_add_lease()
1829 int error = -EAGAIN; in generic_delete_lease()
1842 spin_lock(&ctx->flc_lock); in generic_delete_lease()
1843 list_for_each_entry(fl, &ctx->flc_lease, fl_list) { in generic_delete_lease()
1844 if (fl->fl_file == filp && in generic_delete_lease()
1845 fl->fl_owner == owner) { in generic_delete_lease()
1852 error = fl->fl_lmops->lm_change(victim, F_UNLCK, &dispose); in generic_delete_lease()
1853 spin_unlock(&ctx->flc_lock); in generic_delete_lease()
1860 * generic_setlease - sets a lease on an open file
1863 * @flp: input - file_lock to use, output - file_lock inserted
1867 * The (input) flp->fl_lmops->lm_break function is required
1878 return -EACCES; in generic_setlease()
1879 if (!S_ISREG(inode->i_mode)) in generic_setlease()
1880 return -EINVAL; in generic_setlease()
1890 if (!(*flp)->fl_lmops->lm_break) { in generic_setlease()
1892 return -ENOLCK; in generic_setlease()
1897 return -EINVAL; in generic_setlease()
1936 * vfs_setlease - sets a lease on an open file
1945 * an existing lease, the ``(*lease)->fl_lmops->lm_break`` operation must be
1946 * set; if not, this function will return -ENOLCK (and generate a scary-looking
1949 * The "priv" pointer is passed directly to the lm_setup function as-is. It
1957 if (filp->f_op->setlease) in vfs_setlease()
1958 return filp->f_op->setlease(filp, arg, lease, priv); in vfs_setlease()
1977 return -ENOMEM; in do_fcntl_add_lease()
1979 new->fa_fd = fd; in do_fcntl_add_lease()
1990 * fcntl_setlease - sets a lease on an open file
2007 * flock_lock_inode_wait - Apply a FLOCK-style lock to a file
2021 error = wait_event_interruptible(fl->fl_wait, in flock_lock_inode_wait()
2022 list_empty(&fl->fl_blocked_member)); in flock_lock_inode_wait()
2031 * locks_lock_inode_wait - Apply a lock to an inode
2040 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) { in locks_lock_inode_wait()
2055 * sys_flock: - flock() system call.
2062 * - %LOCK_SH -- a shared lock.
2063 * - %LOCK_EX -- an exclusive lock.
2064 * - %LOCK_UN -- remove an existing lock.
2065 * - %LOCK_MAND -- a 'mandatory' flock. (DEPRECATED)
2084 … flock(2). This support has been removed and the request ignored.\n", current->comm, current->pid); in SYSCALL_DEFINE2()
2092 error = -EBADF; in SYSCALL_DEFINE2()
2097 if (type != F_UNLCK && !(f.file->f_mode & (FMODE_READ | FMODE_WRITE))) in SYSCALL_DEFINE2()
2110 if (f.file->f_op->flock) in SYSCALL_DEFINE2()
2111 error = f.file->f_op->flock(f.file, in SYSCALL_DEFINE2()
2125 * vfs_test_lock - test file byte range lock
2129 * Returns -ERRNO on failure. Indicates presence of conflicting lock by
2130 * setting conf->fl_type to something other than F_UNLCK.
2134 WARN_ON_ONCE(filp != fl->fl_file); in vfs_test_lock()
2135 if (filp->f_op->lock) in vfs_test_lock()
2136 return filp->f_op->lock(filp, F_GETLK, fl); in vfs_test_lock()
2143 * locks_translate_pid - translate a file_lock's fl_pid number into a namespace
2155 return -1; in locks_translate_pid()
2157 return fl->fl_pid; in locks_translate_pid()
2164 return (pid_t)fl->fl_pid; in locks_translate_pid()
2167 pid = find_pid_ns(fl->fl_pid, &init_pid_ns); in locks_translate_pid()
2175 flock->l_pid = locks_translate_pid(fl, task_active_pid_ns(current)); in posix_lock_to_flock()
2181 if (fl->fl_start > OFFT_OFFSET_MAX) in posix_lock_to_flock()
2182 return -EOVERFLOW; in posix_lock_to_flock()
2183 if (fl->fl_end != OFFSET_MAX && fl->fl_end > OFFT_OFFSET_MAX) in posix_lock_to_flock()
2184 return -EOVERFLOW; in posix_lock_to_flock()
2186 flock->l_start = fl->fl_start; in posix_lock_to_flock()
2187 flock->l_len = fl->fl_end == OFFSET_MAX ? 0 : in posix_lock_to_flock()
2188 fl->fl_end - fl->fl_start + 1; in posix_lock_to_flock()
2189 flock->l_whence = 0; in posix_lock_to_flock()
2190 flock->l_type = fl->fl_type; in posix_lock_to_flock()
2197 flock->l_pid = locks_translate_pid(fl, task_active_pid_ns(current)); in posix_lock_to_flock64()
2198 flock->l_start = fl->fl_start; in posix_lock_to_flock64()
2199 flock->l_len = fl->fl_end == OFFSET_MAX ? 0 : in posix_lock_to_flock64()
2200 fl->fl_end - fl->fl_start + 1; in posix_lock_to_flock64()
2201 flock->l_whence = 0; in posix_lock_to_flock64()
2202 flock->l_type = fl->fl_type; in posix_lock_to_flock64()
2216 return -ENOMEM; in fcntl_getlk()
2217 error = -EINVAL; in fcntl_getlk()
2218 if (cmd != F_OFD_GETLK && flock->l_type != F_RDLCK in fcntl_getlk()
2219 && flock->l_type != F_WRLCK) in fcntl_getlk()
2227 error = -EINVAL; in fcntl_getlk()
2228 if (flock->l_pid != 0) in fcntl_getlk()
2231 fl->fl_flags |= FL_OFDLCK; in fcntl_getlk()
2232 fl->fl_owner = filp; in fcntl_getlk()
2239 flock->l_type = fl->fl_type; in fcntl_getlk()
2240 if (fl->fl_type != F_UNLCK) { in fcntl_getlk()
2251 * vfs_lock_file - file byte range lock
2260 * If the filesystem defines a private ->lock() method, then @conf will
2265 * locks, the ->lock() interface may return asynchronously, before the lock has
2267 * lm_grant is set. Callers expecting ->lock() to return asynchronously
2269 * the request is for a blocking lock. When ->lock() does return asynchronously,
2270 * it must return FILE_LOCK_DEFERRED, and call ->lm_grant() when the lock
2272 * If the request is for non-blocking lock the file system should return
2280 * ->lm_grant() before returning to the caller with a FILE_LOCK_DEFERRED
2285 WARN_ON_ONCE(filp != fl->fl_file); in vfs_lock_file()
2286 if (filp->f_op->lock) in vfs_lock_file()
2287 return filp->f_op->lock(filp, cmd, fl); in vfs_lock_file()
2298 error = security_file_lock(filp, fl->fl_type); in do_lock_file_wait()
2306 error = wait_event_interruptible(fl->fl_wait, in do_lock_file_wait()
2307 list_empty(&fl->fl_blocked_member)); in do_lock_file_wait()
2316 /* Ensure that fl->fl_file has compatible f_mode for F_SETLK calls */
2320 switch (fl->fl_type) { in check_fmode_for_setlk()
2322 if (!(fl->fl_file->f_mode & FMODE_READ)) in check_fmode_for_setlk()
2323 return -EBADF; in check_fmode_for_setlk()
2326 if (!(fl->fl_file->f_mode & FMODE_WRITE)) in check_fmode_for_setlk()
2327 return -EBADF; in check_fmode_for_setlk()
2344 return -ENOLCK; in fcntl_setlk()
2355 * If the cmd is requesting file-private locks, then set the in fcntl_setlk()
2360 error = -EINVAL; in fcntl_setlk()
2361 if (flock->l_pid != 0) in fcntl_setlk()
2365 file_lock->fl_flags |= FL_OFDLCK; in fcntl_setlk()
2366 file_lock->fl_owner = filp; in fcntl_setlk()
2369 error = -EINVAL; in fcntl_setlk()
2370 if (flock->l_pid != 0) in fcntl_setlk()
2374 file_lock->fl_flags |= FL_OFDLCK; in fcntl_setlk()
2375 file_lock->fl_owner = filp; in fcntl_setlk()
2378 file_lock->fl_flags |= FL_SLEEP; in fcntl_setlk()
2389 if (!error && file_lock->fl_type != F_UNLCK && in fcntl_setlk()
2390 !(file_lock->fl_flags & FL_OFDLCK)) { in fcntl_setlk()
2391 struct files_struct *files = current->files; in fcntl_setlk()
2393 * We need that spin_lock here - it prevents reordering between in fcntl_setlk()
2394 * update of i_flctx->flc_posix and check for it done in in fcntl_setlk()
2397 spin_lock(&files->file_lock); in fcntl_setlk()
2399 spin_unlock(&files->file_lock); in fcntl_setlk()
2402 error = -EBADF; in fcntl_setlk()
2422 return -ENOMEM; in fcntl_getlk64()
2424 error = -EINVAL; in fcntl_getlk64()
2425 if (cmd != F_OFD_GETLK && flock->l_type != F_RDLCK in fcntl_getlk64()
2426 && flock->l_type != F_WRLCK) in fcntl_getlk64()
2434 error = -EINVAL; in fcntl_getlk64()
2435 if (flock->l_pid != 0) in fcntl_getlk64()
2438 fl->fl_flags |= FL_OFDLCK; in fcntl_getlk64()
2439 fl->fl_owner = filp; in fcntl_getlk64()
2446 flock->l_type = fl->fl_type; in fcntl_getlk64()
2447 if (fl->fl_type != F_UNLCK) in fcntl_getlk64()
2466 return -ENOLCK; in fcntl_setlk64()
2477 * If the cmd is requesting file-private locks, then set the in fcntl_setlk64()
2482 error = -EINVAL; in fcntl_setlk64()
2483 if (flock->l_pid != 0) in fcntl_setlk64()
2487 file_lock->fl_flags |= FL_OFDLCK; in fcntl_setlk64()
2488 file_lock->fl_owner = filp; in fcntl_setlk64()
2491 error = -EINVAL; in fcntl_setlk64()
2492 if (flock->l_pid != 0) in fcntl_setlk64()
2496 file_lock->fl_flags |= FL_OFDLCK; in fcntl_setlk64()
2497 file_lock->fl_owner = filp; in fcntl_setlk64()
2500 file_lock->fl_flags |= FL_SLEEP; in fcntl_setlk64()
2511 if (!error && file_lock->fl_type != F_UNLCK && in fcntl_setlk64()
2512 !(file_lock->fl_flags & FL_OFDLCK)) { in fcntl_setlk64()
2513 struct files_struct *files = current->files; in fcntl_setlk64()
2515 * We need that spin_lock here - it prevents reordering between in fcntl_setlk64()
2516 * update of i_flctx->flc_posix and check for it done in in fcntl_setlk64()
2519 spin_lock(&files->file_lock); in fcntl_setlk64()
2521 spin_unlock(&files->file_lock); in fcntl_setlk64()
2524 error = -EBADF; in fcntl_setlk64()
2551 if (!ctx || list_empty(&ctx->flc_posix)) in locks_remove_posix()
2560 lock.fl_pid = current->tgid; in locks_remove_posix()
2567 if (lock.fl_ops && lock.fl_ops->fl_release_private) in locks_remove_posix()
2568 lock.fl_ops->fl_release_private(&lock); in locks_remove_posix()
2580 if (list_empty(&flctx->flc_flock)) in locks_remove_flock()
2586 if (filp->f_op->flock) in locks_remove_flock()
2587 filp->f_op->flock(filp, F_SETLKW, &fl); in locks_remove_flock()
2591 if (fl.fl_ops && fl.fl_ops->fl_release_private) in locks_remove_flock()
2592 fl.fl_ops->fl_release_private(&fl); in locks_remove_flock()
2602 if (list_empty(&ctx->flc_lease)) in locks_remove_lease()
2606 spin_lock(&ctx->flc_lock); in locks_remove_lease()
2607 list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list) in locks_remove_lease()
2608 if (filp == fl->fl_file) in locks_remove_lease()
2610 spin_unlock(&ctx->flc_lock); in locks_remove_lease()
2636 spin_lock(&ctx->flc_lock); in locks_remove_file()
2637 locks_check_ctx_file_list(filp, &ctx->flc_posix, "POSIX"); in locks_remove_file()
2638 locks_check_ctx_file_list(filp, &ctx->flc_flock, "FLOCK"); in locks_remove_file()
2639 locks_check_ctx_file_list(filp, &ctx->flc_lease, "LEASE"); in locks_remove_file()
2640 spin_unlock(&ctx->flc_lock); in locks_remove_file()
2644 * vfs_cancel_lock - file byte range unblock lock
2652 WARN_ON_ONCE(filp != fl->fl_file); in vfs_cancel_lock()
2653 if (filp->f_op->lock) in vfs_cancel_lock()
2654 return filp->f_op->lock(filp, F_CANCELLK, fl); in vfs_cancel_lock()
2660 * vfs_inode_has_locks - are any file locks held on @inode?
2675 spin_lock(&ctx->flc_lock); in vfs_inode_has_locks()
2676 ret = !list_empty(&ctx->flc_posix) || !list_empty(&ctx->flc_flock); in vfs_inode_has_locks()
2677 spin_unlock(&ctx->flc_lock); in vfs_inode_has_locks()
2696 struct pid_namespace *proc_pidns = proc_pid_ns(file_inode(f->file)->i_sb); in lock_get_status()
2706 if (fl->fl_file != NULL) in lock_get_status()
2707 inode = file_inode(fl->fl_file); in lock_get_status()
2712 seq_printf(f, "%*s", repeat - 1 + (int)strlen(pfx), pfx); in lock_get_status()
2715 if (fl->fl_flags & FL_ACCESS) in lock_get_status()
2727 if (fl->fl_flags & FL_DELEG) in lock_get_status()
2734 else if (fl->fl_file) in lock_get_status()
2741 type = IS_LEASE(fl) ? target_leasetype(fl) : fl->fl_type; in lock_get_status()
2748 MAJOR(inode->i_sb->s_dev), in lock_get_status()
2749 MINOR(inode->i_sb->s_dev), inode->i_ino); in lock_get_status()
2754 if (fl->fl_end == OFFSET_MAX) in lock_get_status()
2755 seq_printf(f, "%Ld EOF\n", fl->fl_start); in lock_get_status()
2757 seq_printf(f, "%Ld %Ld\n", fl->fl_start, fl->fl_end); in lock_get_status()
2768 if (node == NULL || node->fl_blocker == NULL) in get_next_blocked_member()
2773 if (list_entry_is_head(tmp, &node->fl_blocker->fl_blocked_requests, fl_blocked_member) in get_next_blocked_member()
2783 struct locks_iterator *iter = f->private; in locks_show()
2785 struct pid_namespace *proc_pidns = proc_pid_ns(file_inode(f->file)->i_sb); in locks_show()
2800 lock_get_status(f, cur, iter->li_pos, "-> ", level); in locks_show()
2802 lock_get_status(f, cur, iter->li_pos, "", level); in locks_show()
2804 if (!list_empty(&cur->fl_blocked_requests)) { in locks_show()
2806 cur = list_first_entry_or_null(&cur->fl_blocked_requests, in locks_show()
2813 while (tmp == NULL && cur->fl_blocker != NULL) { in locks_show()
2814 cur = cur->fl_blocker; in locks_show()
2815 level--; in locks_show()
2833 if (filp != fl->fl_file) in __show_fd_locks()
2835 if (fl->fl_owner != files && in __show_fd_locks()
2836 fl->fl_owner != filp) in __show_fd_locks()
2856 spin_lock(&ctx->flc_lock); in show_fd_locks()
2857 __show_fd_locks(f, &ctx->flc_flock, &id, filp, files); in show_fd_locks()
2858 __show_fd_locks(f, &ctx->flc_posix, &id, filp, files); in show_fd_locks()
2859 __show_fd_locks(f, &ctx->flc_lease, &id, filp, files); in show_fd_locks()
2860 spin_unlock(&ctx->flc_lock); in show_fd_locks()
2866 struct locks_iterator *iter = f->private; in locks_start()
2868 iter->li_pos = *pos + 1; in locks_start()
2871 return seq_hlist_start_percpu(&file_lock_list.hlist, &iter->li_cpu, *pos); in locks_start()
2876 struct locks_iterator *iter = f->private; in locks_next()
2878 ++iter->li_pos; in locks_next()
2879 return seq_hlist_next_percpu(v, &file_lock_list.hlist, &iter->li_cpu, pos); in locks_next()
2918 spin_lock_init(&fll->lock); in filelock_init()
2919 INIT_HLIST_HEAD(&fll->hlist); in filelock_init()