2 .\" This manpage is Copyright (C) 1992 Drew Eckhardt;
3 .\" and Copyright (C) 1993 Michael Haardt, Ian Jackson;
4 .\" and Copyright (C) 1998 Jamie Lokier;
5 .\" and Copyright (C) 2002-2010, 2014 Michael Kerrisk;
6 .\" and Copyright (C) 2014 Jeff Layton
7 .\" and Copyright (C) 2014 David Herrmann
8 .\" and Copyright (C) 2017 Jens Axboe
10 .\" %%%LICENSE_START(VERBATIM)
11 .\" Permission is granted to make and distribute verbatim copies of this
12 .\" manual provided the copyright notice and this permission notice are
13 .\" preserved on all copies.
15 .\" Permission is granted to copy and distribute modified versions of this
16 .\" manual under the conditions for verbatim copying, provided that the
17 .\" entire resulting derived work is distributed under the terms of a
18 .\" permission notice identical to this one.
20 .\" Since the Linux kernel and libraries are constantly changing, this
21 .\" manual page may be incorrect or out-of-date. The author(s) assume no
22 .\" responsibility for errors or omissions, or for damages resulting from
23 .\" the use of the information contained herein. The author(s) may not
24 .\" have taken the same level of care in the production of this manual,
25 .\" which is licensed free of charge, as they might when working
28 .\" Formatted or processed versions of this manual, if unaccompanied by
29 .\" the source, must acknowledge the copyright and authors of this work.
32 .\" Modified 1993-07-24 by Rik Faith <faith@cs.unc.edu>
33 .\" Modified 1995-09-26 by Andries Brouwer <aeb@cwi.nl>
34 .\" and again on 960413 and 980804 and 981223.
35 .\" Modified 1998-12-11 by Jamie Lokier <jamie@imbolc.ucc.ie>
36 .\" Applied correction by Christian Ehrhardt - aeb, 990712
37 .\" Modified 2002-04-23 by Michael Kerrisk <mtk.manpages@gmail.com>
38 .\" Added note on F_SETFL and O_DIRECT
39 .\" Complete rewrite + expansion of material on file locking
40 .\" Incorporated description of F_NOTIFY, drawing on
41 .\" Stephen Rothwell's notes in Documentation/dnotify.txt.
42 .\" Added description of F_SETLEASE and F_GETLEASE
43 .\" Corrected and polished, aeb, 020527.
44 .\" Modified 2004-03-03 by Michael Kerrisk <mtk.manpages@gmail.com>
45 .\" Modified description of file leases: fixed some errors of detail
46 .\" Replaced the term "lease contestant" by "lease breaker"
47 .\" Modified, 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com>
48 .\" Added notes on capability requirements
49 .\" Modified 2004-12-08, added O_NOATIME after note from Martin Pool
50 .\" 2004-12-10, mtk, noted F_GETOWN bug after suggestion from aeb.
51 .\" 2005-04-08 Jamie Lokier <jamie@shareable.org>, mtk
52 .\" Described behavior of F_SETOWN/F_SETSIG in
53 .\" multithreaded processes, and generally cleaned
54 .\" up the discussion of F_SETOWN.
55 .\" 2005-05-20, Johannes Nicolai <johannes.nicolai@hpi.uni-potsdam.de>,
56 .\" mtk: Noted F_SETOWN bug for socket file descriptor in Linux 2.4
57 .\" and earlier. Added text on permissions required to send signal.
58 .\" 2009-09-30, Michael Kerrisk
59 .\" Note obsolete F_SETOWN behavior with threads.
60 .\" Document F_SETOWN_EX and F_GETOWN_EX
61 .\" 2010-06-17, Michael Kerrisk
62 .\" Document F_SETPIPE_SZ and F_GETPIPE_SZ.
63 .\" 2014-07-08, David Herrmann <dh.herrmann@gmail.com>
64 .\" Document F_ADD_SEALS and F_GET_SEALS
65 .\" 2017-06-26, Jens Axboe <axboe@kernel.dk>
66 .\" Document F_{GET,SET}_RW_HINT and F_{GET,SET}_FILE_RW_HINT
68 .TH FCNTL 2 2017-09-15 "Linux" "Linux Programmer's Manual"
70 fcntl \- manipulate file descriptor
73 .B #include <unistd.h>
76 .BI "int fcntl(int " fd ", int " cmd ", ... /* " arg " */ );"
80 performs one of the operations described below on the open file descriptor
82 The operation is determined by
86 can take an optional third argument.
87 Whether or not this argument is required is determined by
89 The required argument type is indicated in parentheses after each
91 name (in most cases, the required type is
93 and we identify the argument using the name
97 is specified if the argument is not required.
99 Certain of the operations below are supported only since a particular
100 Linux kernel version.
101 The preferred method of checking whether the host kernel supports
102 a particular operation is to invoke
106 value and then test whether the call failed with
108 indicating that the kernel does not recognize this value.
109 .SS Duplicating a file descriptor
111 .BR F_DUPFD " (\fIint\fP)"
112 Duplicate the file descriptor
114 using the lowest-numbered available file descriptor greater than or equal to
116 This is different from
118 which uses exactly the file descriptor specified.
120 On success, the new file descriptor is returned.
126 .BR F_DUPFD_CLOEXEC " (\fIint\fP; since Linux 2.6.24)"
129 but additionally set the
130 close-on-exec flag for the duplicate file descriptor.
131 Specifying this flag permits a program to avoid an additional
137 For an explanation of why this flag is useful,
138 see the description of
142 .SS File descriptor flags
143 The following commands manipulate the flags associated with
145 Currently, only one such flag is defined:
147 the close-on-exec flag.
151 the file descriptor will automatically be closed during a successful
155 fails, the file descriptor is left open.)
158 bit is not set, the file descriptor will remain open across an
161 .BR F_GETFD " (\fIvoid\fP)"
162 Return (as the function result) the file descriptor flags;
166 .BR F_SETFD " (\fIint\fP)"
167 Set the file descriptor flags to the value specified by
170 In multithreaded programs, using
173 to set the close-on-exec flag at the same time as another thread performs a
177 is vulnerable to a race condition that may unintentionally leak
178 the file descriptor to the program executed in the child process.
179 See the discussion of the
183 for details and a remedy to the problem.
184 .SS File status flags
185 Each open file description has certain associated status flags,
190 and possibly modified by
192 Duplicated file descriptors
197 etc.) refer to the same open file description, and thus
198 share the same file status flags.
200 The file status flags and their semantics are described in
203 .BR F_GETFL " (\fIvoid\fP)"
204 Return (as the function result)
205 the file access mode and the file status flags;
209 .BR F_SETFL " (\fIint\fP)"
210 Set the file status flags to the value specified by
213 .RB ( O_RDONLY ", " O_WRONLY ", " O_RDWR )
214 and file creation flags
216 .BR O_CREAT ", " O_EXCL ", " O_NOCTTY ", " O_TRUNC )
220 On Linux, this command can change only the
228 It is not possible to change the
232 flags; see BUGS, below.
233 .SS Advisory record locking
234 Linux implements traditional ("process-associated") UNIX record locks,
235 as standardized by POSIX.
236 For a Linux-specific alternative with better semantics,
237 see the discussion of open file description locks below.
243 are used to acquire, release, and test for the existence of record
244 locks (also known as byte-range, file-segment, or file-region locks).
247 is a pointer to a structure that has at least the following fields
248 (in unspecified order).
254 short l_type; /* Type of lock: F_RDLCK,
256 short l_whence; /* How to interpret l_start:
257 SEEK_SET, SEEK_CUR, SEEK_END */
258 off_t l_start; /* Starting offset for lock */
259 off_t l_len; /* Number of bytes to lock */
260 pid_t l_pid; /* PID of process blocking our lock
261 (set by F_GETLK and F_OFD_GETLK) */
268 .IR l_whence ", " l_start ", and " l_len
269 fields of this structure specify the range of bytes we wish to lock.
270 Bytes past the end of the file may be locked,
271 but not bytes before the start of the file.
274 is the starting offset for the lock, and is interpreted
276 the start of the file (if
280 the current file offset (if
284 or the end of the file (if
288 In the final two cases,
290 can be a negative number provided the
291 offset does not lie before the start of the file.
294 specifies the number of bytes to be locked.
297 is positive, then the range to be locked covers bytes
300 .IR l_start + l_len \-1.
303 has the special meaning: lock all bytes starting at the
304 location specified by
305 .IR l_whence " and " l_start
306 through to the end of file, no matter how large the file grows.
308 POSIX.1-2001 allows (but does not require)
309 an implementation to support a negative
313 is negative, the interval described by
319 This is supported by Linux since kernel versions 2.4.21 and 2.5.49.
323 field can be used to place a read
328 Any number of processes may hold a read lock (shared lock)
329 on a file region, but only one process may hold a write lock
331 An exclusive lock excludes all other locks,
332 both shared and exclusive.
333 A single process can hold only one type of lock on a file region;
334 if a new lock is applied to an already-locked region,
335 then the existing lock is converted to the new lock type.
336 (Such conversions may involve splitting, shrinking, or coalescing with
337 an existing lock if the byte range specified by the new lock does not
338 precisely coincide with the range of the existing lock.)
340 .BR F_SETLK " (\fIstruct flock *\fP)"
347 or release a lock (when
351 on the bytes specified by the
352 .IR l_whence ", " l_start ", and " l_len
355 If a conflicting lock is held by another process,
356 this call returns \-1 and sets
362 (The error returned in this case differs across implementations,
363 so POSIX requires a portable application to check for both errors.)
365 .BR F_SETLKW " (\fIstruct flock *\fP)"
368 but if a conflicting lock is held on the file, then wait for that
370 If a signal is caught while waiting, then the call is interrupted
371 and (after the signal handler has returned)
372 returns immediately (with return value \-1 and
379 .BR F_GETLK " (\fIstruct flock *\fP)"
380 On input to this call,
382 describes a lock we would like to place on the file.
383 If the lock could be placed,
385 does not actually place it, but returns
391 and leaves the other fields of the structure unchanged.
393 If one or more incompatible locks would prevent
394 this lock being placed, then
396 returns details about one of those locks in the
397 .IR l_type ", " l_whence ", " l_start ", and " l_len
400 If the conflicting lock is a traditional (process-associated) record lock,
403 field is set to the PID of the process holding that lock.
404 If the conflicting lock is an open file description lock, then
407 Note that the returned information
408 may already be out of date by the time the caller inspects it.
410 In order to place a read lock,
412 must be open for reading.
413 In order to place a write lock,
415 must be open for writing.
416 To place both types of lock, open a file read-write.
418 When placing locks with
422 whereby two or more processes have their
423 lock requests mutually blocked by locks held by the other processes.
424 For example, suppose process A holds a write lock on byte 100 of a file,
425 and process B holds a write lock on byte 200.
426 If each process then attempts to lock the byte already
427 locked by the other process using
429 then, without deadlock detection,
430 both processes would remain blocked indefinitely.
431 When the kernel detects such deadlocks,
432 it causes one of the blocking lock requests to immediately fail with the error
434 an application that encounters such an error should release
435 some of its locks to allow other applications to proceed before
436 attempting regain the locks that it requires.
437 Circular deadlocks involving more than two processes are also detected.
438 Note, however, that there are limitations to the kernel's
439 deadlock-detection algorithm; see BUGS.
441 As well as being removed by an explicit
443 record locks are automatically released when the process terminates.
445 Record locks are not inherited by a child created via
447 but are preserved across an
450 Because of the buffering performed by the
452 library, the use of record locking with routines in that package
453 should be avoided; use
459 The record locks described above are associated with the process
460 (unlike the open file description locks described below).
461 This has some unfortunate consequences:
465 file descriptor referring to a file,
466 then all of the process's locks on that file are released,
467 regardless of the file descriptor(s) on which the locks were obtained.
468 .\" (Additional file descriptors referring to the same file
469 .\" may have been obtained by calls to
470 .\" .BR open "(2), " dup "(2), " dup2 "(2), or " fcntl ().)
471 This is bad: it means that a process can lose its locks on
476 when for some reason a library function decides to open, read,
477 and close the same file.
479 The threads in a process share locks.
481 a multithreaded program can't use record locking to ensure
482 that threads don't simultaneously access the same region of a file.
484 Open file description locks solve both of these problems.
485 .SS Open file description locks (non-POSIX)
486 Open file description locks are advisory byte-range locks whose operation is
487 in most respects identical to the traditional record locks described above.
488 This lock type is Linux-specific,
489 and available since Linux 3.15.
490 (There is a proposal with the Austin Group
491 .\" FIXME . Review progress into POSIX
492 .\" http://austingroupbugs.net/view.php?id=768
493 to include this lock type in the next revision of POSIX.1.)
494 For an explanation of open file descriptions, see
497 The principal difference between the two lock types
498 is that whereas traditional record locks
499 are associated with a process,
500 open file description locks are associated with the
501 open file description on which they are acquired,
502 much like locks acquired with
504 Consequently (and unlike traditional advisory record locks),
505 open file description locks are inherited across
511 and are only automatically released on the last close
512 of the open file description,
513 instead of being released on any close of the file.
515 Conflicting lock combinations
516 (i.e., a read lock and a write lock or two write locks)
517 where one lock is an open file description lock and the other
518 is a traditional record lock conflict
519 even when they are acquired by the same process on the same file descriptor.
521 Open file description locks placed via the same open file description
522 (i.e., via the same file descriptor,
523 or via a duplicate of the file descriptor created by
528 and so on) are always compatible:
529 if a new lock is placed on an already locked region,
530 then the existing lock is converted to the new lock type.
531 (Such conversions may result in splitting, shrinking, or coalescing with
532 an existing lock as discussed above.)
534 On the other hand, open file description locks may conflict with
535 each other when they are acquired via different open file descriptions.
536 Thus, the threads in a multithreaded program can use
537 open file description locks to synchronize access to a file region
538 by having each thread perform its own
540 on the file and applying locks via the resulting file descriptor.
542 As with traditional advisory locks, the third argument to
548 By contrast with traditional record locks, the
550 field of that structure must be set to zero
551 when using the commands described below.
553 The commands for working with open file description locks are analogous
554 to those used with traditional locks:
556 .BR F_OFD_SETLK " (\fIstruct flock *\fP)"
557 Acquire an open file description lock (when
563 or release an open file description lock (when
567 on the bytes specified by the
568 .IR l_whence ", " l_start ", and " l_len
571 If a conflicting lock is held by another process,
572 this call returns \-1 and sets
577 .BR F_OFD_SETLKW " (\fIstruct flock *\fP)"
580 but if a conflicting lock is held on the file, then wait for that lock to be
582 If a signal is caught while waiting, then the call is interrupted
583 and (after the signal handler has returned) returns immediately
584 (with return value \-1 and
591 .BR F_OFD_GETLK " (\fIstruct flock *\fP)"
592 On input to this call,
594 describes an open file description lock we would like to place on the file.
595 If the lock could be placed,
597 does not actually place it, but returns
603 and leaves the other fields of the structure unchanged.
604 If one or more incompatible locks would prevent this lock being placed,
605 then details about one of these locks are returned via
607 as described above for
610 In the current implementation,
611 .\" commit 57b65325fe34ec4c917bc4e555144b4a94d9e1f7
612 no deadlock detection is performed for open file description locks.
613 (This contrasts with process-associated record locks,
614 for which the kernel does perform deadlock detection.)
616 .SS Mandatory locking
618 the Linux implementation of mandatory locking is unreliable.
620 Because of these bugs,
621 and the fact that the feature is believed to be little used,
622 since Linux 4.5, mandatory locking has been made an optional feature,
623 governed by a configuration option
624 .RB ( CONFIG_MANDATORY_FILE_LOCKING ).
625 This is an initial step toward removing this feature completely.
627 By default, both traditional (process-associated) and open file description
628 record locks are advisory.
629 Advisory locks are not enforced and are useful only between
630 cooperating processes.
632 Both lock types can also be mandatory.
633 Mandatory locks are enforced for all processes.
634 If a process tries to perform an incompatible access (e.g.,
638 on a file region that has an incompatible mandatory lock,
639 then the result depends upon whether the
641 flag is enabled for its open file description.
644 flag is not enabled, then
645 the system call is blocked until the lock is removed
646 or converted to a mode that is compatible with the access.
649 flag is enabled, then the system call fails with the error
652 To make use of mandatory locks, mandatory locking must be enabled
653 both on the filesystem that contains the file to be locked,
654 and on the file itself.
655 Mandatory locking is enabled on a filesystem
656 using the "\-o mand" option to
662 Mandatory locking is enabled on a file by disabling
663 group execute permission on the file and enabling the set-group-ID
669 Mandatory locking is not specified by POSIX.
670 Some other systems also support mandatory locking,
671 although the details of how to enable it vary across systems.
680 are used to manage I/O availability signals:
682 .BR F_GETOWN " (\fIvoid\fP)"
683 Return (as the function result)
684 the process ID or process group currently receiving
688 signals for events on file descriptor
690 Process IDs are returned as positive values;
691 process group IDs are returned as negative values (but see BUGS below).
695 .BR F_SETOWN " (\fIint\fP)"
696 Set the process ID or process group ID that will receive
700 signals for events on the file descriptor
702 The target process or process group ID is specified in
704 A process ID is specified as a positive value;
705 a process group ID is specified as a negative value.
706 Most commonly, the calling process specifies itself as the owner
712 As well as setting the file descriptor owner,
713 one must also enable generation of signals on the file descriptor.
714 This is done by using the
719 file status flag on the file descriptor.
722 signal is sent whenever input or output becomes possible
723 on the file descriptor.
727 command can be used to obtain delivery of a signal other than
730 Sending a signal to the owner process (group) specified by
732 is subject to the same permissions checks as are described for
734 where the sending process is the one that employs
736 (but see BUGS below).
737 If this permission check fails, then the signal is
742 operation records the caller's credentials at the time of the
745 and it is these saved credentials that are used for the permission checks.
747 If the file descriptor
754 signals that are delivered when out-of-band
755 data arrives on that socket.
757 is sent in any situation where
759 would report the socket as having an "exceptional condition".)
760 .\" The following appears to be rubbish. It doesn't seem to
761 .\" be true according to the kernel source, and I can write
762 .\" a program that gets a terminal-generated SIGIO even though
763 .\" it is not the foreground process group of the terminal.
766 .\" If the file descriptor
768 .\" refers to a terminal device, then SIGIO
769 .\" signals are sent to the foreground process group of the terminal.
771 The following was true in 2.6.x kernels up to and including
775 If a nonzero value is given to
777 in a multithreaded process running with a threading library
778 that supports thread groups (e.g., NPTL),
779 then a positive value given to
781 has a different meaning:
782 .\" The relevant place in the (2.6) kernel source is the
783 .\" 'switch' in fs/fcntl.c::send_sigio_to_task() -- MTK, Apr 2005
784 instead of being a process ID identifying a whole process,
785 it is a thread ID identifying a specific thread within a process.
786 Consequently, it may be necessary to pass
792 to get sensible results when
795 (In current Linux threading implementations,
796 a main thread's thread ID is the same as its process ID.
797 This means that a single-threaded program can equally use
802 Note, however, that the statements in this paragraph do not apply
805 signal generated for out-of-band data on a socket:
806 this signal is always sent to either a process or a process group,
807 depending on the value given to
809 .\" send_sigurg()/send_sigurg_to_task() bypasses
810 .\" kill_fasync()/send_sigio()/send_sigio_to_task()
811 .\" to directly call send_group_sig_info()
812 .\" -- MTK, Apr 2005 (kernel 2.6.11)
815 The above behavior was accidentally dropped in Linux 2.6.12,
816 and won't be restored.
817 From Linux 2.6.32 onward, use
823 signals at a particular thread.
825 .BR F_GETOWN_EX " (\fIstruct f_owner_ex *\fP) (since Linux 2.6.32)"
826 Return the current file descriptor owner settings
827 as defined by a previous
830 The information is returned in the structure pointed to by
832 which has the following form:
845 field will have one of the values
852 field is a positive integer representing a thread ID, process ID,
858 .BR F_SETOWN_EX " (\fIstruct f_owner_ex *\fP) (since Linux 2.6.32)"
859 This operation performs a similar task to
861 It allows the caller to direct I/O availability signals
862 to a specific thread, process, or process group.
863 The caller specifies the target of signals via
865 which is a pointer to a
870 field has one of the following values, which define how
876 Send the signal to the thread whose thread ID
877 (the value returned by a call to
885 Send the signal to the process whose ID
890 Send the signal to the process group whose ID
893 (Note that, unlike with
895 a process group ID is specified as a positive value here.)
898 .BR F_GETSIG " (\fIvoid\fP)"
899 Return (as the function result)
900 the signal sent when input or output becomes possible.
901 A value of zero means
904 Any other value (including
907 signal sent instead, and in this case additional info is available to
908 the signal handler if installed with
913 .BR F_SETSIG " (\fIint\fP)"
914 Set the signal sent when input or output becomes possible
915 to the value given in
917 A value of zero means to send the default
920 Any other value (including
922 is the signal to send instead, and in this case additional info
923 is available to the signal handler if installed with
926 .\" The following was true only up until 2.6.11:
928 .\" Additionally, passing a nonzero value to
930 .\" changes the signal recipient from a whole process to a specific thread
931 .\" within a process.
932 .\" See the description of
934 .\" for more details.
938 with a nonzero value, and setting
943 extra information about I/O events is passed to
949 field indicates the source is
953 field gives the file descriptor associated with the event.
955 there is no indication which file descriptors are pending, and you
956 should use the usual mechanisms
962 set etc.) to determine which file descriptors are available for I/O.
964 Note that the file descriptor provided in
966 is the one that was specified during the
969 This can lead to an unusual corner case.
970 If the file descriptor is duplicated
972 or similar), and the original file descriptor is closed,
973 then I/O events will continue to be generated, but the
975 field will contain the number of the now closed file descriptor.
977 By selecting a real time signal (value >=
979 multiple I/O events may be queued using the same signal numbers.
980 (Queuing is dependent on available memory.)
981 Extra information is available
984 is set for the signal handler, as above.
986 Note that Linux imposes a limit on the
987 number of real-time signals that may be queued to a
992 and if this limit is reached, then the kernel reverts to
995 and this signal is delivered to the entire
996 process rather than to a specific thread.
997 .\" See fs/fcntl.c::send_sigio_to_task() (2.4/2.6) sources -- MTK, Apr 05
999 Using these mechanisms, a program can implement fully asynchronous I/O
1008 is specific to BSD and Linux.
1013 specified in POSIX.1 is in conjunction with the use of the
1016 (POSIX does not specify the
1025 POSIX has asynchronous I/O and the
1027 structure to achieve similar things; these are also available
1028 in Linux as part of the GNU C Library (Glibc).
1033 (Linux 2.4 onward) are used (respectively) to establish a new lease,
1034 and retrieve the current lease, on the open file description
1035 referred to by the file descriptor
1037 A file lease provides a mechanism whereby the process holding
1038 the lease (the "lease holder") is notified (via delivery of a signal)
1039 when a process (the "lease breaker") tries to
1043 the file referred to by that file descriptor.
1045 .BR F_SETLEASE " (\fIint\fP)"
1046 Set or remove a file lease according to which of the following
1047 values is specified in the integer
1052 Take out a read lease.
1053 This will cause the calling process to be notified when
1054 the file is opened for writing or is truncated.
1055 .\" The following became true in kernel 2.6.10:
1056 .\" See the man-pages-2.09 Changelog for further info.
1057 A read lease can be placed only on a file descriptor that
1058 is opened read-only.
1061 Take out a write lease.
1062 This will cause the caller to be notified when
1063 the file is opened for reading or writing or is truncated.
1064 A write lease may be placed on a file only if there are no
1065 other open file descriptors for the file.
1068 Remove our lease from the file.
1071 Leases are associated with an open file description (see
1073 This means that duplicate file descriptors (created by, for example,
1077 refer to the same lease, and this lease may be modified
1078 or released using any of these descriptors.
1079 Furthermore, the lease is released by either an explicit
1081 operation on any of these duplicate file descriptors, or when all
1082 such file descriptors have been closed.
1084 Leases may be taken out only on regular files.
1085 An unprivileged process may take out a lease only on a file whose
1086 UID (owner) matches the filesystem UID of the process.
1089 capability may take out leases on arbitrary files.
1091 .BR F_GETLEASE " (\fIvoid\fP)"
1092 Indicates what type of lease is associated with the file descriptor
1095 .BR F_RDLCK ", " F_WRLCK ", or " F_UNLCK ,
1096 indicating, respectively, a read lease , a write lease, or no lease.
1100 When a process (the "lease breaker") performs an
1104 that conflicts with a lease established via
1106 the system call is blocked by the kernel and
1107 the kernel notifies the lease holder by sending it a signal
1110 The lease holder should respond to receipt of this signal by doing
1111 whatever cleanup is required in preparation for the file to be
1112 accessed by another process (e.g., flushing cached buffers) and
1113 then either remove or downgrade its lease.
1114 A lease is removed by performing an
1120 If the lease holder currently holds a write lease on the file,
1121 and the lease breaker is opening the file for reading,
1122 then it is sufficient for the lease holder to downgrade
1123 the lease to a read lease.
1124 This is done by performing an
1131 If the lease holder fails to downgrade or remove the lease within
1132 the number of seconds specified in
1133 .IR /proc/sys/fs/lease-break-time ,
1134 then the kernel forcibly removes or downgrades the lease holder's lease.
1136 Once a lease break has been initiated,
1138 returns the target lease type (either
1142 depending on what would be compatible with the lease breaker)
1143 until the lease holder voluntarily downgrades or removes the lease or
1144 the kernel forcibly does so after the lease break timer expires.
1146 Once the lease has been voluntarily or forcibly removed or downgraded,
1147 and assuming the lease breaker has not unblocked its system call,
1148 the kernel permits the lease breaker's system call to proceed.
1150 If the lease breaker's blocked
1154 is interrupted by a signal handler,
1155 then the system call fails with the error
1157 but the other steps still occur as described above.
1158 If the lease breaker is killed by a signal while blocked in
1162 then the other steps still occur as described above.
1163 If the lease breaker specifies the
1167 then the call immediately fails with the error
1169 but the other steps still occur as described above.
1171 The default signal used to notify the lease holder is
1173 but this can be changed using the
1179 command is performed (even one specifying
1182 handler is established using
1184 then the handler will receive a
1186 structure as its second argument, and the
1188 field of this argument will hold the file descriptor of the leased file
1189 that has been accessed by another process.
1190 (This is useful if the caller holds leases against multiple files.)
1191 .SS File and directory change notification (dnotify)
1193 .BR F_NOTIFY " (\fIint\fP)"
1195 Provide notification when the directory referred to by
1197 or any of the files that it contains is changed.
1198 The events to be notified are specified in
1200 which is a bit mask specified by ORing together zero or more of
1231 into this directory).
1237 to another directory,
1241 A file was renamed within this directory
1245 The attributes of a file were changed
1254 (In order to obtain these definitions, the
1256 feature test macro must be defined before including
1260 Directory notifications are normally "one-shot", and the application
1261 must reregister to receive further notifications.
1266 then notification will remain in effect until explicitly removed.
1268 .\" The following does seem a poor API-design choice...
1271 requests is cumulative, with the events in
1273 being added to the set already monitored.
1274 To disable notification of all events, make an
1280 Notification occurs via delivery of a signal.
1281 The default signal is
1283 but this can be changed using the
1289 is one of the nonqueuing standard signals;
1290 switching to the use of a real-time signal means that
1291 multiple notifications can be queued to the process.)
1292 In the latter case, the signal handler receives a
1294 structure as its second argument (if the handler was
1299 field of this structure contains the file descriptor which
1300 generated the notification (useful when establishing notification
1301 on multiple directories).
1303 Especially when using
1305 a real time signal should be used for notification,
1306 so that multiple notifications can be queued.
1309 New applications should use the
1311 interface (available since kernel 2.6.13),
1312 which provides a much superior interface for obtaining notifications of
1316 .SS Changing the capacity of a pipe
1318 .BR F_SETPIPE_SZ " (\fIint\fP; since Linux 2.6.35)"
1319 Change the capacity of the pipe referred to by
1324 An unprivileged process can adjust the pipe capacity to any value
1325 between the system page size and the limit defined in
1326 .IR /proc/sys/fs/pipe-max-size
1329 Attempts to set the pipe capacity below the page size are silently
1330 rounded up to the page size.
1331 Attempts by an unprivileged process to set the pipe capacity above the limit in
1332 .IR /proc/sys/fs/pipe-max-size
1335 a privileged process
1336 .RB ( CAP_SYS_RESOURCE )
1337 can override the limit.
1339 When allocating the buffer for the pipe,
1340 the kernel may use a capacity larger than
1342 if that is convenient for the implementation.
1343 (In the current implementation,
1344 the allocation is the next higher power-of-two page-size multiple
1345 of the requested size.)
1346 The actual capacity (in bytes) that is set is returned as the function result.
1348 Attempting to set the pipe capacity smaller than the amount
1349 of buffer space currently used to store data produces the error
1352 .BR F_GETPIPE_SZ " (\fIvoid\fP; since Linux 2.6.35)"
1353 Return (as the function result) the capacity of the pipe referred to by
1357 File seals limit the set of allowed operations on a given file.
1358 For each seal that is set on a file,
1359 a specific set of operations will fail with
1361 on this file from now on.
1362 The file is said to be sealed.
1363 The default set of seals depends on the type of the underlying
1364 file and filesystem.
1365 For an overview of file sealing, a discussion of its purpose,
1366 and some code examples, see
1367 .BR memfd_create (2).
1370 file seals can be applied only to a file descriptor returned by
1371 .BR memfd_create (2)
1373 .B MFD_ALLOW_SEALING
1375 On other filesystems, all
1377 operations that operate on seals will return
1380 Seals are a property of an inode.
1381 Thus, all open file descriptors referring to the same inode share
1382 the same set of seals.
1383 Furthermore, seals can never be removed, only added.
1385 .BR F_ADD_SEALS " (\fIint\fP; since Linux 3.17)"
1386 Add the seals given in the bit-mask argument
1388 to the set of seals of the inode referred to by the file descriptor
1390 Seals cannot be removed again.
1391 Once this call succeeds, the seals are enforced by the kernel immediately.
1392 If the current set of seals includes
1394 (see below), then this call will be rejected with
1396 Adding a seal that is already set is a no-op, in case
1399 In order to place a seal, the file descriptor
1403 .BR F_GET_SEALS " (\fIvoid\fP; since Linux 3.17)"
1404 Return (as the function result) the current set of seals
1405 of the inode referred to by
1407 If no seals are set, 0 is returned.
1408 If the file does not support sealing, \-1 is returned and
1413 The following seals are available:
1416 If this seal is set, any further call to
1422 Therefore, this seal prevents any modifications to the set of seals itself.
1423 If the initial set of seals of a file includes
1425 then this effectively causes the set of seals to be constant and locked.
1428 If this seal is set, the file in question cannot be reduced in size.
1437 Those calls fail with
1439 if you try to shrink the file in question.
1440 Increasing the file size is still possible.
1443 If this seal is set, the size of the file in question cannot be increased.
1446 beyond the end of the file,
1451 These calls fail with
1453 if you use them to increase the file size.
1454 If you keep the size or shrink it, those calls still work as expected.
1457 If this seal is set, you cannot modify the contents of the file.
1458 Note that shrinking or growing the size of the file is
1459 still possible and allowed.
1460 .\" One or more other seals are typically used with F_SEAL_WRITE
1461 .\" because, given a file with the F_SEAL_WRITE seal set, then,
1462 .\" while it would no longer be possible to (say) write zeros into
1463 .\" the last 100 bytes of a file, it would still be possible
1464 .\" to (say) shrink the file by 100 bytes using ftruncate(), and
1465 .\" then increase the file size by 100 bytes, which would have
1466 .\" the effect of replacing the last hundred bytes by zeros.
1468 Thus, this seal is normally used in combination with one of the other seals.
1473 (only in combination with the
1474 .B FALLOC_FL_PUNCH_HOLE
1476 Those calls fail with
1478 if this seal is set.
1479 Furthermore, trying to create new shared, writable memory-mappings via
1486 operation to set the
1490 if any writable, shared mapping exists.
1491 Such mappings must be unmapped before you can add this seal.
1492 Furthermore, if there are any asynchronous I/O operations
1493 .RB ( io_submit (2))
1494 pending on the file,
1495 all outstanding writes will be discarded.
1497 .SS File read/write hints
1498 Write lifetime hints can be used to inform the kernel about the relative
1499 expected lifetime of writes on a given inode or
1500 via a particular open file description.
1503 for an explanation of open file descriptions.)
1504 In this context, the term "write lifetime" means
1505 the expected time the data will live on media, before
1506 being overwritten or erased.
1508 An application may use the different hint values specified below to
1509 separate writes into different write classes,
1510 so that multiple users or applications running on a single storage back-end
1511 can aggregate their I/O patterns in a consistent manner.
1512 However, there are no functional semantics implied by these flags,
1513 and different I/O classes can use the write lifetime hints
1514 in arbitrary ways, so long as the hints are used consistently.
1516 The following operations can be applied to the file descriptor,
1519 .BR F_GET_RW_HINT " (\fIuint64_t *\fP; since Linux 4.13)"
1520 Returns the value of the read/write hint associated with the underlying inode
1524 .BR F_SET_RW_HINT " (\fIuint64_t *\fP; since Linux 4.13)"
1525 Sets the read/write hint value associated with the
1526 underlying inode referred to by
1528 This hint persists until either it is explicitly modified or
1529 the underlying filesystem is unmounted.
1531 .BR F_GET_FILE_RW_HINT " (\fIuint64_t *\fP; since Linux 4.13)"
1532 Returns the value of the read/write hint associated with
1533 the open file description referred to by
1536 .BR F_SET_FILE_RW_HINT " (\fIuint64_t *\fP; since Linux 4.13)"
1537 Sets the read/write hint value associated with the open file description
1541 If an open file description has not been assigned a read/write hint,
1542 then it shall use the value assigned to the inode, if any.
1544 The following read/write
1545 hints are valid since Linux 4.13:
1547 .BR RWH_WRITE_LIFE_NOT_SET
1548 No specific hint has been set.
1549 This is the default value.
1551 .BR RWH_WRITE_LIFE_NONE
1552 No specific write lifetime is associated with this file or inode.
1554 .BR RWH_WRITE_LIFE_SHORT
1555 Data written to this inode or via this open file description
1556 is expected to have a short lifetime.
1558 .BR RWH_WRITE_LIFE_MEDIUM
1559 Data written to this inode or via this open file description
1560 is expected to have a lifetime longer than
1562 .BR RWH_WRITE_LIFE_SHORT .
1564 .BR RWH_WRITE_LIFE_LONG
1565 Data written to this inode or via this open file description
1566 is expected to have a lifetime longer than
1568 .BR RWH_WRITE_LIFE_MEDIUM .
1570 .BR RWH_WRITE_LIFE_EXTREME
1571 Data written to this inode or via this open file description
1572 is expected to have a lifetime longer than
1574 .BR RWH_WRITE_LIFE_LONG .
1576 All the write-specific hints are relative to each other,
1577 and no individual absolute meaning should be attributed to them.
1579 For a successful call, the return value depends on the operation:
1582 The new file descriptor.
1585 Value of file descriptor flags.
1588 Value of file status flags.
1591 Type of lease held on file descriptor.
1594 Value of file descriptor owner.
1597 Value of signal sent when read or write becomes possible, or zero
1602 .BR F_GETPIPE_SZ ", " F_SETPIPE_SZ
1606 A bit mask identifying the seals that have been set
1607 for the inode referred to by
1613 On error, \-1 is returned, and
1615 is set appropriately.
1618 .BR EACCES " or " EAGAIN
1619 Operation is prohibited by locks held by other processes.
1622 The operation is prohibited because the file has been memory-mapped by
1627 is not an open file descriptor
1635 and the file descriptor open mode doesn't match with the
1636 type of lock requested.
1642 and the new pipe capacity specified in
1644 is smaller than the amount of buffer space currently
1645 used to store data in the pipe.
1654 and there exists a writable, shared mapping on the file referred to by
1658 It was detected that the specified
1660 command would cause a deadlock.
1664 is outside your accessible address space.
1672 and the operation was interrupted by a signal; see
1683 and the operation was interrupted by a signal before the lock was checked or
1685 Most likely when locking a remote file (e.g., locking over
1686 NFS), but can sometimes happen locally.
1689 The value specified in
1691 is not recognized by this kernel.
1699 includes an unrecognized sealing bit.
1707 and the filesystem containing the inode referred to by
1709 does not support sealing.
1717 is negative or is greater than the maximum allowable value
1718 (see the discussion of
1729 is not an allowable signal number.
1740 was not specified as zero.
1746 and the per-process limit on the number of open file descriptors
1750 Too many segment locks open, lock table is full, or a remote locking
1751 protocol failed (e.g., locking over NFS).
1759 does not refer to a directory.
1765 and the soft or hard user pipe limit has been reached; see
1769 Attempted to clear the
1771 flag on a file that has the append-only attribute set.
1779 was not open for writing
1780 or the current set of seals on the file already includes
1783 SVr4, 4.3BSD, POSIX.1-2001.
1794 are specified in POSIX.1-2001.
1799 are specified in POSIX.1-2001.
1800 (To get their definitions, define either
1801 .\" .BR _BSD_SOURCE ,
1804 with the value 500 or greater, or
1806 with the value 200809L or greater.)
1809 is specified in POSIX.1-2008.
1810 (To get this definition, define
1812 with the value 200809L or greater, or
1814 with the value 700 or greater.)
1829 macro to obtain these definitions.)
1831 .\" SVr4 documents additional EIO, ENOLINK and EOVERFLOW error conditions.
1837 are Linux-specific (and one must define
1839 to obtain their definitions),
1840 but work is being done to have them included in the next version of POSIX.1.
1846 .\" FIXME . Once glibc adds support, add a note about FTM requirements
1848 The errors returned by
1850 are different from those returned by
1856 system call was not designed to handle large file offsets
1862 system call was added in Linux 2.4.
1863 The newer system call employs a different structure for file locking,
1865 and corresponding commands,
1870 However, these details can be ignored by applications using glibc, whose
1872 wrapper function transparently employs the more recent system call
1873 where it is available.
1876 Since kernel 2.0, there is no interaction between the types of lock
1882 Several systems have more fields in
1884 such as, for example,
1886 .\" e.g., Solaris 8 documents this field in fcntl(2), and Irix 6.5
1887 .\" documents it in fcntl(5). mtk, May 2007
1888 .\" Also, FreeBSD documents it (Apr 2014).
1891 alone is not going to be very useful if the process holding the lock
1892 may live on a different machine.
1896 system call was not designed to handle large file offsets
1902 system call was added in Linux 2.4.
1903 The newer system call employs a different structure for file locking,
1905 and corresponding commands,
1910 However, these details can be ignored by applications using glibc, whose
1912 wrapper function transparently employs the more recent system call
1913 where it is available.
1914 .SS Record locking and NFS
1915 Before Linux 3.12, if an NFSv4 client
1916 loses contact with the server for a period of time
1917 (defined as more than 90 seconds with no communication),
1919 .\" Neil Brown: With NFSv3 the failure mode is the reverse. If
1920 .\" the server loses contact with a client then any lock stays in place
1921 .\" indefinitely ("why can't I read my mail"... I remember it well).
1923 it might lose and regain a lock without ever being aware of the fact.
1924 (The period of time after which contact is assumed lost is known as
1925 the NFSv4 leasetime.
1926 On a Linux NFS server, this can be determined by looking at
1927 .IR /proc/fs/nfsd/nfsv4leasetime ,
1928 which expresses the period in seconds.
1929 The default value for this file is 90.)
1932 .\" Note that this is not a firm timeout. The server runs a job
1933 .\" periodically to clean out expired stateful objects, and it's likely
1934 .\" that there is some time (maybe even up to another whole lease period)
1935 .\" between when the timeout expires and the job actually runs. If the
1936 .\" client gets a RENEW in there within that window, its lease will be
1937 .\" renewed and its state preserved.
1939 This scenario potentially risks data corruption,
1940 since another process might acquire a lock in the intervening period
1941 and perform file I/O.
1944 .\" commit ef1820f9be27b6ad158f433ab38002ab8131db4d
1945 if an NFSv4 client loses contact with the server,
1946 any I/O to the file by a process which "thinks" it holds
1947 a lock will fail until that process closes and reopens the file.
1949 .IR nfs.recover_lost_locks ,
1950 can be set to 1 to obtain the pre-3.12 behavior,
1951 whereby the client will attempt to recover lost locks
1952 when contact is reestablished with the server.
1953 Because of the attendant risk of data corruption,
1954 .\" commit f6de7a39c181dfb8a2c534661a53c73afb3081cd
1955 this parameter defaults to 0 (disabled).
1958 It is not possible to use
1960 to change the state of the
1965 .\" FIXME . According to POSIX.1-2001, O_SYNC should also be modifiable
1966 .\" via fcntl(2), but currently Linux does not permit this
1967 .\" See http://bugzilla.kernel.org/show_bug.cgi?id=5994
1968 Attempts to change the state of these flags are silently ignored.
1970 A limitation of the Linux system call conventions on some
1971 architectures (notably i386) means that if a (negative)
1972 process group ID to be returned by
1974 falls in the range \-1 to \-4095, then the return value is wrongly
1975 interpreted by glibc as an error in the system call;
1976 .\" glibc source: sysdeps/unix/sysv/linux/i386/sysdep.h
1977 that is, the return value of
1981 will contain the (positive) process group ID.
1984 operation avoids this problem.
1985 .\" mtk, Dec 04: some limited testing on alpha and ia64 seems to
1986 .\" indicate that ANY negative PGID value will cause F_GETOWN
1987 .\" to misinterpret the return as an error. Some other architectures
1988 .\" seem to have the same range check as i386.
1989 Since glibc version 2.11, glibc makes the kernel
1991 problem invisible by implementing
1996 In Linux 2.4 and earlier, there is bug that can occur
1997 when an unprivileged process uses
1999 to specify the owner
2000 of a socket file descriptor
2001 as a process (group) other than the caller.
2008 even when the owner process (group) is one that the caller
2009 has permission to send signals to.
2010 Despite this error return, the file descriptor owner is set,
2011 and signals will be sent to the owner.
2013 .SS Deadlock detection
2014 The deadlock-detection algorithm employed by the kernel when dealing with
2016 requests can yield both
2017 false negatives (failures to detect deadlocks,
2018 leaving a set of deadlocked processes blocked indefinitely)
2021 errors when there is no deadlock).
2023 the kernel limits the lock depth of its dependency search to 10 steps,
2024 meaning that circular deadlock chains that exceed
2025 that size will not be detected.
2026 In addition, the kernel may falsely indicate a deadlock
2027 when two or more processes created using the
2030 flag place locks that appear (to the kernel) to conflict.
2032 .SS Mandatory locking
2033 The Linux implementation of mandatory locking
2034 is subject to race conditions which render it unreliable:
2035 .\" http://marc.info/?l=linux-kernel&m=119013491707153&w=2
2037 .\" Reconfirmed by Jeff Layton
2038 .\" From: Jeff Layton <jlayton <at> redhat.com>
2039 .\" Subject: Re: Status of fcntl() mandatory locking
2040 .\" Newsgroups: gmane.linux.file-systems
2041 .\" Date: 2014-04-28 10:07:57 GMT
2042 .\" http://thread.gmane.org/gmane.linux.file-systems/84481/focus=84518
2045 call that overlaps with a lock may modify data after the mandatory lock is
2049 call that overlaps with a lock may detect changes to data that were made
2050 only after a write lock was acquired.
2051 Similar races exist between mandatory locks and
2053 It is therefore inadvisable to rely on mandatory locking.
2060 .BR capabilities (7),
2061 .BR feature_test_macros (7),
2065 .IR mandatory-locking.txt ,
2068 in the Linux kernel source directory
2069 .IR Documentation/filesystems/
2070 (on older kernels, these files are directly under the
2073 .I mandatory-locking.txt