This chapter describes extended attributes (EAs), which allow arbitrary metadata, in the form of name-value pairs, to be associated with file i-nodes. EAs were added to Linux in version 2.6.
EAs are used to implement access control lists (Chapter 17) and file capabilities (Chapter 39). However, the design of EAs is general enough to allow them to be used for other purposes as well. For example, EAs could be used to record a file version number, information about the MIME type or character set for the file, or (a pointer to) a graphical icon.
EAs are not specified in SUSv3. However, a similar feature is provided on a few other UNIX implementations, notably the modern BSDs (see extattr(2)) and Solaris 9 and later (see fsattr(5)).
EAs require support from the underlying file system. This support is provided in Btrfs, ext2, ext3, ext4, JFS, Reiserfs, and XFS.
Support for EAs is optional for each file system, and is controlled by kernel configuration options under the File systems menu. EAs are supported on Reiserfs since Linux 2.6.7.
EAs have names of the form namespace.name. The namespace component serves to separate EAs into functionally distinct classes. The name component uniquely identifies an EA within the given namespace.
Four values are supported for namespace: user, trusted, system, and security. These four types of EAs are used as follows:
User EAs may be manipulated by unprivileged processes, subject to file permission checks: to retrieve the value of a user EA requires read permission on the file; to change the value of a user EA requires write permission. (Lack of the required permission results in an EACCES
error.) In order to associate user EAs with a file on ext2, ext3, ext4, or Reiserfs file systems, the underlying file system must be mounted with the user_xattr option:
$mount -o user_xattr
device directory
Trusted EAs are like user EAs in that they can be manipulated by user processes. The difference is that a process must be privileged (CAP_SYS_ADMIN
) in order to manipulate trusted EAs.
System EAs are used by the kernel to associate system objects with a file. Currently, the only supported object type is an access control list (Chapter 17).
Security EAs are used to store file security labels for operating system security modules, and to associate capabilities with executable files (File Capabilities). Security EAs were initially devised to support Security-Enhanced Linux (SELinux, http://www.nsa.gov/research/selinux/).
An i-node may have multiple associated EAs, in the same namespace or in different namespaces. The EA names within each namespace are distinct sets. In the user and trusted namespaces, EA names can be arbitrary strings. In the system namespace, only names explicitly permitted by the kernel (e.g., those used for access control lists) are allowed.
JFS supports another namespace, os2, that is not implemented in other file systems. The os2 namespace is provided to support legacy OS/2 file-system EAs. A process doesn’t need to be privileged in order to create os2 EAs.
From the shell, we can use the setfattr(1) and getfattr(1) commands to set and view the EAs on a file:
$ touch tfile
$ setfattr -n user.x -v "The past is not dead." tfile
$ setfattr -n user.y -v "In fact, it's not even past." tfile
$ getfattr -n user.x tfile
Retrieve value of a single EA # file: tfile Informational message from getfattr user.x="The past is not dead." The getfattr command prints a blank line after each file’s attributes $ getfattr -d tfile Dump values of all user EAs # file: tfile user.x="The past is not dead." user.y="In fact, it's not even past."$ setfattr -n user.x tfile
Change value of EA to be an empty string$ getfattr -d tfile
# file: tfile user.x user.y="In fact, it's not even past."$ setfattr -x user.y tfile
Remove an EA$ getfattr -d tfile
# file: tfile user.x
One of the points that the preceding shell session demonstrates is that the value of an EA may be an empty string, which is not the same as an EA that is undefined. (At the end of the shell session, the value of user.x is an empty string and user.y is undefined.)
By default, getfattr lists only the values of user EAs. The -m option can be used to specify a regular expression pattern that selects the EA names that are to be displayed:
$ getfattr -m '
pattern
'
file
The default value for pattern is ^user\.
. We can list all EAs on a file using the following command:
$ getfattr -m - file