Debian Codegraph @

upstream/fedora-44/man7/inode.7

raw
1 '\" t
2 .\" Copyright (c) 2017 Michael Kerrisk <[email protected]>
3 .\"
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .\"
6 .TH inode 7 2024-09-01 "Linux man-pages 6.13"
7 .SH NAME
8 inode \- file inode information
9 .SH DESCRIPTION
10 Each file has an inode containing metadata about the file.
11 An application can retrieve this metadata using
12 .BR stat (2)
13 (or related calls), which returns a
14 .I stat
15 structure, or
16 .BR statx (2),
17 which returns a
18 .I statx
19 structure.
20 .P
21 The following is a list of the information typically found in,
22 or associated with, the file inode,
23 with the names of the corresponding structure fields returned by
24 .BR stat (2)
25 and
26 .BR statx (2):
27 .TP
28 Device where inode resides
29 \fIstat.st_dev\fP; \fIstatx.stx_dev_minor\fP and \fIstatx.stx_dev_major\fP
30 .IP
31 Each inode (as well as the associated file) resides in a filesystem
32 that is hosted on a device.
33 That device is identified by the combination of its major ID
34 (which identifies the general class of device)
35 and minor ID (which identifies a specific instance in the general class).
36 .TP
37 Inode number
38 \fIstat.st_ino\fP; \fIstatx.stx_ino\fP
39 .IP
40 Each file in a filesystem has a unique inode number.
41 Inode numbers are guaranteed to be unique only within a filesystem
42 (i.e., the same inode numbers may be used by different filesystems,
43 which is the reason that hard links may not cross filesystem boundaries).
44 This field contains the file's inode number.
45 .TP
46 File type and mode
47 \fIstat.st_mode\fP; \fIstatx.stx_mode\fP
48 .IP
49 See the discussion of file type and mode, below.
50 .TP
51 Link count
52 \fIstat.st_nlink\fP; \fIstatx.stx_nlink\fP
53 .IP
54 This field contains the number of hard links to the file.
55 Additional links to an existing file are created using
56 .BR link (2).
57 .TP
58 User ID
59 \fIstat.st_uid\fP; \fIstatx.stx_uid\fP
60 .IP
61 This field records the user ID of the owner of the file.
62 For newly created files,
63 the file user ID is the effective user ID of the creating process.
64 The user ID of a file can be changed using
65 .BR chown (2).
66 .TP
67 Group ID
68 \fIstat.st_gid\fP; \fIstatx.stx_gid\fP
69 .IP
70 The inode records the ID of the group owner of the file.
71 For newly created files,
72 the file group ID is either the group ID of the parent directory or
73 the effective group ID of the creating process,
74 depending on whether or not the set-group-ID bit
75 is set on the parent directory (see below).
76 The group ID of a file can be changed using
77 .BR chown (2).
78 .TP
79 Device represented by this inode
80 \fIstat.st_rdev\fP; \fIstatx.stx_rdev_minor\fP and \fIstatx.stx_rdev_major\fP
81 .IP
82 If this file (inode) represents a device,
83 then the inode records the major and minor ID of that device.
84 .TP
85 File size
86 \fIstat.st_size\fP; \fIstatx.stx_size\fP
87 .IP
88 This field gives the size of the file (if it is a regular
89 file or a symbolic link) in bytes.
90 The size of a symbolic link is the length of the pathname
91 it contains, without a terminating null byte.
92 .TP
93 Preferred block size for I/O
94 \fIstat.st_blksize\fP; \fIstatx.stx_blksize\fP
95 .IP
96 This field gives the "preferred" blocksize for efficient filesystem I/O.
97 (Writing to a file in smaller chunks may cause
98 an inefficient read-modify-rewrite.)
99 .TP
100 Number of blocks allocated to the file
101 \fIstat.st_blocks\fP; \fIstatx.stx_blocks\fP
102 .IP
103 This field indicates the number of blocks allocated to the file,
104 512-byte units,
105 (This may be smaller than
106 .IR st_size /512
107 when the file has holes.)
108 .IP
109 The POSIX.1 standard notes
110 .\" Rationale for sys/stat.h in POSIX.1-2008
111 that the unit for the
112 .I st_blocks
113 member of the
114 .I stat
115 structure is not defined by the standard.
116 On many implementations it is 512 bytes;
117 on a few systems, a different unit is used, such as 1024.
118 Furthermore, the unit may differ on a per-filesystem basis.
119 .TP
120 Last access timestamp (atime)
121 \fIstat.st_atime\fP; \fIstatx.stx_atime\fP
122 .IP
123 This is the file's last access timestamp.
124 It is changed by file accesses, for example, by
125 .BR execve (2),
126 .BR mknod (2),
127 .BR pipe (2),
128 .BR utime (2),
129 and
130 .BR read (2)
131 (of more than zero bytes).
132 Other interfaces, such as
133 .BR mmap (2),
134 may or may not update the atime timestamp
135 .IP
136 Some filesystem types allow mounting in such a way that file
137 and/or directory accesses do not cause an update of the atime timestamp.
138 (See
139 .IR noatime ,
140 .IR nodiratime ,
141 and
142 .I relatime
143 in
144 .BR mount (8),
145 and related information in
146 .BR mount (2).)
147 In addition, the atime timestamp
148 is not updated if a file is opened with the
149 .B O_NOATIME
150 flag; see
151 .BR open (2).
152 .TP
153 File creation (birth) timestamp (btime)
154 (not returned in the \fIstat\fP structure); \fIstatx.stx_btime\fP
155 .IP
156 The file's creation timestamp.
157 This is set on file creation and not changed subsequently.
158 .IP
159 The btime timestamp was not historically present on UNIX systems
160 and is not currently supported by most Linux filesystems.
161 .\" FIXME Is it supported on ext4 and XFS?
162 .TP
163 Last modification timestamp (mtime)
164 \fIstat.st_mtime\fP; \fIstatx.stx_mtime\fP
165 .IP
166 This is the file's last modification timestamp.
167 It is changed by file modifications, for example, by
168 .BR mknod (2),
169 .BR truncate (2),
170 .BR utime (2),
171 and
172 .BR write (2)
173 (of more than zero bytes).
174 Moreover, the mtime timestamp
175 of a directory is changed by the creation or deletion of files
176 in that directory.
177 The mtime timestamp is
178 .I not
179 changed for changes in owner, group, hard link count, or mode.
180 .TP
181 Last status change timestamp (ctime)
182 \fIstat.st_ctime\fP; \fIstatx.stx_ctime\fP
183 .IP
184 This is the file's last status change timestamp.
185 It is changed by writing or by setting inode information
186 (i.e., owner, group, link count, mode, etc.).
187 .P
188 The timestamp fields report time measured with a zero point at the
189 .IR Epoch ,
190 1970-01-01 00:00:00 +0000, UTC (see
191 .BR time (7)).
192 .P
193 Nanosecond timestamps are supported on XFS, JFS, Btrfs, and
194 ext4 (since Linux 2.6.23).
195 .\" commit ef7f38359ea8b3e9c7f2cae9a4d4935f55ca9e80
196 Nanosecond timestamps are not supported in ext2, ext3, and Reiserfs.
197 In order to return timestamps with nanosecond precision,
198 the timestamp fields in the
199 .I stat
200 and
201 .I statx
202 structures are defined as structures that include a nanosecond component.
203 See
204 .BR stat (2)
205 and
206 .BR statx (2)
207 for details.
208 On filesystems that do not support subsecond timestamps,
209 the nanosecond fields in the
210 .I stat
211 and
212 .I statx
213 structures are returned with the value 0.
214 .\"
215 .SS The file type and mode
216 The
217 .I stat.st_mode
218 field (for
219 .BR statx (2),
220 the
221 .I statx.stx_mode
222 field) contains the file type and mode.
223 .P
224 POSIX refers to the
225 .I stat.st_mode
226 bits corresponding to the mask
227 .B S_IFMT
228 (see below) as the
229 .IR "file type" ,
230 the 12 bits corresponding to the mask 07777 as the
231 .I file mode bits
232 and the least significant 9 bits (0777) as the
233 .IR "file permission bits" .
234 .P
235 The following mask values are defined for the file type:
236 .in +4n
237 .TS
238 lB l l.
239 S_IFMT 0170000 bit mask for the file type bit field
240
241 S_IFSOCK 0140000 socket
242 S_IFLNK 0120000 symbolic link
243 S_IFREG 0100000 regular file
244 S_IFBLK 0060000 block device
245 S_IFDIR 0040000 directory
246 S_IFCHR 0020000 character device
247 S_IFIFO 0010000 FIFO
248 .TE
249 .in
250 .P
251 Thus, to test for a regular file (for example), one could write:
252 .P
253 .in +4n
254 .EX
255 stat(pathname, &sb);
256 if ((sb.st_mode & S_IFMT) == S_IFREG) {
257 /* Handle regular file */
258 }
259 .EE
260 .in
261 .P
262 Because tests of the above form are common, additional
263 macros are defined by POSIX to allow the test of the file type in
264 .I st_mode
265 to be written more concisely:
266 .RS 4
267 .TP 1.2i
268 .BR S_ISREG (m)
269 is it a regular file?
270 .TP
271 .BR S_ISDIR (m)
272 directory?
273 .TP
274 .BR S_ISCHR (m)
275 character device?
276 .TP
277 .BR S_ISBLK (m)
278 block device?
279 .TP
280 .BR S_ISFIFO (m)
281 FIFO (named pipe)?
282 .TP
283 .BR S_ISLNK (m)
284 symbolic link? (Not in POSIX.1-1996.)
285 .TP
286 .BR S_ISSOCK (m)
287 socket? (Not in POSIX.1-1996.)
288 .RE
289 .P
290 The preceding code snippet could thus be rewritten as:
291 .P
292 .in +4n
293 .EX
294 stat(pathname, &sb);
295 if (S_ISREG(sb.st_mode)) {
296 /* Handle regular file */
297 }
298 .EE
299 .in
300 .P
301 The definitions of most of the above file type test macros
302 are provided if any of the following feature test macros is defined:
303 .B _BSD_SOURCE
304 (in glibc 2.19 and earlier),
305 .B _SVID_SOURCE
306 (in glibc 2.19 and earlier),
307 or
308 .B _DEFAULT_SOURCE
309 (in glibc 2.20 and later).
310 In addition, definitions of all of the above macros except
311 .B S_IFSOCK
312 and
313 .BR S_ISSOCK ()
314 are provided if
315 .B _XOPEN_SOURCE
316 is defined.
317 .P
318 The definition of
319 .B S_IFSOCK
320 can also be exposed either by defining
321 .B _XOPEN_SOURCE
322 with a value of 500 or greater or (since glibc 2.24) by defining both
323 .B _XOPEN_SOURCE
324 and
325 .BR _XOPEN_SOURCE_EXTENDED .
326 .P
327 The definition of
328 .BR S_ISSOCK ()
329 is exposed if any of the following feature test macros is defined:
330 .B _BSD_SOURCE
331 (in glibc 2.19 and earlier),
332 .B _DEFAULT_SOURCE
333 (in glibc 2.20 and later),
334 .B _XOPEN_SOURCE
335 with a value of 500 or greater,
336 .B _POSIX_C_SOURCE
337 with a value of 200112L or greater, or (since glibc 2.24) by defining both
338 .B _XOPEN_SOURCE
339 and
340 .BR _XOPEN_SOURCE_EXTENDED .
341 .P
342 The following mask values are defined for
343 the file mode component of the
344 .I st_mode
345 field:
346 .in +4n
347 .TS
348 lB l lx.
349 S_ISUID 04000 T{
350 set-user-ID bit (see
351 .BR execve (2))
352 T}
353 S_ISGID 02000 T{
354 set-group-ID bit (see below)
355 T}
356 S_ISVTX 01000 T{
357 sticky bit (see below)
358 T}
359
360 S_IRWXU 00700 T{
361 owner has read, write, and execute permission
362 T}
363 S_IRUSR 00400 T{
364 owner has read permission
365 T}
366 S_IWUSR 00200 T{
367 owner has write permission
368 T}
369 S_IXUSR 00100 T{
370 owner has execute permission
371 T}
372
373 S_IRWXG 00070 T{
374 group has read, write, and execute permission
375 T}
376 S_IRGRP 00040 T{
377 group has read permission
378 T}
379 S_IWGRP 00020 T{
380 group has write permission
381 T}
382 S_IXGRP 00010 T{
383 group has execute permission
384 T}
385
386 S_IRWXO 00007 T{
387 others (not in group) have read, write, and execute permission
388 T}
389 S_IROTH 00004 T{
390 others have read permission
391 T}
392 S_IWOTH 00002 T{
393 others have write permission
394 T}
395 S_IXOTH 00001 T{
396 others have execute permission
397 T}
398 .TE
399 .in
400 .P
401 The set-group-ID bit
402 .RB ( S_ISGID )
403 has several special uses.
404 For a directory, it indicates that BSD semantics are to be used
405 for that directory: files created there inherit their group ID from
406 the directory, not from the effective group ID of the creating process,
407 and directories created there will also get the
408 .B S_ISGID
409 bit set.
410 For an executable file, the set-group-ID bit causes the effective group ID
411 of a process that executes the file to change as described in
412 .BR execve (2).
413 For a file that does not have the group execution bit
414 .RB ( S_IXGRP )
415 set,
416 the set-group-ID bit indicates mandatory file/record locking.
417 .P
418 The sticky bit
419 .RB ( S_ISVTX )
420 on a directory means that a file
421 in that directory can be renamed or deleted only by the owner
422 of the file, by the owner of the directory, and by a privileged
423 process.
424 .SH STANDARDS
425 POSIX.1-2008.
426 .SH HISTORY
427 POSIX.1-2001.
428 .P
429 POSIX.1-1990 did not describe the
430 .BR S_IFMT ,
431 .BR S_IFSOCK ,
432 .BR S_IFLNK ,
433 .BR S_IFREG ,
434 .BR S_IFBLK ,
435 .BR S_IFDIR ,
436 .BR S_IFCHR ,
437 .BR S_IFIFO ,
438 and
439 .B S_ISVTX
440 constants, but instead specified the use of
441 the macros
442 .BR S_ISDIR ()
443 and so on.
444 .P
445 The
446 .BR S_ISLNK ()
447 and
448 .BR S_ISSOCK ()
449 macros were not in
450 POSIX.1-1996;
451 the former is from SVID 4, the latter from SUSv2.
452 .P
453 UNIX\ V7 (and later systems) had
454 .BR S_IREAD ,
455 .BR S_IWRITE ,
456 .BR S_IEXEC ,
457 and
458 where POSIX
459 prescribes the synonyms
460 .BR S_IRUSR ,
461 .BR S_IWUSR ,
462 and
463 .BR S_IXUSR .
464 .SH NOTES
465 For pseudofiles that are autogenerated by the kernel, the file size
466 (\fIstat.st_size\fP; \fIstatx.stx_size\fP)
467 reported by the kernel is not accurate.
468 For example, the value 0 is returned for many files under the
469 .I /proc
470 directory,
471 while various files under
472 .I /sys
473 report a size of 4096 bytes, even though the file content is smaller.
474 For such files, one should simply try to read as many bytes as possible
475 (and append \[aq]\[rs]0\[aq] to the returned buffer
476 if it is to be interpreted as a string).
477 .SH SEE ALSO
478 .BR stat (1),
479 .BR stat (2),
480 .BR statx (2),
481 .BR symlink (7)