ioctl_iflags(2)

✨ ioctl_iflags(2) β€” the System Calls Manual page, but make it make sense πŸ’…

okay bestie, so this one is ioctl_iflags, and it lives in section 2 of the manual (that's the system calls section, iykyk). let's get into it 🫢


🌸 NAME 🌸

ioctl_iflags β€” the ioctl() operations that deal with inode flags, bestie


πŸ¦‹ DESCRIPTION πŸ¦‹

so like, a bunch of Linux filesystems are out here supporting the whole concept of inode flags β€” these are attributes that literally change the semantics of your files and directories. not just metadata vibes, actual behavior changes. and you can read them and change them with two ioctl(2) operations:

int attr;
fd = open("pathname", ...);

ioctl(fd, FS_IOC_GETFLAGS, &attr);  /* Place current flags
                                       in 'attr' */
attr |= FS_NOATIME_FL;              /* Tweak returned bit mask */
ioctl(fd, FS_IOC_SETFLAGS, &attr);  /* Update flags for inode
                                       referred to by 'fd' */

and if you're not trying to write C at 2am, the shell commands lsattr(1) and chattr(1) are the friendly little interfaces to these same two operations β€” they let you look at and change the inode flags attached to a file. iconic behavior honestly.

here's the full flag lineup (with the little letter that lsattr(1) and chattr(1) use to show it, because they're extra like that):

  • FS_APPEND_FL 'a' β€” the file can only be opened with the O_APPEND flag. and yes, this restriction applies even to the superuser. only a privileged process (CAP_LINUX_IMMUTABLE) can set or clear this attribute. no cap. πŸ‘‘
  • FS_COMPR_FL 'c' β€” store the file in a compressed format on disk. heads up though: most of the mainstream filesystem implementations do not support this flag. one exception is btrfs(5).
  • FS_DIRSYNC_FL 'D' (since Linux 2.6.0) β€” write directory changes synchronously to disk. this gives you semantics equivalent to the mount(2) MS_DIRSYNC option, but on a per-directory basis instead of the whole mount. can only be applied to directories. πŸ’«
  • FS_IMMUTABLE_FL 'i' β€” the file is immutable: no changes are permitted to the file contents or its metadata (permissions, timestamps, ownership, link count, and so on). and again β€” this restriction applies even to the superuser. only a privileged process (CAP_LINUX_IMMUTABLE) can set or clear this attribute.
  • FS_JOURNAL_DATA_FL 'j' β€” enable journaling of file data on ext3(5) and ext4(5) filesystems. on a filesystem that's journaling in ordered or writeback mode, a privileged (CAP_SYS_RESOURCE) process can set this flag to turn on journaling of data updates on a per-file basis.
  • FS_NOATIME_FL 'A' β€” don't update the file's last access time when the file is accessed. this can give you I/O performance benefits for applications that genuinely do not care about the accuracy of that timestamp. it's functionally similar to the mount(2) MS_NOATIME flag, but per-file. slay for your performance. πŸ”₯
  • FS_NOCOW_FL 'C' (since Linux 2.6.39) β€” the file will not be subject to copy-on-write updates. this only does anything on filesystems that support copy-on-write semantics, like Btrfs. see chattr(1) and btrfs(5).
  • FS_NODUMP_FL 'd' β€” don't include this file in backups made using dump(8).
  • FS_NOTAIL_FL 't' β€” supported only on Reiserfs. it disables the Reiserfs tail-packing feature, which is the thing that tries to pack small files (and the final fragment of larger files) into the same disk block as the file metadata.
  • FS_PROJINHERIT_FL 'P' (since Linux 4.5) β€” inherit the quota project ID. files and subdirectories will inherit the project ID of the directory. can only be applied to directories. 🧚
  • FS_SECRM_FL 's' β€” mark the file for secure deletion. okay so plot twist: this feature is not implemented by any filesystem, because securely erasing a file from a recording medium is, and I quote, surprisingly difficult. πŸ’€
  • FS_SYNC_FL 'S' β€” make file updates synchronous. for files, this makes all writes synchronous (as though all opens of the file were done with the O_SYNC flag). for directories, it does the same thing as the FS_DIRSYNC_FL flag.
  • FS_TOPDIR_FL 'T' β€” mark a directory for special treatment under the Orlov block-allocation strategy. see chattr(1) for the details. can only be applied to directories, and only actually has an effect for ext2, ext3, and ext4.
  • FS_UNRM_FL 'u' β€” allow the file to be undeleted if it's deleted. and another plot twist: this feature is not implemented by any filesystem either, since you can totally implement file-recovery mechanisms outside the kernel.

and here's the inheritance tea 🍡 β€” in most cases, when any of the above flags is set on a directory, that flag gets inherited by the files and subdirectories created inside that directory. the exceptions are:

  • FS_TOPDIR_FL, which is not inheritable at all
  • FS_DIRSYNC_FL, which is inherited only by subdirectories

🌈 STANDARDS 🌈

Linux. that's it, that's the standard. periodt.


πŸŽ€ NOTES πŸŽ€

to change a file's inode flags using the FS_IOC_SETFLAGS operation, the effective user ID of the caller must match the owner of the file, or the caller must have the CAP_FOWNER capability. no exceptions bestie.

and this one's important: the type of the argument you pass to the FS_IOC_GETFLAGS and FS_IOC_SETFLAGS operations is int * β€” notwithstanding the implication in the kernel source file include/uapi/linux/fs.h that the argument is long *. do not get got by that. πŸ™ˆ


πŸ‘‘ SEE ALSO πŸ‘‘

chattr(1), lsattr(1), mount(2), btrfs(5), ext4(5), xfs(5), xattr(7), mount(8)


and that's the whole page, bestie, you ate that 🫢 this lil writeup came from the Linux man-pages 6.7 release, dated 2023-10-31, and the page itself is ioctl_iflags(2). go touch some inode flags (responsibly). πŸ’–βœ¨πŸŒΈ

πŸ“¦ where this page comes from