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