blob: bdb2da76f75d660517a6dfb8aec3e94a7c0d8e46 [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
35// HORRIBLE HACK, FIXME
36#include "cryptfs.h"
37
38// FIXME horrible cut-and-paste code
Paul Crowley14c8c072018-09-18 13:30:21 -070039static inline int unix_read(int fd, void* buff, int len) {
Paul Crowleyf71ace32016-06-02 11:01:19 -070040 return TEMP_FAILURE_RETRY(read(fd, buff, len));
41}
42
Paul Crowley14c8c072018-09-18 13:30:21 -070043static inline int unix_write(int fd, const void* buff, int len) {
Paul Crowleyf71ace32016-06-02 11:01:19 -070044 return TEMP_FAILURE_RETRY(write(fd, buff, len));
45}
46
47#define CRYPT_SECTORS_PER_BUFSIZE (CRYPT_INPLACE_BUFSIZE / CRYPT_SECTOR_SIZE)
48
49/* aligned 32K writes tends to make flash happy.
50 * SD card association recommends it.
51 */
Paul Crowleyf71ace32016-06-02 11:01:19 -070052#define BLOCKS_AT_A_TIME 8
Paul Crowleyf71ace32016-06-02 11:01:19 -070053
Paul Crowley14c8c072018-09-18 13:30:21 -070054struct encryptGroupsData {
Paul Crowleyf71ace32016-06-02 11:01:19 -070055 int realfd;
56 int cryptofd;
57 off64_t numblocks;
58 off64_t one_pct, cur_pct, new_pct;
59 off64_t blocks_already_done, tot_numblocks;
60 off64_t used_blocks_already_done, tot_used_blocks;
Paul Lawrence236e5e82019-06-25 14:44:33 -070061 const char* real_blkdev;
62 const char* crypto_blkdev;
Paul Crowleyf71ace32016-06-02 11:01:19 -070063 int count;
64 off64_t offset;
65 char* buffer;
66 off64_t last_written_sector;
67 int completed;
68 time_t time_started;
69 int remaining_time;
Paul Crowley0fd26262018-01-30 09:48:19 -080070 bool set_progress_properties;
Paul Crowleyf71ace32016-06-02 11:01:19 -070071};
72
Paul Crowley14c8c072018-09-18 13:30:21 -070073static void update_progress(struct encryptGroupsData* data, int is_used) {
Paul Crowleyf71ace32016-06-02 11:01:19 -070074 data->blocks_already_done++;
75
76 if (is_used) {
77 data->used_blocks_already_done++;
78 }
79 if (data->tot_used_blocks) {
80 data->new_pct = data->used_blocks_already_done / data->one_pct;
81 } else {
82 data->new_pct = data->blocks_already_done / data->one_pct;
83 }
84
Paul Crowley0fd26262018-01-30 09:48:19 -080085 if (!data->set_progress_properties) return;
86
Paul Crowleyf71ace32016-06-02 11:01:19 -070087 if (data->new_pct > data->cur_pct) {
88 char buf[8];
89 data->cur_pct = data->new_pct;
90 snprintf(buf, sizeof(buf), "%" PRId64, data->cur_pct);
Paul Crowley772cc852018-02-01 09:53:27 -080091 android::base::SetProperty("vold.encrypt_progress", buf);
Paul Crowleyf71ace32016-06-02 11:01:19 -070092 }
93
94 if (data->cur_pct >= 5) {
95 struct timespec time_now;
96 if (clock_gettime(CLOCK_MONOTONIC, &time_now)) {
Paul Crowley772cc852018-02-01 09:53:27 -080097 LOG(WARNING) << "Error getting time";
Paul Crowleyf71ace32016-06-02 11:01:19 -070098 } else {
99 double elapsed_time = difftime(time_now.tv_sec, data->time_started);
Paul Crowley14c8c072018-09-18 13:30:21 -0700100 off64_t remaining_blocks = data->tot_used_blocks - data->used_blocks_already_done;
101 int remaining_time =
102 (int)(elapsed_time * remaining_blocks / data->used_blocks_already_done);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700103
104 // Change time only if not yet set, lower, or a lot higher for
105 // best user experience
Paul Crowley14c8c072018-09-18 13:30:21 -0700106 if (data->remaining_time == -1 || remaining_time < data->remaining_time ||
107 remaining_time > data->remaining_time + 60) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700108 char buf[8];
109 snprintf(buf, sizeof(buf), "%d", remaining_time);
Paul Crowley772cc852018-02-01 09:53:27 -0800110 android::base::SetProperty("vold.encrypt_time_remaining", buf);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700111 data->remaining_time = remaining_time;
112 }
113 }
114 }
115}
116
Paul Crowley14c8c072018-09-18 13:30:21 -0700117static void log_progress(struct encryptGroupsData const* data, bool completed) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700118 // Precondition - if completed data = 0 else data != 0
119
120 // Track progress so we can skip logging blocks
121 static off64_t offset = -1;
122
123 // Need to close existing 'Encrypting from' log?
124 if (completed || (offset != -1 && data->offset != offset)) {
Paul Crowley772cc852018-02-01 09:53:27 -0800125 LOG(INFO) << "Encrypted to sector " << offset / info.block_size * CRYPT_SECTOR_SIZE;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700126 offset = -1;
127 }
128
129 // Need to start new 'Encrypting from' log?
130 if (!completed && offset != data->offset) {
Paul Crowley772cc852018-02-01 09:53:27 -0800131 LOG(INFO) << "Encrypting from sector " << data->offset / info.block_size * CRYPT_SECTOR_SIZE;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700132 }
133
134 // Update offset
135 if (!completed) {
136 offset = data->offset + (off64_t)data->count * info.block_size;
137 }
138}
139
Paul Crowley14c8c072018-09-18 13:30:21 -0700140static int flush_outstanding_data(struct encryptGroupsData* data) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700141 if (data->count == 0) {
142 return 0;
143 }
144
Sudheer Shanka4b6ca4e2018-09-21 10:54:54 -0700145 LOG(DEBUG) << "Copying " << data->count << " blocks at offset " << data->offset;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700146
Paul Crowley772cc852018-02-01 09:53:27 -0800147 if (pread64(data->realfd, data->buffer, info.block_size * data->count, data->offset) <= 0) {
148 LOG(ERROR) << "Error reading real_blkdev " << data->real_blkdev << " for inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700149 return -1;
150 }
151
Paul Crowley772cc852018-02-01 09:53:27 -0800152 if (pwrite64(data->cryptofd, data->buffer, info.block_size * data->count, data->offset) <= 0) {
153 LOG(ERROR) << "Error writing crypto_blkdev " << data->crypto_blkdev
154 << " for inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700155 return -1;
156 } else {
Paul Crowley14c8c072018-09-18 13:30:21 -0700157 log_progress(data, false);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700158 }
159
160 data->count = 0;
Paul Crowley14c8c072018-09-18 13:30:21 -0700161 data->last_written_sector =
162 (data->offset + data->count) / info.block_size * CRYPT_SECTOR_SIZE - 1;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700163 return 0;
164}
165
Paul Crowley14c8c072018-09-18 13:30:21 -0700166static int encrypt_groups(struct encryptGroupsData* data) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700167 unsigned int i;
Paul Crowley14c8c072018-09-18 13:30:21 -0700168 u8* block_bitmap = 0;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700169 unsigned int block;
170 off64_t ret;
171 int rc = -1;
172
Paul Crowley14c8c072018-09-18 13:30:21 -0700173 data->buffer = (char*)malloc(info.block_size * BLOCKS_AT_A_TIME);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700174 if (!data->buffer) {
Paul Crowley772cc852018-02-01 09:53:27 -0800175 LOG(ERROR) << "Failed to allocate crypto buffer";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700176 goto errout;
177 }
178
Paul Crowley14c8c072018-09-18 13:30:21 -0700179 block_bitmap = (u8*)malloc(info.block_size);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700180 if (!block_bitmap) {
Paul Crowley772cc852018-02-01 09:53:27 -0800181 LOG(ERROR) << "failed to allocate block bitmap";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700182 goto errout;
183 }
184
185 for (i = 0; i < aux_info.groups; ++i) {
Paul Crowley772cc852018-02-01 09:53:27 -0800186 LOG(INFO) << "Encrypting group " << i;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700187
188 u32 first_block = aux_info.first_data_block + i * info.blocks_per_group;
Paul Crowley14c8c072018-09-18 13:30:21 -0700189 u32 block_count = std::min(info.blocks_per_group, (u32)(aux_info.len_blocks - first_block));
Paul Crowleyf71ace32016-06-02 11:01:19 -0700190
Paul Crowley14c8c072018-09-18 13:30:21 -0700191 off64_t offset = (u64)info.block_size * aux_info.bg_desc[i].bg_block_bitmap;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700192
193 ret = pread64(data->realfd, block_bitmap, info.block_size, offset);
194 if (ret != (int)info.block_size) {
Paul Crowley772cc852018-02-01 09:53:27 -0800195 LOG(ERROR) << "failed to read all of block group bitmap " << i;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700196 goto errout;
197 }
198
199 offset = (u64)info.block_size * first_block;
200
201 data->count = 0;
202
203 for (block = 0; block < block_count; block++) {
Will Shiu4ac43f02020-07-29 17:03:17 +0800204 int used;
205
206 if (aux_info.bg_desc[i].bg_flags & EXT4_BG_BLOCK_UNINIT) {
207 // In block groups with an uninitialized block bitmap, we only
208 // need to encrypt the backup superblock (if one is present).
209 used = (ext4_bg_has_super_block(i) && block < 1 + aux_info.bg_desc_blocks);
210 } else {
211 used = bitmap_get_bit(block_bitmap, block);
212 }
213
Paul Crowleyf71ace32016-06-02 11:01:19 -0700214 update_progress(data, used);
215 if (used) {
216 if (data->count == 0) {
217 data->offset = offset;
218 }
219 data->count++;
220 } else {
221 if (flush_outstanding_data(data)) {
222 goto errout;
223 }
224 }
225
226 offset += info.block_size;
227
228 /* Write data if we are aligned or buffer size reached */
Paul Crowley14c8c072018-09-18 13:30:21 -0700229 if (offset % (info.block_size * BLOCKS_AT_A_TIME) == 0 ||
230 data->count == BLOCKS_AT_A_TIME) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700231 if (flush_outstanding_data(data)) {
232 goto errout;
233 }
234 }
Paul Crowleyf71ace32016-06-02 11:01:19 -0700235 }
236 if (flush_outstanding_data(data)) {
237 goto errout;
238 }
239 }
240
241 data->completed = 1;
242 rc = 0;
243
244errout:
245 log_progress(0, true);
246 free(data->buffer);
247 free(block_bitmap);
248 return rc;
249}
250
Paul Lawrence236e5e82019-06-25 14:44:33 -0700251static int cryptfs_enable_inplace_ext4(const char* crypto_blkdev, const char* real_blkdev,
252 off64_t size, off64_t* size_already_done, off64_t tot_size,
Paul Crowley0fd26262018-01-30 09:48:19 -0800253 off64_t previously_encrypted_upto,
254 bool set_progress_properties) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700255 u32 i;
256 struct encryptGroupsData data;
Paul Crowley14c8c072018-09-18 13:30:21 -0700257 int rc; // Can't initialize without causing warning -Wclobbered
Paul Crowleyf71ace32016-06-02 11:01:19 -0700258 int retries = RETRY_MOUNT_ATTEMPTS;
259 struct timespec time_started = {0};
260
261 if (previously_encrypted_upto > *size_already_done) {
Paul Crowley772cc852018-02-01 09:53:27 -0800262 LOG(DEBUG) << "Not fast encrypting since resuming part way through";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700263 return -1;
264 }
265
266 memset(&data, 0, sizeof(data));
267 data.real_blkdev = real_blkdev;
268 data.crypto_blkdev = crypto_blkdev;
Paul Crowley0fd26262018-01-30 09:48:19 -0800269 data.set_progress_properties = set_progress_properties;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700270
Paul Crowley0fd26262018-01-30 09:48:19 -0800271 LOG(DEBUG) << "Opening" << real_blkdev;
Paul Crowley14c8c072018-09-18 13:30:21 -0700272 if ((data.realfd = open(real_blkdev, O_RDWR | O_CLOEXEC)) < 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800273 PLOG(ERROR) << "Error opening real_blkdev " << real_blkdev << " for inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700274 rc = -1;
275 goto errout;
276 }
277
Paul Crowley0fd26262018-01-30 09:48:19 -0800278 LOG(DEBUG) << "Opening" << crypto_blkdev;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700279 // Wait until the block device appears. Re-use the mount retry values since it is reasonable.
Paul Crowley14c8c072018-09-18 13:30:21 -0700280 while ((data.cryptofd = open(crypto_blkdev, O_WRONLY | O_CLOEXEC)) < 0) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700281 if (--retries) {
Paul Crowley772cc852018-02-01 09:53:27 -0800282 PLOG(ERROR) << "Error opening crypto_blkdev " << crypto_blkdev
283 << " for ext4 inplace encrypt, retrying";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700284 sleep(RETRY_MOUNT_DELAY_SECONDS);
285 } else {
Paul Crowley772cc852018-02-01 09:53:27 -0800286 PLOG(ERROR) << "Error opening crypto_blkdev " << crypto_blkdev
287 << " for ext4 inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700288 rc = ENABLE_INPLACE_ERR_DEV;
289 goto errout;
290 }
291 }
292
Paul Crowley14c8c072018-09-18 13:30:21 -0700293 if (setjmp(setjmp_env)) { // NOLINT
Paul Crowley772cc852018-02-01 09:53:27 -0800294 LOG(ERROR) << "Reading ext4 extent caused an exception";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700295 rc = -1;
296 goto errout;
297 }
298
299 if (read_ext(data.realfd, 0) != 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800300 LOG(ERROR) << "Failed to read ext4 extent";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700301 rc = -1;
302 goto errout;
303 }
304
305 data.numblocks = size / CRYPT_SECTORS_PER_BUFSIZE;
306 data.tot_numblocks = tot_size / CRYPT_SECTORS_PER_BUFSIZE;
307 data.blocks_already_done = *size_already_done / CRYPT_SECTORS_PER_BUFSIZE;
308
Paul Crowley772cc852018-02-01 09:53:27 -0800309 LOG(INFO) << "Encrypting ext4 filesystem in place...";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700310
311 data.tot_used_blocks = data.numblocks;
312 for (i = 0; i < aux_info.groups; ++i) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700313 data.tot_used_blocks -= aux_info.bg_desc[i].bg_free_blocks_count;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700314 }
315
316 data.one_pct = data.tot_used_blocks / 100;
317 data.cur_pct = 0;
318
319 if (clock_gettime(CLOCK_MONOTONIC, &time_started)) {
Paul Crowley772cc852018-02-01 09:53:27 -0800320 LOG(WARNING) << "Error getting time at start";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700321 // Note - continue anyway - we'll run with 0
322 }
323 data.time_started = time_started.tv_sec;
324 data.remaining_time = -1;
325
326 rc = encrypt_groups(&data);
327 if (rc) {
Paul Crowley772cc852018-02-01 09:53:27 -0800328 LOG(ERROR) << "Error encrypting groups";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700329 goto errout;
330 }
331
332 *size_already_done += data.completed ? size : data.last_written_sector;
333 rc = 0;
334
335errout:
336 close(data.realfd);
337 close(data.cryptofd);
338
339 return rc;
340}
341
Paul Crowley14c8c072018-09-18 13:30:21 -0700342static void log_progress_f2fs(u64 block, bool completed) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700343 // Precondition - if completed data = 0 else data != 0
344
345 // Track progress so we can skip logging blocks
346 static u64 last_block = (u64)-1;
347
348 // Need to close existing 'Encrypting from' log?
349 if (completed || (last_block != (u64)-1 && block != last_block + 1)) {
Paul Crowley772cc852018-02-01 09:53:27 -0800350 LOG(INFO) << "Encrypted to block " << last_block;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700351 last_block = -1;
352 }
353
354 // Need to start new 'Encrypting from' log?
355 if (!completed && (last_block == (u64)-1 || block != last_block + 1)) {
Paul Crowley772cc852018-02-01 09:53:27 -0800356 LOG(INFO) << "Encrypting from block " << block;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700357 }
358
359 // Update offset
360 if (!completed) {
361 last_block = block;
362 }
363}
364
Paul Crowley14c8c072018-09-18 13:30:21 -0700365static int encrypt_one_block_f2fs(u64 pos, void* data) {
366 struct encryptGroupsData* priv_dat = (struct encryptGroupsData*)data;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700367
368 priv_dat->blocks_already_done = pos - 1;
369 update_progress(priv_dat, 1);
370
371 off64_t offset = pos * CRYPT_INPLACE_BUFSIZE;
372
373 if (pread64(priv_dat->realfd, priv_dat->buffer, CRYPT_INPLACE_BUFSIZE, offset) <= 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800374 LOG(ERROR) << "Error reading real_blkdev " << priv_dat->crypto_blkdev
375 << " for f2fs inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700376 return -1;
377 }
378
379 if (pwrite64(priv_dat->cryptofd, priv_dat->buffer, CRYPT_INPLACE_BUFSIZE, offset) <= 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800380 LOG(ERROR) << "Error writing crypto_blkdev " << priv_dat->crypto_blkdev
381 << " for f2fs inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700382 return -1;
383 } else {
384 log_progress_f2fs(pos, false);
385 }
386
387 return 0;
388}
389
Paul Lawrence236e5e82019-06-25 14:44:33 -0700390static int cryptfs_enable_inplace_f2fs(const char* crypto_blkdev, const char* real_blkdev,
391 off64_t size, off64_t* size_already_done, off64_t tot_size,
Paul Crowley0fd26262018-01-30 09:48:19 -0800392 off64_t previously_encrypted_upto,
393 bool set_progress_properties) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700394 struct encryptGroupsData data;
Paul Crowley14c8c072018-09-18 13:30:21 -0700395 struct f2fs_info* f2fs_info = NULL;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700396 int rc = ENABLE_INPLACE_ERR_OTHER;
Denis Hsu1740eff2019-12-26 16:06:37 +0800397 struct timespec time_started = {0};
398
Paul Crowleyf71ace32016-06-02 11:01:19 -0700399 if (previously_encrypted_upto > *size_already_done) {
Paul Crowley772cc852018-02-01 09:53:27 -0800400 LOG(DEBUG) << "Not fast encrypting since resuming part way through";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700401 return ENABLE_INPLACE_ERR_OTHER;
402 }
403 memset(&data, 0, sizeof(data));
404 data.real_blkdev = real_blkdev;
405 data.crypto_blkdev = crypto_blkdev;
Paul Crowley0fd26262018-01-30 09:48:19 -0800406 data.set_progress_properties = set_progress_properties;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700407 data.realfd = -1;
408 data.cryptofd = -1;
Paul Crowley14c8c072018-09-18 13:30:21 -0700409 if ((data.realfd = open64(real_blkdev, O_RDWR | O_CLOEXEC)) < 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800410 PLOG(ERROR) << "Error opening real_blkdev " << real_blkdev << " for f2fs inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700411 goto errout;
412 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700413 if ((data.cryptofd = open64(crypto_blkdev, O_WRONLY | O_CLOEXEC)) < 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800414 PLOG(ERROR) << "Error opening crypto_blkdev " << crypto_blkdev
415 << " for f2fs inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700416 rc = ENABLE_INPLACE_ERR_DEV;
417 goto errout;
418 }
419
420 f2fs_info = generate_f2fs_info(data.realfd);
Paul Crowley14c8c072018-09-18 13:30:21 -0700421 if (!f2fs_info) goto errout;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700422
423 data.numblocks = size / CRYPT_SECTORS_PER_BUFSIZE;
424 data.tot_numblocks = tot_size / CRYPT_SECTORS_PER_BUFSIZE;
425 data.blocks_already_done = *size_already_done / CRYPT_SECTORS_PER_BUFSIZE;
426
427 data.tot_used_blocks = get_num_blocks_used(f2fs_info);
428
429 data.one_pct = data.tot_used_blocks / 100;
430 data.cur_pct = 0;
Denis Hsu1740eff2019-12-26 16:06:37 +0800431 if (clock_gettime(CLOCK_MONOTONIC, &time_started)) {
432 LOG(WARNING) << "Error getting time at start";
433 // Note - continue anyway - we'll run with 0
434 }
435 data.time_started = time_started.tv_sec;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700436 data.remaining_time = -1;
437
Denis Hsu1740eff2019-12-26 16:06:37 +0800438
Paul Crowley14c8c072018-09-18 13:30:21 -0700439 data.buffer = (char*)malloc(f2fs_info->block_size);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700440 if (!data.buffer) {
Paul Crowley772cc852018-02-01 09:53:27 -0800441 LOG(ERROR) << "Failed to allocate crypto buffer";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700442 goto errout;
443 }
444
445 data.count = 0;
446
447 /* Currently, this either runs to completion, or hits a nonrecoverable error */
448 rc = run_on_used_blocks(data.blocks_already_done, f2fs_info, &encrypt_one_block_f2fs, &data);
449
450 if (rc) {
Paul Crowley772cc852018-02-01 09:53:27 -0800451 LOG(ERROR) << "Error in running over f2fs blocks";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700452 rc = ENABLE_INPLACE_ERR_OTHER;
453 goto errout;
454 }
455
456 *size_already_done += size;
457 rc = 0;
458
459errout:
Paul Crowley772cc852018-02-01 09:53:27 -0800460 if (rc) LOG(ERROR) << "Failed to encrypt f2fs filesystem on " << real_blkdev;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700461
462 log_progress_f2fs(0, true);
463 free(f2fs_info);
464 free(data.buffer);
465 close(data.realfd);
466 close(data.cryptofd);
467
468 return rc;
469}
470
Paul Lawrence236e5e82019-06-25 14:44:33 -0700471static int cryptfs_enable_inplace_full(const char* crypto_blkdev, const char* real_blkdev,
472 off64_t size, off64_t* size_already_done, off64_t tot_size,
Paul Crowley0fd26262018-01-30 09:48:19 -0800473 off64_t previously_encrypted_upto,
474 bool set_progress_properties) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700475 int realfd, cryptofd;
Paul Crowley14c8c072018-09-18 13:30:21 -0700476 char* buf[CRYPT_INPLACE_BUFSIZE];
Paul Crowleyf71ace32016-06-02 11:01:19 -0700477 int rc = ENABLE_INPLACE_ERR_OTHER;
478 off64_t numblocks, i, remainder;
479 off64_t one_pct, cur_pct, new_pct;
480 off64_t blocks_already_done, tot_numblocks;
481
Paul Crowley14c8c072018-09-18 13:30:21 -0700482 if ((realfd = open(real_blkdev, O_RDONLY | O_CLOEXEC)) < 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800483 PLOG(ERROR) << "Error opening real_blkdev " << real_blkdev << " for inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700484 return ENABLE_INPLACE_ERR_OTHER;
485 }
486
Paul Crowley14c8c072018-09-18 13:30:21 -0700487 if ((cryptofd = open(crypto_blkdev, O_WRONLY | O_CLOEXEC)) < 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800488 PLOG(ERROR) << "Error opening crypto_blkdev " << crypto_blkdev << " for inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700489 close(realfd);
490 return ENABLE_INPLACE_ERR_DEV;
491 }
492
493 /* This is pretty much a simple loop of reading 4K, and writing 4K.
494 * The size passed in is the number of 512 byte sectors in the filesystem.
495 * So compute the number of whole 4K blocks we should read/write,
496 * and the remainder.
497 */
498 numblocks = size / CRYPT_SECTORS_PER_BUFSIZE;
499 remainder = size % CRYPT_SECTORS_PER_BUFSIZE;
500 tot_numblocks = tot_size / CRYPT_SECTORS_PER_BUFSIZE;
501 blocks_already_done = *size_already_done / CRYPT_SECTORS_PER_BUFSIZE;
502
Paul Crowley772cc852018-02-01 09:53:27 -0800503 LOG(ERROR) << "Encrypting filesystem in place...";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700504
505 i = previously_encrypted_upto + 1 - *size_already_done;
506
507 if (lseek64(realfd, i * CRYPT_SECTOR_SIZE, SEEK_SET) < 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800508 PLOG(ERROR) << "Cannot seek to previously encrypted point on " << real_blkdev;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700509 goto errout;
510 }
511
512 if (lseek64(cryptofd, i * CRYPT_SECTOR_SIZE, SEEK_SET) < 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800513 PLOG(ERROR) << "Cannot seek to previously encrypted point on " << crypto_blkdev;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700514 goto errout;
515 }
516
Paul Crowley14c8c072018-09-18 13:30:21 -0700517 for (; i < size && i % CRYPT_SECTORS_PER_BUFSIZE != 0; ++i) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700518 if (unix_read(realfd, buf, CRYPT_SECTOR_SIZE) <= 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800519 PLOG(ERROR) << "Error reading initial sectors from real_blkdev " << real_blkdev
520 << " for inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700521 goto errout;
522 }
523 if (unix_write(cryptofd, buf, CRYPT_SECTOR_SIZE) <= 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800524 PLOG(ERROR) << "Error writing initial sectors to crypto_blkdev " << crypto_blkdev
525 << " for inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700526 goto errout;
527 } else {
Paul Crowley772cc852018-02-01 09:53:27 -0800528 LOG(INFO) << "Encrypted 1 block at " << i;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700529 }
530 }
531
532 one_pct = tot_numblocks / 100;
533 cur_pct = 0;
534 /* process the majority of the filesystem in blocks */
Paul Crowley14c8c072018-09-18 13:30:21 -0700535 for (i /= CRYPT_SECTORS_PER_BUFSIZE; i < numblocks; i++) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700536 new_pct = (i + blocks_already_done) / one_pct;
Paul Crowley0fd26262018-01-30 09:48:19 -0800537 if (set_progress_properties && new_pct > cur_pct) {
Greg Kaisere0691cc2018-12-18 12:34:07 -0800538 char property_buf[8];
Paul Crowleyf71ace32016-06-02 11:01:19 -0700539
540 cur_pct = new_pct;
Greg Kaisere0691cc2018-12-18 12:34:07 -0800541 snprintf(property_buf, sizeof(property_buf), "%" PRId64, cur_pct);
542 android::base::SetProperty("vold.encrypt_progress", property_buf);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700543 }
544 if (unix_read(realfd, buf, CRYPT_INPLACE_BUFSIZE) <= 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800545 PLOG(ERROR) << "Error reading real_blkdev " << real_blkdev << " for inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700546 goto errout;
547 }
548 if (unix_write(cryptofd, buf, CRYPT_INPLACE_BUFSIZE) <= 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800549 PLOG(ERROR) << "Error writing crypto_blkdev " << crypto_blkdev << " for inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700550 goto errout;
551 } else {
Paul Crowley772cc852018-02-01 09:53:27 -0800552 LOG(DEBUG) << "Encrypted " << CRYPT_SECTORS_PER_BUFSIZE << " block at "
553 << i * CRYPT_SECTORS_PER_BUFSIZE;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700554 }
Paul Crowleyf71ace32016-06-02 11:01:19 -0700555 }
556
557 /* Do any remaining sectors */
Paul Crowley14c8c072018-09-18 13:30:21 -0700558 for (i = 0; i < remainder; i++) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700559 if (unix_read(realfd, buf, CRYPT_SECTOR_SIZE) <= 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800560 LOG(ERROR) << "Error reading final sectors from real_blkdev " << real_blkdev
561 << " for inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700562 goto errout;
563 }
564 if (unix_write(cryptofd, buf, CRYPT_SECTOR_SIZE) <= 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800565 LOG(ERROR) << "Error writing final sectors to crypto_blkdev " << crypto_blkdev
566 << " for inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700567 goto errout;
568 } else {
Paul Crowley772cc852018-02-01 09:53:27 -0800569 LOG(INFO) << "Encrypted 1 block at next location";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700570 }
571 }
572
573 *size_already_done += size;
574 rc = 0;
575
576errout:
577 close(realfd);
578 close(cryptofd);
579
580 return rc;
581}
582
583/* returns on of the ENABLE_INPLACE_* return codes */
Paul Lawrence236e5e82019-06-25 14:44:33 -0700584int cryptfs_enable_inplace(const char* crypto_blkdev, const char* real_blkdev, off64_t size,
Paul Crowley772cc852018-02-01 09:53:27 -0800585 off64_t* size_already_done, off64_t tot_size,
Paul Crowley0fd26262018-01-30 09:48:19 -0800586 off64_t previously_encrypted_upto, bool set_progress_properties) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700587 int rc_ext4, rc_f2fs, rc_full;
Paul Crowley0fd26262018-01-30 09:48:19 -0800588 LOG(DEBUG) << "cryptfs_enable_inplace(" << crypto_blkdev << ", " << real_blkdev << ", " << size
589 << ", " << size_already_done << ", " << tot_size << ", " << previously_encrypted_upto
590 << ", " << set_progress_properties << ")";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700591 if (previously_encrypted_upto) {
Paul Crowley772cc852018-02-01 09:53:27 -0800592 LOG(DEBUG) << "Continuing encryption from " << previously_encrypted_upto;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700593 }
594
595 if (*size_already_done + size < previously_encrypted_upto) {
Paul Crowley0fd26262018-01-30 09:48:19 -0800596 LOG(DEBUG) << "cryptfs_enable_inplace already done";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700597 *size_already_done += size;
598 return 0;
599 }
600
601 /* TODO: identify filesystem type.
602 * As is, cryptfs_enable_inplace_ext4 will fail on an f2fs partition, and
603 * then we will drop down to cryptfs_enable_inplace_f2fs.
604 * */
Paul Crowley0fd26262018-01-30 09:48:19 -0800605 if ((rc_ext4 = cryptfs_enable_inplace_ext4(crypto_blkdev, real_blkdev, size, size_already_done,
606 tot_size, previously_encrypted_upto,
607 set_progress_properties)) == 0) {
608 LOG(DEBUG) << "cryptfs_enable_inplace_ext4 success";
609 return 0;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700610 }
Paul Crowley772cc852018-02-01 09:53:27 -0800611 LOG(DEBUG) << "cryptfs_enable_inplace_ext4()=" << rc_ext4;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700612
Paul Crowley0fd26262018-01-30 09:48:19 -0800613 if ((rc_f2fs = cryptfs_enable_inplace_f2fs(crypto_blkdev, real_blkdev, size, size_already_done,
614 tot_size, previously_encrypted_upto,
615 set_progress_properties)) == 0) {
616 LOG(DEBUG) << "cryptfs_enable_inplace_f2fs success";
617 return 0;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700618 }
Paul Crowley772cc852018-02-01 09:53:27 -0800619 LOG(DEBUG) << "cryptfs_enable_inplace_f2fs()=" << rc_f2fs;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700620
Paul Crowley0fd26262018-01-30 09:48:19 -0800621 rc_full =
622 cryptfs_enable_inplace_full(crypto_blkdev, real_blkdev, size, size_already_done, tot_size,
623 previously_encrypted_upto, set_progress_properties);
Paul Crowley772cc852018-02-01 09:53:27 -0800624 LOG(DEBUG) << "cryptfs_enable_inplace_full()=" << rc_full;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700625
626 /* Hack for b/17898962, the following is the symptom... */
Paul Crowley14c8c072018-09-18 13:30:21 -0700627 if (rc_ext4 == ENABLE_INPLACE_ERR_DEV && rc_f2fs == ENABLE_INPLACE_ERR_DEV &&
628 rc_full == ENABLE_INPLACE_ERR_DEV) {
Paul Crowley0fd26262018-01-30 09:48:19 -0800629 LOG(DEBUG) << "ENABLE_INPLACE_ERR_DEV";
630 return ENABLE_INPLACE_ERR_DEV;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700631 }
632 return rc_full;
633}