open.2: Make it clearer that an FD is an index into the process's FD table
[man-pages.git] / man2 / ioctl_getfsmap.2
blob4ff1c8a9070428b73140c886f857711e2a4d4693
1 .\" Copyright (c) 2017, Oracle.  All rights reserved.
2 .\"
3 .\" %%%LICENSE_START(GPLv2+_DOC_FULL)
4 .\" This is free documentation; you can redistribute it and/or
5 .\" modify it under the terms of the GNU General Public License as
6 .\" published by the Free Software Foundation; either version 2 of
7 .\" the License, or (at your option) any later version.
8 .\"
9 .\" The GNU General Public License's references to "object code"
10 .\" and "executables" are to be interpreted as the output of any
11 .\" document formatting or typesetting system, including
12 .\" intermediate and printed output.
13 .\"
14 .\" This manual is distributed in the hope that it will be useful,
15 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
16 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 .\" GNU General Public License for more details.
18 .\"
19 .\" You should have received a copy of the GNU General Public
20 .\" License along with this manual; if not, see
21 .\" <http://www.gnu.org/licenses/>.
22 .\" %%%LICENSE_END
23 .TH IOCTL_GETFSMAP 2 2021-03-22 "Linux" "Linux Programmer's Manual"
24 .SH NAME
25 ioctl_getfsmap \- retrieve the physical layout of the filesystem
26 .SH SYNOPSIS
27 .nf
28 .B #include <sys/ioctl.h>
29 .B #include <linux/fs.h>
30 .B #include <linux/fsmap.h>
31 .PP
32 .BI "int ioctl(int " fd ", FS_IOC_GETFSMAP, struct fsmap_head * " arg );
33 .fi
34 .SH DESCRIPTION
35 This
36 .BR ioctl (2)
37 operation retrieves physical extent mappings for a filesystem.
38 This information can be used to discover which files are mapped to a physical
39 block, examine free space, or find known bad blocks, among other things.
40 .PP
41 The sole argument to this operation should be a pointer to a single
42 .IR "struct fsmap_head" ":"
43 .PP
44 .in +4n
45 .EX
46 struct fsmap {
47     __u32 fmr_device;      /* Device ID */
48     __u32 fmr_flags;       /* Mapping flags */
49     __u64 fmr_physical;    /* Device offset of segment */
50     __u64 fmr_owner;       /* Owner ID */
51     __u64 fmr_offset;      /* File offset of segment */
52     __u64 fmr_length;      /* Length of segment */
53     __u64 fmr_reserved[3]; /* Must be zero */
56 struct fsmap_head {
57     __u32 fmh_iflags;       /* Control flags */
58     __u32 fmh_oflags;       /* Output flags */
59     __u32 fmh_count;        /* # of entries in array incl. input */
60     __u32 fmh_entries;      /* # of entries filled in (output) */
61     __u64 fmh_reserved[6];  /* Must be zero */
63     struct fsmap fmh_keys[2];  /* Low and high keys for
64                                   the mapping search */
65     struct fsmap fmh_recs[];   /* Returned records */
67 .EE
68 .in
69 .PP
70 The two
71 .I fmh_keys
72 array elements specify the lowest and highest reverse-mapping
73 key for which the application would like physical mapping
74 information.
75 A reverse mapping key consists of the tuple (device, block, owner, offset).
76 The owner and offset fields are part of the key because some filesystems
77 support sharing physical blocks between multiple files and
78 therefore may return multiple mappings for a given physical block.
79 .PP
80 Filesystem mappings are copied into the
81 .I fmh_recs
82 array, which immediately follows the header data.
83 .\"
84 .SS Fields of struct fsmap_head
85 The
86 .I fmh_iflags
87 field is a bit mask passed to the kernel to alter the output.
88 No flags are currently defined, so the caller must set this value to zero.
89 .PP
90 The
91 .I fmh_oflags
92 field is a bit mask of flags set by the kernel concerning the returned mappings.
94 .B FMH_OF_DEV_T
95 is set, then the
96 .I fmr_device
97 field represents a
98 .I dev_t
99 structure containing the major and minor numbers of the block device.
102 .I fmh_count
103 field contains the number of elements in the array being passed to the
104 kernel.
105 If this value is 0,
106 .I fmh_entries
107 will be set to the number of records that would have been returned had
108 the array been large enough;
109 no mapping information will be returned.
112 .I fmh_entries
113 field contains the number of elements in the
114 .I fmh_recs
115 array that contain useful information.
118 .I fmh_reserved
119 fields must be set to zero.
121 .SS Keys
122 The two key records in
123 .I fsmap_head.fmh_keys
124 specify the lowest and highest extent records in the keyspace that the caller
125 wants returned.
126 A filesystem that can share blocks between files likely requires the tuple
127 .RI "(" "device" ", " "physical" ", " "owner" ", " "offset" ", " "flags" ")"
128 to uniquely index any filesystem mapping record.
129 Classic non-sharing filesystems might be able to identify any record with only
130 .RI "(" "device" ", " "physical" ", " "flags" ")."
131 For example, if the low key is set to (8:0, 36864, 0, 0, 0), the filesystem will
132 only return records for extents starting at or above 36\ KiB on disk.
133 If the high key is set to (8:0, 1048576, 0, 0, 0), only records below 1\ MiB will
134 be returned.
135 The format of
136 .I fmr_device
137 in the keys must match the format of the same field in the output records,
138 as defined below.
139 By convention, the field
140 .I fsmap_head.fmh_keys[0]
141 must contain the low key and
142 .I fsmap_head.fmh_keys[1]
143 must contain the high key for the request.
145 For convenience, if
146 .I fmr_length
147 is set in the low key, it will be added to
148 .IR fmr_block " or " fmr_offset
149 as appropriate.
150 The caller can take advantage of this subtlety to set up subsequent calls
151 by copying
152 .I fsmap_head.fmh_recs[fsmap_head.fmh_entries \- 1]
153 into the low key.
154 The function
155 .I fsmap_advance
156 (defined in
157 .IR linux/fsmap.h )
158 provides this functionality.
160 .SS Fields of struct fsmap
162 .I fmr_device
163 field uniquely identifies the underlying storage device.
164 If the
165 .B FMH_OF_DEV_T
166 flag is set in the header's
167 .I fmh_oflags
168 field, this field contains a
169 .I dev_t
170 from which major and minor numbers can be extracted.
171 If the flag is not set, this field contains a value that must be unique
172 for each unique storage device.
175 .I fmr_physical
176 field contains the disk address of the extent in bytes.
179 .I fmr_owner
180 field contains the owner of the extent.
181 This is an inode number unless
182 .B FMR_OF_SPECIAL_OWNER
183 is set in the
184 .I fmr_flags
185 field, in which case the value is determined by the filesystem.
186 See the section below about owner values for more details.
189 .I fmr_offset
190 field contains the logical address in the mapping record in bytes.
191 This field has no meaning if the
192 .BR FMR_OF_SPECIAL_OWNER " or " FMR_OF_EXTENT_MAP
193 flags are set in
194 .IR fmr_flags "."
197 .I fmr_length
198 field contains the length of the extent in bytes.
201 .I fmr_flags
202 field is a bit mask of extent state flags.
203 The bits are:
204 .RS 0.4i
206 .B FMR_OF_PREALLOC
207 The extent is allocated but not yet written.
209 .B FMR_OF_ATTR_FORK
210 This extent contains extended attribute data.
212 .B FMR_OF_EXTENT_MAP
213 This extent contains extent map information for the owner.
215 .B FMR_OF_SHARED
216 Parts of this extent may be shared.
218 .B FMR_OF_SPECIAL_OWNER
220 .I fmr_owner
221 field contains a special value instead of an inode number.
223 .B FMR_OF_LAST
224 This is the last record in the data set.
228 .I fmr_reserved
229 field will be set to zero.
231 .SS Owner values
232 Generally, the value of the
233 .I fmr_owner
234 field for non-metadata extents should be an inode number.
235 However, filesystems are under no obligation to report inode numbers;
236 they may instead report
237 .B FMR_OWN_UNKNOWN
238 if the inode number cannot easily be retrieved, if the caller lacks
239 sufficient privilege, if the filesystem does not support stable
240 inode numbers, or for any other reason.
241 If a filesystem wishes to condition the reporting of inode numbers based
242 on process capabilities, it is strongly urged that the
243 .B CAP_SYS_ADMIN
244 capability be used for this purpose.
246 The following special owner values are generic to all filesystems:
247 .RS 0.4i
249 .B FMR_OWN_FREE
250 Free space.
252 .B FMR_OWN_UNKNOWN
253 This extent is in use but its owner is not known or not easily retrieved.
255 .B FMR_OWN_METADATA
256 This extent is filesystem metadata.
259 XFS can return the following special owner values:
260 .RS 0.4i
262 .B XFS_FMR_OWN_FREE
263 Free space.
265 .B XFS_FMR_OWN_UNKNOWN
266 This extent is in use but its owner is not known or not easily retrieved.
268 .B XFS_FMR_OWN_FS
269 Static filesystem metadata which exists at a fixed address.
270 These are the AG superblock, the AGF, the AGFL, and the AGI headers.
272 .B XFS_FMR_OWN_LOG
273 The filesystem journal.
275 .B XFS_FMR_OWN_AG
276 Allocation group metadata, such as the free space btrees and the
277 reverse mapping btrees.
279 .B XFS_FMR_OWN_INOBT
280 The inode and free inode btrees.
282 .B XFS_FMR_OWN_INODES
283 Inode records.
285 .B XFS_FMR_OWN_REFC
286 Reference count information.
288 .B XFS_FMR_OWN_COW
289 This extent is being used to stage a copy-on-write.
291 .B XFS_FMR_OWN_DEFECTIVE:
292 This extent has been marked defective either by the filesystem or the
293 underlying device.
296 ext4 can return the following special owner values:
297 .RS 0.4i
299 .B EXT4_FMR_OWN_FREE
300 Free space.
302 .B EXT4_FMR_OWN_UNKNOWN
303 This extent is in use but its owner is not known or not easily retrieved.
305 .B EXT4_FMR_OWN_FS
306 Static filesystem metadata which exists at a fixed address.
307 This is the superblock and the group descriptors.
309 .B EXT4_FMR_OWN_LOG
310 The filesystem journal.
312 .B EXT4_FMR_OWN_INODES
313 Inode records.
315 .B EXT4_FMR_OWN_BLKBM
316 Block bit map.
318 .B EXT4_FMR_OWN_INOBM
319 Inode bit map.
321 .SH RETURN VALUE
322 On error, \-1 is returned, and
323 .I errno
324 is set to indicate the error.
325 .SH ERRORS
326 The error placed in
327 .I errno
328 can be one of, but is not limited to, the following:
330 .B EBADF
331 .IR fd
332 is not open for reading.
334 .B EBADMSG
335 The filesystem has detected a checksum error in the metadata.
337 .B EFAULT
338 The pointer passed in was not mapped to a valid memory address.
340 .B EINVAL
341 The array is not long enough, the keys do not point to a valid part of
342 the filesystem, the low key points to a higher point in the filesystem's
343 physical storage address space than the high key, or a nonzero value
344 was passed in one of the fields that must be zero.
346 .B ENOMEM
347 Insufficient memory to process the request.
349 .B EOPNOTSUPP
350 The filesystem does not support this command.
352 .B EUCLEAN
353 The filesystem metadata is corrupt and needs repair.
354 .SH VERSIONS
356 .B FS_IOC_GETFSMAP
357 operation first appeared in Linux 4.12.
358 .SH CONFORMING TO
359 This API is Linux-specific.
360 Not all filesystems support it.
361 .SH EXAMPLES
363 .I io/fsmap.c
364 in the
365 .I xfsprogs
366 distribution for a sample program.
367 .SH SEE ALSO
368 .BR ioctl (2)