ioctl_getfsmap(2)
β¨ NAME β¨
ioctl_getfsmap β retrieve the physical layout of the filesystem π
That's it, that's the whole vibe. It's the ioctl(2) call you hit up when you need to know where your bytes are physically chillin'. No cap.
π LIBRARY π
Standard C library (libc, -lc) π«Ά
π SYNOPSIS π
#include <linux/fsmap.h> /* Definition of FS_IOC_GETFSMAP,
FM?_OF_*, and *FMR_OWN_* constants */
#include <sys/ioctl.h>
int ioctl(int fd, FS_IOC_GETFSMAP, struct fsmap_head * arg);
Like, two includes and one call. She's low maintenance bestie. πΈ
π¦ DESCRIPTION π¦
So this ioctl(2) operation pulls up the physical extent mappings for a filesystem. What's it for, you ask? It can be used to:
- discover which files are mapped to a physical block π
- examine free space
- find known bad blocks
...among other things. It's giving detective energy. π
The sole argument to this operation should be a pointer to a single struct fsmap_head:
struct fsmap {
__u32 fmr_device; /* Device ID */
__u32 fmr_flags; /* Mapping flags */
__u64 fmr_physical; /* Device offset of segment */
__u64 fmr_owner; /* Owner ID */
__u64 fmr_offset; /* File offset of segment */
__u64 fmr_length; /* Length of segment */
__u64 fmr_reserved[3]; /* Must be zero */
};
struct fsmap_head {
__u32 fmh_iflags; /* Control flags */
__u32 fmh_oflags; /* Output flags */
__u32 fmh_count; /* # of entries in array incl. input */
__u32 fmh_entries; /* # of entries filled in (output) */
__u64 fmh_reserved[6]; /* Must be zero */
struct fsmap fmh_keys[2]; /* Low and high keys for
the mapping search */
struct fsmap fmh_recs[]; /* Returned records */
};
The two fmh_keys array elements specify the lowest and highest reverse-mapping key for which the application would like physical mapping information. A reverse mapping key consists of the tuple (device, block, owner, offset). And why are owner and offset even in the key? Because some filesystems support sharing physical blocks between multiple files, and therefore may return multiple mappings for a given physical block. Sharing is caring, ig. π€
Filesystem mappings get copied into the fmh_recs array, which immediately follows the header data. π₯
π Fields of struct fsmap_head π
- The
fmh_iflagsfield is a bit mask passed to the kernel to alter the output. No flags are currently defined, so the caller must set this value to zero. Zero. Periodt. π« - The
fmh_oflagsfield is a bit mask of flags set by the kernel concerning the returned mappings. IfFMH_OF_DEV_Tis set, then thefmr_devicefield represents adev_tstructure containing the major and minor numbers of the block device. - The
fmh_countfield contains the number of elements in the array being passed to the kernel. If this value is0,fmh_entrieswill be set to the number of records that would have been returned had the array been large enough; no mapping information will be returned. It's a little "how many would you have needed, bestie" vibe check. β¨ - The
fmh_entriesfield contains the number of elements in thefmh_recsarray that contain useful information. - The
fmh_reservedfields must be set to zero. (Not optional, not negotiable π)
π Keys π
The two key records in fsmap_head.fmh_keys specify the lowest and highest extent records in the keyspace that the caller wants returned.
A filesystem that can share blocks between files likely requires the tuple (device, physical, owner, offset, flags) to uniquely index any filesystem mapping record. Classic non-sharing filesystems might be able to identify any record with only (device, physical, flags).
For example, if the low key is set to (8:0, 36864, 0, 0, 0), the filesystem will only return records for extents starting at or above 36 KiB on disk. If the high key is set to (8:0, 1048576, 0, 0, 0), only records below 1 MiB will be returned. π―
The format of fmr_device in the keys must match the format of the same field in the output records, as defined below. By convention, the field fsmap_head.fmh_keys[0] must contain the low key and fsmap_head.fmh_keys[1] must contain the high key for the operation.
For convenience, if fmr_length is set in the low key, it will be added to fmr_block or fmr_offset as appropriate. The caller can take advantage of this subtlety to set up subsequent calls by copying fsmap_head.fmh_recs[fsmap_head.fmh_entries - 1] into the low key. The function fsmap_advance (defined in linux/fsmap.h) provides this functionality. Paging through your extents, one call at a time β it's giving scrolling. π
π Fields of struct fsmap π
fmr_deviceβ uniquely identifies the underlying storage device. If theFMH_OF_DEV_Tflag is set in the header'sfmh_oflagsfield, this field contains adev_tfrom which major and minor numbers can be extracted. If the flag is not set, this field contains a value that must be unique for each unique storage device.fmr_physicalβ contains the disk address of the extent in bytes.fmr_ownerβ contains the owner of the extent. This is an inode number unlessFMR_OF_SPECIAL_OWNERis set in thefmr_flagsfield, in which case the value is determined by the filesystem. See the section below about owner values for more details.fmr_offsetβ contains the logical address in the mapping record in bytes. This field has no meaning if theFMR_OF_SPECIAL_OWNERorFMR_OF_EXTENT_MAPflags are set infmr_flags. πfmr_lengthβ contains the length of the extent in bytes.fmr_flagsβ a bit mask of extent state flags. The bits are:FMR_OF_PREALLOCβ The extent is allocated but not yet written.FMR_OF_ATTR_FORKβ This extent contains extended attribute data.FMR_OF_EXTENT_MAPβ This extent contains extent map information for the owner.FMR_OF_SHAREDβ Parts of this extent may be shared.FMR_OF_SPECIAL_OWNERβ Thefmr_ownerfield contains a special value instead of an inode number.FMR_OF_LASTβ This is the last record in the data set. π
- The
fmr_reservedfield will be set to zero.
π Owner values π
Generally, the value of the fmr_owner field for non-metadata extents should be an inode number. However β and this is the tea β β filesystems are under no obligation to report inode numbers; they may instead report FMR_OWN_UNKNOWN if the inode number cannot easily be retrieved, if the caller lacks sufficient privilege, if the filesystem does not support stable inode numbers, or for any other reason. If a filesystem wishes to condition the reporting of inode numbers based on process capabilities, it is strongly urged that the CAP_SYS_ADMIN capability be used for this purpose.
The following special owner values are generic to all filesystems:
FMR_OWN_FREEβ Free space. ποΈFMR_OWN_UNKNOWNβ This extent is in use but its owner is not known or not easily retrieved.FMR_OWN_METADATAβ This extent is filesystem metadata.
XFS can return the following special owner values:
XFS_FMR_OWN_FREEβ Free space.XFS_FMR_OWN_UNKNOWNβ This extent is in use but its owner is not known or not easily retrieved.XFS_FMR_OWN_FSβ Static filesystem metadata which exists at a fixed address. These are the AG superblock, the AGF, the AGFL, and the AGI headers.XFS_FMR_OWN_LOGβ The filesystem journal.XFS_FMR_OWN_AGβ Allocation group metadata, such as the free space btrees and the reverse mapping btrees.XFS_FMR_OWN_INOBTβ The inode and free inode btrees.XFS_FMR_OWN_INODESβ Inode records.XFS_FMR_OWN_REFCβ Reference count information.XFS_FMR_OWN_COWβ This extent is being used to stage a copy-on-write.XFS_FMR_OWN_DEFECTIVE:β This extent has been marked defective either by the filesystem or the underlying device. π
ext4 can return the following special owner values:
EXT4_FMR_OWN_FREEβ Free space.EXT4_FMR_OWN_UNKNOWNβ This extent is in use but its owner is not known or not easily retrieved.EXT4_FMR_OWN_FSβ Static filesystem metadata which exists at a fixed address. This is the superblock and the group descriptors.EXT4_FMR_OWN_LOGβ The filesystem journal.EXT4_FMR_OWN_INODESβ Inode records.EXT4_FMR_OWN_BLKBMβ Block bit map.EXT4_FMR_OWN_INOBMβ Inode bit map.
π₯ RETURN VALUE π₯
On error, -1 is returned, and errno is set to indicate the error.
π ERRORS π
The error placed in errno can be one of, but is not limited to, the following:
EBADFβfdis not open for reading.EBADMSGβ The filesystem has detected a checksum error in the metadata.EFAULTβ The pointer passed in was not mapped to a valid memory address.EINVALβ The array is not long enough, the keys do not point to a valid part of the filesystem, the low key points to a higher point in the filesystem's physical storage address space than the high key, or a nonzero value was passed in one of the fields that must be zero.ENOMEMβ Insufficient memory to process the operation.EOPNOTSUPPβ The filesystem does not support this operation.EUCLEANβ The filesystem metadata is corrupt and needs repair.
Not the whole list, bestie β just "one of, but is not limited to" the following. Other errors can absolutely show up and crash the party. π
π STANDARDS π
- Linux. π§
- Not all filesystems support it. (The audacity, honestly.)
β³ HISTORY β³
- Linux 4.12. Vintage but make it iconic. β¨
π§ EXAMPLES π§
See io/fsmap.c in the xfsprogs distribution for a sample program.
π SEE ALSO π
Okay so that's the whole page bestie π go map some extents, stay safe, and remember: reserved fields are always zero, no exceptions, no excuses. π
This was the ioctl_getfsmap(2) page from Linux man-pages 6.7, dated 2024-03-03. Love her, read her, cite her. π¦π
π¦ where this page comes from
- ubuntu noble: manpages-dev 6.7-2
- π the page's license: GPL-2.0-or-later
- Copyright (c) 2017, Oracle. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms