Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "EncryptInplace.h" |
| 18 | |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 19 | #include <ext4_utils/ext4.h> |
| 20 | #include <ext4_utils/ext4_utils.h> |
| 21 | #include <f2fs_sparseblock.h> |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 22 | #include <fcntl.h> |
| 23 | #include <inttypes.h> |
| 24 | #include <stdint.h> |
| 25 | #include <stdio.h> |
| 26 | #include <sys/stat.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <time.h> |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 29 | |
| 30 | #include <algorithm> |
| 31 | |
Paul Crowley | 772cc85 | 2018-02-01 09:53:27 -0800 | [diff] [blame] | 32 | #include <android-base/logging.h> |
| 33 | #include <android-base/properties.h> |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 34 | |
| 35 | // HORRIBLE HACK, FIXME |
| 36 | #include "cryptfs.h" |
| 37 | |
| 38 | // FIXME horrible cut-and-paste code |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 39 | static inline int unix_read(int fd, void* buff, int len) { |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 40 | return TEMP_FAILURE_RETRY(read(fd, buff, len)); |
| 41 | } |
| 42 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 43 | static inline int unix_write(int fd, const void* buff, int len) { |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 44 | return TEMP_FAILURE_RETRY(write(fd, buff, len)); |
| 45 | } |
| 46 | |
| 47 | #define CRYPT_SECTORS_PER_BUFSIZE (CRYPT_INPLACE_BUFSIZE / CRYPT_SECTOR_SIZE) |
| 48 | |
| 49 | /* aligned 32K writes tends to make flash happy. |
| 50 | * SD card association recommends it. |
| 51 | */ |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 52 | #define BLOCKS_AT_A_TIME 8 |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 53 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 54 | struct encryptGroupsData { |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 55 | int realfd; |
| 56 | int cryptofd; |
| 57 | off64_t numblocks; |
| 58 | off64_t one_pct, cur_pct, new_pct; |
| 59 | off64_t blocks_already_done, tot_numblocks; |
| 60 | off64_t used_blocks_already_done, tot_used_blocks; |
Paul Lawrence | 236e5e8 | 2019-06-25 14:44:33 -0700 | [diff] [blame] | 61 | const char* real_blkdev; |
| 62 | const char* crypto_blkdev; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 63 | int count; |
| 64 | off64_t offset; |
| 65 | char* buffer; |
| 66 | off64_t last_written_sector; |
| 67 | int completed; |
| 68 | time_t time_started; |
| 69 | int remaining_time; |
Paul Crowley | 0fd2626 | 2018-01-30 09:48:19 -0800 | [diff] [blame] | 70 | bool set_progress_properties; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 71 | }; |
| 72 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 73 | static void update_progress(struct encryptGroupsData* data, int is_used) { |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 74 | data->blocks_already_done++; |
| 75 | |
| 76 | if (is_used) { |
| 77 | data->used_blocks_already_done++; |
| 78 | } |
| 79 | if (data->tot_used_blocks) { |
| 80 | data->new_pct = data->used_blocks_already_done / data->one_pct; |
| 81 | } else { |
| 82 | data->new_pct = data->blocks_already_done / data->one_pct; |
| 83 | } |
| 84 | |
Paul Crowley | 0fd2626 | 2018-01-30 09:48:19 -0800 | [diff] [blame] | 85 | if (!data->set_progress_properties) return; |
| 86 | |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 87 | if (data->new_pct > data->cur_pct) { |
| 88 | char buf[8]; |
| 89 | data->cur_pct = data->new_pct; |
| 90 | snprintf(buf, sizeof(buf), "%" PRId64, data->cur_pct); |
Paul Crowley | 772cc85 | 2018-02-01 09:53:27 -0800 | [diff] [blame] | 91 | android::base::SetProperty("vold.encrypt_progress", buf); |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | if (data->cur_pct >= 5) { |
| 95 | struct timespec time_now; |
| 96 | if (clock_gettime(CLOCK_MONOTONIC, &time_now)) { |
Paul Crowley | 772cc85 | 2018-02-01 09:53:27 -0800 | [diff] [blame] | 97 | LOG(WARNING) << "Error getting time"; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 98 | } else { |
| 99 | double elapsed_time = difftime(time_now.tv_sec, data->time_started); |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 100 | off64_t remaining_blocks = data->tot_used_blocks - data->used_blocks_already_done; |
| 101 | int remaining_time = |
| 102 | (int)(elapsed_time * remaining_blocks / data->used_blocks_already_done); |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 103 | |
| 104 | // Change time only if not yet set, lower, or a lot higher for |
| 105 | // best user experience |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 106 | if (data->remaining_time == -1 || remaining_time < data->remaining_time || |
| 107 | remaining_time > data->remaining_time + 60) { |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 108 | char buf[8]; |
| 109 | snprintf(buf, sizeof(buf), "%d", remaining_time); |
Paul Crowley | 772cc85 | 2018-02-01 09:53:27 -0800 | [diff] [blame] | 110 | android::base::SetProperty("vold.encrypt_time_remaining", buf); |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 111 | data->remaining_time = remaining_time; |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 117 | static void log_progress(struct encryptGroupsData const* data, bool completed) { |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 118 | // Precondition - if completed data = 0 else data != 0 |
| 119 | |
| 120 | // Track progress so we can skip logging blocks |
| 121 | static off64_t offset = -1; |
| 122 | |
| 123 | // Need to close existing 'Encrypting from' log? |
| 124 | if (completed || (offset != -1 && data->offset != offset)) { |
Paul Crowley | 772cc85 | 2018-02-01 09:53:27 -0800 | [diff] [blame] | 125 | LOG(INFO) << "Encrypted to sector " << offset / info.block_size * CRYPT_SECTOR_SIZE; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 126 | offset = -1; |
| 127 | } |
| 128 | |
| 129 | // Need to start new 'Encrypting from' log? |
| 130 | if (!completed && offset != data->offset) { |
Paul Crowley | 772cc85 | 2018-02-01 09:53:27 -0800 | [diff] [blame] | 131 | LOG(INFO) << "Encrypting from sector " << data->offset / info.block_size * CRYPT_SECTOR_SIZE; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | // Update offset |
| 135 | if (!completed) { |
| 136 | offset = data->offset + (off64_t)data->count * info.block_size; |
| 137 | } |
| 138 | } |
| 139 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 140 | static int flush_outstanding_data(struct encryptGroupsData* data) { |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 141 | if (data->count == 0) { |
| 142 | return 0; |
| 143 | } |
| 144 | |
Sudheer Shanka | 4b6ca4e | 2018-09-21 10:54:54 -0700 | [diff] [blame] | 145 | LOG(DEBUG) << "Copying " << data->count << " blocks at offset " << data->offset; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 146 | |
Paul Crowley | 772cc85 | 2018-02-01 09:53:27 -0800 | [diff] [blame] | 147 | if (pread64(data->realfd, data->buffer, info.block_size * data->count, data->offset) <= 0) { |
| 148 | LOG(ERROR) << "Error reading real_blkdev " << data->real_blkdev << " for inplace encrypt"; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 149 | return -1; |
| 150 | } |
| 151 | |
Paul Crowley | 772cc85 | 2018-02-01 09:53:27 -0800 | [diff] [blame] | 152 | if (pwrite64(data->cryptofd, data->buffer, info.block_size * data->count, data->offset) <= 0) { |
| 153 | LOG(ERROR) << "Error writing crypto_blkdev " << data->crypto_blkdev |
| 154 | << " for inplace encrypt"; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 155 | return -1; |
| 156 | } else { |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 157 | log_progress(data, false); |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | data->count = 0; |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 161 | data->last_written_sector = |
| 162 | (data->offset + data->count) / info.block_size * CRYPT_SECTOR_SIZE - 1; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 163 | return 0; |
| 164 | } |
| 165 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 166 | static int encrypt_groups(struct encryptGroupsData* data) { |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 167 | unsigned int i; |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 168 | u8* block_bitmap = 0; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 169 | unsigned int block; |
| 170 | off64_t ret; |
| 171 | int rc = -1; |
| 172 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 173 | data->buffer = (char*)malloc(info.block_size * BLOCKS_AT_A_TIME); |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 174 | if (!data->buffer) { |
Paul Crowley | 772cc85 | 2018-02-01 09:53:27 -0800 | [diff] [blame] | 175 | LOG(ERROR) << "Failed to allocate crypto buffer"; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 176 | goto errout; |
| 177 | } |
| 178 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 179 | block_bitmap = (u8*)malloc(info.block_size); |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 180 | if (!block_bitmap) { |
Paul Crowley | 772cc85 | 2018-02-01 09:53:27 -0800 | [diff] [blame] | 181 | LOG(ERROR) << "failed to allocate block bitmap"; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 182 | goto errout; |
| 183 | } |
| 184 | |
| 185 | for (i = 0; i < aux_info.groups; ++i) { |
Paul Crowley | 772cc85 | 2018-02-01 09:53:27 -0800 | [diff] [blame] | 186 | LOG(INFO) << "Encrypting group " << i; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 187 | |
| 188 | u32 first_block = aux_info.first_data_block + i * info.blocks_per_group; |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 189 | u32 block_count = std::min(info.blocks_per_group, (u32)(aux_info.len_blocks - first_block)); |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 190 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 191 | off64_t offset = (u64)info.block_size * aux_info.bg_desc[i].bg_block_bitmap; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 192 | |
| 193 | ret = pread64(data->realfd, block_bitmap, info.block_size, offset); |
| 194 | if (ret != (int)info.block_size) { |
Paul Crowley | 772cc85 | 2018-02-01 09:53:27 -0800 | [diff] [blame] | 195 | LOG(ERROR) << "failed to read all of block group bitmap " << i; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 196 | goto errout; |
| 197 | } |
| 198 | |
| 199 | offset = (u64)info.block_size * first_block; |
| 200 | |
| 201 | data->count = 0; |
| 202 | |
| 203 | for (block = 0; block < block_count; block++) { |
Will Shiu | 4ac43f0 | 2020-07-29 17:03:17 +0800 | [diff] [blame] | 204 | int used; |
| 205 | |
| 206 | if (aux_info.bg_desc[i].bg_flags & EXT4_BG_BLOCK_UNINIT) { |
| 207 | // In block groups with an uninitialized block bitmap, we only |
| 208 | // need to encrypt the backup superblock (if one is present). |
| 209 | used = (ext4_bg_has_super_block(i) && block < 1 + aux_info.bg_desc_blocks); |
| 210 | } else { |
| 211 | used = bitmap_get_bit(block_bitmap, block); |
| 212 | } |
| 213 | |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 214 | update_progress(data, used); |
| 215 | if (used) { |
| 216 | if (data->count == 0) { |
| 217 | data->offset = offset; |
| 218 | } |
| 219 | data->count++; |
| 220 | } else { |
| 221 | if (flush_outstanding_data(data)) { |
| 222 | goto errout; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | offset += info.block_size; |
| 227 | |
| 228 | /* Write data if we are aligned or buffer size reached */ |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 229 | if (offset % (info.block_size * BLOCKS_AT_A_TIME) == 0 || |
| 230 | data->count == BLOCKS_AT_A_TIME) { |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 231 | if (flush_outstanding_data(data)) { |
| 232 | goto errout; |
| 233 | } |
| 234 | } |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 235 | } |
| 236 | if (flush_outstanding_data(data)) { |
| 237 | goto errout; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | data->completed = 1; |
| 242 | rc = 0; |
| 243 | |
| 244 | errout: |
| 245 | log_progress(0, true); |
| 246 | free(data->buffer); |
| 247 | free(block_bitmap); |
| 248 | return rc; |
| 249 | } |
| 250 | |
Paul Lawrence | 236e5e8 | 2019-06-25 14:44:33 -0700 | [diff] [blame] | 251 | static int cryptfs_enable_inplace_ext4(const char* crypto_blkdev, const char* real_blkdev, |
| 252 | off64_t size, off64_t* size_already_done, off64_t tot_size, |
Paul Crowley | 0fd2626 | 2018-01-30 09:48:19 -0800 | [diff] [blame] | 253 | off64_t previously_encrypted_upto, |
| 254 | bool set_progress_properties) { |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 255 | u32 i; |
| 256 | struct encryptGroupsData data; |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 257 | int rc; // Can't initialize without causing warning -Wclobbered |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 258 | int retries = RETRY_MOUNT_ATTEMPTS; |
| 259 | struct timespec time_started = {0}; |
| 260 | |
| 261 | if (previously_encrypted_upto > *size_already_done) { |
Paul Crowley | 772cc85 | 2018-02-01 09:53:27 -0800 | [diff] [blame] | 262 | LOG(DEBUG) << "Not fast encrypting since resuming part way through"; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 263 | return -1; |
| 264 | } |
| 265 | |
| 266 | memset(&data, 0, sizeof(data)); |
| 267 | data.real_blkdev = real_blkdev; |
| 268 | data.crypto_blkdev = crypto_blkdev; |
Paul Crowley | 0fd2626 | 2018-01-30 09:48:19 -0800 | [diff] [blame] | 269 | data.set_progress_properties = set_progress_properties; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 270 | |
Paul Crowley | 0fd2626 | 2018-01-30 09:48:19 -0800 | [diff] [blame] | 271 | LOG(DEBUG) << "Opening" << real_blkdev; |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 272 | if ((data.realfd = open(real_blkdev, O_RDWR | O_CLOEXEC)) < 0) { |
Paul Crowley | 772cc85 | 2018-02-01 09:53:27 -0800 | [diff] [blame] | 273 | PLOG(ERROR) << "Error opening real_blkdev " << real_blkdev << " for inplace encrypt"; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 274 | rc = -1; |
| 275 | goto errout; |
| 276 | } |
| 277 | |
Paul Crowley | 0fd2626 | 2018-01-30 09:48:19 -0800 | [diff] [blame] | 278 | LOG(DEBUG) << "Opening" << crypto_blkdev; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 279 | // Wait until the block device appears. Re-use the mount retry values since it is reasonable. |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 280 | while ((data.cryptofd = open(crypto_blkdev, O_WRONLY | O_CLOEXEC)) < 0) { |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 281 | if (--retries) { |
Paul Crowley | 772cc85 | 2018-02-01 09:53:27 -0800 | [diff] [blame] | 282 | PLOG(ERROR) << "Error opening crypto_blkdev " << crypto_blkdev |
| 283 | << " for ext4 inplace encrypt, retrying"; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 284 | sleep(RETRY_MOUNT_DELAY_SECONDS); |
| 285 | } else { |
Paul Crowley | 772cc85 | 2018-02-01 09:53:27 -0800 | [diff] [blame] | 286 | PLOG(ERROR) << "Error opening crypto_blkdev " << crypto_blkdev |
| 287 | << " for ext4 inplace encrypt"; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 288 | rc = ENABLE_INPLACE_ERR_DEV; |
| 289 | goto errout; |
| 290 | } |
| 291 | } |
| 292 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 293 | if (setjmp(setjmp_env)) { // NOLINT |
Paul Crowley | 772cc85 | 2018-02-01 09:53:27 -0800 | [diff] [blame] | 294 | LOG(ERROR) << "Reading ext4 extent caused an exception"; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 295 | rc = -1; |
| 296 | goto errout; |
| 297 | } |
| 298 | |
| 299 | if (read_ext(data.realfd, 0) != 0) { |
Paul Crowley | 772cc85 | 2018-02-01 09:53:27 -0800 | [diff] [blame] | 300 | LOG(ERROR) << "Failed to read ext4 extent"; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 301 | rc = -1; |
| 302 | goto errout; |
| 303 | } |
| 304 | |
| 305 | data.numblocks = size / CRYPT_SECTORS_PER_BUFSIZE; |
| 306 | data.tot_numblocks = tot_size / CRYPT_SECTORS_PER_BUFSIZE; |
| 307 | data.blocks_already_done = *size_already_done / CRYPT_SECTORS_PER_BUFSIZE; |
| 308 | |
Paul Crowley | 772cc85 | 2018-02-01 09:53:27 -0800 | [diff] [blame] | 309 | LOG(INFO) << "Encrypting ext4 filesystem in place..."; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 310 | |
| 311 | data.tot_used_blocks = data.numblocks; |
| 312 | for (i = 0; i < aux_info.groups; ++i) { |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 313 | data.tot_used_blocks -= aux_info.bg_desc[i].bg_free_blocks_count; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | data.one_pct = data.tot_used_blocks / 100; |
| 317 | data.cur_pct = 0; |
| 318 | |
| 319 | if (clock_gettime(CLOCK_MONOTONIC, &time_started)) { |
Paul Crowley | 772cc85 | 2018-02-01 09:53:27 -0800 | [diff] [blame] | 320 | LOG(WARNING) << "Error getting time at start"; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 321 | // Note - continue anyway - we'll run with 0 |
| 322 | } |
| 323 | data.time_started = time_started.tv_sec; |
| 324 | data.remaining_time = -1; |
| 325 | |
| 326 | rc = encrypt_groups(&data); |
| 327 | if (rc) { |
Paul Crowley | 772cc85 | 2018-02-01 09:53:27 -0800 | [diff] [blame] | 328 | LOG(ERROR) << "Error encrypting groups"; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 329 | goto errout; |
| 330 | } |
| 331 | |
| 332 | *size_already_done += data.completed ? size : data.last_written_sector; |
| 333 | rc = 0; |
| 334 | |
| 335 | errout: |
| 336 | close(data.realfd); |
| 337 | close(data.cryptofd); |
| 338 | |
| 339 | return rc; |
| 340 | } |
| 341 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 342 | static void log_progress_f2fs(u64 block, bool completed) { |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 343 | // Precondition - if completed data = 0 else data != 0 |
| 344 | |
| 345 | // Track progress so we can skip logging blocks |
| 346 | static u64 last_block = (u64)-1; |
| 347 | |
| 348 | // Need to close existing 'Encrypting from' log? |
| 349 | if (completed || (last_block != (u64)-1 && block != last_block + 1)) { |
Paul Crowley | 772cc85 | 2018-02-01 09:53:27 -0800 | [diff] [blame] | 350 | LOG(INFO) << "Encrypted to block " << last_block; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 351 | last_block = -1; |
| 352 | } |
| 353 | |
| 354 | // Need to start new 'Encrypting from' log? |
| 355 | if (!completed && (last_block == (u64)-1 || block != last_block + 1)) { |
Paul Crowley | 772cc85 | 2018-02-01 09:53:27 -0800 | [diff] [blame] | 356 | LOG(INFO) << "Encrypting from block " << block; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | // Update offset |
| 360 | if (!completed) { |
| 361 | last_block = block; |
| 362 | } |
| 363 | } |
| 364 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 365 | static int encrypt_one_block_f2fs(u64 pos, void* data) { |
| 366 | struct encryptGroupsData* priv_dat = (struct encryptGroupsData*)data; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 367 | |
| 368 | priv_dat->blocks_already_done = pos - 1; |
| 369 | update_progress(priv_dat, 1); |
| 370 | |
| 371 | off64_t offset = pos * CRYPT_INPLACE_BUFSIZE; |
| 372 | |
| 373 | if (pread64(priv_dat->realfd, priv_dat->buffer, CRYPT_INPLACE_BUFSIZE, offset) <= 0) { |
Paul Crowley | 772cc85 | 2018-02-01 09:53:27 -0800 | [diff] [blame] | 374 | LOG(ERROR) << "Error reading real_blkdev " << priv_dat->crypto_blkdev |
| 375 | << " for f2fs inplace encrypt"; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 376 | return -1; |
| 377 | } |
| 378 | |
| 379 | if (pwrite64(priv_dat->cryptofd, priv_dat->buffer, CRYPT_INPLACE_BUFSIZE, offset) <= 0) { |
Paul Crowley | 772cc85 | 2018-02-01 09:53:27 -0800 | [diff] [blame] | 380 | LOG(ERROR) << "Error writing crypto_blkdev " << priv_dat->crypto_blkdev |
| 381 | << " for f2fs inplace encrypt"; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 382 | return -1; |
| 383 | } else { |
| 384 | log_progress_f2fs(pos, false); |
| 385 | } |
| 386 | |
| 387 | return 0; |
| 388 | } |
| 389 | |
Paul Lawrence | 236e5e8 | 2019-06-25 14:44:33 -0700 | [diff] [blame] | 390 | static int cryptfs_enable_inplace_f2fs(const char* crypto_blkdev, const char* real_blkdev, |
| 391 | off64_t size, off64_t* size_already_done, off64_t tot_size, |
Paul Crowley | 0fd2626 | 2018-01-30 09:48:19 -0800 | [diff] [blame] | 392 | off64_t previously_encrypted_upto, |
| 393 | bool set_progress_properties) { |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 394 | struct encryptGroupsData data; |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 395 | struct f2fs_info* f2fs_info = NULL; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 396 | int rc = ENABLE_INPLACE_ERR_OTHER; |
Denis Hsu | 1740eff | 2019-12-26 16:06:37 +0800 | [diff] [blame] | 397 | struct timespec time_started = {0}; |
| 398 | |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 399 | if (previously_encrypted_upto > *size_already_done) { |
Paul Crowley | 772cc85 | 2018-02-01 09:53:27 -0800 | [diff] [blame] | 400 | LOG(DEBUG) << "Not fast encrypting since resuming part way through"; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 401 | return ENABLE_INPLACE_ERR_OTHER; |
| 402 | } |
| 403 | memset(&data, 0, sizeof(data)); |
| 404 | data.real_blkdev = real_blkdev; |
| 405 | data.crypto_blkdev = crypto_blkdev; |
Paul Crowley | 0fd2626 | 2018-01-30 09:48:19 -0800 | [diff] [blame] | 406 | data.set_progress_properties = set_progress_properties; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 407 | data.realfd = -1; |
| 408 | data.cryptofd = -1; |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 409 | if ((data.realfd = open64(real_blkdev, O_RDWR | O_CLOEXEC)) < 0) { |
Paul Crowley | 772cc85 | 2018-02-01 09:53:27 -0800 | [diff] [blame] | 410 | PLOG(ERROR) << "Error opening real_blkdev " << real_blkdev << " for f2fs inplace encrypt"; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 411 | goto errout; |
| 412 | } |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 413 | if ((data.cryptofd = open64(crypto_blkdev, O_WRONLY | O_CLOEXEC)) < 0) { |
Paul Crowley | 772cc85 | 2018-02-01 09:53:27 -0800 | [diff] [blame] | 414 | PLOG(ERROR) << "Error opening crypto_blkdev " << crypto_blkdev |
| 415 | << " for f2fs inplace encrypt"; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 416 | rc = ENABLE_INPLACE_ERR_DEV; |
| 417 | goto errout; |
| 418 | } |
| 419 | |
| 420 | f2fs_info = generate_f2fs_info(data.realfd); |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 421 | if (!f2fs_info) goto errout; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 422 | |
| 423 | data.numblocks = size / CRYPT_SECTORS_PER_BUFSIZE; |
| 424 | data.tot_numblocks = tot_size / CRYPT_SECTORS_PER_BUFSIZE; |
| 425 | data.blocks_already_done = *size_already_done / CRYPT_SECTORS_PER_BUFSIZE; |
| 426 | |
| 427 | data.tot_used_blocks = get_num_blocks_used(f2fs_info); |
| 428 | |
| 429 | data.one_pct = data.tot_used_blocks / 100; |
| 430 | data.cur_pct = 0; |
Denis Hsu | 1740eff | 2019-12-26 16:06:37 +0800 | [diff] [blame] | 431 | if (clock_gettime(CLOCK_MONOTONIC, &time_started)) { |
| 432 | LOG(WARNING) << "Error getting time at start"; |
| 433 | // Note - continue anyway - we'll run with 0 |
| 434 | } |
| 435 | data.time_started = time_started.tv_sec; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 436 | data.remaining_time = -1; |
| 437 | |
Denis Hsu | 1740eff | 2019-12-26 16:06:37 +0800 | [diff] [blame] | 438 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 439 | data.buffer = (char*)malloc(f2fs_info->block_size); |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 440 | if (!data.buffer) { |
Paul Crowley | 772cc85 | 2018-02-01 09:53:27 -0800 | [diff] [blame] | 441 | LOG(ERROR) << "Failed to allocate crypto buffer"; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 442 | goto errout; |
| 443 | } |
| 444 | |
| 445 | data.count = 0; |
| 446 | |
| 447 | /* Currently, this either runs to completion, or hits a nonrecoverable error */ |
| 448 | rc = run_on_used_blocks(data.blocks_already_done, f2fs_info, &encrypt_one_block_f2fs, &data); |
| 449 | |
| 450 | if (rc) { |
Paul Crowley | 772cc85 | 2018-02-01 09:53:27 -0800 | [diff] [blame] | 451 | LOG(ERROR) << "Error in running over f2fs blocks"; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 452 | rc = ENABLE_INPLACE_ERR_OTHER; |
| 453 | goto errout; |
| 454 | } |
| 455 | |
| 456 | *size_already_done += size; |
| 457 | rc = 0; |
| 458 | |
| 459 | errout: |
Paul Crowley | 772cc85 | 2018-02-01 09:53:27 -0800 | [diff] [blame] | 460 | if (rc) LOG(ERROR) << "Failed to encrypt f2fs filesystem on " << real_blkdev; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 461 | |
| 462 | log_progress_f2fs(0, true); |
| 463 | free(f2fs_info); |
| 464 | free(data.buffer); |
| 465 | close(data.realfd); |
| 466 | close(data.cryptofd); |
| 467 | |
| 468 | return rc; |
| 469 | } |
| 470 | |
Paul Lawrence | 236e5e8 | 2019-06-25 14:44:33 -0700 | [diff] [blame] | 471 | static int cryptfs_enable_inplace_full(const char* crypto_blkdev, const char* real_blkdev, |
| 472 | off64_t size, off64_t* size_already_done, off64_t tot_size, |
Paul Crowley | 0fd2626 | 2018-01-30 09:48:19 -0800 | [diff] [blame] | 473 | off64_t previously_encrypted_upto, |
| 474 | bool set_progress_properties) { |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 475 | int realfd, cryptofd; |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 476 | char* buf[CRYPT_INPLACE_BUFSIZE]; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 477 | int rc = ENABLE_INPLACE_ERR_OTHER; |
| 478 | off64_t numblocks, i, remainder; |
| 479 | off64_t one_pct, cur_pct, new_pct; |
| 480 | off64_t blocks_already_done, tot_numblocks; |
| 481 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 482 | if ((realfd = open(real_blkdev, O_RDONLY | O_CLOEXEC)) < 0) { |
Paul Crowley | 772cc85 | 2018-02-01 09:53:27 -0800 | [diff] [blame] | 483 | PLOG(ERROR) << "Error opening real_blkdev " << real_blkdev << " for inplace encrypt"; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 484 | return ENABLE_INPLACE_ERR_OTHER; |
| 485 | } |
| 486 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 487 | if ((cryptofd = open(crypto_blkdev, O_WRONLY | O_CLOEXEC)) < 0) { |
Paul Crowley | 772cc85 | 2018-02-01 09:53:27 -0800 | [diff] [blame] | 488 | PLOG(ERROR) << "Error opening crypto_blkdev " << crypto_blkdev << " for inplace encrypt"; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 489 | close(realfd); |
| 490 | return ENABLE_INPLACE_ERR_DEV; |
| 491 | } |
| 492 | |
| 493 | /* This is pretty much a simple loop of reading 4K, and writing 4K. |
| 494 | * The size passed in is the number of 512 byte sectors in the filesystem. |
| 495 | * So compute the number of whole 4K blocks we should read/write, |
| 496 | * and the remainder. |
| 497 | */ |
| 498 | numblocks = size / CRYPT_SECTORS_PER_BUFSIZE; |
| 499 | remainder = size % CRYPT_SECTORS_PER_BUFSIZE; |
| 500 | tot_numblocks = tot_size / CRYPT_SECTORS_PER_BUFSIZE; |
| 501 | blocks_already_done = *size_already_done / CRYPT_SECTORS_PER_BUFSIZE; |
| 502 | |
Paul Crowley | 772cc85 | 2018-02-01 09:53:27 -0800 | [diff] [blame] | 503 | LOG(ERROR) << "Encrypting filesystem in place..."; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 504 | |
| 505 | i = previously_encrypted_upto + 1 - *size_already_done; |
| 506 | |
| 507 | if (lseek64(realfd, i * CRYPT_SECTOR_SIZE, SEEK_SET) < 0) { |
Paul Crowley | 772cc85 | 2018-02-01 09:53:27 -0800 | [diff] [blame] | 508 | PLOG(ERROR) << "Cannot seek to previously encrypted point on " << real_blkdev; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 509 | goto errout; |
| 510 | } |
| 511 | |
| 512 | if (lseek64(cryptofd, i * CRYPT_SECTOR_SIZE, SEEK_SET) < 0) { |
Paul Crowley | 772cc85 | 2018-02-01 09:53:27 -0800 | [diff] [blame] | 513 | PLOG(ERROR) << "Cannot seek to previously encrypted point on " << crypto_blkdev; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 514 | goto errout; |
| 515 | } |
| 516 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 517 | for (; i < size && i % CRYPT_SECTORS_PER_BUFSIZE != 0; ++i) { |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 518 | if (unix_read(realfd, buf, CRYPT_SECTOR_SIZE) <= 0) { |
Paul Crowley | 772cc85 | 2018-02-01 09:53:27 -0800 | [diff] [blame] | 519 | PLOG(ERROR) << "Error reading initial sectors from real_blkdev " << real_blkdev |
| 520 | << " for inplace encrypt"; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 521 | goto errout; |
| 522 | } |
| 523 | if (unix_write(cryptofd, buf, CRYPT_SECTOR_SIZE) <= 0) { |
Paul Crowley | 772cc85 | 2018-02-01 09:53:27 -0800 | [diff] [blame] | 524 | PLOG(ERROR) << "Error writing initial sectors to crypto_blkdev " << crypto_blkdev |
| 525 | << " for inplace encrypt"; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 526 | goto errout; |
| 527 | } else { |
Paul Crowley | 772cc85 | 2018-02-01 09:53:27 -0800 | [diff] [blame] | 528 | LOG(INFO) << "Encrypted 1 block at " << i; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 529 | } |
| 530 | } |
| 531 | |
| 532 | one_pct = tot_numblocks / 100; |
| 533 | cur_pct = 0; |
| 534 | /* process the majority of the filesystem in blocks */ |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 535 | for (i /= CRYPT_SECTORS_PER_BUFSIZE; i < numblocks; i++) { |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 536 | new_pct = (i + blocks_already_done) / one_pct; |
Paul Crowley | 0fd2626 | 2018-01-30 09:48:19 -0800 | [diff] [blame] | 537 | if (set_progress_properties && new_pct > cur_pct) { |
Greg Kaiser | e0691cc | 2018-12-18 12:34:07 -0800 | [diff] [blame] | 538 | char property_buf[8]; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 539 | |
| 540 | cur_pct = new_pct; |
Greg Kaiser | e0691cc | 2018-12-18 12:34:07 -0800 | [diff] [blame] | 541 | snprintf(property_buf, sizeof(property_buf), "%" PRId64, cur_pct); |
| 542 | android::base::SetProperty("vold.encrypt_progress", property_buf); |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 543 | } |
| 544 | if (unix_read(realfd, buf, CRYPT_INPLACE_BUFSIZE) <= 0) { |
Paul Crowley | 772cc85 | 2018-02-01 09:53:27 -0800 | [diff] [blame] | 545 | PLOG(ERROR) << "Error reading real_blkdev " << real_blkdev << " for inplace encrypt"; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 546 | goto errout; |
| 547 | } |
| 548 | if (unix_write(cryptofd, buf, CRYPT_INPLACE_BUFSIZE) <= 0) { |
Paul Crowley | 772cc85 | 2018-02-01 09:53:27 -0800 | [diff] [blame] | 549 | PLOG(ERROR) << "Error writing crypto_blkdev " << crypto_blkdev << " for inplace encrypt"; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 550 | goto errout; |
| 551 | } else { |
Paul Crowley | 772cc85 | 2018-02-01 09:53:27 -0800 | [diff] [blame] | 552 | LOG(DEBUG) << "Encrypted " << CRYPT_SECTORS_PER_BUFSIZE << " block at " |
| 553 | << i * CRYPT_SECTORS_PER_BUFSIZE; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 554 | } |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | /* Do any remaining sectors */ |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 558 | for (i = 0; i < remainder; i++) { |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 559 | if (unix_read(realfd, buf, CRYPT_SECTOR_SIZE) <= 0) { |
Paul Crowley | 772cc85 | 2018-02-01 09:53:27 -0800 | [diff] [blame] | 560 | LOG(ERROR) << "Error reading final sectors from real_blkdev " << real_blkdev |
| 561 | << " for inplace encrypt"; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 562 | goto errout; |
| 563 | } |
| 564 | if (unix_write(cryptofd, buf, CRYPT_SECTOR_SIZE) <= 0) { |
Paul Crowley | 772cc85 | 2018-02-01 09:53:27 -0800 | [diff] [blame] | 565 | LOG(ERROR) << "Error writing final sectors to crypto_blkdev " << crypto_blkdev |
| 566 | << " for inplace encrypt"; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 567 | goto errout; |
| 568 | } else { |
Paul Crowley | 772cc85 | 2018-02-01 09:53:27 -0800 | [diff] [blame] | 569 | LOG(INFO) << "Encrypted 1 block at next location"; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 570 | } |
| 571 | } |
| 572 | |
| 573 | *size_already_done += size; |
| 574 | rc = 0; |
| 575 | |
| 576 | errout: |
| 577 | close(realfd); |
| 578 | close(cryptofd); |
| 579 | |
| 580 | return rc; |
| 581 | } |
| 582 | |
| 583 | /* returns on of the ENABLE_INPLACE_* return codes */ |
Paul Lawrence | 236e5e8 | 2019-06-25 14:44:33 -0700 | [diff] [blame] | 584 | int cryptfs_enable_inplace(const char* crypto_blkdev, const char* real_blkdev, off64_t size, |
Paul Crowley | 772cc85 | 2018-02-01 09:53:27 -0800 | [diff] [blame] | 585 | off64_t* size_already_done, off64_t tot_size, |
Paul Crowley | 0fd2626 | 2018-01-30 09:48:19 -0800 | [diff] [blame] | 586 | off64_t previously_encrypted_upto, bool set_progress_properties) { |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 587 | int rc_ext4, rc_f2fs, rc_full; |
Paul Crowley | 0fd2626 | 2018-01-30 09:48:19 -0800 | [diff] [blame] | 588 | LOG(DEBUG) << "cryptfs_enable_inplace(" << crypto_blkdev << ", " << real_blkdev << ", " << size |
| 589 | << ", " << size_already_done << ", " << tot_size << ", " << previously_encrypted_upto |
| 590 | << ", " << set_progress_properties << ")"; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 591 | if (previously_encrypted_upto) { |
Paul Crowley | 772cc85 | 2018-02-01 09:53:27 -0800 | [diff] [blame] | 592 | LOG(DEBUG) << "Continuing encryption from " << previously_encrypted_upto; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | if (*size_already_done + size < previously_encrypted_upto) { |
Paul Crowley | 0fd2626 | 2018-01-30 09:48:19 -0800 | [diff] [blame] | 596 | LOG(DEBUG) << "cryptfs_enable_inplace already done"; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 597 | *size_already_done += size; |
| 598 | return 0; |
| 599 | } |
| 600 | |
| 601 | /* TODO: identify filesystem type. |
| 602 | * As is, cryptfs_enable_inplace_ext4 will fail on an f2fs partition, and |
| 603 | * then we will drop down to cryptfs_enable_inplace_f2fs. |
| 604 | * */ |
Paul Crowley | 0fd2626 | 2018-01-30 09:48:19 -0800 | [diff] [blame] | 605 | if ((rc_ext4 = cryptfs_enable_inplace_ext4(crypto_blkdev, real_blkdev, size, size_already_done, |
| 606 | tot_size, previously_encrypted_upto, |
| 607 | set_progress_properties)) == 0) { |
| 608 | LOG(DEBUG) << "cryptfs_enable_inplace_ext4 success"; |
| 609 | return 0; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 610 | } |
Paul Crowley | 772cc85 | 2018-02-01 09:53:27 -0800 | [diff] [blame] | 611 | LOG(DEBUG) << "cryptfs_enable_inplace_ext4()=" << rc_ext4; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 612 | |
Paul Crowley | 0fd2626 | 2018-01-30 09:48:19 -0800 | [diff] [blame] | 613 | if ((rc_f2fs = cryptfs_enable_inplace_f2fs(crypto_blkdev, real_blkdev, size, size_already_done, |
| 614 | tot_size, previously_encrypted_upto, |
| 615 | set_progress_properties)) == 0) { |
| 616 | LOG(DEBUG) << "cryptfs_enable_inplace_f2fs success"; |
| 617 | return 0; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 618 | } |
Paul Crowley | 772cc85 | 2018-02-01 09:53:27 -0800 | [diff] [blame] | 619 | LOG(DEBUG) << "cryptfs_enable_inplace_f2fs()=" << rc_f2fs; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 620 | |
Paul Crowley | 0fd2626 | 2018-01-30 09:48:19 -0800 | [diff] [blame] | 621 | rc_full = |
| 622 | cryptfs_enable_inplace_full(crypto_blkdev, real_blkdev, size, size_already_done, tot_size, |
| 623 | previously_encrypted_upto, set_progress_properties); |
Paul Crowley | 772cc85 | 2018-02-01 09:53:27 -0800 | [diff] [blame] | 624 | LOG(DEBUG) << "cryptfs_enable_inplace_full()=" << rc_full; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 625 | |
| 626 | /* Hack for b/17898962, the following is the symptom... */ |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 627 | if (rc_ext4 == ENABLE_INPLACE_ERR_DEV && rc_f2fs == ENABLE_INPLACE_ERR_DEV && |
| 628 | rc_full == ENABLE_INPLACE_ERR_DEV) { |
Paul Crowley | 0fd2626 | 2018-01-30 09:48:19 -0800 | [diff] [blame] | 629 | LOG(DEBUG) << "ENABLE_INPLACE_ERR_DEV"; |
| 630 | return ENABLE_INPLACE_ERR_DEV; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 631 | } |
| 632 | return rc_full; |
| 633 | } |