Linux – Are the attributes set by the “chattr” command implemented as extended attributes

linux

The chattr command is used to set some attributes for a file (for example: append only (a), immutable (i), etc.).

Are these attributes implemented as extended attributes, or are they their own category of attributes?

Best Answer

No, those flags are set using the FC_IOC_SETFLAGS ioctl() (also known as EXT2_IOC_SETFLAGS for ext* file systems, and corresponding one for other filesystems).

In most file systems that support it, that translates to one bit map of the inode structure.

For instance, in ext4 and several other file systems, that's the i_flags inode structure member (a 32 bit integer).

Some foreign (non-Linux) filesystems like Apple's HFS+ have a similar concept with equivalent flags and the FC_IOC_SETFLAGS ioctl does the translation there.

When using the stat command (which dumps the inode structure) in debugfs on ext* file systems, that's the Flags: number in the output:

 $ sudo debugfs /dev/vda
 debugfs: stat /tmp/file
 Inode: 1835209   Type: regular    Mode:  0644   Flags: 0x80010
 [...]

0x80000 is FS_EXTENT_FL (e in lsattr output), 0x10 is FS_IMMUTABLE_FL (i).

The new statx() system call can also return (part of) that information (not all systems at this time (early 2019) will have a recent enough version of the GNU libc (2.28 or newer) to be able to call it easily though).

On a recent system, you can use xfs_io's statx command as an interface to the statx() system call:

$ xfs_io -rc 'statx -r' /tmp/a
[...]
stat.attributes = 0x10
[...]

(here 0x10 is STATX_ATTR_IMMUTABLE, the FS_EXTENT_FL one doesn't have a corresponding statx() flag).

Related Question