Difference between xattr and chattr

5,973

The attributes as handled by lsattr/chattr on Linux and some of which can be stored by quite a few file systems (ext2/3/4, reiserfs, JFS, OCFS2, btrfs, XFS, nilfs2, hfsplus...) and even queried over CIFS/SMB (when with POSIX extensions) are flags. Just bits than can be turned on or off to disable or enable an attribute (like immutable or archive...). How they are stored is file system specific, but generally as a 16/32/64 bit record in the inode.

The full list of flags is found on Linux native filesystems (ext2/3/4, btrfs...) though not all of the flags apply to all of FS, and for other non-native FS, Linux tries to map them to equivalent features in the corresponding file system. For instance the simmutable flag as stored by OSX on HFS+ file systems is mapped to the corresponding immutable flag in Linux chattr. What flag is supported by what file system is hardly documented at all. Often, reading the kernel source code is the only option.

Extended attributes on the other hand, as set with setfattr or attr on Linux store more than flags. They are attached to a file as well, and are key/value pairs that can be (both key and value) arbitrary arrays of bytes (though with limitation of size on some file systems).

The key can be for instance: system.posix_acl_access or user.rsync.%stat. The system namespace is reserved for the system (you wouldn't change the POSIX ACLs with setfattr, but more with setfacl, POSIX ACLs just happen to be stored as extended attributes at least on some file systems), while the user namespace can be used by applications (here rsync uses it for its --fake-super option, to store information about ownership or permissions when you're not superuser).

Again, how they are stored is filesystem specific. See WikiPedia for more information.

Share:
5,973
Cerberuzs
Author by

Cerberuzs

Updated on September 18, 2022

Comments

  • Cerberuzs
    Cerberuzs almost 2 years

    What is the relation and the difference between xattr and chattr? I want to know when I set a chattr attribute in Linux what is happening inside the Linux kernel and inode metadata.

    • Graeme
      Graeme over 10 years
      I wrote a long answer on a related question yesterday which might be helpful - unix.stackexchange.com/questions/118840/…. Note that the attr command is intended for XFS filesystems, the equivalent filesystem independent command is setfattr
  • Graeme
    Graeme over 10 years
    Do lsattr/chattr work for non ext filesystems? If not how would you change the flags on non ext systems on Linux? Also the attr man page says that it is written for XFS (even though it works for me on ext4). I actually spent a long time looking into this yesterday, good information is hard to come by. Please see - unix.stackexchange.com/questions/118840/…
  • Stéphane Chazelas
    Stéphane Chazelas over 10 years
    @Graeme, answers to those questions are in my answer. lsattr/chattr are for file flags. Not all flags are supported by all filesystems, for instance, some of them like compression won't work on ext4 but will work on btrfs.
  • Graeme
    Graeme over 10 years
    If you are interested, I wrote a script to test these flags on a few filesystems and put the results on my answer linked above. I had no success setting/reading any of them on reiserfs.