blob: 438f9596cbf953834575bd04ae4af7717ea4a975 [file] [log] [blame]
Paul Crowleyf71ace32016-06-02 11:01:19 -07001/*
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 Crowleyf71ace32016-06-02 11:01:19 -070019#include <ext4_utils/ext4.h>
20#include <ext4_utils/ext4_utils.h>
21#include <f2fs_sparseblock.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070022#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 Crowleyf71ace32016-06-02 11:01:19 -070029
30#include <algorithm>
31
Paul Crowley772cc852018-02-01 09:53:27 -080032#include <android-base/logging.h>
33#include <android-base/properties.h>
Paul Crowleyf71ace32016-06-02 11:01:19 -070034
Paul Crowleyf71ace32016-06-02 11:01:19 -070035// FIXME horrible cut-and-paste code
Paul Crowley14c8c072018-09-18 13:30:21 -070036static inline int unix_read(int fd, void* buff, int len) {
Paul Crowleyf71ace32016-06-02 11:01:19 -070037 return TEMP_FAILURE_RETRY(read(fd, buff, len));
38}
39
Paul Crowley14c8c072018-09-18 13:30:21 -070040static inline int unix_write(int fd, const void* buff, int len) {
Paul Crowleyf71ace32016-06-02 11:01:19 -070041 return TEMP_FAILURE_RETRY(write(fd, buff, len));
42}
43
44#define CRYPT_SECTORS_PER_BUFSIZE (CRYPT_INPLACE_BUFSIZE / CRYPT_SECTOR_SIZE)
45
46/* aligned 32K writes tends to make flash happy.
47 * SD card association recommends it.
48 */
Paul Crowleyf71ace32016-06-02 11:01:19 -070049#define BLOCKS_AT_A_TIME 8
Paul Crowleyf71ace32016-06-02 11:01:19 -070050
Paul Crowley14c8c072018-09-18 13:30:21 -070051struct encryptGroupsData {
Paul Crowleyf71ace32016-06-02 11:01:19 -070052 int realfd;
53 int cryptofd;
Paul Crowleyf71ace32016-06-02 11:01:19 -070054 off64_t one_pct, cur_pct, new_pct;
Eric Biggersc01995e2020-11-03 14:11:00 -080055 off64_t blocks_already_done;
Paul Crowleyf71ace32016-06-02 11:01:19 -070056 off64_t used_blocks_already_done, tot_used_blocks;
Paul Lawrence236e5e82019-06-25 14:44:33 -070057 const char* real_blkdev;
58 const char* crypto_blkdev;
Paul Crowleyf71ace32016-06-02 11:01:19 -070059 int count;
60 off64_t offset;
61 char* buffer;
Paul Crowleyf71ace32016-06-02 11:01:19 -070062 time_t time_started;
63 int remaining_time;
Paul Crowley0fd26262018-01-30 09:48:19 -080064 bool set_progress_properties;
Paul Crowleyf71ace32016-06-02 11:01:19 -070065};
66
Paul Crowley14c8c072018-09-18 13:30:21 -070067static void update_progress(struct encryptGroupsData* data, int is_used) {
Paul Crowleyf71ace32016-06-02 11:01:19 -070068 data->blocks_already_done++;
69
70 if (is_used) {
71 data->used_blocks_already_done++;
72 }
73 if (data->tot_used_blocks) {
74 data->new_pct = data->used_blocks_already_done / data->one_pct;
75 } else {
76 data->new_pct = data->blocks_already_done / data->one_pct;
77 }
78
Paul Crowley0fd26262018-01-30 09:48:19 -080079 if (!data->set_progress_properties) return;
80
Paul Crowleyf71ace32016-06-02 11:01:19 -070081 if (data->new_pct > data->cur_pct) {
82 char buf[8];
83 data->cur_pct = data->new_pct;
84 snprintf(buf, sizeof(buf), "%" PRId64, data->cur_pct);
Paul Crowley772cc852018-02-01 09:53:27 -080085 android::base::SetProperty("vold.encrypt_progress", buf);
Paul Crowleyf71ace32016-06-02 11:01:19 -070086 }
87
88 if (data->cur_pct >= 5) {
89 struct timespec time_now;
90 if (clock_gettime(CLOCK_MONOTONIC, &time_now)) {
Paul Crowley772cc852018-02-01 09:53:27 -080091 LOG(WARNING) << "Error getting time";
Paul Crowleyf71ace32016-06-02 11:01:19 -070092 } else {
93 double elapsed_time = difftime(time_now.tv_sec, data->time_started);
Paul Crowley14c8c072018-09-18 13:30:21 -070094 off64_t remaining_blocks = data->tot_used_blocks - data->used_blocks_already_done;
95 int remaining_time =
96 (int)(elapsed_time * remaining_blocks / data->used_blocks_already_done);
Paul Crowleyf71ace32016-06-02 11:01:19 -070097
98 // Change time only if not yet set, lower, or a lot higher for
99 // best user experience
Paul Crowley14c8c072018-09-18 13:30:21 -0700100 if (data->remaining_time == -1 || remaining_time < data->remaining_time ||
101 remaining_time > data->remaining_time + 60) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700102 char buf[8];
103 snprintf(buf, sizeof(buf), "%d", remaining_time);
Paul Crowley772cc852018-02-01 09:53:27 -0800104 android::base::SetProperty("vold.encrypt_time_remaining", buf);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700105 data->remaining_time = remaining_time;
106 }
107 }
108 }
109}
110
Paul Crowley14c8c072018-09-18 13:30:21 -0700111static void log_progress(struct encryptGroupsData const* data, bool completed) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700112 // Precondition - if completed data = 0 else data != 0
113
114 // Track progress so we can skip logging blocks
115 static off64_t offset = -1;
116
117 // Need to close existing 'Encrypting from' log?
118 if (completed || (offset != -1 && data->offset != offset)) {
Paul Crowley772cc852018-02-01 09:53:27 -0800119 LOG(INFO) << "Encrypted to sector " << offset / info.block_size * CRYPT_SECTOR_SIZE;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700120 offset = -1;
121 }
122
123 // Need to start new 'Encrypting from' log?
124 if (!completed && offset != data->offset) {
Paul Crowley772cc852018-02-01 09:53:27 -0800125 LOG(INFO) << "Encrypting from sector " << data->offset / info.block_size * CRYPT_SECTOR_SIZE;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700126 }
127
128 // Update offset
129 if (!completed) {
130 offset = data->offset + (off64_t)data->count * info.block_size;
131 }
132}
133
Paul Crowley14c8c072018-09-18 13:30:21 -0700134static int flush_outstanding_data(struct encryptGroupsData* data) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700135 if (data->count == 0) {
136 return 0;
137 }
138
Sudheer Shanka4b6ca4e2018-09-21 10:54:54 -0700139 LOG(DEBUG) << "Copying " << data->count << " blocks at offset " << data->offset;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700140
Paul Crowley772cc852018-02-01 09:53:27 -0800141 if (pread64(data->realfd, data->buffer, info.block_size * data->count, data->offset) <= 0) {
142 LOG(ERROR) << "Error reading real_blkdev " << data->real_blkdev << " for inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700143 return -1;
144 }
145
Paul Crowley772cc852018-02-01 09:53:27 -0800146 if (pwrite64(data->cryptofd, data->buffer, info.block_size * data->count, data->offset) <= 0) {
147 LOG(ERROR) << "Error writing crypto_blkdev " << data->crypto_blkdev
148 << " for inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700149 return -1;
150 } else {
Paul Crowley14c8c072018-09-18 13:30:21 -0700151 log_progress(data, false);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700152 }
153
154 data->count = 0;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700155 return 0;
156}
157
Paul Crowley14c8c072018-09-18 13:30:21 -0700158static int encrypt_groups(struct encryptGroupsData* data) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700159 unsigned int i;
Paul Crowley14c8c072018-09-18 13:30:21 -0700160 u8* block_bitmap = 0;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700161 unsigned int block;
162 off64_t ret;
163 int rc = -1;
164
Paul Crowley14c8c072018-09-18 13:30:21 -0700165 data->buffer = (char*)malloc(info.block_size * BLOCKS_AT_A_TIME);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700166 if (!data->buffer) {
Paul Crowley772cc852018-02-01 09:53:27 -0800167 LOG(ERROR) << "Failed to allocate crypto buffer";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700168 goto errout;
169 }
170
Paul Crowley14c8c072018-09-18 13:30:21 -0700171 block_bitmap = (u8*)malloc(info.block_size);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700172 if (!block_bitmap) {
Paul Crowley772cc852018-02-01 09:53:27 -0800173 LOG(ERROR) << "failed to allocate block bitmap";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700174 goto errout;
175 }
176
177 for (i = 0; i < aux_info.groups; ++i) {
Paul Crowley772cc852018-02-01 09:53:27 -0800178 LOG(INFO) << "Encrypting group " << i;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700179
180 u32 first_block = aux_info.first_data_block + i * info.blocks_per_group;
Paul Crowley14c8c072018-09-18 13:30:21 -0700181 u32 block_count = std::min(info.blocks_per_group, (u32)(aux_info.len_blocks - first_block));
Paul Crowleyf71ace32016-06-02 11:01:19 -0700182
Paul Crowley14c8c072018-09-18 13:30:21 -0700183 off64_t offset = (u64)info.block_size * aux_info.bg_desc[i].bg_block_bitmap;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700184
185 ret = pread64(data->realfd, block_bitmap, info.block_size, offset);
186 if (ret != (int)info.block_size) {
Paul Crowley772cc852018-02-01 09:53:27 -0800187 LOG(ERROR) << "failed to read all of block group bitmap " << i;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700188 goto errout;
189 }
190
191 offset = (u64)info.block_size * first_block;
192
193 data->count = 0;
194
195 for (block = 0; block < block_count; block++) {
Will Shiu4ac43f02020-07-29 17:03:17 +0800196 int used;
197
198 if (aux_info.bg_desc[i].bg_flags & EXT4_BG_BLOCK_UNINIT) {
199 // In block groups with an uninitialized block bitmap, we only
200 // need to encrypt the backup superblock (if one is present).
201 used = (ext4_bg_has_super_block(i) && block < 1 + aux_info.bg_desc_blocks);
202 } else {
203 used = bitmap_get_bit(block_bitmap, block);
204 }
205
Paul Crowleyf71ace32016-06-02 11:01:19 -0700206 update_progress(data, used);
207 if (used) {
208 if (data->count == 0) {
209 data->offset = offset;
210 }
211 data->count++;
212 } else {
213 if (flush_outstanding_data(data)) {
214 goto errout;
215 }
216 }
217
218 offset += info.block_size;
219
220 /* Write data if we are aligned or buffer size reached */
Paul Crowley14c8c072018-09-18 13:30:21 -0700221 if (offset % (info.block_size * BLOCKS_AT_A_TIME) == 0 ||
222 data->count == BLOCKS_AT_A_TIME) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700223 if (flush_outstanding_data(data)) {
224 goto errout;
225 }
226 }
Paul Crowleyf71ace32016-06-02 11:01:19 -0700227 }
228 if (flush_outstanding_data(data)) {
229 goto errout;
230 }
231 }
232
Paul Crowleyf71ace32016-06-02 11:01:19 -0700233 rc = 0;
234
235errout:
236 log_progress(0, true);
237 free(data->buffer);
238 free(block_bitmap);
239 return rc;
240}
241
Paul Lawrence236e5e82019-06-25 14:44:33 -0700242static int cryptfs_enable_inplace_ext4(const char* crypto_blkdev, const char* real_blkdev,
Eric Biggersc01995e2020-11-03 14:11:00 -0800243 off64_t size, bool set_progress_properties) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700244 u32 i;
245 struct encryptGroupsData data;
Paul Crowley14c8c072018-09-18 13:30:21 -0700246 int rc; // Can't initialize without causing warning -Wclobbered
Paul Crowleyf71ace32016-06-02 11:01:19 -0700247 int retries = RETRY_MOUNT_ATTEMPTS;
248 struct timespec time_started = {0};
249
Paul Crowleyf71ace32016-06-02 11:01:19 -0700250 memset(&data, 0, sizeof(data));
251 data.real_blkdev = real_blkdev;
252 data.crypto_blkdev = crypto_blkdev;
Paul Crowley0fd26262018-01-30 09:48:19 -0800253 data.set_progress_properties = set_progress_properties;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700254
Paul Crowley0fd26262018-01-30 09:48:19 -0800255 LOG(DEBUG) << "Opening" << real_blkdev;
Paul Crowley14c8c072018-09-18 13:30:21 -0700256 if ((data.realfd = open(real_blkdev, O_RDWR | O_CLOEXEC)) < 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800257 PLOG(ERROR) << "Error opening real_blkdev " << real_blkdev << " for inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700258 rc = -1;
259 goto errout;
260 }
261
Paul Crowley0fd26262018-01-30 09:48:19 -0800262 LOG(DEBUG) << "Opening" << crypto_blkdev;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700263 // Wait until the block device appears. Re-use the mount retry values since it is reasonable.
Paul Crowley14c8c072018-09-18 13:30:21 -0700264 while ((data.cryptofd = open(crypto_blkdev, O_WRONLY | O_CLOEXEC)) < 0) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700265 if (--retries) {
Paul Crowley772cc852018-02-01 09:53:27 -0800266 PLOG(ERROR) << "Error opening crypto_blkdev " << crypto_blkdev
267 << " for ext4 inplace encrypt, retrying";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700268 sleep(RETRY_MOUNT_DELAY_SECONDS);
269 } else {
Paul Crowley772cc852018-02-01 09:53:27 -0800270 PLOG(ERROR) << "Error opening crypto_blkdev " << crypto_blkdev
271 << " for ext4 inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700272 rc = ENABLE_INPLACE_ERR_DEV;
273 goto errout;
274 }
275 }
276
Paul Crowley14c8c072018-09-18 13:30:21 -0700277 if (setjmp(setjmp_env)) { // NOLINT
Paul Crowley772cc852018-02-01 09:53:27 -0800278 LOG(ERROR) << "Reading ext4 extent caused an exception";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700279 rc = -1;
280 goto errout;
281 }
282
283 if (read_ext(data.realfd, 0) != 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800284 LOG(ERROR) << "Failed to read ext4 extent";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700285 rc = -1;
286 goto errout;
287 }
288
Eric Biggersc01995e2020-11-03 14:11:00 -0800289 data.blocks_already_done = 0;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700290
Paul Crowley772cc852018-02-01 09:53:27 -0800291 LOG(INFO) << "Encrypting ext4 filesystem in place...";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700292
Eric Biggersc01995e2020-11-03 14:11:00 -0800293 data.tot_used_blocks = size / CRYPT_SECTORS_PER_BUFSIZE;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700294 for (i = 0; i < aux_info.groups; ++i) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700295 data.tot_used_blocks -= aux_info.bg_desc[i].bg_free_blocks_count;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700296 }
297
298 data.one_pct = data.tot_used_blocks / 100;
299 data.cur_pct = 0;
300
301 if (clock_gettime(CLOCK_MONOTONIC, &time_started)) {
Paul Crowley772cc852018-02-01 09:53:27 -0800302 LOG(WARNING) << "Error getting time at start";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700303 // Note - continue anyway - we'll run with 0
304 }
305 data.time_started = time_started.tv_sec;
306 data.remaining_time = -1;
307
308 rc = encrypt_groups(&data);
309 if (rc) {
Paul Crowley772cc852018-02-01 09:53:27 -0800310 LOG(ERROR) << "Error encrypting groups";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700311 goto errout;
312 }
313
Paul Crowleyf71ace32016-06-02 11:01:19 -0700314 rc = 0;
315
316errout:
317 close(data.realfd);
318 close(data.cryptofd);
319
320 return rc;
321}
322
Paul Crowley14c8c072018-09-18 13:30:21 -0700323static void log_progress_f2fs(u64 block, bool completed) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700324 // Precondition - if completed data = 0 else data != 0
325
326 // Track progress so we can skip logging blocks
327 static u64 last_block = (u64)-1;
328
329 // Need to close existing 'Encrypting from' log?
330 if (completed || (last_block != (u64)-1 && block != last_block + 1)) {
Paul Crowley772cc852018-02-01 09:53:27 -0800331 LOG(INFO) << "Encrypted to block " << last_block;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700332 last_block = -1;
333 }
334
335 // Need to start new 'Encrypting from' log?
336 if (!completed && (last_block == (u64)-1 || block != last_block + 1)) {
Paul Crowley772cc852018-02-01 09:53:27 -0800337 LOG(INFO) << "Encrypting from block " << block;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700338 }
339
340 // Update offset
341 if (!completed) {
342 last_block = block;
343 }
344}
345
Paul Crowley14c8c072018-09-18 13:30:21 -0700346static int encrypt_one_block_f2fs(u64 pos, void* data) {
347 struct encryptGroupsData* priv_dat = (struct encryptGroupsData*)data;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700348
349 priv_dat->blocks_already_done = pos - 1;
350 update_progress(priv_dat, 1);
351
352 off64_t offset = pos * CRYPT_INPLACE_BUFSIZE;
353
354 if (pread64(priv_dat->realfd, priv_dat->buffer, CRYPT_INPLACE_BUFSIZE, offset) <= 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800355 LOG(ERROR) << "Error reading real_blkdev " << priv_dat->crypto_blkdev
356 << " for f2fs inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700357 return -1;
358 }
359
360 if (pwrite64(priv_dat->cryptofd, priv_dat->buffer, CRYPT_INPLACE_BUFSIZE, offset) <= 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800361 LOG(ERROR) << "Error writing crypto_blkdev " << priv_dat->crypto_blkdev
362 << " for f2fs inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700363 return -1;
364 } else {
365 log_progress_f2fs(pos, false);
366 }
367
368 return 0;
369}
370
Paul Lawrence236e5e82019-06-25 14:44:33 -0700371static int cryptfs_enable_inplace_f2fs(const char* crypto_blkdev, const char* real_blkdev,
Eric Biggersc01995e2020-11-03 14:11:00 -0800372 off64_t size, bool set_progress_properties) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700373 struct encryptGroupsData data;
Paul Crowley14c8c072018-09-18 13:30:21 -0700374 struct f2fs_info* f2fs_info = NULL;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700375 int rc = ENABLE_INPLACE_ERR_OTHER;
Denis Hsu1740eff2019-12-26 16:06:37 +0800376 struct timespec time_started = {0};
377
Paul Crowleyf71ace32016-06-02 11:01:19 -0700378 memset(&data, 0, sizeof(data));
379 data.real_blkdev = real_blkdev;
380 data.crypto_blkdev = crypto_blkdev;
Paul Crowley0fd26262018-01-30 09:48:19 -0800381 data.set_progress_properties = set_progress_properties;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700382 data.realfd = -1;
383 data.cryptofd = -1;
Paul Crowley14c8c072018-09-18 13:30:21 -0700384 if ((data.realfd = open64(real_blkdev, O_RDWR | O_CLOEXEC)) < 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800385 PLOG(ERROR) << "Error opening real_blkdev " << real_blkdev << " for f2fs inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700386 goto errout;
387 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700388 if ((data.cryptofd = open64(crypto_blkdev, O_WRONLY | O_CLOEXEC)) < 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800389 PLOG(ERROR) << "Error opening crypto_blkdev " << crypto_blkdev
390 << " for f2fs inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700391 rc = ENABLE_INPLACE_ERR_DEV;
392 goto errout;
393 }
394
395 f2fs_info = generate_f2fs_info(data.realfd);
Paul Crowley14c8c072018-09-18 13:30:21 -0700396 if (!f2fs_info) goto errout;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700397
Eric Biggersc01995e2020-11-03 14:11:00 -0800398 data.blocks_already_done = 0;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700399
400 data.tot_used_blocks = get_num_blocks_used(f2fs_info);
401
402 data.one_pct = data.tot_used_blocks / 100;
403 data.cur_pct = 0;
Denis Hsu1740eff2019-12-26 16:06:37 +0800404 if (clock_gettime(CLOCK_MONOTONIC, &time_started)) {
405 LOG(WARNING) << "Error getting time at start";
406 // Note - continue anyway - we'll run with 0
407 }
408 data.time_started = time_started.tv_sec;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700409 data.remaining_time = -1;
410
Denis Hsu1740eff2019-12-26 16:06:37 +0800411
Paul Crowley14c8c072018-09-18 13:30:21 -0700412 data.buffer = (char*)malloc(f2fs_info->block_size);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700413 if (!data.buffer) {
Paul Crowley772cc852018-02-01 09:53:27 -0800414 LOG(ERROR) << "Failed to allocate crypto buffer";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700415 goto errout;
416 }
417
418 data.count = 0;
419
420 /* Currently, this either runs to completion, or hits a nonrecoverable error */
421 rc = run_on_used_blocks(data.blocks_already_done, f2fs_info, &encrypt_one_block_f2fs, &data);
422
423 if (rc) {
Paul Crowley772cc852018-02-01 09:53:27 -0800424 LOG(ERROR) << "Error in running over f2fs blocks";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700425 rc = ENABLE_INPLACE_ERR_OTHER;
426 goto errout;
427 }
428
Paul Crowleyf71ace32016-06-02 11:01:19 -0700429 rc = 0;
430
431errout:
Paul Crowley772cc852018-02-01 09:53:27 -0800432 if (rc) LOG(ERROR) << "Failed to encrypt f2fs filesystem on " << real_blkdev;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700433
434 log_progress_f2fs(0, true);
435 free(f2fs_info);
436 free(data.buffer);
437 close(data.realfd);
438 close(data.cryptofd);
439
440 return rc;
441}
442
Paul Lawrence236e5e82019-06-25 14:44:33 -0700443static int cryptfs_enable_inplace_full(const char* crypto_blkdev, const char* real_blkdev,
Eric Biggersc01995e2020-11-03 14:11:00 -0800444 off64_t size, bool set_progress_properties) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700445 int realfd, cryptofd;
Paul Crowley14c8c072018-09-18 13:30:21 -0700446 char* buf[CRYPT_INPLACE_BUFSIZE];
Paul Crowleyf71ace32016-06-02 11:01:19 -0700447 int rc = ENABLE_INPLACE_ERR_OTHER;
448 off64_t numblocks, i, remainder;
449 off64_t one_pct, cur_pct, new_pct;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700450
Paul Crowley14c8c072018-09-18 13:30:21 -0700451 if ((realfd = open(real_blkdev, O_RDONLY | O_CLOEXEC)) < 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800452 PLOG(ERROR) << "Error opening real_blkdev " << real_blkdev << " for inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700453 return ENABLE_INPLACE_ERR_OTHER;
454 }
455
Paul Crowley14c8c072018-09-18 13:30:21 -0700456 if ((cryptofd = open(crypto_blkdev, O_WRONLY | O_CLOEXEC)) < 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800457 PLOG(ERROR) << "Error opening crypto_blkdev " << crypto_blkdev << " for inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700458 close(realfd);
459 return ENABLE_INPLACE_ERR_DEV;
460 }
461
462 /* This is pretty much a simple loop of reading 4K, and writing 4K.
463 * The size passed in is the number of 512 byte sectors in the filesystem.
464 * So compute the number of whole 4K blocks we should read/write,
465 * and the remainder.
466 */
467 numblocks = size / CRYPT_SECTORS_PER_BUFSIZE;
468 remainder = size % CRYPT_SECTORS_PER_BUFSIZE;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700469
Paul Crowley772cc852018-02-01 09:53:27 -0800470 LOG(ERROR) << "Encrypting filesystem in place...";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700471
Eric Biggersc01995e2020-11-03 14:11:00 -0800472 one_pct = numblocks / 100;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700473 cur_pct = 0;
474 /* process the majority of the filesystem in blocks */
Eric Biggersc01995e2020-11-03 14:11:00 -0800475 for (i = 0; i < numblocks; i++) {
476 new_pct = i / one_pct;
Paul Crowley0fd26262018-01-30 09:48:19 -0800477 if (set_progress_properties && new_pct > cur_pct) {
Greg Kaisere0691cc2018-12-18 12:34:07 -0800478 char property_buf[8];
Paul Crowleyf71ace32016-06-02 11:01:19 -0700479
480 cur_pct = new_pct;
Greg Kaisere0691cc2018-12-18 12:34:07 -0800481 snprintf(property_buf, sizeof(property_buf), "%" PRId64, cur_pct);
482 android::base::SetProperty("vold.encrypt_progress", property_buf);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700483 }
484 if (unix_read(realfd, buf, CRYPT_INPLACE_BUFSIZE) <= 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800485 PLOG(ERROR) << "Error reading real_blkdev " << real_blkdev << " for inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700486 goto errout;
487 }
488 if (unix_write(cryptofd, buf, CRYPT_INPLACE_BUFSIZE) <= 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800489 PLOG(ERROR) << "Error writing crypto_blkdev " << crypto_blkdev << " for inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700490 goto errout;
491 } else {
Paul Crowley772cc852018-02-01 09:53:27 -0800492 LOG(DEBUG) << "Encrypted " << CRYPT_SECTORS_PER_BUFSIZE << " block at "
493 << i * CRYPT_SECTORS_PER_BUFSIZE;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700494 }
Paul Crowleyf71ace32016-06-02 11:01:19 -0700495 }
496
497 /* Do any remaining sectors */
Paul Crowley14c8c072018-09-18 13:30:21 -0700498 for (i = 0; i < remainder; i++) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700499 if (unix_read(realfd, buf, CRYPT_SECTOR_SIZE) <= 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800500 LOG(ERROR) << "Error reading final sectors from real_blkdev " << real_blkdev
501 << " for inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700502 goto errout;
503 }
504 if (unix_write(cryptofd, buf, CRYPT_SECTOR_SIZE) <= 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800505 LOG(ERROR) << "Error writing final sectors to crypto_blkdev " << crypto_blkdev
506 << " for inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700507 goto errout;
508 } else {
Paul Crowley772cc852018-02-01 09:53:27 -0800509 LOG(INFO) << "Encrypted 1 block at next location";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700510 }
511 }
512
Paul Crowleyf71ace32016-06-02 11:01:19 -0700513 rc = 0;
514
515errout:
516 close(realfd);
517 close(cryptofd);
518
519 return rc;
520}
521
522/* returns on of the ENABLE_INPLACE_* return codes */
Paul Lawrence236e5e82019-06-25 14:44:33 -0700523int cryptfs_enable_inplace(const char* crypto_blkdev, const char* real_blkdev, off64_t size,
Eric Biggersc01995e2020-11-03 14:11:00 -0800524 bool set_progress_properties) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700525 int rc_ext4, rc_f2fs, rc_full;
Paul Crowley0fd26262018-01-30 09:48:19 -0800526 LOG(DEBUG) << "cryptfs_enable_inplace(" << crypto_blkdev << ", " << real_blkdev << ", " << size
Paul Crowley0fd26262018-01-30 09:48:19 -0800527 << ", " << set_progress_properties << ")";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700528
529 /* TODO: identify filesystem type.
530 * As is, cryptfs_enable_inplace_ext4 will fail on an f2fs partition, and
531 * then we will drop down to cryptfs_enable_inplace_f2fs.
532 * */
Eric Biggersc01995e2020-11-03 14:11:00 -0800533 if ((rc_ext4 = cryptfs_enable_inplace_ext4(crypto_blkdev, real_blkdev, size,
Paul Crowley0fd26262018-01-30 09:48:19 -0800534 set_progress_properties)) == 0) {
535 LOG(DEBUG) << "cryptfs_enable_inplace_ext4 success";
536 return 0;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700537 }
Paul Crowley772cc852018-02-01 09:53:27 -0800538 LOG(DEBUG) << "cryptfs_enable_inplace_ext4()=" << rc_ext4;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700539
Eric Biggersc01995e2020-11-03 14:11:00 -0800540 if ((rc_f2fs = cryptfs_enable_inplace_f2fs(crypto_blkdev, real_blkdev, size,
Paul Crowley0fd26262018-01-30 09:48:19 -0800541 set_progress_properties)) == 0) {
542 LOG(DEBUG) << "cryptfs_enable_inplace_f2fs success";
543 return 0;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700544 }
Paul Crowley772cc852018-02-01 09:53:27 -0800545 LOG(DEBUG) << "cryptfs_enable_inplace_f2fs()=" << rc_f2fs;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700546
Paul Crowley0fd26262018-01-30 09:48:19 -0800547 rc_full =
Eric Biggersc01995e2020-11-03 14:11:00 -0800548 cryptfs_enable_inplace_full(crypto_blkdev, real_blkdev, size, set_progress_properties);
Paul Crowley772cc852018-02-01 09:53:27 -0800549 LOG(DEBUG) << "cryptfs_enable_inplace_full()=" << rc_full;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700550
551 /* Hack for b/17898962, the following is the symptom... */
Paul Crowley14c8c072018-09-18 13:30:21 -0700552 if (rc_ext4 == ENABLE_INPLACE_ERR_DEV && rc_f2fs == ENABLE_INPLACE_ERR_DEV &&
553 rc_full == ENABLE_INPLACE_ERR_DEV) {
Paul Crowley0fd26262018-01-30 09:48:19 -0800554 LOG(DEBUG) << "ENABLE_INPLACE_ERR_DEV";
555 return ENABLE_INPLACE_ERR_DEV;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700556 }
557 return rc_full;
558}