Skip to content

Commit

Permalink
Enable trim/discards support
Browse files Browse the repository at this point in the history
Fixes #47
  • Loading branch information
bwalex committed Dec 7, 2013
1 parent f53de8b commit 8afa0ba
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
Expand Up @@ -10,6 +10,9 @@

* Fixed handling of disks and volumes with 4k sectors (issue #45).

* Add an option to allow TRIM (discards) on the mapped volume
(issue #47).


2013-07-31 Version 1.1 - Alex Hornung <alex@alexhornung.com>

Expand Down
8 changes: 7 additions & 1 deletion main.c
Expand Up @@ -155,6 +155,8 @@ usage(void)
"\t Protect a hidden volume when mounting the outer volume.\n"
" -s <disk path>, --system-encryption=<disk path>\n"
"\t Specifies that the disk (e.g. /dev/da0) is using system encryption.\n"
" -t, --allow-trim\n"
"\t Allow discards (TRIM command) on mapped volume.\n"
" --fde\n"
"\t Specifies that the disk (e.g. /dev/da0) is using full disk encryption.\n"
" --use-backup\n"
Expand Down Expand Up @@ -192,6 +194,7 @@ static struct option longopts[] = {
{ "protect-hidden", no_argument, NULL, 'e' },
{ "device", required_argument, NULL, 'd' },
{ "system-encryption", required_argument, NULL, 's' },
{ "allow-trim", no_argument, NULL, 't' },
{ "fde", no_argument, NULL, FLAG_LONG_FDE },
{ "use-backup", no_argument, NULL, FLAG_LONG_USE_BACKUP },
{ "modify", no_argument, NULL, FLAG_LONG_MOD },
Expand Down Expand Up @@ -242,7 +245,7 @@ main(int argc, char *argv[])
n_hkeyfiles = 0;
n_newkeyfiles = 0;

while ((ch = getopt_long(argc, argv, "a:b:cd:ef:ghij:k:m:s:u:vwx:y:z",
while ((ch = getopt_long(argc, argv, "a:b:cd:ef:ghij:k:m:s:tu:vwx:y:z",
longopts, NULL)) != -1) {
switch(ch) {
case 'a':
Expand Down Expand Up @@ -300,6 +303,9 @@ main(int argc, char *argv[])
flags |= TC_FLAG_SYS;
sys_dev = optarg;
break;
case 't':
flags |= TC_FLAG_ALLOW_TRIM;
break;
case 'u':
unmap_vol = 1;
map_name = optarg;
Expand Down
8 changes: 7 additions & 1 deletion tcplay.3
Expand Up @@ -28,7 +28,7 @@
.\" OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.Dd July 30, 2013
.Dd December 07, 2013
.Dt TCPLAY 3
.Os
.Sh NAME
Expand Down Expand Up @@ -115,6 +115,7 @@ typedef struct tc_api_opts {
const char *tc_system_device;
int tc_use_fde;
int tc_use_backup;
int tc_allow_trim;

/* Fields for modify */
const char *tc_new_passphrase;
Expand Down Expand Up @@ -412,6 +413,11 @@ is specified,
.Nm tcplay
will use the backup headers at the end of a volume
instead of the primary headers to access it.
If
.Fa tc_allow_trim
is specified,
.Nm tcplay
will allow TRIM (discards) on the mapped volume.
.Pp
The
.Fn tc_api_unmap_volume
Expand Down
11 changes: 10 additions & 1 deletion tcplay.8
Expand Up @@ -29,7 +29,7 @@
.\" OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.Dd July 29, 2013
.Dd December 07, 2013
.Dt TCPLAY 8
.Os
.Sh NAME
Expand Down Expand Up @@ -66,6 +66,7 @@
.Op Fl f Ar keyfile_hidden
.Op Fl k Ar keyfile
.Op Fl s Ar system_device
.Op Fl t
.Op Fl -fde
.Op Fl -use-backup
.Nm
Expand Down Expand Up @@ -271,6 +272,14 @@ to access the volume.
.El
.Pp
Additional options only for the
.Fl -map
command are:
.Bl -tag -width indent
.It Fl t , Fl -allow-trim
This option enables TRIM (discard) support on the mapped volume.
.El
.Pp
Additional options only for the
.Fl -modify
command are:
.Bl -tag -width indent
Expand Down
11 changes: 6 additions & 5 deletions tcplay.c
Expand Up @@ -1489,7 +1489,7 @@ dm_get_table(const char *name)
++c;
}

if (c != 5) {
if (c < 5) {
tc_log(1, "could not get all the info required from "
"the table\n");
goto error;
Expand Down Expand Up @@ -1796,12 +1796,13 @@ dm_setup(const char *mapname, struct tcplay_info *info)
start = INFO_TO_DM_BLOCKS(info, offset);
}

/* aes-cbc-essiv:sha256 7997f8af... 0 /dev/ad0s0a 8 */
/* iv off---^ block off--^ */
snprintf(params, 512, "%s %s %"PRIu64 " %s %"PRIu64,
/* aes-cbc-essiv:sha256 7997f8af... 0 /dev/ad0s0a 8 <opts> */
/* iv off---^ block off--^ <opts> */
snprintf(params, 512, "%s %s %"PRIu64 " %s %"PRIu64 " %s",
cipher_chain->cipher->dm_crypt_str, cipher_chain->dm_key,
(uint64_t)INFO_TO_DM_BLOCKS(info, skip), dev,
(uint64_t)offset);
(uint64_t)offset,
TC_FLAG_SET(info->flags, ALLOW_TRIM) ? "1 allow_discards" : "");
#ifdef DEBUG
printf("Params: %s\n", params);
#endif
Expand Down
1 change: 1 addition & 0 deletions tcplay.h
Expand Up @@ -67,6 +67,7 @@
#define TC_FLAG_FDE 0x0002
#define TC_FLAG_BACKUP 0x0004
#define TC_FLAG_ONLY_RESTORE 0x0008
#define TC_FLAG_ALLOW_TRIM 0x0010

#define TC_FLAG_SET(f, x) ((f & TC_FLAG_##x) == TC_FLAG_##x)

Expand Down
2 changes: 2 additions & 0 deletions tcplay_api.c
Expand Up @@ -178,6 +178,8 @@ tc_api_map_volume(tc_api_opts *api_opts)
flags |= TC_FLAG_FDE;
if (api_opts->tc_use_backup)
flags |= TC_FLAG_BACKUP;
if (api_opts->tc_allow_trim)
flags |= TC_FLAG_ALLOW_TRIM;

err = map_volume(api_opts->tc_map_name, api_opts->tc_device,
flags, api_opts->tc_system_device,
Expand Down
1 change: 1 addition & 0 deletions tcplay_api.h
Expand Up @@ -61,6 +61,7 @@ typedef struct tc_api_opts {
const char *tc_system_device;
int tc_use_fde;
int tc_use_backup;
int tc_allow_trim;

/* Fields for modify */
const char *tc_new_passphrase;
Expand Down
1 change: 1 addition & 0 deletions test/features/support/env.rb
Expand Up @@ -28,6 +28,7 @@ class TCApiOpts < FFI::Struct
:tc_system_device, :pointer,
:tc_use_fde, :int,
:tc_use_backup, :int,
:tc_allow_trim, :int,

# Fields for modify
:tc_new_passphrase, :pointer,
Expand Down

0 comments on commit 8afa0ba

Please sign in to comment.