110 Commits
Author SHA1 Message Date
Ernesto A. Fernández 0667dbff17 0.3.11
This release prevents deadlock if a single volume gets mounted and
unmounted at the same time. It also fixes two other (smaller) mount bugs
found while testing this one.

Support is added for the upcoming 6.11 kernel release, with a patch by
Woody Suwalski and a later patch of my own because things broke a second
time.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2024-09-12 20:20:49 -03:00
Ernesto A. Fernández af2ee526c0 Avoid double parsing of mount options
Originally all mount options were parsed inside apfs_fill_super(),
because I was following the example of other filesystems (such as ext2)
that have no subvolumes or snapshots. When I implemented those features,
I realized that some mount options would be needed early on mount, so
that sget() could use them to decide if the superblock is already
mounted.

My lazy solution at the time was to leave parse_options() as it was, but
also add some redundant code early on mount to read the needed options.
Just looking at the amount of TODOs in those functions makes it clear
that this was always very silly. And now that the locking during mount
has been simplified, this is causing a race, and I want to take that
stuff seriously.

The bug is that while parse_options() rereads the mount options needed
by sget(), there is nothing preventing another concurrent mount from
testing the superblock. Say the first mount is for volume 1: the volume
number gets briefly reset to the default value of zero, and if the other
mount actually wants volume 0, sget() will consider this a match.

Anyway, split parse_options() into two equally clean functions, one for
early mount and another to get called within apfs_fill_super(). I even
considered simply parsing all the options early during mount, but that
would require a few more changes because the nxi is not attached at that
point. Maybe some other time.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2024-09-11 16:54:47 -03:00
Ernesto A. Fernández 44f4b4f631 Don't redefine MAX() and MIN() for the 6.11 kernel
The build has become broken once again for the 6.11 kernel:

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

The problem is that the second release candidate introduced generic
MAX() and MIN() macros for everyone to use, and that crashes with our
own definitions. So, only define them for older kernels.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2024-08-30 20:25:44 -03:00
Ernesto A. Fernández b44d2a37bf Don't forget to initialize the omap cache spinlock
The omap cache has been in place for years, and it seems that its
spinlock was never actually initialized before use. I have no idea what
the consequences of this are, but for one it does cause lockdep to turn
itself off after every mount. Fix it.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2024-08-15 16:01:07 -03:00
Ernesto A. Fernández 4881d7c707 Don't return 0 on mount if sget() fails
For the previous patch I had to carefully test all the possible errors
during mount and unmount. I noticed that injecting an error inside
apfs_set_super() triggers npd. The cause is that the error code returned
by sget() gets swallowed and the kernel is allowed to think that the
mount succeeded.

This is not a new issue, it's been around since apfs_mount() was first
implemented. I never noticed because apfs_set_super() would only fail if
we ran out of anonymous devices. So fix it in a new commit.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2024-08-15 16:01:07 -03:00
Ernesto A. Fernández 2982c8655f Don't use the mount mutex so much
Repeatedly running generic/604 of xfstests eventually hangs the kernel.
This is a test that forces a mount to race with an unmount. The problem
is a standard deadlock: ->put_super() gets called while the s_umount
lock is held, and then starts a transaction, which takes nxs_mutex;
meanwhile ->mount() first takes nxs_mutex and then calls sget(), which
takes s_umount.

Before this patch, I had never really put much thought as to where
exactly nxs_mutex is needed. It's obviously the lock for the list of
mounted containers, but it's also used as a generic way to avoid
modifications to a container while another of its subvolumes is getting
mounted. It's always been obvious that I'm holding it for too long and
in too many places, so I want to finally put some effort into figuring
this out now, and leave a record here so that I don't forget.

The first obvious point is that the mutex can be dropped before calling
sget(), and retaken later. There should be no problem if another mount
called it at the same time, and it's enough to fix the bug. But putting
some thought here exposes an existing issue with this code: sget() may
actually test some superblock that is already half-unmounted and had its
info freed, so apfs_test_super() is risking npd or use-after-free.

Looking at other filesystems I realize that my mistake was freeing the
info inside ->put_super(), because at that point the super is still
listed in the fs_supers list for apfs. Instead ->kill_sb() should free
the info itself, after generic_shutdown_super() is done (which calls
->put_super()), and after kill_super_notify() has taken care of the
list. Since kill_super_notify() can't be called from inside a driver, a
little hack is needed to call it through kill_anon_super(). The cleanups
for apfs_mount() also need to be reconsidered, since ->put_super() is
only called by deactivate_locked_super() after the root dentry is set,
but the rest of ->kill_sb() runs either way.

Going back to the mutex: even if a race for sget() itself is not an
issue, dropping it there does have other consequences. The reference
count for the container superblock is increased before sget(), and it's
later checked on some initializations to see if this is the first mount.
That won't do anymore, because a second mount could increase the refcnt
before the mutex is retaken, and the first mount would never complete
its work. Use other fields for this check: in apfs_read_main_super() use
the blocksize (which is zero before the first mount sets it); for
apfs_set_nx_flags(), define a new flag to tell if the other flags have
been set.

This already tells us two functions that definitely need to be protected
by the mutex, so that they can complete their initializations before any
other mount comes to check them. The situation is similar for
apfs_first_read_omap(), which goes through the container's volume list
to check for previous reads. After going line by line through the mount
code, I'm fairly confident that the mutex only needs to be held for
apfs_attach_nxi() and these three functions. For unmount it's the same:
apfs_free_main_super() and apfs_put_omap() require the mutex. The second
will now unset the omap pointer for the volume atomically, so that it
can be checked by apfs_first_read_omap(); it is renamed accordingly.

So far this is all about the races between different mounts and umounts;
but we also don't want the mount to read inconsistent data from disk
because some existing mount happened to write to it. Back when the
driver could only mount one volume at a time, apfs_fill_super() didn't
need any locking, so when multiple mounts got implemented I decided to
just ban transactions during mounts entirely, by taking the nxs_mutex.
This double lock looks very awkward and is a factor on the deadlock we
are fixing, so let's clean that up now: use the read semaphore itself to
protect apfs_fill_super(). Of course this means that two mounts for the
same container can now run through this function at the same time, so be
careful with changes to the shared nxi: we've already dealt with this
issue for apfs_set_nx_flags(), but apfs_set_trans_buffer_limit() might
also be unsafe. Just in case, move it to apfs_read_main_super(), it
makes more sense that way anyway.

By the way, apfs_read_main_super() also reads from disk, but it only
does it on the first mount, so there is no risk of racing against a
transaction. There is one function that writes without a transaction,
namely apfs_make_super_copy(), but that function takes the nxs_mutex to
ensure that no mount will begin during the superblock update.

I think that covers everything? I'm really not sure if this writeup was
clear at all, I have a hard time reasoning about locks. I also put a
simpler list of the uses of nxs_mutex in a comment next to its
declaration, which should be more practical as a reference.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2024-08-15 16:01:06 -03:00
Ernesto A. Fernández 707d91b486 Undo code reordering in previous patch
I just picked up a patch to add support for the 6.11 kernel. I only did
a shallow review, and now I notice that the code was reordered and a
null buffer head is getting dereferenced. Revert that.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2024-07-30 17:55:14 -03:00
Woody Suwalski 3e5c902a9a Kernel 6.11: use folio for page ops (#74) 2024-07-30 17:23:17 -03:00
Ernesto A. Fernández 695455bfdb 0.3.10
Transactions are now committed regularly, so it should no longer be
strictly necessary to unmount the filesystem cleanly for writes to take
effect. It's still recommended, of course.

The release also fixes two bugs. One was a big performance regression
from 0.3.9, seen when deleting large numbers of files. The other was
that mounts would go read-only if two snapshots were attempted with the
same name.

Support is added for the new 6.10 kernel version, with a patch by Woody
Suwalski.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2024-07-15 15:13:17 -03:00
Woody Suwalski 071cdacc32 Small tweaks for building on kernel 6.10 2024-05-28 20:46:02 -03:00
Ernesto A. Fernández 57eb739f59 Don't abort a snapshot if the name is in use
Attempting to create two snapshots with the same name will abort a
transaction and leave the filesystem in a read-only state. From the
comment it seems that I was aware of this issue when I first implemented
snapshots, but I guess I didn't like the obvious solution in this patch
at the time, so I decided to leave it for later.

Since taking snapshots is not expected to be a bottleneck, just run the
query for the name once more ahead of time, as soon as the transaction
opens. Now if the name already exists we don't need to abort, because no
changes have taken place.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2024-05-28 18:54:56 -03:00
Ernesto A. Fernández f088d1a2e3 Set up a task to commit transactions
Currently my driver only commits transactions "synchronously", that is,
immediately following a user action that triggers it. This implies, in
particular, that the filesystem must get unmounted explicitly or else
data will almost certainly get dropped. While technically valid
behaviour, this is very weird and some people have complained about it
in the past.

So, stop ignoring requests for commit via apfs_transaction_commit() when
they are considered premature. Instead schedule a commit task, with a
small delay to avoid constant commits when the driver is still busy.

Note that the previous two patches were intended to help with this one:
the plan was that the commit task wouldn't need to hold a specific
volume superblock, and could instead work with the nx info. In the end
this proved too messy, so I'm just saving the volume superblock that
requested the commit in the transaction struct.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2024-05-23 18:46:25 -03:00
Ernesto A. Fernández 8bcaaebb89 Move trans_buffers_max from volume to container
The limit on the number of buffer heads in a transaction depends only on
the ram size, so it's the same for all volumes. Move it to the container
superblock info. Frankly this could just be a global variable, set when
the driver is first inserted, but there is no problem with recalculating
it on all mounts.

The point of this change, along with the previous patch, is that soon we
won't require holding some volume to start and commit transactions.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2024-05-21 18:26:18 -03:00
Ernesto A. Fernández 781e0024d2 Stop attempting to undo aborted transactions
When I first implemented transactions, my intention was to revert the
in-memory filesystem structures to a consistent state whenever we hit
the kind of errors that can be expected during normal operation. So I
always kept some stuff from the previous transaction around to copy it
back if needed. I never tested any of this though, so it most likely
never worked.

Later improvements mean that the kind of silly errors I had in mind
(things like ENOENT) are no longer supposed to happen during
transactions. The one exception here is ENOSPC, but that's only because
I haven't yet bothered to clean up the space reservations. The errors
that do happen are things like ENOMEM, EIO or EFSCORRUPTED, and I think
it's probably ok if the mount doesn't work perfectly after one of those.
Instead we force it into a read-only state, and let the user decide how
to proceed.

So, there is now little point in keeping my original half-baked plans
littering the transaction code. Remove all that stuff.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2024-05-20 20:06:53 -03:00
Ernesto A. Fernández 5d41bd7e64 Initialize transaction fields on first mount
The container superblock info keeps a list of inodes and buffers that
have been modified in the current transaction. This is a bit of a hack,
but so far I haven't figured out a way to get rid of it. The lists need
to be initialized before they are first used, and this is currently done
by apfs_transaction_start() itself, after checking if this is the first
start since the last commit. This works fine, but it's really weird,
since the list gets regularly reinitialized to "empty" after getting
emptied naturally by a commit. Initialization should really be separate
from all the code that actually works with the transaction.

Define a new apfs_transaction_init() to take care of this, and call it
from apfs_attach_nxi() which already takes care of the initialization of
all other container info fields.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2024-05-20 15:18:14 -03:00
Ernesto A. Fernández 4e9984c815 Rename the transaction limit constants
TRANSACTION_MAIN_QUEUE_MAX, TRANSACTION_BUFFERS_MAX and
TRANSACTION_STARTS_MAX were originally desfined inside transaction.c, so
I guess I didn't bother to give them the driver prefix. I don't expect
the names to collide with anything, but it's tidier to follow the same
naming conventions everywhere, so run

  sed -i 's/TRANSACTION_/APFS_TRANS_/' *.{c,h}

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2024-05-16 20:49:12 -03:00
Ernesto A. Fernández f1c9ab6c38 Adjust TRANSACTION_MAIN_QUEUE_MAX
The iozone automatic tests suffered a big performance regression between
0.3.8 and 0.3.9, in my case from around 14m to close to 20m. The problem
goes away if the newly introduced apfs_dstream_delete_front() is tweaked
to delete files in larger chunks. That said, I don't think my choice for
TRANSACTION_MAIN_QUEUE_MAX was ever based on anything, so it's better to
adjust that instead of coming up with some hack for file deletion alone.

After a lot of testing, I think I get the best results if I raise it
from 4096 to 10000. The iozone benchmark is now better than before, at
around 13m for me.

Of course this will mean that transactions commit less often. I should
look into putting that in a workqueue like the file deletion.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2024-05-16 20:32:39 -03:00
Ernesto A. Fernández d533b6be3a 0.3.9
This release fixes the build for the soon to be released 6.9 kernel.

Support is tentatively added for the new 0x80 incompatible volume
feature, which appears simple enough so far and isn't causing any
problems.

A console warning that showed up during compressed file reads on recent
kernels is now fixed.

Support is improved for tiny filesystems of under 100 MiB, though they
remain awkward to test due to fragmentation issues.

The behaviour of apfs_modified_by is tweaked to better match the
official driver.

Free queues should now be much harder to fill up. The most important
change here is that files are now deleted across multiple transactions.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2024-05-07 22:13:48 -03:00
Ernesto A. Fernández 02cb8bb8d9 Suuport volumes with the 0x80 incompatible feature
The official newfs_apfs now sets a 0x80 incompatible feature in all
volumes by default. The feature is undocumented as far as I know, but
after some testing it would seem that its effect is to set a flag in
preallocated extents. I can't imagine why such a feature would be
incompatible instead of read-only compatible, so there is a chance that
I'm missing something important, but for now enable support and let's
see if anything breaks.

By the way, no real changes are needed here because we don't preallocate
extents ourselves, and we put existing preallocated extents through the
same CoW as all the others. The only problem remaining is that writes
after the end of a file will not flip the "preallocation" flags that may
be set in previous blocks, but neither the official fsck nor the driver
seem to care about this. I just don't get what this flag is for, which
is a little scary of course.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2024-05-03 21:25:50 -03:00
Ernesto A. Fernández 6e7371ae02 Sync apfs_raw.h with the fsck again
I've recently noticed a number of new flags and added them to the fsck.
I heven't quite figured out most of them yet, but copy the information
here anyway. Also add checks for the new incompatible features, so that
they don't get accepted by default.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2024-05-03 20:54:26 -03:00
Ernesto A. Fernández 2a10ca61e6 Update the volume superblock with latest fields
If the latest newfs_apfs creates an image with the default options,
older versions of fsck_apfs complain about an unknown volume superblock
field called "apfs_doc_id_index_flags". Several such new fields appear
to have been added, and after some bit flipping I managed to retrieve
the names for all of them, except for "apfs_doc_id_tree_oid" which is
only a (probably correct) guess.

I have no plans for these fields right now, but document them all in
apfs_raw.h anyway, so that I don't forget.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2024-04-23 19:51:27 -03:00
Ernesto A. Fernández 48f6df6262 Improve error reporting for orphan cleanups
Orphan cleanup explicitly reports the error code from apfs_iget() when
it gets swallowed, but it's always ENODATA so it looks silly. Leave the
message in place but ignore the error code.

Instead, report all error codes returned by apfs_clean_orphans() before
they get discarded by apfs_orphan_cleanup_work(). That information might
actually be useful at some point.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2024-04-17 20:40:12 -03:00
Ernesto A. Fernández 33c9a99e96 Always update apfs_modified_by on unmount
The official reference is a little ambiguous about apfs_modified_by. On
its first introduction it says the following:

  If the implementation's information is already the last entry in this
  field, you can update the field as usual (creating a duplicate), or
  leave the field's value unmodified. Both behaviors are permitted.

So far the driver has followed these instructions and left the field
unmodified, but later on the reference describes the apfs_modified_by
timestamp as:

  The time that the program last modified this volume.

This seems to mean that the timestamp must always change, even if the
implementation in use has not. More importantly, on the matter of the
cloneinfo xid, the reference says the following:

  When unmounting a volume, the value of this field is set to the latest
  transaction identifier, the same as the apfs_modified_by field.

So apfs_modified_by must always get updated on unmount, or so it seems.
I looked at older checkpoints for a few official images, and indeed they
always seem to do this on unmount, and never before. Always by shifting.

Stop lawyering about the specification and just copy what the official
driver does.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2024-04-17 19:09:38 -03:00
Ernesto A. Fernández ca493f9586 Don't give up when a free queue gets full
I keep running into situations where too much fragmentation gets a free
queue full, and the filesystem goes read-only with the "free queue has
too many nodes" message. I've been patching this up by trying to flush
more often, but in the end it's best to force an extra flush whenever we
run out of space.

Rework apfs_free_queue_insert_nocache() so that it checks for space
before actually inserting anything. If we are out of room, flush and
start again. Make this last-resort flush stronger with a new "force"
argument for apfs_flush_free_queue(), otherwise it might still be
insufficient. Also, stop preserving multiple transactions in the queues,
I don't really see the point and it makes all this stuff much worse.

With these changes I'm hoping that free queues will no longer fill up
in practice, but it's still possible in theory, so leave an error
message in place to identify this issue quickly.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2024-04-12 17:24:04 -03:00
Ernesto A. Fernández 212cc6ef63 Make apfs_query_found_extent() check ids as well
I've been trying to run tests with a small image of around 100 MiB. I
wasn't expecting any problems, but surprisingly a lot of the tests fail.
The difference with larger containers appears to be that my hack to
avoid fragmentation (allocating data and metada in different chunks)
does not work when you only have one chunk. So this bug actually affects
any image, it's just easier to trigger here.

So, as for the bug itself: when working with large dstream xattrs (like
my apfs/002 test does), I regularly hit the "extent is not tail - bug!"
error. The problem is one that I've faced many times before with my
messy query code: queries looking for an extent without knowing it's
exact logical address can't be "exact" so they don't reliably return
ENODATA when they find nothing of value.

To deal with this, the extent code always checks the retrieved record by
calling apfs_query_found_extent(), but that only checks the record type.
In this particular case, the query is finding a proper file extent
record, but it belongs to a different dstream. I originally didn't think
such things could happen, but that has proved wrong more than once. From
now on, make apfs_query_found_extent() check the extent id as well.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2024-04-11 16:36:13 -03:00
Ernesto A. Fernández fa6d1d3986 Check if fq root is empty before reading first key
After the last patch I'm regularly getting the following error message
in the console:

  APFS (800010g): index out of bounds (0 of 0) (apfs_node_locate_key:598)

What happens here is that the container xid is set to 1 by the mkfs, but
the ip fq is empty so the oldest xid is set to 0. So when the driver
first starts a transaction, apfs_flush_free_queue() will believe that
there is an actual "0" transaction to flush, and end up trying to update
the oldest xid field for the empty queue.

This is all a bit odd but not a real problem, as soon as the fq starts
getting used all the flush checks will work as expected. The annoying
message comes from apfs_free_queue_oldest_xid(), which directly calls
apfs_node_locate_key() for the first key in the root node, without
verifying if such a key even exists. Avoid that.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2024-04-05 19:09:24 -03:00
Ernesto A. Fernández 076e715042 Preserve only one transaction in the ip free queue
Currently apfs_flush_free_queue() preserves several transactions in both
the ip and main free queue. This is a problem for tiny filesystems (5
MiB, for example) because the ip only has 6 blocks and inevitably uses 2
per transaction, getting full almost immediately. Allow more recent
transactions to get flushed for the ip.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2024-04-05 16:10:31 -03:00
Ernesto A. Fernández 38f9100760 Get rid of kmap_atomic()
As mentioned in the code, my implementation of apfs_compress_readpage()
was mostly copied from ext4_read_inline_page() because it was simple
enough and I didn't know what I was doing. As it turns out, I picked the
wrong function to copy: ext4_read_inline_page() only reads in-memory
data and doesn't go to sleep. We do read from disk, and we do go to
sleep, so newer kernels flood the console with warnings.

Replace kmap_atomic() with kmap(). The only expected negative effect of
this change would be worse performance on 32-bit archs, but I haven't
even tested those in years. The alternative kmap_local_page() is not
available in old kernels.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2024-03-27 21:09:50 -03:00
Ernesto A. Fernández cc320ed9ea Provide an explanation for forced read-only mounts
The driver requires setting an explicit "readwrite" mount option for an
actual writable mount to take place, otherwise the mount is silently
switched to read-only. The point is to prevent users that don't
understand the risks from potentially corrupting their data. Hopefully
this would push them to check the readme, but some might not even notice
that we have one (for example, if they got the driver as a distro
package). Try to explain the situation in the console.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2024-03-26 20:47:48 -03:00
Ernesto A. Fernández f50c8f2084 Open block devices as files with Linux 6.9
The first 6.9 release candidate is out, and bdev_open_by_path() is gone.
We are no longer supposed to work with block device handles, we must use
a normal open file struct instead. Update the code following what was
done to btrfs in upstream commit 9ae061cf2a46 ("btrfs: port device
access to file").

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2024-03-26 13:49:24 -03:00
Ernesto A. Fernández 21983fb418 Define SLAB_MEM_SPREAD flag for Linux 6.9
The first 6.9 release candidate is out, and the SLAB_MEM_SPREAD flag is
no longer available for kmem_cache_create(). Apparently it has been a
no-op for a while, so the least invasive way to fix the build is to just
define it as zero.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2024-03-26 13:48:32 -03:00
Ernesto A. Fernández 514a2164d8 Split orphan cleanup among multiple transactions
Gilgamesh reports via email that the following iozone benchmark is
failing:

  ./iozone -a -i 0 -r 1M -n 1g -g 1g -f /mnt/apfs/test

I've tried this myself on a small (3 GiB) filesystem, and it is the
commit after the unlink that fails, complaining about the main free
queue being full:

  APFS (800010g): free queue has too many nodes (348 > 347) (apfs_free_queue_insert_nocache:849)

The problem is that random writes to a big file create a lot of extents,
and when the file is deleted they all get dumped to the main free queue
as separate records. The queue gets full and the commit fails.

Stop forcing file deletion to happen in a single transaction. Instead,
set up a workqueue whenever an orphan inode gets evicted, and leave the
cleanup for another time. Also allow the cleanup to execute only
partially (deleting the first extents like the official driver) and
reschedule itself (again via inode eviction).

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2024-03-22 21:20:27 -03:00
Ernesto A. Fernández b8fe780c71 Don't leave stale record data on failed queries
When a query fails with ENODATA, the caller may still need to work with
the result (for instance to insert something in that location), but it
should never be confused for an actual valid record. Some changes I'm
working on trigger this problem regularly for apfs_query_found_extent(),
making it check stale records and report success. I need that function
to be reliable, so always clear the key and value lengths from queries
on failure.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2024-03-18 18:45:37 -03:00
Ernesto A. Fernández 3f9d529bcb 0.3.8
This release reworks ephemeral objects to support larger containers of
up to ~7.8 TiB, and to allow the creation of checkpoint mapping blocks.

Also the build is no longer broken for the new 6.8 kernel, thanks to a
patch by Woody Suwalski.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2024-03-11 00:32:25 -03:00
Woody Suwalski 60ef1e9991 Building on kernel 6.8
[eafer: moved bdev handle to the container superblock]
2024-03-10 20:58:42 -03:00
Ernesto A. Fernández 0313a0fee1 Rework handling of ephemeral objects
In pretty much all containers I've encountered so far, every objects has
a single block. There is an annoying exception though: containers with a
size between ~6.7 TiB and ~7.8 TiB have a multiblock spaceman. I have no
simple way to deal with this because my transactions currently handle
ephemeral objects very roughly, ignoring all the cpm metadata and just
updating each block in the expected header location.

Instead of trying to hack this in somehow, start doing the right thing
as explained in the official reference and keep all ephemeral objects in
memory at all times. So, when the first transaction starts, go through
the cpm metadata and read each object along with its size and oid. When
a new object gets created (or an old one gets deleted), all changes
happen in memory. On commit time, checksums are updated and all objects
get written to disk, but nothing neads to be read.

So this is all much cleaner, and I took this opportunity to support
multiple cpm blocks as well.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2024-03-01 21:12:56 -03:00
Ernesto A. Fernández 784dd43146 Fix typo in previous commit
It's annoying to have to make a new commit just for this, but I had
already pushed the bad one...

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2024-02-22 16:01:46 -03:00
Ernesto A. Fernández 7df84d2173 Mention fusion drives in the README
Fusion drives are not yet supported, but the README does not mention
this. Fix it.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2024-02-20 20:04:21 -03:00
Ernesto A. Fernández fbf052e1e9 Check container features as soon as possible
Failure to mount fusion drives currently gets reported in a confusing
way:

  https://github.com/linux-apfs/linux-apfs-rw/issues/39#issuecomment-1947466697

The problem is that all feature checks are currently run at the same
time for both the container and the volume, after the volume superblock
has been mapped. Fusion drives never get that far, we can't even work
with the container's object map.

Split the feature checks into two functions, one for the container and
one for the volume. Leave the call for the volume in the same place, but
move the one for the container into apfs_map_main_super().

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2024-02-20 19:46:42 -03:00
Ernesto A. Fernández 41af006115 0.3.7
This is a small release that fixes the dkms build, recently broken by
the implementation of version reporting. From now on I will run a dkms
build before each new tag.

The release also lifts the ban on filesystems with half-written backup
superblocks, a problem that some users appear to have encountered
already.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2024-01-18 20:07:43 -03:00
Ernesto A. Fernández cba2045422 Report dkms version when git is not available
Some users might build the driver from the tarball that github produces
on release, so the reported git commit would be an empty string. Instead
extract the version from the dkms file and use that, but add a question
mark so that I know that it might not be accurate.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2024-01-18 19:35:20 -03:00
Ernesto A. Fernández 5e133a2ca5 Fix version header generation for dkms
The dkms build fails for the latest release:

  https://github.com/linux-apfs/linux-apfs-rw/pull/60

The problem is that dkms does not call my Makefile at all, so the
version header is never generated. Dkms does have a PRE_BUILD hook
though, so use that to call a script that takes care of this.

From now on, I must always remember to run a dkms build before a new
release.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2024-01-17 22:36:05 -03:00
Ernesto A. Fernández 9ae07db606 Don't refuse to mount a bad backup superblock
The backup superblock is only needed to locate the actual superblock, so
it doesn't matter if it's corrupted beyond the first sector, which the
disk is supposed to write atomically. If a crash happened at the right
moment during unmount, any implementation might end up with a
half-written superblock where the checksum doesn't add up, but that's
perfectly fine.

If this is encountered during mount, just report it and proceed. This is
not a theoretical concern, someone appears to have run into the problem
already:

  https://techsparx.com/linux/ubuntu/mount-apfs.html

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2024-01-08 21:22:08 -03:00
Ernesto A. Fernández 10557edf23 0.3.6
This release heavily reworks the node defragmentation and split logic to
fix a handful of rarely triggered data corruption bugs. The new version
is easier to understand and hopefully more robust.

Support is added for containers with a size of up to ~6.7 TiB, improving
the previous maximum of ~1.3 TiB. The next release will probably keep
pushing this limit.

Changes are made to the way that subvolume "devices" are reported. Now
we are closer to matching the behaviour of btrfs and userland tools like
Nautilus are far less confused.

The driver version information is now provided by both the on-disk
filesystem metadata and by modinfo. This may be useful for responding to
bugs at some point.

A few small issues with snapshot reporting have been addressed, as well
as an mmap-related warning in recent kernel versions.

Finally, support is added for the 6.7 kernel version (released today),
contributed by Woody Suwalski.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2024-01-07 20:53:51 -03:00
Ernesto A. Fernández bcedfe1752 Read error before setting error pointer to null
I'm in a rush to get the tag out today and I'm making a mess. The
previous patch set an error pointer to null in apfs_node_split(), but I
didn't notice that the actual error would get read later. Reorder this.
I usually would just squash this into the previous patch, but in my rush
I have already pushed it.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2024-01-07 19:10:40 -03:00
Ernesto A. Fernández 8b2287d91f Don't free error pointers in apfs_node_split()
We mistakenly free an error pointer if apfs_create_node() happens to
fail. Set the node to null before the cleanup, but I guess I should try
to avoid error pointers in the future.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2024-01-07 19:03:09 -03:00
Woody Suwalski 5dc836ad96 Support for kernel 6.7
[eafer: fixed some issues with folios and timestamps]
Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2024-01-07 16:27:45 -03:00
Ernesto A. Fernández fe13bc9636 Don't refresh queries pointing to deleted nodes
Running generic/013 for a few minutes still leaves the filesystem in a
corrupted state, with a missing inode record. This is another regression
caused by my big rework of the node split logic. When a single-record
node is needed but the parent update fails with -EAGAIN,
apfs_create_single_rec_node() leaves the query pointing to the aborted
new node. The refresh does not change this, so the inode record ends up
abandoned in this ghost node.

In then end all of this is caused by the weird way I originally designed
the query struct, which forces me to always keep a more or less valid
leaf query instead of just nuking the whole thing and starting over. I'd
like to change this eventually, but for now just adjust the cleanup for
apfs_create_single_rec_node().

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2024-01-06 01:48:44 -03:00
Ernesto A. Fernández af1491622d Avoid filename normalizations issues on refresh
The previous patch introduced a regression in generic/013, seen when the
test is allowed to run for 20 minutes or so. The result is a corrupted
filesystem where one inode appears to have too many dentries, and
another inode has too few to balance.

The problem is that query refreshes just rerun the query once and expect
the correct result, but dentry queries for an existing record actually
need to be called repeatedly to deal with hash collisions. For
normalization-sensitive filesystems (which I use for most of my tests)
"collisions" are constant because there is no hash. So if a split
happens during a dentry deletion, chances are very high that the refresh
will end up pointing to the wrong record.

Don't add awkward code to deal with this to apfs_query_refresh().
Instead transform the multiple query from apfs_dentry_lookup() into a
regular exact query before returning, setting the key to the
unnormalized name. The refresh code will have no trouble working with
that.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2024-01-04 23:22:34 -03:00
Ernesto A. Fernández e7983cb3fe Restart btree modifications after a node split
I'm encountering some rare failures in generic/075. The problem happens
when a node split is triggered by inserting a record into the first
position. The parent needs to get updated of course, but we've lost the
reference to it after the split, so the filesystem becomes corrupted.

The reality is that this stuff has always been too hacky, and new corner
cases keep popping up. Stop trying to guess which queries need to know
and might have forgotten their ancestors: just restart the whole btree
operation after any split, refreshing the query with a simple rerun.
This has to be the safest possible approach, and I don't expect it to
hurt performance, since node splits should never be a bottleneck.

Big changes are needed to get it to work though, because the b-tree must
be in a consistent state whenever the query gets refreshed. There are
two different considerations here. First, no split can take place unless
the parent has enough room to index the new child node; otherwise the
parent split would trigger a query refresh while half the child's
records are orphaned. To sum up, every cascade of node splits must
happen from root to leaf.

The second consideration is that, even if a node has enough room, no
updates related to the actual requested operation (the leaf record
insertion/deletion/replacement) can take place at any level until all
splits are done. A different policy would mean that a query refresh
might become necessary halfway through the operation, and so the leaf
record could be found to be unexpectedly missing, or covered by the
wrong index key.

So at a given level: the split, if needed, always comes first, followed
by the function call for parent updates, and only then the node gets
modified. After a split an EAGAIN error gets thrown, which then bubbles
back up to the leaf (either through the splits themselves, or through
the parent update calls), so only a single split can happen for the
whole b-tree on each refresh iteration - always at the highest possible
level in the split cascade that would have been triggered before this
patch. The btree, of course, remains valid on EAGAIN, and the leaf
records remain the same.

Inside apfs_node_split() itself, no changes to the old node can be done
until the new one is safely inserted in the b-tree. If that triggers a
split at a higher level, then the new node must be properly cleaned up
(making use of the recent changes to apfs_delete_node()) and, as usual,
EAGAIN must get bubbled up.

A similar cleanup needs to be take place in the one other function that
creates non-root nodes: apfs_create_single_rec_node(). The new
single-record node must be repeatedly destroyed until the parent manages
to index it without any splits.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2024-01-04 22:01:40 -03:00
Ernesto A. Fernández 2de7f65d5b Implement a helper to extract root node from query
Implement apfs_query_root() and use it to simplify two functions. It
will get reused further in the following patch, including inside node.c,
so don't make it static.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-12-30 16:29:55 -03:00
Ernesto A. Fernández e872d3971a Allow NULL to be passed to apfs_node_free()
Allow NULL node pointers to be "freed" with apfs_node_free(). This will
simplify some cleanups soon.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-12-29 23:48:30 -03:00
Ernesto A. Fernández eb6dd65019 Don't let node insert/replace fail with ENOSPC
Currently node insertions/deletions are attempted immediately and they
return -ENOSPC when there is no room, letting the caller know that the
node needs to get split. But in future patches I'll need to reorder the
operations so that the node only gets updated after all the ancestors.

The previous patches already ensured that apfs_node_replace() will never
erroneously report lack of space, no matter how fragmented the node may
be. This was already true of apfs_node_insert(). So from now on we can
safely run space checks ahead of time with only the node metadata, use
that to decide if a split is needed, and only update the records after
all that is done.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-12-29 23:22:46 -03:00
Ernesto A. Fernández 0c8beaa186 Also defragment freed space in apfs_node_replace()
In the near future we want to make sure that apfs_node_replace() will
never fail as long as it has enough space. The defragmentation from the
previous patch is not enough for this, because it still leaves the
replaced record in place instead of moving those useless bytes into the
free area.

To solve this as simply as possible, set the table-of-contents entry for
the replaced record to zero length, for either the key or the value (or
both), depending on which we are replacing. The defragmentation code
will take care of the rest. Of course this weird toc entry remains in
place if the function fails, but it will be replaced eventually so it
should not matter.

By the way, this hack cannot be used on trees with fixed key/value
sizes, because those sizes are simply not reported in the toc. Of course
records in such trees can generally be replaced in place, so they won't
need defragmentation. There is one exception though: the ghost records
of the free queue. Luckily we don't currently call replace on free queue
leaves, so we can safely ignore this problem. Leave an assert in place
so that I don't forget.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-12-29 22:04:16 -03:00
Ernesto A. Fernández 2adf86cba9 Defragment the node in apfs_node_replace()
Like we already do in apfs_node_insert(), attempt to defragment the node
before returning -ENOSPC in apfs_node_replace(). This shouldn't make
much of a difference right now (other than packing the records a bit
tighter together), but in future patches it will help us prevent
apfs_node_replace() from ever failing in normal operation.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-12-29 21:56:02 -03:00
Ernesto A. Fernández f883518a96 Move toc entry update into new function
Define a new apfs_node_update_toc_entry() to be used when replacing
records in a node. This makes apfs_node_replace() somewhat cleaner, but
more importantly it will allow that part to be reused in future patches.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-12-29 19:40:24 -03:00
Ernesto A. Fernández 4eb83167a9 Document some node functions that may throw ENOSPC
Improve the documentation of the record allocation functions to make it
clear that they may return -ENOSPC, because that case will often need to
be handled separately in the future.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-12-29 19:35:22 -03:00
Ernesto A. Fernández e053f3b2b5 Call apfs_delete_node() with a node, not a query
Some changes I'm working on will require being able to delete nodes that
have not yet been indexed in a btree. This cannot be done with the
current version of apfs_delete_node(), because it handles the parent
node modifications itself. So from now on, let the caller handle that if
it's necessary. This is also more symmetrical with apfs_create_node(),
as would be expected.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-12-29 18:28:01 -03:00
Ernesto A. Fernández 196ac31122 Don't allow a stale offset for ghost records
When apfs_node_locate_data() gets called for a ghost record (that is, a
record without a value), the function correctly returns the zero length,
but the offset is left unset, and so a stale number may remain in the
query structure. This does not matter of course, because a zero-length
value will never be read, but I've found it very distracting while
debugging a separate issue with query offsets. Just set it to zero.

To be consistent, do the same in apfs_node_insert(), though I haven't
been bothered by this one yet.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-12-28 19:48:25 -03:00
Ernesto A. Fernández f1775ed0ef Turn the search key into a query field
I've been working on making some very needed changes to how node splits
work. The plan is to always rerun queries after a split, to keep the
query chain valid. One problem I've encountered while implementing this
is that the search key is not really preserved by apfs_update_inode().
We have a pointer to it, but it was allocated in the stack of some other
function, so the data is just noise.

I've always thought it was silly to allocate both a query and a key
before a lookup. Instead, turn the search key into a field within the
query structure, and copy it to the children as the query gets run. This
will ensure that the key remains available for future reruns.

One thing to note is that, while this simplifies most callers a little
bit, some of them still allocate a key in the stack and later copy it to
the query. I didn't want to make any big changes and risk introducing
bugs at this point, since I'm busy with other stuff.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-12-20 20:33:03 -03:00
Ernesto A. Fernández a33f06d8da Do use anon devices, but in a different way
Like I mentioned in the previous patch, df is refusing to acknowledge
multiple mounted volumes or snapshots for the same device, and it's
reporting only one. Luckily the source code for df is straightforward,
so I figured out that it calls stat() on the mount point to find the
device number instead of just accepting the one reported by mountinfo.
This way each mounted volume/snapshot can choose to report a different
number, instead of just going with the one set in the superblock.

After confirming that this is indeed what btrfs does, implement it here.
This fixes the df issue, but sadly it doesn't fix all problems with
thunar. It can tell the different mounts apart, but for some reason I
need to close it and reopen it for that to happen (in fact I now realize
that it could already do that in the previous commit). Since btrfs does
have plugins for udisks and such, maybe this is not the driver's fault,
but I'm not sure.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-12-08 22:08:13 -03:00
Ernesto A. Fernández 9c0d396e20 Don't use anon devices
Nautilus and other file managers fail to unmount apfs volumes:

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

This has been broken for years, since commit 4a6c0e2b77 ("Support
simultaneous mount of multiple volumes"), but I never noticed because I
always use the command line, and there are no problems either there or
with libmount.

The error message ("Device `%s' is not mounted") appears to come from
udisks. I don't know much about this stuff, but from tests to my driver
it seems that userland expects /proc/self/mountinfo to report the device
number for the mounted device, not just the name. The tricks we do with
anon devices to support multiple mounts cause a confusion.

Stop using anon devices. Instead set the real device number on the
superblock, and ignore it for all purposes other that mountinfo
reporting, which is done by the common code.

This should solve the reported bug, but causes other weird behaviour.
For instance, if I mount the current transaction and later mount a
snapshot, both df and thunar refuse to acknowledge the existence of the
first mount. I don't know how btrfs deals with this, but they seem to
have plugins for udisks and libblockdev so it's probably not as simple
as a driver change.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-12-07 22:16:55 -03:00
Ernesto A. Fernández 760cf779ef Add snapshot name to apfs_show_options()
I just noticed that the snapshot name option is not getting output in
/proc/mounts. I guess I just forgot to implement this, do it now.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-12-07 21:52:06 -03:00
Ernesto A. Fernández 0d274c7b48 Fix ->statfs() block count for snapshot mounts
I just noticed that df throws a "No data available" error if it's used
while a snapshot is mounted. The problem happens while gathering the
total number of used blocks: to iterate through the existing volume
superblocks, this requires working with the container omap, which we
otherwise only do for snapshots during mount. apfs_omap_lookup_block()
is written with the idea that snapshots will only call it for the volume
omap, and so it uses the snapshot xid to get the wanted (old) version of
the object. But for used space calculations we actually want the latest,
and in fact the container omap doesn't even have the old versions of the
volumes, so the call fails.

Reimplement apfs_omap_lookup_block() as a wrapper around a new
apfs_omap_lookup_block_with_xid() that uses the current snapshot
transaction, and use this to implement apfs_omap_lookup_newest_block()
to be used in this situation.

By the way, there was no output on dmesg when I discovered this bug.
That's no good, so add a new error message in here.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-12-07 21:22:32 -03:00
Ernesto A. Fernández 53a9145401 Support multiblock internal pool bitmaps
A container with a size starting at 1454383304704 bytes (~1.3 TiB) needs
more than one block to hold the bitmap of its internal pool. Add support
for such large filesystems.

To this end, replace the single buffer head holding the bitmap in the
spaceman struct with an array, which requires allocating the spaceman
separately from the container superblock info. Then rework the alloc and
free functions to work with the whole array.

The biggest changes are in apfs_rotate_ip_bitmaps(), which now has to
copy all the bitmap blocks at once to make them available for the
transaction. This is probably not what the official implementation
intended, but it should be technically correct.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-12-01 20:46:36 -03:00
Ernesto A. Fernández 1e8251a458 Report driver version for modinfo
The previous commit started reporting the driver version on disk after a
modification. Report it as well in the driver metadata, so that it can
be obtained with modinfo.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-11-14 20:54:54 -03:00
Ernesto A. Fernández e0b1eeb29d Include release info in apfs_modified_by field
The filesystem superblock has an apfs_modified_by field to report which
implementation is responsible for the latest changes. This could be
useful to track down bugs eventually, but it will need to have more
information. Right now we only report my name and the name of the
module; change it to also report the exact commit id, or the tag if
there is one. There is a strict size limit here, so shorten my name to
"eafer", which is what I use on github, so it will make the driver easy
to track down.

Also move the APFS_MODULE_ID_STRING macro definition to super.c. We'll
now need to recompile that file with every build, but if we left it
inside apfs.h then we would have to recompile everything all the time.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-11-03 22:10:03 -03:00
Ernesto A. Fernández 3c36f372be Get rid of apfs_end_buffer_write_sync()
Some mmap related tests such as generic/030 are throwing the following
callstack on kernels 6.1 and above:

[ 9810.190496] ------------[ cut here ]------------
[ 9810.192000] irq 15 handler ata_bmdma_interrupt+0x0/0x1f0 [libata] enabled interrupts
[ 9810.193639] WARNING: CPU: 0 PID: 0 at kernel/irq/handle.c:161 __handle_irq_event_percpu+0x150/0x190
[ 9810.195220] Modules linked in: apfs(OE) libcrc32c(E) 9p(E) fscache(E) netfs(E) intel_rapl_msr(E) intel_rapl_common(E) intel_pmc_core_pltdrv(E) intel_pmc_core(E) kvm_intel(E) kvm(E) irqbypass(E) ghash_clmulni_intel(E) ppdev(E) aesni_intel(E) bochs(E) libaes(E) drm_vram_helper(E) drm_ttm_helper(E) crypto_simd(E) cryptd(E) ttm(E) rapl(E) snd_pcm(E) snd_timer(E) 9pnet_virtio(E) drm_kms_helper(E) 9pnet(E) snd(E) soundcore(E) pcspkr(E) parport_pc(E) parport(E) button(E) joydev(E) evdev(E) serio_raw(E) sg(E) binfmt_misc(E) fuse(E) drm(E) efi_pstore(E) configfs(E) qemu_fw_cfg(E) ip_tables(E) x_tables(E) autofs4(E) ext4(E) crc32c_generic(E) crc16(E) mbcache(E) jbd2(E) sd_mod(E) t10_pi(E) crc64_rocksoft(E) crc64(E) crc_t10dif(E) crct10dif_generic(E) ata_generic(E) ata_piix(E) libata(E) crct10dif_pclmul(E) crct10dif_common(E) crc32_pclmul(E) virtio_pci(E) virtio_pci_legacy_dev(E) crc32c_intel(E) virtio_pci_modern_dev(E) virtio(E) psmouse(E) virtio_ring(E) e1000(E) scsi_mod(E) scsi_common(E)
[ 9810.195294]  i2c_piix4(E) floppy(E) [last unloaded: apfs(OE)]
[ 9810.215274] CPU: 0 PID: 0 Comm: swapper/0 Tainted: G        W  OE      6.0.0-rc2+ #24
[ 9810.216639] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.15.0-1 04/01/2014
[ 9810.217983] RIP: 0010:__handle_irq_event_percpu+0x150/0x190
[ 9810.218904] Code: 44 00 00 e9 09 ff ff ff 80 3d 8f 16 4e 01 00 75 1b 48 8b 13 44 89 ee 48 c7 c7 e8 31 91 bc c6 05 79 16 4e 01 01 e8 90 26 81 00 <0f> 0b fa 0f 1f 44 00 00 e9 fc fe ff ff 3e 48 0f ba 6b 40 01 0f 82
[ 9810.223642] RSP: 0018:ffffaf4240003f60 EFLAGS: 00010286
[ 9810.224836] RAX: 0000000000000000 RBX: ffff94804172f080 RCX: 0000000000000000
[ 9810.226451] RDX: 0000000000010002 RSI: ffffffffbc94d2a2 RDI: 00000000ffffffff
[ 9810.228082] RBP: 0000000000000001 R08: 0000000000000000 R09: ffffaf4240003e00
[ 9810.229506] R10: 0000000000000003 R11: ffffffffbccc4778 R12: 0000000000000000
[ 9810.231182] R13: 000000000000000f R14: ffff94804019ae00 R15: 0000000000000000
[ 9810.232856] FS:  0000000000000000(0000) GS:ffff948177c00000(0000) knlGS:0000000000000000
[ 9810.234771] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[ 9810.236112] CR2: 00007f6d608b1210 CR3: 000000006020a004 CR4: 0000000000370ef0
[ 9810.237812] Call Trace:
[ 9810.239294]  <IRQ>
[ 9810.239767]  handle_irq_event+0x34/0x70
[ 9810.240688]  handle_edge_irq+0x9f/0x240
[ 9810.241577]  __common_interrupt+0x63/0x100
[ 9810.242444]  common_interrupt+0xb4/0xd0
[ 9810.243141]  </IRQ>
[ 9810.243575]  <TASK>
[ 9810.244008]  asm_common_interrupt+0x22/0x40
[ 9810.244820] RIP: 0010:native_safe_halt+0xb/0x10
[ 9810.245705] Code: 01 00 3e 80 48 02 20 48 8b 00 a8 08 75 bf e9 7b ff ff ff cc cc cc cc cc cc cc cc cc cc cc cc 66 90 0f 00 2d a9 eb 49 00 fb f4 <c3> cc cc cc cc 66 90 0f 00 2d 99 eb 49 00 f4 c3 cc cc cc cc cc 0f
[ 9810.249342] RSP: 0018:ffffffffbcc03e90 EFLAGS: 00000252
[ 9810.250451] RAX: ffffffffbc16ab90 RBX: ffffffffbcc139c0 RCX: 0000000000000000
[ 9810.252391] RDX: 4000000000000000 RSI: ffffffffbc94d2a2 RDI: 00000000005ec6ec
[ 9810.253784] RBP: 0000000000000000 R08: 00000a4bf1fdb94f R09: 0000000000000000
[ 9810.256613] R10: 0000000000000000 R11: 0000000000000000 R12: 0000000000000000
[ 9810.258508] R13: 0000000000000000 R14: ffffffffbcc13118 R15: 0000000000000000
[ 9810.260053]  ? ldsem_down_write+0x230/0x230
[ 9810.261021]  default_idle+0xa/0x10
[ 9810.261734]  default_idle_call+0x36/0xf0
[ 9810.262574]  do_idle+0x220/0x2b0
[ 9810.263341]  cpu_startup_entry+0x19/0x20
[ 9810.264217]  rest_init+0xcb/0xd0
[ 9810.265119]  arch_call_rest_init+0xa/0x14
[ 9810.266173]  start_kernel+0x6d2/0x6fb
[ 9810.267059]  secondary_startup_64_no_verify+0xce/0xdb
[ 9810.268261]  </TASK>
[ 9810.268837] ---[ end trace 0000000000000000 ]---

I bisected this back to upstream commit f168420c62e7 ("blk-mq: don't
redirect completion for hctx withs only one ctx mapping"), but that
doesn't tell me much.

I can't tell for sure what's going wrong here, but it seems to be
related to my implementation of ->b_end_io(), which exists to force page
faults when we write to an mmapped page again after a commit. I didn't
follow any clear reference when I wrote that code, and I'm guessing
something about it is not allowed. Also, this code runs asynchronously,
which seems to fit with the nature of the problem, since my driver is
not mentioned in the callstack.

Just use the standard end_buffer_write_sync() for ->b_end_io(), and wait
for each buffer before cleaning its page. This is similar to what we do
for reads and it doesn't have any original async stuff, so hopefully it
will work better. At least none of my tests are failing anymore.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-11-03 20:49:35 -03:00
Ernesto A. Fernández 620d6257f8 0.3.5
This release adds support for reading compressed files of arbitrary
on-disk size, which is becoming more and more necessary in recent ipsw
images.

A number of bugs are addressed for systems with little ram, reported by
Gilgamesh via email.

Support is added for the 6.6 kernel version, mainly thanks to Woody
Suwalski. I'm afraid the driver sometimes triggers a warning for recent
kernels, but I didn't manage to fix it in time for the new upstream
release, and it's better to at least get things to build.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-10-30 13:58:24 -03:00
Ernesto A. Fernández 225a17aa77 Set splice read/write methods explicitly
Several of xfstests (generic/249 for example) are failing on kernel 6.6
because of sendfile issues. Starting with upstream commit 36e2c7421f02
("fs: don't allow splice read/write without explicit ops"), the
->splice_read() and ->splice_write() methods need to be set explicitly,
or else we just get EINVAL when sendfile needs them. This change
happened in the 5.10 release, but I never noticed because I usually only
run tests on 5.3.

So, set the methods as required, taking into consideration the kernel
version because there was another related change in commit 2cb1e08985e3
("splice: Use filemap_splice_read() instead of
generic_file_splice_read()").

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-10-27 22:13:05 -03:00
Ernesto A. Fernández 6813d7e614 Ignore return value for generic_update_time()
Attempts to run xfstests for the 6.6 kernel fail brutally. Transactions
are getting aborted inside apfs_update_time(), which has never happened
before. The problem comes from upstream commit 541d4c798a59 ("fs: drop
the timespec64 arg from generic_update_time"): generic_update_time()
can't fail and used to always return a symbolic 0, but now instead
returns some flags - which my code mistakes for an error. We don't care
about the flags, so just ignore the return value for all kernel
versions.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-10-27 19:24:13 -03:00
Ernesto A. Fernández 4dab24ef6b Stop triggering kernel panics for silly reasons
I was reading the list of deprecated interfaces at [0] and it mentions
that the use of BUG() and BUG_ON() is discouraged in the kernel, and
that we should be prepared to fail gracefully even for impossible
situations.

I definitely agree with this: so far I've used BUG_ON() to implement
ASSERT(), but in practice I always turn that into a WARN_ON() while
debugging. Just leave it as WARN_ON() permanently.

I also use BUG() in apfs_query_storage() to mark an impossible query
tree flag. I can't imagine how this would ever be reached, but still
keep going and return an invalid storage type. This way the error can
get reported later, after calling either apfs_create_node() or
apfs_read_node(), which have been modified to fail gracefully and
complain with apfs_alert() in this situation. I also considered putting
an assertion in apfs_query_storage(), but that would require reordering
apfs.h and it seems unnecessarily messy.

Warnings should not be abused either, because of the panic_on_warn
option. Replace our one call to WARN_ON(), inside apfs_btree_replace(),
with another apfs_alert(). This function was already failing gracefully.

[0]: https://docs.kernel.org/process/deprecated.html#zero-length-and-one-element-arrays

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-10-27 15:44:47 -03:00
Ernesto A. Fernández 1d9630b194 Get rid of zero-length arrays
According to checkpatch.pl, the use of zero-length arrays is now
deprecated in the kernel. Replace them all with C99 flexible array
members.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-10-26 21:56:56 -03:00
Ernesto A. Fernández 2d6cdba8f6 Fix trivial style issues
Some code that wasn't originally written by me has a style that differs
slightly from the rest of the module. Since I've been refactoring
compress.c quite a lot lately, I have made the inconsistencies more
visible, so I think it's a good time to switch everything to the typical
kernel style.

For this purpose I'm running checkpatch.pl for the first time in years,
and it has detected a handful of other issues that I introduced myself.
Fix most of the issues now, but I prefer to leave the zero-length array
stuff for a separate patch.

For the record, at the moment I'm choosing to ignore style inconsistency
in the compression libraries, even in libzbitmap which I wrote myself. I
prefer to keep them as close to upstream as possible.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-10-26 21:44:30 -03:00
Woody Suwalski 1bca725a63 Fix for the kernel 6.6 build errors
[eafer: removed unnecessary setting of some timestamps]
Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-10-25 22:43:27 -03:00
Ernesto A. Fernández 12f526f2de Perform locking for compressed reads
For some reason I never remembered to take the big lock during reads of
compressed files. Do it. As always, my locking is coarse and hacky, and
I will have get back to it at some point.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-10-24 21:30:08 -03:00
Ernesto A. Fernández 2ec72ff747 Simplify gotos in apfs_compress_file_open()
After the recent refactoring of the compression code, the fail_enomem
label feels a little unnecessary. Get rid of it.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-10-24 20:42:15 -03:00
Ernesto A. Fernández 133ed4fbf3 Don't swallow errors in compressed ->readpage()
While debugging the previous patch, I noticed that my implementation of
compressed ->readpage() from f9ca9061bb ("Implement mmap for
compressed files") has a serious issue: if something goes wrong during
the read, the error code is swallowed and stale garbage becomes visible
to the user.

I messed this up because I was following ext4_read_inline_page(), which
first calls ext4_read_inline_data() and then, without checking the
return code, calls SetPageUptodate(). But ext4_read_inline_data() can't
actually fail, so this is not a problem. In my version the function
called is apfs_compress_file_read_page(), which does fail sometimes,
leaving a stale page marked as updated. Fix it, and also return the
error code correctly.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-10-19 23:33:21 -03:00
Ernesto A. Fernández 71b11b377b Unify read of attr and rsrc compressed files
Currently, compressed files that are stored directly in the decmpfs
xattr are handled separately: they get entirely uncompressed on open,
and on reads the contents simply get copied. To get rid of this code
duplication, use a new apfs_compressed_data struct to read from both
inline and dstream xattrs, and let the regular resource-compressed code
handle reads for both types.

These changes weren't made exclusively for code quality. A compressed
file may be too big to fit inside decmpfs, but still small enough to fit
inside a xattr. If that happens, the resulting resource fork will be an
inline xattr, and the dstream read code implemented with e4bb06ecef
("Avoid reading whole compressed files into memory") will break down. So
the apfs_compressed_data struct is strictly necessary for the resource
case; using the same struct for both cases simplifies testing, because
inline resource compressed files are rare (I've seen them, but right now
I don't even have an image with one of them).

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-10-19 23:33:21 -03:00
Ernesto A. Fernández e6370242fa Try to preread the dstream for compressed files
After commit e4bb06ecef ("Avoid reading whole compressed files into
memory"), performance for compressed reads worsened by about 50%. The
problem is that, when we stopped reading the xattr all at once, we lost
the advantage of letting the kernel batch and schedule the buffer reads.

The ideal way to handle this would be to make a fake inode for the xattr
dstream, and let the kernel do readahead in the usual manner. I don't
feel very confident that I can get that right though, so I'm leaving it
for some other time.

For now, just request all the blocks as soon as a read begins (I would
have preferred to do it on open, but that's slower for some reason), and
ignore the results. Compressed data is supposed to be small so, as long
as there is enough memory, this has all the performance benefits of the
original implementation (in fact it's somewhat faster), but it still
avoids the problems with large virtual memory allocation.

Of course, unlike normal readahead, this will be useless if memory is
scarce, or if we don't read the whole file sequentially.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-10-13 22:01:15 -03:00
Ernesto A. Fernández 040358bd4a Check for failure in buffer head array allocation
I've neglected to check for failure after allocating an array in
apfs_nonsparse_dstream_read(). This is not a new bug: it got carried
over from the original xattr code, which has been around since
84c3ecebac ("Don't read dstream xattr blocks one by one"). Fix it now.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-10-11 20:28:54 -03:00
Ernesto A. Fernández e537eccc88 Fix apfs_compress_file_read_block() return code
In commit e4bb06ecef ("Avoid reading whole compressed files into
memory"), I moved part of the code of apfs_compress_file_read_block()
into a separate apfs_compress_file_read_from_block() that must call it.
I made a mistake, though: the original version of the function would
reset the return value before returning, but this version no longer does
it. This doesn't play well with the decompression functions for some of
the algorithms, which return decompressed lengths. So we get a "failed
to read block into buffer" message and the decompression is abandoned.
Fix this.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-10-10 19:04:34 -03:00
Ernesto A. Fernández c6de394653 Make all compressed reads use the page cache
When implementing compressed mmap I was forced to duplicate the code for
regular compressed reads, so that one version could copy to a user
buffer and the other to the kernel. To simplify this, get rid of the
regular version and instead use the generic read iterator, which will
make use of the same mmap code.

This should also allow regular compressed reads to take advantage of the
page cache.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-10-06 21:21:14 -03:00
Ernesto A. Fernández e99d7b4d51 Get rid of apfs_compress_file_mmap()
When compressed mmap was implemented, apfs_compress_file_mmap() was
added with the purpose of ignoring calls to ->page_mkwrite(). With the
last patch, I no longer allow compressed files to be opened for writing
at all, so I can just use regular ->mmap() for all files.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-10-06 20:18:31 -03:00
Ernesto A. Fernández a55f0effc7 Ban the opening of compressed files for writing
In commit 31df49c6d8 ("Ban writes to compressed files explicitly"), I
blocked writes to compressed files when they actually happen, not when
the file gets opened. My fear was that banning rw open entirely could
break other people's code, since I'm not sure who is using this driver
and how.

Now I've implemented compressed mmap, and blocking writes when they
happen is becoming a little too hacky. Do this properly, and wait to see
if anyone complains.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-10-06 19:08:10 -03:00
Ernesto A. Fernández f9ca9061bb Implement mmap for compressed files
Attempts to mmap compressed files currently fail with ENODEV. I guess I
had forgotten about compressed mmap. Implement it now, in read-only mode
like all compressed operations so far.

Since the existing read code works with userland buffers, it can't be
reused to copy stuff into the kernel. For now I've been forced to
duplicate a lot of code, but I think it might be a good idea to use the
generic read implementations instead. I'll look into that soon.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-10-05 22:36:52 -03:00
Ernesto A. Fernández 131953ffd8 Always lock pages before making them clean
Gilgamesh reports via email that his kernel often crashes on my call to
page_mkclean() due to "BUG_ON(!PageLocked(page))".

I should have expected problems such as this, since the locking in
apfs_end_buffer_write_sync() is not sane at all. Apparently back when I
wrote this, I only realized that I needed to lock the page because of
the above-mentioned panic. So I tried to take the lock, but this caused
a deadlock in generic/013 of xfstests. Instead of figuring out what the
problem was, I just replaced lock_page() with trylock_page(), to silence
the panic and still avoid deadlock. Of course this leaves us open to a
race, which in this case became visible because whatever was holding the
page released the lock before the check happened in page_mkclean().

In my defense, most of the locking in the driver is insane at this
point, so I don't usually put much thought into it unless I see actual
problems, as in this case.

So, replace trylock_page() with lock_page(), and figure out what was
causing the generic/013 deadlock in the first place. This was actually
very simple: a check of the contents of the problem page showed that
it's always a chunk-info block, and those are the only block types that
both get dirtied and added to the transaction. This is unnecessary, and
it causes some conflict on commit with whatever the kernel does to the
dirty blocks. Stop dirtying the cib blocks.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-09-29 20:54:45 -03:00
Gilgamesh 12dde7a08f Only commit in apfs_sync_fs() if wait == 1
I've been ignoring the 'wait' argument to apfs_sync_fs() so far, but
this means that transactions get committed twice in a row whenever a
sync is requested. Instead, just do nothing when wait == 0. If the
caller actually needs a sync, they will have to try again with wait == 1
later anyway.

[eafer: added comment and commit message]
Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-09-27 19:41:26 -03:00
Ernesto A. Fernández 19430cbdb2 Improve error messages in apfs_xattr_get_dstream()
I wrote apfs_xattr_get_dstream() long before error messages were changed
in d4797823ea ("Improve error reporting"), but the code wasn't
actually merged until recently, and I forgot to update the error
messages to our current standards. Fix that now.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-09-25 20:03:05 -03:00
Ernesto A. Fernández 1756ff18d3 Limit transaction size on systems without much ram
Gilgamesh reports via email that there is a kernel crash when you try to
copy an 800Mb file on a system with 512Mb of memory. The reason is that
I don't allow buffer heads to be reclaimed before commit time (I tried
it at some point and it was trickier than it seemed), and since commit
c5dfaa2de9 ("Allow bigger transactions"), each transaction can get as
big as 256Mb.

So, until I can get around to the reclaim issue (or hopefully get rid of
buffer heads), make transactions smaller for systems with little memory.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-09-22 16:28:05 -03:00
Ernesto A. Fernández 31df49c6d8 Ban writes to compressed files explicitly
The official implementation doesn't seem to truly support writes to
compressed files: when it's attempted, the file gets decompressed
transparently instead. I don't intend to that in this driver, unless
someone actually requests it. Users can just make a decompressed copy of
the file and replace the original.

By mistake, truncating compressed files is currently allowed, but since
compressed reads don't depend on the vfs-reported size, nothing seems to
happen. Other writes fail, but the error code is confusing and there is
no output on dmesg. Ban all writes explicitly, and explain the problem
in the console.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-09-21 19:50:09 -03:00
Ernesto A. Fernández e4bb06ecef Avoid reading whole compressed files into memory
Currently, compressed files saved in xattrs are read into memory all at
once, and later decompressed as needed. This has always required a large
memory allocation, which can be a problem in general, but especially for
32-bit archs which can't request more than 128Mb from vmalloc(). As
mentioned in commit 1e3fa28c4f ("Increase limit for compressed size
of files"), we are beginning to encounter files bigger than that limit.

Using the new apfs_nonsparse_dstream_read() to read from arbitrary xattr
offsets, rework the compressed reads so that we only allocate memory for
one compressed block at a time. For now the change is only for resource
xattrs, since I don't expect inline xattrs to ever be this big.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-09-20 15:41:49 -03:00
Ernesto A. Fernández a68325ef9e Spin xattr dstream reads into a new function
Lately I'm encountering more and more compressed files that are too
large for their xattrs to be read into memory all at once, so I'll need
to be able to read xattrs from arbitrary offsets. The code for this is
very similar to what's already inside apfs_xattr_extents_read(), so spin
it into a new apfs_nonsparse_dstream_read() with a generic interface
that can later be reused from compress.c.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-09-20 15:34:00 -03:00
Ernesto A. Fernández af0671f58f 0.3.4
This release fixes the build for the Linux 6.5 release candidate, and it
should also fix a long-standing mmap bug in Linux 5.14 and above, which
resulted in null pointer dereference.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-08-14 23:50:19 -03:00
Ernesto A. Fernández 740d7db44b Fix mmap for kernels 5.14 and above
Adamantinum reports trouble while attempting to use apfs as root with
kernel version 6.4.8:

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

His callstack shows an npd in mmap, and the bug can be easily reproduced
by generic/029, but not in kernel version 5.3, where I usually run my
tests. Via bisection I tracked the problem back to kernel commit
0af573780b0b ("mm: require ->set_page_dirty to be explicitly wired up").
Starting with kernel 5.14, the ->set_page_dirty() address space method
gets called unconditionally, without checking for null first, so set it
explicitly.

Kernel 5.18 replaced ->set_page_dirty() with ->dirty_folio(), so take
care of that as well.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-08-14 19:50:22 -03:00
Ernesto A. Fernández bc2be91bb4 Update FMODE_* stuff for the 6.5 kernel
The build is broken for the Linux 6.5 release candidate:

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

The fmode_t type can no longer be used for block open flags, and we must
switch to blk_mode_t instead. FMODE_EXCL is no longer needed, since just
having a holder already implies it, but this requires a change to the
blkdev_put() api. There is also a new argument to blkdev_get_by_path()
which can be safely ignored. And finally, sb->s_mode is gone, but it
doesn't seem to make any difference.

Update all the code to the new apis.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-08-08 20:58:11 -03:00
Ernesto A. Fernández 5c4773c036 Convert all strcpy() to strscpy()
The build appears to be broken for some configurations on the Linux 6.5
release candidate:

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

Some sort of static overflow check is going on for strcpy(), and several
of my calls are triggering false positives. Just be safe and switch them
all to strscpy(). The qstr stuff in particular looked a bit too weird
anyway.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-08-08 19:14:10 -03:00
Ernesto A. Fernández 1e3fa28c4f Increase limit for compressed size of files
A recent cryptex image contains a file with a compressed size above the
current 128Mb limit. This limit exists because of vmalloc(), and it can
be increased (at least for 64-bit archs). Do that for now, but I really
should try to restructure the code to use smaller kmalloc() allocations
instead.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-06-07 20:40:53 -03:00
Ernesto A. Fernández d4799475db 0.3.3
This release fixes builds for kernels below 5.3, which have all been
broken for a while. So far development has only been focused on Linux
5.3, and I've relied on reporters to find out about issues on other
versions. From now on I will at least run builds for a range of kernels
before each release, so hopefully this won't happen again.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-05-24 23:05:25 -03:00
Ernesto A. Fernández c54e3a7423 Silence b_blocknr width warnings on old kernels
While fixing the build for old kernels, I noticed that in some of them
you get a warning when you prink a buffer head's b_blocknr as 0x%llx.
I haven't checked but I'm guessing that, at some point, sector_t on
64-bit archs went from being 'unsigned long' to 'unsigned long long',
so these warnings went away. Anyway, just cast b_blocknr before printing
it in all kernels.

At some point I do need to attempt a 32-bit build though. It's been a
long time, and I'm sure there will be much worse problems than this one.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-05-19 18:49:38 -03:00
Ernesto A. Fernández b185ce13bd Fix builds before generic_copy_file_range()
Even with the previous patch, builds are still broken for all kernels
before 5.2. The problem is that generic_copy_file_range() wasn't
introduced until Linux 5.3. The good news is that we don't actually need
it for older kernels: as I explained in the commit message for the patch
that broke this, we can just returning an error code from
apfs_remap_file_range().

Fixes: 396650b056 ("Don't let copy_file_range() use clones")
Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-05-19 18:31:51 -03:00
Ernesto A. Fernández 42d70a8997 Fix build for kernels with separate clone/dedupe
Luflosi reports that the build is broken once again for Linux 4.14 and
4.19:

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

The problem here is that the clone and dedupe operations used to be
implemented with separate methods. Since Linux 4.20, they both use the
same ->remap_file_range(), with some flags passed to tell them apart.
So when I implemented clones I used this new version and broke the build
for the old ones. Fix it now.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-05-19 18:31:51 -03:00
Ernesto A. Fernández 97ba9b0dc8 0.3.2
This release improves error messages, which will hopefully make it
easier to communicate with bug reporters. A number of bugs are also
addressed, most importantly a missing check on the node count limit for
the free queues, which could trigger reports of corruption in some rare
cases (no data loss though, as far as I can see).

Support is added for the 6.3 kernel version, courtesy of Woody Suwalski.

I was hoping to wait until encryption was ready before making a release,
but I'm not finding much time for apfs these days so that could take a
while. In the meantime, it's better to get this stuff out the door.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-05-16 17:57:01 -03:00
Ernesto A. Fernández 8b99af84d1 Try to avoid failing insertion on fragmented nodes
I'm getting somewhat regular failures on generic/112 because the ip free
queue for the test image has a one-node limit, and it sometimes gets
full. The thing is, the node actually has plenty of room, but it's all
in the key free list, so I can't allocate values. From now on, if an
insertion fails with ENOSPC, check the available room and defragment
the node if it should be enough. Then retry the insertion instead of
jumping straight to a split.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-04-17 20:05:26 -03:00
Ernesto A. Fernández 3a605ec8ff Don't ignore error code in apfs_flush_fq_rec()
The previous patch was intended to trigger an immediate abort when a
free queue violated its own node limit. But I managed to trigger this
event again with generic/112, and to my surprise there were several
operations in dmesg before the abort finally happened. The problem here
is in apfs_flush_fq_rec(): when apfs_main_free() fails (in this case
because the ip free queue is full), the error code is reported but not
bubbled up. This is a mistake of course, so fix it.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-04-12 17:49:12 -03:00
Ernesto A. Fernández ccefdd13d8 Don't allow free queues to exceed their node limit
Free queues have a node limit that is set by the mkfs. Very rarely,
generic/112 manages to break this limit, and corruption is reported.
Preventing corruption is always the number one priority, so add a check
for this limit after every free queue insertion so that we can abort
immediately when it fails.

Remember though, that there is a deeper issue here: either the btrees
are becoming too fragmented, or the flushes are not frequent enough. The
queues should never fill like this.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-04-12 16:47:55 -03:00
Ernesto A. Fernández 5beaaae75c Fix out-of-bounds read on snapshot ioctl
Don't call strlen() on the name argument of the ioctl, because it may
not be null-terminated. I'm not sure what the consequences of this are,
but I don't think it should be exploitable. Use strnlen() instead.

Also move the length check outside the transaction: we don't want simple
user mistakes triggering aborts. And fix the returned error code: there
is no on-disk corruption going on, just user error.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-04-05 23:06:22 -03:00
Ernesto A. Fernández 0f47fc762a Fix memory leak on xattr length overflow
Always free the allocated dstream in apfs_xattr_extents_read() before
returning.

Fixes: 362c4e32ab ("Allow internal reads of huge xattrs")
Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-04-05 22:21:53 -03:00
Ernesto A. Fernández d4797823ea Improve error reporting
The driver is much closer to being usable, so I might start getting
subtler bug reports soon. To make them easier to handle, put error
messages all over the place. I should have done this from the beginning,
but I guess I didn't fully understand the need back then.

From now my general policy will be to use apfs_warn() for user errors or
unsupported features; apfs_err() for things that are probably corruption
or io errors; and apfs_alert() for things that are most likely bugs.
These last two should be rare, so the same error/alert will be thrown by
several layers in the callstack to provide as much information as
possible. Be careful and don't flood the console on normal situations.

Also, make messages with a log level lower than warning output their
function name and line number, which I think will help debugging more
than the actual messages.

Signed-off-by: Ernesto A. Fernández <ernesto@corellium.com>
2023-04-05 21:59:24 -03:00
Woody Suwalski 24daddc890 Changes needed to build on kernel 6.3 2023-03-24 15:12:47 -03:00
25 changed files with 5116 additions and 2463 deletions

No files matched your search

+1
View File
@@ -9,3 +9,4 @@
*.a
*.mod
tags
version.h
+2
View File
@@ -14,8 +14,10 @@ apfs-y := btree.o compress.o dir.o extents.o file.o inode.o key.o libzbitmap.o \
spaceman.o super.o symlink.o transaction.o unicode.o xattr.o xfield.o
default:
./genver.sh
make -C $(KERNEL_DIR) M=$(PWD)
install:
make -C $(KERNEL_DIR) M=$(PWD) modules_install
clean:
rm -f version.h
make -C $(KERNEL_DIR) M=$(PWD) clean
+2 -1
View File
@@ -22,7 +22,8 @@ This module is the result of reverse engineering and testing has been limited.
If you make use of the write support, expect data corruption. Please report any
issues that you find, but I can't promise a quick resolution at this stage.
Encryption is not yet implemented even in read-only mode.
Encryption is not yet implemented even in read-only mode, and neither are
fusion drives.
Build
=====
+176 -66
View File
File diff suppressed because it is too large. Load diff
+62 -26
View File
@@ -268,6 +268,10 @@ struct apfs_phys_ext_val {
#define APFS_FILE_EXTENT_LEN_MASK 0x00ffffffffffffffULL
#define APFS_FILE_EXTENT_FLAG_MASK 0xff00000000000000ULL
#define APFS_FILE_EXTENT_FLAG_SHIFT 56
#define APFS_FILE_EXTENT_CRYPTO_FLAG 0x01 /* Made-up name */
#define APFS_FILE_EXTENT_PREALLOCATED 0x02 /* Made-up name */
#define APFS_VALID_FILE_EXTENT_FLAGS (APFS_FILE_EXTENT_CRYPTO_FLAG \
| APFS_FILE_EXTENT_PREALLOCATED)
/*
* Put a bound on maximum file size so that a growing truncation will always
@@ -306,7 +310,7 @@ struct apfs_wrapped_crypto_state {
__le32 key_os_version;
__le16 key_revision;
__le16 key_len;
u8 persistent_key[0];
u8 persistent_key[];
} __packed;
/*
@@ -503,7 +507,7 @@ struct apfs_dir_stats_val {
struct apfs_sibling_val {
__le64 parent_id;
__le16 name_len;
u8 name[0];
u8 name[];
} __packed;
/*
@@ -622,7 +626,7 @@ enum {
struct apfs_drec_key {
struct apfs_key_header hdr;
__le16 name_len;
u8 name[0];
u8 name[];
} __packed;
/*
@@ -632,7 +636,7 @@ struct apfs_drec_key {
struct apfs_drec_hashed_key {
struct apfs_key_header hdr;
__le32 name_len_and_hash;
u8 name[0];
u8 name[];
} __packed;
/*
@@ -641,7 +645,7 @@ struct apfs_drec_hashed_key {
struct apfs_xattr_key {
struct apfs_key_header hdr;
__le16 name_len;
u8 name[0];
u8 name[];
} __packed;
/*
@@ -660,7 +664,7 @@ struct apfs_snap_metadata_key {
struct apfs_snap_name_key {
struct apfs_key_header hdr;
__le16 name_len;
u8 name[0];
u8 name[];
} __packed;
/*
@@ -1051,13 +1055,19 @@ struct apfs_checkpoint_map_phys {
#define APFS_FS_SPILLEDOVER 0x00000010LL
#define APFS_FS_RUN_SPILLOVER_CLEANER 0x00000020LL
#define APFS_FS_ALWAYS_CHECK_EXTENTREF 0x00000040LL
#define APFS_FS_PREVIOUSLY_SEALED 0x00000080LL /* Made-up name */
#define APFS_FS_PFK 0x00000100LL /* Made-up name */
#define APFS_FS_UNKNOWN_200 0x00000200LL
#define APFS_FS_FLAGS_VALID_MASK (APFS_FS_UNENCRYPTED \
| APFS_FS_EFFACEABLE \
| APFS_FS_RESERVED_4 \
| APFS_FS_ONEKEY \
| APFS_FS_SPILLEDOVER \
| APFS_FS_RUN_SPILLOVER_CLEANER \
| APFS_FS_ALWAYS_CHECK_EXTENTREF)
| APFS_FS_EFFACEABLE \
| APFS_FS_RESERVED_4 \
| APFS_FS_ONEKEY \
| APFS_FS_SPILLEDOVER \
| APFS_FS_RUN_SPILLOVER_CLEANER \
| APFS_FS_ALWAYS_CHECK_EXTENTREF \
| APFS_FS_PREVIOUSLY_SEALED \
| APFS_FS_PFK \
| APFS_FS_UNKNOWN_200)
#define APFS_FS_CRYPTOFLAGS (APFS_FS_UNENCRYPTED \
| APFS_FS_EFFACEABLE \
@@ -1124,7 +1134,9 @@ struct apfs_checkpoint_map_phys {
#define APFS_INCOMPAT_NORMALIZATION_INSENSITIVE 0x00000008LL
#define APFS_INCOMPAT_INCOMPLETE_RESTORE 0x00000010LL
#define APFS_INCOMPAT_SEALED_VOLUME 0x00000020LL
#define APFS_INCOMPAT_RESERVED_40 0x00000040LL
#define APFS_INCOMPAT_PFK 0x00000040LL /* Made-up name */
#define APFS_INCOMPAT_EXTENT_PREALLOC_FLAG 0x00000080LL /* Made-up name */
#define APFS_INCOMPAT_SECONDARY_FSROOT 0x00000100LL /* Made-up name */
#define APFS_SUPPORTED_INCOMPAT_MASK (APFS_INCOMPAT_CASE_INSENSITIVE \
| APFS_INCOMPAT_DATALESS_SNAPS \
@@ -1132,7 +1144,9 @@ struct apfs_checkpoint_map_phys {
| APFS_INCOMPAT_NORMALIZATION_INSENSITIVE \
| APFS_INCOMPAT_INCOMPLETE_RESTORE \
| APFS_INCOMPAT_SEALED_VOLUME \
| APFS_INCOMPAT_RESERVED_40)
| APFS_INCOMPAT_PFK \
| APFS_INCOMPAT_EXTENT_PREALLOC_FLAG \
| APFS_INCOMPAT_SECONDARY_FSROOT)
#define APFS_MODIFIED_NAMELEN 32
@@ -1162,6 +1176,18 @@ struct apfs_modified_by {
#define APFS_CRYPTO_RESERVED_5 5
#define APFS_UNASSIGNED_CRYPTO_ID (~0ULL)
/* Doc id index flags. I'm making up the names for now. */
#define APFS_DOC_ID_HAS_PREV_TREE 0x00000001
#define APFS_DOC_ID_UNKNOWN_02 0x00000002
#define APFS_DOC_ID_UNKNOWN_04 0x00000004
#define APFS_DOC_ID_UNKNOWN_08 0x00000008
#define APFS_DOC_ID_UNKNOWN_10 0x00000010
#define APFS_DOC_ID_VALID_FLAGS (APFS_DOC_ID_HAS_PREV_TREE \
| APFS_DOC_ID_UNKNOWN_02 \
| APFS_DOC_ID_UNKNOWN_04 \
| APFS_DOC_ID_UNKNOWN_08 \
| APFS_DOC_ID_UNKNOWN_10)
/*
* Structure used to store the encryption state
*/
@@ -1241,15 +1267,24 @@ struct apfs_superblock {
__le64 apfs_snap_meta_ext_oid;
char apfs_volume_group_id[UUID_SIZE];
/*3F0*/ char apfs_volume_group_id[UUID_SIZE];
__le64 apfs_integrity_meta_oid;
/*400*/ __le64 apfs_integrity_meta_oid;
__le64 apfs_fext_tree_oid;
__le32 apfs_fext_tree_type;
/*410*/ __le32 apfs_fext_tree_type;
__le32 reserved_type;
__le64 reserved_oid;
/*420*/ __le64 apfs_doc_id_index_xid;
__le32 apfs_doc_id_index_flags;
__le32 apfs_doc_id_tree_type;
/*430*/ __le64 apfs_doc_id_tree_oid; /* Made-up name */
__le64 apfs_prev_doc_id_tree_oid;
__le64 apfs_doc_id_fixup_cursor;
__le64 apfs_sec_root_tree_oid;
/*450*/ __le32 apfs_sec_root_tree_type;
} __packed;
/* Extended attributes constants */
@@ -1268,9 +1303,10 @@ enum {
APFS_XATTR_DATA_EMBEDDED = 0x00000002,
APFS_XATTR_FILE_SYSTEM_OWNED = 0x00000004,
APFS_XATTR_RESERVED_8 = 0x00000008,
APFS_XATTR_UNKNOWN_10 = 0x00000010,
};
#define APFS_XATTR_VALID_FLAGS 0x0000000f
#define APFS_XATTR_VALID_FLAGS 0x0000001f
/*
* Structure of the value of an extended attributes record
@@ -1278,7 +1314,7 @@ enum {
struct apfs_xattr_val {
__le16 flags;
__le16 xdata_len;
u8 xdata[0];
u8 xdata[];
} __packed;
/*
@@ -1372,7 +1408,7 @@ struct apfs_file_info_key {
struct apfs_file_data_hash_val {
__le16 hashed_len;
u8 hash_size;
u8 hash[0];
u8 hash[];
} __packed;
#define APFS_FILE_INFO_DATA_HASH 1
@@ -1435,7 +1471,7 @@ struct apfs_compress_rsrc_data {
struct apfs_compress_rsrc_block {
__le32 offs;
__le32 size;
} __packed block[0];
} __packed block[];
} __packed;
/*
@@ -1456,7 +1492,7 @@ struct apfs_snap_metadata_val {
__le32 extentref_tree_type;
__le32 flags;
__le16 name_len;
u8 name[0];
u8 name[];
} __packed;
/*
@@ -1475,7 +1511,7 @@ struct apfs_snap_meta_ext {
__le32 sme_version;
__le32 sme_flags;
__le64 sme_snap_xid;
char sme_uuid[UUID_SIZE];
char sme_uuid[UUID_SIZE];
__le64 sme_token;
} __packed;
@@ -1503,11 +1539,11 @@ enum {
* Structure of a single entry in the keybag
*/
struct apfs_keybag_entry {
char ke_uuid[UUID_SIZE];
char ke_uuid[UUID_SIZE];
__le16 ke_tag;
__le16 ke_keylen;
__le32 padding;
u8 ke_keydata[0];
u8 ke_keydata[];
} __packed;
#define APFS_KEYBAG_VERSION 2
@@ -1520,7 +1556,7 @@ struct apfs_kb_locker {
__le16 kl_nkeys;
__le32 kl_nbytes;
__le64 padding;
struct apfs_keybag_entry kl_entries[0];
struct apfs_keybag_entry kl_entries[];
} __packed;
#endif /* _APFS_RAW_H */
+465 -230
View File
File diff suppressed because it is too large. Load diff
+327 -300
View File
File diff suppressed because it is too large. Load diff
+286 -89
View File
File diff suppressed because it is too large. Load diff
+2 -1
View File
@@ -1,7 +1,8 @@
PACKAGE_NAME="linux-apfs-rw"
PACKAGE_VERSION="0.3.1"
PACKAGE_VERSION="0.3.11"
BUILT_MODULE_NAME[0]="apfs"
DEST_MODULE_LOCATION[0]="/extra"
AUTOINSTALL="yes"
PRE_BUILD="genver.sh"
+551 -112
View File
File diff suppressed because it is too large. Load diff
+53 -4
View File
@@ -5,6 +5,9 @@
#include "apfs.h"
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 8, 0)
#include <linux/splice.h>
#endif
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 17, 0)
typedef int vm_fault_t;
#endif
@@ -18,6 +21,9 @@ static vm_fault_t apfs_page_mkwrite(struct vm_fault *vmf)
struct vm_area_struct *vma = vmf->vma;
#endif
struct page *page = vmf->page;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 7, 0)
struct folio *folio;
#endif
struct inode *inode = file_inode(vma->vm_file);
struct super_block *sb = inode->i_sb;
struct buffer_head *bh, *head;
@@ -42,8 +48,10 @@ static vm_fault_t apfs_page_mkwrite(struct vm_fault *vmf)
apfs_inode_join_transaction(sb, inode);
err = apfs_inode_create_exclusive_dstream(inode);
if (err)
if (err) {
apfs_err(sb, "dstream creation failed for ino 0x%llx", apfs_ino(inode));
goto out_abort;
}
lock_page(page);
wait_for_stable_page(page);
@@ -52,8 +60,16 @@ static vm_fault_t apfs_page_mkwrite(struct vm_fault *vmf)
goto out_unlock;
}
if (!page_has_buffers(page))
if (!page_has_buffers(page)) {
#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 7, 0)
create_empty_buffers(page, sb->s_blocksize, 0);
#else
folio = page_folio(page);
bh = folio_buffers(folio);
if (!bh)
bh = create_empty_buffers(folio, sb->s_blocksize, 0);
#endif
}
size = i_size_read(inode);
if (page->index == size >> PAGE_SHIFT)
@@ -77,8 +93,10 @@ static vm_fault_t apfs_page_mkwrite(struct vm_fault *vmf)
unlock_page(page); /* XXX: race? */
err = block_page_mkwrite(vma, vmf, apfs_get_new_block);
if (err)
if (err) {
apfs_err(sb, "mkwrite failed for ino 0x%llx", apfs_ino(inode));
goto out_abort;
}
set_page_dirty(page);
/* An immediate commit would leave the page unlocked */
@@ -95,7 +113,11 @@ out_abort:
apfs_transaction_abort(sb);
out:
if (err)
#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 6, 0)
ret = block_page_mkwrite_return(err);
#else
ret = vmf_fs_error(err);
#endif
sb_end_pagefault(inode->i_sb);
return ret;
}
@@ -106,7 +128,7 @@ static const struct vm_operations_struct apfs_file_vm_ops = {
.page_mkwrite = apfs_page_mkwrite,
};
static int apfs_file_mmap(struct file * file, struct vm_area_struct * vma)
int apfs_file_mmap(struct file *file, struct vm_area_struct *vma)
{
struct address_space *mapping = file->f_mapping;
@@ -133,6 +155,16 @@ int apfs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
return apfs_sync_fs(sb, true /* wait */);
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 8, 0)
static ssize_t apfs_copy_file_range(struct file *src_file, loff_t src_off,
struct file *dst_file, loff_t dst_off,
size_t len, unsigned int flags)
{
return (splice_copy_file_range(src_file, src_off,
dst_file, dst_off, len));
}
#endif
const struct file_operations apfs_file_operations = {
.llseek = generic_file_llseek,
.read_iter = generic_file_read_iter,
@@ -141,8 +173,25 @@ const struct file_operations apfs_file_operations = {
.open = generic_file_open,
.fsync = apfs_fsync,
.unlocked_ioctl = apfs_file_ioctl,
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 8, 0)
.copy_file_range = apfs_copy_file_range,
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(5, 3, 0)
.copy_file_range = generic_copy_file_range,
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)
.remap_file_range = apfs_remap_file_range,
#else
.clone_file_range = apfs_clone_file_range,
#endif
#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 5, 0)
.splice_read = generic_file_splice_read,
#else
.splice_read = filemap_splice_read,
#endif
.splice_write = iter_file_splice_write,
};
#if LINUX_VERSION_CODE == KERNEL_VERSION(5, 3, 0)
Executable
+13
View File
@@ -0,0 +1,13 @@
#!/bin/sh
# SPDX-License-Identifier: GPL-2.0-only
#
# Script to generate the module version header
#
if command -v git >/dev/null 2>&1 && [ -d .git ]; then
GIT_COMMIT=$(git describe HEAD | tail -c 9)
else
GIT_COMMIT="$(grep PACKAGE_VERSION dkms.conf | cut -d '"' -f2)?"
fi
printf '#define GIT_COMMIT\t"%s"\n' "$GIT_COMMIT" > version.h
+513 -133
View File
File diff suppressed because it is too large. Load diff
+34 -10
View File
@@ -81,8 +81,10 @@ int apfs_keycmp(struct apfs_key *k1, struct apfs_key *k2)
*/
int apfs_read_cat_key(void *raw, int size, struct apfs_key *key, bool hashed)
{
if (size < sizeof(struct apfs_key_header))
if (size < sizeof(struct apfs_key_header)) {
apfs_err(NULL, "bad key length (%d)", size);
return -EFSCORRUPTED;
}
key->id = apfs_cat_cnid((struct apfs_key_header *)raw);
key->type = apfs_cat_type((struct apfs_key_header *)raw);
@@ -92,6 +94,7 @@ int apfs_read_cat_key(void *raw, int size, struct apfs_key *key, bool hashed)
if (size < sizeof(struct apfs_drec_hashed_key) + 1 ||
*((char *)raw + size - 1) != 0) {
/* Filename must have NULL-termination */
apfs_err(NULL, "invalid drec key (%d)", size);
return -EFSCORRUPTED;
}
/* Name length is not used in key comparisons, only the hash */
@@ -103,6 +106,7 @@ int apfs_read_cat_key(void *raw, int size, struct apfs_key *key, bool hashed)
if (size < sizeof(struct apfs_drec_key) + 1 ||
*((char *)raw + size - 1) != 0) {
/* Filename must have NULL-termination */
apfs_err(NULL, "invalid drec key (%d)", size);
return -EFSCORRUPTED;
}
/* There's no hash */
@@ -114,21 +118,26 @@ int apfs_read_cat_key(void *raw, int size, struct apfs_key *key, bool hashed)
if (size < sizeof(struct apfs_xattr_key) + 1 ||
*((char *)raw + size - 1) != 0) {
/* xattr name must have NULL-termination */
apfs_err(NULL, "invalid xattr key (%d)", size);
return -EFSCORRUPTED;
}
key->number = 0;
key->name = ((struct apfs_xattr_key *)raw)->name;
break;
case APFS_TYPE_FILE_EXTENT:
if (size != sizeof(struct apfs_file_extent_key))
if (size != sizeof(struct apfs_file_extent_key)) {
apfs_err(NULL, "bad key length (%d)", size);
return -EFSCORRUPTED;
}
key->number = le64_to_cpu(
((struct apfs_file_extent_key *)raw)->logical_addr);
key->name = NULL;
break;
case APFS_TYPE_SIBLING_LINK:
if (size != sizeof(struct apfs_sibling_link_key))
if (size != sizeof(struct apfs_sibling_link_key)) {
apfs_err(NULL, "bad key length (%d)", size);
return -EFSCORRUPTED;
}
key->number = le64_to_cpu(
((struct apfs_sibling_link_key *)raw)->sibling_id);
key->name = NULL;
@@ -146,8 +155,10 @@ int apfs_read_fext_key(void *raw, int size, struct apfs_key *key)
{
struct apfs_fext_tree_key *raw_key;
if (size != sizeof(*raw_key))
if (size != sizeof(*raw_key)) {
apfs_err(NULL, "bad key length (%d)", size);
return -EFSCORRUPTED;
}
raw_key = raw;
key->id = le64_to_cpu(raw_key->private_id);
@@ -169,8 +180,10 @@ int apfs_read_free_queue_key(void *raw, int size, struct apfs_key *key)
{
struct apfs_spaceman_free_queue_key *raw_key;
if (size < sizeof(struct apfs_spaceman_free_queue_key))
if (size < sizeof(struct apfs_spaceman_free_queue_key)) {
apfs_err(NULL, "bad key length (%d)", size);
return -EFSCORRUPTED;
}
raw_key = raw;
key->id = le64_to_cpu(raw_key->sfqk_xid);
@@ -191,8 +204,10 @@ int apfs_read_free_queue_key(void *raw, int size, struct apfs_key *key)
*/
int apfs_read_omap_key(void *raw, int size, struct apfs_key *key)
{
if (size < sizeof(struct apfs_omap_key))
if (size < sizeof(struct apfs_omap_key)) {
apfs_err(NULL, "bad key length (%d)", size);
return -EFSCORRUPTED;
}
key->id = le64_to_cpu(((struct apfs_omap_key *)raw)->ok_oid);
key->type = 0;
@@ -212,8 +227,10 @@ int apfs_read_omap_key(void *raw, int size, struct apfs_key *key)
*/
int apfs_read_extentref_key(void *raw, int size, struct apfs_key *key)
{
if (size != sizeof(struct apfs_phys_ext_key))
if (size != sizeof(struct apfs_phys_ext_key)) {
apfs_err(NULL, "bad key length (%d)", size);
return -EFSCORRUPTED;
}
key->id = apfs_cat_cnid((struct apfs_key_header *)raw);
key->type = apfs_cat_type((struct apfs_key_header *)raw);
key->number = 0;
@@ -223,21 +240,26 @@ int apfs_read_extentref_key(void *raw, int size, struct apfs_key *key)
int apfs_read_snap_meta_key(void *raw, int size, struct apfs_key *key)
{
if (size < sizeof(struct apfs_key_header))
if (size < sizeof(struct apfs_key_header)) {
apfs_err(NULL, "bad key length (%d)", size);
return -EFSCORRUPTED;
}
key->id = apfs_cat_cnid((struct apfs_key_header *)raw);
key->type = apfs_cat_type((struct apfs_key_header *)raw);
key->number = 0;
switch (key->type) {
case APFS_TYPE_SNAP_METADATA:
if (size != sizeof(struct apfs_snap_metadata_key))
if (size != sizeof(struct apfs_snap_metadata_key)) {
apfs_err(NULL, "bad key length (%d)", size);
return -EFSCORRUPTED;
}
key->name = NULL;
return 0;
case APFS_TYPE_SNAP_NAME:
if (size < sizeof(struct apfs_snap_name_key) + 1 || *((char *)raw + size - 1) != 0) {
/* snapshot name must have NULL-termination */
apfs_err(NULL, "invalid snap name key (%d)", size);
return -EFSCORRUPTED;
}
key->name = ((struct apfs_snap_name_key *)raw)->name;
@@ -251,8 +273,10 @@ int apfs_read_omap_snap_key(void *raw, int size, struct apfs_key *key)
{
__le64 *xid = NULL;
if (size != sizeof(*xid))
if (size != sizeof(*xid)) {
apfs_err(NULL, "bad key length (%d)", size);
return -EFSCORRUPTED;
}
xid = raw;
key->id = le64_to_cpup(xid);
+9 -2
View File
@@ -6,8 +6,9 @@
#include <linux/fs.h>
#include "apfs.h"
void apfs_msg(struct super_block *sb, const char *prefix, const char *fmt, ...)
void apfs_msg(struct super_block *sb, const char *prefix, const char *func, int line, const char *fmt, ...)
{
char *sb_id = NULL;
struct va_format vaf;
va_list args;
@@ -16,7 +17,13 @@ void apfs_msg(struct super_block *sb, const char *prefix, const char *fmt, ...)
vaf.fmt = fmt;
vaf.va = &args;
printk("%sAPFS (%s): %pV\n", prefix, sb->s_id, &vaf);
/* The superblock is not available to all callers */
sb_id = sb ? sb->s_id : "?";
if (func)
printk("%sAPFS (%s): %pV (%s:%d)\n", prefix, sb_id, &vaf, func, line);
else
printk("%sAPFS (%s): %pV\n", prefix, sb_id, &vaf);
va_end(args);
}
+10 -3
View File
@@ -18,13 +18,17 @@ static struct dentry *apfs_lookup(struct inode *dir, struct dentry *dentry,
return ERR_PTR(-ENAMETOOLONG);
err = apfs_inode_by_name(dir, &dentry->d_name, &ino);
if (err && err != -ENODATA)
if (err && err != -ENODATA) {
apfs_err(dir->i_sb, "inode lookup by name failed");
return ERR_PTR(err);
}
if (!err) {
inode = apfs_iget(dir->i_sb, ino);
if (IS_ERR(inode))
if (IS_ERR(inode)) {
apfs_err(dir->i_sb, "iget failed");
return ERR_CAST(inode);
}
}
return d_splice_alias(inode, dentry);
@@ -33,9 +37,12 @@ static struct dentry *apfs_lookup(struct inode *dir, struct dentry *dentry,
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 12, 0)
static int apfs_symlink(struct inode *dir, struct dentry *dentry,
const char *symname)
#else
#elif LINUX_VERSION_CODE < KERNEL_VERSION(6, 3, 0)
static int apfs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
struct dentry *dentry, const char *symname)
#else
static int apfs_symlink(struct mnt_idmap *idmap, struct inode *dir,
struct dentry *dentry, const char *symname)
#endif
{
/* Symlink permissions don't mean anything and their value is fixed */
+572 -251
View File
File diff suppressed because it is too large. Load diff
+111 -187
View File
@@ -37,129 +37,129 @@ static u64 apfs_fletcher64(void *addr, size_t len)
int apfs_obj_verify_csum(struct super_block *sb, struct buffer_head *bh)
{
struct apfs_obj_phys *obj = (struct apfs_obj_phys *)bh->b_data;
/* The checksum may be stale until the transaction is committed */
if (buffer_trans(bh))
return 1;
return apfs_multiblock_verify_csum(bh->b_data, sb->s_blocksize);
}
return (le64_to_cpu(obj->o_cksum) ==
apfs_fletcher64((char *) obj + APFS_MAX_CKSUM_SIZE,
sb->s_blocksize - APFS_MAX_CKSUM_SIZE));
/**
* apfs_multiblock_verify_csum - Verify an object's checksum
* @object: the object to verify
* @size: size of the object in bytes (may be multiple blocks)
*
* Returns 1 on success, 0 on failure.
*/
int apfs_multiblock_verify_csum(char *object, u32 size)
{
struct apfs_obj_phys *obj = (struct apfs_obj_phys *)object;
u64 actual_csum, header_csum;
header_csum = le64_to_cpu(obj->o_cksum);
actual_csum = apfs_fletcher64(object + APFS_MAX_CKSUM_SIZE, size - APFS_MAX_CKSUM_SIZE);
return header_csum == actual_csum;
}
/**
* apfs_obj_set_csum - Set the fletcher checksum in an object header
* @sb: superblock structure
* @obj: the object header
*
* The object must have a length of a single block.
*/
void apfs_obj_set_csum(struct super_block *sb, struct apfs_obj_phys *obj)
{
u64 cksum = apfs_fletcher64((char *)obj + APFS_MAX_CKSUM_SIZE,
sb->s_blocksize - APFS_MAX_CKSUM_SIZE);
apfs_multiblock_set_csum((char *)obj, sb->s_blocksize);
}
/**
* apfs_multiblock_set_csum - Set an object's checksum
* @object: the object to checksum
* @size: size of the object in bytes (may be multiple blocks)
*/
void apfs_multiblock_set_csum(char *object, u32 size)
{
struct apfs_obj_phys *obj = (struct apfs_obj_phys *)object;
u64 cksum;
cksum = apfs_fletcher64(object + APFS_MAX_CKSUM_SIZE, size - APFS_MAX_CKSUM_SIZE);
obj->o_cksum = cpu_to_le64(cksum);
}
/**
* apfs_cpm_lookup_oid - Search a checkpoint-mapping block for a given oid
* @sb: superblock structure
* @cpm: checkpoint-mapping block (on disk)
* @oid: the ephemeral object id to look up
* @bno: on return, the block number for the object
*
* Returns -EFSCORRUPTED in case of corruption, or -EAGAIN if @oid is not
* listed in @cpm; returns 0 on success.
*/
static int apfs_cpm_lookup_oid(struct super_block *sb,
struct apfs_checkpoint_map_phys *cpm,
u64 oid, u64 *bno)
{
u32 map_count = le32_to_cpu(cpm->cpm_count);
int i;
if (map_count > apfs_max_maps_per_block(sb))
return -EFSCORRUPTED;
for (i = 0; i < map_count; ++i) {
struct apfs_checkpoint_mapping *map = &cpm->cpm_map[i];
if (le64_to_cpu(map->cpm_oid) == oid) {
*bno = le64_to_cpu(map->cpm_paddr);
return 0;
}
}
return -EAGAIN; /* The mapping may still be in the next block */
}
/**
* apfs_read_cpm_block - Read the checkpoint mapping block
* @sb: super block structure
*
* Only a single cpm block is supported for now. Returns the buffer head for
* the block on success, or NULL in case of failure.
*/
static struct buffer_head *apfs_read_cpm_block(struct super_block *sb)
{
struct apfs_nx_superblock *raw_sb = APFS_NXI(sb)->nx_raw;
u64 desc_base = le64_to_cpu(raw_sb->nx_xp_desc_base);
u32 desc_index = le32_to_cpu(raw_sb->nx_xp_desc_index);
u32 desc_blks = le32_to_cpu(raw_sb->nx_xp_desc_blocks);
u32 desc_len = le32_to_cpu(raw_sb->nx_xp_desc_len);
u64 cpm_bno;
if (!desc_blks || desc_len < 2)
return NULL;
/* Last block in area is superblock; we want the last mapping block */
cpm_bno = desc_base + (desc_index + desc_len - 2) % desc_blks;
return apfs_sb_bread(sb, cpm_bno);
}
/**
* apfs_create_cpoint_map - Create a checkpoint mapping
* apfs_create_cpm_block - Create a new checkpoint-mapping block
* @sb: filesystem superblock
* @oid: ephemeral object id
* @bno: block number
* @bno: block number to use
* @bh_p: on return, the buffer head for the block
*
* Only mappings for free queue nodes are supported for now. Returns 0 on
* success or a negative error code in case of failure.
* Returns 0 on success or a negative error code in case of failure.
*/
int apfs_create_cpoint_map(struct super_block *sb, u64 oid, u64 bno)
int apfs_create_cpm_block(struct super_block *sb, u64 bno, struct buffer_head **bh_p)
{
struct buffer_head *bh;
struct apfs_checkpoint_map_phys *cpm;
struct apfs_checkpoint_mapping *map;
u32 cpm_count;
int err = 0;
struct apfs_nxsb_info *nxi = APFS_NXI(sb);
struct apfs_checkpoint_map_phys *cpm = NULL;
struct buffer_head *bh = NULL;
int err;
bh = apfs_read_cpm_block(sb);
if (!bh)
bh = apfs_getblk(sb, bno);
if (!bh) {
apfs_err(sb, "failed to map cpm block");
return -EIO;
cpm = (struct apfs_checkpoint_map_phys *)bh->b_data;
}
err = apfs_transaction_join(sb, bh);
if (err) {
brelse(bh);
return err;
}
set_buffer_csum(bh);
cpm = (void *)bh->b_data;
memset(cpm, 0, sb->s_blocksize);
cpm->cpm_o.o_oid = cpu_to_le64(bno);
cpm->cpm_o.o_xid = cpu_to_le64(nxi->nx_xid);
cpm->cpm_o.o_type = cpu_to_le32(APFS_OBJ_PHYSICAL | APFS_OBJECT_TYPE_CHECKPOINT_MAP);
cpm->cpm_o.o_subtype = cpu_to_le32(APFS_OBJECT_TYPE_INVALID);
/* For now: the caller will have to update these fields */
cpm->cpm_flags = cpu_to_le32(APFS_CHECKPOINT_MAP_LAST);
cpm->cpm_count = 0;
*bh_p = bh;
return 0;
}
/**
* apfs_create_cpoint_map - Create a checkpoint mapping for an object
* @sb: filesystem superblock
* @cpm: checkpoint mapping block to use
* @obj: header for the ephemeral object
* @bno: block number for the ephemeral object
* @size: size of the ephemeral object in bytes
*
* Returns 0 on success or a negative error code in case of failure, which may
* be -ENOSPC if @cpm is full.
*/
int apfs_create_cpoint_map(struct super_block *sb, struct apfs_checkpoint_map_phys *cpm, struct apfs_obj_phys *obj, u64 bno, u32 size)
{
struct apfs_checkpoint_mapping *map = NULL;
u32 cpm_count;
apfs_assert_in_transaction(sb, &cpm->cpm_o);
cpm_count = le32_to_cpu(cpm->cpm_count);
if (cpm_count >= apfs_max_maps_per_block(sb)) { /* TODO */
apfs_warn(sb, "creation of cpm blocks not yet supported");
err = -EOPNOTSUPP;
goto fail;
}
if (cpm_count >= apfs_max_maps_per_block(sb))
return -ENOSPC;
map = &cpm->cpm_map[cpm_count];
le32_add_cpu(&cpm->cpm_count, 1);
map->cpm_type = cpu_to_le32(APFS_OBJ_EPHEMERAL |
APFS_OBJECT_TYPE_BTREE_NODE);
map->cpm_subtype = cpu_to_le32(APFS_OBJECT_TYPE_SPACEMAN_FREE_QUEUE);
map->cpm_size = cpu_to_le32(sb->s_blocksize);
map->cpm_type = obj->o_type;
map->cpm_subtype = obj->o_subtype;
map->cpm_size = cpu_to_le32(size);
map->cpm_pad = 0;
map->cpm_fs_oid = 0;
map->cpm_oid = cpu_to_le64(oid);
map->cpm_oid = obj->o_oid;
map->cpm_paddr = cpu_to_le64(bno);
fail:
brelse(bh);
return err;
return 0;
}
/**
@@ -195,110 +195,26 @@ u64 apfs_data_index_to_bno(struct super_block *sb, u32 index)
}
/**
* apfs_remove_cpoint_map - Remove a checkpoint mapping
* @sb: filesystem superblock
* @bno: block number to delete
*
* Only mappings for free queue nodes are supported for now. Blocks that come
* after the deleted one are assumed to shift back one place. Returns 0 on
* success or a negative error code in case of failure.
*/
int apfs_remove_cpoint_map(struct super_block *sb, u64 bno)
{
struct buffer_head *bh;
struct apfs_checkpoint_map_phys *cpm;
struct apfs_checkpoint_mapping *map, *maps_start, *maps_end;
struct apfs_checkpoint_mapping *bno_map = NULL;
u32 cpm_count;
u32 bno_off;
int err = 0;
bh = apfs_read_cpm_block(sb);
if (!bh)
return -EIO;
cpm = (struct apfs_checkpoint_map_phys *)bh->b_data;
apfs_assert_in_transaction(sb, &cpm->cpm_o);
/* TODO: multiple cpm blocks? */
cpm_count = le32_to_cpu(cpm->cpm_count);
if (cpm_count > apfs_max_maps_per_block(sb)) {
err = -EFSCORRUPTED;
goto fail;
}
maps_start = &cpm->cpm_map[0];
maps_end = &cpm->cpm_map[cpm_count];
bno_off = apfs_index_in_data_area(sb, bno);
for (map = maps_start; map < maps_end; ++map) {
u32 curr_off;
if (le64_to_cpu(map->cpm_paddr) == bno)
bno_map = map;
curr_off = apfs_index_in_data_area(sb, le64_to_cpu(map->cpm_paddr));
if (curr_off > bno_off)
map->cpm_paddr = cpu_to_le64(apfs_data_index_to_bno(sb, curr_off - 1));
}
if (!bno_map) {
err = -EFSCORRUPTED;
goto fail;
}
memmove(bno_map, bno_map + 1, (maps_end - bno_map - 1) * sizeof(*bno_map));
le32_add_cpu(&cpm->cpm_count, -1);
fail:
brelse(bh);
return err;
}
/**
* apfs_read_ephemeral_object - Find and map an ephemeral object
* apfs_ephemeral_object_lookup - Find an ephemeral object info in memory
* @sb: superblock structure
* @oid: ephemeral object id
*
* Returns the mapped buffer head for the object, or an error pointer in case
* Returns a pointer to the object info on success, or an error pointer in case
* of failure.
*/
struct buffer_head *apfs_read_ephemeral_object(struct super_block *sb, u64 oid)
struct apfs_ephemeral_object_info *apfs_ephemeral_object_lookup(struct super_block *sb, u64 oid)
{
struct apfs_nxsb_info *nxi = APFS_NXI(sb);
struct apfs_nx_superblock *raw_sb = nxi->nx_raw;
u64 desc_base = le64_to_cpu(raw_sb->nx_xp_desc_base);
u32 desc_index = le32_to_cpu(raw_sb->nx_xp_desc_index);
u32 desc_blks = le32_to_cpu(raw_sb->nx_xp_desc_blocks);
u32 desc_len = le32_to_cpu(raw_sb->nx_xp_desc_len);
u32 i;
struct apfs_ephemeral_object_info *list = NULL;
int i;
if (!desc_blks || !desc_len)
return ERR_PTR(-EFSCORRUPTED);
/* Last block in the area is superblock; the rest are mapping blocks */
for (i = 0; i < desc_len - 1; ++i) {
struct buffer_head *bh;
struct apfs_checkpoint_map_phys *cpm;
u64 cpm_bno = desc_base + (desc_index + i) % desc_blks;
u64 obj_bno;
int err;
bh = apfs_sb_bread(sb, cpm_bno);
if (!bh)
return ERR_PTR(-EIO);
cpm = (struct apfs_checkpoint_map_phys *)bh->b_data;
err = apfs_cpm_lookup_oid(sb, cpm, oid, &obj_bno);
brelse(bh);
cpm = NULL;
if (err == -EAGAIN) /* Search the next mapping block */
continue;
if (err)
return ERR_PTR(err);
bh = apfs_sb_bread(sb, obj_bno);
if (!bh)
return ERR_PTR(-EIO);
return bh;
list = nxi->nx_eph_list;
for (i = 0; i < nxi->nx_eph_count; ++i) {
if (list[i].oid == oid)
return &list[i];
}
return ERR_PTR(-EFSCORRUPTED); /* The mapping is missing */
apfs_err(sb, "no mapping for oid 0x%llx", oid);
return ERR_PTR(-EFSCORRUPTED);
}
/**
@@ -325,13 +241,16 @@ struct buffer_head *apfs_read_object_block(struct super_block *sb, u64 bno, bool
ASSERT(write || !preserve);
bh = apfs_sb_bread(sb, bno);
if (!bh)
if (!bh) {
apfs_err(sb, "failed to read object block 0x%llx", bno);
return ERR_PTR(-EIO);
}
obj = (struct apfs_obj_phys *)bh->b_data;
type = le32_to_cpu(obj->o_type);
ASSERT(!(type & APFS_OBJ_EPHEMERAL));
if (nxi->nx_flags & APFS_CHECK_NODES && !apfs_obj_verify_csum(sb, bh)) {
apfs_err(sb, "bad checksum for object in block 0x%llx", bno);
err = -EFSBADCRC;
goto fail;
}
@@ -345,10 +264,13 @@ struct buffer_head *apfs_read_object_block(struct super_block *sb, u64 bno, bool
return bh;
err = apfs_spaceman_allocate_block(sb, &new_bno, true /* backwards */);
if (err)
if (err) {
apfs_err(sb, "block allocation failed");
goto fail;
}
new_bh = apfs_getblk(sb, new_bno);
if (!new_bh) {
apfs_err(sb, "failed to map block for CoW (0x%llx)", new_bno);
err = -EIO;
goto fail;
}
@@ -361,6 +283,8 @@ struct buffer_head *apfs_read_object_block(struct super_block *sb, u64 bno, bool
*/
if (!preserve) {
err = apfs_free_queue_insert(sb, bh->b_blocknr, 1);
if (err)
apfs_err(sb, "free queue insertion failed for 0x%llx", (unsigned long long)bh->b_blocknr);
} else if ((type & APFS_OBJECT_TYPE_MASK) != APFS_OBJECT_TYPE_FS) {
vsb_raw = APFS_SB(sb)->s_vsb_raw;
apfs_assert_in_transaction(sb, &vsb_raw->apfs_o);
+181 -59
View File
File diff suppressed because it is too large. Load diff
+425 -160
View File
File diff suppressed because it is too large. Load diff
Loaded 20 of 25 files, more files were not shown because too many files have changed in this diff. Show more