Skip to content

Commit 2dbff17

Browse files
committed
lib: Add LCFS_BUILD_TRACK_HARDLINKS for directory scans
lcfs_build() walks a directory tree via readdir()/fstatat() and never looks at st_ino/st_dev, so two paths that are hardlinked to the same inode on disk silently become two independent inodes with nlink=1 in the composefs image. This causes fs-verity digest mismatches against hardlink-aware tools and a mounted image that misreports nlink/size (composefs#444). Add an opt-in LCFS_BUILD_TRACK_HARDLINKS flag that keeps a (dev,ino) -> node hash table alive for the whole recursive walk (a link's two names can be anywhere under the root, not just siblings) and, on a repeat inode, wires up a hardlink via the existing lcfs_node_make_hardlink() instead of re-reading the file content. This is opt-in and not the default, matching composefs-rs's own mkcomposefs-compatible implementation, which requires the same --hardlinks flag to enable deduplication. Directories from scanning a real filesystem today rely on every path becoming an independent inode, and changing that by default would change digests for existing consumers (RHEL9, ostree, etc.) that build composefs images from directories. Tracking is also unconditional on st_nlink, i.e. it doesn't skip looking up a file just because its st_nlink report is 1: some filesystems (e.g. fuse-overlayfs, see containers/fuse-overlayfs#435) can misreport that field. Every non-directory type is tracked (regular files, symlinks, FIFOs, device nodes, sockets), since all of them are hardlinkable on disk (e.g. via `ln -P`) and composefs-rs's own scanner doesn't special-case regular files either. The one deliberate exception is a chardev with rdev=0: composefs represents an overlayfs whiteout this way, and the EROFS writer already rejects such an inode outright if it ends up with nlink>1 (a whiteout marks the absence of a file, so hardlinking two of them together is semantically invalid) -- but only when *loading* an image, not when writing one. Leave those untracked so a directory scan can't silently produce an image composefs itself would refuse to read back. Assisted-by: AI Signed-off-by: Colin Walters <walters@verbum.org>
1 parent 379f8ab commit 2dbff17

2 files changed

Lines changed: 128 additions & 4 deletions

File tree

‎libcomposefs/lcfs-writer.c‎

Lines changed: 126 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,7 @@ struct lcfs_node_s *lcfs_load_node_from_file(int dirfd, const char *fname,
786786
if (buildflags & ~(LCFS_BUILD_SKIP_XATTRS | LCFS_BUILD_USE_EPOCH |
787787
LCFS_BUILD_SKIP_DEVICES | LCFS_BUILD_COMPUTE_DIGEST |
788788
LCFS_BUILD_NO_INLINE | LCFS_BUILD_USER_XATTRS |
789-
LCFS_BUILD_BY_DIGEST)) {
789+
LCFS_BUILD_BY_DIGEST | LCFS_BUILD_TRACK_HARDLINKS)) {
790790
errno = EINVAL;
791791
return NULL;
792792
}
@@ -1504,14 +1504,117 @@ bool lcfs_node_dirp(struct lcfs_node_s *node)
15041504
return (node->inode.st_mode & S_IFMT) == S_IFDIR;
15051505
}
15061506

1507+
/* Key used to look up a previously seen inode by (device, inode number)
1508+
* while scanning a directory tree with LCFS_BUILD_TRACK_HARDLINKS. Only
1509+
* the dev/ino fields are significant for hashing/comparison; "node" is
1510+
* the payload (a borrowed pointer, owned by the tree being built). */
1511+
struct lcfs_hardlink_entry {
1512+
dev_t dev;
1513+
ino_t ino;
1514+
struct lcfs_node_s *node;
1515+
};
1516+
1517+
static size_t hardlink_ht_hasher(const void *d, size_t n)
1518+
{
1519+
const struct lcfs_hardlink_entry *v = d;
1520+
return ((size_t)v->dev * 31 + (size_t)v->ino) % n;
1521+
}
1522+
1523+
static bool hardlink_ht_comparator(const void *d1, const void *d2)
1524+
{
1525+
const struct lcfs_hardlink_entry *v1 = d1;
1526+
const struct lcfs_hardlink_entry *v2 = d2;
1527+
1528+
return v1->dev == v2->dev && v1->ino == v2->ino;
1529+
}
1530+
15071531
/* Shared state threaded through the recursive directory walk performed by
15081532
* lcfs_build_internal(). This is kept separate from the plain "buildflags"
15091533
* argument so that state which needs to persist across the *entire* walk
15101534
* (as opposed to a single directory level) has somewhere to live. */
15111535
struct lcfs_build_ctx {
15121536
int buildflags;
1537+
/* (dev,ino) -> struct lcfs_node_s*, or NULL if LCFS_BUILD_TRACK_HARDLINKS
1538+
* was not requested. Every non-directory type is tracked here, since
1539+
* regular files, symlinks, FIFOs, device nodes and sockets can all be
1540+
* hardlinked; directories cannot. */
1541+
Hash_table *hardlink_map;
15131542
};
15141543

1544+
/* Load the node for dirfd/fname, detecting and wiring up hardlinks to a
1545+
* previously seen (device, inode) pair when hardlink tracking is enabled.
1546+
* On a repeat inode this avoids re-reading file content: it creates a
1547+
* lightweight placeholder node and turns it into a hardlink to the
1548+
* existing node with lcfs_node_make_hardlink(), mirroring how the
1549+
* --from-file dumpfile hardlink fixups work in tools/mkcomposefs.c. */
1550+
static struct lcfs_node_s *lcfs_load_node_for_build(struct lcfs_build_ctx *ctx,
1551+
int dirfd, const char *fname,
1552+
int buildflags)
1553+
{
1554+
struct stat sb;
1555+
struct lcfs_hardlink_entry key;
1556+
struct lcfs_hardlink_entry *existing;
1557+
struct lcfs_hardlink_entry *entry;
1558+
struct lcfs_node_s *node;
1559+
1560+
if (ctx->hardlink_map == NULL) {
1561+
return lcfs_load_node_from_file(dirfd, fname, buildflags);
1562+
}
1563+
1564+
if (fstatat(dirfd, fname, &sb, AT_SYMLINK_NOFOLLOW) < 0) {
1565+
return NULL;
1566+
}
1567+
1568+
/* Directories can never be hardlinked, and are handled by recursing
1569+
* instead of via this function; every other type (regular files,
1570+
* symlinks, FIFOs, device nodes, sockets) is a candidate.
1571+
*
1572+
* The one exception is a chardev with rdev=0: composefs represents
1573+
* overlayfs whiteouts this way, and the EROFS writer rejects such an
1574+
* inode outright if it ends up with nlink>1 (a whiteout marks the
1575+
* absence of a file, so hardlinking two of them together is
1576+
* semantically invalid). Leave those untracked so they keep today's
1577+
* independent-inode-per-path behavior instead of failing the build. */
1578+
if (S_ISDIR(sb.st_mode) || (S_ISCHR(sb.st_mode) && sb.st_rdev == 0)) {
1579+
return lcfs_load_node_from_file(dirfd, fname, buildflags);
1580+
}
1581+
1582+
key = (struct lcfs_hardlink_entry){ .dev = sb.st_dev, .ino = sb.st_ino };
1583+
existing = hash_lookup(ctx->hardlink_map, &key);
1584+
if (existing != NULL) {
1585+
node = lcfs_node_new();
1586+
if (node == NULL) {
1587+
return NULL;
1588+
}
1589+
node->inode.st_mode = sb.st_mode;
1590+
lcfs_node_make_hardlink(node, existing->node);
1591+
return node;
1592+
}
1593+
1594+
node = lcfs_load_node_from_file(dirfd, fname, buildflags);
1595+
if (node == NULL) {
1596+
return NULL;
1597+
}
1598+
1599+
entry = malloc(sizeof(struct lcfs_hardlink_entry));
1600+
if (entry == NULL) {
1601+
lcfs_node_unref(node);
1602+
errno = ENOMEM;
1603+
return NULL;
1604+
}
1605+
entry->dev = sb.st_dev;
1606+
entry->ino = sb.st_ino;
1607+
entry->node = node;
1608+
if (hash_insert(ctx->hardlink_map, entry) == NULL) {
1609+
free(entry);
1610+
lcfs_node_unref(node);
1611+
errno = ENOMEM;
1612+
return NULL;
1613+
}
1614+
1615+
return node;
1616+
}
1617+
15151618
static struct lcfs_node_s *lcfs_build_internal(int dirfd, const char *fname,
15161619
struct lcfs_build_ctx *ctx,
15171620
char **failed_path_out)
@@ -1525,7 +1628,7 @@ static struct lcfs_node_s *lcfs_build_internal(int dirfd, const char *fname,
15251628
const char *failed_subpath = NULL;
15261629
int errsv;
15271630

1528-
node = lcfs_load_node_from_file(dirfd, fname, buildflags);
1631+
node = lcfs_load_node_for_build(ctx, dirfd, fname, buildflags);
15291632
if (node == NULL) {
15301633
errsv = errno;
15311634
goto fail;
@@ -1595,7 +1698,7 @@ static struct lcfs_node_s *lcfs_build_internal(int dirfd, const char *fname,
15951698
continue;
15961699
}
15971700

1598-
n = lcfs_load_node_from_file(dfd, de->d_name, buildflags);
1701+
n = lcfs_load_node_for_build(ctx, dfd, de->d_name, buildflags);
15991702
if (n == NULL) {
16001703
errsv = errno;
16011704
failed_subpath = de->d_name;
@@ -1633,8 +1736,27 @@ struct lcfs_node_s *lcfs_build(int dirfd, const char *fname, int buildflags,
16331736
struct lcfs_build_ctx ctx = {
16341737
.buildflags = buildflags,
16351738
};
1739+
struct lcfs_node_s *node;
1740+
int errsv;
1741+
1742+
if (buildflags & LCFS_BUILD_TRACK_HARDLINKS) {
1743+
ctx.hardlink_map = hash_initialize(0, NULL, hardlink_ht_hasher,
1744+
hardlink_ht_comparator, free);
1745+
if (ctx.hardlink_map == NULL) {
1746+
errno = ENOMEM;
1747+
return NULL;
1748+
}
1749+
}
16361750

1637-
return lcfs_build_internal(dirfd, fname, &ctx, failed_path_out);
1751+
node = lcfs_build_internal(dirfd, fname, &ctx, failed_path_out);
1752+
errsv = errno;
1753+
1754+
if (ctx.hardlink_map != NULL) {
1755+
hash_free(ctx.hardlink_map);
1756+
}
1757+
1758+
errno = errsv;
1759+
return node;
16381760
}
16391761

16401762
size_t lcfs_node_get_n_xattr(struct lcfs_node_s *node)

‎libcomposefs/lcfs-writer.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ enum {
2626
LCFS_BUILD_NO_INLINE = (1 << 4),
2727
LCFS_BUILD_USER_XATTRS = (1 << 5), /* Only read user.* xattrs */
2828
LCFS_BUILD_BY_DIGEST = (1 << 6), /* Refer to basedir files by fs-verity digest */
29+
LCFS_BUILD_TRACK_HARDLINKS =
30+
(1 << 7), /* Detect and dedup hardlinked files during directory scan */
2931
};
3032

3133
enum lcfs_format_t {

0 commit comments

Comments
 (0)