12.5. Managing Shared Filesystem Concerns
Ext4, XFS, and Btrfs differ in their metadata, but they share the administrative questions that follow: how to limit a user, group, or directory tree; how sparse files and reflinks affect apparent disk usage; how to preserve ownership, extended attributes, and ACLs; and which check or repair tool applies to the filesystem in front of you.
12.5.1. User, Group, and Project Quotas
A quota limits the blocks or inodes a user, group, or project tree can consume. Ext4 and XFS support all three models; Btrfs uses quota groups (qgroups) per subvolume. Quotas have a soft limit, a hard limit, and a grace period. Usage may exceed the soft limit for the grace period; the hard limit is immediate. Inode limits use the same soft/hard structure.
Create a separate ext4 filesystem for the quota exercise. This overwrites
/dev/vdb3; the quota feature creates the quota inodes in the
superblock, and mounting with usrquota,grpquota enables accounting and
enforcement:
# mkfs.ext4 -q -L handbook-quota -O quota /dev/vdb3
# mkdir -p /mnt/quota
# mount -o usrquota,grpquota /dev/vdb3 /mnt/quota
All three commands are silent. findmnt shows the active quota options:
# findmnt -no OPTIONS /mnt/quota
rw,relatime,quota,usrquota,grpquota
Set a user quota for john: 2 MiB soft, 4 MiB hard, 10 inodes soft, and 20
inodes hard. setquota takes block limits in 1 KiB units by default:
# setquota -u john 2048 4096 10 20 /mnt/quota
The command is silent. Create a directory owned by john so the report has a nonzero starting point:
# mkdir -p /mnt/quota/john-data
# chown john:john /mnt/quota/john-data
Both commands are silent. repquota reports current usage and limits:
# repquota -ug /mnt/quota
*** Report for user quotas on device /dev/vdb3
Block grace time: 7days; Inode grace time: 7days
Block limits File limits
User used soft hard grace used soft hard grace
----------------------------------------------------------------------
root -- 20 0 0 2 0 0
john -- 4 2048 4096 1 10 20
*** Report for group quotas on device /dev/vdb3
Block grace time: 7days; Inode grace time: 7days
Block limits File limits
Group used soft hard grace used soft hard grace
----------------------------------------------------------------------
root -- 20 0 0 2 0 0
john -- 4 0 0 1 0 0
The -- in the status column means usage is below both limits; john's
single file is the directory just created. Attempt to write 6 MiB as john:
$ dd if=/dev/zero of=/mnt/quota/john-data/big.bin bs=1M count=6
dd: error writing '/mnt/quota/john-data/big.bin': Disk quota exceeded
4+0 records in
3+0 records out
4190208 bytes (4.2 MB, 4.0 MiB) copied, 0.00776281 s, 540 MB/s
dd exits 1. The kernel enforces the 4 MiB hard limit; the file ends at
about 4 MiB. The report now shows the soft limit exceeded and the grace
timer running:
# repquota -ug /mnt/quota
*** Report for user quotas on device /dev/vdb3
Block grace time: 7days; Inode grace time: 7days
Block limits File limits
User used soft hard grace used soft hard grace
----------------------------------------------------------------------
root -- 20 0 0 2 0 0
john +- 4096 2048 4096 7days 2 10 20
*** Report for group quotas on device /dev/vdb3
Block grace time: 7days; Inode grace time: 7days
Block limits File limits
Group used soft hard grace used soft hard grace
----------------------------------------------------------------------
root -- 20 0 0 2 0 0
john -- 4096 0 0 2 0 0
The +- status means the user is over the soft limit and inside the
grace period. If the grace period expires, the soft limit is enforced as
a hard limit. Raising the user limit allows work to continue:
# setquota -u john 20480 30720 0 0 /mnt/quota
The command is silent. For a group quota, create a group, add john, and
make a setgid directory so new files inherit the group. The setgid bit is
the 2 in mode 2775:
# groupadd -f design
# usermod -aG design john
# mkdir -p /mnt/quota/team
# chgrp design /mnt/quota/team
# chmod 2775 /mnt/quota/team
All five commands are silent. groupadd -f succeeds whether or not the
group already exists. Set a 4 MiB group hard limit and write from john:
# setquota -g design 2048 4096 0 0 /mnt/quota
The command is silent. As john:
$ dd if=/dev/zero of=/mnt/quota/team/group.bin bs=1M count=6
dd: error writing '/mnt/quota/team/group.bin': Disk quota exceeded
4+0 records in
3+0 records out
4190208 bytes (4.2 MB, 4.0 MiB) copied, 0.0129383 s, 324 MB/s
The group report shows design over its soft limit:
# repquota -g /mnt/quota
*** Report for group quotas on device /dev/vdb3
Block grace time: 7days; Inode grace time: 7days
Block limits File limits
Group used soft hard grace used soft hard grace
----------------------------------------------------------------------
root -- 20 0 0 2 0 0
john -- 4096 0 0 2 0 0
design +- 4096 2048 4096 7days 2 0 0
The group quota applies to files whose group ID is design. The setgid
directory makes every new file in it inherit that group, and john's
membership in design supplies the group's write permission on the
directory. Group membership is read at session start, so a shell opened
before the usermod above does not have it: leave that shell and open a
new one before writing.
XFS supports the same user and group quotas, plus project quotas for a
directory tree. Project quotas are the useful model for a web root, a
container volume, or a shared build directory whose files may belong to
different users. Create the XFS filesystem's project quota state by
mounting with prjquota. The filesystem was created earlier; mount it
again:
# mount -o prjquota /dev/vdb4 /mnt/xfs
The mount is silent. Confirm the project quota option:
# findmnt -no OPTIONS /mnt/xfs
rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,prjquota
prjquota is listed among the active mount options. Now create the
directory that will form the project tree:
# mkdir -p /mnt/xfs/projects/alpha
The directory creation is silent. Mark the tree as project 42. The -s
option sets the project ID on the existing tree and makes new files
inherit it; -p supplies the path without editing /etc/projects:
# xfs_quota -x -c 'project -s -p /mnt/xfs/projects/alpha 42' /mnt/xfs
Setting up project 42 (path /mnt/xfs/projects/alpha)...
Processed 1 (/etc/projects and cmdline) paths for project 42 with recursion depth infinite (-1).
Set the project limit to 2 MiB soft and 4 MiB hard:
# xfs_quota -x -c 'limit -p bsoft=2m bhard=4m 42' /mnt/xfs
The command is silent. Give john write access and attempt a 6 MiB write:
# chown john:john /mnt/xfs/projects/alpha
The command is silent. As john:
$ dd if=/dev/zero of=/mnt/xfs/projects/alpha/big.bin bs=1M count=6
dd: error writing '/mnt/xfs/projects/alpha/big.bin': No space left on device
5+0 records in
4+0 records out
4194304 bytes (4.2 MB, 4.0 MiB) copied, 0.0172784 s, 243 MB/s
XFS reports quota exhaustion as ENOSPC (No space left on device), not
Disk quota exceeded; both mean the write hit the configured limit. The
project report shows usage and limits:
# xfs_quota -x -c 'report -p' /mnt/xfs
Project quota on /mnt/xfs (/dev/vdb4)
Blocks
Project ID Used Soft Hard Warn/Grace
---------- --------------------------------------------------
#0 8196 0 0 00 [--------]
#42 4096 2048 4096 00 [7 days]
| Tool | Use |
|---|---|
setquota -u USER SOFT HARD ISOF ISHARD MOUNT |
Set ext4 user block and inode limits. |
setquota -g GROUP ... |
Set ext4 group limits. |
repquota -ug MOUNT |
Report ext4 user and group usage. |
quotaon MOUNT, quotaoff MOUNT |
Enable or disable quota accounting when mount options do not do it automatically. |
xfs_quota -x -c 'project -s -p PATH ID' MOUNT |
Mark an XFS directory tree as a project. |
xfs_quota -x -c 'limit -p bsoft=... bhard=... ID' MOUNT |
Set XFS project limits. |
xfs_quota -x -c 'report -p' MOUNT |
Report XFS project usage. |
Btrfs does not use the ext4/XFS quota files. Its qgroup facility accounts
for subvolumes; it is enabled with btrfs quota enable and limited with
btrfs qgroup limit. Qgroup accounting is separate from the ext4 and
XFS quota tools and is not interchangeable with them. For a Btrfs
subvolume that needs a size boundary, use qgroups after testing the
accounting on a disposable filesystem.
12.5.2. Sparse Files, Reflinks, and Apparent Disk Usage
A sparse file has logical length without allocated blocks. truncate
creates one; reads of the holes return zeros and no data blocks are
allocated. As john on the Btrfs filesystem:
$ truncate -s 1G /mnt/btrfs/projects/sparse.img
The creation is silent. Inspect the inode:
$ stat -c "size=%s blocks=%b" /mnt/btrfs/projects/sparse.img
size=1073741824 blocks=0
size is the logical length in bytes; blocks is the number of 512-byte
blocks allocated. Zero blocks mean no data extents. du reports the
allocated size, while du --apparent-size reports the logical size:
$ du -h /mnt/btrfs/projects/sparse.img
0 /mnt/btrfs/projects/sparse.img
$ du -h --apparent-size /mnt/btrfs/projects/sparse.img
1.0G /mnt/btrfs/projects/sparse.img
A file can consume 1 GiB of address space and no disk space. Copying it
with a tool that does not preserve holes (for example, some archive
formats or a plain read/write copy) can turn the holes into allocated
zeros. cp --sparse=always and rsync --sparse preserve holes; tar
records holes in its archive format when the tool supports it.
A reflink is a shared extent between two files. Btrfs and XFS support
reflinks; ext4 does not. On Btrfs, copy a 16 MiB random file with
--reflink=always:
$ dd if=/dev/urandom of=/mnt/btrfs/projects/archive.bin bs=1M count=16 status=none
The write is silent. Create the reflink:
$ cp --reflink=always /mnt/btrfs/projects/archive.bin /mnt/btrfs/projects/archive-copy.bin
The reflink copy is also silent. sync completes the source file's
writeback so the extent reports below are stable:
# sync
The command prints nothing. stat reports the same logical and
allocated block counts for both files:
$ stat -c "%n size=%s blocks=%b" /mnt/btrfs/projects/archive.bin /mnt/btrfs/projects/archive-copy.bin
/mnt/btrfs/projects/archive.bin size=16777216 blocks=32768
/mnt/btrfs/projects/archive-copy.bin size=16777216 blocks=32768
The block counts do not show sharing. btrfs filesystem du does:
# btrfs filesystem du -s /mnt/btrfs/projects/archive.bin /mnt/btrfs/projects/archive-copy.bin
Total Exclusive Set shared Filename
16.00MiB 0.00B 16.00MiB /mnt/btrfs/projects/archive.bin
16.00MiB 0.00B 16.00MiB /mnt/btrfs/projects/archive-copy.bin
Each file has a 16 MiB total size, no exclusive extents, and 16 MiB of
shared extents. The pair consumes 16 MiB of physical space, not 32 MiB.
du still adds the two logical allocations and prints 32 MiB:
$ du -ch /mnt/btrfs/projects/archive.bin /mnt/btrfs/projects/archive-copy.bin
16M /mnt/btrfs/projects/archive.bin
16M /mnt/btrfs/projects/archive-copy.bin
32M total
The total is useful for quota and logical-capacity questions, not for
physical free-space questions. Use btrfs filesystem du or compsize
when the physical usage of shared or compressed data matters. XFS also
supports reflinks on a filesystem created with reflink=1, as the one in
this chapter was; cp --reflink=always on XFS produces the same shared
extent and the same logical-versus-physical distinction in du, while
ext4 refuses the copy entirely, as Section 12.5.3 shows.
| Command | Reports |
|---|---|
stat -c '%s %b' FILE |
Logical bytes and allocated 512-byte blocks. |
du -h FILE |
Allocated size, counted per file. |
du -h --apparent-size FILE |
Logical size. |
du -ch FILE... |
Total allocated size, not deduplicated for reflinks. |
btrfs filesystem du -s FILE... |
Total, exclusive, and shared extents. |
compsize FILE |
Compressed and uncompressed physical usage. |
12.5.3. Preserving Extended Attributes and ACLs
Extended attributes are named values attached to an inode. The user.
namespace is available to ordinary file owners; trusted., security.,
and system. namespaces require privilege or have kernel-defined
meanings. Access control lists are stored in the system.posix_acl_*
attributes and enforce per-user and per-group permissions beyond the
traditional mode bits.
Mount the ext4 practice filesystem again and prepare a directory owned by john:
# mount /dev/vdb1 /mnt/ext4
# mkdir -p /mnt/ext4/shared
# chown john:john /mnt/ext4/shared
All three commands are silent. As john, change to the shared directory;
the cd is silent:
$ cd /mnt/ext4/shared
$ printf "%s\n" "quarterly report" > report.txt
The write is silent. Set the attribute:
$ setfattr -n user.department -v finance report.txt
The attribute change is silent. Read the attribute back:
$ getfattr -d report.txt
# file: report.txt
user.department="finance"
Add an ACL entry giving nobody read access, then display the ACL:
$ setfacl -m u:nobody:r report.txt
The command is silent. Display the ACL:
$ getfacl report.txt
# file: report.txt
# owner: john
# group: john
user::rw-
user:nobody:r--
group::rw-
mask::rw-
other::r--
The lines beginning # file: and # owner: are part of getfacl's
output, not root prompts. The mask line limits the permissions of named
users and groups; setfacl recomputes it when an entry is added. The
named entry user:nobody:r-- grants read access, but the effective
permission is the intersection with the mask.
cp -a preserves mode, ownership where permitted, timestamps, xattrs,
and ACLs. Copy the file and read both attributes back:
$ cp -a report.txt report-copy.txt
The copy is silent. Read the attribute:
$ getfattr -d report-copy.txt
# file: report-copy.txt
user.department="finance"
Read the ACL:
$ getfacl report-copy.txt
# file: report-copy.txt
# owner: john
# group: john
user::rw-
user:nobody:r--
group::rw-
mask::rw-
other::r--
A plain cp creates a new file with the default mode and does not copy
the xattr or the named ACL entry:
$ cp report.txt report-plain.txt
The copy is silent. The plain copy has no user attribute:
$ getfattr -d report-plain.txt
getfattr prints nothing. The named ACL entry is gone as well:
$ getfacl report-plain.txt
# file: report-plain.txt
# owner: john
# group: john
user::rw-
group::rw-
other::r--
getfacl shows only the base entries, with no user:nobody:r--. This is
why backup and migration procedures use archive-preserving copies:
cp -a, rsync -aHAX, or tar --xattrs --acls, with the archive format
and restore path tested as well. Chapter 15 covers
those tools in the context of a restorable backup.
Ext4 rejects reflinks, which is a useful contrast with Btrfs:
$ cp --reflink=always report.txt reflink.txt
cp: failed to clone 'reflink.txt' from 'report.txt': Operation not supported
cp exits 1. --reflink=auto would fall back to a normal copy instead of
failing, which is convenient for portable scripts but hides whether the
operation actually shared extents. Use --reflink=always when the
procedure depends on sharing.
| Tool or option | Preserves |
|---|---|
cp -a |
Mode, ownership where permitted, timestamps, links, xattrs, ACLs. |
cp --preserve=all |
The same categories as -a, expressed explicitly. |
rsync -aHAX |
Archive mode plus hard links, ACLs, and xattrs. |
tar --xattrs --acls |
Stores xattrs and ACLs in the archive, if the format supports them. |
setfattr -n, getfattr -d |
Set and display extended attributes. |
setfacl -m, getfacl |
Set and display POSIX ACL entries. |
cp --reflink=always |
Requires filesystem reflink support; fails otherwise. |
12.5.4. Selecting the Correct Check and Repair Procedure
The check tools are not interchangeable, and the wrong one can make a bad situation worse. The first question is whether the filesystem is mounted read-write. A consistent offline check needs it unmounted; Btrfs scrub is the online verification tool. The second question is which filesystem it is, and the third is whether the problem is structural metadata, a data checksum, or a log that needs replay.
With the three filesystems still mounted, the tools refuse an unsafe check. Ext4 returns exit 8:
# e2fsck -fy /dev/vdb1
/dev/vdb1 is mounted.
e2fsck: Cannot continue, aborting.
XFS returns exit 1 and refuses to initialize its library:
# xfs_repair -n /dev/vdb4
xfs_repair: /dev/vdb4 contains a mounted and writable filesystem
fatal error -- couldn't initialize XFS library
Btrfs prints its check progress and then refuses:
# btrfs check --readonly /dev/vdb2
Opening filesystem to check...
ERROR: /dev/vdb2 is currently mounted, use --force if you really intend to check the filesystem
Do not use the suggested --force to work around the mount check. Unmount
the filesystem, or use btrfs scrub for the mounted case. The
btrfs check --force option on a mounted filesystem is not a supported
consistency check;
the filesystem is changing while the checker reads it.
Unmount the practice filesystems, then run each offline check. Ext4:
# umount /mnt/ext4
The unmount is silent. XFS:
# umount /mnt/xfs
The unmount is silent. The two Btrfs mount points:
# umount /mnt/btrfs-snapshots
# umount /mnt/btrfs
Both commands are silent. The quota practice filesystem from Section 12.5.1 is also still mounted:
# umount /mnt/quota
The unmount is silent. No separate check is shown for it; the ext4 procedure below is the one that applies. Check ext4:
# e2fsck -fn /dev/vdb1
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
handbook-data: 17/96000 files (0.0% non-contiguous), 15454/384000 blocks
Check XFS; selected output, with the phase detail omitted as in Section 12.3.3:
# xfs_repair -n /dev/vdb4
Phase 1 - find and verify superblock...
...
Phase 7 - verify link counts...
No modify flag set, skipping filesystem flush and exiting.
Check Btrfs. The phase progress on standard error is omitted from the summary below:
# btrfs check --readonly /dev/vdb2
Opening filesystem to check...
Checking filesystem on /dev/vdb2
UUID: ef5cbaf2-e010-4311-a625-7ae2a3b30084
found 26525696 bytes used, no error found
total csum bytes: 25600
total tree bytes: 311296
total fs tree bytes: 114688
total extent tree bytes: 49152
btree space waste bytes: 228186
file data blocks allocated: 42991616
referenced 75497472
The exit status is 0 for all three clean checks. btrfs check without
--readonly still defaults to read-only; the option makes the intent
explicit. --check-data-csum additionally verifies data checksums, which
is slower and expects the metadata to be sound. --repair is not part of
a routine check: the manual warns that no checker repairs every kind of
corruption, and a repair can make a damaged tree harder to recover.
| Situation | Tool and mode |
|---|---|
| ext4 unmounted, routine check | e2fsck -fn, then e2fsck -fy if errors are found. |
| ext4 mounted | Do not repair; unmount first. A read-only -n check is unreliable while mounted. |
| XFS unmounted, routine check | xfs_repair -n, then xfs_repair if repairs are needed. |
| XFS dirty log | Mount and unmount to replay; use xfs_repair -L only if replay is impossible. |
| Btrfs unmounted, routine check | btrfs check --readonly; add --check-data-csum for data checksums. |
| Btrfs mounted, checksum verification | btrfs scrub start -B or the periodic scrub service. |
| Btrfs structural damage | btrfs check first; --repair only with expert guidance and a backup. |
Figure 12.4. The check and repair path depends on the filesystem and on whether the filesystem is mounted. Conceptual; follow the filesystem-specific options in the text.
Boot-time checking is a separate path. Ext4's fsck.ext4 runs
e2fsck according to /etc/fstab's sixth field and the superblock's
mount-count and interval settings. XFS's fsck.xfs is a no-op
placeholder, and Btrfs's fsck.btrfs is also a no-op; their repairs are
manual, deliberate operations. A system that boots with a damaged XFS or
Btrfs filesystem may reach emergency mode rather than repair itself.
Keep rescue media with all three tool sets, and keep a current backup
before running a repair that can discard metadata.
The repair decision also depends on what failed. A data checksum error on
a Btrfs RAID1 filesystem is a scrub problem: the tree is sound and the
good copy can repair the bad one. A lost Btrfs device is a mount and
degraded-profile problem, not a btrfs check problem. A dirty XFS log is
a replay problem; -L is not the first response. An ext4 inode or bitmap
inconsistency is an e2fsck problem. Matching the tool to the failure is
more important than memorizing a single command sequence.