Commit graph

82 commits

Author SHA1 Message Date
Palmer Dabbelt
eef49c9e3c libdm: dm-user target creation takes an additional argument
Rather than using some implicit naming scheme, we've decided to just let
userspace pick whatever control device naming scheme it wants.  This requires
an additional argument to be passed to the kernel.

Test: cow_snapuserd_test
Bug: 168259959
Signed-off-by: Palmer Dabbelt <palmerdabbelt@google.com>
Change-Id: Ibf7f378b67abb6ca7457a9fdc9b8739e3d31bee3
Signed-off-by: Akilesh Kailash <akailash@google.com>
2020-10-16 18:16:55 +00:00
Dan Shi
67b7dd570f Remove vts10 related configs
Bug: 169582597
Test: build
Change-Id: I38e1e0f8e32b86ab14b905d040c1c62245a1a9e3
2020-10-06 13:52:44 -07:00
Akilesh Kailash
516ded7320 Support of multiple device partitions by dm-user driver
Add basic support for daemon creation and handle signals

Test: Add test case to test system and product COW partitions
Bug: 162790322

Signed-off-by: Akilesh Kailash <akailash@google.com>
Change-Id: Icf8dbe49d2237cec6f7dbcdd84256eb6c5afa1cd
2020-09-16 00:35:42 +00:00
Palmer Dabbelt
7dd8fe8e70 Add "dmctl uuid" command
This makes looking up device UUIDs a bit easier, as rather than depending on
the device mapper's sysfs layout we can depend on dmctl.  There's some
associated libdm plumbing, but the UUID was already pretty much availiable.

Test: I just ran this by hand.
Signed-off-by: Palmer Dabbelt <palmerdabbelt@google.com>
Change-Id: I7028eec6ab04601308047b67057e51a0ff44c0a7
2020-09-15 18:39:39 +00:00
David Anderson
8a11622bca Add experimental daemon for handling dm-user requests.
Right now this is open-coded. As the API stabilizes it will be
integrated into libdm.

Bug: 154536437
Test: manual test
Change-Id: I07bb811b6fb80d85d55e218d788023634c8158b7
2020-07-08 15:46:27 -07:00
Palmer Dabbelt
2def03a2b9 Add libdm and dmctl support for dm-user targets
dm-user is very much a WIP and while this may change (we need to figure out how
to get credentials to the dm-user daemon, for example) it seems like a somewhat
safe bet to assume that the behavior will at least include a start/end range.

Test: I just ran "dmctl create palmer user 0 1024", which created the device.
Signed-off-by: Palmer Dabbelt <palmerdabbelt@google.com>
Change-Id: Ic5f84de6a4f09bf906246035d450edd637cc38ed
2020-06-26 12:24:53 -07:00
Paul Lawrence
2709331f90 Merge "Set block size in dm-bow" 2020-06-08 17:07:36 +00:00
Paul Lawrence
2f0c6cb0f7 Set block size in dm-bow
Fix block-level checkpointing to work correctly when used in combination with
512 byte hardware sectors and metadata encryption with dm-default-key v2.

Bug: 153512828
Test: Parameter is passed to dm-bow based on first_api_level
Change-Id: Ic0a071221559271db20b06b2f17459b5b041e02d
2020-06-05 07:47:45 -07:00
David Anderson
a212760ec6 libdm: Fallback to legacy ueventd logic if running on an older system.
When delivering single-stage, non-AB OTAs, the updater binary is built
on a newer OS than recovery is compiled with. libdm relies on newer ueventd
behavior which therefore breaks this model. As a workaround, we allow
libdm to fallback to the old ueventd logic if the following conditions
hold true: (1) we're in recovery, (2) the device is not an AB device,
and (3) the release is <= 10.

Since the old ueventd behavior can lead to races in libdm, this fallback
should stay as narrow as possible.

Bug: 156536673
Bug: 155202260
Test: manual test
Change-Id: I7f9da49e4ba8dfe165e0923d9918827d51d090cd
2020-05-28 20:44:31 -07:00
Treehugger Robot
edf8335c72 Merge "Rename vts-core to vts" 2020-04-07 05:11:55 +00:00
Dan Shi
ab8acaedae Rename vts-core to vts
Bug: 151896491
Test: local build
Exempt-From-Owner-Approval: This CL moves all tests in vts-core to vts.
It won't change test logic or behavior.

Change-Id: I24779951257ce37fc18929f214d3cf5f76c23a19
Merged-In: I24779951257ce37fc18929f214d3cf5f76c23a19
2020-04-07 02:48:48 +00:00
Paul Crowley
539b98fb58 Abolish DmTargetDefaultKey::IsLegacy
To make it easier to support disk formats created using old versions
of dm-default-key with new kernels, stop checking the kernel module
version; instead, the caller decides whether to use the old or new
options format.

Bug: 150761030
Test: crosshatch and cuttlefish boot normally; cuttlefish
    fails with "default-key: Not enough arguments" as expected when
    option is set to 1
Change-Id: I810bb7e085b9b8648858de1a02165162d1fd163e
2020-04-05 19:51:14 -07:00
David Anderson
e580566b00 libdm: Link libraries statically for VTS.
Bug: 152180548
Test: 32-bit libdm_test
Change-Id: I9f356c854833b5e68820e4d3d4e9709af1288381
2020-03-25 23:43:22 -07:00
Treehugger Robot
ecfdda2c14 Merge "Convert vts configs to Android.bp" 2020-03-10 05:00:33 +00:00
David Anderson
66147bad94 Merge "libdm: Fix WaitForFile early-returning on failed accesses." 2020-03-10 00:06:09 +00:00
Dan Willemsen
a8f188f011 Convert vts configs to Android.bp
Test: treehugger
Change-Id: Ibe8ed03555ac78ee38be6511eb4169cddfcd3555
2020-03-07 21:21:33 -08:00
David Anderson
1a17e18f17 libdm: Make static std::string inline for DmTargetDefaultKey.
Bug: 149942628
Test: snapshotctl w/ ASAN build
Change-Id: I0f40b1f29e46bc9afaf19d8c21d62280e2c01074
2020-02-28 17:04:36 -08:00
David Anderson
940c6b26ef libdm: Fix WaitForFile early-returning on failed accesses.
WaitForFile/WaitForDeletedFile both early return true if an error like
EPERM occurs. This was intentional because the code was modeled off
earlier fs_mgr code, but it makes libdm inherently racy if sepolicy is
not configured correctly. It's better to have these result in explicit
and consistent failures.

Bug: 148103327
Test: fastboot flashall
Change-Id: I0c78818962e1db91b556e523c418db28f7d78fae
Merged-In: I0c78818962e1db91b556e523c418db28f7d78fae
2020-02-26 15:21:53 -08:00
Barani Muthukumaran
2ca1d83ddb Add wrapped key support for metadata encryption
Change metadata_cipher fstab option to metadata_encryption
that includes encryption flags in addition to the cipher.
wrappedkey_v0 encryption flag is used to denote that the
inline encryption hardware supports wrapped keys. dm-default-key
device is created and a wrappedkey is provided along with the
optional wrappedkey_v0 argument.

Bug: 147733587

Test: FBE validation with Fscrypt v2 + inline crypt + wrapped
key changes kernel and metadata encryption with wrapped key.

Change-Id: Id1a18db175680dd6b0adb4594d06566eb1285785
2020-02-18 12:44:40 -08:00
Alessio Balsini
d7cc465481 Add libdm_test to presubmit
Also add libdm_test to the TEST_MAPPING's presubmit of fs_mgr to run the
test on TH whenever changes are applied to the fs_mgr contents.

Test: TH
Bug: none
Change-Id: Icc6adeeacaefbcfeb494f37fc776eb5a886a5183
Signed-off-by: Alessio Balsini <balsini@google.com>
2020-02-03 17:34:44 +00:00
Alessio Balsini
fb19ed9130 Update assertion on DmTargetDefaultKey param string
Due to some recent code updates that introduced some additional
extra_argv, the DmTargetDefaultKey::GetParameterString() test was
failing.
Update the string accordingly.

Bug: none
Test: libdm_test
Change-Id: I63fab27e32a08211a43d4804d90a6f19ea4edc06
Signed-off-by: Alessio Balsini <balsini@google.com>
2020-02-03 10:37:36 +00:00
Paul Crowley
f1b264f17c Add support for v2 of dm-default-key
Version 2 of dm-default-key has an extra parameter and always sets the
DUN.

Bug: 147814592
Test: Cuttlefish boots with keydirectory flag
Test: Crosshatch formatted before this change boots after it
Change-Id: I309bcc3f907a6df745f5c073b0017a7dd5b5354b
2020-01-31 14:45:00 -08:00
Paul Crowley
7823e327c1 Rename key_dir to metadata_key_dir and refactor
Bug: 147814592
Test: Crosshatch boots
Change-Id: I18b03486d2e93c6103880d1af68d9f70147fde63
2020-01-30 16:03:45 -08:00
Yifan Hong
a16f2c81a8 libsnapshot: snaity check overflow bit at the end of update
Make sure CoW device doesn't overflow. Otherwise, data previously
written to snapshot device may be overwritten.

This check acts as a safety guard to bug like b/145180464, where
the computed CoW device size is less than required, but cannot be
caught by FilesystemVerifierAction in update_engine.

Note that this is a sanity check. It doesn't prevent the following:
(1) write a snapshot until it overflows
(2) unmap and re-map the snapshot
(3) Call FinishedSnapshotWrites()

When a snapshot is re-mapped, DeviceMapper::GetTableStatus() won't
return "Overflow".

However, update_engine always writes the full hashtree / FEC / etc.
data (outside of |PartitionUpdate.operations|), calls
FinishedSnapshotWrites(), and then writes the checkpoint. If the process
is interrupted, update_engine does the full FilesystemVerifierAction
from the beginning. Snapshots aren't remapped during the process. Hence,
the hypothetical case above won't happen in reality (at time of writing)
until FilesystemVerifierAction is broken down into steps with
checkpoints.

Still, given the above hypothetical case, this function is only served
as a sanity check.

FinishedSnapshotWrites() now requires all snapshots to be mapped before
calling. Hence, tests needs to be fixed:
- For SnapshotTest (that tests SnapshotManager private APIs), map
  snapshot before calling FinishedSnapshotWrites().
- Factor out common code in SnapshotTest (PrepareOneSnapshot and
  SimulateReboot)
- For SnapshotUpdateTest and children, add MapUpdateSnapshots() helper
  and map all snapshots before calling FinishedSnapshotWrites().

Test: libsnapshot_test
Bug: 145180464

Change-Id: I3558dd1615059ba27f369c27af22e3c686e392f7
2019-12-09 16:06:03 -08:00
Nikita Ioffe
9fb63038f7 libdm: add an api to unwind dm stack
This API is an attempt to consolidate dm-stack unwinding logic
re-implemented in several different places (libfsmgr, libfstab,
libfiemap, etc.).

Test: libdm_test
Bug: 135984674
Change-Id: I9672d4c1e882824ef980667818d4c3aabcb82e1f
2019-12-06 22:10:45 +00:00
Nikita Ioffe
2c28919d48 libdm: add an overload of DeleteDevice accepting a timeout_ms
In some scenarios (e.g. apexd or userspace reboot), dm-devices are
getting deleted and re-created. Since this operation can be racy (newly
created device can get the same path as the previously deleted one,
resulting in the unexpected ENOENT errors on a system call to the path),
it will be nice to have an API that blocks until ueventd processes
corresponding udev events.

Test: libdm_test
Bug: 143970043
Bug: 122059364
Change-Id: I31a19afd9e245bf5e3554011bdde1c3cc4878f1c
2019-12-05 10:37:47 +00:00
Alessio Balsini
d0de1114a9 Fix fuzzer name and sizeof(array)
The fuzzer name "dm_table_fuzzer" was too generic. Looking forward to an
extension to host tests where each fuzzer name should be considered a
unique identifier, change the fuzzer name to, "dm_linear_table_fuzzer".

Fix also the syntax of sizeof(array) declaration.

Bug: none
Test: dm_linear_table_fuzzer
Change-Id: Iaa0ee9a0eb1352f0c5269b07198d0a34d8fb3254
Signed-off-by: Alessio Balsini <balsini@google.com>
2019-10-21 11:44:08 +00:00
Alessio Balsini
08916b9ea2 Fuzzer for libdm's dm-linear devices creation
Create a fuzzer that fuzzes the parameters used for the creation
of dm-linear tables through libdm.
This fuzzer is based on libdm::DmLinear.

Bug: none
Test: dm_linear_fuzzer
Change-Id: I052144c6eb0db4fa6a37b5e54fbcb8193e5dc88f
Signed-off-by: Alessio Balsini <balsini@google.com>
2019-10-10 20:59:33 +01:00
Alessio Balsini
5e6dc03fd5 Move TempDevice to test_util.h
TempDevice is useful class for tests, move it to test_util.h to improve
its visibility and make it usable by other tests.

Bug: none
Test: none
Change-Id: Idad5667844b10559b24f8f5e657e47d99e14209e
Signed-off-by: Alessio Balsini <balsini@google.com>
2019-10-10 20:59:33 +01:00
Yifan Hong
55fb297a0c libdm: Fix DmTable::num_sectors
Test: libdm_test
Change-Id: If147df2d5c4f3b3df7ca57200f16a9ad59ae8327
2019-10-01 19:43:33 -07:00
Yifan Hong
9c7903124c Fix fds libdm_test
Failed because of double free of fds.
Test: run it

Change-Id: I25d7d590ca52d57fb14a5483ff8751127f6a48a6
2019-10-01 19:43:33 -07:00
nelsonli
3beec54bf1 [vts-core] Remove extra folder for vts_core_liblp_test and vts_libdm_test
Remove the extra folder of vts_core for vts_core_liblp_test and
vts_libdm_test since we have a better auto-gen option of auto_gen_config.

Bug: 141684102
Test: 1. m -j vts_core_liblp_test
      2. m -j vts_libdm_test
      3. check out/.../vts_core_liblp_test.config
         and
         out/.../vts_libdm_test.config

Change-Id: Ibcdf96104205b50b3da522364199330ad0190c70
2019-09-27 15:00:12 +08:00
nelsonli
5642e23949 [vts-core] Add libdm_test in vts-core
Add libdm_test to vts-core suite and skip the test if the
first-api-level < 29.

Bug: 132702215
Test: atest vts_libdm_test
Change-Id: Ic805a4c08e5bf9f6c69019d4ea01825ae888e1fe
2019-09-24 11:34:59 +08:00
Paul Lawrence
8c8ce02e2e Merge "Fix ext4/metadata/udc issue" 2019-08-27 15:21:58 +00:00
David Anderson
370226e662 libdm: Add a helper function for GetState+DeleteDevice checks.
Bug: N/A
Test: manual test
Change-Id: Ie97b63b53f548af1e82b73893fa491fac42b32ea
2019-08-21 12:27:56 -07:00
Paul Lawrence
f3405ad73e Fix ext4/metadata/udc issue
Test: Cannot currently test since no device supports this combo
Bug: 137924328
Change-Id: I6474211e7229323da831f99d9572283e0c2d49df
2019-08-14 10:10:31 -07:00
David Anderson
5312c466b9 libsnapshot: Implement merge flow.
This implements InitiateMerge() and WaitForMerge(). InitiateMerge() is
meant to be called after an update has been marked successful.
WaitForMerge() is designed to be called either: immediately after
InitiateMerge, or during each subsequent boot where merging has not
completed.

InitiateMerge converts each snapshot device to a snapshot-merge device.

WaitForMerge polls each snapshot-merge device until no device reports a
"merging" state. One of the following states can result from this:
 - MergeFailed. This will happen if any device failed to merge, or we
   were unable to poll, or any other system-level failure occurred.
 - MergeNeedsReboot. This will happen if a snapshot-merge device has
   completed merging, but we were unable to clean it up due to something
   holding a resource open.
 - MergeCompleted. This indicates that all snapshots completed merging
   and were cleaned up.

If WaitForMerge() returns MergeCompleted, then all snapshots have been
removed and a new update can begin. GetUpdateState() will return None.

MergeFailed and MergeNeedsReboot, on the other hand, are "sticky". They
indicate a merge is still pending. When called again, WaitForMerge()
will poll again to attempt to make more progress in the merge. For
NeedsReboot, a single reboot will ensure all resources are released and
the next WaitForMerge() will successfully finish cleanup. In the failure
case, it is unlikely the next WaitForMerge will succeed, but we always
retry anyway (there is no harm in doing so, and if we get lucky, the
device can take more OTAs).

Bug: 136678799
Test: libsnapshot_test gtests
Change-Id: I5e93fcbffee1973da5ff76363df12d6317a7a7c7
2019-08-07 18:42:23 -07:00
David Anderson
23a87716b5 Merge "Implement basic libsnapshot functionality." 2019-08-03 01:30:49 +00:00
David Anderson
456e50193b Implement basic libsnapshot functionality.
This CL implements some of the libsnapshot internals necessary to work
with update_engine. In particular it implements snapshot and update
state, as well as creating and mapping snapshot devices. It does not
implement anything related to merging, nor does it implement the full
update_engine flow.

Update state is stored in /metadata/ota/state. To synchronize callers of
libsnapshot, we always flock() this file at the top of public functions
in SnapshotManager. Internal functions are only called while the lock is
held, and a "LockedFile" guard object is always passed through to
indicate proof-of-lock.

Low-level functions, such as snapshot management, have been moved to
private methods. Higher-level methods designed for update_engine will
ultimately call into these.

This CL also adds some functional tests for SnapshotManager. Test state
is stored in /metadata/ota/test to avoid conflicts with the rest of the
system.

Bug: 136678799
Test: libsnapshot_test gtest
Change-Id: I78c769ed33b307d5214ee386bb13648e35db6cc6
2019-08-02 13:30:08 -07:00
Alessio Balsini
3565e31b42 libdm: compute percentage of snapshot-merge
Computes the merge completion percentage from the sector information.

Provided test for the function.

Change-Id: I64d83baa0478f9e6969636ee067174910d9b8e03
Bug: N/A
Test: dm_test
Signed-off-by: Alessio Balsini <balsini@google.com>
2019-08-01 02:22:24 +01:00
Alessio Balsini
4560856e33 libdm: improve ParseStatusText() and test it
Simplify the argument parsing of DmTargetSnapshot::ParseStatusText() and
improve its robustness when dealing with wrong imputs.

Add test for DmTargetSnapshot::ParseStatusText().

Change-Id: I7f078c9ecacb402e71db49e3e7072e37cffbc234
Test: dm_test
Signed-off-by: Alessio Balsini <balsini@google.com>
2019-07-31 21:23:58 +01:00
Alessio Balsini
1b80a23c57 libdm: fetch and present device status flags
Fetch detailed information for devices through its flags and store in
helper Info class, i.e.:
- active
- access
- activeTable
- inactiveTable
- bufferFull

Change-Id: I3241c5bca00e038d19f99390f40710ca7cff8456
Bug: 137759376
Test: manual test
Signed-off-by: Alessio Balsini <balsini@google.com>
2019-07-31 00:14:30 +01:00
Alessio Balsini
bdc65bf083 libdm: suspend and resume devices
In some cases it would be required to suspend the device to succesfully
complete some operations. An example is the suspension of the origin
device that is necessary to avoid data corruption when merging a
snapshot.

Introduce suspend and resume ioctls in libdm.

Bug: 137759376
Test: libdm_test
Change-Id: Id2ff34e930a8b32e570cb9f49da9cc3f65cb499c
Signed-off-by: Alessio Balsini <balsini@google.com>
2019-07-29 20:35:56 +01:00
David Anderson
4f9d1b15b4 libdm: Fix race conditions in LoopControl::Attach.
This fixes two race conditions in LoopControl::Attach(). The first is
that after LOOP_CTL_GET_FREE, the path is not be available until it has
been processed by ueventd. This can be fixed by adding a timeout
parameter and a call to WaitForFile().

Second, it is possible (albeit unlikely), given that loop devices are
now being used more aggressively, that two processes race when
attempting LOOP_SET_FD. In this case, one process will win, and the
other will fail with EBUSY. We can handle this case by retrying the
operation while respecting the same timeout parameter.

Bug: 135771280
Test: libdm_test gtest
Change-Id: Icf9facc3ca28fdb6ff5c78612d3dc183fa47b1f3
2019-07-11 15:39:53 -07:00
David Anderson
924858cd18 libdm: Improve the reliability of dm device paths.
This fixes a race condition where WaitForFile() after
GetDmDevicePathByName appears to succeed, but a subsequent operation on
the path fails. This can happen when CreateDevice() is called
immediately after a call to DeleteDevice (from any process), and the
path is re-used, enqueuing udev events to remove and re-add the block
device.

The fix for this is to introduce a new variant of CreateDevice() that
has a timeout parameter. When the timeout is positive, CreateDevice()
will wait for a /dev/block/mapper/by-uuid symlink to be created, which
signals that ueventd has finished processing the operation.

ueventd will now create these by-uuid symlinks for device-mapper nodes.
Unfortunately, the uuid is only available during "change" events, so we
have to special case device-mapper symlink creation. And since the uuid
is not available during "remove" events, we simply find matching links
to remove them.

This ensures that callers of CreateDevice() can use the device path
knowing that no asynchronous removals are pending. Code that uses the
old CreateDevice+WaitForFile pattern will be transitioned to the new
method.

Note that it is safe to ignore the timeout, or to use the "unsafe"
CreateDevice, if the caller ensures the path by other means. For example
first-stage init has no device removal, and regenerates uevents until
it has acquired all the paths it needs.

Finally, since libdm now inspects sysfs unconditionally, libdm consumers
need r_dir_file perms for sysfs_dm in their sepolicy. Additionally
linking to libdm now requires linking to libext2_uuid.

Bug: 135771280
Test: libdm_test
      device flashes, boots
Change-Id: If5a7383ea38f32a7fbbcf24842dce6a668050a70
2019-07-11 15:39:52 -07:00
Tom Cherry
247ffbf314 Fix a few clang-tidy issues and add NOLINT for others
android-base:
* Add NOLINT for expanding namespace std for std::string* ostream
  overload

libdm:
* Fix missing parentesis around macro parameters

init:
* Fix missing CLOEXEC usage and add NOLINT for the intended
  usages.
* Fix missing parentesis around macro parameters
* Fix erase() / remove_if() idiom
* Correctly specific unsigned char when intended
* 'namespace flags' should be signed, since 'flags' it signed for
  clone()
* Add clear to property restore vector<string> to empty after move
* Explicit comparison against 0 for strcmp

Test: build
Change-Id: I8c31dafda2c43ebc5aa50124cbbd6e23ed2c4101
2019-07-09 16:17:36 +00:00
David Anderson
87db478ad6 Merge "libfiemap_writer: Improve device unwrapping to support simple linear devices." 2019-06-20 23:59:06 +00:00
David Anderson
1bfcd580c8 libfiemap_writer: Improve device unwrapping to support simple linear devices.
Normally we reject complex dm targets that would invalidate or shift the
block mappings returned via FIEMAP/FIBMAP. Currently the only targets
allowed are crypt, default-key, and bow. This patch adds support for
"linear" as long as there is only one linear target and it targets
sector 0 of the underlying block device.

This is useful for testing gsid, so we can simulate how a
metadata-encrypted device works without having to create a dm-crypt or
dm-default-key node.

Bug: 134536978
Test: manual test
Change-Id: I7c12bc20d95ff4c90402e66bafb4cf2fce7818e2
2019-06-20 13:39:11 -07:00
David Anderson
ea0dda1473 libdm: Add LoopControl helpers for enabling direct IO.
Bug: 134536978
Test: manual test
Change-Id: Iae25434ac54186fd6006b56eeb7a7f577a880053
2019-06-19 19:29:15 -07:00
David Anderson
d106f1e225 libdm: Add helpers for getting the device number of a dm device.
device-mapper will accept a "major:minor" string in place of a path for
any target parameter that specifies another block devie. This is useful
to have a helper for, since it lets us avoid a call to WaitForFile().

Bug: 134536978
Test: manual test
Change-Id: I13fd6699dd5d405bfa676830825b006b7810aa0f
2019-06-19 19:29:09 -07:00