1 .\" This manpage is Copyright (C) 1992 Drew Eckhardt;
2 .\" and Copyright (C) 1993 Michael Haardt, Ian Jackson;
3 .\" and Copyright (C) 1998 Jamie Lokier;
4 .\" and Copyright (C) 2002-2010, 2014 Michael Kerrisk;
5 .\" and Copyright (C) 2014 Jeff Layton
6 .\" and Copyright (C) 2014 David Herrmann
7 .\" and Copyright (C) 2017 Jens Axboe
9 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
11 .\" Modified 1993-07-24 by Rik Faith <faith@cs.unc.edu>
12 .\" Modified 1995-09-26 by Andries Brouwer <aeb@cwi.nl>
13 .\" and again on 960413 and 980804 and 981223.
14 .\" Modified 1998-12-11 by Jamie Lokier <jamie@imbolc.ucc.ie>
15 .\" Applied correction by Christian Ehrhardt - aeb, 990712
16 .\" Modified 2002-04-23 by Michael Kerrisk <mtk.manpages@gmail.com>
17 .\" Added note on F_SETFL and O_DIRECT
18 .\" Complete rewrite + expansion of material on file locking
19 .\" Incorporated description of F_NOTIFY, drawing on
20 .\" Stephen Rothwell's notes in Documentation/dnotify.txt.
21 .\" Added description of F_SETLEASE and F_GETLEASE
22 .\" Corrected and polished, aeb, 020527.
23 .\" Modified 2004-03-03 by Michael Kerrisk <mtk.manpages@gmail.com>
24 .\" Modified description of file leases: fixed some errors of detail
25 .\" Replaced the term "lease contestant" by "lease breaker"
26 .\" Modified, 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com>
27 .\" Added notes on capability requirements
28 .\" Modified 2004-12-08, added O_NOATIME after note from Martin Pool
29 .\" 2004-12-10, mtk, noted F_GETOWN bug after suggestion from aeb.
30 .\" 2005-04-08 Jamie Lokier <jamie@shareable.org>, mtk
31 .\" Described behavior of F_SETOWN/F_SETSIG in
32 .\" multithreaded processes, and generally cleaned
33 .\" up the discussion of F_SETOWN.
34 .\" 2005-05-20, Johannes Nicolai <johannes.nicolai@hpi.uni-potsdam.de>,
35 .\" mtk: Noted F_SETOWN bug for socket file descriptor in Linux 2.4
36 .\" and earlier. Added text on permissions required to send signal.
37 .\" 2009-09-30, Michael Kerrisk
38 .\" Note obsolete F_SETOWN behavior with threads.
39 .\" Document F_SETOWN_EX and F_GETOWN_EX
40 .\" 2010-06-17, Michael Kerrisk
41 .\" Document F_SETPIPE_SZ and F_GETPIPE_SZ.
42 .\" 2014-07-08, David Herrmann <dh.herrmann@gmail.com>
43 .\" Document F_ADD_SEALS and F_GET_SEALS
44 .\" 2017-06-26, Jens Axboe <axboe@kernel.dk>
45 .\" Document F_{GET,SET}_RW_HINT and F_{GET,SET}_FILE_RW_HINT
47 .TH fcntl 2 (date) "Linux man-pages (unreleased)"
49 fcntl \- manipulate file descriptor
52 .RI ( libc ", " \-lc )
57 .BI "int fcntl(int " fd ", int " op ", ... /* " arg " */ );"
61 performs one of the operations described below on the open file descriptor
63 The operation is determined by
67 can take an optional third argument.
68 Whether or not this argument is required is determined by
70 The required argument type is indicated in parentheses after each
72 name (in most cases, the required type is
74 and we identify the argument using the name
78 is specified if the argument is not required.
80 Certain of the operations below are supported only since a particular
82 The preferred method of checking whether the host kernel supports
83 a particular operation is to invoke
87 value and then test whether the call failed with
89 indicating that the kernel does not recognize this value.
90 .SS Duplicating a file descriptor
92 .BR F_DUPFD " (\fIint\fP)"
93 Duplicate the file descriptor
95 using the lowest-numbered available file descriptor greater than or equal to
97 This is different from
99 which uses exactly the file descriptor specified.
101 On success, the new file descriptor is returned.
107 .BR F_DUPFD_CLOEXEC " (\fIint\fP; since Linux 2.6.24)"
110 but additionally set the
111 close-on-exec flag for the duplicate file descriptor.
112 Specifying this flag permits a program to avoid an additional
118 For an explanation of why this flag is useful,
119 see the description of
123 .SS File descriptor flags
124 The following operations manipulate the flags associated with
126 Currently, only one such flag is defined:
128 the close-on-exec flag.
132 the file descriptor will automatically be closed during a successful
136 fails, the file descriptor is left open.)
139 bit is not set, the file descriptor will remain open across an
142 .BR F_GETFD " (\fIvoid\fP)"
143 Return (as the function result) the file descriptor flags;
147 .BR F_SETFD " (\fIint\fP)"
148 Set the file descriptor flags to the value specified by
151 In multithreaded programs, using
154 to set the close-on-exec flag at the same time as another thread performs a
158 is vulnerable to a race condition that may unintentionally leak
159 the file descriptor to the program executed in the child process.
160 See the discussion of the
164 for details and a remedy to the problem.
165 .SS File status flags
166 Each open file description has certain associated status flags,
171 and possibly modified by
173 Duplicated file descriptors
178 etc.) refer to the same open file description, and thus
179 share the same file status flags.
181 The file status flags and their semantics are described in
184 .BR F_GETFL " (\fIvoid\fP)"
185 Return (as the function result)
186 the file access mode and the file status flags;
190 .BR F_SETFL " (\fIint\fP)"
191 Set the file status flags to the value specified by
194 .RB ( O_RDONLY ", " O_WRONLY ", " O_RDWR )
195 and file creation flags
197 .BR O_CREAT ", " O_EXCL ", " O_NOCTTY ", " O_TRUNC )
201 On Linux, this operation can change only the
209 It is not possible to change the
213 flags; see BUGS, below.
214 .SS Advisory record locking
215 Linux implements traditional ("process-associated") UNIX record locks,
216 as standardized by POSIX.
217 For a Linux-specific alternative with better semantics,
218 see the discussion of open file description locks below.
224 are used to acquire, release, and test for the existence of record
225 locks (also known as byte-range, file-segment, or file-region locks).
228 is a pointer to a structure that has at least the following fields
229 (in unspecified order).
235 short l_type; /* Type of lock: F_RDLCK,
237 short l_whence; /* How to interpret l_start:
238 SEEK_SET, SEEK_CUR, SEEK_END */
239 off_t l_start; /* Starting offset for lock */
240 off_t l_len; /* Number of bytes to lock */
241 pid_t l_pid; /* PID of process blocking our lock
242 (set by F_GETLK and F_OFD_GETLK) */
249 .IR l_whence ", " l_start ", and " l_len
250 fields of this structure specify the range of bytes we wish to lock.
251 Bytes past the end of the file may be locked,
252 but not bytes before the start of the file.
255 is the starting offset for the lock, and is interpreted
257 the start of the file (if
261 the current file offset (if
265 or the end of the file (if
269 In the final two cases,
271 can be a negative number provided the
272 offset does not lie before the start of the file.
275 specifies the number of bytes to be locked.
278 is positive, then the range to be locked covers bytes
281 .IR l_start + l_len \-1.
284 has the special meaning: lock all bytes starting at the
285 location specified by
286 .IR l_whence " and " l_start
287 through to the end of file, no matter how large the file grows.
289 POSIX.1-2001 allows (but does not require)
290 an implementation to support a negative
294 is negative, the interval described by
300 This is supported since Linux 2.4.21 and Linux 2.5.49.
304 field can be used to place a read
309 Any number of processes may hold a read lock (shared lock)
310 on a file region, but only one process may hold a write lock
312 An exclusive lock excludes all other locks,
313 both shared and exclusive.
314 A single process can hold only one type of lock on a file region;
315 if a new lock is applied to an already-locked region,
316 then the existing lock is converted to the new lock type.
317 (Such conversions may involve splitting, shrinking, or coalescing with
318 an existing lock if the byte range specified by the new lock does not
319 precisely coincide with the range of the existing lock.)
321 .BR F_SETLK " (\fIstruct flock *\fP)"
328 or release a lock (when
332 on the bytes specified by the
333 .IR l_whence ", " l_start ", and " l_len
336 If a conflicting lock is held by another process,
337 this call returns \-1 and sets
343 (The error returned in this case differs across implementations,
344 so POSIX requires a portable application to check for both errors.)
346 .BR F_SETLKW " (\fIstruct flock *\fP)"
349 but if a conflicting lock is held on the file, then wait for that
351 If a signal is caught while waiting, then the call is interrupted
352 and (after the signal handler has returned)
353 returns immediately (with return value \-1 and
360 .BR F_GETLK " (\fIstruct flock *\fP)"
361 On input to this call,
363 describes a lock we would like to place on the file.
364 If the lock could be placed,
366 does not actually place it, but returns
372 and leaves the other fields of the structure unchanged.
374 If one or more incompatible locks would prevent
375 this lock being placed, then
377 returns details about one of those locks in the
378 .IR l_type ", " l_whence ", " l_start ", and " l_len
381 If the conflicting lock is a traditional (process-associated) record lock,
384 field is set to the PID of the process holding that lock.
385 If the conflicting lock is an open file description lock, then
388 Note that the returned information
389 may already be out of date by the time the caller inspects it.
391 In order to place a read lock,
393 must be open for reading.
394 In order to place a write lock,
396 must be open for writing.
397 To place both types of lock, open a file read-write.
399 When placing locks with
403 whereby two or more processes have their
404 lock requests mutually blocked by locks held by the other processes.
405 For example, suppose process A holds a write lock on byte 100 of a file,
406 and process B holds a write lock on byte 200.
407 If each process then attempts to lock the byte already
408 locked by the other process using
410 then, without deadlock detection,
411 both processes would remain blocked indefinitely.
412 When the kernel detects such deadlocks,
413 it causes one of the blocking lock requests to immediately fail with the error
415 an application that encounters such an error should release
416 some of its locks to allow other applications to proceed before
417 attempting regain the locks that it requires.
418 Circular deadlocks involving more than two processes are also detected.
419 Note, however, that there are limitations to the kernel's
420 deadlock-detection algorithm; see BUGS.
422 As well as being removed by an explicit
424 record locks are automatically released when the process terminates.
426 Record locks are not inherited by a child created via
428 but are preserved across an
431 Because of the buffering performed by the
433 library, the use of record locking with routines in that package
434 should be avoided; use
440 The record locks described above are associated with the process
441 (unlike the open file description locks described below).
442 This has some unfortunate consequences:
446 file descriptor referring to a file,
447 then all of the process's locks on that file are released,
448 regardless of the file descriptor(s) on which the locks were obtained.
449 .\" (Additional file descriptors referring to the same file
450 .\" may have been obtained by calls to
451 .\" .BR open "(2), " dup "(2), " dup2 "(2), or " fcntl ().)
452 This is bad: it means that a process can lose its locks on
457 when for some reason a library function decides to open, read,
458 and close the same file.
460 The threads in a process share locks.
462 a multithreaded program can't use record locking to ensure
463 that threads don't simultaneously access the same region of a file.
465 Open file description locks solve both of these problems.
466 .SS Open file description locks (non-POSIX)
467 Open file description locks are advisory byte-range locks whose operation is
468 in most respects identical to the traditional record locks described above.
469 This lock type is Linux-specific,
470 and available since Linux 3.15.
471 (There is a proposal with the Austin Group
472 .\" FIXME . Review progress into POSIX
473 .\" http://austingroupbugs.net/view.php?id=768
474 to include this lock type in the next revision of POSIX.1.)
475 For an explanation of open file descriptions, see
478 The principal difference between the two lock types
479 is that whereas traditional record locks
480 are associated with a process,
481 open file description locks are associated with the
482 open file description on which they are acquired,
483 much like locks acquired with
485 Consequently (and unlike traditional advisory record locks),
486 open file description locks are inherited across
492 and are only automatically released on the last close
493 of the open file description,
494 instead of being released on any close of the file.
496 Conflicting lock combinations
497 (i.e., a read lock and a write lock or two write locks)
498 where one lock is an open file description lock and the other
499 is a traditional record lock conflict
500 even when they are acquired by the same process on the same file descriptor.
502 Open file description locks placed via the same open file description
503 (i.e., via the same file descriptor,
504 or via a duplicate of the file descriptor created by
509 and so on) are always compatible:
510 if a new lock is placed on an already locked region,
511 then the existing lock is converted to the new lock type.
512 (Such conversions may result in splitting, shrinking, or coalescing with
513 an existing lock as discussed above.)
515 On the other hand, open file description locks may conflict with
516 each other when they are acquired via different open file descriptions.
517 Thus, the threads in a multithreaded program can use
518 open file description locks to synchronize access to a file region
519 by having each thread perform its own
521 on the file and applying locks via the resulting file descriptor.
523 As with traditional advisory locks, the third argument to
529 By contrast with traditional record locks, the
531 field of that structure must be set to zero
532 when using the operations described below.
534 The operations for working with open file description locks are analogous
535 to those used with traditional locks:
537 .BR F_OFD_SETLK " (\fIstruct flock *\fP)"
538 Acquire an open file description lock (when
544 or release an open file description lock (when
548 on the bytes specified by the
549 .IR l_whence ", " l_start ", and " l_len
552 If a conflicting lock is held by another process,
553 this call returns \-1 and sets
558 .BR F_OFD_SETLKW " (\fIstruct flock *\fP)"
561 but if a conflicting lock is held on the file, then wait for that lock to be
563 If a signal is caught while waiting, then the call is interrupted
564 and (after the signal handler has returned) returns immediately
565 (with return value \-1 and
572 .BR F_OFD_GETLK " (\fIstruct flock *\fP)"
573 On input to this call,
575 describes an open file description lock we would like to place on the file.
576 If the lock could be placed,
578 does not actually place it, but returns
584 and leaves the other fields of the structure unchanged.
585 If one or more incompatible locks would prevent this lock being placed,
586 then details about one of these locks are returned via
588 as described above for
591 In the current implementation,
592 .\" commit 57b65325fe34ec4c917bc4e555144b4a94d9e1f7
593 no deadlock detection is performed for open file description locks.
594 (This contrasts with process-associated record locks,
595 for which the kernel does perform deadlock detection.)
597 .SS Mandatory locking
599 the Linux implementation of mandatory locking is unreliable.
601 Because of these bugs,
602 and the fact that the feature is believed to be little used,
603 since Linux 4.5, mandatory locking has been made an optional feature,
604 governed by a configuration option
605 .RB ( CONFIG_MANDATORY_FILE_LOCKING ).
606 This feature is no longer supported at all in Linux 5.15 and above.
608 By default, both traditional (process-associated) and open file description
609 record locks are advisory.
610 Advisory locks are not enforced and are useful only between
611 cooperating processes.
613 Both lock types can also be mandatory.
614 Mandatory locks are enforced for all processes.
615 If a process tries to perform an incompatible access (e.g.,
619 on a file region that has an incompatible mandatory lock,
620 then the result depends upon whether the
622 flag is enabled for its open file description.
625 flag is not enabled, then
626 the system call is blocked until the lock is removed
627 or converted to a mode that is compatible with the access.
630 flag is enabled, then the system call fails with the error
633 To make use of mandatory locks, mandatory locking must be enabled
634 both on the filesystem that contains the file to be locked,
635 and on the file itself.
636 Mandatory locking is enabled on a filesystem
637 using the "\-o mand" option to
643 Mandatory locking is enabled on a file by disabling
644 group execute permission on the file and enabling the set-group-ID
650 Mandatory locking is not specified by POSIX.
651 Some other systems also support mandatory locking,
652 although the details of how to enable it vary across systems.
655 When an advisory lock is obtained on a networked filesystem such as
656 NFS it is possible that the lock might get lost.
657 This may happen due to administrative action on the server, or due to a
658 network partition (i.e., loss of network connectivity with the server)
659 which lasts long enough for the server to assume
660 that the client is no longer functioning.
662 When the filesystem determines that a lock has been lost, future
666 requests may fail with the error
668 This error will persist until the lock is removed or the file
669 descriptor is closed.
671 .\" commit ef1820f9be27b6ad158f433ab38002ab8131db4d
672 this happens at least for NFSv4 (including all minor versions).
674 Some versions of UNIX send a signal
676 in this circumstance.
677 Linux does not define this signal, and does not provide any
678 asynchronous notification of lost locks.
688 are used to manage I/O availability signals:
690 .BR F_GETOWN " (\fIvoid\fP)"
691 Return (as the function result)
692 the process ID or process group ID currently receiving
696 signals for events on file descriptor
698 Process IDs are returned as positive values;
699 process group IDs are returned as negative values (but see BUGS below).
703 .BR F_SETOWN " (\fIint\fP)"
704 Set the process ID or process group ID that will receive
708 signals for events on the file descriptor
710 The target process or process group ID is specified in
712 A process ID is specified as a positive value;
713 a process group ID is specified as a negative value.
714 Most commonly, the calling process specifies itself as the owner
720 As well as setting the file descriptor owner,
721 one must also enable generation of signals on the file descriptor.
722 This is done by using the
727 file status flag on the file descriptor.
730 signal is sent whenever input or output becomes possible
731 on the file descriptor.
735 operation can be used to obtain delivery of a signal other than
738 Sending a signal to the owner process (group) specified by
740 is subject to the same permissions checks as are described for
742 where the sending process is the one that employs
744 (but see BUGS below).
745 If this permission check fails, then the signal is
750 operation records the caller's credentials at the time of the
753 and it is these saved credentials that are used for the permission checks.
755 If the file descriptor
762 signals that are delivered when out-of-band
763 data arrives on that socket.
765 is sent in any situation where
767 would report the socket as having an "exceptional condition".)
768 .\" The following appears to be rubbish. It doesn't seem to
769 .\" be true according to the kernel source, and I can write
770 .\" a program that gets a terminal-generated SIGIO even though
771 .\" it is not the foreground process group of the terminal.
774 .\" If the file descriptor
776 .\" refers to a terminal device, then SIGIO
777 .\" signals are sent to the foreground process group of the terminal.
779 The following was true in Linux 2.6.x up to and including Linux 2.6.11:
782 If a nonzero value is given to
784 in a multithreaded process running with a threading library
785 that supports thread groups (e.g., NPTL),
786 then a positive value given to
788 has a different meaning:
789 .\" The relevant place in the (2.6) kernel source is the
790 .\" 'switch' in fs/fcntl.c::send_sigio_to_task() -- MTK, Apr 2005
791 instead of being a process ID identifying a whole process,
792 it is a thread ID identifying a specific thread within a process.
793 Consequently, it may be necessary to pass
799 to get sensible results when
802 (In current Linux threading implementations,
803 a main thread's thread ID is the same as its process ID.
804 This means that a single-threaded program can equally use
809 Note, however, that the statements in this paragraph do not apply
812 signal generated for out-of-band data on a socket:
813 this signal is always sent to either a process or a process group,
814 depending on the value given to
816 .\" send_sigurg()/send_sigurg_to_task() bypasses
817 .\" kill_fasync()/send_sigio()/send_sigio_to_task()
818 .\" to directly call send_group_sig_info()
819 .\" -- MTK, Apr 2005 (kernel 2.6.11)
822 The above behavior was accidentally dropped in Linux 2.6.12,
823 and won't be restored.
824 From Linux 2.6.32 onward, use
830 signals at a particular thread.
832 .BR F_GETOWN_EX " (\fIstruct f_owner_ex *\fP) (since Linux 2.6.32)"
833 Return the current file descriptor owner settings
834 as defined by a previous
837 The information is returned in the structure pointed to by
839 which has the following form:
852 field will have one of the values
859 field is a positive integer representing a thread ID, process ID,
865 .BR F_SETOWN_EX " (\fIstruct f_owner_ex *\fP) (since Linux 2.6.32)"
866 This operation performs a similar task to
868 It allows the caller to direct I/O availability signals
869 to a specific thread, process, or process group.
870 The caller specifies the target of signals via
872 which is a pointer to a
877 field has one of the following values, which define how
883 Send the signal to the thread whose thread ID
884 (the value returned by a call to
892 Send the signal to the process whose ID
897 Send the signal to the process group whose ID
900 (Note that, unlike with
902 a process group ID is specified as a positive value here.)
905 .BR F_GETSIG " (\fIvoid\fP)"
906 Return (as the function result)
907 the signal sent when input or output becomes possible.
908 A value of zero means
911 Any other value (including
914 signal sent instead, and in this case additional info is available to
915 the signal handler if installed with
920 .BR F_SETSIG " (\fIint\fP)"
921 Set the signal sent when input or output becomes possible
922 to the value given in
924 A value of zero means to send the default
927 Any other value (including
929 is the signal to send instead, and in this case additional info
930 is available to the signal handler if installed with
933 .\" The following was true only up until Linux 2.6.11:
935 .\" Additionally, passing a nonzero value to
937 .\" changes the signal recipient from a whole process to a specific thread
938 .\" within a process.
939 .\" See the description of
941 .\" for more details.
945 with a nonzero value, and setting
950 extra information about I/O events is passed to
956 field indicates the source is
960 field gives the file descriptor associated with the event.
962 there is no indication which file descriptors are pending, and you
963 should use the usual mechanisms
969 set etc.) to determine which file descriptors are available for I/O.
971 Note that the file descriptor provided in
973 is the one that was specified during the
976 This can lead to an unusual corner case.
977 If the file descriptor is duplicated
979 or similar), and the original file descriptor is closed,
980 then I/O events will continue to be generated, but the
982 field will contain the number of the now closed file descriptor.
984 By selecting a real time signal (value >=
986 multiple I/O events may be queued using the same signal numbers.
987 (Queuing is dependent on available memory.)
988 Extra information is available
991 is set for the signal handler, as above.
993 Note that Linux imposes a limit on the
994 number of real-time signals that may be queued to a
999 and if this limit is reached, then the kernel reverts to
1002 and this signal is delivered to the entire
1003 process rather than to a specific thread.
1004 .\" See fs/fcntl.c::send_sigio_to_task() (2.4/2.6) sources -- MTK, Apr 05
1006 Using these mechanisms, a program can implement fully asynchronous I/O
1015 is specific to BSD and Linux.
1020 specified in POSIX.1 is in conjunction with the use of the
1023 (POSIX does not specify the
1032 POSIX has asynchronous I/O and the
1034 structure to achieve similar things; these are also available
1035 in Linux as part of the GNU C Library (glibc).
1040 (Linux 2.4 onward) are used to establish a new lease,
1041 and retrieve the current lease, on the open file description
1042 referred to by the file descriptor
1044 A file lease provides a mechanism whereby the process holding
1045 the lease (the "lease holder") is notified (via delivery of a signal)
1046 when a process (the "lease breaker") tries to
1050 the file referred to by that file descriptor.
1052 .BR F_SETLEASE " (\fIint\fP)"
1053 Set or remove a file lease according to which of the following
1054 values is specified in the integer
1059 Take out a read lease.
1060 This will cause the calling process to be notified when
1061 the file is opened for writing or is truncated.
1062 .\" The following became true in Linux 2.6.10:
1063 .\" See the man-pages-2.09 Changelog for further info.
1064 A read lease can be placed only on a file descriptor that
1065 is opened read-only.
1068 Take out a write lease.
1069 This will cause the caller to be notified when
1070 the file is opened for reading or writing or is truncated.
1071 A write lease may be placed on a file only if there are no
1072 other open file descriptors for the file.
1075 Remove our lease from the file.
1078 Leases are associated with an open file description (see
1080 This means that duplicate file descriptors (created by, for example,
1084 refer to the same lease, and this lease may be modified
1085 or released using any of these descriptors.
1086 Furthermore, the lease is released by either an explicit
1088 operation on any of these duplicate file descriptors, or when all
1089 such file descriptors have been closed.
1091 Leases may be taken out only on regular files.
1092 An unprivileged process may take out a lease only on a file whose
1093 UID (owner) matches the filesystem UID of the process.
1096 capability may take out leases on arbitrary files.
1098 .BR F_GETLEASE " (\fIvoid\fP)"
1099 Indicates what type of lease is associated with the file descriptor
1102 .BR F_RDLCK ", " F_WRLCK ", or " F_UNLCK ,
1103 indicating, respectively, a read lease , a write lease, or no lease.
1107 When a process (the "lease breaker") performs an
1111 that conflicts with a lease established via
1113 the system call is blocked by the kernel and
1114 the kernel notifies the lease holder by sending it a signal
1117 The lease holder should respond to receipt of this signal by doing
1118 whatever cleanup is required in preparation for the file to be
1119 accessed by another process (e.g., flushing cached buffers) and
1120 then either remove or downgrade its lease.
1121 A lease is removed by performing an
1123 operation specifying
1127 If the lease holder currently holds a write lease on the file,
1128 and the lease breaker is opening the file for reading,
1129 then it is sufficient for the lease holder to downgrade
1130 the lease to a read lease.
1131 This is done by performing an
1133 operation specifying
1138 If the lease holder fails to downgrade or remove the lease within
1139 the number of seconds specified in
1140 .IR /proc/sys/fs/lease\-break\-time ,
1141 then the kernel forcibly removes or downgrades the lease holder's lease.
1143 Once a lease break has been initiated,
1145 returns the target lease type (either
1149 depending on what would be compatible with the lease breaker)
1150 until the lease holder voluntarily downgrades or removes the lease or
1151 the kernel forcibly does so after the lease break timer expires.
1153 Once the lease has been voluntarily or forcibly removed or downgraded,
1154 and assuming the lease breaker has not unblocked its system call,
1155 the kernel permits the lease breaker's system call to proceed.
1157 If the lease breaker's blocked
1161 is interrupted by a signal handler,
1162 then the system call fails with the error
1164 but the other steps still occur as described above.
1165 If the lease breaker is killed by a signal while blocked in
1169 then the other steps still occur as described above.
1170 If the lease breaker specifies the
1174 then the call immediately fails with the error
1176 but the other steps still occur as described above.
1178 The default signal used to notify the lease holder is
1180 but this can be changed using the
1186 operation is performed (even one specifying
1189 handler is established using
1191 then the handler will receive a
1193 structure as its second argument, and the
1195 field of this argument will hold the file descriptor of the leased file
1196 that has been accessed by another process.
1197 (This is useful if the caller holds leases against multiple files.)
1198 .SS File and directory change notification (dnotify)
1200 .BR F_NOTIFY " (\fIint\fP)"
1202 Provide notification when the directory referred to by
1204 or any of the files that it contains is changed.
1205 The events to be notified are specified in
1207 which is a bit mask specified by ORing together zero or more of
1238 into this directory).
1244 to another directory,
1248 A file was renamed within this directory
1252 The attributes of a file were changed
1261 (In order to obtain these definitions, the
1263 feature test macro must be defined before including
1267 Directory notifications are normally "one-shot", and the application
1268 must reregister to receive further notifications.
1273 then notification will remain in effect until explicitly removed.
1275 .\" The following does seem a poor API-design choice...
1278 requests is cumulative, with the events in
1280 being added to the set already monitored.
1281 To disable notification of all events, make an
1287 Notification occurs via delivery of a signal.
1288 The default signal is
1290 but this can be changed using the
1296 is one of the nonqueuing standard signals;
1297 switching to the use of a real-time signal means that
1298 multiple notifications can be queued to the process.)
1299 In the latter case, the signal handler receives a
1301 structure as its second argument (if the handler was
1306 field of this structure contains the file descriptor which
1307 generated the notification (useful when establishing notification
1308 on multiple directories).
1310 Especially when using
1312 a real time signal should be used for notification,
1313 so that multiple notifications can be queued.
1316 New applications should use the
1318 interface (available since Linux 2.6.13),
1319 which provides a much superior interface for obtaining notifications of
1323 .SS Changing the capacity of a pipe
1325 .BR F_SETPIPE_SZ " (\fIint\fP; since Linux 2.6.35)"
1326 Change the capacity of the pipe referred to by
1331 An unprivileged process can adjust the pipe capacity to any value
1332 between the system page size and the limit defined in
1333 .I /proc/sys/fs/pipe\-max\-size
1336 Attempts to set the pipe capacity below the page size are silently
1337 rounded up to the page size.
1338 Attempts by an unprivileged process to set the pipe capacity above the limit in
1339 .I /proc/sys/fs/pipe\-max\-size
1342 a privileged process
1343 .RB ( CAP_SYS_RESOURCE )
1344 can override the limit.
1346 When allocating the buffer for the pipe,
1347 the kernel may use a capacity larger than
1349 if that is convenient for the implementation.
1350 (In the current implementation,
1351 the allocation is the next higher power-of-two page-size multiple
1352 of the requested size.)
1353 The actual capacity (in bytes) that is set is returned as the function result.
1355 Attempting to set the pipe capacity smaller than the amount
1356 of buffer space currently used to store data produces the error
1359 Note that because of the way the pages of the pipe buffer
1360 are employed when data is written to the pipe,
1361 the number of bytes that can be written may be less than the nominal size,
1362 depending on the size of the writes.
1364 .BR F_GETPIPE_SZ " (\fIvoid\fP; since Linux 2.6.35)"
1365 Return (as the function result) the capacity of the pipe referred to by
1369 File seals limit the set of allowed operations on a given file.
1370 For each seal that is set on a file,
1371 a specific set of operations will fail with
1373 on this file from now on.
1374 The file is said to be sealed.
1375 The default set of seals depends on the type of the underlying
1376 file and filesystem.
1377 For an overview of file sealing, a discussion of its purpose,
1378 and some code examples, see
1379 .BR memfd_create (2).
1382 file seals can be applied only to a file descriptor returned by
1383 .BR memfd_create (2)
1385 .B MFD_ALLOW_SEALING
1387 On other filesystems, all
1389 operations that operate on seals will return
1392 Seals are a property of an inode.
1393 Thus, all open file descriptors referring to the same inode share
1394 the same set of seals.
1395 Furthermore, seals can never be removed, only added.
1397 .BR F_ADD_SEALS " (\fIint\fP; since Linux 3.17)"
1398 Add the seals given in the bit-mask argument
1400 to the set of seals of the inode referred to by the file descriptor
1402 Seals cannot be removed again.
1403 Once this call succeeds, the seals are enforced by the kernel immediately.
1404 If the current set of seals includes
1406 (see below), then this call will be rejected with
1408 Adding a seal that is already set is a no-op, in case
1411 In order to place a seal, the file descriptor
1415 .BR F_GET_SEALS " (\fIvoid\fP; since Linux 3.17)"
1416 Return (as the function result) the current set of seals
1417 of the inode referred to by
1419 If no seals are set, 0 is returned.
1420 If the file does not support sealing, \-1 is returned and
1425 The following seals are available:
1428 If this seal is set, any further call to
1432 fails with the error
1434 Therefore, this seal prevents any modifications to the set of seals itself.
1435 If the initial set of seals of a file includes
1437 then this effectively causes the set of seals to be constant and locked.
1440 If this seal is set, the file in question cannot be reduced in size.
1449 Those calls fail with
1451 if you try to shrink the file in question.
1452 Increasing the file size is still possible.
1455 If this seal is set, the size of the file in question cannot be increased.
1458 beyond the end of the file,
1463 These calls fail with
1465 if you use them to increase the file size.
1466 If you keep the size or shrink it, those calls still work as expected.
1469 If this seal is set, you cannot modify the contents of the file.
1470 Note that shrinking or growing the size of the file is
1471 still possible and allowed.
1472 .\" One or more other seals are typically used with F_SEAL_WRITE
1473 .\" because, given a file with the F_SEAL_WRITE seal set, then,
1474 .\" while it would no longer be possible to (say) write zeros into
1475 .\" the last 100 bytes of a file, it would still be possible
1476 .\" to (say) shrink the file by 100 bytes using ftruncate(), and
1477 .\" then increase the file size by 100 bytes, which would have
1478 .\" the effect of replacing the last hundred bytes by zeros.
1480 Thus, this seal is normally used in combination with one of the other seals.
1485 (only in combination with the
1486 .B FALLOC_FL_PUNCH_HOLE
1488 Those calls fail with
1490 if this seal is set.
1491 Furthermore, trying to create new shared, writable memory-mappings via
1498 operation to set the
1502 if any writable, shared mapping exists.
1503 Such mappings must be unmapped before you can add this seal.
1504 Furthermore, if there are any asynchronous I/O operations
1505 .RB ( io_submit (2))
1506 pending on the file,
1507 all outstanding writes will be discarded.
1509 .BR F_SEAL_FUTURE_WRITE " (since Linux 5.1)"
1510 The effect of this seal is similar to
1512 but the contents of the file can still be modified via
1513 shared writable mappings that were created prior to the seal being set.
1514 Any attempt to create a new writable mapping on the file via
1518 Likewise, an attempt to write to the file via
1524 one process can create a memory buffer that it can continue to modify
1525 while sharing that buffer on a "read-only" basis with other processes.
1527 .SS File read/write hints
1528 Write lifetime hints can be used to inform the kernel about the relative
1529 expected lifetime of writes on a given inode or
1530 via a particular open file description.
1533 for an explanation of open file descriptions.)
1534 In this context, the term "write lifetime" means
1535 the expected time the data will live on media, before
1536 being overwritten or erased.
1538 An application may use the different hint values specified below to
1539 separate writes into different write classes,
1540 so that multiple users or applications running on a single storage back-end
1541 can aggregate their I/O patterns in a consistent manner.
1542 However, there are no functional semantics implied by these flags,
1543 and different I/O classes can use the write lifetime hints
1544 in arbitrary ways, so long as the hints are used consistently.
1546 The following operations can be applied to the file descriptor,
1549 .BR F_GET_RW_HINT " (\fIuint64_t *\fP; since Linux 4.13)"
1550 Returns the value of the read/write hint associated with the underlying inode
1554 .BR F_SET_RW_HINT " (\fIuint64_t *\fP; since Linux 4.13)"
1555 Sets the read/write hint value associated with the
1556 underlying inode referred to by
1558 This hint persists until either it is explicitly modified or
1559 the underlying filesystem is unmounted.
1561 .BR F_GET_FILE_RW_HINT " (\fIuint64_t *\fP; since Linux 4.13)"
1562 Returns the value of the read/write hint associated with
1563 the open file description referred to by
1566 .BR F_SET_FILE_RW_HINT " (\fIuint64_t *\fP; since Linux 4.13)"
1567 Sets the read/write hint value associated with the open file description
1571 If an open file description has not been assigned a read/write hint,
1572 then it shall use the value assigned to the inode, if any.
1574 The following read/write
1575 hints are valid since Linux 4.13:
1577 .B RWH_WRITE_LIFE_NOT_SET
1578 No specific hint has been set.
1579 This is the default value.
1581 .B RWH_WRITE_LIFE_NONE
1582 No specific write lifetime is associated with this file or inode.
1584 .B RWH_WRITE_LIFE_SHORT
1585 Data written to this inode or via this open file description
1586 is expected to have a short lifetime.
1588 .B RWH_WRITE_LIFE_MEDIUM
1589 Data written to this inode or via this open file description
1590 is expected to have a lifetime longer than
1592 .BR RWH_WRITE_LIFE_SHORT .
1594 .B RWH_WRITE_LIFE_LONG
1595 Data written to this inode or via this open file description
1596 is expected to have a lifetime longer than
1598 .BR RWH_WRITE_LIFE_MEDIUM .
1600 .B RWH_WRITE_LIFE_EXTREME
1601 Data written to this inode or via this open file description
1602 is expected to have a lifetime longer than
1604 .BR RWH_WRITE_LIFE_LONG .
1606 All the write-specific hints are relative to each other,
1607 and no individual absolute meaning should be attributed to them.
1609 For a successful call, the return value depends on the operation:
1612 The new file descriptor.
1615 Value of file descriptor flags.
1618 Value of file status flags.
1621 Type of lease held on file descriptor.
1624 Value of file descriptor owner.
1627 Value of signal sent when read or write becomes possible, or zero
1638 A bit mask identifying the seals that have been set
1639 for the inode referred to by
1642 All other operations
1645 On error, \-1 is returned, and
1647 is set to indicate the error.
1650 .BR EACCES " or " EAGAIN
1651 Operation is prohibited by locks held by other processes.
1654 The operation is prohibited because the file has been memory-mapped by
1659 is not an open file descriptor
1667 and the file descriptor open mode doesn't match with the
1668 type of lock requested.
1674 and the new pipe capacity specified in
1676 is smaller than the amount of buffer space currently
1677 used to store data in the pipe.
1686 and there exists a writable, shared mapping on the file referred to by
1690 It was detected that the specified
1692 operation would cause a deadlock.
1696 is outside your accessible address space.
1704 and the operation was interrupted by a signal; see
1715 and the operation was interrupted by a signal before the lock was checked or
1717 Most likely when locking a remote file (e.g., locking over
1718 NFS), but can sometimes happen locally.
1721 The value specified in
1723 is not recognized by this kernel.
1731 includes an unrecognized sealing bit.
1739 and the filesystem containing the inode referred to by
1741 does not support sealing.
1749 is negative or is greater than the maximum allowable value
1750 (see the discussion of
1761 is not an allowable signal number.
1772 was not specified as zero.
1778 and the per-process limit on the number of open file descriptors
1782 Too many segment locks open, lock table is full, or a remote locking
1783 protocol failed (e.g., locking over NFS).
1791 does not refer to a directory.
1797 and the soft or hard user pipe limit has been reached; see
1801 Attempted to clear the
1803 flag on a file that has the append-only attribute set.
1811 was not open for writing
1812 or the current set of seals on the file already includes
1830 macro to obtain these definitions.)
1832 .\" SVr4 documents additional EIO, ENOLINK and EOVERFLOW error conditions.
1838 are Linux-specific (and one must define
1840 to obtain their definitions),
1841 but work is being done to have them included in the next version of POSIX.1.
1847 .\" FIXME . Once glibc adds support, add a note about FTM requirements
1849 SVr4, 4.3BSD, POSIX.1-2001.
1861 are specified in POSIX.1-2001.
1866 are specified in POSIX.1-2001.
1867 (To get their definitions, define either
1868 .\" .BR _BSD_SOURCE ,
1871 with the value 500 or greater, or
1873 with the value 200809L or greater.)
1876 is specified in POSIX.1-2008.
1877 (To get this definition, define
1879 with the value 200809L or greater, or
1881 with the value 700 or greater.)
1883 The errors returned by
1885 are different from those returned by
1891 system call was not designed to handle large file offsets
1897 system call was added in Linux 2.4.
1898 The newer system call employs a different structure for file locking,
1900 and corresponding operations,
1905 However, these details can be ignored by applications using glibc, whose
1907 wrapper function transparently employs the more recent system call
1908 where it is available.
1911 Since Linux 2.0, there is no interaction between the types of lock
1917 Several systems have more fields in
1919 such as, for example,
1921 (to identify the machine where the lock is held).
1922 .\" e.g., Solaris 8 documents this field in fcntl(2), and Irix 6.5
1923 .\" documents it in fcntl(5). mtk, May 2007
1924 .\" Also, FreeBSD documents it (Apr 2014).
1927 alone is not going to be very useful if the process holding the lock
1928 may live on a different machine;
1929 on Linux, while present on some architectures (such as MIPS32),
1930 this field is not used.
1934 system call was not designed to handle large file offsets
1940 system call was added in Linux 2.4.
1941 The newer system call employs a different structure for file locking,
1943 and corresponding operations,
1948 However, these details can be ignored by applications using glibc, whose
1950 wrapper function transparently employs the more recent system call
1951 where it is available.
1952 .SS Record locking and NFS
1953 Before Linux 3.12, if an NFSv4 client
1954 loses contact with the server for a period of time
1955 (defined as more than 90 seconds with no communication),
1957 .\" Neil Brown: With NFSv3 the failure mode is the reverse. If
1958 .\" the server loses contact with a client then any lock stays in place
1959 .\" indefinitely ("why can't I read my mail"... I remember it well).
1961 it might lose and regain a lock without ever being aware of the fact.
1962 (The period of time after which contact is assumed lost is known as
1963 the NFSv4 leasetime.
1964 On a Linux NFS server, this can be determined by looking at
1965 .IR /proc/fs/nfsd/nfsv4leasetime ,
1966 which expresses the period in seconds.
1967 The default value for this file is 90.)
1970 .\" Note that this is not a firm timeout. The server runs a job
1971 .\" periodically to clean out expired stateful objects, and it's likely
1972 .\" that there is some time (maybe even up to another whole lease period)
1973 .\" between when the timeout expires and the job actually runs. If the
1974 .\" client gets a RENEW in there within that window, its lease will be
1975 .\" renewed and its state preserved.
1977 This scenario potentially risks data corruption,
1978 since another process might acquire a lock in the intervening period
1979 and perform file I/O.
1982 .\" commit ef1820f9be27b6ad158f433ab38002ab8131db4d
1983 if an NFSv4 client loses contact with the server,
1984 any I/O to the file by a process which "thinks" it holds
1985 a lock will fail until that process closes and reopens the file.
1987 .IR nfs.recover_lost_locks ,
1988 can be set to 1 to obtain the pre-3.12 behavior,
1989 whereby the client will attempt to recover lost locks
1990 when contact is reestablished with the server.
1991 Because of the attendant risk of data corruption,
1992 .\" commit f6de7a39c181dfb8a2c534661a53c73afb3081cd
1993 this parameter defaults to 0 (disabled).
1996 It is not possible to use
1998 to change the state of the
2003 .\" FIXME . According to POSIX.1-2001, O_SYNC should also be modifiable
2004 .\" via fcntl(2), but currently Linux does not permit this
2005 .\" See http://bugzilla.kernel.org/show_bug.cgi?id=5994
2006 Attempts to change the state of these flags are silently ignored.
2008 A limitation of the Linux system call conventions on some
2009 architectures (notably i386) means that if a (negative)
2010 process group ID to be returned by
2012 falls in the range \-1 to \-4095, then the return value is wrongly
2013 interpreted by glibc as an error in the system call;
2014 .\" glibc source: sysdeps/unix/sysv/linux/i386/sysdep.h
2015 that is, the return value of
2019 will contain the (positive) process group ID.
2022 operation avoids this problem.
2023 .\" mtk, Dec 04: some limited testing on alpha and ia64 seems to
2024 .\" indicate that ANY negative PGID value will cause F_GETOWN
2025 .\" to misinterpret the return as an error. Some other architectures
2026 .\" seem to have the same range check as i386.
2027 Since glibc 2.11, glibc makes the kernel
2029 problem invisible by implementing
2034 In Linux 2.4 and earlier, there is bug that can occur
2035 when an unprivileged process uses
2037 to specify the owner
2038 of a socket file descriptor
2039 as a process (group) other than the caller.
2046 even when the owner process (group) is one that the caller
2047 has permission to send signals to.
2048 Despite this error return, the file descriptor owner is set,
2049 and signals will be sent to the owner.
2051 .SS Deadlock detection
2052 The deadlock-detection algorithm employed by the kernel when dealing with
2054 requests can yield both
2055 false negatives (failures to detect deadlocks,
2056 leaving a set of deadlocked processes blocked indefinitely)
2059 errors when there is no deadlock).
2061 the kernel limits the lock depth of its dependency search to 10 steps,
2062 meaning that circular deadlock chains that exceed
2063 that size will not be detected.
2064 In addition, the kernel may falsely indicate a deadlock
2065 when two or more processes created using the
2068 flag place locks that appear (to the kernel) to conflict.
2070 .SS Mandatory locking
2071 The Linux implementation of mandatory locking
2072 is subject to race conditions which render it unreliable:
2073 .\" http://marc.info/?l=linux-kernel&m=119013491707153&w=2
2075 .\" Reconfirmed by Jeff Layton
2076 .\" From: Jeff Layton <jlayton <at> redhat.com>
2077 .\" Subject: Re: Status of fcntl() mandatory locking
2078 .\" Newsgroups: gmane.linux.file-systems
2079 .\" Date: 2014-04-28 10:07:57 GMT
2080 .\" http://thread.gmane.org/gmane.linux.file-systems/84481/focus=84518
2083 call that overlaps with a lock may modify data after the mandatory lock is
2087 call that overlaps with a lock may detect changes to data that were made
2088 only after a write lock was acquired.
2089 Similar races exist between mandatory locks and
2091 It is therefore inadvisable to rely on mandatory locking.
2098 .BR capabilities (7),
2099 .BR feature_test_macros (7),
2103 .IR mandatory\-locking.txt ,
2106 in the Linux kernel source directory
2107 .I Documentation/filesystems/
2108 (on older kernels, these files are directly under the
2111 .I mandatory\-locking.txt