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