]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/statx.2
statx.2: New page describing statx(2) system call added in Linux 4.11
[thirdparty/man-pages.git] / man2 / statx.2
CommitLineData
9905e59b
DH
1'\" t
2.\" Copyright (c) 2017 David Howells <dhowells@redhat.com>
3.\"
4.\" Derived from the stat.2 manual page:
5.\" Copyright (c) 1992 Drew Eckhardt (drew@cs.colorado.edu), March 28, 1992
6.\" Parts Copyright (c) 1995 Nicolai Langfeldt (janl@ifi.uio.no), 1/1/95
7.\" and Copyright (c) 2006, 2007, 2014 Michael Kerrisk <mtk.manpages@gmail.com>
8.\"
9.\" %%%LICENSE_START(VERBATIM)
10.\" Permission is granted to make and distribute verbatim copies of this
11.\" manual provided the copyright notice and this permission notice are
12.\" preserved on all copies.
13.\"
14.\" Permission is granted to copy and distribute modified versions of this
15.\" manual under the conditions for verbatim copying, provided that the
16.\" entire resulting derived work is distributed under the terms of a
17.\" permission notice identical to this one.
18.\"
19.\" Since the Linux kernel and libraries are constantly changing, this
20.\" manual page may be incorrect or out-of-date. The author(s) assume no
21.\" responsibility for errors or omissions, or for damages resulting from
22.\" the use of the information contained herein. The author(s) may not
23.\" have taken the same level of care in the production of this manual,
24.\" which is licensed free of charge, as they might when working
25.\" professionally.
26.\"
27.\" Formatted or processed versions of this manual, if unaccompanied by
28.\" the source, must acknowledge the copyright and authors of this work.
29.\" %%%LICENSE_END
30.\"
31.TH STATX 2 2017-03-07 "Linux" "Linux Programmer's Manual"
32.SH NAME
33statx \- Get file status (extended)
34.SH SYNOPSIS
35.nf
36.B #include <sys/types.h>
37.br
38.B #include <sys/stat.h>
39.br
40.B #include <unistd.h>
41.br
42.BR "#include <fcntl.h> " "/* Definition of AT_* constants */"
43.sp
44.BI "int statx(int " dirfd ", const char *" pathname ", int " flags ","
45.BI " unsigned int " mask ", struct statx *" buf );
46.fi
47.sp
48.in -4n
49Feature Test Macro Requirements for glibc (see
50.BR feature_test_macros (7)):
51.in
52.ad l
53.PD 0
54.sp
55.RS 4
56<unknown as yet>
57.RE
58.PD
59.ad
60.SH DESCRIPTION
61.PP
62This function returns information about a file, storing it in the buffer
63pointed to by
64.IR buf .
65The buffer is filled in according to the following type:
66.PP
67.in +4n
68.nf
69struct statx {
70 __u32 stx_mask; -- Mask of bits indicating filled fields
71 __u32 stx_blksize; -- Block size for filesystem I/O
72 __u64 stx_attributes; -- Extra file attribute indicators
73 __u32 stx_nlink; -- Number of hard links
74 __u32 stx_uid; -- User ID of owner
75 __u32 stx_gid; -- Group ID of owner
76 __u16 stx_mode; -- File type and mode
77 __u64 stx_ino; -- Inode number
78 __u64 stx_size; -- Total size in bytes
79 __u64 stx_blocks; -- Number of 512B blocks allocated
80 struct statx_timestamp stx_atime; -- Time of last access
81 struct statx_timestamp stx_btime; -- Time of creation
82 struct statx_timestamp stx_ctime; -- Time of last status change
83 struct statx_timestamp stx_mtime; -- Time of last modification
84 __u32 stx_rdev_major; } Device number if device file
85 __u32 stx_rdev_minor; }
86 __u32 stx_dev_major; } Device number of containing file
87 __u32 stx_dev_minor; }
88};
89.fi
90.in
91.PP
92Where the timestamps are defined as:
93.PP
94.in +4n
95.nf
96struct statx_timestamp {
97 __s64 tv_sec; -- Number of seconds (UNIX time)
98 __s32 tv_nsec; -- Number of nanoseconds before or since tv_sec
99};
100.fi
101.in
102.PP
103(Note that reserved space and padding is omitted)
104.SS
105Invoking \fBstatx\fR():
106.PP
107To access a file's status, no permissions are required on the file itself, but
108in the case of
109.BR statx ()
110with a path, execute (search) permission is required on all of the directories
111in
112.I pathname
113that lead to the file.
114.PP
115.BR statx ()
116uses
117.IR pathname ", " dirfd " and " flags
118to locate the target file in one of a variety of ways:
119.TP
120[*] By absolute path.
121.I pathname
122points to an absolute path and
123.I dirfd
124is ignored. The file is looked up by name, starting from the root of the
125filesystem as seen by the calling process.
126.TP
127[*] By cwd-relative path.
128.I pathname
129points to a relative path and
130.IR dirfd " is " AT_FDCWD .
131The file is looked up by name, starting from the current working directory.
132.TP
133[*] By dir-relative path.
134.I pathname
135points to relative path and
136.I dirfd
137indicates a file descriptor pointing to a directory. The file is looked up by
138name, starting from the directory specified by
139.IR dirfd .
140.TP
141[*] By file descriptor.
142.IR pathname " is " NULL " and " dirfd
143indicates a file descriptor. The file attached to the file descriptor is
144queried directly. The file descriptor may point to any type of file, not just
145a directory.
146.PP
147.I flags
148can be used to influence a path-based lookup. A value for
149.I flags
150is constructed by OR'ing together zero or more of the following constants:
151.TP
152.BR AT_EMPTY_PATH
153.\" commit 65cfc6722361570bfe255698d9cd4dccaf47570d
154If
155.I pathname
156is an empty string, operate on the file referred to by
157.IR dirfd
158(which may have been obtained using the
159.BR open (2)
160.B O_PATH
161flag).
162If
163.I dirfd
164is
165.BR AT_FDCWD ,
166the call operates on the current working directory.
167In this case,
168.I dirfd
169can refer to any type of file, not just a directory.
170This flag is Linux-specific; define
171.B _GNU_SOURCE
172.\" Before glibc 2.16, defining _ATFILE_SOURCE sufficed
173to obtain its definition.
174.TP
175.BR AT_NO_AUTOMOUNT
176Don't automount the terminal ("basename") component of
177.I pathname
178if it is a directory that is an automount point.
179This allows the caller to gather attributes of an automount point
180(rather than the location it would mount).
181This flag can be used in tools that scan directories
182to prevent mass-automounting of a directory of automount points.
183The
184.B AT_NO_AUTOMOUNT
185flag has no effect if the mount point has already been mounted over.
186This flag is Linux-specific; define
187.B _GNU_SOURCE
188.\" Before glibc 2.16, defining _ATFILE_SOURCE sufficed
189to obtain its definition.
190.TP
191.B AT_SYMLINK_NOFOLLOW
192If
193.I pathname
194is a symbolic link, do not dereference it:
195instead return information about the link itself, like
196.BR lstat ().
197.PP
198.I flags
199can also be used to control what sort of synchronisation the kernel will do
200when querying a file on a remote filesystem. This is done by OR'ing in one of
201the following values:
202.TP
203AT_STATX_SYNC_AS_STAT
204Do whatever
205.BR stat ()
206does. This is the default and is very much filesystem specific.
207.TP
208AT_STATX_FORCE_SYNC
209Force the attributes to be synchronised with the server. This may require that
210a network filesystem perform a data writeback to get the timestamps correct.
211.TP
212AT_STATX_DONT_SYNC
213Don't synchronise anything, but rather just take whatever the system has cached
214if possible. This may mean that the information returned is approximate, but,
215on a network filesystem, it may not involve a round trip to the server - even
216if no lease is held.
217.PP
218The
219.I mask
220argument to
221.BR statx ()
222is used to tell the kernel which fields the caller is interested in.
223.I mask
224is an OR'ed combination of the following constants:
225.PP
226.in +4n
227.TS
228lB l.
229STATX_TYPE Want stx_mode & S_IFMT
230STATX_MODE Want stx_mode & ~S_IFMT
231STATX_NLINK Want stx_nlink
232STATX_UID Want stx_uid
233STATX_GID Want stx_gid
234STATX_ATIME Want stx_atime{,_ns}
235STATX_MTIME Want stx_mtime{,_ns}
236STATX_CTIME Want stx_ctime{,_ns}
237STATX_INO Want stx_ino
238STATX_SIZE Want stx_size
239STATX_BLOCKS Want stx_blocks
240STATX_BASIC_STATS [All of the above]
241STATX_BTIME Want stx_btime{,_ns}
242STATX_ALL [All currently available fields]
243.TE
244.in
245.PP
246.B "Do not"
247simply set
248.I mask
249to UINT_MAX as one or more bits may, in the future, be used to specify an
250extension to the buffer.
251.SS
252The returned information
253.PP
254The status information for the target file is returned in the
255.I statx
256structure pointed to by
257.IR buf .
258Included in this is
259.I stx_mask
260which indicates what other information has been returned.
261.I stx_mask
262has the same format as the mask argument and bits are set in it to indicate
263which fields have been filled in.
264.PP
265It should be noted that the kernel may return fields that weren't requested and
266may fail to return fields that were requested, depending on what the backing
267filesystem supports. In either case,
268.I stx_mask
269will not be equal
270.IR mask .
271.PP
272If a filesystem does not support a field or if it has an unrepresentable value
273(for instance, a file with an exotic type), then the mask bit corresponding to
274that field will be cleared in
275.I stx_mask
276even if the user asked for it and a dummy value will be filled in for
277compatibility purposes if one is available (e.g. a dummy uid and gid may be
278specified to mount under some circumstances).
279.PP
280A filesystem may also fill in fields that the caller didn't ask for if it has
281values for them available at no extra cost. If this happens, the corresponding
282bits will be set in
283.IR stx_mask .
284.PP
285
286.\" Background: inode attributes are modified with i_mutex held, but
287.\" read by stat() without taking the mutex.
288.I Note:
289For performance and simplicity reasons, different fields in the
290.I statx
291structure may contain state information from different moments
292during the execution of the system call.
293For example, if
294.IR stx_mode
295or
296.IR stx_uid
297is changed by another process by calling
298.BR chmod (2)
299or
300.BR chown (2),
301.BR stat ()
302might return the old
303.I stx_mode
304together with the new
305.IR stx_uid ,
306or the old
307.I stx_uid
308together with the new
309.IR stx_mode .
310.PP
311Apart from stx_mask (which is described above), the fields in the
312.I statx
313structure are:
314.TP
315.I stx_mode
316The file type and mode. This is described in more detail below.
317.TP
318.I stx_size
319The size of the file (if it is a regular file or a symbolic link) in bytes.
320The size of a symbolic link is the length of the pathname it contains, without
321a terminating null byte.
322.TP
323.I stx_blocks
324The number of blocks allocated to the file on the medium, in 512-byte units.
325(This may be smaller than
326.IR stx_size /512
327when the file has holes.)
328.TP
329.I stx_blksize
330The "preferred" blocksize for efficient filesystem I/O. (Writing to a file in
331smaller chunks may cause an inefficient read-modify-rewrite.)
332.TP
333.I stx_nlink
334The number of hard links on a file.
335.TP
336.I stx_uid
337The user ID of the file's owner.
338.TP
339.I stx_gid
340The ID of the group that may access the file.
341.TP
342.IR stx_dev_major " and " stx_dev_minor
343The device on which this file (inode) resides.
344.TP
345.IR stx_rdev_major " and " stx_rdev_minor
346The device that this file (inode) represents if the file is of block or
347character device type.
348.TP
349.I stx_attributes
350Further status information about the file (see below for more information).
351.TP
352.I stx_atime
353The file's last access timestamp.
354This field is changed by file accesses, for example, by
355.BR execve (2),
356.BR mknod (2),
357.BR pipe (2),
358.BR utime (2),
359and
360.BR read (2)
361(of more than zero bytes).
362Other routines, such as
363.BR mmap (2),
364may or may not update it.
365.TP
366.I stx_btime
367The file's creation timestamp. This is set on file creation and not changed
368subsequently.
369.TP
370.I stx_ctime
371The file's last status change timestamp. This field is changed by writing or
372by setting inode information (i.e., owner, group, link count, mode, etc.).
373.TP
374.I stx_mtime
375The file's last modification timestamp. This is changed by file modifications,
376for example, by
377.BR mknod (2),
378.BR truncate (2),
379.BR utime (2),
380and
381.BR write (2)
382(of more than zero bytes). Moreover, the modification time of a directory is
383changed by the creation or deletion of files in that directory. This field is
384.I not
385changed for changes in owner, group, hard link count, or mode.
386
387
388
389.PP
390Not all of the Linux filesystems implement all of the timestamp fields. Some
391filesystems allow mounting in such a way that file and/or directory accesses do
392not cause an update of the
393.I stx_atime
394field.
395(See
396.IR noatime ,
397.IR nodiratime ,
398and
399.I relatime
400in
401.BR mount (8),
402and related information in
403.BR mount (2).)
404In addition,
405.I stx_atime
406is not updated if a file is opened with the
407.BR O_NOATIME ;
408see
409.BR open (2).
410
411.SS File attributes
412.PP
413The
414.I stx_attributes
415field contains a set of OR'ed flags that indicate additional attributes of the
416file:
417.TP
418STATX_ATTR_COMPRESSED
419The file is compressed by the fs and may take extra resources to access.
420.TP
421STATX_ATTR_IMMUTABLE
422The file cannot be modified: it cannot be deleted or renamed, no hard links can
423be created to this file and no data can be written to it. See chattr(1).
424.TP
425STATX_ATTR_APPEND
426The file can only be opened in append mode for writing. Random access writing
427is not permitted. See chattr(1).
428.TP
429STATX_ATTR_NODUMP
430File is not a candidate for backup when a backup program such as dump(8) is
431run. See chattr(1).
432.TP
433STATX_ATTR_ENCRYPTED
434A key is required for the file to be encrypted by the filesystem.
435.SS File type and mode
436.PP
437The
438.I stx_mode
439field contains the combined file type and mode. POSIX refers to the bits in
440this field corresponding to the mask
441.B S_IFMT
442(see below) as the
443.IR "file type" ,
444the 12 bits corresponding to the mask 07777 as the
445.IR "file mode bits"
446and the least significant 9 bits (0777) as the
447.IR "file permission bits" .
448.IP
449The following mask values are defined for the file type of the
450.I stx_mode
451field:
452.in +4n
453.TS
454lB l l.
455S_IFMT 0170000 bit mask for the file type bit field
456
457S_IFSOCK 0140000 socket
458S_IFLNK 0120000 symbolic link
459S_IFREG 0100000 regular file
460S_IFBLK 0060000 block device
461S_IFDIR 0040000 directory
462S_IFCHR 0020000 character device
463S_IFIFO 0010000 FIFO
464.TE
465.in
466.IP
467Note that
468.I stx_mode
469has two mask flags covering it: one for the type and one for the mode bits.
470.PP
471To test for a regular file (for example), one could write:
472.nf
473.in +4n
474statx(AT_FDCWD, pathname, 0, STATX_TYPE, &sb);
475if ((sb.stx_mask & STATX_TYPE) && (sb.stx_mode & S_IFMT) == S_IFREG) {
476 /* Handle regular file */
477}
478.in
479.fi
480.PP
481Because tests of the above form are common, additional macros are defined by
482POSIX to allow the test of the file type in
483.I stx_mode
484to be written more concisely:
485.RS 4
486.TS
487lB l.
488\fBS_ISREG\fR(m) Is it a regular file?
489\fBS_ISDIR\fR(m) Is it a directory?
490\fBS_ISCHR\fR(m) Is it a character device?
491\fBS_ISBLK\fR(m) Is it a block device?
492\fBS_ISFIFO\fR(m) Is it a FIFO (named pipe)?
493\fBS_ISLNK\fR(m) Is it a symbolic link? (Not in POSIX.1-1996.)
494\fBS_ISSOCK\fR(m) Is it a socket? (Not in POSIX.1-1996.)
495.TE
496.RE
497.PP
498The preceding code snippet could thus be rewritten as:
499
500.nf
501.in +4n
502statx(AT_FDCWD, pathname, 0, STATX_TYPE, &sb);
503if ((sb.stx_mask & STATX_TYPE) && S_ISREG(sb.stx_mode)) {
504 /* Handle regular file */
505}
506.in
507.fi
508.PP
509The definitions of most of the above file type test macros
510are provided if any of the following feature test macros is defined:
511.BR _BSD_SOURCE
512(in glibc 2.19 and earlier),
513.BR _SVID_SOURCE
514(in glibc 2.19 and earlier),
515or
516.BR _DEFAULT_SOURCE
517(in glibc 2.20 and later).
518In addition, definitions of all of the above macros except
519.BR S_IFSOCK
520and
521.BR S_ISSOCK ()
522are provided if
523.BR _XOPEN_SOURCE
524is defined.
525The definition of
526.BR S_IFSOCK
527can also be exposed by defining
528.BR _XOPEN_SOURCE
529with a value of 500 or greater.
530
531The definition of
532.BR S_ISSOCK ()
533is exposed if any of the following feature test macros is defined:
534.BR _BSD_SOURCE
535(in glibc 2.19 and earlier),
536.BR _DEFAULT_SOURCE
537(in glibc 2.20 and later),
538.BR _XOPEN_SOURCE
539with a value of 500 or greater, or
540.BR _POSIX_C_SOURCE
541with a value of 200112L or greater.
542.PP
543The following mask values are defined for
544the file mode component of the
545.I stx_mode
546field:
547.in +4n
548.TS
549lB l l.
550S_ISUID 04000 set-user-ID bit
551S_ISGID 02000 set-group-ID bit (see below)
552S_ISVTX 01000 sticky bit (see below)
553
554S_IRWXU 00700 owner has read, write, and execute permission
555S_IRUSR 00400 owner has read permission
556S_IWUSR 00200 owner has write permission
557S_IXUSR 00100 owner has execute permission
558
559S_IRWXG 00070 group has read, write, and execute permission
560S_IRGRP 00040 group has read permission
561S_IWGRP 00020 group has write permission
562S_IXGRP 00010 group has execute permission
563
564S_IRWXO 00007 T{
565others (not in group) have read, write, and execute permission
566T}
567S_IROTH 00004 others have read permission
568S_IWOTH 00002 others have write permission
569S_IXOTH 00001 others have execute permission
570.TE
571.in
572.P
573The set-group-ID bit
574.RB ( S_ISGID )
575has several special uses.
576For a directory, it indicates that BSD semantics is to be used
577for that directory: files created there inherit their group ID from
578the directory, not from the effective group ID of the creating process,
579and directories created there will also get the
580.B S_ISGID
581bit set.
582For a file that does not have the group execution bit
583.RB ( S_IXGRP )
584set,
585the set-group-ID bit indicates mandatory file/record locking.
586.P
587The sticky bit
588.RB ( S_ISVTX )
589on a directory means that a file
590in that directory can be renamed or deleted only by the owner
591of the file, by the owner of the directory, and by a privileged
592process.
593
594
595.SH RETURN VALUE
596On success, zero is returned.
597On error, \-1 is returned, and
598.I errno
599is set appropriately.
600.SH ERRORS
601.TP
602.B EINVAL
603Invalid flag specified in
604.IR flags .
605.TP
606.B EACCES
607Search permission is denied for one of the directories
608in the path prefix of
609.IR pathname .
610(See also
611.BR path_resolution (7).)
612.TP
613.B EBADF
614.I dirfd
615is not a valid open file descriptor.
616.TP
617.B EFAULT
618Bad address.
619.TP
620.B ELOOP
621Too many symbolic links encountered while traversing the path.
622.TP
623.B ENAMETOOLONG
624.I pathname
625is too long.
626.TP
627.B ENOENT
628A component of
629.I pathname
630does not exist, or
631.I pathname
632is an empty string and AT_EMPTY_PATH was not specified.
633.TP
634.B ENOMEM
635Out of memory (i.e., kernel memory).
636.TP
637.B ENOTDIR
638A component of the path prefix of
639.I pathname
640is not a directory or
641.I pathname
642is relative and
643.I dirfd
644is a file descriptor referring to a file other than a directory.
645.SH VERSIONS
646.BR statx ()
647was added to Linux in kernel 4.11;
648library support is not yet added to glibc.
649.SH SEE ALSO
650.BR ls (1),
651.BR stat (1),
652.BR access (2),
653.BR chmod (2),
654.BR chown (2),
655.BR stat (2),
656.BR readlink (2),
657.BR utime (2),
658.BR capabilities (7),
659.BR symlink (7)