9 Commits
Author SHA1 Message Date
Ernesto A. Fernández a10279ce66 Plug buffer head leak on submit for the 7.2 kernel
Starting with the 7.2 kernel, buffer heads no longer lose a reference
during the submit-wait procedure. I didn't notice that change, so now I
would expect all buffer heads to be leaking. The fact that I still can't
tell the difference makes me believe, like I mentioned before, that
somehow the kernel is still disposing of the buffer heads. Either way,
fix this.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2026-08-14 21:35:59 -03:00
Ernesto A. Fernández 7711c4704e Plug buffer head leak for checkpoint mapping block
This is the last of the three leaks discovered by Valgrind in the
userland port. During the loop that writes ephemeral objects to disk,
checkpoint mapping blocks are created and kept around for multiple
iterations until they are full. At that point, the cpm buffer head gets
released and we get a new one. All that looks correct, but no release
happens after the final loop. Fix it.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2026-08-14 19:41:33 -03:00
Ernesto A. Fernández 9619c53686 Plug leak of ip bitmap buffer head
This is the second bug discovered by valgrind in the userland tool. The
buffer head for the ip bitmap gets nulled after joining the transaction,
but we still hold a reference to it (the transactions takes a new one).
Fix it.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2026-08-14 19:21:41 -03:00
Ernesto A. Fernández f5b71842e6 Plug checkpoint buffer head leak during mount
Work on the userland port with valgrind has reavealed a number of
longstanding leaks in the kernel driver. For some reason I always
assumed that kmemleak would eventually notice issues with buffer heads,
but it clearly doesn't. This bug in particular must have been around
since the very beginning: all buffer heads for checkpoint descriptor
blocks get leaked during mount.

Since nobody noticed the problem so far, I'm guessing that the kernel
somehow disposes of abandoned buffer heads. Perhaps it has something to
do with the old issue from patch 517dd221df ("Forcefully free buffer
heads on transaction commit")? Either way, fix the leak and get the code
to match the userland port.

One thing to note: before pushing this patch I tried asking Github
Copilot to look for leaks in the super.c file and it spotted the problem
immediately. It seems to have become far more useful since the last time
I tried it, so once I'm done fixing the current crop of leaks I should
try and see what it can find.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2026-08-14 18:48:48 -03:00
Ernesto A. Fernández 83205caaad Fix build for RHEL 9.8
Kexianliang reports that the build is broken once more for RHEL:

  https://github.com/linux-apfs/linux-apfs-rw/issues/123

Fix it.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2026-08-12 17:16:00 -03:00
Ernesto A. Fernández 8a37600293 0.3.21
This release adds support for the upcoming 7.2 kernel version.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2026-08-12 15:42:53 -03:00
Ernesto A. Fernández f0e88ca368 Switch to bh_submit() for kernel 7.2
The build is broken for the 7.2 release candidate. The reason is that
submit_bh() is gone now, replaced with a new bh_submit() that also takes
the I/O end handler as an argument. Adjust apfs_submit_bh() to these
changes.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2026-07-14 19:38:12 -03:00
Ernesto A. Fernández 96d5614d04 Put macos macro guards around uintmax_t typedef
I've been trying to build the driver for userland lately. Most of the
code can be left as it is as long as I spoof the kernel headers and
everything else that the kernel provides. The one exception is my
uintmax_t typedef, because macos is very determined to provide its own
definition for that type. I guess that makes sense, given that the lzfse
code came from Apple in the first place.

Anyway, none of this is a problem for the driver, but since I want to
keep both codebases identical, put the same macro guards in here.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2026-06-24 17:41:11 -03:00
Ernesto A. Fernández b2418aaaa7 Clarify the modprobe situation in the README
I notice a lot of traffic to the following issue:

  https://github.com/linux-apfs/linux-apfs-rw/issues/96

Recent distro kernels seem to ship with a built-in libcrc32c, so the
modprobe step described in the README is not necessary and will fail. I
assumed it would be clear to the users that they can just keep going
when that happens, but some of them are being extra careful, which is
probably the right attitude to have when dealing with a filesystem.

Rephrase the README to make it clear that the modprobe step is not
usually needed anymore.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2026-05-27 16:58:08 -03:00
9 changed files with 56 additions and 20 deletions

No files matched your search

+7 -2
View File
@@ -57,10 +57,15 @@ Now you can just cd to the linux-apfs-rw directory and run::
make
The resulting module is the apfs.ko file. Before you can use it you must insert
it into the kernel, as well as its dependencies. Again as root::
it into the kernel. Again as root::
insmod apfs.ko
If you get an "unknown symbol in module" error, run::
modprobe libcrc32c
insmod apfs.ko
and then try the insmod command again.
Mount
=====
+27 -4
View File
@@ -44,10 +44,33 @@ static inline bool sb_rdonly(const struct super_block *sb) { return sb->s_flags
#endif
/* Compatibility wrapper around submit_bh() */
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 0, 0) || RHEL_VERSION_GE(9, 2)
#define apfs_submit_bh(op, op_flags, bh) submit_bh(op | op_flags, bh)
#if LINUX_VERSION_CODE >= KERNEL_VERSION(7, 2, 0)
#define apfs_submit_bh(op, op_flags, bh) \
do { \
put_bh(bh); \
if (op == REQ_OP_READ) \
bh_submit(bh, op | op_flags, bh_end_read); \
else \
bh_submit(bh, op | op_flags, bh_end_write); \
} while (0)
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(6, 0, 0) || RHEL_VERSION_GE(9, 2)
#define apfs_submit_bh(op, op_flags, bh) \
do { \
if (op == REQ_OP_READ) \
bh->b_end_io = end_buffer_read_sync; \
else \
bh->b_end_io = end_buffer_write_sync; \
submit_bh(op | op_flags, bh); \
} while (0)
#else
#define apfs_submit_bh(op, op_flags, bh) submit_bh(op, op_flags, bh)
#define apfs_submit_bh(op, op_flags, bh) \
do { \
if (op == REQ_OP_READ) \
bh->b_end_io = end_buffer_read_sync; \
else \
bh->b_end_io = end_buffer_write_sync; \
submit_bh(op, op_flags, bh); \
} while (0)
#endif
/*
@@ -1324,7 +1347,7 @@ __apfs_getblk(struct super_block *sb, sector_t block)
block -= nxi->nx_tier2_bno;
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 7, 0)
#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 7, 0) && !RHEL_VERSION_GE(9, 8)
return __getblk_gfp(info->blki_bdev, block, sb->s_blocksize, __GFP_MOVABLE);
#else
return bdev_getblk(info->blki_bdev, block, sb->s_blocksize, __GFP_MOVABLE);
+1 -1
View File
@@ -1,5 +1,5 @@
PACKAGE_NAME="linux-apfs-rw"
PACKAGE_VERSION="0.3.20"
PACKAGE_VERSION="0.3.21"
BUILT_MODULE_NAME[0]="apfs"
DEST_MODULE_LOCATION[0]="/extra"
-2
View File
@@ -2298,7 +2298,6 @@ int apfs_nonsparse_dstream_read(struct apfs_dstream_info *dstream, void *buf, si
if (!buffer_uptodate(bh)) {
get_bh(bh);
lock_buffer(bh);
bh->b_end_io = end_buffer_read_sync;
apfs_submit_bh(REQ_OP_READ, 0, bh);
}
}
@@ -2365,7 +2364,6 @@ void apfs_nonsparse_dstream_preread(struct apfs_dstream_info *dstream)
if (!buffer_uptodate(bh)) {
get_bh(bh);
lock_buffer(bh);
bh->b_end_io = end_buffer_read_sync;
apfs_submit_bh(REQ_OP_READ, 0, bh);
}
brelse(bh);
-1
View File
@@ -556,7 +556,6 @@ int __apfs_write_begin(struct file *file, struct address_space *mapping, loff_t
if (buffer_mapped(bh) && !buffer_uptodate(bh)) {
get_bh(bh);
lock_buffer(bh);
bh->b_end_io = end_buffer_read_sync;
apfs_submit_bh(REQ_OP_READ, 0, bh);
wait_on_buffer(bh);
if (!buffer_uptodate(bh)) {
+2
View File
@@ -70,7 +70,9 @@ typedef int64_t lzfse_offset;
typedef int32_t lzfse_offset;
#endif
#if !defined(__APPLE__) || !defined(__MACH__)
typedef uint64_t uintmax_t;
#endif
/*! @abstract Decoder state object for lzfse compressed blocks. */
typedef struct {
+1
View File
@@ -308,6 +308,7 @@ static int apfs_write_single_ip_bitmap(struct super_block *sb, char *bitmap, u32
err = apfs_transaction_join(sb, bh);
if (err)
goto fail;
put_bh(bh);
bh = NULL;
spaceman->sm_ip_bmaps[idx].dirty = false;
+15 -9
View File
@@ -343,8 +343,8 @@ static int apfs_read_main_super(struct super_block *sb)
xid = le64_to_cpu(msb_raw->nx_o.o_xid);
for (i = 0; i < desc_blocks; ++i) {
struct apfs_nx_superblock *desc_raw;
bool ignore;
brelse(desc_bh);
desc_bh = apfs_sb_bread(sb, desc_base + i);
if (!desc_bh) {
apfs_err(sb, "unable to read checkpoint descriptor");
@@ -352,18 +352,24 @@ static int apfs_read_main_super(struct super_block *sb)
}
desc_raw = (struct apfs_nx_superblock *)desc_bh->b_data;
ignore = false;
if (le32_to_cpu(desc_raw->nx_magic) != APFS_NX_MAGIC)
continue; /* Not a superblock */
ignore = true; /* Not a superblock */
if (le64_to_cpu(desc_raw->nx_o.o_xid) <= xid)
continue; /* Old */
ignore = true; /* Old */
if (!apfs_obj_verify_csum(sb, desc_bh))
continue; /* Corrupted */
ignore = true; /* Corrupted */
xid = le64_to_cpu(desc_raw->nx_o.o_xid);
msb_raw = desc_raw;
bno = desc_base + i;
brelse(bh);
bh = desc_bh;
if (ignore) {
brelse(desc_bh);
} else {
xid = le64_to_cpu(desc_raw->nx_o.o_xid);
msb_raw = desc_raw;
bno = desc_base + i;
brelse(bh);
bh = desc_bh;
}
desc_raw = NULL;
desc_bh = NULL;
}
+3 -1
View File
@@ -640,6 +640,9 @@ static int apfs_write_ephemeral_objects(struct super_block *sb)
}
data_len += obj_blkcnt;
}
brelse(cpm_bh);
cpm = NULL;
cpm_bh = NULL;
/*
* The checkpoint superblock can't be set until the very end of the
@@ -721,7 +724,6 @@ static int apfs_transaction_commit_nx(struct super_block *sb)
clear_buffer_dirty(bh);
get_bh(bh);
lock_buffer(bh);
bh->b_end_io = end_buffer_write_sync;
apfs_submit_bh(REQ_OP_WRITE, REQ_SYNC, bh);
}
list_for_each_entry_safe(bhi, tmp, &nx_trans->t_buffers, list) {